1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
21 * Basic structure on this file was copied from dbtree.c and modified to
22 * support the tag cache interface.
46 #include "filetypes.h"
50 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
52 static int tagtree_play_folder(struct tree_context
* c
);
54 #define SEARCHSTR_SIZE 256
56 static const struct id3_to_search_mapping
{
59 } id3_to_search_mapping
[] = {
60 { "", 0 }, /* offset n/a */
61 { "#directory#", 0 }, /* offset n/a */
62 { "#title#", offsetof(struct mp3entry
, title
) },
63 { "#artist#", offsetof(struct mp3entry
, artist
) },
64 { "#album#", offsetof(struct mp3entry
, album
) },
65 { "#genre#", offsetof(struct mp3entry
, genre_string
) },
66 { "#composer#", offsetof(struct mp3entry
, composer
) },
67 { "#albumartist#", offsetof(struct mp3entry
, albumartist
) },
81 /* Capacity 10 000 entries (for example 10k different artists) */
82 #define UNIQBUF_SIZE (64*1024)
86 #define MAX_MENU_ID_SIZE 32
88 static struct tagcache_search tcs
, tcs2
;
89 static bool sort_inverse
;
92 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
95 * formatstr = "%-3d. %s"
96 * tags[0] = tag_autoscore
101 * sort_inverse = true
103 struct display_format
{
105 struct tagcache_search_clause
*clause
[TAGCACHE_MAX_CLAUSES
];
118 static struct display_format
*formats
[TAGMENU_MAX_FMTS
];
119 static int format_count
;
121 struct search_instruction
{
123 int tagorder
[MAX_TAGS
];
125 struct tagcache_search_clause
*clause
[MAX_TAGS
][TAGCACHE_MAX_CLAUSES
];
126 int format_id
[MAX_TAGS
];
127 int clause_count
[MAX_TAGS
];
128 int result_seek
[MAX_TAGS
];
134 struct search_instruction
*si
;
140 char id
[MAX_MENU_ID_SIZE
];
142 struct menu_entry
*items
[TAGMENU_MAX_ITEMS
];
145 /* Statusbar text of the current view. */
146 static char current_title
[MAX_TAGS
][128];
148 static struct root_menu
*menus
[TAGMENU_MAX_MENUS
];
149 static struct root_menu
*menu
;
150 static struct search_instruction
*csi
;
151 static const char *strp
;
152 static int menu_count
;
153 static int root_menu
;
155 static int current_offset
;
156 static int current_entry_count
;
158 static int format_count
;
159 static struct tree_context
*tc
;
161 static int get_token_str(char *buf
, int size
)
163 /* Find the start. */
164 while (*strp
!= '"' && *strp
!= '\0')
167 if (*strp
== '\0' || *(++strp
) == '\0')
171 while (*strp
!= '"' && *strp
!= '\0' && --size
> 0)
172 *(buf
++) = *(strp
++);
183 #define MATCH(tag,str1,str2,settag) \
184 if (!strcasecmp(str1, str2)) { \
189 static int get_tag(int *tag
)
194 /* Find the start. */
195 while ((*strp
== ' ' || *strp
== '>') && *strp
!= '\0')
198 if (*strp
== '\0' || *strp
== '?')
201 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
203 if (*strp
== '\0' || *strp
== ' ')
210 MATCH(tag
, buf
, "album", tag_album
);
211 MATCH(tag
, buf
, "artist", tag_artist
);
212 MATCH(tag
, buf
, "bitrate", tag_bitrate
);
213 MATCH(tag
, buf
, "composer", tag_composer
);
214 MATCH(tag
, buf
, "comment", tag_comment
);
215 MATCH(tag
, buf
, "albumartist", tag_albumartist
);
216 MATCH(tag
, buf
, "ensemble", tag_albumartist
);
217 MATCH(tag
, buf
, "grouping", tag_grouping
);
218 MATCH(tag
, buf
, "genre", tag_genre
);
219 MATCH(tag
, buf
, "length", tag_length
);
220 MATCH(tag
, buf
, "Lm", tag_virt_length_min
);
221 MATCH(tag
, buf
, "Ls", tag_virt_length_sec
);
222 MATCH(tag
, buf
, "Pm", tag_virt_playtime_min
);
223 MATCH(tag
, buf
, "Ps", tag_virt_playtime_sec
);
224 MATCH(tag
, buf
, "title", tag_title
);
225 MATCH(tag
, buf
, "filename", tag_filename
);
226 MATCH(tag
, buf
, "tracknum", tag_tracknumber
);
227 MATCH(tag
, buf
, "discnum", tag_discnumber
);
228 MATCH(tag
, buf
, "year", tag_year
);
229 MATCH(tag
, buf
, "playcount", tag_playcount
);
230 MATCH(tag
, buf
, "rating", tag_rating
);
231 MATCH(tag
, buf
, "lastplayed", tag_lastplayed
);
232 MATCH(tag
, buf
, "commitid", tag_commitid
);
233 MATCH(tag
, buf
, "entryage", tag_virt_entryage
);
234 MATCH(tag
, buf
, "autoscore", tag_virt_autoscore
);
235 MATCH(tag
, buf
, "%sort", var_sorttype
);
236 MATCH(tag
, buf
, "%limit", var_limit
);
237 MATCH(tag
, buf
, "%strip", var_strip
);
238 MATCH(tag
, buf
, "%menu_start", var_menu_start
);
239 MATCH(tag
, buf
, "%include", var_include
);
240 MATCH(tag
, buf
, "%root_menu", var_rootmenu
);
241 MATCH(tag
, buf
, "%format", var_format
);
242 MATCH(tag
, buf
, "->", menu_next
);
243 MATCH(tag
, buf
, "==>", menu_load
);
245 logf("NO MATCH: %s\n", buf
);
252 static int get_clause(int *condition
)
257 /* Find the start. */
258 while (*strp
== ' ' && *strp
!= '\0')
264 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
266 if (*strp
== '\0' || *strp
== ' ')
273 MATCH(condition
, buf
, "=", clause_is
);
274 MATCH(condition
, buf
, "==", clause_is
);
275 MATCH(condition
, buf
, "!=", clause_is_not
);
276 MATCH(condition
, buf
, ">", clause_gt
);
277 MATCH(condition
, buf
, ">=", clause_gteq
);
278 MATCH(condition
, buf
, "<", clause_lt
);
279 MATCH(condition
, buf
, "<=", clause_lteq
);
280 MATCH(condition
, buf
, "~", clause_contains
);
281 MATCH(condition
, buf
, "!~", clause_not_contains
);
282 MATCH(condition
, buf
, "^", clause_begins_with
);
283 MATCH(condition
, buf
, "!^", clause_not_begins_with
);
284 MATCH(condition
, buf
, "$", clause_ends_with
);
285 MATCH(condition
, buf
, "!$", clause_not_ends_with
);
286 MATCH(condition
, buf
, "@", clause_oneof
);
291 static bool read_clause(struct tagcache_search_clause
*clause
)
293 char buf
[SEARCHSTR_SIZE
];
296 if (get_tag(&clause
->tag
) <= 0)
299 if (get_clause(&clause
->type
) <= 0)
302 if (get_token_str(buf
, sizeof buf
) < 0)
305 for (i
=0; i
<ARRAYLEN(id3_to_search_mapping
); i
++)
307 if (!strcasecmp(buf
, id3_to_search_mapping
[i
].string
))
311 if (i
<ARRAYLEN(id3_to_search_mapping
)) /* runtime search operand found */
313 clause
->source
= source_runtime
+i
;
314 clause
->str
= buffer_alloc(SEARCHSTR_SIZE
);
318 clause
->source
= source_constant
;
319 clause
->str
= buffer_alloc(strlen(buf
)+1);
320 strcpy(clause
->str
, buf
);
323 if (tagcache_is_numeric_tag(clause
->tag
))
325 clause
->numeric
= true;
326 clause
->numeric_data
= atoi(clause
->str
);
329 clause
->numeric
= false;
331 logf("got clause: %d/%d [%s]", clause
->tag
, clause
->type
, clause
->str
);
336 static bool read_variable(char *buf
, int size
)
340 if (!get_clause(&condition
))
343 if (condition
!= clause_is
)
346 if (get_token_str(buf
, size
) < 0)
352 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
353 static int get_format_str(struct display_format
*fmt
)
359 memset(fmt
, 0, sizeof(struct display_format
));
361 if (get_token_str(fmt
->name
, sizeof fmt
->name
) < 0)
364 /* Determine the group id */
366 for (i
= 0; i
< format_count
; i
++)
368 if (!strcasecmp(formats
[i
]->name
, fmt
->name
))
370 fmt
->group_id
= formats
[i
]->group_id
;
374 if (formats
[i
]->group_id
> fmt
->group_id
)
375 fmt
->group_id
= formats
[i
]->group_id
;
378 if (i
== format_count
)
381 logf("format: (%d) %s", fmt
->group_id
, fmt
->name
);
383 if (get_token_str(buf
, sizeof buf
) < 0)
386 fmt
->formatstr
= buffer_alloc(strlen(buf
) + 1);
387 strcpy(fmt
->formatstr
, buf
);
389 while (fmt
->tag_count
< MAX_TAGS
)
391 ret
= get_tag(&fmt
->tags
[fmt
->tag_count
]);
398 switch (fmt
->tags
[fmt
->tag_count
]) {
400 if (!read_variable(buf
, sizeof buf
))
402 if (!strcasecmp("inverse", buf
))
403 fmt
->sort_inverse
= true;
409 if (!read_variable(buf
, sizeof buf
))
411 fmt
->limit
= atoi(buf
);
415 if (!read_variable(buf
, sizeof buf
))
417 fmt
->strip
= atoi(buf
);
428 static int add_format(const char *buf
)
432 if (formats
[format_count
] == NULL
)
433 formats
[format_count
] = buffer_alloc(sizeof(struct display_format
));
435 memset(formats
[format_count
], 0, sizeof(struct display_format
));
436 if (get_format_str(formats
[format_count
]) < 0)
438 logf("get_format_str() parser failed!");
442 while (*strp
!= '\0' && *strp
!= '?')
447 int clause_count
= 0;
452 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
454 logf("too many clauses");
458 formats
[format_count
]->clause
[clause_count
] =
459 buffer_alloc(sizeof(struct tagcache_search_clause
));
461 if (!read_clause(formats
[format_count
]->clause
[clause_count
]))
467 formats
[format_count
]->clause_count
= clause_count
;
475 static int get_condition(struct search_instruction
*inst
)
486 if (get_token_str(buf
, sizeof buf
) < 0)
489 for (i
= 0; i
< format_count
; i
++)
491 if (!strcasecmp(formats
[i
]->name
, buf
))
495 if (i
== format_count
)
497 logf("format not found: %s", buf
);
501 inst
->format_id
[inst
->tagorder_count
] = formats
[i
]->group_id
;
514 clause_count
= inst
->clause_count
[inst
->tagorder_count
];
515 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
517 logf("Too many clauses");
521 inst
->clause
[inst
->tagorder_count
][clause_count
] =
522 buffer_alloc(sizeof(struct tagcache_search_clause
));
524 if (!read_clause(inst
->clause
[inst
->tagorder_count
][clause_count
]))
527 inst
->clause_count
[inst
->tagorder_count
]++;
533 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
534 * : album ? year >= "2000" : songs
540 static bool parse_search(struct menu_entry
*entry
, const char *str
)
544 struct search_instruction
*inst
= entry
->si
;
547 struct root_menu
*new_menu
;
551 /* Parse entry name */
552 if (get_token_str(entry
->name
, sizeof entry
->name
) < 0)
554 logf("No name found.");
558 /* Parse entry type */
559 if (get_tag(&entry
->type
) <= 0)
562 if (entry
->type
== menu_load
)
564 if (get_token_str(buf
, sizeof buf
) < 0)
567 /* Find the matching root menu or "create" it */
568 for (i
= 0; i
< menu_count
; i
++)
570 if (!strcasecmp(menus
[i
]->id
, buf
))
577 if (menu_count
>= TAGMENU_MAX_MENUS
)
579 logf("max menucount reached");
583 /* Allocate a new menu unless link is found. */
584 menus
[menu_count
] = buffer_alloc(sizeof(struct root_menu
));
585 new_menu
= menus
[menu_count
];
586 memset(new_menu
, 0, sizeof(struct root_menu
));
587 strncpy(new_menu
->id
, buf
, MAX_MENU_ID_SIZE
-1);
588 entry
->link
= menu_count
;
594 if (entry
->type
!= menu_next
)
597 while (inst
->tagorder_count
< MAX_TAGS
)
599 ret
= get_tag(&inst
->tagorder
[inst
->tagorder_count
]);
602 logf("Parse error #1");
610 logf("tag: %d", inst
->tagorder
[inst
->tagorder_count
]);
612 while ( (ret
= get_condition(inst
)) > 0 ) ;
616 inst
->tagorder_count
++;
618 if (get_tag(&type
) <= 0 || type
!= menu_next
)
625 static int compare(const void *p1
, const void *p2
)
627 struct tagentry
*e1
= (struct tagentry
*)p1
;
628 struct tagentry
*e2
= (struct tagentry
*)p2
;
631 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
633 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
636 static void tagtree_buffer_event(struct mp3entry
*id3
)
638 /* Do not gather data unless proper setting has been enabled. */
639 if (!global_settings
.runtimedb
)
642 logf("be:%s", id3
->path
);
644 if (!tagcache_find_index(&tcs
, id3
->path
))
646 logf("tc stat: not found: %s", id3
->path
);
650 id3
->playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
652 id3
->rating
= tagcache_get_numeric(&tcs
, tag_rating
);
653 id3
->lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
654 id3
->score
= tagcache_get_numeric(&tcs
, tag_virt_autoscore
) / 10;
655 id3
->playtime
= tagcache_get_numeric(&tcs
, tag_playtime
);
657 /* Store our tagcache index pointer. */
658 id3
->tagcache_idx
= tcs
.idx_id
+1;
660 tagcache_search_finish(&tcs
);
663 static void tagtree_track_finish_event(struct mp3entry
*id3
)
670 /* Do not gather data unless proper setting has been enabled. */
671 if (!global_settings
.runtimedb
)
673 logf("runtimedb gathering not enabled");
677 tagcache_idx
=id3
->tagcache_idx
;
680 logf("No tagcache index pointer found");
685 /* Don't process unplayed tracks. */
686 if (id3
->elapsed
== 0)
688 logf("not logging unplayed track");
692 playcount
= id3
->playcount
+ 1;
693 lastplayed
= tagcache_increase_serial();
696 logf("incorrect tc serial:%ld", lastplayed
);
700 /* Ignore the last 15s (crossfade etc.) */
701 playtime
= id3
->playtime
+ MIN(id3
->length
, id3
->elapsed
+ 15 * 1000);
703 logf("ube:%s", id3
->path
);
704 logf("-> %ld/%ld", playcount
, playtime
);
705 logf("-> %ld/%ld/%ld", id3
->elapsed
, id3
->length
, MIN(id3
->length
, id3
->elapsed
+ 15 * 1000));
707 /* Queue the updates to the tagcache system. */
708 tagcache_update_numeric(tagcache_idx
, tag_playcount
, playcount
);
709 tagcache_update_numeric(tagcache_idx
, tag_playtime
, playtime
);
710 tagcache_update_numeric(tagcache_idx
, tag_lastplayed
, lastplayed
);
713 bool tagtree_export(void)
715 gui_syncsplash(0, str(LANG_CREATING
));
716 if (!tagcache_create_changelog(&tcs
))
718 gui_syncsplash(HZ
*2, ID2P(LANG_FAILED
));
724 bool tagtree_import(void)
726 gui_syncsplash(0, ID2P(LANG_WAIT
));
727 if (!tagcache_import_changelog())
729 gui_syncsplash(HZ
*2, ID2P(LANG_FAILED
));
735 static bool parse_menu(const char *filename
);
737 static int parse_line(int n
, const char *buf
, void *parameters
)
741 static bool read_menu
;
746 logf("parse:%d/%s", n
, buf
);
748 /* First line, do initialisation. */
751 if (strcasecmp(TAGNAVI_VERSION
, buf
))
753 logf("Version mismatch");
776 if (get_tag(&variable
) <= 0)
782 if (add_format(strp
) < 0)
784 logf("Format add fail: %s", data
);
789 if (get_token_str(data
, sizeof(data
)) < 0)
791 logf("%%include empty");
795 if (!parse_menu(data
))
797 logf("Load menu fail: %s", data
);
802 if (menu_count
>= TAGMENU_MAX_MENUS
)
804 logf("max menucount reached");
808 if (get_token_str(data
, sizeof data
) < 0)
810 logf("%%menu_start id empty");
815 for (i
= 0; i
< menu_count
; i
++)
817 if (!strcasecmp(menus
[i
]->id
, data
))
825 menus
[menu_count
] = buffer_alloc(sizeof(struct root_menu
));
826 menu
= menus
[menu_count
];
828 memset(menu
, 0, sizeof(struct root_menu
));
829 strncpy(menu
->id
, data
, MAX_MENU_ID_SIZE
-1);
832 if (get_token_str(menu
->title
, sizeof(menu
->title
)) < 0)
834 logf("%%menu_start title empty");
837 logf("menu: %s", menu
->title
);
842 /* Only set root menu once. */
846 if (get_token_str(data
, sizeof(data
)) < 0)
848 logf("%%root_menu empty");
852 for (i
= 0; i
< menu_count
; i
++)
854 if (!strcasecmp(menus
[i
]->id
, data
))
865 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
867 logf("max itemcount reached");
872 if (menu
->items
[menu
->itemcount
] == NULL
)
874 menu
->items
[menu
->itemcount
] = buffer_alloc(sizeof(struct menu_entry
));
875 memset(menu
->items
[menu
->itemcount
], 0, sizeof(struct menu_entry
));
876 menu
->items
[menu
->itemcount
]->si
= buffer_alloc(sizeof(struct search_instruction
));
879 memset(menu
->items
[menu
->itemcount
]->si
, 0, sizeof(struct search_instruction
));
880 if (!parse_search(menu
->items
[menu
->itemcount
], buf
))
888 static bool parse_menu(const char *filename
)
893 if (menu_count
>= TAGMENU_MAX_MENUS
)
895 logf("max menucount reached");
899 fd
= open(filename
, O_RDONLY
);
902 logf("Search instruction file not found.");
906 /* Now read file for real, parsing into si */
907 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
913 void tagtree_init(void)
919 parse_menu(FILE_SEARCH_INSTRUCTIONS
);
921 /* If no root menu is set, assume it's the first single menu
922 * we have. That shouldn't normally happen. */
926 uniqbuf
= buffer_alloc(UNIQBUF_SIZE
);
928 add_event(PLAYBACK_EVENT_TRACK_BUFFER
, false, tagtree_buffer_event
);
929 add_event(PLAYBACK_EVENT_TRACK_FINISH
, false, tagtree_track_finish_event
);
932 static bool show_search_progress(bool init
, int count
)
934 static int last_tick
= 0;
936 /* Don't show splashes for 1/2 second after starting search */
939 last_tick
= current_tick
+ HZ
/2;
943 /* Update progress every 1/10 of a second */
944 if (current_tick
- last_tick
> HZ
/10)
946 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG
), count
,
947 str(LANG_OFF_ABORT
));
948 if (action_userabort(TIMEOUT_NOBLOCK
))
950 last_tick
= current_tick
;
957 static int format_str(struct tagcache_search
*tcs
, struct display_format
*fmt
,
958 char *buf
, int buf_size
)
961 bool read_format
= false;
967 memset(buf
, 0, buf_size
);
968 for (i
= 0; fmt
->formatstr
[i
] != '\0'; i
++)
970 if (fmt
->formatstr
[i
] == '%')
974 if (parpos
>= fmt
->tag_count
)
976 logf("too many format tags");
983 fmtbuf
[fmtbuf_pos
++] = fmt
->formatstr
[i
];
984 if (fmtbuf_pos
>= buf_size
)
986 logf("format parse error");
990 if (fmt
->formatstr
[i
] == 's')
992 fmtbuf
[fmtbuf_pos
] = '\0';
994 if (fmt
->tags
[parpos
] == tcs
->type
)
996 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
, tcs
->result
);
1000 /* Need to fetch the tag data. */
1001 if (!tagcache_retrieve(tcs
, tcs
->idx_id
, fmt
->tags
[parpos
],
1002 &buf
[buf_pos
], buf_size
- buf_pos
))
1004 logf("retrieve failed");
1008 buf_pos
+= strlen(&buf
[buf_pos
]);
1011 else if (fmt
->formatstr
[i
] == 'd')
1013 fmtbuf
[fmtbuf_pos
] = '\0';
1014 read_format
= false;
1015 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
,
1016 tagcache_get_numeric(tcs
, fmt
->tags
[parpos
]));
1017 buf_pos
+= strlen(&buf
[buf_pos
]);
1023 buf
[buf_pos
++] = fmt
->formatstr
[i
];
1025 if (buf_pos
- 1 >= buf_size
)
1027 logf("buffer overflow");
1032 buf
[buf_pos
++] = '\0';
1037 static int retrieve_entries(struct tree_context
*c
, struct tagcache_search
*tcs
,
1038 int offset
, bool init
)
1040 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1041 struct display_format
*fmt
;
1043 int namebufused
= 0;
1044 int total_count
= 0;
1045 int special_entry_count
= 0;
1046 int level
= c
->currextra
;
1053 #ifdef HAVE_TC_RAMCACHE
1054 && !tagcache_is_ramcache()
1058 /* Show search progress straight away if the disk needs to spin up,
1059 otherwise show it after the normal 1/2 second delay */
1060 show_search_progress(
1061 #if !defined(HAVE_FLASH_STORAGE) && !defined(SIMULATOR)
1062 ata_disk_is_active()
1069 if (c
->currtable
== allsubentries
)
1075 tag
= csi
->tagorder
[level
];
1077 if (!tagcache_search(tcs
, tag
))
1080 /* Prevent duplicate entries in the search list. */
1081 tagcache_search_set_uniqbuf(tcs
, uniqbuf
, UNIQBUF_SIZE
);
1083 if (level
|| csi
->clause_count
[0] || tagcache_is_numeric_tag(tag
))
1086 for (i
= 0; i
< level
; i
++)
1088 if (tagcache_is_numeric_tag(csi
->tagorder
[i
]))
1090 static struct tagcache_search_clause cc
;
1092 memset(&cc
, 0, sizeof(struct tagcache_search_clause
));
1093 cc
.tag
= csi
->tagorder
[i
];
1094 cc
.type
= clause_is
;
1096 cc
.numeric_data
= csi
->result_seek
[i
];
1097 tagcache_search_add_clause(tcs
, &cc
);
1101 tagcache_search_add_filter(tcs
, csi
->tagorder
[i
],
1102 csi
->result_seek
[i
]);
1106 for (i
= 0; i
<= level
; i
++)
1110 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1111 tagcache_search_add_clause(tcs
, csi
->clause
[i
][j
]);
1114 current_offset
= offset
;
1115 current_entry_count
= 0;
1119 for (i
= 0; i
< format_count
; i
++)
1121 if (formats
[i
]->group_id
== csi
->format_id
[level
])
1127 sort_inverse
= fmt
->sort_inverse
;
1128 sort_limit
= fmt
->limit
;
1131 /* Check if sorting is forced. */
1137 sort_inverse
= false;
1142 if (tag
!= tag_title
&& tag
!= tag_filename
)
1146 dptr
->newtable
= allsubentries
;
1147 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1149 current_entry_count
++;
1153 dptr
->newtable
= navibrowse
;
1154 dptr
->name
= str(LANG_TAGNAVI_RANDOM
);
1155 dptr
->extraseek
= -1;
1157 current_entry_count
++;
1159 special_entry_count
+=2;
1162 total_count
+= special_entry_count
;
1164 while (tagcache_get_next(tcs
))
1166 if (total_count
++ < offset
)
1169 dptr
->newtable
= navibrowse
;
1170 if (tag
== tag_title
|| tag
== tag_filename
)
1172 dptr
->newtable
= playtrack
;
1173 dptr
->extraseek
= tcs
->idx_id
;
1176 dptr
->extraseek
= tcs
->result_seek
;
1179 /* Check the format */
1180 for (i
= 0; i
< format_count
; i
++)
1182 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1185 if (tagcache_check_clauses(tcs
, formats
[i
]->clause
,
1186 formats
[i
]->clause_count
))
1193 if (!tcs
->ramresult
|| fmt
)
1199 if (format_str(tcs
, fmt
, buf
, sizeof buf
) < 0)
1201 logf("format_str() failed");
1206 dptr
->name
= &c
->name_buffer
[namebufused
];
1208 namebufused
+= strlen(buf
)+1;
1210 namebufused
+= tcs
->result_len
;
1212 if (namebufused
>= c
->name_buffer_size
)
1214 logf("chunk mode #2: %d", current_entry_count
);
1220 strcpy(dptr
->name
, buf
);
1222 strcpy(dptr
->name
, tcs
->result
);
1225 dptr
->name
= tcs
->result
;
1228 current_entry_count
++;
1230 if (current_entry_count
>= global_settings
.max_files_in_dir
)
1232 logf("chunk mode #3: %d", current_entry_count
);
1238 if (init
&& !tcs
->ramsearch
)
1240 if (!show_search_progress(false, total_count
))
1242 tagcache_search_finish(tcs
);
1243 return current_entry_count
;
1249 qsort(c
->dircache
+ special_entry_count
* c
->dentry_size
,
1250 current_entry_count
- special_entry_count
,
1251 c
->dentry_size
, compare
);
1255 tagcache_search_finish(tcs
);
1256 return current_entry_count
;
1259 while (tagcache_get_next(tcs
))
1261 if (!tcs
->ramsearch
)
1263 if (!show_search_progress(false, total_count
))
1269 tagcache_search_finish(tcs
);
1271 if (!sort
&& (sort_inverse
|| sort_limit
))
1273 gui_syncsplash(HZ
*4, ID2P(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1274 logf("Too small dir buffer");
1279 total_count
= MIN(total_count
, sort_limit
);
1284 for (i
= 0; i
< total_count
; i
++, dptr
++)
1286 int len
= strlen(dptr
->name
);
1291 dptr
->name
= &dptr
->name
[strip
];
1298 static int load_root(struct tree_context
*c
)
1300 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1304 c
->currtable
= root
;
1305 if (c
->dirlevel
== 0)
1306 c
->currextra
= root_menu
;
1308 menu
= menus
[c
->currextra
];
1312 for (i
= 0; i
< menu
->itemcount
; i
++)
1314 dptr
->name
= menu
->items
[i
]->name
;
1315 switch (menu
->items
[i
]->type
)
1318 dptr
->newtable
= navibrowse
;
1319 dptr
->extraseek
= i
;
1323 dptr
->newtable
= root
;
1324 dptr
->extraseek
= menu
->items
[i
]->link
;
1332 current_entry_count
= i
;
1337 int tagtree_load(struct tree_context
* c
)
1340 int table
= c
->currtable
;
1342 c
->dentry_size
= sizeof(struct tagentry
);
1349 c
->currtable
= table
;
1350 c
->currextra
= root_menu
;
1356 count
= load_root(c
);
1361 logf("navibrowse...");
1363 count
= retrieve_entries(c
, &tcs
, 0, true);
1368 logf("Unsupported table %d\n", table
);
1375 count
= load_root(c
);
1376 gui_syncsplash(HZ
, str(LANG_TAGCACHE_BUSY
));
1379 /* The _total_ numer of entries available. */
1380 c
->dirlength
= c
->filesindir
= count
;
1385 int tagtree_enter(struct tree_context
* c
)
1388 struct tagentry
*dptr
;
1389 struct mp3entry
*id3
;
1394 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1397 seek
= dptr
->extraseek
;
1400 if(c
->filesindir
<=2)
1402 srand(current_tick
);
1403 dptr
= (tagtree_get_entry(c
, 2+(rand() % (c
->filesindir
-2))));
1404 seek
= dptr
->extraseek
;
1406 newextra
= dptr
->newtable
;
1408 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1411 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1412 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1413 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1414 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1417 switch (c
->currtable
) {
1419 c
->currextra
= newextra
;
1421 if (newextra
== root
)
1424 c
->currextra
= seek
;
1427 else if (newextra
== navibrowse
)
1431 csi
= menu
->items
[seek
]->si
;
1434 strncpy(current_title
[c
->currextra
], dptr
->name
,
1435 sizeof(current_title
[0]) - 1);
1437 /* Read input as necessary. */
1438 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1440 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1443 source
= csi
->clause
[i
][j
]->source
;
1445 if (source
== source_constant
)
1448 searchstring
=csi
->clause
[i
][j
]->str
;
1449 *searchstring
= '\0';
1451 id3
= audio_current_track();
1453 if (source
== source_current_path
&& id3
)
1456 strncpy(searchstring
, id3
->path
, SEARCHSTR_SIZE
);
1457 e
= strrchr(searchstring
, '/');
1461 else if (source
> source_runtime
&& id3
)
1464 int k
= source
-source_runtime
;
1465 int offset
= id3_to_search_mapping
[k
].id3_offset
;
1466 char **src
= (char**)((char*)id3
+ offset
);
1469 strncpy(searchstring
, *src
, SEARCHSTR_SIZE
);
1470 searchstring
[SEARCHSTR_SIZE
-1] = '\0';
1475 rc
= kbd_input(searchstring
, SEARCHSTR_SIZE
);
1476 if (rc
== -1 || !searchstring
[0])
1481 if (csi
->clause
[i
][j
]->numeric
)
1482 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1489 c
->currtable
= newextra
;
1495 if (newextra
== playtrack
)
1498 /* about to create a new current playlist...
1499 allow user to cancel the operation */
1500 if (!warn_on_pl_erase())
1503 if (tagtree_play_folder(c
) >= 0)
1508 c
->currtable
= newextra
;
1509 csi
->result_seek
[c
->currextra
] = seek
;
1510 if (c
->currextra
< csi
->tagorder_count
-1)
1515 /* Update the statusbar title */
1516 strncpy(current_title
[c
->currextra
], dptr
->name
,
1517 sizeof(current_title
[0]) - 1);
1526 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1531 void tagtree_exit(struct tree_context
* c
)
1534 if (c
->dirlevel
> 0)
1536 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1537 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1538 c
->currtable
= c
->table_history
[c
->dirlevel
];
1539 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1540 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1543 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1545 struct tagentry
*entry
;
1547 entry
= tagtree_get_entry(c
, c
->selected_item
);
1549 if (!tagcache_search(&tcs
, tag_filename
))
1552 if (!tagcache_retrieve(&tcs
, entry
->extraseek
, tcs
.type
, buf
, buflen
))
1554 tagcache_search_finish(&tcs
);
1558 tagcache_search_finish(&tcs
);
1563 static bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1567 int from
, to
, direction
;
1568 int files_left
= c
->filesindir
;
1571 if (!tagcache_search(&tcs
, tag_filename
))
1573 gui_syncsplash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
1578 if (position
== PLAYLIST_REPLACE
)
1580 if (playlist_remove_all_tracks(NULL
) == 0)
1581 position
= PLAYLIST_INSERT_LAST
;
1584 if (position
== PLAYLIST_INSERT_FIRST
)
1586 from
= c
->filesindir
- 1;
1597 for (i
= from
; i
!= to
; i
+= direction
)
1599 /* Count back to zero */
1600 if (!show_search_progress(false, files_left
--))
1603 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1604 tcs
.type
, buf
, sizeof buf
))
1609 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1611 logf("playlist_insert_track failed");
1616 playlist_sync(NULL
);
1617 tagcache_search_finish(&tcs
);
1623 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1625 struct tagentry
*dptr
;
1627 int dirlevel
= tc
->dirlevel
;
1629 /* We need to set the table to allsubentries. */
1630 show_search_progress(true, 0);
1632 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1634 /* Insert a single track? */
1635 if (dptr
->newtable
== playtrack
)
1637 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1639 logf("tagtree_get_filename failed");
1642 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1647 if (dptr
->newtable
== navibrowse
)
1651 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1653 else if (dptr
->newtable
!= allsubentries
)
1655 logf("unsupported table: %d", dptr
->newtable
);
1659 /* Now the current table should be allsubentries. */
1660 if (dptr
->newtable
!= playtrack
)
1664 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1666 /* And now the newtable should be playtrack. */
1667 if (dptr
->newtable
!= playtrack
)
1669 logf("newtable: %d !!", dptr
->newtable
);
1670 tc
->dirlevel
= dirlevel
;
1675 if (tc
->filesindir
<= 0)
1676 gui_syncsplash(HZ
, ID2P(LANG_END_PLAYLIST
));
1679 logf("insert_all_playlist");
1680 if (!insert_all_playlist(tc
, position
, queue
))
1681 gui_syncsplash(HZ
*2, ID2P(LANG_FAILED
));
1684 /* Finally return the dirlevel to its original value. */
1685 while (tc
->dirlevel
> dirlevel
)
1692 static int tagtree_play_folder(struct tree_context
* c
)
1694 if (playlist_create(NULL
, NULL
) < 0)
1696 logf("Failed creating playlist\n");
1700 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1703 if (global_settings
.playlist_shuffle
)
1704 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
1705 if (!global_settings
.play_selected
)
1706 c
->selected_item
= 0;
1707 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1709 playlist_start(c
->selected_item
,0);
1714 struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
1716 struct tagentry
*entry
= (struct tagentry
*)c
->dircache
;
1717 int realid
= id
- current_offset
;
1719 /* Load the next chunk if necessary. */
1720 if (realid
>= current_entry_count
|| realid
< 0)
1723 if (retrieve_entries(c
, &tcs2
, MAX(0, id
- (current_entry_count
/ 2)),
1726 logf("retrieve failed");
1730 realid
= id
- current_offset
;
1734 return &entry
[realid
];
1737 char *tagtree_get_title(struct tree_context
* c
)
1739 switch (c
->currtable
)
1746 return current_title
[c
->currextra
];
1752 int tagtree_get_attr(struct tree_context
* c
)
1755 switch (c
->currtable
)
1758 if (csi
->tagorder
[c
->currextra
] == tag_title
)
1759 attr
= FILE_ATTR_AUDIO
;
1761 attr
= ATTR_DIRECTORY
;
1765 attr
= FILE_ATTR_AUDIO
;
1769 attr
= ATTR_DIRECTORY
;
1776 int tagtree_get_icon(struct tree_context
* c
)
1778 int icon
= Icon_Folder
;
1780 if (tagtree_get_attr(c
) == FILE_ATTR_AUDIO
)