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