src/video/vgl/SDL_vglevents.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 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 */
    42 static keymap_t *vga_keymap = NULL;
    43 static SDLKey keymap[128];
    44 static SDL_keysym *TranslateKey(int scancode, SDL_keysym * keysym);
    45 
    46 static int posted = 0;
    47 static int oldx = -1;
    48 static int oldy = -1;
    49 static 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  */
    56 int
    57 VGL_initkeymaps(int fd)
    58 {
    59     vga_keymap = SDL_malloc(sizeof(keymap_t));
    60     if (!vga_keymap) {
    61         SDL_OutOfMemory();
    62         return (-1);
    63     }
    64     if (ioctl(fd, GIO_KEYMAP, vga_keymap) == -1) {
    65         SDL_free(vga_keymap);
    66         vga_keymap = NULL;
    67         SDL_SetError("Unable to get keyboard map");
    68         return (-1);
    69     }
    70     return (0);
    71 }
    72 
    73 static void
    74 handle_keyboard(_THIS)
    75 {
    76     SDL_keysym keysym;
    77     int c, pressed, scancode;
    78 
    79     while ((c = VGLKeyboardGetCh()) != 0) {
    80         scancode = c & 0x7F;
    81         if (c & 0x80) {
    82             pressed = SDL_RELEASED;
    83         } else {
    84             pressed = SDL_PRESSED;
    85         }
    86 
    87         posted += SDL_PrivateKeyboard(pressed,
    88                                       TranslateKey(scancode, &keysym));
    89     }
    90 }
    91 
    92 int
    93 VGL_initmouse(int fd)
    94 {
    95     mouseinfo.operation = MOUSE_GETINFO;
    96     if (ioctl(fd, CONS_MOUSECTL, &mouseinfo) != 0)
    97         return -1;
    98 
    99     return 0;
   100 }
   101 
   102 static void
   103 handle_mouse(_THIS)
   104 {
   105     char buttons;
   106     int x, y;
   107     int button_state, state_changed, state;
   108     int i;
   109 
   110     ioctl(0, CONS_MOUSECTL, &mouseinfo);
   111     x = mouseinfo.u.data.x;
   112     y = mouseinfo.u.data.y;
   113     buttons = mouseinfo.u.data.buttons;
   114 
   115     if ((x != oldx) || (y != oldy)) {
   116         posted += SDL_PrivateMouseMotion(0, 0, x, y);
   117         oldx = x;
   118         oldy = y;
   119     }
   120 
   121     /* See what's changed */
   122     button_state = SDL_GetMouseState(NULL, NULL);
   123     state_changed = button_state ^ buttons;
   124     for (i = 0; i < 8; i++) {
   125         if (state_changed & (1 << i)) {
   126             if (buttons & (1 << i)) {
   127                 state = SDL_PRESSED;
   128             } else {
   129                 state = SDL_RELEASED;
   130             }
   131             posted += SDL_PrivateMouseButton(state, i + 1, 0, 0);
   132         }
   133     }
   134 }
   135 
   136 
   137 void
   138 VGL_PumpEvents(_THIS)
   139 {
   140     do {
   141         posted = 0;
   142         handle_keyboard(this);
   143         handle_mouse(this);
   144     }
   145     while (posted != 0);
   146 }
   147 
   148 void
   149 VGL_InitOSKeymap(_THIS)
   150 {
   151     int i;
   152 
   153     /* Initialize the BeOS key translation table */
   154     for (i = 0; i < SDL_arraysize(keymap); ++i)
   155         keymap[i] = SDLK_UNKNOWN;
   156 
   157     keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
   158     keymap[SCANCODE_1] = SDLK_1;
   159     keymap[SCANCODE_2] = SDLK_2;
   160     keymap[SCANCODE_3] = SDLK_3;
   161     keymap[SCANCODE_4] = SDLK_4;
   162     keymap[SCANCODE_5] = SDLK_5;
   163     keymap[SCANCODE_6] = SDLK_6;
   164     keymap[SCANCODE_7] = SDLK_7;
   165     keymap[SCANCODE_8] = SDLK_8;
   166     keymap[SCANCODE_9] = SDLK_9;
   167     keymap[SCANCODE_0] = SDLK_0;
   168     keymap[SCANCODE_MINUS] = SDLK_MINUS;
   169     keymap[SCANCODE_EQUAL] = SDLK_EQUALS;
   170     keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
   171     keymap[SCANCODE_TAB] = SDLK_TAB;
   172     keymap[SCANCODE_Q] = SDLK_q;
   173     keymap[SCANCODE_W] = SDLK_w;
   174     keymap[SCANCODE_E] = SDLK_e;
   175     keymap[SCANCODE_R] = SDLK_r;
   176     keymap[SCANCODE_T] = SDLK_t;
   177     keymap[SCANCODE_Y] = SDLK_y;
   178     keymap[SCANCODE_U] = SDLK_u;
   179     keymap[SCANCODE_I] = SDLK_i;
   180     keymap[SCANCODE_O] = SDLK_o;
   181     keymap[SCANCODE_P] = SDLK_p;
   182     keymap[SCANCODE_BRACKET_LEFT] = SDLK_LEFTBRACKET;
   183     keymap[SCANCODE_BRACKET_RIGHT] = SDLK_RIGHTBRACKET;
   184     keymap[SCANCODE_ENTER] = SDLK_RETURN;
   185     keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
   186     keymap[SCANCODE_A] = SDLK_a;
   187     keymap[SCANCODE_S] = SDLK_s;
   188     keymap[SCANCODE_D] = SDLK_d;
   189     keymap[SCANCODE_F] = SDLK_f;
   190     keymap[SCANCODE_G] = SDLK_g;
   191     keymap[SCANCODE_H] = SDLK_h;
   192     keymap[SCANCODE_J] = SDLK_j;
   193     keymap[SCANCODE_K] = SDLK_k;
   194     keymap[SCANCODE_L] = SDLK_l;
   195     keymap[SCANCODE_SEMICOLON] = SDLK_SEMICOLON;
   196     keymap[SCANCODE_APOSTROPHE] = SDLK_QUOTE;
   197     keymap[SCANCODE_GRAVE] = SDLK_BACKQUOTE;
   198     keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
   199     keymap[SCANCODE_BACKSLASH] = SDLK_BACKSLASH;
   200     keymap[SCANCODE_Z] = SDLK_z;
   201     keymap[SCANCODE_X] = SDLK_x;
   202     keymap[SCANCODE_C] = SDLK_c;
   203     keymap[SCANCODE_V] = SDLK_v;
   204     keymap[SCANCODE_B] = SDLK_b;
   205     keymap[SCANCODE_N] = SDLK_n;
   206     keymap[SCANCODE_M] = SDLK_m;
   207     keymap[SCANCODE_COMMA] = SDLK_COMMA;
   208     keymap[SCANCODE_PERIOD] = SDLK_PERIOD;
   209     keymap[SCANCODE_SLASH] = SDLK_SLASH;
   210     keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
   211     keymap[SCANCODE_KEYPADMULTIPLY] = SDLK_KP_MULTIPLY;
   212     keymap[SCANCODE_LEFTALT] = SDLK_LALT;
   213     keymap[SCANCODE_SPACE] = SDLK_SPACE;
   214     keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
   215     keymap[SCANCODE_F1] = SDLK_F1;
   216     keymap[SCANCODE_F2] = SDLK_F2;
   217     keymap[SCANCODE_F3] = SDLK_F3;
   218     keymap[SCANCODE_F4] = SDLK_F4;
   219     keymap[SCANCODE_F5] = SDLK_F5;
   220     keymap[SCANCODE_F6] = SDLK_F6;
   221     keymap[SCANCODE_F7] = SDLK_F7;
   222     keymap[SCANCODE_F8] = SDLK_F8;
   223     keymap[SCANCODE_F9] = SDLK_F9;
   224     keymap[SCANCODE_F10] = SDLK_F10;
   225     keymap[SCANCODE_NUMLOCK] = SDLK_NUMLOCK;
   226     keymap[SCANCODE_SCROLLLOCK] = SDLK_SCROLLOCK;
   227     keymap[SCANCODE_KEYPAD7] = SDLK_KP7;
   228     keymap[SCANCODE_CURSORUPLEFT] = SDLK_KP7;
   229     keymap[SCANCODE_KEYPAD8] = SDLK_KP8;
   230     keymap[SCANCODE_CURSORUP] = SDLK_KP8;
   231     keymap[SCANCODE_KEYPAD9] = SDLK_KP9;
   232     keymap[SCANCODE_CURSORUPRIGHT] = SDLK_KP9;
   233     keymap[SCANCODE_KEYPADMINUS] = SDLK_KP_MINUS;
   234     keymap[SCANCODE_KEYPAD4] = SDLK_KP4;
   235     keymap[SCANCODE_CURSORLEFT] = SDLK_KP4;
   236     keymap[SCANCODE_KEYPAD5] = SDLK_KP5;
   237     keymap[SCANCODE_KEYPAD6] = SDLK_KP6;
   238     keymap[SCANCODE_CURSORRIGHT] = SDLK_KP6;
   239     keymap[SCANCODE_KEYPADPLUS] = SDLK_KP_PLUS;
   240     keymap[SCANCODE_KEYPAD1] = SDLK_KP1;
   241     keymap[SCANCODE_CURSORDOWNLEFT] = SDLK_KP1;
   242     keymap[SCANCODE_KEYPAD2] = SDLK_KP2;
   243     keymap[SCANCODE_CURSORDOWN] = SDLK_KP2;
   244     keymap[SCANCODE_KEYPAD3] = SDLK_KP3;
   245     keymap[SCANCODE_CURSORDOWNRIGHT] = SDLK_KP3;
   246     keymap[SCANCODE_KEYPAD0] = SDLK_KP0;
   247     keymap[SCANCODE_KEYPADPERIOD] = SDLK_KP_PERIOD;
   248     keymap[SCANCODE_LESS] = SDLK_LESS;
   249     keymap[SCANCODE_F11] = SDLK_F11;
   250     keymap[SCANCODE_F12] = SDLK_F12;
   251     keymap[SCANCODE_KEYPADENTER] = SDLK_KP_ENTER;
   252     keymap[SCANCODE_RIGHTCONTROL] = SDLK_RCTRL;
   253     keymap[SCANCODE_CONTROL] = SDLK_RCTRL;
   254     keymap[SCANCODE_KEYPADDIVIDE] = SDLK_KP_DIVIDE;
   255     keymap[SCANCODE_PRINTSCREEN] = SDLK_PRINT;
   256     keymap[SCANCODE_RIGHTALT] = SDLK_RALT;
   257     keymap[SCANCODE_BREAK] = SDLK_BREAK;
   258     keymap[SCANCODE_BREAK_ALTERNATIVE] = SDLK_UNKNOWN;
   259     keymap[SCANCODE_HOME] = SDLK_HOME;
   260     keymap[SCANCODE_CURSORBLOCKUP] = SDLK_UP;
   261     keymap[SCANCODE_PAGEUP] = SDLK_PAGEUP;
   262     keymap[SCANCODE_CURSORBLOCKLEFT] = SDLK_LEFT;
   263     keymap[SCANCODE_CURSORBLOCKRIGHT] = SDLK_RIGHT;
   264     keymap[SCANCODE_END] = SDLK_END;
   265     keymap[SCANCODE_CURSORBLOCKDOWN] = SDLK_DOWN;
   266     keymap[SCANCODE_PAGEDOWN] = SDLK_PAGEDOWN;
   267     keymap[SCANCODE_INSERT] = SDLK_INSERT;
   268     keymap[SCANCODE_REMOVE] = SDLK_DELETE;
   269     keymap[119] = SDLK_PAUSE;
   270     keymap[SCANCODE_RIGHTWIN] = SDLK_RSUPER;
   271     keymap[SCANCODE_LEFTWIN] = SDLK_LSUPER;
   272     keymap[127] = SDLK_MENU;
   273 }
   274 
   275 static SDL_keysym *
   276 TranslateKey(int scancode, SDL_keysym * keysym)
   277 {
   278     /* Set the keysym information */
   279     keysym->scancode = scancode;
   280     keysym->sym = keymap[scancode];
   281     keysym->mod = KMOD_NONE;
   282 
   283     /* If UNICODE is on, get the UNICODE value for the key */
   284     keysym->unicode = 0;
   285     if (SDL_TranslateUNICODE && vga_keymap) {
   286         int map;
   287         SDLMod modstate;
   288 
   289         modstate = SDL_GetModState();
   290         map = 0;
   291         if (modstate & KMOD_SHIFT) {
   292             map += 1;
   293         }
   294         if (modstate & KMOD_CTRL) {
   295             map += 2;
   296         }
   297         if (modstate & KMOD_ALT) {
   298             map += 4;
   299         }
   300         if (!(vga_keymap->key[scancode].spcl & (0x80 >> map))) {
   301             keysym->unicode = vga_keymap->key[scancode].map[map];
   302         }
   303 
   304     }
   305     return (keysym);
   306 }
   307 
   308 /* vi: set ts=4 sw=4 expandtab: */