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