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 #if defined(HAVE_COREAUDIO_COREAUDIO_H)
48 #include <CoreAudio/CoreAudio.h>
50 #define WINE_DEFINITIONS
53 static MIDIClientRef wineMIDIClient
= NULL
;
55 static DWORD MIDIOut_NumDevs
= 0;
56 static DWORD MIDIIn_NumDevs
= 0;
58 typedef struct tagMIDIDestination
{
59 /* graph and synth are only used for MIDI Synth */
66 MIDIOPENDESC midiDesc
;
70 typedef struct tagMIDISource
{
71 MIDIEndpointRef source
;
74 int state
; /* 0 is no recording started, 1 in recording, bit 2 set if in sys exclusive recording */
76 MIDIOPENDESC midiDesc
;
82 static CRITICAL_SECTION midiInLock
; /* Critical section for MIDI In */
83 static CFStringRef MIDIInThreadPortName
= NULL
;
85 static DWORD WINAPI
MIDIIn_MessageThread(LPVOID p
);
87 static MIDIPortRef MIDIInPort
= NULL
;
88 static MIDIPortRef MIDIOutPort
= NULL
;
90 #define MAX_MIDI_SYNTHS 1
92 MIDIDestination
*destinations
;
95 extern int SynthUnit_CreateDefaultSynthUnit(AUGraph
*graph
, AudioUnit
*synth
);
96 extern int SynthUnit_Initialize(AudioUnit synth
, AUGraph graph
);
97 extern int SynthUnit_Close(AUGraph graph
);
100 LONG
CoreAudio_MIDIInit(void)
103 CHAR szPname
[MAXPNAMELEN
] = {0};
105 int numDest
= MIDIGetNumberOfDestinations();
106 CFStringRef name
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("wineMIDIClient.%d"), getpid());
108 wineMIDIClient
= CoreMIDI_CreateClient( name
);
109 if (wineMIDIClient
== NULL
)
112 ERR("can't create wineMIDIClient\n");
117 MIDIOut_NumDevs
= MAX_MIDI_SYNTHS
;
118 MIDIOut_NumDevs
+= numDest
;
120 MIDIIn_NumDevs
= MIDIGetNumberOfSources();
122 TRACE("MIDIOut_NumDevs %d MIDIIn_NumDevs %d\n", MIDIOut_NumDevs
, MIDIIn_NumDevs
);
124 destinations
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, MIDIOut_NumDevs
* sizeof(MIDIDestination
));
125 sources
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, MIDIIn_NumDevs
* sizeof(MIDISource
));
127 if (MIDIIn_NumDevs
> 0)
129 InitializeCriticalSection(&midiInLock
);
130 MIDIInThreadPortName
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("MIDIInThreadPortName.%u"), getpid());
131 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
= 16;
195 destinations
[i
].caps
.wNotes
= 16;
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 DeleteCriticalSection(&midiInLock
);
214 if (wineMIDIClient
) MIDIClientDispose(wineMIDIClient
); /* MIDIClientDispose will close all ports */
216 HeapFree(GetProcessHeap(), 0, sources
);
217 HeapFree(GetProcessHeap(), 0, destinations
);
222 /**************************************************************************
223 * MIDI_NotifyClient [internal]
225 static DWORD
MIDI_NotifyClient(UINT wDevID
, WORD wMsg
, DWORD dwParam1
, DWORD dwParam2
)
232 TRACE("wDevID=%d wMsg=%d dwParm1=%04X dwParam2=%04X\n", wDevID
, wMsg
, dwParam1
, dwParam2
);
239 dwCallBack
= destinations
[wDevID
].midiDesc
.dwCallback
;
240 uFlags
= destinations
[wDevID
].wFlags
;
241 hDev
= destinations
[wDevID
].midiDesc
.hMidi
;
242 dwInstance
= destinations
[wDevID
].midiDesc
.dwInstance
;
252 dwCallBack
= sources
[wDevID
].midiDesc
.dwCallback
;
253 uFlags
= sources
[wDevID
].wFlags
;
254 hDev
= sources
[wDevID
].midiDesc
.hMidi
;
255 dwInstance
= sources
[wDevID
].midiDesc
.dwInstance
;
258 WARN("Unsupported MSW-MIDI message %u\n", wMsg
);
259 return MMSYSERR_ERROR
;
262 return DriverCallback(dwCallBack
, uFlags
, hDev
, wMsg
, dwInstance
, dwParam1
, dwParam2
) ?
263 MMSYSERR_NOERROR
: MMSYSERR_ERROR
;
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 return MIDI_NotifyClient(wDevID
, MOM_OPEN
, 0L, 0L);
313 static DWORD
MIDIOut_Close(WORD wDevID
)
315 DWORD ret
= MMSYSERR_NOERROR
;
317 TRACE("wDevID=%d\n", wDevID
);
319 if (wDevID
>= MIDIOut_NumDevs
) {
320 WARN("bad device ID : %d\n", wDevID
);
321 return MMSYSERR_BADDEVICEID
;
324 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
325 SynthUnit_Close(destinations
[wDevID
].graph
);
327 destinations
[wDevID
].graph
= 0;
328 destinations
[wDevID
].synth
= 0;
330 if (MIDI_NotifyClient(wDevID
, MOM_CLOSE
, 0L, 0L) != MMSYSERR_NOERROR
) {
331 WARN("can't notify client !\n");
332 ret
= MMSYSERR_INVALPARAM
;
334 destinations
[wDevID
].midiDesc
.hMidi
= 0;
339 static DWORD
MIDIOut_Data(WORD wDevID
, DWORD dwParam
)
341 WORD evt
= LOBYTE(LOWORD(dwParam
));
342 UInt8 chn
= (evt
& 0x0F);
344 TRACE("wDevID=%d dwParam=%08X\n", wDevID
, dwParam
);
346 if (wDevID
>= MIDIOut_NumDevs
) {
347 WARN("bad device ID : %d\n", wDevID
);
348 return MMSYSERR_BADDEVICEID
;
351 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
353 WORD d1
= HIBYTE(LOWORD(dwParam
));
354 WORD d2
= LOBYTE(HIWORD(dwParam
));
355 OSStatus err
= noErr
;
357 err
= MusicDeviceMIDIEvent(destinations
[wDevID
].synth
, (evt
& 0xF0) | chn
, d1
, d2
, 0);
360 ERR("MusicDeviceMIDIEvent(%p, %04x, %04x, %04x, %d) return %s\n", destinations
[wDevID
].synth
, (evt
& 0xF0) | chn
, d1
, d2
, 0, wine_dbgstr_fourcc(err
));
361 return MMSYSERR_ERROR
;
367 buffer
[0] = (evt
& 0xF0) | chn
;
368 buffer
[1] = HIBYTE(LOWORD(dwParam
));
369 buffer
[2] = LOBYTE(HIWORD(dwParam
));
371 MIDIOut_Send(MIDIOutPort
, destinations
[wDevID
].dest
, buffer
, 3);
374 return MMSYSERR_NOERROR
;
377 static DWORD
MIDIOut_LongData(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
380 OSStatus err
= noErr
;
382 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
384 /* Note: MS doc does not say much about the dwBytesRecorded member of the MIDIHDR structure
385 * but it seems to be used only for midi input.
386 * Taking a look at the WAVEHDR structure (which is quite similar) confirms this assumption.
389 if (wDevID
>= MIDIOut_NumDevs
) {
390 WARN("bad device ID : %d\n", wDevID
);
391 return MMSYSERR_BADDEVICEID
;
394 if (lpMidiHdr
== NULL
) {
395 WARN("Invalid Parameter\n");
396 return MMSYSERR_INVALPARAM
;
399 lpData
= (LPBYTE
) lpMidiHdr
->lpData
;
402 return MIDIERR_UNPREPARED
;
403 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
404 return MIDIERR_UNPREPARED
;
405 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
406 return MIDIERR_STILLPLAYING
;
407 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
408 lpMidiHdr
->dwFlags
|= MHDR_INQUEUE
;
410 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
411 * data, or can it also contain raw MIDI data, to be split up and sent to
413 * If the latest is true, then the following WARNing will fire up
415 if (lpData
[0] != 0xF0 || lpData
[lpMidiHdr
->dwBufferLength
- 1] != 0xF7) {
416 WARN("Alledged system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
419 TRACE("dwBufferLength=%u !\n", lpMidiHdr
->dwBufferLength
);
420 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
421 lpData
[0], lpData
[1], lpData
[2], lpData
[lpMidiHdr
->dwBufferLength
-3],
422 lpData
[lpMidiHdr
->dwBufferLength
-2], lpData
[lpMidiHdr
->dwBufferLength
-1]);
425 if (lpData
[0] != 0xF0) {
426 /* System Exclusive */
427 ERR("Add missing 0xF0 marker at the beginning of system exclusive byte stream\n");
429 if (lpData
[lpMidiHdr
->dwBufferLength
- 1] != 0xF7) {
430 /* Send end of System Exclusive */
431 ERR("Add missing 0xF7 marker at the end of system exclusive byte stream\n");
433 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
) /* FIXME */
435 err
= MusicDeviceSysEx(destinations
[wDevID
].synth
, (const UInt8
*) lpData
, lpMidiHdr
->dwBufferLength
);
438 ERR("MusicDeviceSysEx(%p, %p, %d) return %s\n", destinations
[wDevID
].synth
, lpData
, lpMidiHdr
->dwBufferLength
, wine_dbgstr_fourcc(err
));
439 return MMSYSERR_ERROR
;
444 FIXME("MOD_MIDIPORT\n");
447 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
448 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
449 if (MIDI_NotifyClient(wDevID
, MOM_DONE
, (DWORD
)lpMidiHdr
, 0L) != MMSYSERR_NOERROR
) {
450 WARN("can't notify client !\n");
451 return MMSYSERR_INVALPARAM
;
453 return MMSYSERR_NOERROR
;
456 /**************************************************************************
457 * MIDIOut_Prepare [internal]
459 static DWORD
MIDIOut_Prepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
461 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
463 if (wDevID
>= MIDIOut_NumDevs
) {
464 WARN("bad device ID : %d\n", wDevID
);
465 return MMSYSERR_BADDEVICEID
;
468 /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
469 * asks to prepare MIDIHDR which dwFlags != 0.
470 * So at least check for the inqueue flag
472 if (dwSize
< sizeof(MIDIHDR
) || lpMidiHdr
== 0 ||
473 lpMidiHdr
->lpData
== 0 || (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) != 0) {
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
* 0xFFFF) + ((WORD
) (right
* 0xFFFF) << 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 WARN("Invalid parameter %p %p %08x %d\n", lpMidiHdr
, lpMidiHdr
->lpData
,
725 lpMidiHdr
->dwFlags
, dwSize
);
726 return MMSYSERR_INVALPARAM
;
729 lpMidiHdr
->lpNext
= 0;
730 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
731 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
732 return MMSYSERR_NOERROR
;
735 static DWORD
MIDIIn_Unprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
737 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
738 if (wDevID
>= MIDIIn_NumDevs
) {
739 WARN("bad device ID : %d\n", wDevID
);
740 return MMSYSERR_BADDEVICEID
;
742 if (dwSize
< sizeof(MIDIHDR
) || lpMidiHdr
== 0) {
743 WARN("Invalid Parameter\n");
744 return MMSYSERR_INVALPARAM
;
746 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) {
747 WARN("Still playing\n");
748 return MIDIERR_STILLPLAYING
;
751 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
752 return MMSYSERR_NOERROR
;
755 static DWORD
MIDIIn_GetDevCaps(WORD wDevID
, LPMIDIINCAPSW lpCaps
, DWORD dwSize
)
757 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID
, lpCaps
, dwSize
);
759 if (lpCaps
== NULL
) {
760 WARN("Invalid Parameter\n");
761 return MMSYSERR_INVALPARAM
;
764 if (wDevID
>= MIDIIn_NumDevs
) {
765 WARN("bad device ID : %d\n", wDevID
);
766 return MMSYSERR_BADDEVICEID
;
768 memcpy(lpCaps
, &sources
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
769 return MMSYSERR_NOERROR
;
772 static DWORD
MIDIIn_GetNumDevs(void)
775 return MIDIIn_NumDevs
;
778 static DWORD
MIDIIn_Start(WORD wDevID
)
780 TRACE("%d\n", wDevID
);
782 if (wDevID
>= MIDIIn_NumDevs
) {
783 WARN("bad device ID : %d\n", wDevID
);
784 return MMSYSERR_BADDEVICEID
;
786 sources
[wDevID
].state
= 1;
787 sources
[wDevID
].startTime
= GetTickCount();
788 return MMSYSERR_NOERROR
;
791 static DWORD
MIDIIn_Stop(WORD wDevID
)
793 TRACE("%d\n", wDevID
);
794 if (wDevID
>= MIDIIn_NumDevs
) {
795 WARN("bad device ID : %d\n", wDevID
);
796 return MMSYSERR_BADDEVICEID
;
798 sources
[wDevID
].state
= 0;
799 return MMSYSERR_NOERROR
;
802 static DWORD
MIDIIn_Reset(WORD wDevID
)
804 DWORD dwTime
= GetTickCount();
806 TRACE("%d\n", wDevID
);
807 if (wDevID
>= MIDIIn_NumDevs
) {
808 WARN("bad device ID : %d\n", wDevID
);
809 return MMSYSERR_BADDEVICEID
;
812 EnterCriticalSection(&midiInLock
);
813 while (sources
[wDevID
].lpQueueHdr
) {
814 sources
[wDevID
].lpQueueHdr
->dwFlags
&= ~MHDR_INQUEUE
;
815 sources
[wDevID
].lpQueueHdr
->dwFlags
|= MHDR_DONE
;
816 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
817 if (MIDI_NotifyClient(wDevID
, MIM_LONGDATA
, (DWORD
)sources
[wDevID
].lpQueueHdr
, dwTime
) != MMSYSERR_NOERROR
) {
818 WARN("Couldn't notify client\n");
820 sources
[wDevID
].lpQueueHdr
= (LPMIDIHDR
)sources
[wDevID
].lpQueueHdr
->lpNext
;
822 LeaveCriticalSection(&midiInLock
);
824 return MMSYSERR_NOERROR
;
828 * MIDI In Mach message handling
832 * Call from CoreMIDI IO threaded callback,
833 * we can't call Wine debug channels, critical section or anything using NtCurrentTeb here.
835 void MIDIIn_SendMessage(MIDIMessage msg
)
839 CFMessagePortRef messagePort
;
840 messagePort
= CFMessagePortCreateRemote(kCFAllocatorDefault
, MIDIInThreadPortName
);
842 data
= CFDataCreate(kCFAllocatorDefault
, (UInt8
*) &msg
, sizeof(msg
));
845 CFMessagePortSendRequest(messagePort
, 0, data
, 0.0, 0.0, NULL
, NULL
);
847 CFRelease(messagePort
);
851 static CFDataRef
MIDIIn_MessageHandler(CFMessagePortRef local
, SInt32 msgid
, CFDataRef data
, void *info
)
853 MIDIMessage
*msg
= NULL
;
855 MIDISource
*src
= NULL
;
863 msg
= (MIDIMessage
*) CFDataGetBytePtr(data
);
864 TRACE("devID=%d\n", msg
->devID
);
865 for (i
= 0; i
< msg
->length
; ++i
) {
866 TRACE("%02X ", msg
->data
[i
]);
869 src
= &sources
[msg
->devID
];
872 TRACE("input not started, thrown away\n");
875 /* FIXME skipping SysEx */
876 if (msg
->data
[0] == 0xF0)
878 FIXME("Starting System Exclusive\n");
883 for (i
= 0; i
< msg
->length
; ++i
)
885 if (msg
->data
[i
] == 0xF7)
887 FIXME("Ending System Exclusive\n");
893 EnterCriticalSection(&midiInLock
);
894 currentTime
= GetTickCount() - src
->startTime
;
896 while (pos
< msg
->length
)
899 switch (msg
->data
[pos
] & 0xF0)
902 sendData
= (msg
->data
[pos
] << 0);
908 sendData
= (msg
->data
[pos
+ 1] << 8) | (msg
->data
[pos
] << 0);
912 sendData
= (msg
->data
[pos
+ 2] << 16) |
913 (msg
->data
[pos
+ 1] << 8) |
914 (msg
->data
[pos
] << 0);
918 MIDI_NotifyClient(msg
->devID
, MIM_DATA
, sendData
, currentTime
);
920 LeaveCriticalSection(&midiInLock
);
923 CFRunLoopStop(CFRunLoopGetCurrent());
930 static DWORD WINAPI
MIDIIn_MessageThread(LPVOID p
)
932 CFMessagePortRef local
;
933 CFRunLoopSourceRef source
;
936 local
= CFMessagePortCreateLocal(kCFAllocatorDefault
, MIDIInThreadPortName
, &MIDIIn_MessageHandler
, NULL
, &info
);
938 source
= CFMessagePortCreateRunLoopSource(kCFAllocatorDefault
, local
, (CFIndex
)0);
939 CFRunLoopAddSource(CFRunLoopGetCurrent(), source
, kCFRunLoopDefaultMode
);
943 CFRunLoopSourceInvalidate(source
);
946 CFRelease(MIDIInThreadPortName
);
947 MIDIInThreadPortName
= NULL
;
952 /**************************************************************************
955 DWORD WINAPI
CoreAudio_modMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
, DWORD dwParam1
, DWORD dwParam2
)
957 TRACE("%d %08x %08x %08x %08x\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
966 return MIDIOut_Open(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
968 return MIDIOut_Close(wDevID
);
970 return MIDIOut_Data(wDevID
, dwParam1
);
972 return MIDIOut_LongData(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
974 return MIDIOut_Prepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
976 return MIDIOut_Unprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
977 case MODM_GETDEVCAPS
:
978 return MIDIOut_GetDevCaps(wDevID
, (LPMIDIOUTCAPSW
) dwParam1
, dwParam2
);
979 case MODM_GETNUMDEVS
:
980 return MIDIOut_GetNumDevs();
982 return MIDIOut_GetVolume(wDevID
, (DWORD
*)dwParam1
);
984 return MIDIOut_SetVolume(wDevID
, dwParam1
);
986 return MIDIOut_Reset(wDevID
);
988 TRACE("Unsupported message (08%x)\n", wMsg
);
990 return MMSYSERR_NOTSUPPORTED
;
993 /**************************************************************************
996 DWORD WINAPI
CoreAudio_midMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
, DWORD dwParam1
, DWORD dwParam2
)
998 TRACE("%d %08x %08x %08x %08x\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1006 return MIDIIn_Open(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
1008 return MIDIIn_Close(wDevID
);
1009 case MIDM_ADDBUFFER
:
1010 return MIDIIn_AddBuffer(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1012 return MIDIIn_Prepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1013 case MIDM_UNPREPARE
:
1014 return MIDIIn_Unprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1015 case MIDM_GETDEVCAPS
:
1016 return MIDIIn_GetDevCaps(wDevID
, (LPMIDIINCAPSW
) dwParam1
, dwParam2
);
1017 case MIDM_GETNUMDEVS
:
1018 return MIDIIn_GetNumDevs();
1020 return MIDIIn_Start(wDevID
);
1022 return MIDIIn_Stop(wDevID
);
1024 return MIDIIn_Reset(wDevID
);
1026 TRACE("Unsupported message\n");
1028 return MMSYSERR_NOTSUPPORTED
;
1032 DWORD WINAPI
CoreAudio_modMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
, DWORD dwParam1
, DWORD dwParam2
)
1034 TRACE("%08x, %08x, %08x, %08x, %08x\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1035 return MMSYSERR_NOTENABLED
;
1038 DWORD WINAPI
CoreAudio_midMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
1039 DWORD dwParam1
, DWORD dwParam2
)
1041 TRACE("%08x, %08x, %08x, %08x, %08x\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1042 return MMSYSERR_NOTENABLED
;