]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - in_svgalib.c
added model_zymotic.h (forgot)
[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();
64 static void IN_init_mouse();
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 Force_CenterView_f(void)
96 {
97         cl.viewangles[PITCH] = 0;
98 }
99
100
101 void IN_Init(void)
102 {
103         if (COM_CheckParm("-nokbd")) UseKeyboard = 0;
104         if (COM_CheckParm("-nomouse")) UseMouse = 0;
105
106         if (UseKeyboard)
107                 IN_init_kb();
108         if (UseMouse)
109                 IN_init_mouse();
110
111         in_svgalib_inited = 1;
112 }
113
114 static void IN_init_kb()
115 {
116         int i;
117
118         for (i=0 ; i<128 ; i++) {
119                 scantokey[i] = ' ';
120         }
121
122         scantokey[  1] = K_ESCAPE;
123         scantokey[  2] = '1';
124         scantokey[  3] = '2';
125         scantokey[  4] = '3';
126         scantokey[  5] = '4';
127         scantokey[  6] = '5';
128         scantokey[  7] = '6';
129         scantokey[  8] = '7';
130         scantokey[  9] = '8';
131         scantokey[ 10] = '9';
132         scantokey[ 11] = '0';
133         scantokey[ 12] = '-';
134         scantokey[ 13] = '=';
135         scantokey[ 14] = K_BACKSPACE;
136         scantokey[ 15] = K_TAB;
137         scantokey[ 16] = 'q';
138         scantokey[ 17] = 'w';
139         scantokey[ 18] = 'e';
140         scantokey[ 19] = 'r';
141         scantokey[ 20] = 't';
142         scantokey[ 21] = 'y';
143         scantokey[ 22] = 'u';
144         scantokey[ 23] = 'i';
145         scantokey[ 24] = 'o';
146         scantokey[ 25] = 'p';
147         scantokey[ 26] = '[';
148         scantokey[ 27] = ']';
149         scantokey[ 28] = K_ENTER;
150         scantokey[ 29] = K_CTRL;        /*left */
151         scantokey[ 30] = 'a';
152         scantokey[ 31] = 's';
153         scantokey[ 32] = 'd';
154         scantokey[ 33] = 'f';
155         scantokey[ 34] = 'g';
156         scantokey[ 35] = 'h';
157         scantokey[ 36] = 'j';
158         scantokey[ 37] = 'k';
159         scantokey[ 38] = 'l';
160         scantokey[ 39] = ';';
161         scantokey[ 40] = '\'';
162         scantokey[ 41] = '`';
163         scantokey[ 42] = K_SHIFT;       /*left */
164         scantokey[ 43] = '\\';
165         scantokey[ 44] = 'z';
166         scantokey[ 45] = 'x';
167         scantokey[ 46] = 'c';
168         scantokey[ 47] = 'v';
169         scantokey[ 48] = 'b';
170         scantokey[ 49] = 'n';
171         scantokey[ 50] = 'm';
172         scantokey[ 51] = ',';
173         scantokey[ 52] = '.';
174         scantokey[ 53] = '/';
175         scantokey[ 54] = K_SHIFT;       /*right */
176         scantokey[ 55] = KP_MULTIPLY;
177         scantokey[ 56] = K_ALT;         /*left */
178         scantokey[ 57] = ' ';
179         scantokey[ 58] = K_CAPSLOCK;
180         scantokey[ 59] = K_F1;
181         scantokey[ 60] = K_F2;
182         scantokey[ 61] = K_F3;
183         scantokey[ 62] = K_F4;
184         scantokey[ 63] = K_F5;
185         scantokey[ 64] = K_F6;
186         scantokey[ 65] = K_F7;
187         scantokey[ 66] = K_F8;
188         scantokey[ 67] = K_F9;
189         scantokey[ 68] = K_F10;
190         scantokey[ 69] = KP_NUMLCK;
191         scantokey[ 70] = K_SCRLCK;
192         scantokey[ 71] = KP_HOME;
193         scantokey[ 72] = KP_UPARROW;
194         scantokey[ 73] = KP_PGUP;
195         scantokey[ 74] = KP_MINUS;
196         scantokey[ 75] = KP_LEFTARROW;
197         scantokey[ 76] = KP_5;
198         scantokey[ 77] = KP_RIGHTARROW;
199         scantokey[ 79] = KP_END;
200         scantokey[ 78] = KP_PLUS;
201         scantokey[ 80] = KP_DOWNARROW;
202         scantokey[ 81] = KP_PGDN;
203         scantokey[ 82] = KP_INS;
204         scantokey[ 83] = KP_DEL;
205         /* 84 to 86 not used */
206         scantokey[ 87] = K_F11;
207         scantokey[ 88] = K_F12;
208         /* 89 to 95 not used */
209         scantokey[ 96] = KP_ENTER;      /* keypad enter */
210         scantokey[ 97] = K_CTRL;        /* right */
211         scantokey[ 98] = KP_DIVIDE;
212         scantokey[ 99] = K_PRNTSCR;     /* print screen */
213         scantokey[100] = K_ALT;         /* right */
214
215         scantokey[101] = K_PAUSE;       /* break */
216         scantokey[102] = K_HOME;
217         scantokey[103] = K_UPARROW;
218         scantokey[104] = K_PGUP;
219         scantokey[105] = K_LEFTARROW;
220         scantokey[106] = K_RIGHTARROW;
221         scantokey[107] = K_END;
222         scantokey[108] = K_DOWNARROW;
223         scantokey[109] = K_PGDN;
224         scantokey[110] = K_INS;
225         scantokey[111] = K_DEL;
226         scantokey[119] = K_PAUSE;
227
228         if (keyboard_init()) {
229                 Sys_Error("keyboard_init() failed");
230         }
231         keyboard_seteventhandler(keyhandler);
232 }
233
234 static void IN_init_mouse()
235 {
236         int mtype;
237         char *mousedev;
238         int mouserate = MOUSE_DEFAULTSAMPLERATE;
239
240         Cvar_RegisterVariable (&m_filter);
241         Cmd_AddCommand("force_centerview", Force_CenterView_f);
242
243         mouse_buttons = 3;
244
245         mtype = vga_getmousetype();
246
247         mousedev = "/dev/mouse";
248         if (getenv("MOUSEDEV")) mousedev = getenv("MOUSEDEV");
249         if (COM_CheckParm("-mdev")) {
250                 mousedev = com_argv[COM_CheckParm("-mdev")+1];
251         }
252
253         if (getenv("MOUSERATE")) mouserate = atoi(getenv("MOUSERATE"));
254         if (COM_CheckParm("-mrate")) {
255                 mouserate = atoi(com_argv[COM_CheckParm("-mrate")+1]);
256         }
257
258 #if 0
259         printf("Mouse: dev=%s,type=%s,speed=%d\n",
260                 mousedev, mice[mtype].name, mouserate);
261 #endif
262         if (mouse_init(mousedev, mtype, mouserate)) {
263                 Con_Printf("No mouse found\n");
264                 UseMouse = 0;
265         } else{
266                 mouse_seteventhandler((void*)mousehandler);
267         }
268 }
269
270 void IN_Shutdown(void)
271 {
272         Con_Printf("IN_Shutdown\n");
273
274         if (UseMouse) mouse_close();
275         if (UseKeyboard) keyboard_close();
276         in_svgalib_inited = 0;
277 }
278
279
280 void Sys_SendKeyEvents(void)
281 {
282         if (!in_svgalib_inited) return;
283
284         if (UseKeyboard) {
285                 while ((keyboard_update()));
286         }
287 }
288
289
290 void IN_Commands(void)
291 {
292         if (UseMouse)
293         {
294                 /* Poll mouse values */
295                 while (mouse_update())
296                         ;
297
298                 /* Perform button actions */
299                 if ((mouse_buttonstate & MOUSE_LEFTBUTTON) &&
300                         !(mouse_oldbuttonstate & MOUSE_LEFTBUTTON))
301                         Key_Event (K_MOUSE1, true);
302                 else if (!(mouse_buttonstate & MOUSE_LEFTBUTTON) &&
303                         (mouse_oldbuttonstate & MOUSE_LEFTBUTTON))
304                         Key_Event (K_MOUSE1, false);
305
306                 if ((mouse_buttonstate & MOUSE_RIGHTBUTTON) &&
307                         !(mouse_oldbuttonstate & MOUSE_RIGHTBUTTON))
308                         Key_Event (K_MOUSE2, true);
309                 else if (!(mouse_buttonstate & MOUSE_RIGHTBUTTON) &&
310                         (mouse_oldbuttonstate & MOUSE_RIGHTBUTTON))
311                         Key_Event (K_MOUSE2, false);
312
313                 if ((mouse_buttonstate & MOUSE_MIDDLEBUTTON) &&
314                         !(mouse_oldbuttonstate & MOUSE_MIDDLEBUTTON))
315                         Key_Event (K_MOUSE3, true);
316                 else if (!(mouse_buttonstate & MOUSE_MIDDLEBUTTON) &&
317                         (mouse_oldbuttonstate & MOUSE_MIDDLEBUTTON))
318                         Key_Event (K_MOUSE3, false);
319
320                 mouse_oldbuttonstate = mouse_buttonstate;
321         }
322 }
323
324
325 void IN_Move(usercmd_t *cmd)
326 {
327         if (!UseMouse) return;
328
329         /* Poll mouse values */
330         while (mouse_update())
331                 ;
332
333         if (m_filter.value) {
334                 mouse_x = (mx + old_mouse_x) * 0.5;
335                 mouse_y = (my + old_mouse_y) * 0.5;
336         } else {
337                 mouse_x = mx;
338                 mouse_y = my;
339         }
340         old_mouse_x = mx;
341         old_mouse_y = my;
342         /* Clear for next update */
343         mx = my = 0;
344
345         mouse_x *= sensitivity.value;
346         mouse_y *= sensitivity.value;
347
348         /* Add mouse X/Y movement to cmd */
349         if ( (in_strafe.state & 1) ||
350              (lookstrafe.value && (in_mlook.state & 1) )) {
351                 cmd->sidemove += m_side.value * mouse_x;
352         } else {
353                 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
354         }
355
356         if ((in_mlook.state & 1)) V_StopPitchDrift();
357
358         if ((in_mlook.state & 1) && !(in_strafe.state & 1)) {
359                 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
360                 if (cl.viewangles[PITCH] > 80) {
361                         cl.viewangles[PITCH] = 80;
362                 }
363                 if (cl.viewangles[PITCH] < -70) {
364                         cl.viewangles[PITCH] = -70;
365                 }
366         } else {
367                 if ((in_strafe.state & 1) && noclip_anglehack) {
368                         cmd->upmove -= m_forward.value * mouse_y;
369                 } else {
370                         cmd->forwardmove -= m_forward.value * mouse_y;
371                 }
372         }
373 }
374
375 void IN_HandlePause (qboolean pause)
376 {
377 }