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.
27 /*#define LOGF_ENABLE*/
31 #include "string-extra.h"
50 #include "filetypes.h"
52 #include "appevents.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
69 static const struct id3_to_search_mapping
{
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
) },
94 /* Capacity 10 000 entries (for example 10k different artists) */
95 #define UNIQBUF_SIZE (64*1024)
99 #define MAX_MENU_ID_SIZE 32
101 static bool sort_inverse
;
104 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
107 * formatstr = "%-3d. %s"
108 * tags[0] = tag_autoscore
109 * tags[1] = tag_title
113 * sort_inverse = true
115 struct display_format
{
117 struct tagcache_search_clause
*clause
[TAGCACHE_MAX_CLAUSES
];
129 static struct display_format
*formats
[TAGMENU_MAX_FMTS
];
130 static int format_count
;
132 struct search_instruction
{
134 int tagorder
[MAX_TAGS
];
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
];
145 struct search_instruction
*si
;
151 char id
[MAX_MENU_ID_SIZE
];
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
;
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')
177 if (*strp
== '\0' || *(++strp
) == '\0')
181 while (*strp
!= '"' && *strp
!= '\0' && --size
> 0)
182 *(buf
++) = *(strp
++);
193 #define MATCH(tag,str1,str2,settag) \
194 if (!strcasecmp(str1, str2)) { \
199 static int get_tag(int *tag
)
204 /* Find the start. */
205 while ((*strp
== ' ' || *strp
== '>') && *strp
!= '\0')
208 if (*strp
== '\0' || *strp
== '?')
211 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
213 if (*strp
== '\0' || *strp
== ' ')
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
);
262 static int get_clause(int *condition
)
267 /* Find the start. */
268 while (*strp
== ' ' && *strp
!= '\0')
274 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
276 if (*strp
== '\0' || *strp
== ' ')
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
);
301 static bool read_clause(struct tagcache_search_clause
*clause
)
303 char buf
[SEARCHSTR_SIZE
];
306 if (get_tag(&clause
->tag
) <= 0)
309 if (get_clause(&clause
->type
) <= 0)
312 if (get_token_str(buf
, sizeof buf
) < 0)
315 for (i
=0; i
<ARRAYLEN(id3_to_search_mapping
); i
++)
317 if (!strcasecmp(buf
, id3_to_search_mapping
[i
].string
))
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
);
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
);
339 clause
->numeric
= false;
341 logf("got clause: %d/%d [%s]", clause
->tag
, clause
->type
, clause
->str
);
346 static bool read_variable(char *buf
, int size
)
350 if (!get_clause(&condition
))
353 if (condition
!= clause_is
)
356 if (get_token_str(buf
, size
) < 0)
362 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
363 static int get_format_str(struct display_format
*fmt
)
369 memset(fmt
, 0, sizeof(struct display_format
));
371 if (get_token_str(fmt
->name
, sizeof fmt
->name
) < 0)
374 /* Determine the group id */
376 for (i
= 0; i
< format_count
; i
++)
378 if (!strcasecmp(formats
[i
]->name
, fmt
->name
))
380 fmt
->group_id
= formats
[i
]->group_id
;
384 if (formats
[i
]->group_id
> fmt
->group_id
)
385 fmt
->group_id
= formats
[i
]->group_id
;
388 if (i
== format_count
)
391 logf("format: (%d) %s", fmt
->group_id
, fmt
->name
);
393 if (get_token_str(buf
, sizeof buf
) < 0)
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
]);
408 switch (fmt
->tags
[fmt
->tag_count
]) {
410 if (!read_variable(buf
, sizeof buf
))
412 if (!strcasecmp("inverse", buf
))
413 fmt
->sort_inverse
= true;
417 if (!read_variable(buf
, sizeof buf
))
419 fmt
->limit
= atoi(buf
);
423 if (!read_variable(buf
, sizeof buf
))
425 fmt
->strip
= atoi(buf
);
436 static int add_format(const char *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!");
450 while (*strp
!= '\0' && *strp
!= '?')
455 int clause_count
= 0;
460 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
462 logf("too many clauses");
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
]))
475 formats
[format_count
]->clause_count
= clause_count
;
483 static int get_condition(struct search_instruction
*inst
)
494 if (get_token_str(buf
, sizeof buf
) < 0)
497 for (i
= 0; i
< format_count
; i
++)
499 if (!strcasecmp(formats
[i
]->name
, buf
))
503 if (i
== format_count
)
505 logf("format not found: %s", buf
);
509 inst
->format_id
[inst
->tagorder_count
] = formats
[i
]->group_id
;
522 clause_count
= inst
->clause_count
[inst
->tagorder_count
];
523 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
525 logf("Too many clauses");
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
]))
535 inst
->clause_count
[inst
->tagorder_count
]++;
541 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
542 * : album ? year >= "2000" : songs
548 static bool parse_search(struct menu_entry
*entry
, const char *str
)
552 struct search_instruction
*inst
= entry
->si
;
555 struct menu_root
*new_menu
;
559 /* Parse entry name */
560 if (get_token_str(entry
->name
, sizeof entry
->name
) < 0)
562 logf("No name found.");
566 /* Parse entry type */
567 if (get_tag(&entry
->type
) <= 0)
570 if (entry
->type
== menu_load
)
572 if (get_token_str(buf
, sizeof buf
) < 0)
575 /* Find the matching root menu or "create" it */
576 for (i
= 0; i
< menu_count
; i
++)
578 if (!strcasecmp(menus
[i
]->id
, buf
))
585 if (menu_count
>= TAGMENU_MAX_MENUS
)
587 logf("max menucount reached");
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
;
602 if (entry
->type
!= menu_next
)
605 while (inst
->tagorder_count
< MAX_TAGS
)
607 ret
= get_tag(&inst
->tagorder
[inst
->tagorder_count
]);
610 logf("Parse error #1");
618 logf("tag: %d", inst
->tagorder
[inst
->tagorder_count
]);
620 while ( (ret
= get_condition(inst
)) > 0 ) ;
624 inst
->tagorder_count
++;
626 if (get_tag(&type
) <= 0 || type
!= menu_next
)
633 static int compare(const void *p1
, const void *p2
)
635 struct tagentry
*e1
= (struct tagentry
*)p1
;
636 struct tagentry
*e2
= (struct tagentry
*)p2
;
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
)
653 logf("be:%s", id3
->path
);
655 if (!tagcache_find_index(&tcs
, id3
->path
))
657 logf("tc stat: not found: %s", id3
->path
);
661 id3
->playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
663 id3
->rating
= tagcache_get_numeric(&tcs
, tag_rating
);
664 id3
->lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
665 id3
->score
= tagcache_get_numeric(&tcs
, tag_virt_autoscore
) / 10;
666 id3
->playtime
= tagcache_get_numeric(&tcs
, tag_playtime
);
668 /* Store our tagcache index pointer. */
669 id3
->tagcache_idx
= tcs
.idx_id
+1;
671 tagcache_search_finish(&tcs
);
674 static void tagtree_track_finish_event(void *data
)
680 struct mp3entry
*id3
= (struct mp3entry
*)data
;
682 /* Do not gather data unless proper setting has been enabled. */
683 if (!global_settings
.runtimedb
)
685 logf("runtimedb gathering not enabled");
689 tagcache_idx
=id3
->tagcache_idx
;
692 logf("No tagcache index pointer found");
697 /* Don't process unplayed tracks. */
698 if (id3
->elapsed
== 0)
700 logf("not logging unplayed track");
704 playcount
= id3
->playcount
+ 1;
705 lastplayed
= tagcache_increase_serial();
708 logf("incorrect tc serial:%ld", lastplayed
);
712 /* Ignore the last 15s (crossfade etc.) */
713 playtime
= id3
->playtime
+ MIN(id3
->length
, id3
->elapsed
+ 15 * 1000);
715 logf("ube:%s", id3
->path
);
716 logf("-> %ld/%ld", playcount
, playtime
);
717 logf("-> %ld/%ld/%ld", id3
->elapsed
, id3
->length
, MIN(id3
->length
, id3
->elapsed
+ 15 * 1000));
719 /* Queue the updates to the tagcache system. */
720 tagcache_update_numeric(tagcache_idx
, tag_playcount
, playcount
);
721 tagcache_update_numeric(tagcache_idx
, tag_playtime
, playtime
);
722 tagcache_update_numeric(tagcache_idx
, tag_lastplayed
, lastplayed
);
725 bool tagtree_export(void)
727 struct tagcache_search tcs
;
729 splash(0, str(LANG_CREATING
));
730 if (!tagcache_create_changelog(&tcs
))
732 splash(HZ
*2, ID2P(LANG_FAILED
));
738 bool tagtree_import(void)
740 splash(0, ID2P(LANG_WAIT
));
741 if (!tagcache_import_changelog())
743 splash(HZ
*2, ID2P(LANG_FAILED
));
749 static bool parse_menu(const char *filename
);
751 static int parse_line(int n
, const char *buf
, void *parameters
)
755 static bool read_menu
;
760 logf("parse:%d/%s", n
, buf
);
762 /* First line, do initialisation. */
765 if (strcasecmp(TAGNAVI_VERSION
, buf
))
767 logf("Version mismatch");
790 if (get_tag(&variable
) <= 0)
796 if (add_format(strp
) < 0)
798 logf("Format add fail: %s", data
);
803 if (get_token_str(data
, sizeof(data
)) < 0)
805 logf("%%include empty");
809 if (!parse_menu(data
))
811 logf("Load menu fail: %s", data
);
816 if (menu_count
>= TAGMENU_MAX_MENUS
)
818 logf("max menucount reached");
822 if (get_token_str(data
, sizeof data
) < 0)
824 logf("%%menu_start id empty");
829 for (i
= 0; i
< menu_count
; i
++)
831 if (!strcasecmp(menus
[i
]->id
, data
))
839 menus
[menu_count
] = buffer_alloc(sizeof(struct menu_root
));
840 menu
= menus
[menu_count
];
842 memset(menu
, 0, sizeof(struct menu_root
));
843 strlcpy(menu
->id
, data
, MAX_MENU_ID_SIZE
);
846 if (get_token_str(menu
->title
, sizeof(menu
->title
)) < 0)
848 logf("%%menu_start title empty");
851 logf("menu: %s", menu
->title
);
856 /* Only set root menu once. */
860 if (get_token_str(data
, sizeof(data
)) < 0)
862 logf("%%rootmenu empty");
866 for (i
= 0; i
< menu_count
; i
++)
868 if (!strcasecmp(menus
[i
]->id
, data
))
879 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
881 logf("max itemcount reached");
886 if (menu
->items
[menu
->itemcount
] == NULL
)
888 menu
->items
[menu
->itemcount
] = buffer_alloc(sizeof(struct menu_entry
));
889 memset(menu
->items
[menu
->itemcount
], 0, sizeof(struct menu_entry
));
890 menu
->items
[menu
->itemcount
]->si
= buffer_alloc(sizeof(struct search_instruction
));
893 memset(menu
->items
[menu
->itemcount
]->si
, 0, sizeof(struct search_instruction
));
894 if (!parse_search(menu
->items
[menu
->itemcount
], buf
))
902 static bool parse_menu(const char *filename
)
907 if (menu_count
>= TAGMENU_MAX_MENUS
)
909 logf("max menucount reached");
913 fd
= open(filename
, O_RDONLY
);
916 logf("Search instruction file not found.");
920 /* Now read file for real, parsing into si */
921 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
927 void tagtree_init(void)
933 parse_menu(FILE_SEARCH_INSTRUCTIONS
);
935 /* If no root menu is set, assume it's the first single menu
936 * we have. That shouldn't normally happen. */
940 uniqbuf
= buffer_alloc(UNIQBUF_SIZE
);
942 add_event(PLAYBACK_EVENT_TRACK_BUFFER
, false, tagtree_buffer_event
);
943 add_event(PLAYBACK_EVENT_TRACK_FINISH
, false, tagtree_track_finish_event
);
946 static bool show_search_progress(bool init
, int count
)
948 static int last_tick
= 0;
950 /* Don't show splashes for 1/2 second after starting search */
953 last_tick
= current_tick
+ HZ
/2;
957 /* Update progress every 1/10 of a second */
958 if (TIME_AFTER(current_tick
, last_tick
+ HZ
/10))
960 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG
), count
, str(LANG_OFF_ABORT
));
961 if (action_userabort(TIMEOUT_NOBLOCK
))
963 last_tick
= current_tick
;
970 static int format_str(struct tagcache_search
*tcs
, struct display_format
*fmt
,
971 char *buf
, int buf_size
)
974 bool read_format
= false;
980 memset(buf
, 0, buf_size
);
981 for (i
= 0; fmt
->formatstr
[i
] != '\0'; i
++)
983 if (fmt
->formatstr
[i
] == '%')
987 if (parpos
>= fmt
->tag_count
)
989 logf("too many format tags");
996 fmtbuf
[fmtbuf_pos
++] = fmt
->formatstr
[i
];
997 if (fmtbuf_pos
>= buf_size
)
999 logf("format parse error");
1003 if (fmt
->formatstr
[i
] == 's')
1005 fmtbuf
[fmtbuf_pos
] = '\0';
1006 read_format
= false;
1007 if (fmt
->tags
[parpos
] == tcs
->type
)
1009 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
, tcs
->result
);
1013 /* Need to fetch the tag data. */
1014 if (!tagcache_retrieve(tcs
, tcs
->idx_id
, fmt
->tags
[parpos
],
1015 &buf
[buf_pos
], buf_size
- buf_pos
))
1017 logf("retrieve failed");
1021 buf_pos
+= strlen(&buf
[buf_pos
]);
1024 else if (fmt
->formatstr
[i
] == 'd')
1026 fmtbuf
[fmtbuf_pos
] = '\0';
1027 read_format
= false;
1028 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
,
1029 tagcache_get_numeric(tcs
, fmt
->tags
[parpos
]));
1030 buf_pos
+= strlen(&buf
[buf_pos
]);
1036 buf
[buf_pos
++] = fmt
->formatstr
[i
];
1038 if (buf_pos
- 1 >= buf_size
)
1040 logf("buffer overflow");
1045 buf
[buf_pos
++] = '\0';
1050 static int retrieve_entries(struct tree_context
*c
, int offset
, bool init
)
1052 struct tagcache_search tcs
;
1053 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1054 struct display_format
*fmt
;
1056 int namebufused
= 0;
1057 int total_count
= 0;
1058 int special_entry_count
= 0;
1059 int level
= c
->currextra
;
1065 /* Show search progress straight away if the disk needs to spin up,
1066 otherwise show it after the normal 1/2 second delay */
1067 show_search_progress(
1068 #ifdef HAVE_DISK_STORAGE
1069 storage_disk_is_active()
1075 if (c
->currtable
== ALLSUBENTRIES
)
1081 tag
= csi
->tagorder
[level
];
1083 if (!tagcache_search(&tcs
, tag
))
1086 /* Prevent duplicate entries in the search list. */
1087 tagcache_search_set_uniqbuf(&tcs
, uniqbuf
, UNIQBUF_SIZE
);
1089 if (level
|| csi
->clause_count
[0] || TAGCACHE_IS_NUMERIC(tag
))
1092 for (i
= 0; i
< level
; i
++)
1094 if (TAGCACHE_IS_NUMERIC(csi
->tagorder
[i
]))
1096 static struct tagcache_search_clause cc
;
1098 memset(&cc
, 0, sizeof(struct tagcache_search_clause
));
1099 cc
.tag
= csi
->tagorder
[i
];
1100 cc
.type
= clause_is
;
1102 cc
.numeric_data
= csi
->result_seek
[i
];
1103 tagcache_search_add_clause(&tcs
, &cc
);
1107 tagcache_search_add_filter(&tcs
, csi
->tagorder
[i
],
1108 csi
->result_seek
[i
]);
1112 for (i
= 0; i
<= level
; i
++)
1116 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1117 tagcache_search_add_clause(&tcs
, csi
->clause
[i
][j
]);
1120 current_offset
= offset
;
1121 current_entry_count
= 0;
1125 for (i
= 0; i
< format_count
; i
++)
1127 if (formats
[i
]->group_id
== csi
->format_id
[level
])
1133 sort_inverse
= fmt
->sort_inverse
;
1134 sort_limit
= fmt
->limit
;
1140 sort_inverse
= false;
1145 if (tag
!= tag_title
&& tag
!= tag_filename
)
1149 dptr
->newtable
= ALLSUBENTRIES
;
1150 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1152 current_entry_count
++;
1156 dptr
->newtable
= NAVIBROWSE
;
1157 dptr
->name
= str(LANG_TAGNAVI_RANDOM
);
1158 dptr
->extraseek
= -1;
1160 current_entry_count
++;
1162 special_entry_count
+=2;
1165 total_count
+= special_entry_count
;
1167 while (tagcache_get_next(&tcs
))
1169 if (total_count
++ < offset
)
1172 if ( strcmp(tcs
.result
, UNTAGGED
) == 0)
1174 tcs
.result_len
= strlcpy(tcs
.result
,
1175 str(LANG_TAGNAVI_UNTAGGED
), TAG_MAXLEN
)+1;
1178 dptr
->newtable
= NAVIBROWSE
;
1179 if (tag
== tag_title
|| tag
== tag_filename
)
1181 dptr
->newtable
= PLAYTRACK
;
1182 dptr
->extraseek
= tcs
.idx_id
;
1185 dptr
->extraseek
= tcs
.result_seek
;
1188 /* Check the format */
1189 for (i
= 0; i
< format_count
; i
++)
1191 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1194 if (tagcache_check_clauses(&tcs
, formats
[i
]->clause
,
1195 formats
[i
]->clause_count
))
1202 if (!tcs
.ramresult
|| fmt
)
1208 if (format_str(&tcs
, fmt
, buf
, sizeof buf
) < 0)
1210 logf("format_str() failed");
1211 tagcache_search_finish(&tcs
);
1216 dptr
->name
= &c
->name_buffer
[namebufused
];
1218 namebufused
+= strlen(buf
)+1;
1220 namebufused
+= tcs
.result_len
;
1222 if (namebufused
>= c
->name_buffer_size
)
1224 logf("chunk mode #2: %d", current_entry_count
);
1230 strcpy(dptr
->name
, buf
);
1232 strcpy(dptr
->name
, tcs
.result
);
1235 dptr
->name
= tcs
.result
;
1238 current_entry_count
++;
1240 if (current_entry_count
>= global_settings
.max_files_in_dir
)
1242 logf("chunk mode #3: %d", current_entry_count
);
1248 if (init
&& !tcs
.ramsearch
)
1250 if (!show_search_progress(false, total_count
))
1251 { /* user aborted */
1252 tagcache_search_finish(&tcs
);
1253 return current_entry_count
;
1259 qsort(c
->dircache
+ special_entry_count
* c
->dentry_size
,
1260 current_entry_count
- special_entry_count
,
1261 c
->dentry_size
, compare
);
1265 tagcache_search_finish(&tcs
);
1266 return current_entry_count
;
1269 while (tagcache_get_next(&tcs
))
1273 if (!show_search_progress(false, total_count
))
1279 tagcache_search_finish(&tcs
);
1281 if (!sort
&& (sort_inverse
|| sort_limit
))
1283 splashf(HZ
*4, ID2P(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1284 logf("Too small dir buffer");
1289 total_count
= MIN(total_count
, sort_limit
);
1294 for (i
= 0; i
< total_count
; i
++, dptr
++)
1296 int len
= strlen(dptr
->name
);
1301 dptr
->name
= &dptr
->name
[strip
];
1309 static int load_root(struct tree_context
*c
)
1311 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1315 c
->currtable
= ROOT
;
1316 if (c
->dirlevel
== 0)
1317 c
->currextra
= rootmenu
;
1319 menu
= menus
[c
->currextra
];
1323 for (i
= 0; i
< menu
->itemcount
; i
++)
1325 dptr
->name
= menu
->items
[i
]->name
;
1326 switch (menu
->items
[i
]->type
)
1329 dptr
->newtable
= NAVIBROWSE
;
1330 dptr
->extraseek
= i
;
1334 dptr
->newtable
= ROOT
;
1335 dptr
->extraseek
= menu
->items
[i
]->link
;
1343 current_entry_count
= i
;
1348 int tagtree_load(struct tree_context
* c
)
1351 int table
= c
->currtable
;
1353 c
->dentry_size
= sizeof(struct tagentry
);
1360 c
->currtable
= table
;
1361 c
->currextra
= rootmenu
;
1367 count
= load_root(c
);
1372 logf("navibrowse...");
1374 count
= retrieve_entries(c
, 0, true);
1379 logf("Unsupported table %d\n", table
);
1386 count
= load_root(c
);
1387 splash(HZ
, str(LANG_TAGCACHE_BUSY
));
1390 /* The _total_ numer of entries available. */
1391 c
->dirlength
= c
->filesindir
= count
;
1396 int tagtree_enter(struct tree_context
* c
)
1399 struct tagentry
*dptr
;
1400 struct mp3entry
*id3
;
1405 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1408 seek
= dptr
->extraseek
;
1411 if(c
->filesindir
<=2)
1413 srand(current_tick
);
1414 dptr
= (tagtree_get_entry(c
, 2+(rand() % (c
->filesindir
-2))));
1415 seek
= dptr
->extraseek
;
1417 newextra
= dptr
->newtable
;
1419 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1422 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1423 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1424 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1425 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1428 switch (c
->currtable
) {
1430 c
->currextra
= newextra
;
1432 if (newextra
== ROOT
)
1435 c
->currextra
= seek
;
1438 else if (newextra
== NAVIBROWSE
)
1442 csi
= menu
->items
[seek
]->si
;
1445 strlcpy(current_title
[c
->currextra
], dptr
->name
,
1446 sizeof(current_title
[0]));
1448 /* Read input as necessary. */
1449 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1451 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1454 source
= csi
->clause
[i
][j
]->source
;
1456 if (source
== source_constant
)
1459 searchstring
=csi
->clause
[i
][j
]->str
;
1460 *searchstring
= '\0';
1462 id3
= audio_current_track();
1464 if (source
== source_current_path
&& id3
)
1467 strlcpy(searchstring
, id3
->path
, SEARCHSTR_SIZE
);
1468 e
= strrchr(searchstring
, '/');
1472 else if (source
> source_runtime
&& id3
)
1475 int k
= source
-source_runtime
;
1476 int offset
= id3_to_search_mapping
[k
].id3_offset
;
1477 char **src
= (char**)((char*)id3
+ offset
);
1480 strlcpy(searchstring
, *src
, SEARCHSTR_SIZE
);
1485 rc
= kbd_input(searchstring
, SEARCHSTR_SIZE
);
1486 if (rc
< 0 || !searchstring
[0])
1491 if (csi
->clause
[i
][j
]->numeric
)
1492 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1499 c
->currtable
= newextra
;
1505 if (newextra
== PLAYTRACK
)
1507 if (global_settings
.party_mode
&& audio_status()) {
1508 splash(HZ
, ID2P(LANG_PARTY_MODE
));
1512 /* about to create a new current playlist...
1513 allow user to cancel the operation */
1514 if (!warn_on_pl_erase())
1516 if (tagtree_play_folder(c
) >= 0)
1521 c
->currtable
= newextra
;
1522 csi
->result_seek
[c
->currextra
] = seek
;
1523 if (c
->currextra
< csi
->tagorder_count
-1)
1528 /* Update the statusbar title */
1529 strlcpy(current_title
[c
->currextra
], dptr
->name
,
1530 sizeof(current_title
[0]));
1539 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1544 void tagtree_exit(struct tree_context
* c
)
1547 if (c
->dirlevel
> 0)
1549 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1550 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1551 c
->currtable
= c
->table_history
[c
->dirlevel
];
1552 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1553 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1556 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1558 struct tagcache_search tcs
;
1559 struct tagentry
*entry
;
1561 entry
= tagtree_get_entry(c
, c
->selected_item
);
1563 if (!tagcache_search(&tcs
, tag_filename
))
1566 if (!tagcache_retrieve(&tcs
, entry
->extraseek
, tcs
.type
, buf
, buflen
))
1568 tagcache_search_finish(&tcs
);
1572 tagcache_search_finish(&tcs
);
1577 static bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1579 struct tagcache_search tcs
;
1582 int from
, to
, direction
;
1583 int files_left
= c
->filesindir
;
1586 if (!tagcache_search(&tcs
, tag_filename
))
1588 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
1593 if (position
== PLAYLIST_REPLACE
)
1595 if (playlist_remove_all_tracks(NULL
) == 0)
1596 position
= PLAYLIST_INSERT_LAST
;
1604 if (position
== PLAYLIST_INSERT_FIRST
)
1606 from
= c
->filesindir
- 1;
1617 for (i
= from
; i
!= to
; i
+= direction
)
1619 /* Count back to zero */
1620 if (!show_search_progress(false, files_left
--))
1623 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1624 tcs
.type
, buf
, sizeof buf
))
1629 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1631 logf("playlist_insert_track failed");
1636 playlist_sync(NULL
);
1637 tagcache_search_finish(&tcs
);
1643 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1645 struct tagentry
*dptr
;
1647 int dirlevel
= tc
->dirlevel
;
1649 show_search_progress(
1650 #ifdef HAVE_DISK_STORAGE
1651 storage_disk_is_active()
1658 /* We need to set the table to allsubentries. */
1659 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1661 /* Insert a single track? */
1662 if (dptr
->newtable
== PLAYTRACK
)
1664 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1666 logf("tagtree_get_filename failed");
1669 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1674 if (dptr
->newtable
== NAVIBROWSE
)
1678 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1680 else if (dptr
->newtable
!= ALLSUBENTRIES
)
1682 logf("unsupported table: %d", dptr
->newtable
);
1686 /* Now the current table should be allsubentries. */
1687 if (dptr
->newtable
!= PLAYTRACK
)
1691 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1693 /* And now the newtable should be playtrack. */
1694 if (dptr
->newtable
!= PLAYTRACK
)
1696 logf("newtable: %d !!", dptr
->newtable
);
1697 tc
->dirlevel
= dirlevel
;
1702 if (tc
->filesindir
<= 0)
1703 splash(HZ
, ID2P(LANG_END_PLAYLIST
));
1706 logf("insert_all_playlist");
1707 if (!insert_all_playlist(tc
, position
, queue
))
1708 splash(HZ
*2, ID2P(LANG_FAILED
));
1711 /* Finally return the dirlevel to its original value. */
1712 while (tc
->dirlevel
> dirlevel
)
1719 static int tagtree_play_folder(struct tree_context
* c
)
1721 if (playlist_create(NULL
, NULL
) < 0)
1723 logf("Failed creating playlist\n");
1727 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1730 if (global_settings
.playlist_shuffle
)
1731 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
1732 if (!global_settings
.play_selected
)
1733 c
->selected_item
= 0;
1734 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1736 playlist_start(c
->selected_item
,0);
1737 playlist_get_current()->num_inserted_tracks
= 0; /* make warn on playlist erase work */
1741 struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
1743 struct tagentry
*entry
= (struct tagentry
*)c
->dircache
;
1744 int realid
= id
- current_offset
;
1746 /* Load the next chunk if necessary. */
1747 if (realid
>= current_entry_count
|| realid
< 0)
1750 if (retrieve_entries(c
, MAX(0, id
- (current_entry_count
/ 2)),
1753 logf("retrieve failed");
1757 realid
= id
- current_offset
;
1761 return &entry
[realid
];
1764 char *tagtree_get_title(struct tree_context
* c
)
1766 switch (c
->currtable
)
1773 return current_title
[c
->currextra
];
1779 int tagtree_get_attr(struct tree_context
* c
)
1782 switch (c
->currtable
)
1785 if (csi
->tagorder
[c
->currextra
] == tag_title
)
1786 attr
= FILE_ATTR_AUDIO
;
1788 attr
= ATTR_DIRECTORY
;
1792 attr
= FILE_ATTR_AUDIO
;
1796 attr
= ATTR_DIRECTORY
;
1803 int tagtree_get_icon(struct tree_context
* c
)
1805 int icon
= Icon_Folder
;
1807 if (tagtree_get_attr(c
) == FILE_ATTR_AUDIO
)