1 /***************************************************************************
4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
11 * Copyright (C) 2003 Hardeep Sidhu
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 * Kevin Ferrare 2005/10/16
24 * multi-screen support, rewrote a lot of code
37 #include "filetypes.h"
43 #include "backlight.h"
47 #include "playlist_viewer.h"
48 #include "playlist_catalog.h"
51 #include "statusbar.h"
53 #include "playlist_menu.h"
56 /* Maximum number of tracks we can have loaded at one time */
57 #define MAX_PLAYLIST_ENTRIES 200
59 /* The number of items between the selected one and the end/start of
60 * the buffer under which the buffer must reload */
61 #define MIN_BUFFER_MARGIN (screens[0].nb_lines+1)
63 /* Information about a specific track */
64 struct playlist_entry
{
65 char *name
; /* Formatted track name */
66 int index
; /* Playlist index */
67 int display_index
; /* Display index */
68 bool queued
; /* Is track queued? */
69 bool skipped
; /* Is track marked as bad? */
78 struct playlist_buffer
80 char *name_buffer
; /* Buffer used to store track names */
81 int buffer_size
; /* Size of name buffer */
83 int first_index
; /* Real index of first track loaded inside
86 enum direction direction
; /* Direction of the buffer (if the buffer
87 was loaded BACKWARD, the last track in
88 the buffer has a real index < to the
89 real index of the the first track)*/
91 struct playlist_entry tracks
[MAX_PLAYLIST_ENTRIES
];
92 int num_loaded
; /* Number of track entries loaded in buffer */
95 /* Global playlist viewer settings */
96 struct playlist_viewer
{
97 struct playlist_info
* playlist
; /* playlist being viewed */
98 int num_tracks
; /* Number of tracks in playlist */
99 int current_playing_track
; /* Index of current playing track */
100 int selected_track
; /* The selected track, relative (first is 0)*/
101 int move_track
; /* Playlist index of track to move or -1 */
102 struct playlist_buffer buffer
;
105 static struct playlist_viewer viewer
;
107 /* Used when viewing playlists on disk */
108 static struct playlist_info temp_playlist
;
110 static void playlist_buffer_init(struct playlist_buffer
*pb
, char *names_buffer
,
111 int names_buffer_size
);
112 static void playlist_buffer_load_entries(struct playlist_buffer
* pb
, int index
,
113 enum direction direction
);
114 static int playlist_entry_load(struct playlist_entry
*entry
, int index
,
115 char* name_buffer
, int remaining_size
);
117 static struct playlist_entry
* playlist_buffer_get_track(struct playlist_buffer
*pb
,
120 static bool playlist_viewer_init(struct playlist_viewer
* viewer
,
121 const char* filename
, bool reload
);
123 static void format_name(char* dest
, const char* src
);
124 static void format_line(const struct playlist_entry
* track
, char* str
,
127 static bool update_playlist(bool force
);
128 static int onplay_menu(int index
);
129 static bool viewer_menu(void);
130 static int save_playlist_func(void);
132 static void playlist_buffer_init(struct playlist_buffer
*pb
, char *names_buffer
,
133 int names_buffer_size
)
135 pb
->name_buffer
=names_buffer
;
136 pb
->buffer_size
=names_buffer_size
;
142 * Loads the entries following 'index' in the playlist buffer
144 static void playlist_buffer_load_entries(struct playlist_buffer
*pb
, int index
,
145 enum direction direction
)
147 int num_entries
= viewer
.num_tracks
;
148 char* p
= pb
->name_buffer
;
149 int remaining
= pb
->buffer_size
;
152 pb
->first_index
= index
;
153 if (num_entries
> MAX_PLAYLIST_ENTRIES
)
154 num_entries
= MAX_PLAYLIST_ENTRIES
;
156 for(i
=0; i
<num_entries
; i
++)
158 int len
= playlist_entry_load(&(pb
->tracks
[i
]), index
, p
, remaining
);
161 /* Out of name buffer space */
169 if(direction
==FORWARD
)
173 index
+=viewer
.num_tracks
;
174 index
%=viewer
.num_tracks
;
176 pb
->direction
=direction
;
180 static void playlist_buffer_load_entries_screen(struct playlist_buffer
* pb
,
181 enum direction direction
)
183 if(direction
==FORWARD
)
185 int min_start
=viewer
.selected_track
-2*screens
[0].nb_lines
;
187 min_start
+=viewer
.num_tracks
;
188 min_start
%= viewer
.num_tracks
;
189 playlist_buffer_load_entries(pb
, min_start
, FORWARD
);
193 int max_start
=viewer
.selected_track
+2*screens
[0].nb_lines
;
194 max_start
%=viewer
.num_tracks
;
195 playlist_buffer_load_entries(pb
, max_start
, BACKWARD
);
199 static int playlist_entry_load(struct playlist_entry
*entry
, int index
,
200 char* name_buffer
, int remaining_size
)
202 struct playlist_track_info info
;
205 /* Playlist viewer orders songs based on display index. We need to
206 convert to real playlist index to access track */
207 index
= (index
+ playlist_get_first_index(viewer
.playlist
)) %
209 if (playlist_get_track_info(viewer
.playlist
, index
, &info
) < 0)
212 len
= strlen(info
.filename
) + 1;
214 if (len
<= remaining_size
)
216 strcpy(name_buffer
, info
.filename
);
218 entry
->name
= name_buffer
;
219 entry
->index
= info
.index
;
220 entry
->display_index
= info
.display_index
;
221 entry
->queued
= info
.attr
& PLAYLIST_ATTR_QUEUED
;
222 entry
->skipped
= info
.attr
& PLAYLIST_ATTR_SKIPPED
;
228 static int playlist_buffer_get_index(struct playlist_buffer
*pb
, int index
)
231 if(pb
->direction
==FORWARD
)
233 if(index
>=pb
->first_index
)
234 buffer_index
=index
-pb
->first_index
;
235 else /* rotation : track0 in buffer + requested track */
236 buffer_index
=(viewer
.num_tracks
-pb
->first_index
)+(index
);
240 if(index
<=pb
->first_index
)
241 buffer_index
=pb
->first_index
-index
;
242 else /* rotation : track0 in buffer + dist from the last track
243 to the requested track (num_tracks-requested track) */
244 buffer_index
=(pb
->first_index
)+(viewer
.num_tracks
-index
);
246 return(buffer_index
);
249 #define distance(a, b) \
250 a>b? (a) - (b) : (b) - (a)
251 static bool playlist_buffer_needs_reload(struct playlist_buffer
* pb
,
254 if(pb
->num_loaded
==viewer
.num_tracks
)
256 int selected_index
=playlist_buffer_get_index(pb
, track_index
);
257 int first_buffer_index
=playlist_buffer_get_index(pb
, pb
->first_index
);
258 int distance_beginning
=distance(selected_index
, first_buffer_index
);
259 if(distance_beginning
<MIN_BUFFER_MARGIN
)
262 if(pb
->num_loaded
- distance_beginning
< MIN_BUFFER_MARGIN
)
267 static struct playlist_entry
* playlist_buffer_get_track(struct playlist_buffer
*pb
,
270 int buffer_index
=playlist_buffer_get_index(pb
, index
);
271 return(&(pb
->tracks
[buffer_index
]));
274 /* Initialize the playlist viewer. */
275 static bool playlist_viewer_init(struct playlist_viewer
* viewer
,
276 const char* filename
, bool reload
)
280 bool is_playing
= audio_status() & (AUDIO_STATUS_PLAY
| AUDIO_STATUS_PAUSE
);
281 bool have_list
= filename
|| is_playing
;
282 if (!have_list
&& (global_status
.resume_index
!= -1))
284 /* Try to restore the list from control file */
285 have_list
= (playlist_resume() != -1);
287 if (!have_list
&& (playlist_amount() > 0))
289 /*If dynamic playlist still exists, view it anyway even
290 if playback has reached the end of the playlist */
295 /* Nothing to view, exit */
296 splash(HZ
, str(LANG_CATALOG_NO_PLAYLISTS
));
300 buffer
= plugin_get_buffer(&buffer_size
);
305 viewer
->playlist
= NULL
;
308 /* Viewing playlist on disk */
309 const char *dir
, *file
;
311 char *index_buffer
= NULL
;
312 ssize_t index_buffer_size
= 0;
314 viewer
->playlist
= &temp_playlist
;
316 /* Separate directory from filename */
317 temp_ptr
= strrchr(filename
+1,'/');
332 /* Something is playing, use half the plugin buffer for playlist
334 index_buffer_size
= buffer_size
/ 2;
335 index_buffer
= buffer
;
338 playlist_create_ex(viewer
->playlist
, dir
, file
, index_buffer
,
339 index_buffer_size
, buffer
+index_buffer_size
,
340 buffer_size
-index_buffer_size
);
345 buffer
+= index_buffer_size
;
346 buffer_size
-= index_buffer_size
;
348 playlist_buffer_init(&viewer
->buffer
, buffer
, buffer_size
);
350 viewer
->move_track
= -1;
354 if (viewer
->playlist
)
355 viewer
->selected_track
= 0;
357 viewer
->selected_track
= playlist_get_display_index() - 1;
360 if (!update_playlist(true))
365 /* Format trackname for display purposes */
366 static void format_name(char* dest
, const char* src
)
368 switch (global_settings
.playlist_viewer_track_display
)
373 /* Only display the filename */
374 char* p
= strrchr(src
, '/');
378 /* Remove the extension */
390 /* Format display line */
391 static void format_line(const struct playlist_entry
* track
, char* str
,
397 format_name(name
, track
->name
);
402 if (global_settings
.playlist_viewer_indices
)
403 /* Display playlist index */
404 snprintf(str
, len
, "%d. %s%s", track
->display_index
, skipped
, name
);
406 snprintf(str
, len
, "%s%s", skipped
, name
);
410 /* Update playlist in case something has changed or forced */
411 static bool update_playlist(bool force
)
413 if (!viewer
.playlist
)
414 playlist_get_resume_info(&viewer
.current_playing_track
);
416 viewer
.current_playing_track
= -1;
417 int nb_tracks
=playlist_amount_ex(viewer
.playlist
);
418 force
=force
|| nb_tracks
!= viewer
.num_tracks
;
422 viewer
.num_tracks
= nb_tracks
;
423 if (viewer
.num_tracks
<= 0)
425 playlist_buffer_load_entries_screen(&viewer
.buffer
, FORWARD
);
426 if (viewer
.buffer
.num_loaded
<= 0)
432 /* Menu of playlist commands. Invoked via ON+PLAY on main viewer screen.
433 Returns -1 if USB attached, 0 if no playlist change, and 1 if playlist
435 static int onplay_menu(int index
)
438 struct playlist_entry
* current_track
=
439 playlist_buffer_get_track(&viewer
.buffer
, index
);
440 MENUITEM_STRINGLIST(menu_items
, ID2P(LANG_PLAYLIST
), NULL
,
441 ID2P(LANG_REMOVE
), ID2P(LANG_MOVE
),
442 ID2P(LANG_CATALOG_ADD_TO
), ID2P(LANG_CATALOG_ADD_TO_NEW
));
443 bool current
= (current_track
->index
== viewer
.current_playing_track
);
445 result
= do_menu(&menu_items
, NULL
, NULL
, false);
446 if (result
== MENU_ATTACHED_USB
)
450 else if (result
>= 0)
452 /* Abort current move */
453 viewer
.move_track
= -1;
459 playlist_delete(viewer
.playlist
, current_track
->index
);
462 if (playlist_amount_ex(viewer
.playlist
) <= 0)
466 /* Start playing new track except if it's the lasttrack
467 track in the playlist and repeat mode is disabled */
469 playlist_buffer_get_track(&viewer
.buffer
, index
);
470 if (current_track
->display_index
!=viewer
.num_tracks
||
471 global_settings
.repeat_mode
== REPEAT_ALL
)
473 #if CONFIG_CODEC != SWCODEC
474 talk_buffer_steal(); /* will use the mp3 buffer */
477 viewer
.current_playing_track
= -1;
485 viewer
.move_track
= current_track
->index
;
488 case 2: /* add to catalog */
489 case 3: /* add to a new one */
490 catalog_add_to_a_playlist(current_track
->name
,
500 /* Menu of viewer options. Invoked via F1(r) or Menu(p). */
501 MENUITEM_SETTING(show_icons
, &global_settings
.playlist_viewer_icons
, NULL
);
502 MENUITEM_SETTING(show_indices
, &global_settings
.playlist_viewer_indices
, NULL
);
503 MENUITEM_SETTING(track_display
,
504 &global_settings
.playlist_viewer_track_display
, NULL
);
505 MENUITEM_FUNCTION(save_playlist_item
, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST
),
506 save_playlist_func
, 0, NULL
, Icon_NOICON
);
507 MAKE_MENU(viewer_settings_menu
, ID2P(LANG_PLAYLISTVIEWER_SETTINGS
),
509 &show_icons
, &show_indices
, &track_display
, &save_playlist_item
);
510 static bool viewer_menu(void)
512 return do_menu(&viewer_settings_menu
, NULL
, NULL
, false) == MENU_ATTACHED_USB
;
515 /* Save playlist to disk */
516 static int save_playlist_func(void)
518 save_playlist_screen(viewer
.playlist
);
522 /* View current playlist */
523 bool playlist_viewer(void)
525 return playlist_viewer_ex(NULL
);
528 static int get_track_num( struct playlist_viewer
* local_viewer
,
531 if( local_viewer
->move_track
>= 0 )
533 if( local_viewer
->selected_track
== selected_item
)
535 return local_viewer
->move_track
;
537 else if( local_viewer
->selected_track
> selected_item
538 && selected_item
>= local_viewer
->move_track
)
540 return selected_item
+1;
542 else if( local_viewer
->selected_track
< selected_item
543 && selected_item
<= local_viewer
->move_track
)
545 return selected_item
-1;
548 return selected_item
;
551 static char *playlist_callback_name(int selected_item
,
556 struct playlist_viewer
* local_viewer
= (struct playlist_viewer
*)data
;
558 int track_num
= get_track_num(local_viewer
, selected_item
);
559 struct playlist_entry
*track
=
560 playlist_buffer_get_track(&(local_viewer
->buffer
), track_num
);
562 format_line(track
, buffer
, buffer_len
);
568 static int playlist_callback_icons(int selected_item
, void *data
)
570 struct playlist_viewer
* local_viewer
=(struct playlist_viewer
*)data
;
572 int track_num
= get_track_num(local_viewer
, selected_item
);
573 struct playlist_entry
*track
=
574 playlist_buffer_get_track(&(local_viewer
->buffer
), track_num
);
576 if (track
->index
== local_viewer
->current_playing_track
)
578 /* Current playing track */
581 else if (track
->index
== local_viewer
->move_track
)
583 /* Track we are moving */
586 else if (track
->queued
)
595 /* Main viewer function. Filename identifies playlist to be viewed. If NULL,
596 view current playlist. */
597 bool playlist_viewer_ex(const char* filename
)
599 bool ret
= false; /* return value */
600 bool exit
=false; /* exit viewer */
602 struct gui_synclist playlist_lists
;
603 if (!playlist_viewer_init(&viewer
, filename
, false))
606 gui_synclist_init(&playlist_lists
, playlist_callback_name
,
607 &viewer
, false, 1, NULL
);
608 gui_synclist_set_icon_callback(&playlist_lists
,
609 global_settings
.playlist_viewer_icons
?
610 &playlist_callback_icons
:NULL
);
611 gui_synclist_set_nb_items(&playlist_lists
, viewer
.num_tracks
);
612 gui_synclist_select_item(&playlist_lists
, viewer
.selected_track
);
613 gui_synclist_set_title(&playlist_lists
, str(LANG_PLAYLIST
), Icon_Playlist
);
614 gui_synclist_draw(&playlist_lists
);
619 if (global_status
.resume_index
!= -1 && !viewer
.playlist
)
620 playlist_get_resume_info(&track
);
624 if (track
!= viewer
.current_playing_track
||
625 playlist_amount_ex(viewer
.playlist
) != viewer
.num_tracks
)
627 /* Playlist has changed (new track started?) */
628 if (!update_playlist(false))
630 /*Needed because update_playlist gives wrong value when
632 viewer
.current_playing_track
= track
;
633 gui_synclist_set_nb_items(&playlist_lists
, viewer
.num_tracks
);
634 /* Abort move on playlist change */
635 viewer
.move_track
= -1;
636 gui_synclist_draw(&playlist_lists
);
639 /* Timeout so we can determine if play status has changed */
640 button
= get_action(CONTEXT_TREE
,HZ
/2);
641 if( (gui_synclist_do_button(&playlist_lists
, &button
,LIST_WRAP_UNLESS_HELD
)) )
643 viewer
.selected_track
=gui_synclist_get_sel_pos(&playlist_lists
);
644 if(playlist_buffer_needs_reload(&viewer
.buffer
,
645 viewer
.selected_track
))
646 playlist_buffer_load_entries_screen(&viewer
.buffer
,
647 button
==ACTION_STD_NEXT
?
652 gui_synclist_draw(&playlist_lists
);
656 case ACTION_TREE_WPS
:
657 case ACTION_STD_CANCEL
:
659 if (viewer
.move_track
>= 0)
661 viewer
.selected_track
= viewer
.move_track
;
662 gui_synclist_select_item(&playlist_lists
, viewer
.move_track
);
663 viewer
.move_track
= -1;
664 gui_synclist_draw(&playlist_lists
);
672 struct playlist_entry
* current_track
=
673 playlist_buffer_get_track(&viewer
.buffer
,
674 viewer
.selected_track
);
675 if (viewer
.move_track
>= 0)
680 ret_val
= playlist_move(viewer
.playlist
, viewer
.move_track
,
681 current_track
->index
);
683 splashf(HZ
, (unsigned char *)"%s %s", str(LANG_MOVE
),
685 update_playlist(true);
686 viewer
.move_track
= -1;
688 else if (!viewer
.playlist
)
691 playlist_start(current_track
->index
, 0);
692 update_playlist(false);
697 if (playlist_set_current(viewer
.playlist
) < 0)
700 playlist_start(current_track
->index
, 0);
702 /* Our playlist is now the current list */
703 if (!playlist_viewer_init(&viewer
, NULL
, true))
706 gui_synclist_draw(&playlist_lists
);
710 case ACTION_STD_CONTEXT
:
715 ret_val
= onplay_menu(viewer
.selected_track
);
724 /* Playlist changed */
725 gui_synclist_del_item(&playlist_lists
);
726 update_playlist(true);
727 if (viewer
.num_tracks
<= 0)
729 if(viewer
.selected_track
>= viewer
.num_tracks
)
730 viewer
.selected_track
= viewer
.num_tracks
-1;
732 gui_synclist_draw(&playlist_lists
);
735 case ACTION_STD_MENU
:
741 gui_synclist_set_icon_callback(
743 global_settings
.playlist_viewer_icons
?
744 &playlist_callback_icons
:NULL
746 gui_synclist_draw(&playlist_lists
);
750 gui_syncstatusbar_draw(&statusbars
, false);
754 if(default_event_handler(button
) == SYS_USB_CONNECTED
)
765 playlist_close(viewer
.playlist
);
769 static char *playlist_search_callback_name(int selected_item
, void * data
,
770 char *buffer
, size_t buffer_len
)
772 (void)buffer_len
; /* this should probably be used */
773 int *found_indicies
= (int*)data
;
774 static struct playlist_track_info track
;
775 playlist_get_track_info(viewer
.playlist
, found_indicies
[selected_item
], &track
);
776 format_name(buffer
, track
.filename
);
780 bool search_playlist(void)
782 char search_str
[32] = "";
783 bool ret
= false, exit
= false;
784 int i
, playlist_count
;
785 int found_indicies
[MAX_PLAYLIST_ENTRIES
],found_indicies_count
= 0;
787 struct gui_synclist playlist_lists
;
788 struct playlist_track_info track
;
790 if (!playlist_viewer_init(&viewer
, 0, false))
792 if (kbd_input(search_str
,sizeof(search_str
)) == -1)
795 playlist_count
= playlist_amount_ex(viewer
.playlist
);
796 for (i
=0;(i
<playlist_count
)&&(found_indicies_count
<MAX_PLAYLIST_ENTRIES
);i
++)
798 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG
), found_indicies_count
,
799 str(LANG_OFF_ABORT
));
800 if (action_userabort(TIMEOUT_NOBLOCK
))
802 if (!found_indicies_count
)
806 playlist_get_track_info(viewer
.playlist
,i
,&track
);
807 if (strcasestr(track
.filename
,search_str
))
809 found_indicies
[found_indicies_count
++] = track
.index
;
812 if (!found_indicies_count
)
817 gui_synclist_init(&playlist_lists
, playlist_search_callback_name
,
818 found_indicies
, false, 1, NULL
);
819 gui_synclist_set_icon_callback(&playlist_lists
, NULL
);
820 gui_synclist_set_nb_items(&playlist_lists
, found_indicies_count
);
821 gui_synclist_select_item(&playlist_lists
, 0);
822 gui_synclist_draw(&playlist_lists
);
825 button
= get_action(CONTEXT_LIST
,TIMEOUT_BLOCK
);
826 if (gui_synclist_do_button(&playlist_lists
, &button
, LIST_WRAP_UNLESS_HELD
))
830 case ACTION_STD_CANCEL
:
836 found_indicies
[gui_synclist_get_sel_pos(&playlist_lists
)]
843 if(default_event_handler(button
) == SYS_USB_CONNECTED
)