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 if ( stat ( ovrfile, &statbuf ) == 0 && time ( NULL ) - statbuf.st_mtime > 5 ) { // race with pndnotifyd
267 buflen = statbuf.st_size;
268 if ( ( iconbuf = malloc ( statbuf.st_size ) ) ) {
269 int fd = open ( ovrfile, O_RDONLY );
271 if ( read ( fd, iconbuf, statbuf.st_size ) != statbuf.st_size ) {
284 // if this is a real pnd file (dir-app or pnd-file-app), then try to pull icon from there
287 if ( app -> object_flags & PND_DISCO_GENERATED ) {
289 // maybe we can discover this single-file and find an icon?
290 if ( strcasestr ( app -> object_filename, PND_PACKAGE_FILEEXT ) ) {
292 // looks like a pnd, now what do we do..
293 pnd_box_handle h = pnd_disco_file ( app -> object_path, app -> object_filename );
296 pnd_disco_t *d = pnd_box_get_head ( h );
297 iconbuf = pnd_emit_icon_to_buffer ( d, &buflen );
300 } // filename has .pnd?
304 // pull icon into buffer from .pnd if not already found an icon
305 iconbuf = pnd_emit_icon_to_buffer ( app, &buflen );
309 } // already got icon?
315 // ready up a RWbuffer for SDL
316 SDL_RWops *rwops = SDL_RWFromMem ( iconbuf, buflen );
318 s = IMG_Load_RW ( rwops, 1 /* free the rwops */ );
324 free ( iconbuf ); // ditch the icon from ram
326 //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
329 if ( s -> w < maxwidth ) {
331 if ( pnd_conf_get_as_int_d ( g_conf, "grid.scale_up_bool", 1 ) ) {
333 double scale = (double)maxwidth / (double)s -> w;
334 //pnd_log ( pndn_debug, " Upscaling; scale factor %f\n", scale );
335 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
336 SDL_FreeSurface ( s );
339 } else if ( s -> w > maxwidth ) {
341 double scale = (double)maxwidth / (double)s -> w;
342 //pnd_log ( pndn_debug, " Downscaling; scale factor %f\n", scale );
343 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
344 SDL_FreeSurface ( s );
349 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
350 bzero ( c, sizeof(mm_cache_t) );
352 if ( ! g_icon_cache ) {
355 c -> next = g_icon_cache;
359 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
365 mm_cache_t *cache_query ( char *id, mm_cache_t *head ) {
366 mm_cache_t *iter = head;
373 if ( iter -> uniqueid &&
374 strcasecmp ( iter -> uniqueid, id ) == 0 )
384 mm_cache_t *cache_query_icon ( char *id ) {
385 return ( cache_query ( id, g_icon_cache ) );
388 mm_cache_t *cache_query_preview ( char *id ) {
389 return ( cache_query ( id, g_preview_cache ) );
392 unsigned char cache_find_writable ( char *originpath, char *r_writepath, unsigned int len ) {
393 static char *searchpaths = NULL;
394 static unsigned int minfree = 0;
395 char searchpath [ PATH_MAX ] = "";
396 char cmdbuf [ PATH_MAX ];
398 unsigned int freespace = 0;
400 // figure out the mountpoint for the file, and stick that in at front of searchpath
401 // so that it will get picked first, if possible.
402 char mountpath [ PATH_MAX ];
403 if ( pnd_determine_mountpoint ( originpath, mountpath, PATH_MAX ) ) {
404 sprintf ( searchpath, "%s:", mountpath );
405 //pnd_log ( pndn_debug, "Preferred cache target for %s: %s\n", originpath, mountpath );
408 // try to find a device, in order of searchpath, with enough space for this cache-out
411 // populate the searchpath
412 if ( ! searchpaths ) {
413 searchpaths = pnd_conf_get_as_char ( g_conf, "previewpic.cache_searchpath" );
414 minfree = pnd_conf_get_as_int_d ( g_conf, "previewpic.cache_minfree", 500 );
417 if ( ! searchpaths ) {
418 return ( 0 ); // fail!
421 strncat ( searchpath, searchpaths, PATH_MAX );
426 // since I didn't figure out which /sys/block I can pull remaining space from, I'll use df for now :/
427 sprintf ( cmdbuf, "/bin/df %s", buffer );
429 f = popen ( cmdbuf, "r" );
432 while ( fgets ( cmdbuf, PATH_MAX, f ) ) {
434 // /dev/sdc2 7471392 725260 6366600 11% /media/IMAGE
435 if ( sscanf ( cmdbuf, "%*s %*u %*u %u %*u %*s\n", &freespace ) == 1 ) {
436 strncpy ( r_writepath, buffer, len );
437 if ( freespace > minfree ) {