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)
130 * SendDriverMessage16 (USER32.@)
132 LRESULT WINAPI SendDriverMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1,
138 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
140 if ((lpDrv = DRIVER_FindFromHDrvr16(hDriver)) != NULL) {
141 retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
143 WARN("Bad driver handle %u\n", hDriver);
146 TRACE("retval = %ld\n", retval);
150 /**************************************************************************
151 * DRIVER_RemoveFromList [internal]
153 * Generates all the logic to handle driver closure / deletion
154 * Removes a driver struct to the list of open drivers.
156 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
158 lpDrv->dwDriverID = 0;
159 if (DRIVER_GetNumberOfModuleRefs(lpDrv) == 1) {
160 DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
161 DRIVER_SendMessage(lpDrv, DRV_FREE, 0L, 0L);
164 if (lpDrv->lpPrevItem)
165 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
167 lpDrvItemList = lpDrv->lpNextItem;
168 if (lpDrv->lpNextItem)
169 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
174 /**************************************************************************
175 * DRIVER_AddToList [internal]
177 * Adds a driver struct to the list of open drivers.
178 * Generates all the logic to handle driver creation / open.
180 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
182 /* First driver to be loaded for this module, need to load correctly the module */
183 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv) == 0) {
184 if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
185 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
188 /* returned value is not checked */
189 DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
192 lpNewDrv->lpNextItem = NULL;
193 if (lpDrvItemList == NULL) {
194 lpDrvItemList = lpNewDrv;
195 lpNewDrv->lpPrevItem = NULL;
197 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
198 while (lpDrv->lpNextItem != NULL)
199 lpDrv = lpDrv->lpNextItem;
201 lpDrv->lpNextItem = lpNewDrv;
202 lpNewDrv->lpPrevItem = lpDrv;
204 /* Now just open a new instance of a driver on this module */
205 lpNewDrv->dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
207 if (lpNewDrv->dwDriverID == 0) {
208 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
209 DRIVER_RemoveFromList(lpNewDrv);
216 /**************************************************************************
217 * DRIVER_TryOpenDriver16 [internal]
219 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
221 static LPWINE_DRIVER DRIVER_TryOpenDriver16(LPCSTR lpFileName, LPARAM lParam2)
223 static WORD DRIVER_hDrvr16Counter /* = 0 */;
224 LPWINE_DRIVER lpDrv = NULL;
230 TRACE("('%s', %08lX);\n", lpFileName, lParam2);
232 if (strlen(lpFileName) < 1) return lpDrv;
234 if ((lpSFN = strrchr(lpFileName, '\\')) == NULL)
238 if ((ptr = strchr(lpFileName, ' ')) != NULL) {
240 while (*ptr == ' ') ptr++;
241 if (*ptr == '\0') ptr = NULL;
244 if ((hModule = LoadLibrary16(lpFileName)) < 32) goto exit;
245 if ((lpProc = (DRIVERPROC16)GetProcAddress16(hModule, "DRIVERPROC")) == NULL)
248 if ((lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER))) == NULL)
251 lpDrv->dwDriverID = 0;
252 while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter));
253 lpDrv->hDriver16 = DRIVER_hDrvr16Counter;
254 lstrcpynA(lpDrv->szAliasName, lpSFN, sizeof(lpDrv->szAliasName));
255 lpDrv->hModule16 = hModule;
256 lpDrv->lpDrvProc = lpProc;
258 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2)) goto exit;
262 TRACE("Unable to load 16 bit module (%s): %04x\n", lpFileName, hModule);
263 if (hModule >= 32) FreeLibrary16(hModule);
264 HeapFree(GetProcessHeap(), 0, lpDrv);
268 /**************************************************************************
269 * OpenDriver (USER.252)
270 * OpenDriver16 (USER32.@)
272 HDRVR16 WINAPI OpenDriver16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
274 LPWINE_DRIVER lpDrv = NULL;
277 TRACE("(%s, %s, %08lX);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam2);
279 if (!lpDriverName || !*lpDriverName) return 0;
281 if (lpSectionName == NULL) {
282 strcpy(drvName, lpDriverName);
284 if ((lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2)))
286 /* in case hDriver is NULL, search in Drivers section */
287 lpSectionName = "Drivers";
289 if (GetPrivateProfileStringA(lpSectionName, lpDriverName, "",
290 drvName, sizeof(drvName), "SYSTEM.INI") > 0) {
291 lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2);
294 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName));
298 TRACE("=> %04x / %08lx\n", lpDrv->hDriver16, (DWORD)lpDrv);
299 return lpDrv->hDriver16;
302 /**************************************************************************
303 * CloseDriver (USER.253)
304 * CloseDriver16 (USER32.@)
306 LRESULT WINAPI CloseDriver16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
310 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
312 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
313 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
315 if (DRIVER_RemoveFromList(lpDrv)) {
316 HeapFree(GetProcessHeap(), 0, lpDrv);
320 WARN("Failed to close driver\n");
324 /**************************************************************************
325 * GetDriverModuleHandle (USER.254)
326 * GetDriverModuleHandle16 (USER32.@)
328 HMODULE16 WINAPI GetDriverModuleHandle16(HDRVR16 hDrvr)
331 HMODULE16 hModule = 0;
333 TRACE("(%04x);\n", hDrvr);
335 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
336 hModule = lpDrv->hModule16;
338 TRACE("=> %04x\n", hModule);
342 /**************************************************************************
343 * DefDriverProc (USER.255)
344 * DefDriverProc16 (USER32.@)
346 LRESULT WINAPI DefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg,
347 LPARAM lParam1, LPARAM lParam2)
349 TRACE("devID=0x%08lx hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
350 dwDevID, hDriv, wMsg, lParam1, lParam2);
360 case DRV_QUERYCONFIGURE:
363 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK);
373 /**************************************************************************
374 * GetDriverInfo (USER.256)
376 BOOL16 WINAPI GetDriverInfo16(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
381 TRACE("(%04x, %p);\n", hDrvr, lpDrvInfo);
383 if (lpDrvInfo == NULL || lpDrvInfo->length != sizeof(DRIVERINFOSTRUCT16))
386 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
387 lpDrvInfo->hDriver = lpDrv->hDriver16;
388 lpDrvInfo->hModule = lpDrv->hModule16;
389 lstrcpynA(lpDrvInfo->szAliasName, lpDrv->szAliasName, sizeof(lpDrvInfo->szAliasName));
396 /**************************************************************************
397 * GetNextDriver (USER.257)
399 HDRVR16 WINAPI GetNextDriver16(HDRVR16 hDrvr, DWORD dwFlags)
404 TRACE("(%04x, %08lX);\n", hDrvr, dwFlags);
407 if (lpDrvItemList == NULL) {
408 FIXME("drivers list empty !\n");
409 /* FIXME: code was using DRIVER_LoadStartupDrivers(); before ?
410 * I (EPP) don't quite understand this
412 if (lpDrvItemList == NULL)
415 lpDrv = lpDrvItemList;
416 if (dwFlags & GND_REVERSE) {
417 while (lpDrv->lpNextItem)
418 lpDrv = lpDrv->lpNextItem;
421 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
422 if (dwFlags & GND_REVERSE) {
423 lpDrv = (lpDrv->lpPrevItem) ? lpDrv->lpPrevItem : NULL;
425 lpDrv = (lpDrv->lpNextItem) ? lpDrv->lpNextItem : NULL;
430 hRetDrv = (lpDrv) ? lpDrv->hDriver16 : (HDRVR16)0;
431 TRACE("return %04x !\n", hRetDrv);