Move handling of "rockbox_default.[r]wps" into wps.c, out of the skin engine (which...
[kugel-rb/myfork.git] / apps / gui / wps.c
blobed3bf5f6b30c37ca99807762fde8b6953f5ee359
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 static int wpsbars;
82 /* currently only one wps_state is needed */
83 static struct wps_state wps_state;
84 static struct gui_wps gui_wps[NB_SCREENS];
85 static struct wps_data wps_datas[NB_SCREENS];
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);
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 screens[screen].backdrop_unload(BACKDROP_SKIN_WPS);
108 #ifndef __PCTOOL__
110 * Hardcode loading WPS_DEFAULTCFG to cause a reset ideally this
111 * wants to be a virtual file. Feel free to modify dirbrowse()
112 * if you're feeling brave.
115 if (! strcmp(buf, DEFAULT_WPS(screen)) )
117 #ifdef HAVE_REMOTE_LCD
118 if (screen == SCREEN_REMOTE)
119 global_settings.rwps_file[0] = '\0';
120 else
121 #endif
122 global_settings.wps_file[0] = '\0';
123 buf = NULL;
126 #endif /* __PCTOOL__ */
128 loaded_ok = buf && skin_data_load(gui_wps[screen].data,
129 &screens[screen], buf, isfile);
130 if (!loaded_ok) /* load the hardcoded default */
132 char *skin_buf[NB_SCREENS] = {
133 #ifdef HAVE_LCD_BITMAP
134 "%s%?it<%?in<%in. |>%it|%fn>\n"
135 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
136 "%s%?id<%id|%?d1<%d1|(root)>> %?iy<(%iy)|>\n\n"
137 "%al%pc/%pt%ar[%pp:%pe]\n"
138 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
139 "%pb\n%pm\n",
140 #else
141 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d2> - %?id<%id|%d1>\n"
142 "%pc%?ps<*|/>%pt\n",
143 #endif
144 #ifdef HAVE_REMOTE_LCD
145 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
146 "%s%?it<%?in<%in. |>%it|%fn>\n"
147 "%al%pc/%pt%ar[%pp:%pe]\n"
148 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
149 "%pb\n",
150 #endif
152 skin_data_load(gui_wps[screen].data, &screens[screen],
153 skin_buf[screen], false);
155 #ifdef HAVE_REMOVE_LCD
156 gui_wps[screen].data->remote_wps = !(screen == SCREEN_MAIN);
157 #endif
160 void wps_data_init(enum screen_type screen)
162 skin_data_init(gui_wps[screen].data);
166 static bool wps_fading_out = false;
167 void fade(bool fade_in, bool updatewps)
169 int fp_global_vol = global_settings.volume << 8;
170 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
171 int fp_step = (fp_global_vol - fp_min_vol) / 30;
172 int i;
173 wps_fading_out = !fade_in;
174 if (fade_in) {
175 /* fade in */
176 int fp_volume = fp_min_vol;
178 /* zero out the sound */
179 sound_set_volume(fp_min_vol >> 8);
181 sleep(HZ/10); /* let audio thread run */
182 audio_resume();
184 while (fp_volume < fp_global_vol - fp_step) {
185 fp_volume += fp_step;
186 sound_set_volume(fp_volume >> 8);
187 if (updatewps)
189 FOR_NB_SCREENS(i)
190 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
192 sleep(1);
194 sound_set_volume(global_settings.volume);
196 else {
197 /* fade out */
198 int fp_volume = fp_global_vol;
200 while (fp_volume > fp_min_vol + fp_step) {
201 fp_volume -= fp_step;
202 sound_set_volume(fp_volume >> 8);
203 if (updatewps)
205 FOR_NB_SCREENS(i)
206 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
208 sleep(1);
210 audio_pause();
211 wps_fading_out = false;
212 #if CONFIG_CODEC != SWCODEC
213 #ifndef SIMULATOR
214 /* let audio thread run and wait for the mas to run out of data */
215 while (!mp3_pause_done())
216 #endif
217 sleep(HZ/10);
218 #endif
220 /* reset volume to what it was before the fade */
221 sound_set_volume(global_settings.volume);
224 bool is_wps_fading(void)
226 return wps_fading_out;
229 static bool update_onvol_change(struct gui_wps * gwps)
231 skin_update(gwps, WPS_REFRESH_NON_STATIC);
233 #ifdef HAVE_LCD_CHARCELLS
234 splashf(0, "Vol: %3d dB",
235 sound_val2phys(SOUND_VOLUME, global_settings.volume));
236 return true;
237 #endif
238 return false;
242 bool ffwd_rew(int button)
244 unsigned int step = 0; /* current ff/rewind step */
245 unsigned int max_step = 0; /* maximum ff/rewind step */
246 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
247 int direction = -1; /* forward=1 or backward=-1 */
248 bool exit = false;
249 bool usb = false;
250 int i = 0;
251 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
253 if (button == ACTION_NONE)
255 status_set_ffmode(0);
256 return usb;
258 while (!exit)
260 switch ( button )
262 case ACTION_WPS_SEEKFWD:
263 direction = 1;
264 case ACTION_WPS_SEEKBACK:
265 if (wps_state.ff_rewind)
267 if (direction == 1)
269 /* fast forwarding, calc max step relative to end */
270 max_step = (wps_state.id3->length -
271 (wps_state.id3->elapsed +
272 ff_rewind_count)) *
273 FF_REWIND_MAX_PERCENT / 100;
275 else
277 /* rewinding, calc max step relative to start */
278 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
279 FF_REWIND_MAX_PERCENT / 100;
282 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
284 if (step > max_step)
285 step = max_step;
287 ff_rewind_count += step * direction;
289 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
290 step += step >> ff_rw_accel;
292 else
294 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
295 wps_state.id3 && wps_state.id3->length )
297 if (!wps_state.paused)
298 #if (CONFIG_CODEC == SWCODEC)
299 audio_pre_ff_rewind();
300 #else
301 audio_pause();
302 #endif
303 #if CONFIG_KEYPAD == PLAYER_PAD
304 FOR_NB_SCREENS(i)
305 gui_wps[i].display->stop_scroll();
306 #endif
307 if (direction > 0)
308 status_set_ffmode(STATUS_FASTFORWARD);
309 else
310 status_set_ffmode(STATUS_FASTBACKWARD);
312 wps_state.ff_rewind = true;
314 step = 1000 * global_settings.ff_rewind_min_step;
316 else
317 break;
320 if (direction > 0) {
321 if ((wps_state.id3->elapsed + ff_rewind_count) >
322 wps_state.id3->length)
323 ff_rewind_count = wps_state.id3->length -
324 wps_state.id3->elapsed;
326 else {
327 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
328 ff_rewind_count = -wps_state.id3->elapsed;
331 /* set the wps state ff_rewind_count so the progess info
332 displays corectly */
333 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
334 ff_rewind_count:-ff_rewind_count;
335 FOR_NB_SCREENS(i)
337 skin_update(&gui_wps[i],
338 WPS_REFRESH_PLAYER_PROGRESS |
339 WPS_REFRESH_DYNAMIC);
342 break;
344 case ACTION_WPS_STOPSEEK:
345 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
346 audio_ff_rewind(wps_state.id3->elapsed);
347 wps_state.ff_rewind_count = 0;
348 wps_state.ff_rewind = false;
349 status_set_ffmode(0);
350 #if (CONFIG_CODEC != SWCODEC)
351 if (!wps_state.paused)
352 audio_resume();
353 #endif
354 #ifdef HAVE_LCD_CHARCELLS
355 FOR_NB_SCREENS(i)
356 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
357 #endif
358 exit = true;
359 break;
361 default:
362 if(default_event_handler(button) == SYS_USB_CONNECTED) {
363 status_set_ffmode(0);
364 usb = true;
365 exit = true;
367 break;
369 if (!exit)
371 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
372 #ifdef HAVE_TOUCHSCREEN
373 if (button == ACTION_TOUCHSCREEN)
374 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
375 #endif
378 return usb;
382 void display_keylock_text(bool locked)
384 int i;
385 FOR_NB_SCREENS(i)
386 gui_wps[i].display->stop_scroll();
388 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
394 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
395 static void gwps_caption_backlight(struct wps_state *state)
397 if (state && state->id3)
399 #ifdef HAVE_BACKLIGHT
400 if (global_settings.caption_backlight)
402 /* turn on backlight n seconds before track ends, and turn it off n
403 seconds into the new track. n == backlight_timeout, or 5s */
404 int n = global_settings.backlight_timeout * 1000;
406 if ( n < 1000 )
407 n = 5000; /* use 5s if backlight is always on or off */
409 if (((state->id3->elapsed < 1000) ||
410 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
411 (state->paused == false))
412 backlight_on();
414 #endif
415 #ifdef HAVE_REMOTE_LCD
416 if (global_settings.remote_caption_backlight)
418 /* turn on remote backlight n seconds before track ends, and turn it
419 off n seconds into the new track. n == remote_backlight_timeout,
420 or 5s */
421 int n = global_settings.remote_backlight_timeout * 1000;
423 if ( n < 1000 )
424 n = 5000; /* use 5s if backlight is always on or off */
426 if (((state->id3->elapsed < 1000) ||
427 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
428 (state->paused == false))
429 remote_backlight_on();
431 #endif
434 #endif
437 static void change_dir(int direction)
439 if (global_settings.prevent_skip)
440 return;
442 if (direction < 0)
443 audio_prev_dir();
444 else if (direction > 0)
445 audio_next_dir();
446 /* prevent the next dir to immediatly start being ffw'd */
447 action_wait_for_release();
450 static void prev_track(unsigned long skip_thresh)
452 if (wps_state.id3->elapsed < skip_thresh)
454 audio_prev();
455 return;
457 else
459 if (wps_state.id3->cuesheet)
461 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
462 return;
465 if (!wps_state.paused)
466 #if (CONFIG_CODEC == SWCODEC)
467 audio_pre_ff_rewind();
468 #else
469 audio_pause();
470 #endif
472 audio_ff_rewind(0);
474 #if (CONFIG_CODEC != SWCODEC)
475 if (!wps_state.paused)
476 audio_resume();
477 #endif
481 static void next_track(void)
483 /* take care of if we're playing a cuesheet */
484 if (wps_state.id3->cuesheet)
486 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
488 /* if the result was false, then we really want
489 to skip to the next track */
490 return;
494 audio_next();
497 static void play_hop(int direction)
499 unsigned long step = ((unsigned long)global_settings.skip_length)*1000;
500 unsigned long elapsed = wps_state.id3->elapsed;
501 unsigned long remaining = wps_state.id3->length - elapsed;
503 if (!global_settings.prevent_skip &&
504 (!step ||
505 (direction > 0 && step >= remaining) ||
506 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
507 { /* Do normal track skipping */
508 if (direction > 0)
509 next_track();
510 else if (direction < 0)
511 prev_track(DEFAULT_SKIP_TRESH);
512 return;
515 if (direction == 1 && step >= remaining)
517 #if CONFIG_CODEC == SWCODEC
518 if(global_settings.beep)
519 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
520 #endif
521 return;
523 else if ((direction == -1 && elapsed < step))
525 elapsed = 0;
527 else
529 elapsed += step * direction;
531 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
533 #if (CONFIG_CODEC == SWCODEC)
534 audio_pre_ff_rewind();
535 #else
536 audio_pause();
537 #endif
539 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
540 #if (CONFIG_CODEC != SWCODEC)
541 if (!wps_state.paused)
542 audio_resume();
543 #endif
546 static void gwps_fix_statusbars(void)
548 #ifdef HAVE_LCD_BITMAP
549 int i;
550 wpsbars = VP_SB_HIDE_ALL;
551 FOR_NB_SCREENS(i)
553 bool draw = false;
554 if (gui_wps[i].data->wps_sb_tag)
555 draw = gui_wps[i].data->show_sb_on_wps;
556 else if (statusbar_position(i) != STATUSBAR_OFF)
557 draw = true;
558 if (draw)
559 wpsbars |= (VP_SB_ONSCREEN(i) | VP_SB_IGNORE_SETTING(i));
561 #else
562 wpsbars = VP_SB_ALLSCREENS;
563 #endif
566 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
568 * If the user is unable to see the wps, because the display is deactivated,
569 * we suppress updates until the wps is activated again (the lcd driver will
570 * call this hook to issue an instant update)
571 * */
572 static void wps_lcd_activation_hook(void)
574 wps_state.do_full_update = true;
575 /* force timeout in wps main loop, so that the update is instantly */
576 queue_post(&button_queue, BUTTON_NONE, 0);
578 #endif
580 static void gwps_leave_wps(void)
582 int i, oldbars = VP_SB_HIDE_ALL;
584 FOR_NB_SCREENS(i)
586 gui_wps[i].display->stop_scroll();
587 gui_wps[i].display->backdrop_show(BACKDROP_MAIN);
589 if (global_settings.statusbar)
590 oldbars = VP_SB_ALLSCREENS;
592 viewportmanager_set_statusbar(oldbars);
593 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
594 /* Play safe and unregister the hook */
595 lcd_activation_set_hook(NULL);
596 #endif
597 send_event(GUI_EVENT_REFRESH, NULL);
600 void gwps_draw_statusbars(void)
602 viewportmanager_set_statusbar(wpsbars);
604 #ifdef HAVE_TOUCHSCREEN
605 int wps_get_touchaction(struct wps_data *data)
607 short x,y;
608 short vx, vy;
609 int type = action_get_touchscreen_press(&x, &y);
610 static int last_action = ACTION_NONE;
611 struct touchregion *r;
612 bool repeated = (type == BUTTON_REPEAT);
613 bool released = (type == BUTTON_REL);
614 struct skin_token_list *regions = data->touchregions;
615 while (regions)
617 r = (struct touchregion *)regions->token->value.data;
618 /* make sure this region's viewport is visible */
619 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
621 regions = regions->next;
622 continue;
624 /* reposition the touch inside the viewport */
625 vx = x - r->wvp->vp.x;
626 vy = y - r->wvp->vp.y;
627 /* check if it's inside this viewport */
628 if (vx >= 0 && vx < r->wvp->vp.x + r->wvp->vp.width &&
629 vy >= 0 && vy < r->wvp->vp.y + r->wvp->vp.height)
631 /* now see if the point is inside this region */
632 if (vx >= r->x && vx < r->x+r->width &&
633 vy >= r->y && vy < r->y+r->height)
635 /* reposition the touch within the area */
636 vx -= r->x;
637 vy -= r->y;
639 switch(r->type)
641 case WPS_TOUCHREGION_ACTION:
642 if ((repeated && r->repeat) || (released && !r->repeat))
644 last_action = r->action;
645 return r->action;
647 break;
648 case WPS_TOUCHREGION_SCROLLBAR:
649 if(r->width > r->height)
650 /* landscape */
651 wps_state.id3->elapsed = (vx *
652 wps_state.id3->length) / r->width;
653 else
654 /* portrait */
655 wps_state.id3->elapsed = (vy *
656 wps_state.id3->length) / r->height;
658 audio_ff_rewind(wps_state.id3->elapsed);
659 break;
660 case WPS_TOUCHREGION_VOLUME:
662 int i;
663 const int min_vol = sound_min(SOUND_VOLUME);
664 const int max_vol = sound_max(SOUND_VOLUME);
665 if(r->width > r->height)
666 /* landscape */
667 global_settings.volume = (vx *
668 (max_vol - min_vol)) / r->width;
669 else
670 /* portrait */
671 global_settings.volume = (vy *
672 (max_vol-min_vol)) / r->height;
674 global_settings.volume += min_vol;
675 setvol();
676 FOR_NB_SCREENS(i)
678 gui_wps[i].data->button_time_volume = current_tick;
680 return ACTION_REDRAW;
685 regions = regions->next;
688 if ((last_action == ACTION_WPS_SEEKBACK || last_action == ACTION_WPS_SEEKFWD))
689 return ACTION_WPS_STOPSEEK;
690 last_action = ACTION_TOUCHSCREEN;
691 return ACTION_TOUCHSCREEN;
693 #endif
694 /* The WPS can be left in two ways:
695 * a) call a function, which draws over the wps. In this case, the wps
696 * will be still active (i.e. the below function didn't return)
697 * b) return with a value evaluated by root_menu.c, in this case the wps
698 * is really left, and root_menu will handle the next screen
700 * In either way, call gwps_leave_wps(), in order to restore the correct
701 * "main screen" backdrops and statusbars
703 long gui_wps_show(void)
705 long button = 0;
706 bool restore = true;
707 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
708 bool exit = false;
709 bool bookmark = false;
710 bool update = false;
711 int i;
712 long last_left = 0, last_right = 0;
714 #ifdef HAVE_LCD_CHARCELLS
715 status_set_audio(true);
716 status_set_param(false);
717 #endif
719 #ifdef AB_REPEAT_ENABLE
720 ab_repeat_init();
721 ab_reset_markers();
722 #endif
723 wps_state_init();
725 while ( 1 )
727 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
729 /* did someone else (i.e power thread) change audio pause mode? */
730 if (wps_state.paused != audio_paused) {
731 wps_state.paused = audio_paused;
733 /* if another thread paused audio, we are probably in car mode,
734 about to shut down. lets save the settings. */
735 if (wps_state.paused) {
736 settings_save();
737 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
738 call_storage_idle_notifys(true);
739 #endif
743 #ifdef HAVE_LCD_BITMAP
744 /* when the peak meter is enabled we want to have a
745 few extra updates to make it look smooth. On the
746 other hand we don't want to waste energy if it
747 isn't displayed */
748 bool pm=false;
749 FOR_NB_SCREENS(i)
751 if(gui_wps[i].data->peak_meter_enabled)
752 pm = true;
755 if (pm) {
756 long next_refresh = current_tick;
757 long next_big_refresh = current_tick + HZ / 5;
758 button = BUTTON_NONE;
759 while (TIME_BEFORE(current_tick, next_big_refresh)) {
760 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_NOBLOCK);
761 if (button != ACTION_NONE) {
762 break;
764 peak_meter_peek();
765 sleep(0); /* Sleep until end of current tick. */
767 if (TIME_AFTER(current_tick, next_refresh)) {
768 FOR_NB_SCREENS(i)
770 if(gui_wps[i].data->peak_meter_enabled)
771 skin_update(&gui_wps[i], WPS_REFRESH_PEAK_METER);
772 next_refresh += HZ / PEAK_METER_FPS;
779 /* The peak meter is disabled
780 -> no additional screen updates needed */
781 else
782 #endif
784 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,
785 restore ? HZ/100 : HZ/5);
788 /* Exit if audio has stopped playing. This happens e.g. at end of
789 playlist or if using the sleep timer. */
790 if (!(audio_status() & AUDIO_STATUS_PLAY))
791 exit = true;
792 #ifdef HAVE_TOUCHSCREEN
793 if (button == ACTION_TOUCHSCREEN)
794 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
795 #endif
796 /* The iPods/X5/M5 use a single button for the A-B mode markers,
797 defined as ACTION_WPSAB_SINGLE in their config files. */
798 #ifdef ACTION_WPSAB_SINGLE
799 if (!global_settings.party_mode && ab_repeat_mode_enabled())
801 static int wps_ab_state = 0;
802 if (button == ACTION_WPSAB_SINGLE)
804 switch (wps_ab_state)
806 case 0: /* set the A spot */
807 button = ACTION_WPS_ABSETA_PREVDIR;
808 break;
809 case 1: /* set the B spot */
810 button = ACTION_WPS_ABSETB_NEXTDIR;
811 break;
812 case 2:
813 button = ACTION_WPS_ABRESET;
814 break;
816 wps_ab_state = (wps_ab_state+1) % 3;
819 #endif
820 switch(button)
822 case ACTION_WPS_CONTEXT:
824 gwps_leave_wps();
825 /* if music is stopped in the context menu we want to exit the wps */
826 if (onplay(wps_state.id3->path,
827 FILE_ATTR_AUDIO, CONTEXT_WPS) == ONPLAY_MAINMENU
828 || !audio_status())
829 return GO_TO_ROOT;
830 restore = true;
832 break;
834 case ACTION_WPS_BROWSE:
835 #ifdef HAVE_LCD_CHARCELLS
836 status_set_record(false);
837 status_set_audio(false);
838 #endif
839 gwps_leave_wps();
840 return GO_TO_PREVIOUS_BROWSER;
841 break;
843 /* play/pause */
844 case ACTION_WPS_PLAY:
845 if (global_settings.party_mode)
846 break;
847 if ( wps_state.paused )
849 wps_state.paused = false;
850 if ( global_settings.fade_on_stop )
851 fade(true, true);
852 else
853 audio_resume();
855 else
857 wps_state.paused = true;
858 if ( global_settings.fade_on_stop )
859 fade(false, true);
860 else
861 audio_pause();
862 settings_save();
863 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
864 call_storage_idle_notifys(true); /* make sure resume info is saved */
865 #endif
867 break;
869 case ACTION_WPS_VOLUP:
871 FOR_NB_SCREENS(i)
872 gui_wps[i].data->button_time_volume = current_tick;
873 global_settings.volume++;
874 bool res = false;
875 setvol();
876 FOR_NB_SCREENS(i)
878 if(update_onvol_change(&gui_wps[i]))
879 res = true;
881 if (res) {
882 restore = true;
883 restoretimer = RESTORE_WPS_NEXT_SECOND;
886 break;
887 case ACTION_WPS_VOLDOWN:
889 FOR_NB_SCREENS(i)
890 gui_wps[i].data->button_time_volume = current_tick;
891 global_settings.volume--;
892 setvol();
893 bool res = false;
894 FOR_NB_SCREENS(i)
896 if(update_onvol_change(&gui_wps[i]))
897 res = true;
899 if (res) {
900 restore = true;
901 restoretimer = RESTORE_WPS_NEXT_SECOND;
904 break;
905 /* fast forward
906 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
907 case ACTION_WPS_SEEKFWD:
908 if (global_settings.party_mode)
909 break;
910 if (current_tick -last_right < HZ)
912 if (wps_state.id3->cuesheet)
914 audio_next();
916 else
918 change_dir(1);
921 else
922 ffwd_rew(ACTION_WPS_SEEKFWD);
923 last_right = last_left = 0;
924 break;
925 /* fast rewind
926 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
927 case ACTION_WPS_SEEKBACK:
928 if (global_settings.party_mode)
929 break;
930 if (current_tick -last_left < HZ)
932 if (wps_state.id3->cuesheet)
934 if (!wps_state.paused)
935 #if (CONFIG_CODEC == SWCODEC)
936 audio_pre_ff_rewind();
937 #else
938 audio_pause();
939 #endif
940 audio_ff_rewind(0);
942 else
944 change_dir(-1);
947 else
948 ffwd_rew(ACTION_WPS_SEEKBACK);
949 last_left = last_right = 0;
950 break;
952 /* prev / restart */
953 case ACTION_WPS_SKIPPREV:
954 if (global_settings.party_mode)
955 break;
956 last_left = current_tick;
957 #ifdef AB_REPEAT_ENABLE
958 /* if we're in A/B repeat mode and the current position
959 is past the A marker, jump back to the A marker... */
960 if ( ab_repeat_mode_enabled() )
962 if ( ab_after_A_marker(wps_state.id3->elapsed) )
964 ab_jump_to_A_marker();
965 break;
966 #if (AB_REPEAT_ENABLE == 2)
967 } else {
968 ab_reset_markers();
969 #endif
972 else
973 /* ...otherwise, do it normally */
974 #endif
975 play_hop(-1);
976 break;
978 /* next
979 OR if skip length set, hop by predetermined amount. */
980 case ACTION_WPS_SKIPNEXT:
981 if (global_settings.party_mode)
982 break;
983 last_right = current_tick;
984 #ifdef AB_REPEAT_ENABLE
985 /* if we're in A/B repeat mode and the current position is
986 before the A marker, jump to the A marker... */
987 if ( ab_repeat_mode_enabled() )
989 if ( ab_before_A_marker(wps_state.id3->elapsed) )
991 ab_jump_to_A_marker();
992 break;
993 #if (AB_REPEAT_ENABLE == 2)
994 } else {
995 ab_reset_markers();
996 #endif
999 else
1000 /* ...otherwise, do it normally */
1001 #endif
1002 play_hop(1);
1003 break;
1004 /* next / prev directories */
1005 /* and set A-B markers if in a-b mode */
1006 case ACTION_WPS_ABSETB_NEXTDIR:
1007 if (global_settings.party_mode)
1008 break;
1009 #if defined(AB_REPEAT_ENABLE)
1010 if (ab_repeat_mode_enabled())
1012 ab_set_B_marker(wps_state.id3->elapsed);
1013 ab_jump_to_A_marker();
1015 else
1016 #endif
1018 change_dir(1);
1020 break;
1021 case ACTION_WPS_ABSETA_PREVDIR:
1022 if (global_settings.party_mode)
1023 break;
1024 #if defined(AB_REPEAT_ENABLE)
1025 if (ab_repeat_mode_enabled())
1026 ab_set_A_marker(wps_state.id3->elapsed);
1027 else
1028 #endif
1030 change_dir(-1);
1032 break;
1033 /* menu key functions */
1034 case ACTION_WPS_MENU:
1035 gwps_leave_wps();
1036 return GO_TO_ROOT;
1037 break;
1040 #ifdef HAVE_QUICKSCREEN
1041 case ACTION_WPS_QUICKSCREEN:
1043 gwps_leave_wps();
1044 if (quick_screen_quick(button))
1045 return SYS_USB_CONNECTED;
1046 restore = true;
1048 break;
1049 #endif /* HAVE_QUICKSCREEN */
1051 /* screen settings */
1052 #ifdef BUTTON_F3
1053 case ACTION_F3:
1055 gwps_leave_wps();
1056 if (quick_screen_f3(BUTTON_F3))
1057 return SYS_USB_CONNECTED;
1058 restore = true;
1060 break;
1061 #endif /* BUTTON_F3 */
1063 /* pitch screen */
1064 #ifdef HAVE_PITCHSCREEN
1065 case ACTION_WPS_PITCHSCREEN:
1067 gwps_leave_wps();
1068 if (1 == gui_syncpitchscreen_run())
1069 return SYS_USB_CONNECTED;
1070 restore = true;
1072 break;
1073 #endif /* HAVE_PITCHSCREEN */
1075 #ifdef AB_REPEAT_ENABLE
1076 /* reset A&B markers */
1077 case ACTION_WPS_ABRESET:
1078 if (ab_repeat_mode_enabled())
1080 ab_reset_markers();
1081 update = true;
1083 break;
1084 #endif /* AB_REPEAT_ENABLE */
1086 /* stop and exit wps */
1087 case ACTION_WPS_STOP:
1088 if (global_settings.party_mode)
1089 break;
1090 bookmark = true;
1091 exit = true;
1092 break;
1094 case ACTION_WPS_ID3SCREEN:
1096 gwps_leave_wps();
1097 browse_id3();
1098 restore = true;
1100 break;
1101 #ifdef HAVE_TOUCHSCREEN
1102 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1104 global_settings.playlist_shuffle =
1105 !global_settings.playlist_shuffle;
1106 #if CONFIG_CODEC == SWCODEC
1107 dsp_set_replaygain();
1108 #endif
1109 if (global_settings.playlist_shuffle)
1110 playlist_randomise(NULL, current_tick, true);
1111 else
1112 playlist_sort(NULL, true);
1114 break;
1115 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1117 const struct settings_list *rep_setting =
1118 find_setting(&global_settings.repeat_mode, NULL);
1119 option_select_next_val(rep_setting, false, true);
1120 audio_flush_and_reload_tracks();
1122 break;
1123 #endif /* HAVE_TOUCHSCREEN */
1124 /* this case is used by the softlock feature
1125 * it requests a full update here */
1126 case ACTION_REDRAW:
1127 wps_state.do_full_update = true;
1128 break;
1129 case ACTION_NONE: /* Timeout, do a partial update */
1130 update = true;
1131 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1132 break;
1133 #ifdef HAVE_RECORDING
1134 case ACTION_WPS_REC:
1135 exit = true;
1136 break;
1137 #endif
1138 case SYS_POWEROFF:
1139 default_event_handler(SYS_POWEROFF);
1140 break;
1141 case ACTION_WPS_VIEW_PLAYLIST:
1142 gwps_leave_wps();
1143 if (playlist_viewer()) /* true if USB connected */
1144 return SYS_USB_CONNECTED;
1145 restore = true;
1146 break;
1147 default:
1148 if(default_event_handler(button) == SYS_USB_CONNECTED)
1149 return GO_TO_ROOT;
1150 update = true;
1151 break;
1154 if (wps_state.do_full_update || update)
1156 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1157 gwps_caption_backlight(&wps_state);
1158 #endif
1159 FOR_NB_SCREENS(i)
1161 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1162 if (lcd_active()
1163 #ifdef HAVE_REMOTE_LCD
1164 /* currently, all remotes are readable without backlight
1165 * so still update those */
1166 || (i == SCREEN_REMOTE)
1167 #endif
1169 #endif
1171 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
1174 wps_state.do_full_update = false;
1175 update = false;
1178 if (restore && wps_state.id3 &&
1179 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1180 TIME_AFTER(current_tick, restoretimer)))
1182 restore = false;
1183 restoretimer = RESTORE_WPS_INSTANTLY;
1184 gwps_fix_statusbars();
1185 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1186 lcd_activation_set_hook(wps_lcd_activation_hook);
1187 #endif
1188 FOR_NB_SCREENS(i)
1190 screens[i].stop_scroll();
1191 gui_wps_display(&gui_wps[i]);
1195 if (exit) {
1196 #ifdef HAVE_LCD_CHARCELLS
1197 status_set_record(false);
1198 status_set_audio(false);
1199 #endif
1200 if (global_settings.fade_on_stop)
1201 fade(false, true);
1203 if (bookmark)
1204 bookmark_autobookmark();
1205 audio_stop();
1206 #ifdef AB_REPEAT_ENABLE
1207 ab_reset_markers();
1208 #endif
1209 gwps_leave_wps();
1210 #ifdef HAVE_RECORDING
1211 if (button == ACTION_WPS_REC)
1212 return GO_TO_RECSCREEN;
1213 #endif
1214 if (global_settings.browse_current)
1215 return GO_TO_PREVIOUS_BROWSER;
1216 return GO_TO_PREVIOUS;
1219 if (button && !IS_SYSEVENT(button) )
1220 storage_spin();
1222 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1225 /* this is called from the playback thread so NO DRAWING! */
1226 static void track_changed_callback(void *param)
1228 wps_state.id3 = (struct mp3entry*)param;
1229 wps_state.nid3 = audio_next_track();
1230 if (wps_state.id3->cuesheet)
1232 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1233 cue_spoof_id3(wps_state.id3->cuesheet, wps_state.id3);
1235 wps_state.do_full_update = true;
1237 static void nextid3available_callback(void* param)
1239 (void)param;
1240 wps_state.nid3 = audio_next_track();
1241 wps_state.do_full_update = true;
1245 static void wps_state_init(void)
1247 wps_state.ff_rewind = false;
1248 wps_state.paused = false;
1249 if(audio_status() & AUDIO_STATUS_PLAY)
1251 wps_state.id3 = audio_current_track();
1252 wps_state.nid3 = audio_next_track();
1254 else
1256 wps_state.id3 = NULL;
1257 wps_state.nid3 = NULL;
1259 /* We'll be updating due to restore initialized with true */
1260 wps_state.do_full_update = false;
1261 /* add the WPS track event callbacks */
1262 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1263 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1267 #ifdef HAVE_LCD_BITMAP
1268 static void statusbar_toggle_handler(void *data)
1270 (void)data;
1271 int i;
1272 gwps_fix_statusbars();
1274 FOR_NB_SCREENS(i)
1276 struct viewport *vp = &find_viewport(VP_DEFAULT_LABEL, &wps_datas[i])->vp;
1277 bool draw = wpsbars & (VP_SB_ONSCREEN(i) | VP_SB_IGNORE_SETTING(i));
1278 if (!draw)
1280 vp->y = 0;
1281 vp->height = screens[i].lcdheight;
1283 else
1285 bool bar_at_top = statusbar_position(i) != STATUSBAR_BOTTOM;
1286 vp->y = bar_at_top?STATUSBAR_HEIGHT:0;
1287 vp->height = screens[i].lcdheight - STATUSBAR_HEIGHT;
1291 #endif
1293 void gui_sync_wps_init(void)
1295 int i;
1296 FOR_NB_SCREENS(i)
1298 skin_data_init(&wps_datas[i]);
1299 #ifdef HAVE_ALBUMART
1300 wps_datas[i].wps_uses_albumart = 0;
1301 #endif
1302 #ifdef HAVE_REMOTE_LCD
1303 wps_datas[i].remote_wps = (i == SCREEN_REMOTE);
1304 #endif
1305 gui_wps[i].data = &wps_datas[i];
1306 gui_wps[i].display = &screens[i];
1307 /* Currently no seperate wps_state needed/possible
1308 so use the only available ( "global" ) one */
1309 gui_wps[i].state = &wps_state;
1310 gui_wps[i].display->backdrop_unload(BACKDROP_SKIN_WPS);
1312 #ifdef HAVE_LCD_BITMAP
1313 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggle_handler);
1314 #endif
1317 #ifdef HAVE_ALBUMART
1318 bool wps_uses_albumart(int *width, int *height)
1320 int i;
1321 FOR_NB_SCREENS(i) {
1322 struct gui_wps *gwps = &gui_wps[i];
1323 if (gwps->data && (gwps->data->wps_uses_albumart != WPS_ALBUMART_NONE))
1325 if (width)
1326 *width = gui_wps[0].data->albumart_max_width;
1327 if (height)
1328 *height = gui_wps[0].data->albumart_max_height;
1329 return true;
1332 return false;
1334 #endif
1337 #ifdef IPOD_ACCESSORY_PROTOCOL
1338 int wps_get_ff_rewind_count(void)
1340 return wps_state.ff_rewind_count;
1342 #endif