wined3d: Remove fake nvidia card in GetAdapterIdentifier.
[wine/dibdrv.git] / dlls / winecoreaudio.drv / midi.c
blobfd00cb375e7c44bee431f9de12fddb04230298d9
1 /*
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
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"
44 WINE_DEFAULT_DEBUG_CHANNEL(midi);
46 #if defined(HAVE_COREAUDIO_COREAUDIO_H)
47 #include <CoreAudio/CoreAudio.h>
49 #define WINE_DEFINITIONS
50 #include "coremidi.h"
52 static MIDIClientRef wineMIDIClient = NULL;
54 static DWORD MIDIOut_NumDevs = 0;
56 typedef struct tagMIDIDestination {
57 /* graph and synth are only used for MIDI Synth */
58 AUGraph graph;
59 AudioUnit synth;
61 MIDIPortRef port;
62 MIDIOUTCAPSW caps;
63 MIDIOPENDESC midiDesc;
64 WORD wFlags;
65 } MIDIDestination;
67 #define MAX_MIDI_SYNTHS 1
69 MIDIDestination *destinations;
71 extern int SynthUnit_CreateDefaultSynthUnit(AUGraph *graph, AudioUnit *synth);
72 extern int SynthUnit_Initialize(AudioUnit synth, AUGraph graph);
73 extern int SynthUnit_Close(AUGraph graph);
76 LONG CoreAudio_MIDIInit(void)
78 int i;
79 CHAR szPname[MAXPNAMELEN] = {0};
81 int numDest = MIDIGetNumberOfDestinations();
82 CFStringRef name = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("wineMIDIClient.%d"), getpid());
84 wineMIDIClient = CoreMIDI_CreateClient( name );
85 if (wineMIDIClient == NULL)
87 CFRelease(name);
88 ERR("can't create wineMIDIClient\n");
89 return 0;
91 CFRelease(name);
93 MIDIOut_NumDevs = MAX_MIDI_SYNTHS;
94 MIDIOut_NumDevs += numDest;
96 TRACE("MIDIOut_NumDevs %d\n", MIDIOut_NumDevs);
98 destinations = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MIDIOut_NumDevs * sizeof(MIDIDestination));
100 /* initialise MIDI synths */
101 for (i = 0; i < MAX_MIDI_SYNTHS; i++)
103 snprintf(szPname, sizeof(szPname), "CoreAudio MIDI Synth %d", i);
104 MultiByteToWideChar(CP_ACP, 0, szPname, -1, destinations[i].caps.szPname, sizeof(destinations[i].caps.szPname)/sizeof(WCHAR));
106 destinations[i].caps.wTechnology = MOD_SYNTH;
107 destinations[i].caps.wChannelMask = 0xFFFF;
109 destinations[i].caps.wMid = 0x00FF; /* Manufac ID */
110 destinations[i].caps.wPid = 0x0001; /* Product ID */
111 destinations[i].caps.vDriverVersion = 0x0001;
112 destinations[i].caps.dwSupport = MIDICAPS_VOLUME;
113 destinations[i].caps.wVoices = 16;
114 destinations[i].caps.wNotes = 16;
116 /* initialise available destinations */
117 for (i = MAX_MIDI_SYNTHS; i < numDest + MAX_MIDI_SYNTHS; i++)
119 MIDIEndpointRef endpoint = MIDIGetDestination(i - MAX_MIDI_SYNTHS);
121 CoreMIDI_GetObjectName(endpoint, szPname, sizeof(szPname));
122 MultiByteToWideChar(CP_ACP, 0, szPname, -1, destinations[i].caps.szPname, sizeof(destinations[i].caps.szPname)/sizeof(WCHAR));
124 name = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("WineOutputPort.%d.%u"), i, getpid());
125 MIDIOutputPortCreate(wineMIDIClient, name, &destinations[i].port);
126 CFRelease(name);
128 destinations[i].caps.wTechnology = MOD_MIDIPORT;
129 destinations[i].caps.wChannelMask = 0xFFFF;
131 destinations[i].caps.wMid = 0x00FF; /* Manufac ID */
132 destinations[i].caps.wPid = 0x0001;
133 destinations[i].caps.vDriverVersion = 0x0001;
134 destinations[i].caps.dwSupport = 0;
135 destinations[i].caps.wVoices = 16;
136 destinations[i].caps.wNotes = 16;
138 return 1;
141 LONG CoreAudio_MIDIRelease(void)
143 TRACE("\n");
144 if (wineMIDIClient) MIDIClientDispose(wineMIDIClient); /* MIDIClientDispose will close all ports */
145 HeapFree(GetProcessHeap(), 0, destinations);
146 return 1;
150 /**************************************************************************
151 * MIDI_NotifyClient [internal]
153 static DWORD MIDI_NotifyClient(UINT wDevID, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
155 DWORD dwCallBack;
156 UINT uFlags;
157 HANDLE hDev;
158 DWORD dwInstance;
160 TRACE("wDevID=%d wMsg=%d dwParm1=%04X dwParam2=%04X\n", wDevID, wMsg, dwParam1, dwParam2);
162 switch (wMsg) {
163 case MOM_OPEN:
164 case MOM_CLOSE:
165 case MOM_DONE:
166 case MOM_POSITIONCB:
167 dwCallBack = destinations[wDevID].midiDesc.dwCallback;
168 uFlags = destinations[wDevID].wFlags;
169 hDev = destinations[wDevID].midiDesc.hMidi;
170 dwInstance = destinations[wDevID].midiDesc.dwInstance;
171 break;
173 case MIM_OPEN:
174 case MIM_CLOSE:
175 case MIM_DATA:
176 case MIM_LONGDATA:
177 case MIM_ERROR:
178 case MIM_LONGERROR:
179 case MIM_MOREDATA:
180 default:
181 WARN("Unsupported MSW-MIDI message %u\n", wMsg);
182 return MMSYSERR_ERROR;
185 return DriverCallback(dwCallBack, uFlags, hDev, wMsg, dwInstance, dwParam1, dwParam2) ?
186 MMSYSERR_NOERROR : MMSYSERR_ERROR;
189 static DWORD MIDIOut_Open(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
191 MIDIDestination *dest;
193 TRACE("wDevID=%d lpDesc=%p dwFlags=%08x\n", wDevID, lpDesc, dwFlags);
195 if (lpDesc == NULL) {
196 WARN("Invalid Parameter\n");
197 return MMSYSERR_INVALPARAM;
200 if (wDevID >= MIDIOut_NumDevs) {
201 WARN("bad device ID : %d\n", wDevID);
202 return MMSYSERR_BADDEVICEID;
205 if (destinations[wDevID].midiDesc.hMidi != 0) {
206 WARN("device already open !\n");
207 return MMSYSERR_ALLOCATED;
210 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
211 WARN("bad dwFlags\n");
212 return MMSYSERR_INVALFLAG;
214 dest = &destinations[wDevID];
216 if (dest->caps.wTechnology == MOD_SYNTH)
218 if (!SynthUnit_CreateDefaultSynthUnit(&dest->graph, &dest->synth))
220 ERR("SynthUnit_CreateDefaultSynthUnit dest=%p failed\n", dest);
221 return MMSYSERR_ERROR;
224 if (!SynthUnit_Initialize(dest->synth, dest->graph))
226 ERR("SynthUnit_Initialise dest=%p failed\n", dest);
227 return MMSYSERR_ERROR;
230 else
232 FIXME("MOD_MIDIPORT\n");
234 dest->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
235 dest->midiDesc = *lpDesc;
237 return MIDI_NotifyClient(wDevID, MOM_OPEN, 0L, 0L);
240 static DWORD MIDIOut_Close(WORD wDevID)
242 DWORD ret = MMSYSERR_NOERROR;
244 TRACE("wDevID=%d\n", wDevID);
246 if (wDevID >= MIDIOut_NumDevs) {
247 WARN("bad device ID : %d\n", wDevID);
248 return MMSYSERR_BADDEVICEID;
251 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
252 SynthUnit_Close(destinations[wDevID].graph);
253 else
254 FIXME("MOD_MIDIPORT\n");
256 destinations[wDevID].graph = 0;
257 destinations[wDevID].synth = 0;
259 if (MIDI_NotifyClient(wDevID, MOM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
260 WARN("can't notify client !\n");
261 ret = MMSYSERR_INVALPARAM;
263 destinations[wDevID].midiDesc.hMidi = 0;
265 return ret;
268 static DWORD MIDIOut_Data(WORD wDevID, DWORD dwParam)
270 WORD evt = LOBYTE(LOWORD(dwParam));
271 WORD d1 = HIBYTE(LOWORD(dwParam));
272 WORD d2 = LOBYTE(HIWORD(dwParam));
273 UInt8 chn = (evt & 0x0F);
274 OSStatus err = noErr;
276 TRACE("wDevID=%d dwParam=%08X\n", wDevID, dwParam);
278 if (wDevID >= MIDIOut_NumDevs) {
279 WARN("bad device ID : %d\n", wDevID);
280 return MMSYSERR_BADDEVICEID;
283 TRACE("evt=%08x d1=%04x d2=%04x (evt & 0xF0)=%04x chn=%d\n", evt, d1, d2, (evt & 0xF0), chn);
285 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
287 err = MusicDeviceMIDIEvent(destinations[wDevID].synth, (evt & 0xF0) | chn, d1, d2, 0);
288 if (err != noErr)
290 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);
291 return MMSYSERR_ERROR;
294 else FIXME("MOD_MIDIPORT\n");
296 return MMSYSERR_NOERROR;
299 static DWORD MIDIOut_LongData(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
301 LPBYTE lpData;
302 OSStatus err = noErr;
304 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
306 /* Note: MS doc does not say much about the dwBytesRecorded member of the MIDIHDR structure
307 * but it seems to be used only for midi input.
308 * Taking a look at the WAVEHDR structure (which is quite similar) confirms this assumption.
311 if (wDevID >= MIDIOut_NumDevs) {
312 WARN("bad device ID : %d\n", wDevID);
313 return MMSYSERR_BADDEVICEID;
316 if (lpMidiHdr == NULL) {
317 WARN("Invalid Parameter\n");
318 return MMSYSERR_INVALPARAM;
321 lpData = (LPBYTE) lpMidiHdr->lpData;
323 if (lpData == NULL)
324 return MIDIERR_UNPREPARED;
325 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
326 return MIDIERR_UNPREPARED;
327 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
328 return MIDIERR_STILLPLAYING;
329 lpMidiHdr->dwFlags &= ~MHDR_DONE;
330 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
332 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
333 * data, or can it also contain raw MIDI data, to be split up and sent to
334 * modShortData() ?
335 * If the latest is true, then the following WARNing will fire up
337 if (lpData[0] != 0xF0 || lpData[lpMidiHdr->dwBufferLength - 1] != 0xF7) {
338 WARN("Alledged system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
341 TRACE("dwBufferLength=%u !\n", lpMidiHdr->dwBufferLength);
342 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
343 lpData[0], lpData[1], lpData[2], lpData[lpMidiHdr->dwBufferLength-3],
344 lpData[lpMidiHdr->dwBufferLength-2], lpData[lpMidiHdr->dwBufferLength-1]);
347 if (lpData[0] != 0xF0) {
348 /* System Exclusive */
349 ERR("Add missing 0xF0 marker at the beginning of system exclusive byte stream\n");
351 if (lpData[lpMidiHdr->dwBufferLength - 1] != 0xF7) {
352 /* Send end of System Exclusive */
353 ERR("Add missing 0xF7 marker at the end of system exclusive byte stream\n");
355 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH) /* FIXME */
357 err = MusicDeviceSysEx(destinations[wDevID].synth, (const UInt8 *) lpData, lpMidiHdr->dwBufferLength);
358 if (err != noErr)
360 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);
361 return MMSYSERR_ERROR;
364 else
366 FIXME("MOD_MIDIPORT\n");
369 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
370 lpMidiHdr->dwFlags |= MHDR_DONE;
371 if (MIDI_NotifyClient(wDevID, MOM_DONE, (DWORD)lpMidiHdr, 0L) != MMSYSERR_NOERROR) {
372 WARN("can't notify client !\n");
373 return MMSYSERR_INVALPARAM;
375 return MMSYSERR_NOERROR;
378 /**************************************************************************
379 * MIDIOut_Prepare [internal]
381 static DWORD MIDIOut_Prepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
383 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
385 if (wDevID >= MIDIOut_NumDevs) {
386 WARN("bad device ID : %d\n", wDevID);
387 return MMSYSERR_BADDEVICEID;
390 /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
391 * asks to prepare MIDIHDR which dwFlags != 0.
392 * So at least check for the inqueue flag
394 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
395 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0 ||
396 lpMidiHdr->dwBufferLength >= 0x10000ul) {
397 WARN("%p %p %08x %lu/%d\n", lpMidiHdr, lpMidiHdr->lpData,
398 lpMidiHdr->dwFlags, sizeof(MIDIHDR), dwSize);
399 return MMSYSERR_INVALPARAM;
402 lpMidiHdr->lpNext = 0;
403 lpMidiHdr->dwFlags |= MHDR_PREPARED;
404 lpMidiHdr->dwFlags &= ~MHDR_DONE;
405 return MMSYSERR_NOERROR;
408 /**************************************************************************
409 * MIDIOut_Unprepare [internal]
411 static DWORD MIDIOut_Unprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
413 TRACE("wDevID=%d lpMidiHdr=%p dwSize=%d\n", wDevID, lpMidiHdr, dwSize);
415 if (wDevID >= MIDIOut_NumDevs) {
416 WARN("bad device ID : %d\n", wDevID);
417 return MMSYSERR_BADDEVICEID;
419 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0)
420 return MMSYSERR_INVALPARAM;
421 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
422 return MIDIERR_STILLPLAYING;
424 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
426 return MMSYSERR_NOERROR;
429 static DWORD MIDIOut_GetDevCaps(WORD wDevID, LPMIDIOUTCAPSW lpCaps, DWORD dwSize)
431 TRACE("wDevID=%d lpCaps=%p dwSize=%d\n", wDevID, lpCaps, dwSize);
433 if (lpCaps == NULL) {
434 WARN("Invalid Parameter\n");
435 return MMSYSERR_INVALPARAM;
438 if (wDevID >= MIDIOut_NumDevs) {
439 WARN("bad device ID : %d\n", wDevID);
440 return MMSYSERR_BADDEVICEID;
442 memcpy(lpCaps, &destinations[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
443 return MMSYSERR_NOERROR;
446 static DWORD MIDIOut_GetNumDevs(void)
448 TRACE("\n");
449 return MIDIOut_NumDevs;
452 static DWORD MIDIOut_GetVolume(WORD wDevID, DWORD *lpdwVolume)
454 TRACE("%d\n", wDevID);
456 if (wDevID >= MIDIOut_NumDevs) {
457 WARN("bad device ID : %d\n", wDevID);
458 return MMSYSERR_BADDEVICEID;
460 if (lpdwVolume == NULL) {
461 WARN("Invalid Parameter\n");
462 return MMSYSERR_INVALPARAM;
465 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
467 float left;
468 float right;
469 AudioUnit_GetVolume(destinations[wDevID].synth, &left, &right);
471 *lpdwVolume = ((WORD) left * 0xFFFFl) + (((WORD) right * 0xFFFFl) << 16);
473 return MMSYSERR_NOERROR;
476 return MMSYSERR_NOTSUPPORTED;
479 static DWORD MIDIOut_SetVolume(WORD wDevID, DWORD dwVolume)
481 TRACE("%d\n", wDevID);
483 if (wDevID >= MIDIOut_NumDevs) {
484 WARN("bad device ID : %d\n", wDevID);
485 return MMSYSERR_BADDEVICEID;
487 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
489 float left;
490 float right;
492 left = LOWORD(dwVolume) / 65535.0f;
493 right = HIWORD(dwVolume) / 65535.0f;
494 AudioUnit_SetVolume(destinations[wDevID].synth, left, right);
496 return MMSYSERR_NOERROR;
499 return MMSYSERR_NOTSUPPORTED;
502 static DWORD MIDIOut_Reset(WORD wDevID)
504 unsigned chn;
506 TRACE("%d\n", wDevID);
508 if (wDevID >= MIDIOut_NumDevs) {
509 WARN("bad device ID : %d\n", wDevID);
510 return MMSYSERR_BADDEVICEID;
512 if (destinations[wDevID].caps.wTechnology == MOD_SYNTH)
514 for (chn = 0; chn < 16; chn++) {
515 /* turn off every note */
516 MusicDeviceMIDIEvent(destinations[wDevID].synth, 0xB0 | chn, 0x7B, 0, 0);
517 /* remove sustain on channel */
518 MusicDeviceMIDIEvent(destinations[wDevID].synth, 0xB0 | chn, 0x40, 0, 0);
521 else FIXME("MOD_MIDIPORT\n");
523 /* FIXME: the LongData buffers must also be returned to the app */
524 return MMSYSERR_NOERROR;
527 /**************************************************************************
528 * modMessage
530 DWORD WINAPI CoreAudio_modMessage(UINT wDevID, UINT wMsg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2)
532 TRACE("%d %08x %08x %08x %08x\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
534 switch (wMsg) {
535 case DRVM_INIT:
536 case DRVM_EXIT:
537 case DRVM_ENABLE:
538 case DRVM_DISABLE:
539 return 0;
540 case MODM_OPEN:
541 return MIDIOut_Open(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
542 case MODM_CLOSE:
543 return MIDIOut_Close(wDevID);
544 case MODM_DATA:
545 return MIDIOut_Data(wDevID, dwParam1);
546 case MODM_LONGDATA:
547 return MIDIOut_LongData(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
548 case MODM_PREPARE:
549 return MIDIOut_Prepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
550 case MODM_UNPREPARE:
551 return MIDIOut_Unprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
552 case MODM_GETDEVCAPS:
553 return MIDIOut_GetDevCaps(wDevID, (LPMIDIOUTCAPSW) dwParam1, dwParam2);
554 case MODM_GETNUMDEVS:
555 return MIDIOut_GetNumDevs();
556 case MODM_GETVOLUME:
557 return MIDIOut_GetVolume(wDevID, (DWORD*)dwParam1);
558 case MODM_SETVOLUME:
559 return MIDIOut_SetVolume(wDevID, dwParam1);
560 case MODM_RESET:
561 return MIDIOut_Reset(wDevID);
562 default:
563 TRACE("Unsupported message (08%x)\n", wMsg);
565 return MMSYSERR_NOTSUPPORTED;
568 #else
570 DWORD WINAPI CoreAudio_modMessage(UINT wDevID, UINT wMsg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2)
572 TRACE("%08x, %08x, %08x, %08x, %08x\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
573 return MMSYSERR_NOTENABLED;
576 #endif