src/video/ggi/SDL_ggievents.c
changeset 1981 3f21778e7433
parent 1980 0e7becb566ee
child 1982 3b4ce57c6215
equal deleted inserted replaced
1980:0e7becb566ee 1981:3f21778e7433
     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 GGI events into SDL events */
       
    25 
       
    26 #include <sys/types.h>
       
    27 #include <sys/time.h>
       
    28 #include <unistd.h>
       
    29 #include <fcntl.h>
       
    30 #include <termios.h>
       
    31 
       
    32 #include <ggi/keyboard.h>
       
    33 
       
    34 #include "SDL_ggikeys.h"
       
    35 
       
    36 #include "SDL.h"
       
    37 #include "../SDL_sysvideo.h"
       
    38 #include "../../events/SDL_sysevents.h"
       
    39 #include "../../events/SDL_events_c.h"
       
    40 #include "SDL_ggivideo.h"
       
    41 #include "SDL_ggievents_c.h"
       
    42 
       
    43 /* The translation tables from a GGI keycode to a SDL keysym */
       
    44 static SDLKey keymap[128];
       
    45 static SDL_keysym *GGI_TranslateKey(ggi_event * ev, SDL_keysym * keysym);
       
    46 
       
    47 static int posted = 0;
       
    48 
       
    49 void
       
    50 GGI_PumpEvents(_THIS)
       
    51 {
       
    52     struct timeval *tvp, tv = { 0, 0 };
       
    53     ggi_event ev;
       
    54 
       
    55     tvp = &tv;
       
    56 
       
    57 /*	ggiFlush(VIS); */
       
    58 
       
    59     while (ggiEventPoll(VIS, emAll, tvp))
       
    60 /*	while (ggiEventPoll(VIS, (emKeyboard | emPointer | emCommand), tvp)) */
       
    61     {
       
    62         int queueevent_mouse = 0, queueevent_kbd = 0;
       
    63         static int buttons = 0;
       
    64         static int mouse_x = 0, mouse_y = 0, mouse_z = 0;
       
    65         int x = 0, y = 0, z = 0, rx = 0, ry = 0, rz = 0;
       
    66         uint32 sym;
       
    67         int pressed_mouse, pressed_kbd;
       
    68         SDL_keysym keysym;
       
    69 
       
    70         posted = 0;
       
    71 
       
    72         /* FIXME: We do not actually want all events, only
       
    73          * mouse and keyboard events.  Having to handle all
       
    74          * events will slow things down.  */
       
    75 
       
    76         ggiEventRead(VIS, &ev, emAll);
       
    77 /*		ggiEventRead(VIS, &ev, (emKeyboard | emPointer | emCommand)); */
       
    78 
       
    79         switch (ev.any.type) {
       
    80         case evPtrRelative:
       
    81             x = ev.pmove.x;
       
    82             y = ev.pmove.y;
       
    83             z = ev.pmove.wheel;
       
    84             posted += SDL_PrivateMouseMotion(0, 1, x, y);
       
    85             break;
       
    86         case evPtrAbsolute:
       
    87             if (mouse_x != ev.pmove.x || mouse_y != ev.pmove.y
       
    88                 || mouse_z != ev.pmove.wheel) {
       
    89                 x = ev.pmove.x - mouse_x;
       
    90                 y = ev.pmove.y - mouse_y;
       
    91                 z = ev.pmove.wheel - mouse_z;
       
    92                 mouse_x = ev.pmove.x;
       
    93                 mouse_y = ev.pmove.y;
       
    94                 mouse_z = ev.pmove.wheel;
       
    95                 posted += SDL_PrivateMouseMotion(0, 1, x, y);
       
    96             }
       
    97             break;
       
    98         case evPtrButtonPress:
       
    99             posted +=
       
   100                 SDL_PrivateMouseButton(SDL_PRESSED, ev.pbutton.button, 0, 0);
       
   101             break;
       
   102         case evPtrButtonRelease:
       
   103             posted +=
       
   104                 SDL_PrivateMouseButton(SDL_RELEASED, ev.pbutton.button, 0, 0);
       
   105             break;
       
   106         case evKeyPress:
       
   107         case evKeyRepeat:
       
   108             posted +=
       
   109                 SDL_PrivateKeyboard(SDL_PRESSED,
       
   110                                     GGI_TranslateKey(&ev, &keysym));
       
   111             break;
       
   112         case evKeyRelease:
       
   113             posted +=
       
   114                 SDL_PrivateKeyboard(SDL_RELEASED,
       
   115                                     GGI_TranslateKey(&ev, &keysym));
       
   116             break;
       
   117         case evCommand:
       
   118             fprintf(stderr, "Command event %x recieved\n", ev.cmd.code);
       
   119             break;
       
   120         default:
       
   121             fprintf(stderr, "Unhandled event type %d\n", ev.any.type);
       
   122             break;
       
   123         }
       
   124     }
       
   125 
       
   126 }
       
   127 
       
   128 void
       
   129 GGI_InitOSKeymap(_THIS)
       
   130 {
       
   131     int i;
       
   132 
       
   133     /* Initialize the GGI key translation table */
       
   134     for (i = 0; i < SDL_arraysize(keymap); ++i)
       
   135         keymap[i] = SDLK_UNKNOWN;
       
   136 
       
   137     keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
       
   138     keymap[SCANCODE_1] = SDLK_1;
       
   139     keymap[SCANCODE_2] = SDLK_2;
       
   140     keymap[SCANCODE_3] = SDLK_3;
       
   141     keymap[SCANCODE_4] = SDLK_4;
       
   142     keymap[SCANCODE_5] = SDLK_5;
       
   143     keymap[SCANCODE_6] = SDLK_6;
       
   144     keymap[SCANCODE_7] = SDLK_7;
       
   145     keymap[SCANCODE_8] = SDLK_8;
       
   146     keymap[SCANCODE_9] = SDLK_9;
       
   147     keymap[SCANCODE_0] = SDLK_0;
       
   148     keymap[SCANCODE_MINUS] = SDLK_MINUS;
       
   149     keymap[SCANCODE_EQUAL] = SDLK_EQUALS;
       
   150     keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
       
   151     keymap[SCANCODE_TAB] = SDLK_TAB;
       
   152     keymap[SCANCODE_Q] = SDLK_q;
       
   153     keymap[SCANCODE_W] = SDLK_w;
       
   154     keymap[SCANCODE_E] = SDLK_e;
       
   155     keymap[SCANCODE_R] = SDLK_r;
       
   156     keymap[SCANCODE_T] = SDLK_t;
       
   157     keymap[SCANCODE_Y] = SDLK_y;
       
   158     keymap[SCANCODE_U] = SDLK_u;
       
   159     keymap[SCANCODE_I] = SDLK_i;
       
   160     keymap[SCANCODE_O] = SDLK_o;
       
   161     keymap[SCANCODE_P] = SDLK_p;
       
   162     keymap[SCANCODE_BRACKET_LEFT] = SDLK_LEFTBRACKET;
       
   163     keymap[SCANCODE_BRACKET_RIGHT] = SDLK_RIGHTBRACKET;
       
   164     keymap[SCANCODE_ENTER] = SDLK_RETURN;
       
   165     keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
       
   166     keymap[SCANCODE_A] = SDLK_a;
       
   167     keymap[SCANCODE_S] = SDLK_s;
       
   168     keymap[SCANCODE_D] = SDLK_d;
       
   169     keymap[SCANCODE_F] = SDLK_f;
       
   170     keymap[SCANCODE_G] = SDLK_g;
       
   171     keymap[SCANCODE_H] = SDLK_h;
       
   172     keymap[SCANCODE_J] = SDLK_j;
       
   173     keymap[SCANCODE_K] = SDLK_k;
       
   174     keymap[SCANCODE_L] = SDLK_l;
       
   175     keymap[SCANCODE_SEMICOLON] = SDLK_SEMICOLON;
       
   176     keymap[SCANCODE_APOSTROPHE] = SDLK_QUOTE;
       
   177     keymap[SCANCODE_GRAVE] = SDLK_BACKQUOTE;
       
   178     keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
       
   179     keymap[SCANCODE_BACKSLASH] = SDLK_BACKSLASH;
       
   180     keymap[SCANCODE_Z] = SDLK_z;
       
   181     keymap[SCANCODE_X] = SDLK_x;
       
   182     keymap[SCANCODE_C] = SDLK_c;
       
   183     keymap[SCANCODE_V] = SDLK_v;
       
   184     keymap[SCANCODE_B] = SDLK_b;
       
   185     keymap[SCANCODE_N] = SDLK_n;
       
   186     keymap[SCANCODE_M] = SDLK_m;
       
   187     keymap[SCANCODE_COMMA] = SDLK_COMMA;
       
   188     keymap[SCANCODE_PERIOD] = SDLK_PERIOD;
       
   189     keymap[SCANCODE_SLASH] = SDLK_SLASH;
       
   190     keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
       
   191     keymap[SCANCODE_KEYPADMULTIPLY] = SDLK_KP_MULTIPLY;
       
   192     keymap[SCANCODE_LEFTALT] = SDLK_LALT;
       
   193     keymap[SCANCODE_SPACE] = SDLK_SPACE;
       
   194     keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
       
   195     keymap[SCANCODE_F1] = SDLK_F1;
       
   196     keymap[SCANCODE_F2] = SDLK_F2;
       
   197     keymap[SCANCODE_F3] = SDLK_F3;
       
   198     keymap[SCANCODE_F4] = SDLK_F4;
       
   199     keymap[SCANCODE_F5] = SDLK_F5;
       
   200     keymap[SCANCODE_F6] = SDLK_F6;
       
   201     keymap[SCANCODE_F7] = SDLK_F7;
       
   202     keymap[SCANCODE_F8] = SDLK_F8;
       
   203     keymap[SCANCODE_F9] = SDLK_F9;
       
   204     keymap[SCANCODE_F10] = SDLK_F10;
       
   205     keymap[SCANCODE_NUMLOCK] = SDLK_NUMLOCK;
       
   206     keymap[SCANCODE_SCROLLLOCK] = SDLK_SCROLLOCK;
       
   207     keymap[SCANCODE_KEYPAD7] = SDLK_KP7;
       
   208     keymap[SCANCODE_CURSORUPLEFT] = SDLK_KP7;
       
   209     keymap[SCANCODE_KEYPAD8] = SDLK_KP8;
       
   210     keymap[SCANCODE_CURSORUP] = SDLK_KP8;
       
   211     keymap[SCANCODE_KEYPAD9] = SDLK_KP9;
       
   212     keymap[SCANCODE_CURSORUPRIGHT] = SDLK_KP9;
       
   213     keymap[SCANCODE_KEYPADMINUS] = SDLK_KP_MINUS;
       
   214     keymap[SCANCODE_KEYPAD4] = SDLK_KP4;
       
   215     keymap[SCANCODE_CURSORLEFT] = SDLK_KP4;
       
   216     keymap[SCANCODE_KEYPAD5] = SDLK_KP5;
       
   217     keymap[SCANCODE_KEYPAD6] = SDLK_KP6;
       
   218     keymap[SCANCODE_CURSORRIGHT] = SDLK_KP6;
       
   219     keymap[SCANCODE_KEYPADPLUS] = SDLK_KP_PLUS;
       
   220     keymap[SCANCODE_KEYPAD1] = SDLK_KP1;
       
   221     keymap[SCANCODE_CURSORDOWNLEFT] = SDLK_KP1;
       
   222     keymap[SCANCODE_KEYPAD2] = SDLK_KP2;
       
   223     keymap[SCANCODE_CURSORDOWN] = SDLK_KP2;
       
   224     keymap[SCANCODE_KEYPAD3] = SDLK_KP3;
       
   225     keymap[SCANCODE_CURSORDOWNRIGHT] = SDLK_KP3;
       
   226     keymap[SCANCODE_KEYPAD0] = SDLK_KP0;
       
   227     keymap[SCANCODE_KEYPADPERIOD] = SDLK_KP_PERIOD;
       
   228     keymap[SCANCODE_LESS] = SDLK_LESS;
       
   229     keymap[SCANCODE_F11] = SDLK_F11;
       
   230     keymap[SCANCODE_F12] = SDLK_F12;
       
   231     keymap[SCANCODE_KEYPADENTER] = SDLK_KP_ENTER;
       
   232     keymap[SCANCODE_RIGHTCONTROL] = SDLK_RCTRL;
       
   233     keymap[SCANCODE_CONTROL] = SDLK_RCTRL;
       
   234     keymap[SCANCODE_KEYPADDIVIDE] = SDLK_KP_DIVIDE;
       
   235     keymap[SCANCODE_PRINTSCREEN] = SDLK_PRINT;
       
   236     keymap[SCANCODE_RIGHTALT] = SDLK_RALT;
       
   237     keymap[SCANCODE_BREAK] = SDLK_BREAK;
       
   238     keymap[SCANCODE_BREAK_ALTERNATIVE] = SDLK_UNKNOWN;
       
   239     keymap[SCANCODE_HOME] = SDLK_HOME;
       
   240     keymap[SCANCODE_CURSORBLOCKUP] = SDLK_UP;
       
   241     keymap[SCANCODE_PAGEUP] = SDLK_PAGEUP;
       
   242     keymap[SCANCODE_CURSORBLOCKLEFT] = SDLK_LEFT;
       
   243     keymap[SCANCODE_CURSORBLOCKRIGHT] = SDLK_RIGHT;
       
   244     keymap[SCANCODE_END] = SDLK_END;
       
   245     keymap[SCANCODE_CURSORBLOCKDOWN] = SDLK_DOWN;
       
   246     keymap[SCANCODE_PAGEDOWN] = SDLK_PAGEDOWN;
       
   247     keymap[SCANCODE_INSERT] = SDLK_INSERT;
       
   248     keymap[SCANCODE_REMOVE] = SDLK_DELETE;
       
   249     keymap[119] = SDLK_PAUSE;
       
   250     keymap[SCANCODE_RIGHTWIN] = SDLK_RSUPER;
       
   251     keymap[SCANCODE_LEFTWIN] = SDLK_LSUPER;
       
   252     keymap[127] = SDLK_MENU;
       
   253 }
       
   254 
       
   255 
       
   256 
       
   257 static SDL_keysym *
       
   258 GGI_TranslateKey(gii_event * ev, SDL_keysym * keysym)
       
   259 {
       
   260     /* Set the keysym information */
       
   261     keysym->scancode = ev->key.button;
       
   262     keysym->sym = keymap[ev->key.button];
       
   263     keysym->mod = KMOD_NONE;
       
   264 
       
   265     /* If UNICODE is on, get the UNICODE value for the key */
       
   266     keysym->unicode = 0;
       
   267     if (SDL_TranslateUNICODE) {
       
   268         keysym->unicode = GII_UNICODE(ev->key.sym);
       
   269     }
       
   270 
       
   271     return keysym;
       
   272 }
       
   273 
       
   274 /* vi: set ts=4 sw=4 expandtab: */