Set a default size for windows when CW_USEDEFAULT is used.
[wine.git] / windows / driver.c
blobf80ce5c8615b3fd5de8fc08023c0915fe22e12f5
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 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 "driver.h"
18 #include "ldt.h"
19 #include "module.h"
20 #include "mmddk.h"
21 #include "debugtools.h"
23 DEFAULT_DEBUG_CHANNEL(driver);
25 /* The following definitions are WINE internals */
26 /* FIXME: This is a WINE internal struct and should be moved in include/wine directory
27 * Please note that WINE shares 16 and 32 bit drivers on a single list...
28 * Basically, we maintain an external double view on drivers, so that a 16 bit drivers
29 * can be loaded/used... by 32 functions transparently
32 /* Who said goofy boy ? */
33 #define WINE_DI_MAGIC 0x900F1B01
35 typedef struct tagWINE_DRIVER
37 DWORD dwMagic;
38 char szAliasName[128];
39 /* as usual LPWINE_DRIVER == hDriver32 */
40 HDRVR16 hDriver16;
41 union {
42 struct {
43 HMODULE16 hModule;
44 DRIVERPROC16 lpDrvProc;
45 } d16;
46 struct {
47 HMODULE hModule;
48 DRIVERPROC lpDrvProc;
49 } d32;
50 } d;
51 DWORD dwDriverID;
52 DWORD dwFlags;
53 struct tagWINE_DRIVER* lpPrevItem;
54 struct tagWINE_DRIVER* lpNextItem;
55 } WINE_DRIVER, *LPWINE_DRIVER;
57 LPWINE_DRIVER DRIVER_RegisterDriver16(LPCSTR, HMODULE16, DRIVERPROC16, LPARAM, BOOL);
58 LPWINE_DRIVER DRIVER_RegisterDriver32(LPCSTR, HMODULE, DRIVERPROC, LPARAM, BOOL);
60 static LPWINE_DRIVER lpDrvItemList = NULL;
62 /* TODO list :
63 * - LoadModule count and clean up is not handled correctly (it's not a
64 * problem as long as FreeLibrary is not working correctly)
65 * - shoudln't the allocations be done on a per process basis ?
66 * - split 16/32 bit functions between DLLs as windows do (16 bit in USER, 32 bit in WINMM)
69 /* ### start build ### */
70 extern LONG CALLBACK DRIVER_CallTo16_long_lwwll(FARPROC16,LONG,WORD,WORD,LONG,LONG);
71 /* ### stop build ### */
73 /**************************************************************************
74 * LoadStartupDrivers [internal]
76 static void WINE_UNUSED DRIVER_LoadStartupDrivers(void)
78 char str[256];
80 if (GetPrivateProfileStringA("drivers", NULL, "", str, sizeof(str), "SYSTEM.INI") < 2) {
81 ERR("Can't find drivers section in system.ini\n");
82 } else {
83 HDRVR16 hDrv;
84 LPSTR ptr;
86 for (ptr = str; lstrlenA(ptr) != 0; ptr += lstrlenA(ptr) + 1) {
87 TRACE("str='%s'\n", ptr);
88 hDrv = OpenDriver16(ptr, "drivers", 0L);
89 TRACE("hDrv=%04x\n", hDrv);
91 TRACE("end of list !\n");
95 /**************************************************************************
96 * DRIVER_GetNumberOfModuleRefs [internal]
98 * Returns the number of open drivers which share the same module.
100 static WORD DRIVER_GetNumberOfModuleRefs(LPWINE_DRIVER lpNewDrv)
102 LPWINE_DRIVER lpDrv;
103 WORD count = 0;
105 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
106 if (lpDrv->dwFlags & WINE_GDF_16BIT) {
107 if (lpDrv->d.d16.hModule == lpNewDrv->d.d16.hModule) {
108 count++;
110 } else {
111 if (lpDrv->d.d32.hModule == lpNewDrv->d.d32.hModule) {
112 count++;
116 return count;
119 /**************************************************************************
120 * DRIVER_FindFromHDrvr16 [internal]
122 * From a hDrvr being 16 bits, returns the WINE internal structure.
124 static LPWINE_DRIVER DRIVER_FindFromHDrvr16(HDRVR16 hDrvr)
126 LPWINE_DRIVER lpDrv;
128 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
129 if (lpDrv->hDriver16 == hDrvr) {
130 break;
133 return lpDrv;
136 /**************************************************************************
137 * DRIVER_FindFromHDrvr [internal]
139 * From a hDrvr (being 16 or 32 bits), returns the WINE internal structure.
141 static LPWINE_DRIVER DRIVER_FindFromHDrvr(HDRVR hDrvr)
143 if (!IsBadWritePtr((void*)hDrvr, sizeof(WINE_DRIVER)) &&
144 ((LPWINE_DRIVER)hDrvr)->dwMagic == WINE_DI_MAGIC) {
145 return (LPWINE_DRIVER)hDrvr;
147 return DRIVER_FindFromHDrvr16(hDrvr);
150 /**************************************************************************
151 * DRIVER_MapMsg16To32 [internal]
153 * Map a 16 bit driver message to a 32 bit driver message.
154 * 1 : ok, some memory allocated, need to call DRIVER_UnMapMsg16To32
155 * 0 : ok, no memory allocated
156 * -1 : ko, unknown message
157 * -2 : ko, memory problem
159 static int DRIVER_MapMsg16To32(WORD wMsg, DWORD* lParam1, DWORD* lParam2)
161 int ret = -1;
163 switch (wMsg) {
164 case DRV_LOAD:
165 case DRV_ENABLE:
166 case DRV_DISABLE:
167 case DRV_FREE:
168 case DRV_QUERYCONFIGURE:
169 case DRV_REMOVE:
170 case DRV_EXITSESSION:
171 case DRV_EXITAPPLICATION:
172 case DRV_POWER:
173 /* lParam1 and lParam2 are not used */
174 ret = 0;
175 break;
176 case DRV_OPEN:
177 case DRV_CLOSE:
178 /* lParam1 is a NULL terminated string */
179 /* lParam2 is a pointer to an MCI_OPEN_DRIVER_PARMS for an MCI device */
180 if (*lParam1)
181 *lParam1 = (DWORD)PTR_SEG_TO_LIN(*lParam1);
182 if (*lParam2 && wMsg == DRV_OPEN) {
183 LPMCI_OPEN_DRIVER_PARMS16 modp16 = PTR_SEG_TO_LIN(*lParam2);
184 char* ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(LPMCI_OPEN_DRIVER_PARMS16) + sizeof(MCI_OPEN_DRIVER_PARMSA));
185 LPMCI_OPEN_DRIVER_PARMSA modp32;
187 if (ptr) {
188 *(LPMCI_OPEN_DRIVER_PARMS16*)ptr = modp16;
189 modp32 = (LPMCI_OPEN_DRIVER_PARMSA)(ptr + sizeof(LPMCI_OPEN_DRIVER_PARMSA));
191 modp32->wDeviceID = modp16->wDeviceID;
192 modp32->lpstrParams = PTR_SEG_TO_LIN(modp16->lpstrParams);
193 } else {
194 return -2;
196 *lParam2 = (DWORD)modp32;
198 ret = 1;
199 break;
200 case DRV_CONFIGURE:
201 case DRV_INSTALL:
202 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO */
203 if (*lParam2) {
204 LPDRVCONFIGINFO dci32 = HeapAlloc(GetProcessHeap(), 0, sizeof(DRVCONFIGINFO));
205 LPDRVCONFIGINFO16 dci16 = PTR_SEG_TO_LIN(*lParam2);
207 if (dci32) {
208 dci32->dwDCISize = sizeof(DRVCONFIGINFO);
209 dci32->lpszDCISectionName = HEAP_strdupAtoW(GetProcessHeap(), 0, PTR_SEG_TO_LIN(dci16->lpszDCISectionName));
210 dci32->lpszDCIAliasName = HEAP_strdupAtoW(GetProcessHeap(), 0, PTR_SEG_TO_LIN(dci16->lpszDCIAliasName));
211 if (dci32->lpszDCISectionName == NULL || dci32->lpszDCIAliasName == NULL)
212 return -2;
213 } else {
214 return -2;
216 *lParam2 = (DWORD)dci32;
217 ret = 1;
218 } else {
219 ret = 0;
221 break;
222 default:
223 if (wMsg >= 0x800 && wMsg < 0x900) {
224 /* FIXME: another hack to handle MCI messages...
225 * should find a *NICE* way to integrate DRIVER_ and
226 * MCI_ mapping/unmapping functions
228 ret = 0;
229 } else {
230 FIXME("Unknown message 0x%04x\n", wMsg);
233 return ret;
236 /**************************************************************************
237 * DRIVER_MapMsg16To32 [internal]
239 * UnMap a 16 bit driver message to a 32 bit driver message.
240 * 0 : ok
241 * -1 : ko
242 * -2 : ko, memory problem
244 static int DRIVER_UnMapMsg16To32(WORD wMsg, DWORD lParam1, DWORD lParam2)
246 int ret = -1;
248 switch (wMsg) {
249 case DRV_LOAD:
250 case DRV_ENABLE:
251 case DRV_DISABLE:
252 case DRV_FREE:
253 case DRV_QUERYCONFIGURE:
254 case DRV_REMOVE:
255 case DRV_EXITSESSION:
256 case DRV_EXITAPPLICATION:
257 case DRV_POWER:
258 /* lParam1 and lParam2 are not used */
259 case DRV_OPEN:
260 case DRV_CLOSE:
261 /* lParam1 is a NULL terminated string */
262 /* lParam2 is a pointer to an MCI_OPEN_DRIVER_PARMS for an MCI device */
263 if (lParam2 && wMsg == DRV_OPEN) {
264 LPMCI_OPEN_DRIVER_PARMSA modp32 = (LPMCI_OPEN_DRIVER_PARMSA)lParam2;
265 LPMCI_OPEN_DRIVER_PARMS16 modp16 = *(LPMCI_OPEN_DRIVER_PARMS16*)(lParam2 - sizeof(LPMCI_OPEN_DRIVER_PARMSA));
267 modp16->wCustomCommandTable = modp32->wCustomCommandTable;
268 modp16->wType = modp32->wType;
269 if (!HeapFree(GetProcessHeap(), 0, modp32))
270 FIXME("bad free line=%d\n", __LINE__);
272 ret = 0;
273 break;
274 case DRV_CONFIGURE:
275 case DRV_INSTALL:
276 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO, lParam2 */
277 if (lParam2) {
278 LPDRVCONFIGINFO dci32 = (LPDRVCONFIGINFO)lParam2;
279 if (!HeapFree(GetProcessHeap(), 0, (LPVOID)dci32->lpszDCISectionName))
280 FIXME("bad free line=%d\n", __LINE__);
281 if (!HeapFree(GetProcessHeap(), 0, (LPVOID)dci32->lpszDCIAliasName))
282 FIXME("bad free line=%d\n", __LINE__);
283 if (!HeapFree(GetProcessHeap(), 0, dci32))
284 FIXME("bad free line=%d\n", __LINE__);
286 ret = 0;
287 break;
288 default:
289 if (wMsg >= 0x800 && wMsg < 0x900) {
290 /* FIXME: another hack to handle MCI messages...
291 * should find a *NICE* way to integrate DRIVER_ and
292 * MCI_ mapping/unmapping functions
294 ret = 0;
295 } else {
296 FIXME("Unknown message 0x%04x\n", wMsg);
299 return ret;
302 /**************************************************************************
303 * DRIVER_MapMsg32To16 [internal]
305 * Map a 32 bit driver message to a 16 bit driver message.
306 * 1 : ok, some memory allocated, need to call DRIVER_UnMapMsg32To16
307 * 0 : ok, no memory allocated
308 * -1 : ko, unknown message
309 * -2 : ko, memory problem
311 static int DRIVER_MapMsg32To16(WORD wMsg, DWORD* lParam1, DWORD* lParam2)
313 int ret = -1;
315 switch (wMsg) {
316 case DRV_LOAD:
317 case DRV_ENABLE:
318 case DRV_DISABLE:
319 case DRV_FREE:
320 case DRV_QUERYCONFIGURE:
321 case DRV_REMOVE:
322 case DRV_EXITSESSION:
323 case DRV_EXITAPPLICATION:
324 case DRV_POWER:
325 /* lParam1 and lParam2 are not used */
326 ret = 0;
327 break;
328 case DRV_OPEN:
329 case DRV_CLOSE:
330 /* lParam1 is a NULL terminated string */
331 /* lParam2 is a pointer to an MCI_OPEN_DRIVER_PARMS for an MCI device */
332 if (*lParam1) {
333 LPSTR str = SEGPTR_STRDUP((LPSTR)*lParam1);
334 if (str) {
335 *lParam1 = (LPARAM)SEGPTR_GET(str);
336 ret = 0;
337 } else {
338 ret = -2;
340 } else {
341 ret = 0;
343 if (*lParam2 && wMsg == DRV_OPEN) {
344 LPMCI_OPEN_DRIVER_PARMS16 modp16;
345 char* ptr = SEGPTR_ALLOC(sizeof(LPMCI_OPEN_DRIVER_PARMSA) + sizeof(MCI_OPEN_DRIVER_PARMS16));
346 LPMCI_OPEN_DRIVER_PARMSA modp32 = (LPMCI_OPEN_DRIVER_PARMSA)(*lParam2);
348 if (ptr) {
349 *(LPMCI_OPEN_DRIVER_PARMSA*)ptr = modp32;
350 modp16 = (LPMCI_OPEN_DRIVER_PARMS16)(ptr + sizeof(LPMCI_OPEN_DRIVER_PARMSA));
352 modp16->wDeviceID = modp32->wDeviceID;
353 modp16->lpstrParams = PTR_SEG_TO_LIN(modp32->lpstrParams);
354 } else {
355 return -2;
357 *lParam2 = (DWORD)SEGPTR_GET(modp16);
358 ret = 1;
360 break;
361 case DRV_CONFIGURE:
362 case DRV_INSTALL:
363 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO */
364 if (*lParam2) {
365 LPDRVCONFIGINFO16 dci16 = (LPDRVCONFIGINFO16)SEGPTR_ALLOC(sizeof(DRVCONFIGINFO16));
366 LPDRVCONFIGINFO dci32 = (LPDRVCONFIGINFO)(*lParam2);
368 if (dci16) {
369 LPSTR str1, str2;
371 dci16->dwDCISize = sizeof(DRVCONFIGINFO16);
373 if ((str1 = HEAP_strdupWtoA(GetProcessHeap(), 0, dci32->lpszDCISectionName)) != NULL &&
374 (str2 = SEGPTR_STRDUP(str1)) != NULL) {
375 dci16->lpszDCISectionName = (LPSTR)SEGPTR_GET(str2);
376 if (!HeapFree(GetProcessHeap(), 0, str1))
377 FIXME("bad free line=%d\n", __LINE__);
378 } else {
379 return -2;
381 if ((str1 = HEAP_strdupWtoA(GetProcessHeap(), 0, dci32->lpszDCIAliasName)) != NULL &&
382 (str2 = SEGPTR_STRDUP(str1)) != NULL) {
383 dci16->lpszDCIAliasName = (LPSTR)SEGPTR_GET(str2);
384 if (!HeapFree(GetProcessHeap(), 0, str1))
385 FIXME("bad free line=%d\n", __LINE__);
386 } else {
387 return -2;
389 } else {
390 return -2;
392 *lParam2 = (LPARAM)SEGPTR_GET(dci16);
393 ret = 1;
394 } else {
395 ret = 0;
397 break;
398 default:
399 if (wMsg >= 0x800 && wMsg < 0x900) {
400 /* FIXME: another hack to handle MCI messages...
401 * should find a *NICE* way to integrate DRIVER_ and
402 * MCI_ mapping/unmapping functions
404 ret = 0;
405 } else {
406 FIXME("Unknown message 0x%04x\n", wMsg);
409 return ret;
412 /**************************************************************************
413 * DRIVER_UnMapMsg32To16 [internal]
415 * UnMap a 32 bit driver message to a 16 bit driver message.
416 * 0 : ok
417 * -1 : ko
418 * -2 : ko, memory problem
420 static int DRIVER_UnMapMsg32To16(WORD wMsg, DWORD lParam1, DWORD lParam2)
422 int ret = -1;
424 switch (wMsg) {
425 case DRV_LOAD:
426 case DRV_ENABLE:
427 case DRV_DISABLE:
428 case DRV_FREE:
429 case DRV_QUERYCONFIGURE:
430 case DRV_REMOVE:
431 case DRV_EXITSESSION:
432 case DRV_EXITAPPLICATION:
433 case DRV_POWER:
434 /* lParam1 and lParam2 are not used */
435 case DRV_OPEN:
436 case DRV_CLOSE:
437 /* lParam1 is a NULL terminated string, lParam2 is unknown => may lead to some problem */
438 /* lParam2 is a pointer to an MCI_OPEN_DRIVER_PARMS for an MCI device */
439 if (lParam1) if (!SEGPTR_FREE(PTR_SEG_TO_LIN(lParam1)))
440 FIXME("bad free line=%d\n", __LINE__);
442 if (lParam2 && wMsg == DRV_OPEN) {
443 LPMCI_OPEN_DRIVER_PARMS16 modp16 = (LPMCI_OPEN_DRIVER_PARMS16)PTR_SEG_TO_LIN(lParam2);
444 LPMCI_OPEN_DRIVER_PARMSA modp32 = *(LPMCI_OPEN_DRIVER_PARMSA*)((char*)modp16 - sizeof(LPMCI_OPEN_DRIVER_PARMSA));
446 modp32->wCustomCommandTable = modp16->wCustomCommandTable;
447 modp32->wType = modp16->wType;
448 if (!SEGPTR_FREE((char*)modp16 - sizeof(LPMCI_OPEN_DRIVER_PARMSA)))
449 FIXME("bad free line=%d\n", __LINE__);
451 ret = 0;
452 break;
453 case DRV_CONFIGURE:
454 case DRV_INSTALL:
455 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO, lParam2 */
456 if (lParam2) {
457 LPDRVCONFIGINFO16 dci16 = (LPDRVCONFIGINFO16)PTR_SEG_TO_LIN(lParam2);
459 if (!SEGPTR_FREE(PTR_SEG_TO_LIN(dci16->lpszDCISectionName)))
460 FIXME("bad free line=%d\n", __LINE__);
461 if (!SEGPTR_FREE(PTR_SEG_TO_LIN(dci16->lpszDCIAliasName)))
462 FIXME("bad free line=%d\n", __LINE__);
463 if (!SEGPTR_FREE(dci16))
464 FIXME("bad free line=%d\n", __LINE__);
466 ret = 0;
467 break;
468 default:
469 if (wMsg >= 0x800 && wMsg < 0x900) {
470 /* FIXME: another hack to handle MCI messages...
471 * should find a *NICE* way to integrate DRIVER_ and
472 * MCI_ mapping/unmapping functions
474 ret = 0;
475 } else {
476 FIXME("Unknown message 0x%04x\n", wMsg);
479 return ret;
482 /**************************************************************************
483 * SendDriverMessage [USER.251]
485 LRESULT WINAPI SendDriverMessage16(HDRVR16 hDriver, UINT16 msg, LPARAM lParam1,
486 LPARAM lParam2)
488 LPWINE_DRIVER lpDrv;
489 LRESULT retval = 0;
490 int mapRet;
492 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
494 lpDrv = DRIVER_FindFromHDrvr16(hDriver);
495 if (lpDrv != NULL && lpDrv->hDriver16 == hDriver) {
496 if (lpDrv->dwFlags & WINE_GDF_16BIT) {
497 TRACE("Before CallDriverProc proc=%p driverID=%08lx hDrv=%u wMsg=%04x p1=%08lx p2=%08lx\n",
498 lpDrv->d.d16.lpDrvProc, lpDrv->dwDriverID, hDriver, msg, lParam1, lParam2);
499 retval = DRIVER_CallTo16_long_lwwll((FARPROC16)lpDrv->d.d16.lpDrvProc, lpDrv->dwDriverID,
500 hDriver, msg, lParam1, lParam2);
501 } else {
502 mapRet = DRIVER_MapMsg16To32(msg, &lParam1, &lParam2);
503 if (mapRet >= 0) {
504 TRACE("Before func32 call proc=%p driverID=%08lx hDrv=%u wMsg=%04x p1=%08lx p2=%08lx\n",
505 lpDrv->d.d32.lpDrvProc, lpDrv->dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
506 retval = lpDrv->d.d32.lpDrvProc(lpDrv->dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
507 if (mapRet >= 1) {
508 DRIVER_UnMapMsg16To32(msg, lParam1, lParam2);
510 } else {
511 retval = 0;
514 } else {
515 WARN("Bad driver handle %u\n", hDriver);
518 TRACE("retval = %ld\n", retval);
519 return retval;
522 /**************************************************************************
523 * SendDriverMessage [WINMM.19]
525 LRESULT WINAPI SendDriverMessage(HDRVR hDriver, UINT msg, LPARAM lParam1,
526 LPARAM lParam2)
528 LPWINE_DRIVER lpDrv;
529 LRESULT retval = 0;
530 int mapRet;
532 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
534 lpDrv = DRIVER_FindFromHDrvr(hDriver);
536 if (lpDrv != NULL) {
537 if (lpDrv->dwFlags & WINE_GDF_16BIT) {
538 mapRet = DRIVER_MapMsg32To16(msg, &lParam1, &lParam2);
539 if (mapRet >= 0) {
540 TRACE("Before CallDriverProc proc=%p driverID=%08lx hDrv=%u wMsg=%04x p1=%08lx p2=%08lx\n",
541 lpDrv->d.d16.lpDrvProc, lpDrv->dwDriverID, lpDrv->hDriver16, msg, lParam1, lParam2);
542 retval = DRIVER_CallTo16_long_lwwll((FARPROC16)lpDrv->d.d16.lpDrvProc, lpDrv->dwDriverID,
543 lpDrv->hDriver16, msg, lParam1, lParam2);
544 if (mapRet >= 1) {
545 DRIVER_UnMapMsg32To16(msg, lParam1, lParam2);
547 } else {
548 retval = 0;
550 } else {
551 TRACE("Before func32 call proc=%p driverID=%08lx hDrv=%u wMsg=%04x p1=%08lx p2=%08lx\n",
552 lpDrv->d.d32.lpDrvProc, lpDrv->dwDriverID, hDriver, msg, lParam1, lParam2);
553 retval = lpDrv->d.d32.lpDrvProc(lpDrv->dwDriverID, hDriver, msg, lParam1, lParam2);
555 } else {
556 WARN("Bad driver handle %u\n", hDriver);
558 TRACE("retval = %ld\n", retval);
560 return retval;
563 /**************************************************************************
564 * DRIVER_RemoveFromList [internal]
566 * Generates all the logic to handle driver closure / deletion
567 * Removes a driver struct to the list of open drivers.
569 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
571 lpDrv->dwDriverID = 0;
572 if (DRIVER_GetNumberOfModuleRefs(lpDrv) == 1) {
573 SendDriverMessage((HDRVR)lpDrv, DRV_DISABLE, 0L, 0L);
574 SendDriverMessage((HDRVR)lpDrv, DRV_FREE, 0L, 0L);
577 if (lpDrv->lpPrevItem)
578 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
579 else
580 lpDrvItemList = lpDrv->lpNextItem;
581 if (lpDrv->lpNextItem)
582 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
584 return TRUE;
587 /**************************************************************************
588 * DRIVER_AddToList [internal]
590 * Adds a driver struct to the list of open drivers.
591 * Generates all the logic to handle driver creation / open.
593 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam, BOOL bCallFrom32)
595 lpNewDrv->dwMagic = WINE_DI_MAGIC;
596 /* First driver to be loaded for this module, need to load correctly the module */
597 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv) == 0) {
598 if (SendDriverMessage((HDRVR)lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
599 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
600 return FALSE;
602 if (SendDriverMessage((HDRVR)lpNewDrv, DRV_ENABLE, 0L, 0L) != DRV_SUCCESS) {
603 TRACE("DRV_ENABLE failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
604 return FALSE;
608 lpNewDrv->lpNextItem = NULL;
609 if (lpDrvItemList == NULL) {
610 lpDrvItemList = lpNewDrv;
611 lpNewDrv->lpPrevItem = NULL;
612 } else {
613 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
614 while (lpDrv->lpNextItem != NULL)
615 lpDrv = lpDrv->lpNextItem;
617 lpDrv->lpNextItem = lpNewDrv;
618 lpNewDrv->lpPrevItem = lpDrv;
621 /* Now just open a new instance of a driver on this module */
622 if (bCallFrom32) {
623 lpNewDrv->dwDriverID = SendDriverMessage((HDRVR)lpNewDrv, DRV_OPEN, 0L, lParam);
624 } else {
625 lpNewDrv->dwDriverID = SendDriverMessage16(lpNewDrv->hDriver16, DRV_OPEN, 0L, lParam);
627 if (lpNewDrv->dwDriverID == 0) {
628 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
629 DRIVER_RemoveFromList(lpNewDrv);
630 return FALSE;
633 return TRUE;
636 /**************************************************************************
637 * DRIVER_CreateDrvr16 [internal]
639 * Creates unique ID for 16 bit drivers.
641 static HDRVR16 DRIVER_CreateDrvr16()
643 static WORD DRIVER_hDrvr16Counter = 0;
645 while (DRIVER_FindFromHDrvr16(++DRIVER_hDrvr16Counter));
646 return DRIVER_hDrvr16Counter;
649 /**************************************************************************
650 * DRIVER_CloseDriver [internal]
653 BOOL DRIVER_CloseDriver(LPWINE_DRIVER lpDrv, DWORD lParam1, DWORD lParam2)
655 if (lpDrv != NULL) {
656 SendDriverMessage((HDRVR)lpDrv, DRV_CLOSE, lParam1, lParam2);
658 if (DRIVER_RemoveFromList(lpDrv)) {
659 HeapFree(GetProcessHeap(), 0, lpDrv);
660 return TRUE;
663 WARN("Failed to close driver\n");
664 return FALSE;
667 /**************************************************************************
668 * DRIVER_RegisterDriver16 [internal]
670 * Creates all the WINE internal representations for a 16 bit driver.
671 * The driver is also open by sending the correct messages.
673 LPWINE_DRIVER DRIVER_RegisterDriver16(LPCSTR lpName, HMODULE16 hModule, DRIVERPROC16 lpProc,
674 LPARAM lParam, BOOL bCallFrom32)
676 LPWINE_DRIVER lpDrv;
678 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
679 if (lpDrv != NULL) {
680 lpDrv->dwFlags = WINE_GDF_EXIST|WINE_GDF_16BIT;
681 lpDrv->dwDriverID = 0;
682 lpDrv->hDriver16 = DRIVER_CreateDrvr16();
683 lstrcpynA(lpDrv->szAliasName, lpName, sizeof(lpDrv->szAliasName));
684 lpDrv->d.d16.hModule = hModule;
685 lpDrv->d.d16.lpDrvProc = lpProc;
687 if (!DRIVER_AddToList(lpDrv, lParam, bCallFrom32)) {
688 HeapFree(GetProcessHeap(), 0, lpDrv);
689 lpDrv = NULL;
692 return lpDrv;
695 /**************************************************************************
696 * DRIVER_RegisterDriver32 [internal]
698 * Creates all the WINE internal representations for a 32 bit driver.
699 * The driver is also open by sending the correct messages.
701 LPWINE_DRIVER DRIVER_RegisterDriver32(LPCSTR lpName, HMODULE hModule, DRIVERPROC lpProc,
702 LPARAM lParam, BOOL bCallFrom32)
704 LPWINE_DRIVER lpDrv;
706 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
707 if (lpDrv != NULL) {
708 lpDrv->dwFlags = WINE_GDF_EXIST;
709 lpDrv->dwDriverID = 0;
710 lpDrv->hDriver16 = DRIVER_CreateDrvr16();
711 lstrcpynA(lpDrv->szAliasName, lpName, sizeof(lpDrv->szAliasName));
712 lpDrv->d.d32.hModule = hModule;
713 lpDrv->d.d32.lpDrvProc = lpProc;
715 if (!DRIVER_AddToList(lpDrv, lParam, bCallFrom32)) {
716 HeapFree(GetProcessHeap(), 0, lpDrv);
717 lpDrv = NULL;
720 return lpDrv;
723 /**************************************************************************
724 * DRIVER_TryOpenDriver16 [internal]
726 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
728 static HDRVR16 DRIVER_TryOpenDriver16(LPCSTR lpFileName, LPARAM lParam, BOOL bCallFrom32)
730 LPWINE_DRIVER lpDrv = NULL;
731 LPCSTR lpSFN;
732 HMODULE16 hModule;
733 DRIVERPROC16 lpProc;
735 TRACE("('%s', %08lX, %d);\n", lpFileName, lParam, bCallFrom32);
737 if (lstrlenA(lpFileName) < 1)
738 return 0;
740 lpSFN = strrchr(lpFileName, '\\');
741 lpSFN = (lpSFN) ? (lpSFN + 1) : lpFileName;
743 if ((hModule = LoadModule16(lpFileName, (LPVOID)-1)) >= 32) {
744 if ((lpProc = (DRIVERPROC16)WIN32_GetProcAddress16(hModule, "DRIVERPROC")) != NULL) {
745 lpDrv = DRIVER_RegisterDriver16(lpSFN, hModule, lpProc, lParam, bCallFrom32);
746 } else {
747 FreeLibrary16(hModule);
748 TRACE("No DriverProc found\n");
749 lpDrv = 0;
751 } else {
752 TRACE("Unable to load 16 bit module (%s): %d\n", lpFileName, hModule);
754 return lpDrv ? lpDrv->hDriver16 : 0;
757 /**************************************************************************
758 * DRIVER_TryOpenDriver32 [internal]
760 * Tries to load a 32 bit driver whose DLL's (module) name is lpFileName.
762 static HDRVR DRIVER_TryOpenDriver32(LPCSTR lpFileName, LPARAM lParam, BOOL bCallFrom32)
764 LPWINE_DRIVER lpDrv = NULL;
765 LPCSTR lpSFN;
766 HMODULE hModule;
767 DRIVERPROC lpProc;
769 TRACE("('%s', %08lX, %d);\n", lpFileName, lParam, bCallFrom32);
771 if (lstrlenA(lpFileName) < 1)
772 return 0;
774 lpSFN = strrchr(lpFileName, '\\');
775 lpSFN = (lpSFN) ? (lpSFN + 1) : lpFileName;
777 if ((hModule = LoadLibraryA(lpFileName)) != 0) {
778 if ((lpProc = GetProcAddress(hModule, "DriverProc")) != NULL) {
779 lpDrv = DRIVER_RegisterDriver32(lpSFN, hModule, lpProc, lParam, bCallFrom32);
780 } else {
781 FreeLibrary(hModule);
782 lpDrv = 0;
783 TRACE("No DriverProc found\n");
785 } else {
786 TRACE("Unable to load 32 bit module \"%s\"\n", lpFileName);
788 TRACE("=> %p\n", lpDrv);
789 return (HDRVR)lpDrv;
792 /**************************************************************************
793 * OpenDriver16 [USER.252]
795 HDRVR16 WINAPI OpenDriver16(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam)
797 HDRVR16 hDriver = 0;
798 char drvName[128];
800 TRACE("('%s', '%s', %08lX);\n", lpDriverName, lpSectionName, lParam);
802 if (lpSectionName == NULL) {
803 hDriver = DRIVER_TryOpenDriver16(lpDriverName, lParam, FALSE);
804 if (!hDriver) {
805 hDriver = DRIVER_TryOpenDriver32(lpDriverName, lParam, FALSE);
807 if (!hDriver) {
808 /* in case hDriver is NULL, search in Drivers32 section */
809 lpSectionName = "Drivers";
812 if (!hDriver && GetPrivateProfileStringA(lpSectionName, lpDriverName, "",
813 drvName, sizeof(drvName), "SYSTEM.INI") > 0) {
814 hDriver = DRIVER_TryOpenDriver16(drvName, lParam, FALSE);
816 if (!hDriver)
817 ERR("Failed to open driver %s from section %s\n", lpDriverName, lpSectionName);
818 else
819 TRACE("=> %08x\n", hDriver);
820 return hDriver;
823 /**************************************************************************
824 * OpenDriverA [WINMM.15]
825 * (0,1,DRV_LOAD ,0 ,0)
826 * (0,1,DRV_ENABLE,0 ,0)
827 * (0,1,DRV_OPEN ,buf[256],0)
829 HDRVR WINAPI OpenDriverA(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam)
831 HDRVR hDriver = 0;
832 char drvName[128];
834 TRACE("(%s, %s, 0x%08lx);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam);
836 if (lpSectionName == NULL) {
837 lstrcpynA(drvName, lpDriverName, sizeof(drvName));
838 hDriver = DRIVER_TryOpenDriver32(lpDriverName, lParam, TRUE);
839 if (!hDriver) {
840 hDriver = DRIVER_TryOpenDriver16(lpDriverName, lParam, TRUE);
842 if (!hDriver) {
843 if (GetPrivateProfileStringA("Drivers32", lpDriverName, "", drvName,
844 sizeof(drvName), "SYSTEM.INI")) {
845 hDriver = DRIVER_TryOpenDriver32(drvName, lParam, TRUE);
849 if (!hDriver) {
850 if (GetPrivateProfileStringA("Drivers", lpDriverName, "", drvName,
851 sizeof(drvName), "SYSTEM.INI")) {
852 hDriver = DRIVER_TryOpenDriver16(drvName, lParam, TRUE);
856 } else {
857 if (GetPrivateProfileStringA(lpSectionName, lpDriverName, "", drvName,
858 sizeof(drvName), "SYSTEM.INI")) {
859 hDriver = DRIVER_TryOpenDriver32(drvName, lParam, TRUE);
860 if (!hDriver) {
861 hDriver = DRIVER_TryOpenDriver16(drvName, lParam, TRUE);
865 if (!hDriver)
866 ERR("Failed to open driver %s from section %s\n", lpDriverName, lpSectionName);
867 else
868 TRACE("=> %08x\n", hDriver);
869 return hDriver;
872 /**************************************************************************
873 * OpenDriverW [WINMM.15]
875 HDRVR WINAPI OpenDriverW(LPCWSTR lpDriverName, LPCWSTR lpSectionName, LPARAM lParam)
877 LPSTR dn = HEAP_strdupWtoA(GetProcessHeap(), 0, lpDriverName);
878 LPSTR sn = HEAP_strdupWtoA(GetProcessHeap(), 0, lpSectionName);
879 HDRVR ret = OpenDriverA(dn, sn, lParam);
881 if (dn) HeapFree(GetProcessHeap(), 0, dn);
882 if (sn) HeapFree(GetProcessHeap(), 0, sn);
883 return ret;
886 /**************************************************************************
887 * CloseDriver16 [USER.253]
889 LRESULT WINAPI CloseDriver16(HDRVR16 hDrvr, LPARAM lParam1, LPARAM lParam2)
891 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
893 return DRIVER_CloseDriver(DRIVER_FindFromHDrvr16(hDrvr), lParam1, lParam2);
896 /**************************************************************************
897 * CloseDriver [WINMM.4]
899 LRESULT WINAPI CloseDriver(HDRVR hDrvr, LPARAM lParam1, LPARAM lParam2)
901 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
903 return DRIVER_CloseDriver(DRIVER_FindFromHDrvr(hDrvr), lParam1, lParam2);
906 /**************************************************************************
907 * GetDriverModuleHandle [USER.254]
909 HMODULE16 WINAPI GetDriverModuleHandle16(HDRVR16 hDrvr)
911 LPWINE_DRIVER lpDrv;
912 HMODULE16 hModule = 0;
914 TRACE("(%04x);\n", hDrvr);
916 lpDrv = DRIVER_FindFromHDrvr16(hDrvr);
917 if (lpDrv != NULL && lpDrv->hDriver16 == hDrvr && (lpDrv->dwFlags & WINE_GDF_16BIT)) {
918 hModule = lpDrv->d.d16.hModule;
920 TRACE("=> %d\n", hModule);
921 return hModule;
924 /**************************************************************************
925 * GetDriverFlags [WINMM.13]
926 * [in] hDrvr handle to the driver
928 * Returns:
929 * 0x00000000 if hDrvr is an invalid handle
930 * 0x80000000 if hDrvr is a valid 32 bit driver
931 * 0x90000000 if hDrvr is a valid 16 bit driver
933 DWORD WINAPI GetDriverFlags(HDRVR hDrvr)
935 LPWINE_DRIVER lpDrv;
936 DWORD ret = 0;
938 TRACE("(%04x)\n", hDrvr);
940 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
941 ret = lpDrv->dwFlags;
943 return ret;
946 /**************************************************************************
947 * GetDriverModuleHandle [WINMM.14]
949 HMODULE WINAPI GetDriverModuleHandle(HDRVR hDrvr)
951 LPWINE_DRIVER lpDrv;
952 HMODULE hModule = 0;
954 TRACE("(%04x);\n", hDrvr);
956 lpDrv = DRIVER_FindFromHDrvr(hDrvr);
957 if (lpDrv != NULL && !(lpDrv->dwFlags & WINE_GDF_16BIT)) {
958 hModule = lpDrv->d.d32.hModule;
960 TRACE("=> %d\n", hModule);
961 return hModule;
964 /**************************************************************************
965 * DefDriverProc16 [USER.255]
967 LRESULT WINAPI DefDriverProc16(DWORD dwDevID, HDRVR16 hDriv, UINT16 wMsg,
968 LPARAM lParam1, LPARAM lParam2)
970 TRACE("devID=0x%08lx hDrv=0x%04x wMsg=%04x lP1=0x%08lx lP2=0x%08lx\n",
971 dwDevID, hDriv, wMsg, lParam1, lParam2);
973 switch(wMsg) {
974 case DRV_LOAD:
975 case DRV_FREE:
976 case DRV_ENABLE:
977 case DRV_DISABLE:
978 return (LRESULT)1L;
979 case DRV_OPEN:
980 case DRV_CLOSE:
981 case DRV_QUERYCONFIGURE:
982 return (LRESULT)0L;
983 case DRV_CONFIGURE:
984 MessageBoxA(0, "Driver isn't configurable !", "Wine Driver", MB_OK);
985 return (LRESULT)0L;
986 case DRV_INSTALL:
987 case DRV_REMOVE:
988 return DRV_SUCCESS;
989 default:
990 return (LRESULT)0L;
994 /**************************************************************************
995 * GetDriverInfo [USER.256]
997 BOOL16 WINAPI GetDriverInfo16(HDRVR16 hDrvr, LPDRIVERINFOSTRUCT16 lpDrvInfo)
999 LPWINE_DRIVER lpDrv;
1000 BOOL16 ret = FALSE;
1002 TRACE("(%04x, %p);\n", hDrvr, lpDrvInfo);
1004 if (lpDrvInfo == NULL || lpDrvInfo->length != sizeof(DRIVERINFOSTRUCT16))
1005 return FALSE;
1007 lpDrv = DRIVER_FindFromHDrvr16(hDrvr);
1008 if (lpDrv != NULL && lpDrv->hDriver16 == hDrvr &&
1009 (lpDrv->dwFlags & WINE_GDF_16BIT)) {
1010 lpDrvInfo->hDriver = lpDrv->hDriver16;
1011 lpDrvInfo->hModule = lpDrv->d.d16.hModule;
1012 lstrcpynA(lpDrvInfo->szAliasName, lpDrv->szAliasName, sizeof(lpDrvInfo->szAliasName));
1013 ret = TRUE;
1016 return ret;
1019 /**************************************************************************
1020 * GetNextDriver [USER.257]
1022 HDRVR16 WINAPI GetNextDriver16(HDRVR16 hDrvr, DWORD dwFlags)
1024 HDRVR16 hRetDrv = 0;
1025 LPWINE_DRIVER lpDrv;
1027 TRACE("(%04x, %08lX);\n", hDrvr, dwFlags);
1029 if (hDrvr == 0) {
1030 if (lpDrvItemList == NULL) {
1031 FIXME("drivers list empty !\n");
1032 /* FIXME: code was using DRIVER_LoadStartupDrivers(); before ?
1033 * I (EPP) don't quite understand this
1035 if (lpDrvItemList == NULL)
1036 return 0;
1038 lpDrv = lpDrvItemList;
1039 if (dwFlags & GND_REVERSE) {
1040 while (lpDrv->lpNextItem)
1041 lpDrv = lpDrv->lpNextItem;
1043 } else {
1044 lpDrv = DRIVER_FindFromHDrvr16(hDrvr);
1045 if (lpDrv != NULL) {
1046 if (dwFlags & GND_REVERSE) {
1047 lpDrv = (lpDrv->lpPrevItem) ? lpDrv->lpPrevItem : NULL;
1048 } else {
1049 lpDrv = (lpDrv->lpNextItem) ? lpDrv->lpNextItem : NULL;
1054 hRetDrv = (lpDrv) ? lpDrv->hDriver16 : (HDRVR16)0;
1055 TRACE("return %04x !\n", hRetDrv);
1056 return hRetDrv;