Added category mapping support to mmenu .. lets user define what categories to have...
[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   category_push ( g_x11_present ? CATEGORY_ALL "    (X11)" : CATEGORY_ALL "   (No X11)", NULL );
246
247   // set up category mappings
248   if ( pnd_conf_get_as_int_d ( g_conf, "categories.map_on", 0 ) ) {
249     g_catmap = category_map_setup();
250   }
251
252   /* inhale applications, icons, categories, etc
253    */
254   applications_scan();
255
256   /* actual work now
257    */
258
259   while ( 1 ) { // forever!
260
261     // show the menu, or changes thereof
262     ui_render ( CHANGED_NOTHING );
263
264     // wait for input or time-based events (like animations)
265     // deal with inputs
266     ui_process_input ( 1 /* block */ );
267
268     // sleep? block?
269     usleep ( 5000 );
270
271   } // while
272
273   return ( 0 );
274 }
275
276 void emit_and_quit ( char *s ) {
277   printf ( "%s\n", s );
278   exit ( 0 );
279 }
280
281 void applications_free ( void ) {
282
283   // free up all our category apprefs, but keep the preview and icon cache's..
284   category_freeall();
285
286   // free up old disco_t
287   if ( g_active_apps ) {
288     pnd_disco_t *p = pnd_box_get_head ( g_active_apps );
289     pnd_disco_t *n;
290     while ( p ) {
291       n = pnd_box_get_next ( p );
292       pnd_disco_destroy ( p );
293       p = n;
294     }
295     pnd_box_delete ( g_active_apps );
296   }
297
298   return;
299 }
300
301 void applications_scan ( void ) {
302
303   // show disco screen
304   ui_discoverscreen ( 1 /* clear screen */ );
305
306   // determine current app list, cache icons
307   // - ignore overrides for now
308
309   g_active_apps = 0;
310   pnd_box_handle merge_apps = 0;
311
312   // desktop apps?
313   if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.desktop_apps", 1 ) ) {
314     pnd_log ( pndn_debug, "Looking for pnd applications here: %s\n",
315               pnd_conf_get_as_char ( g_desktopconf, "desktop.searchpath" ) );
316     g_active_apps = pnd_disco_search ( pnd_conf_get_as_char ( g_desktopconf, "desktop.searchpath" ), NULL );
317   }
318
319   // menu apps?
320   if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.menu_apps", 1 ) ) {
321     pnd_log ( pndn_debug, "Looking for pnd applications here: %s\n",
322               pnd_conf_get_as_char ( g_desktopconf, "menu.searchpath" ) );
323     merge_apps = pnd_disco_search ( pnd_conf_get_as_char ( g_desktopconf, "menu.searchpath" ), NULL );
324   }
325
326   // merge lists
327   if ( merge_apps ) {
328     if ( g_active_apps ) {
329       // got menu apps, and got desktop apps, merge
330       pnd_box_append ( g_active_apps, merge_apps );
331     } else {
332       // got menu apps, had no desktop apps, so just assign
333       g_active_apps = merge_apps;
334     }
335   }
336
337   // aux apps?
338   char *aux_apps = NULL;
339   merge_apps = 0;
340   aux_apps = pnd_conf_get_as_char ( g_conf, "minimenu.aux_searchpath" );
341   if ( aux_apps && aux_apps [ 0 ] ) {
342     pnd_log ( pndn_debug, "Looking for pnd applications here: %s\n", aux_apps );
343     merge_apps = pnd_disco_search ( aux_apps, NULL );
344   }
345
346   // merge aux apps
347   if ( merge_apps ) {
348     if ( g_active_apps ) {
349       pnd_box_append ( g_active_apps, merge_apps );
350     } else {
351       g_active_apps = merge_apps;
352     }
353   }
354
355   // do it
356   g_active_appcount = pnd_box_get_size ( g_active_apps );
357
358   unsigned char maxwidth, maxheight;
359   maxwidth = pnd_conf_get_as_int_d ( g_conf, MMENU_DISP_ICON_MAX_WIDTH, 50 );
360   maxheight = pnd_conf_get_as_int_d ( g_conf, MMENU_DISP_ICON_MAX_HEIGHT, 50 );
361
362   // show cache screen
363   ui_cachescreen ( 1 /* clear screen */, NULL );
364
365   pnd_log ( pndn_debug, "Found pnd applications, and caching icons:\n" );
366   pnd_disco_t *iter = pnd_box_get_head ( g_active_apps );
367   unsigned int itercount = 0;
368   while ( iter ) {
369     //pnd_log ( pndn_debug, "  App: '%s'\n", IFNULL(iter->title_en,"No Name") );
370
371     // update cachescreen
372     // ... every 5 filenames, just to avoid slowing it too much
373     if ( itercount % 5 == 0 ) {
374       ui_cachescreen ( 0 /* clear screen */, IFNULL(iter->title_en,"No Name") );
375     }
376
377     // cache the icon, unless deferred
378     if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_icons_later", 0 ) == 0 ) {
379       if ( iter -> pnd_icon_pos &&
380            ! cache_icon ( iter, maxwidth, maxheight ) )
381       {
382         pnd_log ( pndn_warning, "  Couldn't load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
383       }
384     }
385
386     // cache the preview --> SHOULD DEFER
387     if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_previews_now", 0 ) > 0 ) {
388       // load the preview pics now!
389       if ( iter -> preview_pic1 &&
390            ! 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 ) ) )
391       {
392         pnd_log ( pndn_warning, "  Couldn't load preview pic: '%s' -> '%s'\n", IFNULL(iter->title_en,"No Name"), iter -> preview_pic1 );
393       }
394     } // preview now?
395
396     // push the categories .. or suppress application
397     //
398     if ( ( pnd_pxml_get_x11 ( iter -> option_no_x11 ) != pnd_pxml_x11_required ) ||
399          ( pnd_pxml_get_x11 ( iter -> option_no_x11 ) == pnd_pxml_x11_required && g_x11_present == 1 )
400        )
401     {
402
403       // push to All category
404       // we do this first, so first category is always All
405       if ( ! category_push ( g_x11_present ? CATEGORY_ALL "    (X11)" : CATEGORY_ALL "   (No X11)", iter ) ) {
406         pnd_log ( pndn_warning, "  Couldn't categorize to All: '%s'\n", IFNULL(iter -> title_en, "No Name") );
407       }
408
409       // main categories
410       if ( iter -> main_category && pnd_conf_get_as_int_d ( g_conf, "tabs.top_maincat", 1 ) ) {
411         if ( ! category_meta_push ( iter -> main_category, iter ) ) {
412           pnd_log ( pndn_warning, "  Couldn't categorize to %s: '%s'\n", iter -> main_category, IFNULL(iter -> title_en, "No Name") );
413         }
414       }
415
416       if ( iter -> main_category1 && pnd_conf_get_as_int_d ( g_conf, "tabs.top_maincat1", 0 ) ) {
417         if ( ! category_meta_push ( iter -> main_category1, iter ) ) {
418           pnd_log ( pndn_warning, "  Couldn't categorize to %s: '%s'\n", iter -> main_category1, IFNULL(iter -> title_en, "No Name") );
419         }
420       }
421
422       if ( iter -> main_category2 && pnd_conf_get_as_int_d ( g_conf, "tabs.top_maincat2", 0 ) ) {
423         if ( ! category_meta_push ( iter -> main_category2, iter ) ) {
424           pnd_log ( pndn_warning, "  Couldn't categorize to %s: '%s'\n", iter -> main_category2, IFNULL(iter -> title_en, "No Name") );
425         }
426       }
427
428       // alt categories
429       if ( iter -> alt_category && pnd_conf_get_as_int_d ( g_conf, "tabs.top_altcat", 0 ) ) {
430         if ( ! category_meta_push ( iter -> alt_category, iter ) ) {
431           pnd_log ( pndn_warning, "  Couldn't categorize to %s: '%s'\n", iter -> alt_category, IFNULL(iter -> title_en, "No Name") );
432         }
433       }
434
435       if ( iter -> alt_category1 && pnd_conf_get_as_int_d ( g_conf, "tabs.top_altcat1", 0 ) ) {
436         if ( ! category_meta_push ( iter -> alt_category1, iter ) ) {
437           pnd_log ( pndn_warning, "  Couldn't categorize to %s: '%s'\n", iter -> alt_category1, IFNULL(iter -> title_en, "No Name") );
438         }
439       }
440
441       if ( iter -> alt_category2 && pnd_conf_get_as_int_d ( g_conf, "tabs.top_altcat2", 0 ) ) {
442         if ( ! category_meta_push ( iter -> alt_category2, iter ) ) {
443           pnd_log ( pndn_warning, "  Couldn't categorize to %s: '%s'\n", iter -> alt_category2, IFNULL(iter -> title_en, "No Name") );
444         }
445       }
446
447     } // register with categories or filter out
448
449     // next
450     iter = pnd_box_get_next ( iter );
451     itercount++;
452   } // while
453
454   // dump categories
455   //category_dump();
456
457   // let deferred icon cache go now
458   ui_post_scan();
459
460   return;
461 }