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