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 */
9 #define __USE_GNU /* for strcasestr */
10 #include <string.h> /* for making ftw.h happy */
16 #include "SDL_image.h"
17 #include "SDL_rotozoom.h"
19 #define __USE_GNU /* for strcasestr */
20 #include <unistd.h> /* for unlink */
21 #include <string.h> /* for making ftw.h happy */
24 #include "pnd_utility.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"
32 #include "../lib/pnd_pathiter.h"
33 #include "pnd_locate.h"
34 #include "pnd_notify.h"
35 #include "pnd_dbusnotify.h"
43 extern pnd_conf_handle g_conf;
44 extern unsigned char g_pvwcache;
45 extern pnd_conf_handle g_desktopconf;
47 mm_cache_t *g_icon_cache = NULL;
48 mm_cache_t *g_preview_cache = NULL;
50 unsigned char cache_preview ( pnd_disco_t *app, unsigned int maxwidth, unsigned int maxheight ) {
54 // does this sucker even have a preview?
55 if ( ! app -> preview_pic1 ) {
56 return ( 1 ); // nothing here, so thats fine
59 // check if already cached
60 if ( ( c = cache_query_preview ( app -> unique_id ) ) ) {
61 return ( 1 ); // already got it
64 // not cached, load it up
68 ui_show_hourglass ( 1 /* updaterect*/ );
70 // see if we can mount the pnd/dir
71 // does preview file exist?
72 // if so, load it up, size it, cache it
73 // if not, warning and bail
76 // can we mount? or can we find it in preview cache? or an override?
77 char fullpath [ PATH_MAX ] = "";
78 char filepath [ PATH_MAX ] = "";
80 // first, check for preview override
82 char ovrfile [ PATH_MAX ];
84 sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
85 fooby = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
87 sprintf ( fooby, "_pvw#%u.png", app -> subapp_number );
89 if ( stat ( ovrfile, &statbuf ) == 0 ) {
90 strncpy ( filepath, ovrfile, PATH_MAX );
95 // if not yet found, try to find in cache
96 if ( filepath [ 0 ] == '\0' && g_pvwcache ) {
97 static char *cache_findpath = NULL;
98 if ( ! cache_findpath ) {
99 cache_findpath = pnd_conf_get_as_char ( g_conf, "previewpic.cache_findpath" );
101 char buffer [ FILENAME_MAX ];
102 sprintf ( buffer, "%s.png", app -> unique_id );
103 char *f = pnd_locate_filename ( cache_findpath, buffer );
105 strncpy ( filepath, f, PATH_MAX );
109 // unique-id to use for the cache mount
110 char *uid = app -> unique_id;
112 if ( app -> appdata_dirname ) {
113 uid = app -> appdata_dirname;
116 // if we don't have a file path sorted out yet, means we need to mount and figure it
117 if ( ! filepath [ 0 ] ) {
118 sprintf ( fullpath, "%s/%s", app -> object_path, app -> object_filename );
120 if ( ! pnd_pnd_mount ( pnd_run_script, fullpath, uid ) ) {
121 pnd_log ( pndn_debug, "Couldn't mount '%s' for preview\n", fullpath );
122 return ( 0 ); // couldn't mount?!
125 sprintf ( filepath, "%s/%s/%s", PND_MOUNT_PATH, uid, app -> preview_pic1 );
128 // load whatever path we've got
129 s = IMG_Load ( filepath );
132 // unmount it, if mounted
133 if ( fullpath [ 0 ] ) {
134 pnd_pnd_unmount ( pnd_run_script, fullpath, uid );
136 pnd_log ( pndn_debug, "Couldn't open image '%s' for preview\n", filepath );
140 // try to copy file to the cache, if we're doing that, and if mounted
141 if ( g_pvwcache && fullpath [ 0 ] ) {
142 char cacheoutpath [ PATH_MAX ] = "";
144 // figure out where we want to write the file to
145 if ( cache_find_writable ( app -> object_path, cacheoutpath, PATH_MAX ) ) {
146 static char *cache_path = NULL;
147 char buffer [ PATH_MAX ];
148 if ( ! cache_path ) {
149 cache_path = pnd_conf_get_as_char ( g_conf, "previewpic.cache_path" );
152 snprintf ( buffer, PATH_MAX, "%s/%s", cacheoutpath, cache_path );
154 if ( stat ( buffer, &statbuf ) != 0 ) {
155 snprintf ( buffer, PATH_MAX, "/bin/mkdir -p %s/%s", cacheoutpath, cache_path );
158 // set up target filename to copy
159 snprintf ( buffer, PATH_MAX, "%s/%s/%s.png", cacheoutpath, cache_path, app -> unique_id );
160 pnd_log ( pndn_debug, "Found free space to cache preview to here: %s", buffer );
161 if ( ! pnd_filecopy ( filepath, buffer ) ) {
162 pnd_log ( pndn_error, "ERROR: Copying preview from %s to %s", filepath, buffer );
165 pnd_log ( pndn_warning, "WARN: Couldn't find a device to cache preview to.\n" );
168 } // preview file cache
170 // unmount it, if mounted
171 if ( fullpath [ 0 ] ) {
172 pnd_pnd_unmount ( pnd_run_script, fullpath, app -> unique_id );
175 //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
178 if ( s -> w < maxwidth ) {
180 if ( pnd_conf_get_as_int_d ( g_conf, "previewpic.scale_up_bool", 1 ) ) {
182 double scale = (double)maxwidth / (double)s -> w;
183 //pnd_log ( pndn_debug, " Upscaling; scale factor %f\n", scale );
184 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
185 SDL_FreeSurface ( s );
188 } else if ( s -> w > maxwidth ) {
190 double scale = (double)maxwidth / (double)s -> w;
191 //pnd_log ( pndn_debug, " Downscaling; scale factor %f\n", scale );
192 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
193 SDL_FreeSurface ( s );
198 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
199 bzero ( c, sizeof(mm_cache_t) );
201 if ( ! g_preview_cache ) {
204 c -> next = g_preview_cache;
208 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
214 unsigned char cache_icon ( pnd_disco_t *app, unsigned char maxwidth, unsigned char maxheight ) {
218 // check if already cached
219 if ( ( c = cache_query_icon ( app -> unique_id ) ) ) {
220 return ( 1 ); // already got it
223 // not cached, load it up
225 unsigned char *iconbuf = NULL;
226 unsigned int buflen = 0;
228 // same-path icon override?
229 char ovrfile [ PATH_MAX ];
231 sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
232 fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
234 strcpy ( fixpxml, ".png" );
236 if ( stat ( ovrfile, &statbuf ) == 0 ) {
237 buflen = statbuf.st_size;
238 if ( ( iconbuf = malloc ( statbuf.st_size ) ) ) {
239 int fd = open ( ovrfile, O_RDONLY );
241 if ( read ( fd, iconbuf, statbuf.st_size ) != statbuf.st_size ) {
252 // perhaps pndnotifyd has dropped a copy of the icon into /tmp?
255 static char *iconpath = NULL;
258 iconpath = pnd_conf_get_as_char ( g_desktopconf, "desktop.iconpath" );
261 sprintf ( ovrfile, "%s/%s.png", iconpath, app -> unique_id );
263 // making sure the file is at least a few seconds old, to help avoid race condition
265 if ( stat ( ovrfile, &statbuf ) == 0 && time ( NULL ) - statbuf.st_mtime > 5 ) { // race with pndnotifyd
266 buflen = statbuf.st_size;
267 if ( ( iconbuf = malloc ( statbuf.st_size ) ) ) {
268 int fd = open ( ovrfile, O_RDONLY );
270 if ( read ( fd, iconbuf, statbuf.st_size ) != statbuf.st_size ) {
283 // if this is a real pnd file (dir-app or pnd-file-app), then try to pull icon from there
286 if ( app -> object_flags & PND_DISCO_GENERATED ) {
288 // maybe we can discover this single-file and find an icon?
289 if ( strcasestr ( app -> object_filename, PND_PACKAGE_FILEEXT ) ) {
291 // looks like a pnd, now what do we do..
292 pnd_box_handle h = pnd_disco_file ( app -> object_path, app -> object_filename );
295 pnd_disco_t *d = pnd_box_get_head ( h );
296 iconbuf = pnd_emit_icon_to_buffer ( d, &buflen );
299 } // filename has .pnd?
303 // pull icon into buffer from .pnd if not already found an icon
304 iconbuf = pnd_emit_icon_to_buffer ( app, &buflen );
308 } // already got icon?
314 // ready up a RWbuffer for SDL
315 SDL_RWops *rwops = SDL_RWFromMem ( iconbuf, buflen );
317 s = IMG_Load_RW ( rwops, 1 /* free the rwops */ );
323 free ( iconbuf ); // ditch the icon from ram
325 //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
328 if ( s -> w < maxwidth ) {
330 if ( pnd_conf_get_as_int_d ( g_conf, "grid.scale_up_bool", 1 ) ) {
332 double scale = (double)maxwidth / (double)s -> w;
333 //pnd_log ( pndn_debug, " Upscaling; scale factor %f\n", scale );
334 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
335 SDL_FreeSurface ( s );
338 } else if ( s -> w > maxwidth ) {
340 double scale = (double)maxwidth / (double)s -> w;
341 //pnd_log ( pndn_debug, " Downscaling; scale factor %f\n", scale );
342 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
343 SDL_FreeSurface ( s );
348 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
349 bzero ( c, sizeof(mm_cache_t) );
351 if ( ! g_icon_cache ) {
354 c -> next = g_icon_cache;
358 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
364 mm_cache_t *cache_query ( char *id, mm_cache_t *head ) {
365 mm_cache_t *iter = head;
372 if ( iter -> uniqueid &&
373 strcasecmp ( iter -> uniqueid, id ) == 0 )
383 mm_cache_t *cache_query_icon ( char *id ) {
384 return ( cache_query ( id, g_icon_cache ) );
387 mm_cache_t *cache_query_preview ( char *id ) {
388 return ( cache_query ( id, g_preview_cache ) );
391 unsigned char cache_find_writable ( char *originpath, char *r_writepath, unsigned int len ) {
392 static char *searchpaths = NULL;
393 static unsigned int minfree = 0;
394 char searchpath [ PATH_MAX ] = "";
395 char cmdbuf [ PATH_MAX ];
397 unsigned int freespace = 0;
399 // figure out the mountpoint for the file, and stick that in at front of searchpath
400 // so that it will get picked first, if possible.
401 char mountpath [ PATH_MAX ];
402 if ( pnd_determine_mountpoint ( originpath, mountpath, PATH_MAX ) ) {
403 sprintf ( searchpath, "%s:", mountpath );
404 //pnd_log ( pndn_debug, "Preferred cache target for %s: %s\n", originpath, mountpath );
407 // try to find a device, in order of searchpath, with enough space for this cache-out
410 // populate the searchpath
411 if ( ! searchpaths ) {
412 searchpaths = pnd_conf_get_as_char ( g_conf, "previewpic.cache_searchpath" );
413 minfree = pnd_conf_get_as_int_d ( g_conf, "previewpic.cache_minfree", 500 );
416 if ( ! searchpaths ) {
417 return ( 0 ); // fail!
420 strncat ( searchpath, searchpaths, PATH_MAX );
425 // since I didn't figure out which /sys/block I can pull remaining space from, I'll use df for now :/
426 sprintf ( cmdbuf, "/bin/df %s", buffer );
428 f = popen ( cmdbuf, "r" );
431 while ( fgets ( cmdbuf, PATH_MAX, f ) ) {
433 // /dev/sdc2 7471392 725260 6366600 11% /media/IMAGE
434 if ( sscanf ( cmdbuf, "%*s %*u %*u %u %*u %*s\n", &freespace ) == 1 ) {
435 strncpy ( r_writepath, buffer, len );
436 if ( freespace > minfree ) {