Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / gui / wps.c
blob916ca2cbcf6c774e9b3b11cc08c92f0764fc802c
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 "settings.h"
35 #include "skin_engine/skin_engine.h"
36 #include "mp3_playback.h"
37 #include "audio.h"
38 #include "usb.h"
39 #include "status.h"
40 #include "storage.h"
41 #include "screens.h"
42 #include "playlist.h"
43 #ifdef HAVE_LCD_BITMAP
44 #include "icons.h"
45 #include "peakmeter.h"
46 #endif
47 #include "lang.h"
48 #include "bookmark.h"
49 #include "misc.h"
50 #include "sound.h"
51 #include "onplay.h"
52 #include "abrepeat.h"
53 #include "playback.h"
54 #include "splash.h"
55 #include "cuesheet.h"
56 #include "ata_idle_notify.h"
57 #include "root_menu.h"
58 #include "backdrop.h"
59 #include "quickscreen.h"
60 #include "pitchscreen.h"
61 #include "appevents.h"
62 #include "viewport.h"
63 #include "pcmbuf.h"
64 #include "option_select.h"
65 #include "dsp.h"
66 #include "playlist_viewer.h"
67 #include "wps.h"
68 #include "statusbar-skinned.h"
70 #define RESTORE_WPS_INSTANTLY 0l
71 #define RESTORE_WPS_NEXT_SECOND ((long)(HZ+current_tick))
73 #define FF_REWIND_MAX_PERCENT 3 /* cap ff/rewind step size at max % of file */
74 /* 3% of 30min file == 54s step size */
75 #define MIN_FF_REWIND_STEP 500
77 /* currently only one wps_state is needed, initialize to 0 */
78 struct wps_state wps_state = { .id3 = NULL };
79 static struct gui_wps gui_wps[NB_SCREENS] = {{ .data = NULL }};
80 static struct wps_data wps_datas[NB_SCREENS] = {{ .wps_loaded = 0 }};
81 static struct wps_sync_data wps_sync_data = { .do_full_update = false };
83 /* initial setup of wps_data */
84 static void wps_state_init(void);
85 static void track_changed_callback(void *param);
86 static void nextid3available_callback(void* param);
88 #ifdef HAVE_TOUCHSCREEN
89 static void wps_disarm_touchregions(struct wps_data *data);
90 #endif
92 #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps"
93 #ifdef HAVE_REMOTE_LCD
94 #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps"
95 #define DEFAULT_WPS(screen) ((screen) == SCREEN_MAIN ? \
96 WPS_DEFAULTCFG:RWPS_DEFAULTCFG)
97 #else
98 #define DEFAULT_WPS(screen) (WPS_DEFAULTCFG)
99 #endif
101 void wps_data_load(enum screen_type screen, const char *buf, bool isfile)
103 bool loaded_ok;
105 #ifndef __PCTOOL__
107 * Hardcode loading WPS_DEFAULTCFG to cause a reset ideally this
108 * wants to be a virtual file. Feel free to modify dirbrowse()
109 * if you're feeling brave.
112 if (buf && ! strcmp(buf, DEFAULT_WPS(screen)) )
114 #ifdef HAVE_REMOTE_LCD
115 if (screen == SCREEN_REMOTE)
116 global_settings.rwps_file[0] = '\0';
117 else
118 #endif
119 global_settings.wps_file[0] = '\0';
120 buf = NULL;
123 #endif /* __PCTOOL__ */
125 loaded_ok = buf && skin_data_load(screen, gui_wps[screen].data, buf, isfile);
127 if (!loaded_ok) /* load the hardcoded default */
129 char *skin_buf[NB_SCREENS] = {
130 #ifdef HAVE_LCD_BITMAP
131 #if LCD_DEPTH > 1
132 "%Xd\n"
133 #endif
134 "%s%?it<%?in<%in. |>%it|%fn>\n"
135 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
136 "%s%?id<%id|%?d1<%d1|(root)>> %?iy<(%iy)|>\n\n"
137 "%al%pc/%pt%ar[%pp:%pe]\n"
138 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
139 "%pb\n%pm\n",
140 #else
141 "%s%pp/%pe: %?it<%it|%fn> - %?ia<%ia|%d2> - %?id<%id|%d1>\n"
142 "%pc%?ps<*|/>%pt\n",
143 #endif
144 #ifdef HAVE_REMOTE_LCD
145 #if LCD_REMOTE_DEPTH > 1
146 "%Xd\n"
147 #endif
148 "%s%?ia<%ia|%?d2<%d2|(root)>>\n"
149 "%s%?it<%?in<%in. |>%it|%fn>\n"
150 "%al%pc/%pt%ar[%pp:%pe]\n"
151 "%fbkBit %?fv<avg|> %?iv<(id3v%iv)|(no id3)>\n"
152 "%pb\n",
153 #endif
155 skin_data_load(screen, gui_wps[screen].data, skin_buf[screen], false);
159 void fade(bool fade_in, bool updatewps)
161 int fp_global_vol = global_settings.volume << 8;
162 int fp_min_vol = sound_min(SOUND_VOLUME) << 8;
163 int fp_step = (fp_global_vol - fp_min_vol) / 30;
164 int i;
165 wps_state.is_fading = !fade_in;
166 if (fade_in) {
167 /* fade in */
168 int fp_volume = fp_min_vol;
170 /* zero out the sound */
171 sound_set_volume(fp_min_vol >> 8);
173 sleep(HZ/10); /* let audio thread run */
174 audio_resume();
176 while (fp_volume < fp_global_vol - fp_step) {
177 fp_volume += fp_step;
178 sound_set_volume(fp_volume >> 8);
179 if (updatewps)
181 FOR_NB_SCREENS(i)
182 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
184 sleep(1);
186 sound_set_volume(global_settings.volume);
188 else {
189 /* fade out */
190 int fp_volume = fp_global_vol;
192 while (fp_volume > fp_min_vol + fp_step) {
193 fp_volume -= fp_step;
194 sound_set_volume(fp_volume >> 8);
195 if (updatewps)
197 FOR_NB_SCREENS(i)
198 skin_update(&gui_wps[i], WPS_REFRESH_NON_STATIC);
200 sleep(1);
202 audio_pause();
203 wps_state.is_fading = false;
204 #if CONFIG_CODEC != SWCODEC
205 #ifndef SIMULATOR
206 /* let audio thread run and wait for the mas to run out of data */
207 while (!mp3_pause_done())
208 #endif
209 sleep(HZ/10);
210 #endif
212 /* reset volume to what it was before the fade */
213 sound_set_volume(global_settings.volume);
217 static bool update_onvol_change(struct gui_wps * gwps)
219 skin_update(gwps, WPS_REFRESH_NON_STATIC);
221 #ifdef HAVE_LCD_CHARCELLS
222 splashf(0, "Vol: %3d dB",
223 sound_val2phys(SOUND_VOLUME, global_settings.volume));
224 return true;
225 #endif
226 return false;
230 bool ffwd_rew(int button)
232 unsigned int step = 0; /* current ff/rewind step */
233 unsigned int max_step = 0; /* maximum ff/rewind step */
234 int ff_rewind_count = 0; /* current ff/rewind count (in ticks) */
235 int direction = -1; /* forward=1 or backward=-1 */
236 bool exit = false;
237 bool usb = false;
238 int i = 0;
239 const long ff_rw_accel = (global_settings.ff_rewind_accel + 3);
241 if (button == ACTION_NONE)
243 status_set_ffmode(0);
244 return usb;
246 while (!exit)
248 switch ( button )
250 case ACTION_WPS_SEEKFWD:
251 direction = 1;
252 case ACTION_WPS_SEEKBACK:
253 if (wps_state.ff_rewind)
255 if (direction == 1)
257 /* fast forwarding, calc max step relative to end */
258 max_step = (wps_state.id3->length -
259 (wps_state.id3->elapsed +
260 ff_rewind_count)) *
261 FF_REWIND_MAX_PERCENT / 100;
263 else
265 /* rewinding, calc max step relative to start */
266 max_step = (wps_state.id3->elapsed + ff_rewind_count) *
267 FF_REWIND_MAX_PERCENT / 100;
270 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
272 if (step > max_step)
273 step = max_step;
275 ff_rewind_count += step * direction;
277 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
278 step += step >> ff_rw_accel;
280 else
282 if ( (audio_status() & AUDIO_STATUS_PLAY) &&
283 wps_state.id3 && wps_state.id3->length )
285 if (!wps_state.paused)
286 #if (CONFIG_CODEC == SWCODEC)
287 audio_pre_ff_rewind();
288 #else
289 audio_pause();
290 #endif
291 #if CONFIG_KEYPAD == PLAYER_PAD
292 FOR_NB_SCREENS(i)
293 gui_wps[i].display->stop_scroll();
294 #endif
295 if (direction > 0)
296 status_set_ffmode(STATUS_FASTFORWARD);
297 else
298 status_set_ffmode(STATUS_FASTBACKWARD);
300 wps_state.ff_rewind = true;
302 step = 1000 * global_settings.ff_rewind_min_step;
304 else
305 break;
308 if (direction > 0) {
309 if ((wps_state.id3->elapsed + ff_rewind_count) >
310 wps_state.id3->length)
311 ff_rewind_count = wps_state.id3->length -
312 wps_state.id3->elapsed;
314 else {
315 if ((int)(wps_state.id3->elapsed + ff_rewind_count) < 0)
316 ff_rewind_count = -wps_state.id3->elapsed;
319 /* set the wps state ff_rewind_count so the progess info
320 displays corectly */
321 wps_state.ff_rewind_count = (wps_state.wps_time_countup == false)?
322 ff_rewind_count:-ff_rewind_count;
323 FOR_NB_SCREENS(i)
325 skin_update(&gui_wps[i],
326 WPS_REFRESH_PLAYER_PROGRESS |
327 WPS_REFRESH_DYNAMIC);
330 break;
332 case ACTION_WPS_STOPSEEK:
333 wps_state.id3->elapsed = wps_state.id3->elapsed+ff_rewind_count;
334 audio_ff_rewind(wps_state.id3->elapsed);
335 wps_state.ff_rewind_count = 0;
336 wps_state.ff_rewind = false;
337 status_set_ffmode(0);
338 #if (CONFIG_CODEC != SWCODEC)
339 if (!wps_state.paused)
340 audio_resume();
341 #endif
342 #ifdef HAVE_LCD_CHARCELLS
343 FOR_NB_SCREENS(i)
344 skin_update(&gui_wps[i], WPS_REFRESH_ALL);
345 #endif
346 exit = true;
347 break;
349 default:
350 if(default_event_handler(button) == SYS_USB_CONNECTED) {
351 status_set_ffmode(0);
352 usb = true;
353 exit = true;
355 break;
357 if (!exit)
359 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_BLOCK);
360 #ifdef HAVE_TOUCHSCREEN
361 if (button == ACTION_TOUCHSCREEN)
362 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
363 #endif
366 return usb;
370 void display_keylock_text(bool locked)
372 int i;
373 FOR_NB_SCREENS(i)
374 gui_wps[i].display->stop_scroll();
376 splash(HZ, locked ? ID2P(LANG_KEYLOCK_ON) : ID2P(LANG_KEYLOCK_OFF));
382 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
383 static void gwps_caption_backlight(struct wps_state *state)
385 if (state && state->id3)
387 #ifdef HAVE_BACKLIGHT
388 if (global_settings.caption_backlight)
390 /* turn on backlight n seconds before track ends, and turn it off n
391 seconds into the new track. n == backlight_timeout, or 5s */
392 int n = global_settings.backlight_timeout * 1000;
394 if ( n < 1000 )
395 n = 5000; /* use 5s if backlight is always on or off */
397 if (((state->id3->elapsed < 1000) ||
398 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
399 (state->paused == false))
400 backlight_on();
402 #endif
403 #ifdef HAVE_REMOTE_LCD
404 if (global_settings.remote_caption_backlight)
406 /* turn on remote backlight n seconds before track ends, and turn it
407 off n seconds into the new track. n == remote_backlight_timeout,
408 or 5s */
409 int n = global_settings.remote_backlight_timeout * 1000;
411 if ( n < 1000 )
412 n = 5000; /* use 5s if backlight is always on or off */
414 if (((state->id3->elapsed < 1000) ||
415 ((state->id3->length - state->id3->elapsed) < (unsigned)n)) &&
416 (state->paused == false))
417 remote_backlight_on();
419 #endif
422 #endif
425 static void change_dir(int direction)
427 if (global_settings.prevent_skip)
428 return;
430 if (direction < 0)
431 audio_prev_dir();
432 else if (direction > 0)
433 audio_next_dir();
434 /* prevent the next dir to immediatly start being ffw'd */
435 action_wait_for_release();
438 static void prev_track(unsigned long skip_thresh)
440 if (wps_state.id3->elapsed < skip_thresh)
442 audio_prev();
443 return;
445 else
447 if (wps_state.id3->cuesheet)
449 curr_cuesheet_skip(wps_state.id3->cuesheet, -1, wps_state.id3->elapsed);
450 return;
453 if (!wps_state.paused)
454 #if (CONFIG_CODEC == SWCODEC)
455 audio_pre_ff_rewind();
456 #else
457 audio_pause();
458 #endif
460 audio_ff_rewind(0);
462 #if (CONFIG_CODEC != SWCODEC)
463 if (!wps_state.paused)
464 audio_resume();
465 #endif
469 static void next_track(void)
471 /* take care of if we're playing a cuesheet */
472 if (wps_state.id3->cuesheet)
474 if (curr_cuesheet_skip(wps_state.id3->cuesheet, 1, wps_state.id3->elapsed))
476 /* if the result was false, then we really want
477 to skip to the next track */
478 return;
482 audio_next();
485 static void play_hop(int direction)
487 long step = global_settings.skip_length*1000;
488 long elapsed = wps_state.id3->elapsed;
489 long remaining = wps_state.id3->length - elapsed;
491 if (step < 0)
493 if (direction < 0)
495 prev_track(DEFAULT_SKIP_TRESH);
496 return;
498 else if (remaining < DEFAULT_SKIP_TRESH*2)
500 next_track();
501 return;
503 else
504 elapsed += (remaining - DEFAULT_SKIP_TRESH*2);
506 else if (!global_settings.prevent_skip &&
507 (!step ||
508 (direction > 0 && step >= remaining) ||
509 (direction < 0 && elapsed < DEFAULT_SKIP_TRESH)))
510 { /* Do normal track skipping */
511 if (direction > 0)
512 next_track();
513 else if (direction < 0)
514 prev_track(DEFAULT_SKIP_TRESH);
515 return;
517 else if (direction == 1 && step >= remaining)
519 #if CONFIG_CODEC == SWCODEC
520 if(global_settings.beep)
521 pcmbuf_beep(1000, 150, 1500*global_settings.beep);
522 #endif
523 return;
525 else if ((direction == -1 && elapsed < step))
527 elapsed = 0;
529 else
531 elapsed += step * direction;
533 if((audio_status() & AUDIO_STATUS_PLAY) && !wps_state.paused)
535 #if (CONFIG_CODEC == SWCODEC)
536 audio_pre_ff_rewind();
537 #else
538 audio_pause();
539 #endif
541 audio_ff_rewind(wps_state.id3->elapsed = elapsed);
542 #if (CONFIG_CODEC != SWCODEC)
543 if (!wps_state.paused)
544 audio_resume();
545 #endif
549 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
551 * If the user is unable to see the wps, because the display is deactivated,
552 * we suppress updates until the wps is activated again (the lcd driver will
553 * call this hook to issue an instant update)
554 * */
555 static void wps_lcd_activation_hook(void *param)
557 (void)param;
558 wps_sync_data.do_full_update = true;
559 /* force timeout in wps main loop, so that the update is instantly */
560 queue_post(&button_queue, BUTTON_NONE, 0);
562 #endif
564 static void gwps_leave_wps(void)
566 int i;
568 FOR_NB_SCREENS(i)
570 gui_wps[i].display->stop_scroll();
571 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
572 gui_wps[i].display->backdrop_show(sb_get_backdrop(i));
573 #endif
575 #ifdef HAVE_LCD_BITMAP
576 bool draw = false;
577 if (gui_wps[i].data->wps_sb_tag)
578 draw = gui_wps[i].data->show_sb_on_wps;
579 else if (statusbar_position(i) != STATUSBAR_OFF)
580 draw = true;
581 #endif
582 viewportmanager_theme_undo(i, draw);
586 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
587 /* Play safe and unregister the hook */
588 remove_event(LCD_EVENT_ACTIVATION, wps_lcd_activation_hook);
589 #endif
590 /* unhandle statusbar update delay */
591 sb_skin_set_update_delay(DEFAULT_UPDATE_DELAY);
595 * display the wps on entering or restoring */
596 static void gwps_enter_wps(void)
598 int i;
599 FOR_NB_SCREENS(i)
601 struct gui_wps *gwps = &gui_wps[i];
602 struct screen *display = gwps->display;
603 #ifdef HAVE_LCD_BITMAP
604 bool draw = false;
605 if (gui_wps[i].data->wps_sb_tag)
606 draw = gui_wps[i].data->show_sb_on_wps;
607 else if (statusbar_position(i) != STATUSBAR_OFF)
608 draw = true;
609 #endif
610 display->stop_scroll();
611 viewportmanager_theme_enable(i, draw, NULL);
613 /* Update the values in the first (default) viewport - in case the user
614 has modified the statusbar or colour settings */
615 #if LCD_DEPTH > 1
616 if (display->depth > 1)
618 struct viewport *vp = &find_viewport(VP_DEFAULT_LABEL, gwps->data)->vp;
619 vp->fg_pattern = display->get_foreground();
620 vp->bg_pattern = display->get_background();
622 #endif
623 /* make the backdrop actually take effect */
624 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
625 display->backdrop_show(gwps->data->backdrop);
626 #endif
627 display->clear_display();
628 skin_update(gwps, WPS_REFRESH_ALL);
630 #ifdef HAVE_TOUCHSCREEN
631 wps_disarm_touchregions(gui_wps[i].data);
632 #endif
634 /* force statusbar/skin update since we just cleared the whole screen */
635 send_event(GUI_EVENT_ACTIONUPDATE, (void*)1);
638 #ifdef HAVE_TOUCHSCREEN
639 /** Disarms all touchregions. */
640 static void wps_disarm_touchregions(struct wps_data *data)
642 struct skin_token_list *regions = data->touchregions;
643 while (regions)
645 ((struct touchregion *)regions->token->value.data)->armed = false;
646 regions = regions->next;
650 int wps_get_touchaction(struct wps_data *data)
652 int returncode = ACTION_NONE;
653 short x,y;
654 short vx, vy;
655 int type = action_get_touchscreen_press(&x, &y);
656 static int last_action = ACTION_NONE;
657 struct touchregion *r;
658 bool repeated = (type == BUTTON_REPEAT);
659 bool released = (type == BUTTON_REL);
660 bool pressed = (type == BUTTON_TOUCHSCREEN);
661 struct skin_token_list *regions = data->touchregions;
663 while (regions)
665 r = (struct touchregion *)regions->token->value.data;
666 /* make sure this region's viewport is visible */
667 if (r->wvp->hidden_flags&VP_DRAW_HIDDEN)
669 regions = regions->next;
670 continue;
672 /* check if it's inside this viewport */
673 if (viewport_point_within_vp(&(r->wvp->vp), x, y))
674 { /* reposition the touch inside the viewport since touchregions
675 * are relative to a preceding viewport */
676 vx = x - r->wvp->vp.x;
677 vy = y - r->wvp->vp.y;
678 /* now see if the point is inside this region */
679 if (vx >= r->x && vx < r->x+r->width &&
680 vy >= r->y && vy < r->y+r->height)
682 /* reposition the touch within the area */
683 vx -= r->x;
684 vy -= r->y;
686 switch(r->type)
688 case WPS_TOUCHREGION_ACTION:
689 if (r->armed && ((repeated && r->repeat) || (released && !r->repeat)))
691 last_action = r->action;
692 returncode = r->action;
694 if (pressed)
695 r->armed = true;
696 break;
697 case WPS_TOUCHREGION_SCROLLBAR:
698 if(r->width > r->height)
699 /* landscape */
700 wps_state.id3->elapsed = (vx *
701 wps_state.id3->length) / r->width;
702 else
703 /* portrait */
704 wps_state.id3->elapsed = (vy *
705 wps_state.id3->length) / r->height;
707 if (!wps_state.paused)
708 #if (CONFIG_CODEC == SWCODEC)
709 audio_pre_ff_rewind();
710 #else
711 audio_pause();
712 #endif
713 audio_ff_rewind(wps_state.id3->elapsed);
714 #if (CONFIG_CODEC != SWCODEC)
715 if (!wps_state.paused)
716 audio_resume();
717 #endif
718 break;
719 case WPS_TOUCHREGION_VOLUME:
721 const int min_vol = sound_min(SOUND_VOLUME);
722 const int max_vol = sound_max(SOUND_VOLUME);
723 if(r->width > r->height)
724 /* landscape */
725 global_settings.volume = (vx *
726 (max_vol - min_vol)) / r->width;
727 else
728 /* portrait */
729 global_settings.volume = ((r->height - vy) *
730 (max_vol-min_vol)) / r->height;
732 global_settings.volume += min_vol;
733 setvol();
734 returncode = ACTION_REDRAW;
739 regions = regions->next;
742 /* On release, all regions are disarmed. */
743 if (released)
744 wps_disarm_touchregions(data);
746 if (returncode != ACTION_NONE)
747 return returncode;
749 if ((last_action == ACTION_WPS_SEEKBACK || last_action == ACTION_WPS_SEEKFWD))
750 return ACTION_WPS_STOPSEEK;
751 last_action = ACTION_TOUCHSCREEN;
752 return ACTION_TOUCHSCREEN;
754 #endif
755 /* The WPS can be left in two ways:
756 * a) call a function, which draws over the wps. In this case, the wps
757 * will be still active (i.e. the below function didn't return)
758 * b) return with a value evaluated by root_menu.c, in this case the wps
759 * is really left, and root_menu will handle the next screen
761 * In either way, call gwps_leave_wps(), in order to restore the correct
762 * "main screen" backdrops and statusbars
764 long gui_wps_show(void)
766 long button = 0;
767 bool restore = true;
768 long restoretimer = RESTORE_WPS_INSTANTLY; /* timer to delay screen redraw temporarily */
769 bool exit = false;
770 bool bookmark = false;
771 bool update = false;
772 bool vol_changed = false;
773 int i;
774 long last_left = 0, last_right = 0;
776 #ifdef HAVE_LCD_CHARCELLS
777 status_set_audio(true);
778 status_set_param(false);
779 #endif
781 #ifdef AB_REPEAT_ENABLE
782 ab_repeat_init();
783 ab_reset_markers();
784 #endif
785 wps_state_init();
787 while ( 1 )
789 bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false;
791 /* did someone else (i.e power thread) change audio pause mode? */
792 if (wps_state.paused != audio_paused) {
793 wps_state.paused = audio_paused;
795 /* if another thread paused audio, we are probably in car mode,
796 about to shut down. lets save the settings. */
797 if (wps_state.paused) {
798 settings_save();
799 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
800 call_storage_idle_notifys(true);
801 #endif
804 #ifdef HAVE_LCD_BITMAP
805 /* when the peak meter is enabled we want to have a
806 few extra updates to make it look smooth. On the
807 other hand we don't want to waste energy if it
808 isn't displayed */
809 bool pm=false;
810 FOR_NB_SCREENS(i)
812 if(gui_wps[i].data->peak_meter_enabled)
813 pm = true;
816 if (pm) {
817 long next_refresh = current_tick;
818 long next_big_refresh = current_tick + HZ / 5;
819 button = BUTTON_NONE;
820 while (TIME_BEFORE(current_tick, next_big_refresh)) {
821 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,TIMEOUT_NOBLOCK);
822 /* check for restore to not let the peakmeter delay the
823 * initial draw of the wps, don't delay handling of button
824 * presses either */
825 if (button != ACTION_NONE || restore) {
826 break;
828 peak_meter_peek();
829 sleep(0); /* Sleep until end of current tick. */
831 if (TIME_AFTER(current_tick, next_refresh)) {
832 FOR_NB_SCREENS(i)
834 if(gui_wps[i].data->peak_meter_enabled)
835 skin_update(&gui_wps[i], WPS_REFRESH_PEAK_METER);
836 next_refresh += HZ / PEAK_METER_FPS;
843 /* The peak meter is disabled
844 -> no additional screen updates needed */
845 else
846 #endif
847 { /* 1 is the minimum timeout which lets other threads run.
848 * audio thread (apprently) needs to run before displaying the wps
849 * or bad things happen with regards to cuesheet
850 * (probably a race condition, on sh at least) */
851 button = get_action(CONTEXT_WPS|ALLOW_SOFTLOCK,
852 restore ? 1 : HZ/5);
855 /* Exit if audio has stopped playing. This happens e.g. at end of
856 playlist or if using the sleep timer. */
857 if (!(audio_status() & AUDIO_STATUS_PLAY))
858 exit = true;
859 #ifdef HAVE_TOUCHSCREEN
860 if (button == ACTION_TOUCHSCREEN)
861 button = wps_get_touchaction(gui_wps[SCREEN_MAIN].data);
862 #endif
863 /* The iPods/X5/M5 use a single button for the A-B mode markers,
864 defined as ACTION_WPSAB_SINGLE in their config files. */
865 #ifdef ACTION_WPSAB_SINGLE
866 if (!global_settings.party_mode && ab_repeat_mode_enabled())
868 static int wps_ab_state = 0;
869 if (button == ACTION_WPSAB_SINGLE)
871 switch (wps_ab_state)
873 case 0: /* set the A spot */
874 button = ACTION_WPS_ABSETA_PREVDIR;
875 break;
876 case 1: /* set the B spot */
877 button = ACTION_WPS_ABSETB_NEXTDIR;
878 break;
879 case 2:
880 button = ACTION_WPS_ABRESET;
881 break;
883 wps_ab_state = (wps_ab_state+1) % 3;
886 #endif
887 switch(button)
889 case ACTION_WPS_CONTEXT:
890 case ACTION_WPS_HOTKEY:
892 bool hotkey = button == ACTION_WPS_HOTKEY;
893 gwps_leave_wps();
894 int retval = onplay(wps_state.id3->path,
895 FILE_ATTR_AUDIO, CONTEXT_WPS, hotkey);
896 /* if music is stopped in the context menu we want to exit the wps */
897 if (retval == ONPLAY_MAINMENU
898 || !audio_status())
899 return GO_TO_ROOT;
900 else if (retval == ONPLAY_PLAYLIST)
901 return GO_TO_PLAYLIST_VIEWER;
902 restore = true;
904 break;
906 case ACTION_WPS_BROWSE:
907 #ifdef HAVE_LCD_CHARCELLS
908 status_set_record(false);
909 status_set_audio(false);
910 #endif
911 gwps_leave_wps();
912 return GO_TO_PREVIOUS_BROWSER;
913 break;
915 /* play/pause */
916 case ACTION_WPS_PLAY:
917 if (global_settings.party_mode)
918 break;
919 if ( wps_state.paused )
921 wps_state.paused = false;
922 if ( global_settings.fade_on_stop )
923 fade(true, true);
924 else
925 audio_resume();
927 else
929 wps_state.paused = true;
930 if ( global_settings.fade_on_stop )
931 fade(false, true);
932 else
933 audio_pause();
934 settings_save();
935 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
936 call_storage_idle_notifys(true); /* make sure resume info is saved */
937 #endif
939 break;
941 case ACTION_WPS_VOLUP:
942 global_settings.volume++;
943 vol_changed = true;
944 break;
945 case ACTION_WPS_VOLDOWN:
946 global_settings.volume--;
947 vol_changed = true;
948 break;
949 /* fast forward
950 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
951 case ACTION_WPS_SEEKFWD:
952 if (global_settings.party_mode)
953 break;
954 if (current_tick -last_right < HZ)
956 if (wps_state.id3->cuesheet)
958 audio_next();
960 else
962 change_dir(1);
965 else
966 ffwd_rew(ACTION_WPS_SEEKFWD);
967 last_right = last_left = 0;
968 break;
969 /* fast rewind
970 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
971 case ACTION_WPS_SEEKBACK:
972 if (global_settings.party_mode)
973 break;
974 if (current_tick -last_left < HZ)
976 if (wps_state.id3->cuesheet)
978 if (!wps_state.paused)
979 #if (CONFIG_CODEC == SWCODEC)
980 audio_pre_ff_rewind();
981 #else
982 audio_pause();
983 #endif
984 audio_ff_rewind(0);
986 else
988 change_dir(-1);
991 else
992 ffwd_rew(ACTION_WPS_SEEKBACK);
993 last_left = last_right = 0;
994 break;
996 /* prev / restart */
997 case ACTION_WPS_SKIPPREV:
998 if (global_settings.party_mode)
999 break;
1000 last_left = current_tick;
1001 #ifdef AB_REPEAT_ENABLE
1002 /* if we're in A/B repeat mode and the current position
1003 is past the A marker, jump back to the A marker... */
1004 if ( ab_repeat_mode_enabled() )
1006 if ( ab_after_A_marker(wps_state.id3->elapsed) )
1008 ab_jump_to_A_marker();
1009 break;
1010 #if (AB_REPEAT_ENABLE == 2)
1011 } else {
1012 ab_reset_markers();
1013 #endif
1016 else
1017 /* ...otherwise, do it normally */
1018 #endif
1019 play_hop(-1);
1020 break;
1022 /* next
1023 OR if skip length set, hop by predetermined amount. */
1024 case ACTION_WPS_SKIPNEXT:
1025 if (global_settings.party_mode)
1026 break;
1027 last_right = current_tick;
1028 #ifdef AB_REPEAT_ENABLE
1029 /* if we're in A/B repeat mode and the current position is
1030 before the A marker, jump to the A marker... */
1031 if ( ab_repeat_mode_enabled() )
1033 if ( ab_before_A_marker(wps_state.id3->elapsed) )
1035 ab_jump_to_A_marker();
1036 break;
1037 #if (AB_REPEAT_ENABLE == 2)
1038 } else {
1039 ab_reset_markers();
1040 #endif
1043 else
1044 /* ...otherwise, do it normally */
1045 #endif
1046 play_hop(1);
1047 break;
1048 /* next / prev directories */
1049 /* and set A-B markers if in a-b mode */
1050 case ACTION_WPS_ABSETB_NEXTDIR:
1051 if (global_settings.party_mode)
1052 break;
1053 #if defined(AB_REPEAT_ENABLE)
1054 if (ab_repeat_mode_enabled())
1056 ab_set_B_marker(wps_state.id3->elapsed);
1057 ab_jump_to_A_marker();
1059 else
1060 #endif
1062 change_dir(1);
1064 break;
1065 case ACTION_WPS_ABSETA_PREVDIR:
1066 if (global_settings.party_mode)
1067 break;
1068 #if defined(AB_REPEAT_ENABLE)
1069 if (ab_repeat_mode_enabled())
1070 ab_set_A_marker(wps_state.id3->elapsed);
1071 else
1072 #endif
1074 change_dir(-1);
1076 break;
1077 /* menu key functions */
1078 case ACTION_WPS_MENU:
1079 gwps_leave_wps();
1080 return GO_TO_ROOT;
1081 break;
1084 #ifdef HAVE_QUICKSCREEN
1085 case ACTION_WPS_QUICKSCREEN:
1087 gwps_leave_wps();
1088 if (quick_screen_quick(button))
1089 return GO_TO_ROOT;
1090 restore = true;
1092 break;
1093 #endif /* HAVE_QUICKSCREEN */
1095 /* screen settings */
1096 #ifdef BUTTON_F3
1097 case ACTION_F3:
1099 gwps_leave_wps();
1100 if (quick_screen_f3(BUTTON_F3))
1101 return GO_TO_ROOT;
1102 restore = true;
1104 break;
1105 #endif /* BUTTON_F3 */
1107 /* pitch screen */
1108 #ifdef HAVE_PITCHSCREEN
1109 case ACTION_WPS_PITCHSCREEN:
1111 gwps_leave_wps();
1112 if (1 == gui_syncpitchscreen_run())
1113 return GO_TO_ROOT;
1114 restore = true;
1116 break;
1117 #endif /* HAVE_PITCHSCREEN */
1119 #ifdef AB_REPEAT_ENABLE
1120 /* reset A&B markers */
1121 case ACTION_WPS_ABRESET:
1122 if (ab_repeat_mode_enabled())
1124 ab_reset_markers();
1125 update = true;
1127 break;
1128 #endif /* AB_REPEAT_ENABLE */
1130 /* stop and exit wps */
1131 case ACTION_WPS_STOP:
1132 if (global_settings.party_mode)
1133 break;
1134 bookmark = true;
1135 exit = true;
1136 break;
1138 case ACTION_WPS_ID3SCREEN:
1140 gwps_leave_wps();
1141 if (browse_id3())
1142 return GO_TO_ROOT;
1143 restore = true;
1145 break;
1146 #ifdef HAVE_TOUCHSCREEN
1147 case ACTION_TOUCH_SHUFFLE: /* toggle shuffle mode */
1149 global_settings.playlist_shuffle =
1150 !global_settings.playlist_shuffle;
1151 #if CONFIG_CODEC == SWCODEC
1152 dsp_set_replaygain();
1153 #endif
1154 if (global_settings.playlist_shuffle)
1155 playlist_randomise(NULL, current_tick, true);
1156 else
1157 playlist_sort(NULL, true);
1159 break;
1160 case ACTION_TOUCH_REPMODE: /* cycle the repeat mode setting */
1162 const struct settings_list *rep_setting =
1163 find_setting(&global_settings.repeat_mode, NULL);
1164 option_select_next_val(rep_setting, false, true);
1165 audio_flush_and_reload_tracks();
1167 break;
1168 #endif /* HAVE_TOUCHSCREEN */
1169 /* this case is used by the softlock feature
1170 * it requests a full update here */
1171 case ACTION_REDRAW:
1172 wps_sync_data.do_full_update = true;
1173 break;
1174 case ACTION_NONE: /* Timeout, do a partial update */
1175 update = true;
1176 ffwd_rew(button); /* hopefully fix the ffw/rwd bug */
1177 break;
1178 #ifdef HAVE_RECORDING
1179 case ACTION_WPS_REC:
1180 exit = true;
1181 break;
1182 #endif
1183 case SYS_POWEROFF:
1184 default_event_handler(SYS_POWEROFF);
1185 break;
1186 case ACTION_WPS_VIEW_PLAYLIST:
1187 gwps_leave_wps();
1188 return GO_TO_PLAYLIST_VIEWER;
1189 break;
1190 default:
1191 if(default_event_handler(button) == SYS_USB_CONNECTED)
1193 gwps_leave_wps();
1194 return GO_TO_ROOT;
1196 update = true;
1197 break;
1200 if (vol_changed)
1202 bool res = false;
1203 vol_changed = false;
1204 setvol();
1205 FOR_NB_SCREENS(i)
1207 if(update_onvol_change(&gui_wps[i]))
1208 res = true;
1210 if (res) {
1211 restore = true;
1212 restoretimer = RESTORE_WPS_NEXT_SECOND;
1217 if (restore &&
1218 ((restoretimer == RESTORE_WPS_INSTANTLY) ||
1219 TIME_AFTER(current_tick, restoretimer)))
1221 restore = false;
1222 restoretimer = RESTORE_WPS_INSTANTLY;
1223 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1224 add_event(LCD_EVENT_ACTIVATION, false, wps_lcd_activation_hook);
1225 #endif
1226 /* we remove the update delay since it's not very usable in the wps,
1227 * e.g. during volume changing or ffwd/rewind */
1228 sb_skin_set_update_delay(0);
1229 wps_sync_data.do_full_update = update = false;
1230 gwps_enter_wps();
1232 else if (wps_sync_data.do_full_update || update)
1234 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
1235 gwps_caption_backlight(&wps_state);
1236 #endif
1237 FOR_NB_SCREENS(i)
1239 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1240 /* currently, all remotes are readable without backlight
1241 * so still update those */
1242 if (lcd_active() || (i != SCREEN_MAIN))
1243 #endif
1245 skin_update(&gui_wps[i], wps_sync_data.do_full_update ?
1246 WPS_REFRESH_ALL : WPS_REFRESH_NON_STATIC);
1249 wps_sync_data.do_full_update = false;
1250 update = false;
1253 if (exit) {
1254 #ifdef HAVE_LCD_CHARCELLS
1255 status_set_record(false);
1256 status_set_audio(false);
1257 #endif
1258 if (global_settings.fade_on_stop)
1259 fade(false, true);
1261 if (bookmark)
1262 bookmark_autobookmark(true);
1263 audio_stop();
1264 #ifdef AB_REPEAT_ENABLE
1265 ab_reset_markers();
1266 #endif
1267 gwps_leave_wps();
1268 #ifdef HAVE_RECORDING
1269 if (button == ACTION_WPS_REC)
1270 return GO_TO_RECSCREEN;
1271 #endif
1272 if (global_settings.browse_current)
1273 return GO_TO_PREVIOUS_BROWSER;
1274 return GO_TO_PREVIOUS;
1277 if (button && !IS_SYSEVENT(button) )
1278 storage_spin();
1280 return GO_TO_ROOT; /* unreachable - just to reduce compiler warnings */
1283 /* this is called from the playback thread so NO DRAWING! */
1284 static void track_changed_callback(void *param)
1286 wps_state.id3 = (struct mp3entry*)param;
1287 wps_state.nid3 = audio_next_track();
1288 if (wps_state.id3->cuesheet)
1290 cue_find_current_track(wps_state.id3->cuesheet, wps_state.id3->elapsed);
1291 cue_spoof_id3(wps_state.id3->cuesheet, wps_state.id3);
1293 wps_sync_data.do_full_update = true;
1295 static void nextid3available_callback(void* param)
1297 (void)param;
1298 wps_state.nid3 = audio_next_track();
1299 wps_sync_data.do_full_update = true;
1303 static void wps_state_init(void)
1305 wps_state.ff_rewind = false;
1306 wps_state.paused = false;
1307 if(audio_status() & AUDIO_STATUS_PLAY)
1309 wps_state.id3 = audio_current_track();
1310 wps_state.nid3 = audio_next_track();
1312 else
1314 wps_state.id3 = NULL;
1315 wps_state.nid3 = NULL;
1317 /* We'll be updating due to restore initialized with true */
1318 wps_sync_data.do_full_update = false;
1319 /* add the WPS track event callbacks */
1320 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
1321 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false, nextid3available_callback);
1325 void gui_sync_wps_init(void)
1327 int i;
1328 FOR_NB_SCREENS(i)
1330 #ifdef HAVE_ALBUMART
1331 wps_datas[i].albumart = NULL;
1332 wps_datas[i].playback_aa_slot = -1;
1333 #endif
1334 gui_wps[i].data = &wps_datas[i];
1335 gui_wps[i].display = &screens[i];
1336 /* Currently no seperate wps_state needed/possible
1337 so use the only available ( "global" ) one */
1338 gui_wps[i].state = &wps_state;
1339 /* must point to the same struct for both screens */
1340 gui_wps[i].sync_data = &wps_sync_data;
1345 #ifdef IPOD_ACCESSORY_PROTOCOL
1346 bool is_wps_fading(void)
1348 return wps_state.is_fading;
1351 int wps_get_ff_rewind_count(void)
1353 return wps_state.ff_rewind_count;
1355 #endif