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 {"tracknum", tag_tracknumber
},
242 {"discnum", tag_discnumber
},
244 {"playcount", tag_playcount
},
245 {"rating", tag_rating
},
246 {"lastplayed", tag_lastplayed
},
247 {"lastoffset", tag_lastoffset
},
248 {"commitid", tag_commitid
},
249 {"entryage", tag_virt_entryage
},
250 {"autoscore", tag_virt_autoscore
},
251 {"%sort", var_sorttype
},
252 {"%limit", var_limit
},
253 {"%strip", var_strip
},
254 {"%menu_start", var_menu_start
},
255 {"%include", var_include
},
256 {"%root_menu", var_rootmenu
},
257 {"%format", var_format
},
264 /* Find the start. */
265 while ((*strp
== ' ' || *strp
== '>') && *strp
!= '\0')
268 if (*strp
== '\0' || *strp
== '?')
271 for (i
= 0; i
< sizeof(buf
)-1; i
++)
273 if (*strp
== '\0' || *strp
== ' ')
280 for (i
= 0; i
< ARRAYLEN(get_tag_match
); i
++)
282 if (!strcasecmp(buf
, get_tag_match
[i
].str
))
284 *tag
= get_tag_match
[i
].symbol
;
289 logf("NO MATCH: %s\n", buf
);
296 static int get_clause(int *condition
)
298 static const struct match get_clause_match
[] =
302 {"!=", clause_is_not
},
307 {"~", clause_contains
},
308 {"!~", clause_not_contains
},
309 {"^", clause_begins_with
},
310 {"!^", clause_not_begins_with
},
311 {"$", clause_ends_with
},
312 {"!$", clause_not_ends_with
},
319 /* Find the start. */
320 while (*strp
== ' ' && *strp
!= '\0')
326 for (i
= 0; i
< sizeof(buf
)-1; i
++)
328 if (*strp
== '\0' || *strp
== ' ')
335 for (i
= 0; i
< ARRAYLEN(get_clause_match
); i
++)
337 if (!strcasecmp(buf
, get_clause_match
[i
].str
))
339 *condition
= get_clause_match
[i
].symbol
;
347 static bool read_clause(struct tagcache_search_clause
*clause
)
349 char buf
[SEARCHSTR_SIZE
];
352 if (get_tag(&clause
->tag
) <= 0)
355 if (get_clause(&clause
->type
) <= 0)
358 if (get_token_str(buf
, sizeof buf
) < 0)
361 for (i
=0; i
<ARRAYLEN(id3_to_search_mapping
); i
++)
363 if (!strcasecmp(buf
, id3_to_search_mapping
[i
].string
))
367 if (i
<ARRAYLEN(id3_to_search_mapping
)) /* runtime search operand found */
369 clause
->source
= source_runtime
+i
;
370 clause
->str
= tagtree_alloc(SEARCHSTR_SIZE
);
374 clause
->source
= source_constant
;
375 clause
->str
= tagtree_strdup(buf
);
378 if (TAGCACHE_IS_NUMERIC(clause
->tag
))
380 clause
->numeric
= true;
381 clause
->numeric_data
= atoi(clause
->str
);
384 clause
->numeric
= false;
386 logf("got clause: %d/%d [%s]", clause
->tag
, clause
->type
, clause
->str
);
391 static bool read_variable(char *buf
, int size
)
395 if (!get_clause(&condition
))
398 if (condition
!= clause_is
)
401 if (get_token_str(buf
, size
) < 0)
407 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
408 static int get_format_str(struct display_format
*fmt
)
414 memset(fmt
, 0, sizeof(struct display_format
));
416 if (get_token_str(fmt
->name
, sizeof fmt
->name
) < 0)
419 /* Determine the group id */
421 for (i
= 0; i
< format_count
; i
++)
423 if (!strcasecmp(formats
[i
]->name
, fmt
->name
))
425 fmt
->group_id
= formats
[i
]->group_id
;
429 if (formats
[i
]->group_id
> fmt
->group_id
)
430 fmt
->group_id
= formats
[i
]->group_id
;
433 if (i
== format_count
)
436 logf("format: (%d) %s", fmt
->group_id
, fmt
->name
);
438 if (get_token_str(buf
, sizeof buf
) < 0)
441 fmt
->formatstr
= tagtree_strdup(buf
);
443 while (fmt
->tag_count
< MAX_TAGS
)
445 ret
= get_tag(&fmt
->tags
[fmt
->tag_count
]);
452 switch (fmt
->tags
[fmt
->tag_count
]) {
454 if (!read_variable(buf
, sizeof buf
))
456 if (!strcasecmp("inverse", buf
))
457 fmt
->sort_inverse
= true;
461 if (!read_variable(buf
, sizeof buf
))
463 fmt
->limit
= atoi(buf
);
467 if (!read_variable(buf
, sizeof buf
))
469 fmt
->strip
= atoi(buf
);
480 static int add_format(const char *buf
)
482 if (format_count
>= TAGMENU_MAX_FMTS
)
484 logf("too many formats");
490 if (formats
[format_count
] == NULL
)
491 formats
[format_count
] = tagtree_alloc0(sizeof(struct display_format
));
493 if (get_format_str(formats
[format_count
]) < 0)
495 logf("get_format_str() parser failed!");
496 memset(formats
[format_count
], 0, sizeof(struct display_format
));
500 while (*strp
!= '\0' && *strp
!= '?')
505 int clause_count
= 0;
510 struct tagcache_search_clause
*newclause
;
512 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
514 logf("too many clauses");
518 newclause
= tagtree_alloc(sizeof(struct tagcache_search_clause
));
520 formats
[format_count
]->clause
[clause_count
] = newclause
;
521 if (!read_clause(newclause
))
527 formats
[format_count
]->clause_count
= clause_count
;
535 static int get_condition(struct search_instruction
*inst
)
537 struct tagcache_search_clause
*new_clause
;
547 if (get_token_str(buf
, sizeof buf
) < 0)
550 for (i
= 0; i
< format_count
; i
++)
552 if (!strcasecmp(formats
[i
]->name
, buf
))
556 if (i
== format_count
)
558 logf("format not found: %s", buf
);
562 inst
->format_id
[inst
->tagorder_count
] = formats
[i
]->group_id
;
575 clause_count
= inst
->clause_count
[inst
->tagorder_count
];
576 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
578 logf("Too many clauses");
582 new_clause
= tagtree_alloc(sizeof(struct tagcache_search_clause
));
583 inst
->clause
[inst
->tagorder_count
][clause_count
] = new_clause
;
588 new_clause
->type
= clause_logical_or
;
590 else if (!read_clause(new_clause
))
593 inst
->clause_count
[inst
->tagorder_count
]++;
599 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
600 * : album ? year >= "2000" : songs
606 static bool parse_search(struct menu_entry
*entry
, const char *str
)
610 struct search_instruction
*inst
= &entry
->si
;
613 struct menu_root
*new_menu
;
617 /* Parse entry name */
618 if (get_token_str(entry
->name
, sizeof entry
->name
) < 0)
620 logf("No name found.");
624 /* Parse entry type */
625 if (get_tag(&entry
->type
) <= 0)
628 if (entry
->type
== menu_load
)
630 if (get_token_str(buf
, sizeof buf
) < 0)
633 /* Find the matching root menu or "create" it */
634 for (i
= 0; i
< menu_count
; i
++)
636 if (!strcasecmp(menus
[i
]->id
, buf
))
643 if (menu_count
>= TAGMENU_MAX_MENUS
)
645 logf("max menucount reached");
649 /* Allocate a new menu unless link is found. */
650 menus
[menu_count
] = tagtree_alloc0(sizeof(struct menu_root
));
651 new_menu
= menus
[menu_count
];
652 strlcpy(new_menu
->id
, buf
, MAX_MENU_ID_SIZE
);
653 entry
->link
= menu_count
;
659 if (entry
->type
!= menu_next
)
662 while (inst
->tagorder_count
< MAX_TAGS
)
664 ret
= get_tag(&inst
->tagorder
[inst
->tagorder_count
]);
667 logf("Parse error #1");
675 logf("tag: %d", inst
->tagorder
[inst
->tagorder_count
]);
677 while ( (ret
= get_condition(inst
)) > 0 ) ;
681 inst
->tagorder_count
++;
683 if (get_tag(&type
) <= 0 || type
!= menu_next
)
690 static int compare(const void *p1
, const void *p2
)
692 struct tagentry
*e1
= (struct tagentry
*)p1
;
693 struct tagentry
*e2
= (struct tagentry
*)p2
;
696 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
698 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
701 static void tagtree_buffer_event(void *data
)
703 struct tagcache_search tcs
;
704 struct mp3entry
*id3
= (struct mp3entry
*)data
;
706 /* Do not gather data unless proper setting has been enabled. */
707 if (!global_settings
.runtimedb
&& !global_settings
.autoresume_enable
)
710 logf("be:%s", id3
->path
);
712 while (! tagcache_is_fully_initialized())
715 if (!tagcache_find_index(&tcs
, id3
->path
))
717 logf("tc stat: not found: %s", id3
->path
);
721 if (global_settings
.runtimedb
)
723 id3
->playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
725 id3
->rating
= tagcache_get_numeric(&tcs
, tag_rating
);
726 id3
->lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
727 id3
->score
= tagcache_get_numeric(&tcs
, tag_virt_autoscore
) / 10;
728 id3
->playtime
= tagcache_get_numeric(&tcs
, tag_playtime
);
730 logf("-> %ld/%ld", id3
->playcount
, id3
->playtime
);
733 #if CONFIG_CODEC == SWCODEC
734 if (global_settings
.autoresume_enable
)
736 /* Load current file resume offset if not already defined (by
737 another resume mechanism) */
738 if (id3
->offset
== 0)
740 id3
->offset
= tagcache_get_numeric(&tcs
, tag_lastoffset
);
742 logf("tagtree_buffer_event: Set offset for %s to %lX\n",
743 str_or_empty(id3
->title
), id3
->offset
);
748 /* Store our tagcache index pointer. */
749 id3
->tagcache_idx
= tcs
.idx_id
+1;
751 tagcache_search_finish(&tcs
);
754 static void tagtree_track_finish_event(void *data
)
758 struct mp3entry
*id3
= (struct mp3entry
*)data
;
760 /* Do not gather data unless proper setting has been enabled. */
761 if (!global_settings
.runtimedb
&& !global_settings
.autoresume_enable
)
763 logf("runtimedb gathering and autoresume not enabled");
767 tagcache_idx
=id3
->tagcache_idx
;
770 logf("No tagcache index pointer found");
775 /* Don't process unplayed tracks, or tracks interrupted within the
777 if (id3
->elapsed
== 0
778 #if CONFIG_CODEC == SWCODEC /* HWCODEC doesn't have automatic_skip */
779 || (id3
->elapsed
< 15 * 1000 && !audio_automatic_skip())
783 logf("not logging unplayed or skipped track");
787 lastplayed
= tagcache_increase_serial();
790 logf("incorrect tc serial:%ld", lastplayed
);
794 if (global_settings
.runtimedb
)
799 playcount
= id3
->playcount
+ 1;
801 /* Ignore the last 15s (crossfade etc.) */
802 playtime
= id3
->playtime
+ MIN(id3
->length
, id3
->elapsed
+ 15 * 1000);
804 logf("ube:%s", id3
->path
);
805 logf("-> %ld/%ld", playcount
, playtime
);
806 logf("-> %ld/%ld/%ld", id3
->elapsed
, id3
->length
,
807 MIN(id3
->length
, id3
->elapsed
+ 15 * 1000));
809 /* Queue the updates to the tagcache system. */
810 tagcache_update_numeric(tagcache_idx
, tag_playcount
, playcount
);
811 tagcache_update_numeric(tagcache_idx
, tag_playtime
, playtime
);
812 tagcache_update_numeric(tagcache_idx
, tag_lastplayed
, lastplayed
);
815 #if CONFIG_CODEC == SWCODEC
816 if (global_settings
.autoresume_enable
)
819 = audio_automatic_skip() ? 0 : id3
->offset
;
821 tagcache_update_numeric(tagcache_idx
, tag_lastoffset
, offset
);
823 logf("tagtree_track_finish_event: Save offset for %s: %lX",
824 str_or_empty(id3
->title
), offset
);
829 bool tagtree_export(void)
831 struct tagcache_search tcs
;
833 splash(0, str(LANG_CREATING
));
834 if (!tagcache_create_changelog(&tcs
))
836 splash(HZ
*2, ID2P(LANG_FAILED
));
842 bool tagtree_import(void)
844 splash(0, ID2P(LANG_WAIT
));
845 if (!tagcache_import_changelog())
847 splash(HZ
*2, ID2P(LANG_FAILED
));
853 static bool parse_menu(const char *filename
);
855 static int parse_line(int n
, char *buf
, void *parameters
)
859 static bool read_menu
;
865 /* Strip possible <CR> at end of line. */
866 p
= strchr(buf
, '\r');
870 logf("parse:%d/%s", n
, buf
);
872 /* First line, do initialisation. */
875 if (strcasecmp(TAGNAVI_VERSION
, buf
))
877 logf("Version mismatch");
900 if (get_tag(&variable
) <= 0)
906 if (add_format(strp
) < 0)
908 logf("Format add fail: %s", data
);
913 if (get_token_str(data
, sizeof(data
)) < 0)
915 logf("%%include empty");
919 if (!parse_menu(data
))
921 logf("Load menu fail: %s", data
);
926 if (menu_count
>= TAGMENU_MAX_MENUS
)
928 logf("max menucount reached");
932 if (get_token_str(data
, sizeof data
) < 0)
934 logf("%%menu_start id empty");
939 for (i
= 0; i
< menu_count
; i
++)
941 if (!strcasecmp(menus
[i
]->id
, data
))
949 menus
[menu_count
] = tagtree_alloc0(sizeof(struct menu_root
));
950 menu
= menus
[menu_count
];
952 strlcpy(menu
->id
, data
, MAX_MENU_ID_SIZE
);
955 if (get_token_str(menu
->title
, sizeof(menu
->title
)) < 0)
957 logf("%%menu_start title empty");
960 logf("menu: %s", menu
->title
);
965 /* Only set root menu once. */
969 if (get_token_str(data
, sizeof(data
)) < 0)
971 logf("%%rootmenu empty");
975 for (i
= 0; i
< menu_count
; i
++)
977 if (!strcasecmp(menus
[i
]->id
, data
))
988 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
990 logf("max itemcount reached");
995 if (menu
->items
[menu
->itemcount
] == NULL
)
996 menu
->items
[menu
->itemcount
] = tagtree_alloc0(sizeof(struct menu_entry
));
998 if (!parse_search(menu
->items
[menu
->itemcount
], buf
))
1006 static bool parse_menu(const char *filename
)
1011 if (menu_count
>= TAGMENU_MAX_MENUS
)
1013 logf("max menucount reached");
1017 fd
= open(filename
, O_RDONLY
);
1020 logf("Search instruction file not found.");
1024 /* Now read file for real, parsing into si */
1025 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
1031 void tagtree_init(void)
1037 parse_menu(FILE_SEARCH_INSTRUCTIONS
);
1039 /* If no root menu is set, assume it's the first single menu
1040 * we have. That shouldn't normally happen. */
1044 uniqbuf
= tagtree_alloc(UNIQBUF_SIZE
);
1046 add_event(PLAYBACK_EVENT_TRACK_BUFFER
, false, tagtree_buffer_event
);
1047 add_event(PLAYBACK_EVENT_TRACK_FINISH
, false, tagtree_track_finish_event
);
1050 static bool show_search_progress(bool init
, int count
)
1052 static int last_tick
= 0;
1054 /* Don't show splashes for 1/2 second after starting search */
1057 last_tick
= current_tick
+ HZ
/2;
1061 /* Update progress every 1/10 of a second */
1062 if (TIME_AFTER(current_tick
, last_tick
+ HZ
/10))
1064 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG
), count
, str(LANG_OFF_ABORT
));
1065 if (action_userabort(TIMEOUT_NOBLOCK
))
1067 last_tick
= current_tick
;
1074 static int format_str(struct tagcache_search
*tcs
, struct display_format
*fmt
,
1075 char *buf
, int buf_size
)
1078 bool read_format
= false;
1084 memset(buf
, 0, buf_size
);
1085 for (i
= 0; fmt
->formatstr
[i
] != '\0'; i
++)
1087 if (fmt
->formatstr
[i
] == '%')
1091 if (parpos
>= fmt
->tag_count
)
1093 logf("too many format tags");
1100 fmtbuf
[fmtbuf_pos
++] = fmt
->formatstr
[i
];
1101 if (fmtbuf_pos
>= buf_size
)
1103 logf("format parse error");
1107 if (fmt
->formatstr
[i
] == 's')
1109 fmtbuf
[fmtbuf_pos
] = '\0';
1110 read_format
= false;
1111 if (fmt
->tags
[parpos
] == tcs
->type
)
1113 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
, tcs
->result
);
1117 /* Need to fetch the tag data. */
1118 if (!tagcache_retrieve(tcs
, tcs
->idx_id
, fmt
->tags
[parpos
],
1119 &buf
[buf_pos
], buf_size
- buf_pos
))
1121 logf("retrieve failed");
1125 buf_pos
+= strlen(&buf
[buf_pos
]);
1128 else if (fmt
->formatstr
[i
] == 'd')
1130 fmtbuf
[fmtbuf_pos
] = '\0';
1131 read_format
= false;
1132 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
,
1133 tagcache_get_numeric(tcs
, fmt
->tags
[parpos
]));
1134 buf_pos
+= strlen(&buf
[buf_pos
]);
1140 buf
[buf_pos
++] = fmt
->formatstr
[i
];
1142 if (buf_pos
- 1 >= buf_size
)
1144 logf("buffer overflow");
1149 buf
[buf_pos
++] = '\0';
1154 static int retrieve_entries(struct tree_context
*c
, int offset
, bool init
)
1156 struct tagcache_search tcs
;
1157 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1158 struct display_format
*fmt
;
1160 int namebufused
= 0;
1161 int total_count
= 0;
1162 int special_entry_count
= 0;
1163 int level
= c
->currextra
;
1169 /* Show search progress straight away if the disk needs to spin up,
1170 otherwise show it after the normal 1/2 second delay */
1171 show_search_progress(
1172 #ifdef HAVE_DISK_STORAGE
1173 storage_disk_is_active()
1179 if (c
->currtable
== ALLSUBENTRIES
)
1185 tag
= csi
->tagorder
[level
];
1187 if (!tagcache_search(&tcs
, tag
))
1190 /* Prevent duplicate entries in the search list. */
1191 tagcache_search_set_uniqbuf(&tcs
, uniqbuf
, UNIQBUF_SIZE
);
1193 if (level
|| csi
->clause_count
[0] || TAGCACHE_IS_NUMERIC(tag
))
1196 for (i
= 0; i
< level
; i
++)
1198 if (TAGCACHE_IS_NUMERIC(csi
->tagorder
[i
]))
1200 static struct tagcache_search_clause cc
;
1202 memset(&cc
, 0, sizeof(struct tagcache_search_clause
));
1203 cc
.tag
= csi
->tagorder
[i
];
1204 cc
.type
= clause_is
;
1206 cc
.numeric_data
= csi
->result_seek
[i
];
1207 tagcache_search_add_clause(&tcs
, &cc
);
1211 tagcache_search_add_filter(&tcs
, csi
->tagorder
[i
],
1212 csi
->result_seek
[i
]);
1216 for (i
= 0; i
<= level
; i
++)
1220 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1221 tagcache_search_add_clause(&tcs
, csi
->clause
[i
][j
]);
1224 current_offset
= offset
;
1225 current_entry_count
= 0;
1229 for (i
= 0; i
< format_count
; i
++)
1231 if (formats
[i
]->group_id
== csi
->format_id
[level
])
1237 sort_inverse
= fmt
->sort_inverse
;
1238 sort_limit
= fmt
->limit
;
1244 sort_inverse
= false;
1249 if (tag
!= tag_title
&& tag
!= tag_filename
)
1253 dptr
->newtable
= ALLSUBENTRIES
;
1254 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1256 current_entry_count
++;
1260 dptr
->newtable
= NAVIBROWSE
;
1261 dptr
->name
= str(LANG_TAGNAVI_RANDOM
);
1262 dptr
->extraseek
= -1;
1264 current_entry_count
++;
1266 special_entry_count
+=2;
1269 total_count
+= special_entry_count
;
1271 while (tagcache_get_next(&tcs
))
1273 if (total_count
++ < offset
)
1276 dptr
->newtable
= NAVIBROWSE
;
1277 if (tag
== tag_title
|| tag
== tag_filename
)
1279 dptr
->newtable
= PLAYTRACK
;
1280 dptr
->extraseek
= tcs
.idx_id
;
1283 dptr
->extraseek
= tcs
.result_seek
;
1286 /* Check the format */
1287 for (i
= 0; i
< format_count
; i
++)
1289 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1292 if (tagcache_check_clauses(&tcs
, formats
[i
]->clause
,
1293 formats
[i
]->clause_count
))
1300 if (strcmp(tcs
.result
, UNTAGGED
) == 0)
1302 tcs
.result
= str(LANG_TAGNAVI_UNTAGGED
);
1303 tcs
.result_len
= strlen(tcs
.result
);
1304 tcs
.ramresult
= true;
1307 if (!tcs
.ramresult
|| fmt
)
1313 if (format_str(&tcs
, fmt
, buf
, sizeof buf
) < 0)
1315 logf("format_str() failed");
1316 tagcache_search_finish(&tcs
);
1321 dptr
->name
= &c
->name_buffer
[namebufused
];
1323 namebufused
+= strlen(buf
)+1;
1325 namebufused
+= tcs
.result_len
;
1327 if (namebufused
>= c
->name_buffer_size
)
1329 logf("chunk mode #2: %d", current_entry_count
);
1335 strcpy(dptr
->name
, buf
);
1337 strcpy(dptr
->name
, tcs
.result
);
1340 dptr
->name
= tcs
.result
;
1343 current_entry_count
++;
1345 if (current_entry_count
>= c
->dircache_count
)
1347 logf("chunk mode #3: %d", current_entry_count
);
1353 if (init
&& !tcs
.ramsearch
)
1355 if (!show_search_progress(false, total_count
))
1356 { /* user aborted */
1357 tagcache_search_finish(&tcs
);
1358 return current_entry_count
;
1364 qsort(c
->dircache
+ special_entry_count
* c
->dentry_size
,
1365 current_entry_count
- special_entry_count
,
1366 c
->dentry_size
, compare
);
1370 tagcache_search_finish(&tcs
);
1371 return current_entry_count
;
1374 while (tagcache_get_next(&tcs
))
1378 if (!show_search_progress(false, total_count
))
1384 tagcache_search_finish(&tcs
);
1386 if (!sort
&& (sort_inverse
|| sort_limit
))
1388 splashf(HZ
*4, ID2P(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1389 logf("Too small dir buffer");
1394 total_count
= MIN(total_count
, sort_limit
);
1399 for (i
= 0; i
< total_count
; i
++, dptr
++)
1401 int len
= strlen(dptr
->name
);
1406 dptr
->name
= &dptr
->name
[strip
];
1414 static int load_root(struct tree_context
*c
)
1416 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1420 c
->currtable
= ROOT
;
1421 if (c
->dirlevel
== 0)
1422 c
->currextra
= rootmenu
;
1424 menu
= menus
[c
->currextra
];
1428 for (i
= 0; i
< menu
->itemcount
; i
++)
1430 dptr
->name
= menu
->items
[i
]->name
;
1431 switch (menu
->items
[i
]->type
)
1434 dptr
->newtable
= NAVIBROWSE
;
1435 dptr
->extraseek
= i
;
1439 dptr
->newtable
= ROOT
;
1440 dptr
->extraseek
= menu
->items
[i
]->link
;
1448 current_entry_count
= i
;
1453 int tagtree_load(struct tree_context
* c
)
1456 int table
= c
->currtable
;
1458 c
->dentry_size
= sizeof(struct tagentry
);
1465 c
->currtable
= table
;
1466 c
->currextra
= rootmenu
;
1472 count
= load_root(c
);
1477 logf("navibrowse...");
1479 count
= retrieve_entries(c
, 0, true);
1484 logf("Unsupported table %d\n", table
);
1491 count
= load_root(c
);
1492 splash(HZ
, str(LANG_TAGCACHE_BUSY
));
1495 /* The _total_ numer of entries available. */
1496 c
->dirlength
= c
->filesindir
= count
;
1501 int tagtree_enter(struct tree_context
* c
)
1504 struct tagentry
*dptr
;
1505 struct mp3entry
*id3
;
1510 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1513 seek
= dptr
->extraseek
;
1516 if(c
->filesindir
<=2)
1518 srand(current_tick
);
1519 dptr
= (tagtree_get_entry(c
, 2+(rand() % (c
->filesindir
-2))));
1520 seek
= dptr
->extraseek
;
1522 newextra
= dptr
->newtable
;
1524 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1527 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1528 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1529 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1530 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1533 switch (c
->currtable
) {
1535 c
->currextra
= newextra
;
1537 if (newextra
== ROOT
)
1540 c
->currextra
= seek
;
1543 else if (newextra
== NAVIBROWSE
)
1547 csi
= &menu
->items
[seek
]->si
;
1550 strlcpy(current_title
[c
->currextra
], dptr
->name
,
1551 sizeof(current_title
[0]));
1553 /* Read input as necessary. */
1554 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1556 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1560 if (csi
->clause
[i
][j
]->type
== clause_logical_or
)
1563 source
= csi
->clause
[i
][j
]->source
;
1565 if (source
== source_constant
)
1568 searchstring
=csi
->clause
[i
][j
]->str
;
1569 *searchstring
= '\0';
1571 id3
= audio_current_track();
1573 if (source
== source_current_path
&& id3
)
1576 strlcpy(searchstring
, id3
->path
, SEARCHSTR_SIZE
);
1577 e
= strrchr(searchstring
, '/');
1581 else if (source
> source_runtime
&& id3
)
1584 int k
= source
-source_runtime
;
1585 int offset
= id3_to_search_mapping
[k
].id3_offset
;
1586 char **src
= (char**)((char*)id3
+ offset
);
1589 strlcpy(searchstring
, *src
, SEARCHSTR_SIZE
);
1594 rc
= kbd_input(searchstring
, SEARCHSTR_SIZE
);
1595 if (rc
< 0 || !searchstring
[0])
1600 if (csi
->clause
[i
][j
]->numeric
)
1601 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1608 c
->currtable
= newextra
;
1614 if (newextra
== PLAYTRACK
)
1616 if (global_settings
.party_mode
&& audio_status()) {
1617 splash(HZ
, ID2P(LANG_PARTY_MODE
));
1621 /* about to create a new current playlist...
1622 allow user to cancel the operation */
1623 if (!warn_on_pl_erase())
1625 if (tagtree_play_folder(c
) >= 0)
1630 c
->currtable
= newextra
;
1631 csi
->result_seek
[c
->currextra
] = seek
;
1632 if (c
->currextra
< csi
->tagorder_count
-1)
1637 /* Update the statusbar title */
1638 strlcpy(current_title
[c
->currextra
], dptr
->name
,
1639 sizeof(current_title
[0]));
1648 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1653 void tagtree_exit(struct tree_context
* c
)
1656 if (c
->dirlevel
> 0)
1658 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1659 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1660 c
->currtable
= c
->table_history
[c
->dirlevel
];
1661 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1662 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1665 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1667 struct tagcache_search tcs
;
1668 struct tagentry
*entry
;
1670 entry
= tagtree_get_entry(c
, c
->selected_item
);
1672 if (!tagcache_search(&tcs
, tag_filename
))
1675 if (!tagcache_retrieve(&tcs
, entry
->extraseek
, tcs
.type
, buf
, buflen
))
1677 tagcache_search_finish(&tcs
);
1681 tagcache_search_finish(&tcs
);
1686 static bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1688 struct tagcache_search tcs
;
1691 int from
, to
, direction
;
1692 int files_left
= c
->filesindir
;
1695 if (!tagcache_search(&tcs
, tag_filename
))
1697 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
1702 if (position
== PLAYLIST_REPLACE
)
1704 if (playlist_remove_all_tracks(NULL
) == 0)
1705 position
= PLAYLIST_INSERT_LAST
;
1713 if (position
== PLAYLIST_INSERT_FIRST
)
1715 from
= c
->filesindir
- 1;
1726 for (i
= from
; i
!= to
; i
+= direction
)
1728 /* Count back to zero */
1729 if (!show_search_progress(false, files_left
--))
1732 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1733 tcs
.type
, buf
, sizeof buf
))
1738 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1740 logf("playlist_insert_track failed");
1745 playlist_sync(NULL
);
1746 tagcache_search_finish(&tcs
);
1752 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1754 struct tagentry
*dptr
;
1756 int dirlevel
= tc
->dirlevel
;
1758 show_search_progress(
1759 #ifdef HAVE_DISK_STORAGE
1760 storage_disk_is_active()
1767 /* We need to set the table to allsubentries. */
1768 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1770 /* Insert a single track? */
1771 if (dptr
->newtable
== PLAYTRACK
)
1773 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1775 logf("tagtree_get_filename failed");
1778 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1783 if (dptr
->newtable
== NAVIBROWSE
)
1787 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1789 else if (dptr
->newtable
!= ALLSUBENTRIES
)
1791 logf("unsupported table: %d", dptr
->newtable
);
1795 /* Now the current table should be allsubentries. */
1796 if (dptr
->newtable
!= PLAYTRACK
)
1800 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1802 /* And now the newtable should be playtrack. */
1803 if (dptr
->newtable
!= PLAYTRACK
)
1805 logf("newtable: %d !!", dptr
->newtable
);
1806 tc
->dirlevel
= dirlevel
;
1811 if (tc
->filesindir
<= 0)
1812 splash(HZ
, ID2P(LANG_END_PLAYLIST
));
1815 logf("insert_all_playlist");
1816 if (!insert_all_playlist(tc
, position
, queue
))
1817 splash(HZ
*2, ID2P(LANG_FAILED
));
1820 /* Finally return the dirlevel to its original value. */
1821 while (tc
->dirlevel
> dirlevel
)
1828 static int tagtree_play_folder(struct tree_context
* c
)
1830 if (playlist_create(NULL
, NULL
) < 0)
1832 logf("Failed creating playlist\n");
1836 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1839 if (global_settings
.playlist_shuffle
)
1840 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
1841 if (!global_settings
.play_selected
)
1842 c
->selected_item
= 0;
1843 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1845 playlist_start(c
->selected_item
,0);
1846 playlist_get_current()->num_inserted_tracks
= 0; /* make warn on playlist erase work */
1850 struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
1852 struct tagentry
*entry
= (struct tagentry
*)c
->dircache
;
1853 int realid
= id
- current_offset
;
1855 /* Load the next chunk if necessary. */
1856 if (realid
>= current_entry_count
|| realid
< 0)
1859 if (retrieve_entries(c
, MAX(0, id
- (current_entry_count
/ 2)),
1862 logf("retrieve failed");
1866 realid
= id
- current_offset
;
1870 return &entry
[realid
];
1873 char *tagtree_get_title(struct tree_context
* c
)
1875 switch (c
->currtable
)
1882 return current_title
[c
->currextra
];
1888 int tagtree_get_attr(struct tree_context
* c
)
1891 switch (c
->currtable
)
1894 if (csi
->tagorder
[c
->currextra
] == tag_title
)
1895 attr
= FILE_ATTR_AUDIO
;
1897 attr
= ATTR_DIRECTORY
;
1901 attr
= FILE_ATTR_AUDIO
;
1905 attr
= ATTR_DIRECTORY
;
1912 int tagtree_get_icon(struct tree_context
* c
)
1914 int icon
= Icon_Folder
;
1916 if (tagtree_get_attr(c
) == FILE_ATTR_AUDIO
)