1/* 2 SDL - Simple DirectMedia Layer 3 Copyright (C) 1997-2012 Sam Lantinga 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 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 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 19 Sam Lantinga 20 slouken@libsdl.org 21*/ 22#include "SDL_config.h" 23 24/* Handle the event stream, converting X11 events into SDL events */ 25 26#include <stdio.h> 27 28#include <sys/fbio.h> 29#include <sys/consio.h> 30#include <sys/kbio.h> 31#include <vgl.h> 32 33#include "SDL.h" 34#include "SDL_thread.h" 35#include "../../events/SDL_sysevents.h" 36#include "../../events/SDL_events_c.h" 37#include "SDL_vglvideo.h" 38#include "SDL_vglevents_c.h" 39 40/* The translation tables from a console scancode to a SDL keysym */ 41/* FIXME: Free the keymap when we shut down the video mode */ 42static keymap_t *vga_keymap = NULL; 43static SDLKey keymap[128]; 44static SDL_keysym *TranslateKey(int scancode, SDL_keysym *keysym); 45 46static int posted = 0; 47static int oldx = -1; 48static int oldy = -1; 49static struct mouse_info mouseinfo; 50 51/* Ugh, we have to duplicate the kernel's keysym mapping code... 52 Oh, it's not so bad. :-) 53 54 FIXME: Add keyboard LED handling code 55 */ 56int VGL_initkeymaps(int fd) 57{ 58 vga_keymap = SDL_malloc(sizeof(keymap_t)); 59 if ( ! vga_keymap ) { 60 SDL_OutOfMemory(); 61 return(-1); 62 } 63 if (ioctl(fd, GIO_KEYMAP, vga_keymap) == -1) { 64 SDL_free(vga_keymap); 65 vga_keymap = NULL; 66 SDL_SetError("Unable to get keyboard map"); 67 return(-1); 68 } 69 return(0); 70} 71 72static void handle_keyboard(_THIS) 73{ 74 SDL_keysym keysym; 75 int c, pressed, scancode; 76 77 while ((c = VGLKeyboardGetCh()) != 0) { 78 scancode = c & 0x7F; 79 if (c & 0x80) { 80 pressed = SDL_RELEASED; 81 } else { 82 pressed = SDL_PRESSED; 83 } 84 85 posted += SDL_PrivateKeyboard(pressed, 86 TranslateKey(scancode, &keysym)); 87 } 88} 89 90int VGL_initmouse(int fd) 91{ 92 mouseinfo.operation = MOUSE_GETINFO; 93 if (ioctl(fd, CONS_MOUSECTL, &mouseinfo) != 0) 94 return -1; 95 96 return 0; 97} 98 99static void handle_mouse(_THIS) 100{ 101 char buttons; 102 int x, y; 103 int button_state, state_changed, state; 104 int i; 105 106 ioctl(0, CONS_MOUSECTL, &mouseinfo); 107 x = mouseinfo.u.data.x; 108 y = mouseinfo.u.data.y; 109 buttons = mouseinfo.u.data.buttons; 110 111 if ((x != oldx) || (y != oldy)) { 112 posted += SDL_PrivateMouseMotion(0, 0, x, y); 113 oldx = x; 114 oldy = y; 115 } 116 117 /* See what's changed */ 118 button_state = SDL_GetMouseState(NULL, NULL); 119 state_changed = button_state ^ buttons; 120 for (i = 0; i < 8; i++) { 121 if (state_changed & (1<<i)) { 122 if (buttons & (1<<i)) { 123 state = SDL_PRESSED; 124 } else { 125 state = SDL_RELEASED; 126 } 127 posted += SDL_PrivateMouseButton(state, i + 1, 0, 0); 128 } 129 } 130} 131 132 133void VGL_PumpEvents(_THIS) 134{ 135 do { 136 posted = 0; 137 handle_keyboard(this); 138 handle_mouse(this); 139 } while (posted != 0); 140} 141 142void VGL_InitOSKeymap(_THIS) 143{ 144 int i; 145 146 /* Initialize the BeOS key translation table */ 147 for ( i=0; i<SDL_arraysize(keymap); ++i ) 148 keymap[i] = SDLK_UNKNOWN; 149 150 keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE; 151 keymap[SCANCODE_1] = SDLK_1; 152 keymap[SCANCODE_2] = SDLK_2; 153 keymap[SCANCODE_3] = SDLK_3; 154 keymap[SCANCODE_4] = SDLK_4; 155 keymap[SCANCODE_5] = SDLK_5; 156 keymap[SCANCODE_6] = SDLK_6; 157 keymap[SCANCODE_7] = SDLK_7; 158 keymap[SCANCODE_8] = SDLK_8; 159 keymap[SCANCODE_9] = SDLK_9; 160 keymap[SCANCODE_0] = SDLK_0; 161 keymap[SCANCODE_MINUS] = SDLK_MINUS; 162 keymap[SCANCODE_EQUAL] = SDLK_EQUALS; 163 keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE; 164 keymap[SCANCODE_TAB] = SDLK_TAB; 165 keymap[SCANCODE_Q] = SDLK_q; 166 keymap[SCANCODE_W] = SDLK_w; 167 keymap[SCANCODE_E] = SDLK_e; 168 keymap[SCANCODE_R] = SDLK_r; 169 keymap[SCANCODE_T] = SDLK_t; 170 keymap[SCANCODE_Y] = SDLK_y; 171 keymap[SCANCODE_U] = SDLK_u; 172 keymap[SCANCODE_I] = SDLK_i; 173 keymap[SCANCODE_O] = SDLK_o; 174 keymap[SCANCODE_P] = SDLK_p; 175 keymap[SCANCODE_BRACKET_LEFT] = SDLK_LEFTBRACKET; 176 keymap[SCANCODE_BRACKET_RIGHT] = SDLK_RIGHTBRACKET; 177 keymap[SCANCODE_ENTER] = SDLK_RETURN; 178 keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL; 179 keymap[SCANCODE_A] = SDLK_a; 180 keymap[SCANCODE_S] = SDLK_s; 181 keymap[SCANCODE_D] = SDLK_d; 182 keymap[SCANCODE_F] = SDLK_f; 183 keymap[SCANCODE_G] = SDLK_g; 184 keymap[SCANCODE_H] = SDLK_h; 185 keymap[SCANCODE_J] = SDLK_j; 186 keymap[SCANCODE_K] = SDLK_k; 187 keymap[SCANCODE_L] = SDLK_l; 188 keymap[SCANCODE_SEMICOLON] = SDLK_SEMICOLON; 189 keymap[SCANCODE_APOSTROPHE] = SDLK_QUOTE; 190 keymap[SCANCODE_GRAVE] = SDLK_BACKQUOTE; 191 keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT; 192 keymap[SCANCODE_BACKSLASH] = SDLK_BACKSLASH; 193 keymap[SCANCODE_Z] = SDLK_z; 194 keymap[SCANCODE_X] = SDLK_x; 195 keymap[SCANCODE_C] = SDLK_c; 196 keymap[SCANCODE_V] = SDLK_v; 197 keymap[SCANCODE_B] = SDLK_b; 198 keymap[SCANCODE_N] = SDLK_n; 199 keymap[SCANCODE_M] = SDLK_m; 200 keymap[SCANCODE_COMMA] = SDLK_COMMA; 201 keymap[SCANCODE_PERIOD] = SDLK_PERIOD; 202 keymap[SCANCODE_SLASH] = SDLK_SLASH; 203 keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT; 204 keymap[SCANCODE_KEYPADMULTIPLY] = SDLK_KP_MULTIPLY; 205 keymap[SCANCODE_LEFTALT] = SDLK_LALT; 206 keymap[SCANCODE_SPACE] = SDLK_SPACE; 207 keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK; 208 keymap[SCANCODE_F1] = SDLK_F1; 209 keymap[SCANCODE_F2] = SDLK_F2; 210 keymap[SCANCODE_F3] = SDLK_F3; 211 keymap[SCANCODE_F4] = SDLK_F4; 212 keymap[SCANCODE_F5] = SDLK_F5; 213 keymap[SCANCODE_F6] = SDLK_F6; 214 keymap[SCANCODE_F7] = SDLK_F7; 215 keymap[SCANCODE_F8] = SDLK_F8; 216 keymap[SCANCODE_F9] = SDLK_F9; 217 keymap[SCANCODE_F10] = SDLK_F10; 218 keymap[SCANCODE_NUMLOCK] = SDLK_NUMLOCK; 219 keymap[SCANCODE_SCROLLLOCK] = SDLK_SCROLLOCK; 220 keymap[SCANCODE_KEYPAD7] = SDLK_KP7; 221 keymap[SCANCODE_CURSORUPLEFT] = SDLK_KP7; 222 keymap[SCANCODE_KEYPAD8] = SDLK_KP8; 223 keymap[SCANCODE_CURSORUP] = SDLK_KP8; 224 keymap[SCANCODE_KEYPAD9] = SDLK_KP9; 225 keymap[SCANCODE_CURSORUPRIGHT] = SDLK_KP9; 226 keymap[SCANCODE_KEYPADMINUS] = SDLK_KP_MINUS; 227 keymap[SCANCODE_KEYPAD4] = SDLK_KP4; 228 keymap[SCANCODE_CURSORLEFT] = SDLK_KP4; 229 keymap[SCANCODE_KEYPAD5] = SDLK_KP5; 230 keymap[SCANCODE_KEYPAD6] = SDLK_KP6; 231 keymap[SCANCODE_CURSORRIGHT] = SDLK_KP6; 232 keymap[SCANCODE_KEYPADPLUS] = SDLK_KP_PLUS; 233 keymap[SCANCODE_KEYPAD1] = SDLK_KP1; 234 keymap[SCANCODE_CURSORDOWNLEFT] = SDLK_KP1; 235 keymap[SCANCODE_KEYPAD2] = SDLK_KP2; 236 keymap[SCANCODE_CURSORDOWN] = SDLK_KP2; 237 keymap[SCANCODE_KEYPAD3] = SDLK_KP3; 238 keymap[SCANCODE_CURSORDOWNRIGHT] = SDLK_KP3; 239 keymap[SCANCODE_KEYPAD0] = SDLK_KP0; 240 keymap[SCANCODE_KEYPADPERIOD] = SDLK_KP_PERIOD; 241 keymap[SCANCODE_LESS] = SDLK_LESS; 242 keymap[SCANCODE_F11] = SDLK_F11; 243 keymap[SCANCODE_F12] = SDLK_F12; 244 keymap[SCANCODE_KEYPADENTER] = SDLK_KP_ENTER; 245 keymap[SCANCODE_RIGHTCONTROL] = SDLK_RCTRL; 246 keymap[SCANCODE_CONTROL] = SDLK_RCTRL; 247 keymap[SCANCODE_KEYPADDIVIDE] = SDLK_KP_DIVIDE; 248 keymap[SCANCODE_PRINTSCREEN] = SDLK_PRINT; 249 keymap[SCANCODE_RIGHTALT] = SDLK_RALT; 250 keymap[SCANCODE_BREAK] = SDLK_BREAK; 251 keymap[SCANCODE_BREAK_ALTERNATIVE] = SDLK_UNKNOWN; 252 keymap[SCANCODE_HOME] = SDLK_HOME; 253 keymap[SCANCODE_CURSORBLOCKUP] = SDLK_UP; 254 keymap[SCANCODE_PAGEUP] = SDLK_PAGEUP; 255 keymap[SCANCODE_CURSORBLOCKLEFT] = SDLK_LEFT; 256 keymap[SCANCODE_CURSORBLOCKRIGHT] = SDLK_RIGHT; 257 keymap[SCANCODE_END] = SDLK_END; 258 keymap[SCANCODE_CURSORBLOCKDOWN] = SDLK_DOWN; 259 keymap[SCANCODE_PAGEDOWN] = SDLK_PAGEDOWN; 260 keymap[SCANCODE_INSERT] = SDLK_INSERT; 261 keymap[SCANCODE_REMOVE] = SDLK_DELETE; 262 keymap[119] = SDLK_PAUSE; 263 keymap[SCANCODE_RIGHTWIN] = SDLK_RSUPER; 264 keymap[SCANCODE_LEFTWIN] = SDLK_LSUPER; 265 keymap[127] = SDLK_MENU; 266} 267 268static SDL_keysym *TranslateKey(int scancode, SDL_keysym *keysym) 269{ 270 /* Set the keysym information */ 271 keysym->scancode = scancode; 272 keysym->sym = keymap[scancode]; 273 keysym->mod = KMOD_NONE; 274 275 /* If UNICODE is on, get the UNICODE value for the key */ 276 keysym->unicode = 0; 277 if ( SDL_TranslateUNICODE && vga_keymap ) { 278 int map; 279 SDLMod modstate; 280 281 modstate = SDL_GetModState(); 282 map = 0; 283 if ( modstate & KMOD_SHIFT ) { 284 map += 1; 285 } 286 if ( modstate & KMOD_CTRL ) { 287 map += 2; 288 } 289 if ( modstate & KMOD_ALT ) { 290 map += 4; 291 } 292 if ( !(vga_keymap->key[scancode].spcl & (0x80 >> map)) ) { 293 keysym->unicode = vga_keymap->key[scancode].map[map]; 294 } 295 296 } 297 return(keysym); 298} 299 300