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