Move Info tab content to a separate widget.
[maemo-rb.git] / apps / gui / wps.c
blob1e48e802027e0dd41670121895ca9698463cdb4c
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 "dsp.h"
65 #include "playlist_viewer.h"
66 #include "wps.h"
67 #include "statusbar-skinned.h"
69 #define RESTORE_WPS_INSTANTLY 0l
70 #define RESTORE_WPS_NEXT_SECOND ((long)(HZ+current_tick))
72 #define FF_REWIND_MAX_PERCENT 3 /* cap ff/rewind step size at max % of file */
73 /* 3% of 30min file == 54s step size */
74 #define MIN_FF_REWIND_STEP 500
76 /* initial setup of wps_data */
77 static void wps_state_init(void);
78 static void track_changed_callback(void *param);
79 static void nextid3available_callback(void* param);
81 #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps"
82 #ifdef HAVE_REMOTE_LCD
83 #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps"
84 #define DEFAULT_WPS(screen) ((screen) == SCREEN_MAIN ? \
85 WPS_DEFAULTCFG:RWPS_DEFAULTCFG)
86 #else
87 #define DEFAULT_WPS(screen) (WPS_DEFAULTCFG)
88 #endif
90 char* wps_default_skin(enum screen_type screen)
92 static char *skin_buf[NB_SCREENS] = {
93 #ifdef HAVE_LCD_BITMAP
94 #if LCD_DEPTH > 1
95 "%X(d)\n"
96 #endif
97 "%s%?it<%?in<%in. |>%it|%fn>\n"
98 "%s%?ia<%ia|%?d(2)<%d(2)|%(root%)>>\n"
99 "%s%?id<%id|%?d(1)<%d(1)|%(root%)>> %?iy<%(%iy%)|>\n\n"
100 "%al%pc/%pt%ar[%pp:%pe]\n"
101 "%fbkBit %?fv<avg|> %?iv<%(id3v%iv%)|%(no id3%)>\n"
102 "%pb\n%pm\n",
103 #else
104 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d(2)> - %?id<%id|%d(1)>\n"
105 "%pc%?ps<*|/>%pt\n",
106 #endif
107 #ifdef HAVE_REMOTE_LCD
108 #if LCD_REMOTE_DEPTH > 1
109 "%X(d)\n"
110 #endif
111 "%s%?ia<%ia|%?d(2)<%d(2)|%(root%)>>\n"
112 "%s%?it<%?in<%in. |>%it|%fn>\n"
113 "%al%pc/%pt%ar[%pp:%pe]\n"
114 "%fbkBit %?fv<avg|> %?iv<%(id3v%iv%)|%(no id3%)>\n"
115 "%pb\n",
116 #endif
118 return skin_buf[screen];
121 static void update_non_static(void)
123 FOR_NB_SCREENS(i)
124 skin_update(WPS, i, SKIN_REFRESH_NON_STATIC);
127 void pause_action(bool may_fade, bool updatewps)
129 #if CONFIG_CODEC == SWCODEC
130 /* Do audio first, then update, unless skin were to use its local
131 status in which case, reverse it */
132 audio_pause();
134 if (updatewps)
135 update_non_static();
136 #else
137 if (may_fade && global_settings.fade_on_stop)
138 fade(false, updatewps);
139 else
140 audio_pause();
141 #endif
143 if (global_settings.pause_rewind) {
144 long newpos;
146 #if (CONFIG_CODEC == SWCODEC)
147 audio_pre_ff_rewind();
148 #endif
149 newpos = audio_current_track()->elapsed
150 - global_settings.pause_rewind * 1000;
151 audio_ff_rewind(newpos > 0 ? newpos : 0);
154 (void)may_fade;
157 void unpause_action(bool may_fade, bool updatewps)
159 #if CONFIG_CODEC == SWCODEC
160 /* Do audio first, then update, unless skin were to use its local
161 status in which case, reverse it */
162 audio_resume();
164 if (updatewps)
165 update_non_static();
166 #else
167 if (may_fade && global_settings.fade_on_stop)
168 fade(true, updatewps);
169 else
170 audio_resume();
171 #endif
173 (void)may_fade;
176 #if CONFIG_CODEC != SWCODEC
177 void fade(bool fade_in, bool updatewps)
179 int fp_global_vol = global_settings.volume << 8;
180 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
181 int fp_step = (fp_global_vol - fp_min_vol) / 10;
183 skin_get_global_state()->is_fading = !fade_in;
184 if (fade_in) {
185 /* fade in */
186 int fp_volume = fp_min_vol;
188 /* zero out the sound */
189 sound_set_volume(fp_min_vol >> 8);
191 sleep(HZ/10); /* let audio thread run */
192 audio_resume();
194 if (updatewps)
195 update_non_static();
197 while (fp_volume < fp_global_vol - fp_step) {
198 fp_volume += fp_step;
199 sound_set_volume(fp_volume >> 8);
200 sleep(1);
202 sound_set_volume(global_settings.volume);
204 else {
205 /* fade out */
206 int fp_volume = fp_global_vol;
208 if (updatewps)
209 update_non_static();
211 while (fp_volume > fp_min_vol + fp_step) {
212 fp_volume -= fp_step;
213 sound_set_volume(fp_volume >> 8);
214 sleep(1);
216 audio_pause();
218 skin_get_global_state()->is_fading = false;
219 #if CONFIG_CODEC != SWCODEC
220 #ifndef SIMULATOR
221 /* let audio thread run and wait for the mas to run out of data */
222 while (!mp3_pause_done())
223 #endif
224 sleep(HZ/10);
225 #endif
227 /* reset volume to what it was before the fade */
228 sound_set_volume(global_settings.volume);
231 #endif /* SWCODEC */
233 static bool update_onvol_change(enum screen_type screen)
235 skin_update(WPS, screen, SKIN_REFRESH_NON_STATIC);
237 #ifdef HAVE_LCD_CHARCELLS
238 splashf(0, "Vol: %3d dB",
239 sound_val2phys(SOUND_VOLUME, global_settings.volume));
240 return true;
241 #endif
242 return false;
246 #ifdef HAVE_TOUCHSCREEN
247 static int skintouch_to_wps(struct wps_data *data)
249 int offset = 0;
250 struct touchregion *region;
251 int button = skin_get_touchaction(data, &offset, &region);
252 switch (button)
254 case ACTION_STD_PREV:
255 return ACTION_WPS_SKIPPREV;
256 case ACTION_STD_PREVREPEAT:
257 return ACTION_WPS_SEEKBACK;
258 case ACTION_STD_NEXT:
259 return ACTION_WPS_SKIPNEXT;
260 case ACTION_STD_NEXTREPEAT:
261 return ACTION_WPS_SEEKFWD;
262 case ACTION_STD_MENU:
263 return ACTION_WPS_MENU;
264 case ACTION_STD_CONTEXT:
265 return ACTION_WPS_CONTEXT;
266 case ACTION_STD_QUICKSCREEN:
267 return ACTION_WPS_QUICKSCREEN;
268 #ifdef HAVE_HOTKEY
269 case ACTION_STD_HOTKEY:
270 return ACTION_WPS_HOTKEY;
271 #endif
272 case ACTION_TOUCH_SCROLLBAR:
273 skin_get_global_state()->id3->elapsed = skin_get_global_state()->id3->length*offset/100;
274 #if (CONFIG_CODEC == SWCODEC)
275 audio_pre_ff_rewind();
276 #else
277 if (!skin_get_global_state()->paused)
278 audio_pause();
279 #endif
280 audio_ff_rewind(skin_get_global_state()->id3->elapsed);
281 #if (CONFIG_CODEC != SWCODEC)
282 if (!skin_get_global_state()->paused)
283 audio_resume();
284 #endif
285 return ACTION_TOUCHSCREEN;
286 case ACTION_TOUCH_VOLUME:
288 const int min_vol = sound_min(SOUND_VOLUME);
289 const int max_vol = sound_max(SOUND_VOLUME);
290 global_settings.volume = (offset * (max_vol - min_vol)) / 100;
291 global_settings.volume += min_vol;
292 setvol();
294 return ACTION_TOUCHSCREEN;
296 return button;
298 #endif /* HAVE_TOUCHSCREEN */
300 bool ffwd_rew(int button)
302 unsigned int step = 0; /* current ff/rewind step */
303 unsigned int max_step = 0; /* maximum ff/rewind step */
304 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
305 int direction = -1; /* forward=1 or backward=-1 */
306 bool exit = false;
307 bool usb = false;
308 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
310 if (button == ACTION_NONE)
312 status_set_ffmode(0);
313 return usb;
315 while (!exit)
317 switch ( button )
319 case ACTION_WPS_SEEKFWD:
320 direction = 1;
321 case ACTION_WPS_SEEKBACK:
322 if (skin_get_global_state()->ff_rewind)
324 if (direction == 1)
326 /* fast forwarding, calc max step relative to end */
327 max_step = (skin_get_global_state()->id3->length -
328 (skin_get_global_state()->id3->elapsed +
329 ff_rewind_count)) *
330 FF_REWIND_MAX_PERCENT / 100;
332 else
334 /* rewinding, calc max step relative to start */
335 max_step = (skin_get_global_state()->id3->elapsed + ff_rewind_count) *
336 FF_REWIND_MAX_PERCENT / 100;
339 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
341 if (step > max_step)
342 step = max_step;
344 ff_rewind_count += step * direction;
346 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
347 step += step >> ff_rw_accel;
349 else
351 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
352 skin_get_global_state()->id3 && skin_get_global_state()->id3->length )
354 #if (CONFIG_CODEC == SWCODEC)
355 audio_pre_ff_rewind();
356 #else
357 if (!skin_get_global_state()->paused)
358 audio_pause();
359 #endif
360 #if CONFIG_KEYPAD == PLAYER_PAD
361 FOR_NB_SCREENS(i)
362 skin_get_gwps(WPS, i)->display->stop_scroll();
363 #endif
364 if (direction > 0)
365 status_set_ffmode(STATUS_FASTFORWARD);
366 else
367 status_set_ffmode(STATUS_FASTBACKWARD);
369 skin_get_global_state()->ff_rewind = true;
371 step = 1000 * global_settings.ff_rewind_min_step;
373 else
374 break;
377 if (direction > 0) {
378 if ((skin_get_global_state()->id3->elapsed + ff_rewind_count) >
379 skin_get_global_state()->id3->length)
380 ff_rewind_count = skin_get_global_state()->id3->length -
381 skin_get_global_state()->id3->elapsed;
383 else {
384 if ((int)(skin_get_global_state()->id3->elapsed + ff_rewind_count) < 0)
385 ff_rewind_count = -skin_get_global_state()->id3->elapsed;
388 /* set the wps state ff_rewind_count so the progess info
389 displays corectly */
390 skin_get_global_state()->ff_rewind_count = ff_rewind_count;
392 FOR_NB_SCREENS(i)
394 skin_update(WPS, i,
395 SKIN_REFRESH_PLAYER_PROGRESS |
396 SKIN_REFRESH_DYNAMIC);
399 break;
401 case ACTION_WPS_STOPSEEK:
402 skin_get_global_state()->id3->elapsed = skin_get_global_state()->id3->elapsed+ff_rewind_count;
403 audio_ff_rewind(skin_get_global_state()->id3->elapsed);
404 skin_get_global_state()->ff_rewind_count = 0;
405 skin_get_global_state()->ff_rewind = false;
406 status_set_ffmode(0);
407 #if (CONFIG_CODEC != SWCODEC)
408 if (!skin_get_global_state()->paused)
409 audio_resume();
410 #endif
411 #ifdef HAVE_LCD_CHARCELLS
412 FOR_NB_SCREENS(i)
413 skin_update(WPS, i, SKIN_REFRESH_ALL);
414 #endif
415 exit = true;
416 break;
418 default:
419 if(default_event_handler(button) == SYS_USB_CONNECTED) {
420 status_set_ffmode(0);
421 usb = true;
422 exit = true;
424 break;
426 if (!exit)
428 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
429 #ifdef HAVE_TOUCHSCREEN
430 if (button == ACTION_TOUCHSCREEN)
431 button = skintouch_to_wps(skin_get_gwps(WPS, SCREEN_MAIN)->data);
432 #endif
433 if (button != ACTION_WPS_SEEKFWD &&
434 button != ACTION_WPS_SEEKBACK)
435 button = ACTION_WPS_STOPSEEK;
438 return usb;
441 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
442 static void gwps_caption_backlight(struct wps_state *state)
444 if (state && state->id3)
446 #ifdef HAVE_BACKLIGHT
447 if (global_settings.caption_backlight)
449 /* turn on backlight n seconds before track ends, and turn it off n
450 seconds into the new track. n == backlight_timeout, or 5s */
451 int n = global_settings.backlight_timeout * 1000;
453 if ( n < 1000 )
454 n = 5000; /* use 5s if backlight is always on or off */
456 if (((state->id3->elapsed < 1000) ||
457 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
458 (state->paused == false))
459 backlight_on();
461 #endif
462 #ifdef HAVE_REMOTE_LCD
463 if (global_settings.remote_caption_backlight)
465 /* turn on remote backlight n seconds before track ends, and turn it
466 off n seconds into the new track. n == remote_backlight_timeout,
467 or 5s */
468 int n = global_settings.remote_backlight_timeout * 1000;
470 if ( n < 1000 )
471 n = 5000; /* use 5s if backlight is always on or off */
473 if (((state->id3->elapsed < 1000) ||
474 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
475 (state->paused == false))
476 remote_backlight_on();
478 #endif
481 #endif
484 static void change_dir(int direction)
486 if (global_settings.prevent_skip)
487 return;
489 if (direction < 0)
490 audio_prev_dir();
491 else if (direction > 0)
492 audio_next_dir();
493 /* prevent the next dir to immediatly start being ffw'd */
494 action_wait_for_release();
497 static void prev_track(unsigned long skip_thresh)
499 struct wps_state *state = skin_get_global_state();
500 if (state->id3->elapsed < skip_thresh)
502 audio_prev();
503 return;
505 else
507 if (state->id3->cuesheet)
509 curr_cuesheet_skip(state->id3->cuesheet, -1, state->id3->elapsed);
510 return;
513 #if (CONFIG_CODEC == SWCODEC)
514 audio_pre_ff_rewind();
515 #else
516 if (!state->paused)
517 audio_pause();
518 #endif
520 audio_ff_rewind(0);
522 #if (CONFIG_CODEC != SWCODEC)
523 if (!state->paused)
524 audio_resume();
525 #endif
529 static void next_track(void)
531 struct wps_state *state = skin_get_global_state();
532 /* take care of if we're playing a cuesheet */
533 if (state->id3->cuesheet)
535 if (curr_cuesheet_skip(state->id3->cuesheet, 1, state->id3->elapsed))
537 /* if the result was false, then we really want
538 to skip to the next track */
539 return;
543 audio_next();
546 static void play_hop(int direction)
548 struct wps_state *state = skin_get_global_state();
549 struct cuesheet *cue = state->id3->cuesheet;
550 long step = global_settings.skip_length*1000;
551 long elapsed = state->id3->elapsed;
552 long remaining = state->id3->length - elapsed;
554 /* if cuesheet is active, then we want the current tracks end instead of
555 * the total end */
556 if (cue && (cue->curr_track_idx+1 < cue->track_count))
558 int next = cue->curr_track_idx+1;
559 struct cue_track_info *t = &cue->tracks[next];
560 remaining = t->offset - elapsed;
563 if (step < 0)
565 if (direction < 0)
567 prev_track(DEFAULT_SKIP_TRESH);
568 return;
570 else if (remaining < DEFAULT_SKIP_TRESH*2)
572 next_track();
573 return;
575 else
576 elapsed += (remaining - DEFAULT_SKIP_TRESH*2);
578 else if (!global_settings.prevent_skip &&
579 (!step ||
580 (direction > 0 && step >= remaining) ||
581 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
582 { /* Do normal track skipping */
583 if (direction > 0)
584 next_track();
585 else if (direction < 0)
586 prev_track(DEFAULT_SKIP_TRESH);
587 return;
589 else if (direction == 1 && step >= remaining)
591 #if CONFIG_CODEC == SWCODEC
592 system_sound_play(SOUND_TRACK_NO_MORE);
593 #endif
594 return;
596 else if ((direction == -1 && elapsed < step))
598 elapsed = 0;
600 else
602 elapsed += step * direction;
604 if(audio_status() & AUDIO_STATUS_PLAY)
606 #if (CONFIG_CODEC == SWCODEC)
607 audio_pre_ff_rewind();
608 #else
609 if (!state->paused)
610 audio_pause();
611 #endif
614 #if (CONFIG_CODEC == SWCODEC)
615 audio_ff_rewind(elapsed);
616 #else
617 audio_ff_rewind(state->id3->elapsed = elapsed);
618 if (!state->paused)
619 audio_resume();
620 #endif
624 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
626 * If the user is unable to see the wps, because the display is deactivated,
627 * we suppress updates until the wps is activated again (the lcd driver will
628 * call this hook to issue an instant update)
629 * */
630 static void wps_lcd_activation_hook(void *param)
632 (void)param;
633 skin_request_full_update(WPS);
634 /* force timeout in wps main loop, so that the update is instantly */
635 queue_post(&button_queue, BUTTON_NONE, 0);
637 #endif
639 static void gwps_leave_wps(void)
641 FOR_NB_SCREENS(i)
643 skin_get_gwps(WPS, i)->display->stop_scroll();
644 #ifdef HAVE_BACKDROP_IMAGE
645 skin_backdrop_show(sb_get_backdrop(i));
646 #endif
647 viewportmanager_theme_undo(i, skin_has_sbs(i, skin_get_gwps(WPS, i)->data));
651 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
652 /* Play safe and unregister the hook */
653 remove_event(LCD_EVENT_ACTIVATION, wps_lcd_activation_hook);
654 #endif
655 /* unhandle statusbar update delay */
656 sb_skin_set_update_delay(DEFAULT_UPDATE_DELAY);
657 #ifdef HAVE_TOUCHSCREEN
658 touchscreen_set_mode(global_settings.touch_mode);
659 #endif
663 * display the wps on entering or restoring */
664 static void gwps_enter_wps(void)
666 struct gui_wps *gwps;
667 struct screen *display;
668 FOR_NB_SCREENS(i)
670 gwps = skin_get_gwps(WPS, i);
671 display = gwps->display;
672 display->stop_scroll();
673 viewportmanager_theme_enable(i, skin_has_sbs(i, skin_get_gwps(WPS, i)->data), NULL);
675 /* Update the values in the first (default) viewport - in case the user
676 has modified the statusbar or colour settings */
677 #if LCD_DEPTH > 1
678 if (display->depth > 1)
680 struct skin_viewport *svp = skin_find_item(VP_DEFAULT_LABEL_STRING,
681 SKIN_FIND_VP, gwps->data);
682 if (svp)
684 struct viewport *vp = &svp->vp;
685 vp->fg_pattern = display->get_foreground();
686 vp->bg_pattern = display->get_background();
689 #endif
690 /* make the backdrop actually take effect */
691 #ifdef HAVE_BACKDROP_IMAGE
692 skin_backdrop_show(gwps->data->backdrop_id);
693 #endif
694 display->clear_display();
695 skin_update(WPS, i, SKIN_REFRESH_ALL);
698 #ifdef HAVE_TOUCHSCREEN
699 gwps = skin_get_gwps(WPS, SCREEN_MAIN);
700 skin_disarm_touchregions(gwps->data);
701 if (gwps->data->touchregions < 0)
702 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
703 #endif
704 /* force statusbar/skin update since we just cleared the whole screen */
705 send_event(GUI_EVENT_ACTIONUPDATE, (void*)1);
708 void wps_do_playpause(bool updatewps)
710 struct wps_state *state = skin_get_global_state();
711 if ( state->paused )
713 state->paused = false;
714 unpause_action(true, updatewps);
716 else
718 state->paused = true;
719 pause_action(true, updatewps);
720 settings_save();
721 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
722 call_storage_idle_notifys(true); /* make sure resume info is saved */
723 #endif
728 /* The WPS can be left in two ways:
729 * a) call a function, which draws over the wps. In this case, the wps
730 * will be still active (i.e. the below function didn't return)
731 * b) return with a value evaluated by root_menu.c, in this case the wps
732 * is really left, and root_menu will handle the next screen
734 * In either way, call gwps_leave_wps(), in order to restore the correct
735 * "main screen" backdrops and statusbars
737 long gui_wps_show(void)
739 long button = 0;
740 bool restore = true;
741 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
742 bool exit = false;
743 bool bookmark = false;
744 bool update = false;
745 bool vol_changed = false;
746 long last_left = 0, last_right = 0;
747 struct wps_state *state = skin_get_global_state();
749 #ifdef HAVE_LCD_CHARCELLS
750 status_set_audio(true);
751 status_set_param(false);
752 #endif
754 #ifdef AB_REPEAT_ENABLE
755 ab_repeat_init();
756 ab_reset_markers();
757 #endif
758 wps_state_init();
760 while ( 1 )
762 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
764 /* did someone else (i.e power thread) change audio pause mode? */
765 if (state->paused != audio_paused) {
766 state->paused = audio_paused;
768 /* if another thread paused audio, we are probably in car mode,
769 about to shut down. lets save the settings. */
770 if (state->paused) {
771 settings_save();
772 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
773 call_storage_idle_notifys(true);
774 #endif
777 button = skin_wait_for_action(WPS, CONTEXT_WPS|ALLOW_SOFTLOCK,
778 restore ? 1 : HZ/5);
780 /* Exit if audio has stopped playing. This happens e.g. at end of
781 playlist or if using the sleep timer. */
782 if (!(audio_status() & AUDIO_STATUS_PLAY))
783 exit = true;
784 #ifdef HAVE_TOUCHSCREEN
785 if (button == ACTION_TOUCHSCREEN)
786 button = skintouch_to_wps(skin_get_gwps(WPS, SCREEN_MAIN)->data);
787 #endif
788 /* The iPods/X5/M5 use a single button for the A-B mode markers,
789 defined as ACTION_WPSAB_SINGLE in their config files. */
790 #ifdef ACTION_WPSAB_SINGLE
791 if (!global_settings.party_mode && ab_repeat_mode_enabled())
793 static int wps_ab_state = 0;
794 if (button == ACTION_WPSAB_SINGLE)
796 switch (wps_ab_state)
798 case 0: /* set the A spot */
799 button = ACTION_WPS_ABSETA_PREVDIR;
800 break;
801 case 1: /* set the B spot */
802 button = ACTION_WPS_ABSETB_NEXTDIR;
803 break;
804 case 2:
805 button = ACTION_WPS_ABRESET;
806 break;
808 wps_ab_state = (wps_ab_state+1) % 3;
811 #endif
812 switch(button)
814 #ifdef HAVE_HOTKEY
815 case ACTION_WPS_HOTKEY:
816 if (!global_settings.hotkey_wps)
817 break;
818 /* fall through */
819 #endif
820 case ACTION_WPS_CONTEXT:
822 bool hotkey = button == ACTION_WPS_HOTKEY;
823 gwps_leave_wps();
824 int retval = onplay(state->id3->path,
825 FILE_ATTR_AUDIO, CONTEXT_WPS, hotkey);
826 /* if music is stopped in the context menu we want to exit the wps */
827 if (retval == ONPLAY_MAINMENU
828 || !audio_status())
829 return GO_TO_ROOT;
830 else if (retval == ONPLAY_PLAYLIST)
831 return GO_TO_PLAYLIST_VIEWER;
832 #ifdef HAVE_PICTUREFLOW_INTEGRATION
833 else if (retval == ONPLAY_PICTUREFLOW)
834 return GO_TO_PICTUREFLOW;
835 #endif
836 restore = true;
838 break;
840 case ACTION_WPS_BROWSE:
841 #ifdef HAVE_LCD_CHARCELLS
842 status_set_record(false);
843 status_set_audio(false);
844 #endif
845 gwps_leave_wps();
846 return GO_TO_PREVIOUS_BROWSER;
847 break;
849 /* play/pause */
850 case ACTION_WPS_PLAY:
851 if (global_settings.party_mode)
852 break;
853 wps_do_playpause(true);
854 break;
856 case ACTION_WPS_VOLUP:
857 global_settings.volume++;
858 vol_changed = true;
859 break;
860 case ACTION_WPS_VOLDOWN:
861 global_settings.volume--;
862 vol_changed = true;
863 break;
864 /* fast forward
865 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
866 case ACTION_WPS_SEEKFWD:
867 if (global_settings.party_mode)
868 break;
869 if (current_tick -last_right < HZ)
871 if (state->id3->cuesheet)
873 audio_next();
875 else
877 change_dir(1);
880 else
881 ffwd_rew(ACTION_WPS_SEEKFWD);
882 last_right = last_left = 0;
883 break;
884 /* fast rewind
885 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
886 case ACTION_WPS_SEEKBACK:
887 if (global_settings.party_mode)
888 break;
889 if (current_tick -last_left < HZ)
891 if (state->id3->cuesheet)
893 #if (CONFIG_CODEC == SWCODEC)
894 audio_pre_ff_rewind();
895 #else
896 if (!state->paused)
897 audio_pause();
898 #endif
899 audio_ff_rewind(0);
901 else
903 change_dir(-1);
906 else
907 ffwd_rew(ACTION_WPS_SEEKBACK);
908 last_left = last_right = 0;
909 break;
911 /* prev / restart */
912 case ACTION_WPS_SKIPPREV:
913 if (global_settings.party_mode)
914 break;
915 last_left = current_tick;
916 #ifdef AB_REPEAT_ENABLE
917 /* if we're in A/B repeat mode and the current position
918 is past the A marker, jump back to the A marker... */
919 if ( ab_repeat_mode_enabled() )
921 if ( ab_after_A_marker(state->id3->elapsed) )
923 ab_jump_to_A_marker();
924 break;
927 else
928 /* ...otherwise, do it normally */
929 #endif
930 play_hop(-1);
931 break;
933 /* next
934 OR if skip length set, hop by predetermined amount. */
935 case ACTION_WPS_SKIPNEXT:
936 if (global_settings.party_mode)
937 break;
938 last_right = current_tick;
939 #ifdef AB_REPEAT_ENABLE
940 /* if we're in A/B repeat mode and the current position is
941 before the A marker, jump to the A marker... */
942 if ( ab_repeat_mode_enabled() )
944 if ( ab_before_A_marker(state->id3->elapsed) )
946 ab_jump_to_A_marker();
947 break;
950 else
951 /* ...otherwise, do it normally */
952 #endif
953 play_hop(1);
954 break;
955 /* next / prev directories */
956 /* and set A-B markers if in a-b mode */
957 case ACTION_WPS_ABSETB_NEXTDIR:
958 if (global_settings.party_mode)
959 break;
960 #if defined(AB_REPEAT_ENABLE)
961 if (ab_repeat_mode_enabled())
963 ab_set_B_marker(state->id3->elapsed);
964 ab_jump_to_A_marker();
966 else
967 #endif
969 change_dir(1);
971 break;
972 case ACTION_WPS_ABSETA_PREVDIR:
973 if (global_settings.party_mode)
974 break;
975 #if defined(AB_REPEAT_ENABLE)
976 if (ab_repeat_mode_enabled())
977 ab_set_A_marker(state->id3->elapsed);
978 else
979 #endif
981 change_dir(-1);
983 break;
984 /* menu key functions */
985 case ACTION_WPS_MENU:
986 gwps_leave_wps();
987 return GO_TO_ROOT;
988 break;
991 #ifdef HAVE_QUICKSCREEN
992 case ACTION_WPS_QUICKSCREEN:
994 gwps_leave_wps();
995 if (quick_screen_quick(button))
996 return GO_TO_ROOT;
997 restore = true;
999 break;
1000 #endif /* HAVE_QUICKSCREEN */
1002 /* screen settings */
1003 #ifdef BUTTON_F3
1004 case ACTION_F3:
1006 gwps_leave_wps();
1007 if (quick_screen_f3(BUTTON_F3))
1008 return GO_TO_ROOT;
1009 restore = true;
1011 break;
1012 #endif /* BUTTON_F3 */
1014 /* pitch screen */
1015 #ifdef HAVE_PITCHSCREEN
1016 case ACTION_WPS_PITCHSCREEN:
1018 gwps_leave_wps();
1019 if (1 == gui_syncpitchscreen_run())
1020 return GO_TO_ROOT;
1021 restore = true;
1023 break;
1024 #endif /* HAVE_PITCHSCREEN */
1026 #ifdef AB_REPEAT_ENABLE
1027 /* reset A&B markers */
1028 case ACTION_WPS_ABRESET:
1029 if (ab_repeat_mode_enabled())
1031 ab_reset_markers();
1032 update = true;
1034 break;
1035 #endif /* AB_REPEAT_ENABLE */
1037 /* stop and exit wps */
1038 case ACTION_WPS_STOP:
1039 if (global_settings.party_mode)
1040 break;
1041 bookmark = true;
1042 exit = true;
1043 break;
1045 case ACTION_WPS_ID3SCREEN:
1047 gwps_leave_wps();
1048 if (browse_id3())
1049 return GO_TO_ROOT;
1050 restore = true;
1052 break;
1053 /* this case is used by the softlock feature
1054 * it requests a full update here */
1055 case ACTION_REDRAW:
1056 skin_request_full_update(WPS);
1057 break;
1058 case ACTION_NONE: /* Timeout, do a partial update */
1059 update = true;
1060 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1061 break;
1062 #ifdef HAVE_RECORDING
1063 case ACTION_WPS_REC:
1064 exit = true;
1065 break;
1066 #endif
1067 case ACTION_WPS_VIEW_PLAYLIST:
1068 gwps_leave_wps();
1069 return GO_TO_PLAYLIST_VIEWER;
1070 break;
1071 default:
1072 switch(default_event_handler(button))
1073 { /* music has been stopped by the default handler */
1074 case SYS_USB_CONNECTED:
1075 case SYS_CALL_INCOMING:
1076 case BUTTON_MULTIMEDIA_STOP:
1077 gwps_leave_wps();
1078 return GO_TO_ROOT;
1080 update = true;
1081 break;
1084 if (vol_changed)
1086 bool res = false;
1087 vol_changed = false;
1088 setvol();
1089 FOR_NB_SCREENS(i)
1091 if(update_onvol_change(i))
1092 res = true;
1094 if (res) {
1095 restore = true;
1096 restoretimer = RESTORE_WPS_NEXT_SECOND;
1101 if (restore &&
1102 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1103 TIME_AFTER(current_tick, restoretimer)))
1105 restore = false;
1106 restoretimer = RESTORE_WPS_INSTANTLY;
1107 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1108 add_event(LCD_EVENT_ACTIVATION, false, wps_lcd_activation_hook);
1109 #endif
1110 /* we remove the update delay since it's not very usable in the wps,
1111 * e.g. during volume changing or ffwd/rewind */
1112 sb_skin_set_update_delay(0);
1113 skin_request_full_update(WPS);
1114 update = true;
1115 gwps_enter_wps();
1117 else
1119 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1120 gwps_caption_backlight(state);
1121 #endif
1122 FOR_NB_SCREENS(i)
1124 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1125 /* currently, all remotes are readable without backlight
1126 * so still update those */
1127 if (lcd_active() || (i != SCREEN_MAIN))
1128 #endif
1130 bool full_update = skin_do_full_update(WPS, i);
1131 if (update || full_update)
1133 skin_update(WPS, i, full_update ?
1134 SKIN_REFRESH_ALL : SKIN_REFRESH_NON_STATIC);
1138 update = false;
1141 if (exit) {
1142 #ifdef HAVE_LCD_CHARCELLS
1143 status_set_record(false);
1144 status_set_audio(false);
1145 #endif
1146 #if CONFIG_CODEC != SWCODEC
1147 if (global_settings.fade_on_stop)
1148 fade(false, true);
1149 #else
1150 audio_pause();
1151 update_non_static();
1152 #endif
1153 if (bookmark)
1154 bookmark_autobookmark(true);
1155 audio_stop();
1156 #ifdef AB_REPEAT_ENABLE
1157 ab_reset_markers();
1158 #endif
1159 gwps_leave_wps();
1160 #ifdef HAVE_RECORDING
1161 if (button == ACTION_WPS_REC)
1162 return GO_TO_RECSCREEN;
1163 #endif
1164 if (global_settings.browse_current)
1165 return GO_TO_PREVIOUS_BROWSER;
1166 return GO_TO_PREVIOUS;
1169 if (button && !IS_SYSEVENT(button) )
1170 storage_spin();
1172 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1175 /* this is called from the playback thread so NO DRAWING! */
1176 static void track_changed_callback(void *param)
1178 struct wps_state *state = skin_get_global_state();
1179 state->id3 = (struct mp3entry*)param;
1180 state->nid3 = audio_next_track();
1181 if (state->id3->cuesheet)
1183 cue_find_current_track(state->id3->cuesheet, state->id3->elapsed);
1185 skin_request_full_update(WPS);
1187 static void nextid3available_callback(void* param)
1189 (void)param;
1190 skin_get_global_state()->nid3 = audio_next_track();
1191 skin_request_full_update(WPS);
1194 #ifdef AUDIO_FAST_SKIP_PREVIEW
1195 /* this is called on the audio_skip caller thread */
1196 static void track_skip_callback(void *param)
1198 struct wps_state *state = skin_get_global_state();
1199 state->id3 = audio_current_track();
1200 state->nid3 = audio_next_track();
1201 skin_request_full_update(WPS);
1202 (void)param;
1204 #endif /* AUDIO_FAST_SKIP_PREVIEW */
1206 static void wps_state_init(void)
1208 struct wps_state *state = skin_get_global_state();
1209 state->ff_rewind = false;
1210 state->paused = false;
1211 if(audio_status() & AUDIO_STATUS_PLAY)
1213 state->id3 = audio_current_track();
1214 state->nid3 = audio_next_track();
1216 else
1218 state->id3 = NULL;
1219 state->nid3 = NULL;
1221 /* We'll be updating due to restore initialized with true */
1222 skin_request_full_update(WPS);
1223 /* add the WPS track event callbacks */
1224 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1225 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1226 #if CONFIG_CODEC == SWCODEC
1227 /* Use the same callback as ..._TRACK_CHANGE for when remaining handles have
1228 finished */
1229 add_event(PLAYBACK_EVENT_CUR_TRACK_READY, false, track_changed_callback);
1230 #endif
1231 #ifdef AUDIO_FAST_SKIP_PREVIEW
1232 add_event(PLAYBACK_EVENT_TRACK_SKIP, false, track_skip_callback);
1233 #endif
1237 #ifdef IPOD_ACCESSORY_PROTOCOL
1238 bool is_wps_fading(void)
1240 return skin_get_global_state()->is_fading;
1243 int wps_get_ff_rewind_count(void)
1245 return skin_get_global_state()->ff_rewind_count;
1247 #endif