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