Skip to content

Commit d461324

Browse files
committed
linux: delete temp files as soon as they are not needed
Stop generating one file in /tmp for each icon change. Let's move the clean up to the set icon instead of waiting for the quit clean up.
1 parent 3fd1443 commit d461324

1 file changed

Lines changed: 13 additions & 16 deletions

File tree

systray_linux.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
static AppIndicator *global_app_indicator;
99
static GtkWidget *global_tray_menu = NULL;
1010
static GList *global_menu_items = NULL;
11-
// Keep track of all generated temp files to remove when app quits
12-
static GArray *global_temp_icon_file_names = NULL;
11+
static char temp_file_name[PATH_MAX] = "";
1312

1413
typedef struct {
1514
GtkWidget *menu_item;
@@ -31,24 +30,32 @@ int nativeLoop(void) {
3130
app_indicator_set_status(global_app_indicator, APP_INDICATOR_STATUS_ACTIVE);
3231
global_tray_menu = gtk_menu_new();
3332
app_indicator_set_menu(global_app_indicator, GTK_MENU(global_tray_menu));
34-
global_temp_icon_file_names = g_array_new(TRUE, FALSE, sizeof(char*));
3533
systray_ready();
3634
gtk_main();
3735
systray_on_exit();
3836
return 0;
3937
}
4038

39+
void _unlink_temp_file() {
40+
if (strlen(temp_file_name) != 0) {
41+
int ret = unlink(temp_file_name);
42+
if (ret == -1) {
43+
printf("failed to remove temp icon file %s: %s\n", temp_file_name, strerror(errno));
44+
}
45+
temp_file_name[0] = '\0';
46+
}
47+
}
48+
4149
// runs in main thread, should always return FALSE to prevent gtk to execute it again
4250
gboolean do_set_icon(gpointer data) {
4351
GBytes* bytes = (GBytes*)data;
44-
char* temp_file_name = malloc(PATH_MAX);
52+
_unlink_temp_file();
4553
strcpy(temp_file_name, "/tmp/systray_XXXXXX");
4654
int fd = mkstemp(temp_file_name);
4755
if (fd == -1) {
4856
printf("failed to create temp icon file %s: %s\n", temp_file_name, strerror(errno));
4957
return FALSE;
5058
}
51-
g_array_append_val(global_temp_icon_file_names, temp_file_name);
5259
gsize size = 0;
5360
gconstpointer icon_data = g_bytes_get_data(bytes, &size);
5461
ssize_t written = write(fd, icon_data, size);
@@ -146,17 +153,7 @@ gboolean do_show_menu_item(gpointer data) {
146153

147154
// runs in main thread, should always return FALSE to prevent gtk to execute it again
148155
gboolean do_quit(gpointer data) {
149-
int i;
150-
for (i = 0; i < INT_MAX; ++i) {
151-
char * temp_file_name = g_array_index(global_temp_icon_file_names, char*, i);
152-
if (temp_file_name == NULL) {
153-
break;
154-
}
155-
int ret = unlink(temp_file_name);
156-
if (ret == -1) {
157-
printf("failed to remove temp icon file %s: %s\n", temp_file_name, strerror(errno));
158-
}
159-
}
156+
_unlink_temp_file();
160157
// app indicator doesn't provide a way to remove it, hide it as a workaround
161158
app_indicator_set_status(global_app_indicator, APP_INDICATOR_STATUS_PASSIVE);
162159
gtk_main_quit();

0 commit comments

Comments
 (0)