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