keyclick: Add a callback so screens can cancel a click. Add a generic list callback...
[maemo-rb.git] / apps / menus / eq_menu.c
blobc1d9792c44cffdc7691c12b622df6fb82ecc0b94
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 int i;
74 dsp_set_eq(global_settings.eq_enabled);
75 dsp_set_eq_precut(global_settings.eq_precut);
76 /* Update all bands */
77 for(i = 0; i < 5; i++) {
78 dsp_set_eq_coefs(i);
82 static int eq_setting_callback(int action, const struct menu_item_ex *this_item)
84 switch (action)
86 case ACTION_ENTER_MENUITEM:
87 action = lowlatency_callback(action, this_item);
88 break;
89 case ACTION_EXIT_MENUITEM:
90 eq_apply();
91 action = lowlatency_callback(action, this_item);
92 break;
95 return action;
97 MENUITEM_SETTING(eq_enable, &global_settings.eq_enabled, eq_setting_callback);
98 MENUITEM_SETTING(eq_precut, &global_settings.eq_precut, eq_setting_callback);
100 MENUITEM_SETTING(cutoff_0, &global_settings.eq_band0_cutoff, eq_setting_callback);
101 MENUITEM_SETTING(cutoff_1, &global_settings.eq_band1_cutoff, eq_setting_callback);
102 MENUITEM_SETTING(cutoff_2, &global_settings.eq_band2_cutoff, eq_setting_callback);
103 MENUITEM_SETTING(cutoff_3, &global_settings.eq_band3_cutoff, eq_setting_callback);
104 MENUITEM_SETTING(cutoff_4, &global_settings.eq_band4_cutoff, eq_setting_callback);
106 MENUITEM_SETTING(q_0, &global_settings.eq_band0_q, eq_setting_callback);
107 MENUITEM_SETTING(q_1, &global_settings.eq_band1_q, eq_setting_callback);
108 MENUITEM_SETTING(q_2, &global_settings.eq_band2_q, eq_setting_callback);
109 MENUITEM_SETTING(q_3, &global_settings.eq_band3_q, eq_setting_callback);
110 MENUITEM_SETTING(q_4, &global_settings.eq_band4_q, eq_setting_callback);
112 MENUITEM_SETTING(gain_0, &global_settings.eq_band0_gain, eq_setting_callback);
113 MENUITEM_SETTING(gain_1, &global_settings.eq_band1_gain, eq_setting_callback);
114 MENUITEM_SETTING(gain_2, &global_settings.eq_band2_gain, eq_setting_callback);
115 MENUITEM_SETTING(gain_3, &global_settings.eq_band3_gain, eq_setting_callback);
116 MENUITEM_SETTING(gain_4, &global_settings.eq_band4_gain, eq_setting_callback);
118 static char* gainitem_get_name(int selected_item, void * data, char *buffer)
120 (void)selected_item;
121 int *setting = (int*)data;
122 snprintf(buffer, MAX_PATH, str(LANG_EQUALIZER_GAIN_ITEM), *setting);
123 return buffer;
126 static int gainitem_speak_item(int selected_item, void * data)
128 (void)selected_item;
129 int *setting = (int*)data;
130 talk_number(*setting, false);
131 talk_id(LANG_EQUALIZER_GAIN_ITEM, true);
132 return 0;
135 static int do_option(void * param)
137 const struct menu_item_ex *setting = (const struct menu_item_ex*)param;
138 lowlatency_callback(ACTION_ENTER_MENUITEM, setting);
139 do_setting_from_menu(setting, NULL);
140 eq_apply();
141 lowlatency_callback(ACTION_EXIT_MENUITEM, setting);
142 return 0;
145 MENUITEM_FUNCTION_DYNTEXT(gain_item_0, MENU_FUNC_USEPARAM,
146 do_option, (void*)&gain_0,
147 gainitem_get_name, gainitem_speak_item,
148 &global_settings.eq_band0_cutoff, NULL, Icon_NOICON);
149 MENUITEM_FUNCTION_DYNTEXT(gain_item_1, MENU_FUNC_USEPARAM,
150 do_option, (void*)&gain_1,
151 gainitem_get_name, gainitem_speak_item,
152 &global_settings.eq_band1_cutoff, NULL, Icon_NOICON);
153 MENUITEM_FUNCTION_DYNTEXT(gain_item_2, MENU_FUNC_USEPARAM,
154 do_option, (void*)&gain_2,
155 gainitem_get_name, gainitem_speak_item,
156 &global_settings.eq_band2_cutoff, NULL, Icon_NOICON);
157 MENUITEM_FUNCTION_DYNTEXT(gain_item_3, MENU_FUNC_USEPARAM,
158 do_option, (void*)&gain_3,
159 gainitem_get_name, gainitem_speak_item,
160 &global_settings.eq_band3_cutoff, NULL, Icon_NOICON);
161 MENUITEM_FUNCTION_DYNTEXT(gain_item_4, MENU_FUNC_USEPARAM,
162 do_option, (void*)&gain_4,
163 gainitem_get_name, gainitem_speak_item,
164 &global_settings.eq_band4_cutoff, NULL, Icon_NOICON);
166 MAKE_MENU(gain_menu, ID2P(LANG_EQUALIZER_GAIN), NULL, Icon_NOICON, &gain_item_0,
167 &gain_item_1, &gain_item_2, &gain_item_3, &gain_item_4);
169 static const struct menu_item_ex *band_items[3][3] = {
170 { &cutoff_1, &q_1, &gain_1 },
171 { &cutoff_2, &q_2, &gain_2 },
172 { &cutoff_3, &q_3, &gain_3 }
175 static char* centerband_get_name(int selected_item, void * data, char *buffer)
177 (void)selected_item;
178 int band = (intptr_t)data;
179 snprintf(buffer, MAX_PATH, str(LANG_EQUALIZER_BAND_PEAK), band);
180 return buffer;
183 static int centerband_speak_item(int selected_item, void * data)
185 (void)selected_item;
186 int band = (intptr_t)data;
187 talk_id(LANG_EQUALIZER_BAND_PEAK, false);
188 talk_number(band, true);
189 return 0;
192 static int do_center_band_menu(void* param)
194 int band = (intptr_t)param;
195 struct menu_item_ex menu;
196 struct menu_callback_with_desc cb_and_desc;
197 char desc[MAX_PATH];
199 cb_and_desc.menu_callback = NULL;
200 snprintf(desc, MAX_PATH, str(LANG_EQUALIZER_BAND_PEAK), band);
201 cb_and_desc.desc = desc;
202 cb_and_desc.icon_id = Icon_EQ;
203 menu.flags = MT_MENU|(3<<MENU_COUNT_SHIFT)|MENU_HAS_DESC;
204 menu.submenus = band_items[band-1];
205 menu.callback_and_desc = &cb_and_desc;
206 do_menu(&menu, NULL, NULL, false);
207 return 0;
210 MAKE_MENU(band_0_menu, ID2P(LANG_EQUALIZER_BAND_LOW_SHELF), NULL,
211 Icon_EQ, &cutoff_0, &q_0, &gain_0);
212 MENUITEM_FUNCTION_DYNTEXT(band_1_menu, MENU_FUNC_USEPARAM,
213 do_center_band_menu, (void*)1,
214 centerband_get_name, centerband_speak_item,
215 (void*)1, NULL, Icon_EQ);
216 MENUITEM_FUNCTION_DYNTEXT(band_2_menu, MENU_FUNC_USEPARAM,
217 do_center_band_menu, (void*)2,
218 centerband_get_name, centerband_speak_item,
219 (void*)2, NULL, Icon_EQ);
220 MENUITEM_FUNCTION_DYNTEXT(band_3_menu, MENU_FUNC_USEPARAM,
221 do_center_band_menu, (void*)3,
222 centerband_get_name, centerband_speak_item,
223 (void*)3, NULL, Icon_EQ);
224 MAKE_MENU(band_4_menu, ID2P(LANG_EQUALIZER_BAND_HIGH_SHELF), NULL,
225 Icon_EQ, &cutoff_4, &q_4, &gain_4);
227 MAKE_MENU(advanced_eq_menu_, ID2P(LANG_EQUALIZER_ADVANCED), NULL, Icon_EQ,
228 &band_0_menu, &band_1_menu, &band_2_menu, &band_3_menu, &band_4_menu);
231 enum eq_slider_mode {
232 GAIN,
233 CUTOFF,
237 enum eq_type {
238 LOW_SHELF,
239 PEAK,
240 HIGH_SHELF
243 /* Size of just the slider/srollbar */
244 #define SCROLLBAR_SIZE 6
246 /* Draw the UI for a whole EQ band */
247 static int draw_eq_slider(struct screen * screen, int x, int y,
248 int width, int cutoff, int q, int gain, bool selected,
249 enum eq_slider_mode mode, int band)
251 char buf[26];
252 int steps, min_item, max_item;
253 int abs_gain = abs(gain);
254 int x1, x2, y1, total_height;
255 int w, h;
257 switch(mode) {
258 case Q:
259 steps = EQ_Q_MAX - EQ_Q_MIN;
260 min_item = q - EQ_Q_STEP - EQ_Q_MIN;
261 max_item = q + EQ_Q_STEP - EQ_Q_MIN;
262 break;
263 case CUTOFF:
264 steps = EQ_CUTOFF_MAX - EQ_CUTOFF_MIN;
265 min_item = cutoff - EQ_CUTOFF_FAST_STEP * 2;
266 max_item = cutoff + EQ_CUTOFF_FAST_STEP * 2;
267 break;
268 case GAIN:
269 default:
270 steps = EQ_GAIN_MAX - EQ_GAIN_MIN;
271 min_item = abs(EQ_GAIN_MIN) + gain - EQ_GAIN_STEP * 5;
272 max_item = abs(EQ_GAIN_MIN) + gain + EQ_GAIN_STEP * 5;
273 break;
276 /* Start two pixels in, one for border, one for margin */
277 x1 = x + 2;
278 y1 = y + 2;
280 /* Print out the band label */
281 if (band == 0) {
282 screen->putsxy(x1, y1, "LS: ");
283 screen->getstringsize("LS:", &w, &h);
284 } else if (band == 4) {
285 screen->putsxy(x1, y1, "HS: ");
286 screen->getstringsize("HS:", &w, &h);
287 } else {
288 snprintf(buf, sizeof(buf), "PK%d:", band);
289 screen->putsxy(x1, y1, buf);
290 screen->getstringsize(buf, &w, &h);
293 screen->getstringsize("A", &w, &h);
294 x1 += 5*w; /* 4 chars for label + 1 space = 5 */
296 /* Print out gain part of status line (left justify after label) */
297 if (mode == GAIN && selected)
298 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
299 else
300 screen->set_drawmode(DRMODE_SOLID);
302 snprintf(buf, sizeof(buf), "%s%2d.%d%s", gain < 0 ? "-" : " ",
303 abs_gain / EQ_USER_DIVISOR, abs_gain % EQ_USER_DIVISOR,
304 screen->lcdwidth >= 160 ? "dB" : "");
305 screen->putsxy(x1, y1, buf);
306 screen->getstringsize(buf, &w, &h);
307 x1 += w;
309 /* Print out Q part of status line (right justify) */
310 if (mode == Q && selected)
311 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
312 else
313 screen->set_drawmode(DRMODE_SOLID);
315 snprintf(buf, sizeof(buf), "%d.%d%s", q / EQ_USER_DIVISOR,
316 q % EQ_USER_DIVISOR, screen->lcdwidth >= 160 ? " Q" : "");
317 screen->getstringsize(buf, &w, &h);
318 x2 = x + width - w - 2;
319 screen->putsxy(x2, y1, buf);
321 /* Print out cutoff part of status line (center between gain & Q) */
322 if (mode == CUTOFF && selected)
323 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
324 else
325 screen->set_drawmode(DRMODE_SOLID);
327 snprintf(buf, sizeof(buf), "%5d%s", cutoff,
328 screen->lcdwidth >= 160 ? "Hz" : "");
329 screen->getstringsize(buf, &w, &h);
330 x1 = x1 + (x2 - x1 - w)/2;
331 screen->putsxy(x1, y1, buf);
333 /* Draw selection box */
334 total_height = 3 + h + 1 + SCROLLBAR_SIZE + 3;
335 screen->set_drawmode(DRMODE_SOLID);
336 if (selected) {
337 screen->drawrect(x, y, width, total_height);
340 /* Draw horizontal slider. Reuse scrollbar for this */
341 gui_scrollbar_draw(screen, x + 3, y1 + h + 1, width - 6, SCROLLBAR_SIZE,
342 steps, min_item, max_item, HORIZONTAL);
344 return total_height;
347 /* Draw's all the EQ sliders. Returns the total height of the sliders drawn */
348 static void draw_eq_sliders(struct screen * screen, int x, int y,
349 int nb_eq_sliders, int start_item,
350 int current_band, enum eq_slider_mode mode)
352 int i, gain, q, cutoff;
353 int height = y;
354 int *setting = &global_settings.eq_band0_cutoff;
356 start_item = MIN(start_item, 5-nb_eq_sliders);
357 for (i=0; i<5; i++) {
358 cutoff = *setting++;
359 q = *setting++;
360 gain = *setting++;
362 if (i == start_item + nb_eq_sliders)
363 break;
365 if (i >= start_item) {
366 height += draw_eq_slider(screen, x, height, screen->lcdwidth - x - 1,
367 cutoff, q, gain, i == current_band, mode,
369 /* add a margin */
370 height++;
373 if (nb_eq_sliders != 5)
374 gui_scrollbar_draw(screen, 0, y, SCROLLBAR_SIZE - 1,
375 screen->lcdheight - y, 5,
376 start_item, start_item + nb_eq_sliders,
377 VERTICAL);
378 return;
381 /* Provides a graphical means of editing the EQ settings */
382 bool eq_menu_graphical(void)
384 bool exit_request = false;
385 bool result = true;
386 bool has_changed = false;
387 int button;
388 int *setting;
389 int current_band, x, y, step, fast_step, min, max;
390 enum eq_slider_mode mode;
391 char buf[24];
392 int w, h, height, start_item, nb_eq_sliders[NB_SCREENS];
393 FOR_NB_SCREENS(i)
394 viewportmanager_theme_enable(i, false, NULL);
397 FOR_NB_SCREENS(i) {
398 screens[i].set_viewport(NULL);
399 screens[i].setfont(FONT_SYSFIXED);
400 screens[i].clear_display();
402 /* Figure out how many sliders can be drawn on the screen */
403 screens[i].getstringsize("A", &w, &h);
405 /* Total height includes margins (1), text, slider, and line selector (1) */
406 height = 3 + h + 1 + SCROLLBAR_SIZE + 3;
407 nb_eq_sliders[i] = screens[i].lcdheight / height;
409 /* Make sure the "Edit Mode" text fits too */
410 height = nb_eq_sliders[i]*height + h + 2;
411 if (height > screens[i].lcdheight)
412 nb_eq_sliders[i]--;
414 if (nb_eq_sliders[i] > 5)
415 nb_eq_sliders[i] = 5;
418 y = h + 1;
420 /* Start off editing gain on the first band */
421 mode = GAIN;
422 current_band = 0;
424 while (!exit_request) {
425 FOR_NB_SCREENS(i)
427 screens[i].clear_display();
429 /* Set pointer to the band data currently editable */
430 if (mode == GAIN) {
431 /* gain */
432 setting = &global_settings.eq_band0_gain;
433 setting += current_band * 3;
435 step = EQ_GAIN_STEP;
436 fast_step = EQ_GAIN_FAST_STEP;
437 min = EQ_GAIN_MIN;
438 max = EQ_GAIN_MAX;
440 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
441 str(LANG_SYSFONT_GAIN), "(dB)");
443 screens[i].putsxy(0, 0, buf);
444 } else if (mode == CUTOFF) {
445 /* cutoff */
446 setting = &global_settings.eq_band0_cutoff;
447 setting += current_band * 3;
449 step = EQ_CUTOFF_STEP;
450 fast_step = EQ_CUTOFF_FAST_STEP;
451 min = EQ_CUTOFF_MIN;
452 max = EQ_CUTOFF_MAX;
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;
468 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
469 str(LANG_EQUALIZER_BAND_Q), "");
471 screens[i].putsxy(0, 0, buf);
474 /* Draw scrollbar if needed */
475 if (nb_eq_sliders[i] != 5)
477 if (current_band == 0) {
478 start_item = 0;
479 } else if (current_band == 4) {
480 start_item = 5 - nb_eq_sliders[i];
481 } else {
482 start_item = current_band - 1;
484 x = SCROLLBAR_SIZE;
485 } else {
486 x = 1;
487 start_item = 0;
489 /* Draw equalizer band details */
490 draw_eq_sliders(&screens[i], x, y, nb_eq_sliders[i], start_item,
491 current_band, mode);
493 screens[i].update();
496 button = get_action(CONTEXT_SETTINGS_EQ,TIMEOUT_BLOCK);
498 switch (button) {
499 case ACTION_SETTINGS_DEC:
500 case ACTION_SETTINGS_DECREPEAT:
501 *(setting) -= step;
502 has_changed = true;
503 if (*(setting) < min)
504 *(setting) = min;
505 break;
507 case ACTION_SETTINGS_INC:
508 case ACTION_SETTINGS_INCREPEAT:
509 *(setting) += step;
510 has_changed = true;
511 if (*(setting) > max)
512 *(setting) = max;
513 break;
515 case ACTION_SETTINGS_INCBIGSTEP:
516 *(setting) += fast_step;
517 has_changed = true;
518 if (*(setting) > max)
519 *(setting) = max;
520 break;
522 case ACTION_SETTINGS_DECBIGSTEP:
523 *(setting) -= fast_step;
524 has_changed = true;
525 if (*(setting) < min)
526 *(setting) = min;
527 break;
529 case ACTION_STD_PREV:
530 case ACTION_STD_PREVREPEAT:
531 current_band--;
532 if (current_band < 0)
533 current_band = 4; /* wrap around */
534 break;
536 case ACTION_STD_NEXT:
537 case ACTION_STD_NEXTREPEAT:
538 current_band++;
539 if (current_band > 4)
540 current_band = 0; /* wrap around */
541 break;
543 case ACTION_STD_OK:
544 mode++;
545 if (mode > Q)
546 mode = GAIN; /* wrap around */
547 break;
549 case ACTION_STD_CANCEL:
550 exit_request = true;
551 result = false;
552 break;
553 default:
554 if(default_event_handler(button) == SYS_USB_CONNECTED) {
555 exit_request = true;
556 result = true;
558 break;
561 /* Update the filter if the user changed something */
562 if (has_changed) {
563 dsp_set_eq_coefs(current_band);
564 has_changed = false;
568 /* Reset screen settings */
569 FOR_NB_SCREENS(i)
571 screens[i].setfont(FONT_UI);
572 screens[i].clear_display();
573 screens[i].set_viewport(NULL);
574 viewportmanager_theme_undo(i, false);
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 static struct browse_folder_info eqs = { EQS_DIR, SHOW_CFG };
595 MENUITEM_FUNCTION(eq_graphical, 0, ID2P(LANG_EQUALIZER_GRAPHICAL),
596 (int(*)(void))eq_menu_graphical, NULL, lowlatency_callback,
597 Icon_EQ);
598 MENUITEM_FUNCTION(eq_save, 0, ID2P(LANG_EQUALIZER_SAVE),
599 (int(*)(void))eq_save_preset, NULL, NULL, Icon_NOICON);
600 MENUITEM_FUNCTION(eq_browse, MENU_FUNC_USEPARAM, ID2P(LANG_EQUALIZER_BROWSE),
601 browse_folder, (void*)&eqs, lowlatency_callback,
602 Icon_NOICON);
604 MAKE_MENU(equalizer_menu, ID2P(LANG_EQUALIZER), NULL, Icon_EQ,
605 &eq_enable, &eq_graphical, &eq_precut, &gain_menu,
606 &advanced_eq_menu_, &eq_save, &eq_browse);