-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoystickMidiMediator.h
91 lines (79 loc) · 2.86 KB
/
JoystickMidiMediator.h
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
//
// Created by guru on 11.10.23.
//
#ifndef DOJOYSTICK_JOYSTICKMIDIMEDIATOR_H
#define DOJOYSTICK_JOYSTICKMIDIMEDIATOR_H
#include "DPF/distrho/DistrhoDetails.hpp"
#include "DPF/distrho/extra/RingBuffer.hpp"
#include "DPF/distrho/extra/Thread.hpp"
#include "Settings.h"
#include "joystick-gateway.h"
#include <algorithm>
#include <iostream>
#include <linux/joystick.h>
#include <memory>
#include <string>
#include <thread>
#include <vector>
namespace DoJoyStick {
class DoJoyStickThread : public DISTRHO::Thread {
};
class MidiObserver {
public:
virtual void onMidiEvent(const MidiEvent &midiEvent) = 0;
};
//
// class ThreadProvider {
// public:
// virtual void startThread(std::function<void(void)>) = 0;
// virtual void stopThread();
// };
/** JoyStickMidiMediator
* is the connection from the joystick to midi
* to use, you have to provide the Settings and a
* possibility to run a thread on the desired function.
* The request for a ThreadProvider (over using std::thread)
* was made because DISTRHO DPF Plugin framework
* somehow hast problems, when lv2 manifests are created.
*/
class JoystickMidiMediator : public DISTRHO::Thread {
public:
explicit JoystickMidiMediator(const Settings &settings);
~JoystickMidiMediator() override;
void addObserver(MidiObserver *const observer) {
std::cout << "observer added...\n"
<< std::flush;
observers.push_back(observer);
std::cout << "obserers size: " << observers.size() << "\n"
<< std::flush;
};
void removeObserver(MidiObserver *const observer) {
std::cout << "observer removed \n"
<< std::flush;
auto predicate = [&observer](const MidiObserver *elem) { return elem == observer; };
observers.erase(std::remove_if(observers.begin(), observers.end(), predicate), observers.end());
};
protected:
// from DISTRHO::Thread
void run() override {
joystickGateway.js_event_loop();
}
void notifyObservers(const MidiEvent &midiEvent) {
std::cout << "NOTIFY... " << observers.size() << "\n"
<< std::flush;
for (auto observer: observers) {
observer->onMidiEvent(midiEvent);
}
}
private:
static void
event_handler(JoystickEvent event, void *);
void setupJoystick();
JoystickGateway joystickGateway;
std::thread joystickThread;
std::vector<MidiObserver *> observers;
void sendMidiMessage(const char *msg, size_t size);
static void printMidiMessage(const char *msg, size_t size, bool hex = true);
};
}// namespace DoJoyStick
#endif//DOJOYSTICK_JOYSTICKMIDIMEDIATOR_H