Subversion Repositories HelenOS

Rev

Rev 4377 | 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 <sysinfo.h>
  41. #include <stdio.h>
  42. #include <unistd.h>
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <ipc/ns.h>
  46. #include <async.h>
  47. #include <errno.h>
  48. #include <adt/fifo.h>
  49. #include <io/console.h>
  50. #include <io/keycode.h>
  51.  
  52. #include <kbd.h>
  53. #include <keybuffer.h>
  54. #include <kbd_port.h>
  55. #include <kbd_ctl.h>
  56. #include <layout.h>
  57.  
  58. #define NAME "kbd"
  59.  
  60. int cons_connected = 0;
  61. int phone2cons = -1;
  62. keybuffer_t keybuffer;
  63.  
  64. /** Currently active modifiers. */
  65. static unsigned mods = KM_NUM_LOCK;
  66.  
  67. /** Currently pressed lock keys. We track these to tackle autorepeat. */
  68. static unsigned lock_keys;
  69.  
  70. int cir_service = 0;
  71. int cir_phone = -1;
  72.  
  73. #define NUM_LAYOUTS 3
  74.  
  75. static layout_op_t *layout[NUM_LAYOUTS] = {
  76.     &us_qwerty_op,
  77.     &us_dvorak_op,
  78.     &cz_op
  79. };
  80.  
  81. static int active_layout = 0;
  82.  
  83. void kbd_push_scancode(int scancode)
  84. {
  85. /*  printf("scancode: 0x%x\n", scancode);*/
  86.     kbd_ctl_parse_scancode(scancode);
  87. }
  88.  
  89. void kbd_push_ev(int type, unsigned int key)
  90. {
  91.     console_event_t ev;
  92.     unsigned mod_mask;
  93.  
  94.     switch (key) {
  95.     case KC_LCTRL: mod_mask = KM_LCTRL; break;
  96.     case KC_RCTRL: mod_mask = KM_RCTRL; break;
  97.     case KC_LSHIFT: mod_mask = KM_LSHIFT; break;
  98.     case KC_RSHIFT: mod_mask = KM_RSHIFT; break;
  99.     case KC_LALT: mod_mask = KM_LALT; break;
  100.     case KC_RALT: mod_mask = KM_RALT; break;
  101.     default: mod_mask = 0; break;
  102.     }
  103.  
  104.     if (mod_mask != 0) {
  105.         if (type == KEY_PRESS)
  106.             mods = mods | mod_mask;
  107.         else
  108.             mods = mods & ~mod_mask;
  109.     }
  110.  
  111.     switch (key) {
  112.     case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break;
  113.     case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; break;
  114.     case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; break;
  115.     default: mod_mask = 0; break;
  116.     }
  117.  
  118.     if (mod_mask != 0) {
  119.         if (type == KEY_PRESS) {
  120.             /*
  121.              * Only change lock state on transition from released
  122.              * to pressed. This prevents autorepeat from messing
  123.              * up the lock state.
  124.              */
  125.             mods = mods ^ (mod_mask & ~lock_keys);
  126.             lock_keys = lock_keys | mod_mask;
  127.         } else {
  128.             lock_keys = lock_keys & ~mod_mask;
  129.         }
  130.     }
  131. /*
  132.     printf("type: %d\n", type);
  133.     printf("mods: 0x%x\n", mods);
  134.     printf("keycode: %u\n", key);
  135. */
  136.     if (type == KEY_PRESS && (mods & KM_LCTRL) &&
  137.         key == KC_F1) {
  138.         active_layout = 0;
  139.         layout[active_layout]->reset();
  140.         return;
  141.     }
  142.  
  143.     if (type == KEY_PRESS && (mods & KM_LCTRL) &&
  144.         key == KC_F2) {
  145.         active_layout = 1;
  146.         layout[active_layout]->reset();
  147.         return;
  148.     }
  149.  
  150.     if (type == KEY_PRESS && (mods & KM_LCTRL) &&
  151.         key == KC_F3) {
  152.         active_layout = 2;
  153.         layout[active_layout]->reset();
  154.         return;
  155.     }
  156.  
  157.     ev.type = type;
  158.     ev.key = key;
  159.     ev.mods = mods;
  160.  
  161.     ev.c = layout[active_layout]->parse_ev(&ev);
  162.  
  163.     async_msg_4(phone2cons, KBD_EVENT, ev.type, ev.key, ev.mods, ev.c);
  164. }
  165.  
  166. static void console_connection(ipc_callid_t iid, ipc_call_t *icall)
  167. {
  168.     ipc_callid_t callid;
  169.     ipc_call_t call;
  170.     int retval;
  171.  
  172.     if (cons_connected) {
  173.         ipc_answer_0(iid, ELIMIT);
  174.         return;
  175.     }
  176.     cons_connected = 1;
  177.     ipc_answer_0(iid, EOK);
  178.  
  179.     while (1) {
  180.         callid = async_get_call(&call);
  181.         switch (IPC_GET_METHOD(call)) {
  182.         case IPC_M_PHONE_HUNGUP:
  183.             cons_connected = 0;
  184.             ipc_hangup(phone2cons);
  185.             phone2cons = -1;
  186.             ipc_answer_0(callid, EOK);
  187.             return;
  188.         case IPC_M_CONNECT_TO_ME:
  189.             if (phone2cons != -1) {
  190.                 retval = ELIMIT;
  191.                 break;
  192.             }
  193.             phone2cons = IPC_GET_ARG5(call);
  194.             retval = 0;
  195.             break;
  196.         case KBD_YIELD:
  197.             kbd_port_yield();
  198.             retval = 0;
  199.             break;
  200.         case KBD_RECLAIM:
  201.             kbd_port_reclaim();
  202.             retval = 0;
  203.             break;
  204.         default:
  205.             retval = EINVAL;
  206.         }
  207.         ipc_answer_0(callid, retval);
  208.     }  
  209. }
  210.  
  211.  
  212. int main(int argc, char **argv)
  213. {
  214.     printf(NAME ": HelenOS Keyboard service\n");
  215.    
  216.     ipcarg_t phonead;
  217.    
  218.     if (sysinfo_value("kbd.cir.fhc") == 1)
  219.         cir_service = SERVICE_FHC;
  220.     else if (sysinfo_value("kbd.cir.obio") == 1)
  221.         cir_service = SERVICE_OBIO;
  222.    
  223.     if (cir_service) {
  224.         while (cir_phone < 0) {
  225.             cir_phone = ipc_connect_me_to_blocking(PHONE_NS, cir_service,
  226.                 0, 0);
  227.         }
  228.     }
  229.    
  230.     /* Initialize port driver. */
  231.     if (kbd_port_init() != 0)
  232.         return -1;
  233.  
  234.     /* Initialize controller driver. */
  235.     if (kbd_ctl_init() != 0)
  236.         return -1;
  237.  
  238.     /* Initialize (reset) layout. */
  239.     layout[active_layout]->reset();
  240.    
  241.     /* Initialize key buffer */
  242.     keybuffer_init(&keybuffer);
  243.    
  244.     async_set_client_connection(console_connection);
  245.  
  246.     /* Register service at nameserver. */
  247.     if (ipc_connect_to_me(PHONE_NS, SERVICE_KEYBOARD, 0, 0, &phonead) != 0)
  248.         return -1;
  249.    
  250.     printf(NAME ": Accepting connections\n");
  251.     async_manager();
  252.  
  253.     /* Not reached. */
  254.     return 0;
  255. }
  256.  
  257. /**
  258.  * @}
  259.  */
  260.