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
;
135 struct search_instruction
{
137 int tagorder
[MAX_TAGS
];
139 struct tagcache_search_clause
*clause
[MAX_TAGS
][TAGCACHE_MAX_CLAUSES
];
140 int format_id
[MAX_TAGS
];
141 int clause_count
[MAX_TAGS
];
142 int result_seek
[MAX_TAGS
];
148 struct search_instruction
*si
;
154 char id
[MAX_MENU_ID_SIZE
];
156 struct menu_entry
*items
[TAGMENU_MAX_ITEMS
];
165 /* Statusbar text of the current view. */
166 static char current_title
[MAX_TAGS
][128];
168 static struct menu_root
*menus
[TAGMENU_MAX_MENUS
];
169 static struct menu_root
*menu
;
170 static struct search_instruction
*csi
;
171 static const char *strp
;
172 static int menu_count
;
175 static int current_offset
;
176 static int current_entry_count
;
178 static struct tree_context
*tc
;
180 static int get_token_str(char *buf
, int size
)
182 /* Find the start. */
183 while (*strp
!= '"' && *strp
!= '\0')
186 if (*strp
== '\0' || *(++strp
) == '\0')
190 while (*strp
!= '"' && *strp
!= '\0' && --size
> 0)
191 *(buf
++) = *(strp
++);
202 static int get_tag(int *tag
)
204 static const struct match get_tag_match
[] =
206 {"album", tag_album
},
207 {"artist", tag_artist
},
208 {"bitrate", tag_bitrate
},
209 {"composer", tag_composer
},
210 {"comment", tag_comment
},
211 {"albumartist", tag_albumartist
},
212 {"ensemble", tag_albumartist
},
213 {"grouping", tag_grouping
},
214 {"genre", tag_genre
},
215 {"length", tag_length
},
216 {"Lm", tag_virt_length_min
},
217 {"Ls", tag_virt_length_sec
},
218 {"Pm", tag_virt_playtime_min
},
219 {"Ps", tag_virt_playtime_sec
},
220 {"title", tag_title
},
221 {"filename", tag_filename
},
222 {"tracknum", tag_tracknumber
},
223 {"discnum", tag_discnumber
},
225 {"playcount", tag_playcount
},
226 {"rating", tag_rating
},
227 {"lastplayed", tag_lastplayed
},
228 {"lastoffset", tag_lastoffset
},
229 {"commitid", tag_commitid
},
230 {"entryage", tag_virt_entryage
},
231 {"autoscore", tag_virt_autoscore
},
232 {"%sort", var_sorttype
},
233 {"%limit", var_limit
},
234 {"%strip", var_strip
},
235 {"%menu_start", var_menu_start
},
236 {"%include", var_include
},
237 {"%root_menu", var_rootmenu
},
238 {"%format", var_format
},
245 /* Find the start. */
246 while ((*strp
== ' ' || *strp
== '>') && *strp
!= '\0')
249 if (*strp
== '\0' || *strp
== '?')
252 for (i
= 0; i
< sizeof(buf
)-1; i
++)
254 if (*strp
== '\0' || *strp
== ' ')
261 for (i
= 0; i
< ARRAYLEN(get_tag_match
); i
++)
263 if (!strcasecmp(buf
, get_tag_match
[i
].str
))
265 *tag
= get_tag_match
[i
].symbol
;
270 logf("NO MATCH: %s\n", buf
);
277 static int get_clause(int *condition
)
279 static const struct match get_clause_match
[] =
283 {"!=", clause_is_not
},
288 {"~", clause_contains
},
289 {"!~", clause_not_contains
},
290 {"^", clause_begins_with
},
291 {"!^", clause_not_begins_with
},
292 {"$", clause_ends_with
},
293 {"!$", clause_not_ends_with
},
300 /* Find the start. */
301 while (*strp
== ' ' && *strp
!= '\0')
307 for (i
= 0; i
< sizeof(buf
)-1; i
++)
309 if (*strp
== '\0' || *strp
== ' ')
316 for (i
= 0; i
< ARRAYLEN(get_clause_match
); i
++)
318 if (!strcasecmp(buf
, get_clause_match
[i
].str
))
320 *condition
= get_clause_match
[i
].symbol
;
328 static bool read_clause(struct tagcache_search_clause
*clause
)
330 char buf
[SEARCHSTR_SIZE
];
333 if (get_tag(&clause
->tag
) <= 0)
336 if (get_clause(&clause
->type
) <= 0)
339 if (get_token_str(buf
, sizeof buf
) < 0)
342 for (i
=0; i
<ARRAYLEN(id3_to_search_mapping
); i
++)
344 if (!strcasecmp(buf
, id3_to_search_mapping
[i
].string
))
348 if (i
<ARRAYLEN(id3_to_search_mapping
)) /* runtime search operand found */
350 clause
->source
= source_runtime
+i
;
351 clause
->str
= buffer_alloc(SEARCHSTR_SIZE
);
355 clause
->source
= source_constant
;
356 clause
->str
= buffer_alloc(strlen(buf
)+1);
357 strcpy(clause
->str
, buf
);
360 if (TAGCACHE_IS_NUMERIC(clause
->tag
))
362 clause
->numeric
= true;
363 clause
->numeric_data
= atoi(clause
->str
);
366 clause
->numeric
= false;
368 logf("got clause: %d/%d [%s]", clause
->tag
, clause
->type
, clause
->str
);
373 static bool read_variable(char *buf
, int size
)
377 if (!get_clause(&condition
))
380 if (condition
!= clause_is
)
383 if (get_token_str(buf
, size
) < 0)
389 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
390 static int get_format_str(struct display_format
*fmt
)
396 memset(fmt
, 0, sizeof(struct display_format
));
398 if (get_token_str(fmt
->name
, sizeof fmt
->name
) < 0)
401 /* Determine the group id */
403 for (i
= 0; i
< format_count
; i
++)
405 if (!strcasecmp(formats
[i
]->name
, fmt
->name
))
407 fmt
->group_id
= formats
[i
]->group_id
;
411 if (formats
[i
]->group_id
> fmt
->group_id
)
412 fmt
->group_id
= formats
[i
]->group_id
;
415 if (i
== format_count
)
418 logf("format: (%d) %s", fmt
->group_id
, fmt
->name
);
420 if (get_token_str(buf
, sizeof buf
) < 0)
423 fmt
->formatstr
= buffer_alloc(strlen(buf
) + 1);
424 strcpy(fmt
->formatstr
, buf
);
426 while (fmt
->tag_count
< MAX_TAGS
)
428 ret
= get_tag(&fmt
->tags
[fmt
->tag_count
]);
435 switch (fmt
->tags
[fmt
->tag_count
]) {
437 if (!read_variable(buf
, sizeof buf
))
439 if (!strcasecmp("inverse", buf
))
440 fmt
->sort_inverse
= true;
444 if (!read_variable(buf
, sizeof buf
))
446 fmt
->limit
= atoi(buf
);
450 if (!read_variable(buf
, sizeof buf
))
452 fmt
->strip
= atoi(buf
);
463 static int add_format(const char *buf
)
467 if (formats
[format_count
] == NULL
)
468 formats
[format_count
] = buffer_alloc(sizeof(struct display_format
));
470 memset(formats
[format_count
], 0, sizeof(struct display_format
));
471 if (get_format_str(formats
[format_count
]) < 0)
473 logf("get_format_str() parser failed!");
477 while (*strp
!= '\0' && *strp
!= '?')
482 int clause_count
= 0;
487 struct tagcache_search_clause
*newclause
;
489 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
491 logf("too many clauses");
495 newclause
= buffer_alloc(sizeof(struct tagcache_search_clause
));
497 formats
[format_count
]->clause
[clause_count
] = newclause
;
498 if (!read_clause(newclause
))
504 formats
[format_count
]->clause_count
= clause_count
;
512 static int get_condition(struct search_instruction
*inst
)
514 struct tagcache_search_clause
*new_clause
;
524 if (get_token_str(buf
, sizeof buf
) < 0)
527 for (i
= 0; i
< format_count
; i
++)
529 if (!strcasecmp(formats
[i
]->name
, buf
))
533 if (i
== format_count
)
535 logf("format not found: %s", buf
);
539 inst
->format_id
[inst
->tagorder_count
] = formats
[i
]->group_id
;
552 clause_count
= inst
->clause_count
[inst
->tagorder_count
];
553 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
555 logf("Too many clauses");
559 new_clause
= buffer_alloc(sizeof(struct tagcache_search_clause
));
560 inst
->clause
[inst
->tagorder_count
][clause_count
] = new_clause
;
565 new_clause
->type
= clause_logical_or
;
567 else if (!read_clause(new_clause
))
570 inst
->clause_count
[inst
->tagorder_count
]++;
576 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
577 * : album ? year >= "2000" : songs
583 static bool parse_search(struct menu_entry
*entry
, const char *str
)
587 struct search_instruction
*inst
= entry
->si
;
590 struct menu_root
*new_menu
;
594 /* Parse entry name */
595 if (get_token_str(entry
->name
, sizeof entry
->name
) < 0)
597 logf("No name found.");
601 /* Parse entry type */
602 if (get_tag(&entry
->type
) <= 0)
605 if (entry
->type
== menu_load
)
607 if (get_token_str(buf
, sizeof buf
) < 0)
610 /* Find the matching root menu or "create" it */
611 for (i
= 0; i
< menu_count
; i
++)
613 if (!strcasecmp(menus
[i
]->id
, buf
))
620 if (menu_count
>= TAGMENU_MAX_MENUS
)
622 logf("max menucount reached");
626 /* Allocate a new menu unless link is found. */
627 menus
[menu_count
] = buffer_alloc(sizeof(struct menu_root
));
628 new_menu
= menus
[menu_count
];
629 memset(new_menu
, 0, sizeof(struct menu_root
));
630 strlcpy(new_menu
->id
, buf
, MAX_MENU_ID_SIZE
);
631 entry
->link
= menu_count
;
637 if (entry
->type
!= menu_next
)
640 while (inst
->tagorder_count
< MAX_TAGS
)
642 ret
= get_tag(&inst
->tagorder
[inst
->tagorder_count
]);
645 logf("Parse error #1");
653 logf("tag: %d", inst
->tagorder
[inst
->tagorder_count
]);
655 while ( (ret
= get_condition(inst
)) > 0 ) ;
659 inst
->tagorder_count
++;
661 if (get_tag(&type
) <= 0 || type
!= menu_next
)
668 static int compare(const void *p1
, const void *p2
)
670 struct tagentry
*e1
= (struct tagentry
*)p1
;
671 struct tagentry
*e2
= (struct tagentry
*)p2
;
674 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
676 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
679 static void tagtree_buffer_event(void *data
)
681 struct tagcache_search tcs
;
682 struct mp3entry
*id3
= (struct mp3entry
*)data
;
684 /* Do not gather data unless proper setting has been enabled. */
685 if (!global_settings
.runtimedb
&& !global_settings
.autoresume_enable
)
688 logf("be:%s", id3
->path
);
690 while (! tagcache_is_fully_initialized())
693 if (!tagcache_find_index(&tcs
, id3
->path
))
695 logf("tc stat: not found: %s", id3
->path
);
699 if (global_settings
.runtimedb
)
701 id3
->playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
703 id3
->rating
= tagcache_get_numeric(&tcs
, tag_rating
);
704 id3
->lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
705 id3
->score
= tagcache_get_numeric(&tcs
, tag_virt_autoscore
) / 10;
706 id3
->playtime
= tagcache_get_numeric(&tcs
, tag_playtime
);
708 logf("-> %ld/%ld", id3
->playcount
, id3
->playtime
);
711 #if CONFIG_CODEC == SWCODEC
712 if (global_settings
.autoresume_enable
)
714 /* Load current file resume offset if not already defined (by
715 another resume mechanism) */
716 if (id3
->offset
== 0)
718 id3
->offset
= tagcache_get_numeric(&tcs
, tag_lastoffset
);
720 logf("tagtree_buffer_event: Set offset for %s to %lX\n",
721 str_or_empty(id3
->title
), id3
->offset
);
726 /* Store our tagcache index pointer. */
727 id3
->tagcache_idx
= tcs
.idx_id
+1;
729 tagcache_search_finish(&tcs
);
732 static void tagtree_track_finish_event(void *data
)
736 struct mp3entry
*id3
= (struct mp3entry
*)data
;
738 /* Do not gather data unless proper setting has been enabled. */
739 if (!global_settings
.runtimedb
&& !global_settings
.autoresume_enable
)
741 logf("runtimedb gathering and autoresume not enabled");
745 tagcache_idx
=id3
->tagcache_idx
;
748 logf("No tagcache index pointer found");
753 /* Don't process unplayed tracks, or tracks interrupted within the
755 if (id3
->elapsed
== 0
756 #if CONFIG_CODEC == SWCODEC /* HWCODEC doesn't have automatic_skip */
757 || (id3
->elapsed
< 15 * 1000 && !audio_automatic_skip())
761 logf("not logging unplayed or skipped track");
765 lastplayed
= tagcache_increase_serial();
768 logf("incorrect tc serial:%ld", lastplayed
);
772 if (global_settings
.runtimedb
)
777 playcount
= id3
->playcount
+ 1;
779 /* Ignore the last 15s (crossfade etc.) */
780 playtime
= id3
->playtime
+ MIN(id3
->length
, id3
->elapsed
+ 15 * 1000);
782 logf("ube:%s", id3
->path
);
783 logf("-> %ld/%ld", playcount
, playtime
);
784 logf("-> %ld/%ld/%ld", id3
->elapsed
, id3
->length
,
785 MIN(id3
->length
, id3
->elapsed
+ 15 * 1000));
787 /* Queue the updates to the tagcache system. */
788 tagcache_update_numeric(tagcache_idx
, tag_playcount
, playcount
);
789 tagcache_update_numeric(tagcache_idx
, tag_playtime
, playtime
);
790 tagcache_update_numeric(tagcache_idx
, tag_lastplayed
, lastplayed
);
793 #if CONFIG_CODEC == SWCODEC
794 if (global_settings
.autoresume_enable
)
797 = audio_automatic_skip() ? 0 : id3
->offset
;
799 tagcache_update_numeric(tagcache_idx
, tag_lastoffset
, offset
);
801 logf("tagtree_track_finish_event: Save offset for %s: %lX",
802 str_or_empty(id3
->title
), offset
);
807 bool tagtree_export(void)
809 struct tagcache_search tcs
;
811 splash(0, str(LANG_CREATING
));
812 if (!tagcache_create_changelog(&tcs
))
814 splash(HZ
*2, ID2P(LANG_FAILED
));
820 bool tagtree_import(void)
822 splash(0, ID2P(LANG_WAIT
));
823 if (!tagcache_import_changelog())
825 splash(HZ
*2, ID2P(LANG_FAILED
));
831 static bool parse_menu(const char *filename
);
833 static int parse_line(int n
, const char *buf
, void *parameters
)
837 static bool read_menu
;
842 logf("parse:%d/%s", n
, buf
);
844 /* First line, do initialisation. */
847 if (strcasecmp(TAGNAVI_VERSION
, buf
))
849 logf("Version mismatch");
872 if (get_tag(&variable
) <= 0)
878 if (add_format(strp
) < 0)
880 logf("Format add fail: %s", data
);
885 if (get_token_str(data
, sizeof(data
)) < 0)
887 logf("%%include empty");
891 if (!parse_menu(data
))
893 logf("Load menu fail: %s", data
);
898 if (menu_count
>= TAGMENU_MAX_MENUS
)
900 logf("max menucount reached");
904 if (get_token_str(data
, sizeof data
) < 0)
906 logf("%%menu_start id empty");
911 for (i
= 0; i
< menu_count
; i
++)
913 if (!strcasecmp(menus
[i
]->id
, data
))
921 menus
[menu_count
] = buffer_alloc(sizeof(struct menu_root
));
922 menu
= menus
[menu_count
];
924 memset(menu
, 0, sizeof(struct menu_root
));
925 strlcpy(menu
->id
, data
, MAX_MENU_ID_SIZE
);
928 if (get_token_str(menu
->title
, sizeof(menu
->title
)) < 0)
930 logf("%%menu_start title empty");
933 logf("menu: %s", menu
->title
);
938 /* Only set root menu once. */
942 if (get_token_str(data
, sizeof(data
)) < 0)
944 logf("%%rootmenu empty");
948 for (i
= 0; i
< menu_count
; i
++)
950 if (!strcasecmp(menus
[i
]->id
, data
))
961 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
963 logf("max itemcount reached");
968 if (menu
->items
[menu
->itemcount
] == NULL
)
970 menu
->items
[menu
->itemcount
] = buffer_alloc(sizeof(struct menu_entry
));
971 memset(menu
->items
[menu
->itemcount
], 0, sizeof(struct menu_entry
));
972 menu
->items
[menu
->itemcount
]->si
= buffer_alloc(sizeof(struct search_instruction
));
975 memset(menu
->items
[menu
->itemcount
]->si
, 0, sizeof(struct search_instruction
));
976 if (!parse_search(menu
->items
[menu
->itemcount
], buf
))
984 static bool parse_menu(const char *filename
)
989 if (menu_count
>= TAGMENU_MAX_MENUS
)
991 logf("max menucount reached");
995 fd
= open(filename
, O_RDONLY
);
998 logf("Search instruction file not found.");
1002 /* Now read file for real, parsing into si */
1003 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
1009 void tagtree_init(void)
1015 parse_menu(FILE_SEARCH_INSTRUCTIONS
);
1017 /* If no root menu is set, assume it's the first single menu
1018 * we have. That shouldn't normally happen. */
1022 uniqbuf
= buffer_alloc(UNIQBUF_SIZE
);
1024 add_event(PLAYBACK_EVENT_TRACK_BUFFER
, false, tagtree_buffer_event
);
1025 add_event(PLAYBACK_EVENT_TRACK_FINISH
, false, tagtree_track_finish_event
);
1028 static bool show_search_progress(bool init
, int count
)
1030 static int last_tick
= 0;
1032 /* Don't show splashes for 1/2 second after starting search */
1035 last_tick
= current_tick
+ HZ
/2;
1039 /* Update progress every 1/10 of a second */
1040 if (TIME_AFTER(current_tick
, last_tick
+ HZ
/10))
1042 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG
), count
, str(LANG_OFF_ABORT
));
1043 if (action_userabort(TIMEOUT_NOBLOCK
))
1045 last_tick
= current_tick
;
1052 static int format_str(struct tagcache_search
*tcs
, struct display_format
*fmt
,
1053 char *buf
, int buf_size
)
1056 bool read_format
= false;
1062 memset(buf
, 0, buf_size
);
1063 for (i
= 0; fmt
->formatstr
[i
] != '\0'; i
++)
1065 if (fmt
->formatstr
[i
] == '%')
1069 if (parpos
>= fmt
->tag_count
)
1071 logf("too many format tags");
1078 fmtbuf
[fmtbuf_pos
++] = fmt
->formatstr
[i
];
1079 if (fmtbuf_pos
>= buf_size
)
1081 logf("format parse error");
1085 if (fmt
->formatstr
[i
] == 's')
1087 fmtbuf
[fmtbuf_pos
] = '\0';
1088 read_format
= false;
1089 if (fmt
->tags
[parpos
] == tcs
->type
)
1091 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
, tcs
->result
);
1095 /* Need to fetch the tag data. */
1096 if (!tagcache_retrieve(tcs
, tcs
->idx_id
, fmt
->tags
[parpos
],
1097 &buf
[buf_pos
], buf_size
- buf_pos
))
1099 logf("retrieve failed");
1103 buf_pos
+= strlen(&buf
[buf_pos
]);
1106 else if (fmt
->formatstr
[i
] == 'd')
1108 fmtbuf
[fmtbuf_pos
] = '\0';
1109 read_format
= false;
1110 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
,
1111 tagcache_get_numeric(tcs
, fmt
->tags
[parpos
]));
1112 buf_pos
+= strlen(&buf
[buf_pos
]);
1118 buf
[buf_pos
++] = fmt
->formatstr
[i
];
1120 if (buf_pos
- 1 >= buf_size
)
1122 logf("buffer overflow");
1127 buf
[buf_pos
++] = '\0';
1132 static int retrieve_entries(struct tree_context
*c
, int offset
, bool init
)
1134 struct tagcache_search tcs
;
1135 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1136 struct display_format
*fmt
;
1138 int namebufused
= 0;
1139 int total_count
= 0;
1140 int special_entry_count
= 0;
1141 int level
= c
->currextra
;
1147 /* Show search progress straight away if the disk needs to spin up,
1148 otherwise show it after the normal 1/2 second delay */
1149 show_search_progress(
1150 #ifdef HAVE_DISK_STORAGE
1151 storage_disk_is_active()
1157 if (c
->currtable
== ALLSUBENTRIES
)
1163 tag
= csi
->tagorder
[level
];
1165 if (!tagcache_search(&tcs
, tag
))
1168 /* Prevent duplicate entries in the search list. */
1169 tagcache_search_set_uniqbuf(&tcs
, uniqbuf
, UNIQBUF_SIZE
);
1171 if (level
|| csi
->clause_count
[0] || TAGCACHE_IS_NUMERIC(tag
))
1174 for (i
= 0; i
< level
; i
++)
1176 if (TAGCACHE_IS_NUMERIC(csi
->tagorder
[i
]))
1178 static struct tagcache_search_clause cc
;
1180 memset(&cc
, 0, sizeof(struct tagcache_search_clause
));
1181 cc
.tag
= csi
->tagorder
[i
];
1182 cc
.type
= clause_is
;
1184 cc
.numeric_data
= csi
->result_seek
[i
];
1185 tagcache_search_add_clause(&tcs
, &cc
);
1189 tagcache_search_add_filter(&tcs
, csi
->tagorder
[i
],
1190 csi
->result_seek
[i
]);
1194 for (i
= 0; i
<= level
; i
++)
1198 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1199 tagcache_search_add_clause(&tcs
, csi
->clause
[i
][j
]);
1202 current_offset
= offset
;
1203 current_entry_count
= 0;
1207 for (i
= 0; i
< format_count
; i
++)
1209 if (formats
[i
]->group_id
== csi
->format_id
[level
])
1215 sort_inverse
= fmt
->sort_inverse
;
1216 sort_limit
= fmt
->limit
;
1222 sort_inverse
= false;
1227 if (tag
!= tag_title
&& tag
!= tag_filename
)
1231 dptr
->newtable
= ALLSUBENTRIES
;
1232 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1234 current_entry_count
++;
1238 dptr
->newtable
= NAVIBROWSE
;
1239 dptr
->name
= str(LANG_TAGNAVI_RANDOM
);
1240 dptr
->extraseek
= -1;
1242 current_entry_count
++;
1244 special_entry_count
+=2;
1247 total_count
+= special_entry_count
;
1249 while (tagcache_get_next(&tcs
))
1251 if (total_count
++ < offset
)
1254 if ( strcmp(tcs
.result
, UNTAGGED
) == 0)
1256 tcs
.result_len
= strlcpy(tcs
.result
,
1257 str(LANG_TAGNAVI_UNTAGGED
), TAG_MAXLEN
)+1;
1260 dptr
->newtable
= NAVIBROWSE
;
1261 if (tag
== tag_title
|| tag
== tag_filename
)
1263 dptr
->newtable
= PLAYTRACK
;
1264 dptr
->extraseek
= tcs
.idx_id
;
1267 dptr
->extraseek
= tcs
.result_seek
;
1270 /* Check the format */
1271 for (i
= 0; i
< format_count
; i
++)
1273 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1276 if (tagcache_check_clauses(&tcs
, formats
[i
]->clause
,
1277 formats
[i
]->clause_count
))
1284 if (!tcs
.ramresult
|| fmt
)
1290 if (format_str(&tcs
, fmt
, buf
, sizeof buf
) < 0)
1292 logf("format_str() failed");
1293 tagcache_search_finish(&tcs
);
1298 dptr
->name
= &c
->name_buffer
[namebufused
];
1300 namebufused
+= strlen(buf
)+1;
1302 namebufused
+= tcs
.result_len
;
1304 if (namebufused
>= c
->name_buffer_size
)
1306 logf("chunk mode #2: %d", current_entry_count
);
1312 strcpy(dptr
->name
, buf
);
1314 strcpy(dptr
->name
, tcs
.result
);
1317 dptr
->name
= tcs
.result
;
1320 current_entry_count
++;
1322 if (current_entry_count
>= c
->dircache_count
)
1324 logf("chunk mode #3: %d", current_entry_count
);
1330 if (init
&& !tcs
.ramsearch
)
1332 if (!show_search_progress(false, total_count
))
1333 { /* user aborted */
1334 tagcache_search_finish(&tcs
);
1335 return current_entry_count
;
1341 qsort(c
->dircache
+ special_entry_count
* c
->dentry_size
,
1342 current_entry_count
- special_entry_count
,
1343 c
->dentry_size
, compare
);
1347 tagcache_search_finish(&tcs
);
1348 return current_entry_count
;
1351 while (tagcache_get_next(&tcs
))
1355 if (!show_search_progress(false, total_count
))
1361 tagcache_search_finish(&tcs
);
1363 if (!sort
&& (sort_inverse
|| sort_limit
))
1365 splashf(HZ
*4, ID2P(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1366 logf("Too small dir buffer");
1371 total_count
= MIN(total_count
, sort_limit
);
1376 for (i
= 0; i
< total_count
; i
++, dptr
++)
1378 int len
= strlen(dptr
->name
);
1383 dptr
->name
= &dptr
->name
[strip
];
1391 static int load_root(struct tree_context
*c
)
1393 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1397 c
->currtable
= ROOT
;
1398 if (c
->dirlevel
== 0)
1399 c
->currextra
= rootmenu
;
1401 menu
= menus
[c
->currextra
];
1405 for (i
= 0; i
< menu
->itemcount
; i
++)
1407 dptr
->name
= menu
->items
[i
]->name
;
1408 switch (menu
->items
[i
]->type
)
1411 dptr
->newtable
= NAVIBROWSE
;
1412 dptr
->extraseek
= i
;
1416 dptr
->newtable
= ROOT
;
1417 dptr
->extraseek
= menu
->items
[i
]->link
;
1425 current_entry_count
= i
;
1430 int tagtree_load(struct tree_context
* c
)
1433 int table
= c
->currtable
;
1435 c
->dentry_size
= sizeof(struct tagentry
);
1442 c
->currtable
= table
;
1443 c
->currextra
= rootmenu
;
1449 count
= load_root(c
);
1454 logf("navibrowse...");
1456 count
= retrieve_entries(c
, 0, true);
1461 logf("Unsupported table %d\n", table
);
1468 count
= load_root(c
);
1469 splash(HZ
, str(LANG_TAGCACHE_BUSY
));
1472 /* The _total_ numer of entries available. */
1473 c
->dirlength
= c
->filesindir
= count
;
1478 int tagtree_enter(struct tree_context
* c
)
1481 struct tagentry
*dptr
;
1482 struct mp3entry
*id3
;
1487 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1490 seek
= dptr
->extraseek
;
1493 if(c
->filesindir
<=2)
1495 srand(current_tick
);
1496 dptr
= (tagtree_get_entry(c
, 2+(rand() % (c
->filesindir
-2))));
1497 seek
= dptr
->extraseek
;
1499 newextra
= dptr
->newtable
;
1501 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1504 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1505 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1506 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1507 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1510 switch (c
->currtable
) {
1512 c
->currextra
= newextra
;
1514 if (newextra
== ROOT
)
1517 c
->currextra
= seek
;
1520 else if (newextra
== NAVIBROWSE
)
1524 csi
= menu
->items
[seek
]->si
;
1527 strlcpy(current_title
[c
->currextra
], dptr
->name
,
1528 sizeof(current_title
[0]));
1530 /* Read input as necessary. */
1531 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1533 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1537 if (csi
->clause
[i
][j
]->type
== clause_logical_or
)
1540 source
= csi
->clause
[i
][j
]->source
;
1542 if (source
== source_constant
)
1545 searchstring
=csi
->clause
[i
][j
]->str
;
1546 *searchstring
= '\0';
1548 id3
= audio_current_track();
1550 if (source
== source_current_path
&& id3
)
1553 strlcpy(searchstring
, id3
->path
, SEARCHSTR_SIZE
);
1554 e
= strrchr(searchstring
, '/');
1558 else if (source
> source_runtime
&& id3
)
1561 int k
= source
-source_runtime
;
1562 int offset
= id3_to_search_mapping
[k
].id3_offset
;
1563 char **src
= (char**)((char*)id3
+ offset
);
1566 strlcpy(searchstring
, *src
, SEARCHSTR_SIZE
);
1571 rc
= kbd_input(searchstring
, SEARCHSTR_SIZE
);
1572 if (rc
< 0 || !searchstring
[0])
1577 if (csi
->clause
[i
][j
]->numeric
)
1578 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1585 c
->currtable
= newextra
;
1591 if (newextra
== PLAYTRACK
)
1593 if (global_settings
.party_mode
&& audio_status()) {
1594 splash(HZ
, ID2P(LANG_PARTY_MODE
));
1598 /* about to create a new current playlist...
1599 allow user to cancel the operation */
1600 if (!warn_on_pl_erase())
1602 if (tagtree_play_folder(c
) >= 0)
1607 c
->currtable
= newextra
;
1608 csi
->result_seek
[c
->currextra
] = seek
;
1609 if (c
->currextra
< csi
->tagorder_count
-1)
1614 /* Update the statusbar title */
1615 strlcpy(current_title
[c
->currextra
], dptr
->name
,
1616 sizeof(current_title
[0]));
1625 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1630 void tagtree_exit(struct tree_context
* c
)
1633 if (c
->dirlevel
> 0)
1635 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1636 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1637 c
->currtable
= c
->table_history
[c
->dirlevel
];
1638 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1639 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1642 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1644 struct tagcache_search tcs
;
1645 struct tagentry
*entry
;
1647 entry
= tagtree_get_entry(c
, c
->selected_item
);
1649 if (!tagcache_search(&tcs
, tag_filename
))
1652 if (!tagcache_retrieve(&tcs
, entry
->extraseek
, tcs
.type
, buf
, buflen
))
1654 tagcache_search_finish(&tcs
);
1658 tagcache_search_finish(&tcs
);
1663 static bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1665 struct tagcache_search tcs
;
1668 int from
, to
, direction
;
1669 int files_left
= c
->filesindir
;
1672 if (!tagcache_search(&tcs
, tag_filename
))
1674 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
1679 if (position
== PLAYLIST_REPLACE
)
1681 if (playlist_remove_all_tracks(NULL
) == 0)
1682 position
= PLAYLIST_INSERT_LAST
;
1690 if (position
== PLAYLIST_INSERT_FIRST
)
1692 from
= c
->filesindir
- 1;
1703 for (i
= from
; i
!= to
; i
+= direction
)
1705 /* Count back to zero */
1706 if (!show_search_progress(false, files_left
--))
1709 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1710 tcs
.type
, buf
, sizeof buf
))
1715 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1717 logf("playlist_insert_track failed");
1722 playlist_sync(NULL
);
1723 tagcache_search_finish(&tcs
);
1729 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1731 struct tagentry
*dptr
;
1733 int dirlevel
= tc
->dirlevel
;
1735 show_search_progress(
1736 #ifdef HAVE_DISK_STORAGE
1737 storage_disk_is_active()
1744 /* We need to set the table to allsubentries. */
1745 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1747 /* Insert a single track? */
1748 if (dptr
->newtable
== PLAYTRACK
)
1750 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1752 logf("tagtree_get_filename failed");
1755 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1760 if (dptr
->newtable
== NAVIBROWSE
)
1764 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1766 else if (dptr
->newtable
!= ALLSUBENTRIES
)
1768 logf("unsupported table: %d", dptr
->newtable
);
1772 /* Now the current table should be allsubentries. */
1773 if (dptr
->newtable
!= PLAYTRACK
)
1777 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1779 /* And now the newtable should be playtrack. */
1780 if (dptr
->newtable
!= PLAYTRACK
)
1782 logf("newtable: %d !!", dptr
->newtable
);
1783 tc
->dirlevel
= dirlevel
;
1788 if (tc
->filesindir
<= 0)
1789 splash(HZ
, ID2P(LANG_END_PLAYLIST
));
1792 logf("insert_all_playlist");
1793 if (!insert_all_playlist(tc
, position
, queue
))
1794 splash(HZ
*2, ID2P(LANG_FAILED
));
1797 /* Finally return the dirlevel to its original value. */
1798 while (tc
->dirlevel
> dirlevel
)
1805 static int tagtree_play_folder(struct tree_context
* c
)
1807 if (playlist_create(NULL
, NULL
) < 0)
1809 logf("Failed creating playlist\n");
1813 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1816 if (global_settings
.playlist_shuffle
)
1817 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
1818 if (!global_settings
.play_selected
)
1819 c
->selected_item
= 0;
1820 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1822 playlist_start(c
->selected_item
,0);
1823 playlist_get_current()->num_inserted_tracks
= 0; /* make warn on playlist erase work */
1827 struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
1829 struct tagentry
*entry
= (struct tagentry
*)c
->dircache
;
1830 int realid
= id
- current_offset
;
1832 /* Load the next chunk if necessary. */
1833 if (realid
>= current_entry_count
|| realid
< 0)
1836 if (retrieve_entries(c
, MAX(0, id
- (current_entry_count
/ 2)),
1839 logf("retrieve failed");
1843 realid
= id
- current_offset
;
1847 return &entry
[realid
];
1850 char *tagtree_get_title(struct tree_context
* c
)
1852 switch (c
->currtable
)
1859 return current_title
[c
->currextra
];
1865 int tagtree_get_attr(struct tree_context
* c
)
1868 switch (c
->currtable
)
1871 if (csi
->tagorder
[c
->currextra
] == tag_title
)
1872 attr
= FILE_ATTR_AUDIO
;
1874 attr
= ATTR_DIRECTORY
;
1878 attr
= FILE_ATTR_AUDIO
;
1882 attr
= ATTR_DIRECTORY
;
1889 int tagtree_get_icon(struct tree_context
* c
)
1891 int icon
= Icon_Folder
;
1893 if (tagtree_get_attr(c
) == FILE_ATTR_AUDIO
)