NULL check so it doesnt display (null) on the comment line
[kugel-rb.git] / apps / gui / wps.c
blobfb703062cdfe2ebd4c6a03184a5042abbf4b370e
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 #ifndef __PCTOOL__
104 * Hardcode loading WPS_DEFAULTCFG to cause a reset ideally this
105 * wants to be a virtual file. Feel free to modify dirbrowse()
106 * if you're feeling brave.
109 if (buf && ! strcmp(buf, DEFAULT_WPS(screen)) )
111 #ifdef HAVE_REMOTE_LCD
112 if (screen == SCREEN_REMOTE)
113 global_settings.rwps_file[0] = '\0';
114 else
115 #endif
116 global_settings.wps_file[0] = '\0';
117 buf = NULL;
120 #endif /* __PCTOOL__ */
122 loaded_ok = buf && skin_data_load(screen, gui_wps[screen].data, buf, isfile);
124 if (!loaded_ok) /* load the hardcoded default */
126 char *skin_buf[NB_SCREENS] = {
127 #ifdef HAVE_LCD_BITMAP
128 #if LCD_DEPTH > 1
129 "%Xd\n"
130 #endif
131 "%s%?it<%?in<%in. |>%it|%fn>\n"
132 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
133 "%s%?id<%id|%?d1<%d1|(root)>> %?iy<(%iy)|>\n\n"
134 "%al%pc/%pt%ar[%pp:%pe]\n"
135 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
136 "%pb\n%pm\n",
137 #else
138 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d2> - %?id<%id|%d1>\n"
139 "%pc%?ps<*|/>%pt\n",
140 #endif
141 #ifdef HAVE_REMOTE_LCD
142 #if LCD_REMOTE_DEPTH > 1
143 "%Xd\n"
144 #endif
145 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
146 "%s%?it<%?in<%in. |>%it|%fn>\n"
147 "%al%pc/%pt%ar[%pp:%pe]\n"
148 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
149 "%pb\n",
150 #endif
152 skin_data_load(screen, gui_wps[screen].data, skin_buf[screen], false);
156 void fade(bool fade_in, bool updatewps)
158 int fp_global_vol = global_settings.volume << 8;
159 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
160 int fp_step = (fp_global_vol - fp_min_vol) / 30;
161 int i;
162 wps_state.is_fading = !fade_in;
163 if (fade_in) {
164 /* fade in */
165 int fp_volume = fp_min_vol;
167 /* zero out the sound */
168 sound_set_volume(fp_min_vol >> 8);
170 sleep(HZ/10); /* let audio thread run */
171 audio_resume();
173 while (fp_volume < fp_global_vol - fp_step) {
174 fp_volume += fp_step;
175 sound_set_volume(fp_volume >> 8);
176 if (updatewps)
178 FOR_NB_SCREENS(i)
179 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
181 sleep(1);
183 sound_set_volume(global_settings.volume);
185 else {
186 /* fade out */
187 int fp_volume = fp_global_vol;
189 while (fp_volume > fp_min_vol + fp_step) {
190 fp_volume -= fp_step;
191 sound_set_volume(fp_volume >> 8);
192 if (updatewps)
194 FOR_NB_SCREENS(i)
195 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
197 sleep(1);
199 audio_pause();
200 wps_state.is_fading = false;
201 #if CONFIG_CODEC != SWCODEC
202 #ifndef SIMULATOR
203 /* let audio thread run and wait for the mas to run out of data */
204 while (!mp3_pause_done())
205 #endif
206 sleep(HZ/10);
207 #endif
209 /* reset volume to what it was before the fade */
210 sound_set_volume(global_settings.volume);
214 static bool update_onvol_change(struct gui_wps * gwps)
216 skin_update(gwps, WPS_REFRESH_NON_STATIC);
218 #ifdef HAVE_LCD_CHARCELLS
219 splashf(0, "Vol: %3d dB",
220 sound_val2phys(SOUND_VOLUME, global_settings.volume));
221 return true;
222 #endif
223 return false;
227 bool ffwd_rew(int button)
229 unsigned int step = 0; /* current ff/rewind step */
230 unsigned int max_step = 0; /* maximum ff/rewind step */
231 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
232 int direction = -1; /* forward=1 or backward=-1 */
233 bool exit = false;
234 bool usb = false;
235 int i = 0;
236 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
238 if (button == ACTION_NONE)
240 status_set_ffmode(0);
241 return usb;
243 while (!exit)
245 switch ( button )
247 case ACTION_WPS_SEEKFWD:
248 direction = 1;
249 case ACTION_WPS_SEEKBACK:
250 if (wps_state.ff_rewind)
252 if (direction == 1)
254 /* fast forwarding, calc max step relative to end */
255 max_step = (wps_state.id3->length -
256 (wps_state.id3->elapsed +
257 ff_rewind_count)) *
258 FF_REWIND_MAX_PERCENT / 100;
260 else
262 /* rewinding, calc max step relative to start */
263 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
264 FF_REWIND_MAX_PERCENT / 100;
267 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
269 if (step > max_step)
270 step = max_step;
272 ff_rewind_count += step * direction;
274 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
275 step += step >> ff_rw_accel;
277 else
279 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
280 wps_state.id3 && wps_state.id3->length )
282 if (!wps_state.paused)
283 #if (CONFIG_CODEC == SWCODEC)
284 audio_pre_ff_rewind();
285 #else
286 audio_pause();
287 #endif
288 #if CONFIG_KEYPAD == PLAYER_PAD
289 FOR_NB_SCREENS(i)
290 gui_wps[i].display->stop_scroll();
291 #endif
292 if (direction > 0)
293 status_set_ffmode(STATUS_FASTFORWARD);
294 else
295 status_set_ffmode(STATUS_FASTBACKWARD);
297 wps_state.ff_rewind = true;
299 step = 1000 * global_settings.ff_rewind_min_step;
301 else
302 break;
305 if (direction > 0) {
306 if ((wps_state.id3->elapsed + ff_rewind_count) >
307 wps_state.id3->length)
308 ff_rewind_count = wps_state.id3->length -
309 wps_state.id3->elapsed;
311 else {
312 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
313 ff_rewind_count = -wps_state.id3->elapsed;
316 /* set the wps state ff_rewind_count so the progess info
317 displays corectly */
318 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
319 ff_rewind_count:-ff_rewind_count;
320 FOR_NB_SCREENS(i)
322 skin_update(&gui_wps[i],
323 WPS_REFRESH_PLAYER_PROGRESS |
324 WPS_REFRESH_DYNAMIC);
327 break;
329 case ACTION_WPS_STOPSEEK:
330 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
331 audio_ff_rewind(wps_state.id3->elapsed);
332 wps_state.ff_rewind_count = 0;
333 wps_state.ff_rewind = false;
334 status_set_ffmode(0);
335 #if (CONFIG_CODEC != SWCODEC)
336 if (!wps_state.paused)
337 audio_resume();
338 #endif
339 #ifdef HAVE_LCD_CHARCELLS
340 FOR_NB_SCREENS(i)
341 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
342 #endif
343 exit = true;
344 break;
346 default:
347 if(default_event_handler(button) == SYS_USB_CONNECTED) {
348 status_set_ffmode(0);
349 usb = true;
350 exit = true;
352 break;
354 if (!exit)
356 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
357 #ifdef HAVE_TOUCHSCREEN
358 if (button == ACTION_TOUCHSCREEN)
359 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
360 #endif
363 return usb;
367 void display_keylock_text(bool locked)
369 int i;
370 FOR_NB_SCREENS(i)
371 gui_wps[i].display->stop_scroll();
373 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
379 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
380 static void gwps_caption_backlight(struct wps_state *state)
382 if (state && state->id3)
384 #ifdef HAVE_BACKLIGHT
385 if (global_settings.caption_backlight)
387 /* turn on backlight n seconds before track ends, and turn it off n
388 seconds into the new track. n == backlight_timeout, or 5s */
389 int n = global_settings.backlight_timeout * 1000;
391 if ( n < 1000 )
392 n = 5000; /* use 5s if backlight is always on or off */
394 if (((state->id3->elapsed < 1000) ||
395 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
396 (state->paused == false))
397 backlight_on();
399 #endif
400 #ifdef HAVE_REMOTE_LCD
401 if (global_settings.remote_caption_backlight)
403 /* turn on remote backlight n seconds before track ends, and turn it
404 off n seconds into the new track. n == remote_backlight_timeout,
405 or 5s */
406 int n = global_settings.remote_backlight_timeout * 1000;
408 if ( n < 1000 )
409 n = 5000; /* use 5s if backlight is always on or off */
411 if (((state->id3->elapsed < 1000) ||
412 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
413 (state->paused == false))
414 remote_backlight_on();
416 #endif
419 #endif
422 static void change_dir(int direction)
424 if (global_settings.prevent_skip)
425 return;
427 if (direction < 0)
428 audio_prev_dir();
429 else if (direction > 0)
430 audio_next_dir();
431 /* prevent the next dir to immediatly start being ffw'd */
432 action_wait_for_release();
435 static void prev_track(unsigned long skip_thresh)
437 if (wps_state.id3->elapsed < skip_thresh)
439 audio_prev();
440 return;
442 else
444 if (wps_state.id3->cuesheet)
446 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
447 return;
450 if (!wps_state.paused)
451 #if (CONFIG_CODEC == SWCODEC)
452 audio_pre_ff_rewind();
453 #else
454 audio_pause();
455 #endif
457 audio_ff_rewind(0);
459 #if (CONFIG_CODEC != SWCODEC)
460 if (!wps_state.paused)
461 audio_resume();
462 #endif
466 static void next_track(void)
468 /* take care of if we're playing a cuesheet */
469 if (wps_state.id3->cuesheet)
471 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
473 /* if the result was false, then we really want
474 to skip to the next track */
475 return;
479 audio_next();
482 static void play_hop(int direction)
484 long step = global_settings.skip_length*1000;
485 long elapsed = wps_state.id3->elapsed;
486 long remaining = wps_state.id3->length - elapsed;
488 if (step < 0)
490 if (direction < 0)
492 prev_track(DEFAULT_SKIP_TRESH);
493 return;
495 else if (remaining < DEFAULT_SKIP_TRESH*2)
497 next_track();
498 return;
500 else
501 elapsed += (remaining - DEFAULT_SKIP_TRESH*2);
503 else if (!global_settings.prevent_skip &&
504 (!step ||
505 (direction > 0 && step >= remaining) ||
506 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
507 { /* Do normal track skipping */
508 if (direction > 0)
509 next_track();
510 else if (direction < 0)
511 prev_track(DEFAULT_SKIP_TRESH);
512 return;
514 else if (direction == 1 && step >= remaining)
516 #if CONFIG_CODEC == SWCODEC
517 if(global_settings.beep)
518 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
519 #endif
520 return;
522 else if ((direction == -1 && elapsed < step))
524 elapsed = 0;
526 else
528 elapsed += step * direction;
530 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
532 #if (CONFIG_CODEC == SWCODEC)
533 audio_pre_ff_rewind();
534 #else
535 audio_pause();
536 #endif
538 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
539 #if (CONFIG_CODEC != SWCODEC)
540 if (!wps_state.paused)
541 audio_resume();
542 #endif
546 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
548 * If the user is unable to see the wps, because the display is deactivated,
549 * we suppress updates until the wps is activated again (the lcd driver will
550 * call this hook to issue an instant update)
551 * */
552 static void wps_lcd_activation_hook(void *param)
554 (void)param;
555 wps_sync_data.do_full_update = true;
556 /* force timeout in wps main loop, so that the update is instantly */
557 queue_post(&button_queue, BUTTON_NONE, 0);
559 #endif
561 static void gwps_leave_wps(void)
563 int i;
565 FOR_NB_SCREENS(i)
567 gui_wps[i].display->stop_scroll();
568 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
569 gui_wps[i].display->backdrop_show(sb_get_backdrop(i));
570 #endif
572 #ifdef HAVE_LCD_BITMAP
573 bool draw = false;
574 if (gui_wps[i].data->wps_sb_tag)
575 draw = gui_wps[i].data->show_sb_on_wps;
576 else if (statusbar_position(i) != STATUSBAR_OFF)
577 draw = true;
578 #endif
579 viewportmanager_theme_undo(i, draw);
583 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
584 /* Play safe and unregister the hook */
585 remove_event(LCD_EVENT_ACTIVATION, wps_lcd_activation_hook);
586 #endif
587 /* unhandle statusbar update delay */
588 sb_skin_set_update_delay(DEFAULT_UPDATE_DELAY);
592 * display the wps on entering or restoring */
593 static void gwps_enter_wps(void)
595 int i;
596 FOR_NB_SCREENS(i)
598 struct gui_wps *gwps = &gui_wps[i];
599 struct screen *display = gwps->display;
600 #ifdef HAVE_LCD_BITMAP
601 bool draw = false;
602 if (gui_wps[i].data->wps_sb_tag)
603 draw = gui_wps[i].data->show_sb_on_wps;
604 else if (statusbar_position(i) != STATUSBAR_OFF)
605 draw = true;
606 #endif
607 display->stop_scroll();
608 viewportmanager_theme_enable(i, draw, NULL);
610 /* Update the values in the first (default) viewport - in case the user
611 has modified the statusbar or colour settings */
612 #if LCD_DEPTH > 1
613 if (display->depth > 1)
615 struct viewport *vp = &find_viewport(VP_DEFAULT_LABEL, gwps->data)->vp;
616 vp->fg_pattern = display->get_foreground();
617 vp->bg_pattern = display->get_background();
619 #endif
620 /* make the backdrop actually take effect */
621 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
622 display->backdrop_show(gwps->data->backdrop);
623 #endif
624 display->clear_display();
625 skin_update(gwps, WPS_REFRESH_ALL);
627 /* force statusbar/skin update since we just cleared the whole screen */
628 send_event(GUI_EVENT_ACTIONUPDATE, (void*)1);
631 #ifdef HAVE_TOUCHSCREEN
632 int wps_get_touchaction(struct wps_data *data)
634 short x,y;
635 short vx, vy;
636 int type = action_get_touchscreen_press(&x, &y);
637 static int last_action = ACTION_NONE;
638 struct touchregion *r;
639 bool repeated = (type == BUTTON_REPEAT);
640 bool released = (type == BUTTON_REL);
641 struct skin_token_list *regions = data->touchregions;
642 while (regions)
644 r = (struct touchregion *)regions->token->value.data;
645 /* make sure this region's viewport is visible */
646 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
648 regions = regions->next;
649 continue;
651 /* check if it's inside this viewport */
652 if (viewport_point_within_vp(&(r->wvp->vp), x, y))
653 { /* reposition the touch inside the viewport since touchregions
654 * are relative to a preceding viewport */
655 vx = x - r->wvp->vp.x;
656 vy = y - r->wvp->vp.y;
657 /* now see if the point is inside this region */
658 if (vx >= r->x && vx < r->x+r->width &&
659 vy >= r->y && vy < r->y+r->height)
661 /* reposition the touch within the area */
662 vx -= r->x;
663 vy -= r->y;
665 switch(r->type)
667 case WPS_TOUCHREGION_ACTION:
668 if ((repeated && r->repeat) || (released && !r->repeat))
670 last_action = r->action;
671 return r->action;
673 break;
674 case WPS_TOUCHREGION_SCROLLBAR:
675 if(r->width > r->height)
676 /* landscape */
677 wps_state.id3->elapsed = (vx *
678 wps_state.id3->length) / r->width;
679 else
680 /* portrait */
681 wps_state.id3->elapsed = (vy *
682 wps_state.id3->length) / r->height;
684 if (!wps_state.paused)
685 #if (CONFIG_CODEC == SWCODEC)
686 audio_pre_ff_rewind();
687 #else
688 audio_pause();
689 #endif
690 audio_ff_rewind(wps_state.id3->elapsed);
691 #if (CONFIG_CODEC != SWCODEC)
692 if (!wps_state.paused)
693 audio_resume();
694 #endif
695 break;
696 case WPS_TOUCHREGION_VOLUME:
698 const int min_vol = sound_min(SOUND_VOLUME);
699 const int max_vol = sound_max(SOUND_VOLUME);
700 if(r->width > r->height)
701 /* landscape */
702 global_settings.volume = (vx *
703 (max_vol - min_vol)) / r->width;
704 else
705 /* portrait */
706 global_settings.volume = ((r->height - vy) *
707 (max_vol-min_vol)) / r->height;
709 global_settings.volume += min_vol;
710 setvol();
711 return ACTION_REDRAW;
716 regions = regions->next;
719 if ((last_action == ACTION_WPS_SEEKBACK || last_action == ACTION_WPS_SEEKFWD))
720 return ACTION_WPS_STOPSEEK;
721 last_action = ACTION_TOUCHSCREEN;
722 return ACTION_TOUCHSCREEN;
724 #endif
725 /* The WPS can be left in two ways:
726 * a) call a function, which draws over the wps. In this case, the wps
727 * will be still active (i.e. the below function didn't return)
728 * b) return with a value evaluated by root_menu.c, in this case the wps
729 * is really left, and root_menu will handle the next screen
731 * In either way, call gwps_leave_wps(), in order to restore the correct
732 * "main screen" backdrops and statusbars
734 long gui_wps_show(void)
736 long button = 0;
737 bool restore = true;
738 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
739 bool exit = false;
740 bool bookmark = false;
741 bool update = false;
742 bool vol_changed = false;
743 int i;
744 long last_left = 0, last_right = 0;
746 #ifdef HAVE_LCD_CHARCELLS
747 status_set_audio(true);
748 status_set_param(false);
749 #endif
751 #ifdef AB_REPEAT_ENABLE
752 ab_repeat_init();
753 ab_reset_markers();
754 #endif
755 wps_state_init();
757 while ( 1 )
759 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
761 /* did someone else (i.e power thread) change audio pause mode? */
762 if (wps_state.paused != audio_paused) {
763 wps_state.paused = audio_paused;
765 /* if another thread paused audio, we are probably in car mode,
766 about to shut down. lets save the settings. */
767 if (wps_state.paused) {
768 settings_save();
769 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
770 call_storage_idle_notifys(true);
771 #endif
774 #ifdef HAVE_LCD_BITMAP
775 /* when the peak meter is enabled we want to have a
776 few extra updates to make it look smooth. On the
777 other hand we don't want to waste energy if it
778 isn't displayed */
779 bool pm=false;
780 FOR_NB_SCREENS(i)
782 if(gui_wps[i].data->peak_meter_enabled)
783 pm = true;
786 if (pm) {
787 long next_refresh = current_tick;
788 long next_big_refresh = current_tick + HZ / 5;
789 button = BUTTON_NONE;
790 while (TIME_BEFORE(current_tick, next_big_refresh)) {
791 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_NOBLOCK);
792 /* check for restore to not let the peakmeter delay the
793 * initial draw of the wps, don't delay handling of button
794 * presses either */
795 if (button != ACTION_NONE || restore) {
796 break;
798 peak_meter_peek();
799 sleep(0); /* Sleep until end of current tick. */
801 if (TIME_AFTER(current_tick, next_refresh)) {
802 FOR_NB_SCREENS(i)
804 if(gui_wps[i].data->peak_meter_enabled)
805 skin_update(&gui_wps[i], WPS_REFRESH_PEAK_METER);
806 next_refresh += HZ / PEAK_METER_FPS;
813 /* The peak meter is disabled
814 -> no additional screen updates needed */
815 else
816 #endif
817 { /* 1 is the minimum timeout which lets other threads run.
818 * audio thread (apprently) needs to run before displaying the wps
819 * or bad things happen with regards to cuesheet
820 * (probably a race condition, on sh at least) */
821 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,
822 restore ? 1 : HZ/5);
825 /* Exit if audio has stopped playing. This happens e.g. at end of
826 playlist or if using the sleep timer. */
827 if (!(audio_status() & AUDIO_STATUS_PLAY))
828 exit = true;
829 #ifdef HAVE_TOUCHSCREEN
830 if (button == ACTION_TOUCHSCREEN)
831 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
832 #endif
833 /* The iPods/X5/M5 use a single button for the A-B mode markers,
834 defined as ACTION_WPSAB_SINGLE in their config files. */
835 #ifdef ACTION_WPSAB_SINGLE
836 if (!global_settings.party_mode && ab_repeat_mode_enabled())
838 static int wps_ab_state = 0;
839 if (button == ACTION_WPSAB_SINGLE)
841 switch (wps_ab_state)
843 case 0: /* set the A spot */
844 button = ACTION_WPS_ABSETA_PREVDIR;
845 break;
846 case 1: /* set the B spot */
847 button = ACTION_WPS_ABSETB_NEXTDIR;
848 break;
849 case 2:
850 button = ACTION_WPS_ABRESET;
851 break;
853 wps_ab_state = (wps_ab_state+1) % 3;
856 #endif
857 switch(button)
859 case ACTION_WPS_CONTEXT:
861 gwps_leave_wps();
862 /* if music is stopped in the context menu we want to exit the wps */
863 if (onplay(wps_state.id3->path,
864 FILE_ATTR_AUDIO, CONTEXT_WPS) == ONPLAY_MAINMENU
865 || !audio_status())
866 return GO_TO_ROOT;
867 restore = true;
869 break;
871 case ACTION_WPS_BROWSE:
872 #ifdef HAVE_LCD_CHARCELLS
873 status_set_record(false);
874 status_set_audio(false);
875 #endif
876 gwps_leave_wps();
877 return GO_TO_PREVIOUS_BROWSER;
878 break;
880 /* play/pause */
881 case ACTION_WPS_PLAY:
882 if (global_settings.party_mode)
883 break;
884 if ( wps_state.paused )
886 wps_state.paused = false;
887 if ( global_settings.fade_on_stop )
888 fade(true, true);
889 else
890 audio_resume();
892 else
894 wps_state.paused = true;
895 if ( global_settings.fade_on_stop )
896 fade(false, true);
897 else
898 audio_pause();
899 settings_save();
900 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
901 call_storage_idle_notifys(true); /* make sure resume info is saved */
902 #endif
904 break;
906 case ACTION_WPS_VOLUP:
907 global_settings.volume++;
908 vol_changed = true;
909 break;
910 case ACTION_WPS_VOLDOWN:
911 global_settings.volume--;
912 vol_changed = true;
913 break;
914 /* fast forward
915 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
916 case ACTION_WPS_SEEKFWD:
917 if (global_settings.party_mode)
918 break;
919 if (current_tick -last_right < HZ)
921 if (wps_state.id3->cuesheet)
923 audio_next();
925 else
927 change_dir(1);
930 else
931 ffwd_rew(ACTION_WPS_SEEKFWD);
932 last_right = last_left = 0;
933 break;
934 /* fast rewind
935 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
936 case ACTION_WPS_SEEKBACK:
937 if (global_settings.party_mode)
938 break;
939 if (current_tick -last_left < HZ)
941 if (wps_state.id3->cuesheet)
943 if (!wps_state.paused)
944 #if (CONFIG_CODEC == SWCODEC)
945 audio_pre_ff_rewind();
946 #else
947 audio_pause();
948 #endif
949 audio_ff_rewind(0);
951 else
953 change_dir(-1);
956 else
957 ffwd_rew(ACTION_WPS_SEEKBACK);
958 last_left = last_right = 0;
959 break;
961 /* prev / restart */
962 case ACTION_WPS_SKIPPREV:
963 if (global_settings.party_mode)
964 break;
965 last_left = current_tick;
966 #ifdef AB_REPEAT_ENABLE
967 /* if we're in A/B repeat mode and the current position
968 is past the A marker, jump back to the A marker... */
969 if ( ab_repeat_mode_enabled() )
971 if ( ab_after_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;
987 /* next
988 OR if skip length set, hop by predetermined amount. */
989 case ACTION_WPS_SKIPNEXT:
990 if (global_settings.party_mode)
991 break;
992 last_right = current_tick;
993 #ifdef AB_REPEAT_ENABLE
994 /* if we're in A/B repeat mode and the current position is
995 before the A marker, jump to the A marker... */
996 if ( ab_repeat_mode_enabled() )
998 if ( ab_before_A_marker(wps_state.id3->elapsed) )
1000 ab_jump_to_A_marker();
1001 break;
1002 #if (AB_REPEAT_ENABLE == 2)
1003 } else {
1004 ab_reset_markers();
1005 #endif
1008 else
1009 /* ...otherwise, do it normally */
1010 #endif
1011 play_hop(1);
1012 break;
1013 /* next / prev directories */
1014 /* and set A-B markers if in a-b mode */
1015 case ACTION_WPS_ABSETB_NEXTDIR:
1016 if (global_settings.party_mode)
1017 break;
1018 #if defined(AB_REPEAT_ENABLE)
1019 if (ab_repeat_mode_enabled())
1021 ab_set_B_marker(wps_state.id3->elapsed);
1022 ab_jump_to_A_marker();
1024 else
1025 #endif
1027 change_dir(1);
1029 break;
1030 case ACTION_WPS_ABSETA_PREVDIR:
1031 if (global_settings.party_mode)
1032 break;
1033 #if defined(AB_REPEAT_ENABLE)
1034 if (ab_repeat_mode_enabled())
1035 ab_set_A_marker(wps_state.id3->elapsed);
1036 else
1037 #endif
1039 change_dir(-1);
1041 break;
1042 /* menu key functions */
1043 case ACTION_WPS_MENU:
1044 gwps_leave_wps();
1045 return GO_TO_ROOT;
1046 break;
1049 #ifdef HAVE_QUICKSCREEN
1050 case ACTION_WPS_QUICKSCREEN:
1052 gwps_leave_wps();
1053 if (quick_screen_quick(button))
1054 return GO_TO_ROOT;
1055 restore = true;
1057 break;
1058 #endif /* HAVE_QUICKSCREEN */
1060 /* screen settings */
1061 #ifdef BUTTON_F3
1062 case ACTION_F3:
1064 gwps_leave_wps();
1065 if (quick_screen_f3(BUTTON_F3))
1066 return GO_TO_ROOT;
1067 restore = true;
1069 break;
1070 #endif /* BUTTON_F3 */
1072 /* pitch screen */
1073 #ifdef HAVE_PITCHSCREEN
1074 case ACTION_WPS_PITCHSCREEN:
1076 gwps_leave_wps();
1077 if (1 == gui_syncpitchscreen_run())
1078 return GO_TO_ROOT;
1079 restore = true;
1081 break;
1082 #endif /* HAVE_PITCHSCREEN */
1084 #ifdef AB_REPEAT_ENABLE
1085 /* reset A&B markers */
1086 case ACTION_WPS_ABRESET:
1087 if (ab_repeat_mode_enabled())
1089 ab_reset_markers();
1090 update = true;
1092 break;
1093 #endif /* AB_REPEAT_ENABLE */
1095 /* stop and exit wps */
1096 case ACTION_WPS_STOP:
1097 if (global_settings.party_mode)
1098 break;
1099 bookmark = true;
1100 exit = true;
1101 break;
1103 case ACTION_WPS_ID3SCREEN:
1105 gwps_leave_wps();
1106 if (browse_id3())
1107 return GO_TO_ROOT;
1108 restore = true;
1110 break;
1111 #ifdef HAVE_TOUCHSCREEN
1112 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1114 global_settings.playlist_shuffle =
1115 !global_settings.playlist_shuffle;
1116 #if CONFIG_CODEC == SWCODEC
1117 dsp_set_replaygain();
1118 #endif
1119 if (global_settings.playlist_shuffle)
1120 playlist_randomise(NULL, current_tick, true);
1121 else
1122 playlist_sort(NULL, true);
1124 break;
1125 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1127 const struct settings_list *rep_setting =
1128 find_setting(&global_settings.repeat_mode, NULL);
1129 option_select_next_val(rep_setting, false, true);
1130 audio_flush_and_reload_tracks();
1132 break;
1133 #endif /* HAVE_TOUCHSCREEN */
1134 /* this case is used by the softlock feature
1135 * it requests a full update here */
1136 case ACTION_REDRAW:
1137 wps_sync_data.do_full_update = true;
1138 break;
1139 case ACTION_NONE: /* Timeout, do a partial update */
1140 update = true;
1141 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1142 break;
1143 #ifdef HAVE_RECORDING
1144 case ACTION_WPS_REC:
1145 exit = true;
1146 break;
1147 #endif
1148 case SYS_POWEROFF:
1149 default_event_handler(SYS_POWEROFF);
1150 break;
1151 case ACTION_WPS_VIEW_PLAYLIST:
1152 gwps_leave_wps();
1153 if (playlist_viewer()) /* true if USB connected */
1154 return GO_TO_ROOT;
1155 restore = true;
1156 break;
1157 default:
1158 if(default_event_handler(button) == SYS_USB_CONNECTED)
1160 gwps_leave_wps();
1161 return GO_TO_ROOT;
1163 update = true;
1164 break;
1167 if (vol_changed)
1169 bool res = false;
1170 vol_changed = false;
1171 setvol();
1172 FOR_NB_SCREENS(i)
1174 if(update_onvol_change(&gui_wps[i]))
1175 res = true;
1177 if (res) {
1178 restore = true;
1179 restoretimer = RESTORE_WPS_NEXT_SECOND;
1184 if (restore &&
1185 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1186 TIME_AFTER(current_tick, restoretimer)))
1188 restore = false;
1189 restoretimer = RESTORE_WPS_INSTANTLY;
1190 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1191 add_event(LCD_EVENT_ACTIVATION, false, wps_lcd_activation_hook);
1192 #endif
1193 /* we remove the update delay since it's not very usable in the wps,
1194 * e.g. during volume changing or ffwd/rewind */
1195 sb_skin_set_update_delay(0);
1196 wps_sync_data.do_full_update = update = false;
1197 gwps_enter_wps();
1199 else if (wps_sync_data.do_full_update || update)
1201 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1202 gwps_caption_backlight(&wps_state);
1203 #endif
1204 FOR_NB_SCREENS(i)
1206 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1207 /* currently, all remotes are readable without backlight
1208 * so still update those */
1209 if (lcd_active() || (i != SCREEN_MAIN))
1210 #endif
1212 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
1215 wps_sync_data.do_full_update = false;
1216 update = false;
1219 if (exit) {
1220 #ifdef HAVE_LCD_CHARCELLS
1221 status_set_record(false);
1222 status_set_audio(false);
1223 #endif
1224 if (global_settings.fade_on_stop)
1225 fade(false, true);
1227 if (bookmark)
1228 bookmark_autobookmark();
1229 audio_stop();
1230 #ifdef AB_REPEAT_ENABLE
1231 ab_reset_markers();
1232 #endif
1233 gwps_leave_wps();
1234 #ifdef HAVE_RECORDING
1235 if (button == ACTION_WPS_REC)
1236 return GO_TO_RECSCREEN;
1237 #endif
1238 if (global_settings.browse_current)
1239 return GO_TO_PREVIOUS_BROWSER;
1240 return GO_TO_PREVIOUS;
1243 if (button && !IS_SYSEVENT(button) )
1244 storage_spin();
1246 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1249 /* this is called from the playback thread so NO DRAWING! */
1250 static void track_changed_callback(void *param)
1252 wps_state.id3 = (struct mp3entry*)param;
1253 wps_state.nid3 = audio_next_track();
1254 if (wps_state.id3->cuesheet)
1256 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1257 cue_spoof_id3(wps_state.id3->cuesheet, wps_state.id3);
1259 wps_sync_data.do_full_update = true;
1261 static void nextid3available_callback(void* param)
1263 (void)param;
1264 wps_state.nid3 = audio_next_track();
1265 wps_sync_data.do_full_update = true;
1269 static void wps_state_init(void)
1271 wps_state.ff_rewind = false;
1272 wps_state.paused = false;
1273 if(audio_status() & AUDIO_STATUS_PLAY)
1275 wps_state.id3 = audio_current_track();
1276 wps_state.nid3 = audio_next_track();
1278 else
1280 wps_state.id3 = NULL;
1281 wps_state.nid3 = NULL;
1283 /* We'll be updating due to restore initialized with true */
1284 wps_sync_data.do_full_update = false;
1285 /* add the WPS track event callbacks */
1286 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1287 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1291 void gui_sync_wps_init(void)
1293 int i;
1294 FOR_NB_SCREENS(i)
1296 #ifdef HAVE_ALBUMART
1297 wps_datas[i].albumart = NULL;
1298 wps_datas[i].playback_aa_slot = -1;
1299 #endif
1300 gui_wps[i].data = &wps_datas[i];
1301 gui_wps[i].display = &screens[i];
1302 /* Currently no seperate wps_state needed/possible
1303 so use the only available ( "global" ) one */
1304 gui_wps[i].state = &wps_state;
1305 /* must point to the same struct for both screens */
1306 gui_wps[i].sync_data = &wps_sync_data;
1311 #ifdef IPOD_ACCESSORY_PROTOCOL
1312 bool is_wps_fading(void)
1314 return wps_state.is_fading;
1317 int wps_get_ff_rewind_count(void)
1319 return wps_state.ff_rewind_count;
1321 #endif