use the -newos toolchain even if -elf is present.
[newos.git] / apps / window_server / PS2Mouse.cpp
blobe8877e2286a852821359a96199e4ff01b7dda13e
1 #include "PS2Mouse.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
6 struct mouse_data {
7 char status;
8 char delta_x;
9 char delta_y;
10 }; // mouse_data
12 PS2Mouse::PS2Mouse(int xmax, int ymax)
13 : fXPos(xmax / 2),
14 fYPos(ymax / 2),
15 fXMax(xmax),
16 fYMax(ymax)
18 fFd = open("/dev/ps2mouse", 0);
19 if(fFd < 0)
20 return;
24 void PS2Mouse::GetPos(int *out_x, int *out_y, int *out_buttons)
26 mouse_data data;
27 int err;
29 retry:
30 err = read(fFd, &data, sizeof(mouse_data));
31 if(err < 0) {
32 usleep(1000000);
33 goto retry;
36 fXPos += data.delta_x;
37 if(fXPos < 0)
38 fXPos = 0;
39 if(fXPos > fXMax)
40 fXPos = fXMax;
41 *out_x = fXPos;
43 fYPos += data.delta_y;
44 if(fYPos < 0)
45 fYPos = 0;
46 if(fYPos > fYMax)
47 fYPos = fYMax;
48 *out_y = fYMax - fYPos;
50 *out_buttons = 0;
51 *out_buttons |= (data.status & 0x80) ? MAIN_BUTTON : 0;
52 *out_buttons |= (data.status & 0x40) ? SECONDARY_BUTTON : 0;
53 *out_buttons |= (data.status & 0x20) ? TERTIARY_BUTTON : 0;
55 return;