User definable UI viewport, to be able to restrict the UI into a viewport for all...
[kugel-rb/myfork.git] / apps / gui / wps.c
blob055f8d01e68dcc34950c4b1a30ef4cdbbfdb1f31
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Jerome Kuptz
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include "config.h"
26 #include "system.h"
27 #include "file.h"
28 #include "lcd.h"
29 #include "font.h"
30 #include "backlight.h"
31 #include "action.h"
32 #include "kernel.h"
33 #include "filetypes.h"
34 #include "debug.h"
35 #include "sprintf.h"
36 #include "settings.h"
37 #include "skin_engine/skin_engine.h"
38 #include "mp3_playback.h"
39 #include "audio.h"
40 #include "usb.h"
41 #include "status.h"
42 #include "storage.h"
43 #include "screens.h"
44 #include "playlist.h"
45 #ifdef HAVE_LCD_BITMAP
46 #include "icons.h"
47 #include "peakmeter.h"
48 #endif
49 #include "lang.h"
50 #include "bookmark.h"
51 #include "misc.h"
52 #include "sound.h"
53 #include "onplay.h"
54 #include "abrepeat.h"
55 #include "playback.h"
56 #include "splash.h"
57 #include "cuesheet.h"
58 #include "ata_idle_notify.h"
59 #include "root_menu.h"
60 #include "backdrop.h"
61 #include "quickscreen.h"
62 #include "pitchscreen.h"
63 #include "appevents.h"
64 #include "viewport.h"
65 #include "pcmbuf.h"
66 #include "option_select.h"
67 #include "dsp.h"
68 #include "playlist_viewer.h"
69 #include "wps.h"
71 #define RESTORE_WPS_INSTANTLY 0l
72 #define RESTORE_WPS_NEXT_SECOND ((long)(HZ+current_tick))
73 /* in milliseconds */
74 #define DEFAULT_SKIP_TRESH 3000ul
77 #define FF_REWIND_MAX_PERCENT 3 /* cap ff/rewind step size at max % of file */
78 /* 3% of 30min file == 54s step size */
79 #define MIN_FF_REWIND_STEP 500
81 static int wpsbars;
82 /* currently only one wps_state is needed */
83 static struct wps_state wps_state;
84 static struct gui_wps gui_wps[NB_SCREENS];
85 static struct wps_data wps_datas[NB_SCREENS];
87 /* initial setup of wps_data */
88 static void wps_state_init(void);
89 static void track_changed_callback(void *param);
90 static void nextid3available_callback(void* param);
93 void wps_data_load(enum screen_type screen, const char *buf, bool isfile)
95 bool loaded_ok;
97 screens[screen].backdrop_unload(BACKDROP_SKIN_WPS);
99 loaded_ok = buf && skin_data_load(gui_wps[screen].data,
100 &screens[screen], buf, isfile);
101 if (!loaded_ok) /* load the hardcoded default */
103 char *skin_buf[NB_SCREENS] = {
104 #ifdef HAVE_LCD_BITMAP
105 "%s%?it<%?in<%in. |>%it|%fn>\n"
106 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
107 "%s%?id<%id|%?d1<%d1|(root)>> %?iy<(%iy)|>\n\n"
108 "%al%pc/%pt%ar[%pp:%pe]\n"
109 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
110 "%pb\n%pm\n",
111 #else
112 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d2> - %?id<%id|%d1>\n"
113 "%pc%?ps<*|/>%pt\n",
114 #endif
115 #ifdef HAVE_REMOTE_LCD
116 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
117 "%s%?it<%?in<%in. |>%it|%fn>\n"
118 "%al%pc/%pt%ar[%pp:%pe]\n"
119 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
120 "%pb\n",
121 #endif
123 skin_data_load(gui_wps[screen].data, &screens[screen],
124 skin_buf[screen], false);
126 #ifdef HAVE_REMOVE_LCD
127 gui_wps[screen].data->remote_wps = !(screen == SCREEN_MAIN);
128 #endif
131 void wps_data_init(enum screen_type screen)
133 skin_data_init(gui_wps[screen].data);
137 static bool wps_fading_out = false;
138 void fade(bool fade_in, bool updatewps)
140 int fp_global_vol = global_settings.volume << 8;
141 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
142 int fp_step = (fp_global_vol - fp_min_vol) / 30;
143 int i;
144 wps_fading_out = !fade_in;
145 if (fade_in) {
146 /* fade in */
147 int fp_volume = fp_min_vol;
149 /* zero out the sound */
150 sound_set_volume(fp_min_vol >> 8);
152 sleep(HZ/10); /* let audio thread run */
153 audio_resume();
155 while (fp_volume < fp_global_vol - fp_step) {
156 fp_volume += fp_step;
157 sound_set_volume(fp_volume >> 8);
158 if (updatewps)
160 FOR_NB_SCREENS(i)
161 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
163 sleep(1);
165 sound_set_volume(global_settings.volume);
167 else {
168 /* fade out */
169 int fp_volume = fp_global_vol;
171 while (fp_volume > fp_min_vol + fp_step) {
172 fp_volume -= fp_step;
173 sound_set_volume(fp_volume >> 8);
174 if (updatewps)
176 FOR_NB_SCREENS(i)
177 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
179 sleep(1);
181 audio_pause();
182 wps_fading_out = false;
183 #if CONFIG_CODEC != SWCODEC
184 #ifndef SIMULATOR
185 /* let audio thread run and wait for the mas to run out of data */
186 while (!mp3_pause_done())
187 #endif
188 sleep(HZ/10);
189 #endif
191 /* reset volume to what it was before the fade */
192 sound_set_volume(global_settings.volume);
195 bool is_wps_fading(void)
197 return wps_fading_out;
200 static bool update_onvol_change(struct gui_wps * gwps)
202 skin_update(gwps, WPS_REFRESH_NON_STATIC);
204 #ifdef HAVE_LCD_CHARCELLS
205 splashf(0, "Vol: %3d dB",
206 sound_val2phys(SOUND_VOLUME, global_settings.volume));
207 return true;
208 #endif
209 return false;
213 bool ffwd_rew(int button)
215 unsigned int step = 0; /* current ff/rewind step */
216 unsigned int max_step = 0; /* maximum ff/rewind step */
217 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
218 int direction = -1; /* forward=1 or backward=-1 */
219 bool exit = false;
220 bool usb = false;
221 int i = 0;
222 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
224 if (button == ACTION_NONE)
226 status_set_ffmode(0);
227 return usb;
229 while (!exit)
231 switch ( button )
233 case ACTION_WPS_SEEKFWD:
234 direction = 1;
235 case ACTION_WPS_SEEKBACK:
236 if (wps_state.ff_rewind)
238 if (direction == 1)
240 /* fast forwarding, calc max step relative to end */
241 max_step = (wps_state.id3->length -
242 (wps_state.id3->elapsed +
243 ff_rewind_count)) *
244 FF_REWIND_MAX_PERCENT / 100;
246 else
248 /* rewinding, calc max step relative to start */
249 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
250 FF_REWIND_MAX_PERCENT / 100;
253 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
255 if (step > max_step)
256 step = max_step;
258 ff_rewind_count += step * direction;
260 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
261 step += step >> ff_rw_accel;
263 else
265 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
266 wps_state.id3 && wps_state.id3->length )
268 if (!wps_state.paused)
269 #if (CONFIG_CODEC == SWCODEC)
270 audio_pre_ff_rewind();
271 #else
272 audio_pause();
273 #endif
274 #if CONFIG_KEYPAD == PLAYER_PAD
275 FOR_NB_SCREENS(i)
276 gui_wps[i].display->stop_scroll();
277 #endif
278 if (direction > 0)
279 status_set_ffmode(STATUS_FASTFORWARD);
280 else
281 status_set_ffmode(STATUS_FASTBACKWARD);
283 wps_state.ff_rewind = true;
285 step = 1000 * global_settings.ff_rewind_min_step;
287 else
288 break;
291 if (direction > 0) {
292 if ((wps_state.id3->elapsed + ff_rewind_count) >
293 wps_state.id3->length)
294 ff_rewind_count = wps_state.id3->length -
295 wps_state.id3->elapsed;
297 else {
298 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
299 ff_rewind_count = -wps_state.id3->elapsed;
302 /* set the wps state ff_rewind_count so the progess info
303 displays corectly */
304 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
305 ff_rewind_count:-ff_rewind_count;
306 FOR_NB_SCREENS(i)
308 skin_update(&gui_wps[i],
309 WPS_REFRESH_PLAYER_PROGRESS |
310 WPS_REFRESH_DYNAMIC);
313 break;
315 case ACTION_WPS_STOPSEEK:
316 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
317 audio_ff_rewind(wps_state.id3->elapsed);
318 wps_state.ff_rewind_count = 0;
319 wps_state.ff_rewind = false;
320 status_set_ffmode(0);
321 #if (CONFIG_CODEC != SWCODEC)
322 if (!wps_state.paused)
323 audio_resume();
324 #endif
325 #ifdef HAVE_LCD_CHARCELLS
326 FOR_NB_SCREENS(i)
327 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
328 #endif
329 exit = true;
330 break;
332 default:
333 if(default_event_handler(button) == SYS_USB_CONNECTED) {
334 status_set_ffmode(0);
335 usb = true;
336 exit = true;
338 break;
340 if (!exit)
342 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
343 #ifdef HAVE_TOUCHSCREEN
344 if (button == ACTION_TOUCHSCREEN)
345 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
346 #endif
349 return usb;
353 void display_keylock_text(bool locked)
355 int i;
356 FOR_NB_SCREENS(i)
357 gui_wps[i].display->stop_scroll();
359 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
365 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
366 static void gwps_caption_backlight(struct wps_state *state)
368 if (state && state->id3)
370 #ifdef HAVE_BACKLIGHT
371 if (global_settings.caption_backlight)
373 /* turn on backlight n seconds before track ends, and turn it off n
374 seconds into the new track. n == backlight_timeout, or 5s */
375 int n = global_settings.backlight_timeout * 1000;
377 if ( n < 1000 )
378 n = 5000; /* use 5s if backlight is always on or off */
380 if (((state->id3->elapsed < 1000) ||
381 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
382 (state->paused == false))
383 backlight_on();
385 #endif
386 #ifdef HAVE_REMOTE_LCD
387 if (global_settings.remote_caption_backlight)
389 /* turn on remote backlight n seconds before track ends, and turn it
390 off n seconds into the new track. n == remote_backlight_timeout,
391 or 5s */
392 int n = global_settings.remote_backlight_timeout * 1000;
394 if ( n < 1000 )
395 n = 5000; /* use 5s if backlight is always on or off */
397 if (((state->id3->elapsed < 1000) ||
398 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
399 (state->paused == false))
400 remote_backlight_on();
402 #endif
405 #endif
408 static void change_dir(int direction)
410 if (global_settings.prevent_skip)
411 return;
413 if (direction < 0)
414 audio_prev_dir();
415 else if (direction > 0)
416 audio_next_dir();
417 /* prevent the next dir to immediatly start being ffw'd */
418 action_wait_for_release();
421 static void prev_track(unsigned long skip_thresh)
423 if (wps_state.id3->elapsed < skip_thresh)
425 audio_prev();
426 return;
428 else
430 if (wps_state.id3->cuesheet)
432 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
433 return;
436 if (!wps_state.paused)
437 #if (CONFIG_CODEC == SWCODEC)
438 audio_pre_ff_rewind();
439 #else
440 audio_pause();
441 #endif
443 audio_ff_rewind(0);
445 #if (CONFIG_CODEC != SWCODEC)
446 if (!wps_state.paused)
447 audio_resume();
448 #endif
452 static void next_track(void)
454 /* take care of if we're playing a cuesheet */
455 if (wps_state.id3->cuesheet)
457 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
459 /* if the result was false, then we really want
460 to skip to the next track */
461 return;
465 audio_next();
468 static void play_hop(int direction)
470 unsigned long step = ((unsigned long)global_settings.skip_length)*1000;
471 unsigned long elapsed = wps_state.id3->elapsed;
472 unsigned long remaining = wps_state.id3->length - elapsed;
474 if (!global_settings.prevent_skip &&
475 (!step ||
476 (direction > 0 && step >= remaining) ||
477 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
478 { /* Do normal track skipping */
479 if (direction > 0)
480 next_track();
481 else if (direction < 0)
482 prev_track(DEFAULT_SKIP_TRESH);
483 return;
486 if (direction == 1 && step >= remaining)
488 #if CONFIG_CODEC == SWCODEC
489 if(global_settings.beep)
490 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
491 #endif
492 return;
494 else if ((direction == -1 && elapsed < step))
496 elapsed = 0;
498 else
500 elapsed += step * direction;
502 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
504 #if (CONFIG_CODEC == SWCODEC)
505 audio_pre_ff_rewind();
506 #else
507 audio_pause();
508 #endif
510 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
511 #if (CONFIG_CODEC != SWCODEC)
512 if (!wps_state.paused)
513 audio_resume();
514 #endif
517 static void gwps_fix_statusbars(void)
519 #ifdef HAVE_LCD_BITMAP
520 int i;
521 wpsbars = VP_SB_HIDE_ALL;
522 FOR_NB_SCREENS(i)
524 bool draw = false;
525 if (gui_wps[i].data->wps_sb_tag)
526 draw = gui_wps[i].data->show_sb_on_wps;
527 else if (statusbar_position(i) != STATUSBAR_OFF)
528 draw = true;
529 if (draw)
530 wpsbars |= (VP_SB_ONSCREEN(i) | VP_SB_IGNORE_SETTING(i));
532 #else
533 wpsbars = VP_SB_ALLSCREENS;
534 #endif
537 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
539 * If the user is unable to see the wps, because the display is deactivated,
540 * we suppress updates until the wps is activated again (the lcd driver will
541 * call this hook to issue an instant update)
542 * */
543 static void wps_lcd_activation_hook(void)
545 wps_state.do_full_update = true;
546 /* force timeout in wps main loop, so that the update is instantly */
547 queue_post(&button_queue, BUTTON_NONE, 0);
549 #endif
551 static void gwps_leave_wps(void)
553 int i, oldbars = VP_SB_HIDE_ALL;
555 FOR_NB_SCREENS(i)
557 gui_wps[i].display->stop_scroll();
558 gui_wps[i].display->backdrop_show(BACKDROP_MAIN);
560 if (global_settings.statusbar)
561 oldbars = VP_SB_ALLSCREENS;
563 viewportmanager_set_statusbar(oldbars);
564 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
565 /* Play safe and unregister the hook */
566 lcd_activation_set_hook(NULL);
567 #endif
568 send_event(GUI_EVENT_REFRESH, NULL);
571 void gwps_draw_statusbars(void)
573 viewportmanager_set_statusbar(wpsbars);
575 #ifdef HAVE_TOUCHSCREEN
576 int wps_get_touchaction(struct wps_data *data)
578 short x,y;
579 short vx, vy;
580 int type = action_get_touchscreen_press(&x, &y);
581 static int last_action = ACTION_NONE;
582 struct touchregion *r;
583 bool repeated = (type == BUTTON_REPEAT);
584 bool released = (type == BUTTON_REL);
585 struct skin_token_list *regions = data->touchregions;
586 while (regions)
588 r = (struct touchregion *)regions->token->value.data;
589 /* make sure this region's viewport is visible */
590 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
592 regions = regions->next;
593 continue;
595 /* reposition the touch inside the viewport */
596 vx = x - r->wvp->vp.x;
597 vy = y - r->wvp->vp.y;
598 /* check if it's inside this viewport */
599 if (vx >= 0 && vx < r->wvp->vp.x + r->wvp->vp.width &&
600 vy >= 0 && vy < r->wvp->vp.y + r->wvp->vp.height)
602 /* now see if the point is inside this region */
603 if (vx >= r->x && vx < r->x+r->width &&
604 vy >= r->y && vy < r->y+r->height)
606 /* reposition the touch within the area */
607 vx -= r->x;
608 vy -= r->y;
610 switch(r->type)
612 case WPS_TOUCHREGION_ACTION:
613 if ((repeated && r->repeat) || (released && !r->repeat))
615 last_action = r->action;
616 return r->action;
618 break;
619 case WPS_TOUCHREGION_SCROLLBAR:
620 if(r->width > r->height)
621 /* landscape */
622 wps_state.id3->elapsed = (vx *
623 wps_state.id3->length) / r->width;
624 else
625 /* portrait */
626 wps_state.id3->elapsed = (vy *
627 wps_state.id3->length) / r->height;
629 audio_ff_rewind(wps_state.id3->elapsed);
630 break;
631 case WPS_TOUCHREGION_VOLUME:
633 const int min_vol = sound_min(SOUND_VOLUME);
634 const int max_vol = sound_max(SOUND_VOLUME);
635 if(r->width > r->height)
636 /* landscape */
637 global_settings.volume = (vx *
638 (max_vol - min_vol)) / r->width;
639 else
640 /* portrait */
641 global_settings.volume = (vy *
642 (max_vol-min_vol)) / r->height;
644 global_settings.volume += min_vol;
645 setvol();
646 break;
651 regions = regions->next;
654 if ((last_action == ACTION_WPS_SEEKBACK || last_action == ACTION_WPS_SEEKFWD))
655 return ACTION_WPS_STOPSEEK;
656 last_action = ACTION_TOUCHSCREEN;
657 return ACTION_TOUCHSCREEN;
659 #endif
660 /* The WPS can be left in two ways:
661 * a) call a function, which draws over the wps. In this case, the wps
662 * will be still active (i.e. the below function didn't return)
663 * b) return with a value evaluated by root_menu.c, in this case the wps
664 * is really left, and root_menu will handle the next screen
666 * In either way, call gwps_leave_wps(), in order to restore the correct
667 * "main screen" backdrops and statusbars
669 long gui_wps_show(void)
671 long button = 0;
672 bool restore = true;
673 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
674 bool exit = false;
675 bool bookmark = false;
676 bool update = false;
677 int i;
678 long last_left = 0, last_right = 0;
680 #ifdef HAVE_LCD_CHARCELLS
681 status_set_audio(true);
682 status_set_param(false);
683 #endif
685 #ifdef AB_REPEAT_ENABLE
686 ab_repeat_init();
687 ab_reset_markers();
688 #endif
689 wps_state_init();
691 while ( 1 )
693 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
695 /* did someone else (i.e power thread) change audio pause mode? */
696 if (wps_state.paused != audio_paused) {
697 wps_state.paused = audio_paused;
699 /* if another thread paused audio, we are probably in car mode,
700 about to shut down. lets save the settings. */
701 if (wps_state.paused) {
702 settings_save();
703 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
704 call_storage_idle_notifys(true);
705 #endif
709 #ifdef HAVE_LCD_BITMAP
710 /* when the peak meter is enabled we want to have a
711 few extra updates to make it look smooth. On the
712 other hand we don't want to waste energy if it
713 isn't displayed */
714 bool pm=false;
715 FOR_NB_SCREENS(i)
717 if(gui_wps[i].data->peak_meter_enabled)
718 pm = true;
721 if (pm) {
722 long next_refresh = current_tick;
723 long next_big_refresh = current_tick + HZ / 5;
724 button = BUTTON_NONE;
725 while (TIME_BEFORE(current_tick, next_big_refresh)) {
726 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_NOBLOCK);
727 if (button != ACTION_NONE) {
728 break;
730 peak_meter_peek();
731 sleep(0); /* Sleep until end of current tick. */
733 if (TIME_AFTER(current_tick, next_refresh)) {
734 FOR_NB_SCREENS(i)
736 if(gui_wps[i].data->peak_meter_enabled)
737 skin_update(&gui_wps[i], WPS_REFRESH_PEAK_METER);
738 next_refresh += HZ / PEAK_METER_FPS;
745 /* The peak meter is disabled
746 -> no additional screen updates needed */
747 else
748 #endif
750 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,
751 restore ? HZ/100 : HZ/5);
754 /* Exit if audio has stopped playing. This happens e.g. at end of
755 playlist or if using the sleep timer. */
756 if (!(audio_status() & AUDIO_STATUS_PLAY))
757 exit = true;
758 #ifdef HAVE_TOUCHSCREEN
759 if (button == ACTION_TOUCHSCREEN)
760 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
761 #endif
762 /* The iPods/X5/M5 use a single button for the A-B mode markers,
763 defined as ACTION_WPSAB_SINGLE in their config files. */
764 #ifdef ACTION_WPSAB_SINGLE
765 if (!global_settings.party_mode && ab_repeat_mode_enabled())
767 static int wps_ab_state = 0;
768 if (button == ACTION_WPSAB_SINGLE)
770 switch (wps_ab_state)
772 case 0: /* set the A spot */
773 button = ACTION_WPS_ABSETA_PREVDIR;
774 break;
775 case 1: /* set the B spot */
776 button = ACTION_WPS_ABSETB_NEXTDIR;
777 break;
778 case 2:
779 button = ACTION_WPS_ABRESET;
780 break;
782 wps_ab_state = (wps_ab_state+1) % 3;
785 #endif
786 switch(button)
788 case ACTION_WPS_CONTEXT:
790 gwps_leave_wps();
791 /* if music is stopped in the context menu we want to exit the wps */
792 if (onplay(wps_state.id3->path,
793 FILE_ATTR_AUDIO, CONTEXT_WPS) == ONPLAY_MAINMENU
794 || !audio_status())
795 return GO_TO_ROOT;
796 restore = true;
798 break;
800 case ACTION_WPS_BROWSE:
801 #ifdef HAVE_LCD_CHARCELLS
802 status_set_record(false);
803 status_set_audio(false);
804 #endif
805 gwps_leave_wps();
806 return GO_TO_PREVIOUS_BROWSER;
807 break;
809 /* play/pause */
810 case ACTION_WPS_PLAY:
811 if (global_settings.party_mode)
812 break;
813 if ( wps_state.paused )
815 wps_state.paused = false;
816 if ( global_settings.fade_on_stop )
817 fade(true, true);
818 else
819 audio_resume();
821 else
823 wps_state.paused = true;
824 if ( global_settings.fade_on_stop )
825 fade(false, true);
826 else
827 audio_pause();
828 settings_save();
829 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
830 call_storage_idle_notifys(true); /* make sure resume info is saved */
831 #endif
833 break;
835 case ACTION_WPS_VOLUP:
837 FOR_NB_SCREENS(i)
838 gui_wps[i].data->button_time_volume = current_tick;
839 global_settings.volume++;
840 bool res = false;
841 setvol();
842 FOR_NB_SCREENS(i)
844 if(update_onvol_change(&gui_wps[i]))
845 res = true;
847 if (res) {
848 restore = true;
849 restoretimer = RESTORE_WPS_NEXT_SECOND;
852 break;
853 case ACTION_WPS_VOLDOWN:
855 FOR_NB_SCREENS(i)
856 gui_wps[i].data->button_time_volume = current_tick;
857 global_settings.volume--;
858 setvol();
859 bool res = false;
860 FOR_NB_SCREENS(i)
862 if(update_onvol_change(&gui_wps[i]))
863 res = true;
865 if (res) {
866 restore = true;
867 restoretimer = RESTORE_WPS_NEXT_SECOND;
870 break;
871 /* fast forward
872 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
873 case ACTION_WPS_SEEKFWD:
874 if (global_settings.party_mode)
875 break;
876 if (current_tick -last_right < HZ)
878 if (wps_state.id3->cuesheet)
880 audio_next();
882 else
884 change_dir(1);
887 else
888 ffwd_rew(ACTION_WPS_SEEKFWD);
889 last_right = last_left = 0;
890 break;
891 /* fast rewind
892 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
893 case ACTION_WPS_SEEKBACK:
894 if (global_settings.party_mode)
895 break;
896 if (current_tick -last_left < HZ)
898 if (wps_state.id3->cuesheet)
900 if (!wps_state.paused)
901 #if (CONFIG_CODEC == SWCODEC)
902 audio_pre_ff_rewind();
903 #else
904 audio_pause();
905 #endif
906 audio_ff_rewind(0);
908 else
910 change_dir(-1);
913 else
914 ffwd_rew(ACTION_WPS_SEEKBACK);
915 last_left = last_right = 0;
916 break;
918 /* prev / restart */
919 case ACTION_WPS_SKIPPREV:
920 if (global_settings.party_mode)
921 break;
922 last_left = current_tick;
923 #ifdef AB_REPEAT_ENABLE
924 /* if we're in A/B repeat mode and the current position
925 is past the A marker, jump back to the A marker... */
926 if ( ab_repeat_mode_enabled() )
928 if ( ab_after_A_marker(wps_state.id3->elapsed) )
930 ab_jump_to_A_marker();
931 break;
932 #if (AB_REPEAT_ENABLE == 2)
933 } else {
934 ab_reset_markers();
935 #endif
938 else
939 /* ...otherwise, do it normally */
940 #endif
941 play_hop(-1);
942 break;
944 /* next
945 OR if skip length set, hop by predetermined amount. */
946 case ACTION_WPS_SKIPNEXT:
947 if (global_settings.party_mode)
948 break;
949 last_right = current_tick;
950 #ifdef AB_REPEAT_ENABLE
951 /* if we're in A/B repeat mode and the current position is
952 before the A marker, jump to the A marker... */
953 if ( ab_repeat_mode_enabled() )
955 if ( ab_before_A_marker(wps_state.id3->elapsed) )
957 ab_jump_to_A_marker();
958 break;
959 #if (AB_REPEAT_ENABLE == 2)
960 } else {
961 ab_reset_markers();
962 #endif
965 else
966 /* ...otherwise, do it normally */
967 #endif
968 play_hop(1);
969 break;
970 /* next / prev directories */
971 /* and set A-B markers if in a-b mode */
972 case ACTION_WPS_ABSETB_NEXTDIR:
973 if (global_settings.party_mode)
974 break;
975 #if defined(AB_REPEAT_ENABLE)
976 if (ab_repeat_mode_enabled())
978 ab_set_B_marker(wps_state.id3->elapsed);
979 ab_jump_to_A_marker();
981 else
982 #endif
984 change_dir(1);
986 break;
987 case ACTION_WPS_ABSETA_PREVDIR:
988 if (global_settings.party_mode)
989 break;
990 #if defined(AB_REPEAT_ENABLE)
991 if (ab_repeat_mode_enabled())
992 ab_set_A_marker(wps_state.id3->elapsed);
993 else
994 #endif
996 change_dir(-1);
998 break;
999 /* menu key functions */
1000 case ACTION_WPS_MENU:
1001 gwps_leave_wps();
1002 return GO_TO_ROOT;
1003 break;
1006 #ifdef HAVE_QUICKSCREEN
1007 case ACTION_WPS_QUICKSCREEN:
1009 gwps_leave_wps();
1010 if (quick_screen_quick(button))
1011 return SYS_USB_CONNECTED;
1012 restore = true;
1014 break;
1015 #endif /* HAVE_QUICKSCREEN */
1017 /* screen settings */
1018 #ifdef BUTTON_F3
1019 case ACTION_F3:
1021 gwps_leave_wps();
1022 if (quick_screen_f3(BUTTON_F3))
1023 return SYS_USB_CONNECTED;
1024 restore = true;
1026 break;
1027 #endif /* BUTTON_F3 */
1029 /* pitch screen */
1030 #ifdef HAVE_PITCHSCREEN
1031 case ACTION_WPS_PITCHSCREEN:
1033 gwps_leave_wps();
1034 if (1 == gui_syncpitchscreen_run())
1035 return SYS_USB_CONNECTED;
1036 restore = true;
1038 break;
1039 #endif /* HAVE_PITCHSCREEN */
1041 #ifdef AB_REPEAT_ENABLE
1042 /* reset A&B markers */
1043 case ACTION_WPS_ABRESET:
1044 if (ab_repeat_mode_enabled())
1046 ab_reset_markers();
1047 update = true;
1049 break;
1050 #endif /* AB_REPEAT_ENABLE */
1052 /* stop and exit wps */
1053 case ACTION_WPS_STOP:
1054 if (global_settings.party_mode)
1055 break;
1056 bookmark = true;
1057 exit = true;
1058 break;
1060 case ACTION_WPS_ID3SCREEN:
1062 gwps_leave_wps();
1063 browse_id3();
1064 restore = true;
1066 break;
1067 #ifdef HAVE_TOUCHSCREEN
1068 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1070 global_settings.playlist_shuffle =
1071 !global_settings.playlist_shuffle;
1072 #if CONFIG_CODEC == SWCODEC
1073 dsp_set_replaygain();
1074 #endif
1075 if (global_settings.playlist_shuffle)
1076 playlist_randomise(NULL, current_tick, true);
1077 else
1078 playlist_sort(NULL, true);
1080 break;
1081 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1083 const struct settings_list *rep_setting =
1084 find_setting(&global_settings.repeat_mode, NULL);
1085 option_select_next_val(rep_setting, false, true);
1086 audio_flush_and_reload_tracks();
1088 break;
1089 #endif /* HAVE_TOUCHSCREEN */
1090 /* this case is used by the softlock feature
1091 * it requests a full update here */
1092 case ACTION_REDRAW:
1093 wps_state.do_full_update = true;
1094 break;
1095 case ACTION_NONE: /* Timeout, do a partial update */
1096 update = true;
1097 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1098 break;
1099 #ifdef HAVE_RECORDING
1100 case ACTION_WPS_REC:
1101 exit = true;
1102 break;
1103 #endif
1104 case SYS_POWEROFF:
1105 default_event_handler(SYS_POWEROFF);
1106 break;
1107 case ACTION_WPS_VIEW_PLAYLIST:
1108 gwps_leave_wps();
1109 if (playlist_viewer()) /* true if USB connected */
1110 return SYS_USB_CONNECTED;
1111 restore = true;
1112 break;
1113 default:
1114 if(default_event_handler(button) == SYS_USB_CONNECTED)
1115 return GO_TO_ROOT;
1116 update = true;
1117 break;
1120 if (wps_state.do_full_update || update)
1122 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1123 gwps_caption_backlight(&wps_state);
1124 #endif
1125 FOR_NB_SCREENS(i)
1127 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1128 if (lcd_active()
1129 #ifdef HAVE_REMOTE_LCD
1130 /* currently, all remotes are readable without backlight
1131 * so still update those */
1132 || (i == SCREEN_REMOTE)
1133 #endif
1135 #endif
1137 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
1140 wps_state.do_full_update = false;
1141 update = false;
1144 if (restore && wps_state.id3 &&
1145 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1146 TIME_AFTER(current_tick, restoretimer)))
1148 restore = false;
1149 restoretimer = RESTORE_WPS_INSTANTLY;
1150 gwps_fix_statusbars();
1151 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1152 lcd_activation_set_hook(wps_lcd_activation_hook);
1153 #endif
1154 FOR_NB_SCREENS(i)
1156 screens[i].stop_scroll();
1157 gui_wps_display(&gui_wps[i]);
1161 if (exit) {
1162 #ifdef HAVE_LCD_CHARCELLS
1163 status_set_record(false);
1164 status_set_audio(false);
1165 #endif
1166 if (global_settings.fade_on_stop)
1167 fade(false, true);
1169 if (bookmark)
1170 bookmark_autobookmark();
1171 audio_stop();
1172 #ifdef AB_REPEAT_ENABLE
1173 ab_reset_markers();
1174 #endif
1175 gwps_leave_wps();
1176 #ifdef HAVE_RECORDING
1177 if (button == ACTION_WPS_REC)
1178 return GO_TO_RECSCREEN;
1179 #endif
1180 if (global_settings.browse_current)
1181 return GO_TO_PREVIOUS_BROWSER;
1182 return GO_TO_PREVIOUS;
1185 if (button && !IS_SYSEVENT(button) )
1186 storage_spin();
1188 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1191 /* this is called from the playback thread so NO DRAWING! */
1192 static void track_changed_callback(void *param)
1194 wps_state.id3 = (struct mp3entry*)param;
1195 wps_state.nid3 = audio_next_track();
1196 if (wps_state.id3->cuesheet)
1198 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1199 cue_spoof_id3(wps_state.id3->cuesheet, wps_state.id3);
1201 wps_state.do_full_update = true;
1203 static void nextid3available_callback(void* param)
1205 (void)param;
1206 wps_state.nid3 = audio_next_track();
1207 wps_state.do_full_update = true;
1211 static void wps_state_init(void)
1213 wps_state.ff_rewind = false;
1214 wps_state.paused = false;
1215 if(audio_status() & AUDIO_STATUS_PLAY)
1217 wps_state.id3 = audio_current_track();
1218 wps_state.nid3 = audio_next_track();
1220 else
1222 wps_state.id3 = NULL;
1223 wps_state.nid3 = NULL;
1225 /* We'll be updating due to restore initialized with true */
1226 wps_state.do_full_update = false;
1227 /* add the WPS track event callbacks */
1228 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1229 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1233 #ifdef HAVE_LCD_BITMAP
1234 static void statusbar_toggle_handler(void *data)
1236 (void)data;
1237 int i;
1238 gwps_fix_statusbars();
1240 FOR_NB_SCREENS(i)
1242 struct viewport *vp = &gui_wps[i].data->viewports[0].vp;
1243 bool draw = wpsbars & (VP_SB_ONSCREEN(i) | VP_SB_IGNORE_SETTING(i));
1244 if (!draw)
1246 vp->y = 0;
1247 vp->height = screens[i].lcdheight;
1249 else
1251 bool bar_at_top = statusbar_position(i) != STATUSBAR_BOTTOM;
1252 vp->y = bar_at_top?STATUSBAR_HEIGHT:0;
1253 vp->height = screens[i].lcdheight - STATUSBAR_HEIGHT;
1257 #endif
1259 void gui_sync_wps_init(void)
1261 int i;
1262 FOR_NB_SCREENS(i)
1264 skin_data_init(&wps_datas[i]);
1265 #ifdef HAVE_ALBUMART
1266 wps_datas[i].wps_uses_albumart = 0;
1267 #endif
1268 #ifdef HAVE_REMOTE_LCD
1269 wps_datas[i].remote_wps = (i == SCREEN_REMOTE);
1270 #endif
1271 gui_wps[i].data = &wps_datas[i];
1272 gui_wps[i].display = &screens[i];
1273 /* Currently no seperate wps_state needed/possible
1274 so use the only available ( "global" ) one */
1275 gui_wps[i].state = &wps_state;
1276 gui_wps[i].display->backdrop_unload(BACKDROP_SKIN_WPS);
1278 #ifdef HAVE_LCD_BITMAP
1279 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggle_handler);
1280 #endif
1283 #ifdef HAVE_ALBUMART
1284 bool wps_uses_albumart(int *width, int *height)
1286 int i;
1287 FOR_NB_SCREENS(i) {
1288 struct gui_wps *gwps = &gui_wps[i];
1289 if (gwps->data && (gwps->data->wps_uses_albumart != WPS_ALBUMART_NONE))
1291 if (width)
1292 *width = gui_wps[0].data->albumart_max_width;
1293 if (height)
1294 *height = gui_wps[0].data->albumart_max_height;
1295 return true;
1298 return false;
1300 #endif
1303 #ifdef IPOD_ACCESSORY_PROTOCOL
1304 int wps_get_ff_rewind_count(void)
1306 return wps_state.ff_rewind_count;
1308 #endif