Add platform file for Iaudio M5.
[Rockbox.git] / apps / tagtree.c
blob59be2a857f97fae31eb17b1610cf30916ee21059
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 /**
21 * Basic structure on this file was copied from dbtree.c and modified to
22 * support the tag cache interface.
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include "system.h"
28 #include "kernel.h"
29 #include "splash.h"
30 #include "icons.h"
31 #include "tree.h"
32 #include "action.h"
33 #include "settings.h"
34 #include "tagcache.h"
35 #include "tagtree.h"
36 #include "lang.h"
37 #include "logf.h"
38 #include "playlist.h"
39 #include "keyboard.h"
40 #include "gui/list.h"
41 #include "buffer.h"
42 #include "atoi.h"
43 #include "playback.h"
44 #include "yesno.h"
45 #include "misc.h"
47 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
49 static int tagtree_play_folder(struct tree_context* c);
51 static char searchstring[128];
53 enum variables {
54 var_sorttype = 100,
55 var_limit,
56 var_strip,
57 var_menu_start,
58 var_include,
59 var_rootmenu,
60 var_format,
61 menu_next,
62 menu_load,
65 /* Capacity 10 000 entries (for example 10k different artists) */
66 #define UNIQBUF_SIZE (64*1024)
67 static long *uniqbuf;
69 #define MAX_TAGS 5
71 static struct tagcache_search tcs, tcs2;
72 static bool sort_inverse;
75 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
77 * valid = true
78 * formatstr = "%-3d. %s"
79 * tags[0] = tag_autoscore
80 * tags[1] = tag_title
81 * tag_count = 2
83 * limit = 100
84 * sort_inverse = true
86 struct display_format {
87 char name[32];
88 struct tagcache_search_clause *clause[TAGCACHE_MAX_CLAUSES];
89 int clause_count;
90 char *formatstr;
91 int group_id;
92 int tags[MAX_TAGS];
93 int tag_count;
95 int limit;
96 int strip;
97 bool sort_inverse;
98 bool sort;
101 static struct display_format *formats[TAGMENU_MAX_FMTS];
102 static int format_count;
104 struct search_instruction {
105 char name[64];
106 int tagorder[MAX_TAGS];
107 int tagorder_count;
108 struct tagcache_search_clause *clause[MAX_TAGS][TAGCACHE_MAX_CLAUSES];
109 int format_id[MAX_TAGS];
110 int clause_count[MAX_TAGS];
111 int result_seek[MAX_TAGS];
114 struct menu_entry {
115 char name[64];
116 int type;
117 struct search_instruction *si;
118 int link;
121 struct root_menu {
122 char title[64];
123 char id[32];
124 int itemcount;
125 struct root_menu *parent;
126 struct menu_entry *items[TAGMENU_MAX_ITEMS];
129 /* Statusbar text of the current view. */
130 static char current_title[MAX_TAGS][128];
132 static struct root_menu *menus[TAGMENU_MAX_MENUS];
133 static struct root_menu *menu;
134 static struct search_instruction *csi;
135 static const char *strp;
136 static int menu_count;
137 static int root_menu;
139 static int current_offset;
140 static int current_entry_count;
142 static int format_count;
143 static struct tree_context *tc;
145 static int get_token_str(char *buf, int size)
147 /* Find the start. */
148 while (*strp != '"' && *strp != '\0')
149 strp++;
151 if (*strp == '\0' || *(++strp) == '\0')
152 return -1;
154 /* Read the data. */
155 while (*strp != '"' && *strp != '\0' && --size > 0)
156 *(buf++) = *(strp++);
158 *buf = '\0';
159 if (*strp != '"')
160 return -2;
162 strp++;
164 return 0;
167 #define MATCH(tag,str1,str2,settag) \
168 if (!strcasecmp(str1, str2)) { \
169 *tag = settag; \
170 return 1; \
173 static int get_tag(int *tag)
175 char buf[128];
176 int i;
178 /* Find the start. */
179 while ((*strp == ' ' || *strp == '>') && *strp != '\0')
180 strp++;
182 if (*strp == '\0' || *strp == '?')
183 return 0;
185 for (i = 0; i < (int)sizeof(buf)-1; i++)
187 if (*strp == '\0' || *strp == ' ')
188 break ;
189 buf[i] = *strp;
190 strp++;
192 buf[i] = '\0';
194 MATCH(tag, buf, "album", tag_album);
195 MATCH(tag, buf, "artist", tag_artist);
196 MATCH(tag, buf, "bitrate", tag_bitrate);
197 MATCH(tag, buf, "composer", tag_composer);
198 MATCH(tag, buf, "comment", tag_comment);
199 MATCH(tag, buf, "albumartist", tag_albumartist);
200 MATCH(tag, buf, "ensemble", tag_albumartist);
201 MATCH(tag, buf, "genre", tag_genre);
202 MATCH(tag, buf, "length", tag_length);
203 MATCH(tag, buf, "title", tag_title);
204 MATCH(tag, buf, "filename", tag_filename);
205 MATCH(tag, buf, "tracknum", tag_tracknumber);
206 MATCH(tag, buf, "year", tag_year);
207 MATCH(tag, buf, "playcount", tag_playcount);
208 MATCH(tag, buf, "lastplayed", tag_lastplayed);
209 MATCH(tag, buf, "commitid", tag_commitid);
210 MATCH(tag, buf, "entryage", tag_virt_entryage);
211 MATCH(tag, buf, "autoscore", tag_virt_autoscore);
212 MATCH(tag, buf, "%sort", var_sorttype);
213 MATCH(tag, buf, "%limit", var_limit);
214 MATCH(tag, buf, "%strip", var_strip);
215 MATCH(tag, buf, "%menu_start", var_menu_start);
216 MATCH(tag, buf, "%include", var_include);
217 MATCH(tag, buf, "%root_menu", var_rootmenu);
218 MATCH(tag, buf, "%format", var_format);
219 MATCH(tag, buf, "->", menu_next);
220 MATCH(tag, buf, "==>", menu_load);
222 logf("NO MATCH: %s\n", buf);
223 if (buf[0] == '?')
224 return 0;
226 return -1;
229 static int get_clause(int *condition)
231 char buf[4];
232 int i;
234 /* Find the start. */
235 while (*strp == ' ' && *strp != '\0')
236 strp++;
238 if (*strp == '\0')
239 return 0;
241 for (i = 0; i < (int)sizeof(buf)-1; i++)
243 if (*strp == '\0' || *strp == ' ')
244 break ;
245 buf[i] = *strp;
246 strp++;
248 buf[i] = '\0';
250 MATCH(condition, buf, "=", clause_is);
251 MATCH(condition, buf, "==", clause_is);
252 MATCH(condition, buf, "!=", clause_is_not);
253 MATCH(condition, buf, ">", clause_gt);
254 MATCH(condition, buf, ">=", clause_gteq);
255 MATCH(condition, buf, "<", clause_lt);
256 MATCH(condition, buf, "<=", clause_lteq);
257 MATCH(condition, buf, "~", clause_contains);
258 MATCH(condition, buf, "!~", clause_not_contains);
259 MATCH(condition, buf, "^", clause_begins_with);
260 MATCH(condition, buf, "!^", clause_not_begins_with);
261 MATCH(condition, buf, "$", clause_ends_with);
262 MATCH(condition, buf, "!$", clause_not_ends_with);
263 MATCH(condition, buf, "@", clause_oneof);
265 return 0;
268 static bool read_clause(struct tagcache_search_clause *clause)
270 char buf[256];
272 if (get_tag(&clause->tag) <= 0)
273 return false;
275 if (get_clause(&clause->type) <= 0)
276 return false;
278 if (get_token_str(buf, sizeof buf) < 0)
279 return false;
281 clause->str = buffer_alloc(strlen(buf)+1);
282 strcpy(clause->str, buf);
284 logf("got clause: %d/%d [%s]", clause->tag, clause->type, clause->str);
286 if (*(clause->str) == '\0')
287 clause->input = true;
288 else
289 clause->input = false;
291 if (tagcache_is_numeric_tag(clause->tag))
293 clause->numeric = true;
294 clause->numeric_data = atoi(clause->str);
296 else
297 clause->numeric = false;
299 return true;
302 static bool read_variable(char *buf, int size)
304 int condition;
306 if (!get_clause(&condition))
307 return false;
309 if (condition != clause_is)
310 return false;
312 if (get_token_str(buf, size) < 0)
313 return false;
315 return true;
318 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
319 static int get_format_str(struct display_format *fmt)
321 int ret;
322 char buf[128];
323 int i;
325 memset(fmt, 0, sizeof(struct display_format));
327 if (get_token_str(fmt->name, sizeof fmt->name) < 0)
328 return -10;
330 /* Determine the group id */
331 fmt->group_id = 0;
332 for (i = 0; i < format_count; i++)
334 if (!strcasecmp(formats[i]->name, fmt->name))
336 fmt->group_id = formats[i]->group_id;
337 break;
340 if (formats[i]->group_id > fmt->group_id)
341 fmt->group_id = formats[i]->group_id;
344 if (i == format_count)
345 fmt->group_id++;
347 logf("format: (%d) %s", fmt->group_id, fmt->name);
349 if (get_token_str(buf, sizeof buf) < 0)
350 return -10;
352 fmt->formatstr = buffer_alloc(strlen(buf) + 1);
353 strcpy(fmt->formatstr, buf);
355 while (fmt->tag_count < MAX_TAGS)
357 ret = get_tag(&fmt->tags[fmt->tag_count]);
358 if (ret < 0)
359 return -11;
361 if (ret == 0)
362 break;
364 switch (fmt->tags[fmt->tag_count]) {
365 case var_sorttype:
366 if (!read_variable(buf, sizeof buf))
367 return -12;
368 if (!strcasecmp("inverse", buf))
369 fmt->sort_inverse = true;
371 fmt->sort = true;
372 break;
374 case var_limit:
375 if (!read_variable(buf, sizeof buf))
376 return -13;
377 fmt->limit = atoi(buf);
378 break;
380 case var_strip:
381 if (!read_variable(buf, sizeof buf))
382 return -14;
383 fmt->strip = atoi(buf);
384 break;
386 default:
387 fmt->tag_count++;
391 return 1;
394 static int add_format(const char *buf)
396 strp = buf;
398 if (formats[format_count] == NULL)
399 formats[format_count] = buffer_alloc(sizeof(struct display_format));
401 memset(formats[format_count], 0, sizeof(struct display_format));
402 if (get_format_str(formats[format_count]) < 0)
404 logf("get_format_str() parser failed!");
405 return -4;
408 while (*strp != '\0' && *strp != '?')
409 strp++;
411 if (*strp == '?')
413 int clause_count = 0;
414 strp++;
416 while (1)
418 if (clause_count >= TAGCACHE_MAX_CLAUSES)
420 logf("too many clauses");
421 break;
424 formats[format_count]->clause[clause_count] =
425 buffer_alloc(sizeof(struct tagcache_search_clause));
427 if (!read_clause(formats[format_count]->clause[clause_count]))
428 break;
430 clause_count++;
433 formats[format_count]->clause_count = clause_count;
436 format_count++;
438 return 1;
441 static int get_condition(struct search_instruction *inst)
443 int clause_count;
444 char buf[128];
446 switch (*strp)
448 case '=':
450 int i;
452 if (get_token_str(buf, sizeof buf) < 0)
453 return -1;
455 for (i = 0; i < format_count; i++)
457 if (!strcasecmp(formats[i]->name, buf))
458 break;
461 if (i == format_count)
463 logf("format not found: %s", buf);
464 return -2;
467 inst->format_id[inst->tagorder_count] = formats[i]->group_id;
468 return 1;
470 case '?':
471 case ' ':
472 case '&':
473 strp++;
474 return 1;
475 case '-':
476 case '\0':
477 return 0;
480 clause_count = inst->clause_count[inst->tagorder_count];
481 if (clause_count >= TAGCACHE_MAX_CLAUSES)
483 logf("Too many clauses");
484 return false;
487 inst->clause[inst->tagorder_count][clause_count] =
488 buffer_alloc(sizeof(struct tagcache_search_clause));
490 if (!read_clause(inst->clause[inst->tagorder_count][clause_count]))
491 return -1;
493 inst->clause_count[inst->tagorder_count]++;
495 return 1;
498 /* example search:
499 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
500 * : album ? year >= "2000" : songs
501 * ^ begins with
502 * * contains
503 * $ ends with
506 static bool parse_search(struct menu_entry *entry, const char *str)
508 int ret;
509 int type;
510 struct search_instruction *inst = entry->si;
511 char buf[MAX_PATH];
512 int i;
514 strp = str;
516 /* Parse entry name */
517 if (get_token_str(entry->name, sizeof entry->name) < 0)
519 logf("No name found.");
520 return false;
523 /* Parse entry type */
524 if (get_tag(&entry->type) <= 0)
525 return false;
527 if (entry->type == menu_load)
529 if (get_token_str(buf, sizeof buf) < 0)
530 return false;
532 /* Find the matching root menu or "create" it */
533 for (i = 0; i < menu_count; i++)
535 if (!strcasecmp(menus[i]->id, buf))
537 entry->link = i;
538 menus[i]->parent = menu;
539 return true;
543 return false;
546 if (entry->type != menu_next)
547 return false;
549 while (inst->tagorder_count < MAX_TAGS)
551 ret = get_tag(&inst->tagorder[inst->tagorder_count]);
552 if (ret < 0)
554 logf("Parse error #1");
555 logf("%s", strp);
556 return false;
559 if (ret == 0)
560 break ;
562 logf("tag: %d", inst->tagorder[inst->tagorder_count]);
564 while ( (ret = get_condition(inst)) > 0 ) ;
565 if (ret < 0)
566 return false;
568 inst->tagorder_count++;
570 if (get_tag(&type) <= 0 || type != menu_next)
571 break;
574 return true;
577 static int compare(const void *p1, const void *p2)
579 struct tagentry *e1 = (struct tagentry *)p1;
580 struct tagentry *e2 = (struct tagentry *)p2;
582 if (sort_inverse)
583 return strncasecmp(e2->name, e1->name, MAX_PATH);
585 return strncasecmp(e1->name, e2->name, MAX_PATH);
588 static void tagtree_buffer_event(struct mp3entry *id3, bool last_track)
590 (void)id3;
591 (void)last_track;
593 /* Do not gather data unless proper setting has been enabled. */
594 if (!global_settings.runtimedb)
595 return;
597 logf("be:%d%s", last_track, id3->path);
599 if (!tagcache_find_index(&tcs, id3->path))
601 logf("tc stat: not found: %s", id3->path);
602 return;
605 id3->playcount = tagcache_get_numeric(&tcs, tag_playcount);
606 id3->lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
607 id3->rating = tagcache_get_numeric(&tcs, tag_virt_autoscore) / 10;
609 tagcache_search_finish(&tcs);
612 static void tagtree_unbuffer_event(struct mp3entry *id3, bool last_track)
614 (void)last_track;
615 long playcount;
616 long playtime;
617 long lastplayed;
619 /* Do not gather data unless proper setting has been enabled. */
620 if (!global_settings.runtimedb)
622 logf("runtimedb gathering not enabled");
623 return;
626 /* Don't process unplayed tracks. */
627 if (id3->elapsed == 0)
629 logf("not logging unplayed track");
630 return;
633 if (!tagcache_find_index(&tcs, id3->path))
635 logf("tc stat: not found: %s", id3->path);
636 return;
639 playcount = tagcache_get_numeric(&tcs, tag_playcount);
640 playtime = tagcache_get_numeric(&tcs, tag_playtime);
641 lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
643 playcount++;
645 lastplayed = tagcache_increase_serial();
646 if (lastplayed < 0)
648 logf("incorrect tc serial:%d", lastplayed);
649 tagcache_search_finish(&tcs);
650 return;
653 /* Ignore the last 15s (crossfade etc.) */
654 playtime += MIN(id3->length, id3->elapsed + 15 * 1000);
656 logf("ube:%s", id3->path);
657 logf("-> %d/%d/%d", last_track, playcount, playtime);
658 logf("-> %d/%d/%d", id3->elapsed, id3->length, MIN(id3->length, id3->elapsed + 15 * 1000));
660 /* lastplayed not yet supported. */
662 if (!tagcache_modify_numeric_entry(&tcs, tag_playcount, playcount)
663 || !tagcache_modify_numeric_entry(&tcs, tag_playtime, playtime)
664 || !tagcache_modify_numeric_entry(&tcs, tag_lastplayed, lastplayed))
666 logf("tc stat: modify failed!");
667 tagcache_search_finish(&tcs);
668 return;
671 tagcache_search_finish(&tcs);
674 bool tagtree_export(void)
676 gui_syncsplash(0, true, str(LANG_CREATING));
677 if (!tagcache_create_changelog(&tcs))
679 gui_syncsplash(HZ*2, true, str(LANG_FAILED));
682 return false;
685 bool tagtree_import(void)
687 gui_syncsplash(0, true, str(LANG_WAIT));
688 if (!tagcache_import_changelog())
690 gui_syncsplash(HZ*2, true, str(LANG_FAILED));
693 return false;
696 static bool parse_menu(const char *filename);
698 static int parse_line(int n, const char *buf, void *parameters)
700 char data[256];
701 int variable;
702 static bool read_menu;
703 int i;
705 (void)parameters;
707 logf("parse:%d/%s", n, buf);
709 /* First line, do initialisation. */
710 if (n == 0)
712 if (strcasecmp(TAGNAVI_VERSION, buf))
714 logf("Version mismatch");
715 return -1;
718 read_menu = false;
721 if (buf[0] == '#')
722 return 0;
724 if (buf[0] == '\0')
726 if (read_menu)
728 /* End the menu */
729 menu_count++;
730 read_menu = false;
732 return 0;
735 if (!read_menu)
737 strp = buf;
738 if (get_tag(&variable) <= 0)
739 return 0;
741 switch (variable)
743 case var_format:
744 if (add_format(strp) < 0)
746 logf("Format add fail: %s", data);
748 break;
750 case var_include:
751 if (get_token_str(data, sizeof(data)) < 0)
753 logf("%include empty");
754 return 0;
757 if (!parse_menu(data))
759 logf("Load menu fail: %s", data);
761 break;
763 case var_menu_start:
764 if (menu_count >= TAGMENU_MAX_MENUS)
766 logf("max menucount reached");
767 return 0;
770 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
771 menu = menus[menu_count];
772 memset(menu, 0, sizeof(struct root_menu));
773 if (get_token_str(menu->id, sizeof(menu->id)) < 0)
775 logf("%menu_start id empty");
776 return 0;
778 if (get_token_str(menu->title, sizeof(menu->title)) < 0)
780 logf("%menu_start title empty");
781 return 0;
783 logf("menu: %s", menu->title);
784 menu->itemcount = 0;
785 read_menu = true;
786 break;
788 case var_rootmenu:
789 /* Only set root menu once. */
790 if (root_menu)
791 break;
793 if (get_token_str(data, sizeof(data)) < 0)
795 logf("%root_menu empty");
796 return 0;
799 for (i = 0; i < menu_count; i++)
801 if (!strcasecmp(menus[i]->id, data))
803 root_menu = i;
806 break;
809 return 0;
812 if (menu->itemcount >= TAGMENU_MAX_ITEMS)
814 logf("max itemcount reached");
815 return 0;
818 /* Allocate */
819 if (menu->items[menu->itemcount] == NULL)
821 menu->items[menu->itemcount] = buffer_alloc(sizeof(struct menu_entry));
822 memset(menu->items[menu->itemcount], 0, sizeof(struct menu_entry));
823 menu->items[menu->itemcount]->si = buffer_alloc(sizeof(struct search_instruction));
826 memset(menu->items[menu->itemcount]->si, 0, sizeof(struct search_instruction));
827 if (!parse_search(menu->items[menu->itemcount], buf))
828 return 0;
830 menu->itemcount++;
832 return 0;
835 static bool parse_menu(const char *filename)
837 int fd;
838 char buf[1024];
840 if (menu_count >= TAGMENU_MAX_MENUS)
842 logf("max menucount reached");
843 return false;
846 fd = open(filename, O_RDONLY);
847 if (fd < 0)
849 logf("Search instruction file not found.");
850 return false;
853 /* Now read file for real, parsing into si */
854 fast_readline(fd, buf, sizeof buf, NULL, parse_line);
855 close(fd);
857 return true;
860 void tagtree_init(void)
862 format_count = 0;
863 menu_count = 0;
864 menu = NULL;
865 root_menu = 0;
866 parse_menu(FILE_SEARCH_INSTRUCTIONS);
868 uniqbuf = buffer_alloc(UNIQBUF_SIZE);
869 audio_set_track_buffer_event(tagtree_buffer_event);
870 audio_set_track_unbuffer_event(tagtree_unbuffer_event);
873 static bool show_search_progress(bool init, int count)
875 static int last_tick = 0;
877 if (init)
879 last_tick = current_tick;
880 return true;
883 if (current_tick - last_tick > HZ/4)
885 gui_syncsplash(0, true, str(LANG_PLAYLIST_SEARCH_MSG), count,
886 #if CONFIG_KEYPAD == PLAYER_PAD
887 str(LANG_STOP_ABORT)
888 #else
889 str(LANG_OFF_ABORT)
890 #endif
892 if (action_userabort(TIMEOUT_NOBLOCK))
893 return false;
894 last_tick = current_tick;
895 yield();
898 return true;
901 static int format_str(struct tagcache_search *tcs, struct display_format *fmt,
902 char *buf, int buf_size)
904 char fmtbuf[8];
905 bool read_format = false;
906 int fmtbuf_pos = 0;
907 int parpos = 0;
908 int buf_pos = 0;
909 int i;
911 memset(buf, 0, buf_size);
912 for (i = 0; fmt->formatstr[i] != '\0'; i++)
914 if (fmt->formatstr[i] == '%')
916 read_format = true;
917 fmtbuf_pos = 0;
918 if (parpos >= fmt->tag_count)
920 logf("too many format tags");
921 return -1;
925 if (read_format)
927 fmtbuf[fmtbuf_pos++] = fmt->formatstr[i];
928 if (fmtbuf_pos >= buf_size)
930 logf("format parse error");
931 return -2;
934 if (fmt->formatstr[i] == 's')
936 fmtbuf[fmtbuf_pos] = '\0';
937 read_format = false;
938 if (fmt->tags[parpos] == tcs->type)
940 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf, tcs->result);
942 else
944 /* Need to fetch the tag data. */
945 if (!tagcache_retrieve(tcs, tcs->idx_id, fmt->tags[parpos],
946 &buf[buf_pos], buf_size - buf_pos))
948 logf("retrieve failed");
949 return -3;
952 buf_pos += strlen(&buf[buf_pos]);
953 parpos++;
955 else if (fmt->formatstr[i] == 'd')
957 fmtbuf[fmtbuf_pos] = '\0';
958 read_format = false;
959 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf,
960 tagcache_get_numeric(tcs, fmt->tags[parpos]));
961 buf_pos += strlen(&buf[buf_pos]);
962 parpos++;
964 continue;
967 buf[buf_pos++] = fmt->formatstr[i];
969 if (buf_pos - 1 >= buf_size)
971 logf("buffer overflow");
972 return -4;
976 buf[buf_pos++] = '\0';
978 return 0;
981 static int retrieve_entries(struct tree_context *c, struct tagcache_search *tcs,
982 int offset, bool init)
984 struct tagentry *dptr = (struct tagentry *)c->dircache;
985 struct display_format *fmt;
986 int i;
987 int namebufused = 0;
988 int total_count = 0;
989 int special_entry_count = 0;
990 int level = c->currextra;
991 int tag;
992 bool sort = false;
993 int sort_limit;
994 int strip;
996 if (init
997 #ifdef HAVE_TC_RAMCACHE
998 && !tagcache_is_ramcache()
999 #endif
1002 show_search_progress(true, 0);
1003 gui_syncsplash(0, true, str(LANG_PLAYLIST_SEARCH_MSG),
1004 0, csi->name);
1007 if (c->currtable == allsubentries)
1009 tag = tag_title;
1010 level--;
1012 else
1013 tag = csi->tagorder[level];
1015 if (!tagcache_search(tcs, tag))
1016 return -1;
1018 /* Prevent duplicate entries in the search list. */
1019 tagcache_search_set_uniqbuf(tcs, uniqbuf, UNIQBUF_SIZE);
1021 if (level || csi->clause_count[0] || tagcache_is_numeric_tag(tag))
1022 sort = true;
1024 for (i = 0; i < level; i++)
1026 if (tagcache_is_numeric_tag(csi->tagorder[i]))
1028 static struct tagcache_search_clause cc;
1030 memset(&cc, 0, sizeof(struct tagcache_search_clause));
1031 cc.tag = csi->tagorder[i];
1032 cc.type = clause_is;
1033 cc.numeric = true;
1034 cc.numeric_data = csi->result_seek[i];
1035 tagcache_search_add_clause(tcs, &cc);
1037 else
1039 tagcache_search_add_filter(tcs, csi->tagorder[i],
1040 csi->result_seek[i]);
1044 for (i = 0; i <= level; i++)
1046 int j;
1048 for (j = 0; j < csi->clause_count[i]; j++)
1049 tagcache_search_add_clause(tcs, csi->clause[i][j]);
1052 current_offset = offset;
1053 current_entry_count = 0;
1054 c->dirfull = false;
1056 fmt = NULL;
1057 for (i = 0; i < format_count; i++)
1059 if (formats[i]->group_id == csi->format_id[level])
1060 fmt = formats[i];
1063 if (fmt)
1065 sort_inverse = fmt->sort_inverse;
1066 sort_limit = fmt->limit;
1067 strip = fmt->strip;
1069 /* Check if sorting is forced. */
1070 if (fmt->sort)
1071 sort = true;
1073 else
1075 sort_inverse = false;
1076 sort_limit = 0;
1077 strip = 0;
1080 if (tag != tag_title && tag != tag_filename)
1082 if (offset == 0)
1084 dptr->newtable = allsubentries;
1085 dptr->name = str(LANG_TAGNAVI_ALL_TRACKS);
1086 dptr++;
1087 current_entry_count++;
1089 special_entry_count++;
1092 total_count += special_entry_count;
1094 while (tagcache_get_next(tcs))
1096 if (total_count++ < offset)
1097 continue;
1099 dptr->newtable = navibrowse;
1100 if (tag == tag_title || tag == tag_filename)
1102 dptr->newtable = playtrack;
1103 dptr->extraseek = tcs->idx_id;
1105 else
1106 dptr->extraseek = tcs->result_seek;
1108 fmt = NULL;
1109 /* Check the format */
1110 for (i = 0; i < format_count; i++)
1112 if (formats[i]->group_id != csi->format_id[level])
1113 continue;
1115 if (tagcache_check_clauses(tcs, formats[i]->clause,
1116 formats[i]->clause_count))
1118 fmt = formats[i];
1119 break;
1123 if (!tcs->ramresult || fmt)
1125 char buf[MAX_PATH];
1127 if (fmt)
1129 if (format_str(tcs, fmt, buf, sizeof buf) < 0)
1131 logf("format_str() failed");
1132 return 0;
1136 dptr->name = &c->name_buffer[namebufused];
1137 if (fmt)
1138 namebufused += strlen(buf)+1;
1139 else
1140 namebufused += tcs->result_len;
1142 if (namebufused >= c->name_buffer_size)
1144 logf("chunk mode #2: %d", current_entry_count);
1145 c->dirfull = true;
1146 sort = false;
1147 break ;
1149 if (fmt)
1150 strcpy(dptr->name, buf);
1151 else
1152 strcpy(dptr->name, tcs->result);
1154 else
1155 dptr->name = tcs->result;
1157 dptr++;
1158 current_entry_count++;
1160 if (current_entry_count >= global_settings.max_files_in_dir)
1162 logf("chunk mode #3: %d", current_entry_count);
1163 c->dirfull = true;
1164 sort = false;
1165 break ;
1168 if (init && !tcs->ramsearch)
1170 if (!show_search_progress(false, i))
1172 tagcache_search_finish(tcs);
1173 return current_entry_count;
1178 if (sort)
1179 qsort(c->dircache + special_entry_count * c->dentry_size,
1180 current_entry_count - special_entry_count,
1181 c->dentry_size, compare);
1183 if (!init)
1185 tagcache_search_finish(tcs);
1186 return current_entry_count;
1189 while (tagcache_get_next(tcs))
1191 if (!tcs->ramsearch)
1193 if (!show_search_progress(false, total_count))
1194 break;
1196 total_count++;
1199 tagcache_search_finish(tcs);
1201 if (!sort && (sort_inverse || sort_limit))
1203 gui_syncsplash(HZ*4, true, str(LANG_SHOWDIR_BUFFER_FULL), total_count);
1204 logf("Too small dir buffer");
1205 return 0;
1208 if (sort_limit)
1209 total_count = MIN(total_count, sort_limit);
1211 if (strip)
1213 dptr = c->dircache;
1214 for (i = 0; i < total_count; i++, dptr++)
1216 int len = strlen(dptr->name);
1218 if (len < strip)
1219 continue;
1221 dptr->name = &dptr->name[strip];
1225 return total_count;
1228 static int load_root(struct tree_context *c)
1230 struct tagentry *dptr = (struct tagentry *)c->dircache;
1231 int i;
1233 tc = c;
1234 c->currtable = root;
1235 if (c->dirlevel == 0)
1236 c->currextra = root_menu;
1238 menu = menus[c->currextra];
1239 if (menu == NULL)
1240 return 0;
1242 for (i = 0; i < menu->itemcount; i++)
1244 dptr->name = menu->items[i]->name;
1245 switch (menu->items[i]->type)
1247 case menu_next:
1248 dptr->newtable = navibrowse;
1249 dptr->extraseek = i;
1250 break;
1252 case menu_load:
1253 dptr->newtable = root;
1254 dptr->extraseek = menu->items[i]->link;
1255 break;
1258 dptr++;
1261 current_offset = 0;
1262 current_entry_count = i;
1264 return i;
1267 int tagtree_load(struct tree_context* c)
1269 int count;
1270 int table = c->currtable;
1272 c->dentry_size = sizeof(struct tagentry);
1273 c->dirsindir = 0;
1275 if (!table)
1277 c->dirfull = false;
1278 table = root;
1279 c->currtable = table;
1280 c->currextra = root_menu;
1283 switch (table)
1285 case root:
1286 count = load_root(c);
1287 break;
1289 case allsubentries:
1290 case navibrowse:
1291 logf("navibrowse...");
1292 cpu_boost(true);
1293 count = retrieve_entries(c, &tcs, 0, true);
1294 cpu_boost(false);
1295 break;
1297 default:
1298 logf("Unsupported table %d\n", table);
1299 return -1;
1302 if (count < 0)
1304 c->dirlevel = 0;
1305 count = load_root(c);
1306 gui_syncsplash(HZ, true, str(LANG_TAGCACHE_BUSY));
1309 /* The _total_ numer of entries available. */
1310 c->dirlength = c->filesindir = count;
1312 return count;
1315 int tagtree_enter(struct tree_context* c)
1317 int rc = 0;
1318 struct tagentry *dptr;
1319 int newextra;
1320 int seek;
1322 dptr = tagtree_get_entry(c, c->selected_item);
1324 c->dirfull = false;
1325 newextra = dptr->newtable;
1326 seek = dptr->extraseek;
1328 if (c->dirlevel >= MAX_DIR_LEVELS)
1329 return 0;
1331 c->selected_item_history[c->dirlevel]=c->selected_item;
1332 c->table_history[c->dirlevel] = c->currtable;
1333 c->extra_history[c->dirlevel] = c->currextra;
1334 c->pos_history[c->dirlevel] = c->firstpos;
1335 c->dirlevel++;
1337 switch (c->currtable) {
1338 case root:
1339 c->currextra = newextra;
1341 if (newextra == root)
1343 menu = menus[seek];
1344 c->currextra = seek;
1347 else if (newextra == navibrowse)
1349 int i, j;
1351 csi = menu->items[seek]->si;
1352 c->currextra = 0;
1354 strncpy(current_title[c->currextra], dptr->name,
1355 sizeof(current_title[0]) - 1);
1357 /* Read input as necessary. */
1358 for (i = 0; i < csi->tagorder_count; i++)
1360 for (j = 0; j < csi->clause_count[i]; j++)
1362 if (!csi->clause[i][j]->input)
1363 continue;
1365 rc = kbd_input(searchstring, sizeof(searchstring));
1366 if (rc == -1 || !searchstring[0])
1368 tagtree_exit(c);
1369 return 0;
1372 if (csi->clause[i][j]->numeric)
1373 csi->clause[i][j]->numeric_data = atoi(searchstring);
1374 else
1375 csi->clause[i][j]->str = searchstring;
1379 c->currtable = newextra;
1381 break;
1383 case navibrowse:
1384 case allsubentries:
1385 if (newextra == playtrack)
1387 c->dirlevel--;
1388 /* about to create a new current playlist...
1389 allow user to cancel the operation */
1390 if (global_settings.warnon_erase_dynplaylist &&
1391 !global_settings.party_mode &&
1392 playlist_modified(NULL))
1394 char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
1395 struct text_message message={lines, 1};
1397 if (gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
1398 break;
1401 if (tagtree_play_folder(c) >= 0)
1402 rc = 2;
1403 break;
1406 c->currtable = newextra;
1407 csi->result_seek[c->currextra] = seek;
1408 if (c->currextra < csi->tagorder_count-1)
1409 c->currextra++;
1410 else
1411 c->dirlevel--;
1413 /* Update the statusbar title */
1414 strncpy(current_title[c->currextra], dptr->name,
1415 sizeof(current_title[0]) - 1);
1416 break;
1418 default:
1419 c->dirlevel--;
1420 break;
1423 c->selected_item=0;
1424 gui_synclist_select_item(&tree_lists, c->selected_item);
1426 return rc;
1429 void tagtree_exit(struct tree_context* c)
1431 c->dirfull = false;
1432 if (c->dirlevel > 0)
1433 c->dirlevel--;
1434 c->selected_item=c->selected_item_history[c->dirlevel];
1435 gui_synclist_select_item(&tree_lists, c->selected_item);
1436 c->currtable = c->table_history[c->dirlevel];
1437 c->currextra = c->extra_history[c->dirlevel];
1438 c->firstpos = c->pos_history[c->dirlevel];
1441 int tagtree_get_filename(struct tree_context* c, char *buf, int buflen)
1443 struct tagentry *entry;
1445 entry = tagtree_get_entry(c, c->selected_item);
1447 if (!tagcache_search(&tcs, tag_filename))
1448 return -1;
1450 if (!tagcache_retrieve(&tcs, entry->extraseek, tcs.type, buf, buflen))
1452 tagcache_search_finish(&tcs);
1453 return -2;
1456 tagcache_search_finish(&tcs);
1458 return 0;
1461 static bool insert_all_playlist(struct tree_context *c, int position, bool queue)
1463 int i;
1464 char buf[MAX_PATH];
1465 int from, to, direction;
1466 int files_left = c->filesindir;
1468 cpu_boost(true);
1469 if (!tagcache_search(&tcs, tag_filename))
1471 gui_syncsplash(HZ, true, str(LANG_TAGCACHE_BUSY));
1472 cpu_boost(false);
1473 return false;
1476 if (position == PLAYLIST_REPLACE)
1478 if (remove_all_tracks(NULL) == 0)
1479 position = PLAYLIST_INSERT_LAST;
1480 else return -1; }
1482 if (position == PLAYLIST_INSERT_FIRST)
1484 from = c->filesindir - 1;
1485 to = -1;
1486 direction = -1;
1488 else
1490 from = 0;
1491 to = c->filesindir;
1492 direction = 1;
1495 for (i = from; i != to; i += direction)
1497 /* Count back to zero */
1498 if (!show_search_progress(false, files_left--))
1499 break;
1501 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, i)->extraseek,
1502 tcs.type, buf, sizeof buf))
1504 continue;
1507 if (playlist_insert_track(NULL, buf, position, queue, false) < 0)
1509 logf("playlist_insert_track failed");
1510 break;
1512 yield();
1514 playlist_sync(NULL);
1515 tagcache_search_finish(&tcs);
1516 cpu_boost(false);
1518 return true;
1521 bool tagtree_insert_selection_playlist(int position, bool queue)
1523 struct tagentry *dptr;
1524 char buf[MAX_PATH];
1525 int dirlevel = tc->dirlevel;
1527 /* We need to set the table to allsubentries. */
1528 show_search_progress(true, 0);
1530 dptr = tagtree_get_entry(tc, tc->selected_item);
1532 /* Insert a single track? */
1533 if (dptr->newtable == playtrack)
1535 if (tagtree_get_filename(tc, buf, sizeof buf) < 0)
1537 logf("tagtree_get_filename failed");
1538 return false;
1540 playlist_insert_track(NULL, buf, position, queue, true);
1542 return true;
1545 if (dptr->newtable == navibrowse)
1547 tagtree_enter(tc);
1548 tagtree_load(tc);
1549 dptr = tagtree_get_entry(tc, tc->selected_item);
1551 else if (dptr->newtable != allsubentries)
1553 logf("unsupported table: %d", dptr->newtable);
1554 return false;
1557 /* Now the current table should be allsubentries. */
1558 if (dptr->newtable != playtrack)
1560 tagtree_enter(tc);
1561 tagtree_load(tc);
1562 dptr = tagtree_get_entry(tc, tc->selected_item);
1564 /* And now the newtable should be playtrack. */
1565 if (dptr->newtable != playtrack)
1567 logf("newtable: %d !!", dptr->newtable);
1568 tc->dirlevel = dirlevel;
1569 return false;
1573 if (tc->filesindir <= 0)
1574 gui_syncsplash(HZ, true, str(LANG_END_PLAYLIST_PLAYER));
1575 else
1577 logf("insert_all_playlist");
1578 if (!insert_all_playlist(tc, position, queue))
1579 gui_syncsplash(HZ*2, true, str(LANG_FAILED));
1582 /* Finally return the dirlevel to its original value. */
1583 while (tc->dirlevel > dirlevel)
1584 tagtree_exit(tc);
1585 tagtree_load(tc);
1587 return true;
1590 static int tagtree_play_folder(struct tree_context* c)
1592 if (playlist_create(NULL, NULL) < 0)
1594 logf("Failed creating playlist\n");
1595 return -1;
1598 if (!insert_all_playlist(c, PLAYLIST_INSERT_LAST, false))
1599 return -2;
1601 if (global_settings.playlist_shuffle)
1602 c->selected_item = playlist_shuffle(current_tick, c->selected_item);
1603 if (!global_settings.play_selected)
1604 c->selected_item = 0;
1605 gui_synclist_select_item(&tree_lists, c->selected_item);
1607 playlist_start(c->selected_item,0);
1609 return 0;
1612 struct tagentry* tagtree_get_entry(struct tree_context *c, int id)
1614 struct tagentry *entry = (struct tagentry *)c->dircache;
1615 int realid = id - current_offset;
1617 /* Load the next chunk if necessary. */
1618 if (realid >= current_entry_count || realid < 0)
1620 cpu_boost(true);
1621 if (retrieve_entries(c, &tcs2, MAX(0, id - (current_entry_count / 2)),
1622 false) < 0)
1624 logf("retrieve failed");
1625 cpu_boost(false);
1626 return NULL;
1628 realid = id - current_offset;
1629 cpu_boost(false);
1632 return &entry[realid];
1635 char *tagtree_get_title(struct tree_context* c)
1637 switch (c->currtable)
1639 case root:
1640 return menu->title;
1642 case navibrowse:
1643 case allsubentries:
1644 return current_title[c->currextra];
1647 return "?";
1650 int tagtree_get_attr(struct tree_context* c)
1652 int attr = -1;
1653 switch (c->currtable)
1655 case navibrowse:
1656 if (csi->tagorder[c->currextra] == tag_title)
1657 attr = TREE_ATTR_MPA;
1658 else
1659 attr = ATTR_DIRECTORY;
1660 break;
1662 case allsubentries:
1663 attr = TREE_ATTR_MPA;
1664 break;
1666 default:
1667 attr = ATTR_DIRECTORY;
1668 break;
1671 return attr;
1674 #ifdef HAVE_LCD_BITMAP
1675 const unsigned char* tagtree_get_icon(struct tree_context* c)
1676 #else
1677 int tagtree_get_icon(struct tree_context* c)
1678 #endif
1680 int icon = Icon_Folder;
1682 if (tagtree_get_attr(c) == TREE_ATTR_MPA)
1683 icon = Icon_Audio;
1685 #ifdef HAVE_LCD_BITMAP
1686 return bitmap_icons_6x8[icon];
1687 #else
1688 return icon;
1689 #endif