- Minor API files update.
[wine.git] / windows / driver.c
bloba5fc11ac44883523ccfcadfbed00c1d197b3439f
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * WINE Drivers functions
6 * Copyright 1994 Martin Ayotte
7 * Copyright 1998 Marcus Meissner
8 * Copyright 1999,2000 Eric Pouech
9 */
11 #include <string.h>
12 #include "heap.h"
13 #include "windef.h"
14 #include "wine/winbase16.h"
15 #include "wingdi.h"
16 #include "winuser.h"
17 #include "mmddk.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 */
26 HDRVR16 hDriver16;
27 HMODULE16 hModule16;
28 DRIVERPROC16 lpDrvProc;
29 DWORD dwDriverID;
30 struct tagWINE_DRIVER* lpPrevItem;
31 struct tagWINE_DRIVER* lpNextItem;
32 } WINE_DRIVER, *LPWINE_DRIVER;
34 static LPWINE_DRIVER lpDrvItemList = NULL;
36 /* TODO list :
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)
50 char str[256];
52 if (GetPrivateProfileStringA("drivers", NULL, "", str, sizeof(str), "SYSTEM.INI") < 2) {
53 ERR("Can't find drivers section in system.ini\n");
54 } else {
55 HDRVR16 hDrv;
56 LPSTR ptr;
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)
74 LPWINE_DRIVER lpDrv;
75 WORD count = 0;
77 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
78 if (lpDrv->hModule16 == lpNewDrv->hModule16) {
79 count++;
82 return count;
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)
92 LPWINE_DRIVER lpDrv;
94 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
95 if (lpDrv->hDriver16 == hDrvr) {
96 break;
99 return lpDrv;
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,
119 LPARAM lParam2)
121 LPWINE_DRIVER lpDrv;
122 LRESULT retval = 0;
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);
128 } else {
129 WARN("Bad driver handle %u\n", hDriver);
132 TRACE("retval = %ld\n", retval);
133 return 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;
152 else
153 lpDrvItemList = lpDrv->lpNextItem;
154 if (lpDrv->lpNextItem)
155 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
157 return TRUE;
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);
172 return FALSE;
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;
182 } else {
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);
196 return FALSE;
199 return TRUE;
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;
211 HMODULE16 hModule;
212 DRIVERPROC16 lpProc;
213 LPCSTR lpSFN;
214 LPSTR ptr;
216 TRACE("('%s', %08lX);\n", lpFileName, lParam2);
218 if (strlen(lpFileName) < 1) return lpDrv;
220 if ((lpSFN = strrchr(lpFileName, '\\')) == NULL)
221 lpSFN = lpFileName;
222 else
223 lpSFN++;
224 if ((ptr = strchr(lpFileName, ' ')) != NULL) {
225 *ptr++ = '\0';
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)
232 goto exit;
234 if ((lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER))) == NULL)
235 goto exit;
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;
246 return lpDrv;
247 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);
251 return NULL;
254 /**************************************************************************
255 * OpenDriver (USER.252)
256 * OpenDriver16 (USER32.@)
258 HDRVR16 WINAPI OpenDriver16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
260 LPWINE_DRIVER lpDrv = NULL;
261 char drvName[128];
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)))
271 goto the_end;
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);
279 if (!lpDrv) {
280 TRACE("Failed to open driver %s from system.ini file, section %s\n", lpDriverName, lpSectionName);
281 return 0;
283 the_end:
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)
294 LPWINE_DRIVER lpDrv;
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);
303 return TRUE;
306 WARN("Failed to close driver\n");
307 return FALSE;
310 /**************************************************************************
311 * GetDriverModuleHandle (USER.254)
312 * GetDriverModuleHandle16 (USER32.@)
314 HMODULE16 WINAPI GetDriverModuleHandle16(HDRVR16 hDrvr)
316 LPWINE_DRIVER lpDrv;
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);
325 return 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);
338 switch(wMsg) {
339 case DRV_LOAD:
340 case DRV_FREE:
341 case DRV_ENABLE:
342 case DRV_DISABLE:
343 return (LRESULT)1L;
344 case DRV_OPEN:
345 case DRV_CLOSE:
346 case DRV_QUERYCONFIGURE:
347 return (LRESULT)0L;
348 case DRV_CONFIGURE:
349 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK);
350 return (LRESULT)0L;
351 case DRV_INSTALL:
352 case DRV_REMOVE:
353 return DRV_SUCCESS;
354 default:
355 return (LRESULT)0L;
359 /**************************************************************************
360 * GetDriverInfo (USER.256)
362 BOOL16 WINAPI GetDriverInfo16(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
364 LPWINE_DRIVER lpDrv;
365 BOOL16 ret = FALSE;
367 TRACE("(%04x, %p);\n", hDrvr, lpDrvInfo);
369 if (lpDrvInfo == NULL || lpDrvInfo->length != sizeof(DRIVERINFOSTRUCT16))
370 return FALSE;
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));
376 ret = TRUE;
379 return ret;
382 /**************************************************************************
383 * GetNextDriver (USER.257)
385 HDRVR16 WINAPI GetNextDriver16(HDRVR16 hDrvr, DWORD dwFlags)
387 HDRVR16 hRetDrv = 0;
388 LPWINE_DRIVER lpDrv;
390 TRACE("(%04x, %08lX);\n", hDrvr, dwFlags);
392 if (hDrvr == 0) {
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)
399 return 0;
401 lpDrv = lpDrvItemList;
402 if (dwFlags & GND_REVERSE) {
403 while (lpDrv->lpNextItem)
404 lpDrv = lpDrv->lpNextItem;
406 } else {
407 if ((lpDrv = DRIVER_FindFromHDrvr16(hDrvr)) != NULL) {
408 if (dwFlags & GND_REVERSE) {
409 lpDrv = (lpDrv->lpPrevItem) ? lpDrv->lpPrevItem : NULL;
410 } else {
411 lpDrv = (lpDrv->lpNextItem) ? lpDrv->lpNextItem : NULL;
416 hRetDrv = (lpDrv) ? lpDrv->hDriver16 : (HDRVR16)0;
417 TRACE("return %04x !\n", hRetDrv);
418 return hRetDrv;