1 | /**************************************************************************/ |
2 | /* midi_driver_winmidi.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 WINMIDI_ENABLED |
32 | |
33 | #include "midi_driver_winmidi.h" |
34 | |
35 | #include "core/string/print_string.h" |
36 | |
37 | void MIDIDriverWinMidi::read(HMIDIIN hMidiIn, UINT wMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2) { |
38 | if (wMsg == MIM_DATA) { |
39 | receive_input_packet((uint64_t)dwParam2, (uint8_t *)&dwParam1, 3); |
40 | } |
41 | } |
42 | |
43 | Error MIDIDriverWinMidi::open() { |
44 | for (UINT i = 0; i < midiInGetNumDevs(); i++) { |
45 | HMIDIIN midi_in; |
46 | |
47 | MMRESULT res = midiInOpen(&midi_in, i, (DWORD_PTR)read, (DWORD_PTR)this, CALLBACK_FUNCTION); |
48 | if (res == MMSYSERR_NOERROR) { |
49 | midiInStart(midi_in); |
50 | connected_sources.insert(i, midi_in); |
51 | } else { |
52 | char err[256]; |
53 | midiInGetErrorText(res, err, 256); |
54 | ERR_PRINT("midiInOpen error: " + String(err)); |
55 | |
56 | MIDIINCAPS caps; |
57 | res = midiInGetDevCaps(i, &caps, sizeof(MIDIINCAPS)); |
58 | if (res == MMSYSERR_NOERROR) { |
59 | ERR_PRINT("Can't open MIDI device \"" + String(caps.szPname) + "\", is it being used by another application?" ); |
60 | } |
61 | } |
62 | } |
63 | |
64 | return OK; |
65 | } |
66 | |
67 | PackedStringArray MIDIDriverWinMidi::get_connected_inputs() { |
68 | PackedStringArray list; |
69 | |
70 | for (int i = 0; i < connected_sources.size(); i++) { |
71 | HMIDIIN midi_in = connected_sources[i]; |
72 | UINT id = 0; |
73 | MMRESULT res = midiInGetID(midi_in, &id); |
74 | if (res == MMSYSERR_NOERROR) { |
75 | MIDIINCAPS caps; |
76 | res = midiInGetDevCaps(i, &caps, sizeof(MIDIINCAPS)); |
77 | if (res == MMSYSERR_NOERROR) { |
78 | list.push_back(caps.szPname); |
79 | } |
80 | } |
81 | } |
82 | |
83 | return list; |
84 | } |
85 | |
86 | void MIDIDriverWinMidi::close() { |
87 | for (int i = 0; i < connected_sources.size(); i++) { |
88 | HMIDIIN midi_in = connected_sources[i]; |
89 | midiInStop(midi_in); |
90 | midiInClose(midi_in); |
91 | } |
92 | connected_sources.clear(); |
93 | } |
94 | |
95 | MIDIDriverWinMidi::MIDIDriverWinMidi() { |
96 | } |
97 | |
98 | MIDIDriverWinMidi::~MIDIDriverWinMidi() { |
99 | close(); |
100 | } |
101 | |
102 | #endif // WINMIDI_ENABLED |
103 | |