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 char maxwidth, unsigned char 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 double scale = (double)maxwidth / (double)s -> w;
77 //pnd_log ( pndn_debug, " Upscaling; scale factor %f\n", scale );
78 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
79 SDL_FreeSurface ( s );
81 } else if ( s -> w > maxwidth ) {
83 double scale = (double)maxwidth / (double)s -> w;
84 //pnd_log ( pndn_debug, " Downscaling; scale factor %f\n", scale );
85 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
86 SDL_FreeSurface ( s );
91 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
92 bzero ( c, sizeof(mm_cache_t) );
94 if ( ! g_preview_cache ) {
97 c -> next = g_preview_cache;
101 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
107 unsigned char cache_icon ( pnd_disco_t *app, unsigned char maxwidth, unsigned char maxheight ) {
111 // check if already cached
112 if ( ( c = cache_query_icon ( app -> unique_id ) ) ) {
113 return ( 1 ); // already got it
116 // not cached, load it up
119 // pull icon into buffer
120 unsigned int buflen = 0;
121 unsigned char *iconbuf;
122 iconbuf = pnd_emit_icon_to_buffer ( app, &buflen );
128 // ready up a RWbuffer for SDL
129 SDL_RWops *rwops = SDL_RWFromMem ( iconbuf, buflen );
131 s = IMG_Load_RW ( rwops, 1 /* free the rwops */ );
137 free ( iconbuf ); // ditch the icon from ram
139 //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );
142 if ( s -> w < maxwidth ) {
144 double scale = (double)maxwidth / (double)s -> w;
145 //pnd_log ( pndn_debug, " Upscaling; scale factor %f\n", scale );
146 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
147 SDL_FreeSurface ( s );
149 } else if ( s -> w > maxwidth ) {
151 double scale = (double)maxwidth / (double)s -> w;
152 //pnd_log ( pndn_debug, " Downscaling; scale factor %f\n", scale );
153 scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
154 SDL_FreeSurface ( s );
159 c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
160 bzero ( c, sizeof(mm_cache_t) );
162 if ( ! g_icon_cache ) {
165 c -> next = g_icon_cache;
169 strncpy ( c -> uniqueid, app -> unique_id, 1000 );
175 mm_cache_t *cache_query ( char *id, mm_cache_t *head ) {
176 mm_cache_t *iter = head;
183 if ( iter -> uniqueid &&
184 strcasecmp ( iter -> uniqueid, id ) == 0 )
194 mm_cache_t *cache_query_icon ( char *id ) {
195 return ( cache_query ( id, g_icon_cache ) );
198 mm_cache_t *cache_query_preview ( char *id ) {
199 return ( cache_query ( id, g_preview_cache ) );