2 * Sample MIDI Wine Driver for Mac OS X (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 split 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;
55 static DWORD MIDIIn_NumDevs = 0;
57 typedef struct tagMIDIDestination {
58 /* graph and synth are only used for MIDI Synth */
65 MIDIOPENDESC midiDesc;
69 typedef struct tagMIDISource {
70 MIDIEndpointRef source;
73 int state; /* 0 is no recording started, 1 in recording, bit 2 set if in sys exclusive recording */
75 MIDIOPENDESC midiDesc;
81 static CRITICAL_SECTION midiInLock; /* Critical section for MIDI In */
82 static CFStringRef MIDIInThreadPortName = NULL;
84 static DWORD WINAPI MIDIIn_MessageThread(LPVOID p);
86 static MIDIPortRef MIDIInPort = NULL;
87 static MIDIPortRef MIDIOutPort = NULL;
89 #define MAX_MIDI_SYNTHS 1
91 MIDIDestination *destinations;
94 extern int SynthUnit_CreateDefaultSynthUnit(AUGraph *graph, AudioUnit *synth);
95 extern int SynthUnit_Initialize(AudioUnit synth, AUGraph graph);
96 extern int SynthUnit_Close(AUGraph graph);
99 LONG CoreAudio_MIDIInit(void)
102 CHAR szPname[MAXPNAMELEN] = {0};
104 int numDest = MIDIGetNumberOfDestinations();
105 CFStringRef name = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("wineMIDIClient.%d"), getpid());
107 wineMIDIClient = CoreMIDI_CreateClient( name );
108 if (wineMIDIClient == NULL)
111 ERR("can't create wineMIDIClient\n");
116 MIDIOut_NumDevs = MAX_MIDI_SYNTHS;
117 MIDIOut_NumDevs += numDest;
119 MIDIIn_NumDevs = MIDIGetNumberOfSources();
121 TRACE("MIDIOut_NumDevs %d MIDIIn_NumDevs %d\n", MIDIOut_NumDevs, MIDIIn_NumDevs);
123 destinations = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MIDIOut_NumDevs * sizeof(MIDIDestination));
124 sources = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MIDIIn_NumDevs * sizeof(MIDISource));
126 if (MIDIIn_NumDevs > 0)
128 InitializeCriticalSection(&midiInLock);
129 MIDIInThreadPortName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("MIDIInThreadPortName.%u"), getpid());
130 CreateThread(NULL, 0, MIDIIn_MessageThread, NULL, 0, NULL);
132 name = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("WineInputPort.%u"), getpid());
133 MIDIInputPortCreate(wineMIDIClient, name, MIDIIn_ReadProc, NULL, &MIDIInPort);
138 name = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("WineOutputPort.%u"), getpid());
139 MIDIOutputPortCreate(wineMIDIClient, name, &MIDIOutPort);
143 /* initialize sources */
144 for (i = 0; i < MIDIIn_NumDevs; i++)
146 sources[i].wDevID = i;
147 sources[i].source = MIDIGetSource(i);
149 CoreMIDI_GetObjectName(sources[i].source, szPname, sizeof(szPname));
150 MultiByteToWideChar(CP_ACP, 0, szPname, -1, sources[i].caps.szPname, sizeof(sources[i].caps.szPname)/sizeof(WCHAR));
152 MIDIPortConnectSource(MIDIInPort, sources[i].source, &sources[i].wDevID);
154 sources[i].state = 0;
156 sources[i].caps.wMid = 0x00FF; /* Manufac ID */
157 sources[i].caps.wPid = 0x0001; /* Product ID */
158 sources[i].caps.vDriverVersion = 0x0001;
159 sources[i].caps.dwSupport = 0;
162 /* initialise MIDI synths */
163 for (i = 0; i < MAX_MIDI_SYNTHS; i++)
165 snprintf(szPname, sizeof(szPname), "CoreAudio MIDI Synth %d", i);
166 MultiByteToWideChar(CP_ACP, 0, szPname, -1, destinations[i].caps.szPname, sizeof(destinations[i].caps.szPname)/sizeof(WCHAR));
168 destinations[i].caps.wTechnology = MOD_SYNTH;
169 destinations[i].caps.wChannelMask = 0xFFFF;
171 destinations[i].caps.wMid = 0x00FF; /* Manufac ID */
172 destinations[i].caps.wPid = 0x0001; /* Product ID */
173 destinations[i].caps.vDriverVersion = 0x0001;
174 destinations[i].caps.dwSupport = MIDICAPS_VOLUME;
175 destinations[i].caps.wVoices = 16;
176 destinations[i].caps.wNotes = 16;
178 /* initialise available destinations */
179 for (i = MAX_MIDI_SYNTHS; i < numDest + MAX_MIDI_SYNTHS; i++)
181 destinations[i].dest = MIDIGetDestination(i - MAX_MIDI_SYNTHS);
183 CoreMIDI_GetObjectName(destinations[i].dest, szPname, sizeof(szPname));
184 MultiByteToWideChar(CP_ACP, 0, szPname, -1, destinations[i].caps.szPname, sizeof(destinations[i].caps.szPname)/sizeof(WCHAR));
186 destinations[i].caps.wTechnology = MOD_MIDIPORT;
187 destinations[i].caps.wChannelMask = 0xFFFF;
189 destinations[i].caps.wMid = 0x00FF; /* Manufac ID */
190 destinations[i].caps.wPid = 0x0001;
191 destinations[i].caps.vDriverVersion = 0x0001;
192 destinations[i].caps.dwSupport = 0;
193 destinations[i].caps.wVoices = 16;
194 destinations[i].caps.wNotes = 16;
199 LONG CoreAudio_MIDIRelease(void)
202 if (MIDIIn_NumDevs > 0)
204 CFMessagePortRef messagePort;
205 /* Stop CFRunLoop in MIDIIn_MessageThread */
206 messagePort = CFMessagePortCreateRemote(kCFAllocatorDefault, MIDIInThreadPortName);
207 CFMessagePortSendRequest(messagePort, 1, NULL, 0.0, 0.0, NULL, NULL);
208 CFRelease(messagePort);
210 DeleteCriticalSection(&midiInLock);
213 if (wineMIDIClient) MIDIClientDispose(wineMIDIClient); /* MIDIClientDispose will close all ports */
215 HeapFree(GetProcessHeap(), 0, sources);
216 HeapFree(GetProcessHeap(), 0, destinations);
221 /**************************************************************************
222 * MIDI_NotifyClient [internal]
224 static DWORD MIDI_NotifyClient(UINT wDevID, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
231 TRACE("wDevID=%d wMsg=%d dwParm1=%04X dwParam2=%04X\n", wDevID, wMsg, dwParam1, dwParam2);
238 dwCallBack = destinations[wDevID].midiDesc.dwCallback;
239 uFlags = destinations[wDevID].wFlags;
240 hDev = destinations[wDevID].midiDesc.hMidi;
241 dwInstance = destinations[wDevID].midiDesc.dwInstance;
251 dwCallBack = sources[wDevID].midiDesc.dwCallback;
252 uFlags = sources[wDevID].wFlags;
253 hDev = sources[wDevID].midiDesc.hMidi;
254 dwInstance = sources[wDevID].midiDesc.dwInstance;
257 WARN("Unsupported MSW-MIDI message %u\n", wMsg);
258 return MMSYSERR_ERROR;
261 return DriverCallback(dwCallBack, uFlags, hDev, wMsg, dwInstance, dwParam1, dwParam2) ?
262 MMSYSERR_NOERROR : MMSYSERR_ERROR;
265 static DWORD MIDIOut_Open(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
267 MIDIDestination *dest;
269 TRACE("wDevID=%d lpDesc=%p dwFlags=%08x\n", wDevID, lpDesc, dwFlags);
271 if (lpDesc == NULL) {
272 WARN("Invalid Parameter\n");
273 return MMSYSERR_INVALPARAM;
276 if (wDevID >= MIDIOut_NumDevs) {
277 WARN("bad device ID : %d\n", wDevID);
278 return MMSYSERR_BADDEVICEID;
281 if (destinations[wDevID].midiDesc.hMidi != 0) {
282 WARN("device already open !\n");
283 return MMSYSERR_ALLOCATED;
286 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
287 WARN("bad dwFlags\n");
288 return MMSYSERR_INVALFLAG;
290 dest = &destinations[wDevID];
292 if (dest->caps.wTechnology == MOD_SYNTH)
294 if (!SynthUnit_CreateDefaultSynthUnit(&dest->graph, &dest->synth))
296 ERR("SynthUnit_CreateDefaultSynthUnit dest=%p failed\n", dest);
297 return MMSYSERR_ERROR;
300 if (!SynthUnit_Initialize(dest->synth, dest->graph))
302 ERR("SynthUnit_Initialise dest=%p failed\n", dest);
303 return MMSYSERR_ERROR;
306 dest->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
307 dest->midiDesc = *lpDesc;
309 return MIDI_NotifyClient(wDevID, MOM_OPEN, 0L, 0L);
312 static DWORD MIDIOut_Close(WORD wDevID)
314 DWORD ret = MMSYSERR_NOERROR;
316 TRACE("wDevID=%d\n", wDevID);
318 if (wDevID >= MIDIOut_NumDevs) {
319 WARN("bad device ID : %d\n", wDevID);
320 return MMSYSERR_BADDEVICEID;
323 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
324 SynthUnit_Close(destinations[wDevID].graph);
326 destinations[wDevID].graph = 0;
327 destinations[wDevID].synth = 0;
329 if (MIDI_NotifyClient(wDevID, MOM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
330 WARN("can't notify client !\n");
331 ret = MMSYSERR_INVALPARAM;
333 destinations[wDevID].midiDesc.hMidi = 0;
338 static DWORD MIDIOut_Data(WORD wDevID, DWORD dwParam)
340 WORD evt = LOBYTE(LOWORD(dwParam));
341 UInt8 chn = (evt & 0x0F);
343 TRACE("wDevID=%d dwParam=%08X\n", wDevID, dwParam);
345 if (wDevID >= MIDIOut_NumDevs) {
346 WARN("bad device ID : %d\n", wDevID);
347 return MMSYSERR_BADDEVICEID;
350 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
352 WORD d1 = HIBYTE(LOWORD(dwParam));
353 WORD d2 = LOBYTE(HIWORD(dwParam));
354 OSStatus err = noErr;
356 err = MusicDeviceMIDIEvent(destinations[wDevID].synth, (evt & 0xF0) | chn, d1, d2, 0);
359 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);
360 return MMSYSERR_ERROR;
366 buffer[0] = (evt & 0xF0) | chn;
367 buffer[1] = HIBYTE(LOWORD(dwParam));
368 buffer[2] = LOBYTE(HIWORD(dwParam));
370 MIDIOut_Send(MIDIOutPort, destinations[wDevID].dest, buffer, 3);
373 return MMSYSERR_NOERROR;
376 static DWORD MIDIOut_LongData(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
379 OSStatus err = noErr;
381 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
383 /* Note: MS doc does not say much about the dwBytesRecorded member of the MIDIHDR structure
384 * but it seems to be used only for midi input.
385 * Taking a look at the WAVEHDR structure (which is quite similar) confirms this assumption.
388 if (wDevID >= MIDIOut_NumDevs) {
389 WARN("bad device ID : %d\n", wDevID);
390 return MMSYSERR_BADDEVICEID;
393 if (lpMidiHdr == NULL) {
394 WARN("Invalid Parameter\n");
395 return MMSYSERR_INVALPARAM;
398 lpData = (LPBYTE) lpMidiHdr->lpData;
401 return MIDIERR_UNPREPARED;
402 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
403 return MIDIERR_UNPREPARED;
404 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
405 return MIDIERR_STILLPLAYING;
406 lpMidiHdr->dwFlags &= ~MHDR_DONE;
407 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
409 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
410 * data, or can it also contain raw MIDI data, to be split up and sent to
412 * If the latest is true, then the following WARNing will fire up
414 if (lpData[0] != 0xF0 || lpData[lpMidiHdr->dwBufferLength - 1] != 0xF7) {
415 WARN("Alledged system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
418 TRACE("dwBufferLength=%u !\n", lpMidiHdr->dwBufferLength);
419 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
420 lpData[0], lpData[1], lpData[2], lpData[lpMidiHdr->dwBufferLength-3],
421 lpData[lpMidiHdr->dwBufferLength-2], lpData[lpMidiHdr->dwBufferLength-1]);
424 if (lpData[0] != 0xF0) {
425 /* System Exclusive */
426 ERR("Add missing 0xF0 marker at the beginning of system exclusive byte stream\n");
428 if (lpData[lpMidiHdr->dwBufferLength - 1] != 0xF7) {
429 /* Send end of System Exclusive */
430 ERR("Add missing 0xF7 marker at the end of system exclusive byte stream\n");
432 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH) /* FIXME */
434 err = MusicDeviceSysEx(destinations[wDevID].synth, (const UInt8 *) lpData, lpMidiHdr->dwBufferLength);
437 ERR("MusicDeviceSysEx(%p, %p, %d) return %c%c%c%c\n", destinations[wDevID].synth, lpData, lpMidiHdr->dwBufferLength, (char) (err >> 24), (char) (err >> 16), (char) (err >> 8), (char) err);
438 return MMSYSERR_ERROR;
443 FIXME("MOD_MIDIPORT\n");
446 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
447 lpMidiHdr->dwFlags |= MHDR_DONE;
448 if (MIDI_NotifyClient(wDevID, MOM_DONE, (DWORD)lpMidiHdr, 0L) != MMSYSERR_NOERROR) {
449 WARN("can't notify client !\n");
450 return MMSYSERR_INVALPARAM;
452 return MMSYSERR_NOERROR;
455 /**************************************************************************
456 * MIDIOut_Prepare [internal]
458 static DWORD MIDIOut_Prepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
460 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
462 if (wDevID >= MIDIOut_NumDevs) {
463 WARN("bad device ID : %d\n", wDevID);
464 return MMSYSERR_BADDEVICEID;
467 /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
468 * asks to prepare MIDIHDR which dwFlags != 0.
469 * So at least check for the inqueue flag
471 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
472 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0 ||
473 lpMidiHdr->dwBufferLength >= 0x10000ul) {
474 WARN("%p %p %08x %lu/%d\n", lpMidiHdr, lpMidiHdr->lpData,
475 lpMidiHdr->dwFlags, sizeof(MIDIHDR), dwSize);
476 return MMSYSERR_INVALPARAM;
479 lpMidiHdr->lpNext = 0;
480 lpMidiHdr->dwFlags |= MHDR_PREPARED;
481 lpMidiHdr->dwFlags &= ~MHDR_DONE;
482 return MMSYSERR_NOERROR;
485 /**************************************************************************
486 * MIDIOut_Unprepare [internal]
488 static DWORD MIDIOut_Unprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
490 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
492 if (wDevID >= MIDIOut_NumDevs) {
493 WARN("bad device ID : %d\n", wDevID);
494 return MMSYSERR_BADDEVICEID;
496 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0)
497 return MMSYSERR_INVALPARAM;
498 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
499 return MIDIERR_STILLPLAYING;
501 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
503 return MMSYSERR_NOERROR;
506 static DWORD MIDIOut_GetDevCaps(WORD wDevID, LPMIDIOUTCAPSW lpCaps, DWORD dwSize)
508 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID, lpCaps, dwSize);
510 if (lpCaps == NULL) {
511 WARN("Invalid Parameter\n");
512 return MMSYSERR_INVALPARAM;
515 if (wDevID >= MIDIOut_NumDevs) {
516 WARN("bad device ID : %d\n", wDevID);
517 return MMSYSERR_BADDEVICEID;
519 memcpy(lpCaps, &destinations[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
520 return MMSYSERR_NOERROR;
523 static DWORD MIDIOut_GetNumDevs(void)
526 return MIDIOut_NumDevs;
529 static DWORD MIDIOut_GetVolume(WORD wDevID, DWORD *lpdwVolume)
531 TRACE("%d\n", wDevID);
533 if (wDevID >= MIDIOut_NumDevs) {
534 WARN("bad device ID : %d\n", wDevID);
535 return MMSYSERR_BADDEVICEID;
537 if (lpdwVolume == NULL) {
538 WARN("Invalid Parameter\n");
539 return MMSYSERR_INVALPARAM;
542 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
546 AudioUnit_GetVolume(destinations[wDevID].synth, &left, &right);
548 *lpdwVolume = ((WORD) left * 0xFFFFl) + (((WORD) right * 0xFFFFl) << 16);
550 return MMSYSERR_NOERROR;
553 return MMSYSERR_NOTSUPPORTED;
556 static DWORD MIDIOut_SetVolume(WORD wDevID, DWORD dwVolume)
558 TRACE("%d\n", wDevID);
560 if (wDevID >= MIDIOut_NumDevs) {
561 WARN("bad device ID : %d\n", wDevID);
562 return MMSYSERR_BADDEVICEID;
564 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
569 left = LOWORD(dwVolume) / 65535.0f;
570 right = HIWORD(dwVolume) / 65535.0f;
571 AudioUnit_SetVolume(destinations[wDevID].synth, left, right);
573 return MMSYSERR_NOERROR;
576 return MMSYSERR_NOTSUPPORTED;
579 static DWORD MIDIOut_Reset(WORD wDevID)
583 TRACE("%d\n", wDevID);
585 if (wDevID >= MIDIOut_NumDevs) {
586 WARN("bad device ID : %d\n", wDevID);
587 return MMSYSERR_BADDEVICEID;
589 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
591 for (chn = 0; chn < 16; chn++) {
592 /* turn off every note */
593 MusicDeviceMIDIEvent(destinations[wDevID].synth, 0xB0 | chn, 0x7B, 0, 0);
594 /* remove sustain on channel */
595 MusicDeviceMIDIEvent(destinations[wDevID].synth, 0xB0 | chn, 0x40, 0, 0);
598 else FIXME("MOD_MIDIPORT\n");
600 /* FIXME: the LongData buffers must also be returned to the app */
601 return MMSYSERR_NOERROR;
604 static DWORD MIDIIn_Open(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
606 TRACE("wDevID=%d lpDesc=%p dwFlags=%08x\n", wDevID, lpDesc, dwFlags);
608 if (lpDesc == NULL) {
609 WARN("Invalid Parameter\n");
610 return MMSYSERR_INVALPARAM;
612 if (wDevID >= MIDIIn_NumDevs) {
613 WARN("bad device ID : %d\n", wDevID);
614 return MMSYSERR_BADDEVICEID;
616 if (sources[wDevID].midiDesc.hMidi != 0) {
617 WARN("device already open !\n");
618 return MMSYSERR_ALLOCATED;
620 if ((dwFlags & MIDI_IO_STATUS) != 0) {
621 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
622 dwFlags &= ~MIDI_IO_STATUS;
624 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
625 FIXME("Bad dwFlags\n");
626 return MMSYSERR_INVALFLAG;
629 sources[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
630 sources[wDevID].lpQueueHdr = NULL;
631 sources[wDevID].midiDesc = *lpDesc;
632 sources[wDevID].startTime = 0;
633 sources[wDevID].state = 0;
635 return MIDI_NotifyClient(wDevID, MIM_OPEN, 0L, 0L);
638 static DWORD MIDIIn_Close(WORD wDevID)
640 DWORD ret = MMSYSERR_NOERROR;
642 TRACE("wDevID=%d\n", wDevID);
644 if (wDevID >= MIDIIn_NumDevs) {
645 WARN("bad device ID : %d\n", wDevID);
646 return MMSYSERR_BADDEVICEID;
649 if (sources[wDevID].midiDesc.hMidi == 0) {
650 WARN("device not opened !\n");
651 return MMSYSERR_ERROR;
653 if (sources[wDevID].lpQueueHdr != 0) {
654 return MIDIERR_STILLPLAYING;
657 ret = MIDI_NotifyClient(wDevID, MIM_CLOSE, 0L, 0L);
658 sources[wDevID].midiDesc.hMidi = 0;
662 static DWORD MIDIIn_AddBuffer(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
664 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
666 if (wDevID >= MIDIIn_NumDevs) {
667 WARN("bad device ID : %d\n", wDevID);
668 return MMSYSERR_BADDEVICEID;
670 if (lpMidiHdr == NULL) {
671 WARN("Invalid Parameter\n");
672 return MMSYSERR_INVALPARAM;
674 if (sizeof(MIDIHDR) > dwSize) {
675 WARN("Invalid Parameter\n");
676 return MMSYSERR_INVALPARAM;
678 if (lpMidiHdr->dwBufferLength == 0) {
679 WARN("Invalid Parameter\n");
680 return MMSYSERR_INVALPARAM;
682 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) {
683 WARN("Still playing\n");
684 return MIDIERR_STILLPLAYING;
686 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) {
687 WARN("Unprepared\n");
688 return MIDIERR_UNPREPARED;
691 EnterCriticalSection(&midiInLock);
692 lpMidiHdr->dwFlags &= ~WHDR_DONE;
693 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
694 lpMidiHdr->dwBytesRecorded = 0;
695 lpMidiHdr->lpNext = 0;
696 if (sources[wDevID].lpQueueHdr == 0) {
697 sources[wDevID].lpQueueHdr = lpMidiHdr;
700 for (ptr = sources[wDevID].lpQueueHdr;
702 ptr = (LPMIDIHDR)ptr->lpNext);
703 ptr->lpNext = (struct midihdr_tag*)lpMidiHdr;
705 LeaveCriticalSection(&midiInLock);
707 return MMSYSERR_NOERROR;
710 static DWORD MIDIIn_Prepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
712 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
714 if (wDevID >= MIDIIn_NumDevs) {
715 WARN("bad device ID : %d\n", wDevID);
716 return MMSYSERR_BADDEVICEID;
718 /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
719 * asks to prepare MIDIHDR which dwFlags != 0.
720 * So at least check for the inqueue flag
722 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
723 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0 ||
724 lpMidiHdr->dwBufferLength >= 0x10000ul) {
725 WARN("Invalid parameter %p %p %08x %d\n", lpMidiHdr, lpMidiHdr->lpData,
726 lpMidiHdr->dwFlags, dwSize);
727 return MMSYSERR_INVALPARAM;
730 lpMidiHdr->lpNext = 0;
731 lpMidiHdr->dwFlags |= MHDR_PREPARED;
732 lpMidiHdr->dwFlags &= ~MHDR_DONE;
733 return MMSYSERR_NOERROR;
736 static DWORD MIDIIn_Unprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
738 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
739 if (wDevID >= MIDIIn_NumDevs) {
740 WARN("bad device ID : %d\n", wDevID);
741 return MMSYSERR_BADDEVICEID;
743 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0) {
744 WARN("Invalid Parameter\n");
745 return MMSYSERR_INVALPARAM;
747 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) {
748 WARN("Still playing\n");
749 return MIDIERR_STILLPLAYING;
752 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
753 return MMSYSERR_NOERROR;
756 static DWORD MIDIIn_GetDevCaps(WORD wDevID, LPMIDIINCAPSW lpCaps, DWORD dwSize)
758 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID, lpCaps, dwSize);
760 if (lpCaps == NULL) {
761 WARN("Invalid Parameter\n");
762 return MMSYSERR_INVALPARAM;
765 if (wDevID >= MIDIIn_NumDevs) {
766 WARN("bad device ID : %d\n", wDevID);
767 return MMSYSERR_BADDEVICEID;
769 memcpy(lpCaps, &sources[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
770 return MMSYSERR_NOERROR;
773 static DWORD MIDIIn_GetNumDevs(void)
776 return MIDIIn_NumDevs;
779 static DWORD MIDIIn_Start(WORD wDevID)
781 TRACE("%d\n", wDevID);
783 if (wDevID >= MIDIIn_NumDevs) {
784 WARN("bad device ID : %d\n", wDevID);
785 return MMSYSERR_BADDEVICEID;
787 sources[wDevID].state = 1;
788 sources[wDevID].startTime = GetTickCount();
789 return MMSYSERR_NOERROR;
792 static DWORD MIDIIn_Stop(WORD wDevID)
794 TRACE("%d\n", wDevID);
795 if (wDevID >= MIDIIn_NumDevs) {
796 WARN("bad device ID : %d\n", wDevID);
797 return MMSYSERR_BADDEVICEID;
799 sources[wDevID].state = 0;
800 return MMSYSERR_NOERROR;
803 static DWORD MIDIIn_Reset(WORD wDevID)
805 DWORD dwTime = GetTickCount();
807 TRACE("%d\n", wDevID);
808 if (wDevID >= MIDIIn_NumDevs) {
809 WARN("bad device ID : %d\n", wDevID);
810 return MMSYSERR_BADDEVICEID;
813 EnterCriticalSection(&midiInLock);
814 while (sources[wDevID].lpQueueHdr) {
815 sources[wDevID].lpQueueHdr->dwFlags &= ~MHDR_INQUEUE;
816 sources[wDevID].lpQueueHdr->dwFlags |= MHDR_DONE;
817 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
818 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA, (DWORD)sources[wDevID].lpQueueHdr, dwTime) != MMSYSERR_NOERROR) {
819 WARN("Couldn't notify client\n");
821 sources[wDevID].lpQueueHdr = (LPMIDIHDR)sources[wDevID].lpQueueHdr->lpNext;
823 LeaveCriticalSection(&midiInLock);
825 return MMSYSERR_NOERROR;
829 * MIDI In Mach message handling
833 * Call from CoreMIDI IO threaded callback,
834 * we can't call Wine debug channels, critical section or anything using NtCurrentTeb here.
836 void MIDIIn_SendMessage(MIDIMessage msg)
840 CFMessagePortRef messagePort;
841 messagePort = CFMessagePortCreateRemote(kCFAllocatorDefault, MIDIInThreadPortName);
843 data = CFDataCreate(kCFAllocatorDefault, (UInt8 *) &msg, sizeof(msg));
846 CFMessagePortSendRequest(messagePort, 0, data, 0.0, 0.0, NULL, NULL);
848 CFRelease(messagePort);
852 static CFDataRef MIDIIn_MessageHandler(CFMessagePortRef local, SInt32 msgid, CFDataRef data, void *info)
854 MIDIMessage *msg = NULL;
856 MIDISource *src = NULL;
864 msg = (MIDIMessage *) CFDataGetBytePtr(data);
865 TRACE("devID=%d\n", msg->devID);
866 for (i = 0; i < msg->length; ++i) {
867 TRACE("%02X ", msg->data[i]);
870 src = &sources[msg->devID];
873 TRACE("input not started, thrown away\n");
876 /* FIXME skipping SysEx */
877 if (msg->data[0] == 0xF0)
879 FIXME("Starting System Exclusive\n");
884 for (i = 0; i < msg->length; ++i)
886 if (msg->data[i] == 0xF7)
888 FIXME("Ending System Exclusive\n");
894 EnterCriticalSection(&midiInLock);
895 currentTime = GetTickCount() - src->startTime;
897 while (pos < msg->length)
900 switch (msg->data[pos] & 0xF0)
903 sendData = (msg->data[pos] << 0);
909 sendData = (msg->data[pos + 1] << 8) | (msg->data[pos] << 0);
913 sendData = (msg->data[pos + 2] << 16) |
914 (msg->data[pos + 1] << 8) |
915 (msg->data[pos] << 0);
919 MIDI_NotifyClient(msg->devID, MIM_DATA, sendData, currentTime);
921 LeaveCriticalSection(&midiInLock);
924 CFRunLoopStop(CFRunLoopGetCurrent());
931 static DWORD WINAPI MIDIIn_MessageThread(LPVOID p)
933 CFMessagePortRef local;
934 CFRunLoopSourceRef source;
937 local = CFMessagePortCreateLocal(kCFAllocatorDefault, MIDIInThreadPortName, &MIDIIn_MessageHandler, NULL, &info);
939 source = CFMessagePortCreateRunLoopSource(kCFAllocatorDefault, local, (CFIndex)0);
940 CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
944 CFRunLoopSourceInvalidate(source);
947 CFRelease(MIDIInThreadPortName);
948 MIDIInThreadPortName = NULL;
953 /**************************************************************************
956 DWORD WINAPI CoreAudio_modMessage(UINT wDevID, UINT wMsg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2)
958 TRACE("%d %08x %08x %08x %08x\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
967 return MIDIOut_Open(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
969 return MIDIOut_Close(wDevID);
971 return MIDIOut_Data(wDevID, dwParam1);
973 return MIDIOut_LongData(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
975 return MIDIOut_Prepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
977 return MIDIOut_Unprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
978 case MODM_GETDEVCAPS:
979 return MIDIOut_GetDevCaps(wDevID, (LPMIDIOUTCAPSW) dwParam1, dwParam2);
980 case MODM_GETNUMDEVS:
981 return MIDIOut_GetNumDevs();
983 return MIDIOut_GetVolume(wDevID, (DWORD*)dwParam1);
985 return MIDIOut_SetVolume(wDevID, dwParam1);
987 return MIDIOut_Reset(wDevID);
989 TRACE("Unsupported message (08%x)\n", wMsg);
991 return MMSYSERR_NOTSUPPORTED;
994 /**************************************************************************
997 DWORD WINAPI CoreAudio_midMessage(UINT wDevID, UINT wMsg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2)
999 TRACE("%d %08x %08x %08x %08x\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
1007 return MIDIIn_Open(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1009 return MIDIIn_Close(wDevID);
1010 case MIDM_ADDBUFFER:
1011 return MIDIIn_AddBuffer(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1013 return MIDIIn_Prepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1014 case MIDM_UNPREPARE:
1015 return MIDIIn_Unprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1016 case MIDM_GETDEVCAPS:
1017 return MIDIIn_GetDevCaps(wDevID, (LPMIDIINCAPSW) dwParam1, dwParam2);
1018 case MIDM_GETNUMDEVS:
1019 return MIDIIn_GetNumDevs();
1021 return MIDIIn_Start(wDevID);
1023 return MIDIIn_Stop(wDevID);
1025 return MIDIIn_Reset(wDevID);
1027 TRACE("Unsupported message\n");
1029 return MMSYSERR_NOTSUPPORTED;
1033 DWORD WINAPI CoreAudio_modMessage(UINT wDevID, UINT wMsg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2)
1035 TRACE("%08x, %08x, %08x, %08x, %08x\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
1036 return MMSYSERR_NOTENABLED;
1039 DWORD WINAPI CoreAudio_midMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1040 DWORD dwParam1, DWORD dwParam2)
1042 TRACE("%08x, %08x, %08x, %08x, %08x\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
1043 return MMSYSERR_NOTENABLED;