1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
23 * Basic structure on this file was copied from dbtree.c and modified to
24 * support the tag cache interface.
48 #include "filetypes.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
{
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
) },
83 /* Capacity 10 000 entries (for example 10k different artists) */
84 #define UNIQBUF_SIZE (64*1024)
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"
97 * formatstr = "%-3d. %s"
98 * tags[0] = tag_autoscore
103 * sort_inverse = true
105 struct display_format
{
107 struct tagcache_search_clause
*clause
[TAGCACHE_MAX_CLAUSES
];
119 static struct display_format
*formats
[TAGMENU_MAX_FMTS
];
120 static int format_count
;
122 struct search_instruction
{
124 int tagorder
[MAX_TAGS
];
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
];
135 struct search_instruction
*si
;
141 char id
[MAX_MENU_ID_SIZE
];
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')
168 if (*strp
== '\0' || *(++strp
) == '\0')
172 while (*strp
!= '"' && *strp
!= '\0' && --size
> 0)
173 *(buf
++) = *(strp
++);
184 #define MATCH(tag,str1,str2,settag) \
185 if (!strcasecmp(str1, str2)) { \
190 static int get_tag(int *tag
)
195 /* Find the start. */
196 while ((*strp
== ' ' || *strp
== '>') && *strp
!= '\0')
199 if (*strp
== '\0' || *strp
== '?')
202 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
204 if (*strp
== '\0' || *strp
== ' ')
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
);
253 static int get_clause(int *condition
)
258 /* Find the start. */
259 while (*strp
== ' ' && *strp
!= '\0')
265 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
267 if (*strp
== '\0' || *strp
== ' ')
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
);
292 static bool read_clause(struct tagcache_search_clause
*clause
)
294 char buf
[SEARCHSTR_SIZE
];
297 if (get_tag(&clause
->tag
) <= 0)
300 if (get_clause(&clause
->type
) <= 0)
303 if (get_token_str(buf
, sizeof buf
) < 0)
306 for (i
=0; i
<ARRAYLEN(id3_to_search_mapping
); i
++)
308 if (!strcasecmp(buf
, id3_to_search_mapping
[i
].string
))
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
);
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
);
330 clause
->numeric
= false;
332 logf("got clause: %d/%d [%s]", clause
->tag
, clause
->type
, clause
->str
);
337 static bool read_variable(char *buf
, int size
)
341 if (!get_clause(&condition
))
344 if (condition
!= clause_is
)
347 if (get_token_str(buf
, size
) < 0)
353 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
354 static int get_format_str(struct display_format
*fmt
)
360 memset(fmt
, 0, sizeof(struct display_format
));
362 if (get_token_str(fmt
->name
, sizeof fmt
->name
) < 0)
365 /* Determine the group id */
367 for (i
= 0; i
< format_count
; i
++)
369 if (!strcasecmp(formats
[i
]->name
, fmt
->name
))
371 fmt
->group_id
= formats
[i
]->group_id
;
375 if (formats
[i
]->group_id
> fmt
->group_id
)
376 fmt
->group_id
= formats
[i
]->group_id
;
379 if (i
== format_count
)
382 logf("format: (%d) %s", fmt
->group_id
, fmt
->name
);
384 if (get_token_str(buf
, sizeof buf
) < 0)
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
]);
399 switch (fmt
->tags
[fmt
->tag_count
]) {
401 if (!read_variable(buf
, sizeof buf
))
403 if (!strcasecmp("inverse", buf
))
404 fmt
->sort_inverse
= true;
408 if (!read_variable(buf
, sizeof buf
))
410 fmt
->limit
= atoi(buf
);
414 if (!read_variable(buf
, sizeof buf
))
416 fmt
->strip
= atoi(buf
);
427 static int add_format(const char *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!");
441 while (*strp
!= '\0' && *strp
!= '?')
446 int clause_count
= 0;
451 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
453 logf("too many clauses");
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
]))
466 formats
[format_count
]->clause_count
= clause_count
;
474 static int get_condition(struct search_instruction
*inst
)
485 if (get_token_str(buf
, sizeof buf
) < 0)
488 for (i
= 0; i
< format_count
; i
++)
490 if (!strcasecmp(formats
[i
]->name
, buf
))
494 if (i
== format_count
)
496 logf("format not found: %s", buf
);
500 inst
->format_id
[inst
->tagorder_count
] = formats
[i
]->group_id
;
513 clause_count
= inst
->clause_count
[inst
->tagorder_count
];
514 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
516 logf("Too many clauses");
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
]))
526 inst
->clause_count
[inst
->tagorder_count
]++;
532 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
533 * : album ? year >= "2000" : songs
539 static bool parse_search(struct menu_entry
*entry
, const char *str
)
543 struct search_instruction
*inst
= entry
->si
;
546 struct root_menu
*new_menu
;
550 /* Parse entry name */
551 if (get_token_str(entry
->name
, sizeof entry
->name
) < 0)
553 logf("No name found.");
557 /* Parse entry type */
558 if (get_tag(&entry
->type
) <= 0)
561 if (entry
->type
== menu_load
)
563 if (get_token_str(buf
, sizeof buf
) < 0)
566 /* Find the matching root menu or "create" it */
567 for (i
= 0; i
< menu_count
; i
++)
569 if (!strcasecmp(menus
[i
]->id
, buf
))
576 if (menu_count
>= TAGMENU_MAX_MENUS
)
578 logf("max menucount reached");
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
;
593 if (entry
->type
!= menu_next
)
596 while (inst
->tagorder_count
< MAX_TAGS
)
598 ret
= get_tag(&inst
->tagorder
[inst
->tagorder_count
]);
601 logf("Parse error #1");
609 logf("tag: %d", inst
->tagorder
[inst
->tagorder_count
]);
611 while ( (ret
= get_condition(inst
)) > 0 ) ;
615 inst
->tagorder_count
++;
617 if (get_tag(&type
) <= 0 || type
!= menu_next
)
624 static int compare(const void *p1
, const void *p2
)
626 struct tagentry
*e1
= (struct tagentry
*)p1
;
627 struct tagentry
*e2
= (struct tagentry
*)p2
;
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
)
641 logf("be:%s", id3
->path
);
643 if (!tagcache_find_index(&tcs
, id3
->path
))
645 logf("tc stat: not found: %s", id3
->path
);
649 id3
->playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
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
)
669 /* Do not gather data unless proper setting has been enabled. */
670 if (!global_settings
.runtimedb
)
672 logf("runtimedb gathering not enabled");
676 tagcache_idx
=id3
->tagcache_idx
;
679 logf("No tagcache index pointer found");
684 /* Don't process unplayed tracks. */
685 if (id3
->elapsed
== 0)
687 logf("not logging unplayed track");
691 playcount
= id3
->playcount
+ 1;
692 lastplayed
= tagcache_increase_serial();
695 logf("incorrect tc serial:%ld", lastplayed
);
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 gui_syncsplash(0, str(LANG_CREATING
));
715 if (!tagcache_create_changelog(&tcs
))
717 gui_syncsplash(HZ
*2, ID2P(LANG_FAILED
));
723 bool tagtree_import(void)
725 gui_syncsplash(0, ID2P(LANG_WAIT
));
726 if (!tagcache_import_changelog())
728 gui_syncsplash(HZ
*2, ID2P(LANG_FAILED
));
734 static bool parse_menu(const char *filename
);
736 static int parse_line(int n
, const char *buf
, void *parameters
)
740 static bool read_menu
;
745 logf("parse:%d/%s", n
, buf
);
747 /* First line, do initialisation. */
750 if (strcasecmp(TAGNAVI_VERSION
, buf
))
752 logf("Version mismatch");
775 if (get_tag(&variable
) <= 0)
781 if (add_format(strp
) < 0)
783 logf("Format add fail: %s", data
);
788 if (get_token_str(data
, sizeof(data
)) < 0)
790 logf("%%include empty");
794 if (!parse_menu(data
))
796 logf("Load menu fail: %s", data
);
801 if (menu_count
>= TAGMENU_MAX_MENUS
)
803 logf("max menucount reached");
807 if (get_token_str(data
, sizeof data
) < 0)
809 logf("%%menu_start id empty");
814 for (i
= 0; i
< menu_count
; i
++)
816 if (!strcasecmp(menus
[i
]->id
, data
))
824 menus
[menu_count
] = buffer_alloc(sizeof(struct root_menu
));
825 menu
= menus
[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");
836 logf("menu: %s", menu
->title
);
841 /* Only set root menu once. */
845 if (get_token_str(data
, sizeof(data
)) < 0)
847 logf("%%root_menu empty");
851 for (i
= 0; i
< menu_count
; i
++)
853 if (!strcasecmp(menus
[i
]->id
, data
))
864 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
866 logf("max itemcount reached");
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
))
887 static bool parse_menu(const char *filename
)
892 if (menu_count
>= TAGMENU_MAX_MENUS
)
894 logf("max menucount reached");
898 fd
= open(filename
, O_RDONLY
);
901 logf("Search instruction file not found.");
905 /* Now read file for real, parsing into si */
906 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
912 void tagtree_init(void)
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. */
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 */
938 last_tick
= current_tick
+ HZ
/2;
942 /* Update progress every 1/10 of a second */
943 if (current_tick
- last_tick
> HZ
/10)
945 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG
), count
,
946 str(LANG_OFF_ABORT
));
947 if (action_userabort(TIMEOUT_NOBLOCK
))
949 last_tick
= current_tick
;
956 static int format_str(struct tagcache_search
*tcs
, struct display_format
*fmt
,
957 char *buf
, int buf_size
)
960 bool read_format
= false;
966 memset(buf
, 0, buf_size
);
967 for (i
= 0; fmt
->formatstr
[i
] != '\0'; i
++)
969 if (fmt
->formatstr
[i
] == '%')
973 if (parpos
>= fmt
->tag_count
)
975 logf("too many format tags");
982 fmtbuf
[fmtbuf_pos
++] = fmt
->formatstr
[i
];
983 if (fmtbuf_pos
>= buf_size
)
985 logf("format parse error");
989 if (fmt
->formatstr
[i
] == 's')
991 fmtbuf
[fmtbuf_pos
] = '\0';
993 if (fmt
->tags
[parpos
] == tcs
->type
)
995 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
, tcs
->result
);
999 /* Need to fetch the tag data. */
1000 if (!tagcache_retrieve(tcs
, tcs
->idx_id
, fmt
->tags
[parpos
],
1001 &buf
[buf_pos
], buf_size
- buf_pos
))
1003 logf("retrieve failed");
1007 buf_pos
+= strlen(&buf
[buf_pos
]);
1010 else if (fmt
->formatstr
[i
] == 'd')
1012 fmtbuf
[fmtbuf_pos
] = '\0';
1013 read_format
= false;
1014 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
,
1015 tagcache_get_numeric(tcs
, fmt
->tags
[parpos
]));
1016 buf_pos
+= strlen(&buf
[buf_pos
]);
1022 buf
[buf_pos
++] = fmt
->formatstr
[i
];
1024 if (buf_pos
- 1 >= buf_size
)
1026 logf("buffer overflow");
1031 buf
[buf_pos
++] = '\0';
1036 static int retrieve_entries(struct tree_context
*c
, struct tagcache_search
*tcs
,
1037 int offset
, bool init
)
1039 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1040 struct display_format
*fmt
;
1042 int namebufused
= 0;
1043 int total_count
= 0;
1044 int special_entry_count
= 0;
1045 int level
= c
->currextra
;
1052 #ifdef HAVE_TC_RAMCACHE
1053 && !tagcache_is_ramcache()
1057 /* Show search progress straight away if the disk needs to spin up,
1058 otherwise show it after the normal 1/2 second delay */
1059 show_search_progress(
1060 #if !defined(HAVE_FLASH_STORAGE)
1061 ata_disk_is_active()
1068 if (c
->currtable
== allsubentries
)
1074 tag
= csi
->tagorder
[level
];
1076 if (!tagcache_search(tcs
, tag
))
1079 /* Prevent duplicate entries in the search list. */
1080 tagcache_search_set_uniqbuf(tcs
, uniqbuf
, UNIQBUF_SIZE
);
1082 if (level
|| csi
->clause_count
[0] || tagcache_is_numeric_tag(tag
))
1085 for (i
= 0; i
< level
; i
++)
1087 if (tagcache_is_numeric_tag(csi
->tagorder
[i
]))
1089 static struct tagcache_search_clause cc
;
1091 memset(&cc
, 0, sizeof(struct tagcache_search_clause
));
1092 cc
.tag
= csi
->tagorder
[i
];
1093 cc
.type
= clause_is
;
1095 cc
.numeric_data
= csi
->result_seek
[i
];
1096 tagcache_search_add_clause(tcs
, &cc
);
1100 tagcache_search_add_filter(tcs
, csi
->tagorder
[i
],
1101 csi
->result_seek
[i
]);
1105 for (i
= 0; i
<= level
; i
++)
1109 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1110 tagcache_search_add_clause(tcs
, csi
->clause
[i
][j
]);
1113 current_offset
= offset
;
1114 current_entry_count
= 0;
1118 for (i
= 0; i
< format_count
; i
++)
1120 if (formats
[i
]->group_id
== csi
->format_id
[level
])
1126 sort_inverse
= fmt
->sort_inverse
;
1127 sort_limit
= fmt
->limit
;
1133 sort_inverse
= false;
1138 if (tag
!= tag_title
&& tag
!= tag_filename
)
1142 dptr
->newtable
= allsubentries
;
1143 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1145 current_entry_count
++;
1149 dptr
->newtable
= navibrowse
;
1150 dptr
->name
= str(LANG_TAGNAVI_RANDOM
);
1151 dptr
->extraseek
= -1;
1153 current_entry_count
++;
1155 special_entry_count
+=2;
1158 total_count
+= special_entry_count
;
1160 while (tagcache_get_next(tcs
))
1162 if (total_count
++ < offset
)
1165 dptr
->newtable
= navibrowse
;
1166 if (tag
== tag_title
|| tag
== tag_filename
)
1168 dptr
->newtable
= playtrack
;
1169 dptr
->extraseek
= tcs
->idx_id
;
1172 dptr
->extraseek
= tcs
->result_seek
;
1175 /* Check the format */
1176 for (i
= 0; i
< format_count
; i
++)
1178 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1181 if (tagcache_check_clauses(tcs
, formats
[i
]->clause
,
1182 formats
[i
]->clause_count
))
1189 if (!tcs
->ramresult
|| fmt
)
1195 if (format_str(tcs
, fmt
, buf
, sizeof buf
) < 0)
1197 logf("format_str() failed");
1202 dptr
->name
= &c
->name_buffer
[namebufused
];
1204 namebufused
+= strlen(buf
)+1;
1206 namebufused
+= tcs
->result_len
;
1208 if (namebufused
>= c
->name_buffer_size
)
1210 logf("chunk mode #2: %d", current_entry_count
);
1216 strcpy(dptr
->name
, buf
);
1218 strcpy(dptr
->name
, tcs
->result
);
1221 dptr
->name
= tcs
->result
;
1224 current_entry_count
++;
1226 if (current_entry_count
>= global_settings
.max_files_in_dir
)
1228 logf("chunk mode #3: %d", current_entry_count
);
1234 if (init
&& !tcs
->ramsearch
)
1236 if (!show_search_progress(false, total_count
))
1238 tagcache_search_finish(tcs
);
1239 return current_entry_count
;
1245 qsort(c
->dircache
+ special_entry_count
* c
->dentry_size
,
1246 current_entry_count
- special_entry_count
,
1247 c
->dentry_size
, compare
);
1251 tagcache_search_finish(tcs
);
1252 return current_entry_count
;
1255 while (tagcache_get_next(tcs
))
1257 if (!tcs
->ramsearch
)
1259 if (!show_search_progress(false, total_count
))
1265 tagcache_search_finish(tcs
);
1267 if (!sort
&& (sort_inverse
|| sort_limit
))
1269 gui_syncsplash(HZ
*4, ID2P(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1270 logf("Too small dir buffer");
1275 total_count
= MIN(total_count
, sort_limit
);
1280 for (i
= 0; i
< total_count
; i
++, dptr
++)
1282 int len
= strlen(dptr
->name
);
1287 dptr
->name
= &dptr
->name
[strip
];
1294 static int load_root(struct tree_context
*c
)
1296 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1300 c
->currtable
= root
;
1301 if (c
->dirlevel
== 0)
1302 c
->currextra
= root_menu
;
1304 menu
= menus
[c
->currextra
];
1308 for (i
= 0; i
< menu
->itemcount
; i
++)
1310 dptr
->name
= menu
->items
[i
]->name
;
1311 switch (menu
->items
[i
]->type
)
1314 dptr
->newtable
= navibrowse
;
1315 dptr
->extraseek
= i
;
1319 dptr
->newtable
= root
;
1320 dptr
->extraseek
= menu
->items
[i
]->link
;
1328 current_entry_count
= i
;
1333 int tagtree_load(struct tree_context
* c
)
1336 int table
= c
->currtable
;
1338 c
->dentry_size
= sizeof(struct tagentry
);
1345 c
->currtable
= table
;
1346 c
->currextra
= root_menu
;
1352 count
= load_root(c
);
1357 logf("navibrowse...");
1359 count
= retrieve_entries(c
, &tcs
, 0, true);
1364 logf("Unsupported table %d\n", table
);
1371 count
= load_root(c
);
1372 gui_syncsplash(HZ
, str(LANG_TAGCACHE_BUSY
));
1375 /* The _total_ numer of entries available. */
1376 c
->dirlength
= c
->filesindir
= count
;
1381 int tagtree_enter(struct tree_context
* c
)
1384 struct tagentry
*dptr
;
1385 struct mp3entry
*id3
;
1390 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1393 seek
= dptr
->extraseek
;
1396 if(c
->filesindir
<=2)
1398 srand(current_tick
);
1399 dptr
= (tagtree_get_entry(c
, 2+(rand() % (c
->filesindir
-2))));
1400 seek
= dptr
->extraseek
;
1402 newextra
= dptr
->newtable
;
1404 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1407 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1408 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1409 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1410 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1413 switch (c
->currtable
) {
1415 c
->currextra
= newextra
;
1417 if (newextra
== root
)
1420 c
->currextra
= seek
;
1423 else if (newextra
== navibrowse
)
1427 csi
= menu
->items
[seek
]->si
;
1430 strncpy(current_title
[c
->currextra
], dptr
->name
,
1431 sizeof(current_title
[0]) - 1);
1433 /* Read input as necessary. */
1434 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1436 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1439 source
= csi
->clause
[i
][j
]->source
;
1441 if (source
== source_constant
)
1444 searchstring
=csi
->clause
[i
][j
]->str
;
1445 *searchstring
= '\0';
1447 id3
= audio_current_track();
1449 if (source
== source_current_path
&& id3
)
1452 strncpy(searchstring
, id3
->path
, SEARCHSTR_SIZE
);
1453 e
= strrchr(searchstring
, '/');
1457 else if (source
> source_runtime
&& id3
)
1460 int k
= source
-source_runtime
;
1461 int offset
= id3_to_search_mapping
[k
].id3_offset
;
1462 char **src
= (char**)((char*)id3
+ offset
);
1465 strncpy(searchstring
, *src
, SEARCHSTR_SIZE
);
1466 searchstring
[SEARCHSTR_SIZE
-1] = '\0';
1471 rc
= kbd_input(searchstring
, SEARCHSTR_SIZE
);
1472 if (rc
== -1 || !searchstring
[0])
1477 if (csi
->clause
[i
][j
]->numeric
)
1478 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1485 c
->currtable
= newextra
;
1491 if (newextra
== playtrack
)
1494 /* about to create a new current playlist...
1495 allow user to cancel the operation */
1496 if (!warn_on_pl_erase())
1499 if (tagtree_play_folder(c
) >= 0)
1504 c
->currtable
= newextra
;
1505 csi
->result_seek
[c
->currextra
] = seek
;
1506 if (c
->currextra
< csi
->tagorder_count
-1)
1511 /* Update the statusbar title */
1512 strncpy(current_title
[c
->currextra
], dptr
->name
,
1513 sizeof(current_title
[0]) - 1);
1522 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1527 void tagtree_exit(struct tree_context
* c
)
1530 if (c
->dirlevel
> 0)
1532 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1533 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1534 c
->currtable
= c
->table_history
[c
->dirlevel
];
1535 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1536 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1539 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1541 struct tagentry
*entry
;
1543 entry
= tagtree_get_entry(c
, c
->selected_item
);
1545 if (!tagcache_search(&tcs
, tag_filename
))
1548 if (!tagcache_retrieve(&tcs
, entry
->extraseek
, tcs
.type
, buf
, buflen
))
1550 tagcache_search_finish(&tcs
);
1554 tagcache_search_finish(&tcs
);
1559 static bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1563 int from
, to
, direction
;
1564 int files_left
= c
->filesindir
;
1567 if (!tagcache_search(&tcs
, tag_filename
))
1569 gui_syncsplash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
1574 if (position
== PLAYLIST_REPLACE
)
1576 if (playlist_remove_all_tracks(NULL
) == 0)
1577 position
= PLAYLIST_INSERT_LAST
;
1585 if (position
== PLAYLIST_INSERT_FIRST
)
1587 from
= c
->filesindir
- 1;
1598 for (i
= from
; i
!= to
; i
+= direction
)
1600 /* Count back to zero */
1601 if (!show_search_progress(false, files_left
--))
1604 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1605 tcs
.type
, buf
, sizeof buf
))
1610 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1612 logf("playlist_insert_track failed");
1617 playlist_sync(NULL
);
1618 tagcache_search_finish(&tcs
);
1624 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1626 struct tagentry
*dptr
;
1628 int dirlevel
= tc
->dirlevel
;
1630 /* We need to set the table to allsubentries. */
1631 show_search_progress(true, 0);
1633 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1635 /* Insert a single track? */
1636 if (dptr
->newtable
== playtrack
)
1638 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1640 logf("tagtree_get_filename failed");
1643 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1648 if (dptr
->newtable
== navibrowse
)
1652 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1654 else if (dptr
->newtable
!= allsubentries
)
1656 logf("unsupported table: %d", dptr
->newtable
);
1660 /* Now the current table should be allsubentries. */
1661 if (dptr
->newtable
!= playtrack
)
1665 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1667 /* And now the newtable should be playtrack. */
1668 if (dptr
->newtable
!= playtrack
)
1670 logf("newtable: %d !!", dptr
->newtable
);
1671 tc
->dirlevel
= dirlevel
;
1676 if (tc
->filesindir
<= 0)
1677 gui_syncsplash(HZ
, ID2P(LANG_END_PLAYLIST
));
1680 logf("insert_all_playlist");
1681 if (!insert_all_playlist(tc
, position
, queue
))
1682 gui_syncsplash(HZ
*2, ID2P(LANG_FAILED
));
1685 /* Finally return the dirlevel to its original value. */
1686 while (tc
->dirlevel
> dirlevel
)
1693 static int tagtree_play_folder(struct tree_context
* c
)
1695 if (playlist_create(NULL
, NULL
) < 0)
1697 logf("Failed creating playlist\n");
1701 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1704 if (global_settings
.playlist_shuffle
)
1705 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
1706 if (!global_settings
.play_selected
)
1707 c
->selected_item
= 0;
1708 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1710 playlist_start(c
->selected_item
,0);
1715 struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
1717 struct tagentry
*entry
= (struct tagentry
*)c
->dircache
;
1718 int realid
= id
- current_offset
;
1720 /* Load the next chunk if necessary. */
1721 if (realid
>= current_entry_count
|| realid
< 0)
1724 if (retrieve_entries(c
, &tcs2
, MAX(0, id
- (current_entry_count
/ 2)),
1727 logf("retrieve failed");
1731 realid
= id
- current_offset
;
1735 return &entry
[realid
];
1738 char *tagtree_get_title(struct tree_context
* c
)
1740 switch (c
->currtable
)
1747 return current_title
[c
->currextra
];
1753 int tagtree_get_attr(struct tree_context
* c
)
1756 switch (c
->currtable
)
1759 if (csi
->tagorder
[c
->currextra
] == tag_title
)
1760 attr
= FILE_ATTR_AUDIO
;
1762 attr
= ATTR_DIRECTORY
;
1766 attr
= FILE_ATTR_AUDIO
;
1770 attr
= ATTR_DIRECTORY
;
1777 int tagtree_get_icon(struct tree_context
* c
)
1779 int icon
= Icon_Folder
;
1781 if (tagtree_get_attr(c
) == FILE_ATTR_AUDIO
)