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
24 #include "wine/port.h"
36 #include "wine/debug.h"
37 #include "wine/unicode.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
=
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 WINE_MMTHREAD
* (*pFnGetMMThread16
)(UINT16 h
) /* = NULL */;
57 LPWINE_DRIVER (*pFnOpenDriver16
)(LPCWSTR
,LPCWSTR
,LPARAM
) /* = NULL */;
58 LRESULT (*pFnCloseDriver16
)(UINT16
,LPARAM
,LPARAM
) /* = NULL */;
59 LRESULT (*pFnSendMessage16
)(UINT16
,UINT
,LPARAM
,LPARAM
) /* = NULL */;
61 static void DRIVER_Dump(const char *comment
)
66 TRACE("%s\n", comment
);
68 EnterCriticalSection( &mmdriver_lock
);
70 for (lpDrv
= lpDrvItemList
; lpDrv
!= NULL
; lpDrv
= lpDrv
->lpNextItem
)
72 TRACE("%p, magic %04lx, id %p, next %p\n", lpDrv
, lpDrv
->dwMagic
, (void *)lpDrv
->d
.d32
.dwDriverID
, lpDrv
->lpNextItem
);
75 LeaveCriticalSection( &mmdriver_lock
);
79 /**************************************************************************
80 * DRIVER_GetNumberOfModuleRefs [internal]
82 * Returns the number of open drivers which share the same module.
84 static unsigned DRIVER_GetNumberOfModuleRefs(HMODULE hModule
, WINE_DRIVER
** found
)
89 EnterCriticalSection( &mmdriver_lock
);
91 if (found
) *found
= NULL
;
92 for (lpDrv
= lpDrvItemList
; lpDrv
; lpDrv
= lpDrv
->lpNextItem
)
94 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
) && lpDrv
->d
.d32
.hModule
== hModule
)
96 if (found
&& !*found
) *found
= lpDrv
;
101 LeaveCriticalSection( &mmdriver_lock
);
105 /**************************************************************************
106 * DRIVER_FindFromHDrvr [internal]
108 * From a hDrvr being 32 bits, returns the WINE internal structure.
110 LPWINE_DRIVER
DRIVER_FindFromHDrvr(HDRVR hDrvr
)
116 d
= (LPWINE_DRIVER
)hDrvr
;
117 if (d
&& d
->dwMagic
!= WINE_DI_MAGIC
) d
= NULL
;
125 if (d
) TRACE("%p -> %p, %p\n", hDrvr
, d
->d
.d32
.lpDrvProc
, (void *)d
->d
.d32
.dwDriverID
);
126 else TRACE("%p -> NULL\n", hDrvr
);
131 /**************************************************************************
132 * DRIVER_SendMessage [internal]
134 inline static LRESULT
DRIVER_SendMessage(LPWINE_DRIVER lpDrv
, UINT msg
,
135 LPARAM lParam1
, LPARAM lParam2
)
139 if (lpDrv
->dwFlags
& WINE_GDF_16BIT
) {
140 /* no need to check mmsystem presence: the driver must have been opened as a 16 bit one,
142 if (pFnSendMessage16
)
143 ret
= pFnSendMessage16(lpDrv
->d
.d16
.hDriver16
, msg
, lParam1
, lParam2
);
145 TRACE("Before call32 proc=%p drvrID=%08lx hDrv=%p wMsg=%04x p1=%08lx p2=%08lx\n",
146 lpDrv
->d
.d32
.lpDrvProc
, lpDrv
->d
.d32
.dwDriverID
, (HDRVR
)lpDrv
, msg
, lParam1
, lParam2
);
147 ret
= lpDrv
->d
.d32
.lpDrvProc(lpDrv
->d
.d32
.dwDriverID
, (HDRVR
)lpDrv
, msg
, lParam1
, lParam2
);
148 TRACE("After call32 proc=%p drvrID=%08lx hDrv=%p wMsg=%04x p1=%08lx p2=%08lx => %08lx\n",
149 lpDrv
->d
.d32
.lpDrvProc
, lpDrv
->d
.d32
.dwDriverID
, (HDRVR
)lpDrv
, msg
, lParam1
, lParam2
, ret
);
154 /**************************************************************************
155 * SendDriverMessage [WINMM.@]
156 * DrvSendMessage [WINMM.@]
158 LRESULT WINAPI
SendDriverMessage(HDRVR hDriver
, UINT msg
, LPARAM lParam1
,
164 TRACE("(%p, %04X, %08lX, %08lX)\n", hDriver
, msg
, lParam1
, lParam2
);
166 if ((lpDrv
= DRIVER_FindFromHDrvr(hDriver
)) != NULL
) {
167 retval
= DRIVER_SendMessage(lpDrv
, msg
, lParam1
, lParam2
);
169 WARN("Bad driver handle %p\n", hDriver
);
171 TRACE("retval = %ld\n", retval
);
176 /**************************************************************************
177 * DRIVER_RemoveFromList [internal]
179 * Generates all the logic to handle driver closure / deletion
180 * Removes a driver struct to the list of open drivers.
182 static BOOL
DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv
)
184 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
)) {
185 /* last of this driver in list ? */
186 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->d
.d32
.hModule
, NULL
) == 1) {
187 DRIVER_SendMessage(lpDrv
, DRV_DISABLE
, 0L, 0L);
188 DRIVER_SendMessage(lpDrv
, DRV_FREE
, 0L, 0L);
192 EnterCriticalSection( &mmdriver_lock
);
194 if (lpDrv
->lpPrevItem
)
195 lpDrv
->lpPrevItem
->lpNextItem
= lpDrv
->lpNextItem
;
197 lpDrvItemList
= lpDrv
->lpNextItem
;
198 if (lpDrv
->lpNextItem
)
199 lpDrv
->lpNextItem
->lpPrevItem
= lpDrv
->lpPrevItem
;
200 /* trash magic number */
201 lpDrv
->dwMagic
^= 0xa5a5a5a5;
202 lpDrv
->d
.d32
.lpDrvProc
= NULL
;
203 lpDrv
->d
.d32
.dwDriverID
= 0;
204 lpDrv
->d
.d16
.hDriver16
= 0;
206 LeaveCriticalSection( &mmdriver_lock
);
211 /**************************************************************************
212 * DRIVER_AddToList [internal]
214 * Adds a driver struct to the list of open drivers.
215 * Generates all the logic to handle driver creation / open.
217 static BOOL
DRIVER_AddToList(LPWINE_DRIVER lpNewDrv
, LPARAM lParam1
, LPARAM lParam2
)
219 lpNewDrv
->dwMagic
= WINE_DI_MAGIC
;
220 /* First driver to be loaded for this module, need to load correctly the module */
221 if (!(lpNewDrv
->dwFlags
& WINE_GDF_16BIT
)) {
222 /* first of this driver in list ? */
223 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv
->d
.d32
.hModule
, NULL
) == 0) {
224 if (DRIVER_SendMessage(lpNewDrv
, DRV_LOAD
, 0L, 0L) != DRV_SUCCESS
) {
225 TRACE("DRV_LOAD failed on driver %p\n", lpNewDrv
);
228 /* returned value is not checked */
229 DRIVER_SendMessage(lpNewDrv
, DRV_ENABLE
, 0L, 0L);
232 /* Now just open a new instance of a driver on this module */
233 lpNewDrv
->d
.d32
.dwDriverID
= DRIVER_SendMessage(lpNewDrv
, DRV_OPEN
, lParam1
, lParam2
);
235 if (lpNewDrv
->d
.d32
.dwDriverID
== 0)
237 TRACE("DRV_OPEN failed on driver %p\n", lpNewDrv
);
242 EnterCriticalSection( &mmdriver_lock
);
244 lpNewDrv
->lpNextItem
= NULL
;
245 if (lpDrvItemList
== NULL
) {
246 lpDrvItemList
= lpNewDrv
;
247 lpNewDrv
->lpPrevItem
= NULL
;
249 LPWINE_DRIVER lpDrv
= lpDrvItemList
; /* find end of list */
250 while (lpDrv
->lpNextItem
!= NULL
)
251 lpDrv
= lpDrv
->lpNextItem
;
253 lpDrv
->lpNextItem
= lpNewDrv
;
254 lpNewDrv
->lpPrevItem
= lpDrv
;
257 LeaveCriticalSection( &mmdriver_lock
);
261 /**************************************************************************
262 * DRIVER_GetLibName [internal]
265 BOOL
DRIVER_GetLibName(LPCWSTR keyName
, LPCWSTR sectName
, LPWSTR buf
, int sz
)
269 static const WCHAR wszSystemIni
[] = {'S','Y','S','T','E','M','.','I','N','I',0};
270 WCHAR wsznull
= '\0';
272 TRACE("registry: %s, %s, %p, %d\n", debugstr_w(keyName
), debugstr_w(sectName
), buf
, sz
);
274 lRet
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, HKLM_BASE
, 0, KEY_QUERY_VALUE
, &hKey
);
275 if (lRet
== ERROR_SUCCESS
) {
276 lRet
= RegOpenKeyExW(hKey
, sectName
, 0, KEY_QUERY_VALUE
, &hSecKey
);
277 if (lRet
== ERROR_SUCCESS
) {
279 lRet
= RegQueryValueExW(hSecKey
, keyName
, 0, 0, (void*)buf
, &bufLen
);
280 RegCloseKey( hSecKey
);
284 if (lRet
== ERROR_SUCCESS
) return TRUE
;
286 /* default to system.ini if we can't find it in the registry,
287 * to support native installations where system.ini is still used */
288 TRACE("system.ini: %s, %s, %p, %d\n", debugstr_w(keyName
), debugstr_w(sectName
), buf
, sz
);
289 return GetPrivateProfileStringW(sectName
, keyName
, &wsznull
, buf
, sz
/ sizeof(WCHAR
), wszSystemIni
);
292 /**************************************************************************
293 * DRIVER_TryOpenDriver32 [internal]
295 * Tries to load a 32 bit driver whose DLL's (module) name is fn
297 LPWINE_DRIVER
DRIVER_TryOpenDriver32(LPCWSTR fn
, LPARAM lParam2
)
299 LPWINE_DRIVER lpDrv
= NULL
;
304 TRACE("(%s, %08lX);\n", debugstr_w(fn
), lParam2
);
306 if ((ptr
= strchrW(fn
, ' ')) != NULL
) {
308 while (*ptr
== ' ') ptr
++;
309 if (*ptr
== '\0') ptr
= NULL
;
312 lpDrv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER
));
313 if (lpDrv
== NULL
) {cause
= "OOM"; goto exit
;}
315 if ((hModule
= LoadLibraryW(fn
)) == 0) {cause
= "Not a 32 bit lib"; goto exit
;}
317 lpDrv
->d
.d32
.lpDrvProc
= (DRIVERPROC
)GetProcAddress(hModule
, "DriverProc");
318 if (lpDrv
->d
.d32
.lpDrvProc
== NULL
) {cause
= "no DriverProc"; goto exit
;}
321 lpDrv
->d
.d32
.hModule
= hModule
;
322 lpDrv
->d
.d32
.dwDriverID
= 0;
324 /* Win32 installable drivers must support a two phase opening scheme:
325 * + first open with NULL as lParam2 (session instance),
326 * + then do a second open with the real non null lParam2)
328 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->d
.d32
.hModule
, NULL
) == 0 && lParam2
)
332 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, 0L))
334 cause
= "load0 failed";
337 ret
= DRIVER_TryOpenDriver32(fn
, lParam2
);
340 CloseDriver((HDRVR
)lpDrv
, 0L, 0L);
341 cause
= "load1 failed";
344 lpDrv
->dwFlags
|= WINE_GDF_SESSION
;
348 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, lParam2
))
349 {cause
= "load failed"; goto exit
;}
351 TRACE("=> %p\n", lpDrv
);
354 FreeLibrary(hModule
);
355 HeapFree(GetProcessHeap(), 0, lpDrv
);
356 TRACE("Unable to load 32 bit module %s: %s\n", debugstr_w(fn
), cause
);
360 /**************************************************************************
361 * OpenDriverA [WINMM.@]
363 * (0,1,DRV_LOAD ,0 ,0)
364 * (0,1,DRV_ENABLE,0 ,0)
365 * (0,1,DRV_OPEN ,buf[256],0)
367 HDRVR WINAPI
OpenDriverA(LPCSTR lpDriverName
, LPCSTR lpSectionName
, LPARAM lParam
)
376 len
= MultiByteToWideChar( CP_ACP
, 0, lpDriverName
, -1, NULL
, 0 );
377 dn
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
379 MultiByteToWideChar( CP_ACP
, 0, lpDriverName
, -1, dn
, len
);
384 len
= MultiByteToWideChar( CP_ACP
, 0, lpSectionName
, -1, NULL
, 0 );
385 sn
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
387 MultiByteToWideChar( CP_ACP
, 0, lpSectionName
, -1, sn
, len
);
390 ret
= OpenDriver(dn
, sn
, lParam
);
393 HeapFree(GetProcessHeap(), 0, dn
);
394 HeapFree(GetProcessHeap(), 0, sn
);
398 /**************************************************************************
399 * OpenDriver [WINMM.@]
402 HDRVR WINAPI
OpenDriver(LPCWSTR lpDriverName
, LPCWSTR lpSectionName
, LPARAM lParam
)
404 LPWINE_DRIVER lpDrv
= NULL
;
405 WCHAR libName
[MAX_PATH
+ 1];
406 LPCWSTR lsn
= lpSectionName
;
408 TRACE("(%s, %s, 0x%08lx);\n",
409 debugstr_w(lpDriverName
), debugstr_w(lpSectionName
), lParam
);
411 DRIVER_Dump("BEFORE:");
414 static const WCHAR wszDrivers32
[] = {'D','r','i','v','e','r','s','3','2',0};
415 lstrcpynW(libName
, lpDriverName
, sizeof(libName
) / sizeof(WCHAR
));
417 if ((lpDrv
= DRIVER_TryOpenDriver32(libName
, lParam
)))
421 if (DRIVER_GetLibName(lpDriverName
, lsn
, libName
, sizeof(libName
)) &&
422 (lpDrv
= DRIVER_TryOpenDriver32(libName
, lParam
)))
425 /* now we will try a 16 bit driver (and add all the glue to make it work... which
426 * is located in our mmsystem implementation)
427 * so ensure, we can load our mmsystem, otherwise just fail
429 WINMM_CheckForMMSystem();
430 if (pFnOpenDriver16
&&
431 (lpDrv
= pFnOpenDriver16(lpDriverName
, lpSectionName
, lParam
)))
433 if (DRIVER_AddToList(lpDrv
, 0, lParam
)) goto the_end
;
434 HeapFree(GetProcessHeap(), 0, lpDrv
);
436 TRACE("Failed to open driver %s from system.ini file, section %s\n",
437 debugstr_w(lpDriverName
), debugstr_w(lpSectionName
));
440 TRACE("=> %p\n", lpDrv
);
442 DRIVER_Dump("AFTER:");
447 /**************************************************************************
448 * CloseDriver [WINMM.@]
451 LRESULT WINAPI
CloseDriver(HDRVR hDrvr
, LPARAM lParam1
, LPARAM lParam2
)
456 TRACE("(%p, %08lX, %08lX);\n", hDrvr
, lParam1
, lParam2
);
458 DRIVER_Dump("BEFORE:");
460 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
)
462 if (lpDrv
->dwFlags
& WINE_GDF_16BIT
)
464 if (pFnCloseDriver16
)
465 pFnCloseDriver16(lpDrv
->d
.d16
.hDriver16
, lParam1
, lParam2
);
468 DRIVER_SendMessage(lpDrv
, DRV_CLOSE
, lParam1
, lParam2
);
470 DRIVER_RemoveFromList(lpDrv
);
472 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
))
474 LPWINE_DRIVER lpDrv0
;
476 if (lpDrv
->dwFlags
& WINE_GDF_SESSION
)
477 FIXME("WINE_GDF_SESSION: Shouldn't happen (%p)\n", lpDrv
);
478 /* if driver has an opened session instance, we have to close it too */
479 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->d
.d32
.hModule
, &lpDrv0
) == 1 &&
480 (lpDrv0
->dwFlags
& WINE_GDF_SESSION
))
482 DRIVER_SendMessage(lpDrv0
, DRV_CLOSE
, 0, 0);
483 DRIVER_RemoveFromList(lpDrv0
);
484 FreeLibrary(lpDrv0
->d
.d32
.hModule
);
485 HeapFree(GetProcessHeap(), 0, lpDrv0
);
487 FreeLibrary(lpDrv
->d
.d32
.hModule
);
489 HeapFree(GetProcessHeap(), 0, lpDrv
);
494 WARN("Failed to close driver\n");
498 DRIVER_Dump("AFTER:");
503 /**************************************************************************
504 * GetDriverFlags [WINMM.@]
505 * [in] hDrvr handle to the driver
508 * 0x00000000 if hDrvr is an invalid handle
509 * 0x80000000 if hDrvr is a valid 32 bit driver
510 * 0x90000000 if hDrvr is a valid 16 bit driver
512 * native WINMM doesn't return those flags
513 * 0x80000000 for a valid 32 bit driver and that's it
514 * (I may have mixed up the two flags :-(
516 DWORD WINAPI
GetDriverFlags(HDRVR hDrvr
)
521 TRACE("(%p)\n", hDrvr
);
523 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
) {
524 ret
= WINE_GDF_EXIST
| (lpDrv
->dwFlags
& WINE_GDF_EXTERNAL_MASK
);
529 /**************************************************************************
530 * GetDriverModuleHandle [WINMM.@]
531 * DrvGetModuleHandle [WINMM.@]
533 HMODULE WINAPI
GetDriverModuleHandle(HDRVR hDrvr
)
538 TRACE("(%p);\n", hDrvr
);
540 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
) {
541 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
))
542 hModule
= lpDrv
->d
.d32
.hModule
;
544 TRACE("=> %p\n", hModule
);
548 /**************************************************************************
549 * DefDriverProc [WINMM.@]
550 * DrvDefDriverProc [WINMM.@]
552 LRESULT WINAPI
DefDriverProc(DWORD_PTR dwDriverIdentifier
, HDRVR hDrv
,
553 UINT Msg
, LPARAM lParam1
, LPARAM lParam2
)
569 /**************************************************************************
570 * DriverCallback [WINMM.@]
572 BOOL WINAPI
DriverCallback(DWORD dwCallBack
, UINT uFlags
, HDRVR hDev
,
573 UINT wMsg
, DWORD dwUser
, DWORD dwParam1
,
576 TRACE("(%08X, %04X, %p, %04X, %08X, %08X, %08X); !\n",
577 dwCallBack
, uFlags
, hDev
, wMsg
, dwUser
, dwParam1
, dwParam2
);
579 switch (uFlags
& DCB_TYPEMASK
) {
583 WARN("uFlags=%04X has null DCB value, but dwCallBack=%08X is not null !\n", uFlags
, dwCallBack
);
586 TRACE("Window(%04X) handle=%p!\n", dwCallBack
, hDev
);
587 PostMessageA((HWND
)dwCallBack
, wMsg
, (WPARAM
)hDev
, dwParam1
);
589 case DCB_TASK
: /* aka DCB_THREAD */
590 TRACE("Task(%04x) !\n", dwCallBack
);
591 PostThreadMessageA(dwCallBack
, wMsg
, (WPARAM
)hDev
, dwParam1
);
594 TRACE("Function (32 bit) !\n");
595 ((LPDRVCALLBACK
)dwCallBack
)(hDev
, wMsg
, dwUser
, dwParam1
, dwParam2
);
598 TRACE("Event(%08x) !\n", dwCallBack
);
599 SetEvent((HANDLE
)dwCallBack
);
601 case 6: /* I would dub it DCB_MMTHREADSIGNAL */
602 /* this is an undocumented DCB_ value used for mmThreads
603 * loword of dwCallBack contains the handle of the lpMMThd block
604 * which dwSignalCount has to be incremented
606 if (pFnGetMMThread16
)
608 WINE_MMTHREAD
* lpMMThd
= pFnGetMMThread16(LOWORD(dwCallBack
));
610 TRACE("mmThread (%04x, %p) !\n", LOWORD(dwCallBack
), lpMMThd
);
611 /* same as mmThreadSignal16 */
612 InterlockedIncrement(&lpMMThd
->dwSignalCount
);
613 SetEvent(lpMMThd
->hEvent
);
614 /* some other stuff on lpMMThd->hVxD */
619 /* this is an undocumented DCB_ value for... I don't know */
623 WARN("Unknown callback type %d\n", uFlags
& DCB_TYPEMASK
);
630 /******************************************************************
635 void DRIVER_UnloadAll(void)
638 LPWINE_DRIVER lpNextDrv
= NULL
;
642 EnterCriticalSection( &mmdriver_lock
);
644 for (lpDrv
= lpDrvItemList
; lpDrv
!= NULL
; lpDrv
= lpNextDrv
)
646 lpNextDrv
= lpDrv
->lpNextItem
;
648 /* session instances will be unloaded automatically */
649 if (!(lpDrv
->dwFlags
& WINE_GDF_SESSION
))
651 LeaveCriticalSection( &mmdriver_lock
);
652 CloseDriver((HDRVR
)lpDrv
, 0, 0);
654 /* restart from the beginning of the list */
659 LeaveCriticalSection( &mmdriver_lock
);
661 TRACE("Unloaded %u drivers\n", count
);