keyboard.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * File : keyboard.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://openlab.rt-thread.com/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-09-15 QiuYi the first version
  13. */
  14. #include <rtthread.h>
  15. #include <rthw.h>
  16. #include <bsp.h>
  17. #define NO 0
  18. #define SHIFT (1 << 0)
  19. #define CTL (1 << 1)
  20. #define ALT (1 << 2)
  21. #define CAPSLOCK (1<<3)
  22. #define NUMLOCK (1<<4)
  23. #define SCROLLOCK (1<<5)
  24. static int shiftcode[256] =
  25. {
  26. [29] CTL,
  27. [42] SHIFT,
  28. [54] SHIFT,
  29. [56] ALT,
  30. };
  31. static int togglecode[256] =
  32. {
  33. [58] CAPSLOCK,
  34. [69] NUMLOCK,
  35. [70] SCROLLOCK,
  36. };
  37. static char normalmap[256] =
  38. {
  39. NO, 033, '1', '2', '3', '4', '5', '6',
  40. '7', '8', '9', '0', '-', '=', '\b', '\t',
  41. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
  42. 'o', 'p', '[', ']', '\n', NO, 'a', 's',
  43. 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
  44. '\'', '`', NO, '\\', 'z', 'x', 'c', 'v',
  45. 'b', 'n', 'm', ',', '.', '/', NO, '*',
  46. NO, ' ', NO, NO, NO, NO, NO, NO,
  47. NO, NO, NO, NO, NO, NO, NO, '7',
  48. '8', '9', '-', '4', '5', '6', '+', '1',
  49. '2', '3', '0', '.',
  50. };
  51. static char shiftmap[256] =
  52. {
  53. NO, 033, '!', '@', '#', '$', '%', '^',
  54. '&', '*', '(', ')', '_', '+', '\b', '\t',
  55. 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
  56. 'O', 'P', '{', '}', '\n', NO, 'A', 'S',
  57. 'D', 'F', 'G', 'H', 'J', 'K', 'L', ';',
  58. '"', '~', NO, '|', 'Z', 'X', 'C', 'V',
  59. 'B', 'N', 'M', '<', '>', '?', NO, '*',
  60. NO, ' ', NO, NO, NO, NO, NO, NO,
  61. NO, NO, NO, NO, NO, NO, NO, '7',
  62. '8', '9', '-', '4', '5', '6', '+', '1',
  63. '2', '3', '0', '.',
  64. };
  65. #define C(x) (x-'@')
  66. static char ctlmap[256] =
  67. {
  68. NO, NO, NO, NO, NO, NO, NO, NO,
  69. NO, NO, NO, NO, NO, NO, NO, NO,
  70. C('Q'), C('W'), C('E'), C('R'), C('T'), C('Y'), C('U'), C('I'),
  71. C('O'), C('P'), NO, NO, '\r', NO, C('A'), C('S'),
  72. C('D'), C('F'), C('G'), C('H'), C('J'), C('K'), C('L'), NO,
  73. NO, NO, NO, C('\\'), C('Z'), C('X'), C('C'), C('V'),
  74. C('B'), C('N'), C('M'), NO, NO, C('/'), NO, NO,
  75. };
  76. static char *charcode[4] =
  77. {
  78. normalmap,
  79. shiftmap,
  80. ctlmap,
  81. ctlmap,
  82. };
  83. /**
  84. * @addtogroup QEMU
  85. */
  86. /*@{*/
  87. /**
  88. * This function get a char from the keyboard
  89. */
  90. char rt_keyboard_getc(void)
  91. {
  92. int c;
  93. rt_uint8_t data;
  94. static rt_uint32_t shift;
  95. if ((inb(KBSTATP) & KBS_DIB) == 0)
  96. return -1;
  97. data = inb(KBDATAP);
  98. if (data & 0x80)
  99. {
  100. /* key up */
  101. shift &= ~shiftcode[data&~0x80];
  102. return 0;
  103. }
  104. /* key down */
  105. shift |= shiftcode[data];
  106. shift ^= togglecode[data];
  107. c = charcode[shift&(CTL|SHIFT)][data];
  108. if (shift&CAPSLOCK)
  109. {
  110. if ('a' <= c && c <= 'z')
  111. c += 'A' - 'a';
  112. else if ('A' <= c && c <= 'Z')
  113. c += 'a' - 'A';
  114. }
  115. return c;
  116. }
  117. /*@}*/