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