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