Forgot the #
[kugel-rb.git] / apps / gui / wps.c
blobe2a7dcb9617bb631dcc2d72f69cd437a6eb0331d
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 #ifdef HAVE_TOUCHSCREEN
90 static void wps_disarm_touchregions(struct wps_data *data);
91 #endif
93 #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps"
94 #ifdef HAVE_REMOTE_LCD
95 #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps"
96 #define DEFAULT_WPS(screen) ((screen) == SCREEN_MAIN ? \
97 WPS_DEFAULTCFG:RWPS_DEFAULTCFG)
98 #else
99 #define DEFAULT_WPS(screen) (WPS_DEFAULTCFG)
100 #endif
102 void wps_data_load(enum screen_type screen, const char *buf, bool isfile)
104 bool loaded_ok;
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 #if LCD_DEPTH > 1
133 "%Xd\n"
134 #endif
135 "%s%?it<%?in<%in. |>%it|%fn>\n"
136 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
137 "%s%?id<%id|%?d1<%d1|(root)>> %?iy<(%iy)|>\n\n"
138 "%al%pc/%pt%ar[%pp:%pe]\n"
139 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
140 "%pb\n%pm\n",
141 #else
142 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d2> - %?id<%id|%d1>\n"
143 "%pc%?ps<*|/>%pt\n",
144 #endif
145 #ifdef HAVE_REMOTE_LCD
146 #if LCD_REMOTE_DEPTH > 1
147 "%Xd\n"
148 #endif
149 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
150 "%s%?it<%?in<%in. |>%it|%fn>\n"
151 "%al%pc/%pt%ar[%pp:%pe]\n"
152 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
153 "%pb\n",
154 #endif
156 skin_data_load(screen, gui_wps[screen].data, skin_buf[screen], false);
160 void fade(bool fade_in, bool updatewps)
162 int fp_global_vol = global_settings.volume << 8;
163 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
164 int fp_step = (fp_global_vol - fp_min_vol) / 30;
165 int i;
166 wps_state.is_fading = !fade_in;
167 if (fade_in) {
168 /* fade in */
169 int fp_volume = fp_min_vol;
171 /* zero out the sound */
172 sound_set_volume(fp_min_vol >> 8);
174 sleep(HZ/10); /* let audio thread run */
175 audio_resume();
177 while (fp_volume < fp_global_vol - fp_step) {
178 fp_volume += fp_step;
179 sound_set_volume(fp_volume >> 8);
180 if (updatewps)
182 FOR_NB_SCREENS(i)
183 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
185 sleep(1);
187 sound_set_volume(global_settings.volume);
189 else {
190 /* fade out */
191 int fp_volume = fp_global_vol;
193 while (fp_volume > fp_min_vol + fp_step) {
194 fp_volume -= fp_step;
195 sound_set_volume(fp_volume >> 8);
196 if (updatewps)
198 FOR_NB_SCREENS(i)
199 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
201 sleep(1);
203 audio_pause();
204 wps_state.is_fading = false;
205 #if CONFIG_CODEC != SWCODEC
206 #ifndef SIMULATOR
207 /* let audio thread run and wait for the mas to run out of data */
208 while (!mp3_pause_done())
209 #endif
210 sleep(HZ/10);
211 #endif
213 /* reset volume to what it was before the fade */
214 sound_set_volume(global_settings.volume);
218 static bool update_onvol_change(struct gui_wps * gwps)
220 skin_update(gwps, WPS_REFRESH_NON_STATIC);
222 #ifdef HAVE_LCD_CHARCELLS
223 splashf(0, "Vol: %3d dB",
224 sound_val2phys(SOUND_VOLUME, global_settings.volume));
225 return true;
226 #endif
227 return false;
231 bool ffwd_rew(int button)
233 unsigned int step = 0; /* current ff/rewind step */
234 unsigned int max_step = 0; /* maximum ff/rewind step */
235 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
236 int direction = -1; /* forward=1 or backward=-1 */
237 bool exit = false;
238 bool usb = false;
239 int i = 0;
240 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
242 if (button == ACTION_NONE)
244 status_set_ffmode(0);
245 return usb;
247 while (!exit)
249 switch ( button )
251 case ACTION_WPS_SEEKFWD:
252 direction = 1;
253 case ACTION_WPS_SEEKBACK:
254 if (wps_state.ff_rewind)
256 if (direction == 1)
258 /* fast forwarding, calc max step relative to end */
259 max_step = (wps_state.id3->length -
260 (wps_state.id3->elapsed +
261 ff_rewind_count)) *
262 FF_REWIND_MAX_PERCENT / 100;
264 else
266 /* rewinding, calc max step relative to start */
267 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
268 FF_REWIND_MAX_PERCENT / 100;
271 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
273 if (step > max_step)
274 step = max_step;
276 ff_rewind_count += step * direction;
278 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
279 step += step >> ff_rw_accel;
281 else
283 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
284 wps_state.id3 && wps_state.id3->length )
286 if (!wps_state.paused)
287 #if (CONFIG_CODEC == SWCODEC)
288 audio_pre_ff_rewind();
289 #else
290 audio_pause();
291 #endif
292 #if CONFIG_KEYPAD == PLAYER_PAD
293 FOR_NB_SCREENS(i)
294 gui_wps[i].display->stop_scroll();
295 #endif
296 if (direction > 0)
297 status_set_ffmode(STATUS_FASTFORWARD);
298 else
299 status_set_ffmode(STATUS_FASTBACKWARD);
301 wps_state.ff_rewind = true;
303 step = 1000 * global_settings.ff_rewind_min_step;
305 else
306 break;
309 if (direction > 0) {
310 if ((wps_state.id3->elapsed + ff_rewind_count) >
311 wps_state.id3->length)
312 ff_rewind_count = wps_state.id3->length -
313 wps_state.id3->elapsed;
315 else {
316 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
317 ff_rewind_count = -wps_state.id3->elapsed;
320 /* set the wps state ff_rewind_count so the progess info
321 displays corectly */
322 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
323 ff_rewind_count:-ff_rewind_count;
324 FOR_NB_SCREENS(i)
326 skin_update(&gui_wps[i],
327 WPS_REFRESH_PLAYER_PROGRESS |
328 WPS_REFRESH_DYNAMIC);
331 break;
333 case ACTION_WPS_STOPSEEK:
334 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
335 audio_ff_rewind(wps_state.id3->elapsed);
336 wps_state.ff_rewind_count = 0;
337 wps_state.ff_rewind = false;
338 status_set_ffmode(0);
339 #if (CONFIG_CODEC != SWCODEC)
340 if (!wps_state.paused)
341 audio_resume();
342 #endif
343 #ifdef HAVE_LCD_CHARCELLS
344 FOR_NB_SCREENS(i)
345 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
346 #endif
347 exit = true;
348 break;
350 default:
351 if(default_event_handler(button) == SYS_USB_CONNECTED) {
352 status_set_ffmode(0);
353 usb = true;
354 exit = true;
356 break;
358 if (!exit)
360 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
361 #ifdef HAVE_TOUCHSCREEN
362 if (button == ACTION_TOUCHSCREEN)
363 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
364 #endif
367 return usb;
371 void display_keylock_text(bool locked)
373 int i;
374 FOR_NB_SCREENS(i)
375 gui_wps[i].display->stop_scroll();
377 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
383 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
384 static void gwps_caption_backlight(struct wps_state *state)
386 if (state && state->id3)
388 #ifdef HAVE_BACKLIGHT
389 if (global_settings.caption_backlight)
391 /* turn on backlight n seconds before track ends, and turn it off n
392 seconds into the new track. n == backlight_timeout, or 5s */
393 int n = global_settings.backlight_timeout * 1000;
395 if ( n < 1000 )
396 n = 5000; /* use 5s if backlight is always on or off */
398 if (((state->id3->elapsed < 1000) ||
399 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
400 (state->paused == false))
401 backlight_on();
403 #endif
404 #ifdef HAVE_REMOTE_LCD
405 if (global_settings.remote_caption_backlight)
407 /* turn on remote backlight n seconds before track ends, and turn it
408 off n seconds into the new track. n == remote_backlight_timeout,
409 or 5s */
410 int n = global_settings.remote_backlight_timeout * 1000;
412 if ( n < 1000 )
413 n = 5000; /* use 5s if backlight is always on or off */
415 if (((state->id3->elapsed < 1000) ||
416 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
417 (state->paused == false))
418 remote_backlight_on();
420 #endif
423 #endif
426 static void change_dir(int direction)
428 if (global_settings.prevent_skip)
429 return;
431 if (direction < 0)
432 audio_prev_dir();
433 else if (direction > 0)
434 audio_next_dir();
435 /* prevent the next dir to immediatly start being ffw'd */
436 action_wait_for_release();
439 static void prev_track(unsigned long skip_thresh)
441 if (wps_state.id3->elapsed < skip_thresh)
443 audio_prev();
444 return;
446 else
448 if (wps_state.id3->cuesheet)
450 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
451 return;
454 if (!wps_state.paused)
455 #if (CONFIG_CODEC == SWCODEC)
456 audio_pre_ff_rewind();
457 #else
458 audio_pause();
459 #endif
461 audio_ff_rewind(0);
463 #if (CONFIG_CODEC != SWCODEC)
464 if (!wps_state.paused)
465 audio_resume();
466 #endif
470 static void next_track(void)
472 /* take care of if we're playing a cuesheet */
473 if (wps_state.id3->cuesheet)
475 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
477 /* if the result was false, then we really want
478 to skip to the next track */
479 return;
483 audio_next();
486 static void play_hop(int direction)
488 long step = global_settings.skip_length*1000;
489 long elapsed = wps_state.id3->elapsed;
490 long remaining = wps_state.id3->length - elapsed;
492 if (step < 0)
494 if (direction < 0)
496 prev_track(DEFAULT_SKIP_TRESH);
497 return;
499 else if (remaining < DEFAULT_SKIP_TRESH*2)
501 next_track();
502 return;
504 else
505 elapsed += (remaining - DEFAULT_SKIP_TRESH*2);
507 else if (!global_settings.prevent_skip &&
508 (!step ||
509 (direction > 0 && step >= remaining) ||
510 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
511 { /* Do normal track skipping */
512 if (direction > 0)
513 next_track();
514 else if (direction < 0)
515 prev_track(DEFAULT_SKIP_TRESH);
516 return;
518 else if (direction == 1 && step >= remaining)
520 #if CONFIG_CODEC == SWCODEC
521 if(global_settings.beep)
522 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
523 #endif
524 return;
526 else if ((direction == -1 && elapsed < step))
528 elapsed = 0;
530 else
532 elapsed += step * direction;
534 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
536 #if (CONFIG_CODEC == SWCODEC)
537 audio_pre_ff_rewind();
538 #else
539 audio_pause();
540 #endif
542 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
543 #if (CONFIG_CODEC != SWCODEC)
544 if (!wps_state.paused)
545 audio_resume();
546 #endif
550 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
552 * If the user is unable to see the wps, because the display is deactivated,
553 * we suppress updates until the wps is activated again (the lcd driver will
554 * call this hook to issue an instant update)
555 * */
556 static void wps_lcd_activation_hook(void *param)
558 (void)param;
559 wps_sync_data.do_full_update = true;
560 /* force timeout in wps main loop, so that the update is instantly */
561 queue_post(&button_queue, BUTTON_NONE, 0);
563 #endif
565 static void gwps_leave_wps(void)
567 int i;
569 FOR_NB_SCREENS(i)
571 gui_wps[i].display->stop_scroll();
572 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
573 gui_wps[i].display->backdrop_show(sb_get_backdrop(i));
574 #endif
576 #ifdef HAVE_LCD_BITMAP
577 bool draw = false;
578 if (gui_wps[i].data->wps_sb_tag)
579 draw = gui_wps[i].data->show_sb_on_wps;
580 else if (statusbar_position(i) != STATUSBAR_OFF)
581 draw = true;
582 #endif
583 viewportmanager_theme_undo(i, draw);
587 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
588 /* Play safe and unregister the hook */
589 remove_event(LCD_EVENT_ACTIVATION, wps_lcd_activation_hook);
590 #endif
591 /* unhandle statusbar update delay */
592 sb_skin_set_update_delay(DEFAULT_UPDATE_DELAY);
596 * display the wps on entering or restoring */
597 static void gwps_enter_wps(void)
599 int i;
600 FOR_NB_SCREENS(i)
602 struct gui_wps *gwps = &gui_wps[i];
603 struct screen *display = gwps->display;
604 #ifdef HAVE_LCD_BITMAP
605 bool draw = false;
606 if (gui_wps[i].data->wps_sb_tag)
607 draw = gui_wps[i].data->show_sb_on_wps;
608 else if (statusbar_position(i) != STATUSBAR_OFF)
609 draw = true;
610 #endif
611 display->stop_scroll();
612 viewportmanager_theme_enable(i, draw, NULL);
614 /* Update the values in the first (default) viewport - in case the user
615 has modified the statusbar or colour settings */
616 #if LCD_DEPTH > 1
617 if (display->depth > 1)
619 struct viewport *vp = &find_viewport(VP_DEFAULT_LABEL, gwps->data)->vp;
620 vp->fg_pattern = display->get_foreground();
621 vp->bg_pattern = display->get_background();
623 #endif
624 /* make the backdrop actually take effect */
625 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
626 display->backdrop_show(gwps->data->backdrop);
627 #endif
628 display->clear_display();
629 skin_update(gwps, WPS_REFRESH_ALL);
631 #ifdef HAVE_TOUCHSCREEN
632 wps_disarm_touchregions(gui_wps[i].data);
633 #endif
635 /* force statusbar/skin update since we just cleared the whole screen */
636 send_event(GUI_EVENT_ACTIONUPDATE, (void*)1);
639 #ifdef HAVE_TOUCHSCREEN
640 /** Disarms all touchregions. */
641 static void wps_disarm_touchregions(struct wps_data *data)
643 struct skin_token_list *regions = data->touchregions;
644 while (regions)
646 ((struct touchregion *)regions->token->value.data)->armed = false;
647 regions = regions->next;
651 int wps_get_touchaction(struct wps_data *data)
653 int returncode = ACTION_NONE;
654 short x,y;
655 short vx, vy;
656 int type = action_get_touchscreen_press(&x, &y);
657 static int last_action = ACTION_NONE;
658 struct touchregion *r;
659 bool repeated = (type == BUTTON_REPEAT);
660 bool released = (type == BUTTON_REL);
661 bool pressed = (type == BUTTON_TOUCHSCREEN);
662 struct skin_token_list *regions = data->touchregions;
664 while (regions)
666 r = (struct touchregion *)regions->token->value.data;
667 /* make sure this region's viewport is visible */
668 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
670 regions = regions->next;
671 continue;
673 /* check if it's inside this viewport */
674 if (viewport_point_within_vp(&(r->wvp->vp), x, y))
675 { /* reposition the touch inside the viewport since touchregions
676 * are relative to a preceding viewport */
677 vx = x - r->wvp->vp.x;
678 vy = y - r->wvp->vp.y;
679 /* now see if the point is inside this region */
680 if (vx >= r->x && vx < r->x+r->width &&
681 vy >= r->y && vy < r->y+r->height)
683 /* reposition the touch within the area */
684 vx -= r->x;
685 vy -= r->y;
687 switch(r->type)
689 case WPS_TOUCHREGION_ACTION:
690 if (r->armed && ((repeated && r->repeat) || (released && !r->repeat)))
692 last_action = r->action;
693 returncode = r->action;
695 if (pressed)
696 r->armed = true;
697 break;
698 case WPS_TOUCHREGION_SCROLLBAR:
699 if(r->width > r->height)
700 /* landscape */
701 wps_state.id3->elapsed = (vx *
702 wps_state.id3->length) / r->width;
703 else
704 /* portrait */
705 wps_state.id3->elapsed = (vy *
706 wps_state.id3->length) / r->height;
708 if (!wps_state.paused)
709 #if (CONFIG_CODEC == SWCODEC)
710 audio_pre_ff_rewind();
711 #else
712 audio_pause();
713 #endif
714 audio_ff_rewind(wps_state.id3->elapsed);
715 #if (CONFIG_CODEC != SWCODEC)
716 if (!wps_state.paused)
717 audio_resume();
718 #endif
719 break;
720 case WPS_TOUCHREGION_VOLUME:
722 const int min_vol = sound_min(SOUND_VOLUME);
723 const int max_vol = sound_max(SOUND_VOLUME);
724 if(r->width > r->height)
725 /* landscape */
726 global_settings.volume = (vx *
727 (max_vol - min_vol)) / r->width;
728 else
729 /* portrait */
730 global_settings.volume = ((r->height - vy) *
731 (max_vol-min_vol)) / r->height;
733 global_settings.volume += min_vol;
734 setvol();
735 returncode = ACTION_REDRAW;
740 regions = regions->next;
743 /* On release, all regions are disarmed. */
744 if (released)
745 wps_disarm_touchregions(data);
747 if (returncode != ACTION_NONE)
748 return returncode;
750 if ((last_action == ACTION_WPS_SEEKBACK || last_action == ACTION_WPS_SEEKFWD))
751 return ACTION_WPS_STOPSEEK;
752 last_action = ACTION_TOUCHSCREEN;
753 return ACTION_TOUCHSCREEN;
755 #endif
756 /* The WPS can be left in two ways:
757 * a) call a function, which draws over the wps. In this case, the wps
758 * will be still active (i.e. the below function didn't return)
759 * b) return with a value evaluated by root_menu.c, in this case the wps
760 * is really left, and root_menu will handle the next screen
762 * In either way, call gwps_leave_wps(), in order to restore the correct
763 * "main screen" backdrops and statusbars
765 long gui_wps_show(void)
767 long button = 0;
768 bool restore = true;
769 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
770 bool exit = false;
771 bool bookmark = false;
772 bool update = false;
773 bool vol_changed = false;
774 int i;
775 long last_left = 0, last_right = 0;
777 #ifdef HAVE_LCD_CHARCELLS
778 status_set_audio(true);
779 status_set_param(false);
780 #endif
782 #ifdef AB_REPEAT_ENABLE
783 ab_repeat_init();
784 ab_reset_markers();
785 #endif
786 wps_state_init();
788 while ( 1 )
790 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
792 /* did someone else (i.e power thread) change audio pause mode? */
793 if (wps_state.paused != audio_paused) {
794 wps_state.paused = audio_paused;
796 /* if another thread paused audio, we are probably in car mode,
797 about to shut down. lets save the settings. */
798 if (wps_state.paused) {
799 settings_save();
800 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
801 call_storage_idle_notifys(true);
802 #endif
805 #ifdef HAVE_LCD_BITMAP
806 /* when the peak meter is enabled we want to have a
807 few extra updates to make it look smooth. On the
808 other hand we don't want to waste energy if it
809 isn't displayed */
810 bool pm=false;
811 FOR_NB_SCREENS(i)
813 if(gui_wps[i].data->peak_meter_enabled)
814 pm = true;
817 if (pm) {
818 long next_refresh = current_tick;
819 long next_big_refresh = current_tick + HZ / 5;
820 button = BUTTON_NONE;
821 while (TIME_BEFORE(current_tick, next_big_refresh)) {
822 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_NOBLOCK);
823 /* check for restore to not let the peakmeter delay the
824 * initial draw of the wps, don't delay handling of button
825 * presses either */
826 if (button != ACTION_NONE || restore) {
827 break;
829 peak_meter_peek();
830 sleep(0); /* Sleep until end of current tick. */
832 if (TIME_AFTER(current_tick, next_refresh)) {
833 FOR_NB_SCREENS(i)
835 if(gui_wps[i].data->peak_meter_enabled)
836 skin_update(&gui_wps[i], WPS_REFRESH_PEAK_METER);
837 next_refresh += HZ / PEAK_METER_FPS;
844 /* The peak meter is disabled
845 -> no additional screen updates needed */
846 else
847 #endif
848 { /* 1 is the minimum timeout which lets other threads run.
849 * audio thread (apprently) needs to run before displaying the wps
850 * or bad things happen with regards to cuesheet
851 * (probably a race condition, on sh at least) */
852 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,
853 restore ? 1 : HZ/5);
856 /* Exit if audio has stopped playing. This happens e.g. at end of
857 playlist or if using the sleep timer. */
858 if (!(audio_status() & AUDIO_STATUS_PLAY))
859 exit = true;
860 #ifdef HAVE_TOUCHSCREEN
861 if (button == ACTION_TOUCHSCREEN)
862 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
863 #endif
864 /* The iPods/X5/M5 use a single button for the A-B mode markers,
865 defined as ACTION_WPSAB_SINGLE in their config files. */
866 #ifdef ACTION_WPSAB_SINGLE
867 if (!global_settings.party_mode && ab_repeat_mode_enabled())
869 static int wps_ab_state = 0;
870 if (button == ACTION_WPSAB_SINGLE)
872 switch (wps_ab_state)
874 case 0: /* set the A spot */
875 button = ACTION_WPS_ABSETA_PREVDIR;
876 break;
877 case 1: /* set the B spot */
878 button = ACTION_WPS_ABSETB_NEXTDIR;
879 break;
880 case 2:
881 button = ACTION_WPS_ABRESET;
882 break;
884 wps_ab_state = (wps_ab_state+1) % 3;
887 #endif
888 switch(button)
890 case ACTION_WPS_CONTEXT:
892 gwps_leave_wps();
893 int retval = onplay(wps_state.id3->path,
894 FILE_ATTR_AUDIO, CONTEXT_WPS);
895 /* if music is stopped in the context menu we want to exit the wps */
896 if (retval == ONPLAY_MAINMENU
897 || !audio_status())
898 return GO_TO_ROOT;
899 else if (retval == ONPLAY_PLAYLIST)
900 return GO_TO_PLAYLIST_VIEWER;
901 restore = true;
903 break;
905 case ACTION_WPS_BROWSE:
906 #ifdef HAVE_LCD_CHARCELLS
907 status_set_record(false);
908 status_set_audio(false);
909 #endif
910 gwps_leave_wps();
911 return GO_TO_PREVIOUS_BROWSER;
912 break;
914 /* play/pause */
915 case ACTION_WPS_PLAY:
916 if (global_settings.party_mode)
917 break;
918 if ( wps_state.paused )
920 wps_state.paused = false;
921 if ( global_settings.fade_on_stop )
922 fade(true, true);
923 else
924 audio_resume();
926 else
928 wps_state.paused = true;
929 if ( global_settings.fade_on_stop )
930 fade(false, true);
931 else
932 audio_pause();
933 settings_save();
934 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
935 call_storage_idle_notifys(true); /* make sure resume info is saved */
936 #endif
938 break;
940 case ACTION_WPS_VOLUP:
941 global_settings.volume++;
942 vol_changed = true;
943 break;
944 case ACTION_WPS_VOLDOWN:
945 global_settings.volume--;
946 vol_changed = true;
947 break;
948 /* fast forward
949 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
950 case ACTION_WPS_SEEKFWD:
951 if (global_settings.party_mode)
952 break;
953 if (current_tick -last_right < HZ)
955 if (wps_state.id3->cuesheet)
957 audio_next();
959 else
961 change_dir(1);
964 else
965 ffwd_rew(ACTION_WPS_SEEKFWD);
966 last_right = last_left = 0;
967 break;
968 /* fast rewind
969 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
970 case ACTION_WPS_SEEKBACK:
971 if (global_settings.party_mode)
972 break;
973 if (current_tick -last_left < HZ)
975 if (wps_state.id3->cuesheet)
977 if (!wps_state.paused)
978 #if (CONFIG_CODEC == SWCODEC)
979 audio_pre_ff_rewind();
980 #else
981 audio_pause();
982 #endif
983 audio_ff_rewind(0);
985 else
987 change_dir(-1);
990 else
991 ffwd_rew(ACTION_WPS_SEEKBACK);
992 last_left = last_right = 0;
993 break;
995 /* prev / restart */
996 case ACTION_WPS_SKIPPREV:
997 if (global_settings.party_mode)
998 break;
999 last_left = current_tick;
1000 #ifdef AB_REPEAT_ENABLE
1001 /* if we're in A/B repeat mode and the current position
1002 is past the A marker, jump back to the A marker... */
1003 if ( ab_repeat_mode_enabled() )
1005 if ( ab_after_A_marker(wps_state.id3->elapsed) )
1007 ab_jump_to_A_marker();
1008 break;
1009 #if (AB_REPEAT_ENABLE == 2)
1010 } else {
1011 ab_reset_markers();
1012 #endif
1015 else
1016 /* ...otherwise, do it normally */
1017 #endif
1018 play_hop(-1);
1019 break;
1021 /* next
1022 OR if skip length set, hop by predetermined amount. */
1023 case ACTION_WPS_SKIPNEXT:
1024 if (global_settings.party_mode)
1025 break;
1026 last_right = current_tick;
1027 #ifdef AB_REPEAT_ENABLE
1028 /* if we're in A/B repeat mode and the current position is
1029 before the A marker, jump to the A marker... */
1030 if ( ab_repeat_mode_enabled() )
1032 if ( ab_before_A_marker(wps_state.id3->elapsed) )
1034 ab_jump_to_A_marker();
1035 break;
1036 #if (AB_REPEAT_ENABLE == 2)
1037 } else {
1038 ab_reset_markers();
1039 #endif
1042 else
1043 /* ...otherwise, do it normally */
1044 #endif
1045 play_hop(1);
1046 break;
1047 /* next / prev directories */
1048 /* and set A-B markers if in a-b mode */
1049 case ACTION_WPS_ABSETB_NEXTDIR:
1050 if (global_settings.party_mode)
1051 break;
1052 #if defined(AB_REPEAT_ENABLE)
1053 if (ab_repeat_mode_enabled())
1055 ab_set_B_marker(wps_state.id3->elapsed);
1056 ab_jump_to_A_marker();
1058 else
1059 #endif
1061 change_dir(1);
1063 break;
1064 case ACTION_WPS_ABSETA_PREVDIR:
1065 if (global_settings.party_mode)
1066 break;
1067 #if defined(AB_REPEAT_ENABLE)
1068 if (ab_repeat_mode_enabled())
1069 ab_set_A_marker(wps_state.id3->elapsed);
1070 else
1071 #endif
1073 change_dir(-1);
1075 break;
1076 /* menu key functions */
1077 case ACTION_WPS_MENU:
1078 gwps_leave_wps();
1079 return GO_TO_ROOT;
1080 break;
1083 #ifdef HAVE_QUICKSCREEN
1084 case ACTION_WPS_QUICKSCREEN:
1086 gwps_leave_wps();
1087 if (quick_screen_quick(button))
1088 return GO_TO_ROOT;
1089 restore = true;
1091 break;
1092 #endif /* HAVE_QUICKSCREEN */
1094 /* screen settings */
1095 #ifdef BUTTON_F3
1096 case ACTION_F3:
1098 gwps_leave_wps();
1099 if (quick_screen_f3(BUTTON_F3))
1100 return GO_TO_ROOT;
1101 restore = true;
1103 break;
1104 #endif /* BUTTON_F3 */
1106 /* pitch screen */
1107 #ifdef HAVE_PITCHSCREEN
1108 case ACTION_WPS_PITCHSCREEN:
1110 gwps_leave_wps();
1111 if (1 == gui_syncpitchscreen_run())
1112 return GO_TO_ROOT;
1113 restore = true;
1115 break;
1116 #endif /* HAVE_PITCHSCREEN */
1118 #ifdef AB_REPEAT_ENABLE
1119 /* reset A&B markers */
1120 case ACTION_WPS_ABRESET:
1121 if (ab_repeat_mode_enabled())
1123 ab_reset_markers();
1124 update = true;
1126 break;
1127 #endif /* AB_REPEAT_ENABLE */
1129 /* stop and exit wps */
1130 case ACTION_WPS_STOP:
1131 if (global_settings.party_mode)
1132 break;
1133 bookmark = true;
1134 exit = true;
1135 break;
1137 case ACTION_WPS_ID3SCREEN:
1139 gwps_leave_wps();
1140 if (browse_id3())
1141 return GO_TO_ROOT;
1142 restore = true;
1144 break;
1145 #ifdef HAVE_TOUCHSCREEN
1146 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1148 global_settings.playlist_shuffle =
1149 !global_settings.playlist_shuffle;
1150 #if CONFIG_CODEC == SWCODEC
1151 dsp_set_replaygain();
1152 #endif
1153 if (global_settings.playlist_shuffle)
1154 playlist_randomise(NULL, current_tick, true);
1155 else
1156 playlist_sort(NULL, true);
1158 break;
1159 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1161 const struct settings_list *rep_setting =
1162 find_setting(&global_settings.repeat_mode, NULL);
1163 option_select_next_val(rep_setting, false, true);
1164 audio_flush_and_reload_tracks();
1166 break;
1167 #endif /* HAVE_TOUCHSCREEN */
1168 /* this case is used by the softlock feature
1169 * it requests a full update here */
1170 case ACTION_REDRAW:
1171 wps_sync_data.do_full_update = true;
1172 break;
1173 case ACTION_NONE: /* Timeout, do a partial update */
1174 update = true;
1175 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1176 break;
1177 #ifdef HAVE_RECORDING
1178 case ACTION_WPS_REC:
1179 exit = true;
1180 break;
1181 #endif
1182 case SYS_POWEROFF:
1183 default_event_handler(SYS_POWEROFF);
1184 break;
1185 case ACTION_WPS_VIEW_PLAYLIST:
1186 gwps_leave_wps();
1187 return GO_TO_PLAYLIST_VIEWER;
1188 break;
1189 default:
1190 if(default_event_handler(button) == SYS_USB_CONNECTED)
1192 gwps_leave_wps();
1193 return GO_TO_ROOT;
1195 update = true;
1196 break;
1199 if (vol_changed)
1201 bool res = false;
1202 vol_changed = false;
1203 setvol();
1204 FOR_NB_SCREENS(i)
1206 if(update_onvol_change(&gui_wps[i]))
1207 res = true;
1209 if (res) {
1210 restore = true;
1211 restoretimer = RESTORE_WPS_NEXT_SECOND;
1216 if (restore &&
1217 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1218 TIME_AFTER(current_tick, restoretimer)))
1220 restore = false;
1221 restoretimer = RESTORE_WPS_INSTANTLY;
1222 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1223 add_event(LCD_EVENT_ACTIVATION, false, wps_lcd_activation_hook);
1224 #endif
1225 /* we remove the update delay since it's not very usable in the wps,
1226 * e.g. during volume changing or ffwd/rewind */
1227 sb_skin_set_update_delay(0);
1228 wps_sync_data.do_full_update = update = false;
1229 gwps_enter_wps();
1231 else if (wps_sync_data.do_full_update || update)
1233 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1234 gwps_caption_backlight(&wps_state);
1235 #endif
1236 FOR_NB_SCREENS(i)
1238 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1239 /* currently, all remotes are readable without backlight
1240 * so still update those */
1241 if (lcd_active() || (i != SCREEN_MAIN))
1242 #endif
1244 skin_update(&gui_wps[i], wps_sync_data.do_full_update ?
1245 WPS_REFRESH_ALL : WPS_REFRESH_NON_STATIC);
1248 wps_sync_data.do_full_update = false;
1249 update = false;
1252 if (exit) {
1253 #ifdef HAVE_LCD_CHARCELLS
1254 status_set_record(false);
1255 status_set_audio(false);
1256 #endif
1257 if (global_settings.fade_on_stop)
1258 fade(false, true);
1260 if (bookmark)
1261 bookmark_autobookmark();
1262 audio_stop();
1263 #ifdef AB_REPEAT_ENABLE
1264 ab_reset_markers();
1265 #endif
1266 gwps_leave_wps();
1267 #ifdef HAVE_RECORDING
1268 if (button == ACTION_WPS_REC)
1269 return GO_TO_RECSCREEN;
1270 #endif
1271 if (global_settings.browse_current)
1272 return GO_TO_PREVIOUS_BROWSER;
1273 return GO_TO_PREVIOUS;
1276 if (button && !IS_SYSEVENT(button) )
1277 storage_spin();
1279 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1282 /* this is called from the playback thread so NO DRAWING! */
1283 static void track_changed_callback(void *param)
1285 wps_state.id3 = (struct mp3entry*)param;
1286 wps_state.nid3 = audio_next_track();
1287 if (wps_state.id3->cuesheet)
1289 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1290 cue_spoof_id3(wps_state.id3->cuesheet, wps_state.id3);
1292 wps_sync_data.do_full_update = true;
1294 static void nextid3available_callback(void* param)
1296 (void)param;
1297 wps_state.nid3 = audio_next_track();
1298 wps_sync_data.do_full_update = true;
1302 static void wps_state_init(void)
1304 wps_state.ff_rewind = false;
1305 wps_state.paused = false;
1306 if(audio_status() & AUDIO_STATUS_PLAY)
1308 wps_state.id3 = audio_current_track();
1309 wps_state.nid3 = audio_next_track();
1311 else
1313 wps_state.id3 = NULL;
1314 wps_state.nid3 = NULL;
1316 /* We'll be updating due to restore initialized with true */
1317 wps_sync_data.do_full_update = false;
1318 /* add the WPS track event callbacks */
1319 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1320 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1324 void gui_sync_wps_init(void)
1326 int i;
1327 FOR_NB_SCREENS(i)
1329 #ifdef HAVE_ALBUMART
1330 wps_datas[i].albumart = NULL;
1331 wps_datas[i].playback_aa_slot = -1;
1332 #endif
1333 gui_wps[i].data = &wps_datas[i];
1334 gui_wps[i].display = &screens[i];
1335 /* Currently no seperate wps_state needed/possible
1336 so use the only available ( "global" ) one */
1337 gui_wps[i].state = &wps_state;
1338 /* must point to the same struct for both screens */
1339 gui_wps[i].sync_data = &wps_sync_data;
1344 #ifdef IPOD_ACCESSORY_PROTOCOL
1345 bool is_wps_fading(void)
1347 return wps_state.is_fading;
1350 int wps_get_ff_rewind_count(void)
1352 return wps_state.ff_rewind_count;
1354 #endif