msvcrt: Remove non-needed defines from msvcrt.h.
[wine.git] / dlls / midimap / midimap.c
blob86e4289d89776347dce75da657d633a6e55319ba
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/debug.h"
37 * Here's how Windows stores the midiOut mapping information.
39 * Full form (in HKU) is:
41 * [Software\\Microsoft\\Windows\\CurrentVersion\\Multimedia\\MIDIMap] 988836060
42 * "AutoScheme"=dword:00000000
43 * "ConfigureCount"=dword:00000004
44 * "CurrentInstrument"="Wine OSS midi"
45 * "CurrentScheme"="epp"
46 * "DriverList"=""
47 * "UseScheme"=dword:00000000
49 * AutoScheme: ?
50 * CurrentInstrument: name of midiOut device to use when UseScheme is 0. Wine uses an extension
51 * of the form #n to link to n'th midiOut device of the system
52 * CurrentScheme: when UseScheme is non null, it's the scheme to use (see below)
53 * DriverList: ?
54 * UseScheme: trigger for simple/complex mapping
56 * A scheme is defined (in HKLM) as:
58 * [System\\CurrentControlSet\\Control\\MediaProperties\\PrivateProperties\\Midi\\Schemes\\<nameScheme>]
59 * <nameScheme>: one key for each defined scheme (system wide)
60 * under each one of these <nameScheme> keys, there's:
61 * [...\\<nameScheme>\\<idxDevice>]
62 * "Channels"="<bitMask>"
63 * (the default value of this key also refers to the name of the device).
65 * this defines, for each midiOut device (identified by its index in <idxDevice>), which
66 * channels have to be mapped onto it. The <bitMask> defines the channels (from 0 to 15)
67 * will be mapped (mapping occurs for channel <ch> if bit <ch> is set in <bitMask>
69 * Further mapping information can also be defined in:
70 * [System\\CurrentControlSet\\Control\\MediaProperties\\PrivateProperties\\Midi\\Ports\\<nameDevice>\\Instruments\\<idx>]
71 * "Definition"="<.idf file>"
72 * "FriendlyName"="#for .idx file#"
73 * "Port"="<idxPort>"
75 * This last part isn't implemented (.idf file support).
78 WINE_DEFAULT_DEBUG_CHANNEL(msacm);
79 WINE_DECLARE_DEBUG_CHANNEL(winediag);
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 (lstrcmpW(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, ARRAY_SIZE(buffer)); 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 size = sizeof(buffer);
224 if (!RegQueryValueExW(hKey, L"CurrentScheme", 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 size = sizeof(buffer);
237 if (!RegQueryValueExW(hKey, L"CurrentInstrument", 0, &type, (void*)buffer, &size) && *buffer)
239 ret = MIDIMAP_LoadSettingsDefault(mom, buffer);
241 else
243 ret = MIDIMAP_LoadSettingsDefault(mom, NULL);
247 RegCloseKey(hKey);
249 if (ret && TRACE_ON(msacm))
251 unsigned i;
253 for (i = 0; i < 16; i++)
255 TRACE("chnMap[%2d] => %d\n",
256 i, mom->ChannelMap[i] ? mom->ChannelMap[i]->uDevID : -1);
259 return ret;
262 static void MIDIMAP_NotifyClient(MIDIMAPDATA* mom, WORD wMsg,
263 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
265 DriverCallback(mom->midiDesc.dwCallback, mom->wCbFlags, (HDRVR)mom->midiDesc.hMidi,
266 wMsg, mom->midiDesc.dwInstance, dwParam1, dwParam2);
269 static DWORD modOpen(DWORD_PTR *lpdwUser, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
271 MIDIMAPDATA* mom = HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIMAPDATA));
273 TRACE("(%p %p %08x)\n", lpdwUser, lpDesc, dwFlags);
275 if (!mom) return MMSYSERR_NOMEM;
276 if (!lpDesc) {
277 HeapFree(GetProcessHeap(), 0, mom);
278 return MMSYSERR_INVALPARAM;
281 if (MIDIMAP_LoadSettings(mom))
283 UINT chn;
284 *lpdwUser = (DWORD_PTR)mom;
285 mom->self = mom;
286 mom->wCbFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
287 mom->midiDesc = *lpDesc;
289 for (chn = 0; chn < 16; chn++)
291 if (mom->ChannelMap[chn]->loaded) continue;
292 if (midiOutOpen(&mom->ChannelMap[chn]->hMidi, mom->ChannelMap[chn]->uDevID,
293 0L, 0L, CALLBACK_NULL) == MMSYSERR_NOERROR)
294 mom->ChannelMap[chn]->loaded = 1;
295 else
296 mom->ChannelMap[chn]->loaded = -1;
297 /* FIXME: should load here the IDF midi data... and allow channel and
298 * patch mappings
301 MIDIMAP_NotifyClient(mom, MOM_OPEN, 0L, 0L);
302 return MMSYSERR_NOERROR;
304 HeapFree(GetProcessHeap(), 0, mom);
305 return MIDIERR_INVALIDSETUP;
308 static DWORD modClose(MIDIMAPDATA* mom)
310 UINT i;
311 DWORD ret = MMSYSERR_NOERROR;
313 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
315 for (i = 0; i < 16; i++)
317 DWORD t;
318 if (mom->ChannelMap[i] && mom->ChannelMap[i]->loaded > 0)
320 t = midiOutClose(mom->ChannelMap[i]->hMidi);
321 if (t == MMSYSERR_NOERROR)
323 mom->ChannelMap[i]->loaded = 0;
324 mom->ChannelMap[i]->hMidi = 0;
326 else if (ret == MMSYSERR_NOERROR)
327 ret = t;
330 if (ret == MMSYSERR_NOERROR) {
331 MIDIMAP_NotifyClient(mom, MOM_CLOSE, 0L, 0L);
332 HeapFree(GetProcessHeap(), 0, mom);
334 return ret;
337 static DWORD modLongData(MIDIMAPDATA* mom, LPMIDIHDR lpMidiHdr, DWORD_PTR dwParam2)
339 WORD chn;
340 DWORD ret = MMSYSERR_NOERROR;
341 MIDIHDR mh;
343 if (MIDIMAP_IsBadData(mom))
344 return MMSYSERR_ERROR;
345 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
346 return MIDIERR_UNPREPARED;
347 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
348 return MIDIERR_STILLPLAYING;
350 mh = *lpMidiHdr;
351 lpMidiHdr->dwFlags &= ~MHDR_DONE;
352 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
353 for (chn = 0; chn < 16; chn++)
355 if (mom->ChannelMap[chn] && mom->ChannelMap[chn]->loaded > 0)
357 mh.dwFlags = 0;
358 midiOutPrepareHeader(mom->ChannelMap[chn]->hMidi, &mh, sizeof(mh));
359 ret = midiOutLongMsg(mom->ChannelMap[chn]->hMidi, &mh, sizeof(mh));
360 /* As of 2009, wineXYZ.drv's LongData handlers are synchronous */
361 if (!ret && !(mh.dwFlags & MHDR_DONE))
362 FIXME("wait until MHDR_DONE\n");
363 midiOutUnprepareHeader(mom->ChannelMap[chn]->hMidi, &mh, sizeof(mh));
364 if (ret != MMSYSERR_NOERROR) break;
367 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
368 lpMidiHdr->dwFlags |= MHDR_DONE;
369 MIDIMAP_NotifyClient(mom, MOM_DONE, (DWORD_PTR)lpMidiHdr, 0L);
370 return ret;
373 static DWORD modData(MIDIMAPDATA* mom, DWORD_PTR dwParam)
375 BYTE lb = LOBYTE(LOWORD(dwParam));
376 WORD chn = lb & 0x0F;
377 DWORD ret = MMSYSERR_NOERROR;
379 if (MIDIMAP_IsBadData(mom))
380 return MMSYSERR_ERROR;
382 if (!mom->ChannelMap[chn]) return MMSYSERR_NOERROR;
384 switch (lb & 0xF0)
386 case 0x80:
387 case 0x90:
388 case 0xA0:
389 case 0xB0:
390 case 0xC0:
391 case 0xD0:
392 case 0xE0:
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 < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0 || lpMidiHdr->lpData == 0)
429 return MMSYSERR_INVALPARAM;
430 if (lpMidiHdr->dwFlags & MHDR_PREPARED)
431 return MMSYSERR_NOERROR;
433 lpMidiHdr->dwFlags |= MHDR_PREPARED;
434 lpMidiHdr->dwFlags &= ~(MHDR_DONE|MHDR_INQUEUE); /* flags cleared since w2k */
435 return MMSYSERR_NOERROR;
438 static DWORD modUnprepare(MIDIMAPDATA* mom, LPMIDIHDR lpMidiHdr, DWORD_PTR dwSize)
440 if (MIDIMAP_IsBadData(mom)) return MMSYSERR_ERROR;
441 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0 || lpMidiHdr->lpData == 0)
442 return MMSYSERR_INVALPARAM;
443 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
444 return MMSYSERR_NOERROR;
445 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
446 return MIDIERR_STILLPLAYING;
448 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
449 return MMSYSERR_NOERROR;
452 static DWORD modGetVolume(MIDIMAPDATA* mom, DWORD* lpdwVolume)
454 if (!lpdwVolume) return MMSYSERR_INVALPARAM;
455 *lpdwVolume = 0xFFFFFFFF; /* tests show this initial value */
456 return MMSYSERR_NOERROR;
459 static DWORD modSetVolume(MIDIMAPDATA* mom, DWORD dwVolume)
461 /* Native forwards it to some underlying device
462 * GetVolume returns what was last set here. */
463 FIXME("stub\n");
464 return MMSYSERR_NOERROR;
467 static DWORD modGetDevCaps(UINT wDevID, MIDIMAPDATA* mom, LPMIDIOUTCAPSW lpMidiCaps, DWORD_PTR size)
469 static const MIDIOUTCAPSW mappercaps = {
470 0x00FF, 0x0001, 0x0100, /* Manufacturer and Product ID */
471 L"Wine midi mapper", MOD_MAPPER, 0, 0, 0xFFFF,
472 /* Native returns volume caps of underlying device + MIDICAPS_STREAM */
473 MIDICAPS_VOLUME|MIDICAPS_LRVOLUME
475 if (lpMidiCaps == NULL) return MMSYSERR_INVALPARAM;
476 if (!numMidiOutPorts) return MMSYSERR_BADDEVICEID;
478 memcpy(lpMidiCaps, &mappercaps, min(size, sizeof(*lpMidiCaps)));
479 return MMSYSERR_NOERROR;
482 static DWORD modReset(MIDIMAPDATA* mom)
484 WORD chn;
485 DWORD ret = MMSYSERR_NOERROR;
487 if (MIDIMAP_IsBadData(mom))
488 return MMSYSERR_ERROR;
490 for (chn = 0; chn < 16; chn++)
492 if (mom->ChannelMap[chn] && mom->ChannelMap[chn]->loaded > 0)
494 ret = midiOutReset(mom->ChannelMap[chn]->hMidi);
495 if (ret != MMSYSERR_NOERROR) break;
498 return ret;
501 static LRESULT MIDIMAP_drvOpen(void);
502 static LRESULT MIDIMAP_drvClose(void);
504 /**************************************************************************
505 * modMessage (MIDIMAP.@)
507 DWORD WINAPI MIDIMAP_modMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
508 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
510 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
511 wDevID, wMsg, dwUser, dwParam1, dwParam2);
513 switch (wMsg)
515 case DRVM_INIT:
516 return MIDIMAP_drvOpen();
517 case DRVM_EXIT:
518 return MIDIMAP_drvClose();
519 case DRVM_ENABLE:
520 case DRVM_DISABLE:
521 /* FIXME: Pretend this is supported */
522 return 0;
524 case MODM_OPEN: return modOpen((DWORD_PTR *)dwUser, (LPMIDIOPENDESC)dwParam1, dwParam2);
525 case MODM_CLOSE: return modClose ((MIDIMAPDATA*)dwUser);
527 case MODM_DATA: return modData ((MIDIMAPDATA*)dwUser, dwParam1);
528 case MODM_LONGDATA: return modLongData ((MIDIMAPDATA*)dwUser, (LPMIDIHDR)dwParam1, dwParam2);
529 case MODM_PREPARE: return modPrepare ((MIDIMAPDATA*)dwUser, (LPMIDIHDR)dwParam1, dwParam2);
530 case MODM_UNPREPARE: return modUnprepare ((MIDIMAPDATA*)dwUser, (LPMIDIHDR)dwParam1, dwParam2);
531 case MODM_RESET: return modReset ((MIDIMAPDATA*)dwUser);
533 case MODM_GETDEVCAPS: return modGetDevCaps (wDevID, (MIDIMAPDATA*)dwUser, (LPMIDIOUTCAPSW)dwParam1,dwParam2);
534 case MODM_GETNUMDEVS: return 1;
535 case MODM_GETVOLUME: return modGetVolume ((MIDIMAPDATA*)dwUser, (DWORD*)dwParam1);
536 case MODM_SETVOLUME: return modSetVolume ((MIDIMAPDATA*)dwUser, dwParam1);
537 default:
538 FIXME("unknown message %d!\n", wMsg);
540 return MMSYSERR_NOTSUPPORTED;
543 /*======================================================================*
544 * Driver part *
545 *======================================================================*/
547 /**************************************************************************
548 * MIDIMAP_drvOpen [internal]
550 static LRESULT MIDIMAP_drvOpen(void)
552 MIDIOUTCAPSW moc;
553 unsigned dev, i;
554 BOOL found_valid_port = FALSE;
556 if (midiOutPorts)
557 return 0;
559 numMidiOutPorts = midiOutGetNumDevs();
560 midiOutPorts = HeapAlloc(GetProcessHeap(), 0,
561 numMidiOutPorts * sizeof(MIDIOUTPORT));
562 for (dev = 0; dev < numMidiOutPorts; dev++)
564 if (midiOutGetDevCapsW(dev, &moc, sizeof(moc)) == 0L)
566 lstrcpyW(midiOutPorts[dev].name, moc.szPname);
567 midiOutPorts[dev].loaded = 0;
568 midiOutPorts[dev].hMidi = 0;
569 midiOutPorts[dev].uDevID = dev;
570 midiOutPorts[dev].lpbPatch = NULL;
571 for (i = 0; i < 16; i++)
572 midiOutPorts[dev].aChn[i] = i;
573 if (wcsncmp(midiOutPorts[dev].name, L"Midi Through", lstrlenW(L"Midi Through")) != 0)
574 found_valid_port = TRUE;
576 else
578 midiOutPorts[dev].loaded = -1;
582 if (!found_valid_port)
583 ERR_(winediag)("No software synthesizer midi port found, Midi sound output probably won't work.\n");
585 return 1;
588 /**************************************************************************
589 * MIDIMAP_drvClose [internal]
591 static LRESULT MIDIMAP_drvClose(void)
593 if (midiOutPorts)
595 HeapFree(GetProcessHeap(), 0, midiOutPorts);
596 midiOutPorts = NULL;
597 return 1;
599 return 0;
602 /**************************************************************************
603 * DriverProc (MIDIMAP.@)
605 LRESULT CALLBACK MIDIMAP_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
606 LPARAM dwParam1, LPARAM dwParam2)
608 /* EPP TRACE("(%08lX, %04X, %08lX, %08lX, %08lX)\n", */
609 /* EPP dwDevID, hDriv, wMsg, dwParam1, dwParam2); */
611 switch (wMsg)
613 case DRV_LOAD: return 1;
614 case DRV_FREE: return 1;
615 case DRV_OPEN: return 1;
616 case DRV_CLOSE: return 1;
617 case DRV_ENABLE: return 1;
618 case DRV_DISABLE: return 1;
619 case DRV_QUERYCONFIGURE: return 1;
620 case DRV_CONFIGURE: MessageBoxA(0, "MIDIMAP MultiMedia Driver !", "OSS Driver", MB_OK); return 1;
621 case DRV_INSTALL: return DRVCNF_RESTART;
622 case DRV_REMOVE: return DRVCNF_RESTART;
623 default:
624 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);