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 thru */
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.@]
219 LRESULT WINAPI SendDriverMessage(HDRVR hDriver, UINT msg, LPARAM lParam1,
225 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
227 if ((lpDrv = DRIVER_FindFromHDrvr(hDriver)) != NULL) {
228 retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
230 WARN("Bad driver handle %u\n", hDriver);
232 TRACE("retval = %ld\n", retval);
237 /**************************************************************************
238 * DRIVER_RemoveFromList [internal]
240 * Generates all the logic to handle driver closure / deletion
241 * Removes a driver struct to the list of open drivers.
243 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
245 if (!(lpDrv->dwFlags & WINE_GDF_16BIT)) {
246 if (DRIVER_GetNumberOfModuleRefs(lpDrv) == 1) {
247 DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
248 DRIVER_SendMessage(lpDrv, DRV_FREE, 0L, 0L);
252 if (lpDrv->lpPrevItem)
253 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
255 lpDrvItemList = lpDrv->lpNextItem;
256 if (lpDrv->lpNextItem)
257 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
262 /**************************************************************************
263 * DRIVER_AddToList [internal]
265 * Adds a driver struct to the list of open drivers.
266 * Generates all the logic to handle driver creation / open.
268 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
270 lpNewDrv->dwMagic = WINE_DI_MAGIC;
271 /* First driver to be loaded for this module, need to load correctly the module */
272 if (!(lpNewDrv->dwFlags & WINE_GDF_16BIT)) {
273 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv) == 0) {
274 if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
275 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
278 /* returned value is not checked */
279 DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
283 lpNewDrv->lpNextItem = NULL;
284 if (lpDrvItemList == NULL) {
285 lpDrvItemList = lpNewDrv;
286 lpNewDrv->lpPrevItem = NULL;
288 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
289 while (lpDrv->lpNextItem != NULL)
290 lpDrv = lpDrv->lpNextItem;
292 lpDrv->lpNextItem = lpNewDrv;
293 lpNewDrv->lpPrevItem = lpDrv;
296 if (!(lpNewDrv->dwFlags & WINE_GDF_16BIT)) {
297 /* Now just open a new instance of a driver on this module */
298 lpNewDrv->d.d32.dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
300 if (lpNewDrv->d.d32.dwDriverID == 0) {
301 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
302 DRIVER_RemoveFromList(lpNewDrv);
309 /**************************************************************************
310 * DRIVER_GetLibName [internal]
313 BOOL DRIVER_GetLibName(LPCSTR keyName, LPCSTR sectName, LPSTR buf, int sz)
315 /* should also do some registry diving */
316 return GetPrivateProfileStringA(sectName, keyName, "", buf, sz, "SYSTEM.INI");
319 /**************************************************************************
320 * DRIVER_TryOpenDriver32 [internal]
322 * Tries to load a 32 bit driver whose DLL's (module) name is fn
324 LPWINE_DRIVER DRIVER_TryOpenDriver32(LPCSTR fn, LPARAM lParam2)
326 LPWINE_DRIVER lpDrv = NULL;
331 TRACE("('%s', %08lX);\n", fn, lParam2);
333 if ((ptr = strchr(fn, ' ')) != NULL) {
335 while (*ptr == ' ') ptr++;
336 if (*ptr == '\0') ptr = NULL;
339 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
340 if (lpDrv == NULL) {cause = "OOM"; goto exit;}
342 if ((hModule = LoadLibraryA(fn)) == 0) {cause = "Not a 32 bit lib"; goto exit;}
344 lpDrv->d.d32.lpDrvProc = (DRIVERPROC)GetProcAddress(hModule, "DriverProc");
345 if (lpDrv->d.d32.lpDrvProc == NULL) {cause = "no DriverProc"; goto exit;}
348 lpDrv->d.d32.hModule = hModule;
349 lpDrv->d.d32.dwDriverID = 0;
351 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2)) {cause = "load faile"; goto exit;}
353 TRACE("=> %p\n", lpDrv);
356 FreeLibrary(hModule);
357 HeapFree(GetProcessHeap(), 0, lpDrv);
358 TRACE("Unable to load 32 bit module \"%s\": %s\n", fn, cause);
362 /**************************************************************************
363 * DRIVER_TryOpenDriver16 [internal]
365 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
367 static LPWINE_DRIVER DRIVER_TryOpenDriver16(LPCSTR fn, LPCSTR sn, LPARAM lParam2)
369 LPWINE_DRIVER lpDrv = NULL;
372 TRACE("('%s', %08lX);\n", sn, lParam2);
374 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
375 if (lpDrv == NULL) {cause = "OOM"; goto exit;}
377 /* FIXME: shall we do some black magic here on sn ?
378 * drivers32 => drivers
382 lpDrv->d.d16.hDriver16 = OpenDriver16(fn, sn, lParam2);
383 if (lpDrv->d.d16.hDriver16 == 0) {cause = "Not a 16 bit driver"; goto exit;}
384 lpDrv->dwFlags = WINE_GDF_16BIT;
386 if (!DRIVER_AddToList(lpDrv, 0, lParam2)) {cause = "load faile"; goto exit;}
388 TRACE("=> %p\n", lpDrv);
391 HeapFree(GetProcessHeap(), 0, lpDrv);
392 TRACE("Unable to load 32 bit module \"%s\": %s\n", fn, cause);
396 /**************************************************************************
397 * OpenDriverA [WINMM.@]
398 * (0,1,DRV_LOAD ,0 ,0)
399 * (0,1,DRV_ENABLE,0 ,0)
400 * (0,1,DRV_OPEN ,buf[256],0)
402 HDRVR WINAPI OpenDriverA(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
404 LPWINE_DRIVER lpDrv = NULL;
406 LPCSTR lsn = lpSectionName;
408 TRACE("(%s, %s, 0x%08lx);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam2);
411 lstrcpynA(libName, lpDriverName, sizeof(libName));
413 if ((lpDrv = DRIVER_TryOpenDriver32(libName, lParam2)))
417 if (DRIVER_GetLibName(lpDriverName, lsn, libName, sizeof(libName)) &&
418 (lpDrv = DRIVER_TryOpenDriver32(libName, lParam2)))
421 if (!(lpDrv = DRIVER_TryOpenDriver16(lpDriverName, lpSectionName, lParam2)))
422 TRACE("Failed to open driver %s from system.ini file, section %s\n", lpDriverName, lpSectionName);
424 if (lpDrv) TRACE("=> %08lx\n", (DWORD)lpDrv);
428 /**************************************************************************
429 * OpenDriverW [WINMM.@]
431 HDRVR WINAPI OpenDriverW(LPCWSTR lpDriverName, LPCWSTR lpSectionName, LPARAM lParam)
433 LPSTR dn = HEAP_strdupWtoA(GetProcessHeap(), 0, lpDriverName);
434 LPSTR sn = HEAP_strdupWtoA(GetProcessHeap(), 0, lpSectionName);
435 HDRVR ret = OpenDriverA(dn, sn, lParam);
437 if (dn) HeapFree(GetProcessHeap(), 0, dn);
438 if (sn) HeapFree(GetProcessHeap(), 0, sn);
442 /**************************************************************************
443 * CloseDriver [WINMM.@]
445 LRESULT WINAPI CloseDriver(HDRVR hDrvr, LPARAM lParam1, LPARAM lParam2)
449 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
451 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
452 if (lpDrv->dwFlags & WINE_GDF_16BIT)
453 CloseDriver16(lpDrv->d.d16.hDriver16, lParam1, lParam2);
455 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
456 if (DRIVER_RemoveFromList(lpDrv)) {
457 HeapFree(GetProcessHeap(), 0, lpDrv);
461 WARN("Failed to close driver\n");
465 /**************************************************************************
466 * GetDriverFlags [WINMM.@]
467 * [in] hDrvr handle to the driver
470 * 0x00000000 if hDrvr is an invalid handle
471 * 0x80000000 if hDrvr is a valid 32 bit driver
472 * 0x90000000 if hDrvr is a valid 16 bit driver
474 * native WINMM doesn't return those flags
475 * 0x80000000 for a valid 32 bit driver and that's it
476 * (I may have mixed up the two flags :-(
478 DWORD WINAPI GetDriverFlags(HDRVR hDrvr)
483 TRACE("(%04x)\n", hDrvr);
485 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
486 ret = WINE_GDF_EXIST | lpDrv->dwFlags;
491 /**************************************************************************
492 * GetDriverModuleHandle [WINMM.@]
494 HMODULE WINAPI GetDriverModuleHandle(HDRVR hDrvr)
499 TRACE("(%04x);\n", hDrvr);
501 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
502 if (!(lpDrv->dwFlags & WINE_GDF_16BIT))
503 hModule = lpDrv->d.d32.hModule;
505 TRACE("=> %04x\n", hModule);
509 /**************************************************************************
510 * DrvOpen [MMSYSTEM.1100]
512 HDRVR16 WINAPI DrvOpen16(LPSTR lpDriverName, LPSTR lpSectionName, LPARAM lParam)
514 return OpenDriver16(lpDriverName, lpSectionName, lParam);
517 /**************************************************************************
518 * DrvClose [MMSYSTEM.1101]
520 LRESULT WINAPI DrvClose16(HDRVR16 hDrv, LPARAM lParam1, LPARAM lParam2)
522 return CloseDriver16(hDrv, lParam1, lParam2);
525 /**************************************************************************
526 * DrvSendMessage [MMSYSTEM.1102]
528 LRESULT WINAPI DrvSendMessage16(HDRVR16 hDrv, WORD msg, LPARAM lParam1,
531 return SendDriverMessage16(hDrv, msg, lParam1, lParam2);
534 /**************************************************************************
535 * DrvGetModuleHandle [MMSYSTEM.1103]
537 HANDLE16 WINAPI DrvGetModuleHandle16(HDRVR16 hDrv)
539 return GetDriverModuleHandle16(hDrv);
542 /**************************************************************************
543 * DrvDefDriverProc [MMSYSTEM.1104]
545 LRESULT WINAPI DrvDefDriverProc16(DWORD dwDriverID, HDRVR16 hDrv, WORD wMsg,
546 DWORD dwParam1, DWORD dwParam2)
548 return DefDriverProc16(dwDriverID, hDrv, wMsg, dwParam1, dwParam2);
551 /**************************************************************************
552 * DefDriverProc [WINMM.@]
554 LRESULT WINAPI DefDriverProc(DWORD dwDriverIdentifier, HDRVR hDrv,
555 UINT Msg, LPARAM lParam1, LPARAM lParam2)
571 /**************************************************************************
572 * DriverProc [MMSYSTEM.6]
574 LRESULT WINAPI DriverProc16(DWORD dwDevID, HDRVR16 hDrv, WORD wMsg,
575 DWORD dwParam1, DWORD dwParam2)
577 TRACE("dwDevID=%08lx hDrv=%04x wMsg=%04x dwParam1=%08lx dwParam2=%08lx\n",
578 dwDevID, hDrv, wMsg, dwParam1, dwParam2);
580 return DrvDefDriverProc16(dwDevID, hDrv, wMsg, dwParam1, dwParam2);