Directory browser is now available in config panel, and works! Will only run files...
[pandora-libraries.git] / minimenu / mmenu.c
1
2 /* minimenu
3  * aka "2wm" - too weak menu, two week menu, akin to twm
4  *
5  * Craig wants a super minimal menu ASAP before launch, so lets see what I can put together in 2 weeks with not much
6  * free time ;) I'd like to do a fuller ('tiny', but with plugin support and a decent expansion and customizing design..)
7  * but later, baby!
8  *
9  */
10
11 /* mmenu - This is the actual menu
12  * The goal of this app is to show a application picker screen of some sort, allow the user to perform some useful
13  * activities (such as set clock speed, say), and request an app to run, or shutdown, etc.
14  * To keep the memory footprint down, when invoking an application, the menu _exits_, and simply spits out
15  * an operation for mmwrapper to perform. In the case of no wrapper, the menu will just exit, which is handy for
16  * debugging.
17  */
18
19 /* mmenu lifecycle:
20  * 1) determine app list (via pnd scan, .desktop scan, whatever voodoo)
21  * 2) show a menu, allow user to interact:
22  *    a) user picks an application to run, or -> exit, pass shell run line to wrapper
23  *    b) user requests menu shutdown -> exit, tell wrapper to exit as well
24  *    c) user performsn some operation (set clock, copy files, whatever) -> probably stays within the menu
25  */
26
27 #include <stdio.h> /* for FILE etc */
28 #include <stdlib.h> /* for malloc */
29 #include <unistd.h> /* for unlink */
30 #include <limits.h> /* for PATH_MAX */
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #define __USE_GNU /* for strcasestr */
34 #include <string.h> /* for making ftw.h happy */
35 #include <strings.h>
36 #include <ctype.h>
37 #include <sys/wait.h>
38 #include <dirent.h>
39 #include <signal.h> // for sigaction
40
41 #include "pnd_logger.h"
42 #include "pnd_pxml.h"
43 #include "pnd_utility.h"
44 #include "pnd_conf.h"
45 #include "pnd_container.h"
46 #include "pnd_discovery.h"
47 #include "pnd_locate.h"
48 #include "pnd_device.h"
49 #include "pnd_pndfiles.h"
50 #include "../lib/pnd_pathiter.h"
51 #include "pnd_notify.h"
52 #include "pnd_dbusnotify.h"
53 #include "pnd_apps.h"
54
55 #include "mmenu.h"
56 #include "mmwrapcmd.h"
57 #include "mmapps.h"
58 #include "mmcache.h"
59 #include "mmcat.h"
60 #include "mmui.h"
61 #include "mmconf.h"
62
63 pnd_box_handle g_active_apps = NULL;
64 unsigned int g_active_appcount = 0;
65 char g_username [ 128 ]; // since we have to wait for login (!!), store username here
66 pnd_conf_handle g_conf = 0;
67 pnd_conf_handle g_desktopconf = 0;
68
69 char *pnd_run_script = NULL;
70 unsigned char g_x11_present = 1; // >0 if X is present
71 unsigned char g_catmap = 0; // if 1, we're doing category mapping
72 unsigned char g_pvwcache = 0; // if 1, we're trying to do preview caching
73 unsigned char g_autorescan = 1; // default to auto rescan
74
75 pnd_dbusnotify_handle dbh = 0; // if >0, dbusnotify is active
76 pnd_notify_handle nh = 0; // if >0, inotify is active
77
78 char g_skin_selected [ 100 ] = "default";
79 char *g_skinpath = NULL; // where 'skin_selected' is located .. the fullpath including skin-dir-name
80 pnd_conf_handle g_skinconf = NULL;
81
82 void sigquit_handler ( int n );
83 unsigned char cat_is_visible ( pnd_conf_handle h, char *catname );
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   conf_merge_into ( conf_determine_location ( g_conf ), g_conf );
186   conf_setup_missing ( g_conf );
187
188   // desktop conf for app finding preferences
189   g_desktopconf = pnd_conf_fetch_by_id ( pnd_conf_desktop, PND_CONF_SEARCHPATH );
190
191   if ( ! g_desktopconf ) {
192     pnd_log ( pndn_error, "ERROR: Couldn't fetch desktop conf file\n" );
193     emit_and_quit ( MM_QUIT );
194   }
195
196   /* set up quit signal handler
197    */
198   sigset_t ss;
199   sigemptyset ( &ss );
200
201   struct sigaction siggy;
202   siggy.sa_handler = sigquit_handler;
203   siggy.sa_mask = ss; /* implicitly blocks the origin signal */
204   siggy.sa_flags = SA_RESTART; /* don't need anything */
205   sigaction ( SIGQUIT, &siggy, NULL );
206
207   /* category conf file
208    */
209   {
210     char *locater = pnd_locate_filename ( pnd_conf_get_as_char ( g_conf, "categories.catmap_searchpath" ),
211                                           pnd_conf_get_as_char ( g_conf, "categories.catmap_confname" ) );
212
213     if ( locater ) {
214       pnd_log ( pndn_rem, "Found category conf at '%s'\n", locater );
215       pnd_conf_handle h = pnd_conf_fetch_by_path ( locater );
216       if ( h ) {
217         // lets just merge the skin conf onto the regular conf, so it just magicly works
218         pnd_box_append ( g_conf, h );
219       }
220     } else {
221       pnd_log ( pndn_debug, "No additional category conf file found.\n" );
222     }
223
224   } // cat conf
225
226   // redo log filter
227   pnd_log_set_filter ( pnd_conf_get_as_int_d ( g_conf, "minimenu.loglevel", pndn_error ) );
228
229   /* setup
230    */
231
232   // X11?
233   if ( pnd_conf_get_as_char ( g_conf, "minimenu.x11_present_sh" ) ) {
234     FILE *fx = popen ( pnd_conf_get_as_char ( g_conf, "minimenu.x11_present_sh" ), "r" );
235     char buffer [ 100 ];
236     if ( fx ) {
237       if ( fgets ( buffer, 100, fx ) ) {
238         if ( atoi ( buffer ) ) {
239           g_x11_present = 1;
240           pnd_log ( pndn_rem, "X11 seems to be present [pid %u]\n", atoi(buffer) );
241         } else {
242           g_x11_present = 0;
243           pnd_log ( pndn_rem, "X11 seems NOT to be present\n" );
244         }
245       } else {
246           g_x11_present = 0;
247           pnd_log ( pndn_rem, "X11 seems NOT to be present\n" );
248       }
249       pclose ( fx );
250     }
251   } // x11?
252
253   // pnd_run.sh
254   pnd_run_script = pnd_locate_filename ( pnd_conf_get_as_char ( g_conf, "minimenu.pndrun" ), "pnd_run.sh" );
255
256   if ( ! pnd_run_script ) {
257     pnd_log ( pndn_error, "ERROR: Couldn't locate pnd_run.sh!\n" );
258     emit_and_quit ( MM_QUIT );
259   }
260
261   pnd_run_script = strdup ( pnd_run_script ); // so we don't lose it next pnd_locate
262
263   pnd_log ( pndn_rem, "Found pnd_run.sh at '%s'\n", pnd_run_script );
264
265   // auto rescan?
266   if ( pnd_conf_get_as_int ( g_conf, "minimenu.auto_rescan" ) != PND_CONF_BADNUM ) {
267     g_autorescan = pnd_conf_get_as_int ( g_conf, "minimenu.auto_rescan" );
268   }
269   pnd_log ( pndn_debug, "application rescan is set to: %s\n", g_autorescan ? "auto" : "manual" );
270
271   // figure out what skin is selected (or default)
272   FILE *f;
273   char *s = strdup ( pnd_conf_get_as_char ( g_conf, "minimenu.skin_selected" ) );
274   s = pnd_expand_tilde ( s );
275   if ( ( f = fopen ( s, "r" ) ) ) {
276     char buffer [ 100 ];
277     if ( fgets ( buffer, 100, f ) ) {
278       // see if that dir can be located
279       if ( strchr ( buffer, '\n' ) ) {
280         * strchr ( buffer, '\n' ) = '\0';
281       }
282       char *found = pnd_locate_filename ( pnd_conf_get_as_char ( g_conf, "minimenu.skin_searchpath" ), buffer );
283       if ( found ) {
284         strncpy ( g_skin_selected, buffer, 100 );
285         g_skinpath = strdup ( found );
286       } else {
287         pnd_log ( pndn_warning, "Couldn't locate skin named '%s' so falling back.\n", buffer );
288       }
289     }
290     fclose ( f );
291   }
292   free ( s );
293
294   if ( g_skinpath ) {
295     pnd_log ( pndn_rem, "Skin is selected: '%s'\n", g_skin_selected );
296   } else {
297     pnd_log ( pndn_rem, "Skin falling back to: '%s'\n", g_skin_selected );
298
299     char *found = pnd_locate_filename ( pnd_conf_get_as_char ( g_conf, "minimenu.skin_searchpath" ),
300                                         g_skin_selected );
301     if ( found ) {
302       g_skinpath = strdup ( found );
303     } else {
304       pnd_log ( pndn_error, "Couldn't locate skin named '%s'.\n", g_skin_selected );
305       emit_and_quit ( MM_QUIT );
306     }
307
308   }
309   pnd_log ( pndn_rem, "Skin path determined to be: '%s'\n", g_skinpath );
310
311   // lets see if this skin-path actually has a skin conf
312   {
313     char fullpath [ PATH_MAX ];
314     sprintf ( fullpath, "%s/%s", g_skinpath, pnd_conf_get_as_char ( g_conf, "minimenu.skin_confname" ) );
315     g_skinconf = pnd_conf_fetch_by_path ( fullpath );
316   }
317
318   // figure out skin path if we didn't get it already
319   if ( ! g_skinconf ) {
320     pnd_log ( pndn_error, "ERROR: Couldn't set up skin!\n" );
321     emit_and_quit ( MM_QUIT );
322   }
323
324   // lets just merge the skin conf onto the regular conf, so it just magicly works
325   pnd_box_append ( g_conf, g_skinconf );
326
327   // did user override the splash image?
328   char *splash = pnd_conf_get_as_char ( g_conf, "minimenu.force_wallpaper" );
329   if ( splash ) {
330     // we've got a filename, presumably; lets see if it exists
331     struct stat statbuf;
332     if ( stat ( splash, &statbuf ) == 0 ) {
333       // file seems to exist, lets set our override to that..
334       pnd_conf_set_char ( g_conf, "graphics.IMG_BACKGROUND_800480", splash );
335     }
336   }
337
338   // attempt to set up UI
339   if ( ! ui_setup() ) {
340     pnd_log ( pndn_error, "ERROR: Couldn't set up the UI!\n" );
341     emit_and_quit ( MM_QUIT );
342   }
343
344   // show load screen
345   ui_loadscreen();
346
347   // store flag if we're doing preview caching or not
348   if ( pnd_conf_get_as_int_d ( g_conf, "previewpic.do_cache", 0 ) ) {
349     g_pvwcache = 1;
350   }
351
352   // set up static image cache
353   if ( ! ui_imagecache ( g_skinpath ) ) {
354     pnd_log ( pndn_error, "ERROR: Couldn't set up static UI image cache!\n" );
355     emit_and_quit ( MM_QUIT );
356   }
357
358   // create all cat
359   if ( pnd_conf_get_as_int_d ( g_conf, "categories.do_all_cat", 1 ) ) {
360     category_push ( g_x11_present ? CATEGORY_ALL "    (X11)" : CATEGORY_ALL "   (No X11)", NULL /*app*/, 0, NULL /* fspath */ );
361   }
362
363   // set up category mappings
364   if ( pnd_conf_get_as_int_d ( g_conf, "categories.map_on", 0 ) ) {
365     g_catmap = category_map_setup();
366   }
367
368   /* inhale applications, icons, categories, etc
369    */
370   applications_scan();
371
372   /* actual work now
373    */
374   unsigned char block = 1;
375
376   if ( g_autorescan ) {
377     block = 0;
378
379     // set up notifications
380     dbh = pnd_dbusnotify_init();
381     pnd_log ( pndn_debug, "Setting up dbusnotify\n" );
382     //setup_notifications();
383
384   } // set up rescan
385
386   /* set speed to minimenu run-speed, now that we're all set up
387    */
388 #if 0 /* something crashes at high speed image caching.. */
389   int use_mm_speed = pnd_conf_get_as_int_d ( g_conf, "minimenu.use_mm_speed", 0 );
390   if ( use_mm_speed > 0 ) {
391     int mm_speed = pnd_conf_get_as_int_d ( g_conf, "minimenu.mm_speed", -1 );
392     if ( mm_speed > 50 && mm_speed < 800 ) {
393       char buffer [ 512 ];
394       snprintf ( buffer, 500, "sudo /usr/pandora/scripts/op_cpuspeed.sh %d", mm_speed );
395       system ( buffer );
396     }
397   } // do speed change?
398 #endif
399
400   // do it!
401   while ( 1 ) { // forever!
402
403     // show the menu, or changes thereof
404     ui_render();
405
406     // wait for input or time-based events (like animations)
407     // deal with inputs
408     ui_process_input ( block /* block */ );
409
410     // did a rescan event trigger?
411     if ( g_autorescan ) {
412       unsigned char watch_dbus = 0;
413       unsigned char watch_inotify = 0;
414
415       if ( dbh ) {
416         watch_dbus = pnd_dbusnotify_rediscover_p ( dbh );
417       }
418
419       if ( nh ) {
420         watch_inotify = pnd_notify_rediscover_p ( nh );
421       }
422
423       if ( watch_dbus || watch_inotify ) {
424         pnd_log ( pndn_debug, "dbusnotify detected SD event\n" );
425         applications_free();
426         applications_scan();
427       }
428
429     } // rescan?
430
431     // sleep? block?
432     usleep ( 5000 );
433
434   } // while
435
436   return ( 0 );
437 }
438
439 void emit_and_quit ( char *s ) {
440   printf ( "%s\n", s );
441   // shutdown notifications
442   if ( g_autorescan ) {
443
444     if ( dbh ) {
445       pnd_dbusnotify_shutdown ( dbh );
446     }
447     if ( nh ) {
448       pnd_notify_shutdown ( nh );
449     }
450
451   }
452
453   exit ( 0 );
454 }
455
456 static unsigned int is_dir_empty ( char *fullpath ) {
457   DIR *d = opendir ( fullpath );
458
459   if ( ! d ) {
460     return ( 0 ); // not empty, since we don't know
461   }
462
463   struct dirent *de = readdir ( d );
464
465   while ( de ) {
466
467     if ( strcmp ( de -> d_name, "." ) == 0 ) {
468       // irrelevent
469     } else if ( strcmp ( de -> d_name, ".." ) == 0 ) {
470       // irrelevent
471     } else {
472       // something else came in, so dir must not be empty
473       closedir ( d );
474       return ( 0 ); 
475     }
476
477     de = readdir ( d );
478   }
479
480   closedir ( d );
481
482   return ( 1 ); // dir is empty
483 }
484
485 void applications_free ( void ) {
486
487   // free up all our category apprefs, but keep the preview and icon cache's..
488   category_freeall();
489
490   // free up old disco_t
491   if ( g_active_apps ) {
492     pnd_disco_t *p = pnd_box_get_head ( g_active_apps );
493     pnd_disco_t *n;
494     while ( p ) {
495       n = pnd_box_get_next ( p );
496       pnd_disco_destroy ( p );
497       p = n;
498     }
499     pnd_box_delete ( g_active_apps );
500   }
501
502   return;
503 }
504
505 void applications_scan ( void ) {
506
507   // show disco screen
508   ui_discoverscreen ( 1 /* clear screen */ );
509
510   // determine current app list, cache icons
511   // - ignore overrides for now
512
513   g_active_apps = 0;
514   pnd_box_handle merge_apps = 0;
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   // do it
599   g_active_appcount = pnd_box_get_size ( g_active_apps );
600
601   unsigned char maxwidth, maxheight;
602   maxwidth = pnd_conf_get_as_int_d ( g_conf, "grid.icon_max_width", 50 );
603   maxheight = pnd_conf_get_as_int_d ( g_conf, "grid.icon_max_height", 50 );
604
605   // show cache screen
606   ui_cachescreen ( 1 /* clear screen */, NULL );
607
608   pnd_log ( pndn_debug, "Found pnd applications, and caching icons:\n" );
609   pnd_disco_t *iter = pnd_box_get_head ( g_active_apps );
610   unsigned int itercount = 0;
611   while ( iter ) {
612     //pnd_log ( pndn_debug, "  App: '%s'\n", IFNULL(iter->title_en,"No Name") );
613
614     // update cachescreen
615     // ... every 5 filenames, just to avoid slowing it too much
616     if ( itercount % 5 == 0 ) {
617       ui_cachescreen ( 0 /* clear screen */, IFNULL(iter->title_en,"No Name") );
618     }
619
620     // if an ovr was flagged by libpnd, lets go inhale it just so we've got the
621     // notes handy, since notes are not handled by libpnd proper
622     pnd_conf_handle ovrh = 0;
623     if ( iter -> object_flags & PND_DISCO_FLAG_OVR ) {
624       char ovrfile [ PATH_MAX ];
625       char *fixpxml;
626       sprintf ( ovrfile, "%s/%s", iter -> object_path, iter -> object_filename );
627       fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
628       strcpy ( fixpxml, PXML_SAMEPATH_OVERRIDE_FILEEXT );
629
630       ovrh = pnd_conf_fetch_by_path ( ovrfile );
631
632 #if 0
633       if ( ovrh ) {
634         pnd_log ( pndn_debug, "Found ovr file for %s # %u\n", iter -> object_filename, iter -> subapp_number );
635       }
636 #endif
637
638     } // ovr
639
640     // cache the icon, unless deferred
641     if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_icons_later", 0 ) == 0 ) {
642       if ( iter -> pnd_icon_pos &&
643            ! cache_icon ( iter, maxwidth, maxheight ) )
644       {
645         pnd_log ( pndn_warning, "  Couldn't load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
646       }
647     }
648
649     // cache the preview --> SHOULD DEFER
650     if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_previews_now", 0 ) > 0 ) {
651       // load the preview pics now!
652       if ( iter -> preview_pic1 &&
653            ! 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 ) ) )
654       {
655         pnd_log ( pndn_warning, "  Couldn't load preview pic: '%s' -> '%s'\n", IFNULL(iter->title_en,"No Name"), iter -> preview_pic1 );
656       }
657     } // preview now?
658
659     // push the categories .. or suppress application
660     //
661     if ( ( pnd_pxml_get_x11 ( iter -> option_no_x11 ) != pnd_pxml_x11_required ) ||
662          ( pnd_pxml_get_x11 ( iter -> option_no_x11 ) == pnd_pxml_x11_required && g_x11_present == 1 )
663        )
664     {
665
666       // push to All category
667       // we do this first, so first category is always All
668       if ( pnd_conf_get_as_int_d ( g_conf, "categories.do_all_cat", 1 ) ) {
669         category_push ( g_x11_present ? CATEGORY_ALL "    (X11)" : CATEGORY_ALL "   (No X11)", iter, ovrh, NULL /* fspath */ );
670       } // all?
671
672       // is this app suppressed? if not, show it in whatever categories the user is allowing
673       if ( iter -> unique_id && app_is_visible ( g_conf, iter -> unique_id ) ) {
674
675         // main categories
676         category_meta_push ( iter -> main_category, NULL /* no parent cat */, iter, ovrh, cat_is_visible ( g_conf, iter -> main_category ) ); //pnd_conf_get_as_int_d ( g_conf, "tabs.top_maincat", 1 ) );
677         category_meta_push ( iter -> main_category1, iter -> main_category, iter, ovrh, cat_is_visible ( g_conf, iter -> main_category1 ) ); //pnd_conf_get_as_int_d ( g_conf, "tabs.top_maincat1", 0 ) );
678         category_meta_push ( iter -> main_category2, iter -> main_category, iter, ovrh, cat_is_visible ( g_conf, iter -> main_category2 ) ); //pnd_conf_get_as_int_d ( g_conf, "tabs.top_maincat2", 0 ) );
679         // alt categories
680         category_meta_push ( iter -> alt_category, NULL /* no parent cat */, iter, ovrh, cat_is_visible ( g_conf, iter -> alt_category ) ); //pnd_conf_get_as_int_d ( g_conf, "tabs.top_altcat", 0 ) );
681         category_meta_push ( iter -> alt_category1, iter -> alt_category, iter, ovrh, cat_is_visible ( g_conf, iter -> alt_category1 ) ); //pnd_conf_get_as_int_d ( g_conf, "tabs.top_altcat1", 0 ) );
682         category_meta_push ( iter -> alt_category2, iter -> alt_category, iter, ovrh, cat_is_visible ( g_conf, iter -> alt_category2 ) ); //pnd_conf_get_as_int_d ( g_conf, "tabs.top_altcat2", 0 ) );
683
684       } // app is visible?
685
686     } // register with categories or filter out
687
688     // next
689     iter = pnd_box_get_next ( iter );
690     itercount++;
691   } // while
692
693   // sort (some) categories
694   category_sort();
695
696   // set up filesystem browser tabs
697   if ( pnd_conf_get_as_int_d ( g_conf, "filesystem.do_browser", 0 ) ) {
698     char *searchpath = pnd_conf_get_as_char ( g_conf, "filesystem.tab_searchpaths" );
699
700     SEARCHPATH_PRE
701     {
702       char *c, *tabname;
703       c = strrchr ( buffer, '/' );
704       if ( c && (*(c+1)!='\0') ) {
705         tabname = c;
706       } else {
707         tabname = buffer;
708       }
709
710       // check if dir is empty; if so, skip it.
711       if ( ! is_dir_empty ( buffer ) ) {
712         category_push ( tabname /* tab name */, NULL /* app */, 0 /* override */, buffer /* fspath */ );
713       }
714
715     }
716     SEARCHPATH_POST
717
718   } // set up fs browser tabs
719
720   // dump categories
721   //category_dump();
722
723   // let deferred icon cache go now
724   ui_post_scan();
725
726   return;
727 }
728
729 static char _vbuf [ 512 ];
730 unsigned char cat_is_visible ( pnd_conf_handle h, char *catname ) {
731   snprintf ( _vbuf, 500, "tabshow.%s", catname );
732   return ( pnd_conf_get_as_int_d ( g_conf, _vbuf, 1 ) ); // default to 'show' when unknown
733 }
734
735 unsigned char app_is_visible ( pnd_conf_handle h, char *uniqueid ) {
736   snprintf ( _vbuf, 500, "appshow.%s", uniqueid );
737   return ( pnd_conf_get_as_int_d ( g_conf, _vbuf, 1 ) ); // default to 'show' when unknown
738 }
739
740 void sigquit_handler ( int n ) {
741   pnd_log ( pndn_rem, "SIGQUIT received; graceful exit.\n" );
742   emit_and_quit ( MM_QUIT );
743 }
744
745 void setup_notifications ( void ) {
746
747   // figure out notify path
748   char *configpath;
749   char *notifypath = NULL;
750
751   configpath = pnd_conf_query_searchpath();
752
753   pnd_conf_handle apph;
754
755   apph = pnd_conf_fetch_by_id ( pnd_conf_apps, configpath );
756
757   if ( apph ) {
758
759     notifypath = pnd_conf_get_as_char ( apph, PND_APPS_NOTIFY_KEY );
760
761     if ( ! notifypath ) {
762       notifypath = PND_APPS_NOTIFYPATH;
763     }
764
765   }
766
767   // given notify path.. ripped from pndnotifyd :(
768   char *searchpath = notifypath;
769
770   // if this is first time through, we can just set it up; for subsequent times
771   // through, we need to close existing fd and re-open it, since we're too lame
772   // to store the list of watches and 'rm' them
773 #if 1
774   if ( nh ) {
775     pnd_notify_shutdown ( nh );
776     nh = 0;
777   }
778 #endif
779
780   // set up a new set of notifies
781   if ( ! nh ) {
782     nh = pnd_notify_init();
783   }
784
785   if ( ! nh ) {
786     pnd_log ( pndn_rem, "INOTIFY failed to init.\n" );
787     exit ( -1 );
788   }
789
790 #if 0
791   pnd_log ( pndn_rem, "INOTIFY is up.\n" );
792 #endif
793
794   SEARCHPATH_PRE
795   {
796
797     pnd_log ( pndn_rem, "Watching path '%s' and its descendents.\n", buffer );
798     pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
799
800   }
801   SEARCHPATH_POST
802
803   return;
804 }
805
806 // for Pleng
807 // Goal: normally menu will quit when an app is invoked, but there are cases when some folks
808 // may configure their system and want mmenu to live instead (save on restart time, don't care
809 // about RAM, using a multitasking tray/window manager setup...), so instead of 'exit and emit'
810 // here we just run the app directly and cross fingers!
811 void emit_and_run ( char *buffer ) {
812
813   // run the bloody thing
814   int f;
815
816   if ( ( f = fork() ) < 0 ) {
817     // error forking
818   } else if ( f > 0 ) {
819     // parent
820   } else {
821     // child, do it
822     execl ( "/bin/sh", "/bin/sh", "-c", buffer + strlen(MM_RUN) + 1, (char*) NULL );
823   } 
824
825   return;
826 }
827
828 // this code was swiped from pnd_utility pnd_exec_no_wait_1 as it became a little too minimenu-specific to remain there
829 void exec_raw_binary ( char *fullpath ) {
830   int i;
831
832   if ( ( i = fork() ) < 0 ) {
833     printf ( "ERROR: Couldn't fork()\n" );
834     return;
835   }
836
837   if ( i ) {
838     return; // parent process, don't care
839   }
840
841   // child process, do something
842   execl ( "/bin/sh", "/bin/sh", "-c", fullpath, (char*) NULL );
843   //execl ( fullpath, fullpath, (char*) NULL );
844
845   // 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!
846   exit ( -1 );
847
848   // getting here is an error
849   //printf ( "Error attempting to run %s\n", fullpath );
850
851   return;
852 }