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]
60 static void WINE_UNUSED
DRIVER_LoadStartupDrivers(void)
64 if (GetPrivateProfileStringA("drivers", NULL
, "", str
, sizeof(str
), "SYSTEM.INI") < 2) {
65 ERR("Can't find drivers section in system.ini\n");
70 for (ptr
= str
; *ptr
; ptr
+= strlen(ptr
) + 1) {
71 TRACE("str='%s'\n", ptr
);
72 hDrv
= OpenDriver16(ptr
, "drivers", 0L);
73 TRACE("hDrv=%04x\n", hDrv
);
75 TRACE("end of list !\n");
79 /**************************************************************************
80 * DRIVER_GetNumberOfModuleRefs [internal]
82 * Returns the number of open drivers which share the same module.
84 static WORD
DRIVER_GetNumberOfModuleRefs(LPWINE_DRIVER lpNewDrv
)
89 for (lpDrv
= lpDrvItemList
; lpDrv
; lpDrv
= lpDrv
->lpNextItem
) {
90 if (lpDrv
->hModule16
== lpNewDrv
->hModule16
) {
97 /**************************************************************************
98 * DRIVER_FindFromHDrvr16 [internal]
100 * From a hDrvr being 16 bits, returns the WINE internal structure.
102 static LPWINE_DRIVER
DRIVER_FindFromHDrvr16(HDRVR16 hDrvr
)
106 for (lpDrv
= lpDrvItemList
; lpDrv
; lpDrv
= lpDrv
->lpNextItem
) {
107 if (lpDrv
->hDriver16
== hDrvr
) {
114 /**************************************************************************
115 * DRIVER_SendMessage [internal]
117 static LRESULT
inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv
, UINT16 msg
,
118 LPARAM lParam1
, LPARAM lParam2
)
123 TRACE("Before CallDriverProc proc=%p driverID=%08lx wMsg=%04x p1=%08lx p2=%08lx\n",
124 lpDrv
->lpDrvProc
, lpDrv
->dwDriverID
, msg
, lParam1
, lParam2
);
126 args
[7] = HIWORD(lpDrv
->dwDriverID
);
127 args
[6] = LOWORD(lpDrv
->dwDriverID
);
128 args
[5] = lpDrv
->hDriver16
;
130 args
[3] = HIWORD(lParam1
);
131 args
[2] = LOWORD(lParam1
);
132 args
[1] = HIWORD(lParam2
);
133 args
[0] = LOWORD(lParam2
);
134 WOWCallback16Ex( (DWORD
)lpDrv
->lpDrvProc
, WCB16_PASCAL
, sizeof(args
), args
, &ret
);
138 /**************************************************************************
139 * SendDriverMessage (USER.251)
141 LRESULT WINAPI
SendDriverMessage16(HDRVR16 hDriver
, UINT16 msg
, LPARAM lParam1
,
147 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver
, msg
, lParam1
, lParam2
);
149 if ((lpDrv
= DRIVER_FindFromHDrvr16(hDriver
)) != NULL
) {
150 retval
= DRIVER_SendMessage(lpDrv
, msg
, lParam1
, lParam2
);
152 WARN("Bad driver handle %u\n", hDriver
);
155 TRACE("retval = %ld\n", retval
);
159 /**************************************************************************
160 * DRIVER_RemoveFromList [internal]
162 * Generates all the logic to handle driver closure / deletion
163 * Removes a driver struct to the list of open drivers.
165 static BOOL
DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv
)
167 lpDrv
->dwDriverID
= 0;
168 if (DRIVER_GetNumberOfModuleRefs(lpDrv
) == 1) {
169 DRIVER_SendMessage(lpDrv
, DRV_DISABLE
, 0L, 0L);
170 DRIVER_SendMessage(lpDrv
, DRV_FREE
, 0L, 0L);
173 if (lpDrv
->lpPrevItem
)
174 lpDrv
->lpPrevItem
->lpNextItem
= lpDrv
->lpNextItem
;
176 lpDrvItemList
= lpDrv
->lpNextItem
;
177 if (lpDrv
->lpNextItem
)
178 lpDrv
->lpNextItem
->lpPrevItem
= lpDrv
->lpPrevItem
;
183 /**************************************************************************
184 * DRIVER_AddToList [internal]
186 * Adds a driver struct to the list of open drivers.
187 * Generates all the logic to handle driver creation / open.
189 static BOOL
DRIVER_AddToList(LPWINE_DRIVER lpNewDrv
, LPARAM lParam1
, LPARAM lParam2
)
191 /* First driver to be loaded for this module, need to load correctly the module */
192 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv
) == 0) {
193 if (DRIVER_SendMessage(lpNewDrv
, DRV_LOAD
, 0L, 0L) != DRV_SUCCESS
) {
194 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD
)lpNewDrv
);
197 /* returned value is not checked */
198 DRIVER_SendMessage(lpNewDrv
, DRV_ENABLE
, 0L, 0L);
201 lpNewDrv
->lpNextItem
= NULL
;
202 if (lpDrvItemList
== NULL
) {
203 lpDrvItemList
= lpNewDrv
;
204 lpNewDrv
->lpPrevItem
= NULL
;
206 LPWINE_DRIVER lpDrv
= lpDrvItemList
; /* find end of list */
207 while (lpDrv
->lpNextItem
!= NULL
)
208 lpDrv
= lpDrv
->lpNextItem
;
210 lpDrv
->lpNextItem
= lpNewDrv
;
211 lpNewDrv
->lpPrevItem
= lpDrv
;
213 /* Now just open a new instance of a driver on this module */
214 lpNewDrv
->dwDriverID
= DRIVER_SendMessage(lpNewDrv
, DRV_OPEN
, lParam1
, lParam2
);
216 if (lpNewDrv
->dwDriverID
== 0) {
217 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD
)lpNewDrv
);
218 DRIVER_RemoveFromList(lpNewDrv
);
225 /**************************************************************************
226 * DRIVER_TryOpenDriver16 [internal]
228 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
230 static LPWINE_DRIVER
DRIVER_TryOpenDriver16(LPCSTR lpFileName
, LPARAM lParam2
)
232 static WORD DRIVER_hDrvr16Counter
/* = 0 */;
233 LPWINE_DRIVER lpDrv
= NULL
;
239 TRACE("('%s', %08lX);\n", lpFileName
, lParam2
);
241 if (strlen(lpFileName
) < 1) return lpDrv
;
243 if ((lpSFN
= strrchr(lpFileName
, '\\')) == NULL
)
247 if ((ptr
= strchr(lpFileName
, ' ')) != NULL
) {
249 while (*ptr
== ' ') ptr
++;
250 if (*ptr
== '\0') ptr
= NULL
;
253 if ((hModule
= LoadLibrary16(lpFileName
)) < 32) goto exit
;
254 if ((lpProc
= (DRIVERPROC16
)GetProcAddress16(hModule
, "DRIVERPROC")) == NULL
)
257 if ((lpDrv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER
))) == NULL
)
260 lpDrv
->dwDriverID
= 0;
261 while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter
));
262 lpDrv
->hDriver16
= DRIVER_hDrvr16Counter
;
263 lstrcpynA(lpDrv
->szAliasName
, lpSFN
, sizeof(lpDrv
->szAliasName
));
264 lpDrv
->hModule16
= hModule
;
265 lpDrv
->lpDrvProc
= lpProc
;
267 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, lParam2
)) goto exit
;
271 TRACE("Unable to load 16 bit module (%s): %04x\n", lpFileName
, hModule
);
272 if (hModule
>= 32) FreeLibrary16(hModule
);
273 HeapFree(GetProcessHeap(), 0, lpDrv
);
277 /**************************************************************************
278 * OpenDriver (USER.252)
280 HDRVR16 WINAPI
OpenDriver16(LPCSTR lpDriverName
, LPCSTR lpSectionName
, LPARAM lParam2
)
282 LPWINE_DRIVER lpDrv
= NULL
;
285 TRACE("(%s, %s, %08lX);\n", debugstr_a(lpDriverName
), debugstr_a(lpSectionName
), lParam2
);
287 if (!lpDriverName
|| !*lpDriverName
) return 0;
289 if (lpSectionName
== NULL
) {
290 strcpy(drvName
, lpDriverName
);
292 if ((lpDrv
= DRIVER_TryOpenDriver16(drvName
, lParam2
)))
294 /* in case hDriver is NULL, search in Drivers section */
295 lpSectionName
= "Drivers";
297 if (GetPrivateProfileStringA(lpSectionName
, lpDriverName
, "",
298 drvName
, sizeof(drvName
), "SYSTEM.INI") > 0) {
299 lpDrv
= DRIVER_TryOpenDriver16(drvName
, lParam2
);
302 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName
), debugstr_a(lpSectionName
));
306 TRACE("=> %04x / %08lx\n", lpDrv
->hDriver16
, (DWORD
)lpDrv
);
307 return lpDrv
->hDriver16
;
310 /**************************************************************************
311 * CloseDriver (USER.253)
313 LRESULT WINAPI
CloseDriver16(HDRVR16 hDrvr
, LPARAM lParam1
, LPARAM lParam2
)
317 TRACE("(%04x, %08lX, %08lX);\n", hDrvr
, lParam1
, lParam2
);
319 if ((lpDrv
= DRIVER_FindFromHDrvr16(hDrvr
)) != NULL
) {
320 DRIVER_SendMessage(lpDrv
, DRV_CLOSE
, lParam1
, lParam2
);
322 if (DRIVER_RemoveFromList(lpDrv
)) {
323 HeapFree(GetProcessHeap(), 0, lpDrv
);
327 WARN("Failed to close driver\n");
331 /**************************************************************************
332 * GetDriverModuleHandle (USER.254)
334 HMODULE16 WINAPI
GetDriverModuleHandle16(HDRVR16 hDrvr
)
337 HMODULE16 hModule
= 0;
339 TRACE("(%04x);\n", hDrvr
);
341 if ((lpDrv
= DRIVER_FindFromHDrvr16(hDrvr
)) != NULL
) {
342 hModule
= lpDrv
->hModule16
;
344 TRACE("=> %04x\n", hModule
);
348 /**************************************************************************
349 * DefDriverProc (USER.255)
351 LRESULT WINAPI
DefDriverProc16(DWORD dwDevID
, HDRVR16 hDriv
, UINT16 wMsg
,
352 LPARAM lParam1
, LPARAM lParam2
)
354 TRACE("devID=0x%08lx hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
355 dwDevID
, hDriv
, wMsg
, lParam1
, lParam2
);
365 case DRV_QUERYCONFIGURE
:
368 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK
);
378 /**************************************************************************
379 * GetDriverInfo (USER.256)
381 BOOL16 WINAPI
GetDriverInfo16(HDRVR16 hDrvr
, LPDRIVERINFOSTRUCT16 lpDrvInfo
)
386 TRACE("(%04x, %p);\n", hDrvr
, lpDrvInfo
);
388 if (lpDrvInfo
== NULL
|| lpDrvInfo
->length
!= sizeof(DRIVERINFOSTRUCT16
))
391 if ((lpDrv
= DRIVER_FindFromHDrvr16(hDrvr
)) != NULL
) {
392 lpDrvInfo
->hDriver
= lpDrv
->hDriver16
;
393 lpDrvInfo
->hModule
= lpDrv
->hModule16
;
394 lstrcpynA(lpDrvInfo
->szAliasName
, lpDrv
->szAliasName
, sizeof(lpDrvInfo
->szAliasName
));
401 /**************************************************************************
402 * GetNextDriver (USER.257)
404 HDRVR16 WINAPI
GetNextDriver16(HDRVR16 hDrvr
, DWORD dwFlags
)
409 TRACE("(%04x, %08lX);\n", hDrvr
, dwFlags
);
412 if (lpDrvItemList
== NULL
) {
413 FIXME("drivers list empty !\n");
414 /* FIXME: code was using DRIVER_LoadStartupDrivers(); before ?
415 * I (EPP) don't quite understand this
417 if (lpDrvItemList
== NULL
)
420 lpDrv
= lpDrvItemList
;
421 if (dwFlags
& GND_REVERSE
) {
422 while (lpDrv
->lpNextItem
)
423 lpDrv
= lpDrv
->lpNextItem
;
426 if ((lpDrv
= DRIVER_FindFromHDrvr16(hDrvr
)) != NULL
) {
427 if (dwFlags
& GND_REVERSE
) {
428 lpDrv
= (lpDrv
->lpPrevItem
) ? lpDrv
->lpPrevItem
: NULL
;
430 lpDrv
= (lpDrv
->lpNextItem
) ? lpDrv
->lpNextItem
: NULL
;
435 hRetDrv
= (lpDrv
) ? lpDrv
->hDriver16
: (HDRVR16
)0;
436 TRACE("return %04x !\n", hRetDrv
);