src/events/SDL_keyboard.c
author Gabriel Jacobo <gabomdq@gmail.com>
Wed, 21 Aug 2013 09:47:10 -0300
changeset 7678 286c42d7c5ed
parent 7677 871d43c6968a
child 7778 a571a9947869
permissions -rw-r--r--
OCD fixes: Adds a space after /* (glory to regular expressions!)
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@0
    22
slouken@0
    23
/* General keyboard handling code for SDL */
slouken@0
    24
slouken@1358
    25
#include "SDL_timer.h"
slouken@0
    26
#include "SDL_events.h"
slouken@0
    27
#include "SDL_events_c.h"
slouken@5123
    28
#include "../video/SDL_sysvideo.h"
slouken@0
    29
slouken@0
    30
gabomdq@7678
    31
/* #define DEBUG_KEYBOARD */
slouken@6661
    32
slouken@1895
    33
/* Global keyboard information */
slouken@4465
    34
slouken@4465
    35
typedef struct SDL_Keyboard SDL_Keyboard;
slouken@4465
    36
slouken@4465
    37
struct SDL_Keyboard
slouken@4465
    38
{
slouken@4465
    39
    /* Data common to all keyboards */
slouken@4465
    40
    SDL_Window *focus;
slouken@4465
    41
    Uint16 modstate;
slouken@4465
    42
    Uint8 keystate[SDL_NUM_SCANCODES];
slouken@5220
    43
    SDL_Keycode keymap[SDL_NUM_SCANCODES];
slouken@4465
    44
};
slouken@4465
    45
slouken@4465
    46
static SDL_Keyboard SDL_keyboard;
slouken@0
    47
slouken@5220
    48
static const SDL_Keycode SDL_default_keymap[SDL_NUM_SCANCODES] = {
slouken@2303
    49
    0, 0, 0, 0,
slouken@2303
    50
    'a',
slouken@2303
    51
    'b',
slouken@2303
    52
    'c',
slouken@2303
    53
    'd',
slouken@2303
    54
    'e',
slouken@2303
    55
    'f',
slouken@2303
    56
    'g',
slouken@2303
    57
    'h',
slouken@2303
    58
    'i',
slouken@2303
    59
    'j',
slouken@2303
    60
    'k',
slouken@2303
    61
    'l',
slouken@2303
    62
    'm',
slouken@2303
    63
    'n',
slouken@2303
    64
    'o',
slouken@2303
    65
    'p',
slouken@2303
    66
    'q',
slouken@2303
    67
    'r',
slouken@2303
    68
    's',
slouken@2303
    69
    't',
slouken@2303
    70
    'u',
slouken@2303
    71
    'v',
slouken@2303
    72
    'w',
slouken@2303
    73
    'x',
slouken@2303
    74
    'y',
slouken@2303
    75
    'z',
slouken@2303
    76
    '1',
slouken@2303
    77
    '2',
slouken@2303
    78
    '3',
slouken@2303
    79
    '4',
slouken@2303
    80
    '5',
slouken@2303
    81
    '6',
slouken@2303
    82
    '7',
slouken@2303
    83
    '8',
slouken@2303
    84
    '9',
slouken@2303
    85
    '0',
slouken@2303
    86
    SDLK_RETURN,
slouken@2303
    87
    SDLK_ESCAPE,
slouken@2303
    88
    SDLK_BACKSPACE,
slouken@2303
    89
    SDLK_TAB,
slouken@2303
    90
    SDLK_SPACE,
slouken@2303
    91
    '-',
slouken@2303
    92
    '=',
slouken@2303
    93
    '[',
slouken@2303
    94
    ']',
slouken@2303
    95
    '\\',
slouken@2303
    96
    '#',
slouken@2303
    97
    ';',
slouken@2303
    98
    '\'',
slouken@2303
    99
    '`',
slouken@2303
   100
    ',',
slouken@2303
   101
    '.',
slouken@2303
   102
    '/',
slouken@2303
   103
    SDLK_CAPSLOCK,
slouken@2303
   104
    SDLK_F1,
slouken@2303
   105
    SDLK_F2,
slouken@2303
   106
    SDLK_F3,
slouken@2303
   107
    SDLK_F4,
slouken@2303
   108
    SDLK_F5,
slouken@2303
   109
    SDLK_F6,
slouken@2303
   110
    SDLK_F7,
slouken@2303
   111
    SDLK_F8,
slouken@2303
   112
    SDLK_F9,
slouken@2303
   113
    SDLK_F10,
slouken@2303
   114
    SDLK_F11,
slouken@2303
   115
    SDLK_F12,
slouken@2303
   116
    SDLK_PRINTSCREEN,
slouken@2303
   117
    SDLK_SCROLLLOCK,
slouken@2303
   118
    SDLK_PAUSE,
slouken@2303
   119
    SDLK_INSERT,
slouken@2303
   120
    SDLK_HOME,
slouken@2303
   121
    SDLK_PAGEUP,
slouken@2303
   122
    SDLK_DELETE,
slouken@2303
   123
    SDLK_END,
slouken@2303
   124
    SDLK_PAGEDOWN,
slouken@2303
   125
    SDLK_RIGHT,
slouken@2303
   126
    SDLK_LEFT,
slouken@2303
   127
    SDLK_DOWN,
slouken@2303
   128
    SDLK_UP,
slouken@2303
   129
    SDLK_NUMLOCKCLEAR,
slouken@2303
   130
    SDLK_KP_DIVIDE,
slouken@2303
   131
    SDLK_KP_MULTIPLY,
slouken@2303
   132
    SDLK_KP_MINUS,
slouken@2303
   133
    SDLK_KP_PLUS,
slouken@2303
   134
    SDLK_KP_ENTER,
slouken@2303
   135
    SDLK_KP_1,
slouken@2303
   136
    SDLK_KP_2,
slouken@2303
   137
    SDLK_KP_3,
slouken@2303
   138
    SDLK_KP_4,
slouken@2303
   139
    SDLK_KP_5,
slouken@2303
   140
    SDLK_KP_6,
slouken@2303
   141
    SDLK_KP_7,
slouken@2303
   142
    SDLK_KP_8,
slouken@2303
   143
    SDLK_KP_9,
slouken@2303
   144
    SDLK_KP_0,
slouken@2303
   145
    SDLK_KP_PERIOD,
slouken@2303
   146
    0,
slouken@2303
   147
    SDLK_APPLICATION,
slouken@2303
   148
    SDLK_POWER,
slouken@2303
   149
    SDLK_KP_EQUALS,
slouken@2303
   150
    SDLK_F13,
slouken@2303
   151
    SDLK_F14,
slouken@2303
   152
    SDLK_F15,
slouken@2303
   153
    SDLK_F16,
slouken@2303
   154
    SDLK_F17,
slouken@2303
   155
    SDLK_F18,
slouken@2303
   156
    SDLK_F19,
slouken@2303
   157
    SDLK_F20,
slouken@2303
   158
    SDLK_F21,
slouken@2303
   159
    SDLK_F22,
slouken@2303
   160
    SDLK_F23,
slouken@2303
   161
    SDLK_F24,
slouken@2303
   162
    SDLK_EXECUTE,
slouken@2303
   163
    SDLK_HELP,
slouken@2303
   164
    SDLK_MENU,
slouken@2303
   165
    SDLK_SELECT,
slouken@2303
   166
    SDLK_STOP,
slouken@2303
   167
    SDLK_AGAIN,
slouken@2303
   168
    SDLK_UNDO,
slouken@2303
   169
    SDLK_CUT,
slouken@2303
   170
    SDLK_COPY,
slouken@2303
   171
    SDLK_PASTE,
slouken@2303
   172
    SDLK_FIND,
slouken@2303
   173
    SDLK_MUTE,
slouken@2303
   174
    SDLK_VOLUMEUP,
slouken@2303
   175
    SDLK_VOLUMEDOWN,
slouken@2303
   176
    0, 0, 0,
slouken@2303
   177
    SDLK_KP_COMMA,
slouken@2303
   178
    SDLK_KP_EQUALSAS400,
slouken@2303
   179
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
slouken@2303
   180
    SDLK_ALTERASE,
slouken@2303
   181
    SDLK_SYSREQ,
slouken@2303
   182
    SDLK_CANCEL,
slouken@2303
   183
    SDLK_CLEAR,
slouken@2303
   184
    SDLK_PRIOR,
slouken@2303
   185
    SDLK_RETURN2,
slouken@2303
   186
    SDLK_SEPARATOR,
slouken@2303
   187
    SDLK_OUT,
slouken@2303
   188
    SDLK_OPER,
slouken@2303
   189
    SDLK_CLEARAGAIN,
slouken@2303
   190
    SDLK_CRSEL,
slouken@2303
   191
    SDLK_EXSEL,
slouken@2303
   192
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
slouken@2303
   193
    SDLK_KP_00,
slouken@2303
   194
    SDLK_KP_000,
slouken@2303
   195
    SDLK_THOUSANDSSEPARATOR,
slouken@2303
   196
    SDLK_DECIMALSEPARATOR,
slouken@2303
   197
    SDLK_CURRENCYUNIT,
slouken@2303
   198
    SDLK_CURRENCYSUBUNIT,
slouken@2303
   199
    SDLK_KP_LEFTPAREN,
slouken@2303
   200
    SDLK_KP_RIGHTPAREN,
slouken@2303
   201
    SDLK_KP_LEFTBRACE,
slouken@2303
   202
    SDLK_KP_RIGHTBRACE,
slouken@2303
   203
    SDLK_KP_TAB,
slouken@2303
   204
    SDLK_KP_BACKSPACE,
slouken@2303
   205
    SDLK_KP_A,
slouken@2303
   206
    SDLK_KP_B,
slouken@2303
   207
    SDLK_KP_C,
slouken@2303
   208
    SDLK_KP_D,
slouken@2303
   209
    SDLK_KP_E,
slouken@2303
   210
    SDLK_KP_F,
slouken@2303
   211
    SDLK_KP_XOR,
slouken@2303
   212
    SDLK_KP_POWER,
slouken@2303
   213
    SDLK_KP_PERCENT,
slouken@2303
   214
    SDLK_KP_LESS,
slouken@2303
   215
    SDLK_KP_GREATER,
slouken@2303
   216
    SDLK_KP_AMPERSAND,
slouken@2303
   217
    SDLK_KP_DBLAMPERSAND,
slouken@2303
   218
    SDLK_KP_VERTICALBAR,
slouken@2303
   219
    SDLK_KP_DBLVERTICALBAR,
slouken@2303
   220
    SDLK_KP_COLON,
slouken@2303
   221
    SDLK_KP_HASH,
slouken@2303
   222
    SDLK_KP_SPACE,
slouken@2303
   223
    SDLK_KP_AT,
slouken@2303
   224
    SDLK_KP_EXCLAM,
slouken@2303
   225
    SDLK_KP_MEMSTORE,
slouken@2303
   226
    SDLK_KP_MEMRECALL,
slouken@2303
   227
    SDLK_KP_MEMCLEAR,
slouken@2303
   228
    SDLK_KP_MEMADD,
slouken@2303
   229
    SDLK_KP_MEMSUBTRACT,
slouken@2303
   230
    SDLK_KP_MEMMULTIPLY,
slouken@2303
   231
    SDLK_KP_MEMDIVIDE,
slouken@2303
   232
    SDLK_KP_PLUSMINUS,
slouken@2303
   233
    SDLK_KP_CLEAR,
slouken@2303
   234
    SDLK_KP_CLEARENTRY,
slouken@2303
   235
    SDLK_KP_BINARY,
slouken@2303
   236
    SDLK_KP_OCTAL,
slouken@2303
   237
    SDLK_KP_DECIMAL,
slouken@2303
   238
    SDLK_KP_HEXADECIMAL,
slouken@2303
   239
    0, 0,
slouken@2303
   240
    SDLK_LCTRL,
slouken@2303
   241
    SDLK_LSHIFT,
slouken@2303
   242
    SDLK_LALT,
slouken@2303
   243
    SDLK_LGUI,
slouken@2303
   244
    SDLK_RCTRL,
slouken@2303
   245
    SDLK_RSHIFT,
slouken@2303
   246
    SDLK_RALT,
slouken@2303
   247
    SDLK_RGUI,
slouken@2303
   248
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
slouken@2303
   249
    SDLK_MODE,
slouken@2303
   250
    SDLK_AUDIONEXT,
slouken@2303
   251
    SDLK_AUDIOPREV,
slouken@2303
   252
    SDLK_AUDIOSTOP,
slouken@2303
   253
    SDLK_AUDIOPLAY,
slouken@2303
   254
    SDLK_AUDIOMUTE,
slouken@2303
   255
    SDLK_MEDIASELECT,
slouken@2305
   256
    SDLK_WWW,
slouken@2303
   257
    SDLK_MAIL,
slouken@2303
   258
    SDLK_CALCULATOR,
slouken@2303
   259
    SDLK_COMPUTER,
slouken@2303
   260
    SDLK_AC_SEARCH,
slouken@2303
   261
    SDLK_AC_HOME,
slouken@2303
   262
    SDLK_AC_BACK,
slouken@2303
   263
    SDLK_AC_FORWARD,
slouken@2303
   264
    SDLK_AC_STOP,
slouken@2303
   265
    SDLK_AC_REFRESH,
slouken@2303
   266
    SDLK_AC_BOOKMARKS,
slouken@2303
   267
    SDLK_BRIGHTNESSDOWN,
slouken@2303
   268
    SDLK_BRIGHTNESSUP,
slouken@2303
   269
    SDLK_DISPLAYSWITCH,
slouken@2303
   270
    SDLK_KBDILLUMTOGGLE,
slouken@2303
   271
    SDLK_KBDILLUMDOWN,
slouken@2303
   272
    SDLK_KBDILLUMUP,
slouken@2303
   273
    SDLK_EJECT,
slouken@2303
   274
    SDLK_SLEEP,
slouken@2303
   275
};
slouken@2303
   276
slouken@2303
   277
static const char *SDL_scancode_names[SDL_NUM_SCANCODES] = {
slouken@2303
   278
    NULL, NULL, NULL, NULL,
slouken@2303
   279
    "A",
slouken@2303
   280
    "B",
slouken@2303
   281
    "C",
slouken@2303
   282
    "D",
slouken@2303
   283
    "E",
slouken@2303
   284
    "F",
slouken@2303
   285
    "G",
slouken@2303
   286
    "H",
slouken@2303
   287
    "I",
slouken@2303
   288
    "J",
slouken@2303
   289
    "K",
slouken@2303
   290
    "L",
slouken@2303
   291
    "M",
slouken@2303
   292
    "N",
slouken@2303
   293
    "O",
slouken@2303
   294
    "P",
slouken@2303
   295
    "Q",
slouken@2303
   296
    "R",
slouken@2303
   297
    "S",
slouken@2303
   298
    "T",
slouken@2303
   299
    "U",
slouken@2303
   300
    "V",
slouken@2303
   301
    "W",
slouken@2303
   302
    "X",
slouken@2303
   303
    "Y",
slouken@2303
   304
    "Z",
slouken@2303
   305
    "1",
slouken@2303
   306
    "2",
slouken@2303
   307
    "3",
slouken@2303
   308
    "4",
slouken@2303
   309
    "5",
slouken@2303
   310
    "6",
slouken@2303
   311
    "7",
slouken@2303
   312
    "8",
slouken@2303
   313
    "9",
slouken@2303
   314
    "0",
slouken@2303
   315
    "Return",
slouken@2303
   316
    "Escape",
slouken@2303
   317
    "Backspace",
slouken@2303
   318
    "Tab",
slouken@2303
   319
    "Space",
slouken@2303
   320
    "-",
slouken@2303
   321
    "=",
slouken@2303
   322
    "[",
slouken@2303
   323
    "]",
slouken@2303
   324
    "\\",
slouken@2303
   325
    "#",
slouken@2303
   326
    ";",
slouken@2303
   327
    "'",
slouken@2303
   328
    "`",
slouken@2303
   329
    ",",
slouken@2303
   330
    ".",
slouken@2303
   331
    "/",
slouken@2303
   332
    "CapsLock",
slouken@2303
   333
    "F1",
slouken@2303
   334
    "F2",
slouken@2303
   335
    "F3",
slouken@2303
   336
    "F4",
slouken@2303
   337
    "F5",
slouken@2303
   338
    "F6",
slouken@2303
   339
    "F7",
slouken@2303
   340
    "F8",
slouken@2303
   341
    "F9",
slouken@2303
   342
    "F10",
slouken@2303
   343
    "F11",
slouken@2303
   344
    "F12",
slouken@2303
   345
    "PrintScreen",
slouken@2303
   346
    "ScrollLock",
slouken@2303
   347
    "Pause",
slouken@2303
   348
    "Insert",
slouken@2303
   349
    "Home",
slouken@2303
   350
    "PageUp",
slouken@2303
   351
    "Delete",
slouken@2303
   352
    "End",
slouken@2303
   353
    "PageDown",
slouken@2303
   354
    "Right",
slouken@2303
   355
    "Left",
slouken@2303
   356
    "Down",
slouken@2303
   357
    "Up",
slouken@2303
   358
    "Numlock",
slouken@2303
   359
    "Keypad /",
slouken@2303
   360
    "Keypad *",
slouken@2303
   361
    "Keypad -",
slouken@2303
   362
    "Keypad +",
slouken@2303
   363
    "Keypad Enter",
slouken@2303
   364
    "Keypad 1",
slouken@2303
   365
    "Keypad 2",
slouken@2303
   366
    "Keypad 3",
slouken@2303
   367
    "Keypad 4",
slouken@2303
   368
    "Keypad 5",
slouken@2303
   369
    "Keypad 6",
slouken@2303
   370
    "Keypad 7",
slouken@2303
   371
    "Keypad 8",
slouken@2303
   372
    "Keypad 9",
slouken@2303
   373
    "Keypad 0",
slouken@2303
   374
    "Keypad .",
slouken@2303
   375
    NULL,
slouken@2303
   376
    "Application",
slouken@2303
   377
    "Power",
slouken@2303
   378
    "Keypad =",
slouken@2303
   379
    "F13",
slouken@2303
   380
    "F14",
slouken@2303
   381
    "F15",
slouken@2303
   382
    "F16",
slouken@2303
   383
    "F17",
slouken@2303
   384
    "F18",
slouken@2303
   385
    "F19",
slouken@2303
   386
    "F20",
slouken@2303
   387
    "F21",
slouken@2303
   388
    "F22",
slouken@2303
   389
    "F23",
slouken@2303
   390
    "F24",
slouken@2303
   391
    "Execute",
slouken@2303
   392
    "Help",
slouken@2303
   393
    "Menu",
slouken@2303
   394
    "Select",
slouken@2303
   395
    "Stop",
slouken@2303
   396
    "Again",
slouken@2303
   397
    "Undo",
slouken@2303
   398
    "Cut",
slouken@2303
   399
    "Copy",
slouken@2303
   400
    "Paste",
slouken@2303
   401
    "Find",
slouken@2303
   402
    "Mute",
slouken@2303
   403
    "VolumeUp",
slouken@2303
   404
    "VolumeDown",
slouken@2303
   405
    NULL, NULL, NULL,
slouken@2303
   406
    "Keypad ,",
slouken@2303
   407
    "Keypad = (AS400)",
slouken@2303
   408
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
slouken@2303
   409
    NULL, NULL, NULL, NULL, NULL, NULL,
slouken@2303
   410
    "AltErase",
slouken@2303
   411
    "SysReq",
slouken@2303
   412
    "Cancel",
slouken@2303
   413
    "Clear",
slouken@2303
   414
    "Prior",
slouken@2303
   415
    "Return",
slouken@2303
   416
    "Separator",
slouken@2303
   417
    "Out",
slouken@2303
   418
    "Oper",
slouken@2303
   419
    "Clear / Again",
slouken@2303
   420
    "CrSel",
slouken@2303
   421
    "ExSel",
slouken@2303
   422
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
slouken@2303
   423
    "Keypad 00",
slouken@2303
   424
    "Keypad 000",
slouken@2303
   425
    "ThousandsSeparator",
slouken@2303
   426
    "DecimalSeparator",
slouken@2303
   427
    "CurrencyUnit",
slouken@2303
   428
    "CurrencySubUnit",
slouken@2303
   429
    "Keypad (",
slouken@2303
   430
    "Keypad )",
slouken@2303
   431
    "Keypad {",
slouken@2303
   432
    "Keypad }",
slouken@2303
   433
    "Keypad Tab",
slouken@2303
   434
    "Keypad Backspace",
slouken@2303
   435
    "Keypad A",
slouken@2303
   436
    "Keypad B",
slouken@2303
   437
    "Keypad C",
slouken@2303
   438
    "Keypad D",
slouken@2303
   439
    "Keypad E",
slouken@2303
   440
    "Keypad F",
slouken@2303
   441
    "Keypad XOR",
slouken@2303
   442
    "Keypad ^",
slouken@2303
   443
    "Keypad %",
slouken@2303
   444
    "Keypad <",
slouken@2303
   445
    "Keypad >",
slouken@2303
   446
    "Keypad &",
slouken@2303
   447
    "Keypad &&",
slouken@2303
   448
    "Keypad |",
slouken@2303
   449
    "Keypad ||",
slouken@2303
   450
    "Keypad :",
slouken@2303
   451
    "Keypad #",
slouken@2303
   452
    "Keypad Space",
slouken@2303
   453
    "Keypad @",
slouken@2303
   454
    "Keypad !",
slouken@2303
   455
    "Keypad MemStore",
slouken@2303
   456
    "Keypad MemRecall",
slouken@2303
   457
    "Keypad MemClear",
slouken@2303
   458
    "Keypad MemAdd",
slouken@2303
   459
    "Keypad MemSubtract",
slouken@2303
   460
    "Keypad MemMultiply",
slouken@2303
   461
    "Keypad MemDivide",
slouken@2303
   462
    "Keypad +/-",
slouken@2303
   463
    "Keypad Clear",
slouken@2303
   464
    "Keypad ClearEntry",
slouken@2303
   465
    "Keypad Binary",
slouken@2303
   466
    "Keypad Octal",
slouken@2303
   467
    "Keypad Decimal",
slouken@2303
   468
    "Keypad Hexadecimal",
slouken@2303
   469
    NULL, NULL,
slouken@2303
   470
    "Left Ctrl",
slouken@2303
   471
    "Left Shift",
slouken@2303
   472
    "Left Alt",
slouken@2303
   473
    "Left GUI",
slouken@2303
   474
    "Right Ctrl",
slouken@2303
   475
    "Right Shift",
slouken@2303
   476
    "Right Alt",
slouken@2303
   477
    "Right GUI",
slouken@2303
   478
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
slouken@2303
   479
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
slouken@2303
   480
    NULL,
slouken@2303
   481
    "ModeSwitch",
slouken@2303
   482
    "AudioNext",
slouken@2303
   483
    "AudioPrev",
slouken@2303
   484
    "AudioStop",
slouken@2303
   485
    "AudioPlay",
slouken@2303
   486
    "AudioMute",
slouken@2303
   487
    "MediaSelect",
slouken@2305
   488
    "WWW",
slouken@2303
   489
    "Mail",
slouken@2303
   490
    "Calculator",
slouken@2303
   491
    "Computer",
slouken@2303
   492
    "AC Search",
slouken@2303
   493
    "AC Home",
slouken@2303
   494
    "AC Back",
slouken@2303
   495
    "AC Forward",
slouken@2303
   496
    "AC Stop",
slouken@2303
   497
    "AC Refresh",
slouken@2303
   498
    "AC Bookmarks",
slouken@2303
   499
    "BrightnessDown",
slouken@2303
   500
    "BrightnessUp",
slouken@2303
   501
    "DisplaySwitch",
slouken@2303
   502
    "KBDIllumToggle",
slouken@2303
   503
    "KBDIllumDown",
slouken@2303
   504
    "KBDIllumUp",
slouken@2303
   505
    "Eject",
slouken@2303
   506
    "Sleep",
slouken@2303
   507
};
slouken@2303
   508
bob@2295
   509
/* Taken from SDL_iconv() */
slouken@2303
   510
static char *
slouken@2303
   511
SDL_UCS4ToUTF8(Uint32 ch, char *dst)
bob@2295
   512
{
bob@2295
   513
    Uint8 *p = (Uint8 *) dst;
bob@2295
   514
    if (ch <= 0x7F) {
bob@2295
   515
        *p = (Uint8) ch;
bob@2295
   516
        ++dst;
bob@2295
   517
    } else if (ch <= 0x7FF) {
bob@2295
   518
        p[0] = 0xC0 | (Uint8) ((ch >> 6) & 0x1F);
bob@2295
   519
        p[1] = 0x80 | (Uint8) (ch & 0x3F);
bob@2295
   520
        dst += 2;
bob@2295
   521
    } else if (ch <= 0xFFFF) {
bob@2295
   522
        p[0] = 0xE0 | (Uint8) ((ch >> 12) & 0x0F);
bob@2295
   523
        p[1] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
bob@2295
   524
        p[2] = 0x80 | (Uint8) (ch & 0x3F);
bob@2295
   525
        dst += 3;
bob@2295
   526
    } else if (ch <= 0x1FFFFF) {
bob@2295
   527
        p[0] = 0xF0 | (Uint8) ((ch >> 18) & 0x07);
bob@2295
   528
        p[1] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
bob@2295
   529
        p[2] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
bob@2295
   530
        p[3] = 0x80 | (Uint8) (ch & 0x3F);
bob@2295
   531
        dst += 4;
bob@2295
   532
    } else if (ch <= 0x3FFFFFF) {
bob@2295
   533
        p[0] = 0xF8 | (Uint8) ((ch >> 24) & 0x03);
bob@2295
   534
        p[1] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
bob@2295
   535
        p[2] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
bob@2295
   536
        p[3] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
bob@2295
   537
        p[4] = 0x80 | (Uint8) (ch & 0x3F);
bob@2295
   538
        dst += 5;
bob@2295
   539
    } else {
bob@2295
   540
        p[0] = 0xFC | (Uint8) ((ch >> 30) & 0x01);
bob@2295
   541
        p[1] = 0x80 | (Uint8) ((ch >> 24) & 0x3F);
bob@2295
   542
        p[2] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
bob@2295
   543
        p[3] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
bob@2295
   544
        p[4] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
bob@2295
   545
        p[5] = 0x80 | (Uint8) (ch & 0x3F);
bob@2295
   546
        dst += 6;
bob@2295
   547
    }
bob@2295
   548
    return dst;
bob@2295
   549
}
bob@2295
   550
slouken@0
   551
/* Public functions */
slouken@1895
   552
int
slouken@1895
   553
SDL_KeyboardInit(void)
slouken@0
   554
{
slouken@4490
   555
    SDL_Keyboard *keyboard = &SDL_keyboard;
slouken@4490
   556
slouken@4490
   557
    /* Set the default keymap */
slouken@4490
   558
    SDL_memcpy(keyboard->keymap, SDL_default_keymap, sizeof(SDL_default_keymap));
slouken@1895
   559
    return (0);
slouken@1123
   560
}
slouken@0
   561
slouken@4465
   562
void
slouken@4465
   563
SDL_ResetKeyboard(void)
slouken@0
   564
{
slouken@4465
   565
    SDL_Keyboard *keyboard = &SDL_keyboard;
slouken@5218
   566
    SDL_Scancode scancode;
slouken@0
   567
slouken@6661
   568
#ifdef DEBUG_KEYBOARD
slouken@6661
   569
    printf("Resetting keyboard\n");
slouken@6661
   570
#endif
slouken@2303
   571
    for (scancode = 0; scancode < SDL_NUM_SCANCODES; ++scancode) {
slouken@2303
   572
        if (keyboard->keystate[scancode] == SDL_PRESSED) {
slouken@4465
   573
            SDL_SendKeyboardKey(SDL_RELEASED, scancode);
slouken@1895
   574
        }
slouken@1895
   575
    }
slouken@0
   576
}
slouken@0
   577
slouken@1895
   578
void
slouken@5220
   579
SDL_GetDefaultKeymap(SDL_Keycode * keymap)
slouken@0
   580
{
slouken@2303
   581
    SDL_memcpy(keymap, SDL_default_keymap, sizeof(SDL_default_keymap));
slouken@1895
   582
}
slouken@1895
   583
slouken@1895
   584
void
slouken@5220
   585
SDL_SetKeymap(int start, SDL_Keycode * keys, int length)
slouken@1895
   586
{
slouken@4465
   587
    SDL_Keyboard *keyboard = &SDL_keyboard;
slouken@1895
   588
slouken@2303
   589
    if (start < 0 || start + length > SDL_NUM_SCANCODES) {
slouken@2303
   590
        return;
slouken@1895
   591
    }
slouken@2268
   592
slouken@2303
   593
    SDL_memcpy(&keyboard->keymap[start], keys, sizeof(*keys) * length);
slouken@1895
   594
}
slouken@1895
   595
slouken@1895
   596
void
slouken@5218
   597
SDL_SetScancodeName(SDL_Scancode scancode, const char *name)
slouken@2268
   598
{
slouken@2303
   599
    SDL_scancode_names[scancode] = name;
slouken@2268
   600
}
slouken@2268
   601
slouken@4465
   602
SDL_Window *
slouken@4465
   603
SDL_GetKeyboardFocus(void)
slouken@4465
   604
{
slouken@4465
   605
    SDL_Keyboard *keyboard = &SDL_keyboard;
slouken@4465
   606
slouken@4465
   607
    return keyboard->focus;
slouken@4465
   608
}
slouken@4465
   609
slouken@2268
   610
void
slouken@4465
   611
SDL_SetKeyboardFocus(SDL_Window * window)
slouken@1895
   612
{
slouken@4465
   613
    SDL_Keyboard *keyboard = &SDL_keyboard;
slouken@1895
   614
slouken@6665
   615
    if (keyboard->focus && !window) {
slouken@6665
   616
        /* We won't get anymore keyboard messages, so reset keyboard state */
slouken@6665
   617
        SDL_ResetKeyboard();
slouken@6665
   618
    }
slouken@6665
   619
slouken@1895
   620
    /* See if the current window has lost focus */
slouken@3685
   621
    if (keyboard->focus && keyboard->focus != window) {
slouken@4465
   622
        SDL_SendWindowEvent(keyboard->focus, SDL_WINDOWEVENT_FOCUS_LOST,
slouken@4465
   623
                            0, 0);
slouken@4890
   624
slouken@4890
   625
        /* Ensures IME compositions are committed */
slouken@4890
   626
        if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
slouken@4890
   627
            SDL_VideoDevice *video = SDL_GetVideoDevice();
slouken@4890
   628
            if (video && video->StopTextInput) {
slouken@4890
   629
                video->StopTextInput(video);
slouken@4890
   630
            }
slouken@4890
   631
        }
slouken@1895
   632
    }
slouken@1895
   633
slouken@3685
   634
    keyboard->focus = window;
slouken@1895
   635
slouken@1895
   636
    if (keyboard->focus) {
slouken@3502
   637
        SDL_SendWindowEvent(keyboard->focus, SDL_WINDOWEVENT_FOCUS_GAINED,
slouken@3502
   638
                            0, 0);
slouken@4435
   639
slouken@4435
   640
        if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
slouken@4890
   641
            SDL_VideoDevice *video = SDL_GetVideoDevice();
slouken@4890
   642
            if (video && video->StartTextInput) {
slouken@4890
   643
                video->StartTextInput(video);
slouken@4890
   644
            }
slouken@4435
   645
        }
slouken@1895
   646
    }
slouken@1895
   647
}
slouken@1895
   648
slouken@1895
   649
int
slouken@5218
   650
SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode)
slouken@1895
   651
{
slouken@4465
   652
    SDL_Keyboard *keyboard = &SDL_keyboard;
slouken@2129
   653
    int posted;
slouken@1895
   654
    Uint16 modstate;
slouken@4429
   655
    Uint32 type;
slouken@4565
   656
    Uint8 repeat;
slouken@1895
   657
slouken@4465
   658
    if (!scancode) {
slouken@1895
   659
        return 0;
slouken@1895
   660
    }
slouken@6661
   661
#ifdef DEBUG_KEYBOARD
slouken@2303
   662
    printf("The '%s' key has been %s\n", SDL_GetScancodeName(scancode),
slouken@1895
   663
           state == SDL_PRESSED ? "pressed" : "released");
slouken@0
   664
#endif
slouken@1895
   665
    if (state == SDL_PRESSED) {
slouken@1895
   666
        modstate = keyboard->modstate;
slouken@2303
   667
        switch (scancode) {
slouken@2303
   668
        case SDL_SCANCODE_NUMLOCKCLEAR:
slouken@1895
   669
            keyboard->modstate ^= KMOD_NUM;
slouken@1895
   670
            break;
slouken@2303
   671
        case SDL_SCANCODE_CAPSLOCK:
slouken@1895
   672
            keyboard->modstate ^= KMOD_CAPS;
slouken@1895
   673
            break;
slouken@2303
   674
        case SDL_SCANCODE_LCTRL:
slouken@1895
   675
            keyboard->modstate |= KMOD_LCTRL;
slouken@1895
   676
            break;
slouken@2303
   677
        case SDL_SCANCODE_RCTRL:
slouken@1895
   678
            keyboard->modstate |= KMOD_RCTRL;
slouken@1895
   679
            break;
slouken@2303
   680
        case SDL_SCANCODE_LSHIFT:
slouken@1895
   681
            keyboard->modstate |= KMOD_LSHIFT;
slouken@1895
   682
            break;
slouken@2303
   683
        case SDL_SCANCODE_RSHIFT:
slouken@1895
   684
            keyboard->modstate |= KMOD_RSHIFT;
slouken@1895
   685
            break;
slouken@2303
   686
        case SDL_SCANCODE_LALT:
slouken@1895
   687
            keyboard->modstate |= KMOD_LALT;
slouken@1895
   688
            break;
slouken@2303
   689
        case SDL_SCANCODE_RALT:
slouken@1895
   690
            keyboard->modstate |= KMOD_RALT;
slouken@1895
   691
            break;
slouken@2303
   692
        case SDL_SCANCODE_LGUI:
slouken@2303
   693
            keyboard->modstate |= KMOD_LGUI;
slouken@1895
   694
            break;
slouken@2303
   695
        case SDL_SCANCODE_RGUI:
slouken@2303
   696
            keyboard->modstate |= KMOD_RGUI;
slouken@1895
   697
            break;
slouken@2303
   698
        case SDL_SCANCODE_MODE:
slouken@1895
   699
            keyboard->modstate |= KMOD_MODE;
slouken@1895
   700
            break;
slouken@1895
   701
        default:
slouken@1895
   702
            break;
slouken@1895
   703
        }
slouken@1895
   704
    } else {
slouken@2303
   705
        switch (scancode) {
slouken@2303
   706
        case SDL_SCANCODE_NUMLOCKCLEAR:
slouken@2303
   707
        case SDL_SCANCODE_CAPSLOCK:
slouken@1895
   708
            break;
slouken@2303
   709
        case SDL_SCANCODE_LCTRL:
slouken@1895
   710
            keyboard->modstate &= ~KMOD_LCTRL;
slouken@1895
   711
            break;
slouken@2303
   712
        case SDL_SCANCODE_RCTRL:
slouken@1895
   713
            keyboard->modstate &= ~KMOD_RCTRL;
slouken@1895
   714
            break;
slouken@2303
   715
        case SDL_SCANCODE_LSHIFT:
slouken@1895
   716
            keyboard->modstate &= ~KMOD_LSHIFT;
slouken@1895
   717
            break;
slouken@2303
   718
        case SDL_SCANCODE_RSHIFT:
slouken@1895
   719
            keyboard->modstate &= ~KMOD_RSHIFT;
slouken@1895
   720
            break;
slouken@2303
   721
        case SDL_SCANCODE_LALT:
slouken@1895
   722
            keyboard->modstate &= ~KMOD_LALT;
slouken@1895
   723
            break;
slouken@2303
   724
        case SDL_SCANCODE_RALT:
slouken@1895
   725
            keyboard->modstate &= ~KMOD_RALT;
slouken@1895
   726
            break;
slouken@2303
   727
        case SDL_SCANCODE_LGUI:
slouken@2303
   728
            keyboard->modstate &= ~KMOD_LGUI;
slouken@1895
   729
            break;
slouken@2303
   730
        case SDL_SCANCODE_RGUI:
slouken@2303
   731
            keyboard->modstate &= ~KMOD_RGUI;
slouken@1895
   732
            break;
slouken@2303
   733
        case SDL_SCANCODE_MODE:
slouken@1895
   734
            keyboard->modstate &= ~KMOD_MODE;
slouken@1895
   735
            break;
slouken@1895
   736
        default:
slouken@1895
   737
            break;
slouken@1895
   738
        }
slouken@1895
   739
        modstate = keyboard->modstate;
slouken@1895
   740
    }
slouken@0
   741
slouken@1895
   742
    /* Figure out what type of event this is */
slouken@1895
   743
    switch (state) {
slouken@1895
   744
    case SDL_PRESSED:
slouken@1895
   745
        type = SDL_KEYDOWN;
slouken@1895
   746
        break;
slouken@1895
   747
    case SDL_RELEASED:
slouken@1895
   748
        type = SDL_KEYUP;
slouken@1895
   749
        break;
slouken@1895
   750
    default:
slouken@1895
   751
        /* Invalid state -- bail */
slouken@1895
   752
        return 0;
slouken@1895
   753
    }
slouken@0
   754
slouken@2303
   755
    /* Drop events that don't change state */
slouken@4565
   756
    repeat = (state && keyboard->keystate[scancode]);
slouken@4560
   757
    if (keyboard->keystate[scancode] == state && !repeat) {
slouken@1895
   758
#if 0
slouken@2303
   759
        printf("Keyboard event didn't change state - dropped!\n");
slouken@1895
   760
#endif
slouken@2303
   761
        return 0;
slouken@2303
   762
    }
slouken@0
   763
slouken@2303
   764
    /* Update internal keyboard state */
slouken@2303
   765
    keyboard->keystate[scancode] = state;
slouken@0
   766
slouken@1895
   767
    /* Post the event, if desired */
slouken@1895
   768
    posted = 0;
slouken@4429
   769
    if (SDL_GetEventState(type) == SDL_ENABLE) {
slouken@1895
   770
        SDL_Event event;
slouken@1895
   771
        event.key.type = type;
slouken@1895
   772
        event.key.state = state;
slouken@4565
   773
        event.key.repeat = repeat;
slouken@1895
   774
        event.key.keysym.scancode = scancode;
slouken@2303
   775
        event.key.keysym.sym = keyboard->keymap[scancode];
slouken@1895
   776
        event.key.keysym.mod = modstate;
gzjjgod@4437
   777
        event.key.windowID = keyboard->focus ? keyboard->focus->id : 0;
slouken@1895
   778
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   779
    }
slouken@1895
   780
    return (posted);
slouken@1895
   781
}
slouken@1327
   782
slouken@1895
   783
int
slouken@4465
   784
SDL_SendKeyboardText(const char *text)
slouken@1895
   785
{
slouken@4465
   786
    SDL_Keyboard *keyboard = &SDL_keyboard;
slouken@1895
   787
    int posted;
slouken@0
   788
slouken@4529
   789
    /* Don't post text events for unprintable characters */
slouken@4567
   790
    if ((unsigned char)*text < ' ' || *text == 127) {
slouken@4529
   791
        return 0;
slouken@4529
   792
    }
slouken@4529
   793
slouken@1895
   794
    /* Post the event, if desired */
slouken@1895
   795
    posted = 0;
slouken@4429
   796
    if (SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE) {
slouken@1895
   797
        SDL_Event event;
slouken@1895
   798
        event.text.type = SDL_TEXTINPUT;
slouken@4435
   799
        event.text.windowID = keyboard->focus ? keyboard->focus->id : 0;
dewyatt@4754
   800
        SDL_utf8strlcpy(event.text.text, text, SDL_arraysize(event.text.text));
slouken@1895
   801
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   802
    }
slouken@1895
   803
    return (posted);
slouken@0
   804
}
slouken@0
   805
slouken@3280
   806
int
slouken@4465
   807
SDL_SendEditingText(const char *text, int start, int length)
slouken@3280
   808
{
slouken@4465
   809
    SDL_Keyboard *keyboard = &SDL_keyboard;
slouken@3280
   810
    int posted;
slouken@3280
   811
slouken@3280
   812
    /* Post the event, if desired */
slouken@3280
   813
    posted = 0;
slouken@4429
   814
    if (SDL_GetEventState(SDL_TEXTEDITING) == SDL_ENABLE) {
slouken@3280
   815
        SDL_Event event;
slouken@3280
   816
        event.edit.type = SDL_TEXTEDITING;
slouken@4435
   817
        event.edit.windowID = keyboard->focus ? keyboard->focus->id : 0;
slouken@3280
   818
        event.edit.start = start;
slouken@3280
   819
        event.edit.length = length;
dewyatt@4754
   820
        SDL_utf8strlcpy(event.edit.text, text, SDL_arraysize(event.edit.text));
slouken@3280
   821
        posted = (SDL_PushEvent(&event) > 0);
slouken@3280
   822
    }
slouken@3280
   823
    return (posted);
slouken@3280
   824
}
slouken@3280
   825
slouken@2303
   826
void
slouken@2303
   827
SDL_KeyboardQuit(void)
slouken@2303
   828
{
slouken@2303
   829
}
slouken@2303
   830
slouken@7258
   831
const Uint8 *
slouken@2303
   832
SDL_GetKeyboardState(int *numkeys)
slouken@2303
   833
{
slouken@4465
   834
    SDL_Keyboard *keyboard = &SDL_keyboard;
slouken@2303
   835
slouken@2303
   836
    if (numkeys != (int *) 0) {
slouken@2303
   837
        *numkeys = SDL_NUM_SCANCODES;
slouken@2303
   838
    }
slouken@2303
   839
    return keyboard->keystate;
slouken@2303
   840
}
slouken@2303
   841
slouken@5220
   842
SDL_Keymod
slouken@2303
   843
SDL_GetModState(void)
slouken@2303
   844
{
slouken@4465
   845
    SDL_Keyboard *keyboard = &SDL_keyboard;
slouken@2303
   846
slouken@2303
   847
    return keyboard->modstate;
slouken@2303
   848
}
slouken@2303
   849
slouken@2303
   850
void
slouken@5220
   851
SDL_SetModState(SDL_Keymod modstate)
slouken@2303
   852
{
slouken@4465
   853
    SDL_Keyboard *keyboard = &SDL_keyboard;
slouken@2303
   854
slouken@2303
   855
    keyboard->modstate = modstate;
slouken@2303
   856
}
slouken@2303
   857
slouken@5220
   858
SDL_Keycode
slouken@5218
   859
SDL_GetKeyFromScancode(SDL_Scancode scancode)
slouken@2303
   860
{
slouken@4465
   861
    SDL_Keyboard *keyboard = &SDL_keyboard;
slouken@7191
   862
aschiffler@6983
   863
    if (scancode < SDL_SCANCODE_UNKNOWN || scancode >= SDL_NUM_SCANCODES) {
aschiffler@6813
   864
          SDL_InvalidParamError("scancode");
aschiffler@6813
   865
          return 0;
aschiffler@6813
   866
    }
slouken@2303
   867
slouken@2303
   868
    return keyboard->keymap[scancode];
slouken@2303
   869
}
slouken@2303
   870
slouken@5218
   871
SDL_Scancode
slouken@5220
   872
SDL_GetScancodeFromKey(SDL_Keycode key)
slouken@2303
   873
{
slouken@4465
   874
    SDL_Keyboard *keyboard = &SDL_keyboard;
slouken@5218
   875
    SDL_Scancode scancode;
slouken@2303
   876
slouken@2303
   877
    for (scancode = SDL_SCANCODE_UNKNOWN; scancode < SDL_NUM_SCANCODES;
slouken@2303
   878
         ++scancode) {
slouken@2303
   879
        if (keyboard->keymap[scancode] == key) {
slouken@2303
   880
            return scancode;
slouken@2303
   881
        }
slouken@2303
   882
    }
slouken@2303
   883
    return SDL_SCANCODE_UNKNOWN;
slouken@2303
   884
}
slouken@2303
   885
slouken@2303
   886
const char *
slouken@5218
   887
SDL_GetScancodeName(SDL_Scancode scancode)
slouken@2303
   888
{
aschiffler@6983
   889
    const char *name;
aschiffler@6983
   890
    if (scancode < SDL_SCANCODE_UNKNOWN || scancode >= SDL_NUM_SCANCODES) {
aschiffler@6983
   891
          SDL_InvalidParamError("scancode");
slouken@7191
   892
          return "";
aschiffler@6983
   893
    }
slouken@2303
   894
aschiffler@6983
   895
    name = SDL_scancode_names[scancode];
slouken@2303
   896
    if (name)
slouken@2303
   897
        return name;
slouken@2303
   898
    else
slouken@2303
   899
        return "";
slouken@2303
   900
}
slouken@2303
   901
slouken@6029
   902
SDL_Scancode SDL_GetScancodeFromName(const char *name)
slouken@6029
   903
{
slouken@7191
   904
    int i;
slouken@6029
   905
slouken@7191
   906
    if (!name || !*name) {
slouken@7191
   907
            SDL_InvalidParamError("name");
slouken@7191
   908
        return SDL_SCANCODE_UNKNOWN;
slouken@7191
   909
    }
slouken@6029
   910
slouken@7191
   911
    for (i = 0; i < SDL_arraysize(SDL_scancode_names); ++i) {
slouken@7191
   912
        if (!SDL_scancode_names[i]) {
slouken@7191
   913
            continue;
slouken@7191
   914
        }
slouken@7191
   915
        if (SDL_strcasecmp(name, SDL_scancode_names[i]) == 0) {
slouken@7191
   916
            return (SDL_Scancode)i;
slouken@7191
   917
        }
slouken@7191
   918
    }
aschiffler@6811
   919
slouken@7191
   920
    SDL_InvalidParamError("name");
slouken@7191
   921
    return SDL_SCANCODE_UNKNOWN;
slouken@6029
   922
}
slouken@6029
   923
slouken@2303
   924
const char *
slouken@5220
   925
SDL_GetKeyName(SDL_Keycode key)
slouken@2303
   926
{
slouken@2303
   927
    static char name[8];
slouken@2303
   928
    char *end;
slouken@2303
   929
slouken@2303
   930
    if (key & SDLK_SCANCODE_MASK) {
slouken@2303
   931
        return
slouken@5218
   932
            SDL_GetScancodeName((SDL_Scancode) (key & ~SDLK_SCANCODE_MASK));
slouken@2303
   933
    }
slouken@2303
   934
slouken@2989
   935
    switch (key) {
slouken@2989
   936
    case SDLK_RETURN:
slouken@2990
   937
        return SDL_GetScancodeName(SDL_SCANCODE_RETURN);
slouken@2989
   938
    case SDLK_ESCAPE:
slouken@2990
   939
        return SDL_GetScancodeName(SDL_SCANCODE_ESCAPE);
slouken@2989
   940
    case SDLK_BACKSPACE:
slouken@2990
   941
        return SDL_GetScancodeName(SDL_SCANCODE_BACKSPACE);
slouken@2989
   942
    case SDLK_TAB:
slouken@2990
   943
        return SDL_GetScancodeName(SDL_SCANCODE_TAB);
slouken@2989
   944
    case SDLK_SPACE:
slouken@2990
   945
        return SDL_GetScancodeName(SDL_SCANCODE_SPACE);
slouken@2989
   946
    case SDLK_DELETE:
slouken@2990
   947
        return SDL_GetScancodeName(SDL_SCANCODE_DELETE);
slouken@2989
   948
    default:
slouken@2989
   949
        /* Unaccented letter keys on latin keyboards are normally
slouken@2989
   950
           labeled in upper case (and probably on others like Greek or
slouken@2989
   951
           Cyrillic too, so if you happen to know for sure, please
slouken@2989
   952
           adapt this). */
slouken@2989
   953
        if (key >= 'a' && key <= 'z') {
slouken@2989
   954
            key -= 32;
slouken@2989
   955
        }
slouken@2989
   956
slouken@2989
   957
        end = SDL_UCS4ToUTF8((Uint32) key, name);
slouken@2989
   958
        *end = '\0';
slouken@2989
   959
        return name;
slouken@2303
   960
    }
slouken@2303
   961
}
slouken@2303
   962
slouken@6029
   963
SDL_Keycode
slouken@6029
   964
SDL_GetKeyFromName(const char *name)
slouken@6029
   965
{
slouken@7191
   966
    SDL_Keycode key;
slouken@6029
   967
aschiffler@6803
   968
        /* Check input */
aschiffler@6803
   969
        if (name == NULL) return SDLK_UNKNOWN;
slouken@6029
   970
slouken@7191
   971
    /* If it's a single UTF-8 character, then that's the keycode itself */
slouken@7191
   972
    key = *(const unsigned char *)name;
slouken@7191
   973
    if (key >= 0xF0) {
slouken@7191
   974
        if (SDL_strlen(name) == 4) {
slouken@7191
   975
            int i = 0;
slouken@7191
   976
            key  = (Uint16)(name[i]&0x07) << 18;
slouken@7191
   977
            key |= (Uint16)(name[++i]&0x3F) << 12;
slouken@7191
   978
            key |= (Uint16)(name[++i]&0x3F) << 6;
slouken@7191
   979
            key |= (Uint16)(name[++i]&0x3F);
slouken@7191
   980
            return key;
slouken@7191
   981
        }
slouken@7191
   982
        return SDLK_UNKNOWN;
slouken@7191
   983
    } else if (key >= 0xE0) {
slouken@7191
   984
        if (SDL_strlen(name) == 3) {
slouken@7191
   985
            int i = 0;
slouken@7191
   986
            key  = (Uint16)(name[i]&0x0F) << 12;
slouken@7191
   987
            key |= (Uint16)(name[++i]&0x3F) << 6;
slouken@7191
   988
            key |= (Uint16)(name[++i]&0x3F);
slouken@7191
   989
            return key;
slouken@7191
   990
        }
slouken@7191
   991
        return SDLK_UNKNOWN;
slouken@7191
   992
    } else if (key >= 0xC0) {
slouken@7191
   993
        if (SDL_strlen(name) == 2) {
slouken@7191
   994
            int i = 0;
slouken@7191
   995
            key  = (Uint16)(name[i]&0x1F) << 6;
slouken@7191
   996
            key |= (Uint16)(name[++i]&0x3F);
slouken@7191
   997
            return key;
slouken@7191
   998
        }
slouken@7191
   999
        return SDLK_UNKNOWN;
slouken@7191
  1000
    } else {
slouken@7191
  1001
        if (SDL_strlen(name) == 1) {
slouken@7191
  1002
            if (key >= 'A' && key <= 'Z') {
slouken@7191
  1003
                key += 32;
slouken@7191
  1004
            }
slouken@7191
  1005
            return key;
slouken@7191
  1006
        }
slouken@7191
  1007
slouken@7191
  1008
        /* Get the scancode for this name, and the associated keycode */
slouken@7191
  1009
        return SDL_default_keymap[SDL_GetScancodeFromName(name)];
slouken@7191
  1010
    }
slouken@6029
  1011
}
slouken@6029
  1012
slouken@1895
  1013
/* vi: set ts=4 sw=4 expandtab: */