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
25 WINE_DEFAULT_DEBUG_CHANNEL(driver
);
27 static CRITICAL_SECTION mmdriver_lock
;
28 static CRITICAL_SECTION_DEBUG mmdriver_lock_debug
=
31 { &mmdriver_lock_debug
.ProcessLocksList
, &mmdriver_lock_debug
.ProcessLocksList
},
32 0, 0, { (DWORD_PTR
)(__FILE__
": mmdriver_lock") }
34 static CRITICAL_SECTION mmdriver_lock
= { &mmdriver_lock_debug
, -1, 0, 0, 0, 0 };
36 static LPWINE_DRIVER lpDrvItemList
/* = NULL */;
37 static const WCHAR HKLM_BASE
[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
38 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',0};
40 static void DRIVER_Dump(const char *comment
)
45 TRACE("%s\n", comment
);
47 EnterCriticalSection( &mmdriver_lock
);
49 for (lpDrv
= lpDrvItemList
; lpDrv
!= NULL
; lpDrv
= lpDrv
->lpNextItem
)
51 TRACE("%p, magic %04lx, id %p, next %p\n", lpDrv
, lpDrv
->dwMagic
, lpDrv
->d
.d32
.dwDriverID
, lpDrv
->lpNextItem
);
54 LeaveCriticalSection( &mmdriver_lock
);
58 /**************************************************************************
59 * DRIVER_GetNumberOfModuleRefs [internal]
61 * Returns the number of open drivers which share the same module.
63 static unsigned DRIVER_GetNumberOfModuleRefs(HMODULE hModule
, WINE_DRIVER
** found
)
68 EnterCriticalSection( &mmdriver_lock
);
70 if (found
) *found
= NULL
;
71 for (lpDrv
= lpDrvItemList
; lpDrv
; lpDrv
= lpDrv
->lpNextItem
)
73 if (lpDrv
->hModule
== hModule
)
75 if (found
&& !*found
) *found
= lpDrv
;
80 LeaveCriticalSection( &mmdriver_lock
);
84 /**************************************************************************
85 * DRIVER_FindFromHDrvr [internal]
87 * From a hDrvr being 32 bits, returns the WINE internal structure.
89 LPWINE_DRIVER
DRIVER_FindFromHDrvr(HDRVR hDrvr
)
95 d
= (LPWINE_DRIVER
)hDrvr
;
96 if (d
&& d
->dwMagic
!= WINE_DI_MAGIC
) d
= NULL
;
104 if (d
) TRACE("%p -> %p, %p\n", hDrvr
, d
->lpDrvProc
, (void *)d
->dwDriverID
);
105 else TRACE("%p -> NULL\n", hDrvr
);
110 /**************************************************************************
111 * DRIVER_SendMessage [internal]
113 static inline LRESULT
DRIVER_SendMessage(LPWINE_DRIVER lpDrv
, UINT msg
,
114 LPARAM lParam1
, LPARAM lParam2
)
118 TRACE("Before call32 proc=%p drvrID=%08lx hDrv=%p wMsg=%04x p1=%08lx p2=%08lx\n",
119 lpDrv
->lpDrvProc
, lpDrv
->dwDriverID
, lpDrv
, msg
, lParam1
, lParam2
);
120 ret
= lpDrv
->lpDrvProc(lpDrv
->dwDriverID
, (HDRVR
)lpDrv
, msg
, lParam1
, lParam2
);
121 TRACE("After call32 proc=%p drvrID=%08lx hDrv=%p wMsg=%04x p1=%08lx p2=%08lx => %08lx\n",
122 lpDrv
->lpDrvProc
, lpDrv
->dwDriverID
, lpDrv
, msg
, lParam1
, lParam2
, ret
);
127 /**************************************************************************
128 * SendDriverMessage [WINMM.@]
129 * DrvSendMessage [WINMM.@]
131 LRESULT WINAPI
SendDriverMessage(HDRVR hDriver
, UINT msg
, LPARAM lParam1
,
137 TRACE("(%p, %04X, %08lX, %08lX)\n", hDriver
, msg
, lParam1
, lParam2
);
139 if ((lpDrv
= DRIVER_FindFromHDrvr(hDriver
)) != NULL
) {
140 retval
= DRIVER_SendMessage(lpDrv
, msg
, lParam1
, lParam2
);
142 WARN("Bad driver handle %p\n", hDriver
);
144 TRACE("retval = %ld\n", retval
);
149 /**************************************************************************
150 * DRIVER_RemoveFromList [internal]
152 * Generates all the logic to handle driver closure / deletion
153 * Removes a driver struct to the list of open drivers.
155 static BOOL
DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv
)
157 /* last of this driver in list ? */
158 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->hModule
, NULL
) == 1) {
159 DRIVER_SendMessage(lpDrv
, DRV_DISABLE
, 0L, 0L);
160 DRIVER_SendMessage(lpDrv
, DRV_FREE
, 0L, 0L);
163 EnterCriticalSection( &mmdriver_lock
);
165 if (lpDrv
->lpPrevItem
)
166 lpDrv
->lpPrevItem
->lpNextItem
= lpDrv
->lpNextItem
;
168 lpDrvItemList
= lpDrv
->lpNextItem
;
169 if (lpDrv
->lpNextItem
)
170 lpDrv
->lpNextItem
->lpPrevItem
= lpDrv
->lpPrevItem
;
171 /* trash magic number */
172 lpDrv
->dwMagic
^= 0xa5a5a5a5;
173 lpDrv
->lpDrvProc
= NULL
;
174 lpDrv
->dwDriverID
= 0;
176 LeaveCriticalSection( &mmdriver_lock
);
181 /**************************************************************************
182 * DRIVER_AddToList [internal]
184 * Adds a driver struct to the list of open drivers.
185 * Generates all the logic to handle driver creation / open.
187 static BOOL
DRIVER_AddToList(LPWINE_DRIVER lpNewDrv
, LPARAM lParam1
, LPARAM lParam2
)
189 lpNewDrv
->dwMagic
= WINE_DI_MAGIC
;
190 /* First driver to be loaded for this module, need to load correctly the module */
191 /* first of this driver in list ? */
192 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv
->hModule
, NULL
) == 0) {
193 if (DRIVER_SendMessage(lpNewDrv
, DRV_LOAD
, 0L, 0L) != DRV_SUCCESS
) {
194 TRACE("DRV_LOAD failed on driver %p\n", lpNewDrv
);
197 /* returned value is not checked */
198 DRIVER_SendMessage(lpNewDrv
, DRV_ENABLE
, 0L, 0L);
201 /* Now just open a new instance of a driver on this module */
202 lpNewDrv
->dwDriverID
= DRIVER_SendMessage(lpNewDrv
, DRV_OPEN
, lParam1
, lParam2
);
204 if (lpNewDrv
->dwDriverID
== 0)
206 TRACE("DRV_OPEN failed on driver %p\n", lpNewDrv
);
210 EnterCriticalSection( &mmdriver_lock
);
212 lpNewDrv
->lpNextItem
= NULL
;
213 if (lpDrvItemList
== NULL
) {
214 lpDrvItemList
= lpNewDrv
;
215 lpNewDrv
->lpPrevItem
= NULL
;
217 LPWINE_DRIVER lpDrv
= lpDrvItemList
; /* find end of list */
218 while (lpDrv
->lpNextItem
!= NULL
)
219 lpDrv
= lpDrv
->lpNextItem
;
221 lpDrv
->lpNextItem
= lpNewDrv
;
222 lpNewDrv
->lpPrevItem
= lpDrv
;
225 LeaveCriticalSection( &mmdriver_lock
);
229 /**************************************************************************
230 * DRIVER_GetLibName [internal]
233 BOOL
DRIVER_GetLibName(LPCWSTR keyName
, LPCWSTR sectName
, LPWSTR buf
, int sz
)
237 static const WCHAR wszSystemIni
[] = {'S','Y','S','T','E','M','.','I','N','I',0};
238 WCHAR wsznull
= '\0';
240 /* This takes us as far as Windows NT\CurrentVersion */
241 lRet
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, HKLM_BASE
, 0, KEY_QUERY_VALUE
, &hKey
);
243 if (lRet
== ERROR_SUCCESS
)
245 /* Now we descend into the section name that we were given */
246 lRet
= RegOpenKeyExW(hKey
, sectName
, 0, KEY_QUERY_VALUE
, &hSecKey
);
248 if (lRet
== ERROR_SUCCESS
)
250 /* Retrieve the desired value - this is the filename of the lib */
252 lRet
= RegQueryValueExW(hSecKey
, keyName
, 0, 0, (void*)buf
, &bufLen
);
253 RegCloseKey( hSecKey
);
259 /* Finish up if we've got what we want from the registry */
260 if (lRet
== ERROR_SUCCESS
)
263 /* default to system.ini if we can't find it in the registry,
264 * to support native installations where system.ini is still used */
265 return GetPrivateProfileStringW(sectName
, keyName
, &wsznull
, buf
, sz
/ sizeof(WCHAR
), wszSystemIni
);
268 /**************************************************************************
269 * DRIVER_TryOpenDriver32 [internal]
271 * Tries to load a 32 bit driver whose DLL's (module) name is fn
273 LPWINE_DRIVER
DRIVER_TryOpenDriver32(LPCWSTR fn
, LPARAM lParam2
)
275 LPWINE_DRIVER lpDrv
= NULL
;
280 TRACE("(%s, %08lX);\n", debugstr_w(fn
), lParam2
);
282 if ((ptr
= strchrW(fn
, ' ')) != NULL
)
293 lpDrv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER
));
301 if ((hModule
= LoadLibraryW(fn
)) == 0)
303 cause
= "Not a 32 bit lib";
307 lpDrv
->lpDrvProc
= (DRIVERPROC
)GetProcAddress(hModule
, "DriverProc");
309 if (lpDrv
->lpDrvProc
== NULL
)
311 cause
= "no DriverProc";
316 lpDrv
->hModule
= hModule
;
317 lpDrv
->dwDriverID
= 0;
319 /* Win32 installable drivers must support a two phase opening scheme:
320 * + first open with NULL as lParam2 (session instance),
321 * + then do a second open with the real non null lParam2)
323 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->hModule
, NULL
) == 0 && lParam2
)
327 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, 0L))
329 cause
= "load0 failed";
332 ret
= DRIVER_TryOpenDriver32(fn
, lParam2
);
335 CloseDriver((HDRVR
)lpDrv
, 0L, 0L);
336 cause
= "load1 failed";
342 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, lParam2
))
344 cause
= "load failed";
348 TRACE("=> %p\n", lpDrv
);
352 FreeLibrary(hModule
);
353 HeapFree(GetProcessHeap(), 0, lpDrv
);
354 TRACE("Unable to load 32 bit module %s: %s\n", debugstr_w(fn
), cause
);
358 /**************************************************************************
359 * OpenDriverA [WINMM.@]
361 * (0,1,DRV_LOAD ,0 ,0)
362 * (0,1,DRV_ENABLE,0 ,0)
363 * (0,1,DRV_OPEN ,buf[256],0)
365 HDRVR WINAPI
OpenDriverA(LPCSTR lpDriverName
, LPCSTR lpSectionName
, LPARAM lParam
)
374 len
= MultiByteToWideChar( CP_ACP
, 0, lpDriverName
, -1, NULL
, 0 );
375 dn
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
377 MultiByteToWideChar( CP_ACP
, 0, lpDriverName
, -1, dn
, len
);
382 len
= MultiByteToWideChar( CP_ACP
, 0, lpSectionName
, -1, NULL
, 0 );
383 sn
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
385 MultiByteToWideChar( CP_ACP
, 0, lpSectionName
, -1, sn
, len
);
388 ret
= OpenDriver(dn
, sn
, lParam
);
391 HeapFree(GetProcessHeap(), 0, dn
);
392 HeapFree(GetProcessHeap(), 0, sn
);
396 /**************************************************************************
397 * OpenDriver [WINMM.@]
400 HDRVR WINAPI
OpenDriver(LPCWSTR lpDriverName
, LPCWSTR lpSectionName
, LPARAM lParam
)
402 LPWINE_DRIVER lpDrv
= NULL
;
404 LPCWSTR lsn
= lpSectionName
;
406 TRACE("(%s, %s, 0x%08lx);\n",
407 debugstr_w(lpDriverName
), debugstr_w(lpSectionName
), lParam
);
409 /* If no section name is specified, either the caller is intending on
410 opening a driver by filename, or wants to open a user-installable
411 driver that has an entry in the Drivers32 key in the registry */
414 /* Default registry key */
415 static const WCHAR wszDrivers32
[] = {'D','r','i','v','e','r','s','3','2',0};
417 lstrcpynW(libName
, lpDriverName
, sizeof(libName
) / sizeof(WCHAR
));
419 /* Try and open the driver by filename */
420 if ( (lpDrv
= DRIVER_TryOpenDriver32(libName
, lParam
)) )
423 /* If we got here, the file wasn't found. So we assume the caller
424 wanted a driver specified under the Drivers32 registry key */
428 /* Attempt to locate the driver filename in the registry */
429 if ( DRIVER_GetLibName(lpDriverName
, lsn
, libName
, sizeof(libName
)) )
431 /* Now we have the filename, we can try and load it */
432 if ( (lpDrv
= DRIVER_TryOpenDriver32(libName
, lParam
)) )
436 /* now we will try a 16 bit driver (and add all the glue to make it work... which
437 * is located in our mmsystem implementation)
438 * so ensure, we can load our mmsystem, otherwise just fail
440 WINMM_CheckForMMSystem();
442 if (pFnOpenDriver16
&&
443 (lpDrv
= pFnOpenDriver16(lpDriverName
, lpSectionName
, lParam
)))
445 if (DRIVER_AddToList(lpDrv
, 0, lParam
)) goto the_end
;
446 HeapFree(GetProcessHeap(), 0, lpDrv
);
448 TRACE("Failed to open driver %s from system.ini file, section %s\n",
449 debugstr_w(lpDriverName
), debugstr_w(lpSectionName
));
454 if (lpDrv
) TRACE("=> %p\n", lpDrv
);
458 /**************************************************************************
459 * CloseDriver [WINMM.@]
462 LRESULT WINAPI
CloseDriver(HDRVR hDrvr
, LPARAM lParam1
, LPARAM lParam2
)
467 TRACE("(%p, %08lX, %08lX);\n", hDrvr
, lParam1
, lParam2
);
469 DRIVER_Dump("BEFORE:");
471 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
)
473 LPWINE_DRIVER lpDrv0
;
475 DRIVER_SendMessage(lpDrv
, DRV_CLOSE
, lParam1
, lParam2
);
477 DRIVER_RemoveFromList(lpDrv
);
479 if (lpDrv
->dwFlags
& WINE_GDF_SESSION
)
480 FIXME("WINE_GDF_SESSION: Shouldn't happen (%p)\n", lpDrv
);
481 /* if driver has an opened session instance, we have to close it too */
482 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->hModule
, &lpDrv0
) == 1 &&
483 (lpDrv0
->dwFlags
& WINE_GDF_SESSION
))
485 DRIVER_SendMessage(lpDrv0
, DRV_CLOSE
, 0, 0);
486 DRIVER_RemoveFromList(lpDrv0
);
487 FreeLibrary(lpDrv0
->hModule
);
488 HeapFree(GetProcessHeap(), 0, lpDrv0
);
490 FreeLibrary(lpDrv
->hModule
);
492 HeapFree(GetProcessHeap(), 0, lpDrv
);
497 WARN("Failed to close driver\n");
501 DRIVER_Dump("AFTER:");
506 /**************************************************************************
507 * GetDriverFlags [WINMM.@]
508 * [in] hDrvr handle to the driver
511 * 0x00000000 if hDrvr is an invalid handle
512 * 0x80000000 if hDrvr is a valid 32 bit driver
513 * 0x90000000 if hDrvr is a valid 16 bit driver
515 * native WINMM doesn't return those flags
516 * 0x80000000 for a valid 32 bit driver and that's it
517 * (I may have mixed up the two flags :-(
519 DWORD WINAPI
GetDriverFlags(HDRVR hDrvr
)
524 TRACE("(%p)\n", hDrvr
);
526 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
) {
527 ret
= WINE_GDF_EXIST
| (lpDrv
->dwFlags
& WINE_GDF_EXTERNAL_MASK
);
532 /**************************************************************************
533 * GetDriverModuleHandle [WINMM.@]
534 * DrvGetModuleHandle [WINMM.@]
536 HMODULE WINAPI
GetDriverModuleHandle(HDRVR hDrvr
)
541 TRACE("(%p);\n", hDrvr
);
543 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
) {
544 hModule
= lpDrv
->hModule
;
546 TRACE("=> %p\n", hModule
);
550 /**************************************************************************
551 * DefDriverProc [WINMM.@]
552 * DrvDefDriverProc [WINMM.@]
554 LRESULT WINAPI
DefDriverProc(DWORD_PTR dwDriverIdentifier
, HDRVR hDrv
,
555 UINT Msg
, LPARAM lParam1
, LPARAM lParam2
)
571 /**************************************************************************
572 * DriverCallback [WINMM.@]
574 BOOL WINAPI
DriverCallback(DWORD_PTR dwCallBack
, DWORD uFlags
, HDRVR hDev
,
575 DWORD wMsg
, DWORD_PTR dwUser
, DWORD_PTR dwParam1
,
578 TRACE("(%08lX, %04X, %p, %04X, %08lX, %08lX, %08lX)\n",
579 dwCallBack
, uFlags
, hDev
, wMsg
, dwUser
, dwParam1
, dwParam2
);
581 switch (uFlags
& DCB_TYPEMASK
) {
585 WARN("uFlags=%04X has null DCB value, but dwCallBack=%08lX is not null !\n", uFlags
, dwCallBack
);
588 TRACE("Window(%04lX) handle=%p!\n", dwCallBack
, hDev
);
589 PostMessageA((HWND
)dwCallBack
, wMsg
, (WPARAM
)hDev
, dwParam1
);
591 case DCB_TASK
: /* aka DCB_THREAD */
592 TRACE("Task(%04lx) !\n", dwCallBack
);
593 PostThreadMessageA(dwCallBack
, wMsg
, (WPARAM
)hDev
, dwParam1
);
596 TRACE("Function (32 bit) !\n");
597 ((LPDRVCALLBACK
)dwCallBack
)(hDev
, wMsg
, dwUser
, dwParam1
, dwParam2
);
600 TRACE("Event(%08lx) !\n", dwCallBack
);
601 SetEvent((HANDLE
)dwCallBack
);
604 /* FIXME: for now only usable in mmsystem.dll16
605 * If needed, should be enabled back
607 case 6: /* I would dub it DCB_MMTHREADSIGNAL */
608 /* this is an undocumented DCB_ value used for mmThreads
609 * loword of dwCallBack contains the handle of the lpMMThd block
610 * which dwSignalCount has to be incremented
612 if (pFnGetMMThread16
)
614 WINE_MMTHREAD
* lpMMThd
= pFnGetMMThread16(LOWORD(dwCallBack
));
616 TRACE("mmThread (%04x, %p) !\n", LOWORD(dwCallBack
), lpMMThd
);
617 /* same as mmThreadSignal16 */
618 InterlockedIncrement(&lpMMThd
->dwSignalCount
);
619 SetEvent(lpMMThd
->hEvent
);
620 /* some other stuff on lpMMThd->hVxD */
626 /* this is an undocumented DCB_ value for... I don't know */
630 WARN("Unknown callback type %d\n", uFlags
& DCB_TYPEMASK
);
637 /******************************************************************
642 void DRIVER_UnloadAll(void)
645 LPWINE_DRIVER lpNextDrv
= NULL
;
649 EnterCriticalSection( &mmdriver_lock
);
651 for (lpDrv
= lpDrvItemList
; lpDrv
!= NULL
; lpDrv
= lpNextDrv
)
653 lpNextDrv
= lpDrv
->lpNextItem
;
655 /* session instances will be unloaded automatically */
656 if (!(lpDrv
->dwFlags
& WINE_GDF_SESSION
))
658 LeaveCriticalSection( &mmdriver_lock
);
659 CloseDriver((HDRVR
)lpDrv
, 0, 0);
661 /* restart from the beginning of the list */
666 LeaveCriticalSection( &mmdriver_lock
);
668 TRACE("Unloaded %u drivers\n", count
);