keyboard.c 25.6 KB
Newer Older
Lionel Ulmer's avatar
Lionel Ulmer committed
1 2 3 4
/*		DirectInput Keyboard device
 *
 * Copyright 1998 Marcus Meissner
 * Copyright 1998,1999 Lionel Ulmer
5
 * Copyright 2000-2001 TransGaming Technologies Inc.
6
 * Copyright 2005 Raphael Junqueira
Lionel Ulmer's avatar
Lionel Ulmer committed
7
 *
8 9 10 11 12 13 14 15 16 17 18 19 20
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Lionel Ulmer's avatar
Lionel Ulmer committed
21 22 23
 */

#include "config.h"
24 25
#include "wine/port.h"

26
#include <stdarg.h>
Lionel Ulmer's avatar
Lionel Ulmer committed
27
#include <string.h>
28
#include "windef.h"
29 30
#include "winbase.h"
#include "winuser.h"
Lionel Ulmer's avatar
Lionel Ulmer committed
31 32 33 34 35
#include "winerror.h"
#include "dinput.h"

#include "dinput_private.h"
#include "device_private.h"
36
#include "wine/debug.h"
37
#include "wine/unicode.h"
Lionel Ulmer's avatar
Lionel Ulmer committed
38

39
WINE_DEFAULT_DEBUG_CHANNEL(dinput);
Lionel Ulmer's avatar
Lionel Ulmer committed
40

41 42
#define WINE_DINPUT_KEYBOARD_MAX_KEYS 256

43 44
static const IDirectInputDevice8AVtbl SysKeyboardAvt;
static const IDirectInputDevice8WVtbl SysKeyboardWvt;
45

46 47
typedef struct SysKeyboardImpl SysKeyboardImpl;
struct SysKeyboardImpl
Lionel Ulmer's avatar
Lionel Ulmer committed
48
{
49
        const void                     *lpVtbl;
50
        LONG                            ref;
Lionel Ulmer's avatar
Lionel Ulmer committed
51 52
        GUID                            guid;

53
	IDirectInputImpl*               dinput;
54 55

	HANDLE	hEvent;
Lionel Ulmer's avatar
Lionel Ulmer committed
56 57
        /* SysKeyboardAImpl */
	int                             acquired;
58 59 60 61 62 63 64 65 66 67 68
        int                             buffersize;  /* set in 'SetProperty'         */
        LPDIDEVICEOBJECTDATA            buffer;      /* buffer for 'GetDeviceData'.
                                                        Alloc at 'Acquire', Free at
                                                        'Unacquire'                  */
        int                             count;       /* number of objects in use in
                                                        'buffer'                     */
        int                             start;       /* 'buffer' rotates. This is the
                                                        first in use (if count > 0)  */
        BOOL                            overflow;    /* return DI_BUFFEROVERFLOW in
                                                        'GetDeviceData'              */
        CRITICAL_SECTION                crit;
Lionel Ulmer's avatar
Lionel Ulmer committed
69 70
};

71 72 73 74 75 76
static SysKeyboardImpl* current_lock = NULL; 
/* Today's acquired device
 * FIXME: currently this can be only one.
 * Maybe this should be a linked list or st.
 * I don't know what the rules are for multiple acquired keyboards,
 * 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 82 83 84 85 86 87 88 89
static CRITICAL_SECTION keyboard_crit;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
    0, 0, &keyboard_crit,
    { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
      0, 0, { 0, (DWORD)(__FILE__ ": keyboard_crit") }
};
static CRITICAL_SECTION keyboard_crit = { &critsect_debug, -1, 0, 0, 0, 0 };

90
static LONG keyboard_users = 0;
91
static HHOOK keyboard_hook = NULL;
92 93 94

LRESULT CALLBACK KeyboardCallback( int code, WPARAM wparam, LPARAM lparam )
{
95 96 97 98 99 100
  BYTE dik_code;
  BOOL down;
  DWORD timestamp;
  KBDLLHOOKSTRUCT *hook = (KBDLLHOOKSTRUCT *)lparam;
  BYTE new_diks;

101 102
  TRACE("(%d,%d,%ld)\n", code, wparam, lparam);

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  /** returns now if not HC_ACTION */
  if (code != HC_ACTION) return CallNextHookEx(keyboard_hook, code, wparam, lparam);
  
  {
    dik_code = hook->scanCode;
    if (hook->flags & LLKHF_EXTENDED) dik_code |= 0x80;
    down = !(hook->flags & LLKHF_UP);
    timestamp = hook->time;
  }

  /** returns now if key event already known */
  new_diks = (down ? 0x80 : 0);
  /*if (new_diks != DInputKeyState[dik_code]) return CallNextHookEx(keyboard_hook, code, wparam, lparam); TO BE FIXED */

  DInputKeyState[dik_code] = new_diks;
  TRACE(" setting %02X to %02X\n", dik_code, DInputKeyState[dik_code]);
      
  if (current_lock != NULL) {
    if (current_lock->hEvent) SetEvent(current_lock->hEvent);
    
    if (current_lock->buffer != NULL) {
      int n;
      
      EnterCriticalSection(&(current_lock->crit));
127
      
128
      n = (current_lock->start + current_lock->count) % current_lock->buffersize;
129
      
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
      current_lock->buffer[n].dwOfs = dik_code;
      current_lock->buffer[n].dwData = down ? 0x80 : 0;
      current_lock->buffer[n].dwTimeStamp = timestamp;
      current_lock->buffer[n].dwSequence = current_lock->dinput->evsequence++;
      
      TRACE("Adding event at offset %d : %ld - %ld - %ld - %ld\n", n,
	    current_lock->buffer[n].dwOfs, current_lock->buffer[n].dwData, current_lock->buffer[n].dwTimeStamp, current_lock->buffer[n].dwSequence);
      
      if (current_lock->count == current_lock->buffersize) {
	current_lock->start = ++current_lock->start % current_lock->buffersize;
	current_lock->overflow = TRUE;
      } else
	current_lock->count++;
      
      LeaveCriticalSection(&(current_lock->crit));
145
    }
146
  }
147 148
  return CallNextHookEx(keyboard_hook, code, wparam, lparam);
}
149

Lionel Ulmer's avatar
Lionel Ulmer committed
150 151 152 153 154 155 156
static GUID DInput_Wine_Keyboard_GUID = { /* 0ab8648a-7735-11d2-8c73-71df54a96441 */
  0x0ab8648a,
  0x7735,
  0x11d2,
  {0x8c, 0x73, 0x71, 0xdf, 0x54, 0xa9, 0x64, 0x41}
};

157
static void fill_keyboard_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version) {
158 159 160 161 162 163 164 165 166 167 168 169 170
    DWORD dwSize;
    DIDEVICEINSTANCEA ddi;
    
    dwSize = lpddi->dwSize;

    TRACE("%ld %p\n", dwSize, lpddi);
    
    memset(lpddi, 0, dwSize);
    memset(&ddi, 0, sizeof(ddi));

    ddi.dwSize = dwSize;
    ddi.guidInstance = GUID_SysKeyboard;/* DInput's GUID */
    ddi.guidProduct = DInput_Wine_Keyboard_GUID; /* Vendor's GUID */
171
    if (version >= 0x0800)
172 173 174
        ddi.dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
    else
        ddi.dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
175 176 177 178 179 180
    strcpy(ddi.tszInstanceName, "Keyboard");
    strcpy(ddi.tszProductName, "Wine Keyboard");

    memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
}

181
static void fill_keyboard_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version) {
182 183 184 185 186 187 188 189 190 191 192 193 194
    DWORD dwSize;
    DIDEVICEINSTANCEW ddi;
    
    dwSize = lpddi->dwSize;

    TRACE("%ld %p\n", dwSize, lpddi);
    
    memset(lpddi, 0, dwSize);
    memset(&ddi, 0, sizeof(ddi));
 
    ddi.dwSize = dwSize;
    ddi.guidInstance = GUID_SysKeyboard;/* DInput's GUID */
    ddi.guidProduct = DInput_Wine_Keyboard_GUID; /* Vendor's GUID */
195
    if (version >= 0x0800)
196 197 198 199 200 201 202 203 204
        ddi.dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
    else
        ddi.dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
    MultiByteToWideChar(CP_ACP, 0, "Keyboard", -1, ddi.tszInstanceName, MAX_PATH);
    MultiByteToWideChar(CP_ACP, 0, "Wine Keyboard", -1, ddi.tszProductName, MAX_PATH);

    memcpy(lpddi, &ddi, (dwSize < sizeof(ddi) ? dwSize : sizeof(ddi)));
}
 
205
static BOOL keyboarddev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
206
{
207 208 209
  if (id != 0)
    return FALSE;

210
  if ((dwDevType == 0) ||
211 212
      ((dwDevType == DIDEVTYPE_KEYBOARD) && (version < 0x0800)) ||
      (((dwDevType == DI8DEVCLASS_KEYBOARD) || (dwDevType == DI8DEVTYPE_KEYBOARD)) && (version >= 0x0800))) {
213 214 215 216 217 218 219 220 221 222
    TRACE("Enumerating the Keyboard device\n");
 
    fill_keyboard_dideviceinstanceA(lpddi, version);
    
    return TRUE;
  }

  return FALSE;
}

223
static BOOL keyboarddev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
Lionel Ulmer's avatar
Lionel Ulmer committed
224
{
225 226 227
  if (id != 0)
    return FALSE;

228
  if ((dwDevType == 0) ||
229 230
      ((dwDevType == DIDEVTYPE_KEYBOARD) && (version < 0x0800)) ||
      (((dwDevType == DI8DEVCLASS_KEYBOARD) || (dwDevType == DI8DEVTYPE_KEYBOARD)) && (version >= 0x0800))) {
Lionel Ulmer's avatar
Lionel Ulmer committed
231
    TRACE("Enumerating the Keyboard device\n");
232

233
    fill_keyboard_dideviceinstanceW(lpddi, version);
234
    
Lionel Ulmer's avatar
Lionel Ulmer committed
235 236 237 238 239 240
    return TRUE;
  }

  return FALSE;
}

241
static SysKeyboardImpl *alloc_device(REFGUID rguid, const void *kvt, IDirectInputImpl *dinput)
Lionel Ulmer's avatar
Lionel Ulmer committed
242
{
243
    SysKeyboardImpl* newDevice;
244 245
    DWORD kbd_users;

246
    newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SysKeyboardImpl));
247
    newDevice->lpVtbl = kvt;
Lionel Ulmer's avatar
Lionel Ulmer committed
248 249 250
    newDevice->ref = 1;
    memcpy(&(newDevice->guid),rguid,sizeof(*rguid));
    newDevice->dinput = dinput;
251
    InitializeCriticalSection(&(newDevice->crit));
Lionel Ulmer's avatar
Lionel Ulmer committed
252

253
    EnterCriticalSection(&keyboard_crit);
254 255 256 257
    kbd_users = InterlockedIncrement(&keyboard_users);
    if (1 == kbd_users) {
      keyboard_hook = SetWindowsHookExW( WH_KEYBOARD_LL, KeyboardCallback, DINPUT_instance, 0 );
    }
258 259
    LeaveCriticalSection(&keyboard_crit);

Lionel Ulmer's avatar
Lionel Ulmer committed
260 261 262 263
    return newDevice;
}


264
static HRESULT keyboarddev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
Lionel Ulmer's avatar
Lionel Ulmer committed
265 266 267
{
  if ((IsEqualGUID(&GUID_SysKeyboard,rguid)) ||          /* Generic Keyboard */
      (IsEqualGUID(&DInput_Wine_Keyboard_GUID,rguid))) { /* Wine Keyboard */
268 269 270 271 272
    if ((riid == NULL) ||
	IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
	IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
	IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
	IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
273
      *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &SysKeyboardAvt, dinput);
Lionel Ulmer's avatar
Lionel Ulmer committed
274 275 276 277 278
      TRACE("Creating a Keyboard device (%p)\n", *pdev);
      return DI_OK;
    } else
      return DIERR_NOINTERFACE;
  }
279 280
  return DIERR_DEVICENOTREG;
}
Lionel Ulmer's avatar
Lionel Ulmer committed
281

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
static HRESULT keyboarddev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
{
  if ((IsEqualGUID(&GUID_SysKeyboard,rguid)) ||          /* Generic Keyboard */
      (IsEqualGUID(&DInput_Wine_Keyboard_GUID,rguid))) { /* Wine Keyboard */
    if ((riid == NULL) ||
	IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
	IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
	IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
	IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
      *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &SysKeyboardWvt, dinput);
      TRACE("Creating a Keyboard device (%p)\n", *pdev);
      return DI_OK;
    } else
      return DIERR_NOINTERFACE;
  }
Lionel Ulmer's avatar
Lionel Ulmer committed
297 298 299
  return DIERR_DEVICENOTREG;
}

300
const struct dinput_device keyboard_device = {
301
  "Wine keyboard driver",
302 303 304 305
  keyboarddev_enum_deviceA,
  keyboarddev_enum_deviceW,
  keyboarddev_create_deviceA,
  keyboarddev_create_deviceW
Lionel Ulmer's avatar
Lionel Ulmer committed
306 307
};

308 309
static ULONG WINAPI SysKeyboardAImpl_Release(LPDIRECTINPUTDEVICE8A iface)
{
310
	SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
311
	ULONG ref;
312
	DWORD kbd_users;
313

314 315 316
	ref = InterlockedDecrement(&(This->ref));
	if (ref)
		return ref;
317

318 319 320
	EnterCriticalSection(&keyboard_crit);	
	kbd_users = InterlockedDecrement(&keyboard_users);
	if (0 == kbd_users) {
321 322 323 324 325 326
	    UnhookWindowsHookEx( keyboard_hook );
	    keyboard_hook = 0;
	}
	LeaveCriticalSection(&keyboard_crit);

	/* Free the data queue */
327
	HeapFree(GetProcessHeap(),0,This->buffer);
328 329 330 331

	DeleteCriticalSection(&(This->crit));

	HeapFree(GetProcessHeap(),0,This);
332
	return DI_OK;
333 334
}

Lionel Ulmer's avatar
Lionel Ulmer committed
335
static HRESULT WINAPI SysKeyboardAImpl_SetProperty(
336
	LPDIRECTINPUTDEVICE8A iface,REFGUID rguid,LPCDIPROPHEADER ph
Lionel Ulmer's avatar
Lionel Ulmer committed
337 338
)
{
339
	SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
Lionel Ulmer's avatar
Lionel Ulmer committed
340 341 342 343 344 345 346 347 348 349

	TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
	TRACE("(size=%ld,headersize=%ld,obj=%ld,how=%ld\n",
            ph->dwSize,ph->dwHeaderSize,ph->dwObj,ph->dwHow);
	if (!HIWORD(rguid)) {
		switch ((DWORD)rguid) {
		case (DWORD) DIPROP_BUFFERSIZE: {
			LPCDIPROPDWORD	pd = (LPCDIPROPDWORD)ph;

			TRACE("(buffersize=%ld)\n",pd->dwData);
350 351 352 353 354 355

                        if (This->acquired)
                           return DIERR_INVALIDPARAM;

                        This->buffersize = pd->dwData;

Lionel Ulmer's avatar
Lionel Ulmer committed
356 357 358 359 360 361 362
			break;
		}
		default:
			WARN("Unknown type %ld\n",(DWORD)rguid);
			break;
		}
	}
363
	return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
364 365
}

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
static HRESULT WINAPI SysKeyboardAImpl_GetProperty(
	LPDIRECTINPUTDEVICE8A iface,REFGUID rguid,LPDIPROPHEADER ph
)
{
	SysKeyboardImpl *This = (SysKeyboardImpl *)iface;

	TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
	TRACE("(size=%ld,headersize=%ld,obj=%ld,how=%ld\n",
            ph->dwSize,ph->dwHeaderSize,ph->dwObj,ph->dwHow);
	if (!HIWORD(rguid)) {
		switch ((DWORD)rguid) {
		case (DWORD) DIPROP_BUFFERSIZE: {
			LPDIPROPDWORD	pd = (LPDIPROPDWORD)ph;

			TRACE("(buffersize=%ld)\n",pd->dwData);

                        if (This->acquired)
                           return DIERR_INVALIDPARAM;

                        pd->dwData = This->buffersize;

			break;
		}
		default:
			WARN("Unknown type %ld\n",(DWORD)rguid);
			break;
		}
	}
	return DI_OK;
}

Lionel Ulmer's avatar
Lionel Ulmer committed
397
static HRESULT WINAPI SysKeyboardAImpl_GetDeviceState(
398
	LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
Lionel Ulmer's avatar
Lionel Ulmer committed
399 400
)
{
401 402
    SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
    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 407
      return DIERR_INVALIDPARAM;

408
    MsgWaitForMultipleObjectsEx(0, NULL, 0, QS_ALLINPUT, 0);
409

410 411
    EnterCriticalSection(&(This->crit));

412 413
    if (TRACE_ON(dinput)) {
	int i;
414
	for (i = 0; i < WINE_DINPUT_KEYBOARD_MAX_KEYS; i++) {
415 416 417 418 419 420
	    if (DInputKeyState[i] != 0x00) {
		TRACE(" - %02X: %02x\n", i, DInputKeyState[i]);
	    }
	}
    }
    
421 422 423
    memcpy(ptr, DInputKeyState, WINE_DINPUT_KEYBOARD_MAX_KEYS);
    LeaveCriticalSection(&(This->crit));

424
    return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
425 426 427
}

static HRESULT WINAPI SysKeyboardAImpl_GetDeviceData(
428
	LPDIRECTINPUTDEVICE8A iface,DWORD dodsize,LPDIDEVICEOBJECTDATA dod,
Lionel Ulmer's avatar
Lionel Ulmer committed
429 430 431
	LPDWORD entries,DWORD flags
)
{
432
	SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
433
	int ret = DI_OK, i = 0;
Lionel Ulmer's avatar
Lionel Ulmer committed
434 435 436 437

	TRACE("(this=%p,%ld,%p,%p(%ld)),0x%08lx)\n",
	      This,dodsize,dod,entries,entries?*entries:0,flags);

438 439 440
	if (This->acquired == 0)
	  return DIERR_NOTACQUIRED;

441 442 443
        if (This->buffer == NULL)
          return DIERR_NOTBUFFERED;

444
        if (dodsize < sizeof(DIDEVICEOBJECTDATA_DX3))
445 446
          return DIERR_INVALIDPARAM;

447
        MsgWaitForMultipleObjectsEx(0, NULL, 0, QS_ALLINPUT, 0);
448

449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
        EnterCriticalSection(&(This->crit));

        /* Copy item at a time for the case dodsize > sizeof(buffer[n]) */
        while ((i < *entries || *entries == INFINITE) && i < This->count)
          {
            if (dod != NULL)
              {
                int n = (This->start + i) % This->buffersize;
                LPDIDEVICEOBJECTDATA pd
                   = (LPDIDEVICEOBJECTDATA)((BYTE *)dod + dodsize * i);
                pd->dwOfs       = This->buffer[n].dwOfs;
                pd->dwData      = This->buffer[n].dwData;
                pd->dwTimeStamp = This->buffer[n].dwTimeStamp;
                pd->dwSequence  = This->buffer[n].dwSequence;
              }
            i++;
          }

        *entries = i;

        if (This->overflow)
          ret = DI_BUFFEROVERFLOW;

        if (!(flags & DIGDD_PEEK))
          {
            /* Empty buffer */
            This->count -= i;
            This->start = (This->start + i) % This->buffersize;
            This->overflow = FALSE;
          }

        LeaveCriticalSection(&(This->crit));

482 483
	TRACE("Returning %ld events queued\n", *entries);

484
        return ret;
Lionel Ulmer's avatar
Lionel Ulmer committed
485 486
}

487 488 489 490 491 492
static HRESULT WINAPI SysKeyboardAImpl_EnumObjects(
	LPDIRECTINPUTDEVICE8A iface,
	LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback,
	LPVOID lpvRef,
	DWORD dwFlags)
{
493
    SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
494 495 496 497 498
    DIDEVICEOBJECTINSTANCEA ddoi;
    int i;
    
    TRACE("(this=%p,%p,%p,%08lx)\n", This, lpCallback, lpvRef, dwFlags);
    if (TRACE_ON(dinput)) {
499
        TRACE("  - flags = ");
500
	_dump_EnumObjects_flags(dwFlags);
501
	TRACE("\n");
502 503 504 505 506 507
    }

    /* Only the fields till dwFFMaxForce are relevant */
    memset(&ddoi, 0, sizeof(ddoi));
    ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA, dwFFMaxForce);

508
    for (i = 0; i < WINE_DINPUT_KEYBOARD_MAX_KEYS; i++) {
509 510 511 512
        /* Report 255 keys :-) */
        ddoi.guidType = GUID_Key;
	ddoi.dwOfs = i;
	ddoi.dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_BUTTON;
513
	GetKeyNameTextA(((i & 0x7f) << 16) | ((i & 0x80) << 17), ddoi.tszName, sizeof(ddoi.tszName));
514 515 516 517 518 519 520
	_dump_OBJECTINSTANCEA(&ddoi);
	if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
    }
    
    return DI_OK;
}

521 522 523 524 525
static HRESULT WINAPI SysKeyboardWImpl_EnumObjects(LPDIRECTINPUTDEVICE8W iface,
						   LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback,
						   LPVOID lpvRef,
						   DWORD dwFlags)
{
526
  SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
527 528 529 530 531 532 533 534 535

  device_enumobjects_AtoWcb_data data;

  data.lpCallBack = lpCallback;
  data.lpvRef = lpvRef;

  return SysKeyboardAImpl_EnumObjects((LPDIRECTINPUTDEVICE8A) This, (LPDIENUMDEVICEOBJECTSCALLBACKA) DIEnumDevicesCallbackAtoW, (LPVOID) &data, dwFlags);
}

536
static HRESULT WINAPI SysKeyboardAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface);
537

538
static HRESULT WINAPI SysKeyboardAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
Lionel Ulmer's avatar
Lionel Ulmer committed
539
{
540
	SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
541

Lionel Ulmer's avatar
Lionel Ulmer committed
542
	TRACE("(this=%p)\n",This);
543

544 545 546 547 548
        if (This->acquired)
          return S_FALSE;

        This->acquired = 1;

549 550 551 552 553 554 555 556 557 558 559 560 561 562
        if (current_lock != NULL) {
	  FIXME("Not more than one keyboard can be acquired at the same time.\n");
	  SysKeyboardAImpl_Unacquire(iface);
	}
	
        current_lock = This;

        if (This->buffersize > 0) {
	  This->buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
				   This->buffersize * sizeof(*(This->buffer)));
	  This->start = 0;
	  This->count = 0;
	  This->overflow = FALSE;
	} else {
563
          This->buffer = NULL;
564 565 566
	}

	/*keyboard_hook = SetWindowsHookExW( WH_KEYBOARD_LL, KeyboardCallback, DINPUT_instance, 0 );*/
567

Lionel Ulmer's avatar
Lionel Ulmer committed
568 569 570
	return DI_OK;
}

571
static HRESULT WINAPI SysKeyboardAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
Lionel Ulmer's avatar
Lionel Ulmer committed
572
{
573
	SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
Lionel Ulmer's avatar
Lionel Ulmer committed
574 575
	TRACE("(this=%p)\n",This);

576 577 578
        if (This->acquired == 0)
          return DI_NOEFFECT;

579 580 581
	/* No more locks */
        if (current_lock == This)
          current_lock = NULL;
582
        else
583
          ERR("this != current_lock\n");
584

585
	/* Unacquire device */
586 587
        This->acquired = 0;

588 589 590 591
        if (This->buffersize >= 0) {
	  HeapFree(GetProcessHeap(), 0, This->buffer);
	  This->buffer = NULL;
	}
Lionel Ulmer's avatar
Lionel Ulmer committed
592 593 594 595

	return DI_OK;
}

596 597
static HRESULT WINAPI SysKeyboardAImpl_SetEventNotification(LPDIRECTINPUTDEVICE8A iface,
							    HANDLE hnd) {
598
  SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
599 600 601 602 603 604 605

  TRACE("(this=%p,0x%08lx)\n",This,(DWORD)hnd);

  This->hEvent = hnd;
  return DI_OK;
}

606 607 608
/******************************************************************************
  *     GetCapabilities : get the device capablitites
  */
Lionel Ulmer's avatar
Lionel Ulmer committed
609
static HRESULT WINAPI SysKeyboardAImpl_GetCapabilities(
610
	LPDIRECTINPUTDEVICE8A iface,
Lionel Ulmer's avatar
Lionel Ulmer committed
611 612
	LPDIDEVCAPS lpDIDevCaps)
{
613 614
    SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
    DIDEVCAPS devcaps;
Lionel Ulmer's avatar
Lionel Ulmer committed
615

616
    TRACE("(this=%p,%p)\n",This,lpDIDevCaps);
Lionel Ulmer's avatar
Lionel Ulmer committed
617

618 619 620 621 622 623 624
    if ((lpDIDevCaps->dwSize != sizeof(DIDEVCAPS)) && (lpDIDevCaps->dwSize != sizeof(DIDEVCAPS_DX3))) {
        WARN("invalid parameter\n");
        return DIERR_INVALIDPARAM;
    }
    
    devcaps.dwSize = lpDIDevCaps->dwSize;
    devcaps.dwFlags = DIDC_ATTACHED;
625
    if (This->dinput->dwVersion >= 0x0800)
626
	devcaps.dwDevType = DI8DEVTYPE_KEYBOARD | (DI8DEVTYPEKEYBOARD_UNKNOWN << 8);
627
    else
628 629
	devcaps.dwDevType = DIDEVTYPE_KEYBOARD | (DIDEVTYPEKEYBOARD_UNKNOWN << 8);
    devcaps.dwAxes = 0;
630
    devcaps.dwButtons = WINE_DINPUT_KEYBOARD_MAX_KEYS;
631 632 633 634 635 636 637 638 639 640
    devcaps.dwPOVs = 0;
    devcaps.dwFFSamplePeriod = 0;
    devcaps.dwFFMinTimeResolution = 0;
    devcaps.dwFirmwareRevision = 100;
    devcaps.dwHardwareRevision = 100;
    devcaps.dwFFDriverVersion = 0;

    memcpy(lpDIDevCaps, &devcaps, lpDIDevCaps->dwSize);
    
    return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
641 642
}

643 644 645 646 647 648 649 650 651 652 653
/******************************************************************************
  *     GetObjectInfo : get information about a device object such as a button
  *                     or axis
  */
static HRESULT WINAPI
SysKeyboardAImpl_GetObjectInfo(
	LPDIRECTINPUTDEVICE8A iface,
	LPDIDEVICEOBJECTINSTANCEA pdidoi,
	DWORD dwObj,
	DWORD dwHow)
{
654
    SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
    DIDEVICEOBJECTINSTANCEA ddoi;
    DWORD dwSize = pdidoi->dwSize;
    
    TRACE("(this=%p,%p,%ld,0x%08lx)\n", This, pdidoi, dwObj, dwHow);

    if (dwHow == DIPH_BYID) {
        WARN(" querying by id not supported yet...\n");
	return DI_OK;
    }

    memset(pdidoi, 0, dwSize);
    memset(&ddoi, 0, sizeof(ddoi));

    ddoi.dwSize = dwSize;
    ddoi.guidType = GUID_Key;
    ddoi.dwOfs = dwObj;
    ddoi.dwType = DIDFT_MAKEINSTANCE(dwObj) | DIDFT_BUTTON;
    GetKeyNameTextA(((dwObj & 0x7f) << 16) | ((dwObj & 0x80) << 17), ddoi.tszName, sizeof(ddoi.tszName));

    /* And return our just filled device object instance structure */
    memcpy(pdidoi, &ddoi, (dwSize < sizeof(ddoi) ? dwSize : sizeof(ddoi)));
    
    _dump_OBJECTINSTANCEA(pdidoi);

    return DI_OK;
}

682 683 684 685 686
static HRESULT WINAPI SysKeyboardWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
						     LPDIDEVICEOBJECTINSTANCEW pdidoi,
						     DWORD dwObj,
						     DWORD dwHow)
{
687
    SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
    DIDEVICEOBJECTINSTANCEW ddoi;
    DWORD dwSize = pdidoi->dwSize;
    
    TRACE("(this=%p,%p,%ld,0x%08lx)\n", This, pdidoi, dwObj, dwHow);

    if (dwHow == DIPH_BYID) {
        WARN(" querying by id not supported yet...\n");
	return DI_OK;
    }

    memset(pdidoi, 0, dwSize);
    memset(&ddoi, 0, sizeof(ddoi));

    ddoi.dwSize = dwSize;
    ddoi.guidType = GUID_Key;
    ddoi.dwOfs = dwObj;
    ddoi.dwType = DIDFT_MAKEINSTANCE(dwObj) | DIDFT_BUTTON;
    GetKeyNameTextW(((dwObj & 0x7f) << 16) | ((dwObj & 0x80) << 17), ddoi.tszName, sizeof(ddoi.tszName));

    /* And return our just filled device object instance structure */
    memcpy(pdidoi, &ddoi, (dwSize < sizeof(ddoi) ? dwSize : sizeof(ddoi)));
    
    _dump_OBJECTINSTANCEW(pdidoi);

    return DI_OK;
}

715 716 717 718 719 720 721
/******************************************************************************
  *     GetDeviceInfo : get information about a device's identity
  */
static HRESULT WINAPI SysKeyboardAImpl_GetDeviceInfo(
	LPDIRECTINPUTDEVICE8A iface,
	LPDIDEVICEINSTANCEA pdidi)
{
722
    SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
723 724 725
    TRACE("(this=%p,%p)\n", This, pdidi);

    if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)) {
726
        WARN(" dinput3 not supported yet...\n");
727 728 729
	return DI_OK;
    }

730
    fill_keyboard_dideviceinstanceA(pdidi, This->dinput->dwVersion);
731 732 733 734
    
    return DI_OK;
}

735 736
static HRESULT WINAPI SysKeyboardWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW pdidi) 
{
737
    SysKeyboardImpl *This = (SysKeyboardImpl *)iface;
738 739 740
    TRACE("(this=%p,%p)\n", This, pdidi);

    if (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)) {
741
        WARN(" dinput3 not supported yet...\n");
742 743 744
	return DI_OK;
    }

745
    fill_keyboard_dideviceinstanceW(pdidi, This->dinput->dwVersion);
746 747 748
    
    return DI_OK;
}
749

750
static const IDirectInputDevice8AVtbl SysKeyboardAvt =
Lionel Ulmer's avatar
Lionel Ulmer committed
751 752 753
{
	IDirectInputDevice2AImpl_QueryInterface,
	IDirectInputDevice2AImpl_AddRef,
754
	SysKeyboardAImpl_Release,
Lionel Ulmer's avatar
Lionel Ulmer committed
755
	SysKeyboardAImpl_GetCapabilities,
756
	SysKeyboardAImpl_EnumObjects,
757
	SysKeyboardAImpl_GetProperty,
Lionel Ulmer's avatar
Lionel Ulmer committed
758 759 760 761 762 763
	SysKeyboardAImpl_SetProperty,
	SysKeyboardAImpl_Acquire,
	SysKeyboardAImpl_Unacquire,
	SysKeyboardAImpl_GetDeviceState,
	SysKeyboardAImpl_GetDeviceData,
	IDirectInputDevice2AImpl_SetDataFormat,
764
	SysKeyboardAImpl_SetEventNotification,
Lionel Ulmer's avatar
Lionel Ulmer committed
765
	IDirectInputDevice2AImpl_SetCooperativeLevel,
766 767
	SysKeyboardAImpl_GetObjectInfo,
	SysKeyboardAImpl_GetDeviceInfo,
Lionel Ulmer's avatar
Lionel Ulmer committed
768 769 770 771 772 773 774 775 776 777
	IDirectInputDevice2AImpl_RunControlPanel,
	IDirectInputDevice2AImpl_Initialize,
	IDirectInputDevice2AImpl_CreateEffect,
	IDirectInputDevice2AImpl_EnumEffects,
	IDirectInputDevice2AImpl_GetEffectInfo,
	IDirectInputDevice2AImpl_GetForceFeedbackState,
	IDirectInputDevice2AImpl_SendForceFeedbackCommand,
	IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
	IDirectInputDevice2AImpl_Escape,
	IDirectInputDevice2AImpl_Poll,
778 779 780 781 782 783
        IDirectInputDevice2AImpl_SendDeviceData,
        IDirectInputDevice7AImpl_EnumEffectsInFile,
        IDirectInputDevice7AImpl_WriteEffectToFile,
        IDirectInputDevice8AImpl_BuildActionMap,
        IDirectInputDevice8AImpl_SetActionMap,
        IDirectInputDevice8AImpl_GetImageInfo
Lionel Ulmer's avatar
Lionel Ulmer committed
784
};
785 786 787 788 789 790 791

#if !defined(__STRICT_ANSI__) && defined(__GNUC__)
# define XCAST(fun)	(typeof(SysKeyboardWvt.fun))
#else
# define XCAST(fun)	(void*)
#endif

792
static const IDirectInputDevice8WVtbl SysKeyboardWvt =
793 794 795 796 797 798
{
	IDirectInputDevice2WImpl_QueryInterface,
	XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
	XCAST(Release)SysKeyboardAImpl_Release,
	XCAST(GetCapabilities)SysKeyboardAImpl_GetCapabilities,
	SysKeyboardWImpl_EnumObjects,
799
	XCAST(GetProperty)SysKeyboardAImpl_GetProperty,
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
	XCAST(SetProperty)SysKeyboardAImpl_SetProperty,
	XCAST(Acquire)SysKeyboardAImpl_Acquire,
	XCAST(Unacquire)SysKeyboardAImpl_Unacquire,
	XCAST(GetDeviceState)SysKeyboardAImpl_GetDeviceState,
	XCAST(GetDeviceData)SysKeyboardAImpl_GetDeviceData,
	XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
	XCAST(SetEventNotification)SysKeyboardAImpl_SetEventNotification,
	XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
	SysKeyboardWImpl_GetObjectInfo,
	SysKeyboardWImpl_GetDeviceInfo,
	XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
	XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
	XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
	IDirectInputDevice2WImpl_EnumEffects,
	IDirectInputDevice2WImpl_GetEffectInfo,
	XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
	XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
	XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
	XCAST(Escape)IDirectInputDevice2AImpl_Escape,
	XCAST(Poll)IDirectInputDevice2AImpl_Poll,
        XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
        IDirectInputDevice7WImpl_EnumEffectsInFile,
        IDirectInputDevice7WImpl_WriteEffectToFile,
        IDirectInputDevice8WImpl_BuildActionMap,
        IDirectInputDevice8WImpl_SetActionMap,
        IDirectInputDevice8WImpl_GetImageInfo
};
#undef XCAST