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 #include "filetypes.h"
49 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
51 static int tagtree_play_folder(struct tree_context
* c
);
53 static char searchstring
[128];
67 /* Capacity 10 000 entries (for example 10k different artists) */
68 #define UNIQBUF_SIZE (64*1024)
72 #define MAX_MENU_ID_SIZE 32
74 static struct tagcache_search tcs
, tcs2
;
75 static bool sort_inverse
;
78 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
81 * formatstr = "%-3d. %s"
82 * tags[0] = tag_autoscore
89 struct display_format
{
91 struct tagcache_search_clause
*clause
[TAGCACHE_MAX_CLAUSES
];
104 static struct display_format
*formats
[TAGMENU_MAX_FMTS
];
105 static int format_count
;
107 struct search_instruction
{
109 int tagorder
[MAX_TAGS
];
111 struct tagcache_search_clause
*clause
[MAX_TAGS
][TAGCACHE_MAX_CLAUSES
];
112 int format_id
[MAX_TAGS
];
113 int clause_count
[MAX_TAGS
];
114 int result_seek
[MAX_TAGS
];
120 struct search_instruction
*si
;
126 char id
[MAX_MENU_ID_SIZE
];
128 struct menu_entry
*items
[TAGMENU_MAX_ITEMS
];
131 /* Statusbar text of the current view. */
132 static char current_title
[MAX_TAGS
][128];
134 static struct root_menu
*menus
[TAGMENU_MAX_MENUS
];
135 static struct root_menu
*menu
;
136 static struct search_instruction
*csi
;
137 static const char *strp
;
138 static int menu_count
;
139 static int root_menu
;
141 static int current_offset
;
142 static int current_entry_count
;
144 static int format_count
;
145 static struct tree_context
*tc
;
147 static int get_token_str(char *buf
, int size
)
149 /* Find the start. */
150 while (*strp
!= '"' && *strp
!= '\0')
153 if (*strp
== '\0' || *(++strp
) == '\0')
157 while (*strp
!= '"' && *strp
!= '\0' && --size
> 0)
158 *(buf
++) = *(strp
++);
169 #define MATCH(tag,str1,str2,settag) \
170 if (!strcasecmp(str1, str2)) { \
175 static int get_tag(int *tag
)
180 /* Find the start. */
181 while ((*strp
== ' ' || *strp
== '>') && *strp
!= '\0')
184 if (*strp
== '\0' || *strp
== '?')
187 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
189 if (*strp
== '\0' || *strp
== ' ')
196 MATCH(tag
, buf
, "album", tag_album
);
197 MATCH(tag
, buf
, "artist", tag_artist
);
198 MATCH(tag
, buf
, "bitrate", tag_bitrate
);
199 MATCH(tag
, buf
, "composer", tag_composer
);
200 MATCH(tag
, buf
, "comment", tag_comment
);
201 MATCH(tag
, buf
, "albumartist", tag_albumartist
);
202 MATCH(tag
, buf
, "ensemble", tag_albumartist
);
203 MATCH(tag
, buf
, "genre", tag_genre
);
204 MATCH(tag
, buf
, "length", tag_length
);
205 MATCH(tag
, buf
, "Lm", tag_virt_length_min
);
206 MATCH(tag
, buf
, "Ls", tag_virt_length_sec
);
207 MATCH(tag
, buf
, "Pm", tag_virt_playtime_min
);
208 MATCH(tag
, buf
, "Ps", tag_virt_playtime_sec
);
209 MATCH(tag
, buf
, "title", tag_title
);
210 MATCH(tag
, buf
, "filename", tag_filename
);
211 MATCH(tag
, buf
, "tracknum", tag_tracknumber
);
212 MATCH(tag
, buf
, "year", tag_year
);
213 MATCH(tag
, buf
, "playcount", tag_playcount
);
214 MATCH(tag
, buf
, "rating", tag_rating
);
215 MATCH(tag
, buf
, "lastplayed", tag_lastplayed
);
216 MATCH(tag
, buf
, "commitid", tag_commitid
);
217 MATCH(tag
, buf
, "entryage", tag_virt_entryage
);
218 MATCH(tag
, buf
, "autoscore", tag_virt_autoscore
);
219 MATCH(tag
, buf
, "%sort", var_sorttype
);
220 MATCH(tag
, buf
, "%limit", var_limit
);
221 MATCH(tag
, buf
, "%strip", var_strip
);
222 MATCH(tag
, buf
, "%menu_start", var_menu_start
);
223 MATCH(tag
, buf
, "%include", var_include
);
224 MATCH(tag
, buf
, "%root_menu", var_rootmenu
);
225 MATCH(tag
, buf
, "%format", var_format
);
226 MATCH(tag
, buf
, "->", menu_next
);
227 MATCH(tag
, buf
, "==>", menu_load
);
229 logf("NO MATCH: %s\n", buf
);
236 static int get_clause(int *condition
)
241 /* Find the start. */
242 while (*strp
== ' ' && *strp
!= '\0')
248 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
250 if (*strp
== '\0' || *strp
== ' ')
257 MATCH(condition
, buf
, "=", clause_is
);
258 MATCH(condition
, buf
, "==", clause_is
);
259 MATCH(condition
, buf
, "!=", clause_is_not
);
260 MATCH(condition
, buf
, ">", clause_gt
);
261 MATCH(condition
, buf
, ">=", clause_gteq
);
262 MATCH(condition
, buf
, "<", clause_lt
);
263 MATCH(condition
, buf
, "<=", clause_lteq
);
264 MATCH(condition
, buf
, "~", clause_contains
);
265 MATCH(condition
, buf
, "!~", clause_not_contains
);
266 MATCH(condition
, buf
, "^", clause_begins_with
);
267 MATCH(condition
, buf
, "!^", clause_not_begins_with
);
268 MATCH(condition
, buf
, "$", clause_ends_with
);
269 MATCH(condition
, buf
, "!$", clause_not_ends_with
);
270 MATCH(condition
, buf
, "@", clause_oneof
);
275 static bool read_clause(struct tagcache_search_clause
*clause
)
279 if (get_tag(&clause
->tag
) <= 0)
282 if (get_clause(&clause
->type
) <= 0)
285 if (get_token_str(buf
, sizeof buf
) < 0)
288 clause
->str
= buffer_alloc(strlen(buf
)+1);
289 strcpy(clause
->str
, buf
);
291 logf("got clause: %d/%d [%s]", clause
->tag
, clause
->type
, clause
->str
);
293 if (*(clause
->str
) == '\0')
294 clause
->input
= true;
296 clause
->input
= false;
298 if (tagcache_is_numeric_tag(clause
->tag
))
300 clause
->numeric
= true;
301 clause
->numeric_data
= atoi(clause
->str
);
304 clause
->numeric
= false;
309 static bool read_variable(char *buf
, int size
)
313 if (!get_clause(&condition
))
316 if (condition
!= clause_is
)
319 if (get_token_str(buf
, size
) < 0)
325 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
326 static int get_format_str(struct display_format
*fmt
)
332 memset(fmt
, 0, sizeof(struct display_format
));
334 if (get_token_str(fmt
->name
, sizeof fmt
->name
) < 0)
337 /* Determine the group id */
339 for (i
= 0; i
< format_count
; i
++)
341 if (!strcasecmp(formats
[i
]->name
, fmt
->name
))
343 fmt
->group_id
= formats
[i
]->group_id
;
347 if (formats
[i
]->group_id
> fmt
->group_id
)
348 fmt
->group_id
= formats
[i
]->group_id
;
351 if (i
== format_count
)
354 logf("format: (%d) %s", fmt
->group_id
, fmt
->name
);
356 if (get_token_str(buf
, sizeof buf
) < 0)
359 fmt
->formatstr
= buffer_alloc(strlen(buf
) + 1);
360 strcpy(fmt
->formatstr
, buf
);
362 while (fmt
->tag_count
< MAX_TAGS
)
364 ret
= get_tag(&fmt
->tags
[fmt
->tag_count
]);
371 switch (fmt
->tags
[fmt
->tag_count
]) {
373 if (!read_variable(buf
, sizeof buf
))
375 if (!strcasecmp("inverse", buf
))
376 fmt
->sort_inverse
= true;
382 if (!read_variable(buf
, sizeof buf
))
384 fmt
->limit
= atoi(buf
);
388 if (!read_variable(buf
, sizeof buf
))
390 fmt
->strip
= atoi(buf
);
401 static int add_format(const char *buf
)
405 if (formats
[format_count
] == NULL
)
406 formats
[format_count
] = buffer_alloc(sizeof(struct display_format
));
408 memset(formats
[format_count
], 0, sizeof(struct display_format
));
409 if (get_format_str(formats
[format_count
]) < 0)
411 logf("get_format_str() parser failed!");
415 while (*strp
!= '\0' && *strp
!= '?')
420 int clause_count
= 0;
425 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
427 logf("too many clauses");
431 formats
[format_count
]->clause
[clause_count
] =
432 buffer_alloc(sizeof(struct tagcache_search_clause
));
434 if (!read_clause(formats
[format_count
]->clause
[clause_count
]))
440 formats
[format_count
]->clause_count
= clause_count
;
448 static int get_condition(struct search_instruction
*inst
)
459 if (get_token_str(buf
, sizeof buf
) < 0)
462 for (i
= 0; i
< format_count
; i
++)
464 if (!strcasecmp(formats
[i
]->name
, buf
))
468 if (i
== format_count
)
470 logf("format not found: %s", buf
);
474 inst
->format_id
[inst
->tagorder_count
] = formats
[i
]->group_id
;
487 clause_count
= inst
->clause_count
[inst
->tagorder_count
];
488 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
490 logf("Too many clauses");
494 inst
->clause
[inst
->tagorder_count
][clause_count
] =
495 buffer_alloc(sizeof(struct tagcache_search_clause
));
497 if (!read_clause(inst
->clause
[inst
->tagorder_count
][clause_count
]))
500 inst
->clause_count
[inst
->tagorder_count
]++;
506 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
507 * : album ? year >= "2000" : songs
513 static bool parse_search(struct menu_entry
*entry
, const char *str
)
517 struct search_instruction
*inst
= entry
->si
;
520 struct root_menu
*new_menu
;
524 /* Parse entry name */
525 if (get_token_str(entry
->name
, sizeof entry
->name
) < 0)
527 logf("No name found.");
531 /* Parse entry type */
532 if (get_tag(&entry
->type
) <= 0)
535 if (entry
->type
== menu_load
)
537 if (get_token_str(buf
, sizeof buf
) < 0)
540 /* Find the matching root menu or "create" it */
541 for (i
= 0; i
< menu_count
; i
++)
543 if (!strcasecmp(menus
[i
]->id
, buf
))
550 if (menu_count
>= TAGMENU_MAX_MENUS
)
552 logf("max menucount reached");
556 /* Allocate a new menu unless link is found. */
557 menus
[menu_count
] = buffer_alloc(sizeof(struct root_menu
));
558 new_menu
= menus
[menu_count
];
559 memset(new_menu
, 0, sizeof(struct root_menu
));
560 strncpy(new_menu
->id
, buf
, MAX_MENU_ID_SIZE
-1);
561 entry
->link
= menu_count
;
567 if (entry
->type
!= menu_next
)
570 while (inst
->tagorder_count
< MAX_TAGS
)
572 ret
= get_tag(&inst
->tagorder
[inst
->tagorder_count
]);
575 logf("Parse error #1");
583 logf("tag: %d", inst
->tagorder
[inst
->tagorder_count
]);
585 while ( (ret
= get_condition(inst
)) > 0 ) ;
589 inst
->tagorder_count
++;
591 if (get_tag(&type
) <= 0 || type
!= menu_next
)
598 static int compare(const void *p1
, const void *p2
)
600 struct tagentry
*e1
= (struct tagentry
*)p1
;
601 struct tagentry
*e2
= (struct tagentry
*)p2
;
604 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
606 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
609 static void tagtree_buffer_event(struct mp3entry
*id3
, bool last_track
)
614 /* Do not gather data unless proper setting has been enabled. */
615 if (!global_settings
.runtimedb
)
618 logf("be:%d%s", last_track
, id3
->path
);
620 if (!tagcache_find_index(&tcs
, id3
->path
))
622 logf("tc stat: not found: %s", id3
->path
);
626 id3
->playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
628 id3
->rating
= tagcache_get_numeric(&tcs
, tag_rating
);
629 id3
->lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
630 id3
->score
= tagcache_get_numeric(&tcs
, tag_virt_autoscore
) / 10;
631 id3
->playtime
= tagcache_get_numeric(&tcs
, tag_playtime
);
633 /* Store our tagcache index pointer. */
634 id3
->tagcache_idx
= tcs
.idx_id
;
636 tagcache_search_finish(&tcs
);
639 static void tagtree_unbuffer_event(struct mp3entry
*id3
, bool last_track
)
646 /* Do not gather data unless proper setting has been enabled. */
647 if (!global_settings
.runtimedb
)
649 logf("runtimedb gathering not enabled");
653 if (!id3
->tagcache_idx
)
655 logf("No tagcache index pointer found");
659 /* Don't process unplayed tracks. */
660 if (id3
->elapsed
== 0)
662 logf("not logging unplayed track");
666 playcount
= id3
->playcount
+ 1;
667 lastplayed
= tagcache_increase_serial();
670 logf("incorrect tc serial:%ld", lastplayed
);
674 /* Ignore the last 15s (crossfade etc.) */
675 playtime
= id3
->playtime
+ MIN(id3
->length
, id3
->elapsed
+ 15 * 1000);
677 logf("ube:%s", id3
->path
);
678 logf("-> %d/%ld/%ld", last_track
, playcount
, playtime
);
679 logf("-> %ld/%ld/%ld", id3
->elapsed
, id3
->length
, MIN(id3
->length
, id3
->elapsed
+ 15 * 1000));
681 /* Queue the updates to the tagcache system. */
682 tagcache_update_numeric(id3
->tagcache_idx
, tag_playcount
, playcount
);
683 tagcache_update_numeric(id3
->tagcache_idx
, tag_playtime
, playtime
);
684 tagcache_update_numeric(id3
->tagcache_idx
, tag_lastplayed
, lastplayed
);
687 bool tagtree_export(void)
689 gui_syncsplash(0, str(LANG_CREATING
));
690 if (!tagcache_create_changelog(&tcs
))
692 gui_syncsplash(HZ
*2, str(LANG_FAILED
));
698 bool tagtree_import(void)
700 gui_syncsplash(0, str(LANG_WAIT
));
701 if (!tagcache_import_changelog())
703 gui_syncsplash(HZ
*2, str(LANG_FAILED
));
709 static bool parse_menu(const char *filename
);
711 static int parse_line(int n
, const char *buf
, void *parameters
)
715 static bool read_menu
;
720 logf("parse:%d/%s", n
, buf
);
722 /* First line, do initialisation. */
725 if (strcasecmp(TAGNAVI_VERSION
, buf
))
727 logf("Version mismatch");
750 if (get_tag(&variable
) <= 0)
756 if (add_format(strp
) < 0)
758 logf("Format add fail: %s", data
);
763 if (get_token_str(data
, sizeof(data
)) < 0)
765 logf("%%include empty");
769 if (!parse_menu(data
))
771 logf("Load menu fail: %s", data
);
776 if (menu_count
>= TAGMENU_MAX_MENUS
)
778 logf("max menucount reached");
782 if (get_token_str(data
, sizeof data
) < 0)
784 logf("%%menu_start id empty");
789 for (i
= 0; i
< menu_count
; i
++)
791 if (!strcasecmp(menus
[i
]->id
, data
))
799 menus
[menu_count
] = buffer_alloc(sizeof(struct root_menu
));
800 menu
= menus
[menu_count
];
802 memset(menu
, 0, sizeof(struct root_menu
));
803 strncpy(menu
->id
, data
, MAX_MENU_ID_SIZE
-1);
806 if (get_token_str(menu
->title
, sizeof(menu
->title
)) < 0)
808 logf("%%menu_start title empty");
811 logf("menu: %s", menu
->title
);
816 /* Only set root menu once. */
820 if (get_token_str(data
, sizeof(data
)) < 0)
822 logf("%%root_menu empty");
826 for (i
= 0; i
< menu_count
; i
++)
828 if (!strcasecmp(menus
[i
]->id
, data
))
839 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
841 logf("max itemcount reached");
846 if (menu
->items
[menu
->itemcount
] == NULL
)
848 menu
->items
[menu
->itemcount
] = buffer_alloc(sizeof(struct menu_entry
));
849 memset(menu
->items
[menu
->itemcount
], 0, sizeof(struct menu_entry
));
850 menu
->items
[menu
->itemcount
]->si
= buffer_alloc(sizeof(struct search_instruction
));
853 memset(menu
->items
[menu
->itemcount
]->si
, 0, sizeof(struct search_instruction
));
854 if (!parse_search(menu
->items
[menu
->itemcount
], buf
))
862 static bool parse_menu(const char *filename
)
867 if (menu_count
>= TAGMENU_MAX_MENUS
)
869 logf("max menucount reached");
873 fd
= open(filename
, O_RDONLY
);
876 logf("Search instruction file not found.");
880 /* Now read file for real, parsing into si */
881 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
887 void tagtree_init(void)
893 parse_menu(FILE_SEARCH_INSTRUCTIONS
);
895 /* If no root menu is set, assume it's the first single menu
896 * we have. That shouldn't normally happen. */
900 uniqbuf
= buffer_alloc(UNIQBUF_SIZE
);
901 audio_set_track_buffer_event(tagtree_buffer_event
);
902 audio_set_track_unbuffer_event(tagtree_unbuffer_event
);
905 static bool show_search_progress(bool init
, int count
)
907 static int last_tick
= 0;
911 last_tick
= current_tick
;
915 if (current_tick
- last_tick
> HZ
/4)
917 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG
), count
,
918 #if CONFIG_KEYPAD == PLAYER_PAD
924 if (action_userabort(TIMEOUT_NOBLOCK
))
926 last_tick
= current_tick
;
933 static int format_str(struct tagcache_search
*tcs
, struct display_format
*fmt
,
934 char *buf
, int buf_size
)
937 bool read_format
= false;
943 memset(buf
, 0, buf_size
);
944 for (i
= 0; fmt
->formatstr
[i
] != '\0'; i
++)
946 if (fmt
->formatstr
[i
] == '%')
950 if (parpos
>= fmt
->tag_count
)
952 logf("too many format tags");
959 fmtbuf
[fmtbuf_pos
++] = fmt
->formatstr
[i
];
960 if (fmtbuf_pos
>= buf_size
)
962 logf("format parse error");
966 if (fmt
->formatstr
[i
] == 's')
968 fmtbuf
[fmtbuf_pos
] = '\0';
970 if (fmt
->tags
[parpos
] == tcs
->type
)
972 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
, tcs
->result
);
976 /* Need to fetch the tag data. */
977 if (!tagcache_retrieve(tcs
, tcs
->idx_id
, fmt
->tags
[parpos
],
978 &buf
[buf_pos
], buf_size
- buf_pos
))
980 logf("retrieve failed");
984 buf_pos
+= strlen(&buf
[buf_pos
]);
987 else if (fmt
->formatstr
[i
] == 'd')
989 fmtbuf
[fmtbuf_pos
] = '\0';
991 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
,
992 tagcache_get_numeric(tcs
, fmt
->tags
[parpos
]));
993 buf_pos
+= strlen(&buf
[buf_pos
]);
999 buf
[buf_pos
++] = fmt
->formatstr
[i
];
1001 if (buf_pos
- 1 >= buf_size
)
1003 logf("buffer overflow");
1008 buf
[buf_pos
++] = '\0';
1013 static int retrieve_entries(struct tree_context
*c
, struct tagcache_search
*tcs
,
1014 int offset
, bool init
)
1016 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1017 struct display_format
*fmt
;
1019 int namebufused
= 0;
1020 int total_count
= 0;
1021 int special_entry_count
= 0;
1022 int level
= c
->currextra
;
1029 #ifdef HAVE_TC_RAMCACHE
1030 && !tagcache_is_ramcache()
1034 show_search_progress(true, 0);
1035 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG
),
1039 if (c
->currtable
== allsubentries
)
1045 tag
= csi
->tagorder
[level
];
1047 if (!tagcache_search(tcs
, tag
))
1050 /* Prevent duplicate entries in the search list. */
1051 tagcache_search_set_uniqbuf(tcs
, uniqbuf
, UNIQBUF_SIZE
);
1053 if (level
|| csi
->clause_count
[0] || tagcache_is_numeric_tag(tag
))
1056 for (i
= 0; i
< level
; i
++)
1058 if (tagcache_is_numeric_tag(csi
->tagorder
[i
]))
1060 static struct tagcache_search_clause cc
;
1062 memset(&cc
, 0, sizeof(struct tagcache_search_clause
));
1063 cc
.tag
= csi
->tagorder
[i
];
1064 cc
.type
= clause_is
;
1066 cc
.numeric_data
= csi
->result_seek
[i
];
1067 tagcache_search_add_clause(tcs
, &cc
);
1071 tagcache_search_add_filter(tcs
, csi
->tagorder
[i
],
1072 csi
->result_seek
[i
]);
1076 for (i
= 0; i
<= level
; i
++)
1080 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1081 tagcache_search_add_clause(tcs
, csi
->clause
[i
][j
]);
1084 current_offset
= offset
;
1085 current_entry_count
= 0;
1089 for (i
= 0; i
< format_count
; i
++)
1091 if (formats
[i
]->group_id
== csi
->format_id
[level
])
1097 sort_inverse
= fmt
->sort_inverse
;
1098 sort_limit
= fmt
->limit
;
1101 /* Check if sorting is forced. */
1107 sort_inverse
= false;
1112 if (tag
!= tag_title
&& tag
!= tag_filename
)
1116 dptr
->newtable
= allsubentries
;
1117 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1119 current_entry_count
++;
1121 special_entry_count
++;
1124 total_count
+= special_entry_count
;
1126 while (tagcache_get_next(tcs
))
1128 if (total_count
++ < offset
)
1131 dptr
->newtable
= navibrowse
;
1132 if (tag
== tag_title
|| tag
== tag_filename
)
1134 dptr
->newtable
= playtrack
;
1135 dptr
->extraseek
= tcs
->idx_id
;
1138 dptr
->extraseek
= tcs
->result_seek
;
1141 /* Check the format */
1142 for (i
= 0; i
< format_count
; i
++)
1144 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1147 if (tagcache_check_clauses(tcs
, formats
[i
]->clause
,
1148 formats
[i
]->clause_count
))
1155 if (!tcs
->ramresult
|| fmt
)
1161 if (format_str(tcs
, fmt
, buf
, sizeof buf
) < 0)
1163 logf("format_str() failed");
1168 dptr
->name
= &c
->name_buffer
[namebufused
];
1170 namebufused
+= strlen(buf
)+1;
1172 namebufused
+= tcs
->result_len
;
1174 if (namebufused
>= c
->name_buffer_size
)
1176 logf("chunk mode #2: %d", current_entry_count
);
1182 strcpy(dptr
->name
, buf
);
1184 strcpy(dptr
->name
, tcs
->result
);
1187 dptr
->name
= tcs
->result
;
1190 current_entry_count
++;
1192 if (current_entry_count
>= global_settings
.max_files_in_dir
)
1194 logf("chunk mode #3: %d", current_entry_count
);
1200 if (init
&& !tcs
->ramsearch
)
1202 if (!show_search_progress(false, i
))
1204 tagcache_search_finish(tcs
);
1205 return current_entry_count
;
1211 qsort(c
->dircache
+ special_entry_count
* c
->dentry_size
,
1212 current_entry_count
- special_entry_count
,
1213 c
->dentry_size
, compare
);
1217 tagcache_search_finish(tcs
);
1218 return current_entry_count
;
1221 while (tagcache_get_next(tcs
))
1223 if (!tcs
->ramsearch
)
1225 if (!show_search_progress(false, total_count
))
1231 tagcache_search_finish(tcs
);
1233 if (!sort
&& (sort_inverse
|| sort_limit
))
1235 gui_syncsplash(HZ
*4, str(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1236 logf("Too small dir buffer");
1241 total_count
= MIN(total_count
, sort_limit
);
1246 for (i
= 0; i
< total_count
; i
++, dptr
++)
1248 int len
= strlen(dptr
->name
);
1253 dptr
->name
= &dptr
->name
[strip
];
1260 static int load_root(struct tree_context
*c
)
1262 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1266 c
->currtable
= root
;
1267 if (c
->dirlevel
== 0)
1268 c
->currextra
= root_menu
;
1270 menu
= menus
[c
->currextra
];
1274 for (i
= 0; i
< menu
->itemcount
; i
++)
1276 dptr
->name
= menu
->items
[i
]->name
;
1277 switch (menu
->items
[i
]->type
)
1280 dptr
->newtable
= navibrowse
;
1281 dptr
->extraseek
= i
;
1285 dptr
->newtable
= root
;
1286 dptr
->extraseek
= menu
->items
[i
]->link
;
1294 current_entry_count
= i
;
1299 int tagtree_load(struct tree_context
* c
)
1302 int table
= c
->currtable
;
1304 c
->dentry_size
= sizeof(struct tagentry
);
1311 c
->currtable
= table
;
1312 c
->currextra
= root_menu
;
1318 count
= load_root(c
);
1323 logf("navibrowse...");
1325 count
= retrieve_entries(c
, &tcs
, 0, true);
1330 logf("Unsupported table %d\n", table
);
1337 count
= load_root(c
);
1338 gui_syncsplash(HZ
, str(LANG_TAGCACHE_BUSY
));
1341 /* The _total_ numer of entries available. */
1342 c
->dirlength
= c
->filesindir
= count
;
1347 int tagtree_enter(struct tree_context
* c
)
1350 struct tagentry
*dptr
;
1354 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1357 newextra
= dptr
->newtable
;
1358 seek
= dptr
->extraseek
;
1360 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1363 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1364 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1365 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1366 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1369 switch (c
->currtable
) {
1371 c
->currextra
= newextra
;
1373 if (newextra
== root
)
1376 c
->currextra
= seek
;
1379 else if (newextra
== navibrowse
)
1383 csi
= menu
->items
[seek
]->si
;
1386 strncpy(current_title
[c
->currextra
], dptr
->name
,
1387 sizeof(current_title
[0]) - 1);
1389 /* Read input as necessary. */
1390 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1392 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1394 if (!csi
->clause
[i
][j
]->input
)
1397 rc
= kbd_input(searchstring
, sizeof(searchstring
));
1398 if (rc
== -1 || !searchstring
[0])
1404 if (csi
->clause
[i
][j
]->numeric
)
1405 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1407 csi
->clause
[i
][j
]->str
= searchstring
;
1411 c
->currtable
= newextra
;
1417 if (newextra
== playtrack
)
1420 /* about to create a new current playlist...
1421 allow user to cancel the operation */
1422 if (global_settings
.warnon_erase_dynplaylist
&&
1423 !global_settings
.party_mode
&&
1424 playlist_modified(NULL
))
1426 char *lines
[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
1427 struct text_message message
={lines
, 1};
1429 if (gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
1433 if (tagtree_play_folder(c
) >= 0)
1438 c
->currtable
= newextra
;
1439 csi
->result_seek
[c
->currextra
] = seek
;
1440 if (c
->currextra
< csi
->tagorder_count
-1)
1445 /* Update the statusbar title */
1446 strncpy(current_title
[c
->currextra
], dptr
->name
,
1447 sizeof(current_title
[0]) - 1);
1456 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1461 void tagtree_exit(struct tree_context
* c
)
1464 if (c
->dirlevel
> 0)
1466 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1467 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1468 c
->currtable
= c
->table_history
[c
->dirlevel
];
1469 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1470 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1473 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1475 struct tagentry
*entry
;
1477 entry
= tagtree_get_entry(c
, c
->selected_item
);
1479 if (!tagcache_search(&tcs
, tag_filename
))
1482 if (!tagcache_retrieve(&tcs
, entry
->extraseek
, tcs
.type
, buf
, buflen
))
1484 tagcache_search_finish(&tcs
);
1488 tagcache_search_finish(&tcs
);
1493 static bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1497 int from
, to
, direction
;
1498 int files_left
= c
->filesindir
;
1501 if (!tagcache_search(&tcs
, tag_filename
))
1503 gui_syncsplash(HZ
, str(LANG_TAGCACHE_BUSY
));
1508 if (position
== PLAYLIST_REPLACE
)
1510 if (remove_all_tracks(NULL
) == 0)
1511 position
= PLAYLIST_INSERT_LAST
;
1514 if (position
== PLAYLIST_INSERT_FIRST
)
1516 from
= c
->filesindir
- 1;
1527 for (i
= from
; i
!= to
; i
+= direction
)
1529 /* Count back to zero */
1530 if (!show_search_progress(false, files_left
--))
1533 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1534 tcs
.type
, buf
, sizeof buf
))
1539 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1541 logf("playlist_insert_track failed");
1546 playlist_sync(NULL
);
1547 tagcache_search_finish(&tcs
);
1553 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1555 struct tagentry
*dptr
;
1557 int dirlevel
= tc
->dirlevel
;
1559 /* We need to set the table to allsubentries. */
1560 show_search_progress(true, 0);
1562 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1564 /* Insert a single track? */
1565 if (dptr
->newtable
== playtrack
)
1567 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1569 logf("tagtree_get_filename failed");
1572 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1577 if (dptr
->newtable
== navibrowse
)
1581 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1583 else if (dptr
->newtable
!= allsubentries
)
1585 logf("unsupported table: %d", dptr
->newtable
);
1589 /* Now the current table should be allsubentries. */
1590 if (dptr
->newtable
!= playtrack
)
1594 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1596 /* And now the newtable should be playtrack. */
1597 if (dptr
->newtable
!= playtrack
)
1599 logf("newtable: %d !!", dptr
->newtable
);
1600 tc
->dirlevel
= dirlevel
;
1605 if (tc
->filesindir
<= 0)
1606 gui_syncsplash(HZ
, str(LANG_END_PLAYLIST_PLAYER
));
1609 logf("insert_all_playlist");
1610 if (!insert_all_playlist(tc
, position
, queue
))
1611 gui_syncsplash(HZ
*2, str(LANG_FAILED
));
1614 /* Finally return the dirlevel to its original value. */
1615 while (tc
->dirlevel
> dirlevel
)
1622 static int tagtree_play_folder(struct tree_context
* c
)
1624 if (playlist_create(NULL
, NULL
) < 0)
1626 logf("Failed creating playlist\n");
1630 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1633 if (global_settings
.playlist_shuffle
)
1634 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
1635 if (!global_settings
.play_selected
)
1636 c
->selected_item
= 0;
1637 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1639 playlist_start(c
->selected_item
,0);
1644 struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
1646 struct tagentry
*entry
= (struct tagentry
*)c
->dircache
;
1647 int realid
= id
- current_offset
;
1649 /* Load the next chunk if necessary. */
1650 if (realid
>= current_entry_count
|| realid
< 0)
1653 if (retrieve_entries(c
, &tcs2
, MAX(0, id
- (current_entry_count
/ 2)),
1656 logf("retrieve failed");
1660 realid
= id
- current_offset
;
1664 return &entry
[realid
];
1667 char *tagtree_get_title(struct tree_context
* c
)
1669 switch (c
->currtable
)
1676 return current_title
[c
->currextra
];
1682 int tagtree_get_attr(struct tree_context
* c
)
1685 switch (c
->currtable
)
1688 if (csi
->tagorder
[c
->currextra
] == tag_title
)
1689 attr
= FILE_ATTR_AUDIO
;
1691 attr
= ATTR_DIRECTORY
;
1695 attr
= FILE_ATTR_AUDIO
;
1699 attr
= ATTR_DIRECTORY
;
1706 int tagtree_get_icon(struct tree_context
* c
)
1708 int icon
= Icon_Folder
;
1710 if (tagtree_get_attr(c
) == FILE_ATTR_AUDIO
)