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
36 #include "filetypes.h"
42 #include "backlight.h"
46 #include "playlist_viewer.h"
47 #include "playlist_catalog.h"
50 #include "statusbar.h"
52 #include "playlist_menu.h"
55 /* Maximum number of tracks we can have loaded at one time */
56 #define MAX_PLAYLIST_ENTRIES 200
58 /* The number of items between the selected one and the end/start of
59 * the buffer under which the buffer must reload */
60 #define MIN_BUFFER_MARGIN (screens[0].getnblines()+1)
62 /* Information about a specific track */
63 struct playlist_entry
{
64 char *name
; /* Formatted track name */
65 int index
; /* Playlist index */
66 int display_index
; /* Display index */
67 bool queued
; /* Is track queued? */
68 bool skipped
; /* Is track marked as bad? */
77 struct playlist_buffer
79 char *name_buffer
; /* Buffer used to store track names */
80 int buffer_size
; /* Size of name buffer */
82 int first_index
; /* Real index of first track loaded inside
85 enum direction direction
; /* Direction of the buffer (if the buffer
86 was loaded BACKWARD, the last track in
87 the buffer has a real index < to the
88 real index of the the first track)*/
90 struct playlist_entry tracks
[MAX_PLAYLIST_ENTRIES
];
91 int num_loaded
; /* Number of track entries loaded in buffer */
94 /* Global playlist viewer settings */
95 struct playlist_viewer
{
96 struct playlist_info
* playlist
; /* playlist being viewed */
97 int num_tracks
; /* Number of tracks in playlist */
98 int current_playing_track
; /* Index of current playing track */
99 int selected_track
; /* The selected track, relative (first is 0)*/
100 int move_track
; /* Playlist index of track to move or -1 */
101 struct playlist_buffer buffer
;
104 static struct playlist_viewer viewer
;
106 /* Used when viewing playlists on disk */
107 static struct playlist_info temp_playlist
;
109 static void playlist_buffer_init(struct playlist_buffer
*pb
, char *names_buffer
,
110 int names_buffer_size
);
111 static void playlist_buffer_load_entries(struct playlist_buffer
* pb
, int index
,
112 enum direction direction
);
113 static int playlist_entry_load(struct playlist_entry
*entry
, int index
,
114 char* name_buffer
, int remaining_size
);
116 static struct playlist_entry
* playlist_buffer_get_track(struct playlist_buffer
*pb
,
119 static bool playlist_viewer_init(struct playlist_viewer
* viewer
,
120 const char* filename
, bool reload
);
122 static void format_name(char* dest
, const char* src
);
123 static void format_line(const struct playlist_entry
* track
, char* str
,
126 static bool update_playlist(bool force
);
127 static int onplay_menu(int index
);
128 static bool viewer_menu(void);
129 static int save_playlist_func(void);
131 static void playlist_buffer_init(struct playlist_buffer
*pb
, char *names_buffer
,
132 int names_buffer_size
)
134 pb
->name_buffer
=names_buffer
;
135 pb
->buffer_size
=names_buffer_size
;
141 * Loads the entries following 'index' in the playlist buffer
143 static void playlist_buffer_load_entries(struct playlist_buffer
*pb
, int index
,
144 enum direction direction
)
146 int num_entries
= viewer
.num_tracks
;
147 char* p
= pb
->name_buffer
;
148 int remaining
= pb
->buffer_size
;
151 pb
->first_index
= index
;
152 if (num_entries
> MAX_PLAYLIST_ENTRIES
)
153 num_entries
= MAX_PLAYLIST_ENTRIES
;
155 for(i
=0; i
<num_entries
; i
++)
157 int len
= playlist_entry_load(&(pb
->tracks
[i
]), index
, p
, remaining
);
160 /* Out of name buffer space */
168 if(direction
==FORWARD
)
172 index
+=viewer
.num_tracks
;
173 index
%=viewer
.num_tracks
;
175 pb
->direction
=direction
;
179 static void playlist_buffer_load_entries_screen(struct playlist_buffer
* pb
,
180 enum direction direction
)
182 if(direction
==FORWARD
)
184 int min_start
=viewer
.selected_track
-2*screens
[0].getnblines();
186 min_start
+=viewer
.num_tracks
;
187 min_start
%= viewer
.num_tracks
;
188 playlist_buffer_load_entries(pb
, min_start
, FORWARD
);
192 int max_start
=viewer
.selected_track
+2*screens
[0].getnblines();
193 max_start
%=viewer
.num_tracks
;
194 playlist_buffer_load_entries(pb
, max_start
, BACKWARD
);
198 static int playlist_entry_load(struct playlist_entry
*entry
, int index
,
199 char* name_buffer
, int remaining_size
)
201 struct playlist_track_info info
;
204 /* Playlist viewer orders songs based on display index. We need to
205 convert to real playlist index to access track */
206 index
= (index
+ playlist_get_first_index(viewer
.playlist
)) %
208 if (playlist_get_track_info(viewer
.playlist
, index
, &info
) < 0)
211 len
= strlen(info
.filename
) + 1;
213 if (len
<= remaining_size
)
215 strcpy(name_buffer
, info
.filename
);
217 entry
->name
= name_buffer
;
218 entry
->index
= info
.index
;
219 entry
->display_index
= info
.display_index
;
220 entry
->queued
= info
.attr
& PLAYLIST_ATTR_QUEUED
;
221 entry
->skipped
= info
.attr
& PLAYLIST_ATTR_SKIPPED
;
227 static int playlist_buffer_get_index(struct playlist_buffer
*pb
, int index
)
230 if(pb
->direction
==FORWARD
)
232 if(index
>=pb
->first_index
)
233 buffer_index
=index
-pb
->first_index
;
234 else /* rotation : track0 in buffer + requested track */
235 buffer_index
=(viewer
.num_tracks
-pb
->first_index
)+(index
);
239 if(index
<=pb
->first_index
)
240 buffer_index
=pb
->first_index
-index
;
241 else /* rotation : track0 in buffer + dist from the last track
242 to the requested track (num_tracks-requested track) */
243 buffer_index
=(pb
->first_index
)+(viewer
.num_tracks
-index
);
245 return(buffer_index
);
248 #define distance(a, b) \
249 a>b? (a) - (b) : (b) - (a)
250 static bool playlist_buffer_needs_reload(struct playlist_buffer
* pb
,
253 if(pb
->num_loaded
==viewer
.num_tracks
)
255 int selected_index
=playlist_buffer_get_index(pb
, track_index
);
256 int first_buffer_index
=playlist_buffer_get_index(pb
, pb
->first_index
);
257 int distance_beginning
=distance(selected_index
, first_buffer_index
);
258 if(distance_beginning
<MIN_BUFFER_MARGIN
)
261 if(pb
->num_loaded
- distance_beginning
< MIN_BUFFER_MARGIN
)
266 static struct playlist_entry
* playlist_buffer_get_track(struct playlist_buffer
*pb
,
269 int buffer_index
=playlist_buffer_get_index(pb
, index
);
270 return(&(pb
->tracks
[buffer_index
]));
273 /* Initialize the playlist viewer. */
274 static bool playlist_viewer_init(struct playlist_viewer
* viewer
,
275 const char* filename
, bool reload
)
279 bool is_playing
= audio_status() & (AUDIO_STATUS_PLAY
| AUDIO_STATUS_PAUSE
);
280 bool have_list
= filename
|| is_playing
;
281 if (!have_list
&& (global_status
.resume_index
!= -1))
283 /* Try to restore the list from control file */
284 have_list
= (playlist_resume() != -1);
286 if (!have_list
&& (playlist_amount() > 0))
288 /*If dynamic playlist still exists, view it anyway even
289 if playback has reached the end of the playlist */
294 /* Nothing to view, exit */
295 splash(HZ
, str(LANG_CATALOG_NO_PLAYLISTS
));
299 buffer
= plugin_get_buffer(&buffer_size
);
304 viewer
->playlist
= NULL
;
307 /* Viewing playlist on disk */
308 const char *dir
, *file
;
310 char *index_buffer
= NULL
;
311 ssize_t index_buffer_size
= 0;
313 viewer
->playlist
= &temp_playlist
;
315 /* Separate directory from filename */
316 temp_ptr
= strrchr(filename
+1,'/');
331 /* Something is playing, use half the plugin buffer for playlist
333 index_buffer_size
= buffer_size
/ 2;
334 index_buffer
= buffer
;
337 playlist_create_ex(viewer
->playlist
, dir
, file
, index_buffer
,
338 index_buffer_size
, buffer
+index_buffer_size
,
339 buffer_size
-index_buffer_size
);
344 buffer
+= index_buffer_size
;
345 buffer_size
-= index_buffer_size
;
347 playlist_buffer_init(&viewer
->buffer
, buffer
, buffer_size
);
349 viewer
->move_track
= -1;
353 if (viewer
->playlist
)
354 viewer
->selected_track
= 0;
356 viewer
->selected_track
= playlist_get_display_index() - 1;
359 if (!update_playlist(true))
364 /* Format trackname for display purposes */
365 static void format_name(char* dest
, const char* src
)
367 switch (global_settings
.playlist_viewer_track_display
)
372 /* Only display the filename */
373 char* p
= strrchr(src
, '/');
377 /* Remove the extension */
389 /* Format display line */
390 static void format_line(const struct playlist_entry
* track
, char* str
,
396 format_name(name
, track
->name
);
401 if (global_settings
.playlist_viewer_indices
)
402 /* Display playlist index */
403 snprintf(str
, len
, "%d. %s%s", track
->display_index
, skipped
, name
);
405 snprintf(str
, len
, "%s%s", skipped
, name
);
409 /* Update playlist in case something has changed or forced */
410 static bool update_playlist(bool force
)
412 if (!viewer
.playlist
)
413 playlist_get_resume_info(&viewer
.current_playing_track
);
415 viewer
.current_playing_track
= -1;
416 int nb_tracks
=playlist_amount_ex(viewer
.playlist
);
417 force
=force
|| nb_tracks
!= viewer
.num_tracks
;
421 viewer
.num_tracks
= nb_tracks
;
422 if (viewer
.num_tracks
<= 0)
424 playlist_buffer_load_entries_screen(&viewer
.buffer
, FORWARD
);
425 if (viewer
.buffer
.num_loaded
<= 0)
431 /* Menu of playlist commands. Invoked via ON+PLAY on main viewer screen.
432 Returns -1 if USB attached, 0 if no playlist change, and 1 if playlist
434 static int onplay_menu(int index
)
437 struct playlist_entry
* current_track
=
438 playlist_buffer_get_track(&viewer
.buffer
, index
);
439 MENUITEM_STRINGLIST(menu_items
, ID2P(LANG_PLAYLIST
), NULL
,
440 ID2P(LANG_REMOVE
), ID2P(LANG_MOVE
),
441 ID2P(LANG_CATALOG_ADD_TO
), ID2P(LANG_CATALOG_ADD_TO_NEW
));
442 bool current
= (current_track
->index
== viewer
.current_playing_track
);
444 result
= do_menu(&menu_items
, NULL
, NULL
, false);
445 if (result
== MENU_ATTACHED_USB
)
449 else if (result
>= 0)
451 /* Abort current move */
452 viewer
.move_track
= -1;
458 playlist_delete(viewer
.playlist
, current_track
->index
);
461 if (playlist_amount_ex(viewer
.playlist
) <= 0)
465 /* Start playing new track except if it's the lasttrack
466 track in the playlist and repeat mode is disabled */
468 playlist_buffer_get_track(&viewer
.buffer
, index
);
469 if (current_track
->display_index
!=viewer
.num_tracks
||
470 global_settings
.repeat_mode
== REPEAT_ALL
)
472 #if CONFIG_CODEC != SWCODEC
473 talk_buffer_steal(); /* will use the mp3 buffer */
476 viewer
.current_playing_track
= -1;
484 viewer
.move_track
= current_track
->index
;
487 case 2: /* add to catalog */
488 case 3: /* add to a new one */
489 catalog_add_to_a_playlist(current_track
->name
,
499 /* Menu of viewer options. Invoked via F1(r) or Menu(p). */
500 MENUITEM_SETTING(show_icons
, &global_settings
.playlist_viewer_icons
, NULL
);
501 MENUITEM_SETTING(show_indices
, &global_settings
.playlist_viewer_indices
, NULL
);
502 MENUITEM_SETTING(track_display
,
503 &global_settings
.playlist_viewer_track_display
, NULL
);
504 MENUITEM_FUNCTION(save_playlist_item
, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST
),
505 save_playlist_func
, 0, NULL
, Icon_NOICON
);
506 MAKE_MENU(viewer_settings_menu
, ID2P(LANG_PLAYLISTVIEWER_SETTINGS
),
508 &show_icons
, &show_indices
, &track_display
, &save_playlist_item
);
509 static bool viewer_menu(void)
511 return do_menu(&viewer_settings_menu
, NULL
, NULL
, false) == MENU_ATTACHED_USB
;
514 /* Save playlist to disk */
515 static int save_playlist_func(void)
517 save_playlist_screen(viewer
.playlist
);
521 /* View current playlist */
522 bool playlist_viewer(void)
524 return playlist_viewer_ex(NULL
);
527 static int get_track_num( struct playlist_viewer
* local_viewer
,
530 if( local_viewer
->move_track
>= 0 )
532 if( local_viewer
->selected_track
== selected_item
)
534 return local_viewer
->move_track
;
536 else if( local_viewer
->selected_track
> selected_item
537 && selected_item
>= local_viewer
->move_track
)
539 return selected_item
+1;
541 else if( local_viewer
->selected_track
< selected_item
542 && selected_item
<= local_viewer
->move_track
)
544 return selected_item
-1;
547 return selected_item
;
550 static char *playlist_callback_name(int selected_item
,
555 struct playlist_viewer
* local_viewer
= (struct playlist_viewer
*)data
;
557 int track_num
= get_track_num(local_viewer
, selected_item
);
558 struct playlist_entry
*track
=
559 playlist_buffer_get_track(&(local_viewer
->buffer
), track_num
);
561 format_line(track
, buffer
, buffer_len
);
567 static int playlist_callback_icons(int selected_item
, void *data
)
569 struct playlist_viewer
* local_viewer
=(struct playlist_viewer
*)data
;
571 int track_num
= get_track_num(local_viewer
, selected_item
);
572 struct playlist_entry
*track
=
573 playlist_buffer_get_track(&(local_viewer
->buffer
), track_num
);
575 if (track
->index
== local_viewer
->current_playing_track
)
577 /* Current playing track */
580 else if (track
->index
== local_viewer
->move_track
)
582 /* Track we are moving */
585 else if (track
->queued
)
594 /* Main viewer function. Filename identifies playlist to be viewed. If NULL,
595 view current playlist. */
596 bool playlist_viewer_ex(const char* filename
)
598 bool ret
= false; /* return value */
599 bool exit
=false; /* exit viewer */
601 struct gui_synclist playlist_lists
;
602 if (!playlist_viewer_init(&viewer
, filename
, false))
605 gui_synclist_init(&playlist_lists
, playlist_callback_name
,
606 &viewer
, false, 1, NULL
);
607 gui_synclist_set_icon_callback(&playlist_lists
,
608 global_settings
.playlist_viewer_icons
?
609 &playlist_callback_icons
:NULL
);
610 gui_synclist_set_nb_items(&playlist_lists
, viewer
.num_tracks
);
611 gui_synclist_select_item(&playlist_lists
, viewer
.selected_track
);
612 gui_synclist_set_title(&playlist_lists
, str(LANG_PLAYLIST
), Icon_Playlist
);
613 gui_synclist_draw(&playlist_lists
);
618 if (global_status
.resume_index
!= -1 && !viewer
.playlist
)
619 playlist_get_resume_info(&track
);
623 if (track
!= viewer
.current_playing_track
||
624 playlist_amount_ex(viewer
.playlist
) != viewer
.num_tracks
)
626 /* Playlist has changed (new track started?) */
627 if (!update_playlist(false))
629 /*Needed because update_playlist gives wrong value when
631 viewer
.current_playing_track
= track
;
632 gui_synclist_set_nb_items(&playlist_lists
, viewer
.num_tracks
);
633 /* Abort move on playlist change */
634 viewer
.move_track
= -1;
635 gui_synclist_draw(&playlist_lists
);
638 /* Timeout so we can determine if play status has changed */
639 button
= get_action(CONTEXT_TREE
,HZ
/2);
640 if( (gui_synclist_do_button(&playlist_lists
, &button
,LIST_WRAP_UNLESS_HELD
)) )
642 viewer
.selected_track
=gui_synclist_get_sel_pos(&playlist_lists
);
643 if(playlist_buffer_needs_reload(&viewer
.buffer
,
644 viewer
.selected_track
))
645 playlist_buffer_load_entries_screen(&viewer
.buffer
,
646 button
==ACTION_STD_NEXT
?
651 gui_synclist_draw(&playlist_lists
);
655 case ACTION_TREE_WPS
:
656 case ACTION_STD_CANCEL
:
658 if (viewer
.move_track
>= 0)
660 viewer
.selected_track
= viewer
.move_track
;
661 gui_synclist_select_item(&playlist_lists
, viewer
.move_track
);
662 viewer
.move_track
= -1;
663 gui_synclist_draw(&playlist_lists
);
671 struct playlist_entry
* current_track
=
672 playlist_buffer_get_track(&viewer
.buffer
,
673 viewer
.selected_track
);
674 if (viewer
.move_track
>= 0)
679 ret_val
= playlist_move(viewer
.playlist
, viewer
.move_track
,
680 current_track
->index
);
682 splashf(HZ
, (unsigned char *)"%s %s", str(LANG_MOVE
),
684 update_playlist(true);
685 viewer
.move_track
= -1;
687 else if (!viewer
.playlist
)
690 playlist_start(current_track
->index
, 0);
691 update_playlist(false);
696 if (playlist_set_current(viewer
.playlist
) < 0)
699 playlist_start(current_track
->index
, 0);
701 /* Our playlist is now the current list */
702 if (!playlist_viewer_init(&viewer
, NULL
, true))
705 gui_synclist_draw(&playlist_lists
);
709 case ACTION_STD_CONTEXT
:
714 ret_val
= onplay_menu(viewer
.selected_track
);
723 /* Playlist changed */
724 gui_synclist_del_item(&playlist_lists
);
725 update_playlist(true);
726 if (viewer
.num_tracks
<= 0)
728 if(viewer
.selected_track
>= viewer
.num_tracks
)
729 viewer
.selected_track
= viewer
.num_tracks
-1;
731 gui_synclist_draw(&playlist_lists
);
734 case ACTION_STD_MENU
:
740 gui_synclist_set_icon_callback(
742 global_settings
.playlist_viewer_icons
?
743 &playlist_callback_icons
:NULL
745 gui_synclist_draw(&playlist_lists
);
749 if(default_event_handler(button
) == SYS_USB_CONNECTED
)
760 playlist_close(viewer
.playlist
);
764 static char *playlist_search_callback_name(int selected_item
, void * data
,
765 char *buffer
, size_t buffer_len
)
767 (void)buffer_len
; /* this should probably be used */
768 int *found_indicies
= (int*)data
;
769 static struct playlist_track_info track
;
770 playlist_get_track_info(viewer
.playlist
, found_indicies
[selected_item
], &track
);
771 format_name(buffer
, track
.filename
);
775 bool search_playlist(void)
777 char search_str
[32] = "";
778 bool ret
= false, exit
= false;
779 int i
, playlist_count
;
780 int found_indicies
[MAX_PLAYLIST_ENTRIES
];
781 int found_indicies_count
= 0, last_found_count
= -1;
783 struct gui_synclist playlist_lists
;
784 struct playlist_track_info track
;
786 if (!playlist_viewer_init(&viewer
, 0, false))
788 if (kbd_input(search_str
, sizeof(search_str
)) == -1)
791 playlist_count
= playlist_amount_ex(viewer
.playlist
);
795 for (i
=0; (i
<playlist_count
)&&(found_indicies_count
<MAX_PLAYLIST_ENTRIES
); i
++)
797 if (found_indicies_count
!= last_found_count
)
799 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG
), found_indicies_count
,
800 str(LANG_OFF_ABORT
));
801 last_found_count
= found_indicies_count
;
804 if (action_userabort(TIMEOUT_NOBLOCK
))
807 playlist_get_track_info(viewer
.playlist
, i
, &track
);
809 if (strcasestr(track
.filename
,search_str
))
810 found_indicies
[found_indicies_count
++] = track
.index
;
817 if (!found_indicies_count
)
823 gui_synclist_init(&playlist_lists
, playlist_search_callback_name
,
824 found_indicies
, false, 1, NULL
);
825 gui_synclist_set_title(&playlist_lists
, str(LANG_SEARCH_RESULTS
), NOICON
);
826 gui_synclist_set_icon_callback(&playlist_lists
, NULL
);
827 gui_synclist_set_nb_items(&playlist_lists
, found_indicies_count
);
828 gui_synclist_select_item(&playlist_lists
, 0);
829 gui_synclist_draw(&playlist_lists
);
832 button
= get_action(CONTEXT_LIST
, HZ
/4);
833 if (gui_synclist_do_button(&playlist_lists
, &button
, LIST_WRAP_UNLESS_HELD
))
837 case ACTION_STD_CANCEL
:
843 found_indicies
[gui_synclist_get_sel_pos(&playlist_lists
)]
849 if(default_event_handler(button
) == SYS_USB_CONNECTED
)