futile attempt to keep the ondioSP rombox working. This will almost certainly be...
[kugel-rb.git] / apps / gui / wps.c
blob3c0982270e0a871f342a750a4c3f9b5b65fce69f
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))
74 #define FF_REWIND_MAX_PERCENT 3 /* cap ff/rewind step size at max % of file */
75 /* 3% of 30min file == 54s step size */
76 #define MIN_FF_REWIND_STEP 500
78 /* currently only one wps_state is needed, initialize to 0 */
79 struct wps_state wps_state = { .id3 = NULL };
80 static struct gui_wps gui_wps[NB_SCREENS] = {{ .data = NULL }};
81 static struct wps_data wps_datas[NB_SCREENS] = {{ .wps_loaded = 0 }};
82 static struct wps_sync_data wps_sync_data = { .do_full_update = false };
84 /* initial setup of wps_data */
85 static void wps_state_init(void);
86 static void track_changed_callback(void *param);
87 static void nextid3available_callback(void* param);
89 #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps"
90 #ifdef HAVE_REMOTE_LCD
91 #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps"
92 #define DEFAULT_WPS(screen) ((screen) == SCREEN_MAIN ? \
93 WPS_DEFAULTCFG:RWPS_DEFAULTCFG)
94 #else
95 #define DEFAULT_WPS(screen) (WPS_DEFAULTCFG)
96 #endif
98 void wps_data_load(enum screen_type screen, const char *buf, bool isfile)
100 bool loaded_ok;
102 #if LCD_DEPTH > 1
103 screens[screen].backdrop_unload(BACKDROP_SKIN_WPS);
104 #endif
106 #ifndef __PCTOOL__
108 * Hardcode loading WPS_DEFAULTCFG to cause a reset ideally this
109 * wants to be a virtual file. Feel free to modify dirbrowse()
110 * if you're feeling brave.
113 if (buf && ! strcmp(buf, DEFAULT_WPS(screen)) )
115 #ifdef HAVE_REMOTE_LCD
116 if (screen == SCREEN_REMOTE)
117 global_settings.rwps_file[0] = '\0';
118 else
119 #endif
120 global_settings.wps_file[0] = '\0';
121 buf = NULL;
124 #endif /* __PCTOOL__ */
126 loaded_ok = buf && skin_data_load(screen, gui_wps[screen].data, buf, isfile);
128 if (!loaded_ok) /* load the hardcoded default */
130 char *skin_buf[NB_SCREENS] = {
131 #ifdef HAVE_LCD_BITMAP
132 "%s%?it<%?in<%in. |>%it|%fn>\n"
133 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
134 "%s%?id<%id|%?d1<%d1|(root)>> %?iy<(%iy)|>\n\n"
135 "%al%pc/%pt%ar[%pp:%pe]\n"
136 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
137 "%pb\n%pm\n",
138 #else
139 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d2> - %?id<%id|%d1>\n"
140 "%pc%?ps<*|/>%pt\n",
141 #endif
142 #ifdef HAVE_REMOTE_LCD
143 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
144 "%s%?it<%?in<%in. |>%it|%fn>\n"
145 "%al%pc/%pt%ar[%pp:%pe]\n"
146 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
147 "%pb\n",
148 #endif
150 skin_data_load(screen, gui_wps[screen].data, skin_buf[screen], false);
154 void fade(bool fade_in, bool updatewps)
156 int fp_global_vol = global_settings.volume << 8;
157 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
158 int fp_step = (fp_global_vol - fp_min_vol) / 30;
159 int i;
160 wps_state.is_fading = !fade_in;
161 if (fade_in) {
162 /* fade in */
163 int fp_volume = fp_min_vol;
165 /* zero out the sound */
166 sound_set_volume(fp_min_vol >> 8);
168 sleep(HZ/10); /* let audio thread run */
169 audio_resume();
171 while (fp_volume < fp_global_vol - fp_step) {
172 fp_volume += fp_step;
173 sound_set_volume(fp_volume >> 8);
174 if (updatewps)
176 FOR_NB_SCREENS(i)
177 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
179 sleep(1);
181 sound_set_volume(global_settings.volume);
183 else {
184 /* fade out */
185 int fp_volume = fp_global_vol;
187 while (fp_volume > fp_min_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 audio_pause();
198 wps_state.is_fading = false;
199 #if CONFIG_CODEC != SWCODEC
200 #ifndef SIMULATOR
201 /* let audio thread run and wait for the mas to run out of data */
202 while (!mp3_pause_done())
203 #endif
204 sleep(HZ/10);
205 #endif
207 /* reset volume to what it was before the fade */
208 sound_set_volume(global_settings.volume);
212 static bool update_onvol_change(struct gui_wps * gwps)
214 skin_update(gwps, WPS_REFRESH_NON_STATIC);
216 #ifdef HAVE_LCD_CHARCELLS
217 splashf(0, "Vol: %3d dB",
218 sound_val2phys(SOUND_VOLUME, global_settings.volume));
219 return true;
220 #endif
221 return false;
225 bool ffwd_rew(int button)
227 unsigned int step = 0; /* current ff/rewind step */
228 unsigned int max_step = 0; /* maximum ff/rewind step */
229 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
230 int direction = -1; /* forward=1 or backward=-1 */
231 bool exit = false;
232 bool usb = false;
233 int i = 0;
234 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
236 if (button == ACTION_NONE)
238 status_set_ffmode(0);
239 return usb;
241 while (!exit)
243 switch ( button )
245 case ACTION_WPS_SEEKFWD:
246 direction = 1;
247 case ACTION_WPS_SEEKBACK:
248 if (wps_state.ff_rewind)
250 if (direction == 1)
252 /* fast forwarding, calc max step relative to end */
253 max_step = (wps_state.id3->length -
254 (wps_state.id3->elapsed +
255 ff_rewind_count)) *
256 FF_REWIND_MAX_PERCENT / 100;
258 else
260 /* rewinding, calc max step relative to start */
261 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
262 FF_REWIND_MAX_PERCENT / 100;
265 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
267 if (step > max_step)
268 step = max_step;
270 ff_rewind_count += step * direction;
272 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
273 step += step >> ff_rw_accel;
275 else
277 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
278 wps_state.id3 && wps_state.id3->length )
280 if (!wps_state.paused)
281 #if (CONFIG_CODEC == SWCODEC)
282 audio_pre_ff_rewind();
283 #else
284 audio_pause();
285 #endif
286 #if CONFIG_KEYPAD == PLAYER_PAD
287 FOR_NB_SCREENS(i)
288 gui_wps[i].display->stop_scroll();
289 #endif
290 if (direction > 0)
291 status_set_ffmode(STATUS_FASTFORWARD);
292 else
293 status_set_ffmode(STATUS_FASTBACKWARD);
295 wps_state.ff_rewind = true;
297 step = 1000 * global_settings.ff_rewind_min_step;
299 else
300 break;
303 if (direction > 0) {
304 if ((wps_state.id3->elapsed + ff_rewind_count) >
305 wps_state.id3->length)
306 ff_rewind_count = wps_state.id3->length -
307 wps_state.id3->elapsed;
309 else {
310 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
311 ff_rewind_count = -wps_state.id3->elapsed;
314 /* set the wps state ff_rewind_count so the progess info
315 displays corectly */
316 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
317 ff_rewind_count:-ff_rewind_count;
318 FOR_NB_SCREENS(i)
320 skin_update(&gui_wps[i],
321 WPS_REFRESH_PLAYER_PROGRESS |
322 WPS_REFRESH_DYNAMIC);
325 break;
327 case ACTION_WPS_STOPSEEK:
328 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
329 audio_ff_rewind(wps_state.id3->elapsed);
330 wps_state.ff_rewind_count = 0;
331 wps_state.ff_rewind = false;
332 status_set_ffmode(0);
333 #if (CONFIG_CODEC != SWCODEC)
334 if (!wps_state.paused)
335 audio_resume();
336 #endif
337 #ifdef HAVE_LCD_CHARCELLS
338 FOR_NB_SCREENS(i)
339 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
340 #endif
341 exit = true;
342 break;
344 default:
345 if(default_event_handler(button) == SYS_USB_CONNECTED) {
346 status_set_ffmode(0);
347 usb = true;
348 exit = true;
350 break;
352 if (!exit)
354 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
355 #ifdef HAVE_TOUCHSCREEN
356 if (button == ACTION_TOUCHSCREEN)
357 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
358 #endif
361 return usb;
365 void display_keylock_text(bool locked)
367 int i;
368 FOR_NB_SCREENS(i)
369 gui_wps[i].display->stop_scroll();
371 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
377 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
378 static void gwps_caption_backlight(struct wps_state *state)
380 if (state && state->id3)
382 #ifdef HAVE_BACKLIGHT
383 if (global_settings.caption_backlight)
385 /* turn on backlight n seconds before track ends, and turn it off n
386 seconds into the new track. n == backlight_timeout, or 5s */
387 int n = global_settings.backlight_timeout * 1000;
389 if ( n < 1000 )
390 n = 5000; /* use 5s if backlight is always on or off */
392 if (((state->id3->elapsed < 1000) ||
393 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
394 (state->paused == false))
395 backlight_on();
397 #endif
398 #ifdef HAVE_REMOTE_LCD
399 if (global_settings.remote_caption_backlight)
401 /* turn on remote backlight n seconds before track ends, and turn it
402 off n seconds into the new track. n == remote_backlight_timeout,
403 or 5s */
404 int n = global_settings.remote_backlight_timeout * 1000;
406 if ( n < 1000 )
407 n = 5000; /* use 5s if backlight is always on or off */
409 if (((state->id3->elapsed < 1000) ||
410 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
411 (state->paused == false))
412 remote_backlight_on();
414 #endif
417 #endif
420 static void change_dir(int direction)
422 if (global_settings.prevent_skip)
423 return;
425 if (direction < 0)
426 audio_prev_dir();
427 else if (direction > 0)
428 audio_next_dir();
429 /* prevent the next dir to immediatly start being ffw'd */
430 action_wait_for_release();
433 static void prev_track(unsigned long skip_thresh)
435 if (wps_state.id3->elapsed < skip_thresh)
437 audio_prev();
438 return;
440 else
442 if (wps_state.id3->cuesheet)
444 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
445 return;
448 if (!wps_state.paused)
449 #if (CONFIG_CODEC == SWCODEC)
450 audio_pre_ff_rewind();
451 #else
452 audio_pause();
453 #endif
455 audio_ff_rewind(0);
457 #if (CONFIG_CODEC != SWCODEC)
458 if (!wps_state.paused)
459 audio_resume();
460 #endif
464 static void next_track(void)
466 /* take care of if we're playing a cuesheet */
467 if (wps_state.id3->cuesheet)
469 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
471 /* if the result was false, then we really want
472 to skip to the next track */
473 return;
477 audio_next();
480 static void play_hop(int direction)
482 long step = global_settings.skip_length*1000;
483 long elapsed = wps_state.id3->elapsed;
484 long remaining = wps_state.id3->length - elapsed;
486 if (step < 0)
488 if (direction < 0)
490 prev_track(DEFAULT_SKIP_TRESH);
491 return;
493 else if (remaining < DEFAULT_SKIP_TRESH*2)
495 next_track();
496 return;
498 else
499 elapsed += (remaining - DEFAULT_SKIP_TRESH*2);
501 else if (!global_settings.prevent_skip &&
502 (!step ||
503 (direction > 0 && step >= remaining) ||
504 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
505 { /* Do normal track skipping */
506 if (direction > 0)
507 next_track();
508 else if (direction < 0)
509 prev_track(DEFAULT_SKIP_TRESH);
510 return;
512 else if (direction == 1 && step >= remaining)
514 #if CONFIG_CODEC == SWCODEC
515 if(global_settings.beep)
516 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
517 #endif
518 return;
520 else if ((direction == -1 && elapsed < step))
522 elapsed = 0;
524 else
526 elapsed += step * direction;
528 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
530 #if (CONFIG_CODEC == SWCODEC)
531 audio_pre_ff_rewind();
532 #else
533 audio_pause();
534 #endif
536 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
537 #if (CONFIG_CODEC != SWCODEC)
538 if (!wps_state.paused)
539 audio_resume();
540 #endif
544 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
546 * If the user is unable to see the wps, because the display is deactivated,
547 * we suppress updates until the wps is activated again (the lcd driver will
548 * call this hook to issue an instant update)
549 * */
550 static void wps_lcd_activation_hook(void *param)
552 (void)param;
553 wps_sync_data.do_full_update = true;
554 /* force timeout in wps main loop, so that the update is instantly */
555 queue_post(&button_queue, BUTTON_NONE, 0);
557 #endif
559 static void gwps_leave_wps(void)
561 int i;
563 FOR_NB_SCREENS(i)
565 gui_wps[i].display->stop_scroll();
566 #if LCD_DEPTH > 1
567 gui_wps[i].display->backdrop_show(BACKDROP_MAIN);
568 #endif
570 #ifdef HAVE_LCD_BITMAP
571 bool draw = false;
572 if (gui_wps[i].data->wps_sb_tag)
573 draw = gui_wps[i].data->show_sb_on_wps;
574 else if (statusbar_position(i) != STATUSBAR_OFF)
575 draw = true;
576 #endif
577 viewportmanager_theme_undo(i, draw);
581 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
582 /* Play safe and unregister the hook */
583 remove_event(LCD_EVENT_ACTIVATION, wps_lcd_activation_hook);
584 #endif
585 /* unhandle statusbar update delay */
586 sb_skin_set_update_delay(DEFAULT_UPDATE_DELAY);
590 * display the wps on entering or restoring */
591 static void gwps_enter_wps(void)
593 int i;
594 FOR_NB_SCREENS(i)
596 struct gui_wps *gwps = &gui_wps[i];
597 struct screen *display = gwps->display;
598 #ifdef HAVE_LCD_BITMAP
599 bool draw = false;
600 if (gui_wps[i].data->wps_sb_tag)
601 draw = gui_wps[i].data->show_sb_on_wps;
602 else if (statusbar_position(i) != STATUSBAR_OFF)
603 draw = true;
604 #endif
605 viewportmanager_theme_enable(i, draw, NULL);
607 display->stop_scroll();
608 /* Update the values in the first (default) viewport - in case the user
609 has modified the statusbar or colour settings */
610 #if LCD_DEPTH > 1
611 if (display->depth > 1)
613 struct viewport *vp = &find_viewport(VP_DEFAULT_LABEL, gwps->data)->vp;
614 vp->fg_pattern = display->get_foreground();
615 vp->bg_pattern = display->get_background();
617 display->backdrop_show(BACKDROP_SKIN_WPS);
618 #endif
619 /* make the backdrop actually take effect */
620 display->clear_display();
621 skin_update(gwps, WPS_REFRESH_ALL);
623 /* force statusbar/skin update since we just cleared the whole screen */
624 send_event(GUI_EVENT_ACTIONUPDATE, (void*)1);
627 #ifdef HAVE_TOUCHSCREEN
628 int wps_get_touchaction(struct wps_data *data)
630 short x,y;
631 short vx, vy;
632 int type = action_get_touchscreen_press(&x, &y);
633 static int last_action = ACTION_NONE;
634 struct touchregion *r;
635 bool repeated = (type == BUTTON_REPEAT);
636 bool released = (type == BUTTON_REL);
637 struct skin_token_list *regions = data->touchregions;
638 while (regions)
640 r = (struct touchregion *)regions->token->value.data;
641 /* make sure this region's viewport is visible */
642 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
644 regions = regions->next;
645 continue;
647 /* check if it's inside this viewport */
648 if (viewport_point_within_vp(&(r->wvp->vp), x, y))
649 { /* reposition the touch inside the viewport since touchregions
650 * are relative to a preceding viewport */
651 vx = x - r->wvp->vp.x;
652 vy = y - r->wvp->vp.y;
653 /* now see if the point is inside this region */
654 if (vx >= r->x && vx < r->x+r->width &&
655 vy >= r->y && vy < r->y+r->height)
657 /* reposition the touch within the area */
658 vx -= r->x;
659 vy -= r->y;
661 switch(r->type)
663 case WPS_TOUCHREGION_ACTION:
664 if ((repeated && r->repeat) || (released && !r->repeat))
666 last_action = r->action;
667 return r->action;
669 break;
670 case WPS_TOUCHREGION_SCROLLBAR:
671 if(r->width > r->height)
672 /* landscape */
673 wps_state.id3->elapsed = (vx *
674 wps_state.id3->length) / r->width;
675 else
676 /* portrait */
677 wps_state.id3->elapsed = (vy *
678 wps_state.id3->length) / r->height;
680 if (!wps_state.paused)
681 #if (CONFIG_CODEC == SWCODEC)
682 audio_pre_ff_rewind();
683 #else
684 audio_pause();
685 #endif
686 audio_ff_rewind(wps_state.id3->elapsed);
687 #if (CONFIG_CODEC != SWCODEC)
688 if (!wps_state.paused)
689 audio_resume();
690 #endif
691 break;
692 case WPS_TOUCHREGION_VOLUME:
694 const int min_vol = sound_min(SOUND_VOLUME);
695 const int max_vol = sound_max(SOUND_VOLUME);
696 if(r->width > r->height)
697 /* landscape */
698 global_settings.volume = (vx *
699 (max_vol - min_vol)) / r->width;
700 else
701 /* portrait */
702 global_settings.volume = (vy *
703 (max_vol-min_vol)) / r->height;
705 global_settings.volume += min_vol;
706 setvol();
707 return ACTION_REDRAW;
712 regions = regions->next;
715 if ((last_action == ACTION_WPS_SEEKBACK || last_action == ACTION_WPS_SEEKFWD))
716 return ACTION_WPS_STOPSEEK;
717 last_action = ACTION_TOUCHSCREEN;
718 return ACTION_TOUCHSCREEN;
720 #endif
721 /* The WPS can be left in two ways:
722 * a) call a function, which draws over the wps. In this case, the wps
723 * will be still active (i.e. the below function didn't return)
724 * b) return with a value evaluated by root_menu.c, in this case the wps
725 * is really left, and root_menu will handle the next screen
727 * In either way, call gwps_leave_wps(), in order to restore the correct
728 * "main screen" backdrops and statusbars
730 long gui_wps_show(void)
732 long button = 0;
733 bool restore = true;
734 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
735 bool exit = false;
736 bool bookmark = false;
737 bool update = false;
738 bool vol_changed = false;
739 int i;
740 long last_left = 0, last_right = 0;
742 #ifdef HAVE_LCD_CHARCELLS
743 status_set_audio(true);
744 status_set_param(false);
745 #endif
747 #ifdef AB_REPEAT_ENABLE
748 ab_repeat_init();
749 ab_reset_markers();
750 #endif
751 wps_state_init();
753 while ( 1 )
755 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
757 /* did someone else (i.e power thread) change audio pause mode? */
758 if (wps_state.paused != audio_paused) {
759 wps_state.paused = audio_paused;
761 /* if another thread paused audio, we are probably in car mode,
762 about to shut down. lets save the settings. */
763 if (wps_state.paused) {
764 settings_save();
765 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
766 call_storage_idle_notifys(true);
767 #endif
770 #ifdef HAVE_LCD_BITMAP
771 /* when the peak meter is enabled we want to have a
772 few extra updates to make it look smooth. On the
773 other hand we don't want to waste energy if it
774 isn't displayed */
775 bool pm=false;
776 FOR_NB_SCREENS(i)
778 if(gui_wps[i].data->peak_meter_enabled)
779 pm = true;
782 if (pm) {
783 long next_refresh = current_tick;
784 long next_big_refresh = current_tick + HZ / 5;
785 button = BUTTON_NONE;
786 while (TIME_BEFORE(current_tick, next_big_refresh)) {
787 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_NOBLOCK);
788 /* check for restore to not let the peakmeter delay the
789 * initial draw of the wps, don't delay handling of button
790 * presses either */
791 if (button != ACTION_NONE || restore) {
792 break;
794 peak_meter_peek();
795 sleep(0); /* Sleep until end of current tick. */
797 if (TIME_AFTER(current_tick, next_refresh)) {
798 FOR_NB_SCREENS(i)
800 if(gui_wps[i].data->peak_meter_enabled)
801 skin_update(&gui_wps[i], WPS_REFRESH_PEAK_METER);
802 next_refresh += HZ / PEAK_METER_FPS;
809 /* The peak meter is disabled
810 -> no additional screen updates needed */
811 else
812 #endif
813 { /* 1 is the minimum timeout which lets other threads run.
814 * audio thread (apprently) needs to run before displaying the wps
815 * or bad things happen with regards to cuesheet
816 * (probably a race condition, on sh at least) */
817 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,
818 restore ? 1 : HZ/5);
821 /* Exit if audio has stopped playing. This happens e.g. at end of
822 playlist or if using the sleep timer. */
823 if (!(audio_status() & AUDIO_STATUS_PLAY))
824 exit = true;
825 #ifdef HAVE_TOUCHSCREEN
826 if (button == ACTION_TOUCHSCREEN)
827 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
828 #endif
829 /* The iPods/X5/M5 use a single button for the A-B mode markers,
830 defined as ACTION_WPSAB_SINGLE in their config files. */
831 #ifdef ACTION_WPSAB_SINGLE
832 if (!global_settings.party_mode && ab_repeat_mode_enabled())
834 static int wps_ab_state = 0;
835 if (button == ACTION_WPSAB_SINGLE)
837 switch (wps_ab_state)
839 case 0: /* set the A spot */
840 button = ACTION_WPS_ABSETA_PREVDIR;
841 break;
842 case 1: /* set the B spot */
843 button = ACTION_WPS_ABSETB_NEXTDIR;
844 break;
845 case 2:
846 button = ACTION_WPS_ABRESET;
847 break;
849 wps_ab_state = (wps_ab_state+1) % 3;
852 #endif
853 switch(button)
855 case ACTION_WPS_CONTEXT:
857 gwps_leave_wps();
858 /* if music is stopped in the context menu we want to exit the wps */
859 if (onplay(wps_state.id3->path,
860 FILE_ATTR_AUDIO, CONTEXT_WPS) == ONPLAY_MAINMENU
861 || !audio_status())
862 return GO_TO_ROOT;
863 restore = true;
865 break;
867 case ACTION_WPS_BROWSE:
868 #ifdef HAVE_LCD_CHARCELLS
869 status_set_record(false);
870 status_set_audio(false);
871 #endif
872 gwps_leave_wps();
873 return GO_TO_PREVIOUS_BROWSER;
874 break;
876 /* play/pause */
877 case ACTION_WPS_PLAY:
878 if (global_settings.party_mode)
879 break;
880 if ( wps_state.paused )
882 wps_state.paused = false;
883 if ( global_settings.fade_on_stop )
884 fade(true, true);
885 else
886 audio_resume();
888 else
890 wps_state.paused = true;
891 if ( global_settings.fade_on_stop )
892 fade(false, true);
893 else
894 audio_pause();
895 settings_save();
896 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
897 call_storage_idle_notifys(true); /* make sure resume info is saved */
898 #endif
900 break;
902 case ACTION_WPS_VOLUP:
903 global_settings.volume++;
904 vol_changed = true;
905 break;
906 case ACTION_WPS_VOLDOWN:
907 global_settings.volume--;
908 vol_changed = true;
909 break;
910 /* fast forward
911 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
912 case ACTION_WPS_SEEKFWD:
913 if (global_settings.party_mode)
914 break;
915 if (current_tick -last_right < HZ)
917 if (wps_state.id3->cuesheet)
919 audio_next();
921 else
923 change_dir(1);
926 else
927 ffwd_rew(ACTION_WPS_SEEKFWD);
928 last_right = last_left = 0;
929 break;
930 /* fast rewind
931 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
932 case ACTION_WPS_SEEKBACK:
933 if (global_settings.party_mode)
934 break;
935 if (current_tick -last_left < HZ)
937 if (wps_state.id3->cuesheet)
939 if (!wps_state.paused)
940 #if (CONFIG_CODEC == SWCODEC)
941 audio_pre_ff_rewind();
942 #else
943 audio_pause();
944 #endif
945 audio_ff_rewind(0);
947 else
949 change_dir(-1);
952 else
953 ffwd_rew(ACTION_WPS_SEEKBACK);
954 last_left = last_right = 0;
955 break;
957 /* prev / restart */
958 case ACTION_WPS_SKIPPREV:
959 if (global_settings.party_mode)
960 break;
961 last_left = current_tick;
962 #ifdef AB_REPEAT_ENABLE
963 /* if we're in A/B repeat mode and the current position
964 is past the A marker, jump back to the A marker... */
965 if ( ab_repeat_mode_enabled() )
967 if ( ab_after_A_marker(wps_state.id3->elapsed) )
969 ab_jump_to_A_marker();
970 break;
971 #if (AB_REPEAT_ENABLE == 2)
972 } else {
973 ab_reset_markers();
974 #endif
977 else
978 /* ...otherwise, do it normally */
979 #endif
980 play_hop(-1);
981 break;
983 /* next
984 OR if skip length set, hop by predetermined amount. */
985 case ACTION_WPS_SKIPNEXT:
986 if (global_settings.party_mode)
987 break;
988 last_right = current_tick;
989 #ifdef AB_REPEAT_ENABLE
990 /* if we're in A/B repeat mode and the current position is
991 before the A marker, jump to the A marker... */
992 if ( ab_repeat_mode_enabled() )
994 if ( ab_before_A_marker(wps_state.id3->elapsed) )
996 ab_jump_to_A_marker();
997 break;
998 #if (AB_REPEAT_ENABLE == 2)
999 } else {
1000 ab_reset_markers();
1001 #endif
1004 else
1005 /* ...otherwise, do it normally */
1006 #endif
1007 play_hop(1);
1008 break;
1009 /* next / prev directories */
1010 /* and set A-B markers if in a-b mode */
1011 case ACTION_WPS_ABSETB_NEXTDIR:
1012 if (global_settings.party_mode)
1013 break;
1014 #if defined(AB_REPEAT_ENABLE)
1015 if (ab_repeat_mode_enabled())
1017 ab_set_B_marker(wps_state.id3->elapsed);
1018 ab_jump_to_A_marker();
1020 else
1021 #endif
1023 change_dir(1);
1025 break;
1026 case ACTION_WPS_ABSETA_PREVDIR:
1027 if (global_settings.party_mode)
1028 break;
1029 #if defined(AB_REPEAT_ENABLE)
1030 if (ab_repeat_mode_enabled())
1031 ab_set_A_marker(wps_state.id3->elapsed);
1032 else
1033 #endif
1035 change_dir(-1);
1037 break;
1038 /* menu key functions */
1039 case ACTION_WPS_MENU:
1040 gwps_leave_wps();
1041 return GO_TO_ROOT;
1042 break;
1045 #ifdef HAVE_QUICKSCREEN
1046 case ACTION_WPS_QUICKSCREEN:
1048 gwps_leave_wps();
1049 if (quick_screen_quick(button))
1050 return GO_TO_ROOT;
1051 restore = true;
1053 break;
1054 #endif /* HAVE_QUICKSCREEN */
1056 /* screen settings */
1057 #ifdef BUTTON_F3
1058 case ACTION_F3:
1060 gwps_leave_wps();
1061 if (quick_screen_f3(BUTTON_F3))
1062 return GO_TO_ROOT;
1063 restore = true;
1065 break;
1066 #endif /* BUTTON_F3 */
1068 /* pitch screen */
1069 #ifdef HAVE_PITCHSCREEN
1070 case ACTION_WPS_PITCHSCREEN:
1072 gwps_leave_wps();
1073 if (1 == gui_syncpitchscreen_run())
1074 return GO_TO_ROOT;
1075 restore = true;
1077 break;
1078 #endif /* HAVE_PITCHSCREEN */
1080 #ifdef AB_REPEAT_ENABLE
1081 /* reset A&B markers */
1082 case ACTION_WPS_ABRESET:
1083 if (ab_repeat_mode_enabled())
1085 ab_reset_markers();
1086 update = true;
1088 break;
1089 #endif /* AB_REPEAT_ENABLE */
1091 /* stop and exit wps */
1092 case ACTION_WPS_STOP:
1093 if (global_settings.party_mode)
1094 break;
1095 bookmark = true;
1096 exit = true;
1097 break;
1099 case ACTION_WPS_ID3SCREEN:
1101 gwps_leave_wps();
1102 if (browse_id3())
1103 return GO_TO_ROOT;
1104 restore = true;
1106 break;
1107 #ifdef HAVE_TOUCHSCREEN
1108 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1110 global_settings.playlist_shuffle =
1111 !global_settings.playlist_shuffle;
1112 #if CONFIG_CODEC == SWCODEC
1113 dsp_set_replaygain();
1114 #endif
1115 if (global_settings.playlist_shuffle)
1116 playlist_randomise(NULL, current_tick, true);
1117 else
1118 playlist_sort(NULL, true);
1120 break;
1121 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1123 const struct settings_list *rep_setting =
1124 find_setting(&global_settings.repeat_mode, NULL);
1125 option_select_next_val(rep_setting, false, true);
1126 audio_flush_and_reload_tracks();
1128 break;
1129 #endif /* HAVE_TOUCHSCREEN */
1130 /* this case is used by the softlock feature
1131 * it requests a full update here */
1132 case ACTION_REDRAW:
1133 wps_sync_data.do_full_update = true;
1134 break;
1135 case ACTION_NONE: /* Timeout, do a partial update */
1136 update = true;
1137 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1138 break;
1139 #ifdef HAVE_RECORDING
1140 case ACTION_WPS_REC:
1141 exit = true;
1142 break;
1143 #endif
1144 case SYS_POWEROFF:
1145 default_event_handler(SYS_POWEROFF);
1146 break;
1147 case ACTION_WPS_VIEW_PLAYLIST:
1148 gwps_leave_wps();
1149 if (playlist_viewer()) /* true if USB connected */
1150 return GO_TO_ROOT;
1151 restore = true;
1152 break;
1153 default:
1154 if(default_event_handler(button) == SYS_USB_CONNECTED)
1156 gwps_leave_wps();
1157 return GO_TO_ROOT;
1159 update = true;
1160 break;
1163 if (vol_changed)
1165 bool res = false;
1166 vol_changed = false;
1167 setvol();
1168 FOR_NB_SCREENS(i)
1170 if(update_onvol_change(&gui_wps[i]))
1171 res = true;
1173 if (res) {
1174 restore = true;
1175 restoretimer = RESTORE_WPS_NEXT_SECOND;
1180 if (restore &&
1181 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1182 TIME_AFTER(current_tick, restoretimer)))
1184 restore = false;
1185 restoretimer = RESTORE_WPS_INSTANTLY;
1186 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1187 add_event(LCD_EVENT_ACTIVATION, false, wps_lcd_activation_hook);
1188 #endif
1189 /* we remove the update delay since it's not very usable in the wps,
1190 * e.g. during volume changing or ffwd/rewind */
1191 sb_skin_set_update_delay(0);
1192 wps_sync_data.do_full_update = update = false;
1193 gwps_enter_wps();
1195 else if (wps_sync_data.do_full_update || update)
1197 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1198 gwps_caption_backlight(&wps_state);
1199 #endif
1200 FOR_NB_SCREENS(i)
1202 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1203 /* currently, all remotes are readable without backlight
1204 * so still update those */
1205 if (lcd_active() || (i != SCREEN_MAIN))
1206 #endif
1208 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
1211 wps_sync_data.do_full_update = false;
1212 update = false;
1215 if (exit) {
1216 #ifdef HAVE_LCD_CHARCELLS
1217 status_set_record(false);
1218 status_set_audio(false);
1219 #endif
1220 if (global_settings.fade_on_stop)
1221 fade(false, true);
1223 if (bookmark)
1224 bookmark_autobookmark();
1225 audio_stop();
1226 #ifdef AB_REPEAT_ENABLE
1227 ab_reset_markers();
1228 #endif
1229 gwps_leave_wps();
1230 #ifdef HAVE_RECORDING
1231 if (button == ACTION_WPS_REC)
1232 return GO_TO_RECSCREEN;
1233 #endif
1234 if (global_settings.browse_current)
1235 return GO_TO_PREVIOUS_BROWSER;
1236 return GO_TO_PREVIOUS;
1239 if (button && !IS_SYSEVENT(button) )
1240 storage_spin();
1242 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1245 /* this is called from the playback thread so NO DRAWING! */
1246 static void track_changed_callback(void *param)
1248 wps_state.id3 = (struct mp3entry*)param;
1249 wps_state.nid3 = audio_next_track();
1250 if (wps_state.id3->cuesheet)
1252 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1253 cue_spoof_id3(wps_state.id3->cuesheet, wps_state.id3);
1255 wps_sync_data.do_full_update = true;
1257 static void nextid3available_callback(void* param)
1259 (void)param;
1260 wps_state.nid3 = audio_next_track();
1261 wps_sync_data.do_full_update = true;
1265 static void wps_state_init(void)
1267 wps_state.ff_rewind = false;
1268 wps_state.paused = false;
1269 if(audio_status() & AUDIO_STATUS_PLAY)
1271 wps_state.id3 = audio_current_track();
1272 wps_state.nid3 = audio_next_track();
1274 else
1276 wps_state.id3 = NULL;
1277 wps_state.nid3 = NULL;
1279 /* We'll be updating due to restore initialized with true */
1280 wps_sync_data.do_full_update = false;
1281 /* add the WPS track event callbacks */
1282 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1283 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1287 void gui_sync_wps_init(void)
1289 int i;
1290 FOR_NB_SCREENS(i)
1292 #ifdef HAVE_ALBUMART
1293 wps_datas[i].albumart = NULL;
1294 wps_datas[i].playback_aa_slot = -1;
1295 #endif
1296 gui_wps[i].data = &wps_datas[i];
1297 gui_wps[i].display = &screens[i];
1298 /* Currently no seperate wps_state needed/possible
1299 so use the only available ( "global" ) one */
1300 gui_wps[i].state = &wps_state;
1301 #if LCD_DEPTH > 1
1302 gui_wps[i].display->backdrop_unload(BACKDROP_SKIN_WPS);
1303 #endif
1304 /* must point to the same struct for both screens */
1305 gui_wps[i].sync_data = &wps_sync_data;
1310 #ifdef IPOD_ACCESSORY_PROTOCOL
1311 bool is_wps_fading(void)
1313 return wps_state.is_fading;
1316 int wps_get_ff_rewind_count(void)
1318 return wps_state.ff_rewind_count;
1320 #endif