src/video/svga/SDL_svgaevents.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 <vga.h>
    27 #include <vgamouse.h>
    28 #include <vgakeyboard.h>
    29 #if defined(__LINUX__)
    30 #include <linux/kd.h>
    31 #include <linux/keyboard.h>
    32 #elif defined(__FREEBSD__)
    33 #include <sys/kbio.h>
    34 #else
    35 #error You must choose your operating system here
    36 #endif
    37 
    38 #include "../../events/SDL_sysevents.h"
    39 #include "../../events/SDL_events_c.h"
    40 #include "SDL_svgavideo.h"
    41 #include "SDL_svgaevents_c.h"
    42 
    43 /* The translation tables from a console scancode to a SDL keysym */
    44 #if defined(linux)
    45 #define NUM_VGAKEYMAPS	(1<<KG_CAPSSHIFT)
    46 static Uint16 vga_keymap[NUM_VGAKEYMAPS][NR_KEYS];
    47 #elif defined(__FREEBSD__)
    48 /* FIXME: Free the keymap when we shut down the video mode */
    49 static keymap_t *vga_keymap = NULL;
    50 #else
    51 #error You must choose your operating system here
    52 #endif
    53 static SDLKey keymap[128];
    54 static SDL_keysym *TranslateKey(int scancode, SDL_keysym * keysym);
    55 
    56 /* Ugh, we have to duplicate the kernel's keysym mapping code...
    57    Oh, it's not so bad. :-)
    58 
    59    FIXME: Add keyboard LED handling code
    60  */
    61 #if defined(linux)
    62 int
    63 SVGA_initkeymaps(int fd)
    64 {
    65     struct kbentry entry;
    66     int map, i;
    67 
    68     /* Load all the keysym mappings */
    69     for (map = 0; map < NUM_VGAKEYMAPS; ++map) {
    70         SDL_memset(vga_keymap[map], 0, NR_KEYS * sizeof(Uint16));
    71         for (i = 0; i < NR_KEYS; ++i) {
    72             entry.kb_table = map;
    73             entry.kb_index = i;
    74             if (ioctl(fd, KDGKBENT, &entry) == 0) {
    75                 /* The "Enter" key is a special case */
    76                 if (entry.kb_value == K_ENTER) {
    77                     entry.kb_value = K(KT_ASCII, 13);
    78                 }
    79                 /* Handle numpad specially as well */
    80                 if (KTYP(entry.kb_value) == KT_PAD) {
    81                     switch (entry.kb_value) {
    82                     case K_P0:
    83                     case K_P1:
    84                     case K_P2:
    85                     case K_P3:
    86                     case K_P4:
    87                     case K_P5:
    88                     case K_P6:
    89                     case K_P7:
    90                     case K_P8:
    91                     case K_P9:
    92                         vga_keymap[map][i] = entry.kb_value;
    93                         vga_keymap[map][i] += '0';
    94                         break;
    95                     case K_PPLUS:
    96                         vga_keymap[map][i] = K(KT_ASCII, '+');
    97                         break;
    98                     case K_PMINUS:
    99                         vga_keymap[map][i] = K(KT_ASCII, '-');
   100                         break;
   101                     case K_PSTAR:
   102                         vga_keymap[map][i] = K(KT_ASCII, '*');
   103                         break;
   104                     case K_PSLASH:
   105                         vga_keymap[map][i] = K(KT_ASCII, '/');
   106                         break;
   107                     case K_PENTER:
   108                         vga_keymap[map][i] = K(KT_ASCII, '\r');
   109                         break;
   110                     case K_PCOMMA:
   111                         vga_keymap[map][i] = K(KT_ASCII, ',');
   112                         break;
   113                     case K_PDOT:
   114                         vga_keymap[map][i] = K(KT_ASCII, '.');
   115                         break;
   116                     default:
   117                         break;
   118                     }
   119                 }
   120                 /* Do the normal key translation */
   121                 if ((KTYP(entry.kb_value) == KT_LATIN) ||
   122                     (KTYP(entry.kb_value) == KT_ASCII) ||
   123                     (KTYP(entry.kb_value) == KT_LETTER)) {
   124                     vga_keymap[map][i] = entry.kb_value;
   125                 }
   126             }
   127         }
   128     }
   129     return (0);
   130 }
   131 #elif defined(__FREEBSD__)
   132 int
   133 SVGA_initkeymaps(int fd)
   134 {
   135     vga_keymap = SDL_malloc(sizeof(keymap_t));
   136     if (!vga_keymap) {
   137         SDL_OutOfMemory();
   138         return (-1);
   139     }
   140     if (ioctl(fd, GIO_KEYMAP, vga_keymap) == -1) {
   141         SDL_free(vga_keymap);
   142         vga_keymap = NULL;
   143         SDL_SetError("Unable to get keyboard map");
   144         return (-1);
   145     }
   146     return (0);
   147 }
   148 #else
   149 #error You must choose your operating system here
   150 #endif
   151 
   152 int posted = 0;
   153 
   154 void
   155 SVGA_mousecallback(int button, int dx, int dy, int u1, int u2, int u3, int u4)
   156 {
   157     if (dx || dy) {
   158         posted += SDL_PrivateMouseMotion(0, 1, dx, dy);
   159     }
   160     if (button & MOUSE_LEFTBUTTON) {
   161         if (!(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(1))) {
   162             posted += SDL_PrivateMouseButton(SDL_PRESSED, 1, 0, 0);
   163         }
   164     } else {
   165         if ((SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(1))) {
   166             posted += SDL_PrivateMouseButton(SDL_RELEASED, 1, 0, 0);
   167         }
   168     }
   169     if (button & MOUSE_MIDDLEBUTTON) {
   170         if (!(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(2))) {
   171             posted += SDL_PrivateMouseButton(SDL_PRESSED, 2, 0, 0);
   172         }
   173     } else {
   174         if ((SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(2))) {
   175             posted += SDL_PrivateMouseButton(SDL_RELEASED, 2, 0, 0);
   176         }
   177     }
   178     if (button & MOUSE_RIGHTBUTTON) {
   179         if (!(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(3))) {
   180             posted += SDL_PrivateMouseButton(SDL_PRESSED, 3, 0, 0);
   181         }
   182     } else {
   183         if ((SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(3))) {
   184             posted += SDL_PrivateMouseButton(SDL_RELEASED, 3, 0, 0);
   185         }
   186     }
   187 }
   188 
   189 void
   190 SVGA_keyboardcallback(int scancode, int pressed)
   191 {
   192     SDL_keysym keysym;
   193 
   194     if (pressed) {
   195         posted += SDL_PrivateKeyboard(SDL_PRESSED,
   196                                       TranslateKey(scancode, &keysym));
   197     } else {
   198         posted += SDL_PrivateKeyboard(SDL_RELEASED,
   199                                       TranslateKey(scancode, &keysym));
   200     }
   201 }
   202 
   203 void
   204 SVGA_PumpEvents(_THIS)
   205 {
   206     do {
   207         posted = 0;
   208         mouse_update();
   209         keyboard_update();
   210     }
   211     while (posted);
   212 }
   213 
   214 void
   215 SVGA_InitOSKeymap(_THIS)
   216 {
   217     int i;
   218 
   219     /* Initialize the BeOS key translation table */
   220     for (i = 0; i < SDL_arraysize(keymap); ++i)
   221         keymap[i] = SDLK_UNKNOWN;
   222 
   223     keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
   224     keymap[SCANCODE_1] = SDLK_1;
   225     keymap[SCANCODE_2] = SDLK_2;
   226     keymap[SCANCODE_3] = SDLK_3;
   227     keymap[SCANCODE_4] = SDLK_4;
   228     keymap[SCANCODE_5] = SDLK_5;
   229     keymap[SCANCODE_6] = SDLK_6;
   230     keymap[SCANCODE_7] = SDLK_7;
   231     keymap[SCANCODE_8] = SDLK_8;
   232     keymap[SCANCODE_9] = SDLK_9;
   233     keymap[SCANCODE_0] = SDLK_0;
   234     keymap[SCANCODE_MINUS] = SDLK_MINUS;
   235     keymap[SCANCODE_EQUAL] = SDLK_EQUALS;
   236     keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
   237     keymap[SCANCODE_TAB] = SDLK_TAB;
   238     keymap[SCANCODE_Q] = SDLK_q;
   239     keymap[SCANCODE_W] = SDLK_w;
   240     keymap[SCANCODE_E] = SDLK_e;
   241     keymap[SCANCODE_R] = SDLK_r;
   242     keymap[SCANCODE_T] = SDLK_t;
   243     keymap[SCANCODE_Y] = SDLK_y;
   244     keymap[SCANCODE_U] = SDLK_u;
   245     keymap[SCANCODE_I] = SDLK_i;
   246     keymap[SCANCODE_O] = SDLK_o;
   247     keymap[SCANCODE_P] = SDLK_p;
   248     keymap[SCANCODE_BRACKET_LEFT] = SDLK_LEFTBRACKET;
   249     keymap[SCANCODE_BRACKET_RIGHT] = SDLK_RIGHTBRACKET;
   250     keymap[SCANCODE_ENTER] = SDLK_RETURN;
   251     keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
   252     keymap[SCANCODE_A] = SDLK_a;
   253     keymap[SCANCODE_S] = SDLK_s;
   254     keymap[SCANCODE_D] = SDLK_d;
   255     keymap[SCANCODE_F] = SDLK_f;
   256     keymap[SCANCODE_G] = SDLK_g;
   257     keymap[SCANCODE_H] = SDLK_h;
   258     keymap[SCANCODE_J] = SDLK_j;
   259     keymap[SCANCODE_K] = SDLK_k;
   260     keymap[SCANCODE_L] = SDLK_l;
   261     keymap[SCANCODE_SEMICOLON] = SDLK_SEMICOLON;
   262     keymap[SCANCODE_APOSTROPHE] = SDLK_QUOTE;
   263     keymap[SCANCODE_GRAVE] = SDLK_BACKQUOTE;
   264     keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
   265     keymap[SCANCODE_BACKSLASH] = SDLK_BACKSLASH;
   266     keymap[SCANCODE_Z] = SDLK_z;
   267     keymap[SCANCODE_X] = SDLK_x;
   268     keymap[SCANCODE_C] = SDLK_c;
   269     keymap[SCANCODE_V] = SDLK_v;
   270     keymap[SCANCODE_B] = SDLK_b;
   271     keymap[SCANCODE_N] = SDLK_n;
   272     keymap[SCANCODE_M] = SDLK_m;
   273     keymap[SCANCODE_COMMA] = SDLK_COMMA;
   274     keymap[SCANCODE_PERIOD] = SDLK_PERIOD;
   275     keymap[SCANCODE_SLASH] = SDLK_SLASH;
   276     keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
   277     keymap[SCANCODE_KEYPADMULTIPLY] = SDLK_KP_MULTIPLY;
   278     keymap[SCANCODE_LEFTALT] = SDLK_LALT;
   279     keymap[SCANCODE_SPACE] = SDLK_SPACE;
   280     keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
   281     keymap[SCANCODE_F1] = SDLK_F1;
   282     keymap[SCANCODE_F2] = SDLK_F2;
   283     keymap[SCANCODE_F3] = SDLK_F3;
   284     keymap[SCANCODE_F4] = SDLK_F4;
   285     keymap[SCANCODE_F5] = SDLK_F5;
   286     keymap[SCANCODE_F6] = SDLK_F6;
   287     keymap[SCANCODE_F7] = SDLK_F7;
   288     keymap[SCANCODE_F8] = SDLK_F8;
   289     keymap[SCANCODE_F9] = SDLK_F9;
   290     keymap[SCANCODE_F10] = SDLK_F10;
   291     keymap[SCANCODE_NUMLOCK] = SDLK_NUMLOCK;
   292     keymap[SCANCODE_SCROLLLOCK] = SDLK_SCROLLOCK;
   293     keymap[SCANCODE_KEYPAD7] = SDLK_KP7;
   294     keymap[SCANCODE_CURSORUPLEFT] = SDLK_KP7;
   295     keymap[SCANCODE_KEYPAD8] = SDLK_KP8;
   296     keymap[SCANCODE_CURSORUP] = SDLK_KP8;
   297     keymap[SCANCODE_KEYPAD9] = SDLK_KP9;
   298     keymap[SCANCODE_CURSORUPRIGHT] = SDLK_KP9;
   299     keymap[SCANCODE_KEYPADMINUS] = SDLK_KP_MINUS;
   300     keymap[SCANCODE_KEYPAD4] = SDLK_KP4;
   301     keymap[SCANCODE_CURSORLEFT] = SDLK_KP4;
   302     keymap[SCANCODE_KEYPAD5] = SDLK_KP5;
   303     keymap[SCANCODE_KEYPAD6] = SDLK_KP6;
   304     keymap[SCANCODE_CURSORRIGHT] = SDLK_KP6;
   305     keymap[SCANCODE_KEYPADPLUS] = SDLK_KP_PLUS;
   306     keymap[SCANCODE_KEYPAD1] = SDLK_KP1;
   307     keymap[SCANCODE_CURSORDOWNLEFT] = SDLK_KP1;
   308     keymap[SCANCODE_KEYPAD2] = SDLK_KP2;
   309     keymap[SCANCODE_CURSORDOWN] = SDLK_KP2;
   310     keymap[SCANCODE_KEYPAD3] = SDLK_KP3;
   311     keymap[SCANCODE_CURSORDOWNRIGHT] = SDLK_KP3;
   312     keymap[SCANCODE_KEYPAD0] = SDLK_KP0;
   313     keymap[SCANCODE_KEYPADPERIOD] = SDLK_KP_PERIOD;
   314     keymap[SCANCODE_LESS] = SDLK_LESS;
   315     keymap[SCANCODE_F11] = SDLK_F11;
   316     keymap[SCANCODE_F12] = SDLK_F12;
   317     keymap[SCANCODE_KEYPADENTER] = SDLK_KP_ENTER;
   318     keymap[SCANCODE_RIGHTCONTROL] = SDLK_RCTRL;
   319     keymap[SCANCODE_CONTROL] = SDLK_RCTRL;
   320     keymap[SCANCODE_KEYPADDIVIDE] = SDLK_KP_DIVIDE;
   321     keymap[SCANCODE_PRINTSCREEN] = SDLK_PRINT;
   322     keymap[SCANCODE_RIGHTALT] = SDLK_RALT;
   323     keymap[SCANCODE_BREAK] = SDLK_BREAK;
   324     keymap[SCANCODE_BREAK_ALTERNATIVE] = SDLK_UNKNOWN;
   325     keymap[SCANCODE_HOME] = SDLK_HOME;
   326     keymap[SCANCODE_CURSORBLOCKUP] = SDLK_UP;
   327     keymap[SCANCODE_PAGEUP] = SDLK_PAGEUP;
   328     keymap[SCANCODE_CURSORBLOCKLEFT] = SDLK_LEFT;
   329     keymap[SCANCODE_CURSORBLOCKRIGHT] = SDLK_RIGHT;
   330     keymap[SCANCODE_END] = SDLK_END;
   331     keymap[SCANCODE_CURSORBLOCKDOWN] = SDLK_DOWN;
   332     keymap[SCANCODE_PAGEDOWN] = SDLK_PAGEDOWN;
   333     keymap[SCANCODE_INSERT] = SDLK_INSERT;
   334     keymap[SCANCODE_REMOVE] = SDLK_DELETE;
   335     keymap[119] = SDLK_PAUSE;
   336     keymap[SCANCODE_RIGHTWIN] = SDLK_RSUPER;
   337     keymap[SCANCODE_LEFTWIN] = SDLK_LSUPER;
   338     keymap[127] = SDLK_MENU;
   339 }
   340 
   341 #if defined(linux)
   342 static SDL_keysym *
   343 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 *
   387 TranslateKey(int scancode, SDL_keysym * keysym)
   388 {
   389     /* Set the keysym information */
   390     keysym->scancode = scancode;
   391     keysym->sym = keymap[scancode];
   392     keysym->mod = KMOD_NONE;
   393 
   394     /* If UNICODE is on, get the UNICODE value for the key */
   395     keysym->unicode = 0;
   396     if (SDL_TranslateUNICODE && vga_keymap) {
   397         int map;
   398         SDLMod modstate;
   399 
   400         modstate = SDL_GetModState();
   401         map = 0;
   402         if (modstate & KMOD_SHIFT) {
   403             map += 1;
   404         }
   405         if (modstate & KMOD_CTRL) {
   406             map += 2;
   407         }
   408         if (modstate & KMOD_ALT) {
   409             map += 4;
   410         }
   411         if (!(vga_keymap->key[scancode].spcl & (0x80 >> map))) {
   412             keysym->unicode = vga_keymap->key[scancode].map[map];
   413         }
   414 
   415     }
   416     return (keysym);
   417 }
   418 #else
   419 #error You must choose your operating system here
   420 #endif
   421 /* vi: set ts=4 sw=4 expandtab: */