src/events/SDL_keyboard.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 24 Jan 2010 21:10:53 +0000
changeset 3697 f7b03b6838cb
parent 3685 64ce267332c6
child 4429 faa9fc8e7f67
permissions -rw-r--r--
Fixed bug #926

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