midimap: Implement callback/notification.
[wine.git] / dlls / midimap / midimap.c
blob77b859684d47546984f146c7c95ba6e42e1acc26
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 == '#' && isdigit(name[1]))
121 const WCHAR* ptr = name + 1;
122 *dev = 0;
125 *dev = *dev * 10 + *ptr - '0';
126 } while (isdigit(*++ptr));
127 if (*dev < numMidiOutPorts)
128 return TRUE;
130 return FALSE;
133 static BOOL MIDIMAP_LoadSettingsDefault(MIDIMAPDATA* mom, const WCHAR* port)
135 unsigned i, dev = 0;
137 if (port != NULL && !MIDIMAP_FindPort(port, &dev))
139 ERR("Registry glitch: couldn't find midi out (%s)\n", wine_dbgstr_w(port));
140 dev = 0;
143 /* this is necessary when no midi out ports are present */
144 if (dev >= numMidiOutPorts)
145 return FALSE;
146 /* sets default */
147 for (i = 0; i < 16; i++) mom->ChannelMap[i] = &midiOutPorts[dev];
149 return TRUE;
152 static BOOL MIDIMAP_LoadSettingsScheme(MIDIMAPDATA* mom, const WCHAR* scheme)
154 HKEY hSchemesKey, hKey, hPortKey;
155 unsigned i, idx, dev;
156 WCHAR buffer[256], port[256];
157 DWORD type, size, mask;
159 for (i = 0; i < 16; i++) mom->ChannelMap[i] = NULL;
161 if (RegOpenKeyA(HKEY_LOCAL_MACHINE,
162 "System\\CurrentControlSet\\Control\\MediaProperties\\PrivateProperties\\Midi\\Schemes",
163 &hSchemesKey))
165 return FALSE;
167 if (RegOpenKeyW(hSchemesKey, scheme, &hKey))
169 RegCloseKey(hSchemesKey);
170 return FALSE;
173 for (idx = 0; !RegEnumKeyW(hKey, idx, buffer, sizeof(buffer)/sizeof(buffer[0])); idx++)
175 if (RegOpenKeyW(hKey, buffer, &hPortKey)) continue;
177 size = sizeof(port);
178 if (RegQueryValueExW(hPortKey, NULL, 0, &type, (void*)port, &size)) continue;
180 if (!MIDIMAP_FindPort(port, &dev)) continue;
182 size = sizeof(mask);
183 if (RegQueryValueExA(hPortKey, "Channels", 0, &type, (void*)&mask, &size))
184 continue;
186 for (i = 0; i < 16; i++)
188 if (mask & (1 << i))
190 if (mom->ChannelMap[i])
191 ERR("Quirks in registry, channel %u is mapped twice\n", i);
192 mom->ChannelMap[i] = &midiOutPorts[dev];
197 RegCloseKey(hSchemesKey);
198 RegCloseKey(hKey);
200 return TRUE;
203 static BOOL MIDIMAP_LoadSettings(MIDIMAPDATA* mom)
205 HKEY hKey;
206 BOOL ret;
208 if (RegOpenKeyA(HKEY_CURRENT_USER,
209 "Software\\Microsoft\\Windows\\CurrentVersion\\Multimedia\\MIDIMap", &hKey))
211 ret = MIDIMAP_LoadSettingsDefault(mom, NULL);
213 else
215 DWORD type, size, out;
216 WCHAR buffer[256];
218 ret = 2;
219 size = sizeof(out);
220 if (!RegQueryValueExA(hKey, "UseScheme", 0, &type, (void*)&out, &size) && out)
222 static const WCHAR cs[] = {'C','u','r','r','e','n','t','S','c','h','e','m','e',0};
223 size = sizeof(buffer);
224 if (!RegQueryValueExW(hKey, cs, 0, &type, (void*)buffer, &size))
226 if (!(ret = MIDIMAP_LoadSettingsScheme(mom, buffer)))
227 ret = MIDIMAP_LoadSettingsDefault(mom, NULL);
229 else
231 ERR("Wrong registry: UseScheme is active, but no CurrentScheme found\n");
234 if (ret == 2)
236 static const WCHAR ci[] = {'C','u','r','r','e','n','t','I','n','s','t','r','u','m','e','n','t',0};
237 size = sizeof(buffer);
238 if (!RegQueryValueExW(hKey, ci, 0, &type, (void*)buffer, &size) && *buffer)
240 ret = MIDIMAP_LoadSettingsDefault(mom, buffer);
242 else
244 ret = MIDIMAP_LoadSettingsDefault(mom, NULL);
248 RegCloseKey(hKey);
250 if (ret && TRACE_ON(msacm))
252 unsigned i;
254 for (i = 0; i < 16; i++)
256 TRACE("chnMap[%2d] => %d\n",
257 i, mom->ChannelMap[i] ? mom->ChannelMap[i]->uDevID : -1);
260 return ret;
263 static void MIDIMAP_NotifyClient(MIDIMAPDATA* mom, WORD wMsg,
264 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
266 DriverCallback(mom->midiDesc.dwCallback, mom->wCbFlags, (HDRVR)mom->midiDesc.hMidi,
267 wMsg, mom->midiDesc.dwInstance, dwParam1, dwParam2);
270 static DWORD modOpen(DWORD_PTR *lpdwUser, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
272 MIDIMAPDATA* mom = HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIMAPDATA));
274 TRACE("(%p %p %08x)\n", lpdwUser, lpDesc, dwFlags);
276 if (!mom) return MMSYSERR_NOMEM;
277 if (!lpDesc) {
278 HeapFree(GetProcessHeap(), 0, mom);
279 return MMSYSERR_INVALPARAM;
282 if (MIDIMAP_LoadSettings(mom))
284 *lpdwUser = (DWORD_PTR)mom;
285 mom->self = mom;
287 mom->wCbFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
288 mom->midiDesc = *lpDesc;
289 MIDIMAP_NotifyClient(mom, MOM_OPEN, 0L, 0L);
291 return MMSYSERR_NOERROR;
293 HeapFree(GetProcessHeap(), 0, mom);
294 return MIDIERR_INVALIDSETUP;
297 static DWORD modClose(MIDIMAPDATA* mom)
299 UINT i;
300 DWORD ret = MMSYSERR_NOERROR;
302 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
304 for (i = 0; i < 16; i++)
306 DWORD t;
307 if (mom->ChannelMap[i] && mom->ChannelMap[i]->loaded > 0)
309 t = midiOutClose(mom->ChannelMap[i]->hMidi);
310 if (t == MMSYSERR_NOERROR)
312 mom->ChannelMap[i]->loaded = 0;
313 mom->ChannelMap[i]->hMidi = 0;
315 else if (ret == MMSYSERR_NOERROR)
316 ret = t;
319 if (ret == MMSYSERR_NOERROR) {
320 MIDIMAP_NotifyClient(mom, MOM_CLOSE, 0L, 0L);
321 HeapFree(GetProcessHeap(), 0, mom);
323 return ret;
326 static DWORD modLongData(MIDIMAPDATA* mom, LPMIDIHDR lpMidiHdr, DWORD_PTR dwParam2)
328 WORD chn;
329 DWORD ret = MMSYSERR_NOERROR;
330 MIDIHDR mh;
332 if (MIDIMAP_IsBadData(mom))
333 return MMSYSERR_ERROR;
334 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
335 return MIDIERR_UNPREPARED;
336 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
337 return MIDIERR_STILLPLAYING;
339 mh = *lpMidiHdr;
340 lpMidiHdr->dwFlags &= ~MHDR_DONE;
341 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
342 for (chn = 0; chn < 16; chn++)
344 if (mom->ChannelMap[chn] && mom->ChannelMap[chn]->loaded > 0)
346 mh.dwFlags = 0;
347 midiOutPrepareHeader(mom->ChannelMap[chn]->hMidi, &mh, sizeof(mh));
348 ret = midiOutLongMsg(mom->ChannelMap[chn]->hMidi, &mh, sizeof(mh));
349 /* As of 2009, wineXYZ.drv's LongData handlers are synchronous */
350 if (!ret && !(mh.dwFlags & MHDR_DONE))
351 FIXME("wait until MHDR_DONE\n");
352 midiOutUnprepareHeader(mom->ChannelMap[chn]->hMidi, &mh, sizeof(mh));
353 if (ret != MMSYSERR_NOERROR) break;
356 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
357 lpMidiHdr->dwFlags |= MHDR_DONE;
358 MIDIMAP_NotifyClient(mom, MOM_DONE, (DWORD_PTR)lpMidiHdr, 0L);
359 return ret;
362 static DWORD modData(MIDIMAPDATA* mom, DWORD_PTR dwParam)
364 BYTE lb = LOBYTE(LOWORD(dwParam));
365 WORD chn = lb & 0x0F;
366 DWORD ret = MMSYSERR_NOERROR;
368 if (MIDIMAP_IsBadData(mom))
369 return MMSYSERR_ERROR;
371 if (!mom->ChannelMap[chn]) return MMSYSERR_NOERROR;
373 switch (lb & 0xF0)
375 case 0x80:
376 case 0x90:
377 case 0xA0:
378 case 0xB0:
379 case 0xC0:
380 case 0xD0:
381 case 0xE0:
382 if (mom->ChannelMap[chn]->loaded == 0)
384 if (midiOutOpen(&mom->ChannelMap[chn]->hMidi, mom->ChannelMap[chn]->uDevID,
385 0L, 0L, CALLBACK_NULL) == MMSYSERR_NOERROR)
386 mom->ChannelMap[chn]->loaded = 1;
387 else
388 mom->ChannelMap[chn]->loaded = -1;
389 /* FIXME: should load here the IDF midi data... and allow channel and
390 * patch mappings
393 if (mom->ChannelMap[chn]->loaded > 0)
395 /* change channel */
396 dwParam &= ~0x0F;
397 dwParam |= mom->ChannelMap[chn]->aChn[chn];
399 if ((LOBYTE(LOWORD(dwParam)) & 0xF0) == 0xC0 /* program change */ &&
400 mom->ChannelMap[chn]->lpbPatch)
402 BYTE patch = HIBYTE(LOWORD(dwParam));
404 /* change patch */
405 dwParam &= ~0x0000FF00;
406 dwParam |= mom->ChannelMap[chn]->lpbPatch[patch];
408 ret = midiOutShortMsg(mom->ChannelMap[chn]->hMidi, dwParam);
410 break;
411 case 0xF0:
412 for (chn = 0; chn < 16; chn++)
414 if (mom->ChannelMap[chn]->loaded > 0)
415 ret = midiOutShortMsg(mom->ChannelMap[chn]->hMidi, dwParam);
417 break;
418 default:
419 FIXME("ooch %lx\n", dwParam);
422 return ret;
425 static DWORD modPrepare(MIDIMAPDATA* mom, LPMIDIHDR lpMidiHdr, DWORD_PTR dwSize)
427 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
428 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
429 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE))
430 return MMSYSERR_INVALPARAM;
432 lpMidiHdr->dwFlags |= MHDR_PREPARED;
433 lpMidiHdr->dwFlags &= ~MHDR_DONE;
434 return MMSYSERR_NOERROR;
437 static DWORD modUnprepare(MIDIMAPDATA* mom, LPMIDIHDR lpMidiHdr, DWORD_PTR dwParam2)
439 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
440 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
441 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
443 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
444 return MMSYSERR_NOERROR;
447 static DWORD modGetVolume(MIDIMAPDATA* mom, DWORD* lpdwVolume)
449 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
450 if (!lpdwVolume) return MMSYSERR_INVALPARAM;
451 *lpdwVolume = 0xFFFFFFFF; /* tests show this initial value */
452 return MMSYSERR_NOERROR;
455 static DWORD modSetVolume(MIDIMAPDATA* mom, DWORD dwVolume)
457 /* Native forwards it to some underlying device
458 * GetVolume returns what was last set here. */
459 FIXME("stub\n");
460 return MMSYSERR_NOERROR;
463 static DWORD modGetDevCaps(UINT wDevID, MIDIMAPDATA* mom, LPMIDIOUTCAPSW lpMidiCaps, DWORD_PTR size)
465 static const MIDIOUTCAPSW mappercaps = {
466 0x00FF, 0x0001, 0x0100, /* Manufacturer and Product ID */
467 {'W','i','n','e',' ','m','i','d','i',' ','m','a','p','p','e','r',0},
468 MOD_MAPPER, 0, 0, 0xFFFF,
469 /* Native returns volume caps of underlying device + MIDICAPS_STREAM */
470 MIDICAPS_VOLUME|MIDICAPS_LRVOLUME
472 if (lpMidiCaps == NULL) return MMSYSERR_INVALPARAM;
474 memcpy(lpMidiCaps, &mappercaps, min(size, sizeof(*lpMidiCaps)));
475 return MMSYSERR_NOERROR;
478 static DWORD modReset(MIDIMAPDATA* mom)
480 WORD chn;
481 DWORD ret = MMSYSERR_NOERROR;
483 if (MIDIMAP_IsBadData(mom))
484 return MMSYSERR_ERROR;
486 for (chn = 0; chn < 16; chn++)
488 if (mom->ChannelMap[chn] && mom->ChannelMap[chn]->loaded > 0)
490 ret = midiOutReset(mom->ChannelMap[chn]->hMidi);
491 if (ret != MMSYSERR_NOERROR) break;
494 return ret;
497 /**************************************************************************
498 * modMessage (MIDIMAP.@)
500 DWORD WINAPI MIDIMAP_modMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
501 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
503 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
504 wDevID, wMsg, dwUser, dwParam1, dwParam2);
506 switch (wMsg)
508 case DRVM_INIT:
509 case DRVM_EXIT:
510 case DRVM_ENABLE:
511 case DRVM_DISABLE:
512 /* FIXME: Pretend this is supported */
513 return 0;
515 case MODM_OPEN: return modOpen((DWORD_PTR *)dwUser, (LPMIDIOPENDESC)dwParam1, dwParam2);
516 case MODM_CLOSE: return modClose ((MIDIMAPDATA*)dwUser);
518 case MODM_DATA: return modData ((MIDIMAPDATA*)dwUser, dwParam1);
519 case MODM_LONGDATA: return modLongData ((MIDIMAPDATA*)dwUser, (LPMIDIHDR)dwParam1, dwParam2);
520 case MODM_PREPARE: return modPrepare ((MIDIMAPDATA*)dwUser, (LPMIDIHDR)dwParam1, dwParam2);
521 case MODM_UNPREPARE: return modUnprepare ((MIDIMAPDATA*)dwUser, (LPMIDIHDR)dwParam1, dwParam2);
522 case MODM_RESET: return modReset ((MIDIMAPDATA*)dwUser);
524 case MODM_GETDEVCAPS: return modGetDevCaps (wDevID, (MIDIMAPDATA*)dwUser, (LPMIDIOUTCAPSW)dwParam1,dwParam2);
525 case MODM_GETNUMDEVS: return 1;
526 case MODM_GETVOLUME: return modGetVolume ((MIDIMAPDATA*)dwUser, (DWORD*)dwParam1);
527 case MODM_SETVOLUME: return modSetVolume ((MIDIMAPDATA*)dwUser, dwParam1);
528 default:
529 FIXME("unknown message %d!\n", wMsg);
531 return MMSYSERR_NOTSUPPORTED;
534 /*======================================================================*
535 * Driver part *
536 *======================================================================*/
538 /**************************************************************************
539 * MIDIMAP_drvOpen [internal]
541 static LRESULT MIDIMAP_drvOpen(LPSTR str)
543 MIDIOUTCAPSW moc;
544 unsigned dev, i;
546 if (midiOutPorts)
547 return 0;
549 numMidiOutPorts = midiOutGetNumDevs();
550 midiOutPorts = HeapAlloc(GetProcessHeap(), 0,
551 numMidiOutPorts * sizeof(MIDIOUTPORT));
552 for (dev = 0; dev < numMidiOutPorts; dev++)
554 if (midiOutGetDevCapsW(dev, &moc, sizeof(moc)) == 0L)
556 strcpyW(midiOutPorts[dev].name, moc.szPname);
557 midiOutPorts[dev].loaded = 0;
558 midiOutPorts[dev].hMidi = 0;
559 midiOutPorts[dev].uDevID = dev;
560 midiOutPorts[dev].lpbPatch = NULL;
561 for (i = 0; i < 16; i++)
562 midiOutPorts[dev].aChn[i] = i;
564 else
566 midiOutPorts[dev].loaded = -1;
570 return 1;
573 /**************************************************************************
574 * MIDIMAP_drvClose [internal]
576 static LRESULT MIDIMAP_drvClose(DWORD_PTR dwDevID)
578 if (midiOutPorts)
580 HeapFree(GetProcessHeap(), 0, midiOutPorts);
581 midiOutPorts = NULL;
582 return 1;
584 return 0;
587 /**************************************************************************
588 * DriverProc (MIDIMAP.@)
590 LRESULT CALLBACK MIDIMAP_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
591 LPARAM dwParam1, LPARAM dwParam2)
593 /* EPP TRACE("(%08lX, %04X, %08lX, %08lX, %08lX)\n", */
594 /* EPP dwDevID, hDriv, wMsg, dwParam1, dwParam2); */
596 switch (wMsg)
598 case DRV_LOAD: return 1;
599 case DRV_FREE: return 1;
600 case DRV_OPEN: return MIDIMAP_drvOpen((LPSTR)dwParam1);
601 case DRV_CLOSE: return MIDIMAP_drvClose(dwDevID);
602 case DRV_ENABLE: return 1;
603 case DRV_DISABLE: return 1;
604 case DRV_QUERYCONFIGURE: return 1;
605 case DRV_CONFIGURE: MessageBoxA(0, "MIDIMAP MultiMedia Driver !", "OSS Driver", MB_OK); return 1;
606 case DRV_INSTALL: return DRVCNF_RESTART;
607 case DRV_REMOVE: return DRVCNF_RESTART;
608 default:
609 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);