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