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