mmenu; fix for minor bug -- when 'do not show app in main cat, if it has subcats...
[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 #include <dirent.h>
39 #include <signal.h> // for sigaction
40
41 #include "pnd_logger.h"
42 #include "pnd_pxml.h"
43 #include "pnd_utility.h"
44 #include "pnd_conf.h"
45 #include "pnd_container.h"
46 #include "pnd_discovery.h"
47 #include "pnd_locate.h"
48 #include "pnd_device.h"
49 #include "pnd_pndfiles.h"
50 #include "../lib/pnd_pathiter.h"
51 #include "pnd_notify.h"
52 #include "pnd_dbusnotify.h"
53 #include "pnd_apps.h"
54
55 #include "mmenu.h"
56 #include "mmwrapcmd.h"
57 #include "mmapps.h"
58 #include "mmcache.h"
59 #include "mmcat.h"
60 #include "mmui.h"
61 #include "mmconf.h"
62
63 pnd_box_handle g_active_apps = NULL;
64 unsigned int g_active_appcount = 0;
65 char g_username [ 128 ]; // since we have to wait for login (!!), store username here
66 pnd_conf_handle g_conf = 0;
67 pnd_conf_handle g_desktopconf = 0;
68
69 char *pnd_run_script = NULL;
70 unsigned char g_x11_present = 1; // >0 if X is present
71 unsigned char g_catmap = 0; // if 1, we're doing category mapping
72 unsigned char g_pvwcache = 0; // if 1, we're trying to do preview caching
73 unsigned char g_autorescan = 1; // default to auto rescan
74
75 pnd_dbusnotify_handle dbh = 0; // if >0, dbusnotify is active
76 pnd_notify_handle nh = 0; // if >0, inotify is active
77
78 char g_skin_selected [ 100 ] = "default";
79 char *g_skinpath = NULL; // where 'skin_selected' is located .. the fullpath including skin-dir-name
80 pnd_conf_handle g_skinconf = NULL;
81
82 void sigquit_handler ( int n );
83 unsigned char app_is_visible ( pnd_conf_handle h, char *uniqueid );
84
85 int main ( int argc, char *argv[] ) {
86   int logall = -1; // -1 means normal logging rules; >=0 means log all!
87   int i;
88
89   // boilerplate stuff from pndnotifyd and pndevmapperd
90
91   /* iterate across args
92    */
93   for ( i = 1; i < argc; i++ ) {
94
95     if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'l' ) {
96
97       if ( isdigit ( argv [ i ][ 2 ] ) ) {
98         unsigned char x = atoi ( argv [ i ] + 2 );
99         if ( x >= 0 &&
100              x < pndn_none )
101         {
102           logall = x;
103         }
104       } else {
105         logall = 0;
106       }
107
108     } else {
109       //printf ( "Unknown: %s\n", argv [ i ] );
110       printf ( "%s [-l##]\n", argv [ 0 ] );
111       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" );
112       printf ( "-f\tFull path of frontend to run\n" );
113       exit ( 0 );
114     }
115
116   } // for
117
118   /* enable logging?
119    */
120   pnd_log_set_pretext ( "mmenu" );
121   pnd_log_set_flush ( 1 );
122
123   if ( logall == -1 ) {
124     // standard logging; non-daemon versus daemon
125
126 #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
127     struct stat statbuf;
128     if ( stat ( PND_DEVICE_BATTERY_GAUGE_PERC, &statbuf ) == 0 ) {
129       // on pandora
130       pnd_log_set_filter ( pndn_error );
131     } else {
132       pnd_log_set_filter ( pndn_debug );
133     }
134 #endif
135
136     pnd_log_to_stdout();
137
138   } else {
139     FILE *f;
140
141     f = fopen ( "/tmp/mmenu.log", "w" );
142
143     if ( f ) {
144       pnd_log_set_filter ( logall );
145       pnd_log_to_stream ( f );
146       pnd_log ( pndn_rem, "logall mode - logging to /tmp/mmenu.log\n" );
147     }
148
149     if ( logall == pndn_debug ) {
150       pnd_log_set_buried_logging ( 1 ); // log the shit out of it
151       pnd_log ( pndn_rem, "logall mode 0 - turned on buried logging\n" );
152     }
153
154   } // logall
155
156   pnd_log ( pndn_rem, "%s built %s %s", argv [ 0 ], __DATE__, __TIME__ );
157
158   pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );
159
160   // wait for a user to be logged in - we should probably get hupped when a user logs in, so we can handle
161   // log-out and back in again, with SDs popping in and out between..
162   pnd_log ( pndn_rem, "Checking to see if a user is logged in\n" );
163   while ( 1 ) {
164     if ( pnd_check_login ( g_username, 127 ) ) {
165       break;
166     }
167     pnd_log ( pndn_debug, "  No one logged in yet .. spinning.\n" );
168     sleep ( 2 );
169   } // spin
170   pnd_log ( pndn_rem, "Looks like user '%s' is in, continue.\n", g_username );
171
172   /* conf files
173    */
174
175   // mmenu conf
176   g_conf = pnd_conf_fetch_by_name ( MMENU_CONF, MMENU_CONF_SEARCHPATH );
177
178   if ( ! g_conf ) {
179     pnd_log ( pndn_error, "ERROR: Couldn't fetch conf file '%s'!\n", MMENU_CONF );
180     emit_and_quit ( MM_QUIT );
181   }
182
183   // override mmenu conf via user preference conf
184   conf_merge_into ( conf_determine_location ( g_conf ), g_conf );
185   conf_setup_missing ( g_conf );
186
187   // desktop conf for app finding preferences
188   g_desktopconf = pnd_conf_fetch_by_id ( pnd_conf_desktop, PND_CONF_SEARCHPATH );
189
190   if ( ! g_desktopconf ) {
191     pnd_log ( pndn_error, "ERROR: Couldn't fetch desktop conf file\n" );
192     emit_and_quit ( MM_QUIT );
193   }
194
195   /* set up quit signal handler
196    */
197   sigset_t ss;
198   sigemptyset ( &ss );
199
200   struct sigaction siggy;
201   siggy.sa_handler = sigquit_handler;
202   siggy.sa_mask = ss; /* implicitly blocks the origin signal */
203   siggy.sa_flags = SA_RESTART; /* don't need anything */
204   sigaction ( SIGQUIT, &siggy, NULL );
205
206   /* category conf file
207    */
208   {
209     char *locater = pnd_locate_filename ( pnd_conf_get_as_char ( g_conf, "categories.catmap_searchpath" ),
210                                           pnd_conf_get_as_char ( g_conf, "categories.catmap_confname" ) );
211
212     if ( locater ) {
213       pnd_log ( pndn_rem, "Found category conf at '%s'\n", locater );
214       pnd_conf_handle h = pnd_conf_fetch_by_path ( locater );
215       if ( h ) {
216         // lets just merge the skin conf onto the regular conf, so it just magicly works
217         pnd_box_append ( g_conf, h );
218       }
219     } else {
220       pnd_log ( pndn_debug, "No additional category conf file found.\n" );
221     }
222
223   } // cat conf
224
225   // redo log filter
226   pnd_log_set_filter ( pnd_conf_get_as_int_d ( g_conf, "minimenu.loglevel", pndn_error ) );
227
228   /* setup
229    */
230
231   // X11?
232   if ( pnd_conf_get_as_char ( g_conf, "minimenu.x11_present_sh" ) ) {
233     FILE *fx = popen ( pnd_conf_get_as_char ( g_conf, "minimenu.x11_present_sh" ), "r" );
234     char buffer [ 100 ];
235     if ( fx ) {
236       if ( fgets ( buffer, 100, fx ) ) {
237         if ( atoi ( buffer ) ) {
238           g_x11_present = 1;
239           pnd_log ( pndn_rem, "X11 seems to be present [pid %u]\n", atoi(buffer) );
240         } else {
241           g_x11_present = 0;
242           pnd_log ( pndn_rem, "X11 seems NOT to be present\n" );
243         }
244       } else {
245           g_x11_present = 0;
246           pnd_log ( pndn_rem, "X11 seems NOT to be present\n" );
247       }
248       pclose ( fx );
249     }
250   } // x11?
251
252   // pnd_run.sh
253   pnd_run_script = pnd_locate_filename ( pnd_conf_get_as_char ( g_conf, "minimenu.pndrun" ), "pnd_run.sh" );
254
255   if ( ! pnd_run_script ) {
256     pnd_log ( pndn_error, "ERROR: Couldn't locate pnd_run.sh!\n" );
257     emit_and_quit ( MM_QUIT );
258   }
259
260   pnd_run_script = strdup ( pnd_run_script ); // so we don't lose it next pnd_locate
261
262   pnd_log ( pndn_rem, "Found pnd_run.sh at '%s'\n", pnd_run_script );
263
264   // auto rescan?
265   if ( pnd_conf_get_as_int ( g_conf, "minimenu.auto_rescan" ) != PND_CONF_BADNUM ) {
266     g_autorescan = pnd_conf_get_as_int ( g_conf, "minimenu.auto_rescan" );
267   }
268   pnd_log ( pndn_debug, "application rescan is set to: %s\n", g_autorescan ? "auto" : "manual" );
269
270   // figure out what skin is selected (or default)
271   FILE *f;
272   char *s = strdup ( pnd_conf_get_as_char ( g_conf, "minimenu.skin_selected" ) );
273   s = pnd_expand_tilde ( s );
274   if ( ( f = fopen ( s, "r" ) ) ) {
275     char buffer [ 100 ];
276     if ( fgets ( buffer, 100, f ) ) {
277       // see if that dir can be located
278       if ( strchr ( buffer, '\n' ) ) {
279         * strchr ( buffer, '\n' ) = '\0';
280       }
281       char *found = pnd_locate_filename ( pnd_conf_get_as_char ( g_conf, "minimenu.skin_searchpath" ), buffer );
282       if ( found ) {
283         strncpy ( g_skin_selected, buffer, 100 );
284         g_skinpath = strdup ( found );
285       } else {
286         pnd_log ( pndn_warning, "Couldn't locate skin named '%s' so falling back.\n", buffer );
287       }
288     }
289     fclose ( f );
290   }
291   free ( s );
292
293   if ( g_skinpath ) {
294     pnd_log ( pndn_rem, "Skin is selected: '%s'\n", g_skin_selected );
295   } else {
296     pnd_log ( pndn_rem, "Skin falling back to: '%s'\n", g_skin_selected );
297
298     char *found = pnd_locate_filename ( pnd_conf_get_as_char ( g_conf, "minimenu.skin_searchpath" ),
299                                         g_skin_selected );
300     if ( found ) {
301       g_skinpath = strdup ( found );
302     } else {
303       pnd_log ( pndn_error, "Couldn't locate skin named '%s'.\n", g_skin_selected );
304       emit_and_quit ( MM_QUIT );
305     }
306
307   }
308   pnd_log ( pndn_rem, "Skin path determined to be: '%s'\n", g_skinpath );
309
310   // lets see if this skin-path actually has a skin conf
311   {
312     char fullpath [ PATH_MAX ];
313     sprintf ( fullpath, "%s/%s", g_skinpath, pnd_conf_get_as_char ( g_conf, "minimenu.skin_confname" ) );
314     g_skinconf = pnd_conf_fetch_by_path ( fullpath );
315   }
316
317   // figure out skin path if we didn't get it already
318   if ( ! g_skinconf ) {
319     pnd_log ( pndn_error, "ERROR: Couldn't set up skin!\n" );
320     emit_and_quit ( MM_QUIT );
321   }
322
323   // lets just merge the skin conf onto the regular conf, so it just magicly works
324   pnd_box_append ( g_conf, g_skinconf );
325
326   // did user override the splash image?
327   char *splash = pnd_conf_get_as_char ( g_conf, "minimenu.force_wallpaper" );
328   if ( splash ) {
329     // we've got a filename, presumably; lets see if it exists
330     struct stat statbuf;
331     if ( stat ( splash, &statbuf ) == 0 ) {
332       // file seems to exist, lets set our override to that..
333       pnd_conf_set_char ( g_conf, "graphics.IMG_BACKGROUND_800480", splash );
334     }
335   }
336
337   // attempt to set up UI
338   if ( ! ui_setup() ) {
339     pnd_log ( pndn_error, "ERROR: Couldn't set up the UI!\n" );
340     emit_and_quit ( MM_QUIT );
341   }
342
343   // show load screen
344   ui_loadscreen();
345
346   // store flag if we're doing preview caching or not
347   if ( pnd_conf_get_as_int_d ( g_conf, "previewpic.do_cache", 0 ) ) {
348     g_pvwcache = 1;
349   }
350
351   // set up static image cache
352   if ( ! ui_imagecache ( g_skinpath ) ) {
353     pnd_log ( pndn_error, "ERROR: Couldn't set up static UI image cache!\n" );
354     emit_and_quit ( MM_QUIT );
355   }
356
357   // init categories
358   category_init();
359
360   // create all cat
361   if ( pnd_conf_get_as_int_d ( g_conf, "categories.do_all_cat", 1 ) ) {
362     category_push ( g_x11_present ? CATEGORY_ALL "    (X11)" : CATEGORY_ALL "   (No X11)", NULL /* parent cat */, NULL /*app*/, 0, NULL /* fspath */, 1 /* visible */ );
363   }
364
365   // set up category mappings
366   if ( pnd_conf_get_as_int_d ( g_conf, "categories.map_on", 0 ) ) {
367     g_catmap = category_map_setup();
368   }
369
370   /* inhale applications, icons, categories, etc
371    */
372   applications_scan();
373
374   /* actual work now
375    */
376   if ( g_autorescan ) {
377
378     // set up notifications
379     dbh = pnd_dbusnotify_init();
380     pnd_log ( pndn_debug, "Setting up dbusnotify\n" );
381     //setup_notifications();
382
383     // create a timer thread, that will trigger us to check for SD insert notifications every once in awhile
384     ui_threaded_timer_create();
385
386   } // set up rescan
387
388   /* set speed to minimenu run-speed, now that we're all set up
389    */
390 #if 0 /* something crashes at high speed image caching.. */
391   int use_mm_speed = pnd_conf_get_as_int_d ( g_conf, "minimenu.use_mm_speed", 0 );
392   if ( use_mm_speed > 0 ) {
393     int mm_speed = pnd_conf_get_as_int_d ( g_conf, "minimenu.mm_speed", -1 );
394     if ( mm_speed > 50 && mm_speed < 800 ) {
395       char buffer [ 512 ];
396       snprintf ( buffer, 500, "sudo /usr/pandora/scripts/op_cpuspeed.sh %d", mm_speed );
397       system ( buffer );
398     }
399   } // do speed change?
400 #endif
401
402   // do it!
403   while ( 1 ) { // forever!
404
405     // show the menu, or changes thereof
406     ui_render();
407
408     // wait for input or time-based events (like animations) and deal with inputs
409     ui_process_input ( dbh, nh );
410
411   } // while
412
413   return ( 0 );
414 }
415
416 void emit_and_quit ( char *s ) {
417   printf ( "%s\n", s );
418   // shutdown notifications
419   if ( g_autorescan ) {
420
421     if ( dbh ) {
422       pnd_dbusnotify_shutdown ( dbh );
423     }
424     if ( nh ) {
425       pnd_notify_shutdown ( nh );
426     }
427
428   }
429
430   exit ( 0 );
431 }
432
433 static unsigned int is_dir_empty ( char *fullpath ) {
434   DIR *d = opendir ( fullpath );
435
436   if ( ! d ) {
437     return ( 0 ); // not empty, since we don't know
438   }
439
440   struct dirent *de = readdir ( d );
441
442   while ( de ) {
443
444     if ( strcmp ( de -> d_name, "." ) == 0 ) {
445       // irrelevent
446     } else if ( strcmp ( de -> d_name, ".." ) == 0 ) {
447       // irrelevent
448     } else {
449       // something else came in, so dir must not be empty
450       closedir ( d );
451       return ( 0 ); 
452     }
453
454     de = readdir ( d );
455   }
456
457   closedir ( d );
458
459   return ( 1 ); // dir is empty
460 }
461
462 void applications_free ( void ) {
463
464   // free up all our category apprefs, but keep the preview and icon cache's..
465   category_freeall();
466
467   // free up old disco_t
468   if ( g_active_apps ) {
469     pnd_disco_t *p = pnd_box_get_head ( g_active_apps );
470     pnd_disco_t *n;
471     while ( p ) {
472       n = pnd_box_get_next ( p );
473       pnd_disco_destroy ( p );
474       p = n;
475     }
476     pnd_box_delete ( g_active_apps );
477   }
478
479   return;
480 }
481
482 void applications_scan ( void ) {
483
484   // has user disabled pnd scanning, by chance?
485   if ( ! pnd_conf_get_as_int_d ( g_conf, "filesystem.do_pnd_disco", 1 ) ) {
486     goto dirbrowser_scan; // skip pnd's
487   }
488
489   // show disco screen
490   ui_discoverscreen ( 1 /* clear screen */ );
491
492   // determine current app list, cache icons
493   // - ignore overrides for now
494
495   g_active_apps = 0;
496   pnd_box_handle merge_apps = 0;
497
498   // desktop apps?
499   if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.desktop_apps", 1 ) ) {
500     pnd_log ( pndn_debug, "Looking for pnd applications here: %s\n",
501               pnd_conf_get_as_char ( g_desktopconf, "desktop.searchpath" ) );
502     g_active_apps = pnd_disco_search ( pnd_conf_get_as_char ( g_desktopconf, "desktop.searchpath" ), NULL );
503   }
504
505   // menu apps?
506   if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.menu_apps", 1 ) ) {
507     pnd_log ( pndn_debug, "Looking for pnd applications here: %s\n",
508               pnd_conf_get_as_char ( g_desktopconf, "menu.searchpath" ) );
509     merge_apps = pnd_disco_search ( pnd_conf_get_as_char ( g_desktopconf, "menu.searchpath" ), NULL );
510   }
511
512   // merge lists
513   if ( merge_apps ) {
514     if ( g_active_apps ) {
515       // the key from pnd_disco_search() is the _path_, so easy to look for duplicates
516       // this is pretty inefficient, being linked lists; perhaps should switch to hash tables when
517       // we expect thousands of apps.. or at least an index or something.
518       void *a = pnd_box_get_head ( merge_apps );
519       void *nexta = NULL;
520       while ( a ) {
521         nexta = pnd_box_get_next ( a );
522
523         // if the key for the node is also found in active apps, toss out the merging one
524         if ( pnd_box_find_by_key ( g_active_apps, pnd_box_get_key ( a ) ) ) {
525           //fprintf ( stderr, "Merging app id '%s' is duplicate; discarding it.\n", pnd_box_get_key ( a ) );
526           pnd_box_delete_node ( merge_apps, a );
527         }
528
529         a = nexta;
530       }
531
532       // got menu apps, and got desktop apps, merge
533       pnd_box_append ( g_active_apps, merge_apps );
534     } else {
535       // got menu apps, had no desktop apps, so just assign
536       g_active_apps = merge_apps;
537     }
538   }
539
540   // aux apps?
541   char *aux_apps = NULL;
542   merge_apps = 0;
543   aux_apps = pnd_conf_get_as_char ( g_conf, "minimenu.aux_searchpath" );
544   if ( aux_apps && aux_apps [ 0 ] ) {
545     pnd_log ( pndn_debug, "Looking for pnd applications here: %s\n", aux_apps );
546     merge_apps = pnd_disco_search ( aux_apps, NULL );
547   }
548
549   // merge aux apps
550   if ( merge_apps ) {
551     if ( g_active_apps ) {
552
553       // LAME: snipped from above; should just catenate the 3 sets of searchpaths into a
554       // master searchpath, possibly removing duplicate paths _then_, and keep all this much
555       // more efficient
556
557       // the key from pnd_disco_search() is the _path_, so easy to look for duplicates
558       // this is pretty inefficient, being linked lists; perhaps should switch to hash tables when
559       // we expect thousands of apps.. or at least an index or something.
560       void *a = pnd_box_get_head ( merge_apps );
561       void *nexta = NULL;
562       while ( a ) {
563         nexta = pnd_box_get_next ( a );
564
565         // if the key for the node is also found in active apps, toss out the merging one
566         if ( pnd_box_find_by_key ( g_active_apps, pnd_box_get_key ( a ) ) ) {
567           fprintf ( stderr, "Merging app id '%s' is duplicate; discarding it.\n", pnd_box_get_key ( a ) );
568           pnd_box_delete_node ( merge_apps, a );
569         }
570
571         a = nexta;
572       }
573
574       pnd_box_append ( g_active_apps, merge_apps );
575     } else {
576       g_active_apps = merge_apps;
577     }
578   }
579
580   // do it
581   g_active_appcount = pnd_box_get_size ( g_active_apps );
582
583   unsigned char maxwidth, maxheight;
584   maxwidth = pnd_conf_get_as_int_d ( g_conf, "grid.icon_max_width", 50 );
585   maxheight = pnd_conf_get_as_int_d ( g_conf, "grid.icon_max_height", 50 );
586
587   // show cache screen
588   ui_cachescreen ( 1 /* clear screen */, NULL );
589
590   pnd_log ( pndn_debug, "Found pnd applications, and caching icons:\n" );
591   pnd_disco_t *iter = pnd_box_get_head ( g_active_apps );
592   unsigned int itercount = 0;
593   while ( iter ) {
594     //pnd_log ( pndn_debug, "  App: '%s'\n", IFNULL(iter->title_en,"No Name") );
595
596     // update cachescreen
597     // ... every 5 filenames, just to avoid slowing it too much
598     if ( itercount % 5 == 0 ) {
599       ui_cachescreen ( 0 /* clear screen */, IFNULL(iter->title_en,"No Name") );
600     }
601
602     // if an ovr was flagged by libpnd, lets go inhale it just so we've got the
603     // notes handy, since notes are not handled by libpnd proper
604     pnd_conf_handle ovrh = 0;
605     if ( iter -> object_flags & PND_DISCO_FLAG_OVR ) {
606       char ovrfile [ PATH_MAX ];
607       char *fixpxml;
608       sprintf ( ovrfile, "%s/%s", iter -> object_path, iter -> object_filename );
609       fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
610       strcpy ( fixpxml, PXML_SAMEPATH_OVERRIDE_FILEEXT );
611
612       ovrh = pnd_conf_fetch_by_path ( ovrfile );
613
614 #if 0
615       if ( ovrh ) {
616         pnd_log ( pndn_debug, "Found ovr file for %s # %u\n", iter -> object_filename, iter -> subapp_number );
617       }
618 #endif
619
620       if ( ovrh ) {
621         // lets also check to see if this ovr is specifying category overrides; if so, we can trust those
622         // more than categories specified by pnd-packager.
623         char ovrkey [ 41 ];
624
625         snprintf ( ovrkey, 40, "Application-%u.maincategory", iter -> subapp_number );
626         if ( pnd_conf_get_as_char ( ovrh, ovrkey ) ) {
627           iter -> object_flags |= PND_DISCO_CUSTOM1;
628           //printf ( "App '%s' has main cat ovr %s\n", iter -> title_en, pnd_conf_get_as_char ( ovrh, ovrkey ) );
629         }
630
631         snprintf ( ovrkey, 40, "Application-%u.maincategorysub1", iter -> subapp_number );
632         if ( pnd_conf_get_as_char ( ovrh, ovrkey ) ) {
633           iter -> object_flags |= PND_DISCO_CUSTOM2;
634           //printf ( "App '%s' has sub cat ovr %s\n", iter -> title_en, pnd_conf_get_as_char ( ovrh, ovrkey ) );
635         }
636
637       } // got ovr loaded/
638
639     } // ovr
640
641     // cache the icon, unless deferred
642     if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_icons_later", 0 ) == 0 ) {
643       if ( iter -> pnd_icon_pos &&
644            ! cache_icon ( iter, maxwidth, maxheight ) )
645       {
646         pnd_log ( pndn_warning, "  Couldn't load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
647       }
648     }
649
650     // cache the preview --> SHOULD DEFER
651     if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_previews_now", 0 ) > 0 ) {
652       // load the preview pics now!
653       if ( iter -> preview_pic1 &&
654            ! 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 ) ) )
655       {
656         pnd_log ( pndn_warning, "  Couldn't load preview pic: '%s' -> '%s'\n", IFNULL(iter->title_en,"No Name"), iter -> preview_pic1 );
657       }
658     } // preview now?
659
660     // push the categories .. or suppress application
661     //
662     if ( ( pnd_pxml_get_x11 ( iter -> option_no_x11 ) != pnd_pxml_x11_required ) ||
663          ( pnd_pxml_get_x11 ( iter -> option_no_x11 ) == pnd_pxml_x11_required && g_x11_present == 1 )
664        )
665     {
666  
667       if ( iter -> title_en == NULL || iter -> title_en [ 0 ] == '\0' ) {
668         // null title; just skip it.
669       } else {
670
671         // push to All category
672         // we do this first, so first category is always All
673         if ( pnd_conf_get_as_int_d ( g_conf, "categories.do_all_cat", 1 ) ) {
674           category_push ( g_x11_present ? CATEGORY_ALL "    (X11)" : CATEGORY_ALL "   (No X11)", NULL /* parent cat */, iter, ovrh, NULL /* fspath */, 1 /* visible */ );
675         } // all?
676
677         // is this app suppressed? if not, show it in whatever categories the user is allowing
678         if ( iter -> unique_id && app_is_visible ( g_conf, iter -> unique_id ) ) {
679
680 #if 0
681           pnd_log ( pndn_rem, "App %s [%s] cat %s %s %s alt %s %s %s\n",
682                     iter -> unique_id, IFNULL(iter->title_en,"n/a"),
683                     IFNULL(iter->main_category,"n/a"), IFNULL(iter->main_category1,"n/a"), IFNULL(iter->main_category2,"n/a"),
684                     IFNULL(iter->alt_category,"n/a"), IFNULL(iter->alt_category1,"n/a"), IFNULL(iter->alt_category2,"n/a") );
685 #endif
686
687           // main categories
688           category_meta_push ( iter -> main_category, NULL /* no parent cat */, iter, ovrh, cat_is_visible ( g_conf, iter -> main_category ), 1);
689           category_meta_push ( iter -> main_category1, iter -> main_category, iter, ovrh, cat_is_visible ( g_conf, iter -> main_category1 ), 0 );
690           category_meta_push ( iter -> main_category2, iter -> main_category, iter, ovrh, cat_is_visible ( g_conf, iter -> main_category2 ), 0 );
691           // alt categories
692           category_meta_push ( iter -> alt_category, NULL /* no parent cat */, iter, ovrh, cat_is_visible ( g_conf, iter -> alt_category ), 2 );
693           category_meta_push ( iter -> alt_category1, iter -> alt_category, iter, ovrh, cat_is_visible ( g_conf, iter -> alt_category1 ), 0 );
694           category_meta_push ( iter -> alt_category2, iter -> alt_category, iter, ovrh, cat_is_visible ( g_conf, iter -> alt_category2 ), 0 );
695
696         } // app is visible?
697
698       } // has title?
699
700     } // register with categories or filter out
701
702     // next
703     iter = pnd_box_get_next ( iter );
704     itercount++;
705   } // while
706
707  dirbrowser_scan:
708
709   // set up filesystem browser tabs
710   if ( pnd_conf_get_as_int_d ( g_conf, "filesystem.do_browser", 0 ) ) {
711     char *searchpath = pnd_conf_get_as_char ( g_conf, "filesystem.tab_searchpaths" );
712
713     SEARCHPATH_PRE
714     {
715       char *c, *tabname;
716       c = strrchr ( buffer, '/' );
717       if ( c && (*(c+1)!='\0') ) {
718         tabname = c;
719       } else {
720         tabname = buffer;
721       }
722
723       // check if dir is empty; if so, skip it.
724       if ( ! is_dir_empty ( buffer ) ) {
725         category_push ( tabname /* tab name */, NULL /* parent cat */, NULL /* app */, 0 /* override */, buffer /* fspath */, 1 /* visible */ );
726       }
727
728     }
729     SEARCHPATH_POST
730
731   } // set up fs browser tabs
732
733   // dump categories
734   //category_dump();
735
736   // publish desired categories
737   category_publish ( CFNORMAL, NULL );
738
739   // let deferred icon cache go now
740   ui_post_scan();
741
742   return;
743 }
744
745 static char _vbuf [ 512 ];
746 unsigned char cat_is_visible ( pnd_conf_handle h, char *catname ) {
747   snprintf ( _vbuf, 500, "tabshow.%s", catname );
748   return ( pnd_conf_get_as_int_d ( g_conf, _vbuf, 1 ) ); // default to 'show' when unknown
749 }
750
751 unsigned char app_is_visible ( pnd_conf_handle h, char *uniqueid ) {
752   snprintf ( _vbuf, 500, "appshow.%s", uniqueid );
753   return ( pnd_conf_get_as_int_d ( g_conf, _vbuf, 1 ) ); // default to 'show' when unknown
754 }
755
756 void sigquit_handler ( int n ) {
757   pnd_log ( pndn_rem, "SIGQUIT received; graceful exit.\n" );
758   emit_and_quit ( MM_QUIT );
759 }
760
761 void setup_notifications ( void ) {
762
763   // figure out notify path
764   char *configpath;
765   char *notifypath = NULL;
766
767   configpath = pnd_conf_query_searchpath();
768
769   pnd_conf_handle apph;
770
771   apph = pnd_conf_fetch_by_id ( pnd_conf_apps, configpath );
772
773   if ( apph ) {
774
775     notifypath = pnd_conf_get_as_char ( apph, PND_APPS_NOTIFY_KEY );
776
777     if ( ! notifypath ) {
778       notifypath = PND_APPS_NOTIFYPATH;
779     }
780
781   }
782
783   // given notify path.. ripped from pndnotifyd :(
784   char *searchpath = notifypath;
785
786   // if this is first time through, we can just set it up; for subsequent times
787   // through, we need to close existing fd and re-open it, since we're too lame
788   // to store the list of watches and 'rm' them
789 #if 1
790   if ( nh ) {
791     pnd_notify_shutdown ( nh );
792     nh = 0;
793   }
794 #endif
795
796   // set up a new set of notifies
797   if ( ! nh ) {
798     nh = pnd_notify_init();
799   }
800
801   if ( ! nh ) {
802     pnd_log ( pndn_rem, "INOTIFY failed to init.\n" );
803     exit ( -1 );
804   }
805
806 #if 0
807   pnd_log ( pndn_rem, "INOTIFY is up.\n" );
808 #endif
809
810   SEARCHPATH_PRE
811   {
812
813     pnd_log ( pndn_rem, "Watching path '%s' and its descendents.\n", buffer );
814     pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
815
816   }
817   SEARCHPATH_POST
818
819   return;
820 }
821
822 // for Pleng
823 // Goal: normally menu will quit when an app is invoked, but there are cases when some folks
824 // may configure their system and want mmenu to live instead (save on restart time, don't care
825 // about RAM, using a multitasking tray/window manager setup...), so instead of 'exit and emit'
826 // here we just run the app directly and cross fingers!
827 void emit_and_run ( char *buffer ) {
828
829   // run the bloody thing
830   int f;
831
832   if ( ( f = fork() ) < 0 ) {
833     // error forking
834   } else if ( f > 0 ) {
835     // parent
836   } else {
837     // child, do it
838     execl ( "/bin/sh", "/bin/sh", "-c", buffer + strlen(MM_RUN) + 1, (char*) NULL );
839   } 
840
841   return;
842 }
843
844 // this code was swiped from pnd_utility pnd_exec_no_wait_1 as it became a little too minimenu-specific to remain there
845 void exec_raw_binary ( char *fullpath ) {
846   int i;
847
848   if ( ( i = fork() ) < 0 ) {
849     printf ( "ERROR: Couldn't fork()\n" );
850     return;
851   }
852
853   if ( i ) {
854     return; // parent process, don't care
855   }
856
857   // child process, do something
858   execl ( "/bin/sh", "/bin/sh", "-c", fullpath, (char*) NULL );
859   //execl ( fullpath, fullpath, (char*) NULL );
860
861   // error invoking something, and we're the child process, so just die before all hell breaks lose with us thinking we're the (second!) parent on return!
862   exit ( -1 );
863
864   // getting here is an error
865   //printf ( "Error attempting to run %s\n", fullpath );
866
867   return;
868 }