Add a get_offset API call to make codec_advance_buffer_loc_callback work.
[Rockbox.git] / apps / menus / main_menu.c
blob52311812969073722d6bf0bc22decd17799f11f2
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();
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(reset_settings_item, 0, ID2P(LANG_RESET),
94 reset_settings, NULL, NULL, Icon_NOICON);
96 MAKE_MENU(manage_settings, ID2P(LANG_MANAGE_MENU), NULL, Icon_Config,
97 &browse_configs, &reset_settings_item,
98 &save_settings_item, &save_theme_item);
99 /* MANAGE SETTINGS MENU */
100 /**********************************/
102 /***********************************/
103 /* INFO MENU */
105 static bool show_credits(void)
107 if (plugin_load(VIEWERS_DIR "/credits.rock",NULL) != PLUGIN_OK)
109 /* show the rockbox logo and version untill a button is pressed */
110 show_logo();
111 get_action(CONTEXT_STD, TIMEOUT_BLOCK);
113 return false;
116 #ifdef HAVE_LCD_CHARCELLS
117 #define SIZE_FMT "%s%s"
118 #else
119 #define SIZE_FMT "%s %s"
120 #endif
121 struct info_data
123 bool new_data;
124 unsigned long size;
125 unsigned long free;
126 #ifdef HAVE_MULTIVOLUME
127 unsigned long size2;
128 unsigned long free2;
129 #endif
131 enum infoscreenorder
133 INFO_VERSION = 0,
134 #if CONFIG_RTC
135 INFO_TIME,
136 INFO_DATE,
137 #endif
138 INFO_BUFFER,
139 INFO_BATTERY,
140 INFO_DISK1, /* capacity or internal capacity/free on hotswap */
141 INFO_DISK2, /* free space or external capacity/free on hotswap */
142 INFO_COUNT
145 static char* info_getname(int selected_item, void *data, char *buffer)
147 struct info_data *info = (struct info_data*)data;
148 #if CONFIG_RTC
149 struct tm *tm;
150 #endif
151 const unsigned char *kbyte_units[] =
153 ID2P(LANG_KILOBYTE),
154 ID2P(LANG_MEGABYTE),
155 ID2P(LANG_GIGABYTE)
157 char s1[32];
158 #ifdef HAVE_MULTIVOLUME
159 char s2[32];
160 #endif
161 if (info->new_data)
163 fat_size(IF_MV2(0,) &info->size, &info->free);
164 #ifdef HAVE_MULTIVOLUME
165 if (fat_ismounted(1))
166 fat_size(1, &info->size2, &info->free2);
167 else
168 info->size2 = 0;
169 #endif
170 info->new_data = false;
172 switch (selected_item)
174 case INFO_VERSION:
175 snprintf(buffer, MAX_PATH, "%s: %s",
176 str(LANG_VERSION), appsversion);
177 break;
178 #if CONFIG_RTC
179 case INFO_TIME:
180 tm = get_time();
181 snprintf(buffer, MAX_PATH, "%02d:%02d:%02d %s",
182 global_settings.timeformat == 0 ? tm->tm_hour : tm->tm_hour-12,
183 tm->tm_min,
184 tm->tm_sec,
185 global_settings.timeformat == 0 ? "" : tm->tm_hour>11 ? "P" : "A");
186 break;
187 case INFO_DATE:
188 tm = get_time();
189 snprintf(buffer, MAX_PATH, "%s %d %d",
190 str(LANG_MONTH_JANUARY + tm->tm_mon),
191 tm->tm_mday,
192 tm->tm_year+1900);
193 break;
194 #endif
195 case INFO_BUFFER: /* buffer */
197 long buflen = ((audiobufend - audiobuf) * 2) / 2097; /* avoid overflow */
198 int integer = buflen / 1000;
199 int decimal = buflen % 1000;
200 snprintf(buffer, MAX_PATH, (char *)str(LANG_BUFFER_STAT),
201 integer, decimal);
203 break;
204 case INFO_BATTERY: /* battery */
205 #if CONFIG_CHARGING == CHARGING_CONTROL
206 if (charge_state == CHARGING)
207 snprintf(buffer, MAX_PATH, (char *)str(LANG_BATTERY_CHARGE));
208 else if (charge_state == TOPOFF)
209 snprintf(buffer, MAX_PATH, (char *)str(LANG_BATTERY_TOPOFF_CHARGE));
210 else if (charge_state == TRICKLE)
211 snprintf(buffer, MAX_PATH, (char *)str(LANG_BATTERY_TRICKLE_CHARGE));
212 else
213 #endif
214 if (battery_level() >= 0)
215 snprintf(buffer, MAX_PATH, (char *)str(LANG_BATTERY_TIME), battery_level(),
216 battery_time() / 60, battery_time() % 60);
217 else
218 strcpy(buffer, "(n/a)");
219 break;
220 case INFO_DISK1: /* disk usage 1 */
221 #ifdef HAVE_MULTIVOLUME
222 output_dyn_value(s1, sizeof s1, info->free, kbyte_units, true);
223 output_dyn_value(s2, sizeof s2, info->size, kbyte_units, true);
224 snprintf(buffer, MAX_PATH, "%s %s/%s", str(LANG_DISK_NAME_INTERNAL),
225 s1, s2);
226 #else
227 output_dyn_value(s1, sizeof s1, info->size, kbyte_units, true);
228 snprintf(buffer, MAX_PATH, SIZE_FMT, str(LANG_DISK_SIZE_INFO), s1);
229 #endif
230 break;
231 case INFO_DISK2: /* disk usage 2 */
232 #ifdef HAVE_MULTIVOLUME
233 if (info->size2)
235 output_dyn_value(s1, sizeof s1, info->free2, kbyte_units, true);
236 output_dyn_value(s2, sizeof s2, info->size2, kbyte_units, true);
237 snprintf(buffer, MAX_PATH, "%s %s/%s", str(LANG_DISK_NAME_MMC),
238 s1, s2);
240 else
241 return "";
242 #else
243 output_dyn_value(s1, sizeof s1, info->free, kbyte_units, true);
244 snprintf(buffer, MAX_PATH, SIZE_FMT, str(LANG_DISK_FREE_INFO), s1);
245 #endif
246 break;
248 return buffer;
250 static int info_speak_item(int selected_item, void * data)
252 struct info_data *info = (struct info_data*)data;
253 const unsigned char *kbyte_units[] = {
254 ID2P(LANG_KILOBYTE),
255 ID2P(LANG_MEGABYTE),
256 ID2P(LANG_GIGABYTE)
258 switch (selected_item)
260 case INFO_VERSION: /* version */
261 talk_id(LANG_VERSION, false);
262 talk_spell(appsversion, true);
263 break;
264 #if CONFIG_RTC
265 case INFO_TIME:
266 talk_id(LANG_CURRENT_TIME, false);
267 talk_time(get_time(), true);
268 break;
269 case INFO_DATE:
270 talk_date(get_time(), true);
271 break;
272 #endif
273 case INFO_BUFFER: /* buffer */
275 talk_id(LANG_BUFFER_STAT, false);
276 long buflen = ((audiobufend - audiobuf) * 2) / 2097; /* avoid overflow */
277 output_dyn_value(NULL, 0, buflen, kbyte_units, true);
278 break;
280 case INFO_BATTERY: /* battery */
281 if (battery_level() >= 0)
283 talk_id(LANG_BATTERY_TIME, false);
284 talk_value(battery_level(), UNIT_PERCENT, true);
285 if(battery_time () / 60 > 0)
286 talk_value(battery_time () / 60, UNIT_INT, true);
287 if(battery_time () % 60 > 0)
288 talk_value(battery_time () % 60, UNIT_INT, true);
289 #if CONFIG_CHARGING >= CHARGING_MONITOR
290 if (charge_state == CHARGING)
291 talk_id(LANG_BATTERY_CHARGE, true);
292 #if CONFIG_CHARGING == CHARGING_CONTROL
293 else if (charge_state == TOPOFF)
294 talk_id(LANG_BATTERY_TOPOFF_CHARGE, true);
295 #endif
296 else if (charge_state == TRICKLE)
297 talk_id(LANG_BATTERY_TRICKLE_CHARGE, true);
298 #endif
300 break;
301 case INFO_DISK1: /* disk 1 */
302 #ifdef HAVE_MULTIVOLUME
303 talk_id(LANG_DISK_FREE_INFO, false);
304 talk_id(LANG_DISK_NAME_INTERNAL, true);
305 output_dyn_value(NULL, 0, info->free, kbyte_units, true);
306 #else
307 talk_id(LANG_DISK_SIZE_INFO, false);
308 output_dyn_value(NULL, 0, info->size, kbyte_units, true);
309 #endif
310 break;
311 case INFO_DISK2: /* disk 2 */
312 #ifdef HAVE_MULTIVOLUME
313 if (info->size2)
315 talk_id(LANG_DISK_FREE_INFO, false);
316 talk_id(LANG_DISK_NAME_MMC, false);
317 output_dyn_value(NULL, 0, info->free2, kbyte_units, true);
319 #else
320 talk_id(LANG_DISK_FREE_INFO, false);
321 output_dyn_value(NULL, 0, info->free, kbyte_units, true);
322 #endif
323 break;
325 return 0;
328 static int info_action_callback(int action, struct gui_synclist *lists)
330 if (action == ACTION_STD_CANCEL)
331 return action;
332 if ((action == ACTION_STD_OK)
333 #ifdef HAVE_MULTIVOLUME
334 || action == SYS_HOTSWAP_INSERTED
335 || action == SYS_HOTSWAP_EXTRACTED
336 #endif
339 #ifndef SIMULATOR
340 struct info_data *info = (struct info_data *)lists->gui_list[SCREEN_MAIN].data;
341 info->new_data = true;
342 gui_syncsplash(0, ID2P(LANG_SCANNING_DISK));
343 fat_recalc_free(IF_MV(0));
344 #ifdef HAVE_MULTIVOLUME
345 if (fat_ismounted(1))
346 fat_recalc_free(1);
347 #endif
348 #else
349 (void) lists;
350 #endif
351 return ACTION_REDRAW;
353 return action;
355 static bool show_info(void)
357 struct info_data data = {.new_data = true };
358 struct simplelist_info info;
359 simplelist_info_init(&info, str(LANG_ROCKBOX_INFO), INFO_COUNT, (void*)&data);
360 info.hide_selection = !global_settings.talk_menu;
361 info.get_name = info_getname;
362 if(global_settings.talk_menu)
363 info.get_talk = info_speak_item;
364 info.action_callback = info_action_callback;
365 return simplelist_show_list(&info);
367 MENUITEM_FUNCTION(show_info_item, 0, ID2P(LANG_ROCKBOX_INFO),
368 (menu_function)show_info, NULL, NULL, Icon_NOICON);
371 /* sleep Menu */
372 static void sleep_timer_formatter(char* buffer, size_t buffer_size, int value,
373 const char* unit)
375 int minutes, hours;
377 (void) unit;
379 if (value) {
380 hours = value / 60;
381 minutes = value - (hours * 60);
382 snprintf(buffer, buffer_size, "%d:%02d", hours, minutes);
383 } else {
384 snprintf(buffer, buffer_size, "%s", str(LANG_OFF));
388 static void sleep_timer_set(int minutes)
390 set_sleep_timer(minutes * 60);
393 static int sleep_timer(void)
395 int minutes = (get_sleep_timer() + 59) / 60; /* round up */
396 return (int)set_int(str(LANG_SLEEP_TIMER), "", UNIT_MIN, &minutes,
397 &sleep_timer_set, -5, 300, 0, sleep_timer_formatter);
400 MENUITEM_FUNCTION(sleep_timer_call, 0, ID2P(LANG_SLEEP_TIMER), sleep_timer,
401 NULL, NULL, Icon_Menu_setting); /* make it look like a
402 setting to the user */
403 MENUITEM_FUNCTION(show_credits_item, 0, ID2P(LANG_VERSION),
404 (menu_function)show_credits, NULL, NULL, Icon_NOICON);
405 MENUITEM_FUNCTION(show_runtime_item, 0, ID2P(LANG_RUNNING_TIME),
406 (menu_function)view_runtime, NULL, NULL, Icon_NOICON);
407 MENUITEM_FUNCTION(debug_menu_item, 0, ID2P(LANG_DEBUG),
408 (menu_function)debug_menu, NULL, NULL, Icon_NOICON);
410 MAKE_MENU(info_menu, ID2P(LANG_SYSTEM), 0, Icon_Questionmark,
411 &show_info_item, &show_credits_item, &show_runtime_item,
412 &sleep_timer_call, &debug_menu_item);
413 /* INFO MENU */
414 /***********************************/
416 /***********************************/
417 /* MAIN MENU */
420 #ifdef HAVE_LCD_CHARCELLS
421 int mainmenu_callback(int action,const struct menu_item_ex *this_item)
423 (void)this_item;
424 switch (action)
426 case ACTION_ENTER_MENUITEM:
427 status_set_param(true);
428 break;
429 case ACTION_EXIT_MENUITEM:
430 status_set_param(false);
431 break;
433 return action;
435 #else
436 #define mainmenu_callback NULL
437 #endif
438 MAKE_MENU(main_menu_, ID2P(LANG_SETTINGS), mainmenu_callback,
439 Icon_Submenu_Entered,
440 &sound_settings,
441 &settings_menu_item, &theme_menu,
442 #ifdef HAVE_RECORDING
443 &recording_settings,
444 #endif
445 &manage_settings,
447 /* MAIN MENU */
448 /***********************************/