2 * WINE Drivers functions
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1998 Marcus Meissner
6 * Copyright 1999,2000 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * - LoadModule count and clean up is not handled correctly (it's not a
24 * problem as long as FreeLibrary is not working correctly)
32 #include "wine/winbase16.h"
37 #include "wine/mmsystem16.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(driver
);
42 typedef struct tagWINE_DRIVER
44 char szAliasName
[128];
45 /* as usual LPWINE_DRIVER == hDriver32 */
48 DRIVERPROC16 lpDrvProc
;
50 struct tagWINE_DRIVER
* lpPrevItem
;
51 struct tagWINE_DRIVER
* lpNextItem
;
52 } WINE_DRIVER
, *LPWINE_DRIVER
;
54 static LPWINE_DRIVER lpDrvItemList
= NULL
;
57 /**************************************************************************
58 * LoadStartupDrivers [internal]
61 static void DRIVER_LoadStartupDrivers(void)
65 if (GetPrivateProfileStringA("drivers", NULL
, "", str
, sizeof(str
), "SYSTEM.INI") < 2) {
66 ERR("Can't find drivers section in system.ini\n");
71 for (ptr
= str
; *ptr
; ptr
+= strlen(ptr
) + 1) {
72 TRACE("str='%s'\n", ptr
);
73 hDrv
= OpenDriver16(ptr
, "drivers", 0L);
74 TRACE("hDrv=%04x\n", hDrv
);
76 TRACE("end of list !\n");
81 /**************************************************************************
82 * DRIVER_GetNumberOfModuleRefs [internal]
84 * Returns the number of open drivers which share the same module.
86 static WORD
DRIVER_GetNumberOfModuleRefs(LPWINE_DRIVER lpNewDrv
)
91 for (lpDrv
= lpDrvItemList
; lpDrv
; lpDrv
= lpDrv
->lpNextItem
) {
92 if (lpDrv
->hModule16
== lpNewDrv
->hModule16
) {
99 /**************************************************************************
100 * DRIVER_FindFromHDrvr16 [internal]
102 * From a hDrvr being 16 bits, returns the WINE internal structure.
104 static LPWINE_DRIVER
DRIVER_FindFromHDrvr16(HDRVR16 hDrvr
)
108 for (lpDrv
= lpDrvItemList
; lpDrv
; lpDrv
= lpDrv
->lpNextItem
) {
109 if (lpDrv
->hDriver16
== hDrvr
) {
116 /**************************************************************************
117 * DRIVER_SendMessage [internal]
119 static LRESULT
inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv
, UINT16 msg
,
120 LPARAM lParam1
, LPARAM lParam2
)
125 TRACE("Before CallDriverProc proc=%p driverID=%08lx wMsg=%04x p1=%08lx p2=%08lx\n",
126 lpDrv
->lpDrvProc
, lpDrv
->dwDriverID
, msg
, lParam1
, lParam2
);
128 args
[7] = HIWORD(lpDrv
->dwDriverID
);
129 args
[6] = LOWORD(lpDrv
->dwDriverID
);
130 args
[5] = lpDrv
->hDriver16
;
132 args
[3] = HIWORD(lParam1
);
133 args
[2] = LOWORD(lParam1
);
134 args
[1] = HIWORD(lParam2
);
135 args
[0] = LOWORD(lParam2
);
136 WOWCallback16Ex( (DWORD
)lpDrv
->lpDrvProc
, WCB16_PASCAL
, sizeof(args
), args
, &ret
);
140 /**************************************************************************
141 * SendDriverMessage (USER.251)
143 LRESULT WINAPI
SendDriverMessage16(HDRVR16 hDriver
, UINT16 msg
, LPARAM lParam1
,
149 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver
, msg
, lParam1
, lParam2
);
151 if ((lpDrv
= DRIVER_FindFromHDrvr16(hDriver
)) != NULL
) {
152 retval
= DRIVER_SendMessage(lpDrv
, msg
, lParam1
, lParam2
);
154 WARN("Bad driver handle %u\n", hDriver
);
157 TRACE("retval = %ld\n", retval
);
161 /**************************************************************************
162 * DRIVER_RemoveFromList [internal]
164 * Generates all the logic to handle driver closure / deletion
165 * Removes a driver struct to the list of open drivers.
167 static BOOL
DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv
)
169 lpDrv
->dwDriverID
= 0;
170 if (DRIVER_GetNumberOfModuleRefs(lpDrv
) == 1) {
171 DRIVER_SendMessage(lpDrv
, DRV_DISABLE
, 0L, 0L);
172 DRIVER_SendMessage(lpDrv
, DRV_FREE
, 0L, 0L);
175 if (lpDrv
->lpPrevItem
)
176 lpDrv
->lpPrevItem
->lpNextItem
= lpDrv
->lpNextItem
;
178 lpDrvItemList
= lpDrv
->lpNextItem
;
179 if (lpDrv
->lpNextItem
)
180 lpDrv
->lpNextItem
->lpPrevItem
= lpDrv
->lpPrevItem
;
185 /**************************************************************************
186 * DRIVER_AddToList [internal]
188 * Adds a driver struct to the list of open drivers.
189 * Generates all the logic to handle driver creation / open.
191 static BOOL
DRIVER_AddToList(LPWINE_DRIVER lpNewDrv
, LPARAM lParam1
, LPARAM lParam2
)
193 /* First driver to be loaded for this module, need to load correctly the module */
194 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv
) == 0) {
195 if (DRIVER_SendMessage(lpNewDrv
, DRV_LOAD
, 0L, 0L) != DRV_SUCCESS
) {
196 TRACE("DRV_LOAD failed on driver %p\n", lpNewDrv
);
199 /* returned value is not checked */
200 DRIVER_SendMessage(lpNewDrv
, DRV_ENABLE
, 0L, 0L);
203 lpNewDrv
->lpNextItem
= NULL
;
204 if (lpDrvItemList
== NULL
) {
205 lpDrvItemList
= lpNewDrv
;
206 lpNewDrv
->lpPrevItem
= NULL
;
208 LPWINE_DRIVER lpDrv
= lpDrvItemList
; /* find end of list */
209 while (lpDrv
->lpNextItem
!= NULL
)
210 lpDrv
= lpDrv
->lpNextItem
;
212 lpDrv
->lpNextItem
= lpNewDrv
;
213 lpNewDrv
->lpPrevItem
= lpDrv
;
215 /* Now just open a new instance of a driver on this module */
216 lpNewDrv
->dwDriverID
= DRIVER_SendMessage(lpNewDrv
, DRV_OPEN
, lParam1
, lParam2
);
218 if (lpNewDrv
->dwDriverID
== 0) {
219 TRACE("DRV_OPEN failed on driver %p\n", lpNewDrv
);
220 DRIVER_RemoveFromList(lpNewDrv
);
227 /**************************************************************************
228 * DRIVER_TryOpenDriver16 [internal]
230 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
232 static LPWINE_DRIVER
DRIVER_TryOpenDriver16(LPCSTR lpFileName
, LPARAM lParam2
)
234 static WORD DRIVER_hDrvr16Counter
/* = 0 */;
235 LPWINE_DRIVER lpDrv
= NULL
;
241 TRACE("('%s', %08lX);\n", lpFileName
, lParam2
);
243 if (strlen(lpFileName
) < 1) return lpDrv
;
245 if ((lpSFN
= strrchr(lpFileName
, '\\')) == NULL
)
249 if ((ptr
= strchr(lpFileName
, ' ')) != NULL
) {
251 while (*ptr
== ' ') ptr
++;
252 if (*ptr
== '\0') ptr
= NULL
;
255 if ((hModule
= LoadLibrary16(lpFileName
)) < 32) goto exit
;
256 if ((lpProc
= (DRIVERPROC16
)GetProcAddress16(hModule
, "DRIVERPROC")) == NULL
)
259 if ((lpDrv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER
))) == NULL
)
262 lpDrv
->dwDriverID
= 0;
263 while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter
));
264 lpDrv
->hDriver16
= DRIVER_hDrvr16Counter
;
265 lstrcpynA(lpDrv
->szAliasName
, lpSFN
, sizeof(lpDrv
->szAliasName
));
266 lpDrv
->hModule16
= hModule
;
267 lpDrv
->lpDrvProc
= lpProc
;
269 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, lParam2
)) goto exit
;
273 TRACE("Unable to load 16 bit module (%s): %04x\n", lpFileName
, hModule
);
274 if (hModule
>= 32) FreeLibrary16(hModule
);
275 HeapFree(GetProcessHeap(), 0, lpDrv
);
279 /**************************************************************************
280 * OpenDriver (USER.252)
282 HDRVR16 WINAPI
OpenDriver16(LPCSTR lpDriverName
, LPCSTR lpSectionName
, LPARAM lParam2
)
284 LPWINE_DRIVER lpDrv
= NULL
;
287 TRACE("(%s, %s, %08lX);\n", debugstr_a(lpDriverName
), debugstr_a(lpSectionName
), lParam2
);
289 if (!lpDriverName
|| !*lpDriverName
) return 0;
291 if (lpSectionName
== NULL
) {
292 strcpy(drvName
, lpDriverName
);
294 if ((lpDrv
= DRIVER_TryOpenDriver16(drvName
, lParam2
)))
296 /* in case hDriver is NULL, search in Drivers section */
297 lpSectionName
= "Drivers";
299 if (GetPrivateProfileStringA(lpSectionName
, lpDriverName
, "",
300 drvName
, sizeof(drvName
), "SYSTEM.INI") > 0) {
301 lpDrv
= DRIVER_TryOpenDriver16(drvName
, lParam2
);
304 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName
), debugstr_a(lpSectionName
));
308 TRACE("=> %04x / %p\n", lpDrv
->hDriver16
, lpDrv
);
309 return lpDrv
->hDriver16
;
312 /**************************************************************************
313 * CloseDriver (USER.253)
315 LRESULT WINAPI
CloseDriver16(HDRVR16 hDrvr
, LPARAM lParam1
, LPARAM lParam2
)
319 TRACE("(%04x, %08lX, %08lX);\n", hDrvr
, lParam1
, lParam2
);
321 if ((lpDrv
= DRIVER_FindFromHDrvr16(hDrvr
)) != NULL
) {
322 DRIVER_SendMessage(lpDrv
, DRV_CLOSE
, lParam1
, lParam2
);
324 if (DRIVER_RemoveFromList(lpDrv
)) {
325 HeapFree(GetProcessHeap(), 0, lpDrv
);
329 WARN("Failed to close driver\n");
333 /**************************************************************************
334 * GetDriverModuleHandle (USER.254)
336 HMODULE16 WINAPI
GetDriverModuleHandle16(HDRVR16 hDrvr
)
339 HMODULE16 hModule
= 0;
341 TRACE("(%04x);\n", hDrvr
);
343 if ((lpDrv
= DRIVER_FindFromHDrvr16(hDrvr
)) != NULL
) {
344 hModule
= lpDrv
->hModule16
;
346 TRACE("=> %04x\n", hModule
);
350 /**************************************************************************
351 * DefDriverProc (USER.255)
353 LRESULT WINAPI
DefDriverProc16(DWORD dwDevID
, HDRVR16 hDriv
, UINT16 wMsg
,
354 LPARAM lParam1
, LPARAM lParam2
)
356 TRACE("devID=0x%08lx hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
357 dwDevID
, hDriv
, wMsg
, lParam1
, lParam2
);
367 case DRV_QUERYCONFIGURE
:
370 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK
);
380 /**************************************************************************
381 * GetDriverInfo (USER.256)
383 BOOL16 WINAPI
GetDriverInfo16(HDRVR16 hDrvr
, LPDRIVERINFOSTRUCT16 lpDrvInfo
)
388 TRACE("(%04x, %p);\n", hDrvr
, lpDrvInfo
);
390 if (lpDrvInfo
== NULL
|| lpDrvInfo
->length
!= sizeof(DRIVERINFOSTRUCT16
))
393 if ((lpDrv
= DRIVER_FindFromHDrvr16(hDrvr
)) != NULL
) {
394 lpDrvInfo
->hDriver
= lpDrv
->hDriver16
;
395 lpDrvInfo
->hModule
= lpDrv
->hModule16
;
396 lstrcpynA(lpDrvInfo
->szAliasName
, lpDrv
->szAliasName
, sizeof(lpDrvInfo
->szAliasName
));
403 /**************************************************************************
404 * GetNextDriver (USER.257)
406 HDRVR16 WINAPI
GetNextDriver16(HDRVR16 hDrvr
, DWORD dwFlags
)
411 TRACE("(%04x, %08lX);\n", hDrvr
, dwFlags
);
414 if (lpDrvItemList
== NULL
) {
415 FIXME("drivers list empty !\n");
416 /* FIXME: code was using DRIVER_LoadStartupDrivers(); before ?
417 * I (EPP) don't quite understand this
419 if (lpDrvItemList
== NULL
)
422 lpDrv
= lpDrvItemList
;
423 if (dwFlags
& GND_REVERSE
) {
424 while (lpDrv
->lpNextItem
)
425 lpDrv
= lpDrv
->lpNextItem
;
428 if ((lpDrv
= DRIVER_FindFromHDrvr16(hDrvr
)) != NULL
) {
429 if (dwFlags
& GND_REVERSE
) {
430 lpDrv
= (lpDrv
->lpPrevItem
) ? lpDrv
->lpPrevItem
: NULL
;
432 lpDrv
= (lpDrv
->lpNextItem
) ? lpDrv
->lpNextItem
: NULL
;
437 hRetDrv
= (lpDrv
) ? lpDrv
->hDriver16
: (HDRVR16
)0;
438 TRACE("return %04x !\n", hRetDrv
);