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
);
80 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
82 typedef struct tagMIDIOUTPORT
84 WCHAR name
[MAXPNAMELEN
];
87 unsigned short uDevID
;
89 unsigned int aChn
[16];
92 typedef struct tagMIDIMAPDATA
94 struct tagMIDIMAPDATA
* self
;
95 MIDIOUTPORT
* ChannelMap
[16];
96 MIDIOPENDESC midiDesc
;
100 static MIDIOUTPORT
* midiOutPorts
;
101 static unsigned numMidiOutPorts
;
103 static BOOL
MIDIMAP_IsBadData(MIDIMAPDATA
* mm
)
105 if (!IsBadReadPtr(mm
, sizeof(MIDIMAPDATA
)) && mm
->self
== mm
)
107 TRACE("Bad midimap data (%p)\n", mm
);
111 static BOOL
MIDIMAP_FindPort(const WCHAR
* name
, unsigned* dev
)
113 for (*dev
= 0; *dev
< numMidiOutPorts
; (*dev
)++)
115 TRACE("%s\n", wine_dbgstr_w(midiOutPorts
[*dev
].name
));
116 if (strcmpW(midiOutPorts
[*dev
].name
, name
) == 0)
119 /* try the form #nnn */
120 if (*name
== '#' && name
[1] >= '0' && name
[1] <= '9')
122 const WCHAR
* ptr
= name
+ 1;
126 *dev
= *dev
* 10 + *ptr
- '0';
128 } while (*ptr
>= '0' && *ptr
<= '9');
129 if (*dev
< numMidiOutPorts
)
135 static BOOL
MIDIMAP_LoadSettingsDefault(MIDIMAPDATA
* mom
, const WCHAR
* port
)
139 if (port
!= NULL
&& !MIDIMAP_FindPort(port
, &dev
))
141 ERR("Registry glitch: couldn't find midi out (%s)\n", wine_dbgstr_w(port
));
145 /* this is necessary when no midi out ports are present */
146 if (dev
>= numMidiOutPorts
)
149 for (i
= 0; i
< 16; i
++) mom
->ChannelMap
[i
] = &midiOutPorts
[dev
];
154 static BOOL
MIDIMAP_LoadSettingsScheme(MIDIMAPDATA
* mom
, const WCHAR
* scheme
)
156 HKEY hSchemesKey
, hKey
, hPortKey
;
157 unsigned i
, idx
, dev
;
158 WCHAR buffer
[256], port
[256];
159 DWORD type
, size
, mask
;
161 for (i
= 0; i
< 16; i
++) mom
->ChannelMap
[i
] = NULL
;
163 if (RegOpenKeyA(HKEY_LOCAL_MACHINE
,
164 "System\\CurrentControlSet\\Control\\MediaProperties\\PrivateProperties\\Midi\\Schemes",
169 if (RegOpenKeyW(hSchemesKey
, scheme
, &hKey
))
171 RegCloseKey(hSchemesKey
);
175 for (idx
= 0; !RegEnumKeyW(hKey
, idx
, buffer
, sizeof(buffer
)/sizeof(buffer
[0])); idx
++)
177 if (RegOpenKeyW(hKey
, buffer
, &hPortKey
)) continue;
180 if (RegQueryValueExW(hPortKey
, NULL
, 0, &type
, (void*)port
, &size
)) continue;
182 if (!MIDIMAP_FindPort(port
, &dev
)) continue;
185 if (RegQueryValueExA(hPortKey
, "Channels", 0, &type
, (void*)&mask
, &size
))
188 for (i
= 0; i
< 16; i
++)
192 if (mom
->ChannelMap
[i
])
193 ERR("Quirks in registry, channel %u is mapped twice\n", i
);
194 mom
->ChannelMap
[i
] = &midiOutPorts
[dev
];
199 RegCloseKey(hSchemesKey
);
205 static BOOL
MIDIMAP_LoadSettings(MIDIMAPDATA
* mom
)
210 if (RegOpenKeyA(HKEY_CURRENT_USER
,
211 "Software\\Microsoft\\Windows\\CurrentVersion\\Multimedia\\MIDIMap", &hKey
))
213 ret
= MIDIMAP_LoadSettingsDefault(mom
, NULL
);
217 DWORD type
, size
, out
;
222 if (!RegQueryValueExA(hKey
, "UseScheme", 0, &type
, (void*)&out
, &size
) && out
)
224 static const WCHAR cs
[] = {'C','u','r','r','e','n','t','S','c','h','e','m','e',0};
225 size
= sizeof(buffer
);
226 if (!RegQueryValueExW(hKey
, cs
, 0, &type
, (void*)buffer
, &size
))
228 if (!(ret
= MIDIMAP_LoadSettingsScheme(mom
, buffer
)))
229 ret
= MIDIMAP_LoadSettingsDefault(mom
, NULL
);
233 ERR("Wrong registry: UseScheme is active, but no CurrentScheme found\n");
238 static const WCHAR ci
[] = {'C','u','r','r','e','n','t','I','n','s','t','r','u','m','e','n','t',0};
239 size
= sizeof(buffer
);
240 if (!RegQueryValueExW(hKey
, ci
, 0, &type
, (void*)buffer
, &size
) && *buffer
)
242 ret
= MIDIMAP_LoadSettingsDefault(mom
, buffer
);
246 ret
= MIDIMAP_LoadSettingsDefault(mom
, NULL
);
252 if (ret
&& TRACE_ON(msacm
))
256 for (i
= 0; i
< 16; i
++)
258 TRACE("chnMap[%2d] => %d\n",
259 i
, mom
->ChannelMap
[i
] ? mom
->ChannelMap
[i
]->uDevID
: -1);
265 static void MIDIMAP_NotifyClient(MIDIMAPDATA
* mom
, WORD wMsg
,
266 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
268 DriverCallback(mom
->midiDesc
.dwCallback
, mom
->wCbFlags
, (HDRVR
)mom
->midiDesc
.hMidi
,
269 wMsg
, mom
->midiDesc
.dwInstance
, dwParam1
, dwParam2
);
272 static DWORD
modOpen(DWORD_PTR
*lpdwUser
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
274 MIDIMAPDATA
* mom
= HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIMAPDATA
));
276 TRACE("(%p %p %08x)\n", lpdwUser
, lpDesc
, dwFlags
);
278 if (!mom
) return MMSYSERR_NOMEM
;
280 HeapFree(GetProcessHeap(), 0, mom
);
281 return MMSYSERR_INVALPARAM
;
284 if (MIDIMAP_LoadSettings(mom
))
286 *lpdwUser
= (DWORD_PTR
)mom
;
289 mom
->wCbFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
290 mom
->midiDesc
= *lpDesc
;
291 MIDIMAP_NotifyClient(mom
, MOM_OPEN
, 0L, 0L);
293 return MMSYSERR_NOERROR
;
295 HeapFree(GetProcessHeap(), 0, mom
);
296 return MIDIERR_INVALIDSETUP
;
299 static DWORD
modClose(MIDIMAPDATA
* mom
)
302 DWORD ret
= MMSYSERR_NOERROR
;
304 if (MIDIMAP_IsBadData(mom
)) return MMSYSERR_ERROR
;
306 for (i
= 0; i
< 16; i
++)
309 if (mom
->ChannelMap
[i
] && mom
->ChannelMap
[i
]->loaded
> 0)
311 t
= midiOutClose(mom
->ChannelMap
[i
]->hMidi
);
312 if (t
== MMSYSERR_NOERROR
)
314 mom
->ChannelMap
[i
]->loaded
= 0;
315 mom
->ChannelMap
[i
]->hMidi
= 0;
317 else if (ret
== MMSYSERR_NOERROR
)
321 if (ret
== MMSYSERR_NOERROR
) {
322 MIDIMAP_NotifyClient(mom
, MOM_CLOSE
, 0L, 0L);
323 HeapFree(GetProcessHeap(), 0, mom
);
328 static DWORD
modLongData(MIDIMAPDATA
* mom
, LPMIDIHDR lpMidiHdr
, DWORD_PTR dwParam2
)
331 DWORD ret
= MMSYSERR_NOERROR
;
334 if (MIDIMAP_IsBadData(mom
))
335 return MMSYSERR_ERROR
;
336 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
337 return MIDIERR_UNPREPARED
;
338 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
339 return MIDIERR_STILLPLAYING
;
342 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
343 lpMidiHdr
->dwFlags
|= MHDR_INQUEUE
;
344 for (chn
= 0; chn
< 16; chn
++)
346 if (mom
->ChannelMap
[chn
] && mom
->ChannelMap
[chn
]->loaded
> 0)
349 midiOutPrepareHeader(mom
->ChannelMap
[chn
]->hMidi
, &mh
, sizeof(mh
));
350 ret
= midiOutLongMsg(mom
->ChannelMap
[chn
]->hMidi
, &mh
, sizeof(mh
));
351 /* As of 2009, wineXYZ.drv's LongData handlers are synchronous */
352 if (!ret
&& !(mh
.dwFlags
& MHDR_DONE
))
353 FIXME("wait until MHDR_DONE\n");
354 midiOutUnprepareHeader(mom
->ChannelMap
[chn
]->hMidi
, &mh
, sizeof(mh
));
355 if (ret
!= MMSYSERR_NOERROR
) break;
358 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
359 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
360 MIDIMAP_NotifyClient(mom
, MOM_DONE
, (DWORD_PTR
)lpMidiHdr
, 0L);
364 static DWORD
modData(MIDIMAPDATA
* mom
, DWORD_PTR dwParam
)
366 BYTE lb
= LOBYTE(LOWORD(dwParam
));
367 WORD chn
= lb
& 0x0F;
368 DWORD ret
= MMSYSERR_NOERROR
;
370 if (MIDIMAP_IsBadData(mom
))
371 return MMSYSERR_ERROR
;
373 if (!mom
->ChannelMap
[chn
]) return MMSYSERR_NOERROR
;
384 if (mom
->ChannelMap
[chn
]->loaded
== 0)
386 if (midiOutOpen(&mom
->ChannelMap
[chn
]->hMidi
, mom
->ChannelMap
[chn
]->uDevID
,
387 0L, 0L, CALLBACK_NULL
) == MMSYSERR_NOERROR
)
388 mom
->ChannelMap
[chn
]->loaded
= 1;
390 mom
->ChannelMap
[chn
]->loaded
= -1;
391 /* FIXME: should load here the IDF midi data... and allow channel and
395 if (mom
->ChannelMap
[chn
]->loaded
> 0)
399 dwParam
|= mom
->ChannelMap
[chn
]->aChn
[chn
];
401 if ((LOBYTE(LOWORD(dwParam
)) & 0xF0) == 0xC0 /* program change */ &&
402 mom
->ChannelMap
[chn
]->lpbPatch
)
404 BYTE patch
= HIBYTE(LOWORD(dwParam
));
407 dwParam
&= ~0x0000FF00;
408 dwParam
|= mom
->ChannelMap
[chn
]->lpbPatch
[patch
];
410 ret
= midiOutShortMsg(mom
->ChannelMap
[chn
]->hMidi
, dwParam
);
414 for (chn
= 0; chn
< 16; chn
++)
416 if (mom
->ChannelMap
[chn
]->loaded
> 0)
417 ret
= midiOutShortMsg(mom
->ChannelMap
[chn
]->hMidi
, dwParam
);
421 FIXME("ooch %lx\n", dwParam
);
427 static DWORD
modPrepare(MIDIMAPDATA
* mom
, LPMIDIHDR lpMidiHdr
, DWORD_PTR dwSize
)
429 if (MIDIMAP_IsBadData(mom
)) return MMSYSERR_ERROR
;
430 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
431 return MMSYSERR_INVALPARAM
;
432 if (lpMidiHdr
->dwFlags
& MHDR_PREPARED
)
433 return MMSYSERR_NOERROR
;
435 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
436 lpMidiHdr
->dwFlags
&= ~(MHDR_DONE
|MHDR_INQUEUE
); /* flags cleared since w2k */
437 return MMSYSERR_NOERROR
;
440 static DWORD
modUnprepare(MIDIMAPDATA
* mom
, LPMIDIHDR lpMidiHdr
, DWORD_PTR dwSize
)
442 if (MIDIMAP_IsBadData(mom
)) return MMSYSERR_ERROR
;
443 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
444 return MMSYSERR_INVALPARAM
;
445 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
446 return MMSYSERR_NOERROR
;
447 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
448 return MIDIERR_STILLPLAYING
;
450 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
451 return MMSYSERR_NOERROR
;
454 static DWORD
modGetVolume(MIDIMAPDATA
* mom
, DWORD
* lpdwVolume
)
456 if (!lpdwVolume
) return MMSYSERR_INVALPARAM
;
457 *lpdwVolume
= 0xFFFFFFFF; /* tests show this initial value */
458 return MMSYSERR_NOERROR
;
461 static DWORD
modSetVolume(MIDIMAPDATA
* mom
, DWORD dwVolume
)
463 /* Native forwards it to some underlying device
464 * GetVolume returns what was last set here. */
466 return MMSYSERR_NOERROR
;
469 static DWORD
modGetDevCaps(UINT wDevID
, MIDIMAPDATA
* mom
, LPMIDIOUTCAPSW lpMidiCaps
, DWORD_PTR size
)
471 static const MIDIOUTCAPSW mappercaps
= {
472 0x00FF, 0x0001, 0x0100, /* Manufacturer and Product ID */
473 {'W','i','n','e',' ','m','i','d','i',' ','m','a','p','p','e','r',0},
474 MOD_MAPPER
, 0, 0, 0xFFFF,
475 /* Native returns volume caps of underlying device + MIDICAPS_STREAM */
476 MIDICAPS_VOLUME
|MIDICAPS_LRVOLUME
478 if (lpMidiCaps
== NULL
) return MMSYSERR_INVALPARAM
;
479 if (!numMidiOutPorts
) return MMSYSERR_BADDEVICEID
;
481 memcpy(lpMidiCaps
, &mappercaps
, min(size
, sizeof(*lpMidiCaps
)));
482 return MMSYSERR_NOERROR
;
485 static DWORD
modReset(MIDIMAPDATA
* mom
)
488 DWORD ret
= MMSYSERR_NOERROR
;
490 if (MIDIMAP_IsBadData(mom
))
491 return MMSYSERR_ERROR
;
493 for (chn
= 0; chn
< 16; chn
++)
495 if (mom
->ChannelMap
[chn
] && mom
->ChannelMap
[chn
]->loaded
> 0)
497 ret
= midiOutReset(mom
->ChannelMap
[chn
]->hMidi
);
498 if (ret
!= MMSYSERR_NOERROR
) break;
504 static LRESULT
MIDIMAP_drvOpen(void);
505 static LRESULT
MIDIMAP_drvClose(void);
507 /**************************************************************************
508 * modMessage (MIDIMAP.@)
510 DWORD WINAPI
MIDIMAP_modMessage(UINT wDevID
, UINT wMsg
, DWORD_PTR dwUser
,
511 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
513 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
514 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
519 return MIDIMAP_drvOpen();
521 return MIDIMAP_drvClose();
524 /* FIXME: Pretend this is supported */
527 case MODM_OPEN
: return modOpen((DWORD_PTR
*)dwUser
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
528 case MODM_CLOSE
: return modClose ((MIDIMAPDATA
*)dwUser
);
530 case MODM_DATA
: return modData ((MIDIMAPDATA
*)dwUser
, dwParam1
);
531 case MODM_LONGDATA
: return modLongData ((MIDIMAPDATA
*)dwUser
, (LPMIDIHDR
)dwParam1
, dwParam2
);
532 case MODM_PREPARE
: return modPrepare ((MIDIMAPDATA
*)dwUser
, (LPMIDIHDR
)dwParam1
, dwParam2
);
533 case MODM_UNPREPARE
: return modUnprepare ((MIDIMAPDATA
*)dwUser
, (LPMIDIHDR
)dwParam1
, dwParam2
);
534 case MODM_RESET
: return modReset ((MIDIMAPDATA
*)dwUser
);
536 case MODM_GETDEVCAPS
: return modGetDevCaps (wDevID
, (MIDIMAPDATA
*)dwUser
, (LPMIDIOUTCAPSW
)dwParam1
,dwParam2
);
537 case MODM_GETNUMDEVS
: return 1;
538 case MODM_GETVOLUME
: return modGetVolume ((MIDIMAPDATA
*)dwUser
, (DWORD
*)dwParam1
);
539 case MODM_SETVOLUME
: return modSetVolume ((MIDIMAPDATA
*)dwUser
, dwParam1
);
541 FIXME("unknown message %d!\n", wMsg
);
543 return MMSYSERR_NOTSUPPORTED
;
546 /*======================================================================*
548 *======================================================================*/
550 /**************************************************************************
551 * MIDIMAP_drvOpen [internal]
553 static LRESULT
MIDIMAP_drvOpen(void)
557 WCHAR throughportW
[] = {'M','i','d','i',' ','T','h','r','o','u','g','h',0};
558 BOOL found_valid_port
= FALSE
;
563 numMidiOutPorts
= midiOutGetNumDevs();
564 midiOutPorts
= HeapAlloc(GetProcessHeap(), 0,
565 numMidiOutPorts
* sizeof(MIDIOUTPORT
));
566 for (dev
= 0; dev
< numMidiOutPorts
; dev
++)
568 if (midiOutGetDevCapsW(dev
, &moc
, sizeof(moc
)) == 0L)
570 strcpyW(midiOutPorts
[dev
].name
, moc
.szPname
);
571 midiOutPorts
[dev
].loaded
= 0;
572 midiOutPorts
[dev
].hMidi
= 0;
573 midiOutPorts
[dev
].uDevID
= dev
;
574 midiOutPorts
[dev
].lpbPatch
= NULL
;
575 for (i
= 0; i
< 16; i
++)
576 midiOutPorts
[dev
].aChn
[i
] = i
;
577 if (strncmpW(midiOutPorts
[dev
].name
, throughportW
, strlenW(throughportW
)) != 0)
578 found_valid_port
= TRUE
;
582 midiOutPorts
[dev
].loaded
= -1;
586 if (!found_valid_port
)
587 ERR_(winediag
)("No software synthesizer midi port found, Midi sound output probably won't work.\n");
592 /**************************************************************************
593 * MIDIMAP_drvClose [internal]
595 static LRESULT
MIDIMAP_drvClose(void)
599 HeapFree(GetProcessHeap(), 0, midiOutPorts
);
606 /**************************************************************************
607 * DriverProc (MIDIMAP.@)
609 LRESULT CALLBACK
MIDIMAP_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
610 LPARAM dwParam1
, LPARAM dwParam2
)
612 /* EPP TRACE("(%08lX, %04X, %08lX, %08lX, %08lX)\n", */
613 /* EPP dwDevID, hDriv, wMsg, dwParam1, dwParam2); */
617 case DRV_LOAD
: return 1;
618 case DRV_FREE
: return 1;
619 case DRV_OPEN
: return 1;
620 case DRV_CLOSE
: return 1;
621 case DRV_ENABLE
: return 1;
622 case DRV_DISABLE
: return 1;
623 case DRV_QUERYCONFIGURE
: return 1;
624 case DRV_CONFIGURE
: MessageBoxA(0, "MIDIMAP MultiMedia Driver !", "OSS Driver", MB_OK
); return 1;
625 case DRV_INSTALL
: return DRVCNF_RESTART
;
626 case DRV_REMOVE
: return DRVCNF_RESTART
;
628 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);