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