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
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wine/winbase16.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(driver);
35 static LPWINE_DRIVER lpDrvItemList = NULL;
38 * - LoadModule count and clean up is not handled correctly (it's not a
39 * problem as long as FreeLibrary is not working correctly)
42 /**************************************************************************
43 * DRIVER_GetNumberOfModuleRefs [internal]
45 * Returns the number of open drivers which share the same module.
47 static WORD DRIVER_GetNumberOfModuleRefs(LPWINE_DRIVER lpNewDrv)
52 if (lpNewDrv->dwFlags & WINE_GDF_16BIT) ERR("OOOch\n");
53 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem) {
54 if (!(lpDrv->dwFlags & WINE_GDF_16BIT) &&
55 lpDrv->d.d32.hModule == lpNewDrv->d.d32.hModule) {
62 /**************************************************************************
63 * DRIVER_FindFromHDrvr [internal]
65 * From a hDrvr being 32 bits, returns the WINE internal structure.
67 LPWINE_DRIVER DRIVER_FindFromHDrvr(HDRVR hDrvr)
69 LPWINE_DRIVER d = (LPWINE_DRIVER)hDrvr;
71 if (hDrvr && HeapValidate(GetProcessHeap(), 0, d) && d->dwMagic == WINE_DI_MAGIC) {
77 /**************************************************************************
78 * DRIVER_MapMsg32To16 [internal]
80 * Map a 32 bit driver message to a 16 bit driver message.
81 * 1 : ok, some memory allocated, need to call DRIVER_UnMapMsg32To16
82 * 0 : ok, no memory allocated
83 * -1 : ko, unknown message
84 * -2 : ko, memory problem
86 static int DRIVER_MapMsg32To16(WORD wMsg, DWORD* lParam1, DWORD* lParam2)
95 case DRV_QUERYCONFIGURE:
98 case DRV_EXITAPPLICATION:
100 case DRV_CLOSE: /* should be 0/0 */
101 case DRV_OPEN: /* pass through */
102 /* lParam1 and lParam2 are not used */
108 /* lParam1 is a handle to a window (conf) or to a driver (inst) or not used,
109 * lParam2 is a pointer to DRVCONFIGINFO
112 LPDRVCONFIGINFO16 dci16 = HeapAlloc( GetProcessHeap(), 0, sizeof(*dci16) );
113 LPDRVCONFIGINFO dci32 = (LPDRVCONFIGINFO)(*lParam2);
118 dci16->dwDCISize = sizeof(DRVCONFIGINFO16);
120 if ((str1 = HEAP_strdupWtoA(GetProcessHeap(), 0, dci32->lpszDCISectionName)) != NULL)
122 dci16->lpszDCISectionName = MapLS( str1 );
126 if ((str1 = HEAP_strdupWtoA(GetProcessHeap(), 0, dci32->lpszDCIAliasName)) != NULL)
128 dci16->lpszDCIAliasName = MapLS( str1 );
135 *lParam2 = MapLS( dci16 );
142 if (!((wMsg >= 0x800 && wMsg < 0x900) || (wMsg >= 0x4000 && wMsg < 0x4100))) {
143 FIXME("Unknown message 0x%04x\n", wMsg);
150 /**************************************************************************
151 * DRIVER_UnMapMsg32To16 [internal]
153 * UnMap a 32 bit driver message to a 16 bit driver message.
156 * -2 : ko, memory problem
158 static int DRIVER_UnMapMsg32To16(WORD wMsg, DWORD lParam1, DWORD lParam2)
167 case DRV_QUERYCONFIGURE:
169 case DRV_EXITSESSION:
170 case DRV_EXITAPPLICATION:
174 /* lParam1 and lParam2 are not used */
178 /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO, lParam2 */
180 LPDRVCONFIGINFO16 dci16 = MapSL(lParam2);
181 HeapFree( GetProcessHeap(), 0, MapSL(dci16->lpszDCISectionName) );
182 HeapFree( GetProcessHeap(), 0, MapSL(dci16->lpszDCIAliasName) );
184 UnMapLS( dci16->lpszDCISectionName );
185 UnMapLS( dci16->lpszDCIAliasName );
186 HeapFree( GetProcessHeap(), 0, dci16 );
191 if (!((wMsg >= 0x800 && wMsg < 0x900) || (wMsg >= 0x4000 && wMsg < 0x4100))) {
192 FIXME("Unknown message 0x%04x\n", wMsg);
199 /**************************************************************************
200 * DRIVER_SendMessage [internal]
202 static LRESULT inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv, UINT msg,
203 LPARAM lParam1, LPARAM lParam2)
205 if (lpDrv->dwFlags & WINE_GDF_16BIT) {
208 TRACE("Before sdm16 call hDrv=%04x wMsg=%04x p1=%08lx p2=%08lx\n",
209 lpDrv->d.d16.hDriver16, msg, lParam1, lParam2);
211 if ((map = DRIVER_MapMsg32To16(msg, &lParam1, &lParam2)) >= 0) {
212 ret = SendDriverMessage16(lpDrv->d.d16.hDriver16, msg, lParam1, lParam2);
214 DRIVER_UnMapMsg32To16(msg, lParam1, lParam2);
220 TRACE("Before func32 call proc=%p driverID=%08lx hDrv=%08x wMsg=%04x p1=%08lx p2=%08lx\n",
221 lpDrv->d.d32.lpDrvProc, lpDrv->d.d32.dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
222 return lpDrv->d.d32.lpDrvProc(lpDrv->d.d32.dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
225 /**************************************************************************
226 * SendDriverMessage [WINMM.@]
227 * DrvSendMessage [WINMM.@]
229 LRESULT WINAPI SendDriverMessage(HDRVR hDriver, UINT msg, LPARAM lParam1,
235 TRACE("(%04x, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
237 if ((lpDrv = DRIVER_FindFromHDrvr(hDriver)) != NULL) {
238 retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
240 WARN("Bad driver handle %u\n", hDriver);
242 TRACE("retval = %ld\n", retval);
247 /**************************************************************************
248 * DRIVER_RemoveFromList [internal]
250 * Generates all the logic to handle driver closure / deletion
251 * Removes a driver struct to the list of open drivers.
253 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
255 if (!(lpDrv->dwFlags & WINE_GDF_16BIT)) {
256 if (DRIVER_GetNumberOfModuleRefs(lpDrv) == 1) {
257 DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
258 DRIVER_SendMessage(lpDrv, DRV_FREE, 0L, 0L);
262 if (lpDrv->lpPrevItem)
263 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
265 lpDrvItemList = lpDrv->lpNextItem;
266 if (lpDrv->lpNextItem)
267 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
272 /**************************************************************************
273 * DRIVER_AddToList [internal]
275 * Adds a driver struct to the list of open drivers.
276 * Generates all the logic to handle driver creation / open.
278 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
280 lpNewDrv->dwMagic = WINE_DI_MAGIC;
281 /* First driver to be loaded for this module, need to load correctly the module */
282 if (!(lpNewDrv->dwFlags & WINE_GDF_16BIT)) {
283 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv) == 0) {
284 if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
285 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
288 /* returned value is not checked */
289 DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
293 lpNewDrv->lpNextItem = NULL;
294 if (lpDrvItemList == NULL) {
295 lpDrvItemList = lpNewDrv;
296 lpNewDrv->lpPrevItem = NULL;
298 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
299 while (lpDrv->lpNextItem != NULL)
300 lpDrv = lpDrv->lpNextItem;
302 lpDrv->lpNextItem = lpNewDrv;
303 lpNewDrv->lpPrevItem = lpDrv;
306 if (!(lpNewDrv->dwFlags & WINE_GDF_16BIT)) {
307 /* Now just open a new instance of a driver on this module */
308 lpNewDrv->d.d32.dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
310 if (lpNewDrv->d.d32.dwDriverID == 0) {
311 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
312 DRIVER_RemoveFromList(lpNewDrv);
319 /**************************************************************************
320 * DRIVER_GetLibName [internal]
323 BOOL DRIVER_GetLibName(LPCSTR keyName, LPCSTR sectName, LPSTR buf, int sz)
325 /* should also do some registry diving */
326 return GetPrivateProfileStringA(sectName, keyName, "", buf, sz, "SYSTEM.INI");
329 /**************************************************************************
330 * DRIVER_TryOpenDriver32 [internal]
332 * Tries to load a 32 bit driver whose DLL's (module) name is fn
334 LPWINE_DRIVER DRIVER_TryOpenDriver32(LPCSTR fn, LPARAM lParam2)
336 LPWINE_DRIVER lpDrv = NULL;
341 TRACE("(%s, %08lX);\n", debugstr_a(fn), lParam2);
343 if ((ptr = strchr(fn, ' ')) != NULL) {
345 while (*ptr == ' ') ptr++;
346 if (*ptr == '\0') ptr = NULL;
349 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
350 if (lpDrv == NULL) {cause = "OOM"; goto exit;}
352 if ((hModule = LoadLibraryA(fn)) == 0) {cause = "Not a 32 bit lib"; goto exit;}
354 lpDrv->d.d32.lpDrvProc = (DRIVERPROC)GetProcAddress(hModule, "DriverProc");
355 if (lpDrv->d.d32.lpDrvProc == NULL) {cause = "no DriverProc"; goto exit;}
358 lpDrv->d.d32.hModule = hModule;
359 lpDrv->d.d32.dwDriverID = 0;
361 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2)) {cause = "load failed"; goto exit;}
363 TRACE("=> %p\n", lpDrv);
366 FreeLibrary(hModule);
367 HeapFree(GetProcessHeap(), 0, lpDrv);
368 TRACE("Unable to load 32 bit module %s: %s\n", debugstr_a(fn), cause);
372 /**************************************************************************
373 * DRIVER_TryOpenDriver16 [internal]
375 * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
377 static LPWINE_DRIVER DRIVER_TryOpenDriver16(LPCSTR fn, LPCSTR sn, LPARAM lParam2)
379 LPWINE_DRIVER lpDrv = NULL;
382 TRACE("(%s, %08lX);\n", debugstr_a(sn), lParam2);
384 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
385 if (lpDrv == NULL) {cause = "OOM"; goto exit;}
387 /* FIXME: shall we do some black magic here on sn ?
388 * drivers32 => drivers
392 lpDrv->d.d16.hDriver16 = OpenDriver16(fn, sn, lParam2);
393 if (lpDrv->d.d16.hDriver16 == 0) {cause = "Not a 16 bit driver"; goto exit;}
394 lpDrv->dwFlags = WINE_GDF_16BIT;
396 if (!DRIVER_AddToList(lpDrv, 0, lParam2)) {cause = "load failed"; goto exit;}
398 TRACE("=> %p\n", lpDrv);
401 HeapFree(GetProcessHeap(), 0, lpDrv);
402 TRACE("Unable to load 32 bit module %s: %s\n", debugstr_a(fn), cause);
406 /**************************************************************************
407 * OpenDriverA [WINMM.@]
409 * (0,1,DRV_LOAD ,0 ,0)
410 * (0,1,DRV_ENABLE,0 ,0)
411 * (0,1,DRV_OPEN ,buf[256],0)
413 HDRVR WINAPI OpenDriverA(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
415 LPWINE_DRIVER lpDrv = NULL;
417 LPCSTR lsn = lpSectionName;
419 TRACE("(%s, %s, 0x%08lx);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam2);
422 lstrcpynA(libName, lpDriverName, sizeof(libName));
424 if ((lpDrv = DRIVER_TryOpenDriver32(libName, lParam2)))
428 if (DRIVER_GetLibName(lpDriverName, lsn, libName, sizeof(libName)) &&
429 (lpDrv = DRIVER_TryOpenDriver32(libName, lParam2)))
432 if (!(lpDrv = DRIVER_TryOpenDriver16(lpDriverName, lpSectionName, lParam2)))
433 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName));
435 if (lpDrv) TRACE("=> %08lx\n", (DWORD)lpDrv);
439 /**************************************************************************
440 * OpenDriver [WINMM.@]
443 HDRVR WINAPI OpenDriverW(LPCWSTR lpDriverName, LPCWSTR lpSectionName, LPARAM lParam)
445 LPSTR dn = HEAP_strdupWtoA(GetProcessHeap(), 0, lpDriverName);
446 LPSTR sn = HEAP_strdupWtoA(GetProcessHeap(), 0, lpSectionName);
447 HDRVR ret = OpenDriverA(dn, sn, lParam);
449 if (dn) HeapFree(GetProcessHeap(), 0, dn);
450 if (sn) HeapFree(GetProcessHeap(), 0, sn);
454 /**************************************************************************
455 * CloseDriver [WINMM.@]
458 LRESULT WINAPI CloseDriver(HDRVR hDrvr, LPARAM lParam1, LPARAM lParam2)
462 TRACE("(%04x, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
464 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
465 if (lpDrv->dwFlags & WINE_GDF_16BIT)
466 CloseDriver16(lpDrv->d.d16.hDriver16, lParam1, lParam2);
468 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
469 if (DRIVER_RemoveFromList(lpDrv)) {
470 HeapFree(GetProcessHeap(), 0, lpDrv);
474 WARN("Failed to close driver\n");
478 /**************************************************************************
479 * GetDriverFlags [WINMM.@]
480 * [in] hDrvr handle to the driver
483 * 0x00000000 if hDrvr is an invalid handle
484 * 0x80000000 if hDrvr is a valid 32 bit driver
485 * 0x90000000 if hDrvr is a valid 16 bit driver
487 * native WINMM doesn't return those flags
488 * 0x80000000 for a valid 32 bit driver and that's it
489 * (I may have mixed up the two flags :-(
491 DWORD WINAPI GetDriverFlags(HDRVR hDrvr)
496 TRACE("(%04x)\n", hDrvr);
498 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
499 ret = WINE_GDF_EXIST | lpDrv->dwFlags;
504 /**************************************************************************
505 * GetDriverModuleHandle [WINMM.@]
506 * DrvGetModuleHandle [WINMM.@]
508 HMODULE WINAPI GetDriverModuleHandle(HDRVR hDrvr)
513 TRACE("(%04x);\n", hDrvr);
515 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
516 if (!(lpDrv->dwFlags & WINE_GDF_16BIT))
517 hModule = lpDrv->d.d32.hModule;
519 TRACE("=> %04x\n", hModule);
523 /**************************************************************************
524 * DrvOpen [MMSYSTEM.1100]
526 HDRVR16 WINAPI DrvOpen16(LPSTR lpDriverName, LPSTR lpSectionName, LPARAM lParam)
528 return OpenDriver16(lpDriverName, lpSectionName, lParam);
531 /**************************************************************************
532 * DrvClose [MMSYSTEM.1101]
534 LRESULT WINAPI DrvClose16(HDRVR16 hDrv, LPARAM lParam1, LPARAM lParam2)
536 return CloseDriver16(hDrv, lParam1, lParam2);
539 /**************************************************************************
540 * DrvSendMessage [MMSYSTEM.1102]
542 LRESULT WINAPI DrvSendMessage16(HDRVR16 hDrv, WORD msg, LPARAM lParam1,
545 return SendDriverMessage16(hDrv, msg, lParam1, lParam2);
548 /**************************************************************************
549 * DrvGetModuleHandle [MMSYSTEM.1103]
551 HANDLE16 WINAPI DrvGetModuleHandle16(HDRVR16 hDrv)
553 return GetDriverModuleHandle16(hDrv);
556 /**************************************************************************
557 * DrvDefDriverProc [MMSYSTEM.1104]
559 LRESULT WINAPI DrvDefDriverProc16(DWORD dwDriverID, HDRVR16 hDrv, WORD wMsg,
560 DWORD dwParam1, DWORD dwParam2)
562 return DefDriverProc16(dwDriverID, hDrv, wMsg, dwParam1, dwParam2);
565 /**************************************************************************
566 * DefDriverProc [WINMM.@]
567 * DrvDefDriverProc [WINMM.@]
569 LRESULT WINAPI DefDriverProc(DWORD dwDriverIdentifier, HDRVR hDrv,
570 UINT Msg, LPARAM lParam1, LPARAM lParam2)
586 /**************************************************************************
587 * DriverProc [MMSYSTEM.6]
589 LRESULT WINAPI DriverProc16(DWORD dwDevID, HDRVR16 hDrv, WORD wMsg,
590 DWORD dwParam1, DWORD dwParam2)
592 TRACE("dwDevID=%08lx hDrv=%04x wMsg=%04x dwParam1=%08lx dwParam2=%08lx\n",
593 dwDevID, hDrv, wMsg, dwParam1, dwParam2);
595 return DrvDefDriverProc16(dwDevID, hDrv, wMsg, dwParam1, dwParam2);