Subversion Repositories HelenOS

Rev

Rev 3478 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2006 Jakub Jermar
  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 genarch
  30.  * @{
  31.  */
  32. /**
  33.  * @file
  34.  * @brief   Key processing.
  35.  */
  36.  
  37. #include <genarch/kbd/key.h>
  38. #include <genarch/kbd/scanc.h>
  39. #ifdef CONFIG_I8042
  40. #include <genarch/kbd/scanc_pc.h>
  41. #endif
  42. #ifdef CONFIG_PL050
  43. #include <genarch/kbd/scanc_pl050.h>
  44. #endif
  45. #if (defined(CONFIG_Z8530) || defined(CONFIG_NS16550))
  46. #include <genarch/kbd/scanc_sun.h>
  47. #endif
  48. #include <synch/spinlock.h>
  49. #include <console/chardev.h>
  50. #include <macros.h>
  51.  
  52. #define PRESSED_SHIFT       (1<<0)
  53. #define PRESSED_CAPSLOCK    (1<<1)
  54. #define LOCKED_CAPSLOCK     (1<<0)
  55.  
  56. #define ACTIVE_READ_BUFF_SIZE 16    /* Must be power of 2 */
  57.  
  58. chardev_t kbrd;
  59.  
  60. static uint8_t active_read_buff[ACTIVE_READ_BUFF_SIZE];
  61.  
  62. SPINLOCK_INITIALIZE(keylock);       /**< keylock protects keyflags and lockflags. */
  63. static volatile int keyflags;       /**< Tracking of multiple keypresses. */
  64. static volatile int lockflags;      /**< Tracking of multiple keys lockings. */
  65.  
  66. /** Process release of key.
  67.  *
  68.  * @param sc Scancode of the key being released.
  69.  */
  70. void key_released(uint8_t sc)
  71. {
  72.     spinlock_lock(&keylock);
  73.     switch (sc) {
  74.     case SC_LSHIFT:
  75.     case SC_RSHIFT:
  76.         keyflags &= ~PRESSED_SHIFT;
  77.         break;
  78.     case SC_CAPSLOCK:
  79.         keyflags &= ~PRESSED_CAPSLOCK;
  80.         if (lockflags & LOCKED_CAPSLOCK)
  81.             lockflags &= ~LOCKED_CAPSLOCK;
  82.         else
  83.             lockflags |= LOCKED_CAPSLOCK;
  84.         break;
  85.     default:
  86.         break;
  87.     }
  88.     spinlock_unlock(&keylock);
  89. }
  90.  
  91. /** Process keypress.
  92.  *
  93.  * @param sc Scancode of the key being pressed.
  94.  */
  95. void key_pressed(uint8_t sc)
  96. {
  97.     char *map = sc_primary_map;
  98.     char ascii = sc_primary_map[sc];
  99.     bool shift, capslock;
  100.     bool letter = false;
  101.  
  102.     spinlock_lock(&keylock);
  103.     switch (sc) {
  104.     case SC_LSHIFT:
  105.     case SC_RSHIFT:
  106.             keyflags |= PRESSED_SHIFT;
  107.         break;
  108.     case SC_CAPSLOCK:
  109.         keyflags |= PRESSED_CAPSLOCK;
  110.         break;
  111.     case SC_SPEC_ESCAPE:
  112.         break;
  113.     case SC_LEFTARR:
  114.         chardev_push_character(&kbrd, 0x1b);
  115.         chardev_push_character(&kbrd, 0x5b);
  116.         chardev_push_character(&kbrd, 0x44);
  117.         break;
  118.     case SC_RIGHTARR:
  119.         chardev_push_character(&kbrd, 0x1b);
  120.         chardev_push_character(&kbrd, 0x5b);
  121.         chardev_push_character(&kbrd, 0x43);
  122.         break;
  123.     case SC_UPARR:
  124.         chardev_push_character(&kbrd, 0x1b);
  125.         chardev_push_character(&kbrd, 0x5b);
  126.         chardev_push_character(&kbrd, 0x41);
  127.         break;
  128.     case SC_DOWNARR:
  129.         chardev_push_character(&kbrd, 0x1b);
  130.         chardev_push_character(&kbrd, 0x5b);
  131.         chardev_push_character(&kbrd, 0x42);
  132.         break;
  133.     case SC_HOME:
  134.         chardev_push_character(&kbrd, 0x1b);
  135.         chardev_push_character(&kbrd, 0x4f);
  136.         chardev_push_character(&kbrd, 0x48);
  137.         break;
  138.     case SC_END:
  139.         chardev_push_character(&kbrd, 0x1b);
  140.         chardev_push_character(&kbrd, 0x4f);
  141.         chardev_push_character(&kbrd, 0x46);
  142.         break;
  143.     case SC_DELETE:
  144.         chardev_push_character(&kbrd, 0x1b);
  145.         chardev_push_character(&kbrd, 0x5b);
  146.         chardev_push_character(&kbrd, 0x33);
  147.         chardev_push_character(&kbrd, 0x7e);
  148.         break;
  149.     default:
  150.             letter = islower(ascii);
  151.         capslock = (keyflags & PRESSED_CAPSLOCK) ||
  152.             (lockflags & LOCKED_CAPSLOCK);
  153.         shift = keyflags & PRESSED_SHIFT;
  154.         if (letter && capslock)
  155.             shift = !shift;
  156.         if (shift)
  157.             map = sc_secondary_map;
  158.         chardev_push_character(&kbrd, map[sc]);
  159.         break;
  160.     }
  161.     spinlock_unlock(&keylock);
  162. }
  163.  
  164. uint8_t active_read_buff_read(void)
  165. {
  166.     static int i=0;
  167.     i &= (ACTIVE_READ_BUFF_SIZE-1);
  168.     if(!active_read_buff[i]) {
  169.         return 0;
  170.     }
  171.     return active_read_buff[i++];
  172. }
  173.  
  174. void active_read_buff_write(uint8_t ch)
  175. {
  176.     static int i=0;
  177.     active_read_buff[i] = ch;
  178.     i++;
  179.     i &= (ACTIVE_READ_BUFF_SIZE-1);
  180.     active_read_buff[i]=0;
  181. }
  182.  
  183.  
  184. void active_read_key_pressed(uint8_t sc)
  185. {
  186.     char *map = sc_primary_map;
  187.     char ascii = sc_primary_map[sc];
  188.     bool shift, capslock;
  189.     bool letter = false;
  190.  
  191.     /*spinlock_lock(&keylock);*/
  192.     switch (sc) {
  193.     case SC_LSHIFT:
  194.     case SC_RSHIFT:
  195.             keyflags |= PRESSED_SHIFT;
  196.         break;
  197.     case SC_CAPSLOCK:
  198.         keyflags |= PRESSED_CAPSLOCK;
  199.         break;
  200.     case SC_SPEC_ESCAPE:
  201.         break;
  202.     case SC_LEFTARR:
  203.         active_read_buff_write(0x1b);
  204.         active_read_buff_write(0x5b);
  205.         active_read_buff_write(0x44);
  206.         break;
  207.     case SC_RIGHTARR:
  208.         active_read_buff_write(0x1b);
  209.         active_read_buff_write(0x5b);
  210.         active_read_buff_write(0x43);
  211.         break;
  212.     case SC_UPARR:
  213.         active_read_buff_write(0x1b);
  214.         active_read_buff_write(0x5b);
  215.         active_read_buff_write(0x41);
  216.         break;
  217.     case SC_DOWNARR:
  218.         active_read_buff_write(0x1b);
  219.         active_read_buff_write(0x5b);
  220.         active_read_buff_write(0x42);
  221.         break;
  222.     case SC_HOME:
  223.         active_read_buff_write(0x1b);
  224.         active_read_buff_write(0x4f);
  225.         active_read_buff_write(0x48);
  226.         break;
  227.     case SC_END:
  228.         active_read_buff_write(0x1b);
  229.         active_read_buff_write(0x4f);
  230.         active_read_buff_write(0x46);
  231.         break;
  232.     case SC_DELETE:
  233.         active_read_buff_write(0x1b);
  234.         active_read_buff_write(0x5b);
  235.         active_read_buff_write(0x33);
  236.         active_read_buff_write(0x7e);
  237.         break;
  238.     default:
  239.             letter = islower(ascii);
  240.         capslock = (keyflags & PRESSED_CAPSLOCK) ||
  241.             (lockflags & LOCKED_CAPSLOCK);
  242.         shift = keyflags & PRESSED_SHIFT;
  243.         if (letter && capslock)
  244.             shift = !shift;
  245.         if (shift)
  246.             map = sc_secondary_map;
  247.         active_read_buff_write(map[sc]);
  248.         break;
  249.     }
  250.     /*spinlock_unlock(&keylock);*/
  251.  
  252. }
  253.  
  254. /** @}
  255.  */
  256.