6 #include "SDL_rotozoom.h"
9 #include "pnd_utility.h"
11 #include "pnd_container.h"
12 #include "pnd_discovery.h"
13 #include "pnd_logger.h"
14 #include "pnd_desktop.h"
15 #include "pnd_pndfiles.h"
22 extern pnd_conf_handle g_conf;
24 mm_cache_t *g_icon_cache = NULL;
25 mm_cache_t *g_preview_cache = NULL;
27 unsigned char cache_preview ( pnd_disco_t *app, unsigned int maxwidth, unsigned int maxheight ) {
31 // does this sucker even have a preview?
32 if ( ! app -> preview_pic1 ) {
33 return ( 1 ); // nothing here, so thats fine
36 // check if already cached
37 if ( ( c = cache_query_preview ( app -> unique_id ) ) ) {
38 return ( 1 ); // already got it
41 // not cached, load it up
44 // see if we can mount the pnd/dir
45 // does preview file exist?
46 // if so, load it up, size it, cache it
47 // if not, warning and bail
51 char fullpath [ PATH_MAX ];
52 char filepath [ PATH_MAX ];
54 sprintf ( fullpath, "%s/%s", app -> object_path, app -> object_filename );
56 if ( ! pnd_pnd_mount ( pnd_run_script, fullpath, app -> unique_id ) ) {
57 pnd_log ( pndn_debug, "Couldn't mount '%s' for preview\n", fullpath );
58 return ( 0 ); // couldn't mount?!
61 sprintf ( filepath, "%s/%s/%s", PND_MOUNT_PATH, app -> unique_id, app -> preview_pic1 );
62 s = IMG_Load ( filepath );
64 pnd_pnd_unmount ( pnd_run_script, fullpath, app -> unique_id );
67 pnd_log ( pndn_debug, "Couldn't open image '%s' for preview\n", filepath );
71 //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
74 if ( s -> w < maxwidth ) {
76 if ( pnd_conf_get_as_int_d ( g_conf, "previewpic.scale_up_bool", 1 ) ) {
78 double scale = (double)maxwidth / (double)s -> w;
79 //pnd_log ( pndn_debug, " Upscaling; scale factor %f\n", scale );
80 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
81 SDL_FreeSurface ( s );
84 } else if ( s -> w > maxwidth ) {
86 double scale = (double)maxwidth / (double)s -> w;
87 //pnd_log ( pndn_debug, " Downscaling; scale factor %f\n", scale );
88 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
89 SDL_FreeSurface ( s );
94 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
95 bzero ( c, sizeof(mm_cache_t) );
97 if ( ! g_preview_cache ) {
100 c -> next = g_preview_cache;
104 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
110 unsigned char cache_icon ( pnd_disco_t *app, unsigned char maxwidth, unsigned char maxheight ) {
114 // check if already cached
115 if ( ( c = cache_query_icon ( app -> unique_id ) ) ) {
116 return ( 1 ); // already got it
119 // not cached, load it up
122 // pull icon into buffer
123 unsigned int buflen = 0;
124 unsigned char *iconbuf;
125 iconbuf = pnd_emit_icon_to_buffer ( app, &buflen );
131 // ready up a RWbuffer for SDL
132 SDL_RWops *rwops = SDL_RWFromMem ( iconbuf, buflen );
134 s = IMG_Load_RW ( rwops, 1 /* free the rwops */ );
140 free ( iconbuf ); // ditch the icon from ram
142 //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
145 if ( s -> w < maxwidth ) {
147 if ( pnd_conf_get_as_int_d ( g_conf, "grid.scale_up_bool", 1 ) ) {
149 double scale = (double)maxwidth / (double)s -> w;
150 //pnd_log ( pndn_debug, " Upscaling; scale factor %f\n", scale );
151 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
152 SDL_FreeSurface ( s );
155 } else if ( s -> w > maxwidth ) {
157 double scale = (double)maxwidth / (double)s -> w;
158 //pnd_log ( pndn_debug, " Downscaling; scale factor %f\n", scale );
159 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
160 SDL_FreeSurface ( s );
165 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
166 bzero ( c, sizeof(mm_cache_t) );
168 if ( ! g_icon_cache ) {
171 c -> next = g_icon_cache;
175 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
181 mm_cache_t *cache_query ( char *id, mm_cache_t *head ) {
182 mm_cache_t *iter = head;
189 if ( iter -> uniqueid &&
190 strcasecmp ( iter -> uniqueid, id ) == 0 )
200 mm_cache_t *cache_query_icon ( char *id ) {
201 return ( cache_query ( id, g_icon_cache ) );
204 mm_cache_t *cache_query_preview ( char *id ) {
205 return ( cache_query ( id, g_preview_cache ) );