src/events/SDL_keyboard.c
branchSDL-1.3
changeset 1674 7688a73b25b1
parent 1673 624e1412fbba
child 1679 153477a6cc31
     1.1 --- a/src/events/SDL_keyboard.c	Sat Jun 10 09:11:59 2006 +0000
     1.2 +++ b/src/events/SDL_keyboard.c	Sun Jun 11 05:27:10 2006 +0000
     1.3 @@ -30,10 +30,10 @@
     1.4  
     1.5  
     1.6  /* Global keyboard information */
     1.7 +int SDL_TranslateUNICODE = 0;
     1.8  static int SDL_num_keyboards;
     1.9  static int SDL_current_keyboard;
    1.10  static SDL_Keyboard **SDL_keyboards;
    1.11 -int SDL_TranslateUNICODE = 0;
    1.12  
    1.13  static const char *SDL_keynames[SDLK_LAST];     /* Array of keycode names */
    1.14  
    1.15 @@ -46,9 +46,6 @@
    1.16      /* Set default mode of UNICODE translation */
    1.17      SDL_EnableUNICODE(DEFAULT_UNICODE_TRANSLATION);
    1.18  
    1.19 -    /* Set default keyboard repeat setting */
    1.20 -    SDL_EnableKeyRepeat(0, 0);
    1.21 -
    1.22      /* Initialize the tables */
    1.23      for (i = 0; i < SDL_arraysize(SDL_keynames); ++i) {
    1.24          switch (i) {
    1.25 @@ -138,9 +135,6 @@
    1.26          case SDLK_LEFT:
    1.27              SDL_keynames[i] = "left";
    1.28              break;
    1.29 -        case SDLK_DOWN:
    1.30 -            SDL_keynames[i] = "down";
    1.31 -            break;
    1.32          case SDLK_INSERT:
    1.33              SDL_keynames[i] = "insert";
    1.34              break;
    1.35 @@ -347,7 +341,7 @@
    1.36  {
    1.37      SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
    1.38      SDL_keysym keysym;
    1.39 -    SDLKey key;
    1.40 +    Uint16 key;
    1.41  
    1.42      if (!keyboard) {
    1.43          return;
    1.44 @@ -357,10 +351,10 @@
    1.45      for (key = SDLK_FIRST; key < SDLK_LAST; ++key) {
    1.46          if (keyboard->keystate[key] == SDL_PRESSED) {
    1.47              keysym.sym = key;
    1.48 -            SDL_SendKeyboardKey(index, SDL_RELEASED, &keysym);
    1.49 +            SDL_SendKeyboardKey(index, 0, SDL_RELEASED, &keysym);
    1.50          }
    1.51      }
    1.52 -    keyboard->keyrepeat.timestamp = 0;
    1.53 +    keyboard->repeat.timestamp = 0;
    1.54  }
    1.55  
    1.56  void
    1.57 @@ -410,15 +404,22 @@
    1.58  Uint8 *
    1.59  SDL_GetKeyState(int *numkeys)
    1.60  {
    1.61 -    if (numkeys != (int *) 0)
    1.62 +    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
    1.63 +
    1.64 +    if (numkeys != (int *) 0) {
    1.65          *numkeys = SDLK_LAST;
    1.66 -    return (SDL_KeyState);
    1.67 +    }
    1.68 +
    1.69 +    if (!keyboard) {
    1.70 +        return NULL;
    1.71 +    }
    1.72 +    return keyboard->keystate;
    1.73  }
    1.74  
    1.75  SDLMod
    1.76  SDL_GetModState(void)
    1.77  {
    1.78 -    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
    1.79 +    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
    1.80  
    1.81      if (!keyboard) {
    1.82          return KMOD_NONE;
    1.83 @@ -429,7 +430,7 @@
    1.84  void
    1.85  SDL_SetModState(SDLMod modstate)
    1.86  {
    1.87 -    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
    1.88 +    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
    1.89  
    1.90      if (!keyboard) {
    1.91          return;
    1.92 @@ -442,11 +443,21 @@
    1.93  {
    1.94      const char *keyname;
    1.95  
    1.96 -    keyname = keynames[key];
    1.97 +    if (key < SDL_tablesize(SDL_keynames)) {
    1.98 +        keyname = SDL_keynames[key];
    1.99 +    } else {
   1.100 +        keyname = NULL;
   1.101 +    }
   1.102      if (keyname == NULL) {
   1.103          if (key < 256) {
   1.104              static char temp[4];
   1.105 -          FIXME:Convert to UTF - 8 keyname = temp;
   1.106 +            char *cvt;
   1.107 +            temp[0] = (char) key;
   1.108 +            temp[1] = '\0';
   1.109 +            cvt = SDL_iconv_string("UTF-8", "LATIN1", temp, 1);
   1.110 +            SDL_strlcpy(temp, cvt, SDL_arraysize(temp));
   1.111 +            SDL_free(cvt);
   1.112 +            keyname = temp;
   1.113          } else {
   1.114              keyname = "unknown key";
   1.115          }
   1.116 @@ -454,27 +465,33 @@
   1.117      return keyname;
   1.118  }
   1.119  
   1.120 -/* These are global for SDL_eventloop.c */
   1.121  int
   1.122 -SDL_SendKeyboardKey(int index, Uint8 state, const SDL_keysym * keysym)
   1.123 +SDL_SendKeyboardKey(int index, SDL_WindowID windowID, Uint8 state,
   1.124 +                    SDL_keysym * keysym)
   1.125  {
   1.126 -    SDL_Event event;
   1.127 +    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
   1.128      int posted, repeatable;
   1.129      Uint16 modstate;
   1.130 +    Uint8 type;
   1.131  
   1.132 -    SDL_memset(&event, 0, sizeof(event));
   1.133 +    if (!keyboard) {
   1.134 +        return 0;
   1.135 +    }
   1.136  
   1.137 +    if (windowID) {
   1.138 +        keyboard->focus = windowID;
   1.139 +    }
   1.140  #if 0
   1.141      printf("The '%s' key has been %s\n", SDL_GetKeyName(keysym->sym),
   1.142             state == SDL_PRESSED ? "pressed" : "released");
   1.143  #endif
   1.144      /* Set up the keysym */
   1.145 -    modstate = (Uint16) SDL_ModState;
   1.146 +    modstate = keyboard->modstate;
   1.147  
   1.148      repeatable = 0;
   1.149  
   1.150      if (state == SDL_PRESSED) {
   1.151 -        keysym->mod = (SDLMod) modstate;
   1.152 +        keysym->mod = modstate;
   1.153          switch (keysym->sym) {
   1.154          case SDLK_UNKNOWN:
   1.155              break;
   1.156 @@ -482,13 +499,13 @@
   1.157              modstate ^= KMOD_NUM;
   1.158              if (!(modstate & KMOD_NUM))
   1.159                  state = SDL_RELEASED;
   1.160 -            keysym->mod = (SDLMod) modstate;
   1.161 +            keysym->mod = modstate;
   1.162              break;
   1.163          case SDLK_CAPSLOCK:
   1.164              modstate ^= KMOD_CAPS;
   1.165              if (!(modstate & KMOD_CAPS))
   1.166                  state = SDL_RELEASED;
   1.167 -            keysym->mod = (SDLMod) modstate;
   1.168 +            keysym->mod = modstate;
   1.169              break;
   1.170          case SDLK_LCTRL:
   1.171              modstate |= KMOD_LCTRL;
   1.172 @@ -559,55 +576,63 @@
   1.173          default:
   1.174              break;
   1.175          }
   1.176 -        keysym->mod = (SDLMod) modstate;
   1.177 +        keysym->mod = modstate;
   1.178      }
   1.179  
   1.180      /* Figure out what type of event this is */
   1.181      switch (state) {
   1.182      case SDL_PRESSED:
   1.183 -        event.type = SDL_KEYDOWN;
   1.184 +        type = SDL_KEYDOWN;
   1.185          break;
   1.186      case SDL_RELEASED:
   1.187 -        event.type = SDL_KEYUP;
   1.188 +        type = SDL_KEYUP;
   1.189          /*
   1.190           * jk 991215 - Added
   1.191           */
   1.192 -        if (SDL_KeyRepeat.timestamp &&
   1.193 -            SDL_KeyRepeat.evt.key.keysym.sym == keysym->sym) {
   1.194 -            SDL_KeyRepeat.timestamp = 0;
   1.195 +        if (keyboard->repeat.timestamp &&
   1.196 +            keyboard->repeat.evt.key.keysym.sym == keysym->sym) {
   1.197 +            keyboard->repeat.timestamp = 0;
   1.198          }
   1.199          break;
   1.200      default:
   1.201          /* Invalid state -- bail */
   1.202 -        return (0);
   1.203 +        return 0;
   1.204      }
   1.205  
   1.206      if (keysym->sym != SDLK_UNKNOWN) {
   1.207          /* Drop events that don't change state */
   1.208 -        if (SDL_KeyState[keysym->sym] == state) {
   1.209 +        if (keyboard->keystate[keysym->sym] == state) {
   1.210  #if 0
   1.211              printf("Keyboard event didn't change state - dropped!\n");
   1.212  #endif
   1.213 -            return (0);
   1.214 +            return 0;
   1.215          }
   1.216  
   1.217          /* Update internal keyboard state */
   1.218 -        SDL_ModState = (SDLMod) modstate;
   1.219 -        SDL_KeyState[keysym->sym] = state;
   1.220 +        keyboard->modstate = modstate;
   1.221 +        keyboard->keystate[keysym->sym] = state;
   1.222      }
   1.223  
   1.224      /* Post the event, if desired */
   1.225      posted = 0;
   1.226 -    if (SDL_ProcessEvents[event.type] == SDL_ENABLE) {
   1.227 +    if (SDL_ProcessEvents[type] == SDL_ENABLE) {
   1.228 +        SDL_Event event;
   1.229 +        event.key.type = type;
   1.230 +        event.key.which = (Uint8) index;
   1.231          event.key.state = state;
   1.232          event.key.keysym = *keysym;
   1.233 +        event.key.windowID = keyboard->focus;
   1.234          /*
   1.235           * jk 991215 - Added
   1.236           */
   1.237 -        if (repeatable && (SDL_KeyRepeat.delay != 0)) {
   1.238 -            SDL_KeyRepeat.evt = event;
   1.239 -            SDL_KeyRepeat.firsttime = 1;
   1.240 -            SDL_KeyRepeat.timestamp = SDL_GetTicks();
   1.241 +        if (repeatable && (keyboard->repeat.delay != 0)) {
   1.242 +            Uint32 timestamp = SDL_GetTicks();
   1.243 +            if (!timestamp) {
   1.244 +                timestamp = 1;
   1.245 +            }
   1.246 +            keyboard->repeat.evt = event;
   1.247 +            keyboard->repeat.firsttime = 1;
   1.248 +            keyboard->repeat.timestamp = 1;
   1.249          }
   1.250          if ((SDL_EventOK == NULL) || SDL_EventOK(&event)) {
   1.251              posted = 1;
   1.252 @@ -623,22 +648,32 @@
   1.253  void
   1.254  SDL_CheckKeyRepeat(void)
   1.255  {
   1.256 -    if (SDL_KeyRepeat.timestamp) {
   1.257 -        Uint32 now, interval;
   1.258 +    int i;
   1.259  
   1.260 -        now = SDL_GetTicks();
   1.261 -        interval = (now - SDL_KeyRepeat.timestamp);
   1.262 -        if (SDL_KeyRepeat.firsttime) {
   1.263 -            if (interval > (Uint32) SDL_KeyRepeat.delay) {
   1.264 -                SDL_KeyRepeat.timestamp = now;
   1.265 -                SDL_KeyRepeat.firsttime = 0;
   1.266 -            }
   1.267 -        } else {
   1.268 -            if (interval > (Uint32) SDL_KeyRepeat.interval) {
   1.269 -                SDL_KeyRepeat.timestamp = now;
   1.270 -                if ((SDL_EventOK == NULL)
   1.271 -                    || SDL_EventOK(&SDL_KeyRepeat.evt)) {
   1.272 -                    SDL_PushEvent(&SDL_KeyRepeat.evt);
   1.273 +    for (i = 0; i < SDL_num_keyboards; ++i) {
   1.274 +        SDL_Keyboard *keyboard = SDL_keyboards[i];
   1.275 +
   1.276 +        if (!keyboard) {
   1.277 +            continue;
   1.278 +        }
   1.279 +
   1.280 +        if (keyboard->repeat.timestamp) {
   1.281 +            Uint32 now, interval;
   1.282 +
   1.283 +            now = SDL_GetTicks();
   1.284 +            interval = (now - keyboard->repeat.timestamp);
   1.285 +            if (keyboard->repeat.firsttime) {
   1.286 +                if (interval > (Uint32) keyboard->repeat.delay) {
   1.287 +                    keyboard->repeat.timestamp = now;
   1.288 +                    keyboard->repeat.firsttime = 0;
   1.289 +                }
   1.290 +            } else {
   1.291 +                if (interval > (Uint32) keyboard->repeat.interval) {
   1.292 +                    keyboard->repeat.timestamp = now;
   1.293 +                    if ((SDL_EventOK == NULL)
   1.294 +                        || SDL_EventOK(&keyboard->repeat.evt)) {
   1.295 +                        SDL_PushEvent(&keyboard->repeat.evt);
   1.296 +                    }
   1.297                  }
   1.298              }
   1.299          }
   1.300 @@ -648,22 +683,46 @@
   1.301  int
   1.302  SDL_EnableKeyRepeat(int delay, int interval)
   1.303  {
   1.304 +    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
   1.305 +
   1.306 +    if (!keyboard) {
   1.307 +        SDL_SetError("No keyboard is currently selected");
   1.308 +        return -1;
   1.309 +    }
   1.310 +
   1.311      if ((delay < 0) || (interval < 0)) {
   1.312          SDL_SetError("keyboard repeat value less than zero");
   1.313 -        return (-1);
   1.314 +        return -1;
   1.315      }
   1.316 -    SDL_KeyRepeat.firsttime = 0;
   1.317 -    SDL_KeyRepeat.delay = delay;
   1.318 -    SDL_KeyRepeat.interval = interval;
   1.319 -    SDL_KeyRepeat.timestamp = 0;
   1.320 -    return (0);
   1.321 +
   1.322 +    keyboard->repeat.firsttime = 0;
   1.323 +    keyboard->repeat.delay = delay;
   1.324 +    keyboard->repeat.interval = interval;
   1.325 +    keyboard->repeat.timestamp = 0;
   1.326 +
   1.327 +    return 0;
   1.328  }
   1.329  
   1.330  void
   1.331  SDL_GetKeyRepeat(int *delay, int *interval)
   1.332  {
   1.333 -    *delay = SDL_KeyRepeat.delay;
   1.334 -    *interval = SDL_KeyRepeat.interval;
   1.335 +    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
   1.336 +
   1.337 +    if (!keyboard) {
   1.338 +        if (delay) {
   1.339 +            *delay = 0;
   1.340 +        }
   1.341 +        if (interval) {
   1.342 +            *interval = 0;
   1.343 +        }
   1.344 +        return;
   1.345 +    }
   1.346 +    if (delay) {
   1.347 +        *delay = keyboard->repeat.delay;
   1.348 +    }
   1.349 +    if (interval) {
   1.350 +        *interval = keyboard->repeat.interval;
   1.351 +    }
   1.352  }
   1.353  
   1.354  /* vi: set ts=4 sw=4 expandtab: */