| 1 | /**************************************************************************/ |
| 2 | /* midi_driver_alsamidi.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 | #ifdef ALSAMIDI_ENABLED |
| 32 | |
| 33 | #include "midi_driver_alsamidi.h" |
| 34 | |
| 35 | #include "core/os/os.h" |
| 36 | |
| 37 | #include <cerrno> |
| 38 | |
| 39 | MIDIDriverALSAMidi::InputConnection::InputConnection(int p_device_index, |
| 40 | snd_rawmidi_t *p_rawmidi) : |
| 41 | parser(p_device_index), rawmidi_ptr(p_rawmidi) {} |
| 42 | |
| 43 | void MIDIDriverALSAMidi::InputConnection::read() { |
| 44 | int read_count; |
| 45 | do { |
| 46 | uint8_t buffer[32]; |
| 47 | read_count = snd_rawmidi_read(rawmidi_ptr, buffer, sizeof(buffer)); |
| 48 | |
| 49 | if (read_count < 0) { |
| 50 | if (read_count != -EAGAIN) { |
| 51 | ERR_PRINT("snd_rawmidi_read error: " + String(snd_strerror(read_count))); |
| 52 | } |
| 53 | } else { |
| 54 | for (int i = 0; i < read_count; i++) { |
| 55 | parser.parse_fragment(buffer[i]); |
| 56 | } |
| 57 | } |
| 58 | } while (read_count > 0); |
| 59 | } |
| 60 | |
| 61 | void MIDIDriverALSAMidi::thread_func(void *p_udata) { |
| 62 | MIDIDriverALSAMidi *md = static_cast<MIDIDriverALSAMidi *>(p_udata); |
| 63 | |
| 64 | while (!md->exit_thread.is_set()) { |
| 65 | md->lock(); |
| 66 | for (InputConnection &conn : md->connected_inputs) { |
| 67 | conn.read(); |
| 68 | } |
| 69 | md->unlock(); |
| 70 | |
| 71 | OS::get_singleton()->delay_usec(1000); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | Error MIDIDriverALSAMidi::open() { |
| 76 | void **hints; |
| 77 | |
| 78 | if (snd_device_name_hint(-1, "rawmidi", &hints) < 0) { |
| 79 | return ERR_CANT_OPEN; |
| 80 | } |
| 81 | |
| 82 | lock(); |
| 83 | int device_index = 0; |
| 84 | for (void **h = hints; *h != nullptr; h++) { |
| 85 | char *name = snd_device_name_get_hint(*h, "NAME"); |
| 86 | |
| 87 | if (name != nullptr) { |
| 88 | snd_rawmidi_t *midi_in; |
| 89 | int ret = snd_rawmidi_open(&midi_in, nullptr, name, SND_RAWMIDI_NONBLOCK); |
| 90 | if (ret >= 0) { |
| 91 | // Get display name. |
| 92 | snd_rawmidi_info_t *info; |
| 93 | snd_rawmidi_info_malloc(&info); |
| 94 | snd_rawmidi_info(midi_in, info); |
| 95 | connected_input_names.push_back(snd_rawmidi_info_get_name(info)); |
| 96 | snd_rawmidi_info_free(info); |
| 97 | |
| 98 | connected_inputs.push_back(InputConnection(device_index, midi_in)); |
| 99 | // Only increment device_index for successfully connected devices. |
| 100 | device_index++; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (name != nullptr) { |
| 105 | free(name); |
| 106 | } |
| 107 | } |
| 108 | snd_device_name_free_hint(hints); |
| 109 | unlock(); |
| 110 | |
| 111 | exit_thread.clear(); |
| 112 | thread.start(MIDIDriverALSAMidi::thread_func, this); |
| 113 | |
| 114 | return OK; |
| 115 | } |
| 116 | |
| 117 | void MIDIDriverALSAMidi::close() { |
| 118 | exit_thread.set(); |
| 119 | if (thread.is_started()) { |
| 120 | thread.wait_to_finish(); |
| 121 | } |
| 122 | |
| 123 | for (const InputConnection &conn : connected_inputs) { |
| 124 | snd_rawmidi_close(conn.rawmidi_ptr); |
| 125 | } |
| 126 | |
| 127 | connected_inputs.clear(); |
| 128 | connected_input_names.clear(); |
| 129 | } |
| 130 | |
| 131 | void MIDIDriverALSAMidi::lock() const { |
| 132 | mutex.lock(); |
| 133 | } |
| 134 | |
| 135 | void MIDIDriverALSAMidi::unlock() const { |
| 136 | mutex.unlock(); |
| 137 | } |
| 138 | |
| 139 | MIDIDriverALSAMidi::MIDIDriverALSAMidi() { |
| 140 | exit_thread.clear(); |
| 141 | } |
| 142 | |
| 143 | MIDIDriverALSAMidi::~MIDIDriverALSAMidi() { |
| 144 | close(); |
| 145 | } |
| 146 | |
| 147 | #endif // ALSAMIDI_ENABLED |