Better thread safety for WarpPointer hack.
[wine] / dlls / mouse / mouse_main.c
1 /*
2  * MOUSE driver
3  * 
4  * Copyright 1998 Ulrich Weigand
5  * 
6  */
7
8 #include <string.h>
9
10 #include "debugtools.h"
11 #include "callback.h"
12 #include "builtin16.h"
13 #include "module.h"
14 #include "mouse.h"
15 #include "monitor.h"
16 #include "windef.h"
17 #include "wingdi.h"
18 #include "winuser.h"
19 #include "win.h"
20 #include "wine/winbase16.h"
21
22 DEFAULT_DEBUG_CHANNEL(event)
23
24 /**********************************************************************/
25
26 MOUSE_DRIVER *MOUSE_Driver = NULL;
27
28 static LPMOUSE_EVENT_PROC DefMouseEventProc = NULL;
29
30 /***********************************************************************
31  *           MOUSE_Inquire                       (MOUSE.1)
32  */
33 WORD WINAPI MOUSE_Inquire(LPMOUSEINFO mouseInfo)
34 {
35     mouseInfo->msExist = TRUE;
36     mouseInfo->msRelative = FALSE;
37     mouseInfo->msNumButtons = 2;
38     mouseInfo->msRate = 34;  /* the DDK says so ... */
39     mouseInfo->msXThreshold = 0;
40     mouseInfo->msYThreshold = 0;
41     mouseInfo->msXRes = 0;
42     mouseInfo->msYRes = 0;
43     mouseInfo->msMouseCommPort = 0;
44
45     return sizeof(MOUSEINFO);
46 }
47
48 /***********************************************************************
49  *           MOUSE_Enable                        (MOUSE.2)
50  */
51 VOID WINAPI MOUSE_Enable(LPMOUSE_EVENT_PROC lpMouseEventProc)
52 {
53     static BOOL initDone = FALSE;
54     
55     THUNK_Free( (FARPROC)DefMouseEventProc );
56     DefMouseEventProc = lpMouseEventProc;
57
58     /* Now initialize the mouse driver */
59     if (initDone == FALSE)
60         {
61                 MOUSE_Driver->pInit();
62         initDone = TRUE;
63         }
64 }
65
66 static VOID WINAPI MOUSE_CallMouseEventProc( FARPROC16 proc,
67                                              DWORD dwFlags, DWORD dx, DWORD dy,
68                                              DWORD cButtons, DWORD dwExtraInfo )
69 {
70     CONTEXT86 context;
71
72     memset( &context, 0, sizeof(context) );
73     CS_reg(&context)  = SELECTOROF( proc );
74     EIP_reg(&context) = OFFSETOF( proc );
75     AX_reg(&context)  = (WORD)dwFlags;
76     BX_reg(&context)  = (WORD)dx;
77     CX_reg(&context)  = (WORD)dy;
78     DX_reg(&context)  = (WORD)cButtons;
79     SI_reg(&context)  = LOWORD( dwExtraInfo );
80     DI_reg(&context)  = HIWORD( dwExtraInfo );
81
82     CallTo16RegisterShort( &context, 0 );
83 }
84
85 VOID WINAPI WIN16_MOUSE_Enable( FARPROC16 proc )
86 {
87     LPMOUSE_EVENT_PROC thunk = 
88       (LPMOUSE_EVENT_PROC)THUNK_Alloc( proc, (RELAY)MOUSE_CallMouseEventProc );
89
90     MOUSE_Enable( thunk );
91 }
92
93 /***********************************************************************
94  *           MOUSE_Disable                       (MOUSE.3)
95  */
96 VOID WINAPI MOUSE_Disable(VOID)
97 {
98     THUNK_Free( (FARPROC)DefMouseEventProc );
99     DefMouseEventProc = 0;
100 }
101
102 /***********************************************************************
103  *           MOUSE_SendEvent
104  */
105 void MOUSE_SendEvent( DWORD mouseStatus, DWORD posX, DWORD posY, 
106                       DWORD keyState, DWORD time, HWND hWnd )
107 {
108     int width  = MONITOR_GetWidth (&MONITOR_PrimaryMonitor);
109     int height = MONITOR_GetHeight(&MONITOR_PrimaryMonitor);
110     int iWndsLocks;
111     WINE_MOUSEEVENT wme;
112
113     if ( !DefMouseEventProc ) return;
114
115     TRACE("(%04lX,%ld,%ld)\n", mouseStatus, posX, posY );
116
117     if (mouseStatus & MOUSEEVENTF_MOVE) {
118       if (mouseStatus & MOUSEEVENTF_ABSOLUTE) {
119         /* Relative mouse movements seems not to be scaled as absolute ones */
120         posX = (((long)posX << 16) + width-1)  / width;
121         posY = (((long)posY << 16) + height-1) / height;
122       }
123     }
124
125     wme.magic    = WINE_MOUSEEVENT_MAGIC;
126     wme.time     = time;
127     wme.hWnd     = hWnd;
128     wme.keyState = keyState;
129     
130     MOUSE_Driver->pEnableWarpPointer(FALSE);
131     /* To avoid deadlocks, we have to suspend all locks on windows structures
132        before the program control is passed to the mouse driver */
133     iWndsLocks = WIN_SuspendWndsLock();
134     DefMouseEventProc( mouseStatus, posX, posY, 0, (DWORD)&wme );
135     WIN_RestoreWndsLock(iWndsLocks);
136     MOUSE_Driver->pEnableWarpPointer(TRUE);
137 }