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