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