Make TTS name conversion functions static members.
[Rockbox.git] / apps / tagtree.c
blob01d80077c2a9d834a19a73814562ea82277f7931
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"
48 #include "audio.h"
50 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
52 static int tagtree_play_folder(struct tree_context* c);
54 #define SEARCHSTR_SIZE 256
56 static const struct id3_to_search_mapping {
57 char *string;
58 size_t id3_offset;
59 } id3_to_search_mapping[] = {
60 { "", 0 }, /* offset n/a */
61 { "#directory#", 0 }, /* offset n/a */
62 { "#title#", offsetof(struct mp3entry, title) },
63 { "#artist#", offsetof(struct mp3entry, artist) },
64 { "#album#", offsetof(struct mp3entry, album) },
65 { "#genre#", offsetof(struct mp3entry, genre_string) },
66 { "#composer#", offsetof(struct mp3entry, composer) },
67 { "#albumartist#", offsetof(struct mp3entry, albumartist) },
69 enum variables {
70 var_sorttype = 100,
71 var_limit,
72 var_strip,
73 var_menu_start,
74 var_include,
75 var_rootmenu,
76 var_format,
77 menu_next,
78 menu_load,
81 /* Capacity 10 000 entries (for example 10k different artists) */
82 #define UNIQBUF_SIZE (64*1024)
83 static long *uniqbuf;
85 #define MAX_TAGS 5
86 #define MAX_MENU_ID_SIZE 32
88 static struct tagcache_search tcs, tcs2;
89 static bool sort_inverse;
92 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
94 * valid = true
95 * formatstr = "%-3d. %s"
96 * tags[0] = tag_autoscore
97 * tags[1] = tag_title
98 * tag_count = 2
100 * limit = 100
101 * sort_inverse = true
103 struct display_format {
104 char name[32];
105 struct tagcache_search_clause *clause[TAGCACHE_MAX_CLAUSES];
106 int clause_count;
107 char *formatstr;
108 int group_id;
109 int tags[MAX_TAGS];
110 int tag_count;
112 int limit;
113 int strip;
114 bool sort_inverse;
115 bool sort;
118 static struct display_format *formats[TAGMENU_MAX_FMTS];
119 static int format_count;
121 struct search_instruction {
122 char name[64];
123 int tagorder[MAX_TAGS];
124 int tagorder_count;
125 struct tagcache_search_clause *clause[MAX_TAGS][TAGCACHE_MAX_CLAUSES];
126 int format_id[MAX_TAGS];
127 int clause_count[MAX_TAGS];
128 int result_seek[MAX_TAGS];
131 struct menu_entry {
132 char name[64];
133 int type;
134 struct search_instruction *si;
135 int link;
138 struct root_menu {
139 char title[64];
140 char id[MAX_MENU_ID_SIZE];
141 int itemcount;
142 struct menu_entry *items[TAGMENU_MAX_ITEMS];
145 /* Statusbar text of the current view. */
146 static char current_title[MAX_TAGS][128];
148 static struct root_menu *menus[TAGMENU_MAX_MENUS];
149 static struct root_menu *menu;
150 static struct search_instruction *csi;
151 static const char *strp;
152 static int menu_count;
153 static int root_menu;
155 static int current_offset;
156 static int current_entry_count;
158 static int format_count;
159 static struct tree_context *tc;
161 static int get_token_str(char *buf, int size)
163 /* Find the start. */
164 while (*strp != '"' && *strp != '\0')
165 strp++;
167 if (*strp == '\0' || *(++strp) == '\0')
168 return -1;
170 /* Read the data. */
171 while (*strp != '"' && *strp != '\0' && --size > 0)
172 *(buf++) = *(strp++);
174 *buf = '\0';
175 if (*strp != '"')
176 return -2;
178 strp++;
180 return 0;
183 #define MATCH(tag,str1,str2,settag) \
184 if (!strcasecmp(str1, str2)) { \
185 *tag = settag; \
186 return 1; \
189 static int get_tag(int *tag)
191 char buf[128];
192 int i;
194 /* Find the start. */
195 while ((*strp == ' ' || *strp == '>') && *strp != '\0')
196 strp++;
198 if (*strp == '\0' || *strp == '?')
199 return 0;
201 for (i = 0; i < (int)sizeof(buf)-1; i++)
203 if (*strp == '\0' || *strp == ' ')
204 break ;
205 buf[i] = *strp;
206 strp++;
208 buf[i] = '\0';
210 MATCH(tag, buf, "album", tag_album);
211 MATCH(tag, buf, "artist", tag_artist);
212 MATCH(tag, buf, "bitrate", tag_bitrate);
213 MATCH(tag, buf, "composer", tag_composer);
214 MATCH(tag, buf, "comment", tag_comment);
215 MATCH(tag, buf, "albumartist", tag_albumartist);
216 MATCH(tag, buf, "ensemble", tag_albumartist);
217 MATCH(tag, buf, "grouping", tag_grouping);
218 MATCH(tag, buf, "genre", tag_genre);
219 MATCH(tag, buf, "length", tag_length);
220 MATCH(tag, buf, "Lm", tag_virt_length_min);
221 MATCH(tag, buf, "Ls", tag_virt_length_sec);
222 MATCH(tag, buf, "Pm", tag_virt_playtime_min);
223 MATCH(tag, buf, "Ps", tag_virt_playtime_sec);
224 MATCH(tag, buf, "title", tag_title);
225 MATCH(tag, buf, "filename", tag_filename);
226 MATCH(tag, buf, "tracknum", tag_tracknumber);
227 MATCH(tag, buf, "discnum", tag_discnumber);
228 MATCH(tag, buf, "year", tag_year);
229 MATCH(tag, buf, "playcount", tag_playcount);
230 MATCH(tag, buf, "rating", tag_rating);
231 MATCH(tag, buf, "lastplayed", tag_lastplayed);
232 MATCH(tag, buf, "commitid", tag_commitid);
233 MATCH(tag, buf, "entryage", tag_virt_entryage);
234 MATCH(tag, buf, "autoscore", tag_virt_autoscore);
235 MATCH(tag, buf, "%sort", var_sorttype);
236 MATCH(tag, buf, "%limit", var_limit);
237 MATCH(tag, buf, "%strip", var_strip);
238 MATCH(tag, buf, "%menu_start", var_menu_start);
239 MATCH(tag, buf, "%include", var_include);
240 MATCH(tag, buf, "%root_menu", var_rootmenu);
241 MATCH(tag, buf, "%format", var_format);
242 MATCH(tag, buf, "->", menu_next);
243 MATCH(tag, buf, "==>", menu_load);
245 logf("NO MATCH: %s\n", buf);
246 if (buf[0] == '?')
247 return 0;
249 return -1;
252 static int get_clause(int *condition)
254 char buf[4];
255 int i;
257 /* Find the start. */
258 while (*strp == ' ' && *strp != '\0')
259 strp++;
261 if (*strp == '\0')
262 return 0;
264 for (i = 0; i < (int)sizeof(buf)-1; i++)
266 if (*strp == '\0' || *strp == ' ')
267 break ;
268 buf[i] = *strp;
269 strp++;
271 buf[i] = '\0';
273 MATCH(condition, buf, "=", clause_is);
274 MATCH(condition, buf, "==", clause_is);
275 MATCH(condition, buf, "!=", clause_is_not);
276 MATCH(condition, buf, ">", clause_gt);
277 MATCH(condition, buf, ">=", clause_gteq);
278 MATCH(condition, buf, "<", clause_lt);
279 MATCH(condition, buf, "<=", clause_lteq);
280 MATCH(condition, buf, "~", clause_contains);
281 MATCH(condition, buf, "!~", clause_not_contains);
282 MATCH(condition, buf, "^", clause_begins_with);
283 MATCH(condition, buf, "!^", clause_not_begins_with);
284 MATCH(condition, buf, "$", clause_ends_with);
285 MATCH(condition, buf, "!$", clause_not_ends_with);
286 MATCH(condition, buf, "@", clause_oneof);
288 return 0;
291 static bool read_clause(struct tagcache_search_clause *clause)
293 char buf[SEARCHSTR_SIZE];
294 unsigned int i;
296 if (get_tag(&clause->tag) <= 0)
297 return false;
299 if (get_clause(&clause->type) <= 0)
300 return false;
302 if (get_token_str(buf, sizeof buf) < 0)
303 return false;
305 for (i=0; i<ARRAYLEN(id3_to_search_mapping); i++)
307 if (!strcasecmp(buf, id3_to_search_mapping[i].string))
308 break;
311 if (i<ARRAYLEN(id3_to_search_mapping)) /* runtime search operand found */
313 clause->source = source_runtime+i;
314 clause->str = buffer_alloc(SEARCHSTR_SIZE);
316 else
318 clause->source = source_constant;
319 clause->str = buffer_alloc(strlen(buf)+1);
320 strcpy(clause->str, buf);
323 if (tagcache_is_numeric_tag(clause->tag))
325 clause->numeric = true;
326 clause->numeric_data = atoi(clause->str);
328 else
329 clause->numeric = false;
331 logf("got clause: %d/%d [%s]", clause->tag, clause->type, clause->str);
333 return true;
336 static bool read_variable(char *buf, int size)
338 int condition;
340 if (!get_clause(&condition))
341 return false;
343 if (condition != clause_is)
344 return false;
346 if (get_token_str(buf, size) < 0)
347 return false;
349 return true;
352 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
353 static int get_format_str(struct display_format *fmt)
355 int ret;
356 char buf[128];
357 int i;
359 memset(fmt, 0, sizeof(struct display_format));
361 if (get_token_str(fmt->name, sizeof fmt->name) < 0)
362 return -10;
364 /* Determine the group id */
365 fmt->group_id = 0;
366 for (i = 0; i < format_count; i++)
368 if (!strcasecmp(formats[i]->name, fmt->name))
370 fmt->group_id = formats[i]->group_id;
371 break;
374 if (formats[i]->group_id > fmt->group_id)
375 fmt->group_id = formats[i]->group_id;
378 if (i == format_count)
379 fmt->group_id++;
381 logf("format: (%d) %s", fmt->group_id, fmt->name);
383 if (get_token_str(buf, sizeof buf) < 0)
384 return -10;
386 fmt->formatstr = buffer_alloc(strlen(buf) + 1);
387 strcpy(fmt->formatstr, buf);
389 while (fmt->tag_count < MAX_TAGS)
391 ret = get_tag(&fmt->tags[fmt->tag_count]);
392 if (ret < 0)
393 return -11;
395 if (ret == 0)
396 break;
398 switch (fmt->tags[fmt->tag_count]) {
399 case var_sorttype:
400 if (!read_variable(buf, sizeof buf))
401 return -12;
402 if (!strcasecmp("inverse", buf))
403 fmt->sort_inverse = true;
405 fmt->sort = true;
406 break;
408 case var_limit:
409 if (!read_variable(buf, sizeof buf))
410 return -13;
411 fmt->limit = atoi(buf);
412 break;
414 case var_strip:
415 if (!read_variable(buf, sizeof buf))
416 return -14;
417 fmt->strip = atoi(buf);
418 break;
420 default:
421 fmt->tag_count++;
425 return 1;
428 static int add_format(const char *buf)
430 strp = buf;
432 if (formats[format_count] == NULL)
433 formats[format_count] = buffer_alloc(sizeof(struct display_format));
435 memset(formats[format_count], 0, sizeof(struct display_format));
436 if (get_format_str(formats[format_count]) < 0)
438 logf("get_format_str() parser failed!");
439 return -4;
442 while (*strp != '\0' && *strp != '?')
443 strp++;
445 if (*strp == '?')
447 int clause_count = 0;
448 strp++;
450 while (1)
452 if (clause_count >= TAGCACHE_MAX_CLAUSES)
454 logf("too many clauses");
455 break;
458 formats[format_count]->clause[clause_count] =
459 buffer_alloc(sizeof(struct tagcache_search_clause));
461 if (!read_clause(formats[format_count]->clause[clause_count]))
462 break;
464 clause_count++;
467 formats[format_count]->clause_count = clause_count;
470 format_count++;
472 return 1;
475 static int get_condition(struct search_instruction *inst)
477 int clause_count;
478 char buf[128];
480 switch (*strp)
482 case '=':
484 int i;
486 if (get_token_str(buf, sizeof buf) < 0)
487 return -1;
489 for (i = 0; i < format_count; i++)
491 if (!strcasecmp(formats[i]->name, buf))
492 break;
495 if (i == format_count)
497 logf("format not found: %s", buf);
498 return -2;
501 inst->format_id[inst->tagorder_count] = formats[i]->group_id;
502 return 1;
504 case '?':
505 case ' ':
506 case '&':
507 strp++;
508 return 1;
509 case '-':
510 case '\0':
511 return 0;
514 clause_count = inst->clause_count[inst->tagorder_count];
515 if (clause_count >= TAGCACHE_MAX_CLAUSES)
517 logf("Too many clauses");
518 return false;
521 inst->clause[inst->tagorder_count][clause_count] =
522 buffer_alloc(sizeof(struct tagcache_search_clause));
524 if (!read_clause(inst->clause[inst->tagorder_count][clause_count]))
525 return -1;
527 inst->clause_count[inst->tagorder_count]++;
529 return 1;
532 /* example search:
533 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
534 * : album ? year >= "2000" : songs
535 * ^ begins with
536 * * contains
537 * $ ends with
540 static bool parse_search(struct menu_entry *entry, const char *str)
542 int ret;
543 int type;
544 struct search_instruction *inst = entry->si;
545 char buf[MAX_PATH];
546 int i;
547 struct root_menu *new_menu;
549 strp = str;
551 /* Parse entry name */
552 if (get_token_str(entry->name, sizeof entry->name) < 0)
554 logf("No name found.");
555 return false;
558 /* Parse entry type */
559 if (get_tag(&entry->type) <= 0)
560 return false;
562 if (entry->type == menu_load)
564 if (get_token_str(buf, sizeof buf) < 0)
565 return false;
567 /* Find the matching root menu or "create" it */
568 for (i = 0; i < menu_count; i++)
570 if (!strcasecmp(menus[i]->id, buf))
572 entry->link = i;
573 return true;
577 if (menu_count >= TAGMENU_MAX_MENUS)
579 logf("max menucount reached");
580 return false;
583 /* Allocate a new menu unless link is found. */
584 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
585 new_menu = menus[menu_count];
586 memset(new_menu, 0, sizeof(struct root_menu));
587 strncpy(new_menu->id, buf, MAX_MENU_ID_SIZE-1);
588 entry->link = menu_count;
589 ++menu_count;
591 return true;
594 if (entry->type != menu_next)
595 return false;
597 while (inst->tagorder_count < MAX_TAGS)
599 ret = get_tag(&inst->tagorder[inst->tagorder_count]);
600 if (ret < 0)
602 logf("Parse error #1");
603 logf("%s", strp);
604 return false;
607 if (ret == 0)
608 break ;
610 logf("tag: %d", inst->tagorder[inst->tagorder_count]);
612 while ( (ret = get_condition(inst)) > 0 ) ;
613 if (ret < 0)
614 return false;
616 inst->tagorder_count++;
618 if (get_tag(&type) <= 0 || type != menu_next)
619 break;
622 return true;
625 static int compare(const void *p1, const void *p2)
627 struct tagentry *e1 = (struct tagentry *)p1;
628 struct tagentry *e2 = (struct tagentry *)p2;
630 if (sort_inverse)
631 return strncasecmp(e2->name, e1->name, MAX_PATH);
633 return strncasecmp(e1->name, e2->name, MAX_PATH);
636 static void tagtree_buffer_event(struct mp3entry *id3)
638 /* Do not gather data unless proper setting has been enabled. */
639 if (!global_settings.runtimedb)
640 return;
642 logf("be:%s", id3->path);
644 if (!tagcache_find_index(&tcs, id3->path))
646 logf("tc stat: not found: %s", id3->path);
647 return;
650 id3->playcount = tagcache_get_numeric(&tcs, tag_playcount);
651 if (!id3->rating)
652 id3->rating = tagcache_get_numeric(&tcs, tag_rating);
653 id3->lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
654 id3->score = tagcache_get_numeric(&tcs, tag_virt_autoscore) / 10;
655 id3->playtime = tagcache_get_numeric(&tcs, tag_playtime);
657 /* Store our tagcache index pointer. */
658 id3->tagcache_idx = tcs.idx_id+1;
660 tagcache_search_finish(&tcs);
663 static void tagtree_unbuffer_event(struct mp3entry *id3)
665 long playcount;
666 long playtime;
667 long lastplayed;
668 long tagcache_idx;
670 /* Do not gather data unless proper setting has been enabled. */
671 if (!global_settings.runtimedb)
673 logf("runtimedb gathering not enabled");
674 return;
677 tagcache_idx=id3->tagcache_idx;
678 if (!tagcache_idx)
680 logf("No tagcache index pointer found");
681 return;
683 tagcache_idx--;
685 /* Don't process unplayed tracks. */
686 if (id3->elapsed == 0)
688 logf("not logging unplayed track");
689 return;
692 playcount = id3->playcount + 1;
693 lastplayed = tagcache_increase_serial();
694 if (lastplayed < 0)
696 logf("incorrect tc serial:%ld", lastplayed);
697 return;
700 /* Ignore the last 15s (crossfade etc.) */
701 playtime = id3->playtime + MIN(id3->length, id3->elapsed + 15 * 1000);
703 logf("ube:%s", id3->path);
704 logf("-> %ld/%ld", playcount, playtime);
705 logf("-> %ld/%ld/%ld", id3->elapsed, id3->length, MIN(id3->length, id3->elapsed + 15 * 1000));
707 /* Queue the updates to the tagcache system. */
708 tagcache_update_numeric(tagcache_idx, tag_playcount, playcount);
709 tagcache_update_numeric(tagcache_idx, tag_playtime, playtime);
710 tagcache_update_numeric(tagcache_idx, tag_lastplayed, lastplayed);
713 bool tagtree_export(void)
715 gui_syncsplash(0, str(LANG_CREATING));
716 if (!tagcache_create_changelog(&tcs))
718 gui_syncsplash(HZ*2, ID2P(LANG_FAILED));
721 return false;
724 bool tagtree_import(void)
726 gui_syncsplash(0, ID2P(LANG_WAIT));
727 if (!tagcache_import_changelog())
729 gui_syncsplash(HZ*2, ID2P(LANG_FAILED));
732 return false;
735 static bool parse_menu(const char *filename);
737 static int parse_line(int n, const char *buf, void *parameters)
739 char data[256];
740 int variable;
741 static bool read_menu;
742 int i;
744 (void)parameters;
746 logf("parse:%d/%s", n, buf);
748 /* First line, do initialisation. */
749 if (n == 0)
751 if (strcasecmp(TAGNAVI_VERSION, buf))
753 logf("Version mismatch");
754 return -1;
757 read_menu = false;
760 if (buf[0] == '#')
761 return 0;
763 if (buf[0] == '\0')
765 if (read_menu)
767 /* End the menu */
768 read_menu = false;
770 return 0;
773 if (!read_menu)
775 strp = buf;
776 if (get_tag(&variable) <= 0)
777 return 0;
779 switch (variable)
781 case var_format:
782 if (add_format(strp) < 0)
784 logf("Format add fail: %s", data);
786 break;
788 case var_include:
789 if (get_token_str(data, sizeof(data)) < 0)
791 logf("%%include empty");
792 return 0;
795 if (!parse_menu(data))
797 logf("Load menu fail: %s", data);
799 break;
801 case var_menu_start:
802 if (menu_count >= TAGMENU_MAX_MENUS)
804 logf("max menucount reached");
805 return 0;
808 if (get_token_str(data, sizeof data) < 0)
810 logf("%%menu_start id empty");
811 return 0;
814 menu = NULL;
815 for (i = 0; i < menu_count; i++)
817 if (!strcasecmp(menus[i]->id, data))
819 menu = menus[i];
823 if (menu == NULL)
825 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
826 menu = menus[menu_count];
827 ++menu_count;
828 memset(menu, 0, sizeof(struct root_menu));
829 strncpy(menu->id, data, MAX_MENU_ID_SIZE-1);
832 if (get_token_str(menu->title, sizeof(menu->title)) < 0)
834 logf("%%menu_start title empty");
835 return 0;
837 logf("menu: %s", menu->title);
838 read_menu = true;
839 break;
841 case var_rootmenu:
842 /* Only set root menu once. */
843 if (root_menu >= 0)
844 break;
846 if (get_token_str(data, sizeof(data)) < 0)
848 logf("%%root_menu empty");
849 return 0;
852 for (i = 0; i < menu_count; i++)
854 if (!strcasecmp(menus[i]->id, data))
856 root_menu = i;
859 break;
862 return 0;
865 if (menu->itemcount >= TAGMENU_MAX_ITEMS)
867 logf("max itemcount reached");
868 return 0;
871 /* Allocate */
872 if (menu->items[menu->itemcount] == NULL)
874 menu->items[menu->itemcount] = buffer_alloc(sizeof(struct menu_entry));
875 memset(menu->items[menu->itemcount], 0, sizeof(struct menu_entry));
876 menu->items[menu->itemcount]->si = buffer_alloc(sizeof(struct search_instruction));
879 memset(menu->items[menu->itemcount]->si, 0, sizeof(struct search_instruction));
880 if (!parse_search(menu->items[menu->itemcount], buf))
881 return 0;
883 menu->itemcount++;
885 return 0;
888 static bool parse_menu(const char *filename)
890 int fd;
891 char buf[1024];
893 if (menu_count >= TAGMENU_MAX_MENUS)
895 logf("max menucount reached");
896 return false;
899 fd = open(filename, O_RDONLY);
900 if (fd < 0)
902 logf("Search instruction file not found.");
903 return false;
906 /* Now read file for real, parsing into si */
907 fast_readline(fd, buf, sizeof buf, NULL, parse_line);
908 close(fd);
910 return true;
913 void tagtree_init(void)
915 format_count = 0;
916 menu_count = 0;
917 menu = NULL;
918 root_menu = -1;
919 parse_menu(FILE_SEARCH_INSTRUCTIONS);
921 /* If no root menu is set, assume it's the first single menu
922 * we have. That shouldn't normally happen. */
923 if (root_menu < 0)
924 root_menu = 0;
926 uniqbuf = buffer_alloc(UNIQBUF_SIZE);
927 audio_set_track_buffer_event(tagtree_buffer_event);
928 audio_set_track_unbuffer_event(tagtree_unbuffer_event);
931 static bool show_search_progress(bool init, int count)
933 static int last_tick = 0;
935 if (init)
937 last_tick = current_tick;
938 return true;
941 if (current_tick - last_tick > HZ/4)
943 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG), count,
944 str(LANG_OFF_ABORT));
945 if (action_userabort(TIMEOUT_NOBLOCK))
946 return false;
947 last_tick = current_tick;
948 yield();
951 return true;
954 static int format_str(struct tagcache_search *tcs, struct display_format *fmt,
955 char *buf, int buf_size)
957 char fmtbuf[8];
958 bool read_format = false;
959 int fmtbuf_pos = 0;
960 int parpos = 0;
961 int buf_pos = 0;
962 int i;
964 memset(buf, 0, buf_size);
965 for (i = 0; fmt->formatstr[i] != '\0'; i++)
967 if (fmt->formatstr[i] == '%')
969 read_format = true;
970 fmtbuf_pos = 0;
971 if (parpos >= fmt->tag_count)
973 logf("too many format tags");
974 return -1;
978 if (read_format)
980 fmtbuf[fmtbuf_pos++] = fmt->formatstr[i];
981 if (fmtbuf_pos >= buf_size)
983 logf("format parse error");
984 return -2;
987 if (fmt->formatstr[i] == 's')
989 fmtbuf[fmtbuf_pos] = '\0';
990 read_format = false;
991 if (fmt->tags[parpos] == tcs->type)
993 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf, tcs->result);
995 else
997 /* Need to fetch the tag data. */
998 if (!tagcache_retrieve(tcs, tcs->idx_id, fmt->tags[parpos],
999 &buf[buf_pos], buf_size - buf_pos))
1001 logf("retrieve failed");
1002 return -3;
1005 buf_pos += strlen(&buf[buf_pos]);
1006 parpos++;
1008 else if (fmt->formatstr[i] == 'd')
1010 fmtbuf[fmtbuf_pos] = '\0';
1011 read_format = false;
1012 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf,
1013 tagcache_get_numeric(tcs, fmt->tags[parpos]));
1014 buf_pos += strlen(&buf[buf_pos]);
1015 parpos++;
1017 continue;
1020 buf[buf_pos++] = fmt->formatstr[i];
1022 if (buf_pos - 1 >= buf_size)
1024 logf("buffer overflow");
1025 return -4;
1029 buf[buf_pos++] = '\0';
1031 return 0;
1034 static int retrieve_entries(struct tree_context *c, struct tagcache_search *tcs,
1035 int offset, bool init)
1037 struct tagentry *dptr = (struct tagentry *)c->dircache;
1038 struct display_format *fmt;
1039 int i;
1040 int namebufused = 0;
1041 int total_count = 0;
1042 int special_entry_count = 0;
1043 int level = c->currextra;
1044 int tag;
1045 bool sort = false;
1046 int sort_limit;
1047 int strip;
1049 if (init
1050 #ifdef HAVE_TC_RAMCACHE
1051 && !tagcache_is_ramcache()
1052 #endif
1055 show_search_progress(true, 0);
1056 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG),
1057 0, csi->name);
1060 if (c->currtable == allsubentries)
1062 tag = tag_title;
1063 level--;
1065 else
1066 tag = csi->tagorder[level];
1068 if (!tagcache_search(tcs, tag))
1069 return -1;
1071 /* Prevent duplicate entries in the search list. */
1072 tagcache_search_set_uniqbuf(tcs, uniqbuf, UNIQBUF_SIZE);
1074 if (level || csi->clause_count[0] || tagcache_is_numeric_tag(tag))
1075 sort = true;
1077 for (i = 0; i < level; i++)
1079 if (tagcache_is_numeric_tag(csi->tagorder[i]))
1081 static struct tagcache_search_clause cc;
1083 memset(&cc, 0, sizeof(struct tagcache_search_clause));
1084 cc.tag = csi->tagorder[i];
1085 cc.type = clause_is;
1086 cc.numeric = true;
1087 cc.numeric_data = csi->result_seek[i];
1088 tagcache_search_add_clause(tcs, &cc);
1090 else
1092 tagcache_search_add_filter(tcs, csi->tagorder[i],
1093 csi->result_seek[i]);
1097 for (i = 0; i <= level; i++)
1099 int j;
1101 for (j = 0; j < csi->clause_count[i]; j++)
1102 tagcache_search_add_clause(tcs, csi->clause[i][j]);
1105 current_offset = offset;
1106 current_entry_count = 0;
1107 c->dirfull = false;
1109 fmt = NULL;
1110 for (i = 0; i < format_count; i++)
1112 if (formats[i]->group_id == csi->format_id[level])
1113 fmt = formats[i];
1116 if (fmt)
1118 sort_inverse = fmt->sort_inverse;
1119 sort_limit = fmt->limit;
1120 strip = fmt->strip;
1122 /* Check if sorting is forced. */
1123 if (fmt->sort)
1124 sort = true;
1126 else
1128 sort_inverse = false;
1129 sort_limit = 0;
1130 strip = 0;
1133 if (tag != tag_title && tag != tag_filename)
1135 if (offset == 0)
1137 dptr->newtable = allsubentries;
1138 dptr->name = str(LANG_TAGNAVI_ALL_TRACKS);
1139 dptr++;
1140 current_entry_count++;
1142 if (offset <= 1)
1144 dptr->newtable = navibrowse;
1145 dptr->name = str(LANG_TAGNAVI_RANDOM);
1146 dptr->extraseek = -1;
1147 dptr++;
1148 current_entry_count++;
1150 special_entry_count+=2;
1153 total_count += special_entry_count;
1155 while (tagcache_get_next(tcs))
1157 if (total_count++ < offset)
1158 continue;
1160 dptr->newtable = navibrowse;
1161 if (tag == tag_title || tag == tag_filename)
1163 dptr->newtable = playtrack;
1164 dptr->extraseek = tcs->idx_id;
1166 else
1167 dptr->extraseek = tcs->result_seek;
1169 fmt = NULL;
1170 /* Check the format */
1171 for (i = 0; i < format_count; i++)
1173 if (formats[i]->group_id != csi->format_id[level])
1174 continue;
1176 if (tagcache_check_clauses(tcs, formats[i]->clause,
1177 formats[i]->clause_count))
1179 fmt = formats[i];
1180 break;
1184 if (!tcs->ramresult || fmt)
1186 char buf[MAX_PATH];
1188 if (fmt)
1190 if (format_str(tcs, fmt, buf, sizeof buf) < 0)
1192 logf("format_str() failed");
1193 return 0;
1197 dptr->name = &c->name_buffer[namebufused];
1198 if (fmt)
1199 namebufused += strlen(buf)+1;
1200 else
1201 namebufused += tcs->result_len;
1203 if (namebufused >= c->name_buffer_size)
1205 logf("chunk mode #2: %d", current_entry_count);
1206 c->dirfull = true;
1207 sort = false;
1208 break ;
1210 if (fmt)
1211 strcpy(dptr->name, buf);
1212 else
1213 strcpy(dptr->name, tcs->result);
1215 else
1216 dptr->name = tcs->result;
1218 dptr++;
1219 current_entry_count++;
1221 if (current_entry_count >= global_settings.max_files_in_dir)
1223 logf("chunk mode #3: %d", current_entry_count);
1224 c->dirfull = true;
1225 sort = false;
1226 break ;
1229 if (init && !tcs->ramsearch)
1231 if (!show_search_progress(false, i))
1233 tagcache_search_finish(tcs);
1234 return current_entry_count;
1239 if (sort)
1240 qsort(c->dircache + special_entry_count * c->dentry_size,
1241 current_entry_count - special_entry_count,
1242 c->dentry_size, compare);
1244 if (!init)
1246 tagcache_search_finish(tcs);
1247 return current_entry_count;
1250 while (tagcache_get_next(tcs))
1252 if (!tcs->ramsearch)
1254 if (!show_search_progress(false, total_count))
1255 break;
1257 total_count++;
1260 tagcache_search_finish(tcs);
1262 if (!sort && (sort_inverse || sort_limit))
1264 gui_syncsplash(HZ*4, ID2P(LANG_SHOWDIR_BUFFER_FULL), total_count);
1265 logf("Too small dir buffer");
1266 return 0;
1269 if (sort_limit)
1270 total_count = MIN(total_count, sort_limit);
1272 if (strip)
1274 dptr = c->dircache;
1275 for (i = 0; i < total_count; i++, dptr++)
1277 int len = strlen(dptr->name);
1279 if (len < strip)
1280 continue;
1282 dptr->name = &dptr->name[strip];
1286 return total_count;
1289 static int load_root(struct tree_context *c)
1291 struct tagentry *dptr = (struct tagentry *)c->dircache;
1292 int i;
1294 tc = c;
1295 c->currtable = root;
1296 if (c->dirlevel == 0)
1297 c->currextra = root_menu;
1299 menu = menus[c->currextra];
1300 if (menu == NULL)
1301 return 0;
1303 for (i = 0; i < menu->itemcount; i++)
1305 dptr->name = menu->items[i]->name;
1306 switch (menu->items[i]->type)
1308 case menu_next:
1309 dptr->newtable = navibrowse;
1310 dptr->extraseek = i;
1311 break;
1313 case menu_load:
1314 dptr->newtable = root;
1315 dptr->extraseek = menu->items[i]->link;
1316 break;
1319 dptr++;
1322 current_offset = 0;
1323 current_entry_count = i;
1325 return i;
1328 int tagtree_load(struct tree_context* c)
1330 int count;
1331 int table = c->currtable;
1333 c->dentry_size = sizeof(struct tagentry);
1334 c->dirsindir = 0;
1336 if (!table)
1338 c->dirfull = false;
1339 table = root;
1340 c->currtable = table;
1341 c->currextra = root_menu;
1344 switch (table)
1346 case root:
1347 count = load_root(c);
1348 break;
1350 case allsubentries:
1351 case navibrowse:
1352 logf("navibrowse...");
1353 cpu_boost(true);
1354 count = retrieve_entries(c, &tcs, 0, true);
1355 cpu_boost(false);
1356 break;
1358 default:
1359 logf("Unsupported table %d\n", table);
1360 return -1;
1363 if (count < 0)
1365 c->dirlevel = 0;
1366 count = load_root(c);
1367 gui_syncsplash(HZ, str(LANG_TAGCACHE_BUSY));
1370 /* The _total_ numer of entries available. */
1371 c->dirlength = c->filesindir = count;
1373 return count;
1376 int tagtree_enter(struct tree_context* c)
1378 int rc = 0;
1379 struct tagentry *dptr;
1380 struct mp3entry *id3;
1381 int newextra;
1382 int seek;
1383 int source;
1385 dptr = tagtree_get_entry(c, c->selected_item);
1387 c->dirfull = false;
1388 seek = dptr->extraseek;
1389 if (seek == -1)
1391 if(c->filesindir<=2)
1392 return 0;
1393 srand(current_tick);
1394 dptr = (tagtree_get_entry(c, 2+(rand() % (c->filesindir-2))));
1395 seek = dptr->extraseek;
1397 newextra = dptr->newtable;
1399 if (c->dirlevel >= MAX_DIR_LEVELS)
1400 return 0;
1402 c->selected_item_history[c->dirlevel]=c->selected_item;
1403 c->table_history[c->dirlevel] = c->currtable;
1404 c->extra_history[c->dirlevel] = c->currextra;
1405 c->pos_history[c->dirlevel] = c->firstpos;
1406 c->dirlevel++;
1408 switch (c->currtable) {
1409 case root:
1410 c->currextra = newextra;
1412 if (newextra == root)
1414 menu = menus[seek];
1415 c->currextra = seek;
1418 else if (newextra == navibrowse)
1420 int i, j;
1422 csi = menu->items[seek]->si;
1423 c->currextra = 0;
1425 strncpy(current_title[c->currextra], dptr->name,
1426 sizeof(current_title[0]) - 1);
1428 /* Read input as necessary. */
1429 for (i = 0; i < csi->tagorder_count; i++)
1431 for (j = 0; j < csi->clause_count[i]; j++)
1433 char* searchstring;
1434 source = csi->clause[i][j]->source;
1436 if (source == source_constant)
1437 continue;
1439 searchstring=csi->clause[i][j]->str;
1440 *searchstring = '\0';
1442 id3 = audio_current_track();
1444 if (source == source_current_path && id3)
1446 char *e;
1447 strncpy(searchstring, id3->path, SEARCHSTR_SIZE);
1448 e = strrchr(searchstring, '/');
1449 if (e)
1450 *e = '\0';
1452 else if (source > source_runtime && id3)
1455 int k = source-source_runtime;
1456 int offset = id3_to_search_mapping[k].id3_offset;
1457 char **src = (char**)((char*)id3 + offset);
1458 if (*src)
1460 strncpy(searchstring, *src, SEARCHSTR_SIZE);
1461 searchstring[SEARCHSTR_SIZE-1] = '\0';
1464 else
1466 rc = kbd_input(searchstring, SEARCHSTR_SIZE);
1467 if (rc == -1 || !searchstring[0])
1469 tagtree_exit(c);
1470 return 0;
1472 if (csi->clause[i][j]->numeric)
1473 csi->clause[i][j]->numeric_data = atoi(searchstring);
1480 c->currtable = newextra;
1482 break;
1484 case navibrowse:
1485 case allsubentries:
1486 if (newextra == playtrack)
1488 c->dirlevel--;
1489 /* about to create a new current playlist...
1490 allow user to cancel the operation */
1491 if (global_settings.warnon_erase_dynplaylist &&
1492 !global_settings.party_mode &&
1493 playlist_modified(NULL))
1495 char *lines[]={ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
1496 struct text_message message={lines, 1};
1498 if (gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
1499 break;
1502 if (tagtree_play_folder(c) >= 0)
1503 rc = 2;
1504 break;
1507 c->currtable = newextra;
1508 csi->result_seek[c->currextra] = seek;
1509 if (c->currextra < csi->tagorder_count-1)
1510 c->currextra++;
1511 else
1512 c->dirlevel--;
1514 /* Update the statusbar title */
1515 strncpy(current_title[c->currextra], dptr->name,
1516 sizeof(current_title[0]) - 1);
1517 break;
1519 default:
1520 c->dirlevel--;
1521 break;
1524 c->selected_item=0;
1525 gui_synclist_select_item(&tree_lists, c->selected_item);
1527 return rc;
1530 void tagtree_exit(struct tree_context* c)
1532 c->dirfull = false;
1533 if (c->dirlevel > 0)
1534 c->dirlevel--;
1535 c->selected_item=c->selected_item_history[c->dirlevel];
1536 gui_synclist_select_item(&tree_lists, c->selected_item);
1537 c->currtable = c->table_history[c->dirlevel];
1538 c->currextra = c->extra_history[c->dirlevel];
1539 c->firstpos = c->pos_history[c->dirlevel];
1542 int tagtree_get_filename(struct tree_context* c, char *buf, int buflen)
1544 struct tagentry *entry;
1546 entry = tagtree_get_entry(c, c->selected_item);
1548 if (!tagcache_search(&tcs, tag_filename))
1549 return -1;
1551 if (!tagcache_retrieve(&tcs, entry->extraseek, tcs.type, buf, buflen))
1553 tagcache_search_finish(&tcs);
1554 return -2;
1557 tagcache_search_finish(&tcs);
1559 return 0;
1562 static bool insert_all_playlist(struct tree_context *c, int position, bool queue)
1564 int i;
1565 char buf[MAX_PATH];
1566 int from, to, direction;
1567 int files_left = c->filesindir;
1569 cpu_boost(true);
1570 if (!tagcache_search(&tcs, tag_filename))
1572 gui_syncsplash(HZ, ID2P(LANG_TAGCACHE_BUSY));
1573 cpu_boost(false);
1574 return false;
1577 if (position == PLAYLIST_REPLACE)
1579 if (remove_all_tracks(NULL) == 0)
1580 position = PLAYLIST_INSERT_LAST;
1581 else return -1; }
1583 if (position == PLAYLIST_INSERT_FIRST)
1585 from = c->filesindir - 1;
1586 to = -1;
1587 direction = -1;
1589 else
1591 from = 0;
1592 to = c->filesindir;
1593 direction = 1;
1596 for (i = from; i != to; i += direction)
1598 /* Count back to zero */
1599 if (!show_search_progress(false, files_left--))
1600 break;
1602 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, i)->extraseek,
1603 tcs.type, buf, sizeof buf))
1605 continue;
1608 if (playlist_insert_track(NULL, buf, position, queue, false) < 0)
1610 logf("playlist_insert_track failed");
1611 break;
1613 yield();
1615 playlist_sync(NULL);
1616 tagcache_search_finish(&tcs);
1617 cpu_boost(false);
1619 return true;
1622 bool tagtree_insert_selection_playlist(int position, bool queue)
1624 struct tagentry *dptr;
1625 char buf[MAX_PATH];
1626 int dirlevel = tc->dirlevel;
1628 /* We need to set the table to allsubentries. */
1629 show_search_progress(true, 0);
1631 dptr = tagtree_get_entry(tc, tc->selected_item);
1633 /* Insert a single track? */
1634 if (dptr->newtable == playtrack)
1636 if (tagtree_get_filename(tc, buf, sizeof buf) < 0)
1638 logf("tagtree_get_filename failed");
1639 return false;
1641 playlist_insert_track(NULL, buf, position, queue, true);
1643 return true;
1646 if (dptr->newtable == navibrowse)
1648 tagtree_enter(tc);
1649 tagtree_load(tc);
1650 dptr = tagtree_get_entry(tc, tc->selected_item);
1652 else if (dptr->newtable != allsubentries)
1654 logf("unsupported table: %d", dptr->newtable);
1655 return false;
1658 /* Now the current table should be allsubentries. */
1659 if (dptr->newtable != playtrack)
1661 tagtree_enter(tc);
1662 tagtree_load(tc);
1663 dptr = tagtree_get_entry(tc, tc->selected_item);
1665 /* And now the newtable should be playtrack. */
1666 if (dptr->newtable != playtrack)
1668 logf("newtable: %d !!", dptr->newtable);
1669 tc->dirlevel = dirlevel;
1670 return false;
1674 if (tc->filesindir <= 0)
1675 gui_syncsplash(HZ, ID2P(LANG_END_PLAYLIST));
1676 else
1678 logf("insert_all_playlist");
1679 if (!insert_all_playlist(tc, position, queue))
1680 gui_syncsplash(HZ*2, ID2P(LANG_FAILED));
1683 /* Finally return the dirlevel to its original value. */
1684 while (tc->dirlevel > dirlevel)
1685 tagtree_exit(tc);
1686 tagtree_load(tc);
1688 return true;
1691 static int tagtree_play_folder(struct tree_context* c)
1693 if (playlist_create(NULL, NULL) < 0)
1695 logf("Failed creating playlist\n");
1696 return -1;
1699 if (!insert_all_playlist(c, PLAYLIST_INSERT_LAST, false))
1700 return -2;
1702 if (global_settings.playlist_shuffle)
1703 c->selected_item = playlist_shuffle(current_tick, c->selected_item);
1704 if (!global_settings.play_selected)
1705 c->selected_item = 0;
1706 gui_synclist_select_item(&tree_lists, c->selected_item);
1708 playlist_start(c->selected_item,0);
1710 return 0;
1713 struct tagentry* tagtree_get_entry(struct tree_context *c, int id)
1715 struct tagentry *entry = (struct tagentry *)c->dircache;
1716 int realid = id - current_offset;
1718 /* Load the next chunk if necessary. */
1719 if (realid >= current_entry_count || realid < 0)
1721 cpu_boost(true);
1722 if (retrieve_entries(c, &tcs2, MAX(0, id - (current_entry_count / 2)),
1723 false) < 0)
1725 logf("retrieve failed");
1726 cpu_boost(false);
1727 return NULL;
1729 realid = id - current_offset;
1730 cpu_boost(false);
1733 return &entry[realid];
1736 char *tagtree_get_title(struct tree_context* c)
1738 switch (c->currtable)
1740 case root:
1741 return menu->title;
1743 case navibrowse:
1744 case allsubentries:
1745 return current_title[c->currextra];
1748 return "?";
1751 int tagtree_get_attr(struct tree_context* c)
1753 int attr = -1;
1754 switch (c->currtable)
1756 case navibrowse:
1757 if (csi->tagorder[c->currextra] == tag_title)
1758 attr = FILE_ATTR_AUDIO;
1759 else
1760 attr = ATTR_DIRECTORY;
1761 break;
1763 case allsubentries:
1764 attr = FILE_ATTR_AUDIO;
1765 break;
1767 default:
1768 attr = ATTR_DIRECTORY;
1769 break;
1772 return attr;
1775 int tagtree_get_icon(struct tree_context* c)
1777 int icon = Icon_Folder;
1779 if (tagtree_get_attr(c) == FILE_ATTR_AUDIO)
1780 icon = Icon_Audio;
1782 return icon;