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/debug.h"
37 * Here's how Windows stores the midiOut mapping information.
39 * Windows XP form (in HKU) is:
41 * [Software\\Microsoft\\Windows\\CurrentVersion\\Multimedia\\MIDIMap]
42 * "szPname"="TiMidity port 0"
45 * szPname: name of midiOut device to use.
47 * If szPname isn't defined, we use Windows 2000 form (also in HKU):
49 * [Software\\Microsoft\\Windows\\CurrentVersion\\Multimedia\\MIDIMap] 988836060
50 * "AutoScheme"=dword:00000000
51 * "ConfigureCount"=dword:00000004
52 * "CurrentInstrument"="Wine OSS midi"
53 * "CurrentScheme"="epp"
55 * "UseScheme"=dword:00000000
58 * CurrentInstrument: name of midiOut device to use when UseScheme is 0. Wine uses an extension
59 * of the form #n to link to n'th midiOut device of the system
60 * CurrentScheme: when UseScheme is non null, it's the scheme to use (see below)
62 * UseScheme: trigger for simple/complex mapping
64 * A scheme is defined (in HKLM) as:
66 * [System\\CurrentControlSet\\Control\\MediaProperties\\PrivateProperties\\Midi\\Schemes\\<nameScheme>]
67 * <nameScheme>: one key for each defined scheme (system wide)
68 * under each one of these <nameScheme> keys, there's:
69 * [...\\<nameScheme>\\<idxDevice>]
70 * "Channels"="<bitMask>"
71 * (the default value of this key also refers to the name of the device).
73 * this defines, for each midiOut device (identified by its index in <idxDevice>), which
74 * channels have to be mapped onto it. The <bitMask> defines the channels (from 0 to 15)
75 * will be mapped (mapping occurs for channel <ch> if bit <ch> is set in <bitMask>
77 * Further mapping information can also be defined in:
78 * [System\\CurrentControlSet\\Control\\MediaProperties\\PrivateProperties\\Midi\\Ports\\<nameDevice>\\Instruments\\<idx>]
79 * "Definition"="<.idf file>"
80 * "FriendlyName"="#for .idx file#"
83 * This last part isn't implemented (.idf file support).
86 WINE_DEFAULT_DEBUG_CHANNEL(midi
);
87 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
89 typedef struct tagMIDIOUTPORT
91 WCHAR name
[MAXPNAMELEN
];
94 unsigned short uDevID
;
96 unsigned int aChn
[16];
99 typedef struct tagMIDIMAPDATA
101 struct tagMIDIMAPDATA
* self
;
102 MIDIOUTPORT
* ChannelMap
[16];
103 MIDIOPENDESC midiDesc
;
107 static MIDIOUTPORT
* midiOutPorts
;
108 static unsigned numMidiOutPorts
;
110 static BOOL
MIDIMAP_IsBadData(MIDIMAPDATA
* mm
)
112 if (!IsBadReadPtr(mm
, sizeof(MIDIMAPDATA
)) && mm
->self
== mm
)
114 TRACE("Bad midimap data (%p)\n", mm
);
118 static BOOL
MIDIMAP_FindPort(const WCHAR
* name
, unsigned* dev
)
120 for (*dev
= 0; *dev
< numMidiOutPorts
; (*dev
)++)
122 TRACE("%s\n", wine_dbgstr_w(midiOutPorts
[*dev
].name
));
123 if (lstrcmpW(midiOutPorts
[*dev
].name
, name
) == 0)
126 /* try the form #nnn */
127 if (*name
== '#' && name
[1] >= '0' && name
[1] <= '9')
129 const WCHAR
* ptr
= name
+ 1;
133 *dev
= *dev
* 10 + *ptr
- '0';
135 } while (*ptr
>= '0' && *ptr
<= '9');
136 if (*dev
< numMidiOutPorts
)
142 static BOOL
MIDIMAP_LoadSettingsDefault(MIDIMAPDATA
* mom
, const WCHAR
* port
)
146 if (port
!= NULL
&& !MIDIMAP_FindPort(port
, &dev
))
148 ERR("Registry glitch: couldn't find midi out (%s)\n", wine_dbgstr_w(port
));
152 /* this is necessary when no midi out ports are present */
153 if (dev
>= numMidiOutPorts
)
156 for (i
= 0; i
< 16; i
++) mom
->ChannelMap
[i
] = &midiOutPorts
[dev
];
161 static BOOL
MIDIMAP_LoadSettingsScheme(MIDIMAPDATA
* mom
, const WCHAR
* scheme
)
163 HKEY hSchemesKey
, hKey
, hPortKey
;
164 unsigned i
, idx
, dev
;
165 WCHAR buffer
[256], port
[256];
166 DWORD type
, size
, mask
;
168 for (i
= 0; i
< 16; i
++) mom
->ChannelMap
[i
] = NULL
;
170 if (RegOpenKeyA(HKEY_LOCAL_MACHINE
,
171 "System\\CurrentControlSet\\Control\\MediaProperties\\PrivateProperties\\Midi\\Schemes",
176 if (RegOpenKeyW(hSchemesKey
, scheme
, &hKey
))
178 RegCloseKey(hSchemesKey
);
182 for (idx
= 0; !RegEnumKeyW(hKey
, idx
, buffer
, ARRAY_SIZE(buffer
)); idx
++)
184 if (RegOpenKeyW(hKey
, buffer
, &hPortKey
)) continue;
187 if (RegQueryValueExW(hPortKey
, NULL
, 0, &type
, (void*)port
, &size
)) continue;
189 if (!MIDIMAP_FindPort(port
, &dev
)) continue;
192 if (RegQueryValueExA(hPortKey
, "Channels", 0, &type
, (void*)&mask
, &size
))
195 for (i
= 0; i
< 16; i
++)
199 if (mom
->ChannelMap
[i
])
200 ERR("Quirks in registry, channel %u is mapped twice\n", i
);
201 mom
->ChannelMap
[i
] = &midiOutPorts
[dev
];
206 RegCloseKey(hSchemesKey
);
212 static BOOL
MIDIMAP_LoadSettings(MIDIMAPDATA
* mom
)
217 if (RegOpenKeyA(HKEY_CURRENT_USER
,
218 "Software\\Microsoft\\Windows\\CurrentVersion\\Multimedia\\MIDIMap", &hKey
))
220 ret
= MIDIMAP_LoadSettingsDefault(mom
, NULL
);
224 DWORD type
, size
, out
;
227 size
= sizeof(buffer
);
228 if (!RegQueryValueExW(hKey
, L
"szPname", 0, &type
, (void*)buffer
, &size
))
230 ret
= MIDIMAP_LoadSettingsDefault(mom
, buffer
);
236 if (!RegQueryValueExA(hKey
, "UseScheme", 0, &type
, (void*)&out
, &size
) && out
)
238 size
= sizeof(buffer
);
239 if (!RegQueryValueExW(hKey
, L
"CurrentScheme", 0, &type
, (void*)buffer
, &size
))
241 if (!(ret
= MIDIMAP_LoadSettingsScheme(mom
, buffer
)))
242 ret
= MIDIMAP_LoadSettingsDefault(mom
, NULL
);
246 ERR("Wrong registry: UseScheme is active, but no CurrentScheme found\n");
251 size
= sizeof(buffer
);
252 if (!RegQueryValueExW(hKey
, L
"CurrentInstrument", 0, &type
, (void*)buffer
, &size
) && *buffer
)
254 ret
= MIDIMAP_LoadSettingsDefault(mom
, buffer
);
258 ret
= MIDIMAP_LoadSettingsDefault(mom
, NULL
);
265 if (ret
&& TRACE_ON(midi
))
269 for (i
= 0; i
< 16; i
++)
271 TRACE("chnMap[%2d] => %d\n",
272 i
, mom
->ChannelMap
[i
] ? mom
->ChannelMap
[i
]->uDevID
: -1);
278 static void MIDIMAP_NotifyClient(MIDIMAPDATA
* mom
, WORD wMsg
,
279 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
281 DriverCallback(mom
->midiDesc
.dwCallback
, mom
->wCbFlags
, (HDRVR
)mom
->midiDesc
.hMidi
,
282 wMsg
, mom
->midiDesc
.dwInstance
, dwParam1
, dwParam2
);
285 static DWORD
modOpen(DWORD_PTR
*lpdwUser
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
287 MIDIMAPDATA
* mom
= HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIMAPDATA
));
289 TRACE("(%p %p %08lx)\n", lpdwUser
, lpDesc
, dwFlags
);
291 if (!mom
) return MMSYSERR_NOMEM
;
293 HeapFree(GetProcessHeap(), 0, mom
);
294 return MMSYSERR_INVALPARAM
;
297 if (MIDIMAP_LoadSettings(mom
))
300 *lpdwUser
= (DWORD_PTR
)mom
;
302 mom
->wCbFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
303 mom
->midiDesc
= *lpDesc
;
305 for (chn
= 0; chn
< 16; chn
++)
307 if (mom
->ChannelMap
[chn
]->loaded
) continue;
308 if (midiOutOpen(&mom
->ChannelMap
[chn
]->hMidi
, mom
->ChannelMap
[chn
]->uDevID
,
309 0L, 0L, CALLBACK_NULL
) == MMSYSERR_NOERROR
)
310 mom
->ChannelMap
[chn
]->loaded
= 1;
312 mom
->ChannelMap
[chn
]->loaded
= -1;
313 /* FIXME: should load here the IDF midi data... and allow channel and
317 MIDIMAP_NotifyClient(mom
, MOM_OPEN
, 0L, 0L);
318 return MMSYSERR_NOERROR
;
320 HeapFree(GetProcessHeap(), 0, mom
);
321 return MIDIERR_INVALIDSETUP
;
324 static DWORD
modClose(MIDIMAPDATA
* mom
)
327 DWORD ret
= MMSYSERR_NOERROR
;
329 if (MIDIMAP_IsBadData(mom
)) return MMSYSERR_ERROR
;
331 for (i
= 0; i
< 16; i
++)
334 if (mom
->ChannelMap
[i
] && mom
->ChannelMap
[i
]->loaded
> 0)
336 t
= midiOutClose(mom
->ChannelMap
[i
]->hMidi
);
337 if (t
== MMSYSERR_NOERROR
)
339 mom
->ChannelMap
[i
]->loaded
= 0;
340 mom
->ChannelMap
[i
]->hMidi
= 0;
342 else if (ret
== MMSYSERR_NOERROR
)
346 if (ret
== MMSYSERR_NOERROR
) {
347 MIDIMAP_NotifyClient(mom
, MOM_CLOSE
, 0L, 0L);
348 HeapFree(GetProcessHeap(), 0, mom
);
353 static DWORD
modLongData(MIDIMAPDATA
* mom
, LPMIDIHDR lpMidiHdr
, DWORD_PTR dwParam2
)
356 DWORD ret
= MMSYSERR_NOERROR
;
359 if (MIDIMAP_IsBadData(mom
))
360 return MMSYSERR_ERROR
;
361 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
362 return MIDIERR_UNPREPARED
;
363 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
364 return MIDIERR_STILLPLAYING
;
367 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
368 lpMidiHdr
->dwFlags
|= MHDR_INQUEUE
;
369 for (chn
= 0; chn
< 16; chn
++)
371 if (mom
->ChannelMap
[chn
] && mom
->ChannelMap
[chn
]->loaded
> 0)
374 midiOutPrepareHeader(mom
->ChannelMap
[chn
]->hMidi
, &mh
, sizeof(mh
));
375 ret
= midiOutLongMsg(mom
->ChannelMap
[chn
]->hMidi
, &mh
, sizeof(mh
));
376 /* As of 2009, wineXYZ.drv's LongData handlers are synchronous */
377 if (!ret
&& !(mh
.dwFlags
& MHDR_DONE
))
378 FIXME("wait until MHDR_DONE\n");
379 midiOutUnprepareHeader(mom
->ChannelMap
[chn
]->hMidi
, &mh
, sizeof(mh
));
380 if (ret
!= MMSYSERR_NOERROR
) break;
383 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
384 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
385 MIDIMAP_NotifyClient(mom
, MOM_DONE
, (DWORD_PTR
)lpMidiHdr
, 0L);
389 static DWORD
modData(MIDIMAPDATA
* mom
, DWORD_PTR dwParam
)
391 BYTE lb
= LOBYTE(LOWORD(dwParam
));
392 WORD chn
= lb
& 0x0F;
393 DWORD ret
= MMSYSERR_NOERROR
;
395 if (MIDIMAP_IsBadData(mom
))
396 return MMSYSERR_ERROR
;
398 if (!mom
->ChannelMap
[chn
]) return MMSYSERR_NOERROR
;
409 if (mom
->ChannelMap
[chn
]->loaded
> 0)
413 dwParam
|= mom
->ChannelMap
[chn
]->aChn
[chn
];
415 if ((LOBYTE(LOWORD(dwParam
)) & 0xF0) == 0xC0 /* program change */ &&
416 mom
->ChannelMap
[chn
]->lpbPatch
)
418 BYTE patch
= HIBYTE(LOWORD(dwParam
));
421 dwParam
&= ~0x0000FF00;
422 dwParam
|= mom
->ChannelMap
[chn
]->lpbPatch
[patch
];
424 ret
= midiOutShortMsg(mom
->ChannelMap
[chn
]->hMidi
, dwParam
);
428 for (chn
= 0; chn
< 16; chn
++)
430 if (mom
->ChannelMap
[chn
]->loaded
> 0)
431 ret
= midiOutShortMsg(mom
->ChannelMap
[chn
]->hMidi
, dwParam
);
435 FIXME("ooch %Ix\n", dwParam
);
441 static DWORD
modPrepare(MIDIMAPDATA
* mom
, LPMIDIHDR lpMidiHdr
, DWORD_PTR dwSize
)
443 if (MIDIMAP_IsBadData(mom
)) return MMSYSERR_ERROR
;
444 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
445 return MMSYSERR_INVALPARAM
;
446 if (lpMidiHdr
->dwFlags
& MHDR_PREPARED
)
447 return MMSYSERR_NOERROR
;
449 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
450 lpMidiHdr
->dwFlags
&= ~(MHDR_DONE
|MHDR_INQUEUE
); /* flags cleared since w2k */
451 return MMSYSERR_NOERROR
;
454 static DWORD
modUnprepare(MIDIMAPDATA
* mom
, LPMIDIHDR lpMidiHdr
, DWORD_PTR dwSize
)
456 if (MIDIMAP_IsBadData(mom
)) return MMSYSERR_ERROR
;
457 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
458 return MMSYSERR_INVALPARAM
;
459 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
460 return MMSYSERR_NOERROR
;
461 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
462 return MIDIERR_STILLPLAYING
;
464 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
465 return MMSYSERR_NOERROR
;
468 static DWORD
modGetVolume(MIDIMAPDATA
* mom
, DWORD
* lpdwVolume
)
470 if (!lpdwVolume
) return MMSYSERR_INVALPARAM
;
471 *lpdwVolume
= 0xFFFFFFFF; /* tests show this initial value */
472 return MMSYSERR_NOERROR
;
475 static DWORD
modSetVolume(MIDIMAPDATA
* mom
, DWORD dwVolume
)
477 /* Native forwards it to some underlying device
478 * GetVolume returns what was last set here. */
480 return MMSYSERR_NOERROR
;
483 static DWORD
modGetDevCaps(UINT wDevID
, MIDIMAPDATA
* mom
, LPMIDIOUTCAPSW lpMidiCaps
, DWORD_PTR size
)
485 static const MIDIOUTCAPSW mappercaps
= {
486 0x00FF, 0x0001, 0x0100, /* Manufacturer and Product ID */
487 L
"Wine midi mapper", MOD_MAPPER
, 0, 0, 0xFFFF,
488 /* Native returns volume caps of underlying device + MIDICAPS_STREAM */
489 MIDICAPS_VOLUME
|MIDICAPS_LRVOLUME
491 if (lpMidiCaps
== NULL
) return MMSYSERR_INVALPARAM
;
492 if (!numMidiOutPorts
) return MMSYSERR_BADDEVICEID
;
494 memcpy(lpMidiCaps
, &mappercaps
, min(size
, sizeof(*lpMidiCaps
)));
495 return MMSYSERR_NOERROR
;
498 static DWORD
modReset(MIDIMAPDATA
* mom
)
501 DWORD ret
= MMSYSERR_NOERROR
;
503 if (MIDIMAP_IsBadData(mom
))
504 return MMSYSERR_ERROR
;
506 for (chn
= 0; chn
< 16; chn
++)
508 if (mom
->ChannelMap
[chn
] && mom
->ChannelMap
[chn
]->loaded
> 0)
510 ret
= midiOutReset(mom
->ChannelMap
[chn
]->hMidi
);
511 if (ret
!= MMSYSERR_NOERROR
) break;
517 static LRESULT
MIDIMAP_drvOpen(void);
518 static LRESULT
MIDIMAP_drvClose(void);
520 /**************************************************************************
521 * modMessage (MIDIMAP.@)
523 DWORD WINAPI
MIDIMAP_modMessage(UINT wDevID
, UINT wMsg
, DWORD_PTR dwUser
,
524 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
526 TRACE("(%u, %04X, %08IX, %08IX, %08IX);\n",
527 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
532 return MIDIMAP_drvOpen();
534 return MIDIMAP_drvClose();
537 /* FIXME: Pretend this is supported */
540 case MODM_OPEN
: return modOpen((DWORD_PTR
*)dwUser
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
541 case MODM_CLOSE
: return modClose ((MIDIMAPDATA
*)dwUser
);
543 case MODM_DATA
: return modData ((MIDIMAPDATA
*)dwUser
, dwParam1
);
544 case MODM_LONGDATA
: return modLongData ((MIDIMAPDATA
*)dwUser
, (LPMIDIHDR
)dwParam1
, dwParam2
);
545 case MODM_PREPARE
: return modPrepare ((MIDIMAPDATA
*)dwUser
, (LPMIDIHDR
)dwParam1
, dwParam2
);
546 case MODM_UNPREPARE
: return modUnprepare ((MIDIMAPDATA
*)dwUser
, (LPMIDIHDR
)dwParam1
, dwParam2
);
547 case MODM_RESET
: return modReset ((MIDIMAPDATA
*)dwUser
);
549 case MODM_GETDEVCAPS
: return modGetDevCaps (wDevID
, (MIDIMAPDATA
*)dwUser
, (LPMIDIOUTCAPSW
)dwParam1
,dwParam2
);
550 case MODM_GETNUMDEVS
: return 1;
551 case MODM_GETVOLUME
: return modGetVolume ((MIDIMAPDATA
*)dwUser
, (DWORD
*)dwParam1
);
552 case MODM_SETVOLUME
: return modSetVolume ((MIDIMAPDATA
*)dwUser
, dwParam1
);
554 FIXME("unknown message %d!\n", wMsg
);
556 return MMSYSERR_NOTSUPPORTED
;
559 /*======================================================================*
561 *======================================================================*/
563 /**************************************************************************
564 * MIDIMAP_drvOpen [internal]
566 static LRESULT
MIDIMAP_drvOpen(void)
570 BOOL found_valid_port
= FALSE
;
575 numMidiOutPorts
= midiOutGetNumDevs();
576 midiOutPorts
= HeapAlloc(GetProcessHeap(), 0,
577 numMidiOutPorts
* sizeof(MIDIOUTPORT
));
578 for (dev
= 0; dev
< numMidiOutPorts
; dev
++)
580 if (midiOutGetDevCapsW(dev
, &moc
, sizeof(moc
)) == 0L)
582 lstrcpyW(midiOutPorts
[dev
].name
, moc
.szPname
);
583 midiOutPorts
[dev
].loaded
= 0;
584 midiOutPorts
[dev
].hMidi
= 0;
585 midiOutPorts
[dev
].uDevID
= dev
;
586 midiOutPorts
[dev
].lpbPatch
= NULL
;
587 for (i
= 0; i
< 16; i
++)
588 midiOutPorts
[dev
].aChn
[i
] = i
;
589 if (wcsncmp(midiOutPorts
[dev
].name
, L
"Midi Through", lstrlenW(L
"Midi Through")) != 0)
590 found_valid_port
= TRUE
;
594 midiOutPorts
[dev
].loaded
= -1;
598 if (!found_valid_port
)
599 ERR_(winediag
)("No software synthesizer midi port found, Midi sound output probably won't work.\n");
604 /**************************************************************************
605 * MIDIMAP_drvClose [internal]
607 static LRESULT
MIDIMAP_drvClose(void)
611 HeapFree(GetProcessHeap(), 0, midiOutPorts
);
618 /**************************************************************************
619 * DriverProc (MIDIMAP.@)
621 LRESULT CALLBACK
MIDIMAP_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
622 LPARAM dwParam1
, LPARAM dwParam2
)
624 /* EPP TRACE("(%08lX, %04X, %08lX, %08lX, %08lX)\n", */
625 /* EPP dwDevID, hDriv, wMsg, dwParam1, dwParam2); */
629 case DRV_LOAD
: return 1;
630 case DRV_FREE
: return 1;
631 case DRV_OPEN
: return 1;
632 case DRV_CLOSE
: return 1;
633 case DRV_ENABLE
: return 1;
634 case DRV_DISABLE
: return 1;
635 case DRV_QUERYCONFIGURE
: return 1;
636 case DRV_CONFIGURE
: MessageBoxA(0, "MIDIMAP MultiMedia Driver !", "OSS Driver", MB_OK
); return 1;
637 case DRV_INSTALL
: return DRVCNF_RESTART
;
638 case DRV_REMOVE
: return DRVCNF_RESTART
;
640 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);