Update several codec Makefiles so that the codec libs build again on Coldfire targets...
[Rockbox.git] / apps / tagtree.c
blobe1cb9e65bc37fb81e6e3381d25d940874967e3cd
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;
100 static struct display_format *formats[TAGMENU_MAX_FMTS];
101 static int format_count;
103 struct search_instruction {
104 char name[64];
105 int tagorder[MAX_TAGS];
106 int tagorder_count;
107 struct tagcache_search_clause *clause[MAX_TAGS][TAGCACHE_MAX_CLAUSES];
108 int format_id[MAX_TAGS];
109 int clause_count[MAX_TAGS];
110 int result_seek[MAX_TAGS];
113 struct menu_entry {
114 char name[64];
115 int type;
116 struct search_instruction *si;
117 int link;
120 struct root_menu {
121 char title[64];
122 char id[32];
123 int itemcount;
124 struct root_menu *parent;
125 struct menu_entry *items[TAGMENU_MAX_ITEMS];
128 /* Statusbar text of the current view. */
129 static char current_title[MAX_TAGS][128];
131 static struct root_menu *menus[TAGMENU_MAX_MENUS];
132 static struct root_menu *menu;
133 static struct search_instruction *csi;
134 static const char *strp;
135 static int menu_count;
136 static int root_menu;
138 static int current_offset;
139 static int current_entry_count;
141 static int format_count;
142 static struct tree_context *tc;
144 static int get_token_str(char *buf, int size)
146 /* Find the start. */
147 while (*strp != '"' && *strp != '\0')
148 strp++;
150 if (*strp == '\0' || *(++strp) == '\0')
151 return -1;
153 /* Read the data. */
154 while (*strp != '"' && *strp != '\0' && --size > 0)
155 *(buf++) = *(strp++);
157 *buf = '\0';
158 if (*strp != '"')
159 return -2;
161 strp++;
163 return 0;
166 #define MATCH(tag,str1,str2,settag) \
167 if (!strcasecmp(str1, str2)) { \
168 *tag = settag; \
169 return 1; \
172 static int get_tag(int *tag)
174 char buf[128];
175 int i;
177 /* Find the start. */
178 while ((*strp == ' ' || *strp == '>') && *strp != '\0')
179 strp++;
181 if (*strp == '\0' || *strp == '?')
182 return 0;
184 for (i = 0; i < (int)sizeof(buf)-1; i++)
186 if (*strp == '\0' || *strp == ' ')
187 break ;
188 buf[i] = *strp;
189 strp++;
191 buf[i] = '\0';
193 MATCH(tag, buf, "album", tag_album);
194 MATCH(tag, buf, "artist", tag_artist);
195 MATCH(tag, buf, "bitrate", tag_bitrate);
196 MATCH(tag, buf, "composer", tag_composer);
197 MATCH(tag, buf, "genre", tag_genre);
198 MATCH(tag, buf, "length", tag_length);
199 MATCH(tag, buf, "title", tag_title);
200 MATCH(tag, buf, "filename", tag_filename);
201 MATCH(tag, buf, "tracknum", tag_tracknumber);
202 MATCH(tag, buf, "year", tag_year);
203 MATCH(tag, buf, "playcount", tag_playcount);
204 MATCH(tag, buf, "lastplayed", tag_lastplayed);
205 MATCH(tag, buf, "autoscore", tag_virt_autoscore);
206 MATCH(tag, buf, "%sort", var_sorttype);
207 MATCH(tag, buf, "%limit", var_limit);
208 MATCH(tag, buf, "%strip", var_strip);
209 MATCH(tag, buf, "%menu_start", var_menu_start);
210 MATCH(tag, buf, "%include", var_include);
211 MATCH(tag, buf, "%root_menu", var_rootmenu);
212 MATCH(tag, buf, "%format", var_format);
213 MATCH(tag, buf, "->", menu_next);
214 MATCH(tag, buf, "==>", menu_load);
216 logf("NO MATCH: %s\n", buf);
217 if (buf[0] == '?')
218 return 0;
220 return -1;
223 static int get_clause(int *condition)
225 char buf[4];
226 int i;
228 /* Find the start. */
229 while (*strp == ' ' && *strp != '\0')
230 strp++;
232 if (*strp == '\0')
233 return 0;
235 for (i = 0; i < (int)sizeof(buf)-1; i++)
237 if (*strp == '\0' || *strp == ' ')
238 break ;
239 buf[i] = *strp;
240 strp++;
242 buf[i] = '\0';
244 MATCH(condition, buf, "=", clause_is);
245 MATCH(condition, buf, "==", clause_is);
246 MATCH(condition, buf, "!=", clause_is_not);
247 MATCH(condition, buf, ">", clause_gt);
248 MATCH(condition, buf, ">=", clause_gteq);
249 MATCH(condition, buf, "<", clause_lt);
250 MATCH(condition, buf, "<=", clause_lteq);
251 MATCH(condition, buf, "~", clause_contains);
252 MATCH(condition, buf, "!~", clause_not_contains);
253 MATCH(condition, buf, "^", clause_begins_with);
254 MATCH(condition, buf, "!^", clause_not_begins_with);
255 MATCH(condition, buf, "$", clause_ends_with);
256 MATCH(condition, buf, "!$", clause_not_ends_with);
257 MATCH(condition, buf, "@", clause_oneof);
259 return 0;
262 static bool read_clause(struct tagcache_search_clause *clause)
264 char buf[256];
266 if (get_tag(&clause->tag) <= 0)
267 return false;
269 if (get_clause(&clause->type) <= 0)
270 return false;
272 if (get_token_str(buf, sizeof buf) < 0)
273 return false;
275 clause->str = buffer_alloc(strlen(buf)+1);
276 strcpy(clause->str, buf);
278 logf("got clause: %d/%d [%s]", clause->tag, clause->type, clause->str);
280 if (*(clause->str) == '\0')
281 clause->input = true;
282 else
283 clause->input = false;
285 if (tagcache_is_numeric_tag(clause->tag))
287 clause->numeric = true;
288 clause->numeric_data = atoi(clause->str);
290 else
291 clause->numeric = false;
293 return true;
296 static bool read_variable(char *buf, int size)
298 int condition;
300 if (!get_clause(&condition))
301 return false;
303 if (condition != clause_is)
304 return false;
306 if (get_token_str(buf, size) < 0)
307 return false;
309 return true;
312 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
313 static int get_format_str(struct display_format *fmt)
315 int ret;
316 char buf[128];
317 int i;
319 memset(fmt, 0, sizeof(struct display_format));
321 if (get_token_str(fmt->name, sizeof fmt->name) < 0)
322 return -10;
324 /* Determine the group id */
325 fmt->group_id = 0;
326 for (i = 0; i < format_count; i++)
328 if (!strcasecmp(formats[i]->name, fmt->name))
330 fmt->group_id = formats[i]->group_id;
331 break;
334 if (formats[i]->group_id > fmt->group_id)
335 fmt->group_id = formats[i]->group_id;
338 if (i == format_count)
339 fmt->group_id++;
341 logf("format: (%d) %s", fmt->group_id, fmt->name);
343 if (get_token_str(buf, sizeof buf) < 0)
344 return -10;
346 fmt->formatstr = buffer_alloc(strlen(buf) + 1);
347 strcpy(fmt->formatstr, buf);
349 while (fmt->tag_count < MAX_TAGS)
351 ret = get_tag(&fmt->tags[fmt->tag_count]);
352 if (ret < 0)
353 return -11;
355 if (ret == 0)
356 break;
358 switch (fmt->tags[fmt->tag_count]) {
359 case var_sorttype:
360 if (!read_variable(buf, sizeof buf))
361 return -12;
362 if (!strcasecmp("inverse", buf))
363 fmt->sort_inverse = true;
364 break;
366 case var_limit:
367 if (!read_variable(buf, sizeof buf))
368 return -13;
369 fmt->limit = atoi(buf);
370 break;
372 case var_strip:
373 if (!read_variable(buf, sizeof buf))
374 return -14;
375 fmt->strip = atoi(buf);
376 break;
378 default:
379 fmt->tag_count++;
383 return 1;
386 static int add_format(const char *buf)
388 strp = buf;
390 if (formats[format_count] == NULL)
391 formats[format_count] = buffer_alloc(sizeof(struct display_format));
393 memset(formats[format_count], 0, sizeof(struct display_format));
394 if (get_format_str(formats[format_count]) < 0)
396 logf("get_format_str() parser failed!");
397 return -4;
400 while (*strp != '\0' && *strp != '?')
401 strp++;
403 if (*strp == '?')
405 int clause_count = 0;
406 strp++;
408 while (1)
410 if (clause_count >= TAGCACHE_MAX_CLAUSES)
412 logf("too many clauses");
413 break;
416 formats[format_count]->clause[clause_count] =
417 buffer_alloc(sizeof(struct tagcache_search_clause));
419 if (!read_clause(formats[format_count]->clause[clause_count]))
420 break;
422 clause_count++;
425 formats[format_count]->clause_count = clause_count;
428 format_count++;
430 return 1;
433 static int get_condition(struct search_instruction *inst)
435 int clause_count;
436 char buf[128];
438 switch (*strp)
440 case '=':
442 int i;
444 if (get_token_str(buf, sizeof buf) < 0)
445 return -1;
447 for (i = 0; i < format_count; i++)
449 if (!strcasecmp(formats[i]->name, buf))
450 break;
453 if (i == format_count)
455 logf("format not found: %s", buf);
456 return -2;
459 inst->format_id[inst->tagorder_count] = formats[i]->group_id;
460 return 1;
462 case '?':
463 case ' ':
464 case '&':
465 strp++;
466 return 1;
467 case '-':
468 case '\0':
469 return 0;
472 clause_count = inst->clause_count[inst->tagorder_count];
473 if (clause_count >= TAGCACHE_MAX_CLAUSES)
475 logf("Too many clauses");
476 return false;
479 inst->clause[inst->tagorder_count][clause_count] =
480 buffer_alloc(sizeof(struct tagcache_search_clause));
482 if (!read_clause(inst->clause[inst->tagorder_count][clause_count]))
483 return -1;
485 inst->clause_count[inst->tagorder_count]++;
487 return 1;
490 /* example search:
491 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
492 * : album ? year >= "2000" : songs
493 * ^ begins with
494 * * contains
495 * $ ends with
498 static bool parse_search(struct menu_entry *entry, const char *str)
500 int ret;
501 int type;
502 struct search_instruction *inst = entry->si;
503 char buf[MAX_PATH];
504 int i;
506 strp = str;
508 /* Parse entry name */
509 if (get_token_str(entry->name, sizeof entry->name) < 0)
511 logf("No name found.");
512 return false;
515 /* Parse entry type */
516 if (get_tag(&entry->type) <= 0)
517 return false;
519 if (entry->type == menu_load)
521 if (get_token_str(buf, sizeof buf) < 0)
522 return false;
524 /* Find the matching root menu or "create" it */
525 for (i = 0; i < menu_count; i++)
527 if (!strcasecmp(menus[i]->id, buf))
529 entry->link = i;
530 menus[i]->parent = menu;
531 return true;
535 return false;
538 if (entry->type != menu_next)
539 return false;
541 while (inst->tagorder_count < MAX_TAGS)
543 ret = get_tag(&inst->tagorder[inst->tagorder_count]);
544 if (ret < 0)
546 logf("Parse error #1");
547 logf("%s", strp);
548 return false;
551 if (ret == 0)
552 break ;
554 logf("tag: %d", inst->tagorder[inst->tagorder_count]);
556 while ( (ret = get_condition(inst)) > 0 ) ;
557 if (ret < 0)
558 return false;
560 inst->tagorder_count++;
562 if (get_tag(&type) <= 0 || type != menu_next)
563 break;
566 return true;
569 static int compare(const void *p1, const void *p2)
571 struct tagentry *e1 = (struct tagentry *)p1;
572 struct tagentry *e2 = (struct tagentry *)p2;
574 if (sort_inverse)
575 return strncasecmp(e2->name, e1->name, MAX_PATH);
577 return strncasecmp(e1->name, e2->name, MAX_PATH);
580 static void tagtree_buffer_event(struct mp3entry *id3, bool last_track)
582 (void)id3;
583 (void)last_track;
585 /* Do not gather data unless proper setting has been enabled. */
586 if (!global_settings.runtimedb)
587 return;
589 logf("be:%d%s", last_track, id3->path);
591 if (!tagcache_find_index(&tcs, id3->path))
593 logf("tc stat: not found: %s", id3->path);
594 return;
597 id3->playcount = tagcache_get_numeric(&tcs, tag_playcount);
598 id3->lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
599 id3->rating = tagcache_get_numeric(&tcs, tag_virt_autoscore) / 10;
601 tagcache_search_finish(&tcs);
604 static void tagtree_unbuffer_event(struct mp3entry *id3, bool last_track)
606 (void)last_track;
607 long playcount;
608 long playtime;
609 long lastplayed;
611 /* Do not gather data unless proper setting has been enabled. */
612 if (!global_settings.runtimedb)
614 logf("runtimedb gathering not enabled");
615 return;
618 /* Don't process unplayed tracks. */
619 if (id3->elapsed == 0)
621 logf("not logging unplayed track");
622 return;
625 if (!tagcache_find_index(&tcs, id3->path))
627 logf("tc stat: not found: %s", id3->path);
628 return;
631 playcount = tagcache_get_numeric(&tcs, tag_playcount);
632 playtime = tagcache_get_numeric(&tcs, tag_playtime);
633 lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
635 playcount++;
637 lastplayed = tagcache_increase_serial();
638 if (lastplayed < 0)
640 logf("incorrect tc serial:%d", lastplayed);
641 tagcache_search_finish(&tcs);
642 return;
645 /* Ignore the last 15s (crossfade etc.) */
646 playtime += MIN(id3->length, id3->elapsed + 15 * 1000);
648 logf("ube:%s", id3->path);
649 logf("-> %d/%d/%d", last_track, playcount, playtime);
650 logf("-> %d/%d/%d", id3->elapsed, id3->length, MIN(id3->length, id3->elapsed + 15 * 1000));
652 /* lastplayed not yet supported. */
654 if (!tagcache_modify_numeric_entry(&tcs, tag_playcount, playcount)
655 || !tagcache_modify_numeric_entry(&tcs, tag_playtime, playtime)
656 || !tagcache_modify_numeric_entry(&tcs, tag_lastplayed, lastplayed))
658 logf("tc stat: modify failed!");
659 tagcache_search_finish(&tcs);
660 return;
663 tagcache_search_finish(&tcs);
666 bool tagtree_export(void)
668 gui_syncsplash(0, true, str(LANG_CREATING));
669 if (!tagcache_create_changelog(&tcs))
671 gui_syncsplash(HZ*2, true, str(LANG_FAILED));
674 return false;
677 bool tagtree_import(void)
679 gui_syncsplash(0, true, str(LANG_WAIT));
680 if (!tagcache_import_changelog())
682 gui_syncsplash(HZ*2, true, str(LANG_FAILED));
685 return false;
688 static bool parse_menu(const char *filename);
690 int parse_line(int n, const char *buf, void *parameters)
692 char data[256];
693 int variable;
694 static bool read_menu;
695 int i;
697 (void)parameters;
699 logf("parse:%d/%s", n, buf);
701 /* First line, do initialisation. */
702 if (n == 0)
704 if (strcasecmp(TAGNAVI_VERSION, buf))
706 logf("Version mismatch");
707 return -1;
710 read_menu = false;
713 if (buf[0] == '#')
714 return 0;
716 if (buf[0] == '\0')
718 if (read_menu)
720 /* End the menu */
721 menu_count++;
722 read_menu = false;
724 return 0;
727 if (!read_menu)
729 strp = buf;
730 if (get_tag(&variable) <= 0)
731 return 0;
733 switch (variable)
735 case var_format:
736 if (add_format(strp) < 0)
738 logf("Format add fail: %s", data);
740 break;
742 case var_include:
743 if (get_token_str(data, sizeof(data)) < 0)
745 logf("%include empty");
746 return 0;
749 if (!parse_menu(data))
751 logf("Load menu fail: %s", data);
753 break;
755 case var_menu_start:
756 if (menu_count >= TAGMENU_MAX_MENUS)
758 logf("max menucount reached");
759 return 0;
762 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
763 menu = menus[menu_count];
764 memset(menu, 0, sizeof(struct root_menu));
765 if (get_token_str(menu->id, sizeof(menu->id)) < 0)
767 logf("%menu_start id empty");
768 return 0;
770 if (get_token_str(menu->title, sizeof(menu->title)) < 0)
772 logf("%menu_start title empty");
773 return 0;
775 logf("menu: %s", menu->title);
776 menu->itemcount = 0;
777 read_menu = true;
778 break;
780 case var_rootmenu:
781 /* Only set root menu once. */
782 if (root_menu)
783 break;
785 if (get_token_str(data, sizeof(data)) < 0)
787 logf("%root_menu empty");
788 return 0;
791 for (i = 0; i < menu_count; i++)
793 if (!strcasecmp(menus[i]->id, data))
795 root_menu = i;
798 break;
801 return 0;
804 if (menu->itemcount >= TAGMENU_MAX_ITEMS)
806 logf("max itemcount reached");
807 return 0;
810 /* Allocate */
811 if (menu->items[menu->itemcount] == NULL)
813 menu->items[menu->itemcount] = buffer_alloc(sizeof(struct menu_entry));
814 memset(menu->items[menu->itemcount], 0, sizeof(struct menu_entry));
815 menu->items[menu->itemcount]->si = buffer_alloc(sizeof(struct search_instruction));
818 memset(menu->items[menu->itemcount]->si, 0, sizeof(struct search_instruction));
819 if (!parse_search(menu->items[menu->itemcount], buf))
820 return 0;
822 menu->itemcount++;
824 return 0;
827 static bool parse_menu(const char *filename)
829 int fd;
830 char buf[1024];
832 if (menu_count >= TAGMENU_MAX_MENUS)
834 logf("max menucount reached");
835 return false;
838 fd = open(filename, O_RDONLY);
839 if (fd < 0)
841 logf("Search instruction file not found.");
842 return false;
845 /* Now read file for real, parsing into si */
846 fast_readline(fd, buf, sizeof buf, NULL, parse_line);
847 close(fd);
849 return true;
852 void tagtree_init(void)
854 format_count = 0;
855 menu_count = 0;
856 menu = NULL;
857 root_menu = 0;
858 parse_menu(FILE_SEARCH_INSTRUCTIONS);
860 uniqbuf = buffer_alloc(UNIQBUF_SIZE);
861 audio_set_track_buffer_event(tagtree_buffer_event);
862 audio_set_track_unbuffer_event(tagtree_unbuffer_event);
865 bool show_search_progress(bool init, int count)
867 static int last_tick = 0;
869 if (init)
871 last_tick = current_tick;
872 return true;
875 if (current_tick - last_tick > HZ/4)
877 gui_syncsplash(0, true, str(LANG_PLAYLIST_SEARCH_MSG), count,
878 #if CONFIG_KEYPAD == PLAYER_PAD
879 str(LANG_STOP_ABORT)
880 #else
881 str(LANG_OFF_ABORT)
882 #endif
884 if (action_userabort(TIMEOUT_NOBLOCK))
885 return false;
886 last_tick = current_tick;
887 yield();
890 return true;
893 int format_str(struct tagcache_search *tcs, struct display_format *fmt,
894 char *buf, int buf_size)
896 char fmtbuf[8];
897 bool read_format = false;
898 int fmtbuf_pos = 0;
899 int parpos = 0;
900 int buf_pos = 0;
901 int i;
903 memset(buf, 0, buf_size);
904 for (i = 0; fmt->formatstr[i] != '\0'; i++)
906 if (fmt->formatstr[i] == '%')
908 read_format = true;
909 fmtbuf_pos = 0;
910 if (parpos >= fmt->tag_count)
912 logf("too many format tags");
913 return -1;
917 if (read_format)
919 fmtbuf[fmtbuf_pos++] = fmt->formatstr[i];
920 if (fmtbuf_pos >= buf_size)
922 logf("format parse error");
923 return -2;
926 if (fmt->formatstr[i] == 's')
928 fmtbuf[fmtbuf_pos] = '\0';
929 read_format = false;
930 if (fmt->tags[parpos] == tcs->type)
932 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf, tcs->result);
934 else
936 /* Need to fetch the tag data. */
937 if (!tagcache_retrieve(tcs, tcs->idx_id, fmt->tags[parpos],
938 &buf[buf_pos], buf_size - buf_pos))
940 logf("retrieve failed");
941 return -3;
944 buf_pos += strlen(&buf[buf_pos]);
945 parpos++;
947 else if (fmt->formatstr[i] == 'd')
949 fmtbuf[fmtbuf_pos] = '\0';
950 read_format = false;
951 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf,
952 tagcache_get_numeric(tcs, fmt->tags[parpos]));
953 buf_pos += strlen(&buf[buf_pos]);
954 parpos++;
956 continue;
959 buf[buf_pos++] = fmt->formatstr[i];
961 if (buf_pos - 1 >= buf_size)
963 logf("buffer overflow");
964 return -4;
968 buf[buf_pos++] = '\0';
970 return 0;
973 int retrieve_entries(struct tree_context *c, struct tagcache_search *tcs,
974 int offset, bool init)
976 struct tagentry *dptr = (struct tagentry *)c->dircache;
977 struct display_format *fmt;
978 int i;
979 int namebufused = 0;
980 int total_count = 0;
981 int special_entry_count = 0;
982 int level = c->currextra;
983 int tag;
984 bool sort = false;
985 int sort_limit;
986 int strip;
988 if (init
989 #ifdef HAVE_TC_RAMCACHE
990 && !tagcache_is_ramcache()
991 #endif
994 show_search_progress(true, 0);
995 gui_syncsplash(0, true, str(LANG_PLAYLIST_SEARCH_MSG),
996 0, csi->name);
999 if (c->currtable == allsubentries)
1001 tag = tag_title;
1002 level--;
1004 else
1005 tag = csi->tagorder[level];
1007 if (!tagcache_search(tcs, tag))
1008 return -1;
1010 /* Prevent duplicate entries in the search list. */
1011 tagcache_search_set_uniqbuf(tcs, uniqbuf, UNIQBUF_SIZE);
1013 if (level || csi->clause_count[0] || tagcache_is_numeric_tag(tag))
1014 sort = true;
1016 for (i = 0; i < level; i++)
1018 if (tagcache_is_numeric_tag(csi->tagorder[i]))
1020 static struct tagcache_search_clause cc;
1022 memset(&cc, 0, sizeof(struct tagcache_search_clause));
1023 cc.tag = csi->tagorder[i];
1024 cc.type = clause_is;
1025 cc.numeric = true;
1026 cc.numeric_data = csi->result_seek[i];
1027 tagcache_search_add_clause(tcs, &cc);
1029 else
1031 tagcache_search_add_filter(tcs, csi->tagorder[i],
1032 csi->result_seek[i]);
1036 for (i = 0; i <= level; i++)
1038 int j;
1040 for (j = 0; j < csi->clause_count[i]; j++)
1041 tagcache_search_add_clause(tcs, csi->clause[i][j]);
1044 current_offset = offset;
1045 current_entry_count = 0;
1046 c->dirfull = false;
1048 fmt = NULL;
1049 for (i = 0; i < format_count; i++)
1051 if (formats[i]->group_id == csi->format_id[level])
1052 fmt = formats[i];
1055 if (fmt)
1057 sort_inverse = fmt->sort_inverse;
1058 sort_limit = fmt->limit;
1059 strip = fmt->strip;
1061 else
1063 sort_inverse = false;
1064 sort_limit = 0;
1065 strip = 0;
1068 if (tag != tag_title && tag != tag_filename)
1070 if (offset == 0)
1072 dptr->newtable = allsubentries;
1073 dptr->name = str(LANG_TAGNAVI_ALL_TRACKS);
1074 dptr++;
1075 current_entry_count++;
1077 special_entry_count++;
1080 total_count += special_entry_count;
1082 while (tagcache_get_next(tcs))
1084 if (total_count++ < offset)
1085 continue;
1087 dptr->newtable = navibrowse;
1088 if (tag == tag_title || tag == tag_filename)
1090 dptr->newtable = playtrack;
1091 dptr->extraseek = tcs->idx_id;
1093 else
1094 dptr->extraseek = tcs->result_seek;
1096 fmt = NULL;
1097 /* Check the format */
1098 for (i = 0; i < format_count; i++)
1100 if (formats[i]->group_id != csi->format_id[level])
1101 continue;
1103 if (tagcache_check_clauses(tcs, formats[i]->clause,
1104 formats[i]->clause_count))
1106 fmt = formats[i];
1107 break;
1111 if (!tcs->ramresult || fmt)
1113 char buf[MAX_PATH];
1115 if (fmt)
1117 if (format_str(tcs, fmt, buf, sizeof buf) < 0)
1119 logf("format_str() failed");
1120 return 0;
1124 dptr->name = &c->name_buffer[namebufused];
1125 if (fmt)
1126 namebufused += strlen(buf)+1;
1127 else
1128 namebufused += tcs->result_len;
1130 if (namebufused >= c->name_buffer_size)
1132 logf("chunk mode #2: %d", current_entry_count);
1133 c->dirfull = true;
1134 sort = false;
1135 break ;
1137 if (fmt)
1138 strcpy(dptr->name, buf);
1139 else
1140 strcpy(dptr->name, tcs->result);
1142 else
1143 dptr->name = tcs->result;
1145 dptr++;
1146 current_entry_count++;
1148 if (current_entry_count >= global_settings.max_files_in_dir)
1150 logf("chunk mode #3: %d", current_entry_count);
1151 c->dirfull = true;
1152 sort = false;
1153 break ;
1156 if (init && !tcs->ramsearch)
1158 if (!show_search_progress(false, i))
1160 tagcache_search_finish(tcs);
1161 return current_entry_count;
1166 if (sort)
1167 qsort(c->dircache + special_entry_count * c->dentry_size,
1168 current_entry_count - special_entry_count,
1169 c->dentry_size, compare);
1171 if (!init)
1173 tagcache_search_finish(tcs);
1174 return current_entry_count;
1177 while (tagcache_get_next(tcs))
1179 if (!tcs->ramsearch)
1181 if (!show_search_progress(false, total_count))
1182 break;
1184 total_count++;
1187 tagcache_search_finish(tcs);
1189 if (!sort && (sort_inverse || sort_limit))
1191 gui_syncsplash(HZ*4, true, str(LANG_SHOWDIR_BUFFER_FULL), total_count);
1192 logf("Too small dir buffer");
1193 return 0;
1196 if (sort_limit)
1197 total_count = MIN(total_count, sort_limit);
1199 if (strip)
1201 dptr = c->dircache;
1202 for (i = 0; i < total_count; i++, dptr++)
1204 int len = strlen(dptr->name);
1206 if (len < strip)
1207 continue;
1209 dptr->name = &dptr->name[strip];
1213 return total_count;
1216 static int load_root(struct tree_context *c)
1218 struct tagentry *dptr = (struct tagentry *)c->dircache;
1219 int i;
1221 tc = c;
1222 c->currtable = root;
1223 if (c->dirlevel == 0)
1224 c->currextra = root_menu;
1226 menu = menus[c->currextra];
1227 if (menu == NULL)
1228 return 0;
1230 for (i = 0; i < menu->itemcount; i++)
1232 dptr->name = menu->items[i]->name;
1233 switch (menu->items[i]->type)
1235 case menu_next:
1236 dptr->newtable = navibrowse;
1237 dptr->extraseek = i;
1238 break;
1240 case menu_load:
1241 dptr->newtable = root;
1242 dptr->extraseek = menu->items[i]->link;
1243 break;
1246 dptr++;
1249 current_offset = 0;
1250 current_entry_count = i;
1252 return i;
1255 int tagtree_load(struct tree_context* c)
1257 int count;
1258 int table = c->currtable;
1260 c->dentry_size = sizeof(struct tagentry);
1261 c->dirsindir = 0;
1263 if (!table)
1265 c->dirfull = false;
1266 table = root;
1267 c->currtable = table;
1268 c->currextra = root_menu;
1271 switch (table)
1273 case root:
1274 count = load_root(c);
1275 break;
1277 case allsubentries:
1278 case navibrowse:
1279 logf("navibrowse...");
1280 cpu_boost_id(true, CPUBOOSTID_TAGTREE);
1281 count = retrieve_entries(c, &tcs, 0, true);
1282 cpu_boost_id(false, CPUBOOSTID_TAGTREE);
1283 break;
1285 default:
1286 logf("Unsupported table %d\n", table);
1287 return -1;
1290 if (count < 0)
1292 c->dirlevel = 0;
1293 count = load_root(c);
1294 gui_syncsplash(HZ, true, str(LANG_TAGCACHE_BUSY));
1297 /* The _total_ numer of entries available. */
1298 c->dirlength = c->filesindir = count;
1300 return count;
1303 int tagtree_enter(struct tree_context* c)
1305 int rc = 0;
1306 struct tagentry *dptr;
1307 int newextra;
1308 int seek;
1310 dptr = tagtree_get_entry(c, c->selected_item);
1312 c->dirfull = false;
1313 newextra = dptr->newtable;
1314 seek = dptr->extraseek;
1316 if (c->dirlevel >= MAX_DIR_LEVELS)
1317 return 0;
1319 c->selected_item_history[c->dirlevel]=c->selected_item;
1320 c->table_history[c->dirlevel] = c->currtable;
1321 c->extra_history[c->dirlevel] = c->currextra;
1322 c->pos_history[c->dirlevel] = c->firstpos;
1323 c->dirlevel++;
1325 switch (c->currtable) {
1326 case root:
1327 c->currextra = newextra;
1329 if (newextra == root)
1331 menu = menus[seek];
1332 c->currextra = seek;
1335 else if (newextra == navibrowse)
1337 int i, j;
1339 csi = menu->items[seek]->si;
1340 c->currextra = 0;
1342 strncpy(current_title[c->currextra], dptr->name,
1343 sizeof(current_title[0]) - 1);
1345 /* Read input as necessary. */
1346 for (i = 0; i < csi->tagorder_count; i++)
1348 for (j = 0; j < csi->clause_count[i]; j++)
1350 if (!csi->clause[i][j]->input)
1351 continue;
1353 rc = kbd_input(searchstring, sizeof(searchstring));
1354 if (rc == -1 || !searchstring[0])
1356 tagtree_exit(c);
1357 return 0;
1360 if (csi->clause[i][j]->numeric)
1361 csi->clause[i][j]->numeric_data = atoi(searchstring);
1362 else
1363 csi->clause[i][j]->str = searchstring;
1367 c->currtable = newextra;
1369 break;
1371 case navibrowse:
1372 case allsubentries:
1373 if (newextra == playtrack)
1375 c->dirlevel--;
1376 /* about to create a new current playlist...
1377 allow user to cancel the operation */
1378 if (global_settings.warnon_erase_dynplaylist &&
1379 !global_settings.party_mode &&
1380 playlist_modified(NULL))
1382 char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
1383 struct text_message message={lines, 1};
1385 if (gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
1386 break;
1389 if (tagtree_play_folder(c) >= 0)
1390 rc = 2;
1391 break;
1394 c->currtable = newextra;
1395 csi->result_seek[c->currextra] = seek;
1396 if (c->currextra < csi->tagorder_count-1)
1397 c->currextra++;
1398 else
1399 c->dirlevel--;
1401 /* Update the statusbar title */
1402 strncpy(current_title[c->currextra], dptr->name,
1403 sizeof(current_title[0]) - 1);
1404 break;
1406 default:
1407 c->dirlevel--;
1408 break;
1411 c->selected_item=0;
1412 gui_synclist_select_item(&tree_lists, c->selected_item);
1414 return rc;
1417 void tagtree_exit(struct tree_context* c)
1419 c->dirfull = false;
1420 if (c->dirlevel > 0)
1421 c->dirlevel--;
1422 c->selected_item=c->selected_item_history[c->dirlevel];
1423 gui_synclist_select_item(&tree_lists, c->selected_item);
1424 c->currtable = c->table_history[c->dirlevel];
1425 c->currextra = c->extra_history[c->dirlevel];
1426 c->firstpos = c->pos_history[c->dirlevel];
1429 int tagtree_get_filename(struct tree_context* c, char *buf, int buflen)
1431 struct tagentry *entry;
1433 entry = tagtree_get_entry(c, c->selected_item);
1435 if (!tagcache_search(&tcs, tag_filename))
1436 return -1;
1438 if (!tagcache_retrieve(&tcs, entry->extraseek, tcs.type, buf, buflen))
1440 tagcache_search_finish(&tcs);
1441 return -2;
1444 tagcache_search_finish(&tcs);
1446 return 0;
1449 bool insert_all_playlist(struct tree_context *c, int position, bool queue)
1451 int i;
1452 char buf[MAX_PATH];
1453 int from, to, direction;
1454 int files_left = c->filesindir;
1456 cpu_boost_id(true, CPUBOOSTID_TAGTREE);
1457 if (!tagcache_search(&tcs, tag_filename))
1459 gui_syncsplash(HZ, true, str(LANG_TAGCACHE_BUSY));
1460 cpu_boost_id(false, CPUBOOSTID_TAGTREE);
1461 return false;
1464 if (position == PLAYLIST_INSERT_FIRST)
1466 from = c->filesindir - 1;
1467 to = -1;
1468 direction = -1;
1470 else
1472 from = 0;
1473 to = c->filesindir;
1474 direction = 1;
1477 for (i = from; i != to; i += direction)
1479 /* Count back to zero */
1480 if (!show_search_progress(false, files_left--))
1481 break;
1483 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, i)->extraseek,
1484 tcs.type, buf, sizeof buf))
1486 continue;
1489 if (playlist_insert_track(NULL, buf, position, queue, false) < 0)
1491 logf("playlist_insert_track failed");
1492 break;
1494 yield();
1496 playlist_sync(NULL);
1497 tagcache_search_finish(&tcs);
1498 cpu_boost_id(false, CPUBOOSTID_TAGTREE);
1500 return true;
1503 bool tagtree_insert_selection_playlist(int position, bool queue)
1505 struct tagentry *dptr;
1506 char buf[MAX_PATH];
1507 int dirlevel = tc->dirlevel;
1509 /* We need to set the table to allsubentries. */
1510 show_search_progress(true, 0);
1512 dptr = tagtree_get_entry(tc, tc->selected_item);
1514 /* Insert a single track? */
1515 if (dptr->newtable == playtrack)
1517 if (tagtree_get_filename(tc, buf, sizeof buf) < 0)
1519 logf("tagtree_get_filename failed");
1520 return false;
1522 playlist_insert_track(NULL, buf, position, queue, true);
1524 return true;
1527 if (dptr->newtable == navibrowse)
1529 tagtree_enter(tc);
1530 tagtree_load(tc);
1531 dptr = tagtree_get_entry(tc, tc->selected_item);
1533 else if (dptr->newtable != allsubentries)
1535 logf("unsupported table: %d", dptr->newtable);
1536 return false;
1539 /* Now the current table should be allsubentries. */
1540 if (dptr->newtable != playtrack)
1542 tagtree_enter(tc);
1543 tagtree_load(tc);
1544 dptr = tagtree_get_entry(tc, tc->selected_item);
1546 /* And now the newtable should be playtrack. */
1547 if (dptr->newtable != playtrack)
1549 logf("newtable: %d !!", dptr->newtable);
1550 tc->dirlevel = dirlevel;
1551 return false;
1555 if (tc->filesindir <= 0)
1556 gui_syncsplash(HZ, true, str(LANG_END_PLAYLIST_PLAYER));
1557 else
1559 logf("insert_all_playlist");
1560 if (!insert_all_playlist(tc, position, queue))
1561 gui_syncsplash(HZ*2, true, str(LANG_FAILED));
1564 /* Finally return the dirlevel to its original value. */
1565 while (tc->dirlevel > dirlevel)
1566 tagtree_exit(tc);
1567 tagtree_load(tc);
1569 return true;
1572 static int tagtree_play_folder(struct tree_context* c)
1574 if (playlist_create(NULL, NULL) < 0)
1576 logf("Failed creating playlist\n");
1577 return -1;
1580 if (!insert_all_playlist(c, PLAYLIST_INSERT_LAST, false))
1581 return -2;
1583 if (global_settings.playlist_shuffle)
1584 c->selected_item = playlist_shuffle(current_tick, c->selected_item);
1585 if (!global_settings.play_selected)
1586 c->selected_item = 0;
1587 gui_synclist_select_item(&tree_lists, c->selected_item);
1589 playlist_start(c->selected_item,0);
1591 return 0;
1594 struct tagentry* tagtree_get_entry(struct tree_context *c, int id)
1596 struct tagentry *entry = (struct tagentry *)c->dircache;
1597 int realid = id - current_offset;
1599 /* Load the next chunk if necessary. */
1600 if (realid >= current_entry_count || realid < 0)
1602 cpu_boost_id(true, CPUBOOSTID_TAGTREE);
1603 if (retrieve_entries(c, &tcs2, MAX(0, id - (current_entry_count / 2)),
1604 false) < 0)
1606 logf("retrieve failed");
1607 cpu_boost_id(false, CPUBOOSTID_TAGTREE);
1608 return NULL;
1610 realid = id - current_offset;
1611 cpu_boost_id(false, CPUBOOSTID_TAGTREE);
1614 return &entry[realid];
1617 char *tagtree_get_title(struct tree_context* c)
1619 switch (c->currtable)
1621 case root:
1622 return menu->title;
1624 case navibrowse:
1625 case allsubentries:
1626 return current_title[c->currextra];
1629 return "?";
1632 int tagtree_get_attr(struct tree_context* c)
1634 int attr = -1;
1635 switch (c->currtable)
1637 case navibrowse:
1638 if (csi->tagorder[c->currextra] == tag_title)
1639 attr = TREE_ATTR_MPA;
1640 else
1641 attr = ATTR_DIRECTORY;
1642 break;
1644 case allsubentries:
1645 attr = TREE_ATTR_MPA;
1646 break;
1648 default:
1649 attr = ATTR_DIRECTORY;
1650 break;
1653 return attr;
1656 #ifdef HAVE_LCD_BITMAP
1657 const unsigned char* tagtree_get_icon(struct tree_context* c)
1658 #else
1659 int tagtree_get_icon(struct tree_context* c)
1660 #endif
1662 int icon = Icon_Folder;
1664 if (tagtree_get_attr(c) == TREE_ATTR_MPA)
1665 icon = Icon_Audio;
1667 #ifdef HAVE_LCD_BITMAP
1668 return bitmap_icons_6x8[icon];
1669 #else
1670 return icon;
1671 #endif