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