Remove code duplication in some generic skin touch action handling.
[maemo-rb.git] / apps / gui / wps.c
blob44e4adb372492739550ac1b6e22b4cce92fed02a
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;
243 return button;
245 #endif /* HAVE_TOUCHSCREEN */
247 bool ffwd_rew(int button)
249 unsigned int step = 0; /* current ff/rewind step */
250 unsigned int max_step = 0; /* maximum ff/rewind step */
251 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
252 int direction = -1; /* forward=1 or backward=-1 */
253 bool exit = false;
254 bool usb = false;
255 int i = 0;
256 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
258 if (button == ACTION_NONE)
260 status_set_ffmode(0);
261 return usb;
263 while (!exit)
265 switch ( button )
267 case ACTION_WPS_SEEKFWD:
268 direction = 1;
269 case ACTION_WPS_SEEKBACK:
270 if (skin_get_global_state()->ff_rewind)
272 if (direction == 1)
274 /* fast forwarding, calc max step relative to end */
275 max_step = (skin_get_global_state()->id3->length -
276 (skin_get_global_state()->id3->elapsed +
277 ff_rewind_count)) *
278 FF_REWIND_MAX_PERCENT / 100;
280 else
282 /* rewinding, calc max step relative to start */
283 max_step = (skin_get_global_state()->id3->elapsed + ff_rewind_count) *
284 FF_REWIND_MAX_PERCENT / 100;
287 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
289 if (step > max_step)
290 step = max_step;
292 ff_rewind_count += step * direction;
294 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
295 step += step >> ff_rw_accel;
297 else
299 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
300 skin_get_global_state()->id3 && skin_get_global_state()->id3->length )
302 if (!skin_get_global_state()->paused)
303 #if (CONFIG_CODEC == SWCODEC)
304 audio_pre_ff_rewind();
305 #else
306 audio_pause();
307 #endif
308 #if CONFIG_KEYPAD == PLAYER_PAD
309 FOR_NB_SCREENS(i)
310 skin_get_gwps(WPS, i)->display->stop_scroll();
311 #endif
312 if (direction > 0)
313 status_set_ffmode(STATUS_FASTFORWARD);
314 else
315 status_set_ffmode(STATUS_FASTBACKWARD);
317 skin_get_global_state()->ff_rewind = true;
319 step = 1000 * global_settings.ff_rewind_min_step;
321 else
322 break;
325 if (direction > 0) {
326 if ((skin_get_global_state()->id3->elapsed + ff_rewind_count) >
327 skin_get_global_state()->id3->length)
328 ff_rewind_count = skin_get_global_state()->id3->length -
329 skin_get_global_state()->id3->elapsed;
331 else {
332 if ((int)(skin_get_global_state()->id3->elapsed + ff_rewind_count) < 0)
333 ff_rewind_count = -skin_get_global_state()->id3->elapsed;
336 /* set the wps state ff_rewind_count so the progess info
337 displays corectly */
338 skin_get_global_state()->ff_rewind_count = ff_rewind_count;
340 FOR_NB_SCREENS(i)
342 skin_update(WPS, i,
343 SKIN_REFRESH_PLAYER_PROGRESS |
344 SKIN_REFRESH_DYNAMIC);
347 break;
349 case ACTION_WPS_STOPSEEK:
350 skin_get_global_state()->id3->elapsed = skin_get_global_state()->id3->elapsed+ff_rewind_count;
351 audio_ff_rewind(skin_get_global_state()->id3->elapsed);
352 skin_get_global_state()->ff_rewind_count = 0;
353 skin_get_global_state()->ff_rewind = false;
354 status_set_ffmode(0);
355 #if (CONFIG_CODEC != SWCODEC)
356 if (!skin_get_global_state()->paused)
357 audio_resume();
358 #endif
359 #ifdef HAVE_LCD_CHARCELLS
360 FOR_NB_SCREENS(i)
361 skin_update(WPS, i, SKIN_REFRESH_ALL);
362 #endif
363 exit = true;
364 break;
366 default:
367 if(default_event_handler(button) == SYS_USB_CONNECTED) {
368 status_set_ffmode(0);
369 usb = true;
370 exit = true;
372 break;
374 if (!exit)
376 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
377 #ifdef HAVE_TOUCHSCREEN
378 if (button == ACTION_TOUCHSCREEN)
379 button = skintouch_to_wps(skin_get_gwps(WPS, SCREEN_MAIN)->data);
380 if (button != ACTION_WPS_SEEKFWD &&
381 button != ACTION_WPS_SEEKBACK)
382 button = ACTION_WPS_STOPSEEK;
383 #endif
386 return usb;
390 void display_keylock_text(bool locked)
392 int i;
393 FOR_NB_SCREENS(i)
394 skin_get_gwps(WPS, i)->display->stop_scroll();
396 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
402 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
403 static void gwps_caption_backlight(struct wps_state *state)
405 if (state && state->id3)
407 #ifdef HAVE_BACKLIGHT
408 if (global_settings.caption_backlight)
410 /* turn on backlight n seconds before track ends, and turn it off n
411 seconds into the new track. n == backlight_timeout, or 5s */
412 int n = global_settings.backlight_timeout * 1000;
414 if ( n < 1000 )
415 n = 5000; /* use 5s if backlight is always on or off */
417 if (((state->id3->elapsed < 1000) ||
418 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
419 (state->paused == false))
420 backlight_on();
422 #endif
423 #ifdef HAVE_REMOTE_LCD
424 if (global_settings.remote_caption_backlight)
426 /* turn on remote backlight n seconds before track ends, and turn it
427 off n seconds into the new track. n == remote_backlight_timeout,
428 or 5s */
429 int n = global_settings.remote_backlight_timeout * 1000;
431 if ( n < 1000 )
432 n = 5000; /* use 5s if backlight is always on or off */
434 if (((state->id3->elapsed < 1000) ||
435 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
436 (state->paused == false))
437 remote_backlight_on();
439 #endif
442 #endif
445 static void change_dir(int direction)
447 if (global_settings.prevent_skip)
448 return;
450 if (direction < 0)
451 audio_prev_dir();
452 else if (direction > 0)
453 audio_next_dir();
454 /* prevent the next dir to immediatly start being ffw'd */
455 action_wait_for_release();
458 static void prev_track(unsigned long skip_thresh)
460 struct wps_state *state = skin_get_global_state();
461 if (state->id3->elapsed < skip_thresh)
463 audio_prev();
464 return;
466 else
468 if (state->id3->cuesheet)
470 curr_cuesheet_skip(state->id3->cuesheet, -1, state->id3->elapsed);
471 return;
474 if (!state->paused)
475 #if (CONFIG_CODEC == SWCODEC)
476 audio_pre_ff_rewind();
477 #else
478 audio_pause();
479 #endif
481 audio_ff_rewind(0);
483 #if (CONFIG_CODEC != SWCODEC)
484 if (!state->paused)
485 audio_resume();
486 #endif
490 static void next_track(void)
492 struct wps_state *state = skin_get_global_state();
493 /* take care of if we're playing a cuesheet */
494 if (state->id3->cuesheet)
496 if (curr_cuesheet_skip(state->id3->cuesheet, 1, state->id3->elapsed))
498 /* if the result was false, then we really want
499 to skip to the next track */
500 return;
504 audio_next();
507 static void play_hop(int direction)
509 struct wps_state *state = skin_get_global_state();
510 long step = global_settings.skip_length*1000;
511 long elapsed = state->id3->elapsed;
512 long remaining = state->id3->length - elapsed;
514 if (step < 0)
516 if (direction < 0)
518 prev_track(DEFAULT_SKIP_TRESH);
519 return;
521 else if (remaining < DEFAULT_SKIP_TRESH*2)
523 next_track();
524 return;
526 else
527 elapsed += (remaining - DEFAULT_SKIP_TRESH*2);
529 else if (!global_settings.prevent_skip &&
530 (!step ||
531 (direction > 0 && step >= remaining) ||
532 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
533 { /* Do normal track skipping */
534 if (direction > 0)
535 next_track();
536 else if (direction < 0)
537 prev_track(DEFAULT_SKIP_TRESH);
538 return;
540 else if (direction == 1 && step >= remaining)
542 #if CONFIG_CODEC == SWCODEC
543 if(global_settings.beep)
544 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
545 #endif
546 return;
548 else if ((direction == -1 && elapsed < step))
550 elapsed = 0;
552 else
554 elapsed += step * direction;
556 if((audio_status() & AUDIO_STATUS_PLAY) && !state->paused)
558 #if (CONFIG_CODEC == SWCODEC)
559 audio_pre_ff_rewind();
560 #else
561 audio_pause();
562 #endif
564 audio_ff_rewind(state->id3->elapsed = elapsed);
565 #if (CONFIG_CODEC != SWCODEC)
566 if (!state->paused)
567 audio_resume();
568 #endif
572 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
574 * If the user is unable to see the wps, because the display is deactivated,
575 * we suppress updates until the wps is activated again (the lcd driver will
576 * call this hook to issue an instant update)
577 * */
578 static void wps_lcd_activation_hook(void *param)
580 (void)param;
581 skin_request_full_update(WPS);
582 /* force timeout in wps main loop, so that the update is instantly */
583 queue_post(&button_queue, BUTTON_NONE, 0);
585 #endif
587 static void gwps_leave_wps(void)
589 int i;
591 FOR_NB_SCREENS(i)
593 skin_get_gwps(WPS, i)->display->stop_scroll();
594 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
595 skin_backdrop_show(sb_get_backdrop(i));
596 #endif
597 viewportmanager_theme_undo(i, skin_has_sbs(i, skin_get_gwps(WPS, i)->data));
601 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
602 /* Play safe and unregister the hook */
603 remove_event(LCD_EVENT_ACTIVATION, wps_lcd_activation_hook);
604 #endif
605 /* unhandle statusbar update delay */
606 sb_skin_set_update_delay(DEFAULT_UPDATE_DELAY);
607 #ifdef HAVE_TOUCHSCREEN
608 touchscreen_set_mode(global_settings.touch_mode);
609 #endif
613 * display the wps on entering or restoring */
614 static void gwps_enter_wps(void)
616 int i;
617 struct gui_wps *gwps;
618 struct screen *display;
619 FOR_NB_SCREENS(i)
621 gwps = skin_get_gwps(WPS, i);
622 display = gwps->display;
623 display->stop_scroll();
624 viewportmanager_theme_enable(i, skin_has_sbs(i, skin_get_gwps(WPS, i)->data), NULL);
626 /* Update the values in the first (default) viewport - in case the user
627 has modified the statusbar or colour settings */
628 #if LCD_DEPTH > 1
629 if (display->depth > 1)
631 struct skin_viewport *svp = find_viewport(VP_DEFAULT_LABEL,
632 false, gwps->data);
633 if (svp)
635 struct viewport *vp = &svp->vp;
636 vp->fg_pattern = display->get_foreground();
637 vp->bg_pattern = display->get_background();
640 #endif
641 /* make the backdrop actually take effect */
642 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
643 skin_backdrop_show(gwps->data->backdrop_id);
644 #endif
645 display->clear_display();
646 skin_update(WPS, i, SKIN_REFRESH_ALL);
649 #ifdef HAVE_TOUCHSCREEN
650 gwps = skin_get_gwps(WPS, SCREEN_MAIN);
651 skin_disarm_touchregions(gwps->data);
652 if (!gwps->data->touchregions)
653 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
654 #endif
655 /* force statusbar/skin update since we just cleared the whole screen */
656 send_event(GUI_EVENT_ACTIONUPDATE, (void*)1);
659 /* The WPS can be left in two ways:
660 * a) call a function, which draws over the wps. In this case, the wps
661 * will be still active (i.e. the below function didn't return)
662 * b) return with a value evaluated by root_menu.c, in this case the wps
663 * is really left, and root_menu will handle the next screen
665 * In either way, call gwps_leave_wps(), in order to restore the correct
666 * "main screen" backdrops and statusbars
668 long gui_wps_show(void)
670 long button = 0;
671 bool restore = true;
672 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
673 bool exit = false;
674 bool bookmark = false;
675 bool update = false;
676 bool vol_changed = false;
677 int i;
678 long last_left = 0, last_right = 0;
679 struct wps_state *state = skin_get_global_state();
681 #ifdef HAVE_LCD_CHARCELLS
682 status_set_audio(true);
683 status_set_param(false);
684 #endif
686 #ifdef AB_REPEAT_ENABLE
687 ab_repeat_init();
688 ab_reset_markers();
689 #endif
690 wps_state_init();
692 while ( 1 )
694 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
696 /* did someone else (i.e power thread) change audio pause mode? */
697 if (state->paused != audio_paused) {
698 state->paused = audio_paused;
700 /* if another thread paused audio, we are probably in car mode,
701 about to shut down. lets save the settings. */
702 if (state->paused) {
703 settings_save();
704 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
705 call_storage_idle_notifys(true);
706 #endif
709 button = skin_wait_for_action(WPS, CONTEXT_WPS|ALLOW_SOFTLOCK,
710 restore ? 1 : HZ/5);
712 /* Exit if audio has stopped playing. This happens e.g. at end of
713 playlist or if using the sleep timer. */
714 if (!(audio_status() & AUDIO_STATUS_PLAY))
715 exit = true;
716 #ifdef HAVE_TOUCHSCREEN
717 if (button == ACTION_TOUCHSCREEN)
718 button = skintouch_to_wps(skin_get_gwps(WPS, SCREEN_MAIN)->data);
719 #endif
720 /* The iPods/X5/M5 use a single button for the A-B mode markers,
721 defined as ACTION_WPSAB_SINGLE in their config files. */
722 #ifdef ACTION_WPSAB_SINGLE
723 if (!global_settings.party_mode && ab_repeat_mode_enabled())
725 static int wps_ab_state = 0;
726 if (button == ACTION_WPSAB_SINGLE)
728 switch (wps_ab_state)
730 case 0: /* set the A spot */
731 button = ACTION_WPS_ABSETA_PREVDIR;
732 break;
733 case 1: /* set the B spot */
734 button = ACTION_WPS_ABSETB_NEXTDIR;
735 break;
736 case 2:
737 button = ACTION_WPS_ABRESET;
738 break;
740 wps_ab_state = (wps_ab_state+1) % 3;
743 #endif
744 switch(button)
746 #ifdef HAVE_HOTKEY
747 case ACTION_WPS_HOTKEY:
748 if (!global_settings.hotkey_wps)
749 break;
750 /* fall through */
751 #endif
752 case ACTION_WPS_CONTEXT:
754 bool hotkey = button == ACTION_WPS_HOTKEY;
755 gwps_leave_wps();
756 int retval = onplay(state->id3->path,
757 FILE_ATTR_AUDIO, CONTEXT_WPS, hotkey);
758 /* if music is stopped in the context menu we want to exit the wps */
759 if (retval == ONPLAY_MAINMENU
760 || !audio_status())
761 return GO_TO_ROOT;
762 else if (retval == ONPLAY_PLAYLIST)
763 return GO_TO_PLAYLIST_VIEWER;
764 #ifdef HAVE_PICTUREFLOW_INTEGRATION
765 else if (retval == ONPLAY_PICTUREFLOW)
766 return GO_TO_PICTUREFLOW;
767 #endif
768 restore = true;
770 break;
772 case ACTION_WPS_BROWSE:
773 #ifdef HAVE_LCD_CHARCELLS
774 status_set_record(false);
775 status_set_audio(false);
776 #endif
777 gwps_leave_wps();
778 return GO_TO_PREVIOUS_BROWSER;
779 break;
781 /* play/pause */
782 case ACTION_WPS_PLAY:
783 if (global_settings.party_mode)
784 break;
785 if ( state->paused )
787 state->paused = false;
788 if ( global_settings.fade_on_stop )
789 fade(true, true);
790 else
791 audio_resume();
793 else
795 state->paused = true;
796 if ( global_settings.fade_on_stop )
797 fade(false, true);
798 else
799 audio_pause();
800 settings_save();
801 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
802 call_storage_idle_notifys(true); /* make sure resume info is saved */
803 #endif
805 break;
807 case ACTION_WPS_VOLUP:
808 global_settings.volume++;
809 vol_changed = true;
810 break;
811 case ACTION_WPS_VOLDOWN:
812 global_settings.volume--;
813 vol_changed = true;
814 break;
815 /* fast forward
816 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
817 case ACTION_WPS_SEEKFWD:
818 if (global_settings.party_mode)
819 break;
820 if (current_tick -last_right < HZ)
822 if (state->id3->cuesheet)
824 audio_next();
826 else
828 change_dir(1);
831 else
832 ffwd_rew(ACTION_WPS_SEEKFWD);
833 last_right = last_left = 0;
834 break;
835 /* fast rewind
836 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
837 case ACTION_WPS_SEEKBACK:
838 if (global_settings.party_mode)
839 break;
840 if (current_tick -last_left < HZ)
842 if (state->id3->cuesheet)
844 if (!state->paused)
845 #if (CONFIG_CODEC == SWCODEC)
846 audio_pre_ff_rewind();
847 #else
848 audio_pause();
849 #endif
850 audio_ff_rewind(0);
852 else
854 change_dir(-1);
857 else
858 ffwd_rew(ACTION_WPS_SEEKBACK);
859 last_left = last_right = 0;
860 break;
862 /* prev / restart */
863 case ACTION_WPS_SKIPPREV:
864 if (global_settings.party_mode)
865 break;
866 last_left = current_tick;
867 #ifdef AB_REPEAT_ENABLE
868 /* if we're in A/B repeat mode and the current position
869 is past the A marker, jump back to the A marker... */
870 if ( ab_repeat_mode_enabled() )
872 if ( ab_after_A_marker(state->id3->elapsed) )
874 ab_jump_to_A_marker();
875 break;
878 else
879 /* ...otherwise, do it normally */
880 #endif
881 play_hop(-1);
882 break;
884 /* next
885 OR if skip length set, hop by predetermined amount. */
886 case ACTION_WPS_SKIPNEXT:
887 if (global_settings.party_mode)
888 break;
889 last_right = current_tick;
890 #ifdef AB_REPEAT_ENABLE
891 /* if we're in A/B repeat mode and the current position is
892 before the A marker, jump to the A marker... */
893 if ( ab_repeat_mode_enabled() )
895 if ( ab_before_A_marker(state->id3->elapsed) )
897 ab_jump_to_A_marker();
898 break;
901 else
902 /* ...otherwise, do it normally */
903 #endif
904 play_hop(1);
905 break;
906 /* next / prev directories */
907 /* and set A-B markers if in a-b mode */
908 case ACTION_WPS_ABSETB_NEXTDIR:
909 if (global_settings.party_mode)
910 break;
911 #if defined(AB_REPEAT_ENABLE)
912 if (ab_repeat_mode_enabled())
914 ab_set_B_marker(state->id3->elapsed);
915 ab_jump_to_A_marker();
917 else
918 #endif
920 change_dir(1);
922 break;
923 case ACTION_WPS_ABSETA_PREVDIR:
924 if (global_settings.party_mode)
925 break;
926 #if defined(AB_REPEAT_ENABLE)
927 if (ab_repeat_mode_enabled())
928 ab_set_A_marker(state->id3->elapsed);
929 else
930 #endif
932 change_dir(-1);
934 break;
935 /* menu key functions */
936 case ACTION_WPS_MENU:
937 gwps_leave_wps();
938 return GO_TO_ROOT;
939 break;
942 #ifdef HAVE_QUICKSCREEN
943 case ACTION_WPS_QUICKSCREEN:
945 gwps_leave_wps();
946 if (quick_screen_quick(button))
947 return GO_TO_ROOT;
948 restore = true;
950 break;
951 #endif /* HAVE_QUICKSCREEN */
953 /* screen settings */
954 #ifdef BUTTON_F3
955 case ACTION_F3:
957 gwps_leave_wps();
958 if (quick_screen_f3(BUTTON_F3))
959 return GO_TO_ROOT;
960 restore = true;
962 break;
963 #endif /* BUTTON_F3 */
965 /* pitch screen */
966 #ifdef HAVE_PITCHSCREEN
967 case ACTION_WPS_PITCHSCREEN:
969 gwps_leave_wps();
970 if (1 == gui_syncpitchscreen_run())
971 return GO_TO_ROOT;
972 restore = true;
974 break;
975 #endif /* HAVE_PITCHSCREEN */
977 #ifdef AB_REPEAT_ENABLE
978 /* reset A&B markers */
979 case ACTION_WPS_ABRESET:
980 if (ab_repeat_mode_enabled())
982 ab_reset_markers();
983 update = true;
985 break;
986 #endif /* AB_REPEAT_ENABLE */
988 /* stop and exit wps */
989 case ACTION_WPS_STOP:
990 if (global_settings.party_mode)
991 break;
992 bookmark = true;
993 exit = true;
994 break;
996 case ACTION_WPS_ID3SCREEN:
998 gwps_leave_wps();
999 if (browse_id3())
1000 return GO_TO_ROOT;
1001 restore = true;
1003 break;
1004 #ifdef HAVE_TOUCHSCREEN
1005 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1007 global_settings.playlist_shuffle =
1008 !global_settings.playlist_shuffle;
1009 #if CONFIG_CODEC == SWCODEC
1010 dsp_set_replaygain();
1011 #endif
1012 if (global_settings.playlist_shuffle)
1013 playlist_randomise(NULL, current_tick, true);
1014 else
1015 playlist_sort(NULL, true);
1017 break;
1018 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1020 const struct settings_list *rep_setting =
1021 find_setting(&global_settings.repeat_mode, NULL);
1022 option_select_next_val(rep_setting, false, true);
1023 audio_flush_and_reload_tracks();
1025 break;
1026 #endif /* HAVE_TOUCHSCREEN */
1027 /* this case is used by the softlock feature
1028 * it requests a full update here */
1029 case ACTION_REDRAW:
1030 skin_request_full_update(WPS);
1031 break;
1032 case ACTION_NONE: /* Timeout, do a partial update */
1033 update = true;
1034 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1035 break;
1036 #ifdef HAVE_RECORDING
1037 case ACTION_WPS_REC:
1038 exit = true;
1039 break;
1040 #endif
1041 case ACTION_WPS_VIEW_PLAYLIST:
1042 gwps_leave_wps();
1043 return GO_TO_PLAYLIST_VIEWER;
1044 break;
1045 default:
1046 switch(default_event_handler(button))
1047 { /* music has been stopped by the default handler */
1048 case SYS_USB_CONNECTED:
1049 case SYS_CALL_INCOMING:
1050 case BUTTON_MULTIMEDIA_STOP:
1051 gwps_leave_wps();
1052 return GO_TO_ROOT;
1054 update = true;
1055 break;
1058 if (vol_changed)
1060 bool res = false;
1061 vol_changed = false;
1062 setvol();
1063 FOR_NB_SCREENS(i)
1065 if(update_onvol_change(i))
1066 res = true;
1068 if (res) {
1069 restore = true;
1070 restoretimer = RESTORE_WPS_NEXT_SECOND;
1075 if (restore &&
1076 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1077 TIME_AFTER(current_tick, restoretimer)))
1079 restore = false;
1080 restoretimer = RESTORE_WPS_INSTANTLY;
1081 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1082 add_event(LCD_EVENT_ACTIVATION, false, wps_lcd_activation_hook);
1083 #endif
1084 /* we remove the update delay since it's not very usable in the wps,
1085 * e.g. during volume changing or ffwd/rewind */
1086 sb_skin_set_update_delay(0);
1087 skin_request_full_update(WPS);
1088 update = true;
1089 gwps_enter_wps();
1091 else
1093 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1094 gwps_caption_backlight(state);
1095 #endif
1096 FOR_NB_SCREENS(i)
1098 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1099 /* currently, all remotes are readable without backlight
1100 * so still update those */
1101 if (lcd_active() || (i != SCREEN_MAIN))
1102 #endif
1104 bool full_update = skin_do_full_update(WPS, i);
1105 if (update || full_update)
1107 skin_update(WPS, i, full_update ?
1108 SKIN_REFRESH_ALL : SKIN_REFRESH_NON_STATIC);
1112 update = false;
1115 if (exit) {
1116 #ifdef HAVE_LCD_CHARCELLS
1117 status_set_record(false);
1118 status_set_audio(false);
1119 #endif
1120 if (global_settings.fade_on_stop)
1121 fade(false, true);
1123 if (bookmark)
1124 bookmark_autobookmark(true);
1125 audio_stop();
1126 #ifdef AB_REPEAT_ENABLE
1127 ab_reset_markers();
1128 #endif
1129 gwps_leave_wps();
1130 #ifdef HAVE_RECORDING
1131 if (button == ACTION_WPS_REC)
1132 return GO_TO_RECSCREEN;
1133 #endif
1134 if (global_settings.browse_current)
1135 return GO_TO_PREVIOUS_BROWSER;
1136 return GO_TO_PREVIOUS;
1139 if (button && !IS_SYSEVENT(button) )
1140 storage_spin();
1142 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1145 /* this is called from the playback thread so NO DRAWING! */
1146 static void track_changed_callback(void *param)
1148 struct wps_state *state = skin_get_global_state();
1149 state->id3 = (struct mp3entry*)param;
1150 state->nid3 = audio_next_track();
1151 if (state->id3->cuesheet)
1153 cue_find_current_track(state->id3->cuesheet, state->id3->elapsed);
1155 skin_request_full_update(WPS);
1157 static void nextid3available_callback(void* param)
1159 (void)param;
1160 skin_get_global_state()->nid3 = audio_next_track();
1161 skin_request_full_update(WPS);
1165 static void wps_state_init(void)
1167 struct wps_state *state = skin_get_global_state();
1168 state->ff_rewind = false;
1169 state->paused = false;
1170 if(audio_status() & AUDIO_STATUS_PLAY)
1172 state->id3 = audio_current_track();
1173 state->nid3 = audio_next_track();
1175 else
1177 state->id3 = NULL;
1178 state->nid3 = NULL;
1180 /* We'll be updating due to restore initialized with true */
1181 skin_request_full_update(WPS);
1182 /* add the WPS track event callbacks */
1183 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1184 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1188 #ifdef IPOD_ACCESSORY_PROTOCOL
1189 bool is_wps_fading(void)
1191 return skin_get_global_state()->is_fading;
1194 int wps_get_ff_rewind_count(void)
1196 return skin_get_global_state()->ff_rewind_count;
1198 #endif