More significant mmenu changes, couple minor fixes/additions to libpnd proper
[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
39 #include "pnd_logger.h"
40 #include "pnd_pxml.h"
41 #include "pnd_utility.h"
42 #include "pnd_conf.h"
43 #include "pnd_container.h"
44 #include "pnd_discovery.h"
45 #include "pnd_locate.h"
46 #include "pnd_device.h"
47 #include "pnd_pndfiles.h"
48
49 #include "mmenu.h"
50 #include "mmwrapcmd.h"
51 #include "mmapps.h"
52 #include "mmcache.h"
53 #include "mmcat.h"
54 #include "mmui.h"
55
56 pnd_box_handle g_active_apps = NULL;
57 unsigned int g_active_appcount = 0;
58 char g_username [ 128 ]; // since we have to wait for login (!!), store username here
59 pnd_conf_handle g_conf = 0;
60 pnd_conf_handle g_desktopconf = 0;
61
62 char *pnd_run_script = NULL;
63 char *g_skinpath = NULL;
64 unsigned char g_x11_present = 1; // >0 if X is present
65 unsigned char g_catmap = 0; // if 1, we're doing category mapping
66 unsigned char g_pvwcache = 0; // if 1, we're trying to do preview caching
67
68 int main ( int argc, char *argv[] ) {
69   int logall = -1; // -1 means normal logging rules; >=0 means log all!
70   int i;
71
72   // boilerplate stuff from pndnotifyd and pndevmapperd
73
74   /* iterate across args
75    */
76   for ( i = 1; i < argc; i++ ) {
77
78     if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'l' ) {
79
80       if ( isdigit ( argv [ i ][ 2 ] ) ) {
81         unsigned char x = atoi ( argv [ i ] + 2 );
82         if ( x >= 0 &&
83              x < pndn_none )
84         {
85           logall = x;
86         }
87       } else {
88         logall = 0;
89       }
90
91     } else {
92       //printf ( "Unknown: %s\n", argv [ i ] );
93       printf ( "%s [-l##]\n", argv [ 0 ] );
94       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" );
95       printf ( "-f\tFull path of frontend to run\n" );
96       exit ( 0 );
97     }
98
99   } // for
100
101   /* enable logging?
102    */
103   pnd_log_set_pretext ( "mmenu" );
104   pnd_log_set_flush ( 1 );
105
106   if ( logall == -1 ) {
107     // standard logging; non-daemon versus daemon
108
109 #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
110     struct stat statbuf;
111     if ( stat ( PND_DEVICE_BATTERY_GAUGE_PERC, &statbuf ) == 0 ) {
112       // on pandora
113       pnd_log_set_filter ( pndn_error );
114     } else {
115       pnd_log_set_filter ( pndn_debug );
116     }
117 #endif
118
119     pnd_log_to_stdout();
120
121   } else {
122     FILE *f;
123
124     f = fopen ( "/tmp/mmenu.log", "w" );
125
126     if ( f ) {
127       pnd_log_set_filter ( logall );
128       pnd_log_to_stream ( f );
129       pnd_log ( pndn_rem, "logall mode - logging to /tmp/mmenu.log\n" );
130     }
131
132     if ( logall == pndn_debug ) {
133       pnd_log_set_buried_logging ( 1 ); // log the shit out of it
134       pnd_log ( pndn_rem, "logall mode 0 - turned on buried logging\n" );
135     }
136
137   } // logall
138
139   pnd_log ( pndn_rem, "%s built %s %s", argv [ 0 ], __DATE__, __TIME__ );
140
141   pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );
142
143   // wait for a user to be logged in - we should probably get hupped when a user logs in, so we can handle
144   // log-out and back in again, with SDs popping in and out between..
145   pnd_log ( pndn_rem, "Checking to see if a user is logged in\n" );
146   while ( 1 ) {
147     if ( pnd_check_login ( g_username, 127 ) ) {
148       break;
149     }
150     pnd_log ( pndn_debug, "  No one logged in yet .. spinning.\n" );
151     sleep ( 2 );
152   } // spin
153   pnd_log ( pndn_rem, "Looks like user '%s' is in, continue.\n", g_username );
154
155   /* conf file
156    */
157   g_conf = pnd_conf_fetch_by_name ( MMENU_CONF, MMENU_CONF_SEARCHPATH );
158
159   if ( ! g_conf ) {
160     pnd_log ( pndn_error, "ERROR: Couldn't fetch conf file '%s'!\n", MMENU_CONF );
161     emit_and_quit ( MM_QUIT );
162   }
163
164   g_desktopconf = pnd_conf_fetch_by_id ( pnd_conf_desktop, PND_CONF_SEARCHPATH );
165
166   if ( ! g_desktopconf ) {
167     pnd_log ( pndn_error, "ERROR: Couldn't fetch desktop conf file\n" );
168     emit_and_quit ( MM_QUIT );
169   }
170
171   // redo log filter
172   pnd_log_set_filter ( pnd_conf_get_as_int_d ( g_conf, "minimenu.loglevel", pndn_error ) );
173
174   /* setup
175    */
176
177   // X11?
178   if ( pnd_conf_get_as_char ( g_conf, "minimenu.x11_present_sh" ) ) {
179     FILE *fx = popen ( pnd_conf_get_as_char ( g_conf, "minimenu.x11_present_sh" ), "r" );
180     char buffer [ 100 ];
181     if ( fx ) {
182       if ( fgets ( buffer, 100, fx ) ) {
183         if ( atoi ( buffer ) ) {
184           g_x11_present = 1;
185           pnd_log ( pndn_rem, "X11 seems to be present [pid %u]\n", atoi(buffer) );
186         } else {
187           g_x11_present = 0;
188           pnd_log ( pndn_rem, "X11 seems NOT to be present\n" );
189         }
190       } else {
191           g_x11_present = 0;
192           pnd_log ( pndn_rem, "X11 seems NOT to be present\n" );
193       }
194       pclose ( fx );
195     }
196   } // x11?
197
198   // pnd_run.sh
199   pnd_run_script = pnd_locate_filename ( pnd_conf_get_as_char ( g_conf, "minimenu.pndrun" ), "pnd_run.sh" );
200
201   if ( ! pnd_run_script ) {
202     pnd_log ( pndn_error, "ERROR: Couldn't locate pnd_run.sh!\n" );
203     emit_and_quit ( MM_QUIT );
204   }
205
206   pnd_run_script = strdup ( pnd_run_script ); // so we don't lose it next pnd_locate
207
208   pnd_log ( pndn_rem, "Found pnd_run.sh at '%s'\n", pnd_run_script );
209
210   // figure out skin path
211   if ( ! pnd_conf_get_as_char ( g_conf, MMENU_ARTPATH ) ||
212        ! pnd_conf_get_as_char ( g_conf, "minimenu.font" )
213      )
214   {
215     pnd_log ( pndn_error, "ERROR: Couldn't set up skin!\n" );
216     emit_and_quit ( MM_QUIT );
217   }
218
219   g_skinpath = pnd_locate_filename ( pnd_conf_get_as_char ( g_conf, MMENU_ARTPATH ),
220                                      pnd_conf_get_as_char ( g_conf, "minimenu.font" ) );
221
222   if ( ! g_skinpath ) {
223     pnd_log ( pndn_error, "ERROR: Couldn't locate skin font!\n" );
224     emit_and_quit ( MM_QUIT );
225   }
226
227   g_skinpath = strdup ( g_skinpath ); // so we don't lose it next pnd_locate
228
229   * strstr ( g_skinpath, pnd_conf_get_as_char ( g_conf, "minimenu.font" ) ) = '\0';
230
231   pnd_log ( pndn_debug, "Looks like skin is at '%s'\n", g_skinpath );
232
233   // attempt to set up UI
234   if ( ! ui_setup() ) {
235     pnd_log ( pndn_error, "ERROR: Couldn't set up the UI!\n" );
236     emit_and_quit ( MM_QUIT );
237   }
238
239   // show load screen
240   ui_loadscreen();
241
242   // store flag if we're doing preview caching or not
243   if ( pnd_conf_get_as_int_d ( g_conf, "previewpic.do_cache", 0 ) ) {
244     g_pvwcache = 1;
245   }
246
247   // set up static image cache
248   if ( ! ui_imagecache ( g_skinpath ) ) {
249     pnd_log ( pndn_error, "ERROR: Couldn't set up static UI image cache!\n" );
250     emit_and_quit ( MM_QUIT );
251   }
252
253   // create all cat
254   if ( pnd_conf_get_as_int_d ( g_conf, "categories.do_all_cat", 1 ) ) {
255     category_push ( g_x11_present ? CATEGORY_ALL "    (X11)" : CATEGORY_ALL "   (No X11)", NULL, 0 );
256   }
257
258   // set up category mappings
259   if ( pnd_conf_get_as_int_d ( g_conf, "categories.map_on", 0 ) ) {
260     g_catmap = category_map_setup();
261   }
262
263   /* inhale applications, icons, categories, etc
264    */
265   applications_scan();
266
267   /* actual work now
268    */
269
270   while ( 1 ) { // forever!
271
272     // show the menu, or changes thereof
273     ui_render();
274
275     // wait for input or time-based events (like animations)
276     // deal with inputs
277     ui_process_input ( 1 /* block */ );
278
279     // sleep? block?
280     usleep ( 5000 );
281
282   } // while
283
284   return ( 0 );
285 }
286
287 void emit_and_quit ( char *s ) {
288   printf ( "%s\n", s );
289   exit ( 0 );
290 }
291
292 void applications_free ( void ) {
293
294   // free up all our category apprefs, but keep the preview and icon cache's..
295   category_freeall();
296
297   // free up old disco_t
298   if ( g_active_apps ) {
299     pnd_disco_t *p = pnd_box_get_head ( g_active_apps );
300     pnd_disco_t *n;
301     while ( p ) {
302       n = pnd_box_get_next ( p );
303       pnd_disco_destroy ( p );
304       p = n;
305     }
306     pnd_box_delete ( g_active_apps );
307   }
308
309   return;
310 }
311
312 void applications_scan ( void ) {
313
314   // show disco screen
315   ui_discoverscreen ( 1 /* clear screen */ );
316
317   // determine current app list, cache icons
318   // - ignore overrides for now
319
320   g_active_apps = 0;
321   pnd_box_handle merge_apps = 0;
322
323   // desktop apps?
324   if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.desktop_apps", 1 ) ) {
325     pnd_log ( pndn_debug, "Looking for pnd applications here: %s\n",
326               pnd_conf_get_as_char ( g_desktopconf, "desktop.searchpath" ) );
327     g_active_apps = pnd_disco_search ( pnd_conf_get_as_char ( g_desktopconf, "desktop.searchpath" ), NULL );
328   }
329
330   // menu apps?
331   if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.menu_apps", 1 ) ) {
332     pnd_log ( pndn_debug, "Looking for pnd applications here: %s\n",
333               pnd_conf_get_as_char ( g_desktopconf, "menu.searchpath" ) );
334     merge_apps = pnd_disco_search ( pnd_conf_get_as_char ( g_desktopconf, "menu.searchpath" ), NULL );
335   }
336
337   // merge lists
338   if ( merge_apps ) {
339     if ( g_active_apps ) {
340       // got menu apps, and got desktop apps, merge
341       pnd_box_append ( g_active_apps, merge_apps );
342     } else {
343       // got menu apps, had no desktop apps, so just assign
344       g_active_apps = merge_apps;
345     }
346   }
347
348   // aux apps?
349   char *aux_apps = NULL;
350   merge_apps = 0;
351   aux_apps = pnd_conf_get_as_char ( g_conf, "minimenu.aux_searchpath" );
352   if ( aux_apps && aux_apps [ 0 ] ) {
353     pnd_log ( pndn_debug, "Looking for pnd applications here: %s\n", aux_apps );
354     merge_apps = pnd_disco_search ( aux_apps, NULL );
355   }
356
357   // merge aux apps
358   if ( merge_apps ) {
359     if ( g_active_apps ) {
360       pnd_box_append ( g_active_apps, merge_apps );
361     } else {
362       g_active_apps = merge_apps;
363     }
364   }
365
366   // do it
367   g_active_appcount = pnd_box_get_size ( g_active_apps );
368
369   unsigned char maxwidth, maxheight;
370   maxwidth = pnd_conf_get_as_int_d ( g_conf, MMENU_DISP_ICON_MAX_WIDTH, 50 );
371   maxheight = pnd_conf_get_as_int_d ( g_conf, MMENU_DISP_ICON_MAX_HEIGHT, 50 );
372
373   // show cache screen
374   ui_cachescreen ( 1 /* clear screen */, NULL );
375
376   pnd_log ( pndn_debug, "Found pnd applications, and caching icons:\n" );
377   pnd_disco_t *iter = pnd_box_get_head ( g_active_apps );
378   unsigned int itercount = 0;
379   while ( iter ) {
380     //pnd_log ( pndn_debug, "  App: '%s'\n", IFNULL(iter->title_en,"No Name") );
381
382     // update cachescreen
383     // ... every 5 filenames, just to avoid slowing it too much
384     if ( itercount % 5 == 0 ) {
385       ui_cachescreen ( 0 /* clear screen */, IFNULL(iter->title_en,"No Name") );
386     }
387
388     // if an ovr was flagged by libpnd, lets go inhale it just so we've got the
389     // notes handy, since notes are not handled by libpnd proper
390     pnd_conf_handle ovrh = 0;
391     if ( iter -> object_flags & PND_DISCO_FLAG_OVR ) {
392       char ovrfile [ PATH_MAX ];
393       char *fixpxml;
394       sprintf ( ovrfile, "%s/%s", iter -> object_path, iter -> object_filename );
395       fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
396       strcpy ( fixpxml, PXML_SAMEPATH_OVERRIDE_FILEEXT );
397
398       ovrh = pnd_conf_fetch_by_path ( ovrfile );
399
400 #if 0
401       if ( ovrh ) {
402         pnd_log ( pndn_debug, "Found ovr file for %s # %u\n", iter -> object_filename, iter -> subapp_number );
403       }
404 #endif
405
406     } // ovr
407
408     // cache the icon, unless deferred
409     if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_icons_later", 0 ) == 0 ) {
410       if ( iter -> pnd_icon_pos &&
411            ! cache_icon ( iter, maxwidth, maxheight ) )
412       {
413         pnd_log ( pndn_warning, "  Couldn't load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
414       }
415     }
416
417     // cache the preview --> SHOULD DEFER
418     if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_previews_now", 0 ) > 0 ) {
419       // load the preview pics now!
420       if ( iter -> preview_pic1 &&
421            ! 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 ) ) )
422       {
423         pnd_log ( pndn_warning, "  Couldn't load preview pic: '%s' -> '%s'\n", IFNULL(iter->title_en,"No Name"), iter -> preview_pic1 );
424       }
425     } // preview now?
426
427     // push the categories .. or suppress application
428     //
429     if ( ( pnd_pxml_get_x11 ( iter -> option_no_x11 ) != pnd_pxml_x11_required ) ||
430          ( pnd_pxml_get_x11 ( iter -> option_no_x11 ) == pnd_pxml_x11_required && g_x11_present == 1 )
431        )
432     {
433
434       // push to All category
435       // we do this first, so first category is always All
436       if ( pnd_conf_get_as_int_d ( g_conf, "categories.do_all_cat", 1 ) ) {
437         if ( ! category_push ( g_x11_present ? CATEGORY_ALL "    (X11)" : CATEGORY_ALL "   (No X11)", iter, ovrh ) ) {
438           pnd_log ( pndn_warning, "  Couldn't categorize to All: '%s'\n", IFNULL(iter -> title_en, "No Name") );
439         }
440       } // all?
441
442       // main categories
443       if ( iter -> main_category && pnd_conf_get_as_int_d ( g_conf, "tabs.top_maincat", 1 ) ) {
444         if ( ! category_meta_push ( iter -> main_category, iter, ovrh ) ) {
445           pnd_log ( pndn_warning, "  Couldn't categorize to %s: '%s'\n", iter -> main_category, IFNULL(iter -> title_en, "No Name") );
446         }
447       }
448
449       if ( iter -> main_category1 && pnd_conf_get_as_int_d ( g_conf, "tabs.top_maincat1", 0 ) ) {
450         if ( ! category_meta_push ( iter -> main_category1, iter, ovrh ) ) {
451           pnd_log ( pndn_warning, "  Couldn't categorize to %s: '%s'\n", iter -> main_category1, IFNULL(iter -> title_en, "No Name") );
452         }
453       }
454
455       if ( iter -> main_category2 && pnd_conf_get_as_int_d ( g_conf, "tabs.top_maincat2", 0 ) ) {
456         if ( ! category_meta_push ( iter -> main_category2, iter, ovrh ) ) {
457           pnd_log ( pndn_warning, "  Couldn't categorize to %s: '%s'\n", iter -> main_category2, IFNULL(iter -> title_en, "No Name") );
458         }
459       }
460
461       // alt categories
462       if ( iter -> alt_category && pnd_conf_get_as_int_d ( g_conf, "tabs.top_altcat", 0 ) ) {
463         if ( ! category_meta_push ( iter -> alt_category, iter, ovrh ) ) {
464           pnd_log ( pndn_warning, "  Couldn't categorize to %s: '%s'\n", iter -> alt_category, IFNULL(iter -> title_en, "No Name") );
465         }
466       }
467
468       if ( iter -> alt_category1 && pnd_conf_get_as_int_d ( g_conf, "tabs.top_altcat1", 0 ) ) {
469         if ( ! category_meta_push ( iter -> alt_category1, iter, ovrh ) ) {
470           pnd_log ( pndn_warning, "  Couldn't categorize to %s: '%s'\n", iter -> alt_category1, IFNULL(iter -> title_en, "No Name") );
471         }
472       }
473
474       if ( iter -> alt_category2 && pnd_conf_get_as_int_d ( g_conf, "tabs.top_altcat2", 0 ) ) {
475         if ( ! category_meta_push ( iter -> alt_category2, iter, ovrh ) ) {
476           pnd_log ( pndn_warning, "  Couldn't categorize to %s: '%s'\n", iter -> alt_category2, IFNULL(iter -> title_en, "No Name") );
477         }
478       }
479
480     } // register with categories or filter out
481
482     // next
483     iter = pnd_box_get_next ( iter );
484     itercount++;
485   } // while
486
487   // dump categories
488   //category_dump();
489
490   // let deferred icon cache go now
491   ui_post_scan();
492
493   return;
494 }