1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
21 * Basic structure on this file was copied from dbtree.c and modified to
22 * support the tag cache interface.
47 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
49 static int tagtree_play_folder(struct tree_context
* c
);
51 static char searchstring
[128];
65 /* Capacity 10 000 entries (for example 10k different artists) */
66 #define UNIQBUF_SIZE (64*1024)
71 static struct tagcache_search tcs
, tcs2
;
72 static bool sort_inverse
;
75 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
78 * formatstr = "%-3d. %s"
79 * tags[0] = tag_autoscore
86 struct display_format
{
88 struct tagcache_search_clause
*clause
[TAGCACHE_MAX_CLAUSES
];
101 static struct display_format
*formats
[TAGMENU_MAX_FMTS
];
102 static int format_count
;
104 struct search_instruction
{
106 int tagorder
[MAX_TAGS
];
108 struct tagcache_search_clause
*clause
[MAX_TAGS
][TAGCACHE_MAX_CLAUSES
];
109 int format_id
[MAX_TAGS
];
110 int clause_count
[MAX_TAGS
];
111 int result_seek
[MAX_TAGS
];
117 struct search_instruction
*si
;
125 struct root_menu
*parent
;
126 struct menu_entry
*items
[TAGMENU_MAX_ITEMS
];
129 /* Statusbar text of the current view. */
130 static char current_title
[MAX_TAGS
][128];
132 static struct root_menu
*menus
[TAGMENU_MAX_MENUS
];
133 static struct root_menu
*menu
;
134 static struct search_instruction
*csi
;
135 static const char *strp
;
136 static int menu_count
;
137 static int root_menu
;
139 static int current_offset
;
140 static int current_entry_count
;
142 static int format_count
;
143 static struct tree_context
*tc
;
145 static int get_token_str(char *buf
, int size
)
147 /* Find the start. */
148 while (*strp
!= '"' && *strp
!= '\0')
151 if (*strp
== '\0' || *(++strp
) == '\0')
155 while (*strp
!= '"' && *strp
!= '\0' && --size
> 0)
156 *(buf
++) = *(strp
++);
167 #define MATCH(tag,str1,str2,settag) \
168 if (!strcasecmp(str1, str2)) { \
173 static int get_tag(int *tag
)
178 /* Find the start. */
179 while ((*strp
== ' ' || *strp
== '>') && *strp
!= '\0')
182 if (*strp
== '\0' || *strp
== '?')
185 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
187 if (*strp
== '\0' || *strp
== ' ')
194 MATCH(tag
, buf
, "album", tag_album
);
195 MATCH(tag
, buf
, "artist", tag_artist
);
196 MATCH(tag
, buf
, "bitrate", tag_bitrate
);
197 MATCH(tag
, buf
, "composer", tag_composer
);
198 MATCH(tag
, buf
, "comment", tag_comment
);
199 MATCH(tag
, buf
, "albumartist", tag_albumartist
);
200 MATCH(tag
, buf
, "ensemble", tag_albumartist
);
201 MATCH(tag
, buf
, "genre", tag_genre
);
202 MATCH(tag
, buf
, "length", tag_length
);
203 MATCH(tag
, buf
, "title", tag_title
);
204 MATCH(tag
, buf
, "filename", tag_filename
);
205 MATCH(tag
, buf
, "tracknum", tag_tracknumber
);
206 MATCH(tag
, buf
, "year", tag_year
);
207 MATCH(tag
, buf
, "playcount", tag_playcount
);
208 MATCH(tag
, buf
, "lastplayed", tag_lastplayed
);
209 MATCH(tag
, buf
, "commitid", tag_commitid
);
210 MATCH(tag
, buf
, "entryage", tag_virt_entryage
);
211 MATCH(tag
, buf
, "autoscore", tag_virt_autoscore
);
212 MATCH(tag
, buf
, "%sort", var_sorttype
);
213 MATCH(tag
, buf
, "%limit", var_limit
);
214 MATCH(tag
, buf
, "%strip", var_strip
);
215 MATCH(tag
, buf
, "%menu_start", var_menu_start
);
216 MATCH(tag
, buf
, "%include", var_include
);
217 MATCH(tag
, buf
, "%root_menu", var_rootmenu
);
218 MATCH(tag
, buf
, "%format", var_format
);
219 MATCH(tag
, buf
, "->", menu_next
);
220 MATCH(tag
, buf
, "==>", menu_load
);
222 logf("NO MATCH: %s\n", buf
);
229 static int get_clause(int *condition
)
234 /* Find the start. */
235 while (*strp
== ' ' && *strp
!= '\0')
241 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
243 if (*strp
== '\0' || *strp
== ' ')
250 MATCH(condition
, buf
, "=", clause_is
);
251 MATCH(condition
, buf
, "==", clause_is
);
252 MATCH(condition
, buf
, "!=", clause_is_not
);
253 MATCH(condition
, buf
, ">", clause_gt
);
254 MATCH(condition
, buf
, ">=", clause_gteq
);
255 MATCH(condition
, buf
, "<", clause_lt
);
256 MATCH(condition
, buf
, "<=", clause_lteq
);
257 MATCH(condition
, buf
, "~", clause_contains
);
258 MATCH(condition
, buf
, "!~", clause_not_contains
);
259 MATCH(condition
, buf
, "^", clause_begins_with
);
260 MATCH(condition
, buf
, "!^", clause_not_begins_with
);
261 MATCH(condition
, buf
, "$", clause_ends_with
);
262 MATCH(condition
, buf
, "!$", clause_not_ends_with
);
263 MATCH(condition
, buf
, "@", clause_oneof
);
268 static bool read_clause(struct tagcache_search_clause
*clause
)
272 if (get_tag(&clause
->tag
) <= 0)
275 if (get_clause(&clause
->type
) <= 0)
278 if (get_token_str(buf
, sizeof buf
) < 0)
281 clause
->str
= buffer_alloc(strlen(buf
)+1);
282 strcpy(clause
->str
, buf
);
284 logf("got clause: %d/%d [%s]", clause
->tag
, clause
->type
, clause
->str
);
286 if (*(clause
->str
) == '\0')
287 clause
->input
= true;
289 clause
->input
= false;
291 if (tagcache_is_numeric_tag(clause
->tag
))
293 clause
->numeric
= true;
294 clause
->numeric_data
= atoi(clause
->str
);
297 clause
->numeric
= false;
302 static bool read_variable(char *buf
, int size
)
306 if (!get_clause(&condition
))
309 if (condition
!= clause_is
)
312 if (get_token_str(buf
, size
) < 0)
318 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
319 static int get_format_str(struct display_format
*fmt
)
325 memset(fmt
, 0, sizeof(struct display_format
));
327 if (get_token_str(fmt
->name
, sizeof fmt
->name
) < 0)
330 /* Determine the group id */
332 for (i
= 0; i
< format_count
; i
++)
334 if (!strcasecmp(formats
[i
]->name
, fmt
->name
))
336 fmt
->group_id
= formats
[i
]->group_id
;
340 if (formats
[i
]->group_id
> fmt
->group_id
)
341 fmt
->group_id
= formats
[i
]->group_id
;
344 if (i
== format_count
)
347 logf("format: (%d) %s", fmt
->group_id
, fmt
->name
);
349 if (get_token_str(buf
, sizeof buf
) < 0)
352 fmt
->formatstr
= buffer_alloc(strlen(buf
) + 1);
353 strcpy(fmt
->formatstr
, buf
);
355 while (fmt
->tag_count
< MAX_TAGS
)
357 ret
= get_tag(&fmt
->tags
[fmt
->tag_count
]);
364 switch (fmt
->tags
[fmt
->tag_count
]) {
366 if (!read_variable(buf
, sizeof buf
))
368 if (!strcasecmp("inverse", buf
))
369 fmt
->sort_inverse
= true;
375 if (!read_variable(buf
, sizeof buf
))
377 fmt
->limit
= atoi(buf
);
381 if (!read_variable(buf
, sizeof buf
))
383 fmt
->strip
= atoi(buf
);
394 static int add_format(const char *buf
)
398 if (formats
[format_count
] == NULL
)
399 formats
[format_count
] = buffer_alloc(sizeof(struct display_format
));
401 memset(formats
[format_count
], 0, sizeof(struct display_format
));
402 if (get_format_str(formats
[format_count
]) < 0)
404 logf("get_format_str() parser failed!");
408 while (*strp
!= '\0' && *strp
!= '?')
413 int clause_count
= 0;
418 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
420 logf("too many clauses");
424 formats
[format_count
]->clause
[clause_count
] =
425 buffer_alloc(sizeof(struct tagcache_search_clause
));
427 if (!read_clause(formats
[format_count
]->clause
[clause_count
]))
433 formats
[format_count
]->clause_count
= clause_count
;
441 static int get_condition(struct search_instruction
*inst
)
452 if (get_token_str(buf
, sizeof buf
) < 0)
455 for (i
= 0; i
< format_count
; i
++)
457 if (!strcasecmp(formats
[i
]->name
, buf
))
461 if (i
== format_count
)
463 logf("format not found: %s", buf
);
467 inst
->format_id
[inst
->tagorder_count
] = formats
[i
]->group_id
;
480 clause_count
= inst
->clause_count
[inst
->tagorder_count
];
481 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
483 logf("Too many clauses");
487 inst
->clause
[inst
->tagorder_count
][clause_count
] =
488 buffer_alloc(sizeof(struct tagcache_search_clause
));
490 if (!read_clause(inst
->clause
[inst
->tagorder_count
][clause_count
]))
493 inst
->clause_count
[inst
->tagorder_count
]++;
499 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
500 * : album ? year >= "2000" : songs
506 static bool parse_search(struct menu_entry
*entry
, const char *str
)
510 struct search_instruction
*inst
= entry
->si
;
516 /* Parse entry name */
517 if (get_token_str(entry
->name
, sizeof entry
->name
) < 0)
519 logf("No name found.");
523 /* Parse entry type */
524 if (get_tag(&entry
->type
) <= 0)
527 if (entry
->type
== menu_load
)
529 if (get_token_str(buf
, sizeof buf
) < 0)
532 /* Find the matching root menu or "create" it */
533 for (i
= 0; i
< menu_count
; i
++)
535 if (!strcasecmp(menus
[i
]->id
, buf
))
538 menus
[i
]->parent
= menu
;
546 if (entry
->type
!= menu_next
)
549 while (inst
->tagorder_count
< MAX_TAGS
)
551 ret
= get_tag(&inst
->tagorder
[inst
->tagorder_count
]);
554 logf("Parse error #1");
562 logf("tag: %d", inst
->tagorder
[inst
->tagorder_count
]);
564 while ( (ret
= get_condition(inst
)) > 0 ) ;
568 inst
->tagorder_count
++;
570 if (get_tag(&type
) <= 0 || type
!= menu_next
)
577 static int compare(const void *p1
, const void *p2
)
579 struct tagentry
*e1
= (struct tagentry
*)p1
;
580 struct tagentry
*e2
= (struct tagentry
*)p2
;
583 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
585 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
588 static void tagtree_buffer_event(struct mp3entry
*id3
, bool last_track
)
593 /* Do not gather data unless proper setting has been enabled. */
594 if (!global_settings
.runtimedb
)
597 logf("be:%d%s", last_track
, id3
->path
);
599 if (!tagcache_find_index(&tcs
, id3
->path
))
601 logf("tc stat: not found: %s", id3
->path
);
605 id3
->playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
606 id3
->lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
607 id3
->rating
= tagcache_get_numeric(&tcs
, tag_virt_autoscore
) / 10;
609 tagcache_search_finish(&tcs
);
612 static void tagtree_unbuffer_event(struct mp3entry
*id3
, bool last_track
)
619 /* Do not gather data unless proper setting has been enabled. */
620 if (!global_settings
.runtimedb
)
622 logf("runtimedb gathering not enabled");
626 /* Don't process unplayed tracks. */
627 if (id3
->elapsed
== 0)
629 logf("not logging unplayed track");
633 if (!tagcache_find_index(&tcs
, id3
->path
))
635 logf("tc stat: not found: %s", id3
->path
);
639 playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
640 playtime
= tagcache_get_numeric(&tcs
, tag_playtime
);
641 lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
645 lastplayed
= tagcache_increase_serial();
648 logf("incorrect tc serial:%d", lastplayed
);
649 tagcache_search_finish(&tcs
);
653 /* Ignore the last 15s (crossfade etc.) */
654 playtime
+= MIN(id3
->length
, id3
->elapsed
+ 15 * 1000);
656 logf("ube:%s", id3
->path
);
657 logf("-> %d/%d/%d", last_track
, playcount
, playtime
);
658 logf("-> %d/%d/%d", id3
->elapsed
, id3
->length
, MIN(id3
->length
, id3
->elapsed
+ 15 * 1000));
660 /* lastplayed not yet supported. */
662 if (!tagcache_modify_numeric_entry(&tcs
, tag_playcount
, playcount
)
663 || !tagcache_modify_numeric_entry(&tcs
, tag_playtime
, playtime
)
664 || !tagcache_modify_numeric_entry(&tcs
, tag_lastplayed
, lastplayed
))
666 logf("tc stat: modify failed!");
667 tagcache_search_finish(&tcs
);
671 tagcache_search_finish(&tcs
);
674 bool tagtree_export(void)
676 gui_syncsplash(0, true, str(LANG_CREATING
));
677 if (!tagcache_create_changelog(&tcs
))
679 gui_syncsplash(HZ
*2, true, str(LANG_FAILED
));
685 bool tagtree_import(void)
687 gui_syncsplash(0, true, str(LANG_WAIT
));
688 if (!tagcache_import_changelog())
690 gui_syncsplash(HZ
*2, true, str(LANG_FAILED
));
696 static bool parse_menu(const char *filename
);
698 static int parse_line(int n
, const char *buf
, void *parameters
)
702 static bool read_menu
;
707 logf("parse:%d/%s", n
, buf
);
709 /* First line, do initialisation. */
712 if (strcasecmp(TAGNAVI_VERSION
, buf
))
714 logf("Version mismatch");
738 if (get_tag(&variable
) <= 0)
744 if (add_format(strp
) < 0)
746 logf("Format add fail: %s", data
);
751 if (get_token_str(data
, sizeof(data
)) < 0)
753 logf("%include empty");
757 if (!parse_menu(data
))
759 logf("Load menu fail: %s", data
);
764 if (menu_count
>= TAGMENU_MAX_MENUS
)
766 logf("max menucount reached");
770 menus
[menu_count
] = buffer_alloc(sizeof(struct root_menu
));
771 menu
= menus
[menu_count
];
772 memset(menu
, 0, sizeof(struct root_menu
));
773 if (get_token_str(menu
->id
, sizeof(menu
->id
)) < 0)
775 logf("%menu_start id empty");
778 if (get_token_str(menu
->title
, sizeof(menu
->title
)) < 0)
780 logf("%menu_start title empty");
783 logf("menu: %s", menu
->title
);
789 /* Only set root menu once. */
793 if (get_token_str(data
, sizeof(data
)) < 0)
795 logf("%root_menu empty");
799 for (i
= 0; i
< menu_count
; i
++)
801 if (!strcasecmp(menus
[i
]->id
, data
))
812 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
814 logf("max itemcount reached");
819 if (menu
->items
[menu
->itemcount
] == NULL
)
821 menu
->items
[menu
->itemcount
] = buffer_alloc(sizeof(struct menu_entry
));
822 memset(menu
->items
[menu
->itemcount
], 0, sizeof(struct menu_entry
));
823 menu
->items
[menu
->itemcount
]->si
= buffer_alloc(sizeof(struct search_instruction
));
826 memset(menu
->items
[menu
->itemcount
]->si
, 0, sizeof(struct search_instruction
));
827 if (!parse_search(menu
->items
[menu
->itemcount
], buf
))
835 static bool parse_menu(const char *filename
)
840 if (menu_count
>= TAGMENU_MAX_MENUS
)
842 logf("max menucount reached");
846 fd
= open(filename
, O_RDONLY
);
849 logf("Search instruction file not found.");
853 /* Now read file for real, parsing into si */
854 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
860 void tagtree_init(void)
866 parse_menu(FILE_SEARCH_INSTRUCTIONS
);
868 uniqbuf
= buffer_alloc(UNIQBUF_SIZE
);
869 audio_set_track_buffer_event(tagtree_buffer_event
);
870 audio_set_track_unbuffer_event(tagtree_unbuffer_event
);
873 static bool show_search_progress(bool init
, int count
)
875 static int last_tick
= 0;
879 last_tick
= current_tick
;
883 if (current_tick
- last_tick
> HZ
/4)
885 gui_syncsplash(0, true, str(LANG_PLAYLIST_SEARCH_MSG
), count
,
886 #if CONFIG_KEYPAD == PLAYER_PAD
892 if (action_userabort(TIMEOUT_NOBLOCK
))
894 last_tick
= current_tick
;
901 static int format_str(struct tagcache_search
*tcs
, struct display_format
*fmt
,
902 char *buf
, int buf_size
)
905 bool read_format
= false;
911 memset(buf
, 0, buf_size
);
912 for (i
= 0; fmt
->formatstr
[i
] != '\0'; i
++)
914 if (fmt
->formatstr
[i
] == '%')
918 if (parpos
>= fmt
->tag_count
)
920 logf("too many format tags");
927 fmtbuf
[fmtbuf_pos
++] = fmt
->formatstr
[i
];
928 if (fmtbuf_pos
>= buf_size
)
930 logf("format parse error");
934 if (fmt
->formatstr
[i
] == 's')
936 fmtbuf
[fmtbuf_pos
] = '\0';
938 if (fmt
->tags
[parpos
] == tcs
->type
)
940 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
, tcs
->result
);
944 /* Need to fetch the tag data. */
945 if (!tagcache_retrieve(tcs
, tcs
->idx_id
, fmt
->tags
[parpos
],
946 &buf
[buf_pos
], buf_size
- buf_pos
))
948 logf("retrieve failed");
952 buf_pos
+= strlen(&buf
[buf_pos
]);
955 else if (fmt
->formatstr
[i
] == 'd')
957 fmtbuf
[fmtbuf_pos
] = '\0';
959 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
,
960 tagcache_get_numeric(tcs
, fmt
->tags
[parpos
]));
961 buf_pos
+= strlen(&buf
[buf_pos
]);
967 buf
[buf_pos
++] = fmt
->formatstr
[i
];
969 if (buf_pos
- 1 >= buf_size
)
971 logf("buffer overflow");
976 buf
[buf_pos
++] = '\0';
981 static int retrieve_entries(struct tree_context
*c
, struct tagcache_search
*tcs
,
982 int offset
, bool init
)
984 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
985 struct display_format
*fmt
;
989 int special_entry_count
= 0;
990 int level
= c
->currextra
;
997 #ifdef HAVE_TC_RAMCACHE
998 && !tagcache_is_ramcache()
1002 show_search_progress(true, 0);
1003 gui_syncsplash(0, true, str(LANG_PLAYLIST_SEARCH_MSG
),
1007 if (c
->currtable
== allsubentries
)
1013 tag
= csi
->tagorder
[level
];
1015 if (!tagcache_search(tcs
, tag
))
1018 /* Prevent duplicate entries in the search list. */
1019 tagcache_search_set_uniqbuf(tcs
, uniqbuf
, UNIQBUF_SIZE
);
1021 if (level
|| csi
->clause_count
[0] || tagcache_is_numeric_tag(tag
))
1024 for (i
= 0; i
< level
; i
++)
1026 if (tagcache_is_numeric_tag(csi
->tagorder
[i
]))
1028 static struct tagcache_search_clause cc
;
1030 memset(&cc
, 0, sizeof(struct tagcache_search_clause
));
1031 cc
.tag
= csi
->tagorder
[i
];
1032 cc
.type
= clause_is
;
1034 cc
.numeric_data
= csi
->result_seek
[i
];
1035 tagcache_search_add_clause(tcs
, &cc
);
1039 tagcache_search_add_filter(tcs
, csi
->tagorder
[i
],
1040 csi
->result_seek
[i
]);
1044 for (i
= 0; i
<= level
; i
++)
1048 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1049 tagcache_search_add_clause(tcs
, csi
->clause
[i
][j
]);
1052 current_offset
= offset
;
1053 current_entry_count
= 0;
1057 for (i
= 0; i
< format_count
; i
++)
1059 if (formats
[i
]->group_id
== csi
->format_id
[level
])
1065 sort_inverse
= fmt
->sort_inverse
;
1066 sort_limit
= fmt
->limit
;
1069 /* Check if sorting is forced. */
1075 sort_inverse
= false;
1080 if (tag
!= tag_title
&& tag
!= tag_filename
)
1084 dptr
->newtable
= allsubentries
;
1085 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1087 current_entry_count
++;
1089 special_entry_count
++;
1092 total_count
+= special_entry_count
;
1094 while (tagcache_get_next(tcs
))
1096 if (total_count
++ < offset
)
1099 dptr
->newtable
= navibrowse
;
1100 if (tag
== tag_title
|| tag
== tag_filename
)
1102 dptr
->newtable
= playtrack
;
1103 dptr
->extraseek
= tcs
->idx_id
;
1106 dptr
->extraseek
= tcs
->result_seek
;
1109 /* Check the format */
1110 for (i
= 0; i
< format_count
; i
++)
1112 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1115 if (tagcache_check_clauses(tcs
, formats
[i
]->clause
,
1116 formats
[i
]->clause_count
))
1123 if (!tcs
->ramresult
|| fmt
)
1129 if (format_str(tcs
, fmt
, buf
, sizeof buf
) < 0)
1131 logf("format_str() failed");
1136 dptr
->name
= &c
->name_buffer
[namebufused
];
1138 namebufused
+= strlen(buf
)+1;
1140 namebufused
+= tcs
->result_len
;
1142 if (namebufused
>= c
->name_buffer_size
)
1144 logf("chunk mode #2: %d", current_entry_count
);
1150 strcpy(dptr
->name
, buf
);
1152 strcpy(dptr
->name
, tcs
->result
);
1155 dptr
->name
= tcs
->result
;
1158 current_entry_count
++;
1160 if (current_entry_count
>= global_settings
.max_files_in_dir
)
1162 logf("chunk mode #3: %d", current_entry_count
);
1168 if (init
&& !tcs
->ramsearch
)
1170 if (!show_search_progress(false, i
))
1172 tagcache_search_finish(tcs
);
1173 return current_entry_count
;
1179 qsort(c
->dircache
+ special_entry_count
* c
->dentry_size
,
1180 current_entry_count
- special_entry_count
,
1181 c
->dentry_size
, compare
);
1185 tagcache_search_finish(tcs
);
1186 return current_entry_count
;
1189 while (tagcache_get_next(tcs
))
1191 if (!tcs
->ramsearch
)
1193 if (!show_search_progress(false, total_count
))
1199 tagcache_search_finish(tcs
);
1201 if (!sort
&& (sort_inverse
|| sort_limit
))
1203 gui_syncsplash(HZ
*4, true, str(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1204 logf("Too small dir buffer");
1209 total_count
= MIN(total_count
, sort_limit
);
1214 for (i
= 0; i
< total_count
; i
++, dptr
++)
1216 int len
= strlen(dptr
->name
);
1221 dptr
->name
= &dptr
->name
[strip
];
1228 static int load_root(struct tree_context
*c
)
1230 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1234 c
->currtable
= root
;
1235 if (c
->dirlevel
== 0)
1236 c
->currextra
= root_menu
;
1238 menu
= menus
[c
->currextra
];
1242 for (i
= 0; i
< menu
->itemcount
; i
++)
1244 dptr
->name
= menu
->items
[i
]->name
;
1245 switch (menu
->items
[i
]->type
)
1248 dptr
->newtable
= navibrowse
;
1249 dptr
->extraseek
= i
;
1253 dptr
->newtable
= root
;
1254 dptr
->extraseek
= menu
->items
[i
]->link
;
1262 current_entry_count
= i
;
1267 int tagtree_load(struct tree_context
* c
)
1270 int table
= c
->currtable
;
1272 c
->dentry_size
= sizeof(struct tagentry
);
1279 c
->currtable
= table
;
1280 c
->currextra
= root_menu
;
1286 count
= load_root(c
);
1291 logf("navibrowse...");
1293 count
= retrieve_entries(c
, &tcs
, 0, true);
1298 logf("Unsupported table %d\n", table
);
1305 count
= load_root(c
);
1306 gui_syncsplash(HZ
, true, str(LANG_TAGCACHE_BUSY
));
1309 /* The _total_ numer of entries available. */
1310 c
->dirlength
= c
->filesindir
= count
;
1315 int tagtree_enter(struct tree_context
* c
)
1318 struct tagentry
*dptr
;
1322 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1325 newextra
= dptr
->newtable
;
1326 seek
= dptr
->extraseek
;
1328 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1331 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1332 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1333 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1334 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1337 switch (c
->currtable
) {
1339 c
->currextra
= newextra
;
1341 if (newextra
== root
)
1344 c
->currextra
= seek
;
1347 else if (newextra
== navibrowse
)
1351 csi
= menu
->items
[seek
]->si
;
1354 strncpy(current_title
[c
->currextra
], dptr
->name
,
1355 sizeof(current_title
[0]) - 1);
1357 /* Read input as necessary. */
1358 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1360 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1362 if (!csi
->clause
[i
][j
]->input
)
1365 rc
= kbd_input(searchstring
, sizeof(searchstring
));
1366 if (rc
== -1 || !searchstring
[0])
1372 if (csi
->clause
[i
][j
]->numeric
)
1373 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1375 csi
->clause
[i
][j
]->str
= searchstring
;
1379 c
->currtable
= newextra
;
1385 if (newextra
== playtrack
)
1388 /* about to create a new current playlist...
1389 allow user to cancel the operation */
1390 if (global_settings
.warnon_erase_dynplaylist
&&
1391 !global_settings
.party_mode
&&
1392 playlist_modified(NULL
))
1394 char *lines
[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
1395 struct text_message message
={lines
, 1};
1397 if (gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
1401 if (tagtree_play_folder(c
) >= 0)
1406 c
->currtable
= newextra
;
1407 csi
->result_seek
[c
->currextra
] = seek
;
1408 if (c
->currextra
< csi
->tagorder_count
-1)
1413 /* Update the statusbar title */
1414 strncpy(current_title
[c
->currextra
], dptr
->name
,
1415 sizeof(current_title
[0]) - 1);
1424 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1429 void tagtree_exit(struct tree_context
* c
)
1432 if (c
->dirlevel
> 0)
1434 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1435 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1436 c
->currtable
= c
->table_history
[c
->dirlevel
];
1437 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1438 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1441 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1443 struct tagentry
*entry
;
1445 entry
= tagtree_get_entry(c
, c
->selected_item
);
1447 if (!tagcache_search(&tcs
, tag_filename
))
1450 if (!tagcache_retrieve(&tcs
, entry
->extraseek
, tcs
.type
, buf
, buflen
))
1452 tagcache_search_finish(&tcs
);
1456 tagcache_search_finish(&tcs
);
1461 static bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1465 int from
, to
, direction
;
1466 int files_left
= c
->filesindir
;
1469 if (!tagcache_search(&tcs
, tag_filename
))
1471 gui_syncsplash(HZ
, true, str(LANG_TAGCACHE_BUSY
));
1476 if (position
== PLAYLIST_REPLACE
)
1478 if (remove_all_tracks(NULL
) == 0)
1479 position
= PLAYLIST_INSERT_LAST
;
1482 if (position
== PLAYLIST_INSERT_FIRST
)
1484 from
= c
->filesindir
- 1;
1495 for (i
= from
; i
!= to
; i
+= direction
)
1497 /* Count back to zero */
1498 if (!show_search_progress(false, files_left
--))
1501 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1502 tcs
.type
, buf
, sizeof buf
))
1507 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1509 logf("playlist_insert_track failed");
1514 playlist_sync(NULL
);
1515 tagcache_search_finish(&tcs
);
1521 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1523 struct tagentry
*dptr
;
1525 int dirlevel
= tc
->dirlevel
;
1527 /* We need to set the table to allsubentries. */
1528 show_search_progress(true, 0);
1530 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1532 /* Insert a single track? */
1533 if (dptr
->newtable
== playtrack
)
1535 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1537 logf("tagtree_get_filename failed");
1540 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1545 if (dptr
->newtable
== navibrowse
)
1549 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1551 else if (dptr
->newtable
!= allsubentries
)
1553 logf("unsupported table: %d", dptr
->newtable
);
1557 /* Now the current table should be allsubentries. */
1558 if (dptr
->newtable
!= playtrack
)
1562 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1564 /* And now the newtable should be playtrack. */
1565 if (dptr
->newtable
!= playtrack
)
1567 logf("newtable: %d !!", dptr
->newtable
);
1568 tc
->dirlevel
= dirlevel
;
1573 if (tc
->filesindir
<= 0)
1574 gui_syncsplash(HZ
, true, str(LANG_END_PLAYLIST_PLAYER
));
1577 logf("insert_all_playlist");
1578 if (!insert_all_playlist(tc
, position
, queue
))
1579 gui_syncsplash(HZ
*2, true, str(LANG_FAILED
));
1582 /* Finally return the dirlevel to its original value. */
1583 while (tc
->dirlevel
> dirlevel
)
1590 static int tagtree_play_folder(struct tree_context
* c
)
1592 if (playlist_create(NULL
, NULL
) < 0)
1594 logf("Failed creating playlist\n");
1598 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1601 if (global_settings
.playlist_shuffle
)
1602 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
1603 if (!global_settings
.play_selected
)
1604 c
->selected_item
= 0;
1605 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1607 playlist_start(c
->selected_item
,0);
1612 struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
1614 struct tagentry
*entry
= (struct tagentry
*)c
->dircache
;
1615 int realid
= id
- current_offset
;
1617 /* Load the next chunk if necessary. */
1618 if (realid
>= current_entry_count
|| realid
< 0)
1621 if (retrieve_entries(c
, &tcs2
, MAX(0, id
- (current_entry_count
/ 2)),
1624 logf("retrieve failed");
1628 realid
= id
- current_offset
;
1632 return &entry
[realid
];
1635 char *tagtree_get_title(struct tree_context
* c
)
1637 switch (c
->currtable
)
1644 return current_title
[c
->currextra
];
1650 int tagtree_get_attr(struct tree_context
* c
)
1653 switch (c
->currtable
)
1656 if (csi
->tagorder
[c
->currextra
] == tag_title
)
1657 attr
= TREE_ATTR_MPA
;
1659 attr
= ATTR_DIRECTORY
;
1663 attr
= TREE_ATTR_MPA
;
1667 attr
= ATTR_DIRECTORY
;
1674 #ifdef HAVE_LCD_BITMAP
1675 const unsigned char* tagtree_get_icon(struct tree_context
* c
)
1677 int tagtree_get_icon(struct tree_context
* c
)
1680 int icon
= Icon_Folder
;
1682 if (tagtree_get_attr(c
) == TREE_ATTR_MPA
)
1685 #ifdef HAVE_LCD_BITMAP
1686 return bitmap_icons_6x8
[icon
];