2 * Sample MIDI Wine Driver for MacOSX (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 splitted 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"
44 WINE_DEFAULT_DEBUG_CHANNEL(midi
);
46 #if defined(HAVE_COREAUDIO_COREAUDIO_H)
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 */
64 MIDIOPENDESC midiDesc
;
68 typedef struct tagMIDISource
{
71 int state
; /* 0 is no recording started, 1 in recording, bit 2 set if in sys exclusive recording */
73 MIDIOPENDESC midiDesc
;
79 static CRITICAL_SECTION midiInLock
; /* Critical section for MIDI In */
80 static CFStringRef MIDIInThreadPortName
= NULL
;
82 static DWORD WINAPI
MIDIIn_MessageThread(LPVOID p
);
84 #define MAX_MIDI_SYNTHS 1
86 MIDIDestination
*destinations
;
89 extern int SynthUnit_CreateDefaultSynthUnit(AUGraph
*graph
, AudioUnit
*synth
);
90 extern int SynthUnit_Initialize(AudioUnit synth
, AUGraph graph
);
91 extern int SynthUnit_Close(AUGraph graph
);
94 LONG
CoreAudio_MIDIInit(void)
97 CHAR szPname
[MAXPNAMELEN
] = {0};
99 int numDest
= MIDIGetNumberOfDestinations();
100 CFStringRef name
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("wineMIDIClient.%d"), getpid());
102 wineMIDIClient
= CoreMIDI_CreateClient( name
);
103 if (wineMIDIClient
== NULL
)
106 ERR("can't create wineMIDIClient\n");
111 MIDIOut_NumDevs
= MAX_MIDI_SYNTHS
;
112 MIDIOut_NumDevs
+= numDest
;
114 MIDIIn_NumDevs
= MIDIGetNumberOfSources();
116 TRACE("MIDIOut_NumDevs %d MIDIIn_NumDevs %d\n", MIDIOut_NumDevs
, MIDIIn_NumDevs
);
118 destinations
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, MIDIOut_NumDevs
* sizeof(MIDIDestination
));
119 sources
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, MIDIIn_NumDevs
* sizeof(MIDISource
));
121 if (MIDIIn_NumDevs
> 0)
123 InitializeCriticalSection(&midiInLock
);
124 MIDIInThreadPortName
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("MIDIInThreadPortName.%u"), getpid());
125 CreateThread(NULL
, 0, MIDIIn_MessageThread
, NULL
, 0, NULL
);
128 /* initialize sources */
129 for (i
= 0; i
< MIDIIn_NumDevs
; i
++)
131 MIDIEndpointRef endpoint
= MIDIGetSource(i
);
133 sources
[i
].wDevID
= i
;
135 CoreMIDI_GetObjectName(endpoint
, szPname
, sizeof(szPname
));
136 MultiByteToWideChar(CP_ACP
, 0, szPname
, -1, sources
[i
].caps
.szPname
, sizeof(sources
[i
].caps
.szPname
)/sizeof(WCHAR
));
138 name
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("WineInputPort.%d.%u"), i
, getpid());
139 MIDIInputPortCreate(wineMIDIClient
, name
, MIDIIn_ReadProc
, &sources
[i
].wDevID
, &sources
[i
].port
);
142 MIDIPortConnectSource(sources
[i
].port
, endpoint
, NULL
);
144 sources
[i
].state
= 0;
146 sources
[i
].caps
.wMid
= 0x00FF; /* Manufac ID */
147 sources
[i
].caps
.wPid
= 0x0001; /* Product ID */
148 sources
[i
].caps
.vDriverVersion
= 0x0001;
149 sources
[i
].caps
.dwSupport
= 0;
152 /* initialise MIDI synths */
153 for (i
= 0; i
< MAX_MIDI_SYNTHS
; i
++)
155 snprintf(szPname
, sizeof(szPname
), "CoreAudio MIDI Synth %d", i
);
156 MultiByteToWideChar(CP_ACP
, 0, szPname
, -1, destinations
[i
].caps
.szPname
, sizeof(destinations
[i
].caps
.szPname
)/sizeof(WCHAR
));
158 destinations
[i
].caps
.wTechnology
= MOD_SYNTH
;
159 destinations
[i
].caps
.wChannelMask
= 0xFFFF;
161 destinations
[i
].caps
.wMid
= 0x00FF; /* Manufac ID */
162 destinations
[i
].caps
.wPid
= 0x0001; /* Product ID */
163 destinations
[i
].caps
.vDriverVersion
= 0x0001;
164 destinations
[i
].caps
.dwSupport
= MIDICAPS_VOLUME
;
165 destinations
[i
].caps
.wVoices
= 16;
166 destinations
[i
].caps
.wNotes
= 16;
168 /* initialise available destinations */
169 for (i
= MAX_MIDI_SYNTHS
; i
< numDest
+ MAX_MIDI_SYNTHS
; i
++)
171 MIDIEndpointRef endpoint
= MIDIGetDestination(i
- MAX_MIDI_SYNTHS
);
173 CoreMIDI_GetObjectName(endpoint
, szPname
, sizeof(szPname
));
174 MultiByteToWideChar(CP_ACP
, 0, szPname
, -1, destinations
[i
].caps
.szPname
, sizeof(destinations
[i
].caps
.szPname
)/sizeof(WCHAR
));
176 name
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("WineOutputPort.%d.%u"), i
, getpid());
177 MIDIOutputPortCreate(wineMIDIClient
, name
, &destinations
[i
].port
);
180 destinations
[i
].caps
.wTechnology
= MOD_MIDIPORT
;
181 destinations
[i
].caps
.wChannelMask
= 0xFFFF;
183 destinations
[i
].caps
.wMid
= 0x00FF; /* Manufac ID */
184 destinations
[i
].caps
.wPid
= 0x0001;
185 destinations
[i
].caps
.vDriverVersion
= 0x0001;
186 destinations
[i
].caps
.dwSupport
= 0;
187 destinations
[i
].caps
.wVoices
= 16;
188 destinations
[i
].caps
.wNotes
= 16;
193 LONG
CoreAudio_MIDIRelease(void)
196 if (MIDIIn_NumDevs
> 0)
198 CFMessagePortRef messagePort
;
199 /* Stop CFRunLoop in MIDIIn_MessageThread */
200 messagePort
= CFMessagePortCreateRemote(kCFAllocatorDefault
, MIDIInThreadPortName
);
201 CFMessagePortSendRequest(messagePort
, 1, NULL
, 0.0, 0.0, NULL
, NULL
);
202 CFRelease(messagePort
);
204 DeleteCriticalSection(&midiInLock
);
207 if (wineMIDIClient
) MIDIClientDispose(wineMIDIClient
); /* MIDIClientDispose will close all ports */
209 HeapFree(GetProcessHeap(), 0, sources
);
210 HeapFree(GetProcessHeap(), 0, destinations
);
215 /**************************************************************************
216 * MIDI_NotifyClient [internal]
218 static DWORD
MIDI_NotifyClient(UINT wDevID
, WORD wMsg
, DWORD dwParam1
, DWORD dwParam2
)
225 TRACE("wDevID=%d wMsg=%d dwParm1=%04X dwParam2=%04X\n", wDevID
, wMsg
, dwParam1
, dwParam2
);
232 dwCallBack
= destinations
[wDevID
].midiDesc
.dwCallback
;
233 uFlags
= destinations
[wDevID
].wFlags
;
234 hDev
= destinations
[wDevID
].midiDesc
.hMidi
;
235 dwInstance
= destinations
[wDevID
].midiDesc
.dwInstance
;
245 dwCallBack
= sources
[wDevID
].midiDesc
.dwCallback
;
246 uFlags
= sources
[wDevID
].wFlags
;
247 hDev
= sources
[wDevID
].midiDesc
.hMidi
;
248 dwInstance
= sources
[wDevID
].midiDesc
.dwInstance
;
251 WARN("Unsupported MSW-MIDI message %u\n", wMsg
);
252 return MMSYSERR_ERROR
;
255 return DriverCallback(dwCallBack
, uFlags
, hDev
, wMsg
, dwInstance
, dwParam1
, dwParam2
) ?
256 MMSYSERR_NOERROR
: MMSYSERR_ERROR
;
259 static DWORD
MIDIOut_Open(WORD wDevID
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
261 MIDIDestination
*dest
;
263 TRACE("wDevID=%d lpDesc=%p dwFlags=%08x\n", wDevID
, lpDesc
, dwFlags
);
265 if (lpDesc
== NULL
) {
266 WARN("Invalid Parameter\n");
267 return MMSYSERR_INVALPARAM
;
270 if (wDevID
>= MIDIOut_NumDevs
) {
271 WARN("bad device ID : %d\n", wDevID
);
272 return MMSYSERR_BADDEVICEID
;
275 if (destinations
[wDevID
].midiDesc
.hMidi
!= 0) {
276 WARN("device already open !\n");
277 return MMSYSERR_ALLOCATED
;
280 if ((dwFlags
& ~CALLBACK_TYPEMASK
) != 0) {
281 WARN("bad dwFlags\n");
282 return MMSYSERR_INVALFLAG
;
284 dest
= &destinations
[wDevID
];
286 if (dest
->caps
.wTechnology
== MOD_SYNTH
)
288 if (!SynthUnit_CreateDefaultSynthUnit(&dest
->graph
, &dest
->synth
))
290 ERR("SynthUnit_CreateDefaultSynthUnit dest=%p failed\n", dest
);
291 return MMSYSERR_ERROR
;
294 if (!SynthUnit_Initialize(dest
->synth
, dest
->graph
))
296 ERR("SynthUnit_Initialise dest=%p failed\n", dest
);
297 return MMSYSERR_ERROR
;
302 FIXME("MOD_MIDIPORT\n");
304 dest
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
305 dest
->midiDesc
= *lpDesc
;
307 return MIDI_NotifyClient(wDevID
, MOM_OPEN
, 0L, 0L);
310 static DWORD
MIDIOut_Close(WORD wDevID
)
312 DWORD ret
= MMSYSERR_NOERROR
;
314 TRACE("wDevID=%d\n", wDevID
);
316 if (wDevID
>= MIDIOut_NumDevs
) {
317 WARN("bad device ID : %d\n", wDevID
);
318 return MMSYSERR_BADDEVICEID
;
321 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
322 SynthUnit_Close(destinations
[wDevID
].graph
);
324 FIXME("MOD_MIDIPORT\n");
326 destinations
[wDevID
].graph
= 0;
327 destinations
[wDevID
].synth
= 0;
329 if (MIDI_NotifyClient(wDevID
, MOM_CLOSE
, 0L, 0L) != MMSYSERR_NOERROR
) {
330 WARN("can't notify client !\n");
331 ret
= MMSYSERR_INVALPARAM
;
333 destinations
[wDevID
].midiDesc
.hMidi
= 0;
338 static DWORD
MIDIOut_Data(WORD wDevID
, DWORD dwParam
)
340 WORD evt
= LOBYTE(LOWORD(dwParam
));
341 WORD d1
= HIBYTE(LOWORD(dwParam
));
342 WORD d2
= LOBYTE(HIWORD(dwParam
));
343 UInt8 chn
= (evt
& 0x0F);
344 OSStatus err
= noErr
;
346 TRACE("wDevID=%d dwParam=%08X\n", wDevID
, dwParam
);
348 if (wDevID
>= MIDIOut_NumDevs
) {
349 WARN("bad device ID : %d\n", wDevID
);
350 return MMSYSERR_BADDEVICEID
;
353 TRACE("evt=%08x d1=%04x d2=%04x (evt & 0xF0)=%04x chn=%d\n", evt
, d1
, d2
, (evt
& 0xF0), chn
);
355 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
357 err
= MusicDeviceMIDIEvent(destinations
[wDevID
].synth
, (evt
& 0xF0) | chn
, d1
, d2
, 0);
360 ERR("MusicDeviceMIDIEvent(%p, %04x, %04x, %04x, %d) return %c%c%c%c\n", destinations
[wDevID
].synth
, (evt
& 0xF0) | chn
, d1
, d2
, 0, (char) (err
>> 24), (char) (err
>> 16), (char) (err
>> 8), (char) err
);
361 return MMSYSERR_ERROR
;
364 else FIXME("MOD_MIDIPORT\n");
366 return MMSYSERR_NOERROR
;
369 static DWORD
MIDIOut_LongData(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
372 OSStatus err
= noErr
;
374 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
376 /* Note: MS doc does not say much about the dwBytesRecorded member of the MIDIHDR structure
377 * but it seems to be used only for midi input.
378 * Taking a look at the WAVEHDR structure (which is quite similar) confirms this assumption.
381 if (wDevID
>= MIDIOut_NumDevs
) {
382 WARN("bad device ID : %d\n", wDevID
);
383 return MMSYSERR_BADDEVICEID
;
386 if (lpMidiHdr
== NULL
) {
387 WARN("Invalid Parameter\n");
388 return MMSYSERR_INVALPARAM
;
391 lpData
= (LPBYTE
) lpMidiHdr
->lpData
;
394 return MIDIERR_UNPREPARED
;
395 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
396 return MIDIERR_UNPREPARED
;
397 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
398 return MIDIERR_STILLPLAYING
;
399 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
400 lpMidiHdr
->dwFlags
|= MHDR_INQUEUE
;
402 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
403 * data, or can it also contain raw MIDI data, to be split up and sent to
405 * If the latest is true, then the following WARNing will fire up
407 if (lpData
[0] != 0xF0 || lpData
[lpMidiHdr
->dwBufferLength
- 1] != 0xF7) {
408 WARN("Alledged system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
411 TRACE("dwBufferLength=%u !\n", lpMidiHdr
->dwBufferLength
);
412 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
413 lpData
[0], lpData
[1], lpData
[2], lpData
[lpMidiHdr
->dwBufferLength
-3],
414 lpData
[lpMidiHdr
->dwBufferLength
-2], lpData
[lpMidiHdr
->dwBufferLength
-1]);
417 if (lpData
[0] != 0xF0) {
418 /* System Exclusive */
419 ERR("Add missing 0xF0 marker at the beginning of system exclusive byte stream\n");
421 if (lpData
[lpMidiHdr
->dwBufferLength
- 1] != 0xF7) {
422 /* Send end of System Exclusive */
423 ERR("Add missing 0xF7 marker at the end of system exclusive byte stream\n");
425 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
) /* FIXME */
427 err
= MusicDeviceSysEx(destinations
[wDevID
].synth
, (const UInt8
*) lpData
, lpMidiHdr
->dwBufferLength
);
430 ERR("MusicDeviceSysEx(%p, %p, %d) return %c%c%c%c\n", destinations
[wDevID
].synth
, lpData
, lpMidiHdr
->dwBufferLength
, (char) (err
>> 24), (char) (err
>> 16), (char) (err
>> 8), (char) err
);
431 return MMSYSERR_ERROR
;
436 FIXME("MOD_MIDIPORT\n");
439 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
440 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
441 if (MIDI_NotifyClient(wDevID
, MOM_DONE
, (DWORD
)lpMidiHdr
, 0L) != MMSYSERR_NOERROR
) {
442 WARN("can't notify client !\n");
443 return MMSYSERR_INVALPARAM
;
445 return MMSYSERR_NOERROR
;
448 /**************************************************************************
449 * MIDIOut_Prepare [internal]
451 static DWORD
MIDIOut_Prepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
453 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
455 if (wDevID
>= MIDIOut_NumDevs
) {
456 WARN("bad device ID : %d\n", wDevID
);
457 return MMSYSERR_BADDEVICEID
;
460 /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
461 * asks to prepare MIDIHDR which dwFlags != 0.
462 * So at least check for the inqueue flag
464 if (dwSize
< sizeof(MIDIHDR
) || lpMidiHdr
== 0 ||
465 lpMidiHdr
->lpData
== 0 || (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) != 0 ||
466 lpMidiHdr
->dwBufferLength
>= 0x10000ul
) {
467 WARN("%p %p %08x %lu/%d\n", lpMidiHdr
, lpMidiHdr
->lpData
,
468 lpMidiHdr
->dwFlags
, sizeof(MIDIHDR
), dwSize
);
469 return MMSYSERR_INVALPARAM
;
472 lpMidiHdr
->lpNext
= 0;
473 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
474 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
475 return MMSYSERR_NOERROR
;
478 /**************************************************************************
479 * MIDIOut_Unprepare [internal]
481 static DWORD
MIDIOut_Unprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
483 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
485 if (wDevID
>= MIDIOut_NumDevs
) {
486 WARN("bad device ID : %d\n", wDevID
);
487 return MMSYSERR_BADDEVICEID
;
489 if (dwSize
< sizeof(MIDIHDR
) || lpMidiHdr
== 0)
490 return MMSYSERR_INVALPARAM
;
491 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
492 return MIDIERR_STILLPLAYING
;
494 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
496 return MMSYSERR_NOERROR
;
499 static DWORD
MIDIOut_GetDevCaps(WORD wDevID
, LPMIDIOUTCAPSW lpCaps
, DWORD dwSize
)
501 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID
, lpCaps
, dwSize
);
503 if (lpCaps
== NULL
) {
504 WARN("Invalid Parameter\n");
505 return MMSYSERR_INVALPARAM
;
508 if (wDevID
>= MIDIOut_NumDevs
) {
509 WARN("bad device ID : %d\n", wDevID
);
510 return MMSYSERR_BADDEVICEID
;
512 memcpy(lpCaps
, &destinations
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
513 return MMSYSERR_NOERROR
;
516 static DWORD
MIDIOut_GetNumDevs(void)
519 return MIDIOut_NumDevs
;
522 static DWORD
MIDIOut_GetVolume(WORD wDevID
, DWORD
*lpdwVolume
)
524 TRACE("%d\n", wDevID
);
526 if (wDevID
>= MIDIOut_NumDevs
) {
527 WARN("bad device ID : %d\n", wDevID
);
528 return MMSYSERR_BADDEVICEID
;
530 if (lpdwVolume
== NULL
) {
531 WARN("Invalid Parameter\n");
532 return MMSYSERR_INVALPARAM
;
535 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
539 AudioUnit_GetVolume(destinations
[wDevID
].synth
, &left
, &right
);
541 *lpdwVolume
= ((WORD
) left
* 0xFFFFl
) + (((WORD
) right
* 0xFFFFl
) << 16);
543 return MMSYSERR_NOERROR
;
546 return MMSYSERR_NOTSUPPORTED
;
549 static DWORD
MIDIOut_SetVolume(WORD wDevID
, DWORD dwVolume
)
551 TRACE("%d\n", wDevID
);
553 if (wDevID
>= MIDIOut_NumDevs
) {
554 WARN("bad device ID : %d\n", wDevID
);
555 return MMSYSERR_BADDEVICEID
;
557 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
562 left
= LOWORD(dwVolume
) / 65535.0f
;
563 right
= HIWORD(dwVolume
) / 65535.0f
;
564 AudioUnit_SetVolume(destinations
[wDevID
].synth
, left
, right
);
566 return MMSYSERR_NOERROR
;
569 return MMSYSERR_NOTSUPPORTED
;
572 static DWORD
MIDIOut_Reset(WORD wDevID
)
576 TRACE("%d\n", wDevID
);
578 if (wDevID
>= MIDIOut_NumDevs
) {
579 WARN("bad device ID : %d\n", wDevID
);
580 return MMSYSERR_BADDEVICEID
;
582 if (destinations
[wDevID
].caps
.wTechnology
== MOD_SYNTH
)
584 for (chn
= 0; chn
< 16; chn
++) {
585 /* turn off every note */
586 MusicDeviceMIDIEvent(destinations
[wDevID
].synth
, 0xB0 | chn
, 0x7B, 0, 0);
587 /* remove sustain on channel */
588 MusicDeviceMIDIEvent(destinations
[wDevID
].synth
, 0xB0 | chn
, 0x40, 0, 0);
591 else FIXME("MOD_MIDIPORT\n");
593 /* FIXME: the LongData buffers must also be returned to the app */
594 return MMSYSERR_NOERROR
;
597 static DWORD
MIDIIn_Open(WORD wDevID
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
599 TRACE("wDevID=%d lpDesc=%p dwFlags=%08x\n", wDevID
, lpDesc
, dwFlags
);
601 if (lpDesc
== NULL
) {
602 WARN("Invalid Parameter\n");
603 return MMSYSERR_INVALPARAM
;
605 if (wDevID
>= MIDIIn_NumDevs
) {
606 WARN("bad device ID : %d\n", wDevID
);
607 return MMSYSERR_BADDEVICEID
;
609 if (sources
[wDevID
].midiDesc
.hMidi
!= 0) {
610 WARN("device already open !\n");
611 return MMSYSERR_ALLOCATED
;
613 if ((dwFlags
& MIDI_IO_STATUS
) != 0) {
614 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
615 dwFlags
&= ~MIDI_IO_STATUS
;
617 if ((dwFlags
& ~CALLBACK_TYPEMASK
) != 0) {
618 FIXME("Bad dwFlags\n");
619 return MMSYSERR_INVALFLAG
;
622 sources
[wDevID
].wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
623 sources
[wDevID
].lpQueueHdr
= NULL
;
624 sources
[wDevID
].midiDesc
= *lpDesc
;
625 sources
[wDevID
].startTime
= 0;
626 sources
[wDevID
].state
= 0;
628 return MIDI_NotifyClient(wDevID
, MIM_OPEN
, 0L, 0L);
631 static DWORD
MIDIIn_Close(WORD wDevID
)
633 DWORD ret
= MMSYSERR_NOERROR
;
635 TRACE("wDevID=%d\n", wDevID
);
637 if (wDevID
>= MIDIIn_NumDevs
) {
638 WARN("bad device ID : %d\n", wDevID
);
639 return MMSYSERR_BADDEVICEID
;
642 if (sources
[wDevID
].midiDesc
.hMidi
== 0) {
643 WARN("device not opened !\n");
644 return MMSYSERR_ERROR
;
646 if (sources
[wDevID
].lpQueueHdr
!= 0) {
647 return MIDIERR_STILLPLAYING
;
650 ret
= MIDI_NotifyClient(wDevID
, MIM_CLOSE
, 0L, 0L);
651 sources
[wDevID
].midiDesc
.hMidi
= 0;
655 static DWORD
MIDIIn_AddBuffer(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
657 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
659 if (wDevID
>= MIDIIn_NumDevs
) {
660 WARN("bad device ID : %d\n", wDevID
);
661 return MMSYSERR_BADDEVICEID
;
663 if (lpMidiHdr
== NULL
) {
664 WARN("Invalid Parameter\n");
665 return MMSYSERR_INVALPARAM
;
667 if (sizeof(MIDIHDR
) > dwSize
) {
668 WARN("Invalid Parameter\n");
669 return MMSYSERR_INVALPARAM
;
671 if (lpMidiHdr
->dwBufferLength
== 0) {
672 WARN("Invalid Parameter\n");
673 return MMSYSERR_INVALPARAM
;
675 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) {
676 WARN("Still playing\n");
677 return MIDIERR_STILLPLAYING
;
679 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
)) {
680 WARN("Unprepared\n");
681 return MIDIERR_UNPREPARED
;
684 EnterCriticalSection(&midiInLock
);
685 if (sources
[wDevID
].lpQueueHdr
== 0) {
686 sources
[wDevID
].lpQueueHdr
= lpMidiHdr
;
689 for (ptr
= sources
[wDevID
].lpQueueHdr
;
691 ptr
= (LPMIDIHDR
)ptr
->lpNext
);
692 ptr
->lpNext
= (struct midihdr_tag
*)lpMidiHdr
;
694 LeaveCriticalSection(&midiInLock
);
696 return MMSYSERR_NOERROR
;
699 static DWORD
MIDIIn_Prepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
701 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
703 if (wDevID
>= MIDIIn_NumDevs
) {
704 WARN("bad device ID : %d\n", wDevID
);
705 return MMSYSERR_BADDEVICEID
;
707 /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
708 * asks to prepare MIDIHDR which dwFlags != 0.
709 * So at least check for the inqueue flag
711 if (dwSize
< sizeof(MIDIHDR
) || lpMidiHdr
== 0 ||
712 lpMidiHdr
->lpData
== 0 || (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) != 0 ||
713 lpMidiHdr
->dwBufferLength
>= 0x10000ul
) {
714 WARN("Invalid parameter %p %p %08x %d\n", lpMidiHdr
, lpMidiHdr
->lpData
,
715 lpMidiHdr
->dwFlags
, dwSize
);
716 return MMSYSERR_INVALPARAM
;
719 lpMidiHdr
->lpNext
= 0;
720 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
721 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
722 return MMSYSERR_NOERROR
;
725 static DWORD
MIDIIn_Unprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
727 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID
, lpMidiHdr
, dwSize
);
728 if (wDevID
>= MIDIIn_NumDevs
) {
729 WARN("bad device ID : %d\n", wDevID
);
730 return MMSYSERR_BADDEVICEID
;
732 if (dwSize
< sizeof(MIDIHDR
) || lpMidiHdr
== 0) {
733 WARN("Invalid Parameter\n");
734 return MMSYSERR_INVALPARAM
;
736 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) {
737 WARN("Still playing\n");
738 return MIDIERR_STILLPLAYING
;
741 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
742 return MMSYSERR_NOERROR
;
745 static DWORD
MIDIIn_GetDevCaps(WORD wDevID
, LPMIDIINCAPSW lpCaps
, DWORD dwSize
)
747 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID
, lpCaps
, dwSize
);
749 if (lpCaps
== NULL
) {
750 WARN("Invalid Parameter\n");
751 return MMSYSERR_INVALPARAM
;
754 if (wDevID
>= MIDIIn_NumDevs
) {
755 WARN("bad device ID : %d\n", wDevID
);
756 return MMSYSERR_BADDEVICEID
;
758 memcpy(lpCaps
, &sources
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
759 return MMSYSERR_NOERROR
;
762 static DWORD
MIDIIn_GetNumDevs(void)
765 return MIDIIn_NumDevs
;
768 static DWORD
MIDIIn_Start(WORD wDevID
)
770 TRACE("%d\n", wDevID
);
772 if (wDevID
>= MIDIIn_NumDevs
) {
773 WARN("bad device ID : %d\n", wDevID
);
774 return MMSYSERR_BADDEVICEID
;
776 sources
[wDevID
].state
= 1;
777 sources
[wDevID
].startTime
= GetTickCount();
778 return MMSYSERR_NOERROR
;
781 static DWORD
MIDIIn_Stop(WORD wDevID
)
783 TRACE("%d\n", wDevID
);
784 if (wDevID
>= MIDIIn_NumDevs
) {
785 WARN("bad device ID : %d\n", wDevID
);
786 return MMSYSERR_BADDEVICEID
;
788 sources
[wDevID
].state
= 0;
789 return MMSYSERR_NOERROR
;
792 static DWORD
MIDIIn_Reset(WORD wDevID
)
794 DWORD dwTime
= GetTickCount();
796 TRACE("%d\n", wDevID
);
797 if (wDevID
>= MIDIIn_NumDevs
) {
798 WARN("bad device ID : %d\n", wDevID
);
799 return MMSYSERR_BADDEVICEID
;
802 EnterCriticalSection(&midiInLock
);
803 while (sources
[wDevID
].lpQueueHdr
) {
804 sources
[wDevID
].lpQueueHdr
->dwFlags
&= ~MHDR_INQUEUE
;
805 sources
[wDevID
].lpQueueHdr
->dwFlags
|= MHDR_DONE
;
806 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
807 if (MIDI_NotifyClient(wDevID
, MIM_LONGDATA
, (DWORD
)sources
[wDevID
].lpQueueHdr
, dwTime
) != MMSYSERR_NOERROR
) {
808 WARN("Couldn't notify client\n");
810 sources
[wDevID
].lpQueueHdr
= (LPMIDIHDR
)sources
[wDevID
].lpQueueHdr
->lpNext
;
812 LeaveCriticalSection(&midiInLock
);
814 return MMSYSERR_NOERROR
;
818 * MIDI In Mach message handling
822 * Call from CoreMIDI IO threaded callback,
823 * we can't call Wine debug channels, critical section or anything using NtCurrentTeb here.
825 void MIDIIn_SendMessage(MIDIMessage msg
)
829 CFMessagePortRef messagePort
;
830 messagePort
= CFMessagePortCreateRemote(kCFAllocatorDefault
, MIDIInThreadPortName
);
832 data
= CFDataCreate(kCFAllocatorDefault
, (UInt8
*) &msg
, sizeof(msg
));
835 CFMessagePortSendRequest(messagePort
, 0, data
, 0.0, 0.0, NULL
, NULL
);
837 CFRelease(messagePort
);
841 static CFDataRef
MIDIIn_MessageHandler(CFMessagePortRef local
, SInt32 msgid
, CFDataRef data
, void *info
)
843 MIDIMessage
*msg
= NULL
;
850 msg
= (MIDIMessage
*) CFDataGetBytePtr(data
);
851 TRACE("devID=%d\n", msg
->devID
);
852 for (i
= 0; i
< msg
->length
; ++i
) {
853 TRACE("%02X ", msg
->data
[i
]);
858 CFRunLoopStop(CFRunLoopGetCurrent());
864 static DWORD WINAPI
MIDIIn_MessageThread(LPVOID p
)
866 CFMessagePortRef local
;
867 CFRunLoopSourceRef source
;
870 local
= CFMessagePortCreateLocal(kCFAllocatorDefault
, MIDIInThreadPortName
, &MIDIIn_MessageHandler
, NULL
, &info
);
872 source
= CFMessagePortCreateRunLoopSource(kCFAllocatorDefault
, local
, (CFIndex
)0);
873 CFRunLoopAddSource(CFRunLoopGetCurrent(), source
, kCFRunLoopDefaultMode
);
877 CFRunLoopSourceInvalidate(source
);
880 CFRelease(MIDIInThreadPortName
);
881 MIDIInThreadPortName
= NULL
;
886 /**************************************************************************
889 DWORD WINAPI
CoreAudio_modMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
, DWORD dwParam1
, DWORD dwParam2
)
891 TRACE("%d %08x %08x %08x %08x\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
900 return MIDIOut_Open(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
902 return MIDIOut_Close(wDevID
);
904 return MIDIOut_Data(wDevID
, dwParam1
);
906 return MIDIOut_LongData(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
908 return MIDIOut_Prepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
910 return MIDIOut_Unprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
911 case MODM_GETDEVCAPS
:
912 return MIDIOut_GetDevCaps(wDevID
, (LPMIDIOUTCAPSW
) dwParam1
, dwParam2
);
913 case MODM_GETNUMDEVS
:
914 return MIDIOut_GetNumDevs();
916 return MIDIOut_GetVolume(wDevID
, (DWORD
*)dwParam1
);
918 return MIDIOut_SetVolume(wDevID
, dwParam1
);
920 return MIDIOut_Reset(wDevID
);
922 TRACE("Unsupported message (08%x)\n", wMsg
);
924 return MMSYSERR_NOTSUPPORTED
;
927 /**************************************************************************
930 DWORD WINAPI
CoreAudio_midMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
, DWORD dwParam1
, DWORD dwParam2
)
932 TRACE("%d %08x %08x %08x %08x\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
940 return MIDIIn_Open(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
942 return MIDIIn_Close(wDevID
);
944 return MIDIIn_AddBuffer(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
946 return MIDIIn_Prepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
948 return MIDIIn_Unprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
949 case MIDM_GETDEVCAPS
:
950 return MIDIIn_GetDevCaps(wDevID
, (LPMIDIINCAPSW
) dwParam1
, dwParam2
);
951 case MIDM_GETNUMDEVS
:
952 return MIDIIn_GetNumDevs();
954 return MIDIIn_Start(wDevID
);
956 return MIDIIn_Stop(wDevID
);
958 return MIDIIn_Reset(wDevID
);
960 TRACE("Unsupported message\n");
962 return MMSYSERR_NOTSUPPORTED
;
966 DWORD WINAPI
CoreAudio_modMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
, DWORD dwParam1
, DWORD dwParam2
)
968 TRACE("%08x, %08x, %08x, %08x, %08x\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
969 return MMSYSERR_NOTENABLED
;
972 DWORD WINAPI
CoreAudio_midMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
973 DWORD dwParam1
, DWORD dwParam2
)
975 TRACE("%08x, %08x, %08x, %08x, %08x\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
976 return MMSYSERR_NOTENABLED
;