Use buflib for all skin engine allocations.
[maemo-rb.git] / apps / menus / main_menu.c
blob8053bf0cf8f357c8c2a9e9bc2baa5d7706f994b8
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 #include "storage.h"
37 #ifdef HAVE_RECORDING
38 #include "recording.h"
39 #endif
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 #include "version.h"
49 #include "time.h"
50 #include "wps.h"
51 #include "skin_buffer.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_save();
78 settings_apply(true);
79 settings_apply_skins();
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 */
114 static bool show_credits(void)
116 char credits[MAX_PATH] = { '\0' };
117 snprintf(credits, MAX_PATH, "%s/credits.rock", VIEWERS_DIR);
118 if (plugin_load(credits, NULL) != PLUGIN_OK)
120 /* show the rockbox logo and version untill a button is pressed */
121 show_logo();
122 while (IS_SYSEVENT(get_action(CONTEXT_STD, TIMEOUT_BLOCK)))
125 return false;
128 #ifdef HAVE_LCD_CHARCELLS
129 #define SIZE_FMT "%s%s"
130 #else
131 #define SIZE_FMT "%s %s"
132 #endif
133 struct info_data
136 bool new_data;
137 unsigned long size;
138 unsigned long free;
139 #ifdef HAVE_MULTIVOLUME
140 unsigned long size2;
141 unsigned long free2;
142 #endif
144 enum infoscreenorder
146 INFO_BATTERY = 0,
147 INFO_DISK1, /* capacity or internal capacity/free on hotswap */
148 INFO_DISK2, /* free space or external capacity/free on hotswap */
149 INFO_BUFFER,
150 INFO_VERSION,
151 INFO_COUNT
154 static const char* info_getname(int selected_item, void *data,
155 char *buffer, size_t buffer_len)
157 struct info_data *info = (struct info_data*)data;
158 char s1[32];
159 #if defined(HAVE_MULTIVOLUME)
160 char s2[32];
161 #endif
162 if (info->new_data)
164 fat_size(IF_MV2(0,) &info->size, &info->free);
165 #ifdef HAVE_MULTIVOLUME
166 if (fat_ismounted(1))
167 fat_size(1, &info->size2, &info->free2);
168 else
169 info->size2 = 0;
170 #endif
171 info->new_data = false;
173 switch (selected_item)
175 case INFO_VERSION:
176 snprintf(buffer, buffer_len, "%s: %s",
177 str(LANG_VERSION), rbversion);
178 break;
180 case INFO_BUFFER: /* buffer */
182 long kib = audio_buffer_available() / 1024; /* to KiB */
183 output_dyn_value(s1, sizeof(s1), kib, kbyte_units, true);
184 snprintf(buffer, buffer_len, "%s %s", str(LANG_BUFFER_STAT), s1);
186 break;
187 case INFO_BATTERY: /* battery */
188 #if CONFIG_CHARGING == CHARGING_SIMPLE
189 /* Only know if plugged */
190 if (charger_inserted())
191 return str(LANG_BATTERY_CHARGE);
192 else
193 #elif CONFIG_CHARGING >= CHARGING_MONITOR
194 #ifdef ARCHOS_RECORDER
195 /* Report the particular algorithm state */
196 if (charge_state == CHARGING)
197 return str(LANG_BATTERY_CHARGE);
198 else if (charge_state == TOPOFF)
199 return str(LANG_BATTERY_TOPOFF_CHARGE);
200 else if (charge_state == TRICKLE)
201 return str(LANG_BATTERY_TRICKLE_CHARGE);
202 else
203 #else /* !ARCHOS_RECORDER */
204 /* Go by what power management reports */
205 if (charging_state())
206 return str(LANG_BATTERY_CHARGE);
207 else
208 #endif /* ARCHOS_RECORDER */
209 #endif /* CONFIG_CHARGING = */
210 if (battery_level() >= 0)
211 snprintf(buffer, buffer_len, str(LANG_BATTERY_TIME),
212 battery_level(), battery_time() / 60, battery_time() % 60);
213 else
214 return "(n/a)";
215 break;
216 case INFO_DISK1: /* disk usage 1 */
217 #ifdef HAVE_MULTIVOLUME
218 output_dyn_value(s1, sizeof s1, info->free, kbyte_units, true);
219 output_dyn_value(s2, sizeof s2, info->size, kbyte_units, true);
220 snprintf(buffer, buffer_len, "%s %s/%s", str(LANG_DISK_NAME_INTERNAL),
221 s1, s2);
222 #else
223 output_dyn_value(s1, sizeof s1, info->free, kbyte_units, true);
224 snprintf(buffer, buffer_len, SIZE_FMT, str(LANG_DISK_FREE_INFO), s1);
225 #endif
226 break;
227 case INFO_DISK2: /* disk usage 2 */
228 #ifdef HAVE_MULTIVOLUME
229 if (info->size2)
231 output_dyn_value(s1, sizeof s1, info->free2, kbyte_units, true);
232 output_dyn_value(s2, sizeof s2, info->size2, kbyte_units, true);
233 snprintf(buffer, buffer_len, "%s %s/%s", str(LANG_DISK_NAME_MMC),
234 s1, s2);
236 else
238 snprintf(buffer, buffer_len, "%s %s", str(LANG_DISK_NAME_MMC),
239 str(LANG_NOT_PRESENT));
241 #else
242 output_dyn_value(s1, sizeof s1, info->size, kbyte_units, true);
243 snprintf(buffer, buffer_len, SIZE_FMT, str(LANG_DISK_SIZE_INFO), s1);
244 #endif
245 break;
247 return buffer;
250 static int info_speak_item(int selected_item, void * data)
252 struct info_data *info = (struct info_data*)data;
254 switch (selected_item)
256 case INFO_VERSION: /* version */
257 talk_id(LANG_VERSION, false);
258 talk_spell(rbversion, true);
259 break;
261 case INFO_BUFFER: /* buffer */
263 talk_id(LANG_BUFFER_STAT, false);
264 long kib = audio_buffer_available() / 1024; /* to KiB */
265 output_dyn_value(NULL, 0, kib, kbyte_units, true);
266 break;
268 case INFO_BATTERY: /* battery */
269 #if CONFIG_CHARGING == CHARGING_SIMPLE
270 /* Only know if plugged */
271 if (charger_inserted())
272 talk_id(LANG_BATTERY_CHARGE, true);
273 else
274 #elif CONFIG_CHARGING >= CHARGING_MONITOR
275 #ifdef ARCHOS_RECORDER
276 /* Report the particular algorithm state */
277 if (charge_state == CHARGING)
278 talk_id(LANG_BATTERY_CHARGE, true);
279 else if (charge_state == TOPOFF)
280 talk_id(LANG_BATTERY_TOPOFF_CHARGE, true);
281 else if (charge_state == TRICKLE)
282 talk_id(LANG_BATTERY_TRICKLE_CHARGE, true);
283 else
284 #else /* !ARCHOS_RECORDER */
285 /* Go by what power management reports */
286 if (charging_state())
287 talk_id(LANG_BATTERY_CHARGE, true);
288 else
289 #endif /* ARCHOS_RECORDER */
290 #endif /* CONFIG_CHARGING = */
291 if (battery_level() >= 0)
293 talk_id(LANG_BATTERY_TIME, false);
294 talk_value(battery_level(), UNIT_PERCENT, true);
295 talk_value(battery_time() *60, UNIT_TIME, true);
297 else talk_id(VOICE_BLANK, false);
298 break;
299 case INFO_DISK1: /* disk 1 */
300 #ifdef HAVE_MULTIVOLUME
301 talk_ids(false, LANG_DISK_NAME_INTERNAL, LANG_DISK_FREE_INFO);
302 output_dyn_value(NULL, 0, info->free, kbyte_units, true);
303 talk_id(LANG_DISK_SIZE_INFO, true);
304 output_dyn_value(NULL, 0, info->size, kbyte_units, true);
305 #else
306 talk_id(LANG_DISK_FREE_INFO, false);
307 output_dyn_value(NULL, 0, info->free, kbyte_units, true);
308 #endif
309 break;
310 case INFO_DISK2: /* disk 2 */
311 #ifdef HAVE_MULTIVOLUME
312 talk_id(LANG_DISK_NAME_MMC, false);
313 if (info->size2)
315 talk_id(LANG_DISK_FREE_INFO, true);
316 output_dyn_value(NULL, 0, info->free2, kbyte_units, true);
317 talk_id(LANG_DISK_SIZE_INFO, true);
318 output_dyn_value(NULL, 0, info->size2, kbyte_units, true);
320 else talk_id(LANG_NOT_PRESENT, true);
321 #else
322 talk_id(LANG_DISK_SIZE_INFO, false);
323 output_dyn_value(NULL, 0, info->size, kbyte_units, true);
324 #endif
325 break;
328 return 0;
331 static int info_action_callback(int action, struct gui_synclist *lists)
333 if (action == ACTION_STD_CANCEL)
334 return action;
335 else if ((action == ACTION_STD_OK)
336 #ifdef HAVE_HOTSWAP
337 || action == SYS_FS_CHANGED
338 #endif
341 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
342 struct info_data *info = (struct info_data *)lists->data;
343 int i;
344 info->new_data = true;
345 splash(0, ID2P(LANG_SCANNING_DISK));
346 for (i = 0; i < NUM_VOLUMES; i++)
348 #ifdef HAVE_HOTSWAP
349 if (fat_ismounted(i))
350 #endif
351 fat_recalc_free(IF_MV(i));
353 #else
354 (void) lists;
355 #endif
356 gui_synclist_speak_item(lists);
357 return ACTION_REDRAW;
359 return action;
361 static bool show_info(void)
363 struct info_data data = {.new_data = true };
364 struct simplelist_info info;
365 simplelist_info_init(&info, str(LANG_ROCKBOX_INFO), INFO_COUNT, (void*)&data);
366 info.hide_selection = !global_settings.talk_menu;
367 if (info.hide_selection)
368 info.scroll_all = true;
369 info.get_name = info_getname;
370 if(global_settings.talk_menu)
371 info.get_talk = info_speak_item;
372 info.action_callback = info_action_callback;
373 return simplelist_show_list(&info);
375 MENUITEM_FUNCTION(show_info_item, 0, ID2P(LANG_ROCKBOX_INFO),
376 (menu_function)show_info, NULL, NULL, Icon_NOICON);
379 /* sleep Menu */
380 static const char* sleep_timer_formatter(char* buffer, size_t buffer_size,
381 int value, const char* unit)
383 (void) unit;
384 int minutes, hours;
386 if (value) {
387 hours = value / 60;
388 minutes = value - (hours * 60);
389 snprintf(buffer, buffer_size, "%d:%02d", hours, minutes);
390 return buffer;
391 } else {
392 return str(LANG_OFF);
396 static void sleep_timer_set(int minutes)
398 if (minutes)
399 global_settings.sleeptimer_duration = minutes;
400 set_sleep_timer(minutes * 60);
403 static int sleep_timer(void)
405 int minutes = global_settings.sleeptimer_duration;
406 if (get_sleep_timer())
407 sleep_timer_set(0);
408 else
409 set_int(str(LANG_SLEEP_TIMER), "", UNIT_MIN, &minutes,
410 &sleep_timer_set, 5, 0, 300, sleep_timer_formatter);
411 return 0;
414 static int seconds_to_min(int secs)
416 return (secs + 10) / 60; /* round up for 50+ seconds */
419 static char* sleep_timer_getname(int selected_item, void * data, char *buffer)
421 (void)selected_item;
422 (void)data;
423 int sec = get_sleep_timer();
424 char timer_buf[10];
425 /* we have no sprintf, so MAX_PATH is a guess */
426 if (sec > 0)
427 { /* show cancel and countdown if running */
428 snprintf(buffer, MAX_PATH, "%s (%s)", str(LANG_SLEEP_TIMER_CANCEL_CURRENT),
429 sleep_timer_formatter(timer_buf, sizeof(timer_buf), seconds_to_min(sec), NULL));
431 else
432 snprintf(buffer, MAX_PATH, "%s", str(LANG_SLEEP_TIMER));
434 return buffer;
437 static int sleep_timer_voice(int selected_item, void*data)
439 (void)selected_item;
440 (void)data;
441 int seconds = get_sleep_timer();
442 if (seconds > 0)
444 long talk_ids[] = {
445 LANG_SLEEP_TIMER_CANCEL_CURRENT,
446 VOICE_PAUSE,
447 seconds_to_min(seconds) | UNIT_MIN << UNIT_SHIFT,
448 TALK_FINAL_ID
450 talk_idarray(talk_ids, true);
452 else
453 talk_id(LANG_SLEEP_TIMER, true);
454 return 0;
457 #if CONFIG_RTC
458 int time_screen(void* ignored);
459 MENUITEM_FUNCTION(timedate_item, MENU_FUNC_CHECK_RETVAL, ID2P(LANG_TIME_MENU),
460 time_screen, NULL, NULL, Icon_Menu_setting );
461 #endif
462 MENUITEM_FUNCTION_DYNTEXT(sleep_timer_call, 0, sleep_timer, NULL, sleep_timer_getname,
463 sleep_timer_voice, NULL, NULL, Icon_Menu_setting);
464 /* make it look like a setting to the user */
465 #if CONFIG_RTC == 0
466 MENUITEM_SETTING(sleeptimer_on_startup,
467 &global_settings.sleeptimer_on_startup, NULL);
468 #endif
470 MENUITEM_FUNCTION(show_credits_item, 0, ID2P(LANG_CREDITS),
471 (menu_function)show_credits, NULL, NULL, Icon_NOICON);
472 MENUITEM_FUNCTION(show_runtime_item, 0, ID2P(LANG_RUNNING_TIME),
473 (menu_function)view_runtime, NULL, NULL, Icon_NOICON);
474 MENUITEM_FUNCTION(debug_menu_item, 0, ID2P(LANG_DEBUG),
475 (menu_function)debug_menu, NULL, NULL, Icon_NOICON);
477 MAKE_MENU(info_menu, ID2P(LANG_SYSTEM), 0, Icon_System_menu,
478 &show_info_item, &show_credits_item,
479 &show_runtime_item, &debug_menu_item);
480 /* INFO MENU */
481 /***********************************/
483 /***********************************/
484 /* MAIN MENU */
487 #ifdef HAVE_LCD_CHARCELLS
488 static int mainmenu_callback(int action,const struct menu_item_ex *this_item)
490 (void)this_item;
491 switch (action)
493 case ACTION_ENTER_MENUITEM:
494 status_set_param(true);
495 break;
496 case ACTION_EXIT_MENUITEM:
497 status_set_param(false);
498 break;
500 return action;
502 #else
503 #define mainmenu_callback NULL
504 #endif
505 MAKE_MENU(main_menu_, ID2P(LANG_SETTINGS), mainmenu_callback,
506 Icon_Submenu_Entered,
507 &sound_settings,
508 &playback_settings,
509 &settings_menu_item, &theme_menu,
510 #ifdef HAVE_RECORDING
511 &recording_settings,
512 #endif
513 #if CONFIG_RTC
514 &timedate_item,
515 #else
516 &sleep_timer_call, &sleeptimer_on_startup,
517 #endif
518 &manage_settings,
520 /* MAIN MENU */
521 /***********************************/