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
14 #include "wine/winbase16.h"
18 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(driver);
22 typedef struct tagWINE_DRIVER
24 char szAliasName[128];
25 /* as usual LPWINE_DRIVER == hDriver32 */
28 DRIVERPROC16 lpDrvProc;
30 struct tagWINE_DRIVER* lpPrevItem;
31 struct tagWINE_DRIVER* lpNextItem;
32 } WINE_DRIVER, *LPWINE_DRIVER;
34 static LPWINE_DRIVER lpDrvItemList = NULL;
37 * - LoadModule count and clean up is not handled correctly (it's not a
38 * problem as long as FreeLibrary is not working correctly)
41 /* ### start build ### */
42 extern LONG CALLBACK DRIVER_CallTo16_long_lwwll(FARPROC16,LONG,WORD,WORD,LONG,LONG);
43 /* ### stop build ### */
45 /**************************************************************************
46 * LoadStartupDrivers [internal]
48 static void WINE_UNUSED DRIVER_LoadStartupDrivers(void)
52 if (GetPrivateProfileStringA("drivers", NULL, "", str, sizeof(str), "SYSTEM.INI") < 2) {
53 ERR("Can't find drivers section in system.ini\n");
58 for (ptr = str; *ptr; ptr += strlen(ptr) + 1) {
59 TRACE("str='%s'\n", ptr);
60 hDrv = OpenDriver16(ptr, "drivers", 0L);
61 TRACE("hDrv=%04x\n", hDrv);
63 TRACE("end of list !\n");
67 /**************************************************************************
68 * DRIVER_GetNumberOfModuleRefs [internal]
70 * Returns the number of open drivers which share the same module.
72 static WORD DRIVER_GetNumberOfModuleRefs(LPWINE_DRIVER lpNewDrv)
77 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
78 if (lpDrv->hModule16 == lpNewDrv->hModule16) {
85 /**************************************************************************
86 * DRIVER_FindFromHDrvr16 [internal]
88 * From a hDrvr being 16 bits, returns the WINE internal structure.
90 static LPWINE_DRIVER DRIVER_FindFromHDrvr16(HDRVR16 hDrvr)
94 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
95 if (lpDrv->hDriver16 == hDrvr) {
102 /**************************************************************************
103 * DRIVER_SendMessage [internal]
105 static LRESULT inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv, UINT16 msg,
106 LPARAM lParam1, LPARAM lParam2)
108 TRACE("Before CallDriverProc proc=%p driverID=%08lx wMsg=%04x p1=%08lx p2=%08lx\n",
109 lpDrv->lpDrvProc, lpDrv->dwDriverID, msg, lParam1, lParam2);
110 return DRIVER_CallTo16_long_lwwll((FARPROC16)lpDrv->lpDrvProc, lpDrv->dwDriverID,
111 lpDrv->hDriver16, msg, lParam1, lParam2);
114 /**************************************************************************
115 * SendDriverMessage (USER.251)
116 * SendDriverMessage16 (USER32.@)
118 LRESULT WINAPI SendDriverMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1,
124 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
126 if ((lpDrv = DRIVER_FindFromHDrvr16(hDriver)) != NULL) {
127 retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
129 WARN("Bad driver handle %u\n", hDriver);
132 TRACE("retval = %ld\n", retval);
136 /**************************************************************************
137 * DRIVER_RemoveFromList [internal]
139 * Generates all the logic to handle driver closure / deletion
140 * Removes a driver struct to the list of open drivers.
142 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
144 lpDrv->dwDriverID = 0;
145 if (DRIVER_GetNumberOfModuleRefs(lpDrv) == 1) {
146 DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
147 DRIVER_SendMessage(lpDrv, DRV_FREE, 0L, 0L);
150 if (lpDrv->lpPrevItem)
151 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
153 lpDrvItemList = lpDrv->lpNextItem;
154 if (lpDrv->lpNextItem)
155 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
160 /**************************************************************************
161 * DRIVER_AddToList [internal]
163 * Adds a driver struct to the list of open drivers.
164 * Generates all the logic to handle driver creation / open.
166 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
168 /* First driver to be loaded for this module, need to load correctly the module */
169 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv) == 0) {
170 if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
171 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
174 /* returned value is not checked */
175 DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
178 lpNewDrv->lpNextItem = NULL;
179 if (lpDrvItemList == NULL) {
180 lpDrvItemList = lpNewDrv;
181 lpNewDrv->lpPrevItem = NULL;
183 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
184 while (lpDrv->lpNextItem != NULL)
185 lpDrv = lpDrv->lpNextItem;
187 lpDrv->lpNextItem = lpNewDrv;
188 lpNewDrv->lpPrevItem = lpDrv;
190 /* Now just open a new instance of a driver on this module */
191 lpNewDrv->dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
193 if (lpNewDrv->dwDriverID == 0) {
194 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
195 DRIVER_RemoveFromList(lpNewDrv);
202 /**************************************************************************
203 * DRIVER_TryOpenDriver16 [internal]
205 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
207 static LPWINE_DRIVER DRIVER_TryOpenDriver16(LPCSTR lpFileName, LPARAM lParam2)
209 static WORD DRIVER_hDrvr16Counter /* = 0 */;
210 LPWINE_DRIVER lpDrv = NULL;
216 TRACE("('%s', %08lX);\n", lpFileName, lParam2);
218 if (strlen(lpFileName) < 1) return lpDrv;
220 if ((lpSFN = strrchr(lpFileName, '\\')) == NULL)
224 if ((ptr = strchr(lpFileName, ' ')) != NULL) {
226 while (*ptr == ' ') ptr++;
227 if (*ptr == '\0') ptr = NULL;
230 if ((hModule = LoadLibrary16(lpFileName)) < 32) goto exit;
231 if ((lpProc = (DRIVERPROC16)GetProcAddress16(hModule, "DRIVERPROC")) == NULL)
234 if ((lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER))) == NULL)
237 lpDrv->dwDriverID = 0;
238 while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter));
239 lpDrv->hDriver16 = DRIVER_hDrvr16Counter;
240 lstrcpynA(lpDrv->szAliasName, lpSFN, sizeof(lpDrv->szAliasName));
241 lpDrv->hModule16 = hModule;
242 lpDrv->lpDrvProc = lpProc;
244 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2)) goto exit;
248 TRACE("Unable to load 16 bit module (%s): %04x\n", lpFileName, hModule);
249 if (hModule >= 32) FreeLibrary16(hModule);
250 HeapFree(GetProcessHeap(), 0, lpDrv);
254 /**************************************************************************
255 * OpenDriver (USER.252)
256 * OpenDriver16 (USER32.@)
258 HDRVR16 WINAPI OpenDriver16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
260 LPWINE_DRIVER lpDrv = NULL;
263 TRACE("('%s', '%s', %08lX);\n", lpDriverName, lpSectionName, lParam2);
265 if (!lpDriverName || !*lpDriverName) return 0;
267 if (lpSectionName == NULL) {
268 strcpy(drvName, lpDriverName);
270 if ((lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2)))
272 /* in case hDriver is NULL, search in Drivers section */
273 lpSectionName = "Drivers";
275 if (GetPrivateProfileStringA(lpSectionName, lpDriverName, "",
276 drvName, sizeof(drvName), "SYSTEM.INI") > 0) {
277 lpDrv = DRIVER_TryOpenDriver16(drvName, lParam2);
280 TRACE("Failed to open driver %s from system.ini file, section %s\n", lpDriverName, lpSectionName);
284 TRACE("=> %04x / %08lx\n", lpDrv->hDriver16, (DWORD)lpDrv);
285 return lpDrv->hDriver16;
288 /**************************************************************************
289 * CloseDriver (USER.253)
290 * CloseDriver16 (USER32.@)
292 LRESULT WINAPI CloseDriver16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
296 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
298 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
299 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
301 if (DRIVER_RemoveFromList(lpDrv)) {
302 HeapFree(GetProcessHeap(), 0, lpDrv);
306 WARN("Failed to close driver\n");
310 /**************************************************************************
311 * GetDriverModuleHandle (USER.254)
312 * GetDriverModuleHandle16 (USER32.@)
314 HMODULE16 WINAPI GetDriverModuleHandle16(HDRVR16 hDrvr)
317 HMODULE16 hModule = 0;
319 TRACE("(%04x);\n", hDrvr);
321 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
322 hModule = lpDrv->hModule16;
324 TRACE("=> %04x\n", hModule);
328 /**************************************************************************
329 * DefDriverProc (USER.255)
330 * DefDriverProc16 (USER32.@)
332 LRESULT WINAPI DefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg,
333 LPARAM lParam1, LPARAM lParam2)
335 TRACE("devID=0x%08lx hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
336 dwDevID, hDriv, wMsg, lParam1, lParam2);
346 case DRV_QUERYCONFIGURE:
349 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK);
359 /**************************************************************************
360 * GetDriverInfo (USER.256)
362 BOOL16 WINAPI GetDriverInfo16(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
367 TRACE("(%04x, %p);\n", hDrvr, lpDrvInfo);
369 if (lpDrvInfo == NULL || lpDrvInfo->length != sizeof(DRIVERINFOSTRUCT16))
372 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
373 lpDrvInfo->hDriver = lpDrv->hDriver16;
374 lpDrvInfo->hModule = lpDrv->hModule16;
375 lstrcpynA(lpDrvInfo->szAliasName, lpDrv->szAliasName, sizeof(lpDrvInfo->szAliasName));
382 /**************************************************************************
383 * GetNextDriver (USER.257)
385 HDRVR16 WINAPI GetNextDriver16(HDRVR16 hDrvr, DWORD dwFlags)
390 TRACE("(%04x, %08lX);\n", hDrvr, dwFlags);
393 if (lpDrvItemList == NULL) {
394 FIXME("drivers list empty !\n");
395 /* FIXME: code was using DRIVER_LoadStartupDrivers(); before ?
396 * I (EPP) don't quite understand this
398 if (lpDrvItemList == NULL)
401 lpDrv = lpDrvItemList;
402 if (dwFlags & GND_REVERSE) {
403 while (lpDrv->lpNextItem)
404 lpDrv = lpDrv->lpNextItem;
407 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
408 if (dwFlags & GND_REVERSE) {
409 lpDrv = (lpDrv->lpPrevItem) ? lpDrv->lpPrevItem : NULL;
411 lpDrv = (lpDrv->lpNextItem) ? lpDrv->lpNextItem : NULL;
416 hRetDrv = (lpDrv) ? lpDrv->hDriver16 : (HDRVR16)0;
417 TRACE("return %04x !\n", hRetDrv);