Skip to content
0.2.0-dev Download ZIP
132 lines ·5.0 KB Raw
1/**************************************************************************/
2/* midi_driver_coremidi.mm */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#import "midi_driver_coremidi.h"
32
33#ifdef COREMIDI_ENABLED
34
35#include "core/string/print_string.h"
36
37#import <CoreServices/CoreServices.h>
38
39Mutex MIDIDriverCoreMidi::mutex;
40bool MIDIDriverCoreMidi::core_midi_closed = false;
41
42MIDIDriverCoreMidi::InputConnection::InputConnection(int p_device_index, MIDIEndpointRef p_source) :
43 parser(p_device_index), source(p_source) {}
44
45void MIDIDriverCoreMidi::read(const MIDIPacketList *packet_list, void *read_proc_ref_con, void *src_conn_ref_con) {
46 MutexLock lock(mutex);
47 if (!core_midi_closed) {
48 InputConnection *source = static_cast<InputConnection *>(src_conn_ref_con);
49 const MIDIPacket *packet = packet_list->packet;
50 for (UInt32 packet_index = 0; packet_index < packet_list->numPackets; packet_index++) {
51 for (UInt16 data_index = 0; data_index < packet->length; data_index++) {
52 source->parser.parse_fragment(packet->data[data_index]);
53 }
54 packet = MIDIPacketNext(packet);
55 }
56 }
57}
58
59Error MIDIDriverCoreMidi::open() {
60 ERR_FAIL_COND_V_MSG(client || core_midi_closed, FAILED,
61 "MIDIDriverCoreMidi cannot be reopened.");
62
63 CFStringRef name = CFStringCreateWithCString(nullptr, "Godot", kCFStringEncodingASCII);
64 OSStatus result = MIDIClientCreate(name, nullptr, nullptr, &client);
65 CFRelease(name);
66 if (result != noErr) {
67 ERR_PRINT("MIDIClientCreate failed, code: " + itos(result));
68 return ERR_CANT_OPEN;
69 }
70
71 result = MIDIInputPortCreate(client, CFSTR("Godot Input"), MIDIDriverCoreMidi::read, (void *)this, &port_in);
72 if (result != noErr) {
73 ERR_PRINT("MIDIInputPortCreate failed, code: " + itos(result));
74 return ERR_CANT_OPEN;
75 }
76
77 int source_count = MIDIGetNumberOfSources();
78 int connection_index = 0;
79 for (int i = 0; i < source_count; i++) {
80 MIDIEndpointRef source = MIDIGetSource(i);
81 if (source) {
82 InputConnection *conn = memnew(InputConnection(connection_index, source));
83 const OSStatus res = MIDIPortConnectSource(port_in, source, static_cast<void *>(conn));
84 if (res != noErr) {
85 memdelete(conn);
86 } else {
87 connected_sources.push_back(conn);
88
89 CFStringRef nameRef = nullptr;
90 char name[256];
91 MIDIObjectGetStringProperty(source, kMIDIPropertyDisplayName, &nameRef);
92 CFStringGetCString(nameRef, name, sizeof(name), kCFStringEncodingUTF8);
93 CFRelease(nameRef);
94 connected_input_names.push_back(name);
95
96 connection_index++; // Contiguous index for successfully connected inputs.
97 }
98 }
99 }
100
101 return OK;
102}
103
104void MIDIDriverCoreMidi::close() {
105 mutex.lock();
106 core_midi_closed = true;
107 mutex.unlock();
108
109 for (InputConnection *conn : connected_sources) {
110 MIDIPortDisconnectSource(port_in, conn->source);
111 memdelete(conn);
112 }
113
114 connected_sources.clear();
115 connected_input_names.clear();
116
117 if (port_in != 0) {
118 MIDIPortDispose(port_in);
119 port_in = 0;
120 }
121
122 if (client != 0) {
123 MIDIClientDispose(client);
124 client = 0;
125 }
126}
127
128MIDIDriverCoreMidi::~MIDIDriverCoreMidi() {
129 close();
130}
131
132#endif // COREMIDI_ENABLED