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