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 */
9 #include <time.h> /* for time() */
11 #include <string.h> /* for making ftw.h happy */
17 #include "SDL_image.h"
18 #include "SDL_rotozoom.h"
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"
42 #include "mmui_context.h"
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
49 mm_cache_t *g_icon_cache = NULL;
50 mm_cache_t *g_preview_cache = NULL;
52 unsigned char cache_preview ( pnd_disco_t *app, unsigned int maxwidth, unsigned int maxheight ) {
56 // does this sucker even have a preview?
57 if ( ! app -> preview_pic1 ) {
58 return ( 1 ); // nothing here, so thats fine
61 // check if already cached
62 if ( ( c = cache_query_preview ( app -> unique_id ) ) ) {
63 return ( 1 ); // already got it
66 // not cached, load it up
70 ui_show_hourglass ( 1 /* updaterect*/ );
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
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 ] = "";
82 // first, check for preview override
84 char ovrfile [ PATH_MAX ];
86 sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
87 fooby = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
89 sprintf ( fooby, "_pvw#%u.png", app -> subapp_number );
91 if ( stat ( ovrfile, &statbuf ) == 0 ) {
92 strncpy ( filepath, ovrfile, PATH_MAX );
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" );
103 char buffer [ FILENAME_MAX ];
104 sprintf ( buffer, "%s.png", app -> unique_id );
105 char *f = pnd_locate_filename ( cache_findpath, buffer );
107 strncpy ( filepath, f, PATH_MAX );
111 // unique-id to use for the cache mount
112 char *uid = app -> unique_id;
114 if ( app -> appdata_dirname ) {
115 uid = app -> appdata_dirname;
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 );
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?!
127 sprintf ( filepath, "%s/%s/%s", PND_MOUNT_PATH, uid, app -> preview_pic1 );
130 // load whatever path we've got
131 s = IMG_Load ( filepath );
134 // unmount it, if mounted
135 if ( fullpath [ 0 ] ) {
136 pnd_pnd_unmount ( pnd_run_script, fullpath, uid );
138 pnd_log ( pndn_debug, "Couldn't open image '%s' for preview\n", filepath );
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 ] = "";
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" );
154 snprintf ( buffer, PATH_MAX, "%s/%s", cacheoutpath, cache_path );
156 if ( stat ( buffer, &statbuf ) != 0 ) {
157 snprintf ( buffer, PATH_MAX, "/bin/mkdir -p %s/%s", cacheoutpath, cache_path );
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 );
167 pnd_log ( pndn_warning, "WARN: Couldn't find a device to cache preview to.\n" );
170 } // preview file cache
172 // unmount it, if mounted
173 if ( fullpath [ 0 ] ) {
174 pnd_pnd_unmount ( pnd_run_script, fullpath, app -> unique_id );
177 //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
180 if ( s -> w < maxwidth ) {
182 if ( pnd_conf_get_as_int_d ( g_conf, "previewpic.scale_up_bool", 1 ) ) {
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 );
190 } else if ( s -> w > maxwidth ) {
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 );
200 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
201 bzero ( c, sizeof(mm_cache_t) );
203 if ( ! g_preview_cache ) {
206 c -> next = g_preview_cache;
210 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
216 unsigned char cache_icon ( pnd_disco_t *app, unsigned char maxwidth, unsigned char maxheight ) {
220 // check if already cached
221 if ( ( c = cache_query_icon ( app -> unique_id ) ) ) {
222 return ( 1 ); // already got it
225 // not cached, load it up
227 unsigned char *iconbuf = NULL;
228 unsigned int buflen = 0;
230 // same-path icon override?
231 char ovrfile [ PATH_MAX ];
233 sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
234 fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
236 strcpy ( fixpxml, ".png" );
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 );
243 if ( read ( fd, iconbuf, statbuf.st_size ) != statbuf.st_size ) {
254 // perhaps pndnotifyd has dropped a copy of the icon into /tmp?
257 static char *iconpath = NULL;
260 iconpath = pnd_conf_get_as_char ( g_desktopconf, "desktop.iconpath" );
263 sprintf ( ovrfile, "%s/%s.png", iconpath, app -> unique_id );
265 // making sure the file is at least a few seconds old, to help avoid race condition
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 );
273 if ( read ( fd, iconbuf, statbuf.st_size ) != statbuf.st_size ) {
286 // if this is a real pnd file (dir-app or pnd-file-app), then try to pull icon from there
289 if ( app -> object_flags & PND_DISCO_GENERATED ) {
291 // maybe we can discover this single-file and find an icon?
292 if ( strcasestr ( app -> object_filename, PND_PACKAGE_FILEEXT ) ) {
294 // looks like a pnd, now what do we do..
295 pnd_box_handle h = pnd_disco_file ( app -> object_path, app -> object_filename );
298 pnd_disco_t *d = pnd_box_get_head ( h );
299 iconbuf = pnd_emit_icon_to_buffer ( d, &buflen );
302 } // filename has .pnd?
306 // pull icon into buffer from .pnd if not already found an icon
307 iconbuf = pnd_emit_icon_to_buffer ( app, &buflen );
311 } // already got icon?
317 // ready up a RWbuffer for SDL
318 SDL_RWops *rwops = SDL_RWFromMem ( iconbuf, buflen );
320 s = IMG_Load_RW ( rwops, 1 /* free the rwops */ );
326 free ( iconbuf ); // ditch the icon from ram
328 //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
331 if ( s -> w < maxwidth ) {
333 if ( pnd_conf_get_as_int_d ( g_conf, "grid.scale_up_bool", 1 ) ) {
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 );
341 } else if ( s -> w > maxwidth ) {
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 );
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 );
356 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
357 bzero ( c, sizeof(mm_cache_t) );
359 if ( ! g_icon_cache ) {
362 c -> next = g_icon_cache;
366 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
368 c -> itiny = scaled_tiny;
373 mm_cache_t *cache_query ( char *id, mm_cache_t *head ) {
374 mm_cache_t *iter = head;
381 if ( iter -> uniqueid &&
382 strcasecmp ( iter -> uniqueid, id ) == 0 )
392 mm_cache_t *cache_query_icon ( char *id ) {
393 return ( cache_query ( id, g_icon_cache ) );
396 mm_cache_t *cache_query_preview ( char *id ) {
397 return ( cache_query ( id, g_preview_cache ) );
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 ];
406 unsigned int freespace = 0;
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 );
416 // try to find a device, in order of searchpath, with enough space for this cache-out
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 );
425 if ( ! searchpaths ) {
426 return ( 0 ); // fail!
429 strncat ( searchpath, searchpaths, PATH_MAX );
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 );
437 f = popen ( cmdbuf, "r" );
440 while ( fgets ( cmdbuf, PATH_MAX, f ) ) {
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 ) {