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"
51 #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].getnblines()+1)
60 /* Information about a specific track */
61 struct playlist_entry
{
62 char *name
; /* Formatted track name */
63 int index
; /* Playlist index */
64 int display_index
; /* Display index */
65 bool queued
; /* Is track queued? */
66 bool skipped
; /* Is track marked as bad? */
75 struct playlist_buffer
77 char *name_buffer
; /* Buffer used to store track names */
78 int buffer_size
; /* Size of name buffer */
80 int first_index
; /* Real index of first track loaded inside
83 enum direction direction
; /* Direction of the buffer (if the buffer
84 was loaded BACKWARD, the last track in
85 the buffer has a real index < to the
86 real index of the the first track)*/
88 struct playlist_entry tracks
[MAX_PLAYLIST_ENTRIES
];
89 int num_loaded
; /* Number of track entries loaded in buffer */
92 /* Global playlist viewer settings */
93 struct playlist_viewer
{
94 struct playlist_info
* playlist
; /* playlist being viewed */
95 int num_tracks
; /* Number of tracks in playlist */
96 int current_playing_track
; /* Index of current playing track */
97 int selected_track
; /* The selected track, relative (first is 0) */
98 int moving_track
; /* The track to move, relative (first is 0)
99 or -1 if nothing is currently being moved */
100 int moving_playlist_index
; /* Playlist-relative index (as opposed to
101 viewer-relative index) of moving track */
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].getnblines();
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].getnblines();
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
->moving_track
= -1;
351 viewer
->moving_playlist_index
= -1;
355 if (viewer
->playlist
)
356 viewer
->selected_track
= 0;
358 viewer
->selected_track
= playlist_get_display_index() - 1;
361 if (!update_playlist(true))
366 /* Format trackname for display purposes */
367 static void format_name(char* dest
, const char* src
)
369 switch (global_settings
.playlist_viewer_track_display
)
374 /* Only display the filename */
375 char* p
= strrchr(src
, '/');
379 /* Remove the extension */
391 /* Format display line */
392 static void format_line(const struct playlist_entry
* track
, char* str
,
398 format_name(name
, track
->name
);
403 if (global_settings
.playlist_viewer_indices
)
404 /* Display playlist index */
405 snprintf(str
, len
, "%d. %s%s", track
->display_index
, skipped
, name
);
407 snprintf(str
, len
, "%s%s", skipped
, name
);
411 /* Update playlist in case something has changed or forced */
412 static bool update_playlist(bool force
)
414 if (!viewer
.playlist
)
415 playlist_get_resume_info(&viewer
.current_playing_track
);
417 viewer
.current_playing_track
= -1;
418 int nb_tracks
=playlist_amount_ex(viewer
.playlist
);
419 force
=force
|| nb_tracks
!= viewer
.num_tracks
;
423 viewer
.num_tracks
= nb_tracks
;
424 if (viewer
.num_tracks
<= 0)
426 global_status
.resume_index
= -1;
427 global_status
.resume_offset
= -1;
430 playlist_buffer_load_entries_screen(&viewer
.buffer
, FORWARD
);
431 if (viewer
.buffer
.num_loaded
<= 0)
433 global_status
.resume_index
= -1;
434 global_status
.resume_offset
= -1;
441 /* Menu of playlist commands. Invoked via ON+PLAY on main viewer screen.
442 Returns -1 if USB attached, 0 if no playlist change, and 1 if playlist
444 static int onplay_menu(int index
)
447 struct playlist_entry
* current_track
=
448 playlist_buffer_get_track(&viewer
.buffer
, index
);
449 MENUITEM_STRINGLIST(menu_items
, ID2P(LANG_PLAYLIST
), NULL
,
450 ID2P(LANG_REMOVE
), ID2P(LANG_MOVE
),
451 ID2P(LANG_CATALOG_ADD_TO
), ID2P(LANG_CATALOG_ADD_TO_NEW
));
452 bool current
= (current_track
->index
== viewer
.current_playing_track
);
454 result
= do_menu(&menu_items
, NULL
, NULL
, false);
455 if (result
== MENU_ATTACHED_USB
)
459 else if (result
>= 0)
461 /* Abort current move */
462 viewer
.moving_track
= -1;
463 viewer
.moving_playlist_index
= -1;
469 playlist_delete(viewer
.playlist
, current_track
->index
);
472 if (playlist_amount_ex(viewer
.playlist
) <= 0)
476 /* Start playing new track except if it's the lasttrack
477 track in the playlist and repeat mode is disabled */
479 playlist_buffer_get_track(&viewer
.buffer
, index
);
480 if (current_track
->display_index
!=viewer
.num_tracks
||
481 global_settings
.repeat_mode
== REPEAT_ALL
)
483 #if CONFIG_CODEC != SWCODEC
484 talk_buffer_steal(); /* will use the mp3 buffer */
487 viewer
.current_playing_track
= -1;
495 viewer
.moving_track
= index
;
496 viewer
.moving_playlist_index
= current_track
->index
;
499 case 2: /* add to catalog */
500 case 3: /* add to a new one */
501 catalog_add_to_a_playlist(current_track
->name
,
511 /* Menu of viewer options. Invoked via F1(r) or Menu(p). */
512 MENUITEM_SETTING(show_icons
, &global_settings
.playlist_viewer_icons
, NULL
);
513 MENUITEM_SETTING(show_indices
, &global_settings
.playlist_viewer_indices
, NULL
);
514 MENUITEM_SETTING(track_display
,
515 &global_settings
.playlist_viewer_track_display
, NULL
);
516 MENUITEM_FUNCTION(save_playlist_item
, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST
),
517 save_playlist_func
, 0, NULL
, Icon_NOICON
);
518 MAKE_MENU(viewer_settings_menu
, ID2P(LANG_PLAYLISTVIEWER_SETTINGS
),
520 &show_icons
, &show_indices
, &track_display
, &save_playlist_item
);
521 static bool viewer_menu(void)
523 return do_menu(&viewer_settings_menu
, NULL
, NULL
, false) == MENU_ATTACHED_USB
;
526 /* Save playlist to disk */
527 static int save_playlist_func(void)
529 save_playlist_screen(viewer
.playlist
);
533 /* View current playlist */
534 bool playlist_viewer(void)
536 return playlist_viewer_ex(NULL
);
539 static int get_track_num( struct playlist_viewer
* local_viewer
,
542 if( local_viewer
->moving_track
>= 0 )
544 if( local_viewer
->selected_track
== selected_item
)
546 return local_viewer
->moving_track
;
548 else if( local_viewer
->selected_track
> selected_item
549 && selected_item
>= local_viewer
->moving_track
)
551 return selected_item
+1; /* move down */
553 else if( local_viewer
->selected_track
< selected_item
554 && selected_item
<= local_viewer
->moving_track
)
556 return selected_item
-1; /* move up */
559 return selected_item
;
562 static char *playlist_callback_name(int selected_item
,
567 struct playlist_viewer
* local_viewer
= (struct playlist_viewer
*)data
;
569 int track_num
= get_track_num(local_viewer
, selected_item
);
570 struct playlist_entry
*track
=
571 playlist_buffer_get_track(&(local_viewer
->buffer
), track_num
);
573 format_line(track
, buffer
, buffer_len
);
579 static enum themable_icons
playlist_callback_icons(int selected_item
,
582 struct playlist_viewer
* local_viewer
=(struct playlist_viewer
*)data
;
584 int track_num
= get_track_num(local_viewer
, selected_item
);
585 struct playlist_entry
*track
=
586 playlist_buffer_get_track(&(local_viewer
->buffer
), track_num
);
588 if (track
->index
== local_viewer
->current_playing_track
)
590 /* Current playing track */
593 else if (track
->index
== local_viewer
->moving_playlist_index
)
595 /* Track we are moving */
598 else if (track
->queued
)
607 /* Main viewer function. Filename identifies playlist to be viewed. If NULL,
608 view current playlist. */
609 bool playlist_viewer_ex(const char* filename
)
611 bool ret
= false; /* return value */
612 bool exit
=false; /* exit viewer */
614 struct gui_synclist playlist_lists
;
615 if (!playlist_viewer_init(&viewer
, filename
, false))
618 gui_synclist_init(&playlist_lists
, playlist_callback_name
,
619 &viewer
, false, 1, NULL
);
620 gui_synclist_set_icon_callback(&playlist_lists
,
621 global_settings
.playlist_viewer_icons
?
622 &playlist_callback_icons
:NULL
);
623 gui_synclist_set_nb_items(&playlist_lists
, viewer
.num_tracks
);
624 gui_synclist_select_item(&playlist_lists
, viewer
.selected_track
);
625 gui_synclist_set_title(&playlist_lists
, str(LANG_PLAYLIST
), Icon_Playlist
);
626 gui_synclist_draw(&playlist_lists
);
631 if (global_status
.resume_index
!= -1 && !viewer
.playlist
)
632 playlist_get_resume_info(&track
);
636 if (track
!= viewer
.current_playing_track
||
637 playlist_amount_ex(viewer
.playlist
) != viewer
.num_tracks
)
639 /* Playlist has changed (new track started?) */
640 if (!update_playlist(false))
642 /*Needed because update_playlist gives wrong value when
644 viewer
.current_playing_track
= track
;
645 gui_synclist_set_nb_items(&playlist_lists
, viewer
.num_tracks
);
646 /* Abort move on playlist change */
647 viewer
.moving_track
= -1;
648 viewer
.moving_playlist_index
= -1;
649 gui_synclist_draw(&playlist_lists
);
652 /* Timeout so we can determine if play status has changed */
653 button
= get_action(CONTEXT_TREE
,HZ
/2);
654 if( (gui_synclist_do_button(&playlist_lists
, &button
,LIST_WRAP_UNLESS_HELD
)) )
656 viewer
.selected_track
=gui_synclist_get_sel_pos(&playlist_lists
);
657 if(playlist_buffer_needs_reload(&viewer
.buffer
,
658 viewer
.selected_track
))
659 playlist_buffer_load_entries_screen(&viewer
.buffer
,
660 button
==ACTION_STD_NEXT
?
665 gui_synclist_draw(&playlist_lists
);
669 case ACTION_TREE_WPS
:
670 case ACTION_STD_CANCEL
:
672 if (viewer
.moving_track
>= 0)
674 viewer
.selected_track
= viewer
.moving_track
;
675 gui_synclist_select_item(&playlist_lists
, viewer
.moving_track
);
676 viewer
.moving_track
= -1;
677 viewer
.moving_playlist_index
= -1;
678 gui_synclist_draw(&playlist_lists
);
686 struct playlist_entry
* current_track
=
687 playlist_buffer_get_track(&viewer
.buffer
,
688 viewer
.selected_track
);
689 if (viewer
.moving_track
>= 0)
694 ret_val
= playlist_move(viewer
.playlist
,
695 viewer
.moving_playlist_index
,
696 current_track
->index
);
698 splashf(HZ
, (unsigned char *)"%s %s", str(LANG_MOVE
),
700 update_playlist(true);
701 viewer
.moving_track
= -1;
702 viewer
.moving_playlist_index
= -1;
704 else if (!viewer
.playlist
)
707 playlist_start(current_track
->index
, 0);
708 update_playlist(false);
713 if (playlist_set_current(viewer
.playlist
) < 0)
716 playlist_start(current_track
->index
, 0);
718 /* Our playlist is now the current list */
719 if (!playlist_viewer_init(&viewer
, NULL
, true))
722 gui_synclist_draw(&playlist_lists
);
726 case ACTION_STD_CONTEXT
:
731 ret_val
= onplay_menu(viewer
.selected_track
);
738 else if (ret_val
> 0)
740 /* Playlist changed */
741 gui_synclist_del_item(&playlist_lists
);
742 update_playlist(true);
743 if (viewer
.num_tracks
<= 0)
745 if(viewer
.selected_track
>= viewer
.num_tracks
)
746 viewer
.selected_track
= viewer
.num_tracks
-1;
748 gui_synclist_draw(&playlist_lists
);
751 case ACTION_STD_MENU
:
757 gui_synclist_set_icon_callback(
759 global_settings
.playlist_viewer_icons
?
760 &playlist_callback_icons
:NULL
762 gui_synclist_draw(&playlist_lists
);
766 if(default_event_handler(button
) == SYS_USB_CONNECTED
)
777 playlist_close(viewer
.playlist
);
781 static char *playlist_search_callback_name(int selected_item
, void * data
,
782 char *buffer
, size_t buffer_len
)
784 (void)buffer_len
; /* this should probably be used */
785 int *found_indicies
= (int*)data
;
786 static struct playlist_track_info track
;
787 playlist_get_track_info(viewer
.playlist
, found_indicies
[selected_item
], &track
);
788 format_name(buffer
, track
.filename
);
792 bool search_playlist(void)
794 char search_str
[32] = "";
795 bool ret
= false, exit
= false;
796 int i
, playlist_count
;
797 int found_indicies
[MAX_PLAYLIST_ENTRIES
];
798 int found_indicies_count
= 0, last_found_count
= -1;
800 struct gui_synclist playlist_lists
;
801 struct playlist_track_info track
;
803 if (!playlist_viewer_init(&viewer
, 0, false))
805 if (kbd_input(search_str
, sizeof(search_str
)) == -1)
808 playlist_count
= playlist_amount_ex(viewer
.playlist
);
812 for (i
=0; (i
<playlist_count
)&&(found_indicies_count
<MAX_PLAYLIST_ENTRIES
); i
++)
814 if (found_indicies_count
!= last_found_count
)
816 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG
), found_indicies_count
,
817 str(LANG_OFF_ABORT
));
818 last_found_count
= found_indicies_count
;
821 if (action_userabort(TIMEOUT_NOBLOCK
))
824 playlist_get_track_info(viewer
.playlist
, i
, &track
);
826 if (strcasestr(track
.filename
,search_str
))
827 found_indicies
[found_indicies_count
++] = track
.index
;
834 if (!found_indicies_count
)
840 gui_synclist_init(&playlist_lists
, playlist_search_callback_name
,
841 found_indicies
, false, 1, NULL
);
842 gui_synclist_set_title(&playlist_lists
, str(LANG_SEARCH_RESULTS
), NOICON
);
843 gui_synclist_set_icon_callback(&playlist_lists
, 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
, HZ
/4);
850 if (gui_synclist_do_button(&playlist_lists
, &button
, LIST_WRAP_UNLESS_HELD
))
854 case ACTION_STD_CANCEL
:
860 found_indicies
[gui_synclist_get_sel_pos(&playlist_lists
)]
866 if(default_event_handler(button
) == SYS_USB_CONNECTED
)