msdaps: Add support for remoting IRowset_GetNextRows.
[wine/multimedia.git] / dlls / midimap / midimap.c
blob129427fa94c65ade01ebf7fd45804090856b0236
1 /*
2 * Wine MIDI mapper driver
4 * Copyright 1999, 2000, 2001 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * TODO:
21 * notification has to be implemented
22 * IDF file loading
25 #include <stdarg.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "mmddk.h"
33 #include "winreg.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
38 * Here's how Windows stores the midiOut mapping information.
40 * Full form (in HKU) is:
42 * [Software\\Microsoft\\Windows\\CurrentVersion\\Multimedia\\MIDIMap] 988836060
43 * "AutoScheme"=dword:00000000
44 * "ConfigureCount"=dword:00000004
45 * "CurrentInstrument"="Wine OSS midi"
46 * "CurrentScheme"="epp"
47 * "DriverList"=""
48 * "UseScheme"=dword:00000000
50 * AutoScheme: ?
51 * CurrentInstrument: name of midiOut device to use when UseScheme is 0. Wine uses an extension
52 * of the form #n to link to n'th midiOut device of the system
53 * CurrentScheme: when UseScheme is non null, it's the scheme to use (see below)
54 * DriverList: ?
55 * UseScheme: trigger for simple/complex mapping
57 * A scheme is defined (in HKLM) as:
59 * [System\\CurrentControlSet\\Control\\MediaProperties\\PrivateProperties\\Midi\\Schemes\\<nameScheme>]
60 * <nameScheme>: one key for each defined scheme (system wide)
61 * under each one of these <nameScheme> keys, there's:
62 * [...\\<nameScheme>\\<idxDevice>]
63 * "Channels"="<bitMask>"
64 * (the default value of this key also refers to the name of the device).
66 * this defines, for each midiOut device (identified by its index in <idxDevice>), which
67 * channels have to be mapped onto it. The <bitMask> defines the channels (from 0 to 15)
68 * will be mapped (mapping occurs for channel <ch> if bit <ch> is set in <bitMask>
70 * Further mapping information can also be defined in:
71 * [System\\CurrentControlSet\\Control\\MediaProperties\\PrivateProperties\\Midi\\Ports\\<nameDevice>\\Instruments\\<idx>]
72 * "Definition"="<.idf file>"
73 * "FriendlyName"="#for .idx file#"
74 * "Port"="<idxPort>"
76 * This last part isn't implemented (.idf file support).
79 WINE_DEFAULT_DEBUG_CHANNEL(msacm);
81 typedef struct tagMIDIOUTPORT
83 WCHAR name[MAXPNAMELEN];
84 int loaded;
85 HMIDIOUT hMidi;
86 unsigned short uDevID;
87 LPBYTE lpbPatch;
88 unsigned int aChn[16];
89 } MIDIOUTPORT;
91 typedef struct tagMIDIMAPDATA
93 struct tagMIDIMAPDATA* self;
94 MIDIOUTPORT* ChannelMap[16];
95 } MIDIMAPDATA;
97 static MIDIOUTPORT* midiOutPorts;
98 static unsigned numMidiOutPorts;
100 static BOOL MIDIMAP_IsBadData(MIDIMAPDATA* mm)
102 if (!IsBadReadPtr(mm, sizeof(MIDIMAPDATA)) && mm->self == mm)
103 return FALSE;
104 TRACE("Bad midimap data (%p)\n", mm);
105 return TRUE;
108 static BOOL MIDIMAP_FindPort(const WCHAR* name, unsigned* dev)
110 for (*dev = 0; *dev < numMidiOutPorts; (*dev)++)
112 TRACE("%s\n", wine_dbgstr_w(midiOutPorts[*dev].name));
113 if (strcmpW(midiOutPorts[*dev].name, name) == 0)
114 return TRUE;
116 /* try the form #nnn */
117 if (*name == '#' && isdigit(name[1]))
119 const WCHAR* ptr = name + 1;
120 *dev = 0;
123 *dev = *dev * 10 + *ptr - '0';
124 } while (isdigit(*++ptr));
125 if (*dev < numMidiOutPorts)
126 return TRUE;
128 return FALSE;
131 static BOOL MIDIMAP_LoadSettingsDefault(MIDIMAPDATA* mom, const WCHAR* port)
133 unsigned i, dev = 0;
135 if (port != NULL && !MIDIMAP_FindPort(port, &dev))
137 ERR("Registry glitch: couldn't find midi out (%s)\n", wine_dbgstr_w(port));
138 dev = 0;
141 /* this is necessary when no midi out ports are present */
142 if (dev >= numMidiOutPorts)
143 return FALSE;
144 /* sets default */
145 for (i = 0; i < 16; i++) mom->ChannelMap[i] = &midiOutPorts[dev];
147 return TRUE;
150 static BOOL MIDIMAP_LoadSettingsScheme(MIDIMAPDATA* mom, const WCHAR* scheme)
152 HKEY hSchemesKey, hKey, hPortKey;
153 unsigned i, idx, dev;
154 WCHAR buffer[256], port[256];
155 DWORD type, size, mask;
157 for (i = 0; i < 16; i++) mom->ChannelMap[i] = NULL;
159 if (RegOpenKeyA(HKEY_LOCAL_MACHINE,
160 "System\\CurrentControlSet\\Control\\MediaProperties\\PrivateProperties\\Midi\\Schemes",
161 &hSchemesKey))
163 return FALSE;
165 if (RegOpenKeyW(hSchemesKey, scheme, &hKey))
167 RegCloseKey(hSchemesKey);
168 return FALSE;
171 for (idx = 0; !RegEnumKeyW(hKey, idx, buffer, sizeof(buffer)/sizeof(buffer[0])); idx++)
173 if (RegOpenKeyW(hKey, buffer, &hPortKey)) continue;
175 size = sizeof(port);
176 if (RegQueryValueExW(hPortKey, NULL, 0, &type, (void*)port, &size)) continue;
178 if (!MIDIMAP_FindPort(port, &dev)) continue;
180 size = sizeof(mask);
181 if (RegQueryValueExA(hPortKey, "Channels", 0, &type, (void*)&mask, &size))
182 continue;
184 for (i = 0; i < 16; i++)
186 if (mask & (1 << i))
188 if (mom->ChannelMap[i])
189 ERR("Quirks in registry, channel %u is mapped twice\n", i);
190 mom->ChannelMap[i] = &midiOutPorts[dev];
195 RegCloseKey(hSchemesKey);
196 RegCloseKey(hKey);
198 return TRUE;
201 static BOOL MIDIMAP_LoadSettings(MIDIMAPDATA* mom)
203 HKEY hKey;
204 BOOL ret;
206 if (RegOpenKeyA(HKEY_CURRENT_USER,
207 "Software\\Microsoft\\Windows\\CurrentVersion\\Multimedia\\MIDIMap", &hKey))
209 ret = MIDIMAP_LoadSettingsDefault(mom, NULL);
211 else
213 DWORD type, size, out;
214 WCHAR buffer[256];
216 ret = 2;
217 size = sizeof(out);
218 if (!RegQueryValueExA(hKey, "UseScheme", 0, &type, (void*)&out, &size) && out)
220 static const WCHAR cs[] = {'C','u','r','r','e','n','t','S','c','h','e','m','e',0};
221 size = sizeof(buffer);
222 if (!RegQueryValueExW(hKey, cs, 0, &type, (void*)buffer, &size))
224 if (!(ret = MIDIMAP_LoadSettingsScheme(mom, buffer)))
225 ret = MIDIMAP_LoadSettingsDefault(mom, NULL);
227 else
229 ERR("Wrong registry: UseScheme is active, but no CurrentScheme found\n");
232 if (ret == 2)
234 static const WCHAR ci[] = {'C','u','r','r','e','n','t','I','n','s','t','r','u','m','e','n','t',0};
235 size = sizeof(buffer);
236 if (!RegQueryValueExW(hKey, ci, 0, &type, (void*)buffer, &size) && *buffer)
238 ret = MIDIMAP_LoadSettingsDefault(mom, buffer);
240 else
242 ret = MIDIMAP_LoadSettingsDefault(mom, NULL);
246 RegCloseKey(hKey);
248 if (ret && TRACE_ON(msacm))
250 unsigned i;
252 for (i = 0; i < 16; i++)
254 TRACE("chnMap[%2d] => %d\n",
255 i, mom->ChannelMap[i] ? mom->ChannelMap[i]->uDevID : -1);
258 return ret;
261 static DWORD modOpen(DWORD_PTR *lpdwUser, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
263 MIDIMAPDATA* mom = HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIMAPDATA));
265 TRACE("(%p %p %08x)\n", lpdwUser, lpDesc, dwFlags);
267 if (!mom) return MMSYSERR_NOMEM;
269 if (HIWORD(dwFlags & CALLBACK_TYPEMASK)) {
270 FIXME("NIY callback flags %08x\n", dwFlags);
271 HeapFree(GetProcessHeap(), 0, mom);
272 return MMSYSERR_INVALFLAG;
275 if (MIDIMAP_LoadSettings(mom))
277 *lpdwUser = (DWORD_PTR)mom;
278 mom->self = mom;
280 return MMSYSERR_NOERROR;
282 HeapFree(GetProcessHeap(), 0, mom);
283 return MIDIERR_INVALIDSETUP;
286 static DWORD modClose(MIDIMAPDATA* mom)
288 UINT i;
289 DWORD ret = MMSYSERR_NOERROR;
291 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
293 for (i = 0; i < 16; i++)
295 DWORD t;
296 if (mom->ChannelMap[i] && mom->ChannelMap[i]->loaded > 0)
298 t = midiOutClose(mom->ChannelMap[i]->hMidi);
299 if (t == MMSYSERR_NOERROR)
301 mom->ChannelMap[i]->loaded = 0;
302 mom->ChannelMap[i]->hMidi = 0;
304 else if (ret == MMSYSERR_NOERROR)
305 ret = t;
308 if (ret == MMSYSERR_NOERROR)
309 HeapFree(GetProcessHeap(), 0, mom);
310 return ret;
313 static DWORD modLongData(MIDIMAPDATA* mom, LPMIDIHDR lpMidiHdr, DWORD_PTR dwParam2)
315 WORD chn;
316 DWORD ret = MMSYSERR_NOERROR;
317 MIDIHDR mh;
319 if (MIDIMAP_IsBadData(mom))
320 return MMSYSERR_ERROR;
321 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
322 return MIDIERR_UNPREPARED;
323 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
324 return MIDIERR_STILLPLAYING;
326 mh = *lpMidiHdr;
327 lpMidiHdr->dwFlags &= ~MHDR_DONE;
328 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
329 for (chn = 0; chn < 16; chn++)
331 if (mom->ChannelMap[chn] && mom->ChannelMap[chn]->loaded > 0)
333 mh.dwFlags = 0;
334 midiOutPrepareHeader(mom->ChannelMap[chn]->hMidi, &mh, sizeof(mh));
335 ret = midiOutLongMsg(mom->ChannelMap[chn]->hMidi, &mh, sizeof(mh));
336 /* As of 2009, wineXYZ.drv's LongData handlers are synchronous */
337 if (!ret && !(mh.dwFlags & MHDR_DONE))
338 FIXME("wait until MHDR_DONE\n");
339 midiOutUnprepareHeader(mom->ChannelMap[chn]->hMidi, &mh, sizeof(mh));
340 if (ret != MMSYSERR_NOERROR) break;
343 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
344 lpMidiHdr->dwFlags |= MHDR_DONE;
345 return ret;
348 static DWORD modData(MIDIMAPDATA* mom, DWORD_PTR dwParam)
350 BYTE lb = LOBYTE(LOWORD(dwParam));
351 WORD chn = lb & 0x0F;
352 DWORD ret = MMSYSERR_NOERROR;
354 if (MIDIMAP_IsBadData(mom))
355 return MMSYSERR_ERROR;
357 if (!mom->ChannelMap[chn]) return MMSYSERR_NOERROR;
359 switch (lb & 0xF0)
361 case 0x80:
362 case 0x90:
363 case 0xA0:
364 case 0xB0:
365 case 0xC0:
366 case 0xD0:
367 case 0xE0:
368 if (mom->ChannelMap[chn]->loaded == 0)
370 if (midiOutOpen(&mom->ChannelMap[chn]->hMidi, mom->ChannelMap[chn]->uDevID,
371 0L, 0L, CALLBACK_NULL) == MMSYSERR_NOERROR)
372 mom->ChannelMap[chn]->loaded = 1;
373 else
374 mom->ChannelMap[chn]->loaded = -1;
375 /* FIXME: should load here the IDF midi data... and allow channel and
376 * patch mappings
379 if (mom->ChannelMap[chn]->loaded > 0)
381 /* change channel */
382 dwParam &= ~0x0F;
383 dwParam |= mom->ChannelMap[chn]->aChn[chn];
385 if ((LOBYTE(LOWORD(dwParam)) & 0xF0) == 0xC0 /* program change */ &&
386 mom->ChannelMap[chn]->lpbPatch)
388 BYTE patch = HIBYTE(LOWORD(dwParam));
390 /* change patch */
391 dwParam &= ~0x0000FF00;
392 dwParam |= mom->ChannelMap[chn]->lpbPatch[patch];
394 ret = midiOutShortMsg(mom->ChannelMap[chn]->hMidi, dwParam);
396 break;
397 case 0xF0:
398 for (chn = 0; chn < 16; chn++)
400 if (mom->ChannelMap[chn]->loaded > 0)
401 ret = midiOutShortMsg(mom->ChannelMap[chn]->hMidi, dwParam);
403 break;
404 default:
405 FIXME("ooch %lx\n", dwParam);
408 return ret;
411 static DWORD modPrepare(MIDIMAPDATA* mom, LPMIDIHDR lpMidiHdr, DWORD_PTR dwSize)
413 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
414 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
415 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE))
416 return MMSYSERR_INVALPARAM;
418 lpMidiHdr->dwFlags |= MHDR_PREPARED;
419 lpMidiHdr->dwFlags &= ~MHDR_DONE;
420 return MMSYSERR_NOERROR;
423 static DWORD modUnprepare(MIDIMAPDATA* mom, LPMIDIHDR lpMidiHdr, DWORD_PTR dwParam2)
425 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
426 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
427 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
429 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
430 return MMSYSERR_NOERROR;
433 static DWORD modGetVolume(MIDIMAPDATA* mom, DWORD* lpdwVolume)
435 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
436 if (!lpdwVolume) return MMSYSERR_INVALPARAM;
437 *lpdwVolume = 0xFFFFFFFF; /* tests show this initial value */
438 return MMSYSERR_NOERROR;
441 static DWORD modSetVolume(MIDIMAPDATA* mom, DWORD dwVolume)
443 /* Native forwards it to some underlying device
444 * GetVolume returns what was last set here. */
445 FIXME("stub\n");
446 return MMSYSERR_NOERROR;
449 static DWORD modGetDevCaps(UINT wDevID, MIDIMAPDATA* mom, LPMIDIOUTCAPSW lpMidiCaps, DWORD_PTR size)
451 static const WCHAR name[] = {'W','i','n','e',' ','m','i','d','i',' ','m','a','p','p','e','r',0};
452 lpMidiCaps->wMid = 0x00FF;
453 lpMidiCaps->wPid = 0x0001;
454 lpMidiCaps->vDriverVersion = 0x0100;
455 lstrcpyW(lpMidiCaps->szPname, name);
456 lpMidiCaps->wTechnology = MOD_MAPPER;
457 lpMidiCaps->wVoices = 0;
458 lpMidiCaps->wNotes = 0;
459 lpMidiCaps->wChannelMask = 0xFFFF;
460 /* Native returns volume caps of underlying device | MIDICAPS_STREAM */
461 lpMidiCaps->dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
463 return MMSYSERR_NOERROR;
466 static DWORD modReset(MIDIMAPDATA* mom)
468 WORD chn;
469 DWORD ret = MMSYSERR_NOERROR;
471 if (MIDIMAP_IsBadData(mom))
472 return MMSYSERR_ERROR;
474 for (chn = 0; chn < 16; chn++)
476 if (mom->ChannelMap[chn] && mom->ChannelMap[chn]->loaded > 0)
478 ret = midiOutReset(mom->ChannelMap[chn]->hMidi);
479 if (ret != MMSYSERR_NOERROR) break;
482 return ret;
485 /**************************************************************************
486 * modMessage (MIDIMAP.@)
488 DWORD WINAPI MIDIMAP_modMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
489 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
491 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
492 wDevID, wMsg, dwUser, dwParam1, dwParam2);
494 switch (wMsg)
496 case DRVM_INIT:
497 case DRVM_EXIT:
498 case DRVM_ENABLE:
499 case DRVM_DISABLE:
500 /* FIXME: Pretend this is supported */
501 return 0;
503 case MODM_OPEN: return modOpen((DWORD_PTR *)dwUser, (LPMIDIOPENDESC)dwParam1, dwParam2);
504 case MODM_CLOSE: return modClose ((MIDIMAPDATA*)dwUser);
506 case MODM_DATA: return modData ((MIDIMAPDATA*)dwUser, dwParam1);
507 case MODM_LONGDATA: return modLongData ((MIDIMAPDATA*)dwUser, (LPMIDIHDR)dwParam1, dwParam2);
508 case MODM_PREPARE: return modPrepare ((MIDIMAPDATA*)dwUser, (LPMIDIHDR)dwParam1, dwParam2);
509 case MODM_UNPREPARE: return modUnprepare ((MIDIMAPDATA*)dwUser, (LPMIDIHDR)dwParam1, dwParam2);
510 case MODM_RESET: return modReset ((MIDIMAPDATA*)dwUser);
512 case MODM_GETDEVCAPS: return modGetDevCaps (wDevID, (MIDIMAPDATA*)dwUser, (LPMIDIOUTCAPSW)dwParam1,dwParam2);
513 case MODM_GETNUMDEVS: return 1;
514 case MODM_GETVOLUME: return modGetVolume ((MIDIMAPDATA*)dwUser, (DWORD*)dwParam1);
515 case MODM_SETVOLUME: return modSetVolume ((MIDIMAPDATA*)dwUser, dwParam1);
516 default:
517 FIXME("unknown message %d!\n", wMsg);
519 return MMSYSERR_NOTSUPPORTED;
522 /*======================================================================*
523 * Driver part *
524 *======================================================================*/
526 /**************************************************************************
527 * MIDIMAP_drvOpen [internal]
529 static LRESULT MIDIMAP_drvOpen(LPSTR str)
531 MIDIOUTCAPSW moc;
532 unsigned dev, i;
534 if (midiOutPorts)
535 return 0;
537 numMidiOutPorts = midiOutGetNumDevs();
538 midiOutPorts = HeapAlloc(GetProcessHeap(), 0,
539 numMidiOutPorts * sizeof(MIDIOUTPORT));
540 for (dev = 0; dev < numMidiOutPorts; dev++)
542 if (midiOutGetDevCapsW(dev, &moc, sizeof(moc)) == 0L)
544 strcpyW(midiOutPorts[dev].name, moc.szPname);
545 midiOutPorts[dev].loaded = 0;
546 midiOutPorts[dev].hMidi = 0;
547 midiOutPorts[dev].uDevID = dev;
548 midiOutPorts[dev].lpbPatch = NULL;
549 for (i = 0; i < 16; i++)
550 midiOutPorts[dev].aChn[i] = i;
552 else
554 midiOutPorts[dev].loaded = -1;
558 return 1;
561 /**************************************************************************
562 * MIDIMAP_drvClose [internal]
564 static LRESULT MIDIMAP_drvClose(DWORD_PTR dwDevID)
566 if (midiOutPorts)
568 HeapFree(GetProcessHeap(), 0, midiOutPorts);
569 midiOutPorts = NULL;
570 return 1;
572 return 0;
575 /**************************************************************************
576 * DriverProc (MIDIMAP.@)
578 LRESULT CALLBACK MIDIMAP_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
579 LPARAM dwParam1, LPARAM dwParam2)
581 /* EPP TRACE("(%08lX, %04X, %08lX, %08lX, %08lX)\n", */
582 /* EPP dwDevID, hDriv, wMsg, dwParam1, dwParam2); */
584 switch (wMsg)
586 case DRV_LOAD: return 1;
587 case DRV_FREE: return 1;
588 case DRV_OPEN: return MIDIMAP_drvOpen((LPSTR)dwParam1);
589 case DRV_CLOSE: return MIDIMAP_drvClose(dwDevID);
590 case DRV_ENABLE: return 1;
591 case DRV_DISABLE: return 1;
592 case DRV_QUERYCONFIGURE: return 1;
593 case DRV_CONFIGURE: MessageBoxA(0, "MIDIMAP MultiMedia Driver !", "OSS Driver", MB_OK); return 1;
594 case DRV_INSTALL: return DRVCNF_RESTART;
595 case DRV_REMOVE: return DRVCNF_RESTART;
596 default:
597 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);