Added some defines for Crypt{Get/Set}KeyParam.
[wine.git] / dlls / dinput / joystick_linuxinput.c
blobdbb420c80ac3239269f14972043ab791986a3acf
1 /* DirectInput Joystick device
3 * Copyright 1998,2000 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
25 #ifdef HAVE_LINUX_INPUT_H
27 #include <assert.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <time.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #ifdef HAVE_SYS_TIME_H
36 # include <sys/time.h>
37 #endif
38 #include <sys/fcntl.h>
39 #ifdef HAVE_SYS_IOCTL_H
40 # include <sys/ioctl.h>
41 #endif
42 #include <errno.h>
43 #ifdef HAVE_SYS_ERRNO_H
44 # include <sys/errno.h>
45 #endif
47 #ifdef HAVE_CORRECT_LINUXINPUT_H
49 #ifdef HAVE_LINUX_INPUT_H
50 # include <linux/input.h>
51 #endif
54 #define EVDEVPREFIX "/dev/input/event"
56 #include "wine/debug.h"
57 #include "wine/unicode.h"
58 #include "windef.h"
59 #include "winbase.h"
60 #include "winerror.h"
61 #include "dinput.h"
63 #include "dinput_private.h"
64 #include "device_private.h"
66 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
68 /* Wine joystick driver object instances */
69 #define WINE_JOYSTICK_AXIS_BASE 0
70 #define WINE_JOYSTICK_BUTTON_BASE 8
72 typedef struct JoystickImpl JoystickImpl;
73 static IDirectInputDevice8AVtbl JoystickAvt;
74 static IDirectInputDevice8WVtbl JoystickWvt;
75 struct JoystickImpl
77 LPVOID lpVtbl;
78 DWORD ref;
79 GUID guid;
82 /* The 'parent' DInput */
83 IDirectInputImpl *dinput;
85 /* joystick private */
86 /* what range and deadzone the game wants */
87 LONG wantmin[ABS_MAX];
88 LONG wantmax[ABS_MAX];
89 LONG deadz[ABS_MAX];
91 /* autodetecting ranges per axe by following movement */
92 LONG havemax[ABS_MAX];
93 LONG havemin[ABS_MAX];
95 int joyfd;
97 LPDIDATAFORMAT df;
98 HANDLE hEvent;
99 LPDIDEVICEOBJECTDATA data_queue;
100 int queue_head, queue_tail, queue_len;
101 DIJOYSTATE2 js;
103 /* data returned by the EVIOCGABS() ioctl */
104 int axes[ABS_MAX+1][5];
106 #define AXE_ABS 0
107 #define AXE_ABSMIN 1
108 #define AXE_ABSMAX 2
109 #define AXE_ABSFUZZ 3
110 #define AXE_ABSFLAT 4
113 /* data returned by EVIOCGBIT for EV_ABS and EV_KEY */
114 BYTE absbits[(ABS_MAX+7)/8];
115 BYTE keybits[(KEY_MAX+7)/8];
118 /* This GUID is slightly different from the linux joystick one. Take note. */
119 static GUID DInput_Wine_Joystick_GUID = { /* 9e573eda-7734-11d2-8d4a-23903fb6bdf7 */
120 0x9e573eda,
121 0x7734,
122 0x11d2,
123 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
126 static void fake_current_js_state(JoystickImpl *ji);
128 #define test_bit(arr,bit) (((BYTE*)arr)[bit>>3]&(1<<(bit&7)))
130 static int joydev_have(void)
132 int i, fd;
133 int havejoy = 0;
135 for (i=0;i<64;i++) {
136 char buf[200];
137 BYTE absbits[(ABS_MAX+7)/8],keybits[(KEY_MAX+7)/8];
139 sprintf(buf,EVDEVPREFIX"%d",i);
140 if (-1!=(fd=open(buf,O_RDONLY))) {
141 if (-1==ioctl(fd,EVIOCGBIT(EV_ABS,sizeof(absbits)),absbits)) {
142 perror("EVIOCGBIT EV_ABS");
143 close(fd);
144 continue;
146 if (-1==ioctl(fd,EVIOCGBIT(EV_KEY,sizeof(keybits)),keybits)) {
147 perror("EVIOCGBIT EV_KEY");
148 close(fd);
149 continue;
151 /* A true joystick has at least axis X and Y, and at least 1
152 * button. copied from linux/drivers/input/joydev.c */
153 if (test_bit(absbits,ABS_X) && test_bit(absbits,ABS_Y) &&
154 ( test_bit(keybits,BTN_TRIGGER) ||
155 test_bit(keybits,BTN_A) ||
156 test_bit(keybits,BTN_1)
159 FIXME("found a joystick at %s!\n",buf);
160 havejoy = 1;
162 close(fd);
164 if (havejoy || (errno==ENODEV))
165 break;
167 return havejoy;
170 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, int version)
172 int havejoy = 0;
174 if ((dwDevType != 0) && (GET_DIDEVICE_TYPE(dwDevType) != DIDEVTYPE_JOYSTICK))
175 return FALSE;
177 if (dwFlags & DIEDFL_FORCEFEEDBACK)
178 return FALSE;
180 havejoy = joydev_have();
182 if (!havejoy)
183 return FALSE;
185 TRACE("Enumerating the linuxinput Joystick device\n");
187 /* Return joystick */
188 lpddi->guidInstance = GUID_Joystick;
189 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
191 lpddi->guidFFDriver = GUID_NULL;
192 if (version >= 8)
193 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
194 else
195 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
197 strcpy(lpddi->tszInstanceName, "Joystick");
198 /* ioctl JSIOCGNAME(len) */
199 strcpy(lpddi->tszProductName, "Wine Joystick");
200 return TRUE;
203 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, int version)
205 int havejoy = 0;
207 if ((dwDevType != 0) && (GET_DIDEVICE_TYPE(dwDevType) != DIDEVTYPE_JOYSTICK))
208 return FALSE;
210 if (dwFlags & DIEDFL_FORCEFEEDBACK)
211 return FALSE;
213 havejoy = joydev_have();
215 if (!havejoy)
216 return FALSE;
218 TRACE("Enumerating the linuxinput Joystick device\n");
220 /* Return joystick */
221 lpddi->guidInstance = GUID_Joystick;
222 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
224 lpddi->guidFFDriver = GUID_NULL;
225 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL<<8);
227 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, lpddi->tszInstanceName, MAX_PATH);
228 /* ioctl JSIOCGNAME(len) */
229 MultiByteToWideChar(CP_ACP, 0, "Wine Joystick", -1, lpddi->tszProductName, MAX_PATH);
230 return TRUE;
233 static JoystickImpl *alloc_device(REFGUID rguid, LPVOID jvt, IDirectInputImpl *dinput)
235 JoystickImpl* newDevice;
236 int i;
238 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
239 newDevice->lpVtbl = jvt;
240 newDevice->ref = 1;
241 newDevice->joyfd = -1;
242 newDevice->dinput = dinput;
243 memcpy(&(newDevice->guid),rguid,sizeof(*rguid));
244 for (i=0;i<ABS_MAX;i++) {
245 newDevice->wantmin[i] = -32768;
246 newDevice->wantmax[i] = 32767;
247 /* TODO:
248 * direct input defines a default for the deadzone somewhere; but as long
249 * as in map_axis the code for the dead zone is commented out its no
250 * problem
252 newDevice->deadz[i] = 0;
254 fake_current_js_state(newDevice);
255 return newDevice;
258 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
260 int havejoy = 0;
262 havejoy = joydev_have();
264 if (!havejoy)
265 return DIERR_DEVICENOTREG;
267 if ((IsEqualGUID(&GUID_Joystick,rguid)) ||
268 (IsEqualGUID(&DInput_Wine_Joystick_GUID,rguid))) {
269 if ((riid == NULL) ||
270 IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
271 IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
272 IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
273 IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
274 *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &JoystickAvt, dinput);
275 TRACE("Creating a Joystick device (%p)\n", *pdev);
276 return DI_OK;
277 } else
278 return DIERR_NOINTERFACE;
281 return DIERR_DEVICENOTREG;
285 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
287 int havejoy = 0;
289 havejoy = joydev_have();
291 if (!havejoy)
292 return DIERR_DEVICENOTREG;
294 if ((IsEqualGUID(&GUID_Joystick,rguid)) ||
295 (IsEqualGUID(&DInput_Wine_Joystick_GUID,rguid))) {
296 if ((riid == NULL) ||
297 IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
298 IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
299 IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
300 IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
301 *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &JoystickWvt, dinput);
302 TRACE("Creating a Joystick device (%p)\n", *pdev);
303 return DI_OK;
304 } else
305 return DIERR_NOINTERFACE;
308 return DIERR_DEVICENOTREG;
311 static dinput_device joydev = {
313 "Wine Linux-input joystick driver",
314 joydev_enum_deviceA,
315 joydev_enum_deviceW,
316 joydev_create_deviceA,
317 joydev_create_deviceW
320 DECL_GLOBAL_CONSTRUCTOR(joydev_register) { dinput_register_device(&joydev); }
322 /******************************************************************************
323 * Joystick
325 static ULONG WINAPI JoystickAImpl_Release(LPDIRECTINPUTDEVICE8A iface)
327 ICOM_THIS(JoystickImpl,iface);
329 This->ref--;
330 if (This->ref)
331 return This->ref;
333 /* Free the data queue */
334 if (This->data_queue != NULL)
335 HeapFree(GetProcessHeap(),0,This->data_queue);
337 /* Free the DataFormat */
338 HeapFree(GetProcessHeap(), 0, This->df);
340 HeapFree(GetProcessHeap(),0,This);
341 return 0;
344 /******************************************************************************
345 * SetDataFormat : the application can choose the format of the data
346 * the device driver sends back with GetDeviceState.
348 static HRESULT WINAPI JoystickAImpl_SetDataFormat(
349 LPDIRECTINPUTDEVICE8A iface,LPCDIDATAFORMAT df
352 ICOM_THIS(JoystickImpl,iface);
354 TRACE("(this=%p,%p)\n",This,df);
356 _dump_DIDATAFORMAT(df);
358 /* Store the new data format */
359 This->df = HeapAlloc(GetProcessHeap(),0,df->dwSize);
360 memcpy(This->df, df, df->dwSize);
361 This->df->rgodf = HeapAlloc(GetProcessHeap(),0,df->dwNumObjs*df->dwObjSize);
362 memcpy(This->df->rgodf,df->rgodf,df->dwNumObjs*df->dwObjSize);
364 return 0;
367 /******************************************************************************
368 * Acquire : gets exclusive control of the joystick
370 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
372 int i;
373 ICOM_THIS(JoystickImpl,iface);
374 char buf[200];
376 TRACE("(this=%p)\n",This);
377 if (This->joyfd!=-1)
378 return 0;
379 for (i=0;i<64;i++) {
380 sprintf(buf,EVDEVPREFIX"%d",i);
381 if (-1==(This->joyfd=open(buf,O_RDONLY))) {
382 if (errno==ENODEV)
383 return DIERR_NOTFOUND;
384 perror(buf);
385 continue;
387 if ((-1!=ioctl(This->joyfd,EVIOCGBIT(EV_ABS,sizeof(This->absbits)),This->absbits)) &&
388 (-1!=ioctl(This->joyfd,EVIOCGBIT(EV_KEY,sizeof(This->keybits)),This->keybits)) &&
389 (test_bit(This->absbits,ABS_X) && test_bit(This->absbits,ABS_Y) &&
390 (test_bit(This->keybits,BTN_TRIGGER)||
391 test_bit(This->keybits,BTN_A) ||
392 test_bit(This->keybits,BTN_1)
396 break;
397 close(This->joyfd);
398 This->joyfd = -1;
400 if (This->joyfd==-1)
401 return DIERR_NOTFOUND;
403 for (i=0;i<ABS_MAX;i++) {
404 if (test_bit(This->absbits,i)) {
405 if (-1==ioctl(This->joyfd,EVIOCGABS(i),&(This->axes[i])))
406 continue;
407 FIXME("axe %d: cur=%d, min=%d, max=%d, fuzz=%d, flat=%d\n",
409 This->axes[i][AXE_ABS],
410 This->axes[i][AXE_ABSMIN],
411 This->axes[i][AXE_ABSMAX],
412 This->axes[i][AXE_ABSFUZZ],
413 This->axes[i][AXE_ABSFLAT]
415 This->havemin[i] = This->axes[i][AXE_ABSMIN];
416 This->havemax[i] = This->axes[i][AXE_ABSMAX];
419 MESSAGE("\n");
421 fake_current_js_state(This);
423 return 0;
426 /******************************************************************************
427 * Unacquire : frees the joystick
429 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
431 ICOM_THIS(JoystickImpl,iface);
433 TRACE("(this=%p)\n",This);
434 if (This->joyfd!=-1) {
435 close(This->joyfd);
436 This->joyfd = -1;
437 return DI_OK;
439 else
440 return DI_NOEFFECT;
444 * This maps the read value (from the input event) to a value in the
445 * 'wanted' range. It also autodetects the possible range of the axe and
446 * adapts values accordingly.
448 static int
449 map_axis(JoystickImpl* This, int axis, int val) {
450 int xmin = This->axes[axis][AXE_ABSMIN];
451 int xmax = This->axes[axis][AXE_ABSMAX];
452 int hmax = This->havemax[axis];
453 int hmin = This->havemin[axis];
454 int wmin = This->wantmin[axis];
455 int wmax = This->wantmax[axis];
456 int ret;
458 if (val > hmax) This->havemax[axis] = hmax = val;
459 if (val < hmin) This->havemin[axis] = hmin = val;
461 if (xmin == xmax) return val;
463 /* map the value from the hmin-hmax range into the wmin-wmax range */
464 ret = (val * (wmax-wmin)) / (hmax-hmin) + wmin;
466 #if 0
467 /* deadzone doesn't work comfortably enough right now. needs more testing*/
468 if ((ret > -deadz/2 ) && (ret < deadz/2)) {
469 FIXME("%d in deadzone, return mid.\n",val);
470 return (wmax-wmin)/2+wmin;
472 #endif
473 return ret;
477 * set the current state of the js device as it would be with the middle
478 * values on the axes
480 static void fake_current_js_state(JoystickImpl *ji)
482 ji->js.lX = map_axis(ji, ABS_X, ji->axes[ABS_X ][AXE_ABS]);
483 ji->js.lY = map_axis(ji, ABS_Y, ji->axes[ABS_Y ][AXE_ABS]);
484 ji->js.lZ = map_axis(ji, ABS_Z, ji->axes[ABS_Z ][AXE_ABS]);
485 ji->js.lRx = map_axis(ji, ABS_RX, ji->axes[ABS_RX][AXE_ABS]);
486 ji->js.lRy = map_axis(ji, ABS_RY, ji->axes[ABS_RY][AXE_ABS]);
487 ji->js.lRz = map_axis(ji, ABS_RZ, ji->axes[ABS_RZ][AXE_ABS]);
490 static void joy_polldev(JoystickImpl *This) {
491 struct timeval tv;
492 fd_set readfds;
493 struct input_event ie;
495 if (This->joyfd==-1)
496 return;
498 while (1) {
499 memset(&tv,0,sizeof(tv));
500 FD_ZERO(&readfds);
501 FD_SET(This->joyfd,&readfds);
503 if (1>select(This->joyfd+1,&readfds,NULL,NULL,&tv))
504 return;
506 /* we have one event, so we can read */
507 if (sizeof(ie)!=read(This->joyfd,&ie,sizeof(ie)))
508 return;
510 TRACE("input_event: type %d, code %d, value %d\n",ie.type,ie.code,ie.value);
511 switch (ie.type) {
512 case EV_KEY: /* button */
513 switch (ie.code) {
514 case BTN_TRIGGER: /* normal flight stick */
515 case BTN_A: /* gamepad */
516 case BTN_1: /* generic */
517 This->js.rgbButtons[0] = ie.value?0x80:0x00;
518 GEN_EVENT(DIJOFS_BUTTON(0),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
519 break;
520 case BTN_THUMB:
521 case BTN_B:
522 case BTN_2:
523 This->js.rgbButtons[1] = ie.value?0x80:0x00;
524 GEN_EVENT(DIJOFS_BUTTON(1),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
525 break;
526 case BTN_THUMB2:
527 case BTN_C:
528 case BTN_3:
529 This->js.rgbButtons[2] = ie.value?0x80:0x00;
530 GEN_EVENT(DIJOFS_BUTTON(2),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
531 break;
532 case BTN_TOP:
533 case BTN_X:
534 case BTN_4:
535 This->js.rgbButtons[3] = ie.value?0x80:0x00;
536 GEN_EVENT(DIJOFS_BUTTON(3),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
537 break;
538 case BTN_TOP2:
539 case BTN_Y:
540 case BTN_5:
541 This->js.rgbButtons[4] = ie.value?0x80:0x00;
542 GEN_EVENT(DIJOFS_BUTTON(4),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
543 break;
544 case BTN_PINKIE:
545 case BTN_Z:
546 case BTN_6:
547 This->js.rgbButtons[5] = ie.value?0x80:0x00;
548 GEN_EVENT(DIJOFS_BUTTON(5),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
549 break;
550 case BTN_BASE:
551 case BTN_TL:
552 case BTN_7:
553 This->js.rgbButtons[6] = ie.value?0x80:0x00;
554 GEN_EVENT(DIJOFS_BUTTON(6),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
555 break;
556 case BTN_BASE2:
557 case BTN_TR:
558 case BTN_8:
559 This->js.rgbButtons[7] = ie.value?0x80:0x00;
560 GEN_EVENT(DIJOFS_BUTTON(7),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
561 break;
562 case BTN_BASE3:
563 case BTN_TL2:
564 case BTN_9:
565 This->js.rgbButtons[8] = ie.value?0x80:0x00;
566 GEN_EVENT(DIJOFS_BUTTON(8),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
567 break;
568 case BTN_BASE4:
569 case BTN_TR2:
570 This->js.rgbButtons[9] = ie.value?0x80:0x00;
571 GEN_EVENT(DIJOFS_BUTTON(9),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
572 break;
573 case BTN_BASE5:
574 case BTN_SELECT:
575 This->js.rgbButtons[10] = ie.value?0x80:0x00;
576 GEN_EVENT(DIJOFS_BUTTON(10),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
577 break;
578 default:
579 FIXME("unhandled joystick button %x, value %d\n",ie.code,ie.value);
580 break;
582 break;
583 case EV_ABS:
584 switch (ie.code) {
585 case ABS_X:
586 This->js.lX = map_axis(This,ABS_X,ie.value);
587 GEN_EVENT(DIJOFS_X,This->js.lX,ie.time.tv_usec,(This->dinput->evsequence)++);
588 break;
589 case ABS_Y:
590 This->js.lY = map_axis(This,ABS_Y,ie.value);
591 GEN_EVENT(DIJOFS_Y,This->js.lY,ie.time.tv_usec,(This->dinput->evsequence)++);
592 break;
593 case ABS_Z:
594 This->js.lZ = map_axis(This,ABS_Z,ie.value);
595 GEN_EVENT(DIJOFS_Z,This->js.lZ,ie.time.tv_usec,(This->dinput->evsequence)++);
596 break;
597 case ABS_RX:
598 This->js.lRx = map_axis(This,ABS_RX,ie.value);
599 GEN_EVENT(DIJOFS_RX,This->js.lRx,ie.time.tv_usec,(This->dinput->evsequence)++);
600 break;
601 case ABS_RY:
602 This->js.lRy = map_axis(This,ABS_RY,ie.value);
603 GEN_EVENT(DIJOFS_RY,This->js.lRy,ie.time.tv_usec,(This->dinput->evsequence)++);
604 break;
605 case ABS_RZ:
606 This->js.lRz = map_axis(This,ABS_RZ,ie.value);
607 GEN_EVENT(DIJOFS_RZ,This->js.lRz,ie.time.tv_usec,(This->dinput->evsequence)++);
608 break;
609 default:
610 FIXME("unhandled joystick axe event (code %d, value %d)\n",ie.code,ie.value);
611 break;
613 break;
614 default:
615 FIXME("joystick cannot handle type %d event (code %d)\n",ie.type,ie.code);
616 break;
621 /******************************************************************************
622 * GetDeviceState : returns the "state" of the joystick.
625 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
626 LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
628 ICOM_THIS(JoystickImpl,iface);
630 joy_polldev(This);
632 TRACE("(this=%p,0x%08lx,%p)\n",This,len,ptr);
633 if ((len != sizeof(DIJOYSTATE)) && (len != sizeof(DIJOYSTATE2))) {
634 FIXME("len %ld is not sizeof(DIJOYSTATE) or DIJOYSTATE2, unsupported format.\n",len);
635 return E_FAIL;
637 memcpy(ptr,&(This->js),len);
638 This->queue_head = 0;
639 This->queue_tail = 0;
640 return 0;
643 /******************************************************************************
644 * GetDeviceData : gets buffered input data.
646 static HRESULT WINAPI JoystickAImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface,
647 DWORD dodsize,
648 LPDIDEVICEOBJECTDATA dod,
649 LPDWORD entries,
650 DWORD flags
652 ICOM_THIS(JoystickImpl,iface);
654 FIXME("(%p)->(dods=%ld,entries=%ld,fl=0x%08lx),STUB!\n",This,dodsize,*entries,flags);
656 joy_polldev(This);
657 if (flags & DIGDD_PEEK)
658 FIXME("DIGDD_PEEK\n");
660 if (dod == NULL) {
661 } else {
663 return 0;
666 /******************************************************************************
667 * SetProperty : change input device properties
669 static HRESULT WINAPI JoystickAImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface,
670 REFGUID rguid,
671 LPCDIPROPHEADER ph)
673 ICOM_THIS(JoystickImpl,iface);
675 FIXME("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
676 FIXME("ph.dwSize = %ld, ph.dwHeaderSize =%ld, ph.dwObj = %ld, ph.dwHow= %ld\n",ph->dwSize, ph->dwHeaderSize,ph->dwObj,ph->dwHow);
678 if (!HIWORD(rguid)) {
679 switch ((DWORD)rguid) {
680 case (DWORD) DIPROP_BUFFERSIZE: {
681 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
683 FIXME("buffersize = %ld\n",pd->dwData);
684 break;
686 case (DWORD)DIPROP_RANGE: {
687 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
689 FIXME("proprange(%ld,%ld)\n",pr->lMin,pr->lMax);
690 switch (ph->dwObj) {
691 case 0: /* X */
692 case 4: /* Y */
693 case 8: /* Z */
694 case 12: /* Rx */
695 case 16: /* Ry */
696 case 20: /* Rz */
697 This->wantmin[ph->dwObj/4] = pr->lMin;
698 This->wantmax[ph->dwObj/4] = pr->lMax;
699 break;
700 default:
701 FIXME("setting proprange %ld - %ld for dwObj %ld\n",pr->lMin,pr->lMax,ph->dwObj);
703 break;
705 case (DWORD)DIPROP_DEADZONE: {
706 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
708 FIXME("setting deadzone(%ld)\n",pd->dwData);
709 This->deadz[ph->dwObj/4] = pd->dwData;
710 break;
712 default:
713 FIXME("Unknown type %ld (%s)\n",(DWORD)rguid,debugstr_guid(rguid));
714 break;
717 fake_current_js_state(This);
718 return 0;
721 /******************************************************************************
722 * SetEventNotification : specifies event to be sent on state change
724 static HRESULT WINAPI JoystickAImpl_SetEventNotification(
725 LPDIRECTINPUTDEVICE8A iface, HANDLE hnd
727 ICOM_THIS(JoystickImpl,iface);
729 TRACE("(this=%p,0x%08lx)\n",This,(DWORD)hnd);
730 This->hEvent = hnd;
731 return DI_OK;
734 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
735 LPDIRECTINPUTDEVICE8A iface,
736 LPDIDEVCAPS lpDIDevCaps)
738 ICOM_THIS(JoystickImpl,iface);
739 int xfd = This->joyfd;
740 int i,axes,buttons;
741 int wasacquired = 1;
743 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
744 if (xfd==-1) {
745 /* yes, games assume we return something, even if unacquired */
746 JoystickAImpl_Acquire(iface);
747 xfd = This->joyfd;
748 wasacquired = 0;
750 lpDIDevCaps->dwFlags = DIDC_ATTACHED;
751 lpDIDevCaps->dwDevType = DIDEVTYPE_JOYSTICK;
753 axes=0;
754 for (i=0;i<ABS_MAX;i++) if (test_bit(This->absbits,i)) axes++;
755 buttons=0;
756 for (i=0;i<KEY_MAX;i++) if (test_bit(This->keybits,i)) buttons++;
758 lpDIDevCaps->dwAxes = axes;
759 lpDIDevCaps->dwButtons = buttons;
761 if (!wasacquired)
762 JoystickAImpl_Unacquire(iface);
764 return DI_OK;
767 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface) {
768 ICOM_THIS(JoystickImpl,iface);
769 TRACE("(),stub!\n");
771 joy_polldev(This);
772 return DI_OK;
775 /******************************************************************************
776 * EnumObjects : enumerate the different buttons and axis...
778 static HRESULT WINAPI JoystickAImpl_EnumObjects(
779 LPDIRECTINPUTDEVICE8A iface,
780 LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback,
781 LPVOID lpvRef,
782 DWORD dwFlags)
784 ICOM_THIS(JoystickImpl,iface);
785 DIDEVICEOBJECTINSTANCEA ddoi;
786 int xfd = This->joyfd;
789 TRACE("(this=%p,%p,%p,%08lx)\n", This, lpCallback, lpvRef, dwFlags);
790 if (TRACE_ON(dinput)) {
791 TRACE(" - flags = ");
792 _dump_EnumObjects_flags(dwFlags);
793 TRACE("\n");
796 if (xfd == -1) return DIERR_NOTACQUIRED;
798 /* Only the fields till dwFFMaxForce are relevant */
799 ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA, dwFFMaxForce);
801 /* For the joystick, do as is done in the GetCapabilities function */
802 /* FIXME: needs more items */
803 if ((dwFlags == DIDFT_ALL) ||
804 (dwFlags & DIDFT_AXIS)) {
805 BYTE i;
807 for (i = 0; i < ABS_MAX; i++) {
808 if (!test_bit(This->absbits,i)) continue;
810 switch (i) {
811 case ABS_X:
812 ddoi.guidType = GUID_XAxis;
813 ddoi.dwOfs = DIJOFS_X;
814 break;
815 case ABS_Y:
816 ddoi.guidType = GUID_YAxis;
817 ddoi.dwOfs = DIJOFS_Y;
818 break;
819 case ABS_Z:
820 ddoi.guidType = GUID_ZAxis;
821 ddoi.dwOfs = DIJOFS_Z;
822 break;
823 case ABS_RX:
824 ddoi.guidType = GUID_RxAxis;
825 ddoi.dwOfs = DIJOFS_RX;
826 break;
827 case ABS_RY:
828 ddoi.guidType = GUID_RyAxis;
829 ddoi.dwOfs = DIJOFS_RY;
830 break;
831 case ABS_RZ:
832 ddoi.guidType = GUID_RzAxis;
833 ddoi.dwOfs = DIJOFS_RZ;
834 break;
835 case ABS_THROTTLE:
836 ddoi.guidType = GUID_Slider;
837 ddoi.dwOfs = DIJOFS_SLIDER(0);
838 break;
839 default:
840 FIXME("unhandled abs axis %d, ignoring!\n",i);
842 ddoi.dwType = DIDFT_MAKEINSTANCE((1<<i) << WINE_JOYSTICK_AXIS_BASE) | DIDFT_ABSAXIS;
843 sprintf(ddoi.tszName, "%d-Axis", i);
844 _dump_OBJECTINSTANCEA(&ddoi);
845 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE)
846 return DI_OK;
850 if ((dwFlags == DIDFT_ALL) ||
851 (dwFlags & DIDFT_BUTTON)) {
852 int i;
854 /*The DInput SDK says that GUID_Button is only for mouse buttons but well*/
856 ddoi.guidType = GUID_Button;
858 for (i = 0; i < KEY_MAX; i++) {
859 if (!test_bit(This->keybits,i)) continue;
861 switch (i) {
862 case BTN_TRIGGER:
863 case BTN_A:
864 case BTN_1:
865 ddoi.dwOfs = DIJOFS_BUTTON(0);
866 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 0) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
867 break;
868 case BTN_THUMB:
869 case BTN_B:
870 case BTN_2:
871 ddoi.dwOfs = DIJOFS_BUTTON(1);
872 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 1) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
873 break;
874 case BTN_THUMB2:
875 case BTN_C:
876 case BTN_3:
877 ddoi.dwOfs = DIJOFS_BUTTON(2);
878 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 2) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
879 break;
880 case BTN_TOP:
881 case BTN_X:
882 case BTN_4:
883 ddoi.dwOfs = DIJOFS_BUTTON(3);
884 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 3) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
885 break;
886 case BTN_TOP2:
887 case BTN_Y:
888 case BTN_5:
889 ddoi.dwOfs = DIJOFS_BUTTON(4);
890 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 4) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
891 break;
892 case BTN_PINKIE:
893 case BTN_Z:
894 case BTN_6:
895 ddoi.dwOfs = DIJOFS_BUTTON(5);
896 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 5) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
897 break;
898 case BTN_BASE:
899 case BTN_TL:
900 case BTN_7:
901 ddoi.dwOfs = DIJOFS_BUTTON(6);
902 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 6) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
903 break;
904 case BTN_BASE2:
905 case BTN_TR:
906 case BTN_8:
907 ddoi.dwOfs = DIJOFS_BUTTON(7);
908 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 7) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
909 break;
910 case BTN_BASE3:
911 case BTN_TL2:
912 case BTN_9:
913 ddoi.dwOfs = DIJOFS_BUTTON(8);
914 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 8) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
915 break;
916 case BTN_BASE4:
917 case BTN_TR2:
918 ddoi.dwOfs = DIJOFS_BUTTON(9);
919 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 9) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
920 break;
921 case BTN_BASE5:
922 case BTN_SELECT:
923 ddoi.dwOfs = DIJOFS_BUTTON(10);
924 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 10) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
925 break;
927 sprintf(ddoi.tszName, "%d-Button", i);
928 _dump_OBJECTINSTANCEA(&ddoi);
929 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE)
930 return DI_OK;
934 if (xfd!=This->joyfd)
935 close(xfd);
937 return DI_OK;
940 static HRESULT WINAPI JoystickWImpl_EnumObjects(LPDIRECTINPUTDEVICE8W iface,
941 LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback,
942 LPVOID lpvRef,
943 DWORD dwFlags)
945 ICOM_THIS(JoystickImpl,iface);
947 device_enumobjects_AtoWcb_data data;
949 data.lpCallBack = lpCallback;
950 data.lpvRef = lpvRef;
952 return JoystickAImpl_EnumObjects((LPDIRECTINPUTDEVICE8A) This, (LPDIENUMDEVICEOBJECTSCALLBACKA) DIEnumDevicesCallbackAtoW, (LPVOID) &data, dwFlags);
955 /******************************************************************************
956 * GetProperty : get input device properties
958 static HRESULT WINAPI JoystickAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface,
959 REFGUID rguid,
960 LPDIPROPHEADER pdiph)
962 ICOM_THIS(JoystickImpl,iface);
964 TRACE("(this=%p,%s,%p): stub!\n",
965 iface, debugstr_guid(rguid), pdiph);
967 if (TRACE_ON(dinput))
968 _dump_DIPROPHEADER(pdiph);
970 if (!HIWORD(rguid)) {
971 switch ((DWORD)rguid) {
972 case (DWORD) DIPROP_BUFFERSIZE: {
973 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
975 TRACE(" return buffersize = %d\n",This->queue_len);
976 pd->dwData = This->queue_len;
977 break;
980 case (DWORD) DIPROP_RANGE: {
981 /* LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph; */
982 if ((pdiph->dwHow == DIPH_BYID) &&
983 (pdiph->dwObj & DIDFT_ABSAXIS)) {
984 /* The app is querying the current range of the axis : return the lMin and lMax values */
985 FIXME("unimplemented axis range query.\n");
988 break;
991 default:
992 FIXME("Unknown type %ld (%s)\n",(DWORD)rguid,debugstr_guid(rguid));
993 break;
998 return DI_OK;
1001 static IDirectInputDevice8AVtbl JoystickAvt =
1003 IDirectInputDevice2AImpl_QueryInterface,
1004 IDirectInputDevice2AImpl_AddRef,
1005 JoystickAImpl_Release,
1006 JoystickAImpl_GetCapabilities,
1007 JoystickAImpl_EnumObjects,
1008 JoystickAImpl_GetProperty,
1009 JoystickAImpl_SetProperty,
1010 JoystickAImpl_Acquire,
1011 JoystickAImpl_Unacquire,
1012 JoystickAImpl_GetDeviceState,
1013 JoystickAImpl_GetDeviceData,
1014 JoystickAImpl_SetDataFormat,
1015 JoystickAImpl_SetEventNotification,
1016 IDirectInputDevice2AImpl_SetCooperativeLevel,
1017 IDirectInputDevice2AImpl_GetObjectInfo,
1018 IDirectInputDevice2AImpl_GetDeviceInfo,
1019 IDirectInputDevice2AImpl_RunControlPanel,
1020 IDirectInputDevice2AImpl_Initialize,
1021 IDirectInputDevice2AImpl_CreateEffect,
1022 IDirectInputDevice2AImpl_EnumEffects,
1023 IDirectInputDevice2AImpl_GetEffectInfo,
1024 IDirectInputDevice2AImpl_GetForceFeedbackState,
1025 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1026 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1027 IDirectInputDevice2AImpl_Escape,
1028 JoystickAImpl_Poll,
1029 IDirectInputDevice2AImpl_SendDeviceData,
1030 IDirectInputDevice7AImpl_EnumEffectsInFile,
1031 IDirectInputDevice7AImpl_WriteEffectToFile,
1032 IDirectInputDevice8AImpl_BuildActionMap,
1033 IDirectInputDevice8AImpl_SetActionMap,
1034 IDirectInputDevice8AImpl_GetImageInfo
1037 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1038 # define XCAST(fun) (typeof(JoystickWvt.fun))
1039 #else
1040 # define XCAST(fun) (void*)
1041 #endif
1043 static IDirectInputDevice8WVtbl JoystickWvt =
1045 IDirectInputDevice2WImpl_QueryInterface,
1046 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1047 XCAST(Release)JoystickAImpl_Release,
1048 XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1049 JoystickWImpl_EnumObjects,
1050 XCAST(GetProperty)JoystickAImpl_GetProperty,
1051 XCAST(SetProperty)JoystickAImpl_SetProperty,
1052 XCAST(Acquire)JoystickAImpl_Acquire,
1053 XCAST(Unacquire)JoystickAImpl_Unacquire,
1054 XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1055 XCAST(GetDeviceData)JoystickAImpl_GetDeviceData,
1056 XCAST(SetDataFormat)JoystickAImpl_SetDataFormat,
1057 XCAST(SetEventNotification)JoystickAImpl_SetEventNotification,
1058 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1059 IDirectInputDevice2WImpl_GetObjectInfo,
1060 IDirectInputDevice2WImpl_GetDeviceInfo,
1061 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1062 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1063 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
1064 IDirectInputDevice2WImpl_EnumEffects,
1065 IDirectInputDevice2WImpl_GetEffectInfo,
1066 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
1067 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1068 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1069 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1070 XCAST(Poll)JoystickAImpl_Poll,
1071 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1072 IDirectInputDevice7WImpl_EnumEffectsInFile,
1073 IDirectInputDevice7WImpl_WriteEffectToFile,
1074 IDirectInputDevice8WImpl_BuildActionMap,
1075 IDirectInputDevice8WImpl_SetActionMap,
1076 IDirectInputDevice8WImpl_GetImageInfo
1078 #undef XCAST
1080 #endif /* HAVE_LINUX_INPUT_H */
1082 #endif