Cygwin support, using SDL.
[btb/d2x.git] / unused / win95 / mouse.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14
15 #pragma off (unreferenced)
16 static char rcsid[] = "$Id: mouse.c,v 1.1.1.1 2001-01-19 03:30:15 bradleyb Exp $";
17 #pragma on (unreferenced)
18
19 #define _WIN32
20 #define WIN32_LEAN_AND_MEAN
21
22 #include <windows.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <conio.h>
26 #include <string.h>
27 #include <i86.h>
28
29 #include "error.h"
30 #include "fix.h"
31 #include "mouse.h"
32 #include "timer.h"
33 #include "mono.h"
34
35 #define ME_CURSOR_MOVED (1<<0)
36 #define ME_LB_P                         (1<<1)
37 #define ME_LB_R                         (1<<2)
38 #define ME_RB_P                         (1<<3)
39 #define ME_RB_R                         (1<<4)
40 #define ME_MB_P                         (1<<5)
41 #define ME_MB_R                         (1<<6)
42 #define ME_OB_P                         (1<<7)
43 #define ME_OB_R                         (1<<8)
44 #define ME_X_C                  (1<<9)
45 #define ME_Y_C                  (1<<10)
46 #define ME_Z_C                  (1<<11)
47 #define ME_P_C                  (1<<12)
48 #define ME_B_C                  (1<<13)
49 #define ME_H_C                  (1<<14)
50 #define ME_O_C                  (1<<15)
51
52 #define MOUSE_MAX_BUTTONS       11
53
54 typedef struct event_info {
55         short x;
56         short y;
57         short z;
58         short pitch;
59         short bank;
60         short heading;
61         ushort button_status;
62         ushort device_dependant;
63 } event_info;
64
65 typedef struct mouse_info {
66         fix             ctime;
67         ubyte           cyberman;
68         int             num_buttons;
69         ubyte           pressed[MOUSE_MAX_BUTTONS];
70         fix             time_went_down[MOUSE_MAX_BUTTONS];
71         fix             time_held_down[MOUSE_MAX_BUTTONS];
72         uint            num_downs[MOUSE_MAX_BUTTONS];
73         uint            num_ups[MOUSE_MAX_BUTTONS];
74         event_info *x_info;
75         ushort  button_status;
76 } mouse_info;
77
78 typedef struct cyberman_info {
79         ubyte device_type;
80         ubyte major_version;
81         ubyte minor_version;
82         ubyte x_descriptor;
83         ubyte y_descriptor;
84         ubyte z_descriptor;
85         ubyte pitch_descriptor;
86         ubyte roll_descriptor;
87         ubyte yaw_descriptor;
88         ubyte reserved;
89 } cyberman_info;
90
91 static mouse_info Mouse;
92
93 static int Mouse_installed = 0;
94 static HWND _hMouseWnd = 0;
95 static Mouse_center = 0;
96
97 extern int timer_initialized;
98
99
100 void mouse_set_window(HWND wnd)
101 {
102         _hMouseWnd = wnd;
103 }
104
105 int Save_x=320,Save_y=240;
106
107 #define MOUSE_CENTER_X  160
108 #define MOUSE_CENTER_Y  100
109
110 int mouse_set_mode(int i)
111 {
112         int old;
113         old = Mouse_center;
114         if (i) Mouse_center = 1;
115         else Mouse_center = 0;
116
117         if (Mouse_center) {
118                 int x,y;
119                 mouse_get_pos( &Save_x, &Save_y);               //save current pos
120                 mouse_get_delta(&x,&y);                                         //flush old movement
121         }
122         else
123                 mouse_set_pos( Save_x, Save_y);                 //restore pos
124
125         return old;
126 }
127
128
129
130 //--------------------------------------------------------
131 // returns 0 if no mouse
132 //           else number of buttons
133
134 int mouse_init(int enable_cyberman)
135 {
136         if (Mouse_installed)
137                 return 2;
138                                 
139         Mouse_installed = 1;
140
141         atexit( mouse_close );
142
143         mouse_flush();
144         
145
146         return 2;
147 }
148
149
150
151 void mouse_close()
152 {
153         if (Mouse_installed)    {
154                 Mouse_installed = 0;
155                 _hMouseWnd = 0;
156         }
157 }
158
159
160 void mouse_set_limits( int x1, int y1, int x2, int y2 )
161 {
162
163 }
164
165 void mouse_get_pos( int *x, int *y)
166 {
167         POINT point;
168
169         GetCursorPos(&point);
170
171         //ScreenToClient(_hMouseWnd, &point);
172         *x = (int)point.x;
173         *y = (int)point.y;
174 }
175
176 void mouse_get_delta( int *dx, int *dy )
177 {
178         POINT point;
179  
180    GetCursorPos(&point);        
181         *dx = (point.x-MOUSE_CENTER_X)/2;
182         *dy = (point.y-MOUSE_CENTER_Y)/2;
183
184         //mprintf((0,"C=%d (%d,%d) (%d,%d) ",Mouse_center,point.x,point.y,*dx,*dy));
185
186         if (Mouse_center) 
187         //SetCursorPos (320,240);
188         SetCursorPos (MOUSE_CENTER_X,MOUSE_CENTER_Y);
189 }
190
191 int mouse_get_btns()
192 {
193         int i;
194         uint flag=1;
195         int status = 0;
196
197         for (i=0; i<MOUSE_MAX_BUTTONS; i++ )    {
198                 if (Mouse.pressed[i])
199                         status |= flag;
200                 flag <<= 1;
201         }
202         return status;
203 }
204
205 void mouse_set_pos( int x, int y)
206 {
207         POINT point;
208         point.x = x;
209         point.y = y;
210         
211         ClientToScreen(_hMouseWnd, &point);
212         SetCursorPos(point.x, point.y);
213 }
214
215 void mouse_flush()
216 {
217         int i;
218         fix CurTime;
219
220
221         //Clear the mouse data
222         CurTime =timer_get_fixed_secondsX();
223         for (i=0; i<MOUSE_MAX_BUTTONS; i++ )    {
224                 Mouse.pressed[i] = 0;
225                 Mouse.time_went_down[i] = CurTime;
226                 Mouse.time_held_down[i] = 0;
227                 Mouse.num_downs[i]=0;
228                 Mouse.num_ups[i]=0;
229         }
230 }
231
232
233 // Returns how many times this button has went down since last call.
234 int mouse_button_down_count(int button) 
235 {
236         int count;
237
238
239         count = Mouse.num_downs[button];
240         Mouse.num_downs[button]=0;
241
242         return count;
243 }
244
245 // Returns 1 if this button is currently down
246 int mouse_button_state(int button)      
247 {
248         int state;
249    
250
251         state = Mouse.pressed[button];
252
253         return state;
254 }
255
256
257
258 // Returns how long this button has been down since last call.
259 fix mouse_button_down_time(int button)  
260 {
261         fix time_down, time;
262
263
264         if ( !Mouse.pressed[button] )   {
265                 time_down = Mouse.time_held_down[button];
266                 Mouse.time_held_down[button] = 0;
267         } else  {
268                 time = timer_get_fixed_secondsX();
269                 time_down =  time - Mouse.time_went_down[button];
270                 Mouse.time_went_down[button] = time;
271         }
272
273         return time_down;
274 }
275
276 void mouse_get_cyberman_pos( int *x, int *y )
277 {
278         *x = 0;
279         *y = 0;
280 }
281
282
283 //      Mouse Callback from windows
284
285 void mouse_win_callback(UINT msg, UINT wParam, UINT lParam)
286 {
287         if (!timer_initialized) return;
288
289         Mouse.ctime = timer_get_fixed_secondsX();
290         
291         switch (msg)
292         {
293                 case WM_LBUTTONDOWN:
294                         mprintf ((0,"Left down!\n"));
295
296                         if (!Mouse.pressed[MB_LEFT]) {
297                                 Mouse.pressed[MB_LEFT] = 1;
298                                 Mouse.time_went_down[MB_LEFT] = Mouse.ctime;
299                         }
300                         Mouse.num_downs[MB_LEFT]++;
301                         break;
302
303                 case WM_LBUTTONUP:
304                         if (Mouse.pressed[MB_LEFT]) {
305                                 Mouse.pressed[MB_LEFT] = 0;
306                                 Mouse.time_held_down[MB_LEFT] += Mouse.ctime-Mouse.time_went_down[MB_LEFT];
307                         }
308                         Mouse.num_ups[MB_LEFT]++;
309                         break;
310
311                 case WM_RBUTTONDOWN:
312         
313                         mprintf ((0,"Right down!\n"));
314                         if (!Mouse.pressed[MB_RIGHT]) {
315                                 Mouse.pressed[MB_RIGHT] = 1;
316                                 Mouse.time_went_down[MB_RIGHT] = Mouse.ctime;
317                         }
318                         Mouse.num_downs[MB_RIGHT]++;
319                         break;
320
321                 case WM_RBUTTONUP:
322                         if (Mouse.pressed[MB_RIGHT])    {
323                                 Mouse.pressed[MB_RIGHT] = 0;
324                                 Mouse.time_held_down[MB_RIGHT] += Mouse.ctime-Mouse.time_went_down[MB_RIGHT];
325                         }
326                         Mouse.num_ups[MB_RIGHT]++;
327                         break;
328
329                 case WM_MBUTTONDOWN:
330                         if (!Mouse.pressed[MB_MIDDLE])  {
331                                 Mouse.pressed[MB_MIDDLE] = 1;
332                                 Mouse.time_went_down[MB_MIDDLE] = Mouse.ctime;
333                         }
334                         Mouse.num_downs[MB_MIDDLE]++;
335                         break;
336
337                 case WM_MBUTTONUP:
338                         if (Mouse.pressed[MB_MIDDLE])   {
339                                 Mouse.pressed[MB_MIDDLE] = 0;
340                                 Mouse.time_held_down[MB_MIDDLE] += Mouse.ctime-Mouse.time_went_down[MB_MIDDLE];
341                         }
342                         Mouse.num_ups[MB_MIDDLE]++;
343                         break;
344
345         }
346 }
347
348
349
350