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
, ARRAY_SIZE(buffer
)); 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
))
287 *lpdwUser
= (DWORD_PTR
)mom
;
289 mom
->wCbFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
290 mom
->midiDesc
= *lpDesc
;
292 for (chn
= 0; chn
< 16; chn
++)
294 if (mom
->ChannelMap
[chn
]->loaded
) continue;
295 if (midiOutOpen(&mom
->ChannelMap
[chn
]->hMidi
, mom
->ChannelMap
[chn
]->uDevID
,
296 0L, 0L, CALLBACK_NULL
) == MMSYSERR_NOERROR
)
297 mom
->ChannelMap
[chn
]->loaded
= 1;
299 mom
->ChannelMap
[chn
]->loaded
= -1;
300 /* FIXME: should load here the IDF midi data... and allow channel and
304 MIDIMAP_NotifyClient(mom
, MOM_OPEN
, 0L, 0L);
305 return MMSYSERR_NOERROR
;
307 HeapFree(GetProcessHeap(), 0, mom
);
308 return MIDIERR_INVALIDSETUP
;
311 static DWORD
modClose(MIDIMAPDATA
* mom
)
314 DWORD ret
= MMSYSERR_NOERROR
;
316 if (MIDIMAP_IsBadData(mom
)) return MMSYSERR_ERROR
;
318 for (i
= 0; i
< 16; i
++)
321 if (mom
->ChannelMap
[i
] && mom
->ChannelMap
[i
]->loaded
> 0)
323 t
= midiOutClose(mom
->ChannelMap
[i
]->hMidi
);
324 if (t
== MMSYSERR_NOERROR
)
326 mom
->ChannelMap
[i
]->loaded
= 0;
327 mom
->ChannelMap
[i
]->hMidi
= 0;
329 else if (ret
== MMSYSERR_NOERROR
)
333 if (ret
== MMSYSERR_NOERROR
) {
334 MIDIMAP_NotifyClient(mom
, MOM_CLOSE
, 0L, 0L);
335 HeapFree(GetProcessHeap(), 0, mom
);
340 static DWORD
modLongData(MIDIMAPDATA
* mom
, LPMIDIHDR lpMidiHdr
, DWORD_PTR dwParam2
)
343 DWORD ret
= MMSYSERR_NOERROR
;
346 if (MIDIMAP_IsBadData(mom
))
347 return MMSYSERR_ERROR
;
348 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
349 return MIDIERR_UNPREPARED
;
350 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
351 return MIDIERR_STILLPLAYING
;
354 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
355 lpMidiHdr
->dwFlags
|= MHDR_INQUEUE
;
356 for (chn
= 0; chn
< 16; chn
++)
358 if (mom
->ChannelMap
[chn
] && mom
->ChannelMap
[chn
]->loaded
> 0)
361 midiOutPrepareHeader(mom
->ChannelMap
[chn
]->hMidi
, &mh
, sizeof(mh
));
362 ret
= midiOutLongMsg(mom
->ChannelMap
[chn
]->hMidi
, &mh
, sizeof(mh
));
363 /* As of 2009, wineXYZ.drv's LongData handlers are synchronous */
364 if (!ret
&& !(mh
.dwFlags
& MHDR_DONE
))
365 FIXME("wait until MHDR_DONE\n");
366 midiOutUnprepareHeader(mom
->ChannelMap
[chn
]->hMidi
, &mh
, sizeof(mh
));
367 if (ret
!= MMSYSERR_NOERROR
) break;
370 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
371 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
372 MIDIMAP_NotifyClient(mom
, MOM_DONE
, (DWORD_PTR
)lpMidiHdr
, 0L);
376 static DWORD
modData(MIDIMAPDATA
* mom
, DWORD_PTR dwParam
)
378 BYTE lb
= LOBYTE(LOWORD(dwParam
));
379 WORD chn
= lb
& 0x0F;
380 DWORD ret
= MMSYSERR_NOERROR
;
382 if (MIDIMAP_IsBadData(mom
))
383 return MMSYSERR_ERROR
;
385 if (!mom
->ChannelMap
[chn
]) return MMSYSERR_NOERROR
;
396 if (mom
->ChannelMap
[chn
]->loaded
> 0)
400 dwParam
|= mom
->ChannelMap
[chn
]->aChn
[chn
];
402 if ((LOBYTE(LOWORD(dwParam
)) & 0xF0) == 0xC0 /* program change */ &&
403 mom
->ChannelMap
[chn
]->lpbPatch
)
405 BYTE patch
= HIBYTE(LOWORD(dwParam
));
408 dwParam
&= ~0x0000FF00;
409 dwParam
|= mom
->ChannelMap
[chn
]->lpbPatch
[patch
];
411 ret
= midiOutShortMsg(mom
->ChannelMap
[chn
]->hMidi
, dwParam
);
415 for (chn
= 0; chn
< 16; chn
++)
417 if (mom
->ChannelMap
[chn
]->loaded
> 0)
418 ret
= midiOutShortMsg(mom
->ChannelMap
[chn
]->hMidi
, dwParam
);
422 FIXME("ooch %lx\n", dwParam
);
428 static DWORD
modPrepare(MIDIMAPDATA
* mom
, LPMIDIHDR lpMidiHdr
, DWORD_PTR dwSize
)
430 if (MIDIMAP_IsBadData(mom
)) return MMSYSERR_ERROR
;
431 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
432 return MMSYSERR_INVALPARAM
;
433 if (lpMidiHdr
->dwFlags
& MHDR_PREPARED
)
434 return MMSYSERR_NOERROR
;
436 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
437 lpMidiHdr
->dwFlags
&= ~(MHDR_DONE
|MHDR_INQUEUE
); /* flags cleared since w2k */
438 return MMSYSERR_NOERROR
;
441 static DWORD
modUnprepare(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
;
448 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
449 return MIDIERR_STILLPLAYING
;
451 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
452 return MMSYSERR_NOERROR
;
455 static DWORD
modGetVolume(MIDIMAPDATA
* mom
, DWORD
* lpdwVolume
)
457 if (!lpdwVolume
) return MMSYSERR_INVALPARAM
;
458 *lpdwVolume
= 0xFFFFFFFF; /* tests show this initial value */
459 return MMSYSERR_NOERROR
;
462 static DWORD
modSetVolume(MIDIMAPDATA
* mom
, DWORD dwVolume
)
464 /* Native forwards it to some underlying device
465 * GetVolume returns what was last set here. */
467 return MMSYSERR_NOERROR
;
470 static DWORD
modGetDevCaps(UINT wDevID
, MIDIMAPDATA
* mom
, LPMIDIOUTCAPSW lpMidiCaps
, DWORD_PTR size
)
472 static const MIDIOUTCAPSW mappercaps
= {
473 0x00FF, 0x0001, 0x0100, /* Manufacturer and Product ID */
474 {'W','i','n','e',' ','m','i','d','i',' ','m','a','p','p','e','r',0},
475 MOD_MAPPER
, 0, 0, 0xFFFF,
476 /* Native returns volume caps of underlying device + MIDICAPS_STREAM */
477 MIDICAPS_VOLUME
|MIDICAPS_LRVOLUME
479 if (lpMidiCaps
== NULL
) return MMSYSERR_INVALPARAM
;
480 if (!numMidiOutPorts
) return MMSYSERR_BADDEVICEID
;
482 memcpy(lpMidiCaps
, &mappercaps
, min(size
, sizeof(*lpMidiCaps
)));
483 return MMSYSERR_NOERROR
;
486 static DWORD
modReset(MIDIMAPDATA
* mom
)
489 DWORD ret
= MMSYSERR_NOERROR
;
491 if (MIDIMAP_IsBadData(mom
))
492 return MMSYSERR_ERROR
;
494 for (chn
= 0; chn
< 16; chn
++)
496 if (mom
->ChannelMap
[chn
] && mom
->ChannelMap
[chn
]->loaded
> 0)
498 ret
= midiOutReset(mom
->ChannelMap
[chn
]->hMidi
);
499 if (ret
!= MMSYSERR_NOERROR
) break;
505 static LRESULT
MIDIMAP_drvOpen(void);
506 static LRESULT
MIDIMAP_drvClose(void);
508 /**************************************************************************
509 * modMessage (MIDIMAP.@)
511 DWORD WINAPI
MIDIMAP_modMessage(UINT wDevID
, UINT wMsg
, DWORD_PTR dwUser
,
512 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
514 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
515 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
520 return MIDIMAP_drvOpen();
522 return MIDIMAP_drvClose();
525 /* FIXME: Pretend this is supported */
528 case MODM_OPEN
: return modOpen((DWORD_PTR
*)dwUser
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
529 case MODM_CLOSE
: return modClose ((MIDIMAPDATA
*)dwUser
);
531 case MODM_DATA
: return modData ((MIDIMAPDATA
*)dwUser
, dwParam1
);
532 case MODM_LONGDATA
: return modLongData ((MIDIMAPDATA
*)dwUser
, (LPMIDIHDR
)dwParam1
, dwParam2
);
533 case MODM_PREPARE
: return modPrepare ((MIDIMAPDATA
*)dwUser
, (LPMIDIHDR
)dwParam1
, dwParam2
);
534 case MODM_UNPREPARE
: return modUnprepare ((MIDIMAPDATA
*)dwUser
, (LPMIDIHDR
)dwParam1
, dwParam2
);
535 case MODM_RESET
: return modReset ((MIDIMAPDATA
*)dwUser
);
537 case MODM_GETDEVCAPS
: return modGetDevCaps (wDevID
, (MIDIMAPDATA
*)dwUser
, (LPMIDIOUTCAPSW
)dwParam1
,dwParam2
);
538 case MODM_GETNUMDEVS
: return 1;
539 case MODM_GETVOLUME
: return modGetVolume ((MIDIMAPDATA
*)dwUser
, (DWORD
*)dwParam1
);
540 case MODM_SETVOLUME
: return modSetVolume ((MIDIMAPDATA
*)dwUser
, dwParam1
);
542 FIXME("unknown message %d!\n", wMsg
);
544 return MMSYSERR_NOTSUPPORTED
;
547 /*======================================================================*
549 *======================================================================*/
551 /**************************************************************************
552 * MIDIMAP_drvOpen [internal]
554 static LRESULT
MIDIMAP_drvOpen(void)
558 WCHAR throughportW
[] = {'M','i','d','i',' ','T','h','r','o','u','g','h',0};
559 BOOL found_valid_port
= FALSE
;
564 numMidiOutPorts
= midiOutGetNumDevs();
565 midiOutPorts
= HeapAlloc(GetProcessHeap(), 0,
566 numMidiOutPorts
* sizeof(MIDIOUTPORT
));
567 for (dev
= 0; dev
< numMidiOutPorts
; dev
++)
569 if (midiOutGetDevCapsW(dev
, &moc
, sizeof(moc
)) == 0L)
571 strcpyW(midiOutPorts
[dev
].name
, moc
.szPname
);
572 midiOutPorts
[dev
].loaded
= 0;
573 midiOutPorts
[dev
].hMidi
= 0;
574 midiOutPorts
[dev
].uDevID
= dev
;
575 midiOutPorts
[dev
].lpbPatch
= NULL
;
576 for (i
= 0; i
< 16; i
++)
577 midiOutPorts
[dev
].aChn
[i
] = i
;
578 if (strncmpW(midiOutPorts
[dev
].name
, throughportW
, strlenW(throughportW
)) != 0)
579 found_valid_port
= TRUE
;
583 midiOutPorts
[dev
].loaded
= -1;
587 if (!found_valid_port
)
588 ERR_(winediag
)("No software synthesizer midi port found, Midi sound output probably won't work.\n");
593 /**************************************************************************
594 * MIDIMAP_drvClose [internal]
596 static LRESULT
MIDIMAP_drvClose(void)
600 HeapFree(GetProcessHeap(), 0, midiOutPorts
);
607 /**************************************************************************
608 * DriverProc (MIDIMAP.@)
610 LRESULT CALLBACK
MIDIMAP_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
611 LPARAM dwParam1
, LPARAM dwParam2
)
613 /* EPP TRACE("(%08lX, %04X, %08lX, %08lX, %08lX)\n", */
614 /* EPP dwDevID, hDriv, wMsg, dwParam1, dwParam2); */
618 case DRV_LOAD
: return 1;
619 case DRV_FREE
: return 1;
620 case DRV_OPEN
: return 1;
621 case DRV_CLOSE
: return 1;
622 case DRV_ENABLE
: return 1;
623 case DRV_DISABLE
: return 1;
624 case DRV_QUERYCONFIGURE
: return 1;
625 case DRV_CONFIGURE
: MessageBoxA(0, "MIDIMAP MultiMedia Driver !", "OSS Driver", MB_OK
); return 1;
626 case DRV_INSTALL
: return DRVCNF_RESTART
;
627 case DRV_REMOVE
: return DRVCNF_RESTART
;
629 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);