Fix skin engine calling wps code to draw the statusbars (add a pointer to viewportman...
[kugel-rb.git] / apps / gui / wps.c
blobcfa857099ef333a59cc4e021ee7541c9d8ad6230
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;
83 /* currently only one wps_state is needed */
84 static struct wps_state wps_state;
85 static struct gui_wps gui_wps[NB_SCREENS];
86 static struct wps_data wps_datas[NB_SCREENS];
88 /* initial setup of wps_data */
89 static void wps_state_init(void);
90 static void track_changed_callback(void *param);
91 static void nextid3available_callback(void* param);
92 static void statusbar_toggle_handler(void *data);
95 #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps"
96 #ifdef HAVE_REMOTE_LCD
97 #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps"
98 #define DEFAULT_WPS(screen) ((screen) == SCREEN_MAIN ? \
99 WPS_DEFAULTCFG:RWPS_DEFAULTCFG)
100 #else
101 #define DEFAULT_WPS(screen) (WPS_DEFAULTCFG)
102 #endif
104 void wps_data_load(enum screen_type screen, const char *buf, bool isfile)
106 bool loaded_ok;
108 screens[screen].backdrop_unload(BACKDROP_SKIN_WPS);
110 #ifndef __PCTOOL__
112 * Hardcode loading WPS_DEFAULTCFG to cause a reset ideally this
113 * wants to be a virtual file. Feel free to modify dirbrowse()
114 * if you're feeling brave.
117 if (! strcmp(buf, DEFAULT_WPS(screen)) )
119 #ifdef HAVE_REMOTE_LCD
120 if (screen == SCREEN_REMOTE)
121 global_settings.rwps_file[0] = '\0';
122 else
123 #endif
124 global_settings.wps_file[0] = '\0';
125 buf = NULL;
128 #endif /* __PCTOOL__ */
130 loaded_ok = buf && skin_data_load(gui_wps[screen].data,
131 &screens[screen], buf, isfile);
132 if (!loaded_ok) /* load the hardcoded default */
134 char *skin_buf[NB_SCREENS] = {
135 #ifdef HAVE_LCD_BITMAP
136 "%s%?it<%?in<%in. |>%it|%fn>\n"
137 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
138 "%s%?id<%id|%?d1<%d1|(root)>> %?iy<(%iy)|>\n\n"
139 "%al%pc/%pt%ar[%pp:%pe]\n"
140 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
141 "%pb\n%pm\n",
142 #else
143 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d2> - %?id<%id|%d1>\n"
144 "%pc%?ps<*|/>%pt\n",
145 #endif
146 #ifdef HAVE_REMOTE_LCD
147 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
148 "%s%?it<%?in<%in. |>%it|%fn>\n"
149 "%al%pc/%pt%ar[%pp:%pe]\n"
150 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
151 "%pb\n",
152 #endif
154 skin_data_load(gui_wps[screen].data, &screens[screen],
155 skin_buf[screen], false);
157 #ifdef HAVE_REMOVE_LCD
158 gui_wps[screen].data->remote_wps = !(screen == SCREEN_MAIN);
159 #endif
162 void wps_data_init(enum screen_type screen)
164 skin_data_init(gui_wps[screen].data);
168 static bool wps_fading_out = false;
169 void fade(bool fade_in, bool updatewps)
171 int fp_global_vol = global_settings.volume << 8;
172 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
173 int fp_step = (fp_global_vol - fp_min_vol) / 30;
174 int i;
175 wps_fading_out = !fade_in;
176 if (fade_in) {
177 /* fade in */
178 int fp_volume = fp_min_vol;
180 /* zero out the sound */
181 sound_set_volume(fp_min_vol >> 8);
183 sleep(HZ/10); /* let audio thread run */
184 audio_resume();
186 while (fp_volume < fp_global_vol - fp_step) {
187 fp_volume += fp_step;
188 sound_set_volume(fp_volume >> 8);
189 if (updatewps)
191 FOR_NB_SCREENS(i)
192 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
194 sleep(1);
196 sound_set_volume(global_settings.volume);
198 else {
199 /* fade out */
200 int fp_volume = fp_global_vol;
202 while (fp_volume > fp_min_vol + fp_step) {
203 fp_volume -= fp_step;
204 sound_set_volume(fp_volume >> 8);
205 if (updatewps)
207 FOR_NB_SCREENS(i)
208 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
210 sleep(1);
212 audio_pause();
213 wps_fading_out = false;
214 #if CONFIG_CODEC != SWCODEC
215 #ifndef SIMULATOR
216 /* let audio thread run and wait for the mas to run out of data */
217 while (!mp3_pause_done())
218 #endif
219 sleep(HZ/10);
220 #endif
222 /* reset volume to what it was before the fade */
223 sound_set_volume(global_settings.volume);
226 bool is_wps_fading(void)
228 return wps_fading_out;
231 static bool update_onvol_change(struct gui_wps * gwps)
233 skin_update(gwps, WPS_REFRESH_NON_STATIC);
235 #ifdef HAVE_LCD_CHARCELLS
236 splashf(0, "Vol: %3d dB",
237 sound_val2phys(SOUND_VOLUME, global_settings.volume));
238 return true;
239 #endif
240 return false;
244 bool ffwd_rew(int button)
246 unsigned int step = 0; /* current ff/rewind step */
247 unsigned int max_step = 0; /* maximum ff/rewind step */
248 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
249 int direction = -1; /* forward=1 or backward=-1 */
250 bool exit = false;
251 bool usb = false;
252 int i = 0;
253 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
255 if (button == ACTION_NONE)
257 status_set_ffmode(0);
258 return usb;
260 while (!exit)
262 switch ( button )
264 case ACTION_WPS_SEEKFWD:
265 direction = 1;
266 case ACTION_WPS_SEEKBACK:
267 if (wps_state.ff_rewind)
269 if (direction == 1)
271 /* fast forwarding, calc max step relative to end */
272 max_step = (wps_state.id3->length -
273 (wps_state.id3->elapsed +
274 ff_rewind_count)) *
275 FF_REWIND_MAX_PERCENT / 100;
277 else
279 /* rewinding, calc max step relative to start */
280 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
281 FF_REWIND_MAX_PERCENT / 100;
284 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
286 if (step > max_step)
287 step = max_step;
289 ff_rewind_count += step * direction;
291 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
292 step += step >> ff_rw_accel;
294 else
296 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
297 wps_state.id3 && wps_state.id3->length )
299 if (!wps_state.paused)
300 #if (CONFIG_CODEC == SWCODEC)
301 audio_pre_ff_rewind();
302 #else
303 audio_pause();
304 #endif
305 #if CONFIG_KEYPAD == PLAYER_PAD
306 FOR_NB_SCREENS(i)
307 gui_wps[i].display->stop_scroll();
308 #endif
309 if (direction > 0)
310 status_set_ffmode(STATUS_FASTFORWARD);
311 else
312 status_set_ffmode(STATUS_FASTBACKWARD);
314 wps_state.ff_rewind = true;
316 step = 1000 * global_settings.ff_rewind_min_step;
318 else
319 break;
322 if (direction > 0) {
323 if ((wps_state.id3->elapsed + ff_rewind_count) >
324 wps_state.id3->length)
325 ff_rewind_count = wps_state.id3->length -
326 wps_state.id3->elapsed;
328 else {
329 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
330 ff_rewind_count = -wps_state.id3->elapsed;
333 /* set the wps state ff_rewind_count so the progess info
334 displays corectly */
335 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
336 ff_rewind_count:-ff_rewind_count;
337 FOR_NB_SCREENS(i)
339 skin_update(&gui_wps[i],
340 WPS_REFRESH_PLAYER_PROGRESS |
341 WPS_REFRESH_DYNAMIC);
344 break;
346 case ACTION_WPS_STOPSEEK:
347 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
348 audio_ff_rewind(wps_state.id3->elapsed);
349 wps_state.ff_rewind_count = 0;
350 wps_state.ff_rewind = false;
351 status_set_ffmode(0);
352 #if (CONFIG_CODEC != SWCODEC)
353 if (!wps_state.paused)
354 audio_resume();
355 #endif
356 #ifdef HAVE_LCD_CHARCELLS
357 FOR_NB_SCREENS(i)
358 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
359 #endif
360 exit = true;
361 break;
363 default:
364 if(default_event_handler(button) == SYS_USB_CONNECTED) {
365 status_set_ffmode(0);
366 usb = true;
367 exit = true;
369 break;
371 if (!exit)
373 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
374 #ifdef HAVE_TOUCHSCREEN
375 if (button == ACTION_TOUCHSCREEN)
376 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
377 #endif
380 return usb;
384 void display_keylock_text(bool locked)
386 int i;
387 FOR_NB_SCREENS(i)
388 gui_wps[i].display->stop_scroll();
390 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
396 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
397 static void gwps_caption_backlight(struct wps_state *state)
399 if (state && state->id3)
401 #ifdef HAVE_BACKLIGHT
402 if (global_settings.caption_backlight)
404 /* turn on backlight n seconds before track ends, and turn it off n
405 seconds into the new track. n == backlight_timeout, or 5s */
406 int n = global_settings.backlight_timeout * 1000;
408 if ( n < 1000 )
409 n = 5000; /* use 5s if backlight is always on or off */
411 if (((state->id3->elapsed < 1000) ||
412 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
413 (state->paused == false))
414 backlight_on();
416 #endif
417 #ifdef HAVE_REMOTE_LCD
418 if (global_settings.remote_caption_backlight)
420 /* turn on remote backlight n seconds before track ends, and turn it
421 off n seconds into the new track. n == remote_backlight_timeout,
422 or 5s */
423 int n = global_settings.remote_backlight_timeout * 1000;
425 if ( n < 1000 )
426 n = 5000; /* use 5s if backlight is always on or off */
428 if (((state->id3->elapsed < 1000) ||
429 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
430 (state->paused == false))
431 remote_backlight_on();
433 #endif
436 #endif
439 static void change_dir(int direction)
441 if (global_settings.prevent_skip)
442 return;
444 if (direction < 0)
445 audio_prev_dir();
446 else if (direction > 0)
447 audio_next_dir();
448 /* prevent the next dir to immediatly start being ffw'd */
449 action_wait_for_release();
452 static void prev_track(unsigned long skip_thresh)
454 if (wps_state.id3->elapsed < skip_thresh)
456 audio_prev();
457 return;
459 else
461 if (wps_state.id3->cuesheet)
463 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
464 return;
467 if (!wps_state.paused)
468 #if (CONFIG_CODEC == SWCODEC)
469 audio_pre_ff_rewind();
470 #else
471 audio_pause();
472 #endif
474 audio_ff_rewind(0);
476 #if (CONFIG_CODEC != SWCODEC)
477 if (!wps_state.paused)
478 audio_resume();
479 #endif
483 static void next_track(void)
485 /* take care of if we're playing a cuesheet */
486 if (wps_state.id3->cuesheet)
488 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
490 /* if the result was false, then we really want
491 to skip to the next track */
492 return;
496 audio_next();
499 static void play_hop(int direction)
501 unsigned long step = ((unsigned long)global_settings.skip_length)*1000;
502 unsigned long elapsed = wps_state.id3->elapsed;
503 unsigned long remaining = wps_state.id3->length - elapsed;
505 if (!global_settings.prevent_skip &&
506 (!step ||
507 (direction > 0 && step >= remaining) ||
508 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
509 { /* Do normal track skipping */
510 if (direction > 0)
511 next_track();
512 else if (direction < 0)
513 prev_track(DEFAULT_SKIP_TRESH);
514 return;
517 if (direction == 1 && step >= remaining)
519 #if CONFIG_CODEC == SWCODEC
520 if(global_settings.beep)
521 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
522 #endif
523 return;
525 else if ((direction == -1 && elapsed < step))
527 elapsed = 0;
529 else
531 elapsed += step * direction;
533 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
535 #if (CONFIG_CODEC == SWCODEC)
536 audio_pre_ff_rewind();
537 #else
538 audio_pause();
539 #endif
541 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
542 #if (CONFIG_CODEC != SWCODEC)
543 if (!wps_state.paused)
544 audio_resume();
545 #endif
549 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
551 * If the user is unable to see the wps, because the display is deactivated,
552 * we suppress updates until the wps is activated again (the lcd driver will
553 * call this hook to issue an instant update)
554 * */
555 static void wps_lcd_activation_hook(void)
557 wps_state.do_full_update = true;
558 /* force timeout in wps main loop, so that the update is instantly */
559 queue_post(&button_queue, BUTTON_NONE, 0);
561 #endif
563 static void gwps_leave_wps(void)
565 int i, oldbars = VP_SB_HIDE_ALL;
567 FOR_NB_SCREENS(i)
569 gui_wps[i].display->stop_scroll();
570 gui_wps[i].display->backdrop_show(BACKDROP_MAIN);
571 if (statusbar_position(i) != STATUSBAR_OFF)
572 oldbars |= VP_SB_ONSCREEN(i);
575 viewportmanager_set_statusbar(oldbars);
576 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
577 /* Play safe and unregister the hook */
578 lcd_activation_set_hook(NULL);
579 #endif
580 send_event(GUI_EVENT_REFRESH, NULL);
583 #ifdef HAVE_TOUCHSCREEN
584 int wps_get_touchaction(struct wps_data *data)
586 short x,y;
587 short vx, vy;
588 int type = action_get_touchscreen_press(&x, &y);
589 static int last_action = ACTION_NONE;
590 struct touchregion *r;
591 bool repeated = (type == BUTTON_REPEAT);
592 bool released = (type == BUTTON_REL);
593 struct skin_token_list *regions = data->touchregions;
594 while (regions)
596 r = (struct touchregion *)regions->token->value.data;
597 /* make sure this region's viewport is visible */
598 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
600 regions = regions->next;
601 continue;
603 /* reposition the touch inside the viewport */
604 vx = x - r->wvp->vp.x;
605 vy = y - r->wvp->vp.y;
606 /* check if it's inside this viewport */
607 if (vx >= 0 && vx < r->wvp->vp.x + r->wvp->vp.width &&
608 vy >= 0 && vy < r->wvp->vp.y + r->wvp->vp.height)
610 /* now see if the point is inside this region */
611 if (vx >= r->x && vx < r->x+r->width &&
612 vy >= r->y && vy < r->y+r->height)
614 /* reposition the touch within the area */
615 vx -= r->x;
616 vy -= r->y;
618 switch(r->type)
620 case WPS_TOUCHREGION_ACTION:
621 if ((repeated && r->repeat) || (released && !r->repeat))
623 last_action = r->action;
624 return r->action;
626 break;
627 case WPS_TOUCHREGION_SCROLLBAR:
628 if(r->width > r->height)
629 /* landscape */
630 wps_state.id3->elapsed = (vx *
631 wps_state.id3->length) / r->width;
632 else
633 /* portrait */
634 wps_state.id3->elapsed = (vy *
635 wps_state.id3->length) / r->height;
637 audio_ff_rewind(wps_state.id3->elapsed);
638 break;
639 case WPS_TOUCHREGION_VOLUME:
641 int i;
642 const int min_vol = sound_min(SOUND_VOLUME);
643 const int max_vol = sound_max(SOUND_VOLUME);
644 if(r->width > r->height)
645 /* landscape */
646 global_settings.volume = (vx *
647 (max_vol - min_vol)) / r->width;
648 else
649 /* portrait */
650 global_settings.volume = (vy *
651 (max_vol-min_vol)) / r->height;
653 global_settings.volume += min_vol;
654 setvol();
655 FOR_NB_SCREENS(i)
657 gui_wps[i].data->button_time_volume = current_tick;
659 return ACTION_REDRAW;
664 regions = regions->next;
667 if ((last_action == ACTION_WPS_SEEKBACK || last_action == ACTION_WPS_SEEKFWD))
668 return ACTION_WPS_STOPSEEK;
669 last_action = ACTION_TOUCHSCREEN;
670 return ACTION_TOUCHSCREEN;
672 #endif
673 /* The WPS can be left in two ways:
674 * a) call a function, which draws over the wps. In this case, the wps
675 * will be still active (i.e. the below function didn't return)
676 * b) return with a value evaluated by root_menu.c, in this case the wps
677 * is really left, and root_menu will handle the next screen
679 * In either way, call gwps_leave_wps(), in order to restore the correct
680 * "main screen" backdrops and statusbars
682 long gui_wps_show(void)
684 long button = 0;
685 bool restore = true;
686 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
687 bool exit = false;
688 bool bookmark = false;
689 bool update = false;
690 int i;
691 long last_left = 0, last_right = 0;
693 #ifdef HAVE_LCD_CHARCELLS
694 status_set_audio(true);
695 status_set_param(false);
696 #endif
698 #ifdef AB_REPEAT_ENABLE
699 ab_repeat_init();
700 ab_reset_markers();
701 #endif
702 wps_state_init();
703 statusbar_toggle_handler(NULL);
705 while ( 1 )
707 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
709 /* did someone else (i.e power thread) change audio pause mode? */
710 if (wps_state.paused != audio_paused) {
711 wps_state.paused = audio_paused;
713 /* if another thread paused audio, we are probably in car mode,
714 about to shut down. lets save the settings. */
715 if (wps_state.paused) {
716 settings_save();
717 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
718 call_storage_idle_notifys(true);
719 #endif
723 #ifdef HAVE_LCD_BITMAP
724 /* when the peak meter is enabled we want to have a
725 few extra updates to make it look smooth. On the
726 other hand we don't want to waste energy if it
727 isn't displayed */
728 bool pm=false;
729 FOR_NB_SCREENS(i)
731 if(gui_wps[i].data->peak_meter_enabled)
732 pm = true;
735 if (pm) {
736 long next_refresh = current_tick;
737 long next_big_refresh = current_tick + HZ / 5;
738 button = BUTTON_NONE;
739 while (TIME_BEFORE(current_tick, next_big_refresh)) {
740 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_NOBLOCK);
741 if (button != ACTION_NONE) {
742 break;
744 peak_meter_peek();
745 sleep(0); /* Sleep until end of current tick. */
747 if (TIME_AFTER(current_tick, next_refresh)) {
748 FOR_NB_SCREENS(i)
750 if(gui_wps[i].data->peak_meter_enabled)
751 skin_update(&gui_wps[i], WPS_REFRESH_PEAK_METER);
752 next_refresh += HZ / PEAK_METER_FPS;
759 /* The peak meter is disabled
760 -> no additional screen updates needed */
761 else
762 #endif
764 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,
765 restore ? HZ/100 : HZ/5);
768 /* Exit if audio has stopped playing. This happens e.g. at end of
769 playlist or if using the sleep timer. */
770 if (!(audio_status() & AUDIO_STATUS_PLAY))
771 exit = true;
772 #ifdef HAVE_TOUCHSCREEN
773 if (button == ACTION_TOUCHSCREEN)
774 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
775 #endif
776 /* The iPods/X5/M5 use a single button for the A-B mode markers,
777 defined as ACTION_WPSAB_SINGLE in their config files. */
778 #ifdef ACTION_WPSAB_SINGLE
779 if (!global_settings.party_mode && ab_repeat_mode_enabled())
781 static int wps_ab_state = 0;
782 if (button == ACTION_WPSAB_SINGLE)
784 switch (wps_ab_state)
786 case 0: /* set the A spot */
787 button = ACTION_WPS_ABSETA_PREVDIR;
788 break;
789 case 1: /* set the B spot */
790 button = ACTION_WPS_ABSETB_NEXTDIR;
791 break;
792 case 2:
793 button = ACTION_WPS_ABRESET;
794 break;
796 wps_ab_state = (wps_ab_state+1) % 3;
799 #endif
800 switch(button)
802 case ACTION_WPS_CONTEXT:
804 gwps_leave_wps();
805 /* if music is stopped in the context menu we want to exit the wps */
806 if (onplay(wps_state.id3->path,
807 FILE_ATTR_AUDIO, CONTEXT_WPS) == ONPLAY_MAINMENU
808 || !audio_status())
809 return GO_TO_ROOT;
810 restore = true;
812 break;
814 case ACTION_WPS_BROWSE:
815 #ifdef HAVE_LCD_CHARCELLS
816 status_set_record(false);
817 status_set_audio(false);
818 #endif
819 gwps_leave_wps();
820 return GO_TO_PREVIOUS_BROWSER;
821 break;
823 /* play/pause */
824 case ACTION_WPS_PLAY:
825 if (global_settings.party_mode)
826 break;
827 if ( wps_state.paused )
829 wps_state.paused = false;
830 if ( global_settings.fade_on_stop )
831 fade(true, true);
832 else
833 audio_resume();
835 else
837 wps_state.paused = true;
838 if ( global_settings.fade_on_stop )
839 fade(false, true);
840 else
841 audio_pause();
842 settings_save();
843 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
844 call_storage_idle_notifys(true); /* make sure resume info is saved */
845 #endif
847 break;
849 case ACTION_WPS_VOLUP:
851 FOR_NB_SCREENS(i)
852 gui_wps[i].data->button_time_volume = current_tick;
853 global_settings.volume++;
854 bool res = false;
855 setvol();
856 FOR_NB_SCREENS(i)
858 if(update_onvol_change(&gui_wps[i]))
859 res = true;
861 if (res) {
862 restore = true;
863 restoretimer = RESTORE_WPS_NEXT_SECOND;
866 break;
867 case ACTION_WPS_VOLDOWN:
869 FOR_NB_SCREENS(i)
870 gui_wps[i].data->button_time_volume = current_tick;
871 global_settings.volume--;
872 setvol();
873 bool res = false;
874 FOR_NB_SCREENS(i)
876 if(update_onvol_change(&gui_wps[i]))
877 res = true;
879 if (res) {
880 restore = true;
881 restoretimer = RESTORE_WPS_NEXT_SECOND;
884 break;
885 /* fast forward
886 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
887 case ACTION_WPS_SEEKFWD:
888 if (global_settings.party_mode)
889 break;
890 if (current_tick -last_right < HZ)
892 if (wps_state.id3->cuesheet)
894 audio_next();
896 else
898 change_dir(1);
901 else
902 ffwd_rew(ACTION_WPS_SEEKFWD);
903 last_right = last_left = 0;
904 break;
905 /* fast rewind
906 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
907 case ACTION_WPS_SEEKBACK:
908 if (global_settings.party_mode)
909 break;
910 if (current_tick -last_left < HZ)
912 if (wps_state.id3->cuesheet)
914 if (!wps_state.paused)
915 #if (CONFIG_CODEC == SWCODEC)
916 audio_pre_ff_rewind();
917 #else
918 audio_pause();
919 #endif
920 audio_ff_rewind(0);
922 else
924 change_dir(-1);
927 else
928 ffwd_rew(ACTION_WPS_SEEKBACK);
929 last_left = last_right = 0;
930 break;
932 /* prev / restart */
933 case ACTION_WPS_SKIPPREV:
934 if (global_settings.party_mode)
935 break;
936 last_left = current_tick;
937 #ifdef AB_REPEAT_ENABLE
938 /* if we're in A/B repeat mode and the current position
939 is past the A marker, jump back to the A marker... */
940 if ( ab_repeat_mode_enabled() )
942 if ( ab_after_A_marker(wps_state.id3->elapsed) )
944 ab_jump_to_A_marker();
945 break;
946 #if (AB_REPEAT_ENABLE == 2)
947 } else {
948 ab_reset_markers();
949 #endif
952 else
953 /* ...otherwise, do it normally */
954 #endif
955 play_hop(-1);
956 break;
958 /* next
959 OR if skip length set, hop by predetermined amount. */
960 case ACTION_WPS_SKIPNEXT:
961 if (global_settings.party_mode)
962 break;
963 last_right = current_tick;
964 #ifdef AB_REPEAT_ENABLE
965 /* if we're in A/B repeat mode and the current position is
966 before the A marker, jump to the A marker... */
967 if ( ab_repeat_mode_enabled() )
969 if ( ab_before_A_marker(wps_state.id3->elapsed) )
971 ab_jump_to_A_marker();
972 break;
973 #if (AB_REPEAT_ENABLE == 2)
974 } else {
975 ab_reset_markers();
976 #endif
979 else
980 /* ...otherwise, do it normally */
981 #endif
982 play_hop(1);
983 break;
984 /* next / prev directories */
985 /* and set A-B markers if in a-b mode */
986 case ACTION_WPS_ABSETB_NEXTDIR:
987 if (global_settings.party_mode)
988 break;
989 #if defined(AB_REPEAT_ENABLE)
990 if (ab_repeat_mode_enabled())
992 ab_set_B_marker(wps_state.id3->elapsed);
993 ab_jump_to_A_marker();
995 else
996 #endif
998 change_dir(1);
1000 break;
1001 case ACTION_WPS_ABSETA_PREVDIR:
1002 if (global_settings.party_mode)
1003 break;
1004 #if defined(AB_REPEAT_ENABLE)
1005 if (ab_repeat_mode_enabled())
1006 ab_set_A_marker(wps_state.id3->elapsed);
1007 else
1008 #endif
1010 change_dir(-1);
1012 break;
1013 /* menu key functions */
1014 case ACTION_WPS_MENU:
1015 gwps_leave_wps();
1016 return GO_TO_ROOT;
1017 break;
1020 #ifdef HAVE_QUICKSCREEN
1021 case ACTION_WPS_QUICKSCREEN:
1023 gwps_leave_wps();
1024 if (quick_screen_quick(button))
1025 return SYS_USB_CONNECTED;
1026 restore = true;
1028 break;
1029 #endif /* HAVE_QUICKSCREEN */
1031 /* screen settings */
1032 #ifdef BUTTON_F3
1033 case ACTION_F3:
1035 gwps_leave_wps();
1036 if (quick_screen_f3(BUTTON_F3))
1037 return SYS_USB_CONNECTED;
1038 restore = true;
1040 break;
1041 #endif /* BUTTON_F3 */
1043 /* pitch screen */
1044 #ifdef HAVE_PITCHSCREEN
1045 case ACTION_WPS_PITCHSCREEN:
1047 gwps_leave_wps();
1048 if (1 == gui_syncpitchscreen_run())
1049 return SYS_USB_CONNECTED;
1050 restore = true;
1052 break;
1053 #endif /* HAVE_PITCHSCREEN */
1055 #ifdef AB_REPEAT_ENABLE
1056 /* reset A&B markers */
1057 case ACTION_WPS_ABRESET:
1058 if (ab_repeat_mode_enabled())
1060 ab_reset_markers();
1061 update = true;
1063 break;
1064 #endif /* AB_REPEAT_ENABLE */
1066 /* stop and exit wps */
1067 case ACTION_WPS_STOP:
1068 if (global_settings.party_mode)
1069 break;
1070 bookmark = true;
1071 exit = true;
1072 break;
1074 case ACTION_WPS_ID3SCREEN:
1076 gwps_leave_wps();
1077 browse_id3();
1078 restore = true;
1080 break;
1081 #ifdef HAVE_TOUCHSCREEN
1082 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1084 global_settings.playlist_shuffle =
1085 !global_settings.playlist_shuffle;
1086 #if CONFIG_CODEC == SWCODEC
1087 dsp_set_replaygain();
1088 #endif
1089 if (global_settings.playlist_shuffle)
1090 playlist_randomise(NULL, current_tick, true);
1091 else
1092 playlist_sort(NULL, true);
1094 break;
1095 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1097 const struct settings_list *rep_setting =
1098 find_setting(&global_settings.repeat_mode, NULL);
1099 option_select_next_val(rep_setting, false, true);
1100 audio_flush_and_reload_tracks();
1102 break;
1103 #endif /* HAVE_TOUCHSCREEN */
1104 /* this case is used by the softlock feature
1105 * it requests a full update here */
1106 case ACTION_REDRAW:
1107 wps_state.do_full_update = true;
1108 break;
1109 case ACTION_NONE: /* Timeout, do a partial update */
1110 update = true;
1111 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1112 break;
1113 #ifdef HAVE_RECORDING
1114 case ACTION_WPS_REC:
1115 exit = true;
1116 break;
1117 #endif
1118 case SYS_POWEROFF:
1119 default_event_handler(SYS_POWEROFF);
1120 break;
1121 case ACTION_WPS_VIEW_PLAYLIST:
1122 gwps_leave_wps();
1123 if (playlist_viewer()) /* true if USB connected */
1124 return SYS_USB_CONNECTED;
1125 restore = true;
1126 break;
1127 default:
1128 if(default_event_handler(button) == SYS_USB_CONNECTED)
1129 return GO_TO_ROOT;
1130 update = true;
1131 break;
1134 if (wps_state.do_full_update || update)
1136 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1137 gwps_caption_backlight(&wps_state);
1138 #endif
1139 FOR_NB_SCREENS(i)
1141 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1142 if (lcd_active()
1143 #ifdef HAVE_REMOTE_LCD
1144 /* currently, all remotes are readable without backlight
1145 * so still update those */
1146 || (i == SCREEN_REMOTE)
1147 #endif
1149 #endif
1151 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
1154 wps_state.do_full_update = false;
1155 update = false;
1158 if (restore && wps_state.id3 &&
1159 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1160 TIME_AFTER(current_tick, restoretimer)))
1162 restore = false;
1163 restoretimer = RESTORE_WPS_INSTANTLY;
1164 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1165 lcd_activation_set_hook(wps_lcd_activation_hook);
1166 #endif
1167 FOR_NB_SCREENS(i)
1169 screens[i].stop_scroll();
1170 gui_wps_display(&gui_wps[i]);
1174 if (exit) {
1175 #ifdef HAVE_LCD_CHARCELLS
1176 status_set_record(false);
1177 status_set_audio(false);
1178 #endif
1179 if (global_settings.fade_on_stop)
1180 fade(false, true);
1182 if (bookmark)
1183 bookmark_autobookmark();
1184 audio_stop();
1185 #ifdef AB_REPEAT_ENABLE
1186 ab_reset_markers();
1187 #endif
1188 gwps_leave_wps();
1189 #ifdef HAVE_RECORDING
1190 if (button == ACTION_WPS_REC)
1191 return GO_TO_RECSCREEN;
1192 #endif
1193 if (global_settings.browse_current)
1194 return GO_TO_PREVIOUS_BROWSER;
1195 return GO_TO_PREVIOUS;
1198 if (button && !IS_SYSEVENT(button) )
1199 storage_spin();
1201 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1204 /* this is called from the playback thread so NO DRAWING! */
1205 static void track_changed_callback(void *param)
1207 wps_state.id3 = (struct mp3entry*)param;
1208 wps_state.nid3 = audio_next_track();
1209 if (wps_state.id3->cuesheet)
1211 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1212 cue_spoof_id3(wps_state.id3->cuesheet, wps_state.id3);
1214 wps_state.do_full_update = true;
1216 static void nextid3available_callback(void* param)
1218 (void)param;
1219 wps_state.nid3 = audio_next_track();
1220 wps_state.do_full_update = true;
1224 static void wps_state_init(void)
1226 wps_state.ff_rewind = false;
1227 wps_state.paused = false;
1228 if(audio_status() & AUDIO_STATUS_PLAY)
1230 wps_state.id3 = audio_current_track();
1231 wps_state.nid3 = audio_next_track();
1233 else
1235 wps_state.id3 = NULL;
1236 wps_state.nid3 = NULL;
1238 /* We'll be updating due to restore initialized with true */
1239 wps_state.do_full_update = false;
1240 /* add the WPS track event callbacks */
1241 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1242 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1246 #ifdef HAVE_LCD_BITMAP
1247 static void statusbar_toggle_handler(void *data)
1249 (void)data;
1250 int i;
1252 wpsbars = VP_SB_HIDE_ALL;
1253 FOR_NB_SCREENS(i)
1254 { /* fix viewports if needed */
1255 skin_statusbar_changed(&gui_wps[i]);
1257 bool draw = false;
1259 /* fix up gui_wps::statusbars, so that the viewportmanager accepts it*/
1260 if (gui_wps[i].data->wps_sb_tag)
1261 draw = gui_wps[i].data->show_sb_on_wps;
1262 else if (statusbar_position(i) != STATUSBAR_OFF)
1263 draw = true;
1264 if (draw)
1265 wpsbars |=
1266 (VP_SB_ONSCREEN(i) | VP_SB_IGNORE_SETTING(i));
1269 #endif
1271 void gui_sync_wps_init(void)
1273 int i;
1274 FOR_NB_SCREENS(i)
1276 skin_data_init(&wps_datas[i]);
1277 #ifdef HAVE_ALBUMART
1278 wps_datas[i].wps_uses_albumart = 0;
1279 #endif
1280 #ifdef HAVE_REMOTE_LCD
1281 wps_datas[i].remote_wps = (i == SCREEN_REMOTE);
1282 #endif
1283 gui_wps[i].data = &wps_datas[i];
1284 gui_wps[i].display = &screens[i];
1285 /* Currently no seperate wps_state needed/possible
1286 so use the only available ( "global" ) one */
1287 gui_wps[i].state = &wps_state;
1288 gui_wps[i].display->backdrop_unload(BACKDROP_SKIN_WPS);
1289 /* only one wpsbars needed/wanted */
1290 gui_wps[i].statusbars = &wpsbars;
1292 *(gui_wps[SCREEN_MAIN].statusbars) =VP_SB_ALLSCREENS;
1293 #ifdef HAVE_LCD_BITMAP
1294 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggle_handler);
1295 #endif
1298 #ifdef HAVE_ALBUMART
1299 bool wps_uses_albumart(int *width, int *height)
1301 int i;
1302 FOR_NB_SCREENS(i) {
1303 struct gui_wps *gwps = &gui_wps[i];
1304 if (gwps->data && (gwps->data->wps_uses_albumart != WPS_ALBUMART_NONE))
1306 if (width)
1307 *width = gui_wps[0].data->albumart_max_width;
1308 if (height)
1309 *height = gui_wps[0].data->albumart_max_height;
1310 return true;
1313 return false;
1315 #endif
1318 #ifdef IPOD_ACCESSORY_PROTOCOL
1319 int wps_get_ff_rewind_count(void)
1321 return wps_state.ff_rewind_count;
1323 #endif