Subversion Repositories HelenOS

Rev

Rev 4581 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2009 Jiri Svoboda
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup kbd
  30.  * @brief   US QWERTY layout.
  31.  * @{
  32.  */
  33.  
  34. #include <kbd.h>
  35. #include <io/console.h>
  36. #include <io/keycode.h>
  37. #include <layout.h>
  38.  
  39. static void layout_reset(void);
  40. static wchar_t layout_parse_ev(console_event_t *ev);
  41.  
  42. layout_op_t us_qwerty_op = {
  43.     layout_reset,
  44.     layout_parse_ev
  45. };
  46.  
  47. static wchar_t map_lcase[] = {
  48.     [KC_Q] = 'q',
  49.     [KC_W] = 'w',
  50.     [KC_E] = 'e',
  51.     [KC_R] = 'r',
  52.     [KC_T] = 't',
  53.     [KC_Y] = 'y',
  54.     [KC_U] = 'u',
  55.     [KC_I] = 'i',
  56.     [KC_O] = 'o',
  57.     [KC_P] = 'p',
  58.  
  59.     [KC_A] = 'a',
  60.     [KC_S] = 's',
  61.     [KC_D] = 'd',
  62.     [KC_F] = 'f',
  63.     [KC_G] = 'g',
  64.     [KC_H] = 'h',
  65.     [KC_J] = 'j',
  66.     [KC_K] = 'k',
  67.     [KC_L] = 'l',
  68.  
  69.     [KC_Z] = 'z',
  70.     [KC_X] = 'x',
  71.     [KC_C] = 'c',
  72.     [KC_V] = 'v',
  73.     [KC_B] = 'b',
  74.     [KC_N] = 'n',
  75.     [KC_M] = 'm',
  76. };
  77.  
  78. static wchar_t map_ucase[] = {
  79.     [KC_Q] = 'Q',
  80.     [KC_W] = 'W',
  81.     [KC_E] = 'E',
  82.     [KC_R] = 'R',
  83.     [KC_T] = 'T',
  84.     [KC_Y] = 'Y',
  85.     [KC_U] = 'U',
  86.     [KC_I] = 'I',
  87.     [KC_O] = 'O',
  88.     [KC_P] = 'P',
  89.  
  90.     [KC_A] = 'A',
  91.     [KC_S] = 'S',
  92.     [KC_D] = 'D',
  93.     [KC_F] = 'F',
  94.     [KC_G] = 'G',
  95.     [KC_H] = 'H',
  96.     [KC_J] = 'J',
  97.     [KC_K] = 'K',
  98.     [KC_L] = 'L',
  99.  
  100.     [KC_Z] = 'Z',
  101.     [KC_X] = 'X',
  102.     [KC_C] = 'C',
  103.     [KC_V] = 'V',
  104.     [KC_B] = 'B',
  105.     [KC_N] = 'N',
  106.     [KC_M] = 'M',
  107. };
  108.  
  109. static wchar_t map_not_shifted[] = {
  110.     [KC_BACKTICK] = '`',
  111.  
  112.     [KC_1] = '1',
  113.     [KC_2] = '2',
  114.     [KC_3] = '3',
  115.     [KC_4] = '4',
  116.     [KC_5] = '5',
  117.     [KC_6] = '6',
  118.     [KC_7] = '7',
  119.     [KC_8] = '8',
  120.     [KC_9] = '9',
  121.     [KC_0] = '0',
  122.  
  123.     [KC_MINUS] = '-',
  124.     [KC_EQUALS] = '=',
  125.  
  126.     [KC_LBRACKET] = '[',
  127.     [KC_RBRACKET] = ']',
  128.  
  129.     [KC_SEMICOLON] = ';',
  130.     [KC_QUOTE] = '\'',
  131.     [KC_BACKSLASH] = '\\',
  132.  
  133.     [KC_COMMA] = ',',
  134.     [KC_PERIOD] = '.',
  135.     [KC_SLASH] = '/',
  136. };
  137.  
  138. static wchar_t map_shifted[] = {
  139.     [KC_BACKTICK] = '~',
  140.  
  141.     [KC_1] = '!',
  142.     [KC_2] = '@',
  143.     [KC_3] = '#',
  144.     [KC_4] = '$',
  145.     [KC_5] = '%',
  146.     [KC_6] = '^',
  147.     [KC_7] = '&',
  148.     [KC_8] = '*',
  149.     [KC_9] = '(',
  150.     [KC_0] = ')',
  151.  
  152.     [KC_MINUS] = '_',
  153.     [KC_EQUALS] = '+',
  154.  
  155.     [KC_LBRACKET] = '{',
  156.     [KC_RBRACKET] = '}',
  157.  
  158.     [KC_SEMICOLON] = ':',
  159.     [KC_QUOTE] = '"',
  160.     [KC_BACKSLASH] = '|',
  161.  
  162.     [KC_COMMA] = '<',
  163.     [KC_PERIOD] = '>',
  164.     [KC_SLASH] = '?',
  165. };
  166.  
  167. static wchar_t map_neutral[] = {
  168.     [KC_BACKSPACE] = '\b',
  169.     [KC_TAB] = '\t',
  170.     [KC_ENTER] = '\n',
  171.     [KC_SPACE] = ' ',
  172.  
  173.     [KC_NSLASH] = '/',
  174.     [KC_NTIMES] = '*',
  175.     [KC_NMINUS] = '-',
  176.     [KC_NPLUS] = '+',
  177.     [KC_NENTER] = '\n'
  178. };
  179.  
  180. static wchar_t map_numeric[] = {
  181.     [KC_N7] = '7',
  182.     [KC_N8] = '8',
  183.     [KC_N9] = '9',
  184.     [KC_N4] = '4',
  185.     [KC_N5] = '5',
  186.     [KC_N6] = '6',
  187.     [KC_N1] = '1',
  188.     [KC_N2] = '2',
  189.     [KC_N3] = '3',
  190.  
  191.     [KC_N0] = '0',
  192.     [KC_NPERIOD] = '.'
  193. };
  194.  
  195. static wchar_t translate(unsigned int key, wchar_t *map, size_t map_length)
  196. {
  197.     if (key >= map_length)
  198.         return 0;
  199.     return map[key];
  200. }
  201.  
  202. static void layout_reset(void)
  203. {
  204. }
  205.  
  206. static wchar_t layout_parse_ev(console_event_t *ev)
  207. {
  208.     wchar_t c;
  209.  
  210.     /* Produce no characters when Ctrl or Alt is pressed. */
  211.     if ((ev->mods & (KM_CTRL | KM_ALT)) != 0)
  212.         return 0;
  213.  
  214.     c = translate(ev->key, map_neutral, sizeof(map_neutral) / sizeof(wchar_t));
  215.     if (c != 0)
  216.         return c;
  217.  
  218.     if (((ev->mods & KM_SHIFT) != 0) ^ ((ev->mods & KM_CAPS_LOCK) != 0))
  219.         c = translate(ev->key, map_ucase, sizeof(map_ucase) / sizeof(wchar_t));
  220.     else
  221.         c = translate(ev->key, map_lcase, sizeof(map_lcase) / sizeof(wchar_t));
  222.  
  223.     if (c != 0)
  224.         return c;
  225.  
  226.     if ((ev->mods & KM_SHIFT) != 0)
  227.         c = translate(ev->key, map_shifted, sizeof(map_shifted) / sizeof(wchar_t));
  228.     else
  229.         c = translate(ev->key, map_not_shifted, sizeof(map_not_shifted) / sizeof(wchar_t));
  230.  
  231.     if (c != 0)
  232.         return c;
  233.  
  234.     if ((ev->mods & KM_NUM_LOCK) != 0)
  235.         c = translate(ev->key, map_numeric, sizeof(map_numeric) / sizeof(wchar_t));
  236.     else
  237.         c = 0;
  238.  
  239.     return c;
  240. }
  241.  
  242. /**
  243.  * @}
  244.  */
  245.