grr.. typo
[Rockbox.git] / apps / menus / main_menu.c
blob39f722f2c609cc3488f35f2faf50098155d3c17f
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 const unsigned char *lines[]={ID2P(LANG_RESET_ASK)};
59 const unsigned char *yes_lines[]={
60 str(LANG_SETTINGS),
61 ID2P(LANG_RESET_DONE_CLEAR)
63 const unsigned char *no_lines[]={yes_lines[0], ID2P(LANG_CANCEL)};
64 struct text_message message={(char **)lines, 1};
65 struct text_message yes_message={(char **)yes_lines, 2};
66 struct text_message no_message={(char **)no_lines, 2};
68 switch(gui_syncyesno_run(&message, &yes_message, &no_message))
70 case YESNO_YES:
71 settings_reset();
72 settings_apply(true);
73 settings_save();
74 break;
75 case YESNO_NO:
76 break;
77 case YESNO_USB:
78 return 1;
80 return 0;
82 static int write_settings_file(void* param)
84 return settings_save_config((intptr_t)param);
87 MENUITEM_FUNCTION(browse_configs, MENU_FUNC_USEPARAM, ID2P(LANG_CUSTOM_CFG),
88 browse_folder, (void*)&config, NULL, Icon_NOICON);
89 MENUITEM_FUNCTION(save_settings_item, MENU_FUNC_USEPARAM, ID2P(LANG_SAVE_SETTINGS),
90 write_settings_file, (void*)SETTINGS_SAVE_ALL, NULL, Icon_NOICON);
91 MENUITEM_FUNCTION(save_theme_item, MENU_FUNC_USEPARAM, ID2P(LANG_SAVE_THEME),
92 write_settings_file, (void*)SETTINGS_SAVE_THEME, NULL, Icon_NOICON);
93 MENUITEM_FUNCTION(save_sound_item, MENU_FUNC_USEPARAM, ID2P(LANG_SAVE_SOUND),
94 write_settings_file, (void*)SETTINGS_SAVE_SOUND, NULL, Icon_NOICON);
95 MENUITEM_FUNCTION(reset_settings_item, 0, ID2P(LANG_RESET),
96 reset_settings, NULL, NULL, Icon_NOICON);
98 MAKE_MENU(manage_settings, ID2P(LANG_MANAGE_MENU), NULL, Icon_Config,
99 &browse_configs, &reset_settings_item,
100 &save_settings_item, &save_sound_item, &save_theme_item);
101 /* MANAGE SETTINGS MENU */
102 /**********************************/
104 /***********************************/
105 /* INFO MENU */
107 static bool show_credits(void)
109 if (plugin_load(VIEWERS_DIR "/credits.rock",NULL) != PLUGIN_OK)
111 /* show the rockbox logo and version untill a button is pressed */
112 show_logo();
113 get_action(CONTEXT_STD, TIMEOUT_BLOCK);
115 return false;
118 #ifdef HAVE_LCD_CHARCELLS
119 #define SIZE_FMT "%s%s"
120 #else
121 #define SIZE_FMT "%s %s"
122 #endif
123 struct info_data
126 bool new_data;
127 unsigned long size;
128 unsigned long free;
129 #ifdef HAVE_MULTIVOLUME
130 unsigned long size2;
131 unsigned long free2;
132 #endif
134 enum infoscreenorder
136 INFO_BATTERY = 0,
137 INFO_DISK1, /* capacity or internal capacity/free on hotswap */
138 INFO_DISK2, /* free space or external capacity/free on hotswap */
139 INFO_BUFFER,
140 INFO_VERSION,
141 #if CONFIG_RTC
142 INFO_DATE,
143 INFO_TIME,
144 #endif
145 INFO_COUNT
148 static const unsigned char *kbyte_units[] =
150 ID2P(LANG_KILOBYTE),
151 ID2P(LANG_MEGABYTE),
152 ID2P(LANG_GIGABYTE)
155 static char* info_getname(int selected_item, void *data,
156 char *buffer, size_t buffer_len)
158 struct info_data *info = (struct info_data*)data;
159 #if CONFIG_RTC
160 struct tm *tm;
161 #endif
162 char s1[32];
163 #ifdef HAVE_MULTIVOLUME
164 char s2[32];
165 #endif
166 if (info->new_data)
168 fat_size(IF_MV2(0,) &info->size, &info->free);
169 #ifdef HAVE_MULTIVOLUME
170 if (fat_ismounted(1))
171 fat_size(1, &info->size2, &info->free2);
172 else
173 info->size2 = 0;
174 #endif
175 info->new_data = false;
177 switch (selected_item)
179 case INFO_VERSION:
180 snprintf(buffer, buffer_len, "%s: %s",
181 str(LANG_VERSION), appsversion);
182 break;
183 #if CONFIG_RTC
184 case INFO_TIME:
185 tm = get_time();
186 snprintf(buffer, buffer_len, "%02d:%02d:%02d %s",
187 global_settings.timeformat == 0 ? tm->tm_hour :
188 ((tm->tm_hour + 11) % 12) + 1,
189 tm->tm_min,
190 tm->tm_sec,
191 global_settings.timeformat == 0 ? "" :
192 tm->tm_hour>11 ? "P" : "A");
193 break;
194 case INFO_DATE:
195 tm = get_time();
196 snprintf(buffer, buffer_len, "%s %d %d",
197 str(LANG_MONTH_JANUARY + tm->tm_mon),
198 tm->tm_mday,
199 tm->tm_year+1900);
200 break;
201 #endif
202 case INFO_BUFFER: /* buffer */
204 long buflen = ((audiobufend - audiobuf) * 2) / 2097; /* avoid overflow */
205 int integer = buflen / 1000;
206 int decimal = buflen % 1000;
207 snprintf(buffer, buffer_len, (char *)str(LANG_BUFFER_STAT),
208 integer, decimal);
210 break;
211 case INFO_BATTERY: /* battery */
212 #if CONFIG_CHARGING == CHARGING_SIMPLE
213 if (charger_input_state == CHARGER)
214 return (char *)str(LANG_BATTERY_CHARGE);
215 else
216 #elif CONFIG_CHARGING >= CHARGING_MONITOR
217 if (charge_state == CHARGING)
218 return (char *)str(LANG_BATTERY_CHARGE);
219 else
220 #if CONFIG_CHARGING == CHARGING_CONTROL
221 if (charge_state == TOPOFF)
222 return (char *)str(LANG_BATTERY_TOPOFF_CHARGE);
223 else
224 #endif
225 if (charge_state == TRICKLE)
226 return (char *)str(LANG_BATTERY_TRICKLE_CHARGE);
227 else
228 #endif
229 if (battery_level() >= 0)
230 snprintf(buffer, buffer_len, (char *)str(LANG_BATTERY_TIME),
231 battery_level(), battery_time() / 60, battery_time() % 60);
232 else
233 return "(n/a)";
234 break;
235 case INFO_DISK1: /* disk usage 1 */
236 #ifdef HAVE_MULTIVOLUME
237 output_dyn_value(s1, sizeof s1, info->free, kbyte_units, true);
238 output_dyn_value(s2, sizeof s2, info->size, kbyte_units, true);
239 snprintf(buffer, buffer_len, "%s %s/%s", str(LANG_DISK_NAME_INTERNAL),
240 s1, s2);
241 #else
242 output_dyn_value(s1, sizeof s1, info->free, kbyte_units, true);
243 snprintf(buffer, buffer_len, SIZE_FMT, str(LANG_DISK_FREE_INFO), s1);
244 #endif
245 break;
246 case INFO_DISK2: /* disk usage 2 */
247 #ifdef HAVE_MULTIVOLUME
248 if (info->size2)
250 output_dyn_value(s1, sizeof s1, info->free2, kbyte_units, true);
251 output_dyn_value(s2, sizeof s2, info->size2, kbyte_units, true);
252 snprintf(buffer, buffer_len, "%s %s/%s", str(LANG_DISK_NAME_MMC),
253 s1, s2);
255 else
257 snprintf(buffer, buffer_len, "%s %s", str(LANG_DISK_NAME_MMC),
258 str(LANG_NOT_PRESENT));
260 #else
261 output_dyn_value(s1, sizeof s1, info->size, kbyte_units, true);
262 snprintf(buffer, buffer_len, SIZE_FMT, str(LANG_DISK_SIZE_INFO), s1);
263 #endif
264 break;
266 return buffer;
269 static int info_speak_item(int selected_item, void * data)
271 struct info_data *info = (struct info_data*)data;
273 switch (selected_item)
275 case INFO_VERSION: /* version */
276 talk_id(LANG_VERSION, false);
277 talk_spell(appsversion, true);
278 break;
279 #if CONFIG_RTC
280 case INFO_TIME:
281 talk_id(VOICE_CURRENT_TIME, false);
282 talk_time(get_time(), true);
283 break;
284 case INFO_DATE:
285 talk_date(get_time(), true);
286 break;
287 #endif
288 case INFO_BUFFER: /* buffer */
290 talk_id(LANG_BUFFER_STAT, false);
291 long buflen = ((audiobufend - audiobuf) * 2) / 2097; /* avoid overflow */
292 output_dyn_value(NULL, 0, buflen, kbyte_units, true);
293 break;
295 case INFO_BATTERY: /* battery */
296 #if CONFIG_CHARGING == CHARGING_SIMPLE
297 if (charger_input_state == CHARGER)
298 talk_id(LANG_BATTERY_CHARGE, true);
299 else
300 #elif CONFIG_CHARGING >= CHARGING_MONITOR
301 if (charge_state == CHARGING)
302 talk_id(LANG_BATTERY_CHARGE, true);
303 else
304 #if CONFIG_CHARGING == CHARGING_CONTROL
305 if (charge_state == TOPOFF)
306 talk_id(LANG_BATTERY_TOPOFF_CHARGE, true);
307 else
308 #endif
309 if (charge_state == TRICKLE)
310 talk_id(LANG_BATTERY_TRICKLE_CHARGE, true);
311 else
312 #endif
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;
350 return 0;
353 static int info_action_callback(int action, struct gui_synclist *lists)
355 if (action == ACTION_STD_CANCEL)
356 return action;
357 if ((action == ACTION_STD_OK)
358 #ifdef HAVE_MULTIVOLUME
359 || action == SYS_HOTSWAP_INSERTED
360 || action == SYS_HOTSWAP_EXTRACTED
361 #endif
364 #ifndef SIMULATOR
365 struct info_data *info = (struct info_data *)lists->data;
366 info->new_data = true;
367 gui_syncsplash(0, ID2P(LANG_SCANNING_DISK));
368 fat_recalc_free(IF_MV(0));
369 #ifdef HAVE_MULTIVOLUME
370 if (fat_ismounted(1))
371 fat_recalc_free(1);
372 #endif
374 #else
376 (void) lists;
377 #endif
378 return ACTION_REDRAW;
380 return action;
382 static bool show_info(void)
384 struct info_data data = {.new_data = true };
385 struct simplelist_info info;
386 simplelist_info_init(&info, str(LANG_ROCKBOX_INFO), INFO_COUNT, (void*)&data);
387 info.hide_selection = !global_settings.talk_menu;
388 info.get_name = info_getname;
389 if(global_settings.talk_menu)
390 info.get_talk = info_speak_item;
391 info.action_callback = info_action_callback;
392 return simplelist_show_list(&info);
394 MENUITEM_FUNCTION(show_info_item, 0, ID2P(LANG_ROCKBOX_INFO),
395 (menu_function)show_info, NULL, NULL, Icon_NOICON);
398 /* sleep Menu */
399 static void sleep_timer_formatter(char* buffer, size_t buffer_size, int value,
400 const char* unit)
402 int minutes, hours;
404 (void) unit;
406 if (value) {
407 hours = value / 60;
408 minutes = value - (hours * 60);
409 snprintf(buffer, buffer_size, "%d:%02d", hours, minutes);
410 } else {
411 snprintf(buffer, buffer_size, "%s", str(LANG_OFF));
415 static void sleep_timer_set(int minutes)
417 set_sleep_timer(minutes * 60);
420 static int sleep_timer(void)
422 int minutes = (get_sleep_timer() + 59) / 60; /* round up */
423 return (int)set_int(str(LANG_SLEEP_TIMER), "", UNIT_MIN, &minutes,
424 &sleep_timer_set, -5, 300, 0, sleep_timer_formatter);
427 MENUITEM_FUNCTION(sleep_timer_call, 0, ID2P(LANG_SLEEP_TIMER), sleep_timer,
428 NULL, NULL, Icon_Menu_setting); /* make it look like a
429 setting to the user */
430 MENUITEM_FUNCTION(show_credits_item, 0, ID2P(LANG_VERSION),
431 (menu_function)show_credits, NULL, NULL, Icon_NOICON);
432 MENUITEM_FUNCTION(show_runtime_item, 0, ID2P(LANG_RUNNING_TIME),
433 (menu_function)view_runtime, NULL, NULL, Icon_NOICON);
434 MENUITEM_FUNCTION(debug_menu_item, 0, ID2P(LANG_DEBUG),
435 (menu_function)debug_menu, NULL, NULL, Icon_NOICON);
437 MAKE_MENU(info_menu, ID2P(LANG_SYSTEM), 0, Icon_Questionmark,
438 &show_info_item, &show_credits_item, &show_runtime_item,
439 &sleep_timer_call, &debug_menu_item);
440 /* INFO MENU */
441 /***********************************/
443 /***********************************/
444 /* MAIN MENU */
447 #ifdef HAVE_LCD_CHARCELLS
448 int mainmenu_callback(int action,const struct menu_item_ex *this_item)
450 (void)this_item;
451 switch (action)
453 case ACTION_ENTER_MENUITEM:
454 status_set_param(true);
455 break;
456 case ACTION_EXIT_MENUITEM:
457 status_set_param(false);
458 break;
460 return action;
462 #else
463 #define mainmenu_callback NULL
464 #endif
465 MAKE_MENU(main_menu_, ID2P(LANG_SETTINGS), mainmenu_callback,
466 Icon_Submenu_Entered,
467 &sound_settings,
468 &settings_menu_item, &theme_menu,
469 #ifdef HAVE_RECORDING
470 &recording_settings,
471 #endif
472 &manage_settings,
474 /* MAIN MENU */
475 /***********************************/