summaryrefslogtreecommitdiffstats
path: root/apps/tagtree.c
blob: bf92d3133c9c88677cb610e467ca1124a01d7c47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 by Miika Pekkarinen
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

/** 
 * Basic structure on this file was copied from dbtree.c and modified to
 * support the tag cache interface.
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "system.h"
#include "kernel.h"
#include "splash.h"
#include "icons.h"
#include "tree.h"
#include "settings.h"
#include "tagcache.h"
#include "tagtree.h"
#include "lang.h"
#include "logf.h"
#include "playlist.h"
#include "keyboard.h"
#include "gui/list.h"
#include "buffer.h"
#include "atoi.h"

#define FILE_SEARCH_INSTRUCTIONS ROCKBOX_DIR "/tagnavi.config"

static int tagtree_play_folder(struct tree_context* c);

static char searchstring[32];
#define MAX_TAGS 5

struct search_instruction {
    char name[64];
    int tagorder[MAX_TAGS];
    int tagorder_count;
    struct tagcache_search_clause clause[MAX_TAGS][TAGCACHE_MAX_CLAUSES];
    int clause_count[MAX_TAGS];
    int result_seek[MAX_TAGS];
};

static struct search_instruction *si, *csi;
static int si_count = 0;
static const char *strp;

static int current_offset;
static int current_entry_count;

static int get_token_str(char *buf, int size)
{
    /* Find the start. */
    while (*strp != '"' && *strp != '\0')
        strp++;

    if (*strp == '\0' || *(++strp) == '\0')
        return -1;
    
    /* Read the data. */
    while (*strp != '"' && *strp != '\0' && --size > 0)
        *(buf++) = *(strp++);
    
    *buf = '\0';
    if (*strp != '"')
        return -2;
    
    strp++;
    
    return 0;
}

#define MATCH(tag,str1,str2,settag) \
    if (!strcasecmp(str1, str2)) { \
        *tag = settag; \
        return 1; \
    }

static int get_tag(int *tag)
{
    char buf[32];
    int i;
    
    /* Find the start. */
    while (*strp == ' ' && *strp != '\0')
        strp++;
    
    if (*strp == '\0')
        return 0;
    
    for (i = 0; i < (int)sizeof(buf)-1; i++)
    {
        if (*strp == '\0' || *strp == ' ')
            break ;
        buf[i] = *strp;
        strp++;
    }
    buf[i] = '\0';
    
    MATCH(tag, buf, "album", tag_album);
    MATCH(tag, buf, "artist", tag_artist);
    MATCH(tag, buf, "bitrate", tag_bitrate);
    MATCH(tag, buf, "composer", tag_composer);
    MATCH(tag, buf, "genre", tag_genre);
    MATCH(tag, buf, "length", tag_length);
    MATCH(tag, buf, "title", tag_title);
    MATCH(tag, buf, "tracknum", tag_tracknumber);
    MATCH(tag, buf, "year", tag_year);
    
    logf("NO MATCH: %s\n", buf);
    if (buf[0] == '?')
        return 0;
    
    return -1;
}

static int get_clause(int *condition)
{
    char buf[4];
    int i;
    
    /* Find the start. */
    while (*strp == ' ' && *strp != '\0')
        strp++;
    
    if (*strp == '\0')
        return 0;
    
    for (i = 0; i < (int)sizeof(buf)-1; i++)
    {
        if (*strp == '\0' || *strp == ' ')
            break ;
        buf[i] = *strp;
        strp++;
    }
    buf[i] = '\0';
    
    MATCH(condition, buf, "=", clause_is);
    MATCH(condition, buf, "==", clause_is);
    MATCH(condition, buf, ">", clause_gt);
    MATCH(condition, buf, ">=", clause_gteq);
    MATCH(condition, buf, "<", clause_lt);
    MATCH(condition, buf, "<=", clause_lteq);
    MATCH(condition, buf, "~", clause_contains);
    MATCH(condition, buf, "^", clause_begins_with);
    MATCH(condition, buf, "$", clause_ends_with);
    
    return 0;
}

static bool add_clause(struct search_instruction *inst,
                       int tag, int type, const char *str)
{
    int len = strlen(str);
    struct tagcache_search_clause *clause;
    
    if (inst->clause_count[inst->tagorder_count] >= TAGCACHE_MAX_CLAUSES)
    {
        logf("Too many clauses");
        return false;
    }
    
    clause = &inst->clause[inst->tagorder_count]
        [inst->clause_count[inst->tagorder_count]];
    if (len >= (int)sizeof(clause->str) - 1)
    {
        logf("Too long str in condition");
        return false;
    }
    
    clause->tag = tag;
    clause->type = type;
    if (len == 0)
        clause->input = true;
    else
        clause->input = false;
    
    if (tagcache_is_numeric_tag(tag))
    {
        clause->numeric = true;
        clause->numeric_data = atoi(str);
    }
    else
    {
        clause->numeric = false;
        strcpy(clause->str, str);
    }
    
    inst->clause_count[inst->tagorder_count]++;
    
    return true;
}

static int get_condition(struct search_instruction *inst)
{
    int tag;
    int condition;
    char buf[32];
    
    switch (*strp)
    {
        case '?':
        case ' ':
        case '&':
            strp++;
            return 1;
        case ':':
            strp++;
        case '\0':
            return 0;
    }

    if (get_tag(&tag) <= 0)
        return -1;
    
    if (get_clause(&condition) <= 0)
        return -2;
    
    if (get_token_str(buf, sizeof buf) < 0)
        return -3;
    
    logf("got clause: %d/%d [%s]", tag, condition, buf);
    add_clause(inst, tag, condition, buf);
    
    return 1;
}

/* example search:
 * "Best" artist ? year >= "2000" & title !^ "crap" & genre = "good genre" \
 *      : album  ? year >= "2000" : songs
 * ^  begins with
 * *  contains
 * $  ends with
 */

static bool parse_search(struct search_instruction *inst, const char *str)
{
    int ret;
    
    memset(inst, 0, sizeof(struct search_instruction));
    strp = str;
    
    if (get_token_str(inst->name, sizeof inst->name) < 0)
    {
        logf("No name found.");
        return false;
    }
    
    while (inst->tagorder_count < MAX_TAGS)
    {
        ret = get_tag(&inst->tagorder[inst->tagorder_count]);
        if (ret < 0) 
        {
            logf("Parse error #1");
            return false;
        }
        
        if (ret == 0)
            break ;
        
        logf("tag: %d", inst->tagorder[inst->tagorder_count]);
        
        while (get_condition(inst) > 0) ;

        inst->tagorder_count++;
    }
    
    return true;
}


static struct tagcache_search tcs;

static int compare(const void *p1, const void *p2)
{
    struct tagentry *e1 = (struct tagentry *)p1;
    struct tagentry *e2 = (struct tagentry *)p2;
    
    return strncasecmp(e1->name, e2->name, MAX_PATH);
}

void tagtree_init(void)
{
    int fd;
    char buf[256];
    int pos = 0;
    
    si_count = 0;
    
    fd = open(FILE_SEARCH_INSTRUCTIONS, O_RDONLY);
    if (fd < 0)
    {
        logf("Search instruction file not found.");
        return ;
    }
    
    si = (struct search_instruction *)(((long)audiobuf & ~0x03) + 0x04);
    
    while ( 1 )
    {
        char *p;
        char *next = NULL;
        int rc;
        
        rc = read(fd, &buf[pos], sizeof(buf)-pos-1);
        if (rc >= 0)
            buf[pos+rc] = '\0';
        
        if ( (p = strchr(buf, '\r')) != NULL)
        {
            *p = '\0';
            next = ++p;
        }
        else
            p = buf;
        
        if ( (p = strchr(p, '\n')) != NULL)
        {
            *p = '\0';
            next = ++p;
        }
        
        if (!parse_search(si + si_count, buf))
            break;
        si_count++;
        
        if (next)
        {
            pos = sizeof(buf) - ((long)next - (long)buf) - 1;
            memmove(buf, next, pos);
        }
        else
            break ;
    }
    close(fd);
    
    audiobuf += sizeof(struct search_instruction) * si_count + 4;
}

bool show_search_progress(bool init, int count)
{
    static int last_tick = 0;
    
    if (init)
    {
        last_tick = current_tick;
        return true;
    }
    
    if (current_tick - last_tick > HZ/2)
    {
        gui_syncsplash(0, true, str(LANG_PLAYLIST_SEARCH_MSG), count,
#if CONFIG_KEYPAD == PLAYER_PAD
                       str(LANG_STOP_ABORT)
#else
                       str(LANG_OFF_ABORT)
#endif
                       );
        if (SETTINGS_CANCEL == button_get(false))
            return false;
        last_tick = current_tick;
    }
    
    return true;
}

int retrieve_entries(struct tree_context *c, struct tagcache_search *tcs, 
                     int offset, bool init)
{
    struct tagentry *dptr = (struct tagentry *)c->dircache;
    int i;
    int namebufused = 0;
    int total_count = 0;
    int extra = c->currextra;
    int tag;
    bool sort = false;
    
    if (init
#ifdef HAVE_TC_RAMCACHE
        && !tagcache_is_ramcache()
#endif
        )
    {
        show_search_progress(true, 0);
        gui_syncsplash(0, true, str(LANG_PLAYLIST_SEARCH_MSG),
                       0, csi->name);
    }
    
    if (c->currtable == allsubentries)
    {
        tag = tag_title;
        extra--;
    }
    else
        tag = csi->tagorder[extra];

    if (!tagcache_search(tcs, tag))
        return -1;
    
    for (i = 0; i < extra; i++)
    {
        tagcache_search_add_filter(tcs, csi->tagorder[i], csi->result_seek[i]);
        sort = true;
    }
    
    for (i = 0; i < csi->clause_count[extra]; i++)
    {
        tagcache_search_add_clause(tcs, &csi->clause[extra][i]);
        sort = true;
    }
    
    current_offset = offset;
    current_entry_count = 0;
    c->dirfull = false;
    
    if (tag != tag_title)
    {
        if (offset == 0)
        {
            dptr->newtable = allsubentries;
            dptr->name = str(LANG_TAGNAVI_ALL_TRACKS);
            dptr++;
            current_entry_count++;
        }
        total_count++;
    }
    
    while (tagcache_get_next(tcs))
    {
        if (total_count++ < offset)
            continue;
        
        dptr->newtable = navibrowse;
        dptr->extraseek = tcs->result_seek;
        if (!tcs->ramsearch || tag == tag_title)
        {
            int tracknum = -1;
            
            dptr->name = &c->name_buffer[namebufused];
            if (tag == tag_title)
            {
                dptr->newtable = playtrack;
                if (c->currtable != allsubentries && c->dirlevel > 1)
                    tracknum = tagcache_get_numeric(tcs, tag_tracknumber);
            }
            
            if (tracknum > 0)
            {
                snprintf(dptr->name, c->name_buffer_size - namebufused, "%02d. %s",
                         tracknum, tcs->result);
                namebufused += strlen(dptr->name) + 1;
                if (namebufused >= c->name_buffer_size)
                {
                    logf("chunk mode #1: %d", current_entry_count);
                    c->dirfull = true;
                    sort = false;
                    break ;
                }
            }
            else
            {
                namebufused += tcs->result_len;
                if (namebufused >= c->name_buffer_size)
                {
                    logf("chunk mode #2: %d", current_entry_count);
                    c->dirfull = true;
                    sort = false;
                    break ;
                }
                strcpy(dptr->name, tcs->result);
            }
        }
        else
            dptr->name = tcs->result;
        
        dptr++;
        current_entry_count++;

        if (current_entry_count >= global_settings.max_files_in_dir)
        {
            logf("chunk mode #3: %d", current_entry_count);
            c->dirfull = true;
            sort = false;
            break ;
        }
        
        if (init && !tcs->ramsearch)
        {
            if (!show_search_progress(false, i))
            {
                tagcache_search_finish(tcs);
                return current_entry_count;
            }
        }
    }
    
    if (sort)
        qsort(c->dircache, current_entry_count, c->dentry_size, compare);
    
    if (!init)
    {
        tagcache_search_finish(tcs);
        return current_entry_count;
    }
    
    while (tagcache_get_next(tcs))
    {
        if (!tcs->ramsearch)
        {
            if (!show_search_progress(false, total_count))
                break;
        }
        total_count++;
    }
    
    tagcache_search_finish(tcs);
    return total_count;
}

static int load_root(struct tree_context *c)
{
    struct tagentry *dptr = (struct tagentry *)c->dircache;
    int i;
    
    c->currtable = root;
    for (i = 0; i < si_count; i++)
    {
        dptr->name = (si+i)->name;
        dptr->newtable = navibrowse;
        dptr->extraseek = i;
        dptr++;
    }
    
    current_offset = 0;
    current_entry_count = i;
    
    return i;
}

int tagtree_load(struct tree_context* c)
{
    int count;
    int table = c->currtable;
    
    c->dentry_size = sizeof(struct tagentry);

    if (!table)
    {
        c->dirfull = false;
        table = root;
        c->currtable = table;
    }

    switch (table) 
    {
        case root:
            count = load_root(c);
            break;

        case allsubentries:
        case navibrowse:
            logf("navibrowse...");
            count = retrieve_entries(c, &tcs, 0, true);
            break;
        
        default:
            logf("Unsupported table %d\n", table);
            return -1;
    }
    
    if (count < 0)
    {
        c->dirlevel = 0;
        count = load_root(c);
        gui_syncsplash(HZ, true, str(LANG_TAGCACHE_BUSY));
    }

    /* The _total_ numer of entries available. */
    c->dirlength = c->filesindir = count;
    
    return count;
}

int tagtree_enter(struct tree_context* c)
{
    int rc = 0;
    struct tagentry *dptr;
    int newextra;
    int seek;

    dptr = tagtree_get_entry(c, c->selected_item);
    
    c->dirfull = false;
    newextra = dptr->newtable;
    seek = dptr->extraseek;

    if (c->dirlevel >= MAX_DIR_LEVELS)
        return 0;

    c->selected_item_history[c->dirlevel]=c->selected_item;
    c->table_history[c->dirlevel] = c->currtable;
    c->extra_history[c->dirlevel] = c->currextra;
    c->pos_history[c->dirlevel] = c->firstpos;
    c->dirlevel++;

    switch (c->currtable) {
        case root:
            c->currextra = newextra;
        
            if (newextra == navibrowse)
            {
                int i, j;
                
                csi = si+seek;
                c->currextra = 0;
                
                /* Read input as necessary. */
                for (i = 0; i < csi->tagorder_count; i++)
                {
                    for (j = 0; j < csi->clause_count[i]; j++)
                    {
                        if (!csi->clause[i][j].input)
                            continue;
                        
                        rc = kbd_input(searchstring, sizeof(searchstring));
                        if (rc == -1 || !searchstring[0])
                        {
                            c->dirlevel--;
                            return 0;
                        }
                        
                        if (csi->clause[i][j].numeric)
                            csi->clause[i][j].numeric_data = atoi(searchstring);
                        else
                            strncpy(csi->clause[i][j].str, searchstring,
                                    sizeof(csi->clause[i][j].str)-1);
                    }
                }
            }
            c->currtable = newextra;
        
            break;

        case navibrowse:
        case allsubentries:
            if (newextra == playtrack)
            {
                c->dirlevel--;
                if (tagtree_play_folder(c) >= 0)
                    rc = 2;
                break;
            }

            c->currtable = newextra;
            csi->result_seek[c->currextra] = seek;
            if (c->currextra < csi->tagorder_count-1)
                c->currextra++;
            else
                c->dirlevel--;
            break;
        
        default:
            c->dirlevel--;
            break;
    }
    
    c->selected_item=0;
    gui_synclist_select_item(&tree_lists, c->selected_item);

    return rc;
}

void tagtree_exit(struct tree_context* c)
{
    c->dirfull = false;
    c->dirlevel--;
    c->selected_item=c->selected_item_history[c->dirlevel];
    gui_synclist_select_item(&tree_lists, c->selected_item);
    c->currtable = c->table_history[c->dirlevel];
    c->currextra = c->extra_history[c->dirlevel];
    c->firstpos  = c->pos_history[c->dirlevel];

    /* Just to be sure when chunked browsing is used. */
    tagcache_search_finish(&tcs);
}

int tagtree_get_filename(struct tree_context* c, char *buf, int buflen)
{
    struct tagentry *entry;
    
    entry = tagtree_get_entry(c, c->selected_item);

    if (!tagcache_search(&tcs, tag_filename))
        return -1;

    tagcache_search_add_filter(&tcs, tag_title, entry->extraseek);
    
    if (!tagcache_get_next(&tcs))
    {
        tagcache_search_finish(&tcs);
        return -2;
    }

    strncpy(buf, tcs.result, buflen-1);
    tagcache_search_finish(&tcs);
    
    return 0;
}
   
static int tagtree_play_folder(struct tree_context* c)
{
    int i;
    char buf[MAX_PATH];

    if (playlist_create(NULL, NULL) < 0)
    {
        logf("Failed creating playlist\n");
        return -1;
    }

    cpu_boost(true);
    if (!tagcache_search(&tcs, tag_filename))
    {
        gui_syncsplash(HZ, true, str(LANG_TAGCACHE_BUSY));
        return -1;
    }
    
    for (i=0; i < c->filesindir; i++)
    {
        if (!show_search_progress(false, i))
            break;
        
        if (!tagcache_retrieve(&tcs, tagtree_get_entry(c, i)->extraseek, 
                               buf, sizeof buf))
        {
            continue;
        }

        playlist_insert_track(NULL, buf, PLAYLIST_INSERT, false);
    }
    tagcache_search_finish(&tcs);
    cpu_boost(false);
    
    if (global_settings.playlist_shuffle)
        c->selected_item = playlist_shuffle(current_tick, c->selected_item);
    if (!global_settings.play_selected)
        c->selected_item = 0;
    gui_synclist_select_item(&tree_lists, c->selected_item);

    playlist_start(c->selected_item,0);

    return 0;
}

struct tagentry* tagtree_get_entry(struct tree_context *c, int id)
{
    struct tagentry *entry = (struct tagentry *)c->dircache;
    int realid = id - current_offset;
    
    /* Load the next chunk if necessary. */
    if (realid >= current_entry_count || realid < 0)
    {
        if (retrieve_entries(c, &tcs, MAX(0, id - (current_entry_count / 2)), 
                             false) < 0)
        {
            return NULL;
        }
        realid = id - current_offset;
    }
    
    return &entry[realid];
}

#ifdef HAVE_LCD_BITMAP
const char* tagtree_get_icon(struct tree_context* c)
#else
int   tagtree_get_icon(struct tree_context* c)
#endif
{
    int icon;

    switch (c->currtable)
    {
        case navibrowse:
            if (csi->tagorder[c->currextra] == tag_title)
                icon = Icon_Audio;
            else
                icon = Icon_Folder;
            break;

        case allsubentries:
            icon = Icon_Audio;
            break;
        
        default:
            icon = Icon_Folder;
            break;
    }

#ifdef HAVE_LCD_BITMAP
    return (char *)bitmap_icons_6x8[icon];
#else
    return icon;
#endif
}