regtools: add the imx233/stmp3780 and the stmp3700 reg map
[maemo-rb.git] / apps / tagtree.c
blob5766d2892ecacaad1dba955a0cb534dcc2e35b71
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 <stdlib.h>
31 #include "string-extra.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 "core_alloc.h"
48 #include "yesno.h"
49 #include "misc.h"
50 #include "filetypes.h"
51 #include "audio.h"
52 #include "appevents.h"
53 #include "storage.h"
54 #include "dir.h"
55 #include "playback.h"
56 #include "panic.h"
58 #define str_or_empty(x) (x ? x : "(NULL)")
60 #define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"
62 static int tagtree_play_folder(struct tree_context* c);
64 /* this needs to be same size as struct entry (tree.h) and name needs to be
65 * the first; so that they're compatible enough to walk arrays of both
66 * derefencing the name member*/
67 struct tagentry {
68 char* name;
69 int newtable;
70 int extraseek;
73 static struct tagentry* tagtree_get_entry(struct tree_context *c, int id);
75 #define SEARCHSTR_SIZE 256
77 enum table {
78 ROOT = 1,
79 NAVIBROWSE,
80 ALLSUBENTRIES,
81 PLAYTRACK,
84 static const struct id3_to_search_mapping {
85 char *string;
86 size_t id3_offset;
87 } id3_to_search_mapping[] = {
88 { "", 0 }, /* offset n/a */
89 { "#directory#", 0 }, /* offset n/a */
90 { "#title#", offsetof(struct mp3entry, title) },
91 { "#artist#", offsetof(struct mp3entry, artist) },
92 { "#album#", offsetof(struct mp3entry, album) },
93 { "#genre#", offsetof(struct mp3entry, genre_string) },
94 { "#composer#", offsetof(struct mp3entry, composer) },
95 { "#albumartist#", offsetof(struct mp3entry, albumartist) },
97 enum variables {
98 var_sorttype = 100,
99 var_limit,
100 var_strip,
101 var_menu_start,
102 var_include,
103 var_rootmenu,
104 var_format,
105 menu_next,
106 menu_load,
109 /* Capacity 10 000 entries (for example 10k different artists) */
110 #define UNIQBUF_SIZE (64*1024)
111 static long uniqbuf[UNIQBUF_SIZE / sizeof(long)];
113 #define MAX_TAGS 5
114 #define MAX_MENU_ID_SIZE 32
116 static bool sort_inverse;
119 * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
121 * valid = true
122 * formatstr = "%-3d. %s"
123 * tags[0] = tag_autoscore
124 * tags[1] = tag_title
125 * tag_count = 2
127 * limit = 100
128 * sort_inverse = true
130 struct display_format {
131 char name[32];
132 struct tagcache_search_clause *clause[TAGCACHE_MAX_CLAUSES];
133 int clause_count;
134 char *formatstr;
135 int group_id;
136 int tags[MAX_TAGS];
137 int tag_count;
139 int limit;
140 int strip;
141 bool sort_inverse;
144 static struct display_format *formats[TAGMENU_MAX_FMTS];
145 static int format_count;
147 struct menu_entry {
148 char name[64];
149 int type;
150 struct search_instruction {
151 char name[64];
152 int tagorder[MAX_TAGS];
153 int tagorder_count;
154 struct tagcache_search_clause *clause[MAX_TAGS][TAGCACHE_MAX_CLAUSES];
155 int format_id[MAX_TAGS];
156 int clause_count[MAX_TAGS];
157 int result_seek[MAX_TAGS];
158 } si;
159 int link;
162 struct menu_root {
163 char title[64];
164 char id[MAX_MENU_ID_SIZE];
165 int itemcount;
166 struct menu_entry *items[TAGMENU_MAX_ITEMS];
169 struct match
171 const char* str;
172 int symbol;
175 /* Statusbar text of the current view. */
176 static char current_title[MAX_TAGS][128];
178 static struct menu_root * menus[TAGMENU_MAX_MENUS];
179 static struct menu_root * menu;
180 static struct search_instruction *csi;
181 static const char *strp;
182 static int menu_count;
183 static int rootmenu;
185 static int current_offset;
186 static int current_entry_count;
188 static struct tree_context *tc;
190 /* a few memory alloc helper */
191 static int tagtree_handle, lock_count;
192 static size_t tagtree_bufsize, tagtree_buf_used;
194 #define UPDATE(x, y) { x = (typeof(x))((char*)(x) + (y)); }
195 static int move_callback(int handle, void* current, void* new)
197 (void)handle; (void)current; (void)new;
198 ptrdiff_t diff = new - current;
200 if (lock_count > 0)
201 return BUFLIB_CB_CANNOT_MOVE;
203 UPDATE(menu, diff);
204 /* loop over menus */
205 for(int i = 0; i < menu_count; i++)
207 struct menu_root* menu = menus[i];
208 /* then over the menu_entries of a menu */
209 for(int j = 0; j < menu->itemcount; j++)
211 struct menu_entry* mentry = menu->items[j];
212 /* then over the search_instructions of each menu_entry */
213 for(int k = 0; k < mentry->si.tagorder_count; k++)
215 for(int l = 0; l < mentry->si.clause_count[k]; l++)
217 UPDATE(mentry->si.clause[k][l]->str, diff);
218 UPDATE(mentry->si.clause[k][l], diff);
221 UPDATE(menu->items[j], diff);
223 UPDATE(menus[i], diff);
226 /* now the same game for formats */
227 for(int i = 0; i < format_count; i++)
229 for(int j = 0; j < formats[i]->clause_count; j++)
231 UPDATE(formats[i]->clause[j]->str, diff);
232 UPDATE(formats[i]->clause[j], diff);
235 if (formats[i]->formatstr)
236 UPDATE(formats[i]->formatstr, diff);
238 UPDATE(formats[i], diff);
240 return BUFLIB_CB_OK;
242 #undef UPDATE
244 static inline void tagtree_lock(void)
246 lock_count++;
249 static inline void tagtree_unlock(void)
251 lock_count--;
254 static struct buflib_callbacks ops = {
255 .move_callback = move_callback,
256 .shrink_callback = NULL,
259 static void* tagtree_alloc(size_t size)
261 char* buf = core_get_data(tagtree_handle) + tagtree_buf_used;
262 size = ALIGN_UP(size, sizeof(void*));
263 tagtree_buf_used += size;
264 return buf;
267 static void* tagtree_alloc0(size_t size)
269 void* ret = tagtree_alloc(size);
270 memset(ret, 0, size);
271 return ret;
274 static char* tagtree_strdup(const char* buf)
276 size_t len = strlen(buf) + 1;
277 char* dest = tagtree_alloc(len);
278 strcpy(dest, buf);
279 return dest;
282 /* save to call without locking */
283 static int get_token_str(char *buf, int size)
285 /* Find the start. */
286 while (*strp != '"' && *strp != '\0')
287 strp++;
289 if (*strp == '\0' || *(++strp) == '\0')
290 return -1;
292 /* Read the data. */
293 while (*strp != '"' && *strp != '\0' && --size > 0)
294 *(buf++) = *(strp++);
296 *buf = '\0';
297 if (*strp != '"')
298 return -2;
300 strp++;
302 return 0;
305 static int get_tag(int *tag)
307 static const struct match get_tag_match[] =
309 {"album", tag_album},
310 {"artist", tag_artist},
311 {"bitrate", tag_bitrate},
312 {"composer", tag_composer},
313 {"comment", tag_comment},
314 {"albumartist", tag_albumartist},
315 {"ensemble", tag_albumartist},
316 {"grouping", tag_grouping},
317 {"genre", tag_genre},
318 {"length", tag_length},
319 {"Lm", tag_virt_length_min},
320 {"Ls", tag_virt_length_sec},
321 {"Pm", tag_virt_playtime_min},
322 {"Ps", tag_virt_playtime_sec},
323 {"title", tag_title},
324 {"filename", tag_filename},
325 {"basename", tag_virt_basename},
326 {"tracknum", tag_tracknumber},
327 {"discnum", tag_discnumber},
328 {"year", tag_year},
329 {"playcount", tag_playcount},
330 {"rating", tag_rating},
331 {"lastplayed", tag_lastplayed},
332 {"lastoffset", tag_lastoffset},
333 {"commitid", tag_commitid},
334 {"entryage", tag_virt_entryage},
335 {"autoscore", tag_virt_autoscore},
336 {"%sort", var_sorttype},
337 {"%limit", var_limit},
338 {"%strip", var_strip},
339 {"%menu_start", var_menu_start},
340 {"%include", var_include},
341 {"%root_menu", var_rootmenu},
342 {"%format", var_format},
343 {"->", menu_next},
344 {"==>", menu_load}
346 char buf[128];
347 unsigned int i;
349 /* Find the start. */
350 while ((*strp == ' ' || *strp == '>') && *strp != '\0')
351 strp++;
353 if (*strp == '\0' || *strp == '?')
354 return 0;
356 for (i = 0; i < sizeof(buf)-1; i++)
358 if (*strp == '\0' || *strp == ' ')
359 break ;
360 buf[i] = *strp;
361 strp++;
363 buf[i] = '\0';
365 for (i = 0; i < ARRAYLEN(get_tag_match); i++)
367 if (!strcasecmp(buf, get_tag_match[i].str))
369 *tag = get_tag_match[i].symbol;
370 return 1;
374 logf("NO MATCH: %s\n", buf);
375 if (buf[0] == '?')
376 return 0;
378 return -1;
381 static int get_clause(int *condition)
383 static const struct match get_clause_match[] =
385 {"=", clause_is},
386 {"==", clause_is},
387 {"!=", clause_is_not},
388 {">", clause_gt},
389 {">=", clause_gteq},
390 {"<", clause_lt},
391 {"<=", clause_lteq},
392 {"~", clause_contains},
393 {"!~", clause_not_contains},
394 {"^", clause_begins_with},
395 {"!^", clause_not_begins_with},
396 {"$", clause_ends_with},
397 {"!$", clause_not_ends_with},
398 {"@", clause_oneof}
401 char buf[4];
402 unsigned int i;
404 /* Find the start. */
405 while (*strp == ' ' && *strp != '\0')
406 strp++;
408 if (*strp == '\0')
409 return 0;
411 for (i = 0; i < sizeof(buf)-1; i++)
413 if (*strp == '\0' || *strp == ' ')
414 break ;
415 buf[i] = *strp;
416 strp++;
418 buf[i] = '\0';
420 for (i = 0; i < ARRAYLEN(get_clause_match); i++)
422 if (!strcasecmp(buf, get_clause_match[i].str))
424 *condition = get_clause_match[i].symbol;
425 return 1;
429 return 0;
432 static bool read_clause(struct tagcache_search_clause *clause)
434 char buf[SEARCHSTR_SIZE];
435 unsigned int i;
437 if (get_tag(&clause->tag) <= 0)
438 return false;
440 if (get_clause(&clause->type) <= 0)
441 return false;
443 if (get_token_str(buf, sizeof buf) < 0)
444 return false;
446 for (i=0; i<ARRAYLEN(id3_to_search_mapping); i++)
448 if (!strcasecmp(buf, id3_to_search_mapping[i].string))
449 break;
452 if (i<ARRAYLEN(id3_to_search_mapping)) /* runtime search operand found */
454 clause->source = source_runtime+i;
455 clause->str = tagtree_alloc(SEARCHSTR_SIZE);
457 else
459 clause->source = source_constant;
460 clause->str = tagtree_strdup(buf);
463 if (TAGCACHE_IS_NUMERIC(clause->tag))
465 clause->numeric = true;
466 clause->numeric_data = atoi(clause->str);
468 else
469 clause->numeric = false;
471 logf("got clause: %d/%d [%s]", clause->tag, clause->type, clause->str);
473 return true;
476 static bool read_variable(char *buf, int size)
478 int condition;
480 if (!get_clause(&condition))
481 return false;
483 if (condition != clause_is)
484 return false;
486 if (get_token_str(buf, size) < 0)
487 return false;
489 return true;
492 /* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" */
493 static int get_format_str(struct display_format *fmt)
495 int ret;
496 char buf[128];
497 int i;
499 memset(fmt, 0, sizeof(struct display_format));
501 if (get_token_str(fmt->name, sizeof fmt->name) < 0)
502 return -10;
504 /* Determine the group id */
505 fmt->group_id = 0;
506 for (i = 0; i < format_count; i++)
508 if (!strcasecmp(formats[i]->name, fmt->name))
510 fmt->group_id = formats[i]->group_id;
511 break;
514 if (formats[i]->group_id > fmt->group_id)
515 fmt->group_id = formats[i]->group_id;
518 if (i == format_count)
519 fmt->group_id++;
521 logf("format: (%d) %s", fmt->group_id, fmt->name);
523 if (get_token_str(buf, sizeof buf) < 0)
524 return -10;
526 fmt->formatstr = tagtree_strdup(buf);
528 while (fmt->tag_count < MAX_TAGS)
530 ret = get_tag(&fmt->tags[fmt->tag_count]);
531 if (ret < 0)
532 return -11;
534 if (ret == 0)
535 break;
537 switch (fmt->tags[fmt->tag_count]) {
538 case var_sorttype:
539 if (!read_variable(buf, sizeof buf))
540 return -12;
541 if (!strcasecmp("inverse", buf))
542 fmt->sort_inverse = true;
543 break;
545 case var_limit:
546 if (!read_variable(buf, sizeof buf))
547 return -13;
548 fmt->limit = atoi(buf);
549 break;
551 case var_strip:
552 if (!read_variable(buf, sizeof buf))
553 return -14;
554 fmt->strip = atoi(buf);
555 break;
557 default:
558 fmt->tag_count++;
562 return 1;
565 static int add_format(const char *buf)
567 if (format_count >= TAGMENU_MAX_FMTS)
569 logf("too many formats");
570 return -1;
573 strp = buf;
575 if (formats[format_count] == NULL)
576 formats[format_count] = tagtree_alloc0(sizeof(struct display_format));
578 if (get_format_str(formats[format_count]) < 0)
580 logf("get_format_str() parser failed!");
581 memset(formats[format_count], 0, sizeof(struct display_format));
582 return -4;
585 while (*strp != '\0' && *strp != '?')
586 strp++;
588 if (*strp == '?')
590 int clause_count = 0;
591 strp++;
593 tagtree_lock();
594 while (1)
596 struct tagcache_search_clause *newclause;
598 if (clause_count >= TAGCACHE_MAX_CLAUSES)
600 logf("too many clauses");
601 break;
604 newclause = tagtree_alloc(sizeof(struct tagcache_search_clause));
606 formats[format_count]->clause[clause_count] = newclause;
607 if (!read_clause(newclause))
608 break;
610 clause_count++;
612 tagtree_unlock();
614 formats[format_count]->clause_count = clause_count;
617 format_count++;
619 return 1;
622 static int get_condition(struct search_instruction *inst)
624 struct tagcache_search_clause *new_clause;
625 int clause_count;
626 char buf[128];
628 switch (*strp)
630 case '=':
632 int i;
634 if (get_token_str(buf, sizeof buf) < 0)
635 return -1;
637 for (i = 0; i < format_count; i++)
639 if (!strcasecmp(formats[i]->name, buf))
640 break;
643 if (i == format_count)
645 logf("format not found: %s", buf);
646 return -2;
649 inst->format_id[inst->tagorder_count] = formats[i]->group_id;
650 return 1;
652 case '?':
653 case ' ':
654 case '&':
655 strp++;
656 return 1;
657 case '-':
658 case '\0':
659 return 0;
662 clause_count = inst->clause_count[inst->tagorder_count];
663 if (clause_count >= TAGCACHE_MAX_CLAUSES)
665 logf("Too many clauses");
666 return false;
669 new_clause = tagtree_alloc(sizeof(struct tagcache_search_clause));
670 inst->clause[inst->tagorder_count][clause_count] = new_clause;
672 if (*strp == '|')
674 strp++;
675 new_clause->type = clause_logical_or;
677 else
679 tagtree_lock();
680 bool ret = read_clause(new_clause);
681 tagtree_unlock();
682 if (!ret)
683 return -1;
685 inst->clause_count[inst->tagorder_count]++;
687 return 1;
690 /* example search:
691 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
692 * : album ? year >= "2000" : songs
693 * ^ begins with
694 * * contains
695 * $ ends with
698 static bool parse_search(struct menu_entry *entry, const char *str)
700 int ret;
701 int type;
702 struct search_instruction *inst = &entry->si;
703 char buf[MAX_PATH];
704 int i;
706 strp = str;
708 /* Parse entry name */
709 if (get_token_str(entry->name, sizeof entry->name) < 0)
711 logf("No name found.");
712 return false;
715 /* Parse entry type */
716 if (get_tag(&entry->type) <= 0)
717 return false;
719 if (entry->type == menu_load)
721 if (get_token_str(buf, sizeof buf) < 0)
722 return false;
724 /* Find the matching root menu or "create" it */
725 for (i = 0; i < menu_count; i++)
727 if (!strcasecmp(menus[i]->id, buf))
729 entry->link = i;
730 return true;
734 if (menu_count >= TAGMENU_MAX_MENUS)
736 logf("max menucount reached");
737 return false;
740 /* Allocate a new menu unless link is found. */
741 menus[menu_count] = tagtree_alloc0(sizeof(struct menu_root));
742 strlcpy(menus[menu_count]->id, buf, MAX_MENU_ID_SIZE);
743 entry->link = menu_count;
744 ++menu_count;
746 return true;
749 if (entry->type != menu_next)
750 return false;
752 while (inst->tagorder_count < MAX_TAGS)
754 ret = get_tag(&inst->tagorder[inst->tagorder_count]);
755 if (ret < 0)
757 logf("Parse error #1");
758 logf("%s", strp);
759 return false;
762 if (ret == 0)
763 break ;
765 logf("tag: %d", inst->tagorder[inst->tagorder_count]);
767 tagtree_lock();
768 while ( (ret = get_condition(inst)) > 0 ) ;
769 tagtree_unlock();
771 if (ret < 0)
772 return false;
774 inst->tagorder_count++;
776 if (get_tag(&type) <= 0 || type != menu_next)
777 break;
780 return true;
783 static int compare(const void *p1, const void *p2)
785 struct tagentry *e1 = (struct tagentry *)p1;
786 struct tagentry *e2 = (struct tagentry *)p2;
788 if (sort_inverse)
789 return strncasecmp(e2->name, e1->name, MAX_PATH);
791 return strncasecmp(e1->name, e2->name, MAX_PATH);
794 static void tagtree_buffer_event(void *data)
796 struct tagcache_search tcs;
797 struct mp3entry *id3 = (struct mp3entry*)data;
799 /* Do not gather data unless proper setting has been enabled. */
800 if (!global_settings.runtimedb && !global_settings.autoresume_enable)
801 return;
803 logf("be:%s", id3->path);
805 while (! tagcache_is_fully_initialized())
806 yield();
808 if (!tagcache_find_index(&tcs, id3->path))
810 logf("tc stat: not found: %s", id3->path);
811 return;
814 if (global_settings.runtimedb)
816 id3->playcount = tagcache_get_numeric(&tcs, tag_playcount);
817 if (!id3->rating)
818 id3->rating = tagcache_get_numeric(&tcs, tag_rating);
819 id3->lastplayed = tagcache_get_numeric(&tcs, tag_lastplayed);
820 id3->score = tagcache_get_numeric(&tcs, tag_virt_autoscore) / 10;
821 id3->playtime = tagcache_get_numeric(&tcs, tag_playtime);
823 logf("-> %ld/%ld", id3->playcount, id3->playtime);
826 #if CONFIG_CODEC == SWCODEC
827 if (global_settings.autoresume_enable)
829 /* Load current file resume offset if not already defined (by
830 another resume mechanism) */
831 if (id3->offset == 0)
833 id3->offset = tagcache_get_numeric(&tcs, tag_lastoffset);
835 logf("tagtree_buffer_event: Set offset for %s to %lX\n",
836 str_or_empty(id3->title), id3->offset);
839 #endif
841 /* Store our tagcache index pointer. */
842 id3->tagcache_idx = tcs.idx_id+1;
844 tagcache_search_finish(&tcs);
847 static void tagtree_track_finish_event(void *data)
849 long lastplayed;
850 long tagcache_idx;
851 struct mp3entry *id3 = (struct mp3entry*)data;
853 /* Do not gather data unless proper setting has been enabled. */
854 if (!global_settings.runtimedb && !global_settings.autoresume_enable)
856 logf("runtimedb gathering and autoresume not enabled");
857 return;
860 tagcache_idx=id3->tagcache_idx;
861 if (!tagcache_idx)
863 logf("No tagcache index pointer found");
864 return;
866 tagcache_idx--;
868 /* Don't process unplayed tracks, or tracks interrupted within the
869 first 15 seconds. */
870 if (id3->elapsed == 0
871 #if CONFIG_CODEC == SWCODEC /* HWCODEC doesn't have automatic_skip */
872 || (id3->elapsed < 15 * 1000 && !audio_automatic_skip())
873 #endif
876 logf("not logging unplayed or skipped track");
877 return;
880 lastplayed = tagcache_increase_serial();
881 if (lastplayed < 0)
883 logf("incorrect tc serial:%ld", lastplayed);
884 return;
887 if (global_settings.runtimedb)
889 long playcount;
890 long playtime;
892 playcount = id3->playcount + 1;
894 /* Ignore the last 15s (crossfade etc.) */
895 playtime = id3->playtime + MIN(id3->length, id3->elapsed + 15 * 1000);
897 logf("ube:%s", id3->path);
898 logf("-> %ld/%ld", playcount, playtime);
899 logf("-> %ld/%ld/%ld", id3->elapsed, id3->length,
900 MIN(id3->length, id3->elapsed + 15 * 1000));
902 /* Queue the updates to the tagcache system. */
903 tagcache_update_numeric(tagcache_idx, tag_playcount, playcount);
904 tagcache_update_numeric(tagcache_idx, tag_playtime, playtime);
905 tagcache_update_numeric(tagcache_idx, tag_lastplayed, lastplayed);
908 #if CONFIG_CODEC == SWCODEC
909 if (global_settings.autoresume_enable)
911 unsigned long offset
912 = audio_automatic_skip() ? 0 : id3->offset;
914 tagcache_update_numeric(tagcache_idx, tag_lastoffset, offset);
916 logf("tagtree_track_finish_event: Save offset for %s: %lX",
917 str_or_empty(id3->title), offset);
919 #endif
922 bool tagtree_export(void)
924 struct tagcache_search tcs;
926 splash(0, str(LANG_CREATING));
927 if (!tagcache_create_changelog(&tcs))
929 splash(HZ*2, ID2P(LANG_FAILED));
932 return false;
935 bool tagtree_import(void)
937 splash(0, ID2P(LANG_WAIT));
938 if (!tagcache_import_changelog())
940 splash(HZ*2, ID2P(LANG_FAILED));
943 return false;
946 static bool parse_menu(const char *filename);
948 static int parse_line(int n, char *buf, void *parameters)
950 char data[256];
951 int variable;
952 static bool read_menu;
953 int i;
954 char *p;
956 (void)parameters;
958 /* Strip possible <CR> at end of line. */
959 p = strchr(buf, '\r');
960 if (p != NULL)
961 *p = '\0';
963 logf("parse:%d/%s", n, buf);
965 /* First line, do initialisation. */
966 if (n == 0)
968 if (strcasecmp(TAGNAVI_VERSION, buf))
970 logf("Version mismatch");
971 return -1;
974 read_menu = false;
977 if (buf[0] == '#')
978 return 0;
980 if (buf[0] == '\0')
982 if (read_menu)
984 /* End the menu */
985 read_menu = false;
987 return 0;
990 if (!read_menu)
992 strp = buf;
993 if (get_tag(&variable) <= 0)
994 return 0;
996 switch (variable)
998 case var_format:
999 if (add_format(strp) < 0)
1001 logf("Format add fail: %s", data);
1003 break;
1005 case var_include:
1006 if (get_token_str(data, sizeof(data)) < 0)
1008 logf("%%include empty");
1009 return 0;
1012 if (!parse_menu(data))
1014 logf("Load menu fail: %s", data);
1016 break;
1018 case var_menu_start:
1019 if (menu_count >= TAGMENU_MAX_MENUS)
1021 logf("max menucount reached");
1022 return 0;
1025 if (get_token_str(data, sizeof data) < 0)
1027 logf("%%menu_start id empty");
1028 return 0;
1031 menu = NULL;
1032 for (i = 0; i < menu_count; i++)
1034 if (!strcasecmp(menus[i]->id, data))
1036 menu = menus[i];
1040 if (menu == NULL)
1042 menus[menu_count] = tagtree_alloc0(sizeof(struct menu_root));
1043 menu = menus[menu_count];
1044 ++menu_count;
1045 strlcpy(menu->id, data, MAX_MENU_ID_SIZE);
1048 if (get_token_str(menu->title, sizeof(menu->title)) < 0)
1050 logf("%%menu_start title empty");
1051 return 0;
1053 logf("menu: %s", menu->title);
1054 read_menu = true;
1055 break;
1057 case var_rootmenu:
1058 /* Only set root menu once. */
1059 if (rootmenu >= 0)
1060 break;
1062 if (get_token_str(data, sizeof(data)) < 0)
1064 logf("%%rootmenu empty");
1065 return 0;
1068 for (i = 0; i < menu_count; i++)
1070 if (!strcasecmp(menus[i]->id, data))
1072 rootmenu = i;
1075 break;
1078 return 0;
1081 if (menu->itemcount >= TAGMENU_MAX_ITEMS)
1083 logf("max itemcount reached");
1084 return 0;
1087 /* Allocate */
1088 if (menu->items[menu->itemcount] == NULL)
1089 menu->items[menu->itemcount] = tagtree_alloc0(sizeof(struct menu_entry));
1091 tagtree_lock();
1092 if (parse_search(menu->items[menu->itemcount], buf))
1093 menu->itemcount++;
1094 tagtree_unlock();
1096 return 0;
1099 static bool parse_menu(const char *filename)
1101 int fd;
1102 char buf[1024];
1104 if (menu_count >= TAGMENU_MAX_MENUS)
1106 logf("max menucount reached");
1107 return false;
1110 fd = open(filename, O_RDONLY);
1111 if (fd < 0)
1113 logf("Search instruction file not found.");
1114 return false;
1117 /* Now read file for real, parsing into si */
1118 fast_readline(fd, buf, sizeof buf, NULL, parse_line);
1119 close(fd);
1121 return true;
1124 void tagtree_init(void)
1126 format_count = 0;
1127 menu_count = 0;
1128 menu = NULL;
1129 rootmenu = -1;
1130 tagtree_handle = core_alloc_maximum("tagtree", &tagtree_bufsize, &ops);
1131 parse_menu(FILE_SEARCH_INSTRUCTIONS);
1133 /* safety check since tree.c needs to cast tagentry to entry */
1134 if (sizeof(struct tagentry) != sizeof(struct entry))
1135 panicf("tagentry(%zu) and entry mismatch(%zu)",
1136 sizeof(struct tagentry), sizeof(struct entry));
1137 if (lock_count > 0)
1138 panicf("tagtree locked after parsing");
1140 /* If no root menu is set, assume it's the first single menu
1141 * we have. That shouldn't normally happen. */
1142 if (rootmenu < 0)
1143 rootmenu = 0;
1145 add_event(PLAYBACK_EVENT_TRACK_BUFFER, false, tagtree_buffer_event);
1146 add_event(PLAYBACK_EVENT_TRACK_FINISH, false, tagtree_track_finish_event);
1148 core_shrink(tagtree_handle, core_get_data(tagtree_handle), tagtree_buf_used);
1151 static bool show_search_progress(bool init, int count)
1153 static int last_tick = 0;
1155 /* Don't show splashes for 1/2 second after starting search */
1156 if (init)
1158 last_tick = current_tick + HZ/2;
1159 return true;
1162 /* Update progress every 1/10 of a second */
1163 if (TIME_AFTER(current_tick, last_tick + HZ/10))
1165 splashf(0, str(LANG_PLAYLIST_SEARCH_MSG), count, str(LANG_OFF_ABORT));
1166 if (action_userabort(TIMEOUT_NOBLOCK))
1167 return false;
1168 last_tick = current_tick;
1169 yield();
1172 return true;
1175 static int format_str(struct tagcache_search *tcs, struct display_format *fmt,
1176 char *buf, int buf_size)
1178 char fmtbuf[20];
1179 bool read_format = false;
1180 unsigned fmtbuf_pos = 0;
1181 int parpos = 0;
1182 int buf_pos = 0;
1183 int i;
1185 memset(buf, 0, buf_size);
1186 for (i = 0; fmt->formatstr[i] != '\0'; i++)
1188 if (fmt->formatstr[i] == '%')
1190 read_format = true;
1191 fmtbuf_pos = 0;
1192 if (parpos >= fmt->tag_count)
1194 logf("too many format tags");
1195 return -1;
1199 char formatchar = fmt->formatstr[i];
1201 if (read_format)
1203 fmtbuf[fmtbuf_pos++] = formatchar;
1204 if (fmtbuf_pos >= sizeof fmtbuf)
1206 logf("format parse error");
1207 return -2;
1210 if (formatchar == 's' || formatchar == 'd')
1212 unsigned space_left = buf_size - buf_pos;
1213 char tmpbuf[MAX_PATH];
1214 char *result;
1216 fmtbuf[fmtbuf_pos] = '\0';
1217 read_format = false;
1219 switch (formatchar)
1221 case 's':
1222 if (fmt->tags[parpos] == tcs->type)
1224 result = tcs->result;
1226 else
1228 /* Need to fetch the tag data. */
1229 int tag = fmt->tags[parpos];
1231 if (!tagcache_retrieve(tcs, tcs->idx_id,
1232 (tag == tag_virt_basename ?
1233 tag_filename : tag),
1234 tmpbuf, sizeof tmpbuf))
1236 logf("retrieve failed");
1237 return -3;
1240 if (tag == tag_virt_basename
1241 && (result = strrchr(tmpbuf, '/')) != NULL)
1243 result++;
1245 else
1246 result = tmpbuf;
1248 buf_pos +=
1249 snprintf(&buf[buf_pos], space_left, fmtbuf, result);
1250 break;
1252 case 'd':
1253 buf_pos +=
1254 snprintf(&buf[buf_pos], space_left, fmtbuf,
1255 tagcache_get_numeric(tcs, fmt->tags[parpos]));
1258 parpos++;
1261 else
1262 buf[buf_pos++] = formatchar;
1264 if (buf_pos >= buf_size - 1) /* need at least one more byte for \0 */
1266 logf("buffer overflow");
1267 return -4;
1271 buf[buf_pos++] = '\0';
1273 return 0;
1276 static struct tagentry* get_entries(struct tree_context *tc)
1278 return core_get_data(tc->cache.entries_handle);
1281 static int retrieve_entries(struct tree_context *c, int offset, bool init)
1283 struct tagcache_search tcs;
1284 struct display_format *fmt;
1285 int i;
1286 int namebufused = 0;
1287 int total_count = 0;
1288 int special_entry_count = 0;
1289 int level = c->currextra;
1290 int tag;
1291 bool sort = false;
1292 int sort_limit;
1293 int strip;
1295 /* Show search progress straight away if the disk needs to spin up,
1296 otherwise show it after the normal 1/2 second delay */
1297 show_search_progress(
1298 #ifdef HAVE_DISK_STORAGE
1299 storage_disk_is_active()
1300 #else
1301 true
1302 #endif
1303 , 0);
1305 if (c->currtable == ALLSUBENTRIES)
1307 tag = tag_title;
1308 level--;
1310 else
1311 tag = csi->tagorder[level];
1313 if (!tagcache_search(&tcs, tag))
1314 return -1;
1316 /* Prevent duplicate entries in the search list. */
1317 tagcache_search_set_uniqbuf(&tcs, uniqbuf, UNIQBUF_SIZE);
1319 if (level || csi->clause_count[0] || TAGCACHE_IS_NUMERIC(tag))
1320 sort = true;
1322 for (i = 0; i < level; i++)
1324 if (TAGCACHE_IS_NUMERIC(csi->tagorder[i]))
1326 static struct tagcache_search_clause cc;
1328 memset(&cc, 0, sizeof(struct tagcache_search_clause));
1329 cc.tag = csi->tagorder[i];
1330 cc.type = clause_is;
1331 cc.numeric = true;
1332 cc.numeric_data = csi->result_seek[i];
1333 tagcache_search_add_clause(&tcs, &cc);
1335 else
1337 tagcache_search_add_filter(&tcs, csi->tagorder[i],
1338 csi->result_seek[i]);
1342 /* because tagcache saves the clauses, we need to lock the buffer
1343 * for the entire duration of the search */
1344 tagtree_lock();
1345 for (i = 0; i <= level; i++)
1347 int j;
1349 for (j = 0; j < csi->clause_count[i]; j++)
1350 tagcache_search_add_clause(&tcs, csi->clause[i][j]);
1353 current_offset = offset;
1354 current_entry_count = 0;
1355 c->dirfull = false;
1357 fmt = NULL;
1358 for (i = 0; i < format_count; i++)
1360 if (formats[i]->group_id == csi->format_id[level])
1361 fmt = formats[i];
1364 if (fmt)
1366 sort_inverse = fmt->sort_inverse;
1367 sort_limit = fmt->limit;
1368 strip = fmt->strip;
1369 sort = true;
1371 else
1373 sort_inverse = false;
1374 sort_limit = 0;
1375 strip = 0;
1378 /* lock buflib out due to possible yields */
1379 tree_lock_cache(c);
1380 struct tagentry *dptr = core_get_data(c->cache.entries_handle);
1382 if (tag != tag_title && tag != tag_filename)
1384 if (offset == 0)
1386 dptr->newtable = ALLSUBENTRIES;
1387 dptr->name = str(LANG_TAGNAVI_ALL_TRACKS);
1388 dptr++;
1389 current_entry_count++;
1390 special_entry_count++;
1392 if (offset <= 1)
1394 dptr->newtable = NAVIBROWSE;
1395 dptr->name = str(LANG_TAGNAVI_RANDOM);
1396 dptr->extraseek = -1;
1397 dptr++;
1398 current_entry_count++;
1399 special_entry_count++;
1402 total_count += 2;
1405 while (tagcache_get_next(&tcs))
1407 if (total_count++ < offset)
1408 continue;
1410 dptr->newtable = NAVIBROWSE;
1411 if (tag == tag_title || tag == tag_filename)
1413 dptr->newtable = PLAYTRACK;
1414 dptr->extraseek = tcs.idx_id;
1416 else
1417 dptr->extraseek = tcs.result_seek;
1419 fmt = NULL;
1420 /* Check the format */
1421 tagtree_lock();
1422 for (i = 0; i < format_count; i++)
1424 if (formats[i]->group_id != csi->format_id[level])
1425 continue;
1427 if (tagcache_check_clauses(&tcs, formats[i]->clause,
1428 formats[i]->clause_count))
1430 fmt = formats[i];
1431 break;
1434 tagtree_unlock();
1436 if (strcmp(tcs.result, UNTAGGED) == 0)
1438 tcs.result = str(LANG_TAGNAVI_UNTAGGED);
1439 tcs.result_len = strlen(tcs.result);
1440 tcs.ramresult = true;
1443 if (!tcs.ramresult || fmt)
1445 dptr->name = core_get_data(c->cache.name_buffer_handle)+namebufused;
1447 if (fmt)
1449 int ret = format_str(&tcs, fmt, dptr->name,
1450 c->cache.name_buffer_size - namebufused);
1451 if (ret == -4) /* buffer full */
1453 logf("chunk mode #2: %d", current_entry_count);
1454 c->dirfull = true;
1455 sort = false;
1456 break ;
1458 else if (ret < 0)
1460 logf("format_str() failed");
1461 tagcache_search_finish(&tcs);
1462 tree_unlock_cache(c);
1463 tagtree_unlock();
1464 return 0;
1466 else
1467 namebufused += strlen(dptr->name)+1;
1469 else
1471 namebufused += tcs.result_len;
1472 if (namebufused < c->cache.name_buffer_size)
1473 strcpy(dptr->name, tcs.result);
1474 else
1476 logf("chunk mode #2a: %d", current_entry_count);
1477 c->dirfull = true;
1478 sort = false;
1479 break ;
1483 else
1484 dptr->name = tcs.result;
1486 dptr++;
1487 current_entry_count++;
1489 if (current_entry_count >= c->cache.max_entries)
1491 logf("chunk mode #3: %d", current_entry_count);
1492 c->dirfull = true;
1493 sort = false;
1494 break ;
1497 if (init && !tcs.ramsearch)
1499 if (!show_search_progress(false, total_count))
1500 { /* user aborted */
1501 tagcache_search_finish(&tcs);
1502 tree_unlock_cache(c);
1503 tagtree_unlock();
1504 return current_entry_count;
1509 if (sort)
1511 struct tagentry *entries = get_entries(c);
1512 qsort(&entries[special_entry_count],
1513 current_entry_count - special_entry_count,
1514 sizeof(struct tagentry), compare);
1517 if (!init)
1519 tagcache_search_finish(&tcs);
1520 tree_unlock_cache(c);
1521 tagtree_unlock();
1522 return current_entry_count;
1525 while (tagcache_get_next(&tcs))
1527 if (!tcs.ramsearch)
1529 if (!show_search_progress(false, total_count))
1530 break;
1532 total_count++;
1535 tagcache_search_finish(&tcs);
1536 tree_unlock_cache(c);
1537 tagtree_unlock();
1539 if (!sort && (sort_inverse || sort_limit))
1541 splashf(HZ*4, ID2P(LANG_SHOWDIR_BUFFER_FULL), total_count);
1542 logf("Too small dir buffer");
1543 return 0;
1546 if (sort_limit)
1547 total_count = MIN(total_count, sort_limit);
1549 if (strip)
1551 dptr = get_entries(c);
1552 for (i = special_entry_count; i < current_entry_count; i++, dptr++)
1554 int len = strlen(dptr->name);
1556 if (len < strip)
1557 continue;
1559 dptr->name = &dptr->name[strip];
1563 return total_count;
1567 static int load_root(struct tree_context *c)
1569 struct tagentry *dptr = core_get_data(c->cache.entries_handle);
1570 int i;
1572 tc = c;
1573 c->currtable = ROOT;
1574 if (c->dirlevel == 0)
1575 c->currextra = rootmenu;
1577 menu = menus[c->currextra];
1578 if (menu == NULL)
1579 return 0;
1581 for (i = 0; i < menu->itemcount; i++)
1583 dptr->name = menu->items[i]->name;
1584 switch (menu->items[i]->type)
1586 case menu_next:
1587 dptr->newtable = NAVIBROWSE;
1588 dptr->extraseek = i;
1589 break;
1591 case menu_load:
1592 dptr->newtable = ROOT;
1593 dptr->extraseek = menu->items[i]->link;
1594 break;
1597 dptr++;
1600 current_offset = 0;
1601 current_entry_count = i;
1603 return i;
1606 int tagtree_load(struct tree_context* c)
1608 int count;
1609 int table = c->currtable;
1611 c->dirsindir = 0;
1613 if (!table)
1615 c->dirfull = false;
1616 table = ROOT;
1617 c->currtable = table;
1618 c->currextra = rootmenu;
1621 switch (table)
1623 case ROOT:
1624 count = load_root(c);
1625 break;
1627 case ALLSUBENTRIES:
1628 case NAVIBROWSE:
1629 logf("navibrowse...");
1630 cpu_boost(true);
1631 count = retrieve_entries(c, 0, true);
1632 cpu_boost(false);
1633 break;
1635 default:
1636 logf("Unsupported table %d\n", table);
1637 return -1;
1640 if (count < 0)
1642 c->dirlevel = 0;
1643 count = load_root(c);
1644 splash(HZ, str(LANG_TAGCACHE_BUSY));
1647 /* The _total_ numer of entries available. */
1648 c->dirlength = c->filesindir = count;
1650 return count;
1653 int tagtree_enter(struct tree_context* c)
1655 int rc = 0;
1656 struct tagentry *dptr;
1657 struct mp3entry *id3;
1658 int newextra;
1659 int seek;
1660 int source;
1662 dptr = tagtree_get_entry(c, c->selected_item);
1664 c->dirfull = false;
1665 seek = dptr->extraseek;
1666 if (seek == -1)
1668 if(c->filesindir<=2)
1669 return 0;
1670 srand(current_tick);
1671 dptr = (tagtree_get_entry(c, 2+(rand() % (c->filesindir-2))));
1672 seek = dptr->extraseek;
1674 newextra = dptr->newtable;
1676 if (c->dirlevel >= MAX_DIR_LEVELS)
1677 return 0;
1679 c->selected_item_history[c->dirlevel]=c->selected_item;
1680 c->table_history[c->dirlevel] = c->currtable;
1681 c->extra_history[c->dirlevel] = c->currextra;
1682 c->pos_history[c->dirlevel] = c->firstpos;
1683 c->dirlevel++;
1685 /* lock buflib for possible I/O to protect dptr */
1686 tree_lock_cache(c);
1687 tagtree_lock();
1689 switch (c->currtable) {
1690 case ROOT:
1691 c->currextra = newextra;
1693 if (newextra == ROOT)
1695 menu = menus[seek];
1696 c->currextra = seek;
1699 else if (newextra == NAVIBROWSE)
1701 int i, j;
1703 csi = &menu->items[seek]->si;
1704 c->currextra = 0;
1706 strlcpy(current_title[c->currextra], dptr->name,
1707 sizeof(current_title[0]));
1709 /* Read input as necessary. */
1710 for (i = 0; i < csi->tagorder_count; i++)
1712 for (j = 0; j < csi->clause_count[i]; j++)
1714 char* searchstring;
1716 if (csi->clause[i][j]->type == clause_logical_or)
1717 continue;
1719 source = csi->clause[i][j]->source;
1721 if (source == source_constant)
1722 continue;
1724 searchstring=csi->clause[i][j]->str;
1725 *searchstring = '\0';
1727 id3 = audio_current_track();
1729 if (source == source_current_path && id3)
1731 char *e;
1732 strlcpy(searchstring, id3->path, SEARCHSTR_SIZE);
1733 e = strrchr(searchstring, '/');
1734 if (e)
1735 *e = '\0';
1737 else if (source > source_runtime && id3)
1740 int k = source-source_runtime;
1741 int offset = id3_to_search_mapping[k].id3_offset;
1742 char **src = (char**)((char*)id3 + offset);
1743 if (*src)
1745 strlcpy(searchstring, *src, SEARCHSTR_SIZE);
1748 else
1750 rc = kbd_input(searchstring, SEARCHSTR_SIZE);
1751 if (rc < 0 || !searchstring[0])
1753 tagtree_exit(c);
1754 tree_unlock_cache(c);
1755 tagtree_unlock();
1756 return 0;
1758 if (csi->clause[i][j]->numeric)
1759 csi->clause[i][j]->numeric_data = atoi(searchstring);
1766 c->currtable = newextra;
1768 break;
1770 case NAVIBROWSE:
1771 case ALLSUBENTRIES:
1772 if (newextra == PLAYTRACK)
1774 if (global_settings.party_mode && audio_status()) {
1775 splash(HZ, ID2P(LANG_PARTY_MODE));
1776 break;
1778 c->dirlevel--;
1779 /* about to create a new current playlist...
1780 allow user to cancel the operation */
1781 if (!warn_on_pl_erase())
1782 break;
1783 if (tagtree_play_folder(c) >= 0)
1784 rc = 2;
1785 break;
1788 c->currtable = newextra;
1789 csi->result_seek[c->currextra] = seek;
1790 if (c->currextra < csi->tagorder_count-1)
1791 c->currextra++;
1792 else
1793 c->dirlevel--;
1795 /* Update the statusbar title */
1796 strlcpy(current_title[c->currextra], dptr->name,
1797 sizeof(current_title[0]));
1798 break;
1800 default:
1801 c->dirlevel--;
1802 break;
1806 c->selected_item=0;
1807 gui_synclist_select_item(&tree_lists, c->selected_item);
1808 tree_unlock_cache(c);
1809 tagtree_unlock();
1811 return rc;
1814 void tagtree_exit(struct tree_context* c)
1816 c->dirfull = false;
1817 if (c->dirlevel > 0)
1818 c->dirlevel--;
1819 c->selected_item=c->selected_item_history[c->dirlevel];
1820 gui_synclist_select_item(&tree_lists, c->selected_item);
1821 c->currtable = c->table_history[c->dirlevel];
1822 c->currextra = c->extra_history[c->dirlevel];
1823 c->firstpos = c->pos_history[c->dirlevel];
1826 int tagtree_get_filename(struct tree_context* c, char *buf, int buflen)
1828 struct tagcache_search tcs;
1829 int extraseek = tagtree_get_entry(c, c->selected_item)->extraseek;
1832 if (!tagcache_search(&tcs, tag_filename))
1833 return -1;
1835 if (!tagcache_retrieve(&tcs, extraseek, tcs.type, buf, buflen))
1837 tagcache_search_finish(&tcs);
1838 return -2;
1841 tagcache_search_finish(&tcs);
1843 return 0;
1846 static bool insert_all_playlist(struct tree_context *c, int position, bool queue)
1848 struct tagcache_search tcs;
1849 int i;
1850 char buf[MAX_PATH];
1851 int from, to, direction;
1852 int files_left = c->filesindir;
1854 cpu_boost(true);
1855 if (!tagcache_search(&tcs, tag_filename))
1857 splash(HZ, ID2P(LANG_TAGCACHE_BUSY));
1858 cpu_boost(false);
1859 return false;
1862 if (position == PLAYLIST_REPLACE)
1864 if (playlist_remove_all_tracks(NULL) == 0)
1865 position = PLAYLIST_INSERT_LAST;
1866 else
1868 cpu_boost(false);
1869 return false;
1873 if (position == PLAYLIST_INSERT_FIRST)
1875 from = c->filesindir - 1;
1876 to = -1;
1877 direction = -1;
1879 else
1881 from = 0;
1882 to = c->filesindir;
1883 direction = 1;
1886 for (i = from; i != to; i += direction)
1888 /* Count back to zero */
1889 if (!show_search_progress(false, files_left--))
1890 break;
1892 if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, i)->extraseek,
1893 tcs.type, buf, sizeof buf))
1895 continue;
1898 if (playlist_insert_track(NULL, buf, position, queue, false) < 0)
1900 logf("playlist_insert_track failed");
1901 break;
1903 yield();
1905 playlist_sync(NULL);
1906 tagcache_search_finish(&tcs);
1907 cpu_boost(false);
1909 return true;
1912 bool tagtree_insert_selection_playlist(int position, bool queue)
1914 char buf[MAX_PATH];
1915 int dirlevel = tc->dirlevel;
1916 int newtable;
1918 show_search_progress(
1919 #ifdef HAVE_DISK_STORAGE
1920 storage_disk_is_active()
1921 #else
1922 true
1923 #endif
1924 , 0);
1927 /* We need to set the table to allsubentries. */
1928 newtable = tagtree_get_entry(tc, tc->selected_item)->newtable;
1930 /* Insert a single track? */
1931 if (newtable == PLAYTRACK)
1933 if (tagtree_get_filename(tc, buf, sizeof buf) < 0)
1935 logf("tagtree_get_filename failed");
1936 return false;
1938 playlist_insert_track(NULL, buf, position, queue, true);
1940 return true;
1943 if (newtable == NAVIBROWSE)
1945 tagtree_enter(tc);
1946 tagtree_load(tc);
1947 newtable = tagtree_get_entry(tc, tc->selected_item)->newtable;
1949 else if (newtable != ALLSUBENTRIES)
1951 logf("unsupported table: %d", newtable);
1952 return false;
1955 /* Now the current table should be allsubentries. */
1956 if (newtable != PLAYTRACK)
1958 tagtree_enter(tc);
1959 tagtree_load(tc);
1960 newtable = tagtree_get_entry(tc, tc->selected_item)->newtable;
1962 /* And now the newtable should be playtrack. */
1963 if (newtable != PLAYTRACK)
1965 logf("newtable: %d !!", newtable);
1966 tc->dirlevel = dirlevel;
1967 return false;
1971 if (tc->filesindir <= 0)
1972 splash(HZ, ID2P(LANG_END_PLAYLIST));
1973 else
1975 logf("insert_all_playlist");
1976 if (!insert_all_playlist(tc, position, queue))
1977 splash(HZ*2, ID2P(LANG_FAILED));
1980 /* Finally return the dirlevel to its original value. */
1981 while (tc->dirlevel > dirlevel)
1982 tagtree_exit(tc);
1983 tagtree_load(tc);
1985 return true;
1988 static int tagtree_play_folder(struct tree_context* c)
1990 if (playlist_create(NULL, NULL) < 0)
1992 logf("Failed creating playlist\n");
1993 return -1;
1996 if (!insert_all_playlist(c, PLAYLIST_INSERT_LAST, false))
1997 return -2;
1999 if (global_settings.playlist_shuffle)
2000 c->selected_item = playlist_shuffle(current_tick, c->selected_item);
2001 if (!global_settings.play_selected)
2002 c->selected_item = 0;
2003 gui_synclist_select_item(&tree_lists, c->selected_item);
2005 playlist_start(c->selected_item,0);
2006 playlist_get_current()->num_inserted_tracks = 0; /* make warn on playlist erase work */
2007 return 0;
2010 static struct tagentry* tagtree_get_entry(struct tree_context *c, int id)
2012 struct tagentry *entry;
2013 int realid = id - current_offset;
2015 /* Load the next chunk if necessary. */
2016 if (realid >= current_entry_count || realid < 0)
2018 cpu_boost(true);
2019 if (retrieve_entries(c, MAX(0, id - (current_entry_count / 2)),
2020 false) < 0)
2022 logf("retrieve failed");
2023 cpu_boost(false);
2024 return NULL;
2026 realid = id - current_offset;
2027 cpu_boost(false);
2030 entry = get_entries(c);
2031 return &entry[realid];
2034 char* tagtree_get_entry_name(struct tree_context *c, int id,
2035 char* buf, size_t bufsize)
2037 struct tagentry *entry = tagtree_get_entry(c, id);
2038 if (!entry)
2039 return NULL;
2040 strlcpy(buf, entry->name, bufsize);
2041 return buf;
2045 char *tagtree_get_title(struct tree_context* c)
2047 switch (c->currtable)
2049 case ROOT:
2050 return menu->title;
2052 case NAVIBROWSE:
2053 case ALLSUBENTRIES:
2054 return current_title[c->currextra];
2057 return "?";
2060 int tagtree_get_attr(struct tree_context* c)
2062 int attr = -1;
2063 switch (c->currtable)
2065 case NAVIBROWSE:
2066 if (csi->tagorder[c->currextra] == tag_title)
2067 attr = FILE_ATTR_AUDIO;
2068 else
2069 attr = ATTR_DIRECTORY;
2070 break;
2072 case ALLSUBENTRIES:
2073 attr = FILE_ATTR_AUDIO;
2074 break;
2076 default:
2077 attr = ATTR_DIRECTORY;
2078 break;
2081 return attr;
2084 int tagtree_get_icon(struct tree_context* c)
2086 int icon = Icon_Folder;
2088 if (tagtree_get_attr(c) == FILE_ATTR_AUDIO)
2089 icon = Icon_Audio;
2091 return icon;