Fix FS#10745 - %mv not working in sbs...
[kugel-rb.git] / apps / gui / wps.c
blob441ac3f9d8cbf56fa93a4d0754b19544fc514aa9
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 "sprintf.h"
35 #include "settings.h"
36 #include "skin_engine/skin_engine.h"
37 #include "mp3_playback.h"
38 #include "audio.h"
39 #include "usb.h"
40 #include "status.h"
41 #include "storage.h"
42 #include "screens.h"
43 #include "playlist.h"
44 #ifdef HAVE_LCD_BITMAP
45 #include "icons.h"
46 #include "peakmeter.h"
47 #endif
48 #include "lang.h"
49 #include "bookmark.h"
50 #include "misc.h"
51 #include "sound.h"
52 #include "onplay.h"
53 #include "abrepeat.h"
54 #include "playback.h"
55 #include "splash.h"
56 #include "cuesheet.h"
57 #include "ata_idle_notify.h"
58 #include "root_menu.h"
59 #include "backdrop.h"
60 #include "quickscreen.h"
61 #include "pitchscreen.h"
62 #include "appevents.h"
63 #include "viewport.h"
64 #include "pcmbuf.h"
65 #include "option_select.h"
66 #include "dsp.h"
67 #include "playlist_viewer.h"
68 #include "wps.h"
69 #include "statusbar-skinned.h"
71 #define RESTORE_WPS_INSTANTLY 0l
72 #define RESTORE_WPS_NEXT_SECOND ((long)(HZ+current_tick))
73 /* in milliseconds */
74 #define DEFAULT_SKIP_TRESH 3000l
77 #define FF_REWIND_MAX_PERCENT 3 /* cap ff/rewind step size at max % of file */
78 /* 3% of 30min file == 54s step size */
79 #define MIN_FF_REWIND_STEP 500
81 /* currently only one wps_state is needed, initialize to 0 */
82 struct wps_state wps_state = { .id3 = NULL };
83 static struct gui_wps gui_wps[NB_SCREENS] = {{ .data = NULL }};
84 static struct wps_data wps_datas[NB_SCREENS] = {{ .wps_loaded = 0 }};
85 static struct wps_sync_data wps_sync_data = { .do_full_update = false };
87 /* initial setup of wps_data */
88 static void wps_state_init(void);
89 static void track_changed_callback(void *param);
90 static void nextid3available_callback(void* param);
91 #ifdef HAVE_LCD_BITMAP
92 static void statusbar_toggle_handler(void *data);
93 #endif
95 #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps"
96 #ifdef HAVE_REMOTE_LCD
97 #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps"
98 #define DEFAULT_WPS(screen) ((screen) == SCREEN_MAIN ? \
99 WPS_DEFAULTCFG:RWPS_DEFAULTCFG)
100 #else
101 #define DEFAULT_WPS(screen) (WPS_DEFAULTCFG)
102 #endif
104 void wps_data_load(enum screen_type screen, const char *buf, bool isfile)
106 bool loaded_ok;
108 screens[screen].backdrop_unload(BACKDROP_SKIN_WPS);
110 #ifndef __PCTOOL__
112 * Hardcode loading WPS_DEFAULTCFG to cause a reset ideally this
113 * wants to be a virtual file. Feel free to modify dirbrowse()
114 * if you're feeling brave.
117 if (buf && ! strcmp(buf, DEFAULT_WPS(screen)) )
119 #ifdef HAVE_REMOTE_LCD
120 if (screen == SCREEN_REMOTE)
121 global_settings.rwps_file[0] = '\0';
122 else
123 #endif
124 global_settings.wps_file[0] = '\0';
125 buf = NULL;
128 #endif /* __PCTOOL__ */
130 loaded_ok = buf && skin_data_load(gui_wps[screen].data, buf, isfile);
132 if (!loaded_ok) /* load the hardcoded default */
134 char *skin_buf[NB_SCREENS] = {
135 #ifdef HAVE_LCD_BITMAP
136 "%s%?it<%?in<%in. |>%it|%fn>\n"
137 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
138 "%s%?id<%id|%?d1<%d1|(root)>> %?iy<(%iy)|>\n\n"
139 "%al%pc/%pt%ar[%pp:%pe]\n"
140 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
141 "%pb\n%pm\n",
142 #else
143 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d2> - %?id<%id|%d1>\n"
144 "%pc%?ps<*|/>%pt\n",
145 #endif
146 #ifdef HAVE_REMOTE_LCD
147 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
148 "%s%?it<%?in<%in. |>%it|%fn>\n"
149 "%al%pc/%pt%ar[%pp:%pe]\n"
150 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
151 "%pb\n",
152 #endif
154 skin_data_load(gui_wps[screen].data, skin_buf[screen], false);
156 #ifdef HAVE_REMOVE_LCD
157 gui_wps[screen].data->remote_wps = !(screen == SCREEN_MAIN);
159 #endif
162 static bool wps_fading_out = false;
163 void fade(bool fade_in, bool updatewps)
165 int fp_global_vol = global_settings.volume << 8;
166 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
167 int fp_step = (fp_global_vol - fp_min_vol) / 30;
168 int i;
169 wps_fading_out = !fade_in;
170 if (fade_in) {
171 /* fade in */
172 int fp_volume = fp_min_vol;
174 /* zero out the sound */
175 sound_set_volume(fp_min_vol >> 8);
177 sleep(HZ/10); /* let audio thread run */
178 audio_resume();
180 while (fp_volume < fp_global_vol - fp_step) {
181 fp_volume += fp_step;
182 sound_set_volume(fp_volume >> 8);
183 if (updatewps)
185 FOR_NB_SCREENS(i)
186 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
188 sleep(1);
190 sound_set_volume(global_settings.volume);
192 else {
193 /* fade out */
194 int fp_volume = fp_global_vol;
196 while (fp_volume > fp_min_vol + fp_step) {
197 fp_volume -= fp_step;
198 sound_set_volume(fp_volume >> 8);
199 if (updatewps)
201 FOR_NB_SCREENS(i)
202 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
204 sleep(1);
206 audio_pause();
207 wps_fading_out = false;
208 #if CONFIG_CODEC != SWCODEC
209 #ifndef SIMULATOR
210 /* let audio thread run and wait for the mas to run out of data */
211 while (!mp3_pause_done())
212 #endif
213 sleep(HZ/10);
214 #endif
216 /* reset volume to what it was before the fade */
217 sound_set_volume(global_settings.volume);
220 bool is_wps_fading(void)
222 return wps_fading_out;
225 static bool update_onvol_change(struct gui_wps * gwps)
227 skin_update(gwps, WPS_REFRESH_NON_STATIC);
229 #ifdef HAVE_LCD_CHARCELLS
230 splashf(0, "Vol: %3d dB",
231 sound_val2phys(SOUND_VOLUME, global_settings.volume));
232 return true;
233 #endif
234 return false;
238 bool ffwd_rew(int button)
240 unsigned int step = 0; /* current ff/rewind step */
241 unsigned int max_step = 0; /* maximum ff/rewind step */
242 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
243 int direction = -1; /* forward=1 or backward=-1 */
244 bool exit = false;
245 bool usb = false;
246 int i = 0;
247 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
249 if (button == ACTION_NONE)
251 status_set_ffmode(0);
252 return usb;
254 while (!exit)
256 switch ( button )
258 case ACTION_WPS_SEEKFWD:
259 direction = 1;
260 case ACTION_WPS_SEEKBACK:
261 if (wps_state.ff_rewind)
263 if (direction == 1)
265 /* fast forwarding, calc max step relative to end */
266 max_step = (wps_state.id3->length -
267 (wps_state.id3->elapsed +
268 ff_rewind_count)) *
269 FF_REWIND_MAX_PERCENT / 100;
271 else
273 /* rewinding, calc max step relative to start */
274 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
275 FF_REWIND_MAX_PERCENT / 100;
278 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
280 if (step > max_step)
281 step = max_step;
283 ff_rewind_count += step * direction;
285 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
286 step += step >> ff_rw_accel;
288 else
290 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
291 wps_state.id3 && wps_state.id3->length )
293 if (!wps_state.paused)
294 #if (CONFIG_CODEC == SWCODEC)
295 audio_pre_ff_rewind();
296 #else
297 audio_pause();
298 #endif
299 #if CONFIG_KEYPAD == PLAYER_PAD
300 FOR_NB_SCREENS(i)
301 gui_wps[i].display->stop_scroll();
302 #endif
303 if (direction > 0)
304 status_set_ffmode(STATUS_FASTFORWARD);
305 else
306 status_set_ffmode(STATUS_FASTBACKWARD);
308 wps_state.ff_rewind = true;
310 step = 1000 * global_settings.ff_rewind_min_step;
312 else
313 break;
316 if (direction > 0) {
317 if ((wps_state.id3->elapsed + ff_rewind_count) >
318 wps_state.id3->length)
319 ff_rewind_count = wps_state.id3->length -
320 wps_state.id3->elapsed;
322 else {
323 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
324 ff_rewind_count = -wps_state.id3->elapsed;
327 /* set the wps state ff_rewind_count so the progess info
328 displays corectly */
329 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
330 ff_rewind_count:-ff_rewind_count;
331 FOR_NB_SCREENS(i)
333 skin_update(&gui_wps[i],
334 WPS_REFRESH_PLAYER_PROGRESS |
335 WPS_REFRESH_DYNAMIC);
338 break;
340 case ACTION_WPS_STOPSEEK:
341 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
342 audio_ff_rewind(wps_state.id3->elapsed);
343 wps_state.ff_rewind_count = 0;
344 wps_state.ff_rewind = false;
345 status_set_ffmode(0);
346 #if (CONFIG_CODEC != SWCODEC)
347 if (!wps_state.paused)
348 audio_resume();
349 #endif
350 #ifdef HAVE_LCD_CHARCELLS
351 FOR_NB_SCREENS(i)
352 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
353 #endif
354 exit = true;
355 break;
357 default:
358 if(default_event_handler(button) == SYS_USB_CONNECTED) {
359 status_set_ffmode(0);
360 usb = true;
361 exit = true;
363 break;
365 if (!exit)
367 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
368 #ifdef HAVE_TOUCHSCREEN
369 if (button == ACTION_TOUCHSCREEN)
370 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
371 #endif
374 return usb;
378 void display_keylock_text(bool locked)
380 int i;
381 FOR_NB_SCREENS(i)
382 gui_wps[i].display->stop_scroll();
384 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
390 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
391 static void gwps_caption_backlight(struct wps_state *state)
393 if (state && state->id3)
395 #ifdef HAVE_BACKLIGHT
396 if (global_settings.caption_backlight)
398 /* turn on backlight n seconds before track ends, and turn it off n
399 seconds into the new track. n == backlight_timeout, or 5s */
400 int n = global_settings.backlight_timeout * 1000;
402 if ( n < 1000 )
403 n = 5000; /* use 5s if backlight is always on or off */
405 if (((state->id3->elapsed < 1000) ||
406 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
407 (state->paused == false))
408 backlight_on();
410 #endif
411 #ifdef HAVE_REMOTE_LCD
412 if (global_settings.remote_caption_backlight)
414 /* turn on remote backlight n seconds before track ends, and turn it
415 off n seconds into the new track. n == remote_backlight_timeout,
416 or 5s */
417 int n = global_settings.remote_backlight_timeout * 1000;
419 if ( n < 1000 )
420 n = 5000; /* use 5s if backlight is always on or off */
422 if (((state->id3->elapsed < 1000) ||
423 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
424 (state->paused == false))
425 remote_backlight_on();
427 #endif
430 #endif
433 static void change_dir(int direction)
435 if (global_settings.prevent_skip)
436 return;
438 if (direction < 0)
439 audio_prev_dir();
440 else if (direction > 0)
441 audio_next_dir();
442 /* prevent the next dir to immediatly start being ffw'd */
443 action_wait_for_release();
446 static void prev_track(unsigned long skip_thresh)
448 if (wps_state.id3->elapsed < skip_thresh)
450 audio_prev();
451 return;
453 else
455 if (wps_state.id3->cuesheet)
457 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
458 return;
461 if (!wps_state.paused)
462 #if (CONFIG_CODEC == SWCODEC)
463 audio_pre_ff_rewind();
464 #else
465 audio_pause();
466 #endif
468 audio_ff_rewind(0);
470 #if (CONFIG_CODEC != SWCODEC)
471 if (!wps_state.paused)
472 audio_resume();
473 #endif
477 static void next_track(void)
479 /* take care of if we're playing a cuesheet */
480 if (wps_state.id3->cuesheet)
482 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
484 /* if the result was false, then we really want
485 to skip to the next track */
486 return;
490 audio_next();
493 static void play_hop(int direction)
495 long step = global_settings.skip_length*1000;
496 long elapsed = wps_state.id3->elapsed;
497 long remaining = wps_state.id3->length - elapsed;
499 if (step < 0)
501 if (direction < 0)
503 prev_track(DEFAULT_SKIP_TRESH);
504 return;
506 else if (remaining < DEFAULT_SKIP_TRESH*2)
508 next_track();
509 return;
511 else
512 elapsed += (remaining - DEFAULT_SKIP_TRESH*2);
514 else if (!global_settings.prevent_skip &&
515 (!step ||
516 (direction > 0 && step >= remaining) ||
517 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
518 { /* Do normal track skipping */
519 if (direction > 0)
520 next_track();
521 else if (direction < 0)
522 prev_track(DEFAULT_SKIP_TRESH);
523 return;
525 else if (direction == 1 && step >= remaining)
527 #if CONFIG_CODEC == SWCODEC
528 if(global_settings.beep)
529 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
530 #endif
531 return;
533 else if ((direction == -1 && elapsed < step))
535 elapsed = 0;
537 else
539 elapsed += step * direction;
541 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
543 #if (CONFIG_CODEC == SWCODEC)
544 audio_pre_ff_rewind();
545 #else
546 audio_pause();
547 #endif
549 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
550 #if (CONFIG_CODEC != SWCODEC)
551 if (!wps_state.paused)
552 audio_resume();
553 #endif
557 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
559 * If the user is unable to see the wps, because the display is deactivated,
560 * we suppress updates until the wps is activated again (the lcd driver will
561 * call this hook to issue an instant update)
562 * */
563 static void wps_lcd_activation_hook(void *param)
565 (void)param;
566 wps_sync_data.do_full_update = true;
567 /* force timeout in wps main loop, so that the update is instantly */
568 queue_post(&button_queue, BUTTON_NONE, 0);
570 #endif
572 static void gwps_leave_wps(void)
574 int i, oldbars = VP_SB_HIDE_ALL;
576 FOR_NB_SCREENS(i)
578 gui_wps[i].display->stop_scroll();
579 gui_wps[i].display->backdrop_show(BACKDROP_MAIN);
580 if (statusbar_position(i) != STATUSBAR_OFF)
581 oldbars |= VP_SB_ONSCREEN(i);
584 viewportmanager_set_statusbar(oldbars);
585 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
586 /* Play safe and unregister the hook */
587 remove_event(LCD_EVENT_ACTIVATION, wps_lcd_activation_hook);
588 #endif
589 /* unhandle statusbar update delay */
590 sb_skin_set_update_delay(DEFAULT_UPDATE_DELAY);
591 send_event(GUI_EVENT_REFRESH, NULL);
594 #ifdef HAVE_TOUCHSCREEN
595 int wps_get_touchaction(struct wps_data *data)
597 short x,y;
598 short vx, vy;
599 int type = action_get_touchscreen_press(&x, &y);
600 static int last_action = ACTION_NONE;
601 struct touchregion *r;
602 bool repeated = (type == BUTTON_REPEAT);
603 bool released = (type == BUTTON_REL);
604 struct skin_token_list *regions = data->touchregions;
605 while (regions)
607 r = (struct touchregion *)regions->token->value.data;
608 /* make sure this region's viewport is visible */
609 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
611 regions = regions->next;
612 continue;
614 /* check if it's inside this viewport */
615 if (viewport_point_within_vp(&(r->wvp->vp), x, y))
616 { /* reposition the touch inside the viewport since touchregions
617 * are relative to a preceding viewport */
618 vx = x - r->wvp->vp.x;
619 vy = y - r->wvp->vp.y;
620 /* now see if the point is inside this region */
621 if (vx >= r->x && vx < r->x+r->width &&
622 vy >= r->y && vy < r->y+r->height)
624 /* reposition the touch within the area */
625 vx -= r->x;
626 vy -= r->y;
628 switch(r->type)
630 case WPS_TOUCHREGION_ACTION:
631 if ((repeated && r->repeat) || (released && !r->repeat))
633 last_action = r->action;
634 return r->action;
636 break;
637 case WPS_TOUCHREGION_SCROLLBAR:
638 if(r->width > r->height)
639 /* landscape */
640 wps_state.id3->elapsed = (vx *
641 wps_state.id3->length) / r->width;
642 else
643 /* portrait */
644 wps_state.id3->elapsed = (vy *
645 wps_state.id3->length) / r->height;
647 if (!wps_state.paused)
648 #if (CONFIG_CODEC == SWCODEC)
649 audio_pre_ff_rewind();
650 #else
651 audio_pause();
652 #endif
653 audio_ff_rewind(wps_state.id3->elapsed);
654 #if (CONFIG_CODEC != SWCODEC)
655 if (!wps_state.paused)
656 audio_resume();
657 #endif
658 break;
659 case WPS_TOUCHREGION_VOLUME:
661 int i;
662 const int min_vol = sound_min(SOUND_VOLUME);
663 const int max_vol = sound_max(SOUND_VOLUME);
664 if(r->width > r->height)
665 /* landscape */
666 global_settings.volume = (vx *
667 (max_vol - min_vol)) / r->width;
668 else
669 /* portrait */
670 global_settings.volume = (vy *
671 (max_vol-min_vol)) / r->height;
673 global_settings.volume += min_vol;
674 setvol();
675 FOR_NB_SCREENS(i)
677 gui_wps[i].data->button_time_volume = current_tick;
679 return ACTION_REDRAW;
684 regions = regions->next;
687 if ((last_action == ACTION_WPS_SEEKBACK || last_action == ACTION_WPS_SEEKFWD))
688 return ACTION_WPS_STOPSEEK;
689 last_action = ACTION_TOUCHSCREEN;
690 return ACTION_TOUCHSCREEN;
692 #endif
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 #else
718 statusbar_toggle_handler(NULL);
719 #endif
721 #ifdef AB_REPEAT_ENABLE
722 ab_repeat_init();
723 ab_reset_markers();
724 #endif
725 wps_state_init();
727 while ( 1 )
729 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
731 /* did someone else (i.e power thread) change audio pause mode? */
732 if (wps_state.paused != audio_paused) {
733 wps_state.paused = audio_paused;
735 /* if another thread paused audio, we are probably in car mode,
736 about to shut down. lets save the settings. */
737 if (wps_state.paused) {
738 settings_save();
739 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
740 call_storage_idle_notifys(true);
741 #endif
744 #ifdef HAVE_LCD_BITMAP
745 /* when the peak meter is enabled we want to have a
746 few extra updates to make it look smooth. On the
747 other hand we don't want to waste energy if it
748 isn't displayed */
749 bool pm=false;
750 FOR_NB_SCREENS(i)
752 if(gui_wps[i].data->peak_meter_enabled)
753 pm = true;
756 if (pm) {
757 long next_refresh = current_tick;
758 long next_big_refresh = current_tick + HZ / 5;
759 button = BUTTON_NONE;
760 while (TIME_BEFORE(current_tick, next_big_refresh)) {
761 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_NOBLOCK);
762 if (button != ACTION_NONE) {
763 break;
765 peak_meter_peek();
766 sleep(0); /* Sleep until end of current tick. */
768 if (TIME_AFTER(current_tick, next_refresh)) {
769 FOR_NB_SCREENS(i)
771 if(gui_wps[i].data->peak_meter_enabled)
772 skin_update(&gui_wps[i], WPS_REFRESH_PEAK_METER);
773 next_refresh += HZ / PEAK_METER_FPS;
780 /* The peak meter is disabled
781 -> no additional screen updates needed */
782 else
783 #endif
785 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,
786 restore ? HZ/100 : HZ/5);
789 /* Exit if audio has stopped playing. This happens e.g. at end of
790 playlist or if using the sleep timer. */
791 if (!(audio_status() & AUDIO_STATUS_PLAY))
792 exit = true;
793 #ifdef HAVE_TOUCHSCREEN
794 if (button == ACTION_TOUCHSCREEN)
795 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
796 #endif
797 /* The iPods/X5/M5 use a single button for the A-B mode markers,
798 defined as ACTION_WPSAB_SINGLE in their config files. */
799 #ifdef ACTION_WPSAB_SINGLE
800 if (!global_settings.party_mode && ab_repeat_mode_enabled())
802 static int wps_ab_state = 0;
803 if (button == ACTION_WPSAB_SINGLE)
805 switch (wps_ab_state)
807 case 0: /* set the A spot */
808 button = ACTION_WPS_ABSETA_PREVDIR;
809 break;
810 case 1: /* set the B spot */
811 button = ACTION_WPS_ABSETB_NEXTDIR;
812 break;
813 case 2:
814 button = ACTION_WPS_ABRESET;
815 break;
817 wps_ab_state = (wps_ab_state+1) % 3;
820 #endif
821 switch(button)
823 case ACTION_WPS_CONTEXT:
825 gwps_leave_wps();
826 /* if music is stopped in the context menu we want to exit the wps */
827 if (onplay(wps_state.id3->path,
828 FILE_ATTR_AUDIO, CONTEXT_WPS) == ONPLAY_MAINMENU
829 || !audio_status())
830 return GO_TO_ROOT;
831 restore = true;
833 break;
835 case ACTION_WPS_BROWSE:
836 #ifdef HAVE_LCD_CHARCELLS
837 status_set_record(false);
838 status_set_audio(false);
839 #endif
840 gwps_leave_wps();
841 return GO_TO_PREVIOUS_BROWSER;
842 break;
844 /* play/pause */
845 case ACTION_WPS_PLAY:
846 if (global_settings.party_mode)
847 break;
848 if ( wps_state.paused )
850 wps_state.paused = false;
851 if ( global_settings.fade_on_stop )
852 fade(true, true);
853 else
854 audio_resume();
856 else
858 wps_state.paused = true;
859 if ( global_settings.fade_on_stop )
860 fade(false, true);
861 else
862 audio_pause();
863 settings_save();
864 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
865 call_storage_idle_notifys(true); /* make sure resume info is saved */
866 #endif
868 break;
870 case ACTION_WPS_VOLUP:
871 global_settings.volume++;
872 vol_changed = true;
873 break;
874 case ACTION_WPS_VOLDOWN:
875 global_settings.volume--;
876 vol_changed = true;
877 break;
878 /* fast forward
879 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
880 case ACTION_WPS_SEEKFWD:
881 if (global_settings.party_mode)
882 break;
883 if (current_tick -last_right < HZ)
885 if (wps_state.id3->cuesheet)
887 audio_next();
889 else
891 change_dir(1);
894 else
895 ffwd_rew(ACTION_WPS_SEEKFWD);
896 last_right = last_left = 0;
897 break;
898 /* fast rewind
899 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
900 case ACTION_WPS_SEEKBACK:
901 if (global_settings.party_mode)
902 break;
903 if (current_tick -last_left < HZ)
905 if (wps_state.id3->cuesheet)
907 if (!wps_state.paused)
908 #if (CONFIG_CODEC == SWCODEC)
909 audio_pre_ff_rewind();
910 #else
911 audio_pause();
912 #endif
913 audio_ff_rewind(0);
915 else
917 change_dir(-1);
920 else
921 ffwd_rew(ACTION_WPS_SEEKBACK);
922 last_left = last_right = 0;
923 break;
925 /* prev / restart */
926 case ACTION_WPS_SKIPPREV:
927 if (global_settings.party_mode)
928 break;
929 last_left = current_tick;
930 #ifdef AB_REPEAT_ENABLE
931 /* if we're in A/B repeat mode and the current position
932 is past the A marker, jump back to the A marker... */
933 if ( ab_repeat_mode_enabled() )
935 if ( ab_after_A_marker(wps_state.id3->elapsed) )
937 ab_jump_to_A_marker();
938 break;
939 #if (AB_REPEAT_ENABLE == 2)
940 } else {
941 ab_reset_markers();
942 #endif
945 else
946 /* ...otherwise, do it normally */
947 #endif
948 play_hop(-1);
949 break;
951 /* next
952 OR if skip length set, hop by predetermined amount. */
953 case ACTION_WPS_SKIPNEXT:
954 if (global_settings.party_mode)
955 break;
956 last_right = current_tick;
957 #ifdef AB_REPEAT_ENABLE
958 /* if we're in A/B repeat mode and the current position is
959 before the A marker, jump to the A marker... */
960 if ( ab_repeat_mode_enabled() )
962 if ( ab_before_A_marker(wps_state.id3->elapsed) )
964 ab_jump_to_A_marker();
965 break;
966 #if (AB_REPEAT_ENABLE == 2)
967 } else {
968 ab_reset_markers();
969 #endif
972 else
973 /* ...otherwise, do it normally */
974 #endif
975 play_hop(1);
976 break;
977 /* next / prev directories */
978 /* and set A-B markers if in a-b mode */
979 case ACTION_WPS_ABSETB_NEXTDIR:
980 if (global_settings.party_mode)
981 break;
982 #if defined(AB_REPEAT_ENABLE)
983 if (ab_repeat_mode_enabled())
985 ab_set_B_marker(wps_state.id3->elapsed);
986 ab_jump_to_A_marker();
988 else
989 #endif
991 change_dir(1);
993 break;
994 case ACTION_WPS_ABSETA_PREVDIR:
995 if (global_settings.party_mode)
996 break;
997 #if defined(AB_REPEAT_ENABLE)
998 if (ab_repeat_mode_enabled())
999 ab_set_A_marker(wps_state.id3->elapsed);
1000 else
1001 #endif
1003 change_dir(-1);
1005 break;
1006 /* menu key functions */
1007 case ACTION_WPS_MENU:
1008 gwps_leave_wps();
1009 return GO_TO_ROOT;
1010 break;
1013 #ifdef HAVE_QUICKSCREEN
1014 case ACTION_WPS_QUICKSCREEN:
1016 gwps_leave_wps();
1017 if (quick_screen_quick(button))
1018 return GO_TO_ROOT;
1019 restore = true;
1021 break;
1022 #endif /* HAVE_QUICKSCREEN */
1024 /* screen settings */
1025 #ifdef BUTTON_F3
1026 case ACTION_F3:
1028 gwps_leave_wps();
1029 if (quick_screen_f3(BUTTON_F3))
1030 return GO_TO_ROOT;
1031 restore = true;
1033 break;
1034 #endif /* BUTTON_F3 */
1036 /* pitch screen */
1037 #ifdef HAVE_PITCHSCREEN
1038 case ACTION_WPS_PITCHSCREEN:
1040 gwps_leave_wps();
1041 if (1 == gui_syncpitchscreen_run())
1042 return GO_TO_ROOT;
1043 restore = true;
1045 break;
1046 #endif /* HAVE_PITCHSCREEN */
1048 #ifdef AB_REPEAT_ENABLE
1049 /* reset A&B markers */
1050 case ACTION_WPS_ABRESET:
1051 if (ab_repeat_mode_enabled())
1053 ab_reset_markers();
1054 update = true;
1056 break;
1057 #endif /* AB_REPEAT_ENABLE */
1059 /* stop and exit wps */
1060 case ACTION_WPS_STOP:
1061 if (global_settings.party_mode)
1062 break;
1063 bookmark = true;
1064 exit = true;
1065 break;
1067 case ACTION_WPS_ID3SCREEN:
1069 gwps_leave_wps();
1070 if (browse_id3())
1071 return GO_TO_ROOT;
1072 restore = true;
1074 break;
1075 #ifdef HAVE_TOUCHSCREEN
1076 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1078 global_settings.playlist_shuffle =
1079 !global_settings.playlist_shuffle;
1080 #if CONFIG_CODEC == SWCODEC
1081 dsp_set_replaygain();
1082 #endif
1083 if (global_settings.playlist_shuffle)
1084 playlist_randomise(NULL, current_tick, true);
1085 else
1086 playlist_sort(NULL, true);
1088 break;
1089 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1091 const struct settings_list *rep_setting =
1092 find_setting(&global_settings.repeat_mode, NULL);
1093 option_select_next_val(rep_setting, false, true);
1094 audio_flush_and_reload_tracks();
1096 break;
1097 #endif /* HAVE_TOUCHSCREEN */
1098 /* this case is used by the softlock feature
1099 * it requests a full update here */
1100 case ACTION_REDRAW:
1101 wps_sync_data.do_full_update = true;
1102 break;
1103 case ACTION_NONE: /* Timeout, do a partial update */
1104 update = true;
1105 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1106 break;
1107 #ifdef HAVE_RECORDING
1108 case ACTION_WPS_REC:
1109 exit = true;
1110 break;
1111 #endif
1112 case SYS_POWEROFF:
1113 default_event_handler(SYS_POWEROFF);
1114 break;
1115 case ACTION_WPS_VIEW_PLAYLIST:
1116 gwps_leave_wps();
1117 if (playlist_viewer()) /* true if USB connected */
1118 return GO_TO_ROOT;
1119 restore = true;
1120 break;
1121 default:
1122 if(default_event_handler(button) == SYS_USB_CONNECTED)
1124 gwps_leave_wps();
1125 return GO_TO_ROOT;
1127 update = true;
1128 break;
1131 if (vol_changed)
1133 bool res = false;
1134 setvol();
1135 FOR_NB_SCREENS(i)
1137 if(update_onvol_change(&gui_wps[i]))
1138 res = true;
1140 if (res) {
1141 restore = true;
1142 restoretimer = RESTORE_WPS_NEXT_SECOND;
1146 if (wps_sync_data.do_full_update || update)
1148 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1149 gwps_caption_backlight(&wps_state);
1150 #endif
1151 FOR_NB_SCREENS(i)
1153 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1154 /* currently, all remotes are readable without backlight
1155 * so still update those */
1156 if (lcd_active() || (i != SCREEN_MAIN))
1157 #endif
1159 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
1162 wps_sync_data.do_full_update = false;
1163 update = false;
1166 if (restore && wps_state.id3 &&
1167 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1168 TIME_AFTER(current_tick, restoretimer)))
1170 restore = false;
1171 restoretimer = RESTORE_WPS_INSTANTLY;
1172 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1173 add_event(LCD_EVENT_ACTIVATION, false, wps_lcd_activation_hook);
1174 #endif
1175 /* we remove the update delay since it's not very usable in the wps,
1176 * e.g. during volume changing or ffwd/rewind */
1177 sb_skin_set_update_delay(0);
1178 FOR_NB_SCREENS(i)
1180 screens[i].stop_scroll();
1181 gui_wps_display(&gui_wps[i]);
1185 if (exit) {
1186 #ifdef HAVE_LCD_CHARCELLS
1187 status_set_record(false);
1188 status_set_audio(false);
1189 #endif
1190 if (global_settings.fade_on_stop)
1191 fade(false, true);
1193 if (bookmark)
1194 bookmark_autobookmark();
1195 audio_stop();
1196 #ifdef AB_REPEAT_ENABLE
1197 ab_reset_markers();
1198 #endif
1199 gwps_leave_wps();
1200 #ifdef HAVE_RECORDING
1201 if (button == ACTION_WPS_REC)
1202 return GO_TO_RECSCREEN;
1203 #endif
1204 if (global_settings.browse_current)
1205 return GO_TO_PREVIOUS_BROWSER;
1206 return GO_TO_PREVIOUS;
1209 if (button && !IS_SYSEVENT(button) )
1210 storage_spin();
1212 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1215 /* this is called from the playback thread so NO DRAWING! */
1216 static void track_changed_callback(void *param)
1218 wps_state.id3 = (struct mp3entry*)param;
1219 wps_state.nid3 = audio_next_track();
1220 if (wps_state.id3->cuesheet)
1222 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1223 cue_spoof_id3(wps_state.id3->cuesheet, wps_state.id3);
1225 wps_sync_data.do_full_update = true;
1227 static void nextid3available_callback(void* param)
1229 (void)param;
1230 wps_state.nid3 = audio_next_track();
1231 wps_sync_data.do_full_update = true;
1235 static void wps_state_init(void)
1237 wps_state.ff_rewind = false;
1238 wps_state.paused = false;
1239 if(audio_status() & AUDIO_STATUS_PLAY)
1241 wps_state.id3 = audio_current_track();
1242 wps_state.nid3 = audio_next_track();
1244 else
1246 wps_state.id3 = NULL;
1247 wps_state.nid3 = NULL;
1249 /* We'll be updating due to restore initialized with true */
1250 wps_sync_data.do_full_update = false;
1251 /* add the WPS track event callbacks */
1252 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1253 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1257 #ifdef HAVE_LCD_BITMAP
1258 static void statusbar_toggle_handler(void *data)
1260 (void)data;
1261 int i;
1263 int *wpsbars = &wps_sync_data.statusbars;
1264 *wpsbars = VP_SB_HIDE_ALL;
1265 FOR_NB_SCREENS(i)
1266 { /* fix viewports if needed */
1267 skin_statusbar_changed(&gui_wps[i]);
1269 bool draw = false;
1271 /* fix up gui_wps::statusbars, so that the viewportmanager accepts it*/
1272 if (gui_wps[i].data->wps_sb_tag)
1273 draw = gui_wps[i].data->show_sb_on_wps;
1274 else if (statusbar_position(i) != STATUSBAR_OFF)
1275 draw = true;
1276 if (draw)
1277 *wpsbars |=
1278 (VP_SB_ONSCREEN(i) | VP_SB_IGNORE_SETTING(i));
1281 #endif
1284 void gui_sync_wps_init(void)
1286 int i;
1287 FOR_NB_SCREENS(i)
1289 #ifdef HAVE_ALBUMART
1290 wps_datas[i].albumart = NULL;
1291 wps_datas[i].playback_aa_slot = -1;
1292 #endif
1293 #ifdef HAVE_REMOTE_LCD
1294 wps_datas[i].remote_wps = (i == SCREEN_REMOTE);
1295 #endif
1296 gui_wps[i].data = &wps_datas[i];
1297 gui_wps[i].display = &screens[i];
1298 /* Currently no seperate wps_state needed/possible
1299 so use the only available ( "global" ) one */
1300 gui_wps[i].state = &wps_state;
1301 gui_wps[i].display->backdrop_unload(BACKDROP_SKIN_WPS);
1302 /* must point to the same struct for both screens */
1303 gui_wps[i].sync_data = &wps_sync_data;
1304 gui_wps[i].sync_data->statusbars = VP_SB_ALLSCREENS;
1306 #ifdef HAVE_LCD_BITMAP
1307 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggle_handler);
1308 #endif
1312 #ifdef IPOD_ACCESSORY_PROTOCOL
1313 int wps_get_ff_rewind_count(void)
1315 return wps_state.ff_rewind_count;
1317 #endif