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"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(driver);
36 typedef struct tagWINE_DRIVER
38 char szAliasName[128];
39 /* as usual LPWINE_DRIVER == hDriver32 */
42 DRIVERPROC16 lpDrvProc;
44 struct tagWINE_DRIVER* lpPrevItem;
45 struct tagWINE_DRIVER* lpNextItem;
46 } WINE_DRIVER, *LPWINE_DRIVER;
48 static LPWINE_DRIVER lpDrvItemList = NULL;
51 * - LoadModule count and clean up is not handled correctly (it's not a
52 * problem as long as FreeLibrary is not working correctly)
55 /* ### start build ### */
56 extern LONG CALLBACK DRIVER_CallTo16_long_lwwll(FARPROC16,LONG,WORD,WORD,LONG,LONG);
57 /* ### stop build ### */
59 /**************************************************************************
60 * LoadStartupDrivers [internal]
62 static void WINE_UNUSED DRIVER_LoadStartupDrivers(void)
66 if (GetPrivateProfileStringA("drivers", NULL, "", str, sizeof(str), "SYSTEM.INI") < 2) {
67 ERR("Can't find drivers section in system.ini\n");
72 for (ptr = str; *ptr; ptr += strlen(ptr) + 1) {
73 TRACE("str='%s'\n", ptr);
74 hDrv = OpenDriver16(ptr, "drivers", 0L);
75 TRACE("hDrv=%04x\n", hDrv);
77 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)
122 TRACE("Before CallDriverProc proc=%p driverID=%08lx wMsg=%04x p1=%08lx p2=%08lx\n",
123 lpDrv->lpDrvProc, lpDrv->dwDriverID, msg, lParam1, lParam2);
124 return DRIVER_CallTo16_long_lwwll((FARPROC16)lpDrv->lpDrvProc, lpDrv->dwDriverID,
125 lpDrv->hDriver16, msg, lParam1, lParam2);
128 /**************************************************************************
129 * SendDriverMessage (USER.251)
131 LRESULT WINAPI SendDriverMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1,
137 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
139 if ((lpDrv = DRIVER_FindFromHDrvr16(hDriver)) != NULL) {
140 retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
142 WARN("Bad driver handle %u\n", hDriver);
145 TRACE("retval = %ld\n", retval);
149 /**************************************************************************
150 * DRIVER_RemoveFromList [internal]
152 * Generates all the logic to handle driver closure / deletion
153 * Removes a driver struct to the list of open drivers.
155 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
157 lpDrv->dwDriverID = 0;
158 if (DRIVER_GetNumberOfModuleRefs(lpDrv) == 1) {
159 DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
160 DRIVER_SendMessage(lpDrv, DRV_FREE, 0L, 0L);
163 if (lpDrv->lpPrevItem)
164 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
166 lpDrvItemList = lpDrv->lpNextItem;
167 if (lpDrv->lpNextItem)
168 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
173 /**************************************************************************
174 * DRIVER_AddToList [internal]
176 * Adds a driver struct to the list of open drivers.
177 * Generates all the logic to handle driver creation / open.
179 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
181 /* First driver to be loaded for this module, need to load correctly the module */
182 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv) == 0) {
183 if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
184 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
187 /* returned value is not checked */
188 DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
191 lpNewDrv->lpNextItem = NULL;
192 if (lpDrvItemList == NULL) {
193 lpDrvItemList = lpNewDrv;
194 lpNewDrv->lpPrevItem = NULL;
196 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
197 while (lpDrv->lpNextItem != NULL)
198 lpDrv = lpDrv->lpNextItem;
200 lpDrv->lpNextItem = lpNewDrv;
201 lpNewDrv->lpPrevItem = lpDrv;
203 /* Now just open a new instance of a driver on this module */
204 lpNewDrv->dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
206 if (lpNewDrv->dwDriverID == 0) {
207 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
208 DRIVER_RemoveFromList(lpNewDrv);
215 /**************************************************************************
216 * DRIVER_TryOpenDriver16 [internal]
218 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
220 static LPWINE_DRIVER DRIVER_TryOpenDriver16(LPCSTR lpFileName, LPARAM lParam2)
222 static WORD DRIVER_hDrvr16Counter /* = 0 */;
223 LPWINE_DRIVER lpDrv = NULL;
229 TRACE("('%s', %08lX);\n", lpFileName, lParam2);
231 if (strlen(lpFileName) < 1) return lpDrv;
233 if ((lpSFN = strrchr(lpFileName, '\\')) == NULL)
237 if ((ptr = strchr(lpFileName, ' ')) != NULL) {
239 while (*ptr == ' ') ptr++;
240 if (*ptr == '\0') ptr = NULL;
243 if ((hModule = LoadLibrary16(lpFileName)) < 32) goto exit;
244 if ((lpProc = (DRIVERPROC16)GetProcAddress16(hModule, "DRIVERPROC")) == NULL)
247 if ((lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER))) == NULL)
250 lpDrv->dwDriverID = 0;
251 while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter));
252 lpDrv->hDriver16 = DRIVER_hDrvr16Counter;
253 lstrcpynA(lpDrv->szAliasName, lpSFN, sizeof(lpDrv->szAliasName));
254 lpDrv->hModule16 = hModule;
255 lpDrv->lpDrvProc = lpProc;
257 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2)) goto exit;
261 TRACE("Unable to load 16 bit module (%s): %04x\n", lpFileName, hModule);
262 if (hModule >= 32) FreeLibrary16(hModule);
263 HeapFree(GetProcessHeap(), 0, lpDrv);
267 /**************************************************************************
268 * OpenDriver (USER.252)
270 HDRVR16 WINAPI OpenDriver16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
272 LPWINE_DRIVER lpDrv = NULL;
275 TRACE("(%s, %s, %08lX);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam2);
277 if (!lpDriverName || !*lpDriverName) return 0;
279 if (lpSectionName == NULL) {
280 strcpy(drvName, lpDriverName);
282 if ((lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2)))
284 /* in case hDriver is NULL, search in Drivers section */
285 lpSectionName = "Drivers";
287 if (GetPrivateProfileStringA(lpSectionName, lpDriverName, "",
288 drvName, sizeof(drvName), "SYSTEM.INI") > 0) {
289 lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2);
292 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName));
296 TRACE("=> %04x / %08lx\n", lpDrv->hDriver16, (DWORD)lpDrv);
297 return lpDrv->hDriver16;
300 /**************************************************************************
301 * CloseDriver (USER.253)
303 LRESULT WINAPI CloseDriver16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
307 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
309 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
310 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
312 if (DRIVER_RemoveFromList(lpDrv)) {
313 HeapFree(GetProcessHeap(), 0, lpDrv);
317 WARN("Failed to close driver\n");
321 /**************************************************************************
322 * GetDriverModuleHandle (USER.254)
324 HMODULE16 WINAPI GetDriverModuleHandle16(HDRVR16 hDrvr)
327 HMODULE16 hModule = 0;
329 TRACE("(%04x);\n", hDrvr);
331 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
332 hModule = lpDrv->hModule16;
334 TRACE("=> %04x\n", hModule);
338 /**************************************************************************
339 * DefDriverProc (USER.255)
341 LRESULT WINAPI DefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg,
342 LPARAM lParam1, LPARAM lParam2)
344 TRACE("devID=0x%08lx hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
345 dwDevID, hDriv, wMsg, lParam1, lParam2);
355 case DRV_QUERYCONFIGURE:
358 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK);
368 /**************************************************************************
369 * GetDriverInfo (USER.256)
371 BOOL16 WINAPI GetDriverInfo16(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
376 TRACE("(%04x, %p);\n", hDrvr, lpDrvInfo);
378 if (lpDrvInfo == NULL || lpDrvInfo->length != sizeof(DRIVERINFOSTRUCT16))
381 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
382 lpDrvInfo->hDriver = lpDrv->hDriver16;
383 lpDrvInfo->hModule = lpDrv->hModule16;
384 lstrcpynA(lpDrvInfo->szAliasName, lpDrv->szAliasName, sizeof(lpDrvInfo->szAliasName));
391 /**************************************************************************
392 * GetNextDriver (USER.257)
394 HDRVR16 WINAPI GetNextDriver16(HDRVR16 hDrvr, DWORD dwFlags)
399 TRACE("(%04x, %08lX);\n", hDrvr, dwFlags);
402 if (lpDrvItemList == NULL) {
403 FIXME("drivers list empty !\n");
404 /* FIXME: code was using DRIVER_LoadStartupDrivers(); before ?
405 * I (EPP) don't quite understand this
407 if (lpDrvItemList == NULL)
410 lpDrv = lpDrvItemList;
411 if (dwFlags & GND_REVERSE) {
412 while (lpDrv->lpNextItem)
413 lpDrv = lpDrv->lpNextItem;
416 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
417 if (dwFlags & GND_REVERSE) {
418 lpDrv = (lpDrv->lpPrevItem) ? lpDrv->lpPrevItem : NULL;
420 lpDrv = (lpDrv->lpNextItem) ? lpDrv->lpNextItem : NULL;
425 hRetDrv = (lpDrv) ? lpDrv->hDriver16 : (HDRVR16)0;
426 TRACE("return %04x !\n", hRetDrv);