FS#10785: Add new context CONTEXT_MORSE_INPUT for keymaps which is used during morse...
[kugel-rb.git] / apps / menus / main_menu.c
blob505801d3cc36b3f3de8d96e024e80406c04ed420
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 "power.h"
31 #include "powermgmt.h"
32 #include "menu.h"
33 #include "misc.h"
34 #include "exported_menus.h"
35 #include "tree.h"
36 #ifdef HAVE_RECORDING
37 #include "recording.h"
38 #endif
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 "wps.h"
53 #include "skin_engine/skin_buffer.h"
55 static const struct browse_folder_info config = {ROCKBOX_DIR, SHOW_CFG};
57 /***********************************/
58 /* MANAGE SETTINGS MENU */
60 static int reset_settings(void)
62 static const char *lines[]={ID2P(LANG_RESET_ASK)};
63 static const char *yes_lines[]={
64 ID2P(LANG_SETTINGS),
65 ID2P(LANG_RESET_DONE_CLEAR)
67 static const char *no_lines[]={
68 ID2P(LANG_SETTINGS),
69 ID2P(LANG_CANCEL)
71 static const struct text_message message={lines, 1};
72 static const struct text_message yes_message={yes_lines, 2};
73 static const struct text_message no_message={no_lines, 2};
75 switch(gui_syncyesno_run(&message, &yes_message, &no_message))
77 case YESNO_YES:
78 settings_reset();
79 settings_apply(true);
80 settings_save();
81 break;
82 case YESNO_NO:
83 break;
84 case YESNO_USB:
85 return 1;
87 return 0;
89 static int write_settings_file(void* param)
91 return settings_save_config((intptr_t)param);
94 MENUITEM_FUNCTION(browse_configs, MENU_FUNC_USEPARAM, ID2P(LANG_CUSTOM_CFG),
95 browse_folder, (void*)&config, NULL, Icon_NOICON);
96 MENUITEM_FUNCTION(save_settings_item, MENU_FUNC_USEPARAM, ID2P(LANG_SAVE_SETTINGS),
97 write_settings_file, (void*)SETTINGS_SAVE_ALL, NULL, Icon_NOICON);
98 MENUITEM_FUNCTION(save_theme_item, MENU_FUNC_USEPARAM, ID2P(LANG_SAVE_THEME),
99 write_settings_file, (void*)SETTINGS_SAVE_THEME, NULL, Icon_NOICON);
100 MENUITEM_FUNCTION(save_sound_item, MENU_FUNC_USEPARAM, ID2P(LANG_SAVE_SOUND),
101 write_settings_file, (void*)SETTINGS_SAVE_SOUND, NULL, Icon_NOICON);
102 MENUITEM_FUNCTION(reset_settings_item, 0, ID2P(LANG_RESET),
103 reset_settings, NULL, NULL, Icon_NOICON);
105 MAKE_MENU(manage_settings, ID2P(LANG_MANAGE_MENU), NULL, Icon_Config,
106 &browse_configs, &reset_settings_item,
107 &save_settings_item, &save_sound_item, &save_theme_item);
108 /* MANAGE SETTINGS MENU */
109 /**********************************/
111 /***********************************/
112 /* INFO MENU */
114 static bool show_credits(void)
116 if (plugin_load(VIEWERS_DIR "/credits.rock",NULL) != PLUGIN_OK)
118 /* show the rockbox logo and version untill a button is pressed */
119 show_logo();
120 while (IS_SYSEVENT(get_action(CONTEXT_STD, TIMEOUT_BLOCK)))
123 return false;
126 #ifdef HAVE_LCD_CHARCELLS
127 #define SIZE_FMT "%s%s"
128 #else
129 #define SIZE_FMT "%s %s"
130 #endif
131 struct info_data
134 bool new_data;
135 unsigned long size;
136 unsigned long free;
137 #ifdef HAVE_MULTIVOLUME
138 unsigned long size2;
139 unsigned long free2;
140 #endif
142 enum infoscreenorder
144 INFO_BATTERY = 0,
145 INFO_DISK1, /* capacity or internal capacity/free on hotswap */
146 INFO_DISK2, /* free space or external capacity/free on hotswap */
147 INFO_BUFFER,
148 INFO_SKIN_USAGE, /* ram usage of the skins */
149 INFO_VERSION,
150 INFO_COUNT
153 static const char* info_getname(int selected_item, void *data,
154 char *buffer, size_t buffer_len)
156 struct info_data *info = (struct info_data*)data;
157 char s1[32];
158 char s2[32];
159 if (info->new_data)
161 fat_size(IF_MV2(0,) &info->size, &info->free);
162 #ifdef HAVE_MULTIVOLUME
163 if (fat_ismounted(1))
164 fat_size(1, &info->size2, &info->free2);
165 else
166 info->size2 = 0;
167 #endif
168 info->new_data = false;
170 switch (selected_item)
172 case INFO_VERSION:
173 snprintf(buffer, buffer_len, "%s: %s",
174 str(LANG_VERSION), appsversion);
175 break;
177 case INFO_BUFFER: /* buffer */
179 long kib = (audiobufend - audiobuf) / 1024; /* to KiB */
180 output_dyn_value(s1, sizeof(s1), kib, kbyte_units, true);
181 snprintf(buffer, buffer_len, "%s %s", str(LANG_BUFFER_STAT), s1);
183 break;
184 case INFO_BATTERY: /* battery */
185 #if CONFIG_CHARGING == CHARGING_SIMPLE
186 /* Only know if plugged */
187 if (charger_inserted())
188 return str(LANG_BATTERY_CHARGE);
189 else
190 #elif CONFIG_CHARGING >= CHARGING_MONITOR
191 #ifdef ARCHOS_RECORDER
192 /* Report the particular algorithm state */
193 if (charge_state == CHARGING)
194 return str(LANG_BATTERY_CHARGE);
195 else if (charge_state == TOPOFF)
196 return str(LANG_BATTERY_TOPOFF_CHARGE);
197 else if (charge_state == TRICKLE)
198 return str(LANG_BATTERY_TRICKLE_CHARGE);
199 else
200 #else /* !ARCHOS_RECORDER */
201 /* Go by what power management reports */
202 if (charging_state())
203 return str(LANG_BATTERY_CHARGE);
204 else
205 #endif /* ARCHOS_RECORDER */
206 #endif /* CONFIG_CHARGING = */
207 if (battery_level() >= 0)
208 snprintf(buffer, buffer_len, str(LANG_BATTERY_TIME),
209 battery_level(), battery_time() / 60, battery_time() % 60);
210 else
211 return "(n/a)";
212 break;
213 case INFO_DISK1: /* disk usage 1 */
214 #ifdef HAVE_MULTIVOLUME
215 output_dyn_value(s1, sizeof s1, info->free, kbyte_units, true);
216 output_dyn_value(s2, sizeof s2, info->size, kbyte_units, true);
217 snprintf(buffer, buffer_len, "%s %s/%s", str(LANG_DISK_NAME_INTERNAL),
218 s1, s2);
219 #else
220 output_dyn_value(s1, sizeof s1, info->free, kbyte_units, true);
221 snprintf(buffer, buffer_len, SIZE_FMT, str(LANG_DISK_FREE_INFO), s1);
222 #endif
223 break;
224 case INFO_DISK2: /* disk usage 2 */
225 #ifdef HAVE_MULTIVOLUME
226 if (info->size2)
228 output_dyn_value(s1, sizeof s1, info->free2, kbyte_units, true);
229 output_dyn_value(s2, sizeof s2, info->size2, kbyte_units, true);
230 snprintf(buffer, buffer_len, "%s %s/%s", str(LANG_DISK_NAME_MMC),
231 s1, s2);
233 else
235 snprintf(buffer, buffer_len, "%s %s", str(LANG_DISK_NAME_MMC),
236 str(LANG_NOT_PRESENT));
238 #else
239 output_dyn_value(s1, sizeof s1, info->size, kbyte_units, true);
240 snprintf(buffer, buffer_len, SIZE_FMT, str(LANG_DISK_SIZE_INFO), s1);
241 #endif
242 break;
243 case INFO_SKIN_USAGE:
244 output_dyn_value(s1, sizeof s1, skin_buffer_usage(), byte_units, true);
245 output_dyn_value(s2, sizeof s2, skin_buffer_usage()
246 +skin_buffer_freespace(), byte_units, true);
247 snprintf(buffer, buffer_len, "%s %s / %s", str(LANG_SKIN_RAM_USAGE), s1, s2);
248 break;
250 return buffer;
253 static int info_speak_item(int selected_item, void * data)
255 struct info_data *info = (struct info_data*)data;
257 switch (selected_item)
259 case INFO_VERSION: /* version */
260 talk_id(LANG_VERSION, false);
261 talk_spell(appsversion, true);
262 break;
264 case INFO_BUFFER: /* buffer */
266 talk_id(LANG_BUFFER_STAT, false);
267 long kib = (audiobufend - audiobuf) / 1024; /* to KiB */
268 output_dyn_value(NULL, 0, kib, kbyte_units, true);
269 break;
271 case INFO_BATTERY: /* battery */
272 #if CONFIG_CHARGING == CHARGING_SIMPLE
273 /* Only know if plugged */
274 if (charger_inserted())
275 talk_id(LANG_BATTERY_CHARGE, true);
276 else
277 #elif CONFIG_CHARGING >= CHARGING_MONITOR
278 #ifdef ARCHOS_RECORDER
279 /* Report the particular algorithm state */
280 if (charge_state == CHARGING)
281 talk_id(LANG_BATTERY_CHARGE, true);
282 else if (charge_state == TOPOFF)
283 talk_id(LANG_BATTERY_TOPOFF_CHARGE, true);
284 else if (charge_state == TRICKLE)
285 talk_id(LANG_BATTERY_TRICKLE_CHARGE, true);
286 else
287 #else /* !ARCHOS_RECORDER */
288 /* Go by what power management reports */
289 if (charging_state())
290 talk_id(LANG_BATTERY_CHARGE, true);
291 else
292 #endif /* ARCHOS_RECORDER */
293 #endif /* CONFIG_CHARGING = */
294 if (battery_level() >= 0)
296 talk_id(LANG_BATTERY_TIME, false);
297 talk_value(battery_level(), UNIT_PERCENT, true);
298 talk_value(battery_time() *60, UNIT_TIME, true);
300 else talk_id(VOICE_BLANK, false);
301 break;
302 case INFO_DISK1: /* disk 1 */
303 #ifdef HAVE_MULTIVOLUME
304 talk_ids(false, LANG_DISK_NAME_INTERNAL, LANG_DISK_FREE_INFO);
305 output_dyn_value(NULL, 0, info->free, kbyte_units, true);
306 talk_id(LANG_DISK_SIZE_INFO, true);
307 output_dyn_value(NULL, 0, info->size, kbyte_units, true);
308 #else
309 talk_id(LANG_DISK_FREE_INFO, false);
310 output_dyn_value(NULL, 0, info->free, kbyte_units, true);
311 #endif
312 break;
313 case INFO_DISK2: /* disk 2 */
314 #ifdef HAVE_MULTIVOLUME
315 talk_id(LANG_DISK_NAME_MMC, false);
316 if (info->size2)
318 talk_id(LANG_DISK_FREE_INFO, true);
319 output_dyn_value(NULL, 0, info->free2, kbyte_units, true);
320 talk_id(LANG_DISK_SIZE_INFO, true);
321 output_dyn_value(NULL, 0, info->size2, kbyte_units, true);
323 else talk_id(LANG_NOT_PRESENT, true);
324 #else
325 talk_id(LANG_DISK_SIZE_INFO, false);
326 output_dyn_value(NULL, 0, info->size, kbyte_units, true);
327 #endif
328 break;
329 case INFO_SKIN_USAGE:
330 talk_id(LANG_SKIN_RAM_USAGE, false);
331 output_dyn_value(NULL, 0, skin_buffer_usage(), byte_units, true);
332 break;
335 return 0;
338 static int info_action_callback(int action, struct gui_synclist *lists)
340 if (action == ACTION_STD_CANCEL)
341 return action;
342 else if ((action == ACTION_STD_OK)
343 #ifdef HAVE_MULTIVOLUME
344 || action == SYS_HOTSWAP_INSERTED
345 || action == SYS_HOTSWAP_EXTRACTED
346 #endif
349 #ifndef SIMULATOR
350 struct info_data *info = (struct info_data *)lists->data;
351 info->new_data = true;
352 splash(0, ID2P(LANG_SCANNING_DISK));
353 fat_recalc_free(IF_MV(0));
354 #ifdef HAVE_MULTIVOLUME
355 if (fat_ismounted(1))
356 fat_recalc_free(1);
357 #endif
359 #else
361 (void) lists;
362 #endif
363 gui_synclist_speak_item(lists);
364 return ACTION_REDRAW;
366 return action;
368 static bool show_info(void)
370 struct info_data data = {.new_data = true };
371 struct simplelist_info info;
372 simplelist_info_init(&info, str(LANG_ROCKBOX_INFO), INFO_COUNT, (void*)&data);
373 info.hide_selection = !global_settings.talk_menu;
374 if (info.hide_selection)
375 info.scroll_all = true;
376 info.get_name = info_getname;
377 if(global_settings.talk_menu)
378 info.get_talk = info_speak_item;
379 info.action_callback = info_action_callback;
380 return simplelist_show_list(&info);
382 MENUITEM_FUNCTION(show_info_item, 0, ID2P(LANG_ROCKBOX_INFO),
383 (menu_function)show_info, NULL, NULL, Icon_NOICON);
386 /* sleep Menu */
387 static const char* sleep_timer_formatter(char* buffer, size_t buffer_size,
388 int value, const char* unit)
390 (void) unit;
391 int minutes, hours;
393 if (value) {
394 hours = value / 60;
395 minutes = value - (hours * 60);
396 snprintf(buffer, buffer_size, "%d:%02d", hours, minutes);
397 return buffer;
398 } else {
399 return str(LANG_OFF);
403 static void sleep_timer_set(int minutes)
405 set_sleep_timer(minutes * 60);
408 static int sleep_timer(void)
410 int minutes = (get_sleep_timer() + 59) / 60; /* round up */
411 return (int)set_int(str(LANG_SLEEP_TIMER), "", UNIT_MIN, &minutes,
412 &sleep_timer_set, -5, 300, 0, sleep_timer_formatter);
416 #if CONFIG_RTC
417 int time_screen(void* ignored);
418 MENUITEM_FUNCTION(timedate_item, MENU_FUNC_CHECK_RETVAL, ID2P(LANG_TIME_MENU), time_screen,
419 NULL, NULL, Icon_Menu_setting );
420 #endif
421 /* This item is in the time/date screen if there is a RTC */
422 MENUITEM_FUNCTION(sleep_timer_call, 0, ID2P(LANG_SLEEP_TIMER), sleep_timer,
423 NULL, NULL, Icon_Menu_setting); /* make it look like a
424 setting to the user */
426 MENUITEM_FUNCTION(show_credits_item, 0, ID2P(LANG_CREDITS),
427 (menu_function)show_credits, NULL, NULL, Icon_NOICON);
428 MENUITEM_FUNCTION(show_runtime_item, 0, ID2P(LANG_RUNNING_TIME),
429 (menu_function)view_runtime, NULL, NULL, Icon_NOICON);
430 MENUITEM_FUNCTION(debug_menu_item, 0, ID2P(LANG_DEBUG),
431 (menu_function)debug_menu, NULL, NULL, Icon_NOICON);
433 MAKE_MENU(info_menu, ID2P(LANG_SYSTEM), 0, Icon_System_menu,
434 #if CONFIG_RTC
435 &timedate_item,
436 #endif
437 &show_info_item, &show_credits_item, &show_runtime_item,
438 #if CONFIG_RTC == 0
439 &sleep_timer_call,
440 #endif
441 &debug_menu_item);
442 /* INFO MENU */
443 /***********************************/
445 /***********************************/
446 /* MAIN MENU */
449 #ifdef HAVE_LCD_CHARCELLS
450 static int mainmenu_callback(int action,const struct menu_item_ex *this_item)
452 (void)this_item;
453 switch (action)
455 case ACTION_ENTER_MENUITEM:
456 status_set_param(true);
457 break;
458 case ACTION_EXIT_MENUITEM:
459 status_set_param(false);
460 break;
462 return action;
464 #else
465 #define mainmenu_callback NULL
466 #endif
467 MAKE_MENU(main_menu_, ID2P(LANG_SETTINGS), mainmenu_callback,
468 Icon_Submenu_Entered,
469 &sound_settings,
470 &playback_settings,
471 &settings_menu_item, &theme_menu,
472 #ifdef HAVE_RECORDING
473 &recording_settings,
474 #endif
475 &manage_settings,
477 /* MAIN MENU */
478 /***********************************/