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