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 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
) {
263 lRet
= RegQueryValueExW(hSecKey
, keyName
, 0, 0, (void*)buf
, &bufLen
);
264 RegCloseKey( hSecKey
);
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
;
288 TRACE("(%s, %08lX);\n", debugstr_w(fn
), lParam2
);
290 if ((ptr
= strchrW(fn
, ' ')) != NULL
) {
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
;}
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
)
316 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, 0L))
318 cause
= "load0 failed";
321 ret
= DRIVER_TryOpenDriver32(fn
, lParam2
);
324 CloseDriver((HDRVR
)lpDrv
, 0L, 0L);
325 cause
= "load1 failed";
328 lpDrv
->dwFlags
|= WINE_GDF_SESSION
;
332 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, lParam2
))
333 {cause
= "load failed"; goto exit
;}
335 TRACE("=> %p\n", lpDrv
);
338 FreeLibrary(hModule
);
339 HeapFree(GetProcessHeap(), 0, lpDrv
);
340 TRACE("Unable to load 32 bit module %s: %s\n", debugstr_w(fn
), cause
);
344 /**************************************************************************
345 * OpenDriverA [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
)
360 len
= MultiByteToWideChar( CP_ACP
, 0, lpDriverName
, -1, NULL
, 0 );
361 dn
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
363 MultiByteToWideChar( CP_ACP
, 0, lpDriverName
, -1, dn
, len
);
368 len
= MultiByteToWideChar( CP_ACP
, 0, lpSectionName
, -1, NULL
, 0 );
369 sn
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
371 MultiByteToWideChar( CP_ACP
, 0, lpSectionName
, -1, sn
, len
);
374 ret
= OpenDriver(dn
, sn
, lParam
);
377 HeapFree(GetProcessHeap(), 0, dn
);
378 HeapFree(GetProcessHeap(), 0, sn
);
382 /**************************************************************************
383 * OpenDriver [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:");
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
)))
405 if (DRIVER_GetLibName(lpDriverName
, lsn
, libName
, sizeof(libName
)) &&
406 (lpDrv
= DRIVER_TryOpenDriver32(libName
, lParam
)))
409 TRACE("Failed to open driver %s from system.ini file, section %s\n",
410 debugstr_w(lpDriverName
), debugstr_w(lpSectionName
));
413 TRACE("=> %p\n", lpDrv
);
415 DRIVER_Dump("AFTER:");
420 /**************************************************************************
421 * CloseDriver [WINMM.@]
424 LRESULT WINAPI
CloseDriver(HDRVR hDrvr
, LPARAM lParam1
, LPARAM lParam2
)
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
);
459 WARN("Failed to close driver\n");
463 DRIVER_Dump("AFTER:");
468 /**************************************************************************
469 * GetDriverFlags [WINMM.@]
470 * [in] hDrvr handle to the driver
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
)
486 TRACE("(%p)\n", hDrvr
);
488 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
) {
489 ret
= WINE_GDF_EXIST
| (lpDrv
->dwFlags
& WINE_GDF_EXTERNAL_MASK
);
494 /**************************************************************************
495 * GetDriverModuleHandle [WINMM.@]
496 * DrvGetModuleHandle [WINMM.@]
498 HMODULE WINAPI
GetDriverModuleHandle(HDRVR hDrvr
)
503 TRACE("(%p);\n", hDrvr
);
505 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
) {
506 hModule
= lpDrv
->hModule
;
508 TRACE("=> %p\n", hModule
);
512 /**************************************************************************
513 * DefDriverProc [WINMM.@]
514 * DrvDefDriverProc [WINMM.@]
516 LRESULT WINAPI
DefDriverProc(DWORD_PTR dwDriverIdentifier
, HDRVR hDrv
,
517 UINT Msg
, LPARAM lParam1
, LPARAM lParam2
)
533 /**************************************************************************
534 * DriverCallback [WINMM.@]
536 BOOL WINAPI
DriverCallback(DWORD_PTR dwCallBack
, DWORD uFlags
, HDRVR hDev
,
537 DWORD wMsg
, DWORD_PTR dwUser
, DWORD_PTR dwParam1
,
540 TRACE("(%08lX, %04X, %p, %04X, %08lX, %08lX, %08lX)\n",
541 dwCallBack
, uFlags
, hDev
, wMsg
, dwUser
, dwParam1
, dwParam2
);
543 switch (uFlags
& DCB_TYPEMASK
) {
548 TRACE("Window(%04lX) handle=%p!\n", dwCallBack
, hDev
);
549 PostMessageA((HWND
)dwCallBack
, wMsg
, (WPARAM
)hDev
, dwParam1
);
551 case DCB_TASK
: /* aka DCB_THREAD */
552 TRACE("Task(%04lx) !\n", dwCallBack
);
553 PostThreadMessageA(dwCallBack
, wMsg
, (WPARAM
)hDev
, dwParam1
);
556 TRACE("Function (32 bit) !\n");
558 ((LPDRVCALLBACK
)dwCallBack
)(hDev
, wMsg
, dwUser
, dwParam1
, dwParam2
);
561 TRACE("Event(%08lx) !\n", dwCallBack
);
562 SetEvent((HANDLE
)dwCallBack
);
565 /* FIXME: for now only usable in mmsystem.dll16
566 * If needed, should be enabled back
568 case 6: /* I would dub it DCB_MMTHREADSIGNAL */
569 /* this is an undocumented DCB_ value used for mmThreads
570 * loword of dwCallBack contains the handle of the lpMMThd block
571 * which dwSignalCount has to be incremented
573 if (pFnGetMMThread16
)
575 WINE_MMTHREAD
* lpMMThd
= pFnGetMMThread16(LOWORD(dwCallBack
));
577 TRACE("mmThread (%04x, %p) !\n", LOWORD(dwCallBack
), lpMMThd
);
578 /* same as mmThreadSignal16 */
579 InterlockedIncrement(&lpMMThd
->dwSignalCount
);
580 SetEvent(lpMMThd
->hEvent
);
581 /* some other stuff on lpMMThd->hVxD */
587 /* this is an undocumented DCB_ value for... I don't know */
591 WARN("Unknown callback type %d\n", uFlags
& DCB_TYPEMASK
);
598 /******************************************************************
603 void DRIVER_UnloadAll(void)
606 LPWINE_DRIVER lpNextDrv
= NULL
;
610 EnterCriticalSection( &mmdriver_lock
);
612 for (lpDrv
= lpDrvItemList
; lpDrv
!= NULL
; lpDrv
= lpNextDrv
)
614 lpNextDrv
= lpDrv
->lpNextItem
;
616 /* session instances will be unloaded automatically */
617 if (!(lpDrv
->dwFlags
& WINE_GDF_SESSION
))
619 LeaveCriticalSection( &mmdriver_lock
);
620 CloseDriver((HDRVR
)lpDrv
, 0, 0);
622 /* restart from the beginning of the list */
627 LeaveCriticalSection( &mmdriver_lock
);
629 TRACE("Unloaded %u drivers\n", count
);