Do some #ifdef'ing to make the Player happy.
[kugel-rb.git] / apps / gui / wps.c
blob1d97501bc17d6c52bf2bcb54718f511c7accbab2
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 #ifdef HAVE_LCD_BITMAP
93 static void statusbar_toggle_handler(void *data);
94 #endif
96 #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps"
97 #ifdef HAVE_REMOTE_LCD
98 #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps"
99 #define DEFAULT_WPS(screen) ((screen) == SCREEN_MAIN ? \
100 WPS_DEFAULTCFG:RWPS_DEFAULTCFG)
101 #else
102 #define DEFAULT_WPS(screen) (WPS_DEFAULTCFG)
103 #endif
105 void wps_data_load(enum screen_type screen, const char *buf, bool isfile)
107 bool loaded_ok;
109 screens[screen].backdrop_unload(BACKDROP_SKIN_WPS);
111 #ifndef __PCTOOL__
113 * Hardcode loading WPS_DEFAULTCFG to cause a reset ideally this
114 * wants to be a virtual file. Feel free to modify dirbrowse()
115 * if you're feeling brave.
118 if (! strcmp(buf, DEFAULT_WPS(screen)) )
120 #ifdef HAVE_REMOTE_LCD
121 if (screen == SCREEN_REMOTE)
122 global_settings.rwps_file[0] = '\0';
123 else
124 #endif
125 global_settings.wps_file[0] = '\0';
126 buf = NULL;
129 #endif /* __PCTOOL__ */
131 loaded_ok = buf && skin_data_load(gui_wps[screen].data,
132 &screens[screen], buf, isfile);
133 if (!loaded_ok) /* load the hardcoded default */
135 char *skin_buf[NB_SCREENS] = {
136 #ifdef HAVE_LCD_BITMAP
137 "%s%?it<%?in<%in. |>%it|%fn>\n"
138 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
139 "%s%?id<%id|%?d1<%d1|(root)>> %?iy<(%iy)|>\n\n"
140 "%al%pc/%pt%ar[%pp:%pe]\n"
141 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
142 "%pb\n%pm\n",
143 #else
144 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d2> - %?id<%id|%d1>\n"
145 "%pc%?ps<*|/>%pt\n",
146 #endif
147 #ifdef HAVE_REMOTE_LCD
148 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
149 "%s%?it<%?in<%in. |>%it|%fn>\n"
150 "%al%pc/%pt%ar[%pp:%pe]\n"
151 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
152 "%pb\n",
153 #endif
155 skin_data_load(gui_wps[screen].data, &screens[screen],
156 skin_buf[screen], false);
158 #ifdef HAVE_REMOVE_LCD
159 gui_wps[screen].data->remote_wps = !(screen == SCREEN_MAIN);
160 #endif
163 void wps_data_init(enum screen_type screen)
165 skin_data_init(gui_wps[screen].data);
169 static bool wps_fading_out = false;
170 void fade(bool fade_in, bool updatewps)
172 int fp_global_vol = global_settings.volume << 8;
173 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
174 int fp_step = (fp_global_vol - fp_min_vol) / 30;
175 int i;
176 wps_fading_out = !fade_in;
177 if (fade_in) {
178 /* fade in */
179 int fp_volume = fp_min_vol;
181 /* zero out the sound */
182 sound_set_volume(fp_min_vol >> 8);
184 sleep(HZ/10); /* let audio thread run */
185 audio_resume();
187 while (fp_volume < fp_global_vol - fp_step) {
188 fp_volume += fp_step;
189 sound_set_volume(fp_volume >> 8);
190 if (updatewps)
192 FOR_NB_SCREENS(i)
193 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
195 sleep(1);
197 sound_set_volume(global_settings.volume);
199 else {
200 /* fade out */
201 int fp_volume = fp_global_vol;
203 while (fp_volume > fp_min_vol + fp_step) {
204 fp_volume -= fp_step;
205 sound_set_volume(fp_volume >> 8);
206 if (updatewps)
208 FOR_NB_SCREENS(i)
209 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
211 sleep(1);
213 audio_pause();
214 wps_fading_out = false;
215 #if CONFIG_CODEC != SWCODEC
216 #ifndef SIMULATOR
217 /* let audio thread run and wait for the mas to run out of data */
218 while (!mp3_pause_done())
219 #endif
220 sleep(HZ/10);
221 #endif
223 /* reset volume to what it was before the fade */
224 sound_set_volume(global_settings.volume);
227 bool is_wps_fading(void)
229 return wps_fading_out;
232 static bool update_onvol_change(struct gui_wps * gwps)
234 skin_update(gwps, WPS_REFRESH_NON_STATIC);
236 #ifdef HAVE_LCD_CHARCELLS
237 splashf(0, "Vol: %3d dB",
238 sound_val2phys(SOUND_VOLUME, global_settings.volume));
239 return true;
240 #endif
241 return false;
245 bool ffwd_rew(int button)
247 unsigned int step = 0; /* current ff/rewind step */
248 unsigned int max_step = 0; /* maximum ff/rewind step */
249 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
250 int direction = -1; /* forward=1 or backward=-1 */
251 bool exit = false;
252 bool usb = false;
253 int i = 0;
254 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
256 if (button == ACTION_NONE)
258 status_set_ffmode(0);
259 return usb;
261 while (!exit)
263 switch ( button )
265 case ACTION_WPS_SEEKFWD:
266 direction = 1;
267 case ACTION_WPS_SEEKBACK:
268 if (wps_state.ff_rewind)
270 if (direction == 1)
272 /* fast forwarding, calc max step relative to end */
273 max_step = (wps_state.id3->length -
274 (wps_state.id3->elapsed +
275 ff_rewind_count)) *
276 FF_REWIND_MAX_PERCENT / 100;
278 else
280 /* rewinding, calc max step relative to start */
281 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
282 FF_REWIND_MAX_PERCENT / 100;
285 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
287 if (step > max_step)
288 step = max_step;
290 ff_rewind_count += step * direction;
292 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
293 step += step >> ff_rw_accel;
295 else
297 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
298 wps_state.id3 && wps_state.id3->length )
300 if (!wps_state.paused)
301 #if (CONFIG_CODEC == SWCODEC)
302 audio_pre_ff_rewind();
303 #else
304 audio_pause();
305 #endif
306 #if CONFIG_KEYPAD == PLAYER_PAD
307 FOR_NB_SCREENS(i)
308 gui_wps[i].display->stop_scroll();
309 #endif
310 if (direction > 0)
311 status_set_ffmode(STATUS_FASTFORWARD);
312 else
313 status_set_ffmode(STATUS_FASTBACKWARD);
315 wps_state.ff_rewind = true;
317 step = 1000 * global_settings.ff_rewind_min_step;
319 else
320 break;
323 if (direction > 0) {
324 if ((wps_state.id3->elapsed + ff_rewind_count) >
325 wps_state.id3->length)
326 ff_rewind_count = wps_state.id3->length -
327 wps_state.id3->elapsed;
329 else {
330 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
331 ff_rewind_count = -wps_state.id3->elapsed;
334 /* set the wps state ff_rewind_count so the progess info
335 displays corectly */
336 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
337 ff_rewind_count:-ff_rewind_count;
338 FOR_NB_SCREENS(i)
340 skin_update(&gui_wps[i],
341 WPS_REFRESH_PLAYER_PROGRESS |
342 WPS_REFRESH_DYNAMIC);
345 break;
347 case ACTION_WPS_STOPSEEK:
348 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
349 audio_ff_rewind(wps_state.id3->elapsed);
350 wps_state.ff_rewind_count = 0;
351 wps_state.ff_rewind = false;
352 status_set_ffmode(0);
353 #if (CONFIG_CODEC != SWCODEC)
354 if (!wps_state.paused)
355 audio_resume();
356 #endif
357 #ifdef HAVE_LCD_CHARCELLS
358 FOR_NB_SCREENS(i)
359 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
360 #endif
361 exit = true;
362 break;
364 default:
365 if(default_event_handler(button) == SYS_USB_CONNECTED) {
366 status_set_ffmode(0);
367 usb = true;
368 exit = true;
370 break;
372 if (!exit)
374 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
375 #ifdef HAVE_TOUCHSCREEN
376 if (button == ACTION_TOUCHSCREEN)
377 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
378 #endif
381 return usb;
385 void display_keylock_text(bool locked)
387 int i;
388 FOR_NB_SCREENS(i)
389 gui_wps[i].display->stop_scroll();
391 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
397 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
398 static void gwps_caption_backlight(struct wps_state *state)
400 if (state && state->id3)
402 #ifdef HAVE_BACKLIGHT
403 if (global_settings.caption_backlight)
405 /* turn on backlight n seconds before track ends, and turn it off n
406 seconds into the new track. n == backlight_timeout, or 5s */
407 int n = global_settings.backlight_timeout * 1000;
409 if ( n < 1000 )
410 n = 5000; /* use 5s if backlight is always on or off */
412 if (((state->id3->elapsed < 1000) ||
413 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
414 (state->paused == false))
415 backlight_on();
417 #endif
418 #ifdef HAVE_REMOTE_LCD
419 if (global_settings.remote_caption_backlight)
421 /* turn on remote backlight n seconds before track ends, and turn it
422 off n seconds into the new track. n == remote_backlight_timeout,
423 or 5s */
424 int n = global_settings.remote_backlight_timeout * 1000;
426 if ( n < 1000 )
427 n = 5000; /* use 5s if backlight is always on or off */
429 if (((state->id3->elapsed < 1000) ||
430 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
431 (state->paused == false))
432 remote_backlight_on();
434 #endif
437 #endif
440 static void change_dir(int direction)
442 if (global_settings.prevent_skip)
443 return;
445 if (direction < 0)
446 audio_prev_dir();
447 else if (direction > 0)
448 audio_next_dir();
449 /* prevent the next dir to immediatly start being ffw'd */
450 action_wait_for_release();
453 static void prev_track(unsigned long skip_thresh)
455 if (wps_state.id3->elapsed < skip_thresh)
457 audio_prev();
458 return;
460 else
462 if (wps_state.id3->cuesheet)
464 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
465 return;
468 if (!wps_state.paused)
469 #if (CONFIG_CODEC == SWCODEC)
470 audio_pre_ff_rewind();
471 #else
472 audio_pause();
473 #endif
475 audio_ff_rewind(0);
477 #if (CONFIG_CODEC != SWCODEC)
478 if (!wps_state.paused)
479 audio_resume();
480 #endif
484 static void next_track(void)
486 /* take care of if we're playing a cuesheet */
487 if (wps_state.id3->cuesheet)
489 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
491 /* if the result was false, then we really want
492 to skip to the next track */
493 return;
497 audio_next();
500 static void play_hop(int direction)
502 unsigned long step = ((unsigned long)global_settings.skip_length)*1000;
503 unsigned long elapsed = wps_state.id3->elapsed;
504 unsigned long remaining = wps_state.id3->length - elapsed;
506 if (!global_settings.prevent_skip &&
507 (!step ||
508 (direction > 0 && step >= remaining) ||
509 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
510 { /* Do normal track skipping */
511 if (direction > 0)
512 next_track();
513 else if (direction < 0)
514 prev_track(DEFAULT_SKIP_TRESH);
515 return;
518 if (direction == 1 && step >= remaining)
520 #if CONFIG_CODEC == SWCODEC
521 if(global_settings.beep)
522 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
523 #endif
524 return;
526 else if ((direction == -1 && elapsed < step))
528 elapsed = 0;
530 else
532 elapsed += step * direction;
534 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
536 #if (CONFIG_CODEC == SWCODEC)
537 audio_pre_ff_rewind();
538 #else
539 audio_pause();
540 #endif
542 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
543 #if (CONFIG_CODEC != SWCODEC)
544 if (!wps_state.paused)
545 audio_resume();
546 #endif
550 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
552 * If the user is unable to see the wps, because the display is deactivated,
553 * we suppress updates until the wps is activated again (the lcd driver will
554 * call this hook to issue an instant update)
555 * */
556 static void wps_lcd_activation_hook(void)
558 wps_state.do_full_update = true;
559 /* force timeout in wps main loop, so that the update is instantly */
560 queue_post(&button_queue, BUTTON_NONE, 0);
562 #endif
564 static void gwps_leave_wps(void)
566 int i, oldbars = VP_SB_HIDE_ALL;
568 FOR_NB_SCREENS(i)
570 gui_wps[i].display->stop_scroll();
571 gui_wps[i].display->backdrop_show(BACKDROP_MAIN);
572 if (statusbar_position(i) != STATUSBAR_OFF)
573 oldbars |= VP_SB_ONSCREEN(i);
576 viewportmanager_set_statusbar(oldbars);
577 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
578 /* Play safe and unregister the hook */
579 lcd_activation_set_hook(NULL);
580 #endif
581 send_event(GUI_EVENT_REFRESH, NULL);
584 #ifdef HAVE_TOUCHSCREEN
585 int wps_get_touchaction(struct wps_data *data)
587 short x,y;
588 short vx, vy;
589 int type = action_get_touchscreen_press(&x, &y);
590 static int last_action = ACTION_NONE;
591 struct touchregion *r;
592 bool repeated = (type == BUTTON_REPEAT);
593 bool released = (type == BUTTON_REL);
594 struct skin_token_list *regions = data->touchregions;
595 while (regions)
597 r = (struct touchregion *)regions->token->value.data;
598 /* make sure this region's viewport is visible */
599 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
601 regions = regions->next;
602 continue;
604 /* reposition the touch inside the viewport */
605 vx = x - r->wvp->vp.x;
606 vy = y - r->wvp->vp.y;
607 /* check if it's inside this viewport */
608 if (vx >= 0 && vx < r->wvp->vp.x + r->wvp->vp.width &&
609 vy >= 0 && vy < r->wvp->vp.y + r->wvp->vp.height)
611 /* now see if the point is inside this region */
612 if (vx >= r->x && vx < r->x+r->width &&
613 vy >= r->y && vy < r->y+r->height)
615 /* reposition the touch within the area */
616 vx -= r->x;
617 vy -= r->y;
619 switch(r->type)
621 case WPS_TOUCHREGION_ACTION:
622 if ((repeated && r->repeat) || (released && !r->repeat))
624 last_action = r->action;
625 return r->action;
627 break;
628 case WPS_TOUCHREGION_SCROLLBAR:
629 if(r->width > r->height)
630 /* landscape */
631 wps_state.id3->elapsed = (vx *
632 wps_state.id3->length) / r->width;
633 else
634 /* portrait */
635 wps_state.id3->elapsed = (vy *
636 wps_state.id3->length) / r->height;
638 audio_ff_rewind(wps_state.id3->elapsed);
639 break;
640 case WPS_TOUCHREGION_VOLUME:
642 int i;
643 const int min_vol = sound_min(SOUND_VOLUME);
644 const int max_vol = sound_max(SOUND_VOLUME);
645 if(r->width > r->height)
646 /* landscape */
647 global_settings.volume = (vx *
648 (max_vol - min_vol)) / r->width;
649 else
650 /* portrait */
651 global_settings.volume = (vy *
652 (max_vol-min_vol)) / r->height;
654 global_settings.volume += min_vol;
655 setvol();
656 FOR_NB_SCREENS(i)
658 gui_wps[i].data->button_time_volume = current_tick;
660 return ACTION_REDRAW;
665 regions = regions->next;
668 if ((last_action == ACTION_WPS_SEEKBACK || last_action == ACTION_WPS_SEEKFWD))
669 return ACTION_WPS_STOPSEEK;
670 last_action = ACTION_TOUCHSCREEN;
671 return ACTION_TOUCHSCREEN;
673 #endif
674 /* The WPS can be left in two ways:
675 * a) call a function, which draws over the wps. In this case, the wps
676 * will be still active (i.e. the below function didn't return)
677 * b) return with a value evaluated by root_menu.c, in this case the wps
678 * is really left, and root_menu will handle the next screen
680 * In either way, call gwps_leave_wps(), in order to restore the correct
681 * "main screen" backdrops and statusbars
683 long gui_wps_show(void)
685 long button = 0;
686 bool restore = true;
687 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
688 bool exit = false;
689 bool bookmark = false;
690 bool update = false;
691 int i;
692 long last_left = 0, last_right = 0;
694 #ifdef HAVE_LCD_CHARCELLS
695 status_set_audio(true);
696 status_set_param(false);
697 #else
698 statusbar_toggle_handler(NULL);
699 #endif
701 #ifdef AB_REPEAT_ENABLE
702 ab_repeat_init();
703 ab_reset_markers();
704 #endif
705 wps_state_init();
707 while ( 1 )
709 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
711 /* did someone else (i.e power thread) change audio pause mode? */
712 if (wps_state.paused != audio_paused) {
713 wps_state.paused = audio_paused;
715 /* if another thread paused audio, we are probably in car mode,
716 about to shut down. lets save the settings. */
717 if (wps_state.paused) {
718 settings_save();
719 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
720 call_storage_idle_notifys(true);
721 #endif
725 #ifdef HAVE_LCD_BITMAP
726 /* when the peak meter is enabled we want to have a
727 few extra updates to make it look smooth. On the
728 other hand we don't want to waste energy if it
729 isn't displayed */
730 bool pm=false;
731 FOR_NB_SCREENS(i)
733 if(gui_wps[i].data->peak_meter_enabled)
734 pm = true;
737 if (pm) {
738 long next_refresh = current_tick;
739 long next_big_refresh = current_tick + HZ / 5;
740 button = BUTTON_NONE;
741 while (TIME_BEFORE(current_tick, next_big_refresh)) {
742 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_NOBLOCK);
743 if (button != ACTION_NONE) {
744 break;
746 peak_meter_peek();
747 sleep(0); /* Sleep until end of current tick. */
749 if (TIME_AFTER(current_tick, next_refresh)) {
750 FOR_NB_SCREENS(i)
752 if(gui_wps[i].data->peak_meter_enabled)
753 skin_update(&gui_wps[i], WPS_REFRESH_PEAK_METER);
754 next_refresh += HZ / PEAK_METER_FPS;
761 /* The peak meter is disabled
762 -> no additional screen updates needed */
763 else
764 #endif
766 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,
767 restore ? HZ/100 : HZ/5);
770 /* Exit if audio has stopped playing. This happens e.g. at end of
771 playlist or if using the sleep timer. */
772 if (!(audio_status() & AUDIO_STATUS_PLAY))
773 exit = true;
774 #ifdef HAVE_TOUCHSCREEN
775 if (button == ACTION_TOUCHSCREEN)
776 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
777 #endif
778 /* The iPods/X5/M5 use a single button for the A-B mode markers,
779 defined as ACTION_WPSAB_SINGLE in their config files. */
780 #ifdef ACTION_WPSAB_SINGLE
781 if (!global_settings.party_mode && ab_repeat_mode_enabled())
783 static int wps_ab_state = 0;
784 if (button == ACTION_WPSAB_SINGLE)
786 switch (wps_ab_state)
788 case 0: /* set the A spot */
789 button = ACTION_WPS_ABSETA_PREVDIR;
790 break;
791 case 1: /* set the B spot */
792 button = ACTION_WPS_ABSETB_NEXTDIR;
793 break;
794 case 2:
795 button = ACTION_WPS_ABRESET;
796 break;
798 wps_ab_state = (wps_ab_state+1) % 3;
801 #endif
802 switch(button)
804 case ACTION_WPS_CONTEXT:
806 gwps_leave_wps();
807 /* if music is stopped in the context menu we want to exit the wps */
808 if (onplay(wps_state.id3->path,
809 FILE_ATTR_AUDIO, CONTEXT_WPS) == ONPLAY_MAINMENU
810 || !audio_status())
811 return GO_TO_ROOT;
812 restore = true;
814 break;
816 case ACTION_WPS_BROWSE:
817 #ifdef HAVE_LCD_CHARCELLS
818 status_set_record(false);
819 status_set_audio(false);
820 #endif
821 gwps_leave_wps();
822 return GO_TO_PREVIOUS_BROWSER;
823 break;
825 /* play/pause */
826 case ACTION_WPS_PLAY:
827 if (global_settings.party_mode)
828 break;
829 if ( wps_state.paused )
831 wps_state.paused = false;
832 if ( global_settings.fade_on_stop )
833 fade(true, true);
834 else
835 audio_resume();
837 else
839 wps_state.paused = true;
840 if ( global_settings.fade_on_stop )
841 fade(false, true);
842 else
843 audio_pause();
844 settings_save();
845 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
846 call_storage_idle_notifys(true); /* make sure resume info is saved */
847 #endif
849 break;
851 case ACTION_WPS_VOLUP:
853 FOR_NB_SCREENS(i)
854 gui_wps[i].data->button_time_volume = current_tick;
855 global_settings.volume++;
856 bool res = false;
857 setvol();
858 FOR_NB_SCREENS(i)
860 if(update_onvol_change(&gui_wps[i]))
861 res = true;
863 if (res) {
864 restore = true;
865 restoretimer = RESTORE_WPS_NEXT_SECOND;
868 break;
869 case ACTION_WPS_VOLDOWN:
871 FOR_NB_SCREENS(i)
872 gui_wps[i].data->button_time_volume = current_tick;
873 global_settings.volume--;
874 setvol();
875 bool res = false;
876 FOR_NB_SCREENS(i)
878 if(update_onvol_change(&gui_wps[i]))
879 res = true;
881 if (res) {
882 restore = true;
883 restoretimer = RESTORE_WPS_NEXT_SECOND;
886 break;
887 /* fast forward
888 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
889 case ACTION_WPS_SEEKFWD:
890 if (global_settings.party_mode)
891 break;
892 if (current_tick -last_right < HZ)
894 if (wps_state.id3->cuesheet)
896 audio_next();
898 else
900 change_dir(1);
903 else
904 ffwd_rew(ACTION_WPS_SEEKFWD);
905 last_right = last_left = 0;
906 break;
907 /* fast rewind
908 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
909 case ACTION_WPS_SEEKBACK:
910 if (global_settings.party_mode)
911 break;
912 if (current_tick -last_left < HZ)
914 if (wps_state.id3->cuesheet)
916 if (!wps_state.paused)
917 #if (CONFIG_CODEC == SWCODEC)
918 audio_pre_ff_rewind();
919 #else
920 audio_pause();
921 #endif
922 audio_ff_rewind(0);
924 else
926 change_dir(-1);
929 else
930 ffwd_rew(ACTION_WPS_SEEKBACK);
931 last_left = last_right = 0;
932 break;
934 /* prev / restart */
935 case ACTION_WPS_SKIPPREV:
936 if (global_settings.party_mode)
937 break;
938 last_left = current_tick;
939 #ifdef AB_REPEAT_ENABLE
940 /* if we're in A/B repeat mode and the current position
941 is past the A marker, jump back to the A marker... */
942 if ( ab_repeat_mode_enabled() )
944 if ( ab_after_A_marker(wps_state.id3->elapsed) )
946 ab_jump_to_A_marker();
947 break;
948 #if (AB_REPEAT_ENABLE == 2)
949 } else {
950 ab_reset_markers();
951 #endif
954 else
955 /* ...otherwise, do it normally */
956 #endif
957 play_hop(-1);
958 break;
960 /* next
961 OR if skip length set, hop by predetermined amount. */
962 case ACTION_WPS_SKIPNEXT:
963 if (global_settings.party_mode)
964 break;
965 last_right = current_tick;
966 #ifdef AB_REPEAT_ENABLE
967 /* if we're in A/B repeat mode and the current position is
968 before the A marker, jump to the A marker... */
969 if ( ab_repeat_mode_enabled() )
971 if ( ab_before_A_marker(wps_state.id3->elapsed) )
973 ab_jump_to_A_marker();
974 break;
975 #if (AB_REPEAT_ENABLE == 2)
976 } else {
977 ab_reset_markers();
978 #endif
981 else
982 /* ...otherwise, do it normally */
983 #endif
984 play_hop(1);
985 break;
986 /* next / prev directories */
987 /* and set A-B markers if in a-b mode */
988 case ACTION_WPS_ABSETB_NEXTDIR:
989 if (global_settings.party_mode)
990 break;
991 #if defined(AB_REPEAT_ENABLE)
992 if (ab_repeat_mode_enabled())
994 ab_set_B_marker(wps_state.id3->elapsed);
995 ab_jump_to_A_marker();
997 else
998 #endif
1000 change_dir(1);
1002 break;
1003 case ACTION_WPS_ABSETA_PREVDIR:
1004 if (global_settings.party_mode)
1005 break;
1006 #if defined(AB_REPEAT_ENABLE)
1007 if (ab_repeat_mode_enabled())
1008 ab_set_A_marker(wps_state.id3->elapsed);
1009 else
1010 #endif
1012 change_dir(-1);
1014 break;
1015 /* menu key functions */
1016 case ACTION_WPS_MENU:
1017 gwps_leave_wps();
1018 return GO_TO_ROOT;
1019 break;
1022 #ifdef HAVE_QUICKSCREEN
1023 case ACTION_WPS_QUICKSCREEN:
1025 gwps_leave_wps();
1026 if (quick_screen_quick(button))
1027 return SYS_USB_CONNECTED;
1028 restore = true;
1030 break;
1031 #endif /* HAVE_QUICKSCREEN */
1033 /* screen settings */
1034 #ifdef BUTTON_F3
1035 case ACTION_F3:
1037 gwps_leave_wps();
1038 if (quick_screen_f3(BUTTON_F3))
1039 return SYS_USB_CONNECTED;
1040 restore = true;
1042 break;
1043 #endif /* BUTTON_F3 */
1045 /* pitch screen */
1046 #ifdef HAVE_PITCHSCREEN
1047 case ACTION_WPS_PITCHSCREEN:
1049 gwps_leave_wps();
1050 if (1 == gui_syncpitchscreen_run())
1051 return SYS_USB_CONNECTED;
1052 restore = true;
1054 break;
1055 #endif /* HAVE_PITCHSCREEN */
1057 #ifdef AB_REPEAT_ENABLE
1058 /* reset A&B markers */
1059 case ACTION_WPS_ABRESET:
1060 if (ab_repeat_mode_enabled())
1062 ab_reset_markers();
1063 update = true;
1065 break;
1066 #endif /* AB_REPEAT_ENABLE */
1068 /* stop and exit wps */
1069 case ACTION_WPS_STOP:
1070 if (global_settings.party_mode)
1071 break;
1072 bookmark = true;
1073 exit = true;
1074 break;
1076 case ACTION_WPS_ID3SCREEN:
1078 gwps_leave_wps();
1079 browse_id3();
1080 restore = true;
1082 break;
1083 #ifdef HAVE_TOUCHSCREEN
1084 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1086 global_settings.playlist_shuffle =
1087 !global_settings.playlist_shuffle;
1088 #if CONFIG_CODEC == SWCODEC
1089 dsp_set_replaygain();
1090 #endif
1091 if (global_settings.playlist_shuffle)
1092 playlist_randomise(NULL, current_tick, true);
1093 else
1094 playlist_sort(NULL, true);
1096 break;
1097 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1099 const struct settings_list *rep_setting =
1100 find_setting(&global_settings.repeat_mode, NULL);
1101 option_select_next_val(rep_setting, false, true);
1102 audio_flush_and_reload_tracks();
1104 break;
1105 #endif /* HAVE_TOUCHSCREEN */
1106 /* this case is used by the softlock feature
1107 * it requests a full update here */
1108 case ACTION_REDRAW:
1109 wps_state.do_full_update = true;
1110 break;
1111 case ACTION_NONE: /* Timeout, do a partial update */
1112 update = true;
1113 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1114 break;
1115 #ifdef HAVE_RECORDING
1116 case ACTION_WPS_REC:
1117 exit = true;
1118 break;
1119 #endif
1120 case SYS_POWEROFF:
1121 default_event_handler(SYS_POWEROFF);
1122 break;
1123 case ACTION_WPS_VIEW_PLAYLIST:
1124 gwps_leave_wps();
1125 if (playlist_viewer()) /* true if USB connected */
1126 return SYS_USB_CONNECTED;
1127 restore = true;
1128 break;
1129 default:
1130 if(default_event_handler(button) == SYS_USB_CONNECTED)
1131 return GO_TO_ROOT;
1132 update = true;
1133 break;
1136 if (wps_state.do_full_update || update)
1138 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1139 gwps_caption_backlight(&wps_state);
1140 #endif
1141 FOR_NB_SCREENS(i)
1143 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1144 if (lcd_active()
1145 #ifdef HAVE_REMOTE_LCD
1146 /* currently, all remotes are readable without backlight
1147 * so still update those */
1148 || (i == SCREEN_REMOTE)
1149 #endif
1151 #endif
1153 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
1156 wps_state.do_full_update = false;
1157 update = false;
1160 if (restore && wps_state.id3 &&
1161 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1162 TIME_AFTER(current_tick, restoretimer)))
1164 restore = false;
1165 restoretimer = RESTORE_WPS_INSTANTLY;
1166 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1167 lcd_activation_set_hook(wps_lcd_activation_hook);
1168 #endif
1169 FOR_NB_SCREENS(i)
1171 screens[i].stop_scroll();
1172 gui_wps_display(&gui_wps[i]);
1176 if (exit) {
1177 #ifdef HAVE_LCD_CHARCELLS
1178 status_set_record(false);
1179 status_set_audio(false);
1180 #endif
1181 if (global_settings.fade_on_stop)
1182 fade(false, true);
1184 if (bookmark)
1185 bookmark_autobookmark();
1186 audio_stop();
1187 #ifdef AB_REPEAT_ENABLE
1188 ab_reset_markers();
1189 #endif
1190 gwps_leave_wps();
1191 #ifdef HAVE_RECORDING
1192 if (button == ACTION_WPS_REC)
1193 return GO_TO_RECSCREEN;
1194 #endif
1195 if (global_settings.browse_current)
1196 return GO_TO_PREVIOUS_BROWSER;
1197 return GO_TO_PREVIOUS;
1200 if (button && !IS_SYSEVENT(button) )
1201 storage_spin();
1203 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1206 /* this is called from the playback thread so NO DRAWING! */
1207 static void track_changed_callback(void *param)
1209 wps_state.id3 = (struct mp3entry*)param;
1210 wps_state.nid3 = audio_next_track();
1211 if (wps_state.id3->cuesheet)
1213 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1214 cue_spoof_id3(wps_state.id3->cuesheet, wps_state.id3);
1216 wps_state.do_full_update = true;
1218 static void nextid3available_callback(void* param)
1220 (void)param;
1221 wps_state.nid3 = audio_next_track();
1222 wps_state.do_full_update = true;
1226 static void wps_state_init(void)
1228 wps_state.ff_rewind = false;
1229 wps_state.paused = false;
1230 if(audio_status() & AUDIO_STATUS_PLAY)
1232 wps_state.id3 = audio_current_track();
1233 wps_state.nid3 = audio_next_track();
1235 else
1237 wps_state.id3 = NULL;
1238 wps_state.nid3 = NULL;
1240 /* We'll be updating due to restore initialized with true */
1241 wps_state.do_full_update = false;
1242 /* add the WPS track event callbacks */
1243 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1244 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1248 #ifdef HAVE_LCD_BITMAP
1249 static void statusbar_toggle_handler(void *data)
1251 (void)data;
1252 int i;
1254 wpsbars = VP_SB_HIDE_ALL;
1255 FOR_NB_SCREENS(i)
1256 { /* fix viewports if needed */
1257 skin_statusbar_changed(&gui_wps[i]);
1259 bool draw = false;
1261 /* fix up gui_wps::statusbars, so that the viewportmanager accepts it*/
1262 if (gui_wps[i].data->wps_sb_tag)
1263 draw = gui_wps[i].data->show_sb_on_wps;
1264 else if (statusbar_position(i) != STATUSBAR_OFF)
1265 draw = true;
1266 if (draw)
1267 wpsbars |=
1268 (VP_SB_ONSCREEN(i) | VP_SB_IGNORE_SETTING(i));
1271 #endif
1273 void gui_sync_wps_init(void)
1275 int i;
1276 FOR_NB_SCREENS(i)
1278 skin_data_init(&wps_datas[i]);
1279 #ifdef HAVE_ALBUMART
1280 wps_datas[i].wps_uses_albumart = 0;
1281 #endif
1282 #ifdef HAVE_REMOTE_LCD
1283 wps_datas[i].remote_wps = (i == SCREEN_REMOTE);
1284 #endif
1285 gui_wps[i].data = &wps_datas[i];
1286 gui_wps[i].display = &screens[i];
1287 /* Currently no seperate wps_state needed/possible
1288 so use the only available ( "global" ) one */
1289 gui_wps[i].state = &wps_state;
1290 gui_wps[i].display->backdrop_unload(BACKDROP_SKIN_WPS);
1291 /* only one wpsbars needed/wanted */
1292 gui_wps[i].statusbars = &wpsbars;
1294 *(gui_wps[SCREEN_MAIN].statusbars) =VP_SB_ALLSCREENS;
1295 #ifdef HAVE_LCD_BITMAP
1296 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggle_handler);
1297 #endif
1300 #ifdef HAVE_ALBUMART
1301 bool wps_uses_albumart(int *width, int *height)
1303 int i;
1304 FOR_NB_SCREENS(i) {
1305 struct gui_wps *gwps = &gui_wps[i];
1306 if (gwps->data && (gwps->data->wps_uses_albumart != WPS_ALBUMART_NONE))
1308 if (width)
1309 *width = gui_wps[0].data->albumart_max_width;
1310 if (height)
1311 *height = gui_wps[0].data->albumart_max_height;
1312 return true;
1315 return false;
1317 #endif
1320 #ifdef IPOD_ACCESSORY_PROTOCOL
1321 int wps_get_ff_rewind_count(void)
1323 return wps_state.ff_rewind_count;
1325 #endif