Change GetFileTitleA/W prototype to match PSDK.
[wine/multimedia.git] / dlls / dinput / joystick_linuxinput.c
blob58f5ad692ff1f1434cdd10b2e4042719e576a26d
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 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 #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;
435 return DI_OK;
437 else
438 return DI_NOEFFECT;
442 * This maps the read value (from the input event) to a value in the
443 * 'wanted' range. It also autodetects the possible range of the axe and
444 * adapts values accordingly.
446 static int
447 map_axis(JoystickImpl* This, int axis, int val) {
448 int xmid = This->axes[axis][AXE_ABS];
449 int xmin = This->axes[axis][AXE_ABSMIN];
450 int xmax = This->axes[axis][AXE_ABSMAX];
451 int hmax = This->havemax[axis];
452 int hmin = This->havemin[axis];
453 int wmin = This->wantmin[axis];
454 int wmax = This->wantmax[axis];
455 int ret;
457 if (val > hmax) This->havemax[axis] = hmax = val;
458 if (val < hmin) This->havemin[axis] = hmin = val;
460 if (xmin == xmax) return val;
461 if ((hmin == hmax) || (hmax==xmid) || (hmin==xmid)) return val;
463 /* don't assume total linearity, but linearity starting from a zeropoint */
464 if (val > xmid) {
465 ret = (val-xmid)*((wmax-wmin)/2)/(hmax-xmid)+0;
466 } else {
467 ret = (xmid-val)*((wmax-wmin)/2)/(hmin-xmid)+0;
470 #if 0
471 /* deadzone doesn't work comfortably enough right now. needs more testing*/
472 if ((ret > -deadz/2 ) && (ret < deadz/2)) {
473 FIXME("%d in deadzone, return mid.\n",val);
474 return (wmax-wmin)/2+wmin;
476 #endif
477 return ret;
480 static void joy_polldev(JoystickImpl *This) {
481 struct timeval tv;
482 fd_set readfds;
483 struct input_event ie;
485 if (This->joyfd==-1)
486 return;
488 while (1) {
489 memset(&tv,0,sizeof(tv));
490 FD_ZERO(&readfds);
491 FD_SET(This->joyfd,&readfds);
493 if (1>select(This->joyfd+1,&readfds,NULL,NULL,&tv))
494 return;
496 /* we have one event, so we can read */
497 if (sizeof(ie)!=read(This->joyfd,&ie,sizeof(ie)))
498 return;
500 TRACE("input_event: type %d, code %d, value %d\n",ie.type,ie.code,ie.value);
501 switch (ie.type) {
502 case EV_KEY: /* button */
503 switch (ie.code) {
504 case BTN_TRIGGER: /* normal flight stick */
505 case BTN_A: /* gamepad */
506 case BTN_1: /* generic */
507 GEN_EVENT(DIJOFS_BUTTON(0),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
508 This->js.rgbButtons[0] = ie.value?0x80:0x00;
509 break;
510 case BTN_THUMB:
511 case BTN_B:
512 case BTN_2:
513 GEN_EVENT(DIJOFS_BUTTON(1),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
514 This->js.rgbButtons[1] = ie.value?0x80:0x00;
515 break;
516 case BTN_THUMB2:
517 case BTN_C:
518 case BTN_3:
519 GEN_EVENT(DIJOFS_BUTTON(2),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
520 This->js.rgbButtons[2] = ie.value?0x80:0x00;
521 break;
522 default:
523 FIXME("unhandled joystick axe %x, value %d\n",ie.code,ie.value);
524 break;
526 break;
527 case EV_ABS:
528 switch (ie.code) {
529 case ABS_X:
530 GEN_EVENT(DIJOFS_X,ie.value,ie.time.tv_usec,(This->dinput->evsequence)++);
531 This->js.lX = map_axis(This,0,ie.value);
532 break;
533 case ABS_Y:
534 GEN_EVENT(DIJOFS_Y,ie.value,ie.time.tv_usec,(This->dinput->evsequence)++);
535 This->js.lY = map_axis(This,1,ie.value);
536 case ABS_Z:
537 GEN_EVENT(DIJOFS_Z,ie.value,ie.time.tv_usec,(This->dinput->evsequence)++);
538 This->js.lZ = map_axis(This,2,ie.value);
539 break;
540 default:
541 FIXME("unhandled joystick axe event (code %d, value %d)\n",ie.code,ie.value);
542 break;
544 break;
545 default:
546 FIXME("joystick cannot handle type %d event (code %d)\n",ie.type,ie.code);
547 break;
552 /******************************************************************************
553 * GetDeviceState : returns the "state" of the joystick.
556 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
557 LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
559 ICOM_THIS(JoystickImpl,iface);
561 joy_polldev(This);
563 TRACE("(this=%p,0x%08lx,%p)\n",This,len,ptr);
564 if ((len != sizeof(DIJOYSTATE)) && (len != sizeof(DIJOYSTATE2))) {
565 FIXME("len %ld is not sizeof(DIJOYSTATE) or DIJOYSTATE2, unsupported format.\n",len);
566 return E_FAIL;
568 memcpy(ptr,&(This->js),len);
569 This->queue_head = 0;
570 This->queue_tail = 0;
571 return 0;
574 /******************************************************************************
575 * GetDeviceData : gets buffered input data.
577 static HRESULT WINAPI JoystickAImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface,
578 DWORD dodsize,
579 LPDIDEVICEOBJECTDATA dod,
580 LPDWORD entries,
581 DWORD flags
583 ICOM_THIS(JoystickImpl,iface);
585 FIXME("(%p)->(dods=%ld,entries=%ld,fl=0x%08lx),STUB!\n",This,dodsize,*entries,flags);
587 joy_polldev(This);
588 if (flags & DIGDD_PEEK)
589 FIXME("DIGDD_PEEK\n");
591 if (dod == NULL) {
592 } else {
594 return 0;
597 /******************************************************************************
598 * SetProperty : change input device properties
600 static HRESULT WINAPI JoystickAImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface,
601 REFGUID rguid,
602 LPCDIPROPHEADER ph)
604 ICOM_THIS(JoystickImpl,iface);
606 FIXME("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
607 FIXME("ph.dwSize = %ld, ph.dwHeaderSize =%ld, ph.dwObj = %ld, ph.dwHow= %ld\n",ph->dwSize, ph->dwHeaderSize,ph->dwObj,ph->dwHow);
609 if (!HIWORD(rguid)) {
610 switch ((DWORD)rguid) {
611 case (DWORD) DIPROP_BUFFERSIZE: {
612 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
614 FIXME("buffersize = %ld\n",pd->dwData);
615 break;
617 case (DWORD)DIPROP_RANGE: {
618 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
620 FIXME("proprange(%ld,%ld)\n",pr->lMin,pr->lMax);
621 switch (ph->dwObj) {
622 case 0: /* X */
623 case 4: /* Y */
624 case 8: /* Z */
625 case 12: /* Rx */
626 case 16: /* Ry */
627 case 20: /* Rz */
628 This->wantmin[ph->dwObj/4] = pr->lMin;
629 This->wantmax[ph->dwObj/4] = pr->lMax;
630 break;
631 default:
632 FIXME("setting proprange %ld - %ld for dwObj %ld\n",pr->lMin,pr->lMax,ph->dwObj);
634 break;
636 case (DWORD)DIPROP_DEADZONE: {
637 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
639 FIXME("setting deadzone(%ld)\n",pd->dwData);
640 This->deadz[ph->dwObj/4] = pd->dwData;
641 break;
643 default:
644 FIXME("Unknown type %ld (%s)\n",(DWORD)rguid,debugstr_guid(rguid));
645 break;
648 return 0;
651 /******************************************************************************
652 * SetEventNotification : specifies event to be sent on state change
654 static HRESULT WINAPI JoystickAImpl_SetEventNotification(
655 LPDIRECTINPUTDEVICE8A iface, HANDLE hnd
657 ICOM_THIS(JoystickImpl,iface);
659 TRACE("(this=%p,0x%08lx)\n",This,(DWORD)hnd);
660 This->hEvent = hnd;
661 return DI_OK;
664 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
665 LPDIRECTINPUTDEVICE8A iface,
666 LPDIDEVCAPS lpDIDevCaps)
668 ICOM_THIS(JoystickImpl,iface);
669 int xfd = This->joyfd;
670 int i,axes,buttons;
671 int wasacquired = 1;
673 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
674 if (xfd==-1) {
675 /* yes, games assume we return something, even if unacquired */
676 JoystickAImpl_Acquire(iface);
677 xfd = This->joyfd;
678 wasacquired = 0;
680 lpDIDevCaps->dwFlags = DIDC_ATTACHED;
681 lpDIDevCaps->dwDevType = DIDEVTYPE_JOYSTICK;
683 axes=0;
684 for (i=0;i<ABS_MAX;i++) if (test_bit(This->absbits,i)) axes++;
685 buttons=0;
686 for (i=0;i<KEY_MAX;i++) if (test_bit(This->keybits,i)) buttons++;
688 lpDIDevCaps->dwAxes = axes;
689 lpDIDevCaps->dwButtons = buttons;
691 if (!wasacquired)
692 JoystickAImpl_Unacquire(iface);
694 return DI_OK;
697 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface) {
698 ICOM_THIS(JoystickImpl,iface);
699 TRACE("(),stub!\n");
701 joy_polldev(This);
702 return DI_OK;
705 /******************************************************************************
706 * EnumObjects : enumerate the different buttons and axis...
708 static HRESULT WINAPI JoystickAImpl_EnumObjects(
709 LPDIRECTINPUTDEVICE8A iface,
710 LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback,
711 LPVOID lpvRef,
712 DWORD dwFlags)
714 ICOM_THIS(JoystickImpl,iface);
715 DIDEVICEOBJECTINSTANCEA ddoi;
716 int xfd = This->joyfd;
719 TRACE("(this=%p,%p,%p,%08lx)\n", This, lpCallback, lpvRef, dwFlags);
720 if (TRACE_ON(dinput)) {
721 TRACE(" - flags = ");
722 _dump_EnumObjects_flags(dwFlags);
723 TRACE("\n");
726 if (xfd == -1) return DIERR_NOTACQUIRED;
728 /* Only the fields till dwFFMaxForce are relevant */
729 ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA, dwFFMaxForce);
731 /* For the joystick, do as is done in the GetCapabilities function */
732 /* FIXME: needs more items */
733 if ((dwFlags == DIDFT_ALL) ||
734 (dwFlags & DIDFT_AXIS)) {
735 BYTE i;
737 for (i = 0; i < ABS_MAX; i++) {
738 if (!test_bit(This->absbits,i)) continue;
740 switch (i) {
741 case ABS_X:
742 ddoi.guidType = GUID_XAxis;
743 ddoi.dwOfs = DIJOFS_X;
744 break;
745 case ABS_Y:
746 ddoi.guidType = GUID_YAxis;
747 ddoi.dwOfs = DIJOFS_Y;
748 break;
749 case ABS_Z:
750 ddoi.guidType = GUID_ZAxis;
751 ddoi.dwOfs = DIJOFS_Z;
752 break;
753 case ABS_RX:
754 ddoi.guidType = GUID_RxAxis;
755 ddoi.dwOfs = DIJOFS_RX;
756 break;
757 case ABS_RY:
758 ddoi.guidType = GUID_RyAxis;
759 ddoi.dwOfs = DIJOFS_RY;
760 break;
761 case ABS_RZ:
762 ddoi.guidType = GUID_RzAxis;
763 ddoi.dwOfs = DIJOFS_RZ;
764 break;
765 case ABS_THROTTLE:
766 ddoi.guidType = GUID_Slider;
767 ddoi.dwOfs = DIJOFS_SLIDER(0);
768 break;
769 default:
770 FIXME("unhandled abs axis %d, ignoring!\n",i);
772 ddoi.dwType = DIDFT_MAKEINSTANCE((1<<i) << WINE_JOYSTICK_AXIS_BASE) | DIDFT_ABSAXIS;
773 sprintf(ddoi.tszName, "%d-Axis", i);
774 _dump_OBJECTINSTANCEA(&ddoi);
775 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE)
776 return DI_OK;
780 if ((dwFlags == DIDFT_ALL) ||
781 (dwFlags & DIDFT_BUTTON)) {
782 int i;
784 /*The DInput SDK says that GUID_Button is only for mouse buttons but well*/
786 ddoi.guidType = GUID_Button;
788 for (i = 0; i < KEY_MAX; i++) {
789 if (!test_bit(This->keybits,i)) continue;
791 switch (i) {
792 case BTN_TRIGGER:
793 case BTN_A:
794 case BTN_1:
795 ddoi.dwOfs = DIJOFS_BUTTON(0);
796 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 0) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
797 break;
798 case BTN_THUMB:
799 case BTN_B:
800 case BTN_2:
801 ddoi.dwOfs = DIJOFS_BUTTON(1);
802 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 1) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
803 break;
804 case BTN_THUMB2:
805 case BTN_C:
806 case BTN_3:
807 ddoi.dwOfs = DIJOFS_BUTTON(2);
808 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 2) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
809 break;
810 case BTN_TOP:
811 case BTN_X:
812 case BTN_4:
813 ddoi.dwOfs = DIJOFS_BUTTON(3);
814 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 3) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
815 break;
816 case BTN_TOP2:
817 case BTN_Y:
818 case BTN_5:
819 ddoi.dwOfs = DIJOFS_BUTTON(4);
820 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 4) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
821 break;
822 case BTN_PINKIE:
823 case BTN_Z:
824 case BTN_6:
825 ddoi.dwOfs = DIJOFS_BUTTON(5);
826 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 5) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
827 break;
828 case BTN_BASE:
829 case BTN_TL:
830 case BTN_7:
831 ddoi.dwOfs = DIJOFS_BUTTON(6);
832 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 6) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
833 break;
834 case BTN_BASE2:
835 case BTN_TR:
836 case BTN_8:
837 ddoi.dwOfs = DIJOFS_BUTTON(7);
838 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 7) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
839 break;
840 case BTN_BASE3:
841 case BTN_TL2:
842 case BTN_9:
843 ddoi.dwOfs = DIJOFS_BUTTON(8);
844 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 8) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
845 break;
846 case BTN_BASE4:
847 case BTN_TR2:
848 ddoi.dwOfs = DIJOFS_BUTTON(9);
849 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 9) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
850 break;
851 case BTN_BASE5:
852 case BTN_SELECT:
853 ddoi.dwOfs = DIJOFS_BUTTON(10);
854 ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 10) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
855 break;
857 sprintf(ddoi.tszName, "%d-Button", i);
858 _dump_OBJECTINSTANCEA(&ddoi);
859 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE)
860 return DI_OK;
864 if (xfd!=This->joyfd)
865 close(xfd);
867 return DI_OK;
870 static HRESULT WINAPI JoystickWImpl_EnumObjects(LPDIRECTINPUTDEVICE8W iface,
871 LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback,
872 LPVOID lpvRef,
873 DWORD dwFlags)
875 ICOM_THIS(JoystickImpl,iface);
877 device_enumobjects_AtoWcb_data data;
879 data.lpCallBack = lpCallback;
880 data.lpvRef = lpvRef;
882 return JoystickAImpl_EnumObjects((LPDIRECTINPUTDEVICE8A) This, (LPDIENUMDEVICEOBJECTSCALLBACKA) DIEnumDevicesCallbackAtoW, (LPVOID) &data, dwFlags);
885 /******************************************************************************
886 * GetProperty : get input device properties
888 static HRESULT WINAPI JoystickAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface,
889 REFGUID rguid,
890 LPDIPROPHEADER pdiph)
892 ICOM_THIS(JoystickImpl,iface);
894 TRACE("(this=%p,%s,%p): stub!\n",
895 iface, debugstr_guid(rguid), pdiph);
897 if (TRACE_ON(dinput))
898 _dump_DIPROPHEADER(pdiph);
900 if (!HIWORD(rguid)) {
901 switch ((DWORD)rguid) {
902 case (DWORD) DIPROP_BUFFERSIZE: {
903 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
905 TRACE(" return buffersize = %d\n",This->queue_len);
906 pd->dwData = This->queue_len;
907 break;
910 case (DWORD) DIPROP_RANGE: {
911 /* LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph; */
912 if ((pdiph->dwHow == DIPH_BYID) &&
913 (pdiph->dwObj & DIDFT_ABSAXIS)) {
914 /* The app is querying the current range of the axis : return the lMin and lMax values */
915 FIXME("unimplemented axis range query.\n");
918 break;
921 default:
922 FIXME("Unknown type %ld (%s)\n",(DWORD)rguid,debugstr_guid(rguid));
923 break;
928 return DI_OK;
931 static ICOM_VTABLE(IDirectInputDevice8A) JoystickAvt =
933 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
934 IDirectInputDevice2AImpl_QueryInterface,
935 IDirectInputDevice2AImpl_AddRef,
936 JoystickAImpl_Release,
937 JoystickAImpl_GetCapabilities,
938 JoystickAImpl_EnumObjects,
939 JoystickAImpl_GetProperty,
940 JoystickAImpl_SetProperty,
941 JoystickAImpl_Acquire,
942 JoystickAImpl_Unacquire,
943 JoystickAImpl_GetDeviceState,
944 JoystickAImpl_GetDeviceData,
945 JoystickAImpl_SetDataFormat,
946 JoystickAImpl_SetEventNotification,
947 IDirectInputDevice2AImpl_SetCooperativeLevel,
948 IDirectInputDevice2AImpl_GetObjectInfo,
949 IDirectInputDevice2AImpl_GetDeviceInfo,
950 IDirectInputDevice2AImpl_RunControlPanel,
951 IDirectInputDevice2AImpl_Initialize,
952 IDirectInputDevice2AImpl_CreateEffect,
953 IDirectInputDevice2AImpl_EnumEffects,
954 IDirectInputDevice2AImpl_GetEffectInfo,
955 IDirectInputDevice2AImpl_GetForceFeedbackState,
956 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
957 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
958 IDirectInputDevice2AImpl_Escape,
959 JoystickAImpl_Poll,
960 IDirectInputDevice2AImpl_SendDeviceData,
961 IDirectInputDevice7AImpl_EnumEffectsInFile,
962 IDirectInputDevice7AImpl_WriteEffectToFile,
963 IDirectInputDevice8AImpl_BuildActionMap,
964 IDirectInputDevice8AImpl_SetActionMap,
965 IDirectInputDevice8AImpl_GetImageInfo
968 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
969 # define XCAST(fun) (typeof(JoystickWvt.fun))
970 #else
971 # define XCAST(fun) (void*)
972 #endif
974 static ICOM_VTABLE(IDirectInputDevice8W) JoystickWvt =
976 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
977 IDirectInputDevice2WImpl_QueryInterface,
978 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
979 XCAST(Release)JoystickAImpl_Release,
980 XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
981 JoystickWImpl_EnumObjects,
982 XCAST(GetProperty)JoystickAImpl_GetProperty,
983 XCAST(SetProperty)JoystickAImpl_SetProperty,
984 XCAST(Acquire)JoystickAImpl_Acquire,
985 XCAST(Unacquire)JoystickAImpl_Unacquire,
986 XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
987 XCAST(GetDeviceData)JoystickAImpl_GetDeviceData,
988 XCAST(SetDataFormat)JoystickAImpl_SetDataFormat,
989 XCAST(SetEventNotification)JoystickAImpl_SetEventNotification,
990 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
991 IDirectInputDevice2WImpl_GetObjectInfo,
992 IDirectInputDevice2WImpl_GetDeviceInfo,
993 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
994 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
995 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
996 IDirectInputDevice2WImpl_EnumEffects,
997 IDirectInputDevice2WImpl_GetEffectInfo,
998 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
999 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1000 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1001 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1002 XCAST(Poll)JoystickAImpl_Poll,
1003 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1004 IDirectInputDevice7WImpl_EnumEffectsInFile,
1005 IDirectInputDevice7WImpl_WriteEffectToFile,
1006 IDirectInputDevice8WImpl_BuildActionMap,
1007 IDirectInputDevice8WImpl_SetActionMap,
1008 IDirectInputDevice8WImpl_GetImageInfo
1010 #undef XCAST
1012 #endif /* HAVE_LINUX_INPUT_H */
1014 #endif