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