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