Fix the WPS to work properly with the statusbar for the previous change.. Side effect...
[kugel-rb.git] / apps / gui / wps.c
blob4f162f452abdc467d4e9586ccef74a774927d83d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Jerome Kuptz
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include "config.h"
26 #include "system.h"
27 #include "file.h"
28 #include "lcd.h"
29 #include "font.h"
30 #include "backlight.h"
31 #include "action.h"
32 #include "kernel.h"
33 #include "filetypes.h"
34 #include "sprintf.h"
35 #include "settings.h"
36 #include "skin_engine/skin_engine.h"
37 #include "mp3_playback.h"
38 #include "audio.h"
39 #include "usb.h"
40 #include "status.h"
41 #include "storage.h"
42 #include "screens.h"
43 #include "playlist.h"
44 #ifdef HAVE_LCD_BITMAP
45 #include "icons.h"
46 #include "peakmeter.h"
47 #endif
48 #include "lang.h"
49 #include "bookmark.h"
50 #include "misc.h"
51 #include "sound.h"
52 #include "onplay.h"
53 #include "abrepeat.h"
54 #include "playback.h"
55 #include "splash.h"
56 #include "cuesheet.h"
57 #include "ata_idle_notify.h"
58 #include "root_menu.h"
59 #include "backdrop.h"
60 #include "quickscreen.h"
61 #include "pitchscreen.h"
62 #include "appevents.h"
63 #include "viewport.h"
64 #include "pcmbuf.h"
65 #include "option_select.h"
66 #include "dsp.h"
67 #include "playlist_viewer.h"
68 #include "wps.h"
69 #include "statusbar-skinned.h"
71 #define RESTORE_WPS_INSTANTLY 0l
72 #define RESTORE_WPS_NEXT_SECOND ((long)(HZ+current_tick))
73 /* in milliseconds */
74 #define DEFAULT_SKIP_TRESH 3000l
77 #define FF_REWIND_MAX_PERCENT 3 /* cap ff/rewind step size at max % of file */
78 /* 3% of 30min file == 54s step size */
79 #define MIN_FF_REWIND_STEP 500
81 /* currently only one wps_state is needed, initialize to 0 */
82 struct wps_state wps_state = { .id3 = NULL };
83 static struct gui_wps gui_wps[NB_SCREENS] = {{ .data = NULL }};
84 static struct wps_data wps_datas[NB_SCREENS] = {{ .wps_loaded = 0 }};
85 static struct wps_sync_data wps_sync_data = { .do_full_update = false };
87 /* initial setup of wps_data */
88 static void wps_state_init(void);
89 static void track_changed_callback(void *param);
90 static void nextid3available_callback(void* param);
91 #ifdef HAVE_LCD_BITMAP
92 static void statusbar_toggle_handler(void *data);
93 #endif
95 #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps"
96 #ifdef HAVE_REMOTE_LCD
97 #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps"
98 #define DEFAULT_WPS(screen) ((screen) == SCREEN_MAIN ? \
99 WPS_DEFAULTCFG:RWPS_DEFAULTCFG)
100 #else
101 #define DEFAULT_WPS(screen) (WPS_DEFAULTCFG)
102 #endif
104 void wps_data_load(enum screen_type screen, const char *buf, bool isfile)
106 bool loaded_ok;
108 screens[screen].backdrop_unload(BACKDROP_SKIN_WPS);
110 #ifndef __PCTOOL__
112 * Hardcode loading WPS_DEFAULTCFG to cause a reset ideally this
113 * wants to be a virtual file. Feel free to modify dirbrowse()
114 * if you're feeling brave.
117 if (buf && ! strcmp(buf, DEFAULT_WPS(screen)) )
119 #ifdef HAVE_REMOTE_LCD
120 if (screen == SCREEN_REMOTE)
121 global_settings.rwps_file[0] = '\0';
122 else
123 #endif
124 global_settings.wps_file[0] = '\0';
125 buf = NULL;
128 #endif /* __PCTOOL__ */
130 loaded_ok = buf && skin_data_load(screen, gui_wps[screen].data, buf, isfile);
132 if (!loaded_ok) /* load the hardcoded default */
134 char *skin_buf[NB_SCREENS] = {
135 #ifdef HAVE_LCD_BITMAP
136 "%s%?it<%?in<%in. |>%it|%fn>\n"
137 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
138 "%s%?id<%id|%?d1<%d1|(root)>> %?iy<(%iy)|>\n\n"
139 "%al%pc/%pt%ar[%pp:%pe]\n"
140 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
141 "%pb\n%pm\n",
142 #else
143 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d2> - %?id<%id|%d1>\n"
144 "%pc%?ps<*|/>%pt\n",
145 #endif
146 #ifdef HAVE_REMOTE_LCD
147 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
148 "%s%?it<%?in<%in. |>%it|%fn>\n"
149 "%al%pc/%pt%ar[%pp:%pe]\n"
150 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
151 "%pb\n",
152 #endif
154 skin_data_load(screen, gui_wps[screen].data, skin_buf[screen], false);
156 #ifdef HAVE_REMOVE_LCD
157 gui_wps[screen].data->remote_wps = !(screen == SCREEN_MAIN);
159 #endif
162 void fade(bool fade_in, bool updatewps)
164 int fp_global_vol = global_settings.volume << 8;
165 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
166 int fp_step = (fp_global_vol - fp_min_vol) / 30;
167 int i;
168 wps_state.is_fading = !fade_in;
169 if (fade_in) {
170 /* fade in */
171 int fp_volume = fp_min_vol;
173 /* zero out the sound */
174 sound_set_volume(fp_min_vol >> 8);
176 sleep(HZ/10); /* let audio thread run */
177 audio_resume();
179 while (fp_volume < fp_global_vol - fp_step) {
180 fp_volume += fp_step;
181 sound_set_volume(fp_volume >> 8);
182 if (updatewps)
184 FOR_NB_SCREENS(i)
185 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
187 sleep(1);
189 sound_set_volume(global_settings.volume);
191 else {
192 /* fade out */
193 int fp_volume = fp_global_vol;
195 while (fp_volume > fp_min_vol + fp_step) {
196 fp_volume -= fp_step;
197 sound_set_volume(fp_volume >> 8);
198 if (updatewps)
200 FOR_NB_SCREENS(i)
201 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
203 sleep(1);
205 audio_pause();
206 wps_state.is_fading = false;
207 #if CONFIG_CODEC != SWCODEC
208 #ifndef SIMULATOR
209 /* let audio thread run and wait for the mas to run out of data */
210 while (!mp3_pause_done())
211 #endif
212 sleep(HZ/10);
213 #endif
215 /* reset volume to what it was before the fade */
216 sound_set_volume(global_settings.volume);
220 static bool update_onvol_change(struct gui_wps * gwps)
222 skin_update(gwps, WPS_REFRESH_NON_STATIC);
224 #ifdef HAVE_LCD_CHARCELLS
225 splashf(0, "Vol: %3d dB",
226 sound_val2phys(SOUND_VOLUME, global_settings.volume));
227 return true;
228 #endif
229 return false;
233 bool ffwd_rew(int button)
235 unsigned int step = 0; /* current ff/rewind step */
236 unsigned int max_step = 0; /* maximum ff/rewind step */
237 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
238 int direction = -1; /* forward=1 or backward=-1 */
239 bool exit = false;
240 bool usb = false;
241 int i = 0;
242 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
244 if (button == ACTION_NONE)
246 status_set_ffmode(0);
247 return usb;
249 while (!exit)
251 switch ( button )
253 case ACTION_WPS_SEEKFWD:
254 direction = 1;
255 case ACTION_WPS_SEEKBACK:
256 if (wps_state.ff_rewind)
258 if (direction == 1)
260 /* fast forwarding, calc max step relative to end */
261 max_step = (wps_state.id3->length -
262 (wps_state.id3->elapsed +
263 ff_rewind_count)) *
264 FF_REWIND_MAX_PERCENT / 100;
266 else
268 /* rewinding, calc max step relative to start */
269 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
270 FF_REWIND_MAX_PERCENT / 100;
273 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
275 if (step > max_step)
276 step = max_step;
278 ff_rewind_count += step * direction;
280 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
281 step += step >> ff_rw_accel;
283 else
285 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
286 wps_state.id3 && wps_state.id3->length )
288 if (!wps_state.paused)
289 #if (CONFIG_CODEC == SWCODEC)
290 audio_pre_ff_rewind();
291 #else
292 audio_pause();
293 #endif
294 #if CONFIG_KEYPAD == PLAYER_PAD
295 FOR_NB_SCREENS(i)
296 gui_wps[i].display->stop_scroll();
297 #endif
298 if (direction > 0)
299 status_set_ffmode(STATUS_FASTFORWARD);
300 else
301 status_set_ffmode(STATUS_FASTBACKWARD);
303 wps_state.ff_rewind = true;
305 step = 1000 * global_settings.ff_rewind_min_step;
307 else
308 break;
311 if (direction > 0) {
312 if ((wps_state.id3->elapsed + ff_rewind_count) >
313 wps_state.id3->length)
314 ff_rewind_count = wps_state.id3->length -
315 wps_state.id3->elapsed;
317 else {
318 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
319 ff_rewind_count = -wps_state.id3->elapsed;
322 /* set the wps state ff_rewind_count so the progess info
323 displays corectly */
324 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
325 ff_rewind_count:-ff_rewind_count;
326 FOR_NB_SCREENS(i)
328 skin_update(&gui_wps[i],
329 WPS_REFRESH_PLAYER_PROGRESS |
330 WPS_REFRESH_DYNAMIC);
333 break;
335 case ACTION_WPS_STOPSEEK:
336 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
337 audio_ff_rewind(wps_state.id3->elapsed);
338 wps_state.ff_rewind_count = 0;
339 wps_state.ff_rewind = false;
340 status_set_ffmode(0);
341 #if (CONFIG_CODEC != SWCODEC)
342 if (!wps_state.paused)
343 audio_resume();
344 #endif
345 #ifdef HAVE_LCD_CHARCELLS
346 FOR_NB_SCREENS(i)
347 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
348 #endif
349 exit = true;
350 break;
352 default:
353 if(default_event_handler(button) == SYS_USB_CONNECTED) {
354 status_set_ffmode(0);
355 usb = true;
356 exit = true;
358 break;
360 if (!exit)
362 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
363 #ifdef HAVE_TOUCHSCREEN
364 if (button == ACTION_TOUCHSCREEN)
365 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
366 #endif
369 return usb;
373 void display_keylock_text(bool locked)
375 int i;
376 FOR_NB_SCREENS(i)
377 gui_wps[i].display->stop_scroll();
379 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
385 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
386 static void gwps_caption_backlight(struct wps_state *state)
388 if (state && state->id3)
390 #ifdef HAVE_BACKLIGHT
391 if (global_settings.caption_backlight)
393 /* turn on backlight n seconds before track ends, and turn it off n
394 seconds into the new track. n == backlight_timeout, or 5s */
395 int n = global_settings.backlight_timeout * 1000;
397 if ( n < 1000 )
398 n = 5000; /* use 5s if backlight is always on or off */
400 if (((state->id3->elapsed < 1000) ||
401 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
402 (state->paused == false))
403 backlight_on();
405 #endif
406 #ifdef HAVE_REMOTE_LCD
407 if (global_settings.remote_caption_backlight)
409 /* turn on remote backlight n seconds before track ends, and turn it
410 off n seconds into the new track. n == remote_backlight_timeout,
411 or 5s */
412 int n = global_settings.remote_backlight_timeout * 1000;
414 if ( n < 1000 )
415 n = 5000; /* use 5s if backlight is always on or off */
417 if (((state->id3->elapsed < 1000) ||
418 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
419 (state->paused == false))
420 remote_backlight_on();
422 #endif
425 #endif
428 static void change_dir(int direction)
430 if (global_settings.prevent_skip)
431 return;
433 if (direction < 0)
434 audio_prev_dir();
435 else if (direction > 0)
436 audio_next_dir();
437 /* prevent the next dir to immediatly start being ffw'd */
438 action_wait_for_release();
441 static void prev_track(unsigned long skip_thresh)
443 if (wps_state.id3->elapsed < skip_thresh)
445 audio_prev();
446 return;
448 else
450 if (wps_state.id3->cuesheet)
452 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
453 return;
456 if (!wps_state.paused)
457 #if (CONFIG_CODEC == SWCODEC)
458 audio_pre_ff_rewind();
459 #else
460 audio_pause();
461 #endif
463 audio_ff_rewind(0);
465 #if (CONFIG_CODEC != SWCODEC)
466 if (!wps_state.paused)
467 audio_resume();
468 #endif
472 static void next_track(void)
474 /* take care of if we're playing a cuesheet */
475 if (wps_state.id3->cuesheet)
477 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
479 /* if the result was false, then we really want
480 to skip to the next track */
481 return;
485 audio_next();
488 static void play_hop(int direction)
490 long step = global_settings.skip_length*1000;
491 long elapsed = wps_state.id3->elapsed;
492 long remaining = wps_state.id3->length - elapsed;
494 if (step < 0)
496 if (direction < 0)
498 prev_track(DEFAULT_SKIP_TRESH);
499 return;
501 else if (remaining < DEFAULT_SKIP_TRESH*2)
503 next_track();
504 return;
506 else
507 elapsed += (remaining - DEFAULT_SKIP_TRESH*2);
509 else if (!global_settings.prevent_skip &&
510 (!step ||
511 (direction > 0 && step >= remaining) ||
512 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
513 { /* Do normal track skipping */
514 if (direction > 0)
515 next_track();
516 else if (direction < 0)
517 prev_track(DEFAULT_SKIP_TRESH);
518 return;
520 else if (direction == 1 && step >= remaining)
522 #if CONFIG_CODEC == SWCODEC
523 if(global_settings.beep)
524 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
525 #endif
526 return;
528 else if ((direction == -1 && elapsed < step))
530 elapsed = 0;
532 else
534 elapsed += step * direction;
536 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
538 #if (CONFIG_CODEC == SWCODEC)
539 audio_pre_ff_rewind();
540 #else
541 audio_pause();
542 #endif
544 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
545 #if (CONFIG_CODEC != SWCODEC)
546 if (!wps_state.paused)
547 audio_resume();
548 #endif
552 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
554 * If the user is unable to see the wps, because the display is deactivated,
555 * we suppress updates until the wps is activated again (the lcd driver will
556 * call this hook to issue an instant update)
557 * */
558 static void wps_lcd_activation_hook(void *param)
560 (void)param;
561 wps_sync_data.do_full_update = true;
562 /* force timeout in wps main loop, so that the update is instantly */
563 queue_post(&button_queue, BUTTON_NONE, 0);
565 #endif
567 static void gwps_leave_wps(void)
569 int i, oldbars = VP_SB_HIDE_ALL;
571 FOR_NB_SCREENS(i)
573 gui_wps[i].display->stop_scroll();
574 gui_wps[i].display->backdrop_show(BACKDROP_MAIN);
575 if (statusbar_position(i) != STATUSBAR_OFF)
576 oldbars |= VP_SB_ONSCREEN(i);
579 viewportmanager_set_statusbar(oldbars);
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);
586 send_event(GUI_EVENT_REFRESH, NULL);
589 #ifdef HAVE_TOUCHSCREEN
590 int wps_get_touchaction(struct wps_data *data)
592 short x,y;
593 short vx, vy;
594 int type = action_get_touchscreen_press(&x, &y);
595 static int last_action = ACTION_NONE;
596 struct touchregion *r;
597 bool repeated = (type == BUTTON_REPEAT);
598 bool released = (type == BUTTON_REL);
599 struct skin_token_list *regions = data->touchregions;
600 while (regions)
602 r = (struct touchregion *)regions->token->value.data;
603 /* make sure this region's viewport is visible */
604 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
606 regions = regions->next;
607 continue;
609 /* check if it's inside this viewport */
610 if (viewport_point_within_vp(&(r->wvp->vp), x, y))
611 { /* reposition the touch inside the viewport since touchregions
612 * are relative to a preceding viewport */
613 vx = x - r->wvp->vp.x;
614 vy = y - r->wvp->vp.y;
615 /* now see if the point is inside this region */
616 if (vx >= r->x && vx < r->x+r->width &&
617 vy >= r->y && vy < r->y+r->height)
619 /* reposition the touch within the area */
620 vx -= r->x;
621 vy -= r->y;
623 switch(r->type)
625 case WPS_TOUCHREGION_ACTION:
626 if ((repeated && r->repeat) || (released && !r->repeat))
628 last_action = r->action;
629 return r->action;
631 break;
632 case WPS_TOUCHREGION_SCROLLBAR:
633 if(r->width > r->height)
634 /* landscape */
635 wps_state.id3->elapsed = (vx *
636 wps_state.id3->length) / r->width;
637 else
638 /* portrait */
639 wps_state.id3->elapsed = (vy *
640 wps_state.id3->length) / r->height;
642 if (!wps_state.paused)
643 #if (CONFIG_CODEC == SWCODEC)
644 audio_pre_ff_rewind();
645 #else
646 audio_pause();
647 #endif
648 audio_ff_rewind(wps_state.id3->elapsed);
649 #if (CONFIG_CODEC != SWCODEC)
650 if (!wps_state.paused)
651 audio_resume();
652 #endif
653 break;
654 case WPS_TOUCHREGION_VOLUME:
656 const int min_vol = sound_min(SOUND_VOLUME);
657 const int max_vol = sound_max(SOUND_VOLUME);
658 if(r->width > r->height)
659 /* landscape */
660 global_settings.volume = (vx *
661 (max_vol - min_vol)) / r->width;
662 else
663 /* portrait */
664 global_settings.volume = (vy *
665 (max_vol-min_vol)) / r->height;
667 global_settings.volume += min_vol;
668 setvol();
669 return ACTION_REDRAW;
674 regions = regions->next;
677 if ((last_action == ACTION_WPS_SEEKBACK || last_action == ACTION_WPS_SEEKFWD))
678 return ACTION_WPS_STOPSEEK;
679 last_action = ACTION_TOUCHSCREEN;
680 return ACTION_TOUCHSCREEN;
682 #endif
683 /* The WPS can be left in two ways:
684 * a) call a function, which draws over the wps. In this case, the wps
685 * will be still active (i.e. the below function didn't return)
686 * b) return with a value evaluated by root_menu.c, in this case the wps
687 * is really left, and root_menu will handle the next screen
689 * In either way, call gwps_leave_wps(), in order to restore the correct
690 * "main screen" backdrops and statusbars
692 long gui_wps_show(void)
694 long button = 0;
695 bool restore = true;
696 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
697 bool exit = false;
698 bool bookmark = false;
699 bool update = false;
700 bool vol_changed = false;
701 int i;
702 long last_left = 0, last_right = 0;
704 #ifdef HAVE_LCD_CHARCELLS
705 status_set_audio(true);
706 status_set_param(false);
707 #else
708 statusbar_toggle_handler(NULL);
709 #endif
711 #ifdef AB_REPEAT_ENABLE
712 ab_repeat_init();
713 ab_reset_markers();
714 #endif
715 wps_state_init();
717 while ( 1 )
719 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
721 /* did someone else (i.e power thread) change audio pause mode? */
722 if (wps_state.paused != audio_paused) {
723 wps_state.paused = audio_paused;
725 /* if another thread paused audio, we are probably in car mode,
726 about to shut down. lets save the settings. */
727 if (wps_state.paused) {
728 settings_save();
729 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
730 call_storage_idle_notifys(true);
731 #endif
734 #ifdef HAVE_LCD_BITMAP
735 /* when the peak meter is enabled we want to have a
736 few extra updates to make it look smooth. On the
737 other hand we don't want to waste energy if it
738 isn't displayed */
739 bool pm=false;
740 FOR_NB_SCREENS(i)
742 if(gui_wps[i].data->peak_meter_enabled)
743 pm = true;
746 if (pm) {
747 long next_refresh = current_tick;
748 long next_big_refresh = current_tick + HZ / 5;
749 button = BUTTON_NONE;
750 while (TIME_BEFORE(current_tick, next_big_refresh)) {
751 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_NOBLOCK);
752 if (button != ACTION_NONE) {
753 break;
755 peak_meter_peek();
756 sleep(0); /* Sleep until end of current tick. */
758 if (TIME_AFTER(current_tick, next_refresh)) {
759 FOR_NB_SCREENS(i)
761 if(gui_wps[i].data->peak_meter_enabled)
762 skin_update(&gui_wps[i], WPS_REFRESH_PEAK_METER);
763 next_refresh += HZ / PEAK_METER_FPS;
770 /* The peak meter is disabled
771 -> no additional screen updates needed */
772 else
773 #endif
775 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,
776 restore ? HZ/100 : HZ/5);
779 /* Exit if audio has stopped playing. This happens e.g. at end of
780 playlist or if using the sleep timer. */
781 if (!(audio_status() & AUDIO_STATUS_PLAY))
782 exit = true;
783 #ifdef HAVE_TOUCHSCREEN
784 if (button == ACTION_TOUCHSCREEN)
785 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
786 #endif
787 /* The iPods/X5/M5 use a single button for the A-B mode markers,
788 defined as ACTION_WPSAB_SINGLE in their config files. */
789 #ifdef ACTION_WPSAB_SINGLE
790 if (!global_settings.party_mode && ab_repeat_mode_enabled())
792 static int wps_ab_state = 0;
793 if (button == ACTION_WPSAB_SINGLE)
795 switch (wps_ab_state)
797 case 0: /* set the A spot */
798 button = ACTION_WPS_ABSETA_PREVDIR;
799 break;
800 case 1: /* set the B spot */
801 button = ACTION_WPS_ABSETB_NEXTDIR;
802 break;
803 case 2:
804 button = ACTION_WPS_ABRESET;
805 break;
807 wps_ab_state = (wps_ab_state+1) % 3;
810 #endif
811 switch(button)
813 case ACTION_WPS_CONTEXT:
815 gwps_leave_wps();
816 /* if music is stopped in the context menu we want to exit the wps */
817 if (onplay(wps_state.id3->path,
818 FILE_ATTR_AUDIO, CONTEXT_WPS) == ONPLAY_MAINMENU
819 || !audio_status())
820 return GO_TO_ROOT;
821 restore = true;
823 break;
825 case ACTION_WPS_BROWSE:
826 #ifdef HAVE_LCD_CHARCELLS
827 status_set_record(false);
828 status_set_audio(false);
829 #endif
830 gwps_leave_wps();
831 return GO_TO_PREVIOUS_BROWSER;
832 break;
834 /* play/pause */
835 case ACTION_WPS_PLAY:
836 if (global_settings.party_mode)
837 break;
838 if ( wps_state.paused )
840 wps_state.paused = false;
841 if ( global_settings.fade_on_stop )
842 fade(true, true);
843 else
844 audio_resume();
846 else
848 wps_state.paused = true;
849 if ( global_settings.fade_on_stop )
850 fade(false, true);
851 else
852 audio_pause();
853 settings_save();
854 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
855 call_storage_idle_notifys(true); /* make sure resume info is saved */
856 #endif
858 break;
860 case ACTION_WPS_VOLUP:
861 global_settings.volume++;
862 vol_changed = true;
863 break;
864 case ACTION_WPS_VOLDOWN:
865 global_settings.volume--;
866 vol_changed = true;
867 break;
868 /* fast forward
869 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
870 case ACTION_WPS_SEEKFWD:
871 if (global_settings.party_mode)
872 break;
873 if (current_tick -last_right < HZ)
875 if (wps_state.id3->cuesheet)
877 audio_next();
879 else
881 change_dir(1);
884 else
885 ffwd_rew(ACTION_WPS_SEEKFWD);
886 last_right = last_left = 0;
887 break;
888 /* fast rewind
889 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
890 case ACTION_WPS_SEEKBACK:
891 if (global_settings.party_mode)
892 break;
893 if (current_tick -last_left < HZ)
895 if (wps_state.id3->cuesheet)
897 if (!wps_state.paused)
898 #if (CONFIG_CODEC == SWCODEC)
899 audio_pre_ff_rewind();
900 #else
901 audio_pause();
902 #endif
903 audio_ff_rewind(0);
905 else
907 change_dir(-1);
910 else
911 ffwd_rew(ACTION_WPS_SEEKBACK);
912 last_left = last_right = 0;
913 break;
915 /* prev / restart */
916 case ACTION_WPS_SKIPPREV:
917 if (global_settings.party_mode)
918 break;
919 last_left = current_tick;
920 #ifdef AB_REPEAT_ENABLE
921 /* if we're in A/B repeat mode and the current position
922 is past the A marker, jump back to the A marker... */
923 if ( ab_repeat_mode_enabled() )
925 if ( ab_after_A_marker(wps_state.id3->elapsed) )
927 ab_jump_to_A_marker();
928 break;
929 #if (AB_REPEAT_ENABLE == 2)
930 } else {
931 ab_reset_markers();
932 #endif
935 else
936 /* ...otherwise, do it normally */
937 #endif
938 play_hop(-1);
939 break;
941 /* next
942 OR if skip length set, hop by predetermined amount. */
943 case ACTION_WPS_SKIPNEXT:
944 if (global_settings.party_mode)
945 break;
946 last_right = current_tick;
947 #ifdef AB_REPEAT_ENABLE
948 /* if we're in A/B repeat mode and the current position is
949 before the A marker, jump to the A marker... */
950 if ( ab_repeat_mode_enabled() )
952 if ( ab_before_A_marker(wps_state.id3->elapsed) )
954 ab_jump_to_A_marker();
955 break;
956 #if (AB_REPEAT_ENABLE == 2)
957 } else {
958 ab_reset_markers();
959 #endif
962 else
963 /* ...otherwise, do it normally */
964 #endif
965 play_hop(1);
966 break;
967 /* next / prev directories */
968 /* and set A-B markers if in a-b mode */
969 case ACTION_WPS_ABSETB_NEXTDIR:
970 if (global_settings.party_mode)
971 break;
972 #if defined(AB_REPEAT_ENABLE)
973 if (ab_repeat_mode_enabled())
975 ab_set_B_marker(wps_state.id3->elapsed);
976 ab_jump_to_A_marker();
978 else
979 #endif
981 change_dir(1);
983 break;
984 case ACTION_WPS_ABSETA_PREVDIR:
985 if (global_settings.party_mode)
986 break;
987 #if defined(AB_REPEAT_ENABLE)
988 if (ab_repeat_mode_enabled())
989 ab_set_A_marker(wps_state.id3->elapsed);
990 else
991 #endif
993 change_dir(-1);
995 break;
996 /* menu key functions */
997 case ACTION_WPS_MENU:
998 gwps_leave_wps();
999 return GO_TO_ROOT;
1000 break;
1003 #ifdef HAVE_QUICKSCREEN
1004 case ACTION_WPS_QUICKSCREEN:
1006 gwps_leave_wps();
1007 if (quick_screen_quick(button))
1008 return GO_TO_ROOT;
1009 restore = true;
1011 break;
1012 #endif /* HAVE_QUICKSCREEN */
1014 /* screen settings */
1015 #ifdef BUTTON_F3
1016 case ACTION_F3:
1018 gwps_leave_wps();
1019 if (quick_screen_f3(BUTTON_F3))
1020 return GO_TO_ROOT;
1021 restore = true;
1023 break;
1024 #endif /* BUTTON_F3 */
1026 /* pitch screen */
1027 #ifdef HAVE_PITCHSCREEN
1028 case ACTION_WPS_PITCHSCREEN:
1030 gwps_leave_wps();
1031 if (1 == gui_syncpitchscreen_run())
1032 return GO_TO_ROOT;
1033 restore = true;
1035 break;
1036 #endif /* HAVE_PITCHSCREEN */
1038 #ifdef AB_REPEAT_ENABLE
1039 /* reset A&B markers */
1040 case ACTION_WPS_ABRESET:
1041 if (ab_repeat_mode_enabled())
1043 ab_reset_markers();
1044 update = true;
1046 break;
1047 #endif /* AB_REPEAT_ENABLE */
1049 /* stop and exit wps */
1050 case ACTION_WPS_STOP:
1051 if (global_settings.party_mode)
1052 break;
1053 bookmark = true;
1054 exit = true;
1055 break;
1057 case ACTION_WPS_ID3SCREEN:
1059 gwps_leave_wps();
1060 if (browse_id3())
1061 return GO_TO_ROOT;
1062 restore = true;
1064 break;
1065 #ifdef HAVE_TOUCHSCREEN
1066 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1068 global_settings.playlist_shuffle =
1069 !global_settings.playlist_shuffle;
1070 #if CONFIG_CODEC == SWCODEC
1071 dsp_set_replaygain();
1072 #endif
1073 if (global_settings.playlist_shuffle)
1074 playlist_randomise(NULL, current_tick, true);
1075 else
1076 playlist_sort(NULL, true);
1078 break;
1079 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1081 const struct settings_list *rep_setting =
1082 find_setting(&global_settings.repeat_mode, NULL);
1083 option_select_next_val(rep_setting, false, true);
1084 audio_flush_and_reload_tracks();
1086 break;
1087 #endif /* HAVE_TOUCHSCREEN */
1088 /* this case is used by the softlock feature
1089 * it requests a full update here */
1090 case ACTION_REDRAW:
1091 wps_sync_data.do_full_update = true;
1092 break;
1093 case ACTION_NONE: /* Timeout, do a partial update */
1094 update = true;
1095 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1096 break;
1097 #ifdef HAVE_RECORDING
1098 case ACTION_WPS_REC:
1099 exit = true;
1100 break;
1101 #endif
1102 case SYS_POWEROFF:
1103 default_event_handler(SYS_POWEROFF);
1104 break;
1105 case ACTION_WPS_VIEW_PLAYLIST:
1106 gwps_leave_wps();
1107 if (playlist_viewer()) /* true if USB connected */
1108 return GO_TO_ROOT;
1109 restore = true;
1110 break;
1111 default:
1112 if(default_event_handler(button) == SYS_USB_CONNECTED)
1114 gwps_leave_wps();
1115 return GO_TO_ROOT;
1117 update = true;
1118 break;
1121 if (vol_changed)
1123 bool res = false;
1124 setvol();
1125 FOR_NB_SCREENS(i)
1127 if(update_onvol_change(&gui_wps[i]))
1128 res = true;
1130 if (res) {
1131 restore = true;
1132 restoretimer = RESTORE_WPS_NEXT_SECOND;
1136 if (wps_sync_data.do_full_update || update)
1138 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1139 gwps_caption_backlight(&wps_state);
1140 #endif
1141 FOR_NB_SCREENS(i)
1143 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1144 /* currently, all remotes are readable without backlight
1145 * so still update those */
1146 if (lcd_active() || (i != SCREEN_MAIN))
1147 #endif
1149 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
1152 wps_sync_data.do_full_update = false;
1153 update = false;
1156 if (restore && wps_state.id3 &&
1157 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1158 TIME_AFTER(current_tick, restoretimer)))
1160 restore = false;
1161 restoretimer = RESTORE_WPS_INSTANTLY;
1162 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1163 add_event(LCD_EVENT_ACTIVATION, false, wps_lcd_activation_hook);
1164 #endif
1165 /* we remove the update delay since it's not very usable in the wps,
1166 * e.g. during volume changing or ffwd/rewind */
1167 sb_skin_set_update_delay(0);
1168 FOR_NB_SCREENS(i)
1170 screens[i].stop_scroll();
1171 gui_wps_display(&gui_wps[i]);
1175 if (exit) {
1176 #ifdef HAVE_LCD_CHARCELLS
1177 status_set_record(false);
1178 status_set_audio(false);
1179 #endif
1180 if (global_settings.fade_on_stop)
1181 fade(false, true);
1183 if (bookmark)
1184 bookmark_autobookmark();
1185 audio_stop();
1186 #ifdef AB_REPEAT_ENABLE
1187 ab_reset_markers();
1188 #endif
1189 gwps_leave_wps();
1190 #ifdef HAVE_RECORDING
1191 if (button == ACTION_WPS_REC)
1192 return GO_TO_RECSCREEN;
1193 #endif
1194 if (global_settings.browse_current)
1195 return GO_TO_PREVIOUS_BROWSER;
1196 return GO_TO_PREVIOUS;
1199 if (button && !IS_SYSEVENT(button) )
1200 storage_spin();
1202 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1205 /* this is called from the playback thread so NO DRAWING! */
1206 static void track_changed_callback(void *param)
1208 wps_state.id3 = (struct mp3entry*)param;
1209 wps_state.nid3 = audio_next_track();
1210 if (wps_state.id3->cuesheet)
1212 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1213 cue_spoof_id3(wps_state.id3->cuesheet, wps_state.id3);
1215 wps_sync_data.do_full_update = true;
1217 static void nextid3available_callback(void* param)
1219 (void)param;
1220 wps_state.nid3 = audio_next_track();
1221 wps_sync_data.do_full_update = true;
1225 static void wps_state_init(void)
1227 wps_state.ff_rewind = false;
1228 wps_state.paused = false;
1229 if(audio_status() & AUDIO_STATUS_PLAY)
1231 wps_state.id3 = audio_current_track();
1232 wps_state.nid3 = audio_next_track();
1234 else
1236 wps_state.id3 = NULL;
1237 wps_state.nid3 = NULL;
1239 /* We'll be updating due to restore initialized with true */
1240 wps_sync_data.do_full_update = false;
1241 /* add the WPS track event callbacks */
1242 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1243 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1247 #ifdef HAVE_LCD_BITMAP
1248 static void statusbar_toggle_handler(void *data)
1250 (void)data;
1251 int i;
1253 int *wpsbars = &wps_sync_data.statusbars;
1254 *wpsbars = VP_SB_HIDE_ALL;
1255 FOR_NB_SCREENS(i)
1256 { /* fix viewports if needed */
1257 skin_statusbar_changed(&gui_wps[i]);
1259 bool draw = false;
1261 /* fix up gui_wps::statusbars, so that the viewportmanager accepts it*/
1262 if (gui_wps[i].data->wps_sb_tag)
1263 draw = gui_wps[i].data->show_sb_on_wps;
1264 else if (statusbar_position(i) != STATUSBAR_OFF)
1265 draw = true;
1266 if (draw)
1267 *wpsbars |=
1268 (VP_SB_ONSCREEN(i) | VP_SB_IGNORE_SETTING(i));
1271 #endif
1274 void gui_sync_wps_init(void)
1276 int i;
1277 FOR_NB_SCREENS(i)
1279 #ifdef HAVE_ALBUMART
1280 wps_datas[i].albumart = NULL;
1281 wps_datas[i].playback_aa_slot = -1;
1282 #endif
1283 #ifdef HAVE_REMOTE_LCD
1284 wps_datas[i].remote_wps = (i == SCREEN_REMOTE);
1285 #endif
1286 gui_wps[i].data = &wps_datas[i];
1287 gui_wps[i].display = &screens[i];
1288 /* Currently no seperate wps_state needed/possible
1289 so use the only available ( "global" ) one */
1290 gui_wps[i].state = &wps_state;
1291 gui_wps[i].display->backdrop_unload(BACKDROP_SKIN_WPS);
1292 /* must point to the same struct for both screens */
1293 gui_wps[i].sync_data = &wps_sync_data;
1294 gui_wps[i].sync_data->statusbars = VP_SB_ALLSCREENS;
1296 #ifdef HAVE_LCD_BITMAP
1297 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggle_handler);
1298 #endif
1302 #ifdef IPOD_ACCESSORY_PROTOCOL
1303 bool is_wps_fading(void)
1305 return wps_state.is_fading;
1308 int wps_get_ff_rewind_count(void)
1310 return wps_state.ff_rewind_count;
1312 #endif