Touchregion support for the Base Skin and FM Skins. display obviously needs to be...
[kugel-rb.git] / apps / gui / wps.c
blobdafc1cd2784cf5a85eef3a07ff42b73a7ed407f3
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 "settings.h"
35 #include "skin_engine/skin_engine.h"
36 #include "mp3_playback.h"
37 #include "audio.h"
38 #include "usb.h"
39 #include "status.h"
40 #include "storage.h"
41 #include "screens.h"
42 #include "playlist.h"
43 #ifdef HAVE_LCD_BITMAP
44 #include "icons.h"
45 #include "peakmeter.h"
46 #endif
47 #include "lang.h"
48 #include "bookmark.h"
49 #include "misc.h"
50 #include "sound.h"
51 #include "onplay.h"
52 #include "abrepeat.h"
53 #include "playback.h"
54 #include "splash.h"
55 #include "cuesheet.h"
56 #include "ata_idle_notify.h"
57 #include "root_menu.h"
58 #include "backdrop.h"
59 #include "quickscreen.h"
60 #include "pitchscreen.h"
61 #include "appevents.h"
62 #include "viewport.h"
63 #include "pcmbuf.h"
64 #include "option_select.h"
65 #include "dsp.h"
66 #include "playlist_viewer.h"
67 #include "wps.h"
68 #include "statusbar-skinned.h"
70 #define RESTORE_WPS_INSTANTLY 0l
71 #define RESTORE_WPS_NEXT_SECOND ((long)(HZ+current_tick))
73 #define FF_REWIND_MAX_PERCENT 3 /* cap ff/rewind step size at max % of file */
74 /* 3% of 30min file == 54s step size */
75 #define MIN_FF_REWIND_STEP 500
77 /* currently only one wps_state is needed, initialize to 0 */
78 struct wps_state wps_state = { .id3 = NULL };
79 static struct gui_wps gui_wps[NB_SCREENS] = {{ .data = NULL }};
80 static struct wps_data wps_datas[NB_SCREENS] = {{ .wps_loaded = 0 }};
81 static struct wps_sync_data wps_sync_data = { .do_full_update = false };
83 /* initial setup of wps_data */
84 static void wps_state_init(void);
85 static void track_changed_callback(void *param);
86 static void nextid3available_callback(void* param);
88 #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps"
89 #ifdef HAVE_REMOTE_LCD
90 #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps"
91 #define DEFAULT_WPS(screen) ((screen) == SCREEN_MAIN ? \
92 WPS_DEFAULTCFG:RWPS_DEFAULTCFG)
93 #else
94 #define DEFAULT_WPS(screen) (WPS_DEFAULTCFG)
95 #endif
97 void wps_data_load(enum screen_type screen, const char *buf, bool isfile)
99 bool loaded_ok;
101 #ifndef __PCTOOL__
103 * Hardcode loading WPS_DEFAULTCFG to cause a reset ideally this
104 * wants to be a virtual file. Feel free to modify dirbrowse()
105 * if you're feeling brave.
108 if (buf && ! strcmp(buf, DEFAULT_WPS(screen)) )
110 #ifdef HAVE_REMOTE_LCD
111 if (screen == SCREEN_REMOTE)
112 global_settings.rwps_file[0] = '\0';
113 else
114 #endif
115 global_settings.wps_file[0] = '\0';
116 buf = NULL;
119 #endif /* __PCTOOL__ */
121 loaded_ok = buf && skin_data_load(screen, gui_wps[screen].data, buf, isfile);
123 if (!loaded_ok) /* load the hardcoded default */
125 char *skin_buf[NB_SCREENS] = {
126 #ifdef HAVE_LCD_BITMAP
127 #if LCD_DEPTH > 1
128 "%X(d)\n"
129 #endif
130 "%s%?it<%?in<%in. |>%it|%fn>\n"
131 "%s%?ia<%ia|%?d(2)<%d(2)|%(root%)>>\n"
132 "%s%?id<%id|%?d(1)<%d(1)|%(root%)>> %?iy<%(%iy%)|>\n\n"
133 "%al%pc/%pt%ar[%pp:%pe]\n"
134 "%fbkBit %?fv<avg|> %?iv<%(id3v%iv%)|%(no id3%)>\n"
135 "%pb\n%pm\n",
136 #else
137 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d(2)> - %?id<%id|%d(1)>\n"
138 "%pc%?ps<*|/>%pt\n",
139 #endif
140 #ifdef HAVE_REMOTE_LCD
141 #if LCD_REMOTE_DEPTH > 1
142 "%X(d)\n"
143 #endif
144 "%s%?ia<%ia|%?d(2)<%d(2)|%(root%)>>\n"
145 "%s%?it<%?in<%in. |>%it|%fn>\n"
146 "%al%pc/%pt%ar[%pp:%pe]\n"
147 "%fbkBit %?fv<avg|> %?iv<%(id3v%iv%)|%(no id3%)>\n"
148 "%pb\n",
149 #endif
151 skin_data_load(screen, gui_wps[screen].data, skin_buf[screen], false);
155 void fade(bool fade_in, bool updatewps)
157 int fp_global_vol = global_settings.volume << 8;
158 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
159 int fp_step = (fp_global_vol - fp_min_vol) / 30;
160 int i;
161 wps_state.is_fading = !fade_in;
162 if (fade_in) {
163 /* fade in */
164 int fp_volume = fp_min_vol;
166 /* zero out the sound */
167 sound_set_volume(fp_min_vol >> 8);
169 sleep(HZ/10); /* let audio thread run */
170 audio_resume();
172 while (fp_volume < fp_global_vol - fp_step) {
173 fp_volume += fp_step;
174 sound_set_volume(fp_volume >> 8);
175 if (updatewps)
177 FOR_NB_SCREENS(i)
178 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
180 sleep(1);
182 sound_set_volume(global_settings.volume);
184 else {
185 /* fade out */
186 int fp_volume = fp_global_vol;
188 while (fp_volume > fp_min_vol + fp_step) {
189 fp_volume -= fp_step;
190 sound_set_volume(fp_volume >> 8);
191 if (updatewps)
193 FOR_NB_SCREENS(i)
194 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
196 sleep(1);
198 audio_pause();
199 wps_state.is_fading = false;
200 #if CONFIG_CODEC != SWCODEC
201 #ifndef SIMULATOR
202 /* let audio thread run and wait for the mas to run out of data */
203 while (!mp3_pause_done())
204 #endif
205 sleep(HZ/10);
206 #endif
208 /* reset volume to what it was before the fade */
209 sound_set_volume(global_settings.volume);
213 static bool update_onvol_change(struct gui_wps * gwps)
215 skin_update(gwps, WPS_REFRESH_NON_STATIC);
217 #ifdef HAVE_LCD_CHARCELLS
218 splashf(0, "Vol: %3d dB",
219 sound_val2phys(SOUND_VOLUME, global_settings.volume));
220 return true;
221 #endif
222 return false;
226 #ifdef HAVE_TOUCHSCREEN
227 int skintouch_to_wps(struct wps_data *data)
229 int offset = 0;
230 int button = skin_get_touchaction(data, &offset);
231 switch (button)
233 case ACTION_STD_PREV:
234 return ACTION_WPS_SKIPPREV;
235 case ACTION_STD_PREVREPEAT:
236 return ACTION_WPS_SEEKBACK;
237 case ACTION_STD_NEXT:
238 return ACTION_WPS_SKIPNEXT;
239 case ACTION_STD_NEXTREPEAT:
240 return ACTION_WPS_SEEKFWD;
241 case ACTION_STD_MENU:
242 return ACTION_WPS_MENU;
243 case ACTION_STD_CONTEXT:
244 return ACTION_WPS_CONTEXT;
245 case ACTION_STD_QUICKSCREEN:
246 return ACTION_WPS_QUICKSCREEN;
247 case WPS_TOUCHREGION_SCROLLBAR:
248 wps_state.id3->elapsed = wps_state.id3->length*offset/100;
249 if (!wps_state.paused)
250 #if (CONFIG_CODEC == SWCODEC)
251 audio_pre_ff_rewind();
252 #else
253 audio_pause();
254 #endif
255 audio_ff_rewind(wps_state.id3->elapsed);
256 #if (CONFIG_CODEC != SWCODEC)
257 if (!wps_state.paused)
258 audio_resume();
259 #endif
260 return ACTION_TOUCHSCREEN;
261 case WPS_TOUCHREGION_VOLUME:
263 const int min_vol = sound_min(SOUND_VOLUME);
264 const int max_vol = sound_max(SOUND_VOLUME);
265 global_settings.volume = (offset * (max_vol - min_vol)) / 100;
266 global_settings.volume += min_vol;
267 setvol();
269 return ACTION_TOUCHSCREEN;
271 return button;
273 #endif
275 bool ffwd_rew(int button)
277 unsigned int step = 0; /* current ff/rewind step */
278 unsigned int max_step = 0; /* maximum ff/rewind step */
279 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
280 int direction = -1; /* forward=1 or backward=-1 */
281 bool exit = false;
282 bool usb = false;
283 int i = 0;
284 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
286 if (button == ACTION_NONE)
288 status_set_ffmode(0);
289 return usb;
291 while (!exit)
293 switch ( button )
295 case ACTION_WPS_SEEKFWD:
296 direction = 1;
297 case ACTION_WPS_SEEKBACK:
298 if (wps_state.ff_rewind)
300 if (direction == 1)
302 /* fast forwarding, calc max step relative to end */
303 max_step = (wps_state.id3->length -
304 (wps_state.id3->elapsed +
305 ff_rewind_count)) *
306 FF_REWIND_MAX_PERCENT / 100;
308 else
310 /* rewinding, calc max step relative to start */
311 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
312 FF_REWIND_MAX_PERCENT / 100;
315 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
317 if (step > max_step)
318 step = max_step;
320 ff_rewind_count += step * direction;
322 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
323 step += step >> ff_rw_accel;
325 else
327 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
328 wps_state.id3 && wps_state.id3->length )
330 if (!wps_state.paused)
331 #if (CONFIG_CODEC == SWCODEC)
332 audio_pre_ff_rewind();
333 #else
334 audio_pause();
335 #endif
336 #if CONFIG_KEYPAD == PLAYER_PAD
337 FOR_NB_SCREENS(i)
338 gui_wps[i].display->stop_scroll();
339 #endif
340 if (direction > 0)
341 status_set_ffmode(STATUS_FASTFORWARD);
342 else
343 status_set_ffmode(STATUS_FASTBACKWARD);
345 wps_state.ff_rewind = true;
347 step = 1000 * global_settings.ff_rewind_min_step;
349 else
350 break;
353 if (direction > 0) {
354 if ((wps_state.id3->elapsed + ff_rewind_count) >
355 wps_state.id3->length)
356 ff_rewind_count = wps_state.id3->length -
357 wps_state.id3->elapsed;
359 else {
360 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
361 ff_rewind_count = -wps_state.id3->elapsed;
364 /* set the wps state ff_rewind_count so the progess info
365 displays corectly */
366 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
367 ff_rewind_count:-ff_rewind_count;
368 FOR_NB_SCREENS(i)
370 skin_update(&gui_wps[i],
371 WPS_REFRESH_PLAYER_PROGRESS |
372 WPS_REFRESH_DYNAMIC);
375 break;
377 case ACTION_WPS_STOPSEEK:
378 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
379 audio_ff_rewind(wps_state.id3->elapsed);
380 wps_state.ff_rewind_count = 0;
381 wps_state.ff_rewind = false;
382 status_set_ffmode(0);
383 #if (CONFIG_CODEC != SWCODEC)
384 if (!wps_state.paused)
385 audio_resume();
386 #endif
387 #ifdef HAVE_LCD_CHARCELLS
388 FOR_NB_SCREENS(i)
389 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
390 #endif
391 exit = true;
392 break;
394 default:
395 if(default_event_handler(button) == SYS_USB_CONNECTED) {
396 status_set_ffmode(0);
397 usb = true;
398 exit = true;
400 break;
402 if (!exit)
404 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
405 #ifdef HAVE_TOUCHSCREEN
406 if (button == ACTION_TOUCHSCREEN)
407 button = skintouch_to_wps(gui_wps[SCREEN_MAIN].data);
408 if (button != ACTION_WPS_SEEKFWD &&
409 button != ACTION_WPS_SEEKBACK)
410 button = ACTION_WPS_STOPSEEK;
411 #endif
414 return usb;
418 void display_keylock_text(bool locked)
420 int i;
421 FOR_NB_SCREENS(i)
422 gui_wps[i].display->stop_scroll();
424 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
430 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
431 static void gwps_caption_backlight(struct wps_state *state)
433 if (state && state->id3)
435 #ifdef HAVE_BACKLIGHT
436 if (global_settings.caption_backlight)
438 /* turn on backlight n seconds before track ends, and turn it off n
439 seconds into the new track. n == backlight_timeout, or 5s */
440 int n = global_settings.backlight_timeout * 1000;
442 if ( n < 1000 )
443 n = 5000; /* use 5s if backlight is always on or off */
445 if (((state->id3->elapsed < 1000) ||
446 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
447 (state->paused == false))
448 backlight_on();
450 #endif
451 #ifdef HAVE_REMOTE_LCD
452 if (global_settings.remote_caption_backlight)
454 /* turn on remote backlight n seconds before track ends, and turn it
455 off n seconds into the new track. n == remote_backlight_timeout,
456 or 5s */
457 int n = global_settings.remote_backlight_timeout * 1000;
459 if ( n < 1000 )
460 n = 5000; /* use 5s if backlight is always on or off */
462 if (((state->id3->elapsed < 1000) ||
463 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
464 (state->paused == false))
465 remote_backlight_on();
467 #endif
470 #endif
473 static void change_dir(int direction)
475 if (global_settings.prevent_skip)
476 return;
478 if (direction < 0)
479 audio_prev_dir();
480 else if (direction > 0)
481 audio_next_dir();
482 /* prevent the next dir to immediatly start being ffw'd */
483 action_wait_for_release();
486 static void prev_track(unsigned long skip_thresh)
488 if (wps_state.id3->elapsed < skip_thresh)
490 audio_prev();
491 return;
493 else
495 if (wps_state.id3->cuesheet)
497 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
498 return;
501 if (!wps_state.paused)
502 #if (CONFIG_CODEC == SWCODEC)
503 audio_pre_ff_rewind();
504 #else
505 audio_pause();
506 #endif
508 audio_ff_rewind(0);
510 #if (CONFIG_CODEC != SWCODEC)
511 if (!wps_state.paused)
512 audio_resume();
513 #endif
517 static void next_track(void)
519 /* take care of if we're playing a cuesheet */
520 if (wps_state.id3->cuesheet)
522 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
524 /* if the result was false, then we really want
525 to skip to the next track */
526 return;
530 audio_next();
533 static void play_hop(int direction)
535 long step = global_settings.skip_length*1000;
536 long elapsed = wps_state.id3->elapsed;
537 long remaining = wps_state.id3->length - elapsed;
539 if (step < 0)
541 if (direction < 0)
543 prev_track(DEFAULT_SKIP_TRESH);
544 return;
546 else if (remaining < DEFAULT_SKIP_TRESH*2)
548 next_track();
549 return;
551 else
552 elapsed += (remaining - DEFAULT_SKIP_TRESH*2);
554 else if (!global_settings.prevent_skip &&
555 (!step ||
556 (direction > 0 && step >= remaining) ||
557 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
558 { /* Do normal track skipping */
559 if (direction > 0)
560 next_track();
561 else if (direction < 0)
562 prev_track(DEFAULT_SKIP_TRESH);
563 return;
565 else if (direction == 1 && step >= remaining)
567 #if CONFIG_CODEC == SWCODEC
568 if(global_settings.beep)
569 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
570 #endif
571 return;
573 else if ((direction == -1 && elapsed < step))
575 elapsed = 0;
577 else
579 elapsed += step * direction;
581 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
583 #if (CONFIG_CODEC == SWCODEC)
584 audio_pre_ff_rewind();
585 #else
586 audio_pause();
587 #endif
589 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
590 #if (CONFIG_CODEC != SWCODEC)
591 if (!wps_state.paused)
592 audio_resume();
593 #endif
597 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
599 * If the user is unable to see the wps, because the display is deactivated,
600 * we suppress updates until the wps is activated again (the lcd driver will
601 * call this hook to issue an instant update)
602 * */
603 static void wps_lcd_activation_hook(void *param)
605 (void)param;
606 wps_sync_data.do_full_update = true;
607 /* force timeout in wps main loop, so that the update is instantly */
608 queue_post(&button_queue, BUTTON_NONE, 0);
610 #endif
612 static void gwps_leave_wps(void)
614 int i;
616 FOR_NB_SCREENS(i)
618 gui_wps[i].display->stop_scroll();
619 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
620 gui_wps[i].display->backdrop_show(sb_get_backdrop(i));
621 #endif
622 viewportmanager_theme_undo(i, skin_has_sbs(i, gui_wps[i].data));
626 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
627 /* Play safe and unregister the hook */
628 remove_event(LCD_EVENT_ACTIVATION, wps_lcd_activation_hook);
629 #endif
630 /* unhandle statusbar update delay */
631 sb_skin_set_update_delay(DEFAULT_UPDATE_DELAY);
635 * display the wps on entering or restoring */
636 static void gwps_enter_wps(void)
638 int i;
639 FOR_NB_SCREENS(i)
641 struct gui_wps *gwps = &gui_wps[i];
642 struct screen *display = gwps->display;
643 display->stop_scroll();
644 viewportmanager_theme_enable(i, skin_has_sbs(i, gui_wps[i].data), NULL);
646 /* Update the values in the first (default) viewport - in case the user
647 has modified the statusbar or colour settings */
648 #if LCD_DEPTH > 1
649 if (display->depth > 1)
651 struct viewport *vp = &find_viewport(VP_DEFAULT_LABEL, gwps->data)->vp;
652 vp->fg_pattern = display->get_foreground();
653 vp->bg_pattern = display->get_background();
655 #endif
656 /* make the backdrop actually take effect */
657 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
658 display->backdrop_show(gwps->data->backdrop);
659 #endif
660 display->clear_display();
661 skin_update(gwps, WPS_REFRESH_ALL);
663 #ifdef HAVE_TOUCHSCREEN
664 skin_disarm_touchregions(gui_wps[i].data);
665 #endif
667 /* force statusbar/skin update since we just cleared the whole screen */
668 send_event(GUI_EVENT_ACTIONUPDATE, (void*)1);
671 /* The WPS can be left in two ways:
672 * a) call a function, which draws over the wps. In this case, the wps
673 * will be still active (i.e. the below function didn't return)
674 * b) return with a value evaluated by root_menu.c, in this case the wps
675 * is really left, and root_menu will handle the next screen
677 * In either way, call gwps_leave_wps(), in order to restore the correct
678 * "main screen" backdrops and statusbars
680 long gui_wps_show(void)
682 long button = 0;
683 bool restore = true;
684 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
685 bool exit = false;
686 bool bookmark = false;
687 bool update = false;
688 bool vol_changed = false;
689 int i;
690 long last_left = 0, last_right = 0;
692 #ifdef HAVE_LCD_CHARCELLS
693 status_set_audio(true);
694 status_set_param(false);
695 #endif
697 #ifdef AB_REPEAT_ENABLE
698 ab_repeat_init();
699 ab_reset_markers();
700 #endif
701 wps_state_init();
703 while ( 1 )
705 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
707 /* did someone else (i.e power thread) change audio pause mode? */
708 if (wps_state.paused != audio_paused) {
709 wps_state.paused = audio_paused;
711 /* if another thread paused audio, we are probably in car mode,
712 about to shut down. lets save the settings. */
713 if (wps_state.paused) {
714 settings_save();
715 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
716 call_storage_idle_notifys(true);
717 #endif
720 button = skin_wait_for_action(gui_wps, CONTEXT_WPS|ALLOW_SOFTLOCK,
721 restore ? 1 : HZ/5);
723 /* Exit if audio has stopped playing. This happens e.g. at end of
724 playlist or if using the sleep timer. */
725 if (!(audio_status() & AUDIO_STATUS_PLAY))
726 exit = true;
727 #ifdef HAVE_TOUCHSCREEN
728 if (button == ACTION_TOUCHSCREEN)
729 button = skintouch_to_wps(gui_wps[SCREEN_MAIN].data);
730 #endif
731 /* The iPods/X5/M5 use a single button for the A-B mode markers,
732 defined as ACTION_WPSAB_SINGLE in their config files. */
733 #ifdef ACTION_WPSAB_SINGLE
734 if (!global_settings.party_mode && ab_repeat_mode_enabled())
736 static int wps_ab_state = 0;
737 if (button == ACTION_WPSAB_SINGLE)
739 switch (wps_ab_state)
741 case 0: /* set the A spot */
742 button = ACTION_WPS_ABSETA_PREVDIR;
743 break;
744 case 1: /* set the B spot */
745 button = ACTION_WPS_ABSETB_NEXTDIR;
746 break;
747 case 2:
748 button = ACTION_WPS_ABRESET;
749 break;
751 wps_ab_state = (wps_ab_state+1) % 3;
754 #endif
755 switch(button)
757 #ifdef HAVE_HOTKEY
758 case ACTION_WPS_HOTKEY:
759 if (!global_settings.hotkey_wps)
760 break;
761 /* fall through */
762 #endif
763 case ACTION_WPS_CONTEXT:
765 bool hotkey = button == ACTION_WPS_HOTKEY;
766 gwps_leave_wps();
767 int retval = onplay(wps_state.id3->path,
768 FILE_ATTR_AUDIO, CONTEXT_WPS, hotkey);
769 /* if music is stopped in the context menu we want to exit the wps */
770 if (retval == ONPLAY_MAINMENU
771 || !audio_status())
772 return GO_TO_ROOT;
773 else if (retval == ONPLAY_PLAYLIST)
774 return GO_TO_PLAYLIST_VIEWER;
775 #ifdef HAVE_PICTUREFLOW_INTEGRATION
776 else if (retval == ONPLAY_PICTUREFLOW)
777 return GO_TO_PICTUREFLOW;
778 #endif
779 restore = true;
781 break;
783 case ACTION_WPS_BROWSE:
784 #ifdef HAVE_LCD_CHARCELLS
785 status_set_record(false);
786 status_set_audio(false);
787 #endif
788 gwps_leave_wps();
789 return GO_TO_PREVIOUS_BROWSER;
790 break;
792 /* play/pause */
793 case ACTION_WPS_PLAY:
794 if (global_settings.party_mode)
795 break;
796 if ( wps_state.paused )
798 wps_state.paused = false;
799 if ( global_settings.fade_on_stop )
800 fade(true, true);
801 else
802 audio_resume();
804 else
806 wps_state.paused = true;
807 if ( global_settings.fade_on_stop )
808 fade(false, true);
809 else
810 audio_pause();
811 settings_save();
812 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
813 call_storage_idle_notifys(true); /* make sure resume info is saved */
814 #endif
816 break;
818 case ACTION_WPS_VOLUP:
819 global_settings.volume++;
820 vol_changed = true;
821 break;
822 case ACTION_WPS_VOLDOWN:
823 global_settings.volume--;
824 vol_changed = true;
825 break;
826 /* fast forward
827 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
828 case ACTION_WPS_SEEKFWD:
829 if (global_settings.party_mode)
830 break;
831 if (current_tick -last_right < HZ)
833 if (wps_state.id3->cuesheet)
835 audio_next();
837 else
839 change_dir(1);
842 else
843 ffwd_rew(ACTION_WPS_SEEKFWD);
844 last_right = last_left = 0;
845 break;
846 /* fast rewind
847 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
848 case ACTION_WPS_SEEKBACK:
849 if (global_settings.party_mode)
850 break;
851 if (current_tick -last_left < HZ)
853 if (wps_state.id3->cuesheet)
855 if (!wps_state.paused)
856 #if (CONFIG_CODEC == SWCODEC)
857 audio_pre_ff_rewind();
858 #else
859 audio_pause();
860 #endif
861 audio_ff_rewind(0);
863 else
865 change_dir(-1);
868 else
869 ffwd_rew(ACTION_WPS_SEEKBACK);
870 last_left = last_right = 0;
871 break;
873 /* prev / restart */
874 case ACTION_WPS_SKIPPREV:
875 if (global_settings.party_mode)
876 break;
877 last_left = current_tick;
878 #ifdef AB_REPEAT_ENABLE
879 /* if we're in A/B repeat mode and the current position
880 is past the A marker, jump back to the A marker... */
881 if ( ab_repeat_mode_enabled() )
883 if ( ab_after_A_marker(wps_state.id3->elapsed) )
885 ab_jump_to_A_marker();
886 break;
887 #if (AB_REPEAT_ENABLE == 2)
888 } else {
889 ab_reset_markers();
890 #endif
893 else
894 /* ...otherwise, do it normally */
895 #endif
896 play_hop(-1);
897 break;
899 /* next
900 OR if skip length set, hop by predetermined amount. */
901 case ACTION_WPS_SKIPNEXT:
902 if (global_settings.party_mode)
903 break;
904 last_right = current_tick;
905 #ifdef AB_REPEAT_ENABLE
906 /* if we're in A/B repeat mode and the current position is
907 before the A marker, jump to the A marker... */
908 if ( ab_repeat_mode_enabled() )
910 if ( ab_before_A_marker(wps_state.id3->elapsed) )
912 ab_jump_to_A_marker();
913 break;
914 #if (AB_REPEAT_ENABLE == 2)
915 } else {
916 ab_reset_markers();
917 #endif
920 else
921 /* ...otherwise, do it normally */
922 #endif
923 play_hop(1);
924 break;
925 /* next / prev directories */
926 /* and set A-B markers if in a-b mode */
927 case ACTION_WPS_ABSETB_NEXTDIR:
928 if (global_settings.party_mode)
929 break;
930 #if defined(AB_REPEAT_ENABLE)
931 if (ab_repeat_mode_enabled())
933 ab_set_B_marker(wps_state.id3->elapsed);
934 ab_jump_to_A_marker();
936 else
937 #endif
939 change_dir(1);
941 break;
942 case ACTION_WPS_ABSETA_PREVDIR:
943 if (global_settings.party_mode)
944 break;
945 #if defined(AB_REPEAT_ENABLE)
946 if (ab_repeat_mode_enabled())
947 ab_set_A_marker(wps_state.id3->elapsed);
948 else
949 #endif
951 change_dir(-1);
953 break;
954 /* menu key functions */
955 case ACTION_WPS_MENU:
956 gwps_leave_wps();
957 return GO_TO_ROOT;
958 break;
961 #ifdef HAVE_QUICKSCREEN
962 case ACTION_WPS_QUICKSCREEN:
964 gwps_leave_wps();
965 if (quick_screen_quick(button))
966 return GO_TO_ROOT;
967 restore = true;
969 break;
970 #endif /* HAVE_QUICKSCREEN */
972 /* screen settings */
973 #ifdef BUTTON_F3
974 case ACTION_F3:
976 gwps_leave_wps();
977 if (quick_screen_f3(BUTTON_F3))
978 return GO_TO_ROOT;
979 restore = true;
981 break;
982 #endif /* BUTTON_F3 */
984 /* pitch screen */
985 #ifdef HAVE_PITCHSCREEN
986 case ACTION_WPS_PITCHSCREEN:
988 gwps_leave_wps();
989 if (1 == gui_syncpitchscreen_run())
990 return GO_TO_ROOT;
991 restore = true;
993 break;
994 #endif /* HAVE_PITCHSCREEN */
996 #ifdef AB_REPEAT_ENABLE
997 /* reset A&B markers */
998 case ACTION_WPS_ABRESET:
999 if (ab_repeat_mode_enabled())
1001 ab_reset_markers();
1002 update = true;
1004 break;
1005 #endif /* AB_REPEAT_ENABLE */
1007 /* stop and exit wps */
1008 case ACTION_WPS_STOP:
1009 if (global_settings.party_mode)
1010 break;
1011 bookmark = true;
1012 exit = true;
1013 break;
1015 case ACTION_WPS_ID3SCREEN:
1017 gwps_leave_wps();
1018 if (browse_id3())
1019 return GO_TO_ROOT;
1020 restore = true;
1022 break;
1023 #ifdef HAVE_TOUCHSCREEN
1024 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1026 global_settings.playlist_shuffle =
1027 !global_settings.playlist_shuffle;
1028 #if CONFIG_CODEC == SWCODEC
1029 dsp_set_replaygain();
1030 #endif
1031 if (global_settings.playlist_shuffle)
1032 playlist_randomise(NULL, current_tick, true);
1033 else
1034 playlist_sort(NULL, true);
1036 break;
1037 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1039 const struct settings_list *rep_setting =
1040 find_setting(&global_settings.repeat_mode, NULL);
1041 option_select_next_val(rep_setting, false, true);
1042 audio_flush_and_reload_tracks();
1044 break;
1045 #endif /* HAVE_TOUCHSCREEN */
1046 /* this case is used by the softlock feature
1047 * it requests a full update here */
1048 case ACTION_REDRAW:
1049 wps_sync_data.do_full_update = true;
1050 break;
1051 case ACTION_NONE: /* Timeout, do a partial update */
1052 update = true;
1053 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1054 break;
1055 #ifdef HAVE_RECORDING
1056 case ACTION_WPS_REC:
1057 exit = true;
1058 break;
1059 #endif
1060 case SYS_POWEROFF:
1061 default_event_handler(SYS_POWEROFF);
1062 break;
1063 case ACTION_WPS_VIEW_PLAYLIST:
1064 gwps_leave_wps();
1065 return GO_TO_PLAYLIST_VIEWER;
1066 break;
1067 default:
1068 if(default_event_handler(button) == SYS_USB_CONNECTED)
1070 gwps_leave_wps();
1071 return GO_TO_ROOT;
1073 update = true;
1074 break;
1077 if (vol_changed)
1079 bool res = false;
1080 vol_changed = false;
1081 setvol();
1082 FOR_NB_SCREENS(i)
1084 if(update_onvol_change(&gui_wps[i]))
1085 res = true;
1087 if (res) {
1088 restore = true;
1089 restoretimer = RESTORE_WPS_NEXT_SECOND;
1094 if (restore &&
1095 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1096 TIME_AFTER(current_tick, restoretimer)))
1098 restore = false;
1099 restoretimer = RESTORE_WPS_INSTANTLY;
1100 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1101 add_event(LCD_EVENT_ACTIVATION, false, wps_lcd_activation_hook);
1102 #endif
1103 /* we remove the update delay since it's not very usable in the wps,
1104 * e.g. during volume changing or ffwd/rewind */
1105 sb_skin_set_update_delay(0);
1106 wps_sync_data.do_full_update = update = false;
1107 gwps_enter_wps();
1109 else if (wps_sync_data.do_full_update || update)
1111 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1112 gwps_caption_backlight(&wps_state);
1113 #endif
1114 FOR_NB_SCREENS(i)
1116 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1117 /* currently, all remotes are readable without backlight
1118 * so still update those */
1119 if (lcd_active() || (i != SCREEN_MAIN))
1120 #endif
1122 skin_update(&gui_wps[i], wps_sync_data.do_full_update ?
1123 WPS_REFRESH_ALL : WPS_REFRESH_NON_STATIC);
1126 wps_sync_data.do_full_update = false;
1127 update = false;
1130 if (exit) {
1131 #ifdef HAVE_LCD_CHARCELLS
1132 status_set_record(false);
1133 status_set_audio(false);
1134 #endif
1135 if (global_settings.fade_on_stop)
1136 fade(false, true);
1138 if (bookmark)
1139 bookmark_autobookmark(true);
1140 audio_stop();
1141 #ifdef AB_REPEAT_ENABLE
1142 ab_reset_markers();
1143 #endif
1144 gwps_leave_wps();
1145 #ifdef HAVE_RECORDING
1146 if (button == ACTION_WPS_REC)
1147 return GO_TO_RECSCREEN;
1148 #endif
1149 if (global_settings.browse_current)
1150 return GO_TO_PREVIOUS_BROWSER;
1151 return GO_TO_PREVIOUS;
1154 if (button && !IS_SYSEVENT(button) )
1155 storage_spin();
1157 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1160 /* this is called from the playback thread so NO DRAWING! */
1161 static void track_changed_callback(void *param)
1163 wps_state.id3 = (struct mp3entry*)param;
1164 wps_state.nid3 = audio_next_track();
1165 if (wps_state.id3->cuesheet)
1167 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1169 wps_sync_data.do_full_update = true;
1171 static void nextid3available_callback(void* param)
1173 (void)param;
1174 wps_state.nid3 = audio_next_track();
1175 wps_sync_data.do_full_update = true;
1179 static void wps_state_init(void)
1181 wps_state.ff_rewind = false;
1182 wps_state.paused = false;
1183 if(audio_status() & AUDIO_STATUS_PLAY)
1185 wps_state.id3 = audio_current_track();
1186 wps_state.nid3 = audio_next_track();
1188 else
1190 wps_state.id3 = NULL;
1191 wps_state.nid3 = NULL;
1193 /* We'll be updating due to restore initialized with true */
1194 wps_sync_data.do_full_update = false;
1195 /* add the WPS track event callbacks */
1196 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1197 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1201 void gui_sync_wps_init(void)
1203 int i;
1204 FOR_NB_SCREENS(i)
1206 #ifdef HAVE_ALBUMART
1207 wps_datas[i].albumart = NULL;
1208 wps_datas[i].playback_aa_slot = -1;
1209 #endif
1210 gui_wps[i].data = &wps_datas[i];
1211 gui_wps[i].display = &screens[i];
1212 /* Currently no seperate wps_state needed/possible
1213 so use the only available ( "global" ) one */
1214 gui_wps[i].state = &wps_state;
1215 /* must point to the same struct for both screens */
1216 gui_wps[i].sync_data = &wps_sync_data;
1221 #ifdef IPOD_ACCESSORY_PROTOCOL
1222 bool is_wps_fading(void)
1224 return wps_state.is_fading;
1227 int wps_get_ff_rewind_count(void)
1229 return wps_state.ff_rewind_count;
1231 #endif