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