6 Copyright (C) 1996-1997 Id Software, Inc.
7 Copyright (C) 1999-2000 Marcus Sundberg [mackan@stacken.kth.se]
8 Copyright (C) 1999,2000 contributors of the QuakeForge project
9 Please see the file "AUTHORS" for a list of contributors
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 2
14 of the License, or (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 See the GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to:
25 Free Software Foundation, Inc.
26 59 Temple Place - Suite 330
27 Boston, MA 02111-1307, USA
47 #include <vgakeyboard.h>
51 static int UseKeyboard = 1;
52 static int UseMouse = 1;
53 static int in_svgalib_inited = 0;
55 static unsigned char scantokey[128];
56 static int mouse_buttons;
57 static int mouse_buttonstate;
58 static int mouse_oldbuttonstate;
59 static float mouse_x, mouse_y;
60 static float old_mouse_x, old_mouse_y;
63 static void IN_init_kb(void);
64 static void IN_init_mouse(void);
66 cvar_t m_filter = {"m_filter","0"};
68 static void keyhandler(int scancode, int state)
74 Con_Printf("scancode=%x (%d%s)\n", scancode, sc, scancode&0x80?"+128":"");
76 Key_Event(scantokey[sc], state == KEY_EVENTPRESS);
80 static void mousehandler(int buttonstate, int dx, int dy, int dz, int drx, int dry, int drz)
82 mouse_buttonstate = buttonstate;
86 Key_Event(K_MWHEELUP, 1);
87 Key_Event(K_MWHEELUP, 0);
89 Key_Event(K_MWHEELDOWN, 1);
90 Key_Event(K_MWHEELDOWN, 0);
97 if (COM_CheckParm("-nokbd")) UseKeyboard = 0;
98 if (COM_CheckParm("-nomouse")) UseMouse = 0;
105 in_svgalib_inited = 1;
108 static void IN_init_kb(void)
112 for (i=0 ; i<128 ; i++) {
116 scantokey[ 1] = K_ESCAPE;
125 scantokey[ 10] = '9';
126 scantokey[ 11] = '0';
127 scantokey[ 12] = '-';
128 scantokey[ 13] = '=';
129 scantokey[ 14] = K_BACKSPACE;
130 scantokey[ 15] = K_TAB;
131 scantokey[ 16] = 'q';
132 scantokey[ 17] = 'w';
133 scantokey[ 18] = 'e';
134 scantokey[ 19] = 'r';
135 scantokey[ 20] = 't';
136 scantokey[ 21] = 'y';
137 scantokey[ 22] = 'u';
138 scantokey[ 23] = 'i';
139 scantokey[ 24] = 'o';
140 scantokey[ 25] = 'p';
141 scantokey[ 26] = '[';
142 scantokey[ 27] = ']';
143 scantokey[ 28] = K_ENTER;
144 scantokey[ 29] = K_CTRL; /*left */
145 scantokey[ 30] = 'a';
146 scantokey[ 31] = 's';
147 scantokey[ 32] = 'd';
148 scantokey[ 33] = 'f';
149 scantokey[ 34] = 'g';
150 scantokey[ 35] = 'h';
151 scantokey[ 36] = 'j';
152 scantokey[ 37] = 'k';
153 scantokey[ 38] = 'l';
154 scantokey[ 39] = ';';
155 scantokey[ 40] = '\'';
156 scantokey[ 41] = '`';
157 scantokey[ 42] = K_SHIFT; /*left */
158 scantokey[ 43] = '\\';
159 scantokey[ 44] = 'z';
160 scantokey[ 45] = 'x';
161 scantokey[ 46] = 'c';
162 scantokey[ 47] = 'v';
163 scantokey[ 48] = 'b';
164 scantokey[ 49] = 'n';
165 scantokey[ 50] = 'm';
166 scantokey[ 51] = ',';
167 scantokey[ 52] = '.';
168 scantokey[ 53] = '/';
169 scantokey[ 54] = K_SHIFT; /*right */
170 scantokey[ 55] = KP_MULTIPLY;
171 scantokey[ 56] = K_ALT; /*left */
172 scantokey[ 57] = ' ';
173 scantokey[ 58] = K_CAPSLOCK;
174 scantokey[ 59] = K_F1;
175 scantokey[ 60] = K_F2;
176 scantokey[ 61] = K_F3;
177 scantokey[ 62] = K_F4;
178 scantokey[ 63] = K_F5;
179 scantokey[ 64] = K_F6;
180 scantokey[ 65] = K_F7;
181 scantokey[ 66] = K_F8;
182 scantokey[ 67] = K_F9;
183 scantokey[ 68] = K_F10;
184 scantokey[ 69] = KP_NUMLCK;
185 scantokey[ 70] = K_SCRLCK;
186 scantokey[ 71] = KP_HOME;
187 scantokey[ 72] = KP_UPARROW;
188 scantokey[ 73] = KP_PGUP;
189 scantokey[ 74] = KP_MINUS;
190 scantokey[ 75] = KP_LEFTARROW;
191 scantokey[ 76] = KP_5;
192 scantokey[ 77] = KP_RIGHTARROW;
193 scantokey[ 79] = KP_END;
194 scantokey[ 78] = KP_PLUS;
195 scantokey[ 80] = KP_DOWNARROW;
196 scantokey[ 81] = KP_PGDN;
197 scantokey[ 82] = KP_INS;
198 scantokey[ 83] = KP_DEL;
199 /* 84 to 86 not used */
200 scantokey[ 87] = K_F11;
201 scantokey[ 88] = K_F12;
202 /* 89 to 95 not used */
203 scantokey[ 96] = KP_ENTER; /* keypad enter */
204 scantokey[ 97] = K_CTRL; /* right */
205 scantokey[ 98] = KP_DIVIDE;
206 scantokey[ 99] = K_PRNTSCR; /* print screen */
207 scantokey[100] = K_ALT; /* right */
209 scantokey[101] = K_PAUSE; /* break */
210 scantokey[102] = K_HOME;
211 scantokey[103] = K_UPARROW;
212 scantokey[104] = K_PGUP;
213 scantokey[105] = K_LEFTARROW;
214 scantokey[106] = K_RIGHTARROW;
215 scantokey[107] = K_END;
216 scantokey[108] = K_DOWNARROW;
217 scantokey[109] = K_PGDN;
218 scantokey[110] = K_INS;
219 scantokey[111] = K_DEL;
220 scantokey[119] = K_PAUSE;
222 if (keyboard_init()) {
223 Sys_Error("keyboard_init() failed");
225 keyboard_seteventhandler(keyhandler);
228 static void IN_init_mouse(void)
232 int mouserate = MOUSE_DEFAULTSAMPLERATE;
234 Cvar_RegisterVariable (&m_filter);
238 mtype = vga_getmousetype();
240 mousedev = "/dev/mouse";
241 if (getenv("MOUSEDEV")) mousedev = getenv("MOUSEDEV");
242 if (COM_CheckParm("-mdev")) {
243 mousedev = com_argv[COM_CheckParm("-mdev")+1];
246 if (getenv("MOUSERATE")) mouserate = atoi(getenv("MOUSERATE"));
247 if (COM_CheckParm("-mrate")) {
248 mouserate = atoi(com_argv[COM_CheckParm("-mrate")+1]);
252 printf("Mouse: dev=%s,type=%s,speed=%d\n",
253 mousedev, mice[mtype].name, mouserate);
255 if (mouse_init(mousedev, mtype, mouserate)) {
256 Con_Printf("No mouse found\n");
259 mouse_seteventhandler((void*)mousehandler);
263 void IN_Shutdown(void)
265 Con_Printf("IN_Shutdown\n");
267 if (UseMouse) mouse_close();
268 if (UseKeyboard) keyboard_close();
269 in_svgalib_inited = 0;
273 void Sys_SendKeyEvents(void)
275 if (!in_svgalib_inited) return;
278 while ((keyboard_update()));
283 void IN_Commands(void)
287 /* Poll mouse values */
288 while (mouse_update())
291 /* Perform button actions */
292 if ((mouse_buttonstate & MOUSE_LEFTBUTTON) &&
293 !(mouse_oldbuttonstate & MOUSE_LEFTBUTTON))
294 Key_Event (K_MOUSE1, true);
295 else if (!(mouse_buttonstate & MOUSE_LEFTBUTTON) &&
296 (mouse_oldbuttonstate & MOUSE_LEFTBUTTON))
297 Key_Event (K_MOUSE1, false);
299 if ((mouse_buttonstate & MOUSE_RIGHTBUTTON) &&
300 !(mouse_oldbuttonstate & MOUSE_RIGHTBUTTON))
301 Key_Event (K_MOUSE2, true);
302 else if (!(mouse_buttonstate & MOUSE_RIGHTBUTTON) &&
303 (mouse_oldbuttonstate & MOUSE_RIGHTBUTTON))
304 Key_Event (K_MOUSE2, false);
306 if ((mouse_buttonstate & MOUSE_MIDDLEBUTTON) &&
307 !(mouse_oldbuttonstate & MOUSE_MIDDLEBUTTON))
308 Key_Event (K_MOUSE3, true);
309 else if (!(mouse_buttonstate & MOUSE_MIDDLEBUTTON) &&
310 (mouse_oldbuttonstate & MOUSE_MIDDLEBUTTON))
311 Key_Event (K_MOUSE3, false);
313 mouse_oldbuttonstate = mouse_buttonstate;
318 void IN_Move(usercmd_t *cmd)
320 int mouselook = (in_mlook.state & 1) || freelook.value;
321 if (!UseMouse) return;
323 /* Poll mouse values */
324 while (mouse_update())
329 mouse_x = (mx + old_mouse_x) * 0.5;
330 mouse_y = (my + old_mouse_y) * 0.5;
339 /* Clear for next update */
342 mouse_x *= sensitivity.value;
343 mouse_y *= sensitivity.value;
345 /* Add mouse X/Y movement to cmd */
346 if ( (in_strafe.state & 1) || (lookstrafe.value && mouselook))
347 cmd->sidemove += m_side.value * mouse_x;
349 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
351 if (mouselook) V_StopPitchDrift();
353 // LordHavoc: changed limits on pitch from -70 to 80, to -90 to 90
354 if (mouselook && !(in_strafe.state & 1))
356 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
357 if (cl.viewangles[PITCH] > 90)
358 cl.viewangles[PITCH] = 90;
359 if (cl.viewangles[PITCH] < -90)
360 cl.viewangles[PITCH] = -90;
364 if ((in_strafe.state & 1) && noclip_anglehack)
365 cmd->upmove -= m_forward.value * mouse_y;
367 cmd->forwardmove -= m_forward.value * mouse_y;
371 void IN_HandlePause (qboolean pause)