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,2000 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
28 #include "wine/winbase16.h"
33 #include "wine/mmsystem16.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(driver
);
38 typedef struct tagWINE_DRIVER
40 char szAliasName
[128];
41 /* as usual LPWINE_DRIVER == hDriver32 */
44 DRIVERPROC16 lpDrvProc
;
46 struct tagWINE_DRIVER
* lpPrevItem
;
47 struct tagWINE_DRIVER
* lpNextItem
;
48 } WINE_DRIVER
, *LPWINE_DRIVER
;
50 static LPWINE_DRIVER lpDrvItemList
= NULL
;
53 * - LoadModule count and clean up is not handled correctly (it's not a
54 * problem as long as FreeLibrary is not working correctly)
58 /**************************************************************************
59 * LoadStartupDrivers [internal]
61 static void WINE_UNUSED
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");
80 /**************************************************************************
81 * DRIVER_GetNumberOfModuleRefs [internal]
83 * Returns the number of open drivers which share the same module.
85 static WORD
DRIVER_GetNumberOfModuleRefs(LPWINE_DRIVER lpNewDrv
)
90 for (lpDrv
= lpDrvItemList
; lpDrv
; lpDrv
= lpDrv
->lpNextItem
) {
91 if (lpDrv
->hModule16
== lpNewDrv
->hModule16
) {
98 /**************************************************************************
99 * DRIVER_FindFromHDrvr16 [internal]
101 * From a hDrvr being 16 bits, returns the WINE internal structure.
103 static LPWINE_DRIVER
DRIVER_FindFromHDrvr16(HDRVR16 hDrvr
)
107 for (lpDrv
= lpDrvItemList
; lpDrv
; lpDrv
= lpDrv
->lpNextItem
) {
108 if (lpDrv
->hDriver16
== hDrvr
) {
115 /**************************************************************************
116 * DRIVER_SendMessage [internal]
118 static LRESULT
inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv
, UINT16 msg
,
119 LPARAM lParam1
, LPARAM lParam2
)
124 TRACE("Before CallDriverProc proc=%p driverID=%08lx wMsg=%04x p1=%08lx p2=%08lx\n",
125 lpDrv
->lpDrvProc
, lpDrv
->dwDriverID
, msg
, lParam1
, lParam2
);
127 args
[7] = HIWORD(lpDrv
->dwDriverID
);
128 args
[6] = LOWORD(lpDrv
->dwDriverID
);
129 args
[5] = lpDrv
->hDriver16
;
131 args
[3] = HIWORD(lParam1
);
132 args
[2] = LOWORD(lParam1
);
133 args
[1] = HIWORD(lParam2
);
134 args
[0] = LOWORD(lParam2
);
135 WOWCallback16Ex( (DWORD
)lpDrv
->lpDrvProc
, WCB16_PASCAL
, sizeof(args
), args
, &ret
);
139 /**************************************************************************
140 * SendDriverMessage (USER.251)
142 LRESULT WINAPI
SendDriverMessage16(HDRVR16 hDriver
, UINT16 msg
, LPARAM lParam1
,
148 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver
, msg
, lParam1
, lParam2
);
150 if ((lpDrv
= DRIVER_FindFromHDrvr16(hDriver
)) != NULL
) {
151 retval
= DRIVER_SendMessage(lpDrv
, msg
, lParam1
, lParam2
);
153 WARN("Bad driver handle %u\n", hDriver
);
156 TRACE("retval = %ld\n", retval
);
160 /**************************************************************************
161 * DRIVER_RemoveFromList [internal]
163 * Generates all the logic to handle driver closure / deletion
164 * Removes a driver struct to the list of open drivers.
166 static BOOL
DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv
)
168 lpDrv
->dwDriverID
= 0;
169 if (DRIVER_GetNumberOfModuleRefs(lpDrv
) == 1) {
170 DRIVER_SendMessage(lpDrv
, DRV_DISABLE
, 0L, 0L);
171 DRIVER_SendMessage(lpDrv
, DRV_FREE
, 0L, 0L);
174 if (lpDrv
->lpPrevItem
)
175 lpDrv
->lpPrevItem
->lpNextItem
= lpDrv
->lpNextItem
;
177 lpDrvItemList
= lpDrv
->lpNextItem
;
178 if (lpDrv
->lpNextItem
)
179 lpDrv
->lpNextItem
->lpPrevItem
= lpDrv
->lpPrevItem
;
184 /**************************************************************************
185 * DRIVER_AddToList [internal]
187 * Adds a driver struct to the list of open drivers.
188 * Generates all the logic to handle driver creation / open.
190 static BOOL
DRIVER_AddToList(LPWINE_DRIVER lpNewDrv
, LPARAM lParam1
, LPARAM lParam2
)
192 /* First driver to be loaded for this module, need to load correctly the module */
193 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv
) == 0) {
194 if (DRIVER_SendMessage(lpNewDrv
, DRV_LOAD
, 0L, 0L) != DRV_SUCCESS
) {
195 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD
)lpNewDrv
);
198 /* returned value is not checked */
199 DRIVER_SendMessage(lpNewDrv
, DRV_ENABLE
, 0L, 0L);
202 lpNewDrv
->lpNextItem
= NULL
;
203 if (lpDrvItemList
== NULL
) {
204 lpDrvItemList
= lpNewDrv
;
205 lpNewDrv
->lpPrevItem
= NULL
;
207 LPWINE_DRIVER lpDrv
= lpDrvItemList
; /* find end of list */
208 while (lpDrv
->lpNextItem
!= NULL
)
209 lpDrv
= lpDrv
->lpNextItem
;
211 lpDrv
->lpNextItem
= lpNewDrv
;
212 lpNewDrv
->lpPrevItem
= lpDrv
;
214 /* Now just open a new instance of a driver on this module */
215 lpNewDrv
->dwDriverID
= DRIVER_SendMessage(lpNewDrv
, DRV_OPEN
, lParam1
, lParam2
);
217 if (lpNewDrv
->dwDriverID
== 0) {
218 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD
)lpNewDrv
);
219 DRIVER_RemoveFromList(lpNewDrv
);
226 /**************************************************************************
227 * DRIVER_TryOpenDriver16 [internal]
229 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
231 static LPWINE_DRIVER
DRIVER_TryOpenDriver16(LPCSTR lpFileName
, LPARAM lParam2
)
233 static WORD DRIVER_hDrvr16Counter
/* = 0 */;
234 LPWINE_DRIVER lpDrv
= NULL
;
240 TRACE("('%s', %08lX);\n", lpFileName
, lParam2
);
242 if (strlen(lpFileName
) < 1) return lpDrv
;
244 if ((lpSFN
= strrchr(lpFileName
, '\\')) == NULL
)
248 if ((ptr
= strchr(lpFileName
, ' ')) != NULL
) {
250 while (*ptr
== ' ') ptr
++;
251 if (*ptr
== '\0') ptr
= NULL
;
254 if ((hModule
= LoadLibrary16(lpFileName
)) < 32) goto exit
;
255 if ((lpProc
= (DRIVERPROC16
)GetProcAddress16(hModule
, "DRIVERPROC")) == NULL
)
258 if ((lpDrv
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER
))) == NULL
)
261 lpDrv
->dwDriverID
= 0;
262 while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter
));
263 lpDrv
->hDriver16
= DRIVER_hDrvr16Counter
;
264 lstrcpynA(lpDrv
->szAliasName
, lpSFN
, sizeof(lpDrv
->szAliasName
));
265 lpDrv
->hModule16
= hModule
;
266 lpDrv
->lpDrvProc
= lpProc
;
268 if (!DRIVER_AddToList(lpDrv
, (LPARAM
)ptr
, lParam2
)) goto exit
;
272 TRACE("Unable to load 16 bit module (%s): %04x\n", lpFileName
, hModule
);
273 if (hModule
>= 32) FreeLibrary16(hModule
);
274 HeapFree(GetProcessHeap(), 0, lpDrv
);
278 /**************************************************************************
279 * OpenDriver (USER.252)
281 HDRVR16 WINAPI
OpenDriver16(LPCSTR lpDriverName
, LPCSTR lpSectionName
, LPARAM lParam2
)
283 LPWINE_DRIVER lpDrv
= NULL
;
286 TRACE("(%s, %s, %08lX);\n", debugstr_a(lpDriverName
), debugstr_a(lpSectionName
), lParam2
);
288 if (!lpDriverName
|| !*lpDriverName
) return 0;
290 if (lpSectionName
== NULL
) {
291 strcpy(drvName
, lpDriverName
);
293 if ((lpDrv
= DRIVER_TryOpenDriver16(drvName
, lParam2
)))
295 /* in case hDriver is NULL, search in Drivers section */
296 lpSectionName
= "Drivers";
298 if (GetPrivateProfileStringA(lpSectionName
, lpDriverName
, "",
299 drvName
, sizeof(drvName
), "SYSTEM.INI") > 0) {
300 lpDrv
= DRIVER_TryOpenDriver16(drvName
, lParam2
);
303 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName
), debugstr_a(lpSectionName
));
307 TRACE("=> %04x / %08lx\n", lpDrv
->hDriver16
, (DWORD
)lpDrv
);
308 return lpDrv
->hDriver16
;
311 /**************************************************************************
312 * CloseDriver (USER.253)
314 LRESULT WINAPI
CloseDriver16(HDRVR16 hDrvr
, LPARAM lParam1
, LPARAM lParam2
)
318 TRACE("(%04x, %08lX, %08lX);\n", hDrvr
, lParam1
, lParam2
);
320 if ((lpDrv
= DRIVER_FindFromHDrvr16(hDrvr
)) != NULL
) {
321 DRIVER_SendMessage(lpDrv
, DRV_CLOSE
, lParam1
, lParam2
);
323 if (DRIVER_RemoveFromList(lpDrv
)) {
324 HeapFree(GetProcessHeap(), 0, lpDrv
);
328 WARN("Failed to close driver\n");
332 /**************************************************************************
333 * GetDriverModuleHandle (USER.254)
335 HMODULE16 WINAPI
GetDriverModuleHandle16(HDRVR16 hDrvr
)
338 HMODULE16 hModule
= 0;
340 TRACE("(%04x);\n", hDrvr
);
342 if ((lpDrv
= DRIVER_FindFromHDrvr16(hDrvr
)) != NULL
) {
343 hModule
= lpDrv
->hModule16
;
345 TRACE("=> %04x\n", hModule
);
349 /**************************************************************************
350 * DefDriverProc (USER.255)
352 LRESULT WINAPI
DefDriverProc16(DWORD dwDevID
, HDRVR16 hDriv
, UINT16 wMsg
,
353 LPARAM lParam1
, LPARAM lParam2
)
355 TRACE("devID=0x%08lx hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
356 dwDevID
, hDriv
, wMsg
, lParam1
, lParam2
);
366 case DRV_QUERYCONFIGURE
:
369 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK
);
379 /**************************************************************************
380 * GetDriverInfo (USER.256)
382 BOOL16 WINAPI
GetDriverInfo16(HDRVR16 hDrvr
, LPDRIVERINFOSTRUCT16 lpDrvInfo
)
387 TRACE("(%04x, %p);\n", hDrvr
, lpDrvInfo
);
389 if (lpDrvInfo
== NULL
|| lpDrvInfo
->length
!= sizeof(DRIVERINFOSTRUCT16
))
392 if ((lpDrv
= DRIVER_FindFromHDrvr16(hDrvr
)) != NULL
) {
393 lpDrvInfo
->hDriver
= lpDrv
->hDriver16
;
394 lpDrvInfo
->hModule
= lpDrv
->hModule16
;
395 lstrcpynA(lpDrvInfo
->szAliasName
, lpDrv
->szAliasName
, sizeof(lpDrvInfo
->szAliasName
));
402 /**************************************************************************
403 * GetNextDriver (USER.257)
405 HDRVR16 WINAPI
GetNextDriver16(HDRVR16 hDrvr
, DWORD dwFlags
)
410 TRACE("(%04x, %08lX);\n", hDrvr
, dwFlags
);
413 if (lpDrvItemList
== NULL
) {
414 FIXME("drivers list empty !\n");
415 /* FIXME: code was using DRIVER_LoadStartupDrivers(); before ?
416 * I (EPP) don't quite understand this
418 if (lpDrvItemList
== NULL
)
421 lpDrv
= lpDrvItemList
;
422 if (dwFlags
& GND_REVERSE
) {
423 while (lpDrv
->lpNextItem
)
424 lpDrv
= lpDrv
->lpNextItem
;
427 if ((lpDrv
= DRIVER_FindFromHDrvr16(hDrvr
)) != NULL
) {
428 if (dwFlags
& GND_REVERSE
) {
429 lpDrv
= (lpDrv
->lpPrevItem
) ? lpDrv
->lpPrevItem
: NULL
;
431 lpDrv
= (lpDrv
->lpNextItem
) ? lpDrv
->lpNextItem
: NULL
;
436 hRetDrv
= (lpDrv
) ? lpDrv
->hDriver16
: (HDRVR16
)0;
437 TRACE("return %04x !\n", hRetDrv
);