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