2 new touch region options... "settings_inc" and "settings_dec" which will increase...
[maemo-rb.git] / apps / gui / wps.c
blob1489ff0026fbb5005ed6c5360230be6812c5c149
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 /* currently only one wps_state is needed, initialize to 0 */
78 struct wps_state wps_state = { .id3 = NULL };
79 static struct gui_wps gui_wps[NB_SCREENS] = {{ .data = NULL }};
80 static struct wps_data wps_datas[NB_SCREENS] = {{ .wps_loaded = 0 }};
81 static struct wps_sync_data wps_sync_data = { .do_full_update = false };
83 /* initial setup of wps_data */
84 static void wps_state_init(void);
85 static void track_changed_callback(void *param);
86 static void nextid3available_callback(void* param);
88 #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps"
89 #ifdef HAVE_REMOTE_LCD
90 #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps"
91 #define DEFAULT_WPS(screen) ((screen) == SCREEN_MAIN ? \
92 WPS_DEFAULTCFG:RWPS_DEFAULTCFG)
93 #else
94 #define DEFAULT_WPS(screen) (WPS_DEFAULTCFG)
95 #endif
97 void wps_data_load(enum screen_type screen, const char *buf, bool isfile)
99 bool loaded_ok;
101 #ifndef __PCTOOL__
103 * Hardcode loading WPS_DEFAULTCFG to cause a reset ideally this
104 * wants to be a virtual file. Feel free to modify dirbrowse()
105 * if you're feeling brave.
108 if (buf && ! strcmp(buf, DEFAULT_WPS(screen)) )
110 #ifdef HAVE_REMOTE_LCD
111 if (screen == SCREEN_REMOTE)
112 global_settings.rwps_file[0] = '\0';
113 else
114 #endif
115 global_settings.wps_file[0] = '\0';
116 buf = NULL;
119 #endif /* __PCTOOL__ */
121 loaded_ok = buf && skin_data_load(screen, gui_wps[screen].data, buf, isfile);
123 if (!loaded_ok) /* load the hardcoded default */
125 char *skin_buf[NB_SCREENS] = {
126 #ifdef HAVE_LCD_BITMAP
127 #if LCD_DEPTH > 1
128 "%X(d)\n"
129 #endif
130 "%s%?it<%?in<%in. |>%it|%fn>\n"
131 "%s%?ia<%ia|%?d(2)<%d(2)|%(root%)>>\n"
132 "%s%?id<%id|%?d(1)<%d(1)|%(root%)>> %?iy<%(%iy%)|>\n\n"
133 "%al%pc/%pt%ar[%pp:%pe]\n"
134 "%fbkBit %?fv<avg|> %?iv<%(id3v%iv%)|%(no id3%)>\n"
135 "%pb\n%pm\n",
136 #else
137 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d(2)> - %?id<%id|%d(1)>\n"
138 "%pc%?ps<*|/>%pt\n",
139 #endif
140 #ifdef HAVE_REMOTE_LCD
141 #if LCD_REMOTE_DEPTH > 1
142 "%X(d)\n"
143 #endif
144 "%s%?ia<%ia|%?d(2)<%d(2)|%(root%)>>\n"
145 "%s%?it<%?in<%in. |>%it|%fn>\n"
146 "%al%pc/%pt%ar[%pp:%pe]\n"
147 "%fbkBit %?fv<avg|> %?iv<%(id3v%iv%)|%(no id3%)>\n"
148 "%pb\n",
149 #endif
151 skin_data_load(screen, gui_wps[screen].data, skin_buf[screen], false);
155 void fade(bool fade_in, bool updatewps)
157 int fp_global_vol = global_settings.volume << 8;
158 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
159 int fp_step = (fp_global_vol - fp_min_vol) / 30;
160 int i;
161 wps_state.is_fading = !fade_in;
162 if (fade_in) {
163 /* fade in */
164 int fp_volume = fp_min_vol;
166 /* zero out the sound */
167 sound_set_volume(fp_min_vol >> 8);
169 sleep(HZ/10); /* let audio thread run */
170 audio_resume();
172 while (fp_volume < fp_global_vol - fp_step) {
173 fp_volume += fp_step;
174 sound_set_volume(fp_volume >> 8);
175 if (updatewps)
177 FOR_NB_SCREENS(i)
178 skin_update(&gui_wps[i], SKIN_REFRESH_NON_STATIC);
180 sleep(1);
182 sound_set_volume(global_settings.volume);
184 else {
185 /* fade out */
186 int fp_volume = fp_global_vol;
188 while (fp_volume > fp_min_vol + fp_step) {
189 fp_volume -= fp_step;
190 sound_set_volume(fp_volume >> 8);
191 if (updatewps)
193 FOR_NB_SCREENS(i)
194 skin_update(&gui_wps[i], SKIN_REFRESH_NON_STATIC);
196 sleep(1);
198 audio_pause();
199 wps_state.is_fading = false;
200 #if CONFIG_CODEC != SWCODEC
201 #ifndef SIMULATOR
202 /* let audio thread run and wait for the mas to run out of data */
203 while (!mp3_pause_done())
204 #endif
205 sleep(HZ/10);
206 #endif
208 /* reset volume to what it was before the fade */
209 sound_set_volume(global_settings.volume);
213 static bool update_onvol_change(struct gui_wps * gwps)
215 skin_update(gwps, SKIN_REFRESH_NON_STATIC);
217 #ifdef HAVE_LCD_CHARCELLS
218 splashf(0, "Vol: %3d dB",
219 sound_val2phys(SOUND_VOLUME, global_settings.volume));
220 return true;
221 #endif
222 return false;
226 #ifdef HAVE_TOUCHSCREEN
227 static int skintouch_to_wps(struct wps_data *data)
229 int offset = 0;
230 struct touchregion *region;
231 int button = skin_get_touchaction(data, &offset, &region);
232 switch (button)
234 case ACTION_STD_PREV:
235 return ACTION_WPS_SKIPPREV;
236 case ACTION_STD_PREVREPEAT:
237 return ACTION_WPS_SEEKBACK;
238 case ACTION_STD_NEXT:
239 return ACTION_WPS_SKIPNEXT;
240 case ACTION_STD_NEXTREPEAT:
241 return ACTION_WPS_SEEKFWD;
242 case ACTION_STD_MENU:
243 return ACTION_WPS_MENU;
244 case ACTION_STD_CONTEXT:
245 return ACTION_WPS_CONTEXT;
246 case ACTION_STD_QUICKSCREEN:
247 return ACTION_WPS_QUICKSCREEN;
248 #ifdef HAVE_HOTKEY
249 case ACTION_STD_HOTKEY:
250 return ACTION_WPS_HOTKEY;
251 #endif
252 case WPS_TOUCHREGION_SCROLLBAR:
253 wps_state.id3->elapsed = wps_state.id3->length*offset/100;
254 if (!wps_state.paused)
255 #if (CONFIG_CODEC == SWCODEC)
256 audio_pre_ff_rewind();
257 #else
258 audio_pause();
259 #endif
260 audio_ff_rewind(wps_state.id3->elapsed);
261 #if (CONFIG_CODEC != SWCODEC)
262 if (!wps_state.paused)
263 audio_resume();
264 #endif
265 return ACTION_TOUCHSCREEN;
266 case WPS_TOUCHREGION_VOLUME:
268 const int min_vol = sound_min(SOUND_VOLUME);
269 const int max_vol = sound_max(SOUND_VOLUME);
270 global_settings.volume = (offset * (max_vol - min_vol)) / 100;
271 global_settings.volume += min_vol;
272 setvol();
274 return ACTION_TOUCHSCREEN;
275 case ACTION_SETTINGS_INC:
276 case ACTION_SETTINGS_DEC:
278 const struct settings_list *setting = region->extradata;
279 option_select_next_val(setting, button == ACTION_SETTINGS_DEC, true);
281 return ACTION_REDRAW;
283 return button;
285 #endif /* HAVE_TOUCHSCREEN */
287 bool ffwd_rew(int button)
289 unsigned int step = 0; /* current ff/rewind step */
290 unsigned int max_step = 0; /* maximum ff/rewind step */
291 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
292 int direction = -1; /* forward=1 or backward=-1 */
293 bool exit = false;
294 bool usb = false;
295 int i = 0;
296 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
298 if (button == ACTION_NONE)
300 status_set_ffmode(0);
301 return usb;
303 while (!exit)
305 switch ( button )
307 case ACTION_WPS_SEEKFWD:
308 direction = 1;
309 case ACTION_WPS_SEEKBACK:
310 if (wps_state.ff_rewind)
312 if (direction == 1)
314 /* fast forwarding, calc max step relative to end */
315 max_step = (wps_state.id3->length -
316 (wps_state.id3->elapsed +
317 ff_rewind_count)) *
318 FF_REWIND_MAX_PERCENT / 100;
320 else
322 /* rewinding, calc max step relative to start */
323 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
324 FF_REWIND_MAX_PERCENT / 100;
327 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
329 if (step > max_step)
330 step = max_step;
332 ff_rewind_count += step * direction;
334 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
335 step += step >> ff_rw_accel;
337 else
339 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
340 wps_state.id3 && wps_state.id3->length )
342 if (!wps_state.paused)
343 #if (CONFIG_CODEC == SWCODEC)
344 audio_pre_ff_rewind();
345 #else
346 audio_pause();
347 #endif
348 #if CONFIG_KEYPAD == PLAYER_PAD
349 FOR_NB_SCREENS(i)
350 gui_wps[i].display->stop_scroll();
351 #endif
352 if (direction > 0)
353 status_set_ffmode(STATUS_FASTFORWARD);
354 else
355 status_set_ffmode(STATUS_FASTBACKWARD);
357 wps_state.ff_rewind = true;
359 step = 1000 * global_settings.ff_rewind_min_step;
361 else
362 break;
365 if (direction > 0) {
366 if ((wps_state.id3->elapsed + ff_rewind_count) >
367 wps_state.id3->length)
368 ff_rewind_count = wps_state.id3->length -
369 wps_state.id3->elapsed;
371 else {
372 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
373 ff_rewind_count = -wps_state.id3->elapsed;
376 /* set the wps state ff_rewind_count so the progess info
377 displays corectly */
378 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
379 ff_rewind_count:-ff_rewind_count;
380 FOR_NB_SCREENS(i)
382 skin_update(&gui_wps[i],
383 SKIN_REFRESH_PLAYER_PROGRESS |
384 SKIN_REFRESH_DYNAMIC);
387 break;
389 case ACTION_WPS_STOPSEEK:
390 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
391 audio_ff_rewind(wps_state.id3->elapsed);
392 wps_state.ff_rewind_count = 0;
393 wps_state.ff_rewind = false;
394 status_set_ffmode(0);
395 #if (CONFIG_CODEC != SWCODEC)
396 if (!wps_state.paused)
397 audio_resume();
398 #endif
399 #ifdef HAVE_LCD_CHARCELLS
400 FOR_NB_SCREENS(i)
401 skin_update(&gui_wps[i], SKIN_REFRESH_ALL);
402 #endif
403 exit = true;
404 break;
406 default:
407 if(default_event_handler(button) == SYS_USB_CONNECTED) {
408 status_set_ffmode(0);
409 usb = true;
410 exit = true;
412 break;
414 if (!exit)
416 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
417 #ifdef HAVE_TOUCHSCREEN
418 if (button == ACTION_TOUCHSCREEN)
419 button = skintouch_to_wps(gui_wps[SCREEN_MAIN].data);
420 if (button != ACTION_WPS_SEEKFWD &&
421 button != ACTION_WPS_SEEKBACK)
422 button = ACTION_WPS_STOPSEEK;
423 #endif
426 return usb;
430 void display_keylock_text(bool locked)
432 int i;
433 FOR_NB_SCREENS(i)
434 gui_wps[i].display->stop_scroll();
436 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
442 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
443 static void gwps_caption_backlight(struct wps_state *state)
445 if (state && state->id3)
447 #ifdef HAVE_BACKLIGHT
448 if (global_settings.caption_backlight)
450 /* turn on backlight n seconds before track ends, and turn it off n
451 seconds into the new track. n == backlight_timeout, or 5s */
452 int n = global_settings.backlight_timeout * 1000;
454 if ( n < 1000 )
455 n = 5000; /* use 5s if backlight is always on or off */
457 if (((state->id3->elapsed < 1000) ||
458 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
459 (state->paused == false))
460 backlight_on();
462 #endif
463 #ifdef HAVE_REMOTE_LCD
464 if (global_settings.remote_caption_backlight)
466 /* turn on remote backlight n seconds before track ends, and turn it
467 off n seconds into the new track. n == remote_backlight_timeout,
468 or 5s */
469 int n = global_settings.remote_backlight_timeout * 1000;
471 if ( n < 1000 )
472 n = 5000; /* use 5s if backlight is always on or off */
474 if (((state->id3->elapsed < 1000) ||
475 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
476 (state->paused == false))
477 remote_backlight_on();
479 #endif
482 #endif
485 static void change_dir(int direction)
487 if (global_settings.prevent_skip)
488 return;
490 if (direction < 0)
491 audio_prev_dir();
492 else if (direction > 0)
493 audio_next_dir();
494 /* prevent the next dir to immediatly start being ffw'd */
495 action_wait_for_release();
498 static void prev_track(unsigned long skip_thresh)
500 if (wps_state.id3->elapsed < skip_thresh)
502 audio_prev();
503 return;
505 else
507 if (wps_state.id3->cuesheet)
509 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
510 return;
513 if (!wps_state.paused)
514 #if (CONFIG_CODEC == SWCODEC)
515 audio_pre_ff_rewind();
516 #else
517 audio_pause();
518 #endif
520 audio_ff_rewind(0);
522 #if (CONFIG_CODEC != SWCODEC)
523 if (!wps_state.paused)
524 audio_resume();
525 #endif
529 static void next_track(void)
531 /* take care of if we're playing a cuesheet */
532 if (wps_state.id3->cuesheet)
534 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_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 long step = global_settings.skip_length*1000;
548 long elapsed = wps_state.id3->elapsed;
549 long remaining = wps_state.id3->length - elapsed;
551 if (step < 0)
553 if (direction < 0)
555 prev_track(DEFAULT_SKIP_TRESH);
556 return;
558 else if (remaining < DEFAULT_SKIP_TRESH*2)
560 next_track();
561 return;
563 else
564 elapsed += (remaining - DEFAULT_SKIP_TRESH*2);
566 else if (!global_settings.prevent_skip &&
567 (!step ||
568 (direction > 0 && step >= remaining) ||
569 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
570 { /* Do normal track skipping */
571 if (direction > 0)
572 next_track();
573 else if (direction < 0)
574 prev_track(DEFAULT_SKIP_TRESH);
575 return;
577 else if (direction == 1 && step >= remaining)
579 #if CONFIG_CODEC == SWCODEC
580 if(global_settings.beep)
581 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
582 #endif
583 return;
585 else if ((direction == -1 && elapsed < step))
587 elapsed = 0;
589 else
591 elapsed += step * direction;
593 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
595 #if (CONFIG_CODEC == SWCODEC)
596 audio_pre_ff_rewind();
597 #else
598 audio_pause();
599 #endif
601 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
602 #if (CONFIG_CODEC != SWCODEC)
603 if (!wps_state.paused)
604 audio_resume();
605 #endif
609 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
611 * If the user is unable to see the wps, because the display is deactivated,
612 * we suppress updates until the wps is activated again (the lcd driver will
613 * call this hook to issue an instant update)
614 * */
615 static void wps_lcd_activation_hook(void *param)
617 (void)param;
618 wps_sync_data.do_full_update = true;
619 /* force timeout in wps main loop, so that the update is instantly */
620 queue_post(&button_queue, BUTTON_NONE, 0);
622 #endif
624 static void gwps_leave_wps(void)
626 int i;
628 FOR_NB_SCREENS(i)
630 gui_wps[i].display->stop_scroll();
631 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
632 gui_wps[i].display->backdrop_show(sb_get_backdrop(i));
633 #endif
634 viewportmanager_theme_undo(i, skin_has_sbs(i, gui_wps[i].data));
638 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
639 /* Play safe and unregister the hook */
640 remove_event(LCD_EVENT_ACTIVATION, wps_lcd_activation_hook);
641 #endif
642 /* unhandle statusbar update delay */
643 sb_skin_set_update_delay(DEFAULT_UPDATE_DELAY);
644 #ifdef HAVE_TOUCHSCREEN
645 touchscreen_set_mode(global_settings.touch_mode);
646 #endif
650 * display the wps on entering or restoring */
651 static void gwps_enter_wps(void)
653 int i;
654 FOR_NB_SCREENS(i)
656 struct gui_wps *gwps = &gui_wps[i];
657 struct screen *display = gwps->display;
658 display->stop_scroll();
659 viewportmanager_theme_enable(i, skin_has_sbs(i, gui_wps[i].data), NULL);
661 /* Update the values in the first (default) viewport - in case the user
662 has modified the statusbar or colour settings */
663 #if LCD_DEPTH > 1
664 if (display->depth > 1)
666 struct skin_viewport *svp = find_viewport(VP_DEFAULT_LABEL,
667 false, gwps->data);
668 if (svp)
670 struct viewport *vp = &svp->vp;
671 vp->fg_pattern = display->get_foreground();
672 vp->bg_pattern = display->get_background();
675 #endif
676 /* make the backdrop actually take effect */
677 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
678 display->backdrop_show(gwps->data->backdrop);
679 #endif
680 display->clear_display();
681 skin_update(gwps, SKIN_REFRESH_ALL);
684 #ifdef HAVE_TOUCHSCREEN
685 skin_disarm_touchregions(gui_wps[SCREEN_MAIN].data);
686 if (!gui_wps[SCREEN_MAIN].data->touchregions)
687 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
688 #endif
689 /* force statusbar/skin update since we just cleared the whole screen */
690 send_event(GUI_EVENT_ACTIONUPDATE, (void*)1);
693 /* The WPS can be left in two ways:
694 * a) call a function, which draws over the wps. In this case, the wps
695 * will be still active (i.e. the below function didn't return)
696 * b) return with a value evaluated by root_menu.c, in this case the wps
697 * is really left, and root_menu will handle the next screen
699 * In either way, call gwps_leave_wps(), in order to restore the correct
700 * "main screen" backdrops and statusbars
702 long gui_wps_show(void)
704 long button = 0;
705 bool restore = true;
706 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
707 bool exit = false;
708 bool bookmark = false;
709 bool update = false;
710 bool vol_changed = false;
711 int i;
712 long last_left = 0, last_right = 0;
714 #ifdef HAVE_LCD_CHARCELLS
715 status_set_audio(true);
716 status_set_param(false);
717 #endif
719 #ifdef AB_REPEAT_ENABLE
720 ab_repeat_init();
721 ab_reset_markers();
722 #endif
723 wps_state_init();
725 while ( 1 )
727 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
729 /* did someone else (i.e power thread) change audio pause mode? */
730 if (wps_state.paused != audio_paused) {
731 wps_state.paused = audio_paused;
733 /* if another thread paused audio, we are probably in car mode,
734 about to shut down. lets save the settings. */
735 if (wps_state.paused) {
736 settings_save();
737 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
738 call_storage_idle_notifys(true);
739 #endif
742 button = skin_wait_for_action(gui_wps, CONTEXT_WPS|ALLOW_SOFTLOCK,
743 restore ? 1 : HZ/5);
745 /* Exit if audio has stopped playing. This happens e.g. at end of
746 playlist or if using the sleep timer. */
747 if (!(audio_status() & AUDIO_STATUS_PLAY))
748 exit = true;
749 #ifdef HAVE_TOUCHSCREEN
750 if (button == ACTION_TOUCHSCREEN)
751 button = skintouch_to_wps(gui_wps[SCREEN_MAIN].data);
752 #endif
753 /* The iPods/X5/M5 use a single button for the A-B mode markers,
754 defined as ACTION_WPSAB_SINGLE in their config files. */
755 #ifdef ACTION_WPSAB_SINGLE
756 if (!global_settings.party_mode && ab_repeat_mode_enabled())
758 static int wps_ab_state = 0;
759 if (button == ACTION_WPSAB_SINGLE)
761 switch (wps_ab_state)
763 case 0: /* set the A spot */
764 button = ACTION_WPS_ABSETA_PREVDIR;
765 break;
766 case 1: /* set the B spot */
767 button = ACTION_WPS_ABSETB_NEXTDIR;
768 break;
769 case 2:
770 button = ACTION_WPS_ABRESET;
771 break;
773 wps_ab_state = (wps_ab_state+1) % 3;
776 #endif
777 switch(button)
779 #ifdef HAVE_HOTKEY
780 case ACTION_WPS_HOTKEY:
781 if (!global_settings.hotkey_wps)
782 break;
783 /* fall through */
784 #endif
785 case ACTION_WPS_CONTEXT:
787 bool hotkey = button == ACTION_WPS_HOTKEY;
788 gwps_leave_wps();
789 int retval = onplay(wps_state.id3->path,
790 FILE_ATTR_AUDIO, CONTEXT_WPS, hotkey);
791 /* if music is stopped in the context menu we want to exit the wps */
792 if (retval == ONPLAY_MAINMENU
793 || !audio_status())
794 return GO_TO_ROOT;
795 else if (retval == ONPLAY_PLAYLIST)
796 return GO_TO_PLAYLIST_VIEWER;
797 #ifdef HAVE_PICTUREFLOW_INTEGRATION
798 else if (retval == ONPLAY_PICTUREFLOW)
799 return GO_TO_PICTUREFLOW;
800 #endif
801 restore = true;
803 break;
805 case ACTION_WPS_BROWSE:
806 #ifdef HAVE_LCD_CHARCELLS
807 status_set_record(false);
808 status_set_audio(false);
809 #endif
810 gwps_leave_wps();
811 return GO_TO_PREVIOUS_BROWSER;
812 break;
814 /* play/pause */
815 case ACTION_WPS_PLAY:
816 if (global_settings.party_mode)
817 break;
818 if ( wps_state.paused )
820 wps_state.paused = false;
821 if ( global_settings.fade_on_stop )
822 fade(true, true);
823 else
824 audio_resume();
826 else
828 wps_state.paused = true;
829 if ( global_settings.fade_on_stop )
830 fade(false, true);
831 else
832 audio_pause();
833 settings_save();
834 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
835 call_storage_idle_notifys(true); /* make sure resume info is saved */
836 #endif
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 (wps_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 (wps_state.id3->cuesheet)
877 if (!wps_state.paused)
878 #if (CONFIG_CODEC == SWCODEC)
879 audio_pre_ff_rewind();
880 #else
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(wps_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(wps_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(wps_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(wps_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 #ifdef HAVE_TOUCHSCREEN
1038 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1040 global_settings.playlist_shuffle =
1041 !global_settings.playlist_shuffle;
1042 #if CONFIG_CODEC == SWCODEC
1043 dsp_set_replaygain();
1044 #endif
1045 if (global_settings.playlist_shuffle)
1046 playlist_randomise(NULL, current_tick, true);
1047 else
1048 playlist_sort(NULL, true);
1050 break;
1051 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1053 const struct settings_list *rep_setting =
1054 find_setting(&global_settings.repeat_mode, NULL);
1055 option_select_next_val(rep_setting, false, true);
1056 audio_flush_and_reload_tracks();
1058 break;
1059 #endif /* HAVE_TOUCHSCREEN */
1060 /* this case is used by the softlock feature
1061 * it requests a full update here */
1062 case ACTION_REDRAW:
1063 wps_sync_data.do_full_update = true;
1064 break;
1065 case ACTION_NONE: /* Timeout, do a partial update */
1066 update = true;
1067 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1068 break;
1069 #ifdef HAVE_RECORDING
1070 case ACTION_WPS_REC:
1071 exit = true;
1072 break;
1073 #endif
1074 case SYS_POWEROFF:
1075 default_event_handler(SYS_POWEROFF);
1076 break;
1077 case ACTION_WPS_VIEW_PLAYLIST:
1078 gwps_leave_wps();
1079 return GO_TO_PLAYLIST_VIEWER;
1080 break;
1081 default:
1082 if(default_event_handler(button) == SYS_USB_CONNECTED)
1084 gwps_leave_wps();
1085 return GO_TO_ROOT;
1087 update = true;
1088 break;
1091 if (vol_changed)
1093 bool res = false;
1094 vol_changed = false;
1095 setvol();
1096 FOR_NB_SCREENS(i)
1098 if(update_onvol_change(&gui_wps[i]))
1099 res = true;
1101 if (res) {
1102 restore = true;
1103 restoretimer = RESTORE_WPS_NEXT_SECOND;
1108 if (restore &&
1109 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1110 TIME_AFTER(current_tick, restoretimer)))
1112 restore = false;
1113 restoretimer = RESTORE_WPS_INSTANTLY;
1114 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1115 add_event(LCD_EVENT_ACTIVATION, false, wps_lcd_activation_hook);
1116 #endif
1117 /* we remove the update delay since it's not very usable in the wps,
1118 * e.g. during volume changing or ffwd/rewind */
1119 sb_skin_set_update_delay(0);
1120 wps_sync_data.do_full_update = update = false;
1121 gwps_enter_wps();
1123 else if (wps_sync_data.do_full_update || update)
1125 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1126 gwps_caption_backlight(&wps_state);
1127 #endif
1128 bool full_update = wps_sync_data.do_full_update;
1129 FOR_NB_SCREENS(i)
1131 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1132 /* currently, all remotes are readable without backlight
1133 * so still update those */
1134 if (lcd_active() || (i != SCREEN_MAIN))
1135 #endif
1137 #if NB_SCREENS > 1
1138 if (i==SCREEN_MAIN && wps_sync_data.do_full_update)
1140 wps_sync_data.do_full_update = false;
1143 #else
1144 wps_sync_data.do_full_update = false;
1145 #endif
1146 skin_update(&gui_wps[i], full_update ?
1147 SKIN_REFRESH_ALL : SKIN_REFRESH_NON_STATIC);
1150 update = false;
1153 if (exit) {
1154 #ifdef HAVE_LCD_CHARCELLS
1155 status_set_record(false);
1156 status_set_audio(false);
1157 #endif
1158 if (global_settings.fade_on_stop)
1159 fade(false, true);
1161 if (bookmark)
1162 bookmark_autobookmark(true);
1163 audio_stop();
1164 #ifdef AB_REPEAT_ENABLE
1165 ab_reset_markers();
1166 #endif
1167 gwps_leave_wps();
1168 #ifdef HAVE_RECORDING
1169 if (button == ACTION_WPS_REC)
1170 return GO_TO_RECSCREEN;
1171 #endif
1172 if (global_settings.browse_current)
1173 return GO_TO_PREVIOUS_BROWSER;
1174 return GO_TO_PREVIOUS;
1177 if (button && !IS_SYSEVENT(button) )
1178 storage_spin();
1180 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1183 /* this is called from the playback thread so NO DRAWING! */
1184 static void track_changed_callback(void *param)
1186 wps_state.id3 = (struct mp3entry*)param;
1187 wps_state.nid3 = audio_next_track();
1188 if (wps_state.id3->cuesheet)
1190 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1192 wps_sync_data.do_full_update = true;
1194 static void nextid3available_callback(void* param)
1196 (void)param;
1197 wps_state.nid3 = audio_next_track();
1198 wps_sync_data.do_full_update = true;
1202 static void wps_state_init(void)
1204 wps_state.ff_rewind = false;
1205 wps_state.paused = false;
1206 if(audio_status() & AUDIO_STATUS_PLAY)
1208 wps_state.id3 = audio_current_track();
1209 wps_state.nid3 = audio_next_track();
1211 else
1213 wps_state.id3 = NULL;
1214 wps_state.nid3 = NULL;
1216 /* We'll be updating due to restore initialized with true */
1217 wps_sync_data.do_full_update = false;
1218 /* add the WPS track event callbacks */
1219 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1220 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1224 void gui_sync_wps_init(void)
1226 int i;
1227 FOR_NB_SCREENS(i)
1229 #ifdef HAVE_ALBUMART
1230 wps_datas[i].albumart = NULL;
1231 wps_datas[i].playback_aa_slot = -1;
1232 #endif
1233 gui_wps[i].data = &wps_datas[i];
1234 gui_wps[i].display = &screens[i];
1235 /* Currently no seperate wps_state needed/possible
1236 so use the only available ( "global" ) one */
1237 gui_wps[i].state = &wps_state;
1238 /* must point to the same struct for both screens */
1239 gui_wps[i].sync_data = &wps_sync_data;
1244 #ifdef IPOD_ACCESSORY_PROTOCOL
1245 bool is_wps_fading(void)
1247 return wps_state.is_fading;
1250 int wps_get_ff_rewind_count(void)
1252 return wps_state.ff_rewind_count;
1254 #endif