Get the user timer working properly consequentially fixing doom without a hack.
[Rockbox.git] / apps / tagtree.c
blob725e90324a8cdbf80f3dc6a0d2c1aa34a14c42ef
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 /**
21 * Basic structure on this file was copied from dbtree.c and modified to
22 * support the tag cache interface.
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include "config.h"
28 #include "system.h"
29 #include "kernel.h"
30 #include "splash.h"
31 #include "icons.h"
32 #include "tree.h"
33 #include "action.h"
34 #include "settings.h"
35 #include "tagcache.h"
36 #include "tagtree.h"
37 #include "lang.h"
38 #include "logf.h"
39 #include "playlist.h"
40 #include "keyboard.h"
41 #include "gui/list.h"
42 #include "buffer.h"
43 #include "atoi.h"
44 #include "playback.h"
45 #include "yesno.h"
46 #include "misc.h"
47 #include "filetypes.h"
48 #include "audio.h"
50 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
52 static int tagtree_play_folder(struct tree_context* c);
54 #define SEARCHSTR_SIZE 256
56 static const struct id3_to_search_mapping {
57 char *string;
58 size_t id3_offset;
59 } id3_to_search_mapping[] = {
60 { "", 0 }, /* offset n/a */
61 { "#directory#", 0 }, /* offset n/a */
62 { "#title#", offsetof(struct mp3entry, title) },
63 { "#artist#", offsetof(struct mp3entry, artist) },
64 { "#album#", offsetof(struct mp3entry, album) },
65 { "#genre#", offsetof(struct mp3entry, genre_string) },
66 { "#composer#", offsetof(struct mp3entry, composer) },
67 { "#albumartist#", offsetof(struct mp3entry, albumartist) },
69 enum variables {
70 var_sorttype = 100,
71 var_limit,
72 var_strip,
73 var_menu_start,
74 var_include,
75 var_rootmenu,
76 var_format,
77 menu_next,
78 menu_load,
81 /* Capacity 10 000 entries (for example 10k different artists) */
82 #define UNIQBUF_SIZE (64*1024)
83 static long *uniqbuf;
85 #define MAX_TAGS 5
86 #define MAX_MENU_ID_SIZE 32
88 static struct tagcache_search tcs, tcs2;
89 static bool sort_inverse;
92 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
94 * valid = true
95 * formatstr = "%-3d. %s"
96 * tags[0] = tag_autoscore
97 * tags[1] = tag_title
98 * tag_count = 2
100 * limit = 100
101 * sort_inverse = true
103 struct display_format {
104 char name[32];
105 struct tagcache_search_clause *clause[TAGCACHE_MAX_CLAUSES];
106 int clause_count;
107 char *formatstr;
108 int group_id;
109 int tags[MAX_TAGS];
110 int tag_count;
112 int limit;
113 int strip;
114 bool sort_inverse;
115 bool sort;
118 static struct display_format *formats[TAGMENU_MAX_FMTS];
119 static int format_count;
121 struct search_instruction {
122 char name[64];
123 int tagorder[MAX_TAGS];
124 int tagorder_count;
125 struct tagcache_search_clause *clause[MAX_TAGS][TAGCACHE_MAX_CLAUSES];
126 int format_id[MAX_TAGS];
127 int clause_count[MAX_TAGS];
128 int result_seek[MAX_TAGS];
131 struct menu_entry {
132 char name[64];
133 int type;
134 struct search_instruction *si;
135 int link;
138 struct root_menu {
139 char title[64];
140 char id[MAX_MENU_ID_SIZE];
141 int itemcount;
142 struct menu_entry *items[TAGMENU_MAX_ITEMS];
145 /* Statusbar text of the current view. */
146 static char current_title[MAX_TAGS][128];
148 static struct root_menu *menus[TAGMENU_MAX_MENUS];
149 static struct root_menu *menu;
150 static struct search_instruction *csi;
151 static const char *strp;
152 static int menu_count;
153 static int root_menu;
155 static int current_offset;
156 static int current_entry_count;
158 static int format_count;
159 static struct tree_context *tc;
161 static int get_token_str(char *buf, int size)
163 /* Find the start. */
164 while (*strp != '"' && *strp != '\0')
165 strp++;
167 if (*strp == '\0' || *(++strp) == '\0')
168 return -1;
170 /* Read the data. */
171 while (*strp != '"' && *strp != '\0' && --size > 0)
172 *(buf++) = *(strp++);
174 *buf = '\0';
175 if (*strp != '"')
176 return -2;
178 strp++;
180 return 0;
183 #define MATCH(tag,str1,str2,settag) \
184 if (!strcasecmp(str1, str2)) { \
185 *tag = settag; \
186 return 1; \
189 static int get_tag(int *tag)
191 char buf[128];
192 int i;
194 /* Find the start. */
195 while ((*strp == ' ' || *strp == '>') && *strp != '\0')
196 strp++;
198 if (*strp == '\0' || *strp == '?')
199 return 0;
201 for (i = 0; i < (int)sizeof(buf)-1; i++)
203 if (*strp == '\0' || *strp == ' ')
204 break ;
205 buf[i] = *strp;
206 strp++;
208 buf[i] = '\0';
210 MATCH(tag, buf, "album", tag_album);
211 MATCH(tag, buf, "artist", tag_artist);
212 MATCH(tag, buf, "bitrate", tag_bitrate);
213 MATCH(tag, buf, "composer", tag_composer);
214 MATCH(tag, buf, "comment", tag_comment);
215 MATCH(tag, buf, "albumartist", tag_albumartist);
216 MATCH(tag, buf, "ensemble", tag_albumartist);
217 MATCH(tag, buf, "grouping", tag_grouping);
218 MATCH(tag, buf, "genre", tag_genre);
219 MATCH(tag, buf, "length", tag_length);
220 MATCH(tag, buf, "Lm", tag_virt_length_min);
221 MATCH(tag, buf, "Ls", tag_virt_length_sec);
222 MATCH(tag, buf, "Pm", tag_virt_playtime_min);
223 MATCH(tag, buf, "Ps", tag_virt_playtime_sec);
224 MATCH(tag, buf, "title", tag_title);
225 MATCH(tag, buf, "filename", tag_filename);
226 MATCH(tag, buf, "tracknum", tag_tracknumber);
227 MATCH(tag, buf, "discnum", tag_discnumber);
228 MATCH(tag, buf, "year", tag_year);
229 MATCH(tag, buf, "playcount", tag_playcount);
230 MATCH(tag, buf, "rating", tag_rating);
231 MATCH(tag, buf, "lastplayed", tag_lastplayed);
232 MATCH(tag, buf, "commitid", tag_commitid);
233 MATCH(tag, buf, "entryage", tag_virt_entryage);
234 MATCH(tag, buf, "autoscore", tag_virt_autoscore);
235 MATCH(tag, buf, "%sort", var_sorttype);
236 MATCH(tag, buf, "%limit", var_limit);
237 MATCH(tag, buf, "%strip", var_strip);
238 MATCH(tag, buf, "%menu_start", var_menu_start);
239 MATCH(tag, buf, "%include", var_include);
240 MATCH(tag, buf, "%root_menu", var_rootmenu);
241 MATCH(tag, buf, "%format", var_format);
242 MATCH(tag, buf, "->", menu_next);
243 MATCH(tag, buf, "==>", menu_load);
245 logf("NO MATCH: %s\n", buf);
246 if (buf[0] == '?')
247 return 0;
249 return -1;
252 static int get_clause(int *condition)
254 char buf[4];
255 int i;
257 /* Find the start. */
258 while (*strp == ' ' && *strp != '\0')
259 strp++;
261 if (*strp == '\0')
262 return 0;
264 for (i = 0; i < (int)sizeof(buf)-1; i++)
266 if (*strp == '\0' || *strp == ' ')
267 break ;
268 buf[i] = *strp;
269 strp++;
271 buf[i] = '\0';
273 MATCH(condition, buf, "=", clause_is);
274 MATCH(condition, buf, "==", clause_is);
275 MATCH(condition, buf, "!=", clause_is_not);
276 MATCH(condition, buf, ">", clause_gt);
277 MATCH(condition, buf, ">=", clause_gteq);
278 MATCH(condition, buf, "<", clause_lt);
279 MATCH(condition, buf, "<=", clause_lteq);
280 MATCH(condition, buf, "~", clause_contains);
281 MATCH(condition, buf, "!~", clause_not_contains);
282 MATCH(condition, buf, "^", clause_begins_with);
283 MATCH(condition, buf, "!^", clause_not_begins_with);
284 MATCH(condition, buf, "$", clause_ends_with);
285 MATCH(condition, buf, "!$", clause_not_ends_with);
286 MATCH(condition, buf, "@", clause_oneof);
288 return 0;
291 static bool read_clause(struct tagcache_search_clause *clause)
293 char buf[SEARCHSTR_SIZE];
294 unsigned int i;
296 if (get_tag(&clause->tag) <= 0)
297 return false;
299 if (get_clause(&clause->type) <= 0)
300 return false;
302 if (get_token_str(buf, sizeof buf) < 0)
303 return false;
305 for (i=0; i<ARRAYLEN(id3_to_search_mapping); i++)
307 if (!strcasecmp(buf, id3_to_search_mapping[i].string))
308 break;
311 if (i<ARRAYLEN(id3_to_search_mapping)) /* runtime search operand found */
313 clause->source = source_runtime+i;
314 clause->str = buffer_alloc(SEARCHSTR_SIZE);
316 else
318 clause->source = source_constant;
319 clause->str = buffer_alloc(strlen(buf)+1);
320 strcpy(clause->str, buf);
323 if (tagcache_is_numeric_tag(clause->tag))
325 clause->numeric = true;
326 clause->numeric_data = atoi(clause->str);
328 else
329 clause->numeric = false;
331 logf("got clause: %d/%d [%s]", clause->tag, clause->type, clause->str);
333 return true;
336 static bool read_variable(char *buf, int size)
338 int condition;
340 if (!get_clause(&condition))
341 return false;
343 if (condition != clause_is)
344 return false;
346 if (get_token_str(buf, size) < 0)
347 return false;
349 return true;
352 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
353 static int get_format_str(struct display_format *fmt)
355 int ret;
356 char buf[128];
357 int i;
359 memset(fmt, 0, sizeof(struct display_format));
361 if (get_token_str(fmt->name, sizeof fmt->name) < 0)
362 return -10;
364 /* Determine the group id */
365 fmt->group_id = 0;
366 for (i = 0; i < format_count; i++)
368 if (!strcasecmp(formats[i]->name, fmt->name))
370 fmt->group_id = formats[i]->group_id;
371 break;
374 if (formats[i]->group_id > fmt->group_id)
375 fmt->group_id = formats[i]->group_id;
378 if (i == format_count)
379 fmt->group_id++;
381 logf("format: (%d) %s", fmt->group_id, fmt->name);
383 if (get_token_str(buf, sizeof buf) < 0)
384 return -10;
386 fmt->formatstr = buffer_alloc(strlen(buf) + 1);
387 strcpy(fmt->formatstr, buf);
389 while (fmt->tag_count < MAX_TAGS)
391 ret = get_tag(&fmt->tags[fmt->tag_count]);
392 if (ret < 0)
393 return -11;
395 if (ret == 0)
396 break;
398 switch (fmt->tags[fmt->tag_count]) {
399 case var_sorttype:
400 if (!read_variable(buf, sizeof buf))
401 return -12;
402 if (!strcasecmp("inverse", buf))
403 fmt->sort_inverse = true;
405 fmt->sort = true;
406 break;
408 case var_limit:
409 if (!read_variable(buf, sizeof buf))
410 return -13;
411 fmt->limit = atoi(buf);
412 break;
414 case var_strip:
415 if (!read_variable(buf, sizeof buf))
416 return -14;
417 fmt->strip = atoi(buf);
418 break;
420 default:
421 fmt->tag_count++;
425 return 1;
428 static int add_format(const char *buf)
430 strp = buf;
432 if (formats[format_count] == NULL)
433 formats[format_count] = buffer_alloc(sizeof(struct display_format));
435 memset(formats[format_count], 0, sizeof(struct display_format));
436 if (get_format_str(formats[format_count]) < 0)
438 logf("get_format_str() parser failed!");
439 return -4;
442 while (*strp != '\0' && *strp != '?')
443 strp++;
445 if (*strp == '?')
447 int clause_count = 0;
448 strp++;
450 while (1)
452 if (clause_count >= TAGCACHE_MAX_CLAUSES)
454 logf("too many clauses");
455 break;
458 formats[format_count]->clause[clause_count] =
459 buffer_alloc(sizeof(struct tagcache_search_clause));
461 if (!read_clause(formats[format_count]->clause[clause_count]))
462 break;
464 clause_count++;
467 formats[format_count]->clause_count = clause_count;
470 format_count++;
472 return 1;
475 static int get_condition(struct search_instruction *inst)
477 int clause_count;
478 char buf[128];
480 switch (*strp)
482 case '=':
484 int i;
486 if (get_token_str(buf, sizeof buf) < 0)
487 return -1;
489 for (i = 0; i < format_count; i++)
491 if (!strcasecmp(formats[i]->name, buf))
492 break;
495 if (i == format_count)
497 logf("format not found: %s", buf);
498 return -2;
501 inst->format_id[inst->tagorder_count] = formats[i]->group_id;
502 return 1;
504 case '?':
505 case ' ':
506 case '&':
507 strp++;
508 return 1;
509 case '-':
510 case '\0':
511 return 0;
514 clause_count = inst->clause_count[inst->tagorder_count];
515 if (clause_count >= TAGCACHE_MAX_CLAUSES)
517 logf("Too many clauses");
518 return false;
521 inst->clause[inst->tagorder_count][clause_count] =
522 buffer_alloc(sizeof(struct tagcache_search_clause));
524 if (!read_clause(inst->clause[inst->tagorder_count][clause_count]))
525 return -1;
527 inst->clause_count[inst->tagorder_count]++;
529 return 1;
532 /* example search:
533 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
534 * : album ? year >= "2000" : songs
535 * ^ begins with
536 * * contains
537 * $ ends with
540 static bool parse_search(struct menu_entry *entry, const char *str)
542 int ret;
543 int type;
544 struct search_instruction *inst = entry->si;
545 char buf[MAX_PATH];
546 int i;
547 struct root_menu *new_menu;
549 strp = str;
551 /* Parse entry name */
552 if (get_token_str(entry->name, sizeof entry->name) < 0)
554 logf("No name found.");
555 return false;
558 /* Parse entry type */
559 if (get_tag(&entry->type) <= 0)
560 return false;
562 if (entry->type == menu_load)
564 if (get_token_str(buf, sizeof buf) < 0)
565 return false;
567 /* Find the matching root menu or "create" it */
568 for (i = 0; i < menu_count; i++)
570 if (!strcasecmp(menus[i]->id, buf))
572 entry->link = i;
573 return true;
577 if (menu_count >= TAGMENU_MAX_MENUS)
579 logf("max menucount reached");
580 return false;
583 /* Allocate a new menu unless link is found. */
584 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
585 new_menu = menus[menu_count];
586 memset(new_menu, 0, sizeof(struct root_menu));
587 strncpy(new_menu->id, buf, MAX_MENU_ID_SIZE-1);
588 entry->link = menu_count;
589 ++menu_count;
591 return true;
594 if (entry->type != menu_next)
595 return false;
597 while (inst->tagorder_count < MAX_TAGS)
599 ret = get_tag(&inst->tagorder[inst->tagorder_count]);
600 if (ret < 0)
602 logf("Parse error #1");
603 logf("%s", strp);
604 return false;
607 if (ret == 0)
608 break ;
610 logf("tag: %d", inst->tagorder[inst->tagorder_count]);
612 while ( (ret = get_condition(inst)) > 0 ) ;
613 if (ret < 0)
614 return false;
616 inst->tagorder_count++;
618 if (get_tag(&type) <= 0 || type != menu_next)
619 break;
622 return true;
625 static int compare(const void *p1, const void *p2)
627 struct tagentry *e1 = (struct tagentry *)p1;
628 struct tagentry *e2 = (struct tagentry *)p2;
630 if (sort_inverse)
631 return strncasecmp(e2->name, e1->name, MAX_PATH);
633 return strncasecmp(e1->name, e2->name, MAX_PATH);
636 void tagtree_buffer_event(struct mp3entry *id3)
638 /* Do not gather data unless proper setting has been enabled. */
639 if (!global_settings.runtimedb)
640 return;
642 logf("be:%d%s", last_track, id3->path);
644 if (!tagcache_find_index(&tcs, id3->path))
646 logf("tc stat: not found: %s", id3->path);
647 return;
650 id3->playcount = tagcache_get_numeric(&tcs, tag_playcount);
651 if (!id3->rating)
652 id3->rating = tagcache_get_numeric(&tcs, tag_rating);
653 id3->lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
654 id3->score = tagcache_get_numeric(&tcs, tag_virt_autoscore) / 10;
655 id3->playtime = tagcache_get_numeric(&tcs, tag_playtime);
657 /* Store our tagcache index pointer. */
658 id3->tagcache_idx = tcs.idx_id;
660 tagcache_search_finish(&tcs);
663 static void tagtree_unbuffer_event(struct mp3entry *id3)
665 long playcount;
666 long playtime;
667 long lastplayed;
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 if (!id3->tagcache_idx)
678 logf("No tagcache index pointer found");
679 return;
682 /* Don't process unplayed tracks. */
683 if (id3->elapsed == 0)
685 logf("not logging unplayed track");
686 return;
689 playcount = id3->playcount + 1;
690 lastplayed = tagcache_increase_serial();
691 if (lastplayed < 0)
693 logf("incorrect tc serial:%ld", lastplayed);
694 return;
697 /* Ignore the last 15s (crossfade etc.) */
698 playtime = id3->playtime + MIN(id3->length, id3->elapsed + 15 * 1000);
700 logf("ube:%s", id3->path);
701 logf("-> %d/%ld/%ld", last_track, playcount, playtime);
702 logf("-> %ld/%ld/%ld", id3->elapsed, id3->length, MIN(id3->length, id3->elapsed + 15 * 1000));
704 /* Queue the updates to the tagcache system. */
705 tagcache_update_numeric(id3->tagcache_idx, tag_playcount, playcount);
706 tagcache_update_numeric(id3->tagcache_idx, tag_playtime, playtime);
707 tagcache_update_numeric(id3->tagcache_idx, tag_lastplayed, lastplayed);
710 bool tagtree_export(void)
712 gui_syncsplash(0, str(LANG_CREATING));
713 if (!tagcache_create_changelog(&tcs))
715 gui_syncsplash(HZ*2, ID2P(LANG_FAILED));
718 return false;
721 bool tagtree_import(void)
723 gui_syncsplash(0, ID2P(LANG_WAIT));
724 if (!tagcache_import_changelog())
726 gui_syncsplash(HZ*2, ID2P(LANG_FAILED));
729 return false;
732 static bool parse_menu(const char *filename);
734 static int parse_line(int n, const char *buf, void *parameters)
736 char data[256];
737 int variable;
738 static bool read_menu;
739 int i;
741 (void)parameters;
743 logf("parse:%d/%s", n, buf);
745 /* First line, do initialisation. */
746 if (n == 0)
748 if (strcasecmp(TAGNAVI_VERSION, buf))
750 logf("Version mismatch");
751 return -1;
754 read_menu = false;
757 if (buf[0] == '#')
758 return 0;
760 if (buf[0] == '\0')
762 if (read_menu)
764 /* End the menu */
765 read_menu = false;
767 return 0;
770 if (!read_menu)
772 strp = buf;
773 if (get_tag(&variable) <= 0)
774 return 0;
776 switch (variable)
778 case var_format:
779 if (add_format(strp) < 0)
781 logf("Format add fail: %s", data);
783 break;
785 case var_include:
786 if (get_token_str(data, sizeof(data)) < 0)
788 logf("%%include empty");
789 return 0;
792 if (!parse_menu(data))
794 logf("Load menu fail: %s", data);
796 break;
798 case var_menu_start:
799 if (menu_count >= TAGMENU_MAX_MENUS)
801 logf("max menucount reached");
802 return 0;
805 if (get_token_str(data, sizeof data) < 0)
807 logf("%%menu_start id empty");
808 return 0;
811 menu = NULL;
812 for (i = 0; i < menu_count; i++)
814 if (!strcasecmp(menus[i]->id, data))
816 menu = menus[i];
820 if (menu == NULL)
822 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
823 menu = menus[menu_count];
824 ++menu_count;
825 memset(menu, 0, sizeof(struct root_menu));
826 strncpy(menu->id, data, MAX_MENU_ID_SIZE-1);
829 if (get_token_str(menu->title, sizeof(menu->title)) < 0)
831 logf("%%menu_start title empty");
832 return 0;
834 logf("menu: %s", menu->title);
835 read_menu = true;
836 break;
838 case var_rootmenu:
839 /* Only set root menu once. */
840 if (root_menu >= 0)
841 break;
843 if (get_token_str(data, sizeof(data)) < 0)
845 logf("%%root_menu empty");
846 return 0;
849 for (i = 0; i < menu_count; i++)
851 if (!strcasecmp(menus[i]->id, data))
853 root_menu = i;
856 break;
859 return 0;
862 if (menu->itemcount >= TAGMENU_MAX_ITEMS)
864 logf("max itemcount reached");
865 return 0;
868 /* Allocate */
869 if (menu->items[menu->itemcount] == NULL)
871 menu->items[menu->itemcount] = buffer_alloc(sizeof(struct menu_entry));
872 memset(menu->items[menu->itemcount], 0, sizeof(struct menu_entry));
873 menu->items[menu->itemcount]->si = buffer_alloc(sizeof(struct search_instruction));
876 memset(menu->items[menu->itemcount]->si, 0, sizeof(struct search_instruction));
877 if (!parse_search(menu->items[menu->itemcount], buf))
878 return 0;
880 menu->itemcount++;
882 return 0;
885 static bool parse_menu(const char *filename)
887 int fd;
888 char buf[1024];
890 if (menu_count >= TAGMENU_MAX_MENUS)
892 logf("max menucount reached");
893 return false;
896 fd = open(filename, O_RDONLY);
897 if (fd < 0)
899 logf("Search instruction file not found.");
900 return false;
903 /* Now read file for real, parsing into si */
904 fast_readline(fd, buf, sizeof buf, NULL, parse_line);
905 close(fd);
907 return true;
910 void tagtree_init(void)
912 format_count = 0;
913 menu_count = 0;
914 menu = NULL;
915 root_menu = -1;
916 parse_menu(FILE_SEARCH_INSTRUCTIONS);
918 /* If no root menu is set, assume it's the first single menu
919 * we have. That shouldn't normally happen. */
920 if (root_menu < 0)
921 root_menu = 0;
923 uniqbuf = buffer_alloc(UNIQBUF_SIZE);
924 #if CONFIG_CODEC != SWCODEC
925 audio_set_track_buffer_event(tagtree_buffer_event);
926 #endif
927 audio_set_track_unbuffer_event(tagtree_unbuffer_event);
930 static bool show_search_progress(bool init, int count)
932 static int last_tick = 0;
934 if (init)
936 last_tick = current_tick;
937 return true;
940 if (current_tick - last_tick > HZ/4)
942 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG), count,
943 str(LANG_OFF_ABORT));
944 if (action_userabort(TIMEOUT_NOBLOCK))
945 return false;
946 last_tick = current_tick;
947 yield();
950 return true;
953 static int format_str(struct tagcache_search *tcs, struct display_format *fmt,
954 char *buf, int buf_size)
956 char fmtbuf[8];
957 bool read_format = false;
958 int fmtbuf_pos = 0;
959 int parpos = 0;
960 int buf_pos = 0;
961 int i;
963 memset(buf, 0, buf_size);
964 for (i = 0; fmt->formatstr[i] != '\0'; i++)
966 if (fmt->formatstr[i] == '%')
968 read_format = true;
969 fmtbuf_pos = 0;
970 if (parpos >= fmt->tag_count)
972 logf("too many format tags");
973 return -1;
977 if (read_format)
979 fmtbuf[fmtbuf_pos++] = fmt->formatstr[i];
980 if (fmtbuf_pos >= buf_size)
982 logf("format parse error");
983 return -2;
986 if (fmt->formatstr[i] == 's')
988 fmtbuf[fmtbuf_pos] = '\0';
989 read_format = false;
990 if (fmt->tags[parpos] == tcs->type)
992 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf, tcs->result);
994 else
996 /* Need to fetch the tag data. */
997 if (!tagcache_retrieve(tcs, tcs->idx_id, fmt->tags[parpos],
998 &buf[buf_pos], buf_size - buf_pos))
1000 logf("retrieve failed");
1001 return -3;
1004 buf_pos += strlen(&buf[buf_pos]);
1005 parpos++;
1007 else if (fmt->formatstr[i] == 'd')
1009 fmtbuf[fmtbuf_pos] = '\0';
1010 read_format = false;
1011 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf,
1012 tagcache_get_numeric(tcs, fmt->tags[parpos]));
1013 buf_pos += strlen(&buf[buf_pos]);
1014 parpos++;
1016 continue;
1019 buf[buf_pos++] = fmt->formatstr[i];
1021 if (buf_pos - 1 >= buf_size)
1023 logf("buffer overflow");
1024 return -4;
1028 buf[buf_pos++] = '\0';
1030 return 0;
1033 static int retrieve_entries(struct tree_context *c, struct tagcache_search *tcs,
1034 int offset, bool init)
1036 struct tagentry *dptr = (struct tagentry *)c->dircache;
1037 struct display_format *fmt;
1038 int i;
1039 int namebufused = 0;
1040 int total_count = 0;
1041 int special_entry_count = 0;
1042 int level = c->currextra;
1043 int tag;
1044 bool sort = false;
1045 int sort_limit;
1046 int strip;
1048 if (init
1049 #ifdef HAVE_TC_RAMCACHE
1050 && !tagcache_is_ramcache()
1051 #endif
1054 show_search_progress(true, 0);
1055 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG),
1056 0, csi->name);
1059 if (c->currtable == allsubentries)
1061 tag = tag_title;
1062 level--;
1064 else
1065 tag = csi->tagorder[level];
1067 if (!tagcache_search(tcs, tag))
1068 return -1;
1070 /* Prevent duplicate entries in the search list. */
1071 tagcache_search_set_uniqbuf(tcs, uniqbuf, UNIQBUF_SIZE);
1073 if (level || csi->clause_count[0] || tagcache_is_numeric_tag(tag))
1074 sort = true;
1076 for (i = 0; i < level; i++)
1078 if (tagcache_is_numeric_tag(csi->tagorder[i]))
1080 static struct tagcache_search_clause cc;
1082 memset(&cc, 0, sizeof(struct tagcache_search_clause));
1083 cc.tag = csi->tagorder[i];
1084 cc.type = clause_is;
1085 cc.numeric = true;
1086 cc.numeric_data = csi->result_seek[i];
1087 tagcache_search_add_clause(tcs, &cc);
1089 else
1091 tagcache_search_add_filter(tcs, csi->tagorder[i],
1092 csi->result_seek[i]);
1096 for (i = 0; i <= level; i++)
1098 int j;
1100 for (j = 0; j < csi->clause_count[i]; j++)
1101 tagcache_search_add_clause(tcs, csi->clause[i][j]);
1104 current_offset = offset;
1105 current_entry_count = 0;
1106 c->dirfull = false;
1108 fmt = NULL;
1109 for (i = 0; i < format_count; i++)
1111 if (formats[i]->group_id == csi->format_id[level])
1112 fmt = formats[i];
1115 if (fmt)
1117 sort_inverse = fmt->sort_inverse;
1118 sort_limit = fmt->limit;
1119 strip = fmt->strip;
1121 /* Check if sorting is forced. */
1122 if (fmt->sort)
1123 sort = true;
1125 else
1127 sort_inverse = false;
1128 sort_limit = 0;
1129 strip = 0;
1132 if (tag != tag_title && tag != tag_filename)
1134 if (offset == 0)
1136 dptr->newtable = allsubentries;
1137 dptr->name = str(LANG_TAGNAVI_ALL_TRACKS);
1138 dptr++;
1139 current_entry_count++;
1141 special_entry_count++;
1144 total_count += special_entry_count;
1146 while (tagcache_get_next(tcs))
1148 if (total_count++ < offset)
1149 continue;
1151 dptr->newtable = navibrowse;
1152 if (tag == tag_title || tag == tag_filename)
1154 dptr->newtable = playtrack;
1155 dptr->extraseek = tcs->idx_id;
1157 else
1158 dptr->extraseek = tcs->result_seek;
1160 fmt = NULL;
1161 /* Check the format */
1162 for (i = 0; i < format_count; i++)
1164 if (formats[i]->group_id != csi->format_id[level])
1165 continue;
1167 if (tagcache_check_clauses(tcs, formats[i]->clause,
1168 formats[i]->clause_count))
1170 fmt = formats[i];
1171 break;
1175 if (!tcs->ramresult || fmt)
1177 char buf[MAX_PATH];
1179 if (fmt)
1181 if (format_str(tcs, fmt, buf, sizeof buf) < 0)
1183 logf("format_str() failed");
1184 return 0;
1188 dptr->name = &c->name_buffer[namebufused];
1189 if (fmt)
1190 namebufused += strlen(buf)+1;
1191 else
1192 namebufused += tcs->result_len;
1194 if (namebufused >= c->name_buffer_size)
1196 logf("chunk mode #2: %d", current_entry_count);
1197 c->dirfull = true;
1198 sort = false;
1199 break ;
1201 if (fmt)
1202 strcpy(dptr->name, buf);
1203 else
1204 strcpy(dptr->name, tcs->result);
1206 else
1207 dptr->name = tcs->result;
1209 dptr++;
1210 current_entry_count++;
1212 if (current_entry_count >= global_settings.max_files_in_dir)
1214 logf("chunk mode #3: %d", current_entry_count);
1215 c->dirfull = true;
1216 sort = false;
1217 break ;
1220 if (init && !tcs->ramsearch)
1222 if (!show_search_progress(false, i))
1224 tagcache_search_finish(tcs);
1225 return current_entry_count;
1230 if (sort)
1231 qsort(c->dircache + special_entry_count * c->dentry_size,
1232 current_entry_count - special_entry_count,
1233 c->dentry_size, compare);
1235 if (!init)
1237 tagcache_search_finish(tcs);
1238 return current_entry_count;
1241 while (tagcache_get_next(tcs))
1243 if (!tcs->ramsearch)
1245 if (!show_search_progress(false, total_count))
1246 break;
1248 total_count++;
1251 tagcache_search_finish(tcs);
1253 if (!sort && (sort_inverse || sort_limit))
1255 gui_syncsplash(HZ*4, ID2P(LANG_SHOWDIR_BUFFER_FULL), total_count);
1256 logf("Too small dir buffer");
1257 return 0;
1260 if (sort_limit)
1261 total_count = MIN(total_count, sort_limit);
1263 if (strip)
1265 dptr = c->dircache;
1266 for (i = 0; i < total_count; i++, dptr++)
1268 int len = strlen(dptr->name);
1270 if (len < strip)
1271 continue;
1273 dptr->name = &dptr->name[strip];
1277 return total_count;
1280 static int load_root(struct tree_context *c)
1282 struct tagentry *dptr = (struct tagentry *)c->dircache;
1283 int i;
1285 tc = c;
1286 c->currtable = root;
1287 if (c->dirlevel == 0)
1288 c->currextra = root_menu;
1290 menu = menus[c->currextra];
1291 if (menu == NULL)
1292 return 0;
1294 for (i = 0; i < menu->itemcount; i++)
1296 dptr->name = menu->items[i]->name;
1297 switch (menu->items[i]->type)
1299 case menu_next:
1300 dptr->newtable = navibrowse;
1301 dptr->extraseek = i;
1302 break;
1304 case menu_load:
1305 dptr->newtable = root;
1306 dptr->extraseek = menu->items[i]->link;
1307 break;
1310 dptr++;
1313 current_offset = 0;
1314 current_entry_count = i;
1316 return i;
1319 int tagtree_load(struct tree_context* c)
1321 int count;
1322 int table = c->currtable;
1324 c->dentry_size = sizeof(struct tagentry);
1325 c->dirsindir = 0;
1327 if (!table)
1329 c->dirfull = false;
1330 table = root;
1331 c->currtable = table;
1332 c->currextra = root_menu;
1335 switch (table)
1337 case root:
1338 count = load_root(c);
1339 break;
1341 case allsubentries:
1342 case navibrowse:
1343 logf("navibrowse...");
1344 cpu_boost(true);
1345 count = retrieve_entries(c, &tcs, 0, true);
1346 cpu_boost(false);
1347 break;
1349 default:
1350 logf("Unsupported table %d\n", table);
1351 return -1;
1354 if (count < 0)
1356 c->dirlevel = 0;
1357 count = load_root(c);
1358 gui_syncsplash(HZ, str(LANG_TAGCACHE_BUSY));
1361 /* The _total_ numer of entries available. */
1362 c->dirlength = c->filesindir = count;
1364 return count;
1367 int tagtree_enter(struct tree_context* c)
1369 int rc = 0;
1370 struct tagentry *dptr;
1371 struct mp3entry *id3;
1372 int newextra;
1373 int seek;
1374 int source;
1376 dptr = tagtree_get_entry(c, c->selected_item);
1378 c->dirfull = false;
1379 newextra = dptr->newtable;
1380 seek = dptr->extraseek;
1382 if (c->dirlevel >= MAX_DIR_LEVELS)
1383 return 0;
1385 c->selected_item_history[c->dirlevel]=c->selected_item;
1386 c->table_history[c->dirlevel] = c->currtable;
1387 c->extra_history[c->dirlevel] = c->currextra;
1388 c->pos_history[c->dirlevel] = c->firstpos;
1389 c->dirlevel++;
1391 switch (c->currtable) {
1392 case root:
1393 c->currextra = newextra;
1395 if (newextra == root)
1397 menu = menus[seek];
1398 c->currextra = seek;
1401 else if (newextra == navibrowse)
1403 int i, j;
1405 csi = menu->items[seek]->si;
1406 c->currextra = 0;
1408 strncpy(current_title[c->currextra], dptr->name,
1409 sizeof(current_title[0]) - 1);
1411 /* Read input as necessary. */
1412 for (i = 0; i < csi->tagorder_count; i++)
1414 for (j = 0; j < csi->clause_count[i]; j++)
1416 char* searchstring;
1417 source = csi->clause[i][j]->source;
1419 if (source == source_constant)
1420 continue;
1422 searchstring=csi->clause[i][j]->str;
1423 *searchstring = '\0';
1425 id3 = audio_current_track();
1427 if (source == source_current_path && id3)
1429 char *e;
1430 strncpy(searchstring, id3->path, SEARCHSTR_SIZE);
1431 e = strrchr(searchstring, '/');
1432 if (e)
1433 *e = '\0';
1435 else if (source > source_runtime && id3)
1438 int k = source-source_runtime;
1439 int offset = id3_to_search_mapping[k].id3_offset;
1440 char **src = (char**)((char*)id3 + offset);
1441 if (*src)
1443 strncpy(searchstring, *src, SEARCHSTR_SIZE);
1444 searchstring[SEARCHSTR_SIZE-1] = '\0';
1447 else
1449 rc = kbd_input(searchstring, SEARCHSTR_SIZE);
1450 if (rc == -1 || !searchstring[0])
1452 tagtree_exit(c);
1453 return 0;
1455 if (csi->clause[i][j]->numeric)
1456 csi->clause[i][j]->numeric_data = atoi(searchstring);
1463 c->currtable = newextra;
1465 break;
1467 case navibrowse:
1468 case allsubentries:
1469 if (newextra == playtrack)
1471 c->dirlevel--;
1472 /* about to create a new current playlist...
1473 allow user to cancel the operation */
1474 if (global_settings.warnon_erase_dynplaylist &&
1475 !global_settings.party_mode &&
1476 playlist_modified(NULL))
1478 char *lines[]={ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
1479 struct text_message message={lines, 1};
1481 if (gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
1482 break;
1485 if (tagtree_play_folder(c) >= 0)
1486 rc = 2;
1487 break;
1490 c->currtable = newextra;
1491 csi->result_seek[c->currextra] = seek;
1492 if (c->currextra < csi->tagorder_count-1)
1493 c->currextra++;
1494 else
1495 c->dirlevel--;
1497 /* Update the statusbar title */
1498 strncpy(current_title[c->currextra], dptr->name,
1499 sizeof(current_title[0]) - 1);
1500 break;
1502 default:
1503 c->dirlevel--;
1504 break;
1507 c->selected_item=0;
1508 gui_synclist_select_item(&tree_lists, c->selected_item);
1510 return rc;
1513 void tagtree_exit(struct tree_context* c)
1515 c->dirfull = false;
1516 if (c->dirlevel > 0)
1517 c->dirlevel--;
1518 c->selected_item=c->selected_item_history[c->dirlevel];
1519 gui_synclist_select_item(&tree_lists, c->selected_item);
1520 c->currtable = c->table_history[c->dirlevel];
1521 c->currextra = c->extra_history[c->dirlevel];
1522 c->firstpos = c->pos_history[c->dirlevel];
1525 int tagtree_get_filename(struct tree_context* c, char *buf, int buflen)
1527 struct tagentry *entry;
1529 entry = tagtree_get_entry(c, c->selected_item);
1531 if (!tagcache_search(&tcs, tag_filename))
1532 return -1;
1534 if (!tagcache_retrieve(&tcs, entry->extraseek, tcs.type, buf, buflen))
1536 tagcache_search_finish(&tcs);
1537 return -2;
1540 tagcache_search_finish(&tcs);
1542 return 0;
1545 static bool insert_all_playlist(struct tree_context *c, int position, bool queue)
1547 int i;
1548 char buf[MAX_PATH];
1549 int from, to, direction;
1550 int files_left = c->filesindir;
1552 cpu_boost(true);
1553 if (!tagcache_search(&tcs, tag_filename))
1555 gui_syncsplash(HZ, ID2P(LANG_TAGCACHE_BUSY));
1556 cpu_boost(false);
1557 return false;
1560 if (position == PLAYLIST_REPLACE)
1562 if (remove_all_tracks(NULL) == 0)
1563 position = PLAYLIST_INSERT_LAST;
1564 else return -1; }
1566 if (position == PLAYLIST_INSERT_FIRST)
1568 from = c->filesindir - 1;
1569 to = -1;
1570 direction = -1;
1572 else
1574 from = 0;
1575 to = c->filesindir;
1576 direction = 1;
1579 for (i = from; i != to; i += direction)
1581 /* Count back to zero */
1582 if (!show_search_progress(false, files_left--))
1583 break;
1585 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, i)->extraseek,
1586 tcs.type, buf, sizeof buf))
1588 continue;
1591 if (playlist_insert_track(NULL, buf, position, queue, false) < 0)
1593 logf("playlist_insert_track failed");
1594 break;
1596 yield();
1598 playlist_sync(NULL);
1599 tagcache_search_finish(&tcs);
1600 cpu_boost(false);
1602 return true;
1605 bool tagtree_insert_selection_playlist(int position, bool queue)
1607 struct tagentry *dptr;
1608 char buf[MAX_PATH];
1609 int dirlevel = tc->dirlevel;
1611 /* We need to set the table to allsubentries. */
1612 show_search_progress(true, 0);
1614 dptr = tagtree_get_entry(tc, tc->selected_item);
1616 /* Insert a single track? */
1617 if (dptr->newtable == playtrack)
1619 if (tagtree_get_filename(tc, buf, sizeof buf) < 0)
1621 logf("tagtree_get_filename failed");
1622 return false;
1624 playlist_insert_track(NULL, buf, position, queue, true);
1626 return true;
1629 if (dptr->newtable == navibrowse)
1631 tagtree_enter(tc);
1632 tagtree_load(tc);
1633 dptr = tagtree_get_entry(tc, tc->selected_item);
1635 else if (dptr->newtable != allsubentries)
1637 logf("unsupported table: %d", dptr->newtable);
1638 return false;
1641 /* Now the current table should be allsubentries. */
1642 if (dptr->newtable != playtrack)
1644 tagtree_enter(tc);
1645 tagtree_load(tc);
1646 dptr = tagtree_get_entry(tc, tc->selected_item);
1648 /* And now the newtable should be playtrack. */
1649 if (dptr->newtable != playtrack)
1651 logf("newtable: %d !!", dptr->newtable);
1652 tc->dirlevel = dirlevel;
1653 return false;
1657 if (tc->filesindir <= 0)
1658 gui_syncsplash(HZ, ID2P(LANG_END_PLAYLIST));
1659 else
1661 logf("insert_all_playlist");
1662 if (!insert_all_playlist(tc, position, queue))
1663 gui_syncsplash(HZ*2, ID2P(LANG_FAILED));
1666 /* Finally return the dirlevel to its original value. */
1667 while (tc->dirlevel > dirlevel)
1668 tagtree_exit(tc);
1669 tagtree_load(tc);
1671 return true;
1674 static int tagtree_play_folder(struct tree_context* c)
1676 if (playlist_create(NULL, NULL) < 0)
1678 logf("Failed creating playlist\n");
1679 return -1;
1682 if (!insert_all_playlist(c, PLAYLIST_INSERT_LAST, false))
1683 return -2;
1685 if (global_settings.playlist_shuffle)
1686 c->selected_item = playlist_shuffle(current_tick, c->selected_item);
1687 if (!global_settings.play_selected)
1688 c->selected_item = 0;
1689 gui_synclist_select_item(&tree_lists, c->selected_item);
1691 playlist_start(c->selected_item,0);
1693 return 0;
1696 struct tagentry* tagtree_get_entry(struct tree_context *c, int id)
1698 struct tagentry *entry = (struct tagentry *)c->dircache;
1699 int realid = id - current_offset;
1701 /* Load the next chunk if necessary. */
1702 if (realid >= current_entry_count || realid < 0)
1704 cpu_boost(true);
1705 if (retrieve_entries(c, &tcs2, MAX(0, id - (current_entry_count / 2)),
1706 false) < 0)
1708 logf("retrieve failed");
1709 cpu_boost(false);
1710 return NULL;
1712 realid = id - current_offset;
1713 cpu_boost(false);
1716 return &entry[realid];
1719 char *tagtree_get_title(struct tree_context* c)
1721 switch (c->currtable)
1723 case root:
1724 return menu->title;
1726 case navibrowse:
1727 case allsubentries:
1728 return current_title[c->currextra];
1731 return "?";
1734 int tagtree_get_attr(struct tree_context* c)
1736 int attr = -1;
1737 switch (c->currtable)
1739 case navibrowse:
1740 if (csi->tagorder[c->currextra] == tag_title)
1741 attr = FILE_ATTR_AUDIO;
1742 else
1743 attr = ATTR_DIRECTORY;
1744 break;
1746 case allsubentries:
1747 attr = FILE_ATTR_AUDIO;
1748 break;
1750 default:
1751 attr = ATTR_DIRECTORY;
1752 break;
1755 return attr;
1758 int tagtree_get_icon(struct tree_context* c)
1760 int icon = Icon_Folder;
1762 if (tagtree_get_attr(c) == FILE_ATTR_AUDIO)
1763 icon = Icon_Audio;
1765 return icon;