summaryrefslogtreecommitdiffhomepage
path: root/opentrack/plugin-support.hpp
blob: 102d11c41b1975850f084c56d101ffdfb403c6f4 (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
/* Copyright (c) 2015 Stanislaw Halik
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 */

#pragma once

#include "plugin-api.hpp"
#include "options.hpp"

#include <QWidget>
#include <QDebug>
#include <QString>
#include <QLibrary>
#include <QFrame>
#include <QList>

#include <cstdio>
#include <cinttypes>
#include <iostream>
#include <algorithm>

#include <QCoreApplication>
#include <QFile>
#include <QDir>
#include <QList>
#include <QStringList>

#ifndef _WIN32
#   include <dlfcn.h>
#endif

#if defined(__APPLE__)
#   define OPENTRACK_SONAME "dylib"
#elif defined(_WIN32)
#   define OPENTRACK_SONAME "dll"
#else
#   define OPENTRACK_SONAME "so"
#endif

#include <iostream>

#ifdef _MSC_VER
#   define OPENTRACK_LIB_PREFIX ""
#else
#   define OPENTRACK_LIB_PREFIX "lib"
#endif


extern "C" typedef void* (*OPENTRACK_CTOR_FUNPTR)(void);
extern "C" typedef Metadata* (*OPENTRACK_METADATA_FUNPTR)(void);

struct dylib {
    enum Type { Filter, Tracker, Protocol };
    
    dylib(const QString& filename, Type t) :
        type(t),
        filename(filename),
        Dialog(nullptr),
        Constructor(nullptr),
        Meta(nullptr),
        handle(nullptr)
    {
        // otherwise dlopen opens the calling executable
        if (filename.size() == 0)
            return;
    
#if defined(_WIN32)
        QString fullPath = QCoreApplication::applicationDirPath() + "/" + this->filename;
        handle = new QLibrary(fullPath);
    
        struct _foo {
            static bool die(QLibrary*& l, bool failp)
            {
                if (failp)
                {
                    qDebug() << "failed" << l->errorString();
                    delete l;
                    l = nullptr;
                }
                return failp;
            }
        };
    
        if (_foo::die(handle, !handle->load()))
            return;
    
        Dialog = (OPENTRACK_CTOR_FUNPTR) handle->resolve("GetDialog");
        if (_foo::die(handle, !Dialog))
            return;
    
        Constructor = (OPENTRACK_CTOR_FUNPTR) handle->resolve("GetConstructor");
        if (_foo::die(handle, !Constructor))
            return;
    
        Meta = (OPENTRACK_METADATA_FUNPTR) handle->resolve("GetMetadata");
        if (_foo::die(handle, !Meta))
            return;
#else
        QByteArray latin1 = QFile::encodeName(filename);
        handle = dlopen(latin1.constData(),
#   if defined(__APPLE__)
                    RTLD_LOCAL|RTLD_FIRST|RTLD_NOW
#   else
                    RTLD_LOCAL|RTLD_NOW // XXX RTLD_DEEPBIND on Linux?
#   endif
                        );
    
        struct _foo {
            static bool err(void*& handle)
            {
                const char* err = dlerror();
                if (err)
                {
                    fprintf(stderr, "Error, ignoring: %s\n", err);
                    fflush(stderr);
                    if (handle)
                        dlclose(handle);
                    handle = nullptr;
                    return true;
                }
                return false;
            }
        };
    
        if (handle)
        {
            if (_foo::err(handle))
                return;
            Dialog = (OPENTRACK_CTOR_FUNPTR) dlsym(handle, "GetDialog");
            if (_foo::err(handle))
                return;
            Constructor = (OPENTRACK_CTOR_FUNPTR) dlsym(handle, "GetConstructor");
            if (_foo::err(handle))
                return;
            Meta = (OPENTRACK_METADATA_FUNPTR) dlsym(handle, "GetMetadata");
            if (_foo::err(handle))
                return;
        } else {
            (void) _foo::err(handle);
            return;
        }
#endif
    
        auto m = mem<Metadata>(Meta());
    
        icon = m->icon();
        name = m->name();
    }
    ~dylib()
    {
#if defined(_WIN32)
        if (handle)
            delete handle;
#else
        if (handle)
            (void) dlclose(handle);
#endif
    }
    
    static QList<mem<dylib>> enum_libraries()
    {
        const char* filters_n[] = { "opentrack-filter-*.",
                                    "opentrack-tracker-*.",
                                    "opentrack-proto-*."
                                  };
        const Type filters_t[] = { Filter, Tracker, Protocol };
    
        QDir settingsDir( QCoreApplication::applicationDirPath() );
    
        QList<mem<dylib>> ret;
    
        for (int i = 0; i < 3; i++)
        {
            QString filter = filters_n[i];
            auto t = filters_t[i];
            QStringList filenames = settingsDir.entryList(QStringList { OPENTRACK_LIB_PREFIX + filter + OPENTRACK_SONAME },
                                                          QDir::Files,
                                                          QDir::Name);
            for (int i = 0; i < filenames.size(); i++) {
                QIcon icon;
                QString longName;
                QString str = filenames.at(i);
                auto lib = std::make_shared<dylib>(str, t);
                qDebug() << "Loading" << str;
                std::cout.flush();
                if (!get_metadata(lib, longName, icon))
                    continue;
                using d = const mem<dylib>&;
                if (std::any_of(ret.cbegin(),
                                ret.cend(),
                                [&](d a) {return a->type == lib->type && a->name == lib->name;}))
                {
                    qDebug() << "Duplicate lib" << lib->filename;
                    continue;
                }
                ret.push_back(lib);
            }
        }
    
        return ret;
    }
    
    Type type;
    QString filename;
    
    QIcon icon;
    QString name;
    
    OPENTRACK_CTOR_FUNPTR Dialog;
    OPENTRACK_CTOR_FUNPTR Constructor;
    OPENTRACK_METADATA_FUNPTR Meta;
private:
#if defined(_WIN32)
    QLibrary* handle;
#else
    void* handle;
#endif
    
    static bool get_metadata(mem<dylib> lib, QString& name, QIcon& icon)
    {
        Metadata* meta;
        if (!lib->Meta || ((meta = lib->Meta()), !meta))
            return false;
        name = meta->name();
        icon = meta->icon();
        delete meta;
        return true;
    }
};

struct Modules {
    Modules() :
        module_list(dylib::enum_libraries()),
        filter_modules(filter(dylib::Filter)),
        tracker_modules(filter(dylib::Tracker)),
        protocol_modules(filter(dylib::Protocol))
    {}
    QList<mem<dylib>>& filters() { return filter_modules; }
    QList<mem<dylib>>& trackers() { return tracker_modules; }
    QList<mem<dylib>>& protocols() { return protocol_modules; }
private:
    QList<mem<dylib>> module_list;
    QList<mem<dylib>> filter_modules;
    QList<mem<dylib>> tracker_modules;
    QList<mem<dylib>> protocol_modules;

    template<typename t>
    static void sort(QList<t>& xs)
    {
        std::sort(xs.begin(), xs.end(), [&](const t& a, const t& b) { return a->name.toLower() < b->name.toLower(); });
    }
    
    QList<mem<dylib>> filter(dylib::Type t)
    {
        QList<mem<dylib>> ret;
        for (auto x : module_list)
            if (x->type == t)
                ret.push_back(x);

        sort(ret);

        return ret;
    }
};

template<typename t>
mem<t> make_dylib_instance(mem<dylib> lib)
{
    mem<t> ret;
    if (lib != nullptr && lib->Constructor)
        ret = mem<t>(reinterpret_cast<t*>(reinterpret_cast<OPENTRACK_CTOR_FUNPTR>(lib->Constructor)()));
    return ret;
}