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