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