- Added missing break statement.
[wine] / dlls / dinput / joystick_linuxinput.c
1 /*              DirectInput Joystick device
2  *
3  * Copyright 1998,2000 Marcus Meissner
4  * Copyright 1998,1999 Lionel Ulmer
5  * Copyright 2000-2001 TransGaming Technologies Inc.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #ifdef HAVE_LINUX_INPUT_H
26
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
46
47 #ifdef HAVE_CORRECT_LINUXINPUT_H
48
49 #ifdef HAVE_LINUX_INPUT_H
50 # include <linux/input.h>
51 #endif
52
53
54 #define EVDEVPREFIX     "/dev/input/event"
55
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"
62
63 #include "dinput_private.h"
64 #include "device_private.h"
65
66 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
67
68 /* Wine joystick driver object instances */
69 #define WINE_JOYSTICK_AXIS_BASE   0
70 #define WINE_JOYSTICK_BUTTON_BASE 8
71
72 typedef struct JoystickImpl JoystickImpl;
73 static ICOM_VTABLE(IDirectInputDevice8A) JoystickAvt;
74 static ICOM_VTABLE(IDirectInputDevice8W) JoystickWvt;
75 struct JoystickImpl
76 {
77         LPVOID                          lpVtbl;
78         DWORD                           ref;
79         GUID                            guid;
80
81
82         /* The 'parent' DInput */
83         IDirectInputImpl               *dinput;
84
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];
90
91         /* autodetecting ranges per axe by following movement */
92         LONG                            havemax[ABS_MAX];
93         LONG                            havemin[ABS_MAX];
94
95         int                             joyfd;
96
97         LPDIDATAFORMAT                  df;
98         HANDLE                          hEvent;
99         LPDIDEVICEOBJECTDATA            data_queue;
100         int                             queue_head, queue_tail, queue_len;
101         DIJOYSTATE2                     js;
102
103         /* data returned by the EVIOCGABS() ioctl */
104         int                             axes[ABS_MAX+1][5];
105
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
111
112
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];
116 };
117
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}
124 };
125
126 #define test_bit(arr,bit) (((BYTE*)arr)[bit>>3]&(1<<(bit&7)))
127
128 static int joydev_have(void)
129 {
130   int i, fd;
131   int havejoy = 0;
132
133   for (i=0;i<64;i++) {
134       char      buf[200];
135       BYTE      absbits[(ABS_MAX+7)/8],keybits[(KEY_MAX+7)/8];
136
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;
143           }
144           if (-1==ioctl(fd,EVIOCGBIT(EV_KEY,sizeof(keybits)),keybits)) {
145               perror("EVIOCGBIT EV_KEY");
146               close(fd);
147               continue;
148           }
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)
155               )
156           ) {
157               FIXME("found a joystick at %s!\n",buf);
158               havejoy = 1;
159           }
160           close(fd);
161       }
162       if (havejoy || (errno==ENODEV))
163           break;
164   }
165   return havejoy;
166 }
167
168 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, int version)
169 {
170   int havejoy = 0;
171
172   if ((dwDevType != 0) && (GET_DIDEVICE_TYPE(dwDevType) != DIDEVTYPE_JOYSTICK))
173       return FALSE;
174
175   if (dwFlags & DIEDFL_FORCEFEEDBACK)
176     return FALSE;
177
178   havejoy = joydev_have();
179
180   if (!havejoy)
181       return FALSE;
182
183   TRACE("Enumerating the linuxinput Joystick device\n");
184
185   /* Return joystick */
186   lpddi->guidInstance   = GUID_Joystick;
187   lpddi->guidProduct    = DInput_Wine_Joystick_GUID;
188
189   lpddi->guidFFDriver = GUID_NULL;
190   lpddi->dwDevType    = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL<<8);
191
192   strcpy(lpddi->tszInstanceName, "Joystick");
193   /* ioctl JSIOCGNAME(len) */
194   strcpy(lpddi->tszProductName, "Wine Joystick");
195   return TRUE;
196 }
197
198 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, int version)
199 {
200   int havejoy = 0;
201
202   if ((dwDevType != 0) && (GET_DIDEVICE_TYPE(dwDevType) != DIDEVTYPE_JOYSTICK))
203       return FALSE;
204
205   if (dwFlags & DIEDFL_FORCEFEEDBACK)
206     return FALSE;
207
208   havejoy = joydev_have();
209
210   if (!havejoy)
211       return FALSE;
212
213   TRACE("Enumerating the linuxinput Joystick device\n");
214
215   /* Return joystick */
216   lpddi->guidInstance   = GUID_Joystick;
217   lpddi->guidProduct    = DInput_Wine_Joystick_GUID;
218
219   lpddi->guidFFDriver = GUID_NULL;
220   lpddi->dwDevType    = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL<<8);
221
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;
226 }
227
228 static JoystickImpl *alloc_device(REFGUID rguid, LPVOID jvt, IDirectInputImpl *dinput)
229 {
230   JoystickImpl* newDevice;
231   int i;
232
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     /* TODO: 
243      * direct input defines a default for the deadzone somewhere; but as long
244      * as in map_axis the code for the dead zone is commented out its no
245      * problem
246      */
247     newDevice->deadz[i]   =  0;
248   }
249   return newDevice;
250 }
251
252 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
253 {
254   int havejoy = 0;
255
256   havejoy = joydev_have();
257
258   if (!havejoy)
259       return DIERR_DEVICENOTREG;
260
261   if ((IsEqualGUID(&GUID_Joystick,rguid)) ||
262       (IsEqualGUID(&DInput_Wine_Joystick_GUID,rguid))) {
263     if ((riid == NULL) ||
264         IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
265         IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
266         IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
267         IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
268       *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &JoystickAvt, dinput);
269       TRACE("Creating a Joystick device (%p)\n", *pdev);
270       return DI_OK;
271     } else
272       return DIERR_NOINTERFACE;
273   }
274
275   return DIERR_DEVICENOTREG;
276 }
277
278
279 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
280 {
281   int havejoy = 0;
282
283   havejoy = joydev_have();
284
285   if (!havejoy)
286       return DIERR_DEVICENOTREG;
287
288   if ((IsEqualGUID(&GUID_Joystick,rguid)) ||
289       (IsEqualGUID(&DInput_Wine_Joystick_GUID,rguid))) {
290     if ((riid == NULL) ||
291         IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
292         IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
293         IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
294         IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
295       *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &JoystickWvt, dinput);
296       TRACE("Creating a Joystick device (%p)\n", *pdev);
297       return DI_OK;
298     } else
299       return DIERR_NOINTERFACE;
300   }
301
302   return DIERR_DEVICENOTREG;
303 }
304
305 static dinput_device joydev = {
306   20,
307   joydev_enum_deviceA,
308   joydev_enum_deviceW,
309   joydev_create_deviceA,
310   joydev_create_deviceW
311 };
312
313 DECL_GLOBAL_CONSTRUCTOR(joydev_register) { dinput_register_device(&joydev); }
314
315 /******************************************************************************
316  *      Joystick
317  */
318 static ULONG WINAPI JoystickAImpl_Release(LPDIRECTINPUTDEVICE8A iface)
319 {
320         ICOM_THIS(JoystickImpl,iface);
321
322         This->ref--;
323         if (This->ref)
324                 return This->ref;
325
326         /* Free the data queue */
327         if (This->data_queue != NULL)
328           HeapFree(GetProcessHeap(),0,This->data_queue);
329
330         /* Free the DataFormat */
331         HeapFree(GetProcessHeap(), 0, This->df);
332
333         HeapFree(GetProcessHeap(),0,This);
334         return 0;
335 }
336
337 /******************************************************************************
338   *   SetDataFormat : the application can choose the format of the data
339   *   the device driver sends back with GetDeviceState.
340   */
341 static HRESULT WINAPI JoystickAImpl_SetDataFormat(
342         LPDIRECTINPUTDEVICE8A iface,LPCDIDATAFORMAT df
343 )
344 {
345   ICOM_THIS(JoystickImpl,iface);
346   int i;
347
348   TRACE("(this=%p,%p)\n",This,df);
349
350   TRACE("(df.dwSize=%ld)\n",df->dwSize);
351   TRACE("(df.dwObjsize=%ld)\n",df->dwObjSize);
352   TRACE("(df.dwFlags=0x%08lx)\n",df->dwFlags);
353   TRACE("(df.dwDataSize=%ld)\n",df->dwDataSize);
354   TRACE("(df.dwNumObjs=%ld)\n",df->dwNumObjs);
355
356   for (i=0;i<df->dwNumObjs;i++) {
357     TRACE("df.rgodf[%d].guid %s (%p)\n",i,debugstr_guid(df->rgodf[i].pguid), df->rgodf[i].pguid);
358     TRACE("df.rgodf[%d].dwOfs %ld\n",i,df->rgodf[i].dwOfs);
359     TRACE("dwType 0x%02x,dwInstance %d\n",DIDFT_GETTYPE(df->rgodf[i].dwType),DIDFT_GETINSTANCE(df->rgodf[i].dwType));
360     TRACE("df.rgodf[%d].dwFlags 0x%08lx\n",i,df->rgodf[i].dwFlags);
361   }
362
363   /* Store the new data format */
364   This->df = HeapAlloc(GetProcessHeap(),0,df->dwSize);
365   memcpy(This->df, df, df->dwSize);
366   This->df->rgodf = HeapAlloc(GetProcessHeap(),0,df->dwNumObjs*df->dwObjSize);
367   memcpy(This->df->rgodf,df->rgodf,df->dwNumObjs*df->dwObjSize);
368
369   return 0;
370 }
371
372 /******************************************************************************
373   *     Acquire : gets exclusive control of the joystick
374   */
375 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
376 {
377     int         i;
378     ICOM_THIS(JoystickImpl,iface);
379     char        buf[200];
380
381     TRACE("(this=%p)\n",This);
382     if (This->joyfd!=-1)
383         return 0;
384     for (i=0;i<64;i++) {
385       sprintf(buf,EVDEVPREFIX"%d",i);
386       if (-1==(This->joyfd=open(buf,O_RDONLY))) {
387         if (errno==ENODEV)
388           return DIERR_NOTFOUND;
389         perror(buf);
390         continue;
391       }
392       if ((-1!=ioctl(This->joyfd,EVIOCGBIT(EV_ABS,sizeof(This->absbits)),This->absbits)) &&
393           (-1!=ioctl(This->joyfd,EVIOCGBIT(EV_KEY,sizeof(This->keybits)),This->keybits)) &&
394           (test_bit(This->absbits,ABS_X) && test_bit(This->absbits,ABS_Y) &&
395            (test_bit(This->keybits,BTN_TRIGGER)||
396             test_bit(This->keybits,BTN_A)        ||
397             test_bit(This->keybits,BTN_1)
398           )
399          )
400       )
401         break;
402       close(This->joyfd);
403       This->joyfd = -1;
404     }
405     if (This->joyfd==-1)
406         return DIERR_NOTFOUND;
407
408     for (i=0;i<ABS_MAX;i++) {
409         if (test_bit(This->absbits,i)) {
410           if (-1==ioctl(This->joyfd,EVIOCGABS(i),&(This->axes[i])))
411             continue;
412           FIXME("axe %d: cur=%d, min=%d, max=%d, fuzz=%d, flat=%d\n",
413               i,
414               This->axes[i][AXE_ABS],
415               This->axes[i][AXE_ABSMIN],
416               This->axes[i][AXE_ABSMAX],
417               This->axes[i][AXE_ABSFUZZ],
418               This->axes[i][AXE_ABSFLAT]
419           );
420           This->havemin[i] = This->axes[i][AXE_ABSMIN];
421           This->havemax[i] = This->axes[i][AXE_ABSMAX];
422         }
423     }
424     MESSAGE("\n");
425
426     return 0;
427 }
428
429 /******************************************************************************
430   *     Unacquire : frees the joystick
431   */
432 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
433 {
434     ICOM_THIS(JoystickImpl,iface);
435
436     TRACE("(this=%p)\n",This);
437     if (This->joyfd!=-1) {
438         close(This->joyfd);
439         This->joyfd = -1;
440         return DI_OK;
441     }
442     else 
443         return DI_NOEFFECT;
444 }
445
446 /*
447  * This maps the read value (from the input event) to a value in the
448  * 'wanted' range. It also autodetects the possible range of the axe and
449  * adapts values accordingly.
450  */
451 static int
452 map_axis(JoystickImpl* This, int axis, int val) {
453     int xmid = This->axes[axis][AXE_ABS];
454     int xmin = This->axes[axis][AXE_ABSMIN];
455     int xmax = This->axes[axis][AXE_ABSMAX];
456     int hmax = This->havemax[axis];
457     int hmin = This->havemin[axis];
458     int wmin = This->wantmin[axis];
459     int wmax = This->wantmax[axis];
460     int ret;
461
462     if (val > hmax) This->havemax[axis] = hmax = val;
463     if (val < hmin) This->havemin[axis] = hmin = val;
464
465     if (xmin == xmax) return val;
466     if ((hmin == hmax) || (hmax==xmid) || (hmin==xmid)) return val;
467
468     /* map the value from the hmin-hmax range into the wmin-wmax range */
469     ret = (val * (wmax-wmin)) / (hmax-hmin) + wmin;
470
471 #if 0
472     /* deadzone doesn't work comfortably enough right now. needs more testing*/
473     if ((ret > -deadz/2 ) && (ret < deadz/2)) {
474         FIXME("%d in deadzone, return mid.\n",val);
475         return (wmax-wmin)/2+wmin;
476     }
477 #endif
478     return ret;
479 }
480
481 static void joy_polldev(JoystickImpl *This) {
482     struct timeval tv;
483     fd_set      readfds;
484     struct      input_event ie;
485
486     if (This->joyfd==-1)
487         return;
488
489     while (1) {
490         memset(&tv,0,sizeof(tv));
491         FD_ZERO(&readfds);
492         FD_SET(This->joyfd,&readfds);
493
494         if (1>select(This->joyfd+1,&readfds,NULL,NULL,&tv))
495             return;
496
497         /* we have one event, so we can read */
498         if (sizeof(ie)!=read(This->joyfd,&ie,sizeof(ie)))
499             return;
500
501         TRACE("input_event: type %d, code %d, value %d\n",ie.type,ie.code,ie.value);
502         switch (ie.type) {
503         case EV_KEY:    /* button */
504             switch (ie.code) {
505             case BTN_TRIGGER:   /* normal flight stick */
506             case BTN_A:         /* gamepad */
507             case BTN_1:         /* generic */
508                 GEN_EVENT(DIJOFS_BUTTON(0),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
509                 This->js.rgbButtons[0] = ie.value?0x80:0x00;
510                 break;
511             case BTN_THUMB:
512             case BTN_B:
513             case BTN_2:
514                 GEN_EVENT(DIJOFS_BUTTON(1),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
515                 This->js.rgbButtons[1] = ie.value?0x80:0x00;
516                 break;
517             case BTN_THUMB2:
518             case BTN_C:
519             case BTN_3:
520                 GEN_EVENT(DIJOFS_BUTTON(2),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
521                 This->js.rgbButtons[2] = ie.value?0x80:0x00;
522                 break;
523             case BTN_TOP:
524             case BTN_X:
525             case BTN_4:
526                 GEN_EVENT(DIJOFS_BUTTON(3),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
527                 This->js.rgbButtons[3] = ie.value?0x80:0x00;
528                 break;
529             case BTN_TOP2:
530             case BTN_Y:
531             case BTN_5:
532                 GEN_EVENT(DIJOFS_BUTTON(4),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
533                 This->js.rgbButtons[4] = ie.value?0x80:0x00;
534                 break;
535             case BTN_PINKIE:
536             case BTN_Z:
537             case BTN_6:
538                 GEN_EVENT(DIJOFS_BUTTON(5),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
539                 This->js.rgbButtons[5] = ie.value?0x80:0x00;
540                 break;
541             case BTN_BASE:
542             case BTN_TL:
543             case BTN_7:
544                 GEN_EVENT(DIJOFS_BUTTON(6),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
545                 This->js.rgbButtons[6] = ie.value?0x80:0x00;
546                 break;
547             case BTN_BASE2:
548             case BTN_TR:
549             case BTN_8:
550                 GEN_EVENT(DIJOFS_BUTTON(7),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
551                 This->js.rgbButtons[7] = ie.value?0x80:0x00;
552                 break;
553             case BTN_BASE3:
554             case BTN_TL2:
555             case BTN_9:
556                 GEN_EVENT(DIJOFS_BUTTON(8),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
557                 This->js.rgbButtons[8] = ie.value?0x80:0x00;
558                 break;
559             case BTN_BASE4:
560             case BTN_TR2:
561                 GEN_EVENT(DIJOFS_BUTTON(9),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
562                 This->js.rgbButtons[9] = ie.value?0x80:0x00;
563                 break;
564             case BTN_BASE5:
565             case BTN_SELECT:
566                 GEN_EVENT(DIJOFS_BUTTON(10),ie.value?0x80:0x0,ie.time.tv_usec,(This->dinput->evsequence)++);
567                 This->js.rgbButtons[10] = ie.value?0x80:0x00;
568                 break;
569             default:
570                 FIXME("unhandled joystick button %x, value %d\n",ie.code,ie.value);
571                 break;
572             }
573             break;
574         case EV_ABS:
575             switch (ie.code) {
576             case ABS_X:
577                 GEN_EVENT(DIJOFS_X,ie.value,ie.time.tv_usec,(This->dinput->evsequence)++);
578                 This->js.lX = map_axis(This,0,ie.value);
579                 break;
580             case ABS_Y:
581                 GEN_EVENT(DIJOFS_Y,ie.value,ie.time.tv_usec,(This->dinput->evsequence)++);
582                 This->js.lY = map_axis(This,1,ie.value);
583                 break;
584             case ABS_Z:
585                 GEN_EVENT(DIJOFS_Z,ie.value,ie.time.tv_usec,(This->dinput->evsequence)++);
586                 This->js.lZ = map_axis(This,2,ie.value);
587                 break;
588             case ABS_RX:
589                 GEN_EVENT(DIJOFS_RX,ie.value,ie.time.tv_usec,(This->dinput->evsequence)++);
590                 This->js.lRx = map_axis(This,3,ie.value);
591                 break;
592             case ABS_RY:
593                 GEN_EVENT(DIJOFS_RY,ie.value,ie.time.tv_usec,(This->dinput->evsequence)++);
594                 This->js.lRy = map_axis(This,4,ie.value);
595                 break;
596             case ABS_RZ:
597                 GEN_EVENT(DIJOFS_RZ,ie.value,ie.time.tv_usec,(This->dinput->evsequence)++);
598                 This->js.lRz = map_axis(This,5,ie.value);
599                 break;
600             default:
601                 FIXME("unhandled joystick axe event (code %d, value %d)\n",ie.code,ie.value);
602                 break;
603             }
604             break;
605         default:
606             FIXME("joystick cannot handle type %d event (code %d)\n",ie.type,ie.code);
607             break;
608         }
609     }
610 }
611
612 /******************************************************************************
613   *     GetDeviceState : returns the "state" of the joystick.
614   *
615   */
616 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
617         LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
618 ) {
619     ICOM_THIS(JoystickImpl,iface);
620
621     joy_polldev(This);
622
623     TRACE("(this=%p,0x%08lx,%p)\n",This,len,ptr);
624     if ((len != sizeof(DIJOYSTATE)) && (len != sizeof(DIJOYSTATE2))) {
625         FIXME("len %ld is not sizeof(DIJOYSTATE) or DIJOYSTATE2, unsupported format.\n",len);
626         return E_FAIL;
627     }
628     memcpy(ptr,&(This->js),len);
629     This->queue_head = 0;
630     This->queue_tail = 0;
631     return 0;
632 }
633
634 /******************************************************************************
635   *     GetDeviceData : gets buffered input data.
636   */
637 static HRESULT WINAPI JoystickAImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface,
638                                               DWORD dodsize,
639                                               LPDIDEVICEOBJECTDATA dod,
640                                               LPDWORD entries,
641                                               DWORD flags
642 ) {
643   ICOM_THIS(JoystickImpl,iface);
644
645   FIXME("(%p)->(dods=%ld,entries=%ld,fl=0x%08lx),STUB!\n",This,dodsize,*entries,flags);
646
647   joy_polldev(This);
648   if (flags & DIGDD_PEEK)
649     FIXME("DIGDD_PEEK\n");
650
651   if (dod == NULL) {
652   } else {
653   }
654   return 0;
655 }
656
657 /******************************************************************************
658   *     SetProperty : change input device properties
659   */
660 static HRESULT WINAPI JoystickAImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface,
661                                             REFGUID rguid,
662                                             LPCDIPROPHEADER ph)
663 {
664   ICOM_THIS(JoystickImpl,iface);
665
666   FIXME("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
667   FIXME("ph.dwSize = %ld, ph.dwHeaderSize =%ld, ph.dwObj = %ld, ph.dwHow= %ld\n",ph->dwSize, ph->dwHeaderSize,ph->dwObj,ph->dwHow);
668
669   if (!HIWORD(rguid)) {
670     switch ((DWORD)rguid) {
671     case (DWORD) DIPROP_BUFFERSIZE: {
672       LPCDIPROPDWORD    pd = (LPCDIPROPDWORD)ph;
673
674       FIXME("buffersize = %ld\n",pd->dwData);
675       break;
676     }
677     case (DWORD)DIPROP_RANGE: {
678       LPCDIPROPRANGE    pr = (LPCDIPROPRANGE)ph;
679
680       FIXME("proprange(%ld,%ld)\n",pr->lMin,pr->lMax);
681       switch (ph->dwObj) {
682       case 0:   /* X */
683       case 4:   /* Y */
684       case 8:   /* Z */
685       case 12:  /* Rx */
686       case 16:  /* Ry */
687       case 20:  /* Rz */
688           This->wantmin[ph->dwObj/4] = pr->lMin;
689           This->wantmax[ph->dwObj/4] = pr->lMax;
690           break;
691       default:
692           FIXME("setting proprange %ld - %ld for dwObj %ld\n",pr->lMin,pr->lMax,ph->dwObj);
693       }
694       break;
695     }
696     case (DWORD)DIPROP_DEADZONE: {
697       LPCDIPROPDWORD    pd = (LPCDIPROPDWORD)ph;
698
699       FIXME("setting deadzone(%ld)\n",pd->dwData);
700       This->deadz[ph->dwObj/4] = pd->dwData;
701       break;
702     }
703     default:
704       FIXME("Unknown type %ld (%s)\n",(DWORD)rguid,debugstr_guid(rguid));
705       break;
706     }
707   }
708   return 0;
709 }
710
711 /******************************************************************************
712   *     SetEventNotification : specifies event to be sent on state change
713   */
714 static HRESULT WINAPI JoystickAImpl_SetEventNotification(
715         LPDIRECTINPUTDEVICE8A iface, HANDLE hnd
716 ) {
717     ICOM_THIS(JoystickImpl,iface);
718
719     TRACE("(this=%p,0x%08lx)\n",This,(DWORD)hnd);
720     This->hEvent = hnd;
721     return DI_OK;
722 }
723
724 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
725         LPDIRECTINPUTDEVICE8A iface,
726         LPDIDEVCAPS lpDIDevCaps)
727 {
728     ICOM_THIS(JoystickImpl,iface);
729     int         xfd = This->joyfd;
730     int         i,axes,buttons;
731     int         wasacquired = 1;
732
733     TRACE("%p->(%p)\n",iface,lpDIDevCaps);
734     if (xfd==-1) {
735         /* yes, games assume we return something, even if unacquired */
736         JoystickAImpl_Acquire(iface);
737         xfd = This->joyfd;
738         wasacquired = 0;
739     }
740     lpDIDevCaps->dwFlags        = DIDC_ATTACHED;
741     lpDIDevCaps->dwDevType      = DIDEVTYPE_JOYSTICK;
742
743     axes=0;
744     for (i=0;i<ABS_MAX;i++) if (test_bit(This->absbits,i)) axes++;
745     buttons=0;
746     for (i=0;i<KEY_MAX;i++) if (test_bit(This->keybits,i)) buttons++;
747
748     lpDIDevCaps->dwAxes = axes;
749     lpDIDevCaps->dwButtons = buttons;
750
751     if (!wasacquired)
752         JoystickAImpl_Unacquire(iface);
753
754     return DI_OK;
755 }
756
757 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface) {
758     ICOM_THIS(JoystickImpl,iface);
759     TRACE("(),stub!\n");
760
761     joy_polldev(This);
762     return DI_OK;
763 }
764
765 /******************************************************************************
766   *     EnumObjects : enumerate the different buttons and axis...
767   */
768 static HRESULT WINAPI JoystickAImpl_EnumObjects(
769         LPDIRECTINPUTDEVICE8A iface,
770         LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback,
771         LPVOID lpvRef,
772         DWORD dwFlags)
773 {
774   ICOM_THIS(JoystickImpl,iface);
775   DIDEVICEOBJECTINSTANCEA ddoi;
776   int xfd = This->joyfd;
777
778
779   TRACE("(this=%p,%p,%p,%08lx)\n", This, lpCallback, lpvRef, dwFlags);
780   if (TRACE_ON(dinput)) {
781     TRACE("  - flags = ");
782     _dump_EnumObjects_flags(dwFlags);
783     TRACE("\n");
784   }
785
786   if (xfd == -1) return DIERR_NOTACQUIRED;
787
788   /* Only the fields till dwFFMaxForce are relevant */
789   ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA, dwFFMaxForce);
790
791   /* For the joystick, do as is done in the GetCapabilities function */
792   /* FIXME: needs more items */
793   if ((dwFlags == DIDFT_ALL) ||
794       (dwFlags & DIDFT_AXIS)) {
795     BYTE i;
796
797     for (i = 0; i < ABS_MAX; i++) {
798       if (!test_bit(This->absbits,i)) continue;
799
800       switch (i) {
801       case ABS_X:
802         ddoi.guidType = GUID_XAxis;
803         ddoi.dwOfs = DIJOFS_X;
804         break;
805       case ABS_Y:
806         ddoi.guidType = GUID_YAxis;
807         ddoi.dwOfs = DIJOFS_Y;
808         break;
809       case ABS_Z:
810         ddoi.guidType = GUID_ZAxis;
811         ddoi.dwOfs = DIJOFS_Z;
812         break;
813       case ABS_RX:
814         ddoi.guidType = GUID_RxAxis;
815         ddoi.dwOfs = DIJOFS_RX;
816         break;
817       case ABS_RY:
818         ddoi.guidType = GUID_RyAxis;
819         ddoi.dwOfs = DIJOFS_RY;
820         break;
821       case ABS_RZ:
822         ddoi.guidType = GUID_RzAxis;
823         ddoi.dwOfs = DIJOFS_RZ;
824         break;
825       case ABS_THROTTLE:
826         ddoi.guidType = GUID_Slider;
827         ddoi.dwOfs = DIJOFS_SLIDER(0);
828         break;
829       default:
830         FIXME("unhandled abs axis %d, ignoring!\n",i);
831       }
832       ddoi.dwType = DIDFT_MAKEINSTANCE((1<<i) << WINE_JOYSTICK_AXIS_BASE) | DIDFT_ABSAXIS;
833       sprintf(ddoi.tszName, "%d-Axis", i);
834       _dump_OBJECTINSTANCEA(&ddoi);
835       if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE)
836           return DI_OK;
837     }
838   }
839
840   if ((dwFlags == DIDFT_ALL) ||
841       (dwFlags & DIDFT_BUTTON)) {
842     int i;
843
844     /*The DInput SDK says that GUID_Button is only for mouse buttons but well*/
845
846     ddoi.guidType = GUID_Button;
847
848     for (i = 0; i < KEY_MAX; i++) {
849       if (!test_bit(This->keybits,i)) continue;
850
851       switch (i) {
852       case BTN_TRIGGER:
853       case BTN_A:
854       case BTN_1:
855           ddoi.dwOfs = DIJOFS_BUTTON(0);
856           ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 0) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
857           break;
858         case BTN_THUMB:
859         case BTN_B:
860         case BTN_2:
861           ddoi.dwOfs = DIJOFS_BUTTON(1);
862           ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 1) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
863           break;
864         case BTN_THUMB2:
865         case BTN_C:
866         case BTN_3:
867           ddoi.dwOfs = DIJOFS_BUTTON(2);
868           ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 2) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
869           break;
870         case BTN_TOP:
871         case BTN_X:
872         case BTN_4:
873           ddoi.dwOfs = DIJOFS_BUTTON(3);
874           ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 3) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
875           break;
876         case BTN_TOP2:
877         case BTN_Y:
878         case BTN_5:
879           ddoi.dwOfs = DIJOFS_BUTTON(4);
880           ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 4) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
881           break;
882         case BTN_PINKIE:
883         case BTN_Z:
884         case BTN_6:
885           ddoi.dwOfs = DIJOFS_BUTTON(5);
886           ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 5) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
887           break;
888         case BTN_BASE:
889         case BTN_TL:
890         case BTN_7:
891           ddoi.dwOfs = DIJOFS_BUTTON(6);
892           ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 6) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
893           break;
894         case BTN_BASE2:
895         case BTN_TR:
896         case BTN_8:
897           ddoi.dwOfs = DIJOFS_BUTTON(7);
898           ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 7) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
899           break;
900         case BTN_BASE3:
901         case BTN_TL2:
902         case BTN_9:
903           ddoi.dwOfs = DIJOFS_BUTTON(8);
904           ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 8) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
905           break;
906         case BTN_BASE4:
907         case BTN_TR2:
908           ddoi.dwOfs = DIJOFS_BUTTON(9);
909           ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 9) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
910           break;
911         case BTN_BASE5:
912         case BTN_SELECT:
913           ddoi.dwOfs = DIJOFS_BUTTON(10);
914           ddoi.dwType = DIDFT_MAKEINSTANCE((0x0001 << 10) << WINE_JOYSTICK_BUTTON_BASE) | DIDFT_PSHBUTTON;
915           break;
916       }
917       sprintf(ddoi.tszName, "%d-Button", i);
918       _dump_OBJECTINSTANCEA(&ddoi);
919       if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE)
920           return DI_OK;
921     }
922   }
923
924   if (xfd!=This->joyfd)
925     close(xfd);
926
927   return DI_OK;
928 }
929
930 static HRESULT WINAPI JoystickWImpl_EnumObjects(LPDIRECTINPUTDEVICE8W iface,
931                                                 LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback,
932                                                 LPVOID lpvRef,
933                                                 DWORD dwFlags)
934 {
935   ICOM_THIS(JoystickImpl,iface);
936
937   device_enumobjects_AtoWcb_data data;
938
939   data.lpCallBack = lpCallback;
940   data.lpvRef = lpvRef;
941
942   return JoystickAImpl_EnumObjects((LPDIRECTINPUTDEVICE8A) This, (LPDIENUMDEVICEOBJECTSCALLBACKA) DIEnumDevicesCallbackAtoW, (LPVOID) &data, dwFlags);
943 }
944
945 /******************************************************************************
946   *     GetProperty : get input device properties
947   */
948 static HRESULT WINAPI JoystickAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface,
949                                                 REFGUID rguid,
950                                                 LPDIPROPHEADER pdiph)
951 {
952   ICOM_THIS(JoystickImpl,iface);
953
954   TRACE("(this=%p,%s,%p): stub!\n",
955         iface, debugstr_guid(rguid), pdiph);
956
957   if (TRACE_ON(dinput))
958     _dump_DIPROPHEADER(pdiph);
959
960   if (!HIWORD(rguid)) {
961     switch ((DWORD)rguid) {
962     case (DWORD) DIPROP_BUFFERSIZE: {
963       LPDIPROPDWORD     pd = (LPDIPROPDWORD)pdiph;
964
965       TRACE(" return buffersize = %d\n",This->queue_len);
966       pd->dwData = This->queue_len;
967       break;
968     }
969
970     case (DWORD) DIPROP_RANGE: {
971       /* LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph; */
972       if ((pdiph->dwHow == DIPH_BYID) &&
973           (pdiph->dwObj & DIDFT_ABSAXIS)) {
974         /* The app is querying the current range of the axis : return the lMin and lMax values */
975         FIXME("unimplemented axis range query.\n");
976       }
977
978       break;
979     }
980
981     default:
982       FIXME("Unknown type %ld (%s)\n",(DWORD)rguid,debugstr_guid(rguid));
983       break;
984     }
985   }
986
987
988   return DI_OK;
989 }
990
991 static ICOM_VTABLE(IDirectInputDevice8A) JoystickAvt =
992 {
993         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
994         IDirectInputDevice2AImpl_QueryInterface,
995         IDirectInputDevice2AImpl_AddRef,
996         JoystickAImpl_Release,
997         JoystickAImpl_GetCapabilities,
998         JoystickAImpl_EnumObjects,
999         JoystickAImpl_GetProperty,
1000         JoystickAImpl_SetProperty,
1001         JoystickAImpl_Acquire,
1002         JoystickAImpl_Unacquire,
1003         JoystickAImpl_GetDeviceState,
1004         JoystickAImpl_GetDeviceData,
1005         JoystickAImpl_SetDataFormat,
1006         JoystickAImpl_SetEventNotification,
1007         IDirectInputDevice2AImpl_SetCooperativeLevel,
1008         IDirectInputDevice2AImpl_GetObjectInfo,
1009         IDirectInputDevice2AImpl_GetDeviceInfo,
1010         IDirectInputDevice2AImpl_RunControlPanel,
1011         IDirectInputDevice2AImpl_Initialize,
1012         IDirectInputDevice2AImpl_CreateEffect,
1013         IDirectInputDevice2AImpl_EnumEffects,
1014         IDirectInputDevice2AImpl_GetEffectInfo,
1015         IDirectInputDevice2AImpl_GetForceFeedbackState,
1016         IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1017         IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1018         IDirectInputDevice2AImpl_Escape,
1019         JoystickAImpl_Poll,
1020         IDirectInputDevice2AImpl_SendDeviceData,
1021         IDirectInputDevice7AImpl_EnumEffectsInFile,
1022         IDirectInputDevice7AImpl_WriteEffectToFile,
1023         IDirectInputDevice8AImpl_BuildActionMap,
1024         IDirectInputDevice8AImpl_SetActionMap,
1025         IDirectInputDevice8AImpl_GetImageInfo
1026 };
1027
1028 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1029 # define XCAST(fun)     (typeof(JoystickWvt.fun))
1030 #else
1031 # define XCAST(fun)     (void*)
1032 #endif
1033
1034 static ICOM_VTABLE(IDirectInputDevice8W) JoystickWvt =
1035 {
1036         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1037         IDirectInputDevice2WImpl_QueryInterface,
1038         XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1039         XCAST(Release)JoystickAImpl_Release,
1040         XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1041         JoystickWImpl_EnumObjects,
1042         XCAST(GetProperty)JoystickAImpl_GetProperty,
1043         XCAST(SetProperty)JoystickAImpl_SetProperty,
1044         XCAST(Acquire)JoystickAImpl_Acquire,
1045         XCAST(Unacquire)JoystickAImpl_Unacquire,
1046         XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1047         XCAST(GetDeviceData)JoystickAImpl_GetDeviceData,
1048         XCAST(SetDataFormat)JoystickAImpl_SetDataFormat,
1049         XCAST(SetEventNotification)JoystickAImpl_SetEventNotification,
1050         XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1051         IDirectInputDevice2WImpl_GetObjectInfo,
1052         IDirectInputDevice2WImpl_GetDeviceInfo,
1053         XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1054         XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1055         XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
1056         IDirectInputDevice2WImpl_EnumEffects,
1057         IDirectInputDevice2WImpl_GetEffectInfo,
1058         XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
1059         XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1060         XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1061         XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1062         XCAST(Poll)JoystickAImpl_Poll,
1063         XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1064         IDirectInputDevice7WImpl_EnumEffectsInFile,
1065         IDirectInputDevice7WImpl_WriteEffectToFile,
1066         IDirectInputDevice8WImpl_BuildActionMap,
1067         IDirectInputDevice8WImpl_SetActionMap,
1068         IDirectInputDevice8WImpl_GetImageInfo
1069 };
1070 #undef XCAST
1071
1072 #endif  /* HAVE_LINUX_INPUT_H */
1073
1074 #endif