Max OS 0.3
Loading...
Searching...
No Matches
mouse.cpp
Go to the documentation of this file.
1
10
11using namespace MaxOS;
12using namespace MaxOS::common;
13using namespace MaxOS::drivers;
14using namespace MaxOS::drivers::peripherals;
15using namespace MaxOS::hardwarecommunication;
16
17MouseEventHandler::MouseEventHandler() = default;
18
26 switch(event->type) {
27
28 case MouseEvents::MOVE:
30 ((MouseMoveEvent*) event)->y);
31 break;
32
33 case MouseEvents::DOWN:
34 this->on_mouse_down_event(((MouseDownEvent*) event)->button);
35 break;
36
37 case MouseEvents::UP:
38 this->on_mouse_up_event(((MouseUpEvent*) event)->button);
39 break;
40
41 }
42
43 return event;
44}
45
54
63
73
74MouseEventHandler::~MouseEventHandler() = default;
75
80 : InterruptHandler(0x2C, 0xC, 0x28),
81 data_port(0x60),
82 command_port(0x64) {
83
84}
85
86MouseDriver::~MouseDriver() = default;
87
92
93 // Get the current state of the mouse
94 command_port.write(0x20);
95 uint8_t status = (data_port.read() | 2);
96
97 // Write the new state
98 command_port.write(0x60);
99 data_port.write(status);
100
101 // Tell the PIC to start listening to the mouse
102 command_port.write(0xAB);
103
104 // Activate the mouse
105 command_port.write(0xD4);
106 data_port.write(0xF4);
107 data_port.read();
108}
109
113void MouseDriver::handle_interrupt() {
114
115 // Check if there is data to handle
116 uint8_t status = command_port.read();
117 if(!(status & 0x20))
118 return;
119
120 // Read the data
121 m_buffer[m_offset] = data_port.read();
122 m_offset = (m_offset + 1) % 3;
123
124 // If the mouse data transmission is incomplete (3rd piece of data isn't through)
125 if(m_offset != 0)
126 return;
127
128 // If the mouse is moved (y-axis is inverted)
129 if(m_buffer[1] != 0 || m_buffer[2] != 0)
130 raise_event(new MouseMoveEvent(m_buffer[1], -m_buffer[2]));
131
132 // Handle button presses
133 for(int i = 0; i < 3; ++i) {
134
135 // This button is still in the same state
136 if((m_buffer[0] & (0x1 << i)) == (m_buttons & (0x1 << i)))
137 continue;
138
139 // Pass to handlers
140 bool is_pressed = (m_buttons & (0x1 << i)) != 0;
141 if(is_pressed)
142 raise_event(new MouseDownEvent(m_buffer[i]));
143 else
144 raise_event(new MouseUpEvent(m_buffer[i]));
145 }
146
147 m_buttons = m_buffer[0];
148}
149
156 return "Mouse";
157}
158
166 button(button) {
167
168}
169
170MouseUpEvent::~MouseUpEvent() = default;
171
178 : Event<MouseEvents>(MouseEvents::DOWN),
179 button(button) {
180}
181
182MouseDownEvent::~MouseDownEvent() = default;
183
191 : Event<MouseEvents>(MouseEvents::MOVE),
192 x(x),
193 y(y) {
194}
195
196MouseMoveEvent::~MouseMoveEvent() = default;
Vector< Event< MouseEvents > * > raise_event(Event< MouseEvents > *event)
Calls the on_event function of all the event m_handlers connected to the event manager and returns a ...
Used to store information about an event, has a type and a return value.
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Event that is triggered when a mouse button is pressed, holds the button that was pressed.
Definition mouse.h:50
MouseDownEvent(uint8_t)
Constructor for the MouseDownEvent class.
Definition mouse.cpp:177
void activate() final
activate the mouse
Definition mouse.cpp:91
MouseDriver()
Constructs a new MouseDriver object and registers it as an interrupt handler for interrupt 0x2C.
Definition mouse.cpp:79
string device_name() final
Get the name of the device.
Definition mouse.cpp:155
virtual void on_mouse_down_event(uint8_t button)
Called when the mouse is pressed.
Definition mouse.cpp:51
virtual void on_mouse_up_event(uint8_t button)
Called when the mouse is released.
Definition mouse.cpp:60
virtual void on_mouse_move_event(int8_t x, int8_t y)
Called when the mouse is moved.
Definition mouse.cpp:70
common::Event< MouseEvents > * on_event(common::Event< MouseEvents > *) override
Handles the triggered event and calls the appropriate function.
Definition mouse.cpp:25
Event that is triggered when the mouse moves, holds the x and y coordinates.
Definition mouse.h:38
MouseMoveEvent(int8_t x, int8_t y)
Constructor for the MouseMoveEvent class.
Definition mouse.cpp:190
Event that is triggered when a mouse button is released, holds the button that was released.
Definition mouse.h:61
MouseUpEvent(uint8_t)
Constructor for the MouseUpEvent class.
Definition mouse.cpp:164
Handles a certain interrupt number.
Definition interrupts.h:30
virtual uint8_t read()
read a byte from the port
Definition port.cpp:51
virtual void write(uint8_t data)
write a byte to the port
Definition port.cpp:41
Defines a MouseDriver for handling PS/2 mouse input and generating mouse events.
MouseEvents
The different types of mouse events that can be triggered.
Definition mouse.h:28