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