]> icculus.org git repositories - divverent/darkplaces.git/blob - in_svgalib.c
Fixed major bug in time wrap code, would have not advanced clock at all after wrap...
[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 static void keyhandler(int scancode, int state)
67 {
68         int sc;
69
70         sc = scancode & 0x7f;
71         Key_Event(scantokey[sc], state == KEY_EVENTPRESS);
72 }
73
74
75 static void mousehandler(int buttonstate, int dx, int dy, int dz, int drx, int dry, int drz)
76 {
77         mouse_buttonstate = buttonstate;
78         uimx += dx;
79         uimy += dy;
80         mx += dx;
81         my += dy;
82         if (drx > 0) {
83                 Key_Event(K_MWHEELUP, 1);
84                 Key_Event(K_MWHEELUP, 0);
85         } else if (drx < 0) {
86                 Key_Event(K_MWHEELDOWN, 1);
87                 Key_Event(K_MWHEELDOWN, 0);
88         }
89 }
90
91
92 void IN_Init(void)
93 {
94         if (COM_CheckParm("-nokbd")) UseKeyboard = 0;
95         if (COM_CheckParm("-nomouse")) UseMouse = 0;
96
97         uimx = uimy = 0;
98         if (UseKeyboard)
99                 IN_init_kb();
100         if (UseMouse)
101                 IN_init_mouse();
102
103         in_svgalib_inited = 1;
104 }
105
106 static void IN_init_kb(void)
107 {
108         int i;
109
110         for (i=0 ; i<128 ; i++) {
111                 scantokey[i] = ' ';
112         }
113
114         scantokey[  1] = K_ESCAPE;
115         scantokey[  2] = '1';
116         scantokey[  3] = '2';
117         scantokey[  4] = '3';
118         scantokey[  5] = '4';
119         scantokey[  6] = '5';
120         scantokey[  7] = '6';
121         scantokey[  8] = '7';
122         scantokey[  9] = '8';
123         scantokey[ 10] = '9';
124         scantokey[ 11] = '0';
125         scantokey[ 12] = '-';
126         scantokey[ 13] = '=';
127         scantokey[ 14] = K_BACKSPACE;
128         scantokey[ 15] = K_TAB;
129         scantokey[ 16] = 'q';
130         scantokey[ 17] = 'w';
131         scantokey[ 18] = 'e';
132         scantokey[ 19] = 'r';
133         scantokey[ 20] = 't';
134         scantokey[ 21] = 'y';
135         scantokey[ 22] = 'u';
136         scantokey[ 23] = 'i';
137         scantokey[ 24] = 'o';
138         scantokey[ 25] = 'p';
139         scantokey[ 26] = '[';
140         scantokey[ 27] = ']';
141         scantokey[ 28] = K_ENTER;
142         scantokey[ 29] = K_CTRL;        /*left */
143         scantokey[ 30] = 'a';
144         scantokey[ 31] = 's';
145         scantokey[ 32] = 'd';
146         scantokey[ 33] = 'f';
147         scantokey[ 34] = 'g';
148         scantokey[ 35] = 'h';
149         scantokey[ 36] = 'j';
150         scantokey[ 37] = 'k';
151         scantokey[ 38] = 'l';
152         scantokey[ 39] = ';';
153         scantokey[ 40] = '\'';
154         scantokey[ 41] = '`';
155         scantokey[ 42] = K_SHIFT;       /*left */
156         scantokey[ 43] = '\\';
157         scantokey[ 44] = 'z';
158         scantokey[ 45] = 'x';
159         scantokey[ 46] = 'c';
160         scantokey[ 47] = 'v';
161         scantokey[ 48] = 'b';
162         scantokey[ 49] = 'n';
163         scantokey[ 50] = 'm';
164         scantokey[ 51] = ',';
165         scantokey[ 52] = '.';
166         scantokey[ 53] = '/';
167         scantokey[ 54] = K_SHIFT;       /*right */
168         scantokey[ 55] = KP_MULTIPLY;
169         scantokey[ 56] = K_ALT;         /*left */
170         scantokey[ 57] = ' ';
171         scantokey[ 58] = K_CAPSLOCK;
172         scantokey[ 59] = K_F1;
173         scantokey[ 60] = K_F2;
174         scantokey[ 61] = K_F3;
175         scantokey[ 62] = K_F4;
176         scantokey[ 63] = K_F5;
177         scantokey[ 64] = K_F6;
178         scantokey[ 65] = K_F7;
179         scantokey[ 66] = K_F8;
180         scantokey[ 67] = K_F9;
181         scantokey[ 68] = K_F10;
182         scantokey[ 69] = KP_NUMLCK;
183         scantokey[ 70] = K_SCRLCK;
184         scantokey[ 71] = KP_HOME;
185         scantokey[ 72] = KP_UPARROW;
186         scantokey[ 73] = KP_PGUP;
187         scantokey[ 74] = KP_MINUS;
188         scantokey[ 75] = KP_LEFTARROW;
189         scantokey[ 76] = KP_5;
190         scantokey[ 77] = KP_RIGHTARROW;
191         scantokey[ 79] = KP_END;
192         scantokey[ 78] = KP_PLUS;
193         scantokey[ 80] = KP_DOWNARROW;
194         scantokey[ 81] = KP_PGDN;
195         scantokey[ 82] = KP_INS;
196         scantokey[ 83] = KP_DEL;
197         /* 84 to 86 not used */
198         scantokey[ 87] = K_F11;
199         scantokey[ 88] = K_F12;
200         /* 89 to 95 not used */
201         scantokey[ 96] = KP_ENTER;      /* keypad enter */
202         scantokey[ 97] = K_CTRL;        /* right */
203         scantokey[ 98] = KP_DIVIDE;
204         scantokey[ 99] = K_PRNTSCR;     /* print screen */
205         scantokey[100] = K_ALT;         /* right */
206
207         scantokey[101] = K_PAUSE;       /* break */
208         scantokey[102] = K_HOME;
209         scantokey[103] = K_UPARROW;
210         scantokey[104] = K_PGUP;
211         scantokey[105] = K_LEFTARROW;
212         scantokey[106] = K_RIGHTARROW;
213         scantokey[107] = K_END;
214         scantokey[108] = K_DOWNARROW;
215         scantokey[109] = K_PGDN;
216         scantokey[110] = K_INS;
217         scantokey[111] = K_DEL;
218         scantokey[119] = K_PAUSE;
219
220         if (keyboard_init()) {
221                 Sys_Error("keyboard_init() failed");
222         }
223         keyboard_seteventhandler(keyhandler);
224 }
225
226 static void IN_init_mouse(void)
227 {
228         int mtype;
229         char *mousedev;
230         int mouserate = MOUSE_DEFAULTSAMPLERATE;
231
232         mouse_buttons = 3;
233
234         mtype = vga_getmousetype();
235
236         mousedev = "/dev/mouse";
237         if (getenv("MOUSEDEV")) mousedev = getenv("MOUSEDEV");
238         if (COM_CheckParm("-mdev")) {
239                 mousedev = com_argv[COM_CheckParm("-mdev")+1];
240         }
241
242         if (getenv("MOUSERATE")) mouserate = atoi(getenv("MOUSERATE"));
243         if (COM_CheckParm("-mrate")) {
244                 mouserate = atoi(com_argv[COM_CheckParm("-mrate")+1]);
245         }
246
247         if (mouse_init(mousedev, mtype, mouserate)) {
248                 Con_Printf("No mouse found\n");
249                 UseMouse = 0;
250         } else{
251                 mouse_seteventhandler((void*)mousehandler);
252         }
253 }
254
255 void IN_Shutdown(void)
256 {
257         Con_Printf("IN_Shutdown\n");
258
259         if (UseMouse)
260                 mouse_close();
261         if (UseKeyboard)
262                 keyboard_close();
263         in_svgalib_inited = 0;
264 }
265
266
267 void Sys_SendKeyEvents(void)
268 {
269         if (!in_svgalib_inited) return;
270
271         if (UseKeyboard) {
272                 while ((keyboard_update()));
273         }
274 }
275
276
277 void IN_Commands(void)
278 {
279         if (UseMouse)
280         {
281                 /* Poll mouse values */
282                 while (mouse_update())
283                         ;
284
285                 /* Perform button actions */
286                 if ((mouse_buttonstate & MOUSE_LEFTBUTTON) &&
287                         !(mouse_oldbuttonstate & MOUSE_LEFTBUTTON))
288                         Key_Event (K_MOUSE1, true);
289                 else if (!(mouse_buttonstate & MOUSE_LEFTBUTTON) &&
290                         (mouse_oldbuttonstate & MOUSE_LEFTBUTTON))
291                         Key_Event (K_MOUSE1, false);
292
293                 if ((mouse_buttonstate & MOUSE_RIGHTBUTTON) &&
294                         !(mouse_oldbuttonstate & MOUSE_RIGHTBUTTON))
295                         Key_Event (K_MOUSE2, true);
296                 else if (!(mouse_buttonstate & MOUSE_RIGHTBUTTON) &&
297                         (mouse_oldbuttonstate & MOUSE_RIGHTBUTTON))
298                         Key_Event (K_MOUSE2, false);
299
300                 if ((mouse_buttonstate & MOUSE_MIDDLEBUTTON) &&
301                         !(mouse_oldbuttonstate & MOUSE_MIDDLEBUTTON))
302                         Key_Event (K_MOUSE3, true);
303                 else if (!(mouse_buttonstate & MOUSE_MIDDLEBUTTON) &&
304                         (mouse_oldbuttonstate & MOUSE_MIDDLEBUTTON))
305                         Key_Event (K_MOUSE3, false);
306
307                 mouse_oldbuttonstate = mouse_buttonstate;
308         }
309 }
310
311
312 void IN_Move(usercmd_t *cmd)
313 {
314         if (!UseMouse)
315                 return;
316
317         /* Poll mouse values */
318         while (mouse_update())
319                 ;
320
321         if (key_dest != key_game)
322                 ui_mouseupdaterelative(uimx, uimy);
323         else
324                 IN_Mouse(cmd, mx, my);
325         mx = 0;
326         my = 0;
327         uimx = 0;
328         uimy = 0;
329 }
330
331 void IN_HandlePause (qboolean pause)
332 {
333 }
334