crypt32: Don't fail if buffer is NULL, just return required size.
[wine] / dlls / dinput / keyboard.c
1 /*              DirectInput Keyboard device
2  *
3  * Copyright 1998 Marcus Meissner
4  * Copyright 1998,1999 Lionel Ulmer
5  * Copyright 2000-2001 TransGaming Technologies Inc.
6  * Copyright 2005 Raphael Junqueira
7  *
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.
12  *
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.
17  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdarg.h>
27 #include <string.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "winerror.h"
32 #include "dinput.h"
33
34 #include "dinput_private.h"
35 #include "device_private.h"
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
40
41 #define WINE_DINPUT_KEYBOARD_MAX_KEYS 256
42
43 static const IDirectInputDevice8AVtbl SysKeyboardAvt;
44 static const IDirectInputDevice8WVtbl SysKeyboardWvt;
45
46 typedef struct SysKeyboardImpl SysKeyboardImpl;
47 struct SysKeyboardImpl
48 {
49     struct IDirectInputDevice2AImpl base;
50     BYTE DInputKeyState[WINE_DINPUT_KEYBOARD_MAX_KEYS];
51 };
52
53 static void KeyboardCallback( LPDIRECTINPUTDEVICE8A iface, WPARAM wparam, LPARAM lparam )
54 {
55     SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
56     int dik_code;
57     KBDLLHOOKSTRUCT *hook = (KBDLLHOOKSTRUCT *)lparam;
58     BYTE new_diks;
59
60     if (wparam != WM_KEYDOWN && wparam != WM_KEYUP &&
61         wparam != WM_SYSKEYDOWN && wparam != WM_SYSKEYUP)
62         return;
63
64     TRACE("(%p) %ld,%ld\n", iface, wparam, lparam);
65
66     dik_code = hook->scanCode & 0xff;
67     if (hook->flags & LLKHF_EXTENDED) dik_code |= 0x80;
68
69     new_diks = hook->flags & LLKHF_UP ? 0 : 0x80;
70
71     /* returns now if key event already known */
72     if (new_diks == This->DInputKeyState[dik_code])
73         return;
74
75     This->DInputKeyState[dik_code] = new_diks;
76     TRACE(" setting %02X to %02X\n", dik_code, This->DInputKeyState[dik_code]);
77       
78     dik_code = id_to_offset(&This->base.data_format, DIDFT_MAKEINSTANCE(dik_code) | DIDFT_PSHBUTTON);
79     EnterCriticalSection(&This->base.crit);
80     queue_event((LPDIRECTINPUTDEVICE8A)This, dik_code, new_diks, hook->time, This->base.dinput->evsequence++);
81     LeaveCriticalSection(&This->base.crit);
82 }
83
84 const GUID DInput_Wine_Keyboard_GUID = { /* 0ab8648a-7735-11d2-8c73-71df54a96441 */
85     0x0ab8648a, 0x7735, 0x11d2, {0x8c, 0x73, 0x71, 0xdf, 0x54, 0xa9, 0x64, 0x41}
86 };
87
88 static void fill_keyboard_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version) {
89     DWORD dwSize;
90     DIDEVICEINSTANCEA ddi;
91     
92     dwSize = lpddi->dwSize;
93
94     TRACE("%d %p\n", dwSize, lpddi);
95     
96     memset(lpddi, 0, dwSize);
97     memset(&ddi, 0, sizeof(ddi));
98
99     ddi.dwSize = dwSize;
100     ddi.guidInstance = GUID_SysKeyboard;/* DInput's GUID */
101     ddi.guidProduct = DInput_Wine_Keyboard_GUID; /* Vendor's GUID */
102     if (version >= 0x0800)
103         ddi.dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
104     else
105         ddi.dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
106     strcpy(ddi.tszInstanceName, "Keyboard");
107     strcpy(ddi.tszProductName, "Wine Keyboard");
108
109     memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
110 }
111
112 static void fill_keyboard_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version) {
113     DWORD dwSize;
114     DIDEVICEINSTANCEW ddi;
115     
116     dwSize = lpddi->dwSize;
117
118     TRACE("%d %p\n", dwSize, lpddi);
119     
120     memset(lpddi, 0, dwSize);
121     memset(&ddi, 0, sizeof(ddi));
122  
123     ddi.dwSize = dwSize;
124     ddi.guidInstance = GUID_SysKeyboard;/* DInput's GUID */
125     ddi.guidProduct = DInput_Wine_Keyboard_GUID; /* Vendor's GUID */
126     if (version >= 0x0800)
127         ddi.dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
128     else
129         ddi.dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
130     MultiByteToWideChar(CP_ACP, 0, "Keyboard", -1, ddi.tszInstanceName, MAX_PATH);
131     MultiByteToWideChar(CP_ACP, 0, "Wine Keyboard", -1, ddi.tszProductName, MAX_PATH);
132
133     memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
134 }
135  
136 static BOOL keyboarddev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
137 {
138   if (id != 0)
139     return FALSE;
140
141   if ((dwDevType == 0) ||
142       ((dwDevType == DIDEVTYPE_KEYBOARD) && (version < 0x0800)) ||
143       (((dwDevType == DI8DEVCLASS_KEYBOARD) || (dwDevType == DI8DEVTYPE_KEYBOARD)) && (version >= 0x0800))) {
144     TRACE("Enumerating the Keyboard device\n");
145  
146     fill_keyboard_dideviceinstanceA(lpddi, version);
147     
148     return TRUE;
149   }
150
151   return FALSE;
152 }
153
154 static BOOL keyboarddev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
155 {
156   if (id != 0)
157     return FALSE;
158
159   if ((dwDevType == 0) ||
160       ((dwDevType == DIDEVTYPE_KEYBOARD) && (version < 0x0800)) ||
161       (((dwDevType == DI8DEVCLASS_KEYBOARD) || (dwDevType == DI8DEVTYPE_KEYBOARD)) && (version >= 0x0800))) {
162     TRACE("Enumerating the Keyboard device\n");
163
164     fill_keyboard_dideviceinstanceW(lpddi, version);
165     
166     return TRUE;
167   }
168
169   return FALSE;
170 }
171
172 static SysKeyboardImpl *alloc_device(REFGUID rguid, const void *kvt, IDirectInputImpl *dinput)
173 {
174     SysKeyboardImpl* newDevice;
175     LPDIDATAFORMAT df = NULL;
176     int i, idx = 0;
177
178     newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SysKeyboardImpl));
179     newDevice->base.lpVtbl = kvt;
180     newDevice->base.ref = 1;
181     memcpy(&newDevice->base.guid, rguid, sizeof(*rguid));
182     newDevice->base.dinput = dinput;
183     newDevice->base.event_proc = KeyboardCallback;
184     InitializeCriticalSection(&newDevice->base.crit);
185     newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": SysKeyboardImpl*->base.crit");
186
187     /* Create copy of default data format */
188     if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIKeyboard.dwSize))) goto failed;
189     memcpy(df, &c_dfDIKeyboard, c_dfDIKeyboard.dwSize);
190     if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto failed;
191
192     for (i = 0; i < df->dwNumObjs; i++)
193     {
194         char buf[MAX_PATH];
195
196         if (!GetKeyNameTextA(((i & 0x7f) << 16) | ((i & 0x80) << 17), buf, sizeof(buf)))
197             continue;
198
199         memcpy(&df->rgodf[idx], &c_dfDIKeyboard.rgodf[i], df->dwObjSize);
200         df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
201     }
202     df->dwNumObjs = idx;
203
204     newDevice->base.data_format.wine_df = df;
205     IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->base.dinput);
206     return newDevice;
207
208 failed:
209     if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
210     HeapFree(GetProcessHeap(), 0, df);
211     HeapFree(GetProcessHeap(), 0, newDevice);
212     return NULL;
213 }
214
215
216 static HRESULT keyboarddev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
217 {
218   if ((IsEqualGUID(&GUID_SysKeyboard,rguid)) ||          /* Generic Keyboard */
219       (IsEqualGUID(&DInput_Wine_Keyboard_GUID,rguid))) { /* Wine Keyboard */
220     if ((riid == NULL) ||
221         IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
222         IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
223         IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
224         IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
225       *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &SysKeyboardAvt, dinput);
226       TRACE("Creating a Keyboard device (%p)\n", *pdev);
227       if (!*pdev) return DIERR_OUTOFMEMORY;
228       return DI_OK;
229     } else
230       return DIERR_NOINTERFACE;
231   }
232   return DIERR_DEVICENOTREG;
233 }
234
235 static HRESULT keyboarddev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
236 {
237   if ((IsEqualGUID(&GUID_SysKeyboard,rguid)) ||          /* Generic Keyboard */
238       (IsEqualGUID(&DInput_Wine_Keyboard_GUID,rguid))) { /* Wine Keyboard */
239     if ((riid == NULL) ||
240         IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
241         IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
242         IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
243         IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
244       *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &SysKeyboardWvt, dinput);
245       TRACE("Creating a Keyboard device (%p)\n", *pdev);
246       if (!*pdev) return DIERR_OUTOFMEMORY;
247       return DI_OK;
248     } else
249       return DIERR_NOINTERFACE;
250   }
251   return DIERR_DEVICENOTREG;
252 }
253
254 const struct dinput_device keyboard_device = {
255   "Wine keyboard driver",
256   keyboarddev_enum_deviceA,
257   keyboarddev_enum_deviceW,
258   keyboarddev_create_deviceA,
259   keyboarddev_create_deviceW
260 };
261
262 static HRESULT WINAPI SysKeyboardAImpl_GetDeviceState(
263         LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
264 )
265 {
266     SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
267     TRACE("(%p)->(%d,%p)\n", This, len, ptr);
268
269     if (!This->base.acquired) return DIERR_NOTACQUIRED;
270
271     if (len != WINE_DINPUT_KEYBOARD_MAX_KEYS)
272         return DIERR_INVALIDPARAM;
273
274     EnterCriticalSection(&This->base.crit);
275
276     if (TRACE_ON(dinput)) {
277         int i;
278         for (i = 0; i < WINE_DINPUT_KEYBOARD_MAX_KEYS; i++) {
279             if (This->DInputKeyState[i] != 0x00)
280                 TRACE(" - %02X: %02x\n", i, This->DInputKeyState[i]);
281         }
282     }
283     
284     memcpy(ptr, This->DInputKeyState, WINE_DINPUT_KEYBOARD_MAX_KEYS);
285     LeaveCriticalSection(&This->base.crit);
286
287     return DI_OK;
288 }
289
290 /******************************************************************************
291   *     GetCapabilities : get the device capablitites
292   */
293 static HRESULT WINAPI SysKeyboardAImpl_GetCapabilities(
294         LPDIRECTINPUTDEVICE8A iface,
295         LPDIDEVCAPS lpDIDevCaps)
296 {
297     SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
298     DIDEVCAPS devcaps;
299
300     TRACE("(this=%p,%p)\n",This,lpDIDevCaps);
301
302     if ((lpDIDevCaps->dwSize != sizeof(DIDEVCAPS)) && (lpDIDevCaps->dwSize != sizeof(DIDEVCAPS_DX3))) {
303         WARN("invalid parameter\n");
304         return DIERR_INVALIDPARAM;
305     }
306     
307     devcaps.dwSize = lpDIDevCaps->dwSize;
308     devcaps.dwFlags = DIDC_ATTACHED;
309     if (This->base.dinput->dwVersion >= 0x0800)
310         devcaps.dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
311     else
312         devcaps.dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
313     devcaps.dwAxes = 0;
314     devcaps.dwButtons = This->base.data_format.wine_df->dwNumObjs;
315     devcaps.dwPOVs = 0;
316     devcaps.dwFFSamplePeriod = 0;
317     devcaps.dwFFMinTimeResolution = 0;
318     devcaps.dwFirmwareRevision = 100;
319     devcaps.dwHardwareRevision = 100;
320     devcaps.dwFFDriverVersion = 0;
321
322     memcpy(lpDIDevCaps, &devcaps, lpDIDevCaps->dwSize);
323     
324     return DI_OK;
325 }
326
327 /******************************************************************************
328   *     GetObjectInfo : get information about a device object such as a button
329   *                     or axis
330   */
331 static HRESULT WINAPI
332 SysKeyboardAImpl_GetObjectInfo(
333         LPDIRECTINPUTDEVICE8A iface,
334         LPDIDEVICEOBJECTINSTANCEA pdidoi,
335         DWORD dwObj,
336         DWORD dwHow)
337 {
338     HRESULT res;
339
340     res = IDirectInputDevice2AImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
341     if (res != DI_OK) return res;
342
343     if (!GetKeyNameTextA((DIDFT_GETINSTANCE(pdidoi->dwType) & 0x80) << 17 |
344                          (DIDFT_GETINSTANCE(pdidoi->dwType) & 0x7f) << 16,
345                          pdidoi->tszName, sizeof(pdidoi->tszName)))
346         return DIERR_OBJECTNOTFOUND;
347
348     _dump_OBJECTINSTANCEA(pdidoi);
349     return res;
350 }
351
352 static HRESULT WINAPI SysKeyboardWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
353                                                      LPDIDEVICEOBJECTINSTANCEW pdidoi,
354                                                      DWORD dwObj,
355                                                      DWORD dwHow)
356 {
357     HRESULT res;
358
359     res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
360     if (res != DI_OK) return res;
361
362     if (!GetKeyNameTextW((DIDFT_GETINSTANCE(pdidoi->dwType) & 0x80) << 17 |
363                          (DIDFT_GETINSTANCE(pdidoi->dwType) & 0x7f) << 16,
364                          pdidoi->tszName, sizeof(pdidoi->tszName)))
365         return DIERR_OBJECTNOTFOUND;
366
367     _dump_OBJECTINSTANCEW(pdidoi);
368     return res;
369 }
370
371 /******************************************************************************
372   *     GetDeviceInfo : get information about a device's identity
373   */
374 static HRESULT WINAPI SysKeyboardAImpl_GetDeviceInfo(
375         LPDIRECTINPUTDEVICE8A iface,
376         LPDIDEVICEINSTANCEA pdidi)
377 {
378     SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
379     TRACE("(this=%p,%p)\n", This, pdidi);
380
381     if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)) {
382         WARN(" dinput3 not supported yet...\n");
383         return DI_OK;
384     }
385
386     fill_keyboard_dideviceinstanceA(pdidi, This->base.dinput->dwVersion);
387     
388     return DI_OK;
389 }
390
391 static HRESULT WINAPI SysKeyboardWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW pdidi) 
392 {
393     SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
394     TRACE("(this=%p,%p)\n", This, pdidi);
395
396     if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)) {
397         WARN(" dinput3 not supported yet...\n");
398         return DI_OK;
399     }
400
401     fill_keyboard_dideviceinstanceW(pdidi, This->base.dinput->dwVersion);
402     
403     return DI_OK;
404 }
405
406 static const IDirectInputDevice8AVtbl SysKeyboardAvt =
407 {
408         IDirectInputDevice2AImpl_QueryInterface,
409         IDirectInputDevice2AImpl_AddRef,
410         IDirectInputDevice2AImpl_Release,
411         SysKeyboardAImpl_GetCapabilities,
412         IDirectInputDevice2AImpl_EnumObjects,
413         IDirectInputDevice2AImpl_GetProperty,
414         IDirectInputDevice2AImpl_SetProperty,
415         IDirectInputDevice2AImpl_Acquire,
416         IDirectInputDevice2AImpl_Unacquire,
417         SysKeyboardAImpl_GetDeviceState,
418         IDirectInputDevice2AImpl_GetDeviceData,
419         IDirectInputDevice2AImpl_SetDataFormat,
420         IDirectInputDevice2AImpl_SetEventNotification,
421         IDirectInputDevice2AImpl_SetCooperativeLevel,
422         SysKeyboardAImpl_GetObjectInfo,
423         SysKeyboardAImpl_GetDeviceInfo,
424         IDirectInputDevice2AImpl_RunControlPanel,
425         IDirectInputDevice2AImpl_Initialize,
426         IDirectInputDevice2AImpl_CreateEffect,
427         IDirectInputDevice2AImpl_EnumEffects,
428         IDirectInputDevice2AImpl_GetEffectInfo,
429         IDirectInputDevice2AImpl_GetForceFeedbackState,
430         IDirectInputDevice2AImpl_SendForceFeedbackCommand,
431         IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
432         IDirectInputDevice2AImpl_Escape,
433         IDirectInputDevice2AImpl_Poll,
434         IDirectInputDevice2AImpl_SendDeviceData,
435         IDirectInputDevice7AImpl_EnumEffectsInFile,
436         IDirectInputDevice7AImpl_WriteEffectToFile,
437         IDirectInputDevice8AImpl_BuildActionMap,
438         IDirectInputDevice8AImpl_SetActionMap,
439         IDirectInputDevice8AImpl_GetImageInfo
440 };
441
442 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
443 # define XCAST(fun)     (typeof(SysKeyboardWvt.fun))
444 #else
445 # define XCAST(fun)     (void*)
446 #endif
447
448 static const IDirectInputDevice8WVtbl SysKeyboardWvt =
449 {
450         IDirectInputDevice2WImpl_QueryInterface,
451         XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
452         XCAST(Release)IDirectInputDevice2AImpl_Release,
453         XCAST(GetCapabilities)SysKeyboardAImpl_GetCapabilities,
454         IDirectInputDevice2WImpl_EnumObjects,
455         XCAST(GetProperty)IDirectInputDevice2AImpl_GetProperty,
456         XCAST(SetProperty)IDirectInputDevice2AImpl_SetProperty,
457         XCAST(Acquire)IDirectInputDevice2AImpl_Acquire,
458         XCAST(Unacquire)IDirectInputDevice2AImpl_Unacquire,
459         XCAST(GetDeviceState)SysKeyboardAImpl_GetDeviceState,
460         XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
461         XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
462         XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
463         XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
464         SysKeyboardWImpl_GetObjectInfo,
465         SysKeyboardWImpl_GetDeviceInfo,
466         XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
467         XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
468         XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
469         IDirectInputDevice2WImpl_EnumEffects,
470         IDirectInputDevice2WImpl_GetEffectInfo,
471         XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
472         XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
473         XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
474         XCAST(Escape)IDirectInputDevice2AImpl_Escape,
475         XCAST(Poll)IDirectInputDevice2AImpl_Poll,
476         XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
477         IDirectInputDevice7WImpl_EnumEffectsInFile,
478         IDirectInputDevice7WImpl_WriteEffectToFile,
479         IDirectInputDevice8WImpl_BuildActionMap,
480         IDirectInputDevice8WImpl_SetActionMap,
481         IDirectInputDevice8WImpl_GetImageInfo
482 };
483 #undef XCAST