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.
48 #include "filetypes.h"
50 #include "appevents.h"
52 #include "dir_uncached.h"
54 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
56 static int tagtree_play_folder(struct tree_context
* c
);
58 #define SEARCHSTR_SIZE 256
60 static const struct id3_to_search_mapping
{
63 } id3_to_search_mapping
[] = {
64 { "", 0 }, /* offset n/a */
65 { "#directory#", 0 }, /* offset n/a */
66 { "#title#", offsetof(struct mp3entry
, title
) },
67 { "#artist#", offsetof(struct mp3entry
, artist
) },
68 { "#album#", offsetof(struct mp3entry
, album
) },
69 { "#genre#", offsetof(struct mp3entry
, genre_string
) },
70 { "#composer#", offsetof(struct mp3entry
, composer
) },
71 { "#albumartist#", offsetof(struct mp3entry
, albumartist
) },
85 /* Capacity 10 000 entries (for example 10k different artists) */
86 #define UNIQBUF_SIZE (64*1024)
90 #define MAX_MENU_ID_SIZE 32
92 static struct tagcache_search tcs
, tcs2
;
93 static bool sort_inverse
;
96 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
99 * formatstr = "%-3d. %s"
100 * tags[0] = tag_autoscore
101 * tags[1] = tag_title
105 * sort_inverse = true
107 struct display_format
{
109 struct tagcache_search_clause
*clause
[TAGCACHE_MAX_CLAUSES
];
121 static struct display_format
*formats
[TAGMENU_MAX_FMTS
];
122 static int format_count
;
124 struct search_instruction
{
126 int tagorder
[MAX_TAGS
];
128 struct tagcache_search_clause
*clause
[MAX_TAGS
][TAGCACHE_MAX_CLAUSES
];
129 int format_id
[MAX_TAGS
];
130 int clause_count
[MAX_TAGS
];
131 int result_seek
[MAX_TAGS
];
137 struct search_instruction
*si
;
143 char id
[MAX_MENU_ID_SIZE
];
145 struct menu_entry
*items
[TAGMENU_MAX_ITEMS
];
148 /* Statusbar text of the current view. */
149 static char current_title
[MAX_TAGS
][128];
151 static struct root_menu
*menus
[TAGMENU_MAX_MENUS
];
152 static struct root_menu
*menu
;
153 static struct search_instruction
*csi
;
154 static const char *strp
;
155 static int menu_count
;
156 static int root_menu
;
158 static int current_offset
;
159 static int current_entry_count
;
161 static int format_count
;
162 static struct tree_context
*tc
;
164 static int get_token_str(char *buf
, int size
)
166 /* Find the start. */
167 while (*strp
!= '"' && *strp
!= '\0')
170 if (*strp
== '\0' || *(++strp
) == '\0')
174 while (*strp
!= '"' && *strp
!= '\0' && --size
> 0)
175 *(buf
++) = *(strp
++);
186 #define MATCH(tag,str1,str2,settag) \
187 if (!strcasecmp(str1, str2)) { \
192 static int get_tag(int *tag
)
197 /* Find the start. */
198 while ((*strp
== ' ' || *strp
== '>') && *strp
!= '\0')
201 if (*strp
== '\0' || *strp
== '?')
204 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
206 if (*strp
== '\0' || *strp
== ' ')
213 MATCH(tag
, buf
, "album", tag_album
);
214 MATCH(tag
, buf
, "artist", tag_artist
);
215 MATCH(tag
, buf
, "bitrate", tag_bitrate
);
216 MATCH(tag
, buf
, "composer", tag_composer
);
217 MATCH(tag
, buf
, "comment", tag_comment
);
218 MATCH(tag
, buf
, "albumartist", tag_albumartist
);
219 MATCH(tag
, buf
, "ensemble", tag_albumartist
);
220 MATCH(tag
, buf
, "grouping", tag_grouping
);
221 MATCH(tag
, buf
, "genre", tag_genre
);
222 MATCH(tag
, buf
, "length", tag_length
);
223 MATCH(tag
, buf
, "Lm", tag_virt_length_min
);
224 MATCH(tag
, buf
, "Ls", tag_virt_length_sec
);
225 MATCH(tag
, buf
, "Pm", tag_virt_playtime_min
);
226 MATCH(tag
, buf
, "Ps", tag_virt_playtime_sec
);
227 MATCH(tag
, buf
, "title", tag_title
);
228 MATCH(tag
, buf
, "filename", tag_filename
);
229 MATCH(tag
, buf
, "tracknum", tag_tracknumber
);
230 MATCH(tag
, buf
, "discnum", tag_discnumber
);
231 MATCH(tag
, buf
, "year", tag_year
);
232 MATCH(tag
, buf
, "playcount", tag_playcount
);
233 MATCH(tag
, buf
, "rating", tag_rating
);
234 MATCH(tag
, buf
, "lastplayed", tag_lastplayed
);
235 MATCH(tag
, buf
, "commitid", tag_commitid
);
236 MATCH(tag
, buf
, "entryage", tag_virt_entryage
);
237 MATCH(tag
, buf
, "autoscore", tag_virt_autoscore
);
238 MATCH(tag
, buf
, "%sort", var_sorttype
);
239 MATCH(tag
, buf
, "%limit", var_limit
);
240 MATCH(tag
, buf
, "%strip", var_strip
);
241 MATCH(tag
, buf
, "%menu_start", var_menu_start
);
242 MATCH(tag
, buf
, "%include", var_include
);
243 MATCH(tag
, buf
, "%root_menu", var_rootmenu
);
244 MATCH(tag
, buf
, "%format", var_format
);
245 MATCH(tag
, buf
, "->", menu_next
);
246 MATCH(tag
, buf
, "==>", menu_load
);
248 logf("NO MATCH: %s\n", buf
);
255 static int get_clause(int *condition
)
260 /* Find the start. */
261 while (*strp
== ' ' && *strp
!= '\0')
267 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
269 if (*strp
== '\0' || *strp
== ' ')
276 MATCH(condition
, buf
, "=", clause_is
);
277 MATCH(condition
, buf
, "==", clause_is
);
278 MATCH(condition
, buf
, "!=", clause_is_not
);
279 MATCH(condition
, buf
, ">", clause_gt
);
280 MATCH(condition
, buf
, ">=", clause_gteq
);
281 MATCH(condition
, buf
, "<", clause_lt
);
282 MATCH(condition
, buf
, "<=", clause_lteq
);
283 MATCH(condition
, buf
, "~", clause_contains
);
284 MATCH(condition
, buf
, "!~", clause_not_contains
);
285 MATCH(condition
, buf
, "^", clause_begins_with
);
286 MATCH(condition
, buf
, "!^", clause_not_begins_with
);
287 MATCH(condition
, buf
, "$", clause_ends_with
);
288 MATCH(condition
, buf
, "!$", clause_not_ends_with
);
289 MATCH(condition
, buf
, "@", clause_oneof
);
294 static bool read_clause(struct tagcache_search_clause
*clause
)
296 char buf
[SEARCHSTR_SIZE
];
299 if (get_tag(&clause
->tag
) <= 0)
302 if (get_clause(&clause
->type
) <= 0)
305 if (get_token_str(buf
, sizeof buf
) < 0)
308 for (i
=0; i
<ARRAYLEN(id3_to_search_mapping
); i
++)
310 if (!strcasecmp(buf
, id3_to_search_mapping
[i
].string
))
314 if (i
<ARRAYLEN(id3_to_search_mapping
)) /* runtime search operand found */
316 clause
->source
= source_runtime
+i
;
317 clause
->str
= buffer_alloc(SEARCHSTR_SIZE
);
321 clause
->source
= source_constant
;
322 clause
->str
= buffer_alloc(strlen(buf
)+1);
323 strcpy(clause
->str
, buf
);
326 if (tagcache_is_numeric_tag(clause
->tag
))
328 clause
->numeric
= true;
329 clause
->numeric_data
= atoi(clause
->str
);
332 clause
->numeric
= false;
334 logf("got clause: %d/%d [%s]", clause
->tag
, clause
->type
, clause
->str
);
339 static bool read_variable(char *buf
, int size
)
343 if (!get_clause(&condition
))
346 if (condition
!= clause_is
)
349 if (get_token_str(buf
, size
) < 0)
355 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
356 static int get_format_str(struct display_format
*fmt
)
362 memset(fmt
, 0, sizeof(struct display_format
));
364 if (get_token_str(fmt
->name
, sizeof fmt
->name
) < 0)
367 /* Determine the group id */
369 for (i
= 0; i
< format_count
; i
++)
371 if (!strcasecmp(formats
[i
]->name
, fmt
->name
))
373 fmt
->group_id
= formats
[i
]->group_id
;
377 if (formats
[i
]->group_id
> fmt
->group_id
)
378 fmt
->group_id
= formats
[i
]->group_id
;
381 if (i
== format_count
)
384 logf("format: (%d) %s", fmt
->group_id
, fmt
->name
);
386 if (get_token_str(buf
, sizeof buf
) < 0)
389 fmt
->formatstr
= buffer_alloc(strlen(buf
) + 1);
390 strcpy(fmt
->formatstr
, buf
);
392 while (fmt
->tag_count
< MAX_TAGS
)
394 ret
= get_tag(&fmt
->tags
[fmt
->tag_count
]);
401 switch (fmt
->tags
[fmt
->tag_count
]) {
403 if (!read_variable(buf
, sizeof buf
))
405 if (!strcasecmp("inverse", buf
))
406 fmt
->sort_inverse
= true;
410 if (!read_variable(buf
, sizeof buf
))
412 fmt
->limit
= atoi(buf
);
416 if (!read_variable(buf
, sizeof buf
))
418 fmt
->strip
= atoi(buf
);
429 static int add_format(const char *buf
)
433 if (formats
[format_count
] == NULL
)
434 formats
[format_count
] = buffer_alloc(sizeof(struct display_format
));
436 memset(formats
[format_count
], 0, sizeof(struct display_format
));
437 if (get_format_str(formats
[format_count
]) < 0)
439 logf("get_format_str() parser failed!");
443 while (*strp
!= '\0' && *strp
!= '?')
448 int clause_count
= 0;
453 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
455 logf("too many clauses");
459 formats
[format_count
]->clause
[clause_count
] =
460 buffer_alloc(sizeof(struct tagcache_search_clause
));
462 if (!read_clause(formats
[format_count
]->clause
[clause_count
]))
468 formats
[format_count
]->clause_count
= clause_count
;
476 static int get_condition(struct search_instruction
*inst
)
487 if (get_token_str(buf
, sizeof buf
) < 0)
490 for (i
= 0; i
< format_count
; i
++)
492 if (!strcasecmp(formats
[i
]->name
, buf
))
496 if (i
== format_count
)
498 logf("format not found: %s", buf
);
502 inst
->format_id
[inst
->tagorder_count
] = formats
[i
]->group_id
;
515 clause_count
= inst
->clause_count
[inst
->tagorder_count
];
516 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
518 logf("Too many clauses");
522 inst
->clause
[inst
->tagorder_count
][clause_count
] =
523 buffer_alloc(sizeof(struct tagcache_search_clause
));
525 if (!read_clause(inst
->clause
[inst
->tagorder_count
][clause_count
]))
528 inst
->clause_count
[inst
->tagorder_count
]++;
534 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
535 * : album ? year >= "2000" : songs
541 static bool parse_search(struct menu_entry
*entry
, const char *str
)
545 struct search_instruction
*inst
= entry
->si
;
548 struct root_menu
*new_menu
;
552 /* Parse entry name */
553 if (get_token_str(entry
->name
, sizeof entry
->name
) < 0)
555 logf("No name found.");
559 /* Parse entry type */
560 if (get_tag(&entry
->type
) <= 0)
563 if (entry
->type
== menu_load
)
565 if (get_token_str(buf
, sizeof buf
) < 0)
568 /* Find the matching root menu or "create" it */
569 for (i
= 0; i
< menu_count
; i
++)
571 if (!strcasecmp(menus
[i
]->id
, buf
))
578 if (menu_count
>= TAGMENU_MAX_MENUS
)
580 logf("max menucount reached");
584 /* Allocate a new menu unless link is found. */
585 menus
[menu_count
] = buffer_alloc(sizeof(struct root_menu
));
586 new_menu
= menus
[menu_count
];
587 memset(new_menu
, 0, sizeof(struct root_menu
));
588 strncpy(new_menu
->id
, buf
, MAX_MENU_ID_SIZE
-1);
589 entry
->link
= menu_count
;
595 if (entry
->type
!= menu_next
)
598 while (inst
->tagorder_count
< MAX_TAGS
)
600 ret
= get_tag(&inst
->tagorder
[inst
->tagorder_count
]);
603 logf("Parse error #1");
611 logf("tag: %d", inst
->tagorder
[inst
->tagorder_count
]);
613 while ( (ret
= get_condition(inst
)) > 0 ) ;
617 inst
->tagorder_count
++;
619 if (get_tag(&type
) <= 0 || type
!= menu_next
)
626 static int compare(const void *p1
, const void *p2
)
628 struct tagentry
*e1
= (struct tagentry
*)p1
;
629 struct tagentry
*e2
= (struct tagentry
*)p2
;
632 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
634 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
637 static void tagtree_buffer_event(struct mp3entry
*id3
)
639 /* Do not gather data unless proper setting has been enabled. */
640 if (!global_settings
.runtimedb
)
643 logf("be:%s", id3
->path
);
645 if (!tagcache_find_index(&tcs
, id3
->path
))
647 logf("tc stat: not found: %s", id3
->path
);
651 id3
->playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
653 id3
->rating
= tagcache_get_numeric(&tcs
, tag_rating
);
654 id3
->lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
655 id3
->score
= tagcache_get_numeric(&tcs
, tag_virt_autoscore
) / 10;
656 id3
->playtime
= tagcache_get_numeric(&tcs
, tag_playtime
);
658 /* Store our tagcache index pointer. */
659 id3
->tagcache_idx
= tcs
.idx_id
+1;
661 tagcache_search_finish(&tcs
);
664 static void tagtree_track_finish_event(struct mp3entry
*id3
)
671 /* Do not gather data unless proper setting has been enabled. */
672 if (!global_settings
.runtimedb
)
674 logf("runtimedb gathering not enabled");
678 tagcache_idx
=id3
->tagcache_idx
;
681 logf("No tagcache index pointer found");
686 /* Don't process unplayed tracks. */
687 if (id3
->elapsed
== 0)
689 logf("not logging unplayed track");
693 playcount
= id3
->playcount
+ 1;
694 lastplayed
= tagcache_increase_serial();
697 logf("incorrect tc serial:%ld", lastplayed
);
701 /* Ignore the last 15s (crossfade etc.) */
702 playtime
= id3
->playtime
+ MIN(id3
->length
, id3
->elapsed
+ 15 * 1000);
704 logf("ube:%s", id3
->path
);
705 logf("-> %ld/%ld", playcount
, playtime
);
706 logf("-> %ld/%ld/%ld", id3
->elapsed
, id3
->length
, MIN(id3
->length
, id3
->elapsed
+ 15 * 1000));
708 /* Queue the updates to the tagcache system. */
709 tagcache_update_numeric(tagcache_idx
, tag_playcount
, playcount
);
710 tagcache_update_numeric(tagcache_idx
, tag_playtime
, playtime
);
711 tagcache_update_numeric(tagcache_idx
, tag_lastplayed
, lastplayed
);
714 bool tagtree_export(void)
716 splash(0, str(LANG_CREATING
));
717 if (!tagcache_create_changelog(&tcs
))
719 splash(HZ
*2, ID2P(LANG_FAILED
));
725 bool tagtree_import(void)
727 splash(0, ID2P(LANG_WAIT
));
728 if (!tagcache_import_changelog())
730 splash(HZ
*2, ID2P(LANG_FAILED
));
736 static bool parse_menu(const char *filename
);
738 static int parse_line(int n
, const char *buf
, void *parameters
)
742 static bool read_menu
;
747 logf("parse:%d/%s", n
, buf
);
749 /* First line, do initialisation. */
752 if (strcasecmp(TAGNAVI_VERSION
, buf
))
754 logf("Version mismatch");
777 if (get_tag(&variable
) <= 0)
783 if (add_format(strp
) < 0)
785 logf("Format add fail: %s", data
);
790 if (get_token_str(data
, sizeof(data
)) < 0)
792 logf("%%include empty");
796 if (!parse_menu(data
))
798 logf("Load menu fail: %s", data
);
803 if (menu_count
>= TAGMENU_MAX_MENUS
)
805 logf("max menucount reached");
809 if (get_token_str(data
, sizeof data
) < 0)
811 logf("%%menu_start id empty");
816 for (i
= 0; i
< menu_count
; i
++)
818 if (!strcasecmp(menus
[i
]->id
, data
))
826 menus
[menu_count
] = buffer_alloc(sizeof(struct root_menu
));
827 menu
= menus
[menu_count
];
829 memset(menu
, 0, sizeof(struct root_menu
));
830 strncpy(menu
->id
, data
, MAX_MENU_ID_SIZE
-1);
833 if (get_token_str(menu
->title
, sizeof(menu
->title
)) < 0)
835 logf("%%menu_start title empty");
838 logf("menu: %s", menu
->title
);
843 /* Only set root menu once. */
847 if (get_token_str(data
, sizeof(data
)) < 0)
849 logf("%%root_menu empty");
853 for (i
= 0; i
< menu_count
; i
++)
855 if (!strcasecmp(menus
[i
]->id
, data
))
866 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
868 logf("max itemcount reached");
873 if (menu
->items
[menu
->itemcount
] == NULL
)
875 menu
->items
[menu
->itemcount
] = buffer_alloc(sizeof(struct menu_entry
));
876 memset(menu
->items
[menu
->itemcount
], 0, sizeof(struct menu_entry
));
877 menu
->items
[menu
->itemcount
]->si
= buffer_alloc(sizeof(struct search_instruction
));
880 memset(menu
->items
[menu
->itemcount
]->si
, 0, sizeof(struct search_instruction
));
881 if (!parse_search(menu
->items
[menu
->itemcount
], buf
))
889 static bool parse_menu(const char *filename
)
894 if (menu_count
>= TAGMENU_MAX_MENUS
)
896 logf("max menucount reached");
900 fd
= open(filename
, O_RDONLY
);
903 logf("Search instruction file not found.");
907 /* Now read file for real, parsing into si */
908 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
914 void tagtree_init(void)
920 parse_menu(FILE_SEARCH_INSTRUCTIONS
);
922 /* If no root menu is set, assume it's the first single menu
923 * we have. That shouldn't normally happen. */
927 uniqbuf
= buffer_alloc(UNIQBUF_SIZE
);
929 add_event(PLAYBACK_EVENT_TRACK_BUFFER
, false, tagtree_buffer_event
);
930 add_event(PLAYBACK_EVENT_TRACK_FINISH
, false, tagtree_track_finish_event
);
933 static bool show_search_progress(bool init
, int count
)
935 static int last_tick
= 0;
937 /* Don't show splashes for 1/2 second after starting search */
940 last_tick
= current_tick
+ HZ
/2;
944 /* Update progress every 1/10 of a second */
945 if (current_tick
- last_tick
> HZ
/10)
947 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG
), count
, 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 #ifdef HAVE_DISK_STORAGE
1062 storage_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
;
1134 sort_inverse
= false;
1139 if (tag
!= tag_title
&& tag
!= tag_filename
)
1143 dptr
->newtable
= allsubentries
;
1144 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1146 current_entry_count
++;
1150 dptr
->newtable
= navibrowse
;
1151 dptr
->name
= str(LANG_TAGNAVI_RANDOM
);
1152 dptr
->extraseek
= -1;
1154 current_entry_count
++;
1156 special_entry_count
+=2;
1159 total_count
+= special_entry_count
;
1161 while (tagcache_get_next(tcs
))
1163 if (total_count
++ < offset
)
1166 dptr
->newtable
= navibrowse
;
1167 if (tag
== tag_title
|| tag
== tag_filename
)
1169 dptr
->newtable
= playtrack
;
1170 dptr
->extraseek
= tcs
->idx_id
;
1173 dptr
->extraseek
= tcs
->result_seek
;
1176 /* Check the format */
1177 for (i
= 0; i
< format_count
; i
++)
1179 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1182 if (tagcache_check_clauses(tcs
, formats
[i
]->clause
,
1183 formats
[i
]->clause_count
))
1190 if (!tcs
->ramresult
|| fmt
)
1196 if (format_str(tcs
, fmt
, buf
, sizeof buf
) < 0)
1198 logf("format_str() failed");
1203 dptr
->name
= &c
->name_buffer
[namebufused
];
1205 namebufused
+= strlen(buf
)+1;
1207 namebufused
+= tcs
->result_len
;
1209 if (namebufused
>= c
->name_buffer_size
)
1211 logf("chunk mode #2: %d", current_entry_count
);
1217 strcpy(dptr
->name
, buf
);
1219 strcpy(dptr
->name
, tcs
->result
);
1222 dptr
->name
= tcs
->result
;
1225 current_entry_count
++;
1227 if (current_entry_count
>= global_settings
.max_files_in_dir
)
1229 logf("chunk mode #3: %d", current_entry_count
);
1235 if (init
&& !tcs
->ramsearch
)
1237 if (!show_search_progress(false, total_count
))
1239 tagcache_search_finish(tcs
);
1240 return current_entry_count
;
1246 qsort(c
->dircache
+ special_entry_count
* c
->dentry_size
,
1247 current_entry_count
- special_entry_count
,
1248 c
->dentry_size
, compare
);
1252 tagcache_search_finish(tcs
);
1253 return current_entry_count
;
1256 while (tagcache_get_next(tcs
))
1258 if (!tcs
->ramsearch
)
1260 if (!show_search_progress(false, total_count
))
1266 tagcache_search_finish(tcs
);
1268 if (!sort
&& (sort_inverse
|| sort_limit
))
1270 splashf(HZ
*4, ID2P(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1271 logf("Too small dir buffer");
1276 total_count
= MIN(total_count
, sort_limit
);
1281 for (i
= 0; i
< total_count
; i
++, dptr
++)
1283 int len
= strlen(dptr
->name
);
1288 dptr
->name
= &dptr
->name
[strip
];
1295 static int load_root(struct tree_context
*c
)
1297 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1301 c
->currtable
= root
;
1302 if (c
->dirlevel
== 0)
1303 c
->currextra
= root_menu
;
1305 menu
= menus
[c
->currextra
];
1309 for (i
= 0; i
< menu
->itemcount
; i
++)
1311 dptr
->name
= menu
->items
[i
]->name
;
1312 switch (menu
->items
[i
]->type
)
1315 dptr
->newtable
= navibrowse
;
1316 dptr
->extraseek
= i
;
1320 dptr
->newtable
= root
;
1321 dptr
->extraseek
= menu
->items
[i
]->link
;
1329 current_entry_count
= i
;
1334 int tagtree_load(struct tree_context
* c
)
1337 int table
= c
->currtable
;
1339 c
->dentry_size
= sizeof(struct tagentry
);
1346 c
->currtable
= table
;
1347 c
->currextra
= root_menu
;
1353 count
= load_root(c
);
1358 logf("navibrowse...");
1360 count
= retrieve_entries(c
, &tcs
, 0, true);
1365 logf("Unsupported table %d\n", table
);
1372 count
= load_root(c
);
1373 splash(HZ
, str(LANG_TAGCACHE_BUSY
));
1376 /* The _total_ numer of entries available. */
1377 c
->dirlength
= c
->filesindir
= count
;
1382 int tagtree_enter(struct tree_context
* c
)
1385 struct tagentry
*dptr
;
1386 struct mp3entry
*id3
;
1391 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1394 seek
= dptr
->extraseek
;
1397 if(c
->filesindir
<=2)
1399 srand(current_tick
);
1400 dptr
= (tagtree_get_entry(c
, 2+(rand() % (c
->filesindir
-2))));
1401 seek
= dptr
->extraseek
;
1403 newextra
= dptr
->newtable
;
1405 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1408 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1409 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1410 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1411 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1414 switch (c
->currtable
) {
1416 c
->currextra
= newextra
;
1418 if (newextra
== root
)
1421 c
->currextra
= seek
;
1424 else if (newextra
== navibrowse
)
1428 csi
= menu
->items
[seek
]->si
;
1431 strncpy(current_title
[c
->currextra
], dptr
->name
,
1432 sizeof(current_title
[0]) - 1);
1434 /* Read input as necessary. */
1435 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1437 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1440 source
= csi
->clause
[i
][j
]->source
;
1442 if (source
== source_constant
)
1445 searchstring
=csi
->clause
[i
][j
]->str
;
1446 *searchstring
= '\0';
1448 id3
= audio_current_track();
1450 if (source
== source_current_path
&& id3
)
1453 strncpy(searchstring
, id3
->path
, SEARCHSTR_SIZE
);
1454 e
= strrchr(searchstring
, '/');
1458 else if (source
> source_runtime
&& id3
)
1461 int k
= source
-source_runtime
;
1462 int offset
= id3_to_search_mapping
[k
].id3_offset
;
1463 char **src
= (char**)((char*)id3
+ offset
);
1466 strncpy(searchstring
, *src
, SEARCHSTR_SIZE
);
1467 searchstring
[SEARCHSTR_SIZE
-1] = '\0';
1472 rc
= kbd_input(searchstring
, SEARCHSTR_SIZE
);
1473 if (rc
== -1 || !searchstring
[0])
1478 if (csi
->clause
[i
][j
]->numeric
)
1479 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1486 c
->currtable
= newextra
;
1492 if (newextra
== playtrack
)
1494 if (global_settings
.party_mode
&& audio_status()) {
1495 splash(HZ
, ID2P(LANG_PARTY_MODE
));
1499 /* about to create a new current playlist...
1500 allow user to cancel the operation */
1501 if (!warn_on_pl_erase())
1504 if (tagtree_play_folder(c
) >= 0)
1509 c
->currtable
= newextra
;
1510 csi
->result_seek
[c
->currextra
] = seek
;
1511 if (c
->currextra
< csi
->tagorder_count
-1)
1516 /* Update the statusbar title */
1517 strncpy(current_title
[c
->currextra
], dptr
->name
,
1518 sizeof(current_title
[0]) - 1);
1527 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1532 void tagtree_exit(struct tree_context
* c
)
1535 if (c
->dirlevel
> 0)
1537 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1538 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1539 c
->currtable
= c
->table_history
[c
->dirlevel
];
1540 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1541 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1544 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1546 struct tagentry
*entry
;
1548 entry
= tagtree_get_entry(c
, c
->selected_item
);
1550 if (!tagcache_search(&tcs
, tag_filename
))
1553 if (!tagcache_retrieve(&tcs
, entry
->extraseek
, tcs
.type
, buf
, buflen
))
1555 tagcache_search_finish(&tcs
);
1559 tagcache_search_finish(&tcs
);
1564 static bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1568 int from
, to
, direction
;
1569 int files_left
= c
->filesindir
;
1572 if (!tagcache_search(&tcs
, tag_filename
))
1574 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
1579 if (position
== PLAYLIST_REPLACE
)
1581 if (playlist_remove_all_tracks(NULL
) == 0)
1582 position
= PLAYLIST_INSERT_LAST
;
1590 if (position
== PLAYLIST_INSERT_FIRST
)
1592 from
= c
->filesindir
- 1;
1603 for (i
= from
; i
!= to
; i
+= direction
)
1605 /* Count back to zero */
1606 if (!show_search_progress(false, files_left
--))
1609 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1610 tcs
.type
, buf
, sizeof buf
))
1615 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1617 logf("playlist_insert_track failed");
1622 playlist_sync(NULL
);
1623 tagcache_search_finish(&tcs
);
1629 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1631 struct tagentry
*dptr
;
1633 int dirlevel
= tc
->dirlevel
;
1635 /* We need to set the table to allsubentries. */
1636 show_search_progress(true, 0);
1638 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1640 /* Insert a single track? */
1641 if (dptr
->newtable
== playtrack
)
1643 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1645 logf("tagtree_get_filename failed");
1648 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1653 if (dptr
->newtable
== navibrowse
)
1657 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1659 else if (dptr
->newtable
!= allsubentries
)
1661 logf("unsupported table: %d", dptr
->newtable
);
1665 /* Now the current table should be allsubentries. */
1666 if (dptr
->newtable
!= playtrack
)
1670 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1672 /* And now the newtable should be playtrack. */
1673 if (dptr
->newtable
!= playtrack
)
1675 logf("newtable: %d !!", dptr
->newtable
);
1676 tc
->dirlevel
= dirlevel
;
1681 if (tc
->filesindir
<= 0)
1682 splash(HZ
, ID2P(LANG_END_PLAYLIST
));
1685 logf("insert_all_playlist");
1686 if (!insert_all_playlist(tc
, position
, queue
))
1687 splash(HZ
*2, ID2P(LANG_FAILED
));
1690 /* Finally return the dirlevel to its original value. */
1691 while (tc
->dirlevel
> dirlevel
)
1698 static int tagtree_play_folder(struct tree_context
* c
)
1700 if (playlist_create(NULL
, NULL
) < 0)
1702 logf("Failed creating playlist\n");
1706 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1709 if (global_settings
.playlist_shuffle
)
1710 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
1711 if (!global_settings
.play_selected
)
1712 c
->selected_item
= 0;
1713 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1715 playlist_start(c
->selected_item
,0);
1720 struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
1722 struct tagentry
*entry
= (struct tagentry
*)c
->dircache
;
1723 int realid
= id
- current_offset
;
1725 /* Load the next chunk if necessary. */
1726 if (realid
>= current_entry_count
|| realid
< 0)
1729 if (retrieve_entries(c
, &tcs2
, MAX(0, id
- (current_entry_count
/ 2)),
1732 logf("retrieve failed");
1736 realid
= id
- current_offset
;
1740 return &entry
[realid
];
1743 char *tagtree_get_title(struct tree_context
* c
)
1745 switch (c
->currtable
)
1752 return current_title
[c
->currextra
];
1758 int tagtree_get_attr(struct tree_context
* c
)
1761 switch (c
->currtable
)
1764 if (csi
->tagorder
[c
->currextra
] == tag_title
)
1765 attr
= FILE_ATTR_AUDIO
;
1767 attr
= ATTR_DIRECTORY
;
1771 attr
= FILE_ATTR_AUDIO
;
1775 attr
= ATTR_DIRECTORY
;
1782 int tagtree_get_icon(struct tree_context
* c
)
1784 int icon
= Icon_Folder
;
1786 if (tagtree_get_attr(c
) == FILE_ATTR_AUDIO
)