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