src/events/SDL_keyboard.c
changeset 4465 3e69e077cb95
parent 4437 25e45611fa3d
child 4490 06c7423f8c60
child 4750 22aa6a631d34
     1.1 --- a/src/events/SDL_keyboard.c	Sun May 09 19:55:28 2010 -0700
     1.2 +++ b/src/events/SDL_keyboard.c	Sun May 09 20:47:22 2010 -0700
     1.3 @@ -30,9 +30,19 @@
     1.4  
     1.5  
     1.6  /* Global keyboard information */
     1.7 -static int SDL_num_keyboards;
     1.8 -static int SDL_current_keyboard;
     1.9 -static SDL_Keyboard **SDL_keyboards;
    1.10 +
    1.11 +typedef struct SDL_Keyboard SDL_Keyboard;
    1.12 +
    1.13 +struct SDL_Keyboard
    1.14 +{
    1.15 +    /* Data common to all keyboards */
    1.16 +    SDL_Window *focus;
    1.17 +    Uint16 modstate;
    1.18 +    Uint8 keystate[SDL_NUM_SCANCODES];
    1.19 +    SDLKey keymap[SDL_NUM_SCANCODES];
    1.20 +};
    1.21 +
    1.22 +static SDL_Keyboard SDL_keyboard;
    1.23  
    1.24  static const SDLKey SDL_default_keymap[SDL_NUM_SCANCODES] = {
    1.25      0, 0, 0, 0,
    1.26 @@ -544,75 +554,15 @@
    1.27      return (0);
    1.28  }
    1.29  
    1.30 -SDL_Keyboard *
    1.31 -SDL_GetKeyboard(int index)
    1.32 +void
    1.33 +SDL_ResetKeyboard(void)
    1.34  {
    1.35 -    if (index < 0 || index >= SDL_num_keyboards) {
    1.36 -        return NULL;
    1.37 -    }
    1.38 -    return SDL_keyboards[index];
    1.39 -}
    1.40 -
    1.41 -int
    1.42 -SDL_AddKeyboard(const SDL_Keyboard * keyboard, int index)
    1.43 -{
    1.44 -    SDL_Keyboard **keyboards;
    1.45 -
    1.46 -    /* Add the keyboard to the list of keyboards */
    1.47 -    if (index < 0 || index >= SDL_num_keyboards || SDL_keyboards[index]) {
    1.48 -        keyboards =
    1.49 -            (SDL_Keyboard **) SDL_realloc(SDL_keyboards,
    1.50 -                                          (SDL_num_keyboards +
    1.51 -                                           1) * sizeof(*keyboards));
    1.52 -        if (!keyboards) {
    1.53 -            SDL_OutOfMemory();
    1.54 -            return -1;
    1.55 -        }
    1.56 -
    1.57 -        SDL_keyboards = keyboards;
    1.58 -        index = SDL_num_keyboards++;
    1.59 -    }
    1.60 -    SDL_keyboards[index] =
    1.61 -        (SDL_Keyboard *) SDL_malloc(sizeof(*SDL_keyboards[index]));
    1.62 -    if (!SDL_keyboards[index]) {
    1.63 -        SDL_OutOfMemory();
    1.64 -        return -1;
    1.65 -    }
    1.66 -    *SDL_keyboards[index] = *keyboard;
    1.67 -
    1.68 -    return index;
    1.69 -}
    1.70 -
    1.71 -void
    1.72 -SDL_DelKeyboard(int index)
    1.73 -{
    1.74 -    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
    1.75 -
    1.76 -    if (!keyboard) {
    1.77 -        return;
    1.78 -    }
    1.79 -
    1.80 -    if (keyboard->FreeKeyboard) {
    1.81 -        keyboard->FreeKeyboard(keyboard);
    1.82 -    }
    1.83 -    SDL_free(keyboard);
    1.84 -
    1.85 -    SDL_keyboards[index] = NULL;
    1.86 -}
    1.87 -
    1.88 -void
    1.89 -SDL_ResetKeyboard(int index)
    1.90 -{
    1.91 -    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
    1.92 +    SDL_Keyboard *keyboard = &SDL_keyboard;
    1.93      SDL_scancode scancode;
    1.94  
    1.95 -    if (!keyboard) {
    1.96 -        return;
    1.97 -    }
    1.98 -
    1.99      for (scancode = 0; scancode < SDL_NUM_SCANCODES; ++scancode) {
   1.100          if (keyboard->keystate[scancode] == SDL_PRESSED) {
   1.101 -            SDL_SendKeyboardKey(index, SDL_RELEASED, scancode);
   1.102 +            SDL_SendKeyboardKey(SDL_RELEASED, scancode);
   1.103          }
   1.104      }
   1.105  }
   1.106 @@ -624,13 +574,9 @@
   1.107  }
   1.108  
   1.109  void
   1.110 -SDL_SetKeymap(int index, int start, SDLKey * keys, int length)
   1.111 +SDL_SetKeymap(int start, SDLKey * keys, int length)
   1.112  {
   1.113 -    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
   1.114 -
   1.115 -    if (!keyboard) {
   1.116 -        return;
   1.117 -    }
   1.118 +    SDL_Keyboard *keyboard = &SDL_keyboard;
   1.119  
   1.120      if (start < 0 || start + length > SDL_NUM_SCANCODES) {
   1.121          return;
   1.122 @@ -645,33 +591,23 @@
   1.123      SDL_scancode_names[scancode] = name;
   1.124  }
   1.125  
   1.126 +SDL_Window *
   1.127 +SDL_GetKeyboardFocus(void)
   1.128 +{
   1.129 +    SDL_Keyboard *keyboard = &SDL_keyboard;
   1.130 +
   1.131 +    return keyboard->focus;
   1.132 +}
   1.133 +
   1.134  void
   1.135 -SDL_SetKeyboardFocus(int index, SDL_Window * window)
   1.136 +SDL_SetKeyboardFocus(SDL_Window * window)
   1.137  {
   1.138 -    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
   1.139 -    int i;
   1.140 -    SDL_bool focus;
   1.141 -
   1.142 -    if (!keyboard) {
   1.143 -        return;
   1.144 -    }
   1.145 +    SDL_Keyboard *keyboard = &SDL_keyboard;
   1.146  
   1.147      /* See if the current window has lost focus */
   1.148      if (keyboard->focus && keyboard->focus != window) {
   1.149 -        focus = SDL_FALSE;
   1.150 -        for (i = 0; i < SDL_num_keyboards; ++i) {
   1.151 -            if (i != index) {
   1.152 -                SDL_Keyboard *check = SDL_GetKeyboard(i);
   1.153 -                if (check && check->focus == keyboard->focus) {
   1.154 -                    focus = SDL_TRUE;
   1.155 -                    break;
   1.156 -                }
   1.157 -            }
   1.158 -        }
   1.159 -        if (!focus) {
   1.160 -            SDL_SendWindowEvent(keyboard->focus, SDL_WINDOWEVENT_FOCUS_LOST,
   1.161 -                                0, 0);
   1.162 -        }
   1.163 +        SDL_SendWindowEvent(keyboard->focus, SDL_WINDOWEVENT_FOCUS_LOST,
   1.164 +                            0, 0);
   1.165      }
   1.166  
   1.167      keyboard->focus = window;
   1.168 @@ -687,14 +623,14 @@
   1.169  }
   1.170  
   1.171  int
   1.172 -SDL_SendKeyboardKey(int index, Uint8 state, SDL_scancode scancode)
   1.173 +SDL_SendKeyboardKey(Uint8 state, SDL_scancode scancode)
   1.174  {
   1.175 -    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
   1.176 +    SDL_Keyboard *keyboard = &SDL_keyboard;
   1.177      int posted;
   1.178      Uint16 modstate;
   1.179      Uint32 type;
   1.180  
   1.181 -    if (!keyboard || !scancode) {
   1.182 +    if (!scancode) {
   1.183          return 0;
   1.184      }
   1.185  #if 0
   1.186 @@ -807,7 +743,6 @@
   1.187      if (SDL_GetEventState(type) == SDL_ENABLE) {
   1.188          SDL_Event event;
   1.189          event.key.type = type;
   1.190 -        event.key.which = (Uint8) index;
   1.191          event.key.state = state;
   1.192          event.key.keysym.scancode = scancode;
   1.193          event.key.keysym.sym = keyboard->keymap[scancode];
   1.194 @@ -820,22 +755,17 @@
   1.195  }
   1.196  
   1.197  int
   1.198 -SDL_SendKeyboardText(int index, const char *text)
   1.199 +SDL_SendKeyboardText(const char *text)
   1.200  {
   1.201 -    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
   1.202 +    SDL_Keyboard *keyboard = &SDL_keyboard;
   1.203      int posted;
   1.204  
   1.205 -    if (!keyboard) {
   1.206 -        return 0;
   1.207 -    }
   1.208 -
   1.209      /* Post the event, if desired */
   1.210      posted = 0;
   1.211      if (SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE) {
   1.212          SDL_Event event;
   1.213          event.text.type = SDL_TEXTINPUT;
   1.214          event.text.windowID = keyboard->focus ? keyboard->focus->id : 0;
   1.215 -        event.text.which = (Uint8) index;
   1.216          SDL_strlcpy(event.text.text, text, SDL_arraysize(event.text.text));
   1.217          event.text.windowID = keyboard->focus ? keyboard->focus->id : 0;
   1.218          posted = (SDL_PushEvent(&event) > 0);
   1.219 @@ -844,22 +774,17 @@
   1.220  }
   1.221  
   1.222  int
   1.223 -SDL_SendEditingText(int index, const char *text, int start, int length)
   1.224 +SDL_SendEditingText(const char *text, int start, int length)
   1.225  {
   1.226 -    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
   1.227 +    SDL_Keyboard *keyboard = &SDL_keyboard;
   1.228      int posted;
   1.229  
   1.230 -    if (!keyboard) {
   1.231 -        return 0;
   1.232 -    }
   1.233 -
   1.234      /* Post the event, if desired */
   1.235      posted = 0;
   1.236      if (SDL_GetEventState(SDL_TEXTEDITING) == SDL_ENABLE) {
   1.237          SDL_Event event;
   1.238          event.edit.type = SDL_TEXTEDITING;
   1.239          event.edit.windowID = keyboard->focus ? keyboard->focus->id : 0;
   1.240 -        event.text.which = (Uint8) index;
   1.241          event.edit.start = start;
   1.242          event.edit.length = length;
   1.243          SDL_strlcpy(event.edit.text, text, SDL_arraysize(event.edit.text));
   1.244 @@ -871,93 +796,49 @@
   1.245  void
   1.246  SDL_KeyboardQuit(void)
   1.247  {
   1.248 -    int i;
   1.249 -
   1.250 -    for (i = 0; i < SDL_num_keyboards; ++i) {
   1.251 -        SDL_DelKeyboard(i);
   1.252 -    }
   1.253 -    SDL_num_keyboards = 0;
   1.254 -    SDL_current_keyboard = 0;
   1.255 -
   1.256 -    if (SDL_keyboards) {
   1.257 -        SDL_free(SDL_keyboards);
   1.258 -        SDL_keyboards = NULL;
   1.259 -    }
   1.260 -}
   1.261 -
   1.262 -int
   1.263 -SDL_GetNumKeyboards(void)
   1.264 -{
   1.265 -    return SDL_num_keyboards;
   1.266 -}
   1.267 -
   1.268 -int
   1.269 -SDL_SelectKeyboard(int index)
   1.270 -{
   1.271 -    if (index >= 0 && index < SDL_num_keyboards) {
   1.272 -        SDL_current_keyboard = index;
   1.273 -    }
   1.274 -    return SDL_current_keyboard;
   1.275  }
   1.276  
   1.277  Uint8 *
   1.278  SDL_GetKeyboardState(int *numkeys)
   1.279  {
   1.280 -    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
   1.281 +    SDL_Keyboard *keyboard = &SDL_keyboard;
   1.282  
   1.283      if (numkeys != (int *) 0) {
   1.284          *numkeys = SDL_NUM_SCANCODES;
   1.285      }
   1.286 -
   1.287 -    if (!keyboard) {
   1.288 -        return NULL;
   1.289 -    }
   1.290      return keyboard->keystate;
   1.291  }
   1.292  
   1.293  SDLMod
   1.294  SDL_GetModState(void)
   1.295  {
   1.296 -    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
   1.297 +    SDL_Keyboard *keyboard = &SDL_keyboard;
   1.298  
   1.299 -    if (!keyboard) {
   1.300 -        return KMOD_NONE;
   1.301 -    }
   1.302      return keyboard->modstate;
   1.303  }
   1.304  
   1.305  void
   1.306  SDL_SetModState(SDLMod modstate)
   1.307  {
   1.308 -    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
   1.309 +    SDL_Keyboard *keyboard = &SDL_keyboard;
   1.310  
   1.311 -    if (!keyboard) {
   1.312 -        return;
   1.313 -    }
   1.314      keyboard->modstate = modstate;
   1.315  }
   1.316  
   1.317  SDLKey
   1.318  SDL_GetKeyFromScancode(SDL_scancode scancode)
   1.319  {
   1.320 -    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
   1.321 +    SDL_Keyboard *keyboard = &SDL_keyboard;
   1.322  
   1.323 -    if (!keyboard) {
   1.324 -        return SDLK_UNKNOWN;
   1.325 -    }
   1.326      return keyboard->keymap[scancode];
   1.327  }
   1.328  
   1.329  SDL_scancode
   1.330  SDL_GetScancodeFromKey(SDLKey key)
   1.331  {
   1.332 -    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
   1.333 +    SDL_Keyboard *keyboard = &SDL_keyboard;
   1.334      SDL_scancode scancode;
   1.335  
   1.336 -    if (!keyboard) {
   1.337 -        return SDL_SCANCODE_UNKNOWN;
   1.338 -    }
   1.339 -
   1.340      for (scancode = SDL_SCANCODE_UNKNOWN; scancode < SDL_NUM_SCANCODES;
   1.341           ++scancode) {
   1.342          if (keyboard->keymap[scancode] == key) {