aboutsummaryrefslogtreecommitdiff
path: root/qmidiplayer-desktop/qmpplugin.cpp
blob: c52f48644972aac92903c9d4bc355201953037d5 (plain) (blame)
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
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#include <cstdio>
#include <cstring>
#include <QDirIterator>
#include "qmpplugin.hpp"
#include "qmpmainwindow.hpp"
#include "qmpmidioutfluid.hpp"
#include "qmpsettingswindow.hpp"
qmpPluginAPIImpl *qmpPluginManager::pluginAPI = nullptr;
qmpMainWindow *qmpPluginManager::mainwindow = nullptr;
#ifdef _WIN32
#include <codecvt>
#include <locale>
std::string wstr2str(std::wstring s)
{
    std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> wsc;
    return wsc.to_bytes(s);
}
void qmpPluginManager::scanPlugins(const std::vector<std::string> &pp)
{
    QDirIterator *dir;
    std::vector<std::wstring> cpluginpaths;
    std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> wsc;
    for (auto p : pp)
        cpluginpaths.push_back(wsc.from_bytes(p));
    dir = new QDirIterator(QCoreApplication::applicationDirPath() + "/plugins/");
    while (dir->hasNext())
    {
        dir->next();
        if (dir->fileInfo().suffix() == "dll")
            cpluginpaths.push_back(QCoreApplication::applicationDirPath().toStdWString() + std::wstring(L"/plugins/") + dir->fileName().toStdWString());
    }
    delete dir;
    for (unsigned i = 0; i < cpluginpaths.size(); ++i)
    {
        HMODULE hso = LoadLibraryW(cpluginpaths[i].c_str());
        if (!hso)
        {
            fprintf(stderr, "Error while loading library: %d\n", GetLastError());
            continue;
        }
        FARPROC hndi = GetProcAddress(hso, "qmpPluginGetInterface");
        if (!hndi)
        {
            fprintf(stderr, "plugin %s doesn't seem to be a qmidiplayer plugin.\n", wstr2str(cpluginpaths[i]).c_str());
            continue;
        }
        FARPROC hndiv = GetProcAddress(hso, "qmpPluginGetAPIRev");
        if (!hndiv)
        {
            fprintf(stderr, "plugin %s is incompatible with this version of qmidiplayer.\n", wstr2str(cpluginpaths[i]).c_str());
            continue;
        }
        qmpPluginAPIRevEntry getv = (qmpPluginAPIRevEntry)hndiv;
        if (strcmp(getv(), QMP_PLUGIN_API_REV))
        {
            fprintf(stderr, "plugin %s is incompatible with this version of qmidiplayer.\n", wstr2str(cpluginpaths[i]).c_str());
            continue;
        }
        qmpPluginEntry e = (qmpPluginEntry)hndi;
        qmpPluginIntf *intf = e(pluginAPI);
        plugins.push_back(qmpPlugin(std::string(intf->pluginGetName()), std::string(intf->pluginGetVersion()), wstr2str(cpluginpaths[i]), intf));
    }
}
#else
void qmpPluginManager::scanPlugins(const std::vector<std::string> &pp)
{
    QDirIterator *dir;
    std::vector<std::string> cpluginpaths(pp);
#ifdef NON_PORTABLE
    QString pdir = QString(QT_STRINGIFY(INSTALL_PREFIX)) + "/lib/qmidiplayer/";
    dir = new QDirIterator(pdir);
    while (dir->hasNext())
    {
        dir->next();
        if (dir->fileInfo().suffix() == "so")
            cpluginpaths.push_back((pdir + dir->fileName()).toStdString());
    }
    delete dir;
#endif
    dir = new QDirIterator(QCoreApplication::applicationDirPath() + "/plugins/");
    while (dir->hasNext())
    {
        dir->next();
        if (dir->fileInfo().suffix() == "so")
            cpluginpaths.push_back(QCoreApplication::applicationDirPath().toStdString() + std::string("/plugins/") + dir->fileName().toStdString());
    }
    delete dir;
    for (unsigned i = 0; i < cpluginpaths.size(); ++i)
    {
        void *hso = dlopen(cpluginpaths[i].c_str(), RTLD_LAZY);
        if (!hso)
        {
            fprintf(stderr, "%s\n", dlerror());
            continue;
        }
        void *hndi = dlsym(hso, "qmpPluginGetInterface");
        if (!hndi)
        {
            fprintf(stderr, "file %s doesn't seem to be a qmidiplayer plugin.\n", cpluginpaths[i].c_str());
            continue;
        }
        void *hndiv = dlsym(hso, "qmpPluginGetAPIRev");
        if (!hndiv)
        {
            fprintf(stderr, "file %s is incompatible with this version of qmidiplayer.\n", cpluginpaths[i].c_str());
            continue;
        }
        qmpPluginAPIRevEntry getv = (qmpPluginAPIRevEntry)hndiv;
        if (strcmp(getv(), QMP_PLUGIN_API_REV))
        {
            fprintf(stderr, "file %s is incompatible with this version of qmidiplayer.\n", cpluginpaths[i].c_str());
            continue;
        }
        qmpPluginEntry e = (qmpPluginEntry)hndi;
        qmpPluginIntf *intf = e(pluginAPI);
        plugins.push_back(qmpPlugin(std::string(intf->pluginGetName()), std::string(intf->pluginGetVersion()), std::string(cpluginpaths[i]), intf));
    }
}
#endif
qmpPluginManager::qmpPluginManager()
{
    mainwindow = qmpMainWindow::getInstance();
    pluginAPI = new qmpPluginAPIImpl();
}
qmpPluginManager::~qmpPluginManager()
{
    for (unsigned i = 0; i < plugins.size(); ++i)
    {
        if (plugins[i].initialized)
            plugins[i].pinterface->deinit();
        delete plugins[i].pinterface;
    }
    mainwindow = nullptr;
    delete pluginAPI;
}
std::vector<qmpPlugin> *qmpPluginManager::getPlugins()
{
    return &plugins;
}
void qmpPluginManager::initPlugins()
{
    for (unsigned i = 0; i < plugins.size(); ++i)
    {
        if (!plugins[i].enabled)
            continue;
        fprintf(stderr, "Loaded plugin: %s\n", plugins[i].path.c_str());
        plugins[i].pinterface->init();
        plugins[i].initialized = true;
    }
}
void qmpPluginManager::deinitPlugins()
{
    for (unsigned i = 0; i < plugins.size(); ++i)
    {
        if (plugins[i].initialized)
            plugins[i].pinterface->deinit();
        plugins[i].enabled = plugins[i].initialized = false;
    }
}

qmpPluginAPIImpl::qmpPluginAPIImpl() {}
qmpPluginAPIImpl::~qmpPluginAPIImpl() {}
#define qmw qmpPluginManager::mainwindow
uint32_t qmpPluginAPIImpl::getDivision()
{
    return qmw && qmw->getPlayer() ? qmw->getPlayer()->getDivision() : 0;
}
uint32_t qmpPluginAPIImpl::getRawTempo()
{
    return qmw && qmw->getPlayer() ? qmw->getPlayer()->getRawTempo() : 0;
}
double qmpPluginAPIImpl::getRealTempo()
{
    return qmw && qmw->getPlayer() ? qmw->getPlayer()->getTempo() : 0;
}
uint32_t qmpPluginAPIImpl::getTimeSig()
{
    int n, d = 0, t;
    qmw &&qmw->getPlayer() ? qmw->getPlayer()->getCurrentTimeSignature(&n, &t) : void(0);
    for (; t >>= 1; ++d);
    return n << 8 | d;
}
int qmpPluginAPIImpl::getKeySig()
{
    return qmw && qmw->getPlayer() ? qmw->getPlayer()->getCurrentKeySignature() : 0;
}
uint32_t qmpPluginAPIImpl::getNoteCount()
{
    return qmw && qmw->getPlayer() ? qmw->getPlayer()->getFileNoteCount() : 0;
}
uint32_t qmpPluginAPIImpl::getMaxTick()
{
    return qmw && qmw->getPlayer() ? qmw->getPlayer()->getMaxTick() : 0;
}
uint32_t qmpPluginAPIImpl::getCurrentPolyphone()
{
    return qmw && qmw->getPlayer() ? qmw->getFluid()->getPolyphone() : 0;
}
uint32_t qmpPluginAPIImpl::getMaxPolyphone()
{
    return qmw && qmw->getPlayer() ? qmw->getFluid()->getMaxPolyphone() : 0;
}
uint32_t qmpPluginAPIImpl::getCurrentTimeStamp()
{
    return qmw && qmw->getPlayer() ? qmw->getPlayer()->getTick() : 0;
}
uint32_t qmpPluginAPIImpl::getCurrentPlaybackPercentage()
{
    return qmw ? qmw->getPlaybackPercentage() : 0;
}

PlaybackStatus qmpPluginAPIImpl::getPlaybackStatus()
{
    return qmw ? qmw->getPlaybackStatus() : PlaybackStatus{false, 0, 0, 0, 0};
}
int qmpPluginAPIImpl::getChannelCC(int ch, int cc)
{
    return qmw && qmw->getPlayer() ? qmw->getPlayer()->getCC(ch, cc) : 0;
}
int qmpPluginAPIImpl::getChannelPreset(int ch)
{
    uint16_t b;
    uint8_t p;
    std::string nm;
    if (qmw && qmw->getPlayer())
    {
        if (qmw->getPlayer()->getChannelOutputDevice(ch)->getChannelPreset(ch, &b, &p, nm))
            return p;
        return qmw->getPlayer()->getCC(ch, 128);
    }
    return 0;
}
void qmpPluginAPIImpl::playerSeek(uint32_t percentage)
{
    if (qmw)
        qmw->playerSeek(percentage);
}
double qmpPluginAPIImpl::getPitchBend(int ch)
{
    return qmw && qmw->getPlayer() ? qmw->getPlayer()->getPitchBend(ch) : 0;
}
void qmpPluginAPIImpl::getPitchBendRaw(int ch, uint32_t *pb, uint32_t *pbr)
{
    if (qmw && qmw->getPlayer())
        qmw->getPlayer()->getPitchBendRaw(ch, pb, pbr);
}
bool qmpPluginAPIImpl::getChannelMask(int ch)
{
    return qmw && qmw->getPlayer() ? qmw->getPlayer()->getChannelMask(ch) : false;
}
std::string qmpPluginAPIImpl::getTitle()
{
    return qmw ? qmw->getTitle() : "";
}
std::wstring qmpPluginAPIImpl::getWTitle()
{
    return qmw ? qmw->getWTitle() : L"";
}
std::string qmpPluginAPIImpl::getChannelPresetString(int ch)
{
    uint16_t b;
    uint8_t p;
    char ret[320];
    ret[0] = 0;
    std::string nm;
    if (qmw && qmw->getPlayer())
    {
        int r = qmw->getPlayer()->getChannelOutputDevice(ch)->getChannelPreset(ch, &b, &p, nm);
        if (!r)
        {
            b = qmw->getPlayer()->getCC(ch, 0) << 7 | qmw->getPlayer()->getCC(ch, 32);
            p = qmw->getPlayer()->getCC(ch, 128);
            nm = qmw->getPlayer()->getChannelOutputDevice(ch)->getPresetName(b, p);
        }
        snprintf(ret, 320, "%03d:%03d %s", b, p, nm.c_str());
    }
    return std::string(ret);
}
bool qmpPluginAPIImpl::isDarkTheme()
{
    return qmw ? qmw->isDarkTheme() : false;
}
void *qmpPluginAPIImpl::getMainWindow()
{
    return (void *)qmw;
}

void qmpPluginAPIImpl::discardCurrentEvent()
{
    if (qmw && qmw->getPlayer())
        qmw->getPlayer()->discardCurrentEvent();
}
void qmpPluginAPIImpl::commitEventChange(SEvent d)
{
    if (qmw && qmw->getPlayer())
        qmw->getPlayer()->commitEventChange(d);
}
void qmpPluginAPIImpl::callEventReaderCB(SEvent d)
{
    if (qmw && qmw->getPlayer())
        qmw->getPlayer()->callEventReaderCB(d);
}
void qmpPluginAPIImpl::setFuncState(std::string name, bool state)
{
    if (qmw)
        qmw->setFuncState(name, state);
}
void qmpPluginAPIImpl::setFuncEnabled(std::string name, bool enable)
{
    if (qmw)
        qmw->setFuncEnabled(name, enable);
}

void qmpPluginAPIImpl::registerMidiOutDevice(qmpMidiOutDevice *dev, std::string name)
{
    qmw->getPlayer()->registerMidiOutDevice(dev, name);
}
void qmpPluginAPIImpl::unregisterMidiOutDevice(std::string name)
{
    qmw->getPlayer()->unregisterMidiOutDevice(name);
}
int qmpPluginAPIImpl::registerEventHandlerIntf(ICallBack *cb, void *userdata)
{
    return qmw->getPlayer()->setEventHandlerCB(cb, userdata);
}
void qmpPluginAPIImpl::unregisterEventHandlerIntf(int intfhandle)
{
    qmw->getPlayer()->unsetEventHandlerCB(intfhandle);
}
int qmpPluginAPIImpl::registerEventReaderIntf(ICallBack *cb, void *userdata)
{
    return qmw->getPlayer()->setEventReaderCB(cb, userdata);
}
void qmpPluginAPIImpl::unregisterEventReaderIntf(int intfhandle)
{
    qmw->getPlayer()->unsetEventReaderCB(intfhandle);
}
int qmpPluginAPIImpl::registerUIHook(std::string e, ICallBack *cb, void *userdat)
{
    return qmw->registerUIHook(e, cb, userdat);
}
int qmpPluginAPIImpl::registerUIHook(std::string e, callback_t cb, void *userdat)
{
    return qmw->registerUIHook(e, cb, userdat);
}
void qmpPluginAPIImpl::unregisterUIHook(std::string e, int hook)
{
    qmw->unregisterUIHook(e, hook);
}
void qmpPluginAPIImpl::registerFunctionality(qmpFuncBaseIntf *i, std::string name, std::string desc, const char *icon, int iconlen, bool checkable)
{
    qmw->registerFunctionality(i, name, desc, icon, iconlen, checkable);
}
void qmpPluginAPIImpl::unregisterFunctionality(std::string name)
{
    qmw->unregisterFunctionality(name);
}
int qmpPluginAPIImpl::registerFileReadFinishedHandlerIntf(ICallBack *cb, void *userdata)
{
    return qmw->getPlayer()->setFileReadFinishedCB(cb, userdata);
}
void qmpPluginAPIImpl::unregisterFileReadFinishedHandlerIntf(int intfhandle)
{
    qmw->getPlayer()->unsetFileReadFinishedCB(intfhandle);
}
void qmpPluginAPIImpl::registerFileReader(qmpFileReader *reader, std::string name)
{
    qmw->getPlayer()->registerReader(reader, name);
}
void qmpPluginAPIImpl::unregisterFileReader(std::string name)
{
    qmw->getPlayer()->unregisterReader(name);
}
int qmpPluginAPIImpl::registerEventHandler(callback_t cb, void *userdata, bool post)
{
    return qmw->getPlayer()->registerEventHandler(cb, userdata, post);
}
void qmpPluginAPIImpl::unregisterEventHandler(int id)
{
    qmw->getPlayer()->unregisterEventHandler(id);
}
int qmpPluginAPIImpl::registerEventReadHandler(callback_t cb, void *userdata)
{
    return qmw->getPlayer()->registerEventReadHandler(cb, userdata);
}
void qmpPluginAPIImpl::unregisterEventReadHandler(int id)
{
    qmw->getPlayer()->unregisterEventReadHandler(id);
}
int qmpPluginAPIImpl::registerFileReadFinishHook(callback_t cb, void *userdata)
{
    return qmw->getPlayer()->registerFileReadFinishHook(cb, userdata);
}
void qmpPluginAPIImpl::unregisterFileReadFinishHook(int id)
{
    qmw->getPlayer()->unregisterFileReadFinishHook(id);
}

void qmpPluginAPIImpl::registerOptionInt(std::string tab, std::string desc, std::string key, int min, int max, int defaultval)
{
    qmw->getSettings()->registerOptionInt(tab, desc, key, min, max, defaultval);
}
int qmpPluginAPIImpl::getOptionInt(std::string key)
{
    return qmw->getSettings()->getOptionInt(key);
}
void qmpPluginAPIImpl::setOptionInt(std::string key, int val)
{
    qmw->getSettings()->setOptionInt(key, val);
}
void qmpPluginAPIImpl::registerOptionUint(std::string tab, std::string desc, std::string key, unsigned min, unsigned max, unsigned defaultval)
{
    qmw->getSettings()->registerOptionUint(tab, desc, key, min, max, defaultval);
}
unsigned qmpPluginAPIImpl::getOptionUint(std::string key)
{
    return qmw->getSettings()->getOptionUint(key);
}
void qmpPluginAPIImpl::setOptionUint(std::string key, unsigned val)
{
    qmw->getSettings()->setOptionUint(key, val);
}
void qmpPluginAPIImpl::registerOptionBool(std::string tab, std::string desc, std::string key, bool defaultval)
{
    qmw->getSettings()->registerOptionBool(tab, desc, key, defaultval);
}
bool qmpPluginAPIImpl::getOptionBool(std::string key)
{
    return qmw->getSettings()->getOptionBool(key);
}
void qmpPluginAPIImpl::setOptionBool(std::string key, bool val)
{
    qmw->getSettings()->setOptionBool(key, val);
}
void qmpPluginAPIImpl::registerOptionDouble(std::string tab, std::string desc, std::string key, double min, double max, double defaultval)
{
    qmw->getSettings()->registerOptionDouble(tab, desc, key, min, max, defaultval);
}
double qmpPluginAPIImpl::getOptionDouble(std::string key)
{
    return qmw->getSettings()->getOptionDouble(key);
}
void qmpPluginAPIImpl::setOptionDouble(std::string key, double val)
{
    qmw->getSettings()->setOptionDouble(key, val);
}
void qmpPluginAPIImpl::registerOptionString(std::string tab, std::string desc, std::string key, std::string defaultval, bool ispath)
{
    qmw->getSettings()->registerOptionString(tab, desc, key, defaultval, ispath);
}
std::string qmpPluginAPIImpl::getOptionString(std::string key)
{
    return qmw->getSettings()->getOptionString(key);
}
void qmpPluginAPIImpl::setOptionString(std::string key, std::string val)
{
    return qmw->getSettings()->setOptionString(key, val);
}
void qmpPluginAPIImpl::registerOptionEnumInt(std::string tab, std::string desc, std::string key, std::vector<std::string> options, int defaultval)
{
    qmw->getSettings()->registerOptionEnumInt(tab, desc, key, options, defaultval);
}
int qmpPluginAPIImpl::getOptionEnumInt(std::string key)
{
    return qmw->getSettings()->getOptionEnumInt(key);
}
void qmpPluginAPIImpl::setOptionEnumInt(std::string key, int val)
{
    return qmw->getSettings()->setOptionEnumInt(key, val);
}