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