1 | /**************************************************************************/ |
2 | /* midi_driver_coremidi.cpp */ |
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 | #include "midi_driver_coremidi.h" |
32 | |
33 | #ifdef COREMIDI_ENABLED |
34 | |
35 | #include "core/string/print_string.h" |
36 | |
37 | #import <CoreAudio/HostTime.h> |
38 | #import <CoreServices/CoreServices.h> |
39 | |
40 | void MIDIDriverCoreMidi::read(const MIDIPacketList *packet_list, void *read_proc_ref_con, void *src_conn_ref_con) { |
41 | MIDIPacket *packet = const_cast<MIDIPacket *>(packet_list->packet); |
42 | for (UInt32 i = 0; i < packet_list->numPackets; i++) { |
43 | receive_input_packet(packet->timeStamp, packet->data, packet->length); |
44 | packet = MIDIPacketNext(packet); |
45 | } |
46 | } |
47 | |
48 | Error MIDIDriverCoreMidi::open() { |
49 | CFStringRef name = CFStringCreateWithCString(nullptr, "Godot" , kCFStringEncodingASCII); |
50 | OSStatus result = MIDIClientCreate(name, nullptr, nullptr, &client); |
51 | CFRelease(name); |
52 | if (result != noErr) { |
53 | ERR_PRINT("MIDIClientCreate failed, code: " + itos(result)); |
54 | return ERR_CANT_OPEN; |
55 | } |
56 | |
57 | result = MIDIInputPortCreate(client, CFSTR("Godot Input" ), MIDIDriverCoreMidi::read, (void *)this, &port_in); |
58 | if (result != noErr) { |
59 | ERR_PRINT("MIDIInputPortCreate failed, code: " + itos(result)); |
60 | return ERR_CANT_OPEN; |
61 | } |
62 | |
63 | int sources = MIDIGetNumberOfSources(); |
64 | for (int i = 0; i < sources; i++) { |
65 | MIDIEndpointRef source = MIDIGetSource(i); |
66 | if (source) { |
67 | MIDIPortConnectSource(port_in, source, (void *)this); |
68 | connected_sources.insert(i, source); |
69 | } |
70 | } |
71 | |
72 | return OK; |
73 | } |
74 | |
75 | void MIDIDriverCoreMidi::close() { |
76 | for (int i = 0; i < connected_sources.size(); i++) { |
77 | MIDIEndpointRef source = connected_sources[i]; |
78 | MIDIPortDisconnectSource(port_in, source); |
79 | } |
80 | connected_sources.clear(); |
81 | |
82 | if (port_in != 0) { |
83 | MIDIPortDispose(port_in); |
84 | port_in = 0; |
85 | } |
86 | |
87 | if (client != 0) { |
88 | MIDIClientDispose(client); |
89 | client = 0; |
90 | } |
91 | } |
92 | |
93 | PackedStringArray MIDIDriverCoreMidi::get_connected_inputs() { |
94 | PackedStringArray list; |
95 | |
96 | for (int i = 0; i < connected_sources.size(); i++) { |
97 | MIDIEndpointRef source = connected_sources[i]; |
98 | CFStringRef ref = nullptr; |
99 | char name[256]; |
100 | |
101 | MIDIObjectGetStringProperty(source, kMIDIPropertyDisplayName, &ref); |
102 | CFStringGetCString(ref, name, sizeof(name), kCFStringEncodingUTF8); |
103 | CFRelease(ref); |
104 | |
105 | list.push_back(name); |
106 | } |
107 | |
108 | return list; |
109 | } |
110 | |
111 | MIDIDriverCoreMidi::MIDIDriverCoreMidi() {} |
112 | |
113 | MIDIDriverCoreMidi::~MIDIDriverCoreMidi() { |
114 | close(); |
115 | } |
116 | |
117 | #endif // COREMIDI_ENABLED |
118 | |