Subversion Repositories HelenOS

Rev

Rev 4228 | Rev 4610 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2009 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 Keyboard processing.
  35.  */
  36.  
  37. #include <genarch/kbrd/kbrd.h>
  38. #include <genarch/kbrd/scanc.h>
  39.  
  40. #ifdef CONFIG_PC_KBD
  41. #include <genarch/kbrd/scanc_pc.h>
  42. #endif
  43.  
  44. #ifdef CONFIG_SUN_KBD
  45. #include <genarch/kbrd/scanc_sun.h>
  46. #endif
  47.  
  48. #include <synch/spinlock.h>
  49. #include <console/chardev.h>
  50. #include <console/console.h>
  51. #include <proc/thread.h>
  52. #include <arch.h>
  53. #include <macros.h>
  54.  
  55. #define IGNORE_CODE  0x7f
  56. #define KEY_RELEASE  0x80
  57.  
  58. #define PRESSED_SHIFT     (1 << 0)
  59. #define PRESSED_CAPSLOCK  (1 << 1)
  60. #define LOCKED_CAPSLOCK   (1 << 0)
  61.  
  62. static indev_operations_t kbrd_raw_ops = {
  63.     .poll = NULL
  64. };
  65.  
  66. /** Process release of key.
  67.  *
  68.  * @param sc Scancode of the key being released.
  69.  */
  70. static void key_released(kbrd_instance_t *instance, wchar_t sc)
  71. {
  72.     spinlock_lock(&instance->keylock);
  73.    
  74.     switch (sc) {
  75.     case SC_LSHIFT:
  76.     case SC_RSHIFT:
  77.         instance->keyflags &= ~PRESSED_SHIFT;
  78.         break;
  79.     case SC_CAPSLOCK:
  80.         instance->keyflags &= ~PRESSED_CAPSLOCK;
  81.         if (instance->lockflags & LOCKED_CAPSLOCK)
  82.             instance->lockflags &= ~LOCKED_CAPSLOCK;
  83.         else
  84.             instance->lockflags |= LOCKED_CAPSLOCK;
  85.         break;
  86.     default:
  87.         break;
  88.     }
  89.    
  90.     spinlock_unlock(&instance->keylock);
  91. }
  92.  
  93. /** Process keypress.
  94.  *
  95.  * @param sc Scancode of the key being pressed.
  96.  */
  97. static void key_pressed(kbrd_instance_t *instance, wchar_t sc)
  98. {
  99.     bool letter;
  100.     bool shift;
  101.     bool capslock;
  102.    
  103.     spinlock_lock(&instance->keylock);
  104.    
  105.     switch (sc) {
  106.     case SC_LSHIFT:
  107.     case SC_RSHIFT:
  108.         instance->keyflags |= PRESSED_SHIFT;
  109.         break;
  110.     case SC_CAPSLOCK:
  111.         instance->keyflags |= PRESSED_CAPSLOCK;
  112.         break;
  113.     case SC_SCAN_ESCAPE:
  114.         break;
  115.     default:
  116.         letter = islower(sc_primary_map[sc]);
  117.         shift = instance->keyflags & PRESSED_SHIFT;
  118.         capslock = (instance->keyflags & PRESSED_CAPSLOCK) ||
  119.             (instance->lockflags & LOCKED_CAPSLOCK);
  120.        
  121.         if ((letter) && (capslock))
  122.             shift = !shift;
  123.        
  124.         if (shift)
  125.             indev_push_character(instance->sink, sc_secondary_map[sc]);
  126.         else
  127.             indev_push_character(instance->sink, sc_primary_map[sc]);
  128.         break;
  129.     }
  130.    
  131.     spinlock_unlock(&instance->keylock);
  132. }
  133.  
  134. static void kkbrd(void *arg)
  135. {
  136.     kbrd_instance_t *instance = (kbrd_instance_t *) arg;
  137.    
  138.     while (true) {
  139.         wchar_t sc = indev_pop_character(&instance->raw);
  140.        
  141.         if (sc == IGNORE_CODE)
  142.             continue;
  143.        
  144.         if (sc & KEY_RELEASE)
  145.             key_released(instance, (sc ^ KEY_RELEASE) & 0x7f);
  146.         else
  147.             key_pressed(instance, sc & 0x7f);
  148.     }
  149. }
  150.  
  151. kbrd_instance_t *kbrd_init(void)
  152. {
  153.     kbrd_instance_t *instance
  154.         = malloc(sizeof(kbrd_instance_t), FRAME_ATOMIC);
  155.     if (instance) {
  156.         instance->thread
  157.             = thread_create(kkbrd, (void *) instance, TASK, 0, "kkbrd", false);
  158.        
  159.         if (!instance->thread) {
  160.             free(instance);
  161.             return NULL;
  162.         }
  163.        
  164.         instance->sink = NULL;
  165.         indev_initialize("kbrd", &instance->raw, &kbrd_raw_ops);
  166.        
  167.         spinlock_initialize(&instance->keylock, "instance_keylock");
  168.         instance->keyflags = 0;
  169.         instance->lockflags = 0;
  170.     }
  171.    
  172.     return instance;
  173. }
  174.  
  175. indev_t *kbrd_wire(kbrd_instance_t *instance, indev_t *sink)
  176. {
  177.     ASSERT(instance);
  178.     ASSERT(sink);
  179.    
  180.     instance->sink = sink;
  181.     thread_ready(instance->thread);
  182.    
  183.     return &instance->raw;
  184. }
  185.  
  186. /** @}
  187.  */
  188.