Fix playlist catalog directory manual spelling
[maemo-rb.git] / apps / playlist_viewer.c
blobffaefebd5f77c877851e5b1d294fa316c923af25
1 /***************************************************************************
3 * __________ __ ___.
4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * \/ \/ \/ \/ \/
9 * $Id$
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
26 #include <string.h>
27 #include "playlist.h"
28 #include "audio.h"
29 #include "screens.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 "splash.h"
50 #include "playlist_menu.h"
51 #include "yesno.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? */
69 enum direction
71 FORWARD,
72 BACKWARD
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
81 the buffer */
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,
118 int index);
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,
125 int len);
127 static bool update_playlist(bool force);
128 static int onplay_menu(int index);
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;
136 pb->first_index = 0;
137 pb->num_loaded = 0;
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;
149 int i;
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);
158 if (len < 0)
160 /* Out of name buffer space */
161 num_entries = i;
162 break;
165 p += len;
166 remaining -= len;
168 if(direction == FORWARD)
169 index++;
170 else
171 index--;
172 index += viewer.num_tracks;
173 index %= viewer.num_tracks;
175 pb->direction = direction;
176 pb->num_loaded = i;
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();
185 while (min_start < 0)
186 min_start += viewer.num_tracks;
187 min_start %= viewer.num_tracks;
188 playlist_buffer_load_entries(pb, min_start, FORWARD);
190 else
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;
202 int len;
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)) %
207 viewer.num_tracks;
208 if (playlist_get_track_info(viewer.playlist, index, &info) < 0)
209 return -1;
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;
222 return len;
224 return -1;
227 static int playlist_buffer_get_index(struct playlist_buffer *pb, int index)
229 int buffer_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;
237 else
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,
251 int track_index)
253 if (pb->num_loaded == viewer.num_tracks)
254 return false;
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)
259 return true;
261 if (pb->num_loaded - distance_beginning < MIN_BUFFER_MARGIN)
262 return true;
263 return false;
266 static struct playlist_entry * playlist_buffer_get_track(struct playlist_buffer *pb,
267 int index)
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)
277 char* buffer;
278 size_t buffer_size;
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 */
290 have_list = true;
292 if (!have_list)
294 /* Nothing to view, exit */
295 splash(HZ, str(LANG_CATALOG_NO_PLAYLISTS));
296 return false;
299 buffer = plugin_get_buffer(&buffer_size);
300 if (!buffer)
301 return false;
303 if (!filename)
304 viewer->playlist = NULL;
305 else
307 /* Viewing playlist on disk */
308 const char *dir, *file;
309 char *temp_ptr;
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,'/');
317 if (temp_ptr)
319 *temp_ptr = 0;
320 dir = filename;
321 file = temp_ptr + 1;
323 else
325 dir = "/";
326 file = filename+1;
329 if (is_playing)
331 /* Something is playing, use half the plugin buffer for playlist
332 indices */
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);
341 if (temp_ptr)
342 *temp_ptr = '/';
344 buffer += index_buffer_size;
345 buffer_size -= index_buffer_size;
347 playlist_buffer_init(&viewer->buffer, buffer, buffer_size);
349 viewer->moving_track = -1;
350 viewer->moving_playlist_index = -1;
352 if (!reload)
354 if (viewer->playlist)
355 viewer->selected_track = 0;
356 else
357 viewer->selected_track = playlist_get_display_index() - 1;
360 if (!update_playlist(true))
361 return false;
362 return 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)
370 case 0:
371 default:
373 /* Only display the filename */
374 char* p = strrchr(src, '/');
376 strcpy(dest, p+1);
378 /* Remove the extension */
379 strrsplt(dest, '.');
381 break;
383 case 1:
384 /* Full path */
385 strcpy(dest, src);
386 break;
390 /* Format display line */
391 static void format_line(const struct playlist_entry* track, char* str,
392 int len)
394 char name[MAX_PATH];
395 char *skipped = "";
397 format_name(name, track->name);
399 if (track->skipped)
400 skipped = "(ERR) ";
402 if (global_settings.playlist_viewer_indices)
403 /* Display playlist index */
404 snprintf(str, len, "%d. %s%s", track->display_index, skipped, name);
405 else
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);
415 else
416 viewer.current_playing_track = -1;
417 int nb_tracks = playlist_amount_ex(viewer.playlist);
418 force = force || nb_tracks != viewer.num_tracks;
419 if (force)
421 /* Reload tracks */
422 viewer.num_tracks = nb_tracks;
423 if (viewer.num_tracks <= 0)
425 global_status.resume_index = -1;
426 global_status.resume_offset = -1;
427 return false;
429 playlist_buffer_load_entries_screen(&viewer.buffer, FORWARD);
430 if (viewer.buffer.num_loaded <= 0)
432 global_status.resume_index = -1;
433 global_status.resume_offset = -1;
434 return false;
437 return true;
440 MENUITEM_FUNCTION(save_playlist_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
441 save_playlist_func, 0, NULL, Icon_NOICON);
443 /* Menu of playlist commands. Invoked via ON+PLAY on main viewer screen.
444 Returns -1 if USB attached, 0 if no playlist change, and 1 if playlist
445 changed. */
446 static int onplay_menu(int index)
448 int result, ret = 0;
449 struct playlist_entry * current_track =
450 playlist_buffer_get_track(&viewer.buffer, index);
451 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_PLAYLIST), NULL,
452 ID2P(LANG_CURRENT_PLAYLIST), ID2P(LANG_CATALOG),
453 ID2P(LANG_REMOVE), ID2P(LANG_MOVE), ID2P(LANG_SHUFFLE),
454 ID2P(LANG_SAVE_DYNAMIC_PLAYLIST));
455 bool current = (current_track->index == viewer.current_playing_track);
457 result = do_menu(&menu_items, NULL, NULL, false);
458 if (result == MENU_ATTACHED_USB)
460 ret = -1;
462 else if (result >= 0)
464 /* Abort current move */
465 viewer.moving_track = -1;
466 viewer.moving_playlist_index = -1;
468 switch (result)
470 case 0:
471 /* playlist */
472 onplay_show_playlist_menu(current_track->name);
473 ret = 0;
474 break;
475 case 1:
476 /* add to catalog */
477 onplay_show_playlist_cat_menu(current_track->name);
478 ret = 0;
479 break;
480 case 2:
481 /* delete track */
482 playlist_delete(viewer.playlist, current_track->index);
483 if (current)
485 if (playlist_amount_ex(viewer.playlist) <= 0)
486 audio_stop();
487 else
489 /* Start playing new track except if it's the lasttrack
490 track in the playlist and repeat mode is disabled */
491 current_track =
492 playlist_buffer_get_track(&viewer.buffer, index);
493 if (current_track->display_index!=viewer.num_tracks ||
494 global_settings.repeat_mode == REPEAT_ALL)
496 audio_play(0);
497 viewer.current_playing_track = -1;
501 ret = 1;
502 break;
503 case 3:
504 /* move track */
505 viewer.moving_track = index;
506 viewer.moving_playlist_index = current_track->index;
507 ret = 0;
508 break;
509 case 4:
510 /* shuffle */
511 playlist_randomise(viewer.playlist, current_tick, false);
512 ret = 1;
513 break;
514 case 5:
515 /* save playlist */
516 save_playlist_screen(viewer.playlist);
517 ret = 0;
518 break;
521 return ret;
524 /* Save playlist to disk */
525 static int save_playlist_func(void)
527 save_playlist_screen(viewer.playlist);
528 return 0;
531 /* View current playlist */
532 enum playlist_viewer_result playlist_viewer(void)
534 return playlist_viewer_ex(NULL);
537 static int get_track_num(struct playlist_viewer *local_viewer,
538 int selected_item)
540 if (local_viewer->moving_track >= 0)
542 if (local_viewer->selected_track == selected_item)
544 return local_viewer->moving_track;
546 else if (local_viewer->selected_track > selected_item
547 && selected_item >= local_viewer->moving_track)
549 return selected_item+1; /* move down */
551 else if (local_viewer->selected_track < selected_item
552 && selected_item <= local_viewer->moving_track)
554 return selected_item-1; /* move up */
557 return selected_item;
560 static const char* playlist_callback_name(int selected_item,
561 void *data,
562 char *buffer,
563 size_t buffer_len)
565 struct playlist_viewer *local_viewer = (struct playlist_viewer *)data;
567 int track_num = get_track_num(local_viewer, selected_item);
568 struct playlist_entry *track =
569 playlist_buffer_get_track(&(local_viewer->buffer), track_num);
571 format_line(track, buffer, buffer_len);
573 return(buffer);
577 static enum themable_icons playlist_callback_icons(int selected_item,
578 void *data)
580 struct playlist_viewer *local_viewer = (struct playlist_viewer *)data;
582 int track_num = get_track_num(local_viewer, selected_item);
583 struct playlist_entry *track =
584 playlist_buffer_get_track(&(local_viewer->buffer), track_num);
586 if (track->index == local_viewer->current_playing_track)
588 /* Current playing track */
589 return Icon_Audio;
591 else if (track->index == local_viewer->moving_playlist_index)
593 /* Track we are moving */
594 return Icon_Moving;
596 else if (track->queued)
598 /* Queued track */
599 return Icon_Queued;
601 else
602 return Icon_NOICON;
605 static int playlist_callback_voice(int selected_item, void *data)
607 struct playlist_viewer *local_viewer = (struct playlist_viewer *)data;
609 int track_num = get_track_num(local_viewer, selected_item);
610 struct playlist_entry *track =
611 playlist_buffer_get_track(&(local_viewer->buffer), track_num);
613 bool enqueue = false;
615 if (global_settings.talk_file_clip || global_settings.talk_file == 2)
617 if (global_settings.playlist_viewer_indices)
619 talk_number(track->display_index, false);
620 enqueue = true;
622 talk_file_or_spell(NULL, track->name, NULL, enqueue);
624 else if (global_settings.talk_file == 1) /* as numbers */
626 talk_id(VOICE_FILE, false);
627 talk_number(track->display_index, true);
630 return 0;
633 /* Main viewer function. Filename identifies playlist to be viewed. If NULL,
634 view current playlist. */
635 enum playlist_viewer_result playlist_viewer_ex(const char* filename)
637 enum playlist_viewer_result ret = PLAYLIST_VIEWER_OK;
638 bool exit = false; /* exit viewer */
639 int button;
640 bool dirty = false;
641 struct gui_synclist playlist_lists;
642 if (!playlist_viewer_init(&viewer, filename, false))
643 goto exit;
645 push_current_activity(ACTIVITY_PLAYLISTVIEWER);
646 gui_synclist_init(&playlist_lists, playlist_callback_name,
647 &viewer, false, 1, NULL);
648 gui_synclist_set_voice_callback(&playlist_lists, playlist_callback_voice);
649 gui_synclist_set_icon_callback(&playlist_lists,
650 global_settings.playlist_viewer_icons?
651 &playlist_callback_icons:NULL);
652 gui_synclist_set_nb_items(&playlist_lists, viewer.num_tracks);
653 gui_synclist_set_title(&playlist_lists, str(LANG_PLAYLIST), Icon_Playlist);
654 gui_synclist_select_item(&playlist_lists, viewer.selected_track);
655 gui_synclist_draw(&playlist_lists);
656 gui_synclist_speak_item(&playlist_lists);
657 while (!exit)
659 int track;
661 if (global_status.resume_index != -1 && !viewer.playlist)
662 playlist_get_resume_info(&track);
663 else
664 track = -1;
666 if (track != viewer.current_playing_track ||
667 playlist_amount_ex(viewer.playlist) != viewer.num_tracks)
669 /* Playlist has changed (new track started?) */
670 if (!update_playlist(false))
671 goto exit;
672 /*Needed because update_playlist gives wrong value when
673 playing is stopped*/
674 viewer.current_playing_track = track;
675 gui_synclist_set_nb_items(&playlist_lists, viewer.num_tracks);
677 gui_synclist_draw(&playlist_lists);
680 /* Timeout so we can determine if play status has changed */
681 bool res = list_do_action(CONTEXT_TREE, HZ/2,
682 &playlist_lists, &button, LIST_WRAP_UNLESS_HELD);
683 /* during moving, another redraw is going to be needed,
684 * since viewer.selected_track is updated too late (after the first draw)
685 * drawing the moving item needs it */
686 viewer.selected_track=gui_synclist_get_sel_pos(&playlist_lists);
687 if (res)
689 bool reload = playlist_buffer_needs_reload(&viewer.buffer,
690 viewer.selected_track);
691 if (reload)
692 playlist_buffer_load_entries_screen(&viewer.buffer,
693 button == ACTION_STD_NEXT ? FORWARD : BACKWARD);
694 if (reload || viewer.moving_track >= 0)
695 gui_synclist_draw(&playlist_lists);
697 switch (button)
699 case ACTION_TREE_WPS:
700 case ACTION_STD_CANCEL:
702 if (viewer.moving_track >= 0)
704 viewer.selected_track = viewer.moving_track;
705 gui_synclist_select_item(&playlist_lists, viewer.moving_track);
706 viewer.moving_track = -1;
707 viewer.moving_playlist_index = -1;
708 gui_synclist_draw(&playlist_lists);
710 else
712 exit = true;
713 ret = PLAYLIST_VIEWER_CANCEL;
715 break;
717 case ACTION_STD_OK:
719 struct playlist_entry * current_track =
720 playlist_buffer_get_track(&viewer.buffer,
721 viewer.selected_track);
723 if (viewer.moving_track >= 0)
725 /* Move track */
726 int ret_val;
728 ret_val = playlist_move(viewer.playlist,
729 viewer.moving_playlist_index,
730 current_track->index);
731 if (ret_val < 0)
732 splashf(HZ, (unsigned char *)"%s %s", str(LANG_MOVE),
733 str(LANG_FAILED));
734 update_playlist(true);
735 viewer.moving_track = -1;
736 viewer.moving_playlist_index = -1;
737 dirty = true;
739 else if (!viewer.playlist)
741 /* play new track */
742 if (!global_settings.party_mode)
744 playlist_start(current_track->index, 0);
745 update_playlist(false);
748 else if (!global_settings.party_mode)
750 int start_index = current_track->index;
751 if (!warn_on_pl_erase())
753 gui_synclist_draw(&playlist_lists);
754 break;
756 /* New playlist */
757 if (playlist_set_current(viewer.playlist) < 0)
758 goto exit;
759 if (global_settings.playlist_shuffle)
760 start_index = playlist_shuffle(current_tick, start_index);
761 playlist_start(start_index, 0);
763 /* Our playlist is now the current list */
764 if (!playlist_viewer_init(&viewer, NULL, true))
765 goto exit;
766 exit = true;
768 gui_synclist_draw(&playlist_lists);
770 break;
772 case ACTION_STD_CONTEXT:
774 /* ON+PLAY menu */
775 int ret_val;
777 ret_val = onplay_menu(viewer.selected_track);
779 if (ret_val < 0)
781 ret = PLAYLIST_VIEWER_USB;
782 goto exit;
784 else if (ret_val > 0)
786 /* Playlist changed */
787 gui_synclist_del_item(&playlist_lists);
788 update_playlist(true);
789 if (viewer.num_tracks <= 0)
790 exit = true;
791 if (viewer.selected_track >= viewer.num_tracks)
792 viewer.selected_track = viewer.num_tracks-1;
793 dirty = true;
795 gui_synclist_draw(&playlist_lists);
796 break;
798 case ACTION_STD_MENU:
799 ret = PLAYLIST_VIEWER_MAINMENU;
800 goto exit;
801 default:
802 if(default_event_handler(button) == SYS_USB_CONNECTED)
804 ret = PLAYLIST_VIEWER_USB;
805 goto exit;
807 break;
811 exit:
812 pop_current_activity();
813 if (viewer.playlist)
815 if(dirty && yesno_pop(ID2P(LANG_SAVE_CHANGES)))
816 save_playlist_screen(viewer.playlist);
817 playlist_close(viewer.playlist);
819 return ret;
822 static const char* playlist_search_callback_name(int selected_item, void * data,
823 char *buffer, size_t buffer_len)
825 (void)buffer_len; /* this should probably be used */
826 int *found_indicies = (int*)data;
827 static struct playlist_track_info track;
828 playlist_get_track_info(viewer.playlist, found_indicies[selected_item], &track);
829 format_name(buffer, track.filename);
830 return buffer;
833 bool search_playlist(void)
835 char search_str[32] = "";
836 bool ret = false, exit = false;
837 int i, playlist_count;
838 int found_indicies[MAX_PLAYLIST_ENTRIES];
839 int found_indicies_count = 0, last_found_count = -1;
840 int button;
841 struct gui_synclist playlist_lists;
842 struct playlist_track_info track;
844 if (!playlist_viewer_init(&viewer, 0, false))
845 return ret;
846 if (kbd_input(search_str, sizeof(search_str)) < 0)
847 return ret;
848 lcd_clear_display();
849 playlist_count = playlist_amount_ex(viewer.playlist);
851 cpu_boost(true);
853 for (i = 0; i < playlist_count &&
854 found_indicies_count < MAX_PLAYLIST_ENTRIES; i++)
856 if (found_indicies_count != last_found_count)
858 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG), found_indicies_count,
859 str(LANG_OFF_ABORT));
860 last_found_count = found_indicies_count;
863 if (action_userabort(TIMEOUT_NOBLOCK))
864 break;
866 playlist_get_track_info(viewer.playlist, i, &track);
868 if (strcasestr(track.filename,search_str))
869 found_indicies[found_indicies_count++] = track.index;
871 yield();
874 cpu_boost(false);
876 if (!found_indicies_count)
878 return ret;
880 backlight_on();
882 gui_synclist_init(&playlist_lists, playlist_search_callback_name,
883 found_indicies, false, 1, NULL);
884 gui_synclist_set_title(&playlist_lists, str(LANG_SEARCH_RESULTS), NOICON);
885 gui_synclist_set_icon_callback(&playlist_lists, NULL);
886 gui_synclist_set_nb_items(&playlist_lists, found_indicies_count);
887 gui_synclist_select_item(&playlist_lists, 0);
888 gui_synclist_draw(&playlist_lists);
889 while (!exit)
891 if (list_do_action(CONTEXT_LIST, HZ/4,
892 &playlist_lists, &button, LIST_WRAP_UNLESS_HELD))
893 continue;
894 switch (button)
896 case ACTION_STD_CANCEL:
897 exit = true;
898 break;
900 case ACTION_STD_OK:
902 int sel = gui_synclist_get_sel_pos(&playlist_lists);
903 playlist_start(found_indicies[sel], 0);
904 exit = 1;
906 break;
908 default:
909 if (default_event_handler(button) == SYS_USB_CONNECTED)
911 ret = true;
912 exit = true;
914 break;
917 return ret;