Correct wrong usage of event callbacks all over the place. It's not supposed to retur...
[kugel-rb.git] / apps / tagtree.c
blob832a49e3594d4a2c49e258de180ac5ac3e89e3b3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Miika Pekkarinen
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 /**
23 * Basic structure on this file was copied from dbtree.c and modified to
24 * support the tag cache interface.
27 /* #define LOGF_ENABLE */
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include "config.h"
33 #include "system.h"
34 #include "kernel.h"
35 #include "splash.h"
36 #include "icons.h"
37 #include "tree.h"
38 #include "action.h"
39 #include "settings.h"
40 #include "tagcache.h"
41 #include "tagtree.h"
42 #include "lang.h"
43 #include "logf.h"
44 #include "playlist.h"
45 #include "keyboard.h"
46 #include "gui/list.h"
47 #include "buffer.h"
48 #include "playback.h"
49 #include "yesno.h"
50 #include "misc.h"
51 #include "filetypes.h"
52 #include "audio.h"
53 #include "appevents.h"
54 #include "storage.h"
55 #include "dir_uncached.h"
57 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
59 static int tagtree_play_folder(struct tree_context* c);
61 #define SEARCHSTR_SIZE 256
63 enum table {
64 ROOT = 1,
65 NAVIBROWSE,
66 ALLSUBENTRIES,
67 PLAYTRACK,
70 static const struct id3_to_search_mapping {
71 char *string;
72 size_t id3_offset;
73 } id3_to_search_mapping[] = {
74 { "", 0 }, /* offset n/a */
75 { "#directory#", 0 }, /* offset n/a */
76 { "#title#", offsetof(struct mp3entry, title) },
77 { "#artist#", offsetof(struct mp3entry, artist) },
78 { "#album#", offsetof(struct mp3entry, album) },
79 { "#genre#", offsetof(struct mp3entry, genre_string) },
80 { "#composer#", offsetof(struct mp3entry, composer) },
81 { "#albumartist#", offsetof(struct mp3entry, albumartist) },
83 enum variables {
84 var_sorttype = 100,
85 var_limit,
86 var_strip,
87 var_menu_start,
88 var_include,
89 var_rootmenu,
90 var_format,
91 menu_next,
92 menu_load,
95 /* Capacity 10 000 entries (for example 10k different artists) */
96 #define UNIQBUF_SIZE (64*1024)
97 static long *uniqbuf;
99 #define MAX_TAGS 5
100 #define MAX_MENU_ID_SIZE 32
102 static bool sort_inverse;
105 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
107 * valid = true
108 * formatstr = "%-3d. %s"
109 * tags[0] = tag_autoscore
110 * tags[1] = tag_title
111 * tag_count = 2
113 * limit = 100
114 * sort_inverse = true
116 struct display_format {
117 char name[32];
118 struct tagcache_search_clause *clause[TAGCACHE_MAX_CLAUSES];
119 int clause_count;
120 char *formatstr;
121 int group_id;
122 int tags[MAX_TAGS];
123 int tag_count;
125 int limit;
126 int strip;
127 bool sort_inverse;
130 static struct display_format *formats[TAGMENU_MAX_FMTS];
131 static int format_count;
133 struct search_instruction {
134 char name[64];
135 int tagorder[MAX_TAGS];
136 int tagorder_count;
137 struct tagcache_search_clause *clause[MAX_TAGS][TAGCACHE_MAX_CLAUSES];
138 int format_id[MAX_TAGS];
139 int clause_count[MAX_TAGS];
140 int result_seek[MAX_TAGS];
143 struct menu_entry {
144 char name[64];
145 int type;
146 struct search_instruction *si;
147 int link;
150 struct menu_root {
151 char title[64];
152 char id[MAX_MENU_ID_SIZE];
153 int itemcount;
154 struct menu_entry *items[TAGMENU_MAX_ITEMS];
157 /* Statusbar text of the current view. */
158 static char current_title[MAX_TAGS][128];
160 static struct menu_root *menus[TAGMENU_MAX_MENUS];
161 static struct menu_root *menu;
162 static struct search_instruction *csi;
163 static const char *strp;
164 static int menu_count;
165 static int rootmenu;
167 static int current_offset;
168 static int current_entry_count;
170 static struct tree_context *tc;
172 static int get_token_str(char *buf, int size)
174 /* Find the start. */
175 while (*strp != '"' && *strp != '\0')
176 strp++;
178 if (*strp == '\0' || *(++strp) == '\0')
179 return -1;
181 /* Read the data. */
182 while (*strp != '"' && *strp != '\0' && --size > 0)
183 *(buf++) = *(strp++);
185 *buf = '\0';
186 if (*strp != '"')
187 return -2;
189 strp++;
191 return 0;
194 #define MATCH(tag,str1,str2,settag) \
195 if (!strcasecmp(str1, str2)) { \
196 *tag = settag; \
197 return 1; \
200 static int get_tag(int *tag)
202 char buf[128];
203 int i;
205 /* Find the start. */
206 while ((*strp == ' ' || *strp == '>') && *strp != '\0')
207 strp++;
209 if (*strp == '\0' || *strp == '?')
210 return 0;
212 for (i = 0; i < (int)sizeof(buf)-1; i++)
214 if (*strp == '\0' || *strp == ' ')
215 break ;
216 buf[i] = *strp;
217 strp++;
219 buf[i] = '\0';
221 MATCH(tag, buf, "album", tag_album);
222 MATCH(tag, buf, "artist", tag_artist);
223 MATCH(tag, buf, "bitrate", tag_bitrate);
224 MATCH(tag, buf, "composer", tag_composer);
225 MATCH(tag, buf, "comment", tag_comment);
226 MATCH(tag, buf, "albumartist", tag_albumartist);
227 MATCH(tag, buf, "ensemble", tag_albumartist);
228 MATCH(tag, buf, "grouping", tag_grouping);
229 MATCH(tag, buf, "genre", tag_genre);
230 MATCH(tag, buf, "length", tag_length);
231 MATCH(tag, buf, "Lm", tag_virt_length_min);
232 MATCH(tag, buf, "Ls", tag_virt_length_sec);
233 MATCH(tag, buf, "Pm", tag_virt_playtime_min);
234 MATCH(tag, buf, "Ps", tag_virt_playtime_sec);
235 MATCH(tag, buf, "title", tag_title);
236 MATCH(tag, buf, "filename", tag_filename);
237 MATCH(tag, buf, "tracknum", tag_tracknumber);
238 MATCH(tag, buf, "discnum", tag_discnumber);
239 MATCH(tag, buf, "year", tag_year);
240 MATCH(tag, buf, "playcount", tag_playcount);
241 MATCH(tag, buf, "rating", tag_rating);
242 MATCH(tag, buf, "lastplayed", tag_lastplayed);
243 MATCH(tag, buf, "commitid", tag_commitid);
244 MATCH(tag, buf, "entryage", tag_virt_entryage);
245 MATCH(tag, buf, "autoscore", tag_virt_autoscore);
246 MATCH(tag, buf, "%sort", var_sorttype);
247 MATCH(tag, buf, "%limit", var_limit);
248 MATCH(tag, buf, "%strip", var_strip);
249 MATCH(tag, buf, "%menu_start", var_menu_start);
250 MATCH(tag, buf, "%include", var_include);
251 MATCH(tag, buf, "%root_menu", var_rootmenu);
252 MATCH(tag, buf, "%format", var_format);
253 MATCH(tag, buf, "->", menu_next);
254 MATCH(tag, buf, "==>", menu_load);
256 logf("NO MATCH: %s\n", buf);
257 if (buf[0] == '?')
258 return 0;
260 return -1;
263 static int get_clause(int *condition)
265 char buf[4];
266 int i;
268 /* Find the start. */
269 while (*strp == ' ' && *strp != '\0')
270 strp++;
272 if (*strp == '\0')
273 return 0;
275 for (i = 0; i < (int)sizeof(buf)-1; i++)
277 if (*strp == '\0' || *strp == ' ')
278 break ;
279 buf[i] = *strp;
280 strp++;
282 buf[i] = '\0';
284 MATCH(condition, buf, "=", clause_is);
285 MATCH(condition, buf, "==", clause_is);
286 MATCH(condition, buf, "!=", clause_is_not);
287 MATCH(condition, buf, ">", clause_gt);
288 MATCH(condition, buf, ">=", clause_gteq);
289 MATCH(condition, buf, "<", clause_lt);
290 MATCH(condition, buf, "<=", clause_lteq);
291 MATCH(condition, buf, "~", clause_contains);
292 MATCH(condition, buf, "!~", clause_not_contains);
293 MATCH(condition, buf, "^", clause_begins_with);
294 MATCH(condition, buf, "!^", clause_not_begins_with);
295 MATCH(condition, buf, "$", clause_ends_with);
296 MATCH(condition, buf, "!$", clause_not_ends_with);
297 MATCH(condition, buf, "@", clause_oneof);
299 return 0;
302 static bool read_clause(struct tagcache_search_clause *clause)
304 char buf[SEARCHSTR_SIZE];
305 unsigned int i;
307 if (get_tag(&clause->tag) <= 0)
308 return false;
310 if (get_clause(&clause->type) <= 0)
311 return false;
313 if (get_token_str(buf, sizeof buf) < 0)
314 return false;
316 for (i=0; i<ARRAYLEN(id3_to_search_mapping); i++)
318 if (!strcasecmp(buf, id3_to_search_mapping[i].string))
319 break;
322 if (i<ARRAYLEN(id3_to_search_mapping)) /* runtime search operand found */
324 clause->source = source_runtime+i;
325 clause->str = buffer_alloc(SEARCHSTR_SIZE);
327 else
329 clause->source = source_constant;
330 clause->str = buffer_alloc(strlen(buf)+1);
331 strcpy(clause->str, buf);
334 if (TAGCACHE_IS_NUMERIC(clause->tag))
336 clause->numeric = true;
337 clause->numeric_data = atoi(clause->str);
339 else
340 clause->numeric = false;
342 logf("got clause: %d/%d [%s]", clause->tag, clause->type, clause->str);
344 return true;
347 static bool read_variable(char *buf, int size)
349 int condition;
351 if (!get_clause(&condition))
352 return false;
354 if (condition != clause_is)
355 return false;
357 if (get_token_str(buf, size) < 0)
358 return false;
360 return true;
363 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
364 static int get_format_str(struct display_format *fmt)
366 int ret;
367 char buf[128];
368 int i;
370 memset(fmt, 0, sizeof(struct display_format));
372 if (get_token_str(fmt->name, sizeof fmt->name) < 0)
373 return -10;
375 /* Determine the group id */
376 fmt->group_id = 0;
377 for (i = 0; i < format_count; i++)
379 if (!strcasecmp(formats[i]->name, fmt->name))
381 fmt->group_id = formats[i]->group_id;
382 break;
385 if (formats[i]->group_id > fmt->group_id)
386 fmt->group_id = formats[i]->group_id;
389 if (i == format_count)
390 fmt->group_id++;
392 logf("format: (%d) %s", fmt->group_id, fmt->name);
394 if (get_token_str(buf, sizeof buf) < 0)
395 return -10;
397 fmt->formatstr = buffer_alloc(strlen(buf) + 1);
398 strcpy(fmt->formatstr, buf);
400 while (fmt->tag_count < MAX_TAGS)
402 ret = get_tag(&fmt->tags[fmt->tag_count]);
403 if (ret < 0)
404 return -11;
406 if (ret == 0)
407 break;
409 switch (fmt->tags[fmt->tag_count]) {
410 case var_sorttype:
411 if (!read_variable(buf, sizeof buf))
412 return -12;
413 if (!strcasecmp("inverse", buf))
414 fmt->sort_inverse = true;
415 break;
417 case var_limit:
418 if (!read_variable(buf, sizeof buf))
419 return -13;
420 fmt->limit = atoi(buf);
421 break;
423 case var_strip:
424 if (!read_variable(buf, sizeof buf))
425 return -14;
426 fmt->strip = atoi(buf);
427 break;
429 default:
430 fmt->tag_count++;
434 return 1;
437 static int add_format(const char *buf)
439 strp = buf;
441 if (formats[format_count] == NULL)
442 formats[format_count] = buffer_alloc(sizeof(struct display_format));
444 memset(formats[format_count], 0, sizeof(struct display_format));
445 if (get_format_str(formats[format_count]) < 0)
447 logf("get_format_str() parser failed!");
448 return -4;
451 while (*strp != '\0' && *strp != '?')
452 strp++;
454 if (*strp == '?')
456 int clause_count = 0;
457 strp++;
459 while (1)
461 if (clause_count >= TAGCACHE_MAX_CLAUSES)
463 logf("too many clauses");
464 break;
467 formats[format_count]->clause[clause_count] =
468 buffer_alloc(sizeof(struct tagcache_search_clause));
470 if (!read_clause(formats[format_count]->clause[clause_count]))
471 break;
473 clause_count++;
476 formats[format_count]->clause_count = clause_count;
479 format_count++;
481 return 1;
484 static int get_condition(struct search_instruction *inst)
486 int clause_count;
487 char buf[128];
489 switch (*strp)
491 case '=':
493 int i;
495 if (get_token_str(buf, sizeof buf) < 0)
496 return -1;
498 for (i = 0; i < format_count; i++)
500 if (!strcasecmp(formats[i]->name, buf))
501 break;
504 if (i == format_count)
506 logf("format not found: %s", buf);
507 return -2;
510 inst->format_id[inst->tagorder_count] = formats[i]->group_id;
511 return 1;
513 case '?':
514 case ' ':
515 case '&':
516 strp++;
517 return 1;
518 case '-':
519 case '\0':
520 return 0;
523 clause_count = inst->clause_count[inst->tagorder_count];
524 if (clause_count >= TAGCACHE_MAX_CLAUSES)
526 logf("Too many clauses");
527 return false;
530 inst->clause[inst->tagorder_count][clause_count] =
531 buffer_alloc(sizeof(struct tagcache_search_clause));
533 if (!read_clause(inst->clause[inst->tagorder_count][clause_count]))
534 return -1;
536 inst->clause_count[inst->tagorder_count]++;
538 return 1;
541 /* example search:
542 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
543 * : album ? year >= "2000" : songs
544 * ^ begins with
545 * * contains
546 * $ ends with
549 static bool parse_search(struct menu_entry *entry, const char *str)
551 int ret;
552 int type;
553 struct search_instruction *inst = entry->si;
554 char buf[MAX_PATH];
555 int i;
556 struct menu_root *new_menu;
558 strp = str;
560 /* Parse entry name */
561 if (get_token_str(entry->name, sizeof entry->name) < 0)
563 logf("No name found.");
564 return false;
567 /* Parse entry type */
568 if (get_tag(&entry->type) <= 0)
569 return false;
571 if (entry->type == menu_load)
573 if (get_token_str(buf, sizeof buf) < 0)
574 return false;
576 /* Find the matching root menu or "create" it */
577 for (i = 0; i < menu_count; i++)
579 if (!strcasecmp(menus[i]->id, buf))
581 entry->link = i;
582 return true;
586 if (menu_count >= TAGMENU_MAX_MENUS)
588 logf("max menucount reached");
589 return false;
592 /* Allocate a new menu unless link is found. */
593 menus[menu_count] = buffer_alloc(sizeof(struct menu_root));
594 new_menu = menus[menu_count];
595 memset(new_menu, 0, sizeof(struct menu_root));
596 strlcpy(new_menu->id, buf, MAX_MENU_ID_SIZE);
597 entry->link = menu_count;
598 ++menu_count;
600 return true;
603 if (entry->type != menu_next)
604 return false;
606 while (inst->tagorder_count < MAX_TAGS)
608 ret = get_tag(&inst->tagorder[inst->tagorder_count]);
609 if (ret < 0)
611 logf("Parse error #1");
612 logf("%s", strp);
613 return false;
616 if (ret == 0)
617 break ;
619 logf("tag: %d", inst->tagorder[inst->tagorder_count]);
621 while ( (ret = get_condition(inst)) > 0 ) ;
622 if (ret < 0)
623 return false;
625 inst->tagorder_count++;
627 if (get_tag(&type) <= 0 || type != menu_next)
628 break;
631 return true;
634 static int compare(const void *p1, const void *p2)
636 struct tagentry *e1 = (struct tagentry *)p1;
637 struct tagentry *e2 = (struct tagentry *)p2;
639 if (sort_inverse)
640 return strncasecmp(e2->name, e1->name, MAX_PATH);
642 return strncasecmp(e1->name, e2->name, MAX_PATH);
645 static void tagtree_buffer_event(void *data)
647 struct tagcache_search tcs;
648 struct mp3entry *id3 = (struct mp3entry*)data;
650 /* Do not gather data unless proper setting has been enabled. */
651 if (!global_settings.runtimedb)
652 return;
654 logf("be:%s", id3->path);
656 if (!tagcache_find_index(&tcs, id3->path))
658 logf("tc stat: not found: %s", id3->path);
659 return;
662 id3->playcount = tagcache_get_numeric(&tcs, tag_playcount);
663 if (!id3->rating)
664 id3->rating = tagcache_get_numeric(&tcs, tag_rating);
665 id3->lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
666 id3->score = tagcache_get_numeric(&tcs, tag_virt_autoscore) / 10;
667 id3->playtime = tagcache_get_numeric(&tcs, tag_playtime);
669 /* Store our tagcache index pointer. */
670 id3->tagcache_idx = tcs.idx_id+1;
672 tagcache_search_finish(&tcs);
675 static void tagtree_track_finish_event(void *data)
677 long playcount;
678 long playtime;
679 long lastplayed;
680 long tagcache_idx;
681 struct mp3entry *id3 = (struct mp3entry*)data;
683 /* Do not gather data unless proper setting has been enabled. */
684 if (!global_settings.runtimedb)
686 logf("runtimedb gathering not enabled");
687 return;
690 tagcache_idx=id3->tagcache_idx;
691 if (!tagcache_idx)
693 logf("No tagcache index pointer found");
694 return;
696 tagcache_idx--;
698 /* Don't process unplayed tracks. */
699 if (id3->elapsed == 0)
701 logf("not logging unplayed track");
702 return;
705 playcount = id3->playcount + 1;
706 lastplayed = tagcache_increase_serial();
707 if (lastplayed < 0)
709 logf("incorrect tc serial:%ld", lastplayed);
710 return;
713 /* Ignore the last 15s (crossfade etc.) */
714 playtime = id3->playtime + MIN(id3->length, id3->elapsed + 15 * 1000);
716 logf("ube:%s", id3->path);
717 logf("-> %ld/%ld", playcount, playtime);
718 logf("-> %ld/%ld/%ld", id3->elapsed, id3->length, MIN(id3->length, id3->elapsed + 15 * 1000));
720 /* Queue the updates to the tagcache system. */
721 tagcache_update_numeric(tagcache_idx, tag_playcount, playcount);
722 tagcache_update_numeric(tagcache_idx, tag_playtime, playtime);
723 tagcache_update_numeric(tagcache_idx, tag_lastplayed, lastplayed);
726 bool tagtree_export(void)
728 struct tagcache_search tcs;
730 splash(0, str(LANG_CREATING));
731 if (!tagcache_create_changelog(&tcs))
733 splash(HZ*2, ID2P(LANG_FAILED));
736 return false;
739 bool tagtree_import(void)
741 splash(0, ID2P(LANG_WAIT));
742 if (!tagcache_import_changelog())
744 splash(HZ*2, ID2P(LANG_FAILED));
747 return false;
750 static bool parse_menu(const char *filename);
752 static int parse_line(int n, const char *buf, void *parameters)
754 char data[256];
755 int variable;
756 static bool read_menu;
757 int i;
759 (void)parameters;
761 logf("parse:%d/%s", n, buf);
763 /* First line, do initialisation. */
764 if (n == 0)
766 if (strcasecmp(TAGNAVI_VERSION, buf))
768 logf("Version mismatch");
769 return -1;
772 read_menu = false;
775 if (buf[0] == '#')
776 return 0;
778 if (buf[0] == '\0')
780 if (read_menu)
782 /* End the menu */
783 read_menu = false;
785 return 0;
788 if (!read_menu)
790 strp = buf;
791 if (get_tag(&variable) <= 0)
792 return 0;
794 switch (variable)
796 case var_format:
797 if (add_format(strp) < 0)
799 logf("Format add fail: %s", data);
801 break;
803 case var_include:
804 if (get_token_str(data, sizeof(data)) < 0)
806 logf("%%include empty");
807 return 0;
810 if (!parse_menu(data))
812 logf("Load menu fail: %s", data);
814 break;
816 case var_menu_start:
817 if (menu_count >= TAGMENU_MAX_MENUS)
819 logf("max menucount reached");
820 return 0;
823 if (get_token_str(data, sizeof data) < 0)
825 logf("%%menu_start id empty");
826 return 0;
829 menu = NULL;
830 for (i = 0; i < menu_count; i++)
832 if (!strcasecmp(menus[i]->id, data))
834 menu = menus[i];
838 if (menu == NULL)
840 menus[menu_count] = buffer_alloc(sizeof(struct menu_root));
841 menu = menus[menu_count];
842 ++menu_count;
843 memset(menu, 0, sizeof(struct menu_root));
844 strlcpy(menu->id, data, MAX_MENU_ID_SIZE);
847 if (get_token_str(menu->title, sizeof(menu->title)) < 0)
849 logf("%%menu_start title empty");
850 return 0;
852 logf("menu: %s", menu->title);
853 read_menu = true;
854 break;
856 case var_rootmenu:
857 /* Only set root menu once. */
858 if (rootmenu >= 0)
859 break;
861 if (get_token_str(data, sizeof(data)) < 0)
863 logf("%%rootmenu empty");
864 return 0;
867 for (i = 0; i < menu_count; i++)
869 if (!strcasecmp(menus[i]->id, data))
871 rootmenu = i;
874 break;
877 return 0;
880 if (menu->itemcount >= TAGMENU_MAX_ITEMS)
882 logf("max itemcount reached");
883 return 0;
886 /* Allocate */
887 if (menu->items[menu->itemcount] == NULL)
889 menu->items[menu->itemcount] = buffer_alloc(sizeof(struct menu_entry));
890 memset(menu->items[menu->itemcount], 0, sizeof(struct menu_entry));
891 menu->items[menu->itemcount]->si = buffer_alloc(sizeof(struct search_instruction));
894 memset(menu->items[menu->itemcount]->si, 0, sizeof(struct search_instruction));
895 if (!parse_search(menu->items[menu->itemcount], buf))
896 return 0;
898 menu->itemcount++;
900 return 0;
903 static bool parse_menu(const char *filename)
905 int fd;
906 char buf[1024];
908 if (menu_count >= TAGMENU_MAX_MENUS)
910 logf("max menucount reached");
911 return false;
914 fd = open(filename, O_RDONLY);
915 if (fd < 0)
917 logf("Search instruction file not found.");
918 return false;
921 /* Now read file for real, parsing into si */
922 fast_readline(fd, buf, sizeof buf, NULL, parse_line);
923 close(fd);
925 return true;
928 void tagtree_init(void)
930 format_count = 0;
931 menu_count = 0;
932 menu = NULL;
933 rootmenu = -1;
934 parse_menu(FILE_SEARCH_INSTRUCTIONS);
936 /* If no root menu is set, assume it's the first single menu
937 * we have. That shouldn't normally happen. */
938 if (rootmenu < 0)
939 rootmenu = 0;
941 uniqbuf = buffer_alloc(UNIQBUF_SIZE);
943 add_event(PLAYBACK_EVENT_TRACK_BUFFER, false, tagtree_buffer_event);
944 add_event(PLAYBACK_EVENT_TRACK_FINISH, false, tagtree_track_finish_event);
947 static bool show_search_progress(bool init, int count)
949 static int last_tick = 0;
951 /* Don't show splashes for 1/2 second after starting search */
952 if (init)
954 last_tick = current_tick + HZ/2;
955 return true;
958 /* Update progress every 1/10 of a second */
959 if (TIME_AFTER(current_tick, last_tick + HZ/10))
961 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG), count, str(LANG_OFF_ABORT));
962 if (action_userabort(TIMEOUT_NOBLOCK))
963 return false;
964 last_tick = current_tick;
965 yield();
968 return true;
971 static int format_str(struct tagcache_search *tcs, struct display_format *fmt,
972 char *buf, int buf_size)
974 char fmtbuf[8];
975 bool read_format = false;
976 int fmtbuf_pos = 0;
977 int parpos = 0;
978 int buf_pos = 0;
979 int i;
981 memset(buf, 0, buf_size);
982 for (i = 0; fmt->formatstr[i] != '\0'; i++)
984 if (fmt->formatstr[i] == '%')
986 read_format = true;
987 fmtbuf_pos = 0;
988 if (parpos >= fmt->tag_count)
990 logf("too many format tags");
991 return -1;
995 if (read_format)
997 fmtbuf[fmtbuf_pos++] = fmt->formatstr[i];
998 if (fmtbuf_pos >= buf_size)
1000 logf("format parse error");
1001 return -2;
1004 if (fmt->formatstr[i] == 's')
1006 fmtbuf[fmtbuf_pos] = '\0';
1007 read_format = false;
1008 if (fmt->tags[parpos] == tcs->type)
1010 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf, tcs->result);
1012 else
1014 /* Need to fetch the tag data. */
1015 if (!tagcache_retrieve(tcs, tcs->idx_id, fmt->tags[parpos],
1016 &buf[buf_pos], buf_size - buf_pos))
1018 logf("retrieve failed");
1019 return -3;
1022 buf_pos += strlen(&buf[buf_pos]);
1023 parpos++;
1025 else if (fmt->formatstr[i] == 'd')
1027 fmtbuf[fmtbuf_pos] = '\0';
1028 read_format = false;
1029 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf,
1030 tagcache_get_numeric(tcs, fmt->tags[parpos]));
1031 buf_pos += strlen(&buf[buf_pos]);
1032 parpos++;
1034 continue;
1037 buf[buf_pos++] = fmt->formatstr[i];
1039 if (buf_pos - 1 >= buf_size)
1041 logf("buffer overflow");
1042 return -4;
1046 buf[buf_pos++] = '\0';
1048 return 0;
1051 static int retrieve_entries(struct tree_context *c, int offset, bool init)
1053 struct tagcache_search tcs;
1054 struct tagentry *dptr = (struct tagentry *)c->dircache;
1055 struct display_format *fmt;
1056 int i;
1057 int namebufused = 0;
1058 int total_count = 0;
1059 int special_entry_count = 0;
1060 int level = c->currextra;
1061 int tag;
1062 bool sort = false;
1063 int sort_limit;
1064 int strip;
1066 /* Show search progress straight away if the disk needs to spin up,
1067 otherwise show it after the normal 1/2 second delay */
1068 show_search_progress(
1069 #ifdef HAVE_DISK_STORAGE
1070 storage_disk_is_active()
1071 #else
1072 true
1073 #endif
1074 , 0);
1076 if (c->currtable == ALLSUBENTRIES)
1078 tag = tag_title;
1079 level--;
1081 else
1082 tag = csi->tagorder[level];
1084 if (!tagcache_search(&tcs, tag))
1085 return -1;
1087 /* Prevent duplicate entries in the search list. */
1088 tagcache_search_set_uniqbuf(&tcs, uniqbuf, UNIQBUF_SIZE);
1090 if (level || csi->clause_count[0] || TAGCACHE_IS_NUMERIC(tag))
1091 sort = true;
1093 for (i = 0; i < level; i++)
1095 if (TAGCACHE_IS_NUMERIC(csi->tagorder[i]))
1097 static struct tagcache_search_clause cc;
1099 memset(&cc, 0, sizeof(struct tagcache_search_clause));
1100 cc.tag = csi->tagorder[i];
1101 cc.type = clause_is;
1102 cc.numeric = true;
1103 cc.numeric_data = csi->result_seek[i];
1104 tagcache_search_add_clause(&tcs, &cc);
1106 else
1108 tagcache_search_add_filter(&tcs, csi->tagorder[i],
1109 csi->result_seek[i]);
1113 for (i = 0; i <= level; i++)
1115 int j;
1117 for (j = 0; j < csi->clause_count[i]; j++)
1118 tagcache_search_add_clause(&tcs, csi->clause[i][j]);
1121 current_offset = offset;
1122 current_entry_count = 0;
1123 c->dirfull = false;
1125 fmt = NULL;
1126 for (i = 0; i < format_count; i++)
1128 if (formats[i]->group_id == csi->format_id[level])
1129 fmt = formats[i];
1132 if (fmt)
1134 sort_inverse = fmt->sort_inverse;
1135 sort_limit = fmt->limit;
1136 strip = fmt->strip;
1137 sort = true;
1139 else
1141 sort_inverse = false;
1142 sort_limit = 0;
1143 strip = 0;
1146 if (tag != tag_title && tag != tag_filename)
1148 if (offset == 0)
1150 dptr->newtable = ALLSUBENTRIES;
1151 dptr->name = str(LANG_TAGNAVI_ALL_TRACKS);
1152 dptr++;
1153 current_entry_count++;
1155 if (offset <= 1)
1157 dptr->newtable = NAVIBROWSE;
1158 dptr->name = str(LANG_TAGNAVI_RANDOM);
1159 dptr->extraseek = -1;
1160 dptr++;
1161 current_entry_count++;
1163 special_entry_count+=2;
1166 total_count += special_entry_count;
1168 while (tagcache_get_next(&tcs))
1170 if (total_count++ < offset)
1171 continue;
1173 dptr->newtable = NAVIBROWSE;
1174 if (tag == tag_title || tag == tag_filename)
1176 dptr->newtable = PLAYTRACK;
1177 dptr->extraseek = tcs.idx_id;
1179 else
1180 dptr->extraseek = tcs.result_seek;
1182 fmt = NULL;
1183 /* Check the format */
1184 for (i = 0; i < format_count; i++)
1186 if (formats[i]->group_id != csi->format_id[level])
1187 continue;
1189 if (tagcache_check_clauses(&tcs, formats[i]->clause,
1190 formats[i]->clause_count))
1192 fmt = formats[i];
1193 break;
1197 if (!tcs.ramresult || fmt)
1199 char buf[MAX_PATH];
1201 if (fmt)
1203 if (format_str(&tcs, fmt, buf, sizeof buf) < 0)
1205 logf("format_str() failed");
1206 tagcache_search_finish(&tcs);
1207 return 0;
1211 dptr->name = &c->name_buffer[namebufused];
1212 if (fmt)
1213 namebufused += strlen(buf)+1;
1214 else
1215 namebufused += tcs.result_len;
1217 if (namebufused >= c->name_buffer_size)
1219 logf("chunk mode #2: %d", current_entry_count);
1220 c->dirfull = true;
1221 sort = false;
1222 break ;
1224 if (fmt)
1225 strcpy(dptr->name, buf);
1226 else
1227 strcpy(dptr->name, tcs.result);
1229 else
1230 dptr->name = tcs.result;
1232 dptr++;
1233 current_entry_count++;
1235 if (current_entry_count >= global_settings.max_files_in_dir)
1237 logf("chunk mode #3: %d", current_entry_count);
1238 c->dirfull = true;
1239 sort = false;
1240 break ;
1243 if (init && !tcs.ramsearch)
1245 if (!show_search_progress(false, total_count))
1246 { /* user aborted */
1247 tagcache_search_finish(&tcs);
1248 return current_entry_count;
1253 if (sort)
1254 qsort(c->dircache + special_entry_count * c->dentry_size,
1255 current_entry_count - special_entry_count,
1256 c->dentry_size, compare);
1258 if (!init)
1260 tagcache_search_finish(&tcs);
1261 return current_entry_count;
1264 while (tagcache_get_next(&tcs))
1266 if (!tcs.ramsearch)
1268 if (!show_search_progress(false, total_count))
1269 break;
1271 total_count++;
1274 tagcache_search_finish(&tcs);
1276 if (!sort && (sort_inverse || sort_limit))
1278 splashf(HZ*4, ID2P(LANG_SHOWDIR_BUFFER_FULL), total_count);
1279 logf("Too small dir buffer");
1280 return 0;
1283 if (sort_limit)
1284 total_count = MIN(total_count, sort_limit);
1286 if (strip)
1288 dptr = c->dircache;
1289 for (i = 0; i < total_count; i++, dptr++)
1291 int len = strlen(dptr->name);
1293 if (len < strip)
1294 continue;
1296 dptr->name = &dptr->name[strip];
1300 return total_count;
1304 static int load_root(struct tree_context *c)
1306 struct tagentry *dptr = (struct tagentry *)c->dircache;
1307 int i;
1309 tc = c;
1310 c->currtable = ROOT;
1311 if (c->dirlevel == 0)
1312 c->currextra = rootmenu;
1314 menu = menus[c->currextra];
1315 if (menu == NULL)
1316 return 0;
1318 for (i = 0; i < menu->itemcount; i++)
1320 dptr->name = menu->items[i]->name;
1321 switch (menu->items[i]->type)
1323 case menu_next:
1324 dptr->newtable = NAVIBROWSE;
1325 dptr->extraseek = i;
1326 break;
1328 case menu_load:
1329 dptr->newtable = ROOT;
1330 dptr->extraseek = menu->items[i]->link;
1331 break;
1334 dptr++;
1337 current_offset = 0;
1338 current_entry_count = i;
1340 return i;
1343 int tagtree_load(struct tree_context* c)
1345 int count;
1346 int table = c->currtable;
1348 c->dentry_size = sizeof(struct tagentry);
1349 c->dirsindir = 0;
1351 if (!table)
1353 c->dirfull = false;
1354 table = ROOT;
1355 c->currtable = table;
1356 c->currextra = rootmenu;
1359 switch (table)
1361 case ROOT:
1362 count = load_root(c);
1363 break;
1365 case ALLSUBENTRIES:
1366 case NAVIBROWSE:
1367 logf("navibrowse...");
1368 cpu_boost(true);
1369 count = retrieve_entries(c, 0, true);
1370 cpu_boost(false);
1371 break;
1373 default:
1374 logf("Unsupported table %d\n", table);
1375 return -1;
1378 if (count < 0)
1380 c->dirlevel = 0;
1381 count = load_root(c);
1382 splash(HZ, str(LANG_TAGCACHE_BUSY));
1385 /* The _total_ numer of entries available. */
1386 c->dirlength = c->filesindir = count;
1388 return count;
1391 int tagtree_enter(struct tree_context* c)
1393 int rc = 0;
1394 struct tagentry *dptr;
1395 struct mp3entry *id3;
1396 int newextra;
1397 int seek;
1398 int source;
1400 dptr = tagtree_get_entry(c, c->selected_item);
1402 c->dirfull = false;
1403 seek = dptr->extraseek;
1404 if (seek == -1)
1406 if(c->filesindir<=2)
1407 return 0;
1408 srand(current_tick);
1409 dptr = (tagtree_get_entry(c, 2+(rand() % (c->filesindir-2))));
1410 seek = dptr->extraseek;
1412 newextra = dptr->newtable;
1414 if (c->dirlevel >= MAX_DIR_LEVELS)
1415 return 0;
1417 c->selected_item_history[c->dirlevel]=c->selected_item;
1418 c->table_history[c->dirlevel] = c->currtable;
1419 c->extra_history[c->dirlevel] = c->currextra;
1420 c->pos_history[c->dirlevel] = c->firstpos;
1421 c->dirlevel++;
1423 switch (c->currtable) {
1424 case ROOT:
1425 c->currextra = newextra;
1427 if (newextra == ROOT)
1429 menu = menus[seek];
1430 c->currextra = seek;
1433 else if (newextra == NAVIBROWSE)
1435 int i, j;
1437 csi = menu->items[seek]->si;
1438 c->currextra = 0;
1440 strlcpy(current_title[c->currextra], dptr->name,
1441 sizeof(current_title[0]));
1443 /* Read input as necessary. */
1444 for (i = 0; i < csi->tagorder_count; i++)
1446 for (j = 0; j < csi->clause_count[i]; j++)
1448 char* searchstring;
1449 source = csi->clause[i][j]->source;
1451 if (source == source_constant)
1452 continue;
1454 searchstring=csi->clause[i][j]->str;
1455 *searchstring = '\0';
1457 id3 = audio_current_track();
1459 if (source == source_current_path && id3)
1461 char *e;
1462 strlcpy(searchstring, id3->path, SEARCHSTR_SIZE);
1463 e = strrchr(searchstring, '/');
1464 if (e)
1465 *e = '\0';
1467 else if (source > source_runtime && id3)
1470 int k = source-source_runtime;
1471 int offset = id3_to_search_mapping[k].id3_offset;
1472 char **src = (char**)((char*)id3 + offset);
1473 if (*src)
1475 strlcpy(searchstring, *src, SEARCHSTR_SIZE);
1478 else
1480 rc = kbd_input(searchstring, SEARCHSTR_SIZE);
1481 if (rc < 0 || !searchstring[0])
1483 tagtree_exit(c);
1484 return 0;
1486 if (csi->clause[i][j]->numeric)
1487 csi->clause[i][j]->numeric_data = atoi(searchstring);
1494 c->currtable = newextra;
1496 break;
1498 case NAVIBROWSE:
1499 case ALLSUBENTRIES:
1500 if (newextra == PLAYTRACK)
1502 if (global_settings.party_mode && audio_status()) {
1503 splash(HZ, ID2P(LANG_PARTY_MODE));
1504 break;
1506 c->dirlevel--;
1507 /* about to create a new current playlist...
1508 allow user to cancel the operation */
1509 if (!warn_on_pl_erase())
1510 break;
1512 if (tagtree_play_folder(c) >= 0)
1513 rc = 2;
1514 break;
1517 c->currtable = newextra;
1518 csi->result_seek[c->currextra] = seek;
1519 if (c->currextra < csi->tagorder_count-1)
1520 c->currextra++;
1521 else
1522 c->dirlevel--;
1524 /* Update the statusbar title */
1525 strlcpy(current_title[c->currextra], dptr->name,
1526 sizeof(current_title[0]));
1527 break;
1529 default:
1530 c->dirlevel--;
1531 break;
1534 c->selected_item=0;
1535 gui_synclist_select_item(&tree_lists, c->selected_item);
1537 return rc;
1540 void tagtree_exit(struct tree_context* c)
1542 c->dirfull = false;
1543 if (c->dirlevel > 0)
1544 c->dirlevel--;
1545 c->selected_item=c->selected_item_history[c->dirlevel];
1546 gui_synclist_select_item(&tree_lists, c->selected_item);
1547 c->currtable = c->table_history[c->dirlevel];
1548 c->currextra = c->extra_history[c->dirlevel];
1549 c->firstpos = c->pos_history[c->dirlevel];
1552 int tagtree_get_filename(struct tree_context* c, char *buf, int buflen)
1554 struct tagcache_search tcs;
1555 struct tagentry *entry;
1557 entry = tagtree_get_entry(c, c->selected_item);
1559 if (!tagcache_search(&tcs, tag_filename))
1560 return -1;
1562 if (!tagcache_retrieve(&tcs, entry->extraseek, tcs.type, buf, buflen))
1564 tagcache_search_finish(&tcs);
1565 return -2;
1568 tagcache_search_finish(&tcs);
1570 return 0;
1573 static bool insert_all_playlist(struct tree_context *c, int position, bool queue)
1575 struct tagcache_search tcs;
1576 int i;
1577 char buf[MAX_PATH];
1578 int from, to, direction;
1579 int files_left = c->filesindir;
1581 cpu_boost(true);
1582 if (!tagcache_search(&tcs, tag_filename))
1584 splash(HZ, ID2P(LANG_TAGCACHE_BUSY));
1585 cpu_boost(false);
1586 return false;
1589 if (position == PLAYLIST_REPLACE)
1591 if (playlist_remove_all_tracks(NULL) == 0)
1592 position = PLAYLIST_INSERT_LAST;
1593 else
1595 cpu_boost(false);
1596 return false;
1600 if (position == PLAYLIST_INSERT_FIRST)
1602 from = c->filesindir - 1;
1603 to = -1;
1604 direction = -1;
1606 else
1608 from = 0;
1609 to = c->filesindir;
1610 direction = 1;
1613 for (i = from; i != to; i += direction)
1615 /* Count back to zero */
1616 if (!show_search_progress(false, files_left--))
1617 break;
1619 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, i)->extraseek,
1620 tcs.type, buf, sizeof buf))
1622 continue;
1625 if (playlist_insert_track(NULL, buf, position, queue, false) < 0)
1627 logf("playlist_insert_track failed");
1628 break;
1630 yield();
1632 playlist_sync(NULL);
1633 tagcache_search_finish(&tcs);
1634 cpu_boost(false);
1636 return true;
1639 bool tagtree_insert_selection_playlist(int position, bool queue)
1641 struct tagentry *dptr;
1642 char buf[MAX_PATH];
1643 int dirlevel = tc->dirlevel;
1645 show_search_progress(
1646 #ifdef HAVE_DISK_STORAGE
1647 storage_disk_is_active()
1648 #else
1649 true
1650 #endif
1651 , 0);
1654 /* We need to set the table to allsubentries. */
1655 dptr = tagtree_get_entry(tc, tc->selected_item);
1657 /* Insert a single track? */
1658 if (dptr->newtable == PLAYTRACK)
1660 if (tagtree_get_filename(tc, buf, sizeof buf) < 0)
1662 logf("tagtree_get_filename failed");
1663 return false;
1665 playlist_insert_track(NULL, buf, position, queue, true);
1667 return true;
1670 if (dptr->newtable == NAVIBROWSE)
1672 tagtree_enter(tc);
1673 tagtree_load(tc);
1674 dptr = tagtree_get_entry(tc, tc->selected_item);
1676 else if (dptr->newtable != ALLSUBENTRIES)
1678 logf("unsupported table: %d", dptr->newtable);
1679 return false;
1682 /* Now the current table should be allsubentries. */
1683 if (dptr->newtable != PLAYTRACK)
1685 tagtree_enter(tc);
1686 tagtree_load(tc);
1687 dptr = tagtree_get_entry(tc, tc->selected_item);
1689 /* And now the newtable should be playtrack. */
1690 if (dptr->newtable != PLAYTRACK)
1692 logf("newtable: %d !!", dptr->newtable);
1693 tc->dirlevel = dirlevel;
1694 return false;
1698 if (tc->filesindir <= 0)
1699 splash(HZ, ID2P(LANG_END_PLAYLIST));
1700 else
1702 logf("insert_all_playlist");
1703 if (!insert_all_playlist(tc, position, queue))
1704 splash(HZ*2, ID2P(LANG_FAILED));
1707 /* Finally return the dirlevel to its original value. */
1708 while (tc->dirlevel > dirlevel)
1709 tagtree_exit(tc);
1710 tagtree_load(tc);
1712 return true;
1715 static int tagtree_play_folder(struct tree_context* c)
1717 if (playlist_create(NULL, NULL) < 0)
1719 logf("Failed creating playlist\n");
1720 return -1;
1723 if (!insert_all_playlist(c, PLAYLIST_INSERT_LAST, false))
1724 return -2;
1726 if (global_settings.playlist_shuffle)
1727 c->selected_item = playlist_shuffle(current_tick, c->selected_item);
1728 if (!global_settings.play_selected)
1729 c->selected_item = 0;
1730 gui_synclist_select_item(&tree_lists, c->selected_item);
1732 playlist_start(c->selected_item,0);
1734 return 0;
1737 struct tagentry* tagtree_get_entry(struct tree_context *c, int id)
1739 struct tagentry *entry = (struct tagentry *)c->dircache;
1740 int realid = id - current_offset;
1742 /* Load the next chunk if necessary. */
1743 if (realid >= current_entry_count || realid < 0)
1745 cpu_boost(true);
1746 if (retrieve_entries(c, MAX(0, id - (current_entry_count / 2)),
1747 false) < 0)
1749 logf("retrieve failed");
1750 cpu_boost(false);
1751 return NULL;
1753 realid = id - current_offset;
1754 cpu_boost(false);
1757 return &entry[realid];
1760 char *tagtree_get_title(struct tree_context* c)
1762 switch (c->currtable)
1764 case ROOT:
1765 return menu->title;
1767 case NAVIBROWSE:
1768 case ALLSUBENTRIES:
1769 return current_title[c->currextra];
1772 return "?";
1775 int tagtree_get_attr(struct tree_context* c)
1777 int attr = -1;
1778 switch (c->currtable)
1780 case NAVIBROWSE:
1781 if (csi->tagorder[c->currextra] == tag_title)
1782 attr = FILE_ATTR_AUDIO;
1783 else
1784 attr = ATTR_DIRECTORY;
1785 break;
1787 case ALLSUBENTRIES:
1788 attr = FILE_ATTR_AUDIO;
1789 break;
1791 default:
1792 attr = ATTR_DIRECTORY;
1793 break;
1796 return attr;
1799 int tagtree_get_icon(struct tree_context* c)
1801 int icon = Icon_Folder;
1803 if (tagtree_get_attr(c) == FILE_ATTR_AUDIO)
1804 icon = Icon_Audio;
1806 return icon;