1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
4 * WINE Drivers functions
6 * Copyright 1994 Martin Ayotte
7 * Copyright 1998 Marcus Meissner
8 * Copyright 1999 Eric Pouech
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(driver
);
39 #define HKLM_BASE "Software\\Microsoft\\Windows NT\\CurrentVersion"
41 static LPWINE_DRIVER lpDrvItemList
/* = NULL */;
43 WINE_MMTHREAD
* (*pFnGetMMThread16
)(UINT16 h
) /* = NULL */;
44 LPWINE_DRIVER (*pFnOpenDriver16
)(LPCSTR
,LPCSTR
,LPARAM
) /* = NULL */;
45 LRESULT (*pFnCloseDriver16
)(UINT16
,LPARAM
,LPARAM
) /* = NULL */;
46 LRESULT (*pFnSendMessage16
)(UINT16
,UINT
,LPARAM
,LPARAM
) /* = NULL */;
48 /**************************************************************************
49 * DRIVER_GetNumberOfModuleRefs [internal]
51 * Returns the number of open drivers which share the same module.
53 static unsigned DRIVER_GetNumberOfModuleRefs(HMODULE hModule
, WINE_DRIVER
** found
)
58 if (found
) *found
= NULL
;
59 for (lpDrv
= lpDrvItemList
; lpDrv
; lpDrv
= lpDrv
->lpNextItem
)
61 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
) && lpDrv
->d
.d32
.hModule
== hModule
)
63 if (found
&& !*found
) *found
= lpDrv
;
70 /**************************************************************************
71 * DRIVER_FindFromHDrvr [internal]
73 * From a hDrvr being 32 bits, returns the WINE internal structure.
75 LPWINE_DRIVER
DRIVER_FindFromHDrvr(HDRVR hDrvr
)
77 LPWINE_DRIVER d
= (LPWINE_DRIVER
)hDrvr
;
79 if (hDrvr
&& HeapValidate(GetProcessHeap(), 0, d
) && d
->dwMagic
== WINE_DI_MAGIC
) {
85 /**************************************************************************
86 * DRIVER_SendMessage [internal]
88 static LRESULT
inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv
, UINT msg
,
89 LPARAM lParam1
, LPARAM lParam2
)
93 if (lpDrv
->dwFlags
& WINE_GDF_16BIT
) {
94 /* no need to check mmsystem presence: the driver must have been opened as a 16 bit one,
97 ret
= pFnSendMessage16(lpDrv
->d
.d16
.hDriver16
, msg
, lParam1
, lParam2
);
99 TRACE("Before call32 proc=%p drvrID=%08lx hDrv=%p wMsg=%04x p1=%08lx p2=%08lx\n",
100 lpDrv
->d
.d32
.lpDrvProc
, lpDrv
->d
.d32
.dwDriverID
, (HDRVR
)lpDrv
, msg
, lParam1
, lParam2
);
101 ret
= lpDrv
->d
.d32
.lpDrvProc(lpDrv
->d
.d32
.dwDriverID
, (HDRVR
)lpDrv
, msg
, lParam1
, lParam2
);
102 TRACE("After call32 proc=%p drvrID=%08lx hDrv=%p wMsg=%04x p1=%08lx p2=%08lx => %08lx\n",
103 lpDrv
->d
.d32
.lpDrvProc
, lpDrv
->d
.d32
.dwDriverID
, (HDRVR
)lpDrv
, msg
, lParam1
, lParam2
, ret
);
108 /**************************************************************************
109 * SendDriverMessage [WINMM.@]
110 * DrvSendMessage [WINMM.@]
112 LRESULT WINAPI
SendDriverMessage(HDRVR hDriver
, UINT msg
, LPARAM lParam1
,
118 TRACE("(%p, %04X, %08lX, %08lX)\n", hDriver
, msg
, lParam1
, lParam2
);
120 if ((lpDrv
= DRIVER_FindFromHDrvr(hDriver
)) != NULL
) {
121 retval
= DRIVER_SendMessage(lpDrv
, msg
, lParam1
, lParam2
);
123 WARN("Bad driver handle %p\n", hDriver
);
125 TRACE("retval = %ld\n", retval
);
130 /**************************************************************************
131 * DRIVER_RemoveFromList [internal]
133 * Generates all the logic to handle driver closure / deletion
134 * Removes a driver struct to the list of open drivers.
136 static BOOL
DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv
)
138 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
)) {
139 /* last of this driver in list ? */
140 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->d
.d32
.hModule
, NULL
) == 1) {
141 DRIVER_SendMessage(lpDrv
, DRV_DISABLE
, 0L, 0L);
142 DRIVER_SendMessage(lpDrv
, DRV_FREE
, 0L, 0L);
146 if (lpDrv
->lpPrevItem
)
147 lpDrv
->lpPrevItem
->lpNextItem
= lpDrv
->lpNextItem
;
149 lpDrvItemList
= lpDrv
->lpNextItem
;
150 if (lpDrv
->lpNextItem
)
151 lpDrv
->lpNextItem
->lpPrevItem
= lpDrv
->lpPrevItem
;
152 /* trash magic number */
153 lpDrv
->dwMagic
^= 0xa5a5a5a5;
158 /**************************************************************************
159 * DRIVER_AddToList [internal]
161 * Adds a driver struct to the list of open drivers.
162 * Generates all the logic to handle driver creation / open.
164 static BOOL
DRIVER_AddToList(LPWINE_DRIVER lpNewDrv
, LPARAM lParam1
, LPARAM lParam2
)
166 lpNewDrv
->dwMagic
= WINE_DI_MAGIC
;
167 /* First driver to be loaded for this module, need to load correctly the module */
168 if (!(lpNewDrv
->dwFlags
& WINE_GDF_16BIT
)) {
169 /* first of this driver in list ? */
170 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv
->d
.d32
.hModule
, NULL
) == 0) {
171 if (DRIVER_SendMessage(lpNewDrv
, DRV_LOAD
, 0L, 0L) != DRV_SUCCESS
) {
172 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD
)lpNewDrv
);
175 /* returned value is not checked */
176 DRIVER_SendMessage(lpNewDrv
, DRV_ENABLE
, 0L, 0L);
180 lpNewDrv
->lpNextItem
= NULL
;
181 if (lpDrvItemList
== NULL
) {
182 lpDrvItemList
= lpNewDrv
;
183 lpNewDrv
->lpPrevItem
= NULL
;
185 LPWINE_DRIVER lpDrv
= lpDrvItemList
; /* find end of list */
186 while (lpDrv
->lpNextItem
!= NULL
)
187 lpDrv
= lpDrv
->lpNextItem
;
189 lpDrv
->lpNextItem
= lpNewDrv
;
190 lpNewDrv
->lpPrevItem
= lpDrv
;
193 if (!(lpNewDrv
->dwFlags
& WINE_GDF_16BIT
)) {
194 /* Now just open a new instance of a driver on this module */
195 lpNewDrv
->d
.d32
.dwDriverID
= DRIVER_SendMessage(lpNewDrv
, DRV_OPEN
, lParam1
, lParam2
);
197 if (lpNewDrv
->d
.d32
.dwDriverID
== 0) {
198 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD
)lpNewDrv
);
199 DRIVER_RemoveFromList(lpNewDrv
);
206 /**************************************************************************
207 * DRIVER_GetLibName [internal]
210 BOOL
DRIVER_GetLibName(LPCSTR keyName
, LPCSTR sectName
, LPSTR buf
, int sz
)
215 lRet
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
, HKLM_BASE
, 0, KEY_QUERY_VALUE
, &hKey
);
216 if (lRet
== ERROR_SUCCESS
) {
217 lRet
= RegOpenKeyExA(hKey
, sectName
, 0, KEY_QUERY_VALUE
, &hSecKey
);
218 if (lRet
== ERROR_SUCCESS
) {
219 lRet
= RegQueryValueExA(hSecKey
, keyName
, 0, 0, buf
, &bufLen
);
220 RegCloseKey( hSecKey
);
224 if (lRet
== ERROR_SUCCESS
) return TRUE
;
225 /* default to system.ini if we can't find it in the registry,
226 * to support native installations where system.ini is still used */
227 return GetPrivateProfileStringA(sectName
, keyName
, "", buf
, sz
, "SYSTEM.INI");
230 /**************************************************************************
231 * DRIVER_TryOpenDriver32 [internal]
233 * Tries to load a 32 bit driver whose DLL's (module) name is fn
235 LPWINE_DRIVER
DRIVER_TryOpenDriver32(LPCSTR fn
, LPARAM lParam2
)
237 LPWINE_DRIVER lpDrv
= NULL
;
242 TRACE("(%s, %08lX);\n", debugstr_a(fn
), lParam2
);
244 if ((ptr
= strchr(fn
, ' ')) != NULL
) {
246 while (*ptr
== ' ') ptr
++;
247 if (*ptr
== '\0') ptr
= NULL
;
250 lpDrv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER
));
251 if (lpDrv
== NULL
) {cause
= "OOM"; goto exit
;}
253 if ((hModule
= LoadLibraryA(fn
)) == 0) {cause
= "Not a 32 bit lib"; goto exit
;}
255 lpDrv
->d
.d32
.lpDrvProc
= (DRIVERPROC
)GetProcAddress(hModule
, "DriverProc");
256 if (lpDrv
->d
.d32
.lpDrvProc
== NULL
) {cause
= "no DriverProc"; goto exit
;}
259 lpDrv
->d
.d32
.hModule
= hModule
;
260 lpDrv
->d
.d32
.dwDriverID
= 0;
262 /* Win32 installable drivers must support a two phase opening scheme:
263 * + first open with NULL as lParam2 (session instance),
264 * + then do a second open with the real non null lParam2)
266 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->d
.d32
.hModule
, NULL
) == 0 && lParam2
)
270 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, 0L))
272 cause
= "load0 failed";
275 ret
= DRIVER_TryOpenDriver32(fn
, lParam2
);
278 CloseDriver((HDRVR
)lpDrv
, 0L, 0L);
279 cause
= "load1 failed";
285 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, lParam2
))
286 {cause
= "load failed"; goto exit
;}
288 TRACE("=> %p\n", lpDrv
);
291 FreeLibrary(hModule
);
292 HeapFree(GetProcessHeap(), 0, lpDrv
);
293 TRACE("Unable to load 32 bit module %s: %s\n", debugstr_a(fn
), cause
);
297 /**************************************************************************
298 * OpenDriverA [WINMM.@]
300 * (0,1,DRV_LOAD ,0 ,0)
301 * (0,1,DRV_ENABLE,0 ,0)
302 * (0,1,DRV_OPEN ,buf[256],0)
304 HDRVR WINAPI
OpenDriverA(LPCSTR lpDriverName
, LPCSTR lpSectionName
, LPARAM lParam2
)
306 LPWINE_DRIVER lpDrv
= NULL
;
308 LPCSTR lsn
= lpSectionName
;
310 TRACE("(%s, %s, 0x%08lx);\n", debugstr_a(lpDriverName
), debugstr_a(lpSectionName
), lParam2
);
313 lstrcpynA(libName
, lpDriverName
, sizeof(libName
));
315 if ((lpDrv
= DRIVER_TryOpenDriver32(libName
, lParam2
)))
319 if (DRIVER_GetLibName(lpDriverName
, lsn
, libName
, sizeof(libName
)) &&
320 (lpDrv
= DRIVER_TryOpenDriver32(libName
, lParam2
)))
323 /* now we will try a 16 bit driver (and add all the glue to make it work... which
324 * is located in our mmsystem implementation)
325 * so ensure, we can load our mmsystem, otherwise just fail
327 WINMM_CheckForMMSystem();
328 if (pFnOpenDriver16
&&
329 (lpDrv
= pFnOpenDriver16(lpDriverName
, lpSectionName
, lParam2
)))
331 if (DRIVER_AddToList(lpDrv
, 0, lParam2
)) goto the_end
;
332 HeapFree(GetProcessHeap(), 0, lpDrv
);
334 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName
), debugstr_a(lpSectionName
));
338 if (lpDrv
) TRACE("=> %08lx\n", (DWORD
)lpDrv
);
342 /**************************************************************************
343 * OpenDriver [WINMM.@]
346 HDRVR WINAPI
OpenDriverW(LPCWSTR lpDriverName
, LPCWSTR lpSectionName
, LPARAM lParam
)
355 len
= WideCharToMultiByte( CP_ACP
, 0, lpDriverName
, -1, NULL
, 0, NULL
, NULL
);
356 dn
= HeapAlloc( GetProcessHeap(), 0, len
);
358 WideCharToMultiByte( CP_ACP
, 0, lpDriverName
, -1, dn
, len
, NULL
, NULL
);
363 len
= WideCharToMultiByte( CP_ACP
, 0, lpSectionName
, -1, NULL
, 0, NULL
, NULL
);
364 sn
= HeapAlloc( GetProcessHeap(), 0, len
);
366 WideCharToMultiByte( CP_ACP
, 0, lpSectionName
, -1, sn
, len
, NULL
, NULL
);
369 ret
= OpenDriverA(dn
, sn
, lParam
);
371 if (dn
) HeapFree(GetProcessHeap(), 0, dn
);
372 if (sn
) HeapFree(GetProcessHeap(), 0, sn
);
376 /**************************************************************************
377 * CloseDriver [WINMM.@]
380 LRESULT WINAPI
CloseDriver(HDRVR hDrvr
, LPARAM lParam1
, LPARAM lParam2
)
384 TRACE("(%p, %08lX, %08lX);\n", hDrvr
, lParam1
, lParam2
);
386 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
)
388 if (lpDrv
->dwFlags
& WINE_GDF_16BIT
)
390 if (pFnCloseDriver16
)
391 pFnCloseDriver16(lpDrv
->d
.d16
.hDriver16
, lParam1
, lParam2
);
395 DRIVER_SendMessage(lpDrv
, DRV_CLOSE
, lParam1
, lParam2
);
396 lpDrv
->d
.d32
.dwDriverID
= 0;
398 if (DRIVER_RemoveFromList(lpDrv
)) {
399 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
))
401 LPWINE_DRIVER lpDrv0
;
403 /* if driver has an opened session instance, we have to close it too */
404 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->d
.d32
.hModule
, &lpDrv0
) == 1)
406 DRIVER_SendMessage(lpDrv0
, DRV_CLOSE
, 0L, 0L);
407 lpDrv0
->d
.d32
.dwDriverID
= 0;
408 DRIVER_RemoveFromList(lpDrv0
);
409 FreeLibrary(lpDrv0
->d
.d32
.hModule
);
410 HeapFree(GetProcessHeap(), 0, lpDrv0
);
412 FreeLibrary(lpDrv
->d
.d32
.hModule
);
414 HeapFree(GetProcessHeap(), 0, lpDrv
);
418 WARN("Failed to close driver\n");
422 /**************************************************************************
423 * GetDriverFlags [WINMM.@]
424 * [in] hDrvr handle to the driver
427 * 0x00000000 if hDrvr is an invalid handle
428 * 0x80000000 if hDrvr is a valid 32 bit driver
429 * 0x90000000 if hDrvr is a valid 16 bit driver
431 * native WINMM doesn't return those flags
432 * 0x80000000 for a valid 32 bit driver and that's it
433 * (I may have mixed up the two flags :-(
435 DWORD WINAPI
GetDriverFlags(HDRVR hDrvr
)
440 TRACE("(%p)\n", hDrvr
);
442 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
) {
443 ret
= WINE_GDF_EXIST
| lpDrv
->dwFlags
;
448 /**************************************************************************
449 * GetDriverModuleHandle [WINMM.@]
450 * DrvGetModuleHandle [WINMM.@]
452 HMODULE WINAPI
GetDriverModuleHandle(HDRVR hDrvr
)
457 TRACE("(%p);\n", hDrvr
);
459 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
) {
460 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
))
461 hModule
= lpDrv
->d
.d32
.hModule
;
463 TRACE("=> %p\n", hModule
);
467 /**************************************************************************
468 * DefDriverProc [WINMM.@]
469 * DrvDefDriverProc [WINMM.@]
471 LRESULT WINAPI
DefDriverProc(DWORD dwDriverIdentifier
, HDRVR hDrv
,
472 UINT Msg
, LPARAM lParam1
, LPARAM lParam2
)
488 /**************************************************************************
489 * DriverCallback [WINMM.@]
491 BOOL WINAPI
DriverCallback(DWORD dwCallBack
, UINT uFlags
, HDRVR hDev
,
492 UINT wMsg
, DWORD dwUser
, DWORD dwParam1
,
495 TRACE("(%08lX, %04X, %p, %04X, %08lX, %08lX, %08lX); !\n",
496 dwCallBack
, uFlags
, hDev
, wMsg
, dwUser
, dwParam1
, dwParam2
);
498 switch (uFlags
& DCB_TYPEMASK
) {
502 WARN("uFlags=%04X has null DCB value, but dwCallBack=%08lX is not null !\n", uFlags
, dwCallBack
);
505 TRACE("Window(%04lX) handle=%p!\n", dwCallBack
, hDev
);
506 PostMessageA((HWND
)dwCallBack
, wMsg
, (WPARAM
)hDev
, dwParam1
);
508 case DCB_TASK
: /* aka DCB_THREAD */
509 TRACE("Task(%04lx) !\n", dwCallBack
);
510 PostThreadMessageA(dwCallBack
, wMsg
, (WPARAM
)hDev
, dwParam1
);
513 TRACE("Function (32 bit) !\n");
514 ((LPDRVCALLBACK
)dwCallBack
)(hDev
, wMsg
, dwUser
, dwParam1
, dwParam2
);
517 TRACE("Event(%08lx) !\n", dwCallBack
);
518 SetEvent((HANDLE
)dwCallBack
);
520 case 6: /* I would dub it DCB_MMTHREADSIGNAL */
521 /* this is an undocumented DCB_ value used for mmThreads
522 * loword of dwCallBack contains the handle of the lpMMThd block
523 * which dwSignalCount has to be incremented
525 if (pFnGetMMThread16
)
527 WINE_MMTHREAD
* lpMMThd
= pFnGetMMThread16(LOWORD(dwCallBack
));
529 TRACE("mmThread (%04x, %p) !\n", LOWORD(dwCallBack
), lpMMThd
);
530 /* same as mmThreadSignal16 */
531 InterlockedIncrement(&lpMMThd
->dwSignalCount
);
532 SetEvent(lpMMThd
->hEvent
);
533 /* some other stuff on lpMMThd->hVxD */
538 /* this is an undocumented DCB_ value for... I don't know */
542 WARN("Unknown callback type %d\n", uFlags
& DCB_TYPEMASK
);
549 /******************************************************************
554 void DRIVER_UnloadAll(void)
557 LPWINE_DRIVER lpNextDrv
= NULL
;
560 for (lpDrv
= lpDrvItemList
; lpDrv
!= NULL
; lpDrv
= lpNextDrv
)
562 lpNextDrv
= lpDrv
->lpNextItem
;
563 CloseDriver((HDRVR
)lpDrv
, 0, 0);
566 TRACE("Unloaded %u drivers\n", count
);