mmenu: fix annoying __USE_GNU redef warnings
[pandora-libraries.git] / minimenu / mmcache.c
1
2 #define __USE_GNU /* for strcasestr */
3 #include <stdio.h> /* for FILE etc */
4 #include <stdlib.h> /* for malloc */
5 #include <unistd.h> /* for unlink */
6 #include <limits.h> /* for PATH_MAX */
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <time.h> /* for time() */
10
11 #include <string.h> /* for making ftw.h happy */
12
13 #include <fcntl.h>
14 #include <limits.h>
15
16 #include "SDL.h"
17 #include "SDL_image.h"
18 #include "SDL_rotozoom.h"
19
20 #include <unistd.h> /* for unlink */
21 #include <string.h> /* for making ftw.h happy */
22
23 #include "pnd_pxml.h"
24 #include "pnd_utility.h"
25 #include "pnd_conf.h"
26 #include "pnd_container.h"
27 #include "pnd_discovery.h"
28 #include "pnd_logger.h"
29 #include "pnd_desktop.h"
30 #include "pnd_pndfiles.h"
31 #include "pnd_apps.h"
32 #include "../lib/pnd_pathiter.h"
33 #include "pnd_locate.h"
34 #include "pnd_notify.h"
35 #include "pnd_dbusnotify.h"
36
37 #include "mmenu.h"
38 #include "mmapps.h"
39 #include "mmcache.h"
40 #include "mmcat.h"
41 #include "mmui.h"
42 #include "mmui_context.h"
43
44 extern pnd_conf_handle g_conf;
45 extern unsigned char g_pvwcache;
46 extern pnd_conf_handle g_desktopconf;
47 SDL_Surface *ui_scale_image ( SDL_Surface *s, unsigned int maxwidth, int maxheight ); // height -1 means ignore
48
49 mm_cache_t *g_icon_cache = NULL;
50 mm_cache_t *g_preview_cache = NULL;
51
52 unsigned char cache_preview ( pnd_disco_t *app, unsigned int maxwidth, unsigned int maxheight ) {
53   SDL_Surface *s;
54   mm_cache_t *c;
55
56   // does this sucker even have a preview?
57   if ( ! app -> preview_pic1 ) {
58     return ( 1 ); // nothing here, so thats fine
59   }
60
61   // check if already cached
62   if ( ( c = cache_query_preview ( app -> unique_id ) ) ) {
63     return ( 1 ); // already got it
64   }
65
66   // not cached, load it up
67   //
68
69   // show hourglass
70   ui_show_hourglass ( 1 /* updaterect*/ );
71
72   // see if we can mount the pnd/dir
73   // does preview file exist?
74   //   if so, load it up, size it, cache it
75   //   if not, warning and bail
76   // unmount it
77
78   // can we mount? or can we find it in preview cache? or an override?
79   char fullpath [ PATH_MAX ] = "";
80   char filepath [ PATH_MAX ] = "";
81
82   // first, check for preview override
83   {
84     char ovrfile [ PATH_MAX ];
85     char *fooby;
86     sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
87     fooby = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
88     if ( fooby ) {
89       sprintf ( fooby, "_pvw#%u.png", app -> subapp_number );
90       struct stat statbuf;
91       if ( stat ( ovrfile, &statbuf ) == 0 ) {
92         strncpy ( filepath, ovrfile, PATH_MAX );
93       } // stat
94     } // ovr?
95   }
96
97   // if not yet found, try to find in cache
98   if ( filepath [ 0 ] == '\0' && g_pvwcache ) {
99     static char *cache_findpath = NULL;
100     if ( ! cache_findpath ) {
101       cache_findpath = pnd_conf_get_as_char ( g_conf, "previewpic.cache_findpath" );
102     }
103     char buffer [ FILENAME_MAX ];
104     sprintf ( buffer, "%s.png", app -> unique_id );
105     char *f = pnd_locate_filename ( cache_findpath, buffer );
106     if ( f ) {
107       strncpy ( filepath, f, PATH_MAX );
108     }
109   }
110
111   // unique-id to use for the cache mount
112   char *uid = app -> unique_id;
113
114   if ( app -> appdata_dirname ) {
115     uid = app -> appdata_dirname;
116   }
117
118   // if we don't have a file path sorted out yet, means we need to mount and figure it
119   if ( ! filepath [ 0 ] ) {
120     sprintf ( fullpath, "%s/%s", app -> object_path, app -> object_filename );
121
122     if ( ! pnd_pnd_mount ( pnd_run_script, fullpath, uid ) ) {
123       pnd_log ( pndn_debug, "Couldn't mount '%s' for preview\n", fullpath );
124       return ( 0 ); // couldn't mount?!
125     }
126
127     sprintf ( filepath, "%s/%s/%s", PND_MOUNT_PATH, uid, app -> preview_pic1 );
128   }
129
130   // load whatever path we've got
131   s = IMG_Load ( filepath );
132
133   if ( ! s ) {
134     // unmount it, if mounted
135     if ( fullpath [ 0 ] ) {
136       pnd_pnd_unmount ( pnd_run_script, fullpath, uid );
137     }
138     pnd_log ( pndn_debug, "Couldn't open image '%s' for preview\n", filepath );
139     return ( 0 );
140   }
141
142   // try to copy file to the cache, if we're doing that, and if mounted
143   if ( g_pvwcache && fullpath [ 0 ] ) {
144     char cacheoutpath [ PATH_MAX ] = "";
145
146     // figure out where we want to write the file to
147     if ( cache_find_writable ( app -> object_path, cacheoutpath, PATH_MAX ) ) {
148       static char *cache_path = NULL;
149       char buffer [ PATH_MAX ];
150       if ( ! cache_path ) {
151         cache_path = pnd_conf_get_as_char ( g_conf, "previewpic.cache_path" );
152       }
153       // make the dir
154       snprintf ( buffer, PATH_MAX, "%s/%s", cacheoutpath, cache_path );
155       struct stat statbuf;
156       if ( stat ( buffer, &statbuf ) != 0 ) {
157         snprintf ( buffer, PATH_MAX, "/bin/mkdir -p %s/%s", cacheoutpath, cache_path );
158         system ( buffer );
159       }
160       // set up target filename to copy
161       snprintf ( buffer, PATH_MAX, "%s/%s/%s.png", cacheoutpath, cache_path, app -> unique_id );
162       pnd_log ( pndn_debug, "Found free space to cache preview to here: %s", buffer );
163       if ( ! pnd_filecopy ( filepath, buffer ) ) {
164         pnd_log ( pndn_error, "ERROR: Copying preview from %s to %s", filepath, buffer );
165       }
166     } else {
167       pnd_log ( pndn_warning, "WARN: Couldn't find a device to cache preview to.\n" );
168     }
169
170   } // preview file cache
171
172   // unmount it, if mounted
173   if ( fullpath [ 0 ] ) {
174     pnd_pnd_unmount ( pnd_run_script, fullpath, app -> unique_id );
175   }
176
177   //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
178
179   // scale
180   if ( s -> w < maxwidth ) {
181     // scale up?
182     if ( pnd_conf_get_as_int_d ( g_conf, "previewpic.scale_up_bool", 1 ) ) {
183       SDL_Surface *scaled;
184       double scale = (double)maxwidth / (double)s -> w;
185       //pnd_log ( pndn_debug, "  Upscaling; scale factor %f\n", scale );
186       scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
187       SDL_FreeSurface ( s );
188       s = scaled;
189     }
190   } else if ( s -> w > maxwidth ) {
191     SDL_Surface *scaled;
192     double scale = (double)maxwidth / (double)s -> w;
193     //pnd_log ( pndn_debug, "  Downscaling; scale factor %f\n", scale );
194     scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
195     SDL_FreeSurface ( s );
196     s = scaled;
197   }
198
199   // add to cache
200   c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
201   bzero ( c, sizeof(mm_cache_t) );
202
203   if ( ! g_preview_cache ) {
204     g_preview_cache = c;
205   } else {
206     c -> next = g_preview_cache;
207     g_preview_cache = c;
208   }
209
210   strncpy ( c -> uniqueid, app -> unique_id, 1000 );
211   c -> i = s;
212
213   return ( 1 );
214 }
215
216 unsigned char cache_icon ( pnd_disco_t *app, unsigned char maxwidth, unsigned char maxheight ) {
217   SDL_Surface *s;
218   mm_cache_t *c;
219
220   // check if already cached
221   if ( ( c = cache_query_icon ( app -> unique_id ) ) ) {
222     return ( 1 ); // already got it
223   }
224
225   // not cached, load it up
226   //
227   unsigned char *iconbuf = NULL;
228   unsigned int buflen = 0;
229
230   // same-path icon override?
231   char ovrfile [ PATH_MAX ];
232   char *fixpxml;
233   sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
234   fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
235   if ( fixpxml ) {
236     strcpy ( fixpxml, ".png" );
237     struct stat statbuf;
238     if ( stat ( ovrfile, &statbuf ) == 0 ) {
239       buflen = statbuf.st_size;
240       if ( ( iconbuf = malloc ( statbuf.st_size ) ) ) {
241         int fd = open ( ovrfile, O_RDONLY );
242         if ( fd >= 0 ) {
243           if ( read ( fd, iconbuf, statbuf.st_size ) != statbuf.st_size ) {
244             free ( iconbuf );
245             close ( fd );
246             return ( 0 );
247           }
248           close ( fd );
249         } // open
250       } // malloc
251     } // stat
252   } // ovr?
253
254   // perhaps pndnotifyd has dropped a copy of the icon into /tmp?
255 #if 1
256   {
257     static char *iconpath = NULL;
258
259     if ( ! iconpath ) {
260       iconpath = pnd_conf_get_as_char ( g_desktopconf, "desktop.iconpath" );
261     }
262
263     sprintf ( ovrfile, "%s/%s.png", iconpath, app -> unique_id );
264
265     // making sure the file is at least a few seconds old, to help avoid race condition
266     struct stat statbuf;
267     // *** commented out this stat() 'race test' since it buggers up icon loading at boot, if using .desktop instead of .pnd scanning..
268     if ( stat ( ovrfile, &statbuf ) == 0 /*&& time ( NULL ) - statbuf.st_mtime > 5*/ ) { // race with pndnotifyd
269       buflen = statbuf.st_size;
270       if ( ( iconbuf = malloc ( statbuf.st_size ) ) ) {
271         int fd = open ( ovrfile, O_RDONLY );
272         if ( fd >= 0 ) {
273           if ( read ( fd, iconbuf, statbuf.st_size ) != statbuf.st_size ) {
274             free ( iconbuf );
275             close ( fd );
276             return ( 0 );
277           }
278           close ( fd );
279         } // open
280       } // malloc
281     } // stat
282
283   }
284 #endif
285
286   // if this is a real pnd file (dir-app or pnd-file-app), then try to pull icon from there
287   if ( ! iconbuf ) {
288
289     if (  app -> object_flags & PND_DISCO_GENERATED ) {
290
291       // maybe we can discover this single-file and find an icon?
292       if ( strcasestr ( app -> object_filename, PND_PACKAGE_FILEEXT ) ) {
293
294         // looks like a pnd, now what do we do..
295         pnd_box_handle h = pnd_disco_file ( app -> object_path, app -> object_filename );
296
297         if ( h ) {
298           pnd_disco_t *d = pnd_box_get_head ( h );
299           iconbuf = pnd_emit_icon_to_buffer ( d, &buflen );
300         }
301
302       } // filename has .pnd?
303
304     } else {
305
306       // pull icon into buffer from .pnd if not already found an icon
307       iconbuf = pnd_emit_icon_to_buffer ( app, &buflen );
308
309     } // generated?
310
311   } // already got icon?
312
313   if ( ! iconbuf ) {
314     return ( 0 );
315   }
316
317   // ready up a RWbuffer for SDL
318   SDL_RWops *rwops = SDL_RWFromMem ( iconbuf, buflen );
319
320   s = IMG_Load_RW ( rwops, 1 /* free the rwops */ );
321
322   if ( ! s ) {
323     return ( 0 );
324   }
325
326   free ( iconbuf ); // ditch the icon from ram
327
328   //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
329
330   // scale the icon?
331   if ( s -> w < maxwidth ) {
332     // scale up?
333     if ( pnd_conf_get_as_int_d ( g_conf, "grid.scale_up_bool", 1 ) ) {
334       SDL_Surface *scaled;
335       double scale = (double)maxwidth / (double)s -> w;
336       //pnd_log ( pndn_debug, "  Upscaling; scale factor %f\n", scale );
337       scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
338       SDL_FreeSurface ( s );
339       s = scaled;
340     }
341   } else if ( s -> w > maxwidth ) {
342     SDL_Surface *scaled;
343     double scale = (double)maxwidth / (double)s -> w;
344     //pnd_log ( pndn_debug, "  Downscaling; scale factor %f\n", scale );
345     scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
346     SDL_FreeSurface ( s );
347     s = scaled;
348   }
349
350   // scale for tiny?
351   SDL_Surface *scaled_tiny = SDL_ConvertSurface ( s, s -> format, s -> flags );
352   extern ui_context_t ui_display_context;
353   scaled_tiny = ui_scale_image ( scaled_tiny, -1 , ui_display_context.text_height );
354
355   // add to cache
356   c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
357   bzero ( c, sizeof(mm_cache_t) );
358
359   if ( ! g_icon_cache ) {
360     g_icon_cache = c;
361   } else {
362     c -> next = g_icon_cache;
363     g_icon_cache = c;
364   }
365
366   strncpy ( c -> uniqueid, app -> unique_id, 1000 );
367   c -> i = s;
368   c -> itiny = scaled_tiny;
369
370   return ( 1 );
371 }
372
373 mm_cache_t *cache_query ( char *id, mm_cache_t *head ) {
374   mm_cache_t *iter = head;
375
376   if ( ! id ) {
377     return ( NULL );
378   }
379
380   while ( iter ) {
381     if ( iter -> uniqueid &&
382          strcasecmp ( iter -> uniqueid, id ) == 0 )
383     {
384       return ( iter );
385     }
386     iter = iter -> next;
387   } // while
388
389   return ( NULL );
390 }
391
392 mm_cache_t *cache_query_icon ( char *id ) {
393   return ( cache_query ( id, g_icon_cache ) );
394 }
395
396 mm_cache_t *cache_query_preview ( char *id ) {
397   return ( cache_query ( id, g_preview_cache ) );
398 }
399
400 unsigned char cache_find_writable ( char *originpath, char *r_writepath, unsigned int len ) {
401   static char *searchpaths = NULL;
402   static unsigned int minfree = 0;
403   char searchpath [ PATH_MAX ] = "";
404   char cmdbuf [ PATH_MAX ];
405   FILE *f;
406   unsigned int freespace = 0;
407
408   // figure out the mountpoint for the file, and stick that in at front of searchpath
409   // so that it will get picked first, if possible.
410   char mountpath [ PATH_MAX ];
411   if ( pnd_determine_mountpoint ( originpath, mountpath, PATH_MAX ) ) {
412     sprintf ( searchpath, "%s:", mountpath );
413     //pnd_log ( pndn_debug, "Preferred cache target for %s: %s\n", originpath, mountpath );
414   }
415
416   // try to find a device, in order of searchpath, with enough space for this cache-out
417   //
418
419   // populate the searchpath
420   if ( ! searchpaths ) {
421     searchpaths = pnd_conf_get_as_char ( g_conf, "previewpic.cache_searchpath" );
422     minfree = pnd_conf_get_as_int_d ( g_conf, "previewpic.cache_minfree", 500 );
423   }
424
425   if ( ! searchpaths ) {
426     return ( 0 ); // fail!
427   }
428
429   strncat ( searchpath, searchpaths, PATH_MAX );
430
431   SEARCHPATH_PRE
432   {
433
434     // since I didn't figure out which /sys/block I can pull remaining space from, I'll use df for now :/
435     sprintf ( cmdbuf, "/bin/df %s", buffer );
436
437     f = popen ( cmdbuf, "r" );
438
439     if ( f ) {
440       while ( fgets ( cmdbuf, PATH_MAX, f ) ) {
441         // just eat it up
442         // /dev/sdc2              7471392    725260   6366600  11% /media/IMAGE
443         if ( sscanf ( cmdbuf, "%*s %*u %*u %u %*u %*s\n", &freespace ) == 1 ) {
444           strncpy ( r_writepath, buffer, len );
445           if ( freespace > minfree ) {
446             pclose ( f );
447             return ( 1 );
448           } // enough free?
449         } // df
450       } // while
451       pclose ( f );
452     }
453
454   }
455   SEARCHPATH_POST
456
457   return ( 0 );
458 }