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"
41 extern pnd_conf_handle g_conf;
42 extern unsigned char g_pvwcache;
44 mm_cache_t *g_icon_cache = NULL;
45 mm_cache_t *g_preview_cache = NULL;
47 unsigned char cache_preview ( pnd_disco_t *app, unsigned int maxwidth, unsigned int maxheight ) {
51 // does this sucker even have a preview?
52 if ( ! app -> preview_pic1 ) {
53 return ( 1 ); // nothing here, so thats fine
56 // check if already cached
57 if ( ( c = cache_query_preview ( app -> unique_id ) ) ) {
58 return ( 1 ); // already got it
61 // not cached, load it up
65 ui_show_hourglass ( 1 /* updaterect*/ );
67 // see if we can mount the pnd/dir
68 // does preview file exist?
69 // if so, load it up, size it, cache it
70 // if not, warning and bail
73 // can we mount? or can we find it in preview cache? or an override?
74 char fullpath [ PATH_MAX ] = "";
75 char filepath [ PATH_MAX ] = "";
77 // first, check for preview override
79 char ovrfile [ PATH_MAX ];
81 sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
82 fooby = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
84 sprintf ( fooby, "_pvw#%u.png", app -> subapp_number );
86 if ( stat ( ovrfile, &statbuf ) == 0 ) {
87 strncpy ( filepath, ovrfile, PATH_MAX );
92 // if not yet found, try to find in cache
93 if ( filepath [ 0 ] == '\0' && g_pvwcache ) {
94 static char *cache_findpath = NULL;
95 if ( ! cache_findpath ) {
96 cache_findpath = pnd_conf_get_as_char ( g_conf, "previewpic.cache_findpath" );
98 char buffer [ FILENAME_MAX ];
99 sprintf ( buffer, "%s.png", app -> unique_id );
100 char *f = pnd_locate_filename ( cache_findpath, buffer );
102 strncpy ( filepath, f, PATH_MAX );
106 // unique-id to use for the cache mount
107 char *uid = app -> unique_id;
109 if ( app -> appdata_dirname ) {
110 uid = app -> appdata_dirname;
113 // if we don't have a file path sorted out yet, means we need to mount and figure it
114 if ( ! filepath [ 0 ] ) {
115 sprintf ( fullpath, "%s/%s", app -> object_path, app -> object_filename );
117 if ( ! pnd_pnd_mount ( pnd_run_script, fullpath, uid ) ) {
118 pnd_log ( pndn_debug, "Couldn't mount '%s' for preview\n", fullpath );
119 return ( 0 ); // couldn't mount?!
122 sprintf ( filepath, "%s/%s/%s", PND_MOUNT_PATH, uid, app -> preview_pic1 );
125 // load whatever path we've got
126 s = IMG_Load ( filepath );
129 // unmount it, if mounted
130 if ( fullpath [ 0 ] ) {
131 pnd_pnd_unmount ( pnd_run_script, fullpath, uid );
133 pnd_log ( pndn_debug, "Couldn't open image '%s' for preview\n", filepath );
137 // try to copy file to the cache, if we're doing that, and if mounted
138 if ( g_pvwcache && fullpath [ 0 ] ) {
139 char cacheoutpath [ PATH_MAX ] = "";
141 // figure out where we want to write the file to
142 if ( cache_find_writable ( app -> object_path, cacheoutpath, PATH_MAX ) ) {
143 static char *cache_path = NULL;
144 char buffer [ PATH_MAX ];
145 if ( ! cache_path ) {
146 cache_path = pnd_conf_get_as_char ( g_conf, "previewpic.cache_path" );
149 snprintf ( buffer, PATH_MAX, "%s/%s", cacheoutpath, cache_path );
151 if ( stat ( buffer, &statbuf ) != 0 ) {
152 snprintf ( buffer, PATH_MAX, "/bin/mkdir -p %s/%s", cacheoutpath, cache_path );
155 // set up target filename to copy
156 snprintf ( buffer, PATH_MAX, "%s/%s/%s.png", cacheoutpath, cache_path, app -> unique_id );
157 pnd_log ( pndn_debug, "Found free space to cache preview to here: %s", buffer );
158 if ( ! pnd_filecopy ( filepath, buffer ) ) {
159 pnd_log ( pndn_error, "ERROR: Copying preview from %s to %s", filepath, buffer );
162 pnd_log ( pndn_warning, "WARN: Couldn't find a device to cache preview to.\n" );
165 } // preview file cache
167 // unmount it, if mounted
168 if ( fullpath [ 0 ] ) {
169 pnd_pnd_unmount ( pnd_run_script, fullpath, app -> unique_id );
172 //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
175 if ( s -> w < maxwidth ) {
177 if ( pnd_conf_get_as_int_d ( g_conf, "previewpic.scale_up_bool", 1 ) ) {
179 double scale = (double)maxwidth / (double)s -> w;
180 //pnd_log ( pndn_debug, " Upscaling; scale factor %f\n", scale );
181 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
182 SDL_FreeSurface ( s );
185 } else if ( s -> w > maxwidth ) {
187 double scale = (double)maxwidth / (double)s -> w;
188 //pnd_log ( pndn_debug, " Downscaling; scale factor %f\n", scale );
189 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
190 SDL_FreeSurface ( s );
195 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
196 bzero ( c, sizeof(mm_cache_t) );
198 if ( ! g_preview_cache ) {
201 c -> next = g_preview_cache;
205 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
211 unsigned char cache_icon ( pnd_disco_t *app, unsigned char maxwidth, unsigned char maxheight ) {
215 // check if already cached
216 if ( ( c = cache_query_icon ( app -> unique_id ) ) ) {
217 return ( 1 ); // already got it
220 // not cached, load it up
222 unsigned char *iconbuf = NULL;
223 unsigned int buflen = 0;
225 // same-path override?
226 char ovrfile [ PATH_MAX ];
228 sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
229 fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
231 strcpy ( fixpxml, ".png" );
233 if ( stat ( ovrfile, &statbuf ) == 0 ) {
234 buflen = statbuf.st_size;
235 if ( ( iconbuf = malloc ( statbuf.st_size ) ) ) {
236 int fd = open ( ovrfile, O_RDONLY );
238 if ( read ( fd, iconbuf, statbuf.st_size ) != statbuf.st_size ) {
249 // if this is a real pnd file (dir-app or pnd-file-app), then try to pull icon from there
252 if ( app -> object_flags & PND_DISCO_GENERATED ) {
254 // maybe we can discover this single-file and find an icon?
255 if ( strcasestr ( app -> object_filename, PND_PACKAGE_FILEEXT ) ) {
257 // looks like a pnd, now what do we do..
258 pnd_box_handle h = pnd_disco_file ( app -> object_path, app -> object_filename );
261 pnd_disco_t *d = pnd_box_get_head ( h );
262 iconbuf = pnd_emit_icon_to_buffer ( d, &buflen );
265 } // filename has .pnd?
269 // pull icon into buffer from .pnd if not already found an icon
270 iconbuf = pnd_emit_icon_to_buffer ( app, &buflen );
274 } // already got icon?
280 // ready up a RWbuffer for SDL
281 SDL_RWops *rwops = SDL_RWFromMem ( iconbuf, buflen );
283 s = IMG_Load_RW ( rwops, 1 /* free the rwops */ );
289 free ( iconbuf ); // ditch the icon from ram
291 //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
294 if ( s -> w < maxwidth ) {
296 if ( pnd_conf_get_as_int_d ( g_conf, "grid.scale_up_bool", 1 ) ) {
298 double scale = (double)maxwidth / (double)s -> w;
299 //pnd_log ( pndn_debug, " Upscaling; scale factor %f\n", scale );
300 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
301 SDL_FreeSurface ( s );
304 } else if ( s -> w > maxwidth ) {
306 double scale = (double)maxwidth / (double)s -> w;
307 //pnd_log ( pndn_debug, " Downscaling; scale factor %f\n", scale );
308 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
309 SDL_FreeSurface ( s );
314 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
315 bzero ( c, sizeof(mm_cache_t) );
317 if ( ! g_icon_cache ) {
320 c -> next = g_icon_cache;
324 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
330 mm_cache_t *cache_query ( char *id, mm_cache_t *head ) {
331 mm_cache_t *iter = head;
338 if ( iter -> uniqueid &&
339 strcasecmp ( iter -> uniqueid, id ) == 0 )
349 mm_cache_t *cache_query_icon ( char *id ) {
350 return ( cache_query ( id, g_icon_cache ) );
353 mm_cache_t *cache_query_preview ( char *id ) {
354 return ( cache_query ( id, g_preview_cache ) );
357 unsigned char cache_find_writable ( char *originpath, char *r_writepath, unsigned int len ) {
358 static char *searchpaths = NULL;
359 static unsigned int minfree = 0;
360 char searchpath [ PATH_MAX ] = "";
361 char cmdbuf [ PATH_MAX ];
363 unsigned int freespace = 0;
365 // figure out the mountpoint for the file, and stick that in at front of searchpath
366 // so that it will get picked first, if possible.
367 char mountpath [ PATH_MAX ];
368 if ( pnd_determine_mountpoint ( originpath, mountpath, PATH_MAX ) ) {
369 sprintf ( searchpath, "%s:", mountpath );
370 //pnd_log ( pndn_debug, "Preferred cache target for %s: %s\n", originpath, mountpath );
373 // try to find a device, in order of searchpath, with enough space for this cache-out
376 // populate the searchpath
377 if ( ! searchpaths ) {
378 searchpaths = pnd_conf_get_as_char ( g_conf, "previewpic.cache_searchpath" );
379 minfree = pnd_conf_get_as_int_d ( g_conf, "previewpic.cache_minfree", 500 );
382 if ( ! searchpaths ) {
383 return ( 0 ); // fail!
386 strncat ( searchpath, searchpaths, PATH_MAX );
391 // since I didn't figure out which /sys/block I can pull remaining space from, I'll use df for now :/
392 sprintf ( cmdbuf, "/bin/df %s", buffer );
394 f = popen ( cmdbuf, "r" );
397 while ( fgets ( cmdbuf, PATH_MAX, f ) ) {
399 // /dev/sdc2 7471392 725260 6366600 11% /media/IMAGE
400 if ( sscanf ( cmdbuf, "%*s %*u %*u %u %*u %*s\n", &freespace ) == 1 ) {
401 strncpy ( r_writepath, buffer, len );
402 if ( freespace > minfree ) {