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