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"
47 #include "core_alloc.h"
50 #include "filetypes.h"
52 #include "appevents.h"
58 #define str_or_empty(x) (x ? x : "(NULL)")
60 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
62 static int tagtree_play_folder(struct tree_context
* c
);
64 /* this needs to be same size as struct entry (tree.h) and name needs to be
65 * the first; so that they're compatible enough to walk arrays of both
66 * derefencing the name member*/
73 static struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
);
75 #define SEARCHSTR_SIZE 256
84 static const struct id3_to_search_mapping
{
87 } id3_to_search_mapping
[] = {
88 { "", 0 }, /* offset n/a */
89 { "#directory#", 0 }, /* offset n/a */
90 { "#title#", offsetof(struct mp3entry
, title
) },
91 { "#artist#", offsetof(struct mp3entry
, artist
) },
92 { "#album#", offsetof(struct mp3entry
, album
) },
93 { "#genre#", offsetof(struct mp3entry
, genre_string
) },
94 { "#composer#", offsetof(struct mp3entry
, composer
) },
95 { "#albumartist#", offsetof(struct mp3entry
, albumartist
) },
109 /* Capacity 10 000 entries (for example 10k different artists) */
110 #define UNIQBUF_SIZE (64*1024)
111 static long uniqbuf
[UNIQBUF_SIZE
/ sizeof(long)];
114 #define MAX_MENU_ID_SIZE 32
116 static bool sort_inverse
;
119 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
122 * formatstr = "%-3d. %s"
123 * tags[0] = tag_autoscore
124 * tags[1] = tag_title
128 * sort_inverse = true
130 struct display_format
{
132 struct tagcache_search_clause
*clause
[TAGCACHE_MAX_CLAUSES
];
144 static struct display_format
*formats
[TAGMENU_MAX_FMTS
];
145 static int format_count
;
150 struct search_instruction
{
152 int tagorder
[MAX_TAGS
];
154 struct tagcache_search_clause
*clause
[MAX_TAGS
][TAGCACHE_MAX_CLAUSES
];
155 int format_id
[MAX_TAGS
];
156 int clause_count
[MAX_TAGS
];
157 int result_seek
[MAX_TAGS
];
164 char id
[MAX_MENU_ID_SIZE
];
166 struct menu_entry
*items
[TAGMENU_MAX_ITEMS
];
175 /* Statusbar text of the current view. */
176 static char current_title
[MAX_TAGS
][128];
178 static struct menu_root
* menus
[TAGMENU_MAX_MENUS
];
179 static struct menu_root
* menu
;
180 static struct search_instruction
*csi
;
181 static const char *strp
;
182 static int menu_count
;
185 static int current_offset
;
186 static int current_entry_count
;
188 static struct tree_context
*tc
;
190 /* a few memory alloc helper */
191 static int tagtree_handle
, lock_count
;
192 static size_t tagtree_bufsize
, tagtree_buf_used
;
194 #define UPDATE(x, y) { x = (typeof(x))((char*)(x) + (y)); }
195 static int move_callback(int handle
, void* current
, void* new)
197 (void)handle
; (void)current
; (void)new;
198 ptrdiff_t diff
= new - current
;
201 return BUFLIB_CB_CANNOT_MOVE
;
204 /* loop over menus */
205 for(int i
= 0; i
< menu_count
; i
++)
207 struct menu_root
* menu
= menus
[i
];
208 /* then over the menu_entries of a menu */
209 for(int j
= 0; j
< menu
->itemcount
; j
++)
211 struct menu_entry
* mentry
= menu
->items
[j
];
212 /* then over the search_instructions of each menu_entry */
213 for(int k
= 0; k
< mentry
->si
.tagorder_count
; k
++)
215 for(int l
= 0; l
< mentry
->si
.clause_count
[k
]; l
++)
217 UPDATE(mentry
->si
.clause
[k
][l
]->str
, diff
);
218 UPDATE(mentry
->si
.clause
[k
][l
], diff
);
221 UPDATE(menu
->items
[j
], diff
);
223 UPDATE(menus
[i
], diff
);
226 /* now the same game for formats */
227 for(int i
= 0; i
< format_count
; i
++)
229 for(int j
= 0; j
< formats
[i
]->clause_count
; j
++)
231 UPDATE(formats
[i
]->clause
[j
]->str
, diff
);
232 UPDATE(formats
[i
]->clause
[j
], diff
);
235 if (formats
[i
]->formatstr
)
236 UPDATE(formats
[i
]->formatstr
, diff
);
238 UPDATE(formats
[i
], diff
);
244 static inline void tagtree_lock(void)
249 static inline void tagtree_unlock(void)
254 static struct buflib_callbacks ops
= {
255 .move_callback
= move_callback
,
256 .shrink_callback
= NULL
,
259 static void* tagtree_alloc(size_t size
)
261 char* buf
= core_get_data(tagtree_handle
) + tagtree_buf_used
;
262 size
= ALIGN_UP(size
, sizeof(void*));
263 tagtree_buf_used
+= size
;
267 static void* tagtree_alloc0(size_t size
)
269 void* ret
= tagtree_alloc(size
);
270 memset(ret
, 0, size
);
274 static char* tagtree_strdup(const char* buf
)
276 size_t len
= strlen(buf
) + 1;
277 char* dest
= tagtree_alloc(len
);
282 /* save to call without locking */
283 static int get_token_str(char *buf
, int size
)
285 /* Find the start. */
286 while (*strp
!= '"' && *strp
!= '\0')
289 if (*strp
== '\0' || *(++strp
) == '\0')
293 while (*strp
!= '"' && *strp
!= '\0' && --size
> 0)
294 *(buf
++) = *(strp
++);
305 static int get_tag(int *tag
)
307 static const struct match get_tag_match
[] =
309 {"album", tag_album
},
310 {"artist", tag_artist
},
311 {"bitrate", tag_bitrate
},
312 {"composer", tag_composer
},
313 {"comment", tag_comment
},
314 {"albumartist", tag_albumartist
},
315 {"ensemble", tag_albumartist
},
316 {"grouping", tag_grouping
},
317 {"genre", tag_genre
},
318 {"length", tag_length
},
319 {"Lm", tag_virt_length_min
},
320 {"Ls", tag_virt_length_sec
},
321 {"Pm", tag_virt_playtime_min
},
322 {"Ps", tag_virt_playtime_sec
},
323 {"title", tag_title
},
324 {"filename", tag_filename
},
325 {"basename", tag_virt_basename
},
326 {"tracknum", tag_tracknumber
},
327 {"discnum", tag_discnumber
},
329 {"playcount", tag_playcount
},
330 {"rating", tag_rating
},
331 {"lastplayed", tag_lastplayed
},
332 {"lastoffset", tag_lastoffset
},
333 {"commitid", tag_commitid
},
334 {"entryage", tag_virt_entryage
},
335 {"autoscore", tag_virt_autoscore
},
336 {"%sort", var_sorttype
},
337 {"%limit", var_limit
},
338 {"%strip", var_strip
},
339 {"%menu_start", var_menu_start
},
340 {"%include", var_include
},
341 {"%root_menu", var_rootmenu
},
342 {"%format", var_format
},
349 /* Find the start. */
350 while ((*strp
== ' ' || *strp
== '>') && *strp
!= '\0')
353 if (*strp
== '\0' || *strp
== '?')
356 for (i
= 0; i
< sizeof(buf
)-1; i
++)
358 if (*strp
== '\0' || *strp
== ' ')
365 for (i
= 0; i
< ARRAYLEN(get_tag_match
); i
++)
367 if (!strcasecmp(buf
, get_tag_match
[i
].str
))
369 *tag
= get_tag_match
[i
].symbol
;
374 logf("NO MATCH: %s\n", buf
);
381 static int get_clause(int *condition
)
383 static const struct match get_clause_match
[] =
387 {"!=", clause_is_not
},
392 {"~", clause_contains
},
393 {"!~", clause_not_contains
},
394 {"^", clause_begins_with
},
395 {"!^", clause_not_begins_with
},
396 {"$", clause_ends_with
},
397 {"!$", clause_not_ends_with
},
404 /* Find the start. */
405 while (*strp
== ' ' && *strp
!= '\0')
411 for (i
= 0; i
< sizeof(buf
)-1; i
++)
413 if (*strp
== '\0' || *strp
== ' ')
420 for (i
= 0; i
< ARRAYLEN(get_clause_match
); i
++)
422 if (!strcasecmp(buf
, get_clause_match
[i
].str
))
424 *condition
= get_clause_match
[i
].symbol
;
432 static bool read_clause(struct tagcache_search_clause
*clause
)
434 char buf
[SEARCHSTR_SIZE
];
437 if (get_tag(&clause
->tag
) <= 0)
440 if (get_clause(&clause
->type
) <= 0)
443 if (get_token_str(buf
, sizeof buf
) < 0)
446 for (i
=0; i
<ARRAYLEN(id3_to_search_mapping
); i
++)
448 if (!strcasecmp(buf
, id3_to_search_mapping
[i
].string
))
452 if (i
<ARRAYLEN(id3_to_search_mapping
)) /* runtime search operand found */
454 clause
->source
= source_runtime
+i
;
455 clause
->str
= tagtree_alloc(SEARCHSTR_SIZE
);
459 clause
->source
= source_constant
;
460 clause
->str
= tagtree_strdup(buf
);
463 if (TAGCACHE_IS_NUMERIC(clause
->tag
))
465 clause
->numeric
= true;
466 clause
->numeric_data
= atoi(clause
->str
);
469 clause
->numeric
= false;
471 logf("got clause: %d/%d [%s]", clause
->tag
, clause
->type
, clause
->str
);
476 static bool read_variable(char *buf
, int size
)
480 if (!get_clause(&condition
))
483 if (condition
!= clause_is
)
486 if (get_token_str(buf
, size
) < 0)
492 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
493 static int get_format_str(struct display_format
*fmt
)
499 memset(fmt
, 0, sizeof(struct display_format
));
501 if (get_token_str(fmt
->name
, sizeof fmt
->name
) < 0)
504 /* Determine the group id */
506 for (i
= 0; i
< format_count
; i
++)
508 if (!strcasecmp(formats
[i
]->name
, fmt
->name
))
510 fmt
->group_id
= formats
[i
]->group_id
;
514 if (formats
[i
]->group_id
> fmt
->group_id
)
515 fmt
->group_id
= formats
[i
]->group_id
;
518 if (i
== format_count
)
521 logf("format: (%d) %s", fmt
->group_id
, fmt
->name
);
523 if (get_token_str(buf
, sizeof buf
) < 0)
526 fmt
->formatstr
= tagtree_strdup(buf
);
528 while (fmt
->tag_count
< MAX_TAGS
)
530 ret
= get_tag(&fmt
->tags
[fmt
->tag_count
]);
537 switch (fmt
->tags
[fmt
->tag_count
]) {
539 if (!read_variable(buf
, sizeof buf
))
541 if (!strcasecmp("inverse", buf
))
542 fmt
->sort_inverse
= true;
546 if (!read_variable(buf
, sizeof buf
))
548 fmt
->limit
= atoi(buf
);
552 if (!read_variable(buf
, sizeof buf
))
554 fmt
->strip
= atoi(buf
);
565 static int add_format(const char *buf
)
567 if (format_count
>= TAGMENU_MAX_FMTS
)
569 logf("too many formats");
575 if (formats
[format_count
] == NULL
)
576 formats
[format_count
] = tagtree_alloc0(sizeof(struct display_format
));
578 if (get_format_str(formats
[format_count
]) < 0)
580 logf("get_format_str() parser failed!");
581 memset(formats
[format_count
], 0, sizeof(struct display_format
));
585 while (*strp
!= '\0' && *strp
!= '?')
590 int clause_count
= 0;
596 struct tagcache_search_clause
*newclause
;
598 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
600 logf("too many clauses");
604 newclause
= tagtree_alloc(sizeof(struct tagcache_search_clause
));
606 formats
[format_count
]->clause
[clause_count
] = newclause
;
607 if (!read_clause(newclause
))
614 formats
[format_count
]->clause_count
= clause_count
;
622 static int get_condition(struct search_instruction
*inst
)
624 struct tagcache_search_clause
*new_clause
;
634 if (get_token_str(buf
, sizeof buf
) < 0)
637 for (i
= 0; i
< format_count
; i
++)
639 if (!strcasecmp(formats
[i
]->name
, buf
))
643 if (i
== format_count
)
645 logf("format not found: %s", buf
);
649 inst
->format_id
[inst
->tagorder_count
] = formats
[i
]->group_id
;
662 clause_count
= inst
->clause_count
[inst
->tagorder_count
];
663 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
665 logf("Too many clauses");
669 new_clause
= tagtree_alloc(sizeof(struct tagcache_search_clause
));
670 inst
->clause
[inst
->tagorder_count
][clause_count
] = new_clause
;
675 new_clause
->type
= clause_logical_or
;
680 bool ret
= read_clause(new_clause
);
685 inst
->clause_count
[inst
->tagorder_count
]++;
691 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
692 * : album ? year >= "2000" : songs
698 static bool parse_search(struct menu_entry
*entry
, const char *str
)
702 struct search_instruction
*inst
= &entry
->si
;
708 /* Parse entry name */
709 if (get_token_str(entry
->name
, sizeof entry
->name
) < 0)
711 logf("No name found.");
715 /* Parse entry type */
716 if (get_tag(&entry
->type
) <= 0)
719 if (entry
->type
== menu_load
)
721 if (get_token_str(buf
, sizeof buf
) < 0)
724 /* Find the matching root menu or "create" it */
725 for (i
= 0; i
< menu_count
; i
++)
727 if (!strcasecmp(menus
[i
]->id
, buf
))
734 if (menu_count
>= TAGMENU_MAX_MENUS
)
736 logf("max menucount reached");
740 /* Allocate a new menu unless link is found. */
741 menus
[menu_count
] = tagtree_alloc0(sizeof(struct menu_root
));
742 strlcpy(menus
[menu_count
]->id
, buf
, MAX_MENU_ID_SIZE
);
743 entry
->link
= menu_count
;
749 if (entry
->type
!= menu_next
)
752 while (inst
->tagorder_count
< MAX_TAGS
)
754 ret
= get_tag(&inst
->tagorder
[inst
->tagorder_count
]);
757 logf("Parse error #1");
765 logf("tag: %d", inst
->tagorder
[inst
->tagorder_count
]);
768 while ( (ret
= get_condition(inst
)) > 0 ) ;
774 inst
->tagorder_count
++;
776 if (get_tag(&type
) <= 0 || type
!= menu_next
)
783 static int compare(const void *p1
, const void *p2
)
785 struct tagentry
*e1
= (struct tagentry
*)p1
;
786 struct tagentry
*e2
= (struct tagentry
*)p2
;
789 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
791 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
794 static void tagtree_buffer_event(void *data
)
796 struct tagcache_search tcs
;
797 struct mp3entry
*id3
= (struct mp3entry
*)data
;
799 /* Do not gather data unless proper setting has been enabled. */
800 if (!global_settings
.runtimedb
&& !global_settings
.autoresume_enable
)
803 logf("be:%s", id3
->path
);
805 while (! tagcache_is_fully_initialized())
808 if (!tagcache_find_index(&tcs
, id3
->path
))
810 logf("tc stat: not found: %s", id3
->path
);
814 if (global_settings
.runtimedb
)
816 id3
->playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
818 id3
->rating
= tagcache_get_numeric(&tcs
, tag_rating
);
819 id3
->lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
820 id3
->score
= tagcache_get_numeric(&tcs
, tag_virt_autoscore
) / 10;
821 id3
->playtime
= tagcache_get_numeric(&tcs
, tag_playtime
);
823 logf("-> %ld/%ld", id3
->playcount
, id3
->playtime
);
826 #if CONFIG_CODEC == SWCODEC
827 if (global_settings
.autoresume_enable
)
829 /* Load current file resume offset if not already defined (by
830 another resume mechanism) */
831 if (id3
->offset
== 0)
833 id3
->offset
= tagcache_get_numeric(&tcs
, tag_lastoffset
);
835 logf("tagtree_buffer_event: Set offset for %s to %lX\n",
836 str_or_empty(id3
->title
), id3
->offset
);
841 /* Store our tagcache index pointer. */
842 id3
->tagcache_idx
= tcs
.idx_id
+1;
844 tagcache_search_finish(&tcs
);
847 static void tagtree_track_finish_event(void *data
)
851 struct mp3entry
*id3
= (struct mp3entry
*)data
;
853 /* Do not gather data unless proper setting has been enabled. */
854 if (!global_settings
.runtimedb
&& !global_settings
.autoresume_enable
)
856 logf("runtimedb gathering and autoresume not enabled");
860 tagcache_idx
=id3
->tagcache_idx
;
863 logf("No tagcache index pointer found");
868 /* Don't process unplayed tracks, or tracks interrupted within the
870 if (id3
->elapsed
== 0
871 #if CONFIG_CODEC == SWCODEC /* HWCODEC doesn't have automatic_skip */
872 || (id3
->elapsed
< 15 * 1000 && !audio_automatic_skip())
876 logf("not logging unplayed or skipped track");
880 lastplayed
= tagcache_increase_serial();
883 logf("incorrect tc serial:%ld", lastplayed
);
887 if (global_settings
.runtimedb
)
892 playcount
= id3
->playcount
+ 1;
894 /* Ignore the last 15s (crossfade etc.) */
895 playtime
= id3
->playtime
+ MIN(id3
->length
, id3
->elapsed
+ 15 * 1000);
897 logf("ube:%s", id3
->path
);
898 logf("-> %ld/%ld", playcount
, playtime
);
899 logf("-> %ld/%ld/%ld", id3
->elapsed
, id3
->length
,
900 MIN(id3
->length
, id3
->elapsed
+ 15 * 1000));
902 /* Queue the updates to the tagcache system. */
903 tagcache_update_numeric(tagcache_idx
, tag_playcount
, playcount
);
904 tagcache_update_numeric(tagcache_idx
, tag_playtime
, playtime
);
905 tagcache_update_numeric(tagcache_idx
, tag_lastplayed
, lastplayed
);
908 #if CONFIG_CODEC == SWCODEC
909 if (global_settings
.autoresume_enable
)
912 = audio_automatic_skip() ? 0 : id3
->offset
;
914 tagcache_update_numeric(tagcache_idx
, tag_lastoffset
, offset
);
916 logf("tagtree_track_finish_event: Save offset for %s: %lX",
917 str_or_empty(id3
->title
), offset
);
922 bool tagtree_export(void)
924 struct tagcache_search tcs
;
926 splash(0, str(LANG_CREATING
));
927 if (!tagcache_create_changelog(&tcs
))
929 splash(HZ
*2, ID2P(LANG_FAILED
));
935 bool tagtree_import(void)
937 splash(0, ID2P(LANG_WAIT
));
938 if (!tagcache_import_changelog())
940 splash(HZ
*2, ID2P(LANG_FAILED
));
946 static bool parse_menu(const char *filename
);
948 static int parse_line(int n
, char *buf
, void *parameters
)
952 static bool read_menu
;
958 /* Strip possible <CR> at end of line. */
959 p
= strchr(buf
, '\r');
963 logf("parse:%d/%s", n
, buf
);
965 /* First line, do initialisation. */
968 if (strcasecmp(TAGNAVI_VERSION
, buf
))
970 logf("Version mismatch");
993 if (get_tag(&variable
) <= 0)
999 if (add_format(strp
) < 0)
1001 logf("Format add fail: %s", data
);
1006 if (get_token_str(data
, sizeof(data
)) < 0)
1008 logf("%%include empty");
1012 if (!parse_menu(data
))
1014 logf("Load menu fail: %s", data
);
1018 case var_menu_start
:
1019 if (menu_count
>= TAGMENU_MAX_MENUS
)
1021 logf("max menucount reached");
1025 if (get_token_str(data
, sizeof data
) < 0)
1027 logf("%%menu_start id empty");
1032 for (i
= 0; i
< menu_count
; i
++)
1034 if (!strcasecmp(menus
[i
]->id
, data
))
1042 menus
[menu_count
] = tagtree_alloc0(sizeof(struct menu_root
));
1043 menu
= menus
[menu_count
];
1045 strlcpy(menu
->id
, data
, MAX_MENU_ID_SIZE
);
1048 if (get_token_str(menu
->title
, sizeof(menu
->title
)) < 0)
1050 logf("%%menu_start title empty");
1053 logf("menu: %s", menu
->title
);
1058 /* Only set root menu once. */
1062 if (get_token_str(data
, sizeof(data
)) < 0)
1064 logf("%%rootmenu empty");
1068 for (i
= 0; i
< menu_count
; i
++)
1070 if (!strcasecmp(menus
[i
]->id
, data
))
1081 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
1083 logf("max itemcount reached");
1088 if (menu
->items
[menu
->itemcount
] == NULL
)
1089 menu
->items
[menu
->itemcount
] = tagtree_alloc0(sizeof(struct menu_entry
));
1092 if (parse_search(menu
->items
[menu
->itemcount
], buf
))
1099 static bool parse_menu(const char *filename
)
1104 if (menu_count
>= TAGMENU_MAX_MENUS
)
1106 logf("max menucount reached");
1110 fd
= open(filename
, O_RDONLY
);
1113 logf("Search instruction file not found.");
1117 /* Now read file for real, parsing into si */
1118 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
1124 void tagtree_init(void)
1130 tagtree_handle
= core_alloc_maximum("tagtree", &tagtree_bufsize
, &ops
);
1131 parse_menu(FILE_SEARCH_INSTRUCTIONS
);
1133 /* safety check since tree.c needs to cast tagentry to entry */
1134 if (sizeof(struct tagentry
) != sizeof(struct entry
))
1135 panicf("tagentry(%zu) and entry mismatch(%zu)",
1136 sizeof(struct tagentry
), sizeof(struct entry
));
1138 panicf("tagtree locked after parsing");
1140 /* If no root menu is set, assume it's the first single menu
1141 * we have. That shouldn't normally happen. */
1145 add_event(PLAYBACK_EVENT_TRACK_BUFFER
, false, tagtree_buffer_event
);
1146 add_event(PLAYBACK_EVENT_TRACK_FINISH
, false, tagtree_track_finish_event
);
1148 core_shrink(tagtree_handle
, core_get_data(tagtree_handle
), tagtree_buf_used
);
1151 static bool show_search_progress(bool init
, int count
)
1153 static int last_tick
= 0;
1155 /* Don't show splashes for 1/2 second after starting search */
1158 last_tick
= current_tick
+ HZ
/2;
1162 /* Update progress every 1/10 of a second */
1163 if (TIME_AFTER(current_tick
, last_tick
+ HZ
/10))
1165 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG
), count
, str(LANG_OFF_ABORT
));
1166 if (action_userabort(TIMEOUT_NOBLOCK
))
1168 last_tick
= current_tick
;
1175 static int format_str(struct tagcache_search
*tcs
, struct display_format
*fmt
,
1176 char *buf
, int buf_size
)
1179 bool read_format
= false;
1180 unsigned fmtbuf_pos
= 0;
1185 memset(buf
, 0, buf_size
);
1186 for (i
= 0; fmt
->formatstr
[i
] != '\0'; i
++)
1188 if (fmt
->formatstr
[i
] == '%')
1192 if (parpos
>= fmt
->tag_count
)
1194 logf("too many format tags");
1199 char formatchar
= fmt
->formatstr
[i
];
1203 fmtbuf
[fmtbuf_pos
++] = formatchar
;
1204 if (fmtbuf_pos
>= sizeof fmtbuf
)
1206 logf("format parse error");
1210 if (formatchar
== 's' || formatchar
== 'd')
1212 unsigned space_left
= buf_size
- buf_pos
;
1213 char tmpbuf
[MAX_PATH
];
1216 fmtbuf
[fmtbuf_pos
] = '\0';
1217 read_format
= false;
1222 if (fmt
->tags
[parpos
] == tcs
->type
)
1224 result
= tcs
->result
;
1228 /* Need to fetch the tag data. */
1229 int tag
= fmt
->tags
[parpos
];
1231 if (!tagcache_retrieve(tcs
, tcs
->idx_id
,
1232 (tag
== tag_virt_basename
?
1233 tag_filename
: tag
),
1234 tmpbuf
, sizeof tmpbuf
))
1236 logf("retrieve failed");
1240 if (tag
== tag_virt_basename
1241 && (result
= strrchr(tmpbuf
, '/')) != NULL
)
1249 snprintf(&buf
[buf_pos
], space_left
, fmtbuf
, result
);
1254 snprintf(&buf
[buf_pos
], space_left
, fmtbuf
,
1255 tagcache_get_numeric(tcs
, fmt
->tags
[parpos
]));
1262 buf
[buf_pos
++] = formatchar
;
1264 if (buf_pos
>= buf_size
- 1) /* need at least one more byte for \0 */
1266 logf("buffer overflow");
1271 buf
[buf_pos
++] = '\0';
1276 static struct tagentry
* get_entries(struct tree_context
*tc
)
1278 return core_get_data(tc
->cache
.entries_handle
);
1281 static int retrieve_entries(struct tree_context
*c
, int offset
, bool init
)
1283 struct tagcache_search tcs
;
1284 struct display_format
*fmt
;
1286 int namebufused
= 0;
1287 int total_count
= 0;
1288 int special_entry_count
= 0;
1289 int level
= c
->currextra
;
1295 /* Show search progress straight away if the disk needs to spin up,
1296 otherwise show it after the normal 1/2 second delay */
1297 show_search_progress(
1298 #ifdef HAVE_DISK_STORAGE
1299 storage_disk_is_active()
1305 if (c
->currtable
== ALLSUBENTRIES
)
1311 tag
= csi
->tagorder
[level
];
1313 if (!tagcache_search(&tcs
, tag
))
1316 /* Prevent duplicate entries in the search list. */
1317 tagcache_search_set_uniqbuf(&tcs
, uniqbuf
, UNIQBUF_SIZE
);
1319 if (level
|| csi
->clause_count
[0] || TAGCACHE_IS_NUMERIC(tag
))
1322 for (i
= 0; i
< level
; i
++)
1324 if (TAGCACHE_IS_NUMERIC(csi
->tagorder
[i
]))
1326 static struct tagcache_search_clause cc
;
1328 memset(&cc
, 0, sizeof(struct tagcache_search_clause
));
1329 cc
.tag
= csi
->tagorder
[i
];
1330 cc
.type
= clause_is
;
1332 cc
.numeric_data
= csi
->result_seek
[i
];
1333 tagcache_search_add_clause(&tcs
, &cc
);
1337 tagcache_search_add_filter(&tcs
, csi
->tagorder
[i
],
1338 csi
->result_seek
[i
]);
1342 /* because tagcache saves the clauses, we need to lock the buffer
1343 * for the entire duration of the search */
1345 for (i
= 0; i
<= level
; i
++)
1349 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1350 tagcache_search_add_clause(&tcs
, csi
->clause
[i
][j
]);
1353 current_offset
= offset
;
1354 current_entry_count
= 0;
1358 for (i
= 0; i
< format_count
; i
++)
1360 if (formats
[i
]->group_id
== csi
->format_id
[level
])
1366 sort_inverse
= fmt
->sort_inverse
;
1367 sort_limit
= fmt
->limit
;
1373 sort_inverse
= false;
1378 /* lock buflib out due to possible yields */
1380 struct tagentry
*dptr
= core_get_data(c
->cache
.entries_handle
);
1382 if (tag
!= tag_title
&& tag
!= tag_filename
)
1386 dptr
->newtable
= ALLSUBENTRIES
;
1387 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1389 current_entry_count
++;
1390 special_entry_count
++;
1394 dptr
->newtable
= NAVIBROWSE
;
1395 dptr
->name
= str(LANG_TAGNAVI_RANDOM
);
1396 dptr
->extraseek
= -1;
1398 current_entry_count
++;
1399 special_entry_count
++;
1405 while (tagcache_get_next(&tcs
))
1407 if (total_count
++ < offset
)
1410 dptr
->newtable
= NAVIBROWSE
;
1411 if (tag
== tag_title
|| tag
== tag_filename
)
1413 dptr
->newtable
= PLAYTRACK
;
1414 dptr
->extraseek
= tcs
.idx_id
;
1417 dptr
->extraseek
= tcs
.result_seek
;
1420 /* Check the format */
1422 for (i
= 0; i
< format_count
; i
++)
1424 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1427 if (tagcache_check_clauses(&tcs
, formats
[i
]->clause
,
1428 formats
[i
]->clause_count
))
1436 if (strcmp(tcs
.result
, UNTAGGED
) == 0)
1438 tcs
.result
= str(LANG_TAGNAVI_UNTAGGED
);
1439 tcs
.result_len
= strlen(tcs
.result
);
1440 tcs
.ramresult
= true;
1443 if (!tcs
.ramresult
|| fmt
)
1445 dptr
->name
= core_get_data(c
->cache
.name_buffer_handle
)+namebufused
;
1449 int ret
= format_str(&tcs
, fmt
, dptr
->name
,
1450 c
->cache
.name_buffer_size
- namebufused
);
1451 if (ret
== -4) /* buffer full */
1453 logf("chunk mode #2: %d", current_entry_count
);
1460 logf("format_str() failed");
1461 tagcache_search_finish(&tcs
);
1462 tree_unlock_cache(c
);
1467 namebufused
+= strlen(dptr
->name
)+1;
1471 namebufused
+= tcs
.result_len
;
1472 if (namebufused
< c
->cache
.name_buffer_size
)
1473 strcpy(dptr
->name
, tcs
.result
);
1476 logf("chunk mode #2a: %d", current_entry_count
);
1484 dptr
->name
= tcs
.result
;
1487 current_entry_count
++;
1489 if (current_entry_count
>= c
->cache
.max_entries
)
1491 logf("chunk mode #3: %d", current_entry_count
);
1497 if (init
&& !tcs
.ramsearch
)
1499 if (!show_search_progress(false, total_count
))
1500 { /* user aborted */
1501 tagcache_search_finish(&tcs
);
1502 tree_unlock_cache(c
);
1504 return current_entry_count
;
1511 struct tagentry
*entries
= get_entries(c
);
1512 qsort(&entries
[special_entry_count
],
1513 current_entry_count
- special_entry_count
,
1514 sizeof(struct tagentry
), compare
);
1519 tagcache_search_finish(&tcs
);
1520 tree_unlock_cache(c
);
1522 return current_entry_count
;
1525 while (tagcache_get_next(&tcs
))
1529 if (!show_search_progress(false, total_count
))
1535 tagcache_search_finish(&tcs
);
1536 tree_unlock_cache(c
);
1539 if (!sort
&& (sort_inverse
|| sort_limit
))
1541 splashf(HZ
*4, ID2P(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1542 logf("Too small dir buffer");
1547 total_count
= MIN(total_count
, sort_limit
);
1551 dptr
= get_entries(c
);
1552 for (i
= special_entry_count
; i
< current_entry_count
; i
++, dptr
++)
1554 int len
= strlen(dptr
->name
);
1559 dptr
->name
= &dptr
->name
[strip
];
1567 static int load_root(struct tree_context
*c
)
1569 struct tagentry
*dptr
= core_get_data(c
->cache
.entries_handle
);
1573 c
->currtable
= ROOT
;
1574 if (c
->dirlevel
== 0)
1575 c
->currextra
= rootmenu
;
1577 menu
= menus
[c
->currextra
];
1581 for (i
= 0; i
< menu
->itemcount
; i
++)
1583 dptr
->name
= menu
->items
[i
]->name
;
1584 switch (menu
->items
[i
]->type
)
1587 dptr
->newtable
= NAVIBROWSE
;
1588 dptr
->extraseek
= i
;
1592 dptr
->newtable
= ROOT
;
1593 dptr
->extraseek
= menu
->items
[i
]->link
;
1601 current_entry_count
= i
;
1606 int tagtree_load(struct tree_context
* c
)
1609 int table
= c
->currtable
;
1617 c
->currtable
= table
;
1618 c
->currextra
= rootmenu
;
1624 count
= load_root(c
);
1629 logf("navibrowse...");
1631 count
= retrieve_entries(c
, 0, true);
1636 logf("Unsupported table %d\n", table
);
1643 count
= load_root(c
);
1644 splash(HZ
, str(LANG_TAGCACHE_BUSY
));
1647 /* The _total_ numer of entries available. */
1648 c
->dirlength
= c
->filesindir
= count
;
1653 int tagtree_enter(struct tree_context
* c
)
1656 struct tagentry
*dptr
;
1657 struct mp3entry
*id3
;
1662 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1665 seek
= dptr
->extraseek
;
1668 if(c
->filesindir
<=2)
1670 srand(current_tick
);
1671 dptr
= (tagtree_get_entry(c
, 2+(rand() % (c
->filesindir
-2))));
1672 seek
= dptr
->extraseek
;
1674 newextra
= dptr
->newtable
;
1676 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1679 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1680 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1681 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1682 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1685 /* lock buflib for possible I/O to protect dptr */
1689 switch (c
->currtable
) {
1691 c
->currextra
= newextra
;
1693 if (newextra
== ROOT
)
1696 c
->currextra
= seek
;
1699 else if (newextra
== NAVIBROWSE
)
1703 csi
= &menu
->items
[seek
]->si
;
1706 strlcpy(current_title
[c
->currextra
], dptr
->name
,
1707 sizeof(current_title
[0]));
1709 /* Read input as necessary. */
1710 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1712 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1716 if (csi
->clause
[i
][j
]->type
== clause_logical_or
)
1719 source
= csi
->clause
[i
][j
]->source
;
1721 if (source
== source_constant
)
1724 searchstring
=csi
->clause
[i
][j
]->str
;
1725 *searchstring
= '\0';
1727 id3
= audio_current_track();
1729 if (source
== source_current_path
&& id3
)
1732 strlcpy(searchstring
, id3
->path
, SEARCHSTR_SIZE
);
1733 e
= strrchr(searchstring
, '/');
1737 else if (source
> source_runtime
&& id3
)
1740 int k
= source
-source_runtime
;
1741 int offset
= id3_to_search_mapping
[k
].id3_offset
;
1742 char **src
= (char**)((char*)id3
+ offset
);
1745 strlcpy(searchstring
, *src
, SEARCHSTR_SIZE
);
1750 rc
= kbd_input(searchstring
, SEARCHSTR_SIZE
);
1751 if (rc
< 0 || !searchstring
[0])
1754 tree_unlock_cache(c
);
1758 if (csi
->clause
[i
][j
]->numeric
)
1759 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1766 c
->currtable
= newextra
;
1772 if (newextra
== PLAYTRACK
)
1774 if (global_settings
.party_mode
&& audio_status()) {
1775 splash(HZ
, ID2P(LANG_PARTY_MODE
));
1779 /* about to create a new current playlist...
1780 allow user to cancel the operation */
1781 if (!warn_on_pl_erase())
1783 if (tagtree_play_folder(c
) >= 0)
1788 c
->currtable
= newextra
;
1789 csi
->result_seek
[c
->currextra
] = seek
;
1790 if (c
->currextra
< csi
->tagorder_count
-1)
1795 /* Update the statusbar title */
1796 strlcpy(current_title
[c
->currextra
], dptr
->name
,
1797 sizeof(current_title
[0]));
1807 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1808 tree_unlock_cache(c
);
1814 void tagtree_exit(struct tree_context
* c
)
1817 if (c
->dirlevel
> 0)
1819 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1820 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1821 c
->currtable
= c
->table_history
[c
->dirlevel
];
1822 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1823 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1826 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1828 struct tagcache_search tcs
;
1829 int extraseek
= tagtree_get_entry(c
, c
->selected_item
)->extraseek
;
1832 if (!tagcache_search(&tcs
, tag_filename
))
1835 if (!tagcache_retrieve(&tcs
, extraseek
, tcs
.type
, buf
, buflen
))
1837 tagcache_search_finish(&tcs
);
1841 tagcache_search_finish(&tcs
);
1846 static bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1848 struct tagcache_search tcs
;
1851 int from
, to
, direction
;
1852 int files_left
= c
->filesindir
;
1855 if (!tagcache_search(&tcs
, tag_filename
))
1857 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
1862 if (position
== PLAYLIST_REPLACE
)
1864 if (playlist_remove_all_tracks(NULL
) == 0)
1865 position
= PLAYLIST_INSERT_LAST
;
1873 if (position
== PLAYLIST_INSERT_FIRST
)
1875 from
= c
->filesindir
- 1;
1886 for (i
= from
; i
!= to
; i
+= direction
)
1888 /* Count back to zero */
1889 if (!show_search_progress(false, files_left
--))
1892 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1893 tcs
.type
, buf
, sizeof buf
))
1898 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1900 logf("playlist_insert_track failed");
1905 playlist_sync(NULL
);
1906 tagcache_search_finish(&tcs
);
1912 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1915 int dirlevel
= tc
->dirlevel
;
1918 show_search_progress(
1919 #ifdef HAVE_DISK_STORAGE
1920 storage_disk_is_active()
1927 /* We need to set the table to allsubentries. */
1928 newtable
= tagtree_get_entry(tc
, tc
->selected_item
)->newtable
;
1930 /* Insert a single track? */
1931 if (newtable
== PLAYTRACK
)
1933 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1935 logf("tagtree_get_filename failed");
1938 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1943 if (newtable
== NAVIBROWSE
)
1947 newtable
= tagtree_get_entry(tc
, tc
->selected_item
)->newtable
;
1949 else if (newtable
!= ALLSUBENTRIES
)
1951 logf("unsupported table: %d", newtable
);
1955 /* Now the current table should be allsubentries. */
1956 if (newtable
!= PLAYTRACK
)
1960 newtable
= tagtree_get_entry(tc
, tc
->selected_item
)->newtable
;
1962 /* And now the newtable should be playtrack. */
1963 if (newtable
!= PLAYTRACK
)
1965 logf("newtable: %d !!", newtable
);
1966 tc
->dirlevel
= dirlevel
;
1971 if (tc
->filesindir
<= 0)
1972 splash(HZ
, ID2P(LANG_END_PLAYLIST
));
1975 logf("insert_all_playlist");
1976 if (!insert_all_playlist(tc
, position
, queue
))
1977 splash(HZ
*2, ID2P(LANG_FAILED
));
1980 /* Finally return the dirlevel to its original value. */
1981 while (tc
->dirlevel
> dirlevel
)
1988 static int tagtree_play_folder(struct tree_context
* c
)
1990 if (playlist_create(NULL
, NULL
) < 0)
1992 logf("Failed creating playlist\n");
1996 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1999 if (global_settings
.playlist_shuffle
)
2000 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
2001 if (!global_settings
.play_selected
)
2002 c
->selected_item
= 0;
2003 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
2005 playlist_start(c
->selected_item
,0);
2006 playlist_get_current()->num_inserted_tracks
= 0; /* make warn on playlist erase work */
2010 static struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
2012 struct tagentry
*entry
;
2013 int realid
= id
- current_offset
;
2015 /* Load the next chunk if necessary. */
2016 if (realid
>= current_entry_count
|| realid
< 0)
2019 if (retrieve_entries(c
, MAX(0, id
- (current_entry_count
/ 2)),
2022 logf("retrieve failed");
2026 realid
= id
- current_offset
;
2030 entry
= get_entries(c
);
2031 return &entry
[realid
];
2034 char* tagtree_get_entry_name(struct tree_context
*c
, int id
,
2035 char* buf
, size_t bufsize
)
2037 struct tagentry
*entry
= tagtree_get_entry(c
, id
);
2040 strlcpy(buf
, entry
->name
, bufsize
);
2045 char *tagtree_get_title(struct tree_context
* c
)
2047 switch (c
->currtable
)
2054 return current_title
[c
->currextra
];
2060 int tagtree_get_attr(struct tree_context
* c
)
2063 switch (c
->currtable
)
2066 if (csi
->tagorder
[c
->currextra
] == tag_title
)
2067 attr
= FILE_ATTR_AUDIO
;
2069 attr
= ATTR_DIRECTORY
;
2073 attr
= FILE_ATTR_AUDIO
;
2077 attr
= ATTR_DIRECTORY
;
2084 int tagtree_get_icon(struct tree_context
* c
)
2086 int icon
= Icon_Folder
;
2088 if (tagtree_get_attr(c
) == FILE_ATTR_AUDIO
)