wineps: Free the output string from the correct heap.
[wine.git] / dlls / winecoreaudio.drv / midi.c
blobf539b0a67832186c821bf2f719f7c8ad5bea86a1
1 /*
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
28 #include "config.h"
29 #include "wine/port.h"
31 #include <string.h>
32 #include <stdarg.h>
33 #include <stdio.h>
35 #include "windef.h"
36 #include "winbase.h"
37 #include "wingdi.h"
38 #include "winuser.h"
39 #include "winnls.h"
40 #include "mmddk.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
50 #include "coremidi.h"
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 */
59 AUGraph graph;
60 AudioUnit synth;
62 MIDIEndpointRef dest;
64 MIDIOUTCAPSW caps;
65 MIDIOPENDESC midiDesc;
66 WORD wFlags;
67 } MIDIDestination;
69 typedef struct tagMIDISource {
70 MIDIEndpointRef source;
72 WORD wDevID;
73 int state; /* 0 is no recording started, 1 in recording, bit 2 set if in sys exclusive recording */
74 MIDIINCAPSW caps;
75 MIDIOPENDESC midiDesc;
76 LPMIDIHDR lpQueueHdr;
77 WORD wFlags;
78 DWORD startTime;
79 } MIDISource;
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;
92 MIDISource *sources;
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)
101 int i;
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)
110 CFRelease(name);
111 ERR("can't create wineMIDIClient\n");
112 return DRV_FAILURE;
114 CFRelease(name);
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);
135 CFRelease(name);
137 if (numDest > 0)
139 name = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("WineOutputPort.%u"), getpid());
140 MIDIOutputPortCreate(wineMIDIClient, name, &MIDIOutPort);
141 CFRelease(name);
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;
156 /* FIXME */
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;
197 return DRV_SUCCESS;
200 LONG CoreAudio_MIDIRelease(void)
202 TRACE("\n");
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);
219 return DRV_SUCCESS;
223 /**************************************************************************
224 * MIDI_NotifyClient [internal]
226 static void MIDI_NotifyClient(UINT wDevID, WORD wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
228 DWORD dwCallBack;
229 UINT uFlags;
230 HANDLE hDev;
231 DWORD dwInstance;
233 TRACE("wDevID=%d wMsg=%d dwParm1=%04lX dwParam2=%04lX\n", wDevID, wMsg, dwParam1, dwParam2);
235 switch (wMsg) {
236 case MOM_OPEN:
237 case MOM_CLOSE:
238 case MOM_DONE:
239 case MOM_POSITIONCB:
240 dwCallBack = destinations[wDevID].midiDesc.dwCallback;
241 uFlags = destinations[wDevID].wFlags;
242 hDev = destinations[wDevID].midiDesc.hMidi;
243 dwInstance = destinations[wDevID].midiDesc.dwInstance;
244 break;
246 case MIM_OPEN:
247 case MIM_CLOSE:
248 case MIM_DATA:
249 case MIM_LONGDATA:
250 case MIM_ERROR:
251 case MIM_LONGERROR:
252 case MIM_MOREDATA:
253 dwCallBack = sources[wDevID].midiDesc.dwCallback;
254 uFlags = sources[wDevID].wFlags;
255 hDev = sources[wDevID].midiDesc.hMidi;
256 dwInstance = sources[wDevID].midiDesc.dwInstance;
257 break;
258 default:
259 ERR("Unsupported MSW-MIDI message %u\n", wMsg);
260 return;
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;
334 return ret;
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);
356 if (err != noErr)
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;
362 else
364 UInt8 buffer[3];
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)
377 LPBYTE lpData;
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;
399 if (lpData == NULL)
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
410 * modShortData() ?
411 * If the latest is true, then the following WARNing will fire up
413 if (lpData[0] != 0xF0 || lpData[lpMidiHdr->dwBufferLength - 1] != 0xF7) {
414 WARN("Alledged 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);
434 if (err != noErr)
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
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 (wDevID >= MIDIOut_NumDevs) {
459 WARN("bad device ID : %d\n", wDevID);
460 return MMSYSERR_BADDEVICEID;
463 /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
464 * asks to prepare MIDIHDR which dwFlags != 0.
465 * So at least check for the inqueue flag
467 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0 ||
468 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0) {
469 WARN("%p %p %08x %lu/%d\n", lpMidiHdr, lpMidiHdr->lpData,
470 lpMidiHdr->dwFlags, offsetof(MIDIHDR,dwOffset), dwSize);
471 return MMSYSERR_INVALPARAM;
474 lpMidiHdr->lpNext = 0;
475 lpMidiHdr->dwFlags |= MHDR_PREPARED;
476 lpMidiHdr->dwFlags &= ~MHDR_DONE;
477 return MMSYSERR_NOERROR;
480 /**************************************************************************
481 * MIDIOut_Unprepare [internal]
483 static DWORD MIDIOut_Unprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
485 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
487 if (wDevID >= MIDIOut_NumDevs) {
488 WARN("bad device ID : %d\n", wDevID);
489 return MMSYSERR_BADDEVICEID;
491 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0)
492 return MMSYSERR_INVALPARAM;
493 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
494 return MIDIERR_STILLPLAYING;
496 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
498 return MMSYSERR_NOERROR;
501 static DWORD MIDIOut_GetDevCaps(WORD wDevID, LPMIDIOUTCAPSW lpCaps, DWORD dwSize)
503 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID, lpCaps, dwSize);
505 if (lpCaps == NULL) {
506 WARN("Invalid Parameter\n");
507 return MMSYSERR_INVALPARAM;
510 if (wDevID >= MIDIOut_NumDevs) {
511 WARN("bad device ID : %d\n", wDevID);
512 return MMSYSERR_BADDEVICEID;
514 memcpy(lpCaps, &destinations[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
515 return MMSYSERR_NOERROR;
518 static DWORD MIDIOut_GetNumDevs(void)
520 TRACE("\n");
521 return MIDIOut_NumDevs;
524 static DWORD MIDIOut_GetVolume(WORD wDevID, DWORD *lpdwVolume)
526 TRACE("%d\n", wDevID);
528 if (wDevID >= MIDIOut_NumDevs) {
529 WARN("bad device ID : %d\n", wDevID);
530 return MMSYSERR_BADDEVICEID;
532 if (lpdwVolume == NULL) {
533 WARN("Invalid Parameter\n");
534 return MMSYSERR_INVALPARAM;
537 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
539 float left;
540 float right;
541 AudioUnit_GetVolume(destinations[wDevID].synth, &left, &right);
543 *lpdwVolume = (WORD) (left * 0xFFFF) + ((WORD) (right * 0xFFFF) << 16);
545 return MMSYSERR_NOERROR;
548 return MMSYSERR_NOTSUPPORTED;
551 static DWORD MIDIOut_SetVolume(WORD wDevID, DWORD dwVolume)
553 TRACE("%d\n", wDevID);
555 if (wDevID >= MIDIOut_NumDevs) {
556 WARN("bad device ID : %d\n", wDevID);
557 return MMSYSERR_BADDEVICEID;
559 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
561 float left;
562 float right;
564 left = LOWORD(dwVolume) / 65535.0f;
565 right = HIWORD(dwVolume) / 65535.0f;
566 AudioUnit_SetVolume(destinations[wDevID].synth, left, right);
568 return MMSYSERR_NOERROR;
571 return MMSYSERR_NOTSUPPORTED;
574 static DWORD MIDIOut_Reset(WORD wDevID)
576 unsigned chn;
578 TRACE("%d\n", wDevID);
580 if (wDevID >= MIDIOut_NumDevs) {
581 WARN("bad device ID : %d\n", wDevID);
582 return MMSYSERR_BADDEVICEID;
584 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
586 for (chn = 0; chn < 16; chn++) {
587 /* turn off every note */
588 MusicDeviceMIDIEvent(destinations[wDevID].synth, 0xB0 | chn, 0x7B, 0, 0);
589 /* remove sustain on channel */
590 MusicDeviceMIDIEvent(destinations[wDevID].synth, 0xB0 | chn, 0x40, 0, 0);
593 else FIXME("MOD_MIDIPORT\n");
595 /* FIXME: the LongData buffers must also be returned to the app */
596 return MMSYSERR_NOERROR;
599 static DWORD MIDIIn_Open(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
601 TRACE("wDevID=%d lpDesc=%p dwFlags=%08x\n", wDevID, lpDesc, dwFlags);
603 if (lpDesc == NULL) {
604 WARN("Invalid Parameter\n");
605 return MMSYSERR_INVALPARAM;
607 if (wDevID >= MIDIIn_NumDevs) {
608 WARN("bad device ID : %d\n", wDevID);
609 return MMSYSERR_BADDEVICEID;
611 if (sources[wDevID].midiDesc.hMidi != 0) {
612 WARN("device already open !\n");
613 return MMSYSERR_ALLOCATED;
615 if ((dwFlags & MIDI_IO_STATUS) != 0) {
616 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
617 dwFlags &= ~MIDI_IO_STATUS;
619 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
620 FIXME("Bad dwFlags\n");
621 return MMSYSERR_INVALFLAG;
624 sources[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
625 sources[wDevID].lpQueueHdr = NULL;
626 sources[wDevID].midiDesc = *lpDesc;
627 sources[wDevID].startTime = 0;
628 sources[wDevID].state = 0;
630 MIDI_NotifyClient(wDevID, MIM_OPEN, 0L, 0L);
631 return MMSYSERR_NOERROR;
634 static DWORD MIDIIn_Close(WORD wDevID)
636 DWORD ret = MMSYSERR_NOERROR;
638 TRACE("wDevID=%d\n", wDevID);
640 if (wDevID >= MIDIIn_NumDevs) {
641 WARN("bad device ID : %d\n", wDevID);
642 return MMSYSERR_BADDEVICEID;
645 if (sources[wDevID].midiDesc.hMidi == 0) {
646 WARN("device not opened !\n");
647 return MMSYSERR_ERROR;
649 if (sources[wDevID].lpQueueHdr != 0) {
650 return MIDIERR_STILLPLAYING;
653 MIDI_NotifyClient(wDevID, MIM_CLOSE, 0L, 0L);
654 sources[wDevID].midiDesc.hMidi = 0;
655 return ret;
658 static DWORD MIDIIn_AddBuffer(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
660 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
662 if (wDevID >= MIDIIn_NumDevs) {
663 WARN("bad device ID : %d\n", wDevID);
664 return MMSYSERR_BADDEVICEID;
666 if (lpMidiHdr == NULL) {
667 WARN("Invalid Parameter\n");
668 return MMSYSERR_INVALPARAM;
670 if (dwSize < offsetof(MIDIHDR,dwOffset)) {
671 WARN("Invalid Parameter\n");
672 return MMSYSERR_INVALPARAM;
674 if (lpMidiHdr->dwBufferLength == 0) {
675 WARN("Invalid Parameter\n");
676 return MMSYSERR_INVALPARAM;
678 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) {
679 WARN("Still playing\n");
680 return MIDIERR_STILLPLAYING;
682 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) {
683 WARN("Unprepared\n");
684 return MIDIERR_UNPREPARED;
687 EnterCriticalSection(&midiInLock);
688 lpMidiHdr->dwFlags &= ~WHDR_DONE;
689 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
690 lpMidiHdr->dwBytesRecorded = 0;
691 lpMidiHdr->lpNext = 0;
692 if (sources[wDevID].lpQueueHdr == 0) {
693 sources[wDevID].lpQueueHdr = lpMidiHdr;
694 } else {
695 LPMIDIHDR ptr;
696 for (ptr = sources[wDevID].lpQueueHdr;
697 ptr->lpNext != 0;
698 ptr = ptr->lpNext);
699 ptr->lpNext = lpMidiHdr;
701 LeaveCriticalSection(&midiInLock);
703 return MMSYSERR_NOERROR;
706 static DWORD MIDIIn_Prepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
708 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
710 if (wDevID >= MIDIIn_NumDevs) {
711 WARN("bad device ID : %d\n", wDevID);
712 return MMSYSERR_BADDEVICEID;
714 /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
715 * asks to prepare MIDIHDR which dwFlags != 0.
716 * So at least check for the inqueue flag
718 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0 ||
719 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0) {
720 WARN("Invalid parameter %p %p %08x %d\n", lpMidiHdr, lpMidiHdr->lpData,
721 lpMidiHdr->dwFlags, dwSize);
722 return MMSYSERR_INVALPARAM;
725 lpMidiHdr->lpNext = 0;
726 lpMidiHdr->dwFlags |= MHDR_PREPARED;
727 lpMidiHdr->dwFlags &= ~MHDR_DONE;
728 return MMSYSERR_NOERROR;
731 static DWORD MIDIIn_Unprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
733 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
734 if (wDevID >= MIDIIn_NumDevs) {
735 WARN("bad device ID : %d\n", wDevID);
736 return MMSYSERR_BADDEVICEID;
738 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0) {
739 WARN("Invalid Parameter\n");
740 return MMSYSERR_INVALPARAM;
742 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) {
743 WARN("Still playing\n");
744 return MIDIERR_STILLPLAYING;
747 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
748 return MMSYSERR_NOERROR;
751 static DWORD MIDIIn_GetDevCaps(WORD wDevID, LPMIDIINCAPSW lpCaps, DWORD dwSize)
753 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID, lpCaps, dwSize);
755 if (lpCaps == NULL) {
756 WARN("Invalid Parameter\n");
757 return MMSYSERR_INVALPARAM;
760 if (wDevID >= MIDIIn_NumDevs) {
761 WARN("bad device ID : %d\n", wDevID);
762 return MMSYSERR_BADDEVICEID;
764 memcpy(lpCaps, &sources[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
765 return MMSYSERR_NOERROR;
768 static DWORD MIDIIn_GetNumDevs(void)
770 TRACE("\n");
771 return MIDIIn_NumDevs;
774 static DWORD MIDIIn_Start(WORD wDevID)
776 TRACE("%d\n", wDevID);
778 if (wDevID >= MIDIIn_NumDevs) {
779 WARN("bad device ID : %d\n", wDevID);
780 return MMSYSERR_BADDEVICEID;
782 sources[wDevID].state = 1;
783 sources[wDevID].startTime = GetTickCount();
784 return MMSYSERR_NOERROR;
787 static DWORD MIDIIn_Stop(WORD wDevID)
789 TRACE("%d\n", wDevID);
790 if (wDevID >= MIDIIn_NumDevs) {
791 WARN("bad device ID : %d\n", wDevID);
792 return MMSYSERR_BADDEVICEID;
794 sources[wDevID].state = 0;
795 return MMSYSERR_NOERROR;
798 static DWORD MIDIIn_Reset(WORD wDevID)
800 DWORD dwTime = GetTickCount();
802 TRACE("%d\n", wDevID);
803 if (wDevID >= MIDIIn_NumDevs) {
804 WARN("bad device ID : %d\n", wDevID);
805 return MMSYSERR_BADDEVICEID;
808 EnterCriticalSection(&midiInLock);
809 while (sources[wDevID].lpQueueHdr) {
810 LPMIDIHDR lpMidiHdr = sources[wDevID].lpQueueHdr;
811 sources[wDevID].lpQueueHdr = lpMidiHdr->lpNext;
812 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
813 lpMidiHdr->dwFlags |= MHDR_DONE;
814 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
815 MIDI_NotifyClient(wDevID, MIM_LONGDATA, (DWORD_PTR)lpMidiHdr, dwTime);
817 LeaveCriticalSection(&midiInLock);
819 return MMSYSERR_NOERROR;
823 * MIDI In Mach message handling
827 * Call from CoreMIDI IO threaded callback,
828 * we can't call Wine debug channels, critical section or anything using NtCurrentTeb here.
830 void MIDIIn_SendMessage(MIDIMessage msg)
832 CFDataRef data;
834 CFMessagePortRef messagePort;
835 messagePort = CFMessagePortCreateRemote(kCFAllocatorDefault, MIDIInThreadPortName);
837 data = CFDataCreate(kCFAllocatorDefault, (UInt8 *) &msg, sizeof(msg));
838 if (data)
840 CFMessagePortSendRequest(messagePort, 0, data, 0.0, 0.0, NULL, NULL);
841 CFRelease(data);
842 CFRelease(messagePort);
846 static CFDataRef MIDIIn_MessageHandler(CFMessagePortRef local, SInt32 msgid, CFDataRef data, void *info)
848 MIDIMessage *msg = NULL;
849 int i = 0;
850 MIDISource *src = NULL;
851 DWORD sendData = 0;
852 int pos = 0;
853 DWORD currentTime;
855 switch (msgid)
857 case 0:
858 msg = (MIDIMessage *) CFDataGetBytePtr(data);
859 TRACE("devID=%d\n", msg->devID);
860 for (i = 0; i < msg->length; ++i) {
861 TRACE("%02X ", msg->data[i]);
863 TRACE("\n");
864 src = &sources[msg->devID];
865 if (src->state < 1)
867 TRACE("input not started, thrown away\n");
868 goto done;
870 /* FIXME skipping SysEx */
871 if (msg->data[0] == 0xF0)
873 FIXME("Starting System Exclusive\n");
874 src->state |= 2;
876 if (src->state & 2)
878 for (i = 0; i < msg->length; ++i)
880 if (msg->data[i] == 0xF7)
882 FIXME("Ending System Exclusive\n");
883 src->state &= ~2;
886 goto done;
888 EnterCriticalSection(&midiInLock);
889 currentTime = GetTickCount() - src->startTime;
891 while (pos < msg->length)
893 sendData = 0;
894 switch (msg->data[pos] & 0xF0)
896 case 0xF0:
897 sendData = (msg->data[pos] << 0);
898 pos++;
899 break;
901 case 0xC0:
902 case 0xD0:
903 sendData = (msg->data[pos + 1] << 8) | (msg->data[pos] << 0);
904 pos += 2;
905 break;
906 default:
907 sendData = (msg->data[pos + 2] << 16) |
908 (msg->data[pos + 1] << 8) |
909 (msg->data[pos] << 0);
910 pos += 3;
911 break;
913 MIDI_NotifyClient(msg->devID, MIM_DATA, sendData, currentTime);
915 LeaveCriticalSection(&midiInLock);
916 break;
917 default:
918 CFRunLoopStop(CFRunLoopGetCurrent());
919 break;
921 done:
922 return NULL;
925 static DWORD WINAPI MIDIIn_MessageThread(LPVOID p)
927 CFMessagePortRef local;
928 CFRunLoopSourceRef source;
929 Boolean info;
931 local = CFMessagePortCreateLocal(kCFAllocatorDefault, MIDIInThreadPortName, &MIDIIn_MessageHandler, NULL, &info);
933 source = CFMessagePortCreateRunLoopSource(kCFAllocatorDefault, local, 0);
934 CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
936 CFRunLoopRun();
938 CFRunLoopSourceInvalidate(source);
939 CFRelease(source);
940 CFRelease(local);
941 CFRelease(MIDIInThreadPortName);
942 MIDIInThreadPortName = NULL;
944 return 0;
947 /**************************************************************************
948 * modMessage
950 DWORD WINAPI CoreAudio_modMessage(UINT wDevID, UINT wMsg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2)
952 TRACE("%d %08x %08x %08x %08x\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
954 switch (wMsg) {
955 case DRVM_INIT:
956 case DRVM_EXIT:
957 case DRVM_ENABLE:
958 case DRVM_DISABLE:
959 return 0;
960 case MODM_OPEN:
961 return MIDIOut_Open(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
962 case MODM_CLOSE:
963 return MIDIOut_Close(wDevID);
964 case MODM_DATA:
965 return MIDIOut_Data(wDevID, dwParam1);
966 case MODM_LONGDATA:
967 return MIDIOut_LongData(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
968 case MODM_PREPARE:
969 return MIDIOut_Prepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
970 case MODM_UNPREPARE:
971 return MIDIOut_Unprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
972 case MODM_GETDEVCAPS:
973 return MIDIOut_GetDevCaps(wDevID, (LPMIDIOUTCAPSW) dwParam1, dwParam2);
974 case MODM_GETNUMDEVS:
975 return MIDIOut_GetNumDevs();
976 case MODM_GETVOLUME:
977 return MIDIOut_GetVolume(wDevID, (DWORD*)dwParam1);
978 case MODM_SETVOLUME:
979 return MIDIOut_SetVolume(wDevID, dwParam1);
980 case MODM_RESET:
981 return MIDIOut_Reset(wDevID);
982 default:
983 TRACE("Unsupported message (08%x)\n", wMsg);
985 return MMSYSERR_NOTSUPPORTED;
988 /**************************************************************************
989 * midMessage
991 DWORD WINAPI CoreAudio_midMessage(UINT wDevID, UINT wMsg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2)
993 TRACE("%d %08x %08x %08x %08x\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
994 switch (wMsg) {
995 case DRVM_INIT:
996 case DRVM_EXIT:
997 case DRVM_ENABLE:
998 case DRVM_DISABLE:
999 return 0;
1000 case MIDM_OPEN:
1001 return MIDIIn_Open(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1002 case MIDM_CLOSE:
1003 return MIDIIn_Close(wDevID);
1004 case MIDM_ADDBUFFER:
1005 return MIDIIn_AddBuffer(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1006 case MIDM_PREPARE:
1007 return MIDIIn_Prepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1008 case MIDM_UNPREPARE:
1009 return MIDIIn_Unprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1010 case MIDM_GETDEVCAPS:
1011 return MIDIIn_GetDevCaps(wDevID, (LPMIDIINCAPSW) dwParam1, dwParam2);
1012 case MIDM_GETNUMDEVS:
1013 return MIDIIn_GetNumDevs();
1014 case MIDM_START:
1015 return MIDIIn_Start(wDevID);
1016 case MIDM_STOP:
1017 return MIDIIn_Stop(wDevID);
1018 case MIDM_RESET:
1019 return MIDIIn_Reset(wDevID);
1020 default:
1021 TRACE("Unsupported message\n");
1023 return MMSYSERR_NOTSUPPORTED;