src/video/directfb/SDL_DirectFB_events.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2004 16:49:27 +0000
changeset 769 b8d311d90021
parent 464 1c4be4a16410
child 1312 c9b51268668f
permissions -rw-r--r--
Updated copyright information for 2004 (Happy New Year!)
     1 /*
     2 	SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2004 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@libsdl.org
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 /* Handle the event stream, converting DirectFB input events into SDL events */
    29 
    30 #include <sys/types.h>
    31 #include <sys/time.h>
    32 #include <stdlib.h>
    33 #include <stdio.h>
    34 #include <unistd.h>
    35 #include <fcntl.h>
    36 #include <termios.h>
    37 
    38 #include <directfb.h>
    39 
    40 #include "SDL.h"
    41 #include "SDL_sysevents.h"
    42 #include "SDL_sysvideo.h"
    43 #include "SDL_events_c.h"
    44 #include "SDL_DirectFB_video.h"
    45 #include "SDL_DirectFB_events.h"
    46 
    47 /* The translation tables from a DirectFB keycode to a SDL keysym */
    48 static SDLKey keymap[256];
    49 static SDL_keysym *DirectFB_TranslateKey (DFBInputEvent *ev, SDL_keysym *keysym);
    50 static int DirectFB_TranslateButton (DFBInputEvent *ev);
    51 
    52 static int posted = 0;
    53 
    54 
    55 void DirectFB_PumpEvents (_THIS)
    56 {
    57   DFBInputEvent evt;
    58 
    59   while (HIDDEN->eventbuffer->GetEvent (HIDDEN->eventbuffer,
    60                                         DFB_EVENT (&evt)) == DFB_OK)
    61     {
    62       SDL_keysym keysym;
    63 
    64       switch (evt.type)
    65         {
    66         case DIET_BUTTONPRESS:
    67           posted += SDL_PrivateMouseButton(SDL_PRESSED,
    68                                            DirectFB_TranslateButton (&evt), 0, 0);
    69           break;
    70         case DIET_BUTTONRELEASE:
    71           posted += SDL_PrivateMouseButton(SDL_RELEASED,
    72                                            DirectFB_TranslateButton (&evt), 0, 0);
    73           break;
    74         case DIET_KEYPRESS:
    75           posted += SDL_PrivateKeyboard(SDL_PRESSED, DirectFB_TranslateKey(&evt, &keysym));
    76           break;
    77         case DIET_KEYRELEASE:
    78           posted += SDL_PrivateKeyboard(SDL_RELEASED, DirectFB_TranslateKey(&evt, &keysym));
    79           break;
    80         case DIET_AXISMOTION:
    81           if (evt.flags & DIEF_AXISREL)
    82             {
    83               if (evt.axis == DIAI_X)
    84                 posted += SDL_PrivateMouseMotion(0, 1, evt.axisrel, 0);
    85               else if (evt.axis == DIAI_Y)
    86                 posted += SDL_PrivateMouseMotion(0, 1, 0, evt.axisrel);
    87             }
    88           break;
    89         default:
    90           ;
    91         }
    92     }
    93 }
    94 
    95 void DirectFB_InitOSKeymap (_THIS)
    96 {
    97   int i;
    98 	
    99   /* Initialize the DirectFB key translation table */
   100   for (i=0; i<SDL_TABLESIZE(keymap); ++i)
   101     keymap[i] = SDLK_UNKNOWN;
   102 
   103   keymap[DIKI_A - DIKI_UNKNOWN] = SDLK_a;
   104   keymap[DIKI_B - DIKI_UNKNOWN] = SDLK_b;
   105   keymap[DIKI_C - DIKI_UNKNOWN] = SDLK_c;
   106   keymap[DIKI_D - DIKI_UNKNOWN] = SDLK_d;
   107   keymap[DIKI_E - DIKI_UNKNOWN] = SDLK_e;
   108   keymap[DIKI_F - DIKI_UNKNOWN] = SDLK_f;
   109   keymap[DIKI_G - DIKI_UNKNOWN] = SDLK_g;
   110   keymap[DIKI_H - DIKI_UNKNOWN] = SDLK_h;
   111   keymap[DIKI_I - DIKI_UNKNOWN] = SDLK_i;
   112   keymap[DIKI_J - DIKI_UNKNOWN] = SDLK_j;
   113   keymap[DIKI_K - DIKI_UNKNOWN] = SDLK_k;
   114   keymap[DIKI_L - DIKI_UNKNOWN] = SDLK_l;
   115   keymap[DIKI_M - DIKI_UNKNOWN] = SDLK_m;
   116   keymap[DIKI_N - DIKI_UNKNOWN] = SDLK_n;
   117   keymap[DIKI_O - DIKI_UNKNOWN] = SDLK_o;
   118   keymap[DIKI_P - DIKI_UNKNOWN] = SDLK_p;
   119   keymap[DIKI_Q - DIKI_UNKNOWN] = SDLK_q;
   120   keymap[DIKI_R - DIKI_UNKNOWN] = SDLK_r;
   121   keymap[DIKI_S - DIKI_UNKNOWN] = SDLK_s;
   122   keymap[DIKI_T - DIKI_UNKNOWN] = SDLK_t;
   123   keymap[DIKI_U - DIKI_UNKNOWN] = SDLK_u;
   124   keymap[DIKI_V - DIKI_UNKNOWN] = SDLK_v;
   125   keymap[DIKI_W - DIKI_UNKNOWN] = SDLK_w;
   126   keymap[DIKI_X - DIKI_UNKNOWN] = SDLK_x;
   127   keymap[DIKI_Y - DIKI_UNKNOWN] = SDLK_y;
   128   keymap[DIKI_Z - DIKI_UNKNOWN] = SDLK_z;
   129   
   130   keymap[DIKI_0 - DIKI_UNKNOWN] = SDLK_0;
   131   keymap[DIKI_1 - DIKI_UNKNOWN] = SDLK_1;
   132   keymap[DIKI_2 - DIKI_UNKNOWN] = SDLK_2;
   133   keymap[DIKI_3 - DIKI_UNKNOWN] = SDLK_3;
   134   keymap[DIKI_4 - DIKI_UNKNOWN] = SDLK_4;
   135   keymap[DIKI_5 - DIKI_UNKNOWN] = SDLK_5;
   136   keymap[DIKI_6 - DIKI_UNKNOWN] = SDLK_6;
   137   keymap[DIKI_7 - DIKI_UNKNOWN] = SDLK_7;
   138   keymap[DIKI_8 - DIKI_UNKNOWN] = SDLK_8;
   139   keymap[DIKI_9 - DIKI_UNKNOWN] = SDLK_9;
   140   
   141   keymap[DIKI_F1 - DIKI_UNKNOWN] = SDLK_F1;
   142   keymap[DIKI_F2 - DIKI_UNKNOWN] = SDLK_F2;
   143   keymap[DIKI_F3 - DIKI_UNKNOWN] = SDLK_F3;
   144   keymap[DIKI_F4 - DIKI_UNKNOWN] = SDLK_F4;
   145   keymap[DIKI_F5 - DIKI_UNKNOWN] = SDLK_F5;
   146   keymap[DIKI_F6 - DIKI_UNKNOWN] = SDLK_F6;
   147   keymap[DIKI_F7 - DIKI_UNKNOWN] = SDLK_F7;
   148   keymap[DIKI_F8 - DIKI_UNKNOWN] = SDLK_F8;
   149   keymap[DIKI_F9 - DIKI_UNKNOWN] = SDLK_F9;
   150   keymap[DIKI_F10 - DIKI_UNKNOWN] = SDLK_F10;
   151   keymap[DIKI_F11 - DIKI_UNKNOWN] = SDLK_F11;
   152   keymap[DIKI_F12 - DIKI_UNKNOWN] = SDLK_F12;
   153   
   154   keymap[DIKI_ESCAPE - DIKI_UNKNOWN] = SDLK_ESCAPE;
   155   keymap[DIKI_LEFT - DIKI_UNKNOWN] = SDLK_LEFT;
   156   keymap[DIKI_RIGHT - DIKI_UNKNOWN] = SDLK_RIGHT;
   157   keymap[DIKI_UP - DIKI_UNKNOWN] = SDLK_UP;
   158   keymap[DIKI_DOWN - DIKI_UNKNOWN] = SDLK_DOWN;
   159   keymap[DIKI_CONTROL_L - DIKI_UNKNOWN] = SDLK_LCTRL;
   160   keymap[DIKI_CONTROL_R - DIKI_UNKNOWN] = SDLK_RCTRL;
   161   keymap[DIKI_SHIFT_L - DIKI_UNKNOWN] = SDLK_LSHIFT;
   162   keymap[DIKI_SHIFT_R - DIKI_UNKNOWN] = SDLK_RSHIFT;
   163   keymap[DIKI_ALT_L - DIKI_UNKNOWN] = SDLK_LALT;
   164   keymap[DIKI_ALTGR - DIKI_UNKNOWN] = SDLK_RALT;
   165   keymap[DIKI_TAB - DIKI_UNKNOWN] = SDLK_TAB;
   166   keymap[DIKI_ENTER - DIKI_UNKNOWN] = SDLK_RETURN;
   167   keymap[DIKI_SPACE - DIKI_UNKNOWN] = SDLK_SPACE;
   168   keymap[DIKI_BACKSPACE - DIKI_UNKNOWN] = SDLK_BACKSPACE;
   169   keymap[DIKI_INSERT - DIKI_UNKNOWN] = SDLK_INSERT;
   170   keymap[DIKI_DELETE - DIKI_UNKNOWN] = SDLK_DELETE;
   171   keymap[DIKI_HOME - DIKI_UNKNOWN] = SDLK_HOME;
   172   keymap[DIKI_END - DIKI_UNKNOWN] = SDLK_END;
   173   keymap[DIKI_PAGE_UP - DIKI_UNKNOWN] = SDLK_PAGEUP;
   174   keymap[DIKI_PAGE_DOWN - DIKI_UNKNOWN] = SDLK_PAGEDOWN;
   175   keymap[DIKI_CAPS_LOCK - DIKI_UNKNOWN] = SDLK_CAPSLOCK;
   176   keymap[DIKI_NUM_LOCK - DIKI_UNKNOWN] = SDLK_NUMLOCK;
   177   keymap[DIKI_SCROLL_LOCK - DIKI_UNKNOWN] = SDLK_SCROLLOCK;
   178   keymap[DIKI_PRINT - DIKI_UNKNOWN] = SDLK_PRINT;
   179   keymap[DIKI_PAUSE - DIKI_UNKNOWN] = SDLK_PAUSE;
   180   keymap[DIKI_KP_DIV - DIKI_UNKNOWN] = SDLK_KP_DIVIDE;
   181   keymap[DIKI_KP_MULT - DIKI_UNKNOWN] = SDLK_KP_MULTIPLY;
   182   keymap[DIKI_KP_MINUS - DIKI_UNKNOWN] = SDLK_KP_MINUS;
   183   keymap[DIKI_KP_PLUS - DIKI_UNKNOWN] = SDLK_KP_PLUS;
   184   keymap[DIKI_KP_ENTER - DIKI_UNKNOWN] = SDLK_KP_ENTER;
   185 }
   186 
   187 
   188 static SDL_keysym *DirectFB_TranslateKey (DFBInputEvent *ev, SDL_keysym *keysym)
   189 {
   190   /* Set the keysym information */
   191   keysym->scancode = ev->key_id;
   192   keysym->mod = KMOD_NONE; /* FIXME */
   193   keysym->unicode = (DFB_KEY_TYPE (ev->key_symbol) == DIKT_UNICODE) ? ev->key_symbol : 0;
   194 
   195   if (ev->key_symbol > 0 && ev->key_symbol < 128)
   196     keysym->sym = ev->key_symbol;
   197   else
   198     keysym->sym = keymap[ev->key_id - DIKI_UNKNOWN];
   199 
   200   return keysym;
   201 }
   202 
   203 static int DirectFB_TranslateButton (DFBInputEvent *ev)
   204 {
   205   switch (ev->button)
   206     {
   207     case DIBI_LEFT:
   208       return 1;
   209     case DIBI_MIDDLE:
   210       return 2;
   211     case DIBI_RIGHT:
   212       return 3;
   213     default:
   214       return 0;
   215     }
   216 }