Remove useless operation.
[maemo-rb.git] / apps / menus / eq_menu.c
blob2cfb80f76a2afa8a368a4eaa17cd2c5ddb48bfc7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 "talk.h"
39 #include "misc.h"
40 #include "sound.h"
41 #include "dsp.h"
42 #include "tree.h"
43 #include "screen_access.h"
44 #include "keyboard.h"
45 #include "gui/scrollbar.h"
46 #include "menu_common.h"
47 #include "viewport.h"
48 #include "exported_menus.h"
51 * Utility functions
54 const char* 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,
57 value % EQ_USER_DIVISOR, unit);
58 return buffer;
61 const char* eq_precut_format(char* buffer, size_t buffer_size, int value, const char* unit)
63 snprintf(buffer, buffer_size, "%s%d.%d %s", value == 0 ? " " : "-",
64 value / EQ_USER_DIVISOR, value % EQ_USER_DIVISOR, unit);
65 return buffer;
69 * Settings functions
71 static void eq_apply(void)
73 dsp_set_eq(global_settings.eq_enabled);
74 dsp_set_eq_precut(global_settings.eq_precut);
75 /* Update all bands */
76 for(int i = 0; i < 5; i++) {
77 dsp_set_eq_coefs(i, global_settings.eq_band_settings[i].cutoff,
78 global_settings.eq_band_settings[i].q,
79 global_settings.eq_band_settings[i].gain);
83 static int eq_setting_callback(int action, const struct menu_item_ex *this_item)
85 switch (action)
87 case ACTION_ENTER_MENUITEM:
88 action = lowlatency_callback(action, this_item);
89 break;
90 case ACTION_EXIT_MENUITEM:
91 eq_apply();
92 action = lowlatency_callback(action, this_item);
93 break;
96 return action;
98 MENUITEM_SETTING(eq_enable, &global_settings.eq_enabled, eq_setting_callback);
99 MENUITEM_SETTING(eq_precut, &global_settings.eq_precut, eq_setting_callback);
101 MENUITEM_SETTING(cutoff_0, &global_settings.eq_band_settings[0].cutoff,
102 eq_setting_callback);
103 MENUITEM_SETTING(cutoff_1, &global_settings.eq_band_settings[1].cutoff,
104 eq_setting_callback);
105 MENUITEM_SETTING(cutoff_2, &global_settings.eq_band_settings[2].cutoff,
106 eq_setting_callback);
107 MENUITEM_SETTING(cutoff_3, &global_settings.eq_band_settings[3].cutoff,
108 eq_setting_callback);
109 MENUITEM_SETTING(cutoff_4, &global_settings.eq_band_settings[4].cutoff,
110 eq_setting_callback);
112 MENUITEM_SETTING(q_0, &global_settings.eq_band_settings[0].q,
113 eq_setting_callback);
114 MENUITEM_SETTING(q_1, &global_settings.eq_band_settings[1].q,
115 eq_setting_callback);
116 MENUITEM_SETTING(q_2, &global_settings.eq_band_settings[2].q,
117 eq_setting_callback);
118 MENUITEM_SETTING(q_3, &global_settings.eq_band_settings[3].q,
119 eq_setting_callback);
120 MENUITEM_SETTING(q_4, &global_settings.eq_band_settings[4].q,
121 eq_setting_callback);
123 MENUITEM_SETTING(gain_0, &global_settings.eq_band_settings[0].gain,
124 eq_setting_callback);
125 MENUITEM_SETTING(gain_1, &global_settings.eq_band_settings[1].gain,
126 eq_setting_callback);
127 MENUITEM_SETTING(gain_2, &global_settings.eq_band_settings[2].gain,
128 eq_setting_callback);
129 MENUITEM_SETTING(gain_3, &global_settings.eq_band_settings[3].gain,
130 eq_setting_callback);
131 MENUITEM_SETTING(gain_4, &global_settings.eq_band_settings[4].gain,
132 eq_setting_callback);
134 static char* gainitem_get_name(int selected_item, void * data, char *buffer)
136 (void)selected_item;
137 int *setting = (int*)data;
138 snprintf(buffer, MAX_PATH, str(LANG_EQUALIZER_GAIN_ITEM), *setting);
139 return buffer;
142 static int gainitem_speak_item(int selected_item, void * data)
144 (void)selected_item;
145 int *setting = (int*)data;
146 talk_number(*setting, false);
147 talk_id(LANG_EQUALIZER_GAIN_ITEM, true);
148 return 0;
151 static int do_option(void * param)
153 const struct menu_item_ex *setting = (const struct menu_item_ex*)param;
154 lowlatency_callback(ACTION_ENTER_MENUITEM, setting);
155 do_setting_from_menu(setting, NULL);
156 eq_apply();
157 lowlatency_callback(ACTION_EXIT_MENUITEM, setting);
158 return 0;
161 MENUITEM_FUNCTION_DYNTEXT(gain_item_0, MENU_FUNC_USEPARAM,
162 do_option, (void*)&gain_0,
163 gainitem_get_name, gainitem_speak_item,
164 &global_settings.eq_band_settings[0].cutoff,
165 NULL, Icon_NOICON);
166 MENUITEM_FUNCTION_DYNTEXT(gain_item_1, MENU_FUNC_USEPARAM,
167 do_option, (void*)&gain_1,
168 gainitem_get_name, gainitem_speak_item,
169 &global_settings.eq_band_settings[1].cutoff,
170 NULL, Icon_NOICON);
171 MENUITEM_FUNCTION_DYNTEXT(gain_item_2, MENU_FUNC_USEPARAM,
172 do_option, (void*)&gain_2,
173 gainitem_get_name, gainitem_speak_item,
174 &global_settings.eq_band_settings[2].cutoff,
175 NULL, Icon_NOICON);
176 MENUITEM_FUNCTION_DYNTEXT(gain_item_3, MENU_FUNC_USEPARAM,
177 do_option, (void*)&gain_3,
178 gainitem_get_name, gainitem_speak_item,
179 &global_settings.eq_band_settings[3].cutoff,
180 NULL, Icon_NOICON);
181 MENUITEM_FUNCTION_DYNTEXT(gain_item_4, MENU_FUNC_USEPARAM,
182 do_option, (void*)&gain_4,
183 gainitem_get_name, gainitem_speak_item,
184 &global_settings.eq_band_settings[4].cutoff,
185 NULL, Icon_NOICON);
187 MAKE_MENU(gain_menu, ID2P(LANG_EQUALIZER_GAIN), NULL, Icon_NOICON, &gain_item_0,
188 &gain_item_1, &gain_item_2, &gain_item_3, &gain_item_4);
190 static const struct menu_item_ex *band_items[3][3] = {
191 { &cutoff_1, &q_1, &gain_1 },
192 { &cutoff_2, &q_2, &gain_2 },
193 { &cutoff_3, &q_3, &gain_3 }
196 static char* centerband_get_name(int selected_item, void * data, char *buffer)
198 (void)selected_item;
199 int band = (intptr_t)data;
200 snprintf(buffer, MAX_PATH, str(LANG_EQUALIZER_BAND_PEAK), band);
201 return buffer;
204 static int centerband_speak_item(int selected_item, void * data)
206 (void)selected_item;
207 int band = (intptr_t)data;
208 talk_id(LANG_EQUALIZER_BAND_PEAK, false);
209 talk_number(band, true);
210 return 0;
213 static int do_center_band_menu(void* param)
215 int band = (intptr_t)param;
216 struct menu_item_ex menu;
217 struct menu_callback_with_desc cb_and_desc;
218 char desc[MAX_PATH];
220 cb_and_desc.menu_callback = NULL;
221 snprintf(desc, MAX_PATH, str(LANG_EQUALIZER_BAND_PEAK), band);
222 cb_and_desc.desc = desc;
223 cb_and_desc.icon_id = Icon_EQ;
224 menu.flags = MT_MENU|(3<<MENU_COUNT_SHIFT)|MENU_HAS_DESC;
225 menu.submenus = band_items[band-1];
226 menu.callback_and_desc = &cb_and_desc;
227 do_menu(&menu, NULL, NULL, false);
228 return 0;
231 MAKE_MENU(band_0_menu, ID2P(LANG_EQUALIZER_BAND_LOW_SHELF), NULL,
232 Icon_EQ, &cutoff_0, &q_0, &gain_0);
233 MENUITEM_FUNCTION_DYNTEXT(band_1_menu, MENU_FUNC_USEPARAM,
234 do_center_band_menu, (void*)1,
235 centerband_get_name, centerband_speak_item,
236 (void*)1, NULL, Icon_EQ);
237 MENUITEM_FUNCTION_DYNTEXT(band_2_menu, MENU_FUNC_USEPARAM,
238 do_center_band_menu, (void*)2,
239 centerband_get_name, centerband_speak_item,
240 (void*)2, NULL, Icon_EQ);
241 MENUITEM_FUNCTION_DYNTEXT(band_3_menu, MENU_FUNC_USEPARAM,
242 do_center_band_menu, (void*)3,
243 centerband_get_name, centerband_speak_item,
244 (void*)3, NULL, Icon_EQ);
245 MAKE_MENU(band_4_menu, ID2P(LANG_EQUALIZER_BAND_HIGH_SHELF), NULL,
246 Icon_EQ, &cutoff_4, &q_4, &gain_4);
248 MAKE_MENU(advanced_eq_menu_, ID2P(LANG_EQUALIZER_ADVANCED), NULL, Icon_EQ,
249 &band_0_menu, &band_1_menu, &band_2_menu, &band_3_menu, &band_4_menu);
252 enum eq_slider_mode {
253 GAIN,
254 CUTOFF,
258 enum eq_type {
259 LOW_SHELF,
260 PEAK,
261 HIGH_SHELF
264 /* Size of just the slider/srollbar */
265 #define SCROLLBAR_SIZE 6
267 /* Draw the UI for a whole EQ band */
268 static int draw_eq_slider(struct screen * screen, int x, int y,
269 int width, int cutoff, int q, int gain, bool selected,
270 enum eq_slider_mode mode, int band)
272 char buf[26];
273 int steps, min_item, max_item;
274 int abs_gain = abs(gain);
275 int x1, x2, y1, total_height;
276 int w, h;
278 switch(mode) {
279 case Q:
280 steps = EQ_Q_MAX - EQ_Q_MIN;
281 min_item = q - EQ_Q_STEP - EQ_Q_MIN;
282 max_item = q + EQ_Q_STEP - EQ_Q_MIN;
283 break;
284 case CUTOFF:
285 steps = EQ_CUTOFF_MAX - EQ_CUTOFF_MIN;
286 min_item = cutoff - EQ_CUTOFF_FAST_STEP * 2;
287 max_item = cutoff + EQ_CUTOFF_FAST_STEP * 2;
288 break;
289 case GAIN:
290 default:
291 steps = EQ_GAIN_MAX - EQ_GAIN_MIN;
292 min_item = abs(EQ_GAIN_MIN) + gain - EQ_GAIN_STEP * 5;
293 max_item = abs(EQ_GAIN_MIN) + gain + EQ_GAIN_STEP * 5;
294 break;
297 /* Start two pixels in, one for border, one for margin */
298 x1 = x + 2;
299 y1 = y + 2;
301 /* Print out the band label */
302 if (band == 0) {
303 screen->putsxy(x1, y1, "LS: ");
304 screen->getstringsize("LS:", &w, &h);
305 } else if (band == 4) {
306 screen->putsxy(x1, y1, "HS: ");
307 screen->getstringsize("HS:", &w, &h);
308 } else {
309 snprintf(buf, sizeof(buf), "PK%d:", band);
310 screen->putsxy(x1, y1, buf);
311 screen->getstringsize(buf, &w, &h);
314 screen->getstringsize("A", &w, &h);
315 x1 += 5*w; /* 4 chars for label + 1 space = 5 */
317 /* Print out gain part of status line (left justify after label) */
318 if (mode == GAIN && selected)
319 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
320 else
321 screen->set_drawmode(DRMODE_SOLID);
323 snprintf(buf, sizeof(buf), "%s%2d.%d%s", gain < 0 ? "-" : " ",
324 abs_gain / EQ_USER_DIVISOR, abs_gain % EQ_USER_DIVISOR,
325 screen->lcdwidth >= 160 ? "dB" : "");
326 screen->putsxy(x1, y1, buf);
327 screen->getstringsize(buf, &w, &h);
328 x1 += w;
330 /* Print out Q part of status line (right justify) */
331 if (mode == Q && selected)
332 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
333 else
334 screen->set_drawmode(DRMODE_SOLID);
336 snprintf(buf, sizeof(buf), "%d.%d%s", q / EQ_USER_DIVISOR,
337 q % EQ_USER_DIVISOR, screen->lcdwidth >= 160 ? " Q" : "");
338 screen->getstringsize(buf, &w, &h);
339 x2 = x + width - w - 2;
340 screen->putsxy(x2, y1, buf);
342 /* Print out cutoff part of status line (center between gain & Q) */
343 if (mode == CUTOFF && selected)
344 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
345 else
346 screen->set_drawmode(DRMODE_SOLID);
348 snprintf(buf, sizeof(buf), "%5d%s", cutoff,
349 screen->lcdwidth >= 160 ? "Hz" : "");
350 screen->getstringsize(buf, &w, &h);
351 x1 = x1 + (x2 - x1 - w)/2;
352 screen->putsxy(x1, y1, buf);
354 /* Draw selection box */
355 total_height = 3 + h + 1 + SCROLLBAR_SIZE + 3;
356 screen->set_drawmode(DRMODE_SOLID);
357 if (selected) {
358 screen->drawrect(x, y, width, total_height);
361 /* Draw horizontal slider. Reuse scrollbar for this */
362 gui_scrollbar_draw(screen, x + 3, y1 + h + 1, width - 6, SCROLLBAR_SIZE,
363 steps, min_item, max_item, HORIZONTAL);
365 return total_height;
368 /* Draw's all the EQ sliders. Returns the total height of the sliders drawn */
369 static void draw_eq_sliders(struct screen * screen, int x, int y,
370 int nb_eq_sliders, int start_item,
371 int current_band, enum eq_slider_mode mode)
373 int height = y;
375 start_item = MIN(start_item, 5 - nb_eq_sliders);
377 for (int i = 0; i < 5; i++) {
378 struct eq_band_setting *setting = &global_settings.eq_band_settings[i];
379 int cutoff = setting->cutoff;
380 int q = setting->q;
381 int gain = setting->gain;
383 if (i == start_item + nb_eq_sliders)
384 break;
386 if (i >= start_item) {
387 height += draw_eq_slider(screen, x, height, screen->lcdwidth - x - 1,
388 cutoff, q, gain, i == current_band, mode,
390 /* add a margin */
391 height++;
395 if (nb_eq_sliders != 5)
396 gui_scrollbar_draw(screen, 0, y, SCROLLBAR_SIZE - 1,
397 screen->lcdheight - y, 5,
398 start_item, start_item + nb_eq_sliders,
399 VERTICAL);
400 return;
403 /* Provides a graphical means of editing the EQ settings */
404 bool eq_menu_graphical(void)
406 bool exit_request = false;
407 bool result = true;
408 bool has_changed = false;
409 int button;
410 int *setting;
411 int current_band, x, y, step, fast_step, min, max;
412 enum eq_slider_mode mode;
413 char buf[24];
414 int w, h, height, start_item, nb_eq_sliders[NB_SCREENS];
415 FOR_NB_SCREENS(i)
416 viewportmanager_theme_enable(i, false, NULL);
419 FOR_NB_SCREENS(i) {
420 screens[i].set_viewport(NULL);
421 screens[i].setfont(FONT_SYSFIXED);
422 screens[i].clear_display();
424 /* Figure out how many sliders can be drawn on the screen */
425 screens[i].getstringsize("A", &w, &h);
427 /* Total height includes margins (1), text, slider, and line selector (1) */
428 height = 3 + h + 1 + SCROLLBAR_SIZE + 3;
429 nb_eq_sliders[i] = screens[i].lcdheight / height;
431 /* Make sure the "Edit Mode" text fits too */
432 height = nb_eq_sliders[i]*height + h + 2;
433 if (height > screens[i].lcdheight)
434 nb_eq_sliders[i]--;
436 if (nb_eq_sliders[i] > 5)
437 nb_eq_sliders[i] = 5;
440 y = h + 1;
442 /* Start off editing gain on the first band */
443 mode = GAIN;
444 current_band = 0;
446 while (!exit_request) {
447 FOR_NB_SCREENS(i)
449 screens[i].clear_display();
451 /* Set pointer to the band data currently editable */
452 if (mode == GAIN) {
453 /* gain */
454 setting = &global_settings.eq_band_settings[current_band].gain;
456 step = EQ_GAIN_STEP;
457 fast_step = EQ_GAIN_FAST_STEP;
458 min = EQ_GAIN_MIN;
459 max = EQ_GAIN_MAX;
461 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
462 str(LANG_SYSFONT_GAIN), "(dB)");
464 screens[i].putsxy(0, 0, buf);
465 } else if (mode == CUTOFF) {
466 /* cutoff */
467 setting = &global_settings.eq_band_settings[current_band].cutoff;
469 step = EQ_CUTOFF_STEP;
470 fast_step = EQ_CUTOFF_FAST_STEP;
471 min = EQ_CUTOFF_MIN;
472 max = EQ_CUTOFF_MAX;
474 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
475 str(LANG_SYSFONT_EQUALIZER_BAND_CUTOFF), "(Hz)");
477 screens[i].putsxy(0, 0, buf);
478 } else {
479 /* Q */
480 setting = &global_settings.eq_band_settings[current_band].q;
482 step = EQ_Q_STEP;
483 fast_step = EQ_Q_FAST_STEP;
484 min = EQ_Q_MIN;
485 max = EQ_Q_MAX;
487 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
488 str(LANG_EQUALIZER_BAND_Q), "");
490 screens[i].putsxy(0, 0, buf);
493 /* Draw scrollbar if needed */
494 if (nb_eq_sliders[i] != 5)
496 if (current_band == 0) {
497 start_item = 0;
498 } else if (current_band == 4) {
499 start_item = 5 - nb_eq_sliders[i];
500 } else {
501 start_item = current_band - 1;
503 x = SCROLLBAR_SIZE;
504 } else {
505 x = 1;
506 start_item = 0;
508 /* Draw equalizer band details */
509 draw_eq_sliders(&screens[i], x, y, nb_eq_sliders[i], start_item,
510 current_band, mode);
512 screens[i].update();
515 button = get_action(CONTEXT_SETTINGS_EQ,TIMEOUT_BLOCK);
517 switch (button) {
518 case ACTION_SETTINGS_DEC:
519 case ACTION_SETTINGS_DECREPEAT:
520 *(setting) -= step;
521 has_changed = true;
522 if (*(setting) < min)
523 *(setting) = min;
524 break;
526 case ACTION_SETTINGS_INC:
527 case ACTION_SETTINGS_INCREPEAT:
528 *(setting) += step;
529 has_changed = true;
530 if (*(setting) > max)
531 *(setting) = max;
532 break;
534 case ACTION_SETTINGS_INCBIGSTEP:
535 *(setting) += fast_step;
536 has_changed = true;
537 if (*(setting) > max)
538 *(setting) = max;
539 break;
541 case ACTION_SETTINGS_DECBIGSTEP:
542 *(setting) -= fast_step;
543 has_changed = true;
544 if (*(setting) < min)
545 *(setting) = min;
546 break;
548 case ACTION_STD_PREV:
549 case ACTION_STD_PREVREPEAT:
550 current_band--;
551 if (current_band < 0)
552 current_band = 4; /* wrap around */
553 break;
555 case ACTION_STD_NEXT:
556 case ACTION_STD_NEXTREPEAT:
557 current_band++;
558 if (current_band > 4)
559 current_band = 0; /* wrap around */
560 break;
562 case ACTION_STD_OK:
563 mode++;
564 if (mode > Q)
565 mode = GAIN; /* wrap around */
566 break;
568 case ACTION_STD_CANCEL:
569 exit_request = true;
570 result = false;
571 break;
572 default:
573 if(default_event_handler(button) == SYS_USB_CONNECTED) {
574 exit_request = true;
575 result = true;
577 break;
580 /* Update the filter if the user changed something */
581 if (has_changed) {
582 dsp_set_eq_coefs(current_band,
583 global_settings.eq_band_settings[current_band].cutoff,
584 global_settings.eq_band_settings[current_band].q,
585 global_settings.eq_band_settings[current_band].gain);
586 has_changed = false;
590 /* Reset screen settings */
591 FOR_NB_SCREENS(i)
593 screens[i].setfont(FONT_UI);
594 screens[i].clear_display();
595 screens[i].set_viewport(NULL);
596 viewportmanager_theme_undo(i, false);
598 return result;
601 static bool eq_save_preset(void)
603 /* make sure that the eq is enabled for setting saving */
604 bool enabled = global_settings.eq_enabled;
605 global_settings.eq_enabled = true;
607 bool result = settings_save_config(SETTINGS_SAVE_EQPRESET);
609 global_settings.eq_enabled = enabled;
611 return result;
614 /* Allows browsing of preset files */
615 static struct browse_folder_info eqs = { EQS_DIR, SHOW_CFG };
617 MENUITEM_FUNCTION(eq_graphical, 0, ID2P(LANG_EQUALIZER_GRAPHICAL),
618 (int(*)(void))eq_menu_graphical, NULL, lowlatency_callback,
619 Icon_EQ);
620 MENUITEM_FUNCTION(eq_save, 0, ID2P(LANG_EQUALIZER_SAVE),
621 (int(*)(void))eq_save_preset, NULL, NULL, Icon_NOICON);
622 MENUITEM_FUNCTION(eq_browse, MENU_FUNC_USEPARAM, ID2P(LANG_EQUALIZER_BROWSE),
623 browse_folder, (void*)&eqs, lowlatency_callback,
624 Icon_NOICON);
626 MAKE_MENU(equalizer_menu, ID2P(LANG_EQUALIZER), NULL, Icon_EQ,
627 &eq_enable, &eq_graphical, &eq_precut, &gain_menu,
628 &advanced_eq_menu_, &eq_save, &eq_browse);