1 | /**************************************************************************/ |
2 | /* midi_driver_alsamidi.h */ |
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 | #ifndef MIDI_DRIVER_ALSAMIDI_H |
32 | #define MIDI_DRIVER_ALSAMIDI_H |
33 | |
34 | #ifdef ALSAMIDI_ENABLED |
35 | |
36 | #include "core/os/midi_driver.h" |
37 | #include "core/os/mutex.h" |
38 | #include "core/os/thread.h" |
39 | #include "core/templates/safe_refcount.h" |
40 | #include "core/templates/vector.h" |
41 | |
42 | #ifdef SOWRAP_ENABLED |
43 | #include "../alsa/asound-so_wrap.h" |
44 | #else |
45 | #include <alsa/asoundlib.h> |
46 | #endif |
47 | |
48 | #include <stdio.h> |
49 | |
50 | class MIDIDriverALSAMidi : public MIDIDriver { |
51 | Thread thread; |
52 | Mutex mutex; |
53 | |
54 | class InputConnection { |
55 | public: |
56 | InputConnection() = default; |
57 | InputConnection(snd_rawmidi_t *midi_in) : |
58 | rawmidi_ptr{ midi_in } {} |
59 | |
60 | // Read in and parse available data, forwarding any complete messages through the driver. |
61 | int read_in(MIDIDriverALSAMidi &driver, uint64_t timestamp); |
62 | |
63 | snd_rawmidi_t *rawmidi_ptr = nullptr; |
64 | |
65 | private: |
66 | static const size_t MSG_BUFFER_SIZE = 3; |
67 | uint8_t buffer[MSG_BUFFER_SIZE] = { 0 }; |
68 | size_t expected_data = 0; |
69 | size_t received_data = 0; |
70 | bool skipping_sys_ex = false; |
71 | void parse_byte(uint8_t byte, MIDIDriverALSAMidi &driver, uint64_t timestamp); |
72 | }; |
73 | |
74 | Vector<InputConnection> connected_inputs; |
75 | |
76 | SafeFlag exit_thread; |
77 | |
78 | static void thread_func(void *p_udata); |
79 | |
80 | enum class MessageCategory { |
81 | Data, |
82 | Voice, |
83 | SysExBegin, |
84 | SystemCommon, // excluding System Exclusive Begin/End |
85 | SysExEnd, |
86 | RealTime, |
87 | }; |
88 | |
89 | // If the passed byte is a status byte, return the associated message category, |
90 | // else return MessageCategory::Data. |
91 | static MessageCategory msg_category(uint8_t msg_part); |
92 | |
93 | // Return the number of data bytes expected for the provided status byte. |
94 | static size_t msg_expected_data(uint8_t status_byte); |
95 | |
96 | void lock() const; |
97 | void unlock() const; |
98 | |
99 | public: |
100 | virtual Error open(); |
101 | virtual void close(); |
102 | |
103 | virtual PackedStringArray get_connected_inputs(); |
104 | |
105 | MIDIDriverALSAMidi(); |
106 | virtual ~MIDIDriverALSAMidi(); |
107 | }; |
108 | |
109 | #endif // ALSAMIDI_ENABLED |
110 | |
111 | #endif // MIDI_DRIVER_ALSAMIDI_H |
112 | |