Allow viewport labels to be mostly free text instead of only one character. If this...
[maemo-rb.git] / apps / gui / wps.c
blob7d583a82b47bad82f3726f19ad3884427d55db1d
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 int button = skin_get_touchaction(data, &offset);
231 switch (button)
233 case ACTION_STD_PREV:
234 return ACTION_WPS_SKIPPREV;
235 case ACTION_STD_PREVREPEAT:
236 return ACTION_WPS_SEEKBACK;
237 case ACTION_STD_NEXT:
238 return ACTION_WPS_SKIPNEXT;
239 case ACTION_STD_NEXTREPEAT:
240 return ACTION_WPS_SEEKFWD;
241 case ACTION_STD_MENU:
242 return ACTION_WPS_MENU;
243 case ACTION_STD_CONTEXT:
244 return ACTION_WPS_CONTEXT;
245 case ACTION_STD_QUICKSCREEN:
246 return ACTION_WPS_QUICKSCREEN;
247 #ifdef HAVE_HOTKEY
248 case ACTION_STD_HOTKEY:
249 return ACTION_WPS_HOTKEY;
250 #endif
251 case WPS_TOUCHREGION_SCROLLBAR:
252 wps_state.id3->elapsed = wps_state.id3->length*offset/100;
253 if (!wps_state.paused)
254 #if (CONFIG_CODEC == SWCODEC)
255 audio_pre_ff_rewind();
256 #else
257 audio_pause();
258 #endif
259 audio_ff_rewind(wps_state.id3->elapsed);
260 #if (CONFIG_CODEC != SWCODEC)
261 if (!wps_state.paused)
262 audio_resume();
263 #endif
264 return ACTION_TOUCHSCREEN;
265 case WPS_TOUCHREGION_VOLUME:
267 const int min_vol = sound_min(SOUND_VOLUME);
268 const int max_vol = sound_max(SOUND_VOLUME);
269 global_settings.volume = (offset * (max_vol - min_vol)) / 100;
270 global_settings.volume += min_vol;
271 setvol();
273 return ACTION_TOUCHSCREEN;
275 return button;
277 #endif /* HAVE_TOUCHSCREEN */
279 bool ffwd_rew(int button)
281 unsigned int step = 0; /* current ff/rewind step */
282 unsigned int max_step = 0; /* maximum ff/rewind step */
283 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
284 int direction = -1; /* forward=1 or backward=-1 */
285 bool exit = false;
286 bool usb = false;
287 int i = 0;
288 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
290 if (button == ACTION_NONE)
292 status_set_ffmode(0);
293 return usb;
295 while (!exit)
297 switch ( button )
299 case ACTION_WPS_SEEKFWD:
300 direction = 1;
301 case ACTION_WPS_SEEKBACK:
302 if (wps_state.ff_rewind)
304 if (direction == 1)
306 /* fast forwarding, calc max step relative to end */
307 max_step = (wps_state.id3->length -
308 (wps_state.id3->elapsed +
309 ff_rewind_count)) *
310 FF_REWIND_MAX_PERCENT / 100;
312 else
314 /* rewinding, calc max step relative to start */
315 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
316 FF_REWIND_MAX_PERCENT / 100;
319 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
321 if (step > max_step)
322 step = max_step;
324 ff_rewind_count += step * direction;
326 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
327 step += step >> ff_rw_accel;
329 else
331 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
332 wps_state.id3 && wps_state.id3->length )
334 if (!wps_state.paused)
335 #if (CONFIG_CODEC == SWCODEC)
336 audio_pre_ff_rewind();
337 #else
338 audio_pause();
339 #endif
340 #if CONFIG_KEYPAD == PLAYER_PAD
341 FOR_NB_SCREENS(i)
342 gui_wps[i].display->stop_scroll();
343 #endif
344 if (direction > 0)
345 status_set_ffmode(STATUS_FASTFORWARD);
346 else
347 status_set_ffmode(STATUS_FASTBACKWARD);
349 wps_state.ff_rewind = true;
351 step = 1000 * global_settings.ff_rewind_min_step;
353 else
354 break;
357 if (direction > 0) {
358 if ((wps_state.id3->elapsed + ff_rewind_count) >
359 wps_state.id3->length)
360 ff_rewind_count = wps_state.id3->length -
361 wps_state.id3->elapsed;
363 else {
364 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
365 ff_rewind_count = -wps_state.id3->elapsed;
368 /* set the wps state ff_rewind_count so the progess info
369 displays corectly */
370 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
371 ff_rewind_count:-ff_rewind_count;
372 FOR_NB_SCREENS(i)
374 skin_update(&gui_wps[i],
375 SKIN_REFRESH_PLAYER_PROGRESS |
376 SKIN_REFRESH_DYNAMIC);
379 break;
381 case ACTION_WPS_STOPSEEK:
382 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
383 audio_ff_rewind(wps_state.id3->elapsed);
384 wps_state.ff_rewind_count = 0;
385 wps_state.ff_rewind = false;
386 status_set_ffmode(0);
387 #if (CONFIG_CODEC != SWCODEC)
388 if (!wps_state.paused)
389 audio_resume();
390 #endif
391 #ifdef HAVE_LCD_CHARCELLS
392 FOR_NB_SCREENS(i)
393 skin_update(&gui_wps[i], SKIN_REFRESH_ALL);
394 #endif
395 exit = true;
396 break;
398 default:
399 if(default_event_handler(button) == SYS_USB_CONNECTED) {
400 status_set_ffmode(0);
401 usb = true;
402 exit = true;
404 break;
406 if (!exit)
408 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
409 #ifdef HAVE_TOUCHSCREEN
410 if (button == ACTION_TOUCHSCREEN)
411 button = skintouch_to_wps(gui_wps[SCREEN_MAIN].data);
412 if (button != ACTION_WPS_SEEKFWD &&
413 button != ACTION_WPS_SEEKBACK)
414 button = ACTION_WPS_STOPSEEK;
415 #endif
418 return usb;
422 void display_keylock_text(bool locked)
424 int i;
425 FOR_NB_SCREENS(i)
426 gui_wps[i].display->stop_scroll();
428 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
434 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
435 static void gwps_caption_backlight(struct wps_state *state)
437 if (state && state->id3)
439 #ifdef HAVE_BACKLIGHT
440 if (global_settings.caption_backlight)
442 /* turn on backlight n seconds before track ends, and turn it off n
443 seconds into the new track. n == backlight_timeout, or 5s */
444 int n = global_settings.backlight_timeout * 1000;
446 if ( n < 1000 )
447 n = 5000; /* use 5s if backlight is always on or off */
449 if (((state->id3->elapsed < 1000) ||
450 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
451 (state->paused == false))
452 backlight_on();
454 #endif
455 #ifdef HAVE_REMOTE_LCD
456 if (global_settings.remote_caption_backlight)
458 /* turn on remote backlight n seconds before track ends, and turn it
459 off n seconds into the new track. n == remote_backlight_timeout,
460 or 5s */
461 int n = global_settings.remote_backlight_timeout * 1000;
463 if ( n < 1000 )
464 n = 5000; /* use 5s if backlight is always on or off */
466 if (((state->id3->elapsed < 1000) ||
467 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
468 (state->paused == false))
469 remote_backlight_on();
471 #endif
474 #endif
477 static void change_dir(int direction)
479 if (global_settings.prevent_skip)
480 return;
482 if (direction < 0)
483 audio_prev_dir();
484 else if (direction > 0)
485 audio_next_dir();
486 /* prevent the next dir to immediatly start being ffw'd */
487 action_wait_for_release();
490 static void prev_track(unsigned long skip_thresh)
492 if (wps_state.id3->elapsed < skip_thresh)
494 audio_prev();
495 return;
497 else
499 if (wps_state.id3->cuesheet)
501 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
502 return;
505 if (!wps_state.paused)
506 #if (CONFIG_CODEC == SWCODEC)
507 audio_pre_ff_rewind();
508 #else
509 audio_pause();
510 #endif
512 audio_ff_rewind(0);
514 #if (CONFIG_CODEC != SWCODEC)
515 if (!wps_state.paused)
516 audio_resume();
517 #endif
521 static void next_track(void)
523 /* take care of if we're playing a cuesheet */
524 if (wps_state.id3->cuesheet)
526 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
528 /* if the result was false, then we really want
529 to skip to the next track */
530 return;
534 audio_next();
537 static void play_hop(int direction)
539 long step = global_settings.skip_length*1000;
540 long elapsed = wps_state.id3->elapsed;
541 long remaining = wps_state.id3->length - elapsed;
543 if (step < 0)
545 if (direction < 0)
547 prev_track(DEFAULT_SKIP_TRESH);
548 return;
550 else if (remaining < DEFAULT_SKIP_TRESH*2)
552 next_track();
553 return;
555 else
556 elapsed += (remaining - DEFAULT_SKIP_TRESH*2);
558 else if (!global_settings.prevent_skip &&
559 (!step ||
560 (direction > 0 && step >= remaining) ||
561 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
562 { /* Do normal track skipping */
563 if (direction > 0)
564 next_track();
565 else if (direction < 0)
566 prev_track(DEFAULT_SKIP_TRESH);
567 return;
569 else if (direction == 1 && step >= remaining)
571 #if CONFIG_CODEC == SWCODEC
572 if(global_settings.beep)
573 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
574 #endif
575 return;
577 else if ((direction == -1 && elapsed < step))
579 elapsed = 0;
581 else
583 elapsed += step * direction;
585 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
587 #if (CONFIG_CODEC == SWCODEC)
588 audio_pre_ff_rewind();
589 #else
590 audio_pause();
591 #endif
593 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
594 #if (CONFIG_CODEC != SWCODEC)
595 if (!wps_state.paused)
596 audio_resume();
597 #endif
601 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
603 * If the user is unable to see the wps, because the display is deactivated,
604 * we suppress updates until the wps is activated again (the lcd driver will
605 * call this hook to issue an instant update)
606 * */
607 static void wps_lcd_activation_hook(void *param)
609 (void)param;
610 wps_sync_data.do_full_update = true;
611 /* force timeout in wps main loop, so that the update is instantly */
612 queue_post(&button_queue, BUTTON_NONE, 0);
614 #endif
616 static void gwps_leave_wps(void)
618 int i;
620 FOR_NB_SCREENS(i)
622 gui_wps[i].display->stop_scroll();
623 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
624 gui_wps[i].display->backdrop_show(sb_get_backdrop(i));
625 #endif
626 viewportmanager_theme_undo(i, skin_has_sbs(i, gui_wps[i].data));
630 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
631 /* Play safe and unregister the hook */
632 remove_event(LCD_EVENT_ACTIVATION, wps_lcd_activation_hook);
633 #endif
634 /* unhandle statusbar update delay */
635 sb_skin_set_update_delay(DEFAULT_UPDATE_DELAY);
639 * display the wps on entering or restoring */
640 static void gwps_enter_wps(void)
642 int i;
643 FOR_NB_SCREENS(i)
645 struct gui_wps *gwps = &gui_wps[i];
646 struct screen *display = gwps->display;
647 display->stop_scroll();
648 viewportmanager_theme_enable(i, skin_has_sbs(i, gui_wps[i].data), NULL);
650 /* Update the values in the first (default) viewport - in case the user
651 has modified the statusbar or colour settings */
652 #if LCD_DEPTH > 1
653 if (display->depth > 1)
655 struct viewport *vp = &find_viewport(VP_DEFAULT_LABEL,
656 false, gwps->data)->vp;
657 vp->fg_pattern = display->get_foreground();
658 vp->bg_pattern = display->get_background();
660 #endif
661 /* make the backdrop actually take effect */
662 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
663 display->backdrop_show(gwps->data->backdrop);
664 #endif
665 display->clear_display();
666 skin_update(gwps, SKIN_REFRESH_ALL);
668 #ifdef HAVE_TOUCHSCREEN
669 skin_disarm_touchregions(gui_wps[i].data);
670 #endif
672 /* force statusbar/skin update since we just cleared the whole screen */
673 send_event(GUI_EVENT_ACTIONUPDATE, (void*)1);
676 /* The WPS can be left in two ways:
677 * a) call a function, which draws over the wps. In this case, the wps
678 * will be still active (i.e. the below function didn't return)
679 * b) return with a value evaluated by root_menu.c, in this case the wps
680 * is really left, and root_menu will handle the next screen
682 * In either way, call gwps_leave_wps(), in order to restore the correct
683 * "main screen" backdrops and statusbars
685 long gui_wps_show(void)
687 long button = 0;
688 bool restore = true;
689 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
690 bool exit = false;
691 bool bookmark = false;
692 bool update = false;
693 bool vol_changed = false;
694 int i;
695 long last_left = 0, last_right = 0;
697 #ifdef HAVE_LCD_CHARCELLS
698 status_set_audio(true);
699 status_set_param(false);
700 #endif
702 #ifdef AB_REPEAT_ENABLE
703 ab_repeat_init();
704 ab_reset_markers();
705 #endif
706 wps_state_init();
708 while ( 1 )
710 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
712 /* did someone else (i.e power thread) change audio pause mode? */
713 if (wps_state.paused != audio_paused) {
714 wps_state.paused = audio_paused;
716 /* if another thread paused audio, we are probably in car mode,
717 about to shut down. lets save the settings. */
718 if (wps_state.paused) {
719 settings_save();
720 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
721 call_storage_idle_notifys(true);
722 #endif
725 button = skin_wait_for_action(gui_wps, CONTEXT_WPS|ALLOW_SOFTLOCK,
726 restore ? 1 : HZ/5);
728 /* Exit if audio has stopped playing. This happens e.g. at end of
729 playlist or if using the sleep timer. */
730 if (!(audio_status() & AUDIO_STATUS_PLAY))
731 exit = true;
732 #ifdef HAVE_TOUCHSCREEN
733 if (button == ACTION_TOUCHSCREEN)
734 button = skintouch_to_wps(gui_wps[SCREEN_MAIN].data);
735 #endif
736 /* The iPods/X5/M5 use a single button for the A-B mode markers,
737 defined as ACTION_WPSAB_SINGLE in their config files. */
738 #ifdef ACTION_WPSAB_SINGLE
739 if (!global_settings.party_mode && ab_repeat_mode_enabled())
741 static int wps_ab_state = 0;
742 if (button == ACTION_WPSAB_SINGLE)
744 switch (wps_ab_state)
746 case 0: /* set the A spot */
747 button = ACTION_WPS_ABSETA_PREVDIR;
748 break;
749 case 1: /* set the B spot */
750 button = ACTION_WPS_ABSETB_NEXTDIR;
751 break;
752 case 2:
753 button = ACTION_WPS_ABRESET;
754 break;
756 wps_ab_state = (wps_ab_state+1) % 3;
759 #endif
760 switch(button)
762 #ifdef HAVE_HOTKEY
763 case ACTION_WPS_HOTKEY:
764 if (!global_settings.hotkey_wps)
765 break;
766 /* fall through */
767 #endif
768 case ACTION_WPS_CONTEXT:
770 bool hotkey = button == ACTION_WPS_HOTKEY;
771 gwps_leave_wps();
772 int retval = onplay(wps_state.id3->path,
773 FILE_ATTR_AUDIO, CONTEXT_WPS, hotkey);
774 /* if music is stopped in the context menu we want to exit the wps */
775 if (retval == ONPLAY_MAINMENU
776 || !audio_status())
777 return GO_TO_ROOT;
778 else if (retval == ONPLAY_PLAYLIST)
779 return GO_TO_PLAYLIST_VIEWER;
780 #ifdef HAVE_PICTUREFLOW_INTEGRATION
781 else if (retval == ONPLAY_PICTUREFLOW)
782 return GO_TO_PICTUREFLOW;
783 #endif
784 restore = true;
786 break;
788 case ACTION_WPS_BROWSE:
789 #ifdef HAVE_LCD_CHARCELLS
790 status_set_record(false);
791 status_set_audio(false);
792 #endif
793 gwps_leave_wps();
794 return GO_TO_PREVIOUS_BROWSER;
795 break;
797 /* play/pause */
798 case ACTION_WPS_PLAY:
799 if (global_settings.party_mode)
800 break;
801 if ( wps_state.paused )
803 wps_state.paused = false;
804 if ( global_settings.fade_on_stop )
805 fade(true, true);
806 else
807 audio_resume();
809 else
811 wps_state.paused = true;
812 if ( global_settings.fade_on_stop )
813 fade(false, true);
814 else
815 audio_pause();
816 settings_save();
817 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
818 call_storage_idle_notifys(true); /* make sure resume info is saved */
819 #endif
821 break;
823 case ACTION_WPS_VOLUP:
824 global_settings.volume++;
825 vol_changed = true;
826 break;
827 case ACTION_WPS_VOLDOWN:
828 global_settings.volume--;
829 vol_changed = true;
830 break;
831 /* fast forward
832 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
833 case ACTION_WPS_SEEKFWD:
834 if (global_settings.party_mode)
835 break;
836 if (current_tick -last_right < HZ)
838 if (wps_state.id3->cuesheet)
840 audio_next();
842 else
844 change_dir(1);
847 else
848 ffwd_rew(ACTION_WPS_SEEKFWD);
849 last_right = last_left = 0;
850 break;
851 /* fast rewind
852 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
853 case ACTION_WPS_SEEKBACK:
854 if (global_settings.party_mode)
855 break;
856 if (current_tick -last_left < HZ)
858 if (wps_state.id3->cuesheet)
860 if (!wps_state.paused)
861 #if (CONFIG_CODEC == SWCODEC)
862 audio_pre_ff_rewind();
863 #else
864 audio_pause();
865 #endif
866 audio_ff_rewind(0);
868 else
870 change_dir(-1);
873 else
874 ffwd_rew(ACTION_WPS_SEEKBACK);
875 last_left = last_right = 0;
876 break;
878 /* prev / restart */
879 case ACTION_WPS_SKIPPREV:
880 if (global_settings.party_mode)
881 break;
882 last_left = current_tick;
883 #ifdef AB_REPEAT_ENABLE
884 /* if we're in A/B repeat mode and the current position
885 is past the A marker, jump back to the A marker... */
886 if ( ab_repeat_mode_enabled() )
888 if ( ab_after_A_marker(wps_state.id3->elapsed) )
890 ab_jump_to_A_marker();
891 break;
894 else
895 /* ...otherwise, do it normally */
896 #endif
897 play_hop(-1);
898 break;
900 /* next
901 OR if skip length set, hop by predetermined amount. */
902 case ACTION_WPS_SKIPNEXT:
903 if (global_settings.party_mode)
904 break;
905 last_right = current_tick;
906 #ifdef AB_REPEAT_ENABLE
907 /* if we're in A/B repeat mode and the current position is
908 before the A marker, jump to the A marker... */
909 if ( ab_repeat_mode_enabled() )
911 if ( ab_before_A_marker(wps_state.id3->elapsed) )
913 ab_jump_to_A_marker();
914 break;
917 else
918 /* ...otherwise, do it normally */
919 #endif
920 play_hop(1);
921 break;
922 /* next / prev directories */
923 /* and set A-B markers if in a-b mode */
924 case ACTION_WPS_ABSETB_NEXTDIR:
925 if (global_settings.party_mode)
926 break;
927 #if defined(AB_REPEAT_ENABLE)
928 if (ab_repeat_mode_enabled())
930 ab_set_B_marker(wps_state.id3->elapsed);
931 ab_jump_to_A_marker();
933 else
934 #endif
936 change_dir(1);
938 break;
939 case ACTION_WPS_ABSETA_PREVDIR:
940 if (global_settings.party_mode)
941 break;
942 #if defined(AB_REPEAT_ENABLE)
943 if (ab_repeat_mode_enabled())
944 ab_set_A_marker(wps_state.id3->elapsed);
945 else
946 #endif
948 change_dir(-1);
950 break;
951 /* menu key functions */
952 case ACTION_WPS_MENU:
953 gwps_leave_wps();
954 return GO_TO_ROOT;
955 break;
958 #ifdef HAVE_QUICKSCREEN
959 case ACTION_WPS_QUICKSCREEN:
961 gwps_leave_wps();
962 if (quick_screen_quick(button))
963 return GO_TO_ROOT;
964 restore = true;
966 break;
967 #endif /* HAVE_QUICKSCREEN */
969 /* screen settings */
970 #ifdef BUTTON_F3
971 case ACTION_F3:
973 gwps_leave_wps();
974 if (quick_screen_f3(BUTTON_F3))
975 return GO_TO_ROOT;
976 restore = true;
978 break;
979 #endif /* BUTTON_F3 */
981 /* pitch screen */
982 #ifdef HAVE_PITCHSCREEN
983 case ACTION_WPS_PITCHSCREEN:
985 gwps_leave_wps();
986 if (1 == gui_syncpitchscreen_run())
987 return GO_TO_ROOT;
988 restore = true;
990 break;
991 #endif /* HAVE_PITCHSCREEN */
993 #ifdef AB_REPEAT_ENABLE
994 /* reset A&B markers */
995 case ACTION_WPS_ABRESET:
996 if (ab_repeat_mode_enabled())
998 ab_reset_markers();
999 update = true;
1001 break;
1002 #endif /* AB_REPEAT_ENABLE */
1004 /* stop and exit wps */
1005 case ACTION_WPS_STOP:
1006 if (global_settings.party_mode)
1007 break;
1008 bookmark = true;
1009 exit = true;
1010 break;
1012 case ACTION_WPS_ID3SCREEN:
1014 gwps_leave_wps();
1015 if (browse_id3())
1016 return GO_TO_ROOT;
1017 restore = true;
1019 break;
1020 #ifdef HAVE_TOUCHSCREEN
1021 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1023 global_settings.playlist_shuffle =
1024 !global_settings.playlist_shuffle;
1025 #if CONFIG_CODEC == SWCODEC
1026 dsp_set_replaygain();
1027 #endif
1028 if (global_settings.playlist_shuffle)
1029 playlist_randomise(NULL, current_tick, true);
1030 else
1031 playlist_sort(NULL, true);
1033 break;
1034 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1036 const struct settings_list *rep_setting =
1037 find_setting(&global_settings.repeat_mode, NULL);
1038 option_select_next_val(rep_setting, false, true);
1039 audio_flush_and_reload_tracks();
1041 break;
1042 #endif /* HAVE_TOUCHSCREEN */
1043 /* this case is used by the softlock feature
1044 * it requests a full update here */
1045 case ACTION_REDRAW:
1046 wps_sync_data.do_full_update = true;
1047 break;
1048 case ACTION_NONE: /* Timeout, do a partial update */
1049 update = true;
1050 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1051 break;
1052 #ifdef HAVE_RECORDING
1053 case ACTION_WPS_REC:
1054 exit = true;
1055 break;
1056 #endif
1057 case SYS_POWEROFF:
1058 default_event_handler(SYS_POWEROFF);
1059 break;
1060 case ACTION_WPS_VIEW_PLAYLIST:
1061 gwps_leave_wps();
1062 return GO_TO_PLAYLIST_VIEWER;
1063 break;
1064 default:
1065 if(default_event_handler(button) == SYS_USB_CONNECTED)
1067 gwps_leave_wps();
1068 return GO_TO_ROOT;
1070 update = true;
1071 break;
1074 if (vol_changed)
1076 bool res = false;
1077 vol_changed = false;
1078 setvol();
1079 FOR_NB_SCREENS(i)
1081 if(update_onvol_change(&gui_wps[i]))
1082 res = true;
1084 if (res) {
1085 restore = true;
1086 restoretimer = RESTORE_WPS_NEXT_SECOND;
1091 if (restore &&
1092 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1093 TIME_AFTER(current_tick, restoretimer)))
1095 restore = false;
1096 restoretimer = RESTORE_WPS_INSTANTLY;
1097 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1098 add_event(LCD_EVENT_ACTIVATION, false, wps_lcd_activation_hook);
1099 #endif
1100 /* we remove the update delay since it's not very usable in the wps,
1101 * e.g. during volume changing or ffwd/rewind */
1102 sb_skin_set_update_delay(0);
1103 wps_sync_data.do_full_update = update = false;
1104 gwps_enter_wps();
1106 else if (wps_sync_data.do_full_update || update)
1108 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1109 gwps_caption_backlight(&wps_state);
1110 #endif
1111 FOR_NB_SCREENS(i)
1113 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1114 /* currently, all remotes are readable without backlight
1115 * so still update those */
1116 if (lcd_active() || (i != SCREEN_MAIN))
1117 #endif
1119 skin_update(&gui_wps[i], wps_sync_data.do_full_update ?
1120 SKIN_REFRESH_ALL : SKIN_REFRESH_NON_STATIC);
1123 wps_sync_data.do_full_update = false;
1124 update = false;
1127 if (exit) {
1128 #ifdef HAVE_LCD_CHARCELLS
1129 status_set_record(false);
1130 status_set_audio(false);
1131 #endif
1132 if (global_settings.fade_on_stop)
1133 fade(false, true);
1135 if (bookmark)
1136 bookmark_autobookmark(true);
1137 audio_stop();
1138 #ifdef AB_REPEAT_ENABLE
1139 ab_reset_markers();
1140 #endif
1141 gwps_leave_wps();
1142 #ifdef HAVE_RECORDING
1143 if (button == ACTION_WPS_REC)
1144 return GO_TO_RECSCREEN;
1145 #endif
1146 if (global_settings.browse_current)
1147 return GO_TO_PREVIOUS_BROWSER;
1148 return GO_TO_PREVIOUS;
1151 if (button && !IS_SYSEVENT(button) )
1152 storage_spin();
1154 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1157 /* this is called from the playback thread so NO DRAWING! */
1158 static void track_changed_callback(void *param)
1160 wps_state.id3 = (struct mp3entry*)param;
1161 wps_state.nid3 = audio_next_track();
1162 if (wps_state.id3->cuesheet)
1164 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1166 wps_sync_data.do_full_update = true;
1168 static void nextid3available_callback(void* param)
1170 (void)param;
1171 wps_state.nid3 = audio_next_track();
1172 wps_sync_data.do_full_update = true;
1176 static void wps_state_init(void)
1178 wps_state.ff_rewind = false;
1179 wps_state.paused = false;
1180 if(audio_status() & AUDIO_STATUS_PLAY)
1182 wps_state.id3 = audio_current_track();
1183 wps_state.nid3 = audio_next_track();
1185 else
1187 wps_state.id3 = NULL;
1188 wps_state.nid3 = NULL;
1190 /* We'll be updating due to restore initialized with true */
1191 wps_sync_data.do_full_update = false;
1192 /* add the WPS track event callbacks */
1193 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1194 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1198 void gui_sync_wps_init(void)
1200 int i;
1201 FOR_NB_SCREENS(i)
1203 #ifdef HAVE_ALBUMART
1204 wps_datas[i].albumart = NULL;
1205 wps_datas[i].playback_aa_slot = -1;
1206 #endif
1207 gui_wps[i].data = &wps_datas[i];
1208 gui_wps[i].display = &screens[i];
1209 /* Currently no seperate wps_state needed/possible
1210 so use the only available ( "global" ) one */
1211 gui_wps[i].state = &wps_state;
1212 /* must point to the same struct for both screens */
1213 gui_wps[i].sync_data = &wps_sync_data;
1218 #ifdef IPOD_ACCESSORY_PROTOCOL
1219 bool is_wps_fading(void)
1221 return wps_state.is_fading;
1224 int wps_get_ff_rewind_count(void)
1226 return wps_state.ff_rewind_count;
1228 #endif