mouse.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include "mouse.h"
  2. #include "timer.h"
  3. #include "vga.h"
  4. #define PS2_DATA 0x60
  5. #define PS2_STATUS 0x64
  6. #define PS2_CMD 0x64
  7. #define STATUS_OUT_FULL 0x01
  8. #define STATUS_IN_FULL 0x02
  9. #define STATUS_AUX_DATA 0x20
  10. #define MOUSE_ACK 0xFA
  11. #define MOUSE_RESEND 0xFE
  12. #define MOUSE_CUR_W 16
  13. #define MOUSE_CUR_H 16
  14. /* Simple triangle cursor bitmap, 16×16.
  15. Hotspot is the top-left pixel of the 16×16 box (x0,y0).
  16. Each row is encoded as 16 bits; MSB (bit 15) maps to c=0. */
  17. static const unsigned short mouse_pointer_bits[MOUSE_CUR_H] = {
  18. 0x8000, /* 1....... ........ row 0: tip */
  19. 0xC000, /* 11...... ........ row 1 */
  20. 0xE000, /* 111..... ........ row 2 */
  21. 0xF000, /* 1111.... ........ row 3 */
  22. 0xF800, /* 11111... ........ row 4 */
  23. 0xFC00, /* 111111.. ........ row 5 */
  24. 0xFE00, /* 1111111. ........ row 6 */
  25. 0xFF00, /* 11111111 ........ row 7 */
  26. 0xFC00, /* 111111.. ........ row 8 */
  27. 0xE000, /* 111..... ........ row 9 */
  28. 0x8000, /* 1....... ........ row 10 */
  29. 0x0000, /* ........ ........ row 11 */
  30. 0x0000, /* ........ ........ row 12 */
  31. 0x0000, /* ........ ........ row 13 */
  32. 0x0000, /* ........ ........ row 14 */
  33. 0x0000 /* ........ ........ row 15 */
  34. };
  35. static int mouse_present_flag;
  36. static int mouse_streaming_flag;
  37. static unsigned int mouse_id;
  38. static int mouse_cursor_flag;
  39. static unsigned int fbw, fbh;
  40. static unsigned int cur_x, cur_y;
  41. static unsigned char cur_saved[MOUSE_CUR_W * MOUSE_CUR_H * 4];
  42. static unsigned char pkt[3];
  43. static unsigned int pkt_idx;
  44. static inline unsigned char inb(unsigned short port)
  45. {
  46. unsigned char v;
  47. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  48. return v;
  49. }
  50. static inline void outb(unsigned short port, unsigned char v)
  51. {
  52. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  53. }
  54. static void ps2_wait_input_empty(void)
  55. {
  56. unsigned spin = 100000;
  57. while (spin-- && (inb(PS2_STATUS) & STATUS_IN_FULL))
  58. ;
  59. }
  60. static void enable_aux_port(void)
  61. {
  62. ps2_wait_input_empty();
  63. outb(PS2_CMD, 0xA8);
  64. }
  65. static void mouse_cmd_write(unsigned char val)
  66. {
  67. ps2_wait_input_empty();
  68. outb(PS2_CMD, 0xD4);
  69. ps2_wait_input_empty();
  70. outb(PS2_DATA, val);
  71. }
  72. static int mouse_read_aux(unsigned char *out, unsigned int timeout_ticks)
  73. {
  74. unsigned int t0 = get_ticks_ms();
  75. while (get_ticks_ms() - t0 < timeout_ticks) {
  76. unsigned char st = inb(PS2_STATUS);
  77. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) ==
  78. (STATUS_OUT_FULL | STATUS_AUX_DATA)) {
  79. *out = inb(PS2_DATA);
  80. return 0;
  81. }
  82. }
  83. return -1;
  84. }
  85. static int mouse_expect_ack(void)
  86. {
  87. unsigned char b;
  88. if (mouse_read_aux(&b, 500) != 0)
  89. return -1;
  90. if (b == MOUSE_RESEND)
  91. return -2;
  92. if (b != MOUSE_ACK)
  93. return -1;
  94. return 0;
  95. }
  96. static int mouse_send_cmd_ack(unsigned char cmd)
  97. {
  98. int r;
  99. mouse_cmd_write(cmd);
  100. r = mouse_expect_ack();
  101. if (r == -2) {
  102. mouse_cmd_write(cmd);
  103. r = mouse_expect_ack();
  104. }
  105. return r;
  106. }
  107. static int mouse_bit_at(int r, int c)
  108. {
  109. if (r < 0 || c < 0 || r >= (int)MOUSE_CUR_H || c >= (int)MOUSE_CUR_W)
  110. return 0;
  111. return (mouse_pointer_bits[(unsigned int)r] & (0x8000u >> c)) != 0;
  112. }
  113. static void mouse_draw_cursor(unsigned int x0, unsigned int y0)
  114. {
  115. unsigned int r, c;
  116. /* Draw only the cursor fill bitmap (no outline). */
  117. for (r = 0; r < MOUSE_CUR_H; r++) {
  118. for (c = 0; c < MOUSE_CUR_W; c++) {
  119. if (mouse_bit_at((int)r, (int)c))
  120. vga_put_pixel_rgb(x0 + c, y0 + r, 255, 255, 255);
  121. }
  122. }
  123. }
  124. static void mouse_clamp_ptr(long *nx, long *ny)
  125. {
  126. long maxx = (long)fbw - (long)MOUSE_CUR_W;
  127. long maxy = (long)fbh - (long)MOUSE_CUR_H;
  128. if (maxx < 0)
  129. maxx = 0;
  130. if (maxy < 0)
  131. maxy = 0;
  132. if (*nx < 0)
  133. *nx = 0;
  134. else if (*nx > maxx)
  135. *nx = maxx;
  136. if (*ny < 0)
  137. *ny = 0;
  138. else if (*ny > maxy)
  139. *ny = maxy;
  140. }
  141. static void mouse_cursor_setup(void)
  142. {
  143. mouse_cursor_flag = 0;
  144. if (!vga_has_vesa())
  145. return;
  146. fbw = vga_fb_width();
  147. fbh = vga_fb_height();
  148. if (fbw < MOUSE_CUR_W || fbh < MOUSE_CUR_H)
  149. return;
  150. /* Start cursor at a fixed visible spot (clamped to the framebuffer).
  151. Using a constant offset avoids any overlap with the terminal header/prompt. */
  152. cur_x = 100;
  153. cur_y = 100;
  154. {
  155. long nx = (long)cur_x;
  156. long ny = (long)cur_y;
  157. mouse_clamp_ptr(&nx, &ny);
  158. cur_x = (unsigned int)nx;
  159. cur_y = (unsigned int)ny;
  160. }
  161. vga_fb_read_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H, cur_saved);
  162. mouse_draw_cursor(cur_x, cur_y);
  163. mouse_cursor_flag = 1;
  164. }
  165. void mouse_init(void)
  166. {
  167. unsigned char id = 0;
  168. mouse_present_flag = 0;
  169. mouse_streaming_flag = 0;
  170. mouse_id = 0;
  171. mouse_cursor_flag = 0;
  172. pkt_idx = 0;
  173. enable_aux_port();
  174. if (mouse_send_cmd_ack(0xF5) != 0)
  175. return;
  176. mouse_cmd_write(0xF2);
  177. if (mouse_expect_ack() != 0)
  178. return;
  179. if (mouse_read_aux(&id, 500) != 0)
  180. return;
  181. mouse_present_flag = 1;
  182. mouse_id = id;
  183. mouse_cursor_setup();
  184. if (mouse_send_cmd_ack(0xF4) != 0)
  185. return;
  186. mouse_streaming_flag = 1;
  187. }
  188. void mouse_poll_update(void)
  189. {
  190. /* If video mode info wasn't ready at mouse_init time, try again. */
  191. if (mouse_present_flag && !mouse_cursor_flag)
  192. mouse_cursor_setup();
  193. if (!mouse_streaming_flag)
  194. return;
  195. for (;;) {
  196. unsigned char st = inb(PS2_STATUS);
  197. unsigned char b0;
  198. int dx, dy;
  199. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) !=
  200. (STATUS_OUT_FULL | STATUS_AUX_DATA))
  201. return;
  202. pkt[pkt_idx++] = inb(PS2_DATA);
  203. if (pkt_idx < 3)
  204. continue;
  205. pkt_idx = 0;
  206. b0 = pkt[0];
  207. dx = (int)(signed char)pkt[1];
  208. dy = (int)(signed char)pkt[2];
  209. if (b0 & 0x10)
  210. dx |= ~0xFF;
  211. if (b0 & 0x20)
  212. dy |= ~0xFF;
  213. if ((b0 & 0xC0) != 0)
  214. continue;
  215. if (mouse_cursor_flag) {
  216. long nx = (long)cur_x + dx;
  217. long ny = (long)cur_y - dy;
  218. mouse_clamp_ptr(&nx, &ny);
  219. if ((unsigned int)nx != cur_x || (unsigned int)ny != cur_y) {
  220. vga_fb_write_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H,
  221. cur_saved);
  222. cur_x = (unsigned int)nx;
  223. cur_y = (unsigned int)ny;
  224. vga_fb_read_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H,
  225. cur_saved);
  226. mouse_draw_cursor(cur_x, cur_y);
  227. }
  228. }
  229. }
  230. }
  231. int mouse_is_present(void)
  232. {
  233. return mouse_present_flag;
  234. }
  235. int mouse_streaming_enabled(void)
  236. {
  237. return mouse_streaming_flag;
  238. }
  239. unsigned int mouse_device_id(void)
  240. {
  241. return mouse_id;
  242. }
  243. int mouse_cursor_visible(void)
  244. {
  245. return mouse_cursor_flag;
  246. }