diff options
author | Max Horn | 2011-03-21 15:42:17 +0100 |
---|---|---|
committer | Max Horn | 2011-03-22 23:51:47 +0100 |
commit | 8982fff1b79aa3cc53a5c328b283e0f102cb647f (patch) | |
tree | 01e3835be4fcdf9acbbea5e7878e0c6cfdb5b5fe /backends/midi | |
parent | 92716d71edfe1b23482306e74a1d249c4f43bb0b (diff) | |
download | scummvm-rg350-8982fff1b79aa3cc53a5c328b283e0f102cb647f.tar.gz scummvm-rg350-8982fff1b79aa3cc53a5c328b283e0f102cb647f.tar.bz2 scummvm-rg350-8982fff1b79aa3cc53a5c328b283e0f102cb647f.zip |
AUDIO: Add pure virtual MidiDriver::isOpen() method
This in turn enables modifying MidiDriver_MPU401::close() to allow
it to be called on a midi driver that has not yet been opened.
The specific issue that triggered me to make these changes was a
crash-upon-quit in HUGO, caused by it instantiating a midi driver,
then encountering an error (missing hugo.dat) *before* having
opened the new midi driver; the general cleanup code then tries
to close the (not yet opened) midi driver -> kaboom
Also fixed some engines which were leaking MidiDriver instances.
Diffstat (limited to 'backends/midi')
-rw-r--r-- | backends/midi/alsa.cpp | 1 | ||||
-rw-r--r-- | backends/midi/camd.cpp | 1 | ||||
-rw-r--r-- | backends/midi/coreaudio.cpp | 17 | ||||
-rw-r--r-- | backends/midi/coremidi.cpp | 11 | ||||
-rw-r--r-- | backends/midi/dmedia.cpp | 3 | ||||
-rw-r--r-- | backends/midi/seq.cpp | 1 | ||||
-rw-r--r-- | backends/midi/stmidi.cpp | 39 | ||||
-rw-r--r-- | backends/midi/timidity.cpp | 49 | ||||
-rw-r--r-- | backends/midi/windows.cpp | 3 |
9 files changed, 71 insertions, 54 deletions
diff --git a/backends/midi/alsa.cpp b/backends/midi/alsa.cpp index feed60eaef..2257f60e27 100644 --- a/backends/midi/alsa.cpp +++ b/backends/midi/alsa.cpp @@ -71,6 +71,7 @@ class MidiDriver_ALSA : public MidiDriver_MPU401 { public: MidiDriver_ALSA(int client, int port); int open(); + bool isOpen() const { return _isOpen; } void close(); void send(uint32 b); void sysEx(const byte *msg, uint16 length); diff --git a/backends/midi/camd.cpp b/backends/midi/camd.cpp index 57251b45e1..ee3baaadd2 100644 --- a/backends/midi/camd.cpp +++ b/backends/midi/camd.cpp @@ -46,6 +46,7 @@ class MidiDriver_CAMD : public MidiDriver_MPU401 { public: MidiDriver_CAMD(); int open(); + bool isOpen() const { return _isOpen; } void close(); void send(uint32 b); void sysEx(const byte *msg, uint16 length); diff --git a/backends/midi/coreaudio.cpp b/backends/midi/coreaudio.cpp index 75c512cad7..0158cc4a1d 100644 --- a/backends/midi/coreaudio.cpp +++ b/backends/midi/coreaudio.cpp @@ -72,7 +72,9 @@ do { \ class MidiDriver_CORE : public MidiDriver_MPU401 { public: MidiDriver_CORE(); + ~MidiDriver_CORE(); int open(); + bool isOpen() const { return _auGraph != 0; } void close(); void send(uint32 b); void sysEx(const byte *msg, uint16 length); @@ -86,10 +88,18 @@ MidiDriver_CORE::MidiDriver_CORE() : _auGraph(0) { } +MidiDriver_CORE::~MidiDriver_CORE() { + if (_auGraph) { + AUGraphStop(_auGraph); + DisposeAUGraph(_auGraph); + _auGraph = 0; + } +} + int MidiDriver_CORE::open() { OSStatus err = 0; - if (_auGraph) + if (isOpen()) return MERR_ALREADY_OPEN; // Open the Music Device. @@ -176,7 +186,6 @@ bail: void MidiDriver_CORE::close() { MidiDriver_MPU401::close(); - if (_auGraph) { AUGraphStop(_auGraph); DisposeAUGraph(_auGraph); @@ -185,7 +194,7 @@ void MidiDriver_CORE::close() { } void MidiDriver_CORE::send(uint32 b) { - assert(_auGraph != NULL); + assert(isOpen()); byte status_byte = (b & 0x000000FF); byte first_byte = (b & 0x0000FF00) >> 8; @@ -198,7 +207,7 @@ void MidiDriver_CORE::sysEx(const byte *msg, uint16 length) { unsigned char buf[266]; assert(length + 2 <= ARRAYSIZE(buf)); - assert(_auGraph != NULL); + assert(isOpen()); // Add SysEx frame buf[0] = 0xF0; diff --git a/backends/midi/coremidi.cpp b/backends/midi/coremidi.cpp index c0663352cb..9123c9bc8c 100644 --- a/backends/midi/coremidi.cpp +++ b/backends/midi/coremidi.cpp @@ -56,6 +56,7 @@ public: MidiDriver_CoreMIDI(); ~MidiDriver_CoreMIDI(); int open(); + bool isOpen() const { return mOutPort != 0 && mDest != 0; } void close(); void send(uint32 b); void sysEx(const byte *msg, uint16 length); @@ -80,7 +81,7 @@ MidiDriver_CoreMIDI::~MidiDriver_CoreMIDI() { } int MidiDriver_CoreMIDI::open() { - if (mDest) + if (isOpen()) return MERR_ALREADY_OPEN; OSStatus err = noErr; @@ -106,7 +107,7 @@ int MidiDriver_CoreMIDI::open() { void MidiDriver_CoreMIDI::close() { MidiDriver_MPU401::close(); - if (mOutPort && mDest) { + if (isOpen()) { MIDIPortDispose(mOutPort); mOutPort = 0; mDest = 0; @@ -114,8 +115,7 @@ void MidiDriver_CoreMIDI::close() { } void MidiDriver_CoreMIDI::send(uint32 b) { - assert(mOutPort != 0); - assert(mDest != 0); + assert(isOpen()); // Extract the MIDI data byte status_byte = (b & 0x000000FF); @@ -158,8 +158,7 @@ void MidiDriver_CoreMIDI::send(uint32 b) { } void MidiDriver_CoreMIDI::sysEx(const byte *msg, uint16 length) { - assert(mOutPort != 0); - assert(mDest != 0); + assert(isOpen()); byte buf[384]; MIDIPacketList *packetList = (MIDIPacketList *)buf; diff --git a/backends/midi/dmedia.cpp b/backends/midi/dmedia.cpp index f0ab84c934..c4c1968354 100644 --- a/backends/midi/dmedia.cpp +++ b/backends/midi/dmedia.cpp @@ -57,6 +57,7 @@ class MidiDriver_DMEDIA : public MidiDriver_MPU401 { public: MidiDriver_DMEDIA(); int open(); + bool isOpen() const { return _isOpen; } void close(); void send(uint32 b); void sysEx(const byte *msg, uint16 length); @@ -179,7 +180,7 @@ void MidiDriver_DMEDIA::sysEx (const byte *msg, uint16 length) { memcpy(buf, msg, length); buf[length] = MD_EOX; event.sysexmsg = buf; - event.msglen = length; + event.msglen = length; event.msg[0] = MD_SYSEX; event.msg[1] = 0; event.msg[2] = 0; diff --git a/backends/midi/seq.cpp b/backends/midi/seq.cpp index 064129b74a..7cd7f0a38e 100644 --- a/backends/midi/seq.cpp +++ b/backends/midi/seq.cpp @@ -55,6 +55,7 @@ class MidiDriver_SEQ : public MidiDriver_MPU401 { public: MidiDriver_SEQ(); int open(); + bool isOpen() const { return _isOpen; } void close(); void send(uint32 b); void sysEx(const byte *msg, uint16 length); diff --git a/backends/midi/stmidi.cpp b/backends/midi/stmidi.cpp index 562c3c7ece..42f829a7a3 100644 --- a/backends/midi/stmidi.cpp +++ b/backends/midi/stmidi.cpp @@ -48,8 +48,9 @@ class MidiDriver_STMIDI : public MidiDriver_MPU401 { public: - MidiDriver_STMIDI() : _isOpen (false) { } + MidiDriver_STMIDI() : _isOpen (false) { } int open(); + bool isOpen() const { return _isOpen; } void close(); void send(uint32 b); void sysEx(const byte *msg, uint16 length); @@ -59,7 +60,7 @@ private: }; int MidiDriver_STMIDI::open() { - if ((_isOpen) && (!Bcostat(4))) + if (_isOpen && (!Bcostat(4))) return MERR_ALREADY_OPEN; warning("ST Midi Port Open"); _isOpen = true; @@ -123,36 +124,36 @@ void MidiDriver_STMIDI::sysEx (const byte *msg, uint16 length) { class StMidiMusicPlugin : public MusicPluginObject { public: - const char *getName() const { - return "STMIDI"; - } + const char *getName() const { + return "STMIDI"; + } - const char *getId() const { - return "stmidi"; - } + const char *getId() const { + return "stmidi"; + } - MusicDevices getDevices() const; - Common::Error createInstance(MidiDriver **mididriver, MidiDriver::DeviceHandle = 0) const; + MusicDevices getDevices() const; + Common::Error createInstance(MidiDriver **mididriver, MidiDriver::DeviceHandle = 0) const; }; MusicDevices StMidiMusicPlugin::getDevices() const { - MusicDevices devices; - // TODO: Return a different music type depending on the configuration - // TODO: List the available devices - devices.push_back(MusicDevice(this, "", MT_GM)); - return devices; + MusicDevices devices; + // TODO: Return a different music type depending on the configuration + // TODO: List the available devices + devices.push_back(MusicDevice(this, "", MT_GM)); + return devices; } Common::Error StMidiMusicPlugin::createInstance(MidiDriver **mididriver, MidiDriver::DeviceHandle) const { - *mididriver = new MidiDriver_STMIDI(); + *mididriver = new MidiDriver_STMIDI(); - return Common::kNoError; + return Common::kNoError; } //#if PLUGIN_ENABLED_DYNAMIC(STMIDI) - //REGISTER_PLUGIN_DYNAMIC(STMIDI, PLUGIN_TYPE_MUSIC, StMidiMusicPlugin); + //REGISTER_PLUGIN_DYNAMIC(STMIDI, PLUGIN_TYPE_MUSIC, StMidiMusicPlugin); //#else - REGISTER_PLUGIN_STATIC(STMIDI, PLUGIN_TYPE_MUSIC, StMidiMusicPlugin); + REGISTER_PLUGIN_STATIC(STMIDI, PLUGIN_TYPE_MUSIC, StMidiMusicPlugin); //#endif #endif diff --git a/backends/midi/timidity.cpp b/backends/midi/timidity.cpp index eeaf872747..642ddf6917 100644 --- a/backends/midi/timidity.cpp +++ b/backends/midi/timidity.cpp @@ -92,49 +92,50 @@ class MidiDriver_TIMIDITY : public MidiDriver_MPU401 { public: MidiDriver_TIMIDITY(); - int open(); - void close(); - void send(uint32 b); - void sysEx(const byte *msg, uint16 length); + int open(); + bool isOpen() const { return _isOpen; } + void close(); + void send(uint32 b); + void sysEx(const byte *msg, uint16 length); private: /* standart routine to extract ip address from a string */ - in_addr_t host_to_addr(const char* address); + in_addr_t host_to_addr(const char* address); /* creates a tcp connection to TiMidity server, returns filedesc (like open()) */ - int connect_to_server(const char* hostname, unsigned short tcp_port); + int connect_to_server(const char* hostname, unsigned short tcp_port); /* send command to the server; printf-like; returns reply string */ - char *timidity_ctl_command(const char *fmt, ...) GCC_PRINTF(2, 3); + char *timidity_ctl_command(const char *fmt, ...) GCC_PRINTF(2, 3); /* timidity data socket-related stuff */ - void timidity_meta_seq(int p1, int p2, int p3); - int timidity_sync(int centsec); - int timidity_eot(); + void timidity_meta_seq(int p1, int p2, int p3); + int timidity_sync(int centsec); + int timidity_eot(); /* write() analogue for any midi data */ - void timidity_write_data(const void *buf, size_t nbytes); + void timidity_write_data(const void *buf, size_t nbytes); /* get single line of server reply on control connection */ - int fdgets(char *buff, size_t buff_size); + int fdgets(char *buff, size_t buff_size); /* teardown connection to server */ - void teardown(); + void teardown(); /* close (if needed) and nullify both control and data filedescs */ - void close_all(); + void close_all(); private: - bool _isOpen; - int _device_num; + bool _isOpen; + int _device_num; - int _control_fd; - int _data_fd; + int _control_fd; + int _data_fd; /* buffer for partial data read from _control_fd - from timidity-io.c, see fdgets() */ - char _controlbuffer[BUFSIZ]; - int _controlbuffer_count; /* beginning of read pointer */ - int _controlbuffer_size; /* end of read pointer */ + char _controlbuffer[BUFSIZ]; + int _controlbuffer_count; /* beginning of read pointer */ + int _controlbuffer_size; /* end of read pointer */ }; MidiDriver_TIMIDITY::MidiDriver_TIMIDITY() { @@ -149,9 +150,9 @@ MidiDriver_TIMIDITY::MidiDriver_TIMIDITY() { } int MidiDriver_TIMIDITY::open() { - char *res; - char timidity_host[MAXHOSTNAMELEN]; - int timidity_port, data_port, i; + char *res; + char timidity_host[MAXHOSTNAMELEN]; + int timidity_port, data_port, i; /* count ourselves open */ if (_isOpen) diff --git a/backends/midi/windows.cpp b/backends/midi/windows.cpp index b585b71fbe..aad7f48d94 100644 --- a/backends/midi/windows.cpp +++ b/backends/midi/windows.cpp @@ -61,6 +61,7 @@ private: public: MidiDriver_WIN(int deviceIndex) : _isOpen(false), _device(deviceIndex) { } int open(); + bool isOpen() const { return _isOpen; } void close(); void send(uint32 b); void sysEx(const byte *msg, uint16 length); @@ -93,6 +94,8 @@ void MidiDriver_WIN::close() { } void MidiDriver_WIN::send(uint32 b) { + assert(_isOpen); + union { DWORD dwData; BYTE bData[4]; |