minimenu - fix so wraparound to offscreen tabs ensures visible, and when doing app...
[pandora-libraries.git] / minimenu / mmui.c
1
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 */
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #define __USE_GNU /* for strcasestr */
9 #include <string.h> /* for making ftw.h happy */
10 #include <time.h>
11 #include <ftw.h>
12 #include "SDL.h"
13 #include "SDL_audio.h"
14 #include "SDL_image.h"
15 #include "SDL_ttf.h"
16 #include "SDL_gfxPrimitives.h"
17 #include "SDL_rotozoom.h"
18 #include "SDL_thread.h"
19 #include <dirent.h>
20
21 #include "pnd_conf.h"
22 #include "pnd_logger.h"
23 #include "pnd_pxml.h"
24 #include "pnd_container.h"
25 #include "pnd_discovery.h"
26 #include "pnd_apps.h"
27 #include "pnd_device.h"
28 #include "../lib/pnd_pathiter.h"
29 #include "pnd_utility.h"
30 #include "pnd_pndfiles.h"
31
32 #include "mmenu.h"
33 #include "mmcat.h"
34 #include "mmcache.h"
35 #include "mmui.h"
36 #include "mmwrapcmd.h"
37
38 #define CHANGED_NOTHING     (0)
39 #define CHANGED_CATEGORY    (1<<0)  /* changed to different category */
40 #define CHANGED_SELECTION   (1<<1)  /* changed app selection */
41 #define CHANGED_DATAUPDATE  (1<<2)  /* deferred preview pic or icon came in */
42 #define CHANGED_APPRELOAD   (1<<3)  /* new set of applications entirely */
43 #define CHANGED_EVERYTHING  (1<<4)  /* redraw it all! */
44 unsigned int render_mask = CHANGED_EVERYTHING;
45
46 /* SDL
47  */
48 SDL_Surface *sdl_realscreen = NULL;
49 unsigned int sdl_ticks = 0;
50 SDL_Thread *g_preview_thread = NULL;
51
52 enum { sdl_user_ticker = 0,
53        sdl_user_finishedpreview = 1,
54        sdl_user_finishedicon = 2,
55 };
56
57 /* app state
58  */
59 unsigned short int g_scale = 1; // 1 == noscale
60
61 SDL_Surface *g_imgcache [ IMG_MAX ];
62
63 TTF_Font *g_big_font = NULL;
64 TTF_Font *g_grid_font = NULL;
65 TTF_Font *g_detailtext_font = NULL;
66 TTF_Font *g_tab_font = NULL;
67
68 extern pnd_conf_handle g_conf;
69
70 /* current display state
71  */
72 int ui_rows_scrolled_down = 0;          // number of rows that should be missing from top of the display
73 mm_appref_t *ui_selected = NULL;
74 unsigned char ui_category = 0;          // current category
75 unsigned char ui_catshift = 0;          // how many cats are offscreen to the left
76
77 extern mm_category_t g_categories [ MAX_CATS ];
78 extern unsigned char g_categorycount;
79
80 static SDL_Surface *ui_scale_image ( SDL_Surface *s, unsigned int maxwidth, int maxheight ); // height -1 means ignore
81 static int ui_selected_index ( void );
82
83 unsigned char ui_setup ( void ) {
84
85   /* set up SDL
86    */
87
88   SDL_Init ( SDL_INIT_EVERYTHING | SDL_INIT_NOPARACHUTE );
89
90   SDL_JoystickOpen ( 0 ); // turn on joy-0
91
92   SDL_WM_SetCaption ( "mmenu", "mmenu" );
93
94   // hide the mouse cursor if we can
95   if ( SDL_ShowCursor ( -1 ) == 1 ) {
96     SDL_ShowCursor ( 0 );
97   }
98
99   atexit ( SDL_Quit );
100
101   // open up a surface
102   unsigned int svm = SDL_SWSURFACE /*| SDL_FULLSCREEN*/ /* 0*/;
103   if ( pnd_conf_get_as_int_d ( g_conf, "display.fullscreen", 0 ) > 0 ) {
104     svm |= SDL_FULLSCREEN;
105   }
106
107   sdl_realscreen =
108     SDL_SetVideoMode ( 800 * g_scale, 480 * g_scale, 16 /*bpp*/, svm );
109
110   if ( ! sdl_realscreen ) {
111     pnd_log ( pndn_error, "ERROR: Couldn't open SDL real screen; dieing." );
112     return ( 0 );
113   }
114
115   pnd_log ( pndn_debug, "Pixel format:" );
116   pnd_log ( pndn_debug, "bpp b: %u\n", sdl_realscreen -> format -> BitsPerPixel );
117   pnd_log ( pndn_debug, "bpp B: %u\n", sdl_realscreen -> format -> BytesPerPixel );
118   pnd_log ( pndn_debug, "R mask: %08x\n", sdl_realscreen -> format -> Rmask );
119   pnd_log ( pndn_debug, "G mask: %08x\n", sdl_realscreen -> format -> Gmask );
120   pnd_log ( pndn_debug, "B mask: %08x\n", sdl_realscreen -> format -> Bmask );
121
122 #if 0 // audio
123   {
124     SDL_AudioSpec fmt;
125
126     /* Set 16-bit stereo audio at 22Khz */
127     fmt.freq = 44100; //22050;
128     fmt.format = AUDIO_S16; //AUDIO_S16;
129     fmt.channels = 1;
130     fmt.samples = 2048;        /* A good value for games */
131     fmt.callback = mixaudio;
132     fmt.userdata = NULL;
133
134     /* Open the audio device and start playing sound! */
135     if ( SDL_OpenAudio ( &fmt, NULL ) < 0 ) {
136       zotlog ( "Unable to open audio: %s\n", SDL_GetError() );
137       exit ( 1 );
138     }
139
140     SDL_PauseAudio ( 0 );
141   }
142 #endif
143
144   // images
145   //IMG_Init ( IMG_INIT_JPG | IMG_INIT_PNG );
146
147   /* fonts
148    */
149
150   // init
151   if ( TTF_Init() == -1 ) {
152     pnd_log ( pndn_error, "ERROR: Couldn't set up SDL TTF lib\n" );
153     return ( 0 ); // couldn't set up SDL TTF
154   }
155
156   char fullpath [ PATH_MAX ];
157   // big font
158   sprintf ( fullpath, "%s/%s", g_skinpath, pnd_conf_get_as_char ( g_conf, "minimenu.font" ) );
159   g_big_font = TTF_OpenFont ( fullpath, pnd_conf_get_as_int_d ( g_conf, "minimenu.font_ptsize", 24 ) );
160   if ( ! g_big_font ) {
161     pnd_log ( pndn_error, "ERROR: Couldn't load font '%s' for size %u\n",
162               pnd_conf_get_as_char ( g_conf, "minimenu.font" ), pnd_conf_get_as_int_d ( g_conf, "minimenu.font_ptsize", 24 ) );
163     return ( 0 ); // couldn't set up SDL TTF
164   }
165
166   // grid font
167   sprintf ( fullpath, "%s/%s", g_skinpath, pnd_conf_get_as_char ( g_conf, "grid.font" ) );
168   g_grid_font = TTF_OpenFont ( fullpath, pnd_conf_get_as_int_d ( g_conf, "grid.font_ptsize", 10 ) );
169   if ( ! g_grid_font ) {
170     pnd_log ( pndn_error, "ERROR: Couldn't load font '%s' for size %u\n",
171               pnd_conf_get_as_char ( g_conf, "grid.font" ), pnd_conf_get_as_int_d ( g_conf, "grid.font_ptsize", 10 ) );
172     return ( 0 ); // couldn't set up SDL TTF
173   }
174
175   // detailtext font
176   sprintf ( fullpath, "%s/%s", g_skinpath, pnd_conf_get_as_char ( g_conf, "detailtext.font" ) );
177   g_detailtext_font = TTF_OpenFont ( fullpath, pnd_conf_get_as_int_d ( g_conf, "detailtext.font_ptsize", 10 ) );
178   if ( ! g_detailtext_font ) {
179     pnd_log ( pndn_error, "ERROR: Couldn't load font '%s' for size %u\n",
180               pnd_conf_get_as_char ( g_conf, "detailtext.font" ), pnd_conf_get_as_int_d ( g_conf, "detailtext.font_ptsize", 10 ) );
181     return ( 0 ); // couldn't set up SDL TTF
182   }
183
184   // tab font
185   sprintf ( fullpath, "%s/%s", g_skinpath, pnd_conf_get_as_char ( g_conf, "tabs.font" ) );
186   g_tab_font = TTF_OpenFont ( fullpath, pnd_conf_get_as_int_d ( g_conf, "tabs.font_ptsize", 10 ) );
187   if ( ! g_tab_font ) {
188     pnd_log ( pndn_error, "ERROR: Couldn't load font '%s' for size %u\n",
189               pnd_conf_get_as_char ( g_conf, "tabs.font" ), pnd_conf_get_as_int_d ( g_conf, "tabs.font_ptsize", 10 ) );
190     return ( 0 ); // couldn't set up SDL TTF
191   }
192
193   return ( 1 );
194 }
195
196 mm_imgcache_t g_imagecache [ IMG_TRUEMAX ] = {
197   { IMG_BACKGROUND_800480,    "graphics.IMG_BACKGROUND_800480" },
198   { IMG_BACKGROUND_TABMASK,   "graphics.IMG_BACKGROUND_TABMASK" },
199   { IMG_DETAIL_PANEL,         "graphics.IMG_DETAIL_PANEL" },
200   { IMG_DETAIL_BG,            "graphics.IMG_DETAIL_BG" },
201   { IMG_SELECTED_ALPHAMASK,   "graphics.IMG_SELECTED_ALPHAMASK" },
202   { IMG_TAB_SEL,              "graphics.IMG_TAB_SEL" },
203   { IMG_TAB_SEL_L,            "graphics.IMG_TAB_SEL_L" },
204   { IMG_TAB_SEL_R,            "graphics.IMG_TAB_SEL_R" },
205   { IMG_TAB_UNSEL,            "graphics.IMG_TAB_UNSEL" },
206   { IMG_TAB_UNSEL_L,          "graphics.IMG_TAB_UNSEL_L" },
207   { IMG_TAB_UNSEL_R,          "graphics.IMG_TAB_UNSEL_R" },
208   { IMG_TAB_LINE,             "graphics.IMG_TAB_LINE" },
209   { IMG_TAB_LINEL,            "graphics.IMG_TAB_LINEL" },
210   { IMG_TAB_LINER,            "graphics.IMG_TAB_LINER" },
211   { IMG_ICON_MISSING,         "graphics.IMG_ICON_MISSING" },
212   { IMG_SELECTED_HILITE,      "graphics.IMG_SELECTED_HILITE" },
213   { IMG_PREVIEW_MISSING,      "graphics.IMG_PREVIEW_MISSING" },
214   { IMG_ARROW_UP,             "graphics.IMG_ARROW_UP", },
215   { IMG_ARROW_DOWN,           "graphics.IMG_ARROW_DOWN", },
216   { IMG_ARROW_SCROLLBAR,      "graphics.IMG_ARROW_SCROLLBAR", },
217   { IMG_HOURGLASS,            "graphics.IMG_HOURGLASS", },
218   { IMG_FOLDER,               "graphics.IMG_FOLDER", },
219   { IMG_EXECBIN,              "graphics.IMG_EXECBIN", },
220   { IMG_MAX,                  NULL },
221 };
222
223 unsigned char ui_imagecache ( char *basepath ) {
224   unsigned int i;
225   char fullpath [ PATH_MAX ];
226
227   // loaded
228
229   for ( i = 0; i < IMG_MAX; i++ ) {
230
231     if ( g_imagecache [ i ].id != i ) {
232       pnd_log ( pndn_error, "ERROR: Internal table mismatch during caching [%u]\n", i );
233       exit ( -1 );
234     }
235
236     char *filename = pnd_conf_get_as_char ( g_conf, g_imagecache [ i ].confname );
237
238     if ( ! filename ) {
239       pnd_log ( pndn_error, "ERROR: Missing filename in conf for key: %s\n", g_imagecache [ i ].confname );
240       return ( 0 );
241     }
242
243     if ( filename [ 0 ] == '/' ) {
244       strncpy ( fullpath, filename, PATH_MAX );
245     } else {
246       sprintf ( fullpath, "%s/%s", basepath, filename );
247     }
248
249     if ( ! ( g_imagecache [ i ].i = IMG_Load ( fullpath ) ) ) {
250       pnd_log ( pndn_error, "ERROR: Couldn't load static cache image: %s\n", fullpath );
251       return ( 0 );
252     }
253
254   } // for
255
256   // generated
257   //g_imagecache [ IMG_SELECTED_ALPHAMASK ].i = SDL_CreateRGBSurface ( SDL_SWSURFACE, 60, 60, 32, 0xFF0000, 0x00FF00, 0xFF, 0xFF000000 );
258   //boxRGBA ( g_imagecache [ IMG_SELECTED_ALPHAMASK ].i, 0, 0, 60, 60, 100, 100, 100, 250 );
259
260   // post processing
261   //
262
263   // scale icons
264   g_imagecache [ IMG_ICON_MISSING ].i = ui_scale_image ( g_imagecache [ IMG_ICON_MISSING ].i, pnd_conf_get_as_int_d ( g_conf, "grid.icon_max_width", 50 ), -1 );
265   // scale text hilight
266   g_imagecache [ IMG_SELECTED_HILITE ].i = ui_scale_image ( g_imagecache [ IMG_SELECTED_HILITE ].i, pnd_conf_get_as_int_d ( g_conf, "grid.text_width", 50 ), -1 );
267   // scale preview no-pic
268   g_imagecache [ IMG_PREVIEW_MISSING ].i = ui_scale_image ( g_imagecache [ IMG_PREVIEW_MISSING ].i,
269                                                             pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 50 ),
270                                                             pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 50 ) );
271
272   // set alpha on detail panel
273   //SDL_SetAlpha ( g_imagecache [ IMG_DETAIL_BG ].i, SDL_SRCALPHA, pnd_conf_get_as_int_d ( g_conf, "display.detail_bg_alpha", 50 ) );
274
275   return ( 1 );
276 } // ui_imagecache
277
278 void ui_render ( void ) {
279
280   // 800x480:
281   // divide width: 550 / 250
282   // divide left side: 5 columns == 110px width
283   //   20px buffer either side == 70px wide icon + 20 + 20?
284
285   // what jobs to do during render?
286   //
287
288 #define R_BG     (1<<0)
289 #define R_TABS   (1<<1)
290 #define R_DETAIL (1<<2)
291 #define R_GRID   (1<<3)
292
293 #define R_ALL (R_BG|R_TABS|R_DETAIL|R_GRID)
294
295   unsigned int render_jobs_b = 0;
296
297   if ( render_mask & CHANGED_EVERYTHING ) {
298     render_jobs_b |= R_ALL;
299   }
300
301   if ( render_mask & CHANGED_SELECTION ) {
302     render_jobs_b |= R_GRID;
303     render_jobs_b |= R_DETAIL;
304   }
305
306   if ( render_mask & CHANGED_CATEGORY ) {
307     render_jobs_b |= R_ALL;
308   }
309
310   render_mask = CHANGED_NOTHING;
311
312   // render everything
313   //
314   unsigned int icon_rows;
315
316 #define MAXRECTS 200
317   SDL_Rect rects [ MAXRECTS ], src;
318   SDL_Rect *dest = rects;
319   bzero ( dest, sizeof(SDL_Rect)*MAXRECTS );
320
321   unsigned int row, displayrow, col;
322   mm_appref_t *appiter;
323
324   unsigned int screen_width = pnd_conf_get_as_int_d ( g_conf, "display.screen_width", 800 );
325
326   unsigned char row_max = pnd_conf_get_as_int_d ( g_conf, "grid.row_max", 4 );
327   unsigned char col_max = pnd_conf_get_as_int_d ( g_conf, "grid.col_max", 5 );
328
329   unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
330   unsigned int font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
331   unsigned int font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
332   unsigned int font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
333
334   unsigned int grid_offset_x = pnd_conf_get_as_int ( g_conf, "grid.grid_offset_x" );
335   unsigned int grid_offset_y = pnd_conf_get_as_int ( g_conf, "grid.grid_offset_y" );
336
337   unsigned int icon_offset_x = pnd_conf_get_as_int ( g_conf, "grid.icon_offset_x" );
338   unsigned int icon_offset_y = pnd_conf_get_as_int ( g_conf, "grid.icon_offset_y" );
339   unsigned int icon_max_width = pnd_conf_get_as_int ( g_conf, "grid.icon_max_width" );
340   unsigned int icon_max_height = pnd_conf_get_as_int ( g_conf, "grid.icon_max_height" );
341   unsigned int sel_icon_offset_x = pnd_conf_get_as_int_d ( g_conf, "grid.sel_offoffset_x", 0 );
342   unsigned int sel_icon_offset_y = pnd_conf_get_as_int_d ( g_conf, "grid.sel_offoffset_y", 0 );
343
344   unsigned int text_width = pnd_conf_get_as_int ( g_conf, "grid.text_width" );
345   unsigned int text_clip_x = pnd_conf_get_as_int ( g_conf, "grid.text_clip_x" );
346   unsigned int text_offset_x = pnd_conf_get_as_int ( g_conf, "grid.text_offset_x" );
347   unsigned int text_offset_y = pnd_conf_get_as_int ( g_conf, "grid.text_offset_y" );
348
349   unsigned int cell_width = pnd_conf_get_as_int ( g_conf, "grid.cell_width" );
350   unsigned int cell_height = pnd_conf_get_as_int ( g_conf, "grid.cell_height" );
351
352   // how many total rows do we need?
353   icon_rows = g_categories [ ui_category ].refcount / col_max;
354   if ( g_categories [ ui_category ].refcount % col_max > 0 ) {
355     icon_rows++;
356   }
357
358   // if no selected app yet, select the first one
359 #if 0
360   if ( ! ui_selected ) {
361     ui_selected = g_categories [ ui_category ].refs;
362   }
363 #endif
364
365   // reset touchscreen regions
366   ui_register_reset();
367
368   // ensure selection is visible
369   if ( ui_selected ) {
370
371     int index = ui_selected_index();
372     int topleft = col_max * ui_rows_scrolled_down;
373     int botright = ( col_max * ( ui_rows_scrolled_down + row_max ) - 1 );
374
375     if ( index < topleft ) {
376       ui_rows_scrolled_down -= pnd_conf_get_as_int_d ( g_conf, "grid.scroll_increment", 1 );
377       render_jobs_b |= R_ALL;
378     } else if ( index > botright ) {
379       ui_rows_scrolled_down += pnd_conf_get_as_int_d ( g_conf, "grid.scroll_increment", 1 );
380       render_jobs_b |= R_ALL;
381     }
382
383     if ( ui_rows_scrolled_down < 0 ) {
384       ui_rows_scrolled_down = 0;
385     } else if ( ui_rows_scrolled_down > icon_rows ) {
386       ui_rows_scrolled_down = icon_rows;
387     }
388
389   } // ensire visible
390
391   // render background
392   if ( render_jobs_b & R_BG ) {
393
394     if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
395       dest -> x = 0;
396       dest -> y = 0;
397       dest -> w = sdl_realscreen -> w;
398       dest -> h = sdl_realscreen -> h;
399       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, NULL /* whole image */, sdl_realscreen, dest /* 0,0 */ );
400       dest++;
401     }
402
403     // tabmask
404     if ( g_imagecache [ IMG_BACKGROUND_TABMASK ].i ) {
405       dest -> x = 0;
406       dest -> y = 0;
407       dest -> w = sdl_realscreen -> w;
408       dest -> h = sdl_realscreen -> h;
409       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_TABMASK ].i, NULL /* whole image */, sdl_realscreen, dest /* 0,0 */ );
410       dest++;
411     }
412
413   } // r_bg
414
415   // tabs
416   if ( g_imagecache [ IMG_TAB_SEL ].i && g_imagecache [ IMG_TAB_UNSEL ].i ) {
417     unsigned int tab_width = pnd_conf_get_as_int ( g_conf, "tabs.tab_width" );
418     unsigned int tab_height = pnd_conf_get_as_int ( g_conf, "tabs.tab_height" );
419     //unsigned int tab_selheight = pnd_conf_get_as_int ( g_conf, "tabs.tab_selheight" );
420     unsigned int tab_offset_x = pnd_conf_get_as_int ( g_conf, "tabs.tab_offset_x" );
421     unsigned int tab_offset_y = pnd_conf_get_as_int ( g_conf, "tabs.tab_offset_y" );
422     unsigned int text_offset_x = pnd_conf_get_as_int ( g_conf, "tabs.text_offset_x" );
423     unsigned int text_offset_y = pnd_conf_get_as_int ( g_conf, "tabs.text_offset_y" );
424     unsigned int text_width = pnd_conf_get_as_int ( g_conf, "tabs.text_width" );
425     unsigned int maxtab = ( screen_width / tab_width ) < g_categorycount ? ( screen_width / tab_width ) + ui_catshift : g_categorycount + ui_catshift;
426     unsigned int maxtabspot = ( screen_width / tab_width );
427
428     // draw tabs with categories
429     for ( col = ui_catshift;
430           col < maxtab;
431           col++ )
432     {
433
434       SDL_Surface *s;
435
436       // if this is the first (leftmost) tab, we use different artwork
437       // than if the other tabs (so skinner can link lines up nicely.)
438       if ( col == ui_catshift ) {
439         // leftmost tab, special case
440
441         if ( col == ui_category ) {
442           s = g_imagecache [ IMG_TAB_SEL_L ].i;
443         } else {
444           s = g_imagecache [ IMG_TAB_UNSEL_L ].i;
445         }
446
447       } else if ( col == maxtab - 1 ) {
448         // rightmost tab, special case
449
450         if ( col == ui_category ) {
451           s = g_imagecache [ IMG_TAB_SEL_R ].i;
452         } else {
453           s = g_imagecache [ IMG_TAB_UNSEL_R ].i;
454         }
455
456       } else {
457         // normal (not leftmost) tab
458
459         if ( col == ui_category ) {
460           s = g_imagecache [ IMG_TAB_SEL ].i;
461         } else {
462           s = g_imagecache [ IMG_TAB_UNSEL ].i;
463         }
464
465       } // first col, or not first col?
466
467       // draw tab
468       src.x = 0;
469       src.y = 0;
470 #if 0
471       src.w = tab_width;
472       if ( col == ui_category ) {
473         src.h = tab_selheight;
474       } else {
475         src.h = tab_height;
476       }
477 #else
478       src.w = s -> w;
479       src.h = s -> h;
480 #endif
481       dest -> x = tab_offset_x + ( (col-ui_catshift) * tab_width );
482       dest -> y = tab_offset_y;
483
484       // store touch info
485       ui_register_tab ( col, dest -> x, dest -> y, tab_width, tab_height );
486
487       if ( render_jobs_b & R_TABS ) {
488         //pnd_log ( pndn_debug, "tab %u at %ux%u\n", col, dest.x, dest.y );
489         SDL_BlitSurface ( s, &src, sdl_realscreen, dest );
490         dest++;
491
492         // draw tab line
493         if ( col == ui_category ) {
494           // no line for selected tab
495         } else {
496           if ( col - ui_catshift == 0 ) {
497             s = g_imagecache [ IMG_TAB_LINEL ].i;
498           } else if ( col - ui_catshift == maxtabspot - 1 ) {
499             s = g_imagecache [ IMG_TAB_LINER ].i;
500           } else {
501             s = g_imagecache [ IMG_TAB_LINE ].i;
502           }
503           dest -> x = tab_offset_x + ( (col-ui_catshift) * tab_width );
504           dest -> y = tab_offset_y + tab_height;
505           SDL_BlitSurface ( s, NULL /* whole image */, sdl_realscreen, dest );
506           dest++;
507         }
508
509         // draw text
510         SDL_Surface *rtext;
511         SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
512         rtext = TTF_RenderText_Blended ( g_tab_font, g_categories [ col ].catname, tmpfontcolor );
513         src.x = 0;
514         src.y = 0;
515         src.w = rtext -> w < text_width ? rtext -> w : text_width;
516         src.h = rtext -> h;
517         dest -> x = tab_offset_x + ( (col-ui_catshift) * tab_width ) + text_offset_x;
518         dest -> y = tab_offset_y + text_offset_y;
519         SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
520         SDL_FreeSurface ( rtext );
521         dest++;
522
523       } // r_tabs
524
525     } // for
526
527     if ( render_jobs_b & R_TABS ) {
528
529       // draw tab lines under where tabs would be if we had categories
530       for ( /* foo */; col < maxtabspot; col++ ) {
531         SDL_Surface *s;
532
533         if ( col - ui_catshift == 0 ) {
534           s = g_imagecache [ IMG_TAB_LINEL ].i;
535         } else if ( col - ui_catshift == maxtabspot - 1 ) {
536           s = g_imagecache [ IMG_TAB_LINER ].i;
537         } else {
538           s = g_imagecache [ IMG_TAB_LINE ].i;
539         }
540         dest -> x = tab_offset_x + ( (col-ui_catshift) * tab_width );
541         dest -> y = tab_offset_y + tab_height;
542         SDL_BlitSurface ( s, NULL /* whole image */, sdl_realscreen, dest );
543         dest++;
544
545       } // for
546
547     } // r_tabs
548
549   } // tabs
550
551   // scroll bars and arrows
552   if ( render_jobs_b & R_BG ) {
553     unsigned char show_bar = 0;
554
555     // up?
556     if ( ui_rows_scrolled_down && g_imagecache [ IMG_ARROW_UP ].i ) {
557       dest -> x = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_up_x", 450 );
558       dest -> y = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_up_y", 80 );
559       SDL_BlitSurface ( g_imagecache [ IMG_ARROW_UP ].i, NULL /* whole image */, sdl_realscreen, dest );
560       dest++;
561
562       show_bar = 1;
563     }
564
565     // down?
566     if ( ui_rows_scrolled_down + row_max < icon_rows && g_imagecache [ IMG_ARROW_DOWN ].i ) {
567       dest -> x = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_down_x", 450 );
568       dest -> y = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_down_y", 80 );
569       SDL_BlitSurface ( g_imagecache [ IMG_ARROW_DOWN ].i, NULL /* whole image */, sdl_realscreen, dest );
570       dest++;
571
572       show_bar = 1;
573     }
574
575     if ( show_bar ) {
576       // show scrollbar as well
577       src.x = 0;
578       src.y = 0;
579       src.w = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_clip_w", 10 );
580       src.h = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_clip_h", 100 );
581       dest -> x = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_x", 450 );
582       dest -> y = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_y", 100 );
583       SDL_BlitSurface ( g_imagecache [ IMG_ARROW_SCROLLBAR ].i, &src /* whole image */, sdl_realscreen, dest );
584       dest++;
585     } // bar
586
587   } // r_bg, scroll bars
588
589   // render detail pane bg
590   if ( render_jobs_b & R_DETAIL ) {
591
592     if ( pnd_conf_get_as_int_d ( g_conf, "detailpane.show", 1 ) ) {
593
594       // render detail bg
595       if ( g_imagecache [ IMG_DETAIL_BG ].i ) {
596         src.x = 0; // pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
597         src.y = 0; // pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
598         src.w = ((SDL_Surface*)(g_imagecache [ IMG_DETAIL_PANEL ].i)) -> w;
599         src.h = ((SDL_Surface*)(g_imagecache [ IMG_DETAIL_PANEL ].i)) -> h;
600         dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
601         dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
602         SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
603         dest++;
604       }
605
606       // render detail pane
607       if ( g_imagecache [ IMG_DETAIL_PANEL ].i ) {
608         dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
609         dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
610         SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_PANEL ].i, NULL /* whole image */, sdl_realscreen, dest );
611         dest++;
612       }
613
614     } // detailpane frame/bg
615
616   } // r_details
617
618   // anything to render?
619   if ( render_jobs_b & R_GRID ) {
620
621     // if just rendering grid, and nothing else, better clear it first
622     if ( ! ( render_jobs_b & R_BG ) ) {
623       if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
624         src.x = grid_offset_x;
625         src.y = grid_offset_y + sel_icon_offset_y;
626         src.w = col_max * cell_width;
627         src.h = row_max * cell_height;
628
629         dest -> x = grid_offset_x;
630         dest -> y = grid_offset_y + sel_icon_offset_y;
631
632         SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, &src, sdl_realscreen, dest );
633         dest++;
634
635       }
636     }
637
638     if ( g_categories [ ui_category ].refs ) {
639
640       appiter = g_categories [ ui_category ].refs;
641       row = 0;
642       displayrow = 0;
643
644       // until we run out of apps, or run out of space
645       while ( appiter != NULL ) {
646
647         for ( col = 0; col < col_max && appiter != NULL; col++ ) {
648
649           // do we even need to render it? or are we suppressing it due to rows scrolled off the top?
650           if ( row >= ui_rows_scrolled_down ) {
651
652             // selected? show hilights
653             if ( appiter == ui_selected ) {
654               SDL_Surface *s = g_imagecache [ IMG_SELECTED_ALPHAMASK ].i;
655               // icon
656               //dest -> x = grid_offset_x + ( col * cell_width ) + icon_offset_x + ( ( icon_max_width - s -> w ) / 2 );
657               dest -> x = grid_offset_x + ( col * cell_width ) + icon_offset_x + sel_icon_offset_x;
658               //dest -> y = grid_offset_y + ( displayrow * cell_height ) + icon_offset_y + ( ( icon_max_height - s -> h ) / 2 );
659               dest -> y = grid_offset_y + ( displayrow * cell_height ) + icon_offset_y + sel_icon_offset_y;
660               SDL_BlitSurface ( s, NULL /* all */, sdl_realscreen, dest );
661               dest++;
662               // text
663               dest -> x = grid_offset_x + ( col * cell_width ) + text_clip_x;
664               dest -> y = grid_offset_y + ( displayrow * cell_height ) + pnd_conf_get_as_int ( g_conf, "grid.text_hilite_offset_y" );
665               SDL_BlitSurface ( g_imagecache [ IMG_SELECTED_HILITE ].i, NULL /* all */, sdl_realscreen, dest );
666               dest++;
667             } // selected?
668
669             // show icon
670             mm_cache_t *ic = cache_query_icon ( appiter -> ref -> unique_id );
671             SDL_Surface *iconsurface;
672             if ( ic ) {
673               iconsurface = ic -> i;
674             } else {
675               //pnd_log ( pndn_warning, "WARNING: TBD: Need Missin-icon icon for '%s'\n", IFNULL(appiter -> ref -> title_en,"No Name") );
676
677               // no icon override; was this a pnd-file (show the unknown icon then), or was this generated from
678               // filesystem (file or directory icon)
679               if ( appiter -> ref -> object_flags & PND_DISCO_GENERATED ) {
680                 if ( appiter -> ref -> object_type == pnd_object_type_directory ) {
681                   iconsurface = g_imagecache [ IMG_FOLDER ].i;
682                 } else {
683                   iconsurface = g_imagecache [ IMG_EXECBIN ].i;
684                 }
685               } else {
686                 iconsurface = g_imagecache [ IMG_ICON_MISSING ].i;
687               }
688
689             }
690
691             // got an icon I hope?
692             if ( iconsurface ) {
693               //pnd_log ( pndn_debug, "Got an icon for '%s'\n", IFNULL(appiter -> ref -> title_en,"No Name") );
694
695               src.x = 0;
696               src.y = 0;
697               src.w = 60;
698               src.h = 60;
699               dest -> x = grid_offset_x + ( col * cell_width ) + icon_offset_x + (( icon_max_width - iconsurface -> w ) / 2);
700               dest -> y = grid_offset_y + ( displayrow * cell_height ) + icon_offset_y + (( icon_max_height - iconsurface -> h ) / 2);
701
702               SDL_BlitSurface ( iconsurface, &src, sdl_realscreen, dest );
703
704               // store touch info
705               ui_register_app ( appiter, dest -> x, dest -> y, src.w, src.h );
706
707               dest++;
708
709             }
710
711             // show text
712             if ( appiter -> ref -> title_en ) {
713               SDL_Surface *rtext;
714               SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
715               rtext = TTF_RenderText_Blended ( g_grid_font, appiter -> ref -> title_en, tmpfontcolor );
716               src.x = 0;
717               src.y = 0;
718               src.w = text_width < rtext -> w ? text_width : rtext -> w;
719               src.h = rtext -> h;
720               if ( rtext -> w > text_width ) {
721                 dest -> x = grid_offset_x + ( col * cell_width ) + text_clip_x;
722               } else {
723                 dest -> x = grid_offset_x + ( col * cell_width ) + text_offset_x - ( rtext -> w / 2 );
724               }
725               dest -> y = grid_offset_y + ( displayrow * cell_height ) + text_offset_y;
726               SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
727               SDL_FreeSurface ( rtext );
728               dest++;
729             }
730
731           } // display now? or scrolled away..
732
733           // next
734           appiter = appiter -> next;
735
736         } // for column 1...X
737
738         if ( row >= ui_rows_scrolled_down ) {
739           displayrow++;
740         }
741
742         row ++;
743
744         // are we done displaying rows?
745         if ( displayrow >= row_max ) {
746           break;
747         }
748
749       } // while
750
751     } else {
752       // no apps to render?
753       pnd_log ( pndn_rem, "No applications to render?\n" );
754     } // apps to renser?
755
756   } // r_grid
757
758   // detail panel
759   if ( ui_selected && render_jobs_b & R_DETAIL ) {
760
761     unsigned int cell_offset_x = pnd_conf_get_as_int ( g_conf, "detailtext.cell_offset_x" );
762     unsigned int cell_offset_y = pnd_conf_get_as_int ( g_conf, "detailtext.cell_offset_y" );
763     unsigned int cell_width = pnd_conf_get_as_int ( g_conf, "detailtext.cell_width" );
764
765     unsigned int desty = cell_offset_y;
766
767     char buffer [ 256 ];
768
769     // full name
770     if ( ui_selected -> ref -> title_en ) {
771       SDL_Surface *rtext;
772       SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
773       rtext = TTF_RenderText_Blended ( g_detailtext_font, ui_selected -> ref -> title_en, tmpfontcolor );
774       src.x = 0;
775       src.y = 0;
776       src.w = rtext -> w < cell_width ? rtext -> w : cell_width;
777       src.h = rtext -> h;
778       dest -> x = cell_offset_x;
779       dest -> y = desty;
780       SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
781       SDL_FreeSurface ( rtext );
782       dest++;
783       desty += src.h;
784     }
785
786     // category
787 #if 0
788     if ( ui_selected -> ref -> main_category ) {
789
790       sprintf ( buffer, "Category: %s", ui_selected -> ref -> main_category );
791
792       SDL_Surface *rtext;
793       SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
794       rtext = TTF_RenderText_Blended ( g_detailtext_font, buffer, tmpfontcolor );
795       src.x = 0;
796       src.y = 0;
797       src.w = rtext -> w < cell_width ? rtext -> w : cell_width;
798       src.h = rtext -> h;
799       dest -> x = cell_offset_x;
800       dest -> y = desty;
801       SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
802       SDL_FreeSurface ( rtext );
803       dest++;
804       desty += src.h;
805     }
806 #endif
807
808     // clock
809     if ( ui_selected -> ref -> clockspeed ) {
810
811       sprintf ( buffer, "CPU Clock: %s", ui_selected -> ref -> clockspeed );
812
813       SDL_Surface *rtext;
814       SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
815       rtext = TTF_RenderText_Blended ( g_detailtext_font, buffer, tmpfontcolor );
816       src.x = 0;
817       src.y = 0;
818       src.w = rtext -> w < cell_width ? rtext -> w : cell_width;
819       src.h = rtext -> h;
820       dest -> x = cell_offset_x;
821       dest -> y = desty;
822       SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
823       SDL_FreeSurface ( rtext );
824       dest++;
825       desty += src.h;
826     }
827
828     // show sub-app# on right side of cpu clock?
829     //if ( ui_selected -> ref -> subapp_number )
830     {
831       sprintf ( buffer, "(app#%u)", ui_selected -> ref -> subapp_number );
832
833       SDL_Surface *rtext;
834       SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
835       rtext = TTF_RenderText_Blended ( g_grid_font, buffer, tmpfontcolor );
836       dest -> x = cell_offset_x + cell_width - rtext -> w;
837       dest -> y = desty - src.h;
838       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
839       SDL_FreeSurface ( rtext );
840       dest++;
841     }
842
843     // info hint
844 #if 0 // merged into hint-line
845     if ( ui_selected -> ref -> info_filename ) {
846
847       sprintf ( buffer, "Documentation - hit Y" );
848
849       SDL_Surface *rtext;
850       SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
851       rtext = TTF_RenderText_Blended ( g_detailtext_font, buffer, tmpfontcolor );
852       src.x = 0;
853       src.y = 0;
854       src.w = rtext -> w < cell_width ? rtext -> w : cell_width;
855       src.h = rtext -> h;
856       dest -> x = cell_offset_x;
857       dest -> y = desty;
858       SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
859       SDL_FreeSurface ( rtext );
860       dest++;
861       desty += src.h;
862     }
863 #endif
864
865     // notes
866     if ( ui_selected -> ovrh ) {
867       char *n;
868       unsigned char i;
869       char buffer [ 50 ];
870
871       desty += 5; // a touch of spacing can't hurt
872
873       for ( i = 1; i < 4; i++ ) {
874         sprintf ( buffer, "Application-%u.note-%u", ui_selected -> ref -> subapp_number, i );
875         n = pnd_conf_get_as_char ( ui_selected -> ovrh, buffer );
876
877         if ( n ) {
878           SDL_Surface *rtext;
879           SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
880           rtext = TTF_RenderText_Blended ( g_detailtext_font, n, tmpfontcolor );
881           src.x = 0;
882           src.y = 0;
883           src.w = rtext -> w < cell_width ? rtext -> w : cell_width;
884           src.h = rtext -> h;
885           dest -> x = cell_offset_x;
886           dest -> y = desty;
887           SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
888           SDL_FreeSurface ( rtext );
889           dest++;
890           desty += rtext -> h;
891         }
892       } // for
893
894     } // r_detail -> notes
895
896     // preview pic
897     mm_cache_t *ic = cache_query_preview ( ui_selected -> ref -> unique_id );
898     SDL_Surface *previewpic;
899
900     if ( ic ) {
901       previewpic = ic -> i;
902     } else {
903       previewpic = g_imagecache [ IMG_PREVIEW_MISSING ].i;
904     }
905
906     if ( previewpic ) {
907       dest -> x = pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_offset_x", 50 ) +
908         ( ( pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 50 ) - previewpic -> w ) / 2 );
909       dest -> y = pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_offset_y", 50 );
910       SDL_BlitSurface ( previewpic, NULL /* whole image */, sdl_realscreen, dest );
911       dest++;
912     }
913
914   } // r_detail && selected?
915
916   // extras
917   //
918
919   // battery
920   if ( render_jobs_b & R_BG ) {
921     static int last_battlevel = 0;
922     static unsigned char batterylevel = 0;
923     char buffer [ 100 ];
924
925     if ( time ( NULL ) - last_battlevel > 60 ) {
926       batterylevel = pnd_device_get_battery_gauge_perc();
927       last_battlevel = time ( NULL );
928     }
929
930     sprintf ( buffer, "Battery: %u%%", batterylevel );
931
932     SDL_Surface *rtext;
933     SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
934     rtext = TTF_RenderText_Blended ( g_grid_font, buffer, tmpfontcolor );
935     dest -> x = pnd_conf_get_as_int_d ( g_conf, "display.battery_x", 20 );
936     dest -> y = pnd_conf_get_as_int_d ( g_conf, "display.battery_y", 450 );
937     SDL_BlitSurface ( rtext, NULL /* all */, sdl_realscreen, dest );
938     SDL_FreeSurface ( rtext );
939     dest++;
940   }
941
942   // hints
943   if ( pnd_conf_get_as_char ( g_conf, "display.hintline" ) ) {
944     char *buffer;
945     unsigned int hintx, hinty;
946     hintx = pnd_conf_get_as_int_d ( g_conf, "display.hint_x", 40 );
947     hinty = pnd_conf_get_as_int_d ( g_conf, "display.hint_y", 450 );
948     static unsigned int lastwidth = 3000;
949
950     if ( ui_selected && ui_selected -> ref -> info_filename ) {
951       buffer = "Documentation - hit Y";
952     } else {
953       buffer = pnd_conf_get_as_char ( g_conf, "display.hintline" );
954     }
955
956     SDL_Surface *rtext;
957     SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
958     rtext = TTF_RenderText_Blended ( g_grid_font, buffer, tmpfontcolor );
959
960     // clear bg
961     if ( ! ( render_jobs_b & R_BG ) ) {
962       src.x = hintx;
963       src.y = hinty;
964       src.w = lastwidth;
965       src.h = rtext -> h;
966       dest -> x = hintx;
967       dest -> y = hinty;
968       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_TABMASK ].i, &src, sdl_realscreen, dest );
969       dest++;
970       lastwidth = rtext -> w;
971     }
972
973     // now render text
974     dest -> x = hintx;
975     dest -> y = hinty;
976     SDL_BlitSurface ( rtext, NULL /* all */, sdl_realscreen, dest );
977     SDL_FreeSurface ( rtext );
978     dest++;
979   }
980
981   // clock time
982   if ( render_jobs_b & R_BG &&
983        pnd_conf_get_as_int_d ( g_conf, "display.clock_x", -1 ) != -1 )
984   {
985     char buffer [ 50 ];
986
987     time_t t = time ( NULL );
988     struct tm *tm = localtime ( &t );
989     strftime ( buffer, 50, "%a %H:%M %F", tm );
990
991     SDL_Surface *rtext;
992     SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
993     rtext = TTF_RenderText_Blended ( g_grid_font, buffer, tmpfontcolor );
994     dest -> x = pnd_conf_get_as_int_d ( g_conf, "display.clock_x", 700 );
995     dest -> y = pnd_conf_get_as_int_d ( g_conf, "display.clock_y", 450 );
996     SDL_BlitSurface ( rtext, NULL /* all */, sdl_realscreen, dest );
997     SDL_FreeSurface ( rtext );
998     dest++;
999   }
1000
1001   // update all the rects and send it all to sdl
1002   // - at this point, we could probably just do 1 rect, of the
1003   //   whole screen, and be faster :/
1004   SDL_UpdateRects ( sdl_realscreen, dest - rects, rects );
1005
1006 } // ui_render
1007
1008 void ui_process_input ( unsigned char block_p ) {
1009   SDL_Event event;
1010
1011   unsigned char ui_event = 0; // if we get a ui event, flip to 1 and break
1012   //static ui_sdl_button_e ui_mask = uisb_none; // current buttons down
1013
1014   while ( ! ui_event &&
1015           block_p ? SDL_WaitEvent ( &event ) : SDL_PollEvent ( &event ) )
1016   {
1017
1018     switch ( event.type ) {
1019
1020     case SDL_USEREVENT:
1021       // update something
1022
1023       if ( event.user.code == sdl_user_ticker ) {
1024
1025         // timer went off, time to load something
1026         if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_previews_later", 0 ) ) {
1027
1028           // load the preview pics now!
1029           pnd_disco_t *iter = ui_selected -> ref;
1030
1031           if ( iter -> preview_pic1 ) {
1032
1033             if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.threaded_preview", 0 ) ) {
1034               // load in bg thread, make user experience chuggy
1035
1036               g_preview_thread = SDL_CreateThread ( (void*)ui_threaded_defered_preview, iter );
1037
1038               if ( ! g_preview_thread ) {
1039                 pnd_log ( pndn_error, "ERROR: Couldn't create preview thread\n" );
1040               }
1041
1042             } else {
1043               // load it now, make user wait
1044
1045               if ( ! cache_preview ( iter, pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 200 ),
1046                                      pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 180 ) )
1047                  )
1048               {
1049                 pnd_log ( pndn_debug, "  Couldn't load preview pic: '%s' -> '%s'\n",
1050                           IFNULL(iter->title_en,"No Name"), iter -> preview_pic1 );
1051               }
1052
1053             } // threaded?
1054
1055           } // got a preview at all?
1056
1057           ui_event++;
1058         }
1059
1060       } else if ( event.user.code == sdl_user_finishedpreview ) {
1061
1062         // if we just finished the one we happen to be looking at, better redraw now; otherwise, if
1063         // we finished another, no big woop
1064         if ( ui_selected && event.user.data1 == ui_selected -> ref ) {
1065           ui_event++;
1066         }
1067
1068       } else if ( event.user.code == sdl_user_finishedicon ) {
1069         // redraw, so we can show the newly loaded icon
1070         ui_event++;
1071
1072       }
1073
1074       render_mask |= CHANGED_EVERYTHING;
1075
1076       break;
1077
1078 #if 0 // joystick motion
1079     case SDL_JOYAXISMOTION:
1080
1081       pnd_log ( PND_LOG_DEFAULT, "joystick axis\n" );
1082
1083         if ( event.jaxis.axis == 0 ) {
1084           // horiz
1085           if ( event.jaxis.value < 0 ) {
1086             ui_push_left ( 0 );
1087             pnd_log ( PND_LOG_DEFAULT, "joystick axis - LEFT\n" );
1088           } else if ( event.jaxis.value > 0 ) {
1089             ui_push_right ( 0 );
1090             pnd_log ( PND_LOG_DEFAULT, "joystick axis - RIGHT\n" );
1091           }
1092         } else if ( event.jaxis.axis == 1 ) {
1093           // vert
1094           if ( event.jaxis.value < 0 ) {
1095             ui_push_up();
1096           } else if ( event.jaxis.value > 0 ) {
1097             ui_push_down();
1098           }
1099         }
1100
1101         ui_event++;
1102
1103         break;
1104 #endif
1105
1106 #if 0 // joystick buttons
1107     case SDL_JOYBUTTONDOWN:
1108
1109       pnd_log ( PND_LOG_DEFAULT, "joystick button down %u\n", event.jbutton.button );
1110
1111       if ( event.jbutton.button == 0 ) { // B
1112         ui_mask |= uisb_b;
1113       } else if ( event.jbutton.button == 1 ) { // Y
1114         ui_mask |= uisb_y;
1115       } else if ( event.jbutton.button == 2 ) { // X
1116         ui_mask |= uisb_x;
1117       } else if ( event.jbutton.button == 3 ) { // A
1118         ui_mask |= uisb_a;
1119
1120       } else if ( event.jbutton.button == 4 ) { // Select
1121         ui_mask |= uisb_select;
1122       } else if ( event.jbutton.button == 5 ) { // Start
1123         ui_mask |= uisb_start;
1124
1125       } else if ( event.jbutton.button == 7 ) { // L
1126         ui_mask |= uisb_l;
1127       } else if ( event.jbutton.button == 8 ) { // R
1128         ui_mask |= uisb_r;
1129
1130       }
1131
1132       ui_event++;
1133
1134       break;
1135
1136     case SDL_JOYBUTTONUP:
1137
1138       pnd_log ( PND_LOG_DEFAULT, "joystick button up %u\n", event.jbutton.button );
1139
1140       if ( event.jbutton.button == 0 ) { // B
1141         ui_mask &= ~uisb_b;
1142         ui_push_exec();
1143       } else if ( event.jbutton.button == 1 ) { // Y
1144         ui_mask &= ~uisb_y;
1145       } else if ( event.jbutton.button == 2 ) { // X
1146         ui_mask &= ~uisb_x;
1147       } else if ( event.jbutton.button == 3 ) { // A
1148         ui_mask &= ~uisb_a;
1149
1150       } else if ( event.jbutton.button == 4 ) { // Select
1151         ui_mask &= ~uisb_select;
1152       } else if ( event.jbutton.button == 5 ) { // Start
1153         ui_mask &= ~uisb_start;
1154
1155       } else if ( event.jbutton.button == 7 ) { // L
1156         ui_mask &= ~uisb_l;
1157         ui_push_ltrigger();
1158       } else if ( event.jbutton.button == 8 ) { // R
1159         ui_mask &= ~uisb_r;
1160         ui_push_rtrigger();
1161
1162       }
1163
1164       ui_event++;
1165
1166       break;
1167 #endif
1168
1169 #if 1 // keyboard events
1170     case SDL_KEYUP:
1171
1172       //pnd_log ( pndn_debug, "key up %u\n", event.key.keysym.sym );
1173
1174       // SDLK_LALT -> Start
1175
1176       // directional
1177       if ( event.key.keysym.sym == SDLK_RIGHT ) {
1178         ui_push_right ( 0 );
1179         ui_event++;
1180       } else if ( event.key.keysym.sym == SDLK_LEFT ) {
1181         ui_push_left ( 0 );
1182         ui_event++;
1183       } else if ( event.key.keysym.sym == SDLK_UP ) {
1184         ui_push_up();
1185         ui_event++;
1186       } else if ( event.key.keysym.sym == SDLK_DOWN ) {
1187         ui_push_down();
1188         ui_event++;
1189       } else if ( event.key.keysym.sym == SDLK_SPACE || event.key.keysym.sym == SDLK_END ) {
1190         ui_push_exec();
1191         ui_event++;
1192       } else if ( event.key.keysym.sym == SDLK_z || event.key.keysym.sym == SDLK_RSHIFT ) {
1193         ui_push_ltrigger();
1194         ui_event++;
1195       } else if ( event.key.keysym.sym == SDLK_x || event.key.keysym.sym == SDLK_RCTRL ) {
1196         ui_push_rtrigger();
1197         ui_event++;
1198       } else if ( event.key.keysym.sym == SDLK_y || event.key.keysym.sym == SDLK_PAGEUP ) {
1199         // info
1200         if ( ui_selected ) {
1201           ui_show_info ( pnd_run_script, ui_selected -> ref );
1202           ui_event++;
1203         }
1204
1205       } else if ( event.key.keysym.sym == SDLK_LALT ) { // start button
1206         ui_push_exec();
1207         ui_event++;
1208
1209       } else if ( event.key.keysym.sym == SDLK_LCTRL /*LALT*/ ) { // select button
1210         char *opts [ 20 ] = {
1211           "Return to Minimenu",
1212           "Shutdown Pandora",
1213           "Rescan for Applications",
1214           "Cache previews to SD now",
1215           "Run xfce4 from Minimenu",
1216           "Run a terminal/console",
1217           "Exit and run xfce4",
1218           "Exit and run pmenu",
1219           "Quit (<- beware)",
1220           "Select a Minimenu skin",
1221           "About Minimenu"
1222         };
1223         int sel = ui_modal_single_menu ( opts, 11, "Minimenu", "Enter to select; other to return." );
1224
1225         char buffer [ 100 ];
1226         if ( sel == 0 ) {
1227           // do nothing
1228         } else if ( sel == 1 ) {
1229           // shutdown
1230           sprintf ( buffer, "sudo poweroff" );
1231           system ( buffer );
1232         } else if ( sel == 2 ) {
1233           // rescan apps
1234           pnd_log ( pndn_debug, "Freeing up applications\n" );
1235           applications_free();
1236           pnd_log ( pndn_debug, "Rescanning applications\n" );
1237           applications_scan();
1238           // reset view
1239           ui_selected = NULL;
1240           ui_rows_scrolled_down = 0;
1241           // set back to first tab, to be safe
1242           ui_category = 0;
1243           ui_catshift = 0;
1244         } else if ( sel == 3 ) {
1245           // cache preview to SD now
1246           extern pnd_box_handle g_active_apps;
1247           pnd_box_handle h = g_active_apps;
1248
1249           unsigned int maxwidth, maxheight;
1250           maxwidth = pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 200 );
1251           maxheight = pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 180 );
1252
1253           pnd_disco_t *iter = pnd_box_get_head ( h );
1254
1255           while ( iter ) {
1256
1257             // cache it
1258             if ( ! cache_preview ( iter, maxwidth, maxheight ) ) {
1259               pnd_log ( pndn_debug, "Force cache: Couldn't load preview pic: '%s' -> '%s'\n",
1260                         IFNULL(iter->title_en,"No Name"), iter -> preview_pic1 );
1261             }
1262
1263             // next
1264             iter = pnd_box_get_next ( iter );
1265           } // while
1266
1267         } else if ( sel == 4 ) {
1268           // run xfce
1269           char buffer [ PATH_MAX ];
1270           sprintf ( buffer, "%s %s\n", MM_RUN, "/usr/bin/startxfce4" );
1271           emit_and_quit ( buffer );
1272         } else if ( sel == 5 ) {
1273           // run terminal
1274           char *argv[5];
1275           argv [ 0 ] = pnd_conf_get_as_char ( g_conf, "utility.terminal" );
1276           argv [ 1 ] = NULL;
1277
1278           if ( argv [ 0 ] ) {
1279             ui_forkexec ( argv );
1280           }
1281
1282         } else if ( sel == 6 ) {
1283           // set env to xfce
1284           sprintf ( buffer, "echo startxfce4 > /tmp/gui.load" );
1285           system ( buffer );
1286           emit_and_quit ( buffer );
1287           //sprintf ( buffer, "sudo poweroff" );
1288           //system ( buffer );
1289           exit ( 0 );
1290         } else if ( sel == 7 ) {
1291           // set env to pmenu
1292           sprintf ( buffer, "echo pmenu > /tmp/gui.load" );
1293           system ( buffer );
1294           emit_and_quit ( buffer );
1295           //sprintf ( buffer, "sudo poweroff" );
1296           //system ( buffer );
1297           exit ( 0 );
1298         } else if ( sel == 8 ) {
1299           emit_and_quit ( MM_QUIT );
1300         } else if ( sel == 9 ) {
1301           // select skin
1302           if ( ui_pick_skin() ) {
1303             emit_and_quit ( MM_RESTART );
1304           }
1305         } else if ( sel == 10 ) {
1306           // about
1307         }
1308
1309         ui_event++;
1310         render_mask |= CHANGED_EVERYTHING;
1311       }
1312
1313       // extras
1314       if ( event.key.keysym.sym == SDLK_q ) {
1315         emit_and_quit ( MM_QUIT );
1316       }
1317
1318       break;
1319 #endif
1320
1321 #if 1 // mouse / touchscreen
1322 #if 0
1323     case SDL_MOUSEBUTTONDOWN:
1324       if ( event.button.button == SDL_BUTTON_LEFT ) {
1325         cb_pointer_press ( gc, event.button.x / g_scale, event.button.y / g_scale );
1326         ui_event++;
1327       }
1328       break;
1329 #endif
1330
1331     case SDL_MOUSEBUTTONUP:
1332       if ( event.button.button == SDL_BUTTON_LEFT ) {
1333         ui_touch_act ( event.button.x, event.button.y );
1334         ui_event++;
1335       }
1336       break;
1337 #endif
1338
1339     case SDL_QUIT:
1340       exit ( 0 );
1341       break;
1342
1343     default:
1344       break;
1345
1346     } // switch event type
1347
1348   } // while poll
1349
1350   return;
1351 }
1352
1353 void ui_push_left ( unsigned char forcecoil ) {
1354
1355   if ( ! ui_selected ) {
1356     ui_push_right ( 0 );
1357     return;
1358   }
1359
1360   // what column we in?
1361   unsigned int col = ui_determine_screen_col ( ui_selected );
1362
1363   // are we already at first item?
1364   if ( forcecoil == 0 &&
1365        pnd_conf_get_as_int_d ( g_conf, "grid.wrap_horiz_samerow", 0 ) &&
1366        col == 0 )
1367   {
1368     unsigned int i = pnd_conf_get_as_int_d ( g_conf, "grid.col_max", 5 ) - 1;
1369     while ( i && ui_selected -> next ) {
1370       ui_push_right ( 0 );
1371       i--;
1372     }
1373
1374   } else if ( g_categories [ ui_category ].refs == ui_selected ) {
1375     // can't go any more left, we're at the head
1376
1377   } else {
1378     // figure out the previous item; yay for singly linked list :/
1379     mm_appref_t *i = g_categories [ ui_category ].refs;
1380     while ( i ) {
1381       if ( i -> next == ui_selected ) {
1382         ui_selected = i;
1383         break;
1384       }
1385       i = i -> next;
1386     }
1387   }
1388
1389   ui_set_selected ( ui_selected );
1390
1391   return;
1392 }
1393
1394 void ui_push_right ( unsigned char forcecoil ) {
1395
1396   if ( ui_selected ) {
1397
1398     // what column we in?
1399     unsigned int col = ui_determine_screen_col ( ui_selected );
1400
1401     // wrap same or no?
1402     if ( forcecoil == 0 &&
1403          pnd_conf_get_as_int_d ( g_conf, "grid.wrap_horiz_samerow", 0 ) &&
1404          // and selected is far-right, or last icon in category (might not be far right)
1405          ( ( col == pnd_conf_get_as_int_d ( g_conf, "grid.col_max", 5 ) - 1 ) ||
1406            ( ui_selected -> next == NULL ) )
1407        )
1408     {
1409       // same wrap
1410       //unsigned int i = pnd_conf_get_as_int_d ( g_conf, "grid.col_max", 5 ) - 1;
1411       while ( col /*i*/ ) {
1412         ui_push_left ( 0 );
1413         col--; //i--;
1414       }
1415
1416     } else {
1417       // just go to the next
1418
1419       if ( ui_selected -> next ) {
1420         ui_selected = ui_selected -> next;
1421       }
1422
1423     }
1424
1425   } else {
1426     ui_selected = g_categories [ ui_category ].refs;
1427   }
1428
1429   ui_set_selected ( ui_selected );
1430
1431   return;
1432 }
1433
1434 void ui_push_up ( void ) {
1435   unsigned char col_max = pnd_conf_get_as_int ( g_conf, "grid.col_max" );
1436
1437   if ( ! ui_selected ) {
1438     return;
1439   }
1440
1441   // what row we in?
1442   unsigned int row = ui_determine_row ( ui_selected );
1443
1444   if ( row == 0 &&
1445        pnd_conf_get_as_int_d ( g_conf, "grid.wrap_vert_stop", 1 ) == 0 )
1446   {
1447     // wrap around instead
1448
1449     unsigned int col = ui_determine_screen_col ( ui_selected );
1450
1451     // go to end
1452     ui_selected = g_categories [ ui_category ].refs;
1453     while ( ui_selected -> next ) {
1454       ui_selected = ui_selected -> next;
1455     }
1456
1457     // try to move to same column
1458     unsigned int newcol = ui_determine_screen_col ( ui_selected );
1459     if ( newcol > col ) {
1460       col = newcol - col;
1461       while ( col ) {
1462         ui_push_left ( 0 );
1463         col--;
1464       }
1465     }
1466
1467     // scroll down to show it
1468     int r = ui_determine_row ( ui_selected ) - 1;
1469     if ( r - pnd_conf_get_as_int ( g_conf, "grid.row_max" ) > 0 ) {
1470       ui_rows_scrolled_down = (unsigned int) r;
1471     }
1472
1473   } else {
1474     // stop at top/bottom
1475
1476     while ( col_max ) {
1477       ui_push_left ( 1 );
1478       col_max--;
1479     }
1480
1481   }
1482
1483   return;
1484 }
1485
1486 void ui_push_down ( void ) {
1487   unsigned char col_max = pnd_conf_get_as_int ( g_conf, "grid.col_max" );
1488
1489   if ( ui_selected ) {
1490
1491     // what row we in?
1492     unsigned int row = ui_determine_row ( ui_selected );
1493
1494     // max rows?
1495     unsigned int icon_rows = g_categories [ ui_category ].refcount / col_max;
1496     if ( g_categories [ ui_category ].refcount % col_max > 0 ) {
1497       icon_rows++;
1498     }
1499
1500     // we at the end?
1501     if ( row == ( icon_rows - 1 ) &&
1502          pnd_conf_get_as_int_d ( g_conf, "grid.wrap_vert_stop", 1 ) == 0 )
1503     {
1504
1505       unsigned char col = ui_determine_screen_col ( ui_selected );
1506
1507       ui_selected = g_categories [ ui_category ].refs;
1508
1509       while ( col ) {
1510         ui_selected = ui_selected -> next;
1511         col--;
1512       }
1513
1514       ui_rows_scrolled_down = 0;
1515
1516       render_mask |= CHANGED_EVERYTHING;
1517
1518     } else {
1519
1520       while ( col_max ) {
1521         ui_push_right ( 1 );
1522         col_max--;
1523       }
1524
1525     }
1526
1527   } else {
1528     ui_push_right ( 0 );
1529   }
1530
1531   return;
1532 }
1533
1534 void ui_push_exec ( void ) {
1535
1536   if ( ! ui_selected ) {
1537     return;
1538   }
1539
1540   // was this icon generated from filesystem, or from pnd-file?
1541   if ( ui_selected -> ref -> object_flags & PND_DISCO_GENERATED ) {
1542
1543     if ( ! ui_selected -> ref -> title_en ) {
1544       return; // no filename
1545     }
1546
1547     if ( ui_selected -> ref -> object_type == pnd_object_type_directory ) {
1548       // delve up/down the dir tree
1549
1550       if ( strcmp ( ui_selected -> ref -> title_en, ".." ) == 0 ) {
1551         // go up
1552         char *c;
1553
1554         // lop off last word; if the thing ends with /, lop that one, then the next word.
1555         while ( ( c = strrchr ( g_categories [ ui_category].fspath, '/' ) ) ) {
1556           *c = '\0'; // lop off the last hunk
1557           if ( *(c+1) != '\0' ) {
1558             break;
1559           }
1560         } // while
1561
1562         // nothing left?
1563         if ( g_categories [ ui_category].fspath [ 0 ] == '\0' ) {
1564           strcpy ( g_categories [ ui_category].fspath, "/" );
1565         }
1566
1567       } else {
1568         // go down
1569         strcat ( g_categories [ ui_category].fspath, "/" );
1570         strcat ( g_categories [ ui_category].fspath, ui_selected -> ref -> title_en );
1571       }
1572
1573       pnd_log ( pndn_debug, "Cat %s is now in path %s\n", g_categories [ ui_category ].catname, g_categories [ ui_category ].fspath );
1574
1575       // rescan the dir
1576       category_fs_restock ( &(g_categories [ ui_category]) );
1577       // forget the selection, nolonger applies
1578       ui_selected = NULL;
1579       ui_set_selected ( ui_selected );
1580       // redraw the grid
1581       render_mask |= CHANGED_SELECTION;
1582
1583     } else {
1584       // just run it arbitrarily?
1585
1586       // if this a pnd-file, or just some executable?
1587       if ( strcasestr ( ui_selected -> ref -> object_filename, PND_PACKAGE_FILEEXT ) ) {
1588         // looks like a pnd, now what do we do..
1589         pnd_box_handle h = pnd_disco_file ( ui_selected -> ref -> object_path, ui_selected -> ref -> object_filename );
1590
1591         if ( h ) {
1592           pnd_disco_t *d = pnd_box_get_head ( h );
1593           pnd_apps_exec_disco ( pnd_run_script, d, PND_EXEC_OPTION_NORUN, NULL );
1594           char buffer [ PATH_MAX ];
1595           sprintf ( buffer, "%s %s\n", MM_RUN, pnd_apps_exec_runline() );
1596           emit_and_quit ( buffer );
1597         }
1598
1599       } else {
1600         // random bin file
1601 #if 1
1602         char cwd [ PATH_MAX ];
1603         getcwd ( cwd, PATH_MAX );
1604
1605         chdir ( g_categories [ ui_category ].fspath );
1606         pnd_exec_no_wait_1 ( ui_selected -> ref -> title_en, NULL );
1607         chdir ( cwd );
1608 #else
1609         char buffer [ PATH_MAX ];
1610         sprintf ( buffer, "%s %s/%s\n", MM_RUN, g_categories [ ui_category ].fspath, ui_selected -> ref -> title_en );
1611         emit_and_quit ( buffer );
1612 #endif
1613       } // pnd or bin?
1614
1615     } // dir or file?
1616
1617   } else {
1618     pnd_apps_exec_disco ( pnd_run_script, ui_selected -> ref, PND_EXEC_OPTION_NORUN, NULL );
1619     char buffer [ PATH_MAX ];
1620     sprintf ( buffer, "%s %s\n", MM_RUN, pnd_apps_exec_runline() );
1621     emit_and_quit ( buffer );
1622   }
1623
1624   return;
1625 }
1626
1627 void ui_push_ltrigger ( void ) {
1628   unsigned char oldcat = ui_category;
1629   unsigned int screen_width = pnd_conf_get_as_int_d ( g_conf, "display.screen_width", 800 );
1630   unsigned int tab_width = pnd_conf_get_as_int ( g_conf, "tabs.tab_width" );
1631
1632   if ( ui_category > 0 ) {
1633     ui_category--;
1634     category_fs_restock ( &(g_categories [ ui_category ]) );
1635   } else {
1636     if ( pnd_conf_get_as_int_d ( g_conf, "tabs.wraparound", 0 ) > 0 ) {
1637       ui_category = g_categorycount - 1;
1638       ui_catshift = 0;
1639       if ( ui_category >= ( screen_width / tab_width ) ) {
1640         ui_catshift = ui_category - ( screen_width / tab_width ) + 1;
1641       }
1642       category_fs_restock ( &(g_categories [ ui_category ]) );
1643     }
1644   }
1645
1646   if ( oldcat != ui_category ) {
1647     ui_selected = NULL;
1648     ui_set_selected ( ui_selected );
1649   }
1650
1651   // make tab visible?
1652   if ( ui_catshift > 0 && ui_category == ui_catshift - 1 ) {
1653     ui_catshift--;
1654   }
1655
1656   // unscroll
1657   ui_rows_scrolled_down = 0;
1658
1659   render_mask |= CHANGED_CATEGORY;
1660
1661   return;
1662 }
1663
1664 void ui_push_rtrigger ( void ) {
1665   unsigned char oldcat = ui_category;
1666
1667   unsigned int screen_width = pnd_conf_get_as_int_d ( g_conf, "display.screen_width", 800 );
1668   unsigned int tab_width = pnd_conf_get_as_int ( g_conf, "tabs.tab_width" );
1669
1670   if ( ui_category < ( g_categorycount - 1 ) ) {
1671     ui_category++;
1672     category_fs_restock ( &(g_categories [ ui_category ]) );
1673   } else {
1674     if ( pnd_conf_get_as_int_d ( g_conf, "tabs.wraparound", 0 ) > 0 ) {
1675       ui_category = 0;
1676       ui_catshift = 0;
1677       category_fs_restock ( &(g_categories [ ui_category ]) );
1678     }
1679   }
1680
1681   if ( oldcat != ui_category ) {
1682     ui_selected = NULL;
1683     ui_set_selected ( ui_selected );
1684   }
1685
1686   // make tab visible?
1687   if ( ui_category > ui_catshift + ( screen_width / tab_width ) - 1 ) {
1688     ui_catshift++;
1689   }
1690
1691   // unscroll
1692   ui_rows_scrolled_down = 0;
1693
1694   render_mask |= CHANGED_CATEGORY;
1695
1696   return;
1697 }
1698
1699 SDL_Surface *ui_scale_image ( SDL_Surface *s, unsigned int maxwidth, int maxheight ) {
1700   double scale = 1000000.0;
1701   double scalex = 1000000.0;
1702   double scaley = 1000000.0;
1703   SDL_Surface *scaled;
1704
1705   scalex = (double)maxwidth / (double)s -> w;
1706
1707   if ( maxheight == -1 ) {
1708     scale = scalex;
1709   } else {
1710     scaley = (double)maxheight / (double)s -> h;
1711
1712     if ( scaley < scalex ) {
1713       scale = scaley;
1714     } else {
1715       scale = scalex;
1716     }
1717
1718   }
1719
1720   scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
1721   SDL_FreeSurface ( s );
1722   s = scaled;
1723
1724   return ( s );
1725 }
1726
1727 void ui_loadscreen ( void ) {
1728
1729   SDL_Rect dest;
1730
1731   unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
1732   unsigned int font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
1733   unsigned int font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
1734   unsigned int font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
1735
1736   // clear the screen
1737   SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
1738
1739   // render text
1740   SDL_Surface *rtext;
1741   SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
1742   rtext = TTF_RenderText_Blended ( g_big_font, "Setting up menu...", tmpfontcolor );
1743   dest.x = 20;
1744   dest.y = 20;
1745   SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, &dest );
1746   SDL_UpdateRects ( sdl_realscreen, 1, &dest );
1747   SDL_FreeSurface ( rtext );
1748
1749   return;
1750 }
1751
1752 void ui_discoverscreen ( unsigned char clearscreen ) {
1753
1754   SDL_Rect dest;
1755
1756   unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
1757   unsigned int font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
1758   unsigned int font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
1759   unsigned int font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
1760
1761   // clear the screen
1762   if ( clearscreen ) {
1763     SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
1764
1765     // render background
1766     if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
1767       dest.x = 0;
1768       dest.y = 0;
1769       dest.w = sdl_realscreen -> w;
1770       dest.h = sdl_realscreen -> h;
1771       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, NULL /* whole image */, sdl_realscreen, NULL /* 0,0 */ );
1772       SDL_UpdateRects ( sdl_realscreen, 1, &dest );
1773     }
1774
1775   }
1776
1777   // render text
1778   SDL_Surface *rtext;
1779   SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
1780   rtext = TTF_RenderText_Blended ( g_big_font, "Looking for applications...", tmpfontcolor );
1781   if ( clearscreen ) {
1782     dest.x = 20;
1783     dest.y = 20;
1784   } else {
1785     dest.x = 20;
1786     dest.y = 40;
1787   }
1788   SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, &dest );
1789   SDL_UpdateRects ( sdl_realscreen, 1, &dest );
1790   SDL_FreeSurface ( rtext );
1791
1792   // render icon
1793   if ( g_imagecache [ IMG_ICON_MISSING ].i ) {
1794     dest.x = rtext -> w + 30;
1795     dest.y = 20;
1796     SDL_BlitSurface ( g_imagecache [ IMG_ICON_MISSING ].i, NULL, sdl_realscreen, &dest );
1797     SDL_UpdateRects ( sdl_realscreen, 1, &dest );
1798   }
1799
1800   return;
1801 }
1802
1803 void ui_cachescreen ( unsigned char clearscreen, char *filename ) {
1804
1805   SDL_Rect rects [ 4 ];
1806   SDL_Rect *dest = rects;
1807   SDL_Rect src;
1808   bzero ( dest, sizeof(SDL_Rect)* 4 );
1809
1810   unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
1811   unsigned int font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
1812   unsigned int font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
1813   unsigned int font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
1814
1815   static unsigned int stepx = 0;
1816
1817   // clear the screen
1818   if ( clearscreen ) {
1819     SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
1820
1821     // render background
1822     if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
1823       dest -> x = 0;
1824       dest -> y = 0;
1825       dest -> w = sdl_realscreen -> w;
1826       dest -> h = sdl_realscreen -> h;
1827       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, NULL /* whole image */, sdl_realscreen, NULL /* 0,0 */ );
1828       dest++;
1829     }
1830
1831   } else {
1832
1833     // render background
1834     if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
1835       src.x = 0;
1836       src.y = 0;
1837       src.w = sdl_realscreen -> w;
1838       src.h = 100;
1839       dest -> x = 0;
1840       dest -> y = 0;
1841       dest -> w = sdl_realscreen -> w;
1842       dest -> h = sdl_realscreen -> h;
1843       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, &src, sdl_realscreen, dest );
1844       dest++;
1845     }
1846
1847   } // clear it
1848
1849   // render text
1850   SDL_Surface *rtext;
1851   SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
1852   rtext = TTF_RenderText_Blended ( g_big_font, "Caching applications artwork...", tmpfontcolor );
1853   dest -> x = 20;
1854   dest -> y = 20;
1855   SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
1856   SDL_FreeSurface ( rtext );
1857   dest++;
1858
1859   // render icon
1860   if ( g_imagecache [ IMG_ICON_MISSING ].i ) {
1861     dest -> x = rtext -> w + 30 + stepx;
1862     dest -> y = 20;
1863     SDL_BlitSurface ( g_imagecache [ IMG_ICON_MISSING ].i, NULL, sdl_realscreen, dest );
1864     dest++;
1865   }
1866
1867   // filename
1868   if ( filename ) {
1869     rtext = TTF_RenderText_Blended ( g_tab_font, filename, tmpfontcolor );
1870     dest -> x = 20;
1871     dest -> y = 50;
1872     SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
1873     SDL_FreeSurface ( rtext );
1874     dest++;
1875   }
1876
1877   // move across
1878   stepx += 20;
1879
1880   if ( stepx > 350 ) {
1881     stepx = 0;
1882   }
1883
1884   SDL_UpdateRects ( sdl_realscreen, dest - rects, rects );
1885
1886   return;
1887 }
1888
1889 int ui_selected_index ( void ) {
1890
1891   if ( ! ui_selected ) {
1892     return ( -1 ); // no index
1893   }
1894
1895   mm_appref_t *r = g_categories [ ui_category ].refs;
1896   int counter = 0;
1897   while ( r ) {
1898     if ( r == ui_selected ) {
1899       return ( counter );
1900     }
1901     r = r -> next;
1902     counter++;
1903   }
1904
1905   return ( -1 );
1906 }
1907
1908 static mm_appref_t *timer_ref = NULL;
1909 void ui_set_selected ( mm_appref_t *r ) {
1910
1911   render_mask |= CHANGED_SELECTION;
1912
1913   if ( ! pnd_conf_get_as_int_d ( g_conf, "minimenu.load_previews_later", 0 ) ) {
1914     return; // no desire to defer anything
1915   }
1916
1917   if ( ! r ) {
1918     // cancel timer
1919     SDL_SetTimer ( 0, NULL );
1920     timer_ref = NULL;
1921     return;
1922   }
1923
1924   SDL_SetTimer ( pnd_conf_get_as_int_d ( g_conf, "previewpic.defer_timer_ms", 1000 ), ui_callback_f );
1925   timer_ref = r;
1926
1927   return;
1928 }
1929
1930 unsigned int ui_callback_f ( unsigned int t ) {
1931
1932   if ( ui_selected != timer_ref ) {
1933     return ( 0 ); // user has moved it, who cares
1934   }
1935
1936   SDL_Event e;
1937   bzero ( &e, sizeof(SDL_Event) );
1938   e.type = SDL_USEREVENT;
1939   e.user.code = sdl_user_ticker;
1940   SDL_PushEvent ( &e );
1941
1942   return ( 0 );
1943 }
1944
1945 int ui_modal_single_menu ( char *argv[], unsigned int argc, char *title, char *footer ) {
1946   SDL_Rect rects [ 40 ];
1947   SDL_Rect *dest = rects;
1948   SDL_Rect src;
1949   SDL_Surface *rtext;
1950
1951   bzero ( rects, sizeof(SDL_Rect) * 40 );
1952
1953   unsigned int sel = 0;
1954
1955   unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
1956   unsigned int font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
1957   unsigned int font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
1958   unsigned int font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
1959
1960   SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
1961
1962   SDL_Color selfontcolor = { 0/*font_rgba_r*/, font_rgba_g, font_rgba_b, font_rgba_a };
1963
1964   unsigned int i;
1965   SDL_Event event;
1966
1967   while ( 1 ) {
1968
1969     // clear
1970     dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
1971     dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
1972     dest -> w = ((SDL_Surface*) g_imagecache [ IMG_DETAIL_PANEL ].i) -> w;
1973     dest -> h = ((SDL_Surface*) g_imagecache [ IMG_DETAIL_PANEL ].i) -> h;
1974     SDL_FillRect( sdl_realscreen, dest, 0 );
1975
1976     // show dialog background
1977     if ( g_imagecache [ IMG_DETAIL_BG ].i ) {
1978       src.x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
1979       src.y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
1980       src.w = ((SDL_Surface*)(g_imagecache [ IMG_DETAIL_PANEL ].i)) -> w;
1981       src.h = ((SDL_Surface*)(g_imagecache [ IMG_DETAIL_PANEL ].i)) -> h;
1982       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
1983       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
1984       SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
1985       // repeat for darken?
1986       SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
1987       SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
1988       //SDL_UpdateRects ( sdl_realscreen, 1, &dest );
1989       dest++;
1990     }
1991
1992     // show dialog frame
1993     if ( g_imagecache [ IMG_DETAIL_PANEL ].i ) {
1994       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
1995       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
1996       SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_PANEL ].i, NULL /* whole image */, sdl_realscreen, dest );
1997       //SDL_UpdateRects ( sdl_realscreen, 1, &dest );
1998       dest++;
1999     }
2000
2001     // show header
2002     if ( title ) {
2003       rtext = TTF_RenderText_Blended ( g_tab_font, title, tmpfontcolor );
2004       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 ) + 20;
2005       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 ) + 20;
2006       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
2007       SDL_FreeSurface ( rtext );
2008       dest++;
2009     }
2010
2011     // show footer
2012     if ( footer ) {
2013       rtext = TTF_RenderText_Blended ( g_tab_font, footer, tmpfontcolor );
2014       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 ) + 20;
2015       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 ) +
2016         ((SDL_Surface*) g_imagecache [ IMG_DETAIL_PANEL ].i) -> h
2017         - 60;
2018       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
2019       SDL_FreeSurface ( rtext );
2020       dest++;
2021     }
2022
2023     // show options
2024     for ( i = 0; i < argc; i++ ) {
2025
2026       // show options
2027       if ( sel == i ) {
2028         rtext = TTF_RenderText_Blended ( g_tab_font, argv [ i ], selfontcolor );
2029       } else {
2030         rtext = TTF_RenderText_Blended ( g_tab_font, argv [ i ], tmpfontcolor );
2031       }
2032       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 ) + 20;
2033       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 ) + 40 + ( 20 * ( i + 1 ) );
2034       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
2035       SDL_FreeSurface ( rtext );
2036       dest++;
2037
2038     } // for
2039
2040     // update all the rects and send it all to sdl
2041     SDL_UpdateRects ( sdl_realscreen, dest - rects, rects );
2042     dest = rects;
2043
2044     // check for input
2045     while ( SDL_WaitEvent ( &event ) ) {
2046
2047       switch ( event.type ) {
2048
2049       case SDL_KEYUP:
2050
2051         if ( event.key.keysym.sym == SDLK_UP ) {
2052           if ( sel ) {
2053             sel--;
2054           }
2055         } else if ( event.key.keysym.sym == SDLK_DOWN ) {
2056           if ( sel < argc - 1 ) {
2057             sel++;
2058           }
2059
2060         } else if ( event.key.keysym.sym == SDLK_RETURN ) {
2061           return ( sel );
2062
2063         } else if ( event.key.keysym.sym == SDLK_q ) {
2064           exit ( 0 );
2065
2066         } else {
2067           return ( -1 ); // nada
2068         }
2069
2070         break;
2071
2072       } // switch
2073
2074       break;
2075     } // while
2076
2077   } // while
2078
2079   return ( -1 );
2080 }
2081
2082 unsigned char ui_determine_row ( mm_appref_t *a ) {
2083   unsigned int row = 0;
2084
2085   mm_appref_t *i = g_categories [ ui_category ].refs;
2086   while ( i != a ) {
2087     i = i -> next;
2088     row++;
2089   } // while
2090   row /= pnd_conf_get_as_int_d ( g_conf, "grid.col_max", 5 );
2091
2092   return ( row );
2093 }
2094
2095 unsigned char ui_determine_screen_row ( mm_appref_t *a ) {
2096   return ( ui_determine_row ( a ) % pnd_conf_get_as_int_d ( g_conf, "grid.row_max", 5 ) );
2097 }
2098
2099 unsigned char ui_determine_screen_col ( mm_appref_t *a ) {
2100   unsigned int col = 0;
2101
2102   mm_appref_t *i = g_categories [ ui_category ].refs;
2103   while ( i != a ) {
2104     i = i -> next;
2105     col++;
2106   } // while
2107   col %= pnd_conf_get_as_int_d ( g_conf, "grid.col_max", 5 );
2108
2109   return ( col );
2110 }
2111
2112 unsigned char ui_show_info ( char *pndrun, pnd_disco_t *p ) {
2113   char *viewer, *searchpath;
2114   pnd_conf_handle desktoph;
2115
2116   // viewer
2117   searchpath = pnd_conf_query_searchpath();
2118
2119   desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, searchpath );
2120
2121   if ( ! desktoph ) {
2122     return ( 0 );
2123   }
2124
2125   viewer = pnd_conf_get_as_char ( desktoph, "info.viewer" );
2126
2127   if ( ! viewer ) {
2128     return ( 0 ); // no way to view the file
2129   }
2130
2131   // etc
2132   if ( ! p -> unique_id ) {
2133     return ( 0 );
2134   }
2135
2136   if ( ! p -> info_filename ) {
2137     return ( 0 );
2138   }
2139
2140   if ( ! p -> info_name ) {
2141     return ( 0 );
2142   }
2143
2144   if ( ! pndrun ) {
2145     return ( 0 );
2146   }
2147
2148   // exec line
2149   char args [ 1001 ];
2150   char *pargs = args;
2151   if ( pnd_conf_get_as_char ( desktoph, "info.viewer_args" ) ) {
2152     snprintf ( pargs, 1001, "%s %s",
2153                pnd_conf_get_as_char ( desktoph, "info.viewer_args" ), p -> info_filename );
2154   } else {
2155     pargs = NULL;
2156   }
2157
2158   char pndfile [ 1024 ];
2159   if ( p -> object_type == pnd_object_type_directory ) {
2160     // for PXML-app-dir, pnd_run.sh doesn't want the PXML.xml.. it just wants the dir-name
2161     strncpy ( pndfile, p -> object_path, 1000 );
2162   } else if ( p -> object_type == pnd_object_type_pnd ) {
2163     // pnd_run.sh wants the full path and filename for the .pnd file
2164     snprintf ( pndfile, 1020, "%s/%s", p -> object_path, p -> object_filename );
2165   }
2166
2167   if ( ! pnd_apps_exec ( pndrun, pndfile, p -> unique_id, viewer, p -> startdir, pargs,
2168                          p -> clockspeed ? atoi ( p -> clockspeed ) : 0, PND_EXEC_OPTION_NORUN ) )
2169   {
2170     return ( 0 );
2171   }
2172
2173   pnd_log ( pndn_debug, "Info Exec=%s\n", pnd_apps_exec_runline() );
2174
2175   // try running it
2176   int x;
2177   if ( ( x = fork() ) < 0 ) {
2178     pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
2179     return ( 0 );
2180   }
2181
2182   if ( x == 0 ) {
2183     execl ( "/bin/sh", "/bin/sh", "-c", pnd_apps_exec_runline(), (char*)NULL );
2184     pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", pnd_apps_exec_runline() );
2185     return ( 0 );
2186   }
2187
2188   return ( 1 );
2189 }
2190
2191 typedef struct {
2192   SDL_Rect r;
2193   int catnum;
2194   mm_appref_t *ref;
2195 } ui_touch_t;
2196 #define MAXTOUCH 100
2197 ui_touch_t ui_touchrects [ MAXTOUCH ];
2198 unsigned char ui_touchrect_count = 0;
2199
2200 void ui_register_reset ( void ) {
2201   bzero ( ui_touchrects, sizeof(ui_touch_t)*MAXTOUCH );
2202   ui_touchrect_count = 0;
2203   return;
2204 }
2205
2206 void ui_register_tab ( unsigned char catnum, unsigned int x, unsigned int y, unsigned int w, unsigned int h ) {
2207
2208   if ( ui_touchrect_count == MAXTOUCH ) {
2209     return;
2210   }
2211
2212   ui_touchrects [ ui_touchrect_count ].r.x = x;
2213   ui_touchrects [ ui_touchrect_count ].r.y = y;
2214   ui_touchrects [ ui_touchrect_count ].r.w = w;
2215   ui_touchrects [ ui_touchrect_count ].r.h = h;
2216   ui_touchrects [ ui_touchrect_count ].catnum = catnum;
2217   ui_touchrect_count++;
2218
2219   return;
2220 }
2221
2222 void ui_register_app ( mm_appref_t *app, unsigned int x, unsigned int y, unsigned int w, unsigned int h ) {
2223
2224   if ( ui_touchrect_count == MAXTOUCH ) {
2225     return;
2226   }
2227
2228   ui_touchrects [ ui_touchrect_count ].r.x = x;
2229   ui_touchrects [ ui_touchrect_count ].r.y = y;
2230   ui_touchrects [ ui_touchrect_count ].r.w = w;
2231   ui_touchrects [ ui_touchrect_count ].r.h = h;
2232   ui_touchrects [ ui_touchrect_count ].ref = app;
2233   ui_touchrect_count++;
2234
2235   return;
2236 }
2237
2238 void ui_touch_act ( unsigned int x, unsigned int y ) {
2239
2240   unsigned char i;
2241   ui_touch_t *t;
2242
2243   for ( i = 0; i < ui_touchrect_count; i++ ) {
2244     t = &(ui_touchrects [ i ]);
2245
2246     if ( x >= t -> r.x &&
2247          x <= t -> r.x + t -> r.w &&
2248          y >= t -> r.y &&
2249          y <= t -> r.y + t -> r.h
2250        )
2251     {
2252
2253       if ( t -> ref ) {
2254         ui_selected = t -> ref;
2255         ui_push_exec();
2256       } else {
2257         if ( ui_category != t -> catnum ) {
2258           ui_selected = NULL;
2259         }
2260         ui_category = t -> catnum;
2261         render_mask |= CHANGED_CATEGORY;
2262       }
2263
2264       break;
2265     }
2266
2267   } // for
2268
2269   return;
2270 }
2271
2272 unsigned char ui_forkexec ( char *argv[] ) {
2273   char *fooby = argv[0];
2274   int x;
2275
2276   if ( ( x = fork() ) < 0 ) {
2277     pnd_log ( pndn_error, "ERROR: Couldn't fork() for '%s'\n", fooby );
2278     return ( 0 );
2279   }
2280
2281   if ( x == 0 ) { // child
2282     execv ( fooby, argv );
2283     pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", fooby );
2284     return ( 0 );
2285   }
2286
2287   // parent, success
2288   return ( 1 );
2289 }
2290
2291 unsigned char ui_threaded_defered_preview ( pnd_disco_t *p ) {
2292
2293   if ( ! cache_preview ( p, pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 200 ),
2294                          pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 180 ) )
2295      )
2296   {
2297     pnd_log ( pndn_debug, "THREAD: Couldn't load preview pic: '%s' -> '%s'\n",
2298               IFNULL(p->title_en,"No Name"), p -> preview_pic1 );
2299   }
2300
2301   // trigger that we completed
2302   SDL_Event e;
2303   bzero ( &e, sizeof(SDL_Event) );
2304   e.type = SDL_USEREVENT;
2305   e.user.code = sdl_user_finishedpreview;
2306   e.user.data1 = p;
2307   SDL_PushEvent ( &e );
2308
2309   return ( 0 );
2310 }
2311
2312 SDL_Thread *g_icon_thread = NULL;
2313 void ui_post_scan ( void ) {
2314
2315   // if deferred icon load, kick off the thread now
2316   if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_icons_later", 0 ) == 1 ) {
2317
2318     g_icon_thread = SDL_CreateThread ( (void*)ui_threaded_defered_icon, NULL );
2319
2320     if ( ! g_icon_thread ) {
2321       pnd_log ( pndn_error, "ERROR: Couldn't create icon thread\n" );
2322     }
2323
2324   } // deferred icon load
2325
2326   return;
2327 }
2328
2329 unsigned char ui_threaded_defered_icon ( void *p ) {
2330   extern pnd_box_handle g_active_apps;
2331   pnd_box_handle h = g_active_apps;
2332
2333   unsigned char maxwidth, maxheight;
2334   maxwidth = pnd_conf_get_as_int_d ( g_conf, "grid.icon_max_width", 50 );
2335   maxheight = pnd_conf_get_as_int_d ( g_conf, "grid.icon_max_height", 50 );
2336
2337   pnd_disco_t *iter = pnd_box_get_head ( h );
2338
2339   while ( iter ) {
2340
2341     // cache it
2342     if ( iter -> pnd_icon_pos &&
2343          ! cache_icon ( iter, maxwidth, maxheight ) )
2344     {
2345       pnd_log ( pndn_warning, "  Couldn't load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
2346     } else {
2347
2348       // trigger that we completed
2349       SDL_Event e;
2350       bzero ( &e, sizeof(SDL_Event) );
2351       e.type = SDL_USEREVENT;
2352       e.user.code = sdl_user_finishedicon;
2353       SDL_PushEvent ( &e );
2354
2355       //pnd_log ( pndn_warning, "  Finished deferred load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
2356       usleep ( pnd_conf_get_as_int_d ( g_conf, "minimenu.defer_icon_us", 50000 ) );
2357
2358     }
2359
2360     // next
2361     iter = pnd_box_get_next ( iter );
2362   } // while
2363
2364   return ( 0 );
2365 }
2366
2367 void ui_show_hourglass ( unsigned char updaterect ) {
2368
2369   SDL_Rect dest;
2370   SDL_Surface *s = g_imagecache [ IMG_HOURGLASS ].i;
2371
2372   dest.x = ( 800 - s -> w ) / 2;
2373   dest.y = ( 480 - s -> h ) / 2;
2374
2375   SDL_BlitSurface ( s, NULL /* whole image */, sdl_realscreen, &dest );
2376
2377   if ( updaterect ) {
2378     SDL_UpdateRects ( sdl_realscreen, 1, &dest );
2379   }
2380
2381   return;
2382 }
2383
2384 unsigned char ui_pick_skin ( void ) {
2385 #define MAXSKINS 10
2386   char *skins [ MAXSKINS ];
2387   unsigned char iter;
2388
2389   char *searchpath = pnd_conf_get_as_char ( g_conf, "minimenu.skin_searchpath" );
2390   char tempname [ 100 ];
2391
2392   iter = 0;
2393
2394   skins [ iter++ ] = "No skin change";
2395
2396   SEARCHPATH_PRE
2397   {
2398     DIR *d = opendir ( buffer );
2399
2400     if ( d ) {
2401       struct dirent *dd;
2402
2403       while ( ( dd = readdir ( d ) ) ) {
2404
2405         if ( dd -> d_name [ 0 ] == '.' ) {
2406           // ignore
2407         } else if ( ( dd -> d_type == DT_DIR || dd -> d_type == DT_UNKNOWN ) &&
2408                     iter < MAXSKINS )
2409         {
2410           snprintf ( tempname, 100, "Skin: %s", dd -> d_name );
2411           skins [ iter++ ] = strdup ( tempname );
2412         }
2413
2414       }
2415
2416       closedir ( d );
2417     }
2418
2419   }
2420   SEARCHPATH_POST
2421
2422   int sel = ui_modal_single_menu ( skins, iter, "Skins", "Enter to select; other to return." );
2423
2424   // did they pick one?
2425   if ( sel > 0 ) {
2426     FILE *f;
2427
2428     char *s = strdup ( pnd_conf_get_as_char ( g_conf, "minimenu.skin_selected" ) );
2429     s = pnd_expand_tilde ( s );
2430
2431     f = fopen ( s, "w" );
2432
2433     free ( s );
2434
2435     if ( f ) {
2436       fprintf ( f, "%s\n", skins [ sel ] + 6 );
2437       fclose ( f );
2438     }
2439
2440     return ( 1 );
2441   }
2442
2443   return ( 0 );
2444 }