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
30 #include "wine/winbase16.h"
35 #include "wine/mmsystem16.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(driver);
40 typedef struct tagWINE_DRIVER
42 char szAliasName[128];
43 /* as usual LPWINE_DRIVER == hDriver32 */
46 DRIVERPROC16 lpDrvProc;
48 struct tagWINE_DRIVER* lpPrevItem;
49 struct tagWINE_DRIVER* lpNextItem;
50 } WINE_DRIVER, *LPWINE_DRIVER;
52 static LPWINE_DRIVER lpDrvItemList = NULL;
55 * - LoadModule count and clean up is not handled correctly (it's not a
56 * problem as long as FreeLibrary is not working correctly)
60 /**************************************************************************
61 * LoadStartupDrivers [internal]
63 static void WINE_UNUSED DRIVER_LoadStartupDrivers(void)
67 if (GetPrivateProfileStringA("drivers", NULL, "", str, sizeof(str), "SYSTEM.INI") < 2) {
68 ERR("Can't find drivers section in system.ini\n");
73 for (ptr = str; *ptr; ptr += strlen(ptr) + 1) {
74 TRACE("str='%s'\n", ptr);
75 hDrv = OpenDriver16(ptr, "drivers", 0L);
76 TRACE("hDrv=%04x\n", hDrv);
78 TRACE("end of list !\n");
82 /**************************************************************************
83 * DRIVER_GetNumberOfModuleRefs [internal]
85 * Returns the number of open drivers which share the same module.
87 static WORD DRIVER_GetNumberOfModuleRefs(LPWINE_DRIVER lpNewDrv)
92 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
93 if (lpDrv->hModule16 == lpNewDrv->hModule16) {
100 /**************************************************************************
101 * DRIVER_FindFromHDrvr16 [internal]
103 * From a hDrvr being 16 bits, returns the WINE internal structure.
105 static LPWINE_DRIVER DRIVER_FindFromHDrvr16(HDRVR16 hDrvr)
109 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
110 if (lpDrv->hDriver16 == hDrvr) {
117 /**************************************************************************
118 * DRIVER_SendMessage [internal]
120 static LRESULT inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv, UINT16 msg,
121 LPARAM lParam1, LPARAM lParam2)
126 TRACE("Before CallDriverProc proc=%p driverID=%08lx wMsg=%04x p1=%08lx p2=%08lx\n",
127 lpDrv->lpDrvProc, lpDrv->dwDriverID, msg, lParam1, lParam2);
129 args[7] = HIWORD(lpDrv->dwDriverID);
130 args[6] = LOWORD(lpDrv->dwDriverID);
131 args[5] = lpDrv->hDriver16;
133 args[3] = HIWORD(lParam1);
134 args[2] = LOWORD(lParam1);
135 args[1] = HIWORD(lParam2);
136 args[0] = LOWORD(lParam2);
137 WOWCallback16Ex( (DWORD)lpDrv->lpDrvProc, WCB16_PASCAL, sizeof(args), args, &ret );
141 /**************************************************************************
142 * SendDriverMessage (USER.251)
144 LRESULT WINAPI SendDriverMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1,
150 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
152 if ((lpDrv = DRIVER_FindFromHDrvr16(hDriver)) != NULL) {
153 retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
155 WARN("Bad driver handle %u\n", hDriver);
158 TRACE("retval = %ld\n", retval);
162 /**************************************************************************
163 * DRIVER_RemoveFromList [internal]
165 * Generates all the logic to handle driver closure / deletion
166 * Removes a driver struct to the list of open drivers.
168 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
170 lpDrv->dwDriverID = 0;
171 if (DRIVER_GetNumberOfModuleRefs(lpDrv) == 1) {
172 DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
173 DRIVER_SendMessage(lpDrv, DRV_FREE, 0L, 0L);
176 if (lpDrv->lpPrevItem)
177 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
179 lpDrvItemList = lpDrv->lpNextItem;
180 if (lpDrv->lpNextItem)
181 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
186 /**************************************************************************
187 * DRIVER_AddToList [internal]
189 * Adds a driver struct to the list of open drivers.
190 * Generates all the logic to handle driver creation / open.
192 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
194 /* First driver to be loaded for this module, need to load correctly the module */
195 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv) == 0) {
196 if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
197 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
200 /* returned value is not checked */
201 DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
204 lpNewDrv->lpNextItem = NULL;
205 if (lpDrvItemList == NULL) {
206 lpDrvItemList = lpNewDrv;
207 lpNewDrv->lpPrevItem = NULL;
209 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
210 while (lpDrv->lpNextItem != NULL)
211 lpDrv = lpDrv->lpNextItem;
213 lpDrv->lpNextItem = lpNewDrv;
214 lpNewDrv->lpPrevItem = lpDrv;
216 /* Now just open a new instance of a driver on this module */
217 lpNewDrv->dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
219 if (lpNewDrv->dwDriverID == 0) {
220 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
221 DRIVER_RemoveFromList(lpNewDrv);
228 /**************************************************************************
229 * DRIVER_TryOpenDriver16 [internal]
231 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
233 static LPWINE_DRIVER DRIVER_TryOpenDriver16(LPCSTR lpFileName, LPARAM lParam2)
235 static WORD DRIVER_hDrvr16Counter /* = 0 */;
236 LPWINE_DRIVER lpDrv = NULL;
242 TRACE("('%s', %08lX);\n", lpFileName, lParam2);
244 if (strlen(lpFileName) < 1) return lpDrv;
246 if ((lpSFN = strrchr(lpFileName, '\\')) == NULL)
250 if ((ptr = strchr(lpFileName, ' ')) != NULL) {
252 while (*ptr == ' ') ptr++;
253 if (*ptr == '\0') ptr = NULL;
256 if ((hModule = LoadLibrary16(lpFileName)) < 32) goto exit;
257 if ((lpProc = (DRIVERPROC16)GetProcAddress16(hModule, "DRIVERPROC")) == NULL)
260 if ((lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER))) == NULL)
263 lpDrv->dwDriverID = 0;
264 while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter));
265 lpDrv->hDriver16 = DRIVER_hDrvr16Counter;
266 lstrcpynA(lpDrv->szAliasName, lpSFN, sizeof(lpDrv->szAliasName));
267 lpDrv->hModule16 = hModule;
268 lpDrv->lpDrvProc = lpProc;
270 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2)) goto exit;
274 TRACE("Unable to load 16 bit module (%s): %04x\n", lpFileName, hModule);
275 if (hModule >= 32) FreeLibrary16(hModule);
276 HeapFree(GetProcessHeap(), 0, lpDrv);
280 /**************************************************************************
281 * OpenDriver (USER.252)
283 HDRVR16 WINAPI OpenDriver16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
285 LPWINE_DRIVER lpDrv = NULL;
288 TRACE("(%s, %s, %08lX);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam2);
290 if (!lpDriverName || !*lpDriverName) return 0;
292 if (lpSectionName == NULL) {
293 strcpy(drvName, lpDriverName);
295 if ((lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2)))
297 /* in case hDriver is NULL, search in Drivers section */
298 lpSectionName = "Drivers";
300 if (GetPrivateProfileStringA(lpSectionName, lpDriverName, "",
301 drvName, sizeof(drvName), "SYSTEM.INI") > 0) {
302 lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2);
305 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName));
309 TRACE("=> %04x / %08lx\n", lpDrv->hDriver16, (DWORD)lpDrv);
310 return lpDrv->hDriver16;
313 /**************************************************************************
314 * CloseDriver (USER.253)
316 LRESULT WINAPI CloseDriver16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
320 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
322 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
323 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
325 if (DRIVER_RemoveFromList(lpDrv)) {
326 HeapFree(GetProcessHeap(), 0, lpDrv);
330 WARN("Failed to close driver\n");
334 /**************************************************************************
335 * GetDriverModuleHandle (USER.254)
337 HMODULE16 WINAPI GetDriverModuleHandle16(HDRVR16 hDrvr)
340 HMODULE16 hModule = 0;
342 TRACE("(%04x);\n", hDrvr);
344 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
345 hModule = lpDrv->hModule16;
347 TRACE("=> %04x\n", hModule);
351 /**************************************************************************
352 * DefDriverProc (USER.255)
354 LRESULT WINAPI DefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg,
355 LPARAM lParam1, LPARAM lParam2)
357 TRACE("devID=0x%08lx hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
358 dwDevID, hDriv, wMsg, lParam1, lParam2);
368 case DRV_QUERYCONFIGURE:
371 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK);
381 /**************************************************************************
382 * GetDriverInfo (USER.256)
384 BOOL16 WINAPI GetDriverInfo16(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
389 TRACE("(%04x, %p);\n", hDrvr, lpDrvInfo);
391 if (lpDrvInfo == NULL || lpDrvInfo->length != sizeof(DRIVERINFOSTRUCT16))
394 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
395 lpDrvInfo->hDriver = lpDrv->hDriver16;
396 lpDrvInfo->hModule = lpDrv->hModule16;
397 lstrcpynA(lpDrvInfo->szAliasName, lpDrv->szAliasName, sizeof(lpDrvInfo->szAliasName));
404 /**************************************************************************
405 * GetNextDriver (USER.257)
407 HDRVR16 WINAPI GetNextDriver16(HDRVR16 hDrvr, DWORD dwFlags)
412 TRACE("(%04x, %08lX);\n", hDrvr, dwFlags);
415 if (lpDrvItemList == NULL) {
416 FIXME("drivers list empty !\n");
417 /* FIXME: code was using DRIVER_LoadStartupDrivers(); before ?
418 * I (EPP) don't quite understand this
420 if (lpDrvItemList == NULL)
423 lpDrv = lpDrvItemList;
424 if (dwFlags & GND_REVERSE) {
425 while (lpDrv->lpNextItem)
426 lpDrv = lpDrv->lpNextItem;
429 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
430 if (dwFlags & GND_REVERSE) {
431 lpDrv = (lpDrv->lpPrevItem) ? lpDrv->lpPrevItem : NULL;
433 lpDrv = (lpDrv->lpNextItem) ? lpDrv->lpNextItem : NULL;
438 hRetDrv = (lpDrv) ? lpDrv->hDriver16 : (HDRVR16)0;
439 TRACE("return %04x !\n", hRetDrv);