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"
57 #define str_or_empty(x) (x ? x : "(NULL)")
59 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
61 static int tagtree_play_folder(struct tree_context
* c
);
63 #define SEARCHSTR_SIZE 256
72 static const struct id3_to_search_mapping
{
75 } id3_to_search_mapping
[] = {
76 { "", 0 }, /* offset n/a */
77 { "#directory#", 0 }, /* offset n/a */
78 { "#title#", offsetof(struct mp3entry
, title
) },
79 { "#artist#", offsetof(struct mp3entry
, artist
) },
80 { "#album#", offsetof(struct mp3entry
, album
) },
81 { "#genre#", offsetof(struct mp3entry
, genre_string
) },
82 { "#composer#", offsetof(struct mp3entry
, composer
) },
83 { "#albumartist#", offsetof(struct mp3entry
, albumartist
) },
97 /* Capacity 10 000 entries (for example 10k different artists) */
98 #define UNIQBUF_SIZE (64*1024)
102 #define MAX_MENU_ID_SIZE 32
104 static bool sort_inverse
;
107 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
110 * formatstr = "%-3d. %s"
111 * tags[0] = tag_autoscore
112 * tags[1] = tag_title
116 * sort_inverse = true
118 struct display_format
{
120 struct tagcache_search_clause
*clause
[TAGCACHE_MAX_CLAUSES
];
132 static struct display_format
*formats
[TAGMENU_MAX_FMTS
];
133 static int format_count
;
138 struct search_instruction
{
140 int tagorder
[MAX_TAGS
];
142 struct tagcache_search_clause
*clause
[MAX_TAGS
][TAGCACHE_MAX_CLAUSES
];
143 int format_id
[MAX_TAGS
];
144 int clause_count
[MAX_TAGS
];
145 int result_seek
[MAX_TAGS
];
152 char id
[MAX_MENU_ID_SIZE
];
154 struct menu_entry
*items
[TAGMENU_MAX_ITEMS
];
163 /* Statusbar text of the current view. */
164 static char current_title
[MAX_TAGS
][128];
166 static struct menu_root
*menus
[TAGMENU_MAX_MENUS
];
167 static struct menu_root
*menu
;
168 static struct search_instruction
*csi
;
169 static const char *strp
;
170 static int menu_count
;
173 static int current_offset
;
174 static int current_entry_count
;
176 static struct tree_context
*tc
;
178 /* a few memory alloc helper */
179 static void* tagtree_alloc(size_t size
)
181 return buffer_alloc(size
);
184 static void* tagtree_alloc0(size_t size
)
186 void* ret
= tagtree_alloc(size
);
187 memset(ret
, 0, size
);
191 static char* tagtree_strdup(const char* buf
)
193 size_t len
= strlen(buf
) + 1;
194 char* dest
= tagtree_alloc(len
);
199 static int get_token_str(char *buf
, int size
)
201 /* Find the start. */
202 while (*strp
!= '"' && *strp
!= '\0')
205 if (*strp
== '\0' || *(++strp
) == '\0')
209 while (*strp
!= '"' && *strp
!= '\0' && --size
> 0)
210 *(buf
++) = *(strp
++);
221 static int get_tag(int *tag
)
223 static const struct match get_tag_match
[] =
225 {"album", tag_album
},
226 {"artist", tag_artist
},
227 {"bitrate", tag_bitrate
},
228 {"composer", tag_composer
},
229 {"comment", tag_comment
},
230 {"albumartist", tag_albumartist
},
231 {"ensemble", tag_albumartist
},
232 {"grouping", tag_grouping
},
233 {"genre", tag_genre
},
234 {"length", tag_length
},
235 {"Lm", tag_virt_length_min
},
236 {"Ls", tag_virt_length_sec
},
237 {"Pm", tag_virt_playtime_min
},
238 {"Ps", tag_virt_playtime_sec
},
239 {"title", tag_title
},
240 {"filename", tag_filename
},
241 {"basename", tag_virt_basename
},
242 {"tracknum", tag_tracknumber
},
243 {"discnum", tag_discnumber
},
245 {"playcount", tag_playcount
},
246 {"rating", tag_rating
},
247 {"lastplayed", tag_lastplayed
},
248 {"lastoffset", tag_lastoffset
},
249 {"commitid", tag_commitid
},
250 {"entryage", tag_virt_entryage
},
251 {"autoscore", tag_virt_autoscore
},
252 {"%sort", var_sorttype
},
253 {"%limit", var_limit
},
254 {"%strip", var_strip
},
255 {"%menu_start", var_menu_start
},
256 {"%include", var_include
},
257 {"%root_menu", var_rootmenu
},
258 {"%format", var_format
},
265 /* Find the start. */
266 while ((*strp
== ' ' || *strp
== '>') && *strp
!= '\0')
269 if (*strp
== '\0' || *strp
== '?')
272 for (i
= 0; i
< sizeof(buf
)-1; i
++)
274 if (*strp
== '\0' || *strp
== ' ')
281 for (i
= 0; i
< ARRAYLEN(get_tag_match
); i
++)
283 if (!strcasecmp(buf
, get_tag_match
[i
].str
))
285 *tag
= get_tag_match
[i
].symbol
;
290 logf("NO MATCH: %s\n", buf
);
297 static int get_clause(int *condition
)
299 static const struct match get_clause_match
[] =
303 {"!=", clause_is_not
},
308 {"~", clause_contains
},
309 {"!~", clause_not_contains
},
310 {"^", clause_begins_with
},
311 {"!^", clause_not_begins_with
},
312 {"$", clause_ends_with
},
313 {"!$", clause_not_ends_with
},
320 /* Find the start. */
321 while (*strp
== ' ' && *strp
!= '\0')
327 for (i
= 0; i
< sizeof(buf
)-1; i
++)
329 if (*strp
== '\0' || *strp
== ' ')
336 for (i
= 0; i
< ARRAYLEN(get_clause_match
); i
++)
338 if (!strcasecmp(buf
, get_clause_match
[i
].str
))
340 *condition
= get_clause_match
[i
].symbol
;
348 static bool read_clause(struct tagcache_search_clause
*clause
)
350 char buf
[SEARCHSTR_SIZE
];
353 if (get_tag(&clause
->tag
) <= 0)
356 if (get_clause(&clause
->type
) <= 0)
359 if (get_token_str(buf
, sizeof buf
) < 0)
362 for (i
=0; i
<ARRAYLEN(id3_to_search_mapping
); i
++)
364 if (!strcasecmp(buf
, id3_to_search_mapping
[i
].string
))
368 if (i
<ARRAYLEN(id3_to_search_mapping
)) /* runtime search operand found */
370 clause
->source
= source_runtime
+i
;
371 clause
->str
= tagtree_alloc(SEARCHSTR_SIZE
);
375 clause
->source
= source_constant
;
376 clause
->str
= tagtree_strdup(buf
);
379 if (TAGCACHE_IS_NUMERIC(clause
->tag
))
381 clause
->numeric
= true;
382 clause
->numeric_data
= atoi(clause
->str
);
385 clause
->numeric
= false;
387 logf("got clause: %d/%d [%s]", clause
->tag
, clause
->type
, clause
->str
);
392 static bool read_variable(char *buf
, int size
)
396 if (!get_clause(&condition
))
399 if (condition
!= clause_is
)
402 if (get_token_str(buf
, size
) < 0)
408 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
409 static int get_format_str(struct display_format
*fmt
)
415 memset(fmt
, 0, sizeof(struct display_format
));
417 if (get_token_str(fmt
->name
, sizeof fmt
->name
) < 0)
420 /* Determine the group id */
422 for (i
= 0; i
< format_count
; i
++)
424 if (!strcasecmp(formats
[i
]->name
, fmt
->name
))
426 fmt
->group_id
= formats
[i
]->group_id
;
430 if (formats
[i
]->group_id
> fmt
->group_id
)
431 fmt
->group_id
= formats
[i
]->group_id
;
434 if (i
== format_count
)
437 logf("format: (%d) %s", fmt
->group_id
, fmt
->name
);
439 if (get_token_str(buf
, sizeof buf
) < 0)
442 fmt
->formatstr
= tagtree_strdup(buf
);
444 while (fmt
->tag_count
< MAX_TAGS
)
446 ret
= get_tag(&fmt
->tags
[fmt
->tag_count
]);
453 switch (fmt
->tags
[fmt
->tag_count
]) {
455 if (!read_variable(buf
, sizeof buf
))
457 if (!strcasecmp("inverse", buf
))
458 fmt
->sort_inverse
= true;
462 if (!read_variable(buf
, sizeof buf
))
464 fmt
->limit
= atoi(buf
);
468 if (!read_variable(buf
, sizeof buf
))
470 fmt
->strip
= atoi(buf
);
481 static int add_format(const char *buf
)
483 if (format_count
>= TAGMENU_MAX_FMTS
)
485 logf("too many formats");
491 if (formats
[format_count
] == NULL
)
492 formats
[format_count
] = tagtree_alloc0(sizeof(struct display_format
));
494 if (get_format_str(formats
[format_count
]) < 0)
496 logf("get_format_str() parser failed!");
497 memset(formats
[format_count
], 0, sizeof(struct display_format
));
501 while (*strp
!= '\0' && *strp
!= '?')
506 int clause_count
= 0;
511 struct tagcache_search_clause
*newclause
;
513 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
515 logf("too many clauses");
519 newclause
= tagtree_alloc(sizeof(struct tagcache_search_clause
));
521 formats
[format_count
]->clause
[clause_count
] = newclause
;
522 if (!read_clause(newclause
))
528 formats
[format_count
]->clause_count
= clause_count
;
536 static int get_condition(struct search_instruction
*inst
)
538 struct tagcache_search_clause
*new_clause
;
548 if (get_token_str(buf
, sizeof buf
) < 0)
551 for (i
= 0; i
< format_count
; i
++)
553 if (!strcasecmp(formats
[i
]->name
, buf
))
557 if (i
== format_count
)
559 logf("format not found: %s", buf
);
563 inst
->format_id
[inst
->tagorder_count
] = formats
[i
]->group_id
;
576 clause_count
= inst
->clause_count
[inst
->tagorder_count
];
577 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
579 logf("Too many clauses");
583 new_clause
= tagtree_alloc(sizeof(struct tagcache_search_clause
));
584 inst
->clause
[inst
->tagorder_count
][clause_count
] = new_clause
;
589 new_clause
->type
= clause_logical_or
;
591 else if (!read_clause(new_clause
))
594 inst
->clause_count
[inst
->tagorder_count
]++;
600 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
601 * : album ? year >= "2000" : songs
607 static bool parse_search(struct menu_entry
*entry
, const char *str
)
611 struct search_instruction
*inst
= &entry
->si
;
614 struct menu_root
*new_menu
;
618 /* Parse entry name */
619 if (get_token_str(entry
->name
, sizeof entry
->name
) < 0)
621 logf("No name found.");
625 /* Parse entry type */
626 if (get_tag(&entry
->type
) <= 0)
629 if (entry
->type
== menu_load
)
631 if (get_token_str(buf
, sizeof buf
) < 0)
634 /* Find the matching root menu or "create" it */
635 for (i
= 0; i
< menu_count
; i
++)
637 if (!strcasecmp(menus
[i
]->id
, buf
))
644 if (menu_count
>= TAGMENU_MAX_MENUS
)
646 logf("max menucount reached");
650 /* Allocate a new menu unless link is found. */
651 menus
[menu_count
] = tagtree_alloc0(sizeof(struct menu_root
));
652 new_menu
= menus
[menu_count
];
653 strlcpy(new_menu
->id
, buf
, MAX_MENU_ID_SIZE
);
654 entry
->link
= menu_count
;
660 if (entry
->type
!= menu_next
)
663 while (inst
->tagorder_count
< MAX_TAGS
)
665 ret
= get_tag(&inst
->tagorder
[inst
->tagorder_count
]);
668 logf("Parse error #1");
676 logf("tag: %d", inst
->tagorder
[inst
->tagorder_count
]);
678 while ( (ret
= get_condition(inst
)) > 0 ) ;
682 inst
->tagorder_count
++;
684 if (get_tag(&type
) <= 0 || type
!= menu_next
)
691 static int compare(const void *p1
, const void *p2
)
693 struct tagentry
*e1
= (struct tagentry
*)p1
;
694 struct tagentry
*e2
= (struct tagentry
*)p2
;
697 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
699 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
702 static void tagtree_buffer_event(void *data
)
704 struct tagcache_search tcs
;
705 struct mp3entry
*id3
= (struct mp3entry
*)data
;
707 /* Do not gather data unless proper setting has been enabled. */
708 if (!global_settings
.runtimedb
&& !global_settings
.autoresume_enable
)
711 logf("be:%s", id3
->path
);
713 while (! tagcache_is_fully_initialized())
716 if (!tagcache_find_index(&tcs
, id3
->path
))
718 logf("tc stat: not found: %s", id3
->path
);
722 if (global_settings
.runtimedb
)
724 id3
->playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
726 id3
->rating
= tagcache_get_numeric(&tcs
, tag_rating
);
727 id3
->lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
728 id3
->score
= tagcache_get_numeric(&tcs
, tag_virt_autoscore
) / 10;
729 id3
->playtime
= tagcache_get_numeric(&tcs
, tag_playtime
);
731 logf("-> %ld/%ld", id3
->playcount
, id3
->playtime
);
734 #if CONFIG_CODEC == SWCODEC
735 if (global_settings
.autoresume_enable
)
737 /* Load current file resume offset if not already defined (by
738 another resume mechanism) */
739 if (id3
->offset
== 0)
741 id3
->offset
= tagcache_get_numeric(&tcs
, tag_lastoffset
);
743 logf("tagtree_buffer_event: Set offset for %s to %lX\n",
744 str_or_empty(id3
->title
), id3
->offset
);
749 /* Store our tagcache index pointer. */
750 id3
->tagcache_idx
= tcs
.idx_id
+1;
752 tagcache_search_finish(&tcs
);
755 static void tagtree_track_finish_event(void *data
)
759 struct mp3entry
*id3
= (struct mp3entry
*)data
;
761 /* Do not gather data unless proper setting has been enabled. */
762 if (!global_settings
.runtimedb
&& !global_settings
.autoresume_enable
)
764 logf("runtimedb gathering and autoresume not enabled");
768 tagcache_idx
=id3
->tagcache_idx
;
771 logf("No tagcache index pointer found");
776 /* Don't process unplayed tracks, or tracks interrupted within the
778 if (id3
->elapsed
== 0
779 #if CONFIG_CODEC == SWCODEC /* HWCODEC doesn't have automatic_skip */
780 || (id3
->elapsed
< 15 * 1000 && !audio_automatic_skip())
784 logf("not logging unplayed or skipped track");
788 lastplayed
= tagcache_increase_serial();
791 logf("incorrect tc serial:%ld", lastplayed
);
795 if (global_settings
.runtimedb
)
800 playcount
= id3
->playcount
+ 1;
802 /* Ignore the last 15s (crossfade etc.) */
803 playtime
= id3
->playtime
+ MIN(id3
->length
, id3
->elapsed
+ 15 * 1000);
805 logf("ube:%s", id3
->path
);
806 logf("-> %ld/%ld", playcount
, playtime
);
807 logf("-> %ld/%ld/%ld", id3
->elapsed
, id3
->length
,
808 MIN(id3
->length
, id3
->elapsed
+ 15 * 1000));
810 /* Queue the updates to the tagcache system. */
811 tagcache_update_numeric(tagcache_idx
, tag_playcount
, playcount
);
812 tagcache_update_numeric(tagcache_idx
, tag_playtime
, playtime
);
813 tagcache_update_numeric(tagcache_idx
, tag_lastplayed
, lastplayed
);
816 #if CONFIG_CODEC == SWCODEC
817 if (global_settings
.autoresume_enable
)
820 = audio_automatic_skip() ? 0 : id3
->offset
;
822 tagcache_update_numeric(tagcache_idx
, tag_lastoffset
, offset
);
824 logf("tagtree_track_finish_event: Save offset for %s: %lX",
825 str_or_empty(id3
->title
), offset
);
830 bool tagtree_export(void)
832 struct tagcache_search tcs
;
834 splash(0, str(LANG_CREATING
));
835 if (!tagcache_create_changelog(&tcs
))
837 splash(HZ
*2, ID2P(LANG_FAILED
));
843 bool tagtree_import(void)
845 splash(0, ID2P(LANG_WAIT
));
846 if (!tagcache_import_changelog())
848 splash(HZ
*2, ID2P(LANG_FAILED
));
854 static bool parse_menu(const char *filename
);
856 static int parse_line(int n
, char *buf
, void *parameters
)
860 static bool read_menu
;
866 /* Strip possible <CR> at end of line. */
867 p
= strchr(buf
, '\r');
871 logf("parse:%d/%s", n
, buf
);
873 /* First line, do initialisation. */
876 if (strcasecmp(TAGNAVI_VERSION
, buf
))
878 logf("Version mismatch");
901 if (get_tag(&variable
) <= 0)
907 if (add_format(strp
) < 0)
909 logf("Format add fail: %s", data
);
914 if (get_token_str(data
, sizeof(data
)) < 0)
916 logf("%%include empty");
920 if (!parse_menu(data
))
922 logf("Load menu fail: %s", data
);
927 if (menu_count
>= TAGMENU_MAX_MENUS
)
929 logf("max menucount reached");
933 if (get_token_str(data
, sizeof data
) < 0)
935 logf("%%menu_start id empty");
940 for (i
= 0; i
< menu_count
; i
++)
942 if (!strcasecmp(menus
[i
]->id
, data
))
950 menus
[menu_count
] = tagtree_alloc0(sizeof(struct menu_root
));
951 menu
= menus
[menu_count
];
953 strlcpy(menu
->id
, data
, MAX_MENU_ID_SIZE
);
956 if (get_token_str(menu
->title
, sizeof(menu
->title
)) < 0)
958 logf("%%menu_start title empty");
961 logf("menu: %s", menu
->title
);
966 /* Only set root menu once. */
970 if (get_token_str(data
, sizeof(data
)) < 0)
972 logf("%%rootmenu empty");
976 for (i
= 0; i
< menu_count
; i
++)
978 if (!strcasecmp(menus
[i
]->id
, data
))
989 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
991 logf("max itemcount reached");
996 if (menu
->items
[menu
->itemcount
] == NULL
)
997 menu
->items
[menu
->itemcount
] = tagtree_alloc0(sizeof(struct menu_entry
));
999 if (!parse_search(menu
->items
[menu
->itemcount
], buf
))
1007 static bool parse_menu(const char *filename
)
1012 if (menu_count
>= TAGMENU_MAX_MENUS
)
1014 logf("max menucount reached");
1018 fd
= open(filename
, O_RDONLY
);
1021 logf("Search instruction file not found.");
1025 /* Now read file for real, parsing into si */
1026 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
1032 void tagtree_init(void)
1038 parse_menu(FILE_SEARCH_INSTRUCTIONS
);
1040 /* If no root menu is set, assume it's the first single menu
1041 * we have. That shouldn't normally happen. */
1045 uniqbuf
= tagtree_alloc(UNIQBUF_SIZE
);
1047 add_event(PLAYBACK_EVENT_TRACK_BUFFER
, false, tagtree_buffer_event
);
1048 add_event(PLAYBACK_EVENT_TRACK_FINISH
, false, tagtree_track_finish_event
);
1051 static bool show_search_progress(bool init
, int count
)
1053 static int last_tick
= 0;
1055 /* Don't show splashes for 1/2 second after starting search */
1058 last_tick
= current_tick
+ HZ
/2;
1062 /* Update progress every 1/10 of a second */
1063 if (TIME_AFTER(current_tick
, last_tick
+ HZ
/10))
1065 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG
), count
, str(LANG_OFF_ABORT
));
1066 if (action_userabort(TIMEOUT_NOBLOCK
))
1068 last_tick
= current_tick
;
1075 static int format_str(struct tagcache_search
*tcs
, struct display_format
*fmt
,
1076 char *buf
, int buf_size
)
1079 bool read_format
= false;
1080 unsigned fmtbuf_pos
= 0;
1085 memset(buf
, 0, buf_size
);
1086 for (i
= 0; fmt
->formatstr
[i
] != '\0'; i
++)
1088 if (fmt
->formatstr
[i
] == '%')
1092 if (parpos
>= fmt
->tag_count
)
1094 logf("too many format tags");
1099 char formatchar
= fmt
->formatstr
[i
];
1103 fmtbuf
[fmtbuf_pos
++] = formatchar
;
1104 if (fmtbuf_pos
>= sizeof fmtbuf
)
1106 logf("format parse error");
1110 if (formatchar
== 's' || formatchar
== 'd')
1112 unsigned space_left
= buf_size
- buf_pos
;
1113 char tmpbuf
[MAX_PATH
];
1116 fmtbuf
[fmtbuf_pos
] = '\0';
1117 read_format
= false;
1122 if (fmt
->tags
[parpos
] == tcs
->type
)
1124 result
= tcs
->result
;
1128 /* Need to fetch the tag data. */
1129 int tag
= fmt
->tags
[parpos
];
1131 if (!tagcache_retrieve(tcs
, tcs
->idx_id
,
1132 (tag
== tag_virt_basename
?
1133 tag_filename
: tag
),
1134 tmpbuf
, sizeof tmpbuf
))
1136 logf("retrieve failed");
1140 if (tag
== tag_virt_basename
1141 && (result
= strrchr(tmpbuf
, '/')) != NULL
)
1149 snprintf(&buf
[buf_pos
], space_left
, fmtbuf
, result
);
1154 snprintf(&buf
[buf_pos
], space_left
, fmtbuf
,
1155 tagcache_get_numeric(tcs
, fmt
->tags
[parpos
]));
1162 buf
[buf_pos
++] = formatchar
;
1164 if (buf_pos
>= buf_size
- 1) /* need at least one more byte for \0 */
1166 logf("buffer overflow");
1171 buf
[buf_pos
++] = '\0';
1176 static int retrieve_entries(struct tree_context
*c
, int offset
, bool init
)
1178 struct tagcache_search tcs
;
1179 struct tagentry
*dptr
= c
->cache
.entries
;
1180 struct display_format
*fmt
;
1182 int namebufused
= 0;
1183 int total_count
= 0;
1184 int special_entry_count
= 0;
1185 int level
= c
->currextra
;
1191 /* Show search progress straight away if the disk needs to spin up,
1192 otherwise show it after the normal 1/2 second delay */
1193 show_search_progress(
1194 #ifdef HAVE_DISK_STORAGE
1195 storage_disk_is_active()
1201 if (c
->currtable
== ALLSUBENTRIES
)
1207 tag
= csi
->tagorder
[level
];
1209 if (!tagcache_search(&tcs
, tag
))
1212 /* Prevent duplicate entries in the search list. */
1213 tagcache_search_set_uniqbuf(&tcs
, uniqbuf
, UNIQBUF_SIZE
);
1215 if (level
|| csi
->clause_count
[0] || TAGCACHE_IS_NUMERIC(tag
))
1218 for (i
= 0; i
< level
; i
++)
1220 if (TAGCACHE_IS_NUMERIC(csi
->tagorder
[i
]))
1222 static struct tagcache_search_clause cc
;
1224 memset(&cc
, 0, sizeof(struct tagcache_search_clause
));
1225 cc
.tag
= csi
->tagorder
[i
];
1226 cc
.type
= clause_is
;
1228 cc
.numeric_data
= csi
->result_seek
[i
];
1229 tagcache_search_add_clause(&tcs
, &cc
);
1233 tagcache_search_add_filter(&tcs
, csi
->tagorder
[i
],
1234 csi
->result_seek
[i
]);
1238 for (i
= 0; i
<= level
; i
++)
1242 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1243 tagcache_search_add_clause(&tcs
, csi
->clause
[i
][j
]);
1246 current_offset
= offset
;
1247 current_entry_count
= 0;
1251 for (i
= 0; i
< format_count
; i
++)
1253 if (formats
[i
]->group_id
== csi
->format_id
[level
])
1259 sort_inverse
= fmt
->sort_inverse
;
1260 sort_limit
= fmt
->limit
;
1266 sort_inverse
= false;
1271 if (tag
!= tag_title
&& tag
!= tag_filename
)
1275 dptr
->newtable
= ALLSUBENTRIES
;
1276 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1278 current_entry_count
++;
1279 special_entry_count
++;
1283 dptr
->newtable
= NAVIBROWSE
;
1284 dptr
->name
= str(LANG_TAGNAVI_RANDOM
);
1285 dptr
->extraseek
= -1;
1287 current_entry_count
++;
1288 special_entry_count
++;
1294 while (tagcache_get_next(&tcs
))
1296 if (total_count
++ < offset
)
1299 dptr
->newtable
= NAVIBROWSE
;
1300 if (tag
== tag_title
|| tag
== tag_filename
)
1302 dptr
->newtable
= PLAYTRACK
;
1303 dptr
->extraseek
= tcs
.idx_id
;
1306 dptr
->extraseek
= tcs
.result_seek
;
1309 /* Check the format */
1310 for (i
= 0; i
< format_count
; i
++)
1312 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1315 if (tagcache_check_clauses(&tcs
, formats
[i
]->clause
,
1316 formats
[i
]->clause_count
))
1323 if (strcmp(tcs
.result
, UNTAGGED
) == 0)
1325 tcs
.result
= str(LANG_TAGNAVI_UNTAGGED
);
1326 tcs
.result_len
= strlen(tcs
.result
);
1327 tcs
.ramresult
= true;
1330 if (!tcs
.ramresult
|| fmt
)
1332 dptr
->name
= &c
->cache
.name_buffer
[namebufused
];
1336 int ret
= format_str(&tcs
, fmt
, dptr
->name
,
1337 c
->cache
.name_buffer_size
- namebufused
);
1338 if (ret
== -4) /* buffer full */
1340 logf("chunk mode #2: %d", current_entry_count
);
1347 logf("format_str() failed");
1348 tagcache_search_finish(&tcs
);
1352 namebufused
+= strlen(dptr
->name
)+1;
1356 namebufused
+= tcs
.result_len
;
1357 if (namebufused
< c
->cache
.name_buffer_size
)
1358 strcpy(dptr
->name
, tcs
.result
);
1361 logf("chunk mode #2a: %d", current_entry_count
);
1369 dptr
->name
= tcs
.result
;
1372 current_entry_count
++;
1374 if (current_entry_count
>= c
->cache
.max_entries
)
1376 logf("chunk mode #3: %d", current_entry_count
);
1382 if (init
&& !tcs
.ramsearch
)
1384 if (!show_search_progress(false, total_count
))
1385 { /* user aborted */
1386 tagcache_search_finish(&tcs
);
1387 return current_entry_count
;
1394 int entry_size
= sizeof(struct tagentry
);
1395 qsort(c
->cache
.entries
+ special_entry_count
* entry_size
,
1396 current_entry_count
- special_entry_count
,
1397 entry_size
, compare
);
1402 tagcache_search_finish(&tcs
);
1403 return current_entry_count
;
1406 while (tagcache_get_next(&tcs
))
1410 if (!show_search_progress(false, total_count
))
1416 tagcache_search_finish(&tcs
);
1418 if (!sort
&& (sort_inverse
|| sort_limit
))
1420 splashf(HZ
*4, ID2P(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1421 logf("Too small dir buffer");
1426 total_count
= MIN(total_count
, sort_limit
);
1430 dptr
= c
->cache
.entries
;
1431 for (i
= special_entry_count
; i
< current_entry_count
; i
++, dptr
++)
1433 int len
= strlen(dptr
->name
);
1438 dptr
->name
= &dptr
->name
[strip
];
1446 static int load_root(struct tree_context
*c
)
1448 struct tagentry
*dptr
= c
->cache
.entries
;
1452 c
->currtable
= ROOT
;
1453 if (c
->dirlevel
== 0)
1454 c
->currextra
= rootmenu
;
1456 menu
= menus
[c
->currextra
];
1460 for (i
= 0; i
< menu
->itemcount
; i
++)
1462 dptr
->name
= menu
->items
[i
]->name
;
1463 switch (menu
->items
[i
]->type
)
1466 dptr
->newtable
= NAVIBROWSE
;
1467 dptr
->extraseek
= i
;
1471 dptr
->newtable
= ROOT
;
1472 dptr
->extraseek
= menu
->items
[i
]->link
;
1480 current_entry_count
= i
;
1485 int tagtree_load(struct tree_context
* c
)
1488 int table
= c
->currtable
;
1496 c
->currtable
= table
;
1497 c
->currextra
= rootmenu
;
1503 count
= load_root(c
);
1508 logf("navibrowse...");
1510 count
= retrieve_entries(c
, 0, true);
1515 logf("Unsupported table %d\n", table
);
1522 count
= load_root(c
);
1523 splash(HZ
, str(LANG_TAGCACHE_BUSY
));
1526 /* The _total_ numer of entries available. */
1527 c
->dirlength
= c
->filesindir
= count
;
1532 int tagtree_enter(struct tree_context
* c
)
1535 struct tagentry
*dptr
;
1536 struct mp3entry
*id3
;
1541 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1544 seek
= dptr
->extraseek
;
1547 if(c
->filesindir
<=2)
1549 srand(current_tick
);
1550 dptr
= (tagtree_get_entry(c
, 2+(rand() % (c
->filesindir
-2))));
1551 seek
= dptr
->extraseek
;
1553 newextra
= dptr
->newtable
;
1555 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1558 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1559 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1560 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1561 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1564 switch (c
->currtable
) {
1566 c
->currextra
= newextra
;
1568 if (newextra
== ROOT
)
1571 c
->currextra
= seek
;
1574 else if (newextra
== NAVIBROWSE
)
1578 csi
= &menu
->items
[seek
]->si
;
1581 strlcpy(current_title
[c
->currextra
], dptr
->name
,
1582 sizeof(current_title
[0]));
1584 /* Read input as necessary. */
1585 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1587 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1591 if (csi
->clause
[i
][j
]->type
== clause_logical_or
)
1594 source
= csi
->clause
[i
][j
]->source
;
1596 if (source
== source_constant
)
1599 searchstring
=csi
->clause
[i
][j
]->str
;
1600 *searchstring
= '\0';
1602 id3
= audio_current_track();
1604 if (source
== source_current_path
&& id3
)
1607 strlcpy(searchstring
, id3
->path
, SEARCHSTR_SIZE
);
1608 e
= strrchr(searchstring
, '/');
1612 else if (source
> source_runtime
&& id3
)
1615 int k
= source
-source_runtime
;
1616 int offset
= id3_to_search_mapping
[k
].id3_offset
;
1617 char **src
= (char**)((char*)id3
+ offset
);
1620 strlcpy(searchstring
, *src
, SEARCHSTR_SIZE
);
1625 rc
= kbd_input(searchstring
, SEARCHSTR_SIZE
);
1626 if (rc
< 0 || !searchstring
[0])
1631 if (csi
->clause
[i
][j
]->numeric
)
1632 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1639 c
->currtable
= newextra
;
1645 if (newextra
== PLAYTRACK
)
1647 if (global_settings
.party_mode
&& audio_status()) {
1648 splash(HZ
, ID2P(LANG_PARTY_MODE
));
1652 /* about to create a new current playlist...
1653 allow user to cancel the operation */
1654 if (!warn_on_pl_erase())
1656 if (tagtree_play_folder(c
) >= 0)
1661 c
->currtable
= newextra
;
1662 csi
->result_seek
[c
->currextra
] = seek
;
1663 if (c
->currextra
< csi
->tagorder_count
-1)
1668 /* Update the statusbar title */
1669 strlcpy(current_title
[c
->currextra
], dptr
->name
,
1670 sizeof(current_title
[0]));
1679 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1684 void tagtree_exit(struct tree_context
* c
)
1687 if (c
->dirlevel
> 0)
1689 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1690 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1691 c
->currtable
= c
->table_history
[c
->dirlevel
];
1692 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1693 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1696 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1698 struct tagcache_search tcs
;
1699 struct tagentry
*entry
;
1701 entry
= tagtree_get_entry(c
, c
->selected_item
);
1703 if (!tagcache_search(&tcs
, tag_filename
))
1706 if (!tagcache_retrieve(&tcs
, entry
->extraseek
, tcs
.type
, buf
, buflen
))
1708 tagcache_search_finish(&tcs
);
1712 tagcache_search_finish(&tcs
);
1717 static bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1719 struct tagcache_search tcs
;
1722 int from
, to
, direction
;
1723 int files_left
= c
->filesindir
;
1726 if (!tagcache_search(&tcs
, tag_filename
))
1728 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
1733 if (position
== PLAYLIST_REPLACE
)
1735 if (playlist_remove_all_tracks(NULL
) == 0)
1736 position
= PLAYLIST_INSERT_LAST
;
1744 if (position
== PLAYLIST_INSERT_FIRST
)
1746 from
= c
->filesindir
- 1;
1757 for (i
= from
; i
!= to
; i
+= direction
)
1759 /* Count back to zero */
1760 if (!show_search_progress(false, files_left
--))
1763 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1764 tcs
.type
, buf
, sizeof buf
))
1769 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1771 logf("playlist_insert_track failed");
1776 playlist_sync(NULL
);
1777 tagcache_search_finish(&tcs
);
1783 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1785 struct tagentry
*dptr
;
1787 int dirlevel
= tc
->dirlevel
;
1789 show_search_progress(
1790 #ifdef HAVE_DISK_STORAGE
1791 storage_disk_is_active()
1798 /* We need to set the table to allsubentries. */
1799 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1801 /* Insert a single track? */
1802 if (dptr
->newtable
== PLAYTRACK
)
1804 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1806 logf("tagtree_get_filename failed");
1809 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1814 if (dptr
->newtable
== NAVIBROWSE
)
1818 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1820 else if (dptr
->newtable
!= ALLSUBENTRIES
)
1822 logf("unsupported table: %d", dptr
->newtable
);
1826 /* Now the current table should be allsubentries. */
1827 if (dptr
->newtable
!= PLAYTRACK
)
1831 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1833 /* And now the newtable should be playtrack. */
1834 if (dptr
->newtable
!= PLAYTRACK
)
1836 logf("newtable: %d !!", dptr
->newtable
);
1837 tc
->dirlevel
= dirlevel
;
1842 if (tc
->filesindir
<= 0)
1843 splash(HZ
, ID2P(LANG_END_PLAYLIST
));
1846 logf("insert_all_playlist");
1847 if (!insert_all_playlist(tc
, position
, queue
))
1848 splash(HZ
*2, ID2P(LANG_FAILED
));
1851 /* Finally return the dirlevel to its original value. */
1852 while (tc
->dirlevel
> dirlevel
)
1859 static int tagtree_play_folder(struct tree_context
* c
)
1861 if (playlist_create(NULL
, NULL
) < 0)
1863 logf("Failed creating playlist\n");
1867 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1870 if (global_settings
.playlist_shuffle
)
1871 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
1872 if (!global_settings
.play_selected
)
1873 c
->selected_item
= 0;
1874 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1876 playlist_start(c
->selected_item
,0);
1877 playlist_get_current()->num_inserted_tracks
= 0; /* make warn on playlist erase work */
1881 struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
1883 struct tagentry
*entry
= (struct tagentry
*)c
->cache
.entries
;
1884 int realid
= id
- current_offset
;
1886 /* Load the next chunk if necessary. */
1887 if (realid
>= current_entry_count
|| realid
< 0)
1890 if (retrieve_entries(c
, MAX(0, id
- (current_entry_count
/ 2)),
1893 logf("retrieve failed");
1897 realid
= id
- current_offset
;
1901 return &entry
[realid
];
1904 char *tagtree_get_title(struct tree_context
* c
)
1906 switch (c
->currtable
)
1913 return current_title
[c
->currextra
];
1919 int tagtree_get_attr(struct tree_context
* c
)
1922 switch (c
->currtable
)
1925 if (csi
->tagorder
[c
->currextra
] == tag_title
)
1926 attr
= FILE_ATTR_AUDIO
;
1928 attr
= ATTR_DIRECTORY
;
1932 attr
= FILE_ATTR_AUDIO
;
1936 attr
= ATTR_DIRECTORY
;
1943 int tagtree_get_icon(struct tree_context
* c
)
1945 int icon
= Icon_Folder
;
1947 if (tagtree_get_attr(c
) == FILE_ATTR_AUDIO
)