Finishing up keyboard code revamp, at least for now... SDL-1.3
authorSam Lantinga <slouken@libsdl.org>
Sun, 11 Jun 2006 05:27:10 +0000
branchSDL-1.3
changeset 16747688a73b25b1
parent 1673 624e1412fbba
child 1675 d33dcfc3fde7
Finishing up keyboard code revamp, at least for now...
include/SDL_keyboard.h
src/events/SDL_events_c.h
src/events/SDL_keyboard.c
src/events/SDL_keyboard_c.h
src/events/SDL_mouse.c
     1.1 --- a/include/SDL_keyboard.h	Sat Jun 10 09:11:59 2006 +0000
     1.2 +++ b/include/SDL_keyboard.h	Sun Jun 11 05:27:10 2006 +0000
     1.3 @@ -49,8 +49,9 @@
     1.4  typedef struct SDL_keysym
     1.5  {
     1.6      Uint8 scancode;             /**< keyboard specific scancode */
     1.7 -    SDLKey sym;                 /**< SDL virtual keysym */
     1.8 -    SDLMod mod;                 /**< current key modifiers */
     1.9 +    Uint8 padding[3];           /**< alignment padding */
    1.10 +    Uint16 sym;                 /**< SDL virtual keysym */
    1.11 +    Uint16 mod;                 /**< current key modifiers */
    1.12  } SDL_keysym;
    1.13  
    1.14  /* Function prototypes */
     2.1 --- a/src/events/SDL_events_c.h	Sat Jun 10 09:11:59 2006 +0000
     2.2 +++ b/src/events/SDL_events_c.h	Sun Jun 11 05:27:10 2006 +0000
     2.3 @@ -24,6 +24,7 @@
     2.4  /* Useful functions and variables from SDL_events.c */
     2.5  #include "SDL_events.h"
     2.6  #include "SDL_mouse_c.h"
     2.7 +#include "SDL_keyboard_c.h"
     2.8  
     2.9  /* Start and stop the event processing loop */
    2.10  extern int SDL_StartEventLoop(Uint32 flags);
    2.11 @@ -34,10 +35,6 @@
    2.12  extern void SDL_Unlock_EventThread(void);
    2.13  extern Uint32 SDL_EventThreadID(void);
    2.14  
    2.15 -extern int SDL_KeyboardInit(void);
    2.16 -extern int SDL_SendKeyboard(Uint8 state, SDL_keysym * key);
    2.17 -extern void SDL_KeyboardQuit(void);
    2.18 -
    2.19  extern int SDL_QuitInit(void);
    2.20  extern int SDL_SendQuit(void);
    2.21  extern void SDL_QuitQuit(void);
    2.22 @@ -53,13 +50,4 @@
    2.23  /* The array of event processing states */
    2.24  extern Uint8 SDL_ProcessEvents[SDL_NUMEVENTS];
    2.25  
    2.26 -/* Used by the event loop to queue pending keyboard repeat events */
    2.27 -extern void SDL_CheckKeyRepeat(void);
    2.28 -
    2.29 -/* Used by the OS keyboard code to detect whether or not to do UNICODE */
    2.30 -#ifndef DEFAULT_UNICODE_TRANSLATION
    2.31 -#define DEFAULT_UNICODE_TRANSLATION 0   /* Default off because of overhead */
    2.32 -#endif
    2.33 -extern int SDL_TranslateUNICODE;
    2.34 -
    2.35  /* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/events/SDL_keyboard.c	Sat Jun 10 09:11:59 2006 +0000
     3.2 +++ b/src/events/SDL_keyboard.c	Sun Jun 11 05:27:10 2006 +0000
     3.3 @@ -30,10 +30,10 @@
     3.4  
     3.5  
     3.6  /* Global keyboard information */
     3.7 +int SDL_TranslateUNICODE = 0;
     3.8  static int SDL_num_keyboards;
     3.9  static int SDL_current_keyboard;
    3.10  static SDL_Keyboard **SDL_keyboards;
    3.11 -int SDL_TranslateUNICODE = 0;
    3.12  
    3.13  static const char *SDL_keynames[SDLK_LAST];     /* Array of keycode names */
    3.14  
    3.15 @@ -46,9 +46,6 @@
    3.16      /* Set default mode of UNICODE translation */
    3.17      SDL_EnableUNICODE(DEFAULT_UNICODE_TRANSLATION);
    3.18  
    3.19 -    /* Set default keyboard repeat setting */
    3.20 -    SDL_EnableKeyRepeat(0, 0);
    3.21 -
    3.22      /* Initialize the tables */
    3.23      for (i = 0; i < SDL_arraysize(SDL_keynames); ++i) {
    3.24          switch (i) {
    3.25 @@ -138,9 +135,6 @@
    3.26          case SDLK_LEFT:
    3.27              SDL_keynames[i] = "left";
    3.28              break;
    3.29 -        case SDLK_DOWN:
    3.30 -            SDL_keynames[i] = "down";
    3.31 -            break;
    3.32          case SDLK_INSERT:
    3.33              SDL_keynames[i] = "insert";
    3.34              break;
    3.35 @@ -347,7 +341,7 @@
    3.36  {
    3.37      SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
    3.38      SDL_keysym keysym;
    3.39 -    SDLKey key;
    3.40 +    Uint16 key;
    3.41  
    3.42      if (!keyboard) {
    3.43          return;
    3.44 @@ -357,10 +351,10 @@
    3.45      for (key = SDLK_FIRST; key < SDLK_LAST; ++key) {
    3.46          if (keyboard->keystate[key] == SDL_PRESSED) {
    3.47              keysym.sym = key;
    3.48 -            SDL_SendKeyboardKey(index, SDL_RELEASED, &keysym);
    3.49 +            SDL_SendKeyboardKey(index, 0, SDL_RELEASED, &keysym);
    3.50          }
    3.51      }
    3.52 -    keyboard->keyrepeat.timestamp = 0;
    3.53 +    keyboard->repeat.timestamp = 0;
    3.54  }
    3.55  
    3.56  void
    3.57 @@ -410,15 +404,22 @@
    3.58  Uint8 *
    3.59  SDL_GetKeyState(int *numkeys)
    3.60  {
    3.61 -    if (numkeys != (int *) 0)
    3.62 +    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
    3.63 +
    3.64 +    if (numkeys != (int *) 0) {
    3.65          *numkeys = SDLK_LAST;
    3.66 -    return (SDL_KeyState);
    3.67 +    }
    3.68 +
    3.69 +    if (!keyboard) {
    3.70 +        return NULL;
    3.71 +    }
    3.72 +    return keyboard->keystate;
    3.73  }
    3.74  
    3.75  SDLMod
    3.76  SDL_GetModState(void)
    3.77  {
    3.78 -    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
    3.79 +    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
    3.80  
    3.81      if (!keyboard) {
    3.82          return KMOD_NONE;
    3.83 @@ -429,7 +430,7 @@
    3.84  void
    3.85  SDL_SetModState(SDLMod modstate)
    3.86  {
    3.87 -    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
    3.88 +    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
    3.89  
    3.90      if (!keyboard) {
    3.91          return;
    3.92 @@ -442,11 +443,21 @@
    3.93  {
    3.94      const char *keyname;
    3.95  
    3.96 -    keyname = keynames[key];
    3.97 +    if (key < SDL_tablesize(SDL_keynames)) {
    3.98 +        keyname = SDL_keynames[key];
    3.99 +    } else {
   3.100 +        keyname = NULL;
   3.101 +    }
   3.102      if (keyname == NULL) {
   3.103          if (key < 256) {
   3.104              static char temp[4];
   3.105 -          FIXME:Convert to UTF - 8 keyname = temp;
   3.106 +            char *cvt;
   3.107 +            temp[0] = (char) key;
   3.108 +            temp[1] = '\0';
   3.109 +            cvt = SDL_iconv_string("UTF-8", "LATIN1", temp, 1);
   3.110 +            SDL_strlcpy(temp, cvt, SDL_arraysize(temp));
   3.111 +            SDL_free(cvt);
   3.112 +            keyname = temp;
   3.113          } else {
   3.114              keyname = "unknown key";
   3.115          }
   3.116 @@ -454,27 +465,33 @@
   3.117      return keyname;
   3.118  }
   3.119  
   3.120 -/* These are global for SDL_eventloop.c */
   3.121  int
   3.122 -SDL_SendKeyboardKey(int index, Uint8 state, const SDL_keysym * keysym)
   3.123 +SDL_SendKeyboardKey(int index, SDL_WindowID windowID, Uint8 state,
   3.124 +                    SDL_keysym * keysym)
   3.125  {
   3.126 -    SDL_Event event;
   3.127 +    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
   3.128      int posted, repeatable;
   3.129      Uint16 modstate;
   3.130 +    Uint8 type;
   3.131  
   3.132 -    SDL_memset(&event, 0, sizeof(event));
   3.133 +    if (!keyboard) {
   3.134 +        return 0;
   3.135 +    }
   3.136  
   3.137 +    if (windowID) {
   3.138 +        keyboard->focus = windowID;
   3.139 +    }
   3.140  #if 0
   3.141      printf("The '%s' key has been %s\n", SDL_GetKeyName(keysym->sym),
   3.142             state == SDL_PRESSED ? "pressed" : "released");
   3.143  #endif
   3.144      /* Set up the keysym */
   3.145 -    modstate = (Uint16) SDL_ModState;
   3.146 +    modstate = keyboard->modstate;
   3.147  
   3.148      repeatable = 0;
   3.149  
   3.150      if (state == SDL_PRESSED) {
   3.151 -        keysym->mod = (SDLMod) modstate;
   3.152 +        keysym->mod = modstate;
   3.153          switch (keysym->sym) {
   3.154          case SDLK_UNKNOWN:
   3.155              break;
   3.156 @@ -482,13 +499,13 @@
   3.157              modstate ^= KMOD_NUM;
   3.158              if (!(modstate & KMOD_NUM))
   3.159                  state = SDL_RELEASED;
   3.160 -            keysym->mod = (SDLMod) modstate;
   3.161 +            keysym->mod = modstate;
   3.162              break;
   3.163          case SDLK_CAPSLOCK:
   3.164              modstate ^= KMOD_CAPS;
   3.165              if (!(modstate & KMOD_CAPS))
   3.166                  state = SDL_RELEASED;
   3.167 -            keysym->mod = (SDLMod) modstate;
   3.168 +            keysym->mod = modstate;
   3.169              break;
   3.170          case SDLK_LCTRL:
   3.171              modstate |= KMOD_LCTRL;
   3.172 @@ -559,55 +576,63 @@
   3.173          default:
   3.174              break;
   3.175          }
   3.176 -        keysym->mod = (SDLMod) modstate;
   3.177 +        keysym->mod = modstate;
   3.178      }
   3.179  
   3.180      /* Figure out what type of event this is */
   3.181      switch (state) {
   3.182      case SDL_PRESSED:
   3.183 -        event.type = SDL_KEYDOWN;
   3.184 +        type = SDL_KEYDOWN;
   3.185          break;
   3.186      case SDL_RELEASED:
   3.187 -        event.type = SDL_KEYUP;
   3.188 +        type = SDL_KEYUP;
   3.189          /*
   3.190           * jk 991215 - Added
   3.191           */
   3.192 -        if (SDL_KeyRepeat.timestamp &&
   3.193 -            SDL_KeyRepeat.evt.key.keysym.sym == keysym->sym) {
   3.194 -            SDL_KeyRepeat.timestamp = 0;
   3.195 +        if (keyboard->repeat.timestamp &&
   3.196 +            keyboard->repeat.evt.key.keysym.sym == keysym->sym) {
   3.197 +            keyboard->repeat.timestamp = 0;
   3.198          }
   3.199          break;
   3.200      default:
   3.201          /* Invalid state -- bail */
   3.202 -        return (0);
   3.203 +        return 0;
   3.204      }
   3.205  
   3.206      if (keysym->sym != SDLK_UNKNOWN) {
   3.207          /* Drop events that don't change state */
   3.208 -        if (SDL_KeyState[keysym->sym] == state) {
   3.209 +        if (keyboard->keystate[keysym->sym] == state) {
   3.210  #if 0
   3.211              printf("Keyboard event didn't change state - dropped!\n");
   3.212  #endif
   3.213 -            return (0);
   3.214 +            return 0;
   3.215          }
   3.216  
   3.217          /* Update internal keyboard state */
   3.218 -        SDL_ModState = (SDLMod) modstate;
   3.219 -        SDL_KeyState[keysym->sym] = state;
   3.220 +        keyboard->modstate = modstate;
   3.221 +        keyboard->keystate[keysym->sym] = state;
   3.222      }
   3.223  
   3.224      /* Post the event, if desired */
   3.225      posted = 0;
   3.226 -    if (SDL_ProcessEvents[event.type] == SDL_ENABLE) {
   3.227 +    if (SDL_ProcessEvents[type] == SDL_ENABLE) {
   3.228 +        SDL_Event event;
   3.229 +        event.key.type = type;
   3.230 +        event.key.which = (Uint8) index;
   3.231          event.key.state = state;
   3.232          event.key.keysym = *keysym;
   3.233 +        event.key.windowID = keyboard->focus;
   3.234          /*
   3.235           * jk 991215 - Added
   3.236           */
   3.237 -        if (repeatable && (SDL_KeyRepeat.delay != 0)) {
   3.238 -            SDL_KeyRepeat.evt = event;
   3.239 -            SDL_KeyRepeat.firsttime = 1;
   3.240 -            SDL_KeyRepeat.timestamp = SDL_GetTicks();
   3.241 +        if (repeatable && (keyboard->repeat.delay != 0)) {
   3.242 +            Uint32 timestamp = SDL_GetTicks();
   3.243 +            if (!timestamp) {
   3.244 +                timestamp = 1;
   3.245 +            }
   3.246 +            keyboard->repeat.evt = event;
   3.247 +            keyboard->repeat.firsttime = 1;
   3.248 +            keyboard->repeat.timestamp = 1;
   3.249          }
   3.250          if ((SDL_EventOK == NULL) || SDL_EventOK(&event)) {
   3.251              posted = 1;
   3.252 @@ -623,22 +648,32 @@
   3.253  void
   3.254  SDL_CheckKeyRepeat(void)
   3.255  {
   3.256 -    if (SDL_KeyRepeat.timestamp) {
   3.257 -        Uint32 now, interval;
   3.258 +    int i;
   3.259  
   3.260 -        now = SDL_GetTicks();
   3.261 -        interval = (now - SDL_KeyRepeat.timestamp);
   3.262 -        if (SDL_KeyRepeat.firsttime) {
   3.263 -            if (interval > (Uint32) SDL_KeyRepeat.delay) {
   3.264 -                SDL_KeyRepeat.timestamp = now;
   3.265 -                SDL_KeyRepeat.firsttime = 0;
   3.266 -            }
   3.267 -        } else {
   3.268 -            if (interval > (Uint32) SDL_KeyRepeat.interval) {
   3.269 -                SDL_KeyRepeat.timestamp = now;
   3.270 -                if ((SDL_EventOK == NULL)
   3.271 -                    || SDL_EventOK(&SDL_KeyRepeat.evt)) {
   3.272 -                    SDL_PushEvent(&SDL_KeyRepeat.evt);
   3.273 +    for (i = 0; i < SDL_num_keyboards; ++i) {
   3.274 +        SDL_Keyboard *keyboard = SDL_keyboards[i];
   3.275 +
   3.276 +        if (!keyboard) {
   3.277 +            continue;
   3.278 +        }
   3.279 +
   3.280 +        if (keyboard->repeat.timestamp) {
   3.281 +            Uint32 now, interval;
   3.282 +
   3.283 +            now = SDL_GetTicks();
   3.284 +            interval = (now - keyboard->repeat.timestamp);
   3.285 +            if (keyboard->repeat.firsttime) {
   3.286 +                if (interval > (Uint32) keyboard->repeat.delay) {
   3.287 +                    keyboard->repeat.timestamp = now;
   3.288 +                    keyboard->repeat.firsttime = 0;
   3.289 +                }
   3.290 +            } else {
   3.291 +                if (interval > (Uint32) keyboard->repeat.interval) {
   3.292 +                    keyboard->repeat.timestamp = now;
   3.293 +                    if ((SDL_EventOK == NULL)
   3.294 +                        || SDL_EventOK(&keyboard->repeat.evt)) {
   3.295 +                        SDL_PushEvent(&keyboard->repeat.evt);
   3.296 +                    }
   3.297                  }
   3.298              }
   3.299          }
   3.300 @@ -648,22 +683,46 @@
   3.301  int
   3.302  SDL_EnableKeyRepeat(int delay, int interval)
   3.303  {
   3.304 +    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
   3.305 +
   3.306 +    if (!keyboard) {
   3.307 +        SDL_SetError("No keyboard is currently selected");
   3.308 +        return -1;
   3.309 +    }
   3.310 +
   3.311      if ((delay < 0) || (interval < 0)) {
   3.312          SDL_SetError("keyboard repeat value less than zero");
   3.313 -        return (-1);
   3.314 +        return -1;
   3.315      }
   3.316 -    SDL_KeyRepeat.firsttime = 0;
   3.317 -    SDL_KeyRepeat.delay = delay;
   3.318 -    SDL_KeyRepeat.interval = interval;
   3.319 -    SDL_KeyRepeat.timestamp = 0;
   3.320 -    return (0);
   3.321 +
   3.322 +    keyboard->repeat.firsttime = 0;
   3.323 +    keyboard->repeat.delay = delay;
   3.324 +    keyboard->repeat.interval = interval;
   3.325 +    keyboard->repeat.timestamp = 0;
   3.326 +
   3.327 +    return 0;
   3.328  }
   3.329  
   3.330  void
   3.331  SDL_GetKeyRepeat(int *delay, int *interval)
   3.332  {
   3.333 -    *delay = SDL_KeyRepeat.delay;
   3.334 -    *interval = SDL_KeyRepeat.interval;
   3.335 +    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
   3.336 +
   3.337 +    if (!keyboard) {
   3.338 +        if (delay) {
   3.339 +            *delay = 0;
   3.340 +        }
   3.341 +        if (interval) {
   3.342 +            *interval = 0;
   3.343 +        }
   3.344 +        return;
   3.345 +    }
   3.346 +    if (delay) {
   3.347 +        *delay = keyboard->repeat.delay;
   3.348 +    }
   3.349 +    if (interval) {
   3.350 +        *interval = keyboard->repeat.interval;
   3.351 +    }
   3.352  }
   3.353  
   3.354  /* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/src/events/SDL_keyboard_c.h	Sat Jun 10 09:11:59 2006 +0000
     4.2 +++ b/src/events/SDL_keyboard_c.h	Sun Jun 11 05:27:10 2006 +0000
     4.3 @@ -31,7 +31,9 @@
     4.4      /* Free the keyboard when it's time */
     4.5      void (*FreeKeyboard) (SDL_Keyboard * keyboard);
     4.6  
     4.7 -    SDLMod modstate;
     4.8 +    /* Data common to all keyboards */
     4.9 +    SDL_WindowID focus;
    4.10 +    Uint16 modstate;
    4.11      Uint8 keystate[SDLK_LAST];
    4.12  
    4.13      struct
    4.14 @@ -42,11 +44,16 @@
    4.15          Uint32 timestamp;       /* the time the first keydown event occurred */
    4.16  
    4.17          SDL_Event evt;          /* the event we are supposed to repeat */
    4.18 -    } keyrepeat;
    4.19 +    } repeat;
    4.20  
    4.21      void *driverdata;
    4.22  };
    4.23  
    4.24 +/* Used by the OS keyboard code to detect whether or not to do UNICODE */
    4.25 +#ifndef DEFAULT_UNICODE_TRANSLATION
    4.26 +#define DEFAULT_UNICODE_TRANSLATION 0   /* Default off because of overhead */
    4.27 +#endif
    4.28 +extern int SDL_TranslateUNICODE;
    4.29  
    4.30  /* Initialize the keyboard subsystem */
    4.31  extern int SDL_KeyboardInit(void);
    4.32 @@ -66,8 +73,11 @@
    4.33  extern void SDL_ResetKeyboard(int index);
    4.34  
    4.35  /* Send a keyboard event for a keyboard at an index */
    4.36 -extern int SDL_SendKeyboardKey(int index, Uint8 state,
    4.37 -                               const SDL_keysym * keysym);
    4.38 +extern int SDL_SendKeyboardKey(int index, SDL_WindowID windowID, Uint8 state,
    4.39 +                               SDL_keysym * keysym);
    4.40 +
    4.41 +/* Used by the event loop to queue pending keyboard repeat events */
    4.42 +extern void SDL_CheckKeyRepeat(void);
    4.43  
    4.44  /* Shutdown the keyboard subsystem */
    4.45  extern void SDL_KeyboardQuit(void);
     5.1 --- a/src/events/SDL_mouse.c	Sat Jun 10 09:11:59 2006 +0000
     5.2 +++ b/src/events/SDL_mouse.c	Sun Jun 11 05:27:10 2006 +0000
     5.3 @@ -25,7 +25,6 @@
     5.4  
     5.5  #include "SDL_events.h"
     5.6  #include "SDL_events_c.h"
     5.7 -#include "SDL_mouse_c.h"
     5.8  #include "default_cursor.h"
     5.9  
    5.10  
    5.11 @@ -333,7 +332,7 @@
    5.12          event.button.button = button;
    5.13          event.button.x = mouse->x;
    5.14          event.button.y = mouse->y;
    5.15 -        event.button.windowID = windowID;
    5.16 +        event.button.windowID = mouse->focus;
    5.17          if ((SDL_EventOK == NULL) || (*SDL_EventOK) (&event)) {
    5.18              posted = 1;
    5.19              SDL_PushEvent(&event);