io.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* I/O library: PS/2 keyboard and text output.
  2. Text output is routed to the current process's screen (see process.c);
  3. slot 0 / boot fall back to a static screen the WM does not render. */
  4. #include "io.h"
  5. #include "fbtext.h"
  6. #include "process.h"
  7. #include "screen.h"
  8. #define VGA_ATTR 0x0A
  9. static unsigned int io_cols = 80;
  10. static unsigned int io_rows = 25;
  11. #define KB_DATA 0x60
  12. #define KB_STATUS 0x64
  13. static inline unsigned char inb(unsigned short port)
  14. {
  15. unsigned char v;
  16. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  17. return v;
  18. }
  19. static inline void outb(unsigned short port, unsigned char v)
  20. {
  21. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  22. }
  23. /* Scancode set 1 (XT) -> ASCII. 0 = ignore. */
  24. static const unsigned char scancode_to_ascii[256] __attribute__((section(".rodata"))) = {
  25. [0x01] = 0x1B, /* Escape */
  26. [0x02] = '1', [0x03] = '2', [0x04] = '3', [0x05] = '4', [0x06] = '5',
  27. [0x07] = '6', [0x08] = '7', [0x09] = '8', [0x0A] = '9', [0x0B] = '0',
  28. [0x0C] = '-', [0x0D] = '=', [0x0E] = '\b', [0x0F] = '\t',
  29. [0x10] = 'q', [0x11] = 'w', [0x12] = 'e', [0x13] = 'r', [0x14] = 't',
  30. [0x15] = 'y', [0x16] = 'u', [0x17] = 'i', [0x18] = 'o', [0x19] = 'p',
  31. [0x1A] = '[', [0x1B] = ']', [0x1C] = '\n',
  32. [0x1E] = 'a', [0x1F] = 's', [0x20] = 'd', [0x21] = 'f', [0x22] = 'g',
  33. [0x23] = 'h', [0x24] = 'j', [0x25] = 'k', [0x26] = 'l', [0x27] = ';',
  34. [0x28] = '\'', [0x29] = '`',
  35. [0x2B] = '\\', [0x2C] = 'z', [0x2D] = 'x', [0x2E] = 'c', [0x2F] = 'v',
  36. [0x30] = 'b', [0x31] = 'n', [0x32] = 'm', [0x33] = ',', [0x34] = '.',
  37. [0x35] = '/', [0x39] = ' ',
  38. };
  39. static int shift_pressed;
  40. static int ctrl_pressed;
  41. static unsigned char last_scancode;
  42. static int kbd_extended; /* 1 after 0xE0, next byte is extended scancode */
  43. #define KBD_BUF_SIZE 16
  44. static volatile unsigned char kbd_buf[KBD_BUF_SIZE];
  45. static volatile unsigned int kbd_head, kbd_tail;
  46. /* Fallback screen for slot 0 / boot / exception output (not rendered by WM). */
  47. static struct text_screen io_fallback;
  48. static struct text_screen *io_current_screen(void)
  49. {
  50. struct text_screen *s = procs[current_pid].screen;
  51. return s ? s : &io_fallback;
  52. }
  53. void kbd_irq(void)
  54. {
  55. unsigned char sc = inb(KB_DATA);
  56. unsigned int next = (kbd_head + 1) % KBD_BUF_SIZE;
  57. if (next != kbd_tail) {
  58. kbd_buf[kbd_head] = sc;
  59. kbd_head = next;
  60. }
  61. outb(0x20, 0x20);
  62. }
  63. static int kbd_buf_get(unsigned char *sc)
  64. {
  65. if (kbd_tail == kbd_head)
  66. return 0;
  67. *sc = kbd_buf[kbd_tail];
  68. kbd_tail = (kbd_tail + 1) % KBD_BUF_SIZE;
  69. return 1;
  70. }
  71. static char apply_shift(char c)
  72. {
  73. if (!shift_pressed) return c;
  74. if (c >= 'a' && c <= 'z') return (char)(c - 32);
  75. switch ((unsigned char)c) {
  76. case '1': return '!'; case '2': return '@'; case '3': return '#';
  77. case '4': return '$'; case '5': return '%'; case '6': return '^';
  78. case '7': return '&'; case '8': return '*'; case '9': return '(';
  79. case '0': return ')'; case '-': return '_'; case '=': return '+';
  80. case '[': return '{'; case ']': return '}'; case ';': return ':';
  81. case '\'': return '"'; case '`': return '~'; case '\\': return '|';
  82. case ',': return '<'; case '.': return '>'; case '/': return '?';
  83. default: return c;
  84. }
  85. }
  86. unsigned int io_get_cols(void)
  87. {
  88. return io_cols;
  89. }
  90. unsigned int io_get_rows(void)
  91. {
  92. return io_rows;
  93. }
  94. void io_init(void)
  95. {
  96. fbtext_init();
  97. io_cols = fbtext_get_cols();
  98. io_rows = fbtext_get_rows();
  99. screen_init(&io_fallback);
  100. io_drain_keyboard();
  101. }
  102. void io_clear_screen(void)
  103. {
  104. screen_clear(io_current_screen());
  105. }
  106. int io_did_scroll(void)
  107. {
  108. return screen_did_scroll(io_current_screen());
  109. }
  110. void io_putchar(char c)
  111. {
  112. screen_putchar(io_current_screen(), c);
  113. }
  114. void io_putstr(const char *s)
  115. {
  116. screen_putstr(io_current_screen(), s);
  117. }
  118. void io_puthex(unsigned int n)
  119. {
  120. screen_puthex(io_current_screen(), n);
  121. }
  122. unsigned int io_get_cursor(void)
  123. {
  124. return screen_get_cursor(io_current_screen());
  125. }
  126. void io_set_cursor(unsigned int pos)
  127. {
  128. screen_set_cursor(io_current_screen(), pos);
  129. }
  130. void io_cursor_show_block(void)
  131. {
  132. struct text_screen *s = io_current_screen();
  133. s->cursor_visible = 1;
  134. s->dirty = 1;
  135. }
  136. void io_cursor_hide_block(void)
  137. {
  138. struct text_screen *s = io_current_screen();
  139. s->cursor_visible = 0;
  140. s->dirty = 1;
  141. }
  142. void io_vga_write(unsigned int pos, unsigned short value)
  143. {
  144. screen_put_at(io_current_screen(), pos, value);
  145. }
  146. void io_drain_keyboard(void)
  147. {
  148. while (inb(KB_STATUS) & 1)
  149. (void)inb(KB_DATA);
  150. kbd_head = kbd_tail = 0;
  151. kbd_extended = 0;
  152. }
  153. /* Process one scancode byte; shared by io_poll_key and io_poll_key_with_hw. */
  154. static int kbd_process_byte(unsigned char sc)
  155. {
  156. if (sc == 0xE0) {
  157. kbd_extended = 1;
  158. return 0;
  159. }
  160. if (kbd_extended) {
  161. kbd_extended = 0;
  162. if (sc & 0x80)
  163. return 0; /* key release */
  164. if (sc == 0x4B) return IO_KEY_LEFT;
  165. if (sc == 0x4D) return IO_KEY_RIGHT;
  166. if (sc == 0x48) return IO_KEY_UP;
  167. if (sc == 0x50) return IO_KEY_DOWN;
  168. return 0;
  169. }
  170. if (sc & 0x80) {
  171. if (sc == 0xAA || sc == 0xB6) shift_pressed = 0;
  172. if (sc == 0x9D) ctrl_pressed = 0;
  173. return 0;
  174. }
  175. if (sc == 0x2A || sc == 0x36) {
  176. shift_pressed = 1;
  177. return 0;
  178. }
  179. if (sc == 0x1D) {
  180. ctrl_pressed = 1;
  181. return 0;
  182. }
  183. {
  184. char c = (char)scancode_to_ascii[sc];
  185. if (c) {
  186. last_scancode = sc;
  187. c = apply_shift(c);
  188. if (ctrl_pressed) {
  189. /* Ctrl+letter -> control character (e.g. Ctrl+C -> 0x03). */
  190. if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
  191. return (unsigned char)(c & 0x1F);
  192. return 0;
  193. }
  194. return (unsigned char)c;
  195. }
  196. }
  197. return 0;
  198. }
  199. int io_poll_key(void)
  200. {
  201. unsigned char sc;
  202. if (!kbd_buf_get(&sc))
  203. return 0;
  204. return kbd_process_byte(sc);
  205. }
  206. /* Like io_poll_key but also reads from port 0x60 when buffer is empty.
  207. For use by full-screen programs (e.g. space.bin) where IRQ may be missed on real HW. */
  208. int io_poll_key_with_hw(void)
  209. {
  210. unsigned char sc;
  211. if (kbd_tail == kbd_head) {
  212. if (!(inb(KB_STATUS) & 1))
  213. return 0;
  214. sc = inb(KB_DATA);
  215. } else {
  216. if (!kbd_buf_get(&sc))
  217. return 0;
  218. }
  219. return kbd_process_byte(sc);
  220. }
  221. unsigned char io_last_scancode(void)
  222. {
  223. return last_scancode;
  224. }