Subversion Repositories HelenOS

Rev

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

  1. /*
  2.  * Copyright (c) 2006 Josef Cejka
  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. /**
  30.  * @addtogroup kbdgen generic
  31.  * @brief   HelenOS generic uspace keyboard handler.
  32.  * @ingroup  kbd
  33.  * @{
  34.  */
  35. /** @file
  36.  */
  37.  
  38. #include <ipc/ipc.h>
  39. #include <ipc/services.h>
  40. #include <stdio.h>
  41. #include <unistd.h>
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44. #include <ipc/ns.h>
  45. #include <async.h>
  46. #include <errno.h>
  47. #include <libadt/fifo.h>
  48. #include <kbd/kbd.h>
  49. #include <kbd/keycode.h>
  50.  
  51. #include <kbd.h>
  52. #include <key_buffer.h>
  53. #include <kbd_port.h>
  54. #include <kbd_ctl.h>
  55. #include <layout.h>
  56.  
  57. #define NAME "kbd"
  58.  
  59. int cons_connected = 0;
  60. int phone2cons = -1;
  61. keybuffer_t keybuffer;
  62.  
  63. /** Currently active modifiers. */
  64. static unsigned mods = KM_NUM_LOCK;
  65.  
  66. /** Currently pressed lock keys. We track these to tackle autorepeat. */
  67. static unsigned lock_keys;
  68.  
  69. void kbd_push_scancode(int scancode)
  70. {
  71.     printf("scancode: 0x%x\n", scancode);
  72.     kbd_ctl_parse_scancode(scancode);
  73. }
  74.  
  75. void kbd_push_ev(int type, unsigned int key)
  76. {
  77.     kbd_event_t ev;
  78.     unsigned mod_mask;
  79.  
  80.     switch (key) {
  81.     case KC_LCTRL: mod_mask = KM_LCTRL; break;
  82.     case KC_RCTRL: mod_mask = KM_RCTRL; break;
  83.     case KC_LSHIFT: mod_mask = KM_LSHIFT; break;
  84.     case KC_RSHIFT: mod_mask = KM_RSHIFT; break;
  85.     case KC_LALT: mod_mask = KM_LALT; break;
  86.     case KC_RALT: mod_mask = KM_RALT; break;
  87.     default: mod_mask = 0; break;
  88.     }
  89.  
  90.     if (mod_mask != 0) {
  91.         if (type == KE_PRESS)
  92.             mods = mods | mod_mask;
  93.         else
  94.             mods = mods & ~mod_mask;
  95.     }
  96.  
  97.     switch (key) {
  98.     case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break;
  99.     case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; break;
  100.     case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; break;
  101.     default: mod_mask = 0; break;
  102.     }
  103.  
  104.     if (mod_mask != 0) {
  105.         if (type == KE_PRESS) {
  106.             /*
  107.              * Only change lock state on transition from released
  108.              * to pressed. This prevents autorepeat from messing
  109.              * up the lock state.
  110.              */
  111.             mods = mods ^ (mod_mask & ~lock_keys);
  112.             lock_keys = lock_keys | mod_mask;
  113.         } else {
  114.             lock_keys = lock_keys & ~mod_mask;
  115.         }
  116.     }
  117.  
  118.     printf("type: %d\n", type);
  119.     printf("mods: 0x%x\n", mods);
  120.     printf("keycode: %u\n", key);
  121.  
  122.     ev.type = type;
  123.     ev.key = key;
  124.     ev.mods = mods;
  125.  
  126.     ev.c = layout_parse_ev(&ev);
  127.  
  128.     async_msg_4(phone2cons, KBD_EVENT, ev.type, ev.key, ev.mods, ev.c);
  129. }
  130.  
  131. static void console_connection(ipc_callid_t iid, ipc_call_t *icall)
  132. {
  133.     ipc_callid_t callid;
  134.     ipc_call_t call;
  135.     int retval;
  136.  
  137.     if (cons_connected) {
  138.         ipc_answer_0(iid, ELIMIT);
  139.         return;
  140.     }
  141.     cons_connected = 1;
  142.     ipc_answer_0(iid, EOK);
  143.  
  144.     while (1) {
  145.         callid = async_get_call(&call);
  146.         switch (IPC_GET_METHOD(call)) {
  147.         case IPC_M_PHONE_HUNGUP:
  148.             cons_connected = 0;
  149.             ipc_hangup(phone2cons);
  150.             phone2cons = -1;
  151.             ipc_answer_0(callid, EOK);
  152.             return;
  153.         case IPC_M_CONNECT_TO_ME:
  154.             if (phone2cons != -1) {
  155.                 retval = ELIMIT;
  156.                 break;
  157.             }
  158.             phone2cons = IPC_GET_ARG5(call);
  159.             retval = 0;
  160.             break;
  161.         default:
  162.             retval = EINVAL;
  163.         }
  164.         ipc_answer_0(callid, retval);
  165.     }  
  166. }
  167.  
  168.  
  169. int main(int argc, char **argv)
  170. {
  171.     printf(NAME ": HelenOS Keyboard service\n");
  172.    
  173.     ipcarg_t phonead;
  174.    
  175.     /* Initialize port driver. */
  176.     if (kbd_port_init())
  177.         return -1;
  178.    
  179.     /* Initialize key buffer */
  180.     keybuffer_init(&keybuffer);
  181.    
  182.     async_set_client_connection(console_connection);
  183.  
  184.     /* Register service at nameserver. */
  185.     if (ipc_connect_to_me(PHONE_NS, SERVICE_KEYBOARD, 0, 0, &phonead) != 0)
  186.         return -1;
  187.    
  188.     printf(NAME ": Accepting connections\n");
  189.     async_manager();
  190.  
  191.     /* Not reached. */
  192.     return 0;
  193. }
  194.  
  195. /**
  196.  * @}
  197.  */
  198.