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