MACOSX: Allow selecting device for CoreMidi

This commit is contained in:
Thierry Crozat 2017-10-03 00:30:13 +01:00
parent 6843870af2
commit 9a07050888

View File

@ -53,7 +53,7 @@ http://lists.apple.com/archives/coreaudio-api/2003/Jul/msg00137.html
*/
class MidiDriver_CoreMIDI : public MidiDriver_MPU401 {
public:
MidiDriver_CoreMIDI();
MidiDriver_CoreMIDI(ItemCount device);
~MidiDriver_CoreMIDI();
int open();
bool isOpen() const { return mOutPort != 0 && mDest != 0; }
@ -62,13 +62,14 @@ public:
void sysEx(const byte *msg, uint16 length);
private:
ItemCount mDevice;
MIDIClientRef mClient;
MIDIPortRef mOutPort;
MIDIEndpointRef mDest;
};
MidiDriver_CoreMIDI::MidiDriver_CoreMIDI()
: mClient(0), mOutPort(0), mDest(0) {
MidiDriver_CoreMIDI::MidiDriver_CoreMIDI(ItemCount device)
: mDevice(device), mClient(0), mOutPort(0), mDest(0) {
OSStatus err;
err = MIDIClientCreate(CFSTR("ScummVM MIDI Driver for OS X"), NULL, NULL, &mClient);
@ -88,9 +89,9 @@ int MidiDriver_CoreMIDI::open() {
mOutPort = 0;
int dests = MIDIGetNumberOfDestinations();
if (dests > 0 && mClient) {
mDest = MIDIGetDestination(0);
ItemCount dests = MIDIGetNumberOfDestinations();
if (mDevice < dests && mClient) {
mDest = MIDIGetDestination(mDevice);
err = MIDIOutputPortCreate( mClient,
CFSTR("scummvm_output_port"),
&mOutPort);
@ -195,21 +196,60 @@ public:
MusicDevices getDevices() const;
Common::Error createInstance(MidiDriver **mididriver, MidiDriver::DeviceHandle = 0) const;
private:
bool getDeviceName(ItemCount deviceIndex, Common::String &outName) const;
};
MusicDevices CoreMIDIMusicPlugin::getDevices() const {
// TODO: Is it possible to get the music type for each device?
// Maybe look at the kMIDIPropertyModel property?
MusicDevices devices;
// TODO: Return a different music type depending on the configuration
// TODO: List the available devices
devices.push_back(MusicDevice(this, "", MT_GM));
ItemCount deviceCount = MIDIGetNumberOfDestinations();
for (ItemCount i = 0 ; i < deviceCount ; ++i) {
Common::String name;
if (getDeviceName(i, name))
devices.push_back(MusicDevice(this, name, MT_GM));
}
return devices;
}
Common::Error CoreMIDIMusicPlugin::createInstance(MidiDriver **mididriver, MidiDriver::DeviceHandle) const {
*mididriver = new MidiDriver_CoreMIDI();
Common::Error CoreMIDIMusicPlugin::createInstance(MidiDriver **mididriver, MidiDriver::DeviceHandle device) const {
ItemCount deviceCount = MIDIGetNumberOfDestinations();
for (ItemCount i = 0 ; i < deviceCount ; ++i) {
Common::String name;
if (getDeviceName(i, name)) {
MusicDevice md(this, name, MT_GM);
if (md.getHandle() == device) {
*mididriver = new MidiDriver_CoreMIDI(i);
return Common::kNoError;
}
}
}
return Common::kUnknownError;
}
bool CoreMIDIMusicPlugin::getDeviceName(ItemCount deviceIndex, Common::String &outName) const {
MIDIEndpointRef dest = MIDIGetDestination(deviceIndex);
if (!dest)
return false;
CFStringRef name = nil;
if (MIDIObjectGetStringProperty(dest, kMIDIPropertyDisplayName, &name) == noErr) {
char buffer[128];
if (CFStringGetCString(name, buffer, sizeof(buffer), kCFStringEncodingASCII)) {
outName = buffer;
CFRelease(name);
return true;
}
CFRelease(name);
}
// Rather than fail use a default name
warning("Failed to get name for CoreMIDi device %lu", deviceIndex);
outName = Common::String::format("Unknown Device %lu", deviceIndex);
return true;
}
//#if PLUGIN_ENABLED_DYNAMIC(COREMIDI)
//REGISTER_PLUGIN_DYNAMIC(COREMIDI, PLUGIN_TYPE_MUSIC, CoreMIDIMusicPlugin);