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 */
8 #include <time.h> /* for time() */
10 #define __USE_GNU /* for strcasestr */
11 #include <string.h> /* for making ftw.h happy */
17 #include "SDL_image.h"
18 #include "SDL_rotozoom.h"
20 #define __USE_GNU /* for strcasestr */
21 #include <unistd.h> /* for unlink */
22 #include <string.h> /* for making ftw.h happy */
25 #include "pnd_utility.h"
27 #include "pnd_container.h"
28 #include "pnd_discovery.h"
29 #include "pnd_logger.h"
30 #include "pnd_desktop.h"
31 #include "pnd_pndfiles.h"
33 #include "../lib/pnd_pathiter.h"
34 #include "pnd_locate.h"
35 #include "pnd_notify.h"
36 #include "pnd_dbusnotify.h"
44 extern pnd_conf_handle g_conf;
45 extern unsigned char g_pvwcache;
46 extern pnd_conf_handle g_desktopconf;
48 mm_cache_t *g_icon_cache = NULL;
49 mm_cache_t *g_preview_cache = NULL;
51 unsigned char cache_preview ( pnd_disco_t *app, unsigned int maxwidth, unsigned int maxheight ) {
55 // does this sucker even have a preview?
56 if ( ! app -> preview_pic1 ) {
57 return ( 1 ); // nothing here, so thats fine
60 // check if already cached
61 if ( ( c = cache_query_preview ( app -> unique_id ) ) ) {
62 return ( 1 ); // already got it
65 // not cached, load it up
69 ui_show_hourglass ( 1 /* updaterect*/ );
71 // see if we can mount the pnd/dir
72 // does preview file exist?
73 // if so, load it up, size it, cache it
74 // if not, warning and bail
77 // can we mount? or can we find it in preview cache? or an override?
78 char fullpath [ PATH_MAX ] = "";
79 char filepath [ PATH_MAX ] = "";
81 // first, check for preview override
83 char ovrfile [ PATH_MAX ];
85 sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
86 fooby = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
88 sprintf ( fooby, "_pvw#%u.png", app -> subapp_number );
90 if ( stat ( ovrfile, &statbuf ) == 0 ) {
91 strncpy ( filepath, ovrfile, PATH_MAX );
96 // if not yet found, try to find in cache
97 if ( filepath [ 0 ] == '\0' && g_pvwcache ) {
98 static char *cache_findpath = NULL;
99 if ( ! cache_findpath ) {
100 cache_findpath = pnd_conf_get_as_char ( g_conf, "previewpic.cache_findpath" );
102 char buffer [ FILENAME_MAX ];
103 sprintf ( buffer, "%s.png", app -> unique_id );
104 char *f = pnd_locate_filename ( cache_findpath, buffer );
106 strncpy ( filepath, f, PATH_MAX );
110 // unique-id to use for the cache mount
111 char *uid = app -> unique_id;
113 if ( app -> appdata_dirname ) {
114 uid = app -> appdata_dirname;
117 // if we don't have a file path sorted out yet, means we need to mount and figure it
118 if ( ! filepath [ 0 ] ) {
119 sprintf ( fullpath, "%s/%s", app -> object_path, app -> object_filename );
121 if ( ! pnd_pnd_mount ( pnd_run_script, fullpath, uid ) ) {
122 pnd_log ( pndn_debug, "Couldn't mount '%s' for preview\n", fullpath );
123 return ( 0 ); // couldn't mount?!
126 sprintf ( filepath, "%s/%s/%s", PND_MOUNT_PATH, uid, app -> preview_pic1 );
129 // load whatever path we've got
130 s = IMG_Load ( filepath );
133 // unmount it, if mounted
134 if ( fullpath [ 0 ] ) {
135 pnd_pnd_unmount ( pnd_run_script, fullpath, uid );
137 pnd_log ( pndn_debug, "Couldn't open image '%s' for preview\n", filepath );
141 // try to copy file to the cache, if we're doing that, and if mounted
142 if ( g_pvwcache && fullpath [ 0 ] ) {
143 char cacheoutpath [ PATH_MAX ] = "";
145 // figure out where we want to write the file to
146 if ( cache_find_writable ( app -> object_path, cacheoutpath, PATH_MAX ) ) {
147 static char *cache_path = NULL;
148 char buffer [ PATH_MAX ];
149 if ( ! cache_path ) {
150 cache_path = pnd_conf_get_as_char ( g_conf, "previewpic.cache_path" );
153 snprintf ( buffer, PATH_MAX, "%s/%s", cacheoutpath, cache_path );
155 if ( stat ( buffer, &statbuf ) != 0 ) {
156 snprintf ( buffer, PATH_MAX, "/bin/mkdir -p %s/%s", cacheoutpath, cache_path );
159 // set up target filename to copy
160 snprintf ( buffer, PATH_MAX, "%s/%s/%s.png", cacheoutpath, cache_path, app -> unique_id );
161 pnd_log ( pndn_debug, "Found free space to cache preview to here: %s", buffer );
162 if ( ! pnd_filecopy ( filepath, buffer ) ) {
163 pnd_log ( pndn_error, "ERROR: Copying preview from %s to %s", filepath, buffer );
166 pnd_log ( pndn_warning, "WARN: Couldn't find a device to cache preview to.\n" );
169 } // preview file cache
171 // unmount it, if mounted
172 if ( fullpath [ 0 ] ) {
173 pnd_pnd_unmount ( pnd_run_script, fullpath, app -> unique_id );
176 //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
179 if ( s -> w < maxwidth ) {
181 if ( pnd_conf_get_as_int_d ( g_conf, "previewpic.scale_up_bool", 1 ) ) {
183 double scale = (double)maxwidth / (double)s -> w;
184 //pnd_log ( pndn_debug, " Upscaling; scale factor %f\n", scale );
185 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
186 SDL_FreeSurface ( s );
189 } else if ( s -> w > maxwidth ) {
191 double scale = (double)maxwidth / (double)s -> w;
192 //pnd_log ( pndn_debug, " Downscaling; scale factor %f\n", scale );
193 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
194 SDL_FreeSurface ( s );
199 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
200 bzero ( c, sizeof(mm_cache_t) );
202 if ( ! g_preview_cache ) {
205 c -> next = g_preview_cache;
209 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
215 unsigned char cache_icon ( pnd_disco_t *app, unsigned char maxwidth, unsigned char maxheight ) {
219 // check if already cached
220 if ( ( c = cache_query_icon ( app -> unique_id ) ) ) {
221 return ( 1 ); // already got it
224 // not cached, load it up
226 unsigned char *iconbuf = NULL;
227 unsigned int buflen = 0;
229 // same-path icon override?
230 char ovrfile [ PATH_MAX ];
232 sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
233 fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
235 strcpy ( fixpxml, ".png" );
237 if ( stat ( ovrfile, &statbuf ) == 0 ) {
238 buflen = statbuf.st_size;
239 if ( ( iconbuf = malloc ( statbuf.st_size ) ) ) {
240 int fd = open ( ovrfile, O_RDONLY );
242 if ( read ( fd, iconbuf, statbuf.st_size ) != statbuf.st_size ) {
253 // perhaps pndnotifyd has dropped a copy of the icon into /tmp?
256 static char *iconpath = NULL;
259 iconpath = pnd_conf_get_as_char ( g_desktopconf, "desktop.iconpath" );
262 sprintf ( ovrfile, "%s/%s.png", iconpath, app -> unique_id );
264 // making sure the file is at least a few seconds old, to help avoid race condition
266 // *** commented out this stat() 'race test' since it buggers up icon loading at boot, if using .desktop instead of .pnd scanning..
267 if ( stat ( ovrfile, &statbuf ) == 0 /*&& time ( NULL ) - statbuf.st_mtime > 5*/ ) { // race with pndnotifyd
268 buflen = statbuf.st_size;
269 if ( ( iconbuf = malloc ( statbuf.st_size ) ) ) {
270 int fd = open ( ovrfile, O_RDONLY );
272 if ( read ( fd, iconbuf, statbuf.st_size ) != statbuf.st_size ) {
285 // if this is a real pnd file (dir-app or pnd-file-app), then try to pull icon from there
288 if ( app -> object_flags & PND_DISCO_GENERATED ) {
290 // maybe we can discover this single-file and find an icon?
291 if ( strcasestr ( app -> object_filename, PND_PACKAGE_FILEEXT ) ) {
293 // looks like a pnd, now what do we do..
294 pnd_box_handle h = pnd_disco_file ( app -> object_path, app -> object_filename );
297 pnd_disco_t *d = pnd_box_get_head ( h );
298 iconbuf = pnd_emit_icon_to_buffer ( d, &buflen );
301 } // filename has .pnd?
305 // pull icon into buffer from .pnd if not already found an icon
306 iconbuf = pnd_emit_icon_to_buffer ( app, &buflen );
310 } // already got icon?
316 // ready up a RWbuffer for SDL
317 SDL_RWops *rwops = SDL_RWFromMem ( iconbuf, buflen );
319 s = IMG_Load_RW ( rwops, 1 /* free the rwops */ );
325 free ( iconbuf ); // ditch the icon from ram
327 //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
330 if ( s -> w < maxwidth ) {
332 if ( pnd_conf_get_as_int_d ( g_conf, "grid.scale_up_bool", 1 ) ) {
334 double scale = (double)maxwidth / (double)s -> w;
335 //pnd_log ( pndn_debug, " Upscaling; scale factor %f\n", scale );
336 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
337 SDL_FreeSurface ( s );
340 } else if ( s -> w > maxwidth ) {
342 double scale = (double)maxwidth / (double)s -> w;
343 //pnd_log ( pndn_debug, " Downscaling; scale factor %f\n", scale );
344 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
345 SDL_FreeSurface ( s );
350 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
351 bzero ( c, sizeof(mm_cache_t) );
353 if ( ! g_icon_cache ) {
356 c -> next = g_icon_cache;
360 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
366 mm_cache_t *cache_query ( char *id, mm_cache_t *head ) {
367 mm_cache_t *iter = head;
374 if ( iter -> uniqueid &&
375 strcasecmp ( iter -> uniqueid, id ) == 0 )
385 mm_cache_t *cache_query_icon ( char *id ) {
386 return ( cache_query ( id, g_icon_cache ) );
389 mm_cache_t *cache_query_preview ( char *id ) {
390 return ( cache_query ( id, g_preview_cache ) );
393 unsigned char cache_find_writable ( char *originpath, char *r_writepath, unsigned int len ) {
394 static char *searchpaths = NULL;
395 static unsigned int minfree = 0;
396 char searchpath [ PATH_MAX ] = "";
397 char cmdbuf [ PATH_MAX ];
399 unsigned int freespace = 0;
401 // figure out the mountpoint for the file, and stick that in at front of searchpath
402 // so that it will get picked first, if possible.
403 char mountpath [ PATH_MAX ];
404 if ( pnd_determine_mountpoint ( originpath, mountpath, PATH_MAX ) ) {
405 sprintf ( searchpath, "%s:", mountpath );
406 //pnd_log ( pndn_debug, "Preferred cache target for %s: %s\n", originpath, mountpath );
409 // try to find a device, in order of searchpath, with enough space for this cache-out
412 // populate the searchpath
413 if ( ! searchpaths ) {
414 searchpaths = pnd_conf_get_as_char ( g_conf, "previewpic.cache_searchpath" );
415 minfree = pnd_conf_get_as_int_d ( g_conf, "previewpic.cache_minfree", 500 );
418 if ( ! searchpaths ) {
419 return ( 0 ); // fail!
422 strncat ( searchpath, searchpaths, PATH_MAX );
427 // since I didn't figure out which /sys/block I can pull remaining space from, I'll use df for now :/
428 sprintf ( cmdbuf, "/bin/df %s", buffer );
430 f = popen ( cmdbuf, "r" );
433 while ( fgets ( cmdbuf, PATH_MAX, f ) ) {
435 // /dev/sdc2 7471392 725260 6366600 11% /media/IMAGE
436 if ( sscanf ( cmdbuf, "%*s %*u %*u %u %*u %*s\n", &freespace ) == 1 ) {
437 strncpy ( r_writepath, buffer, len );
438 if ( freespace > minfree ) {