Don't need that honking huge header, so out it goes.
[kugel-rb.git] / apps / gui / wps.c
blobe5283805762d54e2aed9257f37d4fd0604cb33c1
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 /* initial setup of wps_data */
78 static void wps_state_init(void);
79 static void track_changed_callback(void *param);
80 static void nextid3available_callback(void* param);
82 #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps"
83 #ifdef HAVE_REMOTE_LCD
84 #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps"
85 #define DEFAULT_WPS(screen) ((screen) == SCREEN_MAIN ? \
86 WPS_DEFAULTCFG:RWPS_DEFAULTCFG)
87 #else
88 #define DEFAULT_WPS(screen) (WPS_DEFAULTCFG)
89 #endif
91 char* wps_default_skin(enum screen_type screen)
93 static char *skin_buf[NB_SCREENS] = {
94 #ifdef HAVE_LCD_BITMAP
95 #if LCD_DEPTH > 1
96 "%X(d)\n"
97 #endif
98 "%s%?it<%?in<%in. |>%it|%fn>\n"
99 "%s%?ia<%ia|%?d(2)<%d(2)|%(root%)>>\n"
100 "%s%?id<%id|%?d(1)<%d(1)|%(root%)>> %?iy<%(%iy%)|>\n\n"
101 "%al%pc/%pt%ar[%pp:%pe]\n"
102 "%fbkBit %?fv<avg|> %?iv<%(id3v%iv%)|%(no id3%)>\n"
103 "%pb\n%pm\n",
104 #else
105 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d(2)> - %?id<%id|%d(1)>\n"
106 "%pc%?ps<*|/>%pt\n",
107 #endif
108 #ifdef HAVE_REMOTE_LCD
109 #if LCD_REMOTE_DEPTH > 1
110 "%X(d)\n"
111 #endif
112 "%s%?ia<%ia|%?d(2)<%d(2)|%(root%)>>\n"
113 "%s%?it<%?in<%in. |>%it|%fn>\n"
114 "%al%pc/%pt%ar[%pp:%pe]\n"
115 "%fbkBit %?fv<avg|> %?iv<%(id3v%iv%)|%(no id3%)>\n"
116 "%pb\n",
117 #endif
119 return skin_buf[screen];
122 void fade(bool fade_in, bool updatewps)
124 int fp_global_vol = global_settings.volume << 8;
125 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
126 int fp_step = (fp_global_vol - fp_min_vol) / 30;
127 int i;
128 skin_get_global_state()->is_fading = !fade_in;
129 if (fade_in) {
130 /* fade in */
131 int fp_volume = fp_min_vol;
133 /* zero out the sound */
134 sound_set_volume(fp_min_vol >> 8);
136 sleep(HZ/10); /* let audio thread run */
137 audio_resume();
139 while (fp_volume < fp_global_vol - fp_step) {
140 fp_volume += fp_step;
141 sound_set_volume(fp_volume >> 8);
142 if (updatewps)
144 FOR_NB_SCREENS(i)
145 skin_update(WPS, i, SKIN_REFRESH_NON_STATIC);
147 sleep(1);
149 sound_set_volume(global_settings.volume);
151 else {
152 /* fade out */
153 int fp_volume = fp_global_vol;
155 while (fp_volume > fp_min_vol + fp_step) {
156 fp_volume -= fp_step;
157 sound_set_volume(fp_volume >> 8);
158 if (updatewps)
160 FOR_NB_SCREENS(i)
161 skin_update(WPS, i, SKIN_REFRESH_NON_STATIC);
163 sleep(1);
165 audio_pause();
166 skin_get_global_state()->is_fading = false;
167 #if CONFIG_CODEC != SWCODEC
168 #ifndef SIMULATOR
169 /* let audio thread run and wait for the mas to run out of data */
170 while (!mp3_pause_done())
171 #endif
172 sleep(HZ/10);
173 #endif
175 /* reset volume to what it was before the fade */
176 sound_set_volume(global_settings.volume);
180 static bool update_onvol_change(enum screen_type screen)
182 skin_update(WPS, screen, SKIN_REFRESH_NON_STATIC);
184 #ifdef HAVE_LCD_CHARCELLS
185 splashf(0, "Vol: %3d dB",
186 sound_val2phys(SOUND_VOLUME, global_settings.volume));
187 return true;
188 #endif
189 return false;
193 #ifdef HAVE_TOUCHSCREEN
194 static int skintouch_to_wps(struct wps_data *data)
196 int offset = 0;
197 struct touchregion *region;
198 int button = skin_get_touchaction(data, &offset, &region);
199 switch (button)
201 case ACTION_STD_PREV:
202 return ACTION_WPS_SKIPPREV;
203 case ACTION_STD_PREVREPEAT:
204 return ACTION_WPS_SEEKBACK;
205 case ACTION_STD_NEXT:
206 return ACTION_WPS_SKIPNEXT;
207 case ACTION_STD_NEXTREPEAT:
208 return ACTION_WPS_SEEKFWD;
209 case ACTION_STD_MENU:
210 return ACTION_WPS_MENU;
211 case ACTION_STD_CONTEXT:
212 return ACTION_WPS_CONTEXT;
213 case ACTION_STD_QUICKSCREEN:
214 return ACTION_WPS_QUICKSCREEN;
215 #ifdef HAVE_HOTKEY
216 case ACTION_STD_HOTKEY:
217 return ACTION_WPS_HOTKEY;
218 #endif
219 case WPS_TOUCHREGION_SCROLLBAR:
220 skin_get_global_state()->id3->elapsed = skin_get_global_state()->id3->length*offset/100;
221 if (!skin_get_global_state()->paused)
222 #if (CONFIG_CODEC == SWCODEC)
223 audio_pre_ff_rewind();
224 #else
225 audio_pause();
226 #endif
227 audio_ff_rewind(skin_get_global_state()->id3->elapsed);
228 #if (CONFIG_CODEC != SWCODEC)
229 if (!skin_get_global_state()->paused)
230 audio_resume();
231 #endif
232 return ACTION_TOUCHSCREEN;
233 case WPS_TOUCHREGION_VOLUME:
235 const int min_vol = sound_min(SOUND_VOLUME);
236 const int max_vol = sound_max(SOUND_VOLUME);
237 global_settings.volume = (offset * (max_vol - min_vol)) / 100;
238 global_settings.volume += min_vol;
239 setvol();
241 return ACTION_TOUCHSCREEN;
242 case ACTION_SETTINGS_INC:
243 case ACTION_SETTINGS_DEC:
245 const struct settings_list *setting = region->data;
246 option_select_next_val(setting, button == ACTION_SETTINGS_DEC, true);
248 return ACTION_REDRAW;
249 case ACTION_TOUCH_MUTE:
251 const int min_vol = sound_min(SOUND_VOLUME);
252 if (global_settings.volume == min_vol)
253 global_settings.volume = region->value;
254 else
256 region->value = global_settings.volume;
257 global_settings.volume = min_vol;
259 setvol();
261 return ACTION_REDRAW;
263 return button;
265 #endif /* HAVE_TOUCHSCREEN */
267 bool ffwd_rew(int button)
269 unsigned int step = 0; /* current ff/rewind step */
270 unsigned int max_step = 0; /* maximum ff/rewind step */
271 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
272 int direction = -1; /* forward=1 or backward=-1 */
273 bool exit = false;
274 bool usb = false;
275 int i = 0;
276 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
278 if (button == ACTION_NONE)
280 status_set_ffmode(0);
281 return usb;
283 while (!exit)
285 switch ( button )
287 case ACTION_WPS_SEEKFWD:
288 direction = 1;
289 case ACTION_WPS_SEEKBACK:
290 if (skin_get_global_state()->ff_rewind)
292 if (direction == 1)
294 /* fast forwarding, calc max step relative to end */
295 max_step = (skin_get_global_state()->id3->length -
296 (skin_get_global_state()->id3->elapsed +
297 ff_rewind_count)) *
298 FF_REWIND_MAX_PERCENT / 100;
300 else
302 /* rewinding, calc max step relative to start */
303 max_step = (skin_get_global_state()->id3->elapsed + ff_rewind_count) *
304 FF_REWIND_MAX_PERCENT / 100;
307 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
309 if (step > max_step)
310 step = max_step;
312 ff_rewind_count += step * direction;
314 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
315 step += step >> ff_rw_accel;
317 else
319 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
320 skin_get_global_state()->id3 && skin_get_global_state()->id3->length )
322 if (!skin_get_global_state()->paused)
323 #if (CONFIG_CODEC == SWCODEC)
324 audio_pre_ff_rewind();
325 #else
326 audio_pause();
327 #endif
328 #if CONFIG_KEYPAD == PLAYER_PAD
329 FOR_NB_SCREENS(i)
330 skin_get_gwps(WPS, i)->display->stop_scroll();
331 #endif
332 if (direction > 0)
333 status_set_ffmode(STATUS_FASTFORWARD);
334 else
335 status_set_ffmode(STATUS_FASTBACKWARD);
337 skin_get_global_state()->ff_rewind = true;
339 step = 1000 * global_settings.ff_rewind_min_step;
341 else
342 break;
345 if (direction > 0) {
346 if ((skin_get_global_state()->id3->elapsed + ff_rewind_count) >
347 skin_get_global_state()->id3->length)
348 ff_rewind_count = skin_get_global_state()->id3->length -
349 skin_get_global_state()->id3->elapsed;
351 else {
352 if ((int)(skin_get_global_state()->id3->elapsed + ff_rewind_count) < 0)
353 ff_rewind_count = -skin_get_global_state()->id3->elapsed;
356 /* set the wps state ff_rewind_count so the progess info
357 displays corectly */
358 skin_get_global_state()->ff_rewind_count = (skin_get_global_state()->wps_time_countup == false)?
359 ff_rewind_count:-ff_rewind_count;
360 FOR_NB_SCREENS(i)
362 skin_update(WPS, i,
363 SKIN_REFRESH_PLAYER_PROGRESS |
364 SKIN_REFRESH_DYNAMIC);
367 break;
369 case ACTION_WPS_STOPSEEK:
370 skin_get_global_state()->id3->elapsed = skin_get_global_state()->id3->elapsed+ff_rewind_count;
371 audio_ff_rewind(skin_get_global_state()->id3->elapsed);
372 skin_get_global_state()->ff_rewind_count = 0;
373 skin_get_global_state()->ff_rewind = false;
374 status_set_ffmode(0);
375 #if (CONFIG_CODEC != SWCODEC)
376 if (!skin_get_global_state()->paused)
377 audio_resume();
378 #endif
379 #ifdef HAVE_LCD_CHARCELLS
380 FOR_NB_SCREENS(i)
381 skin_update(WPS, i, SKIN_REFRESH_ALL);
382 #endif
383 exit = true;
384 break;
386 default:
387 if(default_event_handler(button) == SYS_USB_CONNECTED) {
388 status_set_ffmode(0);
389 usb = true;
390 exit = true;
392 break;
394 if (!exit)
396 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
397 #ifdef HAVE_TOUCHSCREEN
398 if (button == ACTION_TOUCHSCREEN)
399 button = skintouch_to_wps(skin_get_gwps(WPS, SCREEN_MAIN)->data);
400 if (button != ACTION_WPS_SEEKFWD &&
401 button != ACTION_WPS_SEEKBACK)
402 button = ACTION_WPS_STOPSEEK;
403 #endif
406 return usb;
410 void display_keylock_text(bool locked)
412 int i;
413 FOR_NB_SCREENS(i)
414 skin_get_gwps(WPS, i)->display->stop_scroll();
416 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
422 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
423 static void gwps_caption_backlight(struct wps_state *state)
425 if (state && state->id3)
427 #ifdef HAVE_BACKLIGHT
428 if (global_settings.caption_backlight)
430 /* turn on backlight n seconds before track ends, and turn it off n
431 seconds into the new track. n == backlight_timeout, or 5s */
432 int n = global_settings.backlight_timeout * 1000;
434 if ( n < 1000 )
435 n = 5000; /* use 5s if backlight is always on or off */
437 if (((state->id3->elapsed < 1000) ||
438 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
439 (state->paused == false))
440 backlight_on();
442 #endif
443 #ifdef HAVE_REMOTE_LCD
444 if (global_settings.remote_caption_backlight)
446 /* turn on remote backlight n seconds before track ends, and turn it
447 off n seconds into the new track. n == remote_backlight_timeout,
448 or 5s */
449 int n = global_settings.remote_backlight_timeout * 1000;
451 if ( n < 1000 )
452 n = 5000; /* use 5s if backlight is always on or off */
454 if (((state->id3->elapsed < 1000) ||
455 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
456 (state->paused == false))
457 remote_backlight_on();
459 #endif
462 #endif
465 static void change_dir(int direction)
467 if (global_settings.prevent_skip)
468 return;
470 if (direction < 0)
471 audio_prev_dir();
472 else if (direction > 0)
473 audio_next_dir();
474 /* prevent the next dir to immediatly start being ffw'd */
475 action_wait_for_release();
478 static void prev_track(unsigned long skip_thresh)
480 struct wps_state *state = skin_get_global_state();
481 if (state->id3->elapsed < skip_thresh)
483 audio_prev();
484 return;
486 else
488 if (state->id3->cuesheet)
490 curr_cuesheet_skip(state->id3->cuesheet, -1, state->id3->elapsed);
491 return;
494 if (!state->paused)
495 #if (CONFIG_CODEC == SWCODEC)
496 audio_pre_ff_rewind();
497 #else
498 audio_pause();
499 #endif
501 audio_ff_rewind(0);
503 #if (CONFIG_CODEC != SWCODEC)
504 if (!state->paused)
505 audio_resume();
506 #endif
510 static void next_track(void)
512 struct wps_state *state = skin_get_global_state();
513 /* take care of if we're playing a cuesheet */
514 if (state->id3->cuesheet)
516 if (curr_cuesheet_skip(state->id3->cuesheet, 1, state->id3->elapsed))
518 /* if the result was false, then we really want
519 to skip to the next track */
520 return;
524 audio_next();
527 static void play_hop(int direction)
529 struct wps_state *state = skin_get_global_state();
530 long step = global_settings.skip_length*1000;
531 long elapsed = state->id3->elapsed;
532 long remaining = state->id3->length - elapsed;
534 if (step < 0)
536 if (direction < 0)
538 prev_track(DEFAULT_SKIP_TRESH);
539 return;
541 else if (remaining < DEFAULT_SKIP_TRESH*2)
543 next_track();
544 return;
546 else
547 elapsed += (remaining - DEFAULT_SKIP_TRESH*2);
549 else if (!global_settings.prevent_skip &&
550 (!step ||
551 (direction > 0 && step >= remaining) ||
552 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
553 { /* Do normal track skipping */
554 if (direction > 0)
555 next_track();
556 else if (direction < 0)
557 prev_track(DEFAULT_SKIP_TRESH);
558 return;
560 else if (direction == 1 && step >= remaining)
562 #if CONFIG_CODEC == SWCODEC
563 if(global_settings.beep)
564 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
565 #endif
566 return;
568 else if ((direction == -1 && elapsed < step))
570 elapsed = 0;
572 else
574 elapsed += step * direction;
576 if((audio_status() & AUDIO_STATUS_PLAY) && !state->paused)
578 #if (CONFIG_CODEC == SWCODEC)
579 audio_pre_ff_rewind();
580 #else
581 audio_pause();
582 #endif
584 audio_ff_rewind(state->id3->elapsed = elapsed);
585 #if (CONFIG_CODEC != SWCODEC)
586 if (!state->paused)
587 audio_resume();
588 #endif
592 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
594 * If the user is unable to see the wps, because the display is deactivated,
595 * we suppress updates until the wps is activated again (the lcd driver will
596 * call this hook to issue an instant update)
597 * */
598 static void wps_lcd_activation_hook(void *param)
600 (void)param;
601 skin_request_full_update(WPS);
602 /* force timeout in wps main loop, so that the update is instantly */
603 queue_post(&button_queue, BUTTON_NONE, 0);
605 #endif
607 static void gwps_leave_wps(void)
609 int i;
611 FOR_NB_SCREENS(i)
613 skin_get_gwps(WPS, i)->display->stop_scroll();
614 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
615 skin_backdrop_show(sb_get_backdrop(i));
616 #endif
617 viewportmanager_theme_undo(i, skin_has_sbs(i, skin_get_gwps(WPS, i)->data));
621 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
622 /* Play safe and unregister the hook */
623 remove_event(LCD_EVENT_ACTIVATION, wps_lcd_activation_hook);
624 #endif
625 /* unhandle statusbar update delay */
626 sb_skin_set_update_delay(DEFAULT_UPDATE_DELAY);
627 #ifdef HAVE_TOUCHSCREEN
628 touchscreen_set_mode(global_settings.touch_mode);
629 #endif
633 * display the wps on entering or restoring */
634 static void gwps_enter_wps(void)
636 int i;
637 struct gui_wps *gwps;
638 struct screen *display;
639 FOR_NB_SCREENS(i)
641 gwps = skin_get_gwps(WPS, i);
642 display = gwps->display;
643 display->stop_scroll();
644 viewportmanager_theme_enable(i, skin_has_sbs(i, skin_get_gwps(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 skin_viewport *svp = find_viewport(VP_DEFAULT_LABEL,
652 false, gwps->data);
653 if (svp)
655 struct viewport *vp = &svp->vp;
656 vp->fg_pattern = display->get_foreground();
657 vp->bg_pattern = display->get_background();
660 #endif
661 /* make the backdrop actually take effect */
662 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
663 skin_backdrop_show(gwps->data->backdrop_id);
664 #endif
665 display->clear_display();
666 skin_update(WPS, i, SKIN_REFRESH_ALL);
669 #ifdef HAVE_TOUCHSCREEN
670 gwps = skin_get_gwps(WPS, SCREEN_MAIN);
671 skin_disarm_touchregions(gwps->data);
672 if (!gwps->data->touchregions)
673 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
674 #endif
675 /* force statusbar/skin update since we just cleared the whole screen */
676 send_event(GUI_EVENT_ACTIONUPDATE, (void*)1);
679 /* The WPS can be left in two ways:
680 * a) call a function, which draws over the wps. In this case, the wps
681 * will be still active (i.e. the below function didn't return)
682 * b) return with a value evaluated by root_menu.c, in this case the wps
683 * is really left, and root_menu will handle the next screen
685 * In either way, call gwps_leave_wps(), in order to restore the correct
686 * "main screen" backdrops and statusbars
688 long gui_wps_show(void)
690 long button = 0;
691 bool restore = true;
692 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
693 bool exit = false;
694 bool bookmark = false;
695 bool update = false;
696 bool vol_changed = false;
697 int i;
698 long last_left = 0, last_right = 0;
699 struct wps_state *state = skin_get_global_state();
701 #ifdef HAVE_LCD_CHARCELLS
702 status_set_audio(true);
703 status_set_param(false);
704 #endif
706 #ifdef AB_REPEAT_ENABLE
707 ab_repeat_init();
708 ab_reset_markers();
709 #endif
710 wps_state_init();
712 while ( 1 )
714 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
716 /* did someone else (i.e power thread) change audio pause mode? */
717 if (state->paused != audio_paused) {
718 state->paused = audio_paused;
720 /* if another thread paused audio, we are probably in car mode,
721 about to shut down. lets save the settings. */
722 if (state->paused) {
723 settings_save();
724 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
725 call_storage_idle_notifys(true);
726 #endif
729 button = skin_wait_for_action(WPS, CONTEXT_WPS|ALLOW_SOFTLOCK,
730 restore ? 1 : HZ/5);
732 /* Exit if audio has stopped playing. This happens e.g. at end of
733 playlist or if using the sleep timer. */
734 if (!(audio_status() & AUDIO_STATUS_PLAY))
735 exit = true;
736 #ifdef HAVE_TOUCHSCREEN
737 if (button == ACTION_TOUCHSCREEN)
738 button = skintouch_to_wps(skin_get_gwps(WPS, SCREEN_MAIN)->data);
739 #endif
740 /* The iPods/X5/M5 use a single button for the A-B mode markers,
741 defined as ACTION_WPSAB_SINGLE in their config files. */
742 #ifdef ACTION_WPSAB_SINGLE
743 if (!global_settings.party_mode && ab_repeat_mode_enabled())
745 static int wps_ab_state = 0;
746 if (button == ACTION_WPSAB_SINGLE)
748 switch (wps_ab_state)
750 case 0: /* set the A spot */
751 button = ACTION_WPS_ABSETA_PREVDIR;
752 break;
753 case 1: /* set the B spot */
754 button = ACTION_WPS_ABSETB_NEXTDIR;
755 break;
756 case 2:
757 button = ACTION_WPS_ABRESET;
758 break;
760 wps_ab_state = (wps_ab_state+1) % 3;
763 #endif
764 switch(button)
766 #ifdef HAVE_HOTKEY
767 case ACTION_WPS_HOTKEY:
768 if (!global_settings.hotkey_wps)
769 break;
770 /* fall through */
771 #endif
772 case ACTION_WPS_CONTEXT:
774 bool hotkey = button == ACTION_WPS_HOTKEY;
775 gwps_leave_wps();
776 int retval = onplay(state->id3->path,
777 FILE_ATTR_AUDIO, CONTEXT_WPS, hotkey);
778 /* if music is stopped in the context menu we want to exit the wps */
779 if (retval == ONPLAY_MAINMENU
780 || !audio_status())
781 return GO_TO_ROOT;
782 else if (retval == ONPLAY_PLAYLIST)
783 return GO_TO_PLAYLIST_VIEWER;
784 #ifdef HAVE_PICTUREFLOW_INTEGRATION
785 else if (retval == ONPLAY_PICTUREFLOW)
786 return GO_TO_PICTUREFLOW;
787 #endif
788 restore = true;
790 break;
792 case ACTION_WPS_BROWSE:
793 #ifdef HAVE_LCD_CHARCELLS
794 status_set_record(false);
795 status_set_audio(false);
796 #endif
797 gwps_leave_wps();
798 return GO_TO_PREVIOUS_BROWSER;
799 break;
801 /* play/pause */
802 case ACTION_WPS_PLAY:
803 if (global_settings.party_mode)
804 break;
805 if ( state->paused )
807 state->paused = false;
808 if ( global_settings.fade_on_stop )
809 fade(true, true);
810 else
811 audio_resume();
813 else
815 state->paused = true;
816 if ( global_settings.fade_on_stop )
817 fade(false, true);
818 else
819 audio_pause();
820 settings_save();
821 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
822 call_storage_idle_notifys(true); /* make sure resume info is saved */
823 #endif
825 break;
827 case ACTION_WPS_VOLUP:
828 global_settings.volume++;
829 vol_changed = true;
830 break;
831 case ACTION_WPS_VOLDOWN:
832 global_settings.volume--;
833 vol_changed = true;
834 break;
835 /* fast forward
836 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
837 case ACTION_WPS_SEEKFWD:
838 if (global_settings.party_mode)
839 break;
840 if (current_tick -last_right < HZ)
842 if (state->id3->cuesheet)
844 audio_next();
846 else
848 change_dir(1);
851 else
852 ffwd_rew(ACTION_WPS_SEEKFWD);
853 last_right = last_left = 0;
854 break;
855 /* fast rewind
856 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
857 case ACTION_WPS_SEEKBACK:
858 if (global_settings.party_mode)
859 break;
860 if (current_tick -last_left < HZ)
862 if (state->id3->cuesheet)
864 if (!state->paused)
865 #if (CONFIG_CODEC == SWCODEC)
866 audio_pre_ff_rewind();
867 #else
868 audio_pause();
869 #endif
870 audio_ff_rewind(0);
872 else
874 change_dir(-1);
877 else
878 ffwd_rew(ACTION_WPS_SEEKBACK);
879 last_left = last_right = 0;
880 break;
882 /* prev / restart */
883 case ACTION_WPS_SKIPPREV:
884 if (global_settings.party_mode)
885 break;
886 last_left = current_tick;
887 #ifdef AB_REPEAT_ENABLE
888 /* if we're in A/B repeat mode and the current position
889 is past the A marker, jump back to the A marker... */
890 if ( ab_repeat_mode_enabled() )
892 if ( ab_after_A_marker(state->id3->elapsed) )
894 ab_jump_to_A_marker();
895 break;
898 else
899 /* ...otherwise, do it normally */
900 #endif
901 play_hop(-1);
902 break;
904 /* next
905 OR if skip length set, hop by predetermined amount. */
906 case ACTION_WPS_SKIPNEXT:
907 if (global_settings.party_mode)
908 break;
909 last_right = current_tick;
910 #ifdef AB_REPEAT_ENABLE
911 /* if we're in A/B repeat mode and the current position is
912 before the A marker, jump to the A marker... */
913 if ( ab_repeat_mode_enabled() )
915 if ( ab_before_A_marker(state->id3->elapsed) )
917 ab_jump_to_A_marker();
918 break;
921 else
922 /* ...otherwise, do it normally */
923 #endif
924 play_hop(1);
925 break;
926 /* next / prev directories */
927 /* and set A-B markers if in a-b mode */
928 case ACTION_WPS_ABSETB_NEXTDIR:
929 if (global_settings.party_mode)
930 break;
931 #if defined(AB_REPEAT_ENABLE)
932 if (ab_repeat_mode_enabled())
934 ab_set_B_marker(state->id3->elapsed);
935 ab_jump_to_A_marker();
937 else
938 #endif
940 change_dir(1);
942 break;
943 case ACTION_WPS_ABSETA_PREVDIR:
944 if (global_settings.party_mode)
945 break;
946 #if defined(AB_REPEAT_ENABLE)
947 if (ab_repeat_mode_enabled())
948 ab_set_A_marker(state->id3->elapsed);
949 else
950 #endif
952 change_dir(-1);
954 break;
955 /* menu key functions */
956 case ACTION_WPS_MENU:
957 gwps_leave_wps();
958 return GO_TO_ROOT;
959 break;
962 #ifdef HAVE_QUICKSCREEN
963 case ACTION_WPS_QUICKSCREEN:
965 gwps_leave_wps();
966 if (quick_screen_quick(button))
967 return GO_TO_ROOT;
968 restore = true;
970 break;
971 #endif /* HAVE_QUICKSCREEN */
973 /* screen settings */
974 #ifdef BUTTON_F3
975 case ACTION_F3:
977 gwps_leave_wps();
978 if (quick_screen_f3(BUTTON_F3))
979 return GO_TO_ROOT;
980 restore = true;
982 break;
983 #endif /* BUTTON_F3 */
985 /* pitch screen */
986 #ifdef HAVE_PITCHSCREEN
987 case ACTION_WPS_PITCHSCREEN:
989 gwps_leave_wps();
990 if (1 == gui_syncpitchscreen_run())
991 return GO_TO_ROOT;
992 restore = true;
994 break;
995 #endif /* HAVE_PITCHSCREEN */
997 #ifdef AB_REPEAT_ENABLE
998 /* reset A&B markers */
999 case ACTION_WPS_ABRESET:
1000 if (ab_repeat_mode_enabled())
1002 ab_reset_markers();
1003 update = true;
1005 break;
1006 #endif /* AB_REPEAT_ENABLE */
1008 /* stop and exit wps */
1009 case ACTION_WPS_STOP:
1010 if (global_settings.party_mode)
1011 break;
1012 bookmark = true;
1013 exit = true;
1014 break;
1016 case ACTION_WPS_ID3SCREEN:
1018 gwps_leave_wps();
1019 if (browse_id3())
1020 return GO_TO_ROOT;
1021 restore = true;
1023 break;
1024 #ifdef HAVE_TOUCHSCREEN
1025 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1027 global_settings.playlist_shuffle =
1028 !global_settings.playlist_shuffle;
1029 #if CONFIG_CODEC == SWCODEC
1030 dsp_set_replaygain();
1031 #endif
1032 if (global_settings.playlist_shuffle)
1033 playlist_randomise(NULL, current_tick, true);
1034 else
1035 playlist_sort(NULL, true);
1037 break;
1038 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1040 const struct settings_list *rep_setting =
1041 find_setting(&global_settings.repeat_mode, NULL);
1042 option_select_next_val(rep_setting, false, true);
1043 audio_flush_and_reload_tracks();
1045 break;
1046 #endif /* HAVE_TOUCHSCREEN */
1047 /* this case is used by the softlock feature
1048 * it requests a full update here */
1049 case ACTION_REDRAW:
1050 skin_request_full_update(WPS);
1051 break;
1052 case ACTION_NONE: /* Timeout, do a partial update */
1053 update = true;
1054 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1055 break;
1056 #ifdef HAVE_RECORDING
1057 case ACTION_WPS_REC:
1058 exit = true;
1059 break;
1060 #endif
1061 case ACTION_WPS_VIEW_PLAYLIST:
1062 gwps_leave_wps();
1063 return GO_TO_PLAYLIST_VIEWER;
1064 break;
1065 default:
1066 switch(default_event_handler(button))
1067 { /* music has been stopped by the default handler */
1068 case SYS_USB_CONNECTED:
1069 case SYS_CALL_INCOMING:
1070 case BUTTON_MULTIMEDIA_STOP:
1071 gwps_leave_wps();
1072 return GO_TO_ROOT;
1074 update = true;
1075 break;
1078 if (vol_changed)
1080 bool res = false;
1081 vol_changed = false;
1082 setvol();
1083 FOR_NB_SCREENS(i)
1085 if(update_onvol_change(i))
1086 res = true;
1088 if (res) {
1089 restore = true;
1090 restoretimer = RESTORE_WPS_NEXT_SECOND;
1095 if (restore &&
1096 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1097 TIME_AFTER(current_tick, restoretimer)))
1099 restore = false;
1100 restoretimer = RESTORE_WPS_INSTANTLY;
1101 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1102 add_event(LCD_EVENT_ACTIVATION, false, wps_lcd_activation_hook);
1103 #endif
1104 /* we remove the update delay since it's not very usable in the wps,
1105 * e.g. during volume changing or ffwd/rewind */
1106 sb_skin_set_update_delay(0);
1107 skin_request_full_update(WPS);
1108 update = true;
1109 gwps_enter_wps();
1111 else
1113 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1114 gwps_caption_backlight(state);
1115 #endif
1116 FOR_NB_SCREENS(i)
1118 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1119 /* currently, all remotes are readable without backlight
1120 * so still update those */
1121 if (lcd_active() || (i != SCREEN_MAIN))
1122 #endif
1124 bool full_update = skin_do_full_update(WPS, i);
1125 if (update || full_update)
1127 skin_update(WPS, i, full_update ?
1128 SKIN_REFRESH_ALL : SKIN_REFRESH_NON_STATIC);
1132 update = false;
1135 if (exit) {
1136 #ifdef HAVE_LCD_CHARCELLS
1137 status_set_record(false);
1138 status_set_audio(false);
1139 #endif
1140 if (global_settings.fade_on_stop)
1141 fade(false, true);
1143 if (bookmark)
1144 bookmark_autobookmark(true);
1145 audio_stop();
1146 #ifdef AB_REPEAT_ENABLE
1147 ab_reset_markers();
1148 #endif
1149 gwps_leave_wps();
1150 #ifdef HAVE_RECORDING
1151 if (button == ACTION_WPS_REC)
1152 return GO_TO_RECSCREEN;
1153 #endif
1154 if (global_settings.browse_current)
1155 return GO_TO_PREVIOUS_BROWSER;
1156 return GO_TO_PREVIOUS;
1159 if (button && !IS_SYSEVENT(button) )
1160 storage_spin();
1162 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1165 /* this is called from the playback thread so NO DRAWING! */
1166 static void track_changed_callback(void *param)
1168 struct wps_state *state = skin_get_global_state();
1169 state->id3 = (struct mp3entry*)param;
1170 state->nid3 = audio_next_track();
1171 if (state->id3->cuesheet)
1173 cue_find_current_track(state->id3->cuesheet, state->id3->elapsed);
1175 skin_request_full_update(WPS);
1177 static void nextid3available_callback(void* param)
1179 (void)param;
1180 skin_get_global_state()->nid3 = audio_next_track();
1181 skin_request_full_update(WPS);
1185 static void wps_state_init(void)
1187 struct wps_state *state = skin_get_global_state();
1188 state->ff_rewind = false;
1189 state->paused = false;
1190 if(audio_status() & AUDIO_STATUS_PLAY)
1192 state->id3 = audio_current_track();
1193 state->nid3 = audio_next_track();
1195 else
1197 state->id3 = NULL;
1198 state->nid3 = NULL;
1200 /* We'll be updating due to restore initialized with true */
1201 skin_request_full_update(WPS);
1202 /* add the WPS track event callbacks */
1203 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1204 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1208 #ifdef IPOD_ACCESSORY_PROTOCOL
1209 bool is_wps_fading(void)
1211 return skin_get_global_state()->is_fading;
1214 int wps_get_ff_rewind_count(void)
1216 return skin_get_global_state()->ff_rewind_count;
1218 #endif