summaryrefslogtreecommitdiff
path: root/plugins/artwork/artwork.c
blob: fbbe71d947b3ff4fa53d9d9f7ce831ace6103fdb (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>
#include <fnmatch.h>
#include "../../deadbeef.h"
#include "artwork.h"
#include "lastfm.h"
#include "albumartorg.h"

#define min(x,y) ((x)<(y)?(x):(y))

//#define trace(...) { fprintf(stderr, __VA_ARGS__); }
#define trace(...)

#define DEFAULT_COVER_PATH (PREFIX "/share/deadbeef/pixmaps/noartwork.jpg")
#define DEFAULT_FILEMASK "*cover*.jpg;*front*.jpg"

static DB_artwork_plugin_t plugin;
DB_functions_t *deadbeef;

DB_FILE *current_file;

typedef struct cover_query_s {
    char *fname;
    char *artist;
    char *album;
    artwork_callback callback;
    void *user_data;
    struct cover_query_s *next;
} cover_query_t;

static cover_query_t *queue;
static cover_query_t *queue_tail;
static uintptr_t mutex;
static uintptr_t cond;
static volatile int terminate;
static volatile int clear_queue;
static intptr_t tid;

int artwork_enable_embedded;
int artwork_enable_local;
int artwork_enable_lfm;
int artwork_enable_aao;
int artwork_reset_time;
char artwork_filemask[200];

void
make_cache_dir_path (char *path, int size, const char *album, const char *artist) {
    int sz = snprintf (path, size, "%s/artcache/", deadbeef->get_config_dir ());
    size -= sz;
    path += sz;

    sz = snprintf (path, size, "%s", artist);
    for (char *p = path; *p; p++) {
        if (*p == '/') {
            *p = '_';
        }
    }
}

void
make_cache_path (char *path, int size, const char *album, const char *artist) {
    int sz = snprintf (path, size, "%s/artcache/", deadbeef->get_config_dir ());
    size -= sz;
    path += sz;

    sz = snprintf (path, size, "%s", artist);
    for (char *p = path; *p; p++) {
        if (*p == '/') {
            *p = '_';
        }
    }
    size -= sz;
    path += sz;
    sz = snprintf (path, size, "/%s.jpg", album);
    for (char *p = path+1; *p; p++) {
        if (*p == '/') {
            *p = '_';
        }
    }
}

void
queue_add (const char *fname, const char *artist, const char *album, artwork_callback callback, void *user_data) {
    if (!artist) {
        artist = "";
    }
    if (!album) {
        album = "";
    }
    deadbeef->mutex_lock (mutex);

    for (cover_query_t *q = queue; q; q = q->next) {
        if (!strcasecmp (artist, q->artist) || !strcasecmp (album, q->album)) {
            deadbeef->mutex_unlock (mutex);
            return; // already in queue
        }
    }

    cover_query_t *q = malloc (sizeof (cover_query_t));
    memset (q, 0, sizeof (cover_query_t));
    q->fname = strdup (fname);
    q->artist = strdup (artist);
    q->album = strdup (album);
    q->callback = callback;
    q->user_data = user_data;
    if (queue_tail) {
        queue_tail->next = q;
        queue_tail = q;
    }
    else {
        queue = queue_tail = q;
    }
    deadbeef->mutex_unlock (mutex);
    deadbeef->cond_signal (cond);
}

void
queue_pop (void) {
    deadbeef->mutex_lock (mutex);
    cover_query_t *next = queue ? queue->next : NULL;
    free (queue->fname);
    free (queue->artist);
    free (queue->album);
    free (queue);
    queue = next;
    if (!queue) {
        queue_tail = NULL;
    }
    deadbeef->mutex_unlock (mutex);
}

static int
check_dir (const char *dir, mode_t mode)
{
    char *tmp = strdup (dir);
    char *slash = tmp;
    struct stat stat_buf;
    do
    {
        slash = strstr (slash+1, "/");
        if (slash)
            *slash = 0;
        if (-1 == stat (tmp, &stat_buf))
        {
            trace ("creating dir %s\n", tmp);
            if (0 != mkdir (tmp, mode))
            {
                trace ("Failed to create %s (%d)\n", tmp, errno);
                free (tmp);
                return 0;
            }
        }
        if (slash)
            *slash = '/';
    }
    while (slash);
    free (tmp);
    return 1;
}

#define BUFFER_SIZE 4096

static int
copy_file (const char *in, const char *out) {
    trace ("copying %s to %s\n", in, out);
    FILE *fin = fopen (in, "rb");
    if (!fin) {
        trace ("artwork: failed to open file %s for reading\n", in);
        return -1;
    }
    FILE *fout = fopen (out, "w+b");
    if (!fout) {
        fclose (fin);
        trace ("artwork: failed to open file %s for writing\n", out);
        return -1;
    }
    char *buf = malloc (BUFFER_SIZE);
    if (!buf) {
        trace ("artwork: failed to alloc %d bytes\n", BUFFER_SIZE);
        fclose (fin);
        fclose (fout);
        return -1;
    }

    fseek (fin, 0, SEEK_END);
    size_t sz = ftell (fin);
    rewind (fin);

    while (sz > 0) {
        int rs = min (sz, BUFFER_SIZE);
        if (fread (buf, rs, 1, fin) != 1) {
            trace ("artwork: failed to read file %s\n", in);
            break;
        }
        if (fwrite (buf, rs, 1, fout) != 1) {
            trace ("artwork: failed to write file %s\n", out);
            break;
        }
        sz -= rs;
    }
    free (buf);
    fclose (fin);
    fclose (fout);
    if (sz > 0) {
        unlink (out);
    }
    return 0;
}

static int
filter_custom (const struct dirent *f)
{
    char mask[200] = "";
    char *p = artwork_filemask;
    while (p) {
        *mask = 0;
        char *e = strchr (p, ';');
        if (e) {
            strncpy (mask, p, e-p);
            mask[e-p] = 0;
            e++;
        }
        else {
            strcpy (mask, p);
        }
        if (*mask) {
            if (!fnmatch (mask, f->d_name, FNM_CASEFOLD)) {
                return 1;
            }
        }
        p = e;
    }
    return 0;
}

static int
filter_jpg (const struct dirent *f)
{
    const char *ext = strrchr (f->d_name, '.');
    if (!ext)
        return 0;
    if (!strcasecmp (ext, ".jpg") || !strcasecmp (ext, ".jpeg")) {
        return 1;
    }

    return 0;
}

static uint8_t *
id3v2_skip_str (int enc, uint8_t *ptr, uint8_t *end) {
    if (enc == 0 || enc == 3) {
        while (ptr < end && *ptr) {
            ptr++;
        }
        ptr++;
        if (ptr >= end) {
            return NULL;
        }
        return ptr;
    }
    else {
        while (ptr < end-1 && (ptr[0] || ptr[1])) {
            ptr += 2;
        }
        ptr += 2;
        if (ptr >= end) {
            return NULL;
        }
        return ptr;
    }
    return NULL;
}


static void
fetcher_thread (void *none)
{
    for (;;) {
        trace ("artwork: waiting for signal\n");
        deadbeef->cond_wait (cond, mutex);
        trace ("artwork: cond signalled\n");
        deadbeef->mutex_unlock (mutex);
        while (!terminate && queue && !clear_queue) {
            cover_query_t *param = queue;
            char path [1024];
            struct dirent **files;
            int files_count;

            make_cache_dir_path (path, sizeof (path), param->album, param->artist);
            trace ("cache folder: %s\n", path);
            if (!check_dir (path, 0755)) {
                queue_pop ();
                trace ("failed to create folder for %s %s\n", param->album, param->artist);
                continue;
            }

            trace ("fetching cover for %s %s\n", param->album, param->artist);

            // try to load embedded from id3v2
            if (deadbeef->is_local_file (param->fname)) {
                if (artwork_enable_embedded) {
                    trace ("trying to load artwork from id3v2 tag for %s\n", param->fname);
                    DB_id3v2_tag_t tag;
                    memset (&tag, 0, sizeof (tag));
                    DB_FILE *fp = deadbeef->fopen (param->fname);
                    current_file = fp;
                    int got_id3v2_pic = 0;
                    if (fp) {
                        int res = deadbeef->junk_id3v2_read_full (NULL, &tag, fp);
                        if (!res) {
                            for (DB_id3v2_frame_t *f = tag.frames; f; f = f->next) {
                                if (!strcmp (f->id, "APIC")) {

                                    if (f->size < 20) {
                                        trace ("artwork: id3v2 APIC frame is too small\n");
                                        continue;
                                    }
                                    uint8_t *data = f->data;
                                    uint8_t *end = f->data + f->size;
                                    int enc = *data;
                                    data++; // enc
                                    // mime-type must always be ASCII - hence enc is 0 here
                                    uint8_t *mime_end = id3v2_skip_str (0, data, end);
                                    if (!mime_end) {
                                        trace ("artwork: corrupted id3v2 APIC frame\n");
                                        continue;
                                    }
                                    if (strcasecmp (data, "image/jpeg") && strcasecmp (data, "image/png")) {
                                        trace ("artwork: unsupported mime type: %s\n", data);
                                        continue;
                                    }
                                    if (*mime_end != 3) {
                                        trace ("artwork: picture type=%d, skipped: %s\n", *mime_end);
                                        continue;
                                    }
                                    trace ("artwork: mime-type=%s, picture type: %d\n", data, *mime_end);
                                    data = mime_end;
                                    data++; // picture type
                                    data = id3v2_skip_str (enc, data, end); // description
                                    if (!data) {
                                        trace ("artwork: corrupted id3v2 APIC frame\n");
                                        continue;
                                    }
                                    int sz = f->size - (data - f->data);

                                    char tmp_path[1024];
                                    char cache_path[1024];
                                    make_cache_path (cache_path, sizeof (cache_path), param->album, param->artist);
                                    trace ("will write id3v2 APIC into %s\n", cache_path);
                                    snprintf (tmp_path, sizeof (tmp_path), "%s.part", cache_path);
                                    FILE *out = fopen (tmp_path, "w+b");
                                    if (!out) {
                                        trace ("artwork: failed to open %s for writing\n", tmp_path);
                                        break;
                                    }
                                    if (fwrite (data, 1, sz, out) != sz) {
                                        trace ("artwork: failed to write id3v2 picture into %s\n", tmp_path);
                                        fclose (out);
                                        unlink (tmp_path);
                                        break;
                                    }
                                    fclose (out);
                                    int err = rename (tmp_path, cache_path);
                                    if (err != 0) {
                                        trace ("Failed not move %s to %s: %s\n", tmp_path, cache_path, strerror (err));
                                        unlink (tmp_path);
                                        break;
                                    }
                                    unlink (tmp_path);
                                    got_id3v2_pic = 1;
                                    break;
                                }
                            }
                        }

                        if (got_id3v2_pic) {
                            if (param->callback) {
                                param->callback (param->fname, param->artist, param->album, param->user_data);
                            }
                            queue_pop ();
                            continue;
                        }
                        deadbeef->junk_id3v2_free (&tag);
                        current_file = NULL;
                        deadbeef->fclose (fp);
                    }
                }
            }

            // try to load embedded from apev2
            if (deadbeef->is_local_file (param->fname)) {
                if (artwork_enable_embedded) {
                    trace ("trying to load artwork from apev2 tag for %s\n", param->fname);
                    DB_apev2_tag_t tag;
                    memset (&tag, 0, sizeof (tag));
                    DB_FILE *fp = deadbeef->fopen (param->fname);
                    current_file = fp;
                    int got_apev2_pic = 0;
                    if (fp) {
                        int res = deadbeef->junk_apev2_read_full (NULL, &tag, fp);
                        if (!res) {
                            for (DB_apev2_frame_t *f = tag.frames; f; f = f->next) {
                                if (!strcasecmp (f->key, "cover art (front)")) {
                                    uint8_t *name = f->data, *ext = f->data, *data = f->data;
                                    uint8_t *end = f->data + f->size;
                                    while (data < end && *data)
                                        data++;
                                    if (data == end) {
                                        trace ("artwork: apev2 cover art frame has no name\n");
                                        continue;
                                    }
                                    int sz = end - ++data;
                                    if (sz < 20) {
                                        trace ("artwork: apev2 cover art frame is too small\n");
                                        continue;
                                    }
                                    ext = strrchr (name, '.');
                                    if (!ext || !*++ext) {
                                        trace ("artwork: apev2 cover art name has no extension\n");
                                        continue;
                                    }
                                    if (strcasecmp (ext, "jpeg") && strcasecmp (ext, "jpg") && strcasecmp (ext, "png")) {
                                        trace ("artwork: unsupported file type: %s\n", ext);
                                        continue;
                                    }
                                    trace ("found apev2 cover art of %d bytes (%s)\n", sz, ext);
                                    char tmp_path[1024];
                                    char cache_path[1024];
                                    make_cache_path (cache_path, sizeof (cache_path), param->album, param->artist);
                                    trace ("will write apev2 cover art into %s\n", cache_path);
                                    snprintf (tmp_path, sizeof (tmp_path), "%s.part", cache_path);
                                    FILE *out = fopen (tmp_path, "w+b");
                                    if (!out) {
                                        trace ("artwork: failed to open %s for writing\n", tmp_path);
                                        break;
                                    }
                                    if (fwrite (data, 1, sz, out) != sz) {
                                        trace ("artwork: failed to write apev2 picture into %s\n", tmp_path);
                                        fclose (out);
                                        unlink (tmp_path);
                                        break;
                                    }
                                    fclose (out);
                                    int err = rename (tmp_path, cache_path);
                                    if (err != 0) {
                                        trace ("Failed not move %s to %s: %s\n", tmp_path, cache_path, strerror (err));
                                        unlink (tmp_path);
                                        break;
                                    }
                                    unlink (tmp_path);
                                    got_apev2_pic = 1;
                                    break;
                                }
                            }
                        }

                        if (got_apev2_pic) {
                            if (param->callback) {
                                param->callback (param->fname, param->artist, param->album, param->user_data);
                            }
                            queue_pop ();
                            continue;
                        }
                        deadbeef->junk_apev2_free (&tag);
                        current_file = NULL;
                        deadbeef->fclose (fp);
                    }
                }

                if (artwork_enable_local) {
                    /* Searching in track directory */
                    strncpy (path, param->fname, sizeof (path));
                    char *slash = strrchr (path, '/');
                    if (slash) {
                        *slash = 0; // assuming at least one slash exist
                    }
                    trace ("scanning directory: %s\n", path);
                    files_count = scandir (path, &files, filter_custom, alphasort);
                    if (files_count == 0) {
                        files_count = scandir (path, &files, filter_jpg, alphasort);
                    }

                    if (files_count > 0) {
                        trace ("found cover for %s - %s in local folder\n", param->artist, param->album);
                        if (check_dir (path, 0755)) {
                            strcat (path, "/");
                            strcat (path, files[0]->d_name);
                            char cache_path[1024];
                            char tmp_path[1024];
                            make_cache_path (cache_path, sizeof (cache_path), param->album, param->artist);
                            snprintf (tmp_path, sizeof (tmp_path), "%s.part", cache_path);
                            copy_file (path, tmp_path);
                            int err = rename (tmp_path, cache_path);
                            if (err != 0) {
                                trace ("Failed not move %s to %s: %s\n", tmp_path, cache_path, strerror (err));
                                unlink (tmp_path);
                            }
                            int i;
                            for (i = 0; i < files_count; i++) {
                                free (files [i]);
                            }
                            if (param->callback) {
                                param->callback (param->fname, param->artist, param->album, param->user_data);
                            }
                            queue_pop ();
                            continue;
                        }
                    }
                }
            }

            make_cache_path (path, sizeof (path), param->album, param->artist);

            if (artwork_enable_lfm && !fetch_from_lastfm (param->artist, param->album, path)) {
                trace ("art found on last.fm for %s %s\n", param->album, param->artist);
            }
            else if (artwork_enable_aao && !fetch_from_albumart_org (param->artist, param->album, path)) {
                trace ("art found on albumart.org for %s %s\n", param->album, param->artist);
            }
            else {
                trace ("art not found for %s %s\n", param->album, param->artist);
//                if (param->callback) {
//                    param->callback (DEFAULT_COVER_PATH, param->artist, param->album, param->user_data);
//                }
                queue_pop ();
                continue;
            }

            trace ("downloaded art for %s %s\n", param->album, param->artist);
            if (param->callback) {
                param->callback (param->fname, param->artist, param->album, param->user_data);
            }
            queue_pop ();
        }
        if (clear_queue) {
            trace ("artwork: received queue clear request\n");
            while (queue) {
                queue_pop ();
            }
            clear_queue = 0;
            trace ("artwork: queue clear done\n");
            continue;
        }
        if (terminate) {
            break;
        }
    }
}

char*
get_album_art (const char *fname, const char *artist, const char *album, artwork_callback callback, void *user_data)
{
//    trace ("get_album_art: %s (%s - %s)\n", fname, artist, album);
    char path [1024];

    if (!album) {
        album = "";
    }
    if (!artist) {
        artist = "";
    }

    if (!*artist || !*album)
    {
        //give up
        return strdup (DEFAULT_COVER_PATH);
    }

    if (!deadbeef->is_local_file (fname)) {
        return strdup (DEFAULT_COVER_PATH);
    }

    make_cache_path (path, sizeof (path), album, artist);
    struct stat stat_buf;
    if (0 == stat (path, &stat_buf)) {
        int cache_period = deadbeef->conf_get_int ("artwork.cache.period", 48);
        time_t tm = time (NULL);
        // invalidate cache every 2 days
        if ((cache_period > 0 && (tm - stat_buf.st_mtime > cache_period * 60 * 60))
                || artwork_reset_time > stat_buf.st_mtime) {
            trace ("reloading cached file %s\n", path);
            unlink (path);
            queue_add (fname, artist, album, callback, user_data);
            return strdup (DEFAULT_COVER_PATH);
        }

        trace ("found %s in cache\n", path);
        return strdup (path);
    }

    queue_add (fname, artist, album, callback, user_data);
    return strdup (DEFAULT_COVER_PATH);
}

DB_plugin_t *
artwork_load (DB_functions_t *api) {
    deadbeef = api;
    return DB_PLUGIN (&plugin);
}

void
artwork_reset (int fast) {
    if (fast) {
//        if (current_file) {
//            deadbeef->fabort (current_file);
//        }
        deadbeef->mutex_lock (mutex);
        while (queue && queue->next) {
            cover_query_t *next = queue->next->next;
            free (queue->next->fname);
            free (queue->next->artist);
            free (queue->next->album);
            queue->next = next;
            if (next == NULL) {
                queue_tail = queue;
            }
        }
        deadbeef->mutex_unlock (mutex);
    }
    else {
        trace ("artwork: reset\n");
        clear_queue = 1;
        deadbeef->cond_signal (cond);
        trace ("artwork: waiting for clear to complete\n");
        while (clear_queue) {
            usleep (100000);
        }
    }
}

static int
artwork_on_configchanged (DB_event_t *ev, uintptr_t data) {
    int new_artwork_enable_embedded = deadbeef->conf_get_int ("artwork.enable_embedded", 1);
    int new_artwork_enable_local = deadbeef->conf_get_int ("artwork.enable_localfolder", 1);
    int new_artwork_enable_lfm = deadbeef->conf_get_int ("artwork.enable_lastfm", 0);
    int new_artwork_enable_aao = deadbeef->conf_get_int ("artwork.enable_albumartorg", 0);
    char new_artwork_filemask[200];
    strncpy (new_artwork_filemask, deadbeef->conf_get_str ("artwork.filemask", DEFAULT_FILEMASK), sizeof (new_artwork_filemask));
    new_artwork_filemask[sizeof(new_artwork_filemask)-1] = 0;

    if (new_artwork_enable_embedded != artwork_enable_embedded
            || new_artwork_enable_local != artwork_enable_local
            || new_artwork_enable_lfm != artwork_enable_lfm
            || new_artwork_enable_aao != artwork_enable_aao
            || strcmp (new_artwork_filemask, artwork_filemask)) {
        artwork_enable_embedded = new_artwork_enable_embedded;
        artwork_enable_local = new_artwork_enable_local;
        artwork_enable_lfm = new_artwork_enable_lfm;
        artwork_enable_aao = new_artwork_enable_aao;
        artwork_reset_time = time (NULL);
        strcpy (artwork_filemask, new_artwork_filemask);
        deadbeef->conf_set_int ("artwork.cache_reset_time", artwork_reset_time);
        deadbeef->sendmessage (M_PLAYLISTREFRESH, 0, 0, 0);
    }

    return 0;
}

static int
artwork_plugin_start (void)
{
    terminate = 0;

    artwork_enable_embedded = deadbeef->conf_get_int ("artwork.enable_embedded", 1);
    artwork_enable_local = deadbeef->conf_get_int ("artwork.enable_localfolder", 1);
    artwork_enable_lfm = deadbeef->conf_get_int ("artwork.enable_lastfm", 0);
    artwork_enable_aao = deadbeef->conf_get_int ("artwork.enable_albumartorg", 0);
    artwork_reset_time = deadbeef->conf_get_int ("artwork.cache_reset_time", 0);

    strncpy (artwork_filemask, deadbeef->conf_get_str ("artwork.filemask", DEFAULT_FILEMASK), sizeof (artwork_filemask));
    artwork_filemask[sizeof(artwork_filemask)-1] = 0;

    deadbeef->ev_subscribe (DB_PLUGIN (&plugin), DB_EV_CONFIGCHANGED, DB_CALLBACK (artwork_on_configchanged), 0);

    mutex = deadbeef->mutex_create_nonrecursive ();
    cond = deadbeef->cond_create ();
    tid = deadbeef->thread_start_low_priority (fetcher_thread, NULL);

    return 0;
}

static int
artwork_plugin_stop (void)
{
    deadbeef->ev_unsubscribe (DB_PLUGIN (&plugin), DB_EV_CONFIGCHANGED, DB_CALLBACK (artwork_on_configchanged), 0);
    if (current_file) {
        deadbeef->fabort (current_file);
    }
    if (tid) {
        terminate = 1;
        deadbeef->cond_signal (cond);
        deadbeef->thread_join (tid);
        tid = 0;
    }
    while (queue) {
        queue_pop ();
    }
    if (mutex) {
        deadbeef->mutex_free (mutex);
        mutex = 0;
    }
    if (cond) {
        deadbeef->cond_free (cond);
        cond = 0;
    }

    return 0;
}

static const char settings_dlg[] =
    "property \"Cache update period (hr)\" entry artwork.cache.period 48;\n"
    "property \"Fetch from embedded tags\" checkbox artwork.enable_embedded 1;\n"
    "property \"Fetch from local folder\" checkbox artwork.enable_localfolder 1;\n"
    "property \"Local cover file mask\" entry artwork.filemask \"" DEFAULT_FILEMASK "\";\n"
    "property \"Fetch from last.fm\" checkbox artwork.enable_lastfm 0;\n"
    "property \"Fetch from albumart.org\" checkbox artwork.enable_albumartorg 0;\n"
;
// define plugin interface
static DB_artwork_plugin_t plugin = {
    .plugin.plugin.api_vmajor = DB_API_VERSION_MAJOR,
    .plugin.plugin.api_vminor = DB_API_VERSION_MINOR,
    .plugin.plugin.type = DB_PLUGIN_MISC,
    .plugin.plugin.id = "cover_loader",
    .plugin.plugin.name = "Album Artwork",
    .plugin.plugin.descr = "Loads album artwork either from local directories or from internet",
    .plugin.plugin.author = "Viktor Semykin, Alexey Yakovenko",
    .plugin.plugin.email = "thesame.ml@gmail.com, waker@users.sourceforge.net",
    .plugin.plugin.website = "http://deadbeef.sf.net",
    .plugin.plugin.start = artwork_plugin_start,
    .plugin.plugin.stop = artwork_plugin_stop,
    .plugin.plugin.configdialog = settings_dlg,
    .get_album_art = get_album_art,
    .reset = artwork_reset,
};