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
];
100 static struct display_format
*formats
[TAGMENU_MAX_FMTS
];
101 static int format_count
;
103 struct search_instruction
{
105 int tagorder
[MAX_TAGS
];
107 struct tagcache_search_clause
*clause
[MAX_TAGS
][TAGCACHE_MAX_CLAUSES
];
108 int format_id
[MAX_TAGS
];
109 int clause_count
[MAX_TAGS
];
110 int result_seek
[MAX_TAGS
];
116 struct search_instruction
*si
;
124 struct root_menu
*parent
;
125 struct menu_entry
*items
[TAGMENU_MAX_ITEMS
];
128 /* Statusbar text of the current view. */
129 static char current_title
[MAX_TAGS
][128];
131 static struct root_menu
*menus
[TAGMENU_MAX_MENUS
];
132 static struct root_menu
*menu
;
133 static struct search_instruction
*csi
;
134 static const char *strp
;
135 static int menu_count
;
136 static int root_menu
;
138 static int current_offset
;
139 static int current_entry_count
;
141 static int format_count
;
142 static struct tree_context
*tc
;
144 static int get_token_str(char *buf
, int size
)
146 /* Find the start. */
147 while (*strp
!= '"' && *strp
!= '\0')
150 if (*strp
== '\0' || *(++strp
) == '\0')
154 while (*strp
!= '"' && *strp
!= '\0' && --size
> 0)
155 *(buf
++) = *(strp
++);
166 #define MATCH(tag,str1,str2,settag) \
167 if (!strcasecmp(str1, str2)) { \
172 static int get_tag(int *tag
)
177 /* Find the start. */
178 while ((*strp
== ' ' || *strp
== '>') && *strp
!= '\0')
181 if (*strp
== '\0' || *strp
== '?')
184 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
186 if (*strp
== '\0' || *strp
== ' ')
193 MATCH(tag
, buf
, "album", tag_album
);
194 MATCH(tag
, buf
, "artist", tag_artist
);
195 MATCH(tag
, buf
, "bitrate", tag_bitrate
);
196 MATCH(tag
, buf
, "composer", tag_composer
);
197 MATCH(tag
, buf
, "genre", tag_genre
);
198 MATCH(tag
, buf
, "length", tag_length
);
199 MATCH(tag
, buf
, "title", tag_title
);
200 MATCH(tag
, buf
, "filename", tag_filename
);
201 MATCH(tag
, buf
, "tracknum", tag_tracknumber
);
202 MATCH(tag
, buf
, "year", tag_year
);
203 MATCH(tag
, buf
, "playcount", tag_playcount
);
204 MATCH(tag
, buf
, "lastplayed", tag_lastplayed
);
205 MATCH(tag
, buf
, "autoscore", tag_virt_autoscore
);
206 MATCH(tag
, buf
, "%sort", var_sorttype
);
207 MATCH(tag
, buf
, "%limit", var_limit
);
208 MATCH(tag
, buf
, "%strip", var_strip
);
209 MATCH(tag
, buf
, "%menu_start", var_menu_start
);
210 MATCH(tag
, buf
, "%include", var_include
);
211 MATCH(tag
, buf
, "%root_menu", var_rootmenu
);
212 MATCH(tag
, buf
, "%format", var_format
);
213 MATCH(tag
, buf
, "->", menu_next
);
214 MATCH(tag
, buf
, "==>", menu_load
);
216 logf("NO MATCH: %s\n", buf
);
223 static int get_clause(int *condition
)
228 /* Find the start. */
229 while (*strp
== ' ' && *strp
!= '\0')
235 for (i
= 0; i
< (int)sizeof(buf
)-1; i
++)
237 if (*strp
== '\0' || *strp
== ' ')
244 MATCH(condition
, buf
, "=", clause_is
);
245 MATCH(condition
, buf
, "==", clause_is
);
246 MATCH(condition
, buf
, "!=", clause_is_not
);
247 MATCH(condition
, buf
, ">", clause_gt
);
248 MATCH(condition
, buf
, ">=", clause_gteq
);
249 MATCH(condition
, buf
, "<", clause_lt
);
250 MATCH(condition
, buf
, "<=", clause_lteq
);
251 MATCH(condition
, buf
, "~", clause_contains
);
252 MATCH(condition
, buf
, "!~", clause_not_contains
);
253 MATCH(condition
, buf
, "^", clause_begins_with
);
254 MATCH(condition
, buf
, "!^", clause_not_begins_with
);
255 MATCH(condition
, buf
, "$", clause_ends_with
);
256 MATCH(condition
, buf
, "!$", clause_not_ends_with
);
257 MATCH(condition
, buf
, "@", clause_oneof
);
262 static bool read_clause(struct tagcache_search_clause
*clause
)
266 if (get_tag(&clause
->tag
) <= 0)
269 if (get_clause(&clause
->type
) <= 0)
272 if (get_token_str(buf
, sizeof buf
) < 0)
275 clause
->str
= buffer_alloc(strlen(buf
)+1);
276 strcpy(clause
->str
, buf
);
278 logf("got clause: %d/%d [%s]", clause
->tag
, clause
->type
, clause
->str
);
280 if (*(clause
->str
) == '\0')
281 clause
->input
= true;
283 clause
->input
= false;
285 if (tagcache_is_numeric_tag(clause
->tag
))
287 clause
->numeric
= true;
288 clause
->numeric_data
= atoi(clause
->str
);
291 clause
->numeric
= false;
296 static bool read_variable(char *buf
, int size
)
300 if (!get_clause(&condition
))
303 if (condition
!= clause_is
)
306 if (get_token_str(buf
, size
) < 0)
312 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
313 static int get_format_str(struct display_format
*fmt
)
319 memset(fmt
, 0, sizeof(struct display_format
));
321 if (get_token_str(fmt
->name
, sizeof fmt
->name
) < 0)
324 /* Determine the group id */
326 for (i
= 0; i
< format_count
; i
++)
328 if (!strcasecmp(formats
[i
]->name
, fmt
->name
))
330 fmt
->group_id
= formats
[i
]->group_id
;
334 if (formats
[i
]->group_id
> fmt
->group_id
)
335 fmt
->group_id
= formats
[i
]->group_id
;
338 if (i
== format_count
)
341 logf("format: (%d) %s", fmt
->group_id
, fmt
->name
);
343 if (get_token_str(buf
, sizeof buf
) < 0)
346 fmt
->formatstr
= buffer_alloc(strlen(buf
) + 1);
347 strcpy(fmt
->formatstr
, buf
);
349 while (fmt
->tag_count
< MAX_TAGS
)
351 ret
= get_tag(&fmt
->tags
[fmt
->tag_count
]);
358 switch (fmt
->tags
[fmt
->tag_count
]) {
360 if (!read_variable(buf
, sizeof buf
))
362 if (!strcasecmp("inverse", buf
))
363 fmt
->sort_inverse
= true;
367 if (!read_variable(buf
, sizeof buf
))
369 fmt
->limit
= atoi(buf
);
373 if (!read_variable(buf
, sizeof buf
))
375 fmt
->strip
= atoi(buf
);
386 static int add_format(const char *buf
)
390 if (formats
[format_count
] == NULL
)
391 formats
[format_count
] = buffer_alloc(sizeof(struct display_format
));
393 memset(formats
[format_count
], 0, sizeof(struct display_format
));
394 if (get_format_str(formats
[format_count
]) < 0)
396 logf("get_format_str() parser failed!");
400 while (*strp
!= '\0' && *strp
!= '?')
405 int clause_count
= 0;
410 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
412 logf("too many clauses");
416 formats
[format_count
]->clause
[clause_count
] =
417 buffer_alloc(sizeof(struct tagcache_search_clause
));
419 if (!read_clause(formats
[format_count
]->clause
[clause_count
]))
425 formats
[format_count
]->clause_count
= clause_count
;
433 static int get_condition(struct search_instruction
*inst
)
444 if (get_token_str(buf
, sizeof buf
) < 0)
447 for (i
= 0; i
< format_count
; i
++)
449 if (!strcasecmp(formats
[i
]->name
, buf
))
453 if (i
== format_count
)
455 logf("format not found: %s", buf
);
459 inst
->format_id
[inst
->tagorder_count
] = formats
[i
]->group_id
;
472 clause_count
= inst
->clause_count
[inst
->tagorder_count
];
473 if (clause_count
>= TAGCACHE_MAX_CLAUSES
)
475 logf("Too many clauses");
479 inst
->clause
[inst
->tagorder_count
][clause_count
] =
480 buffer_alloc(sizeof(struct tagcache_search_clause
));
482 if (!read_clause(inst
->clause
[inst
->tagorder_count
][clause_count
]))
485 inst
->clause_count
[inst
->tagorder_count
]++;
491 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
492 * : album ? year >= "2000" : songs
498 static bool parse_search(struct menu_entry
*entry
, const char *str
)
502 struct search_instruction
*inst
= entry
->si
;
508 /* Parse entry name */
509 if (get_token_str(entry
->name
, sizeof entry
->name
) < 0)
511 logf("No name found.");
515 /* Parse entry type */
516 if (get_tag(&entry
->type
) <= 0)
519 if (entry
->type
== menu_load
)
521 if (get_token_str(buf
, sizeof buf
) < 0)
524 /* Find the matching root menu or "create" it */
525 for (i
= 0; i
< menu_count
; i
++)
527 if (!strcasecmp(menus
[i
]->id
, buf
))
530 menus
[i
]->parent
= menu
;
538 if (entry
->type
!= menu_next
)
541 while (inst
->tagorder_count
< MAX_TAGS
)
543 ret
= get_tag(&inst
->tagorder
[inst
->tagorder_count
]);
546 logf("Parse error #1");
554 logf("tag: %d", inst
->tagorder
[inst
->tagorder_count
]);
556 while ( (ret
= get_condition(inst
)) > 0 ) ;
560 inst
->tagorder_count
++;
562 if (get_tag(&type
) <= 0 || type
!= menu_next
)
569 static int compare(const void *p1
, const void *p2
)
571 struct tagentry
*e1
= (struct tagentry
*)p1
;
572 struct tagentry
*e2
= (struct tagentry
*)p2
;
575 return strncasecmp(e2
->name
, e1
->name
, MAX_PATH
);
577 return strncasecmp(e1
->name
, e2
->name
, MAX_PATH
);
580 static void tagtree_buffer_event(struct mp3entry
*id3
, bool last_track
)
585 /* Do not gather data unless proper setting has been enabled. */
586 if (!global_settings
.runtimedb
)
589 logf("be:%d%s", last_track
, id3
->path
);
591 if (!tagcache_find_index(&tcs
, id3
->path
))
593 logf("tc stat: not found: %s", id3
->path
);
597 id3
->playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
598 id3
->lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
599 id3
->rating
= tagcache_get_numeric(&tcs
, tag_virt_autoscore
) / 10;
601 tagcache_search_finish(&tcs
);
604 static void tagtree_unbuffer_event(struct mp3entry
*id3
, bool last_track
)
611 /* Do not gather data unless proper setting has been enabled. */
612 if (!global_settings
.runtimedb
)
614 logf("runtimedb gathering not enabled");
618 /* Don't process unplayed tracks. */
619 if (id3
->elapsed
== 0)
621 logf("not logging unplayed track");
625 if (!tagcache_find_index(&tcs
, id3
->path
))
627 logf("tc stat: not found: %s", id3
->path
);
631 playcount
= tagcache_get_numeric(&tcs
, tag_playcount
);
632 playtime
= tagcache_get_numeric(&tcs
, tag_playtime
);
633 lastplayed
= tagcache_get_numeric(&tcs
, tag_lastplayed
);
637 lastplayed
= tagcache_increase_serial();
640 logf("incorrect tc serial:%d", lastplayed
);
641 tagcache_search_finish(&tcs
);
645 /* Ignore the last 15s (crossfade etc.) */
646 playtime
+= MIN(id3
->length
, id3
->elapsed
+ 15 * 1000);
648 logf("ube:%s", id3
->path
);
649 logf("-> %d/%d/%d", last_track
, playcount
, playtime
);
650 logf("-> %d/%d/%d", id3
->elapsed
, id3
->length
, MIN(id3
->length
, id3
->elapsed
+ 15 * 1000));
652 /* lastplayed not yet supported. */
654 if (!tagcache_modify_numeric_entry(&tcs
, tag_playcount
, playcount
)
655 || !tagcache_modify_numeric_entry(&tcs
, tag_playtime
, playtime
)
656 || !tagcache_modify_numeric_entry(&tcs
, tag_lastplayed
, lastplayed
))
658 logf("tc stat: modify failed!");
659 tagcache_search_finish(&tcs
);
663 tagcache_search_finish(&tcs
);
666 bool tagtree_export(void)
668 gui_syncsplash(0, true, str(LANG_CREATING
));
669 if (!tagcache_create_changelog(&tcs
))
671 gui_syncsplash(HZ
*2, true, str(LANG_FAILED
));
677 bool tagtree_import(void)
679 gui_syncsplash(0, true, str(LANG_WAIT
));
680 if (!tagcache_import_changelog())
682 gui_syncsplash(HZ
*2, true, str(LANG_FAILED
));
688 static bool parse_menu(const char *filename
);
690 int parse_line(int n
, const char *buf
, void *parameters
)
694 static bool read_menu
;
699 logf("parse:%d/%s", n
, buf
);
701 /* First line, do initialisation. */
704 if (strcasecmp(TAGNAVI_VERSION
, buf
))
706 logf("Version mismatch");
730 if (get_tag(&variable
) <= 0)
736 if (add_format(strp
) < 0)
738 logf("Format add fail: %s", data
);
743 if (get_token_str(data
, sizeof(data
)) < 0)
745 logf("%include empty");
749 if (!parse_menu(data
))
751 logf("Load menu fail: %s", data
);
756 if (menu_count
>= TAGMENU_MAX_MENUS
)
758 logf("max menucount reached");
762 menus
[menu_count
] = buffer_alloc(sizeof(struct root_menu
));
763 menu
= menus
[menu_count
];
764 memset(menu
, 0, sizeof(struct root_menu
));
765 if (get_token_str(menu
->id
, sizeof(menu
->id
)) < 0)
767 logf("%menu_start id empty");
770 if (get_token_str(menu
->title
, sizeof(menu
->title
)) < 0)
772 logf("%menu_start title empty");
775 logf("menu: %s", menu
->title
);
781 /* Only set root menu once. */
785 if (get_token_str(data
, sizeof(data
)) < 0)
787 logf("%root_menu empty");
791 for (i
= 0; i
< menu_count
; i
++)
793 if (!strcasecmp(menus
[i
]->id
, data
))
804 if (menu
->itemcount
>= TAGMENU_MAX_ITEMS
)
806 logf("max itemcount reached");
811 if (menu
->items
[menu
->itemcount
] == NULL
)
813 menu
->items
[menu
->itemcount
] = buffer_alloc(sizeof(struct menu_entry
));
814 memset(menu
->items
[menu
->itemcount
], 0, sizeof(struct menu_entry
));
815 menu
->items
[menu
->itemcount
]->si
= buffer_alloc(sizeof(struct search_instruction
));
818 memset(menu
->items
[menu
->itemcount
]->si
, 0, sizeof(struct search_instruction
));
819 if (!parse_search(menu
->items
[menu
->itemcount
], buf
))
827 static bool parse_menu(const char *filename
)
832 if (menu_count
>= TAGMENU_MAX_MENUS
)
834 logf("max menucount reached");
838 fd
= open(filename
, O_RDONLY
);
841 logf("Search instruction file not found.");
845 /* Now read file for real, parsing into si */
846 fast_readline(fd
, buf
, sizeof buf
, NULL
, parse_line
);
852 void tagtree_init(void)
858 parse_menu(FILE_SEARCH_INSTRUCTIONS
);
860 uniqbuf
= buffer_alloc(UNIQBUF_SIZE
);
861 audio_set_track_buffer_event(tagtree_buffer_event
);
862 audio_set_track_unbuffer_event(tagtree_unbuffer_event
);
865 bool show_search_progress(bool init
, int count
)
867 static int last_tick
= 0;
871 last_tick
= current_tick
;
875 if (current_tick
- last_tick
> HZ
/4)
877 gui_syncsplash(0, true, str(LANG_PLAYLIST_SEARCH_MSG
), count
,
878 #if CONFIG_KEYPAD == PLAYER_PAD
884 if (action_userabort(TIMEOUT_NOBLOCK
))
886 last_tick
= current_tick
;
893 int format_str(struct tagcache_search
*tcs
, struct display_format
*fmt
,
894 char *buf
, int buf_size
)
897 bool read_format
= false;
903 memset(buf
, 0, buf_size
);
904 for (i
= 0; fmt
->formatstr
[i
] != '\0'; i
++)
906 if (fmt
->formatstr
[i
] == '%')
910 if (parpos
>= fmt
->tag_count
)
912 logf("too many format tags");
919 fmtbuf
[fmtbuf_pos
++] = fmt
->formatstr
[i
];
920 if (fmtbuf_pos
>= buf_size
)
922 logf("format parse error");
926 if (fmt
->formatstr
[i
] == 's')
928 fmtbuf
[fmtbuf_pos
] = '\0';
930 if (fmt
->tags
[parpos
] == tcs
->type
)
932 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
, tcs
->result
);
936 /* Need to fetch the tag data. */
937 if (!tagcache_retrieve(tcs
, tcs
->idx_id
, fmt
->tags
[parpos
],
938 &buf
[buf_pos
], buf_size
- buf_pos
))
940 logf("retrieve failed");
944 buf_pos
+= strlen(&buf
[buf_pos
]);
947 else if (fmt
->formatstr
[i
] == 'd')
949 fmtbuf
[fmtbuf_pos
] = '\0';
951 snprintf(&buf
[buf_pos
], buf_size
- buf_pos
, fmtbuf
,
952 tagcache_get_numeric(tcs
, fmt
->tags
[parpos
]));
953 buf_pos
+= strlen(&buf
[buf_pos
]);
959 buf
[buf_pos
++] = fmt
->formatstr
[i
];
961 if (buf_pos
- 1 >= buf_size
)
963 logf("buffer overflow");
968 buf
[buf_pos
++] = '\0';
973 int retrieve_entries(struct tree_context
*c
, struct tagcache_search
*tcs
,
974 int offset
, bool init
)
976 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
977 struct display_format
*fmt
;
981 int special_entry_count
= 0;
982 int level
= c
->currextra
;
989 #ifdef HAVE_TC_RAMCACHE
990 && !tagcache_is_ramcache()
994 show_search_progress(true, 0);
995 gui_syncsplash(0, true, str(LANG_PLAYLIST_SEARCH_MSG
),
999 if (c
->currtable
== allsubentries
)
1005 tag
= csi
->tagorder
[level
];
1007 if (!tagcache_search(tcs
, tag
))
1010 /* Prevent duplicate entries in the search list. */
1011 tagcache_search_set_uniqbuf(tcs
, uniqbuf
, UNIQBUF_SIZE
);
1013 if (level
|| csi
->clause_count
[0] || tagcache_is_numeric_tag(tag
))
1016 for (i
= 0; i
< level
; i
++)
1018 if (tagcache_is_numeric_tag(csi
->tagorder
[i
]))
1020 static struct tagcache_search_clause cc
;
1022 memset(&cc
, 0, sizeof(struct tagcache_search_clause
));
1023 cc
.tag
= csi
->tagorder
[i
];
1024 cc
.type
= clause_is
;
1026 cc
.numeric_data
= csi
->result_seek
[i
];
1027 tagcache_search_add_clause(tcs
, &cc
);
1031 tagcache_search_add_filter(tcs
, csi
->tagorder
[i
],
1032 csi
->result_seek
[i
]);
1036 for (i
= 0; i
<= level
; i
++)
1040 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1041 tagcache_search_add_clause(tcs
, csi
->clause
[i
][j
]);
1044 current_offset
= offset
;
1045 current_entry_count
= 0;
1049 for (i
= 0; i
< format_count
; i
++)
1051 if (formats
[i
]->group_id
== csi
->format_id
[level
])
1057 sort_inverse
= fmt
->sort_inverse
;
1058 sort_limit
= fmt
->limit
;
1063 sort_inverse
= false;
1068 if (tag
!= tag_title
&& tag
!= tag_filename
)
1072 dptr
->newtable
= allsubentries
;
1073 dptr
->name
= str(LANG_TAGNAVI_ALL_TRACKS
);
1075 current_entry_count
++;
1077 special_entry_count
++;
1080 total_count
+= special_entry_count
;
1082 while (tagcache_get_next(tcs
))
1084 if (total_count
++ < offset
)
1087 dptr
->newtable
= navibrowse
;
1088 if (tag
== tag_title
|| tag
== tag_filename
)
1090 dptr
->newtable
= playtrack
;
1091 dptr
->extraseek
= tcs
->idx_id
;
1094 dptr
->extraseek
= tcs
->result_seek
;
1097 /* Check the format */
1098 for (i
= 0; i
< format_count
; i
++)
1100 if (formats
[i
]->group_id
!= csi
->format_id
[level
])
1103 if (tagcache_check_clauses(tcs
, formats
[i
]->clause
,
1104 formats
[i
]->clause_count
))
1111 if (!tcs
->ramresult
|| fmt
)
1117 if (format_str(tcs
, fmt
, buf
, sizeof buf
) < 0)
1119 logf("format_str() failed");
1124 dptr
->name
= &c
->name_buffer
[namebufused
];
1126 namebufused
+= strlen(buf
)+1;
1128 namebufused
+= tcs
->result_len
;
1130 if (namebufused
>= c
->name_buffer_size
)
1132 logf("chunk mode #2: %d", current_entry_count
);
1138 strcpy(dptr
->name
, buf
);
1140 strcpy(dptr
->name
, tcs
->result
);
1143 dptr
->name
= tcs
->result
;
1146 current_entry_count
++;
1148 if (current_entry_count
>= global_settings
.max_files_in_dir
)
1150 logf("chunk mode #3: %d", current_entry_count
);
1156 if (init
&& !tcs
->ramsearch
)
1158 if (!show_search_progress(false, i
))
1160 tagcache_search_finish(tcs
);
1161 return current_entry_count
;
1167 qsort(c
->dircache
+ special_entry_count
* c
->dentry_size
,
1168 current_entry_count
- special_entry_count
,
1169 c
->dentry_size
, compare
);
1173 tagcache_search_finish(tcs
);
1174 return current_entry_count
;
1177 while (tagcache_get_next(tcs
))
1179 if (!tcs
->ramsearch
)
1181 if (!show_search_progress(false, total_count
))
1187 tagcache_search_finish(tcs
);
1189 if (!sort
&& (sort_inverse
|| sort_limit
))
1191 gui_syncsplash(HZ
*4, true, str(LANG_SHOWDIR_BUFFER_FULL
), total_count
);
1192 logf("Too small dir buffer");
1197 total_count
= MIN(total_count
, sort_limit
);
1202 for (i
= 0; i
< total_count
; i
++, dptr
++)
1204 int len
= strlen(dptr
->name
);
1209 dptr
->name
= &dptr
->name
[strip
];
1216 static int load_root(struct tree_context
*c
)
1218 struct tagentry
*dptr
= (struct tagentry
*)c
->dircache
;
1222 c
->currtable
= root
;
1223 if (c
->dirlevel
== 0)
1224 c
->currextra
= root_menu
;
1226 menu
= menus
[c
->currextra
];
1230 for (i
= 0; i
< menu
->itemcount
; i
++)
1232 dptr
->name
= menu
->items
[i
]->name
;
1233 switch (menu
->items
[i
]->type
)
1236 dptr
->newtable
= navibrowse
;
1237 dptr
->extraseek
= i
;
1241 dptr
->newtable
= root
;
1242 dptr
->extraseek
= menu
->items
[i
]->link
;
1250 current_entry_count
= i
;
1255 int tagtree_load(struct tree_context
* c
)
1258 int table
= c
->currtable
;
1260 c
->dentry_size
= sizeof(struct tagentry
);
1267 c
->currtable
= table
;
1268 c
->currextra
= root_menu
;
1274 count
= load_root(c
);
1279 logf("navibrowse...");
1280 cpu_boost_id(true, CPUBOOSTID_TAGTREE
);
1281 count
= retrieve_entries(c
, &tcs
, 0, true);
1282 cpu_boost_id(false, CPUBOOSTID_TAGTREE
);
1286 logf("Unsupported table %d\n", table
);
1293 count
= load_root(c
);
1294 gui_syncsplash(HZ
, true, str(LANG_TAGCACHE_BUSY
));
1297 /* The _total_ numer of entries available. */
1298 c
->dirlength
= c
->filesindir
= count
;
1303 int tagtree_enter(struct tree_context
* c
)
1306 struct tagentry
*dptr
;
1310 dptr
= tagtree_get_entry(c
, c
->selected_item
);
1313 newextra
= dptr
->newtable
;
1314 seek
= dptr
->extraseek
;
1316 if (c
->dirlevel
>= MAX_DIR_LEVELS
)
1319 c
->selected_item_history
[c
->dirlevel
]=c
->selected_item
;
1320 c
->table_history
[c
->dirlevel
] = c
->currtable
;
1321 c
->extra_history
[c
->dirlevel
] = c
->currextra
;
1322 c
->pos_history
[c
->dirlevel
] = c
->firstpos
;
1325 switch (c
->currtable
) {
1327 c
->currextra
= newextra
;
1329 if (newextra
== root
)
1332 c
->currextra
= seek
;
1335 else if (newextra
== navibrowse
)
1339 csi
= menu
->items
[seek
]->si
;
1342 strncpy(current_title
[c
->currextra
], dptr
->name
,
1343 sizeof(current_title
[0]) - 1);
1345 /* Read input as necessary. */
1346 for (i
= 0; i
< csi
->tagorder_count
; i
++)
1348 for (j
= 0; j
< csi
->clause_count
[i
]; j
++)
1350 if (!csi
->clause
[i
][j
]->input
)
1353 rc
= kbd_input(searchstring
, sizeof(searchstring
));
1354 if (rc
== -1 || !searchstring
[0])
1360 if (csi
->clause
[i
][j
]->numeric
)
1361 csi
->clause
[i
][j
]->numeric_data
= atoi(searchstring
);
1363 csi
->clause
[i
][j
]->str
= searchstring
;
1367 c
->currtable
= newextra
;
1373 if (newextra
== playtrack
)
1376 /* about to create a new current playlist...
1377 allow user to cancel the operation */
1378 if (global_settings
.warnon_erase_dynplaylist
&&
1379 !global_settings
.party_mode
&&
1380 playlist_modified(NULL
))
1382 char *lines
[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
1383 struct text_message message
={lines
, 1};
1385 if (gui_syncyesno_run(&message
, NULL
, NULL
) != YESNO_YES
)
1389 if (tagtree_play_folder(c
) >= 0)
1394 c
->currtable
= newextra
;
1395 csi
->result_seek
[c
->currextra
] = seek
;
1396 if (c
->currextra
< csi
->tagorder_count
-1)
1401 /* Update the statusbar title */
1402 strncpy(current_title
[c
->currextra
], dptr
->name
,
1403 sizeof(current_title
[0]) - 1);
1412 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1417 void tagtree_exit(struct tree_context
* c
)
1420 if (c
->dirlevel
> 0)
1422 c
->selected_item
=c
->selected_item_history
[c
->dirlevel
];
1423 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1424 c
->currtable
= c
->table_history
[c
->dirlevel
];
1425 c
->currextra
= c
->extra_history
[c
->dirlevel
];
1426 c
->firstpos
= c
->pos_history
[c
->dirlevel
];
1429 int tagtree_get_filename(struct tree_context
* c
, char *buf
, int buflen
)
1431 struct tagentry
*entry
;
1433 entry
= tagtree_get_entry(c
, c
->selected_item
);
1435 if (!tagcache_search(&tcs
, tag_filename
))
1438 if (!tagcache_retrieve(&tcs
, entry
->extraseek
, tcs
.type
, buf
, buflen
))
1440 tagcache_search_finish(&tcs
);
1444 tagcache_search_finish(&tcs
);
1449 bool insert_all_playlist(struct tree_context
*c
, int position
, bool queue
)
1453 int from
, to
, direction
;
1454 int files_left
= c
->filesindir
;
1456 cpu_boost_id(true, CPUBOOSTID_TAGTREE
);
1457 if (!tagcache_search(&tcs
, tag_filename
))
1459 gui_syncsplash(HZ
, true, str(LANG_TAGCACHE_BUSY
));
1460 cpu_boost_id(false, CPUBOOSTID_TAGTREE
);
1464 if (position
== PLAYLIST_INSERT_FIRST
)
1466 from
= c
->filesindir
- 1;
1477 for (i
= from
; i
!= to
; i
+= direction
)
1479 /* Count back to zero */
1480 if (!show_search_progress(false, files_left
--))
1483 if (!tagcache_retrieve(&tcs
, tagtree_get_entry(c
, i
)->extraseek
,
1484 tcs
.type
, buf
, sizeof buf
))
1489 if (playlist_insert_track(NULL
, buf
, position
, queue
, false) < 0)
1491 logf("playlist_insert_track failed");
1496 playlist_sync(NULL
);
1497 tagcache_search_finish(&tcs
);
1498 cpu_boost_id(false, CPUBOOSTID_TAGTREE
);
1503 bool tagtree_insert_selection_playlist(int position
, bool queue
)
1505 struct tagentry
*dptr
;
1507 int dirlevel
= tc
->dirlevel
;
1509 /* We need to set the table to allsubentries. */
1510 show_search_progress(true, 0);
1512 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1514 /* Insert a single track? */
1515 if (dptr
->newtable
== playtrack
)
1517 if (tagtree_get_filename(tc
, buf
, sizeof buf
) < 0)
1519 logf("tagtree_get_filename failed");
1522 playlist_insert_track(NULL
, buf
, position
, queue
, true);
1527 if (dptr
->newtable
== navibrowse
)
1531 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1533 else if (dptr
->newtable
!= allsubentries
)
1535 logf("unsupported table: %d", dptr
->newtable
);
1539 /* Now the current table should be allsubentries. */
1540 if (dptr
->newtable
!= playtrack
)
1544 dptr
= tagtree_get_entry(tc
, tc
->selected_item
);
1546 /* And now the newtable should be playtrack. */
1547 if (dptr
->newtable
!= playtrack
)
1549 logf("newtable: %d !!", dptr
->newtable
);
1550 tc
->dirlevel
= dirlevel
;
1555 if (tc
->filesindir
<= 0)
1556 gui_syncsplash(HZ
, true, str(LANG_END_PLAYLIST_PLAYER
));
1559 logf("insert_all_playlist");
1560 if (!insert_all_playlist(tc
, position
, queue
))
1561 gui_syncsplash(HZ
*2, true, str(LANG_FAILED
));
1564 /* Finally return the dirlevel to its original value. */
1565 while (tc
->dirlevel
> dirlevel
)
1572 static int tagtree_play_folder(struct tree_context
* c
)
1574 if (playlist_create(NULL
, NULL
) < 0)
1576 logf("Failed creating playlist\n");
1580 if (!insert_all_playlist(c
, PLAYLIST_INSERT_LAST
, false))
1583 if (global_settings
.playlist_shuffle
)
1584 c
->selected_item
= playlist_shuffle(current_tick
, c
->selected_item
);
1585 if (!global_settings
.play_selected
)
1586 c
->selected_item
= 0;
1587 gui_synclist_select_item(&tree_lists
, c
->selected_item
);
1589 playlist_start(c
->selected_item
,0);
1594 struct tagentry
* tagtree_get_entry(struct tree_context
*c
, int id
)
1596 struct tagentry
*entry
= (struct tagentry
*)c
->dircache
;
1597 int realid
= id
- current_offset
;
1599 /* Load the next chunk if necessary. */
1600 if (realid
>= current_entry_count
|| realid
< 0)
1602 cpu_boost_id(true, CPUBOOSTID_TAGTREE
);
1603 if (retrieve_entries(c
, &tcs2
, MAX(0, id
- (current_entry_count
/ 2)),
1606 logf("retrieve failed");
1607 cpu_boost_id(false, CPUBOOSTID_TAGTREE
);
1610 realid
= id
- current_offset
;
1611 cpu_boost_id(false, CPUBOOSTID_TAGTREE
);
1614 return &entry
[realid
];
1617 char *tagtree_get_title(struct tree_context
* c
)
1619 switch (c
->currtable
)
1626 return current_title
[c
->currextra
];
1632 int tagtree_get_attr(struct tree_context
* c
)
1635 switch (c
->currtable
)
1638 if (csi
->tagorder
[c
->currextra
] == tag_title
)
1639 attr
= TREE_ATTR_MPA
;
1641 attr
= ATTR_DIRECTORY
;
1645 attr
= TREE_ATTR_MPA
;
1649 attr
= ATTR_DIRECTORY
;
1656 #ifdef HAVE_LCD_BITMAP
1657 const unsigned char* tagtree_get_icon(struct tree_context
* c
)
1659 int tagtree_get_icon(struct tree_context
* c
)
1662 int icon
= Icon_Folder
;
1664 if (tagtree_get_attr(c
) == TREE_ATTR_MPA
)
1667 #ifdef HAVE_LCD_BITMAP
1668 return bitmap_icons_6x8
[icon
];