2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // in_sun.c -- SUN/X mouse input handler
23 #include <sys/types.h>
32 #include <X11/Xutil.h>
33 #include <X11/Xatom.h>
34 #include <X11/keysym.h>
40 // typedefs and defines
49 extern Display
*x_disp
;
50 extern int x_screen
, x_screen_width
, x_screen_height
;
51 extern int x_center_height
, x_center_width
;
52 extern int x_std_event_mask
;
53 extern Window x_win
, x_root_win
;
54 extern qboolean x_fullscreen
;
55 extern qboolean x_focus
;
56 extern int global_dx
, global_dy
;
61 cvar_t _windowed_mouse
= {"_windowed_mouse","1", true};
63 int x_root_old
, y_root_old
;
68 static int x_mouse_num
, x_mouse_denom
, x_mouse_thresh
;
71 static qboolean x_grabbed
= false;
74 // IN_CenterMouse - center the mouse in the screen
77 void IN_CenterMouse( void )
84 XSelectInput( x_disp
, x_win
, x_std_event_mask
& ~PointerMotionMask
);
85 XWarpPointer( x_disp
, None
, x_root_win
, 0, 0, 0, 0, x_center_width
,
87 XSelectInput( x_disp
, x_win
, x_std_event_mask
);
91 // Check to see if we have grabbed the mouse or not and deal with it
94 static void CheckMouseState(void)
96 if (x_focus
&& _windowed_mouse
.value
&& !x_grabbed
) {
98 printf("fooling with mouse!\n");
99 if (XGetPointerControl( x_disp
, &x_mouse_num
, &x_mouse_denom
, &x_mouse_thresh
))
100 printf( "XGetPointerControl failed!\n" );
101 //printf( "mouse %d/%d thresh %d\n", x_mouse_num, x_mouse_denom, x_mouse_thresh );
104 XAutoRepeatOff(x_disp
);
105 XGrabKeyboard(x_disp
, x_win
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
);
106 XGrabPointer(x_disp
, x_win
, True
,
107 PointerMotionMask
| ButtonPressMask
| ButtonReleaseMask
,
108 GrabModeAsync
, GrabModeAsync
, None
, None
, CurrentTime
);
110 // if (XChangePointerControl( x_disp, True, True, 1, MOUSE_SCALE, x_mouse_thresh ))
111 // printf( "XChangePointerControl failed!\n" );
115 // safe initial values
116 x_root
= x_root_old
= vid
.width
>> 1;
117 y_root
= y_root_old
= vid
.height
>> 1;
118 } else if (x_grabbed
&& (!_windowed_mouse
.value
|| !x_focus
)) {
119 printf("fooling with mouse!\n");
122 if (XChangePointerControl( x_disp
, True
, True
, x_mouse_num
, x_mouse_denom
, x_mouse_thresh
))
123 printf( "XChangePointerControl failed!\n" );
125 XUngrabPointer( x_disp
, CurrentTime
);
126 XUngrabKeyboard( x_disp
, CurrentTime
);
127 XAutoRepeatOn( x_disp
);
133 // IN_Init - setup mouse input
138 if (!x_disp
) Sys_Error( "X display not open!\n" );
140 Cvar_RegisterVariable (&_windowed_mouse
);
142 // we really really want to clean these up...
143 atexit( IN_Shutdown
);
147 // IN_Shutdown - clean up mouse settings (must be done from signal handler too!)
150 void IN_Shutdown (void)
155 if (XChangePointerControl( x_disp
, True
, True
, x_mouse_num
, x_mouse_denom
, x_mouse_thresh
))
156 printf( "XChangePointerControl failed!\n" );
158 XUngrabPointer( x_disp
, CurrentTime
);
159 XUngrabKeyboard( x_disp
, CurrentTime
);
160 XAutoRepeatOn( x_disp
);
164 // IN_Commands - process buttons
167 void IN_Commands (void)
169 // done in X event handler
173 // IN_Move - process mouse moves
177 IN_Move (usercmd_t
*cmd
)
179 static int last_dx
, last_dy
;
180 static long long last_movement
;
181 long long now
, gethrtime();
189 return; // no mouse movement
200 // printf("GOT: dx %d dy %d\n", dx, dy);
202 dx
*= sensitivity
.value
;
203 dy
*= sensitivity
.value
;
206 // implement low pass filter to smooth motion a bit
208 if (now
- last_movement
> 100000000) {
214 dx
= .6 * dx
+ .4 * last_dx
;
215 dy
= .6 * dy
+ .4 * last_dy
;
222 if (in_mlook
.state
& 1)
227 // add mouse X/Y movement to cmd
228 if ((in_strafe
.state
& 1) || (lookstrafe
.value
&& (in_mlook
.state
& 1)))
229 cmd
->sidemove
+= m_side
.value
* dx
;
231 cl
.viewangles
[YAW
] -= m_yaw
.value
* dx
;
233 if (in_mlook
.state
& 1)
236 if ((in_mlook
.state
& 1) && !(in_strafe
.state
& 1)) {
237 cl
.viewangles
[PITCH
] += m_pitch
.value
* dy
;
238 if (cl
.viewangles
[PITCH
] > 80) cl
.viewangles
[PITCH
] = 80;
239 if (cl
.viewangles
[PITCH
] < -70) cl
.viewangles
[PITCH
] = -70;
242 if ((in_strafe
.state
& 1) && noclip_anglehack
) cmd
->upmove
-= m_forward
.value
* dy
;
243 else cmd
->forwardmove
-= m_forward
.value
* dy
;