]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - in_svgalib.c
Forgot to update build number for 105 release
[xonotic/darkplaces.git] / in_svgalib.c
1 /*
2         in_svgalib.c
3
4         (description)
5
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
10
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.
15
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.
19
20         See the GNU General Public License for more details.
21
22         You should have received a copy of the GNU General Public License
23         along with this program; if not, write to:
24
25                 Free Software Foundation, Inc.
26                 59 Temple Place - Suite 330
27                 Boston, MA  02111-1307, USA
28
29         $Id$
30 */
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include "quakedef.h"
37 #include "sys.h"
38 #include "console.h"
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #ifdef HAVE_UNISTD_H
43 #include <unistd.h>
44 #endif
45
46 #include <vga.h>
47 #include <vgakeyboard.h>
48 #include <vgamouse.h>
49
50
51 static int      UseKeyboard = 1;
52 static int      UseMouse = 1;
53 static int      in_svgalib_inited = 0;
54
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;
61 static int      mx, my;
62
63 static void IN_init_kb(void);
64 static void IN_init_mouse(void);
65
66 cvar_t m_filter = {"m_filter","0"};
67
68 static void keyhandler(int scancode, int state)
69 {
70         int sc;
71
72         sc = scancode & 0x7f;
73 #if 0
74         Con_Printf("scancode=%x (%d%s)\n", scancode, sc, scancode&0x80?"+128":"");
75 #endif
76         Key_Event(scantokey[sc], state == KEY_EVENTPRESS);
77 }
78
79
80 static void mousehandler(int buttonstate, int dx, int dy, int dz, int drx, int dry, int drz)
81 {
82         mouse_buttonstate = buttonstate;
83         mx += dx;
84         my += dy;
85         if (drx > 0) {
86                 Key_Event(K_MWHEELUP, 1);
87                 Key_Event(K_MWHEELUP, 0);
88         } else if (drx < 0) {
89                 Key_Event(K_MWHEELDOWN, 1);
90                 Key_Event(K_MWHEELDOWN, 0);
91         }
92 }
93
94
95 void IN_Init(void)
96 {
97         if (COM_CheckParm("-nokbd")) UseKeyboard = 0;
98         if (COM_CheckParm("-nomouse")) UseMouse = 0;
99
100         if (UseKeyboard)
101                 IN_init_kb();
102         if (UseMouse)
103                 IN_init_mouse();
104
105         in_svgalib_inited = 1;
106 }
107
108 static void IN_init_kb(void)
109 {
110         int i;
111
112         for (i=0 ; i<128 ; i++) {
113                 scantokey[i] = ' ';
114         }
115
116         scantokey[  1] = K_ESCAPE;
117         scantokey[  2] = '1';
118         scantokey[  3] = '2';
119         scantokey[  4] = '3';
120         scantokey[  5] = '4';
121         scantokey[  6] = '5';
122         scantokey[  7] = '6';
123         scantokey[  8] = '7';
124         scantokey[  9] = '8';
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 */
208
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;
221
222         if (keyboard_init()) {
223                 Sys_Error("keyboard_init() failed");
224         }
225         keyboard_seteventhandler(keyhandler);
226 }
227
228 static void IN_init_mouse(void)
229 {
230         int mtype;
231         char *mousedev;
232         int mouserate = MOUSE_DEFAULTSAMPLERATE;
233
234         Cvar_RegisterVariable (&m_filter);
235
236         mouse_buttons = 3;
237
238         mtype = vga_getmousetype();
239
240         mousedev = "/dev/mouse";
241         if (getenv("MOUSEDEV")) mousedev = getenv("MOUSEDEV");
242         if (COM_CheckParm("-mdev")) {
243                 mousedev = com_argv[COM_CheckParm("-mdev")+1];
244         }
245
246         if (getenv("MOUSERATE")) mouserate = atoi(getenv("MOUSERATE"));
247         if (COM_CheckParm("-mrate")) {
248                 mouserate = atoi(com_argv[COM_CheckParm("-mrate")+1]);
249         }
250
251 #if 0
252         printf("Mouse: dev=%s,type=%s,speed=%d\n",
253                 mousedev, mice[mtype].name, mouserate);
254 #endif
255         if (mouse_init(mousedev, mtype, mouserate)) {
256                 Con_Printf("No mouse found\n");
257                 UseMouse = 0;
258         } else{
259                 mouse_seteventhandler((void*)mousehandler);
260         }
261 }
262
263 void IN_Shutdown(void)
264 {
265         Con_Printf("IN_Shutdown\n");
266
267         if (UseMouse) mouse_close();
268         if (UseKeyboard) keyboard_close();
269         in_svgalib_inited = 0;
270 }
271
272
273 void Sys_SendKeyEvents(void)
274 {
275         if (!in_svgalib_inited) return;
276
277         if (UseKeyboard) {
278                 while ((keyboard_update()));
279         }
280 }
281
282
283 void IN_Commands(void)
284 {
285         if (UseMouse)
286         {
287                 /* Poll mouse values */
288                 while (mouse_update())
289                         ;
290
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);
298
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);
305
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);
312
313                 mouse_oldbuttonstate = mouse_buttonstate;
314         }
315 }
316
317
318 void IN_Move(usercmd_t *cmd)
319 {
320         int mouselook = (in_mlook.state & 1) || freelook.value;
321         if (!UseMouse) return;
322
323         /* Poll mouse values */
324         while (mouse_update())
325                 ;
326
327         if (m_filter.value)
328         {
329                 mouse_x = (mx + old_mouse_x) * 0.5;
330                 mouse_y = (my + old_mouse_y) * 0.5;
331         }
332         else
333         {
334                 mouse_x = mx;
335                 mouse_y = my;
336         }
337         old_mouse_x = mx;
338         old_mouse_y = my;
339         /* Clear for next update */
340         mx = my = 0;
341
342         mouse_x *= sensitivity.value;
343         mouse_y *= sensitivity.value;
344
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;
348         else
349                 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
350
351         if (mouselook) V_StopPitchDrift();
352
353         // LordHavoc: changed limits on pitch from -70 to 80, to -90 to 90
354         if (mouselook && !(in_strafe.state & 1))
355         {
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;
361         }
362         else
363         {
364                 if ((in_strafe.state & 1) && noclip_anglehack)
365                         cmd->upmove -= m_forward.value * mouse_y;
366                 else
367                         cmd->forwardmove -= m_forward.value * mouse_y;
368         }
369 }
370
371 void IN_HandlePause (qboolean pause)
372 {
373 }