winmm: Fix wrong return type for midiInOpen and midiOutOpen.
[wine.git] / dlls / winmm / driver.c
blobfeecc921220e7df7dae1e9aaee760b39497b0611
1 /*
2 * WINE Drivers functions
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1998 Marcus Meissner
6 * Copyright 1999 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <string.h>
27 #include <stdarg.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "winnls.h"
33 #include "winreg.h"
34 #include "mmddk.h"
35 #include "winemm.h"
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
38 #include "excpt.h"
39 #include "wine/exception.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(driver);
43 static CRITICAL_SECTION mmdriver_lock;
44 static CRITICAL_SECTION_DEBUG mmdriver_lock_debug =
46 0, 0, &mmdriver_lock,
47 { &mmdriver_lock_debug.ProcessLocksList, &mmdriver_lock_debug.ProcessLocksList },
48 0, 0, { (DWORD_PTR)(__FILE__ ": mmdriver_lock") }
50 static CRITICAL_SECTION mmdriver_lock = { &mmdriver_lock_debug, -1, 0, 0, 0, 0 };
52 static LPWINE_DRIVER lpDrvItemList /* = NULL */;
53 static const WCHAR HKLM_BASE[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
54 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',0};
56 static void DRIVER_Dump(const char *comment)
58 #if 0
59 LPWINE_DRIVER lpDrv;
61 TRACE("%s\n", comment);
63 EnterCriticalSection( &mmdriver_lock );
65 for (lpDrv = lpDrvItemList; lpDrv != NULL; lpDrv = lpDrv->lpNextItem)
67 TRACE("%p, magic %04lx, id %p, next %p\n", lpDrv, lpDrv->dwMagic, lpDrv->d.d32.dwDriverID, lpDrv->lpNextItem);
70 LeaveCriticalSection( &mmdriver_lock );
71 #endif
74 /**************************************************************************
75 * DRIVER_GetNumberOfModuleRefs [internal]
77 * Returns the number of open drivers which share the same module.
79 static unsigned DRIVER_GetNumberOfModuleRefs(HMODULE hModule, WINE_DRIVER** found)
81 LPWINE_DRIVER lpDrv;
82 unsigned count = 0;
84 EnterCriticalSection( &mmdriver_lock );
86 if (found) *found = NULL;
87 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem)
89 if (lpDrv->hModule == hModule)
91 if (found && !*found) *found = lpDrv;
92 count++;
96 LeaveCriticalSection( &mmdriver_lock );
97 return count;
100 /**************************************************************************
101 * DRIVER_FindFromHDrvr [internal]
103 * From a hDrvr being 32 bits, returns the WINE internal structure.
105 LPWINE_DRIVER DRIVER_FindFromHDrvr(HDRVR hDrvr)
107 LPWINE_DRIVER d;
109 __TRY
111 d = (LPWINE_DRIVER)hDrvr;
112 if (d && d->dwMagic != WINE_DI_MAGIC) d = NULL;
114 __EXCEPT_PAGE_FAULT
116 return NULL;
118 __ENDTRY;
120 if (d) TRACE("%p -> %p, %p\n", hDrvr, d->lpDrvProc, (void *)d->dwDriverID);
121 else TRACE("%p -> NULL\n", hDrvr);
123 return d;
126 /**************************************************************************
127 * DRIVER_SendMessage [internal]
129 static inline LRESULT DRIVER_SendMessage(LPWINE_DRIVER lpDrv, UINT msg,
130 LPARAM lParam1, LPARAM lParam2)
132 LRESULT ret;
134 TRACE("Before call32 proc=%p drvrID=%08lx hDrv=%p wMsg=%04x p1=%08lx p2=%08lx\n",
135 lpDrv->lpDrvProc, lpDrv->dwDriverID, lpDrv, msg, lParam1, lParam2);
136 ret = lpDrv->lpDrvProc(lpDrv->dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
137 TRACE("After call32 proc=%p drvrID=%08lx hDrv=%p wMsg=%04x p1=%08lx p2=%08lx => %08lx\n",
138 lpDrv->lpDrvProc, lpDrv->dwDriverID, lpDrv, msg, lParam1, lParam2, ret);
140 return ret;
143 /**************************************************************************
144 * SendDriverMessage [WINMM.@]
145 * DrvSendMessage [WINMM.@]
147 LRESULT WINAPI SendDriverMessage(HDRVR hDriver, UINT msg, LPARAM lParam1,
148 LPARAM lParam2)
150 LPWINE_DRIVER lpDrv;
151 LRESULT retval = 0;
153 TRACE("(%p, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
155 if ((lpDrv = DRIVER_FindFromHDrvr(hDriver)) != NULL) {
156 retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
157 } else {
158 WARN("Bad driver handle %p\n", hDriver);
160 TRACE("retval = %ld\n", retval);
162 return retval;
165 /**************************************************************************
166 * DRIVER_RemoveFromList [internal]
168 * Generates all the logic to handle driver closure / deletion
169 * Removes a driver struct to the list of open drivers.
171 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
173 /* last of this driver in list ? */
174 if (DRIVER_GetNumberOfModuleRefs(lpDrv->hModule, NULL) == 1) {
175 DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
176 DRIVER_SendMessage(lpDrv, DRV_FREE, 0L, 0L);
179 EnterCriticalSection( &mmdriver_lock );
181 if (lpDrv->lpPrevItem)
182 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
183 else
184 lpDrvItemList = lpDrv->lpNextItem;
185 if (lpDrv->lpNextItem)
186 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
187 /* trash magic number */
188 lpDrv->dwMagic ^= 0xa5a5a5a5;
189 lpDrv->lpDrvProc = NULL;
190 lpDrv->dwDriverID = 0;
192 LeaveCriticalSection( &mmdriver_lock );
194 return TRUE;
197 /**************************************************************************
198 * DRIVER_AddToList [internal]
200 * Adds a driver struct to the list of open drivers.
201 * Generates all the logic to handle driver creation / open.
203 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
205 lpNewDrv->dwMagic = WINE_DI_MAGIC;
206 /* First driver to be loaded for this module, need to load correctly the module */
207 /* first of this driver in list ? */
208 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv->hModule, NULL) == 0) {
209 if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
210 TRACE("DRV_LOAD failed on driver %p\n", lpNewDrv);
211 return FALSE;
213 /* returned value is not checked */
214 DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
217 /* Now just open a new instance of a driver on this module */
218 lpNewDrv->dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
220 if (lpNewDrv->dwDriverID == 0)
222 TRACE("DRV_OPEN failed on driver %p\n", lpNewDrv);
223 return FALSE;
226 EnterCriticalSection( &mmdriver_lock );
228 lpNewDrv->lpNextItem = NULL;
229 if (lpDrvItemList == NULL) {
230 lpDrvItemList = lpNewDrv;
231 lpNewDrv->lpPrevItem = NULL;
232 } else {
233 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
234 while (lpDrv->lpNextItem != NULL)
235 lpDrv = lpDrv->lpNextItem;
237 lpDrv->lpNextItem = lpNewDrv;
238 lpNewDrv->lpPrevItem = lpDrv;
241 LeaveCriticalSection( &mmdriver_lock );
242 return TRUE;
245 /**************************************************************************
246 * DRIVER_GetLibName [internal]
249 BOOL DRIVER_GetLibName(LPCWSTR keyName, LPCWSTR sectName, LPWSTR buf, int sz)
251 HKEY hKey, hSecKey;
252 DWORD bufLen, lRet;
253 static const WCHAR wszSystemIni[] = {'S','Y','S','T','E','M','.','I','N','I',0};
254 WCHAR wsznull = '\0';
256 TRACE("registry: %s, %s, %p, %d\n", debugstr_w(keyName), debugstr_w(sectName), buf, sz);
258 lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE, HKLM_BASE, 0, KEY_QUERY_VALUE, &hKey);
259 if (lRet == ERROR_SUCCESS) {
260 lRet = RegOpenKeyExW(hKey, sectName, 0, KEY_QUERY_VALUE, &hSecKey);
261 if (lRet == ERROR_SUCCESS) {
262 bufLen = sz;
263 lRet = RegQueryValueExW(hSecKey, keyName, 0, 0, (void*)buf, &bufLen);
264 RegCloseKey( hSecKey );
266 RegCloseKey( hKey );
268 if (lRet == ERROR_SUCCESS) return TRUE;
270 /* default to system.ini if we can't find it in the registry,
271 * to support native installations where system.ini is still used */
272 TRACE("system.ini: %s, %s, %p, %d\n", debugstr_w(keyName), debugstr_w(sectName), buf, sz);
273 return GetPrivateProfileStringW(sectName, keyName, &wsznull, buf, sz / sizeof(WCHAR), wszSystemIni);
276 /**************************************************************************
277 * DRIVER_TryOpenDriver32 [internal]
279 * Tries to load a 32 bit driver whose DLL's (module) name is fn
281 LPWINE_DRIVER DRIVER_TryOpenDriver32(LPCWSTR fn, LPARAM lParam2)
283 LPWINE_DRIVER lpDrv = NULL;
284 HMODULE hModule = 0;
285 LPWSTR ptr;
286 LPCSTR cause = 0;
288 TRACE("(%s, %08lX);\n", debugstr_w(fn), lParam2);
290 if ((ptr = strchrW(fn, ' ')) != NULL) {
291 *ptr++ = '\0';
292 while (*ptr == ' ') ptr++;
293 if (*ptr == '\0') ptr = NULL;
296 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
297 if (lpDrv == NULL) {cause = "OOM"; goto exit;}
299 if ((hModule = LoadLibraryW(fn)) == 0) {cause = "Not a 32 bit lib"; goto exit;}
301 lpDrv->lpDrvProc = (DRIVERPROC)GetProcAddress(hModule, "DriverProc");
302 if (lpDrv->lpDrvProc == NULL) {cause = "no DriverProc"; goto exit;}
304 lpDrv->dwFlags = 0;
305 lpDrv->hModule = hModule;
306 lpDrv->dwDriverID = 0;
308 /* Win32 installable drivers must support a two phase opening scheme:
309 * + first open with NULL as lParam2 (session instance),
310 * + then do a second open with the real non null lParam2)
312 if (DRIVER_GetNumberOfModuleRefs(lpDrv->hModule, NULL) == 0 && lParam2)
314 LPWINE_DRIVER ret;
316 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, 0L))
318 cause = "load0 failed";
319 goto exit;
321 ret = DRIVER_TryOpenDriver32(fn, lParam2);
322 if (!ret)
324 CloseDriver((HDRVR)lpDrv, 0L, 0L);
325 cause = "load1 failed";
326 goto exit;
328 lpDrv->dwFlags |= WINE_GDF_SESSION;
329 return ret;
332 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2))
333 {cause = "load failed"; goto exit;}
335 TRACE("=> %p\n", lpDrv);
336 return lpDrv;
337 exit:
338 FreeLibrary(hModule);
339 HeapFree(GetProcessHeap(), 0, lpDrv);
340 TRACE("Unable to load 32 bit module %s: %s\n", debugstr_w(fn), cause);
341 return NULL;
344 /**************************************************************************
345 * OpenDriverA [WINMM.@]
346 * DrvOpenA [WINMM.@]
347 * (0,1,DRV_LOAD ,0 ,0)
348 * (0,1,DRV_ENABLE,0 ,0)
349 * (0,1,DRV_OPEN ,buf[256],0)
351 HDRVR WINAPI OpenDriverA(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam)
353 INT len;
354 LPWSTR dn = NULL;
355 LPWSTR sn = NULL;
356 HDRVR ret = 0;
358 if (lpDriverName)
360 len = MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, NULL, 0 );
361 dn = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
362 if (!dn) goto done;
363 MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, dn, len );
366 if (lpSectionName)
368 len = MultiByteToWideChar( CP_ACP, 0, lpSectionName, -1, NULL, 0 );
369 sn = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
370 if (!sn) goto done;
371 MultiByteToWideChar( CP_ACP, 0, lpSectionName, -1, sn, len );
374 ret = OpenDriver(dn, sn, lParam);
376 done:
377 HeapFree(GetProcessHeap(), 0, dn);
378 HeapFree(GetProcessHeap(), 0, sn);
379 return ret;
382 /**************************************************************************
383 * OpenDriver [WINMM.@]
384 * DrvOpen [WINMM.@]
386 HDRVR WINAPI OpenDriver(LPCWSTR lpDriverName, LPCWSTR lpSectionName, LPARAM lParam)
388 LPWINE_DRIVER lpDrv = NULL;
389 WCHAR libName[MAX_PATH + 1];
390 LPCWSTR lsn = lpSectionName;
392 TRACE("(%s, %s, 0x%08lx);\n",
393 debugstr_w(lpDriverName), debugstr_w(lpSectionName), lParam);
395 DRIVER_Dump("BEFORE:");
397 if (lsn == NULL) {
398 static const WCHAR wszDrivers32[] = {'D','r','i','v','e','r','s','3','2',0};
399 lstrcpynW(libName, lpDriverName, sizeof(libName) / sizeof(WCHAR));
401 if ((lpDrv = DRIVER_TryOpenDriver32(libName, lParam)))
402 goto the_end;
403 lsn = wszDrivers32;
405 if (DRIVER_GetLibName(lpDriverName, lsn, libName, sizeof(libName)) &&
406 (lpDrv = DRIVER_TryOpenDriver32(libName, lParam)))
407 goto the_end;
409 TRACE("Failed to open driver %s from system.ini file, section %s\n",
410 debugstr_w(lpDriverName), debugstr_w(lpSectionName));
412 the_end:
413 TRACE("=> %p\n", lpDrv);
415 DRIVER_Dump("AFTER:");
417 return (HDRVR)lpDrv;
420 /**************************************************************************
421 * CloseDriver [WINMM.@]
422 * DrvClose [WINMM.@]
424 LRESULT WINAPI CloseDriver(HDRVR hDrvr, LPARAM lParam1, LPARAM lParam2)
426 BOOL ret;
427 LPWINE_DRIVER lpDrv;
429 TRACE("(%p, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
431 DRIVER_Dump("BEFORE:");
433 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL)
435 LPWINE_DRIVER lpDrv0;
437 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
439 DRIVER_RemoveFromList(lpDrv);
441 if (lpDrv->dwFlags & WINE_GDF_SESSION)
442 FIXME("WINE_GDF_SESSION: Shouldn't happen (%p)\n", lpDrv);
443 /* if driver has an opened session instance, we have to close it too */
444 if (DRIVER_GetNumberOfModuleRefs(lpDrv->hModule, &lpDrv0) == 1 &&
445 (lpDrv0->dwFlags & WINE_GDF_SESSION))
447 DRIVER_SendMessage(lpDrv0, DRV_CLOSE, 0, 0);
448 DRIVER_RemoveFromList(lpDrv0);
449 FreeLibrary(lpDrv0->hModule);
450 HeapFree(GetProcessHeap(), 0, lpDrv0);
452 FreeLibrary(lpDrv->hModule);
454 HeapFree(GetProcessHeap(), 0, lpDrv);
455 ret = TRUE;
457 else
459 WARN("Failed to close driver\n");
460 ret = FALSE;
463 DRIVER_Dump("AFTER:");
465 return ret;
468 /**************************************************************************
469 * GetDriverFlags [WINMM.@]
470 * [in] hDrvr handle to the driver
472 * Returns:
473 * 0x00000000 if hDrvr is an invalid handle
474 * 0x80000000 if hDrvr is a valid 32 bit driver
475 * 0x90000000 if hDrvr is a valid 16 bit driver
477 * native WINMM doesn't return those flags
478 * 0x80000000 for a valid 32 bit driver and that's it
479 * (I may have mixed up the two flags :-(
481 DWORD WINAPI GetDriverFlags(HDRVR hDrvr)
483 LPWINE_DRIVER lpDrv;
484 DWORD ret = 0;
486 TRACE("(%p)\n", hDrvr);
488 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
489 ret = WINE_GDF_EXIST | (lpDrv->dwFlags & WINE_GDF_EXTERNAL_MASK);
491 return ret;
494 /**************************************************************************
495 * GetDriverModuleHandle [WINMM.@]
496 * DrvGetModuleHandle [WINMM.@]
498 HMODULE WINAPI GetDriverModuleHandle(HDRVR hDrvr)
500 LPWINE_DRIVER lpDrv;
501 HMODULE hModule = 0;
503 TRACE("(%p);\n", hDrvr);
505 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
506 hModule = lpDrv->hModule;
508 TRACE("=> %p\n", hModule);
509 return hModule;
512 /**************************************************************************
513 * DefDriverProc [WINMM.@]
514 * DrvDefDriverProc [WINMM.@]
516 LRESULT WINAPI DefDriverProc(DWORD_PTR dwDriverIdentifier, HDRVR hDrv,
517 UINT Msg, LPARAM lParam1, LPARAM lParam2)
519 switch (Msg) {
520 case DRV_LOAD:
521 case DRV_FREE:
522 case DRV_ENABLE:
523 case DRV_DISABLE:
524 return 1;
525 case DRV_INSTALL:
526 case DRV_REMOVE:
527 return DRV_SUCCESS;
528 default:
529 return 0;
533 /**************************************************************************
534 * DRIVER_getCallback [internal]
536 static const char* DRIVER_getCallback(DWORD uFlags)
538 switch(uFlags & DCB_TYPEMASK) {
539 case DCB_NULL: return "null";
540 case DCB_WINDOW: return "window";
541 case DCB_TASK: return "task";
542 case DCB_EVENT: return "event";
543 case DCB_FUNCTION: return "32bit function";
544 default: return "UNKNOWN";
548 /**************************************************************************
549 * DriverCallback [WINMM.@]
551 BOOL WINAPI DriverCallback(DWORD_PTR dwCallBack, DWORD uFlags, HDRVR hDev,
552 DWORD wMsg, DWORD_PTR dwUser, DWORD_PTR dwParam1,
553 DWORD_PTR dwParam2)
555 BOOL ret = FALSE;
556 TRACE("(%08lX, %s %04X, %p, %04X, %08lX, %08lX, %08lX)\n",
557 dwCallBack, DRIVER_getCallback(uFlags), uFlags, hDev, wMsg, dwUser, dwParam1, dwParam2);
558 if (!dwCallBack)
559 return ret;
561 switch (uFlags & DCB_TYPEMASK) {
562 case DCB_NULL:
563 /* Native returns FALSE = no notification, not TRUE */
564 return ret;
565 case DCB_WINDOW:
566 ret = PostMessageA((HWND)dwCallBack, wMsg, (WPARAM)hDev, dwParam1);
567 break;
568 case DCB_TASK: /* aka DCB_THREAD */
569 ret = PostThreadMessageA(dwCallBack, wMsg, (WPARAM)hDev, dwParam1);
570 break;
571 case DCB_FUNCTION:
572 ((LPDRVCALLBACK)dwCallBack)(hDev, wMsg, dwUser, dwParam1, dwParam2);
573 ret = TRUE;
574 break;
575 case DCB_EVENT:
576 ret = SetEvent((HANDLE)dwCallBack);
577 break;
578 #if 0
579 /* FIXME: for now only usable in mmsystem.dll16
580 * If needed, should be enabled back
582 case 6: /* I would dub it DCB_MMTHREADSIGNAL */
583 /* this is an undocumented DCB_ value used for mmThreads
584 * loword of dwCallBack contains the handle of the lpMMThd block
585 * which dwSignalCount has to be incremented
587 if (pFnGetMMThread16)
589 WINE_MMTHREAD* lpMMThd = pFnGetMMThread16(LOWORD(dwCallBack));
591 TRACE("mmThread (%04x, %p) !\n", LOWORD(dwCallBack), lpMMThd);
592 /* same as mmThreadSignal16 */
593 InterlockedIncrement(&lpMMThd->dwSignalCount);
594 SetEvent(lpMMThd->hEvent);
595 /* some other stuff on lpMMThd->hVxD */
597 break;
598 #endif
599 #if 0
600 case 4:
601 /* this is an undocumented DCB_ value for... I don't know */
602 break;
603 #endif
604 default:
605 WARN("Unknown callback type %d\n", uFlags & DCB_TYPEMASK);
606 return FALSE;
608 if (ret)
609 TRACE("Done\n");
610 else
611 WARN("Notification failure\n");
612 return ret;
615 /******************************************************************
616 * DRIVER_UnloadAll
620 void DRIVER_UnloadAll(void)
622 LPWINE_DRIVER lpDrv;
623 LPWINE_DRIVER lpNextDrv = NULL;
624 unsigned count = 0;
626 restart:
627 EnterCriticalSection( &mmdriver_lock );
629 for (lpDrv = lpDrvItemList; lpDrv != NULL; lpDrv = lpNextDrv)
631 lpNextDrv = lpDrv->lpNextItem;
633 /* session instances will be unloaded automatically */
634 if (!(lpDrv->dwFlags & WINE_GDF_SESSION))
636 LeaveCriticalSection( &mmdriver_lock );
637 CloseDriver((HDRVR)lpDrv, 0, 0);
638 count++;
639 /* restart from the beginning of the list */
640 goto restart;
644 LeaveCriticalSection( &mmdriver_lock );
646 TRACE("Unloaded %u drivers\n", count);