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