Add a get_offset API call to make codec_advance_buffer_loc_callback work.
[Rockbox.git] / apps / tagtree.c
blobf9ae0bebd3f33ffae81484c12c0bf0534ea07a56
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 "config.h"
28 #include "system.h"
29 #include "kernel.h"
30 #include "splash.h"
31 #include "icons.h"
32 #include "tree.h"
33 #include "action.h"
34 #include "settings.h"
35 #include "tagcache.h"
36 #include "tagtree.h"
37 #include "lang.h"
38 #include "logf.h"
39 #include "playlist.h"
40 #include "keyboard.h"
41 #include "gui/list.h"
42 #include "buffer.h"
43 #include "atoi.h"
44 #include "playback.h"
45 #include "yesno.h"
46 #include "misc.h"
47 #include "filetypes.h"
49 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
51 static int tagtree_play_folder(struct tree_context* c);
53 static char searchstring[128];
55 enum variables {
56 var_sorttype = 100,
57 var_limit,
58 var_strip,
59 var_menu_start,
60 var_include,
61 var_rootmenu,
62 var_format,
63 menu_next,
64 menu_load,
67 /* Capacity 10 000 entries (for example 10k different artists) */
68 #define UNIQBUF_SIZE (64*1024)
69 static long *uniqbuf;
71 #define MAX_TAGS 5
72 #define MAX_MENU_ID_SIZE 32
74 static struct tagcache_search tcs, tcs2;
75 static bool sort_inverse;
78 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
80 * valid = true
81 * formatstr = "%-3d. %s"
82 * tags[0] = tag_autoscore
83 * tags[1] = tag_title
84 * tag_count = 2
86 * limit = 100
87 * sort_inverse = true
89 struct display_format {
90 char name[32];
91 struct tagcache_search_clause *clause[TAGCACHE_MAX_CLAUSES];
92 int clause_count;
93 char *formatstr;
94 int group_id;
95 int tags[MAX_TAGS];
96 int tag_count;
98 int limit;
99 int strip;
100 bool sort_inverse;
101 bool sort;
104 static struct display_format *formats[TAGMENU_MAX_FMTS];
105 static int format_count;
107 struct search_instruction {
108 char name[64];
109 int tagorder[MAX_TAGS];
110 int tagorder_count;
111 struct tagcache_search_clause *clause[MAX_TAGS][TAGCACHE_MAX_CLAUSES];
112 int format_id[MAX_TAGS];
113 int clause_count[MAX_TAGS];
114 int result_seek[MAX_TAGS];
117 struct menu_entry {
118 char name[64];
119 int type;
120 struct search_instruction *si;
121 int link;
124 struct root_menu {
125 char title[64];
126 char id[MAX_MENU_ID_SIZE];
127 int itemcount;
128 struct menu_entry *items[TAGMENU_MAX_ITEMS];
131 /* Statusbar text of the current view. */
132 static char current_title[MAX_TAGS][128];
134 static struct root_menu *menus[TAGMENU_MAX_MENUS];
135 static struct root_menu *menu;
136 static struct search_instruction *csi;
137 static const char *strp;
138 static int menu_count;
139 static int root_menu;
141 static int current_offset;
142 static int current_entry_count;
144 static int format_count;
145 static struct tree_context *tc;
147 static int get_token_str(char *buf, int size)
149 /* Find the start. */
150 while (*strp != '"' && *strp != '\0')
151 strp++;
153 if (*strp == '\0' || *(++strp) == '\0')
154 return -1;
156 /* Read the data. */
157 while (*strp != '"' && *strp != '\0' && --size > 0)
158 *(buf++) = *(strp++);
160 *buf = '\0';
161 if (*strp != '"')
162 return -2;
164 strp++;
166 return 0;
169 #define MATCH(tag,str1,str2,settag) \
170 if (!strcasecmp(str1, str2)) { \
171 *tag = settag; \
172 return 1; \
175 static int get_tag(int *tag)
177 char buf[128];
178 int i;
180 /* Find the start. */
181 while ((*strp == ' ' || *strp == '>') && *strp != '\0')
182 strp++;
184 if (*strp == '\0' || *strp == '?')
185 return 0;
187 for (i = 0; i < (int)sizeof(buf)-1; i++)
189 if (*strp == '\0' || *strp == ' ')
190 break ;
191 buf[i] = *strp;
192 strp++;
194 buf[i] = '\0';
196 MATCH(tag, buf, "album", tag_album);
197 MATCH(tag, buf, "artist", tag_artist);
198 MATCH(tag, buf, "bitrate", tag_bitrate);
199 MATCH(tag, buf, "composer", tag_composer);
200 MATCH(tag, buf, "comment", tag_comment);
201 MATCH(tag, buf, "albumartist", tag_albumartist);
202 MATCH(tag, buf, "ensemble", tag_albumartist);
203 MATCH(tag, buf, "grouping", tag_grouping);
204 MATCH(tag, buf, "genre", tag_genre);
205 MATCH(tag, buf, "length", tag_length);
206 MATCH(tag, buf, "Lm", tag_virt_length_min);
207 MATCH(tag, buf, "Ls", tag_virt_length_sec);
208 MATCH(tag, buf, "Pm", tag_virt_playtime_min);
209 MATCH(tag, buf, "Ps", tag_virt_playtime_sec);
210 MATCH(tag, buf, "title", tag_title);
211 MATCH(tag, buf, "filename", tag_filename);
212 MATCH(tag, buf, "tracknum", tag_tracknumber);
213 MATCH(tag, buf, "discnum", tag_discnumber);
214 MATCH(tag, buf, "year", tag_year);
215 MATCH(tag, buf, "playcount", tag_playcount);
216 MATCH(tag, buf, "rating", tag_rating);
217 MATCH(tag, buf, "lastplayed", tag_lastplayed);
218 MATCH(tag, buf, "commitid", tag_commitid);
219 MATCH(tag, buf, "entryage", tag_virt_entryage);
220 MATCH(tag, buf, "autoscore", tag_virt_autoscore);
221 MATCH(tag, buf, "%sort", var_sorttype);
222 MATCH(tag, buf, "%limit", var_limit);
223 MATCH(tag, buf, "%strip", var_strip);
224 MATCH(tag, buf, "%menu_start", var_menu_start);
225 MATCH(tag, buf, "%include", var_include);
226 MATCH(tag, buf, "%root_menu", var_rootmenu);
227 MATCH(tag, buf, "%format", var_format);
228 MATCH(tag, buf, "->", menu_next);
229 MATCH(tag, buf, "==>", menu_load);
231 logf("NO MATCH: %s\n", buf);
232 if (buf[0] == '?')
233 return 0;
235 return -1;
238 static int get_clause(int *condition)
240 char buf[4];
241 int i;
243 /* Find the start. */
244 while (*strp == ' ' && *strp != '\0')
245 strp++;
247 if (*strp == '\0')
248 return 0;
250 for (i = 0; i < (int)sizeof(buf)-1; i++)
252 if (*strp == '\0' || *strp == ' ')
253 break ;
254 buf[i] = *strp;
255 strp++;
257 buf[i] = '\0';
259 MATCH(condition, buf, "=", clause_is);
260 MATCH(condition, buf, "==", clause_is);
261 MATCH(condition, buf, "!=", clause_is_not);
262 MATCH(condition, buf, ">", clause_gt);
263 MATCH(condition, buf, ">=", clause_gteq);
264 MATCH(condition, buf, "<", clause_lt);
265 MATCH(condition, buf, "<=", clause_lteq);
266 MATCH(condition, buf, "~", clause_contains);
267 MATCH(condition, buf, "!~", clause_not_contains);
268 MATCH(condition, buf, "^", clause_begins_with);
269 MATCH(condition, buf, "!^", clause_not_begins_with);
270 MATCH(condition, buf, "$", clause_ends_with);
271 MATCH(condition, buf, "!$", clause_not_ends_with);
272 MATCH(condition, buf, "@", clause_oneof);
274 return 0;
277 static bool read_clause(struct tagcache_search_clause *clause)
279 char buf[256];
281 if (get_tag(&clause->tag) <= 0)
282 return false;
284 if (get_clause(&clause->type) <= 0)
285 return false;
287 if (get_token_str(buf, sizeof buf) < 0)
288 return false;
290 clause->str = buffer_alloc(strlen(buf)+1);
291 strcpy(clause->str, buf);
293 logf("got clause: %d/%d [%s]", clause->tag, clause->type, clause->str);
295 if (*(clause->str) == '\0')
296 clause->input = true;
297 else
298 clause->input = false;
300 if (tagcache_is_numeric_tag(clause->tag))
302 clause->numeric = true;
303 clause->numeric_data = atoi(clause->str);
305 else
306 clause->numeric = false;
308 return true;
311 static bool read_variable(char *buf, int size)
313 int condition;
315 if (!get_clause(&condition))
316 return false;
318 if (condition != clause_is)
319 return false;
321 if (get_token_str(buf, size) < 0)
322 return false;
324 return true;
327 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
328 static int get_format_str(struct display_format *fmt)
330 int ret;
331 char buf[128];
332 int i;
334 memset(fmt, 0, sizeof(struct display_format));
336 if (get_token_str(fmt->name, sizeof fmt->name) < 0)
337 return -10;
339 /* Determine the group id */
340 fmt->group_id = 0;
341 for (i = 0; i < format_count; i++)
343 if (!strcasecmp(formats[i]->name, fmt->name))
345 fmt->group_id = formats[i]->group_id;
346 break;
349 if (formats[i]->group_id > fmt->group_id)
350 fmt->group_id = formats[i]->group_id;
353 if (i == format_count)
354 fmt->group_id++;
356 logf("format: (%d) %s", fmt->group_id, fmt->name);
358 if (get_token_str(buf, sizeof buf) < 0)
359 return -10;
361 fmt->formatstr = buffer_alloc(strlen(buf) + 1);
362 strcpy(fmt->formatstr, buf);
364 while (fmt->tag_count < MAX_TAGS)
366 ret = get_tag(&fmt->tags[fmt->tag_count]);
367 if (ret < 0)
368 return -11;
370 if (ret == 0)
371 break;
373 switch (fmt->tags[fmt->tag_count]) {
374 case var_sorttype:
375 if (!read_variable(buf, sizeof buf))
376 return -12;
377 if (!strcasecmp("inverse", buf))
378 fmt->sort_inverse = true;
380 fmt->sort = true;
381 break;
383 case var_limit:
384 if (!read_variable(buf, sizeof buf))
385 return -13;
386 fmt->limit = atoi(buf);
387 break;
389 case var_strip:
390 if (!read_variable(buf, sizeof buf))
391 return -14;
392 fmt->strip = atoi(buf);
393 break;
395 default:
396 fmt->tag_count++;
400 return 1;
403 static int add_format(const char *buf)
405 strp = buf;
407 if (formats[format_count] == NULL)
408 formats[format_count] = buffer_alloc(sizeof(struct display_format));
410 memset(formats[format_count], 0, sizeof(struct display_format));
411 if (get_format_str(formats[format_count]) < 0)
413 logf("get_format_str() parser failed!");
414 return -4;
417 while (*strp != '\0' && *strp != '?')
418 strp++;
420 if (*strp == '?')
422 int clause_count = 0;
423 strp++;
425 while (1)
427 if (clause_count >= TAGCACHE_MAX_CLAUSES)
429 logf("too many clauses");
430 break;
433 formats[format_count]->clause[clause_count] =
434 buffer_alloc(sizeof(struct tagcache_search_clause));
436 if (!read_clause(formats[format_count]->clause[clause_count]))
437 break;
439 clause_count++;
442 formats[format_count]->clause_count = clause_count;
445 format_count++;
447 return 1;
450 static int get_condition(struct search_instruction *inst)
452 int clause_count;
453 char buf[128];
455 switch (*strp)
457 case '=':
459 int i;
461 if (get_token_str(buf, sizeof buf) < 0)
462 return -1;
464 for (i = 0; i < format_count; i++)
466 if (!strcasecmp(formats[i]->name, buf))
467 break;
470 if (i == format_count)
472 logf("format not found: %s", buf);
473 return -2;
476 inst->format_id[inst->tagorder_count] = formats[i]->group_id;
477 return 1;
479 case '?':
480 case ' ':
481 case '&':
482 strp++;
483 return 1;
484 case '-':
485 case '\0':
486 return 0;
489 clause_count = inst->clause_count[inst->tagorder_count];
490 if (clause_count >= TAGCACHE_MAX_CLAUSES)
492 logf("Too many clauses");
493 return false;
496 inst->clause[inst->tagorder_count][clause_count] =
497 buffer_alloc(sizeof(struct tagcache_search_clause));
499 if (!read_clause(inst->clause[inst->tagorder_count][clause_count]))
500 return -1;
502 inst->clause_count[inst->tagorder_count]++;
504 return 1;
507 /* example search:
508 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
509 * : album ? year >= "2000" : songs
510 * ^ begins with
511 * * contains
512 * $ ends with
515 static bool parse_search(struct menu_entry *entry, const char *str)
517 int ret;
518 int type;
519 struct search_instruction *inst = entry->si;
520 char buf[MAX_PATH];
521 int i;
522 struct root_menu *new_menu;
524 strp = str;
526 /* Parse entry name */
527 if (get_token_str(entry->name, sizeof entry->name) < 0)
529 logf("No name found.");
530 return false;
533 /* Parse entry type */
534 if (get_tag(&entry->type) <= 0)
535 return false;
537 if (entry->type == menu_load)
539 if (get_token_str(buf, sizeof buf) < 0)
540 return false;
542 /* Find the matching root menu or "create" it */
543 for (i = 0; i < menu_count; i++)
545 if (!strcasecmp(menus[i]->id, buf))
547 entry->link = i;
548 return true;
552 if (menu_count >= TAGMENU_MAX_MENUS)
554 logf("max menucount reached");
555 return false;
558 /* Allocate a new menu unless link is found. */
559 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
560 new_menu = menus[menu_count];
561 memset(new_menu, 0, sizeof(struct root_menu));
562 strncpy(new_menu->id, buf, MAX_MENU_ID_SIZE-1);
563 entry->link = menu_count;
564 ++menu_count;
566 return true;
569 if (entry->type != menu_next)
570 return false;
572 while (inst->tagorder_count < MAX_TAGS)
574 ret = get_tag(&inst->tagorder[inst->tagorder_count]);
575 if (ret < 0)
577 logf("Parse error #1");
578 logf("%s", strp);
579 return false;
582 if (ret == 0)
583 break ;
585 logf("tag: %d", inst->tagorder[inst->tagorder_count]);
587 while ( (ret = get_condition(inst)) > 0 ) ;
588 if (ret < 0)
589 return false;
591 inst->tagorder_count++;
593 if (get_tag(&type) <= 0 || type != menu_next)
594 break;
597 return true;
600 static int compare(const void *p1, const void *p2)
602 struct tagentry *e1 = (struct tagentry *)p1;
603 struct tagentry *e2 = (struct tagentry *)p2;
605 if (sort_inverse)
606 return strncasecmp(e2->name, e1->name, MAX_PATH);
608 return strncasecmp(e1->name, e2->name, MAX_PATH);
611 static void tagtree_buffer_event(struct mp3entry *id3, bool last_track)
613 (void)id3;
614 (void)last_track;
616 /* Do not gather data unless proper setting has been enabled. */
617 if (!global_settings.runtimedb)
618 return;
620 logf("be:%d%s", last_track, id3->path);
622 if (!tagcache_find_index(&tcs, id3->path))
624 logf("tc stat: not found: %s", id3->path);
625 return;
628 id3->playcount = tagcache_get_numeric(&tcs, tag_playcount);
629 if (!id3->rating)
630 id3->rating = tagcache_get_numeric(&tcs, tag_rating);
631 id3->lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
632 id3->score = tagcache_get_numeric(&tcs, tag_virt_autoscore) / 10;
633 id3->playtime = tagcache_get_numeric(&tcs, tag_playtime);
635 /* Store our tagcache index pointer. */
636 id3->tagcache_idx = tcs.idx_id;
638 tagcache_search_finish(&tcs);
641 static void tagtree_unbuffer_event(struct mp3entry *id3, bool last_track)
643 (void)last_track;
644 long playcount;
645 long playtime;
646 long lastplayed;
648 /* Do not gather data unless proper setting has been enabled. */
649 if (!global_settings.runtimedb)
651 logf("runtimedb gathering not enabled");
652 return;
655 if (!id3->tagcache_idx)
657 logf("No tagcache index pointer found");
658 return;
661 /* Don't process unplayed tracks. */
662 if (id3->elapsed == 0)
664 logf("not logging unplayed track");
665 return;
668 playcount = id3->playcount + 1;
669 lastplayed = tagcache_increase_serial();
670 if (lastplayed < 0)
672 logf("incorrect tc serial:%ld", lastplayed);
673 return;
676 /* Ignore the last 15s (crossfade etc.) */
677 playtime = id3->playtime + MIN(id3->length, id3->elapsed + 15 * 1000);
679 logf("ube:%s", id3->path);
680 logf("-> %d/%ld/%ld", last_track, playcount, playtime);
681 logf("-> %ld/%ld/%ld", id3->elapsed, id3->length, MIN(id3->length, id3->elapsed + 15 * 1000));
683 /* Queue the updates to the tagcache system. */
684 tagcache_update_numeric(id3->tagcache_idx, tag_playcount, playcount);
685 tagcache_update_numeric(id3->tagcache_idx, tag_playtime, playtime);
686 tagcache_update_numeric(id3->tagcache_idx, tag_lastplayed, lastplayed);
689 bool tagtree_export(void)
691 gui_syncsplash(0, str(LANG_CREATING));
692 if (!tagcache_create_changelog(&tcs))
694 gui_syncsplash(HZ*2, ID2P(LANG_FAILED));
697 return false;
700 bool tagtree_import(void)
702 gui_syncsplash(0, ID2P(LANG_WAIT));
703 if (!tagcache_import_changelog())
705 gui_syncsplash(HZ*2, ID2P(LANG_FAILED));
708 return false;
711 static bool parse_menu(const char *filename);
713 static int parse_line(int n, const char *buf, void *parameters)
715 char data[256];
716 int variable;
717 static bool read_menu;
718 int i;
720 (void)parameters;
722 logf("parse:%d/%s", n, buf);
724 /* First line, do initialisation. */
725 if (n == 0)
727 if (strcasecmp(TAGNAVI_VERSION, buf))
729 logf("Version mismatch");
730 return -1;
733 read_menu = false;
736 if (buf[0] == '#')
737 return 0;
739 if (buf[0] == '\0')
741 if (read_menu)
743 /* End the menu */
744 read_menu = false;
746 return 0;
749 if (!read_menu)
751 strp = buf;
752 if (get_tag(&variable) <= 0)
753 return 0;
755 switch (variable)
757 case var_format:
758 if (add_format(strp) < 0)
760 logf("Format add fail: %s", data);
762 break;
764 case var_include:
765 if (get_token_str(data, sizeof(data)) < 0)
767 logf("%%include empty");
768 return 0;
771 if (!parse_menu(data))
773 logf("Load menu fail: %s", data);
775 break;
777 case var_menu_start:
778 if (menu_count >= TAGMENU_MAX_MENUS)
780 logf("max menucount reached");
781 return 0;
784 if (get_token_str(data, sizeof data) < 0)
786 logf("%%menu_start id empty");
787 return 0;
790 menu = NULL;
791 for (i = 0; i < menu_count; i++)
793 if (!strcasecmp(menus[i]->id, data))
795 menu = menus[i];
799 if (menu == NULL)
801 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
802 menu = menus[menu_count];
803 ++menu_count;
804 memset(menu, 0, sizeof(struct root_menu));
805 strncpy(menu->id, data, MAX_MENU_ID_SIZE-1);
808 if (get_token_str(menu->title, sizeof(menu->title)) < 0)
810 logf("%%menu_start title empty");
811 return 0;
813 logf("menu: %s", menu->title);
814 read_menu = true;
815 break;
817 case var_rootmenu:
818 /* Only set root menu once. */
819 if (root_menu >= 0)
820 break;
822 if (get_token_str(data, sizeof(data)) < 0)
824 logf("%%root_menu empty");
825 return 0;
828 for (i = 0; i < menu_count; i++)
830 if (!strcasecmp(menus[i]->id, data))
832 root_menu = i;
835 break;
838 return 0;
841 if (menu->itemcount >= TAGMENU_MAX_ITEMS)
843 logf("max itemcount reached");
844 return 0;
847 /* Allocate */
848 if (menu->items[menu->itemcount] == NULL)
850 menu->items[menu->itemcount] = buffer_alloc(sizeof(struct menu_entry));
851 memset(menu->items[menu->itemcount], 0, sizeof(struct menu_entry));
852 menu->items[menu->itemcount]->si = buffer_alloc(sizeof(struct search_instruction));
855 memset(menu->items[menu->itemcount]->si, 0, sizeof(struct search_instruction));
856 if (!parse_search(menu->items[menu->itemcount], buf))
857 return 0;
859 menu->itemcount++;
861 return 0;
864 static bool parse_menu(const char *filename)
866 int fd;
867 char buf[1024];
869 if (menu_count >= TAGMENU_MAX_MENUS)
871 logf("max menucount reached");
872 return false;
875 fd = open(filename, O_RDONLY);
876 if (fd < 0)
878 logf("Search instruction file not found.");
879 return false;
882 /* Now read file for real, parsing into si */
883 fast_readline(fd, buf, sizeof buf, NULL, parse_line);
884 close(fd);
886 return true;
889 void tagtree_init(void)
891 format_count = 0;
892 menu_count = 0;
893 menu = NULL;
894 root_menu = -1;
895 parse_menu(FILE_SEARCH_INSTRUCTIONS);
897 /* If no root menu is set, assume it's the first single menu
898 * we have. That shouldn't normally happen. */
899 if (root_menu < 0)
900 root_menu = 0;
902 uniqbuf = buffer_alloc(UNIQBUF_SIZE);
903 audio_set_track_buffer_event(tagtree_buffer_event);
904 audio_set_track_unbuffer_event(tagtree_unbuffer_event);
907 static bool show_search_progress(bool init, int count)
909 static int last_tick = 0;
911 if (init)
913 last_tick = current_tick;
914 return true;
917 if (current_tick - last_tick > HZ/4)
919 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG), count,
920 str(LANG_OFF_ABORT));
921 if (action_userabort(TIMEOUT_NOBLOCK))
922 return false;
923 last_tick = current_tick;
924 yield();
927 return true;
930 static int format_str(struct tagcache_search *tcs, struct display_format *fmt,
931 char *buf, int buf_size)
933 char fmtbuf[8];
934 bool read_format = false;
935 int fmtbuf_pos = 0;
936 int parpos = 0;
937 int buf_pos = 0;
938 int i;
940 memset(buf, 0, buf_size);
941 for (i = 0; fmt->formatstr[i] != '\0'; i++)
943 if (fmt->formatstr[i] == '%')
945 read_format = true;
946 fmtbuf_pos = 0;
947 if (parpos >= fmt->tag_count)
949 logf("too many format tags");
950 return -1;
954 if (read_format)
956 fmtbuf[fmtbuf_pos++] = fmt->formatstr[i];
957 if (fmtbuf_pos >= buf_size)
959 logf("format parse error");
960 return -2;
963 if (fmt->formatstr[i] == 's')
965 fmtbuf[fmtbuf_pos] = '\0';
966 read_format = false;
967 if (fmt->tags[parpos] == tcs->type)
969 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf, tcs->result);
971 else
973 /* Need to fetch the tag data. */
974 if (!tagcache_retrieve(tcs, tcs->idx_id, fmt->tags[parpos],
975 &buf[buf_pos], buf_size - buf_pos))
977 logf("retrieve failed");
978 return -3;
981 buf_pos += strlen(&buf[buf_pos]);
982 parpos++;
984 else if (fmt->formatstr[i] == 'd')
986 fmtbuf[fmtbuf_pos] = '\0';
987 read_format = false;
988 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf,
989 tagcache_get_numeric(tcs, fmt->tags[parpos]));
990 buf_pos += strlen(&buf[buf_pos]);
991 parpos++;
993 continue;
996 buf[buf_pos++] = fmt->formatstr[i];
998 if (buf_pos - 1 >= buf_size)
1000 logf("buffer overflow");
1001 return -4;
1005 buf[buf_pos++] = '\0';
1007 return 0;
1010 static int retrieve_entries(struct tree_context *c, struct tagcache_search *tcs,
1011 int offset, bool init)
1013 struct tagentry *dptr = (struct tagentry *)c->dircache;
1014 struct display_format *fmt;
1015 int i;
1016 int namebufused = 0;
1017 int total_count = 0;
1018 int special_entry_count = 0;
1019 int level = c->currextra;
1020 int tag;
1021 bool sort = false;
1022 int sort_limit;
1023 int strip;
1025 if (init
1026 #ifdef HAVE_TC_RAMCACHE
1027 && !tagcache_is_ramcache()
1028 #endif
1031 show_search_progress(true, 0);
1032 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG),
1033 0, csi->name);
1036 if (c->currtable == allsubentries)
1038 tag = tag_title;
1039 level--;
1041 else
1042 tag = csi->tagorder[level];
1044 if (!tagcache_search(tcs, tag))
1045 return -1;
1047 /* Prevent duplicate entries in the search list. */
1048 tagcache_search_set_uniqbuf(tcs, uniqbuf, UNIQBUF_SIZE);
1050 if (level || csi->clause_count[0] || tagcache_is_numeric_tag(tag))
1051 sort = true;
1053 for (i = 0; i < level; i++)
1055 if (tagcache_is_numeric_tag(csi->tagorder[i]))
1057 static struct tagcache_search_clause cc;
1059 memset(&cc, 0, sizeof(struct tagcache_search_clause));
1060 cc.tag = csi->tagorder[i];
1061 cc.type = clause_is;
1062 cc.numeric = true;
1063 cc.numeric_data = csi->result_seek[i];
1064 tagcache_search_add_clause(tcs, &cc);
1066 else
1068 tagcache_search_add_filter(tcs, csi->tagorder[i],
1069 csi->result_seek[i]);
1073 for (i = 0; i <= level; i++)
1075 int j;
1077 for (j = 0; j < csi->clause_count[i]; j++)
1078 tagcache_search_add_clause(tcs, csi->clause[i][j]);
1081 current_offset = offset;
1082 current_entry_count = 0;
1083 c->dirfull = false;
1085 fmt = NULL;
1086 for (i = 0; i < format_count; i++)
1088 if (formats[i]->group_id == csi->format_id[level])
1089 fmt = formats[i];
1092 if (fmt)
1094 sort_inverse = fmt->sort_inverse;
1095 sort_limit = fmt->limit;
1096 strip = fmt->strip;
1098 /* Check if sorting is forced. */
1099 if (fmt->sort)
1100 sort = true;
1102 else
1104 sort_inverse = false;
1105 sort_limit = 0;
1106 strip = 0;
1109 if (tag != tag_title && tag != tag_filename)
1111 if (offset == 0)
1113 dptr->newtable = allsubentries;
1114 dptr->name = str(LANG_TAGNAVI_ALL_TRACKS);
1115 dptr++;
1116 current_entry_count++;
1118 special_entry_count++;
1121 total_count += special_entry_count;
1123 while (tagcache_get_next(tcs))
1125 if (total_count++ < offset)
1126 continue;
1128 dptr->newtable = navibrowse;
1129 if (tag == tag_title || tag == tag_filename)
1131 dptr->newtable = playtrack;
1132 dptr->extraseek = tcs->idx_id;
1134 else
1135 dptr->extraseek = tcs->result_seek;
1137 fmt = NULL;
1138 /* Check the format */
1139 for (i = 0; i < format_count; i++)
1141 if (formats[i]->group_id != csi->format_id[level])
1142 continue;
1144 if (tagcache_check_clauses(tcs, formats[i]->clause,
1145 formats[i]->clause_count))
1147 fmt = formats[i];
1148 break;
1152 if (!tcs->ramresult || fmt)
1154 char buf[MAX_PATH];
1156 if (fmt)
1158 if (format_str(tcs, fmt, buf, sizeof buf) < 0)
1160 logf("format_str() failed");
1161 return 0;
1165 dptr->name = &c->name_buffer[namebufused];
1166 if (fmt)
1167 namebufused += strlen(buf)+1;
1168 else
1169 namebufused += tcs->result_len;
1171 if (namebufused >= c->name_buffer_size)
1173 logf("chunk mode #2: %d", current_entry_count);
1174 c->dirfull = true;
1175 sort = false;
1176 break ;
1178 if (fmt)
1179 strcpy(dptr->name, buf);
1180 else
1181 strcpy(dptr->name, tcs->result);
1183 else
1184 dptr->name = tcs->result;
1186 dptr++;
1187 current_entry_count++;
1189 if (current_entry_count >= global_settings.max_files_in_dir)
1191 logf("chunk mode #3: %d", current_entry_count);
1192 c->dirfull = true;
1193 sort = false;
1194 break ;
1197 if (init && !tcs->ramsearch)
1199 if (!show_search_progress(false, i))
1201 tagcache_search_finish(tcs);
1202 return current_entry_count;
1207 if (sort)
1208 qsort(c->dircache + special_entry_count * c->dentry_size,
1209 current_entry_count - special_entry_count,
1210 c->dentry_size, compare);
1212 if (!init)
1214 tagcache_search_finish(tcs);
1215 return current_entry_count;
1218 while (tagcache_get_next(tcs))
1220 if (!tcs->ramsearch)
1222 if (!show_search_progress(false, total_count))
1223 break;
1225 total_count++;
1228 tagcache_search_finish(tcs);
1230 if (!sort && (sort_inverse || sort_limit))
1232 gui_syncsplash(HZ*4, ID2P(LANG_SHOWDIR_BUFFER_FULL), total_count);
1233 logf("Too small dir buffer");
1234 return 0;
1237 if (sort_limit)
1238 total_count = MIN(total_count, sort_limit);
1240 if (strip)
1242 dptr = c->dircache;
1243 for (i = 0; i < total_count; i++, dptr++)
1245 int len = strlen(dptr->name);
1247 if (len < strip)
1248 continue;
1250 dptr->name = &dptr->name[strip];
1254 return total_count;
1257 static int load_root(struct tree_context *c)
1259 struct tagentry *dptr = (struct tagentry *)c->dircache;
1260 int i;
1262 tc = c;
1263 c->currtable = root;
1264 if (c->dirlevel == 0)
1265 c->currextra = root_menu;
1267 menu = menus[c->currextra];
1268 if (menu == NULL)
1269 return 0;
1271 for (i = 0; i < menu->itemcount; i++)
1273 dptr->name = menu->items[i]->name;
1274 switch (menu->items[i]->type)
1276 case menu_next:
1277 dptr->newtable = navibrowse;
1278 dptr->extraseek = i;
1279 break;
1281 case menu_load:
1282 dptr->newtable = root;
1283 dptr->extraseek = menu->items[i]->link;
1284 break;
1287 dptr++;
1290 current_offset = 0;
1291 current_entry_count = i;
1293 return i;
1296 int tagtree_load(struct tree_context* c)
1298 int count;
1299 int table = c->currtable;
1301 c->dentry_size = sizeof(struct tagentry);
1302 c->dirsindir = 0;
1304 if (!table)
1306 c->dirfull = false;
1307 table = root;
1308 c->currtable = table;
1309 c->currextra = root_menu;
1312 switch (table)
1314 case root:
1315 count = load_root(c);
1316 break;
1318 case allsubentries:
1319 case navibrowse:
1320 logf("navibrowse...");
1321 cpu_boost(true);
1322 count = retrieve_entries(c, &tcs, 0, true);
1323 cpu_boost(false);
1324 break;
1326 default:
1327 logf("Unsupported table %d\n", table);
1328 return -1;
1331 if (count < 0)
1333 c->dirlevel = 0;
1334 count = load_root(c);
1335 gui_syncsplash(HZ, str(LANG_TAGCACHE_BUSY));
1338 /* The _total_ numer of entries available. */
1339 c->dirlength = c->filesindir = count;
1341 return count;
1344 int tagtree_enter(struct tree_context* c)
1346 int rc = 0;
1347 struct tagentry *dptr;
1348 int newextra;
1349 int seek;
1351 dptr = tagtree_get_entry(c, c->selected_item);
1353 c->dirfull = false;
1354 newextra = dptr->newtable;
1355 seek = dptr->extraseek;
1357 if (c->dirlevel >= MAX_DIR_LEVELS)
1358 return 0;
1360 c->selected_item_history[c->dirlevel]=c->selected_item;
1361 c->table_history[c->dirlevel] = c->currtable;
1362 c->extra_history[c->dirlevel] = c->currextra;
1363 c->pos_history[c->dirlevel] = c->firstpos;
1364 c->dirlevel++;
1366 switch (c->currtable) {
1367 case root:
1368 c->currextra = newextra;
1370 if (newextra == root)
1372 menu = menus[seek];
1373 c->currextra = seek;
1376 else if (newextra == navibrowse)
1378 int i, j;
1380 csi = menu->items[seek]->si;
1381 c->currextra = 0;
1383 strncpy(current_title[c->currextra], dptr->name,
1384 sizeof(current_title[0]) - 1);
1386 /* Read input as necessary. */
1387 for (i = 0; i < csi->tagorder_count; i++)
1389 for (j = 0; j < csi->clause_count[i]; j++)
1391 if (!csi->clause[i][j]->input)
1392 continue;
1394 rc = kbd_input(searchstring, sizeof(searchstring));
1395 if (rc == -1 || !searchstring[0])
1397 tagtree_exit(c);
1398 return 0;
1401 if (csi->clause[i][j]->numeric)
1402 csi->clause[i][j]->numeric_data = atoi(searchstring);
1403 else
1404 csi->clause[i][j]->str = searchstring;
1408 c->currtable = newextra;
1410 break;
1412 case navibrowse:
1413 case allsubentries:
1414 if (newextra == playtrack)
1416 c->dirlevel--;
1417 /* about to create a new current playlist...
1418 allow user to cancel the operation */
1419 if (global_settings.warnon_erase_dynplaylist &&
1420 !global_settings.party_mode &&
1421 playlist_modified(NULL))
1423 char *lines[]={ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
1424 struct text_message message={lines, 1};
1426 if (gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
1427 break;
1430 if (tagtree_play_folder(c) >= 0)
1431 rc = 2;
1432 break;
1435 c->currtable = newextra;
1436 csi->result_seek[c->currextra] = seek;
1437 if (c->currextra < csi->tagorder_count-1)
1438 c->currextra++;
1439 else
1440 c->dirlevel--;
1442 /* Update the statusbar title */
1443 strncpy(current_title[c->currextra], dptr->name,
1444 sizeof(current_title[0]) - 1);
1445 break;
1447 default:
1448 c->dirlevel--;
1449 break;
1452 c->selected_item=0;
1453 gui_synclist_select_item(&tree_lists, c->selected_item);
1455 return rc;
1458 void tagtree_exit(struct tree_context* c)
1460 c->dirfull = false;
1461 if (c->dirlevel > 0)
1462 c->dirlevel--;
1463 c->selected_item=c->selected_item_history[c->dirlevel];
1464 gui_synclist_select_item(&tree_lists, c->selected_item);
1465 c->currtable = c->table_history[c->dirlevel];
1466 c->currextra = c->extra_history[c->dirlevel];
1467 c->firstpos = c->pos_history[c->dirlevel];
1470 int tagtree_get_filename(struct tree_context* c, char *buf, int buflen)
1472 struct tagentry *entry;
1474 entry = tagtree_get_entry(c, c->selected_item);
1476 if (!tagcache_search(&tcs, tag_filename))
1477 return -1;
1479 if (!tagcache_retrieve(&tcs, entry->extraseek, tcs.type, buf, buflen))
1481 tagcache_search_finish(&tcs);
1482 return -2;
1485 tagcache_search_finish(&tcs);
1487 return 0;
1490 static bool insert_all_playlist(struct tree_context *c, int position, bool queue)
1492 int i;
1493 char buf[MAX_PATH];
1494 int from, to, direction;
1495 int files_left = c->filesindir;
1497 cpu_boost(true);
1498 if (!tagcache_search(&tcs, tag_filename))
1500 gui_syncsplash(HZ, ID2P(LANG_TAGCACHE_BUSY));
1501 cpu_boost(false);
1502 return false;
1505 if (position == PLAYLIST_REPLACE)
1507 if (remove_all_tracks(NULL) == 0)
1508 position = PLAYLIST_INSERT_LAST;
1509 else return -1; }
1511 if (position == PLAYLIST_INSERT_FIRST)
1513 from = c->filesindir - 1;
1514 to = -1;
1515 direction = -1;
1517 else
1519 from = 0;
1520 to = c->filesindir;
1521 direction = 1;
1524 for (i = from; i != to; i += direction)
1526 /* Count back to zero */
1527 if (!show_search_progress(false, files_left--))
1528 break;
1530 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, i)->extraseek,
1531 tcs.type, buf, sizeof buf))
1533 continue;
1536 if (playlist_insert_track(NULL, buf, position, queue, false) < 0)
1538 logf("playlist_insert_track failed");
1539 break;
1541 yield();
1543 playlist_sync(NULL);
1544 tagcache_search_finish(&tcs);
1545 cpu_boost(false);
1547 return true;
1550 bool tagtree_insert_selection_playlist(int position, bool queue)
1552 struct tagentry *dptr;
1553 char buf[MAX_PATH];
1554 int dirlevel = tc->dirlevel;
1556 /* We need to set the table to allsubentries. */
1557 show_search_progress(true, 0);
1559 dptr = tagtree_get_entry(tc, tc->selected_item);
1561 /* Insert a single track? */
1562 if (dptr->newtable == playtrack)
1564 if (tagtree_get_filename(tc, buf, sizeof buf) < 0)
1566 logf("tagtree_get_filename failed");
1567 return false;
1569 playlist_insert_track(NULL, buf, position, queue, true);
1571 return true;
1574 if (dptr->newtable == navibrowse)
1576 tagtree_enter(tc);
1577 tagtree_load(tc);
1578 dptr = tagtree_get_entry(tc, tc->selected_item);
1580 else if (dptr->newtable != allsubentries)
1582 logf("unsupported table: %d", dptr->newtable);
1583 return false;
1586 /* Now the current table should be allsubentries. */
1587 if (dptr->newtable != playtrack)
1589 tagtree_enter(tc);
1590 tagtree_load(tc);
1591 dptr = tagtree_get_entry(tc, tc->selected_item);
1593 /* And now the newtable should be playtrack. */
1594 if (dptr->newtable != playtrack)
1596 logf("newtable: %d !!", dptr->newtable);
1597 tc->dirlevel = dirlevel;
1598 return false;
1602 if (tc->filesindir <= 0)
1603 gui_syncsplash(HZ, ID2P(LANG_END_PLAYLIST));
1604 else
1606 logf("insert_all_playlist");
1607 if (!insert_all_playlist(tc, position, queue))
1608 gui_syncsplash(HZ*2, ID2P(LANG_FAILED));
1611 /* Finally return the dirlevel to its original value. */
1612 while (tc->dirlevel > dirlevel)
1613 tagtree_exit(tc);
1614 tagtree_load(tc);
1616 return true;
1619 static int tagtree_play_folder(struct tree_context* c)
1621 if (playlist_create(NULL, NULL) < 0)
1623 logf("Failed creating playlist\n");
1624 return -1;
1627 if (!insert_all_playlist(c, PLAYLIST_INSERT_LAST, false))
1628 return -2;
1630 if (global_settings.playlist_shuffle)
1631 c->selected_item = playlist_shuffle(current_tick, c->selected_item);
1632 if (!global_settings.play_selected)
1633 c->selected_item = 0;
1634 gui_synclist_select_item(&tree_lists, c->selected_item);
1636 playlist_start(c->selected_item,0);
1638 return 0;
1641 struct tagentry* tagtree_get_entry(struct tree_context *c, int id)
1643 struct tagentry *entry = (struct tagentry *)c->dircache;
1644 int realid = id - current_offset;
1646 /* Load the next chunk if necessary. */
1647 if (realid >= current_entry_count || realid < 0)
1649 cpu_boost(true);
1650 if (retrieve_entries(c, &tcs2, MAX(0, id - (current_entry_count / 2)),
1651 false) < 0)
1653 logf("retrieve failed");
1654 cpu_boost(false);
1655 return NULL;
1657 realid = id - current_offset;
1658 cpu_boost(false);
1661 return &entry[realid];
1664 char *tagtree_get_title(struct tree_context* c)
1666 switch (c->currtable)
1668 case root:
1669 return menu->title;
1671 case navibrowse:
1672 case allsubentries:
1673 return current_title[c->currextra];
1676 return "?";
1679 int tagtree_get_attr(struct tree_context* c)
1681 int attr = -1;
1682 switch (c->currtable)
1684 case navibrowse:
1685 if (csi->tagorder[c->currextra] == tag_title)
1686 attr = FILE_ATTR_AUDIO;
1687 else
1688 attr = ATTR_DIRECTORY;
1689 break;
1691 case allsubentries:
1692 attr = FILE_ATTR_AUDIO;
1693 break;
1695 default:
1696 attr = ATTR_DIRECTORY;
1697 break;
1700 return attr;
1703 int tagtree_get_icon(struct tree_context* c)
1705 int icon = Icon_Folder;
1707 if (tagtree_get_attr(c) == FILE_ATTR_AUDIO)
1708 icon = Icon_Audio;
1710 return icon;