SayoriOS  0.3.3
mouse.c
См. документацию.
1 
10 #include <io/ports.h>
11 #include <sys/trigger.h>
12 #include <drv/input/mouse.h>
13 #include "io/screen.h"
14 #include "sys/isr.h"
15 #include "drv/ps2.h"
16 
17 uint8_t mouse_ready = 0;
18 
19 uint32_t mouse_x = 0;
20 uint32_t mouse_y = 0;
21 
22 
23 int32_t mouse_ox = 0;
24 int32_t mouse_oy = 0;
25 
26 uint32_t mouse_b1 = 0;
27 uint32_t mouse_b2 = 0;
28 uint32_t mouse_b3 = 0;
29 uint32_t mouse_b4 = 0;
30 uint32_t mouse_b5 = 0;
31 
32 int mouse_wheel = 0;
33 
37 typedef struct mouse_flags_byte {
38  unsigned int left_button : 1;
39  unsigned int right_button : 1;
40  unsigned int middle_button : 1;
41 
42  unsigned int always1 : 1;
43 
44  unsigned int x_sign : 1;
45  unsigned int y_sign : 1;
46 
47  unsigned int x_overflow : 1;
48  unsigned int y_overflow : 1;
50 
51 
55 struct dev_ps2m_mouse_packet {
56  int16_t movement_x;
57  int16_t movement_y;
58  uint8_t button_l;
59  uint8_t button_m;
60  uint8_t button_r;
61 } ps2m_buffer;
62 
68 bool isMouseInit(){
69  return mouse_ready==1?true:false;
70 }
71 
77 void mouse_parse_packet(const char *buf, uint8_t has_wheel, uint8_t has_5_buttons) {
78  mouse_flags_byte *mfb = (mouse_flags_byte*) (buf);
79  if (mfb->x_overflow || mfb->y_overflow || !mfb->always1) {
80  return;
81  }
82 
83  int offx = (int16_t) (0xff00 * mfb->x_sign) | buf[1];
84  int offy = (int16_t) (0xff00 * mfb->y_sign) | buf[2];
85  mouse_x += offx;
86  mouse_y -= offy;
87  mouse_b1 = mfb->left_button;
88  mouse_b2 = mfb->right_button;
89  mouse_b3 = mfb->middle_button;
90  ps2m_buffer.movement_x = offx;
91  ps2m_buffer.movement_y = offy;
92  ps2m_buffer.button_l = mouse_b1;
93  ps2m_buffer.button_r = mouse_b2;
94  ps2m_buffer.button_m = mouse_b3;
95 
96 
97  if (mouse_b1 || mouse_b2 || mouse_b3 || (mouse_x != mouse_ox) || (mouse_y != mouse_oy)){
98  CallTrigger(0x0003,(void*)mouse_b1,(void*)mouse_b2,(void*)mouse_b3,(void*)mouse_x,(void*)mouse_y);
99  mouse_ox = mouse_x;
100  mouse_oy = mouse_y;
101  }
102  //qemu_log("MPP: B1: %d | B2: %d | B3: %d | X:%d | Y:%d",mouse_b1,mouse_b2,mouse_b3,mouse_x,mouse_y);
103 
104  if (has_wheel) {
105  mouse_wheel += (char) ((!!(buf[3] & 0x8)) * 0xf8 | (buf[3] & 0x7));
106  if (has_5_buttons) {
107  mouse_b4 = !!(buf[3] & 0x20);
108  // parse buttons 4-5 (byte 3, bits 4-5)
109  }
110  }
111 }
112 
118 void mouse_handler(__attribute__((unused)) struct registers r) {
119  uint8_t status = inb(0x64);
120  if ((status & 1) == 0 || (status >> 5 & 1) == 0) {
121  return;
122  }
123 
124  static int recbyte = 0;
125  static char mousebuf[5];
126 
127  mousebuf[recbyte++] = inb(0x60);
128  if (recbyte == 3 /* + has_wheel */) {
129  recbyte = 0;
130 
131  mouse_parse_packet(mousebuf, 0, 0);
132 
133  // Bounds
134  if (mouse_x < 0) mouse_x = 0;
135  if (mouse_y < 0) mouse_y = 0;
136  if (mouse_x > (int) (getScreenWidth())) mouse_x = getScreenWidth();
137  if (mouse_y > (int) (getScreenHeight())) mouse_y = getScreenHeight(); //-10;
138  CallTrigger(0x0002,(void*)mouse_x,(void*)mouse_y,0,0,0);
139  }
140 }
141 
149 void mouse_wait(uint8_t a_type) {
150  uint32_t _time_out = 100;
151  if (a_type == 0) {
152  while (_time_out--) { //Data
153  if ((inb(0x64) & 1) == 1) {
154  return;
155  }
156  }
157  return;
158  } else {
159  while (_time_out--) { //Signal
160  if ((inb(0x64) & 2) == 0) {
161  return;
162  }
163  }
164  return;
165  }
166 }
167 
173 void mouse_write(uint8_t a_write) { //unsigned char
174  // Ожидаем возможности, пока можно будет отправить команду
175 // mouse_wait(1);
176  ps2_in_wait_until_empty();
177  // Говорим мышке, что мы отправляем команду
178  outb(PS2_STATE_REG, 0xD4);
179  // Ожидаем ответа
180 // mouse_wait(1);
181  ps2_in_wait_until_empty();
182  // Отправляем данные
183  outb(PS2_DATA_PORT, a_write);
184 }
185 
191 uint8_t mouse_read() {
192  // Получаем ответ от мыши
193 // mouse_wait(0);
194  ps2_out_wait_until_full();
195  return inb(PS2_DATA_PORT);
196 }
197 
204  uint8_t status = ps2_read_configuration_byte();
205 
206  ps2_write_configuration_byte(status | 2);
207 
208  // Скажите мыши использовать настройки по умолчанию
209  mouse_write(0xF6);
210  mouse_read(); // Acknowledge
211 
212  // // Включить мышь
213  mouse_write(0xF4);
214  mouse_read(); // Acknowledge
215 
216  // Установить координаты курсора в середине экрана
217  mouse_x = getScreenWidth() / 2;
218  mouse_y = getScreenHeight() / 2;
219 }
220 
221 void ps2_mouse_install_irq() {
222  register_interrupt_handler(IRQ12, &mouse_handler);
223  mouse_ready = 1;
224 }
225 
226 uint32_t mouse_get_x() {return mouse_x;}
227 uint32_t mouse_get_y() {return mouse_y;}
228 uint8_t mouse_get_b1() {return mouse_b1;}
229 uint8_t mouse_get_b2() {return mouse_b2;}
230 uint8_t mouse_get_b3() {return mouse_b3;}
231 uint8_t mouse_get_b4() {return mouse_b4;}
232 uint8_t mouse_get_b5() {return mouse_b5;}
233 
uint32_t mouse_b4
???
Definition: mouse.c:29
int32_t mouse_oy
Позиция мыши по Y (старое значение)
Definition: mouse.c:24
void mouse_wait(uint8_t a_type)
Ожидание ответа мыши
Definition: mouse.c:149
int mouse_wheel
После каждого чтения меняем на 0.
Definition: mouse.c:32
void mouse_install()
Установщик драйвера мыши
Definition: mouse.c:203
uint32_t mouse_b3
Средняя кнопка мыши
Definition: mouse.c:28
uint32_t mouse_b5
???
Definition: mouse.c:30
uint32_t mouse_x
Позиция мыши по X.
Definition: mouse.c:19
struct mouse_flags_byte __attribute__((packed))
Структура данных пакета от мыши
Definition: mouse.c:49
uint8_t mouse_ready
Готова ли мышь к работе
Definition: mouse.c:17
void mouse_write(uint8_t a_write)
Отправка данных для мыши
Definition: mouse.c:173
uint32_t mouse_b1
Левая кнопка мыши
Definition: mouse.c:26
int32_t mouse_ox
Позиция мыши по X (старое значение)
Definition: mouse.c:23
uint32_t mouse_y
Позиция мыши по Y.
Definition: mouse.c:20
void mouse_parse_packet(const char *buf, uint8_t has_wheel, uint8_t has_5_buttons)
Парсинг пакета мыши
Definition: mouse.c:77
uint32_t mouse_b2
Правая кнопка мыши
Definition: mouse.c:27
bool isMouseInit()
Инициализирована ли мышь?
Definition: mouse.c:68
uint8_t mouse_read()
Считывание данных с мыши
Definition: mouse.c:191
void mouse_handler(__attribute__((unused)) struct registers r)
Обработчик мыши
Definition: mouse.c:118
Структура данных пакета от мыши
Definition: mouse.c:37
void CallTrigger(int type, void *data1, void *data2, void *data3, void *data4, void *data5)
Функция для вызовов триггеров (Если самостоятельно надо вызвать триггер)
Definition: trigger.c:93