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