Player: Save a bit of space by only using 7 bytes/char in the glyph table.
[Rockbox.git] / apps / tagtree.c
blob040aea35d1ae6c29a9391dc800aee0a8da40f578
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, "rating", tag_rating);
209 MATCH(tag, buf, "lastplayed", tag_lastplayed);
210 MATCH(tag, buf, "commitid", tag_commitid);
211 MATCH(tag, buf, "entryage", tag_virt_entryage);
212 MATCH(tag, buf, "autoscore", tag_virt_autoscore);
213 MATCH(tag, buf, "%sort", var_sorttype);
214 MATCH(tag, buf, "%limit", var_limit);
215 MATCH(tag, buf, "%strip", var_strip);
216 MATCH(tag, buf, "%menu_start", var_menu_start);
217 MATCH(tag, buf, "%include", var_include);
218 MATCH(tag, buf, "%root_menu", var_rootmenu);
219 MATCH(tag, buf, "%format", var_format);
220 MATCH(tag, buf, "->", menu_next);
221 MATCH(tag, buf, "==>", menu_load);
223 logf("NO MATCH: %s\n", buf);
224 if (buf[0] == '?')
225 return 0;
227 return -1;
230 static int get_clause(int *condition)
232 char buf[4];
233 int i;
235 /* Find the start. */
236 while (*strp == ' ' && *strp != '\0')
237 strp++;
239 if (*strp == '\0')
240 return 0;
242 for (i = 0; i < (int)sizeof(buf)-1; i++)
244 if (*strp == '\0' || *strp == ' ')
245 break ;
246 buf[i] = *strp;
247 strp++;
249 buf[i] = '\0';
251 MATCH(condition, buf, "=", clause_is);
252 MATCH(condition, buf, "==", clause_is);
253 MATCH(condition, buf, "!=", clause_is_not);
254 MATCH(condition, buf, ">", clause_gt);
255 MATCH(condition, buf, ">=", clause_gteq);
256 MATCH(condition, buf, "<", clause_lt);
257 MATCH(condition, buf, "<=", clause_lteq);
258 MATCH(condition, buf, "~", clause_contains);
259 MATCH(condition, buf, "!~", clause_not_contains);
260 MATCH(condition, buf, "^", clause_begins_with);
261 MATCH(condition, buf, "!^", clause_not_begins_with);
262 MATCH(condition, buf, "$", clause_ends_with);
263 MATCH(condition, buf, "!$", clause_not_ends_with);
264 MATCH(condition, buf, "@", clause_oneof);
266 return 0;
269 static bool read_clause(struct tagcache_search_clause *clause)
271 char buf[256];
273 if (get_tag(&clause->tag) <= 0)
274 return false;
276 if (get_clause(&clause->type) <= 0)
277 return false;
279 if (get_token_str(buf, sizeof buf) < 0)
280 return false;
282 clause->str = buffer_alloc(strlen(buf)+1);
283 strcpy(clause->str, buf);
285 logf("got clause: %d/%d [%s]", clause->tag, clause->type, clause->str);
287 if (*(clause->str) == '\0')
288 clause->input = true;
289 else
290 clause->input = false;
292 if (tagcache_is_numeric_tag(clause->tag))
294 clause->numeric = true;
295 clause->numeric_data = atoi(clause->str);
297 else
298 clause->numeric = false;
300 return true;
303 static bool read_variable(char *buf, int size)
305 int condition;
307 if (!get_clause(&condition))
308 return false;
310 if (condition != clause_is)
311 return false;
313 if (get_token_str(buf, size) < 0)
314 return false;
316 return true;
319 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
320 static int get_format_str(struct display_format *fmt)
322 int ret;
323 char buf[128];
324 int i;
326 memset(fmt, 0, sizeof(struct display_format));
328 if (get_token_str(fmt->name, sizeof fmt->name) < 0)
329 return -10;
331 /* Determine the group id */
332 fmt->group_id = 0;
333 for (i = 0; i < format_count; i++)
335 if (!strcasecmp(formats[i]->name, fmt->name))
337 fmt->group_id = formats[i]->group_id;
338 break;
341 if (formats[i]->group_id > fmt->group_id)
342 fmt->group_id = formats[i]->group_id;
345 if (i == format_count)
346 fmt->group_id++;
348 logf("format: (%d) %s", fmt->group_id, fmt->name);
350 if (get_token_str(buf, sizeof buf) < 0)
351 return -10;
353 fmt->formatstr = buffer_alloc(strlen(buf) + 1);
354 strcpy(fmt->formatstr, buf);
356 while (fmt->tag_count < MAX_TAGS)
358 ret = get_tag(&fmt->tags[fmt->tag_count]);
359 if (ret < 0)
360 return -11;
362 if (ret == 0)
363 break;
365 switch (fmt->tags[fmt->tag_count]) {
366 case var_sorttype:
367 if (!read_variable(buf, sizeof buf))
368 return -12;
369 if (!strcasecmp("inverse", buf))
370 fmt->sort_inverse = true;
372 fmt->sort = true;
373 break;
375 case var_limit:
376 if (!read_variable(buf, sizeof buf))
377 return -13;
378 fmt->limit = atoi(buf);
379 break;
381 case var_strip:
382 if (!read_variable(buf, sizeof buf))
383 return -14;
384 fmt->strip = atoi(buf);
385 break;
387 default:
388 fmt->tag_count++;
392 return 1;
395 static int add_format(const char *buf)
397 strp = buf;
399 if (formats[format_count] == NULL)
400 formats[format_count] = buffer_alloc(sizeof(struct display_format));
402 memset(formats[format_count], 0, sizeof(struct display_format));
403 if (get_format_str(formats[format_count]) < 0)
405 logf("get_format_str() parser failed!");
406 return -4;
409 while (*strp != '\0' && *strp != '?')
410 strp++;
412 if (*strp == '?')
414 int clause_count = 0;
415 strp++;
417 while (1)
419 if (clause_count >= TAGCACHE_MAX_CLAUSES)
421 logf("too many clauses");
422 break;
425 formats[format_count]->clause[clause_count] =
426 buffer_alloc(sizeof(struct tagcache_search_clause));
428 if (!read_clause(formats[format_count]->clause[clause_count]))
429 break;
431 clause_count++;
434 formats[format_count]->clause_count = clause_count;
437 format_count++;
439 return 1;
442 static int get_condition(struct search_instruction *inst)
444 int clause_count;
445 char buf[128];
447 switch (*strp)
449 case '=':
451 int i;
453 if (get_token_str(buf, sizeof buf) < 0)
454 return -1;
456 for (i = 0; i < format_count; i++)
458 if (!strcasecmp(formats[i]->name, buf))
459 break;
462 if (i == format_count)
464 logf("format not found: %s", buf);
465 return -2;
468 inst->format_id[inst->tagorder_count] = formats[i]->group_id;
469 return 1;
471 case '?':
472 case ' ':
473 case '&':
474 strp++;
475 return 1;
476 case '-':
477 case '\0':
478 return 0;
481 clause_count = inst->clause_count[inst->tagorder_count];
482 if (clause_count >= TAGCACHE_MAX_CLAUSES)
484 logf("Too many clauses");
485 return false;
488 inst->clause[inst->tagorder_count][clause_count] =
489 buffer_alloc(sizeof(struct tagcache_search_clause));
491 if (!read_clause(inst->clause[inst->tagorder_count][clause_count]))
492 return -1;
494 inst->clause_count[inst->tagorder_count]++;
496 return 1;
499 /* example search:
500 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
501 * : album ? year >= "2000" : songs
502 * ^ begins with
503 * * contains
504 * $ ends with
507 static bool parse_search(struct menu_entry *entry, const char *str)
509 int ret;
510 int type;
511 struct search_instruction *inst = entry->si;
512 char buf[MAX_PATH];
513 int i;
515 strp = str;
517 /* Parse entry name */
518 if (get_token_str(entry->name, sizeof entry->name) < 0)
520 logf("No name found.");
521 return false;
524 /* Parse entry type */
525 if (get_tag(&entry->type) <= 0)
526 return false;
528 if (entry->type == menu_load)
530 if (get_token_str(buf, sizeof buf) < 0)
531 return false;
533 /* Find the matching root menu or "create" it */
534 for (i = 0; i < menu_count; i++)
536 if (!strcasecmp(menus[i]->id, buf))
538 entry->link = i;
539 menus[i]->parent = menu;
540 return true;
544 return false;
547 if (entry->type != menu_next)
548 return false;
550 while (inst->tagorder_count < MAX_TAGS)
552 ret = get_tag(&inst->tagorder[inst->tagorder_count]);
553 if (ret < 0)
555 logf("Parse error #1");
556 logf("%s", strp);
557 return false;
560 if (ret == 0)
561 break ;
563 logf("tag: %d", inst->tagorder[inst->tagorder_count]);
565 while ( (ret = get_condition(inst)) > 0 ) ;
566 if (ret < 0)
567 return false;
569 inst->tagorder_count++;
571 if (get_tag(&type) <= 0 || type != menu_next)
572 break;
575 return true;
578 static int compare(const void *p1, const void *p2)
580 struct tagentry *e1 = (struct tagentry *)p1;
581 struct tagentry *e2 = (struct tagentry *)p2;
583 if (sort_inverse)
584 return strncasecmp(e2->name, e1->name, MAX_PATH);
586 return strncasecmp(e1->name, e2->name, MAX_PATH);
589 static void tagtree_buffer_event(struct mp3entry *id3, bool last_track)
591 (void)id3;
592 (void)last_track;
594 /* Do not gather data unless proper setting has been enabled. */
595 if (!global_settings.runtimedb)
596 return;
598 logf("be:%d%s", last_track, id3->path);
600 if (!tagcache_find_index(&tcs, id3->path))
602 logf("tc stat: not found: %s", id3->path);
603 return;
606 id3->playcount = tagcache_get_numeric(&tcs, tag_playcount);
607 if(!id3->rating) id3->rating = tagcache_get_numeric(&tcs, tag_rating);
608 id3->lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
609 id3->score = tagcache_get_numeric(&tcs, tag_virt_autoscore) / 10;
611 tagcache_search_finish(&tcs);
614 static void tagtree_unbuffer_event(struct mp3entry *id3, bool last_track)
616 (void)last_track;
617 long playcount;
618 long rating;
619 long playtime;
620 long lastplayed;
622 /* Do not gather data unless proper setting has been enabled. */
623 if (!global_settings.runtimedb)
625 logf("runtimedb gathering not enabled");
626 return;
629 /* Don't process unplayed tracks. */
630 if (id3->elapsed == 0)
632 logf("not logging unplayed track");
633 return;
636 if (!tagcache_find_index(&tcs, id3->path))
638 logf("tc stat: not found: %s", id3->path);
639 return;
642 playcount = tagcache_get_numeric(&tcs, tag_playcount);
643 playtime = tagcache_get_numeric(&tcs, tag_playtime);
644 lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
646 playcount++;
648 rating = (long) id3->rating;
650 lastplayed = tagcache_increase_serial();
651 if (lastplayed < 0)
653 logf("incorrect tc serial:%ld", lastplayed);
654 tagcache_search_finish(&tcs);
655 return;
658 /* Ignore the last 15s (crossfade etc.) */
659 playtime += MIN(id3->length, id3->elapsed + 15 * 1000);
661 logf("ube:%s", id3->path);
662 logf("-> %d/%ld/%ld/%ld", last_track, playcount, rating, playtime);
663 logf("-> %ld/%ld/%ld", id3->elapsed, id3->length, MIN(id3->length, id3->elapsed + 15 * 1000));
665 /* lastplayed not yet supported. */
667 if (!tagcache_modify_numeric_entry(&tcs, tag_playcount, playcount)
668 || !tagcache_modify_numeric_entry(&tcs, tag_rating, rating)
669 || !tagcache_modify_numeric_entry(&tcs, tag_playtime, playtime)
670 || !tagcache_modify_numeric_entry(&tcs, tag_lastplayed, lastplayed))
672 logf("tc stat: modify failed!");
673 tagcache_search_finish(&tcs);
674 return;
677 tagcache_search_finish(&tcs);
680 bool tagtree_export(void)
682 gui_syncsplash(0, str(LANG_CREATING));
683 if (!tagcache_create_changelog(&tcs))
685 gui_syncsplash(HZ*2, str(LANG_FAILED));
688 return false;
691 bool tagtree_import(void)
693 gui_syncsplash(0, str(LANG_WAIT));
694 if (!tagcache_import_changelog())
696 gui_syncsplash(HZ*2, str(LANG_FAILED));
699 return false;
702 static bool parse_menu(const char *filename);
704 static int parse_line(int n, const char *buf, void *parameters)
706 char data[256];
707 int variable;
708 static bool read_menu;
709 int i;
711 (void)parameters;
713 logf("parse:%d/%s", n, buf);
715 /* First line, do initialisation. */
716 if (n == 0)
718 if (strcasecmp(TAGNAVI_VERSION, buf))
720 logf("Version mismatch");
721 return -1;
724 read_menu = false;
727 if (buf[0] == '#')
728 return 0;
730 if (buf[0] == '\0')
732 if (read_menu)
734 /* End the menu */
735 menu_count++;
736 read_menu = false;
738 return 0;
741 if (!read_menu)
743 strp = buf;
744 if (get_tag(&variable) <= 0)
745 return 0;
747 switch (variable)
749 case var_format:
750 if (add_format(strp) < 0)
752 logf("Format add fail: %s", data);
754 break;
756 case var_include:
757 if (get_token_str(data, sizeof(data)) < 0)
759 logf("%%include empty");
760 return 0;
763 if (!parse_menu(data))
765 logf("Load menu fail: %s", data);
767 break;
769 case var_menu_start:
770 if (menu_count >= TAGMENU_MAX_MENUS)
772 logf("max menucount reached");
773 return 0;
776 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
777 menu = menus[menu_count];
778 memset(menu, 0, sizeof(struct root_menu));
779 if (get_token_str(menu->id, sizeof(menu->id)) < 0)
781 logf("%%menu_start id empty");
782 return 0;
784 if (get_token_str(menu->title, sizeof(menu->title)) < 0)
786 logf("%%menu_start title empty");
787 return 0;
789 logf("menu: %s", menu->title);
790 menu->itemcount = 0;
791 read_menu = true;
792 break;
794 case var_rootmenu:
795 /* Only set root menu once. */
796 if (root_menu)
797 break;
799 if (get_token_str(data, sizeof(data)) < 0)
801 logf("%%root_menu empty");
802 return 0;
805 for (i = 0; i < menu_count; i++)
807 if (!strcasecmp(menus[i]->id, data))
809 root_menu = i;
812 break;
815 return 0;
818 if (menu->itemcount >= TAGMENU_MAX_ITEMS)
820 logf("max itemcount reached");
821 return 0;
824 /* Allocate */
825 if (menu->items[menu->itemcount] == NULL)
827 menu->items[menu->itemcount] = buffer_alloc(sizeof(struct menu_entry));
828 memset(menu->items[menu->itemcount], 0, sizeof(struct menu_entry));
829 menu->items[menu->itemcount]->si = buffer_alloc(sizeof(struct search_instruction));
832 memset(menu->items[menu->itemcount]->si, 0, sizeof(struct search_instruction));
833 if (!parse_search(menu->items[menu->itemcount], buf))
834 return 0;
836 menu->itemcount++;
838 return 0;
841 static bool parse_menu(const char *filename)
843 int fd;
844 char buf[1024];
846 if (menu_count >= TAGMENU_MAX_MENUS)
848 logf("max menucount reached");
849 return false;
852 fd = open(filename, O_RDONLY);
853 if (fd < 0)
855 logf("Search instruction file not found.");
856 return false;
859 /* Now read file for real, parsing into si */
860 fast_readline(fd, buf, sizeof buf, NULL, parse_line);
861 close(fd);
863 return true;
866 void tagtree_init(void)
868 format_count = 0;
869 menu_count = 0;
870 menu = NULL;
871 root_menu = 0;
872 parse_menu(FILE_SEARCH_INSTRUCTIONS);
874 uniqbuf = buffer_alloc(UNIQBUF_SIZE);
875 audio_set_track_buffer_event(tagtree_buffer_event);
876 audio_set_track_unbuffer_event(tagtree_unbuffer_event);
879 static bool show_search_progress(bool init, int count)
881 static int last_tick = 0;
883 if (init)
885 last_tick = current_tick;
886 return true;
889 if (current_tick - last_tick > HZ/4)
891 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG), count,
892 #if CONFIG_KEYPAD == PLAYER_PAD
893 str(LANG_STOP_ABORT)
894 #else
895 str(LANG_OFF_ABORT)
896 #endif
898 if (action_userabort(TIMEOUT_NOBLOCK))
899 return false;
900 last_tick = current_tick;
901 yield();
904 return true;
907 static int format_str(struct tagcache_search *tcs, struct display_format *fmt,
908 char *buf, int buf_size)
910 char fmtbuf[8];
911 bool read_format = false;
912 int fmtbuf_pos = 0;
913 int parpos = 0;
914 int buf_pos = 0;
915 int i;
917 memset(buf, 0, buf_size);
918 for (i = 0; fmt->formatstr[i] != '\0'; i++)
920 if (fmt->formatstr[i] == '%')
922 read_format = true;
923 fmtbuf_pos = 0;
924 if (parpos >= fmt->tag_count)
926 logf("too many format tags");
927 return -1;
931 if (read_format)
933 fmtbuf[fmtbuf_pos++] = fmt->formatstr[i];
934 if (fmtbuf_pos >= buf_size)
936 logf("format parse error");
937 return -2;
940 if (fmt->formatstr[i] == 's')
942 fmtbuf[fmtbuf_pos] = '\0';
943 read_format = false;
944 if (fmt->tags[parpos] == tcs->type)
946 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf, tcs->result);
948 else
950 /* Need to fetch the tag data. */
951 if (!tagcache_retrieve(tcs, tcs->idx_id, fmt->tags[parpos],
952 &buf[buf_pos], buf_size - buf_pos))
954 logf("retrieve failed");
955 return -3;
958 buf_pos += strlen(&buf[buf_pos]);
959 parpos++;
961 else if (fmt->formatstr[i] == 'd')
963 fmtbuf[fmtbuf_pos] = '\0';
964 read_format = false;
965 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf,
966 tagcache_get_numeric(tcs, fmt->tags[parpos]));
967 buf_pos += strlen(&buf[buf_pos]);
968 parpos++;
970 continue;
973 buf[buf_pos++] = fmt->formatstr[i];
975 if (buf_pos - 1 >= buf_size)
977 logf("buffer overflow");
978 return -4;
982 buf[buf_pos++] = '\0';
984 return 0;
987 static int retrieve_entries(struct tree_context *c, struct tagcache_search *tcs,
988 int offset, bool init)
990 struct tagentry *dptr = (struct tagentry *)c->dircache;
991 struct display_format *fmt;
992 int i;
993 int namebufused = 0;
994 int total_count = 0;
995 int special_entry_count = 0;
996 int level = c->currextra;
997 int tag;
998 bool sort = false;
999 int sort_limit;
1000 int strip;
1002 if (init
1003 #ifdef HAVE_TC_RAMCACHE
1004 && !tagcache_is_ramcache()
1005 #endif
1008 show_search_progress(true, 0);
1009 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG),
1010 0, csi->name);
1013 if (c->currtable == allsubentries)
1015 tag = tag_title;
1016 level--;
1018 else
1019 tag = csi->tagorder[level];
1021 if (!tagcache_search(tcs, tag))
1022 return -1;
1024 /* Prevent duplicate entries in the search list. */
1025 tagcache_search_set_uniqbuf(tcs, uniqbuf, UNIQBUF_SIZE);
1027 if (level || csi->clause_count[0] || tagcache_is_numeric_tag(tag))
1028 sort = true;
1030 for (i = 0; i < level; i++)
1032 if (tagcache_is_numeric_tag(csi->tagorder[i]))
1034 static struct tagcache_search_clause cc;
1036 memset(&cc, 0, sizeof(struct tagcache_search_clause));
1037 cc.tag = csi->tagorder[i];
1038 cc.type = clause_is;
1039 cc.numeric = true;
1040 cc.numeric_data = csi->result_seek[i];
1041 tagcache_search_add_clause(tcs, &cc);
1043 else
1045 tagcache_search_add_filter(tcs, csi->tagorder[i],
1046 csi->result_seek[i]);
1050 for (i = 0; i <= level; i++)
1052 int j;
1054 for (j = 0; j < csi->clause_count[i]; j++)
1055 tagcache_search_add_clause(tcs, csi->clause[i][j]);
1058 current_offset = offset;
1059 current_entry_count = 0;
1060 c->dirfull = false;
1062 fmt = NULL;
1063 for (i = 0; i < format_count; i++)
1065 if (formats[i]->group_id == csi->format_id[level])
1066 fmt = formats[i];
1069 if (fmt)
1071 sort_inverse = fmt->sort_inverse;
1072 sort_limit = fmt->limit;
1073 strip = fmt->strip;
1075 /* Check if sorting is forced. */
1076 if (fmt->sort)
1077 sort = true;
1079 else
1081 sort_inverse = false;
1082 sort_limit = 0;
1083 strip = 0;
1086 if (tag != tag_title && tag != tag_filename)
1088 if (offset == 0)
1090 dptr->newtable = allsubentries;
1091 dptr->name = str(LANG_TAGNAVI_ALL_TRACKS);
1092 dptr++;
1093 current_entry_count++;
1095 special_entry_count++;
1098 total_count += special_entry_count;
1100 while (tagcache_get_next(tcs))
1102 if (total_count++ < offset)
1103 continue;
1105 dptr->newtable = navibrowse;
1106 if (tag == tag_title || tag == tag_filename)
1108 dptr->newtable = playtrack;
1109 dptr->extraseek = tcs->idx_id;
1111 else
1112 dptr->extraseek = tcs->result_seek;
1114 fmt = NULL;
1115 /* Check the format */
1116 for (i = 0; i < format_count; i++)
1118 if (formats[i]->group_id != csi->format_id[level])
1119 continue;
1121 if (tagcache_check_clauses(tcs, formats[i]->clause,
1122 formats[i]->clause_count))
1124 fmt = formats[i];
1125 break;
1129 if (!tcs->ramresult || fmt)
1131 char buf[MAX_PATH];
1133 if (fmt)
1135 if (format_str(tcs, fmt, buf, sizeof buf) < 0)
1137 logf("format_str() failed");
1138 return 0;
1142 dptr->name = &c->name_buffer[namebufused];
1143 if (fmt)
1144 namebufused += strlen(buf)+1;
1145 else
1146 namebufused += tcs->result_len;
1148 if (namebufused >= c->name_buffer_size)
1150 logf("chunk mode #2: %d", current_entry_count);
1151 c->dirfull = true;
1152 sort = false;
1153 break ;
1155 if (fmt)
1156 strcpy(dptr->name, buf);
1157 else
1158 strcpy(dptr->name, tcs->result);
1160 else
1161 dptr->name = tcs->result;
1163 dptr++;
1164 current_entry_count++;
1166 if (current_entry_count >= global_settings.max_files_in_dir)
1168 logf("chunk mode #3: %d", current_entry_count);
1169 c->dirfull = true;
1170 sort = false;
1171 break ;
1174 if (init && !tcs->ramsearch)
1176 if (!show_search_progress(false, i))
1178 tagcache_search_finish(tcs);
1179 return current_entry_count;
1184 if (sort)
1185 qsort(c->dircache + special_entry_count * c->dentry_size,
1186 current_entry_count - special_entry_count,
1187 c->dentry_size, compare);
1189 if (!init)
1191 tagcache_search_finish(tcs);
1192 return current_entry_count;
1195 while (tagcache_get_next(tcs))
1197 if (!tcs->ramsearch)
1199 if (!show_search_progress(false, total_count))
1200 break;
1202 total_count++;
1205 tagcache_search_finish(tcs);
1207 if (!sort && (sort_inverse || sort_limit))
1209 gui_syncsplash(HZ*4, str(LANG_SHOWDIR_BUFFER_FULL), total_count);
1210 logf("Too small dir buffer");
1211 return 0;
1214 if (sort_limit)
1215 total_count = MIN(total_count, sort_limit);
1217 if (strip)
1219 dptr = c->dircache;
1220 for (i = 0; i < total_count; i++, dptr++)
1222 int len = strlen(dptr->name);
1224 if (len < strip)
1225 continue;
1227 dptr->name = &dptr->name[strip];
1231 return total_count;
1234 static int load_root(struct tree_context *c)
1236 struct tagentry *dptr = (struct tagentry *)c->dircache;
1237 int i;
1239 tc = c;
1240 c->currtable = root;
1241 if (c->dirlevel == 0)
1242 c->currextra = root_menu;
1244 menu = menus[c->currextra];
1245 if (menu == NULL)
1246 return 0;
1248 for (i = 0; i < menu->itemcount; i++)
1250 dptr->name = menu->items[i]->name;
1251 switch (menu->items[i]->type)
1253 case menu_next:
1254 dptr->newtable = navibrowse;
1255 dptr->extraseek = i;
1256 break;
1258 case menu_load:
1259 dptr->newtable = root;
1260 dptr->extraseek = menu->items[i]->link;
1261 break;
1264 dptr++;
1267 current_offset = 0;
1268 current_entry_count = i;
1270 return i;
1273 int tagtree_load(struct tree_context* c)
1275 int count;
1276 int table = c->currtable;
1278 c->dentry_size = sizeof(struct tagentry);
1279 c->dirsindir = 0;
1281 if (!table)
1283 c->dirfull = false;
1284 table = root;
1285 c->currtable = table;
1286 c->currextra = root_menu;
1289 switch (table)
1291 case root:
1292 count = load_root(c);
1293 break;
1295 case allsubentries:
1296 case navibrowse:
1297 logf("navibrowse...");
1298 cpu_boost(true);
1299 count = retrieve_entries(c, &tcs, 0, true);
1300 cpu_boost(false);
1301 break;
1303 default:
1304 logf("Unsupported table %d\n", table);
1305 return -1;
1308 if (count < 0)
1310 c->dirlevel = 0;
1311 count = load_root(c);
1312 gui_syncsplash(HZ, str(LANG_TAGCACHE_BUSY));
1315 /* The _total_ numer of entries available. */
1316 c->dirlength = c->filesindir = count;
1318 return count;
1321 int tagtree_enter(struct tree_context* c)
1323 int rc = 0;
1324 struct tagentry *dptr;
1325 int newextra;
1326 int seek;
1328 dptr = tagtree_get_entry(c, c->selected_item);
1330 c->dirfull = false;
1331 newextra = dptr->newtable;
1332 seek = dptr->extraseek;
1334 if (c->dirlevel >= MAX_DIR_LEVELS)
1335 return 0;
1337 c->selected_item_history[c->dirlevel]=c->selected_item;
1338 c->table_history[c->dirlevel] = c->currtable;
1339 c->extra_history[c->dirlevel] = c->currextra;
1340 c->pos_history[c->dirlevel] = c->firstpos;
1341 c->dirlevel++;
1343 switch (c->currtable) {
1344 case root:
1345 c->currextra = newextra;
1347 if (newextra == root)
1349 menu = menus[seek];
1350 c->currextra = seek;
1353 else if (newextra == navibrowse)
1355 int i, j;
1357 csi = menu->items[seek]->si;
1358 c->currextra = 0;
1360 strncpy(current_title[c->currextra], dptr->name,
1361 sizeof(current_title[0]) - 1);
1363 /* Read input as necessary. */
1364 for (i = 0; i < csi->tagorder_count; i++)
1366 for (j = 0; j < csi->clause_count[i]; j++)
1368 if (!csi->clause[i][j]->input)
1369 continue;
1371 rc = kbd_input(searchstring, sizeof(searchstring));
1372 if (rc == -1 || !searchstring[0])
1374 tagtree_exit(c);
1375 return 0;
1378 if (csi->clause[i][j]->numeric)
1379 csi->clause[i][j]->numeric_data = atoi(searchstring);
1380 else
1381 csi->clause[i][j]->str = searchstring;
1385 c->currtable = newextra;
1387 break;
1389 case navibrowse:
1390 case allsubentries:
1391 if (newextra == playtrack)
1393 c->dirlevel--;
1394 /* about to create a new current playlist...
1395 allow user to cancel the operation */
1396 if (global_settings.warnon_erase_dynplaylist &&
1397 !global_settings.party_mode &&
1398 playlist_modified(NULL))
1400 char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
1401 struct text_message message={lines, 1};
1403 if (gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
1404 break;
1407 if (tagtree_play_folder(c) >= 0)
1408 rc = 2;
1409 break;
1412 c->currtable = newextra;
1413 csi->result_seek[c->currextra] = seek;
1414 if (c->currextra < csi->tagorder_count-1)
1415 c->currextra++;
1416 else
1417 c->dirlevel--;
1419 /* Update the statusbar title */
1420 strncpy(current_title[c->currextra], dptr->name,
1421 sizeof(current_title[0]) - 1);
1422 break;
1424 default:
1425 c->dirlevel--;
1426 break;
1429 c->selected_item=0;
1430 gui_synclist_select_item(&tree_lists, c->selected_item);
1432 return rc;
1435 void tagtree_exit(struct tree_context* c)
1437 c->dirfull = false;
1438 if (c->dirlevel > 0)
1439 c->dirlevel--;
1440 c->selected_item=c->selected_item_history[c->dirlevel];
1441 gui_synclist_select_item(&tree_lists, c->selected_item);
1442 c->currtable = c->table_history[c->dirlevel];
1443 c->currextra = c->extra_history[c->dirlevel];
1444 c->firstpos = c->pos_history[c->dirlevel];
1447 int tagtree_get_filename(struct tree_context* c, char *buf, int buflen)
1449 struct tagentry *entry;
1451 entry = tagtree_get_entry(c, c->selected_item);
1453 if (!tagcache_search(&tcs, tag_filename))
1454 return -1;
1456 if (!tagcache_retrieve(&tcs, entry->extraseek, tcs.type, buf, buflen))
1458 tagcache_search_finish(&tcs);
1459 return -2;
1462 tagcache_search_finish(&tcs);
1464 return 0;
1467 static bool insert_all_playlist(struct tree_context *c, int position, bool queue)
1469 int i;
1470 char buf[MAX_PATH];
1471 int from, to, direction;
1472 int files_left = c->filesindir;
1474 cpu_boost(true);
1475 if (!tagcache_search(&tcs, tag_filename))
1477 gui_syncsplash(HZ, str(LANG_TAGCACHE_BUSY));
1478 cpu_boost(false);
1479 return false;
1482 if (position == PLAYLIST_REPLACE)
1484 if (remove_all_tracks(NULL) == 0)
1485 position = PLAYLIST_INSERT_LAST;
1486 else return -1; }
1488 if (position == PLAYLIST_INSERT_FIRST)
1490 from = c->filesindir - 1;
1491 to = -1;
1492 direction = -1;
1494 else
1496 from = 0;
1497 to = c->filesindir;
1498 direction = 1;
1501 for (i = from; i != to; i += direction)
1503 /* Count back to zero */
1504 if (!show_search_progress(false, files_left--))
1505 break;
1507 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, i)->extraseek,
1508 tcs.type, buf, sizeof buf))
1510 continue;
1513 if (playlist_insert_track(NULL, buf, position, queue, false) < 0)
1515 logf("playlist_insert_track failed");
1516 break;
1518 yield();
1520 playlist_sync(NULL);
1521 tagcache_search_finish(&tcs);
1522 cpu_boost(false);
1524 return true;
1527 bool tagtree_insert_selection_playlist(int position, bool queue)
1529 struct tagentry *dptr;
1530 char buf[MAX_PATH];
1531 int dirlevel = tc->dirlevel;
1533 /* We need to set the table to allsubentries. */
1534 show_search_progress(true, 0);
1536 dptr = tagtree_get_entry(tc, tc->selected_item);
1538 /* Insert a single track? */
1539 if (dptr->newtable == playtrack)
1541 if (tagtree_get_filename(tc, buf, sizeof buf) < 0)
1543 logf("tagtree_get_filename failed");
1544 return false;
1546 playlist_insert_track(NULL, buf, position, queue, true);
1548 return true;
1551 if (dptr->newtable == navibrowse)
1553 tagtree_enter(tc);
1554 tagtree_load(tc);
1555 dptr = tagtree_get_entry(tc, tc->selected_item);
1557 else if (dptr->newtable != allsubentries)
1559 logf("unsupported table: %d", dptr->newtable);
1560 return false;
1563 /* Now the current table should be allsubentries. */
1564 if (dptr->newtable != playtrack)
1566 tagtree_enter(tc);
1567 tagtree_load(tc);
1568 dptr = tagtree_get_entry(tc, tc->selected_item);
1570 /* And now the newtable should be playtrack. */
1571 if (dptr->newtable != playtrack)
1573 logf("newtable: %d !!", dptr->newtable);
1574 tc->dirlevel = dirlevel;
1575 return false;
1579 if (tc->filesindir <= 0)
1580 gui_syncsplash(HZ, str(LANG_END_PLAYLIST_PLAYER));
1581 else
1583 logf("insert_all_playlist");
1584 if (!insert_all_playlist(tc, position, queue))
1585 gui_syncsplash(HZ*2, str(LANG_FAILED));
1588 /* Finally return the dirlevel to its original value. */
1589 while (tc->dirlevel > dirlevel)
1590 tagtree_exit(tc);
1591 tagtree_load(tc);
1593 return true;
1596 static int tagtree_play_folder(struct tree_context* c)
1598 if (playlist_create(NULL, NULL) < 0)
1600 logf("Failed creating playlist\n");
1601 return -1;
1604 if (!insert_all_playlist(c, PLAYLIST_INSERT_LAST, false))
1605 return -2;
1607 if (global_settings.playlist_shuffle)
1608 c->selected_item = playlist_shuffle(current_tick, c->selected_item);
1609 if (!global_settings.play_selected)
1610 c->selected_item = 0;
1611 gui_synclist_select_item(&tree_lists, c->selected_item);
1613 playlist_start(c->selected_item,0);
1615 return 0;
1618 struct tagentry* tagtree_get_entry(struct tree_context *c, int id)
1620 struct tagentry *entry = (struct tagentry *)c->dircache;
1621 int realid = id - current_offset;
1623 /* Load the next chunk if necessary. */
1624 if (realid >= current_entry_count || realid < 0)
1626 cpu_boost(true);
1627 if (retrieve_entries(c, &tcs2, MAX(0, id - (current_entry_count / 2)),
1628 false) < 0)
1630 logf("retrieve failed");
1631 cpu_boost(false);
1632 return NULL;
1634 realid = id - current_offset;
1635 cpu_boost(false);
1638 return &entry[realid];
1641 char *tagtree_get_title(struct tree_context* c)
1643 switch (c->currtable)
1645 case root:
1646 return menu->title;
1648 case navibrowse:
1649 case allsubentries:
1650 return current_title[c->currextra];
1653 return "?";
1656 int tagtree_get_attr(struct tree_context* c)
1658 int attr = -1;
1659 switch (c->currtable)
1661 case navibrowse:
1662 if (csi->tagorder[c->currextra] == tag_title)
1663 attr = TREE_ATTR_MPA;
1664 else
1665 attr = ATTR_DIRECTORY;
1666 break;
1668 case allsubentries:
1669 attr = TREE_ATTR_MPA;
1670 break;
1672 default:
1673 attr = ATTR_DIRECTORY;
1674 break;
1677 return attr;
1680 #ifdef HAVE_LCD_BITMAP
1681 const unsigned char* tagtree_get_icon(struct tree_context* c)
1682 #else
1683 int tagtree_get_icon(struct tree_context* c)
1684 #endif
1686 int icon = Icon_Folder;
1688 if (tagtree_get_attr(c) == TREE_ATTR_MPA)
1689 icon = Icon_Audio;
1691 #ifdef HAVE_LCD_BITMAP
1692 return bitmap_icons_6x8[icon];
1693 #else
1694 return icon;
1695 #endif