Few more fixes, notably make audiobufend static.
[kugel-rb.git] / apps / gui / wps.c
blobe686fcc533b90450b719a25109078e554efd4027
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 pause_action(bool may_fade, bool updatewps)
124 if (may_fade && global_settings.fade_on_stop)
125 fade(false, updatewps);
126 else
127 audio_pause();
129 if (global_settings.pause_rewind) {
130 long newpos;
132 #if (CONFIG_CODEC == SWCODEC)
133 audio_pre_ff_rewind();
134 #endif
135 newpos = audio_current_track()->elapsed
136 - global_settings.pause_rewind * 1000;
137 audio_ff_rewind(newpos > 0 ? newpos : 0);
141 void unpause_action(bool may_fade, bool updatewps)
143 if (may_fade && global_settings.fade_on_stop)
144 fade(true, updatewps);
145 else
146 audio_resume();
149 void fade(bool fade_in, bool updatewps)
151 int fp_global_vol = global_settings.volume << 8;
152 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
153 int fp_step = (fp_global_vol - fp_min_vol) / 10;
154 int i;
155 skin_get_global_state()->is_fading = !fade_in;
156 if (fade_in) {
157 /* fade in */
158 int fp_volume = fp_min_vol;
160 /* zero out the sound */
161 sound_set_volume(fp_min_vol >> 8);
163 sleep(HZ/10); /* let audio thread run */
164 audio_resume();
166 if (updatewps) {
167 FOR_NB_SCREENS(i)
168 skin_update(WPS, i, SKIN_REFRESH_NON_STATIC);
171 while (fp_volume < fp_global_vol - fp_step) {
172 fp_volume += fp_step;
173 sound_set_volume(fp_volume >> 8);
174 sleep(1);
176 sound_set_volume(global_settings.volume);
178 else {
179 /* fade out */
180 int fp_volume = fp_global_vol;
182 if (updatewps) {
183 FOR_NB_SCREENS(i)
184 skin_update(WPS, i, SKIN_REFRESH_NON_STATIC);
187 while (fp_volume > fp_min_vol + fp_step) {
188 fp_volume -= fp_step;
189 sound_set_volume(fp_volume >> 8);
190 sleep(1);
192 audio_pause();
194 skin_get_global_state()->is_fading = false;
195 #if CONFIG_CODEC != SWCODEC
196 #ifndef SIMULATOR
197 /* let audio thread run and wait for the mas to run out of data */
198 while (!mp3_pause_done())
199 #endif
200 sleep(HZ/10);
201 #endif
203 /* reset volume to what it was before the fade */
204 sound_set_volume(global_settings.volume);
208 static bool update_onvol_change(enum screen_type screen)
210 skin_update(WPS, screen, SKIN_REFRESH_NON_STATIC);
212 #ifdef HAVE_LCD_CHARCELLS
213 splashf(0, "Vol: %3d dB",
214 sound_val2phys(SOUND_VOLUME, global_settings.volume));
215 return true;
216 #endif
217 return false;
221 #ifdef HAVE_TOUCHSCREEN
222 static int skintouch_to_wps(struct wps_data *data)
224 int offset = 0;
225 struct touchregion *region;
226 int button = skin_get_touchaction(data, &offset, &region);
227 switch (button)
229 case ACTION_STD_PREV:
230 return ACTION_WPS_SKIPPREV;
231 case ACTION_STD_PREVREPEAT:
232 return ACTION_WPS_SEEKBACK;
233 case ACTION_STD_NEXT:
234 return ACTION_WPS_SKIPNEXT;
235 case ACTION_STD_NEXTREPEAT:
236 return ACTION_WPS_SEEKFWD;
237 case ACTION_STD_MENU:
238 return ACTION_WPS_MENU;
239 case ACTION_STD_CONTEXT:
240 return ACTION_WPS_CONTEXT;
241 case ACTION_STD_QUICKSCREEN:
242 return ACTION_WPS_QUICKSCREEN;
243 #ifdef HAVE_HOTKEY
244 case ACTION_STD_HOTKEY:
245 return ACTION_WPS_HOTKEY;
246 #endif
247 case ACTION_TOUCH_SCROLLBAR:
248 skin_get_global_state()->id3->elapsed = skin_get_global_state()->id3->length*offset/100;
249 #if (CONFIG_CODEC == SWCODEC)
250 audio_pre_ff_rewind();
251 #else
252 if (!skin_get_global_state()->paused)
253 audio_pause();
254 #endif
255 audio_ff_rewind(skin_get_global_state()->id3->elapsed);
256 #if (CONFIG_CODEC != SWCODEC)
257 if (!skin_get_global_state()->paused)
258 audio_resume();
259 #endif
260 return ACTION_TOUCHSCREEN;
261 case ACTION_TOUCH_VOLUME:
263 const int min_vol = sound_min(SOUND_VOLUME);
264 const int max_vol = sound_max(SOUND_VOLUME);
265 global_settings.volume = (offset * (max_vol - min_vol)) / 100;
266 global_settings.volume += min_vol;
267 setvol();
269 return ACTION_TOUCHSCREEN;
271 return button;
273 #endif /* HAVE_TOUCHSCREEN */
275 bool ffwd_rew(int button)
277 unsigned int step = 0; /* current ff/rewind step */
278 unsigned int max_step = 0; /* maximum ff/rewind step */
279 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
280 int direction = -1; /* forward=1 or backward=-1 */
281 bool exit = false;
282 bool usb = false;
283 int i = 0;
284 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
286 if (button == ACTION_NONE)
288 status_set_ffmode(0);
289 return usb;
291 while (!exit)
293 switch ( button )
295 case ACTION_WPS_SEEKFWD:
296 direction = 1;
297 case ACTION_WPS_SEEKBACK:
298 if (skin_get_global_state()->ff_rewind)
300 if (direction == 1)
302 /* fast forwarding, calc max step relative to end */
303 max_step = (skin_get_global_state()->id3->length -
304 (skin_get_global_state()->id3->elapsed +
305 ff_rewind_count)) *
306 FF_REWIND_MAX_PERCENT / 100;
308 else
310 /* rewinding, calc max step relative to start */
311 max_step = (skin_get_global_state()->id3->elapsed + ff_rewind_count) *
312 FF_REWIND_MAX_PERCENT / 100;
315 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
317 if (step > max_step)
318 step = max_step;
320 ff_rewind_count += step * direction;
322 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
323 step += step >> ff_rw_accel;
325 else
327 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
328 skin_get_global_state()->id3 && skin_get_global_state()->id3->length )
330 #if (CONFIG_CODEC == SWCODEC)
331 audio_pre_ff_rewind();
332 #else
333 if (!skin_get_global_state()->paused)
334 audio_pause();
335 #endif
336 #if CONFIG_KEYPAD == PLAYER_PAD
337 FOR_NB_SCREENS(i)
338 skin_get_gwps(WPS, i)->display->stop_scroll();
339 #endif
340 if (direction > 0)
341 status_set_ffmode(STATUS_FASTFORWARD);
342 else
343 status_set_ffmode(STATUS_FASTBACKWARD);
345 skin_get_global_state()->ff_rewind = true;
347 step = 1000 * global_settings.ff_rewind_min_step;
349 else
350 break;
353 if (direction > 0) {
354 if ((skin_get_global_state()->id3->elapsed + ff_rewind_count) >
355 skin_get_global_state()->id3->length)
356 ff_rewind_count = skin_get_global_state()->id3->length -
357 skin_get_global_state()->id3->elapsed;
359 else {
360 if ((int)(skin_get_global_state()->id3->elapsed + ff_rewind_count) < 0)
361 ff_rewind_count = -skin_get_global_state()->id3->elapsed;
364 /* set the wps state ff_rewind_count so the progess info
365 displays corectly */
366 skin_get_global_state()->ff_rewind_count = ff_rewind_count;
368 FOR_NB_SCREENS(i)
370 skin_update(WPS, i,
371 SKIN_REFRESH_PLAYER_PROGRESS |
372 SKIN_REFRESH_DYNAMIC);
375 break;
377 case ACTION_WPS_STOPSEEK:
378 skin_get_global_state()->id3->elapsed = skin_get_global_state()->id3->elapsed+ff_rewind_count;
379 audio_ff_rewind(skin_get_global_state()->id3->elapsed);
380 skin_get_global_state()->ff_rewind_count = 0;
381 skin_get_global_state()->ff_rewind = false;
382 status_set_ffmode(0);
383 #if (CONFIG_CODEC != SWCODEC)
384 if (!skin_get_global_state()->paused)
385 audio_resume();
386 #endif
387 #ifdef HAVE_LCD_CHARCELLS
388 FOR_NB_SCREENS(i)
389 skin_update(WPS, i, SKIN_REFRESH_ALL);
390 #endif
391 exit = true;
392 break;
394 default:
395 if(default_event_handler(button) == SYS_USB_CONNECTED) {
396 status_set_ffmode(0);
397 usb = true;
398 exit = true;
400 break;
402 if (!exit)
404 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
405 #ifdef HAVE_TOUCHSCREEN
406 if (button == ACTION_TOUCHSCREEN)
407 button = skintouch_to_wps(skin_get_gwps(WPS, SCREEN_MAIN)->data);
408 if (button != ACTION_WPS_SEEKFWD &&
409 button != ACTION_WPS_SEEKBACK)
410 button = ACTION_WPS_STOPSEEK;
411 #endif
414 return usb;
418 void display_keylock_text(bool locked)
420 int i;
421 FOR_NB_SCREENS(i)
422 skin_get_gwps(WPS, i)->display->stop_scroll();
424 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
430 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
431 static void gwps_caption_backlight(struct wps_state *state)
433 if (state && state->id3)
435 #ifdef HAVE_BACKLIGHT
436 if (global_settings.caption_backlight)
438 /* turn on backlight n seconds before track ends, and turn it off n
439 seconds into the new track. n == backlight_timeout, or 5s */
440 int n = global_settings.backlight_timeout * 1000;
442 if ( n < 1000 )
443 n = 5000; /* use 5s if backlight is always on or off */
445 if (((state->id3->elapsed < 1000) ||
446 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
447 (state->paused == false))
448 backlight_on();
450 #endif
451 #ifdef HAVE_REMOTE_LCD
452 if (global_settings.remote_caption_backlight)
454 /* turn on remote backlight n seconds before track ends, and turn it
455 off n seconds into the new track. n == remote_backlight_timeout,
456 or 5s */
457 int n = global_settings.remote_backlight_timeout * 1000;
459 if ( n < 1000 )
460 n = 5000; /* use 5s if backlight is always on or off */
462 if (((state->id3->elapsed < 1000) ||
463 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
464 (state->paused == false))
465 remote_backlight_on();
467 #endif
470 #endif
473 static void change_dir(int direction)
475 if (global_settings.prevent_skip)
476 return;
478 if (direction < 0)
479 audio_prev_dir();
480 else if (direction > 0)
481 audio_next_dir();
482 /* prevent the next dir to immediatly start being ffw'd */
483 action_wait_for_release();
486 static void prev_track(unsigned long skip_thresh)
488 struct wps_state *state = skin_get_global_state();
489 if (state->id3->elapsed < skip_thresh)
491 audio_prev();
492 return;
494 else
496 if (state->id3->cuesheet)
498 curr_cuesheet_skip(state->id3->cuesheet, -1, state->id3->elapsed);
499 return;
502 #if (CONFIG_CODEC == SWCODEC)
503 audio_pre_ff_rewind();
504 #else
505 if (!state->paused)
506 audio_pause();
507 #endif
509 audio_ff_rewind(0);
511 #if (CONFIG_CODEC != SWCODEC)
512 if (!state->paused)
513 audio_resume();
514 #endif
518 static void next_track(void)
520 struct wps_state *state = skin_get_global_state();
521 /* take care of if we're playing a cuesheet */
522 if (state->id3->cuesheet)
524 if (curr_cuesheet_skip(state->id3->cuesheet, 1, state->id3->elapsed))
526 /* if the result was false, then we really want
527 to skip to the next track */
528 return;
532 audio_next();
535 static void play_hop(int direction)
537 struct wps_state *state = skin_get_global_state();
538 long step = global_settings.skip_length*1000;
539 long elapsed = state->id3->elapsed;
540 long remaining = state->id3->length - elapsed;
542 if (step < 0)
544 if (direction < 0)
546 prev_track(DEFAULT_SKIP_TRESH);
547 return;
549 else if (remaining < DEFAULT_SKIP_TRESH*2)
551 next_track();
552 return;
554 else
555 elapsed += (remaining - DEFAULT_SKIP_TRESH*2);
557 else if (!global_settings.prevent_skip &&
558 (!step ||
559 (direction > 0 && step >= remaining) ||
560 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
561 { /* Do normal track skipping */
562 if (direction > 0)
563 next_track();
564 else if (direction < 0)
565 prev_track(DEFAULT_SKIP_TRESH);
566 return;
568 else if (direction == 1 && step >= remaining)
570 #if CONFIG_CODEC == SWCODEC
571 if(global_settings.beep)
572 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
573 #endif
574 return;
576 else if ((direction == -1 && elapsed < step))
578 elapsed = 0;
580 else
582 elapsed += step * direction;
584 if(audio_status() & AUDIO_STATUS_PLAY)
586 #if (CONFIG_CODEC == SWCODEC)
587 audio_pre_ff_rewind();
588 #else
589 if (!state->paused)
590 audio_pause();
591 #endif
594 #if (CONFIG_CODEC == SWCODEC)
595 audio_ff_rewind(elapsed);
596 #else
597 audio_ff_rewind(state->id3->elapsed = elapsed);
598 if (!state->paused)
599 audio_resume();
600 #endif
604 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
606 * If the user is unable to see the wps, because the display is deactivated,
607 * we suppress updates until the wps is activated again (the lcd driver will
608 * call this hook to issue an instant update)
609 * */
610 static void wps_lcd_activation_hook(void *param)
612 (void)param;
613 skin_request_full_update(WPS);
614 /* force timeout in wps main loop, so that the update is instantly */
615 queue_post(&button_queue, BUTTON_NONE, 0);
617 #endif
619 static void gwps_leave_wps(void)
621 int i;
623 FOR_NB_SCREENS(i)
625 skin_get_gwps(WPS, i)->display->stop_scroll();
626 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
627 skin_backdrop_show(sb_get_backdrop(i));
628 #endif
629 viewportmanager_theme_undo(i, skin_has_sbs(i, skin_get_gwps(WPS, i)->data));
633 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
634 /* Play safe and unregister the hook */
635 remove_event(LCD_EVENT_ACTIVATION, wps_lcd_activation_hook);
636 #endif
637 /* unhandle statusbar update delay */
638 sb_skin_set_update_delay(DEFAULT_UPDATE_DELAY);
639 #ifdef HAVE_TOUCHSCREEN
640 touchscreen_set_mode(global_settings.touch_mode);
641 #endif
645 * display the wps on entering or restoring */
646 static void gwps_enter_wps(void)
648 int i;
649 struct gui_wps *gwps;
650 struct screen *display;
651 FOR_NB_SCREENS(i)
653 gwps = skin_get_gwps(WPS, i);
654 display = gwps->display;
655 display->stop_scroll();
656 viewportmanager_theme_enable(i, skin_has_sbs(i, skin_get_gwps(WPS, i)->data), NULL);
658 /* Update the values in the first (default) viewport - in case the user
659 has modified the statusbar or colour settings */
660 #if LCD_DEPTH > 1
661 if (display->depth > 1)
663 struct skin_viewport *svp = skin_find_item(VP_DEFAULT_LABEL,
664 SKIN_FIND_VP, gwps->data);
665 if (svp)
667 struct viewport *vp = &svp->vp;
668 vp->fg_pattern = display->get_foreground();
669 vp->bg_pattern = display->get_background();
672 #endif
673 /* make the backdrop actually take effect */
674 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
675 skin_backdrop_show(gwps->data->backdrop_id);
676 #endif
677 display->clear_display();
678 skin_update(WPS, i, SKIN_REFRESH_ALL);
681 #ifdef HAVE_TOUCHSCREEN
682 gwps = skin_get_gwps(WPS, SCREEN_MAIN);
683 skin_disarm_touchregions(gwps->data);
684 if (!gwps->data->touchregions)
685 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
686 #endif
687 /* force statusbar/skin update since we just cleared the whole screen */
688 send_event(GUI_EVENT_ACTIONUPDATE, (void*)1);
691 void wps_do_playpause(bool updatewps)
693 struct wps_state *state = skin_get_global_state();
694 if ( state->paused )
696 state->paused = false;
697 unpause_action(true, updatewps);
699 else
701 state->paused = true;
702 pause_action(true, updatewps);
703 settings_save();
704 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
705 call_storage_idle_notifys(true); /* make sure resume info is saved */
706 #endif
711 /* The WPS can be left in two ways:
712 * a) call a function, which draws over the wps. In this case, the wps
713 * will be still active (i.e. the below function didn't return)
714 * b) return with a value evaluated by root_menu.c, in this case the wps
715 * is really left, and root_menu will handle the next screen
717 * In either way, call gwps_leave_wps(), in order to restore the correct
718 * "main screen" backdrops and statusbars
720 long gui_wps_show(void)
722 long button = 0;
723 bool restore = true;
724 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
725 bool exit = false;
726 bool bookmark = false;
727 bool update = false;
728 bool vol_changed = false;
729 int i;
730 long last_left = 0, last_right = 0;
731 struct wps_state *state = skin_get_global_state();
733 #ifdef HAVE_LCD_CHARCELLS
734 status_set_audio(true);
735 status_set_param(false);
736 #endif
738 #ifdef AB_REPEAT_ENABLE
739 ab_repeat_init();
740 ab_reset_markers();
741 #endif
742 wps_state_init();
744 while ( 1 )
746 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
748 /* did someone else (i.e power thread) change audio pause mode? */
749 if (state->paused != audio_paused) {
750 state->paused = audio_paused;
752 /* if another thread paused audio, we are probably in car mode,
753 about to shut down. lets save the settings. */
754 if (state->paused) {
755 settings_save();
756 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
757 call_storage_idle_notifys(true);
758 #endif
761 button = skin_wait_for_action(WPS, CONTEXT_WPS|ALLOW_SOFTLOCK,
762 restore ? 1 : HZ/5);
764 /* Exit if audio has stopped playing. This happens e.g. at end of
765 playlist or if using the sleep timer. */
766 if (!(audio_status() & AUDIO_STATUS_PLAY))
767 exit = true;
768 #ifdef HAVE_TOUCHSCREEN
769 if (button == ACTION_TOUCHSCREEN)
770 button = skintouch_to_wps(skin_get_gwps(WPS, SCREEN_MAIN)->data);
771 #endif
772 /* The iPods/X5/M5 use a single button for the A-B mode markers,
773 defined as ACTION_WPSAB_SINGLE in their config files. */
774 #ifdef ACTION_WPSAB_SINGLE
775 if (!global_settings.party_mode && ab_repeat_mode_enabled())
777 static int wps_ab_state = 0;
778 if (button == ACTION_WPSAB_SINGLE)
780 switch (wps_ab_state)
782 case 0: /* set the A spot */
783 button = ACTION_WPS_ABSETA_PREVDIR;
784 break;
785 case 1: /* set the B spot */
786 button = ACTION_WPS_ABSETB_NEXTDIR;
787 break;
788 case 2:
789 button = ACTION_WPS_ABRESET;
790 break;
792 wps_ab_state = (wps_ab_state+1) % 3;
795 #endif
796 switch(button)
798 #ifdef HAVE_HOTKEY
799 case ACTION_WPS_HOTKEY:
800 if (!global_settings.hotkey_wps)
801 break;
802 /* fall through */
803 #endif
804 case ACTION_WPS_CONTEXT:
806 bool hotkey = button == ACTION_WPS_HOTKEY;
807 gwps_leave_wps();
808 int retval = onplay(state->id3->path,
809 FILE_ATTR_AUDIO, CONTEXT_WPS, hotkey);
810 /* if music is stopped in the context menu we want to exit the wps */
811 if (retval == ONPLAY_MAINMENU
812 || !audio_status())
813 return GO_TO_ROOT;
814 else if (retval == ONPLAY_PLAYLIST)
815 return GO_TO_PLAYLIST_VIEWER;
816 #ifdef HAVE_PICTUREFLOW_INTEGRATION
817 else if (retval == ONPLAY_PICTUREFLOW)
818 return GO_TO_PICTUREFLOW;
819 #endif
820 restore = true;
822 break;
824 case ACTION_WPS_BROWSE:
825 #ifdef HAVE_LCD_CHARCELLS
826 status_set_record(false);
827 status_set_audio(false);
828 #endif
829 gwps_leave_wps();
830 return GO_TO_PREVIOUS_BROWSER;
831 break;
833 /* play/pause */
834 case ACTION_WPS_PLAY:
835 if (global_settings.party_mode)
836 break;
837 wps_do_playpause(true);
838 break;
840 case ACTION_WPS_VOLUP:
841 global_settings.volume++;
842 vol_changed = true;
843 break;
844 case ACTION_WPS_VOLDOWN:
845 global_settings.volume--;
846 vol_changed = true;
847 break;
848 /* fast forward
849 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
850 case ACTION_WPS_SEEKFWD:
851 if (global_settings.party_mode)
852 break;
853 if (current_tick -last_right < HZ)
855 if (state->id3->cuesheet)
857 audio_next();
859 else
861 change_dir(1);
864 else
865 ffwd_rew(ACTION_WPS_SEEKFWD);
866 last_right = last_left = 0;
867 break;
868 /* fast rewind
869 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
870 case ACTION_WPS_SEEKBACK:
871 if (global_settings.party_mode)
872 break;
873 if (current_tick -last_left < HZ)
875 if (state->id3->cuesheet)
877 #if (CONFIG_CODEC == SWCODEC)
878 audio_pre_ff_rewind();
879 #else
880 if (!state->paused)
881 audio_pause();
882 #endif
883 audio_ff_rewind(0);
885 else
887 change_dir(-1);
890 else
891 ffwd_rew(ACTION_WPS_SEEKBACK);
892 last_left = last_right = 0;
893 break;
895 /* prev / restart */
896 case ACTION_WPS_SKIPPREV:
897 if (global_settings.party_mode)
898 break;
899 last_left = current_tick;
900 #ifdef AB_REPEAT_ENABLE
901 /* if we're in A/B repeat mode and the current position
902 is past the A marker, jump back to the A marker... */
903 if ( ab_repeat_mode_enabled() )
905 if ( ab_after_A_marker(state->id3->elapsed) )
907 ab_jump_to_A_marker();
908 break;
911 else
912 /* ...otherwise, do it normally */
913 #endif
914 play_hop(-1);
915 break;
917 /* next
918 OR if skip length set, hop by predetermined amount. */
919 case ACTION_WPS_SKIPNEXT:
920 if (global_settings.party_mode)
921 break;
922 last_right = current_tick;
923 #ifdef AB_REPEAT_ENABLE
924 /* if we're in A/B repeat mode and the current position is
925 before the A marker, jump to the A marker... */
926 if ( ab_repeat_mode_enabled() )
928 if ( ab_before_A_marker(state->id3->elapsed) )
930 ab_jump_to_A_marker();
931 break;
934 else
935 /* ...otherwise, do it normally */
936 #endif
937 play_hop(1);
938 break;
939 /* next / prev directories */
940 /* and set A-B markers if in a-b mode */
941 case ACTION_WPS_ABSETB_NEXTDIR:
942 if (global_settings.party_mode)
943 break;
944 #if defined(AB_REPEAT_ENABLE)
945 if (ab_repeat_mode_enabled())
947 ab_set_B_marker(state->id3->elapsed);
948 ab_jump_to_A_marker();
950 else
951 #endif
953 change_dir(1);
955 break;
956 case ACTION_WPS_ABSETA_PREVDIR:
957 if (global_settings.party_mode)
958 break;
959 #if defined(AB_REPEAT_ENABLE)
960 if (ab_repeat_mode_enabled())
961 ab_set_A_marker(state->id3->elapsed);
962 else
963 #endif
965 change_dir(-1);
967 break;
968 /* menu key functions */
969 case ACTION_WPS_MENU:
970 gwps_leave_wps();
971 return GO_TO_ROOT;
972 break;
975 #ifdef HAVE_QUICKSCREEN
976 case ACTION_WPS_QUICKSCREEN:
978 gwps_leave_wps();
979 if (quick_screen_quick(button))
980 return GO_TO_ROOT;
981 restore = true;
983 break;
984 #endif /* HAVE_QUICKSCREEN */
986 /* screen settings */
987 #ifdef BUTTON_F3
988 case ACTION_F3:
990 gwps_leave_wps();
991 if (quick_screen_f3(BUTTON_F3))
992 return GO_TO_ROOT;
993 restore = true;
995 break;
996 #endif /* BUTTON_F3 */
998 /* pitch screen */
999 #ifdef HAVE_PITCHSCREEN
1000 case ACTION_WPS_PITCHSCREEN:
1002 gwps_leave_wps();
1003 if (1 == gui_syncpitchscreen_run())
1004 return GO_TO_ROOT;
1005 restore = true;
1007 break;
1008 #endif /* HAVE_PITCHSCREEN */
1010 #ifdef AB_REPEAT_ENABLE
1011 /* reset A&B markers */
1012 case ACTION_WPS_ABRESET:
1013 if (ab_repeat_mode_enabled())
1015 ab_reset_markers();
1016 update = true;
1018 break;
1019 #endif /* AB_REPEAT_ENABLE */
1021 /* stop and exit wps */
1022 case ACTION_WPS_STOP:
1023 if (global_settings.party_mode)
1024 break;
1025 bookmark = true;
1026 exit = true;
1027 break;
1029 case ACTION_WPS_ID3SCREEN:
1031 gwps_leave_wps();
1032 if (browse_id3())
1033 return GO_TO_ROOT;
1034 restore = true;
1036 break;
1037 /* this case is used by the softlock feature
1038 * it requests a full update here */
1039 case ACTION_REDRAW:
1040 skin_request_full_update(WPS);
1041 break;
1042 case ACTION_NONE: /* Timeout, do a partial update */
1043 update = true;
1044 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1045 break;
1046 #ifdef HAVE_RECORDING
1047 case ACTION_WPS_REC:
1048 exit = true;
1049 break;
1050 #endif
1051 case ACTION_WPS_VIEW_PLAYLIST:
1052 gwps_leave_wps();
1053 return GO_TO_PLAYLIST_VIEWER;
1054 break;
1055 default:
1056 switch(default_event_handler(button))
1057 { /* music has been stopped by the default handler */
1058 case SYS_USB_CONNECTED:
1059 case SYS_CALL_INCOMING:
1060 case BUTTON_MULTIMEDIA_STOP:
1061 gwps_leave_wps();
1062 return GO_TO_ROOT;
1064 update = true;
1065 break;
1068 if (vol_changed)
1070 bool res = false;
1071 vol_changed = false;
1072 setvol();
1073 FOR_NB_SCREENS(i)
1075 if(update_onvol_change(i))
1076 res = true;
1078 if (res) {
1079 restore = true;
1080 restoretimer = RESTORE_WPS_NEXT_SECOND;
1085 if (restore &&
1086 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1087 TIME_AFTER(current_tick, restoretimer)))
1089 restore = false;
1090 restoretimer = RESTORE_WPS_INSTANTLY;
1091 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1092 add_event(LCD_EVENT_ACTIVATION, false, wps_lcd_activation_hook);
1093 #endif
1094 /* we remove the update delay since it's not very usable in the wps,
1095 * e.g. during volume changing or ffwd/rewind */
1096 sb_skin_set_update_delay(0);
1097 skin_request_full_update(WPS);
1098 update = true;
1099 gwps_enter_wps();
1101 else
1103 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1104 gwps_caption_backlight(state);
1105 #endif
1106 FOR_NB_SCREENS(i)
1108 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1109 /* currently, all remotes are readable without backlight
1110 * so still update those */
1111 if (lcd_active() || (i != SCREEN_MAIN))
1112 #endif
1114 bool full_update = skin_do_full_update(WPS, i);
1115 if (update || full_update)
1117 skin_update(WPS, i, full_update ?
1118 SKIN_REFRESH_ALL : SKIN_REFRESH_NON_STATIC);
1122 update = false;
1125 if (exit) {
1126 #ifdef HAVE_LCD_CHARCELLS
1127 status_set_record(false);
1128 status_set_audio(false);
1129 #endif
1130 if (global_settings.fade_on_stop)
1131 fade(false, true);
1133 if (bookmark)
1134 bookmark_autobookmark(true);
1135 audio_stop();
1136 #ifdef AB_REPEAT_ENABLE
1137 ab_reset_markers();
1138 #endif
1139 gwps_leave_wps();
1140 #ifdef HAVE_RECORDING
1141 if (button == ACTION_WPS_REC)
1142 return GO_TO_RECSCREEN;
1143 #endif
1144 if (global_settings.browse_current)
1145 return GO_TO_PREVIOUS_BROWSER;
1146 return GO_TO_PREVIOUS;
1149 if (button && !IS_SYSEVENT(button) )
1150 storage_spin();
1152 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1155 /* this is called from the playback thread so NO DRAWING! */
1156 static void track_changed_callback(void *param)
1158 struct wps_state *state = skin_get_global_state();
1159 state->id3 = (struct mp3entry*)param;
1160 state->nid3 = audio_next_track();
1161 if (state->id3->cuesheet)
1163 cue_find_current_track(state->id3->cuesheet, state->id3->elapsed);
1165 skin_request_full_update(WPS);
1167 static void nextid3available_callback(void* param)
1169 (void)param;
1170 skin_get_global_state()->nid3 = audio_next_track();
1171 skin_request_full_update(WPS);
1174 #ifdef AUDIO_FAST_SKIP_PREVIEW
1175 /* this is called on the audio_skip caller thread */
1176 static void track_skip_callback(void *param)
1178 struct wps_state *state = skin_get_global_state();
1179 state->id3 = audio_current_track();
1180 state->nid3 = audio_next_track();
1181 skin_request_full_update(WPS);
1182 (void)param;
1184 #endif /* AUDIO_FAST_SKIP_PREVIEW */
1186 static void wps_state_init(void)
1188 struct wps_state *state = skin_get_global_state();
1189 state->ff_rewind = false;
1190 state->paused = false;
1191 if(audio_status() & AUDIO_STATUS_PLAY)
1193 state->id3 = audio_current_track();
1194 state->nid3 = audio_next_track();
1196 else
1198 state->id3 = NULL;
1199 state->nid3 = NULL;
1201 /* We'll be updating due to restore initialized with true */
1202 skin_request_full_update(WPS);
1203 /* add the WPS track event callbacks */
1204 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1205 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1206 #if CONFIG_CODEC == SWCODEC
1207 /* Use the same callback as ..._TRACK_CHANGE for when remaining handles have
1208 finished */
1209 add_event(PLAYBACK_EVENT_CUR_TRACK_READY, false, track_changed_callback);
1210 #endif
1211 #ifdef AUDIO_FAST_SKIP_PREVIEW
1212 add_event(PLAYBACK_EVENT_TRACK_SKIP, false, track_skip_callback);
1213 #endif
1217 #ifdef IPOD_ACCESSORY_PROTOCOL
1218 bool is_wps_fading(void)
1220 return skin_get_global_state()->is_fading;
1223 int wps_get_ff_rewind_count(void)
1225 return skin_get_global_state()->ff_rewind_count;
1227 #endif