Suppress child cats now doesn't make folders in parent cat, for cat-as-folder option...
[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         ui_event++;
1292
1293       } else if ( event.key.keysym.sym == SDLK_LALT ) { // start button
1294         ui_push_exec();
1295         ui_event++;
1296
1297       } else if ( event.key.keysym.sym == SDLK_LCTRL /*LALT*/ ) { // select button
1298         char *opts [ 20 ] = {
1299           "Reveal hidden category",
1300           "Shutdown Pandora",
1301           "Configure Minimenu",
1302           "Rescan for applications",
1303           "Cache previews to SD now",
1304           "Run a terminal/console",
1305           "Run another GUI (xfce, etc)",
1306           "Quit (<- beware)",
1307           "Select a Minimenu skin",
1308           "About Minimenu"
1309         };
1310         int sel = ui_modal_single_menu ( opts, 10, "Minimenu", "Enter to select; other to return." );
1311
1312         char buffer [ 100 ];
1313         if ( sel == 0 ) {
1314           // do nothing
1315           ui_revealscreen();
1316         } else if ( sel == 1 ) {
1317           // shutdown
1318           sprintf ( buffer, "sudo poweroff" );
1319           system ( buffer );
1320         } else if ( sel == 2 ) {
1321           // configure mm
1322           unsigned char restart = conf_run_menu ( NULL );
1323           conf_write ( g_conf, conf_determine_location ( g_conf ) );
1324           if ( restart ) {
1325             emit_and_quit ( MM_RESTART );
1326           }
1327         } else if ( sel == 3 ) {
1328           // rescan apps
1329           pnd_log ( pndn_debug, "Freeing up applications\n" );
1330           applications_free();
1331           pnd_log ( pndn_debug, "Rescanning applications\n" );
1332           applications_scan();
1333         } else if ( sel == 4 ) {
1334           // cache preview to SD now
1335           extern pnd_box_handle g_active_apps;
1336           pnd_box_handle h = g_active_apps;
1337
1338           unsigned int maxwidth, maxheight;
1339           maxwidth = pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 200 );
1340           maxheight = pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 180 );
1341
1342           pnd_disco_t *iter = pnd_box_get_head ( h );
1343
1344           while ( iter ) {
1345
1346             // cache it
1347             if ( ! cache_preview ( iter, maxwidth, maxheight ) ) {
1348               pnd_log ( pndn_debug, "Force cache: Couldn't load preview pic: '%s' -> '%s'\n",
1349                         IFNULL(iter->title_en,"No Name"), iter -> preview_pic1 );
1350             }
1351
1352             // next
1353             iter = pnd_box_get_next ( iter );
1354           } // while
1355
1356         } else if ( sel == 5 ) {
1357           // run terminal
1358           char *argv[5];
1359           argv [ 0 ] = pnd_conf_get_as_char ( g_conf, "utility.terminal" );
1360           argv [ 1 ] = NULL;
1361
1362           if ( argv [ 0 ] ) {
1363             ui_forkexec ( argv );
1364           }
1365
1366         } else if ( sel == 6 ) {
1367           char buffer [ PATH_MAX ];
1368           sprintf ( buffer, "%s %s\n", MM_RUN, "/usr/pandora/scripts/op_switchgui.sh" );
1369           emit_and_quit ( buffer );
1370         } else if ( sel == 7 ) {
1371           emit_and_quit ( MM_QUIT );
1372         } else if ( sel == 8 ) {
1373           // select skin
1374           if ( ui_pick_skin() ) {
1375             emit_and_quit ( MM_RESTART );
1376           }
1377         } else if ( sel == 9 ) {
1378           // about
1379           char buffer [ PATH_MAX ];
1380           sprintf ( buffer, "%s/about.txt", g_skinpath );
1381           ui_aboutscreen ( buffer );
1382         }
1383
1384         ui_event++;
1385         render_mask |= CHANGED_EVERYTHING;
1386
1387       } else {
1388         // unknown SDLK_ keycode?
1389
1390         // many SDLK_keycodes map to ASCII ("a" is ascii(a)), so try to jump to a filename of that name, in this category?
1391         // and if already there, try to jump to next, maybe?
1392         // future: look for sequence typing? ie: user types 'm' then 'a', look for 'ma*' instead of 'm' then 'a' matching
1393         if ( isalpha ( event.key.keysym.sym ) && g_categories [ ui_category ] -> refcount > 0 ) {
1394           mm_appref_t *app = g_categories [ ui_category ] -> refs;
1395
1396           //fprintf ( stderr, "sel %s next %s\n", ui_selected -> ref -> title_en, ui_selected -> next -> ref -> title_en );
1397
1398           // are we already matching the same char? and next item is also same char?
1399           if ( app && ui_selected &&
1400                ui_selected -> ref -> title_en && ui_selected -> next -> ref -> title_en &&
1401                toupper ( ui_selected -> ref -> title_en [ 0 ] ) == toupper ( ui_selected -> next -> ref -> title_en [ 0 ] ) &&
1402                toupper ( ui_selected -> ref -> title_en [ 0 ] ) == toupper ( event.key.keysym.sym )
1403              )
1404           {
1405             // just skip down one
1406             app = ui_selected -> next;
1407           } else {
1408
1409             // walk the category, looking for a first-char match
1410             while ( app ) {
1411               if ( app -> ref -> title_en && toupper ( app -> ref -> title_en [ 0 ] ) == toupper ( event.key.keysym.sym ) ) {
1412                 break;
1413               }
1414               app = app -> next;
1415             }
1416
1417           } // same start letter, or new run?
1418
1419           // found something, or no?
1420           if ( app ) {
1421             // looks like we found a potential match; try switching it to visible selection
1422             ui_selected = app;
1423             ui_set_selected ( ui_selected );
1424           }
1425
1426
1427
1428
1429
1430         } // SDLK_alphanumeric?
1431
1432       } // SDLK_....
1433
1434       // extras
1435 #if 1
1436       if ( event.key.keysym.sym == SDLK_ESCAPE ) {
1437         emit_and_quit ( MM_QUIT );
1438       }
1439 #endif
1440
1441       break;
1442 #endif
1443
1444 #if 1 // mouse / touchscreen
1445 #if 0
1446     case SDL_MOUSEBUTTONDOWN:
1447       if ( event.button.button == SDL_BUTTON_LEFT ) {
1448         cb_pointer_press ( gc, event.button.x / g_scale, event.button.y / g_scale );
1449         ui_event++;
1450       }
1451       break;
1452 #endif
1453
1454     case SDL_MOUSEBUTTONUP:
1455       if ( event.button.button == SDL_BUTTON_LEFT ) {
1456         ui_touch_act ( event.button.x, event.button.y );
1457         ui_event++;
1458       }
1459       break;
1460 #endif
1461
1462     case SDL_QUIT:
1463       exit ( 0 );
1464       break;
1465
1466     default:
1467       break;
1468
1469     } // switch event type
1470
1471   } // while poll
1472
1473   return;
1474 }
1475
1476 void ui_push_left ( unsigned char forcecoil ) {
1477
1478   if ( ! ui_selected ) {
1479     ui_push_right ( 0 );
1480     return;
1481   }
1482
1483   // what column we in?
1484   unsigned int col = ui_determine_screen_col ( ui_selected );
1485
1486   // are we already at first item?
1487   if ( forcecoil == 0 &&
1488        pnd_conf_get_as_int_d ( g_conf, "grid.wrap_horiz_samerow", 0 ) &&
1489        col == 0 )
1490   {
1491     unsigned int i = ui_display_context.col_max - 1;
1492     while ( i && ui_selected -> next ) {
1493       ui_push_right ( 0 );
1494       i--;
1495     }
1496
1497   } else if ( g_categories [ ui_category ] -> refs == ui_selected ) {
1498     // can't go any more left, we're at the head
1499
1500   } else {
1501     // figure out the previous item; yay for singly linked list :/
1502     mm_appref_t *i = g_categories [ ui_category ] -> refs;
1503     while ( i ) {
1504       if ( i -> next == ui_selected ) {
1505         ui_selected = i;
1506         break;
1507       }
1508       i = i -> next;
1509     }
1510   }
1511
1512   ui_set_selected ( ui_selected );
1513
1514   return;
1515 }
1516
1517 void ui_push_right ( unsigned char forcecoil ) {
1518
1519   if ( ui_selected ) {
1520
1521     // what column we in?
1522     unsigned int col = ui_determine_screen_col ( ui_selected );
1523
1524     // wrap same or no?
1525     if ( forcecoil == 0 &&
1526          pnd_conf_get_as_int_d ( g_conf, "grid.wrap_horiz_samerow", 0 ) &&
1527          // and selected is far-right, or last icon in category (might not be far right)
1528          ( ( col == ui_display_context.col_max - 1 ) ||
1529            ( ui_selected -> next == NULL ) )
1530        )
1531     {
1532       // same wrap
1533       //unsigned int i = pnd_conf_get_as_int_d ( g_conf, "grid.col_max", 5 ) - 1;
1534       while ( col /*i*/ ) {
1535         ui_push_left ( 0 );
1536         col--; //i--;
1537       }
1538
1539     } else {
1540       // just go to the next
1541
1542       if ( ui_selected -> next ) {
1543         ui_selected = ui_selected -> next;
1544       }
1545
1546     }
1547
1548   } else {
1549     ui_selected = g_categories [ ui_category ] -> refs;
1550   }
1551
1552   ui_set_selected ( ui_selected );
1553
1554   return;
1555 }
1556
1557 void ui_push_up ( void ) {
1558   unsigned char col_max = ui_display_context.col_max;
1559
1560   if ( ! ui_selected ) {
1561     return;
1562   }
1563
1564   // what row we in?
1565   unsigned int row = ui_determine_row ( ui_selected );
1566
1567   if ( row == 0 &&
1568        pnd_conf_get_as_int_d ( g_conf, "grid.wrap_vert_stop", 0 ) == 0 )
1569   {
1570     // wrap around instead
1571
1572     unsigned int col = ui_determine_screen_col ( ui_selected );
1573
1574     // go to end
1575     ui_selected = g_categories [ ui_category ] -> refs;
1576     while ( ui_selected -> next ) {
1577       ui_selected = ui_selected -> next;
1578     }
1579
1580     // try to move to same column
1581     unsigned int newcol = ui_determine_screen_col ( ui_selected );
1582     if ( newcol > col ) {
1583       col = newcol - col;
1584       while ( col ) {
1585         ui_push_left ( 0 );
1586         col--;
1587       }
1588     }
1589
1590     // scroll down to show it
1591     int r = ui_determine_row ( ui_selected ) - 1;
1592     if ( r - ui_display_context.row_max > 0 ) {
1593       ui_rows_scrolled_down = (unsigned int) r;
1594     }
1595
1596   } else {
1597     // stop at top/bottom
1598
1599     while ( col_max ) {
1600       ui_push_left ( 1 );
1601       col_max--;
1602     }
1603
1604   }
1605
1606   return;
1607 }
1608
1609 void ui_push_down ( void ) {
1610   unsigned char col_max = ui_display_context.col_max;
1611
1612   if ( ui_selected ) {
1613
1614     // what row we in?
1615     unsigned int row = ui_determine_row ( ui_selected );
1616
1617     // max rows?
1618     unsigned int icon_rows = g_categories [ ui_category ] -> refcount / col_max;
1619     if ( g_categories [ ui_category ] -> refcount % col_max > 0 ) {
1620       icon_rows++;
1621     }
1622
1623     // we at the end?
1624     if ( row == ( icon_rows - 1 ) &&
1625          pnd_conf_get_as_int_d ( g_conf, "grid.wrap_vert_stop", 0 ) == 0 )
1626     {
1627
1628       unsigned char col = ui_determine_screen_col ( ui_selected );
1629
1630       ui_selected = g_categories [ ui_category ] -> refs;
1631
1632       while ( col ) {
1633         ui_selected = ui_selected -> next;
1634         col--;
1635       }
1636
1637       ui_rows_scrolled_down = 0;
1638
1639       render_mask |= CHANGED_EVERYTHING;
1640
1641     } else {
1642
1643       while ( col_max ) {
1644         ui_push_right ( 1 );
1645         col_max--;
1646       }
1647
1648     }
1649
1650   } else {
1651     ui_push_right ( 0 );
1652   }
1653
1654   return;
1655 }
1656
1657 // 'backup' is currently 'X', for going back up in a folder/subcat without having to hit exec on the '..' entry
1658 void ui_push_backup ( void ) {
1659
1660   // a subcat-as-dir, or a dir browser?
1661   if ( g_categories [ ui_category] -> fspath ) {
1662     // dir browser, just climb our way back up
1663
1664     // go up
1665     char *c;
1666
1667     // lop off last word; if the thing ends with /, lop that one, then the next word.
1668     while ( ( c = strrchr ( g_categories [ ui_category] -> fspath, '/' ) ) ) {
1669       *c = '\0'; // lop off the last hunk
1670       if ( *(c+1) != '\0' ) {
1671         break;
1672       }
1673     } // while
1674
1675     // nothing left?
1676     if ( g_categories [ ui_category] -> fspath [ 0 ] == '\0' ) {
1677       free ( g_categories [ ui_category] -> fspath );
1678       g_categories [ ui_category] -> fspath = strdup ( "/" );
1679     }
1680
1681   } else {
1682     // a pnd subcat .. are we in one, or at the 'top'?
1683     char *pcatname = g_categories [ ui_category ] -> parent_catname;
1684
1685     if ( ! pcatname ) {
1686       return; // we're at the 'top' already
1687     }
1688
1689     // set to first cat!
1690     ui_category = 0;
1691     // republish cats .. shoudl just be the one
1692     category_publish ( CFNORMAL, NULL );
1693
1694     if ( pcatname ) {
1695       ui_category = category_index ( pcatname );
1696     }
1697
1698   } // dir or subcat?
1699
1700   return;
1701 }
1702
1703 void ui_push_exec ( void ) {
1704
1705   if ( ! ui_selected ) {
1706     return;
1707   }
1708
1709   // was this icon generated from filesystem, or from pnd-file?
1710   if ( ui_selected -> ref -> object_flags & PND_DISCO_GENERATED ) {
1711
1712     if ( ! ui_selected -> ref -> title_en ) {
1713       return; // no filename
1714     }
1715
1716     if ( ui_selected -> ref -> object_type == pnd_object_type_directory ) {
1717
1718       // check if this guy is a dir-browser tab, or is a directory on a pnd tab
1719       if ( ! g_categories [ ui_category] -> fspath ) {
1720         // pnd subcat as dir
1721
1722         // are we already in a subcat? if so, go back to parent; there is no grandparenting or deeper
1723         if ( g_categories [ ui_category ] -> parent_catname ) {
1724           // go back up
1725           ui_push_backup();
1726
1727         } else {
1728           // delve into subcat
1729
1730           // set to first cat!
1731           ui_category = 0;
1732           // republish cats .. shoudl just be the one
1733           category_publish ( CFBYNAME, ui_selected -> ref -> object_path );
1734
1735         }
1736
1737         // forget the selection, nolonger applies
1738         ui_selected = NULL;
1739         ui_set_selected ( ui_selected );
1740         // redraw the grid
1741         render_mask |= CHANGED_EVERYTHING;
1742
1743       } else {
1744
1745         // delve up/down the dir tree
1746         if ( strcmp ( ui_selected -> ref -> title_en, ".." ) == 0 ) {
1747           ui_push_backup();
1748
1749         } else {
1750           // go down
1751           char *temp = malloc ( strlen ( g_categories [ ui_category] -> fspath ) + strlen ( ui_selected -> ref -> title_en ) + 1 + 1 );
1752           sprintf ( temp, "%s/%s", g_categories [ ui_category] -> fspath, ui_selected -> ref -> title_en );
1753           free ( g_categories [ ui_category] -> fspath );
1754           g_categories [ ui_category] -> fspath = temp;
1755         }
1756
1757         pnd_log ( pndn_debug, "Cat %s is now in path %s\n", g_categories [ ui_category ] -> catname, g_categories [ ui_category ]-> fspath );
1758
1759         // forget the selection, nolonger applies
1760         ui_selected = NULL;
1761         ui_set_selected ( ui_selected );
1762         // rescan the dir
1763         category_fs_restock ( g_categories [ ui_category ] );
1764         // redraw the grid
1765         render_mask |= CHANGED_SELECTION;
1766
1767       } // directory browser or pnd subcat?
1768
1769     } else {
1770       // just run it arbitrarily?
1771
1772       // if this a pnd-file, or just some executable?
1773       if ( strcasestr ( ui_selected -> ref -> object_filename, PND_PACKAGE_FILEEXT ) ) {
1774         // looks like a pnd, now what do we do..
1775         pnd_box_handle h = pnd_disco_file ( ui_selected -> ref -> object_path, ui_selected -> ref -> object_filename );
1776
1777         if ( h ) {
1778           pnd_disco_t *d = pnd_box_get_head ( h );
1779           pnd_apps_exec_disco ( pnd_run_script, d, PND_EXEC_OPTION_NORUN, NULL );
1780           char buffer [ PATH_MAX ];
1781           sprintf ( buffer, "%s %s\n", MM_RUN, pnd_apps_exec_runline() );
1782           if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.live_on_run", 0 ) == 0 ) {
1783             emit_and_quit ( buffer );
1784           } else {
1785             emit_and_run ( buffer );
1786           }
1787         }
1788
1789       } else {
1790         // random bin file
1791
1792         // is it even executable? if we don't have handlers for non-executables yet (Jan 2011 we don't),
1793         // then don't even try to run things not-flagged as executable.. but wait most people are on
1794         // FAT filesystems, what a drag, we can't tell at the fs level.
1795         // ... but we can still invoke 'file' and grep out the good bits, at least.
1796         //
1797         // open a stream reading 'file /path/to/file' and check output for 'executable'
1798         // -- not checking for "ARM" so it can pick up x86 (or whatever native) executables in build environment
1799         unsigned char is_executable = 0;
1800
1801         // popen test
1802         {
1803           char popenbuf [ FILENAME_MAX ];
1804           snprintf ( popenbuf, FILENAME_MAX, "%s %s/%s", MIMETYPE_EXE, g_categories [ ui_category ] -> fspath, ui_selected -> ref -> title_en );
1805
1806           FILE *marceau;
1807           if ( ! ( marceau = popen ( popenbuf, "r" ) ) ) {
1808             return; // error, we need some useful error handling and dialog boxes here
1809           }
1810
1811           if ( fgets ( popenbuf, FILENAME_MAX, marceau ) ) {
1812             //printf ( "File test returns: %s\n", popenbuf );
1813             if ( strstr ( popenbuf, "executable" ) != NULL ) {
1814               is_executable = 1;
1815             }
1816           }
1817
1818           pclose ( marceau );
1819
1820         } // popen test
1821
1822         if ( ! is_executable ) {
1823           fprintf ( stderr, "ERROR: File to invoke is not executable, skipping. (%s)\n", ui_selected -> ref -> title_en );
1824           return; // need some error handling here
1825         }
1826
1827 #if 0 // eat up any pending SDL events and toss 'em?
1828         {
1829           SDL_PumpEvents();
1830           SDL_Event e;
1831           while ( SDL_PeepEvents ( &e, 1, SDL_GETEVENT, SDL_ALLEVENTS ) > 0 ) {
1832             // spin
1833           }
1834         }
1835 #endif
1836
1837 #if 1
1838         // just exec it
1839         //
1840
1841         // get CWD so we can restore it on return
1842         char cwd [ PATH_MAX ];
1843         getcwd ( cwd, PATH_MAX );
1844
1845         // full path to executable so we don't rely on implicit "./"
1846         char execbuf [ FILENAME_MAX ];
1847         snprintf ( execbuf, FILENAME_MAX, "%s/%s", g_categories [ ui_category ] -> fspath, ui_selected -> ref -> title_en );
1848
1849         // do it!
1850         chdir ( g_categories [ ui_category ] -> fspath );
1851         exec_raw_binary ( execbuf /*ui_selected -> ref -> title_en*/ );
1852         chdir ( cwd );
1853 #else
1854         // DEPRECATED / NOT TESTED
1855         // get mmwrapper to run it
1856         char buffer [ PATH_MAX ];
1857         sprintf ( buffer, "%s %s/%s\n", MM_RUN, g_categories [ ui_category ] -> fspath, ui_selected -> ref -> title_en );
1858         if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.live_on_run", 0 ) == 0 ) {
1859           emit_and_quit ( buffer );
1860         } else {
1861           emit_and_run ( buffer );
1862         }
1863 #endif
1864       } // pnd or bin?
1865
1866     } // dir or file?
1867
1868   } else {
1869
1870     // set app-run speed
1871     int use_run_speed = pnd_conf_get_as_int_d ( g_conf, "minimenu.use_run_speed", 0 );
1872     if ( use_run_speed > 0 ) {
1873       int mm_speed = pnd_conf_get_as_int_d ( g_conf, "minimenu.run_speed", -1 );
1874       if ( mm_speed > -1 ) {
1875         char buffer [ 512 ];
1876         snprintf ( buffer, 500, "sudo /usr/pandora/scripts/op_cpuspeed.sh %d", mm_speed );
1877         system ( buffer );
1878       }
1879     } // do speed change?
1880
1881     // request app to run and quit mmenu
1882     pnd_apps_exec_disco ( pnd_run_script, ui_selected -> ref, PND_EXEC_OPTION_NORUN, NULL );
1883     char buffer [ PATH_MAX ];
1884     sprintf ( buffer, "%s %s\n", MM_RUN, pnd_apps_exec_runline() );
1885     if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.live_on_run", 0 ) == 0 ) {
1886       emit_and_quit ( buffer );
1887     } else {
1888       emit_and_run ( buffer );
1889     }
1890   }
1891
1892   return;
1893 }
1894
1895 void ui_push_ltrigger ( void ) {
1896   unsigned char oldcat = ui_category;
1897   unsigned int screen_width = ui_display_context.screen_width;
1898   unsigned int tab_width = pnd_conf_get_as_int ( g_conf, "tabs.tab_width" );
1899
1900   if ( g_categorycount == 0 ) {
1901     return;
1902   }
1903
1904   if ( ui_category > 0 ) {
1905     ui_category--;
1906     category_fs_restock ( g_categories [ ui_category ] );
1907   } else {
1908     if ( pnd_conf_get_as_int_d ( g_conf, "tabs.wraparound", 0 ) > 0 ) {
1909       ui_category = g_categorycount - 1;
1910       ui_catshift = 0;
1911       if ( ui_category >= ( screen_width / tab_width ) ) {
1912         ui_catshift = ui_category - ( screen_width / tab_width ) + 1;
1913       }
1914       category_fs_restock ( g_categories [ ui_category ] );
1915     }
1916   }
1917
1918   if ( oldcat != ui_category ) {
1919     ui_selected = NULL;
1920     ui_set_selected ( ui_selected );
1921   }
1922
1923   // make tab visible?
1924   if ( ui_catshift > 0 && ui_category == ui_catshift - 1 ) {
1925     ui_catshift--;
1926   }
1927
1928   // unscroll
1929   ui_rows_scrolled_down = 0;
1930
1931   render_mask |= CHANGED_CATEGORY;
1932
1933   return;
1934 }
1935
1936 void ui_push_rtrigger ( void ) {
1937   unsigned char oldcat = ui_category;
1938
1939   if ( g_categorycount == 0 ) {
1940     return;
1941   }
1942
1943   unsigned int screen_width = ui_display_context.screen_width;
1944   unsigned int tab_width = pnd_conf_get_as_int ( g_conf, "tabs.tab_width" );
1945
1946   if ( ui_category < ( g_categorycount - 1 ) ) {
1947     ui_category++;
1948     category_fs_restock ( g_categories [ ui_category ] );
1949   } else {
1950     if ( pnd_conf_get_as_int_d ( g_conf, "tabs.wraparound", 0 ) > 0 ) {
1951       ui_category = 0;
1952       ui_catshift = 0;
1953       category_fs_restock ( g_categories [ ui_category ] );
1954     }
1955   }
1956
1957   if ( oldcat != ui_category ) {
1958     ui_selected = NULL;
1959     ui_set_selected ( ui_selected );
1960   }
1961
1962   // make tab visible?
1963   if ( ui_category > ui_catshift + ( screen_width / tab_width ) - 1 ) {
1964     ui_catshift++;
1965   }
1966
1967   // unscroll
1968   ui_rows_scrolled_down = 0;
1969
1970   render_mask |= CHANGED_CATEGORY;
1971
1972   return;
1973 }
1974
1975 SDL_Surface *ui_scale_image ( SDL_Surface *s, unsigned int maxwidth, int maxheight ) {
1976   double scale = 1000000.0;
1977   double scalex = 1000000.0;
1978   double scaley = 1000000.0;
1979   SDL_Surface *scaled;
1980
1981   scalex = (double)maxwidth / (double)s -> w;
1982
1983   if ( maxheight == -1 ) {
1984     scale = scalex;
1985   } else {
1986     scaley = (double)maxheight / (double)s -> h;
1987
1988     if ( scaley < scalex ) {
1989       scale = scaley;
1990     } else {
1991       scale = scalex;
1992     }
1993
1994   }
1995
1996   scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
1997   SDL_FreeSurface ( s );
1998   s = scaled;
1999
2000   return ( s );
2001 }
2002
2003 void ui_loadscreen ( void ) {
2004
2005   SDL_Rect dest;
2006
2007   // clear the screen
2008   SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
2009
2010   // render text
2011   SDL_Surface *rtext;
2012   rtext = TTF_RenderText_Blended ( g_big_font, "Setting up menu...", ui_display_context.fontcolor );
2013   dest.x = 20;
2014   dest.y = 20;
2015   SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, &dest );
2016   SDL_UpdateRects ( sdl_realscreen, 1, &dest );
2017   SDL_FreeSurface ( rtext );
2018
2019   return;
2020 }
2021
2022 void ui_discoverscreen ( unsigned char clearscreen ) {
2023
2024   SDL_Rect dest;
2025
2026   // clear the screen
2027   if ( clearscreen ) {
2028     SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
2029
2030     // render background
2031     if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
2032       dest.x = 0;
2033       dest.y = 0;
2034       dest.w = sdl_realscreen -> w;
2035       dest.h = sdl_realscreen -> h;
2036       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, NULL /* whole image */, sdl_realscreen, NULL /* 0,0 */ );
2037       SDL_UpdateRects ( sdl_realscreen, 1, &dest );
2038     }
2039
2040   }
2041
2042   // render text
2043   SDL_Surface *rtext;
2044   rtext = TTF_RenderText_Blended ( g_big_font, "Looking for applications...", ui_display_context.fontcolor );
2045   if ( clearscreen ) {
2046     dest.x = 20;
2047     dest.y = 20;
2048   } else {
2049     dest.x = 20;
2050     dest.y = 40;
2051   }
2052   SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, &dest );
2053   SDL_UpdateRects ( sdl_realscreen, 1, &dest );
2054   SDL_FreeSurface ( rtext );
2055
2056   // render icon
2057   if ( g_imagecache [ IMG_ICON_MISSING ].i ) {
2058     dest.x = rtext -> w + 30;
2059     dest.y = 20;
2060     SDL_BlitSurface ( g_imagecache [ IMG_ICON_MISSING ].i, NULL, sdl_realscreen, &dest );
2061     SDL_UpdateRects ( sdl_realscreen, 1, &dest );
2062   }
2063
2064   return;
2065 }
2066
2067 void ui_cachescreen ( unsigned char clearscreen, char *filename ) {
2068
2069   SDL_Rect rects [ 4 ];
2070   SDL_Rect *dest = rects;
2071   SDL_Rect src;
2072   bzero ( dest, sizeof(SDL_Rect)* 4 );
2073
2074   unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
2075   unsigned int font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
2076   unsigned int font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
2077   unsigned int font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
2078
2079   static unsigned int stepx = 0;
2080
2081   // clear the screen
2082   if ( clearscreen ) {
2083     SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
2084
2085     // render background
2086     if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
2087       dest -> x = 0;
2088       dest -> y = 0;
2089       dest -> w = sdl_realscreen -> w;
2090       dest -> h = sdl_realscreen -> h;
2091       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, NULL /* whole image */, sdl_realscreen, NULL /* 0,0 */ );
2092       dest++;
2093     }
2094
2095   } else {
2096
2097     // render background
2098     if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
2099       src.x = 0;
2100       src.y = 0;
2101       src.w = sdl_realscreen -> w;
2102       src.h = 100;
2103       dest -> x = 0;
2104       dest -> y = 0;
2105       dest -> w = sdl_realscreen -> w;
2106       dest -> h = sdl_realscreen -> h;
2107       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, &src, sdl_realscreen, dest );
2108       dest++;
2109     }
2110
2111   } // clear it
2112
2113   // render text
2114   SDL_Surface *rtext;
2115   SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
2116   rtext = TTF_RenderText_Blended ( g_big_font, "Caching applications artwork...", tmpfontcolor );
2117   dest -> x = 20;
2118   dest -> y = 20;
2119   SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
2120   SDL_FreeSurface ( rtext );
2121   dest++;
2122
2123   // render icon
2124   if ( g_imagecache [ IMG_ICON_MISSING ].i ) {
2125     dest -> x = rtext -> w + 30 + stepx;
2126     dest -> y = 20;
2127     SDL_BlitSurface ( g_imagecache [ IMG_ICON_MISSING ].i, NULL, sdl_realscreen, dest );
2128     dest++;
2129   }
2130
2131   // filename
2132   if ( filename ) {
2133     rtext = TTF_RenderText_Blended ( g_tab_font, filename, tmpfontcolor );
2134     dest -> x = 20;
2135     dest -> y = 50;
2136     SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
2137     SDL_FreeSurface ( rtext );
2138     dest++;
2139   }
2140
2141   // move across
2142   stepx += 20;
2143
2144   if ( stepx > 350 ) {
2145     stepx = 0;
2146   }
2147
2148   SDL_UpdateRects ( sdl_realscreen, dest - rects, rects );
2149
2150   return;
2151 }
2152
2153 int ui_selected_index ( void ) {
2154
2155   if ( ! ui_selected ) {
2156     return ( -1 ); // no index
2157   }
2158
2159   mm_appref_t *r = g_categories [ ui_category ] -> refs;
2160   int counter = 0;
2161   while ( r ) {
2162     if ( r == ui_selected ) {
2163       return ( counter );
2164     }
2165     r = r -> next;
2166     counter++;
2167   }
2168
2169   return ( -1 );
2170 }
2171
2172 static mm_appref_t *timer_ref = NULL;
2173 void ui_set_selected ( mm_appref_t *r ) {
2174
2175   render_mask |= CHANGED_SELECTION;
2176
2177   if ( ! pnd_conf_get_as_int_d ( g_conf, "minimenu.load_previews_later", 0 ) ) {
2178     return; // no desire to defer anything
2179   }
2180
2181   if ( ! r ) {
2182     // cancel timer
2183     SDL_SetTimer ( 0, NULL );
2184     timer_ref = NULL;
2185     return;
2186   }
2187
2188   SDL_SetTimer ( pnd_conf_get_as_int_d ( g_conf, "previewpic.defer_timer_ms", 1000 ), ui_callback_f );
2189   timer_ref = r;
2190
2191   return;
2192 }
2193
2194 unsigned int ui_callback_f ( unsigned int t ) {
2195
2196   if ( ui_selected != timer_ref ) {
2197     return ( 0 ); // user has moved it, who cares
2198   }
2199
2200   SDL_Event e;
2201   bzero ( &e, sizeof(SDL_Event) );
2202   e.type = SDL_USEREVENT;
2203   e.user.code = sdl_user_ticker;
2204   SDL_PushEvent ( &e );
2205
2206   return ( 0 );
2207 }
2208
2209 int ui_modal_single_menu ( char *argv[], unsigned int argc, char *title, char *footer ) {
2210   SDL_Rect rects [ 40 ];
2211   SDL_Rect *dest = rects;
2212   SDL_Rect src;
2213   SDL_Surface *rtext;
2214   unsigned char max_visible = pnd_conf_get_as_int_d ( g_conf, "detailtext.max_visible" , 11 );
2215   unsigned char first_visible = 0;
2216
2217   bzero ( rects, sizeof(SDL_Rect) * 40 );
2218
2219   unsigned int sel = 0;
2220
2221   unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
2222   unsigned int font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
2223   unsigned int font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
2224   unsigned int font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
2225
2226   SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
2227
2228   SDL_Color selfontcolor = { 0/*font_rgba_r*/, font_rgba_g, font_rgba_b, font_rgba_a };
2229
2230   unsigned int i;
2231   SDL_Event event;
2232
2233   while ( 1 ) {
2234
2235     // clear
2236     dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
2237     dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
2238     dest -> w = ((SDL_Surface*) g_imagecache [ IMG_DETAIL_PANEL ].i) -> w;
2239     dest -> h = ((SDL_Surface*) g_imagecache [ IMG_DETAIL_PANEL ].i) -> h;
2240     SDL_FillRect( sdl_realscreen, dest, 0 );
2241
2242     // show dialog background
2243     if ( g_imagecache [ IMG_DETAIL_BG ].i ) {
2244       src.x = 0;
2245       src.y = 0;
2246       src.w = ((SDL_Surface*)(g_imagecache [ IMG_DETAIL_BG ].i)) -> w;
2247       src.h = ((SDL_Surface*)(g_imagecache [ IMG_DETAIL_BG ].i)) -> h;
2248       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
2249       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
2250       SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
2251       // repeat for darken?
2252       //SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
2253       //SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
2254       //SDL_UpdateRects ( sdl_realscreen, 1, &dest );
2255       dest++;
2256     }
2257
2258     // show dialog frame
2259     if ( g_imagecache [ IMG_DETAIL_PANEL ].i ) {
2260       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
2261       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
2262       SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_PANEL ].i, NULL /* whole image */, sdl_realscreen, dest );
2263       //SDL_UpdateRects ( sdl_realscreen, 1, &dest );
2264       dest++;
2265     }
2266
2267     // show header
2268     if ( title ) {
2269       rtext = TTF_RenderText_Blended ( g_tab_font, title, tmpfontcolor );
2270       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 ) + 20;
2271       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 ) + 20;
2272       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
2273       SDL_FreeSurface ( rtext );
2274       dest++;
2275     }
2276
2277     // show footer
2278     if ( footer ) {
2279       rtext = TTF_RenderText_Blended ( g_tab_font, footer, tmpfontcolor );
2280       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 ) + 20;
2281       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 ) +
2282         ((SDL_Surface*) g_imagecache [ IMG_DETAIL_PANEL ].i) -> h
2283         - 60;
2284       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
2285       SDL_FreeSurface ( rtext );
2286       dest++;
2287     }
2288
2289     // show options
2290     for ( i = first_visible; i < first_visible + max_visible && i < argc; i++ ) {
2291
2292       // show options
2293       if ( sel == i ) {
2294         rtext = TTF_RenderText_Blended ( g_tab_font, argv [ i ], selfontcolor );
2295       } else {
2296         rtext = TTF_RenderText_Blended ( g_tab_font, argv [ i ], tmpfontcolor );
2297       }
2298       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 ) + 20;
2299       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 ) + 40 + ( 20 * ( i + 1 - first_visible ) );
2300       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
2301       SDL_FreeSurface ( rtext );
2302       dest++;
2303
2304     } // for
2305
2306     // update all the rects and send it all to sdl
2307     SDL_UpdateRects ( sdl_realscreen, dest - rects, rects );
2308     dest = rects;
2309
2310     // check for input
2311     while ( SDL_WaitEvent ( &event ) ) {
2312
2313       switch ( event.type ) {
2314
2315       //case SDL_KEYUP:
2316       case SDL_KEYDOWN:
2317
2318         if ( event.key.keysym.sym == SDLK_UP ) {
2319           if ( sel ) {
2320             sel--;
2321
2322             if ( sel < first_visible ) {
2323               first_visible--;
2324             }
2325
2326           }
2327         } else if ( event.key.keysym.sym == SDLK_DOWN ) {
2328
2329           if ( sel < argc - 1 ) {
2330             sel++;
2331
2332             // ensure visibility
2333             if ( sel >= first_visible + max_visible ) {
2334               first_visible++;
2335             }
2336
2337           }
2338
2339         } else if ( event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_END ) { // return, or "B"
2340           return ( sel );
2341
2342 #if 0
2343         } else if ( event.key.keysym.sym == SDLK_q ) {
2344           exit ( 0 );
2345 #endif
2346
2347         } else {
2348           return ( -1 ); // nada
2349         }
2350
2351         break;
2352
2353       } // switch
2354
2355       break;
2356     } // while
2357
2358   } // while
2359
2360   return ( -1 );
2361 }
2362
2363 unsigned char ui_determine_row ( mm_appref_t *a ) {
2364   unsigned int row = 0;
2365
2366   mm_appref_t *i = g_categories [ ui_category ] -> refs;
2367   while ( i != a ) {
2368     i = i -> next;
2369     row++;
2370   } // while
2371   row /= ui_display_context.col_max;
2372
2373   return ( row );
2374 }
2375
2376 unsigned char ui_determine_screen_row ( mm_appref_t *a ) {
2377   return ( ui_determine_row ( a ) % ui_display_context.row_max );
2378 }
2379
2380 unsigned char ui_determine_screen_col ( mm_appref_t *a ) {
2381   unsigned int col = 0;
2382
2383   mm_appref_t *i = g_categories [ ui_category ] -> refs;
2384   while ( i != a ) {
2385     i = i -> next;
2386     col++;
2387   } // while
2388   col %= ui_display_context.col_max;
2389
2390   return ( col );
2391 }
2392
2393 unsigned char ui_show_info ( char *pndrun, pnd_disco_t *p ) {
2394   char *viewer, *searchpath;
2395   pnd_conf_handle desktoph;
2396
2397   // viewer
2398   searchpath = pnd_conf_query_searchpath();
2399
2400   desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, searchpath );
2401
2402   if ( ! desktoph ) {
2403     return ( 0 );
2404   }
2405
2406   viewer = pnd_conf_get_as_char ( desktoph, "info.viewer" );
2407
2408   if ( ! viewer ) {
2409     return ( 0 ); // no way to view the file
2410   }
2411
2412   // etc
2413   if ( ! p -> unique_id ) {
2414     return ( 0 );
2415   }
2416
2417   if ( ! p -> info_filename ) {
2418     return ( 0 );
2419   }
2420
2421   if ( ! p -> info_name ) {
2422     return ( 0 );
2423   }
2424
2425   if ( ! pndrun ) {
2426     return ( 0 );
2427   }
2428
2429   // exec line
2430   char args [ 1001 ];
2431   char *pargs = args;
2432   if ( pnd_conf_get_as_char ( desktoph, "info.viewer_args" ) ) {
2433     snprintf ( pargs, 1001, "%s %s",
2434                pnd_conf_get_as_char ( desktoph, "info.viewer_args" ), p -> info_filename );
2435   } else {
2436     pargs = NULL;
2437   }
2438
2439   char pndfile [ 1024 ];
2440   if ( p -> object_type == pnd_object_type_directory ) {
2441     // for PXML-app-dir, pnd_run.sh doesn't want the PXML.xml.. it just wants the dir-name
2442     strncpy ( pndfile, p -> object_path, 1000 );
2443   } else if ( p -> object_type == pnd_object_type_pnd ) {
2444     // pnd_run.sh wants the full path and filename for the .pnd file
2445     snprintf ( pndfile, 1020, "%s/%s", p -> object_path, p -> object_filename );
2446   }
2447
2448   if ( ! pnd_apps_exec ( pndrun, pndfile, p -> unique_id, viewer, p -> startdir, pargs,
2449                          p -> clockspeed ? atoi ( p -> clockspeed ) : 0, PND_EXEC_OPTION_NORUN ) )
2450   {
2451     return ( 0 );
2452   }
2453
2454   pnd_log ( pndn_debug, "Info Exec=%s\n", pnd_apps_exec_runline() );
2455
2456   // try running it
2457   int x;
2458   if ( ( x = fork() ) < 0 ) {
2459     pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
2460     return ( 0 );
2461   }
2462
2463   if ( x == 0 ) {
2464     execl ( "/bin/sh", "/bin/sh", "-c", pnd_apps_exec_runline(), (char*)NULL );
2465     pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", pnd_apps_exec_runline() );
2466     return ( 0 );
2467   }
2468
2469   return ( 1 );
2470 }
2471
2472 typedef struct {
2473   SDL_Rect r;
2474   int catnum;
2475   mm_appref_t *ref;
2476 } ui_touch_t;
2477 #define MAXTOUCH 100
2478 ui_touch_t ui_touchrects [ MAXTOUCH ];
2479 unsigned char ui_touchrect_count = 0;
2480
2481 void ui_register_reset ( void ) {
2482   bzero ( ui_touchrects, sizeof(ui_touch_t)*MAXTOUCH );
2483   ui_touchrect_count = 0;
2484   return;
2485 }
2486
2487 void ui_register_tab ( unsigned char catnum, unsigned int x, unsigned int y, unsigned int w, unsigned int h ) {
2488
2489   if ( ui_touchrect_count == MAXTOUCH ) {
2490     return;
2491   }
2492
2493   ui_touchrects [ ui_touchrect_count ].r.x = x;
2494   ui_touchrects [ ui_touchrect_count ].r.y = y;
2495   ui_touchrects [ ui_touchrect_count ].r.w = w;
2496   ui_touchrects [ ui_touchrect_count ].r.h = h;
2497   ui_touchrects [ ui_touchrect_count ].catnum = catnum;
2498   ui_touchrect_count++;
2499
2500   return;
2501 }
2502
2503 void ui_register_app ( mm_appref_t *app, unsigned int x, unsigned int y, unsigned int w, unsigned int h ) {
2504
2505   if ( ui_touchrect_count == MAXTOUCH ) {
2506     return;
2507   }
2508
2509   ui_touchrects [ ui_touchrect_count ].r.x = x;
2510   ui_touchrects [ ui_touchrect_count ].r.y = y;
2511   ui_touchrects [ ui_touchrect_count ].r.w = w;
2512   ui_touchrects [ ui_touchrect_count ].r.h = h;
2513   ui_touchrects [ ui_touchrect_count ].ref = app;
2514   ui_touchrect_count++;
2515
2516   return;
2517 }
2518
2519 void ui_touch_act ( unsigned int x, unsigned int y ) {
2520
2521   unsigned char i;
2522   ui_touch_t *t;
2523
2524   for ( i = 0; i < ui_touchrect_count; i++ ) {
2525     t = &(ui_touchrects [ i ]);
2526
2527     if ( x >= t -> r.x &&
2528          x <= t -> r.x + t -> r.w &&
2529          y >= t -> r.y &&
2530          y <= t -> r.y + t -> r.h
2531        )
2532     {
2533
2534       if ( t -> ref ) {
2535         ui_selected = t -> ref;
2536         ui_push_exec();
2537       } else {
2538         if ( ui_category != t -> catnum ) {
2539           ui_selected = NULL;
2540         }
2541         ui_category = t -> catnum;
2542         render_mask |= CHANGED_CATEGORY;
2543         // rescan the dir
2544         category_fs_restock ( g_categories [ ui_category ] );
2545       }
2546
2547       break;
2548     }
2549
2550   } // for
2551
2552   return;
2553 }
2554
2555 unsigned char ui_forkexec ( char *argv[] ) {
2556   char *fooby = argv[0];
2557   int x;
2558
2559   if ( ( x = fork() ) < 0 ) {
2560     pnd_log ( pndn_error, "ERROR: Couldn't fork() for '%s'\n", fooby );
2561     return ( 0 );
2562   }
2563
2564   if ( x == 0 ) { // child
2565     execv ( fooby, argv );
2566     pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", fooby );
2567     return ( 0 );
2568   }
2569
2570   // parent, success
2571   return ( 1 );
2572 }
2573
2574 unsigned char ui_threaded_timer_create ( void ) {
2575
2576   g_timer_thread = SDL_CreateThread ( (void*)ui_threaded_timer, NULL );
2577
2578   if ( ! g_timer_thread ) {
2579     pnd_log ( pndn_error, "ERROR: Couldn't create timer thread\n" );
2580     return ( 0 );
2581   }
2582
2583   return ( 1 );
2584 }
2585
2586 int ui_threaded_timer ( pnd_disco_t *p ) {
2587
2588   // this timer's job is to ..
2589   // - do nothing for quite awhile
2590   // - on wake, post event to SDL event queue, so that main thread will check if SD insert/eject occurred
2591   // - goto 10
2592
2593   unsigned int delay_s = 2; // seconds
2594
2595   while ( 1 ) {
2596
2597     // pause...
2598     sleep ( delay_s );
2599
2600     // .. trigger SD check
2601     SDL_Event e;
2602     bzero ( &e, sizeof(SDL_Event) );
2603     e.type = SDL_USEREVENT;
2604     e.user.code = sdl_user_checksd;
2605     SDL_PushEvent ( &e );
2606
2607   } // while
2608
2609   return ( 0 );
2610 }
2611
2612 unsigned char ui_threaded_defered_preview ( pnd_disco_t *p ) {
2613
2614   if ( ! cache_preview ( p, pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 200 ),
2615                          pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 180 ) )
2616      )
2617   {
2618     pnd_log ( pndn_debug, "THREAD: Couldn't load preview pic: '%s' -> '%s'\n",
2619               IFNULL(p->title_en,"No Name"), p -> preview_pic1 );
2620   }
2621
2622   // trigger that we completed
2623   SDL_Event e;
2624   bzero ( &e, sizeof(SDL_Event) );
2625   e.type = SDL_USEREVENT;
2626   e.user.code = sdl_user_finishedpreview;
2627   e.user.data1 = p;
2628   SDL_PushEvent ( &e );
2629
2630   return ( 0 );
2631 }
2632
2633 SDL_Thread *g_icon_thread = NULL;
2634 void ui_post_scan ( void ) {
2635
2636   // if deferred icon load, kick off the thread now
2637   if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_icons_later", 0 ) == 1 ) {
2638
2639     g_icon_thread = SDL_CreateThread ( (void*)ui_threaded_defered_icon, NULL );
2640
2641     if ( ! g_icon_thread ) {
2642       pnd_log ( pndn_error, "ERROR: Couldn't create icon thread\n" );
2643     }
2644
2645   } // deferred icon load
2646
2647   // reset view
2648   ui_selected = NULL;
2649   ui_rows_scrolled_down = 0;
2650   // set back to first tab, to be safe
2651   ui_category = 0;
2652   ui_catshift = 0;
2653
2654   // do we have a preferred category to jump to?
2655   char *dc = pnd_conf_get_as_char ( g_conf, "categories.default_cat" );
2656   if ( dc ) {
2657
2658     // attempt to find default cat; if we do find it, select it; otherwise
2659     // default behaviour will pick first cat (ie: usually All)
2660     unsigned int i;
2661     for ( i = 0; i < g_categorycount; i++ ) {
2662       if ( strcasecmp ( g_categories [ i ] -> catname, dc ) == 0 ) {
2663         ui_category = i;
2664         // ensure visibility
2665         unsigned int screen_width = ui_display_context.screen_width;
2666         unsigned int tab_width = pnd_conf_get_as_int ( g_conf, "tabs.tab_width" );
2667         if ( ui_category > ui_catshift + ( screen_width / tab_width ) - 1 ) {
2668           ui_catshift = ui_category - ( screen_width / tab_width ) + 1;
2669         }
2670         break;
2671       }
2672     }
2673
2674     if ( i == g_categorycount ) {
2675       pnd_log ( pndn_warning, "  User defined default category '%s' but not found, so using default behaviour\n", dc );
2676     }
2677
2678   } // default cat
2679
2680   // if we're sent right to a dirbrowser tab, restock it now (normally we restock on entry)
2681   if ( g_categories [ ui_category ] -> fspath ) {
2682     printf ( "Restock on start: '%s'\n", g_categories [ ui_category ] -> fspath );
2683     category_fs_restock ( g_categories [ ui_category ] );
2684   }
2685
2686   // redraw all
2687   render_mask |= CHANGED_EVERYTHING;
2688
2689   return;
2690 }
2691
2692 unsigned char ui_threaded_defered_icon ( void *p ) {
2693   extern pnd_box_handle g_active_apps;
2694   pnd_box_handle h = g_active_apps;
2695
2696   unsigned char maxwidth, maxheight;
2697   maxwidth = pnd_conf_get_as_int_d ( g_conf, "grid.icon_max_width", 50 );
2698   maxheight = pnd_conf_get_as_int_d ( g_conf, "grid.icon_max_height", 50 );
2699
2700   pnd_disco_t *iter = pnd_box_get_head ( h );
2701
2702   while ( iter ) {
2703
2704     // cache it
2705     if ( iter -> pnd_icon_pos &&
2706          ! cache_icon ( iter, maxwidth, maxheight ) )
2707     {
2708       pnd_log ( pndn_warning, "  Couldn't load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
2709     } else {
2710
2711       // trigger that we completed
2712       SDL_Event e;
2713       bzero ( &e, sizeof(SDL_Event) );
2714       e.type = SDL_USEREVENT;
2715       e.user.code = sdl_user_finishedicon;
2716       SDL_PushEvent ( &e );
2717
2718       //pnd_log ( pndn_warning, "  Finished deferred load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
2719       usleep ( pnd_conf_get_as_int_d ( g_conf, "minimenu.defer_icon_us", 50000 ) );
2720
2721     }
2722
2723     // next
2724     iter = pnd_box_get_next ( iter );
2725   } // while
2726
2727   return ( 0 );
2728 }
2729
2730 void ui_show_hourglass ( unsigned char updaterect ) {
2731
2732   SDL_Rect dest;
2733   SDL_Surface *s = g_imagecache [ IMG_HOURGLASS ].i;
2734
2735   dest.x = ( 800 - s -> w ) / 2;
2736   dest.y = ( 480 - s -> h ) / 2;
2737
2738   SDL_BlitSurface ( s, NULL /* whole image */, sdl_realscreen, &dest );
2739
2740   if ( updaterect ) {
2741     SDL_UpdateRects ( sdl_realscreen, 1, &dest );
2742   }
2743
2744   return;
2745 }
2746
2747 unsigned char ui_pick_skin ( void ) {
2748 #define MAXSKINS 10
2749   char *skins [ MAXSKINS ];
2750   unsigned char iter;
2751
2752   char *searchpath = pnd_conf_get_as_char ( g_conf, "minimenu.skin_searchpath" );
2753   char tempname [ 100 ];
2754
2755   iter = 0;
2756
2757   skins [ iter++ ] = "No skin change";
2758
2759   SEARCHPATH_PRE
2760   {
2761     DIR *d = opendir ( buffer );
2762
2763     if ( d ) {
2764       struct dirent *dd;
2765
2766       while ( ( dd = readdir ( d ) ) ) {
2767
2768         if ( dd -> d_name [ 0 ] == '.' ) {
2769           // ignore
2770         } else if ( ( dd -> d_type == DT_DIR || dd -> d_type == DT_UNKNOWN ) &&
2771                     iter < MAXSKINS )
2772         {
2773           snprintf ( tempname, 100, "Skin: %s", dd -> d_name );
2774           skins [ iter++ ] = strdup ( tempname );
2775         }
2776
2777       }
2778
2779       closedir ( d );
2780     }
2781
2782   }
2783   SEARCHPATH_POST
2784
2785   int sel = ui_modal_single_menu ( skins, iter, "Skins", "Enter to select; other to return." );
2786
2787   // did they pick one?
2788   if ( sel > 0 ) {
2789     FILE *f;
2790
2791     char *s = strdup ( pnd_conf_get_as_char ( g_conf, "minimenu.skin_selected" ) );
2792     s = pnd_expand_tilde ( s );
2793
2794     f = fopen ( s, "w" );
2795
2796     free ( s );
2797
2798     if ( f ) {
2799       fprintf ( f, "%s\n", skins [ sel ] + 6 );
2800       fclose ( f );
2801     }
2802
2803     return ( 1 );
2804   }
2805
2806   return ( 0 );
2807 }
2808
2809 void ui_aboutscreen ( char *textpath ) {
2810 #define PIXELW 7
2811 #define PIXELH 7
2812 #define MARGINW 3
2813 #define MARGINH 3
2814 #define SCRW 800
2815 #define SCRH 480
2816 #define ROWS SCRH / ( PIXELH + MARGINH )
2817 #define COLS SCRW / ( PIXELW + MARGINW )
2818
2819   unsigned char pixelboard [ ROWS * COLS ]; // pixel heat
2820   bzero ( pixelboard, ROWS * COLS );
2821
2822   SDL_Surface *rtext;
2823   SDL_Rect r;
2824
2825   SDL_Color rtextc = { 200, 200, 200, 100 };
2826
2827   // pixel scroller
2828   char *textloop [ 500 ];
2829   unsigned int textmax = 0;
2830   bzero ( textloop, 500 * sizeof(char*) );
2831
2832   // cursor scroller
2833   char cbuffer [ 50000 ];
2834   bzero ( cbuffer, 50000 );
2835   unsigned int crevealed = 0;
2836
2837   FILE *f = fopen ( textpath, "r" );
2838
2839   if ( ! f ) {
2840     pnd_log ( pndn_error, "ERROR: Couldn't open about text: %s\n", textpath );
2841     return;
2842   }
2843
2844   char textbuf [ 100 ];
2845   while ( fgets ( textbuf, 100, f ) ) {
2846
2847     // add to full buffer
2848     strncat ( cbuffer, textbuf, 50000 );
2849
2850     // chomp
2851     if ( strchr ( textbuf, '\n' ) ) {
2852       * strchr ( textbuf, '\n' ) = '\0';
2853     }
2854
2855     // add to pixel loop
2856     if ( 1||textbuf [ 0 ] ) {
2857       textloop [ textmax ] = strdup ( textbuf );
2858       textmax++;
2859     }
2860
2861   } // while fgets
2862
2863   fclose ( f );
2864
2865   unsigned int textiter = 0;
2866   while ( textiter < textmax ) {
2867     char *text = textloop [ textiter ];
2868
2869     rtext = NULL;
2870     if ( text [ 0 ] ) {
2871       // render to surface
2872       rtext = TTF_RenderText_Blended ( g_grid_font, text, rtextc );
2873
2874       // render font to pixelboard
2875       unsigned int px, py;
2876       unsigned char *ph;
2877       unsigned int *pixels = rtext -> pixels;
2878       unsigned char cr, cg, cb, ca;
2879       for ( py = 0; py < rtext -> h; py ++ ) {
2880         for ( px = 0; px < ( rtext -> w > COLS ? COLS : rtext -> w ); px++ ) {
2881
2882           SDL_GetRGBA ( pixels [ ( py * rtext -> pitch / 4 ) + px ],
2883                         rtext -> format, &cr, &cg, &cb, &ca );
2884
2885           if ( ca != 0 ) {
2886
2887             ph = pixelboard + ( /*y offset */ 30 * COLS ) + ( py * COLS ) + px /* / 2 */;
2888
2889             if ( *ph < 100 ) {
2890               *ph = 100;
2891             }
2892
2893             ca /= 5;
2894             if ( *ph + ca < 250 ) {
2895               *ph += ca;
2896             }
2897
2898           } // got a pixel?
2899
2900         } // x
2901       } // y
2902
2903     } // got text?
2904
2905     unsigned int runcount = 10;
2906     while ( runcount-- ) {
2907
2908       // clear display
2909       SDL_FillRect( sdl_realscreen, NULL /* whole */, 0 );
2910
2911       // render pixelboard
2912       unsigned int x, y;
2913       unsigned int c;
2914       for ( y = 0; y < ROWS; y++ ) {
2915         for ( x = 0; x < COLS; x++ ) {
2916
2917           if ( 1||pixelboard [ ( y * COLS ) + x ] ) {
2918
2919             // position
2920             r.x = x * ( PIXELW + MARGINW );
2921             r.y = y * ( PIXELH + MARGINH );
2922             r.w = PIXELW;
2923             r.h = PIXELH;
2924             // heat -> colour
2925             c = SDL_MapRGB ( sdl_realscreen -> format, 100 /* r */, 0 /* g */, pixelboard [ ( y * COLS ) + x ] );
2926             // render
2927             SDL_FillRect( sdl_realscreen, &r /* whole */, c );
2928
2929           }
2930
2931         } // x
2932       } // y
2933
2934       // cool pixels
2935       unsigned char *pc = pixelboard;
2936       for ( y = 0; y < ROWS; y++ ) {
2937         for ( x = 0; x < COLS; x++ ) {
2938
2939           if ( *pc > 10 ) {
2940             (*pc) -= 3;
2941           }
2942
2943           pc++;
2944         } // x
2945       } // y
2946
2947       // slide pixels upwards
2948       memmove ( pixelboard, pixelboard + COLS, ( COLS * ROWS ) - COLS );
2949
2950       // render actual readable text
2951       {
2952
2953         // display up to cursor
2954         SDL_Rect dest;
2955         unsigned int cdraw = 0;
2956         SDL_Surface *cs;
2957         char ctb [ 2 ];
2958
2959         if ( crevealed > 200 ) {
2960           cdraw = crevealed - 200;
2961         }
2962
2963         dest.x = 400;
2964         dest.y = 20;
2965
2966         for ( ; cdraw < crevealed; cdraw++ ) {
2967           ctb [ 0 ] = cbuffer [ cdraw ];
2968           ctb [ 1 ] = '\0';
2969           // move over or down
2970           if ( cbuffer [ cdraw ] == '\n' ) {
2971             // EOL
2972             dest.x = 400;
2973             dest.y += 14;
2974
2975             if ( dest.y > 450 ) {
2976               dest.y = 450;
2977             }
2978
2979           } else {
2980             // draw the char
2981             cs = TTF_RenderText_Blended ( g_tab_font, ctb, rtextc );
2982             if ( cs ) {
2983               SDL_BlitSurface ( cs, NULL /* all */, sdl_realscreen, &dest );
2984               SDL_FreeSurface ( cs );
2985               // over
2986               dest.x += cs -> w;
2987             }
2988           }
2989
2990         }
2991
2992         dest.w = 10;
2993         dest.h = 20;
2994         SDL_FillRect ( sdl_realscreen, &dest /* whole */, 220 );
2995
2996         // increment cursor to next character
2997         if ( cbuffer [ crevealed ] != '\0' ) {
2998           crevealed++;
2999         }
3000
3001       } // draw cursor text
3002
3003       // reveal
3004       //
3005       SDL_UpdateRect ( sdl_realscreen, 0, 0, 0, 0 ); // whole screen
3006
3007       usleep ( 50000 );
3008
3009       // any button? if so, about
3010       {
3011         SDL_PumpEvents();
3012
3013         SDL_Event e;
3014
3015         if ( SDL_PeepEvents ( &e, 1, SDL_GETEVENT, SDL_EVENTMASK(/*SDL_KEYUP|*/SDL_KEYDOWN) ) > 0 ) {
3016           return;
3017         }
3018
3019       }
3020
3021     } // while cooling
3022
3023     if ( rtext ) {
3024       SDL_FreeSurface ( rtext );
3025     }
3026
3027     textiter++;
3028   } // while more text
3029
3030   // free up
3031   unsigned int i;
3032   for ( i = 0; i < textmax; i++ ) {
3033     if ( textloop [ i ] ) {
3034       free ( textloop [ i ] );
3035       textloop [ i ] = 0;
3036     }
3037   }
3038
3039   return;
3040 }
3041
3042 void ui_revealscreen ( void ) {
3043   char *labels [ 500 ];
3044   unsigned int labelmax = 0;
3045   unsigned int i;
3046   char fulllabel [ 200 ];
3047
3048   if ( ! category_count ( CFHIDDEN ) ) {
3049     return; // nothing to do
3050   }
3051
3052   // switch to hidden categories
3053   category_publish ( CFHIDDEN, NULL );
3054
3055   // build up labels to show in menu
3056   for ( i = 0; i < g_categorycount; i++ ) {
3057
3058     if ( g_categories [ i ] -> parent_catname ) {
3059       sprintf ( fulllabel, "%s [%s]", g_categories [ i ] -> catname, g_categories [ i ] -> parent_catname );
3060     } else {
3061       sprintf ( fulllabel, "%s", g_categories [ i ] -> catname );
3062     }
3063
3064     labels [ labelmax++ ] = strdup ( fulllabel );
3065   }
3066
3067   // show menu
3068   int sel = ui_modal_single_menu ( labels, labelmax, "Temporary Category Reveal",
3069                                    "Enter to select; other to return." );
3070
3071   // if selected, try to set this guy to visible
3072   if ( sel >= 0 ) {
3073
3074     // fix up category name, if its been hacked
3075     if ( strchr ( g_categories [ sel ] -> catname, '.' ) ) {
3076       char *t = g_categories [ sel ] -> catname;
3077       g_categories [ sel ] -> catname = strdup ( strchr ( g_categories [ sel ] -> catname, '.' ) + 1 );
3078       free ( t );
3079     }
3080
3081     // reflag this guy to be visible
3082     g_categories [ sel ] -> catflags = CFNORMAL;
3083
3084     // switch to the new category.. cache name.
3085     char *switch_to_name = g_categories [ sel ] -> catname;
3086
3087     // republish categories
3088     category_publish ( CFNORMAL, NULL );
3089
3090     // switch to the new category.. with the cached name!
3091     ui_category = category_index ( switch_to_name );
3092
3093     // ensure visibility
3094     unsigned int screen_width = ui_display_context.screen_width;
3095     unsigned int tab_width = pnd_conf_get_as_int ( g_conf, "tabs.tab_width" );
3096     if ( ui_category > ui_catshift + ( screen_width / tab_width ) - 1 ) {
3097       ui_catshift = ui_category - ( screen_width / tab_width ) + 1;
3098     }
3099
3100     // redraw tabs
3101     render_mask |= CHANGED_CATEGORY;
3102   }
3103
3104   for ( i = 0; i < g_categorycount; i++ ) {
3105     free ( labels [ i ] );
3106   }
3107
3108   return;
3109 }
3110
3111 void ui_recache_context ( ui_context_t *c ) {
3112
3113   c -> screen_width = pnd_conf_get_as_int_d ( g_conf, "display.screen_width", 800 );
3114
3115   c -> font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
3116   c -> font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
3117   c -> font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
3118   c -> font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
3119
3120   c -> grid_offset_x = pnd_conf_get_as_int ( g_conf, "grid.grid_offset_x" );
3121   c -> grid_offset_y = pnd_conf_get_as_int ( g_conf, "grid.grid_offset_y" );
3122
3123   c -> icon_offset_x = pnd_conf_get_as_int ( g_conf, "grid.icon_offset_x" );
3124   c -> icon_offset_y = pnd_conf_get_as_int ( g_conf, "grid.icon_offset_y" );
3125   c -> icon_max_width = pnd_conf_get_as_int ( g_conf, "grid.icon_max_width" );
3126   c -> icon_max_height = pnd_conf_get_as_int ( g_conf, "grid.icon_max_height" );
3127   c -> sel_icon_offset_x = pnd_conf_get_as_int_d ( g_conf, "grid.sel_offoffset_x", 0 );
3128   c -> sel_icon_offset_y = pnd_conf_get_as_int_d ( g_conf, "grid.sel_offoffset_y", 0 );
3129
3130   c -> text_width = pnd_conf_get_as_int ( g_conf, "grid.text_width" );
3131   c -> text_clip_x = pnd_conf_get_as_int ( g_conf, "grid.text_clip_x" );
3132   c -> text_offset_x = pnd_conf_get_as_int ( g_conf, "grid.text_offset_x" );
3133   c -> text_offset_y = pnd_conf_get_as_int ( g_conf, "grid.text_offset_y" );
3134   c -> text_hilite_offset_y = pnd_conf_get_as_int ( g_conf, "grid.text_hilite_offset_y" );
3135
3136   c -> row_max = pnd_conf_get_as_int_d ( g_conf, "grid.row_max", 4 );
3137   c -> col_max = pnd_conf_get_as_int_d ( g_conf, "grid.col_max", 5 );
3138
3139   c -> cell_width = pnd_conf_get_as_int ( g_conf, "grid.cell_width" );
3140   c -> cell_height = pnd_conf_get_as_int ( g_conf, "grid.cell_height" );
3141
3142   c -> arrow_bar_x = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_x", 450 );
3143   c -> arrow_bar_y = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_y", 100 );
3144   c -> arrow_bar_clip_w = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_clip_w", 10 );
3145   c -> arrow_bar_clip_h = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_clip_h", 100 );
3146   c -> arrow_up_x = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_up_x", 450 );
3147   c -> arrow_up_y = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_up_y", 80 );
3148   c -> arrow_down_x = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_down_x", 450 );
3149   c -> arrow_down_y = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_down_y", 80 );
3150
3151   // font colour
3152   SDL_Color tmp = { c -> font_rgba_r, c -> font_rgba_g, c -> font_rgba_b, c -> font_rgba_a };
3153   c -> fontcolor = tmp;
3154
3155   // now that we've got 'normal' (detail pane shown) param's, lets check if detail pane
3156   // is hidden; if so, override some values with those alternate skin values where possible.
3157   if ( ui_detail_hidden ) {
3158     // if detail panel is hidden, and theme cannot support it, unhide the bloody thing. (This may help
3159     // when someone is amid theme hacking or changing.)
3160     if ( ! ui_is_detail_hideable() ) {
3161       ui_detail_hidden = 0;
3162     }
3163
3164     // still hidden?
3165     if ( ui_detail_hidden ) {
3166
3167       c -> row_max = pnd_conf_get_as_int_d ( g_conf, "grid.row_max_w", c -> row_max );
3168       c -> col_max = pnd_conf_get_as_int_d ( g_conf, "grid.col_max_w", c -> col_max );
3169
3170       c -> cell_width = pnd_conf_get_as_int_d ( g_conf, "grid.cell_width_w", c -> cell_width );
3171       c -> cell_height = pnd_conf_get_as_int_d ( g_conf, "grid.cell_height_w", c -> cell_height );
3172
3173       c -> arrow_bar_x = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_x_w", 450 );
3174       c -> arrow_bar_y = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_y_w", 100 );
3175       c -> arrow_up_x = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_up_x_w", 450 );
3176       c -> arrow_up_y = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_up_y_w", 80 );
3177       c -> arrow_down_x = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_down_x_w", 450 );
3178       c -> arrow_down_y = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_down_y_w", 80 );
3179
3180     } // if detail hidden.. still.
3181
3182   } // if detail hidden
3183
3184   return;
3185 }
3186
3187 unsigned char ui_is_detail_hideable ( void ) {
3188
3189   // if skin has a bit of info for wide-mode, we assume wide-mode is available
3190   if ( pnd_conf_get_as_char ( g_conf, "grid.row_max_w" ) != NULL ) {
3191     return ( 1 );
3192   }
3193
3194   // else not!
3195   return ( 0 );
3196 }
3197
3198 void ui_toggle_detail_pane ( void ) {
3199
3200   // no bitmask trickery here; I like it to be stand-out obvious at 3am.
3201
3202   if ( ui_detail_hidden ) {
3203     ui_detail_hidden = 0;
3204   } else {
3205     ui_detail_hidden = 1;
3206   }
3207
3208   // repull skin config
3209   ui_recache_context ( &ui_display_context );
3210
3211   // redraw
3212   render_mask |= CHANGED_EVERYTHING;
3213
3214   return;
3215 }