Replace limiter with dynamic range compressor
[kugel-rb.git] / apps / gui / wps.c
blob1f585fcbff8c49bb23ae6f5780e2ba182a9657fc
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Jerome Kuptz
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 ****************************************************************************/
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include "config.h"
26 #include "system.h"
27 #include "file.h"
28 #include "lcd.h"
29 #include "font.h"
30 #include "backlight.h"
31 #include "action.h"
32 #include "kernel.h"
33 #include "filetypes.h"
34 #include "debug.h"
35 #include "sprintf.h"
36 #include "settings.h"
37 #include "skin_engine/skin_engine.h"
38 #include "mp3_playback.h"
39 #include "audio.h"
40 #include "usb.h"
41 #include "status.h"
42 #include "storage.h"
43 #include "screens.h"
44 #include "playlist.h"
45 #ifdef HAVE_LCD_BITMAP
46 #include "icons.h"
47 #include "peakmeter.h"
48 #endif
49 #include "lang.h"
50 #include "bookmark.h"
51 #include "misc.h"
52 #include "sound.h"
53 #include "onplay.h"
54 #include "abrepeat.h"
55 #include "playback.h"
56 #include "splash.h"
57 #include "cuesheet.h"
58 #include "ata_idle_notify.h"
59 #include "root_menu.h"
60 #include "backdrop.h"
61 #include "quickscreen.h"
62 #include "pitchscreen.h"
63 #include "appevents.h"
64 #include "viewport.h"
65 #include "pcmbuf.h"
66 #include "option_select.h"
67 #include "dsp.h"
68 #include "playlist_viewer.h"
69 #include "wps.h"
71 #define RESTORE_WPS_INSTANTLY 0l
72 #define RESTORE_WPS_NEXT_SECOND ((long)(HZ+current_tick))
73 /* in milliseconds */
74 #define DEFAULT_SKIP_TRESH 3000ul
77 #define FF_REWIND_MAX_PERCENT 3 /* cap ff/rewind step size at max % of file */
78 /* 3% of 30min file == 54s step size */
79 #define MIN_FF_REWIND_STEP 500
81 /* this is for the viewportmanager */
82 static int wpsbars;
83 /* currently only one wps_state is needed */
84 static struct wps_state wps_state;
85 static struct gui_wps gui_wps[NB_SCREENS];
86 static struct wps_data wps_datas[NB_SCREENS];
88 /* initial setup of wps_data */
89 static void wps_state_init(void);
90 static void track_changed_callback(void *param);
91 static void nextid3available_callback(void* param);
92 #ifdef HAVE_LCD_BITMAP
93 static void statusbar_toggle_handler(void *data);
94 #endif
96 #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps"
97 #ifdef HAVE_REMOTE_LCD
98 #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps"
99 #define DEFAULT_WPS(screen) ((screen) == SCREEN_MAIN ? \
100 WPS_DEFAULTCFG:RWPS_DEFAULTCFG)
101 #else
102 #define DEFAULT_WPS(screen) (WPS_DEFAULTCFG)
103 #endif
105 void wps_data_load(enum screen_type screen, const char *buf, bool isfile)
107 bool loaded_ok;
109 screens[screen].backdrop_unload(BACKDROP_SKIN_WPS);
111 #ifndef __PCTOOL__
113 * Hardcode loading WPS_DEFAULTCFG to cause a reset ideally this
114 * wants to be a virtual file. Feel free to modify dirbrowse()
115 * if you're feeling brave.
118 if (buf && ! strcmp(buf, DEFAULT_WPS(screen)) )
120 #ifdef HAVE_REMOTE_LCD
121 if (screen == SCREEN_REMOTE)
122 global_settings.rwps_file[0] = '\0';
123 else
124 #endif
125 global_settings.wps_file[0] = '\0';
126 buf = NULL;
129 #endif /* __PCTOOL__ */
131 loaded_ok = buf && skin_data_load(gui_wps[screen].data,
132 &screens[screen], buf, isfile);
133 if (!loaded_ok) /* load the hardcoded default */
135 char *skin_buf[NB_SCREENS] = {
136 #ifdef HAVE_LCD_BITMAP
137 "%s%?it<%?in<%in. |>%it|%fn>\n"
138 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
139 "%s%?id<%id|%?d1<%d1|(root)>> %?iy<(%iy)|>\n\n"
140 "%al%pc/%pt%ar[%pp:%pe]\n"
141 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
142 "%pb\n%pm\n",
143 #else
144 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d2> - %?id<%id|%d1>\n"
145 "%pc%?ps<*|/>%pt\n",
146 #endif
147 #ifdef HAVE_REMOTE_LCD
148 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
149 "%s%?it<%?in<%in. |>%it|%fn>\n"
150 "%al%pc/%pt%ar[%pp:%pe]\n"
151 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
152 "%pb\n",
153 #endif
155 skin_data_load(gui_wps[screen].data, &screens[screen],
156 skin_buf[screen], false);
158 #ifdef HAVE_REMOVE_LCD
159 gui_wps[screen].data->remote_wps = !(screen == SCREEN_MAIN);
160 #endif
163 void wps_data_init(enum screen_type screen)
165 skin_data_init(gui_wps[screen].data);
169 static bool wps_fading_out = false;
170 void fade(bool fade_in, bool updatewps)
172 int fp_global_vol = global_settings.volume << 8;
173 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
174 int fp_step = (fp_global_vol - fp_min_vol) / 30;
175 int i;
176 wps_fading_out = !fade_in;
177 if (fade_in) {
178 /* fade in */
179 int fp_volume = fp_min_vol;
181 /* zero out the sound */
182 sound_set_volume(fp_min_vol >> 8);
184 sleep(HZ/10); /* let audio thread run */
185 audio_resume();
187 while (fp_volume < fp_global_vol - fp_step) {
188 fp_volume += fp_step;
189 sound_set_volume(fp_volume >> 8);
190 if (updatewps)
192 FOR_NB_SCREENS(i)
193 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
195 sleep(1);
197 sound_set_volume(global_settings.volume);
199 else {
200 /* fade out */
201 int fp_volume = fp_global_vol;
203 while (fp_volume > fp_min_vol + fp_step) {
204 fp_volume -= fp_step;
205 sound_set_volume(fp_volume >> 8);
206 if (updatewps)
208 FOR_NB_SCREENS(i)
209 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
211 sleep(1);
213 audio_pause();
214 wps_fading_out = false;
215 #if CONFIG_CODEC != SWCODEC
216 #ifndef SIMULATOR
217 /* let audio thread run and wait for the mas to run out of data */
218 while (!mp3_pause_done())
219 #endif
220 sleep(HZ/10);
221 #endif
223 /* reset volume to what it was before the fade */
224 sound_set_volume(global_settings.volume);
227 bool is_wps_fading(void)
229 return wps_fading_out;
232 static bool update_onvol_change(struct gui_wps * gwps)
234 skin_update(gwps, WPS_REFRESH_NON_STATIC);
236 #ifdef HAVE_LCD_CHARCELLS
237 splashf(0, "Vol: %3d dB",
238 sound_val2phys(SOUND_VOLUME, global_settings.volume));
239 return true;
240 #endif
241 return false;
245 bool ffwd_rew(int button)
247 unsigned int step = 0; /* current ff/rewind step */
248 unsigned int max_step = 0; /* maximum ff/rewind step */
249 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
250 int direction = -1; /* forward=1 or backward=-1 */
251 bool exit = false;
252 bool usb = false;
253 int i = 0;
254 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
256 if (button == ACTION_NONE)
258 status_set_ffmode(0);
259 return usb;
261 while (!exit)
263 switch ( button )
265 case ACTION_WPS_SEEKFWD:
266 direction = 1;
267 case ACTION_WPS_SEEKBACK:
268 if (wps_state.ff_rewind)
270 if (direction == 1)
272 /* fast forwarding, calc max step relative to end */
273 max_step = (wps_state.id3->length -
274 (wps_state.id3->elapsed +
275 ff_rewind_count)) *
276 FF_REWIND_MAX_PERCENT / 100;
278 else
280 /* rewinding, calc max step relative to start */
281 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
282 FF_REWIND_MAX_PERCENT / 100;
285 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
287 if (step > max_step)
288 step = max_step;
290 ff_rewind_count += step * direction;
292 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
293 step += step >> ff_rw_accel;
295 else
297 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
298 wps_state.id3 && wps_state.id3->length )
300 if (!wps_state.paused)
301 #if (CONFIG_CODEC == SWCODEC)
302 audio_pre_ff_rewind();
303 #else
304 audio_pause();
305 #endif
306 #if CONFIG_KEYPAD == PLAYER_PAD
307 FOR_NB_SCREENS(i)
308 gui_wps[i].display->stop_scroll();
309 #endif
310 if (direction > 0)
311 status_set_ffmode(STATUS_FASTFORWARD);
312 else
313 status_set_ffmode(STATUS_FASTBACKWARD);
315 wps_state.ff_rewind = true;
317 step = 1000 * global_settings.ff_rewind_min_step;
319 else
320 break;
323 if (direction > 0) {
324 if ((wps_state.id3->elapsed + ff_rewind_count) >
325 wps_state.id3->length)
326 ff_rewind_count = wps_state.id3->length -
327 wps_state.id3->elapsed;
329 else {
330 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
331 ff_rewind_count = -wps_state.id3->elapsed;
334 /* set the wps state ff_rewind_count so the progess info
335 displays corectly */
336 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
337 ff_rewind_count:-ff_rewind_count;
338 FOR_NB_SCREENS(i)
340 skin_update(&gui_wps[i],
341 WPS_REFRESH_PLAYER_PROGRESS |
342 WPS_REFRESH_DYNAMIC);
345 break;
347 case ACTION_WPS_STOPSEEK:
348 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
349 audio_ff_rewind(wps_state.id3->elapsed);
350 wps_state.ff_rewind_count = 0;
351 wps_state.ff_rewind = false;
352 status_set_ffmode(0);
353 #if (CONFIG_CODEC != SWCODEC)
354 if (!wps_state.paused)
355 audio_resume();
356 #endif
357 #ifdef HAVE_LCD_CHARCELLS
358 FOR_NB_SCREENS(i)
359 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
360 #endif
361 exit = true;
362 break;
364 default:
365 if(default_event_handler(button) == SYS_USB_CONNECTED) {
366 status_set_ffmode(0);
367 usb = true;
368 exit = true;
370 break;
372 if (!exit)
374 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
375 #ifdef HAVE_TOUCHSCREEN
376 if (button == ACTION_TOUCHSCREEN)
377 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
378 #endif
381 return usb;
385 void display_keylock_text(bool locked)
387 int i;
388 FOR_NB_SCREENS(i)
389 gui_wps[i].display->stop_scroll();
391 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
397 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
398 static void gwps_caption_backlight(struct wps_state *state)
400 if (state && state->id3)
402 #ifdef HAVE_BACKLIGHT
403 if (global_settings.caption_backlight)
405 /* turn on backlight n seconds before track ends, and turn it off n
406 seconds into the new track. n == backlight_timeout, or 5s */
407 int n = global_settings.backlight_timeout * 1000;
409 if ( n < 1000 )
410 n = 5000; /* use 5s if backlight is always on or off */
412 if (((state->id3->elapsed < 1000) ||
413 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
414 (state->paused == false))
415 backlight_on();
417 #endif
418 #ifdef HAVE_REMOTE_LCD
419 if (global_settings.remote_caption_backlight)
421 /* turn on remote backlight n seconds before track ends, and turn it
422 off n seconds into the new track. n == remote_backlight_timeout,
423 or 5s */
424 int n = global_settings.remote_backlight_timeout * 1000;
426 if ( n < 1000 )
427 n = 5000; /* use 5s if backlight is always on or off */
429 if (((state->id3->elapsed < 1000) ||
430 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
431 (state->paused == false))
432 remote_backlight_on();
434 #endif
437 #endif
440 static void change_dir(int direction)
442 if (global_settings.prevent_skip)
443 return;
445 if (direction < 0)
446 audio_prev_dir();
447 else if (direction > 0)
448 audio_next_dir();
449 /* prevent the next dir to immediatly start being ffw'd */
450 action_wait_for_release();
453 static void prev_track(unsigned long skip_thresh)
455 if (wps_state.id3->elapsed < skip_thresh)
457 audio_prev();
458 return;
460 else
462 if (wps_state.id3->cuesheet)
464 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
465 return;
468 if (!wps_state.paused)
469 #if (CONFIG_CODEC == SWCODEC)
470 audio_pre_ff_rewind();
471 #else
472 audio_pause();
473 #endif
475 audio_ff_rewind(0);
477 #if (CONFIG_CODEC != SWCODEC)
478 if (!wps_state.paused)
479 audio_resume();
480 #endif
484 static void next_track(void)
486 /* take care of if we're playing a cuesheet */
487 if (wps_state.id3->cuesheet)
489 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
491 /* if the result was false, then we really want
492 to skip to the next track */
493 return;
497 audio_next();
500 static void play_hop(int direction)
502 unsigned long step = ((unsigned long)global_settings.skip_length)*1000;
503 unsigned long elapsed = wps_state.id3->elapsed;
504 unsigned long remaining = wps_state.id3->length - elapsed;
506 if (!global_settings.prevent_skip &&
507 (!step ||
508 (direction > 0 && step >= remaining) ||
509 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
510 { /* Do normal track skipping */
511 if (direction > 0)
512 next_track();
513 else if (direction < 0)
514 prev_track(DEFAULT_SKIP_TRESH);
515 return;
518 if (direction == 1 && step >= remaining)
520 #if CONFIG_CODEC == SWCODEC
521 if(global_settings.beep)
522 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
523 #endif
524 return;
526 else if ((direction == -1 && elapsed < step))
528 elapsed = 0;
530 else
532 elapsed += step * direction;
534 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
536 #if (CONFIG_CODEC == SWCODEC)
537 audio_pre_ff_rewind();
538 #else
539 audio_pause();
540 #endif
542 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
543 #if (CONFIG_CODEC != SWCODEC)
544 if (!wps_state.paused)
545 audio_resume();
546 #endif
550 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
552 * If the user is unable to see the wps, because the display is deactivated,
553 * we suppress updates until the wps is activated again (the lcd driver will
554 * call this hook to issue an instant update)
555 * */
556 static void wps_lcd_activation_hook(void)
558 wps_state.do_full_update = true;
559 /* force timeout in wps main loop, so that the update is instantly */
560 queue_post(&button_queue, BUTTON_NONE, 0);
562 #endif
564 static void gwps_leave_wps(void)
566 int i, oldbars = VP_SB_HIDE_ALL;
568 FOR_NB_SCREENS(i)
570 gui_wps[i].display->stop_scroll();
571 gui_wps[i].display->backdrop_show(BACKDROP_MAIN);
572 if (statusbar_position(i) != STATUSBAR_OFF)
573 oldbars |= VP_SB_ONSCREEN(i);
576 viewportmanager_set_statusbar(oldbars);
577 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
578 /* Play safe and unregister the hook */
579 lcd_activation_set_hook(NULL);
580 #endif
581 send_event(GUI_EVENT_REFRESH, NULL);
584 #ifdef HAVE_TOUCHSCREEN
585 int wps_get_touchaction(struct wps_data *data)
587 short x,y;
588 short vx, vy;
589 int type = action_get_touchscreen_press(&x, &y);
590 static int last_action = ACTION_NONE;
591 struct touchregion *r;
592 bool repeated = (type == BUTTON_REPEAT);
593 bool released = (type == BUTTON_REL);
594 struct skin_token_list *regions = data->touchregions;
595 while (regions)
597 r = (struct touchregion *)regions->token->value.data;
598 /* make sure this region's viewport is visible */
599 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
601 regions = regions->next;
602 continue;
604 /* check if it's inside this viewport */
605 if (viewport_point_within_vp(&(r->wvp->vp), x, y))
606 { /* reposition the touch inside the viewport since touchregions
607 * are relative to a preceding viewport */
608 vx = x - r->wvp->vp.x;
609 vy = y - r->wvp->vp.y;
610 /* now see if the point is inside this region */
611 if (vx >= r->x && vx < r->x+r->width &&
612 vy >= r->y && vy < r->y+r->height)
614 /* reposition the touch within the area */
615 vx -= r->x;
616 vy -= r->y;
618 switch(r->type)
620 case WPS_TOUCHREGION_ACTION:
621 if ((repeated && r->repeat) || (released && !r->repeat))
623 last_action = r->action;
624 return r->action;
626 break;
627 case WPS_TOUCHREGION_SCROLLBAR:
628 if(r->width > r->height)
629 /* landscape */
630 wps_state.id3->elapsed = (vx *
631 wps_state.id3->length) / r->width;
632 else
633 /* portrait */
634 wps_state.id3->elapsed = (vy *
635 wps_state.id3->length) / r->height;
637 if (!wps_state.paused)
638 #if (CONFIG_CODEC == SWCODEC)
639 audio_pre_ff_rewind();
640 #else
641 audio_pause();
642 #endif
643 audio_ff_rewind(wps_state.id3->elapsed);
644 #if (CONFIG_CODEC != SWCODEC)
645 if (!wps_state.paused)
646 audio_resume();
647 #endif
648 break;
649 case WPS_TOUCHREGION_VOLUME:
651 int i;
652 const int min_vol = sound_min(SOUND_VOLUME);
653 const int max_vol = sound_max(SOUND_VOLUME);
654 if(r->width > r->height)
655 /* landscape */
656 global_settings.volume = (vx *
657 (max_vol - min_vol)) / r->width;
658 else
659 /* portrait */
660 global_settings.volume = (vy *
661 (max_vol-min_vol)) / r->height;
663 global_settings.volume += min_vol;
664 setvol();
665 FOR_NB_SCREENS(i)
667 gui_wps[i].data->button_time_volume = current_tick;
669 return ACTION_REDRAW;
674 regions = regions->next;
677 if ((last_action == ACTION_WPS_SEEKBACK || last_action == ACTION_WPS_SEEKFWD))
678 return ACTION_WPS_STOPSEEK;
679 last_action = ACTION_TOUCHSCREEN;
680 return ACTION_TOUCHSCREEN;
682 #endif
683 /* The WPS can be left in two ways:
684 * a) call a function, which draws over the wps. In this case, the wps
685 * will be still active (i.e. the below function didn't return)
686 * b) return with a value evaluated by root_menu.c, in this case the wps
687 * is really left, and root_menu will handle the next screen
689 * In either way, call gwps_leave_wps(), in order to restore the correct
690 * "main screen" backdrops and statusbars
692 long gui_wps_show(void)
694 long button = 0;
695 bool restore = true;
696 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
697 bool exit = false;
698 bool bookmark = false;
699 bool update = false;
700 int i;
701 long last_left = 0, last_right = 0;
703 #ifdef HAVE_LCD_CHARCELLS
704 status_set_audio(true);
705 status_set_param(false);
706 #else
707 statusbar_toggle_handler(NULL);
708 #endif
710 #ifdef AB_REPEAT_ENABLE
711 ab_repeat_init();
712 ab_reset_markers();
713 #endif
714 wps_state_init();
716 while ( 1 )
718 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
720 /* did someone else (i.e power thread) change audio pause mode? */
721 if (wps_state.paused != audio_paused) {
722 wps_state.paused = audio_paused;
724 /* if another thread paused audio, we are probably in car mode,
725 about to shut down. lets save the settings. */
726 if (wps_state.paused) {
727 settings_save();
728 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
729 call_storage_idle_notifys(true);
730 #endif
734 #ifdef HAVE_LCD_BITMAP
735 /* when the peak meter is enabled we want to have a
736 few extra updates to make it look smooth. On the
737 other hand we don't want to waste energy if it
738 isn't displayed */
739 bool pm=false;
740 FOR_NB_SCREENS(i)
742 if(gui_wps[i].data->peak_meter_enabled)
743 pm = true;
746 if (pm) {
747 long next_refresh = current_tick;
748 long next_big_refresh = current_tick + HZ / 5;
749 button = BUTTON_NONE;
750 while (TIME_BEFORE(current_tick, next_big_refresh)) {
751 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_NOBLOCK);
752 if (button != ACTION_NONE) {
753 break;
755 peak_meter_peek();
756 sleep(0); /* Sleep until end of current tick. */
758 if (TIME_AFTER(current_tick, next_refresh)) {
759 FOR_NB_SCREENS(i)
761 if(gui_wps[i].data->peak_meter_enabled)
762 skin_update(&gui_wps[i], WPS_REFRESH_PEAK_METER);
763 next_refresh += HZ / PEAK_METER_FPS;
770 /* The peak meter is disabled
771 -> no additional screen updates needed */
772 else
773 #endif
775 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,
776 restore ? HZ/100 : HZ/5);
779 /* Exit if audio has stopped playing. This happens e.g. at end of
780 playlist or if using the sleep timer. */
781 if (!(audio_status() & AUDIO_STATUS_PLAY))
782 exit = true;
783 #ifdef HAVE_TOUCHSCREEN
784 if (button == ACTION_TOUCHSCREEN)
785 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
786 #endif
787 /* The iPods/X5/M5 use a single button for the A-B mode markers,
788 defined as ACTION_WPSAB_SINGLE in their config files. */
789 #ifdef ACTION_WPSAB_SINGLE
790 if (!global_settings.party_mode && ab_repeat_mode_enabled())
792 static int wps_ab_state = 0;
793 if (button == ACTION_WPSAB_SINGLE)
795 switch (wps_ab_state)
797 case 0: /* set the A spot */
798 button = ACTION_WPS_ABSETA_PREVDIR;
799 break;
800 case 1: /* set the B spot */
801 button = ACTION_WPS_ABSETB_NEXTDIR;
802 break;
803 case 2:
804 button = ACTION_WPS_ABRESET;
805 break;
807 wps_ab_state = (wps_ab_state+1) % 3;
810 #endif
811 switch(button)
813 case ACTION_WPS_CONTEXT:
815 gwps_leave_wps();
816 /* if music is stopped in the context menu we want to exit the wps */
817 if (onplay(wps_state.id3->path,
818 FILE_ATTR_AUDIO, CONTEXT_WPS) == ONPLAY_MAINMENU
819 || !audio_status())
820 return GO_TO_ROOT;
821 restore = true;
823 break;
825 case ACTION_WPS_BROWSE:
826 #ifdef HAVE_LCD_CHARCELLS
827 status_set_record(false);
828 status_set_audio(false);
829 #endif
830 gwps_leave_wps();
831 return GO_TO_PREVIOUS_BROWSER;
832 break;
834 /* play/pause */
835 case ACTION_WPS_PLAY:
836 if (global_settings.party_mode)
837 break;
838 if ( wps_state.paused )
840 wps_state.paused = false;
841 if ( global_settings.fade_on_stop )
842 fade(true, true);
843 else
844 audio_resume();
846 else
848 wps_state.paused = true;
849 if ( global_settings.fade_on_stop )
850 fade(false, true);
851 else
852 audio_pause();
853 settings_save();
854 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
855 call_storage_idle_notifys(true); /* make sure resume info is saved */
856 #endif
858 break;
860 case ACTION_WPS_VOLUP:
862 FOR_NB_SCREENS(i)
863 gui_wps[i].data->button_time_volume = current_tick;
864 global_settings.volume++;
865 bool res = false;
866 setvol();
867 FOR_NB_SCREENS(i)
869 if(update_onvol_change(&gui_wps[i]))
870 res = true;
872 if (res) {
873 restore = true;
874 restoretimer = RESTORE_WPS_NEXT_SECOND;
877 break;
878 case ACTION_WPS_VOLDOWN:
880 FOR_NB_SCREENS(i)
881 gui_wps[i].data->button_time_volume = current_tick;
882 global_settings.volume--;
883 setvol();
884 bool res = false;
885 FOR_NB_SCREENS(i)
887 if(update_onvol_change(&gui_wps[i]))
888 res = true;
890 if (res) {
891 restore = true;
892 restoretimer = RESTORE_WPS_NEXT_SECOND;
895 break;
896 /* fast forward
897 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
898 case ACTION_WPS_SEEKFWD:
899 if (global_settings.party_mode)
900 break;
901 if (current_tick -last_right < HZ)
903 if (wps_state.id3->cuesheet)
905 audio_next();
907 else
909 change_dir(1);
912 else
913 ffwd_rew(ACTION_WPS_SEEKFWD);
914 last_right = last_left = 0;
915 break;
916 /* fast rewind
917 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
918 case ACTION_WPS_SEEKBACK:
919 if (global_settings.party_mode)
920 break;
921 if (current_tick -last_left < HZ)
923 if (wps_state.id3->cuesheet)
925 if (!wps_state.paused)
926 #if (CONFIG_CODEC == SWCODEC)
927 audio_pre_ff_rewind();
928 #else
929 audio_pause();
930 #endif
931 audio_ff_rewind(0);
933 else
935 change_dir(-1);
938 else
939 ffwd_rew(ACTION_WPS_SEEKBACK);
940 last_left = last_right = 0;
941 break;
943 /* prev / restart */
944 case ACTION_WPS_SKIPPREV:
945 if (global_settings.party_mode)
946 break;
947 last_left = current_tick;
948 #ifdef AB_REPEAT_ENABLE
949 /* if we're in A/B repeat mode and the current position
950 is past the A marker, jump back to the A marker... */
951 if ( ab_repeat_mode_enabled() )
953 if ( ab_after_A_marker(wps_state.id3->elapsed) )
955 ab_jump_to_A_marker();
956 break;
957 #if (AB_REPEAT_ENABLE == 2)
958 } else {
959 ab_reset_markers();
960 #endif
963 else
964 /* ...otherwise, do it normally */
965 #endif
966 play_hop(-1);
967 break;
969 /* next
970 OR if skip length set, hop by predetermined amount. */
971 case ACTION_WPS_SKIPNEXT:
972 if (global_settings.party_mode)
973 break;
974 last_right = current_tick;
975 #ifdef AB_REPEAT_ENABLE
976 /* if we're in A/B repeat mode and the current position is
977 before the A marker, jump to the A marker... */
978 if ( ab_repeat_mode_enabled() )
980 if ( ab_before_A_marker(wps_state.id3->elapsed) )
982 ab_jump_to_A_marker();
983 break;
984 #if (AB_REPEAT_ENABLE == 2)
985 } else {
986 ab_reset_markers();
987 #endif
990 else
991 /* ...otherwise, do it normally */
992 #endif
993 play_hop(1);
994 break;
995 /* next / prev directories */
996 /* and set A-B markers if in a-b mode */
997 case ACTION_WPS_ABSETB_NEXTDIR:
998 if (global_settings.party_mode)
999 break;
1000 #if defined(AB_REPEAT_ENABLE)
1001 if (ab_repeat_mode_enabled())
1003 ab_set_B_marker(wps_state.id3->elapsed);
1004 ab_jump_to_A_marker();
1006 else
1007 #endif
1009 change_dir(1);
1011 break;
1012 case ACTION_WPS_ABSETA_PREVDIR:
1013 if (global_settings.party_mode)
1014 break;
1015 #if defined(AB_REPEAT_ENABLE)
1016 if (ab_repeat_mode_enabled())
1017 ab_set_A_marker(wps_state.id3->elapsed);
1018 else
1019 #endif
1021 change_dir(-1);
1023 break;
1024 /* menu key functions */
1025 case ACTION_WPS_MENU:
1026 gwps_leave_wps();
1027 return GO_TO_ROOT;
1028 break;
1031 #ifdef HAVE_QUICKSCREEN
1032 case ACTION_WPS_QUICKSCREEN:
1034 gwps_leave_wps();
1035 if (quick_screen_quick(button))
1036 return GO_TO_ROOT;
1037 restore = true;
1039 break;
1040 #endif /* HAVE_QUICKSCREEN */
1042 /* screen settings */
1043 #ifdef BUTTON_F3
1044 case ACTION_F3:
1046 gwps_leave_wps();
1047 if (quick_screen_f3(BUTTON_F3))
1048 return GO_TO_ROOT;
1049 restore = true;
1051 break;
1052 #endif /* BUTTON_F3 */
1054 /* pitch screen */
1055 #ifdef HAVE_PITCHSCREEN
1056 case ACTION_WPS_PITCHSCREEN:
1058 gwps_leave_wps();
1059 if (1 == gui_syncpitchscreen_run())
1060 return GO_TO_ROOT;
1061 restore = true;
1063 break;
1064 #endif /* HAVE_PITCHSCREEN */
1066 #ifdef AB_REPEAT_ENABLE
1067 /* reset A&B markers */
1068 case ACTION_WPS_ABRESET:
1069 if (ab_repeat_mode_enabled())
1071 ab_reset_markers();
1072 update = true;
1074 break;
1075 #endif /* AB_REPEAT_ENABLE */
1077 /* stop and exit wps */
1078 case ACTION_WPS_STOP:
1079 if (global_settings.party_mode)
1080 break;
1081 bookmark = true;
1082 exit = true;
1083 break;
1085 case ACTION_WPS_ID3SCREEN:
1087 gwps_leave_wps();
1088 if (browse_id3())
1089 return GO_TO_ROOT;
1090 restore = true;
1092 break;
1093 #ifdef HAVE_TOUCHSCREEN
1094 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1096 global_settings.playlist_shuffle =
1097 !global_settings.playlist_shuffle;
1098 #if CONFIG_CODEC == SWCODEC
1099 dsp_set_replaygain();
1100 #endif
1101 if (global_settings.playlist_shuffle)
1102 playlist_randomise(NULL, current_tick, true);
1103 else
1104 playlist_sort(NULL, true);
1106 break;
1107 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1109 const struct settings_list *rep_setting =
1110 find_setting(&global_settings.repeat_mode, NULL);
1111 option_select_next_val(rep_setting, false, true);
1112 audio_flush_and_reload_tracks();
1114 break;
1115 #endif /* HAVE_TOUCHSCREEN */
1116 /* this case is used by the softlock feature
1117 * it requests a full update here */
1118 case ACTION_REDRAW:
1119 wps_state.do_full_update = true;
1120 break;
1121 case ACTION_NONE: /* Timeout, do a partial update */
1122 update = true;
1123 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1124 break;
1125 #ifdef HAVE_RECORDING
1126 case ACTION_WPS_REC:
1127 exit = true;
1128 break;
1129 #endif
1130 case SYS_POWEROFF:
1131 default_event_handler(SYS_POWEROFF);
1132 break;
1133 case ACTION_WPS_VIEW_PLAYLIST:
1134 gwps_leave_wps();
1135 if (playlist_viewer()) /* true if USB connected */
1136 return GO_TO_ROOT;
1137 restore = true;
1138 break;
1139 default:
1140 if(default_event_handler(button) == SYS_USB_CONNECTED)
1142 gwps_leave_wps();
1143 return GO_TO_ROOT;
1145 update = true;
1146 break;
1149 if (wps_state.do_full_update || update)
1151 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1152 gwps_caption_backlight(&wps_state);
1153 #endif
1154 FOR_NB_SCREENS(i)
1156 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1157 if (lcd_active()
1158 #ifdef HAVE_REMOTE_LCD
1159 /* currently, all remotes are readable without backlight
1160 * so still update those */
1161 || (i == SCREEN_REMOTE)
1162 #endif
1164 #endif
1166 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
1169 wps_state.do_full_update = false;
1170 update = false;
1173 if (restore && wps_state.id3 &&
1174 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1175 TIME_AFTER(current_tick, restoretimer)))
1177 restore = false;
1178 restoretimer = RESTORE_WPS_INSTANTLY;
1179 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1180 lcd_activation_set_hook(wps_lcd_activation_hook);
1181 #endif
1182 FOR_NB_SCREENS(i)
1184 screens[i].stop_scroll();
1185 gui_wps_display(&gui_wps[i]);
1189 if (exit) {
1190 #ifdef HAVE_LCD_CHARCELLS
1191 status_set_record(false);
1192 status_set_audio(false);
1193 #endif
1194 if (global_settings.fade_on_stop)
1195 fade(false, true);
1197 if (bookmark)
1198 bookmark_autobookmark();
1199 audio_stop();
1200 #ifdef AB_REPEAT_ENABLE
1201 ab_reset_markers();
1202 #endif
1203 gwps_leave_wps();
1204 #ifdef HAVE_RECORDING
1205 if (button == ACTION_WPS_REC)
1206 return GO_TO_RECSCREEN;
1207 #endif
1208 if (global_settings.browse_current)
1209 return GO_TO_PREVIOUS_BROWSER;
1210 return GO_TO_PREVIOUS;
1213 if (button && !IS_SYSEVENT(button) )
1214 storage_spin();
1216 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1219 /* this is called from the playback thread so NO DRAWING! */
1220 static void track_changed_callback(void *param)
1222 wps_state.id3 = (struct mp3entry*)param;
1223 wps_state.nid3 = audio_next_track();
1224 if (wps_state.id3->cuesheet)
1226 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1227 cue_spoof_id3(wps_state.id3->cuesheet, wps_state.id3);
1229 wps_state.do_full_update = true;
1231 static void nextid3available_callback(void* param)
1233 (void)param;
1234 wps_state.nid3 = audio_next_track();
1235 wps_state.do_full_update = true;
1239 static void wps_state_init(void)
1241 wps_state.ff_rewind = false;
1242 wps_state.paused = false;
1243 if(audio_status() & AUDIO_STATUS_PLAY)
1245 wps_state.id3 = audio_current_track();
1246 wps_state.nid3 = audio_next_track();
1248 else
1250 wps_state.id3 = NULL;
1251 wps_state.nid3 = NULL;
1253 /* We'll be updating due to restore initialized with true */
1254 wps_state.do_full_update = false;
1255 /* add the WPS track event callbacks */
1256 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1257 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1261 #ifdef HAVE_LCD_BITMAP
1262 static void statusbar_toggle_handler(void *data)
1264 (void)data;
1265 int i;
1267 wpsbars = VP_SB_HIDE_ALL;
1268 FOR_NB_SCREENS(i)
1269 { /* fix viewports if needed */
1270 skin_statusbar_changed(&gui_wps[i]);
1272 bool draw = false;
1274 /* fix up gui_wps::statusbars, so that the viewportmanager accepts it*/
1275 if (gui_wps[i].data->wps_sb_tag)
1276 draw = gui_wps[i].data->show_sb_on_wps;
1277 else if (statusbar_position(i) != STATUSBAR_OFF)
1278 draw = true;
1279 if (draw)
1280 wpsbars |=
1281 (VP_SB_ONSCREEN(i) | VP_SB_IGNORE_SETTING(i));
1284 #endif
1286 void gui_sync_wps_init(void)
1288 int i;
1289 FOR_NB_SCREENS(i)
1291 skin_data_init(&wps_datas[i]);
1292 #ifdef HAVE_ALBUMART
1293 wps_datas[i].albumart = NULL;
1294 #endif
1295 #ifdef HAVE_REMOTE_LCD
1296 wps_datas[i].remote_wps = (i == SCREEN_REMOTE);
1297 #endif
1298 gui_wps[i].data = &wps_datas[i];
1299 gui_wps[i].display = &screens[i];
1300 /* Currently no seperate wps_state needed/possible
1301 so use the only available ( "global" ) one */
1302 gui_wps[i].state = &wps_state;
1303 gui_wps[i].display->backdrop_unload(BACKDROP_SKIN_WPS);
1304 /* only one wpsbars needed/wanted */
1305 gui_wps[i].statusbars = &wpsbars;
1307 *(gui_wps[SCREEN_MAIN].statusbars) =VP_SB_ALLSCREENS;
1308 #ifdef HAVE_LCD_BITMAP
1309 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggle_handler);
1310 #endif
1313 #ifdef HAVE_ALBUMART
1314 bool wps_uses_albumart(int *width, int *height)
1316 int i;
1317 FOR_NB_SCREENS(i) {
1318 struct gui_wps *gwps = &gui_wps[i];
1319 struct skin_albumart *aa = gwps->data->albumart;
1320 if (aa && (aa->wps_uses_albumart != WPS_ALBUMART_NONE))
1322 if (width)
1323 *width = aa->albumart_max_width;
1324 if (height)
1325 *height = aa->albumart_max_height;
1326 return true;
1329 return false;
1331 #endif
1334 #ifdef IPOD_ACCESSORY_PROTOCOL
1335 int wps_get_ff_rewind_count(void)
1337 return wps_state.ff_rewind_count;
1339 #endif