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