1 #include <termios.h>
2 #include <sys/ioctl.h>
3 #include <sys/stat.h>
4 #include <sys/vt.h>
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <signal.h>
8 #include <sys/mman.h>
9 
10 #include <asm/io.h>
11 
12 #include "vga.h"
13 #include "vgakeyboard.h"
14 #include "vgamouse.h"
15 
16 #include "../ref_soft/r_local.h"
17 #include "../client/keys.h"
18 #include "../linux/rw_linux.h"
19 
20 /*****************************************************************************/
21 /* KEYBOARD                                                                  */
22 /*****************************************************************************/
23 
24 static unsigned char scantokey[128];
25 Key_Event_fp_t Key_Event_fp;
26 
keyhandler(int scancode,int state)27 static void keyhandler(int scancode, int state)
28 {
29 	int sc;
30 
31 	sc = scancode & 0x7f;
32 //ri.Con_Printf(PRINT_ALL, "scancode=%x (%d%s)\n", scancode, sc, scancode&0x80?"+128":"");
33 	Key_Event_fp(scantokey[sc], state == KEY_EVENTPRESS);
34 }
35 
KBD_Init(Key_Event_fp_t fp)36 void KBD_Init(Key_Event_fp_t fp)
37 {
38 	int i;
39 
40 	Key_Event_fp = fp;
41 
42 	for (i=0 ; i<128 ; i++)
43 		scantokey[i] = ' ';
44 
45 	scantokey[  1] = K_ESCAPE;
46 	scantokey[  2] = '1';
47 	scantokey[  3] = '2';
48 	scantokey[  4] = '3';
49 	scantokey[  5] = '4';
50 	scantokey[  6] = '5';
51 	scantokey[  7] = '6';
52 	scantokey[  8] = '7';
53 	scantokey[  9] = '8';
54 	scantokey[ 10] = '9';
55 	scantokey[ 11] = '0';
56 	scantokey[ 12] = '-';
57 	scantokey[ 13] = '=';
58 	scantokey[ 14] = K_BACKSPACE;
59 	scantokey[ 15] = K_TAB;
60 	scantokey[ 16] = 'q';
61 	scantokey[ 17] = 'w';
62 	scantokey[ 18] = 'e';
63 	scantokey[ 19] = 'r';
64 	scantokey[ 20] = 't';
65 	scantokey[ 21] = 'y';
66 	scantokey[ 22] = 'u';
67 	scantokey[ 23] = 'i';
68 	scantokey[ 24] = 'o';
69 	scantokey[ 25] = 'p';
70 	scantokey[ 26] = '[';
71 	scantokey[ 27] = ']';
72 	scantokey[ 28] = K_ENTER;
73 	scantokey[ 29] = K_CTRL; //left
74 	scantokey[ 30] = 'a';
75 	scantokey[ 31] = 's';
76 	scantokey[ 32] = 'd';
77 	scantokey[ 33] = 'f';
78 	scantokey[ 34] = 'g';
79 	scantokey[ 35] = 'h';
80 	scantokey[ 36] = 'j';
81 	scantokey[ 37] = 'k';
82 	scantokey[ 38] = 'l';
83 	scantokey[ 39] = ';';
84 	scantokey[ 40] = '\'';
85 	scantokey[ 41] = '`';
86 	scantokey[ 42] = K_SHIFT; //left
87 	scantokey[ 43] = '\\';
88 	scantokey[ 44] = 'z';
89 	scantokey[ 45] = 'x';
90 	scantokey[ 46] = 'c';
91 	scantokey[ 47] = 'v';
92 	scantokey[ 48] = 'b';
93 	scantokey[ 49] = 'n';
94 	scantokey[ 50] = 'm';
95 	scantokey[ 51] = ',';
96 	scantokey[ 52] = '.';
97 	scantokey[ 53] = '/';
98 	scantokey[ 54] = K_SHIFT; //right
99 	scantokey[ 55] = '*'; //keypad
100 	scantokey[ 56] = K_ALT; //left
101 	scantokey[ 57] = ' ';
102 	// 58 caps lock
103 	scantokey[ 59] = K_F1;
104 	scantokey[ 60] = K_F2;
105 	scantokey[ 61] = K_F3;
106 	scantokey[ 62] = K_F4;
107 	scantokey[ 63] = K_F5;
108 	scantokey[ 64] = K_F6;
109 	scantokey[ 65] = K_F7;
110 	scantokey[ 66] = K_F8;
111 	scantokey[ 67] = K_F9;
112 	scantokey[ 68] = K_F10;
113 	// 69 numlock
114 	// 70 scrollock
115 	scantokey[ 71] = K_KP_HOME;
116 	scantokey[ 72] = K_KP_UPARROW;
117 	scantokey[ 73] = K_KP_PGUP;
118 	scantokey[ 74] = K_KP_MINUS;
119 	scantokey[ 75] = K_KP_LEFTARROW;
120 	scantokey[ 76] = K_KP_5;
121 	scantokey[ 77] = K_KP_RIGHTARROW;
122 	scantokey[ 79] = K_KP_END;
123 	scantokey[ 78] = K_KP_PLUS;
124 	scantokey[ 80] = K_KP_DOWNARROW;
125 	scantokey[ 81] = K_KP_PGDN;
126 	scantokey[ 82] = K_KP_INS;
127 	scantokey[ 83] = K_KP_DEL;
128 	// 84 to 86 not used
129 	scantokey[ 87] = K_F11;
130 	scantokey[ 88] = K_F12;
131 	// 89 to 95 not used
132 	scantokey[ 96] = K_KP_ENTER; //keypad enter
133 	scantokey[ 97] = K_CTRL; //right
134 	scantokey[ 98] = K_KP_SLASH;
135 	scantokey[ 99] = K_F12; // print screen, bind to screenshot by default
136 	scantokey[100] = K_ALT; // right
137 
138 	scantokey[101] = K_PAUSE; // break
139 	scantokey[102] = K_HOME;
140 	scantokey[103] = K_UPARROW;
141 	scantokey[104] = K_PGUP;
142 	scantokey[105] = K_LEFTARROW;
143 	scantokey[106] = K_RIGHTARROW;
144 	scantokey[107] = K_END;
145 	scantokey[108] = K_DOWNARROW;
146 	scantokey[109] = K_PGDN;
147 	scantokey[110] = K_INS;
148 	scantokey[111] = K_DEL;
149 
150 	scantokey[119] = K_PAUSE;
151 
152 	if (keyboard_init())
153 		Sys_Error("keyboard_init() failed");
154 	keyboard_seteventhandler(keyhandler);
155 	keyboard_translatekeys(DONT_CATCH_CTRLC);
156 }
157 
KBD_Update(void)158 void KBD_Update(void)
159 {
160 	while (keyboard_update())
161 		;
162 }
163 
KBD_Close(void)164 void KBD_Close(void)
165 {
166 	keyboard_close();
167 }
168 
169 /*****************************************************************************/
170 /* MOUSE                                                                     */
171 /*****************************************************************************/
172 
173 // this is inside the renderer shared lib, so these are called from vid_so
174 
175 static qboolean	UseMouse = true;
176 
177 static int		mouserate = MOUSE_DEFAULTSAMPLERATE;
178 
179 static int     mouse_buttons;
180 static int     mouse_buttonstate;
181 static int     mouse_oldbuttonstate;
182 static float   mouse_x, mouse_y;
183 static float	old_mouse_x, old_mouse_y;
184 static int		mx, my;
185 
186 static cvar_t	*m_filter;
187 static cvar_t	*in_mouse;
188 
189 static cvar_t	*mdev;
190 static cvar_t	*mrate;
191 
192 static qboolean	mlooking;
193 
194 // state struct passed in Init
195 static in_state_t	*in_state;
196 
197 static cvar_t *sensitivity;
198 static cvar_t *lookstrafe;
199 static cvar_t *m_side;
200 static cvar_t *m_yaw;
201 static cvar_t *m_pitch;
202 static cvar_t *m_forward;
203 static cvar_t *freelook;
204 
Force_CenterView_f(void)205 static void Force_CenterView_f (void)
206 {
207 	in_state->viewangles[PITCH] = 0;
208 }
209 
RW_IN_MLookDown(void)210 static void RW_IN_MLookDown (void)
211 {
212 	mlooking = true;
213 }
214 
RW_IN_MLookUp(void)215 static void RW_IN_MLookUp (void)
216 {
217 	mlooking = false;
218 	in_state->IN_CenterView_fp ();
219 }
220 
mousehandler(int buttonstate,int dx,int dy)221 static void mousehandler(int buttonstate, int dx, int dy)
222 {
223 	mouse_buttonstate = buttonstate;
224 	mx += dx;
225 	my += dy;
226 }
227 
RW_IN_Init(in_state_t * in_state_p)228 void RW_IN_Init(in_state_t *in_state_p)
229 {
230 	int mtype;
231 	int i;
232 
233 	in_state = in_state_p;
234 
235 	// mouse variables
236 	m_filter = ri.Cvar_Get ("m_filter", "0", 0);
237     in_mouse = ri.Cvar_Get ("in_mouse", "1", CVAR_ARCHIVE);
238 	freelook = ri.Cvar_Get( "freelook", "0", 0 );
239 	lookstrafe = ri.Cvar_Get ("lookstrafe", "0", 0);
240 	sensitivity = ri.Cvar_Get ("sensitivity", "3", 0);
241 	m_pitch = ri.Cvar_Get ("m_pitch", "0.022", 0);
242 	m_yaw = ri.Cvar_Get ("m_yaw", "0.022", 0);
243 	m_forward = ri.Cvar_Get ("m_forward", "1", 0);
244 	m_side = ri.Cvar_Get ("m_side", "0.8", 0);
245 
246 	ri.Cmd_AddCommand ("+mlook", RW_IN_MLookDown);
247 	ri.Cmd_AddCommand ("-mlook", RW_IN_MLookUp);
248 
249 	ri.Cmd_AddCommand ("force_centerview", Force_CenterView_f);
250 
251 	mouse_buttons = 3;
252 
253 	mtype = vga_getmousetype();
254 
255 	mdev = ri.Cvar_Get ("mdev", "/dev/mouse", 0);
256 	mrate = ri.Cvar_Get ("mrate", "1200", 0);
257 
258 //		printf("Mouse: dev=%s,type=%s,speed=%d\n",
259 //			mousedev, mice[mtype].name, mouserate);
260 
261 	if (mouse_init(mdev->string, mtype, (int)mrate->value))
262 	{
263 		ri.Con_Printf(PRINT_ALL, "No mouse found\n");
264 		UseMouse = false;
265 	}
266 	else
267 		mouse_seteventhandler(mousehandler);
268 }
269 
RW_IN_Shutdown(void)270 void RW_IN_Shutdown(void)
271 {
272 	mouse_close();
273 }
274 
275 /*
276 ===========
277 IN_Commands
278 ===========
279 */
RW_IN_Commands(void)280 void RW_IN_Commands (void)
281 {
282 	if (!UseMouse)
283 		return;
284 
285 	// poll mouse values
286 	mouse_update();
287 
288 	// perform button actions
289 	if ((mouse_buttonstate & MOUSE_LEFTBUTTON) &&
290 		!(mouse_oldbuttonstate & MOUSE_LEFTBUTTON))
291 		in_state->Key_Event_fp (K_MOUSE1, true);
292 	else if (!(mouse_buttonstate & MOUSE_LEFTBUTTON) &&
293 		(mouse_oldbuttonstate & MOUSE_LEFTBUTTON))
294 		in_state->Key_Event_fp (K_MOUSE1, false);
295 
296 	if ((mouse_buttonstate & MOUSE_RIGHTBUTTON) &&
297 		!(mouse_oldbuttonstate & MOUSE_RIGHTBUTTON))
298 		in_state->Key_Event_fp (K_MOUSE2, true);
299 	else if (!(mouse_buttonstate & MOUSE_RIGHTBUTTON) &&
300 		(mouse_oldbuttonstate & MOUSE_RIGHTBUTTON))
301 		in_state->Key_Event_fp (K_MOUSE2, false);
302 
303 	if ((mouse_buttonstate & MOUSE_MIDDLEBUTTON) &&
304 		!(mouse_oldbuttonstate & MOUSE_MIDDLEBUTTON))
305 		Key_Event_fp (K_MOUSE3, true);
306 	else if (!(mouse_buttonstate & MOUSE_MIDDLEBUTTON) &&
307 		(mouse_oldbuttonstate & MOUSE_MIDDLEBUTTON))
308 		in_state->Key_Event_fp (K_MOUSE3, false);
309 
310 	mouse_oldbuttonstate = mouse_buttonstate;
311 }
312 
313 /*
314 ===========
315 IN_Move
316 ===========
317 */
RW_IN_Move(usercmd_t * cmd)318 void RW_IN_Move (usercmd_t *cmd)
319 {
320 	if (!UseMouse)
321 		return;
322 
323 	// poll mouse values
324 	mouse_update();
325 
326 	if (m_filter->value)
327 	{
328 		mouse_x = (mx + old_mouse_x) * 0.5;
329 		mouse_y = (my + old_mouse_y) * 0.5;
330 	}
331 	else
332 	{
333 		mouse_x = mx;
334 		mouse_y = my;
335 	}
336 	old_mouse_x = mx;
337 	old_mouse_y = my;
338 
339 	if (!mx && !my)
340 		return;
341 
342 	mx = my = 0; // clear for next update
343 
344 	mouse_x *= sensitivity->value;
345 	mouse_y *= sensitivity->value;
346 
347 // add mouse X/Y movement to cmd
348 	if ( (*in_state->in_strafe_state & 1) ||
349 		(lookstrafe->value && mlooking ))
350 		cmd->sidemove += m_side->value * mouse_x;
351 	else
352 		in_state->viewangles[YAW] -= m_yaw->value * mouse_x;
353 
354 	if ( (mlooking || freelook->value) &&
355 		!(*in_state->in_strafe_state & 1))
356 	{
357 		in_state->viewangles[PITCH] += m_pitch->value * mouse_y;
358 	}
359 	else
360 	{
361 		cmd->forwardmove -= m_forward->value * mouse_y;
362 	}
363 }
364 
RW_IN_Frame(void)365 void RW_IN_Frame (void)
366 {
367 }
368 
RW_IN_Activate(void)369 void RW_IN_Activate(void)
370 {
371 }
372 
373 
374 
375