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