Fixed some issues found by winapi_check.
[wine] / dlls / dinput / mouse / main.c
1 /*              DirectInput Mouse device
2  *
3  * Copyright 1998 Marcus Meissner
4  * Copyright 1998,1999 Lionel Ulmer
5  *
6  */
7
8 #include "config.h"
9 #include <string.h>
10 #ifdef HAVE_SYS_ERRNO_H
11 # include <sys/errno.h>
12 #endif
13
14 #include "debugtools.h"
15 #include "input.h"
16 #include "user.h"
17 #include "winbase.h"
18 #include "wingdi.h"
19 #include "winerror.h"
20 #include "windef.h"
21 #include "mouse.h"
22 #include "winuser.h"
23 #include "dinput.h"
24
25 #include "dinput_private.h"
26 #include "device_private.h"
27
28 #define MOUSE_HACK
29
30 DEFAULT_DEBUG_CHANNEL(dinput);
31
32 /* Wine mouse driver object instances */
33 #define WINE_MOUSE_X_AXIS_INSTANCE 0x0001
34 #define WINE_MOUSE_Y_AXIS_INSTANCE 0x0002
35 #define WINE_MOUSE_L_BUTTON_INSTANCE 0x0004
36 #define WINE_MOUSE_R_BUTTON_INSTANCE 0x0008
37 #define WINE_MOUSE_M_BUTTON_INSTANCE 0x0010
38
39 /* ------------------------------- */
40 /* Wine mouse internal data format */
41 /* ------------------------------- */
42
43 /* Constants used to access the offset array */
44 #define WINE_MOUSE_X_POSITION 0
45 #define WINE_MOUSE_Y_POSITION 1
46 #define WINE_MOUSE_L_POSITION 2
47 #define WINE_MOUSE_R_POSITION 3
48 #define WINE_MOUSE_M_POSITION 4
49
50 typedef struct {
51   LONG lX;
52   LONG lY;
53   BYTE rgbButtons[4];
54 } Wine_InternalMouseData;
55
56 #define WINE_INTERNALMOUSE_NUM_OBJS 5
57
58 static DIOBJECTDATAFORMAT Wine_InternalMouseObjectFormat[WINE_INTERNALMOUSE_NUM_OBJS] = {
59   { &GUID_XAxis,   FIELD_OFFSET(Wine_InternalMouseData, lX),
60       DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS, 0 },
61   { &GUID_YAxis,   FIELD_OFFSET(Wine_InternalMouseData, lY),
62       DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS, 0 },
63   { &GUID_Button, (FIELD_OFFSET(Wine_InternalMouseData, rgbButtons)) + 0,
64       DIDFT_MAKEINSTANCE(WINE_MOUSE_L_BUTTON_INSTANCE) | DIDFT_PSHBUTTON, 0 },
65   { &GUID_Button, (FIELD_OFFSET(Wine_InternalMouseData, rgbButtons)) + 1,
66       DIDFT_MAKEINSTANCE(WINE_MOUSE_R_BUTTON_INSTANCE) | DIDFT_PSHBUTTON, 0 },
67   { &GUID_Button, (FIELD_OFFSET(Wine_InternalMouseData, rgbButtons)) + 2,
68       DIDFT_MAKEINSTANCE(WINE_MOUSE_M_BUTTON_INSTANCE) | DIDFT_PSHBUTTON, 0 }
69 };
70
71 static DIDATAFORMAT Wine_InternalMouseFormat = {
72   0, /* dwSize - unused */
73   0, /* dwObjsize - unused */
74   0, /* dwFlags - unused */
75   sizeof(Wine_InternalMouseData),
76   WINE_INTERNALMOUSE_NUM_OBJS, /* dwNumObjs */
77   Wine_InternalMouseObjectFormat
78 };
79
80 static ICOM_VTABLE(IDirectInputDevice2A) SysMouseAvt;
81 static ICOM_VTABLE(IDirectInputDevice7A) SysMouse7Avt;
82 typedef struct SysMouseAImpl SysMouseAImpl;
83
84 typedef enum {
85   WARP_NEEDED,  /* Warping is needed */
86   WARP_STARTED, /* Warping has been done, waiting for the warp event */
87   WARP_DONE     /* Warping has been done */
88 } WARP_STATUS;
89
90 struct SysMouseAImpl
91 {
92         /* IDirectInputDevice2AImpl */
93         ICOM_VFIELD(IDirectInputDevice2A);
94         DWORD                           ref;
95         GUID                            guid;
96
97         IDirectInputAImpl *dinput;
98         
99         /* The current data format and the conversion between internal
100            and external data formats */
101         LPDIDATAFORMAT                  df;
102         DataFormat                     *wine_df;
103         int                             offset_array[5];
104         
105         /* SysMouseAImpl */
106         BYTE                            absolute;
107         /* Previous position for relative moves */
108         LONG                            prevX, prevY;
109         LPMOUSE_EVENT_PROC              prev_handler;
110         HWND                            win;
111         DWORD                           win_centerX, win_centerY;
112         LPDIDEVICEOBJECTDATA            data_queue;
113         int                             queue_pos, queue_len;
114         WARP_STATUS                     need_warp;
115         int                             acquired;
116         HANDLE                          hEvent;
117         CRITICAL_SECTION                crit;
118
119         /* This is for mouse reporting. */
120         Wine_InternalMouseData          m_state;
121 };
122
123 static GUID DInput_Wine_Mouse_GUID = { /* 9e573ed8-7734-11d2-8d4a-23903fb6bdf7 */
124   0x9e573ed8,
125   0x7734,
126   0x11d2,
127   {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
128 };
129
130 /* FIXME: This is ugly and not thread safe :/ */
131 static IDirectInputDevice2A* current_lock = NULL;
132
133
134 static BOOL mousedev_enum_device(DWORD dwDevType, DWORD dwFlags, LPCDIDEVICEINSTANCEA lpddi)
135 {
136   if ((dwDevType == 0) || (dwDevType == DIDEVTYPE_MOUSE)) {
137     TRACE("Enumerating the Mouse device\n");
138
139     /* Return mouse */
140     lpddi->guidInstance = GUID_SysMouse;/* DInput's GUID */
141     lpddi->guidProduct = DInput_Wine_Mouse_GUID; /* Vendor's GUID */
142     lpddi->dwDevType = DIDEVTYPE_MOUSE | (DIDEVTYPEMOUSE_UNKNOWN << 8);
143     strcpy(lpddi->tszInstanceName, "Mouse");
144     strcpy(lpddi->tszProductName, "Wine Mouse");
145
146     return TRUE;
147   }
148
149   return FALSE;
150 }
151
152 static SysMouseAImpl *alloc_device(REFGUID rguid, ICOM_VTABLE(IDirectInputDevice2A) *mvt, IDirectInputAImpl *dinput)
153 {
154     int offset_array[5] = {
155       FIELD_OFFSET(Wine_InternalMouseData, lX),
156       FIELD_OFFSET(Wine_InternalMouseData, lY),
157       FIELD_OFFSET(Wine_InternalMouseData, rgbButtons) + 0,
158       FIELD_OFFSET(Wine_InternalMouseData, rgbButtons) + 1,
159       FIELD_OFFSET(Wine_InternalMouseData, rgbButtons) + 2
160     };
161     SysMouseAImpl* newDevice;
162     newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SysMouseAImpl));
163     newDevice->ref = 1;
164     ICOM_VTBL(newDevice) = mvt;
165     InitializeCriticalSection(&(newDevice->crit));
166     memcpy(&(newDevice->guid),rguid,sizeof(*rguid));
167     
168     /* Per default, Wine uses its internal data format */
169     newDevice->df = &Wine_InternalMouseFormat;
170     memcpy(newDevice->offset_array, offset_array, 5 * sizeof(int));
171     newDevice->wine_df = (DataFormat *) HeapAlloc(GetProcessHeap(), 0, sizeof(DataFormat));
172     newDevice->wine_df->size = 0;
173     newDevice->wine_df->internal_format_size = Wine_InternalMouseFormat.dwDataSize;
174     newDevice->wine_df->dt = NULL;
175     newDevice->dinput = dinput;
176
177     return newDevice;
178 }
179
180 static HRESULT mousedev_create_device(IDirectInputAImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
181 {
182   if ((IsEqualGUID(&GUID_SysMouse,rguid)) ||             /* Generic Mouse */
183       (IsEqualGUID(&DInput_Wine_Mouse_GUID,rguid))) { /* Wine Mouse */
184     if ((riid == NULL) || (IsEqualGUID(&IID_IDirectInputDevice2A,riid)) || (IsEqualGUID(&IID_IDirectInputDevice2A,riid))) {
185       *pdev=(IDirectInputDeviceA*) alloc_device(rguid, &SysMouseAvt, dinput);
186     
187       TRACE("Creating a Mouse device (%p)\n", *pdev);
188       return DI_OK;
189     }else if (IsEqualGUID(&IID_IDirectInputDevice7A,riid)) {
190       *pdev=(IDirectInputDeviceA*) alloc_device(rguid, (ICOM_VTABLE(IDirectInputDevice2A) *) &SysMouse7Avt, dinput);
191     
192       TRACE("Creating a Mouse DInput7A device (%p)\n", *pdev);
193       return DI_OK;
194     } else
195       return DIERR_NOINTERFACE;
196   }
197
198   return DIERR_DEVICENOTREG;
199 }
200
201 static dinput_device mousedev = {
202   100,
203   mousedev_enum_device,
204   mousedev_create_device
205 };
206
207 DECL_GLOBAL_CONSTRUCTOR(mousedev_register) { dinput_register_device(&mousedev); }
208
209 /******************************************************************************
210  *      SysMouseA (DInput Mouse support)
211  */
212
213 /******************************************************************************
214   *     Release : release the mouse buffer.
215   */
216 static ULONG WINAPI SysMouseAImpl_Release(LPDIRECTINPUTDEVICE2A iface)
217 {
218         ICOM_THIS(SysMouseAImpl,iface);
219
220         This->ref--;
221         if (This->ref)
222                 return This->ref;
223
224         /* Free the data queue */
225         if (This->data_queue != NULL)
226           HeapFree(GetProcessHeap(),0,This->data_queue);
227
228         /* Install the previous event handler (in case of releasing an acquired
229            mouse device) */
230         if (This->prev_handler != NULL)
231           MOUSE_Enable(This->prev_handler);
232         DeleteCriticalSection(&(This->crit));
233
234         /* Free the DataFormat */
235         if (This->df != &(Wine_InternalMouseFormat)) {
236           HeapFree(GetProcessHeap(), 0, This->df->rgodf);
237           HeapFree(GetProcessHeap(), 0, This->df);
238         }
239         
240         HeapFree(GetProcessHeap(),0,This);
241         return 0;
242 }
243
244
245 /******************************************************************************
246   *     SetCooperativeLevel : store the window in which we will do our
247   *   grabbing.
248   */
249 static HRESULT WINAPI SysMouseAImpl_SetCooperativeLevel(
250         LPDIRECTINPUTDEVICE2A iface,HWND hwnd,DWORD dwflags
251 )
252 {
253   ICOM_THIS(SysMouseAImpl,iface);
254
255   TRACE("(this=%p,0x%08lx,0x%08lx)\n",This,(DWORD)hwnd,dwflags);
256
257   if (TRACE_ON(dinput))
258     _dump_cooperativelevel_DI(dwflags);
259
260   /* Store the window which asks for the mouse */
261   This->win = hwnd;
262   
263   return 0;
264 }
265
266
267 /******************************************************************************
268   *     SetDataFormat : the application can choose the format of the data
269   *   the device driver sends back with GetDeviceState.
270   *
271   *   For the moment, only the "standard" configuration (c_dfDIMouse) is supported
272   *   in absolute and relative mode.
273   */
274 static HRESULT WINAPI SysMouseAImpl_SetDataFormat(
275         LPDIRECTINPUTDEVICE2A iface,LPCDIDATAFORMAT df
276 )
277 {
278   ICOM_THIS(SysMouseAImpl,iface);
279   int i;
280   
281   TRACE("(this=%p,%p)\n",This,df);
282
283   TRACE("(df.dwSize=%ld)\n",df->dwSize);
284   TRACE("(df.dwObjsize=%ld)\n",df->dwObjSize);
285   TRACE("(df.dwFlags=0x%08lx)\n",df->dwFlags);
286   TRACE("(df.dwDataSize=%ld)\n",df->dwDataSize);
287   TRACE("(df.dwNumObjs=%ld)\n",df->dwNumObjs);
288
289   for (i=0;i<df->dwNumObjs;i++) {
290
291     TRACE("df.rgodf[%d].guid %s (%p)\n",i, debugstr_guid(df->rgodf[i].pguid), df->rgodf[i].pguid);
292     TRACE("df.rgodf[%d].dwOfs %ld\n",i,df->rgodf[i].dwOfs);
293     TRACE("dwType 0x%02x,dwInstance %d\n",DIDFT_GETTYPE(df->rgodf[i].dwType),DIDFT_GETINSTANCE(df->rgodf[i].dwType));
294     TRACE("df.rgodf[%d].dwFlags 0x%08lx\n",i,df->rgodf[i].dwFlags);
295   }
296
297   /* Check if the mouse is in absolute or relative mode */
298   if (df->dwFlags == DIDF_ABSAXIS)
299     This->absolute = 1;
300   else if (df->dwFlags == DIDF_RELAXIS)
301     This->absolute = 0;
302   else
303     ERR("Neither absolute nor relative flag set.");
304   
305   /* Store the new data format */
306   This->df = HeapAlloc(GetProcessHeap(),0,df->dwSize);
307   memcpy(This->df, df, df->dwSize);
308   This->df->rgodf = HeapAlloc(GetProcessHeap(),0,df->dwNumObjs*df->dwObjSize);
309   memcpy(This->df->rgodf,df->rgodf,df->dwNumObjs*df->dwObjSize);
310
311   /* Prepare all the data-conversion filters */
312   This->wine_df = create_DataFormat(&(Wine_InternalMouseFormat), df, This->offset_array);
313   
314   return 0;
315 }
316   
317 /* Our private mouse event handler */
318 static void WINAPI dinput_mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
319                                       DWORD cButtons, DWORD dwExtraInfo )
320 {
321   long posX = -1, posY = -1;
322   DWORD keyState, xtime, extra;
323   SysMouseAImpl* This = (SysMouseAImpl*) current_lock;
324   
325   EnterCriticalSection(&(This->crit));
326   /* Mouse moved -> send event if asked */
327   if (This->hEvent)
328     SetEvent(This->hEvent);
329   
330   if (   !IsBadReadPtr( (LPVOID)dwExtraInfo, sizeof(WINE_MOUSEEVENT) )
331       && ((WINE_MOUSEEVENT *)dwExtraInfo)->magic == WINE_MOUSEEVENT_MAGIC ) {
332     WINE_MOUSEEVENT *wme = (WINE_MOUSEEVENT *)dwExtraInfo;
333     keyState = wme->keyState;
334     xtime = wme->time;
335     extra = (DWORD)wme->hWnd;
336     
337     if (dwFlags & MOUSEEVENTF_MOVE) {
338       if (dwFlags & MOUSEEVENTF_ABSOLUTE) {
339         posX = (dx * GetSystemMetrics(SM_CXSCREEN)) >> 16;
340         posY = (dy * GetSystemMetrics(SM_CYSCREEN)) >> 16;
341
342         if (This->absolute) {
343           if (posX != This->prevX)
344             GEN_EVENT(This->offset_array[WINE_MOUSE_X_POSITION], posX, xtime, 0);
345           if (posY != This->prevY)
346             GEN_EVENT(This->offset_array[WINE_MOUSE_Y_POSITION], posY, xtime, 0);
347         } else {
348           /* Now, warp handling */
349           if ((This->need_warp == WARP_STARTED) &&
350               (posX == This->win_centerX) && (posX == This->win_centerX)) {
351             /* Warp has been done... */
352             This->need_warp = WARP_DONE;
353             LeaveCriticalSection(&(This->crit));
354             return;
355           }
356                   
357           /* Relative mouse input with absolute mouse event : the real fun starts here... */
358           if ((This->need_warp == WARP_NEEDED) ||
359               (This->need_warp == WARP_STARTED)) {
360             if (posX != This->prevX)
361               GEN_EVENT(This->offset_array[WINE_MOUSE_X_POSITION], posX - This->prevX, xtime, (This->dinput->evsequence)++);
362             if (posY != This->prevY)
363               GEN_EVENT(This->offset_array[WINE_MOUSE_Y_POSITION], posY - This->prevY, xtime, (This->dinput->evsequence)++);
364           } else {
365             /* This is the first time the event handler has been called after a
366                GetData of GetState. */
367             if (posX != This->win_centerX) {
368               GEN_EVENT(This->offset_array[WINE_MOUSE_X_POSITION], posX - This->win_centerX, xtime, (This->dinput->evsequence)++);
369               This->need_warp = WARP_NEEDED;
370             }
371             
372             if (posY != This->win_centerY) {
373               GEN_EVENT(This->offset_array[WINE_MOUSE_Y_POSITION], posY - This->win_centerY, xtime, (This->dinput->evsequence)++);
374               This->need_warp = WARP_NEEDED;
375             }
376           }
377         }
378
379         This->prevX = posX;
380         This->prevY = posY;
381         
382         if (This->absolute) {
383           This->m_state.lX = posX;
384           This->m_state.lY = posY;
385         } else {
386           This->m_state.lX = posX - This->win_centerX;
387           This->m_state.lY = posY - This->win_centerY;
388         }
389       } else {
390         /* Mouse reporting is in relative mode */
391         posX = (long) dx;
392         posY = (long) dy;
393
394         if (This->absolute) {
395           long aposX, aposY;
396           
397           aposX = This->m_state.lX + posX;
398           if (aposX < 0)
399             aposX = 0;
400           if (aposX >= GetSystemMetrics(SM_CXSCREEN))
401             aposX = GetSystemMetrics(SM_CXSCREEN);
402           
403           aposY = This->m_state.lY + posY;
404           if (aposY < 0)
405             aposY = 0;
406           if (aposY >= GetSystemMetrics(SM_CYSCREEN))
407             aposY = GetSystemMetrics(SM_CYSCREEN);
408           
409           if (posX != 0)
410             GEN_EVENT(This->offset_array[WINE_MOUSE_X_POSITION], aposX, xtime, (This->dinput->evsequence)++);
411           if (posY != 0)
412             GEN_EVENT(This->offset_array[WINE_MOUSE_Y_POSITION], aposY, xtime, (This->dinput->evsequence)++);
413
414           This->m_state.lX = aposX;
415           This->m_state.lY = aposY;
416         } else {
417           if (posX != 0)
418             GEN_EVENT(This->offset_array[WINE_MOUSE_X_POSITION], posX, xtime, (This->dinput->evsequence)++);
419           if (posY != 0)
420             GEN_EVENT(This->offset_array[WINE_MOUSE_Y_POSITION], posY, xtime, (This->dinput->evsequence)++);
421
422           This->m_state.lX = posX;
423           This->m_state.lY = posY;
424         }
425       }
426     }
427   } else {
428     ERR("Mouse event not supported...\n");
429     LeaveCriticalSection(&(This->crit));
430     return ;
431   }
432
433   if (TRACE_ON(dinput)) {
434     if (dwFlags & MOUSEEVENTF_MOVE)
435       TRACE(" %ld %ld (%s)", posX, posY,
436             (dwFlags & MOUSEEVENTF_ABSOLUTE ? "abs" : "rel"));
437     
438     if ( dwFlags & MOUSEEVENTF_LEFTDOWN ) DPRINTF(" LD ");
439     if ( dwFlags & MOUSEEVENTF_LEFTUP )   DPRINTF(" LU ");
440
441     if ( dwFlags & MOUSEEVENTF_RIGHTDOWN ) DPRINTF(" RD ");
442     if ( dwFlags & MOUSEEVENTF_RIGHTUP )   DPRINTF(" RU ");
443
444     if ( dwFlags & MOUSEEVENTF_MIDDLEDOWN ) DPRINTF(" MD ");
445     if ( dwFlags & MOUSEEVENTF_MIDDLEUP )   DPRINTF(" MU ");
446
447     if (!(This->absolute)) DPRINTF(" W=%d ", This->need_warp);
448     
449     DPRINTF("\n");
450   }
451
452   
453   if ( dwFlags & MOUSEEVENTF_LEFTDOWN ) {
454     GEN_EVENT(This->offset_array[WINE_MOUSE_L_POSITION], 0xFF, xtime, (This->dinput->evsequence)++);
455     This->m_state.rgbButtons[0] = 0xFF;
456   }
457   if ( dwFlags & MOUSEEVENTF_LEFTUP ) {
458     GEN_EVENT(This->offset_array[WINE_MOUSE_L_POSITION], 0x00, xtime, (This->dinput->evsequence)++);
459     This->m_state.rgbButtons[0] = 0x00;
460   }
461   if ( dwFlags & MOUSEEVENTF_RIGHTDOWN ) {
462     GEN_EVENT(This->offset_array[WINE_MOUSE_R_POSITION], 0xFF, xtime, (This->dinput->evsequence)++);
463     This->m_state.rgbButtons[1] = 0xFF;
464   }
465   if ( dwFlags & MOUSEEVENTF_RIGHTUP ) {
466     GEN_EVENT(This->offset_array[WINE_MOUSE_R_POSITION], 0x00, xtime, (This->dinput->evsequence)++);
467     This->m_state.rgbButtons[1] = 0x00;
468   }
469   if ( dwFlags & MOUSEEVENTF_MIDDLEDOWN ) {
470     GEN_EVENT(This->offset_array[WINE_MOUSE_M_POSITION], 0xFF, xtime, (This->dinput->evsequence)++);
471     This->m_state.rgbButtons[2] = 0xFF;
472   }
473   if ( dwFlags & MOUSEEVENTF_MIDDLEUP ) {
474     GEN_EVENT(This->offset_array[WINE_MOUSE_M_POSITION], 0x00, xtime, (This->dinput->evsequence)++);
475     This->m_state.rgbButtons[2] = 0x00;
476   }
477
478   TRACE("(X: %ld - Y: %ld   L: %02x M: %02x R: %02x)\n",
479         This->m_state.lX, This->m_state.lY,
480         This->m_state.rgbButtons[0], This->m_state.rgbButtons[2], This->m_state.rgbButtons[1]);
481   
482   LeaveCriticalSection(&(This->crit));
483 }
484
485
486 /******************************************************************************
487   *     Acquire : gets exclusive control of the mouse
488   */
489 static HRESULT WINAPI SysMouseAImpl_Acquire(LPDIRECTINPUTDEVICE2A iface)
490 {
491   ICOM_THIS(SysMouseAImpl,iface);
492   RECT  rect;
493   
494   TRACE("(this=%p)\n",This);
495
496   if (This->acquired == 0) {
497     POINT       point;
498     
499     /* This stores the current mouse handler. */
500     This->prev_handler = mouse_event;
501     
502     /* Store (in a global variable) the current lock */
503     current_lock = (IDirectInputDevice2A*)This;
504
505     /* Init the mouse state */
506     if (This->absolute) {
507       GetCursorPos( &point );
508       This->m_state.lX = point.x;
509       This->m_state.lY = point.y;
510       This->prevX = point.x;
511       This->prevY = point.y;
512     } else {
513       This->m_state.lX = 0;
514       This->m_state.lY = 0;
515     }
516     This->m_state.rgbButtons[0] = (GetKeyState(VK_LBUTTON) ? 0xFF : 0x00);
517     This->m_state.rgbButtons[1] = (GetKeyState(VK_MBUTTON) ? 0xFF : 0x00);
518     This->m_state.rgbButtons[2] = (GetKeyState(VK_RBUTTON) ? 0xFF : 0x00);
519
520     /* Install our own mouse event handler */
521     MOUSE_Enable(dinput_mouse_event);
522     
523     /* Get the window dimension and find the center */
524     GetWindowRect(This->win, &rect);
525     This->win_centerX = (rect.right  - rect.left) / 2;
526     This->win_centerY = (rect.bottom - rect.top ) / 2;
527
528     /* Warp the mouse to the center of the window */
529     if (This->absolute == 0) {
530       TRACE("Warping mouse to %ld - %ld\n", This->win_centerX, This->win_centerY);
531       point.x = This->win_centerX;
532       point.y = This->win_centerY;
533       MapWindowPoints(This->win, HWND_DESKTOP, &point, 1);
534       SetCursorPos( point.x, point.y );
535 #ifdef MOUSE_HACK
536       This->need_warp = WARP_DONE;
537 #else
538       This->need_warp = WARP_STARTED;
539 #endif
540     }
541
542     This->acquired = 1;
543   }
544   return DI_OK;
545 }
546
547 /******************************************************************************
548   *     Unacquire : frees the mouse
549   */
550 static HRESULT WINAPI SysMouseAImpl_Unacquire(LPDIRECTINPUTDEVICE2A iface)
551 {
552     ICOM_THIS(SysMouseAImpl,iface);
553
554     TRACE("(this=%p)\n",This);
555
556     if (This->acquired)
557     {
558         /* Reinstall previous mouse event handler */
559         MOUSE_Enable(This->prev_handler);
560         This->prev_handler = NULL;
561   
562         /* No more locks */
563         current_lock = NULL;
564
565         /* Unacquire device */
566         This->acquired = 0;
567     }
568     else
569         ERR("Unacquiring a not-acquired device !!!\n");
570   
571     return DI_OK;
572 }
573
574 /******************************************************************************
575   *     GetDeviceState : returns the "state" of the mouse.
576   *
577   *   For the moment, only the "standard" return structure (DIMOUSESTATE) is
578   *   supported.
579   */
580 static HRESULT WINAPI SysMouseAImpl_GetDeviceState(
581         LPDIRECTINPUTDEVICE2A iface,DWORD len,LPVOID ptr
582 ) {
583   ICOM_THIS(SysMouseAImpl,iface);
584   
585   EnterCriticalSection(&(This->crit));
586   TRACE("(this=%p,0x%08lx,%p): \n",This,len,ptr);
587   
588   /* Copy the current mouse state */
589   fill_DataFormat(ptr, &(This->m_state), This->wine_df);
590   
591   /* Initialize the buffer when in relative mode */
592   if (This->absolute == 0) {
593     This->m_state.lX = 0;
594     This->m_state.lY = 0;
595   }
596   
597   /* Check if we need to do a mouse warping */
598   if (This->need_warp == WARP_NEEDED) {
599     POINT point;
600
601     TRACE("Warping mouse to %ld - %ld\n", This->win_centerX, This->win_centerY);
602     point.x = This->win_centerX;
603     point.y = This->win_centerY;
604     MapWindowPoints(This->win, HWND_DESKTOP, &point, 1);
605     SetCursorPos( point.x, point.y );
606
607 #ifdef MOUSE_HACK
608     This->need_warp = WARP_DONE;
609 #else
610     This->need_warp = WARP_STARTED;
611 #endif
612   }
613
614   LeaveCriticalSection(&(This->crit));
615   
616   TRACE("(X: %ld - Y: %ld   L: %02x M: %02x R: %02x)\n",
617         This->m_state.lX, This->m_state.lY,
618         This->m_state.rgbButtons[0], This->m_state.rgbButtons[2], This->m_state.rgbButtons[1]);
619   
620   return 0;
621 }
622
623 /******************************************************************************
624   *     GetDeviceState : gets buffered input data.
625   */
626 static HRESULT WINAPI SysMouseAImpl_GetDeviceData(LPDIRECTINPUTDEVICE2A iface,
627                                               DWORD dodsize,
628                                               LPDIDEVICEOBJECTDATA dod,
629                                               LPDWORD entries,
630                                               DWORD flags
631 ) {
632   ICOM_THIS(SysMouseAImpl,iface);
633   
634   EnterCriticalSection(&(This->crit));
635   TRACE("(%p)->(dods=%ld,entries=%ld,fl=0x%08lx)\n",This,dodsize,*entries,flags);
636
637   if (flags & DIGDD_PEEK)
638     FIXME("DIGDD_PEEK\n");
639
640   if (dod == NULL) {
641     *entries = This->queue_pos;
642     This->queue_pos = 0;
643   } else {
644     /* Check for buffer overflow */
645     if (This->queue_pos > *entries) {
646       WARN("Buffer overflow not handled properly yet...\n");
647       This->queue_pos = *entries;
648     }
649     if (dodsize != sizeof(DIDEVICEOBJECTDATA)) {
650       ERR("Wrong structure size !\n");
651       LeaveCriticalSection(&(This->crit));
652       return DIERR_INVALIDPARAM;
653     }
654
655     if (This->queue_pos)
656         TRACE("Application retrieving %d event(s).\n", This->queue_pos); 
657     
658     /* Copy the buffered data into the application queue */
659     memcpy(dod, This->data_queue, This->queue_pos * dodsize);
660     *entries = This->queue_pos;
661
662     /* Reset the event queue */
663     This->queue_pos = 0;
664   }
665   LeaveCriticalSection(&(This->crit));
666   
667   /* Check if we need to do a mouse warping */
668   if (This->need_warp == WARP_NEEDED) {
669     POINT point;
670
671     TRACE("Warping mouse to %ld - %ld\n", This->win_centerX, This->win_centerY);
672     point.x = This->win_centerX;
673     point.y = This->win_centerY;
674     MapWindowPoints(This->win, HWND_DESKTOP, &point, 1);
675     SetCursorPos( point.x, point.y );
676
677 #ifdef MOUSE_HACK
678     This->need_warp = WARP_DONE;
679 #else
680     This->need_warp = WARP_STARTED;
681 #endif
682   }
683   return 0;
684 }
685
686 /******************************************************************************
687   *     SetProperty : change input device properties
688   */
689 static HRESULT WINAPI SysMouseAImpl_SetProperty(LPDIRECTINPUTDEVICE2A iface,
690                                             REFGUID rguid,
691                                             LPCDIPROPHEADER ph)
692 {
693   ICOM_THIS(SysMouseAImpl,iface);
694
695   TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
696   
697   if (!HIWORD(rguid)) {
698     switch ((DWORD)rguid) {
699     case (DWORD) DIPROP_BUFFERSIZE: {
700       LPCDIPROPDWORD    pd = (LPCDIPROPDWORD)ph;
701       
702       TRACE("buffersize = %ld\n",pd->dwData);
703
704       This->data_queue = (LPDIDEVICEOBJECTDATA)HeapAlloc(GetProcessHeap(),0,
705                                                           pd->dwData * sizeof(DIDEVICEOBJECTDATA));
706       This->queue_pos  = 0;
707       This->queue_len  = pd->dwData;
708       break;
709     }
710     case (DWORD) DIPROP_AXISMODE: {
711       LPCDIPROPDWORD    pd = (LPCDIPROPDWORD)ph;
712       This->absolute = !(pd->dwData);
713       TRACE("absolute mode: %d\n", This->absolute);
714       break;
715     }
716     default:
717       FIXME("Unknown type %ld (%s)\n",(DWORD)rguid,debugstr_guid(rguid));
718       break;
719     }
720   }
721   
722   return 0;
723 }
724
725 /******************************************************************************
726   *     GetProperty : get input device properties
727   */
728 static HRESULT WINAPI SysMouseAImpl_GetProperty(LPDIRECTINPUTDEVICE2A iface,
729                                                 REFGUID rguid,
730                                                 LPDIPROPHEADER pdiph)
731 {
732   ICOM_THIS(SysMouseAImpl,iface);
733
734   TRACE("(this=%p,%s,%p): stub!\n",
735         iface, debugstr_guid(rguid), pdiph);
736
737   if (TRACE_ON(dinput))
738     _dump_DIPROPHEADER(pdiph);
739   
740   if (!HIWORD(rguid)) {
741     switch ((DWORD)rguid) {
742     case (DWORD) DIPROP_BUFFERSIZE: {
743       LPDIPROPDWORD     pd = (LPDIPROPDWORD)pdiph;
744       
745       TRACE(" return buffersize = %d\n",This->queue_len);
746       pd->dwData = This->queue_len;
747       break;
748     }
749
750     case (DWORD) DIPROP_RANGE: {
751       LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph;
752
753       if ((pdiph->dwHow == DIPH_BYID) &&
754           ((pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS)) ||
755            (pdiph->dwObj == (DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS)))) {
756         /* Querying the range of either the X or the Y axis.  As I do
757            not know the range, do as if the range where
758            unrestricted...*/
759         pr->lMin = DIPROPRANGE_NOMIN;
760         pr->lMax = DIPROPRANGE_NOMAX;
761       }
762       
763       break;
764     }
765       
766     default:
767       FIXME("Unknown type %ld (%s)\n",(DWORD)rguid,debugstr_guid(rguid));
768       break;
769     }
770   }
771   
772   
773   return DI_OK;
774 }
775
776
777
778 /******************************************************************************
779   *     SetEventNotification : specifies event to be sent on state change
780   */
781 static HRESULT WINAPI SysMouseAImpl_SetEventNotification(LPDIRECTINPUTDEVICE2A iface,
782                                                          HANDLE hnd) {
783   ICOM_THIS(SysMouseAImpl,iface);
784
785   TRACE("(this=%p,0x%08lx)\n",This,(DWORD)hnd);
786
787   This->hEvent = hnd;
788
789   return DI_OK;
790 }
791
792 /******************************************************************************
793   *     GetCapabilities : get the device capablitites
794   */
795 static HRESULT WINAPI SysMouseAImpl_GetCapabilities(
796         LPDIRECTINPUTDEVICE2A iface,
797         LPDIDEVCAPS lpDIDevCaps)
798 {
799   ICOM_THIS(SysMouseAImpl,iface);
800
801   TRACE("(this=%p,%p)\n",This,lpDIDevCaps);
802
803   if (lpDIDevCaps->dwSize == sizeof(DIDEVCAPS)) {
804     lpDIDevCaps->dwFlags = DIDC_ATTACHED;
805     lpDIDevCaps->dwDevType = DIDEVTYPE_MOUSE;
806     lpDIDevCaps->dwAxes = 2;
807     lpDIDevCaps->dwButtons = 3;
808     lpDIDevCaps->dwPOVs = 0;
809     lpDIDevCaps->dwFFSamplePeriod = 0;
810     lpDIDevCaps->dwFFMinTimeResolution = 0;
811     lpDIDevCaps->dwFirmwareRevision = 100;
812     lpDIDevCaps->dwHardwareRevision = 100;
813     lpDIDevCaps->dwFFDriverVersion = 0;
814   } else {
815     /* DirectX 3.0 */
816     FIXME("DirectX 3.0 not supported....\n");
817   }
818   
819   return DI_OK;
820 }
821
822
823 /******************************************************************************
824   *     EnumObjects : enumerate the different buttons and axis...
825   */
826 static HRESULT WINAPI SysMouseAImpl_EnumObjects(
827         LPDIRECTINPUTDEVICE2A iface,
828         LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback,
829         LPVOID lpvRef,
830         DWORD dwFlags)
831 {
832   ICOM_THIS(SysMouseAImpl,iface);
833   DIDEVICEOBJECTINSTANCEA ddoi;
834   
835   TRACE("(this=%p,%p,%p,%08lx)\n", This, lpCallback, lpvRef, dwFlags);
836   if (TRACE_ON(dinput)) {
837     DPRINTF("  - flags = ");
838     _dump_EnumObjects_flags(dwFlags);
839     DPRINTF("\n");
840   }
841
842   /* Only the fields till dwFFMaxForce are relevant */
843   ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA, dwFFMaxForce);
844     
845   /* In a mouse, we have : two relative axis and three buttons */
846   if ((dwFlags == DIDFT_ALL) ||
847       (dwFlags & DIDFT_AXIS)) {
848     /* X axis */
849     ddoi.guidType = GUID_XAxis;
850     ddoi.dwOfs = This->offset_array[WINE_MOUSE_X_POSITION];
851     ddoi.dwType = DIDFT_MAKEINSTANCE(WINE_MOUSE_X_AXIS_INSTANCE) | DIDFT_RELAXIS;
852     strcpy(ddoi.tszName, "X-Axis");
853     _dump_OBJECTINSTANCEA(&ddoi);
854     if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
855     
856     /* Y axis */
857     ddoi.guidType = GUID_YAxis;
858     ddoi.dwOfs = This->offset_array[WINE_MOUSE_Y_POSITION];
859     ddoi.dwType = DIDFT_MAKEINSTANCE(WINE_MOUSE_Y_AXIS_INSTANCE) | DIDFT_RELAXIS;
860     strcpy(ddoi.tszName, "Y-Axis");
861     _dump_OBJECTINSTANCEA(&ddoi);
862     if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
863   }
864
865   if ((dwFlags == DIDFT_ALL) ||
866       (dwFlags & DIDFT_BUTTON)) {
867     ddoi.guidType = GUID_Button;
868
869     /* Left button */
870     ddoi.dwOfs = This->offset_array[WINE_MOUSE_L_POSITION];
871     ddoi.dwType = DIDFT_MAKEINSTANCE(WINE_MOUSE_L_BUTTON_INSTANCE) | DIDFT_PSHBUTTON;
872     strcpy(ddoi.tszName, "Left-Button");
873     _dump_OBJECTINSTANCEA(&ddoi);
874     if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
875
876     /* Right button */
877     ddoi.dwOfs = This->offset_array[WINE_MOUSE_R_POSITION];
878     ddoi.dwType = DIDFT_MAKEINSTANCE(WINE_MOUSE_R_BUTTON_INSTANCE) | DIDFT_PSHBUTTON;
879     strcpy(ddoi.tszName, "Right-Button");
880     _dump_OBJECTINSTANCEA(&ddoi);
881     if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
882
883     /* Middle button */
884     ddoi.dwOfs = This->offset_array[WINE_MOUSE_M_POSITION];
885     ddoi.dwType = DIDFT_MAKEINSTANCE(WINE_MOUSE_M_BUTTON_INSTANCE) | DIDFT_PSHBUTTON;
886     strcpy(ddoi.tszName, "Middle-Button");
887     _dump_OBJECTINSTANCEA(&ddoi);
888     if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
889   }
890
891   return DI_OK;
892 }
893
894
895 static ICOM_VTABLE(IDirectInputDevice2A) SysMouseAvt = 
896 {
897         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
898         IDirectInputDevice2AImpl_QueryInterface,
899         IDirectInputDevice2AImpl_AddRef,
900         SysMouseAImpl_Release,
901         SysMouseAImpl_GetCapabilities,
902         SysMouseAImpl_EnumObjects,
903         SysMouseAImpl_GetProperty,
904         SysMouseAImpl_SetProperty,
905         SysMouseAImpl_Acquire,
906         SysMouseAImpl_Unacquire,
907         SysMouseAImpl_GetDeviceState,
908         SysMouseAImpl_GetDeviceData,
909         SysMouseAImpl_SetDataFormat,
910         SysMouseAImpl_SetEventNotification,
911         SysMouseAImpl_SetCooperativeLevel,
912         IDirectInputDevice2AImpl_GetObjectInfo,
913         IDirectInputDevice2AImpl_GetDeviceInfo,
914         IDirectInputDevice2AImpl_RunControlPanel,
915         IDirectInputDevice2AImpl_Initialize,
916         IDirectInputDevice2AImpl_CreateEffect,
917         IDirectInputDevice2AImpl_EnumEffects,
918         IDirectInputDevice2AImpl_GetEffectInfo,
919         IDirectInputDevice2AImpl_GetForceFeedbackState,
920         IDirectInputDevice2AImpl_SendForceFeedbackCommand,
921         IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
922         IDirectInputDevice2AImpl_Escape,
923         IDirectInputDevice2AImpl_Poll,
924         IDirectInputDevice2AImpl_SendDeviceData,
925 };
926
927 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
928 # define XCAST(fun)     (typeof(SysMouse7Avt.fn##fun))
929 #else
930 # define XCAST(fun)     (void*)
931 #endif
932
933 static ICOM_VTABLE(IDirectInputDevice7A) SysMouse7Avt = 
934 {
935         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
936         XCAST(QueryInterface)IDirectInputDevice2AImpl_QueryInterface,
937         XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
938         XCAST(Release)SysMouseAImpl_Release,
939         XCAST(GetCapabilities)SysMouseAImpl_GetCapabilities,
940         XCAST(EnumObjects)SysMouseAImpl_EnumObjects,
941         XCAST(GetProperty)SysMouseAImpl_GetProperty,
942         XCAST(SetProperty)SysMouseAImpl_SetProperty,
943         XCAST(Acquire)SysMouseAImpl_Acquire,
944         XCAST(Unacquire)SysMouseAImpl_Unacquire,
945         XCAST(GetDeviceState)SysMouseAImpl_GetDeviceState,
946         XCAST(GetDeviceData)SysMouseAImpl_GetDeviceData,
947         XCAST(SetDataFormat)SysMouseAImpl_SetDataFormat,
948         XCAST(SetEventNotification)SysMouseAImpl_SetEventNotification,
949         XCAST(SetCooperativeLevel)SysMouseAImpl_SetCooperativeLevel,
950         XCAST(GetObjectInfo)IDirectInputDevice2AImpl_GetObjectInfo,
951         XCAST(GetDeviceInfo)IDirectInputDevice2AImpl_GetDeviceInfo,
952         XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
953         XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
954         XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
955         XCAST(EnumEffects)IDirectInputDevice2AImpl_EnumEffects,
956         XCAST(GetEffectInfo)IDirectInputDevice2AImpl_GetEffectInfo,
957         XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
958         XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
959         XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
960         XCAST(Escape)IDirectInputDevice2AImpl_Escape,
961         XCAST(Poll)IDirectInputDevice2AImpl_Poll,
962         XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
963         IDirectInputDevice7AImpl_EnumEffectsInFile,
964         IDirectInputDevice7AImpl_WriteEffectToFile
965 };
966
967 #undef XCAST