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"
59 #include "ata_idle_notify.h"
60 #include "root_menu.h"
62 #include "quickscreen.h"
63 #include "pitchscreen.h"
64 #include "appevents.h"
68 #define RESTORE_WPS_INSTANTLY 0l
69 #define RESTORE_WPS_NEXT_SECOND ((long)(HZ+current_tick))
71 #define DEFAULT_SKIP_TRESH 3000ul
74 /* currently only one wps_state is needed */
75 struct wps_state wps_state
;
76 struct gui_wps gui_wps
[NB_SCREENS
];
77 static struct wps_data wps_datas
[NB_SCREENS
];
79 /* initial setup of wps_data */
80 static void wps_state_init(void);
81 static void track_changed_callback(void *param
);
82 static void nextid3available_callback(void* param
);
85 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
86 static void gwps_caption_backlight(struct wps_state
*state
)
88 if (state
&& state
->id3
)
91 if (global_settings
.caption_backlight
)
93 /* turn on backlight n seconds before track ends, and turn it off n
94 seconds into the new track. n == backlight_timeout, or 5s */
95 int n
= global_settings
.backlight_timeout
* 1000;
98 n
= 5000; /* use 5s if backlight is always on or off */
100 if (((state
->id3
->elapsed
< 1000) ||
101 ((state
->id3
->length
- state
->id3
->elapsed
) < (unsigned)n
)) &&
102 (state
->paused
== false))
106 #ifdef HAVE_REMOTE_LCD
107 if (global_settings
.remote_caption_backlight
)
109 /* turn on remote backlight n seconds before track ends, and turn it
110 off n seconds into the new track. n == remote_backlight_timeout,
112 int n
= global_settings
.remote_backlight_timeout
* 1000;
115 n
= 5000; /* use 5s if backlight is always on or off */
117 if (((state
->id3
->elapsed
< 1000) ||
118 ((state
->id3
->length
- state
->id3
->elapsed
) < (unsigned)n
)) &&
119 (state
->paused
== false))
120 remote_backlight_on();
128 static void change_dir(int direction
)
130 if (global_settings
.prevent_skip
)
135 else if (direction
> 0)
139 static void prev_track(unsigned long skip_thresh
)
141 if (wps_state
.id3
->elapsed
< skip_thresh
)
148 if (cuesheet_is_enabled() && wps_state
.id3
->cuesheet_type
)
150 curr_cuesheet_skip(-1, wps_state
.id3
->elapsed
);
154 if (!wps_state
.paused
)
155 #if (CONFIG_CODEC == SWCODEC)
156 audio_pre_ff_rewind();
163 #if (CONFIG_CODEC != SWCODEC)
164 if (!wps_state
.paused
)
170 static void next_track(void)
172 /* take care of if we're playing a cuesheet */
173 if (cuesheet_is_enabled() && wps_state
.id3
->cuesheet_type
)
175 if (curr_cuesheet_skip(1, wps_state
.id3
->elapsed
))
177 /* if the result was false, then we really want
178 to skip to the next track */
186 static void play_hop(int direction
)
188 unsigned long step
= ((unsigned long)global_settings
.skip_length
)*1000;
189 unsigned long elapsed
= wps_state
.id3
->elapsed
;
190 unsigned long remaining
= wps_state
.id3
->length
- elapsed
;
192 if (!global_settings
.prevent_skip
&&
194 (direction
> 0 && step
>= remaining
) ||
195 (direction
< 0 && elapsed
< DEFAULT_SKIP_TRESH
)))
196 { /* Do normal track skipping */
199 else if (direction
< 0)
200 prev_track(DEFAULT_SKIP_TRESH
);
204 if (direction
== 1 && step
>= remaining
)
206 #if CONFIG_CODEC == SWCODEC
207 if(global_settings
.beep
)
208 pcmbuf_beep(1000, 150, 1500*global_settings
.beep
);
212 else if ((direction
== -1 && elapsed
< step
))
218 elapsed
+= step
* direction
;
220 if((audio_status() & AUDIO_STATUS_PLAY
) && !wps_state
.paused
)
222 #if (CONFIG_CODEC == SWCODEC)
223 audio_pre_ff_rewind();
228 audio_ff_rewind(wps_state
.id3
->elapsed
= elapsed
);
229 #if (CONFIG_CODEC != SWCODEC)
230 if (!wps_state
.paused
)
235 static void gwps_fix_statusbars(void)
237 #ifdef HAVE_LCD_BITMAP
239 wpsbars
= VP_SB_HIDE_ALL
;
243 if (gui_wps
[i
].data
->wps_sb_tag
)
244 draw
= gui_wps
[i
].data
->show_sb_on_wps
;
245 else if (global_settings
.statusbar
)
246 wpsbars
|= VP_SB_ONSCREEN(i
);
248 wpsbars
|= (VP_SB_ONSCREEN(i
) | VP_SB_IGNORE_SETTING(i
));
251 wpsbars
= VP_SB_ALLSCREENS
;
255 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
257 * If the user is unable to see the wps, because the display is deactivated,
258 * we surpress updates until the wps gets actived again (the lcd driver will
261 static void wps_lcd_activation_hook(void)
263 /* issue an update */
264 wps_state
.do_full_update
= true;
265 /* force timeout in wps main loop, so that the update is instantly */
266 queue_post(&button_queue
, BUTTON_NONE
, 0);
270 static void gwps_leave_wps(void)
272 int i
, oldbars
= VP_SB_HIDE_ALL
;
275 gui_wps
[i
].display
->stop_scroll();
276 if (global_settings
.statusbar
)
277 oldbars
= VP_SB_ALLSCREENS
;
280 show_main_backdrop();
282 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
283 show_remote_main_backdrop();
285 viewportmanager_set_statusbar(oldbars
);
286 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
287 /* Play safe and unregister the hook */
288 lcd_activation_set_hook(NULL
);
292 void gwps_draw_statusbars(void)
294 viewportmanager_set_statusbar(wpsbars
);
296 #ifdef HAVE_TOUCHSCREEN
297 static int wps_get_touchaction(struct wps_data
*data
)
301 int type
= action_get_touchscreen_press(&x
, &y
);
303 struct touchregion
*r
;
304 if (type
!= BUTTON_REL
)
305 return ACTION_TOUCHSCREEN
;
306 for (i
=0; i
<data
->touchregion_count
; i
++)
308 r
= &data
->touchregion
[i
];
309 /* make sure this region's viewport is visible */
310 if (r
->wvp
->hidden_flags
&VP_DRAW_HIDDEN
)
312 /* reposition the touch inside the viewport */
313 vx
= x
- r
->wvp
->vp
.x
;
314 vy
= y
- r
->wvp
->vp
.y
;
315 /* check if its inside this viewport */
316 if (vx
>= 0 && vx
< r
->wvp
->vp
.x
+ r
->wvp
->vp
.width
&&
317 vy
>= 0 && vy
< r
->wvp
->vp
.y
+ r
->wvp
->vp
.height
)
319 /* now see if the point is inside this region */
320 if (vx
>= r
->x
&& vx
< r
->x
+r
->width
&&
321 vy
>= r
->y
&& vy
< r
->y
+r
->height
)
325 return ACTION_TOUCHSCREEN
;
328 /* The WPS can be left in two ways:
329 * a) call a function, which draws over the wps. In this case, the wps
330 * will be still active (i.e. the below function didn't return)
331 * b) return with a value evaluated by root_menu.c, in this case the wps
332 * is really left, and root_menu will handle the next screen
334 * In either way, call gwps_leave_wps(), in order to restore the correct
335 * "main screen" backdrops and statusbars
337 long gui_wps_show(void)
341 long restoretimer
= RESTORE_WPS_INSTANTLY
; /* timer to delay screen redraw temporarily */
343 bool bookmark
= false;
346 long last_left
= 0, last_right
= 0;
348 #ifdef HAVE_LCD_CHARCELLS
349 status_set_audio(true);
350 status_set_param(false);
353 #ifdef AB_REPEAT_ENABLE
361 bool audio_paused
= (audio_status() & AUDIO_STATUS_PAUSE
)?true:false;
363 /* did someone else (i.e power thread) change audio pause mode? */
364 if (wps_state
.paused
!= audio_paused
) {
365 wps_state
.paused
= audio_paused
;
367 /* if another thread paused audio, we are probably in car mode,
368 about to shut down. lets save the settings. */
369 if (wps_state
.paused
) {
371 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
372 call_storage_idle_notifys(true);
377 #ifdef HAVE_LCD_BITMAP
378 /* when the peak meter is enabled we want to have a
379 few extra updates to make it look smooth. On the
380 other hand we don't want to waste energy if it
385 if(gui_wps
[i
].data
->peak_meter_enabled
)
390 long next_refresh
= current_tick
;
391 long next_big_refresh
= current_tick
+ HZ
/ 5;
392 button
= BUTTON_NONE
;
393 while (TIME_BEFORE(current_tick
, next_big_refresh
)) {
394 button
= get_action(CONTEXT_WPS
|ALLOW_SOFTLOCK
,TIMEOUT_NOBLOCK
);
395 if (button
!= ACTION_NONE
) {
399 sleep(0); /* Sleep until end of current tick. */
401 if (TIME_AFTER(current_tick
, next_refresh
)) {
404 if(gui_wps
[i
].data
->peak_meter_enabled
)
405 gui_wps_redraw(&gui_wps
[i
], 0,
406 WPS_REFRESH_PEAK_METER
);
407 next_refresh
+= HZ
/ PEAK_METER_FPS
;
414 /* The peak meter is disabled
415 -> no additional screen updates needed */
419 button
= get_action(CONTEXT_WPS
|ALLOW_SOFTLOCK
,
420 restore
? HZ
/100 : HZ
/5);
423 /* Exit if audio has stopped playing. This happens e.g. at end of
424 playlist or if using the sleep timer. */
425 if (!(audio_status() & AUDIO_STATUS_PLAY
))
427 #ifdef HAVE_TOUCHSCREEN
428 if (button
== ACTION_TOUCHSCREEN
)
429 button
= wps_get_touchaction(gui_wps
[SCREEN_MAIN
].data
);
431 /* The iPods/X5/M5 use a single button for the A-B mode markers,
432 defined as ACTION_WPSAB_SINGLE in their config files. */
433 #ifdef ACTION_WPSAB_SINGLE
434 if (!global_settings
.party_mode
&& ab_repeat_mode_enabled())
436 static int wps_ab_state
= 0;
437 if (button
== ACTION_WPSAB_SINGLE
)
439 switch (wps_ab_state
)
441 case 0: /* set the A spot */
442 button
= ACTION_WPS_ABSETA_PREVDIR
;
444 case 1: /* set the B spot */
445 button
= ACTION_WPS_ABSETB_NEXTDIR
;
448 button
= ACTION_WPS_ABRESET
;
451 wps_ab_state
= (wps_ab_state
+1) % 3;
457 case ACTION_WPS_CONTEXT
:
460 /* if music is stopped in the context menu we want to exit the wps */
461 if (onplay(wps_state
.id3
->path
,
462 FILE_ATTR_AUDIO
, CONTEXT_WPS
) == ONPLAY_MAINMENU
469 case ACTION_WPS_BROWSE
:
470 #ifdef HAVE_LCD_CHARCELLS
471 status_set_record(false);
472 status_set_audio(false);
475 return GO_TO_PREVIOUS_BROWSER
;
479 case ACTION_WPS_PLAY
:
480 if (global_settings
.party_mode
)
482 if ( wps_state
.paused
)
484 wps_state
.paused
= false;
485 if ( global_settings
.fade_on_stop
)
492 wps_state
.paused
= true;
493 if ( global_settings
.fade_on_stop
)
498 #if !defined(HAVE_RTC_RAM) && !defined(HAVE_SW_POWEROFF)
499 call_storage_idle_notifys(true); /* make sure resume info is saved */
504 case ACTION_WPS_VOLUP
:
507 gui_wps
[i
].data
->button_time_volume
= current_tick
;
508 global_settings
.volume
++;
513 if(update_onvol_change(&gui_wps
[i
]))
518 restoretimer
= RESTORE_WPS_NEXT_SECOND
;
522 case ACTION_WPS_VOLDOWN
:
525 gui_wps
[i
].data
->button_time_volume
= current_tick
;
526 global_settings
.volume
--;
531 if(update_onvol_change(&gui_wps
[i
]))
536 restoretimer
= RESTORE_WPS_NEXT_SECOND
;
541 OR next dir if this is straight after ACTION_WPS_SKIPNEXT */
542 case ACTION_WPS_SEEKFWD
:
543 if (global_settings
.party_mode
)
545 if (current_tick
-last_right
< HZ
)
547 if (cuesheet_is_enabled() && wps_state
.id3
->cuesheet_type
)
557 ffwd_rew(ACTION_WPS_SEEKFWD
);
558 last_right
= last_left
= 0;
561 OR prev dir if this is straight after ACTION_WPS_SKIPPREV,*/
562 case ACTION_WPS_SEEKBACK
:
563 if (global_settings
.party_mode
)
565 if (current_tick
-last_left
< HZ
)
567 if (cuesheet_is_enabled() && wps_state
.id3
->cuesheet_type
)
569 if (!wps_state
.paused
)
570 #if (CONFIG_CODEC == SWCODEC)
571 audio_pre_ff_rewind();
583 ffwd_rew(ACTION_WPS_SEEKBACK
);
584 last_left
= last_right
= 0;
588 case ACTION_WPS_SKIPPREV
:
589 if (global_settings
.party_mode
)
591 last_left
= current_tick
;
592 #ifdef AB_REPEAT_ENABLE
593 /* if we're in A/B repeat mode and the current position
594 is past the A marker, jump back to the A marker... */
595 if ( ab_repeat_mode_enabled() )
597 if ( ab_after_A_marker(wps_state
.id3
->elapsed
) )
599 ab_jump_to_A_marker();
601 #if (AB_REPEAT_ENABLE == 2)
608 /* ...otherwise, do it normally */
614 OR if skip length set, hop by predetermined amount. */
615 case ACTION_WPS_SKIPNEXT
:
616 if (global_settings
.party_mode
)
618 last_right
= current_tick
;
619 #ifdef AB_REPEAT_ENABLE
620 /* if we're in A/B repeat mode and the current position is
621 before the A marker, jump to the A marker... */
622 if ( ab_repeat_mode_enabled() )
624 if ( ab_before_A_marker(wps_state
.id3
->elapsed
) )
626 ab_jump_to_A_marker();
628 #if (AB_REPEAT_ENABLE == 2)
635 /* ...otherwise, do it normally */
639 /* next / prev directories */
640 /* and set A-B markers if in a-b mode */
641 case ACTION_WPS_ABSETB_NEXTDIR
:
642 if (global_settings
.party_mode
)
644 #if defined(AB_REPEAT_ENABLE)
645 if (ab_repeat_mode_enabled())
647 ab_set_B_marker(wps_state
.id3
->elapsed
);
648 ab_jump_to_A_marker();
656 case ACTION_WPS_ABSETA_PREVDIR
:
657 if (global_settings
.party_mode
)
659 #if defined(AB_REPEAT_ENABLE)
660 if (ab_repeat_mode_enabled())
661 ab_set_A_marker(wps_state
.id3
->elapsed
);
668 /* menu key functions */
669 case ACTION_WPS_MENU
:
675 #ifdef HAVE_QUICKSCREEN
676 case ACTION_WPS_QUICKSCREEN
:
679 if (quick_screen_quick(button
))
680 return SYS_USB_CONNECTED
;
684 #endif /* HAVE_QUICKSCREEN */
686 /* screen settings */
691 if (quick_screen_f3(BUTTON_F3
))
692 return SYS_USB_CONNECTED
;
696 #endif /* BUTTON_F3 */
699 #ifdef HAVE_PITCHSCREEN
700 case ACTION_WPS_PITCHSCREEN
:
703 if (1 == gui_syncpitchscreen_run())
704 return SYS_USB_CONNECTED
;
708 #endif /* HAVE_PITCHSCREEN */
710 #ifdef AB_REPEAT_ENABLE
711 /* reset A&B markers */
712 case ACTION_WPS_ABRESET
:
713 if (ab_repeat_mode_enabled())
719 #endif /* AB_REPEAT_ENABLE */
721 /* stop and exit wps */
722 case ACTION_WPS_STOP
:
723 if (global_settings
.party_mode
)
729 case ACTION_WPS_ID3SCREEN
:
737 case ACTION_REDRAW
: /* yes are locked, just redraw */
739 case ACTION_NONE
: /* Timeout, do an partial update */
741 ffwd_rew(button
); /* hopefully fix the ffw/rwd bug */
743 #ifdef HAVE_RECORDING
750 default_event_handler(SYS_POWEROFF
);
754 if(default_event_handler(button
) == SYS_USB_CONNECTED
)
760 if (wps_state
.do_full_update
|| update
)
762 #if defined(HAVE_BACKLIGHT) || defined(HAVE_REMOTE_LCD)
763 gwps_caption_backlight(&wps_state
);
767 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
769 #ifdef HAVE_REMOTE_LCD
770 /* currently, all remotes are readable without backlight
771 * so still update those */
772 || (i
== SCREEN_REMOTE
)
777 gui_wps_update(&gui_wps
[i
]);
780 wps_state
.do_full_update
= false;
784 if (restore
&& wps_state
.id3
&&
785 ((restoretimer
== RESTORE_WPS_INSTANTLY
) ||
786 TIME_AFTER(current_tick
, restoretimer
)))
789 restoretimer
= RESTORE_WPS_INSTANTLY
;
790 gwps_fix_statusbars();
791 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
792 lcd_activation_set_hook(wps_lcd_activation_hook
);
796 screens
[i
].stop_scroll();
797 gui_wps_display(&gui_wps
[i
]);
802 #ifdef HAVE_LCD_CHARCELLS
803 status_set_record(false);
804 status_set_audio(false);
806 if (global_settings
.fade_on_stop
)
810 bookmark_autobookmark();
812 #ifdef AB_REPEAT_ENABLE
816 #ifdef HAVE_RECORDING
817 if (button
== ACTION_WPS_REC
)
818 return GO_TO_RECSCREEN
;
820 if (global_settings
.browse_current
)
821 return GO_TO_PREVIOUS_BROWSER
;
822 return GO_TO_PREVIOUS
;
825 if (button
&& !IS_SYSEVENT(button
) )
828 return GO_TO_ROOT
; /* unreachable - just to reduce compiler warnings */
831 /* this is called from the playback thread so NO DRAWING! */
832 static void track_changed_callback(void *param
)
834 wps_state
.id3
= (struct mp3entry
*)param
;
835 wps_state
.nid3
= audio_next_track();
837 if (cuesheet_is_enabled() && wps_state
.id3
->cuesheet_type
838 && strcmp(wps_state
.id3
->path
, curr_cue
->audio_filename
))
840 /* the current cuesheet isn't the right one any more */
841 /* We need to parse the new cuesheet */
842 char cuepath
[MAX_PATH
];
844 if (look_for_cuesheet_file(wps_state
.id3
->path
, cuepath
) &&
845 parse_cuesheet(cuepath
, curr_cue
))
847 wps_state
.id3
->cuesheet_type
= 1;
848 strcpy(curr_cue
->audio_filename
, wps_state
.id3
->path
);
851 cue_spoof_id3(curr_cue
, wps_state
.id3
);
853 wps_state
.do_full_update
= true;
855 static void nextid3available_callback(void* param
)
858 wps_state
.nid3
= audio_next_track();
859 wps_state
.do_full_update
= true;
864 static void wps_state_init(void)
866 wps_state
.ff_rewind
= false;
867 wps_state
.paused
= false;
868 if(audio_status() & AUDIO_STATUS_PLAY
)
870 wps_state
.id3
= audio_current_track();
871 wps_state
.nid3
= audio_next_track();
875 wps_state
.id3
= NULL
;
876 wps_state
.nid3
= NULL
;
878 /* We'll be updating due to restore initialized with true */
879 wps_state
.do_full_update
= false;
880 /* add the WPS track event callbacks */
881 add_event(PLAYBACK_EVENT_TRACK_CHANGE
, false, track_changed_callback
);
882 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE
, false, nextid3available_callback
);
887 #ifdef HAVE_LCD_BITMAP
888 static void statusbar_toggle_handler(void *data
)
892 gwps_fix_statusbars();
896 struct viewport
*vp
= &gui_wps
[i
].data
->viewports
[0].vp
;
897 bool draw
= wpsbars
& (VP_SB_ONSCREEN(i
) | VP_SB_IGNORE_SETTING(i
));
901 vp
->height
= screens
[i
].lcdheight
;
905 vp
->y
= STATUSBAR_HEIGHT
;
906 vp
->height
= screens
[i
].lcdheight
- STATUSBAR_HEIGHT
;
912 void gui_sync_wps_init(void)
917 wps_data_init(&wps_datas
[i
]);
919 wps_datas
[i
].wps_uses_albumart
= 0;
921 #ifdef HAVE_REMOTE_LCD
922 wps_datas
[i
].remote_wps
= (i
!= 0);
924 gui_wps
[i
].data
= &wps_datas
[i
];
925 gui_wps
[i
].display
= &screens
[i
];
926 /* Currently no seperate wps_state needed/possible
927 so use the only aviable ( "global" ) one */
928 gui_wps
[i
].state
= &wps_state
;
930 #ifdef HAVE_LCD_BITMAP
931 add_event(GUI_EVENT_STATUSBAR_TOGGLE
, false, statusbar_toggle_handler
);
934 unload_wps_backdrop();
936 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
937 unload_remote_wps_backdrop();
942 /* Returns true if at least one of the gui_wps screens has an album art
943 tag in its wps structure */
944 bool gui_sync_wps_uses_albumart(void)
948 struct gui_wps
*gwps
= &gui_wps
[i
];
949 if (gwps
->data
&& (gwps
->data
->wps_uses_albumart
!= WPS_ALBUMART_NONE
))