Make sure 'Battery: Charging' is displayed in the info screen when charging no matter...
[kugel-rb.git] / apps / menus / main_menu.c
blob598373116e30eeb087648f46acec603a9b73a398
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 "bookmark.h"
40 #include "yesno.h"
41 #include "keyboard.h"
42 #include "screens.h"
43 #include "plugin.h"
44 #include "talk.h"
45 #include "buffer.h"
46 #include "splash.h"
47 #include "debug_menu.h"
48 #if defined(SIMULATOR) && defined(ROCKBOX_HAS_LOGF)
49 #include "logfdisp.h"
50 #endif
51 #include "version.h"
52 #include "time.h"
53 #include "gwps.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 #ifdef HAVE_ALBUMART
149 INFO_ALBUMART,
150 #endif
151 INFO_VERSION,
152 INFO_COUNT
155 static const unsigned char *kbyte_units[] =
157 ID2P(LANG_KILOBYTE),
158 ID2P(LANG_MEGABYTE),
159 ID2P(LANG_GIGABYTE)
162 static char* info_getname(int selected_item, void *data,
163 char *buffer, size_t buffer_len)
165 struct info_data *info = (struct info_data*)data;
166 char s1[32];
167 #ifdef HAVE_MULTIVOLUME
168 char s2[32];
169 #endif
170 if (info->new_data)
172 fat_size(IF_MV2(0,) &info->size, &info->free);
173 #ifdef HAVE_MULTIVOLUME
174 if (fat_ismounted(1))
175 fat_size(1, &info->size2, &info->free2);
176 else
177 info->size2 = 0;
178 #endif
179 info->new_data = false;
181 switch (selected_item)
183 case INFO_VERSION:
184 snprintf(buffer, buffer_len, "%s: %s",
185 str(LANG_VERSION), appsversion);
186 break;
188 case INFO_BUFFER: /* buffer */
190 long kib = (audiobufend - audiobuf) / 1024; /* to KiB */
191 output_dyn_value(s1, sizeof(s1), kib, kbyte_units, true);
192 snprintf(buffer, buffer_len, "%s %s", str(LANG_BUFFER_STAT), s1);
194 break;
195 case INFO_BATTERY: /* battery */
196 #if CONFIG_CHARGING == CHARGING_SIMPLE
197 /* Only know if plugged */
198 if (charger_inserted())
199 return (char *)str(LANG_BATTERY_CHARGE);
200 else
201 #elif CONFIG_CHARGING >= CHARGING_MONITOR
202 #ifdef ARCHOS_RECORDER
203 /* Report the particular algorithm state */
204 if (charge_state == CHARGING)
205 return (char *)str(LANG_BATTERY_CHARGE);
206 else if (charge_state == TOPOFF)
207 return (char *)str(LANG_BATTERY_TOPOFF_CHARGE);
208 else if (charge_state == TRICKLE)
209 return (char *)str(LANG_BATTERY_TRICKLE_CHARGE);
210 else
211 #else /* !ARCHOS_RECORDER */
212 /* Go by what power management reports */
213 if (charging_state())
214 return (char *)str(LANG_BATTERY_CHARGE);
215 else
216 #endif /* ARCHOS_RECORDER */
217 #endif /* CONFIG_CHARGING = */
218 if (battery_level() >= 0)
219 snprintf(buffer, buffer_len, (char *)str(LANG_BATTERY_TIME),
220 battery_level(), battery_time() / 60, battery_time() % 60);
221 else
222 return "(n/a)";
223 break;
224 case INFO_DISK1: /* disk usage 1 */
225 #ifdef HAVE_MULTIVOLUME
226 output_dyn_value(s1, sizeof s1, info->free, kbyte_units, true);
227 output_dyn_value(s2, sizeof s2, info->size, kbyte_units, true);
228 snprintf(buffer, buffer_len, "%s %s/%s", str(LANG_DISK_NAME_INTERNAL),
229 s1, s2);
230 #else
231 output_dyn_value(s1, sizeof s1, info->free, kbyte_units, true);
232 snprintf(buffer, buffer_len, SIZE_FMT, str(LANG_DISK_FREE_INFO), s1);
233 #endif
234 break;
235 case INFO_DISK2: /* disk usage 2 */
236 #ifdef HAVE_MULTIVOLUME
237 if (info->size2)
239 output_dyn_value(s1, sizeof s1, info->free2, kbyte_units, true);
240 output_dyn_value(s2, sizeof s2, info->size2, kbyte_units, true);
241 snprintf(buffer, buffer_len, "%s %s/%s", str(LANG_DISK_NAME_MMC),
242 s1, s2);
244 else
246 snprintf(buffer, buffer_len, "%s %s", str(LANG_DISK_NAME_MMC),
247 str(LANG_NOT_PRESENT));
249 #else
250 output_dyn_value(s1, sizeof s1, info->size, kbyte_units, true);
251 snprintf(buffer, buffer_len, SIZE_FMT, str(LANG_DISK_SIZE_INFO), s1);
252 #endif
253 break;
254 #ifdef HAVE_ALBUMART
255 case INFO_ALBUMART: /* album art dimenstions */
256 if (gui_sync_wps_uses_albumart())
258 snprintf(buffer, buffer_len, "%s %dx%d", str(LANG_ALBUMART),
259 gui_wps[0].data->albumart_max_width,
260 gui_wps[0].data->albumart_max_height);
262 else
264 snprintf(buffer, buffer_len, "%s %s", str(LANG_ALBUMART),
265 str(LANG_SET_BOOL_NO));
267 #endif
269 return buffer;
272 static int info_speak_item(int selected_item, void * data)
274 struct info_data *info = (struct info_data*)data;
276 switch (selected_item)
278 case INFO_VERSION: /* version */
279 talk_id(LANG_VERSION, false);
280 talk_spell(appsversion, true);
281 break;
283 case INFO_BUFFER: /* buffer */
285 talk_id(LANG_BUFFER_STAT, false);
286 long kib = (audiobufend - audiobuf) / 1024; /* to KiB */
287 output_dyn_value(NULL, 0, kib, kbyte_units, true);
288 break;
290 case INFO_BATTERY: /* battery */
291 #if CONFIG_CHARGING == CHARGING_SIMPLE
292 /* Only know if plugged */
293 if (charger_inserted())
294 talk_id(LANG_BATTERY_CHARGE, true);
295 else
296 #elif CONFIG_CHARGING >= CHARGING_MONITOR
297 #ifdef ARCHOS_RECORDER
298 /* Report the particular algorithm state */
299 if (charge_state == CHARGING)
300 talk_id(LANG_BATTERY_CHARGE, true);
301 else if (charge_state == TOPOFF)
302 talk_id(LANG_BATTERY_TOPOFF_CHARGE, true);
303 else if (charge_state == TRICKLE)
304 talk_id(LANG_BATTERY_TRICKLE_CHARGE, true);
305 else
306 #else /* !ARCHOS_RECORDER */
307 /* Go by what power management reports */
308 if (charging_state())
309 talk_id(LANG_BATTERY_CHARGE, true);
310 else
311 #endif /* ARCHOS_RECORDER */
312 #endif /* CONFIG_CHARGING = */
313 if (battery_level() >= 0)
315 talk_id(LANG_BATTERY_TIME, false);
316 talk_value(battery_level(), UNIT_PERCENT, true);
317 talk_value(battery_time() *60, UNIT_TIME, true);
319 else talk_id(VOICE_BLANK, false);
320 break;
321 case INFO_DISK1: /* disk 1 */
322 #ifdef HAVE_MULTIVOLUME
323 talk_id(LANG_DISK_NAME_INTERNAL, false);
324 talk_id(LANG_DISK_FREE_INFO, true);
325 output_dyn_value(NULL, 0, info->free, kbyte_units, true);
326 talk_id(LANG_DISK_SIZE_INFO, true);
327 output_dyn_value(NULL, 0, info->size, kbyte_units, true);
328 #else
329 talk_id(LANG_DISK_FREE_INFO, false);
330 output_dyn_value(NULL, 0, info->free, kbyte_units, true);
331 #endif
332 break;
333 case INFO_DISK2: /* disk 2 */
334 #ifdef HAVE_MULTIVOLUME
335 talk_id(LANG_DISK_NAME_MMC, false);
336 if (info->size2)
338 talk_id(LANG_DISK_FREE_INFO, true);
339 output_dyn_value(NULL, 0, info->free2, kbyte_units, true);
340 talk_id(LANG_DISK_SIZE_INFO, true);
341 output_dyn_value(NULL, 0, info->size2, kbyte_units, true);
343 else talk_id(LANG_NOT_PRESENT, true);
344 #else
345 talk_id(LANG_DISK_SIZE_INFO, false);
346 output_dyn_value(NULL, 0, info->size, kbyte_units, true);
347 #endif
348 break;
349 #ifdef HAVE_ALBUMART
350 case INFO_ALBUMART: /* album art dimenstions */
351 if (gui_sync_wps_uses_albumart())
353 talk_id(LANG_ALBUMART, false);
354 talk_value(gui_wps[0].data->albumart_max_width, UNIT_PIXEL, true);
355 talk_value(gui_wps[0].data->albumart_max_height, UNIT_PIXEL, true);
357 else
359 talk_id(LANG_ALBUMART, false);
360 talk_id(LANG_SET_BOOL_NO, true);
362 #endif
364 return 0;
367 static int info_action_callback(int action, struct gui_synclist *lists)
369 #if CONFIG_CHARGING
370 if (action == ACTION_NONE)
371 return ACTION_REDRAW;
372 else
373 #endif
374 if (action == ACTION_STD_CANCEL)
375 return action;
376 else if ((action == ACTION_STD_OK)
377 #ifdef HAVE_MULTIVOLUME
378 || action == SYS_HOTSWAP_INSERTED
379 || action == SYS_HOTSWAP_EXTRACTED
380 #endif
383 #ifndef SIMULATOR
384 struct info_data *info = (struct info_data *)lists->data;
385 info->new_data = true;
386 splash(0, ID2P(LANG_SCANNING_DISK));
387 fat_recalc_free(IF_MV(0));
388 #ifdef HAVE_MULTIVOLUME
389 if (fat_ismounted(1))
390 fat_recalc_free(1);
391 #endif
393 #else
395 (void) lists;
396 #endif
397 gui_synclist_speak_item(lists);
398 return ACTION_REDRAW;
400 return action;
402 static bool show_info(void)
404 struct info_data data = {.new_data = true };
405 struct simplelist_info info;
406 simplelist_info_init(&info, str(LANG_ROCKBOX_INFO), INFO_COUNT, (void*)&data);
407 info.hide_selection = !global_settings.talk_menu;
408 if (info.hide_selection)
409 info.scroll_all = true;
410 info.get_name = info_getname;
411 if(global_settings.talk_menu)
412 info.get_talk = info_speak_item;
413 info.action_callback = info_action_callback;
414 #if CONFIG_CHARGING
415 info.timeout = HZ;
416 #endif
417 return simplelist_show_list(&info);
419 MENUITEM_FUNCTION(show_info_item, 0, ID2P(LANG_ROCKBOX_INFO),
420 (menu_function)show_info, NULL, NULL, Icon_NOICON);
423 /* sleep Menu */
424 static void sleep_timer_formatter(char* buffer, size_t buffer_size, int value,
425 const char* unit)
427 int minutes, hours;
429 (void) unit;
431 if (value) {
432 hours = value / 60;
433 minutes = value - (hours * 60);
434 snprintf(buffer, buffer_size, "%d:%02d", hours, minutes);
435 } else {
436 strncpy(buffer, str(LANG_OFF), buffer_size);
440 static void sleep_timer_set(int minutes)
442 set_sleep_timer(minutes * 60);
445 static int sleep_timer(void)
447 int minutes = (get_sleep_timer() + 59) / 60; /* round up */
448 return (int)set_int(str(LANG_SLEEP_TIMER), "", UNIT_MIN, &minutes,
449 &sleep_timer_set, -5, 300, 0, sleep_timer_formatter);
453 #if CONFIG_RTC
454 int time_screen(void* ignored);
455 MENUITEM_FUNCTION(timedate_item, MENU_FUNC_CHECK_RETVAL, ID2P(LANG_TIME_MENU), time_screen,
456 NULL, NULL, Icon_Menu_setting );
457 #endif
458 /* This item is in the time/date screen if there is a RTC */
459 MENUITEM_FUNCTION(sleep_timer_call, 0, ID2P(LANG_SLEEP_TIMER), sleep_timer,
460 NULL, NULL, Icon_Menu_setting); /* make it look like a
461 setting to the user */
463 MENUITEM_FUNCTION(show_credits_item, 0, ID2P(LANG_CREDITS),
464 (menu_function)show_credits, NULL, NULL, Icon_NOICON);
465 MENUITEM_FUNCTION(show_runtime_item, 0, ID2P(LANG_RUNNING_TIME),
466 (menu_function)view_runtime, NULL, NULL, Icon_NOICON);
467 MENUITEM_FUNCTION(debug_menu_item, 0, ID2P(LANG_DEBUG),
468 (menu_function)debug_menu, NULL, NULL, Icon_NOICON);
470 MAKE_MENU(info_menu, ID2P(LANG_SYSTEM), 0, Icon_Questionmark,
471 #if CONFIG_RTC
472 &timedate_item,
473 #endif
474 &show_info_item, &show_credits_item, &show_runtime_item,
475 #if CONFIG_RTC == 0
476 &sleep_timer_call,
477 #endif
478 &debug_menu_item);
479 /* INFO MENU */
480 /***********************************/
482 /***********************************/
483 /* MAIN MENU */
486 #ifdef HAVE_LCD_CHARCELLS
487 static int mainmenu_callback(int action,const struct menu_item_ex *this_item)
489 (void)this_item;
490 switch (action)
492 case ACTION_ENTER_MENUITEM:
493 status_set_param(true);
494 break;
495 case ACTION_EXIT_MENUITEM:
496 status_set_param(false);
497 break;
499 return action;
501 #else
502 #define mainmenu_callback NULL
503 #endif
504 MAKE_MENU(main_menu_, ID2P(LANG_SETTINGS), mainmenu_callback,
505 Icon_Submenu_Entered,
506 &sound_settings,
507 &playback_settings,
508 &settings_menu_item, &theme_menu,
509 #ifdef HAVE_RECORDING
510 &recording_settings,
511 #endif
512 &manage_settings,
514 /* MAIN MENU */
515 /***********************************/