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
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(driver
);
36 static LPWINE_DRIVER lpDrvItemList
/* = NULL */;
38 WINE_MMTHREAD
* (*pFnGetMMThread16
)(HANDLE16 h
) /* = NULL */;
39 LPWINE_DRIVER (*pFnOpenDriver16
)(LPCSTR
,LPCSTR
,LPARAM
) /* = NULL */;
40 LRESULT (*pFnCloseDriver16
)(HDRVR16
,LPARAM
,LPARAM
) /* = NULL */;
41 LRESULT (*pFnSendMessage16
)(HDRVR16
,UINT
,LPARAM
,LPARAM
) /* = NULL */;
43 /**************************************************************************
44 * DRIVER_GetNumberOfModuleRefs [internal]
46 * Returns the number of open drivers which share the same module.
48 static unsigned DRIVER_GetNumberOfModuleRefs(HMODULE hModule
, WINE_DRIVER
** found
)
53 if (found
) *found
= NULL
;
54 for (lpDrv
= lpDrvItemList
; lpDrv
; lpDrv
= lpDrv
->lpNextItem
)
56 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
) && lpDrv
->d
.d32
.hModule
== hModule
)
58 if (found
&& !*found
) *found
= lpDrv
;
65 /**************************************************************************
66 * DRIVER_FindFromHDrvr [internal]
68 * From a hDrvr being 32 bits, returns the WINE internal structure.
70 LPWINE_DRIVER
DRIVER_FindFromHDrvr(HDRVR hDrvr
)
72 LPWINE_DRIVER d
= (LPWINE_DRIVER
)hDrvr
;
74 if (hDrvr
&& HeapValidate(GetProcessHeap(), 0, d
) && d
->dwMagic
== WINE_DI_MAGIC
) {
80 /**************************************************************************
81 * DRIVER_SendMessage [internal]
83 static LRESULT
inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv
, UINT msg
,
84 LPARAM lParam1
, LPARAM lParam2
)
88 if (lpDrv
->dwFlags
& WINE_GDF_16BIT
) {
89 /* no need to check mmsystem presence: the driver must have been opened as a 16 bit one,
92 ret
= pFnSendMessage16(lpDrv
->d
.d16
.hDriver16
, msg
, lParam1
, lParam2
);
94 TRACE("Before call32 proc=%p drvrID=%08lx hDrv=%p wMsg=%04x p1=%08lx p2=%08lx\n",
95 lpDrv
->d
.d32
.lpDrvProc
, lpDrv
->d
.d32
.dwDriverID
, (HDRVR
)lpDrv
, msg
, lParam1
, lParam2
);
96 ret
= lpDrv
->d
.d32
.lpDrvProc(lpDrv
->d
.d32
.dwDriverID
, (HDRVR
)lpDrv
, msg
, lParam1
, lParam2
);
97 TRACE("After call32 proc=%p drvrID=%08lx hDrv=%p wMsg=%04x p1=%08lx p2=%08lx => %08lx\n",
98 lpDrv
->d
.d32
.lpDrvProc
, lpDrv
->d
.d32
.dwDriverID
, (HDRVR
)lpDrv
, msg
, lParam1
, lParam2
, ret
);
103 /**************************************************************************
104 * SendDriverMessage [WINMM.@]
105 * DrvSendMessage [WINMM.@]
107 LRESULT WINAPI
SendDriverMessage(HDRVR hDriver
, UINT msg
, LPARAM lParam1
,
113 TRACE("(%p, %04X, %08lX, %08lX)\n", hDriver
, msg
, lParam1
, lParam2
);
115 if ((lpDrv
= DRIVER_FindFromHDrvr(hDriver
)) != NULL
) {
116 retval
= DRIVER_SendMessage(lpDrv
, msg
, lParam1
, lParam2
);
118 WARN("Bad driver handle %p\n", hDriver
);
120 TRACE("retval = %ld\n", retval
);
125 /**************************************************************************
126 * DRIVER_RemoveFromList [internal]
128 * Generates all the logic to handle driver closure / deletion
129 * Removes a driver struct to the list of open drivers.
131 static BOOL
DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv
)
133 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
)) {
134 /* last of this driver in list ? */
135 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->d
.d32
.hModule
, NULL
) == 1) {
136 DRIVER_SendMessage(lpDrv
, DRV_DISABLE
, 0L, 0L);
137 DRIVER_SendMessage(lpDrv
, DRV_FREE
, 0L, 0L);
141 if (lpDrv
->lpPrevItem
)
142 lpDrv
->lpPrevItem
->lpNextItem
= lpDrv
->lpNextItem
;
144 lpDrvItemList
= lpDrv
->lpNextItem
;
145 if (lpDrv
->lpNextItem
)
146 lpDrv
->lpNextItem
->lpPrevItem
= lpDrv
->lpPrevItem
;
147 /* trash magic number */
148 lpDrv
->dwMagic
^= 0xa5a5a5a5;
153 /**************************************************************************
154 * DRIVER_AddToList [internal]
156 * Adds a driver struct to the list of open drivers.
157 * Generates all the logic to handle driver creation / open.
159 static BOOL
DRIVER_AddToList(LPWINE_DRIVER lpNewDrv
, LPARAM lParam1
, LPARAM lParam2
)
161 lpNewDrv
->dwMagic
= WINE_DI_MAGIC
;
162 /* First driver to be loaded for this module, need to load correctly the module */
163 if (!(lpNewDrv
->dwFlags
& WINE_GDF_16BIT
)) {
164 /* first of this driver in list ? */
165 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv
->d
.d32
.hModule
, NULL
) == 0) {
166 if (DRIVER_SendMessage(lpNewDrv
, DRV_LOAD
, 0L, 0L) != DRV_SUCCESS
) {
167 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD
)lpNewDrv
);
170 /* returned value is not checked */
171 DRIVER_SendMessage(lpNewDrv
, DRV_ENABLE
, 0L, 0L);
175 lpNewDrv
->lpNextItem
= NULL
;
176 if (lpDrvItemList
== NULL
) {
177 lpDrvItemList
= lpNewDrv
;
178 lpNewDrv
->lpPrevItem
= NULL
;
180 LPWINE_DRIVER lpDrv
= lpDrvItemList
; /* find end of list */
181 while (lpDrv
->lpNextItem
!= NULL
)
182 lpDrv
= lpDrv
->lpNextItem
;
184 lpDrv
->lpNextItem
= lpNewDrv
;
185 lpNewDrv
->lpPrevItem
= lpDrv
;
188 if (!(lpNewDrv
->dwFlags
& WINE_GDF_16BIT
)) {
189 /* Now just open a new instance of a driver on this module */
190 lpNewDrv
->d
.d32
.dwDriverID
= DRIVER_SendMessage(lpNewDrv
, DRV_OPEN
, lParam1
, lParam2
);
192 if (lpNewDrv
->d
.d32
.dwDriverID
== 0) {
193 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD
)lpNewDrv
);
194 DRIVER_RemoveFromList(lpNewDrv
);
201 /**************************************************************************
202 * DRIVER_GetLibName [internal]
205 BOOL
DRIVER_GetLibName(LPCSTR keyName
, LPCSTR sectName
, LPSTR buf
, int sz
)
207 /* should also do some registry diving */
208 return GetPrivateProfileStringA(sectName
, keyName
, "", buf
, sz
, "SYSTEM.INI");
211 /**************************************************************************
212 * DRIVER_TryOpenDriver32 [internal]
214 * Tries to load a 32 bit driver whose DLL's (module) name is fn
216 LPWINE_DRIVER
DRIVER_TryOpenDriver32(LPCSTR fn
, LPARAM lParam2
)
218 LPWINE_DRIVER lpDrv
= NULL
;
223 TRACE("(%s, %08lX);\n", debugstr_a(fn
), lParam2
);
225 if ((ptr
= strchr(fn
, ' ')) != NULL
) {
227 while (*ptr
== ' ') ptr
++;
228 if (*ptr
== '\0') ptr
= NULL
;
231 lpDrv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER
));
232 if (lpDrv
== NULL
) {cause
= "OOM"; goto exit
;}
234 if ((hModule
= LoadLibraryA(fn
)) == 0) {cause
= "Not a 32 bit lib"; goto exit
;}
236 lpDrv
->d
.d32
.lpDrvProc
= (DRIVERPROC
)GetProcAddress(hModule
, "DriverProc");
237 if (lpDrv
->d
.d32
.lpDrvProc
== NULL
) {cause
= "no DriverProc"; goto exit
;}
240 lpDrv
->d
.d32
.hModule
= hModule
;
241 lpDrv
->d
.d32
.dwDriverID
= 0;
243 /* Win32 installable drivers must support a two phase opening scheme:
244 * + first open with NULL as lParam2 (session instance),
245 * + then do a second open with the real non null lParam2)
247 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->d
.d32
.hModule
, NULL
) == 0 && lParam2
)
251 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, 0L))
253 cause
= "load0 failed";
256 ret
= DRIVER_TryOpenDriver32(fn
, lParam2
);
259 CloseDriver((HDRVR
)lpDrv
, 0L, 0L);
260 cause
= "load1 failed";
266 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, lParam2
))
267 {cause
= "load failed"; goto exit
;}
269 TRACE("=> %p\n", lpDrv
);
272 FreeLibrary(hModule
);
273 HeapFree(GetProcessHeap(), 0, lpDrv
);
274 TRACE("Unable to load 32 bit module %s: %s\n", debugstr_a(fn
), cause
);
278 /**************************************************************************
279 * OpenDriverA [WINMM.@]
281 * (0,1,DRV_LOAD ,0 ,0)
282 * (0,1,DRV_ENABLE,0 ,0)
283 * (0,1,DRV_OPEN ,buf[256],0)
285 HDRVR WINAPI
OpenDriverA(LPCSTR lpDriverName
, LPCSTR lpSectionName
, LPARAM lParam2
)
287 LPWINE_DRIVER lpDrv
= NULL
;
289 LPCSTR lsn
= lpSectionName
;
291 TRACE("(%s, %s, 0x%08lx);\n", debugstr_a(lpDriverName
), debugstr_a(lpSectionName
), lParam2
);
294 lstrcpynA(libName
, lpDriverName
, sizeof(libName
));
296 if ((lpDrv
= DRIVER_TryOpenDriver32(libName
, lParam2
)))
300 if (DRIVER_GetLibName(lpDriverName
, lsn
, libName
, sizeof(libName
)) &&
301 (lpDrv
= DRIVER_TryOpenDriver32(libName
, lParam2
)))
304 /* now we will try a 16 bit driver (and add all the glue to make it work... which
305 * is located in our mmsystem implementation)
306 * so ensure, we can load our mmsystem, otherwise just fail
308 WINMM_CheckForMMSystem();
309 if (pFnOpenDriver16
&&
310 (lpDrv
= pFnOpenDriver16(lpDriverName
, lpSectionName
, lParam2
)))
312 if (DRIVER_AddToList(lpDrv
, 0, lParam2
)) goto the_end
;
313 HeapFree(GetProcessHeap(), 0, lpDrv
);
315 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName
), debugstr_a(lpSectionName
));
319 if (lpDrv
) TRACE("=> %08lx\n", (DWORD
)lpDrv
);
323 /**************************************************************************
324 * OpenDriver [WINMM.@]
327 HDRVR WINAPI
OpenDriverW(LPCWSTR lpDriverName
, LPCWSTR lpSectionName
, LPARAM lParam
)
329 LPSTR dn
= HEAP_strdupWtoA(GetProcessHeap(), 0, lpDriverName
);
330 LPSTR sn
= HEAP_strdupWtoA(GetProcessHeap(), 0, lpSectionName
);
331 HDRVR ret
= OpenDriverA(dn
, sn
, lParam
);
333 if (dn
) HeapFree(GetProcessHeap(), 0, dn
);
334 if (sn
) HeapFree(GetProcessHeap(), 0, sn
);
338 /**************************************************************************
339 * CloseDriver [WINMM.@]
342 LRESULT WINAPI
CloseDriver(HDRVR hDrvr
, LPARAM lParam1
, LPARAM lParam2
)
346 TRACE("(%p, %08lX, %08lX);\n", hDrvr
, lParam1
, lParam2
);
348 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
)
350 if (lpDrv
->dwFlags
& WINE_GDF_16BIT
)
352 if (pFnCloseDriver16
)
353 pFnCloseDriver16(lpDrv
->d
.d16
.hDriver16
, lParam1
, lParam2
);
357 DRIVER_SendMessage(lpDrv
, DRV_CLOSE
, lParam1
, lParam2
);
358 lpDrv
->d
.d32
.dwDriverID
= 0;
360 if (DRIVER_RemoveFromList(lpDrv
)) {
361 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
))
363 LPWINE_DRIVER lpDrv0
;
365 /* if driver has an opened session instance, we have to close it too */
366 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->d
.d32
.hModule
, &lpDrv0
) == 1)
368 DRIVER_SendMessage(lpDrv0
, DRV_CLOSE
, 0L, 0L);
369 lpDrv0
->d
.d32
.dwDriverID
= 0;
370 DRIVER_RemoveFromList(lpDrv0
);
371 FreeLibrary(lpDrv
->d
.d32
.hModule
);
372 HeapFree(GetProcessHeap(), 0, lpDrv0
);
374 FreeLibrary(lpDrv
->d
.d32
.hModule
);
376 HeapFree(GetProcessHeap(), 0, lpDrv
);
380 WARN("Failed to close driver\n");
384 /**************************************************************************
385 * GetDriverFlags [WINMM.@]
386 * [in] hDrvr handle to the driver
389 * 0x00000000 if hDrvr is an invalid handle
390 * 0x80000000 if hDrvr is a valid 32 bit driver
391 * 0x90000000 if hDrvr is a valid 16 bit driver
393 * native WINMM doesn't return those flags
394 * 0x80000000 for a valid 32 bit driver and that's it
395 * (I may have mixed up the two flags :-(
397 DWORD WINAPI
GetDriverFlags(HDRVR hDrvr
)
402 TRACE("(%p)\n", hDrvr
);
404 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
) {
405 ret
= WINE_GDF_EXIST
| lpDrv
->dwFlags
;
410 /**************************************************************************
411 * GetDriverModuleHandle [WINMM.@]
412 * DrvGetModuleHandle [WINMM.@]
414 HMODULE WINAPI
GetDriverModuleHandle(HDRVR hDrvr
)
419 TRACE("(%p);\n", hDrvr
);
421 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
) {
422 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
))
423 hModule
= lpDrv
->d
.d32
.hModule
;
425 TRACE("=> %p\n", hModule
);
429 /**************************************************************************
430 * DefDriverProc [WINMM.@]
431 * DrvDefDriverProc [WINMM.@]
433 LRESULT WINAPI
DefDriverProc(DWORD dwDriverIdentifier
, HDRVR hDrv
,
434 UINT Msg
, LPARAM lParam1
, LPARAM lParam2
)
450 /**************************************************************************
451 * DriverCallback [WINMM.@]
453 BOOL WINAPI
DriverCallback(DWORD dwCallBack
, UINT uFlags
, HDRVR hDev
,
454 UINT wMsg
, DWORD dwUser
, DWORD dwParam1
,
457 TRACE("(%08lX, %04X, %p, %04X, %08lX, %08lX, %08lX); !\n",
458 dwCallBack
, uFlags
, hDev
, wMsg
, dwUser
, dwParam1
, dwParam2
);
460 switch (uFlags
& DCB_TYPEMASK
) {
464 WARN("uFlags=%04X has null DCB value, but dwCallBack=%08lX is not null !\n", uFlags
, dwCallBack
);
467 TRACE("Window(%04lX) handle=%p!\n", dwCallBack
, hDev
);
468 PostMessageA((HWND
)dwCallBack
, wMsg
, (WPARAM
)hDev
, dwParam1
);
470 case DCB_TASK
: /* aka DCB_THREAD */
471 TRACE("Task(%04lx) !\n", dwCallBack
);
472 PostThreadMessageA(dwCallBack
, wMsg
, (WPARAM
)hDev
, dwParam1
);
475 TRACE("Function (32 bit) !\n");
476 ((LPDRVCALLBACK
)dwCallBack
)(hDev
, wMsg
, dwUser
, dwParam1
, dwParam2
);
479 TRACE("Event(%08lx) !\n", dwCallBack
);
480 SetEvent((HANDLE
)dwCallBack
);
482 case 6: /* I would dub it DCB_MMTHREADSIGNAL */
483 /* this is an undocumented DCB_ value used for mmThreads
484 * loword of dwCallBack contains the handle of the lpMMThd block
485 * which dwSignalCount has to be incremented
487 if (pFnGetMMThread16
)
489 WINE_MMTHREAD
* lpMMThd
= pFnGetMMThread16(LOWORD(dwCallBack
));
491 TRACE("mmThread (%04x, %p) !\n", LOWORD(dwCallBack
), lpMMThd
);
492 /* same as mmThreadSignal16 */
493 InterlockedIncrement(&lpMMThd
->dwSignalCount
);
494 SetEvent(lpMMThd
->hEvent
);
495 /* some other stuff on lpMMThd->hVxD */
500 /* this is an undocumented DCB_ value for... I don't know */
504 WARN("Unknown callback type %d\n", uFlags
& DCB_TYPEMASK
);
511 /******************************************************************
516 void DRIVER_UnloadAll(void)
519 LPWINE_DRIVER lpNextDrv
= NULL
;
522 for (lpDrv
= lpDrvItemList
; lpDrv
!= NULL
; lpDrv
= lpNextDrv
)
524 lpNextDrv
= lpDrv
->lpNextItem
;
525 CloseDriver((HDRVR
)lpDrv
, 0, 0);
528 TRACE("Unloaded %u drivers\n", count
);