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
;
87 static CRITICAL_SECTION midiInLock
; /* Critical section for MIDI In */
88 static CFStringRef MIDIInThreadPortName
= NULL
;
90 static DWORD WINAPI
MIDIIn_MessageThread(LPVOID p
);
92 static MIDIPortRef MIDIInPort
= NULL
;
93 static MIDIPortRef MIDIOutPort
= NULL
;
95 #define MAX_MIDI_SYNTHS 1
97 MIDIDestination
*destinations
;
100 extern int SynthUnit_CreateDefaultSynthUnit(AUGraph
*graph
, AudioUnit
*synth
);
101 extern int SynthUnit_Initialize(AudioUnit synth
, AUGraph graph
);
102 extern int SynthUnit_Close(AUGraph graph
);
105 LONG
CoreAudio_MIDIInit(void)
108 CHAR szPname
[MAXPNAMELEN
] = {0};
110 int numDest
= MIDIGetNumberOfDestinations();
111 CFStringRef name
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("wineMIDIClient.%d"), getpid());
113 wineMIDIClient
= CoreMIDI_CreateClient( name
);
114 if (wineMIDIClient
== NULL
)
117 ERR("can't create wineMIDIClient\n");
122 MIDIOut_NumDevs
= MAX_MIDI_SYNTHS
;
123 MIDIOut_NumDevs
+= numDest
;
125 MIDIIn_NumDevs
= MIDIGetNumberOfSources();
127 TRACE("MIDIOut_NumDevs %d MIDIIn_NumDevs %d\n", MIDIOut_NumDevs
, MIDIIn_NumDevs
);
129 destinations
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, MIDIOut_NumDevs
* sizeof(MIDIDestination
));
130 sources
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, MIDIIn_NumDevs
* sizeof(MIDISource
));
132 if (MIDIIn_NumDevs
> 0)
134 InitializeCriticalSection(&midiInLock
);
135 midiInLock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": midiInLock");
136 MIDIInThreadPortName
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("MIDIInThreadPortName.%u"), getpid());
137 CloseHandle( CreateThread(NULL
, 0, MIDIIn_MessageThread
, NULL
, 0, NULL
));
139 name
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("WineInputPort.%u"), getpid());
140 MIDIInputPortCreate(wineMIDIClient
, name
, MIDIIn_ReadProc
, NULL
, &MIDIInPort
);
145 name
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("WineOutputPort.%u"), getpid());
146 MIDIOutputPortCreate(wineMIDIClient
, name
, &MIDIOutPort
);
150 /* initialize sources */
151 for (i
= 0; i
< MIDIIn_NumDevs
; i
++)
153 sources
[i
].wDevID
= i
;
154 sources
[i
].source
= MIDIGetSource(i
);
156 CoreMIDI_GetObjectName(sources
[i
].source
, szPname
, sizeof(szPname
));
157 MultiByteToWideChar(CP_ACP
, 0, szPname
, -1, sources
[i
].caps
.szPname
, ARRAY_SIZE(sources
[i
].caps
.szPname
));
159 MIDIPortConnectSource(MIDIInPort
, sources
[i
].source
, &sources
[i
].wDevID
);
161 sources
[i
].state
= 0;
163 sources
[i
].caps
.wMid
= 0x00FF; /* Manufac ID */
164 sources
[i
].caps
.wPid
= 0x0001; /* Product ID */
165 sources
[i
].caps
.vDriverVersion
= 0x0001;
166 sources
[i
].caps
.dwSupport
= 0;
169 /* initialise MIDI synths */
170 for (i
= 0; i
< MAX_MIDI_SYNTHS
; i
++)
172 snprintf(szPname
, sizeof(szPname
), "CoreAudio MIDI Synth %d", i
);
173 MultiByteToWideChar(CP_ACP
, 0, szPname
, -1, destinations
[i
].caps
.szPname
, ARRAY_SIZE(destinations
[i
].caps
.szPname
));
175 destinations
[i
].caps
.wTechnology
= MOD_SYNTH
;
176 destinations
[i
].caps
.wChannelMask
= 0xFFFF;
178 destinations
[i
].caps
.wMid
= 0x00FF; /* Manufac ID */
179 destinations
[i
].caps
.wPid
= 0x0001; /* Product ID */
180 destinations
[i
].caps
.vDriverVersion
= 0x0001;
181 destinations
[i
].caps
.dwSupport
= MIDICAPS_VOLUME
;
182 destinations
[i
].caps
.wVoices
= 16;
183 destinations
[i
].caps
.wNotes
= 16;
185 /* initialise available destinations */
186 for (i
= MAX_MIDI_SYNTHS
; i
< numDest
+ MAX_MIDI_SYNTHS
; i
++)
188 destinations
[i
].dest
= MIDIGetDestination(i
- MAX_MIDI_SYNTHS
);
190 CoreMIDI_GetObjectName(destinations
[i
].dest
, szPname
, sizeof(szPname
));
191 MultiByteToWideChar(CP_ACP
, 0, szPname
, -1, destinations
[i
].caps
.szPname
, ARRAY_SIZE(destinations
[i
].caps
.szPname
));
193 destinations
[i
].caps
.wTechnology
= MOD_MIDIPORT
;
194 destinations
[i
].caps
.wChannelMask
= 0xFFFF;
196 destinations
[i
].caps
.wMid
= 0x00FF; /* Manufac ID */
197 destinations
[i
].caps
.wPid
= 0x0001;
198 destinations
[i
].caps
.vDriverVersion
= 0x0001;
199 destinations
[i
].caps
.dwSupport
= 0;
200 destinations
[i
].caps
.wVoices
= 0;
201 destinations
[i
].caps
.wNotes
= 0;
206 LONG
CoreAudio_MIDIRelease(void)
209 if (MIDIIn_NumDevs
> 0)
211 CFMessagePortRef messagePort
;
212 /* Stop CFRunLoop in MIDIIn_MessageThread */
213 messagePort
= CFMessagePortCreateRemote(kCFAllocatorDefault
, MIDIInThreadPortName
);
214 CFMessagePortSendRequest(messagePort
, 1, NULL
, 0.0, 0.0, NULL
, NULL
);
215 CFRelease(messagePort
);
217 midiInLock
.DebugInfo
->Spare
[0] = 0;
218 DeleteCriticalSection(&midiInLock
);
221 if (wineMIDIClient
) MIDIClientDispose(wineMIDIClient
); /* MIDIClientDispose will close all ports */
223 HeapFree(GetProcessHeap(), 0, sources
);
224 HeapFree(GetProcessHeap(), 0, destinations
);
229 /**************************************************************************
230 * MIDI_NotifyClient [internal]
232 static void MIDI_NotifyClient(UINT wDevID
, WORD wMsg
, DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
234 DWORD_PTR dwCallBack
;
237 DWORD_PTR dwInstance
;
239 TRACE("wDevID=%d wMsg=%d dwParm1=%04lX dwParam2=%04lX\n", wDevID
, wMsg
, dwParam1
, dwParam2
);
246 dwCallBack
= destinations
[wDevID
].midiDesc
.dwCallback
;
247 uFlags
= destinations
[wDevID
].wFlags
;
248 hDev
= destinations
[wDevID
].midiDesc
.hMidi
;
249 dwInstance
= destinations
[wDevID
].midiDesc
.dwInstance
;
259 dwCallBack
= sources
[wDevID
].midiDesc
.dwCallback
;
260 uFlags
= sources
[wDevID
].wFlags
;
261 hDev
= sources
[wDevID
].midiDesc
.hMidi
;
262 dwInstance
= sources
[wDevID
].midiDesc
.dwInstance
;
265 ERR("Unsupported MSW-MIDI message %u\n", wMsg
);
269 DriverCallback(dwCallBack
, uFlags
, hDev
, wMsg
, dwInstance
, dwParam1
, dwParam2
);
272 static DWORD
MIDIOut_Open(WORD wDevID
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
274 MIDIDestination
*dest
;
276 TRACE("wDevID=%d lpDesc=%p dwFlags=%08x\n", wDevID
, lpDesc
, dwFlags
);
278 if (lpDesc
== NULL
) {
279 WARN("Invalid Parameter\n");
280 return MMSYSERR_INVALPARAM
;
283 if (wDevID
>= MIDIOut_NumDevs
) {
284 WARN("bad device ID : %d\n", wDevID
);
285 return MMSYSERR_BADDEVICEID
;
288 if (destinations
[wDevID
].midiDesc
.hMidi
!= 0) {
289 WARN("device already open !\n");
290 return MMSYSERR_ALLOCATED
;
293 if ((dwFlags
& ~CALLBACK_TYPEMASK
) != 0) {
294 WARN("bad dwFlags\n");
295 return MMSYSERR_INVALFLAG
;
297 dest
= &destinations
[wDevID
];
299 if (dest
->caps
.wTechnology
== MOD_SYNTH
)
301 if (!SynthUnit_CreateDefaultSynthUnit(&dest
->graph
, &dest
->synth
))
303 ERR("SynthUnit_CreateDefaultSynthUnit dest=%p failed\n", dest
);
304 return MMSYSERR_ERROR
;
307 if (!SynthUnit_Initialize(dest
->synth
, dest
->graph
))
309 ERR("SynthUnit_Initialise dest=%p failed\n", dest
);
310 return MMSYSERR_ERROR
;
313 dest
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
314 dest
->midiDesc
= *lpDesc
;
316 MIDI_NotifyClient(wDevID
, MOM_OPEN
, 0L, 0L);
317 return MMSYSERR_NOERROR
;
320 static DWORD
MIDIOut_Close(WORD wDevID
)
322 DWORD ret
= MMSYSERR_NOERROR
;
324 TRACE("wDevID=%d\n", wDevID
);
326 if (wDevID
>= MIDIOut_NumDevs
) {
327 WARN("bad device ID : %d\n", wDevID
);
328 return MMSYSERR_BADDEVICEID
;
331 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
332 SynthUnit_Close(destinations
[wDevID
].graph
);
334 destinations
[wDevID
].graph
= 0;
335 destinations
[wDevID
].synth
= 0;
337 MIDI_NotifyClient(wDevID
, MOM_CLOSE
, 0L, 0L);
338 destinations
[wDevID
].midiDesc
.hMidi
= 0;
343 static DWORD
MIDIOut_Data(WORD wDevID
, DWORD dwParam
)
345 WORD evt
= LOBYTE(LOWORD(dwParam
));
346 UInt8 chn
= (evt
& 0x0F);
348 TRACE("wDevID=%d dwParam=%08X\n", wDevID
, dwParam
);
350 if (wDevID
>= MIDIOut_NumDevs
) {
351 WARN("bad device ID : %d\n", wDevID
);
352 return MMSYSERR_BADDEVICEID
;
355 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
357 WORD d1
= HIBYTE(LOWORD(dwParam
));
358 WORD d2
= LOBYTE(HIWORD(dwParam
));
359 OSStatus err
= noErr
;
361 err
= MusicDeviceMIDIEvent(destinations
[wDevID
].synth
, (evt
& 0xF0) | chn
, d1
, d2
, 0);
364 ERR("MusicDeviceMIDIEvent(%p, %04x, %04x, %04x, %d) return %s\n", destinations
[wDevID
].synth
, (evt
& 0xF0) | chn
, d1
, d2
, 0, wine_dbgstr_fourcc(err
));
365 return MMSYSERR_ERROR
;
371 buffer
[0] = (evt
& 0xF0) | chn
;
372 buffer
[1] = HIBYTE(LOWORD(dwParam
));
373 buffer
[2] = LOBYTE(HIWORD(dwParam
));
375 MIDIOut_Send(MIDIOutPort
, destinations
[wDevID
].dest
, buffer
, 3);
378 return MMSYSERR_NOERROR
;
381 static DWORD
MIDIOut_LongData(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
384 OSStatus err
= noErr
;
386 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
388 /* Note: MS doc does not say much about the dwBytesRecorded member of the MIDIHDR structure
389 * but it seems to be used only for midi input.
390 * Taking a look at the WAVEHDR structure (which is quite similar) confirms this assumption.
393 if (wDevID
>= MIDIOut_NumDevs
) {
394 WARN("bad device ID : %d\n", wDevID
);
395 return MMSYSERR_BADDEVICEID
;
398 if (lpMidiHdr
== NULL
) {
399 WARN("Invalid Parameter\n");
400 return MMSYSERR_INVALPARAM
;
403 lpData
= (LPBYTE
) lpMidiHdr
->lpData
;
406 return MIDIERR_UNPREPARED
;
407 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
408 return MIDIERR_UNPREPARED
;
409 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
410 return MIDIERR_STILLPLAYING
;
411 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
412 lpMidiHdr
->dwFlags
|= MHDR_INQUEUE
;
414 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
415 * data, or can it also contain raw MIDI data, to be split up and sent to
417 * If the latter is true, then the following WARNing will fire up
419 if (lpData
[0] != 0xF0 || lpData
[lpMidiHdr
->dwBufferLength
- 1] != 0xF7) {
420 WARN("The allegedly system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
423 TRACE("dwBufferLength=%u !\n", lpMidiHdr
->dwBufferLength
);
424 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
425 lpData
[0], lpData
[1], lpData
[2], lpData
[lpMidiHdr
->dwBufferLength
-3],
426 lpData
[lpMidiHdr
->dwBufferLength
-2], lpData
[lpMidiHdr
->dwBufferLength
-1]);
429 if (lpData
[0] != 0xF0) {
430 /* System Exclusive */
431 ERR("Add missing 0xF0 marker at the beginning of system exclusive byte stream\n");
433 if (lpData
[lpMidiHdr
->dwBufferLength
- 1] != 0xF7) {
434 /* Send end of System Exclusive */
435 ERR("Add missing 0xF7 marker at the end of system exclusive byte stream\n");
437 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
) /* FIXME */
439 err
= MusicDeviceSysEx(destinations
[wDevID
].synth
, (const UInt8
*) lpData
, lpMidiHdr
->dwBufferLength
);
442 ERR("MusicDeviceSysEx(%p, %p, %d) return %s\n", destinations
[wDevID
].synth
, lpData
, lpMidiHdr
->dwBufferLength
, wine_dbgstr_fourcc(err
));
443 return MMSYSERR_ERROR
;
446 else if (destinations
[wDevID
].caps
.wTechnology
== MOD_MIDIPORT
) {
447 MIDIOut_Send(MIDIOutPort
, destinations
[wDevID
].dest
, lpData
, lpMidiHdr
->dwBufferLength
);
450 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
451 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
452 MIDI_NotifyClient(wDevID
, MOM_DONE
, (DWORD_PTR
)lpMidiHdr
, 0L);
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 (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
464 return MMSYSERR_INVALPARAM
;
465 if (lpMidiHdr
->dwFlags
& MHDR_PREPARED
)
466 return MMSYSERR_NOERROR
;
468 lpMidiHdr
->lpNext
= 0;
469 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
470 lpMidiHdr
->dwFlags
&= ~(MHDR_DONE
|MHDR_INQUEUE
); /* flags cleared since w2k */
471 return MMSYSERR_NOERROR
;
474 /**************************************************************************
475 * MIDIOut_Unprepare [internal]
477 static DWORD
MIDIOut_Unprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
479 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
481 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
482 return MMSYSERR_INVALPARAM
;
483 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
484 return MMSYSERR_NOERROR
;
485 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
486 return MIDIERR_STILLPLAYING
;
488 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
489 return MMSYSERR_NOERROR
;
492 static DWORD
MIDIOut_GetDevCaps(WORD wDevID
, LPMIDIOUTCAPSW lpCaps
, DWORD dwSize
)
494 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID
, lpCaps
, dwSize
);
496 if (lpCaps
== NULL
) {
497 WARN("Invalid Parameter\n");
498 return MMSYSERR_INVALPARAM
;
501 if (wDevID
>= MIDIOut_NumDevs
) {
502 WARN("bad device ID : %d\n", wDevID
);
503 return MMSYSERR_BADDEVICEID
;
505 memcpy(lpCaps
, &destinations
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
506 return MMSYSERR_NOERROR
;
509 static DWORD
MIDIOut_GetNumDevs(void)
512 return MIDIOut_NumDevs
;
515 static DWORD
MIDIOut_GetVolume(WORD wDevID
, DWORD
*lpdwVolume
)
517 TRACE("%d\n", wDevID
);
519 if (wDevID
>= MIDIOut_NumDevs
) {
520 WARN("bad device ID : %d\n", wDevID
);
521 return MMSYSERR_BADDEVICEID
;
523 if (lpdwVolume
== NULL
) {
524 WARN("Invalid Parameter\n");
525 return MMSYSERR_INVALPARAM
;
528 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
532 AudioUnit_GetVolume(destinations
[wDevID
].synth
, &left
, &right
);
534 *lpdwVolume
= (WORD
) (left
* 0xFFFF) + ((WORD
) (right
* 0xFFFF) << 16);
536 return MMSYSERR_NOERROR
;
539 return MMSYSERR_NOTSUPPORTED
;
542 static DWORD
MIDIOut_SetVolume(WORD wDevID
, DWORD dwVolume
)
544 TRACE("%d\n", wDevID
);
546 if (wDevID
>= MIDIOut_NumDevs
) {
547 WARN("bad device ID : %d\n", wDevID
);
548 return MMSYSERR_BADDEVICEID
;
550 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
555 left
= LOWORD(dwVolume
) / 65535.0f
;
556 right
= HIWORD(dwVolume
) / 65535.0f
;
557 AudioUnit_SetVolume(destinations
[wDevID
].synth
, left
, right
);
559 return MMSYSERR_NOERROR
;
562 return MMSYSERR_NOTSUPPORTED
;
565 static DWORD
MIDIOut_Reset(WORD wDevID
)
569 TRACE("%d\n", wDevID
);
571 if (wDevID
>= MIDIOut_NumDevs
) {
572 WARN("bad device ID : %d\n", wDevID
);
573 return MMSYSERR_BADDEVICEID
;
575 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
577 for (chn
= 0; chn
< 16; chn
++) {
578 /* turn off every note */
579 MusicDeviceMIDIEvent(destinations
[wDevID
].synth
, 0xB0 | chn
, 0x7B, 0, 0);
580 /* remove sustain on channel */
581 MusicDeviceMIDIEvent(destinations
[wDevID
].synth
, 0xB0 | chn
, 0x40, 0, 0);
584 else FIXME("MOD_MIDIPORT\n");
586 /* FIXME: the LongData buffers must also be returned to the app */
587 return MMSYSERR_NOERROR
;
590 static DWORD
MIDIIn_Open(WORD wDevID
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
592 TRACE("wDevID=%d lpDesc=%p dwFlags=%08x\n", wDevID
, lpDesc
, dwFlags
);
594 if (lpDesc
== NULL
) {
595 WARN("Invalid Parameter\n");
596 return MMSYSERR_INVALPARAM
;
598 if (wDevID
>= MIDIIn_NumDevs
) {
599 WARN("bad device ID : %d\n", wDevID
);
600 return MMSYSERR_BADDEVICEID
;
602 if (sources
[wDevID
].midiDesc
.hMidi
!= 0) {
603 WARN("device already open !\n");
604 return MMSYSERR_ALLOCATED
;
606 if ((dwFlags
& MIDI_IO_STATUS
) != 0) {
607 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
608 dwFlags
&= ~MIDI_IO_STATUS
;
610 if ((dwFlags
& ~CALLBACK_TYPEMASK
) != 0) {
611 FIXME("Bad dwFlags\n");
612 return MMSYSERR_INVALFLAG
;
615 sources
[wDevID
].wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
616 sources
[wDevID
].lpQueueHdr
= NULL
;
617 sources
[wDevID
].midiDesc
= *lpDesc
;
618 sources
[wDevID
].startTime
= 0;
619 sources
[wDevID
].state
= 0;
621 MIDI_NotifyClient(wDevID
, MIM_OPEN
, 0L, 0L);
622 return MMSYSERR_NOERROR
;
625 static DWORD
MIDIIn_Close(WORD wDevID
)
627 DWORD ret
= MMSYSERR_NOERROR
;
629 TRACE("wDevID=%d\n", wDevID
);
631 if (wDevID
>= MIDIIn_NumDevs
) {
632 WARN("bad device ID : %d\n", wDevID
);
633 return MMSYSERR_BADDEVICEID
;
636 if (sources
[wDevID
].midiDesc
.hMidi
== 0) {
637 WARN("device not opened !\n");
638 return MMSYSERR_ERROR
;
640 if (sources
[wDevID
].lpQueueHdr
!= 0) {
641 return MIDIERR_STILLPLAYING
;
644 MIDI_NotifyClient(wDevID
, MIM_CLOSE
, 0L, 0L);
645 sources
[wDevID
].midiDesc
.hMidi
= 0;
649 static DWORD
MIDIIn_AddBuffer(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
651 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
653 if (wDevID
>= MIDIIn_NumDevs
) {
654 WARN("bad device ID : %d\n", wDevID
);
655 return MMSYSERR_BADDEVICEID
;
657 if (lpMidiHdr
== NULL
) {
658 WARN("Invalid Parameter\n");
659 return MMSYSERR_INVALPARAM
;
661 if (dwSize
< offsetof(MIDIHDR
,dwOffset
)) {
662 WARN("Invalid Parameter\n");
663 return MMSYSERR_INVALPARAM
;
665 if (lpMidiHdr
->dwBufferLength
== 0) {
666 WARN("Invalid Parameter\n");
667 return MMSYSERR_INVALPARAM
;
669 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) {
670 WARN("Still playing\n");
671 return MIDIERR_STILLPLAYING
;
673 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
)) {
674 WARN("Unprepared\n");
675 return MIDIERR_UNPREPARED
;
678 EnterCriticalSection(&midiInLock
);
679 lpMidiHdr
->dwFlags
&= ~WHDR_DONE
;
680 lpMidiHdr
->dwFlags
|= MHDR_INQUEUE
;
681 lpMidiHdr
->dwBytesRecorded
= 0;
682 lpMidiHdr
->lpNext
= 0;
683 if (sources
[wDevID
].lpQueueHdr
== 0) {
684 sources
[wDevID
].lpQueueHdr
= lpMidiHdr
;
687 for (ptr
= sources
[wDevID
].lpQueueHdr
;
690 ptr
->lpNext
= lpMidiHdr
;
692 LeaveCriticalSection(&midiInLock
);
694 return MMSYSERR_NOERROR
;
697 static DWORD
MIDIIn_Prepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
699 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
701 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
702 return MMSYSERR_INVALPARAM
;
703 if (lpMidiHdr
->dwFlags
& MHDR_PREPARED
)
704 return MMSYSERR_NOERROR
;
706 lpMidiHdr
->lpNext
= 0;
707 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
708 lpMidiHdr
->dwFlags
&= ~(MHDR_DONE
|MHDR_INQUEUE
); /* flags cleared since w2k */
709 return MMSYSERR_NOERROR
;
712 static DWORD
MIDIIn_Unprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
714 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
716 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
717 return MMSYSERR_INVALPARAM
;
718 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
719 return MMSYSERR_NOERROR
;
720 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
721 return MIDIERR_STILLPLAYING
;
723 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
724 return MMSYSERR_NOERROR
;
727 static DWORD
MIDIIn_GetDevCaps(WORD wDevID
, LPMIDIINCAPSW lpCaps
, DWORD dwSize
)
729 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID
, lpCaps
, dwSize
);
731 if (lpCaps
== NULL
) {
732 WARN("Invalid Parameter\n");
733 return MMSYSERR_INVALPARAM
;
736 if (wDevID
>= MIDIIn_NumDevs
) {
737 WARN("bad device ID : %d\n", wDevID
);
738 return MMSYSERR_BADDEVICEID
;
740 memcpy(lpCaps
, &sources
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
741 return MMSYSERR_NOERROR
;
744 static DWORD
MIDIIn_GetNumDevs(void)
747 return MIDIIn_NumDevs
;
750 static DWORD
MIDIIn_Start(WORD wDevID
)
752 TRACE("%d\n", wDevID
);
754 if (wDevID
>= MIDIIn_NumDevs
) {
755 WARN("bad device ID : %d\n", wDevID
);
756 return MMSYSERR_BADDEVICEID
;
758 sources
[wDevID
].state
= 1;
759 sources
[wDevID
].startTime
= GetTickCount();
760 return MMSYSERR_NOERROR
;
763 static DWORD
MIDIIn_Stop(WORD wDevID
)
765 TRACE("%d\n", wDevID
);
766 if (wDevID
>= MIDIIn_NumDevs
) {
767 WARN("bad device ID : %d\n", wDevID
);
768 return MMSYSERR_BADDEVICEID
;
770 sources
[wDevID
].state
= 0;
771 return MMSYSERR_NOERROR
;
774 static DWORD
MIDIIn_Reset(WORD wDevID
)
776 DWORD dwTime
= GetTickCount();
778 TRACE("%d\n", wDevID
);
779 if (wDevID
>= MIDIIn_NumDevs
) {
780 WARN("bad device ID : %d\n", wDevID
);
781 return MMSYSERR_BADDEVICEID
;
784 EnterCriticalSection(&midiInLock
);
785 while (sources
[wDevID
].lpQueueHdr
) {
786 LPMIDIHDR lpMidiHdr
= sources
[wDevID
].lpQueueHdr
;
787 sources
[wDevID
].lpQueueHdr
= lpMidiHdr
->lpNext
;
788 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
789 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
790 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
791 MIDI_NotifyClient(wDevID
, MIM_LONGDATA
, (DWORD_PTR
)lpMidiHdr
, dwTime
);
793 LeaveCriticalSection(&midiInLock
);
795 return MMSYSERR_NOERROR
;
799 * MIDI In Mach message handling
803 * Call from CoreMIDI IO threaded callback,
804 * we can't call Wine debug channels, critical section or anything using NtCurrentTeb here.
806 void MIDIIn_SendMessage(UInt16 devID
, const void *buffer
, UInt16 length
)
809 CFMutableDataRef data
;
811 CFMessagePortRef messagePort
;
812 messagePort
= CFMessagePortCreateRemote(kCFAllocatorDefault
, MIDIInThreadPortName
);
817 data
= CFDataCreateMutable(kCFAllocatorDefault
, sizeof(msg
) + length
);
820 CFDataAppendBytes(data
, (UInt8
*) &msg
, sizeof(msg
));
821 CFDataAppendBytes(data
, buffer
, length
);
822 CFMessagePortSendRequest(messagePort
, 0, data
, 0.0, 0.0, NULL
, NULL
);
825 CFRelease(messagePort
);
828 static CFDataRef
MIDIIn_MessageHandler(CFMessagePortRef local
, SInt32 msgid
, CFDataRef data
, void *info
)
830 MIDIMessage
*msg
= NULL
;
832 MIDISource
*src
= NULL
;
841 msg
= (MIDIMessage
*) CFDataGetBytePtr(data
);
842 TRACE("devID=%d\n", msg
->devID
);
843 for (i
= 0; i
< msg
->length
; ++i
) {
844 TRACE("%02X ", msg
->data
[i
]);
847 src
= &sources
[msg
->devID
];
850 TRACE("input not started, thrown away\n");
854 sysexStart
= (msg
->data
[0] == 0xF0);
856 if (sysexStart
|| src
->state
& 2) {
858 int len
= msg
->length
;
861 TRACE("Receiving sysex message\n");
865 EnterCriticalSection(&midiInLock
);
866 currentTime
= GetTickCount() - src
->startTime
;
869 LPMIDIHDR lpMidiHdr
= src
->lpQueueHdr
;
871 if (lpMidiHdr
!= NULL
) {
872 int copylen
= min(len
, lpMidiHdr
->dwBufferLength
- lpMidiHdr
->dwBytesRecorded
);
873 memcpy(lpMidiHdr
->lpData
+ lpMidiHdr
->dwBytesRecorded
, msg
->data
+ pos
, copylen
);
874 lpMidiHdr
->dwBytesRecorded
+= copylen
;
878 TRACE("Copied %d bytes of sysex message\n", copylen
);
880 if ((lpMidiHdr
->dwBytesRecorded
== lpMidiHdr
->dwBufferLength
) ||
881 (*(BYTE
*)(lpMidiHdr
->lpData
+ lpMidiHdr
->dwBytesRecorded
- 1) == 0xF7)) {
882 TRACE("Sysex message complete (or buffer limit reached), dispatching %d bytes\n", lpMidiHdr
->dwBytesRecorded
);
883 src
->lpQueueHdr
= lpMidiHdr
->lpNext
;
884 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
885 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
886 MIDI_NotifyClient(msg
->devID
, MIM_LONGDATA
, (DWORD_PTR
)lpMidiHdr
, currentTime
);
891 FIXME("Sysex data received but no buffer to store it!\n");
896 LeaveCriticalSection(&midiInLock
);
900 EnterCriticalSection(&midiInLock
);
901 currentTime
= GetTickCount() - src
->startTime
;
903 while (pos
< msg
->length
)
906 switch (msg
->data
[pos
] & 0xF0)
909 sendData
= (msg
->data
[pos
] << 0);
915 sendData
= (msg
->data
[pos
+ 1] << 8) | (msg
->data
[pos
] << 0);
919 sendData
= (msg
->data
[pos
+ 2] << 16) |
920 (msg
->data
[pos
+ 1] << 8) |
921 (msg
->data
[pos
] << 0);
925 MIDI_NotifyClient(msg
->devID
, MIM_DATA
, sendData
, currentTime
);
927 LeaveCriticalSection(&midiInLock
);
930 CFRunLoopStop(CFRunLoopGetCurrent());
936 static DWORD WINAPI
MIDIIn_MessageThread(LPVOID p
)
938 CFMessagePortRef local
;
939 CFRunLoopSourceRef source
;
942 local
= CFMessagePortCreateLocal(kCFAllocatorDefault
, MIDIInThreadPortName
, &MIDIIn_MessageHandler
, NULL
, &info
);
944 source
= CFMessagePortCreateRunLoopSource(kCFAllocatorDefault
, local
, 0);
945 CFRunLoopAddSource(CFRunLoopGetCurrent(), source
, kCFRunLoopDefaultMode
);
949 CFRunLoopSourceInvalidate(source
);
952 CFRelease(MIDIInThreadPortName
);
953 MIDIInThreadPortName
= NULL
;
958 /**************************************************************************
961 DWORD WINAPI
CoreAudio_modMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
, DWORD dwParam1
, DWORD dwParam2
)
963 TRACE("%d %08x %08x %08x %08x\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
972 return MIDIOut_Open(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
974 return MIDIOut_Close(wDevID
);
976 return MIDIOut_Data(wDevID
, dwParam1
);
978 return MIDIOut_LongData(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
980 return MIDIOut_Prepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
982 return MIDIOut_Unprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
983 case MODM_GETDEVCAPS
:
984 return MIDIOut_GetDevCaps(wDevID
, (LPMIDIOUTCAPSW
) dwParam1
, dwParam2
);
985 case MODM_GETNUMDEVS
:
986 return MIDIOut_GetNumDevs();
988 return MIDIOut_GetVolume(wDevID
, (DWORD
*)dwParam1
);
990 return MIDIOut_SetVolume(wDevID
, dwParam1
);
992 return MIDIOut_Reset(wDevID
);
994 TRACE("Unsupported message (08%x)\n", wMsg
);
996 return MMSYSERR_NOTSUPPORTED
;
999 /**************************************************************************
1002 DWORD WINAPI
CoreAudio_midMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
, DWORD dwParam1
, DWORD dwParam2
)
1004 TRACE("%d %08x %08x %08x %08x\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1012 return MIDIIn_Open(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
1014 return MIDIIn_Close(wDevID
);
1015 case MIDM_ADDBUFFER
:
1016 return MIDIIn_AddBuffer(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1018 return MIDIIn_Prepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1019 case MIDM_UNPREPARE
:
1020 return MIDIIn_Unprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1021 case MIDM_GETDEVCAPS
:
1022 return MIDIIn_GetDevCaps(wDevID
, (LPMIDIINCAPSW
) dwParam1
, dwParam2
);
1023 case MIDM_GETNUMDEVS
:
1024 return MIDIIn_GetNumDevs();
1026 return MIDIIn_Start(wDevID
);
1028 return MIDIIn_Stop(wDevID
);
1030 return MIDIIn_Reset(wDevID
);
1032 TRACE("Unsupported message\n");
1034 return MMSYSERR_NOTSUPPORTED
;