Get rid of unused return values, except the one from decode_chunk() which will be...
[kugel-rb.git] / apps / tagtree.c
blob7777a9f16d4595710048a2efb0457874fd1d23da
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.
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include "config.h"
30 #include "system.h"
31 #include "kernel.h"
32 #include "splash.h"
33 #include "icons.h"
34 #include "tree.h"
35 #include "action.h"
36 #include "settings.h"
37 #include "tagcache.h"
38 #include "tagtree.h"
39 #include "lang.h"
40 #include "logf.h"
41 #include "playlist.h"
42 #include "keyboard.h"
43 #include "gui/list.h"
44 #include "buffer.h"
45 #include "playback.h"
46 #include "yesno.h"
47 #include "misc.h"
48 #include "filetypes.h"
49 #include "audio.h"
50 #include "appevents.h"
51 #include "storage.h"
53 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
55 static int tagtree_play_folder(struct tree_context* c);
57 #define SEARCHSTR_SIZE 256
59 static const struct id3_to_search_mapping {
60 char *string;
61 size_t id3_offset;
62 } id3_to_search_mapping[] = {
63 { "", 0 }, /* offset n/a */
64 { "#directory#", 0 }, /* offset n/a */
65 { "#title#", offsetof(struct mp3entry, title) },
66 { "#artist#", offsetof(struct mp3entry, artist) },
67 { "#album#", offsetof(struct mp3entry, album) },
68 { "#genre#", offsetof(struct mp3entry, genre_string) },
69 { "#composer#", offsetof(struct mp3entry, composer) },
70 { "#albumartist#", offsetof(struct mp3entry, albumartist) },
72 enum variables {
73 var_sorttype = 100,
74 var_limit,
75 var_strip,
76 var_menu_start,
77 var_include,
78 var_rootmenu,
79 var_format,
80 menu_next,
81 menu_load,
84 /* Capacity 10 000 entries (for example 10k different artists) */
85 #define UNIQBUF_SIZE (64*1024)
86 static long *uniqbuf;
88 #define MAX_TAGS 5
89 #define MAX_MENU_ID_SIZE 32
91 static struct tagcache_search tcs, tcs2;
92 static bool sort_inverse;
95 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
97 * valid = true
98 * formatstr = "%-3d. %s"
99 * tags[0] = tag_autoscore
100 * tags[1] = tag_title
101 * tag_count = 2
103 * limit = 100
104 * sort_inverse = true
106 struct display_format {
107 char name[32];
108 struct tagcache_search_clause *clause[TAGCACHE_MAX_CLAUSES];
109 int clause_count;
110 char *formatstr;
111 int group_id;
112 int tags[MAX_TAGS];
113 int tag_count;
115 int limit;
116 int strip;
117 bool sort_inverse;
120 static struct display_format *formats[TAGMENU_MAX_FMTS];
121 static int format_count;
123 struct search_instruction {
124 char name[64];
125 int tagorder[MAX_TAGS];
126 int tagorder_count;
127 struct tagcache_search_clause *clause[MAX_TAGS][TAGCACHE_MAX_CLAUSES];
128 int format_id[MAX_TAGS];
129 int clause_count[MAX_TAGS];
130 int result_seek[MAX_TAGS];
133 struct menu_entry {
134 char name[64];
135 int type;
136 struct search_instruction *si;
137 int link;
140 struct root_menu {
141 char title[64];
142 char id[MAX_MENU_ID_SIZE];
143 int itemcount;
144 struct menu_entry *items[TAGMENU_MAX_ITEMS];
147 /* Statusbar text of the current view. */
148 static char current_title[MAX_TAGS][128];
150 static struct root_menu *menus[TAGMENU_MAX_MENUS];
151 static struct root_menu *menu;
152 static struct search_instruction *csi;
153 static const char *strp;
154 static int menu_count;
155 static int root_menu;
157 static int current_offset;
158 static int current_entry_count;
160 static int format_count;
161 static struct tree_context *tc;
163 static int get_token_str(char *buf, int size)
165 /* Find the start. */
166 while (*strp != '"' && *strp != '\0')
167 strp++;
169 if (*strp == '\0' || *(++strp) == '\0')
170 return -1;
172 /* Read the data. */
173 while (*strp != '"' && *strp != '\0' && --size > 0)
174 *(buf++) = *(strp++);
176 *buf = '\0';
177 if (*strp != '"')
178 return -2;
180 strp++;
182 return 0;
185 #define MATCH(tag,str1,str2,settag) \
186 if (!strcasecmp(str1, str2)) { \
187 *tag = settag; \
188 return 1; \
191 static int get_tag(int *tag)
193 char buf[128];
194 int i;
196 /* Find the start. */
197 while ((*strp == ' ' || *strp == '>') && *strp != '\0')
198 strp++;
200 if (*strp == '\0' || *strp == '?')
201 return 0;
203 for (i = 0; i < (int)sizeof(buf)-1; i++)
205 if (*strp == '\0' || *strp == ' ')
206 break ;
207 buf[i] = *strp;
208 strp++;
210 buf[i] = '\0';
212 MATCH(tag, buf, "album", tag_album);
213 MATCH(tag, buf, "artist", tag_artist);
214 MATCH(tag, buf, "bitrate", tag_bitrate);
215 MATCH(tag, buf, "composer", tag_composer);
216 MATCH(tag, buf, "comment", tag_comment);
217 MATCH(tag, buf, "albumartist", tag_albumartist);
218 MATCH(tag, buf, "ensemble", tag_albumartist);
219 MATCH(tag, buf, "grouping", tag_grouping);
220 MATCH(tag, buf, "genre", tag_genre);
221 MATCH(tag, buf, "length", tag_length);
222 MATCH(tag, buf, "Lm", tag_virt_length_min);
223 MATCH(tag, buf, "Ls", tag_virt_length_sec);
224 MATCH(tag, buf, "Pm", tag_virt_playtime_min);
225 MATCH(tag, buf, "Ps", tag_virt_playtime_sec);
226 MATCH(tag, buf, "title", tag_title);
227 MATCH(tag, buf, "filename", tag_filename);
228 MATCH(tag, buf, "tracknum", tag_tracknumber);
229 MATCH(tag, buf, "discnum", tag_discnumber);
230 MATCH(tag, buf, "year", tag_year);
231 MATCH(tag, buf, "playcount", tag_playcount);
232 MATCH(tag, buf, "rating", tag_rating);
233 MATCH(tag, buf, "lastplayed", tag_lastplayed);
234 MATCH(tag, buf, "commitid", tag_commitid);
235 MATCH(tag, buf, "entryage", tag_virt_entryage);
236 MATCH(tag, buf, "autoscore", tag_virt_autoscore);
237 MATCH(tag, buf, "%sort", var_sorttype);
238 MATCH(tag, buf, "%limit", var_limit);
239 MATCH(tag, buf, "%strip", var_strip);
240 MATCH(tag, buf, "%menu_start", var_menu_start);
241 MATCH(tag, buf, "%include", var_include);
242 MATCH(tag, buf, "%root_menu", var_rootmenu);
243 MATCH(tag, buf, "%format", var_format);
244 MATCH(tag, buf, "->", menu_next);
245 MATCH(tag, buf, "==>", menu_load);
247 logf("NO MATCH: %s\n", buf);
248 if (buf[0] == '?')
249 return 0;
251 return -1;
254 static int get_clause(int *condition)
256 char buf[4];
257 int i;
259 /* Find the start. */
260 while (*strp == ' ' && *strp != '\0')
261 strp++;
263 if (*strp == '\0')
264 return 0;
266 for (i = 0; i < (int)sizeof(buf)-1; i++)
268 if (*strp == '\0' || *strp == ' ')
269 break ;
270 buf[i] = *strp;
271 strp++;
273 buf[i] = '\0';
275 MATCH(condition, buf, "=", clause_is);
276 MATCH(condition, buf, "==", clause_is);
277 MATCH(condition, buf, "!=", clause_is_not);
278 MATCH(condition, buf, ">", clause_gt);
279 MATCH(condition, buf, ">=", clause_gteq);
280 MATCH(condition, buf, "<", clause_lt);
281 MATCH(condition, buf, "<=", clause_lteq);
282 MATCH(condition, buf, "~", clause_contains);
283 MATCH(condition, buf, "!~", clause_not_contains);
284 MATCH(condition, buf, "^", clause_begins_with);
285 MATCH(condition, buf, "!^", clause_not_begins_with);
286 MATCH(condition, buf, "$", clause_ends_with);
287 MATCH(condition, buf, "!$", clause_not_ends_with);
288 MATCH(condition, buf, "@", clause_oneof);
290 return 0;
293 static bool read_clause(struct tagcache_search_clause *clause)
295 char buf[SEARCHSTR_SIZE];
296 unsigned int i;
298 if (get_tag(&clause->tag) <= 0)
299 return false;
301 if (get_clause(&clause->type) <= 0)
302 return false;
304 if (get_token_str(buf, sizeof buf) < 0)
305 return false;
307 for (i=0; i<ARRAYLEN(id3_to_search_mapping); i++)
309 if (!strcasecmp(buf, id3_to_search_mapping[i].string))
310 break;
313 if (i<ARRAYLEN(id3_to_search_mapping)) /* runtime search operand found */
315 clause->source = source_runtime+i;
316 clause->str = buffer_alloc(SEARCHSTR_SIZE);
318 else
320 clause->source = source_constant;
321 clause->str = buffer_alloc(strlen(buf)+1);
322 strcpy(clause->str, buf);
325 if (tagcache_is_numeric_tag(clause->tag))
327 clause->numeric = true;
328 clause->numeric_data = atoi(clause->str);
330 else
331 clause->numeric = false;
333 logf("got clause: %d/%d [%s]", clause->tag, clause->type, clause->str);
335 return true;
338 static bool read_variable(char *buf, int size)
340 int condition;
342 if (!get_clause(&condition))
343 return false;
345 if (condition != clause_is)
346 return false;
348 if (get_token_str(buf, size) < 0)
349 return false;
351 return true;
354 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
355 static int get_format_str(struct display_format *fmt)
357 int ret;
358 char buf[128];
359 int i;
361 memset(fmt, 0, sizeof(struct display_format));
363 if (get_token_str(fmt->name, sizeof fmt->name) < 0)
364 return -10;
366 /* Determine the group id */
367 fmt->group_id = 0;
368 for (i = 0; i < format_count; i++)
370 if (!strcasecmp(formats[i]->name, fmt->name))
372 fmt->group_id = formats[i]->group_id;
373 break;
376 if (formats[i]->group_id > fmt->group_id)
377 fmt->group_id = formats[i]->group_id;
380 if (i == format_count)
381 fmt->group_id++;
383 logf("format: (%d) %s", fmt->group_id, fmt->name);
385 if (get_token_str(buf, sizeof buf) < 0)
386 return -10;
388 fmt->formatstr = buffer_alloc(strlen(buf) + 1);
389 strcpy(fmt->formatstr, buf);
391 while (fmt->tag_count < MAX_TAGS)
393 ret = get_tag(&fmt->tags[fmt->tag_count]);
394 if (ret < 0)
395 return -11;
397 if (ret == 0)
398 break;
400 switch (fmt->tags[fmt->tag_count]) {
401 case var_sorttype:
402 if (!read_variable(buf, sizeof buf))
403 return -12;
404 if (!strcasecmp("inverse", buf))
405 fmt->sort_inverse = true;
406 break;
408 case var_limit:
409 if (!read_variable(buf, sizeof buf))
410 return -13;
411 fmt->limit = atoi(buf);
412 break;
414 case var_strip:
415 if (!read_variable(buf, sizeof buf))
416 return -14;
417 fmt->strip = atoi(buf);
418 break;
420 default:
421 fmt->tag_count++;
425 return 1;
428 static int add_format(const char *buf)
430 strp = buf;
432 if (formats[format_count] == NULL)
433 formats[format_count] = buffer_alloc(sizeof(struct display_format));
435 memset(formats[format_count], 0, sizeof(struct display_format));
436 if (get_format_str(formats[format_count]) < 0)
438 logf("get_format_str() parser failed!");
439 return -4;
442 while (*strp != '\0' && *strp != '?')
443 strp++;
445 if (*strp == '?')
447 int clause_count = 0;
448 strp++;
450 while (1)
452 if (clause_count >= TAGCACHE_MAX_CLAUSES)
454 logf("too many clauses");
455 break;
458 formats[format_count]->clause[clause_count] =
459 buffer_alloc(sizeof(struct tagcache_search_clause));
461 if (!read_clause(formats[format_count]->clause[clause_count]))
462 break;
464 clause_count++;
467 formats[format_count]->clause_count = clause_count;
470 format_count++;
472 return 1;
475 static int get_condition(struct search_instruction *inst)
477 int clause_count;
478 char buf[128];
480 switch (*strp)
482 case '=':
484 int i;
486 if (get_token_str(buf, sizeof buf) < 0)
487 return -1;
489 for (i = 0; i < format_count; i++)
491 if (!strcasecmp(formats[i]->name, buf))
492 break;
495 if (i == format_count)
497 logf("format not found: %s", buf);
498 return -2;
501 inst->format_id[inst->tagorder_count] = formats[i]->group_id;
502 return 1;
504 case '?':
505 case ' ':
506 case '&':
507 strp++;
508 return 1;
509 case '-':
510 case '\0':
511 return 0;
514 clause_count = inst->clause_count[inst->tagorder_count];
515 if (clause_count >= TAGCACHE_MAX_CLAUSES)
517 logf("Too many clauses");
518 return false;
521 inst->clause[inst->tagorder_count][clause_count] =
522 buffer_alloc(sizeof(struct tagcache_search_clause));
524 if (!read_clause(inst->clause[inst->tagorder_count][clause_count]))
525 return -1;
527 inst->clause_count[inst->tagorder_count]++;
529 return 1;
532 /* example search:
533 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
534 * : album ? year >= "2000" : songs
535 * ^ begins with
536 * * contains
537 * $ ends with
540 static bool parse_search(struct menu_entry *entry, const char *str)
542 int ret;
543 int type;
544 struct search_instruction *inst = entry->si;
545 char buf[MAX_PATH];
546 int i;
547 struct root_menu *new_menu;
549 strp = str;
551 /* Parse entry name */
552 if (get_token_str(entry->name, sizeof entry->name) < 0)
554 logf("No name found.");
555 return false;
558 /* Parse entry type */
559 if (get_tag(&entry->type) <= 0)
560 return false;
562 if (entry->type == menu_load)
564 if (get_token_str(buf, sizeof buf) < 0)
565 return false;
567 /* Find the matching root menu or "create" it */
568 for (i = 0; i < menu_count; i++)
570 if (!strcasecmp(menus[i]->id, buf))
572 entry->link = i;
573 return true;
577 if (menu_count >= TAGMENU_MAX_MENUS)
579 logf("max menucount reached");
580 return false;
583 /* Allocate a new menu unless link is found. */
584 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
585 new_menu = menus[menu_count];
586 memset(new_menu, 0, sizeof(struct root_menu));
587 strncpy(new_menu->id, buf, MAX_MENU_ID_SIZE-1);
588 entry->link = menu_count;
589 ++menu_count;
591 return true;
594 if (entry->type != menu_next)
595 return false;
597 while (inst->tagorder_count < MAX_TAGS)
599 ret = get_tag(&inst->tagorder[inst->tagorder_count]);
600 if (ret < 0)
602 logf("Parse error #1");
603 logf("%s", strp);
604 return false;
607 if (ret == 0)
608 break ;
610 logf("tag: %d", inst->tagorder[inst->tagorder_count]);
612 while ( (ret = get_condition(inst)) > 0 ) ;
613 if (ret < 0)
614 return false;
616 inst->tagorder_count++;
618 if (get_tag(&type) <= 0 || type != menu_next)
619 break;
622 return true;
625 static int compare(const void *p1, const void *p2)
627 struct tagentry *e1 = (struct tagentry *)p1;
628 struct tagentry *e2 = (struct tagentry *)p2;
630 if (sort_inverse)
631 return strncasecmp(e2->name, e1->name, MAX_PATH);
633 return strncasecmp(e1->name, e2->name, MAX_PATH);
636 static void tagtree_buffer_event(struct mp3entry *id3)
638 /* Do not gather data unless proper setting has been enabled. */
639 if (!global_settings.runtimedb)
640 return;
642 logf("be:%s", id3->path);
644 if (!tagcache_find_index(&tcs, id3->path))
646 logf("tc stat: not found: %s", id3->path);
647 return;
650 id3->playcount = tagcache_get_numeric(&tcs, tag_playcount);
651 if (!id3->rating)
652 id3->rating = tagcache_get_numeric(&tcs, tag_rating);
653 id3->lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
654 id3->score = tagcache_get_numeric(&tcs, tag_virt_autoscore) / 10;
655 id3->playtime = tagcache_get_numeric(&tcs, tag_playtime);
657 /* Store our tagcache index pointer. */
658 id3->tagcache_idx = tcs.idx_id+1;
660 tagcache_search_finish(&tcs);
663 static void tagtree_track_finish_event(struct mp3entry *id3)
665 long playcount;
666 long playtime;
667 long lastplayed;
668 long tagcache_idx;
670 /* Do not gather data unless proper setting has been enabled. */
671 if (!global_settings.runtimedb)
673 logf("runtimedb gathering not enabled");
674 return;
677 tagcache_idx=id3->tagcache_idx;
678 if (!tagcache_idx)
680 logf("No tagcache index pointer found");
681 return;
683 tagcache_idx--;
685 /* Don't process unplayed tracks. */
686 if (id3->elapsed == 0)
688 logf("not logging unplayed track");
689 return;
692 playcount = id3->playcount + 1;
693 lastplayed = tagcache_increase_serial();
694 if (lastplayed < 0)
696 logf("incorrect tc serial:%ld", lastplayed);
697 return;
700 /* Ignore the last 15s (crossfade etc.) */
701 playtime = id3->playtime + MIN(id3->length, id3->elapsed + 15 * 1000);
703 logf("ube:%s", id3->path);
704 logf("-> %ld/%ld", playcount, playtime);
705 logf("-> %ld/%ld/%ld", id3->elapsed, id3->length, MIN(id3->length, id3->elapsed + 15 * 1000));
707 /* Queue the updates to the tagcache system. */
708 tagcache_update_numeric(tagcache_idx, tag_playcount, playcount);
709 tagcache_update_numeric(tagcache_idx, tag_playtime, playtime);
710 tagcache_update_numeric(tagcache_idx, tag_lastplayed, lastplayed);
713 bool tagtree_export(void)
715 splash(0, str(LANG_CREATING));
716 if (!tagcache_create_changelog(&tcs))
718 splash(HZ*2, ID2P(LANG_FAILED));
721 return false;
724 bool tagtree_import(void)
726 splash(0, ID2P(LANG_WAIT));
727 if (!tagcache_import_changelog())
729 splash(HZ*2, ID2P(LANG_FAILED));
732 return false;
735 static bool parse_menu(const char *filename);
737 static int parse_line(int n, const char *buf, void *parameters)
739 char data[256];
740 int variable;
741 static bool read_menu;
742 int i;
744 (void)parameters;
746 logf("parse:%d/%s", n, buf);
748 /* First line, do initialisation. */
749 if (n == 0)
751 if (strcasecmp(TAGNAVI_VERSION, buf))
753 logf("Version mismatch");
754 return -1;
757 read_menu = false;
760 if (buf[0] == '#')
761 return 0;
763 if (buf[0] == '\0')
765 if (read_menu)
767 /* End the menu */
768 read_menu = false;
770 return 0;
773 if (!read_menu)
775 strp = buf;
776 if (get_tag(&variable) <= 0)
777 return 0;
779 switch (variable)
781 case var_format:
782 if (add_format(strp) < 0)
784 logf("Format add fail: %s", data);
786 break;
788 case var_include:
789 if (get_token_str(data, sizeof(data)) < 0)
791 logf("%%include empty");
792 return 0;
795 if (!parse_menu(data))
797 logf("Load menu fail: %s", data);
799 break;
801 case var_menu_start:
802 if (menu_count >= TAGMENU_MAX_MENUS)
804 logf("max menucount reached");
805 return 0;
808 if (get_token_str(data, sizeof data) < 0)
810 logf("%%menu_start id empty");
811 return 0;
814 menu = NULL;
815 for (i = 0; i < menu_count; i++)
817 if (!strcasecmp(menus[i]->id, data))
819 menu = menus[i];
823 if (menu == NULL)
825 menus[menu_count] = buffer_alloc(sizeof(struct root_menu));
826 menu = menus[menu_count];
827 ++menu_count;
828 memset(menu, 0, sizeof(struct root_menu));
829 strncpy(menu->id, data, MAX_MENU_ID_SIZE-1);
832 if (get_token_str(menu->title, sizeof(menu->title)) < 0)
834 logf("%%menu_start title empty");
835 return 0;
837 logf("menu: %s", menu->title);
838 read_menu = true;
839 break;
841 case var_rootmenu:
842 /* Only set root menu once. */
843 if (root_menu >= 0)
844 break;
846 if (get_token_str(data, sizeof(data)) < 0)
848 logf("%%root_menu empty");
849 return 0;
852 for (i = 0; i < menu_count; i++)
854 if (!strcasecmp(menus[i]->id, data))
856 root_menu = i;
859 break;
862 return 0;
865 if (menu->itemcount >= TAGMENU_MAX_ITEMS)
867 logf("max itemcount reached");
868 return 0;
871 /* Allocate */
872 if (menu->items[menu->itemcount] == NULL)
874 menu->items[menu->itemcount] = buffer_alloc(sizeof(struct menu_entry));
875 memset(menu->items[menu->itemcount], 0, sizeof(struct menu_entry));
876 menu->items[menu->itemcount]->si = buffer_alloc(sizeof(struct search_instruction));
879 memset(menu->items[menu->itemcount]->si, 0, sizeof(struct search_instruction));
880 if (!parse_search(menu->items[menu->itemcount], buf))
881 return 0;
883 menu->itemcount++;
885 return 0;
888 static bool parse_menu(const char *filename)
890 int fd;
891 char buf[1024];
893 if (menu_count >= TAGMENU_MAX_MENUS)
895 logf("max menucount reached");
896 return false;
899 fd = open(filename, O_RDONLY);
900 if (fd < 0)
902 logf("Search instruction file not found.");
903 return false;
906 /* Now read file for real, parsing into si */
907 fast_readline(fd, buf, sizeof buf, NULL, parse_line);
908 close(fd);
910 return true;
913 void tagtree_init(void)
915 format_count = 0;
916 menu_count = 0;
917 menu = NULL;
918 root_menu = -1;
919 parse_menu(FILE_SEARCH_INSTRUCTIONS);
921 /* If no root menu is set, assume it's the first single menu
922 * we have. That shouldn't normally happen. */
923 if (root_menu < 0)
924 root_menu = 0;
926 uniqbuf = buffer_alloc(UNIQBUF_SIZE);
928 add_event(PLAYBACK_EVENT_TRACK_BUFFER, false, tagtree_buffer_event);
929 add_event(PLAYBACK_EVENT_TRACK_FINISH, false, tagtree_track_finish_event);
932 static bool show_search_progress(bool init, int count)
934 static int last_tick = 0;
936 /* Don't show splashes for 1/2 second after starting search */
937 if (init)
939 last_tick = current_tick + HZ/2;
940 return true;
943 /* Update progress every 1/10 of a second */
944 if (current_tick - last_tick > HZ/10)
946 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG), count, str(LANG_OFF_ABORT));
947 if (action_userabort(TIMEOUT_NOBLOCK))
948 return false;
949 last_tick = current_tick;
950 yield();
953 return true;
956 static int format_str(struct tagcache_search *tcs, struct display_format *fmt,
957 char *buf, int buf_size)
959 char fmtbuf[8];
960 bool read_format = false;
961 int fmtbuf_pos = 0;
962 int parpos = 0;
963 int buf_pos = 0;
964 int i;
966 memset(buf, 0, buf_size);
967 for (i = 0; fmt->formatstr[i] != '\0'; i++)
969 if (fmt->formatstr[i] == '%')
971 read_format = true;
972 fmtbuf_pos = 0;
973 if (parpos >= fmt->tag_count)
975 logf("too many format tags");
976 return -1;
980 if (read_format)
982 fmtbuf[fmtbuf_pos++] = fmt->formatstr[i];
983 if (fmtbuf_pos >= buf_size)
985 logf("format parse error");
986 return -2;
989 if (fmt->formatstr[i] == 's')
991 fmtbuf[fmtbuf_pos] = '\0';
992 read_format = false;
993 if (fmt->tags[parpos] == tcs->type)
995 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf, tcs->result);
997 else
999 /* Need to fetch the tag data. */
1000 if (!tagcache_retrieve(tcs, tcs->idx_id, fmt->tags[parpos],
1001 &buf[buf_pos], buf_size - buf_pos))
1003 logf("retrieve failed");
1004 return -3;
1007 buf_pos += strlen(&buf[buf_pos]);
1008 parpos++;
1010 else if (fmt->formatstr[i] == 'd')
1012 fmtbuf[fmtbuf_pos] = '\0';
1013 read_format = false;
1014 snprintf(&buf[buf_pos], buf_size - buf_pos, fmtbuf,
1015 tagcache_get_numeric(tcs, fmt->tags[parpos]));
1016 buf_pos += strlen(&buf[buf_pos]);
1017 parpos++;
1019 continue;
1022 buf[buf_pos++] = fmt->formatstr[i];
1024 if (buf_pos - 1 >= buf_size)
1026 logf("buffer overflow");
1027 return -4;
1031 buf[buf_pos++] = '\0';
1033 return 0;
1036 static int retrieve_entries(struct tree_context *c, struct tagcache_search *tcs,
1037 int offset, bool init)
1039 struct tagentry *dptr = (struct tagentry *)c->dircache;
1040 struct display_format *fmt;
1041 int i;
1042 int namebufused = 0;
1043 int total_count = 0;
1044 int special_entry_count = 0;
1045 int level = c->currextra;
1046 int tag;
1047 bool sort = false;
1048 int sort_limit;
1049 int strip;
1051 if (init
1052 #ifdef HAVE_TC_RAMCACHE
1053 && !tagcache_is_ramcache()
1054 #endif
1057 /* Show search progress straight away if the disk needs to spin up,
1058 otherwise show it after the normal 1/2 second delay */
1059 show_search_progress(
1060 #ifdef HAVE_DISK_STORAGE
1061 storage_disk_is_active()
1062 #else
1063 true
1064 #endif
1065 , 0);
1068 if (c->currtable == allsubentries)
1070 tag = tag_title;
1071 level--;
1073 else
1074 tag = csi->tagorder[level];
1076 if (!tagcache_search(tcs, tag))
1077 return -1;
1079 /* Prevent duplicate entries in the search list. */
1080 tagcache_search_set_uniqbuf(tcs, uniqbuf, UNIQBUF_SIZE);
1082 if (level || csi->clause_count[0] || tagcache_is_numeric_tag(tag))
1083 sort = true;
1085 for (i = 0; i < level; i++)
1087 if (tagcache_is_numeric_tag(csi->tagorder[i]))
1089 static struct tagcache_search_clause cc;
1091 memset(&cc, 0, sizeof(struct tagcache_search_clause));
1092 cc.tag = csi->tagorder[i];
1093 cc.type = clause_is;
1094 cc.numeric = true;
1095 cc.numeric_data = csi->result_seek[i];
1096 tagcache_search_add_clause(tcs, &cc);
1098 else
1100 tagcache_search_add_filter(tcs, csi->tagorder[i],
1101 csi->result_seek[i]);
1105 for (i = 0; i <= level; i++)
1107 int j;
1109 for (j = 0; j < csi->clause_count[i]; j++)
1110 tagcache_search_add_clause(tcs, csi->clause[i][j]);
1113 current_offset = offset;
1114 current_entry_count = 0;
1115 c->dirfull = false;
1117 fmt = NULL;
1118 for (i = 0; i < format_count; i++)
1120 if (formats[i]->group_id == csi->format_id[level])
1121 fmt = formats[i];
1124 if (fmt)
1126 sort_inverse = fmt->sort_inverse;
1127 sort_limit = fmt->limit;
1128 strip = fmt->strip;
1129 sort = true;
1131 else
1133 sort_inverse = false;
1134 sort_limit = 0;
1135 strip = 0;
1138 if (tag != tag_title && tag != tag_filename)
1140 if (offset == 0)
1142 dptr->newtable = allsubentries;
1143 dptr->name = str(LANG_TAGNAVI_ALL_TRACKS);
1144 dptr++;
1145 current_entry_count++;
1147 if (offset <= 1)
1149 dptr->newtable = navibrowse;
1150 dptr->name = str(LANG_TAGNAVI_RANDOM);
1151 dptr->extraseek = -1;
1152 dptr++;
1153 current_entry_count++;
1155 special_entry_count+=2;
1158 total_count += special_entry_count;
1160 while (tagcache_get_next(tcs))
1162 if (total_count++ < offset)
1163 continue;
1165 dptr->newtable = navibrowse;
1166 if (tag == tag_title || tag == tag_filename)
1168 dptr->newtable = playtrack;
1169 dptr->extraseek = tcs->idx_id;
1171 else
1172 dptr->extraseek = tcs->result_seek;
1174 fmt = NULL;
1175 /* Check the format */
1176 for (i = 0; i < format_count; i++)
1178 if (formats[i]->group_id != csi->format_id[level])
1179 continue;
1181 if (tagcache_check_clauses(tcs, formats[i]->clause,
1182 formats[i]->clause_count))
1184 fmt = formats[i];
1185 break;
1189 if (!tcs->ramresult || fmt)
1191 char buf[MAX_PATH];
1193 if (fmt)
1195 if (format_str(tcs, fmt, buf, sizeof buf) < 0)
1197 logf("format_str() failed");
1198 return 0;
1202 dptr->name = &c->name_buffer[namebufused];
1203 if (fmt)
1204 namebufused += strlen(buf)+1;
1205 else
1206 namebufused += tcs->result_len;
1208 if (namebufused >= c->name_buffer_size)
1210 logf("chunk mode #2: %d", current_entry_count);
1211 c->dirfull = true;
1212 sort = false;
1213 break ;
1215 if (fmt)
1216 strcpy(dptr->name, buf);
1217 else
1218 strcpy(dptr->name, tcs->result);
1220 else
1221 dptr->name = tcs->result;
1223 dptr++;
1224 current_entry_count++;
1226 if (current_entry_count >= global_settings.max_files_in_dir)
1228 logf("chunk mode #3: %d", current_entry_count);
1229 c->dirfull = true;
1230 sort = false;
1231 break ;
1234 if (init && !tcs->ramsearch)
1236 if (!show_search_progress(false, total_count))
1238 tagcache_search_finish(tcs);
1239 return current_entry_count;
1244 if (sort)
1245 qsort(c->dircache + special_entry_count * c->dentry_size,
1246 current_entry_count - special_entry_count,
1247 c->dentry_size, compare);
1249 if (!init)
1251 tagcache_search_finish(tcs);
1252 return current_entry_count;
1255 while (tagcache_get_next(tcs))
1257 if (!tcs->ramsearch)
1259 if (!show_search_progress(false, total_count))
1260 break;
1262 total_count++;
1265 tagcache_search_finish(tcs);
1267 if (!sort && (sort_inverse || sort_limit))
1269 splashf(HZ*4, ID2P(LANG_SHOWDIR_BUFFER_FULL), total_count);
1270 logf("Too small dir buffer");
1271 return 0;
1274 if (sort_limit)
1275 total_count = MIN(total_count, sort_limit);
1277 if (strip)
1279 dptr = c->dircache;
1280 for (i = 0; i < total_count; i++, dptr++)
1282 int len = strlen(dptr->name);
1284 if (len < strip)
1285 continue;
1287 dptr->name = &dptr->name[strip];
1291 return total_count;
1294 static int load_root(struct tree_context *c)
1296 struct tagentry *dptr = (struct tagentry *)c->dircache;
1297 int i;
1299 tc = c;
1300 c->currtable = root;
1301 if (c->dirlevel == 0)
1302 c->currextra = root_menu;
1304 menu = menus[c->currextra];
1305 if (menu == NULL)
1306 return 0;
1308 for (i = 0; i < menu->itemcount; i++)
1310 dptr->name = menu->items[i]->name;
1311 switch (menu->items[i]->type)
1313 case menu_next:
1314 dptr->newtable = navibrowse;
1315 dptr->extraseek = i;
1316 break;
1318 case menu_load:
1319 dptr->newtable = root;
1320 dptr->extraseek = menu->items[i]->link;
1321 break;
1324 dptr++;
1327 current_offset = 0;
1328 current_entry_count = i;
1330 return i;
1333 int tagtree_load(struct tree_context* c)
1335 int count;
1336 int table = c->currtable;
1338 c->dentry_size = sizeof(struct tagentry);
1339 c->dirsindir = 0;
1341 if (!table)
1343 c->dirfull = false;
1344 table = root;
1345 c->currtable = table;
1346 c->currextra = root_menu;
1349 switch (table)
1351 case root:
1352 count = load_root(c);
1353 break;
1355 case allsubentries:
1356 case navibrowse:
1357 logf("navibrowse...");
1358 cpu_boost(true);
1359 count = retrieve_entries(c, &tcs, 0, true);
1360 cpu_boost(false);
1361 break;
1363 default:
1364 logf("Unsupported table %d\n", table);
1365 return -1;
1368 if (count < 0)
1370 c->dirlevel = 0;
1371 count = load_root(c);
1372 splash(HZ, str(LANG_TAGCACHE_BUSY));
1375 /* The _total_ numer of entries available. */
1376 c->dirlength = c->filesindir = count;
1378 return count;
1381 int tagtree_enter(struct tree_context* c)
1383 int rc = 0;
1384 struct tagentry *dptr;
1385 struct mp3entry *id3;
1386 int newextra;
1387 int seek;
1388 int source;
1390 dptr = tagtree_get_entry(c, c->selected_item);
1392 c->dirfull = false;
1393 seek = dptr->extraseek;
1394 if (seek == -1)
1396 if(c->filesindir<=2)
1397 return 0;
1398 srand(current_tick);
1399 dptr = (tagtree_get_entry(c, 2+(rand() % (c->filesindir-2))));
1400 seek = dptr->extraseek;
1402 newextra = dptr->newtable;
1404 if (c->dirlevel >= MAX_DIR_LEVELS)
1405 return 0;
1407 c->selected_item_history[c->dirlevel]=c->selected_item;
1408 c->table_history[c->dirlevel] = c->currtable;
1409 c->extra_history[c->dirlevel] = c->currextra;
1410 c->pos_history[c->dirlevel] = c->firstpos;
1411 c->dirlevel++;
1413 switch (c->currtable) {
1414 case root:
1415 c->currextra = newextra;
1417 if (newextra == root)
1419 menu = menus[seek];
1420 c->currextra = seek;
1423 else if (newextra == navibrowse)
1425 int i, j;
1427 csi = menu->items[seek]->si;
1428 c->currextra = 0;
1430 strncpy(current_title[c->currextra], dptr->name,
1431 sizeof(current_title[0]) - 1);
1433 /* Read input as necessary. */
1434 for (i = 0; i < csi->tagorder_count; i++)
1436 for (j = 0; j < csi->clause_count[i]; j++)
1438 char* searchstring;
1439 source = csi->clause[i][j]->source;
1441 if (source == source_constant)
1442 continue;
1444 searchstring=csi->clause[i][j]->str;
1445 *searchstring = '\0';
1447 id3 = audio_current_track();
1449 if (source == source_current_path && id3)
1451 char *e;
1452 strncpy(searchstring, id3->path, SEARCHSTR_SIZE);
1453 e = strrchr(searchstring, '/');
1454 if (e)
1455 *e = '\0';
1457 else if (source > source_runtime && id3)
1460 int k = source-source_runtime;
1461 int offset = id3_to_search_mapping[k].id3_offset;
1462 char **src = (char**)((char*)id3 + offset);
1463 if (*src)
1465 strncpy(searchstring, *src, SEARCHSTR_SIZE);
1466 searchstring[SEARCHSTR_SIZE-1] = '\0';
1469 else
1471 rc = kbd_input(searchstring, SEARCHSTR_SIZE);
1472 if (rc == -1 || !searchstring[0])
1474 tagtree_exit(c);
1475 return 0;
1477 if (csi->clause[i][j]->numeric)
1478 csi->clause[i][j]->numeric_data = atoi(searchstring);
1485 c->currtable = newextra;
1487 break;
1489 case navibrowse:
1490 case allsubentries:
1491 if (newextra == playtrack)
1493 if (global_settings.party_mode && audio_status()) {
1494 splash(HZ, ID2P(LANG_PARTY_MODE));
1495 break;
1497 c->dirlevel--;
1498 /* about to create a new current playlist...
1499 allow user to cancel the operation */
1500 if (!warn_on_pl_erase())
1501 break;
1503 if (tagtree_play_folder(c) >= 0)
1504 rc = 2;
1505 break;
1508 c->currtable = newextra;
1509 csi->result_seek[c->currextra] = seek;
1510 if (c->currextra < csi->tagorder_count-1)
1511 c->currextra++;
1512 else
1513 c->dirlevel--;
1515 /* Update the statusbar title */
1516 strncpy(current_title[c->currextra], dptr->name,
1517 sizeof(current_title[0]) - 1);
1518 break;
1520 default:
1521 c->dirlevel--;
1522 break;
1525 c->selected_item=0;
1526 gui_synclist_select_item(&tree_lists, c->selected_item);
1528 return rc;
1531 void tagtree_exit(struct tree_context* c)
1533 c->dirfull = false;
1534 if (c->dirlevel > 0)
1535 c->dirlevel--;
1536 c->selected_item=c->selected_item_history[c->dirlevel];
1537 gui_synclist_select_item(&tree_lists, c->selected_item);
1538 c->currtable = c->table_history[c->dirlevel];
1539 c->currextra = c->extra_history[c->dirlevel];
1540 c->firstpos = c->pos_history[c->dirlevel];
1543 int tagtree_get_filename(struct tree_context* c, char *buf, int buflen)
1545 struct tagentry *entry;
1547 entry = tagtree_get_entry(c, c->selected_item);
1549 if (!tagcache_search(&tcs, tag_filename))
1550 return -1;
1552 if (!tagcache_retrieve(&tcs, entry->extraseek, tcs.type, buf, buflen))
1554 tagcache_search_finish(&tcs);
1555 return -2;
1558 tagcache_search_finish(&tcs);
1560 return 0;
1563 static bool insert_all_playlist(struct tree_context *c, int position, bool queue)
1565 int i;
1566 char buf[MAX_PATH];
1567 int from, to, direction;
1568 int files_left = c->filesindir;
1570 cpu_boost(true);
1571 if (!tagcache_search(&tcs, tag_filename))
1573 splash(HZ, ID2P(LANG_TAGCACHE_BUSY));
1574 cpu_boost(false);
1575 return false;
1578 if (position == PLAYLIST_REPLACE)
1580 if (playlist_remove_all_tracks(NULL) == 0)
1581 position = PLAYLIST_INSERT_LAST;
1582 else
1584 cpu_boost(false);
1585 return false;
1589 if (position == PLAYLIST_INSERT_FIRST)
1591 from = c->filesindir - 1;
1592 to = -1;
1593 direction = -1;
1595 else
1597 from = 0;
1598 to = c->filesindir;
1599 direction = 1;
1602 for (i = from; i != to; i += direction)
1604 /* Count back to zero */
1605 if (!show_search_progress(false, files_left--))
1606 break;
1608 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, i)->extraseek,
1609 tcs.type, buf, sizeof buf))
1611 continue;
1614 if (playlist_insert_track(NULL, buf, position, queue, false) < 0)
1616 logf("playlist_insert_track failed");
1617 break;
1619 yield();
1621 playlist_sync(NULL);
1622 tagcache_search_finish(&tcs);
1623 cpu_boost(false);
1625 return true;
1628 bool tagtree_insert_selection_playlist(int position, bool queue)
1630 struct tagentry *dptr;
1631 char buf[MAX_PATH];
1632 int dirlevel = tc->dirlevel;
1634 /* We need to set the table to allsubentries. */
1635 show_search_progress(true, 0);
1637 dptr = tagtree_get_entry(tc, tc->selected_item);
1639 /* Insert a single track? */
1640 if (dptr->newtable == playtrack)
1642 if (tagtree_get_filename(tc, buf, sizeof buf) < 0)
1644 logf("tagtree_get_filename failed");
1645 return false;
1647 playlist_insert_track(NULL, buf, position, queue, true);
1649 return true;
1652 if (dptr->newtable == navibrowse)
1654 tagtree_enter(tc);
1655 tagtree_load(tc);
1656 dptr = tagtree_get_entry(tc, tc->selected_item);
1658 else if (dptr->newtable != allsubentries)
1660 logf("unsupported table: %d", dptr->newtable);
1661 return false;
1664 /* Now the current table should be allsubentries. */
1665 if (dptr->newtable != playtrack)
1667 tagtree_enter(tc);
1668 tagtree_load(tc);
1669 dptr = tagtree_get_entry(tc, tc->selected_item);
1671 /* And now the newtable should be playtrack. */
1672 if (dptr->newtable != playtrack)
1674 logf("newtable: %d !!", dptr->newtable);
1675 tc->dirlevel = dirlevel;
1676 return false;
1680 if (tc->filesindir <= 0)
1681 splash(HZ, ID2P(LANG_END_PLAYLIST));
1682 else
1684 logf("insert_all_playlist");
1685 if (!insert_all_playlist(tc, position, queue))
1686 splash(HZ*2, ID2P(LANG_FAILED));
1689 /* Finally return the dirlevel to its original value. */
1690 while (tc->dirlevel > dirlevel)
1691 tagtree_exit(tc);
1692 tagtree_load(tc);
1694 return true;
1697 static int tagtree_play_folder(struct tree_context* c)
1699 if (playlist_create(NULL, NULL) < 0)
1701 logf("Failed creating playlist\n");
1702 return -1;
1705 if (!insert_all_playlist(c, PLAYLIST_INSERT_LAST, false))
1706 return -2;
1708 if (global_settings.playlist_shuffle)
1709 c->selected_item = playlist_shuffle(current_tick, c->selected_item);
1710 if (!global_settings.play_selected)
1711 c->selected_item = 0;
1712 gui_synclist_select_item(&tree_lists, c->selected_item);
1714 playlist_start(c->selected_item,0);
1716 return 0;
1719 struct tagentry* tagtree_get_entry(struct tree_context *c, int id)
1721 struct tagentry *entry = (struct tagentry *)c->dircache;
1722 int realid = id - current_offset;
1724 /* Load the next chunk if necessary. */
1725 if (realid >= current_entry_count || realid < 0)
1727 cpu_boost(true);
1728 if (retrieve_entries(c, &tcs2, MAX(0, id - (current_entry_count / 2)),
1729 false) < 0)
1731 logf("retrieve failed");
1732 cpu_boost(false);
1733 return NULL;
1735 realid = id - current_offset;
1736 cpu_boost(false);
1739 return &entry[realid];
1742 char *tagtree_get_title(struct tree_context* c)
1744 switch (c->currtable)
1746 case root:
1747 return menu->title;
1749 case navibrowse:
1750 case allsubentries:
1751 return current_title[c->currextra];
1754 return "?";
1757 int tagtree_get_attr(struct tree_context* c)
1759 int attr = -1;
1760 switch (c->currtable)
1762 case navibrowse:
1763 if (csi->tagorder[c->currextra] == tag_title)
1764 attr = FILE_ATTR_AUDIO;
1765 else
1766 attr = ATTR_DIRECTORY;
1767 break;
1769 case allsubentries:
1770 attr = FILE_ATTR_AUDIO;
1771 break;
1773 default:
1774 attr = ATTR_DIRECTORY;
1775 break;
1778 return attr;
1781 int tagtree_get_icon(struct tree_context* c)
1783 int icon = Icon_Folder;
1785 if (tagtree_get_attr(c) == FILE_ATTR_AUDIO)
1786 icon = Icon_Audio;
1788 return icon;