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
, "rating", tag_rating
);
209 MATCH(tag
, buf
, "lastplayed", tag_lastplayed
);
210 MATCH(tag
, buf
, "commitid", tag_commitid
);
211 MATCH(tag
, buf
, "entryage", tag_virt_entryage
);
212 MATCH(tag
, buf
, "autoscore", tag_virt_autoscore
);
213 MATCH(tag
, buf
, "%sort", var_sorttype
);
214 MATCH(tag
, buf
, "%limit", var_limit
);
215 MATCH(tag
, buf
, "%strip", var_strip
);
216 MATCH(tag
, buf
, "%menu_start", var_menu_start
);
217 MATCH(tag
, buf
, "%include", var_include
);
218 MATCH(tag
, buf
, "%root_menu", var_rootmenu
);
219 MATCH(tag
, buf
, "%format", var_format
);
220 MATCH(tag
, buf
, "->", menu_next
);
221 MATCH(tag
, buf
, "==>", menu_load
);
223 logf("NO MATCH: %s\n", buf
);
230 static int get_clause(int *condition
)
235 /* Find the start. */
236 while (*strp
== ' ' && *strp
!= '\0')
242 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
244 if (*strp
== '\0' || *strp
== ' ')
251 MATCH(condition
, buf
, "=", clause_is
);
252 MATCH(condition
, buf
, "==", clause_is
);
253 MATCH(condition
, buf
, "!=", clause_is_not
);
254 MATCH(condition
, buf
, ">", clause_gt
);
255 MATCH(condition
, buf
, ">=", clause_gteq
);
256 MATCH(condition
, buf
, "<", clause_lt
);
257 MATCH(condition
, buf
, "<=", clause_lteq
);
258 MATCH(condition
, buf
, "~", clause_contains
);
259 MATCH(condition
, buf
, "!~", clause_not_contains
);
260 MATCH(condition
, buf
, "^", clause_begins_with
);
261 MATCH(condition
, buf
, "!^", clause_not_begins_with
);
262 MATCH(condition
, buf
, "$", clause_ends_with
);
263 MATCH(condition
, buf
, "!$", clause_not_ends_with
);
264 MATCH(condition
, buf
, "@", clause_oneof
);
269 static bool read_clause(struct tagcache_search_clause
*clause
)
273 if (get_tag(&clause
->tag
) <= 0)
276 if (get_clause(&clause
->type
) <= 0)
279 if (get_token_str(buf
, sizeof buf
) < 0)
282 clause
->str
= buffer_alloc(strlen(buf
)+1);
283 strcpy(clause
->str
, buf
);
285 logf("got clause: %d/%d [%s]", clause
->tag
, clause
->type
, clause
->str
);
287 if (*(clause
->str
) == '\0')
288 clause
->input
= true;
290 clause
->input
= false;
292 if (tagcache_is_numeric_tag(clause
->tag
))
294 clause
->numeric
= true;
295 clause
->numeric_data
= atoi(clause
->str
);
298 clause
->numeric
= false;
303 static bool read_variable(char *buf
, int size
)
307 if (!get_clause(&condition
))
310 if (condition
!= clause_is
)
313 if (get_token_str(buf
, size
) < 0)
319 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
320 static int get_format_str(struct display_format
*fmt
)
326 memset(fmt
, 0, sizeof(struct display_format
));
328 if (get_token_str(fmt
->name
, sizeof fmt
->name
) < 0)
331 /* Determine the group id */
333 for (i
= 0; i
< format_count
; i
++)
335 if (!strcasecmp(formats
[i
]->name
, fmt
->name
))
337 fmt
->group_id
= formats
[i
]->group_id
;
341 if (formats
[i
]->group_id
> fmt
->group_id
)
342 fmt
->group_id
= formats
[i
]->group_id
;
345 if (i
== format_count
)
348 logf("format: (%d) %s", fmt
->group_id
, fmt
->name
);
350 if (get_token_str(buf
, sizeof buf
) < 0)
353 fmt
->formatstr
= buffer_alloc(strlen(buf
) + 1);
354 strcpy(fmt
->formatstr
, buf
);
356 while (fmt
->tag_count
< MAX_TAGS
)
358 ret
= get_tag(&fmt
->tags
[fmt
->tag_count
]);
365 switch (fmt
->tags
[fmt
->tag_count
]) {
367 if (!read_variable(buf
, sizeof buf
))
369 if (!strcasecmp("inverse", buf
))
370 fmt
->sort_inverse
= true;
376 if (!read_variable(buf
, sizeof buf
))
378 fmt
->limit
= atoi(buf
);
382 if (!read_variable(buf
, sizeof buf
))
384 fmt
->strip
= atoi(buf
);
395 static int add_format(const char *buf
)
399 if (formats
[format_count
] == NULL
)
400 formats
[format_count
] = buffer_alloc(sizeof(struct display_format
));
402 memset(formats
[format_count
], 0, sizeof(struct display_format
));
403 if (get_format_str(formats
[format_count
]) < 0)
405 logf("get_format_str() parser failed!");
409 while (*strp
!= '\0' && *strp
!= '?')
414 int clause_count
= 0;
419 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
421 logf("too many clauses");
425 formats
[format_count
]->clause
[clause_count
] =
426 buffer_alloc(sizeof(struct tagcache_search_clause
));
428 if (!read_clause(formats
[format_count
]->clause
[clause_count
]))
434 formats
[format_count
]->clause_count
= clause_count
;
442 static int get_condition(struct search_instruction
*inst
)
453 if (get_token_str(buf
, sizeof buf
) < 0)
456 for (i
= 0; i
< format_count
; i
++)
458 if (!strcasecmp(formats
[i
]->name
, buf
))
462 if (i
== format_count
)
464 logf("format not found: %s", buf
);
468 inst
->format_id
[inst
->tagorder_count
] = formats
[i
]->group_id
;
481 clause_count
= inst
->clause_count
[inst
->tagorder_count
];
482 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
484 logf("Too many clauses");
488 inst
->clause
[inst
->tagorder_count
][clause_count
] =
489 buffer_alloc(sizeof(struct tagcache_search_clause
));
491 if (!read_clause(inst
->clause
[inst
->tagorder_count
][clause_count
]))
494 inst
->clause_count
[inst
->tagorder_count
]++;
500 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
501 * : album ? year >= "2000" : songs
507 static bool parse_search(struct menu_entry
*entry
, const char *str
)
511 struct search_instruction
*inst
= entry
->si
;
517 /* Parse entry name */
518 if (get_token_str(entry
->name
, sizeof entry
->name
) < 0)
520 logf("No name found.");
524 /* Parse entry type */
525 if (get_tag(&entry
->type
) <= 0)
528 if (entry
->type
== menu_load
)
530 if (get_token_str(buf
, sizeof buf
) < 0)
533 /* Find the matching root menu or "create" it */
534 for (i
= 0; i
< menu_count
; i
++)
536 if (!strcasecmp(menus
[i
]->id
, buf
))
539 menus
[i
]->parent
= menu
;
547 if (entry
->type
!= menu_next
)
550 while (inst
->tagorder_count
< MAX_TAGS
)
552 ret
= get_tag(&inst
->tagorder
[inst
->tagorder_count
]);
555 logf("Parse error #1");
563 logf("tag: %d", inst
->tagorder
[inst
->tagorder_count
]);
565 while ( (ret
= get_condition(inst
)) > 0 ) ;
569 inst
->tagorder_count
++;
571 if (get_tag(&type
) <= 0 || type
!= menu_next
)
578 static int compare(const void *p1
, const void *p2
)
580 struct tagentry
*e1
= (struct tagentry
*)p1
;
581 struct tagentry
*e2
= (struct tagentry
*)p2
;
584 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
586 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
589 static void tagtree_buffer_event(struct mp3entry
*id3
, bool last_track
)
594 /* Do not gather data unless proper setting has been enabled. */
595 if (!global_settings
.runtimedb
)
598 logf("be:%d%s", last_track
, id3
->path
);
600 if (!tagcache_find_index(&tcs
, id3
->path
))
602 logf("tc stat: not found: %s", id3
->path
);
606 id3
->playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
607 if(!id3
->rating
) id3
->rating
= tagcache_get_numeric(&tcs
, tag_rating
);
608 id3
->lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
609 id3
->score
= tagcache_get_numeric(&tcs
, tag_virt_autoscore
) / 10;
611 tagcache_search_finish(&tcs
);
614 static void tagtree_unbuffer_event(struct mp3entry
*id3
, bool last_track
)
622 /* Do not gather data unless proper setting has been enabled. */
623 if (!global_settings
.runtimedb
)
625 logf("runtimedb gathering not enabled");
629 /* Don't process unplayed tracks. */
630 if (id3
->elapsed
== 0)
632 logf("not logging unplayed track");
636 if (!tagcache_find_index(&tcs
, id3
->path
))
638 logf("tc stat: not found: %s", id3
->path
);
642 playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
643 playtime
= tagcache_get_numeric(&tcs
, tag_playtime
);
644 lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
648 rating
= (long) id3
->rating
;
650 lastplayed
= tagcache_increase_serial();
653 logf("incorrect tc serial:%ld", lastplayed
);
654 tagcache_search_finish(&tcs
);
658 /* Ignore the last 15s (crossfade etc.) */
659 playtime
+= MIN(id3
->length
, id3
->elapsed
+ 15 * 1000);
661 logf("ube:%s", id3
->path
);
662 logf("-> %d/%ld/%ld/%ld", last_track
, playcount
, rating
, playtime
);
663 logf("-> %ld/%ld/%ld", id3
->elapsed
, id3
->length
, MIN(id3
->length
, id3
->elapsed
+ 15 * 1000));
665 /* lastplayed not yet supported. */
667 if (!tagcache_modify_numeric_entry(&tcs
, tag_playcount
, playcount
)
668 || !tagcache_modify_numeric_entry(&tcs
, tag_rating
, rating
)
669 || !tagcache_modify_numeric_entry(&tcs
, tag_playtime
, playtime
)
670 || !tagcache_modify_numeric_entry(&tcs
, tag_lastplayed
, lastplayed
))
672 logf("tc stat: modify failed!");
673 tagcache_search_finish(&tcs
);
677 tagcache_search_finish(&tcs
);
680 bool tagtree_export(void)
682 gui_syncsplash(0, str(LANG_CREATING
));
683 if (!tagcache_create_changelog(&tcs
))
685 gui_syncsplash(HZ
*2, str(LANG_FAILED
));
691 bool tagtree_import(void)
693 gui_syncsplash(0, str(LANG_WAIT
));
694 if (!tagcache_import_changelog())
696 gui_syncsplash(HZ
*2, str(LANG_FAILED
));
702 static bool parse_menu(const char *filename
);
704 static int parse_line(int n
, const char *buf
, void *parameters
)
708 static bool read_menu
;
713 logf("parse:%d/%s", n
, buf
);
715 /* First line, do initialisation. */
718 if (strcasecmp(TAGNAVI_VERSION
, buf
))
720 logf("Version mismatch");
744 if (get_tag(&variable
) <= 0)
750 if (add_format(strp
) < 0)
752 logf("Format add fail: %s", data
);
757 if (get_token_str(data
, sizeof(data
)) < 0)
759 logf("%%include empty");
763 if (!parse_menu(data
))
765 logf("Load menu fail: %s", data
);
770 if (menu_count
>= TAGMENU_MAX_MENUS
)
772 logf("max menucount reached");
776 menus
[menu_count
] = buffer_alloc(sizeof(struct root_menu
));
777 menu
= menus
[menu_count
];
778 memset(menu
, 0, sizeof(struct root_menu
));
779 if (get_token_str(menu
->id
, sizeof(menu
->id
)) < 0)
781 logf("%%menu_start id empty");
784 if (get_token_str(menu
->title
, sizeof(menu
->title
)) < 0)
786 logf("%%menu_start title empty");
789 logf("menu: %s", menu
->title
);
795 /* Only set root menu once. */
799 if (get_token_str(data
, sizeof(data
)) < 0)
801 logf("%%root_menu empty");
805 for (i
= 0; i
< menu_count
; i
++)
807 if (!strcasecmp(menus
[i
]->id
, data
))
818 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
820 logf("max itemcount reached");
825 if (menu
->items
[menu
->itemcount
] == NULL
)
827 menu
->items
[menu
->itemcount
] = buffer_alloc(sizeof(struct menu_entry
));
828 memset(menu
->items
[menu
->itemcount
], 0, sizeof(struct menu_entry
));
829 menu
->items
[menu
->itemcount
]->si
= buffer_alloc(sizeof(struct search_instruction
));
832 memset(menu
->items
[menu
->itemcount
]->si
, 0, sizeof(struct search_instruction
));
833 if (!parse_search(menu
->items
[menu
->itemcount
], buf
))
841 static bool parse_menu(const char *filename
)
846 if (menu_count
>= TAGMENU_MAX_MENUS
)
848 logf("max menucount reached");
852 fd
= open(filename
, O_RDONLY
);
855 logf("Search instruction file not found.");
859 /* Now read file for real, parsing into si */
860 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
866 void tagtree_init(void)
872 parse_menu(FILE_SEARCH_INSTRUCTIONS
);
874 uniqbuf
= buffer_alloc(UNIQBUF_SIZE
);
875 audio_set_track_buffer_event(tagtree_buffer_event
);
876 audio_set_track_unbuffer_event(tagtree_unbuffer_event
);
879 static bool show_search_progress(bool init
, int count
)
881 static int last_tick
= 0;
885 last_tick
= current_tick
;
889 if (current_tick
- last_tick
> HZ
/4)
891 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG
), count
,
892 #if CONFIG_KEYPAD == PLAYER_PAD
898 if (action_userabort(TIMEOUT_NOBLOCK
))
900 last_tick
= current_tick
;
907 static int format_str(struct tagcache_search
*tcs
, struct display_format
*fmt
,
908 char *buf
, int buf_size
)
911 bool read_format
= false;
917 memset(buf
, 0, buf_size
);
918 for (i
= 0; fmt
->formatstr
[i
] != '\0'; i
++)
920 if (fmt
->formatstr
[i
] == '%')
924 if (parpos
>= fmt
->tag_count
)
926 logf("too many format tags");
933 fmtbuf
[fmtbuf_pos
++] = fmt
->formatstr
[i
];
934 if (fmtbuf_pos
>= buf_size
)
936 logf("format parse error");
940 if (fmt
->formatstr
[i
] == 's')
942 fmtbuf
[fmtbuf_pos
] = '\0';
944 if (fmt
->tags
[parpos
] == tcs
->type
)
946 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
, tcs
->result
);
950 /* Need to fetch the tag data. */
951 if (!tagcache_retrieve(tcs
, tcs
->idx_id
, fmt
->tags
[parpos
],
952 &buf
[buf_pos
], buf_size
- buf_pos
))
954 logf("retrieve failed");
958 buf_pos
+= strlen(&buf
[buf_pos
]);
961 else if (fmt
->formatstr
[i
] == 'd')
963 fmtbuf
[fmtbuf_pos
] = '\0';
965 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
,
966 tagcache_get_numeric(tcs
, fmt
->tags
[parpos
]));
967 buf_pos
+= strlen(&buf
[buf_pos
]);
973 buf
[buf_pos
++] = fmt
->formatstr
[i
];
975 if (buf_pos
- 1 >= buf_size
)
977 logf("buffer overflow");
982 buf
[buf_pos
++] = '\0';
987 static int retrieve_entries(struct tree_context
*c
, struct tagcache_search
*tcs
,
988 int offset
, bool init
)
990 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
991 struct display_format
*fmt
;
995 int special_entry_count
= 0;
996 int level
= c
->currextra
;
1003 #ifdef HAVE_TC_RAMCACHE
1004 && !tagcache_is_ramcache()
1008 show_search_progress(true, 0);
1009 gui_syncsplash(0, str(LANG_PLAYLIST_SEARCH_MSG
),
1013 if (c
->currtable
== allsubentries
)
1019 tag
= csi
->tagorder
[level
];
1021 if (!tagcache_search(tcs
, tag
))
1024 /* Prevent duplicate entries in the search list. */
1025 tagcache_search_set_uniqbuf(tcs
, uniqbuf
, UNIQBUF_SIZE
);
1027 if (level
|| csi
->clause_count
[0] || tagcache_is_numeric_tag(tag
))
1030 for (i
= 0; i
< level
; i
++)
1032 if (tagcache_is_numeric_tag(csi
->tagorder
[i
]))
1034 static struct tagcache_search_clause cc
;
1036 memset(&cc
, 0, sizeof(struct tagcache_search_clause
));
1037 cc
.tag
= csi
->tagorder
[i
];
1038 cc
.type
= clause_is
;
1040 cc
.numeric_data
= csi
->result_seek
[i
];
1041 tagcache_search_add_clause(tcs
, &cc
);
1045 tagcache_search_add_filter(tcs
, csi
->tagorder
[i
],
1046 csi
->result_seek
[i
]);
1050 for (i
= 0; i
<= level
; i
++)
1054 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1055 tagcache_search_add_clause(tcs
, csi
->clause
[i
][j
]);
1058 current_offset
= offset
;
1059 current_entry_count
= 0;
1063 for (i
= 0; i
< format_count
; i
++)
1065 if (formats
[i
]->group_id
== csi
->format_id
[level
])
1071 sort_inverse
= fmt
->sort_inverse
;
1072 sort_limit
= fmt
->limit
;
1075 /* Check if sorting is forced. */
1081 sort_inverse
= false;
1086 if (tag
!= tag_title
&& tag
!= tag_filename
)
1090 dptr
->newtable
= allsubentries
;
1091 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1093 current_entry_count
++;
1095 special_entry_count
++;
1098 total_count
+= special_entry_count
;
1100 while (tagcache_get_next(tcs
))
1102 if (total_count
++ < offset
)
1105 dptr
->newtable
= navibrowse
;
1106 if (tag
== tag_title
|| tag
== tag_filename
)
1108 dptr
->newtable
= playtrack
;
1109 dptr
->extraseek
= tcs
->idx_id
;
1112 dptr
->extraseek
= tcs
->result_seek
;
1115 /* Check the format */
1116 for (i
= 0; i
< format_count
; i
++)
1118 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1121 if (tagcache_check_clauses(tcs
, formats
[i
]->clause
,
1122 formats
[i
]->clause_count
))
1129 if (!tcs
->ramresult
|| fmt
)
1135 if (format_str(tcs
, fmt
, buf
, sizeof buf
) < 0)
1137 logf("format_str() failed");
1142 dptr
->name
= &c
->name_buffer
[namebufused
];
1144 namebufused
+= strlen(buf
)+1;
1146 namebufused
+= tcs
->result_len
;
1148 if (namebufused
>= c
->name_buffer_size
)
1150 logf("chunk mode #2: %d", current_entry_count
);
1156 strcpy(dptr
->name
, buf
);
1158 strcpy(dptr
->name
, tcs
->result
);
1161 dptr
->name
= tcs
->result
;
1164 current_entry_count
++;
1166 if (current_entry_count
>= global_settings
.max_files_in_dir
)
1168 logf("chunk mode #3: %d", current_entry_count
);
1174 if (init
&& !tcs
->ramsearch
)
1176 if (!show_search_progress(false, i
))
1178 tagcache_search_finish(tcs
);
1179 return current_entry_count
;
1185 qsort(c
->dircache
+ special_entry_count
* c
->dentry_size
,
1186 current_entry_count
- special_entry_count
,
1187 c
->dentry_size
, compare
);
1191 tagcache_search_finish(tcs
);
1192 return current_entry_count
;
1195 while (tagcache_get_next(tcs
))
1197 if (!tcs
->ramsearch
)
1199 if (!show_search_progress(false, total_count
))
1205 tagcache_search_finish(tcs
);
1207 if (!sort
&& (sort_inverse
|| sort_limit
))
1209 gui_syncsplash(HZ
*4, str(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1210 logf("Too small dir buffer");
1215 total_count
= MIN(total_count
, sort_limit
);
1220 for (i
= 0; i
< total_count
; i
++, dptr
++)
1222 int len
= strlen(dptr
->name
);
1227 dptr
->name
= &dptr
->name
[strip
];
1234 static int load_root(struct tree_context
*c
)
1236 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1240 c
->currtable
= root
;
1241 if (c
->dirlevel
== 0)
1242 c
->currextra
= root_menu
;
1244 menu
= menus
[c
->currextra
];
1248 for (i
= 0; i
< menu
->itemcount
; i
++)
1250 dptr
->name
= menu
->items
[i
]->name
;
1251 switch (menu
->items
[i
]->type
)
1254 dptr
->newtable
= navibrowse
;
1255 dptr
->extraseek
= i
;
1259 dptr
->newtable
= root
;
1260 dptr
->extraseek
= menu
->items
[i
]->link
;
1268 current_entry_count
= i
;
1273 int tagtree_load(struct tree_context
* c
)
1276 int table
= c
->currtable
;
1278 c
->dentry_size
= sizeof(struct tagentry
);
1285 c
->currtable
= table
;
1286 c
->currextra
= root_menu
;
1292 count
= load_root(c
);
1297 logf("navibrowse...");
1299 count
= retrieve_entries(c
, &tcs
, 0, true);
1304 logf("Unsupported table %d\n", table
);
1311 count
= load_root(c
);
1312 gui_syncsplash(HZ
, str(LANG_TAGCACHE_BUSY
));
1315 /* The _total_ numer of entries available. */
1316 c
->dirlength
= c
->filesindir
= count
;
1321 int tagtree_enter(struct tree_context
* c
)
1324 struct tagentry
*dptr
;
1328 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1331 newextra
= dptr
->newtable
;
1332 seek
= dptr
->extraseek
;
1334 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1337 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1338 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1339 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1340 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1343 switch (c
->currtable
) {
1345 c
->currextra
= newextra
;
1347 if (newextra
== root
)
1350 c
->currextra
= seek
;
1353 else if (newextra
== navibrowse
)
1357 csi
= menu
->items
[seek
]->si
;
1360 strncpy(current_title
[c
->currextra
], dptr
->name
,
1361 sizeof(current_title
[0]) - 1);
1363 /* Read input as necessary. */
1364 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1366 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1368 if (!csi
->clause
[i
][j
]->input
)
1371 rc
= kbd_input(searchstring
, sizeof(searchstring
));
1372 if (rc
== -1 || !searchstring
[0])
1378 if (csi
->clause
[i
][j
]->numeric
)
1379 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1381 csi
->clause
[i
][j
]->str
= searchstring
;
1385 c
->currtable
= newextra
;
1391 if (newextra
== playtrack
)
1394 /* about to create a new current playlist...
1395 allow user to cancel the operation */
1396 if (global_settings
.warnon_erase_dynplaylist
&&
1397 !global_settings
.party_mode
&&
1398 playlist_modified(NULL
))
1400 char *lines
[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
1401 struct text_message message
={lines
, 1};
1403 if (gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
1407 if (tagtree_play_folder(c
) >= 0)
1412 c
->currtable
= newextra
;
1413 csi
->result_seek
[c
->currextra
] = seek
;
1414 if (c
->currextra
< csi
->tagorder_count
-1)
1419 /* Update the statusbar title */
1420 strncpy(current_title
[c
->currextra
], dptr
->name
,
1421 sizeof(current_title
[0]) - 1);
1430 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1435 void tagtree_exit(struct tree_context
* c
)
1438 if (c
->dirlevel
> 0)
1440 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1441 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1442 c
->currtable
= c
->table_history
[c
->dirlevel
];
1443 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1444 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1447 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1449 struct tagentry
*entry
;
1451 entry
= tagtree_get_entry(c
, c
->selected_item
);
1453 if (!tagcache_search(&tcs
, tag_filename
))
1456 if (!tagcache_retrieve(&tcs
, entry
->extraseek
, tcs
.type
, buf
, buflen
))
1458 tagcache_search_finish(&tcs
);
1462 tagcache_search_finish(&tcs
);
1467 static bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1471 int from
, to
, direction
;
1472 int files_left
= c
->filesindir
;
1475 if (!tagcache_search(&tcs
, tag_filename
))
1477 gui_syncsplash(HZ
, str(LANG_TAGCACHE_BUSY
));
1482 if (position
== PLAYLIST_REPLACE
)
1484 if (remove_all_tracks(NULL
) == 0)
1485 position
= PLAYLIST_INSERT_LAST
;
1488 if (position
== PLAYLIST_INSERT_FIRST
)
1490 from
= c
->filesindir
- 1;
1501 for (i
= from
; i
!= to
; i
+= direction
)
1503 /* Count back to zero */
1504 if (!show_search_progress(false, files_left
--))
1507 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1508 tcs
.type
, buf
, sizeof buf
))
1513 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1515 logf("playlist_insert_track failed");
1520 playlist_sync(NULL
);
1521 tagcache_search_finish(&tcs
);
1527 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1529 struct tagentry
*dptr
;
1531 int dirlevel
= tc
->dirlevel
;
1533 /* We need to set the table to allsubentries. */
1534 show_search_progress(true, 0);
1536 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1538 /* Insert a single track? */
1539 if (dptr
->newtable
== playtrack
)
1541 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1543 logf("tagtree_get_filename failed");
1546 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1551 if (dptr
->newtable
== navibrowse
)
1555 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1557 else if (dptr
->newtable
!= allsubentries
)
1559 logf("unsupported table: %d", dptr
->newtable
);
1563 /* Now the current table should be allsubentries. */
1564 if (dptr
->newtable
!= playtrack
)
1568 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1570 /* And now the newtable should be playtrack. */
1571 if (dptr
->newtable
!= playtrack
)
1573 logf("newtable: %d !!", dptr
->newtable
);
1574 tc
->dirlevel
= dirlevel
;
1579 if (tc
->filesindir
<= 0)
1580 gui_syncsplash(HZ
, str(LANG_END_PLAYLIST_PLAYER
));
1583 logf("insert_all_playlist");
1584 if (!insert_all_playlist(tc
, position
, queue
))
1585 gui_syncsplash(HZ
*2, str(LANG_FAILED
));
1588 /* Finally return the dirlevel to its original value. */
1589 while (tc
->dirlevel
> dirlevel
)
1596 static int tagtree_play_folder(struct tree_context
* c
)
1598 if (playlist_create(NULL
, NULL
) < 0)
1600 logf("Failed creating playlist\n");
1604 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1607 if (global_settings
.playlist_shuffle
)
1608 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
1609 if (!global_settings
.play_selected
)
1610 c
->selected_item
= 0;
1611 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1613 playlist_start(c
->selected_item
,0);
1618 struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
1620 struct tagentry
*entry
= (struct tagentry
*)c
->dircache
;
1621 int realid
= id
- current_offset
;
1623 /* Load the next chunk if necessary. */
1624 if (realid
>= current_entry_count
|| realid
< 0)
1627 if (retrieve_entries(c
, &tcs2
, MAX(0, id
- (current_entry_count
/ 2)),
1630 logf("retrieve failed");
1634 realid
= id
- current_offset
;
1638 return &entry
[realid
];
1641 char *tagtree_get_title(struct tree_context
* c
)
1643 switch (c
->currtable
)
1650 return current_title
[c
->currextra
];
1656 int tagtree_get_attr(struct tree_context
* c
)
1659 switch (c
->currtable
)
1662 if (csi
->tagorder
[c
->currextra
] == tag_title
)
1663 attr
= TREE_ATTR_MPA
;
1665 attr
= ATTR_DIRECTORY
;
1669 attr
= TREE_ATTR_MPA
;
1673 attr
= ATTR_DIRECTORY
;
1680 #ifdef HAVE_LCD_BITMAP
1681 const unsigned char* tagtree_get_icon(struct tree_context
* c
)
1683 int tagtree_get_icon(struct tree_context
* c
)
1686 int icon
= Icon_Folder
;
1688 if (tagtree_get_attr(c
) == TREE_ATTR_MPA
)
1691 #ifdef HAVE_LCD_BITMAP
1692 return bitmap_icons_6x8
[icon
];