mshtml: Don't crash in QueryInterface if uri is NULL.
[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     const void                 *lpVtbl;
50     LONG                        ref;
51     GUID                        guid;
52
53     IDirectInputImpl*           dinput;
54
55     HANDLE                      hEvent;
56     /* SysKeyboardAImpl */
57     int                         acquired;
58
59     LPDIDEVICEOBJECTDATA        data_queue;  /* buffer for 'GetDeviceData'. Alloc at
60                                                 'Acquire', Free at 'Unacquire'              */
61     int                         queue_len;   /* size of the queue - set in 'SetProperty'    */
62     int                         queue_head;  /* position to write new event into queue      */
63     int                         queue_tail;  /* next event to read from queue               */
64     BOOL                        overflow;    /* return DI_BUFFEROVERFLOW in 'GetDeviceData' */
65     CRITICAL_SECTION            crit;
66 };
67
68 static SysKeyboardImpl* current_lock = NULL; 
69 /* Today's acquired device
70  * FIXME: currently this can be only one.
71  * Maybe this should be a linked list or st.
72  * I don't know what the rules are for multiple acquired keyboards,
73  * but 'DI_LOSTFOCUS' and 'DI_UNACQUIRED' exist for a reason.
74 */
75
76 static BYTE DInputKeyState[WINE_DINPUT_KEYBOARD_MAX_KEYS]; /* array for 'GetDeviceState' */
77
78 LRESULT CALLBACK KeyboardCallback( int code, WPARAM wparam, LPARAM lparam )
79 {
80     SysKeyboardImpl *This = (SysKeyboardImpl *)current_lock;
81     int dik_code;
82     KBDLLHOOKSTRUCT *hook = (KBDLLHOOKSTRUCT *)lparam;
83     BYTE new_diks;
84
85     TRACE("(%d,%d,%ld)\n", code, wparam, lparam);
86
87     /* returns now if not HC_ACTION */
88     if (code != HC_ACTION) return CallNextHookEx(0, code, wparam, lparam);
89   
90     dik_code = hook->scanCode & 0xff;
91     if (hook->flags & LLKHF_EXTENDED) dik_code |= 0x80;
92
93     new_diks = hook->flags & LLKHF_UP ? 0 : 0x80;
94
95     /* returns now if key event already known */
96     if (new_diks == DInputKeyState[dik_code])
97         return CallNextHookEx(0, code, wparam, lparam);
98
99     DInputKeyState[dik_code] = new_diks;
100     TRACE(" setting %02X to %02X\n", dik_code, DInputKeyState[dik_code]);
101       
102     EnterCriticalSection(&This->crit);
103     GEN_EVENT(dik_code, new_diks, hook->time, This->dinput->evsequence++);
104     LeaveCriticalSection(&This->crit);
105
106     if (This->hEvent) SetEvent(This->hEvent);
107     
108     return CallNextHookEx(0, code, wparam, lparam);
109 }
110
111 static GUID DInput_Wine_Keyboard_GUID = { /* 0ab8648a-7735-11d2-8c73-71df54a96441 */
112   0x0ab8648a,
113   0x7735,
114   0x11d2,
115   {0x8c, 0x73, 0x71, 0xdf, 0x54, 0xa9, 0x64, 0x41}
116 };
117
118 static void fill_keyboard_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version) {
119     DWORD dwSize;
120     DIDEVICEINSTANCEA ddi;
121     
122     dwSize = lpddi->dwSize;
123
124     TRACE("%ld %p\n", dwSize, lpddi);
125     
126     memset(lpddi, 0, dwSize);
127     memset(&ddi, 0, sizeof(ddi));
128
129     ddi.dwSize = dwSize;
130     ddi.guidInstance = GUID_SysKeyboard;/* DInput's GUID */
131     ddi.guidProduct = DInput_Wine_Keyboard_GUID; /* Vendor's GUID */
132     if (version >= 0x0800)
133         ddi.dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
134     else
135         ddi.dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
136     strcpy(ddi.tszInstanceName, "Keyboard");
137     strcpy(ddi.tszProductName, "Wine Keyboard");
138
139     memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
140 }
141
142 static void fill_keyboard_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version) {
143     DWORD dwSize;
144     DIDEVICEINSTANCEW ddi;
145     
146     dwSize = lpddi->dwSize;
147
148     TRACE("%ld %p\n", dwSize, lpddi);
149     
150     memset(lpddi, 0, dwSize);
151     memset(&ddi, 0, sizeof(ddi));
152  
153     ddi.dwSize = dwSize;
154     ddi.guidInstance = GUID_SysKeyboard;/* DInput's GUID */
155     ddi.guidProduct = DInput_Wine_Keyboard_GUID; /* Vendor's GUID */
156     if (version >= 0x0800)
157         ddi.dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
158     else
159         ddi.dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
160     MultiByteToWideChar(CP_ACP, 0, "Keyboard", -1, ddi.tszInstanceName, MAX_PATH);
161     MultiByteToWideChar(CP_ACP, 0, "Wine Keyboard", -1, ddi.tszProductName, MAX_PATH);
162
163     memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
164 }
165  
166 static BOOL keyboarddev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
167 {
168   if (id != 0)
169     return FALSE;
170
171   if ((dwDevType == 0) ||
172       ((dwDevType == DIDEVTYPE_KEYBOARD) && (version < 0x0800)) ||
173       (((dwDevType == DI8DEVCLASS_KEYBOARD) || (dwDevType == DI8DEVTYPE_KEYBOARD)) && (version >= 0x0800))) {
174     TRACE("Enumerating the Keyboard device\n");
175  
176     fill_keyboard_dideviceinstanceA(lpddi, version);
177     
178     return TRUE;
179   }
180
181   return FALSE;
182 }
183
184 static BOOL keyboarddev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
185 {
186   if (id != 0)
187     return FALSE;
188
189   if ((dwDevType == 0) ||
190       ((dwDevType == DIDEVTYPE_KEYBOARD) && (version < 0x0800)) ||
191       (((dwDevType == DI8DEVCLASS_KEYBOARD) || (dwDevType == DI8DEVTYPE_KEYBOARD)) && (version >= 0x0800))) {
192     TRACE("Enumerating the Keyboard device\n");
193
194     fill_keyboard_dideviceinstanceW(lpddi, version);
195     
196     return TRUE;
197   }
198
199   return FALSE;
200 }
201
202 static SysKeyboardImpl *alloc_device(REFGUID rguid, const void *kvt, IDirectInputImpl *dinput)
203 {
204     SysKeyboardImpl* newDevice;
205
206     newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SysKeyboardImpl));
207     newDevice->lpVtbl = kvt;
208     newDevice->ref = 1;
209     memcpy(&(newDevice->guid),rguid,sizeof(*rguid));
210     newDevice->dinput = dinput;
211     InitializeCriticalSection(&(newDevice->crit));
212
213     return newDevice;
214 }
215
216
217 static HRESULT keyboarddev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
218 {
219   if ((IsEqualGUID(&GUID_SysKeyboard,rguid)) ||          /* Generic Keyboard */
220       (IsEqualGUID(&DInput_Wine_Keyboard_GUID,rguid))) { /* Wine Keyboard */
221     if ((riid == NULL) ||
222         IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
223         IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
224         IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
225         IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
226       *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &SysKeyboardAvt, dinput);
227       TRACE("Creating a Keyboard device (%p)\n", *pdev);
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       return DI_OK;
247     } else
248       return DIERR_NOINTERFACE;
249   }
250   return DIERR_DEVICENOTREG;
251 }
252
253 const struct dinput_device keyboard_device = {
254   "Wine keyboard driver",
255   keyboarddev_enum_deviceA,
256   keyboarddev_enum_deviceW,
257   keyboarddev_create_deviceA,
258   keyboarddev_create_deviceW
259 };
260
261 static ULONG WINAPI SysKeyboardAImpl_Release(LPDIRECTINPUTDEVICE8A iface)
262 {
263     SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
264     ULONG ref;
265
266     ref = InterlockedDecrement(&(This->ref));
267     if (ref) return ref;
268
269     set_dinput_hook(WH_KEYBOARD_LL, NULL);
270
271     /* Free the data queue */
272     HeapFree(GetProcessHeap(), 0, This->data_queue);
273
274     DeleteCriticalSection(&(This->crit));
275
276     HeapFree(GetProcessHeap(),0,This);
277     return DI_OK;
278 }
279
280 static HRESULT WINAPI SysKeyboardAImpl_SetProperty(
281         LPDIRECTINPUTDEVICE8A iface,REFGUID rguid,LPCDIPROPHEADER ph
282 )
283 {
284         SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
285
286         TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
287         TRACE("(size=%ld,headersize=%ld,obj=%ld,how=%ld\n",
288             ph->dwSize,ph->dwHeaderSize,ph->dwObj,ph->dwHow);
289         if (!HIWORD(rguid)) {
290                 switch (LOWORD(rguid)) {
291                 case (DWORD) DIPROP_BUFFERSIZE: {
292                         LPCDIPROPDWORD  pd = (LPCDIPROPDWORD)ph;
293
294                         TRACE("(buffersize=%ld)\n",pd->dwData);
295
296                         if (This->acquired)
297                            return DIERR_INVALIDPARAM;
298
299                         This->queue_len = pd->dwData;
300
301                         break;
302                 }
303                 default:
304                         WARN("Unknown type %p\n",rguid);
305                         break;
306                 }
307         }
308         return DI_OK;
309 }
310
311 static HRESULT WINAPI SysKeyboardAImpl_GetProperty(
312         LPDIRECTINPUTDEVICE8A iface,REFGUID rguid,LPDIPROPHEADER ph
313 )
314 {
315         SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
316
317         TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
318         TRACE("(size=%ld,headersize=%ld,obj=%ld,how=%ld\n",
319             ph->dwSize,ph->dwHeaderSize,ph->dwObj,ph->dwHow);
320         if (!HIWORD(rguid)) {
321                 switch (LOWORD(rguid)) {
322                 case (DWORD) DIPROP_BUFFERSIZE: {
323                         LPDIPROPDWORD   pd = (LPDIPROPDWORD)ph;
324
325                         TRACE("(buffersize=%ld)\n",pd->dwData);
326
327                         if (This->acquired)
328                            return DIERR_INVALIDPARAM;
329
330                         pd->dwData = This->queue_len;
331
332                         break;
333                 }
334                 default:
335                         WARN("Unknown type %p\n",rguid);
336                         break;
337                 }
338         }
339         return DI_OK;
340 }
341
342 static HRESULT WINAPI SysKeyboardAImpl_GetDeviceState(
343         LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
344 )
345 {
346     SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
347     TRACE("(%p)->(%ld,%p)\n", This, len, ptr);
348
349     if (This->acquired == 0) return DIERR_NOTACQUIRED;
350
351     if (len != WINE_DINPUT_KEYBOARD_MAX_KEYS)
352       return DIERR_INVALIDPARAM;
353
354     MsgWaitForMultipleObjectsEx(0, NULL, 0, QS_ALLINPUT, 0);
355
356     EnterCriticalSection(&(This->crit));
357
358     if (TRACE_ON(dinput)) {
359         int i;
360         for (i = 0; i < WINE_DINPUT_KEYBOARD_MAX_KEYS; i++) {
361             if (DInputKeyState[i] != 0x00) {
362                 TRACE(" - %02X: %02x\n", i, DInputKeyState[i]);
363             }
364         }
365     }
366     
367     memcpy(ptr, DInputKeyState, WINE_DINPUT_KEYBOARD_MAX_KEYS);
368     LeaveCriticalSection(&(This->crit));
369
370     return DI_OK;
371 }
372
373 static HRESULT WINAPI SysKeyboardAImpl_GetDeviceData(
374         LPDIRECTINPUTDEVICE8A iface,DWORD dodsize,LPDIDEVICEOBJECTDATA dod,
375         LPDWORD entries,DWORD flags
376 )
377 {
378     SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
379     HRESULT ret = DI_OK;
380     int len;
381
382     TRACE("(%p) %p -> %p(%ld) x%ld, 0x%08lx\n",
383           This, dod, entries, entries ? *entries : 0, dodsize, flags);
384
385     if (!This->acquired)
386         return DIERR_NOTACQUIRED;
387
388     if (!This->data_queue)
389         return DIERR_NOTBUFFERED;
390
391     if (dodsize < sizeof(DIDEVICEOBJECTDATA_DX3))
392         return DIERR_INVALIDPARAM;
393
394     MsgWaitForMultipleObjectsEx(0, NULL, 0, QS_ALLINPUT, 0);
395     EnterCriticalSection(&This->crit);
396
397     len = ((This->queue_head < This->queue_tail) ? This->queue_len : 0) +
398             This->queue_head - This->queue_tail;
399     if ((*entries != INFINITE) && (len > *entries)) len = *entries;
400
401     if (dod)
402     {
403         int i;
404         for(i = 0; i < len; i++)
405         {
406             int n = (This->queue_tail + i) % This->queue_len;
407             memcpy((char *)dod + dodsize * i, This->data_queue + n, dodsize);
408         }
409     }
410     *entries = len;
411
412     if (This->overflow)
413         ret = DI_BUFFEROVERFLOW;
414
415     if (!(flags & DIGDD_PEEK))
416     {
417         /* Empty buffer */
418         This->queue_tail = (This->queue_tail + len) % This->queue_len;
419         This->overflow = FALSE;
420     }
421
422     LeaveCriticalSection(&This->crit);
423
424     TRACE("Returning %ld events queued\n", *entries);
425     return ret;
426 }
427
428 static HRESULT WINAPI SysKeyboardAImpl_EnumObjects(
429         LPDIRECTINPUTDEVICE8A iface,
430         LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback,
431         LPVOID lpvRef,
432         DWORD dwFlags)
433 {
434     SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
435     DIDEVICEOBJECTINSTANCEA ddoi;
436     int i;
437     
438     TRACE("(this=%p,%p,%p,%08lx)\n", This, lpCallback, lpvRef, dwFlags);
439     if (TRACE_ON(dinput)) {
440         TRACE("  - flags = ");
441         _dump_EnumObjects_flags(dwFlags);
442         TRACE("\n");
443     }
444
445     /* Only the fields till dwFFMaxForce are relevant */
446     memset(&ddoi, 0, sizeof(ddoi));
447     ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA, dwFFMaxForce);
448
449     for (i = 0; i < WINE_DINPUT_KEYBOARD_MAX_KEYS; i++) {
450         /* Report 255 keys :-) */
451         ddoi.guidType = GUID_Key;
452         ddoi.dwOfs = i;
453         ddoi.dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_BUTTON;
454         GetKeyNameTextA(((i & 0x7f) << 16) | ((i & 0x80) << 17), ddoi.tszName, sizeof(ddoi.tszName));
455         _dump_OBJECTINSTANCEA(&ddoi);
456         if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
457     }
458     
459     return DI_OK;
460 }
461
462 static HRESULT WINAPI SysKeyboardWImpl_EnumObjects(LPDIRECTINPUTDEVICE8W iface,
463                                                    LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback,
464                                                    LPVOID lpvRef,
465                                                    DWORD dwFlags)
466 {
467   SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
468
469   device_enumobjects_AtoWcb_data data;
470
471   data.lpCallBack = lpCallback;
472   data.lpvRef = lpvRef;
473
474   return SysKeyboardAImpl_EnumObjects((LPDIRECTINPUTDEVICE8A) This, (LPDIENUMDEVICEOBJECTSCALLBACKA) DIEnumDevicesCallbackAtoW, (LPVOID) &data, dwFlags);
475 }
476
477 static HRESULT WINAPI SysKeyboardAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface);
478
479 static HRESULT WINAPI SysKeyboardAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
480 {
481     SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
482
483     TRACE("(%p)\n",This);
484
485     if (This->acquired) return DI_NOEFFECT;
486     This->acquired = 1;
487
488     if (current_lock != NULL) {
489         FIXME("Not more than one keyboard can be acquired at the same time.\n");
490         SysKeyboardAImpl_Unacquire((LPDIRECTINPUTDEVICE8A)current_lock);
491     }
492     current_lock = This;
493
494     if (This->queue_len > 0)
495     {
496         This->data_queue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
497                                      This->queue_len * sizeof(*(This->data_queue)));
498         This->queue_head = 0;
499         This->queue_tail = 0;
500         This->overflow = FALSE;
501     } else
502         This->data_queue = NULL;
503
504     set_dinput_hook(WH_KEYBOARD_LL, KeyboardCallback);
505
506     return DI_OK;
507 }
508
509 static HRESULT WINAPI SysKeyboardAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
510 {
511         SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
512         TRACE("(this=%p)\n",This);
513
514         if (This->acquired == 0)
515           return DI_NOEFFECT;
516
517         set_dinput_hook(WH_KEYBOARD_LL, NULL);
518
519         /* No more locks */
520         if (current_lock == This)
521           current_lock = NULL;
522         else
523           ERR("this != current_lock\n");
524
525         /* Unacquire device */
526         This->acquired = 0;
527
528     if (This->queue_len >= 0) {
529         HeapFree(GetProcessHeap(), 0, This->data_queue);
530         This->data_queue = NULL;
531     }
532
533     return DI_OK;
534 }
535
536 static HRESULT WINAPI SysKeyboardAImpl_SetEventNotification(LPDIRECTINPUTDEVICE8A iface,
537                                                             HANDLE hnd) {
538   SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
539
540   TRACE("(this=%p,%p)\n",This,hnd);
541
542   This->hEvent = hnd;
543   return DI_OK;
544 }
545
546 /******************************************************************************
547   *     GetCapabilities : get the device capablitites
548   */
549 static HRESULT WINAPI SysKeyboardAImpl_GetCapabilities(
550         LPDIRECTINPUTDEVICE8A iface,
551         LPDIDEVCAPS lpDIDevCaps)
552 {
553     SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
554     DIDEVCAPS devcaps;
555
556     TRACE("(this=%p,%p)\n",This,lpDIDevCaps);
557
558     if ((lpDIDevCaps->dwSize != sizeof(DIDEVCAPS)) && (lpDIDevCaps->dwSize != sizeof(DIDEVCAPS_DX3))) {
559         WARN("invalid parameter\n");
560         return DIERR_INVALIDPARAM;
561     }
562     
563     devcaps.dwSize = lpDIDevCaps->dwSize;
564     devcaps.dwFlags = DIDC_ATTACHED;
565     if (This->dinput->dwVersion >= 0x0800)
566         devcaps.dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
567     else
568         devcaps.dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
569     devcaps.dwAxes = 0;
570     devcaps.dwButtons = WINE_DINPUT_KEYBOARD_MAX_KEYS;
571     devcaps.dwPOVs = 0;
572     devcaps.dwFFSamplePeriod = 0;
573     devcaps.dwFFMinTimeResolution = 0;
574     devcaps.dwFirmwareRevision = 100;
575     devcaps.dwHardwareRevision = 100;
576     devcaps.dwFFDriverVersion = 0;
577
578     memcpy(lpDIDevCaps, &devcaps, lpDIDevCaps->dwSize);
579     
580     return DI_OK;
581 }
582
583 /******************************************************************************
584   *     GetObjectInfo : get information about a device object such as a button
585   *                     or axis
586   */
587 static HRESULT WINAPI
588 SysKeyboardAImpl_GetObjectInfo(
589         LPDIRECTINPUTDEVICE8A iface,
590         LPDIDEVICEOBJECTINSTANCEA pdidoi,
591         DWORD dwObj,
592         DWORD dwHow)
593 {
594     SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
595     DIDEVICEOBJECTINSTANCEA ddoi;
596     DWORD dwSize = pdidoi->dwSize;
597     
598     TRACE("(this=%p,%p,%ld,0x%08lx)\n", This, pdidoi, dwObj, dwHow);
599
600     if (dwHow == DIPH_BYID) {
601         WARN(" querying by id not supported yet...\n");
602         return DI_OK;
603     }
604
605     memset(pdidoi, 0, dwSize);
606     memset(&ddoi, 0, sizeof(ddoi));
607
608     ddoi.dwSize = dwSize;
609     ddoi.guidType = GUID_Key;
610     ddoi.dwOfs = dwObj;
611     ddoi.dwType = DIDFT_MAKEINSTANCE(dwObj) | DIDFT_BUTTON;
612     GetKeyNameTextA(((dwObj & 0x7f) << 16) | ((dwObj & 0x80) << 17), ddoi.tszName, sizeof(ddoi.tszName));
613
614     /* And return our just filled device object instance structure */
615     memcpy(pdidoi, &ddoi, (dwSize < sizeof(ddoi) ? dwSize : sizeof(ddoi)));
616     
617     _dump_OBJECTINSTANCEA(pdidoi);
618
619     return DI_OK;
620 }
621
622 static HRESULT WINAPI SysKeyboardWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
623                                                      LPDIDEVICEOBJECTINSTANCEW pdidoi,
624                                                      DWORD dwObj,
625                                                      DWORD dwHow)
626 {
627     SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
628     DIDEVICEOBJECTINSTANCEW ddoi;
629     DWORD dwSize = pdidoi->dwSize;
630     
631     TRACE("(this=%p,%p,%ld,0x%08lx)\n", This, pdidoi, dwObj, dwHow);
632
633     if (dwHow == DIPH_BYID) {
634         WARN(" querying by id not supported yet...\n");
635         return DI_OK;
636     }
637
638     memset(pdidoi, 0, dwSize);
639     memset(&ddoi, 0, sizeof(ddoi));
640
641     ddoi.dwSize = dwSize;
642     ddoi.guidType = GUID_Key;
643     ddoi.dwOfs = dwObj;
644     ddoi.dwType = DIDFT_MAKEINSTANCE(dwObj) | DIDFT_BUTTON;
645     GetKeyNameTextW(((dwObj & 0x7f) << 16) | ((dwObj & 0x80) << 17), ddoi.tszName, sizeof(ddoi.tszName));
646
647     /* And return our just filled device object instance structure */
648     memcpy(pdidoi, &ddoi, (dwSize < sizeof(ddoi) ? dwSize : sizeof(ddoi)));
649     
650     _dump_OBJECTINSTANCEW(pdidoi);
651
652     return DI_OK;
653 }
654
655 /******************************************************************************
656   *     GetDeviceInfo : get information about a device's identity
657   */
658 static HRESULT WINAPI SysKeyboardAImpl_GetDeviceInfo(
659         LPDIRECTINPUTDEVICE8A iface,
660         LPDIDEVICEINSTANCEA pdidi)
661 {
662     SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
663     TRACE("(this=%p,%p)\n", This, pdidi);
664
665     if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)) {
666         WARN(" dinput3 not supported yet...\n");
667         return DI_OK;
668     }
669
670     fill_keyboard_dideviceinstanceA(pdidi, This->dinput->dwVersion);
671     
672     return DI_OK;
673 }
674
675 static HRESULT WINAPI SysKeyboardWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW pdidi) 
676 {
677     SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
678     TRACE("(this=%p,%p)\n", This, pdidi);
679
680     if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)) {
681         WARN(" dinput3 not supported yet...\n");
682         return DI_OK;
683     }
684
685     fill_keyboard_dideviceinstanceW(pdidi, This->dinput->dwVersion);
686     
687     return DI_OK;
688 }
689
690 static const IDirectInputDevice8AVtbl SysKeyboardAvt =
691 {
692         IDirectInputDevice2AImpl_QueryInterface,
693         IDirectInputDevice2AImpl_AddRef,
694         SysKeyboardAImpl_Release,
695         SysKeyboardAImpl_GetCapabilities,
696         SysKeyboardAImpl_EnumObjects,
697         SysKeyboardAImpl_GetProperty,
698         SysKeyboardAImpl_SetProperty,
699         SysKeyboardAImpl_Acquire,
700         SysKeyboardAImpl_Unacquire,
701         SysKeyboardAImpl_GetDeviceState,
702         SysKeyboardAImpl_GetDeviceData,
703         IDirectInputDevice2AImpl_SetDataFormat,
704         SysKeyboardAImpl_SetEventNotification,
705         IDirectInputDevice2AImpl_SetCooperativeLevel,
706         SysKeyboardAImpl_GetObjectInfo,
707         SysKeyboardAImpl_GetDeviceInfo,
708         IDirectInputDevice2AImpl_RunControlPanel,
709         IDirectInputDevice2AImpl_Initialize,
710         IDirectInputDevice2AImpl_CreateEffect,
711         IDirectInputDevice2AImpl_EnumEffects,
712         IDirectInputDevice2AImpl_GetEffectInfo,
713         IDirectInputDevice2AImpl_GetForceFeedbackState,
714         IDirectInputDevice2AImpl_SendForceFeedbackCommand,
715         IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
716         IDirectInputDevice2AImpl_Escape,
717         IDirectInputDevice2AImpl_Poll,
718         IDirectInputDevice2AImpl_SendDeviceData,
719         IDirectInputDevice7AImpl_EnumEffectsInFile,
720         IDirectInputDevice7AImpl_WriteEffectToFile,
721         IDirectInputDevice8AImpl_BuildActionMap,
722         IDirectInputDevice8AImpl_SetActionMap,
723         IDirectInputDevice8AImpl_GetImageInfo
724 };
725
726 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
727 # define XCAST(fun)     (typeof(SysKeyboardWvt.fun))
728 #else
729 # define XCAST(fun)     (void*)
730 #endif
731
732 static const IDirectInputDevice8WVtbl SysKeyboardWvt =
733 {
734         IDirectInputDevice2WImpl_QueryInterface,
735         XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
736         XCAST(Release)SysKeyboardAImpl_Release,
737         XCAST(GetCapabilities)SysKeyboardAImpl_GetCapabilities,
738         SysKeyboardWImpl_EnumObjects,
739         XCAST(GetProperty)SysKeyboardAImpl_GetProperty,
740         XCAST(SetProperty)SysKeyboardAImpl_SetProperty,
741         XCAST(Acquire)SysKeyboardAImpl_Acquire,
742         XCAST(Unacquire)SysKeyboardAImpl_Unacquire,
743         XCAST(GetDeviceState)SysKeyboardAImpl_GetDeviceState,
744         XCAST(GetDeviceData)SysKeyboardAImpl_GetDeviceData,
745         XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
746         XCAST(SetEventNotification)SysKeyboardAImpl_SetEventNotification,
747         XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
748         SysKeyboardWImpl_GetObjectInfo,
749         SysKeyboardWImpl_GetDeviceInfo,
750         XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
751         XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
752         XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
753         IDirectInputDevice2WImpl_EnumEffects,
754         IDirectInputDevice2WImpl_GetEffectInfo,
755         XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
756         XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
757         XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
758         XCAST(Escape)IDirectInputDevice2AImpl_Escape,
759         XCAST(Poll)IDirectInputDevice2AImpl_Poll,
760         XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
761         IDirectInputDevice7WImpl_EnumEffectsInFile,
762         IDirectInputDevice7WImpl_WriteEffectToFile,
763         IDirectInputDevice8WImpl_BuildActionMap,
764         IDirectInputDevice8WImpl_SetActionMap,
765         IDirectInputDevice8WImpl_GetImageInfo
766 };
767 #undef XCAST