initial commit for a rework to put the fade on pause feature into the playback engine
[kugel-rb.git] / apps / gui / wps.c
blob8361bc636c0afe5a40e50847e5c6fea78d03b250
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Jerome Kuptz
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include "config.h"
26 #include "system.h"
27 #include "file.h"
28 #include "lcd.h"
29 #include "font.h"
30 #include "backlight.h"
31 #include "action.h"
32 #include "kernel.h"
33 #include "filetypes.h"
34 #include "settings.h"
35 #include "skin_engine/skin_engine.h"
36 #include "mp3_playback.h"
37 #include "audio.h"
38 #include "usb.h"
39 #include "status.h"
40 #include "storage.h"
41 #include "screens.h"
42 #include "playlist.h"
43 #ifdef HAVE_LCD_BITMAP
44 #include "icons.h"
45 #include "peakmeter.h"
46 #endif
47 #include "lang.h"
48 #include "bookmark.h"
49 #include "misc.h"
50 #include "sound.h"
51 #include "onplay.h"
52 #include "abrepeat.h"
53 #include "playback.h"
54 #include "splash.h"
55 #include "cuesheet.h"
56 #include "ata_idle_notify.h"
57 #include "root_menu.h"
58 #include "backdrop.h"
59 #include "quickscreen.h"
60 #include "pitchscreen.h"
61 #include "appevents.h"
62 #include "viewport.h"
63 #include "pcmbuf.h"
64 #include "option_select.h"
65 #include "dsp.h"
66 #include "playlist_viewer.h"
67 #include "wps.h"
68 #include "statusbar-skinned.h"
70 #define RESTORE_WPS_INSTANTLY 0l
71 #define RESTORE_WPS_NEXT_SECOND ((long)(HZ+current_tick))
73 #define FF_REWIND_MAX_PERCENT 3 /* cap ff/rewind step size at max % of file */
74 /* 3% of 30min file == 54s step size */
75 #define MIN_FF_REWIND_STEP 500
77 /* currently only one wps_state is needed, initialize to 0 */
78 struct wps_state wps_state = { .id3 = NULL };
79 static struct gui_wps gui_wps[NB_SCREENS] = {{ .data = NULL }};
80 static struct wps_data wps_datas[NB_SCREENS] = {{ .wps_loaded = 0 }};
81 static struct wps_sync_data wps_sync_data = { .do_full_update = false };
83 /* initial setup of wps_data */
84 static void wps_state_init(void);
85 static void track_changed_callback(void *param);
86 static void nextid3available_callback(void* param);
87 static void wps_part_update_cb(void);
89 #ifdef HAVE_TOUCHSCREEN
90 static void wps_disarm_touchregions(struct wps_data *data);
91 #endif
93 #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps"
94 #ifdef HAVE_REMOTE_LCD
95 #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps"
96 #define DEFAULT_WPS(screen) ((screen) == SCREEN_MAIN ? \
97 WPS_DEFAULTCFG:RWPS_DEFAULTCFG)
98 #else
99 #define DEFAULT_WPS(screen) (WPS_DEFAULTCFG)
100 #endif
102 void wps_data_load(enum screen_type screen, const char *buf, bool isfile)
104 bool loaded_ok;
106 #ifndef __PCTOOL__
108 * Hardcode loading WPS_DEFAULTCFG to cause a reset ideally this
109 * wants to be a virtual file. Feel free to modify dirbrowse()
110 * if you're feeling brave.
113 if (buf && ! strcmp(buf, DEFAULT_WPS(screen)) )
115 #ifdef HAVE_REMOTE_LCD
116 if (screen == SCREEN_REMOTE)
117 global_settings.rwps_file[0] = '\0';
118 else
119 #endif
120 global_settings.wps_file[0] = '\0';
121 buf = NULL;
124 #endif /* __PCTOOL__ */
126 loaded_ok = buf && skin_data_load(screen, gui_wps[screen].data, buf, isfile);
128 if (!loaded_ok) /* load the hardcoded default */
130 char *skin_buf[NB_SCREENS] = {
131 #ifdef HAVE_LCD_BITMAP
132 #if LCD_DEPTH > 1
133 "%Xd\n"
134 #endif
135 "%s%?it<%?in<%in. |>%it|%fn>\n"
136 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
137 "%s%?id<%id|%?d1<%d1|(root)>> %?iy<(%iy)|>\n\n"
138 "%al%pc/%pt%ar[%pp:%pe]\n"
139 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
140 "%pb\n%pm\n",
141 #else
142 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d2> - %?id<%id|%d1>\n"
143 "%pc%?ps<*|/>%pt\n",
144 #endif
145 #ifdef HAVE_REMOTE_LCD
146 #if LCD_REMOTE_DEPTH > 1
147 "%Xd\n"
148 #endif
149 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
150 "%s%?it<%?in<%in. |>%it|%fn>\n"
151 "%al%pc/%pt%ar[%pp:%pe]\n"
152 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
153 "%pb\n",
154 #endif
156 skin_data_load(screen, gui_wps[screen].data, skin_buf[screen], false);
160 bool wps_fading_out = false;
162 void wps_fade(bool fade_in, bool updatewps)
164 playback_fade_on_pause(fade_in, updatewps ? wps_part_update_cb : NULL);
167 bool is_wps_fading(void)
169 return (audio_status() & AUDIO_STATUS_PAUSE);
172 static bool update_onvol_change(struct gui_wps * gwps)
174 skin_update(gwps, WPS_REFRESH_NON_STATIC);
176 #ifdef HAVE_LCD_CHARCELLS
177 splashf(0, "Vol: %3d dB",
178 sound_val2phys(SOUND_VOLUME, global_settings.volume));
179 return true;
180 #endif
181 return false;
185 bool ffwd_rew(int button)
187 unsigned int step = 0; /* current ff/rewind step */
188 unsigned int max_step = 0; /* maximum ff/rewind step */
189 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
190 int direction = -1; /* forward=1 or backward=-1 */
191 bool exit = false;
192 bool usb = false;
193 int i = 0;
194 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
196 if (button == ACTION_NONE)
198 status_set_ffmode(0);
199 return usb;
201 while (!exit)
203 switch ( button )
205 case ACTION_WPS_SEEKFWD:
206 direction = 1;
207 case ACTION_WPS_SEEKBACK:
208 if (wps_state.ff_rewind)
210 if (direction == 1)
212 /* fast forwarding, calc max step relative to end */
213 max_step = (wps_state.id3->length -
214 (wps_state.id3->elapsed +
215 ff_rewind_count)) *
216 FF_REWIND_MAX_PERCENT / 100;
218 else
220 /* rewinding, calc max step relative to start */
221 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
222 FF_REWIND_MAX_PERCENT / 100;
225 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
227 if (step > max_step)
228 step = max_step;
230 ff_rewind_count += step * direction;
232 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
233 step += step >> ff_rw_accel;
235 else
237 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
238 wps_state.id3 && wps_state.id3->length )
240 if (!wps_state.paused)
241 #if (CONFIG_CODEC == SWCODEC)
242 audio_pre_ff_rewind();
243 #else
244 audio_pause();
245 #endif
246 #if CONFIG_KEYPAD == PLAYER_PAD
247 FOR_NB_SCREENS(i)
248 gui_wps[i].display->stop_scroll();
249 #endif
250 if (direction > 0)
251 status_set_ffmode(STATUS_FASTFORWARD);
252 else
253 status_set_ffmode(STATUS_FASTBACKWARD);
255 wps_state.ff_rewind = true;
257 step = 1000 * global_settings.ff_rewind_min_step;
259 else
260 break;
263 if (direction > 0) {
264 if ((wps_state.id3->elapsed + ff_rewind_count) >
265 wps_state.id3->length)
266 ff_rewind_count = wps_state.id3->length -
267 wps_state.id3->elapsed;
269 else {
270 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
271 ff_rewind_count = -wps_state.id3->elapsed;
274 /* set the wps state ff_rewind_count so the progess info
275 displays corectly */
276 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
277 ff_rewind_count:-ff_rewind_count;
278 FOR_NB_SCREENS(i)
280 skin_update(&gui_wps[i],
281 WPS_REFRESH_PLAYER_PROGRESS |
282 WPS_REFRESH_DYNAMIC);
285 break;
287 case ACTION_WPS_STOPSEEK:
288 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
289 audio_ff_rewind(wps_state.id3->elapsed);
290 wps_state.ff_rewind_count = 0;
291 wps_state.ff_rewind = false;
292 status_set_ffmode(0);
293 #if (CONFIG_CODEC != SWCODEC)
294 if (!wps_state.paused)
295 audio_resume();
296 #endif
297 #ifdef HAVE_LCD_CHARCELLS
298 FOR_NB_SCREENS(i)
299 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
300 #endif
301 exit = true;
302 break;
304 default:
305 if(default_event_handler(button) == SYS_USB_CONNECTED) {
306 status_set_ffmode(0);
307 usb = true;
308 exit = true;
310 break;
312 if (!exit)
314 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
315 #ifdef HAVE_TOUCHSCREEN
316 if (button == ACTION_TOUCHSCREEN)
317 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
318 #endif
321 return usb;
325 void display_keylock_text(bool locked)
327 int i;
328 FOR_NB_SCREENS(i)
329 gui_wps[i].display->stop_scroll();
331 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
337 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
338 static void gwps_caption_backlight(struct wps_state *state)
340 if (state && state->id3)
342 #ifdef HAVE_BACKLIGHT
343 if (global_settings.caption_backlight)
345 /* turn on backlight n seconds before track ends, and turn it off n
346 seconds into the new track. n == backlight_timeout, or 5s */
347 int n = global_settings.backlight_timeout * 1000;
349 if ( n < 1000 )
350 n = 5000; /* use 5s if backlight is always on or off */
352 if (((state->id3->elapsed < 1000) ||
353 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
354 (state->paused == false))
355 backlight_on();
357 #endif
358 #ifdef HAVE_REMOTE_LCD
359 if (global_settings.remote_caption_backlight)
361 /* turn on remote backlight n seconds before track ends, and turn it
362 off n seconds into the new track. n == remote_backlight_timeout,
363 or 5s */
364 int n = global_settings.remote_backlight_timeout * 1000;
366 if ( n < 1000 )
367 n = 5000; /* use 5s if backlight is always on or off */
369 if (((state->id3->elapsed < 1000) ||
370 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
371 (state->paused == false))
372 remote_backlight_on();
374 #endif
377 #endif
380 static void change_dir(int direction)
382 if (global_settings.prevent_skip)
383 return;
385 if (direction < 0)
386 audio_prev_dir();
387 else if (direction > 0)
388 audio_next_dir();
389 /* prevent the next dir to immediatly start being ffw'd */
390 action_wait_for_release();
393 static void prev_track(unsigned long skip_thresh)
395 if (wps_state.id3->elapsed < skip_thresh)
397 audio_prev();
398 return;
400 else
402 if (wps_state.id3->cuesheet)
404 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
405 return;
408 if (!wps_state.paused)
409 #if (CONFIG_CODEC == SWCODEC)
410 audio_pre_ff_rewind();
411 #else
412 audio_pause();
413 #endif
415 audio_ff_rewind(0);
417 #if (CONFIG_CODEC != SWCODEC)
418 if (!wps_state.paused)
419 audio_resume();
420 #endif
424 static void next_track(void)
426 /* take care of if we're playing a cuesheet */
427 if (wps_state.id3->cuesheet)
429 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
431 /* if the result was false, then we really want
432 to skip to the next track */
433 return;
437 audio_next();
440 static void play_hop(int direction)
442 long step = global_settings.skip_length*1000;
443 long elapsed = wps_state.id3->elapsed;
444 long remaining = wps_state.id3->length - elapsed;
446 if (step < 0)
448 if (direction < 0)
450 prev_track(DEFAULT_SKIP_TRESH);
451 return;
453 else if (remaining < DEFAULT_SKIP_TRESH*2)
455 next_track();
456 return;
458 else
459 elapsed += (remaining - DEFAULT_SKIP_TRESH*2);
461 else if (!global_settings.prevent_skip &&
462 (!step ||
463 (direction > 0 && step >= remaining) ||
464 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
465 { /* Do normal track skipping */
466 if (direction > 0)
467 next_track();
468 else if (direction < 0)
469 prev_track(DEFAULT_SKIP_TRESH);
470 return;
472 else if (direction == 1 && step >= remaining)
474 #if CONFIG_CODEC == SWCODEC
475 if(global_settings.beep)
476 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
477 #endif
478 return;
480 else if ((direction == -1 && elapsed < step))
482 elapsed = 0;
484 else
486 elapsed += step * direction;
488 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
490 #if (CONFIG_CODEC == SWCODEC)
491 audio_pre_ff_rewind();
492 #else
493 audio_pause();
494 #endif
496 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
497 #if (CONFIG_CODEC != SWCODEC)
498 if (!wps_state.paused)
499 audio_resume();
500 #endif
504 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
506 * If the user is unable to see the wps, because the display is deactivated,
507 * we suppress updates until the wps is activated again (the lcd driver will
508 * call this hook to issue an instant update)
509 * */
510 static void wps_lcd_activation_hook(void *param)
512 (void)param;
513 wps_sync_data.do_full_update = true;
514 /* force timeout in wps main loop, so that the update is instantly */
515 queue_post(&button_queue, BUTTON_NONE, 0);
517 #endif
519 static void wps_part_update_cb(void)
521 /* post to get an update */
522 if (queue_empty(&button_queue))
523 queue_post(&button_queue, BUTTON_NONE, 0);
526 static void gwps_leave_wps(void)
528 int i;
530 FOR_NB_SCREENS(i)
532 gui_wps[i].display->stop_scroll();
533 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
534 gui_wps[i].display->backdrop_show(sb_get_backdrop(i));
535 #endif
537 #ifdef HAVE_LCD_BITMAP
538 bool draw = false;
539 if (gui_wps[i].data->wps_sb_tag)
540 draw = gui_wps[i].data->show_sb_on_wps;
541 else if (statusbar_position(i) != STATUSBAR_OFF)
542 draw = true;
543 #endif
544 viewportmanager_theme_undo(i, draw);
548 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
549 /* Play safe and unregister the hook */
550 remove_event(LCD_EVENT_ACTIVATION, wps_lcd_activation_hook);
551 #endif
552 /* unhandle statusbar update delay */
553 sb_skin_set_update_delay(DEFAULT_UPDATE_DELAY);
557 * display the wps on entering or restoring */
558 static void gwps_enter_wps(void)
560 int i;
561 FOR_NB_SCREENS(i)
563 struct gui_wps *gwps = &gui_wps[i];
564 struct screen *display = gwps->display;
565 #ifdef HAVE_LCD_BITMAP
566 bool draw = false;
567 if (gui_wps[i].data->wps_sb_tag)
568 draw = gui_wps[i].data->show_sb_on_wps;
569 else if (statusbar_position(i) != STATUSBAR_OFF)
570 draw = true;
571 #endif
572 display->stop_scroll();
573 viewportmanager_theme_enable(i, draw, NULL);
575 /* Update the values in the first (default) viewport - in case the user
576 has modified the statusbar or colour settings */
577 #if LCD_DEPTH > 1
578 if (display->depth > 1)
580 struct viewport *vp = &find_viewport(VP_DEFAULT_LABEL, gwps->data)->vp;
581 vp->fg_pattern = display->get_foreground();
582 vp->bg_pattern = display->get_background();
584 #endif
585 /* make the backdrop actually take effect */
586 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
587 display->backdrop_show(gwps->data->backdrop);
588 #endif
589 display->clear_display();
590 skin_update(gwps, WPS_REFRESH_ALL);
592 #ifdef HAVE_TOUCHSCREEN
593 wps_disarm_touchregions(gui_wps[i].data);
594 #endif
596 /* force statusbar/skin update since we just cleared the whole screen */
597 send_event(GUI_EVENT_ACTIONUPDATE, (void*)1);
600 #ifdef HAVE_TOUCHSCREEN
601 /** Disarms all touchregions. */
602 static void wps_disarm_touchregions(struct wps_data *data)
604 struct skin_token_list *regions = data->touchregions;
605 while (regions)
607 ((struct touchregion *)regions->token->value.data)->armed = false;
608 regions = regions->next;
612 int wps_get_touchaction(struct wps_data *data)
614 int returncode = ACTION_NONE;
615 short x,y;
616 short vx, vy;
617 int type = action_get_touchscreen_press(&x, &y);
618 static int last_action = ACTION_NONE;
619 struct touchregion *r;
620 bool repeated = (type == BUTTON_REPEAT);
621 bool released = (type == BUTTON_REL);
622 bool pressed = (type == BUTTON_TOUCHSCREEN);
623 struct skin_token_list *regions = data->touchregions;
625 while (regions)
627 r = (struct touchregion *)regions->token->value.data;
628 /* make sure this region's viewport is visible */
629 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
631 regions = regions->next;
632 continue;
634 /* check if it's inside this viewport */
635 if (viewport_point_within_vp(&(r->wvp->vp), x, y))
636 { /* reposition the touch inside the viewport since touchregions
637 * are relative to a preceding viewport */
638 vx = x - r->wvp->vp.x;
639 vy = y - r->wvp->vp.y;
640 /* now see if the point is inside this region */
641 if (vx >= r->x && vx < r->x+r->width &&
642 vy >= r->y && vy < r->y+r->height)
644 /* reposition the touch within the area */
645 vx -= r->x;
646 vy -= r->y;
648 switch(r->type)
650 case WPS_TOUCHREGION_ACTION:
651 if (r->armed && ((repeated && r->repeat) || (released && !r->repeat)))
653 last_action = r->action;
654 returncode = r->action;
656 if (pressed)
657 r->armed = true;
658 break;
659 case WPS_TOUCHREGION_SCROLLBAR:
660 if(r->width > r->height)
661 /* landscape */
662 wps_state.id3->elapsed = (vx *
663 wps_state.id3->length) / r->width;
664 else
665 /* portrait */
666 wps_state.id3->elapsed = (vy *
667 wps_state.id3->length) / r->height;
669 if (!wps_state.paused)
670 #if (CONFIG_CODEC == SWCODEC)
671 audio_pre_ff_rewind();
672 #else
673 audio_pause();
674 #endif
675 audio_ff_rewind(wps_state.id3->elapsed);
676 #if (CONFIG_CODEC != SWCODEC)
677 if (!wps_state.paused)
678 audio_resume();
679 #endif
680 break;
681 case WPS_TOUCHREGION_VOLUME:
683 const int min_vol = sound_min(SOUND_VOLUME);
684 const int max_vol = sound_max(SOUND_VOLUME);
685 if(r->width > r->height)
686 /* landscape */
687 global_settings.volume = (vx *
688 (max_vol - min_vol)) / r->width;
689 else
690 /* portrait */
691 global_settings.volume = ((r->height - vy) *
692 (max_vol-min_vol)) / r->height;
694 global_settings.volume += min_vol;
695 setvol();
696 returncode = ACTION_REDRAW;
701 regions = regions->next;
704 /* On release, all regions are disarmed. */
705 if (released)
706 wps_disarm_touchregions(data);
708 if (returncode != ACTION_NONE)
709 return returncode;
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 #ifdef HAVE_HOTKEY
852 case ACTION_WPS_HOTKEY:
853 if (!global_settings.hotkey_wps)
854 break;
855 /* fall through */
856 #endif
857 case ACTION_WPS_CONTEXT:
859 bool hotkey = button == ACTION_WPS_HOTKEY;
860 gwps_leave_wps();
861 int retval = onplay(wps_state.id3->path,
862 FILE_ATTR_AUDIO, CONTEXT_WPS, hotkey);
863 /* if music is stopped in the context menu we want to exit the wps */
864 if (retval == ONPLAY_MAINMENU
865 || !audio_status())
866 return GO_TO_ROOT;
867 else if (retval == ONPLAY_PLAYLIST)
868 return GO_TO_PLAYLIST_VIEWER;
869 restore = true;
871 break;
873 case ACTION_WPS_BROWSE:
874 #ifdef HAVE_LCD_CHARCELLS
875 status_set_record(false);
876 status_set_audio(false);
877 #endif
878 gwps_leave_wps();
879 return GO_TO_PREVIOUS_BROWSER;
880 break;
882 /* play/pause */
883 case ACTION_WPS_PLAY:
884 if (global_settings.party_mode)
885 break;
886 if ( wps_state.paused )
888 wps_state.paused = false;
889 if ( global_settings.fade_on_stop )
890 wps_fade(true, true);
891 else
892 audio_resume();
894 else
896 wps_state.paused = true;
897 if ( global_settings.fade_on_stop )
898 wps_fade(false, true);
899 else
900 audio_pause();
901 settings_save();
902 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
903 call_storage_idle_notifys(true); /* make sure resume info is saved */
904 #endif
906 break;
908 case ACTION_WPS_VOLUP:
909 global_settings.volume++;
910 vol_changed = true;
911 break;
912 case ACTION_WPS_VOLDOWN:
913 global_settings.volume--;
914 vol_changed = true;
915 break;
916 /* fast forward
917 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
918 case ACTION_WPS_SEEKFWD:
919 if (global_settings.party_mode)
920 break;
921 if (current_tick -last_right < HZ)
923 if (wps_state.id3->cuesheet)
925 audio_next();
927 else
929 change_dir(1);
932 else
933 ffwd_rew(ACTION_WPS_SEEKFWD);
934 last_right = last_left = 0;
935 break;
936 /* fast rewind
937 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
938 case ACTION_WPS_SEEKBACK:
939 if (global_settings.party_mode)
940 break;
941 if (current_tick -last_left < HZ)
943 if (wps_state.id3->cuesheet)
945 if (!wps_state.paused)
946 #if (CONFIG_CODEC == SWCODEC)
947 audio_pre_ff_rewind();
948 #else
949 audio_pause();
950 #endif
951 audio_ff_rewind(0);
953 else
955 change_dir(-1);
958 else
959 ffwd_rew(ACTION_WPS_SEEKBACK);
960 last_left = last_right = 0;
961 break;
963 /* prev / restart */
964 case ACTION_WPS_SKIPPREV:
965 if (global_settings.party_mode)
966 break;
967 last_left = current_tick;
968 #ifdef AB_REPEAT_ENABLE
969 /* if we're in A/B repeat mode and the current position
970 is past the A marker, jump back to the A marker... */
971 if ( ab_repeat_mode_enabled() )
973 if ( ab_after_A_marker(wps_state.id3->elapsed) )
975 ab_jump_to_A_marker();
976 break;
977 #if (AB_REPEAT_ENABLE == 2)
978 } else {
979 ab_reset_markers();
980 #endif
983 else
984 /* ...otherwise, do it normally */
985 #endif
986 play_hop(-1);
987 break;
989 /* next
990 OR if skip length set, hop by predetermined amount. */
991 case ACTION_WPS_SKIPNEXT:
992 if (global_settings.party_mode)
993 break;
994 last_right = current_tick;
995 #ifdef AB_REPEAT_ENABLE
996 /* if we're in A/B repeat mode and the current position is
997 before the A marker, jump to the A marker... */
998 if ( ab_repeat_mode_enabled() )
1000 if ( ab_before_A_marker(wps_state.id3->elapsed) )
1002 ab_jump_to_A_marker();
1003 break;
1004 #if (AB_REPEAT_ENABLE == 2)
1005 } else {
1006 ab_reset_markers();
1007 #endif
1010 else
1011 /* ...otherwise, do it normally */
1012 #endif
1013 play_hop(1);
1014 break;
1015 /* next / prev directories */
1016 /* and set A-B markers if in a-b mode */
1017 case ACTION_WPS_ABSETB_NEXTDIR:
1018 if (global_settings.party_mode)
1019 break;
1020 #if defined(AB_REPEAT_ENABLE)
1021 if (ab_repeat_mode_enabled())
1023 ab_set_B_marker(wps_state.id3->elapsed);
1024 ab_jump_to_A_marker();
1026 else
1027 #endif
1029 change_dir(1);
1031 break;
1032 case ACTION_WPS_ABSETA_PREVDIR:
1033 if (global_settings.party_mode)
1034 break;
1035 #if defined(AB_REPEAT_ENABLE)
1036 if (ab_repeat_mode_enabled())
1037 ab_set_A_marker(wps_state.id3->elapsed);
1038 else
1039 #endif
1041 change_dir(-1);
1043 break;
1044 /* menu key functions */
1045 case ACTION_WPS_MENU:
1046 gwps_leave_wps();
1047 return GO_TO_ROOT;
1048 break;
1051 #ifdef HAVE_QUICKSCREEN
1052 case ACTION_WPS_QUICKSCREEN:
1054 gwps_leave_wps();
1055 if (quick_screen_quick(button))
1056 return GO_TO_ROOT;
1057 restore = true;
1059 break;
1060 #endif /* HAVE_QUICKSCREEN */
1062 /* screen settings */
1063 #ifdef BUTTON_F3
1064 case ACTION_F3:
1066 gwps_leave_wps();
1067 if (quick_screen_f3(BUTTON_F3))
1068 return GO_TO_ROOT;
1069 restore = true;
1071 break;
1072 #endif /* BUTTON_F3 */
1074 /* pitch screen */
1075 #ifdef HAVE_PITCHSCREEN
1076 case ACTION_WPS_PITCHSCREEN:
1078 gwps_leave_wps();
1079 if (1 == gui_syncpitchscreen_run())
1080 return GO_TO_ROOT;
1081 restore = true;
1083 break;
1084 #endif /* HAVE_PITCHSCREEN */
1086 #ifdef AB_REPEAT_ENABLE
1087 /* reset A&B markers */
1088 case ACTION_WPS_ABRESET:
1089 if (ab_repeat_mode_enabled())
1091 ab_reset_markers();
1092 update = true;
1094 break;
1095 #endif /* AB_REPEAT_ENABLE */
1097 /* stop and exit wps */
1098 case ACTION_WPS_STOP:
1099 if (global_settings.party_mode)
1100 break;
1101 bookmark = true;
1102 exit = true;
1103 break;
1105 case ACTION_WPS_ID3SCREEN:
1107 gwps_leave_wps();
1108 if (browse_id3())
1109 return GO_TO_ROOT;
1110 restore = true;
1112 break;
1113 #ifdef HAVE_TOUCHSCREEN
1114 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1116 global_settings.playlist_shuffle =
1117 !global_settings.playlist_shuffle;
1118 #if CONFIG_CODEC == SWCODEC
1119 dsp_set_replaygain();
1120 #endif
1121 if (global_settings.playlist_shuffle)
1122 playlist_randomise(NULL, current_tick, true);
1123 else
1124 playlist_sort(NULL, true);
1126 break;
1127 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1129 const struct settings_list *rep_setting =
1130 find_setting(&global_settings.repeat_mode, NULL);
1131 option_select_next_val(rep_setting, false, true);
1132 audio_flush_and_reload_tracks();
1134 break;
1135 #endif /* HAVE_TOUCHSCREEN */
1136 /* this case is used by the softlock feature
1137 * it requests a full update here */
1138 case ACTION_REDRAW:
1139 wps_sync_data.do_full_update = true;
1140 break;
1141 case ACTION_NONE: /* Timeout, do a partial update */
1142 update = true;
1143 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1144 break;
1145 #ifdef HAVE_RECORDING
1146 case ACTION_WPS_REC:
1147 exit = true;
1148 break;
1149 #endif
1150 case SYS_POWEROFF:
1151 default_event_handler(SYS_POWEROFF);
1152 break;
1153 case ACTION_WPS_VIEW_PLAYLIST:
1154 gwps_leave_wps();
1155 return GO_TO_PLAYLIST_VIEWER;
1156 break;
1157 default:
1158 if(default_event_handler(button) == SYS_USB_CONNECTED)
1160 gwps_leave_wps();
1161 return GO_TO_ROOT;
1163 update = true;
1164 break;
1167 if (vol_changed)
1169 bool res = false;
1170 vol_changed = false;
1171 setvol();
1172 FOR_NB_SCREENS(i)
1174 if(update_onvol_change(&gui_wps[i]))
1175 res = true;
1177 if (res) {
1178 restore = true;
1179 restoretimer = RESTORE_WPS_NEXT_SECOND;
1184 if (restore &&
1185 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1186 TIME_AFTER(current_tick, restoretimer)))
1188 restore = false;
1189 restoretimer = RESTORE_WPS_INSTANTLY;
1190 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1191 add_event(LCD_EVENT_ACTIVATION, false, wps_lcd_activation_hook);
1192 #endif
1193 /* we remove the update delay since it's not very usable in the wps,
1194 * e.g. during volume changing or ffwd/rewind */
1195 sb_skin_set_update_delay(0);
1196 wps_sync_data.do_full_update = update = false;
1197 gwps_enter_wps();
1199 else if (wps_sync_data.do_full_update || update)
1201 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1202 gwps_caption_backlight(&wps_state);
1203 #endif
1204 FOR_NB_SCREENS(i)
1206 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1207 /* currently, all remotes are readable without backlight
1208 * so still update those */
1209 if (lcd_active() || (i != SCREEN_MAIN))
1210 #endif
1212 skin_update(&gui_wps[i], wps_sync_data.do_full_update ?
1213 WPS_REFRESH_ALL : WPS_REFRESH_NON_STATIC);
1216 wps_sync_data.do_full_update = false;
1217 update = false;
1220 if (exit) {
1221 #ifdef HAVE_LCD_CHARCELLS
1222 status_set_record(false);
1223 status_set_audio(false);
1224 #endif
1225 if (global_settings.fade_on_stop)
1226 wps_fade(false, true);
1228 if (bookmark)
1229 bookmark_autobookmark(true);
1230 audio_stop();
1231 #ifdef AB_REPEAT_ENABLE
1232 ab_reset_markers();
1233 #endif
1234 gwps_leave_wps();
1235 #ifdef HAVE_RECORDING
1236 if (button == ACTION_WPS_REC)
1237 return GO_TO_RECSCREEN;
1238 #endif
1239 if (global_settings.browse_current)
1240 return GO_TO_PREVIOUS_BROWSER;
1241 return GO_TO_PREVIOUS;
1244 if (button && !IS_SYSEVENT(button) )
1245 storage_spin();
1247 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1250 /* this is called from the playback thread so NO DRAWING! */
1251 static void track_changed_callback(void *param)
1253 wps_state.id3 = (struct mp3entry*)param;
1254 wps_state.nid3 = audio_next_track();
1255 if (wps_state.id3->cuesheet)
1257 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1258 cue_spoof_id3(wps_state.id3->cuesheet, wps_state.id3);
1260 wps_sync_data.do_full_update = true;
1262 static void nextid3available_callback(void* param)
1264 (void)param;
1265 wps_state.nid3 = audio_next_track();
1266 wps_sync_data.do_full_update = true;
1270 static void wps_state_init(void)
1272 wps_state.ff_rewind = false;
1273 wps_state.paused = false;
1274 if(audio_status() & AUDIO_STATUS_PLAY)
1276 wps_state.id3 = audio_current_track();
1277 wps_state.nid3 = audio_next_track();
1279 else
1281 wps_state.id3 = NULL;
1282 wps_state.nid3 = NULL;
1284 /* We'll be updating due to restore initialized with true */
1285 wps_sync_data.do_full_update = false;
1286 /* add the WPS track event callbacks */
1287 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1288 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1292 void gui_sync_wps_init(void)
1294 int i;
1295 FOR_NB_SCREENS(i)
1297 #ifdef HAVE_ALBUMART
1298 wps_datas[i].albumart = NULL;
1299 wps_datas[i].playback_aa_slot = -1;
1300 #endif
1301 gui_wps[i].data = &wps_datas[i];
1302 gui_wps[i].display = &screens[i];
1303 /* Currently no seperate wps_state needed/possible
1304 so use the only available ( "global" ) one */
1305 gui_wps[i].state = &wps_state;
1306 /* must point to the same struct for both screens */
1307 gui_wps[i].sync_data = &wps_sync_data;
1312 #ifdef IPOD_ACCESSORY_PROTOCOL
1313 bool is_wps_fading(void)
1315 return wps_state.is_fading;
1318 int wps_get_ff_rewind_count(void)
1320 return wps_state.ff_rewind_count;
1322 #endif