File: DistrhoUIProM.cpp

package info (click to toggle)
dpf-plugins 1.6%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 145,284 kB
  • sloc: cpp: 299,382; ansic: 61,509; objc: 2,715; makefile: 2,076; xml: 618; sh: 462; java: 211; python: 184
file content (317 lines) | stat: -rw-r--r-- 8,679 bytes parent folder | download | duplicates (2)
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
/*
 * DISTRHO ProM Plugin
 * Copyright (C) 2015-2022 Filipe Coelho <falktx@falktx.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * For a full copy of the license see the LICENSE file.
 */

/* s/private/public/ as workaround old projectM versions */
#define private public
#include "libprojectM/projectM.hpp"
#undef private

#ifndef PROJECTM_DATA_DIR
#include "libprojectM/projectM-opengl.h"
#endif

#include "DistrhoPluginProM.hpp"
#include "DistrhoUIProM.hpp"

#include "DistrhoPluginUtils.hpp"

START_NAMESPACE_DISTRHO

// -----------------------------------------------------------------------

DistrhoUIProM::DistrhoUIProM()
    : UI(512, 512),
      fPM(nullptr),
      fResizeHandle(this)
{
    const double scaleFactor = getScaleFactor();

    if (d_isNotZero(scaleFactor))
        setSize(512*scaleFactor, 512*scaleFactor);

    setGeometryConstraints(256*scaleFactor, 256*scaleFactor, true);

    // no need to show resize handle if window is user-resizable
    // if (isResizable())
    //     fResizeHandle.hide();
}

DistrhoUIProM::~DistrhoUIProM()
{
    if (fPM == nullptr)
        return;

    if (DistrhoPluginProM* const dspPtr = (DistrhoPluginProM*)getPluginInstancePointer())
    {
        const MutexLocker csm(dspPtr->fMutex);
        dspPtr->fPM = nullptr;
    }
}

// -----------------------------------------------------------------------
// DSP Callbacks

void DistrhoUIProM::parameterChanged(uint32_t, float)
{
}

// -----------------------------------------------------------------------
// UI Callbacks

void DistrhoUIProM::uiIdle()
{
    if (fPM == nullptr)
        return;

    repaint();

    if (DistrhoPluginProM* const dspPtr = (DistrhoPluginProM*)getPluginInstancePointer())
    {
        if (dspPtr->fPM != nullptr)
            return;

        const MutexLocker csm(dspPtr->fMutex);
        dspPtr->fPM = fPM;
    }
}

void DistrhoUIProM::uiReshape(const uint width, const uint height)
{
    UI::uiReshape(width, height);

    if (fPM == nullptr)
    {
#ifdef PROJECTM_DATA_DIR
        fPM = new projectM(PROJECTM_DATA_DIR "/config.inp");
#else
        if (const char* const bundlePath = getBundlePath())
        {
            const String datadir(getResourcePath(bundlePath));
            d_stdout("ProM datadir: '%s'", datadir.buffer());

            projectM::Settings settings;
            settings.presetURL    = datadir + DISTRHO_OS_SEP_STR "presets";
            settings.titleFontURL = datadir + DISTRHO_OS_SEP_STR "fonts" DISTRHO_OS_SEP_STR "Vera.ttf";
            settings.menuFontURL  = datadir + DISTRHO_OS_SEP_STR "fonts" DISTRHO_OS_SEP_STR "VeraMono.ttf";
            settings.datadir      = datadir;
            fPM = new projectM(settings);
        }
        else
        {
            d_stderr2("ProM: failed to find bundle path, UI will be empty");
        }
#endif
    }

    if (fPM != nullptr)
        fPM->projectM_resetGL(width, height);
}

// -----------------------------------------------------------------------
// Widget Callbacks

void DistrhoUIProM::onDisplay()
{
    if (fPM == nullptr)
        return;

    fPM->renderFrame();

    // some projectM versions do not turn off the last set GL program
    glUseProgram(0);
}

static projectMKeycode dgl2pmkey(const DGL_NAMESPACE::Key key) noexcept
{
    switch (key)
    {
    case DGL_NAMESPACE::kKeyBackspace:
        return PROJECTM_K_BACKSPACE;
    case DGL_NAMESPACE::kKeyEscape:
        return PROJECTM_K_ESCAPE;
    case DGL_NAMESPACE::kKeyDelete:
        return PROJECTM_K_DELETE;
    case DGL_NAMESPACE::kKeyF1:
        return PROJECTM_K_F1;
    case DGL_NAMESPACE::kKeyF2:
        return PROJECTM_K_F2;
    case DGL_NAMESPACE::kKeyF3:
        return PROJECTM_K_F3;
    case DGL_NAMESPACE::kKeyF4:
        return PROJECTM_K_F4;
    case DGL_NAMESPACE::kKeyF5:
        return PROJECTM_K_F5;
    case DGL_NAMESPACE::kKeyF6:
        return PROJECTM_K_F6;
    case DGL_NAMESPACE::kKeyF7:
        return PROJECTM_K_F7;
    case DGL_NAMESPACE::kKeyF8:
        return PROJECTM_K_F8;
    case DGL_NAMESPACE::kKeyF9:
        return PROJECTM_K_F9;
    case DGL_NAMESPACE::kKeyF10:
        return PROJECTM_K_F10;
    case DGL_NAMESPACE::kKeyF11:
        return PROJECTM_K_F11;
    case DGL_NAMESPACE::kKeyF12:
        return PROJECTM_K_F12;
    case DGL_NAMESPACE::kKeyLeft:
        return PROJECTM_K_LEFT;
    case DGL_NAMESPACE::kKeyUp:
        return PROJECTM_K_UP;
    case DGL_NAMESPACE::kKeyRight:
        return PROJECTM_K_RIGHT;
    case DGL_NAMESPACE::kKeyDown:
        return PROJECTM_K_DOWN;
    case DGL_NAMESPACE::kKeyPageUp:
        return PROJECTM_K_PAGEUP;
    case DGL_NAMESPACE::kKeyPageDown:
        return PROJECTM_K_PAGEDOWN;
    case DGL_NAMESPACE::kKeyHome:
        return PROJECTM_K_HOME;
    case DGL_NAMESPACE::kKeyEnd:
        return PROJECTM_K_END;
    case DGL_NAMESPACE::kKeyInsert:
        return PROJECTM_K_INSERT;
    case DGL_NAMESPACE::kKeyShiftL:
        return PROJECTM_K_LSHIFT;
    case DGL_NAMESPACE::kKeyShiftR:
        return PROJECTM_K_RSHIFT;
    case DGL_NAMESPACE::kKeyControlL:
        return PROJECTM_K_LCTRL;
    case DGL_NAMESPACE::kKeyControlR:
    case DGL_NAMESPACE::kKeyAltL:
    case DGL_NAMESPACE::kKeyAltR:
    case DGL_NAMESPACE::kKeySuperL:
    case DGL_NAMESPACE::kKeySuperR:
    case DGL_NAMESPACE::kKeyMenu:
    case DGL_NAMESPACE::kKeyCapsLock:
    case DGL_NAMESPACE::kKeyScrollLock:
    case DGL_NAMESPACE::kKeyNumLock:
    case DGL_NAMESPACE::kKeyPrintScreen:
    case DGL_NAMESPACE::kKeyPause:
        break;
    }

    return PROJECTM_K_NONE;
}

bool DistrhoUIProM::onKeyboard(const KeyboardEvent& ev)
{
    if (fPM == nullptr)
        return false;

#ifdef HAVE_PROJECTM_TEXT_FUNCTIONS
    // special handling for text
    if (fPM->isTextInputActive(true) && !ev.press)
    {
        if (ev.key >= ' ' && ev.key <= 'z')
        {
            std::string key;
            key += static_cast<char>(ev.key);
            fPM->setSearchText(key);
            return true;
        }
        else if (ev.key == DGL_NAMESPACE::kKeyBackspace)
        {
            fPM->deleteSearchText();
            return true;
        }
    }
#endif

    projectMKeycode pmKey = PROJECTM_K_NONE;

    if (ev.key >= kKeyF1)
    {
        pmKey = dgl2pmkey(static_cast<DGL_NAMESPACE::Key>(ev.key));
    }
    else if ((ev.key >= PROJECTM_K_0 && ev.key <= PROJECTM_K_9) ||
             (ev.key >= PROJECTM_K_A && ev.key <= PROJECTM_K_Z) ||
             (ev.key >= PROJECTM_K_a && ev.key <= PROJECTM_K_z))
    {
        pmKey = static_cast<projectMKeycode>(ev.key);

        if (ev.key >= PROJECTM_K_A && ev.key <= PROJECTM_K_Z && (ev.mod & DGL_NAMESPACE::kModifierShift))
            pmKey = static_cast<projectMKeycode>(pmKey + (PROJECTM_K_a - PROJECTM_K_A));
    }
    else
    {
        /* missing:
         * PROJECTM_K_CAPSLOCK
         */
        switch (ev.key)
        {
        case DGL_NAMESPACE::kKeyBackspace:
            pmKey = PROJECTM_K_BACKSPACE;
            break;
        case DGL_NAMESPACE::kKeyEscape:
            pmKey = PROJECTM_K_ESCAPE;
            break;
        case DGL_NAMESPACE::kKeyDelete:
            pmKey = PROJECTM_K_DELETE;
            break;
        case '\r':
            pmKey = PROJECTM_K_RETURN;
            break;
#ifdef HAVE_PROJECTM_TEXT_FUNCTIONS
        case '/':
            pmKey = PROJECTM_K_SLASH;
            break;
        case '\\':
            pmKey = PROJECTM_K_BACKSLASH;
            break;
#endif
        case '+':
            pmKey = PROJECTM_K_PLUS;
            break;
        case '-':
            pmKey = PROJECTM_K_MINUS;
            break;
        case '=':
            pmKey = PROJECTM_K_EQUALS;
            break;
        default:
            // d_stdout("Unhandled key %u %u %c", ev.keycode, ev.key, ev.key);
            break;
        }
    }

    if (pmKey == PROJECTM_K_NONE)
        return false;

    fPM->default_key_handler(ev.press ? PROJECTM_KEYUP : PROJECTM_KEYDOWN, pmKey);
    return true;
}

bool DistrhoUIProM::onMouse(const MouseEvent& ev)
{
    if (ev.press)
        getWindow().focus();

    return false;
}

// -----------------------------------------------------------------------

UI* createUI()
{
    return new DistrhoUIProM();
}

// -----------------------------------------------------------------------

END_NAMESPACE_DISTRHO