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
];
159 /* Statusbar text of the current view. */
160 static char current_title
[MAX_TAGS
][128];
162 static struct menu_root
*menus
[TAGMENU_MAX_MENUS
];
163 static struct menu_root
*menu
;
164 static struct search_instruction
*csi
;
165 static const char *strp
;
166 static int menu_count
;
169 static int current_offset
;
170 static int current_entry_count
;
172 static struct tree_context
*tc
;
174 static int get_token_str(char *buf
, int size
)
176 /* Find the start. */
177 while (*strp
!= '"' && *strp
!= '\0')
180 if (*strp
== '\0' || *(++strp
) == '\0')
184 while (*strp
!= '"' && *strp
!= '\0' && --size
> 0)
185 *(buf
++) = *(strp
++);
196 #define MATCH(tag,str1,str2,settag) \
197 if (!strcasecmp(str1, str2)) { \
202 static int get_tag(int *tag
)
207 /* Find the start. */
208 while ((*strp
== ' ' || *strp
== '>') && *strp
!= '\0')
211 if (*strp
== '\0' || *strp
== '?')
214 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
216 if (*strp
== '\0' || *strp
== ' ')
223 MATCH(tag
, buf
, "album", tag_album
);
224 MATCH(tag
, buf
, "artist", tag_artist
);
225 MATCH(tag
, buf
, "bitrate", tag_bitrate
);
226 MATCH(tag
, buf
, "composer", tag_composer
);
227 MATCH(tag
, buf
, "comment", tag_comment
);
228 MATCH(tag
, buf
, "albumartist", tag_albumartist
);
229 MATCH(tag
, buf
, "ensemble", tag_albumartist
);
230 MATCH(tag
, buf
, "grouping", tag_grouping
);
231 MATCH(tag
, buf
, "genre", tag_genre
);
232 MATCH(tag
, buf
, "length", tag_length
);
233 MATCH(tag
, buf
, "Lm", tag_virt_length_min
);
234 MATCH(tag
, buf
, "Ls", tag_virt_length_sec
);
235 MATCH(tag
, buf
, "Pm", tag_virt_playtime_min
);
236 MATCH(tag
, buf
, "Ps", tag_virt_playtime_sec
);
237 MATCH(tag
, buf
, "title", tag_title
);
238 MATCH(tag
, buf
, "filename", tag_filename
);
239 MATCH(tag
, buf
, "tracknum", tag_tracknumber
);
240 MATCH(tag
, buf
, "discnum", tag_discnumber
);
241 MATCH(tag
, buf
, "year", tag_year
);
242 MATCH(tag
, buf
, "playcount", tag_playcount
);
243 MATCH(tag
, buf
, "rating", tag_rating
);
244 MATCH(tag
, buf
, "lastplayed", tag_lastplayed
);
245 MATCH(tag
, buf
, "lastoffset", tag_lastoffset
);
246 MATCH(tag
, buf
, "commitid", tag_commitid
);
247 MATCH(tag
, buf
, "entryage", tag_virt_entryage
);
248 MATCH(tag
, buf
, "autoscore", tag_virt_autoscore
);
249 MATCH(tag
, buf
, "%sort", var_sorttype
);
250 MATCH(tag
, buf
, "%limit", var_limit
);
251 MATCH(tag
, buf
, "%strip", var_strip
);
252 MATCH(tag
, buf
, "%menu_start", var_menu_start
);
253 MATCH(tag
, buf
, "%include", var_include
);
254 MATCH(tag
, buf
, "%root_menu", var_rootmenu
);
255 MATCH(tag
, buf
, "%format", var_format
);
256 MATCH(tag
, buf
, "->", menu_next
);
257 MATCH(tag
, buf
, "==>", menu_load
);
259 logf("NO MATCH: %s\n", buf
);
266 static int get_clause(int *condition
)
271 /* Find the start. */
272 while (*strp
== ' ' && *strp
!= '\0')
278 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
280 if (*strp
== '\0' || *strp
== ' ')
287 MATCH(condition
, buf
, "=", clause_is
);
288 MATCH(condition
, buf
, "==", clause_is
);
289 MATCH(condition
, buf
, "!=", clause_is_not
);
290 MATCH(condition
, buf
, ">", clause_gt
);
291 MATCH(condition
, buf
, ">=", clause_gteq
);
292 MATCH(condition
, buf
, "<", clause_lt
);
293 MATCH(condition
, buf
, "<=", clause_lteq
);
294 MATCH(condition
, buf
, "~", clause_contains
);
295 MATCH(condition
, buf
, "!~", clause_not_contains
);
296 MATCH(condition
, buf
, "^", clause_begins_with
);
297 MATCH(condition
, buf
, "!^", clause_not_begins_with
);
298 MATCH(condition
, buf
, "$", clause_ends_with
);
299 MATCH(condition
, buf
, "!$", clause_not_ends_with
);
300 MATCH(condition
, buf
, "@", clause_oneof
);
305 static bool read_clause(struct tagcache_search_clause
*clause
)
307 char buf
[SEARCHSTR_SIZE
];
310 if (get_tag(&clause
->tag
) <= 0)
313 if (get_clause(&clause
->type
) <= 0)
316 if (get_token_str(buf
, sizeof buf
) < 0)
319 for (i
=0; i
<ARRAYLEN(id3_to_search_mapping
); i
++)
321 if (!strcasecmp(buf
, id3_to_search_mapping
[i
].string
))
325 if (i
<ARRAYLEN(id3_to_search_mapping
)) /* runtime search operand found */
327 clause
->source
= source_runtime
+i
;
328 clause
->str
= buffer_alloc(SEARCHSTR_SIZE
);
332 clause
->source
= source_constant
;
333 clause
->str
= buffer_alloc(strlen(buf
)+1);
334 strcpy(clause
->str
, buf
);
337 if (TAGCACHE_IS_NUMERIC(clause
->tag
))
339 clause
->numeric
= true;
340 clause
->numeric_data
= atoi(clause
->str
);
343 clause
->numeric
= false;
345 logf("got clause: %d/%d [%s]", clause
->tag
, clause
->type
, clause
->str
);
350 static bool read_variable(char *buf
, int size
)
354 if (!get_clause(&condition
))
357 if (condition
!= clause_is
)
360 if (get_token_str(buf
, size
) < 0)
366 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
367 static int get_format_str(struct display_format
*fmt
)
373 memset(fmt
, 0, sizeof(struct display_format
));
375 if (get_token_str(fmt
->name
, sizeof fmt
->name
) < 0)
378 /* Determine the group id */
380 for (i
= 0; i
< format_count
; i
++)
382 if (!strcasecmp(formats
[i
]->name
, fmt
->name
))
384 fmt
->group_id
= formats
[i
]->group_id
;
388 if (formats
[i
]->group_id
> fmt
->group_id
)
389 fmt
->group_id
= formats
[i
]->group_id
;
392 if (i
== format_count
)
395 logf("format: (%d) %s", fmt
->group_id
, fmt
->name
);
397 if (get_token_str(buf
, sizeof buf
) < 0)
400 fmt
->formatstr
= buffer_alloc(strlen(buf
) + 1);
401 strcpy(fmt
->formatstr
, buf
);
403 while (fmt
->tag_count
< MAX_TAGS
)
405 ret
= get_tag(&fmt
->tags
[fmt
->tag_count
]);
412 switch (fmt
->tags
[fmt
->tag_count
]) {
414 if (!read_variable(buf
, sizeof buf
))
416 if (!strcasecmp("inverse", buf
))
417 fmt
->sort_inverse
= true;
421 if (!read_variable(buf
, sizeof buf
))
423 fmt
->limit
= atoi(buf
);
427 if (!read_variable(buf
, sizeof buf
))
429 fmt
->strip
= atoi(buf
);
440 static int add_format(const char *buf
)
444 if (formats
[format_count
] == NULL
)
445 formats
[format_count
] = buffer_alloc(sizeof(struct display_format
));
447 memset(formats
[format_count
], 0, sizeof(struct display_format
));
448 if (get_format_str(formats
[format_count
]) < 0)
450 logf("get_format_str() parser failed!");
454 while (*strp
!= '\0' && *strp
!= '?')
459 int clause_count
= 0;
464 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
466 logf("too many clauses");
470 formats
[format_count
]->clause
[clause_count
] =
471 buffer_alloc(sizeof(struct tagcache_search_clause
));
473 if (!read_clause(formats
[format_count
]->clause
[clause_count
]))
479 formats
[format_count
]->clause_count
= clause_count
;
487 static int get_condition(struct search_instruction
*inst
)
498 if (get_token_str(buf
, sizeof buf
) < 0)
501 for (i
= 0; i
< format_count
; i
++)
503 if (!strcasecmp(formats
[i
]->name
, buf
))
507 if (i
== format_count
)
509 logf("format not found: %s", buf
);
513 inst
->format_id
[inst
->tagorder_count
] = formats
[i
]->group_id
;
526 clause_count
= inst
->clause_count
[inst
->tagorder_count
];
527 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
529 logf("Too many clauses");
533 inst
->clause
[inst
->tagorder_count
][clause_count
] =
534 buffer_alloc(sizeof(struct tagcache_search_clause
));
536 if (!read_clause(inst
->clause
[inst
->tagorder_count
][clause_count
]))
539 inst
->clause_count
[inst
->tagorder_count
]++;
545 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
546 * : album ? year >= "2000" : songs
552 static bool parse_search(struct menu_entry
*entry
, const char *str
)
556 struct search_instruction
*inst
= entry
->si
;
559 struct menu_root
*new_menu
;
563 /* Parse entry name */
564 if (get_token_str(entry
->name
, sizeof entry
->name
) < 0)
566 logf("No name found.");
570 /* Parse entry type */
571 if (get_tag(&entry
->type
) <= 0)
574 if (entry
->type
== menu_load
)
576 if (get_token_str(buf
, sizeof buf
) < 0)
579 /* Find the matching root menu or "create" it */
580 for (i
= 0; i
< menu_count
; i
++)
582 if (!strcasecmp(menus
[i
]->id
, buf
))
589 if (menu_count
>= TAGMENU_MAX_MENUS
)
591 logf("max menucount reached");
595 /* Allocate a new menu unless link is found. */
596 menus
[menu_count
] = buffer_alloc(sizeof(struct menu_root
));
597 new_menu
= menus
[menu_count
];
598 memset(new_menu
, 0, sizeof(struct menu_root
));
599 strlcpy(new_menu
->id
, buf
, MAX_MENU_ID_SIZE
);
600 entry
->link
= menu_count
;
606 if (entry
->type
!= menu_next
)
609 while (inst
->tagorder_count
< MAX_TAGS
)
611 ret
= get_tag(&inst
->tagorder
[inst
->tagorder_count
]);
614 logf("Parse error #1");
622 logf("tag: %d", inst
->tagorder
[inst
->tagorder_count
]);
624 while ( (ret
= get_condition(inst
)) > 0 ) ;
628 inst
->tagorder_count
++;
630 if (get_tag(&type
) <= 0 || type
!= menu_next
)
637 static int compare(const void *p1
, const void *p2
)
639 struct tagentry
*e1
= (struct tagentry
*)p1
;
640 struct tagentry
*e2
= (struct tagentry
*)p2
;
643 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
645 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
648 static void tagtree_buffer_event(void *data
)
650 struct tagcache_search tcs
;
651 struct mp3entry
*id3
= (struct mp3entry
*)data
;
653 /* Do not gather data unless proper setting has been enabled. */
654 if (!global_settings
.runtimedb
&& !global_settings
.autoresume_enable
)
657 logf("be:%s", id3
->path
);
659 while (! tagcache_is_fully_initialized())
662 if (!tagcache_find_index(&tcs
, id3
->path
))
664 logf("tc stat: not found: %s", id3
->path
);
668 if (global_settings
.runtimedb
)
670 id3
->playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
672 id3
->rating
= tagcache_get_numeric(&tcs
, tag_rating
);
673 id3
->lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
674 id3
->score
= tagcache_get_numeric(&tcs
, tag_virt_autoscore
) / 10;
675 id3
->playtime
= tagcache_get_numeric(&tcs
, tag_playtime
);
677 logf("-> %ld/%ld", id3
->playcount
, id3
->playtime
);
680 #if CONFIG_CODEC == SWCODEC
681 if (global_settings
.autoresume_enable
)
683 /* Load current file resume offset if not already defined (by
684 another resume mechanism) */
685 if (id3
->offset
== 0)
687 id3
->offset
= tagcache_get_numeric(&tcs
, tag_lastoffset
);
689 logf("tagtree_buffer_event: Set offset for %s to %lX\n",
690 str_or_empty(id3
->title
), id3
->offset
);
695 /* Store our tagcache index pointer. */
696 id3
->tagcache_idx
= tcs
.idx_id
+1;
698 tagcache_search_finish(&tcs
);
701 static void tagtree_track_finish_event(void *data
)
705 struct mp3entry
*id3
= (struct mp3entry
*)data
;
707 /* Do not gather data unless proper setting has been enabled. */
708 if (!global_settings
.runtimedb
&& !global_settings
.autoresume_enable
)
710 logf("runtimedb gathering and autoresume not enabled");
714 tagcache_idx
=id3
->tagcache_idx
;
717 logf("No tagcache index pointer found");
722 /* Don't process unplayed tracks, or tracks interrupted within the
724 if (id3
->elapsed
== 0
725 #if CONFIG_CODEC == SWCODEC /* HWCODEC doesn't have automatic_skip */
726 || (id3
->elapsed
< 15 * 1000 && !audio_automatic_skip())
730 logf("not logging unplayed or skipped track");
734 lastplayed
= tagcache_increase_serial();
737 logf("incorrect tc serial:%ld", lastplayed
);
741 if (global_settings
.runtimedb
)
746 playcount
= id3
->playcount
+ 1;
748 /* Ignore the last 15s (crossfade etc.) */
749 playtime
= id3
->playtime
+ MIN(id3
->length
, id3
->elapsed
+ 15 * 1000);
751 logf("ube:%s", id3
->path
);
752 logf("-> %ld/%ld", playcount
, playtime
);
753 logf("-> %ld/%ld/%ld", id3
->elapsed
, id3
->length
,
754 MIN(id3
->length
, id3
->elapsed
+ 15 * 1000));
756 /* Queue the updates to the tagcache system. */
757 tagcache_update_numeric(tagcache_idx
, tag_playcount
, playcount
);
758 tagcache_update_numeric(tagcache_idx
, tag_playtime
, playtime
);
759 tagcache_update_numeric(tagcache_idx
, tag_lastplayed
, lastplayed
);
762 #if CONFIG_CODEC == SWCODEC
763 if (global_settings
.autoresume_enable
)
766 = audio_automatic_skip() ? 0 : id3
->offset
;
768 tagcache_update_numeric(tagcache_idx
, tag_lastoffset
, offset
);
770 logf("tagtree_track_finish_event: Save offset for %s: %lX",
771 str_or_empty(id3
->title
), offset
);
776 bool tagtree_export(void)
778 struct tagcache_search tcs
;
780 splash(0, str(LANG_CREATING
));
781 if (!tagcache_create_changelog(&tcs
))
783 splash(HZ
*2, ID2P(LANG_FAILED
));
789 bool tagtree_import(void)
791 splash(0, ID2P(LANG_WAIT
));
792 if (!tagcache_import_changelog())
794 splash(HZ
*2, ID2P(LANG_FAILED
));
800 static bool parse_menu(const char *filename
);
802 static int parse_line(int n
, const char *buf
, void *parameters
)
806 static bool read_menu
;
811 logf("parse:%d/%s", n
, buf
);
813 /* First line, do initialisation. */
816 if (strcasecmp(TAGNAVI_VERSION
, buf
))
818 logf("Version mismatch");
841 if (get_tag(&variable
) <= 0)
847 if (add_format(strp
) < 0)
849 logf("Format add fail: %s", data
);
854 if (get_token_str(data
, sizeof(data
)) < 0)
856 logf("%%include empty");
860 if (!parse_menu(data
))
862 logf("Load menu fail: %s", data
);
867 if (menu_count
>= TAGMENU_MAX_MENUS
)
869 logf("max menucount reached");
873 if (get_token_str(data
, sizeof data
) < 0)
875 logf("%%menu_start id empty");
880 for (i
= 0; i
< menu_count
; i
++)
882 if (!strcasecmp(menus
[i
]->id
, data
))
890 menus
[menu_count
] = buffer_alloc(sizeof(struct menu_root
));
891 menu
= menus
[menu_count
];
893 memset(menu
, 0, sizeof(struct menu_root
));
894 strlcpy(menu
->id
, data
, MAX_MENU_ID_SIZE
);
897 if (get_token_str(menu
->title
, sizeof(menu
->title
)) < 0)
899 logf("%%menu_start title empty");
902 logf("menu: %s", menu
->title
);
907 /* Only set root menu once. */
911 if (get_token_str(data
, sizeof(data
)) < 0)
913 logf("%%rootmenu empty");
917 for (i
= 0; i
< menu_count
; i
++)
919 if (!strcasecmp(menus
[i
]->id
, data
))
930 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
932 logf("max itemcount reached");
937 if (menu
->items
[menu
->itemcount
] == NULL
)
939 menu
->items
[menu
->itemcount
] = buffer_alloc(sizeof(struct menu_entry
));
940 memset(menu
->items
[menu
->itemcount
], 0, sizeof(struct menu_entry
));
941 menu
->items
[menu
->itemcount
]->si
= buffer_alloc(sizeof(struct search_instruction
));
944 memset(menu
->items
[menu
->itemcount
]->si
, 0, sizeof(struct search_instruction
));
945 if (!parse_search(menu
->items
[menu
->itemcount
], buf
))
953 static bool parse_menu(const char *filename
)
958 if (menu_count
>= TAGMENU_MAX_MENUS
)
960 logf("max menucount reached");
964 fd
= open(filename
, O_RDONLY
);
967 logf("Search instruction file not found.");
971 /* Now read file for real, parsing into si */
972 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
978 void tagtree_init(void)
984 parse_menu(FILE_SEARCH_INSTRUCTIONS
);
986 /* If no root menu is set, assume it's the first single menu
987 * we have. That shouldn't normally happen. */
991 uniqbuf
= buffer_alloc(UNIQBUF_SIZE
);
993 add_event(PLAYBACK_EVENT_TRACK_BUFFER
, false, tagtree_buffer_event
);
994 add_event(PLAYBACK_EVENT_TRACK_FINISH
, false, tagtree_track_finish_event
);
997 static bool show_search_progress(bool init
, int count
)
999 static int last_tick
= 0;
1001 /* Don't show splashes for 1/2 second after starting search */
1004 last_tick
= current_tick
+ HZ
/2;
1008 /* Update progress every 1/10 of a second */
1009 if (TIME_AFTER(current_tick
, last_tick
+ HZ
/10))
1011 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG
), count
, str(LANG_OFF_ABORT
));
1012 if (action_userabort(TIMEOUT_NOBLOCK
))
1014 last_tick
= current_tick
;
1021 static int format_str(struct tagcache_search
*tcs
, struct display_format
*fmt
,
1022 char *buf
, int buf_size
)
1025 bool read_format
= false;
1031 memset(buf
, 0, buf_size
);
1032 for (i
= 0; fmt
->formatstr
[i
] != '\0'; i
++)
1034 if (fmt
->formatstr
[i
] == '%')
1038 if (parpos
>= fmt
->tag_count
)
1040 logf("too many format tags");
1047 fmtbuf
[fmtbuf_pos
++] = fmt
->formatstr
[i
];
1048 if (fmtbuf_pos
>= buf_size
)
1050 logf("format parse error");
1054 if (fmt
->formatstr
[i
] == 's')
1056 fmtbuf
[fmtbuf_pos
] = '\0';
1057 read_format
= false;
1058 if (fmt
->tags
[parpos
] == tcs
->type
)
1060 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
, tcs
->result
);
1064 /* Need to fetch the tag data. */
1065 if (!tagcache_retrieve(tcs
, tcs
->idx_id
, fmt
->tags
[parpos
],
1066 &buf
[buf_pos
], buf_size
- buf_pos
))
1068 logf("retrieve failed");
1072 buf_pos
+= strlen(&buf
[buf_pos
]);
1075 else if (fmt
->formatstr
[i
] == 'd')
1077 fmtbuf
[fmtbuf_pos
] = '\0';
1078 read_format
= false;
1079 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
,
1080 tagcache_get_numeric(tcs
, fmt
->tags
[parpos
]));
1081 buf_pos
+= strlen(&buf
[buf_pos
]);
1087 buf
[buf_pos
++] = fmt
->formatstr
[i
];
1089 if (buf_pos
- 1 >= buf_size
)
1091 logf("buffer overflow");
1096 buf
[buf_pos
++] = '\0';
1101 static int retrieve_entries(struct tree_context
*c
, int offset
, bool init
)
1103 struct tagcache_search tcs
;
1104 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1105 struct display_format
*fmt
;
1107 int namebufused
= 0;
1108 int total_count
= 0;
1109 int special_entry_count
= 0;
1110 int level
= c
->currextra
;
1116 /* Show search progress straight away if the disk needs to spin up,
1117 otherwise show it after the normal 1/2 second delay */
1118 show_search_progress(
1119 #ifdef HAVE_DISK_STORAGE
1120 storage_disk_is_active()
1126 if (c
->currtable
== ALLSUBENTRIES
)
1132 tag
= csi
->tagorder
[level
];
1134 if (!tagcache_search(&tcs
, tag
))
1137 /* Prevent duplicate entries in the search list. */
1138 tagcache_search_set_uniqbuf(&tcs
, uniqbuf
, UNIQBUF_SIZE
);
1140 if (level
|| csi
->clause_count
[0] || TAGCACHE_IS_NUMERIC(tag
))
1143 for (i
= 0; i
< level
; i
++)
1145 if (TAGCACHE_IS_NUMERIC(csi
->tagorder
[i
]))
1147 static struct tagcache_search_clause cc
;
1149 memset(&cc
, 0, sizeof(struct tagcache_search_clause
));
1150 cc
.tag
= csi
->tagorder
[i
];
1151 cc
.type
= clause_is
;
1153 cc
.numeric_data
= csi
->result_seek
[i
];
1154 tagcache_search_add_clause(&tcs
, &cc
);
1158 tagcache_search_add_filter(&tcs
, csi
->tagorder
[i
],
1159 csi
->result_seek
[i
]);
1163 for (i
= 0; i
<= level
; i
++)
1167 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1168 tagcache_search_add_clause(&tcs
, csi
->clause
[i
][j
]);
1171 current_offset
= offset
;
1172 current_entry_count
= 0;
1176 for (i
= 0; i
< format_count
; i
++)
1178 if (formats
[i
]->group_id
== csi
->format_id
[level
])
1184 sort_inverse
= fmt
->sort_inverse
;
1185 sort_limit
= fmt
->limit
;
1191 sort_inverse
= false;
1196 if (tag
!= tag_title
&& tag
!= tag_filename
)
1200 dptr
->newtable
= ALLSUBENTRIES
;
1201 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1203 current_entry_count
++;
1207 dptr
->newtable
= NAVIBROWSE
;
1208 dptr
->name
= str(LANG_TAGNAVI_RANDOM
);
1209 dptr
->extraseek
= -1;
1211 current_entry_count
++;
1213 special_entry_count
+=2;
1216 total_count
+= special_entry_count
;
1218 while (tagcache_get_next(&tcs
))
1220 if (total_count
++ < offset
)
1223 if ( strcmp(tcs
.result
, UNTAGGED
) == 0)
1225 tcs
.result_len
= strlcpy(tcs
.result
,
1226 str(LANG_TAGNAVI_UNTAGGED
), TAG_MAXLEN
)+1;
1229 dptr
->newtable
= NAVIBROWSE
;
1230 if (tag
== tag_title
|| tag
== tag_filename
)
1232 dptr
->newtable
= PLAYTRACK
;
1233 dptr
->extraseek
= tcs
.idx_id
;
1236 dptr
->extraseek
= tcs
.result_seek
;
1239 /* Check the format */
1240 for (i
= 0; i
< format_count
; i
++)
1242 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1245 if (tagcache_check_clauses(&tcs
, formats
[i
]->clause
,
1246 formats
[i
]->clause_count
))
1253 if (!tcs
.ramresult
|| fmt
)
1259 if (format_str(&tcs
, fmt
, buf
, sizeof buf
) < 0)
1261 logf("format_str() failed");
1262 tagcache_search_finish(&tcs
);
1267 dptr
->name
= &c
->name_buffer
[namebufused
];
1269 namebufused
+= strlen(buf
)+1;
1271 namebufused
+= tcs
.result_len
;
1273 if (namebufused
>= c
->name_buffer_size
)
1275 logf("chunk mode #2: %d", current_entry_count
);
1281 strcpy(dptr
->name
, buf
);
1283 strcpy(dptr
->name
, tcs
.result
);
1286 dptr
->name
= tcs
.result
;
1289 current_entry_count
++;
1291 if (current_entry_count
>= global_settings
.max_files_in_dir
)
1293 logf("chunk mode #3: %d", current_entry_count
);
1299 if (init
&& !tcs
.ramsearch
)
1301 if (!show_search_progress(false, total_count
))
1302 { /* user aborted */
1303 tagcache_search_finish(&tcs
);
1304 return current_entry_count
;
1310 qsort(c
->dircache
+ special_entry_count
* c
->dentry_size
,
1311 current_entry_count
- special_entry_count
,
1312 c
->dentry_size
, compare
);
1316 tagcache_search_finish(&tcs
);
1317 return current_entry_count
;
1320 while (tagcache_get_next(&tcs
))
1324 if (!show_search_progress(false, total_count
))
1330 tagcache_search_finish(&tcs
);
1332 if (!sort
&& (sort_inverse
|| sort_limit
))
1334 splashf(HZ
*4, ID2P(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1335 logf("Too small dir buffer");
1340 total_count
= MIN(total_count
, sort_limit
);
1345 for (i
= 0; i
< total_count
; i
++, dptr
++)
1347 int len
= strlen(dptr
->name
);
1352 dptr
->name
= &dptr
->name
[strip
];
1360 static int load_root(struct tree_context
*c
)
1362 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1366 c
->currtable
= ROOT
;
1367 if (c
->dirlevel
== 0)
1368 c
->currextra
= rootmenu
;
1370 menu
= menus
[c
->currextra
];
1374 for (i
= 0; i
< menu
->itemcount
; i
++)
1376 dptr
->name
= menu
->items
[i
]->name
;
1377 switch (menu
->items
[i
]->type
)
1380 dptr
->newtable
= NAVIBROWSE
;
1381 dptr
->extraseek
= i
;
1385 dptr
->newtable
= ROOT
;
1386 dptr
->extraseek
= menu
->items
[i
]->link
;
1394 current_entry_count
= i
;
1399 int tagtree_load(struct tree_context
* c
)
1402 int table
= c
->currtable
;
1404 c
->dentry_size
= sizeof(struct tagentry
);
1411 c
->currtable
= table
;
1412 c
->currextra
= rootmenu
;
1418 count
= load_root(c
);
1423 logf("navibrowse...");
1425 count
= retrieve_entries(c
, 0, true);
1430 logf("Unsupported table %d\n", table
);
1437 count
= load_root(c
);
1438 splash(HZ
, str(LANG_TAGCACHE_BUSY
));
1441 /* The _total_ numer of entries available. */
1442 c
->dirlength
= c
->filesindir
= count
;
1447 int tagtree_enter(struct tree_context
* c
)
1450 struct tagentry
*dptr
;
1451 struct mp3entry
*id3
;
1456 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1459 seek
= dptr
->extraseek
;
1462 if(c
->filesindir
<=2)
1464 srand(current_tick
);
1465 dptr
= (tagtree_get_entry(c
, 2+(rand() % (c
->filesindir
-2))));
1466 seek
= dptr
->extraseek
;
1468 newextra
= dptr
->newtable
;
1470 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1473 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1474 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1475 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1476 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1479 switch (c
->currtable
) {
1481 c
->currextra
= newextra
;
1483 if (newextra
== ROOT
)
1486 c
->currextra
= seek
;
1489 else if (newextra
== NAVIBROWSE
)
1493 csi
= menu
->items
[seek
]->si
;
1496 strlcpy(current_title
[c
->currextra
], dptr
->name
,
1497 sizeof(current_title
[0]));
1499 /* Read input as necessary. */
1500 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1502 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1505 source
= csi
->clause
[i
][j
]->source
;
1507 if (source
== source_constant
)
1510 searchstring
=csi
->clause
[i
][j
]->str
;
1511 *searchstring
= '\0';
1513 id3
= audio_current_track();
1515 if (source
== source_current_path
&& id3
)
1518 strlcpy(searchstring
, id3
->path
, SEARCHSTR_SIZE
);
1519 e
= strrchr(searchstring
, '/');
1523 else if (source
> source_runtime
&& id3
)
1526 int k
= source
-source_runtime
;
1527 int offset
= id3_to_search_mapping
[k
].id3_offset
;
1528 char **src
= (char**)((char*)id3
+ offset
);
1531 strlcpy(searchstring
, *src
, SEARCHSTR_SIZE
);
1536 rc
= kbd_input(searchstring
, SEARCHSTR_SIZE
);
1537 if (rc
< 0 || !searchstring
[0])
1542 if (csi
->clause
[i
][j
]->numeric
)
1543 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1550 c
->currtable
= newextra
;
1556 if (newextra
== PLAYTRACK
)
1558 if (global_settings
.party_mode
&& audio_status()) {
1559 splash(HZ
, ID2P(LANG_PARTY_MODE
));
1563 /* about to create a new current playlist...
1564 allow user to cancel the operation */
1565 if (!warn_on_pl_erase())
1567 if (tagtree_play_folder(c
) >= 0)
1572 c
->currtable
= newextra
;
1573 csi
->result_seek
[c
->currextra
] = seek
;
1574 if (c
->currextra
< csi
->tagorder_count
-1)
1579 /* Update the statusbar title */
1580 strlcpy(current_title
[c
->currextra
], dptr
->name
,
1581 sizeof(current_title
[0]));
1590 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1595 void tagtree_exit(struct tree_context
* c
)
1598 if (c
->dirlevel
> 0)
1600 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1601 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1602 c
->currtable
= c
->table_history
[c
->dirlevel
];
1603 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1604 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1607 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1609 struct tagcache_search tcs
;
1610 struct tagentry
*entry
;
1612 entry
= tagtree_get_entry(c
, c
->selected_item
);
1614 if (!tagcache_search(&tcs
, tag_filename
))
1617 if (!tagcache_retrieve(&tcs
, entry
->extraseek
, tcs
.type
, buf
, buflen
))
1619 tagcache_search_finish(&tcs
);
1623 tagcache_search_finish(&tcs
);
1628 static bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1630 struct tagcache_search tcs
;
1633 int from
, to
, direction
;
1634 int files_left
= c
->filesindir
;
1637 if (!tagcache_search(&tcs
, tag_filename
))
1639 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
1644 if (position
== PLAYLIST_REPLACE
)
1646 if (playlist_remove_all_tracks(NULL
) == 0)
1647 position
= PLAYLIST_INSERT_LAST
;
1655 if (position
== PLAYLIST_INSERT_FIRST
)
1657 from
= c
->filesindir
- 1;
1668 for (i
= from
; i
!= to
; i
+= direction
)
1670 /* Count back to zero */
1671 if (!show_search_progress(false, files_left
--))
1674 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1675 tcs
.type
, buf
, sizeof buf
))
1680 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1682 logf("playlist_insert_track failed");
1687 playlist_sync(NULL
);
1688 tagcache_search_finish(&tcs
);
1694 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1696 struct tagentry
*dptr
;
1698 int dirlevel
= tc
->dirlevel
;
1700 show_search_progress(
1701 #ifdef HAVE_DISK_STORAGE
1702 storage_disk_is_active()
1709 /* We need to set the table to allsubentries. */
1710 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1712 /* Insert a single track? */
1713 if (dptr
->newtable
== PLAYTRACK
)
1715 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1717 logf("tagtree_get_filename failed");
1720 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1725 if (dptr
->newtable
== NAVIBROWSE
)
1729 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1731 else if (dptr
->newtable
!= ALLSUBENTRIES
)
1733 logf("unsupported table: %d", dptr
->newtable
);
1737 /* Now the current table should be allsubentries. */
1738 if (dptr
->newtable
!= PLAYTRACK
)
1742 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1744 /* And now the newtable should be playtrack. */
1745 if (dptr
->newtable
!= PLAYTRACK
)
1747 logf("newtable: %d !!", dptr
->newtable
);
1748 tc
->dirlevel
= dirlevel
;
1753 if (tc
->filesindir
<= 0)
1754 splash(HZ
, ID2P(LANG_END_PLAYLIST
));
1757 logf("insert_all_playlist");
1758 if (!insert_all_playlist(tc
, position
, queue
))
1759 splash(HZ
*2, ID2P(LANG_FAILED
));
1762 /* Finally return the dirlevel to its original value. */
1763 while (tc
->dirlevel
> dirlevel
)
1770 static int tagtree_play_folder(struct tree_context
* c
)
1772 if (playlist_create(NULL
, NULL
) < 0)
1774 logf("Failed creating playlist\n");
1778 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1781 if (global_settings
.playlist_shuffle
)
1782 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
1783 if (!global_settings
.play_selected
)
1784 c
->selected_item
= 0;
1785 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1787 playlist_start(c
->selected_item
,0);
1788 playlist_get_current()->num_inserted_tracks
= 0; /* make warn on playlist erase work */
1792 struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
1794 struct tagentry
*entry
= (struct tagentry
*)c
->dircache
;
1795 int realid
= id
- current_offset
;
1797 /* Load the next chunk if necessary. */
1798 if (realid
>= current_entry_count
|| realid
< 0)
1801 if (retrieve_entries(c
, MAX(0, id
- (current_entry_count
/ 2)),
1804 logf("retrieve failed");
1808 realid
= id
- current_offset
;
1812 return &entry
[realid
];
1815 char *tagtree_get_title(struct tree_context
* c
)
1817 switch (c
->currtable
)
1824 return current_title
[c
->currextra
];
1830 int tagtree_get_attr(struct tree_context
* c
)
1833 switch (c
->currtable
)
1836 if (csi
->tagorder
[c
->currextra
] == tag_title
)
1837 attr
= FILE_ATTR_AUDIO
;
1839 attr
= ATTR_DIRECTORY
;
1843 attr
= FILE_ATTR_AUDIO
;
1847 attr
= ATTR_DIRECTORY
;
1854 int tagtree_get_icon(struct tree_context
* c
)
1856 int icon
= Icon_Folder
;
1858 if (tagtree_get_attr(c
) == FILE_ATTR_AUDIO
)