2 * WINE Drivers functions
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1998 Marcus Meissner
6 * Copyright 1999 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 #include "wine/exception.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(driver);
40 static LPWINE_DRIVER lpDrvItemList /* = NULL */;
41 static const WCHAR HKLM_BASE[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
42 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',0};
44 WINE_MMTHREAD* (*pFnGetMMThread16)(UINT16 h) /* = NULL */;
45 LPWINE_DRIVER (*pFnOpenDriver16)(LPCWSTR,LPCWSTR,LPARAM) /* = NULL */;
46 LRESULT (*pFnCloseDriver16)(UINT16,LPARAM,LPARAM) /* = NULL */;
47 LRESULT (*pFnSendMessage16)(UINT16,UINT,LPARAM,LPARAM) /* = NULL */;
49 /**************************************************************************
50 * DRIVER_GetNumberOfModuleRefs [internal]
52 * Returns the number of open drivers which share the same module.
54 static unsigned DRIVER_GetNumberOfModuleRefs(HMODULE hModule, WINE_DRIVER** found)
59 if (found) *found = NULL;
60 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem)
62 if (!(lpDrv->dwFlags & WINE_GDF_16BIT) && lpDrv->d.d32.hModule == hModule)
64 if (found && !*found) *found = lpDrv;
71 /**************************************************************************
72 * DRIVER_FindFromHDrvr [internal]
74 * From a hDrvr being 32 bits, returns the WINE internal structure.
76 LPWINE_DRIVER DRIVER_FindFromHDrvr(HDRVR hDrvr)
82 d = (LPWINE_DRIVER)hDrvr;
83 if (d && d->dwMagic != WINE_DI_MAGIC) d = NULL;
94 /**************************************************************************
95 * DRIVER_SendMessage [internal]
97 static LRESULT inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv, UINT msg,
98 LPARAM lParam1, LPARAM lParam2)
102 if (lpDrv->dwFlags & WINE_GDF_16BIT) {
103 /* no need to check mmsystem presence: the driver must have been opened as a 16 bit one,
105 if (pFnSendMessage16)
106 ret = pFnSendMessage16(lpDrv->d.d16.hDriver16, msg, lParam1, lParam2);
108 TRACE("Before call32 proc=%p drvrID=%08lx hDrv=%p wMsg=%04x p1=%08lx p2=%08lx\n",
109 lpDrv->d.d32.lpDrvProc, lpDrv->d.d32.dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
110 ret = lpDrv->d.d32.lpDrvProc(lpDrv->d.d32.dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
111 TRACE("After call32 proc=%p drvrID=%08lx hDrv=%p wMsg=%04x p1=%08lx p2=%08lx => %08lx\n",
112 lpDrv->d.d32.lpDrvProc, lpDrv->d.d32.dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2, ret);
117 /**************************************************************************
118 * SendDriverMessage [WINMM.@]
119 * DrvSendMessage [WINMM.@]
121 LRESULT WINAPI SendDriverMessage(HDRVR hDriver, UINT msg, LPARAM lParam1,
127 TRACE("(%p, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
129 if ((lpDrv = DRIVER_FindFromHDrvr(hDriver)) != NULL) {
130 retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
132 WARN("Bad driver handle %p\n", hDriver);
134 TRACE("retval = %ld\n", retval);
139 /**************************************************************************
140 * DRIVER_RemoveFromList [internal]
142 * Generates all the logic to handle driver closure / deletion
143 * Removes a driver struct to the list of open drivers.
145 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
147 if (!(lpDrv->dwFlags & WINE_GDF_16BIT)) {
148 /* last of this driver in list ? */
149 if (DRIVER_GetNumberOfModuleRefs(lpDrv->d.d32.hModule, NULL) == 1) {
150 DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
151 DRIVER_SendMessage(lpDrv, DRV_FREE, 0L, 0L);
155 if (lpDrv->lpPrevItem)
156 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
158 lpDrvItemList = lpDrv->lpNextItem;
159 if (lpDrv->lpNextItem)
160 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
161 /* trash magic number */
162 lpDrv->dwMagic ^= 0xa5a5a5a5;
167 /**************************************************************************
168 * DRIVER_AddToList [internal]
170 * Adds a driver struct to the list of open drivers.
171 * Generates all the logic to handle driver creation / open.
173 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
175 lpNewDrv->dwMagic = WINE_DI_MAGIC;
176 /* First driver to be loaded for this module, need to load correctly the module */
177 if (!(lpNewDrv->dwFlags & WINE_GDF_16BIT)) {
178 /* first of this driver in list ? */
179 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv->d.d32.hModule, NULL) == 0) {
180 if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
181 TRACE("DRV_LOAD failed on driver %p\n", lpNewDrv);
184 /* returned value is not checked */
185 DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
189 lpNewDrv->lpNextItem = NULL;
190 if (lpDrvItemList == NULL) {
191 lpDrvItemList = lpNewDrv;
192 lpNewDrv->lpPrevItem = NULL;
194 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
195 while (lpDrv->lpNextItem != NULL)
196 lpDrv = lpDrv->lpNextItem;
198 lpDrv->lpNextItem = lpNewDrv;
199 lpNewDrv->lpPrevItem = lpDrv;
202 if (!(lpNewDrv->dwFlags & WINE_GDF_16BIT)) {
203 /* Now just open a new instance of a driver on this module */
204 lpNewDrv->d.d32.dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
206 if (lpNewDrv->d.d32.dwDriverID == 0) {
207 TRACE("DRV_OPEN failed on driver %p\n", lpNewDrv);
208 DRIVER_RemoveFromList(lpNewDrv);
215 /**************************************************************************
216 * DRIVER_GetLibName [internal]
219 BOOL DRIVER_GetLibName(LPCWSTR keyName, LPCWSTR sectName, LPWSTR buf, int sz)
223 static const WCHAR wszSystemIni[] = {'S','Y','S','T','E','M','.','I','N','I',0};
224 WCHAR wsznull = '\0';
226 lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE, HKLM_BASE, 0, KEY_QUERY_VALUE, &hKey);
227 if (lRet == ERROR_SUCCESS) {
228 lRet = RegOpenKeyExW(hKey, sectName, 0, KEY_QUERY_VALUE, &hSecKey);
229 if (lRet == ERROR_SUCCESS) {
231 lRet = RegQueryValueExW(hSecKey, keyName, 0, 0, (void*)buf, &bufLen);
232 RegCloseKey( hSecKey );
236 if (lRet == ERROR_SUCCESS) return TRUE;
237 /* default to system.ini if we can't find it in the registry,
238 * to support native installations where system.ini is still used */
239 return GetPrivateProfileStringW(sectName, keyName, &wsznull, buf, sz / sizeof(WCHAR), wszSystemIni);
242 /**************************************************************************
243 * DRIVER_TryOpenDriver32 [internal]
245 * Tries to load a 32 bit driver whose DLL's (module) name is fn
247 LPWINE_DRIVER DRIVER_TryOpenDriver32(LPCWSTR fn, LPARAM lParam2)
249 LPWINE_DRIVER lpDrv = NULL;
254 TRACE("(%s, %08lX);\n", debugstr_w(fn), lParam2);
256 if ((ptr = strchrW(fn, ' ')) != NULL) {
258 while (*ptr == ' ') ptr++;
259 if (*ptr == '\0') ptr = NULL;
262 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
263 if (lpDrv == NULL) {cause = "OOM"; goto exit;}
265 if ((hModule = LoadLibraryW(fn)) == 0) {cause = "Not a 32 bit lib"; goto exit;}
267 lpDrv->d.d32.lpDrvProc = (DRIVERPROC)GetProcAddress(hModule, "DriverProc");
268 if (lpDrv->d.d32.lpDrvProc == NULL) {cause = "no DriverProc"; goto exit;}
271 lpDrv->d.d32.hModule = hModule;
272 lpDrv->d.d32.dwDriverID = 0;
274 /* Win32 installable drivers must support a two phase opening scheme:
275 * + first open with NULL as lParam2 (session instance),
276 * + then do a second open with the real non null lParam2)
278 if (DRIVER_GetNumberOfModuleRefs(lpDrv->d.d32.hModule, NULL) == 0 && lParam2)
282 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, 0L))
284 cause = "load0 failed";
287 ret = DRIVER_TryOpenDriver32(fn, lParam2);
290 CloseDriver((HDRVR)lpDrv, 0L, 0L);
291 cause = "load1 failed";
297 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2))
298 {cause = "load failed"; goto exit;}
300 TRACE("=> %p\n", lpDrv);
303 FreeLibrary(hModule);
304 HeapFree(GetProcessHeap(), 0, lpDrv);
305 TRACE("Unable to load 32 bit module %s: %s\n", debugstr_w(fn), cause);
309 /**************************************************************************
310 * OpenDriverA [WINMM.@]
312 * (0,1,DRV_LOAD ,0 ,0)
313 * (0,1,DRV_ENABLE,0 ,0)
314 * (0,1,DRV_OPEN ,buf[256],0)
316 HDRVR WINAPI OpenDriverA(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam)
325 len = MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, NULL, 0 );
326 dn = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
328 MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, dn, len );
333 len = MultiByteToWideChar( CP_ACP, 0, lpSectionName, -1, NULL, 0 );
334 sn = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
336 MultiByteToWideChar( CP_ACP, 0, lpSectionName, -1, sn, len );
339 ret = OpenDriver(dn, sn, lParam);
342 HeapFree(GetProcessHeap(), 0, dn);
343 HeapFree(GetProcessHeap(), 0, sn);
347 /**************************************************************************
348 * OpenDriver [WINMM.@]
351 HDRVR WINAPI OpenDriver(LPCWSTR lpDriverName, LPCWSTR lpSectionName, LPARAM lParam)
353 LPWINE_DRIVER lpDrv = NULL;
355 LPCWSTR lsn = lpSectionName;
357 TRACE("(%s, %s, 0x%08lx);\n",
358 debugstr_w(lpDriverName), debugstr_w(lpSectionName), lParam);
361 static const WCHAR wszDrivers32[] = {'D','r','i','v','e','r','s','3','2',0};
362 lstrcpynW(libName, lpDriverName, sizeof(libName) / sizeof(WCHAR));
364 if ((lpDrv = DRIVER_TryOpenDriver32(libName, lParam)))
368 if (DRIVER_GetLibName(lpDriverName, lsn, libName, sizeof(libName)) &&
369 (lpDrv = DRIVER_TryOpenDriver32(libName, lParam)))
372 /* now we will try a 16 bit driver (and add all the glue to make it work... which
373 * is located in our mmsystem implementation)
374 * so ensure, we can load our mmsystem, otherwise just fail
376 WINMM_CheckForMMSystem();
377 if (pFnOpenDriver16 &&
378 (lpDrv = pFnOpenDriver16(lpDriverName, lpSectionName, lParam)))
380 if (DRIVER_AddToList(lpDrv, 0, lParam)) goto the_end;
381 HeapFree(GetProcessHeap(), 0, lpDrv);
383 TRACE("Failed to open driver %s from system.ini file, section %s\n",
384 debugstr_w(lpDriverName), debugstr_w(lpSectionName));
388 if (lpDrv) TRACE("=> %p\n", lpDrv);
392 /**************************************************************************
393 * CloseDriver [WINMM.@]
396 LRESULT WINAPI CloseDriver(HDRVR hDrvr, LPARAM lParam1, LPARAM lParam2)
400 TRACE("(%p, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
402 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL)
404 if (lpDrv->dwFlags & WINE_GDF_16BIT)
406 if (pFnCloseDriver16)
407 pFnCloseDriver16(lpDrv->d.d16.hDriver16, lParam1, lParam2);
411 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
412 lpDrv->d.d32.dwDriverID = 0;
414 if (DRIVER_RemoveFromList(lpDrv)) {
415 if (!(lpDrv->dwFlags & WINE_GDF_16BIT))
417 LPWINE_DRIVER lpDrv0;
419 /* if driver has an opened session instance, we have to close it too */
420 if (DRIVER_GetNumberOfModuleRefs(lpDrv->d.d32.hModule, &lpDrv0) == 1)
422 DRIVER_SendMessage(lpDrv0, DRV_CLOSE, 0L, 0L);
423 lpDrv0->d.d32.dwDriverID = 0;
424 DRIVER_RemoveFromList(lpDrv0);
425 FreeLibrary(lpDrv0->d.d32.hModule);
426 HeapFree(GetProcessHeap(), 0, lpDrv0);
428 FreeLibrary(lpDrv->d.d32.hModule);
430 HeapFree(GetProcessHeap(), 0, lpDrv);
434 WARN("Failed to close driver\n");
438 /**************************************************************************
439 * GetDriverFlags [WINMM.@]
440 * [in] hDrvr handle to the driver
443 * 0x00000000 if hDrvr is an invalid handle
444 * 0x80000000 if hDrvr is a valid 32 bit driver
445 * 0x90000000 if hDrvr is a valid 16 bit driver
447 * native WINMM doesn't return those flags
448 * 0x80000000 for a valid 32 bit driver and that's it
449 * (I may have mixed up the two flags :-(
451 DWORD WINAPI GetDriverFlags(HDRVR hDrvr)
456 TRACE("(%p)\n", hDrvr);
458 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
459 ret = WINE_GDF_EXIST | lpDrv->dwFlags;
464 /**************************************************************************
465 * GetDriverModuleHandle [WINMM.@]
466 * DrvGetModuleHandle [WINMM.@]
468 HMODULE WINAPI GetDriverModuleHandle(HDRVR hDrvr)
473 TRACE("(%p);\n", hDrvr);
475 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
476 if (!(lpDrv->dwFlags & WINE_GDF_16BIT))
477 hModule = lpDrv->d.d32.hModule;
479 TRACE("=> %p\n", hModule);
483 /**************************************************************************
484 * DefDriverProc [WINMM.@]
485 * DrvDefDriverProc [WINMM.@]
487 LRESULT WINAPI DefDriverProc(DWORD_PTR dwDriverIdentifier, HDRVR hDrv,
488 UINT Msg, LPARAM lParam1, LPARAM lParam2)
504 /**************************************************************************
505 * DriverCallback [WINMM.@]
507 BOOL WINAPI DriverCallback(DWORD dwCallBack, UINT uFlags, HDRVR hDev,
508 UINT wMsg, DWORD dwUser, DWORD dwParam1,
511 TRACE("(%08lX, %04X, %p, %04X, %08lX, %08lX, %08lX); !\n",
512 dwCallBack, uFlags, hDev, wMsg, dwUser, dwParam1, dwParam2);
514 switch (uFlags & DCB_TYPEMASK) {
518 WARN("uFlags=%04X has null DCB value, but dwCallBack=%08lX is not null !\n", uFlags, dwCallBack);
521 TRACE("Window(%04lX) handle=%p!\n", dwCallBack, hDev);
522 PostMessageA((HWND)dwCallBack, wMsg, (WPARAM)hDev, dwParam1);
524 case DCB_TASK: /* aka DCB_THREAD */
525 TRACE("Task(%04lx) !\n", dwCallBack);
526 PostThreadMessageA(dwCallBack, wMsg, (WPARAM)hDev, dwParam1);
529 TRACE("Function (32 bit) !\n");
530 ((LPDRVCALLBACK)dwCallBack)(hDev, wMsg, dwUser, dwParam1, dwParam2);
533 TRACE("Event(%08lx) !\n", dwCallBack);
534 SetEvent((HANDLE)dwCallBack);
536 case 6: /* I would dub it DCB_MMTHREADSIGNAL */
537 /* this is an undocumented DCB_ value used for mmThreads
538 * loword of dwCallBack contains the handle of the lpMMThd block
539 * which dwSignalCount has to be incremented
541 if (pFnGetMMThread16)
543 WINE_MMTHREAD* lpMMThd = pFnGetMMThread16(LOWORD(dwCallBack));
545 TRACE("mmThread (%04x, %p) !\n", LOWORD(dwCallBack), lpMMThd);
546 /* same as mmThreadSignal16 */
547 InterlockedIncrement(&lpMMThd->dwSignalCount);
548 SetEvent(lpMMThd->hEvent);
549 /* some other stuff on lpMMThd->hVxD */
554 /* this is an undocumented DCB_ value for... I don't know */
558 WARN("Unknown callback type %d\n", uFlags & DCB_TYPEMASK);
565 /******************************************************************
570 void DRIVER_UnloadAll(void)
573 LPWINE_DRIVER lpNextDrv = NULL;
576 for (lpDrv = lpDrvItemList; lpDrv != NULL; lpDrv = lpNextDrv)
578 lpNextDrv = lpDrv->lpNextItem;
579 CloseDriver((HDRVR)lpDrv, 0, 0);
582 TRACE("Unloaded %u drivers\n", count);