OXYGENHUB
◆ main openmsx / src / serial / MidiInCoreMIDI.hh
3.1 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
e8555c89 Andy McCall 1
#ifndef MIDIINCOREMIDI_HH
2
#define MIDIINCOREMIDI_HH
3
4
#ifdef __APPLE__
5
6
#include "MidiInDevice.hh"
7
#include "EventListener.hh"
8
#include "serialize_meta.hh"
9
#include "circular_buffer.hh"
10
#include <CoreMIDI/MIDIServices.h>
11
12
#include <cstdint>
13
#include <mutex>
14
15
namespace openmsx {
16
17
class EventDistributor;
18
class Scheduler;
19
class PluggingController;
20
21
/** Sends MIDI events to an existing CoreMIDI destination.
22
  */
23
class MidiInCoreMIDI final : public MidiInDevice, private EventListener
24
{
25
public:
26
	static void registerAll(EventDistributor& eventDistributor,
27
                            Scheduler& scheduler, PluggingController& controller);
28
29
	/** Public for the sake of make_unique<>() - not intended for actual
30
	  * public use.
31
	  */
32
	explicit MidiInCoreMIDI(EventDistributor& eventDistributor_,
33
                            Scheduler& scheduler_, MIDIEndpointRef endpoint);
34
	~MidiInCoreMIDI();
35
36
	// Pluggable
37
	void plugHelper(Connector& connector, EmuTime time) override;
38
	void unplugHelper(EmuTime time) override;
39
	[[nodiscard]] zstring_view getName() const override;
40
	[[nodiscard]] zstring_view getDescription() const override;
41
42
	// MidiInDevice
43
	void signal(EmuTime time) override;
44
45
	template<typename Archive>
46
	void serialize(Archive& ar, unsigned version);
47
48
49
private:
50
	// EventListener
51
	bool signalEvent(const Event& event) override;
52
53
	static void sendPacketList(const MIDIPacketList *pktList,
54
	                           void *readProcRefCon, void *srcConnRefCon);
55
	void sendPacketList(const MIDIPacketList *pktList, void *srcConnRefCon);
56
57
private:
58
	EventDistributor& eventDistributor;
59
	Scheduler& scheduler;
60
	cb_queue<uint8_t> queue;
61
	std::mutex mutex; // to protect queue
62
63
	MIDIClientRef client;
64
	MIDIPortRef port;
65
	MIDIEndpointRef endpoint;
66
67
	std::string name;
68
};
69
70
/** Sends MIDI events from a newly created CoreMIDI virtual source.
71
  * This class acts as a MIDI input, unlike the other class that sends events
72
  * to a MIDI output. It is similar to using an IAC bus, but doesn't require
73
  * prior configuration to work.
74
  */
75
class MidiInCoreMIDIVirtual final : public MidiInDevice, private EventListener
76
{
77
public:
78
	explicit MidiInCoreMIDIVirtual(EventDistributor& eventDistributor_,
79
                                   Scheduler& scheduler_);
80
	~MidiInCoreMIDIVirtual();
81
82
	// Pluggable
83
	void plugHelper(Connector& connector, EmuTime time) override;
84
	void unplugHelper(EmuTime time) override;
85
	zstring_view getName() const override;
86
	zstring_view getDescription() const override;
87
88
	// MidiInDevice
89
	void signal(EmuTime time) override;
90
91
	template<typename Archive>
92
	void serialize(Archive& ar, unsigned version);
93
94
private:
95
	// EventListener
96
	bool signalEvent(const Event& event) override;
97
98
	static void sendPacketList(const MIDIPacketList *pktList,
99
	                           void *readProcRefCon, void *srcConnRefCon);
100
	void sendPacketList(const MIDIPacketList *pktList, void *srcConnRefCon);
101
102
	EventDistributor& eventDistributor;
103
	Scheduler& scheduler;
104
	cb_queue<uint8_t> queue;
105
	std::mutex mutex; // to protect queue
106
107
	MIDIClientRef client;
108
	MIDIEndpointRef endpoint;
109
};
110
111
} // namespace openmsx
112
113
#endif // defined(__APPLE__)
114
#endif // MIDIINCOREMIDI_HH