Remove wps_reset(), and rename skin_data_init to skin_data_reset() and extend it...
[kugel-rb.git] / apps / gui / wps.c
blob3524ba34b70afbc9096ec483abccdca998a405e5
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 "debug.h"
35 #include "sprintf.h"
36 #include "settings.h"
37 #include "skin_engine/skin_engine.h"
38 #include "mp3_playback.h"
39 #include "audio.h"
40 #include "usb.h"
41 #include "status.h"
42 #include "storage.h"
43 #include "screens.h"
44 #include "playlist.h"
45 #ifdef HAVE_LCD_BITMAP
46 #include "icons.h"
47 #include "peakmeter.h"
48 #endif
49 #include "lang.h"
50 #include "bookmark.h"
51 #include "misc.h"
52 #include "sound.h"
53 #include "onplay.h"
54 #include "abrepeat.h"
55 #include "playback.h"
56 #include "splash.h"
57 #include "cuesheet.h"
58 #include "ata_idle_notify.h"
59 #include "root_menu.h"
60 #include "backdrop.h"
61 #include "quickscreen.h"
62 #include "pitchscreen.h"
63 #include "appevents.h"
64 #include "viewport.h"
65 #include "pcmbuf.h"
66 #include "option_select.h"
67 #include "dsp.h"
68 #include "playlist_viewer.h"
69 #include "wps.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 3000ul
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 /* this is for the viewportmanager */
82 static int wpsbars = 0;
84 /* currently only one wps_state is needed, initialize to 0 */
85 static struct wps_state wps_state = { .id3 = NULL};
86 static struct gui_wps gui_wps[NB_SCREENS] = {{ 0 }};
87 static struct wps_data wps_datas[NB_SCREENS] = {{ 0 }};
89 /* initial setup of wps_data */
90 static void wps_state_init(void);
91 static void track_changed_callback(void *param);
92 static void nextid3available_callback(void* param);
93 #ifdef HAVE_LCD_BITMAP
94 static void statusbar_toggle_handler(void *data);
95 #endif
97 #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps"
98 #ifdef HAVE_REMOTE_LCD
99 #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps"
100 #define DEFAULT_WPS(screen) ((screen) == SCREEN_MAIN ? \
101 WPS_DEFAULTCFG:RWPS_DEFAULTCFG)
102 #else
103 #define DEFAULT_WPS(screen) (WPS_DEFAULTCFG)
104 #endif
106 void wps_data_load(enum screen_type screen, const char *buf, bool isfile)
108 bool loaded_ok;
110 screens[screen].backdrop_unload(BACKDROP_SKIN_WPS);
112 #ifndef __PCTOOL__
114 * Hardcode loading WPS_DEFAULTCFG to cause a reset ideally this
115 * wants to be a virtual file. Feel free to modify dirbrowse()
116 * if you're feeling brave.
119 if (buf && ! strcmp(buf, DEFAULT_WPS(screen)) )
121 #ifdef HAVE_REMOTE_LCD
122 if (screen == SCREEN_REMOTE)
123 global_settings.rwps_file[0] = '\0';
124 else
125 #endif
126 global_settings.wps_file[0] = '\0';
127 buf = NULL;
130 #endif /* __PCTOOL__ */
132 loaded_ok = buf && skin_data_load(gui_wps[screen].data,
133 &screens[screen], buf, isfile);
134 if (!loaded_ok) /* load the hardcoded default */
136 char *skin_buf[NB_SCREENS] = {
137 #ifdef HAVE_LCD_BITMAP
138 "%s%?it<%?in<%in. |>%it|%fn>\n"
139 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
140 "%s%?id<%id|%?d1<%d1|(root)>> %?iy<(%iy)|>\n\n"
141 "%al%pc/%pt%ar[%pp:%pe]\n"
142 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
143 "%pb\n%pm\n",
144 #else
145 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d2> - %?id<%id|%d1>\n"
146 "%pc%?ps<*|/>%pt\n",
147 #endif
148 #ifdef HAVE_REMOTE_LCD
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(gui_wps[screen].data, &screens[screen],
157 skin_buf[screen], false);
159 #ifdef HAVE_REMOVE_LCD
160 gui_wps[screen].data->remote_wps = !(screen == SCREEN_MAIN);
161 #endif
164 static bool wps_fading_out = false;
165 void fade(bool fade_in, bool updatewps)
167 int fp_global_vol = global_settings.volume << 8;
168 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
169 int fp_step = (fp_global_vol - fp_min_vol) / 30;
170 int i;
171 wps_fading_out = !fade_in;
172 if (fade_in) {
173 /* fade in */
174 int fp_volume = fp_min_vol;
176 /* zero out the sound */
177 sound_set_volume(fp_min_vol >> 8);
179 sleep(HZ/10); /* let audio thread run */
180 audio_resume();
182 while (fp_volume < fp_global_vol - fp_step) {
183 fp_volume += fp_step;
184 sound_set_volume(fp_volume >> 8);
185 if (updatewps)
187 FOR_NB_SCREENS(i)
188 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
190 sleep(1);
192 sound_set_volume(global_settings.volume);
194 else {
195 /* fade out */
196 int fp_volume = fp_global_vol;
198 while (fp_volume > fp_min_vol + fp_step) {
199 fp_volume -= fp_step;
200 sound_set_volume(fp_volume >> 8);
201 if (updatewps)
203 FOR_NB_SCREENS(i)
204 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
206 sleep(1);
208 audio_pause();
209 wps_fading_out = false;
210 #if CONFIG_CODEC != SWCODEC
211 #ifndef SIMULATOR
212 /* let audio thread run and wait for the mas to run out of data */
213 while (!mp3_pause_done())
214 #endif
215 sleep(HZ/10);
216 #endif
218 /* reset volume to what it was before the fade */
219 sound_set_volume(global_settings.volume);
222 bool is_wps_fading(void)
224 return wps_fading_out;
227 static bool update_onvol_change(struct gui_wps * gwps)
229 skin_update(gwps, WPS_REFRESH_NON_STATIC);
231 #ifdef HAVE_LCD_CHARCELLS
232 splashf(0, "Vol: %3d dB",
233 sound_val2phys(SOUND_VOLUME, global_settings.volume));
234 return true;
235 #endif
236 return false;
240 bool ffwd_rew(int button)
242 unsigned int step = 0; /* current ff/rewind step */
243 unsigned int max_step = 0; /* maximum ff/rewind step */
244 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
245 int direction = -1; /* forward=1 or backward=-1 */
246 bool exit = false;
247 bool usb = false;
248 int i = 0;
249 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
251 if (button == ACTION_NONE)
253 status_set_ffmode(0);
254 return usb;
256 while (!exit)
258 switch ( button )
260 case ACTION_WPS_SEEKFWD:
261 direction = 1;
262 case ACTION_WPS_SEEKBACK:
263 if (wps_state.ff_rewind)
265 if (direction == 1)
267 /* fast forwarding, calc max step relative to end */
268 max_step = (wps_state.id3->length -
269 (wps_state.id3->elapsed +
270 ff_rewind_count)) *
271 FF_REWIND_MAX_PERCENT / 100;
273 else
275 /* rewinding, calc max step relative to start */
276 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
277 FF_REWIND_MAX_PERCENT / 100;
280 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
282 if (step > max_step)
283 step = max_step;
285 ff_rewind_count += step * direction;
287 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
288 step += step >> ff_rw_accel;
290 else
292 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
293 wps_state.id3 && wps_state.id3->length )
295 if (!wps_state.paused)
296 #if (CONFIG_CODEC == SWCODEC)
297 audio_pre_ff_rewind();
298 #else
299 audio_pause();
300 #endif
301 #if CONFIG_KEYPAD == PLAYER_PAD
302 FOR_NB_SCREENS(i)
303 gui_wps[i].display->stop_scroll();
304 #endif
305 if (direction > 0)
306 status_set_ffmode(STATUS_FASTFORWARD);
307 else
308 status_set_ffmode(STATUS_FASTBACKWARD);
310 wps_state.ff_rewind = true;
312 step = 1000 * global_settings.ff_rewind_min_step;
314 else
315 break;
318 if (direction > 0) {
319 if ((wps_state.id3->elapsed + ff_rewind_count) >
320 wps_state.id3->length)
321 ff_rewind_count = wps_state.id3->length -
322 wps_state.id3->elapsed;
324 else {
325 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
326 ff_rewind_count = -wps_state.id3->elapsed;
329 /* set the wps state ff_rewind_count so the progess info
330 displays corectly */
331 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
332 ff_rewind_count:-ff_rewind_count;
333 FOR_NB_SCREENS(i)
335 skin_update(&gui_wps[i],
336 WPS_REFRESH_PLAYER_PROGRESS |
337 WPS_REFRESH_DYNAMIC);
340 break;
342 case ACTION_WPS_STOPSEEK:
343 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
344 audio_ff_rewind(wps_state.id3->elapsed);
345 wps_state.ff_rewind_count = 0;
346 wps_state.ff_rewind = false;
347 status_set_ffmode(0);
348 #if (CONFIG_CODEC != SWCODEC)
349 if (!wps_state.paused)
350 audio_resume();
351 #endif
352 #ifdef HAVE_LCD_CHARCELLS
353 FOR_NB_SCREENS(i)
354 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
355 #endif
356 exit = true;
357 break;
359 default:
360 if(default_event_handler(button) == SYS_USB_CONNECTED) {
361 status_set_ffmode(0);
362 usb = true;
363 exit = true;
365 break;
367 if (!exit)
369 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
370 #ifdef HAVE_TOUCHSCREEN
371 if (button == ACTION_TOUCHSCREEN)
372 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
373 #endif
376 return usb;
380 void display_keylock_text(bool locked)
382 int i;
383 FOR_NB_SCREENS(i)
384 gui_wps[i].display->stop_scroll();
386 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
392 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
393 static void gwps_caption_backlight(struct wps_state *state)
395 if (state && state->id3)
397 #ifdef HAVE_BACKLIGHT
398 if (global_settings.caption_backlight)
400 /* turn on backlight n seconds before track ends, and turn it off n
401 seconds into the new track. n == backlight_timeout, or 5s */
402 int n = global_settings.backlight_timeout * 1000;
404 if ( n < 1000 )
405 n = 5000; /* use 5s if backlight is always on or off */
407 if (((state->id3->elapsed < 1000) ||
408 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
409 (state->paused == false))
410 backlight_on();
412 #endif
413 #ifdef HAVE_REMOTE_LCD
414 if (global_settings.remote_caption_backlight)
416 /* turn on remote backlight n seconds before track ends, and turn it
417 off n seconds into the new track. n == remote_backlight_timeout,
418 or 5s */
419 int n = global_settings.remote_backlight_timeout * 1000;
421 if ( n < 1000 )
422 n = 5000; /* use 5s if backlight is always on or off */
424 if (((state->id3->elapsed < 1000) ||
425 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
426 (state->paused == false))
427 remote_backlight_on();
429 #endif
432 #endif
435 static void change_dir(int direction)
437 if (global_settings.prevent_skip)
438 return;
440 if (direction < 0)
441 audio_prev_dir();
442 else if (direction > 0)
443 audio_next_dir();
444 /* prevent the next dir to immediatly start being ffw'd */
445 action_wait_for_release();
448 static void prev_track(unsigned long skip_thresh)
450 if (wps_state.id3->elapsed < skip_thresh)
452 audio_prev();
453 return;
455 else
457 if (wps_state.id3->cuesheet)
459 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
460 return;
463 if (!wps_state.paused)
464 #if (CONFIG_CODEC == SWCODEC)
465 audio_pre_ff_rewind();
466 #else
467 audio_pause();
468 #endif
470 audio_ff_rewind(0);
472 #if (CONFIG_CODEC != SWCODEC)
473 if (!wps_state.paused)
474 audio_resume();
475 #endif
479 static void next_track(void)
481 /* take care of if we're playing a cuesheet */
482 if (wps_state.id3->cuesheet)
484 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
486 /* if the result was false, then we really want
487 to skip to the next track */
488 return;
492 audio_next();
495 static void play_hop(int direction)
497 unsigned long step = ((unsigned long)global_settings.skip_length)*1000;
498 unsigned long elapsed = wps_state.id3->elapsed;
499 unsigned long remaining = wps_state.id3->length - elapsed;
501 if (!global_settings.prevent_skip &&
502 (!step ||
503 (direction > 0 && step >= remaining) ||
504 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
505 { /* Do normal track skipping */
506 if (direction > 0)
507 next_track();
508 else if (direction < 0)
509 prev_track(DEFAULT_SKIP_TRESH);
510 return;
513 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)
553 wps_state.do_full_update = true;
554 /* force timeout in wps main loop, so that the update is instantly */
555 queue_post(&button_queue, BUTTON_NONE, 0);
557 #endif
559 static void gwps_leave_wps(void)
561 int i, oldbars = VP_SB_HIDE_ALL;
563 FOR_NB_SCREENS(i)
565 gui_wps[i].display->stop_scroll();
566 gui_wps[i].display->backdrop_show(BACKDROP_MAIN);
567 if (statusbar_position(i) != STATUSBAR_OFF)
568 oldbars |= VP_SB_ONSCREEN(i);
571 viewportmanager_set_statusbar(oldbars);
572 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
573 /* Play safe and unregister the hook */
574 lcd_activation_set_hook(NULL);
575 #endif
576 send_event(GUI_EVENT_REFRESH, NULL);
579 #ifdef HAVE_TOUCHSCREEN
580 int wps_get_touchaction(struct wps_data *data)
582 short x,y;
583 short vx, vy;
584 int type = action_get_touchscreen_press(&x, &y);
585 static int last_action = ACTION_NONE;
586 struct touchregion *r;
587 bool repeated = (type == BUTTON_REPEAT);
588 bool released = (type == BUTTON_REL);
589 struct skin_token_list *regions = data->touchregions;
590 while (regions)
592 r = (struct touchregion *)regions->token->value.data;
593 /* make sure this region's viewport is visible */
594 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
596 regions = regions->next;
597 continue;
599 /* check if it's inside this viewport */
600 if (viewport_point_within_vp(&(r->wvp->vp), x, y))
601 { /* reposition the touch inside the viewport since touchregions
602 * are relative to a preceding viewport */
603 vx = x - r->wvp->vp.x;
604 vy = y - r->wvp->vp.y;
605 /* now see if the point is inside this region */
606 if (vx >= r->x && vx < r->x+r->width &&
607 vy >= r->y && vy < r->y+r->height)
609 /* reposition the touch within the area */
610 vx -= r->x;
611 vy -= r->y;
613 switch(r->type)
615 case WPS_TOUCHREGION_ACTION:
616 if ((repeated && r->repeat) || (released && !r->repeat))
618 last_action = r->action;
619 return r->action;
621 break;
622 case WPS_TOUCHREGION_SCROLLBAR:
623 if(r->width > r->height)
624 /* landscape */
625 wps_state.id3->elapsed = (vx *
626 wps_state.id3->length) / r->width;
627 else
628 /* portrait */
629 wps_state.id3->elapsed = (vy *
630 wps_state.id3->length) / r->height;
632 if (!wps_state.paused)
633 #if (CONFIG_CODEC == SWCODEC)
634 audio_pre_ff_rewind();
635 #else
636 audio_pause();
637 #endif
638 audio_ff_rewind(wps_state.id3->elapsed);
639 #if (CONFIG_CODEC != SWCODEC)
640 if (!wps_state.paused)
641 audio_resume();
642 #endif
643 break;
644 case WPS_TOUCHREGION_VOLUME:
646 int i;
647 const int min_vol = sound_min(SOUND_VOLUME);
648 const int max_vol = sound_max(SOUND_VOLUME);
649 if(r->width > r->height)
650 /* landscape */
651 global_settings.volume = (vx *
652 (max_vol - min_vol)) / r->width;
653 else
654 /* portrait */
655 global_settings.volume = (vy *
656 (max_vol-min_vol)) / r->height;
658 global_settings.volume += min_vol;
659 setvol();
660 FOR_NB_SCREENS(i)
662 gui_wps[i].data->button_time_volume = current_tick;
664 return ACTION_REDRAW;
669 regions = regions->next;
672 if ((last_action == ACTION_WPS_SEEKBACK || last_action == ACTION_WPS_SEEKFWD))
673 return ACTION_WPS_STOPSEEK;
674 last_action = ACTION_TOUCHSCREEN;
675 return ACTION_TOUCHSCREEN;
677 #endif
678 /* The WPS can be left in two ways:
679 * a) call a function, which draws over the wps. In this case, the wps
680 * will be still active (i.e. the below function didn't return)
681 * b) return with a value evaluated by root_menu.c, in this case the wps
682 * is really left, and root_menu will handle the next screen
684 * In either way, call gwps_leave_wps(), in order to restore the correct
685 * "main screen" backdrops and statusbars
687 long gui_wps_show(void)
689 long button = 0;
690 bool restore = true;
691 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
692 bool exit = false;
693 bool bookmark = false;
694 bool update = false;
695 int i;
696 long last_left = 0, last_right = 0;
698 #ifdef HAVE_LCD_CHARCELLS
699 status_set_audio(true);
700 status_set_param(false);
701 #else
702 statusbar_toggle_handler(NULL);
703 #endif
705 #ifdef AB_REPEAT_ENABLE
706 ab_repeat_init();
707 ab_reset_markers();
708 #endif
709 wps_state_init();
711 while ( 1 )
713 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
715 /* did someone else (i.e power thread) change audio pause mode? */
716 if (wps_state.paused != audio_paused) {
717 wps_state.paused = audio_paused;
719 /* if another thread paused audio, we are probably in car mode,
720 about to shut down. lets save the settings. */
721 if (wps_state.paused) {
722 settings_save();
723 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
724 call_storage_idle_notifys(true);
725 #endif
729 #ifdef HAVE_LCD_BITMAP
730 /* when the peak meter is enabled we want to have a
731 few extra updates to make it look smooth. On the
732 other hand we don't want to waste energy if it
733 isn't displayed */
734 bool pm=false;
735 FOR_NB_SCREENS(i)
737 if(gui_wps[i].data->peak_meter_enabled)
738 pm = true;
741 if (pm) {
742 long next_refresh = current_tick;
743 long next_big_refresh = current_tick + HZ / 5;
744 button = BUTTON_NONE;
745 while (TIME_BEFORE(current_tick, next_big_refresh)) {
746 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_NOBLOCK);
747 if (button != ACTION_NONE) {
748 break;
750 peak_meter_peek();
751 sleep(0); /* Sleep until end of current tick. */
753 if (TIME_AFTER(current_tick, next_refresh)) {
754 FOR_NB_SCREENS(i)
756 if(gui_wps[i].data->peak_meter_enabled)
757 skin_update(&gui_wps[i], WPS_REFRESH_PEAK_METER);
758 next_refresh += HZ / PEAK_METER_FPS;
765 /* The peak meter is disabled
766 -> no additional screen updates needed */
767 else
768 #endif
770 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,
771 restore ? HZ/100 : HZ/5);
774 /* Exit if audio has stopped playing. This happens e.g. at end of
775 playlist or if using the sleep timer. */
776 if (!(audio_status() & AUDIO_STATUS_PLAY))
777 exit = true;
778 #ifdef HAVE_TOUCHSCREEN
779 if (button == ACTION_TOUCHSCREEN)
780 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
781 #endif
782 /* The iPods/X5/M5 use a single button for the A-B mode markers,
783 defined as ACTION_WPSAB_SINGLE in their config files. */
784 #ifdef ACTION_WPSAB_SINGLE
785 if (!global_settings.party_mode && ab_repeat_mode_enabled())
787 static int wps_ab_state = 0;
788 if (button == ACTION_WPSAB_SINGLE)
790 switch (wps_ab_state)
792 case 0: /* set the A spot */
793 button = ACTION_WPS_ABSETA_PREVDIR;
794 break;
795 case 1: /* set the B spot */
796 button = ACTION_WPS_ABSETB_NEXTDIR;
797 break;
798 case 2:
799 button = ACTION_WPS_ABRESET;
800 break;
802 wps_ab_state = (wps_ab_state+1) % 3;
805 #endif
806 switch(button)
808 case ACTION_WPS_CONTEXT:
810 gwps_leave_wps();
811 /* if music is stopped in the context menu we want to exit the wps */
812 if (onplay(wps_state.id3->path,
813 FILE_ATTR_AUDIO, CONTEXT_WPS) == ONPLAY_MAINMENU
814 || !audio_status())
815 return GO_TO_ROOT;
816 restore = true;
818 break;
820 case ACTION_WPS_BROWSE:
821 #ifdef HAVE_LCD_CHARCELLS
822 status_set_record(false);
823 status_set_audio(false);
824 #endif
825 gwps_leave_wps();
826 return GO_TO_PREVIOUS_BROWSER;
827 break;
829 /* play/pause */
830 case ACTION_WPS_PLAY:
831 if (global_settings.party_mode)
832 break;
833 if ( wps_state.paused )
835 wps_state.paused = false;
836 if ( global_settings.fade_on_stop )
837 fade(true, true);
838 else
839 audio_resume();
841 else
843 wps_state.paused = true;
844 if ( global_settings.fade_on_stop )
845 fade(false, true);
846 else
847 audio_pause();
848 settings_save();
849 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
850 call_storage_idle_notifys(true); /* make sure resume info is saved */
851 #endif
853 break;
855 case ACTION_WPS_VOLUP:
857 FOR_NB_SCREENS(i)
858 gui_wps[i].data->button_time_volume = current_tick;
859 global_settings.volume++;
860 bool res = false;
861 setvol();
862 FOR_NB_SCREENS(i)
864 if(update_onvol_change(&gui_wps[i]))
865 res = true;
867 if (res) {
868 restore = true;
869 restoretimer = RESTORE_WPS_NEXT_SECOND;
872 break;
873 case ACTION_WPS_VOLDOWN:
875 FOR_NB_SCREENS(i)
876 gui_wps[i].data->button_time_volume = current_tick;
877 global_settings.volume--;
878 setvol();
879 bool res = false;
880 FOR_NB_SCREENS(i)
882 if(update_onvol_change(&gui_wps[i]))
883 res = true;
885 if (res) {
886 restore = true;
887 restoretimer = RESTORE_WPS_NEXT_SECOND;
890 break;
891 /* fast forward
892 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
893 case ACTION_WPS_SEEKFWD:
894 if (global_settings.party_mode)
895 break;
896 if (current_tick -last_right < HZ)
898 if (wps_state.id3->cuesheet)
900 audio_next();
902 else
904 change_dir(1);
907 else
908 ffwd_rew(ACTION_WPS_SEEKFWD);
909 last_right = last_left = 0;
910 break;
911 /* fast rewind
912 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
913 case ACTION_WPS_SEEKBACK:
914 if (global_settings.party_mode)
915 break;
916 if (current_tick -last_left < HZ)
918 if (wps_state.id3->cuesheet)
920 if (!wps_state.paused)
921 #if (CONFIG_CODEC == SWCODEC)
922 audio_pre_ff_rewind();
923 #else
924 audio_pause();
925 #endif
926 audio_ff_rewind(0);
928 else
930 change_dir(-1);
933 else
934 ffwd_rew(ACTION_WPS_SEEKBACK);
935 last_left = last_right = 0;
936 break;
938 /* prev / restart */
939 case ACTION_WPS_SKIPPREV:
940 if (global_settings.party_mode)
941 break;
942 last_left = current_tick;
943 #ifdef AB_REPEAT_ENABLE
944 /* if we're in A/B repeat mode and the current position
945 is past the A marker, jump back to the A marker... */
946 if ( ab_repeat_mode_enabled() )
948 if ( ab_after_A_marker(wps_state.id3->elapsed) )
950 ab_jump_to_A_marker();
951 break;
952 #if (AB_REPEAT_ENABLE == 2)
953 } else {
954 ab_reset_markers();
955 #endif
958 else
959 /* ...otherwise, do it normally */
960 #endif
961 play_hop(-1);
962 break;
964 /* next
965 OR if skip length set, hop by predetermined amount. */
966 case ACTION_WPS_SKIPNEXT:
967 if (global_settings.party_mode)
968 break;
969 last_right = current_tick;
970 #ifdef AB_REPEAT_ENABLE
971 /* if we're in A/B repeat mode and the current position is
972 before the A marker, jump to the A marker... */
973 if ( ab_repeat_mode_enabled() )
975 if ( ab_before_A_marker(wps_state.id3->elapsed) )
977 ab_jump_to_A_marker();
978 break;
979 #if (AB_REPEAT_ENABLE == 2)
980 } else {
981 ab_reset_markers();
982 #endif
985 else
986 /* ...otherwise, do it normally */
987 #endif
988 play_hop(1);
989 break;
990 /* next / prev directories */
991 /* and set A-B markers if in a-b mode */
992 case ACTION_WPS_ABSETB_NEXTDIR:
993 if (global_settings.party_mode)
994 break;
995 #if defined(AB_REPEAT_ENABLE)
996 if (ab_repeat_mode_enabled())
998 ab_set_B_marker(wps_state.id3->elapsed);
999 ab_jump_to_A_marker();
1001 else
1002 #endif
1004 change_dir(1);
1006 break;
1007 case ACTION_WPS_ABSETA_PREVDIR:
1008 if (global_settings.party_mode)
1009 break;
1010 #if defined(AB_REPEAT_ENABLE)
1011 if (ab_repeat_mode_enabled())
1012 ab_set_A_marker(wps_state.id3->elapsed);
1013 else
1014 #endif
1016 change_dir(-1);
1018 break;
1019 /* menu key functions */
1020 case ACTION_WPS_MENU:
1021 gwps_leave_wps();
1022 return GO_TO_ROOT;
1023 break;
1026 #ifdef HAVE_QUICKSCREEN
1027 case ACTION_WPS_QUICKSCREEN:
1029 gwps_leave_wps();
1030 if (quick_screen_quick(button))
1031 return GO_TO_ROOT;
1032 restore = true;
1034 break;
1035 #endif /* HAVE_QUICKSCREEN */
1037 /* screen settings */
1038 #ifdef BUTTON_F3
1039 case ACTION_F3:
1041 gwps_leave_wps();
1042 if (quick_screen_f3(BUTTON_F3))
1043 return GO_TO_ROOT;
1044 restore = true;
1046 break;
1047 #endif /* BUTTON_F3 */
1049 /* pitch screen */
1050 #ifdef HAVE_PITCHSCREEN
1051 case ACTION_WPS_PITCHSCREEN:
1053 gwps_leave_wps();
1054 if (1 == gui_syncpitchscreen_run())
1055 return GO_TO_ROOT;
1056 restore = true;
1058 break;
1059 #endif /* HAVE_PITCHSCREEN */
1061 #ifdef AB_REPEAT_ENABLE
1062 /* reset A&B markers */
1063 case ACTION_WPS_ABRESET:
1064 if (ab_repeat_mode_enabled())
1066 ab_reset_markers();
1067 update = true;
1069 break;
1070 #endif /* AB_REPEAT_ENABLE */
1072 /* stop and exit wps */
1073 case ACTION_WPS_STOP:
1074 if (global_settings.party_mode)
1075 break;
1076 bookmark = true;
1077 exit = true;
1078 break;
1080 case ACTION_WPS_ID3SCREEN:
1082 gwps_leave_wps();
1083 if (browse_id3())
1084 return GO_TO_ROOT;
1085 restore = true;
1087 break;
1088 #ifdef HAVE_TOUCHSCREEN
1089 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1091 global_settings.playlist_shuffle =
1092 !global_settings.playlist_shuffle;
1093 #if CONFIG_CODEC == SWCODEC
1094 dsp_set_replaygain();
1095 #endif
1096 if (global_settings.playlist_shuffle)
1097 playlist_randomise(NULL, current_tick, true);
1098 else
1099 playlist_sort(NULL, true);
1101 break;
1102 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1104 const struct settings_list *rep_setting =
1105 find_setting(&global_settings.repeat_mode, NULL);
1106 option_select_next_val(rep_setting, false, true);
1107 audio_flush_and_reload_tracks();
1109 break;
1110 #endif /* HAVE_TOUCHSCREEN */
1111 /* this case is used by the softlock feature
1112 * it requests a full update here */
1113 case ACTION_REDRAW:
1114 wps_state.do_full_update = true;
1115 break;
1116 case ACTION_NONE: /* Timeout, do a partial update */
1117 update = true;
1118 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1119 break;
1120 #ifdef HAVE_RECORDING
1121 case ACTION_WPS_REC:
1122 exit = true;
1123 break;
1124 #endif
1125 case SYS_POWEROFF:
1126 default_event_handler(SYS_POWEROFF);
1127 break;
1128 case ACTION_WPS_VIEW_PLAYLIST:
1129 gwps_leave_wps();
1130 if (playlist_viewer()) /* true if USB connected */
1131 return GO_TO_ROOT;
1132 restore = true;
1133 break;
1134 default:
1135 if(default_event_handler(button) == SYS_USB_CONNECTED)
1137 gwps_leave_wps();
1138 return GO_TO_ROOT;
1140 update = true;
1141 break;
1144 if (wps_state.do_full_update || update)
1146 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1147 gwps_caption_backlight(&wps_state);
1148 #endif
1149 FOR_NB_SCREENS(i)
1151 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1152 if (lcd_active()
1153 #ifdef HAVE_REMOTE_LCD
1154 /* currently, all remotes are readable without backlight
1155 * so still update those */
1156 || (i == SCREEN_REMOTE)
1157 #endif
1159 #endif
1161 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
1164 wps_state.do_full_update = false;
1165 update = false;
1168 if (restore && wps_state.id3 &&
1169 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1170 TIME_AFTER(current_tick, restoretimer)))
1172 restore = false;
1173 restoretimer = RESTORE_WPS_INSTANTLY;
1174 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1175 lcd_activation_set_hook(wps_lcd_activation_hook);
1176 #endif
1177 FOR_NB_SCREENS(i)
1179 screens[i].stop_scroll();
1180 gui_wps_display(&gui_wps[i]);
1184 if (exit) {
1185 #ifdef HAVE_LCD_CHARCELLS
1186 status_set_record(false);
1187 status_set_audio(false);
1188 #endif
1189 if (global_settings.fade_on_stop)
1190 fade(false, true);
1192 if (bookmark)
1193 bookmark_autobookmark();
1194 audio_stop();
1195 #ifdef AB_REPEAT_ENABLE
1196 ab_reset_markers();
1197 #endif
1198 gwps_leave_wps();
1199 #ifdef HAVE_RECORDING
1200 if (button == ACTION_WPS_REC)
1201 return GO_TO_RECSCREEN;
1202 #endif
1203 if (global_settings.browse_current)
1204 return GO_TO_PREVIOUS_BROWSER;
1205 return GO_TO_PREVIOUS;
1208 if (button && !IS_SYSEVENT(button) )
1209 storage_spin();
1211 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1214 /* this is called from the playback thread so NO DRAWING! */
1215 static void track_changed_callback(void *param)
1217 wps_state.id3 = (struct mp3entry*)param;
1218 wps_state.nid3 = audio_next_track();
1219 if (wps_state.id3->cuesheet)
1221 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1222 cue_spoof_id3(wps_state.id3->cuesheet, wps_state.id3);
1224 wps_state.do_full_update = true;
1226 static void nextid3available_callback(void* param)
1228 (void)param;
1229 wps_state.nid3 = audio_next_track();
1230 wps_state.do_full_update = true;
1234 static void wps_state_init(void)
1236 wps_state.ff_rewind = false;
1237 wps_state.paused = false;
1238 if(audio_status() & AUDIO_STATUS_PLAY)
1240 wps_state.id3 = audio_current_track();
1241 wps_state.nid3 = audio_next_track();
1243 else
1245 wps_state.id3 = NULL;
1246 wps_state.nid3 = NULL;
1248 /* We'll be updating due to restore initialized with true */
1249 wps_state.do_full_update = false;
1250 /* add the WPS track event callbacks */
1251 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1252 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1256 #ifdef HAVE_LCD_BITMAP
1257 static void statusbar_toggle_handler(void *data)
1259 (void)data;
1260 int i;
1262 wpsbars = VP_SB_HIDE_ALL;
1263 FOR_NB_SCREENS(i)
1264 { /* fix viewports if needed */
1265 skin_statusbar_changed(&gui_wps[i]);
1267 bool draw = false;
1269 /* fix up gui_wps::statusbars, so that the viewportmanager accepts it*/
1270 if (gui_wps[i].data->wps_sb_tag)
1271 draw = gui_wps[i].data->show_sb_on_wps;
1272 else if (statusbar_position(i) != STATUSBAR_OFF)
1273 draw = true;
1274 if (draw)
1275 wpsbars |=
1276 (VP_SB_ONSCREEN(i) | VP_SB_IGNORE_SETTING(i));
1279 #endif
1281 void gui_sync_wps_init(void)
1283 int i;
1284 FOR_NB_SCREENS(i)
1286 #ifdef HAVE_ALBUMART
1287 wps_datas[i].albumart = NULL;
1288 #endif
1289 #ifdef HAVE_REMOTE_LCD
1290 wps_datas[i].remote_wps = (i == SCREEN_REMOTE);
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 /* only one wpsbars needed/wanted */
1299 gui_wps[i].statusbars = &wpsbars;
1301 *(gui_wps[SCREEN_MAIN].statusbars) =VP_SB_ALLSCREENS;
1302 #ifdef HAVE_LCD_BITMAP
1303 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggle_handler);
1304 #endif
1307 #ifdef HAVE_ALBUMART
1308 bool wps_uses_albumart(int *width, int *height)
1310 int i;
1311 FOR_NB_SCREENS(i) {
1312 struct gui_wps *gwps = &gui_wps[i];
1313 struct skin_albumart *aa = gwps->data->albumart;
1314 if (aa && (aa->state != WPS_ALBUMART_NONE))
1316 if (width)
1317 *width = aa->width;
1318 if (height)
1319 *height = aa->height;
1320 return true;
1323 return false;
1325 #endif
1328 #ifdef IPOD_ACCESSORY_PROTOCOL
1329 int wps_get_ff_rewind_count(void)
1331 return wps_state.ff_rewind_count;
1333 #endif