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