Skip to content

Commit b139821

Browse files
A1029384756slouken
authored andcommitted
tray: linux - use .cache directory for temporary icon paths
1 parent cd0c660 commit b139821

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

src/core/unix/SDL_gtk.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ static bool InitGtk(void)
114114
SDL_GTK_SYM(gtk, libgtk, gtk, menu_item_set_submenu);
115115
SDL_GTK_SYM(gtk, libgtk, gtk, menu_item_get_label);
116116
SDL_GTK_SYM(gtk, libgtk, gtk, menu_item_set_label);
117-
SDL_GTK_SYM(gtk, libgtk, gtk, menu_shell_append);
118-
SDL_GTK_SYM(gtk, libgtk, gtk, menu_shell_insert);
117+
SDL_GTK_SYM(gtk, libgtk, gtk, menu_shell_append);
118+
SDL_GTK_SYM(gtk, libgtk, gtk, menu_shell_insert);
119119
SDL_GTK_SYM(gtk, libgtk, gtk, check_menu_item_new_with_label);
120120
SDL_GTK_SYM(gtk, libgtk, gtk, check_menu_item_get_active);
121121
SDL_GTK_SYM(gtk, libgtk, gtk, check_menu_item_set_active);
@@ -127,6 +127,7 @@ static bool InitGtk(void)
127127

128128
SDL_GTK_SYM(gtk, libgdk, g, signal_connect_data);
129129
SDL_GTK_SYM(gtk, libgdk, g, mkdtemp);
130+
SDL_GTK_SYM(gtk, libgdk, g, get_user_cache_dir);
130131
SDL_GTK_SYM(gtk, libgdk, g, object_ref);
131132
SDL_GTK_SYM(gtk, libgdk, g, object_ref_sink);
132133
SDL_GTK_SYM(gtk, libgdk, g, object_unref);

src/core/unix/SDL_gtk.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ typedef struct SDL_GtkContext
8080
gulong (*signal_connect_data)(gpointer instance, const gchar *detailed_signal, GCallback c_handler, gpointer data, GClosureNotify destroy_data, SDL_GConnectFlags connect_flags);
8181
void (*object_unref)(gpointer object);
8282
gchar *(*mkdtemp)(gchar *template);
83+
gchar *(*get_user_cache_dir)(void);
8384
gpointer (*object_ref_sink)(gpointer object);
8485
gpointer (*object_ref)(gpointer object);
8586
void (*object_get)(gpointer object, const gchar *first_property_name, ...);

src/tray/unix/SDL_tray.c

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,11 @@ struct SDL_TrayEntry {
153153
SDL_TrayMenu *submenu;
154154
};
155155

156-
/* Template for g_mkdtemp(). The Xs will get replaced with a random
157-
* directory name, which is created safely and atomically. */
158-
#define ICON_DIR_TEMPLATE "/tmp/SDL-tray-XXXXXX"
159-
160156
struct SDL_Tray {
161157
AppIndicator *indicator;
162158
SDL_TrayMenu *menu;
163-
char icon_dir[sizeof(ICON_DIR_TEMPLATE)];
164-
char icon_path[256];
159+
char *icon_dir;
160+
char *icon_path;
165161

166162
GtkMenuShell *menu_cached;
167163
};
@@ -188,13 +184,13 @@ static bool new_tmp_filename(SDL_Tray *tray)
188184
{
189185
static int count = 0;
190186

191-
int would_have_written = SDL_snprintf(tray->icon_path, sizeof(tray->icon_path), "%s/%d.bmp", tray->icon_dir, count++);
187+
int would_have_written = SDL_asprintf(&tray->icon_path, "%s/%d.bmp", tray->icon_dir, count++);
192188

193-
if (would_have_written > 0 && ((unsigned) would_have_written) < sizeof(tray->icon_path) - 1) {
189+
if (would_have_written >= 0) {
194190
return true;
195191
}
196192

197-
tray->icon_path[0] = '\0';
193+
tray->icon_path = NULL;
198194
SDL_SetError("Failed to format new temporary filename");
199195
return false;
200196
}
@@ -254,29 +250,47 @@ SDL_Tray *SDL_CreateTray(SDL_Surface *icon, const char *tooltip)
254250
SDL_Tray *tray = NULL;
255251
SDL_GtkContext *gtk = SDL_Gtk_EnterContext();
256252
if (!gtk) {
257-
goto error;
253+
goto tray_error;
258254
}
259255

260256
tray = (SDL_Tray *)SDL_calloc(1, sizeof(*tray));
261257
if (!tray) {
262-
goto error;
258+
goto tray_error;
259+
}
260+
261+
const gchar *cache_dir = gtk->g.get_user_cache_dir();
262+
if (!cache_dir) {
263+
SDL_SetError("Cannot get user cache directory: %s", strerror(errno));
264+
goto tray_error;
265+
}
266+
267+
char *sdl_dir;
268+
SDL_asprintf(&sdl_dir, "%s/SDL", cache_dir);
269+
if (!SDL_GetPathInfo(sdl_dir, NULL)) {
270+
if (!SDL_CreateDirectory(sdl_dir)) {
271+
SDL_SetError("Cannot create directory for tray icon: %s", strerror(errno));
272+
goto sdl_dir_error;
273+
}
263274
}
264275

265276
/* On success, g_mkdtemp edits its argument in-place to replace the Xs
266277
* with a random directory name, which it creates safely and atomically.
267278
* On failure, it sets errno. */
268-
SDL_strlcpy(tray->icon_dir, ICON_DIR_TEMPLATE, sizeof(tray->icon_dir));
279+
SDL_asprintf(&tray->icon_dir, "%s/tray-XXXXXX", sdl_dir);
269280
if (!gtk->g.mkdtemp(tray->icon_dir)) {
270281
SDL_SetError("Cannot create directory for tray icon: %s", strerror(errno));
271-
goto error;
282+
goto icon_dir_error;
272283
}
273284

274285
if (icon) {
275286
if (!new_tmp_filename(tray)) {
276-
goto error;
287+
goto icon_dir_error;
277288
}
278289

279290
SDL_SaveBMP(icon, tray->icon_path);
291+
} else {
292+
// allocate a dummy icon path
293+
SDL_asprintf(&tray->icon_path, " ");
280294
}
281295

282296
tray->indicator = app_indicator_new(get_appindicator_id(), tray->icon_path,
@@ -293,7 +307,13 @@ SDL_Tray *SDL_CreateTray(SDL_Surface *icon, const char *tooltip)
293307

294308
return tray;
295309

296-
error:
310+
icon_dir_error:
311+
SDL_free(tray->icon_dir);
312+
313+
sdl_dir_error:
314+
SDL_free(sdl_dir);
315+
316+
tray_error:
297317
if (tray) {
298318
SDL_free(tray);
299319
}
@@ -311,8 +331,10 @@ void SDL_SetTrayIcon(SDL_Tray *tray, SDL_Surface *icon)
311331
return;
312332
}
313333

314-
if (*tray->icon_path) {
334+
if (tray->icon_path) {
315335
SDL_RemovePath(tray->icon_path);
336+
SDL_free(tray->icon_path);
337+
tray->icon_path = NULL;
316338
}
317339

318340
/* AppIndicator caches the icon files; always change filename to avoid caching */
@@ -321,7 +343,8 @@ void SDL_SetTrayIcon(SDL_Tray *tray, SDL_Surface *icon)
321343
SDL_SaveBMP(icon, tray->icon_path);
322344
app_indicator_set_icon(tray->indicator, tray->icon_path);
323345
} else {
324-
*tray->icon_path = '\0';
346+
SDL_free(tray->icon_path);
347+
tray->icon_path = NULL;
325348
app_indicator_set_icon(tray->indicator, NULL);
326349
}
327350
}
@@ -714,12 +737,14 @@ void SDL_DestroyTray(SDL_Tray *tray)
714737
DestroySDLMenu(tray->menu);
715738
}
716739

717-
if (*tray->icon_path) {
740+
if (tray->icon_path) {
718741
SDL_RemovePath(tray->icon_path);
742+
SDL_free(tray->icon_path);
719743
}
720744

721-
if (*tray->icon_dir) {
745+
if (tray->icon_dir) {
722746
SDL_RemovePath(tray->icon_dir);
747+
SDL_free(tray->icon_dir);
723748
}
724749

725750
SDL_GtkContext *gtk = SDL_Gtk_EnterContext();

0 commit comments

Comments
 (0)