Accept FS#7228 by Dagni McPhee enable pitchscreen on sansa
[Rockbox.git] / apps / tagtree.c
blob405274c6bda865557387b4bfbbf1ac1d36f8c2f4
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
73 static struct tagcache_search tcs, tcs2;
74 static bool sort_inverse;
77 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
79 * valid = true
80 * formatstr = "%-3d. %s"
81 * tags[0] = tag_autoscore
82 * tags[1] = tag_title
83 * tag_count = 2
85 * limit = 100
86 * sort_inverse = true
88 struct display_format {
89 char name[32];
90 struct tagcache_search_clause *clause[TAGCACHE_MAX_CLAUSES];
91 int clause_count;
92 char *formatstr;
93 int group_id;
94 int tags[MAX_TAGS];
95 int tag_count;
97 int limit;
98 int strip;
99 bool sort_inverse;
100 bool sort;
103 static struct display_format *formats[TAGMENU_MAX_FMTS];
104 static int format_count;
106 struct search_instruction {
107 char name[64];
108 int tagorder[MAX_TAGS];
109 int tagorder_count;
110 struct tagcache_search_clause *clause[MAX_TAGS][TAGCACHE_MAX_CLAUSES];
111 int format_id[MAX_TAGS];
112 int clause_count[MAX_TAGS];
113 int result_seek[MAX_TAGS];
116 struct menu_entry {
117 char name[64];
118 int type;
119 struct search_instruction *si;
120 int link;
123 struct root_menu {
124 char title[64];
125 char id[32];
126 int itemcount;
127 struct root_menu *parent;
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, "genre", tag_genre);
204 MATCH(tag, buf, "length", tag_length);
205 MATCH(tag, buf, "Lm", tag_virt_length_min);
206 MATCH(tag, buf, "Ls", tag_virt_length_sec);
207 MATCH(tag, buf, "Pm", tag_virt_playtime_min);
208 MATCH(tag, buf, "Ps", tag_virt_playtime_sec);
209 MATCH(tag, buf, "title", tag_title);
210 MATCH(tag, buf, "filename", tag_filename);
211 MATCH(tag, buf, "tracknum", tag_tracknumber);
212 MATCH(tag, buf, "year", tag_year);
213 MATCH(tag, buf, "playcount", tag_playcount);
214 MATCH(tag, buf, "rating", tag_rating);
215 MATCH(tag, buf, "lastplayed", tag_lastplayed);
216 MATCH(tag, buf, "commitid", tag_commitid);
217 MATCH(tag, buf, "entryage", tag_virt_entryage);
218 MATCH(tag, buf, "autoscore", tag_virt_autoscore);
219 MATCH(tag, buf, "%sort", var_sorttype);
220 MATCH(tag, buf, "%limit", var_limit);
221 MATCH(tag, buf, "%strip", var_strip);
222 MATCH(tag, buf, "%menu_start", var_menu_start);
223 MATCH(tag, buf, "%include", var_include);
224 MATCH(tag, buf, "%root_menu", var_rootmenu);
225 MATCH(tag, buf, "%format", var_format);
226 MATCH(tag, buf, "->", menu_next);
227 MATCH(tag, buf, "==>", menu_load);
229 logf("NO MATCH: %s\n", buf);
230 if (buf[0] == '?')
231 return 0;
233 return -1;
236 static int get_clause(int *condition)
238 char buf[4];
239 int i;
241 /* Find the start. */
242 while (*strp == ' ' && *strp != '\0')
243 strp++;
245 if (*strp == '\0')
246 return 0;
248 for (i = 0; i < (int)sizeof(buf)-1; i++)
250 if (*strp == '\0' || *strp == ' ')
251 break ;
252 buf[i] = *strp;
253 strp++;
255 buf[i] = '\0';
257 MATCH(condition, buf, "=", clause_is);
258 MATCH(condition, buf, "==", clause_is);
259 MATCH(condition, buf, "!=", clause_is_not);
260 MATCH(condition, buf, ">", clause_gt);
261 MATCH(condition, buf, ">=", clause_gteq);
262 MATCH(condition, buf, "<", clause_lt);
263 MATCH(condition, buf, "<=", clause_lteq);
264 MATCH(condition, buf, "~", clause_contains);
265 MATCH(condition, buf, "!~", clause_not_contains);
266 MATCH(condition, buf, "^", clause_begins_with);
267 MATCH(condition, buf, "!^", clause_not_begins_with);
268 MATCH(condition, buf, "$", clause_ends_with);
269 MATCH(condition, buf, "!$", clause_not_ends_with);
270 MATCH(condition, buf, "@", clause_oneof);
272 return 0;
275 static bool read_clause(struct tagcache_search_clause *clause)
277 char buf[256];
279 if (get_tag(&clause->tag) <= 0)
280 return false;
282 if (get_clause(&clause->type) <= 0)
283 return false;
285 if (get_token_str(buf, sizeof buf) < 0)
286 return false;
288 clause->str = buffer_alloc(strlen(buf)+1);
289 strcpy(clause->str, buf);
291 logf("got clause: %d/%d [%s]", clause->tag, clause->type, clause->str);
293 if (*(clause->str) == '\0')
294 clause->input = true;
295 else
296 clause->input = false;
298 if (tagcache_is_numeric_tag(clause->tag))
300 clause->numeric = true;
301 clause->numeric_data = atoi(clause->str);
303 else
304 clause->numeric = false;
306 return true;
309 static bool read_variable(char *buf, int size)
311 int condition;
313 if (!get_clause(&condition))
314 return false;
316 if (condition != clause_is)
317 return false;
319 if (get_token_str(buf, size) < 0)
320 return false;
322 return true;
325 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
326 static int get_format_str(struct display_format *fmt)
328 int ret;
329 char buf[128];
330 int i;
332 memset(fmt, 0, sizeof(struct display_format));
334 if (get_token_str(fmt->name, sizeof fmt->name) < 0)
335 return -10;
337 /* Determine the group id */
338 fmt->group_id = 0;
339 for (i = 0; i < format_count; i++)
341 if (!strcasecmp(formats[i]->name, fmt->name))
343 fmt->group_id = formats[i]->group_id;
344 break;
347 if (formats[i]->group_id > fmt->group_id)
348 fmt->group_id = formats[i]->group_id;
351 if (i == format_count)
352 fmt->group_id++;
354 logf("format: (%d) %s", fmt->group_id, fmt->name);
356 if (get_token_str(buf, sizeof buf) < 0)
357 return -10;
359 fmt->formatstr = buffer_alloc(strlen(buf) + 1);
360 strcpy(fmt->formatstr, buf);
362 while (fmt->tag_count < MAX_TAGS)
364 ret = get_tag(&fmt->tags[fmt->tag_count]);
365 if (ret < 0)
366 return -11;
368 if (ret == 0)
369 break;
371 switch (fmt->tags[fmt->tag_count]) {
372 case var_sorttype:
373 if (!read_variable(buf, sizeof buf))
374 return -12;
375 if (!strcasecmp("inverse", buf))
376 fmt->sort_inverse = true;
378 fmt->sort = true;
379 break;
381 case var_limit:
382 if (!read_variable(buf, sizeof buf))
383 return -13;
384 fmt->limit = atoi(buf);
385 break;
387 case var_strip:
388 if (!read_variable(buf, sizeof buf))
389 return -14;
390 fmt->strip = atoi(buf);
391 break;
393 default:
394 fmt->tag_count++;
398 return 1;
401 static int add_format(const char *buf)
403 strp = buf;
405 if (formats[format_count] == NULL)
406 formats[format_count] = buffer_alloc(sizeof(struct display_format));
408 memset(formats[format_count], 0, sizeof(struct display_format));
409 if (get_format_str(formats[format_count]) < 0)
411 logf("get_format_str() parser failed!");
412 return -4;
415 while (*strp != '\0' && *strp != '?')
416 strp++;
418 if (*strp == '?')
420 int clause_count = 0;
421 strp++;
423 while (1)
425 if (clause_count >= TAGCACHE_MAX_CLAUSES)
427 logf("too many clauses");
428 break;
431 formats[format_count]->clause[clause_count] =
432 buffer_alloc(sizeof(struct tagcache_search_clause));
434 if (!read_clause(formats[format_count]->clause[clause_count]))
435 break;
437 clause_count++;
440 formats[format_count]->clause_count = clause_count;
443 format_count++;
445 return 1;
448 static int get_condition(struct search_instruction *inst)
450 int clause_count;
451 char buf[128];
453 switch (*strp)
455 case '=':
457 int i;
459 if (get_token_str(buf, sizeof buf) < 0)
460 return -1;
462 for (i = 0; i < format_count; i++)
464 if (!strcasecmp(formats[i]->name, buf))
465 break;
468 if (i == format_count)
470 logf("format not found: %s", buf);
471 return -2;
474 inst->format_id[inst->tagorder_count] = formats[i]->group_id;
475 return 1;
477 case '?':
478 case ' ':
479 case '&':
480 strp++;
481 return 1;
482 case '-':
483 case '\0':
484 return 0;
487 clause_count = inst->clause_count[inst->tagorder_count];
488 if (clause_count >= TAGCACHE_MAX_CLAUSES)
490 logf("Too many clauses");
491 return false;
494 inst->clause[inst->tagorder_count][clause_count] =
495 buffer_alloc(sizeof(struct tagcache_search_clause));
497 if (!read_clause(inst->clause[inst->tagorder_count][clause_count]))
498 return -1;
500 inst->clause_count[inst->tagorder_count]++;
502 return 1;
505 /* example search:
506 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
507 * : album ? year >= "2000" : songs
508 * ^ begins with
509 * * contains
510 * $ ends with
513 static bool parse_search(struct menu_entry *entry, const char *str)
515 int ret;
516 int type;
517 struct search_instruction *inst = entry->si;
518 char buf[MAX_PATH];
519 int i;
521 strp = str;
523 /* Parse entry name */
524 if (get_token_str(entry->name, sizeof entry->name) < 0)
526 logf("No name found.");
527 return false;
530 /* Parse entry type */
531 if (get_tag(&entry->type) <= 0)
532 return false;
534 if (entry->type == menu_load)
536 if (get_token_str(buf, sizeof buf) < 0)
537 return false;
539 /* Find the matching root menu or "create" it */
540 for (i = 0; i < menu_count; i++)
542 if (!strcasecmp(menus[i]->id, buf))
544 entry->link = i;
545 menus[i]->parent = menu;
546 return true;
550 return false;
553 if (entry->type != menu_next)
554 return false;
556 while (inst->tagorder_count < MAX_TAGS)
558 ret = get_tag(&inst->tagorder[inst->tagorder_count]);
559 if (ret < 0)
561 logf("Parse error #1");
562 logf("%s", strp);
563 return false;
566 if (ret == 0)
567 break ;
569 logf("tag: %d", inst->tagorder[inst->tagorder_count]);
571 while ( (ret = get_condition(inst)) > 0 ) ;
572 if (ret < 0)
573 return false;
575 inst->tagorder_count++;
577 if (get_tag(&type) <= 0 || type != menu_next)
578 break;
581 return true;
584 static int compare(const void *p1, const void *p2)
586 struct tagentry *e1 = (struct tagentry *)p1;
587 struct tagentry *e2 = (struct tagentry *)p2;
589 if (sort_inverse)
590 return strncasecmp(e2->name, e1->name, MAX_PATH);
592 return strncasecmp(e1->name, e2->name, MAX_PATH);
595 static void tagtree_buffer_event(struct mp3entry *id3, bool last_track)
597 (void)id3;
598 (void)last_track;
600 /* Do not gather data unless proper setting has been enabled. */
601 if (!global_settings.runtimedb)
602 return;
604 logf("be:%d%s", last_track, id3->path);
606 if (!tagcache_find_index(&tcs, id3->path))
608 logf("tc stat: not found: %s", id3->path);
609 return;
612 id3->playcount = tagcache_get_numeric(&tcs, tag_playcount);
613 if(!id3->rating) id3->rating = tagcache_get_numeric(&tcs, tag_rating);
614 id3->lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
615 id3->score = tagcache_get_numeric(&tcs, tag_virt_autoscore) / 10;
617 tagcache_search_finish(&tcs);
620 static void tagtree_unbuffer_event(struct mp3entry *id3, bool last_track)
622 (void)last_track;
623 long playcount;
624 long rating;
625 long playtime;
626 long lastplayed;
628 /* Do not gather data unless proper setting has been enabled. */
629 if (!global_settings.runtimedb)
631 logf("runtimedb gathering not enabled");
632 return;
635 /* Don't process unplayed tracks. */
636 if (id3->elapsed == 0)
638 logf("not logging unplayed track");
639 return;
642 if (!tagcache_find_index(&tcs, id3->path))
644 logf("tc stat: not found: %s", id3->path);
645 return;
648 playcount = tagcache_get_numeric(&tcs, tag_playcount);
649 playtime = tagcache_get_numeric(&tcs, tag_playtime);
650 lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
652 playcount++;
654 rating = (long) id3->rating;
656 lastplayed = tagcache_increase_serial();
657 if (lastplayed < 0)
659 logf("incorrect tc serial:%ld", lastplayed);
660 tagcache_search_finish(&tcs);
661 return;
664 /* Ignore the last 15s (crossfade etc.) */
665 playtime += MIN(id3->length, id3->elapsed + 15 * 1000);
667 logf("ube:%s", id3->path);
668 logf("-> %d/%ld/%ld/%ld", last_track, playcount, rating, playtime);
669 logf("-> %ld/%ld/%ld", id3->elapsed, id3->length, MIN(id3->length, id3->elapsed + 15 * 1000));
671 /* lastplayed not yet supported. */
673 if (!tagcache_modify_numeric_entry(&tcs, tag_playcount, playcount)
674 || !tagcache_modify_numeric_entry(&tcs, tag_rating, rating)
675 || !tagcache_modify_numeric_entry(&tcs, tag_playtime, playtime)
676 || !tagcache_modify_numeric_entry(&tcs, tag_lastplayed, lastplayed))
678 logf("tc stat: modify failed!");
679 tagcache_search_finish(&tcs);
680 return;
683 tagcache_search_finish(&tcs);
686 bool tagtree_export(void)
688 gui_syncsplash(0, str(LANG_CREATING));
689 if (!tagcache_create_changelog(&tcs))
691 gui_syncsplash(HZ*2, str(LANG_FAILED));
694 return false;
697 bool tagtree_import(void)
699 gui_syncsplash(0, str(LANG_WAIT));
700 if (!tagcache_import_changelog())
702 gui_syncsplash(HZ*2, str(LANG_FAILED));
705 return false;
708 static bool parse_menu(const char *filename);
710 static int parse_line(int n, const char *buf, void *parameters)
712 char data[256];
713 int variable;
714 static bool read_menu;
715 int i;
717 (void)parameters;
719 logf("parse:%d/%s", n, buf);
721 /* First line, do initialisation. */
722 if (n == 0)
724 if (strcasecmp(TAGNAVI_VERSION, buf))
726 logf("Version mismatch");
727 return -1;
730 read_menu = false;
733 if (buf[0] == '#')
734 return 0;
736 if (buf[0] == '\0')
738 if (read_menu)
740 /* End the menu */
741 menu_count++;
742 read_menu = false;
744 return 0;
747 if (!read_menu)
749 strp = buf;
750 if (get_tag(&variable) <= 0)
751 return 0;
753 switch (variable)
755 case var_format:
756 if (add_format(strp) < 0)
758 logf("Format add fail: %s", data);
760 break;
762 case var_include:
763 if (get_token_str(data, sizeof(data)) < 0)
765 logf("%%include empty");
766 return 0;
769 if (!parse_menu(data))
771 logf("Load menu fail: %s", data);
773 break;
775 case var_menu_start:
776 if (menu_count >= TAGMENU_MAX_MENUS)
778 logf("max menucount reached");
779 return 0;
782 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
783 menu = menus[menu_count];
784 memset(menu, 0, sizeof(struct root_menu));
785 if (get_token_str(menu->id, sizeof(menu->id)) < 0)
787 logf("%%menu_start id empty");
788 return 0;
790 if (get_token_str(menu->title, sizeof(menu->title)) < 0)
792 logf("%%menu_start title empty");
793 return 0;
795 logf("menu: %s", menu->title);
796 menu->itemcount = 0;
797 read_menu = true;
798 break;
800 case var_rootmenu:
801 /* Only set root menu once. */
802 if (root_menu >= 0)
803 break;
805 if (get_token_str(data, sizeof(data)) < 0)
807 logf("%%root_menu empty");
808 return 0;
811 for (i = 0; i < menu_count; i++)
813 if (!strcasecmp(menus[i]->id, data))
815 root_menu = i;
818 break;
821 return 0;
824 if (menu->itemcount >= TAGMENU_MAX_ITEMS)
826 logf("max itemcount reached");
827 return 0;
830 /* Allocate */
831 if (menu->items[menu->itemcount] == NULL)
833 menu->items[menu->itemcount] = buffer_alloc(sizeof(struct menu_entry));
834 memset(menu->items[menu->itemcount], 0, sizeof(struct menu_entry));
835 menu->items[menu->itemcount]->si = buffer_alloc(sizeof(struct search_instruction));
838 memset(menu->items[menu->itemcount]->si, 0, sizeof(struct search_instruction));
839 if (!parse_search(menu->items[menu->itemcount], buf))
840 return 0;
842 menu->itemcount++;
844 return 0;
847 static bool parse_menu(const char *filename)
849 int fd;
850 char buf[1024];
852 if (menu_count >= TAGMENU_MAX_MENUS)
854 logf("max menucount reached");
855 return false;
858 fd = open(filename, O_RDONLY);
859 if (fd < 0)
861 logf("Search instruction file not found.");
862 return false;
865 /* Now read file for real, parsing into si */
866 fast_readline(fd, buf, sizeof buf, NULL, parse_line);
867 close(fd);
869 return true;
872 void tagtree_init(void)
874 format_count = 0;
875 menu_count = 0;
876 menu = NULL;
877 root_menu = -1;
878 parse_menu(FILE_SEARCH_INSTRUCTIONS);
880 /* If no root menu is set, assume it's the first single menu
881 * we have. That shouldn't normally happen. */
882 if (root_menu < 0)
883 root_menu = 0;
885 uniqbuf = buffer_alloc(UNIQBUF_SIZE);
886 audio_set_track_buffer_event(tagtree_buffer_event);
887 audio_set_track_unbuffer_event(tagtree_unbuffer_event);
890 static bool show_search_progress(bool init, int count)
892 static int last_tick = 0;
894 if (init)
896 last_tick = current_tick;
897 return true;
900 if (current_tick - last_tick > HZ/4)
902 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG), count,
903 #if CONFIG_KEYPAD == PLAYER_PAD
904 str(LANG_STOP_ABORT)
905 #else
906 str(LANG_OFF_ABORT)
907 #endif
909 if (action_userabort(TIMEOUT_NOBLOCK))
910 return false;
911 last_tick = current_tick;
912 yield();
915 return true;
918 static int format_str(struct tagcache_search *tcs, struct display_format *fmt,
919 char *buf, int buf_size)
921 char fmtbuf[8];
922 bool read_format = false;
923 int fmtbuf_pos = 0;
924 int parpos = 0;
925 int buf_pos = 0;
926 int i;
928 memset(buf, 0, buf_size);
929 for (i = 0; fmt->formatstr[i] != '\0'; i++)
931 if (fmt->formatstr[i] == '%')
933 read_format = true;
934 fmtbuf_pos = 0;
935 if (parpos >= fmt->tag_count)
937 logf("too many format tags");
938 return -1;
942 if (read_format)
944 fmtbuf[fmtbuf_pos++] = fmt->formatstr[i];
945 if (fmtbuf_pos >= buf_size)
947 logf("format parse error");
948 return -2;
951 if (fmt->formatstr[i] == 's')
953 fmtbuf[fmtbuf_pos] = '\0';
954 read_format = false;
955 if (fmt->tags[parpos] == tcs->type)
957 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf, tcs->result);
959 else
961 /* Need to fetch the tag data. */
962 if (!tagcache_retrieve(tcs, tcs->idx_id, fmt->tags[parpos],
963 &buf[buf_pos], buf_size - buf_pos))
965 logf("retrieve failed");
966 return -3;
969 buf_pos += strlen(&buf[buf_pos]);
970 parpos++;
972 else if (fmt->formatstr[i] == 'd')
974 fmtbuf[fmtbuf_pos] = '\0';
975 read_format = false;
976 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf,
977 tagcache_get_numeric(tcs, fmt->tags[parpos]));
978 buf_pos += strlen(&buf[buf_pos]);
979 parpos++;
981 continue;
984 buf[buf_pos++] = fmt->formatstr[i];
986 if (buf_pos - 1 >= buf_size)
988 logf("buffer overflow");
989 return -4;
993 buf[buf_pos++] = '\0';
995 return 0;
998 static int retrieve_entries(struct tree_context *c, struct tagcache_search *tcs,
999 int offset, bool init)
1001 struct tagentry *dptr = (struct tagentry *)c->dircache;
1002 struct display_format *fmt;
1003 int i;
1004 int namebufused = 0;
1005 int total_count = 0;
1006 int special_entry_count = 0;
1007 int level = c->currextra;
1008 int tag;
1009 bool sort = false;
1010 int sort_limit;
1011 int strip;
1013 if (init
1014 #ifdef HAVE_TC_RAMCACHE
1015 && !tagcache_is_ramcache()
1016 #endif
1019 show_search_progress(true, 0);
1020 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG),
1021 0, csi->name);
1024 if (c->currtable == allsubentries)
1026 tag = tag_title;
1027 level--;
1029 else
1030 tag = csi->tagorder[level];
1032 if (!tagcache_search(tcs, tag))
1033 return -1;
1035 /* Prevent duplicate entries in the search list. */
1036 tagcache_search_set_uniqbuf(tcs, uniqbuf, UNIQBUF_SIZE);
1038 if (level || csi->clause_count[0] || tagcache_is_numeric_tag(tag))
1039 sort = true;
1041 for (i = 0; i < level; i++)
1043 if (tagcache_is_numeric_tag(csi->tagorder[i]))
1045 static struct tagcache_search_clause cc;
1047 memset(&cc, 0, sizeof(struct tagcache_search_clause));
1048 cc.tag = csi->tagorder[i];
1049 cc.type = clause_is;
1050 cc.numeric = true;
1051 cc.numeric_data = csi->result_seek[i];
1052 tagcache_search_add_clause(tcs, &cc);
1054 else
1056 tagcache_search_add_filter(tcs, csi->tagorder[i],
1057 csi->result_seek[i]);
1061 for (i = 0; i <= level; i++)
1063 int j;
1065 for (j = 0; j < csi->clause_count[i]; j++)
1066 tagcache_search_add_clause(tcs, csi->clause[i][j]);
1069 current_offset = offset;
1070 current_entry_count = 0;
1071 c->dirfull = false;
1073 fmt = NULL;
1074 for (i = 0; i < format_count; i++)
1076 if (formats[i]->group_id == csi->format_id[level])
1077 fmt = formats[i];
1080 if (fmt)
1082 sort_inverse = fmt->sort_inverse;
1083 sort_limit = fmt->limit;
1084 strip = fmt->strip;
1086 /* Check if sorting is forced. */
1087 if (fmt->sort)
1088 sort = true;
1090 else
1092 sort_inverse = false;
1093 sort_limit = 0;
1094 strip = 0;
1097 if (tag != tag_title && tag != tag_filename)
1099 if (offset == 0)
1101 dptr->newtable = allsubentries;
1102 dptr->name = str(LANG_TAGNAVI_ALL_TRACKS);
1103 dptr++;
1104 current_entry_count++;
1106 special_entry_count++;
1109 total_count += special_entry_count;
1111 while (tagcache_get_next(tcs))
1113 if (total_count++ < offset)
1114 continue;
1116 dptr->newtable = navibrowse;
1117 if (tag == tag_title || tag == tag_filename)
1119 dptr->newtable = playtrack;
1120 dptr->extraseek = tcs->idx_id;
1122 else
1123 dptr->extraseek = tcs->result_seek;
1125 fmt = NULL;
1126 /* Check the format */
1127 for (i = 0; i < format_count; i++)
1129 if (formats[i]->group_id != csi->format_id[level])
1130 continue;
1132 if (tagcache_check_clauses(tcs, formats[i]->clause,
1133 formats[i]->clause_count))
1135 fmt = formats[i];
1136 break;
1140 if (!tcs->ramresult || fmt)
1142 char buf[MAX_PATH];
1144 if (fmt)
1146 if (format_str(tcs, fmt, buf, sizeof buf) < 0)
1148 logf("format_str() failed");
1149 return 0;
1153 dptr->name = &c->name_buffer[namebufused];
1154 if (fmt)
1155 namebufused += strlen(buf)+1;
1156 else
1157 namebufused += tcs->result_len;
1159 if (namebufused >= c->name_buffer_size)
1161 logf("chunk mode #2: %d", current_entry_count);
1162 c->dirfull = true;
1163 sort = false;
1164 break ;
1166 if (fmt)
1167 strcpy(dptr->name, buf);
1168 else
1169 strcpy(dptr->name, tcs->result);
1171 else
1172 dptr->name = tcs->result;
1174 dptr++;
1175 current_entry_count++;
1177 if (current_entry_count >= global_settings.max_files_in_dir)
1179 logf("chunk mode #3: %d", current_entry_count);
1180 c->dirfull = true;
1181 sort = false;
1182 break ;
1185 if (init && !tcs->ramsearch)
1187 if (!show_search_progress(false, i))
1189 tagcache_search_finish(tcs);
1190 return current_entry_count;
1195 if (sort)
1196 qsort(c->dircache + special_entry_count * c->dentry_size,
1197 current_entry_count - special_entry_count,
1198 c->dentry_size, compare);
1200 if (!init)
1202 tagcache_search_finish(tcs);
1203 return current_entry_count;
1206 while (tagcache_get_next(tcs))
1208 if (!tcs->ramsearch)
1210 if (!show_search_progress(false, total_count))
1211 break;
1213 total_count++;
1216 tagcache_search_finish(tcs);
1218 if (!sort && (sort_inverse || sort_limit))
1220 gui_syncsplash(HZ*4, str(LANG_SHOWDIR_BUFFER_FULL), total_count);
1221 logf("Too small dir buffer");
1222 return 0;
1225 if (sort_limit)
1226 total_count = MIN(total_count, sort_limit);
1228 if (strip)
1230 dptr = c->dircache;
1231 for (i = 0; i < total_count; i++, dptr++)
1233 int len = strlen(dptr->name);
1235 if (len < strip)
1236 continue;
1238 dptr->name = &dptr->name[strip];
1242 return total_count;
1245 static int load_root(struct tree_context *c)
1247 struct tagentry *dptr = (struct tagentry *)c->dircache;
1248 int i;
1250 tc = c;
1251 c->currtable = root;
1252 if (c->dirlevel == 0)
1253 c->currextra = root_menu;
1255 menu = menus[c->currextra];
1256 if (menu == NULL)
1257 return 0;
1259 for (i = 0; i < menu->itemcount; i++)
1261 dptr->name = menu->items[i]->name;
1262 switch (menu->items[i]->type)
1264 case menu_next:
1265 dptr->newtable = navibrowse;
1266 dptr->extraseek = i;
1267 break;
1269 case menu_load:
1270 dptr->newtable = root;
1271 dptr->extraseek = menu->items[i]->link;
1272 break;
1275 dptr++;
1278 current_offset = 0;
1279 current_entry_count = i;
1281 return i;
1284 int tagtree_load(struct tree_context* c)
1286 int count;
1287 int table = c->currtable;
1289 c->dentry_size = sizeof(struct tagentry);
1290 c->dirsindir = 0;
1292 if (!table)
1294 c->dirfull = false;
1295 table = root;
1296 c->currtable = table;
1297 c->currextra = root_menu;
1300 switch (table)
1302 case root:
1303 count = load_root(c);
1304 break;
1306 case allsubentries:
1307 case navibrowse:
1308 logf("navibrowse...");
1309 cpu_boost(true);
1310 count = retrieve_entries(c, &tcs, 0, true);
1311 cpu_boost(false);
1312 break;
1314 default:
1315 logf("Unsupported table %d\n", table);
1316 return -1;
1319 if (count < 0)
1321 c->dirlevel = 0;
1322 count = load_root(c);
1323 gui_syncsplash(HZ, str(LANG_TAGCACHE_BUSY));
1326 /* The _total_ numer of entries available. */
1327 c->dirlength = c->filesindir = count;
1329 return count;
1332 int tagtree_enter(struct tree_context* c)
1334 int rc = 0;
1335 struct tagentry *dptr;
1336 int newextra;
1337 int seek;
1339 dptr = tagtree_get_entry(c, c->selected_item);
1341 c->dirfull = false;
1342 newextra = dptr->newtable;
1343 seek = dptr->extraseek;
1345 if (c->dirlevel >= MAX_DIR_LEVELS)
1346 return 0;
1348 c->selected_item_history[c->dirlevel]=c->selected_item;
1349 c->table_history[c->dirlevel] = c->currtable;
1350 c->extra_history[c->dirlevel] = c->currextra;
1351 c->pos_history[c->dirlevel] = c->firstpos;
1352 c->dirlevel++;
1354 switch (c->currtable) {
1355 case root:
1356 c->currextra = newextra;
1358 if (newextra == root)
1360 menu = menus[seek];
1361 c->currextra = seek;
1364 else if (newextra == navibrowse)
1366 int i, j;
1368 csi = menu->items[seek]->si;
1369 c->currextra = 0;
1371 strncpy(current_title[c->currextra], dptr->name,
1372 sizeof(current_title[0]) - 1);
1374 /* Read input as necessary. */
1375 for (i = 0; i < csi->tagorder_count; i++)
1377 for (j = 0; j < csi->clause_count[i]; j++)
1379 if (!csi->clause[i][j]->input)
1380 continue;
1382 rc = kbd_input(searchstring, sizeof(searchstring));
1383 if (rc == -1 || !searchstring[0])
1385 tagtree_exit(c);
1386 return 0;
1389 if (csi->clause[i][j]->numeric)
1390 csi->clause[i][j]->numeric_data = atoi(searchstring);
1391 else
1392 csi->clause[i][j]->str = searchstring;
1396 c->currtable = newextra;
1398 break;
1400 case navibrowse:
1401 case allsubentries:
1402 if (newextra == playtrack)
1404 c->dirlevel--;
1405 /* about to create a new current playlist...
1406 allow user to cancel the operation */
1407 if (global_settings.warnon_erase_dynplaylist &&
1408 !global_settings.party_mode &&
1409 playlist_modified(NULL))
1411 char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
1412 struct text_message message={lines, 1};
1414 if (gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
1415 break;
1418 if (tagtree_play_folder(c) >= 0)
1419 rc = 2;
1420 break;
1423 c->currtable = newextra;
1424 csi->result_seek[c->currextra] = seek;
1425 if (c->currextra < csi->tagorder_count-1)
1426 c->currextra++;
1427 else
1428 c->dirlevel--;
1430 /* Update the statusbar title */
1431 strncpy(current_title[c->currextra], dptr->name,
1432 sizeof(current_title[0]) - 1);
1433 break;
1435 default:
1436 c->dirlevel--;
1437 break;
1440 c->selected_item=0;
1441 gui_synclist_select_item(&tree_lists, c->selected_item);
1443 return rc;
1446 void tagtree_exit(struct tree_context* c)
1448 c->dirfull = false;
1449 if (c->dirlevel > 0)
1450 c->dirlevel--;
1451 c->selected_item=c->selected_item_history[c->dirlevel];
1452 gui_synclist_select_item(&tree_lists, c->selected_item);
1453 c->currtable = c->table_history[c->dirlevel];
1454 c->currextra = c->extra_history[c->dirlevel];
1455 c->firstpos = c->pos_history[c->dirlevel];
1458 int tagtree_get_filename(struct tree_context* c, char *buf, int buflen)
1460 struct tagentry *entry;
1462 entry = tagtree_get_entry(c, c->selected_item);
1464 if (!tagcache_search(&tcs, tag_filename))
1465 return -1;
1467 if (!tagcache_retrieve(&tcs, entry->extraseek, tcs.type, buf, buflen))
1469 tagcache_search_finish(&tcs);
1470 return -2;
1473 tagcache_search_finish(&tcs);
1475 return 0;
1478 static bool insert_all_playlist(struct tree_context *c, int position, bool queue)
1480 int i;
1481 char buf[MAX_PATH];
1482 int from, to, direction;
1483 int files_left = c->filesindir;
1485 cpu_boost(true);
1486 if (!tagcache_search(&tcs, tag_filename))
1488 gui_syncsplash(HZ, str(LANG_TAGCACHE_BUSY));
1489 cpu_boost(false);
1490 return false;
1493 if (position == PLAYLIST_REPLACE)
1495 if (remove_all_tracks(NULL) == 0)
1496 position = PLAYLIST_INSERT_LAST;
1497 else return -1; }
1499 if (position == PLAYLIST_INSERT_FIRST)
1501 from = c->filesindir - 1;
1502 to = -1;
1503 direction = -1;
1505 else
1507 from = 0;
1508 to = c->filesindir;
1509 direction = 1;
1512 for (i = from; i != to; i += direction)
1514 /* Count back to zero */
1515 if (!show_search_progress(false, files_left--))
1516 break;
1518 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, i)->extraseek,
1519 tcs.type, buf, sizeof buf))
1521 continue;
1524 if (playlist_insert_track(NULL, buf, position, queue, false) < 0)
1526 logf("playlist_insert_track failed");
1527 break;
1529 yield();
1531 playlist_sync(NULL);
1532 tagcache_search_finish(&tcs);
1533 cpu_boost(false);
1535 return true;
1538 bool tagtree_insert_selection_playlist(int position, bool queue)
1540 struct tagentry *dptr;
1541 char buf[MAX_PATH];
1542 int dirlevel = tc->dirlevel;
1544 /* We need to set the table to allsubentries. */
1545 show_search_progress(true, 0);
1547 dptr = tagtree_get_entry(tc, tc->selected_item);
1549 /* Insert a single track? */
1550 if (dptr->newtable == playtrack)
1552 if (tagtree_get_filename(tc, buf, sizeof buf) < 0)
1554 logf("tagtree_get_filename failed");
1555 return false;
1557 playlist_insert_track(NULL, buf, position, queue, true);
1559 return true;
1562 if (dptr->newtable == navibrowse)
1564 tagtree_enter(tc);
1565 tagtree_load(tc);
1566 dptr = tagtree_get_entry(tc, tc->selected_item);
1568 else if (dptr->newtable != allsubentries)
1570 logf("unsupported table: %d", dptr->newtable);
1571 return false;
1574 /* Now the current table should be allsubentries. */
1575 if (dptr->newtable != playtrack)
1577 tagtree_enter(tc);
1578 tagtree_load(tc);
1579 dptr = tagtree_get_entry(tc, tc->selected_item);
1581 /* And now the newtable should be playtrack. */
1582 if (dptr->newtable != playtrack)
1584 logf("newtable: %d !!", dptr->newtable);
1585 tc->dirlevel = dirlevel;
1586 return false;
1590 if (tc->filesindir <= 0)
1591 gui_syncsplash(HZ, str(LANG_END_PLAYLIST_PLAYER));
1592 else
1594 logf("insert_all_playlist");
1595 if (!insert_all_playlist(tc, position, queue))
1596 gui_syncsplash(HZ*2, str(LANG_FAILED));
1599 /* Finally return the dirlevel to its original value. */
1600 while (tc->dirlevel > dirlevel)
1601 tagtree_exit(tc);
1602 tagtree_load(tc);
1604 return true;
1607 static int tagtree_play_folder(struct tree_context* c)
1609 if (playlist_create(NULL, NULL) < 0)
1611 logf("Failed creating playlist\n");
1612 return -1;
1615 if (!insert_all_playlist(c, PLAYLIST_INSERT_LAST, false))
1616 return -2;
1618 if (global_settings.playlist_shuffle)
1619 c->selected_item = playlist_shuffle(current_tick, c->selected_item);
1620 if (!global_settings.play_selected)
1621 c->selected_item = 0;
1622 gui_synclist_select_item(&tree_lists, c->selected_item);
1624 playlist_start(c->selected_item,0);
1626 return 0;
1629 struct tagentry* tagtree_get_entry(struct tree_context *c, int id)
1631 struct tagentry *entry = (struct tagentry *)c->dircache;
1632 int realid = id - current_offset;
1634 /* Load the next chunk if necessary. */
1635 if (realid >= current_entry_count || realid < 0)
1637 cpu_boost(true);
1638 if (retrieve_entries(c, &tcs2, MAX(0, id - (current_entry_count / 2)),
1639 false) < 0)
1641 logf("retrieve failed");
1642 cpu_boost(false);
1643 return NULL;
1645 realid = id - current_offset;
1646 cpu_boost(false);
1649 return &entry[realid];
1652 char *tagtree_get_title(struct tree_context* c)
1654 switch (c->currtable)
1656 case root:
1657 return menu->title;
1659 case navibrowse:
1660 case allsubentries:
1661 return current_title[c->currextra];
1664 return "?";
1667 int tagtree_get_attr(struct tree_context* c)
1669 int attr = -1;
1670 switch (c->currtable)
1672 case navibrowse:
1673 if (csi->tagorder[c->currextra] == tag_title)
1674 attr = FILE_ATTR_AUDIO;
1675 else
1676 attr = ATTR_DIRECTORY;
1677 break;
1679 case allsubentries:
1680 attr = FILE_ATTR_AUDIO;
1681 break;
1683 default:
1684 attr = ATTR_DIRECTORY;
1685 break;
1688 return attr;
1691 int tagtree_get_icon(struct tree_context* c)
1693 int icon = Icon_Folder;
1695 if (tagtree_get_attr(c) == FILE_ATTR_AUDIO)
1696 icon = Icon_Audio;
1698 return icon;