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"
56 #define str_or_empty(x) (x ? x : "(NULL)")
58 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
60 static int tagtree_play_folder(struct tree_context
* c
);
62 #define SEARCHSTR_SIZE 256
71 static const struct id3_to_search_mapping
{
74 } id3_to_search_mapping
[] = {
75 { "", 0 }, /* offset n/a */
76 { "#directory#", 0 }, /* offset n/a */
77 { "#title#", offsetof(struct mp3entry
, title
) },
78 { "#artist#", offsetof(struct mp3entry
, artist
) },
79 { "#album#", offsetof(struct mp3entry
, album
) },
80 { "#genre#", offsetof(struct mp3entry
, genre_string
) },
81 { "#composer#", offsetof(struct mp3entry
, composer
) },
82 { "#albumartist#", offsetof(struct mp3entry
, albumartist
) },
96 /* Capacity 10 000 entries (for example 10k different artists) */
97 #define UNIQBUF_SIZE (64*1024)
101 #define MAX_MENU_ID_SIZE 32
103 static bool sort_inverse
;
106 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
109 * formatstr = "%-3d. %s"
110 * tags[0] = tag_autoscore
111 * tags[1] = tag_title
115 * sort_inverse = true
117 struct display_format
{
119 struct tagcache_search_clause
*clause
[TAGCACHE_MAX_CLAUSES
];
131 static struct display_format
*formats
[TAGMENU_MAX_FMTS
];
132 static int format_count
;
134 struct search_instruction
{
136 int tagorder
[MAX_TAGS
];
138 struct tagcache_search_clause
*clause
[MAX_TAGS
][TAGCACHE_MAX_CLAUSES
];
139 int format_id
[MAX_TAGS
];
140 int clause_count
[MAX_TAGS
];
141 int result_seek
[MAX_TAGS
];
147 struct search_instruction
*si
;
153 char id
[MAX_MENU_ID_SIZE
];
155 struct menu_entry
*items
[TAGMENU_MAX_ITEMS
];
158 /* Statusbar text of the current view. */
159 static char current_title
[MAX_TAGS
][128];
161 static struct menu_root
*menus
[TAGMENU_MAX_MENUS
];
162 static struct menu_root
*menu
;
163 static struct search_instruction
*csi
;
164 static const char *strp
;
165 static int menu_count
;
168 static int current_offset
;
169 static int current_entry_count
;
171 static struct tree_context
*tc
;
173 #if CONFIG_CODEC == SWCODEC
174 extern bool automatic_skip
; /* Who initiated in-progress skip? (C/A-) */
177 static int get_token_str(char *buf
, int size
)
179 /* Find the start. */
180 while (*strp
!= '"' && *strp
!= '\0')
183 if (*strp
== '\0' || *(++strp
) == '\0')
187 while (*strp
!= '"' && *strp
!= '\0' && --size
> 0)
188 *(buf
++) = *(strp
++);
199 #define MATCH(tag,str1,str2,settag) \
200 if (!strcasecmp(str1, str2)) { \
205 static int get_tag(int *tag
)
210 /* Find the start. */
211 while ((*strp
== ' ' || *strp
== '>') && *strp
!= '\0')
214 if (*strp
== '\0' || *strp
== '?')
217 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
219 if (*strp
== '\0' || *strp
== ' ')
226 MATCH(tag
, buf
, "album", tag_album
);
227 MATCH(tag
, buf
, "artist", tag_artist
);
228 MATCH(tag
, buf
, "bitrate", tag_bitrate
);
229 MATCH(tag
, buf
, "composer", tag_composer
);
230 MATCH(tag
, buf
, "comment", tag_comment
);
231 MATCH(tag
, buf
, "albumartist", tag_albumartist
);
232 MATCH(tag
, buf
, "ensemble", tag_albumartist
);
233 MATCH(tag
, buf
, "grouping", tag_grouping
);
234 MATCH(tag
, buf
, "genre", tag_genre
);
235 MATCH(tag
, buf
, "length", tag_length
);
236 MATCH(tag
, buf
, "Lm", tag_virt_length_min
);
237 MATCH(tag
, buf
, "Ls", tag_virt_length_sec
);
238 MATCH(tag
, buf
, "Pm", tag_virt_playtime_min
);
239 MATCH(tag
, buf
, "Ps", tag_virt_playtime_sec
);
240 MATCH(tag
, buf
, "title", tag_title
);
241 MATCH(tag
, buf
, "filename", tag_filename
);
242 MATCH(tag
, buf
, "tracknum", tag_tracknumber
);
243 MATCH(tag
, buf
, "discnum", tag_discnumber
);
244 MATCH(tag
, buf
, "year", tag_year
);
245 MATCH(tag
, buf
, "playcount", tag_playcount
);
246 MATCH(tag
, buf
, "rating", tag_rating
);
247 MATCH(tag
, buf
, "lastplayed", tag_lastplayed
);
248 MATCH(tag
, buf
, "lastoffset", tag_lastoffset
);
249 MATCH(tag
, buf
, "commitid", tag_commitid
);
250 MATCH(tag
, buf
, "entryage", tag_virt_entryage
);
251 MATCH(tag
, buf
, "autoscore", tag_virt_autoscore
);
252 MATCH(tag
, buf
, "%sort", var_sorttype
);
253 MATCH(tag
, buf
, "%limit", var_limit
);
254 MATCH(tag
, buf
, "%strip", var_strip
);
255 MATCH(tag
, buf
, "%menu_start", var_menu_start
);
256 MATCH(tag
, buf
, "%include", var_include
);
257 MATCH(tag
, buf
, "%root_menu", var_rootmenu
);
258 MATCH(tag
, buf
, "%format", var_format
);
259 MATCH(tag
, buf
, "->", menu_next
);
260 MATCH(tag
, buf
, "==>", menu_load
);
262 logf("NO MATCH: %s\n", buf
);
269 static int get_clause(int *condition
)
274 /* Find the start. */
275 while (*strp
== ' ' && *strp
!= '\0')
281 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
283 if (*strp
== '\0' || *strp
== ' ')
290 MATCH(condition
, buf
, "=", clause_is
);
291 MATCH(condition
, buf
, "==", clause_is
);
292 MATCH(condition
, buf
, "!=", clause_is_not
);
293 MATCH(condition
, buf
, ">", clause_gt
);
294 MATCH(condition
, buf
, ">=", clause_gteq
);
295 MATCH(condition
, buf
, "<", clause_lt
);
296 MATCH(condition
, buf
, "<=", clause_lteq
);
297 MATCH(condition
, buf
, "~", clause_contains
);
298 MATCH(condition
, buf
, "!~", clause_not_contains
);
299 MATCH(condition
, buf
, "^", clause_begins_with
);
300 MATCH(condition
, buf
, "!^", clause_not_begins_with
);
301 MATCH(condition
, buf
, "$", clause_ends_with
);
302 MATCH(condition
, buf
, "!$", clause_not_ends_with
);
303 MATCH(condition
, buf
, "@", clause_oneof
);
308 static bool read_clause(struct tagcache_search_clause
*clause
)
310 char buf
[SEARCHSTR_SIZE
];
313 if (get_tag(&clause
->tag
) <= 0)
316 if (get_clause(&clause
->type
) <= 0)
319 if (get_token_str(buf
, sizeof buf
) < 0)
322 for (i
=0; i
<ARRAYLEN(id3_to_search_mapping
); i
++)
324 if (!strcasecmp(buf
, id3_to_search_mapping
[i
].string
))
328 if (i
<ARRAYLEN(id3_to_search_mapping
)) /* runtime search operand found */
330 clause
->source
= source_runtime
+i
;
331 clause
->str
= buffer_alloc(SEARCHSTR_SIZE
);
335 clause
->source
= source_constant
;
336 clause
->str
= buffer_alloc(strlen(buf
)+1);
337 strcpy(clause
->str
, buf
);
340 if (TAGCACHE_IS_NUMERIC(clause
->tag
))
342 clause
->numeric
= true;
343 clause
->numeric_data
= atoi(clause
->str
);
346 clause
->numeric
= false;
348 logf("got clause: %d/%d [%s]", clause
->tag
, clause
->type
, clause
->str
);
353 static bool read_variable(char *buf
, int size
)
357 if (!get_clause(&condition
))
360 if (condition
!= clause_is
)
363 if (get_token_str(buf
, size
) < 0)
369 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
370 static int get_format_str(struct display_format
*fmt
)
376 memset(fmt
, 0, sizeof(struct display_format
));
378 if (get_token_str(fmt
->name
, sizeof fmt
->name
) < 0)
381 /* Determine the group id */
383 for (i
= 0; i
< format_count
; i
++)
385 if (!strcasecmp(formats
[i
]->name
, fmt
->name
))
387 fmt
->group_id
= formats
[i
]->group_id
;
391 if (formats
[i
]->group_id
> fmt
->group_id
)
392 fmt
->group_id
= formats
[i
]->group_id
;
395 if (i
== format_count
)
398 logf("format: (%d) %s", fmt
->group_id
, fmt
->name
);
400 if (get_token_str(buf
, sizeof buf
) < 0)
403 fmt
->formatstr
= buffer_alloc(strlen(buf
) + 1);
404 strcpy(fmt
->formatstr
, buf
);
406 while (fmt
->tag_count
< MAX_TAGS
)
408 ret
= get_tag(&fmt
->tags
[fmt
->tag_count
]);
415 switch (fmt
->tags
[fmt
->tag_count
]) {
417 if (!read_variable(buf
, sizeof buf
))
419 if (!strcasecmp("inverse", buf
))
420 fmt
->sort_inverse
= true;
424 if (!read_variable(buf
, sizeof buf
))
426 fmt
->limit
= atoi(buf
);
430 if (!read_variable(buf
, sizeof buf
))
432 fmt
->strip
= atoi(buf
);
443 static int add_format(const char *buf
)
447 if (formats
[format_count
] == NULL
)
448 formats
[format_count
] = buffer_alloc(sizeof(struct display_format
));
450 memset(formats
[format_count
], 0, sizeof(struct display_format
));
451 if (get_format_str(formats
[format_count
]) < 0)
453 logf("get_format_str() parser failed!");
457 while (*strp
!= '\0' && *strp
!= '?')
462 int clause_count
= 0;
467 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
469 logf("too many clauses");
473 formats
[format_count
]->clause
[clause_count
] =
474 buffer_alloc(sizeof(struct tagcache_search_clause
));
476 if (!read_clause(formats
[format_count
]->clause
[clause_count
]))
482 formats
[format_count
]->clause_count
= clause_count
;
490 static int get_condition(struct search_instruction
*inst
)
501 if (get_token_str(buf
, sizeof buf
) < 0)
504 for (i
= 0; i
< format_count
; i
++)
506 if (!strcasecmp(formats
[i
]->name
, buf
))
510 if (i
== format_count
)
512 logf("format not found: %s", buf
);
516 inst
->format_id
[inst
->tagorder_count
] = formats
[i
]->group_id
;
529 clause_count
= inst
->clause_count
[inst
->tagorder_count
];
530 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
532 logf("Too many clauses");
536 inst
->clause
[inst
->tagorder_count
][clause_count
] =
537 buffer_alloc(sizeof(struct tagcache_search_clause
));
539 if (!read_clause(inst
->clause
[inst
->tagorder_count
][clause_count
]))
542 inst
->clause_count
[inst
->tagorder_count
]++;
548 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
549 * : album ? year >= "2000" : songs
555 static bool parse_search(struct menu_entry
*entry
, const char *str
)
559 struct search_instruction
*inst
= entry
->si
;
562 struct menu_root
*new_menu
;
566 /* Parse entry name */
567 if (get_token_str(entry
->name
, sizeof entry
->name
) < 0)
569 logf("No name found.");
573 /* Parse entry type */
574 if (get_tag(&entry
->type
) <= 0)
577 if (entry
->type
== menu_load
)
579 if (get_token_str(buf
, sizeof buf
) < 0)
582 /* Find the matching root menu or "create" it */
583 for (i
= 0; i
< menu_count
; i
++)
585 if (!strcasecmp(menus
[i
]->id
, buf
))
592 if (menu_count
>= TAGMENU_MAX_MENUS
)
594 logf("max menucount reached");
598 /* Allocate a new menu unless link is found. */
599 menus
[menu_count
] = buffer_alloc(sizeof(struct menu_root
));
600 new_menu
= menus
[menu_count
];
601 memset(new_menu
, 0, sizeof(struct menu_root
));
602 strlcpy(new_menu
->id
, buf
, MAX_MENU_ID_SIZE
);
603 entry
->link
= menu_count
;
609 if (entry
->type
!= menu_next
)
612 while (inst
->tagorder_count
< MAX_TAGS
)
614 ret
= get_tag(&inst
->tagorder
[inst
->tagorder_count
]);
617 logf("Parse error #1");
625 logf("tag: %d", inst
->tagorder
[inst
->tagorder_count
]);
627 while ( (ret
= get_condition(inst
)) > 0 ) ;
631 inst
->tagorder_count
++;
633 if (get_tag(&type
) <= 0 || type
!= menu_next
)
640 static int compare(const void *p1
, const void *p2
)
642 struct tagentry
*e1
= (struct tagentry
*)p1
;
643 struct tagentry
*e2
= (struct tagentry
*)p2
;
646 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
648 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
651 static void tagtree_buffer_event(void *data
)
653 struct tagcache_search tcs
;
654 struct mp3entry
*id3
= (struct mp3entry
*)data
;
656 /* Do not gather data unless proper setting has been enabled. */
657 if (!global_settings
.runtimedb
&& !global_settings
.autoresume_enable
)
660 logf("be:%s", id3
->path
);
662 while (! tagcache_is_fully_initialized())
665 if (!tagcache_find_index(&tcs
, id3
->path
))
667 logf("tc stat: not found: %s", id3
->path
);
671 if (global_settings
.runtimedb
)
673 id3
->playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
675 id3
->rating
= tagcache_get_numeric(&tcs
, tag_rating
);
676 id3
->lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
677 id3
->score
= tagcache_get_numeric(&tcs
, tag_virt_autoscore
) / 10;
678 id3
->playtime
= tagcache_get_numeric(&tcs
, tag_playtime
);
680 logf("-> %ld/%ld", id3
->playcount
, id3
->playtime
);
683 #if CONFIG_CODEC == SWCODEC
684 if (global_settings
.autoresume_enable
)
686 /* Load current file resume offset if not already defined (by
687 another resume mechanism) */
688 if (id3
->offset
== 0)
690 id3
->offset
= tagcache_get_numeric(&tcs
, tag_lastoffset
);
692 logf("tagtree_buffer_event: Set offset for %s to %lX\n",
693 str_or_empty(id3
->title
), id3
->offset
);
698 /* Store our tagcache index pointer. */
699 id3
->tagcache_idx
= tcs
.idx_id
+1;
701 tagcache_search_finish(&tcs
);
704 static void tagtree_track_finish_event(void *data
)
708 struct mp3entry
*id3
= (struct mp3entry
*)data
;
710 /* Do not gather data unless proper setting has been enabled. */
711 if (!global_settings
.runtimedb
&& !global_settings
.autoresume_enable
)
713 logf("runtimedb gathering and autoresume not enabled");
717 tagcache_idx
=id3
->tagcache_idx
;
720 logf("No tagcache index pointer found");
725 /* Don't process unplayed tracks, or tracks interrupted within the
727 if (id3
->elapsed
== 0
728 #if CONFIG_CODEC == SWCODEC /* HWCODEC doesn't have automatic_skip */
729 || (id3
->elapsed
< 15 * 1000 && !automatic_skip
)
733 logf("not logging unplayed or skipped track");
737 lastplayed
= tagcache_increase_serial();
740 logf("incorrect tc serial:%ld", lastplayed
);
744 if (global_settings
.runtimedb
)
749 playcount
= id3
->playcount
+ 1;
751 /* Ignore the last 15s (crossfade etc.) */
752 playtime
= id3
->playtime
+ MIN(id3
->length
, id3
->elapsed
+ 15 * 1000);
754 logf("ube:%s", id3
->path
);
755 logf("-> %ld/%ld", playcount
, playtime
);
756 logf("-> %ld/%ld/%ld", id3
->elapsed
, id3
->length
,
757 MIN(id3
->length
, id3
->elapsed
+ 15 * 1000));
759 /* Queue the updates to the tagcache system. */
760 tagcache_update_numeric(tagcache_idx
, tag_playcount
, playcount
);
761 tagcache_update_numeric(tagcache_idx
, tag_playtime
, playtime
);
762 tagcache_update_numeric(tagcache_idx
, tag_lastplayed
, lastplayed
);
765 #if CONFIG_CODEC == SWCODEC
766 if (global_settings
.autoresume_enable
)
769 = automatic_skip
? 0 : id3
->offset
;
771 tagcache_update_numeric(tagcache_idx
, tag_lastoffset
, offset
);
773 logf("tagtree_track_finish_event: Save offset for %s: %lX",
774 str_or_empty(id3
->title
), offset
);
779 bool tagtree_export(void)
781 struct tagcache_search tcs
;
783 splash(0, str(LANG_CREATING
));
784 if (!tagcache_create_changelog(&tcs
))
786 splash(HZ
*2, ID2P(LANG_FAILED
));
792 bool tagtree_import(void)
794 splash(0, ID2P(LANG_WAIT
));
795 if (!tagcache_import_changelog())
797 splash(HZ
*2, ID2P(LANG_FAILED
));
803 static bool parse_menu(const char *filename
);
805 static int parse_line(int n
, const char *buf
, void *parameters
)
809 static bool read_menu
;
814 logf("parse:%d/%s", n
, buf
);
816 /* First line, do initialisation. */
819 if (strcasecmp(TAGNAVI_VERSION
, buf
))
821 logf("Version mismatch");
844 if (get_tag(&variable
) <= 0)
850 if (add_format(strp
) < 0)
852 logf("Format add fail: %s", data
);
857 if (get_token_str(data
, sizeof(data
)) < 0)
859 logf("%%include empty");
863 if (!parse_menu(data
))
865 logf("Load menu fail: %s", data
);
870 if (menu_count
>= TAGMENU_MAX_MENUS
)
872 logf("max menucount reached");
876 if (get_token_str(data
, sizeof data
) < 0)
878 logf("%%menu_start id empty");
883 for (i
= 0; i
< menu_count
; i
++)
885 if (!strcasecmp(menus
[i
]->id
, data
))
893 menus
[menu_count
] = buffer_alloc(sizeof(struct menu_root
));
894 menu
= menus
[menu_count
];
896 memset(menu
, 0, sizeof(struct menu_root
));
897 strlcpy(menu
->id
, data
, MAX_MENU_ID_SIZE
);
900 if (get_token_str(menu
->title
, sizeof(menu
->title
)) < 0)
902 logf("%%menu_start title empty");
905 logf("menu: %s", menu
->title
);
910 /* Only set root menu once. */
914 if (get_token_str(data
, sizeof(data
)) < 0)
916 logf("%%rootmenu empty");
920 for (i
= 0; i
< menu_count
; i
++)
922 if (!strcasecmp(menus
[i
]->id
, data
))
933 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
935 logf("max itemcount reached");
940 if (menu
->items
[menu
->itemcount
] == NULL
)
942 menu
->items
[menu
->itemcount
] = buffer_alloc(sizeof(struct menu_entry
));
943 memset(menu
->items
[menu
->itemcount
], 0, sizeof(struct menu_entry
));
944 menu
->items
[menu
->itemcount
]->si
= buffer_alloc(sizeof(struct search_instruction
));
947 memset(menu
->items
[menu
->itemcount
]->si
, 0, sizeof(struct search_instruction
));
948 if (!parse_search(menu
->items
[menu
->itemcount
], buf
))
956 static bool parse_menu(const char *filename
)
961 if (menu_count
>= TAGMENU_MAX_MENUS
)
963 logf("max menucount reached");
967 fd
= open(filename
, O_RDONLY
);
970 logf("Search instruction file not found.");
974 /* Now read file for real, parsing into si */
975 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
981 void tagtree_init(void)
987 parse_menu(FILE_SEARCH_INSTRUCTIONS
);
989 /* If no root menu is set, assume it's the first single menu
990 * we have. That shouldn't normally happen. */
994 uniqbuf
= buffer_alloc(UNIQBUF_SIZE
);
996 add_event(PLAYBACK_EVENT_TRACK_BUFFER
, false, tagtree_buffer_event
);
997 add_event(PLAYBACK_EVENT_TRACK_FINISH
, false, tagtree_track_finish_event
);
1000 static bool show_search_progress(bool init
, int count
)
1002 static int last_tick
= 0;
1004 /* Don't show splashes for 1/2 second after starting search */
1007 last_tick
= current_tick
+ HZ
/2;
1011 /* Update progress every 1/10 of a second */
1012 if (TIME_AFTER(current_tick
, last_tick
+ HZ
/10))
1014 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG
), count
, str(LANG_OFF_ABORT
));
1015 if (action_userabort(TIMEOUT_NOBLOCK
))
1017 last_tick
= current_tick
;
1024 static int format_str(struct tagcache_search
*tcs
, struct display_format
*fmt
,
1025 char *buf
, int buf_size
)
1028 bool read_format
= false;
1034 memset(buf
, 0, buf_size
);
1035 for (i
= 0; fmt
->formatstr
[i
] != '\0'; i
++)
1037 if (fmt
->formatstr
[i
] == '%')
1041 if (parpos
>= fmt
->tag_count
)
1043 logf("too many format tags");
1050 fmtbuf
[fmtbuf_pos
++] = fmt
->formatstr
[i
];
1051 if (fmtbuf_pos
>= buf_size
)
1053 logf("format parse error");
1057 if (fmt
->formatstr
[i
] == 's')
1059 fmtbuf
[fmtbuf_pos
] = '\0';
1060 read_format
= false;
1061 if (fmt
->tags
[parpos
] == tcs
->type
)
1063 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
, tcs
->result
);
1067 /* Need to fetch the tag data. */
1068 if (!tagcache_retrieve(tcs
, tcs
->idx_id
, fmt
->tags
[parpos
],
1069 &buf
[buf_pos
], buf_size
- buf_pos
))
1071 logf("retrieve failed");
1075 buf_pos
+= strlen(&buf
[buf_pos
]);
1078 else if (fmt
->formatstr
[i
] == 'd')
1080 fmtbuf
[fmtbuf_pos
] = '\0';
1081 read_format
= false;
1082 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
,
1083 tagcache_get_numeric(tcs
, fmt
->tags
[parpos
]));
1084 buf_pos
+= strlen(&buf
[buf_pos
]);
1090 buf
[buf_pos
++] = fmt
->formatstr
[i
];
1092 if (buf_pos
- 1 >= buf_size
)
1094 logf("buffer overflow");
1099 buf
[buf_pos
++] = '\0';
1104 static int retrieve_entries(struct tree_context
*c
, int offset
, bool init
)
1106 struct tagcache_search tcs
;
1107 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1108 struct display_format
*fmt
;
1110 int namebufused
= 0;
1111 int total_count
= 0;
1112 int special_entry_count
= 0;
1113 int level
= c
->currextra
;
1119 /* Show search progress straight away if the disk needs to spin up,
1120 otherwise show it after the normal 1/2 second delay */
1121 show_search_progress(
1122 #ifdef HAVE_DISK_STORAGE
1123 storage_disk_is_active()
1129 if (c
->currtable
== ALLSUBENTRIES
)
1135 tag
= csi
->tagorder
[level
];
1137 if (!tagcache_search(&tcs
, tag
))
1140 /* Prevent duplicate entries in the search list. */
1141 tagcache_search_set_uniqbuf(&tcs
, uniqbuf
, UNIQBUF_SIZE
);
1143 if (level
|| csi
->clause_count
[0] || TAGCACHE_IS_NUMERIC(tag
))
1146 for (i
= 0; i
< level
; i
++)
1148 if (TAGCACHE_IS_NUMERIC(csi
->tagorder
[i
]))
1150 static struct tagcache_search_clause cc
;
1152 memset(&cc
, 0, sizeof(struct tagcache_search_clause
));
1153 cc
.tag
= csi
->tagorder
[i
];
1154 cc
.type
= clause_is
;
1156 cc
.numeric_data
= csi
->result_seek
[i
];
1157 tagcache_search_add_clause(&tcs
, &cc
);
1161 tagcache_search_add_filter(&tcs
, csi
->tagorder
[i
],
1162 csi
->result_seek
[i
]);
1166 for (i
= 0; i
<= level
; i
++)
1170 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1171 tagcache_search_add_clause(&tcs
, csi
->clause
[i
][j
]);
1174 current_offset
= offset
;
1175 current_entry_count
= 0;
1179 for (i
= 0; i
< format_count
; i
++)
1181 if (formats
[i
]->group_id
== csi
->format_id
[level
])
1187 sort_inverse
= fmt
->sort_inverse
;
1188 sort_limit
= fmt
->limit
;
1194 sort_inverse
= false;
1199 if (tag
!= tag_title
&& tag
!= tag_filename
)
1203 dptr
->newtable
= ALLSUBENTRIES
;
1204 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1206 current_entry_count
++;
1210 dptr
->newtable
= NAVIBROWSE
;
1211 dptr
->name
= str(LANG_TAGNAVI_RANDOM
);
1212 dptr
->extraseek
= -1;
1214 current_entry_count
++;
1216 special_entry_count
+=2;
1219 total_count
+= special_entry_count
;
1221 while (tagcache_get_next(&tcs
))
1223 if (total_count
++ < offset
)
1226 if ( strcmp(tcs
.result
, UNTAGGED
) == 0)
1228 tcs
.result_len
= strlcpy(tcs
.result
,
1229 str(LANG_TAGNAVI_UNTAGGED
), TAG_MAXLEN
)+1;
1232 dptr
->newtable
= NAVIBROWSE
;
1233 if (tag
== tag_title
|| tag
== tag_filename
)
1235 dptr
->newtable
= PLAYTRACK
;
1236 dptr
->extraseek
= tcs
.idx_id
;
1239 dptr
->extraseek
= tcs
.result_seek
;
1242 /* Check the format */
1243 for (i
= 0; i
< format_count
; i
++)
1245 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1248 if (tagcache_check_clauses(&tcs
, formats
[i
]->clause
,
1249 formats
[i
]->clause_count
))
1256 if (!tcs
.ramresult
|| fmt
)
1262 if (format_str(&tcs
, fmt
, buf
, sizeof buf
) < 0)
1264 logf("format_str() failed");
1265 tagcache_search_finish(&tcs
);
1270 dptr
->name
= &c
->name_buffer
[namebufused
];
1272 namebufused
+= strlen(buf
)+1;
1274 namebufused
+= tcs
.result_len
;
1276 if (namebufused
>= c
->name_buffer_size
)
1278 logf("chunk mode #2: %d", current_entry_count
);
1284 strcpy(dptr
->name
, buf
);
1286 strcpy(dptr
->name
, tcs
.result
);
1289 dptr
->name
= tcs
.result
;
1292 current_entry_count
++;
1294 if (current_entry_count
>= global_settings
.max_files_in_dir
)
1296 logf("chunk mode #3: %d", current_entry_count
);
1302 if (init
&& !tcs
.ramsearch
)
1304 if (!show_search_progress(false, total_count
))
1305 { /* user aborted */
1306 tagcache_search_finish(&tcs
);
1307 return current_entry_count
;
1313 qsort(c
->dircache
+ special_entry_count
* c
->dentry_size
,
1314 current_entry_count
- special_entry_count
,
1315 c
->dentry_size
, compare
);
1319 tagcache_search_finish(&tcs
);
1320 return current_entry_count
;
1323 while (tagcache_get_next(&tcs
))
1327 if (!show_search_progress(false, total_count
))
1333 tagcache_search_finish(&tcs
);
1335 if (!sort
&& (sort_inverse
|| sort_limit
))
1337 splashf(HZ
*4, ID2P(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1338 logf("Too small dir buffer");
1343 total_count
= MIN(total_count
, sort_limit
);
1348 for (i
= 0; i
< total_count
; i
++, dptr
++)
1350 int len
= strlen(dptr
->name
);
1355 dptr
->name
= &dptr
->name
[strip
];
1363 static int load_root(struct tree_context
*c
)
1365 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1369 c
->currtable
= ROOT
;
1370 if (c
->dirlevel
== 0)
1371 c
->currextra
= rootmenu
;
1373 menu
= menus
[c
->currextra
];
1377 for (i
= 0; i
< menu
->itemcount
; i
++)
1379 dptr
->name
= menu
->items
[i
]->name
;
1380 switch (menu
->items
[i
]->type
)
1383 dptr
->newtable
= NAVIBROWSE
;
1384 dptr
->extraseek
= i
;
1388 dptr
->newtable
= ROOT
;
1389 dptr
->extraseek
= menu
->items
[i
]->link
;
1397 current_entry_count
= i
;
1402 int tagtree_load(struct tree_context
* c
)
1405 int table
= c
->currtable
;
1407 c
->dentry_size
= sizeof(struct tagentry
);
1414 c
->currtable
= table
;
1415 c
->currextra
= rootmenu
;
1421 count
= load_root(c
);
1426 logf("navibrowse...");
1428 count
= retrieve_entries(c
, 0, true);
1433 logf("Unsupported table %d\n", table
);
1440 count
= load_root(c
);
1441 splash(HZ
, str(LANG_TAGCACHE_BUSY
));
1444 /* The _total_ numer of entries available. */
1445 c
->dirlength
= c
->filesindir
= count
;
1450 int tagtree_enter(struct tree_context
* c
)
1453 struct tagentry
*dptr
;
1454 struct mp3entry
*id3
;
1459 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1462 seek
= dptr
->extraseek
;
1465 if(c
->filesindir
<=2)
1467 srand(current_tick
);
1468 dptr
= (tagtree_get_entry(c
, 2+(rand() % (c
->filesindir
-2))));
1469 seek
= dptr
->extraseek
;
1471 newextra
= dptr
->newtable
;
1473 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1476 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1477 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1478 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1479 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1482 switch (c
->currtable
) {
1484 c
->currextra
= newextra
;
1486 if (newextra
== ROOT
)
1489 c
->currextra
= seek
;
1492 else if (newextra
== NAVIBROWSE
)
1496 csi
= menu
->items
[seek
]->si
;
1499 strlcpy(current_title
[c
->currextra
], dptr
->name
,
1500 sizeof(current_title
[0]));
1502 /* Read input as necessary. */
1503 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1505 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1508 source
= csi
->clause
[i
][j
]->source
;
1510 if (source
== source_constant
)
1513 searchstring
=csi
->clause
[i
][j
]->str
;
1514 *searchstring
= '\0';
1516 id3
= audio_current_track();
1518 if (source
== source_current_path
&& id3
)
1521 strlcpy(searchstring
, id3
->path
, SEARCHSTR_SIZE
);
1522 e
= strrchr(searchstring
, '/');
1526 else if (source
> source_runtime
&& id3
)
1529 int k
= source
-source_runtime
;
1530 int offset
= id3_to_search_mapping
[k
].id3_offset
;
1531 char **src
= (char**)((char*)id3
+ offset
);
1534 strlcpy(searchstring
, *src
, SEARCHSTR_SIZE
);
1539 rc
= kbd_input(searchstring
, SEARCHSTR_SIZE
);
1540 if (rc
< 0 || !searchstring
[0])
1545 if (csi
->clause
[i
][j
]->numeric
)
1546 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1553 c
->currtable
= newextra
;
1559 if (newextra
== PLAYTRACK
)
1561 if (global_settings
.party_mode
&& audio_status()) {
1562 splash(HZ
, ID2P(LANG_PARTY_MODE
));
1566 /* about to create a new current playlist...
1567 allow user to cancel the operation */
1568 if (!warn_on_pl_erase())
1570 if (tagtree_play_folder(c
) >= 0)
1575 c
->currtable
= newextra
;
1576 csi
->result_seek
[c
->currextra
] = seek
;
1577 if (c
->currextra
< csi
->tagorder_count
-1)
1582 /* Update the statusbar title */
1583 strlcpy(current_title
[c
->currextra
], dptr
->name
,
1584 sizeof(current_title
[0]));
1593 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1598 void tagtree_exit(struct tree_context
* c
)
1601 if (c
->dirlevel
> 0)
1603 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1604 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1605 c
->currtable
= c
->table_history
[c
->dirlevel
];
1606 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1607 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1610 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1612 struct tagcache_search tcs
;
1613 struct tagentry
*entry
;
1615 entry
= tagtree_get_entry(c
, c
->selected_item
);
1617 if (!tagcache_search(&tcs
, tag_filename
))
1620 if (!tagcache_retrieve(&tcs
, entry
->extraseek
, tcs
.type
, buf
, buflen
))
1622 tagcache_search_finish(&tcs
);
1626 tagcache_search_finish(&tcs
);
1631 static bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1633 struct tagcache_search tcs
;
1636 int from
, to
, direction
;
1637 int files_left
= c
->filesindir
;
1640 if (!tagcache_search(&tcs
, tag_filename
))
1642 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
1647 if (position
== PLAYLIST_REPLACE
)
1649 if (playlist_remove_all_tracks(NULL
) == 0)
1650 position
= PLAYLIST_INSERT_LAST
;
1658 if (position
== PLAYLIST_INSERT_FIRST
)
1660 from
= c
->filesindir
- 1;
1671 for (i
= from
; i
!= to
; i
+= direction
)
1673 /* Count back to zero */
1674 if (!show_search_progress(false, files_left
--))
1677 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1678 tcs
.type
, buf
, sizeof buf
))
1683 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1685 logf("playlist_insert_track failed");
1690 playlist_sync(NULL
);
1691 tagcache_search_finish(&tcs
);
1697 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1699 struct tagentry
*dptr
;
1701 int dirlevel
= tc
->dirlevel
;
1703 show_search_progress(
1704 #ifdef HAVE_DISK_STORAGE
1705 storage_disk_is_active()
1712 /* We need to set the table to allsubentries. */
1713 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1715 /* Insert a single track? */
1716 if (dptr
->newtable
== PLAYTRACK
)
1718 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1720 logf("tagtree_get_filename failed");
1723 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1728 if (dptr
->newtable
== NAVIBROWSE
)
1732 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1734 else if (dptr
->newtable
!= ALLSUBENTRIES
)
1736 logf("unsupported table: %d", dptr
->newtable
);
1740 /* Now the current table should be allsubentries. */
1741 if (dptr
->newtable
!= PLAYTRACK
)
1745 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1747 /* And now the newtable should be playtrack. */
1748 if (dptr
->newtable
!= PLAYTRACK
)
1750 logf("newtable: %d !!", dptr
->newtable
);
1751 tc
->dirlevel
= dirlevel
;
1756 if (tc
->filesindir
<= 0)
1757 splash(HZ
, ID2P(LANG_END_PLAYLIST
));
1760 logf("insert_all_playlist");
1761 if (!insert_all_playlist(tc
, position
, queue
))
1762 splash(HZ
*2, ID2P(LANG_FAILED
));
1765 /* Finally return the dirlevel to its original value. */
1766 while (tc
->dirlevel
> dirlevel
)
1773 static int tagtree_play_folder(struct tree_context
* c
)
1775 if (playlist_create(NULL
, NULL
) < 0)
1777 logf("Failed creating playlist\n");
1781 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1784 if (global_settings
.playlist_shuffle
)
1785 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
1786 if (!global_settings
.play_selected
)
1787 c
->selected_item
= 0;
1788 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1790 playlist_start(c
->selected_item
,0);
1791 playlist_get_current()->num_inserted_tracks
= 0; /* make warn on playlist erase work */
1795 struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
1797 struct tagentry
*entry
= (struct tagentry
*)c
->dircache
;
1798 int realid
= id
- current_offset
;
1800 /* Load the next chunk if necessary. */
1801 if (realid
>= current_entry_count
|| realid
< 0)
1804 if (retrieve_entries(c
, MAX(0, id
- (current_entry_count
/ 2)),
1807 logf("retrieve failed");
1811 realid
= id
- current_offset
;
1815 return &entry
[realid
];
1818 char *tagtree_get_title(struct tree_context
* c
)
1820 switch (c
->currtable
)
1827 return current_title
[c
->currextra
];
1833 int tagtree_get_attr(struct tree_context
* c
)
1836 switch (c
->currtable
)
1839 if (csi
->tagorder
[c
->currextra
] == tag_title
)
1840 attr
= FILE_ATTR_AUDIO
;
1842 attr
= ATTR_DIRECTORY
;
1846 attr
= FILE_ATTR_AUDIO
;
1850 attr
= ATTR_DIRECTORY
;
1857 int tagtree_get_icon(struct tree_context
* c
)
1859 int icon
= Icon_Folder
;
1861 if (tagtree_get_attr(c
) == FILE_ATTR_AUDIO
)