initial custom statusbar commit
[kugel-rb.git] / apps / gui / wps.c
blob3ea5cf97f7ab215bb6531b414285b604fffa4043
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] = {{ .data = NULL }};
87 static struct wps_data wps_datas[NB_SCREENS] = {{ .wps_loaded = 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 DEBUGF("%s >> wps loading\n", __func__);
135 if (!loaded_ok) /* load the hardcoded default */
137 char *skin_buf[NB_SCREENS] = {
138 #ifdef HAVE_LCD_BITMAP
139 "%s%?it<%?in<%in. |>%it|%fn>\n"
140 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
141 "%s%?id<%id|%?d1<%d1|(root)>> %?iy<(%iy)|>\n\n"
142 "%al%pc/%pt%ar[%pp:%pe]\n"
143 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
144 "%pb\n%pm\n",
145 #else
146 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d2> - %?id<%id|%d1>\n"
147 "%pc%?ps<*|/>%pt\n",
148 #endif
149 #ifdef HAVE_REMOTE_LCD
150 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
151 "%s%?it<%?in<%in. |>%it|%fn>\n"
152 "%al%pc/%pt%ar[%pp:%pe]\n"
153 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
154 "%pb\n",
155 #endif
157 skin_data_load(gui_wps[screen].data, &screens[screen],
158 skin_buf[screen], false);
160 #ifdef HAVE_REMOVE_LCD
161 gui_wps[screen].data->remote_wps = !(screen == SCREEN_MAIN);
162 #endif
165 static bool wps_fading_out = false;
166 void fade(bool fade_in, bool updatewps)
168 int fp_global_vol = global_settings.volume << 8;
169 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
170 int fp_step = (fp_global_vol - fp_min_vol) / 30;
171 int i;
172 wps_fading_out = !fade_in;
173 if (fade_in) {
174 /* fade in */
175 int fp_volume = fp_min_vol;
177 /* zero out the sound */
178 sound_set_volume(fp_min_vol >> 8);
180 sleep(HZ/10); /* let audio thread run */
181 audio_resume();
183 while (fp_volume < fp_global_vol - fp_step) {
184 fp_volume += fp_step;
185 sound_set_volume(fp_volume >> 8);
186 if (updatewps)
188 FOR_NB_SCREENS(i)
189 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
191 sleep(1);
193 sound_set_volume(global_settings.volume);
195 else {
196 /* fade out */
197 int fp_volume = fp_global_vol;
199 while (fp_volume > fp_min_vol + fp_step) {
200 fp_volume -= fp_step;
201 sound_set_volume(fp_volume >> 8);
202 if (updatewps)
204 FOR_NB_SCREENS(i)
205 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
207 sleep(1);
209 audio_pause();
210 wps_fading_out = false;
211 #if CONFIG_CODEC != SWCODEC
212 #ifndef SIMULATOR
213 /* let audio thread run and wait for the mas to run out of data */
214 while (!mp3_pause_done())
215 #endif
216 sleep(HZ/10);
217 #endif
219 /* reset volume to what it was before the fade */
220 sound_set_volume(global_settings.volume);
223 bool is_wps_fading(void)
225 return wps_fading_out;
228 static bool update_onvol_change(struct gui_wps * gwps)
230 skin_update(gwps, WPS_REFRESH_NON_STATIC);
232 #ifdef HAVE_LCD_CHARCELLS
233 splashf(0, "Vol: %3d dB",
234 sound_val2phys(SOUND_VOLUME, global_settings.volume));
235 return true;
236 #endif
237 return false;
241 bool ffwd_rew(int button)
243 unsigned int step = 0; /* current ff/rewind step */
244 unsigned int max_step = 0; /* maximum ff/rewind step */
245 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
246 int direction = -1; /* forward=1 or backward=-1 */
247 bool exit = false;
248 bool usb = false;
249 int i = 0;
250 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
252 if (button == ACTION_NONE)
254 status_set_ffmode(0);
255 return usb;
257 while (!exit)
259 switch ( button )
261 case ACTION_WPS_SEEKFWD:
262 direction = 1;
263 case ACTION_WPS_SEEKBACK:
264 if (wps_state.ff_rewind)
266 if (direction == 1)
268 /* fast forwarding, calc max step relative to end */
269 max_step = (wps_state.id3->length -
270 (wps_state.id3->elapsed +
271 ff_rewind_count)) *
272 FF_REWIND_MAX_PERCENT / 100;
274 else
276 /* rewinding, calc max step relative to start */
277 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
278 FF_REWIND_MAX_PERCENT / 100;
281 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
283 if (step > max_step)
284 step = max_step;
286 ff_rewind_count += step * direction;
288 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
289 step += step >> ff_rw_accel;
291 else
293 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
294 wps_state.id3 && wps_state.id3->length )
296 if (!wps_state.paused)
297 #if (CONFIG_CODEC == SWCODEC)
298 audio_pre_ff_rewind();
299 #else
300 audio_pause();
301 #endif
302 #if CONFIG_KEYPAD == PLAYER_PAD
303 FOR_NB_SCREENS(i)
304 gui_wps[i].display->stop_scroll();
305 #endif
306 if (direction > 0)
307 status_set_ffmode(STATUS_FASTFORWARD);
308 else
309 status_set_ffmode(STATUS_FASTBACKWARD);
311 wps_state.ff_rewind = true;
313 step = 1000 * global_settings.ff_rewind_min_step;
315 else
316 break;
319 if (direction > 0) {
320 if ((wps_state.id3->elapsed + ff_rewind_count) >
321 wps_state.id3->length)
322 ff_rewind_count = wps_state.id3->length -
323 wps_state.id3->elapsed;
325 else {
326 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
327 ff_rewind_count = -wps_state.id3->elapsed;
330 /* set the wps state ff_rewind_count so the progess info
331 displays corectly */
332 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
333 ff_rewind_count:-ff_rewind_count;
334 FOR_NB_SCREENS(i)
336 skin_update(&gui_wps[i],
337 WPS_REFRESH_PLAYER_PROGRESS |
338 WPS_REFRESH_DYNAMIC);
341 break;
343 case ACTION_WPS_STOPSEEK:
344 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
345 audio_ff_rewind(wps_state.id3->elapsed);
346 wps_state.ff_rewind_count = 0;
347 wps_state.ff_rewind = false;
348 status_set_ffmode(0);
349 #if (CONFIG_CODEC != SWCODEC)
350 if (!wps_state.paused)
351 audio_resume();
352 #endif
353 #ifdef HAVE_LCD_CHARCELLS
354 FOR_NB_SCREENS(i)
355 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
356 #endif
357 exit = true;
358 break;
360 default:
361 if(default_event_handler(button) == SYS_USB_CONNECTED) {
362 status_set_ffmode(0);
363 usb = true;
364 exit = true;
366 break;
368 if (!exit)
370 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
371 #ifdef HAVE_TOUCHSCREEN
372 if (button == ACTION_TOUCHSCREEN)
373 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
374 #endif
377 return usb;
381 void display_keylock_text(bool locked)
383 int i;
384 FOR_NB_SCREENS(i)
385 gui_wps[i].display->stop_scroll();
387 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
393 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
394 static void gwps_caption_backlight(struct wps_state *state)
396 if (state && state->id3)
398 #ifdef HAVE_BACKLIGHT
399 if (global_settings.caption_backlight)
401 /* turn on backlight n seconds before track ends, and turn it off n
402 seconds into the new track. n == backlight_timeout, or 5s */
403 int n = global_settings.backlight_timeout * 1000;
405 if ( n < 1000 )
406 n = 5000; /* use 5s if backlight is always on or off */
408 if (((state->id3->elapsed < 1000) ||
409 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
410 (state->paused == false))
411 backlight_on();
413 #endif
414 #ifdef HAVE_REMOTE_LCD
415 if (global_settings.remote_caption_backlight)
417 /* turn on remote backlight n seconds before track ends, and turn it
418 off n seconds into the new track. n == remote_backlight_timeout,
419 or 5s */
420 int n = global_settings.remote_backlight_timeout * 1000;
422 if ( n < 1000 )
423 n = 5000; /* use 5s if backlight is always on or off */
425 if (((state->id3->elapsed < 1000) ||
426 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
427 (state->paused == false))
428 remote_backlight_on();
430 #endif
433 #endif
436 static void change_dir(int direction)
438 if (global_settings.prevent_skip)
439 return;
441 if (direction < 0)
442 audio_prev_dir();
443 else if (direction > 0)
444 audio_next_dir();
445 /* prevent the next dir to immediatly start being ffw'd */
446 action_wait_for_release();
449 static void prev_track(unsigned long skip_thresh)
451 if (wps_state.id3->elapsed < skip_thresh)
453 audio_prev();
454 return;
456 else
458 if (wps_state.id3->cuesheet)
460 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
461 return;
464 if (!wps_state.paused)
465 #if (CONFIG_CODEC == SWCODEC)
466 audio_pre_ff_rewind();
467 #else
468 audio_pause();
469 #endif
471 audio_ff_rewind(0);
473 #if (CONFIG_CODEC != SWCODEC)
474 if (!wps_state.paused)
475 audio_resume();
476 #endif
480 static void next_track(void)
482 /* take care of if we're playing a cuesheet */
483 if (wps_state.id3->cuesheet)
485 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
487 /* if the result was false, then we really want
488 to skip to the next track */
489 return;
493 audio_next();
496 static void play_hop(int direction)
498 unsigned long step = ((unsigned long)global_settings.skip_length)*1000;
499 unsigned long elapsed = wps_state.id3->elapsed;
500 unsigned long remaining = wps_state.id3->length - elapsed;
502 if (!global_settings.prevent_skip &&
503 (!step ||
504 (direction > 0 && step >= remaining) ||
505 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
506 { /* Do normal track skipping */
507 if (direction > 0)
508 next_track();
509 else if (direction < 0)
510 prev_track(DEFAULT_SKIP_TRESH);
511 return;
514 if (direction == 1 && step >= remaining)
516 #if CONFIG_CODEC == SWCODEC
517 if(global_settings.beep)
518 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
519 #endif
520 return;
522 else if ((direction == -1 && elapsed < step))
524 elapsed = 0;
526 else
528 elapsed += step * direction;
530 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
532 #if (CONFIG_CODEC == SWCODEC)
533 audio_pre_ff_rewind();
534 #else
535 audio_pause();
536 #endif
538 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
539 #if (CONFIG_CODEC != SWCODEC)
540 if (!wps_state.paused)
541 audio_resume();
542 #endif
546 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
548 * If the user is unable to see the wps, because the display is deactivated,
549 * we suppress updates until the wps is activated again (the lcd driver will
550 * call this hook to issue an instant update)
551 * */
552 static void wps_lcd_activation_hook(void)
554 wps_state.do_full_update = true;
555 /* force timeout in wps main loop, so that the update is instantly */
556 queue_post(&button_queue, BUTTON_NONE, 0);
558 #endif
560 static void gwps_leave_wps(void)
562 int i, oldbars = VP_SB_HIDE_ALL;
564 FOR_NB_SCREENS(i)
566 gui_wps[i].display->stop_scroll();
567 gui_wps[i].display->backdrop_show(BACKDROP_MAIN);
568 if (statusbar_position(i) != STATUSBAR_OFF)
569 oldbars |= VP_SB_ONSCREEN(i);
572 viewportmanager_set_statusbar(oldbars);
573 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
574 /* Play safe and unregister the hook */
575 lcd_activation_set_hook(NULL);
576 #endif
577 send_event(GUI_EVENT_REFRESH, NULL);
580 #ifdef HAVE_TOUCHSCREEN
581 int wps_get_touchaction(struct wps_data *data)
583 short x,y;
584 short vx, vy;
585 int type = action_get_touchscreen_press(&x, &y);
586 static int last_action = ACTION_NONE;
587 struct touchregion *r;
588 bool repeated = (type == BUTTON_REPEAT);
589 bool released = (type == BUTTON_REL);
590 struct skin_token_list *regions = data->touchregions;
591 while (regions)
593 r = (struct touchregion *)regions->token->value.data;
594 /* make sure this region's viewport is visible */
595 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
597 regions = regions->next;
598 continue;
600 /* check if it's inside this viewport */
601 if (viewport_point_within_vp(&(r->wvp->vp), x, y))
602 { /* reposition the touch inside the viewport since touchregions
603 * are relative to a preceding viewport */
604 vx = x - r->wvp->vp.x;
605 vy = y - r->wvp->vp.y;
606 /* now see if the point is inside this region */
607 if (vx >= r->x && vx < r->x+r->width &&
608 vy >= r->y && vy < r->y+r->height)
610 /* reposition the touch within the area */
611 vx -= r->x;
612 vy -= r->y;
614 switch(r->type)
616 case WPS_TOUCHREGION_ACTION:
617 if ((repeated && r->repeat) || (released && !r->repeat))
619 last_action = r->action;
620 return r->action;
622 break;
623 case WPS_TOUCHREGION_SCROLLBAR:
624 if(r->width > r->height)
625 /* landscape */
626 wps_state.id3->elapsed = (vx *
627 wps_state.id3->length) / r->width;
628 else
629 /* portrait */
630 wps_state.id3->elapsed = (vy *
631 wps_state.id3->length) / r->height;
633 if (!wps_state.paused)
634 #if (CONFIG_CODEC == SWCODEC)
635 audio_pre_ff_rewind();
636 #else
637 audio_pause();
638 #endif
639 audio_ff_rewind(wps_state.id3->elapsed);
640 #if (CONFIG_CODEC != SWCODEC)
641 if (!wps_state.paused)
642 audio_resume();
643 #endif
644 break;
645 case WPS_TOUCHREGION_VOLUME:
647 int i;
648 const int min_vol = sound_min(SOUND_VOLUME);
649 const int max_vol = sound_max(SOUND_VOLUME);
650 if(r->width > r->height)
651 /* landscape */
652 global_settings.volume = (vx *
653 (max_vol - min_vol)) / r->width;
654 else
655 /* portrait */
656 global_settings.volume = (vy *
657 (max_vol-min_vol)) / r->height;
659 global_settings.volume += min_vol;
660 setvol();
661 FOR_NB_SCREENS(i)
663 gui_wps[i].data->button_time_volume = current_tick;
665 return ACTION_REDRAW;
670 regions = regions->next;
673 if ((last_action == ACTION_WPS_SEEKBACK || last_action == ACTION_WPS_SEEKFWD))
674 return ACTION_WPS_STOPSEEK;
675 last_action = ACTION_TOUCHSCREEN;
676 return ACTION_TOUCHSCREEN;
678 #endif
679 /* The WPS can be left in two ways:
680 * a) call a function, which draws over the wps. In this case, the wps
681 * will be still active (i.e. the below function didn't return)
682 * b) return with a value evaluated by root_menu.c, in this case the wps
683 * is really left, and root_menu will handle the next screen
685 * In either way, call gwps_leave_wps(), in order to restore the correct
686 * "main screen" backdrops and statusbars
688 long gui_wps_show(void)
690 long button = 0;
691 bool restore = true;
692 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
693 bool exit = false;
694 bool bookmark = false;
695 bool update = false;
696 int i;
697 long last_left = 0, last_right = 0;
699 #ifdef HAVE_LCD_CHARCELLS
700 status_set_audio(true);
701 status_set_param(false);
702 #else
703 statusbar_toggle_handler(NULL);
704 #endif
706 #ifdef AB_REPEAT_ENABLE
707 ab_repeat_init();
708 ab_reset_markers();
709 #endif
710 wps_state_init();
712 while ( 1 )
714 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
716 /* did someone else (i.e power thread) change audio pause mode? */
717 if (wps_state.paused != audio_paused) {
718 wps_state.paused = audio_paused;
720 /* if another thread paused audio, we are probably in car mode,
721 about to shut down. lets save the settings. */
722 if (wps_state.paused) {
723 settings_save();
724 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
725 call_storage_idle_notifys(true);
726 #endif
730 #ifdef HAVE_LCD_BITMAP
731 /* when the peak meter is enabled we want to have a
732 few extra updates to make it look smooth. On the
733 other hand we don't want to waste energy if it
734 isn't displayed */
735 bool pm=false;
736 FOR_NB_SCREENS(i)
738 if(gui_wps[i].data->peak_meter_enabled)
739 pm = true;
742 if (pm) {
743 long next_refresh = current_tick;
744 long next_big_refresh = current_tick + HZ / 5;
745 button = BUTTON_NONE;
746 while (TIME_BEFORE(current_tick, next_big_refresh)) {
747 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_NOBLOCK);
748 if (button != ACTION_NONE) {
749 break;
751 peak_meter_peek();
752 sleep(0); /* Sleep until end of current tick. */
754 if (TIME_AFTER(current_tick, next_refresh)) {
755 FOR_NB_SCREENS(i)
757 if(gui_wps[i].data->peak_meter_enabled)
758 skin_update(&gui_wps[i], WPS_REFRESH_PEAK_METER);
759 next_refresh += HZ / PEAK_METER_FPS;
766 /* The peak meter is disabled
767 -> no additional screen updates needed */
768 else
769 #endif
771 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,
772 restore ? HZ/100 : HZ/5);
775 /* Exit if audio has stopped playing. This happens e.g. at end of
776 playlist or if using the sleep timer. */
777 if (!(audio_status() & AUDIO_STATUS_PLAY))
778 exit = true;
779 #ifdef HAVE_TOUCHSCREEN
780 if (button == ACTION_TOUCHSCREEN)
781 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
782 #endif
783 /* The iPods/X5/M5 use a single button for the A-B mode markers,
784 defined as ACTION_WPSAB_SINGLE in their config files. */
785 #ifdef ACTION_WPSAB_SINGLE
786 if (!global_settings.party_mode && ab_repeat_mode_enabled())
788 static int wps_ab_state = 0;
789 if (button == ACTION_WPSAB_SINGLE)
791 switch (wps_ab_state)
793 case 0: /* set the A spot */
794 button = ACTION_WPS_ABSETA_PREVDIR;
795 break;
796 case 1: /* set the B spot */
797 button = ACTION_WPS_ABSETB_NEXTDIR;
798 break;
799 case 2:
800 button = ACTION_WPS_ABRESET;
801 break;
803 wps_ab_state = (wps_ab_state+1) % 3;
806 #endif
807 switch(button)
809 case ACTION_WPS_CONTEXT:
811 gwps_leave_wps();
812 /* if music is stopped in the context menu we want to exit the wps */
813 if (onplay(wps_state.id3->path,
814 FILE_ATTR_AUDIO, CONTEXT_WPS) == ONPLAY_MAINMENU
815 || !audio_status())
816 return GO_TO_ROOT;
817 restore = true;
819 break;
821 case ACTION_WPS_BROWSE:
822 #ifdef HAVE_LCD_CHARCELLS
823 status_set_record(false);
824 status_set_audio(false);
825 #endif
826 gwps_leave_wps();
827 return GO_TO_PREVIOUS_BROWSER;
828 break;
830 /* play/pause */
831 case ACTION_WPS_PLAY:
832 if (global_settings.party_mode)
833 break;
834 if ( wps_state.paused )
836 wps_state.paused = false;
837 if ( global_settings.fade_on_stop )
838 fade(true, true);
839 else
840 audio_resume();
842 else
844 wps_state.paused = true;
845 if ( global_settings.fade_on_stop )
846 fade(false, true);
847 else
848 audio_pause();
849 settings_save();
850 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
851 call_storage_idle_notifys(true); /* make sure resume info is saved */
852 #endif
854 break;
856 case ACTION_WPS_VOLUP:
858 FOR_NB_SCREENS(i)
859 gui_wps[i].data->button_time_volume = current_tick;
860 global_settings.volume++;
861 bool res = false;
862 setvol();
863 FOR_NB_SCREENS(i)
865 if(update_onvol_change(&gui_wps[i]))
866 res = true;
868 if (res) {
869 restore = true;
870 restoretimer = RESTORE_WPS_NEXT_SECOND;
873 break;
874 case ACTION_WPS_VOLDOWN:
876 FOR_NB_SCREENS(i)
877 gui_wps[i].data->button_time_volume = current_tick;
878 global_settings.volume--;
879 setvol();
880 bool res = false;
881 FOR_NB_SCREENS(i)
883 if(update_onvol_change(&gui_wps[i]))
884 res = true;
886 if (res) {
887 restore = true;
888 restoretimer = RESTORE_WPS_NEXT_SECOND;
891 break;
892 /* fast forward
893 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
894 case ACTION_WPS_SEEKFWD:
895 if (global_settings.party_mode)
896 break;
897 if (current_tick -last_right < HZ)
899 if (wps_state.id3->cuesheet)
901 audio_next();
903 else
905 change_dir(1);
908 else
909 ffwd_rew(ACTION_WPS_SEEKFWD);
910 last_right = last_left = 0;
911 break;
912 /* fast rewind
913 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
914 case ACTION_WPS_SEEKBACK:
915 if (global_settings.party_mode)
916 break;
917 if (current_tick -last_left < HZ)
919 if (wps_state.id3->cuesheet)
921 if (!wps_state.paused)
922 #if (CONFIG_CODEC == SWCODEC)
923 audio_pre_ff_rewind();
924 #else
925 audio_pause();
926 #endif
927 audio_ff_rewind(0);
929 else
931 change_dir(-1);
934 else
935 ffwd_rew(ACTION_WPS_SEEKBACK);
936 last_left = last_right = 0;
937 break;
939 /* prev / restart */
940 case ACTION_WPS_SKIPPREV:
941 if (global_settings.party_mode)
942 break;
943 last_left = current_tick;
944 #ifdef AB_REPEAT_ENABLE
945 /* if we're in A/B repeat mode and the current position
946 is past the A marker, jump back to the A marker... */
947 if ( ab_repeat_mode_enabled() )
949 if ( ab_after_A_marker(wps_state.id3->elapsed) )
951 ab_jump_to_A_marker();
952 break;
953 #if (AB_REPEAT_ENABLE == 2)
954 } else {
955 ab_reset_markers();
956 #endif
959 else
960 /* ...otherwise, do it normally */
961 #endif
962 play_hop(-1);
963 break;
965 /* next
966 OR if skip length set, hop by predetermined amount. */
967 case ACTION_WPS_SKIPNEXT:
968 if (global_settings.party_mode)
969 break;
970 last_right = current_tick;
971 #ifdef AB_REPEAT_ENABLE
972 /* if we're in A/B repeat mode and the current position is
973 before the A marker, jump to the A marker... */
974 if ( ab_repeat_mode_enabled() )
976 if ( ab_before_A_marker(wps_state.id3->elapsed) )
978 ab_jump_to_A_marker();
979 break;
980 #if (AB_REPEAT_ENABLE == 2)
981 } else {
982 ab_reset_markers();
983 #endif
986 else
987 /* ...otherwise, do it normally */
988 #endif
989 play_hop(1);
990 break;
991 /* next / prev directories */
992 /* and set A-B markers if in a-b mode */
993 case ACTION_WPS_ABSETB_NEXTDIR:
994 if (global_settings.party_mode)
995 break;
996 #if defined(AB_REPEAT_ENABLE)
997 if (ab_repeat_mode_enabled())
999 ab_set_B_marker(wps_state.id3->elapsed);
1000 ab_jump_to_A_marker();
1002 else
1003 #endif
1005 change_dir(1);
1007 break;
1008 case ACTION_WPS_ABSETA_PREVDIR:
1009 if (global_settings.party_mode)
1010 break;
1011 #if defined(AB_REPEAT_ENABLE)
1012 if (ab_repeat_mode_enabled())
1013 ab_set_A_marker(wps_state.id3->elapsed);
1014 else
1015 #endif
1017 change_dir(-1);
1019 break;
1020 /* menu key functions */
1021 case ACTION_WPS_MENU:
1022 gwps_leave_wps();
1023 return GO_TO_ROOT;
1024 break;
1027 #ifdef HAVE_QUICKSCREEN
1028 case ACTION_WPS_QUICKSCREEN:
1030 gwps_leave_wps();
1031 if (quick_screen_quick(button))
1032 return GO_TO_ROOT;
1033 restore = true;
1035 break;
1036 #endif /* HAVE_QUICKSCREEN */
1038 /* screen settings */
1039 #ifdef BUTTON_F3
1040 case ACTION_F3:
1042 gwps_leave_wps();
1043 if (quick_screen_f3(BUTTON_F3))
1044 return GO_TO_ROOT;
1045 restore = true;
1047 break;
1048 #endif /* BUTTON_F3 */
1050 /* pitch screen */
1051 #ifdef HAVE_PITCHSCREEN
1052 case ACTION_WPS_PITCHSCREEN:
1054 gwps_leave_wps();
1055 if (1 == gui_syncpitchscreen_run())
1056 return GO_TO_ROOT;
1057 restore = true;
1059 break;
1060 #endif /* HAVE_PITCHSCREEN */
1062 #ifdef AB_REPEAT_ENABLE
1063 /* reset A&B markers */
1064 case ACTION_WPS_ABRESET:
1065 if (ab_repeat_mode_enabled())
1067 ab_reset_markers();
1068 update = true;
1070 break;
1071 #endif /* AB_REPEAT_ENABLE */
1073 /* stop and exit wps */
1074 case ACTION_WPS_STOP:
1075 if (global_settings.party_mode)
1076 break;
1077 bookmark = true;
1078 exit = true;
1079 break;
1081 case ACTION_WPS_ID3SCREEN:
1083 gwps_leave_wps();
1084 if (browse_id3())
1085 return GO_TO_ROOT;
1086 restore = true;
1088 break;
1089 #ifdef HAVE_TOUCHSCREEN
1090 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1092 global_settings.playlist_shuffle =
1093 !global_settings.playlist_shuffle;
1094 #if CONFIG_CODEC == SWCODEC
1095 dsp_set_replaygain();
1096 #endif
1097 if (global_settings.playlist_shuffle)
1098 playlist_randomise(NULL, current_tick, true);
1099 else
1100 playlist_sort(NULL, true);
1102 break;
1103 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1105 const struct settings_list *rep_setting =
1106 find_setting(&global_settings.repeat_mode, NULL);
1107 option_select_next_val(rep_setting, false, true);
1108 audio_flush_and_reload_tracks();
1110 break;
1111 #endif /* HAVE_TOUCHSCREEN */
1112 /* this case is used by the softlock feature
1113 * it requests a full update here */
1114 case ACTION_REDRAW:
1115 wps_state.do_full_update = true;
1116 break;
1117 case ACTION_NONE: /* Timeout, do a partial update */
1118 update = true;
1119 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1120 break;
1121 #ifdef HAVE_RECORDING
1122 case ACTION_WPS_REC:
1123 exit = true;
1124 break;
1125 #endif
1126 case SYS_POWEROFF:
1127 default_event_handler(SYS_POWEROFF);
1128 break;
1129 case ACTION_WPS_VIEW_PLAYLIST:
1130 gwps_leave_wps();
1131 if (playlist_viewer()) /* true if USB connected */
1132 return GO_TO_ROOT;
1133 restore = true;
1134 break;
1135 default:
1136 if(default_event_handler(button) == SYS_USB_CONNECTED)
1138 gwps_leave_wps();
1139 return GO_TO_ROOT;
1141 update = true;
1142 break;
1145 if (wps_state.do_full_update || update)
1147 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1148 gwps_caption_backlight(&wps_state);
1149 #endif
1150 FOR_NB_SCREENS(i)
1152 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1153 if (lcd_active()
1154 #ifdef HAVE_REMOTE_LCD
1155 /* currently, all remotes are readable without backlight
1156 * so still update those */
1157 || (i == SCREEN_REMOTE)
1158 #endif
1160 #endif
1162 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
1165 wps_state.do_full_update = false;
1166 update = false;
1169 if (restore && wps_state.id3 &&
1170 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1171 TIME_AFTER(current_tick, restoretimer)))
1173 restore = false;
1174 restoretimer = RESTORE_WPS_INSTANTLY;
1175 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1176 lcd_activation_set_hook(wps_lcd_activation_hook);
1177 #endif
1178 FOR_NB_SCREENS(i)
1180 screens[i].stop_scroll();
1181 gui_wps_display(&gui_wps[i]);
1185 if (exit) {
1186 #ifdef HAVE_LCD_CHARCELLS
1187 status_set_record(false);
1188 status_set_audio(false);
1189 #endif
1190 if (global_settings.fade_on_stop)
1191 fade(false, true);
1193 if (bookmark)
1194 bookmark_autobookmark();
1195 audio_stop();
1196 #ifdef AB_REPEAT_ENABLE
1197 ab_reset_markers();
1198 #endif
1199 gwps_leave_wps();
1200 #ifdef HAVE_RECORDING
1201 if (button == ACTION_WPS_REC)
1202 return GO_TO_RECSCREEN;
1203 #endif
1204 if (global_settings.browse_current)
1205 return GO_TO_PREVIOUS_BROWSER;
1206 return GO_TO_PREVIOUS;
1209 if (button && !IS_SYSEVENT(button) )
1210 storage_spin();
1212 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1215 /* this is called from the playback thread so NO DRAWING! */
1216 static void track_changed_callback(void *param)
1218 wps_state.id3 = (struct mp3entry*)param;
1219 wps_state.nid3 = audio_next_track();
1220 if (wps_state.id3->cuesheet)
1222 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1223 cue_spoof_id3(wps_state.id3->cuesheet, wps_state.id3);
1225 wps_state.do_full_update = true;
1227 static void nextid3available_callback(void* param)
1229 (void)param;
1230 wps_state.nid3 = audio_next_track();
1231 wps_state.do_full_update = true;
1235 static void wps_state_init(void)
1237 wps_state.ff_rewind = false;
1238 wps_state.paused = false;
1239 if(audio_status() & AUDIO_STATUS_PLAY)
1241 wps_state.id3 = audio_current_track();
1242 wps_state.nid3 = audio_next_track();
1244 else
1246 wps_state.id3 = NULL;
1247 wps_state.nid3 = NULL;
1249 /* We'll be updating due to restore initialized with true */
1250 wps_state.do_full_update = false;
1251 /* add the WPS track event callbacks */
1252 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1253 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1257 #ifdef HAVE_LCD_BITMAP
1258 static void statusbar_toggle_handler(void *data)
1260 (void)data;
1261 int i;
1263 wpsbars = VP_SB_HIDE_ALL;
1264 FOR_NB_SCREENS(i)
1265 { /* fix viewports if needed */
1266 skin_statusbar_changed(&gui_wps[i]);
1268 bool draw = false;
1270 /* fix up gui_wps::statusbars, so that the viewportmanager accepts it*/
1271 if (gui_wps[i].data->wps_sb_tag)
1272 draw = gui_wps[i].data->show_sb_on_wps;
1273 else if (statusbar_position(i) != STATUSBAR_OFF)
1274 draw = true;
1275 if (draw)
1276 wpsbars |=
1277 (VP_SB_ONSCREEN(i) | VP_SB_IGNORE_SETTING(i));
1280 #endif
1283 void gui_sync_wps_init(void)
1285 int i;
1286 FOR_NB_SCREENS(i)
1288 #ifdef HAVE_ALBUMART
1289 wps_datas[i].albumart = NULL;
1290 wps_datas[i].playback_aa_slot = -1;
1291 #endif
1292 #ifdef HAVE_REMOTE_LCD
1293 wps_datas[i].remote_wps = (i == SCREEN_REMOTE);
1294 #endif
1295 wps_datas[i].debug = false;
1296 gui_wps[i].data = &wps_datas[i];
1297 gui_wps[i].display = &screens[i];
1298 /* Currently no seperate wps_state needed/possible
1299 so use the only available ( "global" ) one */
1300 gui_wps[i].state = &wps_state;
1301 gui_wps[i].display->backdrop_unload(BACKDROP_SKIN_WPS);
1302 /* only one wpsbars needed/wanted */
1303 gui_wps[i].statusbars = &wpsbars;
1305 *(gui_wps[SCREEN_MAIN].statusbars) =VP_SB_ALLSCREENS;
1306 #ifdef HAVE_LCD_BITMAP
1307 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggle_handler);
1308 #endif
1312 #ifdef IPOD_ACCESSORY_PROTOCOL
1313 int wps_get_ff_rewind_count(void)
1315 return wps_state.ff_rewind_count;
1317 #endif