Lots of minimenu changes, couple minor fixes for pnd_utility
[pandora-libraries.git] / minimenu / mmui.c
1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <limits.h>
5 #include <time.h>
6 #include <unistd.h>
7 #include "SDL.h"
8 #include "SDL_audio.h"
9 #include "SDL_image.h"
10 #include "SDL_ttf.h"
11 #include "SDL_gfxPrimitives.h"
12 #include "SDL_rotozoom.h"
13 #include "SDL_thread.h"
14
15 #include "pnd_conf.h"
16 #include "pnd_logger.h"
17 #include "pnd_pxml.h"
18 #include "pnd_container.h"
19 #include "pnd_discovery.h"
20 #include "pnd_apps.h"
21 #include "pnd_device.h"
22
23 #include "mmenu.h"
24 #include "mmcat.h"
25 #include "mmcache.h"
26 #include "mmui.h"
27 #include "mmwrapcmd.h"
28
29 #define CHANGED_NOTHING     (0)
30 #define CHANGED_CATEGORY    (1<<0)  /* changed to different category */
31 #define CHANGED_SELECTION   (1<<1)  /* changed app selection */
32 #define CHANGED_DATAUPDATE  (1<<2)  /* deferred preview pic or icon came in */
33 #define CHANGED_APPRELOAD   (1<<3)  /* new set of applications entirely */
34 #define CHANGED_EVERYTHING  (1<<4)  /* redraw it all! */
35 unsigned int render_mask = CHANGED_EVERYTHING;
36
37 /* SDL
38  */
39 SDL_Surface *sdl_realscreen = NULL;
40 unsigned int sdl_ticks = 0;
41 SDL_Thread *g_preview_thread = NULL;
42
43 enum { sdl_user_ticker = 0,
44        sdl_user_finishedpreview = 1,
45        sdl_user_finishedicon = 2,
46 };
47
48 /* app state
49  */
50 unsigned short int g_scale = 1; // 1 == noscale
51
52 SDL_Surface *g_imgcache [ IMG_MAX ];
53
54 TTF_Font *g_big_font = NULL;
55 TTF_Font *g_grid_font = NULL;
56 TTF_Font *g_detailtext_font = NULL;
57 TTF_Font *g_tab_font = NULL;
58
59 extern pnd_conf_handle g_conf;
60
61 /* current display state
62  */
63 int ui_rows_scrolled_down = 0;          // number of rows that should be missing from top of the display
64 mm_appref_t *ui_selected = NULL;
65 unsigned char ui_category = 0;          // current category
66 unsigned char ui_catshift = 0;          // how many cats are offscreen to the left
67
68 extern mm_category_t g_categories [ MAX_CATS ];
69 extern unsigned char g_categorycount;
70
71 static SDL_Surface *ui_scale_image ( SDL_Surface *s, unsigned int maxwidth, int maxheight ); // height -1 means ignore
72 static int ui_selected_index ( void );
73
74 unsigned char ui_setup ( void ) {
75
76   /* set up SDL
77    */
78
79   SDL_Init ( SDL_INIT_EVERYTHING | SDL_INIT_NOPARACHUTE );
80
81   SDL_JoystickOpen ( 0 ); // turn on joy-0
82
83   SDL_WM_SetCaption ( "mmenu", "mmenu" );
84
85   // hide the mouse cursor if we can
86   if ( SDL_ShowCursor ( -1 ) == 1 ) {
87     SDL_ShowCursor ( 0 );
88   }
89
90   atexit ( SDL_Quit );
91
92   // open up a surface
93   unsigned int svm = SDL_SWSURFACE /*| SDL_FULLSCREEN*/ /* 0*/;
94   if ( pnd_conf_get_as_int_d ( g_conf, "display.fullscreen", 0 ) > 0 ) {
95     svm |= SDL_FULLSCREEN;
96   }
97
98   sdl_realscreen =
99     SDL_SetVideoMode ( 800 * g_scale, 480 * g_scale, 16 /*bpp*/, svm );
100
101   if ( ! sdl_realscreen ) {
102     pnd_log ( pndn_error, "ERROR: Couldn't open SDL real screen; dieing." );
103     return ( 0 );
104   }
105
106   pnd_log ( pndn_debug, "Pixel format:" );
107   pnd_log ( pndn_debug, "bpp b: %u\n", sdl_realscreen -> format -> BitsPerPixel );
108   pnd_log ( pndn_debug, "bpp B: %u\n", sdl_realscreen -> format -> BytesPerPixel );
109   pnd_log ( pndn_debug, "R mask: %08x\n", sdl_realscreen -> format -> Rmask );
110   pnd_log ( pndn_debug, "G mask: %08x\n", sdl_realscreen -> format -> Gmask );
111   pnd_log ( pndn_debug, "B mask: %08x\n", sdl_realscreen -> format -> Bmask );
112
113 #if 0 // audio
114   {
115     SDL_AudioSpec fmt;
116
117     /* Set 16-bit stereo audio at 22Khz */
118     fmt.freq = 44100; //22050;
119     fmt.format = AUDIO_S16; //AUDIO_S16;
120     fmt.channels = 1;
121     fmt.samples = 2048;        /* A good value for games */
122     fmt.callback = mixaudio;
123     fmt.userdata = NULL;
124
125     /* Open the audio device and start playing sound! */
126     if ( SDL_OpenAudio ( &fmt, NULL ) < 0 ) {
127       zotlog ( "Unable to open audio: %s\n", SDL_GetError() );
128       exit ( 1 );
129     }
130
131     SDL_PauseAudio ( 0 );
132   }
133 #endif
134
135   // images
136   //IMG_Init ( IMG_INIT_JPG | IMG_INIT_PNG );
137
138   /* fonts
139    */
140
141   // init
142   if ( TTF_Init() == -1 ) {
143     pnd_log ( pndn_error, "ERROR: Couldn't set up SDL TTF lib\n" );
144     return ( 0 ); // couldn't set up SDL TTF
145   }
146
147   char fullpath [ PATH_MAX ];
148   // big font
149   sprintf ( fullpath, "%s/%s", g_skinpath, pnd_conf_get_as_char ( g_conf, "minimenu.font" ) );
150   g_big_font = TTF_OpenFont ( fullpath, pnd_conf_get_as_int_d ( g_conf, "minimenu.font_ptsize", 24 ) );
151   if ( ! g_big_font ) {
152     pnd_log ( pndn_error, "ERROR: Couldn't load font '%s' for size %u\n",
153               pnd_conf_get_as_char ( g_conf, "minimenu.font" ), pnd_conf_get_as_int_d ( g_conf, "minimenu.font_ptsize", 24 ) );
154     return ( 0 ); // couldn't set up SDL TTF
155   }
156
157   // grid font
158   sprintf ( fullpath, "%s/%s", g_skinpath, pnd_conf_get_as_char ( g_conf, MMENU_GRID_FONT ) );
159   g_grid_font = TTF_OpenFont ( fullpath, pnd_conf_get_as_int_d ( g_conf, MMENU_GRID_FONTSIZE, 10 ) );
160   if ( ! g_grid_font ) {
161     pnd_log ( pndn_error, "ERROR: Couldn't load font '%s' for size %u\n",
162               pnd_conf_get_as_char ( g_conf, MMENU_GRID_FONT ), pnd_conf_get_as_int_d ( g_conf, MMENU_GRID_FONTSIZE, 10 ) );
163     return ( 0 ); // couldn't set up SDL TTF
164   }
165
166   // detailtext font
167   sprintf ( fullpath, "%s/%s", g_skinpath, pnd_conf_get_as_char ( g_conf, "detailtext.font" ) );
168   g_detailtext_font = TTF_OpenFont ( fullpath, pnd_conf_get_as_int_d ( g_conf, "detailtext.font_ptsize", 10 ) );
169   if ( ! g_detailtext_font ) {
170     pnd_log ( pndn_error, "ERROR: Couldn't load font '%s' for size %u\n",
171               pnd_conf_get_as_char ( g_conf, "detailtext.font" ), pnd_conf_get_as_int_d ( g_conf, "detailtext.font_ptsize", 10 ) );
172     return ( 0 ); // couldn't set up SDL TTF
173   }
174
175   // tab font
176   sprintf ( fullpath, "%s/%s", g_skinpath, pnd_conf_get_as_char ( g_conf, "tabs.font" ) );
177   g_tab_font = TTF_OpenFont ( fullpath, pnd_conf_get_as_int_d ( g_conf, "tabs.font_ptsize", 10 ) );
178   if ( ! g_tab_font ) {
179     pnd_log ( pndn_error, "ERROR: Couldn't load font '%s' for size %u\n",
180               pnd_conf_get_as_char ( g_conf, "tabs.font" ), pnd_conf_get_as_int_d ( g_conf, "tabs.font_ptsize", 10 ) );
181     return ( 0 ); // couldn't set up SDL TTF
182   }
183
184   return ( 1 );
185 }
186
187 mm_imgcache_t g_imagecache [ IMG_TRUEMAX ] = {
188   { IMG_BACKGROUND_800480,    "graphics.IMG_BACKGROUND_800480" },
189   { IMG_BACKGROUND_TABMASK,   "graphics.IMG_BACKGROUND_TABMASK" },
190   { IMG_DETAIL_PANEL,         "graphics.IMG_DETAIL_PANEL" },
191   { IMG_DETAIL_BG,            "graphics.IMG_DETAIL_BG" },
192   { IMG_SELECTED_ALPHAMASK,   "graphics.IMG_SELECTED_ALPHAMASK" },
193   { IMG_TAB_SEL,              "graphics.IMG_TAB_SEL" },
194   { IMG_TAB_UNSEL,            "graphics.IMG_TAB_UNSEL" },
195   { IMG_TAB_LINE,             "graphics.IMG_TAB_LINE" },
196   { IMG_TAB_LINEL,            "graphics.IMG_TAB_LINEL" },
197   { IMG_TAB_LINER,            "graphics.IMG_TAB_LINER" },
198   { IMG_ICON_MISSING,         "graphics.IMG_ICON_MISSING" },
199   { IMG_SELECTED_HILITE,      "graphics.IMG_SELECTED_HILITE" },
200   { IMG_PREVIEW_MISSING,      "graphics.IMG_PREVIEW_MISSING" },
201   { IMG_ARROW_UP,             "graphics.IMG_ARROW_UP", },
202   { IMG_ARROW_DOWN,           "graphics.IMG_ARROW_DOWN", },
203   { IMG_ARROW_SCROLLBAR,      "graphics.IMG_ARROW_SCROLLBAR", },
204   { IMG_HOURGLASS,            "graphics.IMG_HOURGLASS", },
205   { IMG_MAX,                  NULL },
206 };
207
208 unsigned char ui_imagecache ( char *basepath ) {
209   unsigned int i;
210   char fullpath [ PATH_MAX ];
211
212   // loaded
213
214   for ( i = 0; i < IMG_MAX; i++ ) {
215
216     if ( g_imagecache [ i ].id != i ) {
217       pnd_log ( pndn_error, "ERROR: Internal table mismatch during caching [%u]\n", i );
218       exit ( -1 );
219     }
220
221     char *filename = pnd_conf_get_as_char ( g_conf, g_imagecache [ i ].confname );
222
223     if ( ! filename ) {
224       pnd_log ( pndn_error, "ERROR: Missing filename in conf for key: %s\n", g_imagecache [ i ].confname );
225       return ( 0 );
226     }
227
228     if ( filename [ 0 ] == '/' ) {
229       strncpy ( fullpath, filename, PATH_MAX );
230     } else {
231       sprintf ( fullpath, "%s/%s", basepath, filename );
232     }
233
234     if ( ! ( g_imagecache [ i ].i = IMG_Load ( fullpath ) ) ) {
235       pnd_log ( pndn_error, "ERROR: Couldn't load static cache image: %s\n", fullpath );
236       return ( 0 );
237     }
238
239   } // for
240
241   // generated
242   //g_imagecache [ IMG_SELECTED_ALPHAMASK ].i = SDL_CreateRGBSurface ( SDL_SWSURFACE, 60, 60, 32, 0xFF0000, 0x00FF00, 0xFF, 0xFF000000 );
243   //boxRGBA ( g_imagecache [ IMG_SELECTED_ALPHAMASK ].i, 0, 0, 60, 60, 100, 100, 100, 250 );
244
245   // post processing
246   //
247
248   // scale icons
249   g_imagecache [ IMG_SELECTED_ALPHAMASK ].i = ui_scale_image ( g_imagecache [ IMG_SELECTED_ALPHAMASK ].i, pnd_conf_get_as_int_d ( g_conf, "grid.icon_max_width", 50 ), -1 );
250   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 );
251   // scale text hilight
252   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 );
253   // scale preview no-pic
254   g_imagecache [ IMG_PREVIEW_MISSING ].i = ui_scale_image ( g_imagecache [ IMG_PREVIEW_MISSING ].i,
255                                                             pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 50 ),
256                                                             pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 50 ) );
257
258   // set alpha on detail panel
259   //SDL_SetAlpha ( g_imagecache [ IMG_DETAIL_BG ].i, SDL_SRCALPHA, pnd_conf_get_as_int_d ( g_conf, "display.detail_bg_alpha", 50 ) );
260
261   return ( 1 );
262 } // ui_imagecache
263
264 void ui_render ( void ) {
265
266   // 800x480:
267   // divide width: 550 / 250
268   // divide left side: 5 columns == 110px width
269   //   20px buffer either side == 70px wide icon + 20 + 20?
270
271   // what jobs to do during render?
272   //
273
274 #define R_BG     (1<<0)
275 #define R_TABS   (1<<1)
276 #define R_DETAIL (1<<2)
277 #define R_GRID   (1<<3)
278
279 #define R_ALL (R_BG|R_TABS|R_DETAIL|R_GRID)
280
281   unsigned int render_jobs_b = 0;
282
283   if ( render_mask & CHANGED_EVERYTHING ) {
284     render_jobs_b |= R_ALL;
285   }
286
287   if ( render_mask & CHANGED_SELECTION ) {
288     render_jobs_b |= R_GRID;
289     render_jobs_b |= R_DETAIL;
290   }
291
292   if ( render_mask & CHANGED_CATEGORY ) {
293     render_jobs_b |= R_ALL;
294   }
295
296   render_mask = CHANGED_NOTHING;
297
298   // render everything
299   //
300   unsigned int icon_rows;
301
302 #define MAXRECTS 200
303   SDL_Rect rects [ MAXRECTS ], src;
304   SDL_Rect *dest = rects;
305   bzero ( dest, sizeof(SDL_Rect)*MAXRECTS );
306
307   unsigned int row, displayrow, col;
308   mm_appref_t *appiter;
309
310   unsigned int screen_width = pnd_conf_get_as_int_d ( g_conf, "display.screen_width", 800 );
311
312   unsigned char row_max = pnd_conf_get_as_int_d ( g_conf, MMENU_DISP_ROWMAX, 4 );
313   unsigned char col_max = pnd_conf_get_as_int_d ( g_conf, MMENU_DISP_COLMAX, 5 );
314
315   unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
316   unsigned int font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
317   unsigned int font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
318   unsigned int font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
319
320   unsigned int grid_offset_x = pnd_conf_get_as_int ( g_conf, "grid.grid_offset_x" );
321   unsigned int grid_offset_y = pnd_conf_get_as_int ( g_conf, "grid.grid_offset_y" );
322
323   unsigned int icon_offset_x = pnd_conf_get_as_int ( g_conf, "grid.icon_offset_x" );
324   unsigned int icon_offset_y = pnd_conf_get_as_int ( g_conf, "grid.icon_offset_y" );
325   unsigned int icon_max_width = pnd_conf_get_as_int ( g_conf, "grid.icon_max_width" );
326   unsigned int icon_max_height = pnd_conf_get_as_int ( g_conf, "grid.icon_max_height" );
327
328   unsigned int text_width = pnd_conf_get_as_int ( g_conf, "grid.text_width" );
329   unsigned int text_clip_x = pnd_conf_get_as_int ( g_conf, "grid.text_clip_x" );
330   unsigned int text_offset_x = pnd_conf_get_as_int ( g_conf, "grid.text_offset_x" );
331   unsigned int text_offset_y = pnd_conf_get_as_int ( g_conf, "grid.text_offset_y" );
332
333   unsigned int cell_width = pnd_conf_get_as_int ( g_conf, "grid.cell_width" );
334   unsigned int cell_height = pnd_conf_get_as_int ( g_conf, "grid.cell_height" );
335
336   // how many total rows do we need?
337   icon_rows = g_categories [ ui_category ].refcount / col_max;
338   if ( g_categories [ ui_category ].refcount % col_max > 0 ) {
339     icon_rows++;
340   }
341
342   // if no selected app yet, select the first one
343 #if 0
344   if ( ! ui_selected ) {
345     ui_selected = g_categories [ ui_category ].refs;
346   }
347 #endif
348
349   // reset touchscreen regions
350   ui_register_reset();
351
352   // ensure selection is visible
353   if ( ui_selected ) {
354
355     int index = ui_selected_index();
356     int topleft = col_max * ui_rows_scrolled_down;
357     int botright = ( col_max * ( ui_rows_scrolled_down + row_max ) - 1 );
358
359     //pnd_log ( PND_LOG_DEFAULT, "index %u tl %u br %u\n", index, topleft, botright );
360
361     if ( index < topleft ) {
362       ui_rows_scrolled_down -= pnd_conf_get_as_int_d ( g_conf, "grid.scroll_increment", 1 );
363       render_jobs_b |= R_ALL;
364     } else if ( index > botright ) {
365       ui_rows_scrolled_down += pnd_conf_get_as_int_d ( g_conf, "grid.scroll_increment", 1 );
366       render_jobs_b |= R_ALL;
367     }
368
369     if ( ui_rows_scrolled_down < 0 ) {
370       ui_rows_scrolled_down = 0;
371     } else if ( ui_rows_scrolled_down > icon_rows ) {
372       ui_rows_scrolled_down = icon_rows;
373     }
374
375   } // ensire visible
376
377   // render background
378   if ( render_jobs_b & R_BG ) {
379
380     if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
381       dest -> x = 0;
382       dest -> y = 0;
383       dest -> w = sdl_realscreen -> w;
384       dest -> h = sdl_realscreen -> h;
385       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, NULL /* whole image */, sdl_realscreen, dest /* 0,0 */ );
386       dest++;
387     }
388
389     // tabmask
390     if ( g_imagecache [ IMG_BACKGROUND_TABMASK ].i ) {
391       dest -> x = 0;
392       dest -> y = 0;
393       dest -> w = sdl_realscreen -> w;
394       dest -> h = sdl_realscreen -> h;
395       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_TABMASK ].i, NULL /* whole image */, sdl_realscreen, dest /* 0,0 */ );
396       dest++;
397     }
398
399   } // r_bg
400
401   // tabs
402   if ( g_imagecache [ IMG_TAB_SEL ].i && g_imagecache [ IMG_TAB_UNSEL ].i ) {
403     unsigned int tab_width = pnd_conf_get_as_int ( g_conf, "tabs.tab_width" );
404     unsigned int tab_height = pnd_conf_get_as_int ( g_conf, "tabs.tab_height" );
405     unsigned int tab_selheight = pnd_conf_get_as_int ( g_conf, "tabs.tab_selheight" );
406     unsigned int tab_offset_x = pnd_conf_get_as_int ( g_conf, "tabs.tab_offset_x" );
407     unsigned int tab_offset_y = pnd_conf_get_as_int ( g_conf, "tabs.tab_offset_y" );
408     unsigned int text_offset_x = pnd_conf_get_as_int ( g_conf, "tabs.text_offset_x" );
409     unsigned int text_offset_y = pnd_conf_get_as_int ( g_conf, "tabs.text_offset_y" );
410     unsigned int text_width = pnd_conf_get_as_int ( g_conf, "tabs.text_width" );
411     unsigned int maxtab = ( screen_width / tab_width ) < g_categorycount ? ( screen_width / tab_width ) + ui_catshift : g_categorycount + ui_catshift;
412     unsigned int maxtabspot = ( screen_width / tab_width );
413
414     // draw tabs with categories
415     for ( col = ui_catshift;
416           col < maxtab;
417           col++ )
418     {
419
420       SDL_Surface *s;
421       if ( col == ui_category ) {
422         s = g_imagecache [ IMG_TAB_SEL ].i;
423       } else {
424         s = g_imagecache [ IMG_TAB_UNSEL ].i;
425       }
426
427       // draw tab
428       src.x = 0;
429       src.y = 0;
430       src.w = tab_width;
431       if ( col == ui_category ) {
432         src.h = tab_selheight;
433       } else {
434         src.h = tab_height;
435       }
436       dest -> x = tab_offset_x + ( (col-ui_catshift) * tab_width );
437       dest -> y = tab_offset_y;
438
439       // store touch info
440       ui_register_tab ( col, dest -> x, dest -> y, tab_width, tab_height );
441
442       if ( render_jobs_b & R_TABS ) {
443         //pnd_log ( pndn_debug, "tab %u at %ux%u\n", col, dest.x, dest.y );
444         SDL_BlitSurface ( s, &src, sdl_realscreen, dest );
445         dest++;
446
447         // draw tab line
448         if ( col == ui_category ) {
449           // no line for selected tab
450         } else {
451           if ( col - ui_catshift == 0 ) {
452             s = g_imagecache [ IMG_TAB_LINEL ].i;
453           } else if ( col - ui_catshift == maxtabspot - 1 ) {
454             s = g_imagecache [ IMG_TAB_LINER ].i;
455           } else {
456             s = g_imagecache [ IMG_TAB_LINE ].i;
457           }
458           dest -> x = tab_offset_x + ( (col-ui_catshift) * tab_width );
459           dest -> y = tab_offset_y + tab_height;
460           SDL_BlitSurface ( s, NULL /* whole image */, sdl_realscreen, dest );
461           dest++;
462         }
463
464         // draw text
465         SDL_Surface *rtext;
466         SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
467         rtext = TTF_RenderText_Blended ( g_tab_font, g_categories [ col ].catname, tmpfontcolor );
468         src.x = 0;
469         src.y = 0;
470         src.w = rtext -> w < text_width ? rtext -> w : text_width;
471         src.h = rtext -> h;
472         dest -> x = tab_offset_x + ( (col-ui_catshift) * tab_width ) + text_offset_x;
473         dest -> y = tab_offset_y + text_offset_y;
474         SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
475         SDL_FreeSurface ( rtext );
476         dest++;
477
478       } // r_tabs
479
480     } // for
481
482     if ( render_jobs_b & R_TABS ) {
483
484       // draw tab lines under where tabs would be if we had categories
485       for ( /* foo */; col < maxtabspot; col++ ) {
486         SDL_Surface *s;
487
488         if ( col - ui_catshift == 0 ) {
489           s = g_imagecache [ IMG_TAB_LINEL ].i;
490         } else if ( col - ui_catshift == maxtabspot - 1 ) {
491           s = g_imagecache [ IMG_TAB_LINER ].i;
492         } else {
493           s = g_imagecache [ IMG_TAB_LINE ].i;
494         }
495         dest -> x = tab_offset_x + ( (col-ui_catshift) * tab_width );
496         dest -> y = tab_offset_y + tab_height;
497         SDL_BlitSurface ( s, NULL /* whole image */, sdl_realscreen, dest );
498         dest++;
499
500       } // for
501
502     } // r_tabs
503
504   } // tabs
505
506   // scroll bars and arrows
507   if ( render_jobs_b & R_BG ) {
508     unsigned char show_bar = 0;
509
510     // up?
511     if ( ui_rows_scrolled_down && g_imagecache [ IMG_ARROW_UP ].i ) {
512       dest -> x = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_up_x", 450 );
513       dest -> y = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_up_y", 80 );
514       SDL_BlitSurface ( g_imagecache [ IMG_ARROW_UP ].i, NULL /* whole image */, sdl_realscreen, dest );
515       dest++;
516
517       show_bar = 1;
518     }
519
520     // down?
521     if ( ui_rows_scrolled_down + row_max < icon_rows && g_imagecache [ IMG_ARROW_DOWN ].i ) {
522       dest -> x = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_down_x", 450 );
523       dest -> y = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_down_y", 80 );
524       SDL_BlitSurface ( g_imagecache [ IMG_ARROW_DOWN ].i, NULL /* whole image */, sdl_realscreen, dest );
525       dest++;
526
527       show_bar = 1;
528     }
529
530     if ( show_bar ) {
531       // show scrollbar as well
532       src.x = 0;
533       src.y = 0;
534       src.w = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_clip_w", 10 );
535       src.h = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_clip_h", 100 );
536       dest -> x = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_x", 450 );
537       dest -> y = pnd_conf_get_as_int_d ( g_conf, "grid.arrow_bar_y", 100 );
538       SDL_BlitSurface ( g_imagecache [ IMG_ARROW_SCROLLBAR ].i, &src /* whole image */, sdl_realscreen, dest );
539       dest++;
540     } // bar
541
542   } // r_bg, scroll bars
543
544   // render detail pane bg
545   if ( render_jobs_b & R_DETAIL ) {
546
547     if ( pnd_conf_get_as_int_d ( g_conf, "detailpane.show", 1 ) ) {
548
549       // render detail bg
550       if ( g_imagecache [ IMG_DETAIL_BG ].i ) {
551         src.x = 0; // pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
552         src.y = 0; // pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
553         src.w = ((SDL_Surface*)(g_imagecache [ IMG_DETAIL_PANEL ].i)) -> w;
554         src.h = ((SDL_Surface*)(g_imagecache [ IMG_DETAIL_PANEL ].i)) -> h;
555         dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
556         dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
557         SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
558         dest++;
559       }
560
561       // render detail pane
562       if ( g_imagecache [ IMG_DETAIL_PANEL ].i ) {
563         dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
564         dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
565         SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_PANEL ].i, NULL /* whole image */, sdl_realscreen, dest );
566         dest++;
567       }
568
569     } // detailpane frame/bg
570
571   } // r_details
572
573   // anything to render?
574   if ( render_jobs_b & R_GRID ) {
575
576     // if just rendering grid, and nothing else, better clear it first
577     if ( ! ( render_jobs_b & R_BG ) ) {
578       if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
579         src.x = grid_offset_x;
580         src.y = grid_offset_y;
581         src.w = col_max * cell_width;
582         src.h = row_max * cell_height;
583
584         dest -> x = grid_offset_x;
585         dest -> y = grid_offset_y;
586
587         SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, &src, sdl_realscreen, dest );
588         dest++;
589
590       }
591     }
592
593     if ( g_categories [ ui_category ].refs ) {
594
595       appiter = g_categories [ ui_category ].refs;
596       row = 0;
597       displayrow = 0;
598
599       // until we run out of apps, or run out of space
600       while ( appiter != NULL ) {
601
602         for ( col = 0; col < col_max && appiter != NULL; col++ ) {
603
604           // do we even need to render it? or are we suppressing it due to rows scrolled off the top?
605           if ( row >= ui_rows_scrolled_down ) {
606
607             // selected? show hilights
608             if ( appiter == ui_selected ) {
609               // icon
610               dest -> x = grid_offset_x + ( col * cell_width ) + icon_offset_x;
611               dest -> y = grid_offset_y + ( displayrow * cell_height ) + icon_offset_y;
612               SDL_BlitSurface ( g_imagecache [ IMG_SELECTED_ALPHAMASK ].i, NULL /* all */, sdl_realscreen, dest );
613               dest++;
614               // text
615               dest -> x = grid_offset_x + ( col * cell_width ) + text_clip_x;
616               dest -> y = grid_offset_y + ( displayrow * cell_height ) + pnd_conf_get_as_int ( g_conf, "grid.text_hilite_offset_y" );
617               SDL_BlitSurface ( g_imagecache [ IMG_SELECTED_HILITE ].i, NULL /* all */, sdl_realscreen, dest );
618               dest++;
619             } // selected?
620
621             // show icon
622             mm_cache_t *ic = cache_query_icon ( appiter -> ref -> unique_id );
623             SDL_Surface *iconsurface;
624             if ( ic ) {
625               iconsurface = ic -> i;
626             } else {
627               //pnd_log ( pndn_warning, "WARNING: TBD: Need Missin-icon icon for '%s'\n", IFNULL(appiter -> ref -> title_en,"No Name") );
628               iconsurface = g_imagecache [ IMG_ICON_MISSING ].i;
629             }
630             if ( iconsurface ) {
631               //pnd_log ( pndn_debug, "Got an icon for '%s'\n", IFNULL(appiter -> ref -> title_en,"No Name") );
632
633               src.x = 0;
634               src.y = 0;
635               src.w = 60;
636               src.h = 60;
637               dest -> x = grid_offset_x + ( col * cell_width ) + icon_offset_x + (( icon_max_width - iconsurface -> w ) / 2);
638               dest -> y = grid_offset_y + ( displayrow * cell_height ) + icon_offset_y + (( icon_max_height - iconsurface -> h ) / 2);
639
640               SDL_BlitSurface ( iconsurface, &src, sdl_realscreen, dest );
641
642               // store touch info
643               ui_register_app ( appiter, dest -> x, dest -> y, src.w, src.h );
644
645               dest++;
646
647             }
648
649             // show text
650             if ( appiter -> ref -> title_en ) {
651               SDL_Surface *rtext;
652               SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
653               rtext = TTF_RenderText_Blended ( g_grid_font, appiter -> ref -> title_en, tmpfontcolor );
654               src.x = 0;
655               src.y = 0;
656               src.w = text_width < rtext -> w ? text_width : rtext -> w;
657               src.h = rtext -> h;
658               if ( rtext -> w > text_width ) {
659                 dest -> x = grid_offset_x + ( col * cell_width ) + text_clip_x;
660               } else {
661                 dest -> x = grid_offset_x + ( col * cell_width ) + text_offset_x - ( rtext -> w / 2 );
662               }
663               dest -> y = grid_offset_y + ( displayrow * cell_height ) + text_offset_y;
664               SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
665               SDL_FreeSurface ( rtext );
666               dest++;
667             }
668
669           } // display now? or scrolled away..
670
671           // next
672           appiter = appiter -> next;
673
674         } // for column 1...X
675
676         if ( row >= ui_rows_scrolled_down ) {
677           displayrow++;
678         }
679
680         row ++;
681
682         // are we done displaying rows?
683         if ( displayrow >= row_max ) {
684           break;
685         }
686
687       } // while
688
689     } else {
690       // no apps to render?
691       pnd_log ( pndn_rem, "No applications to render?\n" );
692     } // apps to renser?
693
694   } // r_grid
695
696   // detail panel
697   if ( ui_selected && render_jobs_b & R_DETAIL ) {
698
699     unsigned int cell_offset_x = pnd_conf_get_as_int ( g_conf, "detailtext.cell_offset_x" );
700     unsigned int cell_offset_y = pnd_conf_get_as_int ( g_conf, "detailtext.cell_offset_y" );
701     unsigned int cell_width = pnd_conf_get_as_int ( g_conf, "detailtext.cell_width" );
702
703     unsigned int desty = cell_offset_y;
704
705     char buffer [ 256 ];
706
707     // full name
708     if ( ui_selected -> ref -> title_en ) {
709       SDL_Surface *rtext;
710       SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
711       rtext = TTF_RenderText_Blended ( g_detailtext_font, ui_selected -> ref -> title_en, tmpfontcolor );
712       src.x = 0;
713       src.y = 0;
714       src.w = rtext -> w < cell_width ? rtext -> w : cell_width;
715       src.h = rtext -> h;
716       dest -> x = cell_offset_x;
717       dest -> y = desty;
718       SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
719       SDL_FreeSurface ( rtext );
720       dest++;
721       desty += src.h;
722     }
723
724     // category
725     if ( ui_selected -> ref -> main_category ) {
726
727       sprintf ( buffer, "Category: %s", ui_selected -> ref -> main_category );
728
729       SDL_Surface *rtext;
730       SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
731       rtext = TTF_RenderText_Blended ( g_detailtext_font, buffer, tmpfontcolor );
732       src.x = 0;
733       src.y = 0;
734       src.w = rtext -> w < cell_width ? rtext -> w : cell_width;
735       src.h = rtext -> h;
736       dest -> x = cell_offset_x;
737       dest -> y = desty;
738       SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
739       SDL_FreeSurface ( rtext );
740       dest++;
741       desty += src.h;
742     }
743
744     // clock
745     if ( ui_selected -> ref -> clockspeed ) {
746
747       sprintf ( buffer, "CPU Clock: %s", ui_selected -> ref -> clockspeed );
748
749       SDL_Surface *rtext;
750       SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
751       rtext = TTF_RenderText_Blended ( g_detailtext_font, buffer, tmpfontcolor );
752       src.x = 0;
753       src.y = 0;
754       src.w = rtext -> w < cell_width ? rtext -> w : cell_width;
755       src.h = rtext -> h;
756       dest -> x = cell_offset_x;
757       dest -> y = desty;
758       SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
759       SDL_FreeSurface ( rtext );
760       dest++;
761       desty += src.h;
762     }
763
764     // show sub-app# on right side of cpu clock?
765     //if ( ui_selected -> ref -> subapp_number )
766     {
767       sprintf ( buffer, "(app#%u)", ui_selected -> ref -> subapp_number );
768
769       SDL_Surface *rtext;
770       SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
771       rtext = TTF_RenderText_Blended ( g_grid_font, buffer, tmpfontcolor );
772       dest -> x = cell_offset_x + cell_width - rtext -> w;
773       dest -> y = desty - src.h;
774       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
775       SDL_FreeSurface ( rtext );
776       dest++;
777     }
778
779     // info hint
780     if ( ui_selected -> ref -> clockspeed && ui_selected -> ref -> info_filename ) {
781
782       sprintf ( buffer, "Documentation - hit Y" );
783
784       SDL_Surface *rtext;
785       SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
786       rtext = TTF_RenderText_Blended ( g_detailtext_font, buffer, tmpfontcolor );
787       src.x = 0;
788       src.y = 0;
789       src.w = rtext -> w < cell_width ? rtext -> w : cell_width;
790       src.h = rtext -> h;
791       dest -> x = cell_offset_x;
792       dest -> y = desty;
793       SDL_BlitSurface ( rtext, &src, sdl_realscreen, dest );
794       SDL_FreeSurface ( rtext );
795       dest++;
796       desty += src.h;
797     }
798
799     // preview pic
800     mm_cache_t *ic = cache_query_preview ( ui_selected -> ref -> unique_id );
801     SDL_Surface *previewpic;
802
803     if ( ic ) {
804       previewpic = ic -> i;
805     } else {
806       previewpic = g_imagecache [ IMG_PREVIEW_MISSING ].i;
807     }
808
809     if ( previewpic ) {
810       dest -> x = pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_offset_x", 50 ) +
811         ( ( pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 50 ) - previewpic -> w ) / 2 );
812       dest -> y = pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_offset_y", 50 );
813       SDL_BlitSurface ( previewpic, NULL /* whole image */, sdl_realscreen, dest );
814       dest++;
815     }
816
817   } // selected?
818
819   // extras
820   //
821
822   // battery
823   if ( 1 ) {
824     static int last_battlevel = 0;
825     static unsigned char batterylevel = 0;
826     char buffer [ 100 ];
827
828     if ( time ( NULL ) - last_battlevel > 60 ) {
829       batterylevel = pnd_device_get_battery_gauge_perc();
830       last_battlevel = time ( NULL );
831     }
832
833     sprintf ( buffer, "Battery: %u%%", batterylevel );
834
835     SDL_Surface *rtext;
836     SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
837     rtext = TTF_RenderText_Blended ( g_grid_font, buffer, tmpfontcolor );
838     dest -> x = pnd_conf_get_as_int_d ( g_conf, "display.battery_x", 20 );
839     dest -> y = pnd_conf_get_as_int_d ( g_conf, "display.battery_y", 450 );
840     SDL_BlitSurface ( rtext, NULL /* all */, sdl_realscreen, dest );
841     SDL_FreeSurface ( rtext );
842     dest++;
843   }
844
845   // hints
846   if ( pnd_conf_get_as_char ( g_conf, "display.hintline" ) ) {
847     char *buffer = pnd_conf_get_as_char ( g_conf, "display.hintline" );
848     SDL_Surface *rtext;
849     SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
850     rtext = TTF_RenderText_Blended ( g_grid_font, buffer, tmpfontcolor );
851     dest -> x = pnd_conf_get_as_int_d ( g_conf, "display.hint_x", 40 );
852     dest -> y = pnd_conf_get_as_int_d ( g_conf, "display.hint_y", 450 );
853     SDL_BlitSurface ( rtext, NULL /* all */, sdl_realscreen, dest );
854     SDL_FreeSurface ( rtext );
855     dest++;
856   }
857
858   // hints
859   if ( pnd_conf_get_as_int_d ( g_conf, "display.clock_x", -1 ) != -1 ) {
860     char buffer [ 50 ];
861
862     time_t t = time ( NULL );
863     struct tm *tm = localtime ( &t );
864     strftime ( buffer, 50, "%a %H:%M %F", tm );
865
866     SDL_Surface *rtext;
867     SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
868     rtext = TTF_RenderText_Blended ( g_grid_font, buffer, tmpfontcolor );
869     dest -> x = pnd_conf_get_as_int_d ( g_conf, "display.clock_x", 700 );
870     dest -> y = pnd_conf_get_as_int_d ( g_conf, "display.clock_y", 450 );
871     SDL_BlitSurface ( rtext, NULL /* all */, sdl_realscreen, dest );
872     SDL_FreeSurface ( rtext );
873     dest++;
874   }
875
876   // update all the rects and send it all to sdl
877   SDL_UpdateRects ( sdl_realscreen, dest - rects, rects );
878
879 } // ui_render
880
881 void ui_process_input ( unsigned char block_p ) {
882   SDL_Event event;
883
884   unsigned char ui_event = 0; // if we get a ui event, flip to 1 and break
885   //static ui_sdl_button_e ui_mask = uisb_none; // current buttons down
886
887   while ( ! ui_event &&
888           block_p ? SDL_WaitEvent ( &event ) : SDL_PollEvent ( &event ) )
889   {
890
891     switch ( event.type ) {
892
893     case SDL_USEREVENT:
894       // update something
895
896       if ( event.user.code == sdl_user_ticker ) {
897
898         // timer went off, time to load something
899         if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_previews_later", 0 ) ) {
900
901           // load the preview pics now!
902           pnd_disco_t *iter = ui_selected -> ref;
903
904           if ( iter -> preview_pic1 ) {
905
906             if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.threaded_preview", 0 ) ) {
907               // load in bg thread, make user experience chuggy
908
909               g_preview_thread = SDL_CreateThread ( (void*)ui_threaded_defered_preview, iter );
910
911               if ( ! g_preview_thread ) {
912                 pnd_log ( pndn_error, "ERROR: Couldn't create preview thread\n" );
913               }
914
915             } else {
916               // load it now, make user wait
917
918               if ( ! cache_preview ( iter, pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 200 ),
919                                      pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 180 ) )
920                  )
921               {
922                 pnd_log ( pndn_debug, "  Couldn't load preview pic: '%s' -> '%s'\n",
923                           IFNULL(iter->title_en,"No Name"), iter -> preview_pic1 );
924               }
925
926             } // threaded?
927
928           } // got a preview at all?
929
930           ui_event++;
931         }
932
933       } else if ( event.user.code == sdl_user_finishedpreview ) {
934
935         // if we just finished the one we happen to be looking at, better redraw now; otherwise, if
936         // we finished another, no big woop
937         if ( ui_selected && event.user.data1 == ui_selected -> ref ) {
938           ui_event++;
939         }
940
941       } else if ( event.user.code == sdl_user_finishedicon ) {
942         // redraw, so we can show the newly loaded icon
943         ui_event++;
944
945       }
946
947       render_mask |= CHANGED_EVERYTHING;
948
949       break;
950
951 #if 0 // joystick motion
952     case SDL_JOYAXISMOTION:
953
954       pnd_log ( PND_LOG_DEFAULT, "joystick axis\n" );
955
956         if ( event.jaxis.axis == 0 ) {
957           // horiz
958           if ( event.jaxis.value < 0 ) {
959             ui_push_left ( 0 );
960             pnd_log ( PND_LOG_DEFAULT, "joystick axis - LEFT\n" );
961           } else if ( event.jaxis.value > 0 ) {
962             ui_push_right ( 0 );
963             pnd_log ( PND_LOG_DEFAULT, "joystick axis - RIGHT\n" );
964           }
965         } else if ( event.jaxis.axis == 1 ) {
966           // vert
967           if ( event.jaxis.value < 0 ) {
968             ui_push_up();
969           } else if ( event.jaxis.value > 0 ) {
970             ui_push_down();
971           }
972         }
973
974         ui_event++;
975
976         break;
977 #endif
978
979 #if 0 // joystick buttons
980     case SDL_JOYBUTTONDOWN:
981
982       pnd_log ( PND_LOG_DEFAULT, "joystick button down %u\n", event.jbutton.button );
983
984       if ( event.jbutton.button == 0 ) { // B
985         ui_mask |= uisb_b;
986       } else if ( event.jbutton.button == 1 ) { // Y
987         ui_mask |= uisb_y;
988       } else if ( event.jbutton.button == 2 ) { // X
989         ui_mask |= uisb_x;
990       } else if ( event.jbutton.button == 3 ) { // A
991         ui_mask |= uisb_a;
992
993       } else if ( event.jbutton.button == 4 ) { // Select
994         ui_mask |= uisb_select;
995       } else if ( event.jbutton.button == 5 ) { // Start
996         ui_mask |= uisb_start;
997
998       } else if ( event.jbutton.button == 7 ) { // L
999         ui_mask |= uisb_l;
1000       } else if ( event.jbutton.button == 8 ) { // R
1001         ui_mask |= uisb_r;
1002
1003       }
1004
1005       ui_event++;
1006
1007       break;
1008
1009     case SDL_JOYBUTTONUP:
1010
1011       pnd_log ( PND_LOG_DEFAULT, "joystick button up %u\n", event.jbutton.button );
1012
1013       if ( event.jbutton.button == 0 ) { // B
1014         ui_mask &= ~uisb_b;
1015         ui_push_exec();
1016       } else if ( event.jbutton.button == 1 ) { // Y
1017         ui_mask &= ~uisb_y;
1018       } else if ( event.jbutton.button == 2 ) { // X
1019         ui_mask &= ~uisb_x;
1020       } else if ( event.jbutton.button == 3 ) { // A
1021         ui_mask &= ~uisb_a;
1022
1023       } else if ( event.jbutton.button == 4 ) { // Select
1024         ui_mask &= ~uisb_select;
1025       } else if ( event.jbutton.button == 5 ) { // Start
1026         ui_mask &= ~uisb_start;
1027
1028       } else if ( event.jbutton.button == 7 ) { // L
1029         ui_mask &= ~uisb_l;
1030         ui_push_ltrigger();
1031       } else if ( event.jbutton.button == 8 ) { // R
1032         ui_mask &= ~uisb_r;
1033         ui_push_rtrigger();
1034
1035       }
1036
1037       ui_event++;
1038
1039       break;
1040 #endif
1041
1042 #if 1 // keyboard events
1043     case SDL_KEYUP:
1044
1045       //pnd_log ( pndn_debug, "key up %u\n", event.key.keysym.sym );
1046
1047       // SDLK_LALT -> Start
1048
1049       // directional
1050       if ( event.key.keysym.sym == SDLK_RIGHT ) {
1051         ui_push_right ( 0 );
1052         ui_event++;
1053       } else if ( event.key.keysym.sym == SDLK_LEFT ) {
1054         ui_push_left ( 0 );
1055         ui_event++;
1056       } else if ( event.key.keysym.sym == SDLK_UP ) {
1057         ui_push_up();
1058         ui_event++;
1059       } else if ( event.key.keysym.sym == SDLK_DOWN ) {
1060         ui_push_down();
1061         ui_event++;
1062       } else if ( event.key.keysym.sym == SDLK_SPACE || event.key.keysym.sym == SDLK_END ) {
1063         ui_push_exec();
1064         ui_event++;
1065       } else if ( event.key.keysym.sym == SDLK_z || event.key.keysym.sym == SDLK_RSHIFT ) {
1066         ui_push_ltrigger();
1067         ui_event++;
1068       } else if ( event.key.keysym.sym == SDLK_x || event.key.keysym.sym == SDLK_RCTRL ) {
1069         ui_push_rtrigger();
1070         ui_event++;
1071       } else if ( event.key.keysym.sym == SDLK_y || event.key.keysym.sym == SDLK_PAGEUP ) {
1072         // info
1073         if ( ui_selected ) {
1074           ui_show_info ( pnd_run_script, ui_selected -> ref );
1075           ui_event++;
1076         }
1077
1078       } else if ( event.key.keysym.sym == SDLK_LALT ) { // start button
1079         ui_push_exec();
1080         ui_event++;
1081
1082       } else if ( event.key.keysym.sym == SDLK_LCTRL /*LALT*/ ) { // select button
1083         char *opts [ 20 ] = {
1084           "Return to Minimenu",
1085           "Shutdown Pandora",
1086           "Rescan for Applications",
1087           "Cache previews to SD now",
1088           "Run xfce4 from Minimenu",
1089           "Run a terminal/console",
1090           "Exit and run xfce4",
1091           "Exit and run pmenu",
1092           "Quit (<- beware)",
1093           "About Minimenu"
1094         };
1095         int sel = ui_modal_single_menu ( opts, 9, "Minimenu", "Enter to select; other to return." );
1096
1097         char buffer [ 100 ];
1098         if ( sel == 0 ) {
1099           // do nothing
1100         } else if ( sel == 1 ) {
1101           // shutdown
1102           sprintf ( buffer, "sudo poweroff" );
1103           system ( buffer );
1104         } else if ( sel == 2 ) {
1105           // rescan apps
1106           pnd_log ( pndn_debug, "Freeing up applications\n" );
1107           applications_free();
1108           pnd_log ( pndn_debug, "Rescanning applications\n" );
1109           applications_scan();
1110           // reset view
1111           ui_selected = NULL;
1112           ui_rows_scrolled_down = 0;
1113         } else if ( sel == 3 ) {
1114           // cache preview to SD now
1115           extern pnd_box_handle g_active_apps;
1116           pnd_box_handle h = g_active_apps;
1117
1118           unsigned int maxwidth, maxheight;
1119           maxwidth = pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 200 );
1120           maxheight = pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 180 );
1121
1122           pnd_disco_t *iter = pnd_box_get_head ( h );
1123
1124           while ( iter ) {
1125
1126             // cache it
1127             if ( ! cache_preview ( iter, maxwidth, maxheight ) ) {
1128               pnd_log ( pndn_debug, "Force cache: Couldn't load preview pic: '%s' -> '%s'\n",
1129                         IFNULL(iter->title_en,"No Name"), iter -> preview_pic1 );
1130             }
1131
1132             // next
1133             iter = pnd_box_get_next ( iter );
1134           } // while
1135
1136         } else if ( sel == 4 ) {
1137           // run xfce
1138           char buffer [ PATH_MAX ];
1139           sprintf ( buffer, "%s %s\n", MM_RUN, "/usr/bin/startxfce4" );
1140           emit_and_quit ( buffer );
1141         } else if ( sel == 5 ) {
1142           // run terminal
1143           char *argv[5];
1144           argv [ 0 ] = pnd_conf_get_as_char ( g_conf, "utility.terminal" );
1145           argv [ 1 ] = NULL;
1146
1147           if ( argv [ 0 ] ) {
1148             ui_forkexec ( argv );
1149           }
1150
1151         } else if ( sel == 6 ) {
1152           // set env to xfce
1153           sprintf ( buffer, "echo startxfce4 > /tmp/gui.load" );
1154           system ( buffer );
1155           //sprintf ( buffer, "sudo poweroff" );
1156           //system ( buffer );
1157           exit ( 0 );
1158         } else if ( sel == 7 ) {
1159           // set env to pmenu
1160           sprintf ( buffer, "echo pmenu > /tmp/gui.load" );
1161           system ( buffer );
1162           //sprintf ( buffer, "sudo poweroff" );
1163           //system ( buffer );
1164           exit ( 0 );
1165         } else if ( sel == 8 ) {
1166           emit_and_quit ( MM_QUIT );
1167         } else if ( sel == 9 ) {
1168           // about
1169         }
1170
1171         ui_event++;
1172         render_mask |= CHANGED_EVERYTHING;
1173       }
1174
1175       // extras
1176       if ( event.key.keysym.sym == SDLK_q ) {
1177         emit_and_quit ( MM_QUIT );
1178       }
1179
1180       break;
1181 #endif
1182
1183 #if 1 // mouse / touchscreen
1184 #if 0
1185     case SDL_MOUSEBUTTONDOWN:
1186       if ( event.button.button == SDL_BUTTON_LEFT ) {
1187         cb_pointer_press ( gc, event.button.x / g_scale, event.button.y / g_scale );
1188         ui_event++;
1189       }
1190       break;
1191 #endif
1192
1193     case SDL_MOUSEBUTTONUP:
1194       if ( event.button.button == SDL_BUTTON_LEFT ) {
1195         ui_touch_act ( event.button.x, event.button.y );
1196         ui_event++;
1197       }
1198       break;
1199 #endif
1200
1201     case SDL_QUIT:
1202       exit ( 0 );
1203       break;
1204
1205     default:
1206       break;
1207
1208     } // switch event type
1209
1210   } // while poll
1211
1212   return;
1213 }
1214
1215 void ui_push_left ( unsigned char forcecoil ) {
1216
1217   if ( ! ui_selected ) {
1218     ui_push_right ( 0 );
1219     return;
1220   }
1221
1222   // what column we in?
1223   unsigned int col = ui_determine_screen_col ( ui_selected );
1224
1225   // are we alreadt at first item?
1226   if ( forcecoil == 0 &&
1227        pnd_conf_get_as_int_d ( g_conf, "grid.wrap_horiz_samerow", 0 ) &&
1228        col == 0 )
1229   {
1230     unsigned int i = pnd_conf_get_as_int_d ( g_conf, "grid.col_max", 5 ) - 1;
1231     while ( i ) {
1232       ui_push_right ( 0 );
1233       i--;
1234     }
1235
1236   } else if ( g_categories [ ui_category ].refs == ui_selected ) {
1237     // can't go any more left, we're at the head
1238
1239   } else {
1240     // figure out the previous item; yay for singly linked list :/
1241     mm_appref_t *i = g_categories [ ui_category ].refs;
1242     while ( i ) {
1243       if ( i -> next == ui_selected ) {
1244         ui_selected = i;
1245         break;
1246       }
1247       i = i -> next;
1248     }
1249   }
1250
1251   ui_set_selected ( ui_selected );
1252
1253   return;
1254 }
1255
1256 void ui_push_right ( unsigned char forcecoil ) {
1257
1258   if ( ui_selected ) {
1259
1260     // what column we in?
1261     unsigned int col = ui_determine_screen_col ( ui_selected );
1262
1263     // wrap same or no?
1264     if ( forcecoil == 0 &&
1265          pnd_conf_get_as_int_d ( g_conf, "grid.wrap_horiz_samerow", 0 ) &&
1266          col == pnd_conf_get_as_int_d ( g_conf, "grid.col_max", 5 ) - 1 )
1267     {
1268       // same wrap
1269       unsigned int i = pnd_conf_get_as_int_d ( g_conf, "grid.col_max", 5 ) - 1;
1270       while ( i ) {
1271         ui_push_left ( 0 );
1272         i--;
1273       }
1274
1275     } else {
1276       // just go to the next
1277
1278       if ( ui_selected -> next ) {
1279         ui_selected = ui_selected -> next;
1280       }
1281
1282     }
1283
1284   } else {
1285     ui_selected = g_categories [ ui_category ].refs;
1286   }
1287
1288   ui_set_selected ( ui_selected );
1289
1290   return;
1291 }
1292
1293 void ui_push_up ( void ) {
1294   unsigned char col_max = pnd_conf_get_as_int ( g_conf, MMENU_DISP_COLMAX );
1295
1296   if ( ! ui_selected ) {
1297     return;
1298   }
1299
1300   // what row we in?
1301   unsigned int row = ui_determine_row ( ui_selected );
1302
1303   if ( row == 0 &&
1304        pnd_conf_get_as_int_d ( g_conf, "grid.wrap_vert_stop", 1 ) == 0 )
1305   {
1306     // wrap around instead
1307
1308     unsigned int col = ui_determine_screen_col ( ui_selected );
1309
1310     // go to end
1311     ui_selected = g_categories [ ui_category ].refs;
1312     while ( ui_selected -> next ) {
1313       ui_selected = ui_selected -> next;
1314     }
1315
1316     // try to move to same column
1317     unsigned int newcol = ui_determine_screen_col ( ui_selected );
1318     if ( newcol > col ) {
1319       col = newcol - col;
1320       while ( col ) {
1321         ui_push_left ( 0 );
1322         col--;
1323       }
1324     }
1325
1326     // scroll down to show it
1327     int r = ui_determine_row ( ui_selected ) - 1;
1328     if ( r - pnd_conf_get_as_int ( g_conf, MMENU_DISP_ROWMAX ) > 0 ) {
1329       ui_rows_scrolled_down = (unsigned int) r;
1330     }
1331
1332   } else {
1333     // stop at top/bottom
1334
1335     while ( col_max ) {
1336       ui_push_left ( 1 );
1337       col_max--;
1338     }
1339
1340   }
1341
1342   return;
1343 }
1344
1345 void ui_push_down ( void ) {
1346   unsigned char col_max = pnd_conf_get_as_int ( g_conf, MMENU_DISP_COLMAX );
1347
1348   if ( ui_selected ) {
1349
1350     // what row we in?
1351     unsigned int row = ui_determine_row ( ui_selected );
1352
1353     // max rows?
1354     unsigned int icon_rows = g_categories [ ui_category ].refcount / col_max;
1355     if ( g_categories [ ui_category ].refcount % col_max > 0 ) {
1356       icon_rows++;
1357     }
1358
1359     // we at the end?
1360     if ( row == ( icon_rows - 1 ) &&
1361          pnd_conf_get_as_int_d ( g_conf, "grid.wrap_vert_stop", 1 ) == 0 )
1362     {
1363
1364       unsigned char col = ui_determine_screen_col ( ui_selected );
1365
1366       ui_selected = g_categories [ ui_category ].refs;
1367
1368       while ( col ) {
1369         ui_selected = ui_selected -> next;
1370         col--;
1371       }
1372
1373       ui_rows_scrolled_down = 0;
1374
1375     } else {
1376
1377       while ( col_max ) {
1378         ui_push_right ( 1 );
1379         col_max--;
1380       }
1381
1382     }
1383
1384   } else {
1385     ui_push_right ( 0 );
1386   }
1387
1388   return;
1389 }
1390
1391 void ui_push_exec ( void ) {
1392
1393   if ( ui_selected ) {
1394     char buffer [ PATH_MAX ];
1395     sprintf ( buffer, "%s/%s", ui_selected -> ref -> object_path, ui_selected -> ref -> object_filename );
1396     pnd_apps_exec ( pnd_run_script,
1397                     buffer,
1398                     ui_selected -> ref -> unique_id,
1399                     ui_selected -> ref -> exec,
1400                     ui_selected -> ref -> startdir,
1401                     ui_selected -> ref -> execargs,
1402                     ui_selected -> ref -> clockspeed ? atoi ( ui_selected -> ref -> clockspeed ) : 0,
1403                     PND_EXEC_OPTION_NORUN );
1404     sprintf ( buffer, "%s %s\n", MM_RUN, pnd_apps_exec_runline() );
1405     emit_and_quit ( buffer );
1406   }
1407
1408   return;
1409 }
1410
1411 void ui_push_ltrigger ( void ) {
1412   unsigned char oldcat = ui_category;
1413
1414   if ( ui_category > 0 ) {
1415     ui_category--;
1416   } else {
1417     if ( pnd_conf_get_as_int_d ( g_conf, "tabs.wraparound", 0 ) > 0 ) {
1418       ui_category = g_categorycount - 1;
1419     }
1420   }
1421
1422   if ( oldcat != ui_category ) {
1423     ui_selected = NULL;
1424     ui_set_selected ( ui_selected );
1425   }
1426
1427   // make tab visible?
1428   if ( ui_catshift > 0 && ui_category == ui_catshift - 1 ) {
1429     ui_catshift--;
1430   }
1431
1432   // unscroll
1433   ui_rows_scrolled_down = 0;
1434
1435   render_mask |= CHANGED_CATEGORY;
1436
1437   return;
1438 }
1439
1440 void ui_push_rtrigger ( void ) {
1441   unsigned char oldcat = ui_category;
1442
1443   unsigned int screen_width = pnd_conf_get_as_int_d ( g_conf, "display.screen_width", 800 );
1444   unsigned int tab_width = pnd_conf_get_as_int ( g_conf, "tabs.tab_width" );
1445
1446   if ( ui_category < ( g_categorycount - 1 ) ) {
1447     ui_category++;
1448   } else {
1449     if ( pnd_conf_get_as_int_d ( g_conf, "tabs.wraparound", 0 ) > 0 ) {
1450       ui_category = 0;
1451     }
1452   }
1453
1454   if ( oldcat != ui_category ) {
1455     ui_selected = NULL;
1456     ui_set_selected ( ui_selected );
1457   }
1458
1459   // make tab visible?
1460   if ( ui_category > ui_catshift + ( screen_width / tab_width ) - 1 ) {
1461     ui_catshift++;
1462   }
1463
1464   // unscroll
1465   ui_rows_scrolled_down = 0;
1466
1467   render_mask |= CHANGED_CATEGORY;
1468
1469   return;
1470 }
1471
1472 SDL_Surface *ui_scale_image ( SDL_Surface *s, unsigned int maxwidth, int maxheight ) {
1473   double scale = 1000000.0;
1474   double scalex = 1000000.0;
1475   double scaley = 1000000.0;
1476   SDL_Surface *scaled;
1477
1478   scalex = (double)maxwidth / (double)s -> w;
1479
1480   if ( maxheight == -1 ) {
1481     scale = scalex;
1482   } else {
1483     scaley = (double)maxheight / (double)s -> h;
1484
1485     if ( scaley < scalex ) {
1486       scale = scaley;
1487     } else {
1488       scale = scalex;
1489     }
1490
1491   }
1492
1493   scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
1494   SDL_FreeSurface ( s );
1495   s = scaled;
1496
1497   return ( s );
1498 }
1499
1500 void ui_loadscreen ( void ) {
1501
1502   SDL_Rect dest;
1503
1504   unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
1505   unsigned int font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
1506   unsigned int font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
1507   unsigned int font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
1508
1509   // clear the screen
1510   SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
1511
1512   // render text
1513   SDL_Surface *rtext;
1514   SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
1515   rtext = TTF_RenderText_Blended ( g_big_font, "Setting up menu...", tmpfontcolor );
1516   dest.x = 20;
1517   dest.y = 20;
1518   SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, &dest );
1519   SDL_UpdateRects ( sdl_realscreen, 1, &dest );
1520   SDL_FreeSurface ( rtext );
1521
1522   return;
1523 }
1524
1525 void ui_discoverscreen ( unsigned char clearscreen ) {
1526
1527   SDL_Rect dest;
1528
1529   unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
1530   unsigned int font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
1531   unsigned int font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
1532   unsigned int font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
1533
1534   // clear the screen
1535   if ( clearscreen ) {
1536     SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
1537
1538     // render background
1539     if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
1540       dest.x = 0;
1541       dest.y = 0;
1542       dest.w = sdl_realscreen -> w;
1543       dest.h = sdl_realscreen -> h;
1544       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, NULL /* whole image */, sdl_realscreen, NULL /* 0,0 */ );
1545       SDL_UpdateRects ( sdl_realscreen, 1, &dest );
1546     }
1547
1548   }
1549
1550   // render text
1551   SDL_Surface *rtext;
1552   SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
1553   rtext = TTF_RenderText_Blended ( g_big_font, "Looking for applications...", tmpfontcolor );
1554   if ( clearscreen ) {
1555     dest.x = 20;
1556     dest.y = 20;
1557   } else {
1558     dest.x = 20;
1559     dest.y = 40;
1560   }
1561   SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, &dest );
1562   SDL_UpdateRects ( sdl_realscreen, 1, &dest );
1563   SDL_FreeSurface ( rtext );
1564
1565   // render icon
1566   if ( g_imagecache [ IMG_ICON_MISSING ].i ) {
1567     dest.x = rtext -> w + 30;
1568     dest.y = 20;
1569     SDL_BlitSurface ( g_imagecache [ IMG_ICON_MISSING ].i, NULL, sdl_realscreen, &dest );
1570     SDL_UpdateRects ( sdl_realscreen, 1, &dest );
1571   }
1572
1573   return;
1574 }
1575
1576 void ui_cachescreen ( unsigned char clearscreen, char *filename ) {
1577
1578   SDL_Rect rects [ 4 ];
1579   SDL_Rect *dest = rects;
1580   SDL_Rect src;
1581   bzero ( dest, sizeof(SDL_Rect)* 4 );
1582
1583   unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
1584   unsigned int font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
1585   unsigned int font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
1586   unsigned int font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
1587
1588   static unsigned int stepx = 0;
1589
1590   // clear the screen
1591   if ( clearscreen ) {
1592     SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
1593
1594     // render background
1595     if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
1596       dest -> x = 0;
1597       dest -> y = 0;
1598       dest -> w = sdl_realscreen -> w;
1599       dest -> h = sdl_realscreen -> h;
1600       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, NULL /* whole image */, sdl_realscreen, NULL /* 0,0 */ );
1601       dest++;
1602     }
1603
1604   } else {
1605
1606     // render background
1607     if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
1608       src.x = 0;
1609       src.y = 0;
1610       src.w = sdl_realscreen -> w;
1611       src.h = 100;
1612       dest -> x = 0;
1613       dest -> y = 0;
1614       dest -> w = sdl_realscreen -> w;
1615       dest -> h = sdl_realscreen -> h;
1616       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, &src, sdl_realscreen, dest );
1617       dest++;
1618     }
1619
1620   } // clear it
1621
1622   // render text
1623   SDL_Surface *rtext;
1624   SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
1625   rtext = TTF_RenderText_Blended ( g_big_font, "Caching applications artwork...", tmpfontcolor );
1626   dest -> x = 20;
1627   dest -> y = 20;
1628   SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
1629   SDL_FreeSurface ( rtext );
1630   dest++;
1631
1632   // render icon
1633   if ( g_imagecache [ IMG_ICON_MISSING ].i ) {
1634     dest -> x = rtext -> w + 30 + stepx;
1635     dest -> y = 20;
1636     SDL_BlitSurface ( g_imagecache [ IMG_ICON_MISSING ].i, NULL, sdl_realscreen, dest );
1637     dest++;
1638   }
1639
1640   // filename
1641   if ( filename ) {
1642     rtext = TTF_RenderText_Blended ( g_tab_font, filename, tmpfontcolor );
1643     dest -> x = 20;
1644     dest -> y = 50;
1645     SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
1646     SDL_FreeSurface ( rtext );
1647     dest++;
1648   }
1649
1650   // move across
1651   stepx += 20;
1652
1653   if ( stepx > 350 ) {
1654     stepx = 0;
1655   }
1656
1657   SDL_UpdateRects ( sdl_realscreen, dest - rects, rects );
1658
1659   return;
1660 }
1661
1662 int ui_selected_index ( void ) {
1663
1664   if ( ! ui_selected ) {
1665     return ( -1 ); // no index
1666   }
1667
1668   mm_appref_t *r = g_categories [ ui_category ].refs;
1669   int counter = 0;
1670   while ( r ) {
1671     if ( r == ui_selected ) {
1672       return ( counter );
1673     }
1674     r = r -> next;
1675     counter++;
1676   }
1677
1678   return ( -1 );
1679 }
1680
1681 static mm_appref_t *timer_ref = NULL;
1682 void ui_set_selected ( mm_appref_t *r ) {
1683
1684   render_mask |= CHANGED_SELECTION;
1685
1686   if ( ! pnd_conf_get_as_int_d ( g_conf, "minimenu.load_previews_later", 0 ) ) {
1687     return; // no desire to defer anything
1688   }
1689
1690   if ( ! r ) {
1691     // cancel timer
1692     SDL_SetTimer ( 0, NULL );
1693     timer_ref = NULL;
1694     return;
1695   }
1696
1697   SDL_SetTimer ( pnd_conf_get_as_int_d ( g_conf, "previewpic.defer_timer_ms", 1000 ), ui_callback_f );
1698   timer_ref = r;
1699
1700   return;
1701 }
1702
1703 unsigned int ui_callback_f ( unsigned int t ) {
1704
1705   if ( ui_selected != timer_ref ) {
1706     return ( 0 ); // user has moved it, who cares
1707   }
1708
1709   SDL_Event e;
1710   bzero ( &e, sizeof(SDL_Event) );
1711   e.type = SDL_USEREVENT;
1712   e.user.code = sdl_user_ticker;
1713   SDL_PushEvent ( &e );
1714
1715   return ( 0 );
1716 }
1717
1718 int ui_modal_single_menu ( char *argv[], unsigned int argc, char *title, char *footer ) {
1719   SDL_Rect rects [ 40 ];
1720   SDL_Rect *dest = rects;
1721   SDL_Rect src;
1722   SDL_Surface *rtext;
1723
1724   bzero ( rects, sizeof(SDL_Rect) * 40 );
1725
1726   unsigned int sel = 0;
1727
1728   unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
1729   unsigned int font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
1730   unsigned int font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
1731   unsigned int font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
1732
1733   SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
1734
1735   SDL_Color selfontcolor = { 0/*font_rgba_r*/, font_rgba_g, font_rgba_b, font_rgba_a };
1736
1737   unsigned int i;
1738   SDL_Event event;
1739
1740   while ( 1 ) {
1741
1742     // clear
1743     dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
1744     dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
1745     dest -> w = ((SDL_Surface*) g_imagecache [ IMG_DETAIL_PANEL ].i) -> w;
1746     dest -> h = ((SDL_Surface*) g_imagecache [ IMG_DETAIL_PANEL ].i) -> h;
1747     SDL_FillRect( sdl_realscreen, dest, 0 );
1748
1749     // show dialog background
1750     if ( g_imagecache [ IMG_DETAIL_BG ].i ) {
1751       src.x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
1752       src.y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
1753       src.w = ((SDL_Surface*)(g_imagecache [ IMG_DETAIL_PANEL ].i)) -> w;
1754       src.h = ((SDL_Surface*)(g_imagecache [ IMG_DETAIL_PANEL ].i)) -> h;
1755       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
1756       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
1757       SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
1758       // repeat for darken?
1759       SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
1760       SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
1761       //SDL_UpdateRects ( sdl_realscreen, 1, &dest );
1762       dest++;
1763     }
1764
1765     // show dialog frame
1766     if ( g_imagecache [ IMG_DETAIL_PANEL ].i ) {
1767       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
1768       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
1769       SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_PANEL ].i, NULL /* whole image */, sdl_realscreen, dest );
1770       //SDL_UpdateRects ( sdl_realscreen, 1, &dest );
1771       dest++;
1772     }
1773
1774     // show header
1775     if ( title ) {
1776       rtext = TTF_RenderText_Blended ( g_tab_font, title, tmpfontcolor );
1777       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 ) + 20;
1778       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 ) + 20;
1779       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
1780       SDL_FreeSurface ( rtext );
1781       dest++;
1782     }
1783
1784     // show footer
1785     if ( footer ) {
1786       rtext = TTF_RenderText_Blended ( g_tab_font, footer, tmpfontcolor );
1787       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 ) + 20;
1788       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 ) +
1789         ((SDL_Surface*) g_imagecache [ IMG_DETAIL_PANEL ].i) -> h
1790         - 60;
1791       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
1792       SDL_FreeSurface ( rtext );
1793       dest++;
1794     }
1795
1796     // show options
1797     for ( i = 0; i < argc; i++ ) {
1798
1799       // show options
1800       if ( sel == i ) {
1801         rtext = TTF_RenderText_Blended ( g_tab_font, argv [ i ], selfontcolor );
1802       } else {
1803         rtext = TTF_RenderText_Blended ( g_tab_font, argv [ i ], tmpfontcolor );
1804       }
1805       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 ) + 20;
1806       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 ) + 40 + ( 20 * ( i + 1 ) );
1807       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
1808       SDL_FreeSurface ( rtext );
1809       dest++;
1810
1811     } // for
1812
1813     // update all the rects and send it all to sdl
1814     SDL_UpdateRects ( sdl_realscreen, dest - rects, rects );
1815     dest = rects;
1816
1817     // check for input
1818     while ( SDL_WaitEvent ( &event ) ) {
1819
1820       switch ( event.type ) {
1821
1822       case SDL_KEYUP:
1823
1824         if ( event.key.keysym.sym == SDLK_UP ) {
1825           if ( sel ) {
1826             sel--;
1827           }
1828         } else if ( event.key.keysym.sym == SDLK_DOWN ) {
1829           if ( sel < argc - 1 ) {
1830             sel++;
1831           }
1832
1833         } else if ( event.key.keysym.sym == SDLK_RETURN ) {
1834           return ( sel );
1835
1836         } else if ( event.key.keysym.sym == SDLK_q ) {
1837           exit ( 0 );
1838
1839         } else {
1840           return ( -1 ); // nada
1841         }
1842
1843         break;
1844
1845       } // switch
1846
1847       break;
1848     } // while
1849
1850   } // while
1851
1852   return ( -1 );
1853 }
1854
1855 unsigned char ui_determine_row ( mm_appref_t *a ) {
1856   unsigned int row = 0;
1857
1858   mm_appref_t *i = g_categories [ ui_category ].refs;
1859   while ( i != a ) {
1860     i = i -> next;
1861     row++;
1862   } // while
1863   row /= pnd_conf_get_as_int_d ( g_conf, "grid.col_max", 5 );
1864
1865   return ( row );
1866 }
1867
1868 unsigned char ui_determine_screen_row ( mm_appref_t *a ) {
1869   return ( ui_determine_row ( a ) % pnd_conf_get_as_int_d ( g_conf, "grid.row_max", 5 ) );
1870 }
1871
1872 unsigned char ui_determine_screen_col ( mm_appref_t *a ) {
1873   unsigned int col = 0;
1874
1875   mm_appref_t *i = g_categories [ ui_category ].refs;
1876   while ( i != a ) {
1877     i = i -> next;
1878     col++;
1879   } // while
1880   col %= pnd_conf_get_as_int_d ( g_conf, "grid.col_max", 5 );
1881
1882   return ( col );
1883 }
1884
1885 unsigned char ui_show_info ( char *pndrun, pnd_disco_t *p ) {
1886   char *viewer, *searchpath;
1887   pnd_conf_handle desktoph;
1888
1889   // viewer
1890   searchpath = pnd_conf_query_searchpath();
1891
1892   desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, searchpath );
1893
1894   if ( ! desktoph ) {
1895     return ( 0 );
1896   }
1897
1898   viewer = pnd_conf_get_as_char ( desktoph, "info.viewer" );
1899
1900   if ( ! viewer ) {
1901     return ( 0 ); // no way to view the file
1902   }
1903
1904   // etc
1905   if ( ! p -> unique_id ) {
1906     return ( 0 );
1907   }
1908
1909   if ( ! p -> info_filename ) {
1910     return ( 0 );
1911   }
1912
1913   if ( ! p -> info_name ) {
1914     return ( 0 );
1915   }
1916
1917   if ( ! pndrun ) {
1918     return ( 0 );
1919   }
1920
1921   // exec line
1922   char args [ 1001 ];
1923   char *pargs = args;
1924   if ( pnd_conf_get_as_char ( desktoph, "info.viewer_args" ) ) {
1925     snprintf ( pargs, 1001, "%s %s",
1926                pnd_conf_get_as_char ( desktoph, "info.viewer_args" ), p -> info_filename );
1927   } else {
1928     pargs = NULL;
1929   }
1930
1931   char pndfile [ 1024 ];
1932   if ( p -> object_type == pnd_object_type_directory ) {
1933     // for PXML-app-dir, pnd_run.sh doesn't want the PXML.xml.. it just wants the dir-name
1934     strncpy ( pndfile, p -> object_path, 1000 );
1935   } else if ( p -> object_type == pnd_object_type_pnd ) {
1936     // pnd_run.sh wants the full path and filename for the .pnd file
1937     snprintf ( pndfile, 1020, "%s/%s", p -> object_path, p -> object_filename );
1938   }
1939
1940   if ( ! pnd_apps_exec ( pndrun, pndfile, p -> unique_id, viewer, p -> startdir, pargs,
1941                          p -> clockspeed ? atoi ( p -> clockspeed ) : 0, PND_EXEC_OPTION_NORUN ) )
1942   {
1943     return ( 0 );
1944   }
1945
1946   pnd_log ( pndn_debug, "Info Exec=%s\n", pnd_apps_exec_runline() );
1947
1948   // try running it
1949   int x;
1950   if ( ( x = fork() ) < 0 ) {
1951     pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
1952     return ( 0 );
1953   }
1954
1955   if ( x == 0 ) {
1956     execl ( "/bin/sh", "/bin/sh", "-c", pnd_apps_exec_runline(), (char*)NULL );
1957     pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", pnd_apps_exec_runline() );
1958     return ( 0 );
1959   }
1960
1961   return ( 1 );
1962 }
1963
1964 typedef struct {
1965   SDL_Rect r;
1966   int catnum;
1967   mm_appref_t *ref;
1968 } ui_touch_t;
1969 #define MAXTOUCH 100
1970 ui_touch_t ui_touchrects [ MAXTOUCH ];
1971 unsigned char ui_touchrect_count = 0;
1972
1973 void ui_register_reset ( void ) {
1974   bzero ( ui_touchrects, sizeof(ui_touch_t)*MAXTOUCH );
1975   ui_touchrect_count = 0;
1976   return;
1977 }
1978
1979 void ui_register_tab ( unsigned char catnum, unsigned int x, unsigned int y, unsigned int w, unsigned int h ) {
1980
1981   if ( ui_touchrect_count == MAXTOUCH ) {
1982     return;
1983   }
1984
1985   ui_touchrects [ ui_touchrect_count ].r.x = x;
1986   ui_touchrects [ ui_touchrect_count ].r.y = y;
1987   ui_touchrects [ ui_touchrect_count ].r.w = w;
1988   ui_touchrects [ ui_touchrect_count ].r.h = h;
1989   ui_touchrects [ ui_touchrect_count ].catnum = catnum;
1990   ui_touchrect_count++;
1991
1992   return;
1993 }
1994
1995 void ui_register_app ( mm_appref_t *app, unsigned int x, unsigned int y, unsigned int w, unsigned int h ) {
1996
1997   if ( ui_touchrect_count == MAXTOUCH ) {
1998     return;
1999   }
2000
2001   ui_touchrects [ ui_touchrect_count ].r.x = x;
2002   ui_touchrects [ ui_touchrect_count ].r.y = y;
2003   ui_touchrects [ ui_touchrect_count ].r.w = w;
2004   ui_touchrects [ ui_touchrect_count ].r.h = h;
2005   ui_touchrects [ ui_touchrect_count ].ref = app;
2006   ui_touchrect_count++;
2007
2008   return;
2009 }
2010
2011 void ui_touch_act ( unsigned int x, unsigned int y ) {
2012
2013   unsigned char i;
2014   ui_touch_t *t;
2015
2016   for ( i = 0; i < ui_touchrect_count; i++ ) {
2017     t = &(ui_touchrects [ i ]);
2018
2019     if ( x >= t -> r.x &&
2020          x <= t -> r.x + t -> r.w &&
2021          y >= t -> r.y &&
2022          y <= t -> r.y + t -> r.h
2023        )
2024     {
2025
2026       if ( t -> ref ) {
2027         ui_selected = t -> ref;
2028         ui_push_exec();
2029       } else {
2030         if ( ui_category != t -> catnum ) {
2031           ui_selected = NULL;
2032         }
2033         ui_category = t -> catnum;
2034         render_mask |= CHANGED_CATEGORY;
2035       }
2036
2037       break;
2038     }
2039
2040   } // for
2041
2042   return;
2043 }
2044
2045 unsigned char ui_forkexec ( char *argv[] ) {
2046   char *fooby = argv[0];
2047   int x;
2048
2049   if ( ( x = fork() ) < 0 ) {
2050     pnd_log ( pndn_error, "ERROR: Couldn't fork() for '%s'\n", fooby );
2051     return ( 0 );
2052   }
2053
2054   if ( x == 0 ) { // child
2055     execv ( fooby, argv );
2056     pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", fooby );
2057     return ( 0 );
2058   }
2059
2060   // parent, success
2061   return ( 1 );
2062 }
2063
2064 unsigned char ui_threaded_defered_preview ( pnd_disco_t *p ) {
2065
2066   if ( ! cache_preview ( p, pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 200 ),
2067                          pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 180 ) )
2068      )
2069   {
2070     pnd_log ( pndn_debug, "THREAD: Couldn't load preview pic: '%s' -> '%s'\n",
2071               IFNULL(p->title_en,"No Name"), p -> preview_pic1 );
2072   }
2073
2074   // trigger that we completed
2075   SDL_Event e;
2076   bzero ( &e, sizeof(SDL_Event) );
2077   e.type = SDL_USEREVENT;
2078   e.user.code = sdl_user_finishedpreview;
2079   e.user.data1 = p;
2080   SDL_PushEvent ( &e );
2081
2082   return ( 0 );
2083 }
2084
2085 SDL_Thread *g_icon_thread = NULL;
2086 void ui_post_scan ( void ) {
2087
2088   // if deferred icon load, kick off the thread now
2089   if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_icons_later", 0 ) == 1 ) {
2090
2091     g_icon_thread = SDL_CreateThread ( (void*)ui_threaded_defered_icon, NULL );
2092
2093     if ( ! g_icon_thread ) {
2094       pnd_log ( pndn_error, "ERROR: Couldn't create icon thread\n" );
2095     }
2096
2097   } // deferred icon load
2098
2099   return;
2100 }
2101
2102 unsigned char ui_threaded_defered_icon ( void *p ) {
2103   extern pnd_box_handle g_active_apps;
2104   pnd_box_handle h = g_active_apps;
2105
2106   unsigned char maxwidth, maxheight;
2107   maxwidth = pnd_conf_get_as_int_d ( g_conf, MMENU_DISP_ICON_MAX_WIDTH, 50 );
2108   maxheight = pnd_conf_get_as_int_d ( g_conf, MMENU_DISP_ICON_MAX_HEIGHT, 50 );
2109
2110   pnd_disco_t *iter = pnd_box_get_head ( h );
2111
2112   while ( iter ) {
2113
2114     // cache it
2115     if ( iter -> pnd_icon_pos &&
2116          ! cache_icon ( iter, maxwidth, maxheight ) )
2117     {
2118       pnd_log ( pndn_warning, "  Couldn't load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
2119     } else {
2120
2121       // trigger that we completed
2122       SDL_Event e;
2123       bzero ( &e, sizeof(SDL_Event) );
2124       e.type = SDL_USEREVENT;
2125       e.user.code = sdl_user_finishedicon;
2126       SDL_PushEvent ( &e );
2127
2128       //pnd_log ( pndn_warning, "  Finished deferred load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
2129       usleep ( pnd_conf_get_as_int_d ( g_conf, "minimenu.defer_icon_us", 50000 ) );
2130
2131     }
2132
2133     // next
2134     iter = pnd_box_get_next ( iter );
2135   } // while
2136
2137   return ( 0 );
2138 }
2139
2140 void ui_show_hourglass ( unsigned char updaterect ) {
2141
2142   SDL_Rect dest;
2143   SDL_Surface *s = g_imagecache [ IMG_HOURGLASS ].i;
2144
2145   dest.x = ( 800 - s -> w ) / 2;
2146   dest.y = ( 480 - s -> h ) / 2;
2147
2148   SDL_BlitSurface ( s, NULL /* whole image */, sdl_realscreen, &dest );
2149
2150   if ( updaterect ) {
2151     SDL_UpdateRects ( sdl_realscreen, 1, &dest );
2152   }
2153
2154   return;
2155 }