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