minimenu is now skinnable and has skin selector
[pandora-libraries.git] / minimenu / mmenu.c
1
2 /* minimenu
3  * aka "2wm" - too weak menu, two week menu, akin to twm
4  *
5  * Craig wants a super minimal menu ASAP before launch, so lets see what I can put together in 2 weeks with not much
6  * free time ;) I'd like to do a fuller ('tiny', but with plugin support and a decent expansion and customizing design..)
7  * but later, baby!
8  *
9  */
10
11 /* mmenu - This is the actual menu
12  * The goal of this app is to show a application picker screen of some sort, allow the user to perform some useful
13  * activities (such as set clock speed, say), and request an app to run, or shutdown, etc.
14  * To keep the memory footprint down, when invoking an application, the menu _exits_, and simply spits out
15  * an operation for mmwrapper to perform. In the case of no wrapper, the menu will just exit, which is handy for
16  * debugging.
17  */
18
19 /* mmenu lifecycle:
20  * 1) determine app list (via pnd scan, .desktop scan, whatever voodoo)
21  * 2) show a menu, allow user to interact:
22  *    a) user picks an application to run, or -> exit, pass shell run line to wrapper
23  *    b) user requests menu shutdown -> exit, tell wrapper to exit as well
24  *    c) user performsn some operation (set clock, copy files, whatever) -> probably stays within the menu
25  */
26
27 #include <stdio.h> /* for FILE etc */
28 #include <stdlib.h> /* for malloc */
29 #include <unistd.h> /* for unlink */
30 #include <limits.h> /* for PATH_MAX */
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #define __USE_GNU /* for strcasestr */
34 #include <string.h> /* for making ftw.h happy */
35 #include <strings.h>
36 #include <ctype.h>
37 #include <sys/wait.h>
38
39 #include "pnd_logger.h"
40 #include "pnd_pxml.h"
41 #include "pnd_utility.h"
42 #include "pnd_conf.h"
43 #include "pnd_container.h"
44 #include "pnd_discovery.h"
45 #include "pnd_locate.h"
46 #include "pnd_device.h"
47 #include "pnd_pndfiles.h"
48
49 #include "mmenu.h"
50 #include "mmwrapcmd.h"
51 #include "mmapps.h"
52 #include "mmcache.h"
53 #include "mmcat.h"
54 #include "mmui.h"
55
56 pnd_box_handle g_active_apps = NULL;
57 unsigned int g_active_appcount = 0;
58 char g_username [ 128 ]; // since we have to wait for login (!!), store username here
59 pnd_conf_handle g_conf = 0;
60 pnd_conf_handle g_desktopconf = 0;
61
62 char *pnd_run_script = NULL;
63 unsigned char g_x11_present = 1; // >0 if X is present
64 unsigned char g_catmap = 0; // if 1, we're doing category mapping
65 unsigned char g_pvwcache = 0; // if 1, we're trying to do preview caching
66
67 char g_skin_selected [ 100 ] = "default";
68 char *g_skinpath = NULL; // where 'skin_selected' is located .. the fullpath including skin-dir-name
69 pnd_conf_handle g_skinconf = NULL;
70
71 int main ( int argc, char *argv[] ) {
72   int logall = -1; // -1 means normal logging rules; >=0 means log all!
73   int i;
74
75   // boilerplate stuff from pndnotifyd and pndevmapperd
76
77   /* iterate across args
78    */
79   for ( i = 1; i < argc; i++ ) {
80
81     if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'l' ) {
82
83       if ( isdigit ( argv [ i ][ 2 ] ) ) {
84         unsigned char x = atoi ( argv [ i ] + 2 );
85         if ( x >= 0 &&
86              x < pndn_none )
87         {
88           logall = x;
89         }
90       } else {
91         logall = 0;
92       }
93
94     } else {
95       //printf ( "Unknown: %s\n", argv [ i ] );
96       printf ( "%s [-l##]\n", argv [ 0 ] );
97       printf ( "-l#\tLog-it; -l is 0-and-up (or all), and -l2 means 2-and-up (not all); l[0-3] for now. Log goes to /tmp/mmenu.log\n" );
98       printf ( "-f\tFull path of frontend to run\n" );
99       exit ( 0 );
100     }
101
102   } // for
103
104   /* enable logging?
105    */
106   pnd_log_set_pretext ( "mmenu" );
107   pnd_log_set_flush ( 1 );
108
109   if ( logall == -1 ) {
110     // standard logging; non-daemon versus daemon
111
112 #if 1 // HACK: set debug level to high on desktop, but not on pandora; just a convenience while devving, until the conf file is read
113     struct stat statbuf;
114     if ( stat ( PND_DEVICE_BATTERY_GAUGE_PERC, &statbuf ) == 0 ) {
115       // on pandora
116       pnd_log_set_filter ( pndn_error );
117     } else {
118       pnd_log_set_filter ( pndn_debug );
119     }
120 #endif
121
122     pnd_log_to_stdout();
123
124   } else {
125     FILE *f;
126
127     f = fopen ( "/tmp/mmenu.log", "w" );
128
129     if ( f ) {
130       pnd_log_set_filter ( logall );
131       pnd_log_to_stream ( f );
132       pnd_log ( pndn_rem, "logall mode - logging to /tmp/mmenu.log\n" );
133     }
134
135     if ( logall == pndn_debug ) {
136       pnd_log_set_buried_logging ( 1 ); // log the shit out of it
137       pnd_log ( pndn_rem, "logall mode 0 - turned on buried logging\n" );
138     }
139
140   } // logall
141
142   pnd_log ( pndn_rem, "%s built %s %s", argv [ 0 ], __DATE__, __TIME__ );
143
144   pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );
145
146   // wait for a user to be logged in - we should probably get hupped when a user logs in, so we can handle
147   // log-out and back in again, with SDs popping in and out between..
148   pnd_log ( pndn_rem, "Checking to see if a user is logged in\n" );
149   while ( 1 ) {
150     if ( pnd_check_login ( g_username, 127 ) ) {
151       break;
152     }
153     pnd_log ( pndn_debug, "  No one logged in yet .. spinning.\n" );
154     sleep ( 2 );
155   } // spin
156   pnd_log ( pndn_rem, "Looks like user '%s' is in, continue.\n", g_username );
157
158   /* conf file
159    */
160   g_conf = pnd_conf_fetch_by_name ( MMENU_CONF, MMENU_CONF_SEARCHPATH );
161
162   if ( ! g_conf ) {
163     pnd_log ( pndn_error, "ERROR: Couldn't fetch conf file '%s'!\n", MMENU_CONF );
164     emit_and_quit ( MM_QUIT );
165   }
166
167   g_desktopconf = pnd_conf_fetch_by_id ( pnd_conf_desktop, PND_CONF_SEARCHPATH );
168
169   if ( ! g_desktopconf ) {
170     pnd_log ( pndn_error, "ERROR: Couldn't fetch desktop conf file\n" );
171     emit_and_quit ( MM_QUIT );
172   }
173
174   // redo log filter
175   pnd_log_set_filter ( pnd_conf_get_as_int_d ( g_conf, "minimenu.loglevel", pndn_error ) );
176
177   /* setup
178    */
179
180   // X11?
181   if ( pnd_conf_get_as_char ( g_conf, "minimenu.x11_present_sh" ) ) {
182     FILE *fx = popen ( pnd_conf_get_as_char ( g_conf, "minimenu.x11_present_sh" ), "r" );
183     char buffer [ 100 ];
184     if ( fx ) {
185       if ( fgets ( buffer, 100, fx ) ) {
186         if ( atoi ( buffer ) ) {
187           g_x11_present = 1;
188           pnd_log ( pndn_rem, "X11 seems to be present [pid %u]\n", atoi(buffer) );
189         } else {
190           g_x11_present = 0;
191           pnd_log ( pndn_rem, "X11 seems NOT to be present\n" );
192         }
193       } else {
194           g_x11_present = 0;
195           pnd_log ( pndn_rem, "X11 seems NOT to be present\n" );
196       }
197       pclose ( fx );
198     }
199   } // x11?
200
201   // pnd_run.sh
202   pnd_run_script = pnd_locate_filename ( pnd_conf_get_as_char ( g_conf, "minimenu.pndrun" ), "pnd_run.sh" );
203
204   if ( ! pnd_run_script ) {
205     pnd_log ( pndn_error, "ERROR: Couldn't locate pnd_run.sh!\n" );
206     emit_and_quit ( MM_QUIT );
207   }
208
209   pnd_run_script = strdup ( pnd_run_script ); // so we don't lose it next pnd_locate
210
211   pnd_log ( pndn_rem, "Found pnd_run.sh at '%s'\n", pnd_run_script );
212
213   // figure out what skin is selected (or default)
214   FILE *f;
215   char *s = strdup ( pnd_conf_get_as_char ( g_conf, "minimenu.skin_selected" ) );
216   s = pnd_expand_tilde ( s );
217   if ( ( f = fopen ( s, "r" ) ) ) {
218     char buffer [ 100 ];
219     if ( fgets ( buffer, 100, f ) ) {
220       // see if that dir can be located
221       if ( strchr ( buffer, '\n' ) ) {
222         * strchr ( buffer, '\n' ) = '\0';
223       }
224       char *found = pnd_locate_filename ( pnd_conf_get_as_char ( g_conf, "minimenu.skin_searchpath" ), buffer );
225       if ( found ) {
226         strncpy ( g_skin_selected, buffer, 100 );
227         g_skinpath = strdup ( found );
228       } else {
229         pnd_log ( pndn_warning, "Couldn't locate skin named '%s' so falling back.\n", buffer );
230       }
231     }
232     fclose ( f );
233   }
234   free ( s );
235
236   if ( g_skinpath ) {
237     pnd_log ( pndn_rem, "Skin is selected: '%s'\n", g_skin_selected );
238   } else {
239     pnd_log ( pndn_rem, "Skin falling back to: '%s'\n", g_skin_selected );
240
241     char *found = pnd_locate_filename ( pnd_conf_get_as_char ( g_conf, "minimenu.skin_searchpath" ),
242                                         g_skin_selected );
243     if ( found ) {
244       g_skinpath = strdup ( found );
245     } else {
246       pnd_log ( pndn_error, "Couldn't locate skin named '%s'.\n", g_skin_selected );
247       emit_and_quit ( MM_QUIT );
248     }
249
250   }
251   pnd_log ( pndn_rem, "Skin path determined to be: '%s'\n", g_skinpath );
252
253   // lets see if this skin-path actually has a skin conf
254   {
255     char fullpath [ PATH_MAX ];
256     sprintf ( fullpath, "%s/%s", g_skinpath, pnd_conf_get_as_char ( g_conf, "minimenu.skin_confname" ) );
257     g_skinconf = pnd_conf_fetch_by_path ( fullpath );
258   }
259
260   // figure out skin path if we didn't get it already
261   if ( ! g_skinconf ) {
262     pnd_log ( pndn_error, "ERROR: Couldn't set up skin!\n" );
263     emit_and_quit ( MM_QUIT );
264   }
265
266   // lets just merge the skin conf onto the regular conf, so it just magicly works
267   pnd_box_append ( g_conf, g_skinconf );
268
269   // attempt to set up UI
270   if ( ! ui_setup() ) {
271     pnd_log ( pndn_error, "ERROR: Couldn't set up the UI!\n" );
272     emit_and_quit ( MM_QUIT );
273   }
274
275   // show load screen
276   ui_loadscreen();
277
278   // store flag if we're doing preview caching or not
279   if ( pnd_conf_get_as_int_d ( g_conf, "previewpic.do_cache", 0 ) ) {
280     g_pvwcache = 1;
281   }
282
283   // set up static image cache
284   if ( ! ui_imagecache ( g_skinpath ) ) {
285     pnd_log ( pndn_error, "ERROR: Couldn't set up static UI image cache!\n" );
286     emit_and_quit ( MM_QUIT );
287   }
288
289   // create all cat
290   if ( pnd_conf_get_as_int_d ( g_conf, "categories.do_all_cat", 1 ) ) {
291     category_push ( g_x11_present ? CATEGORY_ALL "    (X11)" : CATEGORY_ALL "   (No X11)", NULL, 0 );
292   }
293
294   // set up category mappings
295   if ( pnd_conf_get_as_int_d ( g_conf, "categories.map_on", 0 ) ) {
296     g_catmap = category_map_setup();
297   }
298
299   /* inhale applications, icons, categories, etc
300    */
301   applications_scan();
302
303   /* actual work now
304    */
305
306   while ( 1 ) { // forever!
307
308     // show the menu, or changes thereof
309     ui_render();
310
311     // wait for input or time-based events (like animations)
312     // deal with inputs
313     ui_process_input ( 1 /* block */ );
314
315     // sleep? block?
316     usleep ( 5000 );
317
318   } // while
319
320   return ( 0 );
321 }
322
323 void emit_and_quit ( char *s ) {
324   printf ( "%s\n", s );
325   exit ( 0 );
326 }
327
328 void applications_free ( void ) {
329
330   // free up all our category apprefs, but keep the preview and icon cache's..
331   category_freeall();
332
333   // free up old disco_t
334   if ( g_active_apps ) {
335     pnd_disco_t *p = pnd_box_get_head ( g_active_apps );
336     pnd_disco_t *n;
337     while ( p ) {
338       n = pnd_box_get_next ( p );
339       pnd_disco_destroy ( p );
340       p = n;
341     }
342     pnd_box_delete ( g_active_apps );
343   }
344
345   return;
346 }
347
348 void applications_scan ( void ) {
349
350   // show disco screen
351   ui_discoverscreen ( 1 /* clear screen */ );
352
353   // determine current app list, cache icons
354   // - ignore overrides for now
355
356   g_active_apps = 0;
357   pnd_box_handle merge_apps = 0;
358
359   // desktop apps?
360   if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.desktop_apps", 1 ) ) {
361     pnd_log ( pndn_debug, "Looking for pnd applications here: %s\n",
362               pnd_conf_get_as_char ( g_desktopconf, "desktop.searchpath" ) );
363     g_active_apps = pnd_disco_search ( pnd_conf_get_as_char ( g_desktopconf, "desktop.searchpath" ), NULL );
364   }
365
366   // menu apps?
367   if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.menu_apps", 1 ) ) {
368     pnd_log ( pndn_debug, "Looking for pnd applications here: %s\n",
369               pnd_conf_get_as_char ( g_desktopconf, "menu.searchpath" ) );
370     merge_apps = pnd_disco_search ( pnd_conf_get_as_char ( g_desktopconf, "menu.searchpath" ), NULL );
371   }
372
373   // merge lists
374   if ( merge_apps ) {
375     if ( g_active_apps ) {
376       // got menu apps, and got desktop apps, merge
377       pnd_box_append ( g_active_apps, merge_apps );
378     } else {
379       // got menu apps, had no desktop apps, so just assign
380       g_active_apps = merge_apps;
381     }
382   }
383
384   // aux apps?
385   char *aux_apps = NULL;
386   merge_apps = 0;
387   aux_apps = pnd_conf_get_as_char ( g_conf, "minimenu.aux_searchpath" );
388   if ( aux_apps && aux_apps [ 0 ] ) {
389     pnd_log ( pndn_debug, "Looking for pnd applications here: %s\n", aux_apps );
390     merge_apps = pnd_disco_search ( aux_apps, NULL );
391   }
392
393   // merge aux apps
394   if ( merge_apps ) {
395     if ( g_active_apps ) {
396       pnd_box_append ( g_active_apps, merge_apps );
397     } else {
398       g_active_apps = merge_apps;
399     }
400   }
401
402   // do it
403   g_active_appcount = pnd_box_get_size ( g_active_apps );
404
405   unsigned char maxwidth, maxheight;
406   maxwidth = pnd_conf_get_as_int_d ( g_conf, "grid.icon_max_width", 50 );
407   maxheight = pnd_conf_get_as_int_d ( g_conf, "grid.icon_max_height", 50 );
408
409   // show cache screen
410   ui_cachescreen ( 1 /* clear screen */, NULL );
411
412   pnd_log ( pndn_debug, "Found pnd applications, and caching icons:\n" );
413   pnd_disco_t *iter = pnd_box_get_head ( g_active_apps );
414   unsigned int itercount = 0;
415   while ( iter ) {
416     //pnd_log ( pndn_debug, "  App: '%s'\n", IFNULL(iter->title_en,"No Name") );
417
418     // update cachescreen
419     // ... every 5 filenames, just to avoid slowing it too much
420     if ( itercount % 5 == 0 ) {
421       ui_cachescreen ( 0 /* clear screen */, IFNULL(iter->title_en,"No Name") );
422     }
423
424     // if an ovr was flagged by libpnd, lets go inhale it just so we've got the
425     // notes handy, since notes are not handled by libpnd proper
426     pnd_conf_handle ovrh = 0;
427     if ( iter -> object_flags & PND_DISCO_FLAG_OVR ) {
428       char ovrfile [ PATH_MAX ];
429       char *fixpxml;
430       sprintf ( ovrfile, "%s/%s", iter -> object_path, iter -> object_filename );
431       fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
432       strcpy ( fixpxml, PXML_SAMEPATH_OVERRIDE_FILEEXT );
433
434       ovrh = pnd_conf_fetch_by_path ( ovrfile );
435
436 #if 0
437       if ( ovrh ) {
438         pnd_log ( pndn_debug, "Found ovr file for %s # %u\n", iter -> object_filename, iter -> subapp_number );
439       }
440 #endif
441
442     } // ovr
443
444     // cache the icon, unless deferred
445     if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_icons_later", 0 ) == 0 ) {
446       if ( iter -> pnd_icon_pos &&
447            ! cache_icon ( iter, maxwidth, maxheight ) )
448       {
449         pnd_log ( pndn_warning, "  Couldn't load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
450       }
451     }
452
453     // cache the preview --> SHOULD DEFER
454     if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_previews_now", 0 ) > 0 ) {
455       // load the preview pics now!
456       if ( iter -> preview_pic1 &&
457            ! cache_preview ( iter, pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 200 ), pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 180 ) ) )
458       {
459         pnd_log ( pndn_warning, "  Couldn't load preview pic: '%s' -> '%s'\n", IFNULL(iter->title_en,"No Name"), iter -> preview_pic1 );
460       }
461     } // preview now?
462
463     // push the categories .. or suppress application
464     //
465     if ( ( pnd_pxml_get_x11 ( iter -> option_no_x11 ) != pnd_pxml_x11_required ) ||
466          ( pnd_pxml_get_x11 ( iter -> option_no_x11 ) == pnd_pxml_x11_required && g_x11_present == 1 )
467        )
468     {
469
470       // push to All category
471       // we do this first, so first category is always All
472       if ( pnd_conf_get_as_int_d ( g_conf, "categories.do_all_cat", 1 ) ) {
473         if ( ! category_push ( g_x11_present ? CATEGORY_ALL "    (X11)" : CATEGORY_ALL "   (No X11)", iter, ovrh ) ) {
474           pnd_log ( pndn_warning, "  Couldn't categorize to All: '%s'\n", IFNULL(iter -> title_en, "No Name") );
475         }
476       } // all?
477
478       // main categories
479       if ( iter -> main_category && pnd_conf_get_as_int_d ( g_conf, "tabs.top_maincat", 1 ) ) {
480         if ( ! category_meta_push ( iter -> main_category, iter, ovrh ) ) {
481           pnd_log ( pndn_warning, "  Couldn't categorize to %s: '%s'\n", iter -> main_category, IFNULL(iter -> title_en, "No Name") );
482         }
483       }
484
485       if ( iter -> main_category1 && pnd_conf_get_as_int_d ( g_conf, "tabs.top_maincat1", 0 ) ) {
486         if ( ! category_meta_push ( iter -> main_category1, iter, ovrh ) ) {
487           pnd_log ( pndn_warning, "  Couldn't categorize to %s: '%s'\n", iter -> main_category1, IFNULL(iter -> title_en, "No Name") );
488         }
489       }
490
491       if ( iter -> main_category2 && pnd_conf_get_as_int_d ( g_conf, "tabs.top_maincat2", 0 ) ) {
492         if ( ! category_meta_push ( iter -> main_category2, iter, ovrh ) ) {
493           pnd_log ( pndn_warning, "  Couldn't categorize to %s: '%s'\n", iter -> main_category2, IFNULL(iter -> title_en, "No Name") );
494         }
495       }
496
497       // alt categories
498       if ( iter -> alt_category && pnd_conf_get_as_int_d ( g_conf, "tabs.top_altcat", 0 ) ) {
499         if ( ! category_meta_push ( iter -> alt_category, iter, ovrh ) ) {
500           pnd_log ( pndn_warning, "  Couldn't categorize to %s: '%s'\n", iter -> alt_category, IFNULL(iter -> title_en, "No Name") );
501         }
502       }
503
504       if ( iter -> alt_category1 && pnd_conf_get_as_int_d ( g_conf, "tabs.top_altcat1", 0 ) ) {
505         if ( ! category_meta_push ( iter -> alt_category1, iter, ovrh ) ) {
506           pnd_log ( pndn_warning, "  Couldn't categorize to %s: '%s'\n", iter -> alt_category1, IFNULL(iter -> title_en, "No Name") );
507         }
508       }
509
510       if ( iter -> alt_category2 && pnd_conf_get_as_int_d ( g_conf, "tabs.top_altcat2", 0 ) ) {
511         if ( ! category_meta_push ( iter -> alt_category2, iter, ovrh ) ) {
512           pnd_log ( pndn_warning, "  Couldn't categorize to %s: '%s'\n", iter -> alt_category2, IFNULL(iter -> title_en, "No Name") );
513         }
514       }
515
516     } // register with categories or filter out
517
518     // next
519     iter = pnd_box_get_next ( iter );
520     itercount++;
521   } // while
522
523   // dump categories
524   //category_dump();
525
526   // let deferred icon cache go now
527   ui_post_scan();
528
529   return;
530 }