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"
43 #include "mmui_context.h"
45 extern pnd_conf_handle g_conf;
46 extern unsigned char g_pvwcache;
47 extern pnd_conf_handle g_desktopconf;
48 SDL_Surface *ui_scale_image ( SDL_Surface *s, unsigned int maxwidth, int maxheight ); // height -1 means ignore
50 mm_cache_t *g_icon_cache = NULL;
51 mm_cache_t *g_preview_cache = NULL;
53 unsigned char cache_preview ( pnd_disco_t *app, unsigned int maxwidth, unsigned int maxheight ) {
57 // does this sucker even have a preview?
58 if ( ! app -> preview_pic1 ) {
59 return ( 1 ); // nothing here, so thats fine
62 // check if already cached
63 if ( ( c = cache_query_preview ( app -> unique_id ) ) ) {
64 return ( 1 ); // already got it
67 // not cached, load it up
71 ui_show_hourglass ( 1 /* updaterect*/ );
73 // see if we can mount the pnd/dir
74 // does preview file exist?
75 // if so, load it up, size it, cache it
76 // if not, warning and bail
79 // can we mount? or can we find it in preview cache? or an override?
80 char fullpath [ PATH_MAX ] = "";
81 char filepath [ PATH_MAX ] = "";
83 // first, check for preview override
85 char ovrfile [ PATH_MAX ];
87 sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
88 fooby = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
90 sprintf ( fooby, "_pvw#%u.png", app -> subapp_number );
92 if ( stat ( ovrfile, &statbuf ) == 0 ) {
93 strncpy ( filepath, ovrfile, PATH_MAX );
98 // if not yet found, try to find in cache
99 if ( filepath [ 0 ] == '\0' && g_pvwcache ) {
100 static char *cache_findpath = NULL;
101 if ( ! cache_findpath ) {
102 cache_findpath = pnd_conf_get_as_char ( g_conf, "previewpic.cache_findpath" );
104 char buffer [ FILENAME_MAX ];
105 sprintf ( buffer, "%s.png", app -> unique_id );
106 char *f = pnd_locate_filename ( cache_findpath, buffer );
108 strncpy ( filepath, f, PATH_MAX );
112 // unique-id to use for the cache mount
113 char *uid = app -> unique_id;
115 if ( app -> appdata_dirname ) {
116 uid = app -> appdata_dirname;
119 // if we don't have a file path sorted out yet, means we need to mount and figure it
120 if ( ! filepath [ 0 ] ) {
121 sprintf ( fullpath, "%s/%s", app -> object_path, app -> object_filename );
123 if ( ! pnd_pnd_mount ( pnd_run_script, fullpath, uid ) ) {
124 pnd_log ( pndn_debug, "Couldn't mount '%s' for preview\n", fullpath );
125 return ( 0 ); // couldn't mount?!
128 sprintf ( filepath, "%s/%s/%s", PND_MOUNT_PATH, uid, app -> preview_pic1 );
131 // load whatever path we've got
132 s = IMG_Load ( filepath );
135 // unmount it, if mounted
136 if ( fullpath [ 0 ] ) {
137 pnd_pnd_unmount ( pnd_run_script, fullpath, uid );
139 pnd_log ( pndn_debug, "Couldn't open image '%s' for preview\n", filepath );
143 // try to copy file to the cache, if we're doing that, and if mounted
144 if ( g_pvwcache && fullpath [ 0 ] ) {
145 char cacheoutpath [ PATH_MAX ] = "";
147 // figure out where we want to write the file to
148 if ( cache_find_writable ( app -> object_path, cacheoutpath, PATH_MAX ) ) {
149 static char *cache_path = NULL;
150 char buffer [ PATH_MAX ];
151 if ( ! cache_path ) {
152 cache_path = pnd_conf_get_as_char ( g_conf, "previewpic.cache_path" );
155 snprintf ( buffer, PATH_MAX, "%s/%s", cacheoutpath, cache_path );
157 if ( stat ( buffer, &statbuf ) != 0 ) {
158 snprintf ( buffer, PATH_MAX, "/bin/mkdir -p %s/%s", cacheoutpath, cache_path );
161 // set up target filename to copy
162 snprintf ( buffer, PATH_MAX, "%s/%s/%s.png", cacheoutpath, cache_path, app -> unique_id );
163 pnd_log ( pndn_debug, "Found free space to cache preview to here: %s", buffer );
164 if ( ! pnd_filecopy ( filepath, buffer ) ) {
165 pnd_log ( pndn_error, "ERROR: Copying preview from %s to %s", filepath, buffer );
168 pnd_log ( pndn_warning, "WARN: Couldn't find a device to cache preview to.\n" );
171 } // preview file cache
173 // unmount it, if mounted
174 if ( fullpath [ 0 ] ) {
175 pnd_pnd_unmount ( pnd_run_script, fullpath, app -> unique_id );
178 //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
181 if ( s -> w < maxwidth ) {
183 if ( pnd_conf_get_as_int_d ( g_conf, "previewpic.scale_up_bool", 1 ) ) {
185 double scale = (double)maxwidth / (double)s -> w;
186 //pnd_log ( pndn_debug, " Upscaling; scale factor %f\n", scale );
187 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
188 SDL_FreeSurface ( s );
191 } else if ( s -> w > maxwidth ) {
193 double scale = (double)maxwidth / (double)s -> w;
194 //pnd_log ( pndn_debug, " Downscaling; scale factor %f\n", scale );
195 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
196 SDL_FreeSurface ( s );
201 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
202 bzero ( c, sizeof(mm_cache_t) );
204 if ( ! g_preview_cache ) {
207 c -> next = g_preview_cache;
211 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
217 unsigned char cache_icon ( pnd_disco_t *app, unsigned char maxwidth, unsigned char maxheight ) {
221 // check if already cached
222 if ( ( c = cache_query_icon ( app -> unique_id ) ) ) {
223 return ( 1 ); // already got it
226 // not cached, load it up
228 unsigned char *iconbuf = NULL;
229 unsigned int buflen = 0;
231 // same-path icon override?
232 char ovrfile [ PATH_MAX ];
234 sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
235 fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
237 strcpy ( fixpxml, ".png" );
239 if ( stat ( ovrfile, &statbuf ) == 0 ) {
240 buflen = statbuf.st_size;
241 if ( ( iconbuf = malloc ( statbuf.st_size ) ) ) {
242 int fd = open ( ovrfile, O_RDONLY );
244 if ( read ( fd, iconbuf, statbuf.st_size ) != statbuf.st_size ) {
255 // perhaps pndnotifyd has dropped a copy of the icon into /tmp?
258 static char *iconpath = NULL;
261 iconpath = pnd_conf_get_as_char ( g_desktopconf, "desktop.iconpath" );
264 sprintf ( ovrfile, "%s/%s.png", iconpath, app -> unique_id );
266 // making sure the file is at least a few seconds old, to help avoid race condition
268 // *** commented out this stat() 'race test' since it buggers up icon loading at boot, if using .desktop instead of .pnd scanning..
269 if ( stat ( ovrfile, &statbuf ) == 0 /*&& time ( NULL ) - statbuf.st_mtime > 5*/ ) { // race with pndnotifyd
270 buflen = statbuf.st_size;
271 if ( ( iconbuf = malloc ( statbuf.st_size ) ) ) {
272 int fd = open ( ovrfile, O_RDONLY );
274 if ( read ( fd, iconbuf, statbuf.st_size ) != statbuf.st_size ) {
287 // if this is a real pnd file (dir-app or pnd-file-app), then try to pull icon from there
290 if ( app -> object_flags & PND_DISCO_GENERATED ) {
292 // maybe we can discover this single-file and find an icon?
293 if ( strcasestr ( app -> object_filename, PND_PACKAGE_FILEEXT ) ) {
295 // looks like a pnd, now what do we do..
296 pnd_box_handle h = pnd_disco_file ( app -> object_path, app -> object_filename );
299 pnd_disco_t *d = pnd_box_get_head ( h );
300 iconbuf = pnd_emit_icon_to_buffer ( d, &buflen );
303 } // filename has .pnd?
307 // pull icon into buffer from .pnd if not already found an icon
308 iconbuf = pnd_emit_icon_to_buffer ( app, &buflen );
312 } // already got icon?
318 // ready up a RWbuffer for SDL
319 SDL_RWops *rwops = SDL_RWFromMem ( iconbuf, buflen );
321 s = IMG_Load_RW ( rwops, 1 /* free the rwops */ );
327 free ( iconbuf ); // ditch the icon from ram
329 //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
332 if ( s -> w < maxwidth ) {
334 if ( pnd_conf_get_as_int_d ( g_conf, "grid.scale_up_bool", 1 ) ) {
336 double scale = (double)maxwidth / (double)s -> w;
337 //pnd_log ( pndn_debug, " Upscaling; scale factor %f\n", scale );
338 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
339 SDL_FreeSurface ( s );
342 } else if ( s -> w > maxwidth ) {
344 double scale = (double)maxwidth / (double)s -> w;
345 //pnd_log ( pndn_debug, " Downscaling; scale factor %f\n", scale );
346 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
347 SDL_FreeSurface ( s );
352 SDL_Surface *scaled_tiny = SDL_ConvertSurface ( s, s -> format, s -> flags );
353 extern ui_context_t ui_display_context;
354 scaled_tiny = ui_scale_image ( scaled_tiny, -1 , ui_display_context.text_height );
357 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
358 bzero ( c, sizeof(mm_cache_t) );
360 if ( ! g_icon_cache ) {
363 c -> next = g_icon_cache;
367 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
369 c -> itiny = scaled_tiny;
374 mm_cache_t *cache_query ( char *id, mm_cache_t *head ) {
375 mm_cache_t *iter = head;
382 if ( iter -> uniqueid &&
383 strcasecmp ( iter -> uniqueid, id ) == 0 )
393 mm_cache_t *cache_query_icon ( char *id ) {
394 return ( cache_query ( id, g_icon_cache ) );
397 mm_cache_t *cache_query_preview ( char *id ) {
398 return ( cache_query ( id, g_preview_cache ) );
401 unsigned char cache_find_writable ( char *originpath, char *r_writepath, unsigned int len ) {
402 static char *searchpaths = NULL;
403 static unsigned int minfree = 0;
404 char searchpath [ PATH_MAX ] = "";
405 char cmdbuf [ PATH_MAX ];
407 unsigned int freespace = 0;
409 // figure out the mountpoint for the file, and stick that in at front of searchpath
410 // so that it will get picked first, if possible.
411 char mountpath [ PATH_MAX ];
412 if ( pnd_determine_mountpoint ( originpath, mountpath, PATH_MAX ) ) {
413 sprintf ( searchpath, "%s:", mountpath );
414 //pnd_log ( pndn_debug, "Preferred cache target for %s: %s\n", originpath, mountpath );
417 // try to find a device, in order of searchpath, with enough space for this cache-out
420 // populate the searchpath
421 if ( ! searchpaths ) {
422 searchpaths = pnd_conf_get_as_char ( g_conf, "previewpic.cache_searchpath" );
423 minfree = pnd_conf_get_as_int_d ( g_conf, "previewpic.cache_minfree", 500 );
426 if ( ! searchpaths ) {
427 return ( 0 ); // fail!
430 strncat ( searchpath, searchpaths, PATH_MAX );
435 // since I didn't figure out which /sys/block I can pull remaining space from, I'll use df for now :/
436 sprintf ( cmdbuf, "/bin/df %s", buffer );
438 f = popen ( cmdbuf, "r" );
441 while ( fgets ( cmdbuf, PATH_MAX, f ) ) {
443 // /dev/sdc2 7471392 725260 6366600 11% /media/IMAGE
444 if ( sscanf ( cmdbuf, "%*s %*u %*u %u %*u %*s\n", &freespace ) == 1 ) {
445 strncpy ( r_writepath, buffer, len );
446 if ( freespace > minfree ) {