Make sure ascent/descent values don't become negative after coordinate
[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 ICOM_VTABLE(IDirectInputDevice8A) SysKeyboardAvt;
41 static ICOM_VTABLE(IDirectInputDevice8W) 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)
203 {
204   if ((dwDevType == 0) ||
205       ((dwDevType == DIDEVTYPE_KEYBOARD) && (version < 8)) ||
206       ((dwDevType == DI8DEVTYPE_KEYBOARD) && (version >= 8))) {
207     TRACE("Enumerating the Keyboard device\n");
208  
209     fill_keyboard_dideviceinstanceA(lpddi, version);
210     
211     return TRUE;
212   }
213
214   return FALSE;
215 }
216
217 static BOOL keyboarddev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, int version)
218 {
219   if ((dwDevType == 0) ||
220       ((dwDevType == DIDEVTYPE_KEYBOARD) && (version < 8)) ||
221       ((dwDevType == DI8DEVTYPE_KEYBOARD) && (version >= 8))) {
222     TRACE("Enumerating the Keyboard device\n");
223
224     fill_keyboard_dideviceinstanceW(lpddi, version);
225     
226     return TRUE;
227   }
228
229   return FALSE;
230 }
231
232 static SysKeyboardImpl *alloc_device(REFGUID rguid, LPVOID kvt, IDirectInputImpl *dinput)
233 {
234     SysKeyboardImpl* newDevice;
235     newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SysKeyboardImpl));
236     newDevice->lpVtbl = kvt;
237     newDevice->ref = 1;
238     memcpy(&(newDevice->guid),rguid,sizeof(*rguid));
239     newDevice->dinput = dinput;
240
241     EnterCriticalSection(&keyboard_crit);
242     if (!keyboard_users++)
243         keyboard_hook = SetWindowsHookExW( WH_KEYBOARD_LL, KeyboardCallback, DINPUT_instance, 0 );
244     LeaveCriticalSection(&keyboard_crit);
245
246     return newDevice;
247 }
248
249
250 static HRESULT keyboarddev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
251 {
252   if ((IsEqualGUID(&GUID_SysKeyboard,rguid)) ||          /* Generic Keyboard */
253       (IsEqualGUID(&DInput_Wine_Keyboard_GUID,rguid))) { /* Wine Keyboard */
254     if ((riid == NULL) ||
255         IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
256         IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
257         IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
258         IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
259       *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &SysKeyboardAvt, dinput);
260       TRACE("Creating a Keyboard device (%p)\n", *pdev);
261       return DI_OK;
262     } else
263       return DIERR_NOINTERFACE;
264   }
265   return DIERR_DEVICENOTREG;
266 }
267
268 static HRESULT keyboarddev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
269 {
270   if ((IsEqualGUID(&GUID_SysKeyboard,rguid)) ||          /* Generic Keyboard */
271       (IsEqualGUID(&DInput_Wine_Keyboard_GUID,rguid))) { /* Wine Keyboard */
272     if ((riid == NULL) ||
273         IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
274         IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
275         IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
276         IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
277       *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &SysKeyboardWvt, dinput);
278       TRACE("Creating a Keyboard device (%p)\n", *pdev);
279       return DI_OK;
280     } else
281       return DIERR_NOINTERFACE;
282   }
283   return DIERR_DEVICENOTREG;
284 }
285
286 static dinput_device keyboarddev = {
287   100,
288   keyboarddev_enum_deviceA,
289   keyboarddev_enum_deviceW,
290   keyboarddev_create_deviceA,
291   keyboarddev_create_deviceW
292 };
293
294 DECL_GLOBAL_CONSTRUCTOR(keyboarddev_register) { dinput_register_device(&keyboarddev); }
295
296 static ULONG WINAPI SysKeyboardAImpl_Release(LPDIRECTINPUTDEVICE8A iface)
297 {
298         ICOM_THIS(SysKeyboardImpl,iface);
299
300         This->ref--;
301         if (This->ref)
302                 return This->ref;
303
304         EnterCriticalSection(&keyboard_crit);
305         if (!--keyboard_users) {
306             UnhookWindowsHookEx( keyboard_hook );
307             keyboard_hook = 0;
308         }
309         LeaveCriticalSection(&keyboard_crit);
310
311         /* Free the data queue */
312         if (This->buffer != NULL)
313           HeapFree(GetProcessHeap(),0,This->buffer);
314
315         DeleteCriticalSection(&(This->crit));
316
317         HeapFree(GetProcessHeap(),0,This);
318         return DI_OK;
319 }
320
321 static HRESULT WINAPI SysKeyboardAImpl_SetProperty(
322         LPDIRECTINPUTDEVICE8A iface,REFGUID rguid,LPCDIPROPHEADER ph
323 )
324 {
325         ICOM_THIS(SysKeyboardImpl,iface);
326
327         TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
328         TRACE("(size=%ld,headersize=%ld,obj=%ld,how=%ld\n",
329             ph->dwSize,ph->dwHeaderSize,ph->dwObj,ph->dwHow);
330         if (!HIWORD(rguid)) {
331                 switch ((DWORD)rguid) {
332                 case (DWORD) DIPROP_BUFFERSIZE: {
333                         LPCDIPROPDWORD  pd = (LPCDIPROPDWORD)ph;
334
335                         TRACE("(buffersize=%ld)\n",pd->dwData);
336
337                         if (This->acquired)
338                            return DIERR_INVALIDPARAM;
339
340                         This->buffersize = pd->dwData;
341
342                         break;
343                 }
344                 default:
345                         WARN("Unknown type %ld\n",(DWORD)rguid);
346                         break;
347                 }
348         }
349         return DI_OK;
350 }
351
352 static HRESULT WINAPI SysKeyboardAImpl_GetDeviceState(
353         LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
354 )
355 {
356     TRACE("(%p)->(%ld,%p)\n", iface, len, ptr);
357
358     /* Note: device does not need to be acquired */
359     if (len != 256)
360       return DIERR_INVALIDPARAM;
361
362     MsgWaitForMultipleObjectsEx(0, NULL, 0, 0, 0);
363
364     if (TRACE_ON(dinput)) {
365         int i;
366         for (i = 0; i < 256; i++) {
367             if (DInputKeyState[i] != 0x00) {
368                 TRACE(" - %02X: %02x\n", i, DInputKeyState[i]);
369             }
370         }
371     }
372     
373     memcpy(ptr, DInputKeyState, 256);
374     return DI_OK;
375 }
376
377 static HRESULT WINAPI SysKeyboardAImpl_GetDeviceData(
378         LPDIRECTINPUTDEVICE8A iface,DWORD dodsize,LPDIDEVICEOBJECTDATA dod,
379         LPDWORD entries,DWORD flags
380 )
381 {
382         ICOM_THIS(SysKeyboardImpl,iface);
383         int ret = DI_OK, i = 0;
384
385         TRACE("(this=%p,%ld,%p,%p(%ld)),0x%08lx)\n",
386               This,dodsize,dod,entries,entries?*entries:0,flags);
387
388         if (This->acquired == 0)
389           return DIERR_NOTACQUIRED;
390
391         if (This->buffer == NULL)
392           return DIERR_NOTBUFFERED;
393
394         if (dodsize < sizeof(*dod))
395           return DIERR_INVALIDPARAM;
396
397         MsgWaitForMultipleObjectsEx(0, NULL, 0, 0, 0);
398
399         EnterCriticalSection(&(This->crit));
400
401         /* Copy item at a time for the case dodsize > sizeof(buffer[n]) */
402         while ((i < *entries || *entries == INFINITE) && i < This->count)
403           {
404             if (dod != NULL)
405               {
406                 int n = (This->start + i) % This->buffersize;
407                 LPDIDEVICEOBJECTDATA pd
408                    = (LPDIDEVICEOBJECTDATA)((BYTE *)dod + dodsize * i);
409                 pd->dwOfs       = This->buffer[n].dwOfs;
410                 pd->dwData      = This->buffer[n].dwData;
411                 pd->dwTimeStamp = This->buffer[n].dwTimeStamp;
412                 pd->dwSequence  = This->buffer[n].dwSequence;
413               }
414             i++;
415           }
416
417         *entries = i;
418
419         if (This->overflow)
420           ret = DI_BUFFEROVERFLOW;
421
422         if (!(flags & DIGDD_PEEK))
423           {
424             /* Empty buffer */
425             This->count -= i;
426             This->start = (This->start + i) % This->buffersize;
427             This->overflow = FALSE;
428           }
429
430         LeaveCriticalSection(&(This->crit));
431
432         TRACE("Returning %ld events queued\n", *entries);
433
434         return ret;
435 }
436
437 static HRESULT WINAPI SysKeyboardAImpl_EnumObjects(
438         LPDIRECTINPUTDEVICE8A iface,
439         LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback,
440         LPVOID lpvRef,
441         DWORD dwFlags)
442 {
443     ICOM_THIS(SysKeyboardImpl,iface);
444     DIDEVICEOBJECTINSTANCEA ddoi;
445     int i;
446     
447     TRACE("(this=%p,%p,%p,%08lx)\n", This, lpCallback, lpvRef, dwFlags);
448     if (TRACE_ON(dinput)) {
449         TRACE("  - flags = ");
450         _dump_EnumObjects_flags(dwFlags);
451         TRACE("\n");
452     }
453
454     /* Only the fields till dwFFMaxForce are relevant */
455     memset(&ddoi, 0, sizeof(ddoi));
456     ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA, dwFFMaxForce);
457
458     for (i = 0; i < 256; i++) {
459         /* Report 255 keys :-) */
460         ddoi.guidType = GUID_Key;
461         ddoi.dwOfs = i;
462         ddoi.dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_BUTTON;
463         GetKeyNameTextA(((i & 0x7f) << 16) | ((i & 0x80) << 17), ddoi.tszName, sizeof(ddoi.tszName));
464         _dump_OBJECTINSTANCEA(&ddoi);
465         if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
466     }
467     
468     return DI_OK;
469 }
470
471 static HRESULT WINAPI SysKeyboardWImpl_EnumObjects(LPDIRECTINPUTDEVICE8W iface,
472                                                    LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback,
473                                                    LPVOID lpvRef,
474                                                    DWORD dwFlags)
475 {
476   ICOM_THIS(SysKeyboardImpl,iface);
477
478   device_enumobjects_AtoWcb_data data;
479
480   data.lpCallBack = lpCallback;
481   data.lpvRef = lpvRef;
482
483   return SysKeyboardAImpl_EnumObjects((LPDIRECTINPUTDEVICE8A) This, (LPDIENUMDEVICEOBJECTSCALLBACKA) DIEnumDevicesCallbackAtoW, (LPVOID) &data, dwFlags);
484 }
485
486 static HRESULT WINAPI SysKeyboardAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface);
487
488 static HRESULT WINAPI SysKeyboardAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
489 {
490         ICOM_THIS(SysKeyboardImpl,iface);
491
492         TRACE("(this=%p)\n",This);
493
494         if (This->acquired)
495           return S_FALSE;
496
497         This->acquired = 1;
498
499         if (current != NULL)
500           {
501             FIXME("Not more than one keyboard can be acquired at the same time.\n");
502             SysKeyboardAImpl_Unacquire(iface);
503           }
504
505         current = This;
506
507         if (This->buffersize > 0)
508           {
509             This->buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
510                                      This->buffersize * sizeof(*(This->buffer)));
511             This->start = 0;
512             This->count = 0;
513             This->overflow = FALSE;
514             InitializeCriticalSection(&(This->crit));
515           }
516         else
517           This->buffer = NULL;
518
519         return DI_OK;
520 }
521
522 static HRESULT WINAPI SysKeyboardAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
523 {
524         ICOM_THIS(SysKeyboardImpl,iface);
525         TRACE("(this=%p)\n",This);
526
527         if (This->acquired == 0)
528           return DI_NOEFFECT;
529
530         if (current == This)
531           current = NULL;
532         else
533           ERR("this != current\n");
534
535         This->acquired = 0;
536
537         if (This->buffersize >= 0)
538           {
539             HeapFree(GetProcessHeap(), 0, This->buffer);
540             This->buffer = NULL;
541             DeleteCriticalSection(&(This->crit));
542           }
543
544         return DI_OK;
545 }
546
547 static HRESULT WINAPI SysKeyboardAImpl_SetEventNotification(LPDIRECTINPUTDEVICE8A iface,
548                                                             HANDLE hnd) {
549   ICOM_THIS(SysKeyboardImpl,iface);
550
551   TRACE("(this=%p,0x%08lx)\n",This,(DWORD)hnd);
552
553   This->hEvent = hnd;
554   return DI_OK;
555 }
556
557 /******************************************************************************
558   *     GetCapabilities : get the device capablitites
559   */
560 static HRESULT WINAPI SysKeyboardAImpl_GetCapabilities(
561         LPDIRECTINPUTDEVICE8A iface,
562         LPDIDEVCAPS lpDIDevCaps)
563 {
564   ICOM_THIS(SysKeyboardImpl,iface);
565
566   TRACE("(this=%p,%p)\n",This,lpDIDevCaps);
567
568   if (lpDIDevCaps->dwSize == sizeof(DIDEVCAPS)) {
569     lpDIDevCaps->dwFlags = DIDC_ATTACHED;
570     if (This->dinput->version >= 8)
571         lpDIDevCaps->dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
572     else
573         lpDIDevCaps->dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
574     lpDIDevCaps->dwAxes = 0;
575     lpDIDevCaps->dwButtons = 256;
576     lpDIDevCaps->dwPOVs = 0;
577     lpDIDevCaps->dwFFSamplePeriod = 0;
578     lpDIDevCaps->dwFFMinTimeResolution = 0;
579     lpDIDevCaps->dwFirmwareRevision = 100;
580     lpDIDevCaps->dwHardwareRevision = 100;
581     lpDIDevCaps->dwFFDriverVersion = 0;
582   } else {
583     /* DirectX 3.0 */
584     FIXME("DirectX 3.0 not supported....\n");
585   }
586
587   return DI_OK;
588 }
589
590 /******************************************************************************
591   *     GetObjectInfo : get information about a device object such as a button
592   *                     or axis
593   */
594 static HRESULT WINAPI
595 SysKeyboardAImpl_GetObjectInfo(
596         LPDIRECTINPUTDEVICE8A iface,
597         LPDIDEVICEOBJECTINSTANCEA pdidoi,
598         DWORD dwObj,
599         DWORD dwHow)
600 {
601     ICOM_THIS(SysKeyboardImpl,iface);
602     DIDEVICEOBJECTINSTANCEA ddoi;
603     DWORD dwSize = pdidoi->dwSize;
604     
605     TRACE("(this=%p,%p,%ld,0x%08lx)\n", This, pdidoi, dwObj, dwHow);
606
607     if (dwHow == DIPH_BYID) {
608         WARN(" querying by id not supported yet...\n");
609         return DI_OK;
610     }
611
612     memset(pdidoi, 0, dwSize);
613     memset(&ddoi, 0, sizeof(ddoi));
614
615     ddoi.dwSize = dwSize;
616     ddoi.guidType = GUID_Key;
617     ddoi.dwOfs = dwObj;
618     ddoi.dwType = DIDFT_MAKEINSTANCE(dwObj) | DIDFT_BUTTON;
619     GetKeyNameTextA(((dwObj & 0x7f) << 16) | ((dwObj & 0x80) << 17), ddoi.tszName, sizeof(ddoi.tszName));
620
621     /* And return our just filled device object instance structure */
622     memcpy(pdidoi, &ddoi, (dwSize < sizeof(ddoi) ? dwSize : sizeof(ddoi)));
623     
624     _dump_OBJECTINSTANCEA(pdidoi);
625
626     return DI_OK;
627 }
628
629 static HRESULT WINAPI SysKeyboardWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
630                                                      LPDIDEVICEOBJECTINSTANCEW pdidoi,
631                                                      DWORD dwObj,
632                                                      DWORD dwHow)
633 {
634     ICOM_THIS(SysKeyboardImpl,iface);
635     DIDEVICEOBJECTINSTANCEW ddoi;
636     DWORD dwSize = pdidoi->dwSize;
637     
638     TRACE("(this=%p,%p,%ld,0x%08lx)\n", This, pdidoi, dwObj, dwHow);
639
640     if (dwHow == DIPH_BYID) {
641         WARN(" querying by id not supported yet...\n");
642         return DI_OK;
643     }
644
645     memset(pdidoi, 0, dwSize);
646     memset(&ddoi, 0, sizeof(ddoi));
647
648     ddoi.dwSize = dwSize;
649     ddoi.guidType = GUID_Key;
650     ddoi.dwOfs = dwObj;
651     ddoi.dwType = DIDFT_MAKEINSTANCE(dwObj) | DIDFT_BUTTON;
652     GetKeyNameTextW(((dwObj & 0x7f) << 16) | ((dwObj & 0x80) << 17), ddoi.tszName, sizeof(ddoi.tszName));
653
654     /* And return our just filled device object instance structure */
655     memcpy(pdidoi, &ddoi, (dwSize < sizeof(ddoi) ? dwSize : sizeof(ddoi)));
656     
657     _dump_OBJECTINSTANCEW(pdidoi);
658
659     return DI_OK;
660 }
661
662 /******************************************************************************
663   *     GetDeviceInfo : get information about a device's identity
664   */
665 static HRESULT WINAPI SysKeyboardAImpl_GetDeviceInfo(
666         LPDIRECTINPUTDEVICE8A iface,
667         LPDIDEVICEINSTANCEA pdidi)
668 {
669     ICOM_THIS(SysKeyboardImpl,iface);
670     TRACE("(this=%p,%p)\n", This, pdidi);
671
672     if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)) {
673         WARN(" dinput3 not supporte yet...\n");
674         return DI_OK;
675     }
676
677     fill_keyboard_dideviceinstanceA(pdidi, This->dinput->version);
678     
679     return DI_OK;
680 }
681
682 static HRESULT WINAPI SysKeyboardWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW pdidi) 
683 {
684     ICOM_THIS(SysKeyboardImpl,iface);
685     TRACE("(this=%p,%p)\n", This, pdidi);
686
687     if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)) {
688         WARN(" dinput3 not supporte yet...\n");
689         return DI_OK;
690     }
691
692     fill_keyboard_dideviceinstanceW(pdidi, This->dinput->version);
693     
694     return DI_OK;
695 }
696
697 static ICOM_VTABLE(IDirectInputDevice8A) SysKeyboardAvt =
698 {
699         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
700         IDirectInputDevice2AImpl_QueryInterface,
701         IDirectInputDevice2AImpl_AddRef,
702         SysKeyboardAImpl_Release,
703         SysKeyboardAImpl_GetCapabilities,
704         SysKeyboardAImpl_EnumObjects,
705         IDirectInputDevice2AImpl_GetProperty,
706         SysKeyboardAImpl_SetProperty,
707         SysKeyboardAImpl_Acquire,
708         SysKeyboardAImpl_Unacquire,
709         SysKeyboardAImpl_GetDeviceState,
710         SysKeyboardAImpl_GetDeviceData,
711         IDirectInputDevice2AImpl_SetDataFormat,
712         SysKeyboardAImpl_SetEventNotification,
713         IDirectInputDevice2AImpl_SetCooperativeLevel,
714         SysKeyboardAImpl_GetObjectInfo,
715         SysKeyboardAImpl_GetDeviceInfo,
716         IDirectInputDevice2AImpl_RunControlPanel,
717         IDirectInputDevice2AImpl_Initialize,
718         IDirectInputDevice2AImpl_CreateEffect,
719         IDirectInputDevice2AImpl_EnumEffects,
720         IDirectInputDevice2AImpl_GetEffectInfo,
721         IDirectInputDevice2AImpl_GetForceFeedbackState,
722         IDirectInputDevice2AImpl_SendForceFeedbackCommand,
723         IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
724         IDirectInputDevice2AImpl_Escape,
725         IDirectInputDevice2AImpl_Poll,
726         IDirectInputDevice2AImpl_SendDeviceData,
727         IDirectInputDevice7AImpl_EnumEffectsInFile,
728         IDirectInputDevice7AImpl_WriteEffectToFile,
729         IDirectInputDevice8AImpl_BuildActionMap,
730         IDirectInputDevice8AImpl_SetActionMap,
731         IDirectInputDevice8AImpl_GetImageInfo
732 };
733
734 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
735 # define XCAST(fun)     (typeof(SysKeyboardWvt.fun))
736 #else
737 # define XCAST(fun)     (void*)
738 #endif
739
740 static ICOM_VTABLE(IDirectInputDevice8W) SysKeyboardWvt =
741 {
742         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
743         IDirectInputDevice2WImpl_QueryInterface,
744         XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
745         XCAST(Release)SysKeyboardAImpl_Release,
746         XCAST(GetCapabilities)SysKeyboardAImpl_GetCapabilities,
747         SysKeyboardWImpl_EnumObjects,
748         XCAST(GetProperty)IDirectInputDevice2AImpl_GetProperty,
749         XCAST(SetProperty)SysKeyboardAImpl_SetProperty,
750         XCAST(Acquire)SysKeyboardAImpl_Acquire,
751         XCAST(Unacquire)SysKeyboardAImpl_Unacquire,
752         XCAST(GetDeviceState)SysKeyboardAImpl_GetDeviceState,
753         XCAST(GetDeviceData)SysKeyboardAImpl_GetDeviceData,
754         XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
755         XCAST(SetEventNotification)SysKeyboardAImpl_SetEventNotification,
756         XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
757         SysKeyboardWImpl_GetObjectInfo,
758         SysKeyboardWImpl_GetDeviceInfo,
759         XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
760         XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
761         XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
762         IDirectInputDevice2WImpl_EnumEffects,
763         IDirectInputDevice2WImpl_GetEffectInfo,
764         XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
765         XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
766         XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
767         XCAST(Escape)IDirectInputDevice2AImpl_Escape,
768         XCAST(Poll)IDirectInputDevice2AImpl_Poll,
769         XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
770         IDirectInputDevice7WImpl_EnumEffectsInFile,
771         IDirectInputDevice7WImpl_WriteEffectToFile,
772         IDirectInputDevice8WImpl_BuildActionMap,
773         IDirectInputDevice8WImpl_SetActionMap,
774         IDirectInputDevice8WImpl_GetImageInfo
775 };
776 #undef XCAST