cmd: Remove some superfluous casts.
[wine/wine64.git] / dlls / midimap / midimap.c
blob120a054ad73640534601d1578fd8e8f58e97009b
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 } MIDIMAPDATA;
97 static MIDIOUTPORT* midiOutPorts;
98 static unsigned numMidiOutPorts;
100 static BOOL MIDIMAP_IsBadData(MIDIMAPDATA* mm)
102 if (!IsBadReadPtr(mm, sizeof(MIDIMAPDATA)) && mm->self == mm)
103 return FALSE;
104 TRACE("Bad midimap data (%p)\n", mm);
105 return TRUE;
108 static BOOL MIDIMAP_FindPort(const WCHAR* name, unsigned* dev)
110 for (*dev = 0; *dev < numMidiOutPorts; (*dev)++)
112 TRACE("%s\n", wine_dbgstr_w(midiOutPorts[*dev].name));
113 if (strcmpW(midiOutPorts[*dev].name, name) == 0)
114 return TRUE;
116 /* try the form #nnn */
117 if (*name == '#' && isdigit(name[1]))
119 const WCHAR* ptr = name + 1;
120 *dev = 0;
123 *dev = *dev * 10 + *ptr - '0';
124 } while (isdigit(*++ptr));
125 if (*dev < numMidiOutPorts)
126 return TRUE;
128 return FALSE;
131 static BOOL MIDIMAP_LoadSettingsDefault(MIDIMAPDATA* mom, const WCHAR* port)
133 unsigned i, dev = 0;
135 if (port != NULL && !MIDIMAP_FindPort(port, &dev))
137 ERR("Registry glitch: couldn't find midi out (%s)\n", wine_dbgstr_w(port));
138 dev = 0;
141 /* this is necessary when no midi out ports are present */
142 if (dev >= numMidiOutPorts)
143 return FALSE;
144 /* sets default */
145 for (i = 0; i < 16; i++) mom->ChannelMap[i] = &midiOutPorts[dev];
147 return TRUE;
150 static BOOL MIDIMAP_LoadSettingsScheme(MIDIMAPDATA* mom, const WCHAR* scheme)
152 HKEY hSchemesKey, hKey, hPortKey;
153 unsigned i, idx, dev;
154 WCHAR buffer[256], port[256];
155 DWORD type, size, mask;
157 for (i = 0; i < 16; i++) mom->ChannelMap[i] = NULL;
159 if (RegOpenKeyA(HKEY_LOCAL_MACHINE,
160 "System\\CurrentControlSet\\Control\\MediaProperties\\PrivateProperties\\Midi\\Schemes",
161 &hSchemesKey))
163 return FALSE;
165 if (RegOpenKeyW(hSchemesKey, scheme, &hKey))
167 RegCloseKey(hSchemesKey);
168 return FALSE;
171 for (idx = 0; !RegEnumKeyW(hKey, idx, buffer, sizeof(buffer)/sizeof(buffer[0])); idx++)
173 if (RegOpenKeyW(hKey, buffer, &hPortKey)) continue;
175 size = sizeof(port);
176 if (RegQueryValueExW(hPortKey, NULL, 0, &type, (void*)port, &size)) continue;
178 if (!MIDIMAP_FindPort(port, &dev)) continue;
180 size = sizeof(mask);
181 if (RegQueryValueExA(hPortKey, "Channels", 0, &type, (void*)&mask, &size))
182 continue;
184 for (i = 0; i < 16; i++)
186 if (mask & (1 << i))
188 if (mom->ChannelMap[i])
189 ERR("Quirks in registry, channel %u is mapped twice\n", i);
190 mom->ChannelMap[i] = &midiOutPorts[dev];
195 RegCloseKey(hSchemesKey);
196 RegCloseKey(hKey);
198 return TRUE;
201 static BOOL MIDIMAP_LoadSettings(MIDIMAPDATA* mom)
203 HKEY hKey;
204 BOOL ret;
206 if (RegOpenKeyA(HKEY_CURRENT_USER,
207 "Software\\Microsoft\\Windows\\CurrentVersion\\Multimedia\\MIDIMap", &hKey))
209 ret = MIDIMAP_LoadSettingsDefault(mom, NULL);
211 else
213 DWORD type, size, out;
214 WCHAR buffer[256];
216 ret = 2;
217 size = sizeof(out);
218 if (!RegQueryValueExA(hKey, "UseScheme", 0, &type, (void*)&out, &size) && out)
220 static const WCHAR cs[] = {'C','u','r','r','e','n','t','S','c','h','e','m','e',0};
221 size = sizeof(buffer);
222 if (!RegQueryValueExW(hKey, cs, 0, &type, (void*)buffer, &size))
224 if (!(ret = MIDIMAP_LoadSettingsScheme(mom, buffer)))
225 ret = MIDIMAP_LoadSettingsDefault(mom, NULL);
227 else
229 ERR("Wrong registry: UseScheme is active, but no CurrentScheme found\n");
232 if (ret == 2)
234 static const WCHAR ci[] = {'C','u','r','r','e','n','t','I','n','s','t','r','u','m','e','n','t',0};
235 size = sizeof(buffer);
236 if (!RegQueryValueExW(hKey, ci, 0, &type, (void*)buffer, &size) && *buffer)
238 ret = MIDIMAP_LoadSettingsDefault(mom, buffer);
240 else
242 ret = MIDIMAP_LoadSettingsDefault(mom, NULL);
246 RegCloseKey(hKey);
248 if (ret && TRACE_ON(msacm))
250 unsigned i;
252 for (i = 0; i < 16; i++)
254 TRACE("chnMap[%2d] => %d\n",
255 i, mom->ChannelMap[i] ? mom->ChannelMap[i]->uDevID : -1);
258 return ret;
261 static DWORD modOpen(DWORD_PTR *lpdwUser, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
263 MIDIMAPDATA* mom = HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIMAPDATA));
265 TRACE("(%p %p %08x)\n", lpdwUser, lpDesc, dwFlags);
267 if (!mom) return MMSYSERR_NOMEM;
269 if (MIDIMAP_LoadSettings(mom))
271 *lpdwUser = (DWORD_PTR)mom;
272 mom->self = mom;
274 return MMSYSERR_NOERROR;
276 HeapFree(GetProcessHeap(), 0, mom);
277 return MIDIERR_INVALIDSETUP;
280 static DWORD modClose(MIDIMAPDATA* mom)
282 UINT i;
283 DWORD ret = MMSYSERR_NOERROR;
285 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
287 for (i = 0; i < 16; i++)
289 DWORD t;
290 if (mom->ChannelMap[i] && mom->ChannelMap[i]->loaded > 0)
292 t = midiOutClose(mom->ChannelMap[i]->hMidi);
293 if (t == MMSYSERR_NOERROR)
295 mom->ChannelMap[i]->loaded = 0;
296 mom->ChannelMap[i]->hMidi = 0;
298 else if (ret == MMSYSERR_NOERROR)
299 ret = t;
302 if (ret == MMSYSERR_NOERROR)
303 HeapFree(GetProcessHeap(), 0, mom);
304 return ret;
307 static DWORD modLongData(MIDIMAPDATA* mom, LPMIDIHDR lpMidiHdr, DWORD_PTR dwParam2)
309 WORD chn;
310 DWORD ret = MMSYSERR_NOERROR;
311 MIDIHDR mh;
313 if (MIDIMAP_IsBadData(mom))
314 return MMSYSERR_ERROR;
316 mh = *lpMidiHdr;
317 for (chn = 0; chn < 16; chn++)
319 if (mom->ChannelMap[chn] && mom->ChannelMap[chn]->loaded > 0)
321 mh.dwFlags = 0;
322 midiOutPrepareHeader(mom->ChannelMap[chn]->hMidi, &mh, sizeof(mh));
323 ret = midiOutLongMsg(mom->ChannelMap[chn]->hMidi, &mh, sizeof(mh));
324 midiOutUnprepareHeader(mom->ChannelMap[chn]->hMidi, &mh, sizeof(mh));
325 if (ret != MMSYSERR_NOERROR) break;
328 return ret;
331 static DWORD modData(MIDIMAPDATA* mom, DWORD_PTR dwParam)
333 BYTE lb = LOBYTE(LOWORD(dwParam));
334 WORD chn = lb & 0x0F;
335 DWORD ret = MMSYSERR_NOERROR;
337 if (MIDIMAP_IsBadData(mom))
338 return MMSYSERR_ERROR;
340 if (!mom->ChannelMap[chn]) return MMSYSERR_NOERROR;
342 switch (lb & 0xF0)
344 case 0x80:
345 case 0x90:
346 case 0xA0:
347 case 0xB0:
348 case 0xC0:
349 case 0xD0:
350 case 0xE0:
351 if (mom->ChannelMap[chn]->loaded == 0)
353 if (midiOutOpen(&mom->ChannelMap[chn]->hMidi, mom->ChannelMap[chn]->uDevID,
354 0L, 0L, CALLBACK_NULL) == MMSYSERR_NOERROR)
355 mom->ChannelMap[chn]->loaded = 1;
356 else
357 mom->ChannelMap[chn]->loaded = -1;
358 /* FIXME: should load here the IDF midi data... and allow channel and
359 * patch mappings
362 if (mom->ChannelMap[chn]->loaded > 0)
364 /* change channel */
365 dwParam &= ~0x0F;
366 dwParam |= mom->ChannelMap[chn]->aChn[chn];
368 if ((LOBYTE(LOWORD(dwParam)) & 0xF0) == 0xC0 /* program change */ &&
369 mom->ChannelMap[chn]->lpbPatch)
371 BYTE patch = HIBYTE(LOWORD(dwParam));
373 /* change patch */
374 dwParam &= ~0x0000FF00;
375 dwParam |= mom->ChannelMap[chn]->lpbPatch[patch];
377 ret = midiOutShortMsg(mom->ChannelMap[chn]->hMidi, dwParam);
379 break;
380 case 0xF0:
381 for (chn = 0; chn < 16; chn++)
383 if (mom->ChannelMap[chn]->loaded > 0)
384 ret = midiOutShortMsg(mom->ChannelMap[chn]->hMidi, dwParam);
386 break;
387 default:
388 FIXME("ooch %lx\n", dwParam);
391 return ret;
394 static DWORD modPrepare(MIDIMAPDATA* mom, LPMIDIHDR lpMidiHdr, DWORD_PTR dwParam2)
396 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
397 if (lpMidiHdr->dwFlags & (MHDR_ISSTRM|MHDR_PREPARED))
398 return MMSYSERR_INVALPARAM;
400 lpMidiHdr->dwFlags |= MHDR_PREPARED;
401 return MMSYSERR_NOERROR;
404 static DWORD modUnprepare(MIDIMAPDATA* mom, LPMIDIHDR lpMidiHdr, DWORD_PTR dwParam2)
406 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
407 if ((lpMidiHdr->dwFlags & MHDR_ISSTRM) || !(lpMidiHdr->dwFlags & MHDR_PREPARED))
408 return MMSYSERR_INVALPARAM;
410 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
411 return MMSYSERR_NOERROR;
414 static DWORD modGetDevCaps(UINT wDevID, MIDIMAPDATA* mom, LPMIDIOUTCAPSW lpMidiCaps, DWORD_PTR size)
416 static const WCHAR name[] = {'W','i','n','e',' ','m','i','d','i',' ','m','a','p','p','e','r',0};
417 lpMidiCaps->wMid = 0x00FF;
418 lpMidiCaps->wPid = 0x0001;
419 lpMidiCaps->vDriverVersion = 0x0100;
420 lstrcpyW(lpMidiCaps->szPname, name);
421 lpMidiCaps->wTechnology = MOD_MAPPER;
422 lpMidiCaps->wVoices = 0;
423 lpMidiCaps->wNotes = 0;
424 lpMidiCaps->wChannelMask = 0xFFFF;
425 lpMidiCaps->dwSupport = 0L;
427 return MMSYSERR_NOERROR;
430 static DWORD modReset(MIDIMAPDATA* mom)
432 WORD chn;
433 DWORD ret = MMSYSERR_NOERROR;
435 if (MIDIMAP_IsBadData(mom))
436 return MMSYSERR_ERROR;
438 for (chn = 0; chn < 16; chn++)
440 if (mom->ChannelMap[chn] && mom->ChannelMap[chn]->loaded > 0)
442 ret = midiOutReset(mom->ChannelMap[chn]->hMidi);
443 if (ret != MMSYSERR_NOERROR) break;
446 return ret;
449 /**************************************************************************
450 * modMessage (MIDIMAP.@)
452 DWORD WINAPI MIDIMAP_modMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
453 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
455 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
456 wDevID, wMsg, dwUser, dwParam1, dwParam2);
458 switch (wMsg)
460 case DRVM_INIT:
461 case DRVM_EXIT:
462 case DRVM_ENABLE:
463 case DRVM_DISABLE:
464 /* FIXME: Pretend this is supported */
465 return 0;
467 case MODM_OPEN: return modOpen((DWORD_PTR *)dwUser, (LPMIDIOPENDESC)dwParam1, dwParam2);
468 case MODM_CLOSE: return modClose ((MIDIMAPDATA*)dwUser);
470 case MODM_DATA: return modData ((MIDIMAPDATA*)dwUser, dwParam1);
471 case MODM_LONGDATA: return modLongData ((MIDIMAPDATA*)dwUser, (LPMIDIHDR)dwParam1, dwParam2);
472 case MODM_PREPARE: return modPrepare ((MIDIMAPDATA*)dwUser, (LPMIDIHDR)dwParam1, dwParam2);
473 case MODM_UNPREPARE: return modUnprepare ((MIDIMAPDATA*)dwUser, (LPMIDIHDR)dwParam1, dwParam2);
474 case MODM_RESET: return modReset ((MIDIMAPDATA*)dwUser);
476 case MODM_GETDEVCAPS: return modGetDevCaps (wDevID, (MIDIMAPDATA*)dwUser, (LPMIDIOUTCAPSW)dwParam1,dwParam2);
477 case MODM_GETNUMDEVS: return 1;
478 case MODM_GETVOLUME: return MMSYSERR_NOTSUPPORTED;
479 case MODM_SETVOLUME: return MMSYSERR_NOTSUPPORTED;
480 default:
481 FIXME("unknown message %d!\n", wMsg);
483 return MMSYSERR_NOTSUPPORTED;
486 /*======================================================================*
487 * Driver part *
488 *======================================================================*/
490 /**************************************************************************
491 * MIDIMAP_drvOpen [internal]
493 static LRESULT MIDIMAP_drvOpen(LPSTR str)
495 MIDIOUTCAPSW moc;
496 unsigned dev, i;
498 if (midiOutPorts)
499 return 0;
501 numMidiOutPorts = midiOutGetNumDevs();
502 midiOutPorts = HeapAlloc(GetProcessHeap(), 0,
503 numMidiOutPorts * sizeof(MIDIOUTPORT));
504 for (dev = 0; dev < numMidiOutPorts; dev++)
506 if (midiOutGetDevCapsW(dev, &moc, sizeof(moc)) == 0L)
508 strcpyW(midiOutPorts[dev].name, moc.szPname);
509 midiOutPorts[dev].loaded = 0;
510 midiOutPorts[dev].hMidi = 0;
511 midiOutPorts[dev].uDevID = dev;
512 midiOutPorts[dev].lpbPatch = NULL;
513 for (i = 0; i < 16; i++)
514 midiOutPorts[dev].aChn[i] = i;
516 else
518 midiOutPorts[dev].loaded = -1;
522 return 1;
525 /**************************************************************************
526 * MIDIMAP_drvClose [internal]
528 static LRESULT MIDIMAP_drvClose(DWORD_PTR dwDevID)
530 if (midiOutPorts)
532 HeapFree(GetProcessHeap(), 0, midiOutPorts);
533 midiOutPorts = NULL;
534 return 1;
536 return 0;
539 /**************************************************************************
540 * DriverProc (MIDIMAP.@)
542 LRESULT CALLBACK MIDIMAP_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
543 LPARAM dwParam1, LPARAM dwParam2)
545 /* EPP TRACE("(%08lX, %04X, %08lX, %08lX, %08lX)\n", */
546 /* EPP dwDevID, hDriv, wMsg, dwParam1, dwParam2); */
548 switch (wMsg)
550 case DRV_LOAD: return 1;
551 case DRV_FREE: return 1;
552 case DRV_OPEN: return MIDIMAP_drvOpen((LPSTR)dwParam1);
553 case DRV_CLOSE: return MIDIMAP_drvClose(dwDevID);
554 case DRV_ENABLE: return 1;
555 case DRV_DISABLE: return 1;
556 case DRV_QUERYCONFIGURE: return 1;
557 case DRV_CONFIGURE: MessageBoxA(0, "MIDIMAP MultiMedia Driver !", "OSS Driver", MB_OK); return 1;
558 case DRV_INSTALL: return DRVCNF_RESTART;
559 case DRV_REMOVE: return DRVCNF_RESTART;
560 default:
561 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);