467678e591882a7fcc64a62fc792208d3d358ffb
[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     pnd_apps_exec_disco ( pnd_run_script, ui_selected -> ref, PND_EXEC_OPTION_NORUN, NULL );
1466     char buffer [ PATH_MAX ];
1467     sprintf ( buffer, "%s %s\n", MM_RUN, pnd_apps_exec_runline() );
1468     emit_and_quit ( buffer );
1469   }
1470
1471   return;
1472 }
1473
1474 void ui_push_ltrigger ( void ) {
1475   unsigned char oldcat = ui_category;
1476
1477   if ( ui_category > 0 ) {
1478     ui_category--;
1479   } else {
1480     if ( pnd_conf_get_as_int_d ( g_conf, "tabs.wraparound", 0 ) > 0 ) {
1481       ui_category = g_categorycount - 1;
1482     }
1483   }
1484
1485   if ( oldcat != ui_category ) {
1486     ui_selected = NULL;
1487     ui_set_selected ( ui_selected );
1488   }
1489
1490   // make tab visible?
1491   if ( ui_catshift > 0 && ui_category == ui_catshift - 1 ) {
1492     ui_catshift--;
1493   }
1494
1495   // unscroll
1496   ui_rows_scrolled_down = 0;
1497
1498   render_mask |= CHANGED_CATEGORY;
1499
1500   return;
1501 }
1502
1503 void ui_push_rtrigger ( void ) {
1504   unsigned char oldcat = ui_category;
1505
1506   unsigned int screen_width = pnd_conf_get_as_int_d ( g_conf, "display.screen_width", 800 );
1507   unsigned int tab_width = pnd_conf_get_as_int ( g_conf, "tabs.tab_width" );
1508
1509   if ( ui_category < ( g_categorycount - 1 ) ) {
1510     ui_category++;
1511   } else {
1512     if ( pnd_conf_get_as_int_d ( g_conf, "tabs.wraparound", 0 ) > 0 ) {
1513       ui_category = 0;
1514     }
1515   }
1516
1517   if ( oldcat != ui_category ) {
1518     ui_selected = NULL;
1519     ui_set_selected ( ui_selected );
1520   }
1521
1522   // make tab visible?
1523   if ( ui_category > ui_catshift + ( screen_width / tab_width ) - 1 ) {
1524     ui_catshift++;
1525   }
1526
1527   // unscroll
1528   ui_rows_scrolled_down = 0;
1529
1530   render_mask |= CHANGED_CATEGORY;
1531
1532   return;
1533 }
1534
1535 SDL_Surface *ui_scale_image ( SDL_Surface *s, unsigned int maxwidth, int maxheight ) {
1536   double scale = 1000000.0;
1537   double scalex = 1000000.0;
1538   double scaley = 1000000.0;
1539   SDL_Surface *scaled;
1540
1541   scalex = (double)maxwidth / (double)s -> w;
1542
1543   if ( maxheight == -1 ) {
1544     scale = scalex;
1545   } else {
1546     scaley = (double)maxheight / (double)s -> h;
1547
1548     if ( scaley < scalex ) {
1549       scale = scaley;
1550     } else {
1551       scale = scalex;
1552     }
1553
1554   }
1555
1556   scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
1557   SDL_FreeSurface ( s );
1558   s = scaled;
1559
1560   return ( s );
1561 }
1562
1563 void ui_loadscreen ( void ) {
1564
1565   SDL_Rect dest;
1566
1567   unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
1568   unsigned int font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
1569   unsigned int font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
1570   unsigned int font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
1571
1572   // clear the screen
1573   SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
1574
1575   // render text
1576   SDL_Surface *rtext;
1577   SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
1578   rtext = TTF_RenderText_Blended ( g_big_font, "Setting up menu...", tmpfontcolor );
1579   dest.x = 20;
1580   dest.y = 20;
1581   SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, &dest );
1582   SDL_UpdateRects ( sdl_realscreen, 1, &dest );
1583   SDL_FreeSurface ( rtext );
1584
1585   return;
1586 }
1587
1588 void ui_discoverscreen ( unsigned char clearscreen ) {
1589
1590   SDL_Rect dest;
1591
1592   unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
1593   unsigned int font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
1594   unsigned int font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
1595   unsigned int font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
1596
1597   // clear the screen
1598   if ( clearscreen ) {
1599     SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
1600
1601     // render background
1602     if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
1603       dest.x = 0;
1604       dest.y = 0;
1605       dest.w = sdl_realscreen -> w;
1606       dest.h = sdl_realscreen -> h;
1607       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, NULL /* whole image */, sdl_realscreen, NULL /* 0,0 */ );
1608       SDL_UpdateRects ( sdl_realscreen, 1, &dest );
1609     }
1610
1611   }
1612
1613   // render text
1614   SDL_Surface *rtext;
1615   SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
1616   rtext = TTF_RenderText_Blended ( g_big_font, "Looking for applications...", tmpfontcolor );
1617   if ( clearscreen ) {
1618     dest.x = 20;
1619     dest.y = 20;
1620   } else {
1621     dest.x = 20;
1622     dest.y = 40;
1623   }
1624   SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, &dest );
1625   SDL_UpdateRects ( sdl_realscreen, 1, &dest );
1626   SDL_FreeSurface ( rtext );
1627
1628   // render icon
1629   if ( g_imagecache [ IMG_ICON_MISSING ].i ) {
1630     dest.x = rtext -> w + 30;
1631     dest.y = 20;
1632     SDL_BlitSurface ( g_imagecache [ IMG_ICON_MISSING ].i, NULL, sdl_realscreen, &dest );
1633     SDL_UpdateRects ( sdl_realscreen, 1, &dest );
1634   }
1635
1636   return;
1637 }
1638
1639 void ui_cachescreen ( unsigned char clearscreen, char *filename ) {
1640
1641   SDL_Rect rects [ 4 ];
1642   SDL_Rect *dest = rects;
1643   SDL_Rect src;
1644   bzero ( dest, sizeof(SDL_Rect)* 4 );
1645
1646   unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
1647   unsigned int font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
1648   unsigned int font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
1649   unsigned int font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
1650
1651   static unsigned int stepx = 0;
1652
1653   // clear the screen
1654   if ( clearscreen ) {
1655     SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
1656
1657     // render background
1658     if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
1659       dest -> x = 0;
1660       dest -> y = 0;
1661       dest -> w = sdl_realscreen -> w;
1662       dest -> h = sdl_realscreen -> h;
1663       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, NULL /* whole image */, sdl_realscreen, NULL /* 0,0 */ );
1664       dest++;
1665     }
1666
1667   } else {
1668
1669     // render background
1670     if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
1671       src.x = 0;
1672       src.y = 0;
1673       src.w = sdl_realscreen -> w;
1674       src.h = 100;
1675       dest -> x = 0;
1676       dest -> y = 0;
1677       dest -> w = sdl_realscreen -> w;
1678       dest -> h = sdl_realscreen -> h;
1679       SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, &src, sdl_realscreen, dest );
1680       dest++;
1681     }
1682
1683   } // clear it
1684
1685   // render text
1686   SDL_Surface *rtext;
1687   SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
1688   rtext = TTF_RenderText_Blended ( g_big_font, "Caching applications artwork...", tmpfontcolor );
1689   dest -> x = 20;
1690   dest -> y = 20;
1691   SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
1692   SDL_FreeSurface ( rtext );
1693   dest++;
1694
1695   // render icon
1696   if ( g_imagecache [ IMG_ICON_MISSING ].i ) {
1697     dest -> x = rtext -> w + 30 + stepx;
1698     dest -> y = 20;
1699     SDL_BlitSurface ( g_imagecache [ IMG_ICON_MISSING ].i, NULL, sdl_realscreen, dest );
1700     dest++;
1701   }
1702
1703   // filename
1704   if ( filename ) {
1705     rtext = TTF_RenderText_Blended ( g_tab_font, filename, tmpfontcolor );
1706     dest -> x = 20;
1707     dest -> y = 50;
1708     SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
1709     SDL_FreeSurface ( rtext );
1710     dest++;
1711   }
1712
1713   // move across
1714   stepx += 20;
1715
1716   if ( stepx > 350 ) {
1717     stepx = 0;
1718   }
1719
1720   SDL_UpdateRects ( sdl_realscreen, dest - rects, rects );
1721
1722   return;
1723 }
1724
1725 int ui_selected_index ( void ) {
1726
1727   if ( ! ui_selected ) {
1728     return ( -1 ); // no index
1729   }
1730
1731   mm_appref_t *r = g_categories [ ui_category ].refs;
1732   int counter = 0;
1733   while ( r ) {
1734     if ( r == ui_selected ) {
1735       return ( counter );
1736     }
1737     r = r -> next;
1738     counter++;
1739   }
1740
1741   return ( -1 );
1742 }
1743
1744 static mm_appref_t *timer_ref = NULL;
1745 void ui_set_selected ( mm_appref_t *r ) {
1746
1747   render_mask |= CHANGED_SELECTION;
1748
1749   if ( ! pnd_conf_get_as_int_d ( g_conf, "minimenu.load_previews_later", 0 ) ) {
1750     return; // no desire to defer anything
1751   }
1752
1753   if ( ! r ) {
1754     // cancel timer
1755     SDL_SetTimer ( 0, NULL );
1756     timer_ref = NULL;
1757     return;
1758   }
1759
1760   SDL_SetTimer ( pnd_conf_get_as_int_d ( g_conf, "previewpic.defer_timer_ms", 1000 ), ui_callback_f );
1761   timer_ref = r;
1762
1763   return;
1764 }
1765
1766 unsigned int ui_callback_f ( unsigned int t ) {
1767
1768   if ( ui_selected != timer_ref ) {
1769     return ( 0 ); // user has moved it, who cares
1770   }
1771
1772   SDL_Event e;
1773   bzero ( &e, sizeof(SDL_Event) );
1774   e.type = SDL_USEREVENT;
1775   e.user.code = sdl_user_ticker;
1776   SDL_PushEvent ( &e );
1777
1778   return ( 0 );
1779 }
1780
1781 int ui_modal_single_menu ( char *argv[], unsigned int argc, char *title, char *footer ) {
1782   SDL_Rect rects [ 40 ];
1783   SDL_Rect *dest = rects;
1784   SDL_Rect src;
1785   SDL_Surface *rtext;
1786
1787   bzero ( rects, sizeof(SDL_Rect) * 40 );
1788
1789   unsigned int sel = 0;
1790
1791   unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
1792   unsigned int font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
1793   unsigned int font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
1794   unsigned int font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
1795
1796   SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
1797
1798   SDL_Color selfontcolor = { 0/*font_rgba_r*/, font_rgba_g, font_rgba_b, font_rgba_a };
1799
1800   unsigned int i;
1801   SDL_Event event;
1802
1803   while ( 1 ) {
1804
1805     // clear
1806     dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
1807     dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
1808     dest -> w = ((SDL_Surface*) g_imagecache [ IMG_DETAIL_PANEL ].i) -> w;
1809     dest -> h = ((SDL_Surface*) g_imagecache [ IMG_DETAIL_PANEL ].i) -> h;
1810     SDL_FillRect( sdl_realscreen, dest, 0 );
1811
1812     // show dialog background
1813     if ( g_imagecache [ IMG_DETAIL_BG ].i ) {
1814       src.x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
1815       src.y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
1816       src.w = ((SDL_Surface*)(g_imagecache [ IMG_DETAIL_PANEL ].i)) -> w;
1817       src.h = ((SDL_Surface*)(g_imagecache [ IMG_DETAIL_PANEL ].i)) -> h;
1818       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
1819       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
1820       SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
1821       // repeat for darken?
1822       SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
1823       SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_BG ].i, &src, sdl_realscreen, dest );
1824       //SDL_UpdateRects ( sdl_realscreen, 1, &dest );
1825       dest++;
1826     }
1827
1828     // show dialog frame
1829     if ( g_imagecache [ IMG_DETAIL_PANEL ].i ) {
1830       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 );
1831       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 );
1832       SDL_BlitSurface ( g_imagecache [ IMG_DETAIL_PANEL ].i, NULL /* whole image */, sdl_realscreen, dest );
1833       //SDL_UpdateRects ( sdl_realscreen, 1, &dest );
1834       dest++;
1835     }
1836
1837     // show header
1838     if ( title ) {
1839       rtext = TTF_RenderText_Blended ( g_tab_font, title, tmpfontcolor );
1840       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 ) + 20;
1841       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 ) + 20;
1842       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
1843       SDL_FreeSurface ( rtext );
1844       dest++;
1845     }
1846
1847     // show footer
1848     if ( footer ) {
1849       rtext = TTF_RenderText_Blended ( g_tab_font, footer, tmpfontcolor );
1850       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 ) + 20;
1851       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 ) +
1852         ((SDL_Surface*) g_imagecache [ IMG_DETAIL_PANEL ].i) -> h
1853         - 60;
1854       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
1855       SDL_FreeSurface ( rtext );
1856       dest++;
1857     }
1858
1859     // show options
1860     for ( i = 0; i < argc; i++ ) {
1861
1862       // show options
1863       if ( sel == i ) {
1864         rtext = TTF_RenderText_Blended ( g_tab_font, argv [ i ], selfontcolor );
1865       } else {
1866         rtext = TTF_RenderText_Blended ( g_tab_font, argv [ i ], tmpfontcolor );
1867       }
1868       dest -> x = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_x", 460 ) + 20;
1869       dest -> y = pnd_conf_get_as_int_d ( g_conf, "detailpane.pane_offset_y", 60 ) + 40 + ( 20 * ( i + 1 ) );
1870       SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
1871       SDL_FreeSurface ( rtext );
1872       dest++;
1873
1874     } // for
1875
1876     // update all the rects and send it all to sdl
1877     SDL_UpdateRects ( sdl_realscreen, dest - rects, rects );
1878     dest = rects;
1879
1880     // check for input
1881     while ( SDL_WaitEvent ( &event ) ) {
1882
1883       switch ( event.type ) {
1884
1885       case SDL_KEYUP:
1886
1887         if ( event.key.keysym.sym == SDLK_UP ) {
1888           if ( sel ) {
1889             sel--;
1890           }
1891         } else if ( event.key.keysym.sym == SDLK_DOWN ) {
1892           if ( sel < argc - 1 ) {
1893             sel++;
1894           }
1895
1896         } else if ( event.key.keysym.sym == SDLK_RETURN ) {
1897           return ( sel );
1898
1899         } else if ( event.key.keysym.sym == SDLK_q ) {
1900           exit ( 0 );
1901
1902         } else {
1903           return ( -1 ); // nada
1904         }
1905
1906         break;
1907
1908       } // switch
1909
1910       break;
1911     } // while
1912
1913   } // while
1914
1915   return ( -1 );
1916 }
1917
1918 unsigned char ui_determine_row ( mm_appref_t *a ) {
1919   unsigned int row = 0;
1920
1921   mm_appref_t *i = g_categories [ ui_category ].refs;
1922   while ( i != a ) {
1923     i = i -> next;
1924     row++;
1925   } // while
1926   row /= pnd_conf_get_as_int_d ( g_conf, "grid.col_max", 5 );
1927
1928   return ( row );
1929 }
1930
1931 unsigned char ui_determine_screen_row ( mm_appref_t *a ) {
1932   return ( ui_determine_row ( a ) % pnd_conf_get_as_int_d ( g_conf, "grid.row_max", 5 ) );
1933 }
1934
1935 unsigned char ui_determine_screen_col ( mm_appref_t *a ) {
1936   unsigned int col = 0;
1937
1938   mm_appref_t *i = g_categories [ ui_category ].refs;
1939   while ( i != a ) {
1940     i = i -> next;
1941     col++;
1942   } // while
1943   col %= pnd_conf_get_as_int_d ( g_conf, "grid.col_max", 5 );
1944
1945   return ( col );
1946 }
1947
1948 unsigned char ui_show_info ( char *pndrun, pnd_disco_t *p ) {
1949   char *viewer, *searchpath;
1950   pnd_conf_handle desktoph;
1951
1952   // viewer
1953   searchpath = pnd_conf_query_searchpath();
1954
1955   desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, searchpath );
1956
1957   if ( ! desktoph ) {
1958     return ( 0 );
1959   }
1960
1961   viewer = pnd_conf_get_as_char ( desktoph, "info.viewer" );
1962
1963   if ( ! viewer ) {
1964     return ( 0 ); // no way to view the file
1965   }
1966
1967   // etc
1968   if ( ! p -> unique_id ) {
1969     return ( 0 );
1970   }
1971
1972   if ( ! p -> info_filename ) {
1973     return ( 0 );
1974   }
1975
1976   if ( ! p -> info_name ) {
1977     return ( 0 );
1978   }
1979
1980   if ( ! pndrun ) {
1981     return ( 0 );
1982   }
1983
1984   // exec line
1985   char args [ 1001 ];
1986   char *pargs = args;
1987   if ( pnd_conf_get_as_char ( desktoph, "info.viewer_args" ) ) {
1988     snprintf ( pargs, 1001, "%s %s",
1989                pnd_conf_get_as_char ( desktoph, "info.viewer_args" ), p -> info_filename );
1990   } else {
1991     pargs = NULL;
1992   }
1993
1994   char pndfile [ 1024 ];
1995   if ( p -> object_type == pnd_object_type_directory ) {
1996     // for PXML-app-dir, pnd_run.sh doesn't want the PXML.xml.. it just wants the dir-name
1997     strncpy ( pndfile, p -> object_path, 1000 );
1998   } else if ( p -> object_type == pnd_object_type_pnd ) {
1999     // pnd_run.sh wants the full path and filename for the .pnd file
2000     snprintf ( pndfile, 1020, "%s/%s", p -> object_path, p -> object_filename );
2001   }
2002
2003   if ( ! pnd_apps_exec ( pndrun, pndfile, p -> unique_id, viewer, p -> startdir, pargs,
2004                          p -> clockspeed ? atoi ( p -> clockspeed ) : 0, PND_EXEC_OPTION_NORUN ) )
2005   {
2006     return ( 0 );
2007   }
2008
2009   pnd_log ( pndn_debug, "Info Exec=%s\n", pnd_apps_exec_runline() );
2010
2011   // try running it
2012   int x;
2013   if ( ( x = fork() ) < 0 ) {
2014     pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
2015     return ( 0 );
2016   }
2017
2018   if ( x == 0 ) {
2019     execl ( "/bin/sh", "/bin/sh", "-c", pnd_apps_exec_runline(), (char*)NULL );
2020     pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", pnd_apps_exec_runline() );
2021     return ( 0 );
2022   }
2023
2024   return ( 1 );
2025 }
2026
2027 typedef struct {
2028   SDL_Rect r;
2029   int catnum;
2030   mm_appref_t *ref;
2031 } ui_touch_t;
2032 #define MAXTOUCH 100
2033 ui_touch_t ui_touchrects [ MAXTOUCH ];
2034 unsigned char ui_touchrect_count = 0;
2035
2036 void ui_register_reset ( void ) {
2037   bzero ( ui_touchrects, sizeof(ui_touch_t)*MAXTOUCH );
2038   ui_touchrect_count = 0;
2039   return;
2040 }
2041
2042 void ui_register_tab ( unsigned char catnum, unsigned int x, unsigned int y, unsigned int w, unsigned int h ) {
2043
2044   if ( ui_touchrect_count == MAXTOUCH ) {
2045     return;
2046   }
2047
2048   ui_touchrects [ ui_touchrect_count ].r.x = x;
2049   ui_touchrects [ ui_touchrect_count ].r.y = y;
2050   ui_touchrects [ ui_touchrect_count ].r.w = w;
2051   ui_touchrects [ ui_touchrect_count ].r.h = h;
2052   ui_touchrects [ ui_touchrect_count ].catnum = catnum;
2053   ui_touchrect_count++;
2054
2055   return;
2056 }
2057
2058 void ui_register_app ( mm_appref_t *app, unsigned int x, unsigned int y, unsigned int w, unsigned int h ) {
2059
2060   if ( ui_touchrect_count == MAXTOUCH ) {
2061     return;
2062   }
2063
2064   ui_touchrects [ ui_touchrect_count ].r.x = x;
2065   ui_touchrects [ ui_touchrect_count ].r.y = y;
2066   ui_touchrects [ ui_touchrect_count ].r.w = w;
2067   ui_touchrects [ ui_touchrect_count ].r.h = h;
2068   ui_touchrects [ ui_touchrect_count ].ref = app;
2069   ui_touchrect_count++;
2070
2071   return;
2072 }
2073
2074 void ui_touch_act ( unsigned int x, unsigned int y ) {
2075
2076   unsigned char i;
2077   ui_touch_t *t;
2078
2079   for ( i = 0; i < ui_touchrect_count; i++ ) {
2080     t = &(ui_touchrects [ i ]);
2081
2082     if ( x >= t -> r.x &&
2083          x <= t -> r.x + t -> r.w &&
2084          y >= t -> r.y &&
2085          y <= t -> r.y + t -> r.h
2086        )
2087     {
2088
2089       if ( t -> ref ) {
2090         ui_selected = t -> ref;
2091         ui_push_exec();
2092       } else {
2093         if ( ui_category != t -> catnum ) {
2094           ui_selected = NULL;
2095         }
2096         ui_category = t -> catnum;
2097         render_mask |= CHANGED_CATEGORY;
2098       }
2099
2100       break;
2101     }
2102
2103   } // for
2104
2105   return;
2106 }
2107
2108 unsigned char ui_forkexec ( char *argv[] ) {
2109   char *fooby = argv[0];
2110   int x;
2111
2112   if ( ( x = fork() ) < 0 ) {
2113     pnd_log ( pndn_error, "ERROR: Couldn't fork() for '%s'\n", fooby );
2114     return ( 0 );
2115   }
2116
2117   if ( x == 0 ) { // child
2118     execv ( fooby, argv );
2119     pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", fooby );
2120     return ( 0 );
2121   }
2122
2123   // parent, success
2124   return ( 1 );
2125 }
2126
2127 unsigned char ui_threaded_defered_preview ( pnd_disco_t *p ) {
2128
2129   if ( ! cache_preview ( p, pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 200 ),
2130                          pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 180 ) )
2131      )
2132   {
2133     pnd_log ( pndn_debug, "THREAD: Couldn't load preview pic: '%s' -> '%s'\n",
2134               IFNULL(p->title_en,"No Name"), p -> preview_pic1 );
2135   }
2136
2137   // trigger that we completed
2138   SDL_Event e;
2139   bzero ( &e, sizeof(SDL_Event) );
2140   e.type = SDL_USEREVENT;
2141   e.user.code = sdl_user_finishedpreview;
2142   e.user.data1 = p;
2143   SDL_PushEvent ( &e );
2144
2145   return ( 0 );
2146 }
2147
2148 SDL_Thread *g_icon_thread = NULL;
2149 void ui_post_scan ( void ) {
2150
2151   // if deferred icon load, kick off the thread now
2152   if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_icons_later", 0 ) == 1 ) {
2153
2154     g_icon_thread = SDL_CreateThread ( (void*)ui_threaded_defered_icon, NULL );
2155
2156     if ( ! g_icon_thread ) {
2157       pnd_log ( pndn_error, "ERROR: Couldn't create icon thread\n" );
2158     }
2159
2160   } // deferred icon load
2161
2162   return;
2163 }
2164
2165 unsigned char ui_threaded_defered_icon ( void *p ) {
2166   extern pnd_box_handle g_active_apps;
2167   pnd_box_handle h = g_active_apps;
2168
2169   unsigned char maxwidth, maxheight;
2170   maxwidth = pnd_conf_get_as_int_d ( g_conf, MMENU_DISP_ICON_MAX_WIDTH, 50 );
2171   maxheight = pnd_conf_get_as_int_d ( g_conf, MMENU_DISP_ICON_MAX_HEIGHT, 50 );
2172
2173   pnd_disco_t *iter = pnd_box_get_head ( h );
2174
2175   while ( iter ) {
2176
2177     // cache it
2178     if ( iter -> pnd_icon_pos &&
2179          ! cache_icon ( iter, maxwidth, maxheight ) )
2180     {
2181       pnd_log ( pndn_warning, "  Couldn't load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
2182     } else {
2183
2184       // trigger that we completed
2185       SDL_Event e;
2186       bzero ( &e, sizeof(SDL_Event) );
2187       e.type = SDL_USEREVENT;
2188       e.user.code = sdl_user_finishedicon;
2189       SDL_PushEvent ( &e );
2190
2191       //pnd_log ( pndn_warning, "  Finished deferred load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
2192       usleep ( pnd_conf_get_as_int_d ( g_conf, "minimenu.defer_icon_us", 50000 ) );
2193
2194     }
2195
2196     // next
2197     iter = pnd_box_get_next ( iter );
2198   } // while
2199
2200   return ( 0 );
2201 }
2202
2203 void ui_show_hourglass ( unsigned char updaterect ) {
2204
2205   SDL_Rect dest;
2206   SDL_Surface *s = g_imagecache [ IMG_HOURGLASS ].i;
2207
2208   dest.x = ( 800 - s -> w ) / 2;
2209   dest.y = ( 480 - s -> h ) / 2;
2210
2211   SDL_BlitSurface ( s, NULL /* whole image */, sdl_realscreen, &dest );
2212
2213   if ( updaterect ) {
2214     SDL_UpdateRects ( sdl_realscreen, 1, &dest );
2215   }
2216
2217   return;
2218 }