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