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
21 * notification has to be implemented
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"
48 * "UseScheme"=dword:00000000
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)
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#"
76 * This last part isn't implemented (.idf file support).
79 WINE_DEFAULT_DEBUG_CHANNEL(msacm);
81 typedef struct tagMIDIOUTPORT
83 WCHAR name[MAXPNAMELEN];
86 unsigned short uDevID;
88 unsigned int aChn[16];
91 typedef struct tagMIDIMAPDATA
93 struct tagMIDIMAPDATA* self;
94 MIDIOUTPORT* ChannelMap[16];
97 static MIDIOUTPORT* midiOutPorts;
98 static unsigned numMidiOutPorts;
100 static BOOL MIDIMAP_IsBadData(MIDIMAPDATA* mm)
102 if (!IsBadReadPtr(mm, sizeof(MIDIMAPDATA)) && mm->self == mm)
104 TRACE("Bad midimap data (%p)\n", mm);
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)
116 /* try the form #nnn */
117 if (*name == '#' && isdigit(name[1]))
119 const WCHAR* ptr = name + 1;
123 *dev = *dev * 10 + *ptr - '0';
124 } while (isdigit(*++ptr));
125 if (*dev < numMidiOutPorts)
131 static BOOL MIDIMAP_LoadSettingsDefault(MIDIMAPDATA* mom, const WCHAR* port)
135 if (port != NULL && !MIDIMAP_FindPort(port, &dev))
137 ERR("Registry glitch: couldn't find midi out (%s)\n", wine_dbgstr_w(port));
141 /* this is necessary when no midi out ports are present */
142 if (dev >= numMidiOutPorts)
145 for (i = 0; i < 16; i++) mom->ChannelMap[i] = &midiOutPorts[dev];
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",
165 if (RegOpenKeyW(hSchemesKey, scheme, &hKey))
167 RegCloseKey(hSchemesKey);
171 for (idx = 0; !RegEnumKeyW(hKey, idx, buffer, sizeof(buffer)); idx++)
173 if (RegOpenKeyW(hKey, buffer, &hPortKey)) continue;
176 if (RegQueryValueExW(hPortKey, NULL, 0, &type, (void*)port, &size)) continue;
178 if (!MIDIMAP_FindPort(port, &dev)) continue;
181 if (RegQueryValueExA(hPortKey, "Channels", 0, &type, (void*)&mask, &size))
184 for (i = 0; i < 16; 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);
201 static BOOL MIDIMAP_LoadSettings(MIDIMAPDATA* mom)
206 if (RegOpenKeyA(HKEY_CURRENT_USER,
207 "Software\\Microsoft\\Windows\\CurrentVersion\\Multimedia\\MIDIMap", &hKey))
209 ret = MIDIMAP_LoadSettingsDefault(mom, NULL);
213 DWORD type, size, 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);
229 ERR("Wrong registry: UseScheme is active, but no CurrentScheme found\n");
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);
242 ret = MIDIMAP_LoadSettingsDefault(mom, NULL);
248 if (ret && TRACE_ON(msacm))
252 for (i = 0; i < 16; i++)
254 TRACE("chnMap[%2d] => %d\n",
255 i, mom->ChannelMap[i] ? mom->ChannelMap[i]->uDevID : -1);
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 (MIDIMAP_LoadSettings(mom))
271 *lpdwUser = (DWORD_PTR)mom;
274 return MMSYSERR_NOERROR;
276 HeapFree(GetProcessHeap(), 0, mom);
277 return MIDIERR_INVALIDSETUP;
280 static DWORD modClose(MIDIMAPDATA* mom)
283 DWORD ret = MMSYSERR_NOERROR;
285 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
287 for (i = 0; i < 16; i++)
290 if (mom->ChannelMap[i] && mom->ChannelMap[i]->loaded > 0)
292 t = midiOutClose(mom->ChannelMap[i]->hMidi);
293 if (t == MMSYSERR_NOERROR)
295 mom->ChannelMap[i]->loaded = 0;
296 mom->ChannelMap[i]->hMidi = 0;
298 else if (ret == MMSYSERR_NOERROR)
302 if (ret == MMSYSERR_NOERROR)
303 HeapFree(GetProcessHeap(), 0, mom);
307 static DWORD modLongData(MIDIMAPDATA* mom, LPMIDIHDR lpMidiHdr, DWORD_PTR dwParam2)
310 DWORD ret = MMSYSERR_NOERROR;
313 if (MIDIMAP_IsBadData(mom))
314 return MMSYSERR_ERROR;
317 for (chn = 0; chn < 16; chn++)
319 if (mom->ChannelMap[chn] && mom->ChannelMap[chn]->loaded > 0)
322 midiOutPrepareHeader(mom->ChannelMap[chn]->hMidi, &mh, sizeof(mh));
323 ret = midiOutLongMsg(mom->ChannelMap[chn]->hMidi, &mh, sizeof(mh));
324 midiOutUnprepareHeader(mom->ChannelMap[chn]->hMidi, &mh, sizeof(mh));
325 if (ret != MMSYSERR_NOERROR) break;
331 static DWORD modData(MIDIMAPDATA* mom, DWORD_PTR dwParam)
333 BYTE lb = LOBYTE(LOWORD(dwParam));
334 WORD chn = lb & 0x0F;
335 DWORD ret = MMSYSERR_NOERROR;
337 if (MIDIMAP_IsBadData(mom))
338 return MMSYSERR_ERROR;
340 if (!mom->ChannelMap[chn]) return MMSYSERR_NOERROR;
351 if (mom->ChannelMap[chn]->loaded == 0)
353 if (midiOutOpen(&mom->ChannelMap[chn]->hMidi, mom->ChannelMap[chn]->uDevID,
354 0L, 0L, CALLBACK_NULL) == MMSYSERR_NOERROR)
355 mom->ChannelMap[chn]->loaded = 1;
357 mom->ChannelMap[chn]->loaded = -1;
358 /* FIXME: should load here the IDF midi data... and allow channel and
362 if (mom->ChannelMap[chn]->loaded > 0)
366 dwParam |= mom->ChannelMap[chn]->aChn[chn];
368 if ((LOBYTE(LOWORD(dwParam)) & 0xF0) == 0xC0 /* program change */ &&
369 mom->ChannelMap[chn]->lpbPatch)
371 BYTE patch = HIBYTE(LOWORD(dwParam));
374 dwParam &= ~0x0000FF00;
375 dwParam |= mom->ChannelMap[chn]->lpbPatch[patch];
377 ret = midiOutShortMsg(mom->ChannelMap[chn]->hMidi, dwParam);
381 for (chn = 0; chn < 16; chn++)
383 if (mom->ChannelMap[chn]->loaded > 0)
384 ret = midiOutShortMsg(mom->ChannelMap[chn]->hMidi, dwParam);
388 FIXME("ooch %lx\n", dwParam);
394 static DWORD modPrepare(MIDIMAPDATA* mom, LPMIDIHDR lpMidiHdr, DWORD_PTR dwParam2)
396 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
397 if (lpMidiHdr->dwFlags & (MHDR_ISSTRM|MHDR_PREPARED))
398 return MMSYSERR_INVALPARAM;
400 lpMidiHdr->dwFlags |= MHDR_PREPARED;
401 return MMSYSERR_NOERROR;
404 static DWORD modUnprepare(MIDIMAPDATA* mom, LPMIDIHDR lpMidiHdr, DWORD_PTR dwParam2)
406 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
407 if ((lpMidiHdr->dwFlags & MHDR_ISSTRM) || !(lpMidiHdr->dwFlags & MHDR_PREPARED))
408 return MMSYSERR_INVALPARAM;
410 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
411 return MMSYSERR_NOERROR;
414 static DWORD modGetDevCaps(UINT wDevID, MIDIMAPDATA* mom, LPMIDIOUTCAPSW lpMidiCaps, DWORD_PTR size)
416 static const WCHAR name[] = {'W','i','n','e',' ','m','i','d','i',' ','m','a','p','p','e','r',0};
417 lpMidiCaps->wMid = 0x00FF;
418 lpMidiCaps->wPid = 0x0001;
419 lpMidiCaps->vDriverVersion = 0x0100;
420 lstrcpyW(lpMidiCaps->szPname, name);
421 lpMidiCaps->wTechnology = MOD_MAPPER;
422 lpMidiCaps->wVoices = 0;
423 lpMidiCaps->wNotes = 0;
424 lpMidiCaps->wChannelMask = 0xFFFF;
425 lpMidiCaps->dwSupport = 0L;
427 return MMSYSERR_NOERROR;
430 static DWORD modReset(MIDIMAPDATA* mom)
433 DWORD ret = MMSYSERR_NOERROR;
435 if (MIDIMAP_IsBadData(mom))
436 return MMSYSERR_ERROR;
438 for (chn = 0; chn < 16; chn++)
440 if (mom->ChannelMap[chn] && mom->ChannelMap[chn]->loaded > 0)
442 ret = midiOutReset(mom->ChannelMap[chn]->hMidi);
443 if (ret != MMSYSERR_NOERROR) break;
449 /**************************************************************************
450 * modMessage (MIDIMAP.@)
452 DWORD WINAPI MIDIMAP_modMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
453 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
455 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
456 wDevID, wMsg, dwUser, dwParam1, dwParam2);
464 /* FIXME: Pretend this is supported */
467 case MODM_OPEN: return modOpen((DWORD_PTR *)dwUser, (LPMIDIOPENDESC)dwParam1, dwParam2);
468 case MODM_CLOSE: return modClose ((MIDIMAPDATA*)dwUser);
470 case MODM_DATA: return modData ((MIDIMAPDATA*)dwUser, dwParam1);
471 case MODM_LONGDATA: return modLongData ((MIDIMAPDATA*)dwUser, (LPMIDIHDR)dwParam1, dwParam2);
472 case MODM_PREPARE: return modPrepare ((MIDIMAPDATA*)dwUser, (LPMIDIHDR)dwParam1, dwParam2);
473 case MODM_UNPREPARE: return modUnprepare ((MIDIMAPDATA*)dwUser, (LPMIDIHDR)dwParam1, dwParam2);
474 case MODM_RESET: return modReset ((MIDIMAPDATA*)dwUser);
476 case MODM_GETDEVCAPS: return modGetDevCaps (wDevID, (MIDIMAPDATA*)dwUser, (LPMIDIOUTCAPSW)dwParam1,dwParam2);
477 case MODM_GETNUMDEVS: return 1;
478 case MODM_GETVOLUME: return MMSYSERR_NOTSUPPORTED;
479 case MODM_SETVOLUME: return MMSYSERR_NOTSUPPORTED;
481 FIXME("unknown message %d!\n", wMsg);
483 return MMSYSERR_NOTSUPPORTED;
486 /*======================================================================*
488 *======================================================================*/
490 /**************************************************************************
491 * MIDIMAP_drvOpen [internal]
493 static LRESULT MIDIMAP_drvOpen(LPSTR str)
501 numMidiOutPorts = midiOutGetNumDevs();
502 midiOutPorts = HeapAlloc(GetProcessHeap(), 0,
503 numMidiOutPorts * sizeof(MIDIOUTPORT));
504 for (dev = 0; dev < numMidiOutPorts; dev++)
506 if (midiOutGetDevCapsW(dev, &moc, sizeof(moc)) == 0L)
508 strcpyW(midiOutPorts[dev].name, moc.szPname);
509 midiOutPorts[dev].loaded = 0;
510 midiOutPorts[dev].hMidi = 0;
511 midiOutPorts[dev].uDevID = dev;
512 midiOutPorts[dev].lpbPatch = NULL;
513 for (i = 0; i < 16; i++)
514 midiOutPorts[dev].aChn[i] = i;
518 midiOutPorts[dev].loaded = -1;
525 /**************************************************************************
526 * MIDIMAP_drvClose [internal]
528 static LRESULT MIDIMAP_drvClose(DWORD_PTR dwDevID)
532 HeapFree(GetProcessHeap(), 0, midiOutPorts);
539 /**************************************************************************
540 * DriverProc (MIDIMAP.@)
542 LRESULT CALLBACK MIDIMAP_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
543 LPARAM dwParam1, LPARAM dwParam2)
545 /* EPP TRACE("(%08lX, %04X, %08lX, %08lX, %08lX)\n", */
546 /* EPP dwDevID, hDriv, wMsg, dwParam1, dwParam2); */
550 case DRV_LOAD: return 1;
551 case DRV_FREE: return 1;
552 case DRV_OPEN: return MIDIMAP_drvOpen((LPSTR)dwParam1);
553 case DRV_CLOSE: return MIDIMAP_drvClose(dwDevID);
554 case DRV_ENABLE: return 1;
555 case DRV_DISABLE: return 1;
556 case DRV_QUERYCONFIGURE: return 1;
557 case DRV_CONFIGURE: MessageBoxA(0, "MIDIMAP MultiMedia Driver !", "OSS Driver", MB_OK); return 1;
558 case DRV_INSTALL: return DRVCNF_RESTART;
559 case DRV_REMOVE: return DRVCNF_RESTART;
561 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);