fix FS#10870 - slightly change the viewportmanager theme undo-er to be able to force...
[kugel-rb.git] / apps / gui / wps.c
blobf6bcd78457afe338c3e119a3b6c7ec0dcf0d527a
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);
92 #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps"
93 #ifdef HAVE_REMOTE_LCD
94 #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps"
95 #define DEFAULT_WPS(screen) ((screen) == SCREEN_MAIN ? \
96 WPS_DEFAULTCFG:RWPS_DEFAULTCFG)
97 #else
98 #define DEFAULT_WPS(screen) (WPS_DEFAULTCFG)
99 #endif
101 void wps_data_load(enum screen_type screen, const char *buf, bool isfile)
103 bool loaded_ok;
105 screens[screen].backdrop_unload(BACKDROP_SKIN_WPS);
107 #ifndef __PCTOOL__
109 * Hardcode loading WPS_DEFAULTCFG to cause a reset ideally this
110 * wants to be a virtual file. Feel free to modify dirbrowse()
111 * if you're feeling brave.
114 if (buf && ! strcmp(buf, DEFAULT_WPS(screen)) )
116 #ifdef HAVE_REMOTE_LCD
117 if (screen == SCREEN_REMOTE)
118 global_settings.rwps_file[0] = '\0';
119 else
120 #endif
121 global_settings.wps_file[0] = '\0';
122 buf = NULL;
125 #endif /* __PCTOOL__ */
127 loaded_ok = buf && skin_data_load(screen, gui_wps[screen].data, buf, isfile);
129 if (!loaded_ok) /* load the hardcoded default */
131 char *skin_buf[NB_SCREENS] = {
132 #ifdef HAVE_LCD_BITMAP
133 "%s%?it<%?in<%in. |>%it|%fn>\n"
134 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
135 "%s%?id<%id|%?d1<%d1|(root)>> %?iy<(%iy)|>\n\n"
136 "%al%pc/%pt%ar[%pp:%pe]\n"
137 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
138 "%pb\n%pm\n",
139 #else
140 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d2> - %?id<%id|%d1>\n"
141 "%pc%?ps<*|/>%pt\n",
142 #endif
143 #ifdef HAVE_REMOTE_LCD
144 "%s%?ia<%ia|%?d2<%d2|(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], WPS_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], WPS_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, WPS_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 bool ffwd_rew(int button)
228 unsigned int step = 0; /* current ff/rewind step */
229 unsigned int max_step = 0; /* maximum ff/rewind step */
230 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
231 int direction = -1; /* forward=1 or backward=-1 */
232 bool exit = false;
233 bool usb = false;
234 int i = 0;
235 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
237 if (button == ACTION_NONE)
239 status_set_ffmode(0);
240 return usb;
242 while (!exit)
244 switch ( button )
246 case ACTION_WPS_SEEKFWD:
247 direction = 1;
248 case ACTION_WPS_SEEKBACK:
249 if (wps_state.ff_rewind)
251 if (direction == 1)
253 /* fast forwarding, calc max step relative to end */
254 max_step = (wps_state.id3->length -
255 (wps_state.id3->elapsed +
256 ff_rewind_count)) *
257 FF_REWIND_MAX_PERCENT / 100;
259 else
261 /* rewinding, calc max step relative to start */
262 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
263 FF_REWIND_MAX_PERCENT / 100;
266 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
268 if (step > max_step)
269 step = max_step;
271 ff_rewind_count += step * direction;
273 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
274 step += step >> ff_rw_accel;
276 else
278 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
279 wps_state.id3 && wps_state.id3->length )
281 if (!wps_state.paused)
282 #if (CONFIG_CODEC == SWCODEC)
283 audio_pre_ff_rewind();
284 #else
285 audio_pause();
286 #endif
287 #if CONFIG_KEYPAD == PLAYER_PAD
288 FOR_NB_SCREENS(i)
289 gui_wps[i].display->stop_scroll();
290 #endif
291 if (direction > 0)
292 status_set_ffmode(STATUS_FASTFORWARD);
293 else
294 status_set_ffmode(STATUS_FASTBACKWARD);
296 wps_state.ff_rewind = true;
298 step = 1000 * global_settings.ff_rewind_min_step;
300 else
301 break;
304 if (direction > 0) {
305 if ((wps_state.id3->elapsed + ff_rewind_count) >
306 wps_state.id3->length)
307 ff_rewind_count = wps_state.id3->length -
308 wps_state.id3->elapsed;
310 else {
311 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
312 ff_rewind_count = -wps_state.id3->elapsed;
315 /* set the wps state ff_rewind_count so the progess info
316 displays corectly */
317 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
318 ff_rewind_count:-ff_rewind_count;
319 FOR_NB_SCREENS(i)
321 skin_update(&gui_wps[i],
322 WPS_REFRESH_PLAYER_PROGRESS |
323 WPS_REFRESH_DYNAMIC);
326 break;
328 case ACTION_WPS_STOPSEEK:
329 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
330 audio_ff_rewind(wps_state.id3->elapsed);
331 wps_state.ff_rewind_count = 0;
332 wps_state.ff_rewind = false;
333 status_set_ffmode(0);
334 #if (CONFIG_CODEC != SWCODEC)
335 if (!wps_state.paused)
336 audio_resume();
337 #endif
338 #ifdef HAVE_LCD_CHARCELLS
339 FOR_NB_SCREENS(i)
340 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
341 #endif
342 exit = true;
343 break;
345 default:
346 if(default_event_handler(button) == SYS_USB_CONNECTED) {
347 status_set_ffmode(0);
348 usb = true;
349 exit = true;
351 break;
353 if (!exit)
355 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
356 #ifdef HAVE_TOUCHSCREEN
357 if (button == ACTION_TOUCHSCREEN)
358 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
359 #endif
362 return usb;
366 void display_keylock_text(bool locked)
368 int i;
369 FOR_NB_SCREENS(i)
370 gui_wps[i].display->stop_scroll();
372 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
378 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
379 static void gwps_caption_backlight(struct wps_state *state)
381 if (state && state->id3)
383 #ifdef HAVE_BACKLIGHT
384 if (global_settings.caption_backlight)
386 /* turn on backlight n seconds before track ends, and turn it off n
387 seconds into the new track. n == backlight_timeout, or 5s */
388 int n = global_settings.backlight_timeout * 1000;
390 if ( n < 1000 )
391 n = 5000; /* use 5s if backlight is always on or off */
393 if (((state->id3->elapsed < 1000) ||
394 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
395 (state->paused == false))
396 backlight_on();
398 #endif
399 #ifdef HAVE_REMOTE_LCD
400 if (global_settings.remote_caption_backlight)
402 /* turn on remote backlight n seconds before track ends, and turn it
403 off n seconds into the new track. n == remote_backlight_timeout,
404 or 5s */
405 int n = global_settings.remote_backlight_timeout * 1000;
407 if ( n < 1000 )
408 n = 5000; /* use 5s if backlight is always on or off */
410 if (((state->id3->elapsed < 1000) ||
411 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
412 (state->paused == false))
413 remote_backlight_on();
415 #endif
418 #endif
421 static void change_dir(int direction)
423 if (global_settings.prevent_skip)
424 return;
426 if (direction < 0)
427 audio_prev_dir();
428 else if (direction > 0)
429 audio_next_dir();
430 /* prevent the next dir to immediatly start being ffw'd */
431 action_wait_for_release();
434 static void prev_track(unsigned long skip_thresh)
436 if (wps_state.id3->elapsed < skip_thresh)
438 audio_prev();
439 return;
441 else
443 if (wps_state.id3->cuesheet)
445 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
446 return;
449 if (!wps_state.paused)
450 #if (CONFIG_CODEC == SWCODEC)
451 audio_pre_ff_rewind();
452 #else
453 audio_pause();
454 #endif
456 audio_ff_rewind(0);
458 #if (CONFIG_CODEC != SWCODEC)
459 if (!wps_state.paused)
460 audio_resume();
461 #endif
465 static void next_track(void)
467 /* take care of if we're playing a cuesheet */
468 if (wps_state.id3->cuesheet)
470 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
472 /* if the result was false, then we really want
473 to skip to the next track */
474 return;
478 audio_next();
481 static void play_hop(int direction)
483 long step = global_settings.skip_length*1000;
484 long elapsed = wps_state.id3->elapsed;
485 long remaining = wps_state.id3->length - elapsed;
487 if (step < 0)
489 if (direction < 0)
491 prev_track(DEFAULT_SKIP_TRESH);
492 return;
494 else if (remaining < DEFAULT_SKIP_TRESH*2)
496 next_track();
497 return;
499 else
500 elapsed += (remaining - DEFAULT_SKIP_TRESH*2);
502 else if (!global_settings.prevent_skip &&
503 (!step ||
504 (direction > 0 && step >= remaining) ||
505 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
506 { /* Do normal track skipping */
507 if (direction > 0)
508 next_track();
509 else if (direction < 0)
510 prev_track(DEFAULT_SKIP_TRESH);
511 return;
513 else if (direction == 1 && step >= remaining)
515 #if CONFIG_CODEC == SWCODEC
516 if(global_settings.beep)
517 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
518 #endif
519 return;
521 else if ((direction == -1 && elapsed < step))
523 elapsed = 0;
525 else
527 elapsed += step * direction;
529 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
531 #if (CONFIG_CODEC == SWCODEC)
532 audio_pre_ff_rewind();
533 #else
534 audio_pause();
535 #endif
537 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
538 #if (CONFIG_CODEC != SWCODEC)
539 if (!wps_state.paused)
540 audio_resume();
541 #endif
545 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
547 * If the user is unable to see the wps, because the display is deactivated,
548 * we suppress updates until the wps is activated again (the lcd driver will
549 * call this hook to issue an instant update)
550 * */
551 static void wps_lcd_activation_hook(void *param)
553 (void)param;
554 wps_sync_data.do_full_update = true;
555 /* force timeout in wps main loop, so that the update is instantly */
556 queue_post(&button_queue, BUTTON_NONE, 0);
558 #endif
560 static void gwps_leave_wps(void)
562 int i;
564 FOR_NB_SCREENS(i)
566 gui_wps[i].display->stop_scroll();
567 gui_wps[i].display->backdrop_show(BACKDROP_MAIN);
569 bool draw = false;
570 #ifdef HAVE_LCD_BITMAP
571 if (gui_wps[i].data->wps_sb_tag)
572 draw = gui_wps[i].data->show_sb_on_wps;
573 else if (statusbar_position(i) != STATUSBAR_OFF)
574 draw = true;
575 #endif
576 viewportmanager_theme_undo(i, draw);
580 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
581 /* Play safe and unregister the hook */
582 remove_event(LCD_EVENT_ACTIVATION, wps_lcd_activation_hook);
583 #endif
584 /* unhandle statusbar update delay */
585 sb_skin_set_update_delay(DEFAULT_UPDATE_DELAY);
589 * display the wps on entering or restoring */
590 static void gwps_enter_wps(void)
592 int i;
593 FOR_NB_SCREENS(i)
595 struct gui_wps *gwps = &gui_wps[i];
596 struct screen *display = gwps->display;
597 #ifdef HAVE_LCD_BITMAP
598 bool draw = false;
599 if (gui_wps[i].data->wps_sb_tag)
600 draw = gui_wps[i].data->show_sb_on_wps;
601 else if (statusbar_position(i) != STATUSBAR_OFF)
602 draw = true;
603 #endif
604 viewportmanager_theme_enable(i, draw, NULL);
606 display->stop_scroll();
607 /* Update the values in the first (default) viewport - in case the user
608 has modified the statusbar or colour settings */
609 #if LCD_DEPTH > 1
610 if (display->depth > 1)
612 struct viewport *vp = &find_viewport(VP_DEFAULT_LABEL, gwps->data)->vp;
613 vp->fg_pattern = display->get_foreground();
614 vp->bg_pattern = display->get_background();
616 #endif
617 skin_update(gwps, WPS_REFRESH_ALL);
621 #ifdef HAVE_TOUCHSCREEN
622 int wps_get_touchaction(struct wps_data *data)
624 short x,y;
625 short vx, vy;
626 int type = action_get_touchscreen_press(&x, &y);
627 static int last_action = ACTION_NONE;
628 struct touchregion *r;
629 bool repeated = (type == BUTTON_REPEAT);
630 bool released = (type == BUTTON_REL);
631 struct skin_token_list *regions = data->touchregions;
632 while (regions)
634 r = (struct touchregion *)regions->token->value.data;
635 /* make sure this region's viewport is visible */
636 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
638 regions = regions->next;
639 continue;
641 /* check if it's inside this viewport */
642 if (viewport_point_within_vp(&(r->wvp->vp), x, y))
643 { /* reposition the touch inside the viewport since touchregions
644 * are relative to a preceding viewport */
645 vx = x - r->wvp->vp.x;
646 vy = y - r->wvp->vp.y;
647 /* now see if the point is inside this region */
648 if (vx >= r->x && vx < r->x+r->width &&
649 vy >= r->y && vy < r->y+r->height)
651 /* reposition the touch within the area */
652 vx -= r->x;
653 vy -= r->y;
655 switch(r->type)
657 case WPS_TOUCHREGION_ACTION:
658 if ((repeated && r->repeat) || (released && !r->repeat))
660 last_action = r->action;
661 return r->action;
663 break;
664 case WPS_TOUCHREGION_SCROLLBAR:
665 if(r->width > r->height)
666 /* landscape */
667 wps_state.id3->elapsed = (vx *
668 wps_state.id3->length) / r->width;
669 else
670 /* portrait */
671 wps_state.id3->elapsed = (vy *
672 wps_state.id3->length) / r->height;
674 if (!wps_state.paused)
675 #if (CONFIG_CODEC == SWCODEC)
676 audio_pre_ff_rewind();
677 #else
678 audio_pause();
679 #endif
680 audio_ff_rewind(wps_state.id3->elapsed);
681 #if (CONFIG_CODEC != SWCODEC)
682 if (!wps_state.paused)
683 audio_resume();
684 #endif
685 break;
686 case WPS_TOUCHREGION_VOLUME:
688 const int min_vol = sound_min(SOUND_VOLUME);
689 const int max_vol = sound_max(SOUND_VOLUME);
690 if(r->width > r->height)
691 /* landscape */
692 global_settings.volume = (vx *
693 (max_vol - min_vol)) / r->width;
694 else
695 /* portrait */
696 global_settings.volume = (vy *
697 (max_vol-min_vol)) / r->height;
699 global_settings.volume += min_vol;
700 setvol();
701 return ACTION_REDRAW;
706 regions = regions->next;
709 if ((last_action == ACTION_WPS_SEEKBACK || last_action == ACTION_WPS_SEEKFWD))
710 return ACTION_WPS_STOPSEEK;
711 last_action = ACTION_TOUCHSCREEN;
712 return ACTION_TOUCHSCREEN;
714 #endif
715 /* The WPS can be left in two ways:
716 * a) call a function, which draws over the wps. In this case, the wps
717 * will be still active (i.e. the below function didn't return)
718 * b) return with a value evaluated by root_menu.c, in this case the wps
719 * is really left, and root_menu will handle the next screen
721 * In either way, call gwps_leave_wps(), in order to restore the correct
722 * "main screen" backdrops and statusbars
724 long gui_wps_show(void)
726 long button = 0;
727 bool restore = true;
728 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
729 bool exit = false;
730 bool bookmark = false;
731 bool update = false;
732 bool vol_changed = false;
733 int i;
734 long last_left = 0, last_right = 0;
736 #ifdef HAVE_LCD_CHARCELLS
737 status_set_audio(true);
738 status_set_param(false);
739 #endif
741 #ifdef AB_REPEAT_ENABLE
742 ab_repeat_init();
743 ab_reset_markers();
744 #endif
745 wps_state_init();
747 while ( 1 )
749 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
751 /* did someone else (i.e power thread) change audio pause mode? */
752 if (wps_state.paused != audio_paused) {
753 wps_state.paused = audio_paused;
755 /* if another thread paused audio, we are probably in car mode,
756 about to shut down. lets save the settings. */
757 if (wps_state.paused) {
758 settings_save();
759 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
760 call_storage_idle_notifys(true);
761 #endif
764 #ifdef HAVE_LCD_BITMAP
765 /* when the peak meter is enabled we want to have a
766 few extra updates to make it look smooth. On the
767 other hand we don't want to waste energy if it
768 isn't displayed */
769 bool pm=false;
770 FOR_NB_SCREENS(i)
772 if(gui_wps[i].data->peak_meter_enabled)
773 pm = true;
776 if (pm) {
777 long next_refresh = current_tick;
778 long next_big_refresh = current_tick + HZ / 5;
779 button = BUTTON_NONE;
780 while (TIME_BEFORE(current_tick, next_big_refresh)) {
781 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_NOBLOCK);
782 /* check for restore to not let the peakmeter delay the
783 * initial draw of the wps, don't delay handling of button
784 * presses either */
785 if (button != ACTION_NONE || restore) {
786 break;
788 peak_meter_peek();
789 sleep(0); /* Sleep until end of current tick. */
791 if (TIME_AFTER(current_tick, next_refresh)) {
792 FOR_NB_SCREENS(i)
794 if(gui_wps[i].data->peak_meter_enabled)
795 skin_update(&gui_wps[i], WPS_REFRESH_PEAK_METER);
796 next_refresh += HZ / PEAK_METER_FPS;
803 /* The peak meter is disabled
804 -> no additional screen updates needed */
805 else
806 #endif
807 { /* 1 is the minimum timeout which lets other threads run.
808 * audio thread (apprently) needs to run before displaying the wps
809 * or bad things happen with regards to cuesheet
810 * (probably a race condition, on sh at least) */
811 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,
812 restore ? 1 : HZ/5);
815 /* Exit if audio has stopped playing. This happens e.g. at end of
816 playlist or if using the sleep timer. */
817 if (!(audio_status() & AUDIO_STATUS_PLAY))
818 exit = true;
819 #ifdef HAVE_TOUCHSCREEN
820 if (button == ACTION_TOUCHSCREEN)
821 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
822 #endif
823 /* The iPods/X5/M5 use a single button for the A-B mode markers,
824 defined as ACTION_WPSAB_SINGLE in their config files. */
825 #ifdef ACTION_WPSAB_SINGLE
826 if (!global_settings.party_mode && ab_repeat_mode_enabled())
828 static int wps_ab_state = 0;
829 if (button == ACTION_WPSAB_SINGLE)
831 switch (wps_ab_state)
833 case 0: /* set the A spot */
834 button = ACTION_WPS_ABSETA_PREVDIR;
835 break;
836 case 1: /* set the B spot */
837 button = ACTION_WPS_ABSETB_NEXTDIR;
838 break;
839 case 2:
840 button = ACTION_WPS_ABRESET;
841 break;
843 wps_ab_state = (wps_ab_state+1) % 3;
846 #endif
847 switch(button)
849 case ACTION_WPS_CONTEXT:
851 gwps_leave_wps();
852 /* if music is stopped in the context menu we want to exit the wps */
853 if (onplay(wps_state.id3->path,
854 FILE_ATTR_AUDIO, CONTEXT_WPS) == ONPLAY_MAINMENU
855 || !audio_status())
856 return GO_TO_ROOT;
857 restore = true;
859 break;
861 case ACTION_WPS_BROWSE:
862 #ifdef HAVE_LCD_CHARCELLS
863 status_set_record(false);
864 status_set_audio(false);
865 #endif
866 gwps_leave_wps();
867 return GO_TO_PREVIOUS_BROWSER;
868 break;
870 /* play/pause */
871 case ACTION_WPS_PLAY:
872 if (global_settings.party_mode)
873 break;
874 if ( wps_state.paused )
876 wps_state.paused = false;
877 if ( global_settings.fade_on_stop )
878 fade(true, true);
879 else
880 audio_resume();
882 else
884 wps_state.paused = true;
885 if ( global_settings.fade_on_stop )
886 fade(false, true);
887 else
888 audio_pause();
889 settings_save();
890 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
891 call_storage_idle_notifys(true); /* make sure resume info is saved */
892 #endif
894 break;
896 case ACTION_WPS_VOLUP:
897 global_settings.volume++;
898 vol_changed = true;
899 break;
900 case ACTION_WPS_VOLDOWN:
901 global_settings.volume--;
902 vol_changed = true;
903 break;
904 /* fast forward
905 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
906 case ACTION_WPS_SEEKFWD:
907 if (global_settings.party_mode)
908 break;
909 if (current_tick -last_right < HZ)
911 if (wps_state.id3->cuesheet)
913 audio_next();
915 else
917 change_dir(1);
920 else
921 ffwd_rew(ACTION_WPS_SEEKFWD);
922 last_right = last_left = 0;
923 break;
924 /* fast rewind
925 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
926 case ACTION_WPS_SEEKBACK:
927 if (global_settings.party_mode)
928 break;
929 if (current_tick -last_left < HZ)
931 if (wps_state.id3->cuesheet)
933 if (!wps_state.paused)
934 #if (CONFIG_CODEC == SWCODEC)
935 audio_pre_ff_rewind();
936 #else
937 audio_pause();
938 #endif
939 audio_ff_rewind(0);
941 else
943 change_dir(-1);
946 else
947 ffwd_rew(ACTION_WPS_SEEKBACK);
948 last_left = last_right = 0;
949 break;
951 /* prev / restart */
952 case ACTION_WPS_SKIPPREV:
953 if (global_settings.party_mode)
954 break;
955 last_left = current_tick;
956 #ifdef AB_REPEAT_ENABLE
957 /* if we're in A/B repeat mode and the current position
958 is past the A marker, jump back to the A marker... */
959 if ( ab_repeat_mode_enabled() )
961 if ( ab_after_A_marker(wps_state.id3->elapsed) )
963 ab_jump_to_A_marker();
964 break;
965 #if (AB_REPEAT_ENABLE == 2)
966 } else {
967 ab_reset_markers();
968 #endif
971 else
972 /* ...otherwise, do it normally */
973 #endif
974 play_hop(-1);
975 break;
977 /* next
978 OR if skip length set, hop by predetermined amount. */
979 case ACTION_WPS_SKIPNEXT:
980 if (global_settings.party_mode)
981 break;
982 last_right = current_tick;
983 #ifdef AB_REPEAT_ENABLE
984 /* if we're in A/B repeat mode and the current position is
985 before the A marker, jump to the A marker... */
986 if ( ab_repeat_mode_enabled() )
988 if ( ab_before_A_marker(wps_state.id3->elapsed) )
990 ab_jump_to_A_marker();
991 break;
992 #if (AB_REPEAT_ENABLE == 2)
993 } else {
994 ab_reset_markers();
995 #endif
998 else
999 /* ...otherwise, do it normally */
1000 #endif
1001 play_hop(1);
1002 break;
1003 /* next / prev directories */
1004 /* and set A-B markers if in a-b mode */
1005 case ACTION_WPS_ABSETB_NEXTDIR:
1006 if (global_settings.party_mode)
1007 break;
1008 #if defined(AB_REPEAT_ENABLE)
1009 if (ab_repeat_mode_enabled())
1011 ab_set_B_marker(wps_state.id3->elapsed);
1012 ab_jump_to_A_marker();
1014 else
1015 #endif
1017 change_dir(1);
1019 break;
1020 case ACTION_WPS_ABSETA_PREVDIR:
1021 if (global_settings.party_mode)
1022 break;
1023 #if defined(AB_REPEAT_ENABLE)
1024 if (ab_repeat_mode_enabled())
1025 ab_set_A_marker(wps_state.id3->elapsed);
1026 else
1027 #endif
1029 change_dir(-1);
1031 break;
1032 /* menu key functions */
1033 case ACTION_WPS_MENU:
1034 gwps_leave_wps();
1035 return GO_TO_ROOT;
1036 break;
1039 #ifdef HAVE_QUICKSCREEN
1040 case ACTION_WPS_QUICKSCREEN:
1042 gwps_leave_wps();
1043 if (quick_screen_quick(button))
1044 return GO_TO_ROOT;
1045 restore = true;
1047 break;
1048 #endif /* HAVE_QUICKSCREEN */
1050 /* screen settings */
1051 #ifdef BUTTON_F3
1052 case ACTION_F3:
1054 gwps_leave_wps();
1055 if (quick_screen_f3(BUTTON_F3))
1056 return GO_TO_ROOT;
1057 restore = true;
1059 break;
1060 #endif /* BUTTON_F3 */
1062 /* pitch screen */
1063 #ifdef HAVE_PITCHSCREEN
1064 case ACTION_WPS_PITCHSCREEN:
1066 gwps_leave_wps();
1067 if (1 == gui_syncpitchscreen_run())
1068 return GO_TO_ROOT;
1069 restore = true;
1071 break;
1072 #endif /* HAVE_PITCHSCREEN */
1074 #ifdef AB_REPEAT_ENABLE
1075 /* reset A&B markers */
1076 case ACTION_WPS_ABRESET:
1077 if (ab_repeat_mode_enabled())
1079 ab_reset_markers();
1080 update = true;
1082 break;
1083 #endif /* AB_REPEAT_ENABLE */
1085 /* stop and exit wps */
1086 case ACTION_WPS_STOP:
1087 if (global_settings.party_mode)
1088 break;
1089 bookmark = true;
1090 exit = true;
1091 break;
1093 case ACTION_WPS_ID3SCREEN:
1095 gwps_leave_wps();
1096 if (browse_id3())
1097 return GO_TO_ROOT;
1098 restore = true;
1100 break;
1101 #ifdef HAVE_TOUCHSCREEN
1102 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1104 global_settings.playlist_shuffle =
1105 !global_settings.playlist_shuffle;
1106 #if CONFIG_CODEC == SWCODEC
1107 dsp_set_replaygain();
1108 #endif
1109 if (global_settings.playlist_shuffle)
1110 playlist_randomise(NULL, current_tick, true);
1111 else
1112 playlist_sort(NULL, true);
1114 break;
1115 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1117 const struct settings_list *rep_setting =
1118 find_setting(&global_settings.repeat_mode, NULL);
1119 option_select_next_val(rep_setting, false, true);
1120 audio_flush_and_reload_tracks();
1122 break;
1123 #endif /* HAVE_TOUCHSCREEN */
1124 /* this case is used by the softlock feature
1125 * it requests a full update here */
1126 case ACTION_REDRAW:
1127 wps_sync_data.do_full_update = true;
1128 break;
1129 case ACTION_NONE: /* Timeout, do a partial update */
1130 update = true;
1131 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1132 break;
1133 #ifdef HAVE_RECORDING
1134 case ACTION_WPS_REC:
1135 exit = true;
1136 break;
1137 #endif
1138 case SYS_POWEROFF:
1139 default_event_handler(SYS_POWEROFF);
1140 break;
1141 case ACTION_WPS_VIEW_PLAYLIST:
1142 gwps_leave_wps();
1143 if (playlist_viewer()) /* true if USB connected */
1144 return GO_TO_ROOT;
1145 restore = true;
1146 break;
1147 default:
1148 if(default_event_handler(button) == SYS_USB_CONNECTED)
1150 gwps_leave_wps();
1151 return GO_TO_ROOT;
1153 update = true;
1154 break;
1157 if (vol_changed)
1159 bool res = false;
1160 vol_changed = false;
1161 setvol();
1162 FOR_NB_SCREENS(i)
1164 if(update_onvol_change(&gui_wps[i]))
1165 res = true;
1167 if (res) {
1168 restore = true;
1169 restoretimer = RESTORE_WPS_NEXT_SECOND;
1174 if (restore &&
1175 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1176 TIME_AFTER(current_tick, restoretimer)))
1178 restore = false;
1179 restoretimer = RESTORE_WPS_INSTANTLY;
1180 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1181 add_event(LCD_EVENT_ACTIVATION, false, wps_lcd_activation_hook);
1182 #endif
1183 /* we remove the update delay since it's not very usable in the wps,
1184 * e.g. during volume changing or ffwd/rewind */
1185 sb_skin_set_update_delay(0);
1186 FOR_NB_SCREENS(i)
1187 gui_wps[i].display->backdrop_show(BACKDROP_SKIN_WPS);
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