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