Fix wps showing parts of the main backdrop in some circumstances.
[kugel-rb.git] / apps / gui / wps.c
blobcb73e6f92cd4564fafa410d57c1b12de446c243a
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))
74 #define FF_REWIND_MAX_PERCENT 3 /* cap ff/rewind step size at max % of file */
75 /* 3% of 30min file == 54s step size */
76 #define MIN_FF_REWIND_STEP 500
78 /* currently only one wps_state is needed, initialize to 0 */
79 struct wps_state wps_state = { .id3 = NULL };
80 static struct gui_wps gui_wps[NB_SCREENS] = {{ .data = NULL }};
81 static struct wps_data wps_datas[NB_SCREENS] = {{ .wps_loaded = 0 }};
82 static struct wps_sync_data wps_sync_data = { .do_full_update = false };
84 /* initial setup of wps_data */
85 static void wps_state_init(void);
86 static void track_changed_callback(void *param);
87 static void nextid3available_callback(void* param);
89 #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps"
90 #ifdef HAVE_REMOTE_LCD
91 #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps"
92 #define DEFAULT_WPS(screen) ((screen) == SCREEN_MAIN ? \
93 WPS_DEFAULTCFG:RWPS_DEFAULTCFG)
94 #else
95 #define DEFAULT_WPS(screen) (WPS_DEFAULTCFG)
96 #endif
98 void wps_data_load(enum screen_type screen, const char *buf, bool isfile)
100 bool loaded_ok;
102 screens[screen].backdrop_unload(BACKDROP_SKIN_WPS);
104 #ifndef __PCTOOL__
106 * Hardcode loading WPS_DEFAULTCFG to cause a reset ideally this
107 * wants to be a virtual file. Feel free to modify dirbrowse()
108 * if you're feeling brave.
111 if (buf && ! strcmp(buf, DEFAULT_WPS(screen)) )
113 #ifdef HAVE_REMOTE_LCD
114 if (screen == SCREEN_REMOTE)
115 global_settings.rwps_file[0] = '\0';
116 else
117 #endif
118 global_settings.wps_file[0] = '\0';
119 buf = NULL;
122 #endif /* __PCTOOL__ */
124 loaded_ok = buf && skin_data_load(screen, gui_wps[screen].data, buf, isfile);
126 if (!loaded_ok) /* load the hardcoded default */
128 char *skin_buf[NB_SCREENS] = {
129 #ifdef HAVE_LCD_BITMAP
130 "%s%?it<%?in<%in. |>%it|%fn>\n"
131 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
132 "%s%?id<%id|%?d1<%d1|(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|%d2> - %?id<%id|%d1>\n"
138 "%pc%?ps<*|/>%pt\n",
139 #endif
140 #ifdef HAVE_REMOTE_LCD
141 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
142 "%s%?it<%?in<%in. |>%it|%fn>\n"
143 "%al%pc/%pt%ar[%pp:%pe]\n"
144 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
145 "%pb\n",
146 #endif
148 skin_data_load(screen, gui_wps[screen].data, skin_buf[screen], false);
152 void fade(bool fade_in, bool updatewps)
154 int fp_global_vol = global_settings.volume << 8;
155 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
156 int fp_step = (fp_global_vol - fp_min_vol) / 30;
157 int i;
158 wps_state.is_fading = !fade_in;
159 if (fade_in) {
160 /* fade in */
161 int fp_volume = fp_min_vol;
163 /* zero out the sound */
164 sound_set_volume(fp_min_vol >> 8);
166 sleep(HZ/10); /* let audio thread run */
167 audio_resume();
169 while (fp_volume < fp_global_vol - fp_step) {
170 fp_volume += fp_step;
171 sound_set_volume(fp_volume >> 8);
172 if (updatewps)
174 FOR_NB_SCREENS(i)
175 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
177 sleep(1);
179 sound_set_volume(global_settings.volume);
181 else {
182 /* fade out */
183 int fp_volume = fp_global_vol;
185 while (fp_volume > fp_min_vol + fp_step) {
186 fp_volume -= fp_step;
187 sound_set_volume(fp_volume >> 8);
188 if (updatewps)
190 FOR_NB_SCREENS(i)
191 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
193 sleep(1);
195 audio_pause();
196 wps_state.is_fading = false;
197 #if CONFIG_CODEC != SWCODEC
198 #ifndef SIMULATOR
199 /* let audio thread run and wait for the mas to run out of data */
200 while (!mp3_pause_done())
201 #endif
202 sleep(HZ/10);
203 #endif
205 /* reset volume to what it was before the fade */
206 sound_set_volume(global_settings.volume);
210 static bool update_onvol_change(struct gui_wps * gwps)
212 skin_update(gwps, WPS_REFRESH_NON_STATIC);
214 #ifdef HAVE_LCD_CHARCELLS
215 splashf(0, "Vol: %3d dB",
216 sound_val2phys(SOUND_VOLUME, global_settings.volume));
217 return true;
218 #endif
219 return false;
223 bool ffwd_rew(int button)
225 unsigned int step = 0; /* current ff/rewind step */
226 unsigned int max_step = 0; /* maximum ff/rewind step */
227 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
228 int direction = -1; /* forward=1 or backward=-1 */
229 bool exit = false;
230 bool usb = false;
231 int i = 0;
232 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
234 if (button == ACTION_NONE)
236 status_set_ffmode(0);
237 return usb;
239 while (!exit)
241 switch ( button )
243 case ACTION_WPS_SEEKFWD:
244 direction = 1;
245 case ACTION_WPS_SEEKBACK:
246 if (wps_state.ff_rewind)
248 if (direction == 1)
250 /* fast forwarding, calc max step relative to end */
251 max_step = (wps_state.id3->length -
252 (wps_state.id3->elapsed +
253 ff_rewind_count)) *
254 FF_REWIND_MAX_PERCENT / 100;
256 else
258 /* rewinding, calc max step relative to start */
259 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
260 FF_REWIND_MAX_PERCENT / 100;
263 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
265 if (step > max_step)
266 step = max_step;
268 ff_rewind_count += step * direction;
270 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
271 step += step >> ff_rw_accel;
273 else
275 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
276 wps_state.id3 && wps_state.id3->length )
278 if (!wps_state.paused)
279 #if (CONFIG_CODEC == SWCODEC)
280 audio_pre_ff_rewind();
281 #else
282 audio_pause();
283 #endif
284 #if CONFIG_KEYPAD == PLAYER_PAD
285 FOR_NB_SCREENS(i)
286 gui_wps[i].display->stop_scroll();
287 #endif
288 if (direction > 0)
289 status_set_ffmode(STATUS_FASTFORWARD);
290 else
291 status_set_ffmode(STATUS_FASTBACKWARD);
293 wps_state.ff_rewind = true;
295 step = 1000 * global_settings.ff_rewind_min_step;
297 else
298 break;
301 if (direction > 0) {
302 if ((wps_state.id3->elapsed + ff_rewind_count) >
303 wps_state.id3->length)
304 ff_rewind_count = wps_state.id3->length -
305 wps_state.id3->elapsed;
307 else {
308 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
309 ff_rewind_count = -wps_state.id3->elapsed;
312 /* set the wps state ff_rewind_count so the progess info
313 displays corectly */
314 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
315 ff_rewind_count:-ff_rewind_count;
316 FOR_NB_SCREENS(i)
318 skin_update(&gui_wps[i],
319 WPS_REFRESH_PLAYER_PROGRESS |
320 WPS_REFRESH_DYNAMIC);
323 break;
325 case ACTION_WPS_STOPSEEK:
326 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
327 audio_ff_rewind(wps_state.id3->elapsed);
328 wps_state.ff_rewind_count = 0;
329 wps_state.ff_rewind = false;
330 status_set_ffmode(0);
331 #if (CONFIG_CODEC != SWCODEC)
332 if (!wps_state.paused)
333 audio_resume();
334 #endif
335 #ifdef HAVE_LCD_CHARCELLS
336 FOR_NB_SCREENS(i)
337 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
338 #endif
339 exit = true;
340 break;
342 default:
343 if(default_event_handler(button) == SYS_USB_CONNECTED) {
344 status_set_ffmode(0);
345 usb = true;
346 exit = true;
348 break;
350 if (!exit)
352 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
353 #ifdef HAVE_TOUCHSCREEN
354 if (button == ACTION_TOUCHSCREEN)
355 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
356 #endif
359 return usb;
363 void display_keylock_text(bool locked)
365 int i;
366 FOR_NB_SCREENS(i)
367 gui_wps[i].display->stop_scroll();
369 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
375 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
376 static void gwps_caption_backlight(struct wps_state *state)
378 if (state && state->id3)
380 #ifdef HAVE_BACKLIGHT
381 if (global_settings.caption_backlight)
383 /* turn on backlight n seconds before track ends, and turn it off n
384 seconds into the new track. n == backlight_timeout, or 5s */
385 int n = global_settings.backlight_timeout * 1000;
387 if ( n < 1000 )
388 n = 5000; /* use 5s if backlight is always on or off */
390 if (((state->id3->elapsed < 1000) ||
391 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
392 (state->paused == false))
393 backlight_on();
395 #endif
396 #ifdef HAVE_REMOTE_LCD
397 if (global_settings.remote_caption_backlight)
399 /* turn on remote backlight n seconds before track ends, and turn it
400 off n seconds into the new track. n == remote_backlight_timeout,
401 or 5s */
402 int n = global_settings.remote_backlight_timeout * 1000;
404 if ( n < 1000 )
405 n = 5000; /* use 5s if backlight is always on or off */
407 if (((state->id3->elapsed < 1000) ||
408 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
409 (state->paused == false))
410 remote_backlight_on();
412 #endif
415 #endif
418 static void change_dir(int direction)
420 if (global_settings.prevent_skip)
421 return;
423 if (direction < 0)
424 audio_prev_dir();
425 else if (direction > 0)
426 audio_next_dir();
427 /* prevent the next dir to immediatly start being ffw'd */
428 action_wait_for_release();
431 static void prev_track(unsigned long skip_thresh)
433 if (wps_state.id3->elapsed < skip_thresh)
435 audio_prev();
436 return;
438 else
440 if (wps_state.id3->cuesheet)
442 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
443 return;
446 if (!wps_state.paused)
447 #if (CONFIG_CODEC == SWCODEC)
448 audio_pre_ff_rewind();
449 #else
450 audio_pause();
451 #endif
453 audio_ff_rewind(0);
455 #if (CONFIG_CODEC != SWCODEC)
456 if (!wps_state.paused)
457 audio_resume();
458 #endif
462 static void next_track(void)
464 /* take care of if we're playing a cuesheet */
465 if (wps_state.id3->cuesheet)
467 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
469 /* if the result was false, then we really want
470 to skip to the next track */
471 return;
475 audio_next();
478 static void play_hop(int direction)
480 long step = global_settings.skip_length*1000;
481 long elapsed = wps_state.id3->elapsed;
482 long remaining = wps_state.id3->length - elapsed;
484 if (step < 0)
486 if (direction < 0)
488 prev_track(DEFAULT_SKIP_TRESH);
489 return;
491 else if (remaining < DEFAULT_SKIP_TRESH*2)
493 next_track();
494 return;
496 else
497 elapsed += (remaining - DEFAULT_SKIP_TRESH*2);
499 else if (!global_settings.prevent_skip &&
500 (!step ||
501 (direction > 0 && step >= remaining) ||
502 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
503 { /* Do normal track skipping */
504 if (direction > 0)
505 next_track();
506 else if (direction < 0)
507 prev_track(DEFAULT_SKIP_TRESH);
508 return;
510 else if (direction == 1 && step >= remaining)
512 #if CONFIG_CODEC == SWCODEC
513 if(global_settings.beep)
514 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
515 #endif
516 return;
518 else if ((direction == -1 && elapsed < step))
520 elapsed = 0;
522 else
524 elapsed += step * direction;
526 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
528 #if (CONFIG_CODEC == SWCODEC)
529 audio_pre_ff_rewind();
530 #else
531 audio_pause();
532 #endif
534 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
535 #if (CONFIG_CODEC != SWCODEC)
536 if (!wps_state.paused)
537 audio_resume();
538 #endif
542 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
544 * If the user is unable to see the wps, because the display is deactivated,
545 * we suppress updates until the wps is activated again (the lcd driver will
546 * call this hook to issue an instant update)
547 * */
548 static void wps_lcd_activation_hook(void *param)
550 (void)param;
551 wps_sync_data.do_full_update = true;
552 /* force timeout in wps main loop, so that the update is instantly */
553 queue_post(&button_queue, BUTTON_NONE, 0);
555 #endif
557 static void gwps_leave_wps(void)
559 int i;
561 FOR_NB_SCREENS(i)
563 gui_wps[i].display->stop_scroll();
564 gui_wps[i].display->backdrop_show(BACKDROP_MAIN);
566 #ifdef HAVE_LCD_BITMAP
567 bool draw = false;
568 if (gui_wps[i].data->wps_sb_tag)
569 draw = gui_wps[i].data->show_sb_on_wps;
570 else if (statusbar_position(i) != STATUSBAR_OFF)
571 draw = true;
572 #endif
573 viewportmanager_theme_undo(i, draw);
577 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
578 /* Play safe and unregister the hook */
579 remove_event(LCD_EVENT_ACTIVATION, wps_lcd_activation_hook);
580 #endif
581 /* unhandle statusbar update delay */
582 sb_skin_set_update_delay(DEFAULT_UPDATE_DELAY);
586 * display the wps on entering or restoring */
587 static void gwps_enter_wps(void)
589 int i;
590 FOR_NB_SCREENS(i)
592 struct gui_wps *gwps = &gui_wps[i];
593 struct screen *display = gwps->display;
594 #ifdef HAVE_LCD_BITMAP
595 bool draw = false;
596 if (gui_wps[i].data->wps_sb_tag)
597 draw = gui_wps[i].data->show_sb_on_wps;
598 else if (statusbar_position(i) != STATUSBAR_OFF)
599 draw = true;
600 #endif
601 viewportmanager_theme_enable(i, draw, NULL);
603 display->stop_scroll();
604 /* Update the values in the first (default) viewport - in case the user
605 has modified the statusbar or colour settings */
606 #if LCD_DEPTH > 1
607 if (display->depth > 1)
609 struct viewport *vp = &find_viewport(VP_DEFAULT_LABEL, gwps->data)->vp;
610 vp->fg_pattern = display->get_foreground();
611 vp->bg_pattern = display->get_background();
613 #endif
614 display->backdrop_show(BACKDROP_SKIN_WPS);
615 /* make the backdrop actually take effect */
616 display->clear_display();
617 skin_update(gwps, WPS_REFRESH_ALL);
619 /* force statusbar/skin update since we just cleared the whole screen */
620 send_event(GUI_EVENT_ACTIONUPDATE, (void*)1);
623 #ifdef HAVE_TOUCHSCREEN
624 int wps_get_touchaction(struct wps_data *data)
626 short x,y;
627 short vx, vy;
628 int type = action_get_touchscreen_press(&x, &y);
629 static int last_action = ACTION_NONE;
630 struct touchregion *r;
631 bool repeated = (type == BUTTON_REPEAT);
632 bool released = (type == BUTTON_REL);
633 struct skin_token_list *regions = data->touchregions;
634 while (regions)
636 r = (struct touchregion *)regions->token->value.data;
637 /* make sure this region's viewport is visible */
638 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
640 regions = regions->next;
641 continue;
643 /* check if it's inside this viewport */
644 if (viewport_point_within_vp(&(r->wvp->vp), x, y))
645 { /* reposition the touch inside the viewport since touchregions
646 * are relative to a preceding viewport */
647 vx = x - r->wvp->vp.x;
648 vy = y - r->wvp->vp.y;
649 /* now see if the point is inside this region */
650 if (vx >= r->x && vx < r->x+r->width &&
651 vy >= r->y && vy < r->y+r->height)
653 /* reposition the touch within the area */
654 vx -= r->x;
655 vy -= r->y;
657 switch(r->type)
659 case WPS_TOUCHREGION_ACTION:
660 if ((repeated && r->repeat) || (released && !r->repeat))
662 last_action = r->action;
663 return r->action;
665 break;
666 case WPS_TOUCHREGION_SCROLLBAR:
667 if(r->width > r->height)
668 /* landscape */
669 wps_state.id3->elapsed = (vx *
670 wps_state.id3->length) / r->width;
671 else
672 /* portrait */
673 wps_state.id3->elapsed = (vy *
674 wps_state.id3->length) / r->height;
676 if (!wps_state.paused)
677 #if (CONFIG_CODEC == SWCODEC)
678 audio_pre_ff_rewind();
679 #else
680 audio_pause();
681 #endif
682 audio_ff_rewind(wps_state.id3->elapsed);
683 #if (CONFIG_CODEC != SWCODEC)
684 if (!wps_state.paused)
685 audio_resume();
686 #endif
687 break;
688 case WPS_TOUCHREGION_VOLUME:
690 const int min_vol = sound_min(SOUND_VOLUME);
691 const int max_vol = sound_max(SOUND_VOLUME);
692 if(r->width > r->height)
693 /* landscape */
694 global_settings.volume = (vx *
695 (max_vol - min_vol)) / r->width;
696 else
697 /* portrait */
698 global_settings.volume = (vy *
699 (max_vol-min_vol)) / r->height;
701 global_settings.volume += min_vol;
702 setvol();
703 return ACTION_REDRAW;
708 regions = regions->next;
711 if ((last_action == ACTION_WPS_SEEKBACK || last_action == ACTION_WPS_SEEKFWD))
712 return ACTION_WPS_STOPSEEK;
713 last_action = ACTION_TOUCHSCREEN;
714 return ACTION_TOUCHSCREEN;
716 #endif
717 /* The WPS can be left in two ways:
718 * a) call a function, which draws over the wps. In this case, the wps
719 * will be still active (i.e. the below function didn't return)
720 * b) return with a value evaluated by root_menu.c, in this case the wps
721 * is really left, and root_menu will handle the next screen
723 * In either way, call gwps_leave_wps(), in order to restore the correct
724 * "main screen" backdrops and statusbars
726 long gui_wps_show(void)
728 long button = 0;
729 bool restore = true;
730 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
731 bool exit = false;
732 bool bookmark = false;
733 bool update = false;
734 bool vol_changed = false;
735 int i;
736 long last_left = 0, last_right = 0;
738 #ifdef HAVE_LCD_CHARCELLS
739 status_set_audio(true);
740 status_set_param(false);
741 #endif
743 #ifdef AB_REPEAT_ENABLE
744 ab_repeat_init();
745 ab_reset_markers();
746 #endif
747 wps_state_init();
749 while ( 1 )
751 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
753 /* did someone else (i.e power thread) change audio pause mode? */
754 if (wps_state.paused != audio_paused) {
755 wps_state.paused = audio_paused;
757 /* if another thread paused audio, we are probably in car mode,
758 about to shut down. lets save the settings. */
759 if (wps_state.paused) {
760 settings_save();
761 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
762 call_storage_idle_notifys(true);
763 #endif
766 #ifdef HAVE_LCD_BITMAP
767 /* when the peak meter is enabled we want to have a
768 few extra updates to make it look smooth. On the
769 other hand we don't want to waste energy if it
770 isn't displayed */
771 bool pm=false;
772 FOR_NB_SCREENS(i)
774 if(gui_wps[i].data->peak_meter_enabled)
775 pm = true;
778 if (pm) {
779 long next_refresh = current_tick;
780 long next_big_refresh = current_tick + HZ / 5;
781 button = BUTTON_NONE;
782 while (TIME_BEFORE(current_tick, next_big_refresh)) {
783 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_NOBLOCK);
784 /* check for restore to not let the peakmeter delay the
785 * initial draw of the wps, don't delay handling of button
786 * presses either */
787 if (button != ACTION_NONE || restore) {
788 break;
790 peak_meter_peek();
791 sleep(0); /* Sleep until end of current tick. */
793 if (TIME_AFTER(current_tick, next_refresh)) {
794 FOR_NB_SCREENS(i)
796 if(gui_wps[i].data->peak_meter_enabled)
797 skin_update(&gui_wps[i], WPS_REFRESH_PEAK_METER);
798 next_refresh += HZ / PEAK_METER_FPS;
805 /* The peak meter is disabled
806 -> no additional screen updates needed */
807 else
808 #endif
809 { /* 1 is the minimum timeout which lets other threads run.
810 * audio thread (apprently) needs to run before displaying the wps
811 * or bad things happen with regards to cuesheet
812 * (probably a race condition, on sh at least) */
813 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,
814 restore ? 1 : HZ/5);
817 /* Exit if audio has stopped playing. This happens e.g. at end of
818 playlist or if using the sleep timer. */
819 if (!(audio_status() & AUDIO_STATUS_PLAY))
820 exit = true;
821 #ifdef HAVE_TOUCHSCREEN
822 if (button == ACTION_TOUCHSCREEN)
823 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
824 #endif
825 /* The iPods/X5/M5 use a single button for the A-B mode markers,
826 defined as ACTION_WPSAB_SINGLE in their config files. */
827 #ifdef ACTION_WPSAB_SINGLE
828 if (!global_settings.party_mode && ab_repeat_mode_enabled())
830 static int wps_ab_state = 0;
831 if (button == ACTION_WPSAB_SINGLE)
833 switch (wps_ab_state)
835 case 0: /* set the A spot */
836 button = ACTION_WPS_ABSETA_PREVDIR;
837 break;
838 case 1: /* set the B spot */
839 button = ACTION_WPS_ABSETB_NEXTDIR;
840 break;
841 case 2:
842 button = ACTION_WPS_ABRESET;
843 break;
845 wps_ab_state = (wps_ab_state+1) % 3;
848 #endif
849 switch(button)
851 case ACTION_WPS_CONTEXT:
853 gwps_leave_wps();
854 /* if music is stopped in the context menu we want to exit the wps */
855 if (onplay(wps_state.id3->path,
856 FILE_ATTR_AUDIO, CONTEXT_WPS) == ONPLAY_MAINMENU
857 || !audio_status())
858 return GO_TO_ROOT;
859 restore = true;
861 break;
863 case ACTION_WPS_BROWSE:
864 #ifdef HAVE_LCD_CHARCELLS
865 status_set_record(false);
866 status_set_audio(false);
867 #endif
868 gwps_leave_wps();
869 return GO_TO_PREVIOUS_BROWSER;
870 break;
872 /* play/pause */
873 case ACTION_WPS_PLAY:
874 if (global_settings.party_mode)
875 break;
876 if ( wps_state.paused )
878 wps_state.paused = false;
879 if ( global_settings.fade_on_stop )
880 fade(true, true);
881 else
882 audio_resume();
884 else
886 wps_state.paused = true;
887 if ( global_settings.fade_on_stop )
888 fade(false, true);
889 else
890 audio_pause();
891 settings_save();
892 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
893 call_storage_idle_notifys(true); /* make sure resume info is saved */
894 #endif
896 break;
898 case ACTION_WPS_VOLUP:
899 global_settings.volume++;
900 vol_changed = true;
901 break;
902 case ACTION_WPS_VOLDOWN:
903 global_settings.volume--;
904 vol_changed = true;
905 break;
906 /* fast forward
907 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
908 case ACTION_WPS_SEEKFWD:
909 if (global_settings.party_mode)
910 break;
911 if (current_tick -last_right < HZ)
913 if (wps_state.id3->cuesheet)
915 audio_next();
917 else
919 change_dir(1);
922 else
923 ffwd_rew(ACTION_WPS_SEEKFWD);
924 last_right = last_left = 0;
925 break;
926 /* fast rewind
927 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
928 case ACTION_WPS_SEEKBACK:
929 if (global_settings.party_mode)
930 break;
931 if (current_tick -last_left < HZ)
933 if (wps_state.id3->cuesheet)
935 if (!wps_state.paused)
936 #if (CONFIG_CODEC == SWCODEC)
937 audio_pre_ff_rewind();
938 #else
939 audio_pause();
940 #endif
941 audio_ff_rewind(0);
943 else
945 change_dir(-1);
948 else
949 ffwd_rew(ACTION_WPS_SEEKBACK);
950 last_left = last_right = 0;
951 break;
953 /* prev / restart */
954 case ACTION_WPS_SKIPPREV:
955 if (global_settings.party_mode)
956 break;
957 last_left = current_tick;
958 #ifdef AB_REPEAT_ENABLE
959 /* if we're in A/B repeat mode and the current position
960 is past the A marker, jump back to the A marker... */
961 if ( ab_repeat_mode_enabled() )
963 if ( ab_after_A_marker(wps_state.id3->elapsed) )
965 ab_jump_to_A_marker();
966 break;
967 #if (AB_REPEAT_ENABLE == 2)
968 } else {
969 ab_reset_markers();
970 #endif
973 else
974 /* ...otherwise, do it normally */
975 #endif
976 play_hop(-1);
977 break;
979 /* next
980 OR if skip length set, hop by predetermined amount. */
981 case ACTION_WPS_SKIPNEXT:
982 if (global_settings.party_mode)
983 break;
984 last_right = current_tick;
985 #ifdef AB_REPEAT_ENABLE
986 /* if we're in A/B repeat mode and the current position is
987 before the A marker, jump to the A marker... */
988 if ( ab_repeat_mode_enabled() )
990 if ( ab_before_A_marker(wps_state.id3->elapsed) )
992 ab_jump_to_A_marker();
993 break;
994 #if (AB_REPEAT_ENABLE == 2)
995 } else {
996 ab_reset_markers();
997 #endif
1000 else
1001 /* ...otherwise, do it normally */
1002 #endif
1003 play_hop(1);
1004 break;
1005 /* next / prev directories */
1006 /* and set A-B markers if in a-b mode */
1007 case ACTION_WPS_ABSETB_NEXTDIR:
1008 if (global_settings.party_mode)
1009 break;
1010 #if defined(AB_REPEAT_ENABLE)
1011 if (ab_repeat_mode_enabled())
1013 ab_set_B_marker(wps_state.id3->elapsed);
1014 ab_jump_to_A_marker();
1016 else
1017 #endif
1019 change_dir(1);
1021 break;
1022 case ACTION_WPS_ABSETA_PREVDIR:
1023 if (global_settings.party_mode)
1024 break;
1025 #if defined(AB_REPEAT_ENABLE)
1026 if (ab_repeat_mode_enabled())
1027 ab_set_A_marker(wps_state.id3->elapsed);
1028 else
1029 #endif
1031 change_dir(-1);
1033 break;
1034 /* menu key functions */
1035 case ACTION_WPS_MENU:
1036 gwps_leave_wps();
1037 return GO_TO_ROOT;
1038 break;
1041 #ifdef HAVE_QUICKSCREEN
1042 case ACTION_WPS_QUICKSCREEN:
1044 gwps_leave_wps();
1045 if (quick_screen_quick(button))
1046 return GO_TO_ROOT;
1047 restore = true;
1049 break;
1050 #endif /* HAVE_QUICKSCREEN */
1052 /* screen settings */
1053 #ifdef BUTTON_F3
1054 case ACTION_F3:
1056 gwps_leave_wps();
1057 if (quick_screen_f3(BUTTON_F3))
1058 return GO_TO_ROOT;
1059 restore = true;
1061 break;
1062 #endif /* BUTTON_F3 */
1064 /* pitch screen */
1065 #ifdef HAVE_PITCHSCREEN
1066 case ACTION_WPS_PITCHSCREEN:
1068 gwps_leave_wps();
1069 if (1 == gui_syncpitchscreen_run())
1070 return GO_TO_ROOT;
1071 restore = true;
1073 break;
1074 #endif /* HAVE_PITCHSCREEN */
1076 #ifdef AB_REPEAT_ENABLE
1077 /* reset A&B markers */
1078 case ACTION_WPS_ABRESET:
1079 if (ab_repeat_mode_enabled())
1081 ab_reset_markers();
1082 update = true;
1084 break;
1085 #endif /* AB_REPEAT_ENABLE */
1087 /* stop and exit wps */
1088 case ACTION_WPS_STOP:
1089 if (global_settings.party_mode)
1090 break;
1091 bookmark = true;
1092 exit = true;
1093 break;
1095 case ACTION_WPS_ID3SCREEN:
1097 gwps_leave_wps();
1098 if (browse_id3())
1099 return GO_TO_ROOT;
1100 restore = true;
1102 break;
1103 #ifdef HAVE_TOUCHSCREEN
1104 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1106 global_settings.playlist_shuffle =
1107 !global_settings.playlist_shuffle;
1108 #if CONFIG_CODEC == SWCODEC
1109 dsp_set_replaygain();
1110 #endif
1111 if (global_settings.playlist_shuffle)
1112 playlist_randomise(NULL, current_tick, true);
1113 else
1114 playlist_sort(NULL, true);
1116 break;
1117 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1119 const struct settings_list *rep_setting =
1120 find_setting(&global_settings.repeat_mode, NULL);
1121 option_select_next_val(rep_setting, false, true);
1122 audio_flush_and_reload_tracks();
1124 break;
1125 #endif /* HAVE_TOUCHSCREEN */
1126 /* this case is used by the softlock feature
1127 * it requests a full update here */
1128 case ACTION_REDRAW:
1129 wps_sync_data.do_full_update = true;
1130 break;
1131 case ACTION_NONE: /* Timeout, do a partial update */
1132 update = true;
1133 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1134 break;
1135 #ifdef HAVE_RECORDING
1136 case ACTION_WPS_REC:
1137 exit = true;
1138 break;
1139 #endif
1140 case SYS_POWEROFF:
1141 default_event_handler(SYS_POWEROFF);
1142 break;
1143 case ACTION_WPS_VIEW_PLAYLIST:
1144 gwps_leave_wps();
1145 if (playlist_viewer()) /* true if USB connected */
1146 return GO_TO_ROOT;
1147 restore = true;
1148 break;
1149 default:
1150 if(default_event_handler(button) == SYS_USB_CONNECTED)
1152 gwps_leave_wps();
1153 return GO_TO_ROOT;
1155 update = true;
1156 break;
1159 if (vol_changed)
1161 bool res = false;
1162 vol_changed = false;
1163 setvol();
1164 FOR_NB_SCREENS(i)
1166 if(update_onvol_change(&gui_wps[i]))
1167 res = true;
1169 if (res) {
1170 restore = true;
1171 restoretimer = RESTORE_WPS_NEXT_SECOND;
1176 if (restore &&
1177 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1178 TIME_AFTER(current_tick, restoretimer)))
1180 restore = false;
1181 restoretimer = RESTORE_WPS_INSTANTLY;
1182 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1183 add_event(LCD_EVENT_ACTIVATION, false, wps_lcd_activation_hook);
1184 #endif
1185 /* we remove the update delay since it's not very usable in the wps,
1186 * e.g. during volume changing or ffwd/rewind */
1187 sb_skin_set_update_delay(0);
1188 wps_sync_data.do_full_update = update = false;
1189 gwps_enter_wps();
1191 else if (wps_sync_data.do_full_update || update)
1193 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1194 gwps_caption_backlight(&wps_state);
1195 #endif
1196 FOR_NB_SCREENS(i)
1198 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1199 /* currently, all remotes are readable without backlight
1200 * so still update those */
1201 if (lcd_active() || (i != SCREEN_MAIN))
1202 #endif
1204 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
1207 wps_sync_data.do_full_update = false;
1208 update = false;
1211 if (exit) {
1212 #ifdef HAVE_LCD_CHARCELLS
1213 status_set_record(false);
1214 status_set_audio(false);
1215 #endif
1216 if (global_settings.fade_on_stop)
1217 fade(false, true);
1219 if (bookmark)
1220 bookmark_autobookmark();
1221 audio_stop();
1222 #ifdef AB_REPEAT_ENABLE
1223 ab_reset_markers();
1224 #endif
1225 gwps_leave_wps();
1226 #ifdef HAVE_RECORDING
1227 if (button == ACTION_WPS_REC)
1228 return GO_TO_RECSCREEN;
1229 #endif
1230 if (global_settings.browse_current)
1231 return GO_TO_PREVIOUS_BROWSER;
1232 return GO_TO_PREVIOUS;
1235 if (button && !IS_SYSEVENT(button) )
1236 storage_spin();
1238 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1241 /* this is called from the playback thread so NO DRAWING! */
1242 static void track_changed_callback(void *param)
1244 wps_state.id3 = (struct mp3entry*)param;
1245 wps_state.nid3 = audio_next_track();
1246 if (wps_state.id3->cuesheet)
1248 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1249 cue_spoof_id3(wps_state.id3->cuesheet, wps_state.id3);
1251 wps_sync_data.do_full_update = true;
1253 static void nextid3available_callback(void* param)
1255 (void)param;
1256 wps_state.nid3 = audio_next_track();
1257 wps_sync_data.do_full_update = true;
1261 static void wps_state_init(void)
1263 wps_state.ff_rewind = false;
1264 wps_state.paused = false;
1265 if(audio_status() & AUDIO_STATUS_PLAY)
1267 wps_state.id3 = audio_current_track();
1268 wps_state.nid3 = audio_next_track();
1270 else
1272 wps_state.id3 = NULL;
1273 wps_state.nid3 = NULL;
1275 /* We'll be updating due to restore initialized with true */
1276 wps_sync_data.do_full_update = false;
1277 /* add the WPS track event callbacks */
1278 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1279 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1283 void gui_sync_wps_init(void)
1285 int i;
1286 FOR_NB_SCREENS(i)
1288 #ifdef HAVE_ALBUMART
1289 wps_datas[i].albumart = NULL;
1290 wps_datas[i].playback_aa_slot = -1;
1291 #endif
1292 gui_wps[i].data = &wps_datas[i];
1293 gui_wps[i].display = &screens[i];
1294 /* Currently no seperate wps_state needed/possible
1295 so use the only available ( "global" ) one */
1296 gui_wps[i].state = &wps_state;
1297 gui_wps[i].display->backdrop_unload(BACKDROP_SKIN_WPS);
1298 /* must point to the same struct for both screens */
1299 gui_wps[i].sync_data = &wps_sync_data;
1304 #ifdef IPOD_ACCESSORY_PROTOCOL
1305 bool is_wps_fading(void)
1307 return wps_state.is_fading;
1310 int wps_get_ff_rewind_count(void)
1312 return wps_state.ff_rewind_count;
1314 #endif