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