@@ -153,15 +153,11 @@ struct SDL_TrayEntry {
153
153
SDL_TrayMenu * submenu ;
154
154
};
155
155
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
-
160
156
struct SDL_Tray {
161
157
AppIndicator * indicator ;
162
158
SDL_TrayMenu * menu ;
163
- char icon_dir [ sizeof ( ICON_DIR_TEMPLATE )] ;
164
- char icon_path [ 256 ] ;
159
+ char * icon_dir ;
160
+ char * icon_path ;
165
161
166
162
GtkMenuShell * menu_cached ;
167
163
};
@@ -188,13 +184,13 @@ static bool new_tmp_filename(SDL_Tray *tray)
188
184
{
189
185
static int count = 0 ;
190
186
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 ++ );
192
188
193
- if (would_have_written > 0 && (( unsigned ) would_have_written ) < sizeof ( tray -> icon_path ) - 1 ) {
189
+ if (would_have_written >= 0 ) {
194
190
return true;
195
191
}
196
192
197
- tray -> icon_path [ 0 ] = '\0' ;
193
+ tray -> icon_path = NULL ;
198
194
SDL_SetError ("Failed to format new temporary filename" );
199
195
return false;
200
196
}
@@ -254,29 +250,47 @@ SDL_Tray *SDL_CreateTray(SDL_Surface *icon, const char *tooltip)
254
250
SDL_Tray * tray = NULL ;
255
251
SDL_GtkContext * gtk = SDL_Gtk_EnterContext ();
256
252
if (!gtk ) {
257
- goto error ;
253
+ goto tray_error ;
258
254
}
259
255
260
256
tray = (SDL_Tray * )SDL_calloc (1 , sizeof (* tray ));
261
257
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
+ }
263
274
}
264
275
265
276
/* On success, g_mkdtemp edits its argument in-place to replace the Xs
266
277
* with a random directory name, which it creates safely and atomically.
267
278
* 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 );
269
280
if (!gtk -> g .mkdtemp (tray -> icon_dir )) {
270
281
SDL_SetError ("Cannot create directory for tray icon: %s" , strerror (errno ));
271
- goto error ;
282
+ goto icon_dir_error ;
272
283
}
273
284
274
285
if (icon ) {
275
286
if (!new_tmp_filename (tray )) {
276
- goto error ;
287
+ goto icon_dir_error ;
277
288
}
278
289
279
290
SDL_SaveBMP (icon , tray -> icon_path );
291
+ } else {
292
+ // allocate a dummy icon path
293
+ SDL_asprintf (& tray -> icon_path , " " );
280
294
}
281
295
282
296
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)
293
307
294
308
return tray ;
295
309
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 :
297
317
if (tray ) {
298
318
SDL_free (tray );
299
319
}
@@ -311,8 +331,10 @@ void SDL_SetTrayIcon(SDL_Tray *tray, SDL_Surface *icon)
311
331
return ;
312
332
}
313
333
314
- if (* tray -> icon_path ) {
334
+ if (tray -> icon_path ) {
315
335
SDL_RemovePath (tray -> icon_path );
336
+ SDL_free (tray -> icon_path );
337
+ tray -> icon_path = NULL ;
316
338
}
317
339
318
340
/* AppIndicator caches the icon files; always change filename to avoid caching */
@@ -321,7 +343,8 @@ void SDL_SetTrayIcon(SDL_Tray *tray, SDL_Surface *icon)
321
343
SDL_SaveBMP (icon , tray -> icon_path );
322
344
app_indicator_set_icon (tray -> indicator , tray -> icon_path );
323
345
} else {
324
- * tray -> icon_path = '\0' ;
346
+ SDL_free (tray -> icon_path );
347
+ tray -> icon_path = NULL ;
325
348
app_indicator_set_icon (tray -> indicator , NULL );
326
349
}
327
350
}
@@ -714,12 +737,14 @@ void SDL_DestroyTray(SDL_Tray *tray)
714
737
DestroySDLMenu (tray -> menu );
715
738
}
716
739
717
- if (* tray -> icon_path ) {
740
+ if (tray -> icon_path ) {
718
741
SDL_RemovePath (tray -> icon_path );
742
+ SDL_free (tray -> icon_path );
719
743
}
720
744
721
- if (* tray -> icon_dir ) {
745
+ if (tray -> icon_dir ) {
722
746
SDL_RemovePath (tray -> icon_dir );
747
+ SDL_free (tray -> icon_dir );
723
748
}
724
749
725
750
SDL_GtkContext * gtk = SDL_Gtk_EnterContext ();
0 commit comments