summaryrefslogtreecommitdiff
path: root/src/libaudcore/plugin-registry.cc
blob: b5e81671dc9b021871548cd09b9be1b2157f1343 (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
/*
 * plugin-registry.c
 * Copyright 2009-2013 John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include "plugins-internal.h"

#include <errno.h>
#include <string.h>

#include <glib/gstdio.h>

#include "audstrings.h"
#include "i18n.h"
#include "interface.h"
#include "parse.h"
#include "plugin.h"
#include "runtime.h"
#include "threads.h"

#define FILENAME "plugin-registry"

/* Increment this when the format of the plugin-registry file changes.
 * Add 10 if the format changes in a way that will break
 * parse_plugins_fallback(). */
#define FORMAT 11

/* Oldest file format supported by parse_plugins_fallback() */
#define MIN_FORMAT 2 // "enabled" flag was added in Audacious 2.4

struct PluginWatch
{
    PluginWatchFunc func;
    void * data;
};

class PluginHandle
{
public:
    String basename, path;
    bool loaded;
    int timestamp, version, flags;
    PluginType type;
    Plugin * header;
    String name, domain;
    int priority;
    int has_about, has_configure;
    PluginEnabled enabled;
    Index<PluginWatch> watches;

    /* for transport plugins */
    Index<String> schemes;

    /* for playlist plugins */
    Index<String> exts;
    int can_save;

    /* for input plugins */
    aud::array<InputKey, Index<String>> keys;
    int has_subtunes, writes_tag;

    PluginHandle(const char * basename, const char * path, bool loaded,
                 int timestamp, int version, int flags, PluginType type,
                 Plugin * header)
        : basename(basename), path(path), loaded(loaded), timestamp(timestamp),
          version(version), flags(flags), type(type), header(header),
          priority(0), has_about(false), has_configure(false),
          enabled((type == PluginType::Transport ||
                   type == PluginType::Playlist || type == PluginType::Input)
                      ? PluginEnabled::Primary
                      : PluginEnabled::Disabled),
          can_save(false), has_subtunes(false), writes_tag(false)
    {
    }

    ~PluginHandle()
    {
        if (watches.len())
            AUDWARN("Plugin watch count not zero at exit!\n");
    }
};

static constexpr aud::array<PluginType, const char *> plugin_type_names = {
    "transport", "playlist", "input",   "effect",
    "output",    "vis",      "general", "iface"};

static constexpr aud::array<InputKey, const char *> input_key_names = {
    "scheme", "ext", "mime"};

static aud::array<PluginType, Index<PluginHandle *>> plugins;
static aud::array<PluginType, Index<PluginHandle *>> compatible;
static aud::mutex mutex;
static bool modified = false;

static StringBuf get_basename(const char * path)
{
    const char * slash = strrchr(path, G_DIR_SEPARATOR);
    const char * dot = slash ? strrchr(slash + 1, '.') : nullptr;

    return dot ? str_copy(slash + 1, dot - (slash + 1)) : StringBuf();
}

static FILE * open_registry_file(const char * mode)
{
    StringBuf path = filename_build({aud_get_path(AudPath::UserDir), FILENAME});
    FILE * handle = g_fopen(path, mode);

    if (!handle && errno != ENOENT)
        AUDWARN("%s: %s\n", (const char *)path, strerror(errno));

    return handle;
}

static void transport_plugin_save(PluginHandle * plugin, FILE * handle)
{
    for (const String & scheme : plugin->schemes)
        fprintf(handle, "scheme %s\n", (const char *)scheme);
}

static void playlist_plugin_save(PluginHandle * plugin, FILE * handle)
{
    for (const String & ext : plugin->exts)
        fprintf(handle, "ext %s\n", (const char *)ext);

    fprintf(handle, "saves %d\n", plugin->can_save);
}

static void input_plugin_save(PluginHandle * plugin, FILE * handle)
{
    for (auto k : aud::range<InputKey>())
    {
        for (const String & key : plugin->keys[k])
            fprintf(handle, "%s %s\n", input_key_names[k], (const char *)key);
    }

    fprintf(handle, "subtunes %d\n", plugin->has_subtunes);
    fprintf(handle, "writes %d\n", plugin->writes_tag);
}

static void plugin_save(PluginHandle * plugin, FILE * handle)
{
    fprintf(handle, "%s %s\n", plugin_type_names[plugin->type],
            (const char *)plugin->path);
    fprintf(handle, "stamp %d\n", plugin->timestamp);
    fprintf(handle, "version %d\n", plugin->version);
    fprintf(handle, "flags %d\n", plugin->flags);
    fprintf(handle, "name %s\n", (const char *)plugin->name);

    if (plugin->domain)
        fprintf(handle, "domain %s\n", (const char *)plugin->domain);

    fprintf(handle, "priority %d\n", plugin->priority);
    fprintf(handle, "about %d\n", plugin->has_about);
    fprintf(handle, "config %d\n", plugin->has_configure);
    fprintf(handle, "enabled %d\n", (int)plugin->enabled);

    if (plugin->type == PluginType::Transport)
        transport_plugin_save(plugin, handle);
    else if (plugin->type == PluginType::Playlist)
        playlist_plugin_save(plugin, handle);
    else if (plugin->type == PluginType::Input)
        input_plugin_save(plugin, handle);
}

void plugin_registry_save()
{
    if (!modified)
        return;

    FILE * handle = open_registry_file("w");
    if (!handle)
        return;

    fprintf(handle, "format %d\n", FORMAT);

    for (auto & list : plugins)
    {
        for (PluginHandle * plugin : list)
            plugin_save(plugin, handle);
    }

    fclose(handle);
    modified = false;
}

void plugin_registry_cleanup()
{
    for (auto & list : plugins)
    {
        for (PluginHandle * plugin : list)
            delete plugin;

        list.clear();
    }

    for (auto & list : compatible)
        list.clear();
}

static void transport_plugin_parse(PluginHandle * plugin, TextParser & parser)
{
    while (1)
    {
        String value = parser.get_str("scheme");
        if (!value)
            break;

        plugin->schemes.append(std::move(value));
        parser.next();
    }
}

static void playlist_plugin_parse(PluginHandle * plugin, TextParser & parser)
{
    while (1)
    {
        String value = parser.get_str("ext");
        if (!value)
            break;

        plugin->exts.append(std::move(value));
        parser.next();
    }

    if (parser.get_int("saves", plugin->can_save))
        parser.next();
}

static void input_plugin_parse(PluginHandle * plugin, TextParser & parser)
{
    for (auto key : aud::range<InputKey>())
    {
        while (1)
        {
            String value = parser.get_str(input_key_names[key]);
            if (!value)
                break;

            plugin->keys[key].append(std::move(value));
            parser.next();
        }
    }

    if (parser.get_int("subtunes", plugin->has_subtunes))
        parser.next();
    if (parser.get_int("writes", plugin->writes_tag))
        parser.next();
}

static bool plugin_parse(TextParser & parser)
{
    PluginType type;
    String path;

    for (auto type2 : aud::range<PluginType>())
    {
        type = type2;
        if ((path = parser.get_str(plugin_type_names[type2])))
            break;
    }

    if (!path)
        return false;

    StringBuf basename = get_basename(path);
    if (!basename)
        return false;

    parser.next();

    int timestamp;
    if (!parser.get_int("stamp", timestamp))
        return false;

    parser.next();

    int version = 0, flags = 0;
    if (parser.get_int("version", version))
        parser.next();
    if (parser.get_int("flags", flags))
        parser.next();

    auto plugin = new PluginHandle(basename, String(), false, timestamp,
                                   version, flags, type, nullptr);

    plugins[type].append(plugin);

    plugin->name = parser.get_str("name");
    if (plugin->name)
        parser.next();

    plugin->domain = parser.get_str("domain");
    if (plugin->domain)
        parser.next();

    if (parser.get_int("priority", plugin->priority))
        parser.next();
    if (parser.get_int("about", plugin->has_about))
        parser.next();
    if (parser.get_int("config", plugin->has_configure))
        parser.next();

    int enabled;
    if (parser.get_int("enabled", enabled))
    {
        plugin->enabled = (PluginEnabled)enabled;
        parser.next();
    }

    if (type == PluginType::Transport)
        transport_plugin_parse(plugin, parser);
    else if (type == PluginType::Playlist)
        playlist_plugin_parse(plugin, parser);
    else if (type == PluginType::Input)
        input_plugin_parse(plugin, parser);

    return true;
}

/* try to migrate enabled status from another version */
static void parse_plugins_fallback(TextParser & parser)
{
    for (; !parser.eof(); parser.next())
    {
        PluginType type;
        String path;
        int enabled;

        for (auto type2 : aud::range<PluginType>())
        {
            type = type2;
            if ((path = parser.get_str(plugin_type_names[type2])))
                break;
        }

        if (!path)
            continue;

        StringBuf basename = get_basename(path);
        if (!basename)
            continue;

        parser.next();

        for (; !parser.eof(); parser.next())
        {
            if (parser.get_int("enabled", enabled))
                break;
        }

        if (parser.eof())
            return;

        // setting timestamp to zero forces a rescan
        auto plugin =
            new PluginHandle(basename, String(), false, 0, 0, 0, type, nullptr);
        plugins[type].append(plugin);
        plugin->enabled = (PluginEnabled)enabled;
    }
}

void plugin_registry_load()
{
    FILE * handle = open_registry_file("r");
    if (!handle)
        return;

    TextParser parser(handle);

    int format;
    if (!parser.get_int("format", format))
        goto ERR;

    parser.next();

    if (format == FORMAT)
    {
        while (plugin_parse(parser))
            continue;
    }
    else if (format >= MIN_FORMAT && format < FORMAT + 10)
        parse_plugins_fallback(parser);

ERR:
    fclose(handle);
}

static int plugin_compare(PluginHandle * const & a, PluginHandle * const & b)
{
    if (a->type < b->type)
        return -1;
    if (a->type > b->type)
        return 1;
    if (a->priority < b->priority)
        return -1;
    if (a->priority > b->priority)
        return 1;

    int diff;
    if ((diff = str_compare(dgettext(a->domain, a->name),
                            dgettext(b->domain, b->name))))
        return diff;

    return str_compare(a->path, b->path);
}

void plugin_registry_prune()
{
    auto check_not_found = [](PluginHandle * plugin) {
        if (plugin->path)
            return false;

        AUDINFO("Plugin not found: %s\n", (const char *)plugin->basename);
        delete plugin;
        return true;
    };

    auto check_incompatible = [](PluginHandle * plugin) {
        if (plugin_check_flags(plugin->flags))
            return false;

        AUDINFO("Incompatible plugin flags: %s\n",
                (const char *)plugin->basename);
        return true;
    };

    for (auto type : aud::range<PluginType>())
    {
        plugins[type].remove_if(check_not_found);
        plugins[type].sort(plugin_compare);
        compatible[type].insert(plugins[type].begin(), 0, plugins[type].len());
        compatible[type].remove_if(check_incompatible);
    }
}

/* Note: If there are multiple plugins with the same basename, this returns only
 * one of them.  Different plugins should be given different basenames. */
static PluginHandle * plugin_lookup_basename(const char * basename,
                                             bool compat_only)
{
    for (auto & list : (compat_only ? compatible : plugins))
    {
        for (PluginHandle * plugin : list)
        {
            if (!strcmp(plugin->basename, basename))
                return plugin;
        }
    }

    return nullptr;
}

EXPORT PluginHandle * aud_plugin_lookup_basename(const char * basename)
{
    return plugin_lookup_basename(basename, true);
}

static void plugin_get_info(PluginHandle * plugin, bool is_new)
{
    Plugin * header = plugin->header;

    plugin->version = header->version;
    plugin->flags = header->info.flags;
    plugin->name = String(header->info.name);
    plugin->domain = String(header->info.domain);
    plugin->has_about = (bool)header->info.about;
    plugin->has_configure = (bool)header->info.prefs;

    if (header->type == PluginType::Transport)
    {
        TransportPlugin * tp = (TransportPlugin *)header;

        plugin->schemes.clear();
        for (const char * scheme : tp->schemes)
            plugin->schemes.append(String(scheme));
    }
    else if (header->type == PluginType::Playlist)
    {
        PlaylistPlugin * pp = (PlaylistPlugin *)header;

        plugin->exts.clear();
        for (const char * ext : pp->extensions)
            plugin->exts.append(String(ext));

        plugin->can_save = pp->can_save;
    }
    else if (header->type == PluginType::Input)
    {
        InputPlugin * ip = (InputPlugin *)header;
        plugin->priority = ip->input_info.priority;

        for (auto k : aud::range<InputKey>())
        {
            plugin->keys[k].clear();
            for (auto key = ip->input_info.keys[k]; key && *key; key++)
                plugin->keys[k].append(String(*key));
        }

        plugin->has_subtunes =
            (ip->input_info.flags & InputPlugin::FlagSubtunes);
        plugin->writes_tag =
            (ip->input_info.flags & InputPlugin::FlagWritesTag);
    }
    else if (header->type == PluginType::Output)
    {
        OutputPlugin * op = (OutputPlugin *)header;
        plugin->priority = 10 - op->priority;
    }
    else if (header->type == PluginType::Effect)
    {
        EffectPlugin * ep = (EffectPlugin *)header;
        plugin->priority = ep->order;
    }
    else if (header->type == PluginType::General)
    {
        GeneralPlugin * gp = (GeneralPlugin *)header;
        if (is_new && gp->enabled_by_default)
            plugin->enabled = PluginEnabled::Primary;
    }
}

void plugin_register(const char * path, int timestamp)
{
    StringBuf basename = get_basename(path);
    if (!basename)
        return;

    PluginHandle * plugin = plugin_lookup_basename(basename, false);

    if (plugin)
    {
        AUDINFO("Register plugin: %s\n", path);
        plugin->path = String(path);

        if (plugin->timestamp != timestamp)
        {
            AUDINFO("Rescan plugin: %s\n", path);
            Plugin * header = plugin_load(path);
            if (!header || header->type != plugin->type)
                return;

            plugin->loaded = true;
            plugin->header = header;
            plugin->timestamp = timestamp;

            plugin_get_info(plugin, false);
            modified = true;
        }
    }
    else
    {
        AUDINFO("New plugin: %s\n", path);
        Plugin * header = plugin_load(path);
        if (!header)
            return;

        plugin =
            new PluginHandle(basename, path, true, timestamp, header->version,
                             header->info.flags, header->type, header);
        plugins[plugin->type].append(plugin);

        plugin_get_info(plugin, true);
        modified = true;
    }
}

EXPORT PluginType aud_plugin_get_type(PluginHandle * plugin)
{
    return plugin->type;
}

EXPORT const char * aud_plugin_get_basename(PluginHandle * plugin)
{
    return plugin->basename;
}

EXPORT const void * aud_plugin_get_header(PluginHandle * plugin)
{
    auto mh = mutex.take();

    if (!plugin->loaded)
    {
        Plugin * header = plugin_load(plugin->path);
        if (header && header->type == plugin->type)
            plugin->header = header;

        plugin->loaded = true;
    }

    return plugin->header;
}

EXPORT PluginHandle * aud_plugin_by_header(const void * header)
{
    for (auto & list : compatible)
    {
        for (PluginHandle * plugin : list)
        {
            if (plugin->header == header)
                return plugin;
        }
    }

    return nullptr;
}

EXPORT const Index<PluginHandle *> & aud_plugin_list(PluginType type)
{
    return compatible[type];
}

EXPORT const char * aud_plugin_get_name(PluginHandle * plugin)
{
    return dgettext(plugin->domain, plugin->name);
}

EXPORT bool aud_plugin_has_about(PluginHandle * plugin)
{
    return plugin->has_about;
}

EXPORT bool aud_plugin_has_configure(PluginHandle * plugin)
{
    return plugin->has_configure;
}

EXPORT bool aud_plugin_get_enabled(PluginHandle * plugin)
{
    return plugin->enabled != PluginEnabled::Disabled;
}

static void plugin_call_watches(PluginHandle * plugin)
{
    auto call_and_check_remove = [=](const PluginWatch & watch) {
        return !watch.func(plugin, watch.data);
    };

    plugin->watches.remove_if(call_and_check_remove);
}

PluginEnabled plugin_get_enabled(PluginHandle * plugin)
{
    return plugin->enabled;
}

void plugin_set_enabled(PluginHandle * plugin, PluginEnabled enabled)
{
    plugin->enabled = enabled;
    plugin_call_watches(plugin);
    modified = true;
}

void plugin_set_failed(PluginHandle * plugin)
{
    plugin->header = nullptr;
    plugin_set_enabled(plugin, PluginEnabled::Disabled);
}

EXPORT void aud_plugin_add_watch(PluginHandle * plugin, PluginWatchFunc func,
                                 void * data)
{
    plugin->watches.append(func, data);
}

EXPORT void aud_plugin_remove_watch(PluginHandle * plugin, PluginWatchFunc func,
                                    void * data)
{
    auto is_match = [=](const PluginWatch & watch) {
        return watch.func == func && watch.data == data;
    };

    plugin->watches.remove_if(is_match);
}

const Index<String> & transport_plugin_get_schemes(PluginHandle * plugin)
{
    return plugin->schemes;
}

bool transport_plugin_has_scheme(PluginHandle * plugin, const char * scheme)
{
    for (String & s : plugin->schemes)
    {
        if (!strcmp(s, scheme))
            return true;
    }

    return false;
}

bool playlist_plugin_can_save(PluginHandle * plugin)
{
    return plugin->can_save;
}

const Index<String> & playlist_plugin_get_exts(PluginHandle * plugin)
{
    return plugin->exts;
}

bool playlist_plugin_has_ext(PluginHandle * plugin, const char * ext)
{
    for (String & e : plugin->exts)
    {
        if (!strcmp_nocase(e, ext))
            return true;
    }

    return false;
}

bool input_plugin_has_key(PluginHandle * plugin, InputKey key,
                          const char * value)
{
    for (String & s : plugin->keys[key])
    {
        if (!strcmp_nocase(s, value))
            return true;
    }

    return false;
}

bool input_plugin_has_subtunes(PluginHandle * plugin)
{
    return plugin->has_subtunes;
}

bool input_plugin_can_write_tuple(PluginHandle * plugin)
{
    return plugin->writes_tag;
}

EXPORT Index<const char *> aud_plugin_get_supported_mime_types()
{
    Index<const char *> mimes;

    for (PluginHandle * p : aud_plugin_list(PluginType::Input))
    {
        if (!aud_plugin_get_enabled(p))
            continue;

        for (auto k : p->keys[InputKey::MIME])
            mimes.append((const char *)k);
    }

    /* sort and remove duplicates */
    /* lambda needed to avoid -Wnoexcept-type with GCC */
    mimes.sort([](const char * a, const char * b) { return strcmp(a, b); });

    int len = mimes.len();
    for (int i = 0; i + 1 < len; i++)
    {
        if (!strcmp(mimes[i], mimes[i + 1]))
            mimes[i] = nullptr;
    }

    mimes.remove_if([](const char * m) { return m == nullptr; });
    mimes.append(nullptr);

    return mimes;
}