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 */
51 #include "filetypes.h"
53 #include "appevents.h"
55 #include "dir_uncached.h"
57 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
59 static int tagtree_play_folder(struct tree_context
* c
);
61 #define SEARCHSTR_SIZE 256
70 static const struct id3_to_search_mapping
{
73 } id3_to_search_mapping
[] = {
74 { "", 0 }, /* offset n/a */
75 { "#directory#", 0 }, /* offset n/a */
76 { "#title#", offsetof(struct mp3entry
, title
) },
77 { "#artist#", offsetof(struct mp3entry
, artist
) },
78 { "#album#", offsetof(struct mp3entry
, album
) },
79 { "#genre#", offsetof(struct mp3entry
, genre_string
) },
80 { "#composer#", offsetof(struct mp3entry
, composer
) },
81 { "#albumartist#", offsetof(struct mp3entry
, albumartist
) },
95 /* Capacity 10 000 entries (for example 10k different artists) */
96 #define UNIQBUF_SIZE (64*1024)
100 #define MAX_MENU_ID_SIZE 32
102 static bool sort_inverse
;
105 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
108 * formatstr = "%-3d. %s"
109 * tags[0] = tag_autoscore
110 * tags[1] = tag_title
114 * sort_inverse = true
116 struct display_format
{
118 struct tagcache_search_clause
*clause
[TAGCACHE_MAX_CLAUSES
];
130 static struct display_format
*formats
[TAGMENU_MAX_FMTS
];
131 static int format_count
;
133 struct search_instruction
{
135 int tagorder
[MAX_TAGS
];
137 struct tagcache_search_clause
*clause
[MAX_TAGS
][TAGCACHE_MAX_CLAUSES
];
138 int format_id
[MAX_TAGS
];
139 int clause_count
[MAX_TAGS
];
140 int result_seek
[MAX_TAGS
];
146 struct search_instruction
*si
;
152 char id
[MAX_MENU_ID_SIZE
];
154 struct menu_entry
*items
[TAGMENU_MAX_ITEMS
];
157 /* Statusbar text of the current view. */
158 static char current_title
[MAX_TAGS
][128];
160 static struct menu_root
*menus
[TAGMENU_MAX_MENUS
];
161 static struct menu_root
*menu
;
162 static struct search_instruction
*csi
;
163 static const char *strp
;
164 static int menu_count
;
167 static int current_offset
;
168 static int current_entry_count
;
170 static struct tree_context
*tc
;
172 static int get_token_str(char *buf
, int size
)
174 /* Find the start. */
175 while (*strp
!= '"' && *strp
!= '\0')
178 if (*strp
== '\0' || *(++strp
) == '\0')
182 while (*strp
!= '"' && *strp
!= '\0' && --size
> 0)
183 *(buf
++) = *(strp
++);
194 #define MATCH(tag,str1,str2,settag) \
195 if (!strcasecmp(str1, str2)) { \
200 static int get_tag(int *tag
)
205 /* Find the start. */
206 while ((*strp
== ' ' || *strp
== '>') && *strp
!= '\0')
209 if (*strp
== '\0' || *strp
== '?')
212 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
214 if (*strp
== '\0' || *strp
== ' ')
221 MATCH(tag
, buf
, "album", tag_album
);
222 MATCH(tag
, buf
, "artist", tag_artist
);
223 MATCH(tag
, buf
, "bitrate", tag_bitrate
);
224 MATCH(tag
, buf
, "composer", tag_composer
);
225 MATCH(tag
, buf
, "comment", tag_comment
);
226 MATCH(tag
, buf
, "albumartist", tag_albumartist
);
227 MATCH(tag
, buf
, "ensemble", tag_albumartist
);
228 MATCH(tag
, buf
, "grouping", tag_grouping
);
229 MATCH(tag
, buf
, "genre", tag_genre
);
230 MATCH(tag
, buf
, "length", tag_length
);
231 MATCH(tag
, buf
, "Lm", tag_virt_length_min
);
232 MATCH(tag
, buf
, "Ls", tag_virt_length_sec
);
233 MATCH(tag
, buf
, "Pm", tag_virt_playtime_min
);
234 MATCH(tag
, buf
, "Ps", tag_virt_playtime_sec
);
235 MATCH(tag
, buf
, "title", tag_title
);
236 MATCH(tag
, buf
, "filename", tag_filename
);
237 MATCH(tag
, buf
, "tracknum", tag_tracknumber
);
238 MATCH(tag
, buf
, "discnum", tag_discnumber
);
239 MATCH(tag
, buf
, "year", tag_year
);
240 MATCH(tag
, buf
, "playcount", tag_playcount
);
241 MATCH(tag
, buf
, "rating", tag_rating
);
242 MATCH(tag
, buf
, "lastplayed", tag_lastplayed
);
243 MATCH(tag
, buf
, "commitid", tag_commitid
);
244 MATCH(tag
, buf
, "entryage", tag_virt_entryage
);
245 MATCH(tag
, buf
, "autoscore", tag_virt_autoscore
);
246 MATCH(tag
, buf
, "%sort", var_sorttype
);
247 MATCH(tag
, buf
, "%limit", var_limit
);
248 MATCH(tag
, buf
, "%strip", var_strip
);
249 MATCH(tag
, buf
, "%menu_start", var_menu_start
);
250 MATCH(tag
, buf
, "%include", var_include
);
251 MATCH(tag
, buf
, "%root_menu", var_rootmenu
);
252 MATCH(tag
, buf
, "%format", var_format
);
253 MATCH(tag
, buf
, "->", menu_next
);
254 MATCH(tag
, buf
, "==>", menu_load
);
256 logf("NO MATCH: %s\n", buf
);
263 static int get_clause(int *condition
)
268 /* Find the start. */
269 while (*strp
== ' ' && *strp
!= '\0')
275 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
277 if (*strp
== '\0' || *strp
== ' ')
284 MATCH(condition
, buf
, "=", clause_is
);
285 MATCH(condition
, buf
, "==", clause_is
);
286 MATCH(condition
, buf
, "!=", clause_is_not
);
287 MATCH(condition
, buf
, ">", clause_gt
);
288 MATCH(condition
, buf
, ">=", clause_gteq
);
289 MATCH(condition
, buf
, "<", clause_lt
);
290 MATCH(condition
, buf
, "<=", clause_lteq
);
291 MATCH(condition
, buf
, "~", clause_contains
);
292 MATCH(condition
, buf
, "!~", clause_not_contains
);
293 MATCH(condition
, buf
, "^", clause_begins_with
);
294 MATCH(condition
, buf
, "!^", clause_not_begins_with
);
295 MATCH(condition
, buf
, "$", clause_ends_with
);
296 MATCH(condition
, buf
, "!$", clause_not_ends_with
);
297 MATCH(condition
, buf
, "@", clause_oneof
);
302 static bool read_clause(struct tagcache_search_clause
*clause
)
304 char buf
[SEARCHSTR_SIZE
];
307 if (get_tag(&clause
->tag
) <= 0)
310 if (get_clause(&clause
->type
) <= 0)
313 if (get_token_str(buf
, sizeof buf
) < 0)
316 for (i
=0; i
<ARRAYLEN(id3_to_search_mapping
); i
++)
318 if (!strcasecmp(buf
, id3_to_search_mapping
[i
].string
))
322 if (i
<ARRAYLEN(id3_to_search_mapping
)) /* runtime search operand found */
324 clause
->source
= source_runtime
+i
;
325 clause
->str
= buffer_alloc(SEARCHSTR_SIZE
);
329 clause
->source
= source_constant
;
330 clause
->str
= buffer_alloc(strlen(buf
)+1);
331 strcpy(clause
->str
, buf
);
334 if (TAGCACHE_IS_NUMERIC(clause
->tag
))
336 clause
->numeric
= true;
337 clause
->numeric_data
= atoi(clause
->str
);
340 clause
->numeric
= false;
342 logf("got clause: %d/%d [%s]", clause
->tag
, clause
->type
, clause
->str
);
347 static bool read_variable(char *buf
, int size
)
351 if (!get_clause(&condition
))
354 if (condition
!= clause_is
)
357 if (get_token_str(buf
, size
) < 0)
363 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
364 static int get_format_str(struct display_format
*fmt
)
370 memset(fmt
, 0, sizeof(struct display_format
));
372 if (get_token_str(fmt
->name
, sizeof fmt
->name
) < 0)
375 /* Determine the group id */
377 for (i
= 0; i
< format_count
; i
++)
379 if (!strcasecmp(formats
[i
]->name
, fmt
->name
))
381 fmt
->group_id
= formats
[i
]->group_id
;
385 if (formats
[i
]->group_id
> fmt
->group_id
)
386 fmt
->group_id
= formats
[i
]->group_id
;
389 if (i
== format_count
)
392 logf("format: (%d) %s", fmt
->group_id
, fmt
->name
);
394 if (get_token_str(buf
, sizeof buf
) < 0)
397 fmt
->formatstr
= buffer_alloc(strlen(buf
) + 1);
398 strcpy(fmt
->formatstr
, buf
);
400 while (fmt
->tag_count
< MAX_TAGS
)
402 ret
= get_tag(&fmt
->tags
[fmt
->tag_count
]);
409 switch (fmt
->tags
[fmt
->tag_count
]) {
411 if (!read_variable(buf
, sizeof buf
))
413 if (!strcasecmp("inverse", buf
))
414 fmt
->sort_inverse
= true;
418 if (!read_variable(buf
, sizeof buf
))
420 fmt
->limit
= atoi(buf
);
424 if (!read_variable(buf
, sizeof buf
))
426 fmt
->strip
= atoi(buf
);
437 static int add_format(const char *buf
)
441 if (formats
[format_count
] == NULL
)
442 formats
[format_count
] = buffer_alloc(sizeof(struct display_format
));
444 memset(formats
[format_count
], 0, sizeof(struct display_format
));
445 if (get_format_str(formats
[format_count
]) < 0)
447 logf("get_format_str() parser failed!");
451 while (*strp
!= '\0' && *strp
!= '?')
456 int clause_count
= 0;
461 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
463 logf("too many clauses");
467 formats
[format_count
]->clause
[clause_count
] =
468 buffer_alloc(sizeof(struct tagcache_search_clause
));
470 if (!read_clause(formats
[format_count
]->clause
[clause_count
]))
476 formats
[format_count
]->clause_count
= clause_count
;
484 static int get_condition(struct search_instruction
*inst
)
495 if (get_token_str(buf
, sizeof buf
) < 0)
498 for (i
= 0; i
< format_count
; i
++)
500 if (!strcasecmp(formats
[i
]->name
, buf
))
504 if (i
== format_count
)
506 logf("format not found: %s", buf
);
510 inst
->format_id
[inst
->tagorder_count
] = formats
[i
]->group_id
;
523 clause_count
= inst
->clause_count
[inst
->tagorder_count
];
524 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
526 logf("Too many clauses");
530 inst
->clause
[inst
->tagorder_count
][clause_count
] =
531 buffer_alloc(sizeof(struct tagcache_search_clause
));
533 if (!read_clause(inst
->clause
[inst
->tagorder_count
][clause_count
]))
536 inst
->clause_count
[inst
->tagorder_count
]++;
542 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
543 * : album ? year >= "2000" : songs
549 static bool parse_search(struct menu_entry
*entry
, const char *str
)
553 struct search_instruction
*inst
= entry
->si
;
556 struct menu_root
*new_menu
;
560 /* Parse entry name */
561 if (get_token_str(entry
->name
, sizeof entry
->name
) < 0)
563 logf("No name found.");
567 /* Parse entry type */
568 if (get_tag(&entry
->type
) <= 0)
571 if (entry
->type
== menu_load
)
573 if (get_token_str(buf
, sizeof buf
) < 0)
576 /* Find the matching root menu or "create" it */
577 for (i
= 0; i
< menu_count
; i
++)
579 if (!strcasecmp(menus
[i
]->id
, buf
))
586 if (menu_count
>= TAGMENU_MAX_MENUS
)
588 logf("max menucount reached");
592 /* Allocate a new menu unless link is found. */
593 menus
[menu_count
] = buffer_alloc(sizeof(struct menu_root
));
594 new_menu
= menus
[menu_count
];
595 memset(new_menu
, 0, sizeof(struct menu_root
));
596 strlcpy(new_menu
->id
, buf
, MAX_MENU_ID_SIZE
);
597 entry
->link
= menu_count
;
603 if (entry
->type
!= menu_next
)
606 while (inst
->tagorder_count
< MAX_TAGS
)
608 ret
= get_tag(&inst
->tagorder
[inst
->tagorder_count
]);
611 logf("Parse error #1");
619 logf("tag: %d", inst
->tagorder
[inst
->tagorder_count
]);
621 while ( (ret
= get_condition(inst
)) > 0 ) ;
625 inst
->tagorder_count
++;
627 if (get_tag(&type
) <= 0 || type
!= menu_next
)
634 static int compare(const void *p1
, const void *p2
)
636 struct tagentry
*e1
= (struct tagentry
*)p1
;
637 struct tagentry
*e2
= (struct tagentry
*)p2
;
640 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
642 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
645 static void tagtree_buffer_event(struct mp3entry
*id3
)
647 struct tagcache_search tcs
;
649 /* Do not gather data unless proper setting has been enabled. */
650 if (!global_settings
.runtimedb
)
653 logf("be:%s", id3
->path
);
655 if (!tagcache_find_index(&tcs
, id3
->path
))
657 logf("tc stat: not found: %s", id3
->path
);
661 id3
->playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
663 id3
->rating
= tagcache_get_numeric(&tcs
, tag_rating
);
664 id3
->lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
665 id3
->score
= tagcache_get_numeric(&tcs
, tag_virt_autoscore
) / 10;
666 id3
->playtime
= tagcache_get_numeric(&tcs
, tag_playtime
);
668 /* Store our tagcache index pointer. */
669 id3
->tagcache_idx
= tcs
.idx_id
+1;
671 tagcache_search_finish(&tcs
);
674 static void tagtree_track_finish_event(struct mp3entry
*id3
)
681 /* Do not gather data unless proper setting has been enabled. */
682 if (!global_settings
.runtimedb
)
684 logf("runtimedb gathering not enabled");
688 tagcache_idx
=id3
->tagcache_idx
;
691 logf("No tagcache index pointer found");
696 /* Don't process unplayed tracks. */
697 if (id3
->elapsed
== 0)
699 logf("not logging unplayed track");
703 playcount
= id3
->playcount
+ 1;
704 lastplayed
= tagcache_increase_serial();
707 logf("incorrect tc serial:%ld", lastplayed
);
711 /* Ignore the last 15s (crossfade etc.) */
712 playtime
= id3
->playtime
+ MIN(id3
->length
, id3
->elapsed
+ 15 * 1000);
714 logf("ube:%s", id3
->path
);
715 logf("-> %ld/%ld", playcount
, playtime
);
716 logf("-> %ld/%ld/%ld", id3
->elapsed
, id3
->length
, MIN(id3
->length
, id3
->elapsed
+ 15 * 1000));
718 /* Queue the updates to the tagcache system. */
719 tagcache_update_numeric(tagcache_idx
, tag_playcount
, playcount
);
720 tagcache_update_numeric(tagcache_idx
, tag_playtime
, playtime
);
721 tagcache_update_numeric(tagcache_idx
, tag_lastplayed
, lastplayed
);
724 bool tagtree_export(void)
726 struct tagcache_search tcs
;
728 splash(0, str(LANG_CREATING
));
729 if (!tagcache_create_changelog(&tcs
))
731 splash(HZ
*2, ID2P(LANG_FAILED
));
737 bool tagtree_import(void)
739 splash(0, ID2P(LANG_WAIT
));
740 if (!tagcache_import_changelog())
742 splash(HZ
*2, ID2P(LANG_FAILED
));
748 static bool parse_menu(const char *filename
);
750 static int parse_line(int n
, const char *buf
, void *parameters
)
754 static bool read_menu
;
759 logf("parse:%d/%s", n
, buf
);
761 /* First line, do initialisation. */
764 if (strcasecmp(TAGNAVI_VERSION
, buf
))
766 logf("Version mismatch");
789 if (get_tag(&variable
) <= 0)
795 if (add_format(strp
) < 0)
797 logf("Format add fail: %s", data
);
802 if (get_token_str(data
, sizeof(data
)) < 0)
804 logf("%%include empty");
808 if (!parse_menu(data
))
810 logf("Load menu fail: %s", data
);
815 if (menu_count
>= TAGMENU_MAX_MENUS
)
817 logf("max menucount reached");
821 if (get_token_str(data
, sizeof data
) < 0)
823 logf("%%menu_start id empty");
828 for (i
= 0; i
< menu_count
; i
++)
830 if (!strcasecmp(menus
[i
]->id
, data
))
838 menus
[menu_count
] = buffer_alloc(sizeof(struct menu_root
));
839 menu
= menus
[menu_count
];
841 memset(menu
, 0, sizeof(struct menu_root
));
842 strlcpy(menu
->id
, data
, MAX_MENU_ID_SIZE
);
845 if (get_token_str(menu
->title
, sizeof(menu
->title
)) < 0)
847 logf("%%menu_start title empty");
850 logf("menu: %s", menu
->title
);
855 /* Only set root menu once. */
859 if (get_token_str(data
, sizeof(data
)) < 0)
861 logf("%%rootmenu empty");
865 for (i
= 0; i
< menu_count
; i
++)
867 if (!strcasecmp(menus
[i
]->id
, data
))
878 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
880 logf("max itemcount reached");
885 if (menu
->items
[menu
->itemcount
] == NULL
)
887 menu
->items
[menu
->itemcount
] = buffer_alloc(sizeof(struct menu_entry
));
888 memset(menu
->items
[menu
->itemcount
], 0, sizeof(struct menu_entry
));
889 menu
->items
[menu
->itemcount
]->si
= buffer_alloc(sizeof(struct search_instruction
));
892 memset(menu
->items
[menu
->itemcount
]->si
, 0, sizeof(struct search_instruction
));
893 if (!parse_search(menu
->items
[menu
->itemcount
], buf
))
901 static bool parse_menu(const char *filename
)
906 if (menu_count
>= TAGMENU_MAX_MENUS
)
908 logf("max menucount reached");
912 fd
= open(filename
, O_RDONLY
);
915 logf("Search instruction file not found.");
919 /* Now read file for real, parsing into si */
920 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
926 void tagtree_init(void)
932 parse_menu(FILE_SEARCH_INSTRUCTIONS
);
934 /* If no root menu is set, assume it's the first single menu
935 * we have. That shouldn't normally happen. */
939 uniqbuf
= buffer_alloc(UNIQBUF_SIZE
);
941 add_event(PLAYBACK_EVENT_TRACK_BUFFER
, false, tagtree_buffer_event
);
942 add_event(PLAYBACK_EVENT_TRACK_FINISH
, false, tagtree_track_finish_event
);
945 static bool show_search_progress(bool init
, int count
)
947 static int last_tick
= 0;
949 /* Don't show splashes for 1/2 second after starting search */
952 last_tick
= current_tick
+ HZ
/2;
956 /* Update progress every 1/10 of a second */
957 if (TIME_AFTER(current_tick
, last_tick
+ HZ
/10))
959 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG
), count
, str(LANG_OFF_ABORT
));
960 if (action_userabort(TIMEOUT_NOBLOCK
))
962 last_tick
= current_tick
;
969 static int format_str(struct tagcache_search
*tcs
, struct display_format
*fmt
,
970 char *buf
, int buf_size
)
973 bool read_format
= false;
979 memset(buf
, 0, buf_size
);
980 for (i
= 0; fmt
->formatstr
[i
] != '\0'; i
++)
982 if (fmt
->formatstr
[i
] == '%')
986 if (parpos
>= fmt
->tag_count
)
988 logf("too many format tags");
995 fmtbuf
[fmtbuf_pos
++] = fmt
->formatstr
[i
];
996 if (fmtbuf_pos
>= buf_size
)
998 logf("format parse error");
1002 if (fmt
->formatstr
[i
] == 's')
1004 fmtbuf
[fmtbuf_pos
] = '\0';
1005 read_format
= false;
1006 if (fmt
->tags
[parpos
] == tcs
->type
)
1008 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
, tcs
->result
);
1012 /* Need to fetch the tag data. */
1013 if (!tagcache_retrieve(tcs
, tcs
->idx_id
, fmt
->tags
[parpos
],
1014 &buf
[buf_pos
], buf_size
- buf_pos
))
1016 logf("retrieve failed");
1020 buf_pos
+= strlen(&buf
[buf_pos
]);
1023 else if (fmt
->formatstr
[i
] == 'd')
1025 fmtbuf
[fmtbuf_pos
] = '\0';
1026 read_format
= false;
1027 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
,
1028 tagcache_get_numeric(tcs
, fmt
->tags
[parpos
]));
1029 buf_pos
+= strlen(&buf
[buf_pos
]);
1035 buf
[buf_pos
++] = fmt
->formatstr
[i
];
1037 if (buf_pos
- 1 >= buf_size
)
1039 logf("buffer overflow");
1044 buf
[buf_pos
++] = '\0';
1049 static int retrieve_entries(struct tree_context
*c
, int offset
, bool init
)
1051 struct tagcache_search tcs
;
1052 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1053 struct display_format
*fmt
;
1055 int namebufused
= 0;
1056 int total_count
= 0;
1057 int special_entry_count
= 0;
1058 int level
= c
->currextra
;
1064 /* Show search progress straight away if the disk needs to spin up,
1065 otherwise show it after the normal 1/2 second delay */
1066 show_search_progress(
1067 #ifdef HAVE_DISK_STORAGE
1068 storage_disk_is_active()
1074 if (c
->currtable
== ALLSUBENTRIES
)
1080 tag
= csi
->tagorder
[level
];
1082 if (!tagcache_search(&tcs
, tag
))
1085 /* Prevent duplicate entries in the search list. */
1086 tagcache_search_set_uniqbuf(&tcs
, uniqbuf
, UNIQBUF_SIZE
);
1088 if (level
|| csi
->clause_count
[0] || TAGCACHE_IS_NUMERIC(tag
))
1091 for (i
= 0; i
< level
; i
++)
1093 if (TAGCACHE_IS_NUMERIC(csi
->tagorder
[i
]))
1095 static struct tagcache_search_clause cc
;
1097 memset(&cc
, 0, sizeof(struct tagcache_search_clause
));
1098 cc
.tag
= csi
->tagorder
[i
];
1099 cc
.type
= clause_is
;
1101 cc
.numeric_data
= csi
->result_seek
[i
];
1102 tagcache_search_add_clause(&tcs
, &cc
);
1106 tagcache_search_add_filter(&tcs
, csi
->tagorder
[i
],
1107 csi
->result_seek
[i
]);
1111 for (i
= 0; i
<= level
; i
++)
1115 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1116 tagcache_search_add_clause(&tcs
, csi
->clause
[i
][j
]);
1119 current_offset
= offset
;
1120 current_entry_count
= 0;
1124 for (i
= 0; i
< format_count
; i
++)
1126 if (formats
[i
]->group_id
== csi
->format_id
[level
])
1132 sort_inverse
= fmt
->sort_inverse
;
1133 sort_limit
= fmt
->limit
;
1139 sort_inverse
= false;
1144 if (tag
!= tag_title
&& tag
!= tag_filename
)
1148 dptr
->newtable
= ALLSUBENTRIES
;
1149 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1151 current_entry_count
++;
1155 dptr
->newtable
= NAVIBROWSE
;
1156 dptr
->name
= str(LANG_TAGNAVI_RANDOM
);
1157 dptr
->extraseek
= -1;
1159 current_entry_count
++;
1161 special_entry_count
+=2;
1164 total_count
+= special_entry_count
;
1166 while (tagcache_get_next(&tcs
))
1168 if (total_count
++ < offset
)
1171 dptr
->newtable
= NAVIBROWSE
;
1172 if (tag
== tag_title
|| tag
== tag_filename
)
1174 dptr
->newtable
= PLAYTRACK
;
1175 dptr
->extraseek
= tcs
.idx_id
;
1178 dptr
->extraseek
= tcs
.result_seek
;
1181 /* Check the format */
1182 for (i
= 0; i
< format_count
; i
++)
1184 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1187 if (tagcache_check_clauses(&tcs
, formats
[i
]->clause
,
1188 formats
[i
]->clause_count
))
1195 if (!tcs
.ramresult
|| fmt
)
1201 if (format_str(&tcs
, fmt
, buf
, sizeof buf
) < 0)
1203 logf("format_str() failed");
1204 tagcache_search_finish(&tcs
);
1209 dptr
->name
= &c
->name_buffer
[namebufused
];
1211 namebufused
+= strlen(buf
)+1;
1213 namebufused
+= tcs
.result_len
;
1215 if (namebufused
>= c
->name_buffer_size
)
1217 logf("chunk mode #2: %d", current_entry_count
);
1223 strcpy(dptr
->name
, buf
);
1225 strcpy(dptr
->name
, tcs
.result
);
1228 dptr
->name
= tcs
.result
;
1231 current_entry_count
++;
1233 if (current_entry_count
>= global_settings
.max_files_in_dir
)
1235 logf("chunk mode #3: %d", current_entry_count
);
1241 if (init
&& !tcs
.ramsearch
)
1243 if (!show_search_progress(false, total_count
))
1244 { /* user aborted */
1245 tagcache_search_finish(&tcs
);
1246 return current_entry_count
;
1252 qsort(c
->dircache
+ special_entry_count
* c
->dentry_size
,
1253 current_entry_count
- special_entry_count
,
1254 c
->dentry_size
, compare
);
1258 tagcache_search_finish(&tcs
);
1259 return current_entry_count
;
1262 while (tagcache_get_next(&tcs
))
1266 if (!show_search_progress(false, total_count
))
1272 tagcache_search_finish(&tcs
);
1274 if (!sort
&& (sort_inverse
|| sort_limit
))
1276 splashf(HZ
*4, ID2P(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1277 logf("Too small dir buffer");
1282 total_count
= MIN(total_count
, sort_limit
);
1287 for (i
= 0; i
< total_count
; i
++, dptr
++)
1289 int len
= strlen(dptr
->name
);
1294 dptr
->name
= &dptr
->name
[strip
];
1302 static int load_root(struct tree_context
*c
)
1304 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1308 c
->currtable
= ROOT
;
1309 if (c
->dirlevel
== 0)
1310 c
->currextra
= rootmenu
;
1312 menu
= menus
[c
->currextra
];
1316 for (i
= 0; i
< menu
->itemcount
; i
++)
1318 dptr
->name
= menu
->items
[i
]->name
;
1319 switch (menu
->items
[i
]->type
)
1322 dptr
->newtable
= NAVIBROWSE
;
1323 dptr
->extraseek
= i
;
1327 dptr
->newtable
= ROOT
;
1328 dptr
->extraseek
= menu
->items
[i
]->link
;
1336 current_entry_count
= i
;
1341 int tagtree_load(struct tree_context
* c
)
1344 int table
= c
->currtable
;
1346 c
->dentry_size
= sizeof(struct tagentry
);
1353 c
->currtable
= table
;
1354 c
->currextra
= rootmenu
;
1360 count
= load_root(c
);
1365 logf("navibrowse...");
1367 count
= retrieve_entries(c
, 0, true);
1372 logf("Unsupported table %d\n", table
);
1379 count
= load_root(c
);
1380 splash(HZ
, str(LANG_TAGCACHE_BUSY
));
1383 /* The _total_ numer of entries available. */
1384 c
->dirlength
= c
->filesindir
= count
;
1389 int tagtree_enter(struct tree_context
* c
)
1392 struct tagentry
*dptr
;
1393 struct mp3entry
*id3
;
1398 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1401 seek
= dptr
->extraseek
;
1404 if(c
->filesindir
<=2)
1406 srand(current_tick
);
1407 dptr
= (tagtree_get_entry(c
, 2+(rand() % (c
->filesindir
-2))));
1408 seek
= dptr
->extraseek
;
1410 newextra
= dptr
->newtable
;
1412 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1415 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1416 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1417 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1418 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1421 switch (c
->currtable
) {
1423 c
->currextra
= newextra
;
1425 if (newextra
== ROOT
)
1428 c
->currextra
= seek
;
1431 else if (newextra
== NAVIBROWSE
)
1435 csi
= menu
->items
[seek
]->si
;
1438 strlcpy(current_title
[c
->currextra
], dptr
->name
,
1439 sizeof(current_title
[0]));
1441 /* Read input as necessary. */
1442 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1444 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1447 source
= csi
->clause
[i
][j
]->source
;
1449 if (source
== source_constant
)
1452 searchstring
=csi
->clause
[i
][j
]->str
;
1453 *searchstring
= '\0';
1455 id3
= audio_current_track();
1457 if (source
== source_current_path
&& id3
)
1460 strlcpy(searchstring
, id3
->path
, SEARCHSTR_SIZE
);
1461 e
= strrchr(searchstring
, '/');
1465 else if (source
> source_runtime
&& id3
)
1468 int k
= source
-source_runtime
;
1469 int offset
= id3_to_search_mapping
[k
].id3_offset
;
1470 char **src
= (char**)((char*)id3
+ offset
);
1473 strlcpy(searchstring
, *src
, SEARCHSTR_SIZE
);
1478 rc
= kbd_input(searchstring
, SEARCHSTR_SIZE
);
1479 if (rc
< 0 || !searchstring
[0])
1484 if (csi
->clause
[i
][j
]->numeric
)
1485 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1492 c
->currtable
= newextra
;
1498 if (newextra
== PLAYTRACK
)
1500 if (global_settings
.party_mode
&& audio_status()) {
1501 splash(HZ
, ID2P(LANG_PARTY_MODE
));
1505 /* about to create a new current playlist...
1506 allow user to cancel the operation */
1507 if (!warn_on_pl_erase())
1510 if (tagtree_play_folder(c
) >= 0)
1515 c
->currtable
= newextra
;
1516 csi
->result_seek
[c
->currextra
] = seek
;
1517 if (c
->currextra
< csi
->tagorder_count
-1)
1522 /* Update the statusbar title */
1523 strlcpy(current_title
[c
->currextra
], dptr
->name
,
1524 sizeof(current_title
[0]));
1533 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1538 void tagtree_exit(struct tree_context
* c
)
1541 if (c
->dirlevel
> 0)
1543 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1544 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1545 c
->currtable
= c
->table_history
[c
->dirlevel
];
1546 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1547 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1550 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1552 struct tagcache_search tcs
;
1553 struct tagentry
*entry
;
1555 entry
= tagtree_get_entry(c
, c
->selected_item
);
1557 if (!tagcache_search(&tcs
, tag_filename
))
1560 if (!tagcache_retrieve(&tcs
, entry
->extraseek
, tcs
.type
, buf
, buflen
))
1562 tagcache_search_finish(&tcs
);
1566 tagcache_search_finish(&tcs
);
1571 static bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1573 struct tagcache_search tcs
;
1576 int from
, to
, direction
;
1577 int files_left
= c
->filesindir
;
1580 if (!tagcache_search(&tcs
, tag_filename
))
1582 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
1587 if (position
== PLAYLIST_REPLACE
)
1589 if (playlist_remove_all_tracks(NULL
) == 0)
1590 position
= PLAYLIST_INSERT_LAST
;
1598 if (position
== PLAYLIST_INSERT_FIRST
)
1600 from
= c
->filesindir
- 1;
1611 for (i
= from
; i
!= to
; i
+= direction
)
1613 /* Count back to zero */
1614 if (!show_search_progress(false, files_left
--))
1617 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1618 tcs
.type
, buf
, sizeof buf
))
1623 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1625 logf("playlist_insert_track failed");
1630 playlist_sync(NULL
);
1631 tagcache_search_finish(&tcs
);
1637 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1639 struct tagentry
*dptr
;
1641 int dirlevel
= tc
->dirlevel
;
1643 show_search_progress(
1644 #ifdef HAVE_DISK_STORAGE
1645 storage_disk_is_active()
1652 /* We need to set the table to allsubentries. */
1653 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1655 /* Insert a single track? */
1656 if (dptr
->newtable
== PLAYTRACK
)
1658 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1660 logf("tagtree_get_filename failed");
1663 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1668 if (dptr
->newtable
== NAVIBROWSE
)
1672 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1674 else if (dptr
->newtable
!= ALLSUBENTRIES
)
1676 logf("unsupported table: %d", dptr
->newtable
);
1680 /* Now the current table should be allsubentries. */
1681 if (dptr
->newtable
!= PLAYTRACK
)
1685 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1687 /* And now the newtable should be playtrack. */
1688 if (dptr
->newtable
!= PLAYTRACK
)
1690 logf("newtable: %d !!", dptr
->newtable
);
1691 tc
->dirlevel
= dirlevel
;
1696 if (tc
->filesindir
<= 0)
1697 splash(HZ
, ID2P(LANG_END_PLAYLIST
));
1700 logf("insert_all_playlist");
1701 if (!insert_all_playlist(tc
, position
, queue
))
1702 splash(HZ
*2, ID2P(LANG_FAILED
));
1705 /* Finally return the dirlevel to its original value. */
1706 while (tc
->dirlevel
> dirlevel
)
1713 static int tagtree_play_folder(struct tree_context
* c
)
1715 if (playlist_create(NULL
, NULL
) < 0)
1717 logf("Failed creating playlist\n");
1721 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1724 if (global_settings
.playlist_shuffle
)
1725 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
1726 if (!global_settings
.play_selected
)
1727 c
->selected_item
= 0;
1728 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1730 playlist_start(c
->selected_item
,0);
1735 struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
1737 struct tagentry
*entry
= (struct tagentry
*)c
->dircache
;
1738 int realid
= id
- current_offset
;
1740 /* Load the next chunk if necessary. */
1741 if (realid
>= current_entry_count
|| realid
< 0)
1744 if (retrieve_entries(c
, MAX(0, id
- (current_entry_count
/ 2)),
1747 logf("retrieve failed");
1751 realid
= id
- current_offset
;
1755 return &entry
[realid
];
1758 char *tagtree_get_title(struct tree_context
* c
)
1760 switch (c
->currtable
)
1767 return current_title
[c
->currextra
];
1773 int tagtree_get_attr(struct tree_context
* c
)
1776 switch (c
->currtable
)
1779 if (csi
->tagorder
[c
->currextra
] == tag_title
)
1780 attr
= FILE_ATTR_AUDIO
;
1782 attr
= ATTR_DIRECTORY
;
1786 attr
= FILE_ATTR_AUDIO
;
1790 attr
= ATTR_DIRECTORY
;
1797 int tagtree_get_icon(struct tree_context
* c
)
1799 int icon
= Icon_Folder
;
1801 if (tagtree_get_attr(c
) == FILE_ATTR_AUDIO
)