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