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 WINE_DEFAULT_DEBUG_CHANNEL(driver);
38 static LPWINE_DRIVER lpDrvItemList /* = NULL */;
39 static const WCHAR HKLM_BASE[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
40 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',0};
42 WINE_MMTHREAD* (*pFnGetMMThread16)(UINT16 h) /* = NULL */;
43 LPWINE_DRIVER (*pFnOpenDriver16)(LPCWSTR,LPCWSTR,LPARAM) /* = NULL */;
44 LRESULT (*pFnCloseDriver16)(UINT16,LPARAM,LPARAM) /* = NULL */;
45 LRESULT (*pFnSendMessage16)(UINT16,UINT,LPARAM,LPARAM) /* = NULL */;
47 /**************************************************************************
48 * DRIVER_GetNumberOfModuleRefs [internal]
50 * Returns the number of open drivers which share the same module.
52 static unsigned DRIVER_GetNumberOfModuleRefs(HMODULE hModule, WINE_DRIVER** found)
57 if (found) *found = NULL;
58 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem)
60 if (!(lpDrv->dwFlags & WINE_GDF_16BIT) && lpDrv->d.d32.hModule == hModule)
62 if (found && !*found) *found = lpDrv;
69 /**************************************************************************
70 * DRIVER_FindFromHDrvr [internal]
72 * From a hDrvr being 32 bits, returns the WINE internal structure.
74 LPWINE_DRIVER DRIVER_FindFromHDrvr(HDRVR hDrvr)
76 LPWINE_DRIVER d = (LPWINE_DRIVER)hDrvr;
78 if (hDrvr && HeapValidate(GetProcessHeap(), 0, d) && d->dwMagic == WINE_DI_MAGIC) {
84 /**************************************************************************
85 * DRIVER_SendMessage [internal]
87 static LRESULT inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv, UINT msg,
88 LPARAM lParam1, LPARAM lParam2)
92 if (lpDrv->dwFlags & WINE_GDF_16BIT) {
93 /* no need to check mmsystem presence: the driver must have been opened as a 16 bit one,
96 ret = pFnSendMessage16(lpDrv->d.d16.hDriver16, msg, lParam1, lParam2);
98 TRACE("Before call32 proc=%p drvrID=%08lx hDrv=%p wMsg=%04x p1=%08lx p2=%08lx\n",
99 lpDrv->d.d32.lpDrvProc, lpDrv->d.d32.dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
100 ret = lpDrv->d.d32.lpDrvProc(lpDrv->d.d32.dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
101 TRACE("After call32 proc=%p drvrID=%08lx hDrv=%p wMsg=%04x p1=%08lx p2=%08lx => %08lx\n",
102 lpDrv->d.d32.lpDrvProc, lpDrv->d.d32.dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2, ret);
107 /**************************************************************************
108 * SendDriverMessage [WINMM.@]
109 * DrvSendMessage [WINMM.@]
111 LRESULT WINAPI SendDriverMessage(HDRVR hDriver, UINT msg, LPARAM lParam1,
117 TRACE("(%p, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
119 if ((lpDrv = DRIVER_FindFromHDrvr(hDriver)) != NULL) {
120 retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
122 WARN("Bad driver handle %p\n", hDriver);
124 TRACE("retval = %ld\n", retval);
129 /**************************************************************************
130 * DRIVER_RemoveFromList [internal]
132 * Generates all the logic to handle driver closure / deletion
133 * Removes a driver struct to the list of open drivers.
135 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
137 if (!(lpDrv->dwFlags & WINE_GDF_16BIT)) {
138 /* last of this driver in list ? */
139 if (DRIVER_GetNumberOfModuleRefs(lpDrv->d.d32.hModule, NULL) == 1) {
140 DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
141 DRIVER_SendMessage(lpDrv, DRV_FREE, 0L, 0L);
145 if (lpDrv->lpPrevItem)
146 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
148 lpDrvItemList = lpDrv->lpNextItem;
149 if (lpDrv->lpNextItem)
150 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
151 /* trash magic number */
152 lpDrv->dwMagic ^= 0xa5a5a5a5;
157 /**************************************************************************
158 * DRIVER_AddToList [internal]
160 * Adds a driver struct to the list of open drivers.
161 * Generates all the logic to handle driver creation / open.
163 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
165 lpNewDrv->dwMagic = WINE_DI_MAGIC;
166 /* First driver to be loaded for this module, need to load correctly the module */
167 if (!(lpNewDrv->dwFlags & WINE_GDF_16BIT)) {
168 /* first of this driver in list ? */
169 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv->d.d32.hModule, NULL) == 0) {
170 if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
171 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
174 /* returned value is not checked */
175 DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
179 lpNewDrv->lpNextItem = NULL;
180 if (lpDrvItemList == NULL) {
181 lpDrvItemList = lpNewDrv;
182 lpNewDrv->lpPrevItem = NULL;
184 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
185 while (lpDrv->lpNextItem != NULL)
186 lpDrv = lpDrv->lpNextItem;
188 lpDrv->lpNextItem = lpNewDrv;
189 lpNewDrv->lpPrevItem = lpDrv;
192 if (!(lpNewDrv->dwFlags & WINE_GDF_16BIT)) {
193 /* Now just open a new instance of a driver on this module */
194 lpNewDrv->d.d32.dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
196 if (lpNewDrv->d.d32.dwDriverID == 0) {
197 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
198 DRIVER_RemoveFromList(lpNewDrv);
205 /**************************************************************************
206 * DRIVER_GetLibName [internal]
209 BOOL DRIVER_GetLibName(LPCWSTR keyName, LPCWSTR sectName, LPWSTR buf, int sz)
213 static const WCHAR wszSystemIni[] = {'S','Y','S','T','E','M','.','I','N','I',0};
214 WCHAR wsznull = '\0';
216 lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE, HKLM_BASE, 0, KEY_QUERY_VALUE, &hKey);
217 if (lRet == ERROR_SUCCESS) {
218 lRet = RegOpenKeyExW(hKey, sectName, 0, KEY_QUERY_VALUE, &hSecKey);
219 if (lRet == ERROR_SUCCESS) {
221 lRet = RegQueryValueExW(hSecKey, keyName, 0, 0, (void*)buf, &bufLen);
222 RegCloseKey( hSecKey );
226 if (lRet == ERROR_SUCCESS) return TRUE;
227 /* default to system.ini if we can't find it in the registry,
228 * to support native installations where system.ini is still used */
229 return GetPrivateProfileStringW(sectName, keyName, &wsznull, buf, sz / sizeof(WCHAR), wszSystemIni);
232 /**************************************************************************
233 * DRIVER_TryOpenDriver32 [internal]
235 * Tries to load a 32 bit driver whose DLL's (module) name is fn
237 LPWINE_DRIVER DRIVER_TryOpenDriver32(LPCWSTR fn, LPARAM lParam2)
239 LPWINE_DRIVER lpDrv = NULL;
244 TRACE("(%s, %08lX);\n", debugstr_w(fn), lParam2);
246 if ((ptr = strchrW(fn, ' ')) != NULL) {
248 while (*ptr == ' ') ptr++;
249 if (*ptr == '\0') ptr = NULL;
252 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
253 if (lpDrv == NULL) {cause = "OOM"; goto exit;}
255 if ((hModule = LoadLibraryW(fn)) == 0) {cause = "Not a 32 bit lib"; goto exit;}
257 lpDrv->d.d32.lpDrvProc = (DRIVERPROC)GetProcAddress(hModule, "DriverProc");
258 if (lpDrv->d.d32.lpDrvProc == NULL) {cause = "no DriverProc"; goto exit;}
261 lpDrv->d.d32.hModule = hModule;
262 lpDrv->d.d32.dwDriverID = 0;
264 /* Win32 installable drivers must support a two phase opening scheme:
265 * + first open with NULL as lParam2 (session instance),
266 * + then do a second open with the real non null lParam2)
268 if (DRIVER_GetNumberOfModuleRefs(lpDrv->d.d32.hModule, NULL) == 0 && lParam2)
272 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, 0L))
274 cause = "load0 failed";
277 ret = DRIVER_TryOpenDriver32(fn, lParam2);
280 CloseDriver((HDRVR)lpDrv, 0L, 0L);
281 cause = "load1 failed";
287 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2))
288 {cause = "load failed"; goto exit;}
290 TRACE("=> %p\n", lpDrv);
293 FreeLibrary(hModule);
294 HeapFree(GetProcessHeap(), 0, lpDrv);
295 TRACE("Unable to load 32 bit module %s: %s\n", debugstr_w(fn), cause);
299 /**************************************************************************
300 * OpenDriverA [WINMM.@]
302 * (0,1,DRV_LOAD ,0 ,0)
303 * (0,1,DRV_ENABLE,0 ,0)
304 * (0,1,DRV_OPEN ,buf[256],0)
306 HDRVR WINAPI OpenDriverA(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam)
315 len = MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, NULL, 0 );
316 dn = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
318 MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, dn, len );
323 len = MultiByteToWideChar( CP_ACP, 0, lpSectionName, -1, NULL, 0 );
324 sn = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
326 MultiByteToWideChar( CP_ACP, 0, lpSectionName, -1, sn, len );
329 ret = OpenDriver(dn, sn, lParam);
332 HeapFree(GetProcessHeap(), 0, dn);
333 HeapFree(GetProcessHeap(), 0, sn);
337 /**************************************************************************
338 * OpenDriver [WINMM.@]
341 HDRVR WINAPI OpenDriver(LPCWSTR lpDriverName, LPCWSTR lpSectionName, LPARAM lParam)
343 LPWINE_DRIVER lpDrv = NULL;
345 LPCWSTR lsn = lpSectionName;
347 TRACE("(%s, %s, 0x%08lx);\n",
348 debugstr_w(lpDriverName), debugstr_w(lpSectionName), lParam);
351 static const WCHAR wszDrivers32[] = {'D','r','i','v','e','r','s','3','2',0};
352 lstrcpynW(libName, lpDriverName, sizeof(libName) / sizeof(WCHAR));
354 if ((lpDrv = DRIVER_TryOpenDriver32(libName, lParam)))
358 if (DRIVER_GetLibName(lpDriverName, lsn, libName, sizeof(libName)) &&
359 (lpDrv = DRIVER_TryOpenDriver32(libName, lParam)))
362 /* now we will try a 16 bit driver (and add all the glue to make it work... which
363 * is located in our mmsystem implementation)
364 * so ensure, we can load our mmsystem, otherwise just fail
366 WINMM_CheckForMMSystem();
367 if (pFnOpenDriver16 &&
368 (lpDrv = pFnOpenDriver16(lpDriverName, lpSectionName, lParam)))
370 if (DRIVER_AddToList(lpDrv, 0, lParam)) goto the_end;
371 HeapFree(GetProcessHeap(), 0, lpDrv);
373 TRACE("Failed to open driver %s from system.ini file, section %s\n",
374 debugstr_w(lpDriverName), debugstr_w(lpSectionName));
378 if (lpDrv) TRACE("=> %08lx\n", (DWORD)lpDrv);
382 /**************************************************************************
383 * CloseDriver [WINMM.@]
386 LRESULT WINAPI CloseDriver(HDRVR hDrvr, LPARAM lParam1, LPARAM lParam2)
390 TRACE("(%p, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
392 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL)
394 if (lpDrv->dwFlags & WINE_GDF_16BIT)
396 if (pFnCloseDriver16)
397 pFnCloseDriver16(lpDrv->d.d16.hDriver16, lParam1, lParam2);
401 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
402 lpDrv->d.d32.dwDriverID = 0;
404 if (DRIVER_RemoveFromList(lpDrv)) {
405 if (!(lpDrv->dwFlags & WINE_GDF_16BIT))
407 LPWINE_DRIVER lpDrv0;
409 /* if driver has an opened session instance, we have to close it too */
410 if (DRIVER_GetNumberOfModuleRefs(lpDrv->d.d32.hModule, &lpDrv0) == 1)
412 DRIVER_SendMessage(lpDrv0, DRV_CLOSE, 0L, 0L);
413 lpDrv0->d.d32.dwDriverID = 0;
414 DRIVER_RemoveFromList(lpDrv0);
415 FreeLibrary(lpDrv0->d.d32.hModule);
416 HeapFree(GetProcessHeap(), 0, lpDrv0);
418 FreeLibrary(lpDrv->d.d32.hModule);
420 HeapFree(GetProcessHeap(), 0, lpDrv);
424 WARN("Failed to close driver\n");
428 /**************************************************************************
429 * GetDriverFlags [WINMM.@]
430 * [in] hDrvr handle to the driver
433 * 0x00000000 if hDrvr is an invalid handle
434 * 0x80000000 if hDrvr is a valid 32 bit driver
435 * 0x90000000 if hDrvr is a valid 16 bit driver
437 * native WINMM doesn't return those flags
438 * 0x80000000 for a valid 32 bit driver and that's it
439 * (I may have mixed up the two flags :-(
441 DWORD WINAPI GetDriverFlags(HDRVR hDrvr)
446 TRACE("(%p)\n", hDrvr);
448 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
449 ret = WINE_GDF_EXIST | lpDrv->dwFlags;
454 /**************************************************************************
455 * GetDriverModuleHandle [WINMM.@]
456 * DrvGetModuleHandle [WINMM.@]
458 HMODULE WINAPI GetDriverModuleHandle(HDRVR hDrvr)
463 TRACE("(%p);\n", hDrvr);
465 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
466 if (!(lpDrv->dwFlags & WINE_GDF_16BIT))
467 hModule = lpDrv->d.d32.hModule;
469 TRACE("=> %p\n", hModule);
473 /**************************************************************************
474 * DefDriverProc [WINMM.@]
475 * DrvDefDriverProc [WINMM.@]
477 LRESULT WINAPI DefDriverProc(DWORD_PTR dwDriverIdentifier, HDRVR hDrv,
478 UINT Msg, LPARAM lParam1, LPARAM lParam2)
494 /**************************************************************************
495 * DriverCallback [WINMM.@]
497 BOOL WINAPI DriverCallback(DWORD dwCallBack, UINT uFlags, HDRVR hDev,
498 UINT wMsg, DWORD dwUser, DWORD dwParam1,
501 TRACE("(%08lX, %04X, %p, %04X, %08lX, %08lX, %08lX); !\n",
502 dwCallBack, uFlags, hDev, wMsg, dwUser, dwParam1, dwParam2);
504 switch (uFlags & DCB_TYPEMASK) {
508 WARN("uFlags=%04X has null DCB value, but dwCallBack=%08lX is not null !\n", uFlags, dwCallBack);
511 TRACE("Window(%04lX) handle=%p!\n", dwCallBack, hDev);
512 PostMessageA((HWND)dwCallBack, wMsg, (WPARAM)hDev, dwParam1);
514 case DCB_TASK: /* aka DCB_THREAD */
515 TRACE("Task(%04lx) !\n", dwCallBack);
516 PostThreadMessageA(dwCallBack, wMsg, (WPARAM)hDev, dwParam1);
519 TRACE("Function (32 bit) !\n");
520 ((LPDRVCALLBACK)dwCallBack)(hDev, wMsg, dwUser, dwParam1, dwParam2);
523 TRACE("Event(%08lx) !\n", dwCallBack);
524 SetEvent((HANDLE)dwCallBack);
526 case 6: /* I would dub it DCB_MMTHREADSIGNAL */
527 /* this is an undocumented DCB_ value used for mmThreads
528 * loword of dwCallBack contains the handle of the lpMMThd block
529 * which dwSignalCount has to be incremented
531 if (pFnGetMMThread16)
533 WINE_MMTHREAD* lpMMThd = pFnGetMMThread16(LOWORD(dwCallBack));
535 TRACE("mmThread (%04x, %p) !\n", LOWORD(dwCallBack), lpMMThd);
536 /* same as mmThreadSignal16 */
537 InterlockedIncrement(&lpMMThd->dwSignalCount);
538 SetEvent(lpMMThd->hEvent);
539 /* some other stuff on lpMMThd->hVxD */
544 /* this is an undocumented DCB_ value for... I don't know */
548 WARN("Unknown callback type %d\n", uFlags & DCB_TYPEMASK);
555 /******************************************************************
560 void DRIVER_UnloadAll(void)
563 LPWINE_DRIVER lpNextDrv = NULL;
566 for (lpDrv = lpDrvItemList; lpDrv != NULL; lpDrv = lpNextDrv)
568 lpNextDrv = lpDrv->lpNextItem;
569 CloseDriver((HDRVR)lpDrv, 0, 0);
572 TRACE("Unloaded %u drivers\n", count);