- start assigning custom VK codes from VK_OEM_8 since this code
[wine/hacks.git] / dlls / winmm / driver.c
blob4a43469049a34ee18bffd5d64f7e3e980141fd64
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
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
25 #include <string.h>
26 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winnls.h"
32 #include "mmddk.h"
33 #include "winemm.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(driver);
38 static LPWINE_DRIVER lpDrvItemList /* = NULL */;
40 WINE_MMTHREAD* (*pFnGetMMThread16)(UINT16 h) /* = NULL */;
41 LPWINE_DRIVER (*pFnOpenDriver16)(LPCSTR,LPCSTR,LPARAM) /* = NULL */;
42 LRESULT (*pFnCloseDriver16)(UINT16,LPARAM,LPARAM) /* = NULL */;
43 LRESULT (*pFnSendMessage16)(UINT16,UINT,LPARAM,LPARAM) /* = NULL */;
45 /**************************************************************************
46 * DRIVER_GetNumberOfModuleRefs [internal]
48 * Returns the number of open drivers which share the same module.
50 static unsigned DRIVER_GetNumberOfModuleRefs(HMODULE hModule, WINE_DRIVER** found)
52 LPWINE_DRIVER lpDrv;
53 unsigned count = 0;
55 if (found) *found = NULL;
56 for (lpDrv = lpDrvItemList; lpDrv; lpDrv = lpDrv->lpNextItem)
58 if (!(lpDrv->dwFlags & WINE_GDF_16BIT) && lpDrv->d.d32.hModule == hModule)
60 if (found && !*found) *found = lpDrv;
61 count++;
64 return count;
67 /**************************************************************************
68 * DRIVER_FindFromHDrvr [internal]
70 * From a hDrvr being 32 bits, returns the WINE internal structure.
72 LPWINE_DRIVER DRIVER_FindFromHDrvr(HDRVR hDrvr)
74 LPWINE_DRIVER d = (LPWINE_DRIVER)hDrvr;
76 if (hDrvr && HeapValidate(GetProcessHeap(), 0, d) && d->dwMagic == WINE_DI_MAGIC) {
77 return d;
79 return NULL;
82 /**************************************************************************
83 * DRIVER_SendMessage [internal]
85 static LRESULT inline DRIVER_SendMessage(LPWINE_DRIVER lpDrv, UINT msg,
86 LPARAM lParam1, LPARAM lParam2)
88 LRESULT ret = 0;
90 if (lpDrv->dwFlags & WINE_GDF_16BIT) {
91 /* no need to check mmsystem presence: the driver must have been opened as a 16 bit one,
93 if (pFnSendMessage16)
94 ret = pFnSendMessage16(lpDrv->d.d16.hDriver16, msg, lParam1, lParam2);
95 } else {
96 TRACE("Before call32 proc=%p drvrID=%08lx hDrv=%p wMsg=%04x p1=%08lx p2=%08lx\n",
97 lpDrv->d.d32.lpDrvProc, lpDrv->d.d32.dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
98 ret = lpDrv->d.d32.lpDrvProc(lpDrv->d.d32.dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2);
99 TRACE("After call32 proc=%p drvrID=%08lx hDrv=%p wMsg=%04x p1=%08lx p2=%08lx => %08lx\n",
100 lpDrv->d.d32.lpDrvProc, lpDrv->d.d32.dwDriverID, (HDRVR)lpDrv, msg, lParam1, lParam2, ret);
102 return ret;
105 /**************************************************************************
106 * SendDriverMessage [WINMM.@]
107 * DrvSendMessage [WINMM.@]
109 LRESULT WINAPI SendDriverMessage(HDRVR hDriver, UINT msg, LPARAM lParam1,
110 LPARAM lParam2)
112 LPWINE_DRIVER lpDrv;
113 LRESULT retval = 0;
115 TRACE("(%p, %04X, %08lX, %08lX)\n", hDriver, msg, lParam1, lParam2);
117 if ((lpDrv = DRIVER_FindFromHDrvr(hDriver)) != NULL) {
118 retval = DRIVER_SendMessage(lpDrv, msg, lParam1, lParam2);
119 } else {
120 WARN("Bad driver handle %p\n", hDriver);
122 TRACE("retval = %ld\n", retval);
124 return retval;
127 /**************************************************************************
128 * DRIVER_RemoveFromList [internal]
130 * Generates all the logic to handle driver closure / deletion
131 * Removes a driver struct to the list of open drivers.
133 static BOOL DRIVER_RemoveFromList(LPWINE_DRIVER lpDrv)
135 if (!(lpDrv->dwFlags & WINE_GDF_16BIT)) {
136 /* last of this driver in list ? */
137 if (DRIVER_GetNumberOfModuleRefs(lpDrv->d.d32.hModule, NULL) == 1) {
138 DRIVER_SendMessage(lpDrv, DRV_DISABLE, 0L, 0L);
139 DRIVER_SendMessage(lpDrv, DRV_FREE, 0L, 0L);
143 if (lpDrv->lpPrevItem)
144 lpDrv->lpPrevItem->lpNextItem = lpDrv->lpNextItem;
145 else
146 lpDrvItemList = lpDrv->lpNextItem;
147 if (lpDrv->lpNextItem)
148 lpDrv->lpNextItem->lpPrevItem = lpDrv->lpPrevItem;
149 /* trash magic number */
150 lpDrv->dwMagic ^= 0xa5a5a5a5;
152 return TRUE;
155 /**************************************************************************
156 * DRIVER_AddToList [internal]
158 * Adds a driver struct to the list of open drivers.
159 * Generates all the logic to handle driver creation / open.
161 static BOOL DRIVER_AddToList(LPWINE_DRIVER lpNewDrv, LPARAM lParam1, LPARAM lParam2)
163 lpNewDrv->dwMagic = WINE_DI_MAGIC;
164 /* First driver to be loaded for this module, need to load correctly the module */
165 if (!(lpNewDrv->dwFlags & WINE_GDF_16BIT)) {
166 /* first of this driver in list ? */
167 if (DRIVER_GetNumberOfModuleRefs(lpNewDrv->d.d32.hModule, NULL) == 0) {
168 if (DRIVER_SendMessage(lpNewDrv, DRV_LOAD, 0L, 0L) != DRV_SUCCESS) {
169 TRACE("DRV_LOAD failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
170 return FALSE;
172 /* returned value is not checked */
173 DRIVER_SendMessage(lpNewDrv, DRV_ENABLE, 0L, 0L);
177 lpNewDrv->lpNextItem = NULL;
178 if (lpDrvItemList == NULL) {
179 lpDrvItemList = lpNewDrv;
180 lpNewDrv->lpPrevItem = NULL;
181 } else {
182 LPWINE_DRIVER lpDrv = lpDrvItemList; /* find end of list */
183 while (lpDrv->lpNextItem != NULL)
184 lpDrv = lpDrv->lpNextItem;
186 lpDrv->lpNextItem = lpNewDrv;
187 lpNewDrv->lpPrevItem = lpDrv;
190 if (!(lpNewDrv->dwFlags & WINE_GDF_16BIT)) {
191 /* Now just open a new instance of a driver on this module */
192 lpNewDrv->d.d32.dwDriverID = DRIVER_SendMessage(lpNewDrv, DRV_OPEN, lParam1, lParam2);
194 if (lpNewDrv->d.d32.dwDriverID == 0) {
195 TRACE("DRV_OPEN failed on driver 0x%08lx\n", (DWORD)lpNewDrv);
196 DRIVER_RemoveFromList(lpNewDrv);
197 return FALSE;
200 return TRUE;
203 /**************************************************************************
204 * DRIVER_GetLibName [internal]
207 BOOL DRIVER_GetLibName(LPCSTR keyName, LPCSTR sectName, LPSTR buf, int sz)
209 /* should also do some registry diving */
210 return GetPrivateProfileStringA(sectName, keyName, "", buf, sz, "SYSTEM.INI");
213 /**************************************************************************
214 * DRIVER_TryOpenDriver32 [internal]
216 * Tries to load a 32 bit driver whose DLL's (module) name is fn
218 LPWINE_DRIVER DRIVER_TryOpenDriver32(LPCSTR fn, LPARAM lParam2)
220 LPWINE_DRIVER lpDrv = NULL;
221 HMODULE hModule = 0;
222 LPSTR ptr;
223 LPCSTR cause = 0;
225 TRACE("(%s, %08lX);\n", debugstr_a(fn), lParam2);
227 if ((ptr = strchr(fn, ' ')) != NULL) {
228 *ptr++ = '\0';
229 while (*ptr == ' ') ptr++;
230 if (*ptr == '\0') ptr = NULL;
233 lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
234 if (lpDrv == NULL) {cause = "OOM"; goto exit;}
236 if ((hModule = LoadLibraryA(fn)) == 0) {cause = "Not a 32 bit lib"; goto exit;}
238 lpDrv->d.d32.lpDrvProc = (DRIVERPROC)GetProcAddress(hModule, "DriverProc");
239 if (lpDrv->d.d32.lpDrvProc == NULL) {cause = "no DriverProc"; goto exit;}
241 lpDrv->dwFlags = 0;
242 lpDrv->d.d32.hModule = hModule;
243 lpDrv->d.d32.dwDriverID = 0;
245 /* Win32 installable drivers must support a two phase opening scheme:
246 * + first open with NULL as lParam2 (session instance),
247 * + then do a second open with the real non null lParam2)
249 if (DRIVER_GetNumberOfModuleRefs(lpDrv->d.d32.hModule, NULL) == 0 && lParam2)
251 LPWINE_DRIVER ret;
253 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, 0L))
255 cause = "load0 failed";
256 goto exit;
258 ret = DRIVER_TryOpenDriver32(fn, lParam2);
259 if (!ret)
261 CloseDriver((HDRVR)lpDrv, 0L, 0L);
262 cause = "load1 failed";
263 goto exit;
265 return ret;
268 if (!DRIVER_AddToList(lpDrv, (LPARAM)ptr, lParam2))
269 {cause = "load failed"; goto exit;}
271 TRACE("=> %p\n", lpDrv);
272 return lpDrv;
273 exit:
274 FreeLibrary(hModule);
275 HeapFree(GetProcessHeap(), 0, lpDrv);
276 TRACE("Unable to load 32 bit module %s: %s\n", debugstr_a(fn), cause);
277 return NULL;
280 /**************************************************************************
281 * OpenDriverA [WINMM.@]
282 * DrvOpenA [WINMM.@]
283 * (0,1,DRV_LOAD ,0 ,0)
284 * (0,1,DRV_ENABLE,0 ,0)
285 * (0,1,DRV_OPEN ,buf[256],0)
287 HDRVR WINAPI OpenDriverA(LPCSTR lpDriverName, LPCSTR lpSectionName, LPARAM lParam2)
289 LPWINE_DRIVER lpDrv = NULL;
290 char libName[128];
291 LPCSTR lsn = lpSectionName;
293 TRACE("(%s, %s, 0x%08lx);\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName), lParam2);
295 if (lsn == NULL) {
296 lstrcpynA(libName, lpDriverName, sizeof(libName));
298 if ((lpDrv = DRIVER_TryOpenDriver32(libName, lParam2)))
299 goto the_end;
300 lsn = "Drivers32";
302 if (DRIVER_GetLibName(lpDriverName, lsn, libName, sizeof(libName)) &&
303 (lpDrv = DRIVER_TryOpenDriver32(libName, lParam2)))
304 goto the_end;
306 /* now we will try a 16 bit driver (and add all the glue to make it work... which
307 * is located in our mmsystem implementation)
308 * so ensure, we can load our mmsystem, otherwise just fail
310 WINMM_CheckForMMSystem();
311 if (pFnOpenDriver16 &&
312 (lpDrv = pFnOpenDriver16(lpDriverName, lpSectionName, lParam2)))
314 if (DRIVER_AddToList(lpDrv, 0, lParam2)) goto the_end;
315 HeapFree(GetProcessHeap(), 0, lpDrv);
317 TRACE("Failed to open driver %s from system.ini file, section %s\n", debugstr_a(lpDriverName), debugstr_a(lpSectionName));
318 return 0;
320 the_end:
321 if (lpDrv) TRACE("=> %08lx\n", (DWORD)lpDrv);
322 return (HDRVR)lpDrv;
325 /**************************************************************************
326 * OpenDriver [WINMM.@]
327 * DrvOpen [WINMM.@]
329 HDRVR WINAPI OpenDriverW(LPCWSTR lpDriverName, LPCWSTR lpSectionName, LPARAM lParam)
331 INT len;
332 LPSTR dn = NULL;
333 LPSTR sn = NULL;
334 HDRVR ret;
336 if (lpDriverName)
338 len = WideCharToMultiByte( CP_ACP, 0, lpDriverName, -1, NULL, 0, NULL, NULL );
339 dn = HeapAlloc( GetProcessHeap(), 0, len );
340 if (!dn) return 0;
341 WideCharToMultiByte( CP_ACP, 0, lpDriverName, -1, dn, len, NULL, NULL );
344 if (lpSectionName)
346 len = WideCharToMultiByte( CP_ACP, 0, lpSectionName, -1, NULL, 0, NULL, NULL );
347 sn = HeapAlloc( GetProcessHeap(), 0, len );
348 if (!sn) return 0;
349 WideCharToMultiByte( CP_ACP, 0, lpSectionName, -1, sn, len, NULL, NULL );
352 ret = OpenDriverA(dn, sn, lParam);
354 if (dn) HeapFree(GetProcessHeap(), 0, dn);
355 if (sn) HeapFree(GetProcessHeap(), 0, sn);
356 return ret;
359 /**************************************************************************
360 * CloseDriver [WINMM.@]
361 * DrvClose [WINMM.@]
363 LRESULT WINAPI CloseDriver(HDRVR hDrvr, LPARAM lParam1, LPARAM lParam2)
365 LPWINE_DRIVER lpDrv;
367 TRACE("(%p, %08lX, %08lX);\n", hDrvr, lParam1, lParam2);
369 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL)
371 if (lpDrv->dwFlags & WINE_GDF_16BIT)
373 if (pFnCloseDriver16)
374 pFnCloseDriver16(lpDrv->d.d16.hDriver16, lParam1, lParam2);
376 else
378 DRIVER_SendMessage(lpDrv, DRV_CLOSE, lParam1, lParam2);
379 lpDrv->d.d32.dwDriverID = 0;
381 if (DRIVER_RemoveFromList(lpDrv)) {
382 if (!(lpDrv->dwFlags & WINE_GDF_16BIT))
384 LPWINE_DRIVER lpDrv0;
386 /* if driver has an opened session instance, we have to close it too */
387 if (DRIVER_GetNumberOfModuleRefs(lpDrv->d.d32.hModule, &lpDrv0) == 1)
389 DRIVER_SendMessage(lpDrv0, DRV_CLOSE, 0L, 0L);
390 lpDrv0->d.d32.dwDriverID = 0;
391 DRIVER_RemoveFromList(lpDrv0);
392 FreeLibrary(lpDrv0->d.d32.hModule);
393 HeapFree(GetProcessHeap(), 0, lpDrv0);
395 FreeLibrary(lpDrv->d.d32.hModule);
397 HeapFree(GetProcessHeap(), 0, lpDrv);
398 return TRUE;
401 WARN("Failed to close driver\n");
402 return FALSE;
405 /**************************************************************************
406 * GetDriverFlags [WINMM.@]
407 * [in] hDrvr handle to the driver
409 * Returns:
410 * 0x00000000 if hDrvr is an invalid handle
411 * 0x80000000 if hDrvr is a valid 32 bit driver
412 * 0x90000000 if hDrvr is a valid 16 bit driver
414 * native WINMM doesn't return those flags
415 * 0x80000000 for a valid 32 bit driver and that's it
416 * (I may have mixed up the two flags :-(
418 DWORD WINAPI GetDriverFlags(HDRVR hDrvr)
420 LPWINE_DRIVER lpDrv;
421 DWORD ret = 0;
423 TRACE("(%p)\n", hDrvr);
425 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
426 ret = WINE_GDF_EXIST | lpDrv->dwFlags;
428 return ret;
431 /**************************************************************************
432 * GetDriverModuleHandle [WINMM.@]
433 * DrvGetModuleHandle [WINMM.@]
435 HMODULE WINAPI GetDriverModuleHandle(HDRVR hDrvr)
437 LPWINE_DRIVER lpDrv;
438 HMODULE hModule = 0;
440 TRACE("(%p);\n", hDrvr);
442 if ((lpDrv = DRIVER_FindFromHDrvr(hDrvr)) != NULL) {
443 if (!(lpDrv->dwFlags & WINE_GDF_16BIT))
444 hModule = lpDrv->d.d32.hModule;
446 TRACE("=> %p\n", hModule);
447 return hModule;
450 /**************************************************************************
451 * DefDriverProc [WINMM.@]
452 * DrvDefDriverProc [WINMM.@]
454 LRESULT WINAPI DefDriverProc(DWORD dwDriverIdentifier, HDRVR hDrv,
455 UINT Msg, LPARAM lParam1, LPARAM lParam2)
457 switch (Msg) {
458 case DRV_LOAD:
459 case DRV_FREE:
460 case DRV_ENABLE:
461 case DRV_DISABLE:
462 return 1;
463 case DRV_INSTALL:
464 case DRV_REMOVE:
465 return DRV_SUCCESS;
466 default:
467 return 0;
471 /**************************************************************************
472 * DriverCallback [WINMM.@]
474 BOOL WINAPI DriverCallback(DWORD dwCallBack, UINT uFlags, HDRVR hDev,
475 UINT wMsg, DWORD dwUser, DWORD dwParam1,
476 DWORD dwParam2)
478 TRACE("(%08lX, %04X, %p, %04X, %08lX, %08lX, %08lX); !\n",
479 dwCallBack, uFlags, hDev, wMsg, dwUser, dwParam1, dwParam2);
481 switch (uFlags & DCB_TYPEMASK) {
482 case DCB_NULL:
483 TRACE("Null !\n");
484 if (dwCallBack)
485 WARN("uFlags=%04X has null DCB value, but dwCallBack=%08lX is not null !\n", uFlags, dwCallBack);
486 break;
487 case DCB_WINDOW:
488 TRACE("Window(%04lX) handle=%p!\n", dwCallBack, hDev);
489 PostMessageA((HWND)dwCallBack, wMsg, (WPARAM)hDev, dwParam1);
490 break;
491 case DCB_TASK: /* aka DCB_THREAD */
492 TRACE("Task(%04lx) !\n", dwCallBack);
493 PostThreadMessageA(dwCallBack, wMsg, (WPARAM)hDev, dwParam1);
494 break;
495 case DCB_FUNCTION:
496 TRACE("Function (32 bit) !\n");
497 ((LPDRVCALLBACK)dwCallBack)(hDev, wMsg, dwUser, dwParam1, dwParam2);
498 break;
499 case DCB_EVENT:
500 TRACE("Event(%08lx) !\n", dwCallBack);
501 SetEvent((HANDLE)dwCallBack);
502 break;
503 case 6: /* I would dub it DCB_MMTHREADSIGNAL */
504 /* this is an undocumented DCB_ value used for mmThreads
505 * loword of dwCallBack contains the handle of the lpMMThd block
506 * which dwSignalCount has to be incremented
508 if (pFnGetMMThread16)
510 WINE_MMTHREAD* lpMMThd = pFnGetMMThread16(LOWORD(dwCallBack));
512 TRACE("mmThread (%04x, %p) !\n", LOWORD(dwCallBack), lpMMThd);
513 /* same as mmThreadSignal16 */
514 InterlockedIncrement(&lpMMThd->dwSignalCount);
515 SetEvent(lpMMThd->hEvent);
516 /* some other stuff on lpMMThd->hVxD */
518 break;
519 #if 0
520 case 4:
521 /* this is an undocumented DCB_ value for... I don't know */
522 break;
523 #endif
524 default:
525 WARN("Unknown callback type %d\n", uFlags & DCB_TYPEMASK);
526 return FALSE;
528 TRACE("Done\n");
529 return TRUE;
532 /******************************************************************
533 * DRIVER_UnloadAll
537 void DRIVER_UnloadAll(void)
539 LPWINE_DRIVER lpDrv;
540 LPWINE_DRIVER lpNextDrv = NULL;
541 unsigned count = 0;
543 for (lpDrv = lpDrvItemList; lpDrv != NULL; lpDrv = lpNextDrv)
545 lpNextDrv = lpDrv->lpNextItem;
546 CloseDriver((HDRVR)lpDrv, 0, 0);
547 count++;
549 TRACE("Unloaded %u drivers\n", count);