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
30 #include "wine/winbase16.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(driver
);
35 static LPWINE_DRIVER lpDrvItemList
= NULL
;
37 /**************************************************************************
38 * DRIVER_GetNumberOfModuleRefs [internal]
40 * Returns the number of open drivers which share the same module.
42 static unsigned DRIVER_GetNumberOfModuleRefs(HMODULE hModule
, WINE_DRIVER
** found
)
47 if (found
) *found
= NULL
;
48 for (lpDrv
= lpDrvItemList
; lpDrv
; lpDrv
= lpDrv
->lpNextItem
)
50 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
) && lpDrv
->d
.d32
.hModule
== hModule
)
52 if (found
&& !*found
) *found
= lpDrv
;
59 /**************************************************************************
60 * DRIVER_FindFromHDrvr [internal]
62 * From a hDrvr being 32 bits, returns the WINE internal structure.
64 LPWINE_DRIVER
DRIVER_FindFromHDrvr(HDRVR hDrvr
)
66 LPWINE_DRIVER d
= (LPWINE_DRIVER
)hDrvr
;
68 if (hDrvr
&& HeapValidate(GetProcessHeap(), 0, d
) && d
->dwMagic
== WINE_DI_MAGIC
) {
74 /**************************************************************************
75 * DRIVER_SendMessage [internal]
77 static LRESULT
inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv
, UINT msg
,
78 LPARAM lParam1
, LPARAM lParam2
)
80 if (lpDrv
->dwFlags
& WINE_GDF_16BIT
) {
83 TRACE("Before sdm16 call hDrv=%04x wMsg=%04x p1=%08lx p2=%08lx\n",
84 lpDrv
->d
.d16
.hDriver16
, msg
, lParam1
, lParam2
);
86 if ((map
= DRIVER_MapMsg32To16(msg
, &lParam1
, &lParam2
)) >= 0) {
87 ret
= SendDriverMessage16(lpDrv
->d
.d16
.hDriver16
, msg
, lParam1
, lParam2
);
89 DRIVER_UnMapMsg32To16(msg
, lParam1
, lParam2
);
95 TRACE("Before func32 call proc=%p driverID=%08lx hDrv=%08x wMsg=%04x p1=%08lx p2=%08lx\n",
96 lpDrv
->d
.d32
.lpDrvProc
, lpDrv
->d
.d32
.dwDriverID
, (HDRVR
)lpDrv
, msg
, lParam1
, lParam2
);
97 return lpDrv
->d
.d32
.lpDrvProc(lpDrv
->d
.d32
.dwDriverID
, (HDRVR
)lpDrv
, msg
, lParam1
, lParam2
);
100 /**************************************************************************
101 * SendDriverMessage [WINMM.@]
102 * DrvSendMessage [WINMM.@]
104 LRESULT WINAPI
SendDriverMessage(HDRVR hDriver
, UINT msg
, LPARAM lParam1
,
110 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver
, msg
, lParam1
, lParam2
);
112 if ((lpDrv
= DRIVER_FindFromHDrvr(hDriver
)) != NULL
) {
113 retval
= DRIVER_SendMessage(lpDrv
, msg
, lParam1
, lParam2
);
115 WARN("Bad driver handle %u\n", hDriver
);
117 TRACE("retval = %ld\n", retval
);
122 /**************************************************************************
123 * DRIVER_RemoveFromList [internal]
125 * Generates all the logic to handle driver closure / deletion
126 * Removes a driver struct to the list of open drivers.
128 static BOOL
DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv
)
130 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
)) {
131 /* last of this driver in list ? */
132 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->d
.d32
.hModule
, NULL
) == 1) {
133 DRIVER_SendMessage(lpDrv
, DRV_DISABLE
, 0L, 0L);
134 DRIVER_SendMessage(lpDrv
, DRV_FREE
, 0L, 0L);
138 if (lpDrv
->lpPrevItem
)
139 lpDrv
->lpPrevItem
->lpNextItem
= lpDrv
->lpNextItem
;
141 lpDrvItemList
= lpDrv
->lpNextItem
;
142 if (lpDrv
->lpNextItem
)
143 lpDrv
->lpNextItem
->lpPrevItem
= lpDrv
->lpPrevItem
;
144 /* trash magic number */
145 lpDrv
->dwMagic
^= 0xa5a5a5a5;
150 /**************************************************************************
151 * DRIVER_AddToList [internal]
153 * Adds a driver struct to the list of open drivers.
154 * Generates all the logic to handle driver creation / open.
156 static BOOL
DRIVER_AddToList(LPWINE_DRIVER lpNewDrv
, LPARAM lParam1
, LPARAM lParam2
)
158 lpNewDrv
->dwMagic
= WINE_DI_MAGIC
;
159 /* First driver to be loaded for this module, need to load correctly the module */
160 if (!(lpNewDrv
->dwFlags
& WINE_GDF_16BIT
)) {
161 /* first of this driver in list ? */
162 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv
->d
.d32
.hModule
, NULL
) == 0) {
163 if (DRIVER_SendMessage(lpNewDrv
, DRV_LOAD
, 0L, 0L) != DRV_SUCCESS
) {
164 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD
)lpNewDrv
);
167 /* returned value is not checked */
168 DRIVER_SendMessage(lpNewDrv
, DRV_ENABLE
, 0L, 0L);
172 lpNewDrv
->lpNextItem
= NULL
;
173 if (lpDrvItemList
== NULL
) {
174 lpDrvItemList
= lpNewDrv
;
175 lpNewDrv
->lpPrevItem
= NULL
;
177 LPWINE_DRIVER lpDrv
= lpDrvItemList
; /* find end of list */
178 while (lpDrv
->lpNextItem
!= NULL
)
179 lpDrv
= lpDrv
->lpNextItem
;
181 lpDrv
->lpNextItem
= lpNewDrv
;
182 lpNewDrv
->lpPrevItem
= lpDrv
;
185 if (!(lpNewDrv
->dwFlags
& WINE_GDF_16BIT
)) {
186 /* Now just open a new instance of a driver on this module */
187 lpNewDrv
->d
.d32
.dwDriverID
= DRIVER_SendMessage(lpNewDrv
, DRV_OPEN
, lParam1
, lParam2
);
189 if (lpNewDrv
->d
.d32
.dwDriverID
== 0) {
190 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD
)lpNewDrv
);
191 DRIVER_RemoveFromList(lpNewDrv
);
198 /**************************************************************************
199 * DRIVER_GetLibName [internal]
202 BOOL
DRIVER_GetLibName(LPCSTR keyName
, LPCSTR sectName
, LPSTR buf
, int sz
)
204 /* should also do some registry diving */
205 return GetPrivateProfileStringA(sectName
, keyName
, "", buf
, sz
, "SYSTEM.INI");
208 /**************************************************************************
209 * DRIVER_TryOpenDriver32 [internal]
211 * Tries to load a 32 bit driver whose DLL's (module) name is fn
213 LPWINE_DRIVER
DRIVER_TryOpenDriver32(LPCSTR fn
, LPARAM lParam2
)
215 LPWINE_DRIVER lpDrv
= NULL
;
220 TRACE("(%s, %08lX);\n", debugstr_a(fn
), lParam2
);
222 if ((ptr
= strchr(fn
, ' ')) != NULL
) {
224 while (*ptr
== ' ') ptr
++;
225 if (*ptr
== '\0') ptr
= NULL
;
228 lpDrv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER
));
229 if (lpDrv
== NULL
) {cause
= "OOM"; goto exit
;}
231 if ((hModule
= LoadLibraryA(fn
)) == 0) {cause
= "Not a 32 bit lib"; goto exit
;}
233 lpDrv
->d
.d32
.lpDrvProc
= (DRIVERPROC
)GetProcAddress(hModule
, "DriverProc");
234 if (lpDrv
->d
.d32
.lpDrvProc
== NULL
) {cause
= "no DriverProc"; goto exit
;}
237 lpDrv
->d
.d32
.hModule
= hModule
;
238 lpDrv
->d
.d32
.dwDriverID
= 0;
240 /* Win32 installable drivers must support a two phase opening scheme:
241 * + first open with NULL as lParam2 (session instance),
242 * + then do a second open with the real non null lParam2)
244 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->d
.d32
.hModule
, NULL
) == 0 && lParam2
)
248 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, 0L))
250 cause
= "load0 failed";
253 ret
= DRIVER_TryOpenDriver32(fn
, lParam2
);
256 CloseDriver((HDRVR
)lpDrv
, 0L, 0L);
257 cause
= "load1 failed";
263 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, lParam2
))
264 {cause
= "load failed"; goto exit
;}
266 TRACE("=> %p\n", lpDrv
);
269 FreeLibrary(hModule
);
270 HeapFree(GetProcessHeap(), 0, lpDrv
);
271 TRACE("Unable to load 32 bit module %s: %s\n", debugstr_a(fn
), cause
);
275 /**************************************************************************
276 * DRIVER_TryOpenDriver16 [internal]
278 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
280 static LPWINE_DRIVER
DRIVER_TryOpenDriver16(LPCSTR fn
, LPCSTR sn
, LPARAM lParam2
)
282 LPWINE_DRIVER lpDrv
= NULL
;
285 TRACE("(%s, %08lX);\n", debugstr_a(sn
), lParam2
);
287 lpDrv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER
));
288 if (lpDrv
== NULL
) {cause
= "OOM"; goto exit
;}
290 /* FIXME: shall we do some black magic here on sn ?
291 * drivers32 => drivers
295 lpDrv
->d
.d16
.hDriver16
= OpenDriver16(fn
, sn
, lParam2
);
296 if (lpDrv
->d
.d16
.hDriver16
== 0) {cause
= "Not a 16 bit driver"; goto exit
;}
297 lpDrv
->dwFlags
= WINE_GDF_16BIT
;
299 if (!DRIVER_AddToList(lpDrv
, 0, lParam2
)) {cause
= "load failed"; goto exit
;}
301 TRACE("=> %p\n", lpDrv
);
304 HeapFree(GetProcessHeap(), 0, lpDrv
);
305 TRACE("Unable to load 32 bit module %s: %s\n", debugstr_a(fn
), cause
);
309 /**************************************************************************
310 * OpenDriverA [WINMM.@]
312 * (0,1,DRV_LOAD ,0 ,0)
313 * (0,1,DRV_ENABLE,0 ,0)
314 * (0,1,DRV_OPEN ,buf[256],0)
316 HDRVR WINAPI
OpenDriverA(LPCSTR lpDriverName
, LPCSTR lpSectionName
, LPARAM lParam2
)
318 LPWINE_DRIVER lpDrv
= NULL
;
320 LPCSTR lsn
= lpSectionName
;
322 TRACE("(%s, %s, 0x%08lx);\n", debugstr_a(lpDriverName
), debugstr_a(lpSectionName
), lParam2
);
325 lstrcpynA(libName
, lpDriverName
, sizeof(libName
));
327 if ((lpDrv
= DRIVER_TryOpenDriver32(libName
, lParam2
)))
331 if (DRIVER_GetLibName(lpDriverName
, lsn
, libName
, sizeof(libName
)) &&
332 (lpDrv
= DRIVER_TryOpenDriver32(libName
, lParam2
)))
335 if (!(lpDrv
= DRIVER_TryOpenDriver16(lpDriverName
, lpSectionName
, lParam2
)))
336 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
)
348 LPSTR dn
= HEAP_strdupWtoA(GetProcessHeap(), 0, lpDriverName
);
349 LPSTR sn
= HEAP_strdupWtoA(GetProcessHeap(), 0, lpSectionName
);
350 HDRVR ret
= OpenDriverA(dn
, sn
, lParam
);
352 if (dn
) HeapFree(GetProcessHeap(), 0, dn
);
353 if (sn
) HeapFree(GetProcessHeap(), 0, sn
);
357 /**************************************************************************
358 * CloseDriver [WINMM.@]
361 LRESULT WINAPI
CloseDriver(HDRVR hDrvr
, LPARAM lParam1
, LPARAM lParam2
)
365 TRACE("(%04x, %08lX, %08lX);\n", hDrvr
, lParam1
, lParam2
);
367 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
)
369 if (lpDrv
->dwFlags
& WINE_GDF_16BIT
)
370 CloseDriver16(lpDrv
->d
.d16
.hDriver16
, lParam1
, lParam2
);
373 DRIVER_SendMessage(lpDrv
, DRV_CLOSE
, lParam1
, lParam2
);
374 lpDrv
->d
.d32
.dwDriverID
= 0;
376 if (DRIVER_RemoveFromList(lpDrv
)) {
377 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
))
379 LPWINE_DRIVER lpDrv0
;
381 /* if driver has an opened session instance, we have to close it too */
382 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->d
.d32
.hModule
, &lpDrv0
) == 1)
384 DRIVER_SendMessage(lpDrv0
, DRV_CLOSE
, 0L, 0L);
385 lpDrv0
->d
.d32
.dwDriverID
= 0;
386 DRIVER_RemoveFromList(lpDrv0
);
387 FreeLibrary(lpDrv
->d
.d32
.hModule
);
388 HeapFree(GetProcessHeap(), 0, lpDrv0
);
390 FreeLibrary(lpDrv
->d
.d32
.hModule
);
392 HeapFree(GetProcessHeap(), 0, lpDrv
);
396 WARN("Failed to close driver\n");
400 /**************************************************************************
401 * GetDriverFlags [WINMM.@]
402 * [in] hDrvr handle to the driver
405 * 0x00000000 if hDrvr is an invalid handle
406 * 0x80000000 if hDrvr is a valid 32 bit driver
407 * 0x90000000 if hDrvr is a valid 16 bit driver
409 * native WINMM doesn't return those flags
410 * 0x80000000 for a valid 32 bit driver and that's it
411 * (I may have mixed up the two flags :-(
413 DWORD WINAPI
GetDriverFlags(HDRVR hDrvr
)
418 TRACE("(%04x)\n", hDrvr
);
420 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
) {
421 ret
= WINE_GDF_EXIST
| lpDrv
->dwFlags
;
426 /**************************************************************************
427 * GetDriverModuleHandle [WINMM.@]
428 * DrvGetModuleHandle [WINMM.@]
430 HMODULE WINAPI
GetDriverModuleHandle(HDRVR hDrvr
)
435 TRACE("(%04x);\n", hDrvr
);
437 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
) {
438 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
))
439 hModule
= lpDrv
->d
.d32
.hModule
;
441 TRACE("=> %04x\n", hModule
);
445 /**************************************************************************
446 * DefDriverProc [WINMM.@]
447 * DrvDefDriverProc [WINMM.@]
449 LRESULT WINAPI
DefDriverProc(DWORD dwDriverIdentifier
, HDRVR hDrv
,
450 UINT Msg
, LPARAM lParam1
, LPARAM lParam2
)