Move language resources to a separate file. Only build and include langs when buildin...
[Rockbox.git] / apps / menus / main_menu.c
blob62250a3c4784be966eb96f926f57aca0a4c869ff
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Jonathan Gordon
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <stdbool.h>
21 #include <stddef.h>
22 #include <limits.h>
23 #include "config.h"
24 #include "string.h"
25 #include "lang.h"
26 #include "action.h"
27 #include "settings.h"
28 #include "powermgmt.h"
29 #include "menu.h"
30 #include "misc.h"
31 #include "exported_menus.h"
32 #include "tree.h"
33 #ifdef HAVE_RECORDING
34 #include "recording.h"
35 #endif
36 #include "bookmark.h"
37 #include "yesno.h"
38 #include "keyboard.h"
39 #include "screens.h"
40 #include "plugin.h"
41 #include "talk.h"
42 #include "buffer.h"
43 #include "splash.h"
44 #include "debug_menu.h"
45 #if defined(SIMULATOR) && defined(ROCKBOX_HAS_LOGF)
46 #include "logfdisp.h"
47 #endif
48 #include "version.h"
49 #include "time.h"
51 static const struct browse_folder_info config = {ROCKBOX_DIR, SHOW_CFG};
53 /***********************************/
54 /* MANAGE SETTINGS MENU */
56 static int reset_settings(void)
58 static const char *lines[]={ID2P(LANG_RESET_ASK)};
59 static const char *yes_lines[]={
60 ID2P(LANG_SETTINGS),
61 ID2P(LANG_RESET_DONE_CLEAR)
63 static const char *no_lines[]={
64 ID2P(LANG_SETTINGS),
65 ID2P(LANG_CANCEL)
67 static const struct text_message message={lines, 1};
68 static const struct text_message yes_message={yes_lines, 2};
69 static const struct text_message no_message={no_lines, 2};
71 switch(gui_syncyesno_run(&message, &yes_message, &no_message))
73 case YESNO_YES:
74 settings_reset();
75 settings_apply(true);
76 settings_save();
77 break;
78 case YESNO_NO:
79 break;
80 case YESNO_USB:
81 return 1;
83 return 0;
85 static int write_settings_file(void* param)
87 return settings_save_config((intptr_t)param);
90 MENUITEM_FUNCTION(browse_configs, MENU_FUNC_USEPARAM, ID2P(LANG_CUSTOM_CFG),
91 browse_folder, (void*)&config, NULL, Icon_NOICON);
92 MENUITEM_FUNCTION(save_settings_item, MENU_FUNC_USEPARAM, ID2P(LANG_SAVE_SETTINGS),
93 write_settings_file, (void*)SETTINGS_SAVE_ALL, NULL, Icon_NOICON);
94 MENUITEM_FUNCTION(save_theme_item, MENU_FUNC_USEPARAM, ID2P(LANG_SAVE_THEME),
95 write_settings_file, (void*)SETTINGS_SAVE_THEME, NULL, Icon_NOICON);
96 MENUITEM_FUNCTION(save_sound_item, MENU_FUNC_USEPARAM, ID2P(LANG_SAVE_SOUND),
97 write_settings_file, (void*)SETTINGS_SAVE_SOUND, NULL, Icon_NOICON);
98 MENUITEM_FUNCTION(reset_settings_item, 0, ID2P(LANG_RESET),
99 reset_settings, NULL, NULL, Icon_NOICON);
101 MAKE_MENU(manage_settings, ID2P(LANG_MANAGE_MENU), NULL, Icon_Config,
102 &browse_configs, &reset_settings_item,
103 &save_settings_item, &save_sound_item, &save_theme_item);
104 /* MANAGE SETTINGS MENU */
105 /**********************************/
107 /***********************************/
108 /* INFO MENU */
110 static bool show_credits(void)
112 if (plugin_load(VIEWERS_DIR "/credits.rock",NULL) != PLUGIN_OK)
114 /* show the rockbox logo and version untill a button is pressed */
115 show_logo();
116 get_action(CONTEXT_STD, TIMEOUT_BLOCK);
118 return false;
121 #ifdef HAVE_LCD_CHARCELLS
122 #define SIZE_FMT "%s%s"
123 #else
124 #define SIZE_FMT "%s %s"
125 #endif
126 struct info_data
129 bool new_data;
130 unsigned long size;
131 unsigned long free;
132 #ifdef HAVE_MULTIVOLUME
133 unsigned long size2;
134 unsigned long free2;
135 #endif
137 enum infoscreenorder
139 INFO_BATTERY = 0,
140 INFO_DISK1, /* capacity or internal capacity/free on hotswap */
141 INFO_DISK2, /* free space or external capacity/free on hotswap */
142 INFO_BUFFER,
143 INFO_VERSION,
144 #if CONFIG_RTC
145 INFO_DATE,
146 INFO_TIME,
147 #endif
148 INFO_COUNT
151 static const unsigned char *kbyte_units[] =
153 ID2P(LANG_KILOBYTE),
154 ID2P(LANG_MEGABYTE),
155 ID2P(LANG_GIGABYTE)
158 static char* info_getname(int selected_item, void *data,
159 char *buffer, size_t buffer_len)
161 struct info_data *info = (struct info_data*)data;
162 #if CONFIG_RTC
163 struct tm *tm;
164 #endif
165 char s1[32];
166 #ifdef HAVE_MULTIVOLUME
167 char s2[32];
168 #endif
169 if (info->new_data)
171 fat_size(IF_MV2(0,) &info->size, &info->free);
172 #ifdef HAVE_MULTIVOLUME
173 if (fat_ismounted(1))
174 fat_size(1, &info->size2, &info->free2);
175 else
176 info->size2 = 0;
177 #endif
178 info->new_data = false;
180 switch (selected_item)
182 case INFO_VERSION:
183 snprintf(buffer, buffer_len, "%s: %s",
184 str(LANG_VERSION), appsversion);
185 break;
186 #if CONFIG_RTC
187 case INFO_TIME:
188 tm = get_time();
189 if (valid_time(tm))
191 snprintf(buffer, buffer_len, "%02d:%02d:%02d %s",
192 global_settings.timeformat == 0 ? tm->tm_hour :
193 ((tm->tm_hour + 11) % 12) + 1,
194 tm->tm_min,
195 tm->tm_sec,
196 global_settings.timeformat == 0 ? "" :
197 tm->tm_hour>11 ? "P" : "A");
199 else
201 snprintf(buffer, buffer_len, "%s", "--:--:--");
203 break;
204 case INFO_DATE:
205 tm = get_time();
206 if (valid_time(tm))
208 snprintf(buffer, buffer_len, "%s %d %d",
209 str(LANG_MONTH_JANUARY + tm->tm_mon),
210 tm->tm_mday,
211 tm->tm_year+1900);
213 else
215 snprintf(buffer, buffer_len, "%s", str(LANG_UNKNOWN));
217 break;
218 #endif
219 case INFO_BUFFER: /* buffer */
221 long buflen = ((audiobufend - audiobuf) * 2) / 2097; /* avoid overflow */
222 int integer = buflen / 1000;
223 int decimal = buflen % 1000;
224 snprintf(buffer, buffer_len, (char *)str(LANG_BUFFER_STAT),
225 integer, decimal);
227 break;
228 case INFO_BATTERY: /* battery */
229 #if CONFIG_CHARGING == CHARGING_SIMPLE
230 if (charger_input_state == CHARGER)
231 return (char *)str(LANG_BATTERY_CHARGE);
232 else
233 #elif CONFIG_CHARGING >= CHARGING_MONITOR
234 if (charge_state == CHARGING)
235 return (char *)str(LANG_BATTERY_CHARGE);
236 else
237 #if CONFIG_CHARGING == CHARGING_CONTROL
238 if (charge_state == TOPOFF)
239 return (char *)str(LANG_BATTERY_TOPOFF_CHARGE);
240 else
241 #endif
242 if (charge_state == TRICKLE)
243 return (char *)str(LANG_BATTERY_TRICKLE_CHARGE);
244 else
245 #endif
246 if (battery_level() >= 0)
247 snprintf(buffer, buffer_len, (char *)str(LANG_BATTERY_TIME),
248 battery_level(), battery_time() / 60, battery_time() % 60);
249 else
250 return "(n/a)";
251 break;
252 case INFO_DISK1: /* disk usage 1 */
253 #ifdef HAVE_MULTIVOLUME
254 output_dyn_value(s1, sizeof s1, info->free, kbyte_units, true);
255 output_dyn_value(s2, sizeof s2, info->size, kbyte_units, true);
256 snprintf(buffer, buffer_len, "%s %s/%s", str(LANG_DISK_NAME_INTERNAL),
257 s1, s2);
258 #else
259 output_dyn_value(s1, sizeof s1, info->free, kbyte_units, true);
260 snprintf(buffer, buffer_len, SIZE_FMT, str(LANG_DISK_FREE_INFO), s1);
261 #endif
262 break;
263 case INFO_DISK2: /* disk usage 2 */
264 #ifdef HAVE_MULTIVOLUME
265 if (info->size2)
267 output_dyn_value(s1, sizeof s1, info->free2, kbyte_units, true);
268 output_dyn_value(s2, sizeof s2, info->size2, kbyte_units, true);
269 snprintf(buffer, buffer_len, "%s %s/%s", str(LANG_DISK_NAME_MMC),
270 s1, s2);
272 else
274 snprintf(buffer, buffer_len, "%s %s", str(LANG_DISK_NAME_MMC),
275 str(LANG_NOT_PRESENT));
277 #else
278 output_dyn_value(s1, sizeof s1, info->size, kbyte_units, true);
279 snprintf(buffer, buffer_len, SIZE_FMT, str(LANG_DISK_SIZE_INFO), s1);
280 #endif
281 break;
283 return buffer;
286 static int info_speak_item(int selected_item, void * data)
288 struct info_data *info = (struct info_data*)data;
290 #if CONFIG_RTC
291 struct tm *tm;
292 #endif
294 switch (selected_item)
296 case INFO_VERSION: /* version */
297 talk_id(LANG_VERSION, false);
298 talk_spell(appsversion, true);
299 break;
300 #if CONFIG_RTC
301 case INFO_TIME:
302 tm = get_time();
303 talk_id(VOICE_CURRENT_TIME, false);
304 if (valid_time(tm))
306 talk_time(tm, true);
308 else
310 talk_id(LANG_UNKNOWN, true);
312 break;
313 case INFO_DATE:
314 tm = get_time();
315 if (valid_time(tm))
317 talk_date(get_time(), true);
319 else
321 talk_id(LANG_UNKNOWN, true);
323 break;
324 #endif
325 case INFO_BUFFER: /* buffer */
327 talk_id(LANG_BUFFER_STAT, false);
328 long buflen = ((audiobufend - audiobuf) * 2) / 2097; /* avoid overflow */
329 output_dyn_value(NULL, 0, buflen, kbyte_units, true);
330 break;
332 case INFO_BATTERY: /* battery */
333 #if CONFIG_CHARGING == CHARGING_SIMPLE
334 if (charger_input_state == CHARGER)
335 talk_id(LANG_BATTERY_CHARGE, true);
336 else
337 #elif CONFIG_CHARGING >= CHARGING_MONITOR
338 if (charge_state == CHARGING)
339 talk_id(LANG_BATTERY_CHARGE, true);
340 else
341 #if CONFIG_CHARGING == CHARGING_CONTROL
342 if (charge_state == TOPOFF)
343 talk_id(LANG_BATTERY_TOPOFF_CHARGE, true);
344 else
345 #endif
346 if (charge_state == TRICKLE)
347 talk_id(LANG_BATTERY_TRICKLE_CHARGE, true);
348 else
349 #endif
350 if (battery_level() >= 0)
352 talk_id(LANG_BATTERY_TIME, false);
353 talk_value(battery_level(), UNIT_PERCENT, true);
354 talk_value(battery_time() *60, UNIT_TIME, true);
356 else talk_id(VOICE_BLANK, false);
357 break;
358 case INFO_DISK1: /* disk 1 */
359 #ifdef HAVE_MULTIVOLUME
360 talk_id(LANG_DISK_NAME_INTERNAL, false);
361 talk_id(LANG_DISK_FREE_INFO, true);
362 output_dyn_value(NULL, 0, info->free, kbyte_units, true);
363 talk_id(LANG_DISK_SIZE_INFO, true);
364 output_dyn_value(NULL, 0, info->size, kbyte_units, true);
365 #else
366 talk_id(LANG_DISK_FREE_INFO, false);
367 output_dyn_value(NULL, 0, info->free, kbyte_units, true);
368 #endif
369 break;
370 case INFO_DISK2: /* disk 2 */
371 #ifdef HAVE_MULTIVOLUME
372 talk_id(LANG_DISK_NAME_MMC, false);
373 if (info->size2)
375 talk_id(LANG_DISK_FREE_INFO, true);
376 output_dyn_value(NULL, 0, info->free2, kbyte_units, true);
377 talk_id(LANG_DISK_SIZE_INFO, true);
378 output_dyn_value(NULL, 0, info->size2, kbyte_units, true);
380 else talk_id(LANG_NOT_PRESENT, true);
381 #else
382 talk_id(LANG_DISK_SIZE_INFO, false);
383 output_dyn_value(NULL, 0, info->size, kbyte_units, true);
384 #endif
385 break;
387 return 0;
390 static int info_action_callback(int action, struct gui_synclist *lists)
392 if (action == ACTION_STD_CANCEL)
393 return action;
394 if ((action == ACTION_STD_OK)
395 #ifdef HAVE_MULTIVOLUME
396 || action == SYS_HOTSWAP_INSERTED
397 || action == SYS_HOTSWAP_EXTRACTED
398 #endif
401 #ifndef SIMULATOR
402 struct info_data *info = (struct info_data *)lists->data;
403 info->new_data = true;
404 gui_syncsplash(0, ID2P(LANG_SCANNING_DISK));
405 fat_recalc_free(IF_MV(0));
406 #ifdef HAVE_MULTIVOLUME
407 if (fat_ismounted(1))
408 fat_recalc_free(1);
409 #endif
411 #else
413 (void) lists;
414 #endif
415 return ACTION_REDRAW;
417 return action;
419 static bool show_info(void)
421 struct info_data data = {.new_data = true };
422 struct simplelist_info info;
423 simplelist_info_init(&info, str(LANG_ROCKBOX_INFO), INFO_COUNT, (void*)&data);
424 info.hide_selection = !global_settings.talk_menu;
425 if (info.hide_selection)
426 info.scroll_all = true;
427 info.get_name = info_getname;
428 if(global_settings.talk_menu)
429 info.get_talk = info_speak_item;
430 info.action_callback = info_action_callback;
431 return simplelist_show_list(&info);
433 MENUITEM_FUNCTION(show_info_item, 0, ID2P(LANG_ROCKBOX_INFO),
434 (menu_function)show_info, NULL, NULL, Icon_NOICON);
437 /* sleep Menu */
438 static void sleep_timer_formatter(char* buffer, size_t buffer_size, int value,
439 const char* unit)
441 int minutes, hours;
443 (void) unit;
445 if (value) {
446 hours = value / 60;
447 minutes = value - (hours * 60);
448 snprintf(buffer, buffer_size, "%d:%02d", hours, minutes);
449 } else {
450 snprintf(buffer, buffer_size, "%s", str(LANG_OFF));
454 static void sleep_timer_set(int minutes)
456 set_sleep_timer(minutes * 60);
459 static int sleep_timer(void)
461 int minutes = (get_sleep_timer() + 59) / 60; /* round up */
462 return (int)set_int(str(LANG_SLEEP_TIMER), "", UNIT_MIN, &minutes,
463 &sleep_timer_set, -5, 300, 0, sleep_timer_formatter);
466 MENUITEM_FUNCTION(sleep_timer_call, 0, ID2P(LANG_SLEEP_TIMER), sleep_timer,
467 NULL, NULL, Icon_Menu_setting); /* make it look like a
468 setting to the user */
469 MENUITEM_FUNCTION(show_credits_item, 0, ID2P(LANG_VERSION),
470 (menu_function)show_credits, NULL, NULL, Icon_NOICON);
471 MENUITEM_FUNCTION(show_runtime_item, 0, ID2P(LANG_RUNNING_TIME),
472 (menu_function)view_runtime, NULL, NULL, Icon_NOICON);
473 MENUITEM_FUNCTION(debug_menu_item, 0, ID2P(LANG_DEBUG),
474 (menu_function)debug_menu, NULL, NULL, Icon_NOICON);
476 MAKE_MENU(info_menu, ID2P(LANG_SYSTEM), 0, Icon_Questionmark,
477 &show_info_item, &show_credits_item, &show_runtime_item,
478 &sleep_timer_call, &debug_menu_item);
479 /* INFO MENU */
480 /***********************************/
482 /***********************************/
483 /* MAIN MENU */
486 #ifdef HAVE_LCD_CHARCELLS
487 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 &settings_menu_item, &theme_menu,
508 #ifdef HAVE_RECORDING
509 &recording_settings,
510 #endif
511 &manage_settings,
513 /* MAIN MENU */
514 /***********************************/