src/events/SDL_keyboard.c
author Paul Hunkin <paul@bieh.net>
Wed, 07 Jul 2010 00:43:23 +1200
changeset 4713 ba38983b10c2
parent 4437 25e45611fa3d
child 4729 1f7ad083fd3c
permissions -rw-r--r--
Added preliminary keyboard event support
     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         if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
   684             SDL_StartTextInput();
   685         }
   686     }
   687 }
   688 
   689 int
   690 SDL_SendKeyboardKey(int index, Uint8 state, SDL_scancode scancode)
   691 {
   692     SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
   693     int posted;
   694     Uint16 modstate;
   695     Uint32 type;
   696 
   697     if(!keyboard){
   698         return 7;
   699     }
   700 
   701     if(!scancode){
   702         return 8;
   703     }
   704 
   705     if (!keyboard || !scancode) {
   706         return 1;
   707     }
   708 #if 0
   709     printf("The '%s' key has been %s\n", SDL_GetScancodeName(scancode),
   710            state == SDL_PRESSED ? "pressed" : "released");
   711 #endif
   712     if (state == SDL_PRESSED) {
   713         modstate = keyboard->modstate;
   714         switch (scancode) {
   715         case SDL_SCANCODE_NUMLOCKCLEAR:
   716             keyboard->modstate ^= KMOD_NUM;
   717             break;
   718         case SDL_SCANCODE_CAPSLOCK:
   719             keyboard->modstate ^= KMOD_CAPS;
   720             break;
   721         case SDL_SCANCODE_LCTRL:
   722             keyboard->modstate |= KMOD_LCTRL;
   723             break;
   724         case SDL_SCANCODE_RCTRL:
   725             keyboard->modstate |= KMOD_RCTRL;
   726             break;
   727         case SDL_SCANCODE_LSHIFT:
   728             keyboard->modstate |= KMOD_LSHIFT;
   729             break;
   730         case SDL_SCANCODE_RSHIFT:
   731             keyboard->modstate |= KMOD_RSHIFT;
   732             break;
   733         case SDL_SCANCODE_LALT:
   734             keyboard->modstate |= KMOD_LALT;
   735             break;
   736         case SDL_SCANCODE_RALT:
   737             keyboard->modstate |= KMOD_RALT;
   738             break;
   739         case SDL_SCANCODE_LGUI:
   740             keyboard->modstate |= KMOD_LGUI;
   741             break;
   742         case SDL_SCANCODE_RGUI:
   743             keyboard->modstate |= KMOD_RGUI;
   744             break;
   745         case SDL_SCANCODE_MODE:
   746             keyboard->modstate |= KMOD_MODE;
   747             break;
   748         default:
   749             break;
   750         }
   751     } else {
   752         switch (scancode) {
   753         case SDL_SCANCODE_NUMLOCKCLEAR:
   754         case SDL_SCANCODE_CAPSLOCK:
   755             break;
   756         case SDL_SCANCODE_LCTRL:
   757             keyboard->modstate &= ~KMOD_LCTRL;
   758             break;
   759         case SDL_SCANCODE_RCTRL:
   760             keyboard->modstate &= ~KMOD_RCTRL;
   761             break;
   762         case SDL_SCANCODE_LSHIFT:
   763             keyboard->modstate &= ~KMOD_LSHIFT;
   764             break;
   765         case SDL_SCANCODE_RSHIFT:
   766             keyboard->modstate &= ~KMOD_RSHIFT;
   767             break;
   768         case SDL_SCANCODE_LALT:
   769             keyboard->modstate &= ~KMOD_LALT;
   770             break;
   771         case SDL_SCANCODE_RALT:
   772             keyboard->modstate &= ~KMOD_RALT;
   773             break;
   774         case SDL_SCANCODE_LGUI:
   775             keyboard->modstate &= ~KMOD_LGUI;
   776             break;
   777         case SDL_SCANCODE_RGUI:
   778             keyboard->modstate &= ~KMOD_RGUI;
   779             break;
   780         case SDL_SCANCODE_MODE:
   781             keyboard->modstate &= ~KMOD_MODE;
   782             break;
   783         default:
   784             break;
   785         }
   786         modstate = keyboard->modstate;
   787     }
   788 
   789     /* Figure out what type of event this is */
   790     switch (state) {
   791     case SDL_PRESSED:
   792         type = SDL_KEYDOWN;
   793         break;
   794     case SDL_RELEASED:
   795         type = SDL_KEYUP;
   796         break;
   797     default:
   798         /* Invalid state -- bail */
   799         return 2;
   800     }
   801 
   802     /* Drop events that don't change state */
   803     if (keyboard->keystate[scancode] == state) {
   804 #if 0
   805         printf("Keyboard event didn't change state - dropped!\n");
   806 #endif
   807         return 3;
   808     }
   809 
   810     /* Update internal keyboard state */
   811     keyboard->keystate[scancode] = state;
   812 
   813     /* Post the event, if desired */
   814     posted = 4;
   815     if (SDL_GetEventState(type) == SDL_ENABLE) {
   816         SDL_Event event;
   817         event.key.type = type;
   818         event.key.which = (Uint8) index;
   819         event.key.state = state;
   820         event.key.keysym.scancode = scancode;
   821         event.key.keysym.sym = keyboard->keymap[scancode];
   822         event.key.keysym.mod = modstate;
   823         event.key.keysym.unicode = 0;
   824         event.key.windowID = keyboard->focus ? keyboard->focus->id : 0;
   825         posted = (SDL_PushEvent(&event) > 0);
   826     }
   827     return (posted);
   828 }
   829 
   830 int
   831 SDL_SendKeyboardText(int index, const char *text)
   832 {
   833     SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
   834     int posted;
   835 
   836     if (!keyboard) {
   837         return 0;
   838     }
   839 
   840     /* Post the event, if desired */
   841     posted = 0;
   842     if (SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE) {
   843         SDL_Event event;
   844         event.text.type = SDL_TEXTINPUT;
   845         event.text.windowID = keyboard->focus ? keyboard->focus->id : 0;
   846         event.text.which = (Uint8) index;
   847         SDL_strlcpy(event.text.text, text, SDL_arraysize(event.text.text));
   848         event.text.windowID = keyboard->focus ? keyboard->focus->id : 0;
   849         posted = (SDL_PushEvent(&event) > 0);
   850     }
   851     return (posted);
   852 }
   853 
   854 int
   855 SDL_SendEditingText(int index, const char *text, int start, int length)
   856 {
   857     SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
   858     int posted;
   859 
   860     if (!keyboard) {
   861         return 0;
   862     }
   863 
   864     /* Post the event, if desired */
   865     posted = 0;
   866     if (SDL_GetEventState(SDL_TEXTEDITING) == SDL_ENABLE) {
   867         SDL_Event event;
   868         event.edit.type = SDL_TEXTEDITING;
   869         event.edit.windowID = keyboard->focus ? keyboard->focus->id : 0;
   870         event.text.which = (Uint8) index;
   871         event.edit.start = start;
   872         event.edit.length = length;
   873         SDL_strlcpy(event.edit.text, text, SDL_arraysize(event.edit.text));
   874         posted = (SDL_PushEvent(&event) > 0);
   875     }
   876     return (posted);
   877 }
   878 
   879 void
   880 SDL_KeyboardQuit(void)
   881 {
   882     int i;
   883 
   884     for (i = 0; i < SDL_num_keyboards; ++i) {
   885         SDL_DelKeyboard(i);
   886     }
   887     SDL_num_keyboards = 0;
   888     SDL_current_keyboard = 0;
   889 
   890     if (SDL_keyboards) {
   891         SDL_free(SDL_keyboards);
   892         SDL_keyboards = NULL;
   893     }
   894 }
   895 
   896 int
   897 SDL_GetNumKeyboards(void)
   898 {
   899     return SDL_num_keyboards;
   900 }
   901 
   902 int
   903 SDL_SelectKeyboard(int index)
   904 {
   905     if (index >= 0 && index < SDL_num_keyboards) {
   906         SDL_current_keyboard = index;
   907     }
   908     return SDL_current_keyboard;
   909 }
   910 
   911 Uint8 *
   912 SDL_GetKeyboardState(int *numkeys)
   913 {
   914     SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
   915 
   916     if (numkeys != (int *) 0) {
   917         *numkeys = SDL_NUM_SCANCODES;
   918     }
   919 
   920     if (!keyboard) {
   921         return NULL;
   922     }
   923     return keyboard->keystate;
   924 }
   925 
   926 SDLMod
   927 SDL_GetModState(void)
   928 {
   929     SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
   930 
   931     if (!keyboard) {
   932         return KMOD_NONE;
   933     }
   934     return keyboard->modstate;
   935 }
   936 
   937 void
   938 SDL_SetModState(SDLMod modstate)
   939 {
   940     SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
   941 
   942     if (!keyboard) {
   943         return;
   944     }
   945     keyboard->modstate = modstate;
   946 }
   947 
   948 SDLKey
   949 SDL_GetKeyFromScancode(SDL_scancode scancode)
   950 {
   951     SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
   952 
   953     if (!keyboard) {
   954         return SDLK_UNKNOWN;
   955     }
   956     return keyboard->keymap[scancode];
   957 }
   958 
   959 SDL_scancode
   960 SDL_GetScancodeFromKey(SDLKey key)
   961 {
   962     SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
   963     SDL_scancode scancode;
   964 
   965     if (!keyboard) {
   966         return SDL_SCANCODE_UNKNOWN;
   967     }
   968 
   969     for (scancode = SDL_SCANCODE_UNKNOWN; scancode < SDL_NUM_SCANCODES;
   970          ++scancode) {
   971         if (keyboard->keymap[scancode] == key) {
   972             return scancode;
   973         }
   974     }
   975     return SDL_SCANCODE_UNKNOWN;
   976 }
   977 
   978 const char *
   979 SDL_GetScancodeName(SDL_scancode scancode)
   980 {
   981     const char *name = SDL_scancode_names[scancode];
   982 
   983     if (name)
   984         return name;
   985     else
   986         return "";
   987 }
   988 
   989 const char *
   990 SDL_GetKeyName(SDLKey key)
   991 {
   992     static char name[8];
   993     char *end;
   994 
   995     if (key & SDLK_SCANCODE_MASK) {
   996         return
   997             SDL_GetScancodeName((SDL_scancode) (key & ~SDLK_SCANCODE_MASK));
   998     }
   999 
  1000     switch (key) {
  1001     case SDLK_RETURN:
  1002         return SDL_GetScancodeName(SDL_SCANCODE_RETURN);
  1003     case SDLK_ESCAPE:
  1004         return SDL_GetScancodeName(SDL_SCANCODE_ESCAPE);
  1005     case SDLK_BACKSPACE:
  1006         return SDL_GetScancodeName(SDL_SCANCODE_BACKSPACE);
  1007     case SDLK_TAB:
  1008         return SDL_GetScancodeName(SDL_SCANCODE_TAB);
  1009     case SDLK_SPACE:
  1010         return SDL_GetScancodeName(SDL_SCANCODE_SPACE);
  1011     case SDLK_DELETE:
  1012         return SDL_GetScancodeName(SDL_SCANCODE_DELETE);
  1013     default:
  1014         /* Unaccented letter keys on latin keyboards are normally
  1015            labeled in upper case (and probably on others like Greek or
  1016            Cyrillic too, so if you happen to know for sure, please
  1017            adapt this). */
  1018         if (key >= 'a' && key <= 'z') {
  1019             key -= 32;
  1020         }
  1021 
  1022         end = SDL_UCS4ToUTF8((Uint32) key, name);
  1023         *end = '\0';
  1024         return name;
  1025     }
  1026 }
  1027 
  1028 /* vi: set ts=4 sw=4 expandtab: */