1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
30 #include "backlight.h"
33 #include "filetypes.h"
38 #include "gwps-common.h"
45 #ifdef HAVE_LCD_BITMAP
47 #include "peakmeter.h"
58 #include "ata_idle_notify.h"
59 #include "root_menu.h"
61 #include "quickscreen.h"
62 #include "pitchscreen.h"
63 #include "appevents.h"
66 #include "option_select.h"
68 #include "playlist_viewer.h"
70 #define RESTORE_WPS_INSTANTLY 0l
71 #define RESTORE_WPS_NEXT_SECOND ((long)(HZ+current_tick))
73 #define DEFAULT_SKIP_TRESH 3000ul
76 /* currently only one wps_state is needed */
77 struct wps_state wps_state
;
78 struct gui_wps gui_wps
[NB_SCREENS
];
79 static struct wps_data wps_datas
[NB_SCREENS
];
81 /* initial setup of wps_data */
82 static void wps_state_init(void);
83 static void track_changed_callback(void *param
);
84 static void nextid3available_callback(void* param
);
87 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
88 static void gwps_caption_backlight(struct wps_state
*state
)
90 if (state
&& state
->id3
)
93 if (global_settings
.caption_backlight
)
95 /* turn on backlight n seconds before track ends, and turn it off n
96 seconds into the new track. n == backlight_timeout, or 5s */
97 int n
= global_settings
.backlight_timeout
* 1000;
100 n
= 5000; /* use 5s if backlight is always on or off */
102 if (((state
->id3
->elapsed
< 1000) ||
103 ((state
->id3
->length
- state
->id3
->elapsed
) < (unsigned)n
)) &&
104 (state
->paused
== false))
108 #ifdef HAVE_REMOTE_LCD
109 if (global_settings
.remote_caption_backlight
)
111 /* turn on remote backlight n seconds before track ends, and turn it
112 off n seconds into the new track. n == remote_backlight_timeout,
114 int n
= global_settings
.remote_backlight_timeout
* 1000;
117 n
= 5000; /* use 5s if backlight is always on or off */
119 if (((state
->id3
->elapsed
< 1000) ||
120 ((state
->id3
->length
- state
->id3
->elapsed
) < (unsigned)n
)) &&
121 (state
->paused
== false))
122 remote_backlight_on();
130 static void change_dir(int direction
)
132 if (global_settings
.prevent_skip
)
137 else if (direction
> 0)
139 /* prevent the next dir to immediatly start being ffw'd */
140 action_wait_for_release();
143 static void prev_track(unsigned long skip_thresh
)
145 if (wps_state
.id3
->elapsed
< skip_thresh
)
152 if (wps_state
.id3
->cuesheet
)
154 curr_cuesheet_skip(-1, wps_state
.id3
->elapsed
);
158 if (!wps_state
.paused
)
159 #if (CONFIG_CODEC == SWCODEC)
160 audio_pre_ff_rewind();
167 #if (CONFIG_CODEC != SWCODEC)
168 if (!wps_state
.paused
)
174 static void next_track(void)
176 /* take care of if we're playing a cuesheet */
177 if (wps_state
.id3
->cuesheet
)
179 if (curr_cuesheet_skip(1, wps_state
.id3
->elapsed
))
181 /* if the result was false, then we really want
182 to skip to the next track */
190 static void play_hop(int direction
)
192 unsigned long step
= ((unsigned long)global_settings
.skip_length
)*1000;
193 unsigned long elapsed
= wps_state
.id3
->elapsed
;
194 unsigned long remaining
= wps_state
.id3
->length
- elapsed
;
196 if (!global_settings
.prevent_skip
&&
198 (direction
> 0 && step
>= remaining
) ||
199 (direction
< 0 && elapsed
< DEFAULT_SKIP_TRESH
)))
200 { /* Do normal track skipping */
203 else if (direction
< 0)
204 prev_track(DEFAULT_SKIP_TRESH
);
208 if (direction
== 1 && step
>= remaining
)
210 #if CONFIG_CODEC == SWCODEC
211 if(global_settings
.beep
)
212 pcmbuf_beep(1000, 150, 1500*global_settings
.beep
);
216 else if ((direction
== -1 && elapsed
< step
))
222 elapsed
+= step
* direction
;
224 if((audio_status() & AUDIO_STATUS_PLAY
) && !wps_state
.paused
)
226 #if (CONFIG_CODEC == SWCODEC)
227 audio_pre_ff_rewind();
232 audio_ff_rewind(wps_state
.id3
->elapsed
= elapsed
);
233 #if (CONFIG_CODEC != SWCODEC)
234 if (!wps_state
.paused
)
239 static void gwps_fix_statusbars(void)
241 #ifdef HAVE_LCD_BITMAP
243 wpsbars
= VP_SB_HIDE_ALL
;
247 if (gui_wps
[i
].data
->wps_sb_tag
)
248 draw
= gui_wps
[i
].data
->show_sb_on_wps
;
249 else if (statusbar_position(i
) != STATUSBAR_OFF
)
252 wpsbars
|= (VP_SB_ONSCREEN(i
) | VP_SB_IGNORE_SETTING(i
));
255 wpsbars
= VP_SB_ALLSCREENS
;
259 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
261 * If the user is unable to see the wps, because the display is deactivated,
262 * we suppress updates until the wps is activated again (the lcd driver will
263 * call this hook to issue an instant update)
265 static void wps_lcd_activation_hook(void)
267 wps_state
.do_full_update
= true;
268 /* force timeout in wps main loop, so that the update is instantly */
269 queue_post(&button_queue
, BUTTON_NONE
, 0);
273 static void gwps_leave_wps(void)
275 int i
, oldbars
= VP_SB_HIDE_ALL
;
278 gui_wps
[i
].display
->stop_scroll();
279 if (global_settings
.statusbar
)
280 oldbars
= VP_SB_ALLSCREENS
;
283 show_main_backdrop();
285 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
286 show_remote_main_backdrop();
288 viewportmanager_set_statusbar(oldbars
);
289 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
290 /* Play safe and unregister the hook */
291 lcd_activation_set_hook(NULL
);
295 void gwps_draw_statusbars(void)
297 viewportmanager_set_statusbar(wpsbars
);
299 #ifdef HAVE_TOUCHSCREEN
300 int wps_get_touchaction(struct wps_data
*data
)
304 int type
= action_get_touchscreen_press(&x
, &y
);
306 static int last_action
= ACTION_NONE
;
307 struct touchregion
*r
;
308 bool repeated
= (type
== BUTTON_REPEAT
);
309 bool released
= (type
== BUTTON_REL
);
310 for (i
=0; i
<data
->touchregion_count
; i
++)
312 r
= &data
->touchregion
[i
];
313 /* make sure this region's viewport is visible */
314 if (r
->wvp
->hidden_flags
&VP_DRAW_HIDDEN
)
316 /* reposition the touch inside the viewport */
317 vx
= x
- r
->wvp
->vp
.x
;
318 vy
= y
- r
->wvp
->vp
.y
;
319 /* check if it's inside this viewport */
320 if (vx
>= 0 && vx
< r
->wvp
->vp
.x
+ r
->wvp
->vp
.width
&&
321 vy
>= 0 && vy
< r
->wvp
->vp
.y
+ r
->wvp
->vp
.height
)
323 /* now see if the point is inside this region */
324 if (vx
>= r
->x
&& vx
< r
->x
+r
->width
&&
325 vy
>= r
->y
&& vy
< r
->y
+r
->height
)
327 if ((repeated
&& r
->repeat
) ||
328 (released
&& !r
->repeat
))
330 last_action
= r
->action
;
336 if ((last_action
== ACTION_WPS_SEEKBACK
|| last_action
== ACTION_WPS_SEEKFWD
))
337 return ACTION_WPS_STOPSEEK
;
338 last_action
= ACTION_TOUCHSCREEN
;
339 return ACTION_TOUCHSCREEN
;
342 /* The WPS can be left in two ways:
343 * a) call a function, which draws over the wps. In this case, the wps
344 * will be still active (i.e. the below function didn't return)
345 * b) return with a value evaluated by root_menu.c, in this case the wps
346 * is really left, and root_menu will handle the next screen
348 * In either way, call gwps_leave_wps(), in order to restore the correct
349 * "main screen" backdrops and statusbars
351 long gui_wps_show(void)
355 long restoretimer
= RESTORE_WPS_INSTANTLY
; /* timer to delay screen redraw temporarily */
357 bool bookmark
= false;
360 long last_left
= 0, last_right
= 0;
362 #ifdef HAVE_LCD_CHARCELLS
363 status_set_audio(true);
364 status_set_param(false);
367 #ifdef AB_REPEAT_ENABLE
375 bool audio_paused
= (audio_status() & AUDIO_STATUS_PAUSE
)?true:false;
377 /* did someone else (i.e power thread) change audio pause mode? */
378 if (wps_state
.paused
!= audio_paused
) {
379 wps_state
.paused
= audio_paused
;
381 /* if another thread paused audio, we are probably in car mode,
382 about to shut down. lets save the settings. */
383 if (wps_state
.paused
) {
385 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
386 call_storage_idle_notifys(true);
391 #ifdef HAVE_LCD_BITMAP
392 /* when the peak meter is enabled we want to have a
393 few extra updates to make it look smooth. On the
394 other hand we don't want to waste energy if it
399 if(gui_wps
[i
].data
->peak_meter_enabled
)
404 long next_refresh
= current_tick
;
405 long next_big_refresh
= current_tick
+ HZ
/ 5;
406 button
= BUTTON_NONE
;
407 while (TIME_BEFORE(current_tick
, next_big_refresh
)) {
408 button
= get_action(CONTEXT_WPS
|ALLOW_SOFTLOCK
,TIMEOUT_NOBLOCK
);
409 if (button
!= ACTION_NONE
) {
413 sleep(0); /* Sleep until end of current tick. */
415 if (TIME_AFTER(current_tick
, next_refresh
)) {
418 if(gui_wps
[i
].data
->peak_meter_enabled
)
419 gui_wps_redraw(&gui_wps
[i
], 0,
420 WPS_REFRESH_PEAK_METER
);
421 next_refresh
+= HZ
/ PEAK_METER_FPS
;
428 /* The peak meter is disabled
429 -> no additional screen updates needed */
433 button
= get_action(CONTEXT_WPS
|ALLOW_SOFTLOCK
,
434 restore
? HZ
/100 : HZ
/5);
437 /* Exit if audio has stopped playing. This happens e.g. at end of
438 playlist or if using the sleep timer. */
439 if (!(audio_status() & AUDIO_STATUS_PLAY
))
441 #ifdef HAVE_TOUCHSCREEN
442 if (button
== ACTION_TOUCHSCREEN
)
443 button
= wps_get_touchaction(gui_wps
[SCREEN_MAIN
].data
);
445 /* The iPods/X5/M5 use a single button for the A-B mode markers,
446 defined as ACTION_WPSAB_SINGLE in their config files. */
447 #ifdef ACTION_WPSAB_SINGLE
448 if (!global_settings
.party_mode
&& ab_repeat_mode_enabled())
450 static int wps_ab_state
= 0;
451 if (button
== ACTION_WPSAB_SINGLE
)
453 switch (wps_ab_state
)
455 case 0: /* set the A spot */
456 button
= ACTION_WPS_ABSETA_PREVDIR
;
458 case 1: /* set the B spot */
459 button
= ACTION_WPS_ABSETB_NEXTDIR
;
462 button
= ACTION_WPS_ABRESET
;
465 wps_ab_state
= (wps_ab_state
+1) % 3;
471 case ACTION_WPS_CONTEXT
:
474 /* if music is stopped in the context menu we want to exit the wps */
475 if (onplay(wps_state
.id3
->path
,
476 FILE_ATTR_AUDIO
, CONTEXT_WPS
) == ONPLAY_MAINMENU
483 case ACTION_WPS_BROWSE
:
484 #ifdef HAVE_LCD_CHARCELLS
485 status_set_record(false);
486 status_set_audio(false);
489 return GO_TO_PREVIOUS_BROWSER
;
493 case ACTION_WPS_PLAY
:
494 if (global_settings
.party_mode
)
496 if ( wps_state
.paused
)
498 wps_state
.paused
= false;
499 if ( global_settings
.fade_on_stop
)
506 wps_state
.paused
= true;
507 if ( global_settings
.fade_on_stop
)
512 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
513 call_storage_idle_notifys(true); /* make sure resume info is saved */
518 case ACTION_WPS_VOLUP
:
521 gui_wps
[i
].data
->button_time_volume
= current_tick
;
522 global_settings
.volume
++;
527 if(update_onvol_change(&gui_wps
[i
]))
532 restoretimer
= RESTORE_WPS_NEXT_SECOND
;
536 case ACTION_WPS_VOLDOWN
:
539 gui_wps
[i
].data
->button_time_volume
= current_tick
;
540 global_settings
.volume
--;
545 if(update_onvol_change(&gui_wps
[i
]))
550 restoretimer
= RESTORE_WPS_NEXT_SECOND
;
555 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
556 case ACTION_WPS_SEEKFWD
:
557 if (global_settings
.party_mode
)
559 if (current_tick
-last_right
< HZ
)
561 if (wps_state
.id3
->cuesheet
)
571 ffwd_rew(ACTION_WPS_SEEKFWD
);
572 last_right
= last_left
= 0;
575 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
576 case ACTION_WPS_SEEKBACK
:
577 if (global_settings
.party_mode
)
579 if (current_tick
-last_left
< HZ
)
581 if (wps_state
.id3
->cuesheet
)
583 if (!wps_state
.paused
)
584 #if (CONFIG_CODEC == SWCODEC)
585 audio_pre_ff_rewind();
597 ffwd_rew(ACTION_WPS_SEEKBACK
);
598 last_left
= last_right
= 0;
602 case ACTION_WPS_SKIPPREV
:
603 if (global_settings
.party_mode
)
605 last_left
= current_tick
;
606 #ifdef AB_REPEAT_ENABLE
607 /* if we're in A/B repeat mode and the current position
608 is past the A marker, jump back to the A marker... */
609 if ( ab_repeat_mode_enabled() )
611 if ( ab_after_A_marker(wps_state
.id3
->elapsed
) )
613 ab_jump_to_A_marker();
615 #if (AB_REPEAT_ENABLE == 2)
622 /* ...otherwise, do it normally */
628 OR if skip length set, hop by predetermined amount. */
629 case ACTION_WPS_SKIPNEXT
:
630 if (global_settings
.party_mode
)
632 last_right
= current_tick
;
633 #ifdef AB_REPEAT_ENABLE
634 /* if we're in A/B repeat mode and the current position is
635 before the A marker, jump to the A marker... */
636 if ( ab_repeat_mode_enabled() )
638 if ( ab_before_A_marker(wps_state
.id3
->elapsed
) )
640 ab_jump_to_A_marker();
642 #if (AB_REPEAT_ENABLE == 2)
649 /* ...otherwise, do it normally */
653 /* next / prev directories */
654 /* and set A-B markers if in a-b mode */
655 case ACTION_WPS_ABSETB_NEXTDIR
:
656 if (global_settings
.party_mode
)
658 #if defined(AB_REPEAT_ENABLE)
659 if (ab_repeat_mode_enabled())
661 ab_set_B_marker(wps_state
.id3
->elapsed
);
662 ab_jump_to_A_marker();
670 case ACTION_WPS_ABSETA_PREVDIR
:
671 if (global_settings
.party_mode
)
673 #if defined(AB_REPEAT_ENABLE)
674 if (ab_repeat_mode_enabled())
675 ab_set_A_marker(wps_state
.id3
->elapsed
);
682 /* menu key functions */
683 case ACTION_WPS_MENU
:
689 #ifdef HAVE_QUICKSCREEN
690 case ACTION_WPS_QUICKSCREEN
:
693 if (quick_screen_quick(button
))
694 return SYS_USB_CONNECTED
;
698 #endif /* HAVE_QUICKSCREEN */
700 /* screen settings */
705 if (quick_screen_f3(BUTTON_F3
))
706 return SYS_USB_CONNECTED
;
710 #endif /* BUTTON_F3 */
713 #ifdef HAVE_PITCHSCREEN
714 case ACTION_WPS_PITCHSCREEN
:
717 if (1 == gui_syncpitchscreen_run())
718 return SYS_USB_CONNECTED
;
722 #endif /* HAVE_PITCHSCREEN */
724 #ifdef AB_REPEAT_ENABLE
725 /* reset A&B markers */
726 case ACTION_WPS_ABRESET
:
727 if (ab_repeat_mode_enabled())
733 #endif /* AB_REPEAT_ENABLE */
735 /* stop and exit wps */
736 case ACTION_WPS_STOP
:
737 if (global_settings
.party_mode
)
743 case ACTION_WPS_ID3SCREEN
:
750 #ifdef HAVE_TOUCHSCREEN
751 case ACTION_TOUCH_SHUFFLE
: /* toggle shuffle mode */
753 global_settings
.playlist_shuffle
=
754 !global_settings
.playlist_shuffle
;
755 #if CONFIG_CODEC == SWCODEC
756 dsp_set_replaygain();
758 if (global_settings
.playlist_shuffle
)
759 playlist_randomise(NULL
, current_tick
, true);
761 playlist_sort(NULL
, true);
764 case ACTION_TOUCH_REPMODE
: /* cycle the repeat mode setting */
766 const struct settings_list
*rep_setting
=
767 find_setting(&global_settings
.repeat_mode
, NULL
);
768 option_select_next_val(rep_setting
, false, true);
769 audio_flush_and_reload_tracks();
772 #endif /* HAVE_TOUCHSCREEN */
773 /* this case is used by the softlock feature
774 * it requests a full update here */
776 wps_state
.do_full_update
= true;
778 case ACTION_NONE
: /* Timeout, do a partial update */
780 ffwd_rew(button
); /* hopefully fix the ffw/rwd bug */
782 #ifdef HAVE_RECORDING
788 default_event_handler(SYS_POWEROFF
);
790 case ACTION_WPS_VIEW_PLAYLIST
:
792 if (playlist_viewer()) /* true if USB connected */
793 return SYS_USB_CONNECTED
;
797 if(default_event_handler(button
) == SYS_USB_CONNECTED
)
803 if (wps_state
.do_full_update
|| update
)
805 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
806 gwps_caption_backlight(&wps_state
);
810 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
812 #ifdef HAVE_REMOTE_LCD
813 /* currently, all remotes are readable without backlight
814 * so still update those */
815 || (i
== SCREEN_REMOTE
)
820 gui_wps_update(&gui_wps
[i
]);
823 wps_state
.do_full_update
= false;
827 if (restore
&& wps_state
.id3
&&
828 ((restoretimer
== RESTORE_WPS_INSTANTLY
) ||
829 TIME_AFTER(current_tick
, restoretimer
)))
832 restoretimer
= RESTORE_WPS_INSTANTLY
;
833 gwps_fix_statusbars();
834 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
835 lcd_activation_set_hook(wps_lcd_activation_hook
);
839 screens
[i
].stop_scroll();
840 gui_wps_display(&gui_wps
[i
]);
845 #ifdef HAVE_LCD_CHARCELLS
846 status_set_record(false);
847 status_set_audio(false);
849 if (global_settings
.fade_on_stop
)
853 bookmark_autobookmark();
855 #ifdef AB_REPEAT_ENABLE
859 #ifdef HAVE_RECORDING
860 if (button
== ACTION_WPS_REC
)
861 return GO_TO_RECSCREEN
;
863 if (global_settings
.browse_current
)
864 return GO_TO_PREVIOUS_BROWSER
;
865 return GO_TO_PREVIOUS
;
868 if (button
&& !IS_SYSEVENT(button
) )
871 return GO_TO_ROOT
; /* unreachable - just to reduce compiler warnings */
874 /* this is called from the playback thread so NO DRAWING! */
875 static void track_changed_callback(void *param
)
877 wps_state
.id3
= (struct mp3entry
*)param
;
878 wps_state
.nid3
= audio_next_track();
879 if (wps_state
.id3
->cuesheet
)
881 cue_find_current_track(wps_state
.id3
->cuesheet
, wps_state
.id3
->elapsed
);
882 cue_spoof_id3(wps_state
.id3
->cuesheet
, wps_state
.id3
);
884 wps_state
.do_full_update
= true;
886 static void nextid3available_callback(void* param
)
889 wps_state
.nid3
= audio_next_track();
890 wps_state
.do_full_update
= true;
894 static void wps_state_init(void)
896 wps_state
.ff_rewind
= false;
897 wps_state
.paused
= false;
898 if(audio_status() & AUDIO_STATUS_PLAY
)
900 wps_state
.id3
= audio_current_track();
901 wps_state
.nid3
= audio_next_track();
905 wps_state
.id3
= NULL
;
906 wps_state
.nid3
= NULL
;
908 /* We'll be updating due to restore initialized with true */
909 wps_state
.do_full_update
= false;
910 /* add the WPS track event callbacks */
911 add_event(PLAYBACK_EVENT_TRACK_CHANGE
, false, track_changed_callback
);
912 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE
, false, nextid3available_callback
);
916 #ifdef HAVE_LCD_BITMAP
917 static void statusbar_toggle_handler(void *data
)
921 gwps_fix_statusbars();
925 struct viewport
*vp
= &gui_wps
[i
].data
->viewports
[0].vp
;
926 bool draw
= wpsbars
& (VP_SB_ONSCREEN(i
) | VP_SB_IGNORE_SETTING(i
));
930 vp
->height
= screens
[i
].lcdheight
;
934 bool bar_at_top
= statusbar_position(i
) != STATUSBAR_BOTTOM
;
935 vp
->y
= bar_at_top
?STATUSBAR_HEIGHT
:0;
936 vp
->height
= screens
[i
].lcdheight
- STATUSBAR_HEIGHT
;
942 void gui_sync_wps_init(void)
947 wps_data_init(&wps_datas
[i
]);
949 wps_datas
[i
].wps_uses_albumart
= 0;
951 #ifdef HAVE_REMOTE_LCD
952 wps_datas
[i
].remote_wps
= (i
!= 0);
954 gui_wps
[i
].data
= &wps_datas
[i
];
955 gui_wps
[i
].display
= &screens
[i
];
956 /* Currently no seperate wps_state needed/possible
957 so use the only available ( "global" ) one */
958 gui_wps
[i
].state
= &wps_state
;
960 #ifdef HAVE_LCD_BITMAP
961 add_event(GUI_EVENT_STATUSBAR_TOGGLE
, false, statusbar_toggle_handler
);
964 unload_wps_backdrop();
966 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
967 unload_remote_wps_backdrop();
972 /* Returns true if at least one of the gui_wps screens has an album art
973 tag in its wps structure */
974 bool gui_sync_wps_uses_albumart(void)
978 struct gui_wps
*gwps
= &gui_wps
[i
];
979 if (gwps
->data
&& (gwps
->data
->wps_uses_albumart
!= WPS_ALBUMART_NONE
))