Fix reds for IAP and a missing #include.
[kugel-rb.git] / apps / gui / wps.c
blobd62507e0b0f9baafe85b4f52488119294d94f8c9
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
76 static int wpsbars;
77 /* currently only one wps_state is needed */
78 static struct wps_state wps_state;
79 static 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 void wps_data_load(enum screen_type screen, const char *buf, bool is_file)
95 skin_data_load(gui_wps[screen].data, &screens[screen], buf, is_file);
96 #ifdef HAVE_REMOVE_LCD
97 gui_wps[screen].data->remote_wps = !(screen == SCREEN_MAIN);
98 #endif
101 void wps_data_init(enum screen_type screen)
103 skin_data_init(gui_wps[screen].data);
106 bool wps_fading_out = false;
107 void fade(bool fade_in, bool updatewps)
109 int fp_global_vol = global_settings.volume << 8;
110 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
111 int fp_step = (fp_global_vol - fp_min_vol) / 30;
112 int i;
113 wps_fading_out = !fade_in;
114 if (fade_in) {
115 /* fade in */
116 int fp_volume = fp_min_vol;
118 /* zero out the sound */
119 sound_set_volume(fp_min_vol >> 8);
121 sleep(HZ/10); /* let audio thread run */
122 audio_resume();
124 while (fp_volume < fp_global_vol - fp_step) {
125 fp_volume += fp_step;
126 sound_set_volume(fp_volume >> 8);
127 if (updatewps)
129 FOR_NB_SCREENS(i)
130 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
132 sleep(1);
134 sound_set_volume(global_settings.volume);
136 else {
137 /* fade out */
138 int fp_volume = fp_global_vol;
140 while (fp_volume > fp_min_vol + fp_step) {
141 fp_volume -= fp_step;
142 sound_set_volume(fp_volume >> 8);
143 if (updatewps)
145 FOR_NB_SCREENS(i)
146 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
148 sleep(1);
150 audio_pause();
151 wps_fading_out = false;
152 #if CONFIG_CODEC != SWCODEC
153 #ifndef SIMULATOR
154 /* let audio thread run and wait for the mas to run out of data */
155 while (!mp3_pause_done())
156 #endif
157 sleep(HZ/10);
158 #endif
160 /* reset volume to what it was before the fade */
161 sound_set_volume(global_settings.volume);
164 bool is_wps_fading(void)
166 return wps_fading_out;
169 bool update_onvol_change(struct gui_wps * gwps)
171 skin_update(gwps, WPS_REFRESH_NON_STATIC);
173 #ifdef HAVE_LCD_CHARCELLS
174 splashf(0, "Vol: %3d dB",
175 sound_val2phys(SOUND_VOLUME, global_settings.volume));
176 return true;
177 #endif
178 return false;
182 bool ffwd_rew(int button)
184 unsigned int step = 0; /* current ff/rewind step */
185 unsigned int max_step = 0; /* maximum ff/rewind step */
186 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
187 int direction = -1; /* forward=1 or backward=-1 */
188 bool exit = false;
189 bool usb = false;
190 int i = 0;
191 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
193 if (button == ACTION_NONE)
195 status_set_ffmode(0);
196 return usb;
198 while (!exit)
200 switch ( button )
202 case ACTION_WPS_SEEKFWD:
203 direction = 1;
204 case ACTION_WPS_SEEKBACK:
205 if (wps_state.ff_rewind)
207 if (direction == 1)
209 /* fast forwarding, calc max step relative to end */
210 max_step = (wps_state.id3->length -
211 (wps_state.id3->elapsed +
212 ff_rewind_count)) *
213 FF_REWIND_MAX_PERCENT / 100;
215 else
217 /* rewinding, calc max step relative to start */
218 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
219 FF_REWIND_MAX_PERCENT / 100;
222 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
224 if (step > max_step)
225 step = max_step;
227 ff_rewind_count += step * direction;
229 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
230 step += step >> ff_rw_accel;
232 else
234 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
235 wps_state.id3 && wps_state.id3->length )
237 if (!wps_state.paused)
238 #if (CONFIG_CODEC == SWCODEC)
239 audio_pre_ff_rewind();
240 #else
241 audio_pause();
242 #endif
243 #if CONFIG_KEYPAD == PLAYER_PAD
244 FOR_NB_SCREENS(i)
245 gui_wps[i].display->stop_scroll();
246 #endif
247 if (direction > 0)
248 status_set_ffmode(STATUS_FASTFORWARD);
249 else
250 status_set_ffmode(STATUS_FASTBACKWARD);
252 wps_state.ff_rewind = true;
254 step = 1000 * global_settings.ff_rewind_min_step;
256 else
257 break;
260 if (direction > 0) {
261 if ((wps_state.id3->elapsed + ff_rewind_count) >
262 wps_state.id3->length)
263 ff_rewind_count = wps_state.id3->length -
264 wps_state.id3->elapsed;
266 else {
267 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
268 ff_rewind_count = -wps_state.id3->elapsed;
271 /* set the wps state ff_rewind_count so the progess info
272 displays corectly */
273 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
274 ff_rewind_count:-ff_rewind_count;
275 FOR_NB_SCREENS(i)
277 skin_update(&gui_wps[i],
278 WPS_REFRESH_PLAYER_PROGRESS |
279 WPS_REFRESH_DYNAMIC);
282 break;
284 case ACTION_WPS_STOPSEEK:
285 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
286 audio_ff_rewind(wps_state.id3->elapsed);
287 wps_state.ff_rewind_count = 0;
288 wps_state.ff_rewind = false;
289 status_set_ffmode(0);
290 #if (CONFIG_CODEC != SWCODEC)
291 if (!wps_state.paused)
292 audio_resume();
293 #endif
294 #ifdef HAVE_LCD_CHARCELLS
295 FOR_NB_SCREENS(i)
296 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
297 #endif
298 exit = true;
299 break;
301 default:
302 if(default_event_handler(button) == SYS_USB_CONNECTED) {
303 status_set_ffmode(0);
304 usb = true;
305 exit = true;
307 break;
309 if (!exit)
311 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
312 #ifdef HAVE_TOUCHSCREEN
313 if (button == ACTION_TOUCHSCREEN)
314 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
315 #endif
318 return usb;
322 void display_keylock_text(bool locked)
324 int i;
325 FOR_NB_SCREENS(i)
326 gui_wps[i].display->stop_scroll();
328 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
334 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
335 static void gwps_caption_backlight(struct wps_state *state)
337 if (state && state->id3)
339 #ifdef HAVE_BACKLIGHT
340 if (global_settings.caption_backlight)
342 /* turn on backlight n seconds before track ends, and turn it off n
343 seconds into the new track. n == backlight_timeout, or 5s */
344 int n = global_settings.backlight_timeout * 1000;
346 if ( n < 1000 )
347 n = 5000; /* use 5s if backlight is always on or off */
349 if (((state->id3->elapsed < 1000) ||
350 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
351 (state->paused == false))
352 backlight_on();
354 #endif
355 #ifdef HAVE_REMOTE_LCD
356 if (global_settings.remote_caption_backlight)
358 /* turn on remote backlight n seconds before track ends, and turn it
359 off n seconds into the new track. n == remote_backlight_timeout,
360 or 5s */
361 int n = global_settings.remote_backlight_timeout * 1000;
363 if ( n < 1000 )
364 n = 5000; /* use 5s if backlight is always on or off */
366 if (((state->id3->elapsed < 1000) ||
367 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
368 (state->paused == false))
369 remote_backlight_on();
371 #endif
374 #endif
377 static void change_dir(int direction)
379 if (global_settings.prevent_skip)
380 return;
382 if (direction < 0)
383 audio_prev_dir();
384 else if (direction > 0)
385 audio_next_dir();
386 /* prevent the next dir to immediatly start being ffw'd */
387 action_wait_for_release();
390 static void prev_track(unsigned long skip_thresh)
392 if (wps_state.id3->elapsed < skip_thresh)
394 audio_prev();
395 return;
397 else
399 if (wps_state.id3->cuesheet)
401 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
402 return;
405 if (!wps_state.paused)
406 #if (CONFIG_CODEC == SWCODEC)
407 audio_pre_ff_rewind();
408 #else
409 audio_pause();
410 #endif
412 audio_ff_rewind(0);
414 #if (CONFIG_CODEC != SWCODEC)
415 if (!wps_state.paused)
416 audio_resume();
417 #endif
421 static void next_track(void)
423 /* take care of if we're playing a cuesheet */
424 if (wps_state.id3->cuesheet)
426 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
428 /* if the result was false, then we really want
429 to skip to the next track */
430 return;
434 audio_next();
437 static void play_hop(int direction)
439 unsigned long step = ((unsigned long)global_settings.skip_length)*1000;
440 unsigned long elapsed = wps_state.id3->elapsed;
441 unsigned long remaining = wps_state.id3->length - elapsed;
443 if (!global_settings.prevent_skip &&
444 (!step ||
445 (direction > 0 && step >= remaining) ||
446 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
447 { /* Do normal track skipping */
448 if (direction > 0)
449 next_track();
450 else if (direction < 0)
451 prev_track(DEFAULT_SKIP_TRESH);
452 return;
455 if (direction == 1 && step >= remaining)
457 #if CONFIG_CODEC == SWCODEC
458 if(global_settings.beep)
459 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
460 #endif
461 return;
463 else if ((direction == -1 && elapsed < step))
465 elapsed = 0;
467 else
469 elapsed += step * direction;
471 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
473 #if (CONFIG_CODEC == SWCODEC)
474 audio_pre_ff_rewind();
475 #else
476 audio_pause();
477 #endif
479 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
480 #if (CONFIG_CODEC != SWCODEC)
481 if (!wps_state.paused)
482 audio_resume();
483 #endif
486 static void gwps_fix_statusbars(void)
488 #ifdef HAVE_LCD_BITMAP
489 int i;
490 wpsbars = VP_SB_HIDE_ALL;
491 FOR_NB_SCREENS(i)
493 bool draw = false;
494 if (gui_wps[i].data->wps_sb_tag)
495 draw = gui_wps[i].data->show_sb_on_wps;
496 else if (statusbar_position(i) != STATUSBAR_OFF)
497 draw = true;
498 if (draw)
499 wpsbars |= (VP_SB_ONSCREEN(i) | VP_SB_IGNORE_SETTING(i));
501 #else
502 wpsbars = VP_SB_ALLSCREENS;
503 #endif
506 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
508 * If the user is unable to see the wps, because the display is deactivated,
509 * we suppress updates until the wps is activated again (the lcd driver will
510 * call this hook to issue an instant update)
511 * */
512 static void wps_lcd_activation_hook(void)
514 wps_state.do_full_update = true;
515 /* force timeout in wps main loop, so that the update is instantly */
516 queue_post(&button_queue, BUTTON_NONE, 0);
518 #endif
520 static void gwps_leave_wps(void)
522 int i, oldbars = VP_SB_HIDE_ALL;
524 FOR_NB_SCREENS(i)
525 gui_wps[i].display->stop_scroll();
526 if (global_settings.statusbar)
527 oldbars = VP_SB_ALLSCREENS;
529 #if LCD_DEPTH > 1
530 show_main_backdrop();
531 #endif
532 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
533 show_remote_main_backdrop();
534 #endif
535 viewportmanager_set_statusbar(oldbars);
536 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
537 /* Play safe and unregister the hook */
538 lcd_activation_set_hook(NULL);
539 #endif
542 void gwps_draw_statusbars(void)
544 viewportmanager_set_statusbar(wpsbars);
546 #ifdef HAVE_TOUCHSCREEN
547 int wps_get_touchaction(struct wps_data *data)
549 short x,y;
550 short vx, vy;
551 int type = action_get_touchscreen_press(&x, &y);
552 int i;
553 static int last_action = ACTION_NONE;
554 struct touchregion *r;
555 bool repeated = (type == BUTTON_REPEAT);
556 bool released = (type == BUTTON_REL);
557 for (i=0; i<data->touchregion_count; i++)
559 r = &data->touchregion[i];
560 /* make sure this region's viewport is visible */
561 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
562 continue;
563 /* reposition the touch inside the viewport */
564 vx = x - r->wvp->vp.x;
565 vy = y - r->wvp->vp.y;
566 /* check if it's inside this viewport */
567 if (vx >= 0 && vx < r->wvp->vp.x + r->wvp->vp.width &&
568 vy >= 0 && vy < r->wvp->vp.y + r->wvp->vp.height)
570 /* now see if the point is inside this region */
571 if (vx >= r->x && vx < r->x+r->width &&
572 vy >= r->y && vy < r->y+r->height)
574 if ((repeated && r->repeat) ||
575 (released && !r->repeat))
577 last_action = r->action;
578 return r->action;
583 if ((last_action == ACTION_WPS_SEEKBACK || last_action == ACTION_WPS_SEEKFWD))
584 return ACTION_WPS_STOPSEEK;
585 last_action = ACTION_TOUCHSCREEN;
586 return ACTION_TOUCHSCREEN;
588 #endif
589 /* The WPS can be left in two ways:
590 * a) call a function, which draws over the wps. In this case, the wps
591 * will be still active (i.e. the below function didn't return)
592 * b) return with a value evaluated by root_menu.c, in this case the wps
593 * is really left, and root_menu will handle the next screen
595 * In either way, call gwps_leave_wps(), in order to restore the correct
596 * "main screen" backdrops and statusbars
598 long gui_wps_show(void)
600 long button = 0;
601 bool restore = true;
602 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
603 bool exit = false;
604 bool bookmark = false;
605 bool update = false;
606 int i;
607 long last_left = 0, last_right = 0;
609 #ifdef HAVE_LCD_CHARCELLS
610 status_set_audio(true);
611 status_set_param(false);
612 #endif
614 #ifdef AB_REPEAT_ENABLE
615 ab_repeat_init();
616 ab_reset_markers();
617 #endif
618 wps_state_init();
620 while ( 1 )
622 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
624 /* did someone else (i.e power thread) change audio pause mode? */
625 if (wps_state.paused != audio_paused) {
626 wps_state.paused = audio_paused;
628 /* if another thread paused audio, we are probably in car mode,
629 about to shut down. lets save the settings. */
630 if (wps_state.paused) {
631 settings_save();
632 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
633 call_storage_idle_notifys(true);
634 #endif
638 #ifdef HAVE_LCD_BITMAP
639 /* when the peak meter is enabled we want to have a
640 few extra updates to make it look smooth. On the
641 other hand we don't want to waste energy if it
642 isn't displayed */
643 bool pm=false;
644 FOR_NB_SCREENS(i)
646 if(gui_wps[i].data->peak_meter_enabled)
647 pm = true;
650 if (pm) {
651 long next_refresh = current_tick;
652 long next_big_refresh = current_tick + HZ / 5;
653 button = BUTTON_NONE;
654 while (TIME_BEFORE(current_tick, next_big_refresh)) {
655 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_NOBLOCK);
656 if (button != ACTION_NONE) {
657 break;
659 peak_meter_peek();
660 sleep(0); /* Sleep until end of current tick. */
662 if (TIME_AFTER(current_tick, next_refresh)) {
663 FOR_NB_SCREENS(i)
665 if(gui_wps[i].data->peak_meter_enabled)
666 skin_update(&gui_wps[i], WPS_REFRESH_PEAK_METER);
667 next_refresh += HZ / PEAK_METER_FPS;
674 /* The peak meter is disabled
675 -> no additional screen updates needed */
676 else
677 #endif
679 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,
680 restore ? HZ/100 : HZ/5);
683 /* Exit if audio has stopped playing. This happens e.g. at end of
684 playlist or if using the sleep timer. */
685 if (!(audio_status() & AUDIO_STATUS_PLAY))
686 exit = true;
687 #ifdef HAVE_TOUCHSCREEN
688 if (button == ACTION_TOUCHSCREEN)
689 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
690 #endif
691 /* The iPods/X5/M5 use a single button for the A-B mode markers,
692 defined as ACTION_WPSAB_SINGLE in their config files. */
693 #ifdef ACTION_WPSAB_SINGLE
694 if (!global_settings.party_mode && ab_repeat_mode_enabled())
696 static int wps_ab_state = 0;
697 if (button == ACTION_WPSAB_SINGLE)
699 switch (wps_ab_state)
701 case 0: /* set the A spot */
702 button = ACTION_WPS_ABSETA_PREVDIR;
703 break;
704 case 1: /* set the B spot */
705 button = ACTION_WPS_ABSETB_NEXTDIR;
706 break;
707 case 2:
708 button = ACTION_WPS_ABRESET;
709 break;
711 wps_ab_state = (wps_ab_state+1) % 3;
714 #endif
715 switch(button)
717 case ACTION_WPS_CONTEXT:
719 gwps_leave_wps();
720 /* if music is stopped in the context menu we want to exit the wps */
721 if (onplay(wps_state.id3->path,
722 FILE_ATTR_AUDIO, CONTEXT_WPS) == ONPLAY_MAINMENU
723 || !audio_status())
724 return GO_TO_ROOT;
725 restore = true;
727 break;
729 case ACTION_WPS_BROWSE:
730 #ifdef HAVE_LCD_CHARCELLS
731 status_set_record(false);
732 status_set_audio(false);
733 #endif
734 gwps_leave_wps();
735 return GO_TO_PREVIOUS_BROWSER;
736 break;
738 /* play/pause */
739 case ACTION_WPS_PLAY:
740 if (global_settings.party_mode)
741 break;
742 if ( wps_state.paused )
744 wps_state.paused = false;
745 if ( global_settings.fade_on_stop )
746 fade(true, true);
747 else
748 audio_resume();
750 else
752 wps_state.paused = true;
753 if ( global_settings.fade_on_stop )
754 fade(false, true);
755 else
756 audio_pause();
757 settings_save();
758 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
759 call_storage_idle_notifys(true); /* make sure resume info is saved */
760 #endif
762 break;
764 case ACTION_WPS_VOLUP:
766 FOR_NB_SCREENS(i)
767 gui_wps[i].data->button_time_volume = current_tick;
768 global_settings.volume++;
769 bool res = false;
770 setvol();
771 FOR_NB_SCREENS(i)
773 if(update_onvol_change(&gui_wps[i]))
774 res = true;
776 if (res) {
777 restore = true;
778 restoretimer = RESTORE_WPS_NEXT_SECOND;
781 break;
782 case ACTION_WPS_VOLDOWN:
784 FOR_NB_SCREENS(i)
785 gui_wps[i].data->button_time_volume = current_tick;
786 global_settings.volume--;
787 setvol();
788 bool res = false;
789 FOR_NB_SCREENS(i)
791 if(update_onvol_change(&gui_wps[i]))
792 res = true;
794 if (res) {
795 restore = true;
796 restoretimer = RESTORE_WPS_NEXT_SECOND;
799 break;
800 /* fast forward
801 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
802 case ACTION_WPS_SEEKFWD:
803 if (global_settings.party_mode)
804 break;
805 if (current_tick -last_right < HZ)
807 if (wps_state.id3->cuesheet)
809 audio_next();
811 else
813 change_dir(1);
816 else
817 ffwd_rew(ACTION_WPS_SEEKFWD);
818 last_right = last_left = 0;
819 break;
820 /* fast rewind
821 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
822 case ACTION_WPS_SEEKBACK:
823 if (global_settings.party_mode)
824 break;
825 if (current_tick -last_left < HZ)
827 if (wps_state.id3->cuesheet)
829 if (!wps_state.paused)
830 #if (CONFIG_CODEC == SWCODEC)
831 audio_pre_ff_rewind();
832 #else
833 audio_pause();
834 #endif
835 audio_ff_rewind(0);
837 else
839 change_dir(-1);
842 else
843 ffwd_rew(ACTION_WPS_SEEKBACK);
844 last_left = last_right = 0;
845 break;
847 /* prev / restart */
848 case ACTION_WPS_SKIPPREV:
849 if (global_settings.party_mode)
850 break;
851 last_left = current_tick;
852 #ifdef AB_REPEAT_ENABLE
853 /* if we're in A/B repeat mode and the current position
854 is past the A marker, jump back to the A marker... */
855 if ( ab_repeat_mode_enabled() )
857 if ( ab_after_A_marker(wps_state.id3->elapsed) )
859 ab_jump_to_A_marker();
860 break;
861 #if (AB_REPEAT_ENABLE == 2)
862 } else {
863 ab_reset_markers();
864 #endif
867 else
868 /* ...otherwise, do it normally */
869 #endif
870 play_hop(-1);
871 break;
873 /* next
874 OR if skip length set, hop by predetermined amount. */
875 case ACTION_WPS_SKIPNEXT:
876 if (global_settings.party_mode)
877 break;
878 last_right = current_tick;
879 #ifdef AB_REPEAT_ENABLE
880 /* if we're in A/B repeat mode and the current position is
881 before the A marker, jump to the A marker... */
882 if ( ab_repeat_mode_enabled() )
884 if ( ab_before_A_marker(wps_state.id3->elapsed) )
886 ab_jump_to_A_marker();
887 break;
888 #if (AB_REPEAT_ENABLE == 2)
889 } else {
890 ab_reset_markers();
891 #endif
894 else
895 /* ...otherwise, do it normally */
896 #endif
897 play_hop(1);
898 break;
899 /* next / prev directories */
900 /* and set A-B markers if in a-b mode */
901 case ACTION_WPS_ABSETB_NEXTDIR:
902 if (global_settings.party_mode)
903 break;
904 #if defined(AB_REPEAT_ENABLE)
905 if (ab_repeat_mode_enabled())
907 ab_set_B_marker(wps_state.id3->elapsed);
908 ab_jump_to_A_marker();
910 else
911 #endif
913 change_dir(1);
915 break;
916 case ACTION_WPS_ABSETA_PREVDIR:
917 if (global_settings.party_mode)
918 break;
919 #if defined(AB_REPEAT_ENABLE)
920 if (ab_repeat_mode_enabled())
921 ab_set_A_marker(wps_state.id3->elapsed);
922 else
923 #endif
925 change_dir(-1);
927 break;
928 /* menu key functions */
929 case ACTION_WPS_MENU:
930 gwps_leave_wps();
931 return GO_TO_ROOT;
932 break;
935 #ifdef HAVE_QUICKSCREEN
936 case ACTION_WPS_QUICKSCREEN:
938 gwps_leave_wps();
939 if (quick_screen_quick(button))
940 return SYS_USB_CONNECTED;
941 restore = true;
943 break;
944 #endif /* HAVE_QUICKSCREEN */
946 /* screen settings */
947 #ifdef BUTTON_F3
948 case ACTION_F3:
950 gwps_leave_wps();
951 if (quick_screen_f3(BUTTON_F3))
952 return SYS_USB_CONNECTED;
953 restore = true;
955 break;
956 #endif /* BUTTON_F3 */
958 /* pitch screen */
959 #ifdef HAVE_PITCHSCREEN
960 case ACTION_WPS_PITCHSCREEN:
962 gwps_leave_wps();
963 if (1 == gui_syncpitchscreen_run())
964 return SYS_USB_CONNECTED;
965 restore = true;
967 break;
968 #endif /* HAVE_PITCHSCREEN */
970 #ifdef AB_REPEAT_ENABLE
971 /* reset A&B markers */
972 case ACTION_WPS_ABRESET:
973 if (ab_repeat_mode_enabled())
975 ab_reset_markers();
976 update = true;
978 break;
979 #endif /* AB_REPEAT_ENABLE */
981 /* stop and exit wps */
982 case ACTION_WPS_STOP:
983 if (global_settings.party_mode)
984 break;
985 bookmark = true;
986 exit = true;
987 break;
989 case ACTION_WPS_ID3SCREEN:
991 gwps_leave_wps();
992 browse_id3();
993 restore = true;
995 break;
996 #ifdef HAVE_TOUCHSCREEN
997 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
999 global_settings.playlist_shuffle =
1000 !global_settings.playlist_shuffle;
1001 #if CONFIG_CODEC == SWCODEC
1002 dsp_set_replaygain();
1003 #endif
1004 if (global_settings.playlist_shuffle)
1005 playlist_randomise(NULL, current_tick, true);
1006 else
1007 playlist_sort(NULL, true);
1009 break;
1010 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1012 const struct settings_list *rep_setting =
1013 find_setting(&global_settings.repeat_mode, NULL);
1014 option_select_next_val(rep_setting, false, true);
1015 audio_flush_and_reload_tracks();
1017 break;
1018 #endif /* HAVE_TOUCHSCREEN */
1019 /* this case is used by the softlock feature
1020 * it requests a full update here */
1021 case ACTION_REDRAW:
1022 wps_state.do_full_update = true;
1023 break;
1024 case ACTION_NONE: /* Timeout, do a partial update */
1025 update = true;
1026 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1027 break;
1028 #ifdef HAVE_RECORDING
1029 case ACTION_WPS_REC:
1030 exit = true;
1031 break;
1032 #endif
1033 case SYS_POWEROFF:
1034 default_event_handler(SYS_POWEROFF);
1035 break;
1036 case ACTION_WPS_VIEW_PLAYLIST:
1037 gwps_leave_wps();
1038 if (playlist_viewer()) /* true if USB connected */
1039 return SYS_USB_CONNECTED;
1040 restore = true;
1041 break;
1042 default:
1043 if(default_event_handler(button) == SYS_USB_CONNECTED)
1044 return GO_TO_ROOT;
1045 update = true;
1046 break;
1049 if (wps_state.do_full_update || update)
1051 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1052 gwps_caption_backlight(&wps_state);
1053 #endif
1054 FOR_NB_SCREENS(i)
1056 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1057 if (lcd_active()
1058 #ifdef HAVE_REMOTE_LCD
1059 /* currently, all remotes are readable without backlight
1060 * so still update those */
1061 || (i == SCREEN_REMOTE)
1062 #endif
1064 #endif
1066 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
1069 wps_state.do_full_update = false;
1070 update = false;
1073 if (restore && wps_state.id3 &&
1074 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1075 TIME_AFTER(current_tick, restoretimer)))
1077 restore = false;
1078 restoretimer = RESTORE_WPS_INSTANTLY;
1079 gwps_fix_statusbars();
1080 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1081 lcd_activation_set_hook(wps_lcd_activation_hook);
1082 #endif
1083 FOR_NB_SCREENS(i)
1085 screens[i].stop_scroll();
1086 gui_wps_display(&gui_wps[i]);
1090 if (exit) {
1091 #ifdef HAVE_LCD_CHARCELLS
1092 status_set_record(false);
1093 status_set_audio(false);
1094 #endif
1095 if (global_settings.fade_on_stop)
1096 fade(false, true);
1098 if (bookmark)
1099 bookmark_autobookmark();
1100 audio_stop();
1101 #ifdef AB_REPEAT_ENABLE
1102 ab_reset_markers();
1103 #endif
1104 gwps_leave_wps();
1105 #ifdef HAVE_RECORDING
1106 if (button == ACTION_WPS_REC)
1107 return GO_TO_RECSCREEN;
1108 #endif
1109 if (global_settings.browse_current)
1110 return GO_TO_PREVIOUS_BROWSER;
1111 return GO_TO_PREVIOUS;
1114 if (button && !IS_SYSEVENT(button) )
1115 storage_spin();
1117 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1120 /* this is called from the playback thread so NO DRAWING! */
1121 static void track_changed_callback(void *param)
1123 wps_state.id3 = (struct mp3entry*)param;
1124 wps_state.nid3 = audio_next_track();
1125 if (wps_state.id3->cuesheet)
1127 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1128 cue_spoof_id3(wps_state.id3->cuesheet, wps_state.id3);
1130 wps_state.do_full_update = true;
1132 static void nextid3available_callback(void* param)
1134 (void)param;
1135 wps_state.nid3 = audio_next_track();
1136 wps_state.do_full_update = true;
1140 static void wps_state_init(void)
1142 wps_state.ff_rewind = false;
1143 wps_state.paused = false;
1144 if(audio_status() & AUDIO_STATUS_PLAY)
1146 wps_state.id3 = audio_current_track();
1147 wps_state.nid3 = audio_next_track();
1149 else
1151 wps_state.id3 = NULL;
1152 wps_state.nid3 = NULL;
1154 /* We'll be updating due to restore initialized with true */
1155 wps_state.do_full_update = false;
1156 /* add the WPS track event callbacks */
1157 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1158 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1162 #ifdef HAVE_LCD_BITMAP
1163 static void statusbar_toggle_handler(void *data)
1165 (void)data;
1166 int i;
1167 gwps_fix_statusbars();
1169 FOR_NB_SCREENS(i)
1171 struct viewport *vp = &gui_wps[i].data->viewports[0].vp;
1172 bool draw = wpsbars & (VP_SB_ONSCREEN(i) | VP_SB_IGNORE_SETTING(i));
1173 if (!draw)
1175 vp->y = 0;
1176 vp->height = screens[i].lcdheight;
1178 else
1180 bool bar_at_top = statusbar_position(i) != STATUSBAR_BOTTOM;
1181 vp->y = bar_at_top?STATUSBAR_HEIGHT:0;
1182 vp->height = screens[i].lcdheight - STATUSBAR_HEIGHT;
1186 #endif
1188 void gui_sync_wps_init(void)
1190 int i;
1191 FOR_NB_SCREENS(i)
1193 skin_data_init(&wps_datas[i]);
1194 #ifdef HAVE_ALBUMART
1195 wps_datas[i].wps_uses_albumart = 0;
1196 #endif
1197 #ifdef HAVE_REMOTE_LCD
1198 wps_datas[i].remote_wps = (i != 0);
1199 #endif
1200 gui_wps[i].data = &wps_datas[i];
1201 gui_wps[i].display = &screens[i];
1202 /* Currently no seperate wps_state needed/possible
1203 so use the only available ( "global" ) one */
1204 gui_wps[i].state = &wps_state;
1206 #ifdef HAVE_LCD_BITMAP
1207 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggle_handler);
1208 #endif
1209 #if LCD_DEPTH > 1
1210 unload_wps_backdrop();
1211 #endif
1212 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
1213 unload_remote_wps_backdrop();
1214 #endif
1217 #ifdef HAVE_ALBUMART
1218 bool wps_uses_albumart(int *width, int *height)
1220 int i;
1221 FOR_NB_SCREENS(i) {
1222 struct gui_wps *gwps = &gui_wps[i];
1223 if (gwps->data && (gwps->data->wps_uses_albumart != WPS_ALBUMART_NONE))
1225 if (width)
1226 *width = gui_wps[0].data->albumart_max_width;
1227 if (height)
1228 *height = gui_wps[0].data->albumart_max_height;
1229 return true;
1232 return false;
1234 #endif
1237 #ifdef IPOD_ACCESSORY_PROTOCOL
1238 int wps_get_ff_rewind_count(void)
1240 return wps_state.ff_rewind_count;
1242 #endif