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 midiInLock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": midiInLock");
130 MIDIInThreadPortName
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("MIDIInThreadPortName.%u"), getpid());
131 CloseHandle( CreateThread(NULL
, 0, MIDIIn_MessageThread
, NULL
, 0, NULL
));
133 name
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("WineInputPort.%u"), getpid());
134 MIDIInputPortCreate(wineMIDIClient
, name
, MIDIIn_ReadProc
, NULL
, &MIDIInPort
);
139 name
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("WineOutputPort.%u"), getpid());
140 MIDIOutputPortCreate(wineMIDIClient
, name
, &MIDIOutPort
);
144 /* initialize sources */
145 for (i
= 0; i
< MIDIIn_NumDevs
; i
++)
147 sources
[i
].wDevID
= i
;
148 sources
[i
].source
= MIDIGetSource(i
);
150 CoreMIDI_GetObjectName(sources
[i
].source
, szPname
, sizeof(szPname
));
151 MultiByteToWideChar(CP_ACP
, 0, szPname
, -1, sources
[i
].caps
.szPname
, sizeof(sources
[i
].caps
.szPname
)/sizeof(WCHAR
));
153 MIDIPortConnectSource(MIDIInPort
, sources
[i
].source
, &sources
[i
].wDevID
);
155 sources
[i
].state
= 0;
157 sources
[i
].caps
.wMid
= 0x00FF; /* Manufac ID */
158 sources
[i
].caps
.wPid
= 0x0001; /* Product ID */
159 sources
[i
].caps
.vDriverVersion
= 0x0001;
160 sources
[i
].caps
.dwSupport
= 0;
163 /* initialise MIDI synths */
164 for (i
= 0; i
< MAX_MIDI_SYNTHS
; i
++)
166 snprintf(szPname
, sizeof(szPname
), "CoreAudio MIDI Synth %d", i
);
167 MultiByteToWideChar(CP_ACP
, 0, szPname
, -1, destinations
[i
].caps
.szPname
, sizeof(destinations
[i
].caps
.szPname
)/sizeof(WCHAR
));
169 destinations
[i
].caps
.wTechnology
= MOD_SYNTH
;
170 destinations
[i
].caps
.wChannelMask
= 0xFFFF;
172 destinations
[i
].caps
.wMid
= 0x00FF; /* Manufac ID */
173 destinations
[i
].caps
.wPid
= 0x0001; /* Product ID */
174 destinations
[i
].caps
.vDriverVersion
= 0x0001;
175 destinations
[i
].caps
.dwSupport
= MIDICAPS_VOLUME
;
176 destinations
[i
].caps
.wVoices
= 16;
177 destinations
[i
].caps
.wNotes
= 16;
179 /* initialise available destinations */
180 for (i
= MAX_MIDI_SYNTHS
; i
< numDest
+ MAX_MIDI_SYNTHS
; i
++)
182 destinations
[i
].dest
= MIDIGetDestination(i
- MAX_MIDI_SYNTHS
);
184 CoreMIDI_GetObjectName(destinations
[i
].dest
, szPname
, sizeof(szPname
));
185 MultiByteToWideChar(CP_ACP
, 0, szPname
, -1, destinations
[i
].caps
.szPname
, sizeof(destinations
[i
].caps
.szPname
)/sizeof(WCHAR
));
187 destinations
[i
].caps
.wTechnology
= MOD_MIDIPORT
;
188 destinations
[i
].caps
.wChannelMask
= 0xFFFF;
190 destinations
[i
].caps
.wMid
= 0x00FF; /* Manufac ID */
191 destinations
[i
].caps
.wPid
= 0x0001;
192 destinations
[i
].caps
.vDriverVersion
= 0x0001;
193 destinations
[i
].caps
.dwSupport
= 0;
194 destinations
[i
].caps
.wVoices
= 0;
195 destinations
[i
].caps
.wNotes
= 0;
200 LONG
CoreAudio_MIDIRelease(void)
203 if (MIDIIn_NumDevs
> 0)
205 CFMessagePortRef messagePort
;
206 /* Stop CFRunLoop in MIDIIn_MessageThread */
207 messagePort
= CFMessagePortCreateRemote(kCFAllocatorDefault
, MIDIInThreadPortName
);
208 CFMessagePortSendRequest(messagePort
, 1, NULL
, 0.0, 0.0, NULL
, NULL
);
209 CFRelease(messagePort
);
211 midiInLock
.DebugInfo
->Spare
[0] = 0;
212 DeleteCriticalSection(&midiInLock
);
215 if (wineMIDIClient
) MIDIClientDispose(wineMIDIClient
); /* MIDIClientDispose will close all ports */
217 HeapFree(GetProcessHeap(), 0, sources
);
218 HeapFree(GetProcessHeap(), 0, destinations
);
223 /**************************************************************************
224 * MIDI_NotifyClient [internal]
226 static void MIDI_NotifyClient(UINT wDevID
, WORD wMsg
, DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
233 TRACE("wDevID=%d wMsg=%d dwParm1=%04lX dwParam2=%04lX\n", wDevID
, wMsg
, dwParam1
, dwParam2
);
240 dwCallBack
= destinations
[wDevID
].midiDesc
.dwCallback
;
241 uFlags
= destinations
[wDevID
].wFlags
;
242 hDev
= destinations
[wDevID
].midiDesc
.hMidi
;
243 dwInstance
= destinations
[wDevID
].midiDesc
.dwInstance
;
253 dwCallBack
= sources
[wDevID
].midiDesc
.dwCallback
;
254 uFlags
= sources
[wDevID
].wFlags
;
255 hDev
= sources
[wDevID
].midiDesc
.hMidi
;
256 dwInstance
= sources
[wDevID
].midiDesc
.dwInstance
;
259 ERR("Unsupported MSW-MIDI message %u\n", wMsg
);
263 DriverCallback(dwCallBack
, uFlags
, hDev
, wMsg
, dwInstance
, dwParam1
, dwParam2
);
266 static DWORD
MIDIOut_Open(WORD wDevID
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
268 MIDIDestination
*dest
;
270 TRACE("wDevID=%d lpDesc=%p dwFlags=%08x\n", wDevID
, lpDesc
, dwFlags
);
272 if (lpDesc
== NULL
) {
273 WARN("Invalid Parameter\n");
274 return MMSYSERR_INVALPARAM
;
277 if (wDevID
>= MIDIOut_NumDevs
) {
278 WARN("bad device ID : %d\n", wDevID
);
279 return MMSYSERR_BADDEVICEID
;
282 if (destinations
[wDevID
].midiDesc
.hMidi
!= 0) {
283 WARN("device already open !\n");
284 return MMSYSERR_ALLOCATED
;
287 if ((dwFlags
& ~CALLBACK_TYPEMASK
) != 0) {
288 WARN("bad dwFlags\n");
289 return MMSYSERR_INVALFLAG
;
291 dest
= &destinations
[wDevID
];
293 if (dest
->caps
.wTechnology
== MOD_SYNTH
)
295 if (!SynthUnit_CreateDefaultSynthUnit(&dest
->graph
, &dest
->synth
))
297 ERR("SynthUnit_CreateDefaultSynthUnit dest=%p failed\n", dest
);
298 return MMSYSERR_ERROR
;
301 if (!SynthUnit_Initialize(dest
->synth
, dest
->graph
))
303 ERR("SynthUnit_Initialise dest=%p failed\n", dest
);
304 return MMSYSERR_ERROR
;
307 dest
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
308 dest
->midiDesc
= *lpDesc
;
310 MIDI_NotifyClient(wDevID
, MOM_OPEN
, 0L, 0L);
311 return MMSYSERR_NOERROR
;
314 static DWORD
MIDIOut_Close(WORD wDevID
)
316 DWORD ret
= MMSYSERR_NOERROR
;
318 TRACE("wDevID=%d\n", wDevID
);
320 if (wDevID
>= MIDIOut_NumDevs
) {
321 WARN("bad device ID : %d\n", wDevID
);
322 return MMSYSERR_BADDEVICEID
;
325 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
326 SynthUnit_Close(destinations
[wDevID
].graph
);
328 destinations
[wDevID
].graph
= 0;
329 destinations
[wDevID
].synth
= 0;
331 MIDI_NotifyClient(wDevID
, MOM_CLOSE
, 0L, 0L);
332 destinations
[wDevID
].midiDesc
.hMidi
= 0;
337 static DWORD
MIDIOut_Data(WORD wDevID
, DWORD dwParam
)
339 WORD evt
= LOBYTE(LOWORD(dwParam
));
340 UInt8 chn
= (evt
& 0x0F);
342 TRACE("wDevID=%d dwParam=%08X\n", wDevID
, dwParam
);
344 if (wDevID
>= MIDIOut_NumDevs
) {
345 WARN("bad device ID : %d\n", wDevID
);
346 return MMSYSERR_BADDEVICEID
;
349 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
351 WORD d1
= HIBYTE(LOWORD(dwParam
));
352 WORD d2
= LOBYTE(HIWORD(dwParam
));
353 OSStatus err
= noErr
;
355 err
= MusicDeviceMIDIEvent(destinations
[wDevID
].synth
, (evt
& 0xF0) | chn
, d1
, d2
, 0);
358 ERR("MusicDeviceMIDIEvent(%p, %04x, %04x, %04x, %d) return %s\n", destinations
[wDevID
].synth
, (evt
& 0xF0) | chn
, d1
, d2
, 0, wine_dbgstr_fourcc(err
));
359 return MMSYSERR_ERROR
;
365 buffer
[0] = (evt
& 0xF0) | chn
;
366 buffer
[1] = HIBYTE(LOWORD(dwParam
));
367 buffer
[2] = LOBYTE(HIWORD(dwParam
));
369 MIDIOut_Send(MIDIOutPort
, destinations
[wDevID
].dest
, buffer
, 3);
372 return MMSYSERR_NOERROR
;
375 static DWORD
MIDIOut_LongData(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
378 OSStatus err
= noErr
;
380 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
382 /* Note: MS doc does not say much about the dwBytesRecorded member of the MIDIHDR structure
383 * but it seems to be used only for midi input.
384 * Taking a look at the WAVEHDR structure (which is quite similar) confirms this assumption.
387 if (wDevID
>= MIDIOut_NumDevs
) {
388 WARN("bad device ID : %d\n", wDevID
);
389 return MMSYSERR_BADDEVICEID
;
392 if (lpMidiHdr
== NULL
) {
393 WARN("Invalid Parameter\n");
394 return MMSYSERR_INVALPARAM
;
397 lpData
= (LPBYTE
) lpMidiHdr
->lpData
;
400 return MIDIERR_UNPREPARED
;
401 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
402 return MIDIERR_UNPREPARED
;
403 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
404 return MIDIERR_STILLPLAYING
;
405 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
406 lpMidiHdr
->dwFlags
|= MHDR_INQUEUE
;
408 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
409 * data, or can it also contain raw MIDI data, to be split up and sent to
411 * If the latest is true, then the following WARNing will fire up
413 if (lpData
[0] != 0xF0 || lpData
[lpMidiHdr
->dwBufferLength
- 1] != 0xF7) {
414 WARN("Alledged system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
417 TRACE("dwBufferLength=%u !\n", lpMidiHdr
->dwBufferLength
);
418 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
419 lpData
[0], lpData
[1], lpData
[2], lpData
[lpMidiHdr
->dwBufferLength
-3],
420 lpData
[lpMidiHdr
->dwBufferLength
-2], lpData
[lpMidiHdr
->dwBufferLength
-1]);
423 if (lpData
[0] != 0xF0) {
424 /* System Exclusive */
425 ERR("Add missing 0xF0 marker at the beginning of system exclusive byte stream\n");
427 if (lpData
[lpMidiHdr
->dwBufferLength
- 1] != 0xF7) {
428 /* Send end of System Exclusive */
429 ERR("Add missing 0xF7 marker at the end of system exclusive byte stream\n");
431 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
) /* FIXME */
433 err
= MusicDeviceSysEx(destinations
[wDevID
].synth
, (const UInt8
*) lpData
, lpMidiHdr
->dwBufferLength
);
436 ERR("MusicDeviceSysEx(%p, %p, %d) return %s\n", destinations
[wDevID
].synth
, lpData
, lpMidiHdr
->dwBufferLength
, wine_dbgstr_fourcc(err
));
437 return MMSYSERR_ERROR
;
442 FIXME("MOD_MIDIPORT\n");
445 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
446 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
447 MIDI_NotifyClient(wDevID
, MOM_DONE
, (DWORD_PTR
)lpMidiHdr
, 0L);
448 return MMSYSERR_NOERROR
;
451 /**************************************************************************
452 * MIDIOut_Prepare [internal]
454 static DWORD
MIDIOut_Prepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
456 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
458 if (wDevID
>= MIDIOut_NumDevs
) {
459 WARN("bad device ID : %d\n", wDevID
);
460 return MMSYSERR_BADDEVICEID
;
463 /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
464 * asks to prepare MIDIHDR which dwFlags != 0.
465 * So at least check for the inqueue flag
467 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 ||
468 lpMidiHdr
->lpData
== 0 || (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) != 0) {
469 WARN("%p %p %08x %lu/%d\n", lpMidiHdr
, lpMidiHdr
->lpData
,
470 lpMidiHdr
->dwFlags
, offsetof(MIDIHDR
,dwOffset
), dwSize
);
471 return MMSYSERR_INVALPARAM
;
474 lpMidiHdr
->lpNext
= 0;
475 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
476 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
477 return MMSYSERR_NOERROR
;
480 /**************************************************************************
481 * MIDIOut_Unprepare [internal]
483 static DWORD
MIDIOut_Unprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
485 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
487 if (wDevID
>= MIDIOut_NumDevs
) {
488 WARN("bad device ID : %d\n", wDevID
);
489 return MMSYSERR_BADDEVICEID
;
491 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0)
492 return MMSYSERR_INVALPARAM
;
493 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
494 return MIDIERR_STILLPLAYING
;
496 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
498 return MMSYSERR_NOERROR
;
501 static DWORD
MIDIOut_GetDevCaps(WORD wDevID
, LPMIDIOUTCAPSW lpCaps
, DWORD dwSize
)
503 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID
, lpCaps
, dwSize
);
505 if (lpCaps
== NULL
) {
506 WARN("Invalid Parameter\n");
507 return MMSYSERR_INVALPARAM
;
510 if (wDevID
>= MIDIOut_NumDevs
) {
511 WARN("bad device ID : %d\n", wDevID
);
512 return MMSYSERR_BADDEVICEID
;
514 memcpy(lpCaps
, &destinations
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
515 return MMSYSERR_NOERROR
;
518 static DWORD
MIDIOut_GetNumDevs(void)
521 return MIDIOut_NumDevs
;
524 static DWORD
MIDIOut_GetVolume(WORD wDevID
, DWORD
*lpdwVolume
)
526 TRACE("%d\n", wDevID
);
528 if (wDevID
>= MIDIOut_NumDevs
) {
529 WARN("bad device ID : %d\n", wDevID
);
530 return MMSYSERR_BADDEVICEID
;
532 if (lpdwVolume
== NULL
) {
533 WARN("Invalid Parameter\n");
534 return MMSYSERR_INVALPARAM
;
537 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
541 AudioUnit_GetVolume(destinations
[wDevID
].synth
, &left
, &right
);
543 *lpdwVolume
= (WORD
) (left
* 0xFFFF) + ((WORD
) (right
* 0xFFFF) << 16);
545 return MMSYSERR_NOERROR
;
548 return MMSYSERR_NOTSUPPORTED
;
551 static DWORD
MIDIOut_SetVolume(WORD wDevID
, DWORD dwVolume
)
553 TRACE("%d\n", wDevID
);
555 if (wDevID
>= MIDIOut_NumDevs
) {
556 WARN("bad device ID : %d\n", wDevID
);
557 return MMSYSERR_BADDEVICEID
;
559 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
564 left
= LOWORD(dwVolume
) / 65535.0f
;
565 right
= HIWORD(dwVolume
) / 65535.0f
;
566 AudioUnit_SetVolume(destinations
[wDevID
].synth
, left
, right
);
568 return MMSYSERR_NOERROR
;
571 return MMSYSERR_NOTSUPPORTED
;
574 static DWORD
MIDIOut_Reset(WORD wDevID
)
578 TRACE("%d\n", wDevID
);
580 if (wDevID
>= MIDIOut_NumDevs
) {
581 WARN("bad device ID : %d\n", wDevID
);
582 return MMSYSERR_BADDEVICEID
;
584 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
586 for (chn
= 0; chn
< 16; chn
++) {
587 /* turn off every note */
588 MusicDeviceMIDIEvent(destinations
[wDevID
].synth
, 0xB0 | chn
, 0x7B, 0, 0);
589 /* remove sustain on channel */
590 MusicDeviceMIDIEvent(destinations
[wDevID
].synth
, 0xB0 | chn
, 0x40, 0, 0);
593 else FIXME("MOD_MIDIPORT\n");
595 /* FIXME: the LongData buffers must also be returned to the app */
596 return MMSYSERR_NOERROR
;
599 static DWORD
MIDIIn_Open(WORD wDevID
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
601 TRACE("wDevID=%d lpDesc=%p dwFlags=%08x\n", wDevID
, lpDesc
, dwFlags
);
603 if (lpDesc
== NULL
) {
604 WARN("Invalid Parameter\n");
605 return MMSYSERR_INVALPARAM
;
607 if (wDevID
>= MIDIIn_NumDevs
) {
608 WARN("bad device ID : %d\n", wDevID
);
609 return MMSYSERR_BADDEVICEID
;
611 if (sources
[wDevID
].midiDesc
.hMidi
!= 0) {
612 WARN("device already open !\n");
613 return MMSYSERR_ALLOCATED
;
615 if ((dwFlags
& MIDI_IO_STATUS
) != 0) {
616 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
617 dwFlags
&= ~MIDI_IO_STATUS
;
619 if ((dwFlags
& ~CALLBACK_TYPEMASK
) != 0) {
620 FIXME("Bad dwFlags\n");
621 return MMSYSERR_INVALFLAG
;
624 sources
[wDevID
].wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
625 sources
[wDevID
].lpQueueHdr
= NULL
;
626 sources
[wDevID
].midiDesc
= *lpDesc
;
627 sources
[wDevID
].startTime
= 0;
628 sources
[wDevID
].state
= 0;
630 MIDI_NotifyClient(wDevID
, MIM_OPEN
, 0L, 0L);
631 return MMSYSERR_NOERROR
;
634 static DWORD
MIDIIn_Close(WORD wDevID
)
636 DWORD ret
= MMSYSERR_NOERROR
;
638 TRACE("wDevID=%d\n", wDevID
);
640 if (wDevID
>= MIDIIn_NumDevs
) {
641 WARN("bad device ID : %d\n", wDevID
);
642 return MMSYSERR_BADDEVICEID
;
645 if (sources
[wDevID
].midiDesc
.hMidi
== 0) {
646 WARN("device not opened !\n");
647 return MMSYSERR_ERROR
;
649 if (sources
[wDevID
].lpQueueHdr
!= 0) {
650 return MIDIERR_STILLPLAYING
;
653 MIDI_NotifyClient(wDevID
, MIM_CLOSE
, 0L, 0L);
654 sources
[wDevID
].midiDesc
.hMidi
= 0;
658 static DWORD
MIDIIn_AddBuffer(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
660 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
662 if (wDevID
>= MIDIIn_NumDevs
) {
663 WARN("bad device ID : %d\n", wDevID
);
664 return MMSYSERR_BADDEVICEID
;
666 if (lpMidiHdr
== NULL
) {
667 WARN("Invalid Parameter\n");
668 return MMSYSERR_INVALPARAM
;
670 if (dwSize
< offsetof(MIDIHDR
,dwOffset
)) {
671 WARN("Invalid Parameter\n");
672 return MMSYSERR_INVALPARAM
;
674 if (lpMidiHdr
->dwBufferLength
== 0) {
675 WARN("Invalid Parameter\n");
676 return MMSYSERR_INVALPARAM
;
678 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) {
679 WARN("Still playing\n");
680 return MIDIERR_STILLPLAYING
;
682 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
)) {
683 WARN("Unprepared\n");
684 return MIDIERR_UNPREPARED
;
687 EnterCriticalSection(&midiInLock
);
688 lpMidiHdr
->dwFlags
&= ~WHDR_DONE
;
689 lpMidiHdr
->dwFlags
|= MHDR_INQUEUE
;
690 lpMidiHdr
->dwBytesRecorded
= 0;
691 lpMidiHdr
->lpNext
= 0;
692 if (sources
[wDevID
].lpQueueHdr
== 0) {
693 sources
[wDevID
].lpQueueHdr
= lpMidiHdr
;
696 for (ptr
= sources
[wDevID
].lpQueueHdr
;
699 ptr
->lpNext
= lpMidiHdr
;
701 LeaveCriticalSection(&midiInLock
);
703 return MMSYSERR_NOERROR
;
706 static DWORD
MIDIIn_Prepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
708 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
710 if (wDevID
>= MIDIIn_NumDevs
) {
711 WARN("bad device ID : %d\n", wDevID
);
712 return MMSYSERR_BADDEVICEID
;
714 /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
715 * asks to prepare MIDIHDR which dwFlags != 0.
716 * So at least check for the inqueue flag
718 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 ||
719 lpMidiHdr
->lpData
== 0 || (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) != 0) {
720 WARN("Invalid parameter %p %p %08x %d\n", lpMidiHdr
, lpMidiHdr
->lpData
,
721 lpMidiHdr
->dwFlags
, dwSize
);
722 return MMSYSERR_INVALPARAM
;
725 lpMidiHdr
->lpNext
= 0;
726 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
727 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
728 return MMSYSERR_NOERROR
;
731 static DWORD
MIDIIn_Unprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
733 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
734 if (wDevID
>= MIDIIn_NumDevs
) {
735 WARN("bad device ID : %d\n", wDevID
);
736 return MMSYSERR_BADDEVICEID
;
738 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0) {
739 WARN("Invalid Parameter\n");
740 return MMSYSERR_INVALPARAM
;
742 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) {
743 WARN("Still playing\n");
744 return MIDIERR_STILLPLAYING
;
747 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
748 return MMSYSERR_NOERROR
;
751 static DWORD
MIDIIn_GetDevCaps(WORD wDevID
, LPMIDIINCAPSW lpCaps
, DWORD dwSize
)
753 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID
, lpCaps
, dwSize
);
755 if (lpCaps
== NULL
) {
756 WARN("Invalid Parameter\n");
757 return MMSYSERR_INVALPARAM
;
760 if (wDevID
>= MIDIIn_NumDevs
) {
761 WARN("bad device ID : %d\n", wDevID
);
762 return MMSYSERR_BADDEVICEID
;
764 memcpy(lpCaps
, &sources
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
765 return MMSYSERR_NOERROR
;
768 static DWORD
MIDIIn_GetNumDevs(void)
771 return MIDIIn_NumDevs
;
774 static DWORD
MIDIIn_Start(WORD wDevID
)
776 TRACE("%d\n", wDevID
);
778 if (wDevID
>= MIDIIn_NumDevs
) {
779 WARN("bad device ID : %d\n", wDevID
);
780 return MMSYSERR_BADDEVICEID
;
782 sources
[wDevID
].state
= 1;
783 sources
[wDevID
].startTime
= GetTickCount();
784 return MMSYSERR_NOERROR
;
787 static DWORD
MIDIIn_Stop(WORD wDevID
)
789 TRACE("%d\n", wDevID
);
790 if (wDevID
>= MIDIIn_NumDevs
) {
791 WARN("bad device ID : %d\n", wDevID
);
792 return MMSYSERR_BADDEVICEID
;
794 sources
[wDevID
].state
= 0;
795 return MMSYSERR_NOERROR
;
798 static DWORD
MIDIIn_Reset(WORD wDevID
)
800 DWORD dwTime
= GetTickCount();
802 TRACE("%d\n", wDevID
);
803 if (wDevID
>= MIDIIn_NumDevs
) {
804 WARN("bad device ID : %d\n", wDevID
);
805 return MMSYSERR_BADDEVICEID
;
808 EnterCriticalSection(&midiInLock
);
809 while (sources
[wDevID
].lpQueueHdr
) {
810 LPMIDIHDR lpMidiHdr
= sources
[wDevID
].lpQueueHdr
;
811 sources
[wDevID
].lpQueueHdr
= lpMidiHdr
->lpNext
;
812 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
813 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
814 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
815 MIDI_NotifyClient(wDevID
, MIM_LONGDATA
, (DWORD_PTR
)lpMidiHdr
, dwTime
);
817 LeaveCriticalSection(&midiInLock
);
819 return MMSYSERR_NOERROR
;
823 * MIDI In Mach message handling
827 * Call from CoreMIDI IO threaded callback,
828 * we can't call Wine debug channels, critical section or anything using NtCurrentTeb here.
830 void MIDIIn_SendMessage(MIDIMessage msg
)
834 CFMessagePortRef messagePort
;
835 messagePort
= CFMessagePortCreateRemote(kCFAllocatorDefault
, MIDIInThreadPortName
);
837 data
= CFDataCreate(kCFAllocatorDefault
, (UInt8
*) &msg
, sizeof(msg
));
840 CFMessagePortSendRequest(messagePort
, 0, data
, 0.0, 0.0, NULL
, NULL
);
842 CFRelease(messagePort
);
846 static CFDataRef
MIDIIn_MessageHandler(CFMessagePortRef local
, SInt32 msgid
, CFDataRef data
, void *info
)
848 MIDIMessage
*msg
= NULL
;
850 MIDISource
*src
= NULL
;
858 msg
= (MIDIMessage
*) CFDataGetBytePtr(data
);
859 TRACE("devID=%d\n", msg
->devID
);
860 for (i
= 0; i
< msg
->length
; ++i
) {
861 TRACE("%02X ", msg
->data
[i
]);
864 src
= &sources
[msg
->devID
];
867 TRACE("input not started, thrown away\n");
870 /* FIXME skipping SysEx */
871 if (msg
->data
[0] == 0xF0)
873 FIXME("Starting System Exclusive\n");
878 for (i
= 0; i
< msg
->length
; ++i
)
880 if (msg
->data
[i
] == 0xF7)
882 FIXME("Ending System Exclusive\n");
888 EnterCriticalSection(&midiInLock
);
889 currentTime
= GetTickCount() - src
->startTime
;
891 while (pos
< msg
->length
)
894 switch (msg
->data
[pos
] & 0xF0)
897 sendData
= (msg
->data
[pos
] << 0);
903 sendData
= (msg
->data
[pos
+ 1] << 8) | (msg
->data
[pos
] << 0);
907 sendData
= (msg
->data
[pos
+ 2] << 16) |
908 (msg
->data
[pos
+ 1] << 8) |
909 (msg
->data
[pos
] << 0);
913 MIDI_NotifyClient(msg
->devID
, MIM_DATA
, sendData
, currentTime
);
915 LeaveCriticalSection(&midiInLock
);
918 CFRunLoopStop(CFRunLoopGetCurrent());
925 static DWORD WINAPI
MIDIIn_MessageThread(LPVOID p
)
927 CFMessagePortRef local
;
928 CFRunLoopSourceRef source
;
931 local
= CFMessagePortCreateLocal(kCFAllocatorDefault
, MIDIInThreadPortName
, &MIDIIn_MessageHandler
, NULL
, &info
);
933 source
= CFMessagePortCreateRunLoopSource(kCFAllocatorDefault
, local
, 0);
934 CFRunLoopAddSource(CFRunLoopGetCurrent(), source
, kCFRunLoopDefaultMode
);
938 CFRunLoopSourceInvalidate(source
);
941 CFRelease(MIDIInThreadPortName
);
942 MIDIInThreadPortName
= NULL
;
947 /**************************************************************************
950 DWORD WINAPI
CoreAudio_modMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
, DWORD dwParam1
, DWORD dwParam2
)
952 TRACE("%d %08x %08x %08x %08x\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
961 return MIDIOut_Open(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
963 return MIDIOut_Close(wDevID
);
965 return MIDIOut_Data(wDevID
, dwParam1
);
967 return MIDIOut_LongData(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
969 return MIDIOut_Prepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
971 return MIDIOut_Unprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
972 case MODM_GETDEVCAPS
:
973 return MIDIOut_GetDevCaps(wDevID
, (LPMIDIOUTCAPSW
) dwParam1
, dwParam2
);
974 case MODM_GETNUMDEVS
:
975 return MIDIOut_GetNumDevs();
977 return MIDIOut_GetVolume(wDevID
, (DWORD
*)dwParam1
);
979 return MIDIOut_SetVolume(wDevID
, dwParam1
);
981 return MIDIOut_Reset(wDevID
);
983 TRACE("Unsupported message (08%x)\n", wMsg
);
985 return MMSYSERR_NOTSUPPORTED
;
988 /**************************************************************************
991 DWORD WINAPI
CoreAudio_midMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
, DWORD dwParam1
, DWORD dwParam2
)
993 TRACE("%d %08x %08x %08x %08x\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1001 return MIDIIn_Open(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
1003 return MIDIIn_Close(wDevID
);
1004 case MIDM_ADDBUFFER
:
1005 return MIDIIn_AddBuffer(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1007 return MIDIIn_Prepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1008 case MIDM_UNPREPARE
:
1009 return MIDIIn_Unprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1010 case MIDM_GETDEVCAPS
:
1011 return MIDIIn_GetDevCaps(wDevID
, (LPMIDIINCAPSW
) dwParam1
, dwParam2
);
1012 case MIDM_GETNUMDEVS
:
1013 return MIDIIn_GetNumDevs();
1015 return MIDIIn_Start(wDevID
);
1017 return MIDIIn_Stop(wDevID
);
1019 return MIDIIn_Reset(wDevID
);
1021 TRACE("Unsupported message\n");
1023 return MMSYSERR_NOTSUPPORTED
;