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"
43 #include "coreaudio.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(midi);
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 = 0;
194 destinations[i].caps.wNotes = 0;
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 void MIDI_NotifyClient(UINT wDevID, WORD wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
231 TRACE("wDevID=%d wMsg=%d dwParm1=%04lX dwParam2=%04lX\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 ERR("Unsupported MSW-MIDI message %u\n", wMsg);
261 DriverCallback(dwCallBack, uFlags, hDev, wMsg, dwInstance, dwParam1, dwParam2);
264 static DWORD MIDIOut_Open(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
266 MIDIDestination *dest;
268 TRACE("wDevID=%d lpDesc=%p dwFlags=%08x\n", wDevID, lpDesc, dwFlags);
270 if (lpDesc == NULL) {
271 WARN("Invalid Parameter\n");
272 return MMSYSERR_INVALPARAM;
275 if (wDevID >= MIDIOut_NumDevs) {
276 WARN("bad device ID : %d\n", wDevID);
277 return MMSYSERR_BADDEVICEID;
280 if (destinations[wDevID].midiDesc.hMidi != 0) {
281 WARN("device already open !\n");
282 return MMSYSERR_ALLOCATED;
285 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
286 WARN("bad dwFlags\n");
287 return MMSYSERR_INVALFLAG;
289 dest = &destinations[wDevID];
291 if (dest->caps.wTechnology == MOD_SYNTH)
293 if (!SynthUnit_CreateDefaultSynthUnit(&dest->graph, &dest->synth))
295 ERR("SynthUnit_CreateDefaultSynthUnit dest=%p failed\n", dest);
296 return MMSYSERR_ERROR;
299 if (!SynthUnit_Initialize(dest->synth, dest->graph))
301 ERR("SynthUnit_Initialise dest=%p failed\n", dest);
302 return MMSYSERR_ERROR;
305 dest->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
306 dest->midiDesc = *lpDesc;
308 MIDI_NotifyClient(wDevID, MOM_OPEN, 0L, 0L);
309 return MMSYSERR_NOERROR;
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 MIDI_NotifyClient(wDevID, MOM_CLOSE, 0L, 0L);
330 destinations[wDevID].midiDesc.hMidi = 0;
335 static DWORD MIDIOut_Data(WORD wDevID, DWORD dwParam)
337 WORD evt = LOBYTE(LOWORD(dwParam));
338 UInt8 chn = (evt & 0x0F);
340 TRACE("wDevID=%d dwParam=%08X\n", wDevID, dwParam);
342 if (wDevID >= MIDIOut_NumDevs) {
343 WARN("bad device ID : %d\n", wDevID);
344 return MMSYSERR_BADDEVICEID;
347 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
349 WORD d1 = HIBYTE(LOWORD(dwParam));
350 WORD d2 = LOBYTE(HIWORD(dwParam));
351 OSStatus err = noErr;
353 err = MusicDeviceMIDIEvent(destinations[wDevID].synth, (evt & 0xF0) | chn, d1, d2, 0);
356 ERR("MusicDeviceMIDIEvent(%p, %04x, %04x, %04x, %d) return %s\n", destinations[wDevID].synth, (evt & 0xF0) | chn, d1, d2, 0, wine_dbgstr_fourcc(err));
357 return MMSYSERR_ERROR;
363 buffer[0] = (evt & 0xF0) | chn;
364 buffer[1] = HIBYTE(LOWORD(dwParam));
365 buffer[2] = LOBYTE(HIWORD(dwParam));
367 MIDIOut_Send(MIDIOutPort, destinations[wDevID].dest, buffer, 3);
370 return MMSYSERR_NOERROR;
373 static DWORD MIDIOut_LongData(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
376 OSStatus err = noErr;
378 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
380 /* Note: MS doc does not say much about the dwBytesRecorded member of the MIDIHDR structure
381 * but it seems to be used only for midi input.
382 * Taking a look at the WAVEHDR structure (which is quite similar) confirms this assumption.
385 if (wDevID >= MIDIOut_NumDevs) {
386 WARN("bad device ID : %d\n", wDevID);
387 return MMSYSERR_BADDEVICEID;
390 if (lpMidiHdr == NULL) {
391 WARN("Invalid Parameter\n");
392 return MMSYSERR_INVALPARAM;
395 lpData = (LPBYTE) lpMidiHdr->lpData;
398 return MIDIERR_UNPREPARED;
399 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
400 return MIDIERR_UNPREPARED;
401 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
402 return MIDIERR_STILLPLAYING;
403 lpMidiHdr->dwFlags &= ~MHDR_DONE;
404 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
406 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
407 * data, or can it also contain raw MIDI data, to be split up and sent to
409 * If the latest is true, then the following WARNing will fire up
411 if (lpData[0] != 0xF0 || lpData[lpMidiHdr->dwBufferLength - 1] != 0xF7) {
412 WARN("Alledged system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
415 TRACE("dwBufferLength=%u !\n", lpMidiHdr->dwBufferLength);
416 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
417 lpData[0], lpData[1], lpData[2], lpData[lpMidiHdr->dwBufferLength-3],
418 lpData[lpMidiHdr->dwBufferLength-2], lpData[lpMidiHdr->dwBufferLength-1]);
421 if (lpData[0] != 0xF0) {
422 /* System Exclusive */
423 ERR("Add missing 0xF0 marker at the beginning of system exclusive byte stream\n");
425 if (lpData[lpMidiHdr->dwBufferLength - 1] != 0xF7) {
426 /* Send end of System Exclusive */
427 ERR("Add missing 0xF7 marker at the end of system exclusive byte stream\n");
429 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH) /* FIXME */
431 err = MusicDeviceSysEx(destinations[wDevID].synth, (const UInt8 *) lpData, lpMidiHdr->dwBufferLength);
434 ERR("MusicDeviceSysEx(%p, %p, %d) return %s\n", destinations[wDevID].synth, lpData, lpMidiHdr->dwBufferLength, wine_dbgstr_fourcc(err));
435 return MMSYSERR_ERROR;
440 FIXME("MOD_MIDIPORT\n");
443 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
444 lpMidiHdr->dwFlags |= MHDR_DONE;
445 MIDI_NotifyClient(wDevID, MOM_DONE, (DWORD_PTR)lpMidiHdr, 0L);
446 return MMSYSERR_NOERROR;
449 /**************************************************************************
450 * MIDIOut_Prepare [internal]
452 static DWORD MIDIOut_Prepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
454 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
456 if (wDevID >= MIDIOut_NumDevs) {
457 WARN("bad device ID : %d\n", wDevID);
458 return MMSYSERR_BADDEVICEID;
461 /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
462 * asks to prepare MIDIHDR which dwFlags != 0.
463 * So at least check for the inqueue flag
465 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0 ||
466 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0) {
467 WARN("%p %p %08x %lu/%d\n", lpMidiHdr, lpMidiHdr->lpData,
468 lpMidiHdr->dwFlags, offsetof(MIDIHDR,dwOffset), dwSize);
469 return MMSYSERR_INVALPARAM;
472 lpMidiHdr->lpNext = 0;
473 lpMidiHdr->dwFlags |= MHDR_PREPARED;
474 lpMidiHdr->dwFlags &= ~MHDR_DONE;
475 return MMSYSERR_NOERROR;
478 /**************************************************************************
479 * MIDIOut_Unprepare [internal]
481 static DWORD MIDIOut_Unprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
483 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
485 if (wDevID >= MIDIOut_NumDevs) {
486 WARN("bad device ID : %d\n", wDevID);
487 return MMSYSERR_BADDEVICEID;
489 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0)
490 return MMSYSERR_INVALPARAM;
491 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
492 return MIDIERR_STILLPLAYING;
494 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
496 return MMSYSERR_NOERROR;
499 static DWORD MIDIOut_GetDevCaps(WORD wDevID, LPMIDIOUTCAPSW lpCaps, DWORD dwSize)
501 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID, lpCaps, dwSize);
503 if (lpCaps == NULL) {
504 WARN("Invalid Parameter\n");
505 return MMSYSERR_INVALPARAM;
508 if (wDevID >= MIDIOut_NumDevs) {
509 WARN("bad device ID : %d\n", wDevID);
510 return MMSYSERR_BADDEVICEID;
512 memcpy(lpCaps, &destinations[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
513 return MMSYSERR_NOERROR;
516 static DWORD MIDIOut_GetNumDevs(void)
519 return MIDIOut_NumDevs;
522 static DWORD MIDIOut_GetVolume(WORD wDevID, DWORD *lpdwVolume)
524 TRACE("%d\n", wDevID);
526 if (wDevID >= MIDIOut_NumDevs) {
527 WARN("bad device ID : %d\n", wDevID);
528 return MMSYSERR_BADDEVICEID;
530 if (lpdwVolume == NULL) {
531 WARN("Invalid Parameter\n");
532 return MMSYSERR_INVALPARAM;
535 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
539 AudioUnit_GetVolume(destinations[wDevID].synth, &left, &right);
541 *lpdwVolume = (WORD) (left * 0xFFFF) + ((WORD) (right * 0xFFFF) << 16);
543 return MMSYSERR_NOERROR;
546 return MMSYSERR_NOTSUPPORTED;
549 static DWORD MIDIOut_SetVolume(WORD wDevID, DWORD dwVolume)
551 TRACE("%d\n", wDevID);
553 if (wDevID >= MIDIOut_NumDevs) {
554 WARN("bad device ID : %d\n", wDevID);
555 return MMSYSERR_BADDEVICEID;
557 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
562 left = LOWORD(dwVolume) / 65535.0f;
563 right = HIWORD(dwVolume) / 65535.0f;
564 AudioUnit_SetVolume(destinations[wDevID].synth, left, right);
566 return MMSYSERR_NOERROR;
569 return MMSYSERR_NOTSUPPORTED;
572 static DWORD MIDIOut_Reset(WORD wDevID)
576 TRACE("%d\n", wDevID);
578 if (wDevID >= MIDIOut_NumDevs) {
579 WARN("bad device ID : %d\n", wDevID);
580 return MMSYSERR_BADDEVICEID;
582 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
584 for (chn = 0; chn < 16; chn++) {
585 /* turn off every note */
586 MusicDeviceMIDIEvent(destinations[wDevID].synth, 0xB0 | chn, 0x7B, 0, 0);
587 /* remove sustain on channel */
588 MusicDeviceMIDIEvent(destinations[wDevID].synth, 0xB0 | chn, 0x40, 0, 0);
591 else FIXME("MOD_MIDIPORT\n");
593 /* FIXME: the LongData buffers must also be returned to the app */
594 return MMSYSERR_NOERROR;
597 static DWORD MIDIIn_Open(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
599 TRACE("wDevID=%d lpDesc=%p dwFlags=%08x\n", wDevID, lpDesc, dwFlags);
601 if (lpDesc == NULL) {
602 WARN("Invalid Parameter\n");
603 return MMSYSERR_INVALPARAM;
605 if (wDevID >= MIDIIn_NumDevs) {
606 WARN("bad device ID : %d\n", wDevID);
607 return MMSYSERR_BADDEVICEID;
609 if (sources[wDevID].midiDesc.hMidi != 0) {
610 WARN("device already open !\n");
611 return MMSYSERR_ALLOCATED;
613 if ((dwFlags & MIDI_IO_STATUS) != 0) {
614 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
615 dwFlags &= ~MIDI_IO_STATUS;
617 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
618 FIXME("Bad dwFlags\n");
619 return MMSYSERR_INVALFLAG;
622 sources[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
623 sources[wDevID].lpQueueHdr = NULL;
624 sources[wDevID].midiDesc = *lpDesc;
625 sources[wDevID].startTime = 0;
626 sources[wDevID].state = 0;
628 MIDI_NotifyClient(wDevID, MIM_OPEN, 0L, 0L);
629 return MMSYSERR_NOERROR;
632 static DWORD MIDIIn_Close(WORD wDevID)
634 DWORD ret = MMSYSERR_NOERROR;
636 TRACE("wDevID=%d\n", wDevID);
638 if (wDevID >= MIDIIn_NumDevs) {
639 WARN("bad device ID : %d\n", wDevID);
640 return MMSYSERR_BADDEVICEID;
643 if (sources[wDevID].midiDesc.hMidi == 0) {
644 WARN("device not opened !\n");
645 return MMSYSERR_ERROR;
647 if (sources[wDevID].lpQueueHdr != 0) {
648 return MIDIERR_STILLPLAYING;
651 MIDI_NotifyClient(wDevID, MIM_CLOSE, 0L, 0L);
652 sources[wDevID].midiDesc.hMidi = 0;
656 static DWORD MIDIIn_AddBuffer(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
658 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
660 if (wDevID >= MIDIIn_NumDevs) {
661 WARN("bad device ID : %d\n", wDevID);
662 return MMSYSERR_BADDEVICEID;
664 if (lpMidiHdr == NULL) {
665 WARN("Invalid Parameter\n");
666 return MMSYSERR_INVALPARAM;
668 if (dwSize < offsetof(MIDIHDR,dwOffset)) {
669 WARN("Invalid Parameter\n");
670 return MMSYSERR_INVALPARAM;
672 if (lpMidiHdr->dwBufferLength == 0) {
673 WARN("Invalid Parameter\n");
674 return MMSYSERR_INVALPARAM;
676 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) {
677 WARN("Still playing\n");
678 return MIDIERR_STILLPLAYING;
680 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) {
681 WARN("Unprepared\n");
682 return MIDIERR_UNPREPARED;
685 EnterCriticalSection(&midiInLock);
686 lpMidiHdr->dwFlags &= ~WHDR_DONE;
687 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
688 lpMidiHdr->dwBytesRecorded = 0;
689 lpMidiHdr->lpNext = 0;
690 if (sources[wDevID].lpQueueHdr == 0) {
691 sources[wDevID].lpQueueHdr = lpMidiHdr;
694 for (ptr = sources[wDevID].lpQueueHdr;
697 ptr->lpNext = lpMidiHdr;
699 LeaveCriticalSection(&midiInLock);
701 return MMSYSERR_NOERROR;
704 static DWORD MIDIIn_Prepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
706 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
708 if (wDevID >= MIDIIn_NumDevs) {
709 WARN("bad device ID : %d\n", wDevID);
710 return MMSYSERR_BADDEVICEID;
712 /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
713 * asks to prepare MIDIHDR which dwFlags != 0.
714 * So at least check for the inqueue flag
716 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0 ||
717 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0) {
718 WARN("Invalid parameter %p %p %08x %d\n", lpMidiHdr, lpMidiHdr->lpData,
719 lpMidiHdr->dwFlags, dwSize);
720 return MMSYSERR_INVALPARAM;
723 lpMidiHdr->lpNext = 0;
724 lpMidiHdr->dwFlags |= MHDR_PREPARED;
725 lpMidiHdr->dwFlags &= ~MHDR_DONE;
726 return MMSYSERR_NOERROR;
729 static DWORD MIDIIn_Unprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
731 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
732 if (wDevID >= MIDIIn_NumDevs) {
733 WARN("bad device ID : %d\n", wDevID);
734 return MMSYSERR_BADDEVICEID;
736 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0) {
737 WARN("Invalid Parameter\n");
738 return MMSYSERR_INVALPARAM;
740 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) {
741 WARN("Still playing\n");
742 return MIDIERR_STILLPLAYING;
745 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
746 return MMSYSERR_NOERROR;
749 static DWORD MIDIIn_GetDevCaps(WORD wDevID, LPMIDIINCAPSW lpCaps, DWORD dwSize)
751 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID, lpCaps, dwSize);
753 if (lpCaps == NULL) {
754 WARN("Invalid Parameter\n");
755 return MMSYSERR_INVALPARAM;
758 if (wDevID >= MIDIIn_NumDevs) {
759 WARN("bad device ID : %d\n", wDevID);
760 return MMSYSERR_BADDEVICEID;
762 memcpy(lpCaps, &sources[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
763 return MMSYSERR_NOERROR;
766 static DWORD MIDIIn_GetNumDevs(void)
769 return MIDIIn_NumDevs;
772 static DWORD MIDIIn_Start(WORD wDevID)
774 TRACE("%d\n", wDevID);
776 if (wDevID >= MIDIIn_NumDevs) {
777 WARN("bad device ID : %d\n", wDevID);
778 return MMSYSERR_BADDEVICEID;
780 sources[wDevID].state = 1;
781 sources[wDevID].startTime = GetTickCount();
782 return MMSYSERR_NOERROR;
785 static DWORD MIDIIn_Stop(WORD wDevID)
787 TRACE("%d\n", wDevID);
788 if (wDevID >= MIDIIn_NumDevs) {
789 WARN("bad device ID : %d\n", wDevID);
790 return MMSYSERR_BADDEVICEID;
792 sources[wDevID].state = 0;
793 return MMSYSERR_NOERROR;
796 static DWORD MIDIIn_Reset(WORD wDevID)
798 DWORD dwTime = GetTickCount();
800 TRACE("%d\n", wDevID);
801 if (wDevID >= MIDIIn_NumDevs) {
802 WARN("bad device ID : %d\n", wDevID);
803 return MMSYSERR_BADDEVICEID;
806 EnterCriticalSection(&midiInLock);
807 while (sources[wDevID].lpQueueHdr) {
808 LPMIDIHDR lpMidiHdr = sources[wDevID].lpQueueHdr;
809 sources[wDevID].lpQueueHdr = lpMidiHdr->lpNext;
810 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
811 lpMidiHdr->dwFlags |= MHDR_DONE;
812 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
813 MIDI_NotifyClient(wDevID, MIM_LONGDATA, (DWORD_PTR)lpMidiHdr, dwTime);
815 LeaveCriticalSection(&midiInLock);
817 return MMSYSERR_NOERROR;
821 * MIDI In Mach message handling
825 * Call from CoreMIDI IO threaded callback,
826 * we can't call Wine debug channels, critical section or anything using NtCurrentTeb here.
828 void MIDIIn_SendMessage(MIDIMessage msg)
832 CFMessagePortRef messagePort;
833 messagePort = CFMessagePortCreateRemote(kCFAllocatorDefault, MIDIInThreadPortName);
835 data = CFDataCreate(kCFAllocatorDefault, (UInt8 *) &msg, sizeof(msg));
838 CFMessagePortSendRequest(messagePort, 0, data, 0.0, 0.0, NULL, NULL);
840 CFRelease(messagePort);
844 static CFDataRef MIDIIn_MessageHandler(CFMessagePortRef local, SInt32 msgid, CFDataRef data, void *info)
846 MIDIMessage *msg = NULL;
848 MIDISource *src = NULL;
856 msg = (MIDIMessage *) CFDataGetBytePtr(data);
857 TRACE("devID=%d\n", msg->devID);
858 for (i = 0; i < msg->length; ++i) {
859 TRACE("%02X ", msg->data[i]);
862 src = &sources[msg->devID];
865 TRACE("input not started, thrown away\n");
868 /* FIXME skipping SysEx */
869 if (msg->data[0] == 0xF0)
871 FIXME("Starting System Exclusive\n");
876 for (i = 0; i < msg->length; ++i)
878 if (msg->data[i] == 0xF7)
880 FIXME("Ending System Exclusive\n");
886 EnterCriticalSection(&midiInLock);
887 currentTime = GetTickCount() - src->startTime;
889 while (pos < msg->length)
892 switch (msg->data[pos] & 0xF0)
895 sendData = (msg->data[pos] << 0);
901 sendData = (msg->data[pos + 1] << 8) | (msg->data[pos] << 0);
905 sendData = (msg->data[pos + 2] << 16) |
906 (msg->data[pos + 1] << 8) |
907 (msg->data[pos] << 0);
911 MIDI_NotifyClient(msg->devID, MIM_DATA, sendData, currentTime);
913 LeaveCriticalSection(&midiInLock);
916 CFRunLoopStop(CFRunLoopGetCurrent());
923 static DWORD WINAPI MIDIIn_MessageThread(LPVOID p)
925 CFMessagePortRef local;
926 CFRunLoopSourceRef source;
929 local = CFMessagePortCreateLocal(kCFAllocatorDefault, MIDIInThreadPortName, &MIDIIn_MessageHandler, NULL, &info);
931 source = CFMessagePortCreateRunLoopSource(kCFAllocatorDefault, local, 0);
932 CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
936 CFRunLoopSourceInvalidate(source);
939 CFRelease(MIDIInThreadPortName);
940 MIDIInThreadPortName = NULL;
945 /**************************************************************************
948 DWORD WINAPI CoreAudio_modMessage(UINT wDevID, UINT wMsg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2)
950 TRACE("%d %08x %08x %08x %08x\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
959 return MIDIOut_Open(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
961 return MIDIOut_Close(wDevID);
963 return MIDIOut_Data(wDevID, dwParam1);
965 return MIDIOut_LongData(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
967 return MIDIOut_Prepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
969 return MIDIOut_Unprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
970 case MODM_GETDEVCAPS:
971 return MIDIOut_GetDevCaps(wDevID, (LPMIDIOUTCAPSW) dwParam1, dwParam2);
972 case MODM_GETNUMDEVS:
973 return MIDIOut_GetNumDevs();
975 return MIDIOut_GetVolume(wDevID, (DWORD*)dwParam1);
977 return MIDIOut_SetVolume(wDevID, dwParam1);
979 return MIDIOut_Reset(wDevID);
981 TRACE("Unsupported message (08%x)\n", wMsg);
983 return MMSYSERR_NOTSUPPORTED;
986 /**************************************************************************
989 DWORD WINAPI CoreAudio_midMessage(UINT wDevID, UINT wMsg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2)
991 TRACE("%d %08x %08x %08x %08x\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
999 return MIDIIn_Open(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1001 return MIDIIn_Close(wDevID);
1002 case MIDM_ADDBUFFER:
1003 return MIDIIn_AddBuffer(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1005 return MIDIIn_Prepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1006 case MIDM_UNPREPARE:
1007 return MIDIIn_Unprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1008 case MIDM_GETDEVCAPS:
1009 return MIDIIn_GetDevCaps(wDevID, (LPMIDIINCAPSW) dwParam1, dwParam2);
1010 case MIDM_GETNUMDEVS:
1011 return MIDIIn_GetNumDevs();
1013 return MIDIIn_Start(wDevID);
1015 return MIDIIn_Stop(wDevID);
1017 return MIDIIn_Reset(wDevID);
1019 TRACE("Unsupported message\n");
1021 return MMSYSERR_NOTSUPPORTED;