1 /***************************************************************************
4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
11 * Copyright (C) 2003 Hardeep Sidhu
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
21 * Kevin Ferrare 2005/10/16
22 * multi-screen support, rewrote a lot of code
41 #include "backlight.h"
45 #include "playlist_viewer.h"
48 #include "statusbar.h"
50 #include "playlist_menu.h"
53 /* Maximum number of tracks we can have loaded at one time */
54 #define MAX_PLAYLIST_ENTRIES 200
56 /* The number of items between the selected one and the end/start of
57 * the buffer under which the buffer must reload */
58 #define MIN_BUFFER_MARGIN (screens[0].nb_lines+1)
60 /* Default playlist name for saving */
61 #define DEFAULT_VIEWER_PLAYLIST_NAME "/viewer.m3u"
64 /* Information about a specific track */
65 struct playlist_entry
{
66 char *name
; /* Formatted track name */
67 int index
; /* Playlist index */
68 int display_index
; /* Display index */
69 bool queued
; /* Is track queued? */
70 bool skipped
; /* Is track marked as bad? */
79 struct playlist_buffer
81 char *name_buffer
; /* Buffer used to store track names */
82 int buffer_size
; /* Size of name buffer */
84 int first_index
; /* Real index of first track loaded inside
87 enum direction direction
; /* Direction of the buffer (if the buffer
88 was loaded BACKWARD, the last track in
89 the buffer has a real index < to the
90 real index of the the first track)*/
92 struct playlist_entry tracks
[MAX_PLAYLIST_ENTRIES
];
93 int num_loaded
; /* Number of track entries loaded in buffer */
96 /* Global playlist viewer settings */
97 struct playlist_viewer
{
98 struct playlist_info
* playlist
; /* playlist being viewed */
99 int num_tracks
; /* Number of tracks in playlist */
100 int current_playing_track
; /* Index of current playing track */
101 int selected_track
; /* The selected track, relative (first is 0)*/
102 int move_track
; /* Playlist index of track to move or -1 */
103 struct playlist_buffer buffer
;
106 static struct playlist_viewer viewer
;
108 /* Used when viewing playlists on disk */
109 static struct playlist_info temp_playlist
;
111 void playlist_buffer_init(struct playlist_buffer
* pb
, char * names_buffer
,
112 int names_buffer_size
);
113 void playlist_buffer_load_entries(struct playlist_buffer
* pb
, int index
,
114 enum direction direction
);
115 int playlist_entry_load(struct playlist_entry
*entry
, int index
,
116 char* name_buffer
, int remaining_size
);
118 struct playlist_entry
* playlist_buffer_get_track(struct playlist_buffer
* pb
,
121 static bool playlist_viewer_init(struct playlist_viewer
* viewer
,
122 char* filename
, bool reload
);
124 static void format_name(char* dest
, const char* src
);
125 static void format_line(const struct playlist_entry
* track
, char* str
,
128 static bool update_playlist(bool force
);
129 static int onplay_menu(int index
);
130 static bool viewer_menu(void);
131 static bool show_icons(void);
132 static bool show_indices(void);
133 static bool track_display(void);
134 static bool save_playlist(void);
136 void playlist_buffer_init(struct playlist_buffer
* pb
, char * names_buffer
,
137 int names_buffer_size
)
139 pb
->name_buffer
=names_buffer
;
140 pb
->buffer_size
=names_buffer_size
;
146 * Loads the entries following 'index' in the playlist buffer
148 void playlist_buffer_load_entries(struct playlist_buffer
* pb
, int index
,
149 enum direction direction
)
151 int num_entries
= viewer
.num_tracks
;
152 char* p
= pb
->name_buffer
;
153 int remaining
= pb
->buffer_size
;
156 pb
->first_index
= index
;
157 if (num_entries
> MAX_PLAYLIST_ENTRIES
)
158 num_entries
= MAX_PLAYLIST_ENTRIES
;
160 for(i
=0; i
<num_entries
; i
++)
162 int len
= playlist_entry_load(&(pb
->tracks
[i
]), index
, p
, remaining
);
165 /* Out of name buffer space */
173 if(direction
==FORWARD
)
177 index
+=viewer
.num_tracks
;
178 index
%=viewer
.num_tracks
;
180 pb
->direction
=direction
;
184 void playlist_buffer_load_entries_screen(struct playlist_buffer
* pb
,
185 enum direction direction
)
187 if(direction
==FORWARD
)
189 int min_start
=viewer
.selected_track
-2*screens
[0].nb_lines
;
191 min_start
+=viewer
.num_tracks
;
192 min_start
%= viewer
.num_tracks
;
193 playlist_buffer_load_entries(pb
, min_start
, FORWARD
);
197 int max_start
=viewer
.selected_track
+2*screens
[0].nb_lines
;
198 max_start
%=viewer
.num_tracks
;
199 playlist_buffer_load_entries(pb
, max_start
, BACKWARD
);
203 int playlist_entry_load(struct playlist_entry
*entry
, int index
,
204 char* name_buffer
, int remaining_size
)
206 struct playlist_track_info info
;
209 /* Playlist viewer orders songs based on display index. We need to
210 convert to real playlist index to access track */
211 index
= (index
+ playlist_get_first_index(viewer
.playlist
)) %
213 if (playlist_get_track_info(viewer
.playlist
, index
, &info
) < 0)
216 len
= strlen(info
.filename
) + 1;
218 if (len
<= remaining_size
)
220 strcpy(name_buffer
, info
.filename
);
222 entry
->name
= name_buffer
;
223 entry
->index
= info
.index
;
224 entry
->display_index
= info
.display_index
;
225 entry
->queued
= info
.attr
& PLAYLIST_ATTR_QUEUED
;
226 entry
->skipped
= info
.attr
& PLAYLIST_ATTR_SKIPPED
;
232 int playlist_buffer_get_index(struct playlist_buffer
* pb
, int index
)
235 if(pb
->direction
==FORWARD
)
237 if(index
>=pb
->first_index
)
238 buffer_index
=index
-pb
->first_index
;
239 else /* rotation : track0 in buffer + requested track */
240 buffer_index
=(viewer
.num_tracks
-pb
->first_index
)+(index
);
244 if(index
<=pb
->first_index
)
245 buffer_index
=pb
->first_index
-index
;
246 else /* rotation : track0 in buffer + dist from the last track
247 to the requested track (num_tracks-requested track) */
248 buffer_index
=(pb
->first_index
)+(viewer
.num_tracks
-index
);
250 return(buffer_index
);
253 #define distance(a, b) \
254 a>b? (a) - (b) : (b) - (a)
255 bool playlist_buffer_needs_reload(struct playlist_buffer
* pb
, int track_index
)
257 if(pb
->num_loaded
==viewer
.num_tracks
)
259 int selected_index
=playlist_buffer_get_index(pb
, track_index
);
260 int first_buffer_index
=playlist_buffer_get_index(pb
, pb
->first_index
);
261 int distance_beginning
=distance(selected_index
, first_buffer_index
);
262 if(distance_beginning
<MIN_BUFFER_MARGIN
)
265 if(pb
->num_loaded
- distance_beginning
< MIN_BUFFER_MARGIN
)
270 struct playlist_entry
* playlist_buffer_get_track(struct playlist_buffer
* pb
,
273 int buffer_index
=playlist_buffer_get_index(pb
, index
);
274 return(&(pb
->tracks
[buffer_index
]));
277 /* Initialize the playlist viewer. */
278 static bool playlist_viewer_init(struct playlist_viewer
* viewer
,
279 char* filename
, bool reload
)
283 bool is_playing
= audio_status() & AUDIO_STATUS_PLAY
;
285 if (!filename
&& !is_playing
)
286 /* Nothing is playing, exit */
289 buffer
= plugin_get_buffer(&buffer_size
);
294 viewer
->playlist
= NULL
;
297 /* Viewing playlist on disk */
298 char *dir
, *file
, *temp_ptr
;
299 char *index_buffer
= NULL
;
300 int index_buffer_size
= 0;
302 viewer
->playlist
= &temp_playlist
;
304 /* Separate directory from filename */
305 temp_ptr
= strrchr(filename
+1,'/');
320 /* Something is playing, use half the plugin buffer for playlist
322 index_buffer_size
= buffer_size
/ 2;
323 index_buffer
= buffer
;
326 playlist_create_ex(viewer
->playlist
, dir
, file
, index_buffer
,
327 index_buffer_size
, buffer
+index_buffer_size
,
328 buffer_size
-index_buffer_size
);
333 buffer
+= index_buffer_size
;
334 buffer_size
-= index_buffer_size
;
336 playlist_buffer_init(&viewer
->buffer
, buffer
, buffer_size
);
338 viewer
->move_track
= -1;
342 if (viewer
->playlist
)
343 viewer
->selected_track
= 0;
345 viewer
->selected_track
= playlist_get_display_index() - 1;
348 if (!update_playlist(true))
353 /* Format trackname for display purposes */
354 static void format_name(char* dest
, const char* src
)
356 switch (global_settings
.playlist_viewer_track_display
)
361 /* Only display the filename */
362 char* p
= strrchr(src
, '/');
366 /* Remove the extension */
367 char* q
= strrchr(dest
, '.');
381 /* Format display line */
382 static void format_line(const struct playlist_entry
* track
, char* str
,
388 format_name(name
, track
->name
);
393 if (global_settings
.playlist_viewer_indices
)
394 /* Display playlist index */
395 snprintf(str
, len
, "%d. %s%s", track
->display_index
, skipped
, name
);
397 snprintf(str
, len
, "%s%s", skipped
, name
);
401 /* Update playlist in case something has changed or forced */
402 static bool update_playlist(bool force
)
404 if (!viewer
.playlist
)
405 playlist_get_resume_info(&viewer
.current_playing_track
);
407 viewer
.current_playing_track
= -1;
408 int nb_tracks
=playlist_amount_ex(viewer
.playlist
);
409 force
=force
|| nb_tracks
!= viewer
.num_tracks
;
413 viewer
.num_tracks
= nb_tracks
;
414 if (viewer
.num_tracks
<= 0)
416 playlist_buffer_load_entries_screen(&viewer
.buffer
, FORWARD
);
417 if (viewer
.buffer
.num_loaded
<= 0)
423 /* Menu of playlist commands. Invoked via ON+PLAY on main viewer screen.
424 Returns -1 if USB attached, 0 if no playlist change, and 1 if playlist
426 static int onplay_menu(int index
)
428 struct menu_item items
[3]; /* increase this if you add entries! */
429 int m
, i
=0, result
, ret
= 0;
430 struct playlist_entry
* current_track
=
431 playlist_buffer_get_track(&viewer
.buffer
, index
);
432 bool current
= (current_track
->index
== viewer
.current_playing_track
);
434 items
[i
].desc
= ID2P(LANG_REMOVE
);
437 items
[i
].desc
= ID2P(LANG_MOVE
);
440 items
[i
].desc
= ID2P(LANG_FILE_OPTIONS
);
443 m
= menu_init(items
, i
, NULL
, NULL
, NULL
, NULL
);
444 result
= menu_show(m
);
445 if (result
== MENU_ATTACHED_USB
)
447 else if (result
>= 0)
449 /* Abort current move */
450 viewer
.move_track
= -1;
456 playlist_delete(viewer
.playlist
, current_track
->index
);
459 if (playlist_amount_ex(viewer
.playlist
) <= 0)
463 /* Start playing new track except if it's the lasttrack
464 track in the playlist and repeat mode is disabled */
466 playlist_buffer_get_track(&viewer
.buffer
, index
);
467 if (current_track
->display_index
!=viewer
.num_tracks
||
468 global_settings
.repeat_mode
== REPEAT_ALL
)
470 talk_buffer_steal(); /* will use the mp3 buffer */
472 viewer
.current_playing_track
= -1;
480 viewer
.move_track
= current_track
->index
;
485 onplay(current_track
->name
, TREE_ATTR_MPA
, CONTEXT_TREE
);
487 if (!viewer
.playlist
)
499 /* Menu of viewer options. Invoked via F1(r) or Menu(p). */
500 static bool viewer_menu(void)
505 static const struct menu_item items
[] = {
506 { ID2P(LANG_SHOW_ICONS
), show_icons
},
507 { ID2P(LANG_SHOW_INDICES
), show_indices
},
508 { ID2P(LANG_TRACK_DISPLAY
), track_display
},
509 { ID2P(LANG_SAVE_DYNAMIC_PLAYLIST
), save_playlist
},
512 m
=menu_init( items
, sizeof(items
) / sizeof(*items
), NULL
,
514 result
= menu_run(m
);
522 /* Show icons in viewer? */
523 static bool show_icons(void)
525 return set_bool((char *)str(LANG_SHOW_ICONS
),
526 &global_settings
.playlist_viewer_icons
);
529 /* Show indices of tracks? */
530 static bool show_indices(void)
532 return set_bool((char *)str(LANG_SHOW_INDICES
),
533 &global_settings
.playlist_viewer_indices
);
536 /* How to display a track */
537 static bool track_display(void)
539 static const struct opt_items names
[] = {
540 { STR(LANG_DISPLAY_TRACK_NAME_ONLY
) },
541 { STR(LANG_DISPLAY_FULL_PATH
) }
544 return set_option((char *)str(LANG_TRACK_DISPLAY
),
545 &global_settings
.playlist_viewer_track_display
, INT
, names
, 2,
549 /* Save playlist to disk */
550 static bool save_playlist(void)
552 save_playlist_screen(viewer
.playlist
);
556 /* View current playlist */
557 bool playlist_viewer(void)
559 return playlist_viewer_ex(NULL
);
562 char * playlist_callback_name(int selected_item
, void * data
, char *buffer
)
564 struct playlist_viewer
* local_viewer
= (struct playlist_viewer
*)data
;
565 struct playlist_entry
*track
=
566 playlist_buffer_get_track(&(local_viewer
->buffer
), selected_item
);
567 format_line(track
, buffer
, MAX_PATH
);
572 void playlist_callback_icons(int selected_item
, void * data
, ICON
* icon
)
574 struct playlist_viewer
* local_viewer
=(struct playlist_viewer
*)data
;
575 struct playlist_entry
*track
=
576 playlist_buffer_get_track(&(local_viewer
->buffer
), selected_item
);
577 if (track
->index
== local_viewer
->current_playing_track
)
579 /* Current playing track */
580 #ifdef HAVE_LCD_BITMAP
581 *icon
=bitmap_icons_6x8
[Icon_Audio
];
586 else if (track
->index
== local_viewer
->move_track
)
588 /* Track we are moving */
589 #ifdef HAVE_LCD_BITMAP
590 *icon
=bitmap_icons_6x8
[Icon_Moving
];
595 else if (track
->queued
)
598 #ifdef HAVE_LCD_BITMAP
599 *icon
=bitmap_icons_6x8
[Icon_Queued
];
605 #ifdef HAVE_LCD_BITMAP
612 /* Main viewer function. Filename identifies playlist to be viewed. If NULL,
613 view current playlist. */
614 bool playlist_viewer_ex(char* filename
)
616 bool ret
= false; /* return value */
617 bool exit
=false; /* exit viewer */
619 struct gui_synclist playlist_lists
;
620 if (!playlist_viewer_init(&viewer
, filename
, false))
623 gui_synclist_init(&playlist_lists
, playlist_callback_name
, &viewer
, false, 1);
624 gui_synclist_set_icon_callback(&playlist_lists
,
625 global_settings
.playlist_viewer_icons
?
626 &playlist_callback_icons
:NULL
);
627 gui_synclist_set_nb_items(&playlist_lists
, viewer
.num_tracks
);
628 gui_synclist_select_item(&playlist_lists
, viewer
.selected_track
);
629 gui_synclist_draw(&playlist_lists
);
633 if (!viewer
.playlist
&& !(audio_status() & AUDIO_STATUS_PLAY
))
635 /* Play has stopped */
636 #ifdef HAVE_LCD_CHARCELLS
637 gui_syncsplash(HZ
, true, str(LANG_END_PLAYLIST_PLAYER
));
639 gui_syncsplash(HZ
, true, str(LANG_END_PLAYLIST_RECORDER
));
644 if (viewer
.move_track
!= -1)
645 gui_synclist_flash(&playlist_lists
);
647 if (!viewer
.playlist
)
648 playlist_get_resume_info(&track
);
652 if (track
!= viewer
.current_playing_track
||
653 playlist_amount_ex(viewer
.playlist
) != viewer
.num_tracks
)
655 /* Playlist has changed (new track started?) */
656 if (!update_playlist(false))
658 gui_synclist_set_nb_items(&playlist_lists
, viewer
.num_tracks
);
659 /* Abort move on playlist change */
660 viewer
.move_track
= -1;
661 gui_synclist_draw(&playlist_lists
);
664 /* Timeout so we can determine if play status has changed */
665 button
= get_action(CONTEXT_TREE
,HZ
/2);
667 if( (list_action
=gui_synclist_do_button(&playlist_lists
, button
))!=0 )
669 viewer
.selected_track
=gui_synclist_get_sel_pos(&playlist_lists
);
670 if(playlist_buffer_needs_reload(&viewer
.buffer
,
671 viewer
.selected_track
))
672 playlist_buffer_load_entries_screen(&viewer
.buffer
,
673 list_action
==ACTION_STD_NEXT
?
681 case ACTION_STD_CANCEL
:
686 struct playlist_entry
* current_track
=
687 playlist_buffer_get_track(&viewer
.buffer
,
688 viewer
.selected_track
);
689 if (viewer
.move_track
>= 0)
694 ret
= playlist_move(viewer
.playlist
, viewer
.move_track
,
695 current_track
->index
);
697 gui_syncsplash(HZ
, true, str(LANG_MOVE_FAILED
));
699 update_playlist(true);
700 viewer
.move_track
= -1;
702 else if (!viewer
.playlist
)
705 playlist_start(current_track
->index
, 0);
706 update_playlist(false);
711 if (playlist_set_current(viewer
.playlist
) < 0)
714 playlist_start(current_track
->index
, 0);
716 /* Our playlist is now the current list */
717 if (!playlist_viewer_init(&viewer
, NULL
, true))
720 gui_synclist_draw(&playlist_lists
);
724 case ACTION_STD_CONTEXT
:
729 ret
= onplay_menu(viewer
.selected_track
);
738 /* Playlist changed */
739 gui_synclist_del_item(&playlist_lists
);
740 update_playlist(true);
741 if (viewer
.num_tracks
<= 0)
744 gui_synclist_draw(&playlist_lists
);
747 case ACTION_STD_MENU
:
753 gui_synclist_set_icon_callback(
755 global_settings
.playlist_viewer_icons
?
756 &playlist_callback_icons
:NULL
758 gui_synclist_draw(&playlist_lists
);
762 gui_syncstatusbar_draw(&statusbars
, false);
766 if(default_event_handler(button
) == SYS_USB_CONNECTED
)
777 playlist_close(viewer
.playlist
);
778 action_signalscreenchange();
781 char * playlist_search_callback_name(int selected_item
, void * data
, char *buffer
)
783 int *found_indicies
= (int*)data
;
784 static struct playlist_track_info track
;
785 playlist_get_track_info(viewer
.playlist
,found_indicies
[selected_item
],&track
);
786 format_name(buffer
,track
.filename
);
791 void playlist_search_callback_icons(int selected_item
, void * data
, ICON
* icon
)
795 #ifdef HAVE_LCD_BITMAP
801 bool search_playlist(void)
803 char search_str
[32] = "";
804 bool ret
= false, exit
= false;
805 int i
, playlist_count
;
806 int found_indicies
[MAX_PLAYLIST_ENTRIES
],found_indicies_count
= 0;
808 struct gui_synclist playlist_lists
;
809 struct playlist_track_info track
;
811 if (!playlist_viewer_init(&viewer
, 0, false))
813 if (kbd_input(search_str
,sizeof(search_str
)) == -1)
816 playlist_count
= playlist_amount_ex(viewer
.playlist
);
817 for (i
=0;(i
<playlist_count
)&&(found_indicies_count
<MAX_PLAYLIST_ENTRIES
);i
++)
819 gui_syncsplash(0, true, str(LANG_PLAYLIST_SEARCH_MSG
),found_indicies_count
,
820 #if CONFIG_KEYPAD == PLAYER_PAD
826 if (action_userabort(TIMEOUT_NOBLOCK
))
828 playlist_get_track_info(viewer
.playlist
,i
,&track
);
829 if (strcasestr(track
.filename
,search_str
))
831 found_indicies
[found_indicies_count
++] = track
.index
;
834 if (!found_indicies_count
)
839 gui_synclist_init(&playlist_lists
, playlist_search_callback_name
,
840 found_indicies
, false, 1);
841 gui_synclist_set_icon_callback(&playlist_lists
,
842 global_settings
.playlist_viewer_icons
?
843 &playlist_search_callback_icons
:NULL
);
844 gui_synclist_set_nb_items(&playlist_lists
, found_indicies_count
);
845 gui_synclist_select_item(&playlist_lists
, 0);
846 gui_synclist_draw(&playlist_lists
);
849 button
= get_action(CONTEXT_LIST
,TIMEOUT_BLOCK
);
850 if (gui_synclist_do_button(&playlist_lists
, button
))
854 case ACTION_STD_CANCEL
:
860 found_indicies
[gui_synclist_get_sel_pos(&playlist_lists
)]
867 if(default_event_handler(button
) == SYS_USB_CONNECTED
)
875 action_signalscreenchange();