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/mmsystem16.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(driver);
37 typedef struct tagWINE_DRIVER
39 char szAliasName[128];
40 /* as usual LPWINE_DRIVER == hDriver32 */
43 DRIVERPROC16 lpDrvProc;
45 struct tagWINE_DRIVER* lpPrevItem;
46 struct tagWINE_DRIVER* lpNextItem;
47 } WINE_DRIVER, *LPWINE_DRIVER;
49 static LPWINE_DRIVER lpDrvItemList = NULL;
52 * - LoadModule count and clean up is not handled correctly (it's not a
53 * problem as long as FreeLibrary is not working correctly)
56 /* ### start build ### */
57 extern LONG CALLBACK DRIVER_CallTo16_long_lwwll(FARPROC16,LONG,WORD,WORD,LONG,LONG);
58 /* ### stop build ### */
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)
123 TRACE("Before CallDriverProc proc=%p driverID=%08lx wMsg=%04x p1=%08lx p2=%08lx\n",
124 lpDrv->lpDrvProc, lpDrv->dwDriverID, msg, lParam1, lParam2);
125 return DRIVER_CallTo16_long_lwwll((FARPROC16)lpDrv->lpDrvProc, lpDrv->dwDriverID,
126 lpDrv->hDriver16, msg, lParam1, lParam2);
129 /**************************************************************************
130 * SendDriverMessage (USER.251)
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)
271 HDRVR16 WINAPI OpenDriver16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
273 LPWINE_DRIVER lpDrv = NULL;
276 TRACE("(%s, %s, %08lX);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam2);
278 if (!lpDriverName || !*lpDriverName) return 0;
280 if (lpSectionName == NULL) {
281 strcpy(drvName, lpDriverName);
283 if ((lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2)))
285 /* in case hDriver is NULL, search in Drivers section */
286 lpSectionName = "Drivers";
288 if (GetPrivateProfileStringA(lpSectionName, lpDriverName, "",
289 drvName, sizeof(drvName), "SYSTEM.INI") > 0) {
290 lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2);
293 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName));
297 TRACE("=> %04x / %08lx\n", lpDrv->hDriver16, (DWORD)lpDrv);
298 return lpDrv->hDriver16;
301 /**************************************************************************
302 * CloseDriver (USER.253)
304 LRESULT WINAPI CloseDriver16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
308 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
310 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
311 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
313 if (DRIVER_RemoveFromList(lpDrv)) {
314 HeapFree(GetProcessHeap(), 0, lpDrv);
318 WARN("Failed to close driver\n");
322 /**************************************************************************
323 * GetDriverModuleHandle (USER.254)
325 HMODULE16 WINAPI GetDriverModuleHandle16(HDRVR16 hDrvr)
328 HMODULE16 hModule = 0;
330 TRACE("(%04x);\n", hDrvr);
332 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
333 hModule = lpDrv->hModule16;
335 TRACE("=> %04x\n", hModule);
339 /**************************************************************************
340 * DefDriverProc (USER.255)
342 LRESULT WINAPI DefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg,
343 LPARAM lParam1, LPARAM lParam2)
345 TRACE("devID=0x%08lx hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
346 dwDevID, hDriv, wMsg, lParam1, lParam2);
356 case DRV_QUERYCONFIGURE:
359 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK);
369 /**************************************************************************
370 * GetDriverInfo (USER.256)
372 BOOL16 WINAPI GetDriverInfo16(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
377 TRACE("(%04x, %p);\n", hDrvr, lpDrvInfo);
379 if (lpDrvInfo == NULL || lpDrvInfo->length != sizeof(DRIVERINFOSTRUCT16))
382 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
383 lpDrvInfo->hDriver = lpDrv->hDriver16;
384 lpDrvInfo->hModule = lpDrv->hModule16;
385 lstrcpynA(lpDrvInfo->szAliasName, lpDrv->szAliasName, sizeof(lpDrvInfo->szAliasName));
392 /**************************************************************************
393 * GetNextDriver (USER.257)
395 HDRVR16 WINAPI GetNextDriver16(HDRVR16 hDrvr, DWORD dwFlags)
400 TRACE("(%04x, %08lX);\n", hDrvr, dwFlags);
403 if (lpDrvItemList == NULL) {
404 FIXME("drivers list empty !\n");
405 /* FIXME: code was using DRIVER_LoadStartupDrivers(); before ?
406 * I (EPP) don't quite understand this
408 if (lpDrvItemList == NULL)
411 lpDrv = lpDrvItemList;
412 if (dwFlags & GND_REVERSE) {
413 while (lpDrv->lpNextItem)
414 lpDrv = lpDrv->lpNextItem;
417 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
418 if (dwFlags & GND_REVERSE) {
419 lpDrv = (lpDrv->lpPrevItem) ? lpDrv->lpPrevItem : NULL;
421 lpDrv = (lpDrv->lpNextItem) ? lpDrv->lpNextItem : NULL;
426 hRetDrv = (lpDrv) ? lpDrv->hDriver16 : (HDRVR16)0;
427 TRACE("return %04x !\n", hRetDrv);