summaryrefslogtreecommitdiffstats
path: root/apps/plugins/tagcache/tagcache.c
blob: f86c10d3dd5e48941796093c508ccde1484f6e0a (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2023 by William Wilgus
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

/*Plugin Includes*/

#include "plugin.h"
/* Redefinitons of ANSI C functions. */
#include "lib/wrappers.h"
#include "lib/helper.h"

static void thread_create(void);
static void thread(void); /* the thread running it all */
static void allocate_tempbuf(void);
static void free_tempbuf(void);
static bool do_timed_yield(void);
static void _log(const char *fmt, ...);
/*Aliases*/
#if 0
#ifdef ROCKBOX_HAS_LOGF
    #define logf         rb->logf
#else
    #define logf(...)   {}
#endif
#endif


#define logf _log
#define sleep rb->sleep
#define qsort rb->qsort

#define write(x,y,z)   rb->write(x,y,z)
#define ftruncate rb->ftruncate
#define remove rb->remove

#define vsnprintf    rb->vsnprintf
#define mkdir        rb->mkdir
#define filesize     rb->filesize

#define strtok_r     rb->strtok_r
#define strncasecmp  rb->strncasecmp
#define strcasecmp   rb->strcasecmp

#define current_tick (*rb->current_tick)
#define crc_32(x,y,z)   rb->crc_32(x,y,z)


#if !defined(SIMULATOR) && !defined(APPLICATION)

#define errno  (*rb->__errno())
#endif
#define ENOENT 2        /* No such file or directory */
#define EEXIST 17       /* File exists */
#define MAX_LOG_SIZE 16384

#define EV_EXIT        MAKE_SYS_EVENT(SYS_EVENT_CLS_PRIVATE, 0xFF)
#define EV_ACTION      MAKE_SYS_EVENT(SYS_EVENT_CLS_PRIVATE, 0x02)
#define EV_STARTUP     MAKE_SYS_EVENT(SYS_EVENT_CLS_PRIVATE, 0x01)

/* communication to the worker thread */
static struct
{
    int user_index;
    bool exiting; /* signal to the thread that we want to exit */
    bool resume;
    unsigned int id; /* worker thread id */
    struct  event_queue queue; /* thread event queue */
    long    last_useraction_tick;
} gThread;


/*Support Fns*/
/* open but with a builtin printf for assembling the path */
int open_pathfmt(char *buf, size_t size, int oflag, const char *pathfmt, ...)
{
    va_list ap;
    va_start(ap, pathfmt);
    vsnprintf(buf, size, pathfmt, ap);
    va_end(ap);
    if ((oflag & O_PATH) == O_PATH)
        return -1;
    int handle = open(buf, oflag, 0666);
    //logf("Open: %s %d flag: %x", buf, handle, oflag);
    return handle;
}

static void sleep_yield(void)
{
    rb->queue_remove_from_head(&gThread.queue, EV_ACTION);
    rb->queue_post(&gThread.queue, EV_ACTION, 0);
    sleep(1);
    #undef yield
    rb->yield();
    #define yield sleep_yield
}

/* make sure tag can be displayed by font pf*/
static bool text_is_displayable(struct font *pf, unsigned char *src)
{
    unsigned short code;
    const unsigned char *ptr = src;
    while(*ptr)
    {
        ptr = rb->utf8decode(ptr, &code);

        if(!rb->font_get_bits(pf, code))
        {
            return false;
        }
    }
    return true;
}

/* callback for each tag if false returned tag will not be added */
bool user_check_tag(int index_type, char* build_idx_buf)
{
    static struct font *pf = NULL;
    if (!pf)
        pf = rb->font_get(FONT_UI);

    if (index_type == tag_artist || index_type == tag_album ||
        index_type == tag_genre || index_type == tag_title ||
        index_type == tag_composer || index_type == tag_comment ||
        index_type == tag_albumartist || index_type == tag_virt_canonicalartist)
    {
        /* this could be expanded with more rules / transformations */
        if (rb->utf8length(build_idx_buf) != strlen(build_idx_buf))
        {
            if (!text_is_displayable(pf, build_idx_buf))
            {
                logf("Can't display (%d) %s", index_type, build_idx_buf);
            }
        }
    }
    return true;
}

/* undef features we don't need */
#undef HAVE_DIRCACHE
#undef HAVE_TC_RAMCACHE
#undef HAVE_EEPROM_SETTINGS
/* paste the whole tagcache.c file */
#include "../tagcache.c"

static bool logdump(bool append);
static unsigned char logbuffer[MAX_LOG_SIZE + 1];
static int log_font_h = -1;
static int logindex;
static bool logwrap;
static bool logenabled = true;

static void check_logindex(void)
{
    if(logindex >= MAX_LOG_SIZE)
    {
        /* wrap */
        logdump(true);
        //logwrap = true;
        logindex = 0;
    }
}

static int log_push(void *userp, int c)
{
    (void)userp;

    logbuffer[logindex++] = c;
    check_logindex();

    return 1;
}

/* our logf function */
static void _log(const char *fmt, ...)
{
    if (!logenabled)
    {
        rb->splash(10, "log not enabled");
        return;
    }


    #ifdef USB_ENABLE_SERIAL
    int old_logindex = logindex;
    #endif
    va_list ap;

    va_start(ap, fmt);

#if (CONFIG_PLATFORM & PLATFORM_HOSTED)
    char buf[1024];
    vsnprintf(buf, sizeof buf, fmt, ap);
    DEBUGF("%s\n", buf);
    /* restart va_list otherwise the result if undefined when vuprintf is called */
    va_end(ap);
    va_start(ap, fmt);
#endif

    rb->vuprintf(log_push, NULL, fmt, ap);
    va_end(ap);

    /* add trailing zero */
    log_push(NULL, '\0');
}


int compute_nb_lines(int w, struct font* font)
{
    int i, nb_lines;
    int cur_x, delta_x;

    if(logindex>= MAX_LOG_SIZE || (logindex == 0 && !logwrap))
        return 0;

    if(logwrap)
        i = logindex;
    else
        i = 0;

    cur_x = 0;
    nb_lines = 0;

    do {
        if(logbuffer[i] == '\0')
        {
            cur_x = 0;
            nb_lines++;
        }
        else
        {
            /* does character fit on this line ? */
            delta_x = rb->font_get_width(font, logbuffer[i]);

            if(cur_x + delta_x > w)
            {
                cur_x = 0;
                nb_lines++;
            }

            /* update pointer */
            cur_x += delta_x;
        }

        i++;
        if(i >= MAX_LOG_SIZE)
            i = 0;
    } while(i != logindex);

    return nb_lines;
}

static bool logdisplay(void)
{

    int w, h, i, index;
    int fontnr;
    int cur_x, cur_y, delta_x;
    struct font* font;

    char buf[2];

    fontnr = FONT_FIRSTUSERFONT;
    font = rb->font_get(fontnr);
    buf[1] = '\0';
    w = LCD_WIDTH;
    h = LCD_HEIGHT;

    if (log_font_h < 0) /* init, get the horizontal size of each line */
    {
        rb->font_getstringsize("A", NULL, &log_font_h, fontnr);
        /* start at the end of the log */
        gThread.user_index = compute_nb_lines(w, font) - h/log_font_h -1;
        /* user_index will be number of the first line to display (warning: line!=log entry) */
        /* if negative, will be set 0 to zero later */
    }


    rb->lcd_clear_display();

    if(gThread.user_index < 0 || gThread.user_index >= MAX_LOG_SIZE)
        gThread.user_index = 0;


    if(logwrap)
        i = logindex;
    else
        i = 0;

    index = 0;
    cur_x = 0;
    cur_y = 0;

    /* nothing to print ? */
    if(logindex == 0 && !logwrap)
        goto end_print;

    do {
        if(logbuffer[i] == '\0')
        {
            /* should be display a newline ? */
            if(index >= gThread.user_index)
                cur_y += log_font_h;
            cur_x = 0;
            index++;
        }
        else
        {
            /* does character fit on this line ? */
            delta_x = rb->font_get_width(font, logbuffer[i]);

            if(cur_x + delta_x > w)
            {
                /* should be display a newline ? */
                if(index >= gThread.user_index)
                    cur_y += log_font_h;
                cur_x = 0;
                index++;
            }

            /* should we print character ? */
            if(index >= gThread.user_index)
            {
                buf[0] = logbuffer[i];
                rb->lcd_putsxy(cur_x, cur_y, buf);
            }

            /* update pointer */
            cur_x += delta_x;
        }
        i++;
        /* did we fill the screen ? */
        if(cur_y > h - log_font_h)
        {
            if (TIME_AFTER(current_tick, gThread.last_useraction_tick + HZ))
                gThread.user_index++;
            break;
        }

        if(i >= MAX_LOG_SIZE)
            i = 0;
    } while(i != logindex);

    end_print:
    rb->lcd_update();

    return false;
}

static bool logdump(bool append)
{
    int fd;
    int flags = O_CREAT|O_WRONLY|O_TRUNC;
    /* nothing to print ? */
    if(!logenabled || (logindex == 0 && !logwrap))
    {
        /* nothing is logged just yet */
        return false;
    }
    if (!append)
    {
        flags = O_CREAT|O_WRONLY|O_APPEND;
    }

    fd = open(ROCKBOX_DIR "/db_commit_log.txt", flags, 0666);
    logenabled = false;
    if(-1 != fd) {
        int i;

        if(logwrap)
            i = logindex;
        else
            i = 0;

        do {
            if(logbuffer[i]=='\0')
                rb->fdprintf(fd, "\n");
            else
                rb->fdprintf(fd, "%c", logbuffer[i]);

            i++;
            if(i >= MAX_LOG_SIZE)
                i = 0;
        } while(i != logindex);

        close(fd);
    }

    logenabled = true;

    return false;
}

static void allocate_tempbuf(void)
{
    tempbuf_size = 0;
    tempbuf = rb->plugin_get_audio_buffer(&tempbuf_size);
    tempbuf_size &= ~0x03;

}

static void free_tempbuf(void)
{
    if (tempbuf_size == 0)
        return ;

    rb->plugin_release_audio_buffer();
    tempbuf = NULL;
    tempbuf_size = 0;
}

static bool do_timed_yield(void)
{
    /* Sorting can lock up for quite a while, so yield occasionally */
    static long wakeup_tick = 0;
    if (TIME_AFTER(current_tick, wakeup_tick))
    {

        yield();

        wakeup_tick = current_tick + (HZ/5);
        return true;
    }
    return false;
}

/*-----------------------------------------------------------------------------*/
/******* plugin_start *******                                                  */
/*-----------------------------------------------------------------------------*/

enum plugin_status plugin_start(const void* parameter)
{
    (void) parameter;

    /* Turn off backlight timeout */
    backlight_ignore_timeout();

    memset(&tc_stat, 0, sizeof(struct tagcache_stat));
    memset(&current_tcmh, 0, sizeof(struct master_header));
    filenametag_fd = -1;

    strlcpy(tc_stat.db_path, rb->global_settings->tagcache_db_path, sizeof(tc_stat.db_path));
    if (!rb->dir_exists(tc_stat.db_path)) /* on fail use default DB path */
        strlcpy(tc_stat.db_path, ROCKBOX_DIR, sizeof(tc_stat.db_path));
    tc_stat.initialized = true;

    memset(&gThread, 0, sizeof(gThread));
    logf("started");
    logdump(false);

    thread_create();
    gThread.user_index = 0;
    logdisplay(); /* get something on the screen while user waits */

    allocate_tempbuf();

    commit();

    if (tc_stat.ready)
        rb->tagcache_commit_finalize();

    free_tempbuf();

    logdump(true);
    gThread.user_index++;
    logdisplay();
    rb->thread_wait(gThread.id);
    rb->queue_delete(&gThread.queue);

    /* Turn on backlight timeout (revert to settings) */
    backlight_use_settings();
    return PLUGIN_OK;
}

/****************** main thread + helper ******************/
static void thread(void)
{
    struct queue_event ev;
    while (!gThread.exiting)
    {
        rb->queue_wait_w_tmo(&gThread.queue, &ev, 1);
        switch (ev.id)
        {
            case SYS_USB_CONNECTED:
                rb->usb_acknowledge(SYS_USB_CONNECTED_ACK);
                logenabled = false;
                break;
            case SYS_USB_DISCONNECTED:
                logenabled = true;
                /*fall through*/
            case EV_STARTUP:
                logf("Thread Started");
                break;
            case EV_EXIT:
                return;
            default:
                break;
        }
        logdisplay();

        int action = rb->get_action(CONTEXT_STD, HZ/10);

        switch( action )
        {
            case ACTION_NONE:
                break;
            case ACTION_STD_NEXT:
            case ACTION_STD_NEXTREPEAT:
                gThread.last_useraction_tick = current_tick;
                gThread.user_index++;
                break;
            case ACTION_STD_PREV:
            case ACTION_STD_PREVREPEAT:
                gThread.last_useraction_tick = current_tick;
                gThread.user_index--;
                break;
            case ACTION_STD_OK:
                gThread.last_useraction_tick = current_tick;
                gThread.user_index = 0;
                break;
            case SYS_POWEROFF:
            case ACTION_STD_CANCEL:
                gThread.exiting = true;
                return;
#ifdef HAVE_TOUCHSCREEN
            case ACTION_TOUCHSCREEN:
            {
                gThread.last_useraction_tick = current_tick;
                short x, y;
                static int prev_y;

                action = action_get_touchscreen_press(&x, &y);

                if(action & BUTTON_REL)
                    prev_y = 0;
                else
                {
                    if(prev_y != 0)
                        gThread.user_index += (prev_y - y) / log_font_h;

                    prev_y = y;
                }
            }
#endif
            default:
                break;
        }


        yield();
    }
}

static void thread_create(void)
{
    /* put the thread's queue in the bcast list */
    rb->queue_init(&gThread.queue, true);
    /*Note: tagcache_stack is defined in apps/tagcache.c */
    gThread.last_useraction_tick = current_tick;
    gThread.id = rb->create_thread(thread, tagcache_stack, sizeof(tagcache_stack),
                                      0, "db_commit"
                                      IF_PRIO(, PRIORITY_USER_INTERFACE)
                                      IF_COP(, CPU));
    rb->queue_post(&gThread.queue, EV_STARTUP, 0);
    yield();
}