FS#9281 Rename of splash functions.
[kugel-rb.git] / apps / tagtree.c
blobce562b78e9654168cfae1aa821f42234c34e362d
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 "events.h"
52 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
54 static int tagtree_play_folder(struct tree_context* c);
56 #define SEARCHSTR_SIZE 256
58 static const struct id3_to_search_mapping {
59 char *string;
60 size_t id3_offset;
61 } id3_to_search_mapping[] = {
62 { "", 0 }, /* offset n/a */
63 { "#directory#", 0 }, /* offset n/a */
64 { "#title#", offsetof(struct mp3entry, title) },
65 { "#artist#", offsetof(struct mp3entry, artist) },
66 { "#album#", offsetof(struct mp3entry, album) },
67 { "#genre#", offsetof(struct mp3entry, genre_string) },
68 { "#composer#", offsetof(struct mp3entry, composer) },
69 { "#albumartist#", offsetof(struct mp3entry, albumartist) },
71 enum variables {
72 var_sorttype = 100,
73 var_limit,
74 var_strip,
75 var_menu_start,
76 var_include,
77 var_rootmenu,
78 var_format,
79 menu_next,
80 menu_load,
83 /* Capacity 10 000 entries (for example 10k different artists) */
84 #define UNIQBUF_SIZE (64*1024)
85 static long *uniqbuf;
87 #define MAX_TAGS 5
88 #define MAX_MENU_ID_SIZE 32
90 static struct tagcache_search tcs, tcs2;
91 static bool sort_inverse;
94 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
96 * valid = true
97 * formatstr = "%-3d. %s"
98 * tags[0] = tag_autoscore
99 * tags[1] = tag_title
100 * tag_count = 2
102 * limit = 100
103 * sort_inverse = true
105 struct display_format {
106 char name[32];
107 struct tagcache_search_clause *clause[TAGCACHE_MAX_CLAUSES];
108 int clause_count;
109 char *formatstr;
110 int group_id;
111 int tags[MAX_TAGS];
112 int tag_count;
114 int limit;
115 int strip;
116 bool sort_inverse;
119 static struct display_format *formats[TAGMENU_MAX_FMTS];
120 static int format_count;
122 struct search_instruction {
123 char name[64];
124 int tagorder[MAX_TAGS];
125 int tagorder_count;
126 struct tagcache_search_clause *clause[MAX_TAGS][TAGCACHE_MAX_CLAUSES];
127 int format_id[MAX_TAGS];
128 int clause_count[MAX_TAGS];
129 int result_seek[MAX_TAGS];
132 struct menu_entry {
133 char name[64];
134 int type;
135 struct search_instruction *si;
136 int link;
139 struct root_menu {
140 char title[64];
141 char id[MAX_MENU_ID_SIZE];
142 int itemcount;
143 struct menu_entry *items[TAGMENU_MAX_ITEMS];
146 /* Statusbar text of the current view. */
147 static char current_title[MAX_TAGS][128];
149 static struct root_menu *menus[TAGMENU_MAX_MENUS];
150 static struct root_menu *menu;
151 static struct search_instruction *csi;
152 static const char *strp;
153 static int menu_count;
154 static int root_menu;
156 static int current_offset;
157 static int current_entry_count;
159 static int format_count;
160 static struct tree_context *tc;
162 static int get_token_str(char *buf, int size)
164 /* Find the start. */
165 while (*strp != '"' && *strp != '\0')
166 strp++;
168 if (*strp == '\0' || *(++strp) == '\0')
169 return -1;
171 /* Read the data. */
172 while (*strp != '"' && *strp != '\0' && --size > 0)
173 *(buf++) = *(strp++);
175 *buf = '\0';
176 if (*strp != '"')
177 return -2;
179 strp++;
181 return 0;
184 #define MATCH(tag,str1,str2,settag) \
185 if (!strcasecmp(str1, str2)) { \
186 *tag = settag; \
187 return 1; \
190 static int get_tag(int *tag)
192 char buf[128];
193 int i;
195 /* Find the start. */
196 while ((*strp == ' ' || *strp == '>') && *strp != '\0')
197 strp++;
199 if (*strp == '\0' || *strp == '?')
200 return 0;
202 for (i = 0; i < (int)sizeof(buf)-1; i++)
204 if (*strp == '\0' || *strp == ' ')
205 break ;
206 buf[i] = *strp;
207 strp++;
209 buf[i] = '\0';
211 MATCH(tag, buf, "album", tag_album);
212 MATCH(tag, buf, "artist", tag_artist);
213 MATCH(tag, buf, "bitrate", tag_bitrate);
214 MATCH(tag, buf, "composer", tag_composer);
215 MATCH(tag, buf, "comment", tag_comment);
216 MATCH(tag, buf, "albumartist", tag_albumartist);
217 MATCH(tag, buf, "ensemble", tag_albumartist);
218 MATCH(tag, buf, "grouping", tag_grouping);
219 MATCH(tag, buf, "genre", tag_genre);
220 MATCH(tag, buf, "length", tag_length);
221 MATCH(tag, buf, "Lm", tag_virt_length_min);
222 MATCH(tag, buf, "Ls", tag_virt_length_sec);
223 MATCH(tag, buf, "Pm", tag_virt_playtime_min);
224 MATCH(tag, buf, "Ps", tag_virt_playtime_sec);
225 MATCH(tag, buf, "title", tag_title);
226 MATCH(tag, buf, "filename", tag_filename);
227 MATCH(tag, buf, "tracknum", tag_tracknumber);
228 MATCH(tag, buf, "discnum", tag_discnumber);
229 MATCH(tag, buf, "year", tag_year);
230 MATCH(tag, buf, "playcount", tag_playcount);
231 MATCH(tag, buf, "rating", tag_rating);
232 MATCH(tag, buf, "lastplayed", tag_lastplayed);
233 MATCH(tag, buf, "commitid", tag_commitid);
234 MATCH(tag, buf, "entryage", tag_virt_entryage);
235 MATCH(tag, buf, "autoscore", tag_virt_autoscore);
236 MATCH(tag, buf, "%sort", var_sorttype);
237 MATCH(tag, buf, "%limit", var_limit);
238 MATCH(tag, buf, "%strip", var_strip);
239 MATCH(tag, buf, "%menu_start", var_menu_start);
240 MATCH(tag, buf, "%include", var_include);
241 MATCH(tag, buf, "%root_menu", var_rootmenu);
242 MATCH(tag, buf, "%format", var_format);
243 MATCH(tag, buf, "->", menu_next);
244 MATCH(tag, buf, "==>", menu_load);
246 logf("NO MATCH: %s\n", buf);
247 if (buf[0] == '?')
248 return 0;
250 return -1;
253 static int get_clause(int *condition)
255 char buf[4];
256 int i;
258 /* Find the start. */
259 while (*strp == ' ' && *strp != '\0')
260 strp++;
262 if (*strp == '\0')
263 return 0;
265 for (i = 0; i < (int)sizeof(buf)-1; i++)
267 if (*strp == '\0' || *strp == ' ')
268 break ;
269 buf[i] = *strp;
270 strp++;
272 buf[i] = '\0';
274 MATCH(condition, buf, "=", clause_is);
275 MATCH(condition, buf, "==", clause_is);
276 MATCH(condition, buf, "!=", clause_is_not);
277 MATCH(condition, buf, ">", clause_gt);
278 MATCH(condition, buf, ">=", clause_gteq);
279 MATCH(condition, buf, "<", clause_lt);
280 MATCH(condition, buf, "<=", clause_lteq);
281 MATCH(condition, buf, "~", clause_contains);
282 MATCH(condition, buf, "!~", clause_not_contains);
283 MATCH(condition, buf, "^", clause_begins_with);
284 MATCH(condition, buf, "!^", clause_not_begins_with);
285 MATCH(condition, buf, "$", clause_ends_with);
286 MATCH(condition, buf, "!$", clause_not_ends_with);
287 MATCH(condition, buf, "@", clause_oneof);
289 return 0;
292 static bool read_clause(struct tagcache_search_clause *clause)
294 char buf[SEARCHSTR_SIZE];
295 unsigned int i;
297 if (get_tag(&clause->tag) <= 0)
298 return false;
300 if (get_clause(&clause->type) <= 0)
301 return false;
303 if (get_token_str(buf, sizeof buf) < 0)
304 return false;
306 for (i=0; i<ARRAYLEN(id3_to_search_mapping); i++)
308 if (!strcasecmp(buf, id3_to_search_mapping[i].string))
309 break;
312 if (i<ARRAYLEN(id3_to_search_mapping)) /* runtime search operand found */
314 clause->source = source_runtime+i;
315 clause->str = buffer_alloc(SEARCHSTR_SIZE);
317 else
319 clause->source = source_constant;
320 clause->str = buffer_alloc(strlen(buf)+1);
321 strcpy(clause->str, buf);
324 if (tagcache_is_numeric_tag(clause->tag))
326 clause->numeric = true;
327 clause->numeric_data = atoi(clause->str);
329 else
330 clause->numeric = false;
332 logf("got clause: %d/%d [%s]", clause->tag, clause->type, clause->str);
334 return true;
337 static bool read_variable(char *buf, int size)
339 int condition;
341 if (!get_clause(&condition))
342 return false;
344 if (condition != clause_is)
345 return false;
347 if (get_token_str(buf, size) < 0)
348 return false;
350 return true;
353 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
354 static int get_format_str(struct display_format *fmt)
356 int ret;
357 char buf[128];
358 int i;
360 memset(fmt, 0, sizeof(struct display_format));
362 if (get_token_str(fmt->name, sizeof fmt->name) < 0)
363 return -10;
365 /* Determine the group id */
366 fmt->group_id = 0;
367 for (i = 0; i < format_count; i++)
369 if (!strcasecmp(formats[i]->name, fmt->name))
371 fmt->group_id = formats[i]->group_id;
372 break;
375 if (formats[i]->group_id > fmt->group_id)
376 fmt->group_id = formats[i]->group_id;
379 if (i == format_count)
380 fmt->group_id++;
382 logf("format: (%d) %s", fmt->group_id, fmt->name);
384 if (get_token_str(buf, sizeof buf) < 0)
385 return -10;
387 fmt->formatstr = buffer_alloc(strlen(buf) + 1);
388 strcpy(fmt->formatstr, buf);
390 while (fmt->tag_count < MAX_TAGS)
392 ret = get_tag(&fmt->tags[fmt->tag_count]);
393 if (ret < 0)
394 return -11;
396 if (ret == 0)
397 break;
399 switch (fmt->tags[fmt->tag_count]) {
400 case var_sorttype:
401 if (!read_variable(buf, sizeof buf))
402 return -12;
403 if (!strcasecmp("inverse", buf))
404 fmt->sort_inverse = true;
405 break;
407 case var_limit:
408 if (!read_variable(buf, sizeof buf))
409 return -13;
410 fmt->limit = atoi(buf);
411 break;
413 case var_strip:
414 if (!read_variable(buf, sizeof buf))
415 return -14;
416 fmt->strip = atoi(buf);
417 break;
419 default:
420 fmt->tag_count++;
424 return 1;
427 static int add_format(const char *buf)
429 strp = buf;
431 if (formats[format_count] == NULL)
432 formats[format_count] = buffer_alloc(sizeof(struct display_format));
434 memset(formats[format_count], 0, sizeof(struct display_format));
435 if (get_format_str(formats[format_count]) < 0)
437 logf("get_format_str() parser failed!");
438 return -4;
441 while (*strp != '\0' && *strp != '?')
442 strp++;
444 if (*strp == '?')
446 int clause_count = 0;
447 strp++;
449 while (1)
451 if (clause_count >= TAGCACHE_MAX_CLAUSES)
453 logf("too many clauses");
454 break;
457 formats[format_count]->clause[clause_count] =
458 buffer_alloc(sizeof(struct tagcache_search_clause));
460 if (!read_clause(formats[format_count]->clause[clause_count]))
461 break;
463 clause_count++;
466 formats[format_count]->clause_count = clause_count;
469 format_count++;
471 return 1;
474 static int get_condition(struct search_instruction *inst)
476 int clause_count;
477 char buf[128];
479 switch (*strp)
481 case '=':
483 int i;
485 if (get_token_str(buf, sizeof buf) < 0)
486 return -1;
488 for (i = 0; i < format_count; i++)
490 if (!strcasecmp(formats[i]->name, buf))
491 break;
494 if (i == format_count)
496 logf("format not found: %s", buf);
497 return -2;
500 inst->format_id[inst->tagorder_count] = formats[i]->group_id;
501 return 1;
503 case '?':
504 case ' ':
505 case '&':
506 strp++;
507 return 1;
508 case '-':
509 case '\0':
510 return 0;
513 clause_count = inst->clause_count[inst->tagorder_count];
514 if (clause_count >= TAGCACHE_MAX_CLAUSES)
516 logf("Too many clauses");
517 return false;
520 inst->clause[inst->tagorder_count][clause_count] =
521 buffer_alloc(sizeof(struct tagcache_search_clause));
523 if (!read_clause(inst->clause[inst->tagorder_count][clause_count]))
524 return -1;
526 inst->clause_count[inst->tagorder_count]++;
528 return 1;
531 /* example search:
532 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
533 * : album ? year >= "2000" : songs
534 * ^ begins with
535 * * contains
536 * $ ends with
539 static bool parse_search(struct menu_entry *entry, const char *str)
541 int ret;
542 int type;
543 struct search_instruction *inst = entry->si;
544 char buf[MAX_PATH];
545 int i;
546 struct root_menu *new_menu;
548 strp = str;
550 /* Parse entry name */
551 if (get_token_str(entry->name, sizeof entry->name) < 0)
553 logf("No name found.");
554 return false;
557 /* Parse entry type */
558 if (get_tag(&entry->type) <= 0)
559 return false;
561 if (entry->type == menu_load)
563 if (get_token_str(buf, sizeof buf) < 0)
564 return false;
566 /* Find the matching root menu or "create" it */
567 for (i = 0; i < menu_count; i++)
569 if (!strcasecmp(menus[i]->id, buf))
571 entry->link = i;
572 return true;
576 if (menu_count >= TAGMENU_MAX_MENUS)
578 logf("max menucount reached");
579 return false;
582 /* Allocate a new menu unless link is found. */
583 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
584 new_menu = menus[menu_count];
585 memset(new_menu, 0, sizeof(struct root_menu));
586 strncpy(new_menu->id, buf, MAX_MENU_ID_SIZE-1);
587 entry->link = menu_count;
588 ++menu_count;
590 return true;
593 if (entry->type != menu_next)
594 return false;
596 while (inst->tagorder_count < MAX_TAGS)
598 ret = get_tag(&inst->tagorder[inst->tagorder_count]);
599 if (ret < 0)
601 logf("Parse error #1");
602 logf("%s", strp);
603 return false;
606 if (ret == 0)
607 break ;
609 logf("tag: %d", inst->tagorder[inst->tagorder_count]);
611 while ( (ret = get_condition(inst)) > 0 ) ;
612 if (ret < 0)
613 return false;
615 inst->tagorder_count++;
617 if (get_tag(&type) <= 0 || type != menu_next)
618 break;
621 return true;
624 static int compare(const void *p1, const void *p2)
626 struct tagentry *e1 = (struct tagentry *)p1;
627 struct tagentry *e2 = (struct tagentry *)p2;
629 if (sort_inverse)
630 return strncasecmp(e2->name, e1->name, MAX_PATH);
632 return strncasecmp(e1->name, e2->name, MAX_PATH);
635 static void tagtree_buffer_event(struct mp3entry *id3)
637 /* Do not gather data unless proper setting has been enabled. */
638 if (!global_settings.runtimedb)
639 return;
641 logf("be:%s", id3->path);
643 if (!tagcache_find_index(&tcs, id3->path))
645 logf("tc stat: not found: %s", id3->path);
646 return;
649 id3->playcount = tagcache_get_numeric(&tcs, tag_playcount);
650 if (!id3->rating)
651 id3->rating = tagcache_get_numeric(&tcs, tag_rating);
652 id3->lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
653 id3->score = tagcache_get_numeric(&tcs, tag_virt_autoscore) / 10;
654 id3->playtime = tagcache_get_numeric(&tcs, tag_playtime);
656 /* Store our tagcache index pointer. */
657 id3->tagcache_idx = tcs.idx_id+1;
659 tagcache_search_finish(&tcs);
662 static void tagtree_track_finish_event(struct mp3entry *id3)
664 long playcount;
665 long playtime;
666 long lastplayed;
667 long tagcache_idx;
669 /* Do not gather data unless proper setting has been enabled. */
670 if (!global_settings.runtimedb)
672 logf("runtimedb gathering not enabled");
673 return;
676 tagcache_idx=id3->tagcache_idx;
677 if (!tagcache_idx)
679 logf("No tagcache index pointer found");
680 return;
682 tagcache_idx--;
684 /* Don't process unplayed tracks. */
685 if (id3->elapsed == 0)
687 logf("not logging unplayed track");
688 return;
691 playcount = id3->playcount + 1;
692 lastplayed = tagcache_increase_serial();
693 if (lastplayed < 0)
695 logf("incorrect tc serial:%ld", lastplayed);
696 return;
699 /* Ignore the last 15s (crossfade etc.) */
700 playtime = id3->playtime + MIN(id3->length, id3->elapsed + 15 * 1000);
702 logf("ube:%s", id3->path);
703 logf("-> %ld/%ld", playcount, playtime);
704 logf("-> %ld/%ld/%ld", id3->elapsed, id3->length, MIN(id3->length, id3->elapsed + 15 * 1000));
706 /* Queue the updates to the tagcache system. */
707 tagcache_update_numeric(tagcache_idx, tag_playcount, playcount);
708 tagcache_update_numeric(tagcache_idx, tag_playtime, playtime);
709 tagcache_update_numeric(tagcache_idx, tag_lastplayed, lastplayed);
712 bool tagtree_export(void)
714 splash(0, str(LANG_CREATING));
715 if (!tagcache_create_changelog(&tcs))
717 splash(HZ*2, ID2P(LANG_FAILED));
720 return false;
723 bool tagtree_import(void)
725 splash(0, ID2P(LANG_WAIT));
726 if (!tagcache_import_changelog())
728 splash(HZ*2, ID2P(LANG_FAILED));
731 return false;
734 static bool parse_menu(const char *filename);
736 static int parse_line(int n, const char *buf, void *parameters)
738 char data[256];
739 int variable;
740 static bool read_menu;
741 int i;
743 (void)parameters;
745 logf("parse:%d/%s", n, buf);
747 /* First line, do initialisation. */
748 if (n == 0)
750 if (strcasecmp(TAGNAVI_VERSION, buf))
752 logf("Version mismatch");
753 return -1;
756 read_menu = false;
759 if (buf[0] == '#')
760 return 0;
762 if (buf[0] == '\0')
764 if (read_menu)
766 /* End the menu */
767 read_menu = false;
769 return 0;
772 if (!read_menu)
774 strp = buf;
775 if (get_tag(&variable) <= 0)
776 return 0;
778 switch (variable)
780 case var_format:
781 if (add_format(strp) < 0)
783 logf("Format add fail: %s", data);
785 break;
787 case var_include:
788 if (get_token_str(data, sizeof(data)) < 0)
790 logf("%%include empty");
791 return 0;
794 if (!parse_menu(data))
796 logf("Load menu fail: %s", data);
798 break;
800 case var_menu_start:
801 if (menu_count >= TAGMENU_MAX_MENUS)
803 logf("max menucount reached");
804 return 0;
807 if (get_token_str(data, sizeof data) < 0)
809 logf("%%menu_start id empty");
810 return 0;
813 menu = NULL;
814 for (i = 0; i < menu_count; i++)
816 if (!strcasecmp(menus[i]->id, data))
818 menu = menus[i];
822 if (menu == NULL)
824 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
825 menu = menus[menu_count];
826 ++menu_count;
827 memset(menu, 0, sizeof(struct root_menu));
828 strncpy(menu->id, data, MAX_MENU_ID_SIZE-1);
831 if (get_token_str(menu->title, sizeof(menu->title)) < 0)
833 logf("%%menu_start title empty");
834 return 0;
836 logf("menu: %s", menu->title);
837 read_menu = true;
838 break;
840 case var_rootmenu:
841 /* Only set root menu once. */
842 if (root_menu >= 0)
843 break;
845 if (get_token_str(data, sizeof(data)) < 0)
847 logf("%%root_menu empty");
848 return 0;
851 for (i = 0; i < menu_count; i++)
853 if (!strcasecmp(menus[i]->id, data))
855 root_menu = i;
858 break;
861 return 0;
864 if (menu->itemcount >= TAGMENU_MAX_ITEMS)
866 logf("max itemcount reached");
867 return 0;
870 /* Allocate */
871 if (menu->items[menu->itemcount] == NULL)
873 menu->items[menu->itemcount] = buffer_alloc(sizeof(struct menu_entry));
874 memset(menu->items[menu->itemcount], 0, sizeof(struct menu_entry));
875 menu->items[menu->itemcount]->si = buffer_alloc(sizeof(struct search_instruction));
878 memset(menu->items[menu->itemcount]->si, 0, sizeof(struct search_instruction));
879 if (!parse_search(menu->items[menu->itemcount], buf))
880 return 0;
882 menu->itemcount++;
884 return 0;
887 static bool parse_menu(const char *filename)
889 int fd;
890 char buf[1024];
892 if (menu_count >= TAGMENU_MAX_MENUS)
894 logf("max menucount reached");
895 return false;
898 fd = open(filename, O_RDONLY);
899 if (fd < 0)
901 logf("Search instruction file not found.");
902 return false;
905 /* Now read file for real, parsing into si */
906 fast_readline(fd, buf, sizeof buf, NULL, parse_line);
907 close(fd);
909 return true;
912 void tagtree_init(void)
914 format_count = 0;
915 menu_count = 0;
916 menu = NULL;
917 root_menu = -1;
918 parse_menu(FILE_SEARCH_INSTRUCTIONS);
920 /* If no root menu is set, assume it's the first single menu
921 * we have. That shouldn't normally happen. */
922 if (root_menu < 0)
923 root_menu = 0;
925 uniqbuf = buffer_alloc(UNIQBUF_SIZE);
927 add_event(PLAYBACK_EVENT_TRACK_BUFFER, false, tagtree_buffer_event);
928 add_event(PLAYBACK_EVENT_TRACK_FINISH, false, tagtree_track_finish_event);
931 static bool show_search_progress(bool init, int count)
933 static int last_tick = 0;
935 /* Don't show splashes for 1/2 second after starting search */
936 if (init)
938 last_tick = current_tick + HZ/2;
939 return true;
942 /* Update progress every 1/10 of a second */
943 if (current_tick - last_tick > HZ/10)
945 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG), count, str(LANG_OFF_ABORT));
946 if (action_userabort(TIMEOUT_NOBLOCK))
947 return false;
948 last_tick = current_tick;
949 yield();
952 return true;
955 static int format_str(struct tagcache_search *tcs, struct display_format *fmt,
956 char *buf, int buf_size)
958 char fmtbuf[8];
959 bool read_format = false;
960 int fmtbuf_pos = 0;
961 int parpos = 0;
962 int buf_pos = 0;
963 int i;
965 memset(buf, 0, buf_size);
966 for (i = 0; fmt->formatstr[i] != '\0'; i++)
968 if (fmt->formatstr[i] == '%')
970 read_format = true;
971 fmtbuf_pos = 0;
972 if (parpos >= fmt->tag_count)
974 logf("too many format tags");
975 return -1;
979 if (read_format)
981 fmtbuf[fmtbuf_pos++] = fmt->formatstr[i];
982 if (fmtbuf_pos >= buf_size)
984 logf("format parse error");
985 return -2;
988 if (fmt->formatstr[i] == 's')
990 fmtbuf[fmtbuf_pos] = '\0';
991 read_format = false;
992 if (fmt->tags[parpos] == tcs->type)
994 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf, tcs->result);
996 else
998 /* Need to fetch the tag data. */
999 if (!tagcache_retrieve(tcs, tcs->idx_id, fmt->tags[parpos],
1000 &buf[buf_pos], buf_size - buf_pos))
1002 logf("retrieve failed");
1003 return -3;
1006 buf_pos += strlen(&buf[buf_pos]);
1007 parpos++;
1009 else if (fmt->formatstr[i] == 'd')
1011 fmtbuf[fmtbuf_pos] = '\0';
1012 read_format = false;
1013 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf,
1014 tagcache_get_numeric(tcs, fmt->tags[parpos]));
1015 buf_pos += strlen(&buf[buf_pos]);
1016 parpos++;
1018 continue;
1021 buf[buf_pos++] = fmt->formatstr[i];
1023 if (buf_pos - 1 >= buf_size)
1025 logf("buffer overflow");
1026 return -4;
1030 buf[buf_pos++] = '\0';
1032 return 0;
1035 static int retrieve_entries(struct tree_context *c, struct tagcache_search *tcs,
1036 int offset, bool init)
1038 struct tagentry *dptr = (struct tagentry *)c->dircache;
1039 struct display_format *fmt;
1040 int i;
1041 int namebufused = 0;
1042 int total_count = 0;
1043 int special_entry_count = 0;
1044 int level = c->currextra;
1045 int tag;
1046 bool sort = false;
1047 int sort_limit;
1048 int strip;
1050 if (init
1051 #ifdef HAVE_TC_RAMCACHE
1052 && !tagcache_is_ramcache()
1053 #endif
1056 /* Show search progress straight away if the disk needs to spin up,
1057 otherwise show it after the normal 1/2 second delay */
1058 show_search_progress(
1059 #if !defined(HAVE_FLASH_STORAGE)
1060 ata_disk_is_active()
1061 #else
1062 true
1063 #endif
1064 , 0);
1067 if (c->currtable == allsubentries)
1069 tag = tag_title;
1070 level--;
1072 else
1073 tag = csi->tagorder[level];
1075 if (!tagcache_search(tcs, tag))
1076 return -1;
1078 /* Prevent duplicate entries in the search list. */
1079 tagcache_search_set_uniqbuf(tcs, uniqbuf, UNIQBUF_SIZE);
1081 if (level || csi->clause_count[0] || tagcache_is_numeric_tag(tag))
1082 sort = true;
1084 for (i = 0; i < level; i++)
1086 if (tagcache_is_numeric_tag(csi->tagorder[i]))
1088 static struct tagcache_search_clause cc;
1090 memset(&cc, 0, sizeof(struct tagcache_search_clause));
1091 cc.tag = csi->tagorder[i];
1092 cc.type = clause_is;
1093 cc.numeric = true;
1094 cc.numeric_data = csi->result_seek[i];
1095 tagcache_search_add_clause(tcs, &cc);
1097 else
1099 tagcache_search_add_filter(tcs, csi->tagorder[i],
1100 csi->result_seek[i]);
1104 for (i = 0; i <= level; i++)
1106 int j;
1108 for (j = 0; j < csi->clause_count[i]; j++)
1109 tagcache_search_add_clause(tcs, csi->clause[i][j]);
1112 current_offset = offset;
1113 current_entry_count = 0;
1114 c->dirfull = false;
1116 fmt = NULL;
1117 for (i = 0; i < format_count; i++)
1119 if (formats[i]->group_id == csi->format_id[level])
1120 fmt = formats[i];
1123 if (fmt)
1125 sort_inverse = fmt->sort_inverse;
1126 sort_limit = fmt->limit;
1127 strip = fmt->strip;
1128 sort = true;
1130 else
1132 sort_inverse = false;
1133 sort_limit = 0;
1134 strip = 0;
1137 if (tag != tag_title && tag != tag_filename)
1139 if (offset == 0)
1141 dptr->newtable = allsubentries;
1142 dptr->name = str(LANG_TAGNAVI_ALL_TRACKS);
1143 dptr++;
1144 current_entry_count++;
1146 if (offset <= 1)
1148 dptr->newtable = navibrowse;
1149 dptr->name = str(LANG_TAGNAVI_RANDOM);
1150 dptr->extraseek = -1;
1151 dptr++;
1152 current_entry_count++;
1154 special_entry_count+=2;
1157 total_count += special_entry_count;
1159 while (tagcache_get_next(tcs))
1161 if (total_count++ < offset)
1162 continue;
1164 dptr->newtable = navibrowse;
1165 if (tag == tag_title || tag == tag_filename)
1167 dptr->newtable = playtrack;
1168 dptr->extraseek = tcs->idx_id;
1170 else
1171 dptr->extraseek = tcs->result_seek;
1173 fmt = NULL;
1174 /* Check the format */
1175 for (i = 0; i < format_count; i++)
1177 if (formats[i]->group_id != csi->format_id[level])
1178 continue;
1180 if (tagcache_check_clauses(tcs, formats[i]->clause,
1181 formats[i]->clause_count))
1183 fmt = formats[i];
1184 break;
1188 if (!tcs->ramresult || fmt)
1190 char buf[MAX_PATH];
1192 if (fmt)
1194 if (format_str(tcs, fmt, buf, sizeof buf) < 0)
1196 logf("format_str() failed");
1197 return 0;
1201 dptr->name = &c->name_buffer[namebufused];
1202 if (fmt)
1203 namebufused += strlen(buf)+1;
1204 else
1205 namebufused += tcs->result_len;
1207 if (namebufused >= c->name_buffer_size)
1209 logf("chunk mode #2: %d", current_entry_count);
1210 c->dirfull = true;
1211 sort = false;
1212 break ;
1214 if (fmt)
1215 strcpy(dptr->name, buf);
1216 else
1217 strcpy(dptr->name, tcs->result);
1219 else
1220 dptr->name = tcs->result;
1222 dptr++;
1223 current_entry_count++;
1225 if (current_entry_count >= global_settings.max_files_in_dir)
1227 logf("chunk mode #3: %d", current_entry_count);
1228 c->dirfull = true;
1229 sort = false;
1230 break ;
1233 if (init && !tcs->ramsearch)
1235 if (!show_search_progress(false, total_count))
1237 tagcache_search_finish(tcs);
1238 return current_entry_count;
1243 if (sort)
1244 qsort(c->dircache + special_entry_count * c->dentry_size,
1245 current_entry_count - special_entry_count,
1246 c->dentry_size, compare);
1248 if (!init)
1250 tagcache_search_finish(tcs);
1251 return current_entry_count;
1254 while (tagcache_get_next(tcs))
1256 if (!tcs->ramsearch)
1258 if (!show_search_progress(false, total_count))
1259 break;
1261 total_count++;
1264 tagcache_search_finish(tcs);
1266 if (!sort && (sort_inverse || sort_limit))
1268 splashf(HZ*4, ID2P(LANG_SHOWDIR_BUFFER_FULL), total_count);
1269 logf("Too small dir buffer");
1270 return 0;
1273 if (sort_limit)
1274 total_count = MIN(total_count, sort_limit);
1276 if (strip)
1278 dptr = c->dircache;
1279 for (i = 0; i < total_count; i++, dptr++)
1281 int len = strlen(dptr->name);
1283 if (len < strip)
1284 continue;
1286 dptr->name = &dptr->name[strip];
1290 return total_count;
1293 static int load_root(struct tree_context *c)
1295 struct tagentry *dptr = (struct tagentry *)c->dircache;
1296 int i;
1298 tc = c;
1299 c->currtable = root;
1300 if (c->dirlevel == 0)
1301 c->currextra = root_menu;
1303 menu = menus[c->currextra];
1304 if (menu == NULL)
1305 return 0;
1307 for (i = 0; i < menu->itemcount; i++)
1309 dptr->name = menu->items[i]->name;
1310 switch (menu->items[i]->type)
1312 case menu_next:
1313 dptr->newtable = navibrowse;
1314 dptr->extraseek = i;
1315 break;
1317 case menu_load:
1318 dptr->newtable = root;
1319 dptr->extraseek = menu->items[i]->link;
1320 break;
1323 dptr++;
1326 current_offset = 0;
1327 current_entry_count = i;
1329 return i;
1332 int tagtree_load(struct tree_context* c)
1334 int count;
1335 int table = c->currtable;
1337 c->dentry_size = sizeof(struct tagentry);
1338 c->dirsindir = 0;
1340 if (!table)
1342 c->dirfull = false;
1343 table = root;
1344 c->currtable = table;
1345 c->currextra = root_menu;
1348 switch (table)
1350 case root:
1351 count = load_root(c);
1352 break;
1354 case allsubentries:
1355 case navibrowse:
1356 logf("navibrowse...");
1357 cpu_boost(true);
1358 count = retrieve_entries(c, &tcs, 0, true);
1359 cpu_boost(false);
1360 break;
1362 default:
1363 logf("Unsupported table %d\n", table);
1364 return -1;
1367 if (count < 0)
1369 c->dirlevel = 0;
1370 count = load_root(c);
1371 splash(HZ, str(LANG_TAGCACHE_BUSY));
1374 /* The _total_ numer of entries available. */
1375 c->dirlength = c->filesindir = count;
1377 return count;
1380 int tagtree_enter(struct tree_context* c)
1382 int rc = 0;
1383 struct tagentry *dptr;
1384 struct mp3entry *id3;
1385 int newextra;
1386 int seek;
1387 int source;
1389 dptr = tagtree_get_entry(c, c->selected_item);
1391 c->dirfull = false;
1392 seek = dptr->extraseek;
1393 if (seek == -1)
1395 if(c->filesindir<=2)
1396 return 0;
1397 srand(current_tick);
1398 dptr = (tagtree_get_entry(c, 2+(rand() % (c->filesindir-2))));
1399 seek = dptr->extraseek;
1401 newextra = dptr->newtable;
1403 if (c->dirlevel >= MAX_DIR_LEVELS)
1404 return 0;
1406 c->selected_item_history[c->dirlevel]=c->selected_item;
1407 c->table_history[c->dirlevel] = c->currtable;
1408 c->extra_history[c->dirlevel] = c->currextra;
1409 c->pos_history[c->dirlevel] = c->firstpos;
1410 c->dirlevel++;
1412 switch (c->currtable) {
1413 case root:
1414 c->currextra = newextra;
1416 if (newextra == root)
1418 menu = menus[seek];
1419 c->currextra = seek;
1422 else if (newextra == navibrowse)
1424 int i, j;
1426 csi = menu->items[seek]->si;
1427 c->currextra = 0;
1429 strncpy(current_title[c->currextra], dptr->name,
1430 sizeof(current_title[0]) - 1);
1432 /* Read input as necessary. */
1433 for (i = 0; i < csi->tagorder_count; i++)
1435 for (j = 0; j < csi->clause_count[i]; j++)
1437 char* searchstring;
1438 source = csi->clause[i][j]->source;
1440 if (source == source_constant)
1441 continue;
1443 searchstring=csi->clause[i][j]->str;
1444 *searchstring = '\0';
1446 id3 = audio_current_track();
1448 if (source == source_current_path && id3)
1450 char *e;
1451 strncpy(searchstring, id3->path, SEARCHSTR_SIZE);
1452 e = strrchr(searchstring, '/');
1453 if (e)
1454 *e = '\0';
1456 else if (source > source_runtime && id3)
1459 int k = source-source_runtime;
1460 int offset = id3_to_search_mapping[k].id3_offset;
1461 char **src = (char**)((char*)id3 + offset);
1462 if (*src)
1464 strncpy(searchstring, *src, SEARCHSTR_SIZE);
1465 searchstring[SEARCHSTR_SIZE-1] = '\0';
1468 else
1470 rc = kbd_input(searchstring, SEARCHSTR_SIZE);
1471 if (rc == -1 || !searchstring[0])
1473 tagtree_exit(c);
1474 return 0;
1476 if (csi->clause[i][j]->numeric)
1477 csi->clause[i][j]->numeric_data = atoi(searchstring);
1484 c->currtable = newextra;
1486 break;
1488 case navibrowse:
1489 case allsubentries:
1490 if (newextra == playtrack)
1492 c->dirlevel--;
1493 /* about to create a new current playlist...
1494 allow user to cancel the operation */
1495 if (!warn_on_pl_erase())
1496 break;
1498 if (tagtree_play_folder(c) >= 0)
1499 rc = 2;
1500 break;
1503 c->currtable = newextra;
1504 csi->result_seek[c->currextra] = seek;
1505 if (c->currextra < csi->tagorder_count-1)
1506 c->currextra++;
1507 else
1508 c->dirlevel--;
1510 /* Update the statusbar title */
1511 strncpy(current_title[c->currextra], dptr->name,
1512 sizeof(current_title[0]) - 1);
1513 break;
1515 default:
1516 c->dirlevel--;
1517 break;
1520 c->selected_item=0;
1521 gui_synclist_select_item(&tree_lists, c->selected_item);
1523 return rc;
1526 void tagtree_exit(struct tree_context* c)
1528 c->dirfull = false;
1529 if (c->dirlevel > 0)
1530 c->dirlevel--;
1531 c->selected_item=c->selected_item_history[c->dirlevel];
1532 gui_synclist_select_item(&tree_lists, c->selected_item);
1533 c->currtable = c->table_history[c->dirlevel];
1534 c->currextra = c->extra_history[c->dirlevel];
1535 c->firstpos = c->pos_history[c->dirlevel];
1538 int tagtree_get_filename(struct tree_context* c, char *buf, int buflen)
1540 struct tagentry *entry;
1542 entry = tagtree_get_entry(c, c->selected_item);
1544 if (!tagcache_search(&tcs, tag_filename))
1545 return -1;
1547 if (!tagcache_retrieve(&tcs, entry->extraseek, tcs.type, buf, buflen))
1549 tagcache_search_finish(&tcs);
1550 return -2;
1553 tagcache_search_finish(&tcs);
1555 return 0;
1558 static bool insert_all_playlist(struct tree_context *c, int position, bool queue)
1560 int i;
1561 char buf[MAX_PATH];
1562 int from, to, direction;
1563 int files_left = c->filesindir;
1565 cpu_boost(true);
1566 if (!tagcache_search(&tcs, tag_filename))
1568 splash(HZ, ID2P(LANG_TAGCACHE_BUSY));
1569 cpu_boost(false);
1570 return false;
1573 if (position == PLAYLIST_REPLACE)
1575 if (playlist_remove_all_tracks(NULL) == 0)
1576 position = PLAYLIST_INSERT_LAST;
1577 else
1579 cpu_boost(false);
1580 return false;
1584 if (position == PLAYLIST_INSERT_FIRST)
1586 from = c->filesindir - 1;
1587 to = -1;
1588 direction = -1;
1590 else
1592 from = 0;
1593 to = c->filesindir;
1594 direction = 1;
1597 for (i = from; i != to; i += direction)
1599 /* Count back to zero */
1600 if (!show_search_progress(false, files_left--))
1601 break;
1603 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, i)->extraseek,
1604 tcs.type, buf, sizeof buf))
1606 continue;
1609 if (playlist_insert_track(NULL, buf, position, queue, false) < 0)
1611 logf("playlist_insert_track failed");
1612 break;
1614 yield();
1616 playlist_sync(NULL);
1617 tagcache_search_finish(&tcs);
1618 cpu_boost(false);
1620 return true;
1623 bool tagtree_insert_selection_playlist(int position, bool queue)
1625 struct tagentry *dptr;
1626 char buf[MAX_PATH];
1627 int dirlevel = tc->dirlevel;
1629 /* We need to set the table to allsubentries. */
1630 show_search_progress(true, 0);
1632 dptr = tagtree_get_entry(tc, tc->selected_item);
1634 /* Insert a single track? */
1635 if (dptr->newtable == playtrack)
1637 if (tagtree_get_filename(tc, buf, sizeof buf) < 0)
1639 logf("tagtree_get_filename failed");
1640 return false;
1642 playlist_insert_track(NULL, buf, position, queue, true);
1644 return true;
1647 if (dptr->newtable == navibrowse)
1649 tagtree_enter(tc);
1650 tagtree_load(tc);
1651 dptr = tagtree_get_entry(tc, tc->selected_item);
1653 else if (dptr->newtable != allsubentries)
1655 logf("unsupported table: %d", dptr->newtable);
1656 return false;
1659 /* Now the current table should be allsubentries. */
1660 if (dptr->newtable != playtrack)
1662 tagtree_enter(tc);
1663 tagtree_load(tc);
1664 dptr = tagtree_get_entry(tc, tc->selected_item);
1666 /* And now the newtable should be playtrack. */
1667 if (dptr->newtable != playtrack)
1669 logf("newtable: %d !!", dptr->newtable);
1670 tc->dirlevel = dirlevel;
1671 return false;
1675 if (tc->filesindir <= 0)
1676 splash(HZ, ID2P(LANG_END_PLAYLIST));
1677 else
1679 logf("insert_all_playlist");
1680 if (!insert_all_playlist(tc, position, queue))
1681 splash(HZ*2, ID2P(LANG_FAILED));
1684 /* Finally return the dirlevel to its original value. */
1685 while (tc->dirlevel > dirlevel)
1686 tagtree_exit(tc);
1687 tagtree_load(tc);
1689 return true;
1692 static int tagtree_play_folder(struct tree_context* c)
1694 if (playlist_create(NULL, NULL) < 0)
1696 logf("Failed creating playlist\n");
1697 return -1;
1700 if (!insert_all_playlist(c, PLAYLIST_INSERT_LAST, false))
1701 return -2;
1703 if (global_settings.playlist_shuffle)
1704 c->selected_item = playlist_shuffle(current_tick, c->selected_item);
1705 if (!global_settings.play_selected)
1706 c->selected_item = 0;
1707 gui_synclist_select_item(&tree_lists, c->selected_item);
1709 playlist_start(c->selected_item,0);
1711 return 0;
1714 struct tagentry* tagtree_get_entry(struct tree_context *c, int id)
1716 struct tagentry *entry = (struct tagentry *)c->dircache;
1717 int realid = id - current_offset;
1719 /* Load the next chunk if necessary. */
1720 if (realid >= current_entry_count || realid < 0)
1722 cpu_boost(true);
1723 if (retrieve_entries(c, &tcs2, MAX(0, id - (current_entry_count / 2)),
1724 false) < 0)
1726 logf("retrieve failed");
1727 cpu_boost(false);
1728 return NULL;
1730 realid = id - current_offset;
1731 cpu_boost(false);
1734 return &entry[realid];
1737 char *tagtree_get_title(struct tree_context* c)
1739 switch (c->currtable)
1741 case root:
1742 return menu->title;
1744 case navibrowse:
1745 case allsubentries:
1746 return current_title[c->currextra];
1749 return "?";
1752 int tagtree_get_attr(struct tree_context* c)
1754 int attr = -1;
1755 switch (c->currtable)
1757 case navibrowse:
1758 if (csi->tagorder[c->currextra] == tag_title)
1759 attr = FILE_ATTR_AUDIO;
1760 else
1761 attr = ATTR_DIRECTORY;
1762 break;
1764 case allsubentries:
1765 attr = FILE_ATTR_AUDIO;
1766 break;
1768 default:
1769 attr = ATTR_DIRECTORY;
1770 break;
1773 return attr;
1776 int tagtree_get_icon(struct tree_context* c)
1778 int icon = Icon_Folder;
1780 if (tagtree_get_attr(c) == FILE_ATTR_AUDIO)
1781 icon = Icon_Audio;
1783 return icon;