comparison src/video/svga/SDL_svgaevents.c @ 0:74212992fb08

Initial revision
author Sam Lantinga <slouken@lokigames.com>
date Thu, 26 Apr 2001 16:45:43 +0000
parents
children b0ae59d0f3ee
comparison
equal deleted inserted replaced
-1:000000000000 0:74212992fb08
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 Sam Lantinga
20 slouken@devolution.com
21 */
22
23 #ifdef SAVE_RCSID
24 static char rcsid =
25 "@(#) $Id$";
26 #endif
27
28 /* Handle the event stream, converting X11 events into SDL events */
29
30 #include <stdio.h>
31
32 #include <vga.h>
33 #include <vgamouse.h>
34 #include <vgakeyboard.h>
35 #if defined(linux)
36 #include <linux/kd.h>
37 #include <linux/keyboard.h>
38 #elif defined(__FreeBSD__)
39 #include <sys/kbio.h>
40 #else
41 #error You must choose your operating system here
42 #endif
43
44 #include "SDL.h"
45 #include "SDL_sysevents.h"
46 #include "SDL_events_c.h"
47 #include "SDL_svgavideo.h"
48 #include "SDL_svgaevents_c.h"
49
50 /* The translation tables from a console scancode to a SDL keysym */
51 #if defined(linux)
52 #define NUM_VGAKEYMAPS (1<<KG_CAPSSHIFT)
53 static Uint16 vga_keymap[NUM_VGAKEYMAPS][NR_KEYS];
54 #elif defined(__FreeBSD__)
55 /* FIXME: Free the keymap when we shut down the video mode */
56 static keymap_t *vga_keymap = NULL;
57 #else
58 #error You must choose your operating system here
59 #endif
60 static SDLKey keymap[128];
61 static SDL_keysym *TranslateKey(int scancode, SDL_keysym *keysym);
62
63 /* Ugh, we have to duplicate the kernel's keysym mapping code...
64 Oh, it's not so bad. :-)
65
66 FIXME: Add keyboard LED handling code
67 */
68 #if defined(linux)
69 int SVGA_initkeymaps(int fd)
70 {
71 struct kbentry entry;
72 int map, i;
73
74 /* Load all the keysym mappings */
75 for ( map=0; map<NUM_VGAKEYMAPS; ++map ) {
76 memset(vga_keymap[map], 0, NR_KEYS*sizeof(Uint16));
77 for ( i=0; i<NR_KEYS; ++i ) {
78 entry.kb_table = map;
79 entry.kb_index = i;
80 if ( ioctl(fd, KDGKBENT, &entry) == 0 ) {
81 /* The "Enter" key is a special case */
82 if ( entry.kb_value == K_ENTER ) {
83 entry.kb_value = K(KT_ASCII,13);
84 }
85 /* Handle numpad specially as well */
86 if ( KTYP(entry.kb_value) == KT_PAD ) {
87 switch ( entry.kb_value ) {
88 case K_P0:
89 case K_P1:
90 case K_P2:
91 case K_P3:
92 case K_P4:
93 case K_P5:
94 case K_P6:
95 case K_P7:
96 case K_P8:
97 case K_P9:
98 vga_keymap[map][i]=entry.kb_value;
99 vga_keymap[map][i]+= '0';
100 break;
101 case K_PPLUS:
102 vga_keymap[map][i]=K(KT_ASCII,'+');
103 break;
104 case K_PMINUS:
105 vga_keymap[map][i]=K(KT_ASCII,'-');
106 break;
107 case K_PSTAR:
108 vga_keymap[map][i]=K(KT_ASCII,'*');
109 break;
110 case K_PSLASH:
111 vga_keymap[map][i]=K(KT_ASCII,'/');
112 break;
113 case K_PENTER:
114 vga_keymap[map][i]=K(KT_ASCII,'\r');
115 break;
116 case K_PCOMMA:
117 vga_keymap[map][i]=K(KT_ASCII,',');
118 break;
119 case K_PDOT:
120 vga_keymap[map][i]=K(KT_ASCII,'.');
121 break;
122 default:
123 break;
124 }
125 }
126 /* Do the normal key translation */
127 if ( (KTYP(entry.kb_value) == KT_LATIN) ||
128 (KTYP(entry.kb_value) == KT_ASCII) ||
129 (KTYP(entry.kb_value) == KT_LETTER) ) {
130 vga_keymap[map][i] = entry.kb_value;
131 }
132 }
133 }
134 }
135 return(0);
136 }
137 #elif defined(__FreeBSD__)
138 int SVGA_initkeymaps(int fd)
139 {
140 vga_keymap = malloc(sizeof(keymap_t));
141 if ( ! vga_keymap ) {
142 SDL_OutOfMemory();
143 return(-1);
144 }
145 if (ioctl(fd, GIO_KEYMAP, vga_keymap) == -1) {
146 free(vga_keymap);
147 vga_keymap = NULL;
148 SDL_SetError("Unable to get keyboard map");
149 return(-1);
150 }
151 return(0);
152 }
153 #else
154 #error You must choose your operating system here
155 #endif
156
157 int posted = 0;
158
159 void SVGA_mousecallback(int button, int dx, int dy,
160 int u1,int u2,int u3, int u4)
161 {
162 if ( dx || dy ) {
163 posted += SDL_PrivateMouseMotion(0, 1, dx, dy);
164 }
165 if ( button & MOUSE_LEFTBUTTON ) {
166 if ( !(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(1)) ) {
167 posted += SDL_PrivateMouseButton(SDL_PRESSED, 1, 0, 0);
168 }
169 } else {
170 if ( (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(1)) ) {
171 posted += SDL_PrivateMouseButton(SDL_RELEASED, 1, 0, 0);
172 }
173 }
174 if ( button & MOUSE_MIDDLEBUTTON ) {
175 if ( !(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(2)) ) {
176 posted += SDL_PrivateMouseButton(SDL_PRESSED, 2, 0, 0);
177 }
178 } else {
179 if ( (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(2)) ) {
180 posted += SDL_PrivateMouseButton(SDL_RELEASED, 2, 0, 0);
181 }
182 }
183 if ( button & MOUSE_RIGHTBUTTON ) {
184 if ( !(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(3)) ) {
185 posted += SDL_PrivateMouseButton(SDL_PRESSED, 3, 0, 0);
186 }
187 } else {
188 if ( (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(3)) ) {
189 posted += SDL_PrivateMouseButton(SDL_RELEASED, 3, 0, 0);
190 }
191 }
192 }
193
194 void SVGA_keyboardcallback(int scancode, int pressed)
195 {
196 SDL_keysym keysym;
197
198 if ( pressed ) {
199 posted += SDL_PrivateKeyboard(SDL_PRESSED,
200 TranslateKey(scancode, &keysym));
201 } else {
202 posted += SDL_PrivateKeyboard(SDL_RELEASED,
203 TranslateKey(scancode, &keysym));
204 }
205 }
206
207 void SVGA_PumpEvents(_THIS)
208 {
209 do {
210 posted = 0;
211 mouse_update();
212 keyboard_update();
213 } while ( posted );
214 }
215
216 void SVGA_InitOSKeymap(_THIS)
217 {
218 int i;
219
220 /* Initialize the BeOS key translation table */
221 for ( i=0; i<SDL_TABLESIZE(keymap); ++i )
222 keymap[i] = SDLK_UNKNOWN;
223
224 keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
225 keymap[SCANCODE_1] = SDLK_1;
226 keymap[SCANCODE_2] = SDLK_2;
227 keymap[SCANCODE_3] = SDLK_3;
228 keymap[SCANCODE_4] = SDLK_4;
229 keymap[SCANCODE_5] = SDLK_5;
230 keymap[SCANCODE_6] = SDLK_6;
231 keymap[SCANCODE_7] = SDLK_7;
232 keymap[SCANCODE_8] = SDLK_8;
233 keymap[SCANCODE_9] = SDLK_9;
234 keymap[SCANCODE_0] = SDLK_0;
235 keymap[SCANCODE_MINUS] = SDLK_MINUS;
236 keymap[SCANCODE_EQUAL] = SDLK_EQUALS;
237 keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
238 keymap[SCANCODE_TAB] = SDLK_TAB;
239 keymap[SCANCODE_Q] = SDLK_q;
240 keymap[SCANCODE_W] = SDLK_w;
241 keymap[SCANCODE_E] = SDLK_e;
242 keymap[SCANCODE_R] = SDLK_r;
243 keymap[SCANCODE_T] = SDLK_t;
244 keymap[SCANCODE_Y] = SDLK_y;
245 keymap[SCANCODE_U] = SDLK_u;
246 keymap[SCANCODE_I] = SDLK_i;
247 keymap[SCANCODE_O] = SDLK_o;
248 keymap[SCANCODE_P] = SDLK_p;
249 keymap[SCANCODE_BRACKET_LEFT] = SDLK_LEFTBRACKET;
250 keymap[SCANCODE_BRACKET_RIGHT] = SDLK_RIGHTBRACKET;
251 keymap[SCANCODE_ENTER] = SDLK_RETURN;
252 keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
253 keymap[SCANCODE_A] = SDLK_a;
254 keymap[SCANCODE_S] = SDLK_s;
255 keymap[SCANCODE_D] = SDLK_d;
256 keymap[SCANCODE_F] = SDLK_f;
257 keymap[SCANCODE_G] = SDLK_g;
258 keymap[SCANCODE_H] = SDLK_h;
259 keymap[SCANCODE_J] = SDLK_j;
260 keymap[SCANCODE_K] = SDLK_k;
261 keymap[SCANCODE_L] = SDLK_l;
262 keymap[SCANCODE_SEMICOLON] = SDLK_SEMICOLON;
263 keymap[SCANCODE_APOSTROPHE] = SDLK_QUOTE;
264 keymap[SCANCODE_GRAVE] = SDLK_BACKQUOTE;
265 keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
266 keymap[SCANCODE_BACKSLASH] = SDLK_BACKSLASH;
267 keymap[SCANCODE_Z] = SDLK_z;
268 keymap[SCANCODE_X] = SDLK_x;
269 keymap[SCANCODE_C] = SDLK_c;
270 keymap[SCANCODE_V] = SDLK_v;
271 keymap[SCANCODE_B] = SDLK_b;
272 keymap[SCANCODE_N] = SDLK_n;
273 keymap[SCANCODE_M] = SDLK_m;
274 keymap[SCANCODE_COMMA] = SDLK_COMMA;
275 keymap[SCANCODE_PERIOD] = SDLK_PERIOD;
276 keymap[SCANCODE_SLASH] = SDLK_SLASH;
277 keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
278 keymap[SCANCODE_KEYPADMULTIPLY] = SDLK_KP_MULTIPLY;
279 keymap[SCANCODE_LEFTALT] = SDLK_LALT;
280 keymap[SCANCODE_SPACE] = SDLK_SPACE;
281 keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
282 keymap[SCANCODE_F1] = SDLK_F1;
283 keymap[SCANCODE_F2] = SDLK_F2;
284 keymap[SCANCODE_F3] = SDLK_F3;
285 keymap[SCANCODE_F4] = SDLK_F4;
286 keymap[SCANCODE_F5] = SDLK_F5;
287 keymap[SCANCODE_F6] = SDLK_F6;
288 keymap[SCANCODE_F7] = SDLK_F7;
289 keymap[SCANCODE_F8] = SDLK_F8;
290 keymap[SCANCODE_F9] = SDLK_F9;
291 keymap[SCANCODE_F10] = SDLK_F10;
292 keymap[SCANCODE_NUMLOCK] = SDLK_NUMLOCK;
293 keymap[SCANCODE_SCROLLLOCK] = SDLK_SCROLLOCK;
294 keymap[SCANCODE_KEYPAD7] = SDLK_KP7;
295 keymap[SCANCODE_CURSORUPLEFT] = SDLK_KP7;
296 keymap[SCANCODE_KEYPAD8] = SDLK_KP8;
297 keymap[SCANCODE_CURSORUP] = SDLK_KP8;
298 keymap[SCANCODE_KEYPAD9] = SDLK_KP9;
299 keymap[SCANCODE_CURSORUPRIGHT] = SDLK_KP9;
300 keymap[SCANCODE_KEYPADMINUS] = SDLK_KP_MINUS;
301 keymap[SCANCODE_KEYPAD4] = SDLK_KP4;
302 keymap[SCANCODE_CURSORLEFT] = SDLK_KP4;
303 keymap[SCANCODE_KEYPAD5] = SDLK_KP5;
304 keymap[SCANCODE_KEYPAD6] = SDLK_KP6;
305 keymap[SCANCODE_CURSORRIGHT] = SDLK_KP6;
306 keymap[SCANCODE_KEYPADPLUS] = SDLK_KP_PLUS;
307 keymap[SCANCODE_KEYPAD1] = SDLK_KP1;
308 keymap[SCANCODE_CURSORDOWNLEFT] = SDLK_KP1;
309 keymap[SCANCODE_KEYPAD2] = SDLK_KP2;
310 keymap[SCANCODE_CURSORDOWN] = SDLK_KP2;
311 keymap[SCANCODE_KEYPAD3] = SDLK_KP3;
312 keymap[SCANCODE_CURSORDOWNRIGHT] = SDLK_KP3;
313 keymap[SCANCODE_KEYPAD0] = SDLK_KP0;
314 keymap[SCANCODE_KEYPADPERIOD] = SDLK_KP_PERIOD;
315 keymap[SCANCODE_LESS] = SDLK_LESS;
316 keymap[SCANCODE_F11] = SDLK_F11;
317 keymap[SCANCODE_F12] = SDLK_F12;
318 keymap[SCANCODE_KEYPADENTER] = SDLK_KP_ENTER;
319 keymap[SCANCODE_RIGHTCONTROL] = SDLK_RCTRL;
320 keymap[SCANCODE_CONTROL] = SDLK_RCTRL;
321 keymap[SCANCODE_KEYPADDIVIDE] = SDLK_KP_DIVIDE;
322 keymap[SCANCODE_PRINTSCREEN] = SDLK_PRINT;
323 keymap[SCANCODE_RIGHTALT] = SDLK_RALT;
324 keymap[SCANCODE_BREAK] = SDLK_BREAK;
325 keymap[SCANCODE_BREAK_ALTERNATIVE] = SDLK_UNKNOWN;
326 keymap[SCANCODE_HOME] = SDLK_HOME;
327 keymap[SCANCODE_CURSORBLOCKUP] = SDLK_UP;
328 keymap[SCANCODE_PAGEUP] = SDLK_PAGEUP;
329 keymap[SCANCODE_CURSORBLOCKLEFT] = SDLK_LEFT;
330 keymap[SCANCODE_CURSORBLOCKRIGHT] = SDLK_RIGHT;
331 keymap[SCANCODE_END] = SDLK_END;
332 keymap[SCANCODE_CURSORBLOCKDOWN] = SDLK_DOWN;
333 keymap[SCANCODE_PAGEDOWN] = SDLK_PAGEDOWN;
334 keymap[SCANCODE_INSERT] = SDLK_INSERT;
335 keymap[SCANCODE_REMOVE] = SDLK_DELETE;
336 keymap[119] = SDLK_PAUSE;
337 keymap[SCANCODE_RIGHTWIN] = SDLK_RSUPER;
338 keymap[SCANCODE_LEFTWIN] = SDLK_LSUPER;
339 keymap[127] = SDLK_MENU;
340 }
341
342 #if defined(linux)
343 static SDL_keysym *TranslateKey(int scancode, SDL_keysym *keysym)
344 {
345 /* Set the keysym information */
346 keysym->scancode = scancode;
347 keysym->sym = keymap[scancode];
348 keysym->mod = KMOD_NONE;
349
350 /* If UNICODE is on, get the UNICODE value for the key */
351 keysym->unicode = 0;
352 if ( SDL_TranslateUNICODE ) {
353 int map;
354 SDLMod modstate;
355
356 modstate = SDL_GetModState();
357 map = 0;
358 if ( modstate & KMOD_SHIFT ) {
359 map |= (1<<KG_SHIFT);
360 }
361 if ( modstate & KMOD_CTRL ) {
362 map |= (1<<KG_CTRL);
363 }
364 if ( modstate & KMOD_ALT ) {
365 map |= (1<<KG_ALT);
366 }
367 if ( modstate & KMOD_MODE ) {
368 map |= (1<<KG_ALTGR);
369 }
370 if ( KTYP(vga_keymap[map][scancode]) == KT_LETTER ) {
371 if ( modstate & KMOD_CAPS ) {
372 map ^= (1<<KG_SHIFT);
373 }
374 }
375 if ( KTYP(vga_keymap[map][scancode]) == KT_PAD ) {
376 if ( modstate & KMOD_NUM ) {
377 keysym->unicode=KVAL(vga_keymap[map][scancode]);
378 }
379 } else {
380 keysym->unicode = KVAL(vga_keymap[map][scancode]);
381 }
382 }
383 return(keysym);
384 }
385 #elif defined(__FreeBSD__)
386 static SDL_keysym *TranslateKey(int scancode, SDL_keysym *keysym)
387 {
388 /* Set the keysym information */
389 keysym->scancode = scancode;
390 keysym->sym = keymap[scancode];
391 keysym->mod = KMOD_NONE;
392
393 /* If UNICODE is on, get the UNICODE value for the key */
394 keysym->unicode = 0;
395 if ( SDL_TranslateUNICODE && vga_keymap ) {
396 int map;
397 SDLMod modstate;
398
399 modstate = SDL_GetModState();
400 map = 0;
401 if ( modstate & KMOD_SHIFT ) {
402 map += 1;
403 }
404 if ( modstate & KMOD_CTRL ) {
405 map += 2;
406 }
407 if ( modstate & KMOD_ALT ) {
408 map += 4;
409 }
410 if ( !(vga_keymap->key[scancode].spcl & (0x80 >> map)) ) {
411 keysym->unicode = vga_keymap->key[scancode].map[map];
412 }
413
414 }
415 return(keysym);
416 }
417 #else
418 #error You must choose your operating system here
419 #endif