Touchscreen targets: add basic progress bar & volume handling
[kugel-rb.git] / apps / gui / music_screen.c
blobd5eccd5868a473dfe88562653f8a2b5a5853e439
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 "wps_engine/wps_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 "music_screen.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
76 static int wpsbars;
77 /* currently only one wps_state is needed */
78 struct wps_state wps_state;
79 struct gui_wps gui_wps[NB_SCREENS];
80 static struct wps_data wps_datas[NB_SCREENS];
82 /* initial setup of wps_data */
83 static void wps_state_init(void);
84 static void track_changed_callback(void *param);
85 static void nextid3available_callback(void* param);
89 #define FF_REWIND_MAX_PERCENT 3 /* cap ff/rewind step size at max % of file */
90 /* 3% of 30min file == 54s step size */
91 #define MIN_FF_REWIND_STEP 500
93 bool wps_fading_out = false;
94 void fade(bool fade_in, bool updatewps)
96 int fp_global_vol = global_settings.volume << 8;
97 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
98 int fp_step = (fp_global_vol - fp_min_vol) / 30;
99 int i;
100 wps_fading_out = !fade_in;
101 if (fade_in) {
102 /* fade in */
103 int fp_volume = fp_min_vol;
105 /* zero out the sound */
106 sound_set_volume(fp_min_vol >> 8);
108 sleep(HZ/10); /* let audio thread run */
109 audio_resume();
111 while (fp_volume < fp_global_vol - fp_step) {
112 fp_volume += fp_step;
113 sound_set_volume(fp_volume >> 8);
114 if (updatewps)
116 FOR_NB_SCREENS(i)
117 gui_wps_redraw(&gui_wps[i], 0, WPS_REFRESH_NON_STATIC);
119 sleep(1);
121 sound_set_volume(global_settings.volume);
123 else {
124 /* fade out */
125 int fp_volume = fp_global_vol;
127 while (fp_volume > fp_min_vol + fp_step) {
128 fp_volume -= fp_step;
129 sound_set_volume(fp_volume >> 8);
130 if (updatewps)
132 FOR_NB_SCREENS(i)
133 gui_wps_redraw(&gui_wps[i], 0, WPS_REFRESH_NON_STATIC);
135 sleep(1);
137 audio_pause();
138 wps_fading_out = false;
139 #if CONFIG_CODEC != SWCODEC
140 #ifndef SIMULATOR
141 /* let audio thread run and wait for the mas to run out of data */
142 while (!mp3_pause_done())
143 #endif
144 sleep(HZ/10);
145 #endif
147 /* reset volume to what it was before the fade */
148 sound_set_volume(global_settings.volume);
151 bool is_wps_fading(void)
153 return wps_fading_out;
156 bool update_onvol_change(struct gui_wps * gwps)
158 gui_wps_redraw(gwps, 0, WPS_REFRESH_NON_STATIC);
160 #ifdef HAVE_LCD_CHARCELLS
161 splashf(0, "Vol: %3d dB",
162 sound_val2phys(SOUND_VOLUME, global_settings.volume));
163 return true;
164 #endif
165 return false;
168 bool ffwd_rew(int button)
170 unsigned int step = 0; /* current ff/rewind step */
171 unsigned int max_step = 0; /* maximum ff/rewind step */
172 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
173 int direction = -1; /* forward=1 or backward=-1 */
174 bool exit = false;
175 bool usb = false;
176 int i = 0;
177 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
179 if (button == ACTION_NONE)
181 status_set_ffmode(0);
182 return usb;
184 while (!exit)
186 switch ( button )
188 case ACTION_WPS_SEEKFWD:
189 direction = 1;
190 case ACTION_WPS_SEEKBACK:
191 if (wps_state.ff_rewind)
193 if (direction == 1)
195 /* fast forwarding, calc max step relative to end */
196 max_step = (wps_state.id3->length -
197 (wps_state.id3->elapsed +
198 ff_rewind_count)) *
199 FF_REWIND_MAX_PERCENT / 100;
201 else
203 /* rewinding, calc max step relative to start */
204 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
205 FF_REWIND_MAX_PERCENT / 100;
208 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
210 if (step > max_step)
211 step = max_step;
213 ff_rewind_count += step * direction;
215 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
216 step += step >> ff_rw_accel;
218 else
220 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
221 wps_state.id3 && wps_state.id3->length )
223 if (!wps_state.paused)
224 #if (CONFIG_CODEC == SWCODEC)
225 audio_pre_ff_rewind();
226 #else
227 audio_pause();
228 #endif
229 #if CONFIG_KEYPAD == PLAYER_PAD
230 FOR_NB_SCREENS(i)
231 gui_wps[i].display->stop_scroll();
232 #endif
233 if (direction > 0)
234 status_set_ffmode(STATUS_FASTFORWARD);
235 else
236 status_set_ffmode(STATUS_FASTBACKWARD);
238 wps_state.ff_rewind = true;
240 step = 1000 * global_settings.ff_rewind_min_step;
242 else
243 break;
246 if (direction > 0) {
247 if ((wps_state.id3->elapsed + ff_rewind_count) >
248 wps_state.id3->length)
249 ff_rewind_count = wps_state.id3->length -
250 wps_state.id3->elapsed;
252 else {
253 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
254 ff_rewind_count = -wps_state.id3->elapsed;
257 FOR_NB_SCREENS(i)
258 gui_wps_redraw(&gui_wps[i],
259 (wps_state.wps_time_countup == false)?
260 ff_rewind_count:-ff_rewind_count,
261 WPS_REFRESH_PLAYER_PROGRESS |
262 WPS_REFRESH_DYNAMIC);
264 break;
266 case ACTION_WPS_STOPSEEK:
267 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
268 audio_ff_rewind(wps_state.id3->elapsed);
269 ff_rewind_count = 0;
270 wps_state.ff_rewind = false;
271 status_set_ffmode(0);
272 #if (CONFIG_CODEC != SWCODEC)
273 if (!wps_state.paused)
274 audio_resume();
275 #endif
276 #ifdef HAVE_LCD_CHARCELLS
277 FOR_NB_SCREENS(i)
278 gui_wps_redraw(&gui_wps[i],0, WPS_REFRESH_ALL);
279 #endif
280 exit = true;
281 break;
283 default:
284 if(default_event_handler(button) == SYS_USB_CONNECTED) {
285 status_set_ffmode(0);
286 usb = true;
287 exit = true;
289 break;
291 if (!exit)
293 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
294 #ifdef HAVE_TOUCHSCREEN
295 if (button == ACTION_TOUCHSCREEN)
296 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
297 #endif
300 return usb;
304 void display_keylock_text(bool locked)
306 int i;
307 FOR_NB_SCREENS(i)
308 gui_wps[i].display->stop_scroll();
310 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
316 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
317 static void gwps_caption_backlight(struct wps_state *state)
319 if (state && state->id3)
321 #ifdef HAVE_BACKLIGHT
322 if (global_settings.caption_backlight)
324 /* turn on backlight n seconds before track ends, and turn it off n
325 seconds into the new track. n == backlight_timeout, or 5s */
326 int n = global_settings.backlight_timeout * 1000;
328 if ( n < 1000 )
329 n = 5000; /* use 5s if backlight is always on or off */
331 if (((state->id3->elapsed < 1000) ||
332 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
333 (state->paused == false))
334 backlight_on();
336 #endif
337 #ifdef HAVE_REMOTE_LCD
338 if (global_settings.remote_caption_backlight)
340 /* turn on remote backlight n seconds before track ends, and turn it
341 off n seconds into the new track. n == remote_backlight_timeout,
342 or 5s */
343 int n = global_settings.remote_backlight_timeout * 1000;
345 if ( n < 1000 )
346 n = 5000; /* use 5s if backlight is always on or off */
348 if (((state->id3->elapsed < 1000) ||
349 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
350 (state->paused == false))
351 remote_backlight_on();
353 #endif
356 #endif
359 static void change_dir(int direction)
361 if (global_settings.prevent_skip)
362 return;
364 if (direction < 0)
365 audio_prev_dir();
366 else if (direction > 0)
367 audio_next_dir();
368 /* prevent the next dir to immediatly start being ffw'd */
369 action_wait_for_release();
372 static void prev_track(unsigned long skip_thresh)
374 if (wps_state.id3->elapsed < skip_thresh)
376 audio_prev();
377 return;
379 else
381 if (wps_state.id3->cuesheet)
383 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
384 return;
387 if (!wps_state.paused)
388 #if (CONFIG_CODEC == SWCODEC)
389 audio_pre_ff_rewind();
390 #else
391 audio_pause();
392 #endif
394 audio_ff_rewind(0);
396 #if (CONFIG_CODEC != SWCODEC)
397 if (!wps_state.paused)
398 audio_resume();
399 #endif
403 static void next_track(void)
405 /* take care of if we're playing a cuesheet */
406 if (wps_state.id3->cuesheet)
408 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
410 /* if the result was false, then we really want
411 to skip to the next track */
412 return;
416 audio_next();
419 static void play_hop(int direction)
421 unsigned long step = ((unsigned long)global_settings.skip_length)*1000;
422 unsigned long elapsed = wps_state.id3->elapsed;
423 unsigned long remaining = wps_state.id3->length - elapsed;
425 if (!global_settings.prevent_skip &&
426 (!step ||
427 (direction > 0 && step >= remaining) ||
428 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
429 { /* Do normal track skipping */
430 if (direction > 0)
431 next_track();
432 else if (direction < 0)
433 prev_track(DEFAULT_SKIP_TRESH);
434 return;
437 if (direction == 1 && step >= remaining)
439 #if CONFIG_CODEC == SWCODEC
440 if(global_settings.beep)
441 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
442 #endif
443 return;
445 else if ((direction == -1 && elapsed < step))
447 elapsed = 0;
449 else
451 elapsed += step * direction;
453 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
455 #if (CONFIG_CODEC == SWCODEC)
456 audio_pre_ff_rewind();
457 #else
458 audio_pause();
459 #endif
461 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
462 #if (CONFIG_CODEC != SWCODEC)
463 if (!wps_state.paused)
464 audio_resume();
465 #endif
468 static void gwps_fix_statusbars(void)
470 #ifdef HAVE_LCD_BITMAP
471 int i;
472 wpsbars = VP_SB_HIDE_ALL;
473 FOR_NB_SCREENS(i)
475 bool draw = false;
476 if (gui_wps[i].data->wps_sb_tag)
477 draw = gui_wps[i].data->show_sb_on_wps;
478 else if (statusbar_position(i) != STATUSBAR_OFF)
479 draw = true;
480 if (draw)
481 wpsbars |= (VP_SB_ONSCREEN(i) | VP_SB_IGNORE_SETTING(i));
483 #else
484 wpsbars = VP_SB_ALLSCREENS;
485 #endif
488 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
490 * If the user is unable to see the wps, because the display is deactivated,
491 * we suppress updates until the wps is activated again (the lcd driver will
492 * call this hook to issue an instant update)
493 * */
494 static void wps_lcd_activation_hook(void)
496 wps_state.do_full_update = true;
497 /* force timeout in wps main loop, so that the update is instantly */
498 queue_post(&button_queue, BUTTON_NONE, 0);
500 #endif
502 static void gwps_leave_wps(void)
504 int i, oldbars = VP_SB_HIDE_ALL;
506 FOR_NB_SCREENS(i)
507 gui_wps[i].display->stop_scroll();
508 if (global_settings.statusbar)
509 oldbars = VP_SB_ALLSCREENS;
511 #if LCD_DEPTH > 1
512 show_main_backdrop();
513 #endif
514 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
515 show_remote_main_backdrop();
516 #endif
517 viewportmanager_set_statusbar(oldbars);
518 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
519 /* Play safe and unregister the hook */
520 lcd_activation_set_hook(NULL);
521 #endif
524 void gwps_draw_statusbars(void)
526 viewportmanager_set_statusbar(wpsbars);
528 #ifdef HAVE_TOUCHSCREEN
529 int wps_get_touchaction(struct wps_data *data)
531 short x,y;
532 short vx, vy;
533 int type = action_get_touchscreen_press(&x, &y);
534 int i;
535 static int last_action = ACTION_NONE;
536 struct touchregion *r;
537 bool repeated = (type == BUTTON_REPEAT);
538 bool released = (type == BUTTON_REL);
539 for (i=0; i<data->touchregion_count; i++)
541 r = &data->touchregion[i];
542 /* make sure this region's viewport is visible */
543 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
544 continue;
545 /* reposition the touch inside the viewport */
546 vx = x - r->wvp->vp.x;
547 vy = y - r->wvp->vp.y;
548 /* check if it's inside this viewport */
549 if (vx >= 0 && vx < r->wvp->vp.x + r->wvp->vp.width &&
550 vy >= 0 && vy < r->wvp->vp.y + r->wvp->vp.height)
552 /* now see if the point is inside this region */
553 if (vx >= r->x && vx < r->x+r->width &&
554 vy >= r->y && vy < r->y+r->height)
556 /* reposition the touch within the area */
557 vx -= r->x;
558 vy -= r->y;
560 switch(r->type)
562 case WPS_TOUCHREGION_ACTION:
563 if ((repeated && r->repeat) || (released && !r->repeat))
565 last_action = r->action;
566 return r->action;
568 break;
569 case WPS_TOUCHREGION_SCROLLBAR:
570 if(r->width > r->height)
571 /* landscape */
572 wps_state.id3->elapsed = (vx *
573 wps_state.id3->length) / r->width;
574 else
575 /* portrait */
576 wps_state.id3->elapsed = (vy *
577 wps_state.id3->length) / r->height;
579 audio_ff_rewind(wps_state.id3->elapsed);
580 break;
581 case WPS_TOUCHREGION_VOLUME:
583 const int min_vol = sound_min(SOUND_VOLUME);
584 const int max_vol = sound_max(SOUND_VOLUME);
585 if(r->width > r->height)
586 /* landscape */
587 global_settings.volume = (vx *
588 (max_vol - min_vol)) / r->width;
589 else
590 /* portrait */
591 global_settings.volume = (vy *
592 (max_vol-min_vol)) / r->height;
594 global_settings.volume += min_vol;
595 setvol();
596 break;
602 if ((last_action == ACTION_WPS_SEEKBACK || last_action == ACTION_WPS_SEEKFWD))
603 return ACTION_WPS_STOPSEEK;
604 last_action = ACTION_TOUCHSCREEN;
605 return ACTION_TOUCHSCREEN;
607 #endif
608 /* The WPS can be left in two ways:
609 * a) call a function, which draws over the wps. In this case, the wps
610 * will be still active (i.e. the below function didn't return)
611 * b) return with a value evaluated by root_menu.c, in this case the wps
612 * is really left, and root_menu will handle the next screen
614 * In either way, call gwps_leave_wps(), in order to restore the correct
615 * "main screen" backdrops and statusbars
617 long gui_wps_show(void)
619 long button = 0;
620 bool restore = true;
621 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
622 bool exit = false;
623 bool bookmark = false;
624 bool update = false;
625 int i;
626 long last_left = 0, last_right = 0;
628 #ifdef HAVE_LCD_CHARCELLS
629 status_set_audio(true);
630 status_set_param(false);
631 #endif
633 #ifdef AB_REPEAT_ENABLE
634 ab_repeat_init();
635 ab_reset_markers();
636 #endif
637 wps_state_init();
639 while ( 1 )
641 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
643 /* did someone else (i.e power thread) change audio pause mode? */
644 if (wps_state.paused != audio_paused) {
645 wps_state.paused = audio_paused;
647 /* if another thread paused audio, we are probably in car mode,
648 about to shut down. lets save the settings. */
649 if (wps_state.paused) {
650 settings_save();
651 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
652 call_storage_idle_notifys(true);
653 #endif
657 #ifdef HAVE_LCD_BITMAP
658 /* when the peak meter is enabled we want to have a
659 few extra updates to make it look smooth. On the
660 other hand we don't want to waste energy if it
661 isn't displayed */
662 bool pm=false;
663 FOR_NB_SCREENS(i)
665 if(gui_wps[i].data->peak_meter_enabled)
666 pm = true;
669 if (pm) {
670 long next_refresh = current_tick;
671 long next_big_refresh = current_tick + HZ / 5;
672 button = BUTTON_NONE;
673 while (TIME_BEFORE(current_tick, next_big_refresh)) {
674 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_NOBLOCK);
675 if (button != ACTION_NONE) {
676 break;
678 peak_meter_peek();
679 sleep(0); /* Sleep until end of current tick. */
681 if (TIME_AFTER(current_tick, next_refresh)) {
682 FOR_NB_SCREENS(i)
684 if(gui_wps[i].data->peak_meter_enabled)
685 gui_wps_redraw(&gui_wps[i], 0,
686 WPS_REFRESH_PEAK_METER);
687 next_refresh += HZ / PEAK_METER_FPS;
694 /* The peak meter is disabled
695 -> no additional screen updates needed */
696 else
697 #endif
699 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,
700 restore ? HZ/100 : HZ/5);
703 /* Exit if audio has stopped playing. This happens e.g. at end of
704 playlist or if using the sleep timer. */
705 if (!(audio_status() & AUDIO_STATUS_PLAY))
706 exit = true;
707 #ifdef HAVE_TOUCHSCREEN
708 if (button == ACTION_TOUCHSCREEN)
709 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
710 #endif
711 /* The iPods/X5/M5 use a single button for the A-B mode markers,
712 defined as ACTION_WPSAB_SINGLE in their config files. */
713 #ifdef ACTION_WPSAB_SINGLE
714 if (!global_settings.party_mode && ab_repeat_mode_enabled())
716 static int wps_ab_state = 0;
717 if (button == ACTION_WPSAB_SINGLE)
719 switch (wps_ab_state)
721 case 0: /* set the A spot */
722 button = ACTION_WPS_ABSETA_PREVDIR;
723 break;
724 case 1: /* set the B spot */
725 button = ACTION_WPS_ABSETB_NEXTDIR;
726 break;
727 case 2:
728 button = ACTION_WPS_ABRESET;
729 break;
731 wps_ab_state = (wps_ab_state+1) % 3;
734 #endif
735 switch(button)
737 case ACTION_WPS_CONTEXT:
739 gwps_leave_wps();
740 /* if music is stopped in the context menu we want to exit the wps */
741 if (onplay(wps_state.id3->path,
742 FILE_ATTR_AUDIO, CONTEXT_WPS) == ONPLAY_MAINMENU
743 || !audio_status())
744 return GO_TO_ROOT;
745 restore = true;
747 break;
749 case ACTION_WPS_BROWSE:
750 #ifdef HAVE_LCD_CHARCELLS
751 status_set_record(false);
752 status_set_audio(false);
753 #endif
754 gwps_leave_wps();
755 return GO_TO_PREVIOUS_BROWSER;
756 break;
758 /* play/pause */
759 case ACTION_WPS_PLAY:
760 if (global_settings.party_mode)
761 break;
762 if ( wps_state.paused )
764 wps_state.paused = false;
765 if ( global_settings.fade_on_stop )
766 fade(true, true);
767 else
768 audio_resume();
770 else
772 wps_state.paused = true;
773 if ( global_settings.fade_on_stop )
774 fade(false, true);
775 else
776 audio_pause();
777 settings_save();
778 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
779 call_storage_idle_notifys(true); /* make sure resume info is saved */
780 #endif
782 break;
784 case ACTION_WPS_VOLUP:
786 FOR_NB_SCREENS(i)
787 gui_wps[i].data->button_time_volume = current_tick;
788 global_settings.volume++;
789 bool res = false;
790 setvol();
791 FOR_NB_SCREENS(i)
793 if(update_onvol_change(&gui_wps[i]))
794 res = true;
796 if (res) {
797 restore = true;
798 restoretimer = RESTORE_WPS_NEXT_SECOND;
801 break;
802 case ACTION_WPS_VOLDOWN:
804 FOR_NB_SCREENS(i)
805 gui_wps[i].data->button_time_volume = current_tick;
806 global_settings.volume--;
807 setvol();
808 bool res = false;
809 FOR_NB_SCREENS(i)
811 if(update_onvol_change(&gui_wps[i]))
812 res = true;
814 if (res) {
815 restore = true;
816 restoretimer = RESTORE_WPS_NEXT_SECOND;
819 break;
820 /* fast forward
821 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
822 case ACTION_WPS_SEEKFWD:
823 if (global_settings.party_mode)
824 break;
825 if (current_tick -last_right < HZ)
827 if (wps_state.id3->cuesheet)
829 audio_next();
831 else
833 change_dir(1);
836 else
837 ffwd_rew(ACTION_WPS_SEEKFWD);
838 last_right = last_left = 0;
839 break;
840 /* fast rewind
841 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
842 case ACTION_WPS_SEEKBACK:
843 if (global_settings.party_mode)
844 break;
845 if (current_tick -last_left < HZ)
847 if (wps_state.id3->cuesheet)
849 if (!wps_state.paused)
850 #if (CONFIG_CODEC == SWCODEC)
851 audio_pre_ff_rewind();
852 #else
853 audio_pause();
854 #endif
855 audio_ff_rewind(0);
857 else
859 change_dir(-1);
862 else
863 ffwd_rew(ACTION_WPS_SEEKBACK);
864 last_left = last_right = 0;
865 break;
867 /* prev / restart */
868 case ACTION_WPS_SKIPPREV:
869 if (global_settings.party_mode)
870 break;
871 last_left = current_tick;
872 #ifdef AB_REPEAT_ENABLE
873 /* if we're in A/B repeat mode and the current position
874 is past the A marker, jump back to the A marker... */
875 if ( ab_repeat_mode_enabled() )
877 if ( ab_after_A_marker(wps_state.id3->elapsed) )
879 ab_jump_to_A_marker();
880 break;
881 #if (AB_REPEAT_ENABLE == 2)
882 } else {
883 ab_reset_markers();
884 #endif
887 else
888 /* ...otherwise, do it normally */
889 #endif
890 play_hop(-1);
891 break;
893 /* next
894 OR if skip length set, hop by predetermined amount. */
895 case ACTION_WPS_SKIPNEXT:
896 if (global_settings.party_mode)
897 break;
898 last_right = current_tick;
899 #ifdef AB_REPEAT_ENABLE
900 /* if we're in A/B repeat mode and the current position is
901 before the A marker, jump to the A marker... */
902 if ( ab_repeat_mode_enabled() )
904 if ( ab_before_A_marker(wps_state.id3->elapsed) )
906 ab_jump_to_A_marker();
907 break;
908 #if (AB_REPEAT_ENABLE == 2)
909 } else {
910 ab_reset_markers();
911 #endif
914 else
915 /* ...otherwise, do it normally */
916 #endif
917 play_hop(1);
918 break;
919 /* next / prev directories */
920 /* and set A-B markers if in a-b mode */
921 case ACTION_WPS_ABSETB_NEXTDIR:
922 if (global_settings.party_mode)
923 break;
924 #if defined(AB_REPEAT_ENABLE)
925 if (ab_repeat_mode_enabled())
927 ab_set_B_marker(wps_state.id3->elapsed);
928 ab_jump_to_A_marker();
930 else
931 #endif
933 change_dir(1);
935 break;
936 case ACTION_WPS_ABSETA_PREVDIR:
937 if (global_settings.party_mode)
938 break;
939 #if defined(AB_REPEAT_ENABLE)
940 if (ab_repeat_mode_enabled())
941 ab_set_A_marker(wps_state.id3->elapsed);
942 else
943 #endif
945 change_dir(-1);
947 break;
948 /* menu key functions */
949 case ACTION_WPS_MENU:
950 gwps_leave_wps();
951 return GO_TO_ROOT;
952 break;
955 #ifdef HAVE_QUICKSCREEN
956 case ACTION_WPS_QUICKSCREEN:
958 gwps_leave_wps();
959 if (quick_screen_quick(button))
960 return SYS_USB_CONNECTED;
961 restore = true;
963 break;
964 #endif /* HAVE_QUICKSCREEN */
966 /* screen settings */
967 #ifdef BUTTON_F3
968 case ACTION_F3:
970 gwps_leave_wps();
971 if (quick_screen_f3(BUTTON_F3))
972 return SYS_USB_CONNECTED;
973 restore = true;
975 break;
976 #endif /* BUTTON_F3 */
978 /* pitch screen */
979 #ifdef HAVE_PITCHSCREEN
980 case ACTION_WPS_PITCHSCREEN:
982 gwps_leave_wps();
983 if (1 == gui_syncpitchscreen_run())
984 return SYS_USB_CONNECTED;
985 restore = true;
987 break;
988 #endif /* HAVE_PITCHSCREEN */
990 #ifdef AB_REPEAT_ENABLE
991 /* reset A&B markers */
992 case ACTION_WPS_ABRESET:
993 if (ab_repeat_mode_enabled())
995 ab_reset_markers();
996 update = true;
998 break;
999 #endif /* AB_REPEAT_ENABLE */
1001 /* stop and exit wps */
1002 case ACTION_WPS_STOP:
1003 if (global_settings.party_mode)
1004 break;
1005 bookmark = true;
1006 exit = true;
1007 break;
1009 case ACTION_WPS_ID3SCREEN:
1011 gwps_leave_wps();
1012 browse_id3();
1013 restore = true;
1015 break;
1016 #ifdef HAVE_TOUCHSCREEN
1017 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1019 global_settings.playlist_shuffle =
1020 !global_settings.playlist_shuffle;
1021 #if CONFIG_CODEC == SWCODEC
1022 dsp_set_replaygain();
1023 #endif
1024 if (global_settings.playlist_shuffle)
1025 playlist_randomise(NULL, current_tick, true);
1026 else
1027 playlist_sort(NULL, true);
1029 break;
1030 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1032 const struct settings_list *rep_setting =
1033 find_setting(&global_settings.repeat_mode, NULL);
1034 option_select_next_val(rep_setting, false, true);
1035 audio_flush_and_reload_tracks();
1037 break;
1038 #endif /* HAVE_TOUCHSCREEN */
1039 /* this case is used by the softlock feature
1040 * it requests a full update here */
1041 case ACTION_REDRAW:
1042 wps_state.do_full_update = true;
1043 break;
1044 case ACTION_NONE: /* Timeout, do a partial update */
1045 update = true;
1046 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1047 break;
1048 #ifdef HAVE_RECORDING
1049 case ACTION_WPS_REC:
1050 exit = true;
1051 break;
1052 #endif
1053 case SYS_POWEROFF:
1054 default_event_handler(SYS_POWEROFF);
1055 break;
1056 case ACTION_WPS_VIEW_PLAYLIST:
1057 gwps_leave_wps();
1058 if (playlist_viewer()) /* true if USB connected */
1059 return SYS_USB_CONNECTED;
1060 restore = true;
1061 break;
1062 default:
1063 if(default_event_handler(button) == SYS_USB_CONNECTED)
1064 return GO_TO_ROOT;
1065 update = true;
1066 break;
1069 if (wps_state.do_full_update || update)
1071 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1072 gwps_caption_backlight(&wps_state);
1073 #endif
1074 FOR_NB_SCREENS(i)
1076 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1077 if (lcd_active()
1078 #ifdef HAVE_REMOTE_LCD
1079 /* currently, all remotes are readable without backlight
1080 * so still update those */
1081 || (i == SCREEN_REMOTE)
1082 #endif
1084 #endif
1086 gui_wps_update(&gui_wps[i]);
1089 wps_state.do_full_update = false;
1090 update = false;
1093 if (restore && wps_state.id3 &&
1094 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1095 TIME_AFTER(current_tick, restoretimer)))
1097 restore = false;
1098 restoretimer = RESTORE_WPS_INSTANTLY;
1099 gwps_fix_statusbars();
1100 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1101 lcd_activation_set_hook(wps_lcd_activation_hook);
1102 #endif
1103 FOR_NB_SCREENS(i)
1105 screens[i].stop_scroll();
1106 gui_wps_display(&gui_wps[i]);
1110 if (exit) {
1111 #ifdef HAVE_LCD_CHARCELLS
1112 status_set_record(false);
1113 status_set_audio(false);
1114 #endif
1115 if (global_settings.fade_on_stop)
1116 fade(false, true);
1118 if (bookmark)
1119 bookmark_autobookmark();
1120 audio_stop();
1121 #ifdef AB_REPEAT_ENABLE
1122 ab_reset_markers();
1123 #endif
1124 gwps_leave_wps();
1125 #ifdef HAVE_RECORDING
1126 if (button == ACTION_WPS_REC)
1127 return GO_TO_RECSCREEN;
1128 #endif
1129 if (global_settings.browse_current)
1130 return GO_TO_PREVIOUS_BROWSER;
1131 return GO_TO_PREVIOUS;
1134 if (button && !IS_SYSEVENT(button) )
1135 storage_spin();
1137 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1140 /* this is called from the playback thread so NO DRAWING! */
1141 static void track_changed_callback(void *param)
1143 wps_state.id3 = (struct mp3entry*)param;
1144 wps_state.nid3 = audio_next_track();
1145 if (wps_state.id3->cuesheet)
1147 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1148 cue_spoof_id3(wps_state.id3->cuesheet, wps_state.id3);
1150 wps_state.do_full_update = true;
1152 static void nextid3available_callback(void* param)
1154 (void)param;
1155 wps_state.nid3 = audio_next_track();
1156 wps_state.do_full_update = true;
1160 static void wps_state_init(void)
1162 wps_state.ff_rewind = false;
1163 wps_state.paused = false;
1164 if(audio_status() & AUDIO_STATUS_PLAY)
1166 wps_state.id3 = audio_current_track();
1167 wps_state.nid3 = audio_next_track();
1169 else
1171 wps_state.id3 = NULL;
1172 wps_state.nid3 = NULL;
1174 /* We'll be updating due to restore initialized with true */
1175 wps_state.do_full_update = false;
1176 /* add the WPS track event callbacks */
1177 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1178 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1182 #ifdef HAVE_LCD_BITMAP
1183 static void statusbar_toggle_handler(void *data)
1185 (void)data;
1186 int i;
1187 gwps_fix_statusbars();
1189 FOR_NB_SCREENS(i)
1191 struct viewport *vp = &gui_wps[i].data->viewports[0].vp;
1192 bool draw = wpsbars & (VP_SB_ONSCREEN(i) | VP_SB_IGNORE_SETTING(i));
1193 if (!draw)
1195 vp->y = 0;
1196 vp->height = screens[i].lcdheight;
1198 else
1200 bool bar_at_top = statusbar_position(i) != STATUSBAR_BOTTOM;
1201 vp->y = bar_at_top?STATUSBAR_HEIGHT:0;
1202 vp->height = screens[i].lcdheight - STATUSBAR_HEIGHT;
1206 #endif
1208 void gui_sync_wps_init(void)
1210 int i;
1211 FOR_NB_SCREENS(i)
1213 wps_data_init(&wps_datas[i]);
1214 #ifdef HAVE_ALBUMART
1215 wps_datas[i].wps_uses_albumart = 0;
1216 #endif
1217 #ifdef HAVE_REMOTE_LCD
1218 wps_datas[i].remote_wps = (i != 0);
1219 #endif
1220 gui_wps[i].data = &wps_datas[i];
1221 gui_wps[i].display = &screens[i];
1222 /* Currently no seperate wps_state needed/possible
1223 so use the only available ( "global" ) one */
1224 gui_wps[i].state = &wps_state;
1226 #ifdef HAVE_LCD_BITMAP
1227 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggle_handler);
1228 #endif
1229 #if LCD_DEPTH > 1
1230 unload_wps_backdrop();
1231 #endif
1232 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
1233 unload_remote_wps_backdrop();
1234 #endif
1237 #ifdef HAVE_ALBUMART
1238 /* Returns true if at least one of the gui_wps screens has an album art
1239 tag in its wps structure */
1240 bool gui_sync_wps_uses_albumart(void)
1242 int i;
1243 FOR_NB_SCREENS(i) {
1244 struct gui_wps *gwps = &gui_wps[i];
1245 if (gwps->data && (gwps->data->wps_uses_albumart != WPS_ALBUMART_NONE))
1246 return true;
1248 return false;
1250 #endif