po: Update German translation.
[wine.git] / dlls / midimap / midimap.c
blob64b4dc854668871194a9cafee53e7102ccd906e1
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 MIDIOPENDESC midiDesc;
96 WORD wCbFlags;
97 } MIDIMAPDATA;
99 static MIDIOUTPORT* midiOutPorts;
100 static unsigned numMidiOutPorts;
102 static BOOL MIDIMAP_IsBadData(MIDIMAPDATA* mm)
104 if (!IsBadReadPtr(mm, sizeof(MIDIMAPDATA)) && mm->self == mm)
105 return FALSE;
106 TRACE("Bad midimap data (%p)\n", mm);
107 return TRUE;
110 static BOOL MIDIMAP_FindPort(const WCHAR* name, unsigned* dev)
112 for (*dev = 0; *dev < numMidiOutPorts; (*dev)++)
114 TRACE("%s\n", wine_dbgstr_w(midiOutPorts[*dev].name));
115 if (strcmpW(midiOutPorts[*dev].name, name) == 0)
116 return TRUE;
118 /* try the form #nnn */
119 if (*name == '#' && name[1] >= '0' && name[1] <= '9')
121 const WCHAR* ptr = name + 1;
122 *dev = 0;
125 *dev = *dev * 10 + *ptr - '0';
126 ptr++;
127 } while (*ptr >= '0' && *ptr <= '9');
128 if (*dev < numMidiOutPorts)
129 return TRUE;
131 return FALSE;
134 static BOOL MIDIMAP_LoadSettingsDefault(MIDIMAPDATA* mom, const WCHAR* port)
136 unsigned i, dev = 0;
138 if (port != NULL && !MIDIMAP_FindPort(port, &dev))
140 ERR("Registry glitch: couldn't find midi out (%s)\n", wine_dbgstr_w(port));
141 dev = 0;
144 /* this is necessary when no midi out ports are present */
145 if (dev >= numMidiOutPorts)
146 return FALSE;
147 /* sets default */
148 for (i = 0; i < 16; i++) mom->ChannelMap[i] = &midiOutPorts[dev];
150 return TRUE;
153 static BOOL MIDIMAP_LoadSettingsScheme(MIDIMAPDATA* mom, const WCHAR* scheme)
155 HKEY hSchemesKey, hKey, hPortKey;
156 unsigned i, idx, dev;
157 WCHAR buffer[256], port[256];
158 DWORD type, size, mask;
160 for (i = 0; i < 16; i++) mom->ChannelMap[i] = NULL;
162 if (RegOpenKeyA(HKEY_LOCAL_MACHINE,
163 "System\\CurrentControlSet\\Control\\MediaProperties\\PrivateProperties\\Midi\\Schemes",
164 &hSchemesKey))
166 return FALSE;
168 if (RegOpenKeyW(hSchemesKey, scheme, &hKey))
170 RegCloseKey(hSchemesKey);
171 return FALSE;
174 for (idx = 0; !RegEnumKeyW(hKey, idx, buffer, sizeof(buffer)/sizeof(buffer[0])); idx++)
176 if (RegOpenKeyW(hKey, buffer, &hPortKey)) continue;
178 size = sizeof(port);
179 if (RegQueryValueExW(hPortKey, NULL, 0, &type, (void*)port, &size)) continue;
181 if (!MIDIMAP_FindPort(port, &dev)) continue;
183 size = sizeof(mask);
184 if (RegQueryValueExA(hPortKey, "Channels", 0, &type, (void*)&mask, &size))
185 continue;
187 for (i = 0; i < 16; i++)
189 if (mask & (1 << i))
191 if (mom->ChannelMap[i])
192 ERR("Quirks in registry, channel %u is mapped twice\n", i);
193 mom->ChannelMap[i] = &midiOutPorts[dev];
198 RegCloseKey(hSchemesKey);
199 RegCloseKey(hKey);
201 return TRUE;
204 static BOOL MIDIMAP_LoadSettings(MIDIMAPDATA* mom)
206 HKEY hKey;
207 BOOL ret;
209 if (RegOpenKeyA(HKEY_CURRENT_USER,
210 "Software\\Microsoft\\Windows\\CurrentVersion\\Multimedia\\MIDIMap", &hKey))
212 ret = MIDIMAP_LoadSettingsDefault(mom, NULL);
214 else
216 DWORD type, size, out;
217 WCHAR buffer[256];
219 ret = 2;
220 size = sizeof(out);
221 if (!RegQueryValueExA(hKey, "UseScheme", 0, &type, (void*)&out, &size) && out)
223 static const WCHAR cs[] = {'C','u','r','r','e','n','t','S','c','h','e','m','e',0};
224 size = sizeof(buffer);
225 if (!RegQueryValueExW(hKey, cs, 0, &type, (void*)buffer, &size))
227 if (!(ret = MIDIMAP_LoadSettingsScheme(mom, buffer)))
228 ret = MIDIMAP_LoadSettingsDefault(mom, NULL);
230 else
232 ERR("Wrong registry: UseScheme is active, but no CurrentScheme found\n");
235 if (ret == 2)
237 static const WCHAR ci[] = {'C','u','r','r','e','n','t','I','n','s','t','r','u','m','e','n','t',0};
238 size = sizeof(buffer);
239 if (!RegQueryValueExW(hKey, ci, 0, &type, (void*)buffer, &size) && *buffer)
241 ret = MIDIMAP_LoadSettingsDefault(mom, buffer);
243 else
245 ret = MIDIMAP_LoadSettingsDefault(mom, NULL);
249 RegCloseKey(hKey);
251 if (ret && TRACE_ON(msacm))
253 unsigned i;
255 for (i = 0; i < 16; i++)
257 TRACE("chnMap[%2d] => %d\n",
258 i, mom->ChannelMap[i] ? mom->ChannelMap[i]->uDevID : -1);
261 return ret;
264 static void MIDIMAP_NotifyClient(MIDIMAPDATA* mom, WORD wMsg,
265 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
267 DriverCallback(mom->midiDesc.dwCallback, mom->wCbFlags, (HDRVR)mom->midiDesc.hMidi,
268 wMsg, mom->midiDesc.dwInstance, dwParam1, dwParam2);
271 static DWORD modOpen(DWORD_PTR *lpdwUser, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
273 MIDIMAPDATA* mom = HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIMAPDATA));
275 TRACE("(%p %p %08x)\n", lpdwUser, lpDesc, dwFlags);
277 if (!mom) return MMSYSERR_NOMEM;
278 if (!lpDesc) {
279 HeapFree(GetProcessHeap(), 0, mom);
280 return MMSYSERR_INVALPARAM;
283 if (MIDIMAP_LoadSettings(mom))
285 *lpdwUser = (DWORD_PTR)mom;
286 mom->self = mom;
288 mom->wCbFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
289 mom->midiDesc = *lpDesc;
290 MIDIMAP_NotifyClient(mom, MOM_OPEN, 0L, 0L);
292 return MMSYSERR_NOERROR;
294 HeapFree(GetProcessHeap(), 0, mom);
295 return MIDIERR_INVALIDSETUP;
298 static DWORD modClose(MIDIMAPDATA* mom)
300 UINT i;
301 DWORD ret = MMSYSERR_NOERROR;
303 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
305 for (i = 0; i < 16; i++)
307 DWORD t;
308 if (mom->ChannelMap[i] && mom->ChannelMap[i]->loaded > 0)
310 t = midiOutClose(mom->ChannelMap[i]->hMidi);
311 if (t == MMSYSERR_NOERROR)
313 mom->ChannelMap[i]->loaded = 0;
314 mom->ChannelMap[i]->hMidi = 0;
316 else if (ret == MMSYSERR_NOERROR)
317 ret = t;
320 if (ret == MMSYSERR_NOERROR) {
321 MIDIMAP_NotifyClient(mom, MOM_CLOSE, 0L, 0L);
322 HeapFree(GetProcessHeap(), 0, mom);
324 return ret;
327 static DWORD modLongData(MIDIMAPDATA* mom, LPMIDIHDR lpMidiHdr, DWORD_PTR dwParam2)
329 WORD chn;
330 DWORD ret = MMSYSERR_NOERROR;
331 MIDIHDR mh;
333 if (MIDIMAP_IsBadData(mom))
334 return MMSYSERR_ERROR;
335 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
336 return MIDIERR_UNPREPARED;
337 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
338 return MIDIERR_STILLPLAYING;
340 mh = *lpMidiHdr;
341 lpMidiHdr->dwFlags &= ~MHDR_DONE;
342 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
343 for (chn = 0; chn < 16; chn++)
345 if (mom->ChannelMap[chn] && mom->ChannelMap[chn]->loaded > 0)
347 mh.dwFlags = 0;
348 midiOutPrepareHeader(mom->ChannelMap[chn]->hMidi, &mh, sizeof(mh));
349 ret = midiOutLongMsg(mom->ChannelMap[chn]->hMidi, &mh, sizeof(mh));
350 /* As of 2009, wineXYZ.drv's LongData handlers are synchronous */
351 if (!ret && !(mh.dwFlags & MHDR_DONE))
352 FIXME("wait until MHDR_DONE\n");
353 midiOutUnprepareHeader(mom->ChannelMap[chn]->hMidi, &mh, sizeof(mh));
354 if (ret != MMSYSERR_NOERROR) break;
357 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
358 lpMidiHdr->dwFlags |= MHDR_DONE;
359 MIDIMAP_NotifyClient(mom, MOM_DONE, (DWORD_PTR)lpMidiHdr, 0L);
360 return ret;
363 static DWORD modData(MIDIMAPDATA* mom, DWORD_PTR dwParam)
365 BYTE lb = LOBYTE(LOWORD(dwParam));
366 WORD chn = lb & 0x0F;
367 DWORD ret = MMSYSERR_NOERROR;
369 if (MIDIMAP_IsBadData(mom))
370 return MMSYSERR_ERROR;
372 if (!mom->ChannelMap[chn]) return MMSYSERR_NOERROR;
374 switch (lb & 0xF0)
376 case 0x80:
377 case 0x90:
378 case 0xA0:
379 case 0xB0:
380 case 0xC0:
381 case 0xD0:
382 case 0xE0:
383 if (mom->ChannelMap[chn]->loaded == 0)
385 if (midiOutOpen(&mom->ChannelMap[chn]->hMidi, mom->ChannelMap[chn]->uDevID,
386 0L, 0L, CALLBACK_NULL) == MMSYSERR_NOERROR)
387 mom->ChannelMap[chn]->loaded = 1;
388 else
389 mom->ChannelMap[chn]->loaded = -1;
390 /* FIXME: should load here the IDF midi data... and allow channel and
391 * patch mappings
394 if (mom->ChannelMap[chn]->loaded > 0)
396 /* change channel */
397 dwParam &= ~0x0F;
398 dwParam |= mom->ChannelMap[chn]->aChn[chn];
400 if ((LOBYTE(LOWORD(dwParam)) & 0xF0) == 0xC0 /* program change */ &&
401 mom->ChannelMap[chn]->lpbPatch)
403 BYTE patch = HIBYTE(LOWORD(dwParam));
405 /* change patch */
406 dwParam &= ~0x0000FF00;
407 dwParam |= mom->ChannelMap[chn]->lpbPatch[patch];
409 ret = midiOutShortMsg(mom->ChannelMap[chn]->hMidi, dwParam);
411 break;
412 case 0xF0:
413 for (chn = 0; chn < 16; chn++)
415 if (mom->ChannelMap[chn]->loaded > 0)
416 ret = midiOutShortMsg(mom->ChannelMap[chn]->hMidi, dwParam);
418 break;
419 default:
420 FIXME("ooch %lx\n", dwParam);
423 return ret;
426 static DWORD modPrepare(MIDIMAPDATA* mom, LPMIDIHDR lpMidiHdr, DWORD_PTR dwSize)
428 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
429 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0 || lpMidiHdr->lpData == 0)
430 return MMSYSERR_INVALPARAM;
431 if (lpMidiHdr->dwFlags & MHDR_PREPARED)
432 return MMSYSERR_NOERROR;
434 lpMidiHdr->dwFlags |= MHDR_PREPARED;
435 lpMidiHdr->dwFlags &= ~(MHDR_DONE|MHDR_INQUEUE); /* flags cleared since w2k */
436 return MMSYSERR_NOERROR;
439 static DWORD modUnprepare(MIDIMAPDATA* mom, LPMIDIHDR lpMidiHdr, DWORD_PTR dwSize)
441 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
442 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0 || lpMidiHdr->lpData == 0)
443 return MMSYSERR_INVALPARAM;
444 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
445 return MMSYSERR_NOERROR;
446 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
447 return MIDIERR_STILLPLAYING;
449 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
450 return MMSYSERR_NOERROR;
453 static DWORD modGetVolume(MIDIMAPDATA* mom, DWORD* lpdwVolume)
455 if (!lpdwVolume) return MMSYSERR_INVALPARAM;
456 *lpdwVolume = 0xFFFFFFFF; /* tests show this initial value */
457 return MMSYSERR_NOERROR;
460 static DWORD modSetVolume(MIDIMAPDATA* mom, DWORD dwVolume)
462 /* Native forwards it to some underlying device
463 * GetVolume returns what was last set here. */
464 FIXME("stub\n");
465 return MMSYSERR_NOERROR;
468 static DWORD modGetDevCaps(UINT wDevID, MIDIMAPDATA* mom, LPMIDIOUTCAPSW lpMidiCaps, DWORD_PTR size)
470 static const MIDIOUTCAPSW mappercaps = {
471 0x00FF, 0x0001, 0x0100, /* Manufacturer and Product ID */
472 {'W','i','n','e',' ','m','i','d','i',' ','m','a','p','p','e','r',0},
473 MOD_MAPPER, 0, 0, 0xFFFF,
474 /* Native returns volume caps of underlying device + MIDICAPS_STREAM */
475 MIDICAPS_VOLUME|MIDICAPS_LRVOLUME
477 if (lpMidiCaps == NULL) return MMSYSERR_INVALPARAM;
478 if (!numMidiOutPorts) return MMSYSERR_BADDEVICEID;
480 memcpy(lpMidiCaps, &mappercaps, min(size, sizeof(*lpMidiCaps)));
481 return MMSYSERR_NOERROR;
484 static DWORD modReset(MIDIMAPDATA* mom)
486 WORD chn;
487 DWORD ret = MMSYSERR_NOERROR;
489 if (MIDIMAP_IsBadData(mom))
490 return MMSYSERR_ERROR;
492 for (chn = 0; chn < 16; chn++)
494 if (mom->ChannelMap[chn] && mom->ChannelMap[chn]->loaded > 0)
496 ret = midiOutReset(mom->ChannelMap[chn]->hMidi);
497 if (ret != MMSYSERR_NOERROR) break;
500 return ret;
503 static LRESULT MIDIMAP_drvOpen(void);
504 static LRESULT MIDIMAP_drvClose(void);
506 /**************************************************************************
507 * modMessage (MIDIMAP.@)
509 DWORD WINAPI MIDIMAP_modMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
510 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
512 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
513 wDevID, wMsg, dwUser, dwParam1, dwParam2);
515 switch (wMsg)
517 case DRVM_INIT:
518 return MIDIMAP_drvOpen();
519 case DRVM_EXIT:
520 return MIDIMAP_drvClose();
521 case DRVM_ENABLE:
522 case DRVM_DISABLE:
523 /* FIXME: Pretend this is supported */
524 return 0;
526 case MODM_OPEN: return modOpen((DWORD_PTR *)dwUser, (LPMIDIOPENDESC)dwParam1, dwParam2);
527 case MODM_CLOSE: return modClose ((MIDIMAPDATA*)dwUser);
529 case MODM_DATA: return modData ((MIDIMAPDATA*)dwUser, dwParam1);
530 case MODM_LONGDATA: return modLongData ((MIDIMAPDATA*)dwUser, (LPMIDIHDR)dwParam1, dwParam2);
531 case MODM_PREPARE: return modPrepare ((MIDIMAPDATA*)dwUser, (LPMIDIHDR)dwParam1, dwParam2);
532 case MODM_UNPREPARE: return modUnprepare ((MIDIMAPDATA*)dwUser, (LPMIDIHDR)dwParam1, dwParam2);
533 case MODM_RESET: return modReset ((MIDIMAPDATA*)dwUser);
535 case MODM_GETDEVCAPS: return modGetDevCaps (wDevID, (MIDIMAPDATA*)dwUser, (LPMIDIOUTCAPSW)dwParam1,dwParam2);
536 case MODM_GETNUMDEVS: return 1;
537 case MODM_GETVOLUME: return modGetVolume ((MIDIMAPDATA*)dwUser, (DWORD*)dwParam1);
538 case MODM_SETVOLUME: return modSetVolume ((MIDIMAPDATA*)dwUser, dwParam1);
539 default:
540 FIXME("unknown message %d!\n", wMsg);
542 return MMSYSERR_NOTSUPPORTED;
545 /*======================================================================*
546 * Driver part *
547 *======================================================================*/
549 /**************************************************************************
550 * MIDIMAP_drvOpen [internal]
552 static LRESULT MIDIMAP_drvOpen(void)
554 MIDIOUTCAPSW moc;
555 unsigned dev, i;
557 if (midiOutPorts)
558 return 0;
560 numMidiOutPorts = midiOutGetNumDevs();
561 midiOutPorts = HeapAlloc(GetProcessHeap(), 0,
562 numMidiOutPorts * sizeof(MIDIOUTPORT));
563 for (dev = 0; dev < numMidiOutPorts; dev++)
565 if (midiOutGetDevCapsW(dev, &moc, sizeof(moc)) == 0L)
567 strcpyW(midiOutPorts[dev].name, moc.szPname);
568 midiOutPorts[dev].loaded = 0;
569 midiOutPorts[dev].hMidi = 0;
570 midiOutPorts[dev].uDevID = dev;
571 midiOutPorts[dev].lpbPatch = NULL;
572 for (i = 0; i < 16; i++)
573 midiOutPorts[dev].aChn[i] = i;
575 else
577 midiOutPorts[dev].loaded = -1;
581 return 1;
584 /**************************************************************************
585 * MIDIMAP_drvClose [internal]
587 static LRESULT MIDIMAP_drvClose(void)
589 if (midiOutPorts)
591 HeapFree(GetProcessHeap(), 0, midiOutPorts);
592 midiOutPorts = NULL;
593 return 1;
595 return 0;
598 /**************************************************************************
599 * DriverProc (MIDIMAP.@)
601 LRESULT CALLBACK MIDIMAP_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
602 LPARAM dwParam1, LPARAM dwParam2)
604 /* EPP TRACE("(%08lX, %04X, %08lX, %08lX, %08lX)\n", */
605 /* EPP dwDevID, hDriv, wMsg, dwParam1, dwParam2); */
607 switch (wMsg)
609 case DRV_LOAD: return 1;
610 case DRV_FREE: return 1;
611 case DRV_OPEN: return 1;
612 case DRV_CLOSE: return 1;
613 case DRV_ENABLE: return 1;
614 case DRV_DISABLE: return 1;
615 case DRV_QUERYCONFIGURE: return 1;
616 case DRV_CONFIGURE: MessageBoxA(0, "MIDIMAP MultiMedia Driver !", "OSS Driver", MB_OK); return 1;
617 case DRV_INSTALL: return DRVCNF_RESTART;
618 case DRV_REMOVE: return DRVCNF_RESTART;
619 default:
620 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);