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
)
228 DWORD_PTR dwCallBack
;
231 DWORD_PTR dwInstance
;
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 latter is true, then the following WARNing will fire up
413 if (lpData
[0] != 0xF0 || lpData
[lpMidiHdr
->dwBufferLength
- 1] != 0xF7) {
414 WARN("The allegedly 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
;
440 else if (destinations
[wDevID
].caps
.wTechnology
== MOD_MIDIPORT
) {
441 MIDIOut_Send(MIDIOutPort
, destinations
[wDevID
].dest
, lpData
, lpMidiHdr
->dwBufferLength
);
444 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
445 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
446 MIDI_NotifyClient(wDevID
, MOM_DONE
, (DWORD_PTR
)lpMidiHdr
, 0L);
447 return MMSYSERR_NOERROR
;
450 /**************************************************************************
451 * MIDIOut_Prepare [internal]
453 static DWORD
MIDIOut_Prepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
455 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
457 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
458 return MMSYSERR_INVALPARAM
;
459 if (lpMidiHdr
->dwFlags
& MHDR_PREPARED
)
460 return MMSYSERR_NOERROR
;
462 lpMidiHdr
->lpNext
= 0;
463 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
464 lpMidiHdr
->dwFlags
&= ~(MHDR_DONE
|MHDR_INQUEUE
); /* flags cleared since w2k */
465 return MMSYSERR_NOERROR
;
468 /**************************************************************************
469 * MIDIOut_Unprepare [internal]
471 static DWORD
MIDIOut_Unprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
473 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
475 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
476 return MMSYSERR_INVALPARAM
;
477 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
478 return MMSYSERR_NOERROR
;
479 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
480 return MIDIERR_STILLPLAYING
;
482 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
483 return MMSYSERR_NOERROR
;
486 static DWORD
MIDIOut_GetDevCaps(WORD wDevID
, LPMIDIOUTCAPSW lpCaps
, DWORD dwSize
)
488 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID
, lpCaps
, dwSize
);
490 if (lpCaps
== NULL
) {
491 WARN("Invalid Parameter\n");
492 return MMSYSERR_INVALPARAM
;
495 if (wDevID
>= MIDIOut_NumDevs
) {
496 WARN("bad device ID : %d\n", wDevID
);
497 return MMSYSERR_BADDEVICEID
;
499 memcpy(lpCaps
, &destinations
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
500 return MMSYSERR_NOERROR
;
503 static DWORD
MIDIOut_GetNumDevs(void)
506 return MIDIOut_NumDevs
;
509 static DWORD
MIDIOut_GetVolume(WORD wDevID
, DWORD
*lpdwVolume
)
511 TRACE("%d\n", wDevID
);
513 if (wDevID
>= MIDIOut_NumDevs
) {
514 WARN("bad device ID : %d\n", wDevID
);
515 return MMSYSERR_BADDEVICEID
;
517 if (lpdwVolume
== NULL
) {
518 WARN("Invalid Parameter\n");
519 return MMSYSERR_INVALPARAM
;
522 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
526 AudioUnit_GetVolume(destinations
[wDevID
].synth
, &left
, &right
);
528 *lpdwVolume
= (WORD
) (left
* 0xFFFF) + ((WORD
) (right
* 0xFFFF) << 16);
530 return MMSYSERR_NOERROR
;
533 return MMSYSERR_NOTSUPPORTED
;
536 static DWORD
MIDIOut_SetVolume(WORD wDevID
, DWORD dwVolume
)
538 TRACE("%d\n", wDevID
);
540 if (wDevID
>= MIDIOut_NumDevs
) {
541 WARN("bad device ID : %d\n", wDevID
);
542 return MMSYSERR_BADDEVICEID
;
544 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
549 left
= LOWORD(dwVolume
) / 65535.0f
;
550 right
= HIWORD(dwVolume
) / 65535.0f
;
551 AudioUnit_SetVolume(destinations
[wDevID
].synth
, left
, right
);
553 return MMSYSERR_NOERROR
;
556 return MMSYSERR_NOTSUPPORTED
;
559 static DWORD
MIDIOut_Reset(WORD wDevID
)
563 TRACE("%d\n", wDevID
);
565 if (wDevID
>= MIDIOut_NumDevs
) {
566 WARN("bad device ID : %d\n", wDevID
);
567 return MMSYSERR_BADDEVICEID
;
569 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
571 for (chn
= 0; chn
< 16; chn
++) {
572 /* turn off every note */
573 MusicDeviceMIDIEvent(destinations
[wDevID
].synth
, 0xB0 | chn
, 0x7B, 0, 0);
574 /* remove sustain on channel */
575 MusicDeviceMIDIEvent(destinations
[wDevID
].synth
, 0xB0 | chn
, 0x40, 0, 0);
578 else FIXME("MOD_MIDIPORT\n");
580 /* FIXME: the LongData buffers must also be returned to the app */
581 return MMSYSERR_NOERROR
;
584 static DWORD
MIDIIn_Open(WORD wDevID
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
586 TRACE("wDevID=%d lpDesc=%p dwFlags=%08x\n", wDevID
, lpDesc
, dwFlags
);
588 if (lpDesc
== NULL
) {
589 WARN("Invalid Parameter\n");
590 return MMSYSERR_INVALPARAM
;
592 if (wDevID
>= MIDIIn_NumDevs
) {
593 WARN("bad device ID : %d\n", wDevID
);
594 return MMSYSERR_BADDEVICEID
;
596 if (sources
[wDevID
].midiDesc
.hMidi
!= 0) {
597 WARN("device already open !\n");
598 return MMSYSERR_ALLOCATED
;
600 if ((dwFlags
& MIDI_IO_STATUS
) != 0) {
601 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
602 dwFlags
&= ~MIDI_IO_STATUS
;
604 if ((dwFlags
& ~CALLBACK_TYPEMASK
) != 0) {
605 FIXME("Bad dwFlags\n");
606 return MMSYSERR_INVALFLAG
;
609 sources
[wDevID
].wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
610 sources
[wDevID
].lpQueueHdr
= NULL
;
611 sources
[wDevID
].midiDesc
= *lpDesc
;
612 sources
[wDevID
].startTime
= 0;
613 sources
[wDevID
].state
= 0;
615 MIDI_NotifyClient(wDevID
, MIM_OPEN
, 0L, 0L);
616 return MMSYSERR_NOERROR
;
619 static DWORD
MIDIIn_Close(WORD wDevID
)
621 DWORD ret
= MMSYSERR_NOERROR
;
623 TRACE("wDevID=%d\n", wDevID
);
625 if (wDevID
>= MIDIIn_NumDevs
) {
626 WARN("bad device ID : %d\n", wDevID
);
627 return MMSYSERR_BADDEVICEID
;
630 if (sources
[wDevID
].midiDesc
.hMidi
== 0) {
631 WARN("device not opened !\n");
632 return MMSYSERR_ERROR
;
634 if (sources
[wDevID
].lpQueueHdr
!= 0) {
635 return MIDIERR_STILLPLAYING
;
638 MIDI_NotifyClient(wDevID
, MIM_CLOSE
, 0L, 0L);
639 sources
[wDevID
].midiDesc
.hMidi
= 0;
643 static DWORD
MIDIIn_AddBuffer(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
645 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
647 if (wDevID
>= MIDIIn_NumDevs
) {
648 WARN("bad device ID : %d\n", wDevID
);
649 return MMSYSERR_BADDEVICEID
;
651 if (lpMidiHdr
== NULL
) {
652 WARN("Invalid Parameter\n");
653 return MMSYSERR_INVALPARAM
;
655 if (dwSize
< offsetof(MIDIHDR
,dwOffset
)) {
656 WARN("Invalid Parameter\n");
657 return MMSYSERR_INVALPARAM
;
659 if (lpMidiHdr
->dwBufferLength
== 0) {
660 WARN("Invalid Parameter\n");
661 return MMSYSERR_INVALPARAM
;
663 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) {
664 WARN("Still playing\n");
665 return MIDIERR_STILLPLAYING
;
667 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
)) {
668 WARN("Unprepared\n");
669 return MIDIERR_UNPREPARED
;
672 EnterCriticalSection(&midiInLock
);
673 lpMidiHdr
->dwFlags
&= ~WHDR_DONE
;
674 lpMidiHdr
->dwFlags
|= MHDR_INQUEUE
;
675 lpMidiHdr
->dwBytesRecorded
= 0;
676 lpMidiHdr
->lpNext
= 0;
677 if (sources
[wDevID
].lpQueueHdr
== 0) {
678 sources
[wDevID
].lpQueueHdr
= lpMidiHdr
;
681 for (ptr
= sources
[wDevID
].lpQueueHdr
;
684 ptr
->lpNext
= lpMidiHdr
;
686 LeaveCriticalSection(&midiInLock
);
688 return MMSYSERR_NOERROR
;
691 static DWORD
MIDIIn_Prepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
693 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
695 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
696 return MMSYSERR_INVALPARAM
;
697 if (lpMidiHdr
->dwFlags
& MHDR_PREPARED
)
698 return MMSYSERR_NOERROR
;
700 lpMidiHdr
->lpNext
= 0;
701 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
702 lpMidiHdr
->dwFlags
&= ~(MHDR_DONE
|MHDR_INQUEUE
); /* flags cleared since w2k */
703 return MMSYSERR_NOERROR
;
706 static DWORD
MIDIIn_Unprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
708 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
710 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
711 return MMSYSERR_INVALPARAM
;
712 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
713 return MMSYSERR_NOERROR
;
714 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
715 return MIDIERR_STILLPLAYING
;
717 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
718 return MMSYSERR_NOERROR
;
721 static DWORD
MIDIIn_GetDevCaps(WORD wDevID
, LPMIDIINCAPSW lpCaps
, DWORD dwSize
)
723 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID
, lpCaps
, dwSize
);
725 if (lpCaps
== NULL
) {
726 WARN("Invalid Parameter\n");
727 return MMSYSERR_INVALPARAM
;
730 if (wDevID
>= MIDIIn_NumDevs
) {
731 WARN("bad device ID : %d\n", wDevID
);
732 return MMSYSERR_BADDEVICEID
;
734 memcpy(lpCaps
, &sources
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
735 return MMSYSERR_NOERROR
;
738 static DWORD
MIDIIn_GetNumDevs(void)
741 return MIDIIn_NumDevs
;
744 static DWORD
MIDIIn_Start(WORD wDevID
)
746 TRACE("%d\n", wDevID
);
748 if (wDevID
>= MIDIIn_NumDevs
) {
749 WARN("bad device ID : %d\n", wDevID
);
750 return MMSYSERR_BADDEVICEID
;
752 sources
[wDevID
].state
= 1;
753 sources
[wDevID
].startTime
= GetTickCount();
754 return MMSYSERR_NOERROR
;
757 static DWORD
MIDIIn_Stop(WORD wDevID
)
759 TRACE("%d\n", wDevID
);
760 if (wDevID
>= MIDIIn_NumDevs
) {
761 WARN("bad device ID : %d\n", wDevID
);
762 return MMSYSERR_BADDEVICEID
;
764 sources
[wDevID
].state
= 0;
765 return MMSYSERR_NOERROR
;
768 static DWORD
MIDIIn_Reset(WORD wDevID
)
770 DWORD dwTime
= GetTickCount();
772 TRACE("%d\n", wDevID
);
773 if (wDevID
>= MIDIIn_NumDevs
) {
774 WARN("bad device ID : %d\n", wDevID
);
775 return MMSYSERR_BADDEVICEID
;
778 EnterCriticalSection(&midiInLock
);
779 while (sources
[wDevID
].lpQueueHdr
) {
780 LPMIDIHDR lpMidiHdr
= sources
[wDevID
].lpQueueHdr
;
781 sources
[wDevID
].lpQueueHdr
= lpMidiHdr
->lpNext
;
782 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
783 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
784 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
785 MIDI_NotifyClient(wDevID
, MIM_LONGDATA
, (DWORD_PTR
)lpMidiHdr
, dwTime
);
787 LeaveCriticalSection(&midiInLock
);
789 return MMSYSERR_NOERROR
;
793 * MIDI In Mach message handling
797 * Call from CoreMIDI IO threaded callback,
798 * we can't call Wine debug channels, critical section or anything using NtCurrentTeb here.
800 void MIDIIn_SendMessage(MIDIMessage msg
)
804 CFMessagePortRef messagePort
;
805 messagePort
= CFMessagePortCreateRemote(kCFAllocatorDefault
, MIDIInThreadPortName
);
807 data
= CFDataCreate(kCFAllocatorDefault
, (UInt8
*) &msg
, sizeof(msg
));
810 CFMessagePortSendRequest(messagePort
, 0, data
, 0.0, 0.0, NULL
, NULL
);
813 CFRelease(messagePort
);
816 static CFDataRef
MIDIIn_MessageHandler(CFMessagePortRef local
, SInt32 msgid
, CFDataRef data
, void *info
)
818 MIDIMessage
*msg
= NULL
;
820 MIDISource
*src
= NULL
;
829 msg
= (MIDIMessage
*) CFDataGetBytePtr(data
);
830 TRACE("devID=%d\n", msg
->devID
);
831 for (i
= 0; i
< msg
->length
; ++i
) {
832 TRACE("%02X ", msg
->data
[i
]);
835 src
= &sources
[msg
->devID
];
838 TRACE("input not started, thrown away\n");
842 sysexStart
= (msg
->data
[0] == 0xF0);
844 if (sysexStart
|| src
->state
& 2) {
846 int len
= msg
->length
;
849 TRACE("Receiving sysex message\n");
853 EnterCriticalSection(&midiInLock
);
854 currentTime
= GetTickCount() - src
->startTime
;
857 LPMIDIHDR lpMidiHdr
= src
->lpQueueHdr
;
859 if (lpMidiHdr
!= NULL
) {
860 int copylen
= min(len
, lpMidiHdr
->dwBufferLength
- lpMidiHdr
->dwBytesRecorded
);
861 memcpy(lpMidiHdr
->lpData
+ lpMidiHdr
->dwBytesRecorded
, msg
->data
+ pos
, copylen
);
862 lpMidiHdr
->dwBytesRecorded
+= copylen
;
866 TRACE("Copied %d bytes of sysex message\n", copylen
);
868 if ((lpMidiHdr
->dwBytesRecorded
== lpMidiHdr
->dwBufferLength
) ||
869 (*(BYTE
*)(lpMidiHdr
->lpData
+ lpMidiHdr
->dwBytesRecorded
- 1) == 0xF7)) {
870 TRACE("Sysex message complete (or buffer limit reached), dispatching %d bytes\n", lpMidiHdr
->dwBytesRecorded
);
871 src
->lpQueueHdr
= lpMidiHdr
->lpNext
;
872 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
873 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
874 MIDI_NotifyClient(msg
->devID
, MIM_LONGDATA
, (DWORD_PTR
)lpMidiHdr
, currentTime
);
879 FIXME("Sysex data received but no buffer to store it!\n");
884 LeaveCriticalSection(&midiInLock
);
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());
924 static DWORD WINAPI
MIDIIn_MessageThread(LPVOID p
)
926 CFMessagePortRef local
;
927 CFRunLoopSourceRef source
;
930 local
= CFMessagePortCreateLocal(kCFAllocatorDefault
, MIDIInThreadPortName
, &MIDIIn_MessageHandler
, NULL
, &info
);
932 source
= CFMessagePortCreateRunLoopSource(kCFAllocatorDefault
, local
, 0);
933 CFRunLoopAddSource(CFRunLoopGetCurrent(), source
, kCFRunLoopDefaultMode
);
937 CFRunLoopSourceInvalidate(source
);
940 CFRelease(MIDIInThreadPortName
);
941 MIDIInThreadPortName
= NULL
;
946 /**************************************************************************
949 DWORD WINAPI
CoreAudio_modMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
, DWORD dwParam1
, DWORD dwParam2
)
951 TRACE("%d %08x %08x %08x %08x\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
960 return MIDIOut_Open(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
962 return MIDIOut_Close(wDevID
);
964 return MIDIOut_Data(wDevID
, dwParam1
);
966 return MIDIOut_LongData(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
968 return MIDIOut_Prepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
970 return MIDIOut_Unprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
971 case MODM_GETDEVCAPS
:
972 return MIDIOut_GetDevCaps(wDevID
, (LPMIDIOUTCAPSW
) dwParam1
, dwParam2
);
973 case MODM_GETNUMDEVS
:
974 return MIDIOut_GetNumDevs();
976 return MIDIOut_GetVolume(wDevID
, (DWORD
*)dwParam1
);
978 return MIDIOut_SetVolume(wDevID
, dwParam1
);
980 return MIDIOut_Reset(wDevID
);
982 TRACE("Unsupported message (08%x)\n", wMsg
);
984 return MMSYSERR_NOTSUPPORTED
;
987 /**************************************************************************
990 DWORD WINAPI
CoreAudio_midMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
, DWORD dwParam1
, DWORD dwParam2
)
992 TRACE("%d %08x %08x %08x %08x\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1000 return MIDIIn_Open(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
1002 return MIDIIn_Close(wDevID
);
1003 case MIDM_ADDBUFFER
:
1004 return MIDIIn_AddBuffer(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1006 return MIDIIn_Prepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1007 case MIDM_UNPREPARE
:
1008 return MIDIIn_Unprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1009 case MIDM_GETDEVCAPS
:
1010 return MIDIIn_GetDevCaps(wDevID
, (LPMIDIINCAPSW
) dwParam1
, dwParam2
);
1011 case MIDM_GETNUMDEVS
:
1012 return MIDIIn_GetNumDevs();
1014 return MIDIIn_Start(wDevID
);
1016 return MIDIIn_Stop(wDevID
);
1018 return MIDIIn_Reset(wDevID
);
1020 TRACE("Unsupported message\n");
1022 return MMSYSERR_NOTSUPPORTED
;