2018-07-14 12:11:28 +00:00
|
|
|
/**************************************************************************/
|
2019-02-12 13:23:35 +00:00
|
|
|
/* midi_driver_alsamidi.cpp */
|
2018-07-14 12:11:28 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
#ifdef ALSAMIDI_ENABLED
|
|
|
|
|
2019-02-12 13:23:35 +00:00
|
|
|
#include "midi_driver_alsamidi.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
|
|
|
|
#include "core/os/os.h"
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/string/print_string.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
2018-07-14 12:11:28 +00:00
|
|
|
|
2024-04-10 09:50:28 +00:00
|
|
|
MIDIDriverALSAMidi::InputConnection::InputConnection(int p_device_index,
|
|
|
|
snd_rawmidi_t *p_rawmidi) :
|
|
|
|
parser(p_device_index), rawmidi_ptr(p_rawmidi) {}
|
2018-07-14 12:11:28 +00:00
|
|
|
|
2024-04-10 09:50:28 +00:00
|
|
|
void MIDIDriverALSAMidi::InputConnection::read() {
|
|
|
|
int read_count;
|
2021-10-27 14:14:19 +00:00
|
|
|
do {
|
2024-04-10 09:50:28 +00:00
|
|
|
uint8_t buffer[32];
|
|
|
|
read_count = snd_rawmidi_read(rawmidi_ptr, buffer, sizeof(buffer));
|
2021-10-27 14:14:19 +00:00
|
|
|
|
2024-04-10 09:50:28 +00:00
|
|
|
if (read_count < 0) {
|
|
|
|
if (read_count != -EAGAIN) {
|
|
|
|
ERR_PRINT("snd_rawmidi_read error: " + String(snd_strerror(read_count)));
|
2021-10-27 14:14:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-04-10 09:50:28 +00:00
|
|
|
for (int i = 0; i < read_count; i++) {
|
|
|
|
parser.parse_fragment(buffer[i]);
|
|
|
|
}
|
2021-10-27 14:14:19 +00:00
|
|
|
}
|
2024-04-10 09:50:28 +00:00
|
|
|
} while (read_count > 0);
|
2018-07-14 12:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MIDIDriverALSAMidi::thread_func(void *p_udata) {
|
2022-04-05 10:40:26 +00:00
|
|
|
MIDIDriverALSAMidi *md = static_cast<MIDIDriverALSAMidi *>(p_udata);
|
2018-07-14 12:11:28 +00:00
|
|
|
|
2022-08-19 08:38:13 +00:00
|
|
|
while (!md->exit_thread.is_set()) {
|
2018-07-14 12:11:28 +00:00
|
|
|
md->lock();
|
2024-04-10 09:50:28 +00:00
|
|
|
for (InputConnection &conn : md->connected_inputs) {
|
|
|
|
conn.read();
|
2018-07-14 12:11:28 +00:00
|
|
|
}
|
|
|
|
md->unlock();
|
|
|
|
|
|
|
|
OS::get_singleton()->delay_usec(1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Error MIDIDriverALSAMidi::open() {
|
|
|
|
void **hints;
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (snd_device_name_hint(-1, "rawmidi", &hints) < 0) {
|
2018-07-14 12:11:28 +00:00
|
|
|
return ERR_CANT_OPEN;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-07-14 12:11:28 +00:00
|
|
|
|
2024-04-10 09:50:28 +00:00
|
|
|
lock();
|
|
|
|
int device_index = 0;
|
|
|
|
for (void **h = hints; *h != nullptr; h++) {
|
|
|
|
char *name = snd_device_name_get_hint(*h, "NAME");
|
2018-07-14 12:11:28 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
if (name != nullptr) {
|
2018-07-14 12:11:28 +00:00
|
|
|
snd_rawmidi_t *midi_in;
|
2020-04-01 23:20:12 +00:00
|
|
|
int ret = snd_rawmidi_open(&midi_in, nullptr, name, SND_RAWMIDI_NONBLOCK);
|
2018-07-14 12:11:28 +00:00
|
|
|
if (ret >= 0) {
|
2024-04-10 09:50:28 +00:00
|
|
|
// Get display name.
|
|
|
|
snd_rawmidi_info_t *info;
|
|
|
|
snd_rawmidi_info_malloc(&info);
|
|
|
|
snd_rawmidi_info(midi_in, info);
|
|
|
|
connected_input_names.push_back(snd_rawmidi_info_get_name(info));
|
|
|
|
snd_rawmidi_info_free(info);
|
|
|
|
|
|
|
|
connected_inputs.push_back(InputConnection(device_index, midi_in));
|
|
|
|
// Only increment device_index for successfully connected devices.
|
|
|
|
device_index++;
|
2018-07-14 12:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (name != nullptr) {
|
2018-07-14 12:11:28 +00:00
|
|
|
free(name);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-07-14 12:11:28 +00:00
|
|
|
}
|
|
|
|
snd_device_name_free_hint(hints);
|
2024-04-10 09:50:28 +00:00
|
|
|
unlock();
|
2018-07-14 12:11:28 +00:00
|
|
|
|
2022-08-19 08:38:13 +00:00
|
|
|
exit_thread.clear();
|
2021-01-19 12:29:41 +00:00
|
|
|
thread.start(MIDIDriverALSAMidi::thread_func, this);
|
2018-07-14 12:11:28 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MIDIDriverALSAMidi::close() {
|
2022-08-19 08:38:13 +00:00
|
|
|
exit_thread.set();
|
2023-04-27 16:34:30 +00:00
|
|
|
if (thread.is_started()) {
|
|
|
|
thread.wait_to_finish();
|
|
|
|
}
|
2018-07-14 12:11:28 +00:00
|
|
|
|
2024-04-10 09:50:28 +00:00
|
|
|
for (const InputConnection &conn : connected_inputs) {
|
|
|
|
snd_rawmidi_close(conn.rawmidi_ptr);
|
2018-07-14 12:11:28 +00:00
|
|
|
}
|
2024-04-10 09:50:28 +00:00
|
|
|
|
2018-07-14 12:11:28 +00:00
|
|
|
connected_inputs.clear();
|
2024-04-10 09:50:28 +00:00
|
|
|
connected_input_names.clear();
|
2018-07-14 12:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MIDIDriverALSAMidi::lock() const {
|
2020-02-26 10:28:13 +00:00
|
|
|
mutex.lock();
|
2018-07-14 12:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MIDIDriverALSAMidi::unlock() const {
|
2020-02-26 10:28:13 +00:00
|
|
|
mutex.unlock();
|
2018-07-14 12:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MIDIDriverALSAMidi::MIDIDriverALSAMidi() {
|
2022-08-19 08:38:13 +00:00
|
|
|
exit_thread.clear();
|
2018-07-14 12:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MIDIDriverALSAMidi::~MIDIDriverALSAMidi() {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2019-02-12 13:23:35 +00:00
|
|
|
#endif // ALSAMIDI_ENABLED
|