3 #include <limits.h> /* for PATH_MAX */
4 #define __USE_GNU /* for strndup */
5 #include <string.h> /* for strdup */
11 #include "pnd_container.h"
13 #include "pnd_discovery.h"
19 #include "mmwrapcmd.h"
21 static unsigned char conf_render_text ( TTF_Font *f, char *buffer, SDL_Rect *dest, unsigned int x, unsigned int y, unsigned char selected );
22 static unsigned char conf_render_line ( SDL_Rect *dest, unsigned int y );
23 static char *conf_format_int ( int v, change_type_e c );
25 confitem_t page_general[] = {
26 { "Default tab to show", "On startup, Minimenu will try to switch to this tab", NULL /* default */, "categories.default_cat", ct_visible_tab_list },
27 #if 0 // and also in mmenu.c -- something crashes during image caching
28 { "Set CPU speed within Minimenu", "Whether the next setting is applied or not", "0", "minimenu.use_mm_speed", ct_boolean },
29 { "CPU speed within Minimenu", "Set low; speed to run Minimenu at", "400", "minimenu.mm_speed", ct_cpu_speed },
31 { "Set CPU speed when leaving", "Whether the next setting is applied or not", "0", "minimenu.use_run_speed", ct_boolean },
32 { "CPU speed when leaving", "Before running app, set this speed; app may override.", "500", "minimenu.run_speed", ct_cpu_speed },
33 { "Show 'All' tab", "Whethor an All tab is shown or not", "1", "categories.do_all_cat", ct_boolean },
34 { "Show directory browser tabs", "Show a tab for each SD card?", "0", "filesystem.do_browser", ct_boolean },
35 { "Detail panel on start?", "Show or hide the detail panel when menu starts", "1", "display.show_detail_pane", ct_boolean },
36 { "Start with app selected", "Whethor selection is placed by default or not", "0", "minimenu.start_selected", ct_boolean },
37 { "Sub-categories as folders?", "If no, uses tabs instead of folders within tabs.", "1", "tabs.subcat_as_folders", ct_boolean },
38 { "Wrap tab change", "Changing tab left or right, does it wrap around?", "0", "tabs.wraparound", ct_boolean },
39 { "Grid stop vertical", "Changing selection up or down, does it stop or wrap?", "0", "grid.wrap_vert_stop", ct_boolean },
40 { "Live (not exit) on app run?", "Normally menu exits (to save ram) on app run", "0", "minimenu.live_on_run", ct_boolean },
41 { "Force wallpaper with..", "You can force an override over themes background", "/pandora/appdata/mmenu/wallpaper.png", "minimenu.force_wallpaper", ct_filename },
42 { "", "", NULL, NULL, ct_nil },
43 { "^- Back up to main", "Go back to top level of configuration", NULL, NULL, ct_switch_page, NULL },
47 confitem_t page_appshowhide [ CONF_MAX_LISTLENGTH ] = {
48 { "^- Back up to main", "Go back to top level of configuration", NULL, NULL, ct_switch_page, NULL },
49 { CONF_APPLIST_TAG, "Show or hide this application", "1", "appshow", ct_nil },
53 confitem_t page_tabshowhide [ CONF_MAX_LISTLENGTH ] = {
54 { "^- Back up to main", "Go back to top level of configuration", NULL, NULL, ct_switch_page, NULL },
55 { CONF_TABLIST_TAG, "Show or hide or reorder this tab", "1", "tabshow", ct_nil },
59 confitem_t pages[] = {
60 { "General Options", "Miscellaneous handy options", NULL /* default */, NULL, ct_switch_page, page_general },
61 { "Show/Hide Applications", "Each application can be hidden/revealed", NULL /* default */, NULL, ct_switch_page, page_appshowhide },
62 { "Show/Hide/Order Tabs", "Each tab can be hidden/revealed or re-ordered", NULL /* default */, NULL, ct_switch_page, page_tabshowhide },
63 { "", "", NULL, NULL, ct_nil },
64 { "Exit configuration", "Quit and save configuration", NULL /* default */, NULL, ct_exit },
65 { "", "", NULL, NULL, ct_nil },
66 { "Reset to defaults", "Remove any custom options set in this UI", NULL /* default */, NULL, ct_reset },
70 extern pnd_conf_handle g_conf;
71 extern SDL_Surface *sdl_realscreen;
72 extern mm_imgcache_t g_imagecache [ IMG_TRUEMAX ];
73 extern pnd_box_handle g_active_apps;
75 unsigned char conf_run_menu ( confitem_t *toplevel ) {
76 confitem_t *page = toplevel;
78 unsigned int first_visible = 0;
79 unsigned char max_visible = 12;
81 confitem_t *lastpage = NULL;
91 if ( lastpage != page ) {
92 conf_prepare_page ( page );
96 conf_display_page ( page, sel, first_visible, max_visible );
99 while ( SDL_WaitEvent ( &event ) ) {
101 switch ( event.type ) {
106 if ( event.key.keysym.sym == SDLK_UP ) {
113 if ( sel < first_visible ) {
119 } while ( page [ sel ].type == ct_nil );
121 } else if ( event.key.keysym.sym == SDLK_DOWN ) {
125 if ( page [ sel + 1 ].text ) {
129 if ( sel >= first_visible + max_visible ) {
135 } while ( page [ sel ].type == ct_nil );
137 } else if ( event.key.keysym.sym == SDLK_PAGEUP ) {
140 } else if ( event.key.keysym.sym == SDLK_LEFT || event.key.keysym.sym == SDLK_RIGHT ) {
142 unsigned char left = 0;
143 if ( event.key.keysym.sym == SDLK_LEFT ) {
147 switch ( page [ sel ].type ) {
151 int v = pnd_conf_get_as_int ( g_conf, page [ sel ].key );
152 if ( v == PND_CONF_BADNUM ) {
165 sprintf ( buffer, "%u", v );
166 pnd_conf_set_char ( g_conf, page [ sel ].key, buffer );
171 case ct_visible_tab_list:
173 if ( g_categorycount ) {
174 char *v = pnd_conf_get_as_char ( g_conf, page [ sel ].key );
177 for ( n = 0; n < g_categorycount; n++ ) {
178 if ( strcmp ( v, g_categories [ n ] -> catname ) == 0 ) {
182 if ( n < g_categorycount ) {
184 pnd_conf_set_char ( g_conf, page [ sel ].key, g_categories [ n - 1 ] -> catname );
185 } else if ( ! left && n + 1 < g_categorycount ) {
186 pnd_conf_set_char ( g_conf, page [ sel ].key, g_categories [ n + 1 ] -> catname );
189 pnd_conf_set_char ( g_conf, page [ sel ].key, g_categories [ 0 ] -> catname );
192 pnd_conf_set_char ( g_conf, page [ sel ].key, g_categories [ 0 ] -> catname );
194 } // if category count
200 int v = pnd_conf_get_as_int ( g_conf, page [ sel ].key );
201 if ( v == PND_CONF_BADNUM ) {
211 sprintf ( buffer, "%u", v );
213 pnd_conf_set_char ( g_conf, page [ sel ].key, buffer );
228 } else if ( event.key.keysym.sym == SDLK_ESCAPE ) {
229 emit_and_quit ( MM_QUIT );
231 } else if ( event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_END ) { // return, or "B"
233 switch ( page [ sel ].type ) {
236 return ( 1 /* always cause restart for now */ );
241 conf_reset_to_default ( g_conf );
247 page = page [ sel ].newhead;
248 sel = 0; // should use a stack..
253 case ct_visible_tab_list:
263 break; // case sdl_key_up
265 } // switch what SDL event
267 break; // get out of sdl-wait-event loop
272 return ( 1 /* always cause restart for now */ );
275 void conf_display_page ( confitem_t *page, unsigned int selitem, unsigned int first_visible, unsigned int max_visible ) {
276 extern TTF_Font *g_big_font;
277 extern TTF_Font *g_tab_font;
280 SDL_Rect rects [ MAXRECTS ];
281 SDL_Rect *dest = rects;
282 bzero ( dest, sizeof(SDL_Rect)*MAXRECTS );
284 unsigned short int tx, ty;
287 if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
290 dest -> w = sdl_realscreen -> w;
291 dest -> h = sdl_realscreen -> h;
292 SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, NULL /* whole image */, sdl_realscreen, dest /* 0,0 */ );
305 dest += conf_render_text ( g_big_font, "Minimenu Configuration", dest, 10, 10, CONF_UNSELECTED );
306 dest += conf_render_line ( dest, 45 );
312 if ( first_visible > 0 ) {
315 SDL_BlitSurface ( g_imagecache [ IMG_ARROW_UP ].i, NULL /* whole image */, sdl_realscreen, dest );
320 if ( first_visible + max_visible < conf_determine_pagelength ( page ) ) {
323 SDL_BlitSurface ( g_imagecache [ IMG_ARROW_DOWN ].i, NULL /* whole image */, sdl_realscreen, dest );
331 unsigned char counter = first_visible;
332 while ( page [ counter ].text ) {
335 conf_render_text ( g_tab_font, page [ counter ].text, dest, tx, ty, counter == selitem ? CONF_SELECTED : CONF_UNSELECTED );
338 switch ( page [ counter ].type ) {
343 case ct_visible_tab_list:
345 char *v = pnd_conf_get_as_char ( g_conf, page [ counter ].key );
347 conf_render_text ( g_tab_font, v, dest, tx + 400, ty, counter == selitem ? CONF_SELECTED : CONF_UNSELECTED );
349 conf_render_text ( g_tab_font, "Not specified", dest, tx + 400, ty, counter == selitem ? CONF_SELECTED : CONF_UNSELECTED );
355 int v = pnd_conf_get_as_int ( g_conf, page [ counter ].key );
356 conf_render_text ( g_tab_font, conf_format_int ( v, page [ counter ].type ), dest, tx + 400, ty, counter == selitem ? CONF_SELECTED : CONF_UNSELECTED );
361 int v = pnd_conf_get_as_int ( g_conf, page [ counter ].key );
362 conf_render_text ( g_tab_font, conf_format_int ( v, page [ counter ].type ), dest, tx + 400, ty, counter == selitem ? CONF_SELECTED : CONF_UNSELECTED );
366 conf_render_text ( g_tab_font, page [ counter ].def, dest, tx + 400, ty, counter == selitem ? CONF_SELECTED : CONF_UNSELECTED );
375 if ( counter - first_visible >= max_visible - 1 ) {
384 // description and default
385 if ( page [ selitem ].desc ) {
386 dest += conf_render_text ( g_tab_font, page [ selitem ].desc, dest, 380, 400, CONF_UNSELECTED );
388 if ( page [ selitem ].def ) {
389 char buffer [ 100 ] = "Default: ";
391 switch ( page [ selitem ].type ) {
393 sprintf ( buffer + strlen ( buffer ), "%s", conf_format_int ( atoi ( page [ selitem ].def ), ct_boolean ) );
396 sprintf ( buffer + strlen ( buffer ), "%s", conf_format_int ( atoi ( page [ selitem ].def ), ct_cpu_speed ) );
399 sprintf ( buffer + strlen ( buffer ), "%s", page [ selitem ].def );
405 case ct_visible_tab_list:
409 dest += conf_render_text ( g_tab_font, buffer, dest, 380, 420, CONF_UNSELECTED );
411 dest += conf_render_text ( g_tab_font, "No default value", dest, 380, 420, CONF_UNSELECTED );
414 // cursor's conf item count number - not for top level, just the sublevels
415 if ( page != pages ) {
417 sprintf ( buffer, "Config item %d of %d", selitem + 1, conf_determine_pagelength ( page ) );
418 /*dest += */conf_render_text ( g_tab_font, buffer, dest, 380, 440, CONF_UNSELECTED );
422 dest += conf_render_line ( dest, 380 );
423 dest += conf_render_text ( g_tab_font, "D-pad Up/down; Y return to index", dest, 10, 400, CONF_UNSELECTED );
424 dest += conf_render_text ( g_tab_font, "Left and right to alter selected item", dest, 10, 420, CONF_UNSELECTED );
425 dest += conf_render_text ( g_tab_font, "B or Enter to activate an option", dest, 10, 440, CONF_UNSELECTED );
427 // update all the rects and send it all to sdl
428 // - at this point, we could probably just do 1 rect, of the
429 // whole screen, and be faster :/
430 SDL_UpdateRects ( sdl_realscreen, dest - rects, rects );
435 unsigned char conf_render_text ( TTF_Font *f, char *buffer, SDL_Rect *dest, unsigned int x, unsigned int y, unsigned char selected ) {
436 unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
437 unsigned int font_rgba_g = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_g", 200 );
438 unsigned int font_rgba_b = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_b", 200 );
439 unsigned int font_rgba_a = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_a", 100 );
441 SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
442 SDL_Color selfontcolor = { 0/*font_rgba_r*/, font_rgba_g, font_rgba_b, font_rgba_a };
444 SDL_Surface *rtext = TTF_RenderText_Blended ( f, buffer, selected ? selfontcolor : tmpfontcolor );
447 SDL_BlitSurface ( rtext, NULL /* all */, sdl_realscreen, dest );
448 SDL_FreeSurface ( rtext );
453 unsigned char conf_render_line ( SDL_Rect *dest, unsigned int y ) {
458 SDL_Surface *i = g_imagecache [ IMG_TAB_LINE ].i;
460 while ( dest -> x + i -> w < 800 ) {
461 SDL_BlitSurface ( i, NULL, sdl_realscreen, dest );
466 dest -> w = 480 - 10;
472 char *conf_format_int ( int v, change_type_e c ) {
473 static char buffer [ 50 ];
480 if ( v == PND_CONF_BADNUM ) {
481 strcpy ( buffer, "Leave Alone" );
483 sprintf ( buffer, "%u", v );
490 if ( v == PND_CONF_BADNUM ) {
491 strcpy ( buffer, "FUBAR" );
492 } else if ( v == 0 ) {
493 strcpy ( buffer, "no" );
494 } else if ( v == 1 ) {
495 strcpy ( buffer, "yes" );
497 strcpy ( buffer, "FUBAR 2" );
508 case ct_visible_tab_list:
517 unsigned char conf_prepare_page ( confitem_t *page ) {
520 confitem_t *p = page;
521 confitem_t *template = NULL;
522 while ( p -> text != NULL ) {
524 if ( strcmp ( p -> text, CONF_APPLIST_TAG ) == 0 ) {
525 // rewrite this and subsequent items to be the listing
529 template -> text = ""; // so it won't get repopulated again later
532 pnd_disco_t *iter = pnd_box_get_head ( g_active_apps );
534 while ( p - page < CONF_MAX_LISTLENGTH && iter ) {
536 p -> text = strndup ( iter -> title_en ? iter -> title_en : "Unnamed", 40 );
537 p -> desc = strdup ( iter -> unique_id );
540 sprintf ( buffer, "%s.%s", template -> key, iter -> unique_id );
541 p -> key = strdup ( buffer );
542 p -> type = ct_boolean;
545 // create to positive if not existant
546 if ( ! pnd_conf_get_as_char ( g_conf, buffer ) ) {
547 pnd_conf_set_char ( g_conf, buffer, "1" );
550 iter = pnd_box_get_next ( iter );
552 } // while not run off end of buffer
556 } else if ( strcmp ( p -> text, CONF_TABLIST_TAG ) == 0 ) {
557 // rewrite this and subsequent items to be the listing
561 template -> text = ""; // so it won't get repopulated again later
563 // switch categories being published
564 category_publish ( CFALL, NULL );
568 char catname [ 512 ];
569 char *actual_catname;
571 for ( i = 0; i < g_categorycount; i++ ) {
573 // if this is an invisi-guy, it has parent-cat prepended; we want the real cat name.
574 strncpy ( catname, g_categories [ i ] -> catname, 500 );
576 if ( ( actual_catname = strchr ( catname, '.' ) ) ) {
577 actual_catname++; // skip the period
579 actual_catname = catname;
581 //fprintf ( stderr, "conf ui; got '%s' but showing '%s'\n", cc [ i ].catname, actual_catname );
583 if ( strncmp ( g_categories [ i ] -> catname, "All ", 4 ) == 0 ) {
584 // skip All tab, since it is generated, and managed by another config item
588 p -> text = strndup ( actual_catname, 40 );
592 sprintf ( buffer, "%s.%s", template -> key, actual_catname );
593 p -> key = strdup ( buffer );
594 p -> type = ct_boolean;
597 // create to positive if not existant
598 if ( ! pnd_conf_get_as_char ( g_conf, buffer ) ) {
599 pnd_conf_set_char ( g_conf, buffer, "1" );
602 //fprintf ( stderr, "Created tabshow entry '%s'\n", cc [ i ].catname );
607 // switch categories being published
608 category_publish ( CFNORMAL, NULL );
619 unsigned char conf_write ( pnd_conf_handle h, char *fullpath ) {
621 // cherry pick the named keys from the conf-ui-array
622 // spill out the apps ands tabs 'broups' of conf keys
624 FILE *f = fopen ( fullpath, "w" );
630 fprintf ( f, "# Machine written; do not edit.\n" );
631 fprintf ( f, "# If you do edit, its KEY<tab>VALUE<newline>, nothing extra.\n" );
634 // deal with general keys
635 confitem_t *ci = page_general;
636 while ( ci -> text ) {
637 // does this item have a key? a value? if so, try to emit it.
639 char *v = pnd_conf_get_as_char ( h, ci -> key );
641 fprintf ( f, "%s\t%s\n", ci -> key, v );
647 // deal with apps and tabs
648 char *v = pnd_box_get_head ( g_conf );
651 // does item begin with app or tab tag?
652 char *k = pnd_box_get_key ( v );
655 ( strncasecmp ( k, "appshow.", 8 ) == 0 ||
656 strncasecmp ( k, "tabshow.", 8 ) == 0 )
659 fprintf ( f, "%s\t%s\n", k, v );
662 v = pnd_box_get_next ( v );
670 void conf_merge_into ( char *fullpath, pnd_conf_handle h ) {
672 char buffer [ 1024 ];
675 f = fopen ( fullpath, "r" );
681 while ( fgets ( buffer, 1000, f ) ) {
683 // trim trailing spaces
684 s = strchr ( buffer, ' ' );
690 s = strchr ( buffer, '#' );
695 s = strchr ( buffer, '\n' );
700 // if theres anything left..
701 if ( buffer [ 0 ] != '\0' ) {
703 // assume FOO<tab>BAR<newline> since this really should be machine written, not human screwed with; or if human
704 // edited, assume they know what to do :) I even put in some 'docs' in the conf file.
705 char *s = strchr ( buffer, '\t' );
710 // set it; libpnd conf code already handles 'existant' or 'new'
711 pnd_conf_set_char ( h, buffer, s + 1 );
724 char *conf_determine_location ( pnd_conf_handle h ) {
725 static char path [ PATH_MAX ];
727 bzero ( path, PATH_MAX );
729 if ( ! getenv ( "HOME" ) ) {
730 return ( "." ); // wtf?
733 snprintf ( path, PATH_MAX - strlen(CONF_PREF_FILENAME) - 1, "%s/%s", getenv ( "HOME" ), CONF_PREF_FILENAME );
738 void conf_setup_missing ( pnd_conf_handle h ) {
740 confitem_t *ci = page_general;
742 while ( ci -> text ) {
744 // does this item have a default value?
747 // it does, so lets see if we can pull a current value in; if not, set one
748 char *v = pnd_conf_get_as_char ( h, ci -> key );
751 fprintf ( stderr, "pref conf: no value present in config, better set to default; key is '%s' def '%s'\n", ci -> key, ci -> def );
752 pnd_conf_set_char ( h, ci -> key, ci -> def );
763 void conf_reset_to_default ( pnd_conf_handle h ) {
765 // reset all keys to default value - if present
766 // reset all apps to show
767 // reset all tabs to show
769 // deal with general keys
770 confitem_t *ci = page_general;
771 while ( ci -> text ) {
773 // does this item have a default value? if so, set it
774 if ( ci -> key && ci -> def ) {
775 pnd_conf_set_char ( h, ci -> key, ci -> def );
781 // deal with apps and tabs
782 char *v = pnd_box_get_head ( g_conf );
785 next = pnd_box_get_next ( v );
787 // does item begin with app or tab tag?
788 char *k = pnd_box_get_key ( v );
791 ( strncasecmp ( k, "appshow.", 8 ) == 0 ||
792 strncasecmp ( k, "tabshow.", 8 ) == 0 )
795 pnd_conf_set_char ( g_conf, k, "1" );
804 unsigned int conf_determine_pagelength ( confitem_t *page ) {
805 confitem_t *p = page;
806 while ( p -> text ) {