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
);
627 if(!id3
->rating
) id3
->rating
= tagcache_get_numeric(&tcs
, tag_rating
);
628 id3
->lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
629 id3
->score
= tagcache_get_numeric(&tcs
, tag_virt_autoscore
) / 10;
631 tagcache_search_finish(&tcs
);
634 static void tagtree_unbuffer_event(struct mp3entry
*id3
, bool last_track
)
642 /* Do not gather data unless proper setting has been enabled. */
643 if (!global_settings
.runtimedb
)
645 logf("runtimedb gathering not enabled");
649 /* Don't process unplayed tracks. */
650 if (id3
->elapsed
== 0)
652 logf("not logging unplayed track");
656 if (!tagcache_find_index(&tcs
, id3
->path
))
658 logf("tc stat: not found: %s", id3
->path
);
662 playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
663 playtime
= tagcache_get_numeric(&tcs
, tag_playtime
);
664 lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
668 rating
= (long) id3
->rating
;
670 lastplayed
= tagcache_increase_serial();
673 logf("incorrect tc serial:%ld", lastplayed
);
674 tagcache_search_finish(&tcs
);
678 /* Ignore the last 15s (crossfade etc.) */
679 playtime
+= MIN(id3
->length
, id3
->elapsed
+ 15 * 1000);
681 logf("ube:%s", id3
->path
);
682 logf("-> %d/%ld/%ld/%ld", last_track
, playcount
, rating
, playtime
);
683 logf("-> %ld/%ld/%ld", id3
->elapsed
, id3
->length
, MIN(id3
->length
, id3
->elapsed
+ 15 * 1000));
685 /* lastplayed not yet supported. */
687 if (!tagcache_modify_numeric_entry(&tcs
, tag_playcount
, playcount
)
688 || !tagcache_modify_numeric_entry(&tcs
, tag_rating
, rating
)
689 || !tagcache_modify_numeric_entry(&tcs
, tag_playtime
, playtime
)
690 || !tagcache_modify_numeric_entry(&tcs
, tag_lastplayed
, lastplayed
))
692 logf("tc stat: modify failed!");
693 tagcache_search_finish(&tcs
);
697 tagcache_search_finish(&tcs
);
700 bool tagtree_export(void)
702 gui_syncsplash(0, str(LANG_CREATING
));
703 if (!tagcache_create_changelog(&tcs
))
705 gui_syncsplash(HZ
*2, str(LANG_FAILED
));
711 bool tagtree_import(void)
713 gui_syncsplash(0, str(LANG_WAIT
));
714 if (!tagcache_import_changelog())
716 gui_syncsplash(HZ
*2, str(LANG_FAILED
));
722 static bool parse_menu(const char *filename
);
724 static int parse_line(int n
, const char *buf
, void *parameters
)
728 static bool read_menu
;
733 logf("parse:%d/%s", n
, buf
);
735 /* First line, do initialisation. */
738 if (strcasecmp(TAGNAVI_VERSION
, buf
))
740 logf("Version mismatch");
763 if (get_tag(&variable
) <= 0)
769 if (add_format(strp
) < 0)
771 logf("Format add fail: %s", data
);
776 if (get_token_str(data
, sizeof(data
)) < 0)
778 logf("%%include empty");
782 if (!parse_menu(data
))
784 logf("Load menu fail: %s", data
);
789 if (menu_count
>= TAGMENU_MAX_MENUS
)
791 logf("max menucount reached");
795 if (get_token_str(data
, sizeof data
) < 0)
797 logf("%%menu_start id empty");
802 for (i
= 0; i
< menu_count
; i
++)
804 if (!strcasecmp(menus
[i
]->id
, data
))
812 menus
[menu_count
] = buffer_alloc(sizeof(struct root_menu
));
813 menu
= menus
[menu_count
];
815 memset(menu
, 0, sizeof(struct root_menu
));
816 strncpy(menu
->id
, data
, MAX_MENU_ID_SIZE
-1);
819 if (get_token_str(menu
->title
, sizeof(menu
->title
)) < 0)
821 logf("%%menu_start title empty");
824 logf("menu: %s", menu
->title
);
829 /* Only set root menu once. */
833 if (get_token_str(data
, sizeof(data
)) < 0)
835 logf("%%root_menu empty");
839 for (i
= 0; i
< menu_count
; i
++)
841 if (!strcasecmp(menus
[i
]->id
, data
))
852 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
854 logf("max itemcount reached");
859 if (menu
->items
[menu
->itemcount
] == NULL
)
861 menu
->items
[menu
->itemcount
] = buffer_alloc(sizeof(struct menu_entry
));
862 memset(menu
->items
[menu
->itemcount
], 0, sizeof(struct menu_entry
));
863 menu
->items
[menu
->itemcount
]->si
= buffer_alloc(sizeof(struct search_instruction
));
866 memset(menu
->items
[menu
->itemcount
]->si
, 0, sizeof(struct search_instruction
));
867 if (!parse_search(menu
->items
[menu
->itemcount
], buf
))
875 static bool parse_menu(const char *filename
)
880 if (menu_count
>= TAGMENU_MAX_MENUS
)
882 logf("max menucount reached");
886 fd
= open(filename
, O_RDONLY
);
889 logf("Search instruction file not found.");
893 /* Now read file for real, parsing into si */
894 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
900 void tagtree_init(void)
906 parse_menu(FILE_SEARCH_INSTRUCTIONS
);
908 /* If no root menu is set, assume it's the first single menu
909 * we have. That shouldn't normally happen. */
913 uniqbuf
= buffer_alloc(UNIQBUF_SIZE
);
914 audio_set_track_buffer_event(tagtree_buffer_event
);
915 audio_set_track_unbuffer_event(tagtree_unbuffer_event
);
918 static bool show_search_progress(bool init
, int count
)
920 static int last_tick
= 0;
924 last_tick
= current_tick
;
928 if (current_tick
- last_tick
> HZ
/4)
930 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG
), count
,
931 #if CONFIG_KEYPAD == PLAYER_PAD
937 if (action_userabort(TIMEOUT_NOBLOCK
))
939 last_tick
= current_tick
;
946 static int format_str(struct tagcache_search
*tcs
, struct display_format
*fmt
,
947 char *buf
, int buf_size
)
950 bool read_format
= false;
956 memset(buf
, 0, buf_size
);
957 for (i
= 0; fmt
->formatstr
[i
] != '\0'; i
++)
959 if (fmt
->formatstr
[i
] == '%')
963 if (parpos
>= fmt
->tag_count
)
965 logf("too many format tags");
972 fmtbuf
[fmtbuf_pos
++] = fmt
->formatstr
[i
];
973 if (fmtbuf_pos
>= buf_size
)
975 logf("format parse error");
979 if (fmt
->formatstr
[i
] == 's')
981 fmtbuf
[fmtbuf_pos
] = '\0';
983 if (fmt
->tags
[parpos
] == tcs
->type
)
985 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
, tcs
->result
);
989 /* Need to fetch the tag data. */
990 if (!tagcache_retrieve(tcs
, tcs
->idx_id
, fmt
->tags
[parpos
],
991 &buf
[buf_pos
], buf_size
- buf_pos
))
993 logf("retrieve failed");
997 buf_pos
+= strlen(&buf
[buf_pos
]);
1000 else if (fmt
->formatstr
[i
] == 'd')
1002 fmtbuf
[fmtbuf_pos
] = '\0';
1003 read_format
= false;
1004 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
,
1005 tagcache_get_numeric(tcs
, fmt
->tags
[parpos
]));
1006 buf_pos
+= strlen(&buf
[buf_pos
]);
1012 buf
[buf_pos
++] = fmt
->formatstr
[i
];
1014 if (buf_pos
- 1 >= buf_size
)
1016 logf("buffer overflow");
1021 buf
[buf_pos
++] = '\0';
1026 static int retrieve_entries(struct tree_context
*c
, struct tagcache_search
*tcs
,
1027 int offset
, bool init
)
1029 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1030 struct display_format
*fmt
;
1032 int namebufused
= 0;
1033 int total_count
= 0;
1034 int special_entry_count
= 0;
1035 int level
= c
->currextra
;
1042 #ifdef HAVE_TC_RAMCACHE
1043 && !tagcache_is_ramcache()
1047 show_search_progress(true, 0);
1048 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG
),
1052 if (c
->currtable
== allsubentries
)
1058 tag
= csi
->tagorder
[level
];
1060 if (!tagcache_search(tcs
, tag
))
1063 /* Prevent duplicate entries in the search list. */
1064 tagcache_search_set_uniqbuf(tcs
, uniqbuf
, UNIQBUF_SIZE
);
1066 if (level
|| csi
->clause_count
[0] || tagcache_is_numeric_tag(tag
))
1069 for (i
= 0; i
< level
; i
++)
1071 if (tagcache_is_numeric_tag(csi
->tagorder
[i
]))
1073 static struct tagcache_search_clause cc
;
1075 memset(&cc
, 0, sizeof(struct tagcache_search_clause
));
1076 cc
.tag
= csi
->tagorder
[i
];
1077 cc
.type
= clause_is
;
1079 cc
.numeric_data
= csi
->result_seek
[i
];
1080 tagcache_search_add_clause(tcs
, &cc
);
1084 tagcache_search_add_filter(tcs
, csi
->tagorder
[i
],
1085 csi
->result_seek
[i
]);
1089 for (i
= 0; i
<= level
; i
++)
1093 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1094 tagcache_search_add_clause(tcs
, csi
->clause
[i
][j
]);
1097 current_offset
= offset
;
1098 current_entry_count
= 0;
1102 for (i
= 0; i
< format_count
; i
++)
1104 if (formats
[i
]->group_id
== csi
->format_id
[level
])
1110 sort_inverse
= fmt
->sort_inverse
;
1111 sort_limit
= fmt
->limit
;
1114 /* Check if sorting is forced. */
1120 sort_inverse
= false;
1125 if (tag
!= tag_title
&& tag
!= tag_filename
)
1129 dptr
->newtable
= allsubentries
;
1130 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1132 current_entry_count
++;
1134 special_entry_count
++;
1137 total_count
+= special_entry_count
;
1139 while (tagcache_get_next(tcs
))
1141 if (total_count
++ < offset
)
1144 dptr
->newtable
= navibrowse
;
1145 if (tag
== tag_title
|| tag
== tag_filename
)
1147 dptr
->newtable
= playtrack
;
1148 dptr
->extraseek
= tcs
->idx_id
;
1151 dptr
->extraseek
= tcs
->result_seek
;
1154 /* Check the format */
1155 for (i
= 0; i
< format_count
; i
++)
1157 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1160 if (tagcache_check_clauses(tcs
, formats
[i
]->clause
,
1161 formats
[i
]->clause_count
))
1168 if (!tcs
->ramresult
|| fmt
)
1174 if (format_str(tcs
, fmt
, buf
, sizeof buf
) < 0)
1176 logf("format_str() failed");
1181 dptr
->name
= &c
->name_buffer
[namebufused
];
1183 namebufused
+= strlen(buf
)+1;
1185 namebufused
+= tcs
->result_len
;
1187 if (namebufused
>= c
->name_buffer_size
)
1189 logf("chunk mode #2: %d", current_entry_count
);
1195 strcpy(dptr
->name
, buf
);
1197 strcpy(dptr
->name
, tcs
->result
);
1200 dptr
->name
= tcs
->result
;
1203 current_entry_count
++;
1205 if (current_entry_count
>= global_settings
.max_files_in_dir
)
1207 logf("chunk mode #3: %d", current_entry_count
);
1213 if (init
&& !tcs
->ramsearch
)
1215 if (!show_search_progress(false, i
))
1217 tagcache_search_finish(tcs
);
1218 return current_entry_count
;
1224 qsort(c
->dircache
+ special_entry_count
* c
->dentry_size
,
1225 current_entry_count
- special_entry_count
,
1226 c
->dentry_size
, compare
);
1230 tagcache_search_finish(tcs
);
1231 return current_entry_count
;
1234 while (tagcache_get_next(tcs
))
1236 if (!tcs
->ramsearch
)
1238 if (!show_search_progress(false, total_count
))
1244 tagcache_search_finish(tcs
);
1246 if (!sort
&& (sort_inverse
|| sort_limit
))
1248 gui_syncsplash(HZ
*4, str(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1249 logf("Too small dir buffer");
1254 total_count
= MIN(total_count
, sort_limit
);
1259 for (i
= 0; i
< total_count
; i
++, dptr
++)
1261 int len
= strlen(dptr
->name
);
1266 dptr
->name
= &dptr
->name
[strip
];
1273 static int load_root(struct tree_context
*c
)
1275 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1279 c
->currtable
= root
;
1280 if (c
->dirlevel
== 0)
1281 c
->currextra
= root_menu
;
1283 menu
= menus
[c
->currextra
];
1287 for (i
= 0; i
< menu
->itemcount
; i
++)
1289 dptr
->name
= menu
->items
[i
]->name
;
1290 switch (menu
->items
[i
]->type
)
1293 dptr
->newtable
= navibrowse
;
1294 dptr
->extraseek
= i
;
1298 dptr
->newtable
= root
;
1299 dptr
->extraseek
= menu
->items
[i
]->link
;
1307 current_entry_count
= i
;
1312 int tagtree_load(struct tree_context
* c
)
1315 int table
= c
->currtable
;
1317 c
->dentry_size
= sizeof(struct tagentry
);
1324 c
->currtable
= table
;
1325 c
->currextra
= root_menu
;
1331 count
= load_root(c
);
1336 logf("navibrowse...");
1338 count
= retrieve_entries(c
, &tcs
, 0, true);
1343 logf("Unsupported table %d\n", table
);
1350 count
= load_root(c
);
1351 gui_syncsplash(HZ
, str(LANG_TAGCACHE_BUSY
));
1354 /* The _total_ numer of entries available. */
1355 c
->dirlength
= c
->filesindir
= count
;
1360 int tagtree_enter(struct tree_context
* c
)
1363 struct tagentry
*dptr
;
1367 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1370 newextra
= dptr
->newtable
;
1371 seek
= dptr
->extraseek
;
1373 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1376 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1377 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1378 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1379 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1382 switch (c
->currtable
) {
1384 c
->currextra
= newextra
;
1386 if (newextra
== root
)
1389 c
->currextra
= seek
;
1392 else if (newextra
== navibrowse
)
1396 csi
= menu
->items
[seek
]->si
;
1399 strncpy(current_title
[c
->currextra
], dptr
->name
,
1400 sizeof(current_title
[0]) - 1);
1402 /* Read input as necessary. */
1403 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1405 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1407 if (!csi
->clause
[i
][j
]->input
)
1410 rc
= kbd_input(searchstring
, sizeof(searchstring
));
1411 if (rc
== -1 || !searchstring
[0])
1417 if (csi
->clause
[i
][j
]->numeric
)
1418 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1420 csi
->clause
[i
][j
]->str
= searchstring
;
1424 c
->currtable
= newextra
;
1430 if (newextra
== playtrack
)
1433 /* about to create a new current playlist...
1434 allow user to cancel the operation */
1435 if (global_settings
.warnon_erase_dynplaylist
&&
1436 !global_settings
.party_mode
&&
1437 playlist_modified(NULL
))
1439 char *lines
[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
1440 struct text_message message
={lines
, 1};
1442 if (gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
1446 if (tagtree_play_folder(c
) >= 0)
1451 c
->currtable
= newextra
;
1452 csi
->result_seek
[c
->currextra
] = seek
;
1453 if (c
->currextra
< csi
->tagorder_count
-1)
1458 /* Update the statusbar title */
1459 strncpy(current_title
[c
->currextra
], dptr
->name
,
1460 sizeof(current_title
[0]) - 1);
1469 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1474 void tagtree_exit(struct tree_context
* c
)
1477 if (c
->dirlevel
> 0)
1479 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1480 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1481 c
->currtable
= c
->table_history
[c
->dirlevel
];
1482 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1483 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1486 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1488 struct tagentry
*entry
;
1490 entry
= tagtree_get_entry(c
, c
->selected_item
);
1492 if (!tagcache_search(&tcs
, tag_filename
))
1495 if (!tagcache_retrieve(&tcs
, entry
->extraseek
, tcs
.type
, buf
, buflen
))
1497 tagcache_search_finish(&tcs
);
1501 tagcache_search_finish(&tcs
);
1506 static bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1510 int from
, to
, direction
;
1511 int files_left
= c
->filesindir
;
1514 if (!tagcache_search(&tcs
, tag_filename
))
1516 gui_syncsplash(HZ
, str(LANG_TAGCACHE_BUSY
));
1521 if (position
== PLAYLIST_REPLACE
)
1523 if (remove_all_tracks(NULL
) == 0)
1524 position
= PLAYLIST_INSERT_LAST
;
1527 if (position
== PLAYLIST_INSERT_FIRST
)
1529 from
= c
->filesindir
- 1;
1540 for (i
= from
; i
!= to
; i
+= direction
)
1542 /* Count back to zero */
1543 if (!show_search_progress(false, files_left
--))
1546 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1547 tcs
.type
, buf
, sizeof buf
))
1552 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1554 logf("playlist_insert_track failed");
1559 playlist_sync(NULL
);
1560 tagcache_search_finish(&tcs
);
1566 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1568 struct tagentry
*dptr
;
1570 int dirlevel
= tc
->dirlevel
;
1572 /* We need to set the table to allsubentries. */
1573 show_search_progress(true, 0);
1575 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1577 /* Insert a single track? */
1578 if (dptr
->newtable
== playtrack
)
1580 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1582 logf("tagtree_get_filename failed");
1585 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1590 if (dptr
->newtable
== navibrowse
)
1594 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1596 else if (dptr
->newtable
!= allsubentries
)
1598 logf("unsupported table: %d", dptr
->newtable
);
1602 /* Now the current table should be allsubentries. */
1603 if (dptr
->newtable
!= playtrack
)
1607 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1609 /* And now the newtable should be playtrack. */
1610 if (dptr
->newtable
!= playtrack
)
1612 logf("newtable: %d !!", dptr
->newtable
);
1613 tc
->dirlevel
= dirlevel
;
1618 if (tc
->filesindir
<= 0)
1619 gui_syncsplash(HZ
, str(LANG_END_PLAYLIST_PLAYER
));
1622 logf("insert_all_playlist");
1623 if (!insert_all_playlist(tc
, position
, queue
))
1624 gui_syncsplash(HZ
*2, str(LANG_FAILED
));
1627 /* Finally return the dirlevel to its original value. */
1628 while (tc
->dirlevel
> dirlevel
)
1635 static int tagtree_play_folder(struct tree_context
* c
)
1637 if (playlist_create(NULL
, NULL
) < 0)
1639 logf("Failed creating playlist\n");
1643 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1646 if (global_settings
.playlist_shuffle
)
1647 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
1648 if (!global_settings
.play_selected
)
1649 c
->selected_item
= 0;
1650 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1652 playlist_start(c
->selected_item
,0);
1657 struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
1659 struct tagentry
*entry
= (struct tagentry
*)c
->dircache
;
1660 int realid
= id
- current_offset
;
1662 /* Load the next chunk if necessary. */
1663 if (realid
>= current_entry_count
|| realid
< 0)
1666 if (retrieve_entries(c
, &tcs2
, MAX(0, id
- (current_entry_count
/ 2)),
1669 logf("retrieve failed");
1673 realid
= id
- current_offset
;
1677 return &entry
[realid
];
1680 char *tagtree_get_title(struct tree_context
* c
)
1682 switch (c
->currtable
)
1689 return current_title
[c
->currextra
];
1695 int tagtree_get_attr(struct tree_context
* c
)
1698 switch (c
->currtable
)
1701 if (csi
->tagorder
[c
->currextra
] == tag_title
)
1702 attr
= FILE_ATTR_AUDIO
;
1704 attr
= ATTR_DIRECTORY
;
1708 attr
= FILE_ATTR_AUDIO
;
1712 attr
= ATTR_DIRECTORY
;
1719 int tagtree_get_icon(struct tree_context
* c
)
1721 int icon
= Icon_Folder
;
1723 if (tagtree_get_attr(c
) == FILE_ATTR_AUDIO
)