b362918c125ed9d3e0a65abef7d8f4f43c7accf8
[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 <ctype.h>
13
14 #include "SDL.h"
15 #include "SDL_audio.h"
16 #include "SDL_image.h"
17 #include "SDL_ttf.h"
18 #include "SDL_gfxPrimitives.h"
19 #include "SDL_rotozoom.h"
20 #include "SDL_thread.h"
21 #include <dirent.h>
22
23 #include "pnd_conf.h"
24 #include "pnd_logger.h"
25 #include "pnd_pxml.h"
26 #include "pnd_container.h"
27 #include "pnd_discovery.h"
28 #include "pnd_apps.h"
29 #include "pnd_device.h"
30 #include "../lib/pnd_pathiter.h"
31 #include "pnd_utility.h"
32 #include "pnd_pndfiles.h"
33 #include "pnd_notify.h"
34 #include "pnd_dbusnotify.h"
35
36 #include "mmenu.h"
37 #include "mmcat.h"
38 #include "mmcache.h"
39 #include "mmui.h"
40 #include "mmwrapcmd.h"
41 #include "mmconf.h"
42 #include "mmui_context.h"
43 #include "freedesktop_cats.h"
44 #include "mmcustom_cats.h"
45
46 #define CHANGED_NOTHING     (0)
47 #define CHANGED_CATEGORY    (1<<0)  /* changed to different category */
48 #define CHANGED_SELECTION   (1<<1)  /* changed app selection */
49 #define CHANGED_DATAUPDATE  (1<<2)  /* deferred preview pic or icon came in */
50 #define CHANGED_APPRELOAD   (1<<3)  /* new set of applications entirely */
51 #define CHANGED_EVERYTHING  (1<<4)  /* redraw it all! */
52 unsigned int render_mask = CHANGED_EVERYTHING;
53
54 SDL_Thread *g_icon_thread = NULL;
55 unsigned char g_icon_thread_stop = 0; /* if !0 means thread should stop and maim app may block until it goes back to 0.. */
56 unsigned char g_icon_thread_busy = 0; /* if !0 means thread is running right now */
57
58 #define MIMETYPE_EXE "/usr/bin/file"     /* check for file type prior to invocation */
59
60 /* SDL
61  */
62 SDL_Surface *sdl_realscreen = NULL;
63 unsigned int sdl_ticks = 0;
64 SDL_Thread *g_preview_thread = NULL;
65 SDL_Thread *g_timer_thread = NULL;
66
67 enum { sdl_user_ticker = 0,
68        sdl_user_finishedpreview = 1,
69        sdl_user_finishedicon = 2,
70        sdl_user_checksd = 3,
71 };
72
73 /* app state
74  */
75 unsigned short int g_scale = 1; // 1 == noscale
76
77 SDL_Surface *g_imgcache [ IMG_MAX ];
78
79 TTF_Font *g_big_font = NULL;
80 TTF_Font *g_grid_font = NULL;
81 TTF_Font *g_detailtext_font = NULL;
82 TTF_Font *g_tab_font = NULL;
83
84 extern pnd_conf_handle g_conf;
85
86 /* current display state
87  */
88 int ui_rows_scrolled_down = 0;          // number of rows that should be missing from top of the display
89 mm_appref_t *ui_selected = NULL;
90 unsigned char ui_category = 0;          // current category
91 unsigned char ui_catshift = 0;          // how many cats are offscreen to the left
92 ui_viewmode_e ui_viewmode = uiv_icons;  // default to traditional icon view; why or why is viewstate not per-viewmode :/
93 ui_context_t ui_display_context;        // display paramaters: see mmui_context.h
94 unsigned char ui_detail_hidden = 0;     // if >0, detail panel is hidden
95 // FUTURE: If multiple panels can be shown/hidden, convert ui_detail_hidden to a bitmask
96
97 SDL_Surface *ui_scale_image ( SDL_Surface *s, unsigned int maxwidth, int maxheight ); // height -1 means ignore
98 static int ui_selected_index ( void );
99 static void ui_start_defered_icon_thread ( void );
100 static void ui_stop_defered_icon_thread ( void );
101
102 unsigned char ui_setup ( void ) {
103
104   /* set up SDL
105    */
106
107   SDL_Init ( SDL_INIT_EVERYTHING | SDL_INIT_NOPARACHUTE );
108
109   SDL_JoystickOpen ( 0 ); // turn on joy-0
110
111   SDL_WM_SetCaption ( "mmenu", "mmenu" );
112
113   // hide the mouse cursor if we can
114   if ( SDL_ShowCursor ( -1 ) == 1 ) {
115     SDL_ShowCursor ( 0 );
116   }
117
118   atexit ( SDL_Quit );
119
120   // open up a surface
121   unsigned int svm = SDL_SWSURFACE /*| SDL_FULLSCREEN*/ /* 0*/;
122   if ( pnd_conf_get_as_int_d ( g_conf, "display.fullscreen", 0 ) > 0 ) {
123     svm |= SDL_FULLSCREEN;
124   }
125
126   sdl_realscreen =
127     SDL_SetVideoMode ( 800 * g_scale, 480 * g_scale, 16 /*bpp*/, svm );
128
129   if ( ! sdl_realscreen ) {
130     pnd_log ( pndn_error, "ERROR: Couldn't open SDL real screen; dieing." );
131     return ( 0 );
132   }
133
134   pnd_log ( pndn_debug, "Pixel format:" );
135   pnd_log ( pndn_debug, "bpp b: %u\n", sdl_realscreen -> format -> BitsPerPixel );
136   pnd_log ( pndn_debug, "bpp B: %u\n", sdl_realscreen -> format -> BytesPerPixel );
137   pnd_log ( pndn_debug, "R mask: %08x\n", sdl_realscreen -> format -> Rmask );
138   pnd_log ( pndn_debug, "G mask: %08x\n", sdl_realscreen -> format -> Gmask );
139   pnd_log ( pndn_debug, "B mask: %08x\n", sdl_realscreen -> format -> Bmask );
140
141 #if 0 // audio
142   {
143     SDL_AudioSpec fmt;
144
145     /* Set 16-bit stereo audio at 22Khz */
146     fmt.freq = 44100; //22050;
147     fmt.format = AUDIO_S16; //AUDIO_S16;
148     fmt.channels = 1;
149     fmt.samples = 2048;        /* A good value for games */
150     fmt.callback = mixaudio;
151     fmt.userdata = NULL;
152
153     /* Open the audio device and start playing sound! */
154     if ( SDL_OpenAudio ( &fmt, NULL ) < 0 ) {
155       zotlog ( "Unable to open audio: %s\n", SDL_GetError() );
156       exit ( 1 );
157     }
158
159     SDL_PauseAudio ( 0 );
160   }
161 #endif
162
163   // key repeat
164   SDL_EnableKeyRepeat ( 500, 125 /* 150 */ );
165
166   // images
167   //IMG_Init ( IMG_INIT_JPG | IMG_INIT_PNG );
168
169   /* fonts
170    */
171
172   // init
173   if ( TTF_Init() == -1 ) {
174     pnd_log ( pndn_error, "ERROR: Couldn't set up SDL TTF lib\n" );
175     return ( 0 ); // couldn't set up SDL TTF
176   }
177
178   char fullpath [ PATH_MAX ];
179   // big font
180   sprintf ( fullpath, "%s/%s", g_skinpath, pnd_conf_get_as_char ( g_conf, "minimenu.font" ) );
181   g_big_font = TTF_OpenFont ( fullpath, pnd_conf_get_as_int_d ( g_conf, "minimenu.font_ptsize", 24 ) );
182   if ( ! g_big_font ) {
183     pnd_log ( pndn_error, "ERROR: Couldn't load font '%s' for size %u\n",
184               pnd_conf_get_as_char ( g_conf, "minimenu.font" ), pnd_conf_get_as_int_d ( g_conf, "minimenu.font_ptsize", 24 ) );
185     return ( 0 ); // couldn't set up SDL TTF
186   }
187
188   // grid font
189   sprintf ( fullpath, "%s/%s", g_skinpath, pnd_conf_get_as_char ( g_conf, "grid.font" ) );
190   g_grid_font = TTF_OpenFont ( fullpath, pnd_conf_get_as_int_d ( g_conf, "grid.font_ptsize", 10 ) );
191   if ( ! g_grid_font ) {
192     pnd_log ( pndn_error, "ERROR: Couldn't load font '%s' for size %u\n",
193               pnd_conf_get_as_char ( g_conf, "grid.font" ), pnd_conf_get_as_int_d ( g_conf, "grid.font_ptsize", 10 ) );
194     return ( 0 ); // couldn't set up SDL TTF
195   }
196
197   // detailtext font
198   sprintf ( fullpath, "%s/%s", g_skinpath, pnd_conf_get_as_char ( g_conf, "detailtext.font" ) );
199   g_detailtext_font = TTF_OpenFont ( fullpath, pnd_conf_get_as_int_d ( g_conf, "detailtext.font_ptsize", 10 ) );
200   if ( ! g_detailtext_font ) {
201     pnd_log ( pndn_error, "ERROR: Couldn't load font '%s' for size %u\n",
202               pnd_conf_get_as_char ( g_conf, "detailtext.font" ), pnd_conf_get_as_int_d ( g_conf, "detailtext.font_ptsize", 10 ) );
203     return ( 0 ); // couldn't set up SDL TTF
204   }
205
206   // tab font
207   sprintf ( fullpath, "%s/%s", g_skinpath, pnd_conf_get_as_char ( g_conf, "tabs.font" ) );
208   g_tab_font = TTF_OpenFont ( fullpath, pnd_conf_get_as_int_d ( g_conf, "tabs.font_ptsize", 10 ) );
209   if ( ! g_tab_font ) {
210     pnd_log ( pndn_error, "ERROR: Couldn't load font '%s' for size %u\n",
211               pnd_conf_get_as_char ( g_conf, "tabs.font" ), pnd_conf_get_as_int_d ( g_conf, "tabs.font_ptsize", 10 ) );
212     return ( 0 ); // couldn't set up SDL TTF
213   }
214
215   // determine display context
216   if ( pnd_conf_get_as_int_d ( g_conf, "display.show_detail_pane", 1 ) > 0 ) {
217     ui_detail_hidden = 0;
218   } else {
219     ui_detail_hidden = 1;
220   }
221   ui_recache_context ( &ui_display_context );
222
223   return ( 1 );
224 }
225
226 mm_imgcache_t g_imagecache [ IMG_TRUEMAX ] = {
227   { IMG_BACKGROUND_800480,    "graphics.IMG_BACKGROUND_800480" },
228   { IMG_BACKGROUND_TABMASK,   "graphics.IMG_BACKGROUND_TABMASK" },
229   { IMG_DETAIL_PANEL,         "graphics.IMG_DETAIL_PANEL" },
230   { IMG_DETAIL_BG,            "graphics.IMG_DETAIL_BG" },
231   { IMG_SELECTED_ALPHAMASK,   "graphics.IMG_SELECTED_ALPHAMASK" },
232   { IMG_TAB_SEL,              "graphics.IMG_TAB_SEL" },
233   { IMG_TAB_SEL_L,            "graphics.IMG_TAB_SEL_L" },
234   { IMG_TAB_SEL_R,            "graphics.IMG_TAB_SEL_R" },
235   { IMG_TAB_UNSEL,            "graphics.IMG_TAB_UNSEL" },
236   { IMG_TAB_UNSEL_L,          "graphics.IMG_TAB_UNSEL_L" },
237   { IMG_TAB_UNSEL_R,          "graphics.IMG_TAB_UNSEL_R" },
238   { IMG_TAB_LINE,             "graphics.IMG_TAB_LINE" },
239   { IMG_TAB_LINEL,            "graphics.IMG_TAB_LINEL" },
240   { IMG_TAB_LINER,            "graphics.IMG_TAB_LINER" },
241   { IMG_ICON_MISSING,         "graphics.IMG_ICON_MISSING" },
242   { IMG_SELECTED_HILITE,      "graphics.IMG_SELECTED_HILITE" },
243   { IMG_PREVIEW_MISSING,      "graphics.IMG_PREVIEW_MISSING" },
244   { IMG_ARROW_UP,             "graphics.IMG_ARROW_UP", },
245   { IMG_ARROW_DOWN,           "graphics.IMG_ARROW_DOWN", },
246   { IMG_ARROW_SCROLLBAR,      "graphics.IMG_ARROW_SCROLLBAR", },
247   { IMG_HOURGLASS,            "graphics.IMG_HOURGLASS", },
248   { IMG_FOLDER,               "graphics.IMG_FOLDER", },
249   { IMG_EXECBIN,              "graphics.IMG_EXECBIN", },
250   { IMG_SUBCATFOLDER,         "graphics.IMG_SUBCATFOLDER", "graphics.IMG_FOLDER", },
251   { IMG_DOTDOTFOLDER,         "graphics.IMG_DOTDOTFOLDER", "graphics.IMG_FOLDER", },
252   { IMG_MAX,                  NULL },
253   { IMG_LIST_ALPHAMASK,       NULL }, // generated
254   { IMG_LIST_ALPHAMASK_W,     NULL }, // generated
255 };
256
257 unsigned char ui_imagecache ( char *basepath ) {
258   unsigned int i;
259   char fullpath [ PATH_MAX ];
260   unsigned char try;
261
262   // loaded
263
264   for ( i = 0; i < IMG_MAX; i++ ) {
265
266     if ( g_imagecache [ i ].id != i ) {
267       pnd_log ( pndn_error, "ERROR: Internal table mismatch during caching [%u]\n", i );
268       exit ( -1 );
269     }
270
271     for ( try = 0; try < 2; try++ ) {
272
273       char *filename;
274
275       if ( try == 0 ) {
276         filename = pnd_conf_get_as_char ( g_conf, g_imagecache [ i ].confname );
277       } else {
278         if ( g_imagecache [ i ].alt_confname ) {
279           filename = pnd_conf_get_as_char ( g_conf, g_imagecache [ i ].alt_confname );
280         } else {
281           return ( 0 );
282         }
283       }
284
285       if ( ! filename ) {
286         pnd_log ( pndn_error, "ERROR: (Try %u) Missing filename in conf for key: %s\n", try + 1, g_imagecache [ i ].confname );
287         if ( try == 0 ) { continue; } else { return ( 0 ); }
288       }
289
290       if ( filename [ 0 ] == '/' ) {
291         strncpy ( fullpath, filename, PATH_MAX );
292       } else {
293         sprintf ( fullpath, "%s/%s", basepath, filename );
294       }
295
296       if ( ( g_imagecache [ i ].i = IMG_Load ( fullpath ) ) ) {
297         break; // no retry needed
298       } else {
299         pnd_log ( pndn_error, "ERROR: (Try %u) Couldn't load static cache image: %s\n", try + 1, fullpath );
300         if ( try == 0 ) { continue; } else { return ( 0 ); }
301       }
302
303     } // try twice
304
305   } // for
306
307   // generated
308   //
309
310   //g_imagecache [ IMG_SELECTED_ALPHAMASK ].i = SDL_CreateRGBSurface ( SDL_SWSURFACE, 60, 60, 32, 0xFF0000, 0x00FF00, 0xFF, 0xFF000000 );
311   //boxRGBA ( g_imagecache [ IMG_SELECTED_ALPHAMASK ].i, 0, 0, 60, 60, 100, 100, 100, 250 );
312
313   SDL_Surface *p = g_imagecache [ IMG_SELECTED_ALPHAMASK ].i;
314   g_imagecache [ IMG_LIST_ALPHAMASK ].i = SDL_ConvertSurface ( p, p -> format, p -> flags );
315   g_imagecache [ IMG_LIST_ALPHAMASK_W ].i = SDL_ConvertSurface ( p, p -> format, p -> flags );
316
317   g_imagecache [ IMG_LIST_ALPHAMASK ].i =
318     ui_scale_image ( g_imagecache [ IMG_LIST_ALPHAMASK ].i,
319                      pnd_conf_get_as_int ( g_conf, "grid.col_max" ) * pnd_conf_get_as_int ( g_conf, "grid.cell_width" ) , -1 );
320
321   g_imagecache [ IMG_LIST_ALPHAMASK_W ].i =
322     ui_scale_image ( g_imagecache [ IMG_LIST_ALPHAMASK_W ].i,
323                      pnd_conf_get_as_int ( g_conf, "grid.col_max_w" ) * pnd_conf_get_as_int ( g_conf, "grid.cell_width_w" ) , -1 );
324
325   // post processing
326   //
327
328   // scale icons
329   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 );
330   // scale text hilight
331   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 );
332   // scale preview no-pic
333   g_imagecache [ IMG_PREVIEW_MISSING ].i = ui_scale_image ( g_imagecache [ IMG_PREVIEW_MISSING ].i,
334                                                             pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 50 ),
335                                                             pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 50 ) );
336
337   // set alpha on detail panel
338   //SDL_SetAlpha ( g_imagecache [ IMG_DETAIL_BG ].i, SDL_SRCALPHA, pnd_conf_get_as_int_d ( g_conf, "display.detail_bg_alpha", 50 ) );
339
340   return ( 1 );
341 } // ui_imagecache
342
343 void ui_render ( void ) {
344
345   // 800x480:
346   // divide width: 550 / 250
347   // divide left side: 5 columns == 110px width
348   //   20px buffer either side == 70px wide icon + 20 + 20?
349
350   // what jobs to do during render?
351   //
352
353 #define R_BG     (1<<0)
354 #define R_TABS   (1<<1)
355 #define R_DETAIL (1<<2)
356 #define R_GRID   (1<<3)
357
358 #define R_ALL (R_BG|R_TABS|R_DETAIL|R_GRID)
359
360   unsigned int render_jobs_b = 0;
361
362   if ( render_mask & CHANGED_EVERYTHING ) {
363     render_jobs_b |= R_ALL;
364   }
365
366   if ( render_mask & CHANGED_SELECTION ) {
367     render_jobs_b |= R_GRID;
368     render_jobs_b |= R_DETAIL;
369   }
370
371   if ( render_mask & CHANGED_CATEGORY ) {
372     render_jobs_b |= R_ALL;
373   }
374
375   render_mask = CHANGED_NOTHING;
376
377   // render everything
378   //
379   unsigned int icon_rows;
380   unsigned int icon_visible_rows = 0; // list view, max number of visible rows
381
382 #define MAXRECTS 200
383   SDL_Rect rects [ MAXRECTS ], src;
384   SDL_Rect *dest = rects;
385   bzero ( dest, sizeof(SDL_Rect)*MAXRECTS );
386
387   unsigned int row, displayrow, col;
388   mm_appref_t *appiter;
389
390   ui_context_t *c = &ui_display_context; // for convenience and shorthand
391
392   // on demand icon loading
393   static int load_visible = -1;
394   if ( load_visible == -1 ) {
395     load_visible = pnd_conf_get_as_int_d ( g_conf, "minimenu.load_visible_icons", 0 );
396   }
397
398 #if 1
399   // if no selected app yet, select the first one
400   if ( ! ui_selected && pnd_conf_get_as_int_d ( g_conf, "minimenu.start_selected", 0 ) ) {
401
402     // pick first visible app
403     ui_selected = g_categories [ ui_category ] -> refs;
404
405     // change.. so we pick first visible if option is set .. but now we also try to restore
406     // selection to the last app selected in previous session (if there is one.)
407     char *previous_unique_id = pnd_conf_get_as_char ( g_conf, "minimenu.last_known_app_uid" );
408
409     if ( previous_unique_id ) {
410
411       // 1) we should already be in the right category, since its set in ui_post_scan to minimenu.last_known_catname
412       // 2) so we just pick the app in question..
413       mm_appref_t *previter = g_categories [ ui_category ] -> refs;
414       while ( previter ) {
415         if ( strcmp ( previter -> ref -> unique_id, previous_unique_id ) == 0 ) {
416           break;
417         }
418         previter = previter -> next;
419       }
420
421       if ( previter ) {
422         ui_selected = previter;
423       }
424
425     } // last known app?
426
427   }
428 #endif
429
430   // how many total rows do we need?
431   if ( g_categorycount ) {
432
433     if ( ui_viewmode == uiv_list ) {
434       // in list view, dimension of grid area is ..
435       // grid height == cell-height * row-max
436       // font height == display_context -> text_height
437       // padding == icon_offset_y
438       // max visible --> row-max == grid height / ( font-height + padding )
439
440       icon_rows = g_categories [ ui_category ] -> refcount; // one app per row
441       icon_visible_rows = ( c -> cell_height * c -> row_max ) / ( c -> text_height + c -> icon_offset_y );
442
443     } else {
444
445       icon_rows = g_categories [ ui_category ] -> refcount / c -> col_max;
446       if ( g_categories [ ui_category ] -> refcount % c -> col_max > 0 ) {
447         icon_rows++;
448       }
449
450     }
451
452   } else {
453     icon_rows = 0;
454     icon_visible_rows = 0;
455   }
456
457   // reset touchscreen regions
458   if ( render_jobs_b ) {
459     ui_register_reset();
460   }
461
462   // ensure selection is visible
463   if ( ui_selected ) {
464
465     unsigned char autoscrolled = 1;
466     while ( autoscrolled ) {
467       autoscrolled = 0;
468
469       int index = ui_selected_index();
470
471       if ( ui_viewmode == uiv_list ) {
472
473         if ( index >= ui_rows_scrolled_down + icon_visible_rows ) {
474           ui_rows_scrolled_down += 1;
475           autoscrolled = 1;
476           render_jobs_b |= R_ALL;
477         } else if ( index < ui_rows_scrolled_down ) {
478           ui_rows_scrolled_down -= 1;
479           autoscrolled = 1;
480           render_jobs_b |= R_ALL;
481         }
482
483       } else {
484         // icons
485
486         int topleft = c -> col_max * ui_rows_scrolled_down;
487         int botright = ( c -> col_max * ( ui_rows_scrolled_down + c -> row_max ) - 1 );
488
489         if ( index < topleft ) {
490           ui_rows_scrolled_down -= pnd_conf_get_as_int_d ( g_conf, "grid.scroll_increment", 1 );
491           render_jobs_b |= R_ALL;
492           autoscrolled = 1;
493         } else if ( index > botright ) {
494           ui_rows_scrolled_down += pnd_conf_get_as_int_d ( g_conf, "grid.scroll_increment", 1 );
495           render_jobs_b |= R_ALL;
496           autoscrolled = 1;
497         }
498
499       } // viewmode
500
501     } // while autoscrolling
502
503     if ( ui_rows_scrolled_down < 0 ) {
504       ui_rows_scrolled_down = 0;
505     } else if ( ui_rows_scrolled_down > icon_rows ) {
506       ui_rows_scrolled_down = icon_rows;
507     }
508
509   } // ensure visible
510
511   // render background
512   if ( render_jobs_b & R_BG ) {
513
514     if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
515       dest -> x = 0;
516       dest -> y = 0;
517       dest -> w = sdl_realscreen -> w;
518       dest -> h = sdl_realscreen -> h;
519       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, NULL /* whole image */, sdl_realscreen, dest /* 0,0 */ );
520       dest++;
521     }
522
523     // tabmask
524     if ( g_imagecache [ IMG_BACKGROUND_TABMASK ].i ) {
525       dest -> x = 0;
526       dest -> y = 0;
527       dest -> w = sdl_realscreen -> w;
528       dest -> h = sdl_realscreen -> h;
529       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_TABMASK ].i, NULL /* whole image */, sdl_realscreen, dest /* 0,0 */ );
530       dest++;
531     }
532
533   } // r_bg
534
535   // tabs
536   if ( g_imagecache [ IMG_TAB_SEL ].i && g_imagecache [ IMG_TAB_UNSEL ].i ) {
537     static unsigned int tab_width;
538     static unsigned int tab_height;
539     static unsigned int tab_offset_x;
540     static unsigned int tab_offset_y;
541     static unsigned int text_offset_x;
542     static unsigned int text_offset_y;
543     static unsigned int text_width;
544
545     static unsigned char tab_first_run = 1;
546     if ( tab_first_run ) {
547       tab_first_run = 0;
548       tab_width = pnd_conf_get_as_int ( g_conf, "tabs.tab_width" );
549       tab_height = pnd_conf_get_as_int ( g_conf, "tabs.tab_height" );
550       tab_offset_x = pnd_conf_get_as_int ( g_conf, "tabs.tab_offset_x" );
551       tab_offset_y = pnd_conf_get_as_int ( g_conf, "tabs.tab_offset_y" );
552       text_offset_x = pnd_conf_get_as_int ( g_conf, "tabs.text_offset_x" );
553       text_offset_y = pnd_conf_get_as_int ( g_conf, "tabs.text_offset_y" );
554       text_width = pnd_conf_get_as_int ( g_conf, "tabs.text_width" );
555     }
556
557     unsigned int maxtab = ( c -> screen_width / tab_width ) < g_categorycount ? ( c -> screen_width / tab_width ) + ui_catshift : g_categorycount + ui_catshift;
558     unsigned int maxtabspot = ( c -> screen_width / tab_width );
559
560     if ( g_categorycount > 0 ) {
561
562       // draw tabs with categories
563       for ( col = ui_catshift;
564             col < maxtab;
565             col++ )
566       {
567
568         SDL_Surface *s;
569
570         // if this is the first (leftmost) tab, we use different artwork
571         // than if the other tabs (so skinner can link lines up nicely.)
572         if ( col == ui_catshift ) {
573           // leftmost tab, special case
574
575           if ( col == ui_category ) {
576             s = g_imagecache [ IMG_TAB_SEL_L ].i;
577           } else {
578             s = g_imagecache [ IMG_TAB_UNSEL_L ].i;
579           }
580
581         } else if ( col == maxtab - 1 ) {
582           // rightmost tab, special case
583
584           if ( col == ui_category ) {
585             s = g_imagecache [ IMG_TAB_SEL_R ].i;
586           } else {
587             s = g_imagecache [ IMG_TAB_UNSEL_R ].i;
588           }
589
590         } else {
591           // normal (not leftmost) tab
592
593           if ( col == ui_category ) {
594             s = g_imagecache [ IMG_TAB_SEL ].i;
595           } else {
596             s = g_imagecache [ IMG_TAB_UNSEL ].i;
597           }
598
599         } // first col, or not first col?
600
601         // draw tab
602         src.x = 0;
603         src.y = 0;
604 #if 0
605         src.w = tab_width;
606         if ( col == ui_category ) {
607           src.h = tab_selheight;
608         } else {
609           src.h = tab_height;
610         }
611 #else
612         src.w = s -> w;
613         src.h = s -> h;
614 #endif
615         dest -> x = tab_offset_x + ( (col-ui_catshift) * tab_width );
616         dest -> y = tab_offset_y;
617
618         // store touch info
619         ui_register_tab ( col, dest -> x, dest -> y, tab_width, tab_height );
620
621         if ( render_jobs_b & R_TABS ) {
622           //pnd_log ( pndn_debug, "tab %u at %ux%u\n", col, dest.x, dest.y );
623           SDL_BlitSurface ( s, &src, sdl_realscreen, dest );
624           dest++;
625
626           // draw tab line
627           if ( col == ui_category ) {
628             // no line for selected tab
629           } else {
630             if ( col - ui_catshift == 0 ) {
631               s = g_imagecache [ IMG_TAB_LINEL ].i;
632             } else if ( col - ui_catshift == maxtabspot - 1 ) {
633               s = g_imagecache [ IMG_TAB_LINER ].i;
634             } else {
635               s = g_imagecache [ IMG_TAB_LINE ].i;
636             }
637             dest -> x = tab_offset_x + ( (col-ui_catshift) * tab_width );
638             dest -> y = tab_offset_y + tab_height;
639             SDL_BlitSurface ( s, NULL /* whole image */, sdl_realscreen, dest );
640             dest++;
641           }
642
643           // draw text
644           SDL_Surface *rtext;
645           rtext = TTF_RenderText_Blended ( g_tab_font, g_categories [ col ] -> catname, c -> fontcolor );
646           src.x = 0;
647           src.y = 0;
648           src.w = rtext -> w < text_width ? rtext -> w : text_width;
649           src.h = rtext -> h;
650           dest -> x = tab_offset_x + ( (col-ui_catshift) * tab_width ) + text_offset_x;
651           dest -> y = tab_offset_y + text_offset_y;
652           SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
653           SDL_FreeSurface ( rtext );
654           dest++;
655
656         } // r_tabs
657
658       } // for
659
660     } // if we got categories
661
662     if ( render_jobs_b & R_TABS ) {
663
664       // draw tab lines under where tabs would be if we had categories
665       for ( /* foo */; col < maxtabspot; col++ ) {
666         SDL_Surface *s;
667
668         if ( col - ui_catshift == 0 ) {
669           s = g_imagecache [ IMG_TAB_LINEL ].i;
670         } else if ( col - ui_catshift == maxtabspot - 1 ) {
671           s = g_imagecache [ IMG_TAB_LINER ].i;
672         } else {
673           s = g_imagecache [ IMG_TAB_LINE ].i;
674         }
675         dest -> x = tab_offset_x + ( (col-ui_catshift) * tab_width );
676         dest -> y = tab_offset_y + tab_height;
677         SDL_BlitSurface ( s, NULL /* whole image */, sdl_realscreen, dest );
678         dest++;
679
680       } // for
681
682     } // r_tabs
683
684   } // tabs
685
686   // scroll bars and arrows
687   if ( render_jobs_b & R_BG ) {
688     unsigned char show_bar = 0;
689
690     // up?
691     if ( ui_rows_scrolled_down && g_imagecache [ IMG_ARROW_UP ].i ) {
692       dest -> x = c -> arrow_up_x;
693       dest -> y = c -> arrow_up_y;
694       SDL_BlitSurface ( g_imagecache [ IMG_ARROW_UP ].i, NULL /* whole image */, sdl_realscreen, dest );
695       dest++;
696
697       show_bar = 1;
698     }
699
700     // down?
701     if ( ui_rows_scrolled_down + c -> row_max < icon_rows && g_imagecache [ IMG_ARROW_DOWN ].i ) {
702       dest -> x = c -> arrow_down_x;
703       dest -> y = c -> arrow_down_y;
704       SDL_BlitSurface ( g_imagecache [ IMG_ARROW_DOWN ].i, NULL /* whole image */, sdl_realscreen, dest );
705       dest++;
706
707       show_bar = 1;
708     }
709
710     if ( show_bar ) {
711       // show scrollbar as well
712       src.x = 0;
713       src.y = 0;
714       src.w = c -> arrow_bar_clip_w;
715       src.h = c -> arrow_bar_clip_h;
716       dest -> x = c -> arrow_bar_x;
717       dest -> y = c -> arrow_bar_y;
718       SDL_BlitSurface ( g_imagecache [ IMG_ARROW_SCROLLBAR ].i, &src /* whole image */, sdl_realscreen, dest );
719       dest++;
720     } // bar
721
722   } // r_bg, scroll bars
723
724   // render detail pane bg
725   if ( render_jobs_b & R_DETAIL && ui_detail_hidden == 0 ) {
726
727     if ( pnd_conf_get_as_int_d ( g_conf, "detailpane.show", 1 ) ) {
728
729       // render detail bg
730       if ( g_imagecache [ IMG_DETAIL_BG ].i ) {
731         src.x = 0; // pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
732         src.y = 0; // pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
733         src.w = ((SDL_Surface*)(g_imagecache [ IMG_DETAIL_PANEL ].i)) -> w;
734         src.h = ((SDL_Surface*)(g_imagecache [ IMG_DETAIL_PANEL ].i)) -> h;
735         dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
736         dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
737         SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
738         dest++;
739       }
740
741       // render detail pane
742       if ( g_imagecache [ IMG_DETAIL_PANEL ].i ) {
743         dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
744         dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
745         SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_PANEL ].i, NULL /* whole image */, sdl_realscreen, dest );
746         dest++;
747       }
748
749     } // detailpane frame/bg
750
751   } // r_details
752
753   // anything to render?
754   if ( render_jobs_b & R_GRID && g_categorycount ) {
755
756     // if just rendering grid, and nothing else, better clear it first
757     if ( ! ( render_jobs_b & R_BG ) ) {
758       if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
759         src.x = c -> grid_offset_x;
760         src.y = c -> grid_offset_y + c -> sel_icon_offset_y;
761         src.w = c -> col_max * c -> cell_width;
762         src.h = c -> row_max * c -> cell_height;
763
764         dest -> x = c -> grid_offset_x;
765         dest -> y = c -> grid_offset_y + c -> sel_icon_offset_y;
766
767         SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, &src, sdl_realscreen, dest );
768         dest++;
769
770       }
771     }
772
773     // any apps to render at all?
774     if ( g_categories [ ui_category ] -> refs ) {
775
776       // render grid differently based on view mode..
777       //
778       if ( ui_viewmode == uiv_list ) {
779
780         appiter = g_categories [ ui_category ] -> refs;
781         row = 0; // row under consideration (ie: could be scrolled off)
782         displayrow = 0; // rows displayed
783
784         while ( appiter != NULL ) {
785
786           // do we even need to render it? or are we suppressing it due to rows scrolled off the top?
787           if ( row >= ui_rows_scrolled_down ) {
788
789             // background hilight
790             SDL_Surface *hilight = ui_detail_hidden ? g_imagecache [ IMG_LIST_ALPHAMASK_W ].i : g_imagecache [ IMG_LIST_ALPHAMASK ].i;
791             if ( appiter == ui_selected ) {
792               src.x = 0;
793               src.y = 0;
794               src.w = hilight -> w;
795               src.h = c -> text_height + c -> icon_offset_y;
796               dest -> x = c -> grid_offset_x + c -> text_clip_x;
797               dest -> y = c -> grid_offset_y + ( displayrow * ( c -> text_height + c -> icon_offset_y ) ) - ( c -> icon_offset_y / 2 );
798               SDL_BlitSurface ( hilight, &src, sdl_realscreen, dest );
799               dest++;
800             }
801
802             // show icon
803             mm_cache_t *ic = cache_query_icon ( appiter -> ref -> unique_id );
804             if ( ic ) {
805               dest -> x = c -> grid_offset_x + c -> text_clip_x;
806               dest -> y = c -> grid_offset_y + ( displayrow * ( c -> text_height + c -> icon_offset_y ) ) - ( c -> icon_offset_y / 2 );
807               SDL_BlitSurface ( ic -> itiny, NULL, sdl_realscreen, dest );
808             }
809
810             // show title text
811             if ( appiter -> ref -> title_en ) {
812               SDL_Surface *rtext;
813               rtext = TTF_RenderText_Blended ( g_grid_font, appiter -> ref -> title_en, c -> fontcolor );
814               src.x = 0;
815               src.y = 0;
816               src.w = hilight -> w; //c -> text_width < rtext -> w ? c -> text_width : rtext -> w;
817               src.h = rtext -> h;
818
819               dest -> x = c -> grid_offset_x + c -> text_clip_x;
820               dest -> x += 20; // so all title-text line up, regardless of icon presence
821 #if 0
822               if ( ic ) {
823                 dest -> x += 20; //((SDL_Surface*)ic -> i) -> w;
824               }
825 #endif
826
827               dest -> y = c -> grid_offset_y + ( displayrow * ( c -> text_height + c -> icon_offset_y ) );
828               SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
829               SDL_FreeSurface ( rtext );
830               dest++;
831             }
832
833           } // visible or scrolled?
834
835           if ( row >= ui_rows_scrolled_down ) {
836             displayrow++;
837           }
838
839           // are we done displaying rows?
840           if ( displayrow >= icon_visible_rows ) {
841             break;
842           }
843
844           appiter = appiter -> next;
845           row++;
846
847         } // while
848
849       } else {
850
851         appiter = g_categories [ ui_category ] -> refs;
852         row = 0;
853         displayrow = 0;
854
855         // until we run out of apps, or run out of space
856         while ( appiter != NULL ) {
857
858           for ( col = 0; col < c -> col_max && appiter != NULL; col++ ) {
859
860             // do we even need to render it? or are we suppressing it due to rows scrolled off the top?
861             if ( row >= ui_rows_scrolled_down ) {
862
863               // selected? show hilights
864               if ( appiter == ui_selected ) {
865                 SDL_Surface *s = g_imagecache [ IMG_SELECTED_ALPHAMASK ].i;
866                 // icon
867                 //dest -> x = grid_offset_x + ( col * cell_width ) + icon_offset_x + ( ( icon_max_width - s -> w ) / 2 );
868                 dest -> x = c -> grid_offset_x + ( col * c -> cell_width ) + c -> icon_offset_x + c -> sel_icon_offset_x;
869                 //dest -> y = grid_offset_y + ( displayrow * cell_height ) + icon_offset_y + ( ( icon_max_height - s -> h ) / 2 );
870                 dest -> y = c -> grid_offset_y + ( displayrow * c -> cell_height ) + c -> icon_offset_y + c -> sel_icon_offset_y;
871                 SDL_BlitSurface ( s, NULL /* all */, sdl_realscreen, dest );
872                 dest++;
873                 // text
874                 dest -> x = c -> grid_offset_x + ( col * c -> cell_width ) + c -> text_clip_x;
875                 dest -> y = c -> grid_offset_y + ( displayrow * c -> cell_height ) + c -> text_hilite_offset_y;
876                 SDL_BlitSurface ( g_imagecache [ IMG_SELECTED_HILITE ].i, NULL /* all */, sdl_realscreen, dest );
877                 dest++;
878               } // selected?
879
880               // show icon
881               mm_cache_t *ic = cache_query_icon ( appiter -> ref -> unique_id );
882               SDL_Surface *iconsurface;
883
884               // if icon not in cache, and its a pnd-file source, perhaps try to load it right now..
885               if ( ( ! ic ) &&
886                    ( load_visible ) &&
887                    ( ! ( appiter -> ref -> object_flags & PND_DISCO_GENERATED ) )
888                  )
889               {
890                 // try to load any icons that..
891                 // - are not yet loaded
892                 // - did not fail a previous load attempt
893                 // this way user can upfront load all icons, or defer all icons, or even defer all icons
894                 // and still try to load visible ones 'just before needed'; so not at mmenu load time, but
895                 // as needed (only the ones needed.)
896
897                 if ( ( appiter -> ref -> pnd_icon_pos ) ||
898                      ( appiter -> ref -> icon && appiter -> ref -> object_flags & PND_DISCO_LIBPND_DD )
899                    )
900                 {
901   
902                   // try to cache it?
903                   if ( ! cache_icon ( appiter -> ref, ui_display_context.icon_max_width, ui_display_context.icon_max_width ) ) {
904                     // erm..
905                   }
906
907                   // avoid churn
908                   appiter -> ref -> pnd_icon_pos = 0;
909                   if ( appiter -> ref -> icon ) {
910                     free ( appiter -> ref -> icon );
911                     appiter -> ref -> icon = NULL;
912                   }
913
914                   // pick up as if nothing happened..
915                   ic = cache_query_icon ( appiter -> ref -> unique_id );
916
917                 }
918
919               } // load icon during rendering?
920
921               if ( ic ) {
922                 iconsurface = ic -> i;
923               } else {
924                 //pnd_log ( pndn_warning, "WARNING: TBD: Need Missin-icon icon for '%s'\n", IFNULL(appiter -> ref -> title_en,"No Name") );
925
926                 // no icon override; was this a pnd-file (show the unknown icon then), or was this generated from
927                 // filesystem (file or directory icon)
928                 if ( appiter -> ref -> object_flags & PND_DISCO_GENERATED ) {
929                   if ( appiter -> ref -> object_type == pnd_object_type_directory ) {
930
931                     // is this a subcat, a .., or a filesystem folder?
932                     //iconsurface = g_imagecache [ IMG_FOLDER ].i;
933                     if ( g_categories [ ui_category ] -> fspath ) {
934                       iconsurface = g_imagecache [ IMG_FOLDER ].i;
935                     } else if ( strcmp ( appiter -> ref -> title_en, ".." ) == 0 ) {
936                       iconsurface = g_imagecache [ IMG_DOTDOTFOLDER ].i;
937                     } else {
938                       iconsurface = g_imagecache [ IMG_SUBCATFOLDER ].i;
939                     }
940
941                   } else {
942                     iconsurface = g_imagecache [ IMG_EXECBIN ].i;
943                   }
944                 } else {
945                   iconsurface = g_imagecache [ IMG_ICON_MISSING ].i;
946                 }
947
948               }
949
950               // got an icon I hope?
951               if ( iconsurface ) {
952                 //pnd_log ( pndn_debug, "Got an icon for '%s'\n", IFNULL(appiter -> ref -> title_en,"No Name") );
953
954                 src.x = 0;
955                 src.y = 0;
956                 src.w = 60;
957                 src.h = 60;
958                 dest -> x = c -> grid_offset_x + ( col * c -> cell_width ) + c -> icon_offset_x + (( c -> icon_max_width - iconsurface -> w ) / 2);
959                 dest -> y = c -> grid_offset_y + ( displayrow * c -> cell_height ) + c -> icon_offset_y + (( c -> icon_max_height - iconsurface -> h ) / 2);
960
961                 SDL_BlitSurface ( iconsurface, &src, sdl_realscreen, dest );
962
963                 // store touch info
964                 ui_register_app ( appiter, dest -> x, dest -> y, src.w, src.h );
965
966                 dest++;
967
968               }
969
970               // show text
971               if ( appiter -> ref -> title_en ) {
972                 SDL_Surface *rtext;
973                 rtext = TTF_RenderText_Blended ( g_grid_font, appiter -> ref -> title_en, c -> fontcolor );
974                 src.x = 0;
975                 src.y = 0;
976                 src.w = c -> text_width < rtext -> w ? c -> text_width : rtext -> w;
977                 src.h = rtext -> h;
978                 if ( rtext -> w > c -> text_width ) {
979                   dest -> x = c -> grid_offset_x + ( col * c -> cell_width ) + c -> text_clip_x;
980                 } else {
981                   dest -> x = c -> grid_offset_x + ( col * c -> cell_width ) + c -> text_offset_x - ( rtext -> w / 2 );
982                 }
983                 dest -> y = c -> grid_offset_y + ( displayrow * c -> cell_height ) + c -> text_offset_y;
984                 SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
985                 SDL_FreeSurface ( rtext );
986                 dest++;
987               }
988
989             } // display now? or scrolled away..
990
991             // next
992             appiter = appiter -> next;
993
994           } // for column 1...X
995
996           if ( row >= ui_rows_scrolled_down ) {
997             displayrow++;
998           }
999
1000           row ++;
1001
1002           // are we done displaying rows?
1003           if ( displayrow >= c -> row_max ) {
1004             break;
1005           }
1006
1007         } // while
1008
1009       } // icon view
1010
1011     } else {
1012       // no apps to render?
1013       pnd_log ( pndn_rem, "No applications to render?\n" );
1014     } // apps to renser?
1015
1016   } // r_grid
1017
1018   // detail panel - show app details or blank-message
1019   if ( render_jobs_b & R_DETAIL && ui_detail_hidden == 0 ) {
1020
1021     unsigned int cell_offset_x = pnd_conf_get_as_int ( g_conf, "detailtext.cell_offset_x" );
1022     unsigned int cell_offset_y = pnd_conf_get_as_int ( g_conf, "detailtext.cell_offset_y" );
1023     unsigned int cell_width = pnd_conf_get_as_int ( g_conf, "detailtext.cell_width" );
1024
1025     unsigned int desty = cell_offset_y;
1026
1027     if ( ui_selected ) {
1028
1029       char buffer [ 256 ];
1030
1031       // full name
1032       if ( ui_selected -> ref -> title_en ) {
1033         SDL_Surface *rtext;
1034         rtext = TTF_RenderText_Blended ( g_detailtext_font, ui_selected -> ref -> title_en, c -> fontcolor );
1035         src.x = 0;
1036         src.y = 0;
1037         src.w = rtext -> w < cell_width ? rtext -> w : cell_width;
1038         src.h = rtext -> h;
1039         dest -> x = cell_offset_x;
1040         dest -> y = desty;
1041         SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
1042         SDL_FreeSurface ( rtext );
1043         dest++;
1044         desty += src.h;
1045       }
1046
1047       // category
1048 #if 0
1049       if ( ui_selected -> ref -> main_category ) {
1050
1051         sprintf ( buffer, "Category: %s", ui_selected -> ref -> main_category );
1052
1053         SDL_Surface *rtext;
1054         rtext = TTF_RenderText_Blended ( g_detailtext_font, buffer, c -> fontcolor );
1055         src.x = 0;
1056         src.y = 0;
1057         src.w = rtext -> w < cell_width ? rtext -> w : cell_width;
1058         src.h = rtext -> h;
1059         dest -> x = cell_offset_x;
1060         dest -> y = desty;
1061         SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
1062         SDL_FreeSurface ( rtext );
1063         dest++;
1064         desty += src.h;
1065       }
1066 #endif
1067
1068       // clock
1069       if ( ui_selected -> ref -> clockspeed ) {
1070
1071         sprintf ( buffer, "CPU Clock: %s", ui_selected -> ref -> clockspeed );
1072
1073         SDL_Surface *rtext;
1074         rtext = TTF_RenderText_Blended ( g_detailtext_font, buffer, c -> fontcolor );
1075         src.x = 0;
1076         src.y = 0;
1077         src.w = rtext -> w < cell_width ? rtext -> w : cell_width;
1078         src.h = rtext -> h;
1079         dest -> x = cell_offset_x;
1080         dest -> y = desty;
1081         SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
1082         SDL_FreeSurface ( rtext );
1083         dest++;
1084         desty += src.h;
1085       }
1086
1087       // show sub-app# on right side of cpu clock?
1088       //if ( ui_selected -> ref -> subapp_number )
1089       {
1090         sprintf ( buffer, "(app#%u)", ui_selected -> ref -> subapp_number );
1091
1092         SDL_Surface *rtext;
1093         rtext = TTF_RenderText_Blended ( g_grid_font, buffer, c -> fontcolor );
1094         dest -> x = cell_offset_x + cell_width - rtext -> w;
1095         dest -> y = desty - src.h;
1096         SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
1097         SDL_FreeSurface ( rtext );
1098         dest++;
1099       }
1100
1101       // info hint
1102 #if 0 // merged into hint-line
1103       if ( ui_selected -> ref -> info_filename ) {
1104
1105         sprintf ( buffer, "Documentation - hit Y" );
1106
1107         SDL_Surface *rtext;
1108         rtext = TTF_RenderText_Blended ( g_detailtext_font, buffer, c -> fontcolor );
1109         src.x = 0;
1110         src.y = 0;
1111         src.w = rtext -> w < cell_width ? rtext -> w : cell_width;
1112         src.h = rtext -> h;
1113         dest -> x = cell_offset_x;
1114         dest -> y = desty;
1115         SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
1116         SDL_FreeSurface ( rtext );
1117         dest++;
1118         desty += src.h;
1119       }
1120 #endif
1121
1122       // notes
1123       if ( ui_selected -> ovrh ) {
1124         char *n;
1125         unsigned char i;
1126         char buffer [ 50 ];
1127
1128         desty += 5; // a touch of spacing can't hurt
1129
1130         for ( i = 1; i < 4; i++ ) {
1131           sprintf ( buffer, "Application-%u.note-%u", ui_selected -> ref -> subapp_number, i );
1132           n = pnd_conf_get_as_char ( ui_selected -> ovrh, buffer );
1133
1134           if ( n ) {
1135             SDL_Surface *rtext;
1136             rtext = TTF_RenderText_Blended ( g_detailtext_font, n, c -> fontcolor );
1137             src.x = 0;
1138             src.y = 0;
1139             src.w = rtext -> w < cell_width ? rtext -> w : cell_width;
1140             src.h = rtext -> h;
1141             dest -> x = cell_offset_x;
1142             dest -> y = desty;
1143             SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
1144             SDL_FreeSurface ( rtext );
1145             dest++;
1146             desty += rtext -> h;
1147           }
1148         } // for
1149
1150       } // r_detail -> notes
1151
1152       // preview pic
1153       mm_cache_t *ic = cache_query_preview ( ui_selected -> ref -> unique_id );
1154       SDL_Surface *previewpic;
1155
1156       if ( ic ) {
1157         previewpic = ic -> i;
1158       } else {
1159         previewpic = g_imagecache [ IMG_PREVIEW_MISSING ].i;
1160       }
1161
1162       if ( previewpic ) {
1163         dest -> x = pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_offset_x", 50 ) +
1164           ( ( pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 50 ) - previewpic -> w ) / 2 );
1165         dest -> y = pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_offset_y", 50 );
1166         SDL_BlitSurface ( previewpic, NULL /* whole image */, sdl_realscreen, dest );
1167         dest++;
1168       }
1169
1170     } else {
1171
1172       char *empty_message = "Press SELECT for menu";
1173
1174       SDL_Surface *rtext;
1175
1176       rtext = TTF_RenderText_Blended ( g_detailtext_font, empty_message, c -> fontcolor );
1177
1178       src.x = 0;
1179       src.y = 0;
1180       src.w = rtext -> w < cell_width ? rtext -> w : cell_width;
1181       src.h = rtext -> h;
1182       dest -> x = cell_offset_x;
1183       dest -> y = desty;
1184       SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
1185       SDL_FreeSurface ( rtext );
1186       dest++;
1187
1188       desty += src.h;
1189
1190       rtext = TTF_RenderText_Blended ( g_detailtext_font, "START or B to run app", c -> fontcolor );
1191       dest -> x = cell_offset_x;
1192       dest -> y = desty;
1193       SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
1194       SDL_FreeSurface ( rtext );
1195       dest++;
1196       desty += src.h;
1197
1198       rtext = TTF_RenderText_Blended ( g_detailtext_font, "SPACE for app menu", c -> fontcolor );
1199       dest -> x = cell_offset_x;
1200       dest -> y = desty;
1201       SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
1202       SDL_FreeSurface ( rtext );
1203       dest++;
1204       desty += src.h;
1205
1206       rtext = TTF_RenderText_Blended ( g_detailtext_font, "A to toggle details", c -> fontcolor );
1207       dest -> x = cell_offset_x;
1208       dest -> y = desty;
1209       SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
1210       SDL_FreeSurface ( rtext );
1211       dest++;
1212       desty += src.h;
1213
1214     } // r_detail && selected?
1215
1216   } // r_detail
1217
1218   // extras
1219   //
1220
1221   // battery
1222   if ( render_jobs_b & R_BG ) {
1223     static int last_battlevel = 0;
1224     static unsigned char batterylevel = 0;
1225     char buffer [ 100 ];
1226
1227     if ( time ( NULL ) - last_battlevel > 60 ) {
1228       batterylevel = pnd_device_get_battery_gauge_perc();
1229       last_battlevel = time ( NULL );
1230     }
1231
1232     sprintf ( buffer, "Battery: %u%%", batterylevel );
1233
1234     SDL_Surface *rtext;
1235     rtext = TTF_RenderText_Blended ( g_grid_font, buffer, c -> fontcolor );
1236     dest -> x = pnd_conf_get_as_int_d ( g_conf, "display.battery_x", 20 );
1237     dest -> y = pnd_conf_get_as_int_d ( g_conf, "display.battery_y", 450 );
1238     SDL_BlitSurface ( rtext, NULL /* all */, sdl_realscreen, dest );
1239     SDL_FreeSurface ( rtext );
1240     dest++;
1241   }
1242
1243   // hints
1244   if ( pnd_conf_get_as_char ( g_conf, "display.hintline" ) ) {
1245     char *buffer;
1246     unsigned int hintx, hinty;
1247     hintx = pnd_conf_get_as_int_d ( g_conf, "display.hint_x", 40 );
1248     hinty = pnd_conf_get_as_int_d ( g_conf, "display.hint_y", 450 );
1249     static unsigned int lastwidth = 3000;
1250
1251     if ( ui_selected && ui_selected -> ref -> info_filename ) {
1252       buffer = "Documentation - hit Y";
1253     } else {
1254       buffer = pnd_conf_get_as_char ( g_conf, "display.hintline" );
1255     }
1256
1257     SDL_Surface *rtext;
1258     rtext = TTF_RenderText_Blended ( g_grid_font, buffer, c -> fontcolor );
1259
1260     // clear bg
1261     if ( ! ( render_jobs_b & R_BG ) ) {
1262       src.x = hintx;
1263       src.y = hinty;
1264       src.w = lastwidth;
1265       src.h = rtext -> h;
1266       dest -> x = hintx;
1267       dest -> y = hinty;
1268       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_TABMASK ].i, &src, sdl_realscreen, dest );
1269       dest++;
1270       lastwidth = rtext -> w;
1271     }
1272
1273     // now render text
1274     dest -> x = hintx;
1275     dest -> y = hinty;
1276     SDL_BlitSurface ( rtext, NULL /* all */, sdl_realscreen, dest );
1277     SDL_FreeSurface ( rtext );
1278     dest++;
1279   }
1280
1281   // clock time
1282   if ( render_jobs_b & R_BG &&
1283        pnd_conf_get_as_int_d ( g_conf, "display.clock_x", -1 ) != -1 )
1284   {
1285     char buffer [ 50 ];
1286
1287     time_t t = time ( NULL );
1288     struct tm *tm = localtime ( &t );
1289     strftime ( buffer, 50, "%a %H:%M %F", tm );
1290
1291     SDL_Surface *rtext;
1292     rtext = TTF_RenderText_Blended ( g_grid_font, buffer, c -> fontcolor );
1293     dest -> x = pnd_conf_get_as_int_d ( g_conf, "display.clock_x", 700 );
1294     dest -> y = pnd_conf_get_as_int_d ( g_conf, "display.clock_y", 450 );
1295     SDL_BlitSurface ( rtext, NULL /* all */, sdl_realscreen, dest );
1296     SDL_FreeSurface ( rtext );
1297     dest++;
1298   }
1299
1300   // update all the rects and send it all to sdl
1301   // - at this point, we could probably just do 1 rect, of the
1302   //   whole screen, and be faster :/
1303   SDL_UpdateRects ( sdl_realscreen, dest - rects, rects );
1304
1305 } // ui_render
1306
1307 void ui_process_input ( pnd_dbusnotify_handle dbh, pnd_notify_handle nh ) {
1308   SDL_Event event;
1309
1310   unsigned char ui_event = 0; // if we get a ui event, flip to 1 and break
1311   //static ui_sdl_button_e ui_mask = uisb_none; // current buttons down
1312
1313   while ( ( ! ui_event ) &&
1314           /*block_p ?*/ SDL_WaitEvent ( &event ) /*: SDL_PollEvent ( &event )*/ )
1315   {
1316
1317     switch ( event.type ) {
1318
1319     case SDL_USEREVENT:
1320       // update something
1321
1322       // the user-defined SDL events are all for threaded/delayed previews (and icons, which
1323       // generally are not used); if we're in wide mode, we can skip previews
1324       // to avoid slowing things down when they're not shown.
1325
1326       if ( event.user.code == sdl_user_ticker ) {
1327
1328         if ( ui_detail_hidden ) {
1329           break; // skip building previews when not showing them
1330         }
1331
1332         // timer went off, time to load something
1333         if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_previews_later", 0 ) ) {
1334
1335           if ( ! ui_selected ) {
1336             break;
1337           }
1338
1339           // load the preview pics now!
1340           pnd_disco_t *iter = ui_selected -> ref;
1341
1342           if ( iter -> preview_pic1 ) {
1343
1344             if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.threaded_preview", 0 ) ) {
1345               // load in bg thread, make user experience chuggy
1346
1347               g_preview_thread = SDL_CreateThread ( (void*)ui_threaded_defered_preview, iter );
1348
1349               if ( ! g_preview_thread ) {
1350                 pnd_log ( pndn_error, "ERROR: Couldn't create preview thread\n" );
1351               }
1352
1353             } else {
1354               // load it now, make user wait
1355
1356               if ( ! cache_preview ( iter, pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 200 ),
1357                                      pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 180 ) )
1358                  )
1359               {
1360                 pnd_log ( pndn_debug, "  Couldn't load preview pic: '%s' -> '%s'\n",
1361                           IFNULL(iter->title_en,"No Name"), iter -> preview_pic1 );
1362               }
1363
1364             } // threaded?
1365
1366           } // got a preview at all?
1367
1368           ui_event++;
1369         }
1370
1371       } else if ( event.user.code == sdl_user_finishedpreview ) {
1372
1373         // if we just finished the one we happen to be looking at, better redraw now; otherwise, if
1374         // we finished another, no big woop
1375         if ( ui_selected && event.user.data1 == ui_selected -> ref ) {
1376           ui_event++;
1377         }
1378
1379       } else if ( event.user.code == sdl_user_finishedicon ) {
1380         // redraw, so we can show the newly loaded icon
1381         ui_event++;
1382
1383       } else if ( event.user.code == sdl_user_checksd ) {
1384         // check if any inotify-type events occured, forcing us to rescan/re-disco the SDs
1385
1386         unsigned char watch_dbus = 0;
1387         unsigned char watch_inotify = 0;
1388
1389         if ( dbh ) {
1390           watch_dbus = pnd_dbusnotify_rediscover_p ( dbh );
1391         }
1392
1393         if ( nh ) {
1394           watch_inotify = pnd_notify_rediscover_p ( nh );
1395         }
1396
1397         if ( watch_dbus || watch_inotify ) {
1398           pnd_log ( pndn_debug, "dbusnotify detected SD event\n" );
1399           applications_free();
1400           applications_scan();
1401
1402           ui_event++;
1403         }
1404
1405       } // SDL user event
1406
1407       render_mask |= CHANGED_EVERYTHING;
1408
1409       break;
1410
1411 #if 0 // joystick motion
1412     case SDL_JOYAXISMOTION:
1413
1414       pnd_log ( PND_LOG_DEFAULT, "joystick axis\n" );
1415
1416         if ( event.jaxis.axis == 0 ) {
1417           // horiz
1418           if ( event.jaxis.value < 0 ) {
1419             ui_push_left ( 0 );
1420             pnd_log ( PND_LOG_DEFAULT, "joystick axis - LEFT\n" );
1421           } else if ( event.jaxis.value > 0 ) {
1422             ui_push_right ( 0 );
1423             pnd_log ( PND_LOG_DEFAULT, "joystick axis - RIGHT\n" );
1424           }
1425         } else if ( event.jaxis.axis == 1 ) {
1426           // vert
1427           if ( event.jaxis.value < 0 ) {
1428             ui_push_up();
1429           } else if ( event.jaxis.value > 0 ) {
1430             ui_push_down();
1431           }
1432         }
1433
1434         ui_event++;
1435
1436         break;
1437 #endif
1438
1439 #if 0 // joystick buttons
1440     case SDL_JOYBUTTONDOWN:
1441
1442       pnd_log ( PND_LOG_DEFAULT, "joystick button down %u\n", event.jbutton.button );
1443
1444       if ( event.jbutton.button == 0 ) { // B
1445         ui_mask |= uisb_b;
1446       } else if ( event.jbutton.button == 1 ) { // Y
1447         ui_mask |= uisb_y;
1448       } else if ( event.jbutton.button == 2 ) { // X
1449         ui_mask |= uisb_x;
1450       } else if ( event.jbutton.button == 3 ) { // A
1451         ui_mask |= uisb_a;
1452
1453       } else if ( event.jbutton.button == 4 ) { // Select
1454         ui_mask |= uisb_select;
1455       } else if ( event.jbutton.button == 5 ) { // Start
1456         ui_mask |= uisb_start;
1457
1458       } else if ( event.jbutton.button == 7 ) { // L
1459         ui_mask |= uisb_l;
1460       } else if ( event.jbutton.button == 8 ) { // R
1461         ui_mask |= uisb_r;
1462
1463       }
1464
1465       ui_event++;
1466
1467       break;
1468
1469     case SDL_JOYBUTTONUP:
1470
1471       pnd_log ( PND_LOG_DEFAULT, "joystick button up %u\n", event.jbutton.button );
1472
1473       if ( event.jbutton.button == 0 ) { // B
1474         ui_mask &= ~uisb_b;
1475         ui_push_exec();
1476       } else if ( event.jbutton.button == 1 ) { // Y
1477         ui_mask &= ~uisb_y;
1478       } else if ( event.jbutton.button == 2 ) { // X
1479         ui_mask &= ~uisb_x;
1480       } else if ( event.jbutton.button == 3 ) { // A
1481         ui_mask &= ~uisb_a;
1482
1483       } else if ( event.jbutton.button == 4 ) { // Select
1484         ui_mask &= ~uisb_select;
1485       } else if ( event.jbutton.button == 5 ) { // Start
1486         ui_mask &= ~uisb_start;
1487
1488       } else if ( event.jbutton.button == 7 ) { // L
1489         ui_mask &= ~uisb_l;
1490         ui_push_ltrigger();
1491       } else if ( event.jbutton.button == 8 ) { // R
1492         ui_mask &= ~uisb_r;
1493         ui_push_rtrigger();
1494
1495       }
1496
1497       ui_event++;
1498
1499       break;
1500 #endif
1501
1502 #if 1 // keyboard events
1503     //case SDL_KEYUP:
1504     case SDL_KEYDOWN:
1505
1506       //pnd_log ( pndn_debug, "key up %u\n", event.key.keysym.sym );
1507
1508       // SDLK_LALT -> Start
1509       // page up/down for y/x
1510       // home/end for a and b
1511
1512       // directional
1513       if ( event.key.keysym.sym == SDLK_RIGHT ) {
1514         ui_push_right ( 0 );
1515         ui_event++;
1516       } else if ( event.key.keysym.sym == SDLK_LEFT ) {
1517         ui_push_left ( 0 );
1518         ui_event++;
1519       } else if ( event.key.keysym.sym == SDLK_UP ) {
1520         ui_push_up();
1521         ui_event++;
1522       } else if ( event.key.keysym.sym == SDLK_DOWN ) {
1523         ui_push_down();
1524         ui_event++;
1525       } else if ( event.key.keysym.sym == SDLK_END ) { // B
1526         ui_push_exec();
1527         ui_event++;
1528       } else if ( event.key.keysym.sym == SDLK_SPACE ) { // space
1529         if ( ui_selected ) {
1530           ui_menu_context ( ui_selected );
1531         } else {
1532           // TBD: post an error?
1533         }
1534         render_mask |= CHANGED_EVERYTHING;
1535         ui_event++;
1536       } else if ( event.key.keysym.sym == SDLK_TAB || event.key.keysym.sym == SDLK_HOME ) { // tab or A
1537         // if detail panel is togglable, then toggle it
1538         // if not, make sure its ruddy well shown!
1539         if ( ui_is_detail_hideable() ) {
1540           ui_toggle_detail_pane();
1541         } else {
1542           ui_detail_hidden = 0;
1543         }
1544         ui_event++;
1545       } else if ( event.key.keysym.sym == SDLK_RSHIFT || event.key.keysym.sym == SDLK_COMMA ) { // left trigger or comma
1546         ui_push_ltrigger();
1547         ui_event++;
1548       } else if ( event.key.keysym.sym == SDLK_RCTRL || event.key.keysym.sym == SDLK_PERIOD ) { // right trigger or period
1549         ui_push_rtrigger();
1550         ui_event++;
1551
1552       } else if ( event.key.keysym.sym == SDLK_PAGEUP ) { // Y
1553         // info
1554         if ( ui_selected ) {
1555           ui_show_info ( pnd_run_script, ui_selected -> ref );
1556           ui_event++;
1557         }
1558       } else if ( event.key.keysym.sym == SDLK_PAGEDOWN ) { // X
1559         ui_push_backup();
1560
1561         // forget the selection, nolonger applies
1562         ui_selected = NULL;
1563         ui_set_selected ( ui_selected );
1564         // rescan the dir
1565         if ( g_categories [ ui_category ] -> fspath ) {
1566           category_fs_restock ( g_categories [ ui_category ] );
1567         }
1568         // redraw the grid
1569         render_mask |= CHANGED_EVERYTHING;
1570         ui_event++;
1571
1572       } else if ( event.key.keysym.sym == SDLK_LALT ) { // start button
1573         ui_push_exec();
1574         ui_event++;
1575
1576       } else if ( event.key.keysym.sym == SDLK_LCTRL /*LALT*/ ) { // select button
1577         char *opts [ 20 ] = {
1578           "Toggle view icons<->list",
1579           "Reveal hidden category",
1580           "Shutdown Pandora",
1581           "Configure Minimenu",
1582           "Manage custom app categories",
1583           "Rescan for applications",
1584           "Cache previews to SD now",
1585           "Run a terminal/console",
1586           "Run another GUI (xfce, etc)",
1587           "Quit (<- beware)",
1588           "Select a Minimenu skin",
1589           "About Minimenu"
1590         };
1591         int sel = ui_modal_single_menu ( opts, 12, "Minimenu", "Enter to select; other to return." );
1592
1593         char buffer [ 100 ];
1594         if ( sel == 0 ) {
1595           if ( ui_viewmode == uiv_list ) {
1596             ui_viewmode = uiv_icons;
1597           } else {
1598             ui_viewmode = uiv_list;
1599           }
1600         } else if ( sel == 1 ) {
1601           // do nothing
1602           ui_revealscreen();
1603         } else if ( sel == 2 ) {
1604           // store conf on exit, so that last app/cat can be cached.. for ED :)
1605           conf_write ( g_conf, conf_determine_location ( g_conf ) );
1606           // shutdown
1607           sprintf ( buffer, "sudo poweroff" );
1608           system ( buffer );
1609         } else if ( sel == 3 ) {
1610           // configure mm
1611           unsigned char restart = conf_run_menu ( NULL );
1612           conf_write ( g_conf, conf_determine_location ( g_conf ) );
1613           conf_write ( g_conf, CONF_PREF_TEMPPATH );
1614           if ( restart ) {
1615             emit_and_quit ( MM_RESTART );
1616           }
1617         } else if ( sel == 4 ) {
1618           // manage custom categories
1619           ui_manage_categories();
1620         } else if ( sel == 5 ) {
1621           // rescan apps
1622           pnd_log ( pndn_debug, "Freeing up applications\n" );
1623           applications_free();
1624           pnd_log ( pndn_debug, "Rescanning applications\n" );
1625           applications_scan();
1626         } else if ( sel == 6 ) {
1627           // cache preview to SD now
1628           extern pnd_box_handle g_active_apps;
1629           pnd_box_handle h = g_active_apps;
1630
1631           unsigned int maxwidth, maxheight;
1632           maxwidth = pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 200 );
1633           maxheight = pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 180 );
1634
1635           pnd_disco_t *iter = pnd_box_get_head ( h );
1636
1637           while ( iter ) {
1638
1639             // cache it
1640             if ( ! cache_preview ( iter, maxwidth, maxheight ) ) {
1641               pnd_log ( pndn_debug, "Force cache: Couldn't load preview pic: '%s' -> '%s'\n",
1642                         IFNULL(iter->title_en,"No Name"), iter -> preview_pic1 );
1643             }
1644
1645             // next
1646             iter = pnd_box_get_next ( iter );
1647           } // while
1648
1649         } else if ( sel == 7 ) {
1650           // run terminal
1651           char *argv[5];
1652           argv [ 0 ] = pnd_conf_get_as_char ( g_conf, "utility.terminal" );
1653           argv [ 1 ] = NULL;
1654
1655           if ( argv [ 0 ] ) {
1656             ui_forkexec ( argv );
1657           }
1658
1659         } else if ( sel == 8 ) {
1660           char buffer [ PATH_MAX ];
1661           sprintf ( buffer, "%s %s\n", MM_RUN, "/usr/pandora/scripts/op_switchgui.sh" );
1662           emit_and_quit ( buffer );
1663         } else if ( sel == 9 ) {
1664           emit_and_quit ( MM_QUIT );
1665         } else if ( sel == 10 ) {
1666           // select skin
1667           if ( ui_pick_skin() ) {
1668             emit_and_quit ( MM_RESTART );
1669           }
1670         } else if ( sel == 11 ) {
1671           // about
1672           char buffer [ PATH_MAX ];
1673           sprintf ( buffer, "%s/about.txt", g_skinpath );
1674           ui_aboutscreen ( buffer );
1675         }
1676
1677         ui_event++;
1678         render_mask |= CHANGED_EVERYTHING;
1679
1680       } else {
1681         // unknown SDLK_ keycode?
1682
1683         // many SDLK_keycodes map to ASCII ("a" is ascii(a)), so try to jump to a filename of that name, in this category?
1684         // and if already there, try to jump to next, maybe?
1685         // future: look for sequence typing? ie: user types 'm' then 'a', look for 'ma*' instead of 'm' then 'a' matching
1686         if ( isalpha ( event.key.keysym.sym ) && g_categories [ ui_category ] -> refcount > 0 ) {
1687           mm_appref_t *app = g_categories [ ui_category ] -> refs;
1688
1689           //fprintf ( stderr, "sel %s next %s\n", ui_selected -> ref -> title_en, ui_selected -> next -> ref -> title_en );
1690
1691           // are we already matching the same char? and next item is also same char?
1692           if ( app && ui_selected && ui_selected -> next &&
1693                ui_selected -> ref -> title_en && ui_selected -> next -> ref -> title_en &&
1694                toupper ( ui_selected -> ref -> title_en [ 0 ] ) == toupper ( ui_selected -> next -> ref -> title_en [ 0 ] ) &&
1695                toupper ( ui_selected -> ref -> title_en [ 0 ] ) == toupper ( event.key.keysym.sym )
1696              )
1697           {
1698             // just skip down one
1699             app = ui_selected -> next;
1700           } else {
1701
1702             // walk the category, looking for a first-char match
1703             while ( app ) {
1704               if ( app -> ref -> title_en && toupper ( app -> ref -> title_en [ 0 ] ) == toupper ( event.key.keysym.sym ) ) {
1705                 break;
1706               }
1707               app = app -> next;
1708             }
1709
1710           } // same start letter, or new run?
1711
1712           // found something, or no?
1713           if ( app ) {
1714             // looks like we found a potential match; try switching it to visible selection
1715             ui_selected = app;
1716             ui_set_selected ( ui_selected );
1717             ui_event++;
1718           }
1719
1720
1721         } // SDLK_alphanumeric?
1722
1723       } // SDLK_....
1724
1725       // extras
1726 #if 1
1727       if ( event.key.keysym.sym == SDLK_ESCAPE ) {
1728         emit_and_quit ( MM_QUIT );
1729       }
1730 #endif
1731
1732       break;
1733 #endif
1734
1735 #if 1 // mouse / touchscreen
1736 #if 0
1737     case SDL_MOUSEBUTTONDOWN:
1738       if ( event.button.button == SDL_BUTTON_LEFT ) {
1739         cb_pointer_press ( gc, event.button.x / g_scale, event.button.y / g_scale );
1740         ui_event++;
1741       }
1742       break;
1743 #endif
1744
1745     case SDL_MOUSEBUTTONUP:
1746       if ( event.button.button == SDL_BUTTON_LEFT ) {
1747         ui_touch_act ( event.button.x, event.button.y );
1748         ui_event++;
1749       }
1750       break;
1751 #endif
1752
1753     case SDL_QUIT:
1754       exit ( 0 );
1755       break;
1756
1757     default:
1758       break;
1759
1760     } // switch event type
1761
1762   } // while poll
1763
1764   return;
1765 }
1766
1767 void ui_push_left ( unsigned char forcecoil ) {
1768
1769   if ( ui_viewmode == uiv_list ) {
1770     ui_context_t *c = &ui_display_context;
1771     int i;
1772     int imax = ( c -> cell_height * c -> row_max ) / ( c -> text_height + c -> icon_offset_y ); // visible rows
1773     for ( i = 0; i < imax; i++ ) {
1774       ui_push_up();
1775     }
1776     return;
1777   }
1778
1779   if ( ! ui_selected ) {
1780     ui_push_right ( 0 );
1781     return;
1782   }
1783
1784   // what column we in?
1785   unsigned int col = ui_determine_screen_col ( ui_selected );
1786
1787   // are we already at first item?
1788   if ( forcecoil == 0 &&
1789        pnd_conf_get_as_int_d ( g_conf, "grid.wrap_horiz_samerow", 0 ) &&
1790        col == 0 )
1791   {
1792     unsigned int i = ui_display_context.col_max - 1;
1793     while ( i && ui_selected -> next ) {
1794       ui_push_right ( 0 );
1795       i--;
1796     }
1797
1798   } else if ( g_categories [ ui_category ] -> refs == ui_selected ) {
1799     // can't go any more left, we're at the head
1800
1801   } else {
1802     // figure out the previous item; yay for singly linked list :/
1803     mm_appref_t *i = g_categories [ ui_category ] -> refs;
1804     while ( i ) {
1805       if ( i -> next == ui_selected ) {
1806         ui_selected = i;
1807         break;
1808       }
1809       i = i -> next;
1810     }
1811   }
1812
1813   ui_set_selected ( ui_selected );
1814
1815   return;
1816 }
1817
1818 void ui_push_right ( unsigned char forcecoil ) {
1819
1820   if ( ui_viewmode == uiv_list ) {
1821     ui_context_t *c = &ui_display_context;
1822     int i;
1823     int imax = ( c -> cell_height * c -> row_max ) / ( c -> text_height + c -> icon_offset_y ); // visible rows
1824     for ( i = 0; i < imax; i++ ) {
1825       ui_push_down();
1826     }
1827     return;
1828   }
1829
1830   if ( ui_selected ) {
1831
1832     // what column we in?
1833     unsigned int col = ui_determine_screen_col ( ui_selected );
1834
1835     // wrap same or no?
1836     if ( forcecoil == 0 &&
1837          pnd_conf_get_as_int_d ( g_conf, "grid.wrap_horiz_samerow", 0 ) &&
1838          // and selected is far-right, or last icon in category (might not be far right)
1839          ( ( col == ui_display_context.col_max - 1 ) ||
1840            ( ui_selected -> next == NULL ) )
1841        )
1842     {
1843       // same wrap
1844       //unsigned int i = pnd_conf_get_as_int_d ( g_conf, "grid.col_max", 5 ) - 1;
1845       while ( col /*i*/ ) {
1846         ui_push_left ( 0 );
1847         col--; //i--;
1848       }
1849
1850     } else {
1851       // just go to the next
1852
1853       if ( ui_selected -> next ) {
1854         ui_selected = ui_selected -> next;
1855       }
1856
1857     }
1858
1859   } else {
1860     ui_selected = g_categories [ ui_category ] -> refs;
1861   }
1862
1863   ui_set_selected ( ui_selected );
1864
1865   return;
1866 }
1867
1868 void ui_push_up ( void ) {
1869
1870   unsigned char col_max = ui_display_context.col_max;
1871
1872   // not selected? well, no where to go..
1873   if ( ! ui_selected ) {
1874     return;
1875   }
1876
1877   if ( ui_viewmode == uiv_list ) {
1878
1879     if ( g_categories [ ui_category ] -> refs == ui_selected ) {
1880       // can't go any more up, we're at the head
1881
1882     } else {
1883       // figure out the previous item; yay for singly linked list :/
1884       mm_appref_t *i = g_categories [ ui_category ] -> refs;
1885       while ( i ) {
1886         if ( i -> next == ui_selected ) {
1887           ui_selected = i;
1888           break;
1889         }
1890         i = i -> next;
1891       }
1892
1893     }
1894
1895     // for list view.. we're done
1896     ui_set_selected ( ui_selected );
1897     return;
1898   }
1899
1900   // what row we in?
1901   unsigned int row = ui_determine_row ( ui_selected );
1902
1903   if ( row == 0 &&
1904        pnd_conf_get_as_int_d ( g_conf, "grid.wrap_vert_stop", 0 ) == 0 )
1905   {
1906     // wrap around instead
1907
1908     unsigned int col = ui_determine_screen_col ( ui_selected );
1909
1910     // go to end
1911     ui_selected = g_categories [ ui_category ] -> refs;
1912     while ( ui_selected -> next ) {
1913       ui_selected = ui_selected -> next;
1914     }
1915
1916     // try to move to same column
1917     unsigned int newcol = ui_determine_screen_col ( ui_selected );
1918     if ( newcol > col ) {
1919       col = newcol - col;
1920       while ( col ) {
1921         ui_push_left ( 0 );
1922         col--;
1923       }
1924     }
1925
1926     // scroll down to show it
1927     int r = ui_determine_row ( ui_selected ) - 1;
1928     if ( r - ui_display_context.row_max > 0 ) {
1929       ui_rows_scrolled_down = (unsigned int) r;
1930     }
1931
1932   } else {
1933     // stop at top/bottom
1934
1935     while ( col_max ) {
1936       ui_push_left ( 1 );
1937       col_max--;
1938     }
1939
1940   }
1941
1942   return;
1943 }
1944
1945 void ui_push_down ( void ) {
1946
1947   if ( ui_viewmode == uiv_list ) {
1948
1949     if ( ui_selected ) {
1950
1951       if ( ui_selected -> next ) {
1952         ui_selected = ui_selected -> next;
1953       }
1954
1955     } else {
1956       ui_selected = g_categories [ ui_category ] -> refs; // frist!
1957     }
1958
1959     // list view? we're done.
1960     ui_set_selected ( ui_selected );
1961     return;
1962   }
1963
1964   unsigned char col_max = ui_display_context.col_max;
1965
1966   if ( ui_selected ) {
1967
1968     // what row we in?
1969     unsigned int row = ui_determine_row ( ui_selected );
1970
1971     // max rows?
1972     unsigned int icon_rows = g_categories [ ui_category ] -> refcount / col_max;
1973     if ( g_categories [ ui_category ] -> refcount % col_max > 0 ) {
1974       icon_rows++;
1975     }
1976
1977     // we at the end?
1978     if ( row == ( icon_rows - 1 ) &&
1979          pnd_conf_get_as_int_d ( g_conf, "grid.wrap_vert_stop", 0 ) == 0 )
1980     {
1981
1982       unsigned char col = ui_determine_screen_col ( ui_selected );
1983
1984       ui_selected = g_categories [ ui_category ] -> refs;
1985
1986       while ( col ) {
1987         ui_selected = ui_selected -> next;
1988         col--;
1989       }
1990
1991       ui_rows_scrolled_down = 0;
1992
1993       render_mask |= CHANGED_EVERYTHING;
1994
1995     } else {
1996
1997       while ( col_max ) {
1998         ui_push_right ( 1 );
1999         col_max--;
2000       }
2001
2002     }
2003
2004   } else {
2005     ui_push_right ( 0 );
2006   }
2007
2008   return;
2009 }
2010
2011 // 'backup' is currently 'X', for going back up in a folder/subcat without having to hit exec on the '..' entry
2012 void ui_push_backup ( void ) {
2013
2014   // a subcat-as-dir, or a dir browser?
2015   if ( g_categories [ ui_category] -> fspath ) {
2016     // dir browser, just climb our way back up
2017
2018     // go up
2019     char *c;
2020
2021     // lop off last word; if the thing ends with /, lop that one, then the next word.
2022     while ( ( c = strrchr ( g_categories [ ui_category] -> fspath, '/' ) ) ) {
2023       *c = '\0'; // lop off the last hunk
2024       if ( *(c+1) != '\0' ) {
2025         break;
2026       }
2027     } // while
2028
2029     // nothing left?
2030     if ( g_categories [ ui_category] -> fspath [ 0 ] == '\0' ) {
2031       free ( g_categories [ ui_category] -> fspath );
2032       g_categories [ ui_category] -> fspath = strdup ( "/" );
2033     }
2034
2035   } else {
2036     // a pnd subcat .. are we in one, or at the 'top'?
2037     char *pcatname = g_categories [ ui_category ] -> parent_catname;
2038
2039     if ( ! pcatname ) {
2040       return; // we're at the 'top' already
2041     }
2042
2043     // set to first cat!
2044     ui_category = 0;
2045
2046     // republish cats .. shoudl just be the one
2047     category_publish ( CFNORMAL, NULL );
2048
2049     if ( pcatname ) {
2050       ui_category = category_index ( pcatname );
2051
2052       // ensure tab visible?
2053       unsigned int tab_width = pnd_conf_get_as_int ( g_conf, "tabs.tab_width" );
2054       if ( ui_category > ui_catshift + ( ui_display_context.screen_width / tab_width ) - 1 ) {
2055         ui_catshift = ui_category - ( ui_display_context.screen_width / tab_width ) + 1;
2056       }
2057
2058     }
2059
2060   } // dir or subcat?
2061
2062   return;
2063 }
2064
2065 void ui_push_exec ( void ) {
2066
2067   if ( ! ui_selected ) {
2068     return;
2069   }
2070
2071   // cache the category/app so we can come back to it another time
2072   if ( ui_selected ) {
2073     pnd_conf_set_char ( g_conf, "minimenu.last_known_app_uid", ui_selected -> ref -> unique_id );
2074   }
2075   if ( g_categories [ 0 ] ) {
2076     pnd_conf_set_char ( g_conf, "minimenu.last_known_catname", g_categories [ ui_category ] -> catname );
2077
2078     // and also the parent cat..
2079     if ( g_categories [ ui_category ] -> parent_catname ) {
2080       pnd_conf_set_char ( g_conf, "minimenu.last_known_parentcatname", g_categories [ ui_category ] -> parent_catname );
2081     } else {
2082       char *kv = pnd_box_find_by_key ( g_conf, "minimenu.last_known_parentcatname" );
2083       if ( kv ) {
2084         pnd_box_delete_node ( g_conf, kv );
2085       }
2086
2087     }
2088   }
2089
2090   // cache last known cat/app to /tmp, so we can use it again later
2091   conf_write ( g_conf, CONF_PREF_TEMPPATH );
2092
2093   // was this icon generated from filesystem, or from pnd-file?
2094   if ( ui_selected -> ref -> object_flags & PND_DISCO_GENERATED ) {
2095
2096     if ( ! ui_selected -> ref -> title_en ) {
2097       return; // no filename
2098     }
2099
2100     if ( ui_selected -> ref -> object_type == pnd_object_type_directory ) {
2101
2102       // check if this guy is a dir-browser tab, or is a directory on a pnd tab
2103       if ( ! g_categories [ ui_category] -> fspath ) {
2104         // pnd subcat as dir
2105
2106         // are we already in a subcat? if so, go back to parent; there is no grandparenting or deeper
2107         if ( g_categories [ ui_category ] -> parent_catname ) {
2108           // go back up
2109           ui_push_backup();
2110
2111         } else {
2112           // delve into subcat
2113
2114           // set to first cat!
2115           ui_category = 0;
2116           ui_catshift = 0;
2117
2118           // republish cats .. shoudl just be the one
2119           category_publish ( CFBYNAME, ui_selected -> ref -> object_path );
2120
2121         }
2122
2123         // forget the selection, nolonger applies
2124         ui_selected = NULL;
2125         ui_set_selected ( ui_selected );
2126         // redraw the grid
2127         render_mask |= CHANGED_EVERYTHING;
2128
2129       } else {
2130
2131         // delve up/down the dir tree
2132         if ( strcmp ( ui_selected -> ref -> title_en, ".." ) == 0 ) {
2133           ui_push_backup();
2134
2135         } else {
2136           // go down
2137           char *temp = malloc ( strlen ( g_categories [ ui_category] -> fspath ) + strlen ( ui_selected -> ref -> title_en ) + 1 + 1 );
2138           sprintf ( temp, "%s/%s", g_categories [ ui_category] -> fspath, ui_selected -> ref -> title_en );
2139           free ( g_categories [ ui_category] -> fspath );
2140           g_categories [ ui_category] -> fspath = temp;
2141         }
2142
2143         pnd_log ( pndn_debug, "Cat %s is now in path %s\n", g_categories [ ui_category ] -> catname, g_categories [ ui_category ]-> fspath );
2144
2145         // forget the selection, nolonger applies
2146         ui_selected = NULL;
2147         ui_set_selected ( ui_selected );
2148         // rescan the dir
2149         category_fs_restock ( g_categories [ ui_category ] );
2150         // redraw the grid
2151         render_mask |= CHANGED_SELECTION;
2152
2153       } // directory browser or pnd subcat?
2154
2155     } else {
2156       // just run it arbitrarily?
2157
2158       // if this a pnd-file, or just some executable?
2159       if ( strcasestr ( ui_selected -> ref -> object_filename, PND_PACKAGE_FILEEXT ) ) {
2160         // looks like a pnd, now what do we do..
2161         pnd_box_handle h = pnd_disco_file ( ui_selected -> ref -> object_path, ui_selected -> ref -> object_filename );
2162
2163         if ( h ) {
2164           pnd_disco_t *d = pnd_box_get_head ( h );
2165           pnd_apps_exec_disco ( pnd_run_script, d, PND_EXEC_OPTION_NORUN, NULL );
2166           char buffer [ PATH_MAX ];
2167           sprintf ( buffer, "%s %s\n", MM_RUN, pnd_apps_exec_runline() );
2168           if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.live_on_run", 0 ) == 0 ) {
2169             emit_and_quit ( buffer );
2170           } else {
2171             emit_and_run ( buffer );
2172           }
2173         }
2174
2175       } else {
2176         // random bin file
2177
2178         // is it even executable? if we don't have handlers for non-executables yet (Jan 2011 we don't),
2179         // then don't even try to run things not-flagged as executable.. but wait most people are on
2180         // FAT filesystems, what a drag, we can't tell at the fs level.
2181         // ... but we can still invoke 'file' and grep out the good bits, at least.
2182         //
2183         // open a stream reading 'file /path/to/file' and check output for 'executable'
2184         // -- not checking for "ARM" so it can pick up x86 (or whatever native) executables in build environment
2185         unsigned char is_executable = 0;
2186
2187         // popen test
2188         {
2189           char popenbuf [ FILENAME_MAX ];
2190           snprintf ( popenbuf, FILENAME_MAX, "%s %s/%s", MIMETYPE_EXE, g_categories [ ui_category ] -> fspath, ui_selected -> ref -> title_en );
2191
2192           FILE *marceau;
2193           if ( ! ( marceau = popen ( popenbuf, "r" ) ) ) {
2194             return; // error, we need some useful error handling and dialog boxes here
2195           }
2196
2197           if ( fgets ( popenbuf, FILENAME_MAX, marceau ) ) {
2198             //printf ( "File test returns: %s\n", popenbuf );
2199             if ( strstr ( popenbuf, "executable" ) != NULL ) {
2200               is_executable = 1;
2201             }
2202           }
2203
2204           pclose ( marceau );
2205
2206         } // popen test
2207
2208         if ( ! is_executable ) {
2209           fprintf ( stderr, "ERROR: File to invoke is not executable, skipping. (%s)\n", ui_selected -> ref -> title_en );
2210           return; // need some error handling here
2211         }
2212
2213 #if 0 // eat up any pending SDL events and toss 'em?
2214         {
2215           SDL_PumpEvents();
2216           SDL_Event e;
2217           while ( SDL_PeepEvents ( &e, 1, SDL_GETEVENT, SDL_ALLEVENTS ) > 0 ) {
2218             // spin
2219           }
2220         }
2221 #endif
2222
2223 #if 1
2224         // just exec it
2225         //
2226
2227         // get CWD so we can restore it on return
2228         char cwd [ PATH_MAX ];
2229         getcwd ( cwd, PATH_MAX );
2230
2231         // full path to executable so we don't rely on implicit "./"
2232         char execbuf [ FILENAME_MAX ];
2233         snprintf ( execbuf, FILENAME_MAX, "%s/%s", g_categories [ ui_category ] -> fspath, ui_selected -> ref -> title_en );
2234
2235         // do it!
2236         chdir ( g_categories [ ui_category ] -> fspath );
2237         exec_raw_binary ( execbuf /*ui_selected -> ref -> title_en*/ );
2238         chdir ( cwd );
2239 #else
2240         // DEPRECATED / NOT TESTED
2241         // get mmwrapper to run it
2242         char buffer [ PATH_MAX ];
2243         sprintf ( buffer, "%s %s/%s\n", MM_RUN, g_categories [ ui_category ] -> fspath, ui_selected -> ref -> title_en );
2244         if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.live_on_run", 0 ) == 0 ) {
2245           emit_and_quit ( buffer );
2246         } else {
2247           emit_and_run ( buffer );
2248         }
2249 #endif
2250       } // pnd or bin?
2251
2252     } // dir or file?
2253
2254   } else {
2255
2256     // set app-run speed
2257     int use_run_speed = pnd_conf_get_as_int_d ( g_conf, "minimenu.use_run_speed", 0 );
2258     if ( use_run_speed > 0 ) {
2259       int mm_speed = pnd_conf_get_as_int_d ( g_conf, "minimenu.run_speed", -1 );
2260       if ( mm_speed > -1 ) {
2261         char buffer [ 512 ];
2262         snprintf ( buffer, 500, "sudo /usr/pandora/scripts/op_cpuspeed.sh %d", mm_speed );
2263         system ( buffer );
2264       }
2265     } // do speed change?
2266
2267     // request app to run and quit mmenu
2268     pnd_apps_exec_disco ( pnd_run_script, ui_selected -> ref, PND_EXEC_OPTION_NORUN, NULL );
2269     char buffer [ PATH_MAX ];
2270     sprintf ( buffer, "%s %s\n", MM_RUN, pnd_apps_exec_runline() );
2271     if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.live_on_run", 0 ) == 0 ) {
2272       emit_and_quit ( buffer );
2273     } else {
2274       emit_and_run ( buffer );
2275     }
2276   }
2277
2278   return;
2279 }
2280
2281 void ui_push_ltrigger ( void ) {
2282   unsigned char oldcat = ui_category;
2283   unsigned int screen_width = ui_display_context.screen_width;
2284   unsigned int tab_width = pnd_conf_get_as_int ( g_conf, "tabs.tab_width" );
2285
2286   if ( g_categorycount == 0 ) {
2287     return;
2288   }
2289
2290   if ( g_icon_thread_busy ) {
2291     ui_stop_defered_icon_thread();
2292   }
2293
2294   if ( ui_category > 0 ) {
2295     ui_category--;
2296     category_fs_restock ( g_categories [ ui_category ] );
2297   } else {
2298     if ( pnd_conf_get_as_int_d ( g_conf, "tabs.wraparound", 0 ) > 0 ) {
2299       ui_category = g_categorycount - 1;
2300       ui_catshift = 0;
2301       if ( ui_category >= ( screen_width / tab_width ) ) {
2302         ui_catshift = ui_category - ( screen_width / tab_width ) + 1;
2303       }
2304       category_fs_restock ( g_categories [ ui_category ] );
2305     }
2306   }
2307
2308   if ( oldcat != ui_category ) {
2309     ui_selected = NULL;
2310     ui_set_selected ( ui_selected );
2311   }
2312
2313   // make tab visible?
2314   if ( ui_catshift > 0 && ui_category == ui_catshift - 1 ) {
2315     ui_catshift--;
2316   }
2317
2318   // unscroll
2319   ui_rows_scrolled_down = 0;
2320
2321   render_mask |= CHANGED_CATEGORY;
2322   ui_start_defered_icon_thread();
2323
2324   return;
2325 }
2326
2327 void ui_push_rtrigger ( void ) {
2328   unsigned char oldcat = ui_category;
2329
2330   if ( g_categorycount == 0 ) {
2331     return;
2332   }
2333
2334   if ( g_icon_thread_busy ) {
2335     ui_stop_defered_icon_thread();
2336   }
2337
2338   unsigned int screen_width = ui_display_context.screen_width;
2339   unsigned int tab_width = pnd_conf_get_as_int ( g_conf, "tabs.tab_width" );
2340
2341   if ( ui_category < ( g_categorycount - 1 ) ) {
2342     ui_category++;
2343     category_fs_restock ( g_categories [ ui_category ] );
2344   } else {
2345     if ( pnd_conf_get_as_int_d ( g_conf, "tabs.wraparound", 0 ) > 0 ) {
2346       ui_category = 0;
2347       ui_catshift = 0;
2348       category_fs_restock ( g_categories [ ui_category ] );
2349     }
2350   }
2351
2352   if ( oldcat != ui_category ) {
2353     ui_selected = NULL;
2354     ui_set_selected ( ui_selected );
2355   }
2356
2357   // make tab visible?
2358   if ( ui_category > ui_catshift + ( screen_width / tab_width ) - 1 ) {
2359     ui_catshift++;
2360   }
2361
2362   // unscroll
2363   ui_rows_scrolled_down = 0;
2364
2365   render_mask |= CHANGED_CATEGORY;
2366   ui_start_defered_icon_thread();
2367
2368   return;
2369 }
2370
2371 SDL_Surface *ui_scale_image ( SDL_Surface *s, unsigned int maxwidth, int maxheight ) {
2372   double scale = 1000000.0;
2373   double scalex = 1000000.0;
2374   double scaley = 1000000.0;
2375   SDL_Surface *scaled;
2376
2377   scalex = (double)maxwidth / (double)s -> w;
2378
2379   if ( maxheight == -1 ) {
2380     scale = scalex;
2381   } else {
2382     scaley = (double)maxheight / (double)s -> h;
2383
2384     if ( scaley < scalex ) {
2385       scale = scaley;
2386     } else {
2387       scale = scalex;
2388     }
2389
2390   }
2391
2392   scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
2393   SDL_FreeSurface ( s );
2394   s = scaled;
2395
2396   return ( s );
2397 }
2398
2399 void ui_loadscreen ( void ) {
2400
2401   SDL_Rect dest;
2402
2403   // clear the screen
2404   SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
2405
2406   // render text
2407   SDL_Surface *rtext;
2408   rtext = TTF_RenderText_Blended ( g_big_font, "Setting up menu...", ui_display_context.fontcolor );
2409   dest.x = 20;
2410   dest.y = 20;
2411   SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, &dest );
2412   SDL_UpdateRects ( sdl_realscreen, 1, &dest );
2413   SDL_FreeSurface ( rtext );
2414
2415   return;
2416 }
2417
2418 void ui_discoverscreen ( unsigned char clearscreen ) {
2419
2420   SDL_Rect dest;
2421
2422   // clear the screen
2423   if ( clearscreen ) {
2424     SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
2425
2426     // render background
2427     if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
2428       dest.x = 0;
2429       dest.y = 0;
2430       dest.w = sdl_realscreen -> w;
2431       dest.h = sdl_realscreen -> h;
2432       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, NULL /* whole image */, sdl_realscreen, NULL /* 0,0 */ );
2433       SDL_UpdateRects ( sdl_realscreen, 1, &dest );
2434     }
2435
2436   }
2437
2438   // render text
2439   SDL_Surface *rtext;
2440   rtext = TTF_RenderText_Blended ( g_big_font, "Looking for applications...", ui_display_context.fontcolor );
2441   if ( clearscreen ) {
2442     dest.x = 20;
2443     dest.y = 20;
2444   } else {
2445     dest.x = 20;
2446     dest.y = 40;
2447   }
2448   SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, &dest );
2449   SDL_UpdateRects ( sdl_realscreen, 1, &dest );
2450   SDL_FreeSurface ( rtext );
2451
2452   // render icon
2453   if ( g_imagecache [ IMG_ICON_MISSING ].i ) {
2454     dest.x = rtext -> w + 30;
2455     dest.y = 20;
2456     SDL_BlitSurface ( g_imagecache [ IMG_ICON_MISSING ].i, NULL, sdl_realscreen, &dest );
2457     SDL_UpdateRects ( sdl_realscreen, 1, &dest );
2458   }
2459
2460   return;
2461 }
2462
2463 void ui_cachescreen ( unsigned char clearscreen, char *filename ) {
2464
2465   SDL_Rect rects [ 4 ];
2466   SDL_Rect *dest = rects;
2467   SDL_Rect src;
2468   bzero ( dest, sizeof(SDL_Rect)* 4 );
2469
2470   unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
2471   unsigned int font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
2472   unsigned int font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
2473   unsigned int font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
2474
2475   static unsigned int stepx = 0;
2476
2477   // clear the screen
2478   if ( clearscreen ) {
2479     SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
2480
2481     // render background
2482     if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
2483       dest -> x = 0;
2484       dest -> y = 0;
2485       dest -> w = sdl_realscreen -> w;
2486       dest -> h = sdl_realscreen -> h;
2487       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, NULL /* whole image */, sdl_realscreen, NULL /* 0,0 */ );
2488       dest++;
2489     }
2490
2491   } else {
2492
2493     // render background
2494     if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
2495       src.x = 0;
2496       src.y = 0;
2497       src.w = sdl_realscreen -> w;
2498       src.h = 100;
2499       dest -> x = 0;
2500       dest -> y = 0;
2501       dest -> w = sdl_realscreen -> w;
2502       dest -> h = sdl_realscreen -> h;
2503       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, &src, sdl_realscreen, dest );
2504       dest++;
2505     }
2506
2507   } // clear it
2508
2509   // render text
2510   SDL_Surface *rtext;
2511   SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
2512   rtext = TTF_RenderText_Blended ( g_big_font, "Caching applications artwork...", tmpfontcolor );
2513   dest -> x = 20;
2514   dest -> y = 20;
2515   SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
2516   SDL_FreeSurface ( rtext );
2517   dest++;
2518
2519   // render icon
2520   if ( g_imagecache [ IMG_ICON_MISSING ].i ) {
2521     dest -> x = rtext -> w + 30 + stepx;
2522     dest -> y = 20;
2523     SDL_BlitSurface ( g_imagecache [ IMG_ICON_MISSING ].i, NULL, sdl_realscreen, dest );
2524     dest++;
2525   }
2526
2527   // filename
2528   if ( filename ) {
2529     rtext = TTF_RenderText_Blended ( g_tab_font, filename, tmpfontcolor );
2530     dest -> x = 20;
2531     dest -> y = 50;
2532     SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
2533     SDL_FreeSurface ( rtext );
2534     dest++;
2535   }
2536
2537   // move across
2538   stepx += 20;
2539
2540   if ( stepx > 350 ) {
2541     stepx = 0;
2542   }
2543
2544   SDL_UpdateRects ( sdl_realscreen, dest - rects, rects );
2545
2546   return;
2547 }
2548
2549 int ui_selected_index ( void ) {
2550
2551   if ( ! ui_selected ) {
2552     return ( -1 ); // no index
2553   }
2554
2555   mm_appref_t *r = g_categories [ ui_category ] -> refs;
2556   int counter = 0;
2557   while ( r ) {
2558     if ( r == ui_selected ) {
2559       return ( counter );
2560     }
2561     r = r -> next;
2562     counter++;
2563   }
2564
2565   return ( -1 );
2566 }
2567
2568 static mm_appref_t *timer_ref = NULL;
2569 void ui_set_selected ( mm_appref_t *r ) {
2570
2571   render_mask |= CHANGED_SELECTION;
2572
2573   // preview pic stuff
2574   //
2575
2576   if ( ! pnd_conf_get_as_int_d ( g_conf, "minimenu.load_previews_later", 0 ) ) {
2577     return; // no desire to defer anything
2578   }
2579
2580   if ( ! r ) {
2581     // cancel timer
2582     SDL_SetTimer ( 0, NULL );
2583     timer_ref = NULL;
2584     return;
2585   }
2586
2587   SDL_SetTimer ( pnd_conf_get_as_int_d ( g_conf, "previewpic.defer_timer_ms", 1000 ), ui_callback_f );
2588   timer_ref = r;
2589
2590   return;
2591 }
2592
2593 unsigned int ui_callback_f ( unsigned int t ) {
2594
2595   if ( ui_selected != timer_ref ) {
2596     return ( 0 ); // user has moved it, who cares
2597   }
2598
2599   SDL_Event e;
2600   bzero ( &e, sizeof(SDL_Event) );
2601   e.type = SDL_USEREVENT;
2602   e.user.code = sdl_user_ticker;
2603   SDL_PushEvent ( &e );
2604
2605   return ( 0 );
2606 }
2607
2608 int ui_modal_single_menu ( char *argv[], unsigned int argc, char *title, char *footer ) {
2609   SDL_Rect rects [ 40 ];
2610   SDL_Rect *dest = rects;
2611   SDL_Rect src;
2612   SDL_Surface *rtext;
2613   unsigned char max_visible = pnd_conf_get_as_int_d ( g_conf, "detailtext.max_visible" , 11 );
2614   unsigned char first_visible = 0;
2615
2616   bzero ( rects, sizeof(SDL_Rect) * 40 );
2617
2618   unsigned int sel = 0;
2619
2620   SDL_Color selfontcolor = { 0/*font_rgba_r*/, ui_display_context.font_rgba_g, ui_display_context.font_rgba_b, ui_display_context.font_rgba_a };
2621
2622   unsigned int i;
2623   SDL_Event event;
2624
2625   while ( 1 ) {
2626
2627     // clear
2628     dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
2629     dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
2630     dest -> w = ((SDL_Surface*) g_imagecache [ IMG_DETAIL_PANEL ].i) -> w;
2631     dest -> h = ((SDL_Surface*) g_imagecache [ IMG_DETAIL_PANEL ].i) -> h;
2632     SDL_FillRect( sdl_realscreen, dest, 0 );
2633
2634     // show dialog background
2635     if ( g_imagecache [ IMG_DETAIL_BG ].i ) {
2636       src.x = 0;
2637       src.y = 0;
2638       src.w = ((SDL_Surface*)(g_imagecache [ IMG_DETAIL_BG ].i)) -> w;
2639       src.h = ((SDL_Surface*)(g_imagecache [ IMG_DETAIL_BG ].i)) -> h;
2640       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
2641       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
2642       SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
2643       // repeat for darken?
2644       //SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
2645       //SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
2646       //SDL_UpdateRects ( sdl_realscreen, 1, &dest );
2647       dest++;
2648     }
2649
2650     // show dialog frame
2651     if ( g_imagecache [ IMG_DETAIL_PANEL ].i ) {
2652       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
2653       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
2654       SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_PANEL ].i, NULL /* whole image */, sdl_realscreen, dest );
2655       //SDL_UpdateRects ( sdl_realscreen, 1, &dest );
2656       dest++;
2657     }
2658
2659     // show header
2660     if ( title ) {
2661       rtext = TTF_RenderText_Blended ( g_tab_font, title, ui_display_context.fontcolor );
2662       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 ) + 20;
2663       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 ) + 20;
2664       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
2665       SDL_FreeSurface ( rtext );
2666       dest++;
2667     }
2668
2669     // show footer
2670     if ( footer ) {
2671       rtext = TTF_RenderText_Blended ( g_tab_font, footer, ui_display_context.fontcolor );
2672       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 ) + 20;
2673       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 ) +
2674         ((SDL_Surface*) g_imagecache [ IMG_DETAIL_PANEL ].i) -> h
2675         - 60;
2676       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
2677       SDL_FreeSurface ( rtext );
2678       dest++;
2679     }
2680
2681     // show options
2682     for ( i = first_visible; i < first_visible + max_visible && i < argc; i++ ) {
2683
2684       // show options
2685       if ( sel == i ) {
2686         rtext = TTF_RenderText_Blended ( g_tab_font, argv [ i ], selfontcolor );
2687       } else {
2688         rtext = TTF_RenderText_Blended ( g_tab_font, argv [ i ], ui_display_context.fontcolor );
2689       }
2690       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 ) + 20;
2691       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 ) + 40 + ( 20 * ( i + 1 - first_visible ) );
2692       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
2693       SDL_FreeSurface ( rtext );
2694       dest++;
2695
2696     } // for
2697
2698     // update all the rects and send it all to sdl
2699     SDL_UpdateRects ( sdl_realscreen, dest - rects, rects );
2700     dest = rects;
2701
2702     // check for input
2703     while ( SDL_WaitEvent ( &event ) ) {
2704
2705       switch ( event.type ) {
2706
2707       //case SDL_KEYUP:
2708       case SDL_KEYDOWN:
2709
2710         if ( event.key.keysym.sym == SDLK_UP ) {
2711           if ( sel ) {
2712             sel--;
2713
2714             if ( sel < first_visible ) {
2715               first_visible--;
2716             }
2717
2718           }
2719         } else if ( event.key.keysym.sym == SDLK_DOWN ) {
2720
2721           if ( sel < argc - 1 ) {
2722             sel++;
2723
2724             // ensure visibility
2725             if ( sel >= first_visible + max_visible ) {
2726               first_visible++;
2727             }
2728
2729           }
2730
2731         } else if ( event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_END ) { // return, or "B"
2732           return ( sel );
2733
2734 #if 0
2735         } else if ( event.key.keysym.sym == SDLK_q ) {
2736           exit ( 0 );
2737 #endif
2738
2739         } else {
2740           return ( -1 ); // nada
2741         }
2742
2743         break;
2744
2745       } // switch
2746
2747       break;
2748     } // while
2749
2750   } // while
2751
2752   return ( -1 );
2753 }
2754
2755 unsigned char ui_determine_row ( mm_appref_t *a ) {
2756   unsigned int row = 0;
2757
2758   mm_appref_t *i = g_categories [ ui_category ] -> refs;
2759   while ( i != a ) {
2760     i = i -> next;
2761     row++;
2762   } // while
2763   row /= ui_display_context.col_max;
2764
2765   return ( row );
2766 }
2767
2768 unsigned char ui_determine_screen_row ( mm_appref_t *a ) {
2769   return ( ui_determine_row ( a ) % ui_display_context.row_max );
2770 }
2771
2772 unsigned char ui_determine_screen_col ( mm_appref_t *a ) {
2773   unsigned int col = 0;
2774
2775   mm_appref_t *i = g_categories [ ui_category ] -> refs;
2776   while ( i != a ) {
2777     i = i -> next;
2778     col++;
2779   } // while
2780   col %= ui_display_context.col_max;
2781
2782   return ( col );
2783 }
2784
2785 unsigned char ui_show_info ( char *pndrun, pnd_disco_t *p ) {
2786   char *viewer, *searchpath;
2787   pnd_conf_handle desktoph;
2788
2789   // viewer
2790   searchpath = pnd_conf_query_searchpath();
2791
2792   desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, searchpath );
2793
2794   if ( ! desktoph ) {
2795     return ( 0 );
2796   }
2797
2798   viewer = pnd_conf_get_as_char ( desktoph, "info.viewer" );
2799
2800   if ( ! viewer ) {
2801     return ( 0 ); // no way to view the file
2802   }
2803
2804   // etc
2805   if ( ! p -> unique_id ) {
2806     return ( 0 );
2807   }
2808
2809   if ( ! p -> info_filename ) {
2810     return ( 0 );
2811   }
2812
2813   if ( ! p -> info_name ) {
2814     return ( 0 );
2815   }
2816
2817   if ( ! pndrun ) {
2818     return ( 0 );
2819   }
2820
2821   // exec line
2822   char args [ 1001 ];
2823   char *pargs = args;
2824   if ( pnd_conf_get_as_char ( desktoph, "info.viewer_args" ) ) {
2825     snprintf ( pargs, 1001, "%s %s",
2826                pnd_conf_get_as_char ( desktoph, "info.viewer_args" ), p -> info_filename );
2827   } else {
2828     pargs = NULL;
2829   }
2830
2831   char pndfile [ 1024 ];
2832   if ( p -> object_type == pnd_object_type_directory ) {
2833     // for PXML-app-dir, pnd_run.sh doesn't want the PXML.xml.. it just wants the dir-name
2834     strncpy ( pndfile, p -> object_path, 1000 );
2835   } else if ( p -> object_type == pnd_object_type_pnd ) {
2836     // pnd_run.sh wants the full path and filename for the .pnd file
2837     snprintf ( pndfile, 1020, "%s/%s", p -> object_path, p -> object_filename );
2838   }
2839
2840   if ( ! pnd_apps_exec ( pndrun, pndfile, p -> unique_id, viewer, p -> startdir, pargs,
2841                          p -> clockspeed ? atoi ( p -> clockspeed ) : 0, PND_EXEC_OPTION_NORUN ) )
2842   {
2843     return ( 0 );
2844   }
2845
2846   pnd_log ( pndn_debug, "Info Exec=%s\n", pnd_apps_exec_runline() );
2847
2848   // try running it
2849   int x;
2850   if ( ( x = fork() ) < 0 ) {
2851     pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
2852     return ( 0 );
2853   }
2854
2855   if ( x == 0 ) {
2856     execl ( "/bin/sh", "/bin/sh", "-c", pnd_apps_exec_runline(), (char*)NULL );
2857     pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", pnd_apps_exec_runline() );
2858     return ( 0 );
2859   }
2860
2861   return ( 1 );
2862 }
2863
2864 typedef struct {
2865   SDL_Rect r;
2866   int catnum;
2867   mm_appref_t *ref;
2868 } ui_touch_t;
2869 #define MAXTOUCH 100
2870 ui_touch_t ui_touchrects [ MAXTOUCH ];
2871 unsigned char ui_touchrect_count = 0;
2872
2873 void ui_register_reset ( void ) {
2874   bzero ( ui_touchrects, sizeof(ui_touch_t)*MAXTOUCH );
2875   ui_touchrect_count = 0;
2876   return;
2877 }
2878
2879 void ui_register_tab ( unsigned char catnum, unsigned int x, unsigned int y, unsigned int w, unsigned int h ) {
2880
2881   if ( ui_touchrect_count == MAXTOUCH ) {
2882     return;
2883   }
2884
2885   ui_touchrects [ ui_touchrect_count ].r.x = x;
2886   ui_touchrects [ ui_touchrect_count ].r.y = y;
2887   ui_touchrects [ ui_touchrect_count ].r.w = w;
2888   ui_touchrects [ ui_touchrect_count ].r.h = h;
2889   ui_touchrects [ ui_touchrect_count ].catnum = catnum;
2890   ui_touchrect_count++;
2891
2892   return;
2893 }
2894
2895 void ui_register_app ( mm_appref_t *app, unsigned int x, unsigned int y, unsigned int w, unsigned int h ) {
2896
2897   if ( ui_touchrect_count == MAXTOUCH ) {
2898     return;
2899   }
2900
2901   ui_touchrects [ ui_touchrect_count ].r.x = x;
2902   ui_touchrects [ ui_touchrect_count ].r.y = y;
2903   ui_touchrects [ ui_touchrect_count ].r.w = w;
2904   ui_touchrects [ ui_touchrect_count ].r.h = h;
2905   ui_touchrects [ ui_touchrect_count ].ref = app;
2906   ui_touchrect_count++;
2907
2908   return;
2909 }
2910
2911 void ui_touch_act ( unsigned int x, unsigned int y ) {
2912
2913   unsigned char i;
2914   ui_touch_t *t;
2915
2916   for ( i = 0; i < ui_touchrect_count; i++ ) {
2917     t = &(ui_touchrects [ i ]);
2918
2919     if ( x >= t -> r.x &&
2920          x <= t -> r.x + t -> r.w &&
2921          y >= t -> r.y &&
2922          y <= t -> r.y + t -> r.h
2923        )
2924     {
2925
2926       if ( t -> ref ) {
2927         ui_selected = t -> ref;
2928         ui_push_exec();
2929       } else {
2930         if ( ui_category != t -> catnum ) {
2931           ui_selected = NULL;
2932         }
2933         ui_category = t -> catnum;
2934         render_mask |= CHANGED_CATEGORY;
2935         // rescan the dir
2936         category_fs_restock ( g_categories [ ui_category ] );
2937         ui_start_defered_icon_thread();
2938       }
2939
2940       break;
2941     }
2942
2943   } // for
2944
2945   return;
2946 }
2947
2948 unsigned char ui_forkexec ( char *argv[] ) {
2949   char *fooby = argv[0];
2950   int x;
2951
2952   if ( ( x = fork() ) < 0 ) {
2953     pnd_log ( pndn_error, "ERROR: Couldn't fork() for '%s'\n", fooby );
2954     return ( 0 );
2955   }
2956
2957   if ( x == 0 ) { // child
2958     execv ( fooby, argv );
2959     pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", fooby );
2960     return ( 0 );
2961   }
2962
2963   // parent, success
2964   return ( 1 );
2965 }
2966
2967 unsigned char ui_threaded_timer_create ( void ) {
2968
2969   g_timer_thread = SDL_CreateThread ( (void*)ui_threaded_timer, NULL );
2970
2971   if ( ! g_timer_thread ) {
2972     pnd_log ( pndn_error, "ERROR: Couldn't create timer thread\n" );
2973     return ( 0 );
2974   }
2975
2976   return ( 1 );
2977 }
2978
2979 int ui_threaded_timer ( pnd_disco_t *p ) {
2980
2981   // this timer's job is to ..
2982   // - do nothing for quite awhile
2983   // - on wake, post event to SDL event queue, so that main thread will check if SD insert/eject occurred
2984   // - goto 10
2985
2986   unsigned int delay_s = 2; // seconds
2987
2988   while ( 1 ) {
2989
2990     // pause...
2991     //sleep ( delay_s );
2992     SDL_Delay ( delay_s * 1000 );
2993
2994     // .. trigger SD check
2995     SDL_Event e;
2996     bzero ( &e, sizeof(SDL_Event) );
2997     e.type = SDL_USEREVENT;
2998     e.user.code = sdl_user_checksd;
2999     SDL_PushEvent ( &e );
3000
3001   } // while
3002
3003   return ( 0 );
3004 }
3005
3006 unsigned char ui_threaded_defered_preview ( pnd_disco_t *p ) {
3007
3008   if ( ! cache_preview ( p, pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 200 ),
3009                          pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 180 ) )
3010      )
3011   {
3012     pnd_log ( pndn_debug, "THREAD: Couldn't load preview pic: '%s' -> '%s'\n",
3013               IFNULL(p->title_en,"No Name"), p -> preview_pic1 );
3014   }
3015
3016   // trigger that we completed
3017   SDL_Event e;
3018   bzero ( &e, sizeof(SDL_Event) );
3019   e.type = SDL_USEREVENT;
3020   e.user.code = sdl_user_finishedpreview;
3021   e.user.data1 = p;
3022   SDL_PushEvent ( &e );
3023
3024   return ( 0 );
3025 }
3026
3027 void ui_post_scan ( void ) {
3028
3029   // reset view
3030   ui_selected = NULL;
3031   ui_rows_scrolled_down = 0;
3032   // set back to first tab, to be safe
3033   ui_category = 0;
3034   ui_catshift = 0;
3035
3036   // do we have a preferred category to jump to? or a last known one?
3037   char *dc = pnd_conf_get_as_char ( g_conf, "categories.default_cat" );
3038   char *lastcat = pnd_conf_get_as_char ( g_conf, "minimenu.last_known_catname" );
3039   if ( ( dc ) ||
3040        ( pnd_conf_get_as_int_d ( g_conf, "minimenu.start_selected", 0 ) && lastcat ) )
3041   {
3042     char *catpick = NULL;
3043
3044     if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.start_selected", 0 ) && lastcat ) {
3045       catpick = lastcat;
3046
3047       // if this is a subcat, we have some doctoring to do :/ the hackishness is really
3048       // starting to show..
3049       if ( pnd_conf_get_as_char ( g_conf, "minimenu.last_known_parentcatname" ) ) {
3050         // in subcat view, we only have one cat
3051         ui_category = 0;
3052         ui_catshift = 0;
3053         // the cat name to search for is Child*Parent
3054         char key [ 512 ];
3055         sprintf ( key, "%s*%s",
3056                   pnd_conf_get_as_char ( g_conf, "minimenu.last_known_catname" )
3057                   , pnd_conf_get_as_char ( g_conf, "minimenu.last_known_parentcatname" ) );
3058         category_publish ( CFBYNAME, key );
3059         // since we forced it by hand, no need to do a cat-scan below
3060         catpick = NULL;
3061       }
3062
3063     } else if ( dc ) {
3064       catpick = dc;
3065     }
3066
3067     // attempt to find default cat; if we do find it, select it; otherwise
3068     // default behaviour will pick first cat (ie: usually All)
3069     if ( catpick ) {
3070       unsigned int i;
3071
3072       for ( i = 0; i < g_categorycount; i++ ) {
3073         if ( strcasecmp ( g_categories [ i ] -> catname, catpick ) == 0 ) {
3074           ui_category = i;
3075           // ensure visibility
3076           unsigned int screen_width = ui_display_context.screen_width;
3077           unsigned int tab_width = pnd_conf_get_as_int ( g_conf, "tabs.tab_width" );
3078           if ( ui_category > ui_catshift + ( screen_width / tab_width ) - 1 ) {
3079             ui_catshift = ui_category - ( screen_width / tab_width ) + 1;
3080           }
3081           break;
3082         }
3083       }
3084
3085       if ( i == g_categorycount ) {
3086         pnd_log ( pndn_warning, "  User defined default category or last known cat '%s' but not found, so using default behaviour\n", catpick );
3087       }
3088
3089     } // cat change?
3090
3091   } // default cat
3092
3093   // if we're sent right to a dirbrowser tab, restock it now (normally we restock on entry)
3094   if ( g_categories [ ui_category ] && g_categories [ ui_category ] -> fspath ) {
3095     printf ( "Restock on start: '%s'\n", g_categories [ ui_category ] -> fspath );
3096     category_fs_restock ( g_categories [ ui_category ] );
3097   }
3098
3099   // redraw all
3100   render_mask |= CHANGED_EVERYTHING;
3101
3102   // if deferred icon load, kick off the thread now
3103   if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_icons_later", 0 ) == 1 ) {
3104     ui_start_defered_icon_thread();
3105   } // deferred icon load
3106
3107   return;
3108 }
3109
3110 unsigned char ui_threaded_defered_icon ( void *p ) {
3111   extern pnd_box_handle g_active_apps;
3112
3113   unsigned char maxwidth, maxheight;
3114   maxwidth = pnd_conf_get_as_int_d ( g_conf, "grid.icon_max_width", 50 );
3115   maxheight = pnd_conf_get_as_int_d ( g_conf, "grid.icon_max_height", 50 );
3116
3117   pnd_disco_t *iter;
3118
3119   g_icon_thread_busy = 1;
3120
3121   // work at it in order within current category
3122
3123   mm_appref_t *refiter = g_categories [ ui_category ] ? g_categories [ ui_category ] -> refs : NULL;
3124   while ( refiter && ! g_icon_thread_stop ) {
3125     iter = refiter -> ref;
3126
3127     // has an icon that is not already cached?
3128     if ( ( iter -> pnd_icon_pos ) ||
3129          ( iter -> icon && iter -> object_flags & PND_DISCO_LIBPND_DD )
3130        )
3131     {
3132   
3133       // try to cache it?
3134       if ( ! cache_icon ( iter, maxwidth, maxheight ) ) {
3135         //pnd_log ( pndn_warning, "  Couldn't load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
3136
3137       } else {
3138
3139         // trigger that we completed
3140         SDL_Event e;
3141         bzero ( &e, sizeof(SDL_Event) );
3142         e.type = SDL_USEREVENT;
3143         e.user.code = sdl_user_finishedicon;
3144         SDL_PushEvent ( &e );
3145
3146         //pnd_log ( pndn_warning, "  Finished deferred load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
3147         //usleep ( pnd_conf_get_as_int_d ( g_conf, "minimenu.defer_icon_us", 50000 ) );
3148
3149       }
3150
3151       // avoid churn
3152       iter -> pnd_icon_pos = 0;
3153       if ( iter -> icon ) {
3154         free ( iter -> icon );
3155         iter -> icon = NULL;
3156       }
3157
3158       // let user do something..
3159       SDL_Delay ( 200 );
3160
3161     } // has icon
3162
3163     refiter = refiter -> next;
3164   }
3165
3166   // mark as done
3167   g_icon_thread_busy = 0;
3168
3169   return ( 0 );
3170 }
3171
3172 void ui_show_hourglass ( unsigned char updaterect ) {
3173
3174   SDL_Rect dest;
3175   SDL_Surface *s = g_imagecache [ IMG_HOURGLASS ].i;
3176
3177   dest.x = ( 800 - s -> w ) / 2;
3178   dest.y = ( 480 - s -> h ) / 2;
3179
3180   SDL_BlitSurface ( s, NULL /* whole image */, sdl_realscreen, &dest );
3181
3182   if ( updaterect ) {
3183     SDL_UpdateRects ( sdl_realscreen, 1, &dest );
3184   }
3185
3186   return;
3187 }
3188
3189 unsigned char ui_pick_skin ( void ) {
3190 #define MAXSKINS 10
3191   char *skins [ MAXSKINS ];
3192   unsigned char iter;
3193
3194   char *searchpath = pnd_conf_get_as_char ( g_conf, "minimenu.skin_searchpath" );
3195   char tempname [ 100 ];
3196
3197   iter = 0;
3198
3199   skins [ iter++ ] = "No skin change";
3200
3201   SEARCHPATH_PRE
3202   {
3203     DIR *d = opendir ( buffer );
3204
3205     if ( d ) {
3206       struct dirent *dd;
3207
3208       while ( ( dd = readdir ( d ) ) ) {
3209
3210         if ( dd -> d_name [ 0 ] == '.' ) {
3211           // ignore
3212         } else if ( ( dd -> d_type == DT_DIR || dd -> d_type == DT_UNKNOWN ) &&
3213                     iter < MAXSKINS )
3214         {
3215           snprintf ( tempname, 100, "Skin: %s", dd -> d_name );
3216           skins [ iter++ ] = strdup ( tempname );
3217         }
3218
3219       }
3220
3221       closedir ( d );
3222     }
3223
3224   }
3225   SEARCHPATH_POST
3226
3227   int sel = ui_modal_single_menu ( skins, iter, "Skins", "Enter to select; other to return." );
3228
3229   // did they pick one?
3230   if ( sel > 0 ) {
3231     FILE *f;
3232
3233     char *s = strdup ( pnd_conf_get_as_char ( g_conf, "minimenu.skin_selected" ) );
3234     s = pnd_expand_tilde ( s );
3235
3236     f = fopen ( s, "w" );
3237
3238     free ( s );
3239
3240     if ( f ) {
3241       fprintf ( f, "%s\n", skins [ sel ] + 6 );
3242       fclose ( f );
3243     }
3244
3245     return ( 1 );
3246   }
3247
3248   return ( 0 );
3249 }
3250
3251 void ui_aboutscreen ( char *textpath ) {
3252 #define PIXELW 7
3253 #define PIXELH 7
3254 #define MARGINW 3
3255 #define MARGINH 3
3256 #define SCRW 800
3257 #define SCRH 480
3258 #define ROWS SCRH / ( PIXELH + MARGINH )
3259 #define COLS SCRW / ( PIXELW + MARGINW )
3260
3261   unsigned char pixelboard [ ROWS * COLS ]; // pixel heat
3262   bzero ( pixelboard, ROWS * COLS );
3263
3264   SDL_Surface *rtext;
3265   SDL_Rect r;
3266
3267   SDL_Color rtextc = { 200, 200, 200, 100 };
3268
3269   // pixel scroller
3270   char *textloop [ 500 ];
3271   unsigned int textmax = 0;
3272   bzero ( textloop, 500 * sizeof(char*) );
3273
3274   // cursor scroller
3275   char cbuffer [ 50000 ];
3276   bzero ( cbuffer, 50000 );
3277   unsigned int crevealed = 0;
3278
3279   FILE *f = fopen ( textpath, "r" );
3280
3281   if ( ! f ) {
3282     pnd_log ( pndn_error, "ERROR: Couldn't open about text: %s\n", textpath );
3283     return;
3284   }
3285
3286   char textbuf [ 100 ];
3287   while ( fgets ( textbuf, 100, f ) ) {
3288
3289     // add to full buffer
3290     strncat ( cbuffer, textbuf, 50000 );
3291
3292     // chomp
3293     if ( strchr ( textbuf, '\n' ) ) {
3294       * strchr ( textbuf, '\n' ) = '\0';
3295     }
3296
3297     // add to pixel loop
3298     if ( 1||textbuf [ 0 ] ) {
3299       textloop [ textmax ] = strdup ( textbuf );
3300       textmax++;
3301     }
3302
3303   } // while fgets
3304
3305   fclose ( f );
3306
3307   unsigned int textiter = 0;
3308   while ( textiter < textmax ) {
3309     char *text = textloop [ textiter ];
3310
3311     rtext = NULL;
3312     if ( text [ 0 ] ) {
3313       // render to surface
3314       rtext = TTF_RenderText_Blended ( g_grid_font, text, rtextc );
3315
3316       // render font to pixelboard
3317       unsigned int px, py;
3318       unsigned char *ph;
3319       unsigned int *pixels = rtext -> pixels;
3320       unsigned char cr, cg, cb, ca;
3321       for ( py = 0; py < rtext -> h; py ++ ) {
3322         for ( px = 0; px < ( rtext -> w > COLS ? COLS : rtext -> w ); px++ ) {
3323
3324           SDL_GetRGBA ( pixels [ ( py * rtext -> pitch / 4 ) + px ],
3325                         rtext -> format, &cr, &cg, &cb, &ca );
3326
3327           if ( ca != 0 ) {
3328
3329             ph = pixelboard + ( /*y offset */ 30 * COLS ) + ( py * COLS ) + px /* / 2 */;
3330
3331             if ( *ph < 100 ) {
3332               *ph = 100;
3333             }
3334
3335             ca /= 5;
3336             if ( *ph + ca < 250 ) {
3337               *ph += ca;
3338             }
3339
3340           } // got a pixel?
3341
3342         } // x
3343       } // y
3344
3345     } // got text?
3346
3347     unsigned int runcount = 10;
3348     while ( runcount-- ) {
3349
3350       // clear display
3351       SDL_FillRect( sdl_realscreen, NULL /* whole */, 0 );
3352
3353       // render pixelboard
3354       unsigned int x, y;
3355       unsigned int c;
3356       for ( y = 0; y < ROWS; y++ ) {
3357         for ( x = 0; x < COLS; x++ ) {
3358
3359           if ( 1||pixelboard [ ( y * COLS ) + x ] ) {
3360
3361             // position
3362             r.x = x * ( PIXELW + MARGINW );
3363             r.y = y * ( PIXELH + MARGINH );
3364             r.w = PIXELW;
3365             r.h = PIXELH;
3366             // heat -> colour
3367             c = SDL_MapRGB ( sdl_realscreen -> format, 100 /* r */, 0 /* g */, pixelboard [ ( y * COLS ) + x ] );
3368             // render
3369             SDL_FillRect( sdl_realscreen, &r /* whole */, c );
3370
3371           }
3372
3373         } // x
3374       } // y
3375
3376       // cool pixels
3377       unsigned char *pc = pixelboard;
3378       for ( y = 0; y < ROWS; y++ ) {
3379         for ( x = 0; x < COLS; x++ ) {
3380
3381           if ( *pc > 10 ) {
3382             (*pc) -= 3;
3383           }
3384
3385           pc++;
3386         } // x
3387       } // y
3388
3389       // slide pixels upwards
3390       memmove ( pixelboard, pixelboard + COLS, ( COLS * ROWS ) - COLS );
3391
3392       // render actual readable text
3393       {
3394
3395         // display up to cursor
3396         SDL_Rect dest;
3397         unsigned int cdraw = 0;
3398         SDL_Surface *cs;
3399         char ctb [ 2 ];
3400
3401         if ( crevealed > 200 ) {
3402           cdraw = crevealed - 200;
3403         }
3404
3405         dest.x = 400;
3406         dest.y = 20;
3407
3408         for ( ; cdraw < crevealed; cdraw++ ) {
3409           ctb [ 0 ] = cbuffer [ cdraw ];
3410           ctb [ 1 ] = '\0';
3411           // move over or down
3412           if ( cbuffer [ cdraw ] == '\n' ) {
3413             // EOL
3414             dest.x = 400;
3415             dest.y += 14;
3416
3417             if ( dest.y > 450 ) {
3418               dest.y = 450;
3419             }
3420
3421           } else {
3422             // draw the char
3423             cs = TTF_RenderText_Blended ( g_tab_font, ctb, rtextc );
3424             if ( cs ) {
3425               SDL_BlitSurface ( cs, NULL /* all */, sdl_realscreen, &dest );
3426               SDL_FreeSurface ( cs );
3427               // over
3428               dest.x += cs -> w;
3429             }
3430           }
3431
3432         }
3433
3434         dest.w = 10;
3435         dest.h = 20;
3436         SDL_FillRect ( sdl_realscreen, &dest /* whole */, 220 );
3437
3438         // increment cursor to next character
3439         if ( cbuffer [ crevealed ] != '\0' ) {
3440           crevealed++;
3441         }
3442
3443       } // draw cursor text
3444
3445       // reveal
3446       //
3447       SDL_UpdateRect ( sdl_realscreen, 0, 0, 0, 0 ); // whole screen
3448
3449       usleep ( 50000 );
3450
3451       // any button? if so, about
3452       {
3453         SDL_PumpEvents();
3454
3455         SDL_Event e;
3456
3457         if ( SDL_PeepEvents ( &e, 1, SDL_GETEVENT, SDL_EVENTMASK(/*SDL_KEYUP|*/SDL_KEYDOWN) ) > 0 ) {
3458           return;
3459         }
3460
3461       }
3462
3463     } // while cooling
3464
3465     if ( rtext ) {
3466       SDL_FreeSurface ( rtext );
3467     }
3468
3469     textiter++;
3470   } // while more text
3471
3472   // free up
3473   unsigned int i;
3474   for ( i = 0; i < textmax; i++ ) {
3475     if ( textloop [ i ] ) {
3476       free ( textloop [ i ] );
3477       textloop [ i ] = 0;
3478     }
3479   }
3480
3481   return;
3482 }
3483
3484 void ui_revealscreen ( void ) {
3485   char *labels [ 500 ];
3486   unsigned int labelmax = 0;
3487   unsigned int i;
3488   char fulllabel [ 200 ];
3489
3490   if ( ! category_count ( CFHIDDEN ) ) {
3491     return; // nothing to do
3492   }
3493
3494   // switch to hidden categories
3495   category_publish ( CFHIDDEN, NULL );
3496
3497   // build up labels to show in menu
3498   for ( i = 0; i < g_categorycount; i++ ) {
3499
3500     if ( g_categories [ i ] -> parent_catname ) {
3501       sprintf ( fulllabel, "%s [%s]", g_categories [ i ] -> catname, g_categories [ i ] -> parent_catname );
3502     } else {
3503       sprintf ( fulllabel, "%s", g_categories [ i ] -> catname );
3504     }
3505
3506     labels [ labelmax++ ] = strdup ( fulllabel );
3507   }
3508
3509   // show menu
3510   int sel = ui_modal_single_menu ( labels, labelmax, "Temporary Category Reveal",
3511                                    "Enter to select; other to return." );
3512
3513   // if selected, try to set this guy to visible
3514   if ( sel >= 0 ) {
3515
3516     // fix up category name, if its been hacked
3517 #if 0 // prepending and .. wtf crap is this
3518     if ( strchr ( g_categories [ sel ] -> catname, '.' ) ) {
3519       char *t = g_categories [ sel ] -> catname;
3520       g_categories [ sel ] -> catname = strdup ( strchr ( g_categories [ sel ] -> catname, '.' ) + 1 );
3521       free ( t );
3522     }
3523 #endif
3524
3525     // reflag this guy to be visible
3526     g_categories [ sel ] -> catflags = CFNORMAL;
3527
3528     // switch to the new category.. cache name.
3529     char *switch_to_name = g_categories [ sel ] -> catname;
3530
3531     // republish categories
3532     category_publish ( CFNORMAL, NULL );
3533
3534     // switch to the new category.. with the cached name!
3535     ui_category = category_index ( switch_to_name );
3536
3537     // ensure visibility
3538     unsigned int screen_width = ui_display_context.screen_width;
3539     unsigned int tab_width = pnd_conf_get_as_int ( g_conf, "tabs.tab_width" );
3540     if ( ui_category > ui_catshift + ( screen_width / tab_width ) - 1 ) {
3541       ui_catshift = ui_category - ( screen_width / tab_width ) + 1;
3542     }
3543
3544   }
3545
3546   // republish categories
3547   category_publish ( CFNORMAL, NULL );
3548
3549   // redraw tabs
3550   render_mask |= CHANGED_CATEGORY;
3551   ui_start_defered_icon_thread();
3552
3553   return;
3554 }
3555
3556 void ui_recache_context ( ui_context_t *c ) {
3557
3558   c -> screen_width = pnd_conf_get_as_int_d ( g_conf, "display.screen_width", 800 );
3559
3560   c -> font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
3561   c -> font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
3562   c -> font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
3563   c -> font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
3564
3565   c -> grid_offset_x = pnd_conf_get_as_int ( g_conf, "grid.grid_offset_x" );
3566   c -> grid_offset_y = pnd_conf_get_as_int ( g_conf, "grid.grid_offset_y" );
3567
3568   c -> icon_offset_x = pnd_conf_get_as_int ( g_conf, "grid.icon_offset_x" );
3569   c -> icon_offset_y = pnd_conf_get_as_int ( g_conf, "grid.icon_offset_y" );
3570   c -> icon_max_width = pnd_conf_get_as_int ( g_conf, "grid.icon_max_width" );
3571   c -> icon_max_height = pnd_conf_get_as_int ( g_conf, "grid.icon_max_height" );
3572   c -> sel_icon_offset_x = pnd_conf_get_as_int_d ( g_conf, "grid.sel_offoffset_x", 0 );
3573   c -> sel_icon_offset_y = pnd_conf_get_as_int_d ( g_conf, "grid.sel_offoffset_y", 0 );
3574
3575   c -> text_width = pnd_conf_get_as_int ( g_conf, "grid.text_width" );
3576   c -> text_clip_x = pnd_conf_get_as_int ( g_conf, "grid.text_clip_x" );
3577   c -> text_offset_x = pnd_conf_get_as_int ( g_conf, "grid.text_offset_x" );
3578   c -> text_offset_y = pnd_conf_get_as_int ( g_conf, "grid.text_offset_y" );
3579   c -> text_hilite_offset_y = pnd_conf_get_as_int ( g_conf, "grid.text_hilite_offset_y" );
3580
3581   c -> row_max = pnd_conf_get_as_int_d ( g_conf, "grid.row_max", 4 );
3582   c -> col_max = pnd_conf_get_as_int_d ( g_conf, "grid.col_max", 5 );
3583
3584   c -> cell_width = pnd_conf_get_as_int ( g_conf, "grid.cell_width" );
3585   c -> cell_height = pnd_conf_get_as_int ( g_conf, "grid.cell_height" );
3586
3587   c -> arrow_bar_x = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_x", 450 );
3588   c -> arrow_bar_y = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_y", 100 );
3589   c -> arrow_bar_clip_w = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_clip_w", 10 );
3590   c -> arrow_bar_clip_h = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_clip_h", 100 );
3591   c -> arrow_up_x = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_up_x", 450 );
3592   c -> arrow_up_y = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_up_y", 80 );
3593   c -> arrow_down_x = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_down_x", 450 );
3594   c -> arrow_down_y = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_down_y", 80 );
3595
3596   // font colour
3597   SDL_Color tmp = { c -> font_rgba_r, c -> font_rgba_g, c -> font_rgba_b, c -> font_rgba_a };
3598   c -> fontcolor = tmp;
3599
3600   // determine font height
3601   if ( g_grid_font ) {
3602     SDL_Surface *rtext;
3603     rtext = TTF_RenderText_Blended ( g_grid_font, "M", c -> fontcolor );
3604     c -> text_height = rtext -> h;
3605   } else {
3606     c -> text_height = 10;
3607   }
3608
3609   // now that we've got 'normal' (detail pane shown) param's, lets check if detail pane
3610   // is hidden; if so, override some values with those alternate skin values where possible.
3611   if ( ui_detail_hidden ) {
3612     // if detail panel is hidden, and theme cannot support it, unhide the bloody thing. (This may help
3613     // when someone is amid theme hacking or changing.)
3614     if ( ! ui_is_detail_hideable() ) {
3615       ui_detail_hidden = 0;
3616     }
3617
3618     // still hidden?
3619     if ( ui_detail_hidden ) {
3620
3621       c -> row_max = pnd_conf_get_as_int_d ( g_conf, "grid.row_max_w", c -> row_max );
3622       c -> col_max = pnd_conf_get_as_int_d ( g_conf, "grid.col_max_w", c -> col_max );
3623
3624       c -> cell_width = pnd_conf_get_as_int_d ( g_conf, "grid.cell_width_w", c -> cell_width );
3625       c -> cell_height = pnd_conf_get_as_int_d ( g_conf, "grid.cell_height_w", c -> cell_height );
3626
3627       c -> arrow_bar_x = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_x_w", 450 );
3628       c -> arrow_bar_y = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_y_w", 100 );
3629       c -> arrow_up_x = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_up_x_w", 450 );
3630       c -> arrow_up_y = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_up_y_w", 80 );
3631       c -> arrow_down_x = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_down_x_w", 450 );
3632       c -> arrow_down_y = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_down_y_w", 80 );
3633
3634     } // if detail hidden.. still.
3635
3636   } // if detail hidden
3637
3638   return;
3639 }
3640
3641 unsigned char ui_is_detail_hideable ( void ) {
3642
3643   // if skin has a bit of info for wide-mode, we assume wide-mode is available
3644   if ( pnd_conf_get_as_char ( g_conf, "grid.row_max_w" ) != NULL ) {
3645     return ( 1 );
3646   }
3647
3648   // else not!
3649   return ( 0 );
3650 }
3651
3652 void ui_toggle_detail_pane ( void ) {
3653
3654   // no bitmask trickery here; I like it to be stand-out obvious at 3am.
3655
3656   if ( ui_detail_hidden ) {
3657     ui_detail_hidden = 0;
3658   } else {
3659     ui_detail_hidden = 1;
3660   }
3661
3662   // repull skin config
3663   ui_recache_context ( &ui_display_context );
3664
3665   // redraw
3666   render_mask |= CHANGED_EVERYTHING;
3667
3668   return;
3669 }
3670
3671 void ui_menu_context ( mm_appref_t *a ) {
3672
3673   unsigned char rescan_apps = 0;
3674   unsigned char context_alive = 1;
3675
3676   enum {
3677     context_done = 0,
3678     context_file_info,
3679     context_file_delete,
3680     context_app_info,
3681     context_app_hide,
3682     context_app_recategorize,
3683     context_app_recategorize_sub,
3684     context_app_rename,
3685     context_app_cpuspeed,
3686     context_app_run,
3687     context_app_notes1,
3688     context_app_notes2,
3689     context_app_notes3,
3690     context_menu_max
3691   };
3692
3693   char *verbiage[] = {
3694     "Done (return to grid)",      // context_done
3695     "Get info about file/dir",    // context_file_info
3696     "Delete file/dir",            // context_file_delete
3697     "Get info",                   // context_app_info
3698     "Hide application",           //             hide
3699     "Recategorize",               //             recategorize
3700     "Recategorize subcategory",   //             recategorize
3701     "Change displayed title",     //             rename
3702     "Set CPU speed for launch",   //             cpuspeed
3703     "Run application",            //             run
3704     "Edit notes line 1",          //             notes1
3705     "Edit notes line 2",          //             notes2
3706     "Edit notes line 3",          //             notes3
3707   };
3708
3709   unsigned short int menu [ context_menu_max ];
3710   char *menustring [ context_menu_max ];
3711   unsigned char menumax = 0;
3712
3713   // ++ done
3714   menu [ menumax ] = context_done; menustring [ menumax++ ] = verbiage [ context_done ];
3715
3716   // hook up appropriate menu options based on tab-type and object-type
3717   if ( g_categories [ ui_category ] -> fspath ) {
3718     return; // TBD
3719     menu [ menumax ] = context_file_info; menustring [ menumax++ ] = verbiage [ context_file_info ];
3720     menu [ menumax ] = context_file_delete; menustring [ menumax++ ] = verbiage [ context_file_delete ];
3721   } else {
3722
3723     if ( a -> ref -> object_type == pnd_object_type_directory ) {
3724       return; // don't do anything if the guy is a subcat-as-folder
3725     }
3726
3727     //menu [ menumax ] = context_app_info; menustring [ menumax++ ] = verbiage [ context_app_info ];
3728     menu [ menumax ] = context_app_run; menustring [ menumax++ ] = verbiage [ context_app_run ];
3729     menu [ menumax ] = context_app_hide; menustring [ menumax++ ] = verbiage [ context_app_hide ];
3730     menu [ menumax ] = context_app_recategorize; menustring [ menumax++ ] = verbiage [ context_app_recategorize ];
3731     menu [ menumax ] = context_app_recategorize_sub; menustring [ menumax++ ] = verbiage [ context_app_recategorize_sub ];
3732     menu [ menumax ] = context_app_rename; menustring [ menumax++ ] = verbiage [ context_app_rename ];
3733     menu [ menumax ] = context_app_cpuspeed; menustring [ menumax++ ] = verbiage [ context_app_cpuspeed ];
3734     menu [ menumax ] = context_app_notes1; menustring [ menumax++ ] = verbiage [ context_app_notes1 ];
3735     menu [ menumax ] = context_app_notes2; menustring [ menumax++ ] = verbiage [ context_app_notes2 ];
3736     menu [ menumax ] = context_app_notes3; menustring [ menumax++ ] = verbiage [ context_app_notes3 ];
3737   }
3738
3739   // operate the menu
3740   while ( context_alive ) {
3741
3742     int sel = ui_modal_single_menu ( menustring, menumax, a -> ref -> title_en ? a -> ref -> title_en : "Quickpick Menu" /* title */, "B or Enter; other to cancel." /* footer */ );
3743
3744     if ( sel < 0 ) {
3745       context_alive = 0;
3746
3747     } else {
3748
3749       switch ( menu [ sel ] ) {
3750
3751       case context_done:
3752         context_alive = 0;
3753         break;
3754
3755       case context_file_info:
3756         break;
3757
3758       case context_file_delete:
3759         //ui_menu_twoby ( "Delete - Are you sure?", "B/enter; other to cancel", "Delete", "Do not delete" );
3760         break;
3761
3762       case context_app_info:
3763         break;
3764
3765       case context_app_hide:
3766         {
3767           // determine key
3768           char confkey [ 1000 ];
3769           snprintf ( confkey, 999, "%s.%s", "appshow", a -> ref -> unique_id );
3770
3771           // turn app 'off'
3772           pnd_conf_set_char ( g_conf, confkey, "0" );
3773
3774           // write conf, so it will take next time
3775           conf_write ( g_conf, conf_determine_location ( g_conf ) );
3776           conf_write ( g_conf, CONF_PREF_TEMPPATH );
3777
3778           // can we just 'hide' this guy without reloading all apps? (this is for you, EvilDragon)
3779           if ( 0 ) {
3780             //
3781             // DOESN'T WORK YET; other parts of app are still hanging onto some values and blow up
3782             //
3783             char *uid = strdup ( a -> ref -> unique_id );
3784             unsigned int i;
3785             for ( i = 0; i < g_categorycount; i++ ) {
3786               mm_appref_t *p = g_categories [ i ] -> refs;
3787               mm_appref_t *n;
3788               while ( p ) {
3789                 n = p -> next;
3790
3791                 if ( strcmp ( p -> ref -> unique_id, uid ) == 0 ) {
3792                   free ( p );
3793                   if ( g_categories [ i ] -> refcount ) {
3794                     g_categories [ i ] -> refcount--;
3795                   }
3796                 }
3797
3798                 p = n;
3799               } // while for each appref
3800             } // for each cat/tab
3801
3802             free ( uid );
3803
3804           } else {
3805             // request rescan and wrap up
3806             rescan_apps++;
3807           }
3808
3809           context_alive = 0; // nolonger visible, so lets just get out
3810
3811         }
3812
3813         break;
3814
3815       case context_app_recategorize:
3816         {
3817           char *opts [ 250 ];
3818           unsigned char optmax = 0;
3819           unsigned char i;
3820
3821           // show custom categories
3822           if ( mmcustom_setup() ) {
3823
3824             for ( i = 0; i < mmcustom_count; i++ ) {
3825               if ( mmcustom_complete [ i ].parent_cat == NULL ) {
3826                 opts [ optmax++ ] = mmcustom_complete [ i ].cat;
3827               }
3828             }
3829
3830           }
3831
3832           // show FD categories
3833           i = 2; // skip first two - Other and NoParentCategory
3834           while ( 1 ) {
3835
3836             if ( ! freedesktop_complete [ i ].cat ) {
3837               break;
3838             }
3839
3840             if ( ! freedesktop_complete [ i ].parent_cat ) {
3841               opts [ optmax++ ] = freedesktop_complete [ i ].cat;
3842             }
3843
3844             i++;
3845           } // while
3846
3847           // picker
3848           char prompt [ 101 ];
3849           snprintf ( prompt, 100, "Pick category [%s]", a -> ref -> main_category ? a -> ref -> main_category : "NoParentCategory" );
3850
3851           int sel = ui_modal_single_menu ( opts, optmax, prompt /*"Select parent category"*/, "Enter to select; other to skip." );
3852
3853           if ( sel >= 0 ) {
3854             char confirm [ 1001 ];
3855             snprintf ( confirm, 1000, "Confirm: %s", opts [ sel ] );
3856
3857             if ( ui_menu_twoby ( confirm, "B/enter; other to cancel", "Confirm categorization", "Do not set category" ) == 1 ) {
3858               ovr_replace_or_add ( a, "maincategory", opts [ sel ] );
3859               rescan_apps++;
3860               // when changing main cat, reset subcat, otherwise you go from Game/Emu to Network/Emu and get sent to Other right away
3861               ovr_replace_or_add ( a, "maincategorysub1", freedesktop_complete [ 2 ].cat );
3862             }
3863
3864           }
3865
3866           if ( mmcustom_is_ready() ) {
3867             mmcustom_shutdown();
3868           }
3869
3870         }
3871         break;
3872
3873       case context_app_recategorize_sub:
3874         {
3875           char *opts [ 250 ];
3876           unsigned char optmax = 0;
3877           unsigned char i = 0;
3878
3879           char *whichparentarewe;
3880           if ( g_categories [ ui_category ] -> parent_catname ) {
3881             whichparentarewe = g_categories [ ui_category ] -> parent_catname;
3882           } else {
3883             whichparentarewe = g_categories [ ui_category ] -> catname;
3884           }
3885
3886           // add NoSubcategory magic one
3887           opts [ optmax++ ] = freedesktop_complete [ 2 ].cat;
3888
3889           // add custom categories
3890           if ( mmcustom_setup() ) {
3891
3892             for ( i = 0; i < mmcustom_count; i++ ) {
3893               if ( mmcustom_complete [ i ].parent_cat && strcmp ( mmcustom_complete [ i ].parent_cat, whichparentarewe ) == 0  ) {
3894                 opts [ optmax++ ] = mmcustom_complete [ i ].cat;
3895               }
3896             }
3897
3898           }
3899
3900           // add FD categories
3901           while ( 1 ) {
3902
3903             if ( ! freedesktop_complete [ i ].cat ) {
3904               break;
3905             }
3906
3907             if ( ( freedesktop_complete [ i ].parent_cat ) &&
3908                  ( strcasecmp ( freedesktop_complete [ i ].parent_cat, whichparentarewe ) == 0 )
3909                )
3910             {
3911               opts [ optmax++ ] = freedesktop_complete [ i ].cat;
3912             }
3913
3914             i++;
3915           } // while
3916
3917           char prompt [ 101 ];
3918           //snprintf ( prompt, 100, "Currently: %s", a -> ref -> main_category1 ? a -> ref -> main_category1 : "NoSubcategory" );
3919           snprintf ( prompt, 100, "%s [%s]", a -> ref -> main_category1 ? a -> ref -> main_category1 : "NoSubcategory", whichparentarewe );
3920
3921           int sel = ui_modal_single_menu ( opts, optmax, prompt /*"Select subcategory"*/, "Enter to select; other to skip." );
3922
3923           if ( sel >= 0 ) {
3924             char confirm [ 1001 ];
3925             snprintf ( confirm, 1000, "Confirm: %s", opts [ sel ] );
3926
3927             if ( ui_menu_twoby ( confirm, "B/enter; other to cancel", "Confirm sub-categorization", "Do not set sub-category" ) == 1 ) {
3928               ovr_replace_or_add ( a, "maincategorysub1", opts [ sel ] );
3929               rescan_apps++;
3930             }
3931
3932           }
3933
3934           if ( mmcustom_is_ready() ) {
3935             mmcustom_shutdown();
3936           }
3937
3938         }
3939         break;
3940
3941       case context_app_rename:
3942         {
3943           char namebuf [ 101 ];
3944           unsigned char changed;
3945
3946           changed = ui_menu_get_text_line ( "Rename application", "Use keyboard; Enter when done.",
3947                                             a -> ref -> title_en ? a -> ref -> title_en : "blank", namebuf, 30, 0 /* alphanumeric */ );
3948
3949           if ( changed ) {
3950             char confirm [ 1001 ];
3951             snprintf ( confirm, 1000, "Confirm: %s", namebuf );
3952
3953             if ( ui_menu_twoby ( confirm, "B/enter; other to cancel", "Confirm rename", "Do not rename" ) == 1 ) {
3954               ovr_replace_or_add ( a, "title", namebuf );
3955               rescan_apps++;
3956             }
3957
3958           }
3959
3960         }
3961
3962         break;
3963
3964       case context_app_cpuspeed:
3965         {
3966           char namebuf [ 101 ];
3967           unsigned char changed;
3968
3969           changed = ui_menu_get_text_line ( "Specify runspeed", "Use keyboard; Enter when done.",
3970                                             a -> ref -> clockspeed ? a -> ref -> clockspeed : "500", namebuf, 6, 1 /* numeric */ );
3971
3972           if ( changed ) {
3973             char confirm [ 1001 ];
3974             snprintf ( confirm, 1000, "Confirm: %s", namebuf );
3975
3976             if ( ui_menu_twoby ( confirm, "B/enter; other to cancel", "Confirm clockspeed", "Do not set" ) == 1 ) {
3977               ovr_replace_or_add ( a, "clockspeed", namebuf );
3978               rescan_apps++;
3979             }
3980
3981           }
3982
3983         }
3984
3985         break;
3986
3987       case context_app_run:
3988         ui_push_exec();
3989         break;
3990
3991       case context_app_notes1:
3992       case context_app_notes2:
3993       case context_app_notes3:
3994         {
3995           char namebuf [ 101 ] = "";
3996
3997           char key [ 501 ];
3998           unsigned char notenum;
3999
4000           // which note line?
4001           if ( menu [ sel ] == context_app_notes1 ) {
4002             notenum = 1;
4003           } else if ( menu [ sel ] == context_app_notes2 ) {
4004             notenum = 2;
4005           } else if ( menu [ sel ] == context_app_notes3 ) {
4006             notenum = 3;
4007           }
4008
4009           // figure out key for looking up existing, and for storing replacement
4010           snprintf ( key, 500, "Application-%u.note-%u", a -> ref -> subapp_number, notenum );
4011
4012           // do we have existing value?
4013           if ( a -> ovrh ) {
4014             char *existing = pnd_conf_get_as_char ( a -> ovrh, key );
4015             if ( existing ) {
4016               strncpy ( namebuf, existing, 100 );
4017             }
4018           }
4019
4020           unsigned char changed;
4021
4022           changed = ui_menu_get_text_line ( "Enter replacement note", "Use keyboard; Enter when done.",
4023                                             namebuf, namebuf, 30, 0 /* not-numeric-forced */ );
4024
4025           if ( changed ) {
4026             ovr_replace_or_add ( a, strchr ( key, '.' ) + 1, namebuf );
4027             rescan_apps++;
4028           }
4029
4030         }
4031         break;
4032
4033       default:
4034         return;
4035
4036       } // switch
4037
4038     } // if useful return
4039
4040   } // menu is alive?
4041
4042   // rescan apps?
4043   if ( rescan_apps ) {
4044     applications_free();
4045     applications_scan();
4046   }
4047
4048   return;
4049 }
4050
4051 unsigned char ui_menu_oneby ( char *title, char *footer, char *one ) {
4052   char *opts [ 2 ];
4053   opts [ 0 ] = one;
4054   int sel = ui_modal_single_menu ( opts, 1, title, footer );
4055   if ( sel < 0 ) {
4056     return ( 0 );
4057   }
4058   return ( sel + 1 );
4059 }
4060
4061 unsigned char ui_menu_twoby ( char *title, char *footer, char *one, char *two ) {
4062   char *opts [ 3 ];
4063   opts [ 0 ] = one;
4064   opts [ 1 ] = two;
4065   int sel = ui_modal_single_menu ( opts, 2, title, footer );
4066   if ( sel < 0 ) {
4067     return ( 0 );
4068   }
4069   return ( sel + 1 );
4070 }
4071
4072 unsigned char ui_menu_get_text_line ( char *title, char *footer, char *initialvalue,
4073                                       char *r_buffer, unsigned char maxlen, unsigned char numbersonlyp )
4074 {
4075   SDL_Rect rects [ 40 ];
4076   SDL_Rect *dest = rects;
4077   SDL_Rect src;
4078   SDL_Surface *rtext;
4079
4080   char hacktext [ 1024 ];
4081   unsigned char shifted = 0;
4082
4083   bzero ( rects, sizeof(SDL_Rect) * 40 );
4084
4085   if ( initialvalue ) {
4086     if ( initialvalue == r_buffer ) {
4087       // already good to go
4088     } else {
4089       strncpy ( r_buffer, initialvalue, maxlen );
4090     }
4091   } else {
4092     bzero ( r_buffer, maxlen );
4093   }
4094
4095   while ( 1 ) {
4096
4097     // clear
4098     dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
4099     dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
4100     dest -> w = ((SDL_Surface*) g_imagecache [ IMG_DETAIL_PANEL ].i) -> w;
4101     dest -> h = ((SDL_Surface*) g_imagecache [ IMG_DETAIL_PANEL ].i) -> h;
4102     SDL_FillRect( sdl_realscreen, dest, 0 );
4103
4104     // show dialog background
4105     if ( g_imagecache [ IMG_DETAIL_BG ].i ) {
4106       src.x = 0;
4107       src.y = 0;
4108       src.w = ((SDL_Surface*)(g_imagecache [ IMG_DETAIL_BG ].i)) -> w;
4109       src.h = ((SDL_Surface*)(g_imagecache [ IMG_DETAIL_BG ].i)) -> h;
4110       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
4111       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
4112       SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
4113       dest++;
4114     }
4115
4116     // show dialog frame
4117     if ( g_imagecache [ IMG_DETAIL_PANEL ].i ) {
4118       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
4119       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
4120       SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_PANEL ].i, NULL /* whole image */, sdl_realscreen, dest );
4121       dest++;
4122     }
4123
4124     // show header
4125     if ( title ) {
4126       rtext = TTF_RenderText_Blended ( g_tab_font, title, ui_display_context.fontcolor );
4127       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 ) + 20;
4128       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 ) + 20;
4129       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
4130       SDL_FreeSurface ( rtext );
4131       dest++;
4132     }
4133
4134     // show footer
4135     if ( footer ) {
4136       rtext = TTF_RenderText_Blended ( g_tab_font, footer, ui_display_context.fontcolor );
4137       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 ) + 20;
4138       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 ) +
4139         ((SDL_Surface*) g_imagecache [ IMG_DETAIL_PANEL ].i) -> h
4140         - 60;
4141       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
4142       SDL_FreeSurface ( rtext );
4143       dest++;
4144     }
4145
4146     // show text line - and embed cursor
4147     dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 ) + 20;
4148     dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 ) + 40 + ( 20 * ( 0/*i*/ + 1 - 0/*first_visible*/ ) );
4149
4150     strncpy ( hacktext, r_buffer, 1000 );
4151     strncat ( hacktext, "\n", 1000 ); // add [] in most fonts
4152
4153     rtext = TTF_RenderText_Blended ( g_tab_font, hacktext, ui_display_context.fontcolor );
4154     SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
4155     SDL_FreeSurface ( rtext );
4156     dest++;
4157
4158     // update all the rects and send it all to sdl
4159     SDL_UpdateRects ( sdl_realscreen, dest - rects, rects );
4160     dest = rects;
4161
4162     // check for input
4163     SDL_Event event;
4164     while ( SDL_WaitEvent ( &event ) ) {
4165
4166       switch ( event.type ) {
4167
4168       case SDL_KEYUP:
4169         if ( event.key.keysym.sym == SDLK_LSHIFT || event.key.keysym.sym == SDLK_RSHIFT ) {
4170           shifted = 0;
4171         }
4172         break;
4173
4174       case SDL_KEYDOWN:
4175
4176         if ( event.key.keysym.sym == SDLK_LEFT || event.key.keysym.sym == SDLK_BACKSPACE ) {
4177           if ( strlen ( r_buffer ) > 0 ) {
4178             char *eol = strchr ( r_buffer, '\0' );
4179             *( eol - 1 ) = '\0';
4180           }
4181
4182         } else if ( event.key.keysym.sym == SDLK_UP ) {
4183           r_buffer [ 0 ] = '\0'; // truncate!
4184
4185         } else if ( event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_END ) { // return, or "B"
4186           // on Enter/Return or B, if the buffer has 1 or more chars, we return it as valid.. otherwise, invalid.
4187           if ( strlen ( r_buffer ) > 0 ) {
4188             return ( 1 );
4189           }
4190           return ( 0 );
4191
4192         } else if ( event.key.keysym.sym == SDLK_LSHIFT || event.key.keysym.sym == SDLK_RSHIFT ) {
4193           shifted = 1;
4194
4195         } else if ( event.key.keysym.sym == SDLK_ESCAPE ||
4196                     event.key.keysym.sym == SDLK_PAGEUP ||
4197                     event.key.keysym.sym == SDLK_PAGEDOWN ||
4198                     event.key.keysym.sym == SDLK_HOME
4199                   )
4200         {
4201           return ( 0 );
4202
4203         } else {
4204
4205           if ( isprint(event.key.keysym.sym) ) {
4206
4207             unsigned char good = 1;
4208
4209             if ( numbersonlyp && ( ! isdigit(event.key.keysym.sym) ) ) {
4210               good = 0;
4211             }
4212
4213             if ( maxlen && strlen(r_buffer) >= maxlen ) {
4214               good = 0;
4215             }
4216
4217             if ( good ) {
4218               char b [ 2 ] = { '\0', '\0' };
4219               if ( shifted ) {
4220                 b [ 0 ] = toupper ( event.key.keysym.sym );
4221               } else {
4222                 b [ 0 ] = event.key.keysym.sym;
4223               }
4224               strncat ( r_buffer, b, maxlen );
4225             } // good?
4226
4227           } // printable?
4228
4229         }
4230
4231         break;
4232
4233       } // switch
4234
4235       break;
4236
4237     } // while waiting for input
4238
4239   } // while
4240
4241   return ( 0 );
4242 }
4243
4244 unsigned char ovr_replace_or_add ( mm_appref_t *a, char *keybase, char *newvalue ) {
4245   //printf ( "setting %s:%u - '%s' to '%s' - %s/%s\n", a -> ref -> title_en, a -> ref -> subapp_number, keybase, newvalue, a -> ref -> object_path, a -> ref -> object_filename );
4246
4247   char fullpath [ PATH_MAX ];
4248
4249   sprintf ( fullpath, "%s/%s", a -> ref -> object_path, a -> ref -> object_filename );
4250   char *dot = strrchr ( fullpath, '.' );
4251   if ( dot ) {
4252     sprintf ( dot, PXML_SAMEPATH_OVERRIDE_FILEEXT );
4253   } else {
4254     pnd_log ( pndn_error, "ERROR: Bad pnd-path in disco_t! %s\n", fullpath );
4255     return ( 0 );
4256   }
4257
4258   struct stat statbuf;
4259
4260   if ( stat ( fullpath, &statbuf ) == 0 ) {
4261     // file exists
4262     pnd_conf_handle h;
4263
4264     h = pnd_conf_fetch_by_path ( fullpath );
4265
4266     if ( ! h ) {
4267       return ( 0 ); // fail!
4268     }
4269
4270     char key [ 101 ];
4271     snprintf ( key, 100, "Application-%u.%s", a -> ref -> subapp_number, keybase );
4272
4273     pnd_conf_set_char ( h, key, newvalue );
4274
4275     return ( pnd_conf_write ( h, fullpath ) );
4276
4277   } else {
4278     // file needs to be created - easy!
4279
4280     FILE *f = fopen ( fullpath, "w" );
4281
4282     if ( f ) {
4283       fprintf ( f, "Application-%u.%s\t%s\n", a -> ref -> subapp_number, keybase, newvalue );
4284       fclose ( f );
4285
4286     } else {
4287       return ( 0 ); // fail!
4288     }
4289
4290   } // new or used?
4291
4292   return ( 1 );
4293 }
4294
4295 void ui_manage_categories ( void ) {
4296   unsigned char require_app_scan = 0;
4297
4298   if ( ! mmcustom_setup() ) {
4299     return; // error
4300   }
4301
4302   char *opts [ 20 ] = {
4303     "List custom categories",
4304     "List custom subcategories",
4305     "Register custom category",
4306     "Register custom subcategory",
4307     "Unregister custom category",
4308     "Unregister custom subcategory",
4309     "Done"
4310   };
4311
4312   while ( 1 ) {
4313
4314     int sel = ui_modal_single_menu ( opts, 7, "Custom Categories", "B to select; other to cancel." );
4315
4316     switch ( sel ) {
4317
4318     case 0: // list custom
4319       ui_pick_custom_category ( 0 );
4320       break;
4321
4322     case 1: // list custom sub
4323       if ( mmcustom_count ) {
4324
4325         char *maincat = ui_pick_custom_category ( 2 );
4326
4327         if ( maincat ) {
4328           unsigned int subcount = mmcustom_count_subcats ( maincat );
4329           char titlebuf [ 201 ];
4330
4331           snprintf ( titlebuf, 200, "Category: %s", maincat );
4332
4333           if ( subcount == 0 ) {
4334             ui_menu_oneby ( titlebuf, "B/Enter to accept", "Category has no subcategories." );
4335           } else {
4336
4337             char **list = malloc ( subcount * sizeof(char*) );
4338             int i;
4339             unsigned int counter = 0;
4340
4341             for ( i = 0; i < mmcustom_count; i++ ) {
4342               if ( mmcustom_complete [ i ].parent_cat && strcasecmp ( mmcustom_complete [ i ].parent_cat, maincat ) == 0 ) {
4343                 list [ counter++ ] = mmcustom_complete [ i ].cat;
4344               }
4345             }
4346
4347             ui_modal_single_menu ( list, counter, titlebuf, "Any button to exit." );
4348
4349             free ( list );
4350
4351           } // more than 0 subcats?
4352
4353         } // user picked a main cat?
4354
4355       } else {
4356         ui_menu_oneby ( "Warning", "B/Enter to accept", "There are none registered." );
4357       }
4358       break;
4359
4360     case 2: // register custom
4361       {
4362         unsigned char changed;
4363         char namebuf [ 101 ] = "";
4364
4365         changed = ui_menu_get_text_line ( "Enter unique category name", "Use keyboard; Enter when done.",
4366                                           "Pandora", namebuf, 30, 0 /* alphanumeric */ );
4367
4368         // did the user enter something?
4369         if ( changed ) {
4370
4371           // for now, force use of '*' into something else as we use * internally :/ (FIXME)
4372           {
4373             char *fixme;
4374             while ( ( fixme = strchr ( namebuf, '*' ) ) ) {
4375               *fixme = '_';
4376             }
4377           }
4378
4379           // and if so, is it existant already or not?
4380           if ( mmcustom_query ( namebuf, NULL ) ) {
4381             ui_menu_oneby ( "Warning", "B/Enter to accept", "Already a registered category." );
4382           } else if ( freedesktop_category_query ( namebuf, NULL ) ) {
4383             ui_menu_oneby ( "Warning", "B/Enter to accept", "Already a Standard category." );
4384           } else {
4385
4386             char confirm [ 1001 ];
4387             snprintf ( confirm, 1000, "Confirm: %s", namebuf );
4388
4389             if ( ui_menu_twoby ( confirm, "B/enter; other to cancel", "Confirm new category", "Do not register" ) == 1 ) {
4390               // register, save, recycle the current list
4391               mmcustom_register ( namebuf, NULL );
4392               mmcustom_write ( NULL );
4393               mmcustom_shutdown();
4394               mmcustom_setup();
4395             }
4396
4397           } // dupe?
4398
4399         } // entered something?
4400
4401       }
4402       break;
4403
4404     case 3: // register custom sub
4405       if ( 1 /*mmcustom_count -- we allow FD cats now, so this isn't applicable error */ ) {
4406
4407         char *maincat = ui_pick_custom_category ( 1 /* include FD */ );
4408
4409         if ( maincat ) {
4410           char titlebuf [ 201 ];
4411
4412           snprintf ( titlebuf, 200, "Subcat of: %s", maincat );
4413
4414           unsigned char changed;
4415           char namebuf [ 101 ] = "";
4416
4417           changed = ui_menu_get_text_line ( titlebuf, "Use keyboard; Enter when done.", "Submarine", namebuf, 30, 0 /* alphanumeric */ );
4418
4419           // did the user enter something?
4420           if ( changed ) {
4421
4422             // for now, force use of '*' into something else as we use * internally :/ (FIXME)
4423             {
4424               char *fixme;
4425               while ( ( fixme = strchr ( namebuf, '*' ) ) ) {
4426                 *fixme = '_';
4427               }
4428             }
4429
4430             // and if so, is it existant already or not?
4431             if ( mmcustom_query ( namebuf, maincat ) ) {
4432               ui_menu_oneby ( "Warning", "B/Enter to accept", "Already a subcategory." );
4433             } else if ( freedesktop_category_query ( namebuf, maincat ) ) {
4434               ui_menu_oneby ( "Warning", "B/Enter to accept", "Already a Standard subcategory." );
4435             } else {
4436
4437               char confirm [ 1001 ];
4438               snprintf ( confirm, 1000, "Confirm: %s [%s]", namebuf, maincat );
4439
4440               if ( ui_menu_twoby ( confirm, "B/enter; other to cancel", "Confirm new category", "Do not register" ) == 1 ) {
4441                 // register, save, recycle the current list
4442                 mmcustom_register ( namebuf, maincat );
4443                 mmcustom_write ( NULL );
4444                 mmcustom_shutdown();
4445                 mmcustom_setup();
4446               }
4447
4448             } // dupe?
4449
4450           } // entered something?
4451
4452         } // selected parent cat?
4453
4454       } else {
4455         ui_menu_oneby ( "Warning", "B/Enter to accept", "No categories registered." );
4456       }
4457       break;
4458
4459     case 4: // unreg custom
4460       if ( mmcustom_count ) {
4461         char *maincat = ui_pick_custom_category ( 0 );
4462
4463         if ( maincat ) {
4464           char confirm [ 1001 ];
4465           snprintf ( confirm, 1000, "Confirm remove: %s", maincat );
4466
4467           if ( ui_menu_twoby ( confirm, "B/enter; other to cancel", "Confirm unregister", "Do not unregister" ) == 1 ) {
4468             // register, save, recycle the current list
4469             mmcustom_unregister ( maincat, NULL );
4470             mmcustom_write ( NULL );
4471             mmcustom_shutdown();
4472             mmcustom_setup();
4473           }
4474
4475         } // picked?
4476
4477       } else {
4478         ui_menu_oneby ( "Warning", "B/Enter to accept", "There are none registered." );
4479       }
4480       break;
4481
4482     case 5: // unreg custom sub
4483       if ( mmcustom_count ) {
4484         char *maincat = ui_pick_custom_category ( 2 );
4485
4486         if ( maincat ) {
4487           unsigned int subcount = mmcustom_count_subcats ( maincat );
4488           char titlebuf [ 201 ];
4489
4490           snprintf ( titlebuf, 200, "Category: %s", maincat );
4491
4492           if ( subcount == 0 ) {
4493             ui_menu_oneby ( titlebuf, "B/Enter to accept", "Category has no subcategories." );
4494           } else {
4495
4496             char **list = malloc ( subcount * sizeof(char*) );
4497             int i;
4498             unsigned int counter = 0;
4499
4500             for ( i = 0; i < mmcustom_count; i++ ) {
4501               if ( mmcustom_complete [ i ].parent_cat && strcasecmp ( mmcustom_complete [ i ].parent_cat, maincat ) == 0 ) {
4502                 list [ counter++ ] = mmcustom_complete [ i ].cat;
4503               }
4504             }
4505
4506             int sel = ui_modal_single_menu ( list, counter, titlebuf, "B to selct; other to exit." );
4507
4508             if ( sel >= 0 ) {
4509               char confirm [ 1001 ];
4510               snprintf ( confirm, 1000, "Confirm remove: %s", list [ sel ] );
4511
4512               if ( ui_menu_twoby ( confirm, "B/enter; other to cancel", "Confirm unregister", "Do not unregister" ) == 1 ) {
4513                 // register, save, recycle the current list
4514                 mmcustom_unregister ( list [ sel ], maincat );
4515                 mmcustom_write ( NULL );
4516                 mmcustom_shutdown();
4517                 mmcustom_setup();
4518               }
4519
4520             } // confirm kill?
4521
4522             free ( list );
4523
4524           } // more than 0 subcats?
4525
4526         } // user picked a main cat?
4527
4528       } else {
4529         ui_menu_oneby ( "Warning", "B/Enter to accept", "There are none registered." );
4530       }
4531       break;
4532
4533     } // switch
4534
4535     // exeunt
4536     if ( sel < 0 || sel > 5 ) {
4537       break;
4538     }
4539
4540   } // while running the menu
4541
4542   // shut down custom cats
4543   mmcustom_shutdown();
4544
4545   // reload apps?
4546   if ( require_app_scan ) {
4547     applications_free();
4548     applications_scan();
4549   }
4550
4551   // redraw
4552   render_mask |= CHANGED_EVERYTHING;
4553
4554   return;
4555 }
4556
4557 // mode 0 == custom main only; 1 == custom main + FD main; 2 == custom main + FD mains-with-custom-subs
4558 char *ui_pick_custom_category ( unsigned char mode ) {
4559   char **list;
4560   int i;
4561   unsigned int counter = 0;
4562
4563   // alloc space for list, depending on scope
4564   if ( mode > 0 ) {
4565     list = malloc ( (mmcustom_count+freedesktop_count()) * sizeof(char*) );
4566   } else {
4567     list = malloc ( mmcustom_count * sizeof(char*) );
4568   }
4569
4570   // add custom mains
4571   for ( i = 0; i < mmcustom_count; i++ ) {
4572     if ( mmcustom_complete [ i ].parent_cat == NULL ) {
4573       list [ counter++ ] = mmcustom_complete [ i ].cat;
4574     }
4575   }
4576
4577   // add FD if needed
4578   if ( mode > 0 ) {
4579     i = 3;
4580
4581     while ( 1 ) {
4582
4583       if ( ! freedesktop_complete [ i ].cat ) {
4584         break;
4585       }
4586
4587       // if FD main cat
4588       if ( freedesktop_complete [ i ].parent_cat == NULL ) {
4589
4590         // mode 1 == include them all
4591         // mode 2 == include them if they have a custom subcat
4592         if ( ( mode == 1 ) ||
4593              ( mmcustom_subcount ( freedesktop_complete [ i ].cat ) ) )
4594         {
4595           list [ counter++ ] = freedesktop_complete [ i ].cat;
4596         }
4597
4598       } // if parent cat
4599
4600       i++;
4601     } // while
4602
4603   } // if
4604
4605   // we actually showing anything?
4606   if ( ! counter ) {
4607     ui_menu_oneby ( "Warning", "B/Enter to accept", "There are none registered." );
4608     return ( NULL );
4609   }
4610
4611   // do it
4612   int sel = ui_modal_single_menu ( list, counter, "Custom Categories", "Any button to exit." );
4613
4614   if ( sel < 0 ) {
4615     free ( list );
4616     return ( NULL );
4617   }
4618
4619   char *foo = list [ sel ];
4620   free ( list );
4621
4622   return ( foo );
4623 }
4624
4625 void ui_start_defered_icon_thread ( void ) {
4626
4627   if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_icons_later", 0 ) != 1 ) {
4628     return;
4629   }
4630
4631   if ( g_icon_thread_busy ) {
4632     //fprintf ( stderr, "REM: Waiting for thread to stop..\n" );
4633     ui_stop_defered_icon_thread();
4634   }
4635
4636   //fprintf ( stderr, "WARN: Starting new icon caching thread..\n" );
4637   g_icon_thread = SDL_CreateThread ( (void*)ui_threaded_defered_icon, NULL );
4638
4639   if ( ! g_icon_thread ) {
4640     pnd_log ( pndn_error, "ERROR: Couldn't create icon thread\n" );
4641   }
4642
4643   return;
4644 }
4645
4646 void ui_stop_defered_icon_thread ( void ) {
4647   time_t started = time ( NULL );
4648
4649   // ask thread to stop, then wait for it (if two run at same time, or if we change
4650   // category content under neath it, could be bad..)
4651   g_icon_thread_stop = 1;
4652   while ( g_icon_thread_busy ) {
4653     time ( NULL ); // spin
4654   }
4655   g_icon_thread_stop = 0;
4656
4657   fprintf ( stderr, "REM: Thread stoppage took %u seconds.\n", (int) ( time ( NULL ) - started ) );
4658
4659   return;
4660 }