rbutil: fix voice download for some targets. (again naming issues)
[kugel-rb.git] / apps / tagtree.c
blob9635052ef33df7f05e28f2a7ecd2226c70d6ffb5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 /**
23 * Basic structure on this file was copied from dbtree.c and modified to
24 * support the tag cache interface.
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include "config.h"
30 #include "system.h"
31 #include "kernel.h"
32 #include "splash.h"
33 #include "icons.h"
34 #include "tree.h"
35 #include "action.h"
36 #include "settings.h"
37 #include "tagcache.h"
38 #include "tagtree.h"
39 #include "lang.h"
40 #include "logf.h"
41 #include "playlist.h"
42 #include "keyboard.h"
43 #include "gui/list.h"
44 #include "buffer.h"
45 #include "playback.h"
46 #include "yesno.h"
47 #include "misc.h"
48 #include "filetypes.h"
49 #include "audio.h"
50 #include "appevents.h"
51 #include "storage.h"
52 #include "dir_uncached.h"
54 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
56 static int tagtree_play_folder(struct tree_context* c);
58 #define SEARCHSTR_SIZE 256
60 enum table {
61 ROOT = 1,
62 NAVIBROWSE,
63 ALLSUBENTRIES,
64 PLAYTRACK,
67 static const struct id3_to_search_mapping {
68 char *string;
69 size_t id3_offset;
70 } id3_to_search_mapping[] = {
71 { "", 0 }, /* offset n/a */
72 { "#directory#", 0 }, /* offset n/a */
73 { "#title#", offsetof(struct mp3entry, title) },
74 { "#artist#", offsetof(struct mp3entry, artist) },
75 { "#album#", offsetof(struct mp3entry, album) },
76 { "#genre#", offsetof(struct mp3entry, genre_string) },
77 { "#composer#", offsetof(struct mp3entry, composer) },
78 { "#albumartist#", offsetof(struct mp3entry, albumartist) },
80 enum variables {
81 var_sorttype = 100,
82 var_limit,
83 var_strip,
84 var_menu_start,
85 var_include,
86 var_rootmenu,
87 var_format,
88 menu_next,
89 menu_load,
92 /* Capacity 10 000 entries (for example 10k different artists) */
93 #define UNIQBUF_SIZE (64*1024)
94 static long *uniqbuf;
96 #define MAX_TAGS 5
97 #define MAX_MENU_ID_SIZE 32
99 static struct tagcache_search tcs, tcs2;
100 static bool sort_inverse;
103 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
105 * valid = true
106 * formatstr = "%-3d. %s"
107 * tags[0] = tag_autoscore
108 * tags[1] = tag_title
109 * tag_count = 2
111 * limit = 100
112 * sort_inverse = true
114 struct display_format {
115 char name[32];
116 struct tagcache_search_clause *clause[TAGCACHE_MAX_CLAUSES];
117 int clause_count;
118 char *formatstr;
119 int group_id;
120 int tags[MAX_TAGS];
121 int tag_count;
123 int limit;
124 int strip;
125 bool sort_inverse;
128 static struct display_format *formats[TAGMENU_MAX_FMTS];
129 static int format_count;
131 struct search_instruction {
132 char name[64];
133 int tagorder[MAX_TAGS];
134 int tagorder_count;
135 struct tagcache_search_clause *clause[MAX_TAGS][TAGCACHE_MAX_CLAUSES];
136 int format_id[MAX_TAGS];
137 int clause_count[MAX_TAGS];
138 int result_seek[MAX_TAGS];
141 struct menu_entry {
142 char name[64];
143 int type;
144 struct search_instruction *si;
145 int link;
148 struct menu_root {
149 char title[64];
150 char id[MAX_MENU_ID_SIZE];
151 int itemcount;
152 struct menu_entry *items[TAGMENU_MAX_ITEMS];
155 /* Statusbar text of the current view. */
156 static char current_title[MAX_TAGS][128];
158 static struct menu_root *menus[TAGMENU_MAX_MENUS];
159 static struct menu_root *menu;
160 static struct search_instruction *csi;
161 static const char *strp;
162 static int menu_count;
163 static int rootmenu;
165 static int current_offset;
166 static int current_entry_count;
168 static struct tree_context *tc;
170 static int get_token_str(char *buf, int size)
172 /* Find the start. */
173 while (*strp != '"' && *strp != '\0')
174 strp++;
176 if (*strp == '\0' || *(++strp) == '\0')
177 return -1;
179 /* Read the data. */
180 while (*strp != '"' && *strp != '\0' && --size > 0)
181 *(buf++) = *(strp++);
183 *buf = '\0';
184 if (*strp != '"')
185 return -2;
187 strp++;
189 return 0;
192 #define MATCH(tag,str1,str2,settag) \
193 if (!strcasecmp(str1, str2)) { \
194 *tag = settag; \
195 return 1; \
198 static int get_tag(int *tag)
200 char buf[128];
201 int i;
203 /* Find the start. */
204 while ((*strp == ' ' || *strp == '>') && *strp != '\0')
205 strp++;
207 if (*strp == '\0' || *strp == '?')
208 return 0;
210 for (i = 0; i < (int)sizeof(buf)-1; i++)
212 if (*strp == '\0' || *strp == ' ')
213 break ;
214 buf[i] = *strp;
215 strp++;
217 buf[i] = '\0';
219 MATCH(tag, buf, "album", tag_album);
220 MATCH(tag, buf, "artist", tag_artist);
221 MATCH(tag, buf, "bitrate", tag_bitrate);
222 MATCH(tag, buf, "composer", tag_composer);
223 MATCH(tag, buf, "comment", tag_comment);
224 MATCH(tag, buf, "albumartist", tag_albumartist);
225 MATCH(tag, buf, "ensemble", tag_albumartist);
226 MATCH(tag, buf, "grouping", tag_grouping);
227 MATCH(tag, buf, "genre", tag_genre);
228 MATCH(tag, buf, "length", tag_length);
229 MATCH(tag, buf, "Lm", tag_virt_length_min);
230 MATCH(tag, buf, "Ls", tag_virt_length_sec);
231 MATCH(tag, buf, "Pm", tag_virt_playtime_min);
232 MATCH(tag, buf, "Ps", tag_virt_playtime_sec);
233 MATCH(tag, buf, "title", tag_title);
234 MATCH(tag, buf, "filename", tag_filename);
235 MATCH(tag, buf, "tracknum", tag_tracknumber);
236 MATCH(tag, buf, "discnum", tag_discnumber);
237 MATCH(tag, buf, "year", tag_year);
238 MATCH(tag, buf, "playcount", tag_playcount);
239 MATCH(tag, buf, "rating", tag_rating);
240 MATCH(tag, buf, "lastplayed", tag_lastplayed);
241 MATCH(tag, buf, "commitid", tag_commitid);
242 MATCH(tag, buf, "entryage", tag_virt_entryage);
243 MATCH(tag, buf, "autoscore", tag_virt_autoscore);
244 MATCH(tag, buf, "%sort", var_sorttype);
245 MATCH(tag, buf, "%limit", var_limit);
246 MATCH(tag, buf, "%strip", var_strip);
247 MATCH(tag, buf, "%menu_start", var_menu_start);
248 MATCH(tag, buf, "%include", var_include);
249 MATCH(tag, buf, "%root_menu", var_rootmenu);
250 MATCH(tag, buf, "%format", var_format);
251 MATCH(tag, buf, "->", menu_next);
252 MATCH(tag, buf, "==>", menu_load);
254 logf("NO MATCH: %s\n", buf);
255 if (buf[0] == '?')
256 return 0;
258 return -1;
261 static int get_clause(int *condition)
263 char buf[4];
264 int i;
266 /* Find the start. */
267 while (*strp == ' ' && *strp != '\0')
268 strp++;
270 if (*strp == '\0')
271 return 0;
273 for (i = 0; i < (int)sizeof(buf)-1; i++)
275 if (*strp == '\0' || *strp == ' ')
276 break ;
277 buf[i] = *strp;
278 strp++;
280 buf[i] = '\0';
282 MATCH(condition, buf, "=", clause_is);
283 MATCH(condition, buf, "==", clause_is);
284 MATCH(condition, buf, "!=", clause_is_not);
285 MATCH(condition, buf, ">", clause_gt);
286 MATCH(condition, buf, ">=", clause_gteq);
287 MATCH(condition, buf, "<", clause_lt);
288 MATCH(condition, buf, "<=", clause_lteq);
289 MATCH(condition, buf, "~", clause_contains);
290 MATCH(condition, buf, "!~", clause_not_contains);
291 MATCH(condition, buf, "^", clause_begins_with);
292 MATCH(condition, buf, "!^", clause_not_begins_with);
293 MATCH(condition, buf, "$", clause_ends_with);
294 MATCH(condition, buf, "!$", clause_not_ends_with);
295 MATCH(condition, buf, "@", clause_oneof);
297 return 0;
300 static bool read_clause(struct tagcache_search_clause *clause)
302 char buf[SEARCHSTR_SIZE];
303 unsigned int i;
305 if (get_tag(&clause->tag) <= 0)
306 return false;
308 if (get_clause(&clause->type) <= 0)
309 return false;
311 if (get_token_str(buf, sizeof buf) < 0)
312 return false;
314 for (i=0; i<ARRAYLEN(id3_to_search_mapping); i++)
316 if (!strcasecmp(buf, id3_to_search_mapping[i].string))
317 break;
320 if (i<ARRAYLEN(id3_to_search_mapping)) /* runtime search operand found */
322 clause->source = source_runtime+i;
323 clause->str = buffer_alloc(SEARCHSTR_SIZE);
325 else
327 clause->source = source_constant;
328 clause->str = buffer_alloc(strlen(buf)+1);
329 strcpy(clause->str, buf);
332 if (TAGCACHE_IS_NUMERIC(clause->tag))
334 clause->numeric = true;
335 clause->numeric_data = atoi(clause->str);
337 else
338 clause->numeric = false;
340 logf("got clause: %d/%d [%s]", clause->tag, clause->type, clause->str);
342 return true;
345 static bool read_variable(char *buf, int size)
347 int condition;
349 if (!get_clause(&condition))
350 return false;
352 if (condition != clause_is)
353 return false;
355 if (get_token_str(buf, size) < 0)
356 return false;
358 return true;
361 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
362 static int get_format_str(struct display_format *fmt)
364 int ret;
365 char buf[128];
366 int i;
368 memset(fmt, 0, sizeof(struct display_format));
370 if (get_token_str(fmt->name, sizeof fmt->name) < 0)
371 return -10;
373 /* Determine the group id */
374 fmt->group_id = 0;
375 for (i = 0; i < format_count; i++)
377 if (!strcasecmp(formats[i]->name, fmt->name))
379 fmt->group_id = formats[i]->group_id;
380 break;
383 if (formats[i]->group_id > fmt->group_id)
384 fmt->group_id = formats[i]->group_id;
387 if (i == format_count)
388 fmt->group_id++;
390 logf("format: (%d) %s", fmt->group_id, fmt->name);
392 if (get_token_str(buf, sizeof buf) < 0)
393 return -10;
395 fmt->formatstr = buffer_alloc(strlen(buf) + 1);
396 strcpy(fmt->formatstr, buf);
398 while (fmt->tag_count < MAX_TAGS)
400 ret = get_tag(&fmt->tags[fmt->tag_count]);
401 if (ret < 0)
402 return -11;
404 if (ret == 0)
405 break;
407 switch (fmt->tags[fmt->tag_count]) {
408 case var_sorttype:
409 if (!read_variable(buf, sizeof buf))
410 return -12;
411 if (!strcasecmp("inverse", buf))
412 fmt->sort_inverse = true;
413 break;
415 case var_limit:
416 if (!read_variable(buf, sizeof buf))
417 return -13;
418 fmt->limit = atoi(buf);
419 break;
421 case var_strip:
422 if (!read_variable(buf, sizeof buf))
423 return -14;
424 fmt->strip = atoi(buf);
425 break;
427 default:
428 fmt->tag_count++;
432 return 1;
435 static int add_format(const char *buf)
437 strp = buf;
439 if (formats[format_count] == NULL)
440 formats[format_count] = buffer_alloc(sizeof(struct display_format));
442 memset(formats[format_count], 0, sizeof(struct display_format));
443 if (get_format_str(formats[format_count]) < 0)
445 logf("get_format_str() parser failed!");
446 return -4;
449 while (*strp != '\0' && *strp != '?')
450 strp++;
452 if (*strp == '?')
454 int clause_count = 0;
455 strp++;
457 while (1)
459 if (clause_count >= TAGCACHE_MAX_CLAUSES)
461 logf("too many clauses");
462 break;
465 formats[format_count]->clause[clause_count] =
466 buffer_alloc(sizeof(struct tagcache_search_clause));
468 if (!read_clause(formats[format_count]->clause[clause_count]))
469 break;
471 clause_count++;
474 formats[format_count]->clause_count = clause_count;
477 format_count++;
479 return 1;
482 static int get_condition(struct search_instruction *inst)
484 int clause_count;
485 char buf[128];
487 switch (*strp)
489 case '=':
491 int i;
493 if (get_token_str(buf, sizeof buf) < 0)
494 return -1;
496 for (i = 0; i < format_count; i++)
498 if (!strcasecmp(formats[i]->name, buf))
499 break;
502 if (i == format_count)
504 logf("format not found: %s", buf);
505 return -2;
508 inst->format_id[inst->tagorder_count] = formats[i]->group_id;
509 return 1;
511 case '?':
512 case ' ':
513 case '&':
514 strp++;
515 return 1;
516 case '-':
517 case '\0':
518 return 0;
521 clause_count = inst->clause_count[inst->tagorder_count];
522 if (clause_count >= TAGCACHE_MAX_CLAUSES)
524 logf("Too many clauses");
525 return false;
528 inst->clause[inst->tagorder_count][clause_count] =
529 buffer_alloc(sizeof(struct tagcache_search_clause));
531 if (!read_clause(inst->clause[inst->tagorder_count][clause_count]))
532 return -1;
534 inst->clause_count[inst->tagorder_count]++;
536 return 1;
539 /* example search:
540 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
541 * : album ? year >= "2000" : songs
542 * ^ begins with
543 * * contains
544 * $ ends with
547 static bool parse_search(struct menu_entry *entry, const char *str)
549 int ret;
550 int type;
551 struct search_instruction *inst = entry->si;
552 char buf[MAX_PATH];
553 int i;
554 struct menu_root *new_menu;
556 strp = str;
558 /* Parse entry name */
559 if (get_token_str(entry->name, sizeof entry->name) < 0)
561 logf("No name found.");
562 return false;
565 /* Parse entry type */
566 if (get_tag(&entry->type) <= 0)
567 return false;
569 if (entry->type == menu_load)
571 if (get_token_str(buf, sizeof buf) < 0)
572 return false;
574 /* Find the matching root menu or "create" it */
575 for (i = 0; i < menu_count; i++)
577 if (!strcasecmp(menus[i]->id, buf))
579 entry->link = i;
580 return true;
584 if (menu_count >= TAGMENU_MAX_MENUS)
586 logf("max menucount reached");
587 return false;
590 /* Allocate a new menu unless link is found. */
591 menus[menu_count] = buffer_alloc(sizeof(struct menu_root));
592 new_menu = menus[menu_count];
593 memset(new_menu, 0, sizeof(struct menu_root));
594 strncpy(new_menu->id, buf, MAX_MENU_ID_SIZE-1);
595 entry->link = menu_count;
596 ++menu_count;
598 return true;
601 if (entry->type != menu_next)
602 return false;
604 while (inst->tagorder_count < MAX_TAGS)
606 ret = get_tag(&inst->tagorder[inst->tagorder_count]);
607 if (ret < 0)
609 logf("Parse error #1");
610 logf("%s", strp);
611 return false;
614 if (ret == 0)
615 break ;
617 logf("tag: %d", inst->tagorder[inst->tagorder_count]);
619 while ( (ret = get_condition(inst)) > 0 ) ;
620 if (ret < 0)
621 return false;
623 inst->tagorder_count++;
625 if (get_tag(&type) <= 0 || type != menu_next)
626 break;
629 return true;
632 static int compare(const void *p1, const void *p2)
634 struct tagentry *e1 = (struct tagentry *)p1;
635 struct tagentry *e2 = (struct tagentry *)p2;
637 if (sort_inverse)
638 return strncasecmp(e2->name, e1->name, MAX_PATH);
640 return strncasecmp(e1->name, e2->name, MAX_PATH);
643 static void tagtree_buffer_event(struct mp3entry *id3)
645 /* Do not gather data unless proper setting has been enabled. */
646 if (!global_settings.runtimedb)
647 return;
649 logf("be:%s", id3->path);
651 if (!tagcache_find_index(&tcs, id3->path))
653 logf("tc stat: not found: %s", id3->path);
654 return;
657 id3->playcount = tagcache_get_numeric(&tcs, tag_playcount);
658 if (!id3->rating)
659 id3->rating = tagcache_get_numeric(&tcs, tag_rating);
660 id3->lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
661 id3->score = tagcache_get_numeric(&tcs, tag_virt_autoscore) / 10;
662 id3->playtime = tagcache_get_numeric(&tcs, tag_playtime);
664 /* Store our tagcache index pointer. */
665 id3->tagcache_idx = tcs.idx_id+1;
667 tagcache_search_finish(&tcs);
670 static void tagtree_track_finish_event(struct mp3entry *id3)
672 long playcount;
673 long playtime;
674 long lastplayed;
675 long tagcache_idx;
677 /* Do not gather data unless proper setting has been enabled. */
678 if (!global_settings.runtimedb)
680 logf("runtimedb gathering not enabled");
681 return;
684 tagcache_idx=id3->tagcache_idx;
685 if (!tagcache_idx)
687 logf("No tagcache index pointer found");
688 return;
690 tagcache_idx--;
692 /* Don't process unplayed tracks. */
693 if (id3->elapsed == 0)
695 logf("not logging unplayed track");
696 return;
699 playcount = id3->playcount + 1;
700 lastplayed = tagcache_increase_serial();
701 if (lastplayed < 0)
703 logf("incorrect tc serial:%ld", lastplayed);
704 return;
707 /* Ignore the last 15s (crossfade etc.) */
708 playtime = id3->playtime + MIN(id3->length, id3->elapsed + 15 * 1000);
710 logf("ube:%s", id3->path);
711 logf("-> %ld/%ld", playcount, playtime);
712 logf("-> %ld/%ld/%ld", id3->elapsed, id3->length, MIN(id3->length, id3->elapsed + 15 * 1000));
714 /* Queue the updates to the tagcache system. */
715 tagcache_update_numeric(tagcache_idx, tag_playcount, playcount);
716 tagcache_update_numeric(tagcache_idx, tag_playtime, playtime);
717 tagcache_update_numeric(tagcache_idx, tag_lastplayed, lastplayed);
720 bool tagtree_export(void)
722 splash(0, str(LANG_CREATING));
723 if (!tagcache_create_changelog(&tcs))
725 splash(HZ*2, ID2P(LANG_FAILED));
728 return false;
731 bool tagtree_import(void)
733 splash(0, ID2P(LANG_WAIT));
734 if (!tagcache_import_changelog())
736 splash(HZ*2, ID2P(LANG_FAILED));
739 return false;
742 static bool parse_menu(const char *filename);
744 static int parse_line(int n, const char *buf, void *parameters)
746 char data[256];
747 int variable;
748 static bool read_menu;
749 int i;
751 (void)parameters;
753 logf("parse:%d/%s", n, buf);
755 /* First line, do initialisation. */
756 if (n == 0)
758 if (strcasecmp(TAGNAVI_VERSION, buf))
760 logf("Version mismatch");
761 return -1;
764 read_menu = false;
767 if (buf[0] == '#')
768 return 0;
770 if (buf[0] == '\0')
772 if (read_menu)
774 /* End the menu */
775 read_menu = false;
777 return 0;
780 if (!read_menu)
782 strp = buf;
783 if (get_tag(&variable) <= 0)
784 return 0;
786 switch (variable)
788 case var_format:
789 if (add_format(strp) < 0)
791 logf("Format add fail: %s", data);
793 break;
795 case var_include:
796 if (get_token_str(data, sizeof(data)) < 0)
798 logf("%%include empty");
799 return 0;
802 if (!parse_menu(data))
804 logf("Load menu fail: %s", data);
806 break;
808 case var_menu_start:
809 if (menu_count >= TAGMENU_MAX_MENUS)
811 logf("max menucount reached");
812 return 0;
815 if (get_token_str(data, sizeof data) < 0)
817 logf("%%menu_start id empty");
818 return 0;
821 menu = NULL;
822 for (i = 0; i < menu_count; i++)
824 if (!strcasecmp(menus[i]->id, data))
826 menu = menus[i];
830 if (menu == NULL)
832 menus[menu_count] = buffer_alloc(sizeof(struct menu_root));
833 menu = menus[menu_count];
834 ++menu_count;
835 memset(menu, 0, sizeof(struct menu_root));
836 strncpy(menu->id, data, MAX_MENU_ID_SIZE-1);
839 if (get_token_str(menu->title, sizeof(menu->title)) < 0)
841 logf("%%menu_start title empty");
842 return 0;
844 logf("menu: %s", menu->title);
845 read_menu = true;
846 break;
848 case var_rootmenu:
849 /* Only set root menu once. */
850 if (rootmenu >= 0)
851 break;
853 if (get_token_str(data, sizeof(data)) < 0)
855 logf("%%rootmenu empty");
856 return 0;
859 for (i = 0; i < menu_count; i++)
861 if (!strcasecmp(menus[i]->id, data))
863 rootmenu = i;
866 break;
869 return 0;
872 if (menu->itemcount >= TAGMENU_MAX_ITEMS)
874 logf("max itemcount reached");
875 return 0;
878 /* Allocate */
879 if (menu->items[menu->itemcount] == NULL)
881 menu->items[menu->itemcount] = buffer_alloc(sizeof(struct menu_entry));
882 memset(menu->items[menu->itemcount], 0, sizeof(struct menu_entry));
883 menu->items[menu->itemcount]->si = buffer_alloc(sizeof(struct search_instruction));
886 memset(menu->items[menu->itemcount]->si, 0, sizeof(struct search_instruction));
887 if (!parse_search(menu->items[menu->itemcount], buf))
888 return 0;
890 menu->itemcount++;
892 return 0;
895 static bool parse_menu(const char *filename)
897 int fd;
898 char buf[1024];
900 if (menu_count >= TAGMENU_MAX_MENUS)
902 logf("max menucount reached");
903 return false;
906 fd = open(filename, O_RDONLY);
907 if (fd < 0)
909 logf("Search instruction file not found.");
910 return false;
913 /* Now read file for real, parsing into si */
914 fast_readline(fd, buf, sizeof buf, NULL, parse_line);
915 close(fd);
917 return true;
920 void tagtree_init(void)
922 format_count = 0;
923 menu_count = 0;
924 menu = NULL;
925 rootmenu = -1;
926 parse_menu(FILE_SEARCH_INSTRUCTIONS);
928 /* If no root menu is set, assume it's the first single menu
929 * we have. That shouldn't normally happen. */
930 if (rootmenu < 0)
931 rootmenu = 0;
933 uniqbuf = buffer_alloc(UNIQBUF_SIZE);
935 add_event(PLAYBACK_EVENT_TRACK_BUFFER, false, tagtree_buffer_event);
936 add_event(PLAYBACK_EVENT_TRACK_FINISH, false, tagtree_track_finish_event);
939 static bool show_search_progress(bool init, int count)
941 static int last_tick = 0;
943 /* Don't show splashes for 1/2 second after starting search */
944 if (init)
946 last_tick = current_tick + HZ/2;
947 return true;
950 /* Update progress every 1/10 of a second */
951 if (current_tick - last_tick > HZ/10)
953 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG), count, str(LANG_OFF_ABORT));
954 if (action_userabort(TIMEOUT_NOBLOCK))
955 return false;
956 last_tick = current_tick;
957 yield();
960 return true;
963 static int format_str(struct tagcache_search *tcs, struct display_format *fmt,
964 char *buf, int buf_size)
966 char fmtbuf[8];
967 bool read_format = false;
968 int fmtbuf_pos = 0;
969 int parpos = 0;
970 int buf_pos = 0;
971 int i;
973 memset(buf, 0, buf_size);
974 for (i = 0; fmt->formatstr[i] != '\0'; i++)
976 if (fmt->formatstr[i] == '%')
978 read_format = true;
979 fmtbuf_pos = 0;
980 if (parpos >= fmt->tag_count)
982 logf("too many format tags");
983 return -1;
987 if (read_format)
989 fmtbuf[fmtbuf_pos++] = fmt->formatstr[i];
990 if (fmtbuf_pos >= buf_size)
992 logf("format parse error");
993 return -2;
996 if (fmt->formatstr[i] == 's')
998 fmtbuf[fmtbuf_pos] = '\0';
999 read_format = false;
1000 if (fmt->tags[parpos] == tcs->type)
1002 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf, tcs->result);
1004 else
1006 /* Need to fetch the tag data. */
1007 if (!tagcache_retrieve(tcs, tcs->idx_id, fmt->tags[parpos],
1008 &buf[buf_pos], buf_size - buf_pos))
1010 logf("retrieve failed");
1011 return -3;
1014 buf_pos += strlen(&buf[buf_pos]);
1015 parpos++;
1017 else if (fmt->formatstr[i] == 'd')
1019 fmtbuf[fmtbuf_pos] = '\0';
1020 read_format = false;
1021 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf,
1022 tagcache_get_numeric(tcs, fmt->tags[parpos]));
1023 buf_pos += strlen(&buf[buf_pos]);
1024 parpos++;
1026 continue;
1029 buf[buf_pos++] = fmt->formatstr[i];
1031 if (buf_pos - 1 >= buf_size)
1033 logf("buffer overflow");
1034 return -4;
1038 buf[buf_pos++] = '\0';
1040 return 0;
1043 static int retrieve_entries(struct tree_context *c, struct tagcache_search *tcs,
1044 int offset, bool init)
1046 struct tagentry *dptr = (struct tagentry *)c->dircache;
1047 struct display_format *fmt;
1048 int i;
1049 int namebufused = 0;
1050 int total_count = 0;
1051 int special_entry_count = 0;
1052 int level = c->currextra;
1053 int tag;
1054 bool sort = false;
1055 int sort_limit;
1056 int strip;
1058 if (init
1059 #ifdef HAVE_TC_RAMCACHE
1060 && !tagcache_is_ramcache()
1061 #endif
1064 /* Show search progress straight away if the disk needs to spin up,
1065 otherwise show it after the normal 1/2 second delay */
1066 show_search_progress(
1067 #ifdef HAVE_DISK_STORAGE
1068 storage_disk_is_active()
1069 #else
1070 true
1071 #endif
1072 , 0);
1075 if (c->currtable == ALLSUBENTRIES)
1077 tag = tag_title;
1078 level--;
1080 else
1081 tag = csi->tagorder[level];
1083 if (!tagcache_search(tcs, tag))
1084 return -1;
1086 /* Prevent duplicate entries in the search list. */
1087 tagcache_search_set_uniqbuf(tcs, uniqbuf, UNIQBUF_SIZE);
1089 if (level || csi->clause_count[0] || TAGCACHE_IS_NUMERIC(tag))
1090 sort = true;
1092 for (i = 0; i < level; i++)
1094 if (TAGCACHE_IS_NUMERIC(csi->tagorder[i]))
1096 static struct tagcache_search_clause cc;
1098 memset(&cc, 0, sizeof(struct tagcache_search_clause));
1099 cc.tag = csi->tagorder[i];
1100 cc.type = clause_is;
1101 cc.numeric = true;
1102 cc.numeric_data = csi->result_seek[i];
1103 tagcache_search_add_clause(tcs, &cc);
1105 else
1107 tagcache_search_add_filter(tcs, csi->tagorder[i],
1108 csi->result_seek[i]);
1112 for (i = 0; i <= level; i++)
1114 int j;
1116 for (j = 0; j < csi->clause_count[i]; j++)
1117 tagcache_search_add_clause(tcs, csi->clause[i][j]);
1120 current_offset = offset;
1121 current_entry_count = 0;
1122 c->dirfull = false;
1124 fmt = NULL;
1125 for (i = 0; i < format_count; i++)
1127 if (formats[i]->group_id == csi->format_id[level])
1128 fmt = formats[i];
1131 if (fmt)
1133 sort_inverse = fmt->sort_inverse;
1134 sort_limit = fmt->limit;
1135 strip = fmt->strip;
1136 sort = true;
1138 else
1140 sort_inverse = false;
1141 sort_limit = 0;
1142 strip = 0;
1145 if (tag != tag_title && tag != tag_filename)
1147 if (offset == 0)
1149 dptr->newtable = ALLSUBENTRIES;
1150 dptr->name = str(LANG_TAGNAVI_ALL_TRACKS);
1151 dptr++;
1152 current_entry_count++;
1154 if (offset <= 1)
1156 dptr->newtable = NAVIBROWSE;
1157 dptr->name = str(LANG_TAGNAVI_RANDOM);
1158 dptr->extraseek = -1;
1159 dptr++;
1160 current_entry_count++;
1162 special_entry_count+=2;
1165 total_count += special_entry_count;
1167 while (tagcache_get_next(tcs))
1169 if (total_count++ < offset)
1170 continue;
1172 dptr->newtable = NAVIBROWSE;
1173 if (tag == tag_title || tag == tag_filename)
1175 dptr->newtable = PLAYTRACK;
1176 dptr->extraseek = tcs->idx_id;
1178 else
1179 dptr->extraseek = tcs->result_seek;
1181 fmt = NULL;
1182 /* Check the format */
1183 for (i = 0; i < format_count; i++)
1185 if (formats[i]->group_id != csi->format_id[level])
1186 continue;
1188 if (tagcache_check_clauses(tcs, formats[i]->clause,
1189 formats[i]->clause_count))
1191 fmt = formats[i];
1192 break;
1196 if (!tcs->ramresult || fmt)
1198 char buf[MAX_PATH];
1200 if (fmt)
1202 if (format_str(tcs, fmt, buf, sizeof buf) < 0)
1204 logf("format_str() failed");
1205 return 0;
1209 dptr->name = &c->name_buffer[namebufused];
1210 if (fmt)
1211 namebufused += strlen(buf)+1;
1212 else
1213 namebufused += tcs->result_len;
1215 if (namebufused >= c->name_buffer_size)
1217 logf("chunk mode #2: %d", current_entry_count);
1218 c->dirfull = true;
1219 sort = false;
1220 break ;
1222 if (fmt)
1223 strcpy(dptr->name, buf);
1224 else
1225 strcpy(dptr->name, tcs->result);
1227 else
1228 dptr->name = tcs->result;
1230 dptr++;
1231 current_entry_count++;
1233 if (current_entry_count >= global_settings.max_files_in_dir)
1235 logf("chunk mode #3: %d", current_entry_count);
1236 c->dirfull = true;
1237 sort = false;
1238 break ;
1241 if (init && !tcs->ramsearch)
1243 if (!show_search_progress(false, total_count))
1245 tagcache_search_finish(tcs);
1246 return current_entry_count;
1251 if (sort)
1252 qsort(c->dircache + special_entry_count * c->dentry_size,
1253 current_entry_count - special_entry_count,
1254 c->dentry_size, compare);
1256 if (!init)
1258 tagcache_search_finish(tcs);
1259 return current_entry_count;
1262 while (tagcache_get_next(tcs))
1264 if (!tcs->ramsearch)
1266 if (!show_search_progress(false, total_count))
1267 break;
1269 total_count++;
1272 tagcache_search_finish(tcs);
1274 if (!sort && (sort_inverse || sort_limit))
1276 splashf(HZ*4, ID2P(LANG_SHOWDIR_BUFFER_FULL), total_count);
1277 logf("Too small dir buffer");
1278 return 0;
1281 if (sort_limit)
1282 total_count = MIN(total_count, sort_limit);
1284 if (strip)
1286 dptr = c->dircache;
1287 for (i = 0; i < total_count; i++, dptr++)
1289 int len = strlen(dptr->name);
1291 if (len < strip)
1292 continue;
1294 dptr->name = &dptr->name[strip];
1298 return total_count;
1301 static int load_root(struct tree_context *c)
1303 struct tagentry *dptr = (struct tagentry *)c->dircache;
1304 int i;
1306 tc = c;
1307 c->currtable = ROOT;
1308 if (c->dirlevel == 0)
1309 c->currextra = rootmenu;
1311 menu = menus[c->currextra];
1312 if (menu == NULL)
1313 return 0;
1315 for (i = 0; i < menu->itemcount; i++)
1317 dptr->name = menu->items[i]->name;
1318 switch (menu->items[i]->type)
1320 case menu_next:
1321 dptr->newtable = NAVIBROWSE;
1322 dptr->extraseek = i;
1323 break;
1325 case menu_load:
1326 dptr->newtable = ROOT;
1327 dptr->extraseek = menu->items[i]->link;
1328 break;
1331 dptr++;
1334 current_offset = 0;
1335 current_entry_count = i;
1337 return i;
1340 int tagtree_load(struct tree_context* c)
1342 int count;
1343 int table = c->currtable;
1345 c->dentry_size = sizeof(struct tagentry);
1346 c->dirsindir = 0;
1348 if (!table)
1350 c->dirfull = false;
1351 table = ROOT;
1352 c->currtable = table;
1353 c->currextra = rootmenu;
1356 switch (table)
1358 case ROOT:
1359 count = load_root(c);
1360 break;
1362 case ALLSUBENTRIES:
1363 case NAVIBROWSE:
1364 logf("navibrowse...");
1365 cpu_boost(true);
1366 count = retrieve_entries(c, &tcs, 0, true);
1367 cpu_boost(false);
1368 break;
1370 default:
1371 logf("Unsupported table %d\n", table);
1372 return -1;
1375 if (count < 0)
1377 c->dirlevel = 0;
1378 count = load_root(c);
1379 splash(HZ, str(LANG_TAGCACHE_BUSY));
1382 /* The _total_ numer of entries available. */
1383 c->dirlength = c->filesindir = count;
1385 return count;
1388 int tagtree_enter(struct tree_context* c)
1390 int rc = 0;
1391 struct tagentry *dptr;
1392 struct mp3entry *id3;
1393 int newextra;
1394 int seek;
1395 int source;
1397 dptr = tagtree_get_entry(c, c->selected_item);
1399 c->dirfull = false;
1400 seek = dptr->extraseek;
1401 if (seek == -1)
1403 if(c->filesindir<=2)
1404 return 0;
1405 srand(current_tick);
1406 dptr = (tagtree_get_entry(c, 2+(rand() % (c->filesindir-2))));
1407 seek = dptr->extraseek;
1409 newextra = dptr->newtable;
1411 if (c->dirlevel >= MAX_DIR_LEVELS)
1412 return 0;
1414 c->selected_item_history[c->dirlevel]=c->selected_item;
1415 c->table_history[c->dirlevel] = c->currtable;
1416 c->extra_history[c->dirlevel] = c->currextra;
1417 c->pos_history[c->dirlevel] = c->firstpos;
1418 c->dirlevel++;
1420 switch (c->currtable) {
1421 case ROOT:
1422 c->currextra = newextra;
1424 if (newextra == ROOT)
1426 menu = menus[seek];
1427 c->currextra = seek;
1430 else if (newextra == NAVIBROWSE)
1432 int i, j;
1434 csi = menu->items[seek]->si;
1435 c->currextra = 0;
1437 strncpy(current_title[c->currextra], dptr->name,
1438 sizeof(current_title[0]) - 1);
1440 /* Read input as necessary. */
1441 for (i = 0; i < csi->tagorder_count; i++)
1443 for (j = 0; j < csi->clause_count[i]; j++)
1445 char* searchstring;
1446 source = csi->clause[i][j]->source;
1448 if (source == source_constant)
1449 continue;
1451 searchstring=csi->clause[i][j]->str;
1452 *searchstring = '\0';
1454 id3 = audio_current_track();
1456 if (source == source_current_path && id3)
1458 char *e;
1459 strncpy(searchstring, id3->path, SEARCHSTR_SIZE);
1460 e = strrchr(searchstring, '/');
1461 if (e)
1462 *e = '\0';
1464 else if (source > source_runtime && id3)
1467 int k = source-source_runtime;
1468 int offset = id3_to_search_mapping[k].id3_offset;
1469 char **src = (char**)((char*)id3 + offset);
1470 if (*src)
1472 strncpy(searchstring, *src, SEARCHSTR_SIZE);
1473 searchstring[SEARCHSTR_SIZE-1] = '\0';
1476 else
1478 rc = kbd_input(searchstring, SEARCHSTR_SIZE);
1479 if (rc == -1 || !searchstring[0])
1481 tagtree_exit(c);
1482 return 0;
1484 if (csi->clause[i][j]->numeric)
1485 csi->clause[i][j]->numeric_data = atoi(searchstring);
1492 c->currtable = newextra;
1494 break;
1496 case NAVIBROWSE:
1497 case ALLSUBENTRIES:
1498 if (newextra == PLAYTRACK)
1500 if (global_settings.party_mode && audio_status()) {
1501 splash(HZ, ID2P(LANG_PARTY_MODE));
1502 break;
1504 c->dirlevel--;
1505 /* about to create a new current playlist...
1506 allow user to cancel the operation */
1507 if (!warn_on_pl_erase())
1508 break;
1510 if (tagtree_play_folder(c) >= 0)
1511 rc = 2;
1512 break;
1515 c->currtable = newextra;
1516 csi->result_seek[c->currextra] = seek;
1517 if (c->currextra < csi->tagorder_count-1)
1518 c->currextra++;
1519 else
1520 c->dirlevel--;
1522 /* Update the statusbar title */
1523 strncpy(current_title[c->currextra], dptr->name,
1524 sizeof(current_title[0]) - 1);
1525 break;
1527 default:
1528 c->dirlevel--;
1529 break;
1532 c->selected_item=0;
1533 gui_synclist_select_item(&tree_lists, c->selected_item);
1535 return rc;
1538 void tagtree_exit(struct tree_context* c)
1540 c->dirfull = false;
1541 if (c->dirlevel > 0)
1542 c->dirlevel--;
1543 c->selected_item=c->selected_item_history[c->dirlevel];
1544 gui_synclist_select_item(&tree_lists, c->selected_item);
1545 c->currtable = c->table_history[c->dirlevel];
1546 c->currextra = c->extra_history[c->dirlevel];
1547 c->firstpos = c->pos_history[c->dirlevel];
1550 int tagtree_get_filename(struct tree_context* c, char *buf, int buflen)
1552 struct tagentry *entry;
1554 entry = tagtree_get_entry(c, c->selected_item);
1556 if (!tagcache_search(&tcs, tag_filename))
1557 return -1;
1559 if (!tagcache_retrieve(&tcs, entry->extraseek, tcs.type, buf, buflen))
1561 tagcache_search_finish(&tcs);
1562 return -2;
1565 tagcache_search_finish(&tcs);
1567 return 0;
1570 static bool insert_all_playlist(struct tree_context *c, int position, bool queue)
1572 int i;
1573 char buf[MAX_PATH];
1574 int from, to, direction;
1575 int files_left = c->filesindir;
1577 cpu_boost(true);
1578 if (!tagcache_search(&tcs, tag_filename))
1580 splash(HZ, ID2P(LANG_TAGCACHE_BUSY));
1581 cpu_boost(false);
1582 return false;
1585 if (position == PLAYLIST_REPLACE)
1587 if (playlist_remove_all_tracks(NULL) == 0)
1588 position = PLAYLIST_INSERT_LAST;
1589 else
1591 cpu_boost(false);
1592 return false;
1596 if (position == PLAYLIST_INSERT_FIRST)
1598 from = c->filesindir - 1;
1599 to = -1;
1600 direction = -1;
1602 else
1604 from = 0;
1605 to = c->filesindir;
1606 direction = 1;
1609 for (i = from; i != to; i += direction)
1611 /* Count back to zero */
1612 if (!show_search_progress(false, files_left--))
1613 break;
1615 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, i)->extraseek,
1616 tcs.type, buf, sizeof buf))
1618 continue;
1621 if (playlist_insert_track(NULL, buf, position, queue, false) < 0)
1623 logf("playlist_insert_track failed");
1624 break;
1626 yield();
1628 playlist_sync(NULL);
1629 tagcache_search_finish(&tcs);
1630 cpu_boost(false);
1632 return true;
1635 bool tagtree_insert_selection_playlist(int position, bool queue)
1637 struct tagentry *dptr;
1638 char buf[MAX_PATH];
1639 int dirlevel = tc->dirlevel;
1641 /* We need to set the table to allsubentries. */
1642 show_search_progress(true, 0);
1644 dptr = tagtree_get_entry(tc, tc->selected_item);
1646 /* Insert a single track? */
1647 if (dptr->newtable == PLAYTRACK)
1649 if (tagtree_get_filename(tc, buf, sizeof buf) < 0)
1651 logf("tagtree_get_filename failed");
1652 return false;
1654 playlist_insert_track(NULL, buf, position, queue, true);
1656 return true;
1659 if (dptr->newtable == NAVIBROWSE)
1661 tagtree_enter(tc);
1662 tagtree_load(tc);
1663 dptr = tagtree_get_entry(tc, tc->selected_item);
1665 else if (dptr->newtable != ALLSUBENTRIES)
1667 logf("unsupported table: %d", dptr->newtable);
1668 return false;
1671 /* Now the current table should be allsubentries. */
1672 if (dptr->newtable != PLAYTRACK)
1674 tagtree_enter(tc);
1675 tagtree_load(tc);
1676 dptr = tagtree_get_entry(tc, tc->selected_item);
1678 /* And now the newtable should be playtrack. */
1679 if (dptr->newtable != PLAYTRACK)
1681 logf("newtable: %d !!", dptr->newtable);
1682 tc->dirlevel = dirlevel;
1683 return false;
1687 if (tc->filesindir <= 0)
1688 splash(HZ, ID2P(LANG_END_PLAYLIST));
1689 else
1691 logf("insert_all_playlist");
1692 if (!insert_all_playlist(tc, position, queue))
1693 splash(HZ*2, ID2P(LANG_FAILED));
1696 /* Finally return the dirlevel to its original value. */
1697 while (tc->dirlevel > dirlevel)
1698 tagtree_exit(tc);
1699 tagtree_load(tc);
1701 return true;
1704 static int tagtree_play_folder(struct tree_context* c)
1706 if (playlist_create(NULL, NULL) < 0)
1708 logf("Failed creating playlist\n");
1709 return -1;
1712 if (!insert_all_playlist(c, PLAYLIST_INSERT_LAST, false))
1713 return -2;
1715 if (global_settings.playlist_shuffle)
1716 c->selected_item = playlist_shuffle(current_tick, c->selected_item);
1717 if (!global_settings.play_selected)
1718 c->selected_item = 0;
1719 gui_synclist_select_item(&tree_lists, c->selected_item);
1721 playlist_start(c->selected_item,0);
1723 return 0;
1726 struct tagentry* tagtree_get_entry(struct tree_context *c, int id)
1728 struct tagentry *entry = (struct tagentry *)c->dircache;
1729 int realid = id - current_offset;
1731 /* Load the next chunk if necessary. */
1732 if (realid >= current_entry_count || realid < 0)
1734 cpu_boost(true);
1735 if (retrieve_entries(c, &tcs2, MAX(0, id - (current_entry_count / 2)),
1736 false) < 0)
1738 logf("retrieve failed");
1739 cpu_boost(false);
1740 return NULL;
1742 realid = id - current_offset;
1743 cpu_boost(false);
1746 return &entry[realid];
1749 char *tagtree_get_title(struct tree_context* c)
1751 switch (c->currtable)
1753 case ROOT:
1754 return menu->title;
1756 case NAVIBROWSE:
1757 case ALLSUBENTRIES:
1758 return current_title[c->currextra];
1761 return "?";
1764 int tagtree_get_attr(struct tree_context* c)
1766 int attr = -1;
1767 switch (c->currtable)
1769 case NAVIBROWSE:
1770 if (csi->tagorder[c->currextra] == tag_title)
1771 attr = FILE_ATTR_AUDIO;
1772 else
1773 attr = ATTR_DIRECTORY;
1774 break;
1776 case ALLSUBENTRIES:
1777 attr = FILE_ATTR_AUDIO;
1778 break;
1780 default:
1781 attr = ATTR_DIRECTORY;
1782 break;
1785 return attr;
1788 int tagtree_get_icon(struct tree_context* c)
1790 int icon = Icon_Folder;
1792 if (tagtree_get_attr(c) == FILE_ATTR_AUDIO)
1793 icon = Icon_Audio;
1795 return icon;