Make sd_present() and mmc_present() look only at the actual presence of a card, not...
[kugel-rb.git] / apps / menus / eq_menu.c
blob6087ebfd5be1a726b7472cfe01c8f9ec85eab036
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: eq_menu.c 12179 2007-02-01 23:08:15Z amiconn $
10 * Copyright (C) 2006 Dan Everton
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 "config.h"
23 #include <stdio.h>
24 #include <stdbool.h>
25 #include <string.h>
26 #include "eq_menu.h"
27 #include "system.h"
28 #include "kernel.h"
29 #include "lcd.h"
30 #include "menu.h"
31 #include "action.h"
32 #include "mp3_playback.h"
33 #include "settings.h"
34 #include "screens.h"
35 #include "icons.h"
36 #include "font.h"
37 #include "lang.h"
38 #include "sprintf.h"
39 #include "talk.h"
40 #include "misc.h"
41 #include "sound.h"
42 #include "dsp.h"
43 #include "tree.h"
44 #include "screen_access.h"
45 #include "keyboard.h"
46 #include "gui/scrollbar.h"
47 #include "menu_common.h"
48 #include "viewport.h"
51 * Utility functions
54 void eq_q_format(char* buffer, size_t buffer_size, int value, const char* unit)
56 snprintf(buffer, buffer_size, "%d.%d %s", value / EQ_USER_DIVISOR, value % EQ_USER_DIVISOR, unit);
59 void eq_precut_format(char* buffer, size_t buffer_size, int value, const char* unit)
61 snprintf(buffer, buffer_size, "%s%d.%d %s", value == 0 ? " " : "-",
62 value / EQ_USER_DIVISOR, value % EQ_USER_DIVISOR, unit);
66 * Settings functions
68 static void eq_apply(void)
70 int i;
71 dsp_set_eq(global_settings.eq_enabled);
72 dsp_set_eq_precut(global_settings.eq_precut);
73 /* Update all bands */
74 for(i = 0; i < 5; i++) {
75 dsp_set_eq_coefs(i);
79 static int eq_setting_callback(int action, const struct menu_item_ex *this_item)
81 switch (action)
83 case ACTION_ENTER_MENUITEM:
84 action = lowlatency_callback(action, this_item);
85 break;
86 case ACTION_EXIT_MENUITEM:
87 eq_apply();
88 action = lowlatency_callback(action, this_item);
89 break;
92 return action;
94 MENUITEM_SETTING(eq_enable, &global_settings.eq_enabled, eq_setting_callback);
95 MENUITEM_SETTING(eq_precut, &global_settings.eq_precut, eq_setting_callback);
97 MENUITEM_SETTING(cutoff_0, &global_settings.eq_band0_cutoff, eq_setting_callback);
98 MENUITEM_SETTING(cutoff_1, &global_settings.eq_band1_cutoff, eq_setting_callback);
99 MENUITEM_SETTING(cutoff_2, &global_settings.eq_band2_cutoff, eq_setting_callback);
100 MENUITEM_SETTING(cutoff_3, &global_settings.eq_band3_cutoff, eq_setting_callback);
101 MENUITEM_SETTING(cutoff_4, &global_settings.eq_band4_cutoff, eq_setting_callback);
103 MENUITEM_SETTING(q_0, &global_settings.eq_band0_q, eq_setting_callback);
104 MENUITEM_SETTING(q_1, &global_settings.eq_band1_q, eq_setting_callback);
105 MENUITEM_SETTING(q_2, &global_settings.eq_band2_q, eq_setting_callback);
106 MENUITEM_SETTING(q_3, &global_settings.eq_band3_q, eq_setting_callback);
107 MENUITEM_SETTING(q_4, &global_settings.eq_band4_q, eq_setting_callback);
109 MENUITEM_SETTING(gain_0, &global_settings.eq_band0_gain, eq_setting_callback);
110 MENUITEM_SETTING(gain_1, &global_settings.eq_band1_gain, eq_setting_callback);
111 MENUITEM_SETTING(gain_2, &global_settings.eq_band2_gain, eq_setting_callback);
112 MENUITEM_SETTING(gain_3, &global_settings.eq_band3_gain, eq_setting_callback);
113 MENUITEM_SETTING(gain_4, &global_settings.eq_band4_gain, eq_setting_callback);
115 static char* gainitem_get_name(int selected_item, void * data, char *buffer)
117 (void)selected_item;
118 int *setting = (int*)data;
119 snprintf(buffer, MAX_PATH, str(LANG_EQUALIZER_GAIN_ITEM), *setting);
120 return buffer;
123 static int gainitem_speak_item(int selected_item, void * data)
125 (void)selected_item;
126 int *setting = (int*)data;
127 talk_number(*setting, false);
128 talk_id(LANG_EQUALIZER_GAIN_ITEM, true);
129 return 0;
132 static int do_option(void * param)
134 const struct menu_item_ex *setting = (const struct menu_item_ex*)param;
135 lowlatency_callback(ACTION_ENTER_MENUITEM, setting);
136 do_setting_from_menu(setting, NULL);
137 eq_apply();
138 lowlatency_callback(ACTION_EXIT_MENUITEM, setting);
139 return 0;
142 MENUITEM_FUNCTION_DYNTEXT(gain_item_0, MENU_FUNC_USEPARAM,
143 do_option, (void*)&gain_0,
144 gainitem_get_name, gainitem_speak_item,
145 &global_settings.eq_band0_cutoff, NULL, Icon_NOICON);
146 MENUITEM_FUNCTION_DYNTEXT(gain_item_1, MENU_FUNC_USEPARAM,
147 do_option, (void*)&gain_1,
148 gainitem_get_name, gainitem_speak_item,
149 &global_settings.eq_band1_cutoff, NULL, Icon_NOICON);
150 MENUITEM_FUNCTION_DYNTEXT(gain_item_2, MENU_FUNC_USEPARAM,
151 do_option, (void*)&gain_2,
152 gainitem_get_name, gainitem_speak_item,
153 &global_settings.eq_band2_cutoff, NULL, Icon_NOICON);
154 MENUITEM_FUNCTION_DYNTEXT(gain_item_3, MENU_FUNC_USEPARAM,
155 do_option, (void*)&gain_3,
156 gainitem_get_name, gainitem_speak_item,
157 &global_settings.eq_band3_cutoff, NULL, Icon_NOICON);
158 MENUITEM_FUNCTION_DYNTEXT(gain_item_4, MENU_FUNC_USEPARAM,
159 do_option, (void*)&gain_4,
160 gainitem_get_name, gainitem_speak_item,
161 &global_settings.eq_band4_cutoff, NULL, Icon_NOICON);
163 MAKE_MENU(gain_menu, ID2P(LANG_EQUALIZER_GAIN), NULL, Icon_NOICON, &gain_item_0,
164 &gain_item_1, &gain_item_2, &gain_item_3, &gain_item_4);
166 static const struct menu_item_ex *band_items[3][3] = {
167 { &cutoff_1, &q_1, &gain_1 },
168 { &cutoff_2, &q_2, &gain_2 },
169 { &cutoff_3, &q_3, &gain_3 }
172 static char* centerband_get_name(int selected_item, void * data, char *buffer)
174 (void)selected_item;
175 int band = (intptr_t)data;
176 snprintf(buffer, MAX_PATH, str(LANG_EQUALIZER_BAND_PEAK), band);
177 return buffer;
180 static int centerband_speak_item(int selected_item, void * data)
182 (void)selected_item;
183 int band = (intptr_t)data;
184 talk_id(LANG_EQUALIZER_BAND_PEAK, false);
185 talk_number(band, true);
186 return 0;
189 static int do_center_band_menu(void* param)
191 int band = (intptr_t)param;
192 struct menu_item_ex menu;
193 struct menu_callback_with_desc cb_and_desc;
194 char desc[MAX_PATH];
196 cb_and_desc.menu_callback = NULL;
197 snprintf(desc, MAX_PATH, str(LANG_EQUALIZER_BAND_PEAK), band);
198 cb_and_desc.desc = desc;
199 cb_and_desc.icon_id = Icon_EQ;
200 menu.flags = MT_MENU|(3<<MENU_COUNT_SHIFT)|MENU_HAS_DESC;
201 menu.submenus = band_items[band-1];
202 menu.callback_and_desc = &cb_and_desc;
203 do_menu(&menu, NULL, NULL, false);
204 return 0;
207 MAKE_MENU(band_0_menu, ID2P(LANG_EQUALIZER_BAND_LOW_SHELF), NULL,
208 Icon_EQ, &cutoff_0, &q_0, &gain_0);
209 MENUITEM_FUNCTION_DYNTEXT(band_1_menu, MENU_FUNC_USEPARAM,
210 do_center_band_menu, (void*)1,
211 centerband_get_name, centerband_speak_item,
212 (void*)1, NULL, Icon_EQ);
213 MENUITEM_FUNCTION_DYNTEXT(band_2_menu, MENU_FUNC_USEPARAM,
214 do_center_band_menu, (void*)2,
215 centerband_get_name, centerband_speak_item,
216 (void*)2, NULL, Icon_EQ);
217 MENUITEM_FUNCTION_DYNTEXT(band_3_menu, MENU_FUNC_USEPARAM,
218 do_center_band_menu, (void*)3,
219 centerband_get_name, centerband_speak_item,
220 (void*)3, NULL, Icon_EQ);
221 MAKE_MENU(band_4_menu, ID2P(LANG_EQUALIZER_BAND_HIGH_SHELF), NULL,
222 Icon_EQ, &cutoff_4, &q_4, &gain_4);
224 MAKE_MENU(advanced_eq_menu_, ID2P(LANG_EQUALIZER_ADVANCED), NULL, Icon_EQ,
225 &band_0_menu, &band_1_menu, &band_2_menu, &band_3_menu, &band_4_menu);
228 enum eq_slider_mode {
229 GAIN,
230 CUTOFF,
234 enum eq_type {
235 LOW_SHELF,
236 PEAK,
237 HIGH_SHELF
240 /* Size of just the slider/srollbar */
241 #define SCROLLBAR_SIZE 6
243 /* Draw the UI for a whole EQ band */
244 static int draw_eq_slider(struct screen * screen, int x, int y,
245 int width, int cutoff, int q, int gain, bool selected,
246 enum eq_slider_mode mode, int band)
248 char buf[26];
249 int steps, min_item, max_item;
250 int abs_gain = abs(gain);
251 int x1, x2, y1, total_height;
252 int w, h;
254 switch(mode) {
255 case Q:
256 steps = EQ_Q_MAX - EQ_Q_MIN;
257 min_item = q - EQ_Q_STEP - EQ_Q_MIN;
258 max_item = q + EQ_Q_STEP - EQ_Q_MIN;
259 break;
260 case CUTOFF:
261 steps = EQ_CUTOFF_MAX - EQ_CUTOFF_MIN;
262 min_item = cutoff - EQ_CUTOFF_FAST_STEP * 2;
263 max_item = cutoff + EQ_CUTOFF_FAST_STEP * 2;
264 break;
265 case GAIN:
266 default:
267 steps = EQ_GAIN_MAX - EQ_GAIN_MIN;
268 min_item = abs(EQ_GAIN_MIN) + gain - EQ_GAIN_STEP * 5;
269 max_item = abs(EQ_GAIN_MIN) + gain + EQ_GAIN_STEP * 5;
270 break;
273 /* Start two pixels in, one for border, one for margin */
274 x1 = x + 2;
275 y1 = y + 2;
277 /* Print out the band label */
278 if (band == 0) {
279 screen->putsxy(x1, y1, "LS: ");
280 screen->getstringsize("LS:", &w, &h);
281 } else if (band == 4) {
282 screen->putsxy(x1, y1, "HS: ");
283 screen->getstringsize("HS:", &w, &h);
284 } else {
285 snprintf(buf, sizeof(buf), "PK%d:", band);
286 screen->putsxy(x1, y1, buf);
287 screen->getstringsize(buf, &w, &h);
290 screen->getstringsize("A", &w, &h);
291 x1 += 5*w; /* 4 chars for label + 1 space = 5 */
293 /* Print out gain part of status line (left justify after label) */
294 if (mode == GAIN && selected)
295 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
296 else
297 screen->set_drawmode(DRMODE_SOLID);
299 snprintf(buf, sizeof(buf), "%s%2d.%d%s", gain < 0 ? "-" : " ",
300 abs_gain / EQ_USER_DIVISOR, abs_gain % EQ_USER_DIVISOR,
301 screen->lcdwidth >= 160 ? "dB" : "");
302 screen->putsxy(x1, y1, buf);
303 screen->getstringsize(buf, &w, &h);
304 x1 += w;
306 /* Print out Q part of status line (right justify) */
307 if (mode == Q && selected)
308 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
309 else
310 screen->set_drawmode(DRMODE_SOLID);
312 snprintf(buf, sizeof(buf), "%d.%d%s", q / EQ_USER_DIVISOR,
313 q % EQ_USER_DIVISOR, screen->lcdwidth >= 160 ? " Q" : "");
314 screen->getstringsize(buf, &w, &h);
315 x2 = x + width - w - 2;
316 screen->putsxy(x2, y1, buf);
318 /* Print out cutoff part of status line (center between gain & Q) */
319 if (mode == CUTOFF && selected)
320 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
321 else
322 screen->set_drawmode(DRMODE_SOLID);
324 snprintf(buf, sizeof(buf), "%5d%s", cutoff,
325 screen->lcdwidth >= 160 ? "Hz" : "");
326 screen->getstringsize(buf, &w, &h);
327 x1 = x1 + (x2 - x1 - w)/2;
328 screen->putsxy(x1, y1, buf);
330 /* Draw selection box */
331 total_height = 3 + h + 1 + SCROLLBAR_SIZE + 3;
332 screen->set_drawmode(DRMODE_SOLID);
333 if (selected) {
334 screen->drawrect(x, y, width, total_height);
337 /* Draw horizontal slider. Reuse scrollbar for this */
338 gui_scrollbar_draw(screen, x + 3, y1 + h + 1, width - 6, SCROLLBAR_SIZE,
339 steps, min_item, max_item, HORIZONTAL);
341 return total_height;
344 /* Draw's all the EQ sliders. Returns the total height of the sliders drawn */
345 static void draw_eq_sliders(struct screen * screen, int x, int y,
346 int nb_eq_sliders, int start_item,
347 int current_band, enum eq_slider_mode mode)
349 int i, gain, q, cutoff;
350 int height = y;
351 int *setting = &global_settings.eq_band0_cutoff;
353 start_item = MIN(start_item, 5-nb_eq_sliders);
354 for (i=0; i<5; i++) {
355 cutoff = *setting++;
356 q = *setting++;
357 gain = *setting++;
359 if (i == start_item + nb_eq_sliders)
360 break;
362 if (i >= start_item) {
363 height += draw_eq_slider(screen, x, height, screen->lcdwidth - x - 1,
364 cutoff, q, gain, i == current_band, mode,
366 /* add a margin */
367 height++;
370 if (nb_eq_sliders != 5)
371 gui_scrollbar_draw(screen, 0, y, SCROLLBAR_SIZE - 1,
372 screen->lcdheight - y, 5,
373 start_item, start_item + nb_eq_sliders,
374 VERTICAL);
375 return;
378 /* Provides a graphical means of editing the EQ settings */
379 bool eq_menu_graphical(void)
381 bool exit_request = false;
382 bool result = true;
383 bool has_changed = false;
384 int button;
385 int *setting;
386 int current_band, x, y, step, fast_step, min, max, voice_unit;
387 enum eq_slider_mode mode;
388 enum eq_type current_type;
389 char buf[24];
390 int i, w, h, height, start_item, nb_eq_sliders[NB_SCREENS];
391 int barsenabled = viewportmanager_set_statusbar(VP_SB_HIDE_ALL);
394 FOR_NB_SCREENS(i) {
395 screens[i].set_viewport(NULL);
396 screens[i].setfont(FONT_SYSFIXED);
397 screens[i].clear_display();
399 /* Figure out how many sliders can be drawn on the screen */
400 screens[i].getstringsize("A", &w, &h);
402 /* Total height includes margins (1), text, slider, and line selector (1) */
403 height = 3 + h + 1 + SCROLLBAR_SIZE + 3;
404 nb_eq_sliders[i] = screens[i].lcdheight / height;
406 /* Make sure the "Edit Mode" text fits too */
407 height = nb_eq_sliders[i]*height + h + 2;
408 if (height > screens[i].lcdheight)
409 nb_eq_sliders[i]--;
411 if (nb_eq_sliders[i] > 5)
412 nb_eq_sliders[i] = 5;
415 y = h + 1;
417 /* Start off editing gain on the first band */
418 mode = GAIN;
419 current_type = LOW_SHELF;
420 current_band = 0;
422 while (!exit_request) {
423 FOR_NB_SCREENS(i)
425 screens[i].clear_display();
427 /* Set pointer to the band data currently editable */
428 if (mode == GAIN) {
429 /* gain */
430 setting = &global_settings.eq_band0_gain;
431 setting += current_band * 3;
433 step = EQ_GAIN_STEP;
434 fast_step = EQ_GAIN_FAST_STEP;
435 min = EQ_GAIN_MIN;
436 max = EQ_GAIN_MAX;
437 voice_unit = UNIT_DB;
439 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
440 str(LANG_SYSFONT_GAIN), "(dB)");
442 screens[i].putsxy(0, 0, buf);
443 } else if (mode == CUTOFF) {
444 /* cutoff */
445 setting = &global_settings.eq_band0_cutoff;
446 setting += current_band * 3;
448 step = EQ_CUTOFF_STEP;
449 fast_step = EQ_CUTOFF_FAST_STEP;
450 min = EQ_CUTOFF_MIN;
451 max = EQ_CUTOFF_MAX;
452 voice_unit = UNIT_HERTZ;
454 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
455 str(LANG_SYSFONT_EQUALIZER_BAND_CUTOFF), "(Hz)");
457 screens[i].putsxy(0, 0, buf);
458 } else {
459 /* Q */
460 setting = &global_settings.eq_band0_q;
461 setting += current_band * 3;
463 step = EQ_Q_STEP;
464 fast_step = EQ_Q_FAST_STEP;
465 min = EQ_Q_MIN;
466 max = EQ_Q_MAX;
467 voice_unit = UNIT_INT;
469 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
470 str(LANG_EQUALIZER_BAND_Q), "");
472 screens[i].putsxy(0, 0, buf);
475 /* Draw scrollbar if needed */
476 if (nb_eq_sliders[i] != 5)
478 if (current_band == 0) {
479 start_item = 0;
480 } else if (current_band == 4) {
481 start_item = 5 - nb_eq_sliders[i];
482 } else {
483 start_item = current_band - 1;
485 x = SCROLLBAR_SIZE;
486 } else {
487 x = 1;
488 start_item = 0;
490 /* Draw equalizer band details */
491 draw_eq_sliders(&screens[i], x, y, nb_eq_sliders[i], start_item,
492 current_band, mode);
494 screens[i].update();
497 button = get_action(CONTEXT_SETTINGS_EQ,TIMEOUT_BLOCK);
499 switch (button) {
500 case ACTION_SETTINGS_DEC:
501 case ACTION_SETTINGS_DECREPEAT:
502 *(setting) -= step;
503 has_changed = true;
504 if (*(setting) < min)
505 *(setting) = min;
506 break;
508 case ACTION_SETTINGS_INC:
509 case ACTION_SETTINGS_INCREPEAT:
510 *(setting) += step;
511 has_changed = true;
512 if (*(setting) > max)
513 *(setting) = max;
514 break;
516 case ACTION_SETTINGS_INCBIGSTEP:
517 *(setting) += fast_step;
518 has_changed = true;
519 if (*(setting) > max)
520 *(setting) = max;
521 break;
523 case ACTION_SETTINGS_DECBIGSTEP:
524 *(setting) -= fast_step;
525 has_changed = true;
526 if (*(setting) < min)
527 *(setting) = min;
528 break;
530 case ACTION_STD_PREV:
531 case ACTION_STD_PREVREPEAT:
532 current_band--;
533 if (current_band < 0)
534 current_band = 4; /* wrap around */
535 break;
537 case ACTION_STD_NEXT:
538 case ACTION_STD_NEXTREPEAT:
539 current_band++;
540 if (current_band > 4)
541 current_band = 0; /* wrap around */
542 break;
544 case ACTION_STD_OK:
545 mode++;
546 if (mode > Q)
547 mode = GAIN; /* wrap around */
548 break;
550 case ACTION_STD_CANCEL:
551 exit_request = true;
552 result = false;
553 break;
554 default:
555 if(default_event_handler(button) == SYS_USB_CONNECTED) {
556 exit_request = true;
557 result = true;
559 break;
562 /* Update the filter if the user changed something */
563 if (has_changed) {
564 dsp_set_eq_coefs(current_band);
565 has_changed = false;
569 /* Reset screen settings */
570 FOR_NB_SCREENS(i) {
571 screens[i].setfont(FONT_UI);
572 screens[i].clear_display();
573 screens[i].set_viewport(NULL);
575 viewportmanager_set_statusbar(barsenabled);
576 return result;
579 static bool eq_save_preset(void)
581 /* make sure that the eq is enabled for setting saving */
582 bool enabled = global_settings.eq_enabled;
583 global_settings.eq_enabled = true;
585 bool result = settings_save_config(SETTINGS_SAVE_EQPRESET);
587 global_settings.eq_enabled = enabled;
589 return result;
592 /* Allows browsing of preset files */
593 bool eq_browse_presets(void)
595 return rockbox_browse(EQS_DIR, SHOW_CFG);
598 MENUITEM_FUNCTION(eq_graphical, 0, ID2P(LANG_EQUALIZER_GRAPHICAL),
599 (int(*)(void))eq_menu_graphical, NULL, lowlatency_callback,
600 Icon_EQ);
601 MENUITEM_FUNCTION(eq_save, 0, ID2P(LANG_EQUALIZER_SAVE),
602 (int(*)(void))eq_save_preset, NULL, NULL, Icon_NOICON);
603 MENUITEM_FUNCTION(eq_browse, 0, ID2P(LANG_EQUALIZER_BROWSE),
604 (int(*)(void))eq_browse_presets, NULL, lowlatency_callback,
605 Icon_NOICON);
607 MAKE_MENU(equalizer_menu, ID2P(LANG_EQUALIZER), NULL, Icon_EQ,
608 &eq_enable, &eq_graphical, &eq_precut, &gain_menu,
609 &advanced_eq_menu_, &eq_save, &eq_browse);