Manual: The c200v2 also needs to be set to MSC mode for installation.
[maemo-rb.git] / apps / gui / wps.c
blob1acd00d120ca23eae0d335cd6d3bea4cf6ce63d2
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 #endif
46 #include "lang.h"
47 #include "bookmark.h"
48 #include "misc.h"
49 #include "sound.h"
50 #include "onplay.h"
51 #include "abrepeat.h"
52 #include "playback.h"
53 #include "splash.h"
54 #include "cuesheet.h"
55 #include "ata_idle_notify.h"
56 #include "root_menu.h"
57 #include "backdrop.h"
58 #include "quickscreen.h"
59 #include "pitchscreen.h"
60 #include "appevents.h"
61 #include "viewport.h"
62 #include "pcmbuf.h"
63 #include "option_select.h"
64 #include "playlist_viewer.h"
65 #include "wps.h"
66 #include "statusbar-skinned.h"
68 #define RESTORE_WPS_INSTANTLY 0l
69 #define RESTORE_WPS_NEXT_SECOND ((long)(HZ+current_tick))
71 #define FF_REWIND_MAX_PERCENT 3 /* cap ff/rewind step size at max % of file */
72 /* 3% of 30min file == 54s step size */
73 #define MIN_FF_REWIND_STEP 500
75 /* initial setup of wps_data */
76 static void wps_state_init(void);
77 static void track_changed_callback(void *param);
78 static void nextid3available_callback(void* param);
80 #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps"
81 #ifdef HAVE_REMOTE_LCD
82 #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps"
83 #define DEFAULT_WPS(screen) ((screen) == SCREEN_MAIN ? \
84 WPS_DEFAULTCFG:RWPS_DEFAULTCFG)
85 #else
86 #define DEFAULT_WPS(screen) (WPS_DEFAULTCFG)
87 #endif
89 char* wps_default_skin(enum screen_type screen)
91 static char *skin_buf[NB_SCREENS] = {
92 #ifdef HAVE_LCD_BITMAP
93 #if LCD_DEPTH > 1
94 "%X(d)\n"
95 #endif
96 "%s%?it<%?in<%in. |>%it|%fn>\n"
97 "%s%?ia<%ia|%?d(2)<%d(2)|%(root%)>>\n"
98 "%s%?id<%id|%?d(1)<%d(1)|%(root%)>> %?iy<%(%iy%)|>\n\n"
99 "%al%pc/%pt%ar[%pp:%pe]\n"
100 "%fbkBit %?fv<avg|> %?iv<%(id3v%iv%)|%(no id3%)>\n"
101 "%pb\n%pm\n",
102 #else
103 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d(2)> - %?id<%id|%d(1)>\n"
104 "%pc%?ps<*|/>%pt\n",
105 #endif
106 #ifdef HAVE_REMOTE_LCD
107 #if LCD_REMOTE_DEPTH > 1
108 "%X(d)\n"
109 #endif
110 "%s%?ia<%ia|%?d(2)<%d(2)|%(root%)>>\n"
111 "%s%?it<%?in<%in. |>%it|%fn>\n"
112 "%al%pc/%pt%ar[%pp:%pe]\n"
113 "%fbkBit %?fv<avg|> %?iv<%(id3v%iv%)|%(no id3%)>\n"
114 "%pb\n",
115 #endif
117 return skin_buf[screen];
120 static void update_non_static(void)
122 FOR_NB_SCREENS(i)
123 skin_update(WPS, i, SKIN_REFRESH_NON_STATIC);
126 void pause_action(bool may_fade, bool updatewps)
128 #if CONFIG_CODEC == SWCODEC
129 /* Do audio first, then update, unless skin were to use its local
130 status in which case, reverse it */
131 audio_pause();
133 if (updatewps)
134 update_non_static();
135 #else
136 if (may_fade && global_settings.fade_on_stop)
137 fade(false, updatewps);
138 else
139 audio_pause();
140 #endif
142 if (global_settings.pause_rewind) {
143 long newpos;
145 #if (CONFIG_CODEC == SWCODEC)
146 audio_pre_ff_rewind();
147 #endif
148 newpos = audio_current_track()->elapsed
149 - global_settings.pause_rewind * 1000;
150 audio_ff_rewind(newpos > 0 ? newpos : 0);
153 (void)may_fade;
156 void unpause_action(bool may_fade, bool updatewps)
158 #if CONFIG_CODEC == SWCODEC
159 /* Do audio first, then update, unless skin were to use its local
160 status in which case, reverse it */
161 audio_resume();
163 if (updatewps)
164 update_non_static();
165 #else
166 if (may_fade && global_settings.fade_on_stop)
167 fade(true, updatewps);
168 else
169 audio_resume();
170 #endif
172 (void)may_fade;
175 #if CONFIG_CODEC != SWCODEC
176 void fade(bool fade_in, bool updatewps)
178 int fp_global_vol = global_settings.volume << 8;
179 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
180 int fp_step = (fp_global_vol - fp_min_vol) / 10;
182 skin_get_global_state()->is_fading = !fade_in;
183 if (fade_in) {
184 /* fade in */
185 int fp_volume = fp_min_vol;
187 /* zero out the sound */
188 sound_set_volume(fp_min_vol >> 8);
190 sleep(HZ/10); /* let audio thread run */
191 audio_resume();
193 if (updatewps)
194 update_non_static();
196 while (fp_volume < fp_global_vol - fp_step) {
197 fp_volume += fp_step;
198 sound_set_volume(fp_volume >> 8);
199 sleep(1);
201 sound_set_volume(global_settings.volume);
203 else {
204 /* fade out */
205 int fp_volume = fp_global_vol;
207 if (updatewps)
208 update_non_static();
210 while (fp_volume > fp_min_vol + fp_step) {
211 fp_volume -= fp_step;
212 sound_set_volume(fp_volume >> 8);
213 sleep(1);
215 audio_pause();
217 skin_get_global_state()->is_fading = false;
218 #if CONFIG_CODEC != SWCODEC
219 #ifndef SIMULATOR
220 /* let audio thread run and wait for the mas to run out of data */
221 while (!mp3_pause_done())
222 #endif
223 sleep(HZ/10);
224 #endif
226 /* reset volume to what it was before the fade */
227 sound_set_volume(global_settings.volume);
230 #endif /* SWCODEC */
232 static bool update_onvol_change(enum screen_type screen)
234 skin_update(WPS, screen, SKIN_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 #ifdef HAVE_TOUCHSCREEN
246 static int skintouch_to_wps(struct wps_data *data)
248 int offset = 0;
249 struct touchregion *region;
250 int button = skin_get_touchaction(data, &offset, &region);
251 switch (button)
253 case ACTION_STD_PREV:
254 return ACTION_WPS_SKIPPREV;
255 case ACTION_STD_PREVREPEAT:
256 return ACTION_WPS_SEEKBACK;
257 case ACTION_STD_NEXT:
258 return ACTION_WPS_SKIPNEXT;
259 case ACTION_STD_NEXTREPEAT:
260 return ACTION_WPS_SEEKFWD;
261 case ACTION_STD_MENU:
262 return ACTION_WPS_MENU;
263 case ACTION_STD_CONTEXT:
264 return ACTION_WPS_CONTEXT;
265 case ACTION_STD_QUICKSCREEN:
266 return ACTION_WPS_QUICKSCREEN;
267 #ifdef HAVE_HOTKEY
268 case ACTION_STD_HOTKEY:
269 return ACTION_WPS_HOTKEY;
270 #endif
271 case ACTION_TOUCH_SCROLLBAR:
272 skin_get_global_state()->id3->elapsed = skin_get_global_state()->id3->length*offset/100;
273 #if (CONFIG_CODEC == SWCODEC)
274 audio_pre_ff_rewind();
275 #else
276 if (!skin_get_global_state()->paused)
277 audio_pause();
278 #endif
279 audio_ff_rewind(skin_get_global_state()->id3->elapsed);
280 #if (CONFIG_CODEC != SWCODEC)
281 if (!skin_get_global_state()->paused)
282 audio_resume();
283 #endif
284 return ACTION_TOUCHSCREEN;
285 case ACTION_TOUCH_VOLUME:
287 const int min_vol = sound_min(SOUND_VOLUME);
288 const int max_vol = sound_max(SOUND_VOLUME);
289 global_settings.volume = (offset * (max_vol - min_vol)) / 100;
290 global_settings.volume += min_vol;
291 setvol();
293 return ACTION_TOUCHSCREEN;
295 return button;
297 #endif /* HAVE_TOUCHSCREEN */
299 bool ffwd_rew(int button)
301 unsigned int step = 0; /* current ff/rewind step */
302 unsigned int max_step = 0; /* maximum ff/rewind step */
303 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
304 int direction = -1; /* forward=1 or backward=-1 */
305 bool exit = false;
306 bool usb = false;
307 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
309 if (button == ACTION_NONE)
311 status_set_ffmode(0);
312 return usb;
314 while (!exit)
316 switch ( button )
318 case ACTION_WPS_SEEKFWD:
319 direction = 1;
320 case ACTION_WPS_SEEKBACK:
321 if (skin_get_global_state()->ff_rewind)
323 if (direction == 1)
325 /* fast forwarding, calc max step relative to end */
326 max_step = (skin_get_global_state()->id3->length -
327 (skin_get_global_state()->id3->elapsed +
328 ff_rewind_count)) *
329 FF_REWIND_MAX_PERCENT / 100;
331 else
333 /* rewinding, calc max step relative to start */
334 max_step = (skin_get_global_state()->id3->elapsed + ff_rewind_count) *
335 FF_REWIND_MAX_PERCENT / 100;
338 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
340 if (step > max_step)
341 step = max_step;
343 ff_rewind_count += step * direction;
345 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
346 step += step >> ff_rw_accel;
348 else
350 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
351 skin_get_global_state()->id3 && skin_get_global_state()->id3->length )
353 #if (CONFIG_CODEC == SWCODEC)
354 audio_pre_ff_rewind();
355 #else
356 if (!skin_get_global_state()->paused)
357 audio_pause();
358 #endif
359 #if CONFIG_KEYPAD == PLAYER_PAD
360 FOR_NB_SCREENS(i)
361 skin_get_gwps(WPS, i)->display->stop_scroll();
362 #endif
363 if (direction > 0)
364 status_set_ffmode(STATUS_FASTFORWARD);
365 else
366 status_set_ffmode(STATUS_FASTBACKWARD);
368 skin_get_global_state()->ff_rewind = true;
370 step = 1000 * global_settings.ff_rewind_min_step;
372 else
373 break;
376 if (direction > 0) {
377 if ((skin_get_global_state()->id3->elapsed + ff_rewind_count) >
378 skin_get_global_state()->id3->length)
379 ff_rewind_count = skin_get_global_state()->id3->length -
380 skin_get_global_state()->id3->elapsed;
382 else {
383 if ((int)(skin_get_global_state()->id3->elapsed + ff_rewind_count) < 0)
384 ff_rewind_count = -skin_get_global_state()->id3->elapsed;
387 /* set the wps state ff_rewind_count so the progess info
388 displays corectly */
389 skin_get_global_state()->ff_rewind_count = ff_rewind_count;
391 FOR_NB_SCREENS(i)
393 skin_update(WPS, i,
394 SKIN_REFRESH_PLAYER_PROGRESS |
395 SKIN_REFRESH_DYNAMIC);
398 break;
400 case ACTION_WPS_STOPSEEK:
401 skin_get_global_state()->id3->elapsed = skin_get_global_state()->id3->elapsed+ff_rewind_count;
402 audio_ff_rewind(skin_get_global_state()->id3->elapsed);
403 skin_get_global_state()->ff_rewind_count = 0;
404 skin_get_global_state()->ff_rewind = false;
405 status_set_ffmode(0);
406 #if (CONFIG_CODEC != SWCODEC)
407 if (!skin_get_global_state()->paused)
408 audio_resume();
409 #endif
410 #ifdef HAVE_LCD_CHARCELLS
411 FOR_NB_SCREENS(i)
412 skin_update(WPS, i, SKIN_REFRESH_ALL);
413 #endif
414 exit = true;
415 break;
417 default:
418 if(default_event_handler(button) == SYS_USB_CONNECTED) {
419 status_set_ffmode(0);
420 usb = true;
421 exit = true;
423 break;
425 if (!exit)
427 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
428 #ifdef HAVE_TOUCHSCREEN
429 if (button == ACTION_TOUCHSCREEN)
430 button = skintouch_to_wps(skin_get_gwps(WPS, SCREEN_MAIN)->data);
431 #endif
432 if (button != ACTION_WPS_SEEKFWD &&
433 button != ACTION_WPS_SEEKBACK)
434 button = ACTION_WPS_STOPSEEK;
437 return usb;
440 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
441 static void gwps_caption_backlight(struct wps_state *state)
443 if (state && state->id3)
445 #ifdef HAVE_BACKLIGHT
446 if (global_settings.caption_backlight)
448 /* turn on backlight n seconds before track ends, and turn it off n
449 seconds into the new track. n == backlight_timeout, or 5s */
450 int n = global_settings.backlight_timeout * 1000;
452 if ( n < 1000 )
453 n = 5000; /* use 5s if backlight is always on or off */
455 if (((state->id3->elapsed < 1000) ||
456 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
457 (state->paused == false))
458 backlight_on();
460 #endif
461 #ifdef HAVE_REMOTE_LCD
462 if (global_settings.remote_caption_backlight)
464 /* turn on remote backlight n seconds before track ends, and turn it
465 off n seconds into the new track. n == remote_backlight_timeout,
466 or 5s */
467 int n = global_settings.remote_backlight_timeout * 1000;
469 if ( n < 1000 )
470 n = 5000; /* use 5s if backlight is always on or off */
472 if (((state->id3->elapsed < 1000) ||
473 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
474 (state->paused == false))
475 remote_backlight_on();
477 #endif
480 #endif
483 static void change_dir(int direction)
485 if (global_settings.prevent_skip)
486 return;
488 if (direction < 0)
489 audio_prev_dir();
490 else if (direction > 0)
491 audio_next_dir();
492 /* prevent the next dir to immediatly start being ffw'd */
493 action_wait_for_release();
496 static void prev_track(unsigned long skip_thresh)
498 struct wps_state *state = skin_get_global_state();
499 if (state->id3->elapsed < skip_thresh)
501 audio_prev();
502 return;
504 else
506 if (state->id3->cuesheet)
508 curr_cuesheet_skip(state->id3->cuesheet, -1, state->id3->elapsed);
509 return;
512 #if (CONFIG_CODEC == SWCODEC)
513 audio_pre_ff_rewind();
514 #else
515 if (!state->paused)
516 audio_pause();
517 #endif
519 audio_ff_rewind(0);
521 #if (CONFIG_CODEC != SWCODEC)
522 if (!state->paused)
523 audio_resume();
524 #endif
528 static void next_track(void)
530 struct wps_state *state = skin_get_global_state();
531 /* take care of if we're playing a cuesheet */
532 if (state->id3->cuesheet)
534 if (curr_cuesheet_skip(state->id3->cuesheet, 1, state->id3->elapsed))
536 /* if the result was false, then we really want
537 to skip to the next track */
538 return;
542 audio_next();
545 static void play_hop(int direction)
547 struct wps_state *state = skin_get_global_state();
548 struct cuesheet *cue = state->id3->cuesheet;
549 long step = global_settings.skip_length*1000;
550 long elapsed = state->id3->elapsed;
551 long remaining = state->id3->length - elapsed;
553 /* if cuesheet is active, then we want the current tracks end instead of
554 * the total end */
555 if (cue && (cue->curr_track_idx+1 < cue->track_count))
557 int next = cue->curr_track_idx+1;
558 struct cue_track_info *t = &cue->tracks[next];
559 remaining = t->offset - elapsed;
562 if (step < 0)
564 if (direction < 0)
566 prev_track(DEFAULT_SKIP_TRESH);
567 return;
569 else if (remaining < DEFAULT_SKIP_TRESH*2)
571 next_track();
572 return;
574 else
575 elapsed += (remaining - DEFAULT_SKIP_TRESH*2);
577 else if (!global_settings.prevent_skip &&
578 (!step ||
579 (direction > 0 && step >= remaining) ||
580 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
581 { /* Do normal track skipping */
582 if (direction > 0)
583 next_track();
584 else if (direction < 0)
585 prev_track(DEFAULT_SKIP_TRESH);
586 return;
588 else if (direction == 1 && step >= remaining)
590 #if CONFIG_CODEC == SWCODEC
591 system_sound_play(SOUND_TRACK_NO_MORE);
592 #endif
593 return;
595 else if (direction == -1 && elapsed < step)
597 elapsed = 0;
599 else
601 elapsed += step * direction;
603 if(audio_status() & AUDIO_STATUS_PLAY)
605 #if (CONFIG_CODEC == SWCODEC)
606 audio_pre_ff_rewind();
607 #else
608 if (!state->paused)
609 audio_pause();
610 #endif
613 #if (CONFIG_CODEC == SWCODEC)
614 audio_ff_rewind(elapsed);
615 #else
616 audio_ff_rewind(state->id3->elapsed = elapsed);
617 if (!state->paused)
618 audio_resume();
619 #endif
623 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
625 * If the user is unable to see the wps, because the display is deactivated,
626 * we suppress updates until the wps is activated again (the lcd driver will
627 * call this hook to issue an instant update)
628 * */
629 static void wps_lcd_activation_hook(void *param)
631 (void)param;
632 skin_request_full_update(WPS);
633 /* force timeout in wps main loop, so that the update is instantly */
634 queue_post(&button_queue, BUTTON_NONE, 0);
636 #endif
638 static void gwps_leave_wps(void)
640 FOR_NB_SCREENS(i)
642 skin_get_gwps(WPS, i)->display->stop_scroll();
643 #ifdef HAVE_BACKDROP_IMAGE
644 skin_backdrop_show(sb_get_backdrop(i));
645 #endif
646 viewportmanager_theme_undo(i, skin_has_sbs(i, skin_get_gwps(WPS, i)->data));
650 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
651 /* Play safe and unregister the hook */
652 remove_event(LCD_EVENT_ACTIVATION, wps_lcd_activation_hook);
653 #endif
654 /* unhandle statusbar update delay */
655 sb_skin_set_update_delay(DEFAULT_UPDATE_DELAY);
656 #ifdef HAVE_TOUCHSCREEN
657 touchscreen_set_mode(global_settings.touch_mode);
658 #endif
662 * display the wps on entering or restoring */
663 static void gwps_enter_wps(void)
665 struct gui_wps *gwps;
666 struct screen *display;
667 FOR_NB_SCREENS(i)
669 gwps = skin_get_gwps(WPS, i);
670 display = gwps->display;
671 display->stop_scroll();
672 viewportmanager_theme_enable(i, skin_has_sbs(i, skin_get_gwps(WPS, i)->data), NULL);
674 /* Update the values in the first (default) viewport - in case the user
675 has modified the statusbar or colour settings */
676 #if LCD_DEPTH > 1
677 if (display->depth > 1)
679 struct skin_viewport *svp = skin_find_item(VP_DEFAULT_LABEL_STRING,
680 SKIN_FIND_VP, gwps->data);
681 if (svp)
683 struct viewport *vp = &svp->vp;
684 vp->fg_pattern = display->get_foreground();
685 vp->bg_pattern = display->get_background();
688 #endif
689 /* make the backdrop actually take effect */
690 #ifdef HAVE_BACKDROP_IMAGE
691 skin_backdrop_show(gwps->data->backdrop_id);
692 #endif
693 display->clear_display();
694 skin_update(WPS, i, SKIN_REFRESH_ALL);
697 #ifdef HAVE_TOUCHSCREEN
698 gwps = skin_get_gwps(WPS, SCREEN_MAIN);
699 skin_disarm_touchregions(gwps->data);
700 if (gwps->data->touchregions < 0)
701 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
702 #endif
703 /* force statusbar/skin update since we just cleared the whole screen */
704 send_event(GUI_EVENT_ACTIONUPDATE, (void*)1);
707 void wps_do_playpause(bool updatewps)
709 struct wps_state *state = skin_get_global_state();
710 if ( state->paused )
712 state->paused = false;
713 unpause_action(true, updatewps);
715 else
717 state->paused = true;
718 pause_action(true, updatewps);
719 settings_save();
720 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
721 call_storage_idle_notifys(true); /* make sure resume info is saved */
722 #endif
727 /* The WPS can be left in two ways:
728 * a) call a function, which draws over the wps. In this case, the wps
729 * will be still active (i.e. the below function didn't return)
730 * b) return with a value evaluated by root_menu.c, in this case the wps
731 * is really left, and root_menu will handle the next screen
733 * In either way, call gwps_leave_wps(), in order to restore the correct
734 * "main screen" backdrops and statusbars
736 long gui_wps_show(void)
738 long button = 0;
739 bool restore = true;
740 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
741 bool exit = false;
742 bool bookmark = false;
743 bool update = false;
744 bool vol_changed = false;
745 long last_left = 0, last_right = 0;
746 struct wps_state *state = skin_get_global_state();
748 #ifdef HAVE_LCD_CHARCELLS
749 status_set_audio(true);
750 status_set_param(false);
751 #endif
753 #ifdef AB_REPEAT_ENABLE
754 ab_repeat_init();
755 ab_reset_markers();
756 #endif
757 wps_state_init();
759 while ( 1 )
761 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
763 /* did someone else (i.e power thread) change audio pause mode? */
764 if (state->paused != audio_paused) {
765 state->paused = audio_paused;
767 /* if another thread paused audio, we are probably in car mode,
768 about to shut down. lets save the settings. */
769 if (state->paused) {
770 settings_save();
771 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
772 call_storage_idle_notifys(true);
773 #endif
776 button = skin_wait_for_action(WPS, CONTEXT_WPS|ALLOW_SOFTLOCK,
777 restore ? 1 : 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 = skintouch_to_wps(skin_get_gwps(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 #ifdef HAVE_HOTKEY
814 case ACTION_WPS_HOTKEY:
815 if (!global_settings.hotkey_wps)
816 break;
817 /* fall through */
818 #endif
819 case ACTION_WPS_CONTEXT:
821 bool hotkey = button == ACTION_WPS_HOTKEY;
822 gwps_leave_wps();
823 int retval = onplay(state->id3->path,
824 FILE_ATTR_AUDIO, CONTEXT_WPS, hotkey);
825 /* if music is stopped in the context menu we want to exit the wps */
826 if (retval == ONPLAY_MAINMENU
827 || !audio_status())
828 return GO_TO_ROOT;
829 else if (retval == ONPLAY_PLAYLIST)
830 return GO_TO_PLAYLIST_VIEWER;
831 #ifdef HAVE_PICTUREFLOW_INTEGRATION
832 else if (retval == ONPLAY_PICTUREFLOW)
833 return GO_TO_PICTUREFLOW;
834 #endif
835 restore = true;
837 break;
839 case ACTION_WPS_BROWSE:
840 #ifdef HAVE_LCD_CHARCELLS
841 status_set_record(false);
842 status_set_audio(false);
843 #endif
844 gwps_leave_wps();
845 return GO_TO_PREVIOUS_BROWSER;
846 break;
848 /* play/pause */
849 case ACTION_WPS_PLAY:
850 if (global_settings.party_mode)
851 break;
852 wps_do_playpause(true);
853 break;
855 case ACTION_WPS_VOLUP:
856 global_settings.volume++;
857 vol_changed = true;
858 break;
859 case ACTION_WPS_VOLDOWN:
860 global_settings.volume--;
861 vol_changed = true;
862 break;
863 /* fast forward
864 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
865 case ACTION_WPS_SEEKFWD:
866 if (global_settings.party_mode)
867 break;
868 if (current_tick -last_right < HZ)
870 if (state->id3->cuesheet)
872 audio_next();
874 else
876 change_dir(1);
879 else
880 ffwd_rew(ACTION_WPS_SEEKFWD);
881 last_right = last_left = 0;
882 break;
883 /* fast rewind
884 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
885 case ACTION_WPS_SEEKBACK:
886 if (global_settings.party_mode)
887 break;
888 if (current_tick -last_left < HZ)
890 if (state->id3->cuesheet)
892 #if (CONFIG_CODEC == SWCODEC)
893 audio_pre_ff_rewind();
894 #else
895 if (!state->paused)
896 audio_pause();
897 #endif
898 audio_ff_rewind(0);
900 else
902 change_dir(-1);
905 else
906 ffwd_rew(ACTION_WPS_SEEKBACK);
907 last_left = last_right = 0;
908 break;
910 /* prev / restart */
911 case ACTION_WPS_SKIPPREV:
912 if (global_settings.party_mode)
913 break;
914 last_left = current_tick;
915 #ifdef AB_REPEAT_ENABLE
916 /* if we're in A/B repeat mode and the current position
917 is past the A marker, jump back to the A marker... */
918 if ( ab_repeat_mode_enabled() )
920 if ( ab_after_A_marker(state->id3->elapsed) )
922 ab_jump_to_A_marker();
923 break;
926 else
927 /* ...otherwise, do it normally */
928 #endif
929 play_hop(-1);
930 break;
932 /* next
933 OR if skip length set, hop by predetermined amount. */
934 case ACTION_WPS_SKIPNEXT:
935 if (global_settings.party_mode)
936 break;
937 last_right = current_tick;
938 #ifdef AB_REPEAT_ENABLE
939 /* if we're in A/B repeat mode and the current position is
940 before the A marker, jump to the A marker... */
941 if ( ab_repeat_mode_enabled() )
943 if ( ab_before_A_marker(state->id3->elapsed) )
945 ab_jump_to_A_marker();
946 break;
949 else
950 /* ...otherwise, do it normally */
951 #endif
952 play_hop(1);
953 break;
954 /* next / prev directories */
955 /* and set A-B markers if in a-b mode */
956 case ACTION_WPS_ABSETB_NEXTDIR:
957 if (global_settings.party_mode)
958 break;
959 #if defined(AB_REPEAT_ENABLE)
960 if (ab_repeat_mode_enabled())
962 ab_set_B_marker(state->id3->elapsed);
963 ab_jump_to_A_marker();
965 else
966 #endif
968 change_dir(1);
970 break;
971 case ACTION_WPS_ABSETA_PREVDIR:
972 if (global_settings.party_mode)
973 break;
974 #if defined(AB_REPEAT_ENABLE)
975 if (ab_repeat_mode_enabled())
976 ab_set_A_marker(state->id3->elapsed);
977 else
978 #endif
980 change_dir(-1);
982 break;
983 /* menu key functions */
984 case ACTION_WPS_MENU:
985 gwps_leave_wps();
986 return GO_TO_ROOT;
987 break;
990 #ifdef HAVE_QUICKSCREEN
991 case ACTION_WPS_QUICKSCREEN:
993 gwps_leave_wps();
994 if (quick_screen_quick(button))
995 return GO_TO_ROOT;
996 restore = true;
998 break;
999 #endif /* HAVE_QUICKSCREEN */
1001 /* screen settings */
1002 #ifdef BUTTON_F3
1003 case ACTION_F3:
1005 gwps_leave_wps();
1006 if (quick_screen_f3(BUTTON_F3))
1007 return GO_TO_ROOT;
1008 restore = true;
1010 break;
1011 #endif /* BUTTON_F3 */
1013 /* pitch screen */
1014 #ifdef HAVE_PITCHCONTROL
1015 case ACTION_WPS_PITCHSCREEN:
1017 gwps_leave_wps();
1018 if (1 == gui_syncpitchscreen_run())
1019 return GO_TO_ROOT;
1020 restore = true;
1022 break;
1023 #endif /* HAVE_PITCHCONTROL */
1025 #ifdef AB_REPEAT_ENABLE
1026 /* reset A&B markers */
1027 case ACTION_WPS_ABRESET:
1028 if (ab_repeat_mode_enabled())
1030 ab_reset_markers();
1031 update = true;
1033 break;
1034 #endif /* AB_REPEAT_ENABLE */
1036 /* stop and exit wps */
1037 case ACTION_WPS_STOP:
1038 if (global_settings.party_mode)
1039 break;
1040 bookmark = true;
1041 exit = true;
1042 break;
1044 case ACTION_WPS_LIST_BOOKMARKS:
1045 gwps_leave_wps();
1046 if (bookmark_load_menu() == BOOKMARK_USB_CONNECTED)
1048 return GO_TO_ROOT;
1050 restore = true;
1051 break;
1053 case ACTION_WPS_CREATE_BOOKMARK:
1054 gwps_leave_wps();
1055 bookmark_create_menu();
1056 restore = true;
1057 break;
1059 case ACTION_WPS_ID3SCREEN:
1061 gwps_leave_wps();
1062 if (browse_id3())
1063 return GO_TO_ROOT;
1064 restore = true;
1066 break;
1067 /* this case is used by the softlock feature
1068 * it requests a full update here */
1069 case ACTION_REDRAW:
1070 skin_request_full_update(WPS);
1071 break;
1072 case ACTION_NONE: /* Timeout, do a partial update */
1073 update = true;
1074 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1075 break;
1076 #ifdef HAVE_RECORDING
1077 case ACTION_WPS_REC:
1078 exit = true;
1079 break;
1080 #endif
1081 case ACTION_WPS_VIEW_PLAYLIST:
1082 gwps_leave_wps();
1083 return GO_TO_PLAYLIST_VIEWER;
1084 break;
1085 default:
1086 switch(default_event_handler(button))
1087 { /* music has been stopped by the default handler */
1088 case SYS_USB_CONNECTED:
1089 case SYS_CALL_INCOMING:
1090 case BUTTON_MULTIMEDIA_STOP:
1091 gwps_leave_wps();
1092 return GO_TO_ROOT;
1094 update = true;
1095 break;
1098 if (vol_changed)
1100 bool res = false;
1101 vol_changed = false;
1102 setvol();
1103 FOR_NB_SCREENS(i)
1105 if(update_onvol_change(i))
1106 res = true;
1108 if (res) {
1109 restore = true;
1110 restoretimer = RESTORE_WPS_NEXT_SECOND;
1115 if (restore &&
1116 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1117 TIME_AFTER(current_tick, restoretimer)))
1119 restore = false;
1120 restoretimer = RESTORE_WPS_INSTANTLY;
1121 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1122 add_event(LCD_EVENT_ACTIVATION, false, wps_lcd_activation_hook);
1123 #endif
1124 /* we remove the update delay since it's not very usable in the wps,
1125 * e.g. during volume changing or ffwd/rewind */
1126 sb_skin_set_update_delay(0);
1127 skin_request_full_update(WPS);
1128 update = true;
1129 gwps_enter_wps();
1131 else
1133 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1134 gwps_caption_backlight(state);
1135 #endif
1136 FOR_NB_SCREENS(i)
1138 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1139 /* currently, all remotes are readable without backlight
1140 * so still update those */
1141 if (lcd_active() || (i != SCREEN_MAIN))
1142 #endif
1144 bool full_update = skin_do_full_update(WPS, i);
1145 if (update || full_update)
1147 skin_update(WPS, i, full_update ?
1148 SKIN_REFRESH_ALL : SKIN_REFRESH_NON_STATIC);
1152 update = false;
1155 if (exit) {
1156 #ifdef HAVE_LCD_CHARCELLS
1157 status_set_record(false);
1158 status_set_audio(false);
1159 #endif
1160 #if CONFIG_CODEC != SWCODEC
1161 if (global_settings.fade_on_stop)
1162 fade(false, true);
1163 #else
1164 audio_pause();
1165 update_non_static();
1166 #endif
1167 if (bookmark)
1168 bookmark_autobookmark(true);
1169 audio_stop();
1170 #ifdef AB_REPEAT_ENABLE
1171 ab_reset_markers();
1172 #endif
1173 gwps_leave_wps();
1174 #ifdef HAVE_RECORDING
1175 if (button == ACTION_WPS_REC)
1176 return GO_TO_RECSCREEN;
1177 #endif
1178 if (global_settings.browse_current)
1179 return GO_TO_PREVIOUS_BROWSER;
1180 return GO_TO_PREVIOUS;
1183 if (button && !IS_SYSEVENT(button) )
1184 storage_spin();
1186 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1189 /* this is called from the playback thread so NO DRAWING! */
1190 static void track_changed_callback(void *param)
1192 struct wps_state *state = skin_get_global_state();
1193 state->id3 = (struct mp3entry*)param;
1194 state->nid3 = audio_next_track();
1195 if (state->id3->cuesheet)
1197 cue_find_current_track(state->id3->cuesheet, state->id3->elapsed);
1199 skin_request_full_update(WPS);
1201 static void nextid3available_callback(void* param)
1203 (void)param;
1204 skin_get_global_state()->nid3 = audio_next_track();
1205 skin_request_full_update(WPS);
1208 #ifdef AUDIO_FAST_SKIP_PREVIEW
1209 /* this is called on the audio_skip caller thread */
1210 static void track_skip_callback(void *param)
1212 struct wps_state *state = skin_get_global_state();
1213 state->id3 = audio_current_track();
1214 state->nid3 = audio_next_track();
1215 skin_request_full_update(WPS);
1216 (void)param;
1218 #endif /* AUDIO_FAST_SKIP_PREVIEW */
1220 static void wps_state_init(void)
1222 struct wps_state *state = skin_get_global_state();
1223 state->ff_rewind = false;
1224 state->paused = false;
1225 if(audio_status() & AUDIO_STATUS_PLAY)
1227 state->id3 = audio_current_track();
1228 state->nid3 = audio_next_track();
1230 else
1232 state->id3 = NULL;
1233 state->nid3 = NULL;
1235 /* We'll be updating due to restore initialized with true */
1236 skin_request_full_update(WPS);
1237 /* add the WPS track event callbacks */
1238 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1239 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1240 #if CONFIG_CODEC == SWCODEC
1241 /* Use the same callback as ..._TRACK_CHANGE for when remaining handles have
1242 finished */
1243 add_event(PLAYBACK_EVENT_CUR_TRACK_READY, false, track_changed_callback);
1244 #endif
1245 #ifdef AUDIO_FAST_SKIP_PREVIEW
1246 add_event(PLAYBACK_EVENT_TRACK_SKIP, false, track_skip_callback);
1247 #endif
1251 #ifdef IPOD_ACCESSORY_PROTOCOL
1252 bool is_wps_fading(void)
1254 return skin_get_global_state()->is_fading;
1257 int wps_get_ff_rewind_count(void)
1259 return skin_get_global_state()->ff_rewind_count;
1261 #endif