Also fix Debug->View partitions when SECTOR_SIZE!=512
[kugel-rb.git] / apps / gui / wps.c
blobd2c1361a4400bd45a668801ee981db843642620e
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 "sprintf.h"
35 #include "settings.h"
36 #include "skin_engine/skin_engine.h"
37 #include "mp3_playback.h"
38 #include "audio.h"
39 #include "usb.h"
40 #include "status.h"
41 #include "storage.h"
42 #include "screens.h"
43 #include "playlist.h"
44 #ifdef HAVE_LCD_BITMAP
45 #include "icons.h"
46 #include "peakmeter.h"
47 #endif
48 #include "lang.h"
49 #include "bookmark.h"
50 #include "misc.h"
51 #include "sound.h"
52 #include "onplay.h"
53 #include "abrepeat.h"
54 #include "playback.h"
55 #include "splash.h"
56 #include "cuesheet.h"
57 #include "ata_idle_notify.h"
58 #include "root_menu.h"
59 #include "backdrop.h"
60 #include "quickscreen.h"
61 #include "pitchscreen.h"
62 #include "appevents.h"
63 #include "viewport.h"
64 #include "pcmbuf.h"
65 #include "option_select.h"
66 #include "dsp.h"
67 #include "playlist_viewer.h"
68 #include "wps.h"
69 #include "statusbar-skinned.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 3000l
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 /* currently only one wps_state is needed, initialize to 0 */
82 struct wps_state wps_state = { .id3 = NULL };
83 static struct gui_wps gui_wps[NB_SCREENS] = {{ .data = NULL }};
84 static struct wps_data wps_datas[NB_SCREENS] = {{ .wps_loaded = 0 }};
85 static struct wps_sync_data wps_sync_data = { .do_full_update = false };
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);
91 #ifdef HAVE_LCD_BITMAP
92 static void statusbar_toggle_handler(void *data);
93 #endif
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 (buf && ! 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(screen, gui_wps[screen].data, 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(screen, gui_wps[screen].data, skin_buf[screen], false);
158 void fade(bool fade_in, bool updatewps)
160 int fp_global_vol = global_settings.volume << 8;
161 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
162 int fp_step = (fp_global_vol - fp_min_vol) / 30;
163 int i;
164 wps_state.is_fading = !fade_in;
165 if (fade_in) {
166 /* fade in */
167 int fp_volume = fp_min_vol;
169 /* zero out the sound */
170 sound_set_volume(fp_min_vol >> 8);
172 sleep(HZ/10); /* let audio thread run */
173 audio_resume();
175 while (fp_volume < fp_global_vol - fp_step) {
176 fp_volume += fp_step;
177 sound_set_volume(fp_volume >> 8);
178 if (updatewps)
180 FOR_NB_SCREENS(i)
181 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
183 sleep(1);
185 sound_set_volume(global_settings.volume);
187 else {
188 /* fade out */
189 int fp_volume = fp_global_vol;
191 while (fp_volume > fp_min_vol + fp_step) {
192 fp_volume -= fp_step;
193 sound_set_volume(fp_volume >> 8);
194 if (updatewps)
196 FOR_NB_SCREENS(i)
197 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
199 sleep(1);
201 audio_pause();
202 wps_state.is_fading = false;
203 #if CONFIG_CODEC != SWCODEC
204 #ifndef SIMULATOR
205 /* let audio thread run and wait for the mas to run out of data */
206 while (!mp3_pause_done())
207 #endif
208 sleep(HZ/10);
209 #endif
211 /* reset volume to what it was before the fade */
212 sound_set_volume(global_settings.volume);
216 static bool update_onvol_change(struct gui_wps * gwps)
218 skin_update(gwps, WPS_REFRESH_NON_STATIC);
220 #ifdef HAVE_LCD_CHARCELLS
221 splashf(0, "Vol: %3d dB",
222 sound_val2phys(SOUND_VOLUME, global_settings.volume));
223 return true;
224 #endif
225 return false;
229 bool ffwd_rew(int button)
231 unsigned int step = 0; /* current ff/rewind step */
232 unsigned int max_step = 0; /* maximum ff/rewind step */
233 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
234 int direction = -1; /* forward=1 or backward=-1 */
235 bool exit = false;
236 bool usb = false;
237 int i = 0;
238 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
240 if (button == ACTION_NONE)
242 status_set_ffmode(0);
243 return usb;
245 while (!exit)
247 switch ( button )
249 case ACTION_WPS_SEEKFWD:
250 direction = 1;
251 case ACTION_WPS_SEEKBACK:
252 if (wps_state.ff_rewind)
254 if (direction == 1)
256 /* fast forwarding, calc max step relative to end */
257 max_step = (wps_state.id3->length -
258 (wps_state.id3->elapsed +
259 ff_rewind_count)) *
260 FF_REWIND_MAX_PERCENT / 100;
262 else
264 /* rewinding, calc max step relative to start */
265 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
266 FF_REWIND_MAX_PERCENT / 100;
269 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
271 if (step > max_step)
272 step = max_step;
274 ff_rewind_count += step * direction;
276 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
277 step += step >> ff_rw_accel;
279 else
281 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
282 wps_state.id3 && wps_state.id3->length )
284 if (!wps_state.paused)
285 #if (CONFIG_CODEC == SWCODEC)
286 audio_pre_ff_rewind();
287 #else
288 audio_pause();
289 #endif
290 #if CONFIG_KEYPAD == PLAYER_PAD
291 FOR_NB_SCREENS(i)
292 gui_wps[i].display->stop_scroll();
293 #endif
294 if (direction > 0)
295 status_set_ffmode(STATUS_FASTFORWARD);
296 else
297 status_set_ffmode(STATUS_FASTBACKWARD);
299 wps_state.ff_rewind = true;
301 step = 1000 * global_settings.ff_rewind_min_step;
303 else
304 break;
307 if (direction > 0) {
308 if ((wps_state.id3->elapsed + ff_rewind_count) >
309 wps_state.id3->length)
310 ff_rewind_count = wps_state.id3->length -
311 wps_state.id3->elapsed;
313 else {
314 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
315 ff_rewind_count = -wps_state.id3->elapsed;
318 /* set the wps state ff_rewind_count so the progess info
319 displays corectly */
320 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
321 ff_rewind_count:-ff_rewind_count;
322 FOR_NB_SCREENS(i)
324 skin_update(&gui_wps[i],
325 WPS_REFRESH_PLAYER_PROGRESS |
326 WPS_REFRESH_DYNAMIC);
329 break;
331 case ACTION_WPS_STOPSEEK:
332 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
333 audio_ff_rewind(wps_state.id3->elapsed);
334 wps_state.ff_rewind_count = 0;
335 wps_state.ff_rewind = false;
336 status_set_ffmode(0);
337 #if (CONFIG_CODEC != SWCODEC)
338 if (!wps_state.paused)
339 audio_resume();
340 #endif
341 #ifdef HAVE_LCD_CHARCELLS
342 FOR_NB_SCREENS(i)
343 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
344 #endif
345 exit = true;
346 break;
348 default:
349 if(default_event_handler(button) == SYS_USB_CONNECTED) {
350 status_set_ffmode(0);
351 usb = true;
352 exit = true;
354 break;
356 if (!exit)
358 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
359 #ifdef HAVE_TOUCHSCREEN
360 if (button == ACTION_TOUCHSCREEN)
361 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
362 #endif
365 return usb;
369 void display_keylock_text(bool locked)
371 int i;
372 FOR_NB_SCREENS(i)
373 gui_wps[i].display->stop_scroll();
375 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
381 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
382 static void gwps_caption_backlight(struct wps_state *state)
384 if (state && state->id3)
386 #ifdef HAVE_BACKLIGHT
387 if (global_settings.caption_backlight)
389 /* turn on backlight n seconds before track ends, and turn it off n
390 seconds into the new track. n == backlight_timeout, or 5s */
391 int n = global_settings.backlight_timeout * 1000;
393 if ( n < 1000 )
394 n = 5000; /* use 5s if backlight is always on or off */
396 if (((state->id3->elapsed < 1000) ||
397 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
398 (state->paused == false))
399 backlight_on();
401 #endif
402 #ifdef HAVE_REMOTE_LCD
403 if (global_settings.remote_caption_backlight)
405 /* turn on remote backlight n seconds before track ends, and turn it
406 off n seconds into the new track. n == remote_backlight_timeout,
407 or 5s */
408 int n = global_settings.remote_backlight_timeout * 1000;
410 if ( n < 1000 )
411 n = 5000; /* use 5s if backlight is always on or off */
413 if (((state->id3->elapsed < 1000) ||
414 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
415 (state->paused == false))
416 remote_backlight_on();
418 #endif
421 #endif
424 static void change_dir(int direction)
426 if (global_settings.prevent_skip)
427 return;
429 if (direction < 0)
430 audio_prev_dir();
431 else if (direction > 0)
432 audio_next_dir();
433 /* prevent the next dir to immediatly start being ffw'd */
434 action_wait_for_release();
437 static void prev_track(unsigned long skip_thresh)
439 if (wps_state.id3->elapsed < skip_thresh)
441 audio_prev();
442 return;
444 else
446 if (wps_state.id3->cuesheet)
448 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
449 return;
452 if (!wps_state.paused)
453 #if (CONFIG_CODEC == SWCODEC)
454 audio_pre_ff_rewind();
455 #else
456 audio_pause();
457 #endif
459 audio_ff_rewind(0);
461 #if (CONFIG_CODEC != SWCODEC)
462 if (!wps_state.paused)
463 audio_resume();
464 #endif
468 static void next_track(void)
470 /* take care of if we're playing a cuesheet */
471 if (wps_state.id3->cuesheet)
473 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
475 /* if the result was false, then we really want
476 to skip to the next track */
477 return;
481 audio_next();
484 static void play_hop(int direction)
486 long step = global_settings.skip_length*1000;
487 long elapsed = wps_state.id3->elapsed;
488 long remaining = wps_state.id3->length - elapsed;
490 if (step < 0)
492 if (direction < 0)
494 prev_track(DEFAULT_SKIP_TRESH);
495 return;
497 else if (remaining < DEFAULT_SKIP_TRESH*2)
499 next_track();
500 return;
502 else
503 elapsed += (remaining - DEFAULT_SKIP_TRESH*2);
505 else 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;
516 else if (direction == 1 && step >= remaining)
518 #if CONFIG_CODEC == SWCODEC
519 if(global_settings.beep)
520 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
521 #endif
522 return;
524 else if ((direction == -1 && elapsed < step))
526 elapsed = 0;
528 else
530 elapsed += step * direction;
532 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
534 #if (CONFIG_CODEC == SWCODEC)
535 audio_pre_ff_rewind();
536 #else
537 audio_pause();
538 #endif
540 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
541 #if (CONFIG_CODEC != SWCODEC)
542 if (!wps_state.paused)
543 audio_resume();
544 #endif
548 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
550 * If the user is unable to see the wps, because the display is deactivated,
551 * we suppress updates until the wps is activated again (the lcd driver will
552 * call this hook to issue an instant update)
553 * */
554 static void wps_lcd_activation_hook(void *param)
556 (void)param;
557 wps_sync_data.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 remove_event(LCD_EVENT_ACTIVATION, wps_lcd_activation_hook);
579 #endif
580 /* unhandle statusbar update delay */
581 sb_skin_set_update_delay(DEFAULT_UPDATE_DELAY);
582 send_event(GUI_EVENT_REFRESH, NULL);
585 #ifdef HAVE_TOUCHSCREEN
586 int wps_get_touchaction(struct wps_data *data)
588 short x,y;
589 short vx, vy;
590 int type = action_get_touchscreen_press(&x, &y);
591 static int last_action = ACTION_NONE;
592 struct touchregion *r;
593 bool repeated = (type == BUTTON_REPEAT);
594 bool released = (type == BUTTON_REL);
595 struct skin_token_list *regions = data->touchregions;
596 while (regions)
598 r = (struct touchregion *)regions->token->value.data;
599 /* make sure this region's viewport is visible */
600 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
602 regions = regions->next;
603 continue;
605 /* check if it's inside this viewport */
606 if (viewport_point_within_vp(&(r->wvp->vp), x, y))
607 { /* reposition the touch inside the viewport since touchregions
608 * are relative to a preceding viewport */
609 vx = x - r->wvp->vp.x;
610 vy = y - r->wvp->vp.y;
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 if (!wps_state.paused)
639 #if (CONFIG_CODEC == SWCODEC)
640 audio_pre_ff_rewind();
641 #else
642 audio_pause();
643 #endif
644 audio_ff_rewind(wps_state.id3->elapsed);
645 #if (CONFIG_CODEC != SWCODEC)
646 if (!wps_state.paused)
647 audio_resume();
648 #endif
649 break;
650 case WPS_TOUCHREGION_VOLUME:
652 const int min_vol = sound_min(SOUND_VOLUME);
653 const int max_vol = sound_max(SOUND_VOLUME);
654 if(r->width > r->height)
655 /* landscape */
656 global_settings.volume = (vx *
657 (max_vol - min_vol)) / r->width;
658 else
659 /* portrait */
660 global_settings.volume = (vy *
661 (max_vol-min_vol)) / r->height;
663 global_settings.volume += min_vol;
664 setvol();
665 return ACTION_REDRAW;
670 regions = regions->next;
673 if ((last_action == ACTION_WPS_SEEKBACK || last_action == ACTION_WPS_SEEKFWD))
674 return ACTION_WPS_STOPSEEK;
675 last_action = ACTION_TOUCHSCREEN;
676 return ACTION_TOUCHSCREEN;
678 #endif
679 /* The WPS can be left in two ways:
680 * a) call a function, which draws over the wps. In this case, the wps
681 * will be still active (i.e. the below function didn't return)
682 * b) return with a value evaluated by root_menu.c, in this case the wps
683 * is really left, and root_menu will handle the next screen
685 * In either way, call gwps_leave_wps(), in order to restore the correct
686 * "main screen" backdrops and statusbars
688 long gui_wps_show(void)
690 long button = 0;
691 bool restore = true;
692 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
693 bool exit = false;
694 bool bookmark = false;
695 bool update = false;
696 bool vol_changed = false;
697 int i;
698 long last_left = 0, last_right = 0;
700 #ifdef HAVE_LCD_CHARCELLS
701 status_set_audio(true);
702 status_set_param(false);
703 #else
704 statusbar_toggle_handler(NULL);
705 #endif
707 #ifdef AB_REPEAT_ENABLE
708 ab_repeat_init();
709 ab_reset_markers();
710 #endif
711 wps_state_init();
713 while ( 1 )
715 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
717 /* did someone else (i.e power thread) change audio pause mode? */
718 if (wps_state.paused != audio_paused) {
719 wps_state.paused = audio_paused;
721 /* if another thread paused audio, we are probably in car mode,
722 about to shut down. lets save the settings. */
723 if (wps_state.paused) {
724 settings_save();
725 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
726 call_storage_idle_notifys(true);
727 #endif
730 #ifdef HAVE_LCD_BITMAP
731 /* when the peak meter is enabled we want to have a
732 few extra updates to make it look smooth. On the
733 other hand we don't want to waste energy if it
734 isn't displayed */
735 bool pm=false;
736 FOR_NB_SCREENS(i)
738 if(gui_wps[i].data->peak_meter_enabled)
739 pm = true;
742 if (pm) {
743 long next_refresh = current_tick;
744 long next_big_refresh = current_tick + HZ / 5;
745 button = BUTTON_NONE;
746 while (TIME_BEFORE(current_tick, next_big_refresh)) {
747 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_NOBLOCK);
748 if (button != ACTION_NONE) {
749 break;
751 peak_meter_peek();
752 sleep(0); /* Sleep until end of current tick. */
754 if (TIME_AFTER(current_tick, next_refresh)) {
755 FOR_NB_SCREENS(i)
757 if(gui_wps[i].data->peak_meter_enabled)
758 skin_update(&gui_wps[i], WPS_REFRESH_PEAK_METER);
759 next_refresh += HZ / PEAK_METER_FPS;
766 /* The peak meter is disabled
767 -> no additional screen updates needed */
768 else
769 #endif
771 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,
772 restore ? HZ/100 : HZ/5);
775 /* Exit if audio has stopped playing. This happens e.g. at end of
776 playlist or if using the sleep timer. */
777 if (!(audio_status() & AUDIO_STATUS_PLAY))
778 exit = true;
779 #ifdef HAVE_TOUCHSCREEN
780 if (button == ACTION_TOUCHSCREEN)
781 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
782 #endif
783 /* The iPods/X5/M5 use a single button for the A-B mode markers,
784 defined as ACTION_WPSAB_SINGLE in their config files. */
785 #ifdef ACTION_WPSAB_SINGLE
786 if (!global_settings.party_mode && ab_repeat_mode_enabled())
788 static int wps_ab_state = 0;
789 if (button == ACTION_WPSAB_SINGLE)
791 switch (wps_ab_state)
793 case 0: /* set the A spot */
794 button = ACTION_WPS_ABSETA_PREVDIR;
795 break;
796 case 1: /* set the B spot */
797 button = ACTION_WPS_ABSETB_NEXTDIR;
798 break;
799 case 2:
800 button = ACTION_WPS_ABRESET;
801 break;
803 wps_ab_state = (wps_ab_state+1) % 3;
806 #endif
807 switch(button)
809 case ACTION_WPS_CONTEXT:
811 gwps_leave_wps();
812 /* if music is stopped in the context menu we want to exit the wps */
813 if (onplay(wps_state.id3->path,
814 FILE_ATTR_AUDIO, CONTEXT_WPS) == ONPLAY_MAINMENU
815 || !audio_status())
816 return GO_TO_ROOT;
817 restore = true;
819 break;
821 case ACTION_WPS_BROWSE:
822 #ifdef HAVE_LCD_CHARCELLS
823 status_set_record(false);
824 status_set_audio(false);
825 #endif
826 gwps_leave_wps();
827 return GO_TO_PREVIOUS_BROWSER;
828 break;
830 /* play/pause */
831 case ACTION_WPS_PLAY:
832 if (global_settings.party_mode)
833 break;
834 if ( wps_state.paused )
836 wps_state.paused = false;
837 if ( global_settings.fade_on_stop )
838 fade(true, true);
839 else
840 audio_resume();
842 else
844 wps_state.paused = true;
845 if ( global_settings.fade_on_stop )
846 fade(false, true);
847 else
848 audio_pause();
849 settings_save();
850 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
851 call_storage_idle_notifys(true); /* make sure resume info is saved */
852 #endif
854 break;
856 case ACTION_WPS_VOLUP:
857 global_settings.volume++;
858 vol_changed = true;
859 break;
860 case ACTION_WPS_VOLDOWN:
861 global_settings.volume--;
862 vol_changed = true;
863 break;
864 /* fast forward
865 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
866 case ACTION_WPS_SEEKFWD:
867 if (global_settings.party_mode)
868 break;
869 if (current_tick -last_right < HZ)
871 if (wps_state.id3->cuesheet)
873 audio_next();
875 else
877 change_dir(1);
880 else
881 ffwd_rew(ACTION_WPS_SEEKFWD);
882 last_right = last_left = 0;
883 break;
884 /* fast rewind
885 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
886 case ACTION_WPS_SEEKBACK:
887 if (global_settings.party_mode)
888 break;
889 if (current_tick -last_left < HZ)
891 if (wps_state.id3->cuesheet)
893 if (!wps_state.paused)
894 #if (CONFIG_CODEC == SWCODEC)
895 audio_pre_ff_rewind();
896 #else
897 audio_pause();
898 #endif
899 audio_ff_rewind(0);
901 else
903 change_dir(-1);
906 else
907 ffwd_rew(ACTION_WPS_SEEKBACK);
908 last_left = last_right = 0;
909 break;
911 /* prev / restart */
912 case ACTION_WPS_SKIPPREV:
913 if (global_settings.party_mode)
914 break;
915 last_left = current_tick;
916 #ifdef AB_REPEAT_ENABLE
917 /* if we're in A/B repeat mode and the current position
918 is past the A marker, jump back to the A marker... */
919 if ( ab_repeat_mode_enabled() )
921 if ( ab_after_A_marker(wps_state.id3->elapsed) )
923 ab_jump_to_A_marker();
924 break;
925 #if (AB_REPEAT_ENABLE == 2)
926 } else {
927 ab_reset_markers();
928 #endif
931 else
932 /* ...otherwise, do it normally */
933 #endif
934 play_hop(-1);
935 break;
937 /* next
938 OR if skip length set, hop by predetermined amount. */
939 case ACTION_WPS_SKIPNEXT:
940 if (global_settings.party_mode)
941 break;
942 last_right = current_tick;
943 #ifdef AB_REPEAT_ENABLE
944 /* if we're in A/B repeat mode and the current position is
945 before the A marker, jump to the A marker... */
946 if ( ab_repeat_mode_enabled() )
948 if ( ab_before_A_marker(wps_state.id3->elapsed) )
950 ab_jump_to_A_marker();
951 break;
952 #if (AB_REPEAT_ENABLE == 2)
953 } else {
954 ab_reset_markers();
955 #endif
958 else
959 /* ...otherwise, do it normally */
960 #endif
961 play_hop(1);
962 break;
963 /* next / prev directories */
964 /* and set A-B markers if in a-b mode */
965 case ACTION_WPS_ABSETB_NEXTDIR:
966 if (global_settings.party_mode)
967 break;
968 #if defined(AB_REPEAT_ENABLE)
969 if (ab_repeat_mode_enabled())
971 ab_set_B_marker(wps_state.id3->elapsed);
972 ab_jump_to_A_marker();
974 else
975 #endif
977 change_dir(1);
979 break;
980 case ACTION_WPS_ABSETA_PREVDIR:
981 if (global_settings.party_mode)
982 break;
983 #if defined(AB_REPEAT_ENABLE)
984 if (ab_repeat_mode_enabled())
985 ab_set_A_marker(wps_state.id3->elapsed);
986 else
987 #endif
989 change_dir(-1);
991 break;
992 /* menu key functions */
993 case ACTION_WPS_MENU:
994 gwps_leave_wps();
995 return GO_TO_ROOT;
996 break;
999 #ifdef HAVE_QUICKSCREEN
1000 case ACTION_WPS_QUICKSCREEN:
1002 gwps_leave_wps();
1003 if (quick_screen_quick(button))
1004 return GO_TO_ROOT;
1005 restore = true;
1007 break;
1008 #endif /* HAVE_QUICKSCREEN */
1010 /* screen settings */
1011 #ifdef BUTTON_F3
1012 case ACTION_F3:
1014 gwps_leave_wps();
1015 if (quick_screen_f3(BUTTON_F3))
1016 return GO_TO_ROOT;
1017 restore = true;
1019 break;
1020 #endif /* BUTTON_F3 */
1022 /* pitch screen */
1023 #ifdef HAVE_PITCHSCREEN
1024 case ACTION_WPS_PITCHSCREEN:
1026 gwps_leave_wps();
1027 if (1 == gui_syncpitchscreen_run())
1028 return GO_TO_ROOT;
1029 restore = true;
1031 break;
1032 #endif /* HAVE_PITCHSCREEN */
1034 #ifdef AB_REPEAT_ENABLE
1035 /* reset A&B markers */
1036 case ACTION_WPS_ABRESET:
1037 if (ab_repeat_mode_enabled())
1039 ab_reset_markers();
1040 update = true;
1042 break;
1043 #endif /* AB_REPEAT_ENABLE */
1045 /* stop and exit wps */
1046 case ACTION_WPS_STOP:
1047 if (global_settings.party_mode)
1048 break;
1049 bookmark = true;
1050 exit = true;
1051 break;
1053 case ACTION_WPS_ID3SCREEN:
1055 gwps_leave_wps();
1056 if (browse_id3())
1057 return GO_TO_ROOT;
1058 restore = true;
1060 break;
1061 #ifdef HAVE_TOUCHSCREEN
1062 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1064 global_settings.playlist_shuffle =
1065 !global_settings.playlist_shuffle;
1066 #if CONFIG_CODEC == SWCODEC
1067 dsp_set_replaygain();
1068 #endif
1069 if (global_settings.playlist_shuffle)
1070 playlist_randomise(NULL, current_tick, true);
1071 else
1072 playlist_sort(NULL, true);
1074 break;
1075 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1077 const struct settings_list *rep_setting =
1078 find_setting(&global_settings.repeat_mode, NULL);
1079 option_select_next_val(rep_setting, false, true);
1080 audio_flush_and_reload_tracks();
1082 break;
1083 #endif /* HAVE_TOUCHSCREEN */
1084 /* this case is used by the softlock feature
1085 * it requests a full update here */
1086 case ACTION_REDRAW:
1087 wps_sync_data.do_full_update = true;
1088 break;
1089 case ACTION_NONE: /* Timeout, do a partial update */
1090 update = true;
1091 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1092 break;
1093 #ifdef HAVE_RECORDING
1094 case ACTION_WPS_REC:
1095 exit = true;
1096 break;
1097 #endif
1098 case SYS_POWEROFF:
1099 default_event_handler(SYS_POWEROFF);
1100 break;
1101 case ACTION_WPS_VIEW_PLAYLIST:
1102 gwps_leave_wps();
1103 if (playlist_viewer()) /* true if USB connected */
1104 return GO_TO_ROOT;
1105 restore = true;
1106 break;
1107 default:
1108 if(default_event_handler(button) == SYS_USB_CONNECTED)
1110 gwps_leave_wps();
1111 return GO_TO_ROOT;
1113 update = true;
1114 break;
1117 if (vol_changed)
1119 bool res = false;
1120 vol_changed = false;
1121 setvol();
1122 FOR_NB_SCREENS(i)
1124 if(update_onvol_change(&gui_wps[i]))
1125 res = true;
1127 if (res) {
1128 restore = true;
1129 restoretimer = RESTORE_WPS_NEXT_SECOND;
1133 if (wps_sync_data.do_full_update || update)
1135 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1136 gwps_caption_backlight(&wps_state);
1137 #endif
1138 FOR_NB_SCREENS(i)
1140 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1141 /* currently, all remotes are readable without backlight
1142 * so still update those */
1143 if (lcd_active() || (i != SCREEN_MAIN))
1144 #endif
1146 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
1149 wps_sync_data.do_full_update = false;
1150 update = false;
1153 if (restore && wps_state.id3 &&
1154 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1155 TIME_AFTER(current_tick, restoretimer)))
1157 restore = false;
1158 restoretimer = RESTORE_WPS_INSTANTLY;
1159 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1160 add_event(LCD_EVENT_ACTIVATION, false, wps_lcd_activation_hook);
1161 #endif
1162 /* we remove the update delay since it's not very usable in the wps,
1163 * e.g. during volume changing or ffwd/rewind */
1164 sb_skin_set_update_delay(0);
1165 FOR_NB_SCREENS(i)
1167 screens[i].stop_scroll();
1168 gui_wps_display(&gui_wps[i]);
1172 if (exit) {
1173 #ifdef HAVE_LCD_CHARCELLS
1174 status_set_record(false);
1175 status_set_audio(false);
1176 #endif
1177 if (global_settings.fade_on_stop)
1178 fade(false, true);
1180 if (bookmark)
1181 bookmark_autobookmark();
1182 audio_stop();
1183 #ifdef AB_REPEAT_ENABLE
1184 ab_reset_markers();
1185 #endif
1186 gwps_leave_wps();
1187 #ifdef HAVE_RECORDING
1188 if (button == ACTION_WPS_REC)
1189 return GO_TO_RECSCREEN;
1190 #endif
1191 if (global_settings.browse_current)
1192 return GO_TO_PREVIOUS_BROWSER;
1193 return GO_TO_PREVIOUS;
1196 if (button && !IS_SYSEVENT(button) )
1197 storage_spin();
1199 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1202 /* this is called from the playback thread so NO DRAWING! */
1203 static void track_changed_callback(void *param)
1205 wps_state.id3 = (struct mp3entry*)param;
1206 wps_state.nid3 = audio_next_track();
1207 if (wps_state.id3->cuesheet)
1209 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1210 cue_spoof_id3(wps_state.id3->cuesheet, wps_state.id3);
1212 wps_sync_data.do_full_update = true;
1214 static void nextid3available_callback(void* param)
1216 (void)param;
1217 wps_state.nid3 = audio_next_track();
1218 wps_sync_data.do_full_update = true;
1222 static void wps_state_init(void)
1224 wps_state.ff_rewind = false;
1225 wps_state.paused = false;
1226 if(audio_status() & AUDIO_STATUS_PLAY)
1228 wps_state.id3 = audio_current_track();
1229 wps_state.nid3 = audio_next_track();
1231 else
1233 wps_state.id3 = NULL;
1234 wps_state.nid3 = NULL;
1236 /* We'll be updating due to restore initialized with true */
1237 wps_sync_data.do_full_update = false;
1238 /* add the WPS track event callbacks */
1239 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1240 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1244 #ifdef HAVE_LCD_BITMAP
1245 static void statusbar_toggle_handler(void *data)
1247 (void)data;
1248 int i;
1250 int *wpsbars = &wps_sync_data.statusbars;
1251 *wpsbars = VP_SB_HIDE_ALL;
1252 FOR_NB_SCREENS(i)
1253 { /* fix viewports if needed */
1254 skin_statusbar_changed(&gui_wps[i]);
1256 bool draw = false;
1258 /* fix up gui_wps::statusbars, so that the viewportmanager accepts it*/
1259 if (gui_wps[i].data->wps_sb_tag)
1260 draw = gui_wps[i].data->show_sb_on_wps;
1261 else if (statusbar_position(i) != STATUSBAR_OFF)
1262 draw = true;
1263 if (draw)
1264 *wpsbars |=
1265 (VP_SB_ONSCREEN(i) | VP_SB_IGNORE_SETTING(i));
1268 #endif
1271 void gui_sync_wps_init(void)
1273 int i;
1274 FOR_NB_SCREENS(i)
1276 #ifdef HAVE_ALBUMART
1277 wps_datas[i].albumart = NULL;
1278 wps_datas[i].playback_aa_slot = -1;
1279 #endif
1280 gui_wps[i].data = &wps_datas[i];
1281 gui_wps[i].display = &screens[i];
1282 /* Currently no seperate wps_state needed/possible
1283 so use the only available ( "global" ) one */
1284 gui_wps[i].state = &wps_state;
1285 gui_wps[i].display->backdrop_unload(BACKDROP_SKIN_WPS);
1286 /* must point to the same struct for both screens */
1287 gui_wps[i].sync_data = &wps_sync_data;
1288 gui_wps[i].sync_data->statusbars = VP_SB_ALLSCREENS;
1290 #ifdef HAVE_LCD_BITMAP
1291 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggle_handler);
1292 #endif
1296 #ifdef IPOD_ACCESSORY_PROTOCOL
1297 bool is_wps_fading(void)
1299 return wps_state.is_fading;
1302 int wps_get_ff_rewind_count(void)
1304 return wps_state.ff_rewind_count;
1306 #endif