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_MapMsg32To16 [internal]
77 * Map a 32 bit driver message to a 16 bit driver message.
78 * 1 : ok, some memory allocated, need to call DRIVER_UnMapMsg32To16
79 * 0 : ok, no memory allocated
80 * -1 : ko, unknown message
81 * -2 : ko, memory problem
83 static int DRIVER_MapMsg32To16(WORD wMsg
, DWORD
* lParam1
, DWORD
* lParam2
)
92 case DRV_QUERYCONFIGURE
:
95 case DRV_EXITAPPLICATION
:
97 case DRV_CLOSE
: /* should be 0/0 */
98 case DRV_OPEN
: /* pass through */
99 /* lParam1 and lParam2 are not used */
105 /* lParam1 is a handle to a window (conf) or to a driver (inst) or not used,
106 * lParam2 is a pointer to DRVCONFIGINFO
109 LPDRVCONFIGINFO16 dci16
= HeapAlloc( GetProcessHeap(), 0, sizeof(*dci16
) );
110 LPDRVCONFIGINFO dci32
= (LPDRVCONFIGINFO
)(*lParam2
);
115 dci16
->dwDCISize
= sizeof(DRVCONFIGINFO16
);
117 if ((str1
= HEAP_strdupWtoA(GetProcessHeap(), 0, dci32
->lpszDCISectionName
)) != NULL
)
119 dci16
->lpszDCISectionName
= MapLS( str1
);
123 if ((str1
= HEAP_strdupWtoA(GetProcessHeap(), 0, dci32
->lpszDCIAliasName
)) != NULL
)
125 dci16
->lpszDCIAliasName
= MapLS( str1
);
132 *lParam2
= MapLS( dci16
);
139 if (!((wMsg
>= 0x800 && wMsg
< 0x900) || (wMsg
>= 0x4000 && wMsg
< 0x4100))) {
140 FIXME("Unknown message 0x%04x\n", wMsg
);
147 /**************************************************************************
148 * DRIVER_UnMapMsg32To16 [internal]
150 * UnMap a 32 bit driver message to a 16 bit driver message.
153 * -2 : ko, memory problem
155 static int DRIVER_UnMapMsg32To16(WORD wMsg
, DWORD lParam1
, DWORD lParam2
)
164 case DRV_QUERYCONFIGURE
:
166 case DRV_EXITSESSION
:
167 case DRV_EXITAPPLICATION
:
171 /* lParam1 and lParam2 are not used */
175 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO, lParam2 */
177 LPDRVCONFIGINFO16 dci16
= MapSL(lParam2
);
178 HeapFree( GetProcessHeap(), 0, MapSL(dci16
->lpszDCISectionName
) );
179 HeapFree( GetProcessHeap(), 0, MapSL(dci16
->lpszDCIAliasName
) );
181 UnMapLS( dci16
->lpszDCISectionName
);
182 UnMapLS( dci16
->lpszDCIAliasName
);
183 HeapFree( GetProcessHeap(), 0, dci16
);
188 if (!((wMsg
>= 0x800 && wMsg
< 0x900) || (wMsg
>= 0x4000 && wMsg
< 0x4100))) {
189 FIXME("Unknown message 0x%04x\n", wMsg
);
196 /**************************************************************************
197 * DRIVER_SendMessage [internal]
199 static LRESULT
inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv
, UINT msg
,
200 LPARAM lParam1
, LPARAM lParam2
)
202 if (lpDrv
->dwFlags
& WINE_GDF_16BIT
) {
205 TRACE("Before sdm16 call hDrv=%04x wMsg=%04x p1=%08lx p2=%08lx\n",
206 lpDrv
->d
.d16
.hDriver16
, msg
, lParam1
, lParam2
);
208 if ((map
= DRIVER_MapMsg32To16(msg
, &lParam1
, &lParam2
)) >= 0) {
209 ret
= SendDriverMessage16(lpDrv
->d
.d16
.hDriver16
, msg
, lParam1
, lParam2
);
211 DRIVER_UnMapMsg32To16(msg
, lParam1
, lParam2
);
217 TRACE("Before func32 call proc=%p driverID=%08lx hDrv=%08x wMsg=%04x p1=%08lx p2=%08lx\n",
218 lpDrv
->d
.d32
.lpDrvProc
, lpDrv
->d
.d32
.dwDriverID
, (HDRVR
)lpDrv
, msg
, lParam1
, lParam2
);
219 return lpDrv
->d
.d32
.lpDrvProc(lpDrv
->d
.d32
.dwDriverID
, (HDRVR
)lpDrv
, msg
, lParam1
, lParam2
);
222 /**************************************************************************
223 * SendDriverMessage [WINMM.@]
224 * DrvSendMessage [WINMM.@]
226 LRESULT WINAPI
SendDriverMessage(HDRVR hDriver
, UINT msg
, LPARAM lParam1
,
232 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver
, msg
, lParam1
, lParam2
);
234 if ((lpDrv
= DRIVER_FindFromHDrvr(hDriver
)) != NULL
) {
235 retval
= DRIVER_SendMessage(lpDrv
, msg
, lParam1
, lParam2
);
237 WARN("Bad driver handle %u\n", hDriver
);
239 TRACE("retval = %ld\n", retval
);
244 /**************************************************************************
245 * DRIVER_RemoveFromList [internal]
247 * Generates all the logic to handle driver closure / deletion
248 * Removes a driver struct to the list of open drivers.
250 static BOOL
DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv
)
252 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
)) {
253 /* last of this driver in list ? */
254 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->d
.d32
.hModule
, NULL
) == 1) {
255 DRIVER_SendMessage(lpDrv
, DRV_DISABLE
, 0L, 0L);
256 DRIVER_SendMessage(lpDrv
, DRV_FREE
, 0L, 0L);
260 if (lpDrv
->lpPrevItem
)
261 lpDrv
->lpPrevItem
->lpNextItem
= lpDrv
->lpNextItem
;
263 lpDrvItemList
= lpDrv
->lpNextItem
;
264 if (lpDrv
->lpNextItem
)
265 lpDrv
->lpNextItem
->lpPrevItem
= lpDrv
->lpPrevItem
;
266 /* trash magic number */
267 lpDrv
->dwMagic
^= 0xa5a5a5a5;
272 /**************************************************************************
273 * DRIVER_AddToList [internal]
275 * Adds a driver struct to the list of open drivers.
276 * Generates all the logic to handle driver creation / open.
278 static BOOL
DRIVER_AddToList(LPWINE_DRIVER lpNewDrv
, LPARAM lParam1
, LPARAM lParam2
)
280 lpNewDrv
->dwMagic
= WINE_DI_MAGIC
;
281 /* First driver to be loaded for this module, need to load correctly the module */
282 if (!(lpNewDrv
->dwFlags
& WINE_GDF_16BIT
)) {
283 /* first of this driver in list ? */
284 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv
->d
.d32
.hModule
, NULL
) == 0) {
285 if (DRIVER_SendMessage(lpNewDrv
, DRV_LOAD
, 0L, 0L) != DRV_SUCCESS
) {
286 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD
)lpNewDrv
);
289 /* returned value is not checked */
290 DRIVER_SendMessage(lpNewDrv
, DRV_ENABLE
, 0L, 0L);
294 lpNewDrv
->lpNextItem
= NULL
;
295 if (lpDrvItemList
== NULL
) {
296 lpDrvItemList
= lpNewDrv
;
297 lpNewDrv
->lpPrevItem
= NULL
;
299 LPWINE_DRIVER lpDrv
= lpDrvItemList
; /* find end of list */
300 while (lpDrv
->lpNextItem
!= NULL
)
301 lpDrv
= lpDrv
->lpNextItem
;
303 lpDrv
->lpNextItem
= lpNewDrv
;
304 lpNewDrv
->lpPrevItem
= lpDrv
;
307 if (!(lpNewDrv
->dwFlags
& WINE_GDF_16BIT
)) {
308 /* Now just open a new instance of a driver on this module */
309 lpNewDrv
->d
.d32
.dwDriverID
= DRIVER_SendMessage(lpNewDrv
, DRV_OPEN
, lParam1
, lParam2
);
311 if (lpNewDrv
->d
.d32
.dwDriverID
== 0) {
312 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD
)lpNewDrv
);
313 DRIVER_RemoveFromList(lpNewDrv
);
320 /**************************************************************************
321 * DRIVER_GetLibName [internal]
324 BOOL
DRIVER_GetLibName(LPCSTR keyName
, LPCSTR sectName
, LPSTR buf
, int sz
)
326 /* should also do some registry diving */
327 return GetPrivateProfileStringA(sectName
, keyName
, "", buf
, sz
, "SYSTEM.INI");
330 /**************************************************************************
331 * DRIVER_TryOpenDriver32 [internal]
333 * Tries to load a 32 bit driver whose DLL's (module) name is fn
335 LPWINE_DRIVER
DRIVER_TryOpenDriver32(LPCSTR fn
, LPARAM lParam2
)
337 LPWINE_DRIVER lpDrv
= NULL
;
342 TRACE("(%s, %08lX);\n", debugstr_a(fn
), lParam2
);
344 if ((ptr
= strchr(fn
, ' ')) != NULL
) {
346 while (*ptr
== ' ') ptr
++;
347 if (*ptr
== '\0') ptr
= NULL
;
350 lpDrv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER
));
351 if (lpDrv
== NULL
) {cause
= "OOM"; goto exit
;}
353 if ((hModule
= LoadLibraryA(fn
)) == 0) {cause
= "Not a 32 bit lib"; goto exit
;}
355 lpDrv
->d
.d32
.lpDrvProc
= (DRIVERPROC
)GetProcAddress(hModule
, "DriverProc");
356 if (lpDrv
->d
.d32
.lpDrvProc
== NULL
) {cause
= "no DriverProc"; goto exit
;}
359 lpDrv
->d
.d32
.hModule
= hModule
;
360 lpDrv
->d
.d32
.dwDriverID
= 0;
362 /* Win32 installable drivers must support a two phase opening scheme:
363 * + first open with NULL as lParam2 (session instance),
364 * + then do a second open with the real non null lParam2)
366 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->d
.d32
.hModule
, NULL
) == 0 && lParam2
)
370 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, 0L))
372 cause
= "load0 failed";
375 ret
= DRIVER_TryOpenDriver32(fn
, lParam2
);
378 CloseDriver((HDRVR
)lpDrv
, 0L, 0L);
379 cause
= "load1 failed";
385 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, lParam2
))
386 {cause
= "load failed"; goto exit
;}
388 TRACE("=> %p\n", lpDrv
);
391 FreeLibrary(hModule
);
392 HeapFree(GetProcessHeap(), 0, lpDrv
);
393 TRACE("Unable to load 32 bit module %s: %s\n", debugstr_a(fn
), cause
);
397 /**************************************************************************
398 * DRIVER_TryOpenDriver16 [internal]
400 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
402 static LPWINE_DRIVER
DRIVER_TryOpenDriver16(LPCSTR fn
, LPCSTR sn
, LPARAM lParam2
)
404 LPWINE_DRIVER lpDrv
= NULL
;
407 TRACE("(%s, %08lX);\n", debugstr_a(sn
), lParam2
);
409 lpDrv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER
));
410 if (lpDrv
== NULL
) {cause
= "OOM"; goto exit
;}
412 /* FIXME: shall we do some black magic here on sn ?
413 * drivers32 => drivers
417 lpDrv
->d
.d16
.hDriver16
= OpenDriver16(fn
, sn
, lParam2
);
418 if (lpDrv
->d
.d16
.hDriver16
== 0) {cause
= "Not a 16 bit driver"; goto exit
;}
419 lpDrv
->dwFlags
= WINE_GDF_16BIT
;
421 if (!DRIVER_AddToList(lpDrv
, 0, lParam2
)) {cause
= "load failed"; goto exit
;}
423 TRACE("=> %p\n", lpDrv
);
426 HeapFree(GetProcessHeap(), 0, lpDrv
);
427 TRACE("Unable to load 32 bit module %s: %s\n", debugstr_a(fn
), cause
);
431 /**************************************************************************
432 * OpenDriverA [WINMM.@]
434 * (0,1,DRV_LOAD ,0 ,0)
435 * (0,1,DRV_ENABLE,0 ,0)
436 * (0,1,DRV_OPEN ,buf[256],0)
438 HDRVR WINAPI
OpenDriverA(LPCSTR lpDriverName
, LPCSTR lpSectionName
, LPARAM lParam2
)
440 LPWINE_DRIVER lpDrv
= NULL
;
442 LPCSTR lsn
= lpSectionName
;
444 TRACE("(%s, %s, 0x%08lx);\n", debugstr_a(lpDriverName
), debugstr_a(lpSectionName
), lParam2
);
447 lstrcpynA(libName
, lpDriverName
, sizeof(libName
));
449 if ((lpDrv
= DRIVER_TryOpenDriver32(libName
, lParam2
)))
453 if (DRIVER_GetLibName(lpDriverName
, lsn
, libName
, sizeof(libName
)) &&
454 (lpDrv
= DRIVER_TryOpenDriver32(libName
, lParam2
)))
457 if (!(lpDrv
= DRIVER_TryOpenDriver16(lpDriverName
, lpSectionName
, lParam2
)))
458 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName
), debugstr_a(lpSectionName
));
460 if (lpDrv
) TRACE("=> %08lx\n", (DWORD
)lpDrv
);
464 /**************************************************************************
465 * OpenDriver [WINMM.@]
468 HDRVR WINAPI
OpenDriverW(LPCWSTR lpDriverName
, LPCWSTR lpSectionName
, LPARAM lParam
)
470 LPSTR dn
= HEAP_strdupWtoA(GetProcessHeap(), 0, lpDriverName
);
471 LPSTR sn
= HEAP_strdupWtoA(GetProcessHeap(), 0, lpSectionName
);
472 HDRVR ret
= OpenDriverA(dn
, sn
, lParam
);
474 if (dn
) HeapFree(GetProcessHeap(), 0, dn
);
475 if (sn
) HeapFree(GetProcessHeap(), 0, sn
);
479 /**************************************************************************
480 * CloseDriver [WINMM.@]
483 LRESULT WINAPI
CloseDriver(HDRVR hDrvr
, LPARAM lParam1
, LPARAM lParam2
)
487 TRACE("(%04x, %08lX, %08lX);\n", hDrvr
, lParam1
, lParam2
);
489 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
)
491 if (lpDrv
->dwFlags
& WINE_GDF_16BIT
)
492 CloseDriver16(lpDrv
->d
.d16
.hDriver16
, lParam1
, lParam2
);
495 DRIVER_SendMessage(lpDrv
, DRV_CLOSE
, lParam1
, lParam2
);
496 lpDrv
->d
.d32
.dwDriverID
= 0;
498 if (DRIVER_RemoveFromList(lpDrv
)) {
499 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
))
501 LPWINE_DRIVER lpDrv0
;
503 /* if driver has an opened session instance, we have to close it too */
504 if (DRIVER_GetNumberOfModuleRefs(lpDrv
->d
.d32
.hModule
, &lpDrv0
) == 1)
506 DRIVER_SendMessage(lpDrv0
, DRV_CLOSE
, 0L, 0L);
507 lpDrv0
->d
.d32
.dwDriverID
= 0;
508 DRIVER_RemoveFromList(lpDrv0
);
509 FreeLibrary(lpDrv
->d
.d32
.hModule
);
510 HeapFree(GetProcessHeap(), 0, lpDrv0
);
512 FreeLibrary(lpDrv
->d
.d32
.hModule
);
514 HeapFree(GetProcessHeap(), 0, lpDrv
);
518 WARN("Failed to close driver\n");
522 /**************************************************************************
523 * GetDriverFlags [WINMM.@]
524 * [in] hDrvr handle to the driver
527 * 0x00000000 if hDrvr is an invalid handle
528 * 0x80000000 if hDrvr is a valid 32 bit driver
529 * 0x90000000 if hDrvr is a valid 16 bit driver
531 * native WINMM doesn't return those flags
532 * 0x80000000 for a valid 32 bit driver and that's it
533 * (I may have mixed up the two flags :-(
535 DWORD WINAPI
GetDriverFlags(HDRVR hDrvr
)
540 TRACE("(%04x)\n", hDrvr
);
542 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
) {
543 ret
= WINE_GDF_EXIST
| lpDrv
->dwFlags
;
548 /**************************************************************************
549 * GetDriverModuleHandle [WINMM.@]
550 * DrvGetModuleHandle [WINMM.@]
552 HMODULE WINAPI
GetDriverModuleHandle(HDRVR hDrvr
)
557 TRACE("(%04x);\n", hDrvr
);
559 if ((lpDrv
= DRIVER_FindFromHDrvr(hDrvr
)) != NULL
) {
560 if (!(lpDrv
->dwFlags
& WINE_GDF_16BIT
))
561 hModule
= lpDrv
->d
.d32
.hModule
;
563 TRACE("=> %04x\n", hModule
);
567 /**************************************************************************
568 * DrvOpen [MMSYSTEM.1100]
570 HDRVR16 WINAPI
DrvOpen16(LPSTR lpDriverName
, LPSTR lpSectionName
, LPARAM lParam
)
572 return OpenDriver16(lpDriverName
, lpSectionName
, lParam
);
575 /**************************************************************************
576 * DrvClose [MMSYSTEM.1101]
578 LRESULT WINAPI
DrvClose16(HDRVR16 hDrv
, LPARAM lParam1
, LPARAM lParam2
)
580 return CloseDriver16(hDrv
, lParam1
, lParam2
);
583 /**************************************************************************
584 * DrvSendMessage [MMSYSTEM.1102]
586 LRESULT WINAPI
DrvSendMessage16(HDRVR16 hDrv
, WORD msg
, LPARAM lParam1
,
589 return SendDriverMessage16(hDrv
, msg
, lParam1
, lParam2
);
592 /**************************************************************************
593 * DrvGetModuleHandle [MMSYSTEM.1103]
595 HANDLE16 WINAPI
DrvGetModuleHandle16(HDRVR16 hDrv
)
597 return GetDriverModuleHandle16(hDrv
);
600 /**************************************************************************
601 * DrvDefDriverProc [MMSYSTEM.1104]
603 LRESULT WINAPI
DrvDefDriverProc16(DWORD dwDriverID
, HDRVR16 hDrv
, WORD wMsg
,
604 DWORD dwParam1
, DWORD dwParam2
)
606 return DefDriverProc16(dwDriverID
, hDrv
, wMsg
, dwParam1
, dwParam2
);
609 /**************************************************************************
610 * DefDriverProc [WINMM.@]
611 * DrvDefDriverProc [WINMM.@]
613 LRESULT WINAPI
DefDriverProc(DWORD dwDriverIdentifier
, HDRVR hDrv
,
614 UINT Msg
, LPARAM lParam1
, LPARAM lParam2
)
630 /**************************************************************************
631 * DriverProc [MMSYSTEM.6]
633 LRESULT WINAPI
DriverProc16(DWORD dwDevID
, HDRVR16 hDrv
, WORD wMsg
,
634 DWORD dwParam1
, DWORD dwParam2
)
636 TRACE("dwDevID=%08lx hDrv=%04x wMsg=%04x dwParam1=%08lx dwParam2=%08lx\n",
637 dwDevID
, hDrv
, wMsg
, dwParam1
, dwParam2
);
639 return DrvDefDriverProc16(dwDevID
, hDrv
, wMsg
, dwParam1
, dwParam2
);