DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/gui/midi_coremidi.h
00001 /*
00002  *  This program is free software; you can redistribute it and/or modify
00003  *  it under the terms of the GNU General Public License as published by
00004  *  the Free Software Foundation; either version 2 of the License, or
00005  *  (at your option) any later version.
00006  *
00007  *  This program is distributed in the hope that it will be useful,
00008  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  *  GNU General Public License for more details.
00011  *
00012  *  You should have received a copy of the GNU General Public License along
00013  *  with this program; if not, write to the Free Software Foundation, Inc.,
00014  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00015  */
00016 
00017 #include <CoreMIDI/MIDIServices.h>
00018 #include <sstream>
00019 #include <string>
00020 
00021 class MidiHandler_coremidi : public MidiHandler {
00022 private:
00023         MIDIPortRef m_port;
00024         MIDIClientRef m_client;
00025         MIDIEndpointRef m_endpoint;
00026         MIDIPacket* m_pCurPacket;
00027 public:
00028         MidiHandler_coremidi()  {m_pCurPacket = 0;}
00029         const char * GetName(void) { return "coremidi"; }
00030         bool Open(const char * conf) {  
00031                 // Get the MIDIEndPoint
00032                 m_endpoint = 0;
00033 //              OSStatus result;
00034                 Bitu numDests = MIDIGetNumberOfDestinations();
00035                 Bitu destId = numDests;
00036                 if(conf && *conf) {
00037                         std::string strconf(conf);
00038                         std::istringstream configmidi(strconf);
00039                         configmidi >> destId;
00040                         if (configmidi.fail() && numDests) {
00041                                 lowcase(strconf);
00042                                 for(Bitu i = 0; i<numDests; i++) {
00043                                         MIDIEndpointRef dummy = MIDIGetDestination(i);
00044                                         if (!dummy) continue;
00045                                         CFStringRef midiname = 0;
00046                                         if (MIDIObjectGetStringProperty(dummy,kMIDIPropertyDisplayName,&midiname) == noErr) {
00047                                                 const char* s = CFStringGetCStringPtr(midiname,kCFStringEncodingMacRoman);
00048                                                 if (s) {
00049                                                         std::string devname(s);
00050                                                         lowcase(devname);
00051                                                         if (devname.find(strconf) != std::string::npos) { 
00052                                                                 destId = i;
00053                                                                 break;
00054                                                         }
00055                                                 }
00056                                         }
00057                                 }
00058                         }
00059                 }
00060                 if (destId >= numDests) destId = 0;
00061 
00062                 if (destId < numDests)
00063                 {
00064                         m_endpoint = MIDIGetDestination(destId);
00065                 }
00066 
00067                 // Create a MIDI client and port
00068                 MIDIClientCreate(CFSTR("MyClient"), 0, 0, &m_client);
00069 
00070                 if (!m_client)
00071                 {
00072                         LOG_MSG("MIDI:coremidi: No client created.");
00073                         return false;
00074                 }
00075 
00076                 MIDIOutputPortCreate(m_client, CFSTR("MyOutPort"), &m_port);
00077 
00078                 if (!m_port)
00079                 {
00080                         LOG_MSG("MIDI:coremidi: No port created.");
00081                         return false;
00082                 }
00083 
00084                 
00085                 return true;
00086         }
00087         
00088         void Close(void) {
00089                 // Dispose the port
00090                 MIDIPortDispose(m_port);
00091 
00092                 // Dispose the client
00093                 MIDIClientDispose(m_client);
00094 
00095                 // Dispose the endpoint
00096                 // Not, as it is for Endpoints created by us
00097 //              MIDIEndpointDispose(m_endpoint);
00098         }
00099         
00100         void PlayMsg(Bit8u * msg) {
00101                 // Acquire a MIDIPacketList
00102                 Byte packetBuf[128];
00103                 MIDIPacketList *packetList = (MIDIPacketList *)packetBuf;
00104                 m_pCurPacket = MIDIPacketListInit(packetList);
00105                 
00106                 // Determine the length of msg
00107                 Bitu len=MIDI_evt_len[*msg];
00108                 
00109                 // Add msg to the MIDIPacketList
00110                 MIDIPacketListAdd(packetList, (ByteCount)sizeof(packetBuf), m_pCurPacket, (MIDITimeStamp)0, len, msg);
00111                 
00112                 // Send the MIDIPacketList
00113                 MIDISend(m_port,m_endpoint,packetList);
00114         }
00115         
00116         void PlaySysex(Bit8u * sysex, Bitu len) {
00117                 // Acquire a MIDIPacketList
00118                 Byte packetBuf[SYSEX_SIZE*4];
00119 //              Bitu pos=0;
00120                 MIDIPacketList *packetList = (MIDIPacketList *)packetBuf;
00121                 m_pCurPacket = MIDIPacketListInit(packetList);
00122                 
00123                 // Add msg to the MIDIPacketList
00124                 MIDIPacketListAdd(packetList, (ByteCount)sizeof(packetBuf), m_pCurPacket, (MIDITimeStamp)0, len, sysex);
00125                 
00126                 // Send the MIDIPacketList
00127                 MIDISend(m_port,m_endpoint,packetList);
00128         }
00129         
00130         void ListAll(Program* base) {
00131                 Bitu numDests = MIDIGetNumberOfDestinations();
00132                 for(Bitu i = 0; i < numDests; i++){
00133                         MIDIEndpointRef dest = MIDIGetDestination(i);
00134                         if (!dest) continue;
00135                         CFStringRef midiname = 0;
00136                         if(MIDIObjectGetStringProperty(dest, kMIDIPropertyDisplayName, &midiname) == noErr) {
00137                                 const char * s = CFStringGetCStringPtr(midiname, kCFStringEncodingMacRoman);
00138                                 if (s) base->WriteOut("%02d\t%s\n",i,s);
00139                         }
00140                         //This is for EndPoints created by us.
00141                         //MIDIEndpointDispose(dest);
00142                 }
00143 
00144         }
00145 };
00146 
00147 MidiHandler_coremidi Midi_coremidi;
00148