2 * Sample MIDI Wine Driver for MacOSX (based on OSS midi driver)
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1998 Luiz Otavio L. Zorzella (init procedures)
6 * Copyright 1998/1999 Eric POUECH :
7 * 98/7 changes for making this MIDI driver work on OSS
8 * current support is limited to MIDI ports of OSS systems
9 * 98/9 rewriting MCI code for MIDI
10 * 98/11 splitted in midi.c and mcimidi.c
11 * Copyright 2006 Emmanuel Maillard
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "wine/port.h"
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(midi);
46 #if defined(HAVE_COREAUDIO_COREAUDIO_H)
47 #include <CoreAudio/CoreAudio.h>
49 #define WINE_DEFINITIONS
52 static MIDIClientRef wineMIDIClient = NULL;
54 static DWORD MIDIOut_NumDevs = 0;
56 typedef struct tagMIDIDestination {
57 /* graph and synth are only used for MIDI Synth */
63 MIDIOPENDESC midiDesc;
67 #define MAX_MIDI_SYNTHS 1
69 MIDIDestination *destinations;
71 extern int SynthUnit_CreateDefaultSynthUnit(AUGraph *graph, AudioUnit *synth);
72 extern int SynthUnit_Initialize(AudioUnit synth, AUGraph graph);
73 extern int SynthUnit_Close(AUGraph graph);
76 LONG CoreAudio_MIDIInit(void)
79 CHAR szPname[MAXPNAMELEN] = {0};
81 int numDest = MIDIGetNumberOfDestinations();
82 CFStringRef name = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("wineMIDIClient.%d"), getpid());
84 wineMIDIClient = CoreMIDI_CreateClient( name );
85 if (wineMIDIClient == NULL)
88 ERR("can't create wineMIDIClient\n");
93 MIDIOut_NumDevs = MAX_MIDI_SYNTHS;
94 MIDIOut_NumDevs += numDest;
96 TRACE("MIDIOut_NumDevs %d\n", MIDIOut_NumDevs);
98 destinations = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MIDIOut_NumDevs * sizeof(MIDIDestination));
100 /* initialise MIDI synths */
101 for (i = 0; i < MAX_MIDI_SYNTHS; i++)
103 snprintf(szPname, sizeof(szPname), "CoreAudio MIDI Synth %d", i);
104 MultiByteToWideChar(CP_ACP, 0, szPname, -1, destinations[i].caps.szPname, sizeof(destinations[i].caps.szPname)/sizeof(WCHAR));
106 destinations[i].caps.wTechnology = MOD_SYNTH;
107 destinations[i].caps.wChannelMask = 0xFFFF;
109 destinations[i].caps.wMid = 0x00FF; /* Manufac ID */
110 destinations[i].caps.wPid = 0x0001; /* Product ID */
111 destinations[i].caps.vDriverVersion = 0x0001;
112 destinations[i].caps.dwSupport = MIDICAPS_VOLUME;
113 destinations[i].caps.wVoices = 16;
114 destinations[i].caps.wNotes = 16;
116 /* initialise available destinations */
117 for (i = MAX_MIDI_SYNTHS; i < numDest + MAX_MIDI_SYNTHS; i++)
119 MIDIEndpointRef endpoint = MIDIGetDestination(i - MAX_MIDI_SYNTHS);
121 CoreMIDI_GetObjectName(endpoint, szPname, sizeof(szPname));
122 MultiByteToWideChar(CP_ACP, 0, szPname, -1, destinations[i].caps.szPname, sizeof(destinations[i].caps.szPname)/sizeof(WCHAR));
124 name = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("WineOutputPort.%d.%u"), i, getpid());
125 MIDIOutputPortCreate(wineMIDIClient, name, &destinations[i].port);
128 destinations[i].caps.wTechnology = MOD_MIDIPORT;
129 destinations[i].caps.wChannelMask = 0xFFFF;
131 destinations[i].caps.wMid = 0x00FF; /* Manufac ID */
132 destinations[i].caps.wPid = 0x0001;
133 destinations[i].caps.vDriverVersion = 0x0001;
134 destinations[i].caps.dwSupport = 0;
135 destinations[i].caps.wVoices = 16;
136 destinations[i].caps.wNotes = 16;
141 LONG CoreAudio_MIDIRelease(void)
144 if (wineMIDIClient) MIDIClientDispose(wineMIDIClient); /* MIDIClientDispose will close all ports */
145 HeapFree(GetProcessHeap(), 0, destinations);
150 /**************************************************************************
151 * MIDI_NotifyClient [internal]
153 static DWORD MIDI_NotifyClient(UINT wDevID, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
160 TRACE("wDevID=%d wMsg=%d dwParm1=%04X dwParam2=%04X\n", wDevID, wMsg, dwParam1, dwParam2);
167 dwCallBack = destinations[wDevID].midiDesc.dwCallback;
168 uFlags = destinations[wDevID].wFlags;
169 hDev = destinations[wDevID].midiDesc.hMidi;
170 dwInstance = destinations[wDevID].midiDesc.dwInstance;
181 WARN("Unsupported MSW-MIDI message %u\n", wMsg);
182 return MMSYSERR_ERROR;
185 return DriverCallback(dwCallBack, uFlags, hDev, wMsg, dwInstance, dwParam1, dwParam2) ?
186 MMSYSERR_NOERROR : MMSYSERR_ERROR;
189 static DWORD MIDIOut_Open(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
191 MIDIDestination *dest;
193 TRACE("wDevID=%d lpDesc=%p dwFlags=%08x\n", wDevID, lpDesc, dwFlags);
195 if (lpDesc == NULL) {
196 WARN("Invalid Parameter\n");
197 return MMSYSERR_INVALPARAM;
200 if (wDevID >= MIDIOut_NumDevs) {
201 WARN("bad device ID : %d\n", wDevID);
202 return MMSYSERR_BADDEVICEID;
205 if (destinations[wDevID].midiDesc.hMidi != 0) {
206 WARN("device already open !\n");
207 return MMSYSERR_ALLOCATED;
210 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
211 WARN("bad dwFlags\n");
212 return MMSYSERR_INVALFLAG;
214 dest = &destinations[wDevID];
216 if (dest->caps.wTechnology == MOD_SYNTH)
218 if (!SynthUnit_CreateDefaultSynthUnit(&dest->graph, &dest->synth))
220 ERR("SynthUnit_CreateDefaultSynthUnit dest=%p failed\n", dest);
221 return MMSYSERR_ERROR;
224 if (!SynthUnit_Initialize(dest->synth, dest->graph))
226 ERR("SynthUnit_Initialise dest=%p failed\n", dest);
227 return MMSYSERR_ERROR;
232 FIXME("MOD_MIDIPORT\n");
234 dest->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
235 dest->midiDesc = *lpDesc;
237 return MIDI_NotifyClient(wDevID, MOM_OPEN, 0L, 0L);
240 static DWORD MIDIOut_Close(WORD wDevID)
242 DWORD ret = MMSYSERR_NOERROR;
244 TRACE("wDevID=%d\n", wDevID);
246 if (wDevID >= MIDIOut_NumDevs) {
247 WARN("bad device ID : %d\n", wDevID);
248 return MMSYSERR_BADDEVICEID;
251 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
252 SynthUnit_Close(destinations[wDevID].graph);
254 FIXME("MOD_MIDIPORT\n");
256 destinations[wDevID].graph = 0;
257 destinations[wDevID].synth = 0;
259 if (MIDI_NotifyClient(wDevID, MOM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
260 WARN("can't notify client !\n");
261 ret = MMSYSERR_INVALPARAM;
263 destinations[wDevID].midiDesc.hMidi = 0;
268 static DWORD MIDIOut_Data(WORD wDevID, DWORD dwParam)
270 WORD evt = LOBYTE(LOWORD(dwParam));
271 WORD d1 = HIBYTE(LOWORD(dwParam));
272 WORD d2 = LOBYTE(HIWORD(dwParam));
273 UInt8 chn = (evt & 0x0F);
274 OSStatus err = noErr;
276 TRACE("wDevID=%d dwParam=%08X\n", wDevID, dwParam);
278 if (wDevID >= MIDIOut_NumDevs) {
279 WARN("bad device ID : %d\n", wDevID);
280 return MMSYSERR_BADDEVICEID;
283 TRACE("evt=%08x d1=%04x d2=%04x (evt & 0xF0)=%04x chn=%d\n", evt, d1, d2, (evt & 0xF0), chn);
285 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
287 err = MusicDeviceMIDIEvent(destinations[wDevID].synth, (evt & 0xF0) | chn, d1, d2, 0);
290 ERR("MusicDeviceMIDIEvent(%p, %04x, %04x, %04x, %d) return %c%c%c%c\n", destinations[wDevID].synth, (evt & 0xF0) | chn, d1, d2, 0, (char) (err >> 24), (char) (err >> 16), (char) (err >> 8), (char) err);
291 return MMSYSERR_ERROR;
294 else FIXME("MOD_MIDIPORT\n");
296 return MMSYSERR_NOERROR;
299 /**************************************************************************
300 * MIDIOut_Prepare [internal]
302 static DWORD MIDIOut_Prepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
304 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
306 if (wDevID >= MIDIOut_NumDevs) {
307 WARN("bad device ID : %d\n", wDevID);
308 return MMSYSERR_BADDEVICEID;
311 /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
312 * asks to prepare MIDIHDR which dwFlags != 0.
313 * So at least check for the inqueue flag
315 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
316 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0 ||
317 lpMidiHdr->dwBufferLength >= 0x10000ul) {
318 WARN("%p %p %08x %lu/%d\n", lpMidiHdr, lpMidiHdr->lpData,
319 lpMidiHdr->dwFlags, sizeof(MIDIHDR), dwSize);
320 return MMSYSERR_INVALPARAM;
323 lpMidiHdr->lpNext = 0;
324 lpMidiHdr->dwFlags |= MHDR_PREPARED;
325 lpMidiHdr->dwFlags &= ~MHDR_DONE;
326 return MMSYSERR_NOERROR;
329 /**************************************************************************
330 * MIDIOut_Unprepare [internal]
332 static DWORD MIDIOut_Unprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
334 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
336 if (wDevID >= MIDIOut_NumDevs) {
337 WARN("bad device ID : %d\n", wDevID);
338 return MMSYSERR_BADDEVICEID;
340 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0)
341 return MMSYSERR_INVALPARAM;
342 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
343 return MIDIERR_STILLPLAYING;
345 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
347 return MMSYSERR_NOERROR;
350 static DWORD MIDIOut_GetDevCaps(WORD wDevID, LPMIDIOUTCAPSW lpCaps, DWORD dwSize)
352 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID, lpCaps, dwSize);
354 if (lpCaps == NULL) {
355 WARN("Invalid Parameter\n");
356 return MMSYSERR_INVALPARAM;
359 if (wDevID >= MIDIOut_NumDevs) {
360 WARN("bad device ID : %d\n", wDevID);
361 return MMSYSERR_BADDEVICEID;
363 memcpy(lpCaps, &destinations[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
364 return MMSYSERR_NOERROR;
367 static DWORD MIDIOut_GetNumDevs(void)
370 return MIDIOut_NumDevs;
373 /**************************************************************************
376 DWORD WINAPI CoreAudio_modMessage(UINT wDevID, UINT wMsg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2)
378 TRACE("%d %08x %08x %08x %08x\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
387 return MIDIOut_Open(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
389 return MIDIOut_Close(wDevID);
391 return MIDIOut_Data(wDevID, dwParam1);
393 TRACE("Unsupported message (08%x)\n", wMsg);
394 return MMSYSERR_NOTSUPPORTED;
396 return MIDIOut_Prepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
398 return MIDIOut_Unprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
399 case MODM_GETDEVCAPS:
400 return MIDIOut_GetDevCaps(wDevID, (LPMIDIOUTCAPSW) dwParam1, dwParam2);
401 case MODM_GETNUMDEVS:
402 return MIDIOut_GetNumDevs();
407 TRACE("Unsupported message (08%x)\n", wMsg);
409 return MMSYSERR_NOTSUPPORTED;
414 DWORD WINAPI CoreAudio_modMessage(UINT wDevID, UINT wMsg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2)
416 TRACE("%08x, %08x, %08x, %08x, %08x\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
417 return MMSYSERR_NOTENABLED;