Initial 800x480 cabbiev2 port, based on 480x800x16 one
[kugel-rb.git] / apps / tagtree.c
blob75caab01d49be4b7db30d744794fac2e6b9b3934
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.
27 /*#define LOGF_ENABLE*/
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include "string-extra.h"
32 #include "config.h"
33 #include "system.h"
34 #include "kernel.h"
35 #include "splash.h"
36 #include "icons.h"
37 #include "tree.h"
38 #include "action.h"
39 #include "settings.h"
40 #include "tagcache.h"
41 #include "tagtree.h"
42 #include "lang.h"
43 #include "logf.h"
44 #include "playlist.h"
45 #include "keyboard.h"
46 #include "gui/list.h"
47 #include "buffer.h"
48 #include "yesno.h"
49 #include "misc.h"
50 #include "filetypes.h"
51 #include "audio.h"
52 #include "appevents.h"
53 #include "storage.h"
54 #include "dir.h"
56 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
58 static int tagtree_play_folder(struct tree_context* c);
60 #define SEARCHSTR_SIZE 256
62 enum table {
63 ROOT = 1,
64 NAVIBROWSE,
65 ALLSUBENTRIES,
66 PLAYTRACK,
69 static const struct id3_to_search_mapping {
70 char *string;
71 size_t id3_offset;
72 } id3_to_search_mapping[] = {
73 { "", 0 }, /* offset n/a */
74 { "#directory#", 0 }, /* offset n/a */
75 { "#title#", offsetof(struct mp3entry, title) },
76 { "#artist#", offsetof(struct mp3entry, artist) },
77 { "#album#", offsetof(struct mp3entry, album) },
78 { "#genre#", offsetof(struct mp3entry, genre_string) },
79 { "#composer#", offsetof(struct mp3entry, composer) },
80 { "#albumartist#", offsetof(struct mp3entry, albumartist) },
82 enum variables {
83 var_sorttype = 100,
84 var_limit,
85 var_strip,
86 var_menu_start,
87 var_include,
88 var_rootmenu,
89 var_format,
90 menu_next,
91 menu_load,
94 /* Capacity 10 000 entries (for example 10k different artists) */
95 #define UNIQBUF_SIZE (64*1024)
96 static long *uniqbuf;
98 #define MAX_TAGS 5
99 #define MAX_MENU_ID_SIZE 32
101 static bool sort_inverse;
104 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
106 * valid = true
107 * formatstr = "%-3d. %s"
108 * tags[0] = tag_autoscore
109 * tags[1] = tag_title
110 * tag_count = 2
112 * limit = 100
113 * sort_inverse = true
115 struct display_format {
116 char name[32];
117 struct tagcache_search_clause *clause[TAGCACHE_MAX_CLAUSES];
118 int clause_count;
119 char *formatstr;
120 int group_id;
121 int tags[MAX_TAGS];
122 int tag_count;
124 int limit;
125 int strip;
126 bool sort_inverse;
129 static struct display_format *formats[TAGMENU_MAX_FMTS];
130 static int format_count;
132 struct search_instruction {
133 char name[64];
134 int tagorder[MAX_TAGS];
135 int tagorder_count;
136 struct tagcache_search_clause *clause[MAX_TAGS][TAGCACHE_MAX_CLAUSES];
137 int format_id[MAX_TAGS];
138 int clause_count[MAX_TAGS];
139 int result_seek[MAX_TAGS];
142 struct menu_entry {
143 char name[64];
144 int type;
145 struct search_instruction *si;
146 int link;
149 struct menu_root {
150 char title[64];
151 char id[MAX_MENU_ID_SIZE];
152 int itemcount;
153 struct menu_entry *items[TAGMENU_MAX_ITEMS];
156 /* Statusbar text of the current view. */
157 static char current_title[MAX_TAGS][128];
159 static struct menu_root *menus[TAGMENU_MAX_MENUS];
160 static struct menu_root *menu;
161 static struct search_instruction *csi;
162 static const char *strp;
163 static int menu_count;
164 static int rootmenu;
166 static int current_offset;
167 static int current_entry_count;
169 static struct tree_context *tc;
171 static int get_token_str(char *buf, int size)
173 /* Find the start. */
174 while (*strp != '"' && *strp != '\0')
175 strp++;
177 if (*strp == '\0' || *(++strp) == '\0')
178 return -1;
180 /* Read the data. */
181 while (*strp != '"' && *strp != '\0' && --size > 0)
182 *(buf++) = *(strp++);
184 *buf = '\0';
185 if (*strp != '"')
186 return -2;
188 strp++;
190 return 0;
193 #define MATCH(tag,str1,str2,settag) \
194 if (!strcasecmp(str1, str2)) { \
195 *tag = settag; \
196 return 1; \
199 static int get_tag(int *tag)
201 char buf[128];
202 int i;
204 /* Find the start. */
205 while ((*strp == ' ' || *strp == '>') && *strp != '\0')
206 strp++;
208 if (*strp == '\0' || *strp == '?')
209 return 0;
211 for (i = 0; i < (int)sizeof(buf)-1; i++)
213 if (*strp == '\0' || *strp == ' ')
214 break ;
215 buf[i] = *strp;
216 strp++;
218 buf[i] = '\0';
220 MATCH(tag, buf, "album", tag_album);
221 MATCH(tag, buf, "artist", tag_artist);
222 MATCH(tag, buf, "bitrate", tag_bitrate);
223 MATCH(tag, buf, "composer", tag_composer);
224 MATCH(tag, buf, "comment", tag_comment);
225 MATCH(tag, buf, "albumartist", tag_albumartist);
226 MATCH(tag, buf, "ensemble", tag_albumartist);
227 MATCH(tag, buf, "grouping", tag_grouping);
228 MATCH(tag, buf, "genre", tag_genre);
229 MATCH(tag, buf, "length", tag_length);
230 MATCH(tag, buf, "Lm", tag_virt_length_min);
231 MATCH(tag, buf, "Ls", tag_virt_length_sec);
232 MATCH(tag, buf, "Pm", tag_virt_playtime_min);
233 MATCH(tag, buf, "Ps", tag_virt_playtime_sec);
234 MATCH(tag, buf, "title", tag_title);
235 MATCH(tag, buf, "filename", tag_filename);
236 MATCH(tag, buf, "tracknum", tag_tracknumber);
237 MATCH(tag, buf, "discnum", tag_discnumber);
238 MATCH(tag, buf, "year", tag_year);
239 MATCH(tag, buf, "playcount", tag_playcount);
240 MATCH(tag, buf, "rating", tag_rating);
241 MATCH(tag, buf, "lastplayed", tag_lastplayed);
242 MATCH(tag, buf, "commitid", tag_commitid);
243 MATCH(tag, buf, "entryage", tag_virt_entryage);
244 MATCH(tag, buf, "autoscore", tag_virt_autoscore);
245 MATCH(tag, buf, "%sort", var_sorttype);
246 MATCH(tag, buf, "%limit", var_limit);
247 MATCH(tag, buf, "%strip", var_strip);
248 MATCH(tag, buf, "%menu_start", var_menu_start);
249 MATCH(tag, buf, "%include", var_include);
250 MATCH(tag, buf, "%root_menu", var_rootmenu);
251 MATCH(tag, buf, "%format", var_format);
252 MATCH(tag, buf, "->", menu_next);
253 MATCH(tag, buf, "==>", menu_load);
255 logf("NO MATCH: %s\n", buf);
256 if (buf[0] == '?')
257 return 0;
259 return -1;
262 static int get_clause(int *condition)
264 char buf[4];
265 int i;
267 /* Find the start. */
268 while (*strp == ' ' && *strp != '\0')
269 strp++;
271 if (*strp == '\0')
272 return 0;
274 for (i = 0; i < (int)sizeof(buf)-1; i++)
276 if (*strp == '\0' || *strp == ' ')
277 break ;
278 buf[i] = *strp;
279 strp++;
281 buf[i] = '\0';
283 MATCH(condition, buf, "=", clause_is);
284 MATCH(condition, buf, "==", clause_is);
285 MATCH(condition, buf, "!=", clause_is_not);
286 MATCH(condition, buf, ">", clause_gt);
287 MATCH(condition, buf, ">=", clause_gteq);
288 MATCH(condition, buf, "<", clause_lt);
289 MATCH(condition, buf, "<=", clause_lteq);
290 MATCH(condition, buf, "~", clause_contains);
291 MATCH(condition, buf, "!~", clause_not_contains);
292 MATCH(condition, buf, "^", clause_begins_with);
293 MATCH(condition, buf, "!^", clause_not_begins_with);
294 MATCH(condition, buf, "$", clause_ends_with);
295 MATCH(condition, buf, "!$", clause_not_ends_with);
296 MATCH(condition, buf, "@", clause_oneof);
298 return 0;
301 static bool read_clause(struct tagcache_search_clause *clause)
303 char buf[SEARCHSTR_SIZE];
304 unsigned int i;
306 if (get_tag(&clause->tag) <= 0)
307 return false;
309 if (get_clause(&clause->type) <= 0)
310 return false;
312 if (get_token_str(buf, sizeof buf) < 0)
313 return false;
315 for (i=0; i<ARRAYLEN(id3_to_search_mapping); i++)
317 if (!strcasecmp(buf, id3_to_search_mapping[i].string))
318 break;
321 if (i<ARRAYLEN(id3_to_search_mapping)) /* runtime search operand found */
323 clause->source = source_runtime+i;
324 clause->str = buffer_alloc(SEARCHSTR_SIZE);
326 else
328 clause->source = source_constant;
329 clause->str = buffer_alloc(strlen(buf)+1);
330 strcpy(clause->str, buf);
333 if (TAGCACHE_IS_NUMERIC(clause->tag))
335 clause->numeric = true;
336 clause->numeric_data = atoi(clause->str);
338 else
339 clause->numeric = false;
341 logf("got clause: %d/%d [%s]", clause->tag, clause->type, clause->str);
343 return true;
346 static bool read_variable(char *buf, int size)
348 int condition;
350 if (!get_clause(&condition))
351 return false;
353 if (condition != clause_is)
354 return false;
356 if (get_token_str(buf, size) < 0)
357 return false;
359 return true;
362 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
363 static int get_format_str(struct display_format *fmt)
365 int ret;
366 char buf[128];
367 int i;
369 memset(fmt, 0, sizeof(struct display_format));
371 if (get_token_str(fmt->name, sizeof fmt->name) < 0)
372 return -10;
374 /* Determine the group id */
375 fmt->group_id = 0;
376 for (i = 0; i < format_count; i++)
378 if (!strcasecmp(formats[i]->name, fmt->name))
380 fmt->group_id = formats[i]->group_id;
381 break;
384 if (formats[i]->group_id > fmt->group_id)
385 fmt->group_id = formats[i]->group_id;
388 if (i == format_count)
389 fmt->group_id++;
391 logf("format: (%d) %s", fmt->group_id, fmt->name);
393 if (get_token_str(buf, sizeof buf) < 0)
394 return -10;
396 fmt->formatstr = buffer_alloc(strlen(buf) + 1);
397 strcpy(fmt->formatstr, buf);
399 while (fmt->tag_count < MAX_TAGS)
401 ret = get_tag(&fmt->tags[fmt->tag_count]);
402 if (ret < 0)
403 return -11;
405 if (ret == 0)
406 break;
408 switch (fmt->tags[fmt->tag_count]) {
409 case var_sorttype:
410 if (!read_variable(buf, sizeof buf))
411 return -12;
412 if (!strcasecmp("inverse", buf))
413 fmt->sort_inverse = true;
414 break;
416 case var_limit:
417 if (!read_variable(buf, sizeof buf))
418 return -13;
419 fmt->limit = atoi(buf);
420 break;
422 case var_strip:
423 if (!read_variable(buf, sizeof buf))
424 return -14;
425 fmt->strip = atoi(buf);
426 break;
428 default:
429 fmt->tag_count++;
433 return 1;
436 static int add_format(const char *buf)
438 strp = buf;
440 if (formats[format_count] == NULL)
441 formats[format_count] = buffer_alloc(sizeof(struct display_format));
443 memset(formats[format_count], 0, sizeof(struct display_format));
444 if (get_format_str(formats[format_count]) < 0)
446 logf("get_format_str() parser failed!");
447 return -4;
450 while (*strp != '\0' && *strp != '?')
451 strp++;
453 if (*strp == '?')
455 int clause_count = 0;
456 strp++;
458 while (1)
460 if (clause_count >= TAGCACHE_MAX_CLAUSES)
462 logf("too many clauses");
463 break;
466 formats[format_count]->clause[clause_count] =
467 buffer_alloc(sizeof(struct tagcache_search_clause));
469 if (!read_clause(formats[format_count]->clause[clause_count]))
470 break;
472 clause_count++;
475 formats[format_count]->clause_count = clause_count;
478 format_count++;
480 return 1;
483 static int get_condition(struct search_instruction *inst)
485 int clause_count;
486 char buf[128];
488 switch (*strp)
490 case '=':
492 int i;
494 if (get_token_str(buf, sizeof buf) < 0)
495 return -1;
497 for (i = 0; i < format_count; i++)
499 if (!strcasecmp(formats[i]->name, buf))
500 break;
503 if (i == format_count)
505 logf("format not found: %s", buf);
506 return -2;
509 inst->format_id[inst->tagorder_count] = formats[i]->group_id;
510 return 1;
512 case '?':
513 case ' ':
514 case '&':
515 strp++;
516 return 1;
517 case '-':
518 case '\0':
519 return 0;
522 clause_count = inst->clause_count[inst->tagorder_count];
523 if (clause_count >= TAGCACHE_MAX_CLAUSES)
525 logf("Too many clauses");
526 return false;
529 inst->clause[inst->tagorder_count][clause_count] =
530 buffer_alloc(sizeof(struct tagcache_search_clause));
532 if (!read_clause(inst->clause[inst->tagorder_count][clause_count]))
533 return -1;
535 inst->clause_count[inst->tagorder_count]++;
537 return 1;
540 /* example search:
541 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
542 * : album ? year >= "2000" : songs
543 * ^ begins with
544 * * contains
545 * $ ends with
548 static bool parse_search(struct menu_entry *entry, const char *str)
550 int ret;
551 int type;
552 struct search_instruction *inst = entry->si;
553 char buf[MAX_PATH];
554 int i;
555 struct menu_root *new_menu;
557 strp = str;
559 /* Parse entry name */
560 if (get_token_str(entry->name, sizeof entry->name) < 0)
562 logf("No name found.");
563 return false;
566 /* Parse entry type */
567 if (get_tag(&entry->type) <= 0)
568 return false;
570 if (entry->type == menu_load)
572 if (get_token_str(buf, sizeof buf) < 0)
573 return false;
575 /* Find the matching root menu or "create" it */
576 for (i = 0; i < menu_count; i++)
578 if (!strcasecmp(menus[i]->id, buf))
580 entry->link = i;
581 return true;
585 if (menu_count >= TAGMENU_MAX_MENUS)
587 logf("max menucount reached");
588 return false;
591 /* Allocate a new menu unless link is found. */
592 menus[menu_count] = buffer_alloc(sizeof(struct menu_root));
593 new_menu = menus[menu_count];
594 memset(new_menu, 0, sizeof(struct menu_root));
595 strlcpy(new_menu->id, buf, MAX_MENU_ID_SIZE);
596 entry->link = menu_count;
597 ++menu_count;
599 return true;
602 if (entry->type != menu_next)
603 return false;
605 while (inst->tagorder_count < MAX_TAGS)
607 ret = get_tag(&inst->tagorder[inst->tagorder_count]);
608 if (ret < 0)
610 logf("Parse error #1");
611 logf("%s", strp);
612 return false;
615 if (ret == 0)
616 break ;
618 logf("tag: %d", inst->tagorder[inst->tagorder_count]);
620 while ( (ret = get_condition(inst)) > 0 ) ;
621 if (ret < 0)
622 return false;
624 inst->tagorder_count++;
626 if (get_tag(&type) <= 0 || type != menu_next)
627 break;
630 return true;
633 static int compare(const void *p1, const void *p2)
635 struct tagentry *e1 = (struct tagentry *)p1;
636 struct tagentry *e2 = (struct tagentry *)p2;
638 if (sort_inverse)
639 return strncasecmp(e2->name, e1->name, MAX_PATH);
641 return strncasecmp(e1->name, e2->name, MAX_PATH);
644 static void tagtree_buffer_event(void *data)
646 struct tagcache_search tcs;
647 struct mp3entry *id3 = (struct mp3entry*)data;
649 /* Do not gather data unless proper setting has been enabled. */
650 if (!global_settings.runtimedb)
651 return;
653 logf("be:%s", id3->path);
655 while (! tagcache_is_usable())
656 yield();
658 if (!tagcache_find_index(&tcs, id3->path))
660 logf("tc stat: not found: %s", id3->path);
661 return;
664 id3->playcount = tagcache_get_numeric(&tcs, tag_playcount);
665 if (!id3->rating)
666 id3->rating = tagcache_get_numeric(&tcs, tag_rating);
667 id3->lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
668 id3->score = tagcache_get_numeric(&tcs, tag_virt_autoscore) / 10;
669 id3->playtime = tagcache_get_numeric(&tcs, tag_playtime);
671 /* Store our tagcache index pointer. */
672 id3->tagcache_idx = tcs.idx_id+1;
674 tagcache_search_finish(&tcs);
677 static void tagtree_track_finish_event(void *data)
679 long playcount;
680 long playtime;
681 long lastplayed;
682 long tagcache_idx;
683 struct mp3entry *id3 = (struct mp3entry*)data;
685 /* Do not gather data unless proper setting has been enabled. */
686 if (!global_settings.runtimedb)
688 logf("runtimedb gathering not enabled");
689 return;
692 tagcache_idx=id3->tagcache_idx;
693 if (!tagcache_idx)
695 logf("No tagcache index pointer found");
696 return;
698 tagcache_idx--;
700 /* Don't process unplayed tracks. */
701 if (id3->elapsed == 0)
703 logf("not logging unplayed track");
704 return;
707 playcount = id3->playcount + 1;
708 lastplayed = tagcache_increase_serial();
709 if (lastplayed < 0)
711 logf("incorrect tc serial:%ld", lastplayed);
712 return;
715 /* Ignore the last 15s (crossfade etc.) */
716 playtime = id3->playtime + MIN(id3->length, id3->elapsed + 15 * 1000);
718 logf("ube:%s", id3->path);
719 logf("-> %ld/%ld", playcount, playtime);
720 logf("-> %ld/%ld/%ld", id3->elapsed, id3->length, MIN(id3->length, id3->elapsed + 15 * 1000));
722 /* Queue the updates to the tagcache system. */
723 tagcache_update_numeric(tagcache_idx, tag_playcount, playcount);
724 tagcache_update_numeric(tagcache_idx, tag_playtime, playtime);
725 tagcache_update_numeric(tagcache_idx, tag_lastplayed, lastplayed);
728 bool tagtree_export(void)
730 struct tagcache_search tcs;
732 splash(0, str(LANG_CREATING));
733 if (!tagcache_create_changelog(&tcs))
735 splash(HZ*2, ID2P(LANG_FAILED));
738 return false;
741 bool tagtree_import(void)
743 splash(0, ID2P(LANG_WAIT));
744 if (!tagcache_import_changelog())
746 splash(HZ*2, ID2P(LANG_FAILED));
749 return false;
752 static bool parse_menu(const char *filename);
754 static int parse_line(int n, const char *buf, void *parameters)
756 char data[256];
757 int variable;
758 static bool read_menu;
759 int i;
761 (void)parameters;
763 logf("parse:%d/%s", n, buf);
765 /* First line, do initialisation. */
766 if (n == 0)
768 if (strcasecmp(TAGNAVI_VERSION, buf))
770 logf("Version mismatch");
771 return -1;
774 read_menu = false;
777 if (buf[0] == '#')
778 return 0;
780 if (buf[0] == '\0')
782 if (read_menu)
784 /* End the menu */
785 read_menu = false;
787 return 0;
790 if (!read_menu)
792 strp = buf;
793 if (get_tag(&variable) <= 0)
794 return 0;
796 switch (variable)
798 case var_format:
799 if (add_format(strp) < 0)
801 logf("Format add fail: %s", data);
803 break;
805 case var_include:
806 if (get_token_str(data, sizeof(data)) < 0)
808 logf("%%include empty");
809 return 0;
812 if (!parse_menu(data))
814 logf("Load menu fail: %s", data);
816 break;
818 case var_menu_start:
819 if (menu_count >= TAGMENU_MAX_MENUS)
821 logf("max menucount reached");
822 return 0;
825 if (get_token_str(data, sizeof data) < 0)
827 logf("%%menu_start id empty");
828 return 0;
831 menu = NULL;
832 for (i = 0; i < menu_count; i++)
834 if (!strcasecmp(menus[i]->id, data))
836 menu = menus[i];
840 if (menu == NULL)
842 menus[menu_count] = buffer_alloc(sizeof(struct menu_root));
843 menu = menus[menu_count];
844 ++menu_count;
845 memset(menu, 0, sizeof(struct menu_root));
846 strlcpy(menu->id, data, MAX_MENU_ID_SIZE);
849 if (get_token_str(menu->title, sizeof(menu->title)) < 0)
851 logf("%%menu_start title empty");
852 return 0;
854 logf("menu: %s", menu->title);
855 read_menu = true;
856 break;
858 case var_rootmenu:
859 /* Only set root menu once. */
860 if (rootmenu >= 0)
861 break;
863 if (get_token_str(data, sizeof(data)) < 0)
865 logf("%%rootmenu empty");
866 return 0;
869 for (i = 0; i < menu_count; i++)
871 if (!strcasecmp(menus[i]->id, data))
873 rootmenu = i;
876 break;
879 return 0;
882 if (menu->itemcount >= TAGMENU_MAX_ITEMS)
884 logf("max itemcount reached");
885 return 0;
888 /* Allocate */
889 if (menu->items[menu->itemcount] == NULL)
891 menu->items[menu->itemcount] = buffer_alloc(sizeof(struct menu_entry));
892 memset(menu->items[menu->itemcount], 0, sizeof(struct menu_entry));
893 menu->items[menu->itemcount]->si = buffer_alloc(sizeof(struct search_instruction));
896 memset(menu->items[menu->itemcount]->si, 0, sizeof(struct search_instruction));
897 if (!parse_search(menu->items[menu->itemcount], buf))
898 return 0;
900 menu->itemcount++;
902 return 0;
905 static bool parse_menu(const char *filename)
907 int fd;
908 char buf[1024];
910 if (menu_count >= TAGMENU_MAX_MENUS)
912 logf("max menucount reached");
913 return false;
916 fd = open(filename, O_RDONLY);
917 if (fd < 0)
919 logf("Search instruction file not found.");
920 return false;
923 /* Now read file for real, parsing into si */
924 fast_readline(fd, buf, sizeof buf, NULL, parse_line);
925 close(fd);
927 return true;
930 void tagtree_init(void)
932 format_count = 0;
933 menu_count = 0;
934 menu = NULL;
935 rootmenu = -1;
936 parse_menu(FILE_SEARCH_INSTRUCTIONS);
938 /* If no root menu is set, assume it's the first single menu
939 * we have. That shouldn't normally happen. */
940 if (rootmenu < 0)
941 rootmenu = 0;
943 uniqbuf = buffer_alloc(UNIQBUF_SIZE);
945 add_event(PLAYBACK_EVENT_TRACK_BUFFER, false, tagtree_buffer_event);
946 add_event(PLAYBACK_EVENT_TRACK_FINISH, false, tagtree_track_finish_event);
949 static bool show_search_progress(bool init, int count)
951 static int last_tick = 0;
953 /* Don't show splashes for 1/2 second after starting search */
954 if (init)
956 last_tick = current_tick + HZ/2;
957 return true;
960 /* Update progress every 1/10 of a second */
961 if (TIME_AFTER(current_tick, last_tick + HZ/10))
963 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG), count, str(LANG_OFF_ABORT));
964 if (action_userabort(TIMEOUT_NOBLOCK))
965 return false;
966 last_tick = current_tick;
967 yield();
970 return true;
973 static int format_str(struct tagcache_search *tcs, struct display_format *fmt,
974 char *buf, int buf_size)
976 char fmtbuf[8];
977 bool read_format = false;
978 int fmtbuf_pos = 0;
979 int parpos = 0;
980 int buf_pos = 0;
981 int i;
983 memset(buf, 0, buf_size);
984 for (i = 0; fmt->formatstr[i] != '\0'; i++)
986 if (fmt->formatstr[i] == '%')
988 read_format = true;
989 fmtbuf_pos = 0;
990 if (parpos >= fmt->tag_count)
992 logf("too many format tags");
993 return -1;
997 if (read_format)
999 fmtbuf[fmtbuf_pos++] = fmt->formatstr[i];
1000 if (fmtbuf_pos >= buf_size)
1002 logf("format parse error");
1003 return -2;
1006 if (fmt->formatstr[i] == 's')
1008 fmtbuf[fmtbuf_pos] = '\0';
1009 read_format = false;
1010 if (fmt->tags[parpos] == tcs->type)
1012 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf, tcs->result);
1014 else
1016 /* Need to fetch the tag data. */
1017 if (!tagcache_retrieve(tcs, tcs->idx_id, fmt->tags[parpos],
1018 &buf[buf_pos], buf_size - buf_pos))
1020 logf("retrieve failed");
1021 return -3;
1024 buf_pos += strlen(&buf[buf_pos]);
1025 parpos++;
1027 else if (fmt->formatstr[i] == 'd')
1029 fmtbuf[fmtbuf_pos] = '\0';
1030 read_format = false;
1031 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf,
1032 tagcache_get_numeric(tcs, fmt->tags[parpos]));
1033 buf_pos += strlen(&buf[buf_pos]);
1034 parpos++;
1036 continue;
1039 buf[buf_pos++] = fmt->formatstr[i];
1041 if (buf_pos - 1 >= buf_size)
1043 logf("buffer overflow");
1044 return -4;
1048 buf[buf_pos++] = '\0';
1050 return 0;
1053 static int retrieve_entries(struct tree_context *c, int offset, bool init)
1055 struct tagcache_search tcs;
1056 struct tagentry *dptr = (struct tagentry *)c->dircache;
1057 struct display_format *fmt;
1058 int i;
1059 int namebufused = 0;
1060 int total_count = 0;
1061 int special_entry_count = 0;
1062 int level = c->currextra;
1063 int tag;
1064 bool sort = false;
1065 int sort_limit;
1066 int strip;
1068 /* Show search progress straight away if the disk needs to spin up,
1069 otherwise show it after the normal 1/2 second delay */
1070 show_search_progress(
1071 #ifdef HAVE_DISK_STORAGE
1072 storage_disk_is_active()
1073 #else
1074 true
1075 #endif
1076 , 0);
1078 if (c->currtable == ALLSUBENTRIES)
1080 tag = tag_title;
1081 level--;
1083 else
1084 tag = csi->tagorder[level];
1086 if (!tagcache_search(&tcs, tag))
1087 return -1;
1089 /* Prevent duplicate entries in the search list. */
1090 tagcache_search_set_uniqbuf(&tcs, uniqbuf, UNIQBUF_SIZE);
1092 if (level || csi->clause_count[0] || TAGCACHE_IS_NUMERIC(tag))
1093 sort = true;
1095 for (i = 0; i < level; i++)
1097 if (TAGCACHE_IS_NUMERIC(csi->tagorder[i]))
1099 static struct tagcache_search_clause cc;
1101 memset(&cc, 0, sizeof(struct tagcache_search_clause));
1102 cc.tag = csi->tagorder[i];
1103 cc.type = clause_is;
1104 cc.numeric = true;
1105 cc.numeric_data = csi->result_seek[i];
1106 tagcache_search_add_clause(&tcs, &cc);
1108 else
1110 tagcache_search_add_filter(&tcs, csi->tagorder[i],
1111 csi->result_seek[i]);
1115 for (i = 0; i <= level; i++)
1117 int j;
1119 for (j = 0; j < csi->clause_count[i]; j++)
1120 tagcache_search_add_clause(&tcs, csi->clause[i][j]);
1123 current_offset = offset;
1124 current_entry_count = 0;
1125 c->dirfull = false;
1127 fmt = NULL;
1128 for (i = 0; i < format_count; i++)
1130 if (formats[i]->group_id == csi->format_id[level])
1131 fmt = formats[i];
1134 if (fmt)
1136 sort_inverse = fmt->sort_inverse;
1137 sort_limit = fmt->limit;
1138 strip = fmt->strip;
1139 sort = true;
1141 else
1143 sort_inverse = false;
1144 sort_limit = 0;
1145 strip = 0;
1148 if (tag != tag_title && tag != tag_filename)
1150 if (offset == 0)
1152 dptr->newtable = ALLSUBENTRIES;
1153 dptr->name = str(LANG_TAGNAVI_ALL_TRACKS);
1154 dptr++;
1155 current_entry_count++;
1157 if (offset <= 1)
1159 dptr->newtable = NAVIBROWSE;
1160 dptr->name = str(LANG_TAGNAVI_RANDOM);
1161 dptr->extraseek = -1;
1162 dptr++;
1163 current_entry_count++;
1165 special_entry_count+=2;
1168 total_count += special_entry_count;
1170 while (tagcache_get_next(&tcs))
1172 if (total_count++ < offset)
1173 continue;
1175 if ( strcmp(tcs.result , UNTAGGED ) == 0)
1177 tcs.result_len = strlcpy(tcs.result,
1178 str(LANG_TAGNAVI_UNTAGGED), TAG_MAXLEN )+1;
1181 dptr->newtable = NAVIBROWSE;
1182 if (tag == tag_title || tag == tag_filename)
1184 dptr->newtable = PLAYTRACK;
1185 dptr->extraseek = tcs.idx_id;
1187 else
1188 dptr->extraseek = tcs.result_seek;
1190 fmt = NULL;
1191 /* Check the format */
1192 for (i = 0; i < format_count; i++)
1194 if (formats[i]->group_id != csi->format_id[level])
1195 continue;
1197 if (tagcache_check_clauses(&tcs, formats[i]->clause,
1198 formats[i]->clause_count))
1200 fmt = formats[i];
1201 break;
1205 if (!tcs.ramresult || fmt)
1207 char buf[MAX_PATH];
1209 if (fmt)
1211 if (format_str(&tcs, fmt, buf, sizeof buf) < 0)
1213 logf("format_str() failed");
1214 tagcache_search_finish(&tcs);
1215 return 0;
1219 dptr->name = &c->name_buffer[namebufused];
1220 if (fmt)
1221 namebufused += strlen(buf)+1;
1222 else
1223 namebufused += tcs.result_len;
1225 if (namebufused >= c->name_buffer_size)
1227 logf("chunk mode #2: %d", current_entry_count);
1228 c->dirfull = true;
1229 sort = false;
1230 break ;
1232 if (fmt)
1233 strcpy(dptr->name, buf);
1234 else
1235 strcpy(dptr->name, tcs.result);
1237 else
1238 dptr->name = tcs.result;
1240 dptr++;
1241 current_entry_count++;
1243 if (current_entry_count >= global_settings.max_files_in_dir)
1245 logf("chunk mode #3: %d", current_entry_count);
1246 c->dirfull = true;
1247 sort = false;
1248 break ;
1251 if (init && !tcs.ramsearch)
1253 if (!show_search_progress(false, total_count))
1254 { /* user aborted */
1255 tagcache_search_finish(&tcs);
1256 return current_entry_count;
1261 if (sort)
1262 qsort(c->dircache + special_entry_count * c->dentry_size,
1263 current_entry_count - special_entry_count,
1264 c->dentry_size, compare);
1266 if (!init)
1268 tagcache_search_finish(&tcs);
1269 return current_entry_count;
1272 while (tagcache_get_next(&tcs))
1274 if (!tcs.ramsearch)
1276 if (!show_search_progress(false, total_count))
1277 break;
1279 total_count++;
1282 tagcache_search_finish(&tcs);
1284 if (!sort && (sort_inverse || sort_limit))
1286 splashf(HZ*4, ID2P(LANG_SHOWDIR_BUFFER_FULL), total_count);
1287 logf("Too small dir buffer");
1288 return 0;
1291 if (sort_limit)
1292 total_count = MIN(total_count, sort_limit);
1294 if (strip)
1296 dptr = c->dircache;
1297 for (i = 0; i < total_count; i++, dptr++)
1299 int len = strlen(dptr->name);
1301 if (len < strip)
1302 continue;
1304 dptr->name = &dptr->name[strip];
1308 return total_count;
1312 static int load_root(struct tree_context *c)
1314 struct tagentry *dptr = (struct tagentry *)c->dircache;
1315 int i;
1317 tc = c;
1318 c->currtable = ROOT;
1319 if (c->dirlevel == 0)
1320 c->currextra = rootmenu;
1322 menu = menus[c->currextra];
1323 if (menu == NULL)
1324 return 0;
1326 for (i = 0; i < menu->itemcount; i++)
1328 dptr->name = menu->items[i]->name;
1329 switch (menu->items[i]->type)
1331 case menu_next:
1332 dptr->newtable = NAVIBROWSE;
1333 dptr->extraseek = i;
1334 break;
1336 case menu_load:
1337 dptr->newtable = ROOT;
1338 dptr->extraseek = menu->items[i]->link;
1339 break;
1342 dptr++;
1345 current_offset = 0;
1346 current_entry_count = i;
1348 return i;
1351 int tagtree_load(struct tree_context* c)
1353 int count;
1354 int table = c->currtable;
1356 c->dentry_size = sizeof(struct tagentry);
1357 c->dirsindir = 0;
1359 if (!table)
1361 c->dirfull = false;
1362 table = ROOT;
1363 c->currtable = table;
1364 c->currextra = rootmenu;
1367 switch (table)
1369 case ROOT:
1370 count = load_root(c);
1371 break;
1373 case ALLSUBENTRIES:
1374 case NAVIBROWSE:
1375 logf("navibrowse...");
1376 cpu_boost(true);
1377 count = retrieve_entries(c, 0, true);
1378 cpu_boost(false);
1379 break;
1381 default:
1382 logf("Unsupported table %d\n", table);
1383 return -1;
1386 if (count < 0)
1388 c->dirlevel = 0;
1389 count = load_root(c);
1390 splash(HZ, str(LANG_TAGCACHE_BUSY));
1393 /* The _total_ numer of entries available. */
1394 c->dirlength = c->filesindir = count;
1396 return count;
1399 int tagtree_enter(struct tree_context* c)
1401 int rc = 0;
1402 struct tagentry *dptr;
1403 struct mp3entry *id3;
1404 int newextra;
1405 int seek;
1406 int source;
1408 dptr = tagtree_get_entry(c, c->selected_item);
1410 c->dirfull = false;
1411 seek = dptr->extraseek;
1412 if (seek == -1)
1414 if(c->filesindir<=2)
1415 return 0;
1416 srand(current_tick);
1417 dptr = (tagtree_get_entry(c, 2+(rand() % (c->filesindir-2))));
1418 seek = dptr->extraseek;
1420 newextra = dptr->newtable;
1422 if (c->dirlevel >= MAX_DIR_LEVELS)
1423 return 0;
1425 c->selected_item_history[c->dirlevel]=c->selected_item;
1426 c->table_history[c->dirlevel] = c->currtable;
1427 c->extra_history[c->dirlevel] = c->currextra;
1428 c->pos_history[c->dirlevel] = c->firstpos;
1429 c->dirlevel++;
1431 switch (c->currtable) {
1432 case ROOT:
1433 c->currextra = newextra;
1435 if (newextra == ROOT)
1437 menu = menus[seek];
1438 c->currextra = seek;
1441 else if (newextra == NAVIBROWSE)
1443 int i, j;
1445 csi = menu->items[seek]->si;
1446 c->currextra = 0;
1448 strlcpy(current_title[c->currextra], dptr->name,
1449 sizeof(current_title[0]));
1451 /* Read input as necessary. */
1452 for (i = 0; i < csi->tagorder_count; i++)
1454 for (j = 0; j < csi->clause_count[i]; j++)
1456 char* searchstring;
1457 source = csi->clause[i][j]->source;
1459 if (source == source_constant)
1460 continue;
1462 searchstring=csi->clause[i][j]->str;
1463 *searchstring = '\0';
1465 id3 = audio_current_track();
1467 if (source == source_current_path && id3)
1469 char *e;
1470 strlcpy(searchstring, id3->path, SEARCHSTR_SIZE);
1471 e = strrchr(searchstring, '/');
1472 if (e)
1473 *e = '\0';
1475 else if (source > source_runtime && id3)
1478 int k = source-source_runtime;
1479 int offset = id3_to_search_mapping[k].id3_offset;
1480 char **src = (char**)((char*)id3 + offset);
1481 if (*src)
1483 strlcpy(searchstring, *src, SEARCHSTR_SIZE);
1486 else
1488 rc = kbd_input(searchstring, SEARCHSTR_SIZE);
1489 if (rc < 0 || !searchstring[0])
1491 tagtree_exit(c);
1492 return 0;
1494 if (csi->clause[i][j]->numeric)
1495 csi->clause[i][j]->numeric_data = atoi(searchstring);
1502 c->currtable = newextra;
1504 break;
1506 case NAVIBROWSE:
1507 case ALLSUBENTRIES:
1508 if (newextra == PLAYTRACK)
1510 if (global_settings.party_mode && audio_status()) {
1511 splash(HZ, ID2P(LANG_PARTY_MODE));
1512 break;
1514 c->dirlevel--;
1515 /* about to create a new current playlist...
1516 allow user to cancel the operation */
1517 if (!warn_on_pl_erase())
1518 break;
1519 if (tagtree_play_folder(c) >= 0)
1520 rc = 2;
1521 break;
1524 c->currtable = newextra;
1525 csi->result_seek[c->currextra] = seek;
1526 if (c->currextra < csi->tagorder_count-1)
1527 c->currextra++;
1528 else
1529 c->dirlevel--;
1531 /* Update the statusbar title */
1532 strlcpy(current_title[c->currextra], dptr->name,
1533 sizeof(current_title[0]));
1534 break;
1536 default:
1537 c->dirlevel--;
1538 break;
1541 c->selected_item=0;
1542 gui_synclist_select_item(&tree_lists, c->selected_item);
1544 return rc;
1547 void tagtree_exit(struct tree_context* c)
1549 c->dirfull = false;
1550 if (c->dirlevel > 0)
1551 c->dirlevel--;
1552 c->selected_item=c->selected_item_history[c->dirlevel];
1553 gui_synclist_select_item(&tree_lists, c->selected_item);
1554 c->currtable = c->table_history[c->dirlevel];
1555 c->currextra = c->extra_history[c->dirlevel];
1556 c->firstpos = c->pos_history[c->dirlevel];
1559 int tagtree_get_filename(struct tree_context* c, char *buf, int buflen)
1561 struct tagcache_search tcs;
1562 struct tagentry *entry;
1564 entry = tagtree_get_entry(c, c->selected_item);
1566 if (!tagcache_search(&tcs, tag_filename))
1567 return -1;
1569 if (!tagcache_retrieve(&tcs, entry->extraseek, tcs.type, buf, buflen))
1571 tagcache_search_finish(&tcs);
1572 return -2;
1575 tagcache_search_finish(&tcs);
1577 return 0;
1580 static bool insert_all_playlist(struct tree_context *c, int position, bool queue)
1582 struct tagcache_search tcs;
1583 int i;
1584 char buf[MAX_PATH];
1585 int from, to, direction;
1586 int files_left = c->filesindir;
1588 cpu_boost(true);
1589 if (!tagcache_search(&tcs, tag_filename))
1591 splash(HZ, ID2P(LANG_TAGCACHE_BUSY));
1592 cpu_boost(false);
1593 return false;
1596 if (position == PLAYLIST_REPLACE)
1598 if (playlist_remove_all_tracks(NULL) == 0)
1599 position = PLAYLIST_INSERT_LAST;
1600 else
1602 cpu_boost(false);
1603 return false;
1607 if (position == PLAYLIST_INSERT_FIRST)
1609 from = c->filesindir - 1;
1610 to = -1;
1611 direction = -1;
1613 else
1615 from = 0;
1616 to = c->filesindir;
1617 direction = 1;
1620 for (i = from; i != to; i += direction)
1622 /* Count back to zero */
1623 if (!show_search_progress(false, files_left--))
1624 break;
1626 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, i)->extraseek,
1627 tcs.type, buf, sizeof buf))
1629 continue;
1632 if (playlist_insert_track(NULL, buf, position, queue, false) < 0)
1634 logf("playlist_insert_track failed");
1635 break;
1637 yield();
1639 playlist_sync(NULL);
1640 tagcache_search_finish(&tcs);
1641 cpu_boost(false);
1643 return true;
1646 bool tagtree_insert_selection_playlist(int position, bool queue)
1648 struct tagentry *dptr;
1649 char buf[MAX_PATH];
1650 int dirlevel = tc->dirlevel;
1652 show_search_progress(
1653 #ifdef HAVE_DISK_STORAGE
1654 storage_disk_is_active()
1655 #else
1656 true
1657 #endif
1658 , 0);
1661 /* We need to set the table to allsubentries. */
1662 dptr = tagtree_get_entry(tc, tc->selected_item);
1664 /* Insert a single track? */
1665 if (dptr->newtable == PLAYTRACK)
1667 if (tagtree_get_filename(tc, buf, sizeof buf) < 0)
1669 logf("tagtree_get_filename failed");
1670 return false;
1672 playlist_insert_track(NULL, buf, position, queue, true);
1674 return true;
1677 if (dptr->newtable == NAVIBROWSE)
1679 tagtree_enter(tc);
1680 tagtree_load(tc);
1681 dptr = tagtree_get_entry(tc, tc->selected_item);
1683 else if (dptr->newtable != ALLSUBENTRIES)
1685 logf("unsupported table: %d", dptr->newtable);
1686 return false;
1689 /* Now the current table should be allsubentries. */
1690 if (dptr->newtable != PLAYTRACK)
1692 tagtree_enter(tc);
1693 tagtree_load(tc);
1694 dptr = tagtree_get_entry(tc, tc->selected_item);
1696 /* And now the newtable should be playtrack. */
1697 if (dptr->newtable != PLAYTRACK)
1699 logf("newtable: %d !!", dptr->newtable);
1700 tc->dirlevel = dirlevel;
1701 return false;
1705 if (tc->filesindir <= 0)
1706 splash(HZ, ID2P(LANG_END_PLAYLIST));
1707 else
1709 logf("insert_all_playlist");
1710 if (!insert_all_playlist(tc, position, queue))
1711 splash(HZ*2, ID2P(LANG_FAILED));
1714 /* Finally return the dirlevel to its original value. */
1715 while (tc->dirlevel > dirlevel)
1716 tagtree_exit(tc);
1717 tagtree_load(tc);
1719 return true;
1722 static int tagtree_play_folder(struct tree_context* c)
1724 if (playlist_create(NULL, NULL) < 0)
1726 logf("Failed creating playlist\n");
1727 return -1;
1730 if (!insert_all_playlist(c, PLAYLIST_INSERT_LAST, false))
1731 return -2;
1733 if (global_settings.playlist_shuffle)
1734 c->selected_item = playlist_shuffle(current_tick, c->selected_item);
1735 if (!global_settings.play_selected)
1736 c->selected_item = 0;
1737 gui_synclist_select_item(&tree_lists, c->selected_item);
1739 playlist_start(c->selected_item,0);
1740 playlist_get_current()->num_inserted_tracks = 0; /* make warn on playlist erase work */
1741 return 0;
1744 struct tagentry* tagtree_get_entry(struct tree_context *c, int id)
1746 struct tagentry *entry = (struct tagentry *)c->dircache;
1747 int realid = id - current_offset;
1749 /* Load the next chunk if necessary. */
1750 if (realid >= current_entry_count || realid < 0)
1752 cpu_boost(true);
1753 if (retrieve_entries(c, MAX(0, id - (current_entry_count / 2)),
1754 false) < 0)
1756 logf("retrieve failed");
1757 cpu_boost(false);
1758 return NULL;
1760 realid = id - current_offset;
1761 cpu_boost(false);
1764 return &entry[realid];
1767 char *tagtree_get_title(struct tree_context* c)
1769 switch (c->currtable)
1771 case ROOT:
1772 return menu->title;
1774 case NAVIBROWSE:
1775 case ALLSUBENTRIES:
1776 return current_title[c->currextra];
1779 return "?";
1782 int tagtree_get_attr(struct tree_context* c)
1784 int attr = -1;
1785 switch (c->currtable)
1787 case NAVIBROWSE:
1788 if (csi->tagorder[c->currextra] == tag_title)
1789 attr = FILE_ATTR_AUDIO;
1790 else
1791 attr = ATTR_DIRECTORY;
1792 break;
1794 case ALLSUBENTRIES:
1795 attr = FILE_ATTR_AUDIO;
1796 break;
1798 default:
1799 attr = ATTR_DIRECTORY;
1800 break;
1803 return attr;
1806 int tagtree_get_icon(struct tree_context* c)
1808 int icon = Icon_Folder;
1810 if (tagtree_get_attr(c) == FILE_ATTR_AUDIO)
1811 icon = Icon_Audio;
1813 return icon;