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
;
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 (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
459 return MMSYSERR_INVALPARAM
;
460 if (lpMidiHdr
->dwFlags
& MHDR_PREPARED
)
461 return MMSYSERR_NOERROR
;
463 lpMidiHdr
->lpNext
= 0;
464 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
465 lpMidiHdr
->dwFlags
&= ~(MHDR_DONE
|MHDR_INQUEUE
); /* flags cleared since w2k */
466 return MMSYSERR_NOERROR
;
469 /**************************************************************************
470 * MIDIOut_Unprepare [internal]
472 static DWORD
MIDIOut_Unprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
474 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
476 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
477 return MMSYSERR_INVALPARAM
;
478 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
479 return MMSYSERR_NOERROR
;
480 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
481 return MIDIERR_STILLPLAYING
;
483 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
484 return MMSYSERR_NOERROR
;
487 static DWORD
MIDIOut_GetDevCaps(WORD wDevID
, LPMIDIOUTCAPSW lpCaps
, DWORD dwSize
)
489 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID
, lpCaps
, dwSize
);
491 if (lpCaps
== NULL
) {
492 WARN("Invalid Parameter\n");
493 return MMSYSERR_INVALPARAM
;
496 if (wDevID
>= MIDIOut_NumDevs
) {
497 WARN("bad device ID : %d\n", wDevID
);
498 return MMSYSERR_BADDEVICEID
;
500 memcpy(lpCaps
, &destinations
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
501 return MMSYSERR_NOERROR
;
504 static DWORD
MIDIOut_GetNumDevs(void)
507 return MIDIOut_NumDevs
;
510 static DWORD
MIDIOut_GetVolume(WORD wDevID
, DWORD
*lpdwVolume
)
512 TRACE("%d\n", wDevID
);
514 if (wDevID
>= MIDIOut_NumDevs
) {
515 WARN("bad device ID : %d\n", wDevID
);
516 return MMSYSERR_BADDEVICEID
;
518 if (lpdwVolume
== NULL
) {
519 WARN("Invalid Parameter\n");
520 return MMSYSERR_INVALPARAM
;
523 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
527 AudioUnit_GetVolume(destinations
[wDevID
].synth
, &left
, &right
);
529 *lpdwVolume
= (WORD
) (left
* 0xFFFF) + ((WORD
) (right
* 0xFFFF) << 16);
531 return MMSYSERR_NOERROR
;
534 return MMSYSERR_NOTSUPPORTED
;
537 static DWORD
MIDIOut_SetVolume(WORD wDevID
, DWORD dwVolume
)
539 TRACE("%d\n", wDevID
);
541 if (wDevID
>= MIDIOut_NumDevs
) {
542 WARN("bad device ID : %d\n", wDevID
);
543 return MMSYSERR_BADDEVICEID
;
545 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
550 left
= LOWORD(dwVolume
) / 65535.0f
;
551 right
= HIWORD(dwVolume
) / 65535.0f
;
552 AudioUnit_SetVolume(destinations
[wDevID
].synth
, left
, right
);
554 return MMSYSERR_NOERROR
;
557 return MMSYSERR_NOTSUPPORTED
;
560 static DWORD
MIDIOut_Reset(WORD wDevID
)
564 TRACE("%d\n", wDevID
);
566 if (wDevID
>= MIDIOut_NumDevs
) {
567 WARN("bad device ID : %d\n", wDevID
);
568 return MMSYSERR_BADDEVICEID
;
570 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
572 for (chn
= 0; chn
< 16; chn
++) {
573 /* turn off every note */
574 MusicDeviceMIDIEvent(destinations
[wDevID
].synth
, 0xB0 | chn
, 0x7B, 0, 0);
575 /* remove sustain on channel */
576 MusicDeviceMIDIEvent(destinations
[wDevID
].synth
, 0xB0 | chn
, 0x40, 0, 0);
579 else FIXME("MOD_MIDIPORT\n");
581 /* FIXME: the LongData buffers must also be returned to the app */
582 return MMSYSERR_NOERROR
;
585 static DWORD
MIDIIn_Open(WORD wDevID
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
587 TRACE("wDevID=%d lpDesc=%p dwFlags=%08x\n", wDevID
, lpDesc
, dwFlags
);
589 if (lpDesc
== NULL
) {
590 WARN("Invalid Parameter\n");
591 return MMSYSERR_INVALPARAM
;
593 if (wDevID
>= MIDIIn_NumDevs
) {
594 WARN("bad device ID : %d\n", wDevID
);
595 return MMSYSERR_BADDEVICEID
;
597 if (sources
[wDevID
].midiDesc
.hMidi
!= 0) {
598 WARN("device already open !\n");
599 return MMSYSERR_ALLOCATED
;
601 if ((dwFlags
& MIDI_IO_STATUS
) != 0) {
602 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
603 dwFlags
&= ~MIDI_IO_STATUS
;
605 if ((dwFlags
& ~CALLBACK_TYPEMASK
) != 0) {
606 FIXME("Bad dwFlags\n");
607 return MMSYSERR_INVALFLAG
;
610 sources
[wDevID
].wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
611 sources
[wDevID
].lpQueueHdr
= NULL
;
612 sources
[wDevID
].midiDesc
= *lpDesc
;
613 sources
[wDevID
].startTime
= 0;
614 sources
[wDevID
].state
= 0;
616 MIDI_NotifyClient(wDevID
, MIM_OPEN
, 0L, 0L);
617 return MMSYSERR_NOERROR
;
620 static DWORD
MIDIIn_Close(WORD wDevID
)
622 DWORD ret
= MMSYSERR_NOERROR
;
624 TRACE("wDevID=%d\n", wDevID
);
626 if (wDevID
>= MIDIIn_NumDevs
) {
627 WARN("bad device ID : %d\n", wDevID
);
628 return MMSYSERR_BADDEVICEID
;
631 if (sources
[wDevID
].midiDesc
.hMidi
== 0) {
632 WARN("device not opened !\n");
633 return MMSYSERR_ERROR
;
635 if (sources
[wDevID
].lpQueueHdr
!= 0) {
636 return MIDIERR_STILLPLAYING
;
639 MIDI_NotifyClient(wDevID
, MIM_CLOSE
, 0L, 0L);
640 sources
[wDevID
].midiDesc
.hMidi
= 0;
644 static DWORD
MIDIIn_AddBuffer(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
646 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
648 if (wDevID
>= MIDIIn_NumDevs
) {
649 WARN("bad device ID : %d\n", wDevID
);
650 return MMSYSERR_BADDEVICEID
;
652 if (lpMidiHdr
== NULL
) {
653 WARN("Invalid Parameter\n");
654 return MMSYSERR_INVALPARAM
;
656 if (dwSize
< offsetof(MIDIHDR
,dwOffset
)) {
657 WARN("Invalid Parameter\n");
658 return MMSYSERR_INVALPARAM
;
660 if (lpMidiHdr
->dwBufferLength
== 0) {
661 WARN("Invalid Parameter\n");
662 return MMSYSERR_INVALPARAM
;
664 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) {
665 WARN("Still playing\n");
666 return MIDIERR_STILLPLAYING
;
668 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
)) {
669 WARN("Unprepared\n");
670 return MIDIERR_UNPREPARED
;
673 EnterCriticalSection(&midiInLock
);
674 lpMidiHdr
->dwFlags
&= ~WHDR_DONE
;
675 lpMidiHdr
->dwFlags
|= MHDR_INQUEUE
;
676 lpMidiHdr
->dwBytesRecorded
= 0;
677 lpMidiHdr
->lpNext
= 0;
678 if (sources
[wDevID
].lpQueueHdr
== 0) {
679 sources
[wDevID
].lpQueueHdr
= lpMidiHdr
;
682 for (ptr
= sources
[wDevID
].lpQueueHdr
;
685 ptr
->lpNext
= lpMidiHdr
;
687 LeaveCriticalSection(&midiInLock
);
689 return MMSYSERR_NOERROR
;
692 static DWORD
MIDIIn_Prepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
694 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
696 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
697 return MMSYSERR_INVALPARAM
;
698 if (lpMidiHdr
->dwFlags
& MHDR_PREPARED
)
699 return MMSYSERR_NOERROR
;
701 lpMidiHdr
->lpNext
= 0;
702 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
703 lpMidiHdr
->dwFlags
&= ~(MHDR_DONE
|MHDR_INQUEUE
); /* flags cleared since w2k */
704 return MMSYSERR_NOERROR
;
707 static DWORD
MIDIIn_Unprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
709 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
711 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
712 return MMSYSERR_INVALPARAM
;
713 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
714 return MMSYSERR_NOERROR
;
715 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
716 return MIDIERR_STILLPLAYING
;
718 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
719 return MMSYSERR_NOERROR
;
722 static DWORD
MIDIIn_GetDevCaps(WORD wDevID
, LPMIDIINCAPSW lpCaps
, DWORD dwSize
)
724 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID
, lpCaps
, dwSize
);
726 if (lpCaps
== NULL
) {
727 WARN("Invalid Parameter\n");
728 return MMSYSERR_INVALPARAM
;
731 if (wDevID
>= MIDIIn_NumDevs
) {
732 WARN("bad device ID : %d\n", wDevID
);
733 return MMSYSERR_BADDEVICEID
;
735 memcpy(lpCaps
, &sources
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
736 return MMSYSERR_NOERROR
;
739 static DWORD
MIDIIn_GetNumDevs(void)
742 return MIDIIn_NumDevs
;
745 static DWORD
MIDIIn_Start(WORD wDevID
)
747 TRACE("%d\n", wDevID
);
749 if (wDevID
>= MIDIIn_NumDevs
) {
750 WARN("bad device ID : %d\n", wDevID
);
751 return MMSYSERR_BADDEVICEID
;
753 sources
[wDevID
].state
= 1;
754 sources
[wDevID
].startTime
= GetTickCount();
755 return MMSYSERR_NOERROR
;
758 static DWORD
MIDIIn_Stop(WORD wDevID
)
760 TRACE("%d\n", wDevID
);
761 if (wDevID
>= MIDIIn_NumDevs
) {
762 WARN("bad device ID : %d\n", wDevID
);
763 return MMSYSERR_BADDEVICEID
;
765 sources
[wDevID
].state
= 0;
766 return MMSYSERR_NOERROR
;
769 static DWORD
MIDIIn_Reset(WORD wDevID
)
771 DWORD dwTime
= GetTickCount();
773 TRACE("%d\n", wDevID
);
774 if (wDevID
>= MIDIIn_NumDevs
) {
775 WARN("bad device ID : %d\n", wDevID
);
776 return MMSYSERR_BADDEVICEID
;
779 EnterCriticalSection(&midiInLock
);
780 while (sources
[wDevID
].lpQueueHdr
) {
781 LPMIDIHDR lpMidiHdr
= sources
[wDevID
].lpQueueHdr
;
782 sources
[wDevID
].lpQueueHdr
= lpMidiHdr
->lpNext
;
783 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
784 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
785 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
786 MIDI_NotifyClient(wDevID
, MIM_LONGDATA
, (DWORD_PTR
)lpMidiHdr
, dwTime
);
788 LeaveCriticalSection(&midiInLock
);
790 return MMSYSERR_NOERROR
;
794 * MIDI In Mach message handling
798 * Call from CoreMIDI IO threaded callback,
799 * we can't call Wine debug channels, critical section or anything using NtCurrentTeb here.
801 void MIDIIn_SendMessage(MIDIMessage msg
)
805 CFMessagePortRef messagePort
;
806 messagePort
= CFMessagePortCreateRemote(kCFAllocatorDefault
, MIDIInThreadPortName
);
808 data
= CFDataCreate(kCFAllocatorDefault
, (UInt8
*) &msg
, sizeof(msg
));
811 CFMessagePortSendRequest(messagePort
, 0, data
, 0.0, 0.0, NULL
, NULL
);
814 CFRelease(messagePort
);
817 static CFDataRef
MIDIIn_MessageHandler(CFMessagePortRef local
, SInt32 msgid
, CFDataRef data
, void *info
)
819 MIDIMessage
*msg
= NULL
;
821 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");
841 /* FIXME skipping SysEx */
842 if (msg
->data
[0] == 0xF0)
844 FIXME("Starting System Exclusive\n");
849 for (i
= 0; i
< msg
->length
; ++i
)
851 if (msg
->data
[i
] == 0xF7)
853 FIXME("Ending System Exclusive\n");
859 EnterCriticalSection(&midiInLock
);
860 currentTime
= GetTickCount() - src
->startTime
;
862 while (pos
< msg
->length
)
865 switch (msg
->data
[pos
] & 0xF0)
868 sendData
= (msg
->data
[pos
] << 0);
874 sendData
= (msg
->data
[pos
+ 1] << 8) | (msg
->data
[pos
] << 0);
878 sendData
= (msg
->data
[pos
+ 2] << 16) |
879 (msg
->data
[pos
+ 1] << 8) |
880 (msg
->data
[pos
] << 0);
884 MIDI_NotifyClient(msg
->devID
, MIM_DATA
, sendData
, currentTime
);
886 LeaveCriticalSection(&midiInLock
);
889 CFRunLoopStop(CFRunLoopGetCurrent());
896 static DWORD WINAPI
MIDIIn_MessageThread(LPVOID p
)
898 CFMessagePortRef local
;
899 CFRunLoopSourceRef source
;
902 local
= CFMessagePortCreateLocal(kCFAllocatorDefault
, MIDIInThreadPortName
, &MIDIIn_MessageHandler
, NULL
, &info
);
904 source
= CFMessagePortCreateRunLoopSource(kCFAllocatorDefault
, local
, 0);
905 CFRunLoopAddSource(CFRunLoopGetCurrent(), source
, kCFRunLoopDefaultMode
);
909 CFRunLoopSourceInvalidate(source
);
912 CFRelease(MIDIInThreadPortName
);
913 MIDIInThreadPortName
= NULL
;
918 /**************************************************************************
921 DWORD WINAPI
CoreAudio_modMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
, DWORD dwParam1
, DWORD dwParam2
)
923 TRACE("%d %08x %08x %08x %08x\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
932 return MIDIOut_Open(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
934 return MIDIOut_Close(wDevID
);
936 return MIDIOut_Data(wDevID
, dwParam1
);
938 return MIDIOut_LongData(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
940 return MIDIOut_Prepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
942 return MIDIOut_Unprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
943 case MODM_GETDEVCAPS
:
944 return MIDIOut_GetDevCaps(wDevID
, (LPMIDIOUTCAPSW
) dwParam1
, dwParam2
);
945 case MODM_GETNUMDEVS
:
946 return MIDIOut_GetNumDevs();
948 return MIDIOut_GetVolume(wDevID
, (DWORD
*)dwParam1
);
950 return MIDIOut_SetVolume(wDevID
, dwParam1
);
952 return MIDIOut_Reset(wDevID
);
954 TRACE("Unsupported message (08%x)\n", wMsg
);
956 return MMSYSERR_NOTSUPPORTED
;
959 /**************************************************************************
962 DWORD WINAPI
CoreAudio_midMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
, DWORD dwParam1
, DWORD dwParam2
)
964 TRACE("%d %08x %08x %08x %08x\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
972 return MIDIIn_Open(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
974 return MIDIIn_Close(wDevID
);
976 return MIDIIn_AddBuffer(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
978 return MIDIIn_Prepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
980 return MIDIIn_Unprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
981 case MIDM_GETDEVCAPS
:
982 return MIDIIn_GetDevCaps(wDevID
, (LPMIDIINCAPSW
) dwParam1
, dwParam2
);
983 case MIDM_GETNUMDEVS
:
984 return MIDIIn_GetNumDevs();
986 return MIDIIn_Start(wDevID
);
988 return MIDIIn_Stop(wDevID
);
990 return MIDIIn_Reset(wDevID
);
992 TRACE("Unsupported message\n");
994 return MMSYSERR_NOTSUPPORTED
;