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 static void DRIVER_Dump(const char *comment
)
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
);
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
)
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
;
96 LeaveCriticalSection( &mmdriver_lock
);
100 /**************************************************************************
101 * DRIVER_FindFromHDrvr [internal]
103 * From a hDrvr being 32 bits, returns the WINE internal structure.
105 LPWINE_DRIVER
DRIVER_FindFromHDrvr(HDRVR hDrvr
)
111 d
= (LPWINE_DRIVER
)hDrvr
;
112 if (d
&& d
->dwMagic
!= WINE_DI_MAGIC
) d
= NULL
;
120 if (d
) TRACE("%p -> %p, %p\n", hDrvr
, d
->lpDrvProc
, (void *)d
->dwDriverID
);
121 else TRACE("%p -> NULL\n", hDrvr
);
126 /**************************************************************************
127 * DRIVER_SendMessage [internal]
129 static inline LRESULT
DRIVER_SendMessage(LPWINE_DRIVER lpDrv
, UINT msg
,
130 LPARAM lParam1
, LPARAM lParam2
)
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
);
143 /**************************************************************************
144 * SendDriverMessage [WINMM.@]
145 * DrvSendMessage [WINMM.@]
147 LRESULT WINAPI
SendDriverMessage(HDRVR hDriver
, UINT msg
, LPARAM lParam1
,
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
);
158 WARN("Bad driver handle %p\n", hDriver
);
160 TRACE("retval = %ld\n", 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
;
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
);
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
);
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
);
226 EnterCriticalSection( &mmdriver_lock
);
228 lpNewDrv
->lpNextItem
= NULL
;
229 if (lpDrvItemList
== NULL
) {
230 lpDrvItemList
= lpNewDrv
;
231 lpNewDrv
->lpPrevItem
= NULL
;
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
);
245 /**************************************************************************
246 * DRIVER_GetLibName [internal]
249 BOOL
DRIVER_GetLibName(LPCWSTR keyName
, LPCWSTR sectName
, LPWSTR buf
, int sz
)
253 static const WCHAR wszSystemIni
[] = {'S','Y','S','T','E','M','.','I','N','I',0};
254 WCHAR wsznull
= '\0';
256 /* This takes us as far as Windows NT\CurrentVersion */
257 lRet
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, HKLM_BASE
, 0, KEY_QUERY_VALUE
, &hKey
);
259 if (lRet
== ERROR_SUCCESS
)
261 /* Now we descend into the section name that we were given */
262 lRet
= RegOpenKeyExW(hKey
, sectName
, 0, KEY_QUERY_VALUE
, &hSecKey
);
264 if (lRet
== ERROR_SUCCESS
)
266 /* Retrieve the desired value - this is the filename of the lib */
268 lRet
= RegQueryValueExW(hSecKey
, keyName
, 0, 0, (void*)buf
, &bufLen
);
269 RegCloseKey( hSecKey
);
275 /* Finish up if we've got what we want from the registry */
276 if (lRet
== ERROR_SUCCESS
)
279 /* default to system.ini if we can't find it in the registry,
280 * to support native installations where system.ini is still used */
281 return GetPrivateProfileStringW(sectName
, keyName
, &wsznull
, buf
, sz
/ sizeof(WCHAR
), wszSystemIni
);
284 /**************************************************************************
285 * DRIVER_TryOpenDriver32 [internal]
287 * Tries to load a 32 bit driver whose DLL's (module) name is fn
289 LPWINE_DRIVER
DRIVER_TryOpenDriver32(LPCWSTR fn
, LPARAM lParam2
)
291 LPWINE_DRIVER lpDrv
= NULL
;
296 TRACE("(%s, %08lX);\n", debugstr_w(fn
), lParam2
);
298 if ((ptr
= strchrW(fn
, ' ')) != NULL
)
309 lpDrv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER
));
317 if ((hModule
= LoadLibraryW(fn
)) == 0)
319 cause
= "Not a 32 bit lib";
323 lpDrv
->lpDrvProc
= (DRIVERPROC
)GetProcAddress(hModule
, "DriverProc");
325 if (lpDrv
->lpDrvProc
== NULL
)
327 cause
= "no DriverProc";
332 lpDrv
->hModule
= hModule
;
333 lpDrv
->dwDriverID
= 0;
335 /* Win32 installable drivers must support a two phase opening scheme:
336 * + first open with NULL as lParam2 (session instance),
337 * + then do a second open with the real non null lParam2)
339 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->hModule
, NULL
) == 0 && lParam2
)
343 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, 0L))
345 cause
= "load0 failed";
348 ret
= DRIVER_TryOpenDriver32(fn
, lParam2
);
351 CloseDriver((HDRVR
)lpDrv
, 0L, 0L);
352 cause
= "load1 failed";
358 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, lParam2
))
360 cause
= "load failed";
364 TRACE("=> %p\n", lpDrv
);
368 FreeLibrary(hModule
);
369 HeapFree(GetProcessHeap(), 0, lpDrv
);
370 TRACE("Unable to load 32 bit module %s: %s\n", debugstr_w(fn
), cause
);
374 /**************************************************************************
375 * OpenDriverA [WINMM.@]
377 * (0,1,DRV_LOAD ,0 ,0)
378 * (0,1,DRV_ENABLE,0 ,0)
379 * (0,1,DRV_OPEN ,buf[256],0)
381 HDRVR WINAPI
OpenDriverA(LPCSTR lpDriverName
, LPCSTR lpSectionName
, LPARAM lParam
)
390 len
= MultiByteToWideChar( CP_ACP
, 0, lpDriverName
, -1, NULL
, 0 );
391 dn
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
393 MultiByteToWideChar( CP_ACP
, 0, lpDriverName
, -1, dn
, len
);
398 len
= MultiByteToWideChar( CP_ACP
, 0, lpSectionName
, -1, NULL
, 0 );
399 sn
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
401 MultiByteToWideChar( CP_ACP
, 0, lpSectionName
, -1, sn
, len
);
404 ret
= OpenDriver(dn
, sn
, lParam
);
407 HeapFree(GetProcessHeap(), 0, dn
);
408 HeapFree(GetProcessHeap(), 0, sn
);
412 /**************************************************************************
413 * OpenDriver [WINMM.@]
416 HDRVR WINAPI
OpenDriver(LPCWSTR lpDriverName
, LPCWSTR lpSectionName
, LPARAM lParam
)
418 LPWINE_DRIVER lpDrv
= NULL
;
420 LPCWSTR lsn
= lpSectionName
;
422 TRACE("(%s, %s, 0x%08lx);\n",
423 debugstr_w(lpDriverName
), debugstr_w(lpSectionName
), lParam
);
425 /* If no section name is specified, either the caller is intending on
426 opening a driver by filename, or wants to open a user-installable
427 driver that has an entry in the Drivers32 key in the registry */
430 /* Default registry key */
431 static const WCHAR wszDrivers32
[] = {'D','r','i','v','e','r','s','3','2',0};
433 lstrcpynW(libName
, lpDriverName
, sizeof(libName
) / sizeof(WCHAR
));
435 /* Try and open the driver by filename */
436 if ( (lpDrv
= DRIVER_TryOpenDriver32(libName
, lParam
)) )
439 /* If we got here, the file wasn't found. So we assume the caller
440 wanted a driver specified under the Drivers32 registry key */
444 /* Attempt to locate the driver filename in the registry */
445 if ( DRIVER_GetLibName(lpDriverName
, lsn
, libName
, sizeof(libName
)) )
447 /* Now we have the filename, we can try and load it */
448 if ( (lpDrv
= DRIVER_TryOpenDriver32(libName
, lParam
)) )
452 /* now we will try a 16 bit driver (and add all the glue to make it work... which
453 * is located in our mmsystem implementation)
454 * so ensure, we can load our mmsystem, otherwise just fail
456 WINMM_CheckForMMSystem();
458 if (pFnOpenDriver16
&&
459 (lpDrv
= pFnOpenDriver16(lpDriverName
, lpSectionName
, lParam
)))
461 if (DRIVER_AddToList(lpDrv
, 0, lParam
)) goto the_end
;
462 HeapFree(GetProcessHeap(), 0, lpDrv
);
464 TRACE("Failed to open driver %s from system.ini file, section %s\n",
465 debugstr_w(lpDriverName
), debugstr_w(lpSectionName
));
470 if (lpDrv
) TRACE("=> %08lx\n", (DWORD
)lpDrv
);
474 /**************************************************************************
475 * CloseDriver [WINMM.@]
478 LRESULT WINAPI
CloseDriver(HDRVR hDrvr
, LPARAM lParam1
, LPARAM lParam2
)
483 TRACE("(%p, %08lX, %08lX);\n", hDrvr
, lParam1
, lParam2
);
485 DRIVER_Dump("BEFORE:");
487 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
)
489 LPWINE_DRIVER lpDrv0
;
491 DRIVER_SendMessage(lpDrv
, DRV_CLOSE
, lParam1
, lParam2
);
493 DRIVER_RemoveFromList(lpDrv
);
495 if (lpDrv
->dwFlags
& WINE_GDF_SESSION
)
496 FIXME("WINE_GDF_SESSION: Shouldn't happen (%p)\n", lpDrv
);
497 /* if driver has an opened session instance, we have to close it too */
498 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->hModule
, &lpDrv0
) == 1 &&
499 (lpDrv0
->dwFlags
& WINE_GDF_SESSION
))
501 DRIVER_SendMessage(lpDrv0
, DRV_CLOSE
, 0, 0);
502 DRIVER_RemoveFromList(lpDrv0
);
503 FreeLibrary(lpDrv0
->hModule
);
504 HeapFree(GetProcessHeap(), 0, lpDrv0
);
506 FreeLibrary(lpDrv
->hModule
);
508 HeapFree(GetProcessHeap(), 0, lpDrv
);
513 WARN("Failed to close driver\n");
517 DRIVER_Dump("AFTER:");
522 /**************************************************************************
523 * GetDriverFlags [WINMM.@]
524 * [in] hDrvr handle to the driver
527 * 0x00000000 if hDrvr is an invalid handle
528 * 0x80000000 if hDrvr is a valid 32 bit driver
529 * 0x90000000 if hDrvr is a valid 16 bit driver
531 * native WINMM doesn't return those flags
532 * 0x80000000 for a valid 32 bit driver and that's it
533 * (I may have mixed up the two flags :-(
535 DWORD WINAPI
GetDriverFlags(HDRVR hDrvr
)
540 TRACE("(%p)\n", hDrvr
);
542 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
) {
543 ret
= WINE_GDF_EXIST
| (lpDrv
->dwFlags
& WINE_GDF_EXTERNAL_MASK
);
548 /**************************************************************************
549 * GetDriverModuleHandle [WINMM.@]
550 * DrvGetModuleHandle [WINMM.@]
552 HMODULE WINAPI
GetDriverModuleHandle(HDRVR hDrvr
)
557 TRACE("(%p);\n", hDrvr
);
559 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
) {
560 hModule
= lpDrv
->hModule
;
562 TRACE("=> %p\n", hModule
);
566 /**************************************************************************
567 * DefDriverProc [WINMM.@]
568 * DrvDefDriverProc [WINMM.@]
570 LRESULT WINAPI
DefDriverProc(DWORD_PTR dwDriverIdentifier
, HDRVR hDrv
,
571 UINT Msg
, LPARAM lParam1
, LPARAM lParam2
)
587 /**************************************************************************
588 * DriverCallback [WINMM.@]
590 BOOL WINAPI
DriverCallback(DWORD_PTR dwCallBack
, DWORD uFlags
, HDRVR hDev
,
591 DWORD wMsg
, DWORD_PTR dwUser
, DWORD_PTR dwParam1
,
594 TRACE("(%08lX, %04X, %p, %04X, %08lX, %08lX, %08lX)\n",
595 dwCallBack
, uFlags
, hDev
, wMsg
, dwUser
, dwParam1
, dwParam2
);
597 switch (uFlags
& DCB_TYPEMASK
) {
601 WARN("uFlags=%04X has null DCB value, but dwCallBack=%08lX is not null !\n", uFlags
, dwCallBack
);
604 TRACE("Window(%04lX) handle=%p!\n", dwCallBack
, hDev
);
605 PostMessageA((HWND
)dwCallBack
, wMsg
, (WPARAM
)hDev
, dwParam1
);
607 case DCB_TASK
: /* aka DCB_THREAD */
608 TRACE("Task(%04lx) !\n", dwCallBack
);
609 PostThreadMessageA(dwCallBack
, wMsg
, (WPARAM
)hDev
, dwParam1
);
612 TRACE("Function (32 bit) !\n");
613 ((LPDRVCALLBACK
)dwCallBack
)(hDev
, wMsg
, dwUser
, dwParam1
, dwParam2
);
616 TRACE("Event(%08lx) !\n", dwCallBack
);
617 SetEvent((HANDLE
)dwCallBack
);
620 /* FIXME: for now only usable in mmsystem.dll16
621 * If needed, should be enabled back
623 case 6: /* I would dub it DCB_MMTHREADSIGNAL */
624 /* this is an undocumented DCB_ value used for mmThreads
625 * loword of dwCallBack contains the handle of the lpMMThd block
626 * which dwSignalCount has to be incremented
628 if (pFnGetMMThread16
)
630 WINE_MMTHREAD
* lpMMThd
= pFnGetMMThread16(LOWORD(dwCallBack
));
632 TRACE("mmThread (%04x, %p) !\n", LOWORD(dwCallBack
), lpMMThd
);
633 /* same as mmThreadSignal16 */
634 InterlockedIncrement(&lpMMThd
->dwSignalCount
);
635 SetEvent(lpMMThd
->hEvent
);
636 /* some other stuff on lpMMThd->hVxD */
642 /* this is an undocumented DCB_ value for... I don't know */
646 WARN("Unknown callback type %d\n", uFlags
& DCB_TYPEMASK
);
653 /******************************************************************
658 void DRIVER_UnloadAll(void)
661 LPWINE_DRIVER lpNextDrv
= NULL
;
665 EnterCriticalSection( &mmdriver_lock
);
667 for (lpDrv
= lpDrvItemList
; lpDrv
!= NULL
; lpDrv
= lpNextDrv
)
669 lpNextDrv
= lpDrv
->lpNextItem
;
671 /* session instances will be unloaded automatically */
672 if (!(lpDrv
->dwFlags
& WINE_GDF_SESSION
))
674 LeaveCriticalSection( &mmdriver_lock
);
675 CloseDriver((HDRVR
)lpDrv
, 0, 0);
677 /* restart from the beginning of the list */
682 LeaveCriticalSection( &mmdriver_lock
);
684 TRACE("Unloaded %u drivers\n", count
);