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 Eric Pouech
16 #include "wine/winbase16.h"
17 #include "debugtools.h"
19 DEFAULT_DEBUG_CHANNEL(driver);
21 static LPWINE_DRIVER lpDrvItemList = NULL;
24 * - LoadModule count and clean up is not handled correctly (it's not a
25 * problem as long as FreeLibrary is not working correctly)
28 /**************************************************************************
29 * DRIVER_GetNumberOfModuleRefs [internal]
31 * Returns the number of open drivers which share the same module.
33 static WORD DRIVER_GetNumberOfModuleRefs(LPWINE_DRIVER lpNewDrv)
38 if (lpNewDrv->dwFlags & WINE_GDF_16BIT) ERR("OOOch\n");
39 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
40 if (!(lpDrv->dwFlags & WINE_GDF_16BIT) &&
41 lpDrv->d.d32.hModule == lpNewDrv->d.d32.hModule) {
48 /**************************************************************************
49 * DRIVER_FindFromHDrvr [internal]
51 * From a hDrvr being 32 bits, returns the WINE internal structure.
53 LPWINE_DRIVER DRIVER_FindFromHDrvr(HDRVR hDrvr)
55 LPWINE_DRIVER d = (LPWINE_DRIVER)hDrvr;
57 if (hDrvr && HeapValidate(GetProcessHeap(), 0, d) && d->dwMagic == WINE_DI_MAGIC) {
63 /**************************************************************************
64 * DRIVER_MapMsg32To16 [internal]
66 * Map a 32 bit driver message to a 16 bit driver message.
67 * 1 : ok, some memory allocated, need to call DRIVER_UnMapMsg32To16
68 * 0 : ok, no memory allocated
69 * -1 : ko, unknown message
70 * -2 : ko, memory problem
72 static int DRIVER_MapMsg32To16(WORD wMsg, DWORD* lParam1, DWORD* lParam2)
81 case DRV_QUERYCONFIGURE:
84 case DRV_EXITAPPLICATION:
86 case DRV_CLOSE: /* should be 0/0 */
87 case DRV_OPEN: /* pass through */
88 /* lParam1 and lParam2 are not used */
94 /* lParam1 is a handle to a window (conf) or to a driver (inst) or not used,
95 * lParam2 is a pointer to DRVCONFIGINFO
98 LPDRVCONFIGINFO16 dci16 = (LPDRVCONFIGINFO16)SEGPTR_ALLOC(sizeof(DRVCONFIGINFO16));
99 LPDRVCONFIGINFO dci32 = (LPDRVCONFIGINFO)(*lParam2);
104 dci16->dwDCISize = sizeof(DRVCONFIGINFO16);
106 if ((str1 = HEAP_strdupWtoA(GetProcessHeap(), 0, dci32->lpszDCISectionName)) != NULL &&
107 (str2 = SEGPTR_STRDUP(str1)) != NULL) {
108 dci16->lpszDCISectionName = SEGPTR_GET(str2);
109 if (!HeapFree(GetProcessHeap(), 0, str1))
110 FIXME("bad free line=%d\n", __LINE__);
114 if ((str1 = HEAP_strdupWtoA(GetProcessHeap(), 0, dci32->lpszDCIAliasName)) != NULL &&
115 (str2 = SEGPTR_STRDUP(str1)) != NULL) {
116 dci16->lpszDCIAliasName = SEGPTR_GET(str2);
117 if (!HeapFree(GetProcessHeap(), 0, str1))
118 FIXME("bad free line=%d\n", __LINE__);
125 *lParam2 = (LPARAM)SEGPTR_GET(dci16);
132 if (!((wMsg >= 0x800 && wMsg < 0x900) || (wMsg >= 0x4000 && wMsg < 0x4100))) {
133 FIXME("Unknown message 0x%04x\n", wMsg);
140 /**************************************************************************
141 * DRIVER_UnMapMsg32To16 [internal]
143 * UnMap a 32 bit driver message to a 16 bit driver message.
146 * -2 : ko, memory problem
148 static int DRIVER_UnMapMsg32To16(WORD wMsg, DWORD lParam1, DWORD lParam2)
157 case DRV_QUERYCONFIGURE:
159 case DRV_EXITSESSION:
160 case DRV_EXITAPPLICATION:
164 /* lParam1 and lParam2 are not used */
168 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO, lParam2 */
170 LPDRVCONFIGINFO16 dci16 = MapSL(lParam2);
172 if (!SEGPTR_FREE(MapSL(dci16->lpszDCISectionName)))
173 FIXME("bad free line=%d\n", __LINE__);
174 if (!SEGPTR_FREE(MapSL(dci16->lpszDCIAliasName)))
175 FIXME("bad free line=%d\n", __LINE__);
176 if (!SEGPTR_FREE(dci16))
177 FIXME("bad free line=%d\n", __LINE__);
182 if (!((wMsg >= 0x800 && wMsg < 0x900) || (wMsg >= 0x4000 && wMsg < 0x4100))) {
183 FIXME("Unknown message 0x%04x\n", wMsg);
190 /**************************************************************************
191 * DRIVER_SendMessage [internal]
193 static LRESULT inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv, UINT msg,
194 LPARAM lParam1, LPARAM lParam2)
196 if (lpDrv->dwFlags & WINE_GDF_16BIT) {
199 TRACE("Before sdm16 call hDrv=%04x wMsg=%04x p1=%08lx p2=%08lx\n",
200 lpDrv->d.d16.hDriver16, msg, lParam1, lParam2);
202 if ((map = DRIVER_MapMsg32To16(msg, &lParam1, &lParam2)) >= 0) {
203 ret = SendDriverMessage16(lpDrv->d.d16.hDriver16, msg, lParam1, lParam2);
205 DRIVER_UnMapMsg32To16(msg, lParam1, lParam2);
211 TRACE("Before func32 call proc=%p driverID=%08lx hDrv=%08x wMsg=%04x p1=%08lx p2=%08lx\n",
212 lpDrv->d.d32.lpDrvProc, lpDrv->d.d32.dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
213 return lpDrv->d.d32.lpDrvProc(lpDrv->d.d32.dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
216 /**************************************************************************
217 * SendDriverMessage [WINMM.@]
218 * DrvSendMessage [WINMM.@]
220 LRESULT WINAPI SendDriverMessage(HDRVR hDriver, UINT msg, LPARAM lParam1,
226 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
228 if ((lpDrv = DRIVER_FindFromHDrvr(hDriver)) != NULL) {
229 retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
231 WARN("Bad driver handle %u\n", hDriver);
233 TRACE("retval = %ld\n", retval);
238 /**************************************************************************
239 * DRIVER_RemoveFromList [internal]
241 * Generates all the logic to handle driver closure / deletion
242 * Removes a driver struct to the list of open drivers.
244 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
246 if (!(lpDrv->dwFlags & WINE_GDF_16BIT)) {
247 if (DRIVER_GetNumberOfModuleRefs(lpDrv) == 1) {
248 DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
249 DRIVER_SendMessage(lpDrv, DRV_FREE, 0L, 0L);
253 if (lpDrv->lpPrevItem)
254 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
256 lpDrvItemList = lpDrv->lpNextItem;
257 if (lpDrv->lpNextItem)
258 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
263 /**************************************************************************
264 * DRIVER_AddToList [internal]
266 * Adds a driver struct to the list of open drivers.
267 * Generates all the logic to handle driver creation / open.
269 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
271 lpNewDrv->dwMagic = WINE_DI_MAGIC;
272 /* First driver to be loaded for this module, need to load correctly the module */
273 if (!(lpNewDrv->dwFlags & WINE_GDF_16BIT)) {
274 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv) == 0) {
275 if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
276 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
279 /* returned value is not checked */
280 DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
284 lpNewDrv->lpNextItem = NULL;
285 if (lpDrvItemList == NULL) {
286 lpDrvItemList = lpNewDrv;
287 lpNewDrv->lpPrevItem = NULL;
289 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
290 while (lpDrv->lpNextItem != NULL)
291 lpDrv = lpDrv->lpNextItem;
293 lpDrv->lpNextItem = lpNewDrv;
294 lpNewDrv->lpPrevItem = lpDrv;
297 if (!(lpNewDrv->dwFlags & WINE_GDF_16BIT)) {
298 /* Now just open a new instance of a driver on this module */
299 lpNewDrv->d.d32.dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
301 if (lpNewDrv->d.d32.dwDriverID == 0) {
302 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
303 DRIVER_RemoveFromList(lpNewDrv);
310 /**************************************************************************
311 * DRIVER_GetLibName [internal]
314 BOOL DRIVER_GetLibName(LPCSTR keyName, LPCSTR sectName, LPSTR buf, int sz)
316 /* should also do some registry diving */
317 return GetPrivateProfileStringA(sectName, keyName, "", buf, sz, "SYSTEM.INI");
320 /**************************************************************************
321 * DRIVER_TryOpenDriver32 [internal]
323 * Tries to load a 32 bit driver whose DLL's (module) name is fn
325 LPWINE_DRIVER DRIVER_TryOpenDriver32(LPCSTR fn, LPARAM lParam2)
327 LPWINE_DRIVER lpDrv = NULL;
332 TRACE("('%s', %08lX);\n", fn, lParam2);
334 if ((ptr = strchr(fn, ' ')) != NULL) {
336 while (*ptr == ' ') ptr++;
337 if (*ptr == '\0') ptr = NULL;
340 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
341 if (lpDrv == NULL) {cause = "OOM"; goto exit;}
343 if ((hModule = LoadLibraryA(fn)) == 0) {cause = "Not a 32 bit lib"; goto exit;}
345 lpDrv->d.d32.lpDrvProc = (DRIVERPROC)GetProcAddress(hModule, "DriverProc");
346 if (lpDrv->d.d32.lpDrvProc == NULL) {cause = "no DriverProc"; goto exit;}
349 lpDrv->d.d32.hModule = hModule;
350 lpDrv->d.d32.dwDriverID = 0;
352 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2)) {cause = "load faile"; goto exit;}
354 TRACE("=> %p\n", lpDrv);
357 FreeLibrary(hModule);
358 HeapFree(GetProcessHeap(), 0, lpDrv);
359 TRACE("Unable to load 32 bit module \"%s\": %s\n", fn, cause);
363 /**************************************************************************
364 * DRIVER_TryOpenDriver16 [internal]
366 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
368 static LPWINE_DRIVER DRIVER_TryOpenDriver16(LPCSTR fn, LPCSTR sn, LPARAM lParam2)
370 LPWINE_DRIVER lpDrv = NULL;
373 TRACE("('%s', %08lX);\n", sn, lParam2);
375 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
376 if (lpDrv == NULL) {cause = "OOM"; goto exit;}
378 /* FIXME: shall we do some black magic here on sn ?
379 * drivers32 => drivers
383 lpDrv->d.d16.hDriver16 = OpenDriver16(fn, sn, lParam2);
384 if (lpDrv->d.d16.hDriver16 == 0) {cause = "Not a 16 bit driver"; goto exit;}
385 lpDrv->dwFlags = WINE_GDF_16BIT;
387 if (!DRIVER_AddToList(lpDrv, 0, lParam2)) {cause = "load faile"; goto exit;}
389 TRACE("=> %p\n", lpDrv);
392 HeapFree(GetProcessHeap(), 0, lpDrv);
393 TRACE("Unable to load 32 bit module \"%s\": %s\n", fn, cause);
397 /**************************************************************************
398 * OpenDriverA [WINMM.@]
400 * (0,1,DRV_LOAD ,0 ,0)
401 * (0,1,DRV_ENABLE,0 ,0)
402 * (0,1,DRV_OPEN ,buf[256],0)
404 HDRVR WINAPI OpenDriverA(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
406 LPWINE_DRIVER lpDrv = NULL;
408 LPCSTR lsn = lpSectionName;
410 TRACE("(%s, %s, 0x%08lx);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam2);
413 lstrcpynA(libName, lpDriverName, sizeof(libName));
415 if ((lpDrv = DRIVER_TryOpenDriver32(libName, lParam2)))
419 if (DRIVER_GetLibName(lpDriverName, lsn, libName, sizeof(libName)) &&
420 (lpDrv = DRIVER_TryOpenDriver32(libName, lParam2)))
423 if (!(lpDrv = DRIVER_TryOpenDriver16(lpDriverName, lpSectionName, lParam2)))
424 TRACE("Failed to open driver %s from system.ini file, section %s\n", lpDriverName, lpSectionName);
426 if (lpDrv) TRACE("=> %08lx\n", (DWORD)lpDrv);
430 /**************************************************************************
431 * OpenDriver [WINMM.@]
434 HDRVR WINAPI OpenDriverW(LPCWSTR lpDriverName, LPCWSTR lpSectionName, LPARAM lParam)
436 LPSTR dn = HEAP_strdupWtoA(GetProcessHeap(), 0, lpDriverName);
437 LPSTR sn = HEAP_strdupWtoA(GetProcessHeap(), 0, lpSectionName);
438 HDRVR ret = OpenDriverA(dn, sn, lParam);
440 if (dn) HeapFree(GetProcessHeap(), 0, dn);
441 if (sn) HeapFree(GetProcessHeap(), 0, sn);
445 /**************************************************************************
446 * CloseDriver [WINMM.@]
449 LRESULT WINAPI CloseDriver(HDRVR hDrvr, LPARAM lParam1, LPARAM lParam2)
453 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
455 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
456 if (lpDrv->dwFlags & WINE_GDF_16BIT)
457 CloseDriver16(lpDrv->d.d16.hDriver16, lParam1, lParam2);
459 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
460 if (DRIVER_RemoveFromList(lpDrv)) {
461 HeapFree(GetProcessHeap(), 0, lpDrv);
465 WARN("Failed to close driver\n");
469 /**************************************************************************
470 * GetDriverFlags [WINMM.@]
471 * [in] hDrvr handle to the driver
474 * 0x00000000 if hDrvr is an invalid handle
475 * 0x80000000 if hDrvr is a valid 32 bit driver
476 * 0x90000000 if hDrvr is a valid 16 bit driver
478 * native WINMM doesn't return those flags
479 * 0x80000000 for a valid 32 bit driver and that's it
480 * (I may have mixed up the two flags :-(
482 DWORD WINAPI GetDriverFlags(HDRVR hDrvr)
487 TRACE("(%04x)\n", hDrvr);
489 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
490 ret = WINE_GDF_EXIST | lpDrv->dwFlags;
495 /**************************************************************************
496 * GetDriverModuleHandle [WINMM.@]
497 * DrvGetModuleHandle [WINMM.@]
499 HMODULE WINAPI GetDriverModuleHandle(HDRVR hDrvr)
504 TRACE("(%04x);\n", hDrvr);
506 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
507 if (!(lpDrv->dwFlags & WINE_GDF_16BIT))
508 hModule = lpDrv->d.d32.hModule;
510 TRACE("=> %04x\n", hModule);
514 /**************************************************************************
515 * DrvOpen [MMSYSTEM.1100]
517 HDRVR16 WINAPI DrvOpen16(LPSTR lpDriverName, LPSTR lpSectionName, LPARAM lParam)
519 return OpenDriver16(lpDriverName, lpSectionName, lParam);
522 /**************************************************************************
523 * DrvClose [MMSYSTEM.1101]
525 LRESULT WINAPI DrvClose16(HDRVR16 hDrv, LPARAM lParam1, LPARAM lParam2)
527 return CloseDriver16(hDrv, lParam1, lParam2);
530 /**************************************************************************
531 * DrvSendMessage [MMSYSTEM.1102]
533 LRESULT WINAPI DrvSendMessage16(HDRVR16 hDrv, WORD msg, LPARAM lParam1,
536 return SendDriverMessage16(hDrv, msg, lParam1, lParam2);
539 /**************************************************************************
540 * DrvGetModuleHandle [MMSYSTEM.1103]
542 HANDLE16 WINAPI DrvGetModuleHandle16(HDRVR16 hDrv)
544 return GetDriverModuleHandle16(hDrv);
547 /**************************************************************************
548 * DrvDefDriverProc [MMSYSTEM.1104]
550 LRESULT WINAPI DrvDefDriverProc16(DWORD dwDriverID, HDRVR16 hDrv, WORD wMsg,
551 DWORD dwParam1, DWORD dwParam2)
553 return DefDriverProc16(dwDriverID, hDrv, wMsg, dwParam1, dwParam2);
556 /**************************************************************************
557 * DefDriverProc [WINMM.@]
558 * DrvDefDriverProc [WINMM.@]
560 LRESULT WINAPI DefDriverProc(DWORD dwDriverIdentifier, HDRVR hDrv,
561 UINT Msg, LPARAM lParam1, LPARAM lParam2)
577 /**************************************************************************
578 * DriverProc [MMSYSTEM.6]
580 LRESULT WINAPI DriverProc16(DWORD dwDevID, HDRVR16 hDrv, WORD wMsg,
581 DWORD dwParam1, DWORD dwParam2)
583 TRACE("dwDevID=%08lx hDrv=%04x wMsg=%04x dwParam1=%08lx dwParam2=%08lx\n",
584 dwDevID, hDrv, wMsg, dwParam1, dwParam2);
586 return DrvDefDriverProc16(dwDevID, hDrv, wMsg, dwParam1, dwParam2);