src/events/SDL_keyboard.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 08 Jul 2006 20:07:08 +0000
branchSDL-1.3
changeset 1731 875c3cf1a12c
parent 1724 6c63fc2bd986
child 1732 fd65f12b6de6
permissions -rw-r--r--
SDL_PushEvent() calls the event filter code, and has a return value to tell
whether or not the event was actually pushed.
SDL_GetEventFilter() now returns an SDL_bool instead of the filter function.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* General keyboard handling code for SDL */
slouken@0
    25
slouken@1358
    26
#include "SDL_timer.h"
slouken@0
    27
#include "SDL_events.h"
slouken@0
    28
#include "SDL_events_c.h"
slouken@0
    29
#include "SDL_sysevents.h"
slouken@0
    30
slouken@0
    31
slouken@1673
    32
/* Global keyboard information */
slouken@1674
    33
int SDL_TranslateUNICODE = 0;
slouken@1673
    34
static int SDL_num_keyboards;
slouken@1673
    35
static int SDL_current_keyboard;
slouken@1673
    36
static SDL_Keyboard **SDL_keyboards;
slouken@0
    37
slouken@1673
    38
static const char *SDL_keynames[SDLK_LAST];     /* Array of keycode names */
slouken@0
    39
slouken@0
    40
/* Public functions */
slouken@1662
    41
int
slouken@1668
    42
SDL_KeyboardInit(void)
slouken@0
    43
{
slouken@1673
    44
    int i;
slouken@0
    45
slouken@1662
    46
    /* Set default mode of UNICODE translation */
slouken@1668
    47
    SDL_EnableUNICODE(DEFAULT_UNICODE_TRANSLATION);
slouken@0
    48
slouken@1673
    49
    /* Initialize the tables */
slouken@1673
    50
    for (i = 0; i < SDL_arraysize(SDL_keynames); ++i) {
slouken@1673
    51
        switch (i) {
slouken@1673
    52
        case SDLK_BACKSPACE:
slouken@1673
    53
            SDL_keynames[i] = "backspace";
slouken@1673
    54
            break;
slouken@1673
    55
        case SDLK_TAB:
slouken@1673
    56
            SDL_keynames[i] = "tab";
slouken@1673
    57
            break;
slouken@1673
    58
        case SDLK_CLEAR:
slouken@1673
    59
            SDL_keynames[i] = "clear";
slouken@1673
    60
            break;
slouken@1673
    61
        case SDLK_RETURN:
slouken@1673
    62
            SDL_keynames[i] = "return";
slouken@1673
    63
            break;
slouken@1673
    64
        case SDLK_PAUSE:
slouken@1673
    65
            SDL_keynames[i] = "pause";
slouken@1673
    66
            break;
slouken@1673
    67
        case SDLK_ESCAPE:
slouken@1673
    68
            SDL_keynames[i] = "escape";
slouken@1673
    69
            break;
slouken@1673
    70
        case SDLK_SPACE:
slouken@1673
    71
            SDL_keynames[i] = "space";
slouken@1673
    72
            break;
slouken@0
    73
slouken@1673
    74
        case SDLK_KP0:
slouken@1673
    75
            SDL_keynames[i] = "[0]";
slouken@1673
    76
            break;
slouken@1673
    77
        case SDLK_KP1:
slouken@1673
    78
            SDL_keynames[i] = "[1]";
slouken@1673
    79
            break;
slouken@1673
    80
        case SDLK_KP2:
slouken@1673
    81
            SDL_keynames[i] = "[2]";
slouken@1673
    82
            break;
slouken@1673
    83
        case SDLK_KP3:
slouken@1673
    84
            SDL_keynames[i] = "[3]";
slouken@1673
    85
            break;
slouken@1673
    86
        case SDLK_KP4:
slouken@1673
    87
            SDL_keynames[i] = "[4]";
slouken@1673
    88
            break;
slouken@1673
    89
        case SDLK_KP5:
slouken@1673
    90
            SDL_keynames[i] = "[5]";
slouken@1673
    91
            break;
slouken@1673
    92
        case SDLK_KP6:
slouken@1673
    93
            SDL_keynames[i] = "[6]";
slouken@1673
    94
            break;
slouken@1673
    95
        case SDLK_KP7:
slouken@1673
    96
            SDL_keynames[i] = "[7]";
slouken@1673
    97
            break;
slouken@1673
    98
        case SDLK_KP8:
slouken@1673
    99
            SDL_keynames[i] = "[8]";
slouken@1673
   100
            break;
slouken@1673
   101
        case SDLK_KP9:
slouken@1673
   102
            SDL_keynames[i] = "[9]";
slouken@1673
   103
            break;
slouken@1673
   104
        case SDLK_KP_PERIOD:
slouken@1673
   105
            SDL_keynames[i] = "[.]";
slouken@1673
   106
            break;
slouken@1673
   107
        case SDLK_KP_DIVIDE:
slouken@1673
   108
            SDL_keynames[i] = "[/]";
slouken@1673
   109
            break;
slouken@1673
   110
        case SDLK_KP_MULTIPLY:
slouken@1673
   111
            SDL_keynames[i] = "[*]";
slouken@1673
   112
            break;
slouken@1673
   113
        case SDLK_KP_MINUS:
slouken@1673
   114
            SDL_keynames[i] = "[-]";
slouken@1673
   115
            break;
slouken@1673
   116
        case SDLK_KP_PLUS:
slouken@1673
   117
            SDL_keynames[i] = "[+]";
slouken@1673
   118
            break;
slouken@1673
   119
        case SDLK_KP_ENTER:
slouken@1673
   120
            SDL_keynames[i] = "enter";
slouken@1673
   121
            break;
slouken@1673
   122
        case SDLK_KP_EQUALS:
slouken@1673
   123
            SDL_keynames[i] = "equals";
slouken@1673
   124
            break;
slouken@0
   125
slouken@1673
   126
        case SDLK_UP:
slouken@1673
   127
            SDL_keynames[i] = "up";
slouken@1673
   128
            break;
slouken@1673
   129
        case SDLK_DOWN:
slouken@1673
   130
            SDL_keynames[i] = "down";
slouken@1673
   131
            break;
slouken@1673
   132
        case SDLK_RIGHT:
slouken@1673
   133
            SDL_keynames[i] = "right";
slouken@1673
   134
            break;
slouken@1673
   135
        case SDLK_LEFT:
slouken@1673
   136
            SDL_keynames[i] = "left";
slouken@1673
   137
            break;
slouken@1673
   138
        case SDLK_INSERT:
slouken@1673
   139
            SDL_keynames[i] = "insert";
slouken@1673
   140
            break;
slouken@1673
   141
        case SDLK_HOME:
slouken@1673
   142
            SDL_keynames[i] = "home";
slouken@1673
   143
            break;
slouken@1673
   144
        case SDLK_END:
slouken@1673
   145
            SDL_keynames[i] = "end";
slouken@1673
   146
            break;
slouken@1673
   147
        case SDLK_PAGEUP:
slouken@1673
   148
            SDL_keynames[i] = "page up";
slouken@1673
   149
            break;
slouken@1673
   150
        case SDLK_PAGEDOWN:
slouken@1673
   151
            SDL_keynames[i] = "page down";
slouken@1673
   152
            break;
slouken@0
   153
slouken@1673
   154
        case SDLK_F1:
slouken@1673
   155
            SDL_keynames[i] = "f1";
slouken@1673
   156
            break;
slouken@1673
   157
        case SDLK_F2:
slouken@1673
   158
            SDL_keynames[i] = "f2";
slouken@1673
   159
            break;
slouken@1673
   160
        case SDLK_F3:
slouken@1673
   161
            SDL_keynames[i] = "f3";
slouken@1673
   162
            break;
slouken@1673
   163
        case SDLK_F4:
slouken@1673
   164
            SDL_keynames[i] = "f4";
slouken@1673
   165
            break;
slouken@1673
   166
        case SDLK_F5:
slouken@1673
   167
            SDL_keynames[i] = "f5";
slouken@1673
   168
            break;
slouken@1673
   169
        case SDLK_F6:
slouken@1673
   170
            SDL_keynames[i] = "f6";
slouken@1673
   171
            break;
slouken@1673
   172
        case SDLK_F7:
slouken@1673
   173
            SDL_keynames[i] = "f7";
slouken@1673
   174
            break;
slouken@1673
   175
        case SDLK_F8:
slouken@1673
   176
            SDL_keynames[i] = "f8";
slouken@1673
   177
            break;
slouken@1673
   178
        case SDLK_F9:
slouken@1673
   179
            SDL_keynames[i] = "f9";
slouken@1673
   180
            break;
slouken@1673
   181
        case SDLK_F10:
slouken@1673
   182
            SDL_keynames[i] = "f10";
slouken@1673
   183
            break;
slouken@1673
   184
        case SDLK_F11:
slouken@1673
   185
            SDL_keynames[i] = "f11";
slouken@1673
   186
            break;
slouken@1673
   187
        case SDLK_F12:
slouken@1673
   188
            SDL_keynames[i] = "f12";
slouken@1673
   189
            break;
slouken@1673
   190
        case SDLK_F13:
slouken@1673
   191
            SDL_keynames[i] = "f13";
slouken@1673
   192
            break;
slouken@1673
   193
        case SDLK_F14:
slouken@1673
   194
            SDL_keynames[i] = "f14";
slouken@1673
   195
            break;
slouken@1673
   196
        case SDLK_F15:
slouken@1673
   197
            SDL_keynames[i] = "f15";
slouken@1673
   198
            break;
slouken@0
   199
slouken@1673
   200
        case SDLK_NUMLOCK:
slouken@1673
   201
            SDL_keynames[i] = "numlock";
slouken@1673
   202
            break;
slouken@1673
   203
        case SDLK_CAPSLOCK:
slouken@1673
   204
            SDL_keynames[i] = "caps lock";
slouken@1673
   205
            break;
slouken@1673
   206
        case SDLK_SCROLLOCK:
slouken@1673
   207
            SDL_keynames[i] = "scroll lock";
slouken@1673
   208
            break;
slouken@1673
   209
        case SDLK_RSHIFT:
slouken@1673
   210
            SDL_keynames[i] = "right shift";
slouken@1673
   211
            break;
slouken@1673
   212
        case SDLK_LSHIFT:
slouken@1673
   213
            SDL_keynames[i] = "left shift";
slouken@1673
   214
            break;
slouken@1673
   215
        case SDLK_RCTRL:
slouken@1673
   216
            SDL_keynames[i] = "right ctrl";
slouken@1673
   217
            break;
slouken@1673
   218
        case SDLK_LCTRL:
slouken@1673
   219
            SDL_keynames[i] = "left ctrl";
slouken@1673
   220
            break;
slouken@1673
   221
        case SDLK_RALT:
slouken@1673
   222
            SDL_keynames[i] = "right alt";
slouken@1673
   223
            break;
slouken@1673
   224
        case SDLK_LALT:
slouken@1673
   225
            SDL_keynames[i] = "left alt";
slouken@1673
   226
            break;
slouken@1673
   227
        case SDLK_RMETA:
slouken@1673
   228
            SDL_keynames[i] = "right meta";
slouken@1673
   229
            break;
slouken@1673
   230
        case SDLK_LMETA:
slouken@1673
   231
            SDL_keynames[i] = "left meta";
slouken@1673
   232
            break;
slouken@1673
   233
        case SDLK_LSUPER:
slouken@1673
   234
            SDL_keynames[i] = "left super";     /* "Windows" keys */
slouken@1673
   235
            break;
slouken@1673
   236
        case SDLK_RSUPER:
slouken@1673
   237
            SDL_keynames[i] = "right super";
slouken@1673
   238
            break;
slouken@1673
   239
        case SDLK_MODE:
slouken@1673
   240
            SDL_keynames[i] = "alt gr";
slouken@1673
   241
            break;
slouken@1673
   242
        case SDLK_COMPOSE:
slouken@1673
   243
            SDL_keynames[i] = "compose";
slouken@1673
   244
            break;
slouken@0
   245
slouken@1673
   246
        case SDLK_HELP:
slouken@1673
   247
            SDL_keynames[i] = "help";
slouken@1673
   248
            break;
slouken@1673
   249
        case SDLK_PRINT:
slouken@1673
   250
            SDL_keynames[i] = "print screen";
slouken@1673
   251
            break;
slouken@1673
   252
        case SDLK_SYSREQ:
slouken@1673
   253
            SDL_keynames[i] = "sys req";
slouken@1673
   254
            break;
slouken@1673
   255
        case SDLK_BREAK:
slouken@1673
   256
            SDL_keynames[i] = "break";
slouken@1673
   257
            break;
slouken@1673
   258
        case SDLK_MENU:
slouken@1673
   259
            SDL_keynames[i] = "menu";
slouken@1673
   260
            break;
slouken@1673
   261
        case SDLK_POWER:
slouken@1673
   262
            SDL_keynames[i] = "power";
slouken@1673
   263
            break;
slouken@1673
   264
        case SDLK_EURO:
slouken@1673
   265
            SDL_keynames[i] = "euro";
slouken@1673
   266
            break;
slouken@1673
   267
        case SDLK_UNDO:
slouken@1673
   268
            SDL_keynames[i] = "undo";
slouken@1673
   269
            break;
slouken@0
   270
slouken@1673
   271
        default:
slouken@1673
   272
            SDL_keynames[i] = NULL;
slouken@1673
   273
            break;
slouken@1673
   274
        }
slouken@1673
   275
    }
slouken@0
   276
slouken@1662
   277
    /* Done.  Whew. */
slouken@1662
   278
    return (0);
slouken@0
   279
}
slouken@1662
   280
slouken@1673
   281
SDL_Keyboard *
slouken@1673
   282
SDL_GetKeyboard(int index)
slouken@1673
   283
{
slouken@1673
   284
    if (index < 0 || index >= SDL_num_keyboards) {
slouken@1673
   285
        return NULL;
slouken@1673
   286
    }
slouken@1673
   287
    return SDL_keyboards[index];
slouken@1673
   288
}
slouken@1673
   289
slouken@1673
   290
int
slouken@1673
   291
SDL_AddKeyboard(const SDL_Keyboard * keyboard, int index)
slouken@1673
   292
{
slouken@1673
   293
    SDL_Keyboard **keyboards;
slouken@1673
   294
slouken@1673
   295
    /* Add the keyboard to the list of keyboards */
slouken@1673
   296
    if (index < 0 || index >= SDL_num_keyboards || SDL_keyboards[index]) {
slouken@1673
   297
        keyboards =
slouken@1673
   298
            (SDL_Keyboard **) SDL_realloc(SDL_keyboards,
slouken@1673
   299
                                          (SDL_num_keyboards +
slouken@1673
   300
                                           1) * sizeof(*keyboards));
slouken@1673
   301
        if (!keyboards) {
slouken@1673
   302
            SDL_OutOfMemory();
slouken@1673
   303
            return -1;
slouken@1673
   304
        }
slouken@1673
   305
slouken@1673
   306
        SDL_keyboards = keyboards;
slouken@1673
   307
        index = SDL_num_keyboards++;
slouken@1673
   308
    }
slouken@1673
   309
    SDL_keyboards[index] =
slouken@1673
   310
        (SDL_Keyboard *) SDL_malloc(sizeof(*SDL_keyboards[index]));
slouken@1673
   311
    if (!SDL_keyboards[index]) {
slouken@1673
   312
        SDL_OutOfMemory();
slouken@1673
   313
        return -1;
slouken@1673
   314
    }
slouken@1673
   315
    *SDL_keyboards[index] = *keyboard;
slouken@1673
   316
slouken@1673
   317
    return index;
slouken@1673
   318
}
slouken@1673
   319
slouken@1673
   320
void
slouken@1673
   321
SDL_DelKeyboard(int index)
slouken@1673
   322
{
slouken@1673
   323
    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
slouken@1673
   324
slouken@1673
   325
    if (!keyboard) {
slouken@1673
   326
        return;
slouken@1673
   327
    }
slouken@1673
   328
slouken@1673
   329
    if (keyboard->FreeKeyboard) {
slouken@1673
   330
        keyboard->FreeKeyboard(keyboard);
slouken@1673
   331
    }
slouken@1673
   332
    SDL_free(keyboard);
slouken@1673
   333
slouken@1673
   334
    SDL_keyboards[index] = NULL;
slouken@1673
   335
}
slouken@1673
   336
slouken@1673
   337
void
slouken@1673
   338
SDL_ResetKeyboard(int index)
slouken@1673
   339
{
slouken@1673
   340
    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
slouken@1724
   341
    SDLKey key;
slouken@1673
   342
slouken@1673
   343
    if (!keyboard) {
slouken@1673
   344
        return;
slouken@1673
   345
    }
slouken@1673
   346
slouken@1673
   347
    for (key = SDLK_FIRST; key < SDLK_LAST; ++key) {
slouken@1673
   348
        if (keyboard->keystate[key] == SDL_PRESSED) {
slouken@1724
   349
            SDL_SendKeyboardKey(index, SDL_RELEASED, 0, key);
slouken@1673
   350
        }
slouken@1673
   351
    }
slouken@1674
   352
    keyboard->repeat.timestamp = 0;
slouken@1673
   353
}
slouken@1673
   354
slouken@1662
   355
void
slouken@1668
   356
SDL_KeyboardQuit(void)
slouken@1123
   357
{
slouken@1673
   358
    int i;
slouken@1673
   359
slouken@1673
   360
    for (i = 0; i < SDL_num_keyboards; ++i) {
slouken@1673
   361
        SDL_DelKeyboard(i);
slouken@1673
   362
    }
slouken@1673
   363
    SDL_num_keyboards = 0;
slouken@1673
   364
    SDL_current_keyboard = 0;
slouken@1673
   365
slouken@1673
   366
    if (SDL_keyboards) {
slouken@1673
   367
        SDL_free(SDL_keyboards);
slouken@1673
   368
        SDL_keyboards = NULL;
slouken@1673
   369
    }
slouken@1123
   370
}
slouken@0
   371
slouken@1673
   372
int
slouken@1673
   373
SDL_GetNumKeyboards(void)
slouken@0
   374
{
slouken@1673
   375
    return SDL_num_keyboards;
slouken@1673
   376
}
slouken@0
   377
slouken@1673
   378
int
slouken@1673
   379
SDL_SelectKeyboard(int index)
slouken@1673
   380
{
slouken@1673
   381
    if (index >= 0 && index < SDL_num_keyboards) {
slouken@1673
   382
        SDL_current_keyboard = index;
slouken@1662
   383
    }
slouken@1673
   384
    return SDL_current_keyboard;
slouken@0
   385
}
slouken@0
   386
slouken@1662
   387
int
slouken@1668
   388
SDL_EnableUNICODE(int enable)
slouken@0
   389
{
slouken@1662
   390
    int old_mode;
slouken@0
   391
slouken@1662
   392
    old_mode = SDL_TranslateUNICODE;
slouken@1662
   393
    if (enable >= 0) {
slouken@1662
   394
        SDL_TranslateUNICODE = enable;
slouken@1662
   395
    }
slouken@1662
   396
    return (old_mode);
slouken@0
   397
}
slouken@0
   398
slouken@1662
   399
Uint8 *
slouken@1668
   400
SDL_GetKeyState(int *numkeys)
slouken@0
   401
{
slouken@1674
   402
    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
slouken@1674
   403
slouken@1674
   404
    if (numkeys != (int *) 0) {
slouken@1662
   405
        *numkeys = SDLK_LAST;
slouken@1674
   406
    }
slouken@1674
   407
slouken@1674
   408
    if (!keyboard) {
slouken@1674
   409
        return NULL;
slouken@1674
   410
    }
slouken@1674
   411
    return keyboard->keystate;
slouken@0
   412
}
slouken@0
   413
slouken@1662
   414
SDLMod
slouken@1668
   415
SDL_GetModState(void)
slouken@0
   416
{
slouken@1674
   417
    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
slouken@1673
   418
slouken@1673
   419
    if (!keyboard) {
slouken@1673
   420
        return KMOD_NONE;
slouken@1673
   421
    }
slouken@1673
   422
    return keyboard->modstate;
slouken@1662
   423
}
slouken@0
   424
slouken@1662
   425
void
slouken@1668
   426
SDL_SetModState(SDLMod modstate)
slouken@1662
   427
{
slouken@1674
   428
    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
slouken@1673
   429
slouken@1673
   430
    if (!keyboard) {
slouken@1673
   431
        return;
slouken@1673
   432
    }
slouken@1673
   433
    keyboard->modstate = modstate;
slouken@1662
   434
}
slouken@1662
   435
slouken@1673
   436
const char *
slouken@1668
   437
SDL_GetKeyName(SDLKey key)
slouken@1662
   438
{
slouken@1662
   439
    const char *keyname;
slouken@1662
   440
slouken@1679
   441
    if (key < SDL_arraysize(SDL_keynames)) {
slouken@1674
   442
        keyname = SDL_keynames[key];
slouken@1674
   443
    } else {
slouken@1674
   444
        keyname = NULL;
slouken@1674
   445
    }
slouken@1673
   446
    if (keyname == NULL) {
slouken@1673
   447
        if (key < 256) {
slouken@1673
   448
            static char temp[4];
slouken@1674
   449
            char *cvt;
slouken@1674
   450
            temp[0] = (char) key;
slouken@1674
   451
            temp[1] = '\0';
slouken@1674
   452
            cvt = SDL_iconv_string("UTF-8", "LATIN1", temp, 1);
slouken@1674
   453
            SDL_strlcpy(temp, cvt, SDL_arraysize(temp));
slouken@1674
   454
            SDL_free(cvt);
slouken@1674
   455
            keyname = temp;
slouken@1673
   456
        } else {
slouken@1673
   457
            keyname = "unknown key";
slouken@1673
   458
        }
slouken@1662
   459
    }
slouken@1673
   460
    return keyname;
slouken@0
   461
}
slouken@0
   462
slouken@1724
   463
void
slouken@1724
   464
SDL_SetKeyboardFocus(int index, SDL_WindowID windowID)
slouken@1724
   465
{
slouken@1724
   466
    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
slouken@1724
   467
    int i;
slouken@1724
   468
    SDL_bool focus;
slouken@1724
   469
slouken@1724
   470
    if (!keyboard || (keyboard->focus == windowID)) {
slouken@1724
   471
        return;
slouken@1724
   472
    }
slouken@1724
   473
slouken@1724
   474
    /* See if the current window has lost focus */
slouken@1724
   475
    if (keyboard->focus) {
slouken@1724
   476
        focus = SDL_FALSE;
slouken@1724
   477
        for (i = 0; i < SDL_num_keyboards; ++i) {
slouken@1724
   478
            SDL_Keyboard *check;
slouken@1724
   479
            if (i != index) {
slouken@1724
   480
                check = SDL_GetKeyboard(i);
slouken@1724
   481
                if (check && check->focus == keyboard->focus) {
slouken@1724
   482
                    focus = SDL_TRUE;
slouken@1724
   483
                    break;
slouken@1724
   484
                }
slouken@1724
   485
            }
slouken@1724
   486
        }
slouken@1724
   487
        if (!focus) {
slouken@1724
   488
            SDL_SendWindowEvent(windowID, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
slouken@1724
   489
        }
slouken@1724
   490
    }
slouken@1724
   491
slouken@1724
   492
    keyboard->focus = windowID;
slouken@1724
   493
slouken@1724
   494
    if (keyboard->focus) {
slouken@1724
   495
        focus = SDL_FALSE;
slouken@1724
   496
        for (i = 0; i < SDL_num_keyboards; ++i) {
slouken@1724
   497
            SDL_Keyboard *check;
slouken@1724
   498
            if (i != index) {
slouken@1724
   499
                check = SDL_GetKeyboard(i);
slouken@1724
   500
                if (check && check->focus == keyboard->focus) {
slouken@1724
   501
                    focus = SDL_TRUE;
slouken@1724
   502
                    break;
slouken@1724
   503
                }
slouken@1724
   504
            }
slouken@1724
   505
        }
slouken@1724
   506
        if (!focus) {
slouken@1724
   507
            SDL_SendWindowEvent(windowID, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
slouken@1724
   508
        }
slouken@1724
   509
    }
slouken@1724
   510
}
slouken@1724
   511
slouken@1662
   512
int
slouken@1724
   513
SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode, SDLKey key)
slouken@0
   514
{
slouken@1674
   515
    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
slouken@1662
   516
    int posted, repeatable;
slouken@1662
   517
    Uint16 modstate;
slouken@1674
   518
    Uint8 type;
slouken@0
   519
slouken@1674
   520
    if (!keyboard) {
slouken@1674
   521
        return 0;
slouken@1674
   522
    }
slouken@0
   523
#if 0
slouken@1724
   524
    printf("The '%s' key has been %s\n", SDL_GetKeyName(key),
slouken@1668
   525
           state == SDL_PRESSED ? "pressed" : "released");
slouken@0
   526
#endif
slouken@1662
   527
    repeatable = 0;
slouken@1662
   528
    if (state == SDL_PRESSED) {
slouken@1724
   529
        modstate = keyboard->modstate;
slouken@1724
   530
        switch (key) {
slouken@1662
   531
        case SDLK_UNKNOWN:
slouken@1662
   532
            break;
slouken@1662
   533
        case SDLK_NUMLOCK:
slouken@1724
   534
            keyboard->modstate ^= KMOD_NUM;
slouken@1662
   535
            break;
slouken@1662
   536
        case SDLK_CAPSLOCK:
slouken@1724
   537
            keyboard->modstate ^= KMOD_CAPS;
slouken@1662
   538
            break;
slouken@1662
   539
        case SDLK_LCTRL:
slouken@1724
   540
            keyboard->modstate |= KMOD_LCTRL;
slouken@1662
   541
            break;
slouken@1662
   542
        case SDLK_RCTRL:
slouken@1724
   543
            keyboard->modstate |= KMOD_RCTRL;
slouken@1662
   544
            break;
slouken@1662
   545
        case SDLK_LSHIFT:
slouken@1724
   546
            keyboard->modstate |= KMOD_LSHIFT;
slouken@1662
   547
            break;
slouken@1662
   548
        case SDLK_RSHIFT:
slouken@1724
   549
            keyboard->modstate |= KMOD_RSHIFT;
slouken@1662
   550
            break;
slouken@1662
   551
        case SDLK_LALT:
slouken@1724
   552
            keyboard->modstate |= KMOD_LALT;
slouken@1662
   553
            break;
slouken@1662
   554
        case SDLK_RALT:
slouken@1724
   555
            keyboard->modstate |= KMOD_RALT;
slouken@1662
   556
            break;
slouken@1662
   557
        case SDLK_LMETA:
slouken@1724
   558
            keyboard->modstate |= KMOD_LMETA;
slouken@1662
   559
            break;
slouken@1662
   560
        case SDLK_RMETA:
slouken@1724
   561
            keyboard->modstate |= KMOD_RMETA;
slouken@1662
   562
            break;
slouken@1662
   563
        case SDLK_MODE:
slouken@1724
   564
            keyboard->modstate |= KMOD_MODE;
slouken@1662
   565
            break;
slouken@1662
   566
        default:
slouken@1662
   567
            repeatable = 1;
slouken@1662
   568
            break;
slouken@1662
   569
        }
slouken@1662
   570
    } else {
slouken@1724
   571
        switch (key) {
slouken@1662
   572
        case SDLK_UNKNOWN:
slouken@1662
   573
            break;
slouken@1662
   574
        case SDLK_NUMLOCK:
slouken@1662
   575
        case SDLK_CAPSLOCK:
slouken@1724
   576
            break;
slouken@1662
   577
        case SDLK_LCTRL:
slouken@1724
   578
            keyboard->modstate &= ~KMOD_LCTRL;
slouken@1662
   579
            break;
slouken@1662
   580
        case SDLK_RCTRL:
slouken@1724
   581
            keyboard->modstate &= ~KMOD_RCTRL;
slouken@1662
   582
            break;
slouken@1662
   583
        case SDLK_LSHIFT:
slouken@1724
   584
            keyboard->modstate &= ~KMOD_LSHIFT;
slouken@1662
   585
            break;
slouken@1662
   586
        case SDLK_RSHIFT:
slouken@1724
   587
            keyboard->modstate &= ~KMOD_RSHIFT;
slouken@1662
   588
            break;
slouken@1662
   589
        case SDLK_LALT:
slouken@1724
   590
            keyboard->modstate &= ~KMOD_LALT;
slouken@1662
   591
            break;
slouken@1662
   592
        case SDLK_RALT:
slouken@1724
   593
            keyboard->modstate &= ~KMOD_RALT;
slouken@1662
   594
            break;
slouken@1662
   595
        case SDLK_LMETA:
slouken@1724
   596
            keyboard->modstate &= ~KMOD_LMETA;
slouken@1662
   597
            break;
slouken@1662
   598
        case SDLK_RMETA:
slouken@1724
   599
            keyboard->modstate &= ~KMOD_RMETA;
slouken@1662
   600
            break;
slouken@1662
   601
        case SDLK_MODE:
slouken@1724
   602
            keyboard->modstate &= ~KMOD_MODE;
slouken@1662
   603
            break;
slouken@1662
   604
        default:
slouken@1662
   605
            break;
slouken@1662
   606
        }
slouken@1724
   607
        modstate = keyboard->modstate;
slouken@1662
   608
    }
slouken@0
   609
slouken@1662
   610
    /* Figure out what type of event this is */
slouken@1662
   611
    switch (state) {
slouken@1662
   612
    case SDL_PRESSED:
slouken@1674
   613
        type = SDL_KEYDOWN;
slouken@1662
   614
        break;
slouken@1662
   615
    case SDL_RELEASED:
slouken@1674
   616
        type = SDL_KEYUP;
slouken@1662
   617
        /*
slouken@1662
   618
         * jk 991215 - Added
slouken@1662
   619
         */
slouken@1674
   620
        if (keyboard->repeat.timestamp &&
slouken@1724
   621
            keyboard->repeat.evt.key.keysym.sym == key) {
slouken@1674
   622
            keyboard->repeat.timestamp = 0;
slouken@1662
   623
        }
slouken@1662
   624
        break;
slouken@1662
   625
    default:
slouken@1662
   626
        /* Invalid state -- bail */
slouken@1674
   627
        return 0;
slouken@1662
   628
    }
slouken@0
   629
slouken@1724
   630
    if (key != SDLK_UNKNOWN) {
slouken@1662
   631
        /* Drop events that don't change state */
slouken@1724
   632
        if (keyboard->keystate[key] == state) {
slouken@1282
   633
#if 0
slouken@1668
   634
            printf("Keyboard event didn't change state - dropped!\n");
slouken@1282
   635
#endif
slouken@1674
   636
            return 0;
slouken@1662
   637
        }
slouken@1327
   638
slouken@1662
   639
        /* Update internal keyboard state */
slouken@1724
   640
        keyboard->keystate[key] = state;
slouken@1662
   641
    }
slouken@0
   642
slouken@1662
   643
    /* Post the event, if desired */
slouken@1662
   644
    posted = 0;
slouken@1674
   645
    if (SDL_ProcessEvents[type] == SDL_ENABLE) {
slouken@1674
   646
        SDL_Event event;
slouken@1674
   647
        event.key.type = type;
slouken@1674
   648
        event.key.which = (Uint8) index;
slouken@1662
   649
        event.key.state = state;
slouken@1724
   650
        event.key.keysym.scancode = scancode;
slouken@1724
   651
        event.key.keysym.sym = (Uint16) key;
slouken@1724
   652
        event.key.keysym.mod = modstate;
slouken@1724
   653
        event.key.keysym.unicode = 0;
slouken@1674
   654
        event.key.windowID = keyboard->focus;
slouken@1662
   655
        /*
slouken@1662
   656
         * jk 991215 - Added
slouken@1662
   657
         */
slouken@1674
   658
        if (repeatable && (keyboard->repeat.delay != 0)) {
slouken@1674
   659
            Uint32 timestamp = SDL_GetTicks();
slouken@1674
   660
            if (!timestamp) {
slouken@1674
   661
                timestamp = 1;
slouken@1674
   662
            }
slouken@1674
   663
            keyboard->repeat.evt = event;
slouken@1674
   664
            keyboard->repeat.firsttime = 1;
slouken@1674
   665
            keyboard->repeat.timestamp = 1;
slouken@1662
   666
        }
slouken@1731
   667
        posted = (SDL_PushEvent(&event) > 0);
slouken@1662
   668
    }
slouken@1662
   669
    return (posted);
slouken@0
   670
}
slouken@0
   671
slouken@1724
   672
int
slouken@1724
   673
SDL_SendKeyboardText(int index, const char *text)
slouken@1724
   674
{
slouken@1724
   675
    SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
slouken@1724
   676
    int posted;
slouken@1724
   677
slouken@1724
   678
    if (!keyboard) {
slouken@1724
   679
        return 0;
slouken@1724
   680
    }
slouken@1724
   681
slouken@1724
   682
    /* Post the event, if desired */
slouken@1724
   683
    posted = 0;
slouken@1724
   684
    if (SDL_ProcessEvents[SDL_TEXTINPUT] == SDL_ENABLE) {
slouken@1724
   685
        SDL_Event event;
slouken@1724
   686
        event.text.type = SDL_TEXTINPUT;
slouken@1724
   687
        event.text.which = (Uint8) index;
slouken@1724
   688
        SDL_strlcpy(event.text.text, text, SDL_arraysize(event.text.text));
slouken@1724
   689
        event.key.windowID = keyboard->focus;
slouken@1731
   690
        posted = (SDL_PushEvent(&event) > 0);
slouken@1724
   691
    }
slouken@1724
   692
    return (posted);
slouken@1724
   693
}
slouken@1724
   694
slouken@0
   695
/*
slouken@0
   696
 * jk 991215 - Added
slouken@0
   697
 */
slouken@1662
   698
void
slouken@1668
   699
SDL_CheckKeyRepeat(void)
slouken@0
   700
{
slouken@1674
   701
    int i;
slouken@0
   702
slouken@1674
   703
    for (i = 0; i < SDL_num_keyboards; ++i) {
slouken@1674
   704
        SDL_Keyboard *keyboard = SDL_keyboards[i];
slouken@1674
   705
slouken@1674
   706
        if (!keyboard) {
slouken@1674
   707
            continue;
slouken@1674
   708
        }
slouken@1674
   709
slouken@1674
   710
        if (keyboard->repeat.timestamp) {
slouken@1674
   711
            Uint32 now, interval;
slouken@1674
   712
slouken@1674
   713
            now = SDL_GetTicks();
slouken@1674
   714
            interval = (now - keyboard->repeat.timestamp);
slouken@1674
   715
            if (keyboard->repeat.firsttime) {
slouken@1674
   716
                if (interval > (Uint32) keyboard->repeat.delay) {
slouken@1674
   717
                    keyboard->repeat.timestamp = now;
slouken@1674
   718
                    keyboard->repeat.firsttime = 0;
slouken@1674
   719
                }
slouken@1674
   720
            } else {
slouken@1674
   721
                if (interval > (Uint32) keyboard->repeat.interval) {
slouken@1674
   722
                    keyboard->repeat.timestamp = now;
slouken@1731
   723
                    SDL_PushEvent(&keyboard->repeat.evt);
slouken@1662
   724
                }
slouken@1662
   725
            }
slouken@1662
   726
        }
slouken@1662
   727
    }
slouken@0
   728
}
slouken@0
   729
slouken@1662
   730
int
slouken@1668
   731
SDL_EnableKeyRepeat(int delay, int interval)
slouken@0
   732
{
slouken@1674
   733
    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
slouken@1674
   734
slouken@1674
   735
    if (!keyboard) {
slouken@1674
   736
        SDL_SetError("No keyboard is currently selected");
slouken@1674
   737
        return -1;
slouken@1674
   738
    }
slouken@1674
   739
slouken@1662
   740
    if ((delay < 0) || (interval < 0)) {
slouken@1668
   741
        SDL_SetError("keyboard repeat value less than zero");
slouken@1674
   742
        return -1;
slouken@1662
   743
    }
slouken@1674
   744
slouken@1674
   745
    keyboard->repeat.firsttime = 0;
slouken@1674
   746
    keyboard->repeat.delay = delay;
slouken@1674
   747
    keyboard->repeat.interval = interval;
slouken@1674
   748
    keyboard->repeat.timestamp = 0;
slouken@1674
   749
slouken@1674
   750
    return 0;
slouken@0
   751
}
slouken@0
   752
slouken@1662
   753
void
slouken@1668
   754
SDL_GetKeyRepeat(int *delay, int *interval)
slouken@1507
   755
{
slouken@1674
   756
    SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
slouken@1674
   757
slouken@1674
   758
    if (!keyboard) {
slouken@1674
   759
        if (delay) {
slouken@1674
   760
            *delay = 0;
slouken@1674
   761
        }
slouken@1674
   762
        if (interval) {
slouken@1674
   763
            *interval = 0;
slouken@1674
   764
        }
slouken@1674
   765
        return;
slouken@1674
   766
    }
slouken@1674
   767
    if (delay) {
slouken@1674
   768
        *delay = keyboard->repeat.delay;
slouken@1674
   769
    }
slouken@1674
   770
    if (interval) {
slouken@1674
   771
        *interval = keyboard->repeat.interval;
slouken@1674
   772
    }
slouken@1507
   773
}
slouken@1507
   774
slouken@1662
   775
/* vi: set ts=4 sw=4 expandtab: */