Colour targets: Revert an optimisation from almost 18 months ago that actually turned...
[Rockbox.git] / apps / menus / main_menu.c
blob985efc2580cd097060625694b82cc55011c749d9
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"
53 static const struct browse_folder_info config = {ROCKBOX_DIR, SHOW_CFG};
55 /***********************************/
56 /* MANAGE SETTINGS MENU */
58 static int reset_settings(void)
60 static const char *lines[]={ID2P(LANG_RESET_ASK)};
61 static const char *yes_lines[]={
62 ID2P(LANG_SETTINGS),
63 ID2P(LANG_RESET_DONE_CLEAR)
65 static const char *no_lines[]={
66 ID2P(LANG_SETTINGS),
67 ID2P(LANG_CANCEL)
69 static const struct text_message message={lines, 1};
70 static const struct text_message yes_message={yes_lines, 2};
71 static const struct text_message no_message={no_lines, 2};
73 switch(gui_syncyesno_run(&message, &yes_message, &no_message))
75 case YESNO_YES:
76 settings_reset();
77 settings_apply(true);
78 settings_save();
79 break;
80 case YESNO_NO:
81 break;
82 case YESNO_USB:
83 return 1;
85 return 0;
87 static int write_settings_file(void* param)
89 return settings_save_config((intptr_t)param);
92 MENUITEM_FUNCTION(browse_configs, MENU_FUNC_USEPARAM, ID2P(LANG_CUSTOM_CFG),
93 browse_folder, (void*)&config, NULL, Icon_NOICON);
94 MENUITEM_FUNCTION(save_settings_item, MENU_FUNC_USEPARAM, ID2P(LANG_SAVE_SETTINGS),
95 write_settings_file, (void*)SETTINGS_SAVE_ALL, NULL, Icon_NOICON);
96 MENUITEM_FUNCTION(save_theme_item, MENU_FUNC_USEPARAM, ID2P(LANG_SAVE_THEME),
97 write_settings_file, (void*)SETTINGS_SAVE_THEME, NULL, Icon_NOICON);
98 MENUITEM_FUNCTION(save_sound_item, MENU_FUNC_USEPARAM, ID2P(LANG_SAVE_SOUND),
99 write_settings_file, (void*)SETTINGS_SAVE_SOUND, NULL, Icon_NOICON);
100 MENUITEM_FUNCTION(reset_settings_item, 0, ID2P(LANG_RESET),
101 reset_settings, NULL, NULL, Icon_NOICON);
103 MAKE_MENU(manage_settings, ID2P(LANG_MANAGE_MENU), NULL, Icon_Config,
104 &browse_configs, &reset_settings_item,
105 &save_settings_item, &save_sound_item, &save_theme_item);
106 /* MANAGE SETTINGS MENU */
107 /**********************************/
109 /***********************************/
110 /* INFO MENU */
112 static bool show_credits(void)
114 if (plugin_load(VIEWERS_DIR "/credits.rock",NULL) != PLUGIN_OK)
116 /* show the rockbox logo and version untill a button is pressed */
117 show_logo();
118 get_action(CONTEXT_STD, TIMEOUT_BLOCK);
120 return false;
123 #ifdef HAVE_LCD_CHARCELLS
124 #define SIZE_FMT "%s%s"
125 #else
126 #define SIZE_FMT "%s %s"
127 #endif
128 struct info_data
131 bool new_data;
132 unsigned long size;
133 unsigned long free;
134 #ifdef HAVE_MULTIVOLUME
135 unsigned long size2;
136 unsigned long free2;
137 #endif
139 enum infoscreenorder
141 INFO_BATTERY = 0,
142 INFO_DISK1, /* capacity or internal capacity/free on hotswap */
143 INFO_DISK2, /* free space or external capacity/free on hotswap */
144 INFO_BUFFER,
145 INFO_VERSION,
146 #if CONFIG_RTC
147 INFO_DATE,
148 INFO_TIME,
149 #endif
150 INFO_COUNT
153 static const unsigned char *kbyte_units[] =
155 ID2P(LANG_KILOBYTE),
156 ID2P(LANG_MEGABYTE),
157 ID2P(LANG_GIGABYTE)
160 static char* info_getname(int selected_item, void *data,
161 char *buffer, size_t buffer_len)
163 struct info_data *info = (struct info_data*)data;
164 #if CONFIG_RTC
165 struct tm *tm;
166 #endif
167 char s1[32];
168 #ifdef HAVE_MULTIVOLUME
169 char s2[32];
170 #endif
171 if (info->new_data)
173 fat_size(IF_MV2(0,) &info->size, &info->free);
174 #ifdef HAVE_MULTIVOLUME
175 if (fat_ismounted(1))
176 fat_size(1, &info->size2, &info->free2);
177 else
178 info->size2 = 0;
179 #endif
180 info->new_data = false;
182 switch (selected_item)
184 case INFO_VERSION:
185 snprintf(buffer, buffer_len, "%s: %s",
186 str(LANG_VERSION), appsversion);
187 break;
188 #if CONFIG_RTC
189 case INFO_TIME:
190 tm = get_time();
191 if (valid_time(tm))
193 snprintf(buffer, buffer_len, "%02d:%02d:%02d %s",
194 global_settings.timeformat == 0 ? tm->tm_hour :
195 ((tm->tm_hour + 11) % 12) + 1,
196 tm->tm_min,
197 tm->tm_sec,
198 global_settings.timeformat == 0 ? "" :
199 tm->tm_hour>11 ? "P" : "A");
201 else
203 snprintf(buffer, buffer_len, "%s", "--:--:--");
205 break;
206 case INFO_DATE:
207 tm = get_time();
208 if (valid_time(tm))
210 snprintf(buffer, buffer_len, "%s %d %d",
211 str(LANG_MONTH_JANUARY + tm->tm_mon),
212 tm->tm_mday,
213 tm->tm_year+1900);
215 else
217 snprintf(buffer, buffer_len, "%s", str(LANG_UNKNOWN));
219 break;
220 #endif
221 case INFO_BUFFER: /* buffer */
223 long buflen = ((audiobufend - audiobuf) * 2) / 2097; /* avoid overflow */
224 int integer = buflen / 1000;
225 int decimal = buflen % 1000;
226 snprintf(buffer, buffer_len, (char *)str(LANG_BUFFER_STAT),
227 integer, decimal);
229 break;
230 case INFO_BATTERY: /* battery */
231 #if CONFIG_CHARGING == CHARGING_SIMPLE
232 if (charger_input_state == CHARGER)
233 return (char *)str(LANG_BATTERY_CHARGE);
234 else
235 #elif CONFIG_CHARGING >= CHARGING_MONITOR
236 if (charge_state == CHARGING)
237 return (char *)str(LANG_BATTERY_CHARGE);
238 else
239 #if CONFIG_CHARGING == CHARGING_CONTROL
240 if (charge_state == TOPOFF)
241 return (char *)str(LANG_BATTERY_TOPOFF_CHARGE);
242 else
243 #endif
244 if (charge_state == TRICKLE)
245 return (char *)str(LANG_BATTERY_TRICKLE_CHARGE);
246 else
247 #endif
248 if (battery_level() >= 0)
249 snprintf(buffer, buffer_len, (char *)str(LANG_BATTERY_TIME),
250 battery_level(), battery_time() / 60, battery_time() % 60);
251 else
252 return "(n/a)";
253 break;
254 case INFO_DISK1: /* disk usage 1 */
255 #ifdef HAVE_MULTIVOLUME
256 output_dyn_value(s1, sizeof s1, info->free, kbyte_units, true);
257 output_dyn_value(s2, sizeof s2, info->size, kbyte_units, true);
258 snprintf(buffer, buffer_len, "%s %s/%s", str(LANG_DISK_NAME_INTERNAL),
259 s1, s2);
260 #else
261 output_dyn_value(s1, sizeof s1, info->free, kbyte_units, true);
262 snprintf(buffer, buffer_len, SIZE_FMT, str(LANG_DISK_FREE_INFO), s1);
263 #endif
264 break;
265 case INFO_DISK2: /* disk usage 2 */
266 #ifdef HAVE_MULTIVOLUME
267 if (info->size2)
269 output_dyn_value(s1, sizeof s1, info->free2, kbyte_units, true);
270 output_dyn_value(s2, sizeof s2, info->size2, kbyte_units, true);
271 snprintf(buffer, buffer_len, "%s %s/%s", str(LANG_DISK_NAME_MMC),
272 s1, s2);
274 else
276 snprintf(buffer, buffer_len, "%s %s", str(LANG_DISK_NAME_MMC),
277 str(LANG_NOT_PRESENT));
279 #else
280 output_dyn_value(s1, sizeof s1, info->size, kbyte_units, true);
281 snprintf(buffer, buffer_len, SIZE_FMT, str(LANG_DISK_SIZE_INFO), s1);
282 #endif
283 break;
285 return buffer;
288 static int info_speak_item(int selected_item, void * data)
290 struct info_data *info = (struct info_data*)data;
292 #if CONFIG_RTC
293 struct tm *tm;
294 #endif
296 switch (selected_item)
298 case INFO_VERSION: /* version */
299 talk_id(LANG_VERSION, false);
300 talk_spell(appsversion, true);
301 break;
302 #if CONFIG_RTC
303 case INFO_TIME:
304 tm = get_time();
305 talk_id(VOICE_CURRENT_TIME, false);
306 if (valid_time(tm))
308 talk_time(tm, true);
310 else
312 talk_id(LANG_UNKNOWN, true);
314 break;
315 case INFO_DATE:
316 tm = get_time();
317 if (valid_time(tm))
319 talk_date(get_time(), true);
321 else
323 talk_id(LANG_UNKNOWN, true);
325 break;
326 #endif
327 case INFO_BUFFER: /* buffer */
329 talk_id(LANG_BUFFER_STAT, false);
330 long buflen = ((audiobufend - audiobuf) * 2) / 2097; /* avoid overflow */
331 output_dyn_value(NULL, 0, buflen, kbyte_units, true);
332 break;
334 case INFO_BATTERY: /* battery */
335 #if CONFIG_CHARGING == CHARGING_SIMPLE
336 if (charger_input_state == CHARGER)
337 talk_id(LANG_BATTERY_CHARGE, true);
338 else
339 #elif CONFIG_CHARGING >= CHARGING_MONITOR
340 if (charge_state == CHARGING)
341 talk_id(LANG_BATTERY_CHARGE, true);
342 else
343 #if CONFIG_CHARGING == CHARGING_CONTROL
344 if (charge_state == TOPOFF)
345 talk_id(LANG_BATTERY_TOPOFF_CHARGE, true);
346 else
347 #endif
348 if (charge_state == TRICKLE)
349 talk_id(LANG_BATTERY_TRICKLE_CHARGE, true);
350 else
351 #endif
352 if (battery_level() >= 0)
354 talk_id(LANG_BATTERY_TIME, false);
355 talk_value(battery_level(), UNIT_PERCENT, true);
356 talk_value(battery_time() *60, UNIT_TIME, true);
358 else talk_id(VOICE_BLANK, false);
359 break;
360 case INFO_DISK1: /* disk 1 */
361 #ifdef HAVE_MULTIVOLUME
362 talk_id(LANG_DISK_NAME_INTERNAL, false);
363 talk_id(LANG_DISK_FREE_INFO, true);
364 output_dyn_value(NULL, 0, info->free, kbyte_units, true);
365 talk_id(LANG_DISK_SIZE_INFO, true);
366 output_dyn_value(NULL, 0, info->size, kbyte_units, true);
367 #else
368 talk_id(LANG_DISK_FREE_INFO, false);
369 output_dyn_value(NULL, 0, info->free, kbyte_units, true);
370 #endif
371 break;
372 case INFO_DISK2: /* disk 2 */
373 #ifdef HAVE_MULTIVOLUME
374 talk_id(LANG_DISK_NAME_MMC, false);
375 if (info->size2)
377 talk_id(LANG_DISK_FREE_INFO, true);
378 output_dyn_value(NULL, 0, info->free2, kbyte_units, true);
379 talk_id(LANG_DISK_SIZE_INFO, true);
380 output_dyn_value(NULL, 0, info->size2, kbyte_units, true);
382 else talk_id(LANG_NOT_PRESENT, true);
383 #else
384 talk_id(LANG_DISK_SIZE_INFO, false);
385 output_dyn_value(NULL, 0, info->size, kbyte_units, true);
386 #endif
387 break;
389 return 0;
392 static int info_action_callback(int action, struct gui_synclist *lists)
394 if (action == ACTION_STD_CANCEL)
395 return action;
396 else if ((action == ACTION_STD_OK)
397 #ifdef HAVE_MULTIVOLUME
398 || action == SYS_HOTSWAP_INSERTED
399 || action == SYS_HOTSWAP_EXTRACTED
400 #endif
403 #ifndef SIMULATOR
404 struct info_data *info = (struct info_data *)lists->data;
405 info->new_data = true;
406 gui_syncsplash(0, ID2P(LANG_SCANNING_DISK));
407 fat_recalc_free(IF_MV(0));
408 #ifdef HAVE_MULTIVOLUME
409 if (fat_ismounted(1))
410 fat_recalc_free(1);
411 #endif
413 #else
415 (void) lists;
416 #endif
417 return ACTION_REDRAW;
419 #if CONFIG_RTC
420 else if (action == ACTION_NONE && lists->selected_item == INFO_TIME)
422 static int last_redraw = 0;
423 if (TIME_AFTER(current_tick, last_redraw + HZ/2))
425 last_redraw = current_tick;
426 return ACTION_REDRAW;
429 #endif
430 return action;
432 static bool show_info(void)
434 struct info_data data = {.new_data = true };
435 struct simplelist_info info;
436 simplelist_info_init(&info, str(LANG_ROCKBOX_INFO), INFO_COUNT, (void*)&data);
437 info.hide_selection = !global_settings.talk_menu;
438 if (info.hide_selection)
439 info.scroll_all = true;
440 info.get_name = info_getname;
441 if(global_settings.talk_menu)
442 info.get_talk = info_speak_item;
443 info.action_callback = info_action_callback;
444 return simplelist_show_list(&info);
446 MENUITEM_FUNCTION(show_info_item, 0, ID2P(LANG_ROCKBOX_INFO),
447 (menu_function)show_info, NULL, NULL, Icon_NOICON);
450 /* sleep Menu */
451 static void sleep_timer_formatter(char* buffer, size_t buffer_size, int value,
452 const char* unit)
454 int minutes, hours;
456 (void) unit;
458 if (value) {
459 hours = value / 60;
460 minutes = value - (hours * 60);
461 snprintf(buffer, buffer_size, "%d:%02d", hours, minutes);
462 } else {
463 snprintf(buffer, buffer_size, "%s", str(LANG_OFF));
467 static void sleep_timer_set(int minutes)
469 set_sleep_timer(minutes * 60);
472 static int sleep_timer(void)
474 int minutes = (get_sleep_timer() + 59) / 60; /* round up */
475 return (int)set_int(str(LANG_SLEEP_TIMER), "", UNIT_MIN, &minutes,
476 &sleep_timer_set, -5, 300, 0, sleep_timer_formatter);
479 MENUITEM_FUNCTION(sleep_timer_call, 0, ID2P(LANG_SLEEP_TIMER), sleep_timer,
480 NULL, NULL, Icon_Menu_setting); /* make it look like a
481 setting to the user */
482 MENUITEM_FUNCTION(show_credits_item, 0, ID2P(LANG_VERSION),
483 (menu_function)show_credits, NULL, NULL, Icon_NOICON);
484 MENUITEM_FUNCTION(show_runtime_item, 0, ID2P(LANG_RUNNING_TIME),
485 (menu_function)view_runtime, NULL, NULL, Icon_NOICON);
486 MENUITEM_FUNCTION(debug_menu_item, 0, ID2P(LANG_DEBUG),
487 (menu_function)debug_menu, NULL, NULL, Icon_NOICON);
489 MAKE_MENU(info_menu, ID2P(LANG_SYSTEM), 0, Icon_Questionmark,
490 &show_info_item, &show_credits_item, &show_runtime_item,
491 &sleep_timer_call, &debug_menu_item);
492 /* INFO MENU */
493 /***********************************/
495 /***********************************/
496 /* MAIN MENU */
499 #ifdef HAVE_LCD_CHARCELLS
500 int mainmenu_callback(int action,const struct menu_item_ex *this_item)
502 (void)this_item;
503 switch (action)
505 case ACTION_ENTER_MENUITEM:
506 status_set_param(true);
507 break;
508 case ACTION_EXIT_MENUITEM:
509 status_set_param(false);
510 break;
512 return action;
514 #else
515 #define mainmenu_callback NULL
516 #endif
517 MAKE_MENU(main_menu_, ID2P(LANG_SETTINGS), mainmenu_callback,
518 Icon_Submenu_Entered,
519 &sound_settings,
520 &settings_menu_item, &theme_menu,
521 #ifdef HAVE_RECORDING
522 &recording_settings,
523 #endif
524 &manage_settings,
526 /* MAIN MENU */
527 /***********************************/