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