Fixed header dependencies to be fully compatible with the Windows
[wine/multimedia.git] / dlls / dinput / joystick / linuxinput.c
blob6f4be76ff17dad515afff1a4a2a65c733cc69fc7
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 ICOM_VTABLE(IDirectInputDevice8A) JoystickAvt;
74 static ICOM_VTABLE(IDirectInputDevice8W) 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 DIJOYSTATE 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 #define test_bit(arr,bit) (((BYTE*)arr)[bit>>3]&(1<<(bit&7)))
128 static int joydev_have(void)
130 int i, fd;
131 int havejoy = 0;
133 for (i=0;i<64;i++) {
134 char buf[200];
135 BYTE absbits[(ABS_MAX+7)/8],keybits[(KEY_MAX+7)/8];
137 sprintf(buf,EVDEVPREFIX"%d",i);
138 if (-1!=(fd=open(buf,O_RDONLY))) {
139 if (-1==ioctl(fd,EVIOCGBIT(EV_ABS,sizeof(absbits)),absbits)) {
140 perror("EVIOCGBIT EV_ABS");
141 close(fd);
142 continue;
144 if (-1==ioctl(fd,EVIOCGBIT(EV_KEY,sizeof(keybits)),keybits)) {
145 perror("EVIOCGBIT EV_KEY");
146 close(fd);
147 continue;
149 /* A true joystick has at least axis X and Y, and at least 1
150 * button. copied from linux/drivers/input/joydev.c */
151 if (test_bit(absbits,ABS_X) && test_bit(absbits,ABS_Y) &&
152 ( test_bit(keybits,BTN_TRIGGER) ||
153 test_bit(keybits,BTN_A) ||
154 test_bit(keybits,BTN_1)
157 FIXME("found a joystick at %s!\n",buf);
158 havejoy = 1;
160 close(fd);
162 if (havejoy || (errno==ENODEV))
163 break;
165 return havejoy;
168 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, int version)
170 int havejoy = 0;
172 if ((dwDevType != 0) && (GET_DIDEVICE_TYPE(dwDevType) != DIDEVTYPE_JOYSTICK))
173 return FALSE;
175 if (dwFlags & DIEDFL_FORCEFEEDBACK)
176 return FALSE;
178 havejoy = joydev_have();
180 if (!havejoy)
181 return FALSE;
183 TRACE("Enumerating the linuxinput Joystick device\n");
185 /* Return joystick */
186 lpddi->guidInstance = GUID_Joystick;
187 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
189 lpddi->guidFFDriver = GUID_NULL;
190 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL<<8);
192 strcpy(lpddi->tszInstanceName, "Joystick");
193 /* ioctl JSIOCGNAME(len) */
194 strcpy(lpddi->tszProductName, "Wine Joystick");
195 return TRUE;
198 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, int version)
200 int havejoy = 0;
202 if ((dwDevType != 0) && (GET_DIDEVICE_TYPE(dwDevType) != DIDEVTYPE_JOYSTICK))
203 return FALSE;
205 if (dwFlags & DIEDFL_FORCEFEEDBACK)
206 return FALSE;
208 havejoy = joydev_have();
210 if (!havejoy)
211 return FALSE;
213 TRACE("Enumerating the linuxinput Joystick device\n");
215 /* Return joystick */
216 lpddi->guidInstance = GUID_Joystick;
217 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
219 lpddi->guidFFDriver = GUID_NULL;
220 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL<<8);
222 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, lpddi->tszInstanceName, MAX_PATH);
223 /* ioctl JSIOCGNAME(len) */
224 MultiByteToWideChar(CP_ACP, 0, "Wine Joystick", -1, lpddi->tszProductName, MAX_PATH);
225 return TRUE;
228 static JoystickImpl *alloc_device(REFGUID rguid, LPVOID jvt, IDirectInputImpl *dinput)
230 JoystickImpl* newDevice;
231 int i;
233 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
234 newDevice->lpVtbl = jvt;
235 newDevice->ref = 1;
236 newDevice->joyfd = -1;
237 newDevice->dinput = dinput;
238 memcpy(&(newDevice->guid),rguid,sizeof(*rguid));
239 for (i=0;i<ABS_MAX;i++) {
240 newDevice->wantmin[i] = -32768;
241 newDevice->wantmax[i] = 32767;
242 newDevice->deadz[i] = 1024; /* guessing */
244 return newDevice;
247 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
249 int havejoy = 0;
251 havejoy = joydev_have();
253 if (!havejoy)
254 return DIERR_DEVICENOTREG;
256 if ((IsEqualGUID(&GUID_Joystick,rguid)) ||
257 (IsEqualGUID(&DInput_Wine_Joystick_GUID,rguid))) {
258 if ((riid == NULL) ||
259 IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
260 IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
261 IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
262 IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
263 *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &JoystickAvt, dinput);
264 TRACE("Creating a Joystick device (%p)\n", *pdev);
265 return DI_OK;
266 } else
267 return DIERR_NOINTERFACE;
270 return DIERR_DEVICENOTREG;
274 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
276 int havejoy = 0;
278 havejoy = joydev_have();
280 if (!havejoy)
281 return DIERR_DEVICENOTREG;
283 if ((IsEqualGUID(&GUID_Joystick,rguid)) ||
284 (IsEqualGUID(&DInput_Wine_Joystick_GUID,rguid))) {
285 if ((riid == NULL) ||
286 IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
287 IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
288 IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
289 IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
290 *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &JoystickWvt, dinput);
291 TRACE("Creating a Joystick device (%p)\n", *pdev);
292 return DI_OK;
293 } else
294 return DIERR_NOINTERFACE;
297 return DIERR_DEVICENOTREG;
300 static dinput_device joydev = {
302 joydev_enum_deviceA,
303 joydev_enum_deviceW,
304 joydev_create_deviceA,
305 joydev_create_deviceW
308 DECL_GLOBAL_CONSTRUCTOR(joydev_register) { dinput_register_device(&joydev); }
310 /******************************************************************************
311 * Joystick
313 static ULONG WINAPI JoystickAImpl_Release(LPDIRECTINPUTDEVICE8A iface)
315 ICOM_THIS(JoystickImpl,iface);
317 This->ref--;
318 if (This->ref)
319 return This->ref;
321 /* Free the data queue */
322 if (This->data_queue != NULL)
323 HeapFree(GetProcessHeap(),0,This->data_queue);
325 /* Free the DataFormat */
326 HeapFree(GetProcessHeap(), 0, This->df);
328 HeapFree(GetProcessHeap(),0,This);
329 return 0;
332 /******************************************************************************
333 * SetDataFormat : the application can choose the format of the data
334 * the device driver sends back with GetDeviceState.
336 static HRESULT WINAPI JoystickAImpl_SetDataFormat(
337 LPDIRECTINPUTDEVICE8A iface,LPCDIDATAFORMAT df
340 ICOM_THIS(JoystickImpl,iface);
341 int i;
343 TRACE("(this=%p,%p)\n",This,df);
345 TRACE("(df.dwSize=%ld)\n",df->dwSize);
346 TRACE("(df.dwObjsize=%ld)\n",df->dwObjSize);
347 TRACE("(df.dwFlags=0x%08lx)\n",df->dwFlags);
348 TRACE("(df.dwDataSize=%ld)\n",df->dwDataSize);
349 TRACE("(df.dwNumObjs=%ld)\n",df->dwNumObjs);
351 for (i=0;i<df->dwNumObjs;i++) {
352 TRACE("df.rgodf[%d].guid %s (%p)\n",i,debugstr_guid(df->rgodf[i].pguid), df->rgodf[i].pguid);
353 TRACE("df.rgodf[%d].dwOfs %ld\n",i,df->rgodf[i].dwOfs);
354 TRACE("dwType 0x%02x,dwInstance %d\n",DIDFT_GETTYPE(df->rgodf[i].dwType),DIDFT_GETINSTANCE(df->rgodf[i].dwType));
355 TRACE("df.rgodf[%d].dwFlags 0x%08lx\n",i,df->rgodf[i].dwFlags);
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->havemax[i] = This->axes[i][AXE_ABSMIN];
416 This->havemin[i] = This->axes[i][AXE_ABSMAX];
419 MESSAGE("\n");
421 return 0;
424 /******************************************************************************
425 * Unacquire : frees the joystick
427 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
429 ICOM_THIS(JoystickImpl,iface);
431 TRACE("(this=%p)\n",This);
432 if (This->joyfd!=-1) {
433 close(This->joyfd);
434 This->joyfd = -1;
436 return DI_OK;
440 * This maps the read value (from the input event) to a value in the
441 * 'wanted' range. It also autodetects the possible range of the axe and
442 * adapts values accordingly.
444 static int
445 map_axis(JoystickImpl* This, int axis, int val) {
446 int xmid = This->axes[axis][AXE_ABS];
447 int xmin = This->axes[axis][AXE_ABSMIN];
448 int xmax = This->axes[axis][AXE_ABSMAX];
449 int hmax = This->havemax[axis];
450 int hmin = This->havemin[axis];
451 int wmin = This->wantmin[axis];
452 int wmax = This->wantmax[axis];
453 int ret;
455 if (val > hmax) This->havemax[axis] = hmax = val;
456 if (val < hmin) This->havemin[axis] = hmin = val;
458 if (xmin == xmax) return val;
459 if ((hmin == hmax) || (hmax==xmid) || (hmin==xmid)) return val;
461 /* dont assume total linearity, but linearity starting from a zeropoint */
462 if (val > xmid) {
463 ret = (val-xmid)*((wmax-wmin)/2)/(hmax-xmid)+0;
464 } else {
465 ret = (xmid-val)*((wmax-wmin)/2)/(hmin-xmid)+0;
468 #if 0
469 /* deadzone doesn't work comfortably enough right now. needs more testing*/
470 if ((ret > -deadz/2 ) && (ret < deadz/2)) {
471 FIXME("%d in deadzone, return mid.\n",val);
472 return (wmax-wmin)/2+wmin;
474 #endif
475 return ret;
478 static void joy_polldev(JoystickImpl *This) {
479 struct timeval tv;
480 fd_set readfds;
481 struct input_event ie;
483 if (This->joyfd==-1)
484 return;
486 while (1) {
487 memset(&tv,0,sizeof(tv));
488 FD_ZERO(&readfds);
489 FD_SET(This->joyfd,&readfds);
491 if (1>select(This->joyfd+1,&readfds,NULL,NULL,&tv))
492 return;
494 /* we have one event, so we can read */
495 if (sizeof(ie)!=read(This->joyfd,&ie,sizeof(ie)))
496 return;
498 TRACE("input_event: type %d, code %d, value %d\n",ie.type,ie.code,ie.value);
499 switch (ie.type) {
500 case EV_KEY: /* button */
501 switch (ie.code) {
502 case BTN_TRIGGER: /* normal flight stick */
503 case BTN_A: /* gamepad */
504 case BTN_1: /* generic */
505 GEN_EVENT(DIJOFS_BUTTON(0),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
506 This->js.rgbButtons[0] = ie.value?0x80:0x00;
507 break;
508 case BTN_THUMB:
509 case BTN_B:
510 case BTN_2:
511 GEN_EVENT(DIJOFS_BUTTON(1),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
512 This->js.rgbButtons[1] = ie.value?0x80:0x00;
513 break;
514 case BTN_THUMB2:
515 case BTN_C:
516 case BTN_3:
517 GEN_EVENT(DIJOFS_BUTTON(2),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
518 This->js.rgbButtons[2] = ie.value?0x80:0x00;
519 break;
520 default:
521 FIXME("unhandled joystick axe %x, value %d\n",ie.code,ie.value);
522 break;
524 break;
525 case EV_ABS:
526 switch (ie.code) {
527 case ABS_X:
528 GEN_EVENT(DIJOFS_X,ie.value,ie.time.tv_usec,(This->dinput->evsequence)++);
529 This->js.lX = map_axis(This,0,ie.value);
530 break;
531 case ABS_Y:
532 GEN_EVENT(DIJOFS_Y,ie.value,ie.time.tv_usec,(This->dinput->evsequence)++);
533 This->js.lY = map_axis(This,1,ie.value);
534 case ABS_Z:
535 GEN_EVENT(DIJOFS_Z,ie.value,ie.time.tv_usec,(This->dinput->evsequence)++);
536 This->js.lZ = map_axis(This,2,ie.value);
537 break;
538 default:
539 FIXME("unhandled joystick axe event (code %d, value %d)\n",ie.code,ie.value);
540 break;
542 break;
543 default:
544 FIXME("joystick cannot handle type %d event (code %d)\n",ie.type,ie.code);
545 break;
550 /******************************************************************************
551 * GetDeviceState : returns the "state" of the joystick.
554 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
555 LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
557 ICOM_THIS(JoystickImpl,iface);
559 joy_polldev(This);
561 TRACE("(this=%p,0x%08lx,%p)\n",This,len,ptr);
562 if (len != sizeof(DIJOYSTATE)) {
563 FIXME("len %ld is not sizeof(DIJOYSTATE), unsupported format.\n",len);
565 memcpy(ptr,&(This->js),len);
566 This->queue_head = 0;
567 This->queue_tail = 0;
568 return 0;
571 /******************************************************************************
572 * GetDeviceData : gets buffered input data.
574 static HRESULT WINAPI JoystickAImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface,
575 DWORD dodsize,
576 LPDIDEVICEOBJECTDATA dod,
577 LPDWORD entries,
578 DWORD flags
580 ICOM_THIS(JoystickImpl,iface);
582 FIXME("(%p)->(dods=%ld,entries=%ld,fl=0x%08lx),STUB!\n",This,dodsize,*entries,flags);
584 joy_polldev(This);
585 if (flags & DIGDD_PEEK)
586 FIXME("DIGDD_PEEK\n");
588 if (dod == NULL) {
589 } else {
591 return 0;
594 /******************************************************************************
595 * SetProperty : change input device properties
597 static HRESULT WINAPI JoystickAImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface,
598 REFGUID rguid,
599 LPCDIPROPHEADER ph)
601 ICOM_THIS(JoystickImpl,iface);
603 FIXME("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
604 FIXME("ph.dwSize = %ld, ph.dwHeaderSize =%ld, ph.dwObj = %ld, ph.dwHow= %ld\n",ph->dwSize, ph->dwHeaderSize,ph->dwObj,ph->dwHow);
606 if (!HIWORD(rguid)) {
607 switch ((DWORD)rguid) {
608 case (DWORD) DIPROP_BUFFERSIZE: {
609 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
611 FIXME("buffersize = %ld\n",pd->dwData);
612 break;
614 case (DWORD)DIPROP_RANGE: {
615 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
617 FIXME("proprange(%ld,%ld)\n",pr->lMin,pr->lMax);
618 switch (ph->dwObj) {
619 case 0: /* X */
620 case 4: /* Y */
621 case 8: /* Z */
622 case 12: /* Rx */
623 case 16: /* Ry */
624 case 20: /* Rz */
625 This->wantmin[ph->dwObj/4] = pr->lMin;
626 This->wantmax[ph->dwObj/4] = pr->lMax;
627 break;
628 default:
629 FIXME("setting proprange %ld - %ld for dwObj %ld\n",pr->lMin,pr->lMax,ph->dwObj);
631 break;
633 case (DWORD)DIPROP_DEADZONE: {
634 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
636 FIXME("setting deadzone(%ld)\n",pd->dwData);
637 This->deadz[ph->dwObj/4] = pd->dwData;
638 break;
640 default:
641 FIXME("Unknown type %ld (%s)\n",(DWORD)rguid,debugstr_guid(rguid));
642 break;
645 return 0;
648 /******************************************************************************
649 * SetEventNotification : specifies event to be sent on state change
651 static HRESULT WINAPI JoystickAImpl_SetEventNotification(
652 LPDIRECTINPUTDEVICE8A iface, HANDLE hnd
654 ICOM_THIS(JoystickImpl,iface);
656 TRACE("(this=%p,0x%08lx)\n",This,(DWORD)hnd);
657 This->hEvent = hnd;
658 return DI_OK;
661 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
662 LPDIRECTINPUTDEVICE8A iface,
663 LPDIDEVCAPS lpDIDevCaps)
665 ICOM_THIS(JoystickImpl,iface);
666 int xfd = This->joyfd;
667 int i,axes,buttons;
668 int wasacquired = 1;
670 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
671 if (xfd==-1) {
672 /* yes, games assume we return something, even if unacquired */
673 JoystickAImpl_Acquire(iface);
674 xfd = This->joyfd;
675 wasacquired = 0;
677 lpDIDevCaps->dwFlags = DIDC_ATTACHED;
678 lpDIDevCaps->dwDevType = DIDEVTYPE_JOYSTICK;
680 axes=0;
681 for (i=0;i<ABS_MAX;i++) if (test_bit(This->absbits,i)) axes++;
682 buttons=0;
683 for (i=0;i<KEY_MAX;i++) if (test_bit(This->keybits,i)) buttons++;
685 lpDIDevCaps->dwAxes = axes;
686 lpDIDevCaps->dwButtons = buttons;
688 if (!wasacquired)
689 JoystickAImpl_Unacquire(iface);
691 return DI_OK;
694 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface) {
695 ICOM_THIS(JoystickImpl,iface);
696 TRACE("(),stub!\n");
698 joy_polldev(This);
699 return DI_OK;
702 /******************************************************************************
703 * EnumObjects : enumerate the different buttons and axis...
705 static HRESULT WINAPI JoystickAImpl_EnumObjects(
706 LPDIRECTINPUTDEVICE8A iface,
707 LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback,
708 LPVOID lpvRef,
709 DWORD dwFlags)
711 ICOM_THIS(JoystickImpl,iface);
712 DIDEVICEOBJECTINSTANCEA ddoi;
713 int xfd = This->joyfd;
716 TRACE("(this=%p,%p,%p,%08lx)\n", This, lpCallback, lpvRef, dwFlags);
717 if (TRACE_ON(dinput)) {
718 TRACE(" - flags = ");
719 _dump_EnumObjects_flags(dwFlags);
720 TRACE("\n");
723 if (xfd == -1) return DIERR_NOTACQUIRED;
725 /* Only the fields till dwFFMaxForce are relevant */
726 ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA, dwFFMaxForce);
728 /* For the joystick, do as is done in the GetCapabilities function */
729 /* FIXME: needs more items */
730 if ((dwFlags == DIDFT_ALL) ||
731 (dwFlags & DIDFT_AXIS)) {
732 BYTE i;
734 for (i = 0; i < ABS_MAX; i++) {
735 if (!test_bit(This->absbits,i)) continue;
737 switch (i) {
738 case ABS_X:
739 ddoi.guidType = GUID_XAxis;
740 ddoi.dwOfs = DIJOFS_X;
741 break;
742 case ABS_Y:
743 ddoi.guidType = GUID_YAxis;
744 ddoi.dwOfs = DIJOFS_Y;
745 break;
746 case ABS_Z:
747 ddoi.guidType = GUID_ZAxis;
748 ddoi.dwOfs = DIJOFS_Z;
749 break;
750 case ABS_RX:
751 ddoi.guidType = GUID_RxAxis;
752 ddoi.dwOfs = DIJOFS_RX;
753 break;
754 case ABS_RY:
755 ddoi.guidType = GUID_RyAxis;
756 ddoi.dwOfs = DIJOFS_RY;
757 break;
758 case ABS_RZ:
759 ddoi.guidType = GUID_RzAxis;
760 ddoi.dwOfs = DIJOFS_RZ;
761 break;
762 case ABS_THROTTLE:
763 ddoi.guidType = GUID_Slider;
764 ddoi.dwOfs = DIJOFS_SLIDER(0);
765 break;
766 default:
767 FIXME("unhandled abs axis %d, ignoring!\n",i);
769 ddoi.dwType = DIDFT_MAKEINSTANCE((1<<i) << WINE_JOYSTICK_AXIS_BASE) | DIDFT_ABSAXIS;
770 sprintf(ddoi.tszName, "%d-Axis", i);
771 _dump_OBJECTINSTANCEA(&ddoi);
772 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE)
773 return DI_OK;
777 if ((dwFlags == DIDFT_ALL) ||
778 (dwFlags & DIDFT_BUTTON)) {
779 int i;
781 /*The DInput SDK says that GUID_Button is only for mouse buttons but well*/
783 ddoi.guidType = GUID_Button;
785 for (i = 0; i < KEY_MAX; i++) {
786 if (!test_bit(This->keybits,i)) continue;
788 switch (i) {
789 case BTN_TRIGGER:
790 case BTN_A:
791 case BTN_1:
792 ddoi.dwOfs = DIJOFS_BUTTON(0);
793 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 0) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
794 break;
795 case BTN_THUMB:
796 case BTN_B:
797 case BTN_2:
798 ddoi.dwOfs = DIJOFS_BUTTON(1);
799 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 1) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
800 break;
801 case BTN_THUMB2:
802 case BTN_C:
803 case BTN_3:
804 ddoi.dwOfs = DIJOFS_BUTTON(2);
805 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 2) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
806 break;
807 case BTN_TOP:
808 case BTN_X:
809 case BTN_4:
810 ddoi.dwOfs = DIJOFS_BUTTON(3);
811 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 3) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
812 break;
813 case BTN_TOP2:
814 case BTN_Y:
815 case BTN_5:
816 ddoi.dwOfs = DIJOFS_BUTTON(4);
817 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 4) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
818 break;
819 case BTN_PINKIE:
820 case BTN_Z:
821 case BTN_6:
822 ddoi.dwOfs = DIJOFS_BUTTON(5);
823 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 5) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
824 break;
825 case BTN_BASE:
826 case BTN_TL:
827 case BTN_7:
828 ddoi.dwOfs = DIJOFS_BUTTON(6);
829 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 6) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
830 break;
831 case BTN_BASE2:
832 case BTN_TR:
833 case BTN_8:
834 ddoi.dwOfs = DIJOFS_BUTTON(7);
835 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 7) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
836 break;
837 case BTN_BASE3:
838 case BTN_TL2:
839 case BTN_9:
840 ddoi.dwOfs = DIJOFS_BUTTON(8);
841 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 8) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
842 break;
843 case BTN_BASE4:
844 case BTN_TR2:
845 ddoi.dwOfs = DIJOFS_BUTTON(9);
846 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 9) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
847 break;
848 case BTN_BASE5:
849 case BTN_SELECT:
850 ddoi.dwOfs = DIJOFS_BUTTON(10);
851 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 10) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
852 break;
854 sprintf(ddoi.tszName, "%d-Button", i);
855 _dump_OBJECTINSTANCEA(&ddoi);
856 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE)
857 return DI_OK;
861 if (xfd!=This->joyfd)
862 close(xfd);
864 return DI_OK;
867 static HRESULT WINAPI JoystickWImpl_EnumObjects(LPDIRECTINPUTDEVICE8W iface,
868 LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback,
869 LPVOID lpvRef,
870 DWORD dwFlags)
872 ICOM_THIS(JoystickImpl,iface);
874 device_enumobjects_AtoWcb_data data;
876 data.lpCallBack = lpCallback;
877 data.lpvRef = lpvRef;
879 return JoystickAImpl_EnumObjects((LPDIRECTINPUTDEVICE8A) This, (LPDIENUMDEVICEOBJECTSCALLBACKA) DIEnumDevicesCallbackAtoW, (LPVOID) &data, dwFlags);
882 /******************************************************************************
883 * GetProperty : get input device properties
885 static HRESULT WINAPI JoystickAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface,
886 REFGUID rguid,
887 LPDIPROPHEADER pdiph)
889 ICOM_THIS(JoystickImpl,iface);
891 TRACE("(this=%p,%s,%p): stub!\n",
892 iface, debugstr_guid(rguid), pdiph);
894 if (TRACE_ON(dinput))
895 _dump_DIPROPHEADER(pdiph);
897 if (!HIWORD(rguid)) {
898 switch ((DWORD)rguid) {
899 case (DWORD) DIPROP_BUFFERSIZE: {
900 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
902 TRACE(" return buffersize = %d\n",This->queue_len);
903 pd->dwData = This->queue_len;
904 break;
907 case (DWORD) DIPROP_RANGE: {
908 /* LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph; */
909 if ((pdiph->dwHow == DIPH_BYID) &&
910 (pdiph->dwObj & DIDFT_ABSAXIS)) {
911 /* The app is querying the current range of the axis : return the lMin and lMax values */
912 FIXME("unimplemented axis range query.\n");
915 break;
918 default:
919 FIXME("Unknown type %ld (%s)\n",(DWORD)rguid,debugstr_guid(rguid));
920 break;
925 return DI_OK;
928 static ICOM_VTABLE(IDirectInputDevice8A) JoystickAvt =
930 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
931 IDirectInputDevice2AImpl_QueryInterface,
932 IDirectInputDevice2AImpl_AddRef,
933 JoystickAImpl_Release,
934 JoystickAImpl_GetCapabilities,
935 JoystickAImpl_EnumObjects,
936 JoystickAImpl_GetProperty,
937 JoystickAImpl_SetProperty,
938 JoystickAImpl_Acquire,
939 JoystickAImpl_Unacquire,
940 JoystickAImpl_GetDeviceState,
941 JoystickAImpl_GetDeviceData,
942 JoystickAImpl_SetDataFormat,
943 JoystickAImpl_SetEventNotification,
944 IDirectInputDevice2AImpl_SetCooperativeLevel,
945 IDirectInputDevice2AImpl_GetObjectInfo,
946 IDirectInputDevice2AImpl_GetDeviceInfo,
947 IDirectInputDevice2AImpl_RunControlPanel,
948 IDirectInputDevice2AImpl_Initialize,
949 IDirectInputDevice2AImpl_CreateEffect,
950 IDirectInputDevice2AImpl_EnumEffects,
951 IDirectInputDevice2AImpl_GetEffectInfo,
952 IDirectInputDevice2AImpl_GetForceFeedbackState,
953 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
954 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
955 IDirectInputDevice2AImpl_Escape,
956 JoystickAImpl_Poll,
957 IDirectInputDevice2AImpl_SendDeviceData,
958 IDirectInputDevice7AImpl_EnumEffectsInFile,
959 IDirectInputDevice7AImpl_WriteEffectToFile,
960 IDirectInputDevice8AImpl_BuildActionMap,
961 IDirectInputDevice8AImpl_SetActionMap,
962 IDirectInputDevice8AImpl_GetImageInfo
965 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
966 # define XCAST(fun) (typeof(JoystickWvt.fun))
967 #else
968 # define XCAST(fun) (void*)
969 #endif
971 static ICOM_VTABLE(IDirectInputDevice8W) JoystickWvt =
973 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
974 IDirectInputDevice2WImpl_QueryInterface,
975 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
976 XCAST(Release)JoystickAImpl_Release,
977 XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
978 JoystickWImpl_EnumObjects,
979 XCAST(GetProperty)JoystickAImpl_GetProperty,
980 XCAST(SetProperty)JoystickAImpl_SetProperty,
981 XCAST(Acquire)JoystickAImpl_Acquire,
982 XCAST(Unacquire)JoystickAImpl_Unacquire,
983 XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
984 XCAST(GetDeviceData)JoystickAImpl_GetDeviceData,
985 XCAST(SetDataFormat)JoystickAImpl_SetDataFormat,
986 XCAST(SetEventNotification)JoystickAImpl_SetEventNotification,
987 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
988 IDirectInputDevice2WImpl_GetObjectInfo,
989 IDirectInputDevice2WImpl_GetDeviceInfo,
990 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
991 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
992 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
993 IDirectInputDevice2WImpl_EnumEffects,
994 IDirectInputDevice2WImpl_GetEffectInfo,
995 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
996 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
997 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
998 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
999 XCAST(Poll)JoystickAImpl_Poll,
1000 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1001 IDirectInputDevice7WImpl_EnumEffectsInFile,
1002 IDirectInputDevice7WImpl_WriteEffectToFile,
1003 IDirectInputDevice8WImpl_BuildActionMap,
1004 IDirectInputDevice8WImpl_SetActionMap,
1005 IDirectInputDevice8WImpl_GetImageInfo
1007 #undef XCAST
1009 #endif /* HAVE_LINUX_INPUT_H */
1011 #endif