fix red
[kugel-rb.git] / apps / menus / main_menu.c
blob77322c6ef4824e458e2027f6f3fe977bc0daab00
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Jonathan Gordon
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdbool.h>
23 #include <stddef.h>
24 #include <limits.h>
25 #include "config.h"
26 #include "string.h"
27 #include "lang.h"
28 #include "action.h"
29 #include "settings.h"
30 #include "powermgmt.h"
31 #include "menu.h"
32 #include "misc.h"
33 #include "exported_menus.h"
34 #include "tree.h"
35 #ifdef HAVE_RECORDING
36 #include "recording.h"
37 #endif
38 #include "bookmark.h"
39 #include "yesno.h"
40 #include "keyboard.h"
41 #include "screens.h"
42 #include "plugin.h"
43 #include "talk.h"
44 #include "buffer.h"
45 #include "splash.h"
46 #include "debug_menu.h"
47 #if defined(SIMULATOR) && defined(ROCKBOX_HAS_LOGF)
48 #include "logfdisp.h"
49 #endif
50 #include "version.h"
51 #include "time.h"
52 #include "gwps.h"
54 static const struct browse_folder_info config = {ROCKBOX_DIR, SHOW_CFG};
56 /***********************************/
57 /* MANAGE SETTINGS MENU */
59 static int reset_settings(void)
61 static const char *lines[]={ID2P(LANG_RESET_ASK)};
62 static const char *yes_lines[]={
63 ID2P(LANG_SETTINGS),
64 ID2P(LANG_RESET_DONE_CLEAR)
66 static const char *no_lines[]={
67 ID2P(LANG_SETTINGS),
68 ID2P(LANG_CANCEL)
70 static const struct text_message message={lines, 1};
71 static const struct text_message yes_message={yes_lines, 2};
72 static const struct text_message no_message={no_lines, 2};
74 switch(gui_syncyesno_run(&message, &yes_message, &no_message))
76 case YESNO_YES:
77 settings_reset();
78 settings_apply(true);
79 settings_save();
80 break;
81 case YESNO_NO:
82 break;
83 case YESNO_USB:
84 return 1;
86 return 0;
88 static int write_settings_file(void* param)
90 return settings_save_config((intptr_t)param);
93 MENUITEM_FUNCTION(browse_configs, MENU_FUNC_USEPARAM, ID2P(LANG_CUSTOM_CFG),
94 browse_folder, (void*)&config, NULL, Icon_NOICON);
95 MENUITEM_FUNCTION(save_settings_item, MENU_FUNC_USEPARAM, ID2P(LANG_SAVE_SETTINGS),
96 write_settings_file, (void*)SETTINGS_SAVE_ALL, NULL, Icon_NOICON);
97 MENUITEM_FUNCTION(save_theme_item, MENU_FUNC_USEPARAM, ID2P(LANG_SAVE_THEME),
98 write_settings_file, (void*)SETTINGS_SAVE_THEME, NULL, Icon_NOICON);
99 MENUITEM_FUNCTION(save_sound_item, MENU_FUNC_USEPARAM, ID2P(LANG_SAVE_SOUND),
100 write_settings_file, (void*)SETTINGS_SAVE_SOUND, NULL, Icon_NOICON);
101 MENUITEM_FUNCTION(reset_settings_item, 0, ID2P(LANG_RESET),
102 reset_settings, NULL, NULL, Icon_NOICON);
104 MAKE_MENU(manage_settings, ID2P(LANG_MANAGE_MENU), NULL, Icon_Config,
105 &browse_configs, &reset_settings_item,
106 &save_settings_item, &save_sound_item, &save_theme_item);
107 /* MANAGE SETTINGS MENU */
108 /**********************************/
110 /***********************************/
111 /* INFO MENU */
113 static bool show_credits(void)
115 if (plugin_load(VIEWERS_DIR "/credits.rock",NULL) != PLUGIN_OK)
117 /* show the rockbox logo and version untill a button is pressed */
118 show_logo();
119 get_action(CONTEXT_STD, TIMEOUT_BLOCK);
121 return false;
124 #ifdef HAVE_LCD_CHARCELLS
125 #define SIZE_FMT "%s%s"
126 #else
127 #define SIZE_FMT "%s %s"
128 #endif
129 struct info_data
132 bool new_data;
133 unsigned long size;
134 unsigned long free;
135 #ifdef HAVE_MULTIVOLUME
136 unsigned long size2;
137 unsigned long free2;
138 #endif
140 enum infoscreenorder
142 INFO_BATTERY = 0,
143 INFO_DISK1, /* capacity or internal capacity/free on hotswap */
144 INFO_DISK2, /* free space or external capacity/free on hotswap */
145 INFO_BUFFER,
146 #ifdef HAVE_ALBUMART
147 INFO_ALBUMART,
148 #endif
149 INFO_VERSION,
150 #if CONFIG_RTC
151 INFO_DATE,
152 INFO_TIME,
153 #endif
154 INFO_COUNT
157 static const unsigned char *kbyte_units[] =
159 ID2P(LANG_KILOBYTE),
160 ID2P(LANG_MEGABYTE),
161 ID2P(LANG_GIGABYTE)
164 static char* info_getname(int selected_item, void *data,
165 char *buffer, size_t buffer_len)
167 struct info_data *info = (struct info_data*)data;
168 #if CONFIG_RTC
169 struct tm *tm;
170 #endif
171 char s1[32];
172 #ifdef HAVE_MULTIVOLUME
173 char s2[32];
174 #endif
175 if (info->new_data)
177 fat_size(IF_MV2(0,) &info->size, &info->free);
178 #ifdef HAVE_MULTIVOLUME
179 if (fat_ismounted(1))
180 fat_size(1, &info->size2, &info->free2);
181 else
182 info->size2 = 0;
183 #endif
184 info->new_data = false;
186 switch (selected_item)
188 case INFO_VERSION:
189 snprintf(buffer, buffer_len, "%s: %s",
190 str(LANG_VERSION), appsversion);
191 break;
192 #if CONFIG_RTC
193 case INFO_TIME:
194 tm = get_time();
195 if (valid_time(tm))
197 snprintf(buffer, buffer_len, "%02d:%02d:%02d %s",
198 global_settings.timeformat == 0 ? tm->tm_hour :
199 ((tm->tm_hour + 11) % 12) + 1,
200 tm->tm_min,
201 tm->tm_sec,
202 global_settings.timeformat == 0 ? "" :
203 tm->tm_hour>11 ? "P" : "A");
205 else
207 strncpy(buffer, "--:--:--", buffer_len);
209 break;
210 case INFO_DATE:
211 tm = get_time();
212 if (valid_time(tm))
214 snprintf(buffer, buffer_len, "%s %d %d",
215 str(LANG_MONTH_JANUARY + tm->tm_mon),
216 tm->tm_mday,
217 tm->tm_year+1900);
219 else
221 strncpy(buffer, str(LANG_UNKNOWN), buffer_len);
223 break;
224 #endif
225 case INFO_BUFFER: /* buffer */
227 long kib = (audiobufend - audiobuf) / 1024; /* to KiB */
228 output_dyn_value(s1, sizeof(s1), kib, kbyte_units, true);
229 snprintf(buffer, buffer_len, "%s %s", str(LANG_BUFFER_STAT), s1);
231 break;
232 case INFO_BATTERY: /* battery */
233 #if CONFIG_CHARGING == CHARGING_SIMPLE
234 if (charger_input_state == CHARGER)
235 return (char *)str(LANG_BATTERY_CHARGE);
236 else
237 #elif CONFIG_CHARGING >= CHARGING_MONITOR
238 if (charge_state == CHARGING)
239 return (char *)str(LANG_BATTERY_CHARGE);
240 else
241 #if CONFIG_CHARGING == CHARGING_CONTROL
242 if (charge_state == TOPOFF)
243 return (char *)str(LANG_BATTERY_TOPOFF_CHARGE);
244 else
245 #endif
246 if (charge_state == TRICKLE)
247 return (char *)str(LANG_BATTERY_TRICKLE_CHARGE);
248 else
249 #endif
250 if (battery_level() >= 0)
251 snprintf(buffer, buffer_len, (char *)str(LANG_BATTERY_TIME),
252 battery_level(), battery_time() / 60, battery_time() % 60);
253 else
254 return "(n/a)";
255 break;
256 case INFO_DISK1: /* disk usage 1 */
257 #ifdef HAVE_MULTIVOLUME
258 output_dyn_value(s1, sizeof s1, info->free, kbyte_units, true);
259 output_dyn_value(s2, sizeof s2, info->size, kbyte_units, true);
260 snprintf(buffer, buffer_len, "%s %s/%s", str(LANG_DISK_NAME_INTERNAL),
261 s1, s2);
262 #else
263 output_dyn_value(s1, sizeof s1, info->free, kbyte_units, true);
264 snprintf(buffer, buffer_len, SIZE_FMT, str(LANG_DISK_FREE_INFO), s1);
265 #endif
266 break;
267 case INFO_DISK2: /* disk usage 2 */
268 #ifdef HAVE_MULTIVOLUME
269 if (info->size2)
271 output_dyn_value(s1, sizeof s1, info->free2, kbyte_units, true);
272 output_dyn_value(s2, sizeof s2, info->size2, kbyte_units, true);
273 snprintf(buffer, buffer_len, "%s %s/%s", str(LANG_DISK_NAME_MMC),
274 s1, s2);
276 else
278 snprintf(buffer, buffer_len, "%s %s", str(LANG_DISK_NAME_MMC),
279 str(LANG_NOT_PRESENT));
281 #else
282 output_dyn_value(s1, sizeof s1, info->size, kbyte_units, true);
283 snprintf(buffer, buffer_len, SIZE_FMT, str(LANG_DISK_SIZE_INFO), s1);
284 #endif
285 break;
286 #ifdef HAVE_ALBUMART
287 case INFO_ALBUMART: /* album art dimenstions */
288 if (gui_sync_wps_uses_albumart())
290 snprintf(buffer, buffer_len, "%s %dx%d", str(LANG_ALBUMART),
291 gui_wps[0].data->albumart_max_width,
292 gui_wps[0].data->albumart_max_height);
294 else
296 snprintf(buffer, buffer_len, "%s %s", str(LANG_ALBUMART),
297 str(LANG_SET_BOOL_NO));
299 #endif
301 return buffer;
304 static int info_speak_item(int selected_item, void * data)
306 struct info_data *info = (struct info_data*)data;
308 #if CONFIG_RTC
309 struct tm *tm;
310 #endif
312 switch (selected_item)
314 case INFO_VERSION: /* version */
315 talk_id(LANG_VERSION, false);
316 talk_spell(appsversion, true);
317 break;
318 #if CONFIG_RTC
319 case INFO_TIME:
320 tm = get_time();
321 talk_id(VOICE_CURRENT_TIME, false);
322 if (valid_time(tm))
324 talk_time(tm, true);
326 else
328 talk_id(LANG_UNKNOWN, true);
330 break;
331 case INFO_DATE:
332 tm = get_time();
333 if (valid_time(tm))
335 talk_date(get_time(), true);
337 else
339 talk_id(LANG_UNKNOWN, true);
341 break;
342 #endif
343 case INFO_BUFFER: /* buffer */
345 talk_id(LANG_BUFFER_STAT, false);
346 long kib = (audiobufend - audiobuf) / 1024; /* to KiB */
347 output_dyn_value(NULL, 0, kib, kbyte_units, true);
348 break;
350 case INFO_BATTERY: /* battery */
351 #if CONFIG_CHARGING == CHARGING_SIMPLE
352 if (charger_input_state == CHARGER)
353 talk_id(LANG_BATTERY_CHARGE, true);
354 else
355 #elif CONFIG_CHARGING >= CHARGING_MONITOR
356 if (charge_state == CHARGING)
357 talk_id(LANG_BATTERY_CHARGE, true);
358 else
359 #if CONFIG_CHARGING == CHARGING_CONTROL
360 if (charge_state == TOPOFF)
361 talk_id(LANG_BATTERY_TOPOFF_CHARGE, true);
362 else
363 #endif
364 if (charge_state == TRICKLE)
365 talk_id(LANG_BATTERY_TRICKLE_CHARGE, true);
366 else
367 #endif
368 if (battery_level() >= 0)
370 talk_id(LANG_BATTERY_TIME, false);
371 talk_value(battery_level(), UNIT_PERCENT, true);
372 talk_value(battery_time() *60, UNIT_TIME, true);
374 else talk_id(VOICE_BLANK, false);
375 break;
376 case INFO_DISK1: /* disk 1 */
377 #ifdef HAVE_MULTIVOLUME
378 talk_id(LANG_DISK_NAME_INTERNAL, false);
379 talk_id(LANG_DISK_FREE_INFO, true);
380 output_dyn_value(NULL, 0, info->free, kbyte_units, true);
381 talk_id(LANG_DISK_SIZE_INFO, true);
382 output_dyn_value(NULL, 0, info->size, kbyte_units, true);
383 #else
384 talk_id(LANG_DISK_FREE_INFO, false);
385 output_dyn_value(NULL, 0, info->free, kbyte_units, true);
386 #endif
387 break;
388 case INFO_DISK2: /* disk 2 */
389 #ifdef HAVE_MULTIVOLUME
390 talk_id(LANG_DISK_NAME_MMC, false);
391 if (info->size2)
393 talk_id(LANG_DISK_FREE_INFO, true);
394 output_dyn_value(NULL, 0, info->free2, kbyte_units, true);
395 talk_id(LANG_DISK_SIZE_INFO, true);
396 output_dyn_value(NULL, 0, info->size2, kbyte_units, true);
398 else talk_id(LANG_NOT_PRESENT, true);
399 #else
400 talk_id(LANG_DISK_SIZE_INFO, false);
401 output_dyn_value(NULL, 0, info->size, kbyte_units, true);
402 #endif
403 break;
404 #ifdef HAVE_ALBUMART
405 case INFO_ALBUMART: /* album art dimenstions */
406 if (gui_sync_wps_uses_albumart())
408 talk_id(LANG_ALBUMART, false);
409 talk_value(gui_wps[0].data->albumart_max_width, UNIT_PIXEL, true);
410 talk_value(gui_wps[0].data->albumart_max_height, UNIT_PIXEL, true);
412 else
414 talk_id(LANG_ALBUMART, false);
415 talk_id(LANG_SET_BOOL_NO, true);
417 #endif
419 return 0;
422 static int info_action_callback(int action, struct gui_synclist *lists)
424 if (action == ACTION_STD_CANCEL)
425 return action;
426 else if ((action == ACTION_STD_OK)
427 #ifdef HAVE_MULTIVOLUME
428 || action == SYS_HOTSWAP_INSERTED
429 || action == SYS_HOTSWAP_EXTRACTED
430 #endif
433 #ifndef SIMULATOR
434 struct info_data *info = (struct info_data *)lists->data;
435 info->new_data = true;
436 splash(0, ID2P(LANG_SCANNING_DISK));
437 fat_recalc_free(IF_MV(0));
438 #ifdef HAVE_MULTIVOLUME
439 if (fat_ismounted(1))
440 fat_recalc_free(1);
441 #endif
443 #else
445 (void) lists;
446 #endif
447 gui_synclist_speak_item(lists);
448 return ACTION_REDRAW;
450 #if CONFIG_RTC
451 else if (action == ACTION_NONE)
453 static int last_redraw = 0;
454 if (gui_synclist_item_is_onscreen(lists, 0, INFO_TIME)
455 && TIME_AFTER(current_tick, last_redraw + HZ*5))
457 last_redraw = current_tick;
458 return ACTION_REDRAW;
461 #endif
462 return action;
464 static bool show_info(void)
466 struct info_data data = {.new_data = true };
467 struct simplelist_info info;
468 simplelist_info_init(&info, str(LANG_ROCKBOX_INFO), INFO_COUNT, (void*)&data);
469 info.hide_selection = !global_settings.talk_menu;
470 if (info.hide_selection)
471 info.scroll_all = true;
472 info.get_name = info_getname;
473 if(global_settings.talk_menu)
474 info.get_talk = info_speak_item;
475 info.action_callback = info_action_callback;
476 return simplelist_show_list(&info);
478 MENUITEM_FUNCTION(show_info_item, 0, ID2P(LANG_ROCKBOX_INFO),
479 (menu_function)show_info, NULL, NULL, Icon_NOICON);
482 /* sleep Menu */
483 static void sleep_timer_formatter(char* buffer, size_t buffer_size, int value,
484 const char* unit)
486 int minutes, hours;
488 (void) unit;
490 if (value) {
491 hours = value / 60;
492 minutes = value - (hours * 60);
493 snprintf(buffer, buffer_size, "%d:%02d", hours, minutes);
494 } else {
495 strncpy(buffer, str(LANG_OFF), buffer_size);
499 static void sleep_timer_set(int minutes)
501 set_sleep_timer(minutes * 60);
504 static int sleep_timer(void)
506 int minutes = (get_sleep_timer() + 59) / 60; /* round up */
507 return (int)set_int(str(LANG_SLEEP_TIMER), "", UNIT_MIN, &minutes,
508 &sleep_timer_set, -5, 300, 0, sleep_timer_formatter);
511 MENUITEM_FUNCTION(sleep_timer_call, 0, ID2P(LANG_SLEEP_TIMER), sleep_timer,
512 NULL, NULL, Icon_Menu_setting); /* make it look like a
513 setting to the user */
514 MENUITEM_FUNCTION(show_credits_item, 0, ID2P(LANG_VERSION),
515 (menu_function)show_credits, NULL, NULL, Icon_NOICON);
516 MENUITEM_FUNCTION(show_runtime_item, 0, ID2P(LANG_RUNNING_TIME),
517 (menu_function)view_runtime, NULL, NULL, Icon_NOICON);
518 MENUITEM_FUNCTION(debug_menu_item, 0, ID2P(LANG_DEBUG),
519 (menu_function)debug_menu, NULL, NULL, Icon_NOICON);
521 MAKE_MENU(info_menu, ID2P(LANG_SYSTEM), 0, Icon_Questionmark,
522 &show_info_item, &show_credits_item, &show_runtime_item,
523 &sleep_timer_call, &debug_menu_item);
524 /* INFO MENU */
525 /***********************************/
527 /***********************************/
528 /* MAIN MENU */
531 #ifdef HAVE_LCD_CHARCELLS
532 static int mainmenu_callback(int action,const struct menu_item_ex *this_item)
534 (void)this_item;
535 switch (action)
537 case ACTION_ENTER_MENUITEM:
538 status_set_param(true);
539 break;
540 case ACTION_EXIT_MENUITEM:
541 status_set_param(false);
542 break;
544 return action;
546 #else
547 #define mainmenu_callback NULL
548 #endif
549 MAKE_MENU(main_menu_, ID2P(LANG_SETTINGS), mainmenu_callback,
550 Icon_Submenu_Entered,
551 &sound_settings,
552 &playback_settings,
553 &settings_menu_item, &theme_menu,
554 #ifdef HAVE_RECORDING
555 &recording_settings,
556 #endif
557 &manage_settings,
559 /* MAIN MENU */
560 /***********************************/