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?
74 char fullpath [ PATH_MAX ] = "";
75 char filepath [ PATH_MAX ] = "";
78 static char *cache_findpath = NULL;
79 if ( ! cache_findpath ) {
80 cache_findpath = pnd_conf_get_as_char ( g_conf, "previewpic.cache_findpath" );
82 char buffer [ FILENAME_MAX ];
83 sprintf ( buffer, "%s.png", app -> unique_id );
84 char *f = pnd_locate_filename ( cache_findpath, buffer );
86 strncpy ( filepath, f, PATH_MAX );
90 // if we don't have a file path sorted out yet, means we need to mount and figure it
91 if ( ! filepath [ 0 ] ) {
92 sprintf ( fullpath, "%s/%s", app -> object_path, app -> object_filename );
94 if ( ! pnd_pnd_mount ( pnd_run_script, fullpath, app -> unique_id ) ) {
95 pnd_log ( pndn_debug, "Couldn't mount '%s' for preview\n", fullpath );
96 return ( 0 ); // couldn't mount?!
99 sprintf ( filepath, "%s/%s/%s", PND_MOUNT_PATH, app -> unique_id, app -> preview_pic1 );
102 // load whatever path we've got
103 s = IMG_Load ( filepath );
106 // unmount it, if mounted
107 if ( fullpath [ 0 ] ) {
108 pnd_pnd_unmount ( pnd_run_script, fullpath, app -> unique_id );
110 pnd_log ( pndn_debug, "Couldn't open image '%s' for preview\n", filepath );
114 // try to copy file to the cache, if we're doing that, and if mounted
115 if ( g_pvwcache && fullpath [ 0 ] ) {
116 char cacheoutpath [ PATH_MAX ] = "";
118 // figure out where we want to write the file to
119 if ( cache_find_writable ( cacheoutpath, PATH_MAX ) ) {
120 static char *cache_path = NULL;
121 char buffer [ PATH_MAX ];
122 if ( ! cache_path ) {
123 cache_path = pnd_conf_get_as_char ( g_conf, "previewpic.cache_path" );
126 snprintf ( buffer, PATH_MAX, "%s/%s", cacheoutpath, cache_path );
128 if ( stat ( buffer, &statbuf ) != 0 ) {
129 snprintf ( buffer, PATH_MAX, "/bin/mkdir -p %s/%s", cacheoutpath, cache_path );
132 // set up target filename to copy
133 snprintf ( buffer, PATH_MAX, "%s/%s/%s.png", cacheoutpath, cache_path, app -> unique_id );
134 pnd_log ( pndn_debug, "Found free space to cache preview to here: %s", buffer );
135 if ( ! pnd_filecopy ( filepath, buffer ) ) {
136 pnd_log ( pndn_error, "ERROR: Copying preview from %s to %s", filepath, buffer );
139 pnd_log ( pndn_warning, "WARN: Couldn't find a device to cache preview to.\n" );
142 } // preview file cache
144 // unmount it, if mounted
145 if ( fullpath [ 0 ] ) {
146 pnd_pnd_unmount ( pnd_run_script, fullpath, app -> unique_id );
149 //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
152 if ( s -> w < maxwidth ) {
154 if ( pnd_conf_get_as_int_d ( g_conf, "previewpic.scale_up_bool", 1 ) ) {
156 double scale = (double)maxwidth / (double)s -> w;
157 //pnd_log ( pndn_debug, " Upscaling; scale factor %f\n", scale );
158 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
159 SDL_FreeSurface ( s );
162 } else if ( s -> w > maxwidth ) {
164 double scale = (double)maxwidth / (double)s -> w;
165 //pnd_log ( pndn_debug, " Downscaling; scale factor %f\n", scale );
166 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
167 SDL_FreeSurface ( s );
172 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
173 bzero ( c, sizeof(mm_cache_t) );
175 if ( ! g_preview_cache ) {
178 c -> next = g_preview_cache;
182 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
188 unsigned char cache_icon ( pnd_disco_t *app, unsigned char maxwidth, unsigned char maxheight ) {
192 // check if already cached
193 if ( ( c = cache_query_icon ( app -> unique_id ) ) ) {
194 return ( 1 ); // already got it
197 // not cached, load it up
199 unsigned char *iconbuf = NULL;
200 unsigned int buflen = 0;
202 // same-path override?
203 char ovrfile [ PATH_MAX ];
205 sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
206 fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
208 strcpy ( fixpxml, ".png" );
210 if ( stat ( ovrfile, &statbuf ) == 0 ) {
211 buflen = statbuf.st_size;
212 if ( ( iconbuf = malloc ( statbuf.st_size ) ) ) {
213 int fd = open ( ovrfile, O_RDONLY );
215 if ( read ( fd, iconbuf, statbuf.st_size ) != statbuf.st_size ) {
226 // pull icon into buffer from .pnd
228 iconbuf = pnd_emit_icon_to_buffer ( app, &buflen );
235 // ready up a RWbuffer for SDL
236 SDL_RWops *rwops = SDL_RWFromMem ( iconbuf, buflen );
238 s = IMG_Load_RW ( rwops, 1 /* free the rwops */ );
244 free ( iconbuf ); // ditch the icon from ram
246 //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
249 if ( s -> w < maxwidth ) {
251 if ( pnd_conf_get_as_int_d ( g_conf, "grid.scale_up_bool", 1 ) ) {
253 double scale = (double)maxwidth / (double)s -> w;
254 //pnd_log ( pndn_debug, " Upscaling; scale factor %f\n", scale );
255 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
256 SDL_FreeSurface ( s );
259 } else if ( s -> w > maxwidth ) {
261 double scale = (double)maxwidth / (double)s -> w;
262 //pnd_log ( pndn_debug, " Downscaling; scale factor %f\n", scale );
263 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
264 SDL_FreeSurface ( s );
269 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
270 bzero ( c, sizeof(mm_cache_t) );
272 if ( ! g_icon_cache ) {
275 c -> next = g_icon_cache;
279 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
285 mm_cache_t *cache_query ( char *id, mm_cache_t *head ) {
286 mm_cache_t *iter = head;
293 if ( iter -> uniqueid &&
294 strcasecmp ( iter -> uniqueid, id ) == 0 )
304 mm_cache_t *cache_query_icon ( char *id ) {
305 return ( cache_query ( id, g_icon_cache ) );
308 mm_cache_t *cache_query_preview ( char *id ) {
309 return ( cache_query ( id, g_preview_cache ) );
312 unsigned char cache_find_writable ( char *r_writepath, unsigned int len ) {
313 static char *searchpath = NULL;
314 static unsigned int minfree = 0;
315 char cmdbuf [ PATH_MAX ];
317 unsigned int freespace = 0;
319 // try to find a device, in order of searchpath, with enough space for this cache-out
321 if ( ! searchpath ) {
322 searchpath = pnd_conf_get_as_char ( g_conf, "previewpic.cache_searchpath" );
323 minfree = pnd_conf_get_as_int_d ( g_conf, "previewpic.cache_minfree", 500 );
326 if ( ! searchpath ) {
327 return ( 0 ); // fail!
333 // since I didn't figure out which /sys/block I can pull remaining space from, I'll use df for now :/
334 sprintf ( cmdbuf, "/bin/df %s", buffer );
336 f = popen ( cmdbuf, "r" );
339 while ( fgets ( cmdbuf, PATH_MAX, f ) ) {
341 // /dev/sdc2 7471392 725260 6366600 11% /media/IMAGE
342 if ( sscanf ( cmdbuf, "%*s %*u %*u %u %*u %*s\n", &freespace ) == 1 ) {
343 strncpy ( r_writepath, buffer, len );
344 if ( freespace > minfree ) {