Simulate the effects of sector caching a bit. Bypass I/O yield if a byte counter...
[Rockbox.git] / apps / playlist_viewer.c
blobb00e1eba3d613190cf7262c5defab8ff9dfdff49
1 /***************************************************************************
3 * __________ __ ___.
4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * \/ \/ \/ \/ \/
9 * $Id$
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
24 #include <string.h>
25 #include <sprintf.h>
26 #include "playlist.h"
27 #include "audio.h"
28 #include "screens.h"
29 #include "status.h"
30 #include "settings.h"
31 #include "icons.h"
32 #include "menu.h"
33 #include "plugin.h"
34 #include "keyboard.h"
35 #include "filetypes.h"
36 #include "onplay.h"
37 #include "talk.h"
38 #include "misc.h"
39 #include "action.h"
40 #include "debug.h"
41 #include "backlight.h"
43 #include "lang.h"
45 #include "playlist_viewer.h"
46 #include "playlist_catalog.h"
47 #include "icon.h"
48 #include "list.h"
49 #include "statusbar.h"
50 #include "splash.h"
51 #include "playlist_menu.h"
52 #include "action.h"
54 /* Maximum number of tracks we can have loaded at one time */
55 #define MAX_PLAYLIST_ENTRIES 200
57 /* The number of items between the selected one and the end/start of
58 * the buffer under which the buffer must reload */
59 #define MIN_BUFFER_MARGIN (screens[0].nb_lines+1)
61 /* Information about a specific track */
62 struct playlist_entry {
63 char *name; /* Formatted track name */
64 int index; /* Playlist index */
65 int display_index; /* Display index */
66 bool queued; /* Is track queued? */
67 bool skipped; /* Is track marked as bad? */
70 enum direction
72 FORWARD,
73 BACKWARD
76 struct playlist_buffer
78 char *name_buffer; /* Buffer used to store track names */
79 int buffer_size; /* Size of name buffer */
81 int first_index; /* Real index of first track loaded inside
82 the buffer */
84 enum direction direction; /* Direction of the buffer (if the buffer
85 was loaded BACKWARD, the last track in
86 the buffer has a real index < to the
87 real index of the the first track)*/
89 struct playlist_entry tracks[MAX_PLAYLIST_ENTRIES];
90 int num_loaded; /* Number of track entries loaded in buffer */
93 /* Global playlist viewer settings */
94 struct playlist_viewer {
95 struct playlist_info* playlist; /* playlist being viewed */
96 int num_tracks; /* Number of tracks in playlist */
97 int current_playing_track; /* Index of current playing track */
98 int selected_track; /* The selected track, relative (first is 0)*/
99 int move_track; /* Playlist index of track to move or -1 */
100 struct playlist_buffer buffer;
103 static struct playlist_viewer viewer;
105 /* Used when viewing playlists on disk */
106 static struct playlist_info temp_playlist;
108 static void playlist_buffer_init(struct playlist_buffer *pb, char *names_buffer,
109 int names_buffer_size);
110 static void playlist_buffer_load_entries(struct playlist_buffer * pb, int index,
111 enum direction direction);
112 static int playlist_entry_load(struct playlist_entry *entry, int index,
113 char* name_buffer, int remaining_size);
115 static struct playlist_entry * playlist_buffer_get_track(struct playlist_buffer *pb,
116 int index);
118 static bool playlist_viewer_init(struct playlist_viewer * viewer,
119 char* filename, bool reload);
121 static void format_name(char* dest, const char* src);
122 static void format_line(const struct playlist_entry* track, char* str,
123 int len);
125 static bool update_playlist(bool force);
126 static int onplay_menu(int index);
127 static bool viewer_menu(void);
128 static int save_playlist_func(void);
130 static void playlist_buffer_init(struct playlist_buffer *pb, char *names_buffer,
131 int names_buffer_size)
133 pb->name_buffer=names_buffer;
134 pb->buffer_size=names_buffer_size;
135 pb->first_index=0;
136 pb->num_loaded=0;
140 * Loads the entries following 'index' in the playlist buffer
142 static void playlist_buffer_load_entries(struct playlist_buffer *pb, int index,
143 enum direction direction)
145 int num_entries = viewer.num_tracks;
146 char* p = pb->name_buffer;
147 int remaining = pb->buffer_size;
148 int i;
150 pb->first_index = index;
151 if (num_entries > MAX_PLAYLIST_ENTRIES)
152 num_entries = MAX_PLAYLIST_ENTRIES;
154 for(i=0; i<num_entries; i++)
156 int len = playlist_entry_load(&(pb->tracks[i]), index, p, remaining);
157 if (len < 0)
159 /* Out of name buffer space */
160 num_entries = i;
161 break;
164 p += len;
165 remaining -= len;
167 if(direction==FORWARD)
168 index++;
169 else
170 index--;
171 index+=viewer.num_tracks;
172 index%=viewer.num_tracks;
174 pb->direction=direction;
175 pb->num_loaded = i;
178 static void playlist_buffer_load_entries_screen(struct playlist_buffer * pb,
179 enum direction direction)
181 if(direction==FORWARD)
183 int min_start=viewer.selected_track-2*screens[0].nb_lines;
184 while(min_start<0)
185 min_start+=viewer.num_tracks;
186 min_start %= viewer.num_tracks;
187 playlist_buffer_load_entries(pb, min_start, FORWARD);
189 else
191 int max_start=viewer.selected_track+2*screens[0].nb_lines;
192 max_start%=viewer.num_tracks;
193 playlist_buffer_load_entries(pb, max_start, BACKWARD);
197 static int playlist_entry_load(struct playlist_entry *entry, int index,
198 char* name_buffer, int remaining_size)
200 struct playlist_track_info info;
201 int len;
203 /* Playlist viewer orders songs based on display index. We need to
204 convert to real playlist index to access track */
205 index = (index + playlist_get_first_index(viewer.playlist)) %
206 viewer.num_tracks;
207 if (playlist_get_track_info(viewer.playlist, index, &info) < 0)
208 return -1;
210 len = strlen(info.filename) + 1;
212 if (len <= remaining_size)
214 strcpy(name_buffer, info.filename);
216 entry->name = name_buffer;
217 entry->index = info.index;
218 entry->display_index = info.display_index;
219 entry->queued = info.attr & PLAYLIST_ATTR_QUEUED;
220 entry->skipped = info.attr & PLAYLIST_ATTR_SKIPPED;
221 return len;
223 return -1;
226 static int playlist_buffer_get_index(struct playlist_buffer *pb, int index )
228 int buffer_index;
229 if(pb->direction==FORWARD)
231 if(index>=pb->first_index)
232 buffer_index=index-pb->first_index;
233 else /* rotation : track0 in buffer + requested track */
234 buffer_index=(viewer.num_tracks-pb->first_index)+(index);
236 else
238 if(index<=pb->first_index)
239 buffer_index=pb->first_index-index;
240 else /* rotation : track0 in buffer + dist from the last track
241 to the requested track (num_tracks-requested track) */
242 buffer_index=(pb->first_index)+(viewer.num_tracks-index);
244 return(buffer_index);
247 #define distance(a, b) \
248 a>b? (a) - (b) : (b) - (a)
249 static bool playlist_buffer_needs_reload(struct playlist_buffer* pb, int track_index)
251 if(pb->num_loaded==viewer.num_tracks)
252 return(false);
253 int selected_index=playlist_buffer_get_index(pb, track_index);
254 int first_buffer_index=playlist_buffer_get_index(pb, pb->first_index);
255 int distance_beginning=distance(selected_index, first_buffer_index);
256 if(distance_beginning<MIN_BUFFER_MARGIN)
257 return(true);
259 if(pb->num_loaded - distance_beginning < MIN_BUFFER_MARGIN)
260 return(true);
261 return(false);
264 static struct playlist_entry * playlist_buffer_get_track(struct playlist_buffer *pb,
265 int index)
267 int buffer_index=playlist_buffer_get_index(pb, index);
268 return(&(pb->tracks[buffer_index]));
271 /* Initialize the playlist viewer. */
272 static bool playlist_viewer_init(struct playlist_viewer * viewer,
273 char* filename, bool reload)
275 char* buffer;
276 size_t buffer_size;
277 bool is_playing = audio_status() & (AUDIO_STATUS_PLAY | AUDIO_STATUS_PAUSE);
278 bool have_list = filename || is_playing;
279 if (!have_list && (global_status.resume_index != -1))
281 /* Try to restore the list from control file */
282 have_list = (playlist_resume() != -1);
284 if (!have_list && (playlist_amount() > 0))
286 /*If dynamic playlist still exists, view it anyway even
287 if playback has reached the end of the playlist */
288 have_list = true;
290 if (!have_list)
292 /* Nothing to view, exit */
293 gui_syncsplash(HZ, str(LANG_CATALOG_NO_PLAYLISTS));
294 return false;
297 buffer = plugin_get_buffer(&buffer_size);
298 if (!buffer)
299 return false;
301 if (!filename)
302 viewer->playlist = NULL;
303 else
305 /* Viewing playlist on disk */
306 char *dir, *file, *temp_ptr;
307 char *index_buffer = NULL;
308 ssize_t index_buffer_size = 0;
310 viewer->playlist = &temp_playlist;
312 /* Separate directory from filename */
313 temp_ptr = strrchr(filename+1,'/');
314 if (temp_ptr)
316 *temp_ptr = 0;
317 dir = filename;
318 file = temp_ptr + 1;
320 else
322 dir = "/";
323 file = filename+1;
326 if (is_playing)
328 /* Something is playing, use half the plugin buffer for playlist
329 indices */
330 index_buffer_size = buffer_size / 2;
331 index_buffer = buffer;
334 playlist_create_ex(viewer->playlist, dir, file, index_buffer,
335 index_buffer_size, buffer+index_buffer_size,
336 buffer_size-index_buffer_size);
338 if (temp_ptr)
339 *temp_ptr = '/';
341 buffer += index_buffer_size;
342 buffer_size -= index_buffer_size;
344 playlist_buffer_init(&viewer->buffer, buffer, buffer_size );
346 viewer->move_track = -1;
348 if (!reload)
350 if (viewer->playlist)
351 viewer->selected_track = 0;
352 else
353 viewer->selected_track = playlist_get_display_index() - 1;
356 if (!update_playlist(true))
357 return false;
358 return true;
361 /* Format trackname for display purposes */
362 static void format_name(char* dest, const char* src)
364 switch (global_settings.playlist_viewer_track_display)
366 case 0:
367 default:
369 /* Only display the filename */
370 char* p = strrchr(src, '/');
372 strcpy(dest, p+1);
374 /* Remove the extension */
375 strrsplt(dest, '.');
377 break;
379 case 1:
380 /* Full path */
381 strcpy(dest, src);
382 break;
386 /* Format display line */
387 static void format_line(const struct playlist_entry* track, char* str,
388 int len)
390 char name[MAX_PATH];
391 char *skipped = "";
393 format_name(name, track->name);
395 if (track->skipped)
396 skipped = "(ERR) ";
398 if (global_settings.playlist_viewer_indices)
399 /* Display playlist index */
400 snprintf(str, len, "%d. %s%s", track->display_index, skipped, name);
401 else
402 snprintf(str, len, "%s%s", skipped, name);
406 /* Update playlist in case something has changed or forced */
407 static bool update_playlist(bool force)
409 if (!viewer.playlist)
410 playlist_get_resume_info(&viewer.current_playing_track);
411 else
412 viewer.current_playing_track = -1;
413 int nb_tracks=playlist_amount_ex(viewer.playlist);
414 force=force || nb_tracks != viewer.num_tracks;
415 if (force)
417 /* Reload tracks */
418 viewer.num_tracks = nb_tracks;
419 if (viewer.num_tracks <= 0)
420 return false;
421 playlist_buffer_load_entries_screen(&viewer.buffer, FORWARD);
422 if (viewer.buffer.num_loaded <= 0)
423 return false;
425 return true;
428 /* Menu of playlist commands. Invoked via ON+PLAY on main viewer screen.
429 Returns -1 if USB attached, 0 if no playlist change, and 1 if playlist
430 changed. */
431 static int onplay_menu(int index)
433 int result, ret = 0;
434 struct playlist_entry * current_track=
435 playlist_buffer_get_track(&viewer.buffer, index);
436 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_PLAYLIST), NULL,
437 ID2P(LANG_REMOVE), ID2P(LANG_MOVE),
438 ID2P(LANG_CATALOG_ADD_TO), ID2P(LANG_CATALOG_ADD_TO_NEW));
439 bool current = (current_track->index == viewer.current_playing_track);
441 result = do_menu(&menu_items, NULL);
442 if (result == MENU_ATTACHED_USB)
444 ret = -1;
446 else if (result >= 0)
448 /* Abort current move */
449 viewer.move_track = -1;
451 switch (result)
453 case 0:
454 /* delete track */
455 playlist_delete(viewer.playlist, current_track->index);
456 if (current)
458 if (playlist_amount_ex(viewer.playlist) <= 0)
459 audio_stop();
460 else
462 /* Start playing new track except if it's the lasttrack
463 track in the playlist and repeat mode is disabled */
464 current_track =
465 playlist_buffer_get_track(&viewer.buffer, index);
466 if (current_track->display_index!=viewer.num_tracks ||
467 global_settings.repeat_mode == REPEAT_ALL)
469 #if CONFIG_CODEC != SWCODEC
470 talk_buffer_steal(); /* will use the mp3 buffer */
471 #endif
472 audio_play(0);
473 viewer.current_playing_track = -1;
477 ret = 1;
478 break;
479 case 1:
480 /* move track */
481 viewer.move_track = current_track->index;
482 ret = 0;
483 break;
484 case 2: /* add to catalog */
485 case 3: /* add to a new one */
486 catalog_add_to_a_playlist(current_track->name,
487 FILE_ATTR_AUDIO,
488 result==3 );
489 ret = 0;
490 break;
493 return ret;
496 /* Menu of viewer options. Invoked via F1(r) or Menu(p). */
497 MENUITEM_SETTING(show_icons, &global_settings.playlist_viewer_icons, NULL);
498 MENUITEM_SETTING(show_indices, &global_settings.playlist_viewer_indices, NULL);
499 MENUITEM_SETTING(track_display,
500 &global_settings.playlist_viewer_track_display, NULL);
501 MENUITEM_FUNCTION(save_playlist_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
502 save_playlist_func, 0, NULL, Icon_NOICON);
503 MAKE_MENU(viewer_settings_menu, ID2P(LANG_PLAYLISTVIEWER_SETTINGS),
504 NULL, Icon_Playlist,
505 &show_icons, &show_indices, &track_display, &save_playlist_item);
506 static bool viewer_menu(void)
508 return do_menu(&viewer_settings_menu, NULL) == MENU_ATTACHED_USB;
511 /* Save playlist to disk */
512 static int save_playlist_func(void)
514 save_playlist_screen(viewer.playlist);
515 return 0;
518 /* View current playlist */
519 bool playlist_viewer(void)
521 return playlist_viewer_ex(NULL);
524 static int get_track_num( struct playlist_viewer * local_viewer,
525 int selected_item )
527 if( local_viewer->move_track >= 0 )
529 if( local_viewer->selected_track == selected_item )
531 return local_viewer->move_track;
533 else if( local_viewer->selected_track > selected_item
534 && selected_item >= local_viewer->move_track )
536 return selected_item+1;
538 else if( local_viewer->selected_track < selected_item
539 && selected_item <= local_viewer->move_track )
541 return selected_item-1;
544 return selected_item;
547 static char *playlist_callback_name(int selected_item, void *data, char *buffer)
549 struct playlist_viewer * local_viewer = (struct playlist_viewer *)data;
550 struct playlist_entry *track = playlist_buffer_get_track(&(local_viewer->buffer), get_track_num(local_viewer,selected_item));
551 format_line(track, buffer, MAX_PATH);
552 return(buffer);
556 static int playlist_callback_icons(int selected_item, void *data)
558 struct playlist_viewer * local_viewer=(struct playlist_viewer *)data;
559 struct playlist_entry *track=
560 playlist_buffer_get_track(&(local_viewer->buffer),
561 get_track_num(local_viewer, selected_item));
562 if (track->index == local_viewer->current_playing_track)
564 /* Current playing track */
565 return Icon_Audio;
567 else if (track->index == local_viewer->move_track)
569 /* Track we are moving */
570 return Icon_Moving;
572 else if (track->queued)
574 /* Queued track */
575 return Icon_Queued;
577 else
578 return Icon_NOICON;
581 /* Main viewer function. Filename identifies playlist to be viewed. If NULL,
582 view current playlist. */
583 bool playlist_viewer_ex(char* filename)
585 bool ret = false; /* return value */
586 bool exit=false; /* exit viewer */
587 int button;
588 struct gui_synclist playlist_lists;
589 if (!playlist_viewer_init(&viewer, filename, false))
590 goto exit;
592 gui_synclist_init(&playlist_lists, playlist_callback_name, &viewer, false, 1);
593 gui_synclist_set_icon_callback(&playlist_lists,
594 global_settings.playlist_viewer_icons?
595 &playlist_callback_icons:NULL);
596 gui_synclist_set_nb_items(&playlist_lists, viewer.num_tracks);
597 gui_synclist_select_item(&playlist_lists, viewer.selected_track);
598 gui_synclist_set_title(&playlist_lists, str(LANG_PLAYLIST), Icon_Playlist);
599 gui_synclist_draw(&playlist_lists);
600 while (!exit)
602 int track;
604 if (global_status.resume_index != -1 && !viewer.playlist)
605 playlist_get_resume_info(&track);
606 else
607 track = -1;
609 if (track != viewer.current_playing_track ||
610 playlist_amount_ex(viewer.playlist) != viewer.num_tracks)
612 /* Playlist has changed (new track started?) */
613 if (!update_playlist(false))
614 goto exit;
615 /*Needed because update_playlist gives wrong value when
616 playing is stopped*/
617 viewer.current_playing_track = track;
618 gui_synclist_set_nb_items(&playlist_lists, viewer.num_tracks);
619 /* Abort move on playlist change */
620 viewer.move_track = -1;
621 gui_synclist_draw(&playlist_lists);
624 /* Timeout so we can determine if play status has changed */
625 button = get_action(CONTEXT_TREE,HZ/2);
626 if( (gui_synclist_do_button(&playlist_lists, &button,LIST_WRAP_UNLESS_HELD)) )
628 viewer.selected_track=gui_synclist_get_sel_pos(&playlist_lists);
629 if(playlist_buffer_needs_reload(&viewer.buffer,
630 viewer.selected_track))
631 playlist_buffer_load_entries_screen(&viewer.buffer,
632 button==ACTION_STD_NEXT?
633 FORWARD
635 BACKWARD
637 gui_synclist_draw(&playlist_lists);
639 switch (button)
641 case ACTION_TREE_WPS:
642 case ACTION_STD_CANCEL:
643 exit = true;
644 break;
645 case ACTION_STD_OK:
647 struct playlist_entry * current_track =
648 playlist_buffer_get_track(&viewer.buffer,
649 viewer.selected_track);
650 if (viewer.move_track >= 0)
652 /* Move track */
653 int ret_val;
655 ret_val = playlist_move(viewer.playlist, viewer.move_track,
656 current_track->index);
657 if (ret_val < 0)
658 gui_syncsplash(HZ, (unsigned char *)"%s %s",
659 str(LANG_MOVE), str(LANG_FAILED));
660 update_playlist(true);
661 viewer.move_track = -1;
663 else if (!viewer.playlist)
665 /* play new track */
666 playlist_start(current_track->index, 0);
667 update_playlist(false);
669 else
671 /* New playlist */
672 if (playlist_set_current(viewer.playlist) < 0)
673 goto exit;
675 playlist_start(current_track->index, 0);
677 /* Our playlist is now the current list */
678 if (!playlist_viewer_init(&viewer, NULL, true))
679 goto exit;
681 gui_synclist_draw(&playlist_lists);
683 break;
685 case ACTION_STD_CONTEXT:
687 /* ON+PLAY menu */
688 int ret_val;
690 ret_val = onplay_menu(viewer.selected_track);
692 if (ret_val < 0)
694 ret = true;
695 goto exit;
697 else if (ret > 0)
699 /* Playlist changed */
700 gui_synclist_del_item(&playlist_lists);
701 update_playlist(true);
702 if (viewer.num_tracks <= 0)
703 exit = true;
704 if(viewer.selected_track >= viewer.num_tracks)
705 viewer.selected_track = viewer.num_tracks-1;
707 gui_synclist_draw(&playlist_lists);
708 break;
710 case ACTION_STD_MENU:
711 if (viewer_menu())
713 ret = true;
714 goto exit;
716 gui_synclist_set_icon_callback(
717 &playlist_lists,
718 global_settings.playlist_viewer_icons?
719 &playlist_callback_icons:NULL
721 gui_synclist_draw(&playlist_lists);
722 break;
724 case ACTION_NONE:
725 gui_syncstatusbar_draw(&statusbars, false);
726 break;
728 default:
729 if(default_event_handler(button) == SYS_USB_CONNECTED)
731 ret = true;
732 goto exit;
734 break;
738 exit:
739 if (viewer.playlist)
740 playlist_close(viewer.playlist);
741 return ret;
744 static char *playlist_search_callback_name(int selected_item, void * data, char *buffer)
746 int *found_indicies = (int*)data;
747 static struct playlist_track_info track;
748 playlist_get_track_info(viewer.playlist,found_indicies[selected_item],&track);
749 format_name(buffer,track.filename);
750 return(buffer);
753 bool search_playlist(void)
755 char search_str[32] = "";
756 bool ret = false, exit = false;
757 int i, playlist_count;
758 int found_indicies[MAX_PLAYLIST_ENTRIES],found_indicies_count = 0;
759 int button;
760 struct gui_synclist playlist_lists;
761 struct playlist_track_info track;
763 if (!playlist_viewer_init(&viewer, 0, false))
764 return ret;
765 if (kbd_input(search_str,sizeof(search_str)) == -1)
766 return ret;
767 lcd_clear_display();
768 playlist_count = playlist_amount_ex(viewer.playlist);
769 for (i=0;(i<playlist_count)&&(found_indicies_count<MAX_PLAYLIST_ENTRIES);i++)
771 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG),found_indicies_count,
772 str(LANG_OFF_ABORT));
773 if (action_userabort(TIMEOUT_NOBLOCK))
775 if (!found_indicies_count)
776 return ret;
777 break;
779 playlist_get_track_info(viewer.playlist,i,&track);
780 if (strcasestr(track.filename,search_str))
782 found_indicies[found_indicies_count++] = track.index;
785 if (!found_indicies_count)
787 return ret;
789 backlight_on();
790 gui_synclist_init(&playlist_lists, playlist_search_callback_name,
791 found_indicies, false, 1);
792 gui_synclist_set_icon_callback(&playlist_lists, NULL);
793 gui_synclist_set_nb_items(&playlist_lists, found_indicies_count);
794 gui_synclist_select_item(&playlist_lists, 0);
795 gui_synclist_draw(&playlist_lists);
796 while (!exit)
798 button = get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
799 if (gui_synclist_do_button(&playlist_lists, &button, LIST_WRAP_UNLESS_HELD))
800 continue;
801 switch (button)
803 case ACTION_STD_CANCEL:
804 exit = true;
805 break;
807 case ACTION_STD_OK:
808 playlist_start(
809 found_indicies[gui_synclist_get_sel_pos(&playlist_lists)]
810 ,0);
811 exit = 1;
812 break;
813 case ACTION_NONE:
814 break;
815 default:
816 if(default_event_handler(button) == SYS_USB_CONNECTED)
818 ret = true;
819 exit = true;
821 break;
824 return ret;