PatchworkOS  3984a1d
A non-POSIX operating system.
Loading...
Searching...
No Matches
ps2_mouse.c
Go to the documentation of this file.
1#include "ps2_mouse.h"
2
3#include <kernel/cpu/irq.h>
4#include <kernel/log/log.h>
5
6#include <stdlib.h>
7
9{
10 if (ps2->current.deltaX != 0)
11 {
12 mouse_move_x(mouse, ps2->current.deltaX);
13 }
14
15 if (ps2->current.deltaY != 0)
16 {
17 mouse_move_y(mouse, -ps2->current.deltaY);
18 }
19
20 if ((ps2->prev.flags & PS2_PACKET_BUTTON_LEFT) != (ps2->current.flags & PS2_PACKET_BUTTON_LEFT))
21 {
22 if (ps2->current.flags & PS2_PACKET_BUTTON_LEFT)
23 {
25 }
26 else
27 {
29 }
30 }
31
32 if ((ps2->prev.flags & PS2_PACKET_BUTTON_RIGHT) != (ps2->current.flags & PS2_PACKET_BUTTON_RIGHT))
33 {
34 if (ps2->current.flags & PS2_PACKET_BUTTON_RIGHT)
35 {
37 }
38 else
39 {
41 }
42 }
43
44 if ((ps2->prev.flags & PS2_PACKET_BUTTON_MIDDLE) != (ps2->current.flags & PS2_PACKET_BUTTON_MIDDLE))
45 {
46 if (ps2->current.flags & PS2_PACKET_BUTTON_MIDDLE)
47 {
49 }
50 else
51 {
53 }
54 }
55
56 ps2->prev = ps2->current;
57}
58
60{
61 ps2_mouse_t* mouse = data->private;
63 if (byte == ERR)
64 {
65 return;
66 }
67
68 switch (mouse->index)
69 {
71 {
72 mouse->current.flags = byte;
73
74 if (!(mouse->current.flags & PS2_PACKET_ALWAYS_ONE))
75 {
76 LOG_WARN("mouse packet out of sync flags=0x%02X\n", mouse->current.flags);
77 mouse->index = PS2_PACKET_FLAGS;
78 return;
79 }
80
81 if (mouse->current.flags & PS2_PACKET_X_OVERFLOW)
82 {
83 LOG_WARN("mouse packet x overflow flags=0x%02X\n", mouse->current.flags);
84 }
85
86 if (mouse->current.flags & PS2_PACKET_Y_OVERFLOW)
87 {
88 LOG_WARN("mouse packet y overflow flags=0x%02X\n", mouse->current.flags);
89 }
90
92 }
93 break;
95 {
96 mouse->current.deltaX = (int8_t)byte;
98 }
99 break;
101 {
102 mouse->current.deltaY = (int8_t)byte;
103 mouse->index = PS2_PACKET_FLAGS;
105 }
106 break;
107 }
108}
109
111{
112 ps2_mouse_t* mouse = calloc(1, sizeof(ps2_mouse_t));
113 if (mouse == NULL)
114 {
115 LOG_ERR("failed to allocate memory for PS/2 mouse data\n");
116 return ERR;
117 }
118
119 mouse->mouse = mouse_new(info->name);
120 if (mouse->mouse == NULL)
121 {
122 free(mouse);
123 LOG_ERR("failed to create PS/2 mouse\n");
124 return ERR;
125 }
126
127 mouse->index = 0;
128
130 {
131 mouse_free(mouse->mouse);
132 free(mouse);
133 LOG_ERR("failed to set default PS/2 mouse settings\n");
134 return ERR;
135 }
136
137 info->private = mouse;
138 return 0;
139}
140
142{
143 ps2_mouse_t* mouse = info->private;
144 if (mouse == NULL)
145 {
146 LOG_ERR("PS/2 mouse data is NULL during IRQ registration\n");
147 errno = EINVAL;
148 return ERR;
149 }
150
152 {
153 LOG_ERR("failed to register PS/2 mouse IRQ handler\n");
154 return ERR;
155 }
156
157 return 0;
158}
159
161{
162 if (info->private == NULL)
163 {
164 return;
165 }
166
167 ps2_mouse_t* mouse = info->private;
168
170 mouse_free(mouse->mouse);
171 free(mouse);
172
173 info->private = NULL;
174}
int64_t y
Definition main.c:153
static fd_t data
Definition dwm.c:21
static fd_t mouse
Definition dwm.c:24
void irq_handler_unregister(irq_func_t func, irq_virt_t virt)
Unregister an IRQ handler.
Definition irq.c:474
uint64_t irq_handler_register(irq_virt_t virt, irq_func_t func, void *private)
Register an IRQ handler for a virtual IRQ.
Definition irq.c:425
mouse_t * mouse_new(const char *name)
Allocate and initialize a mouse structure.
Definition mouse.c:144
void mouse_free(mouse_t *mouse)
Free and deinitialize a mouse structure.
Definition mouse.c:219
void mouse_press(mouse_t *mouse, uint32_t button)
Push a mouse button press event to the mouse event queue.
Definition mouse.c:246
void mouse_release(mouse_t *mouse, uint32_t button)
Push a mouse button release event to the mouse event queue.
Definition mouse.c:264
void mouse_move_x(mouse_t *mouse, int64_t delta)
Push a mouse movement in the X direction to the mouse event queue.
Definition mouse.c:282
void mouse_move_y(mouse_t *mouse, int64_t delta)
Push a mouse movement in the Y direction to the mouse event queue.
Definition mouse.c:300
#define LOG_ERR(format,...)
Definition log.h:93
#define LOG_WARN(format,...)
Definition log.h:92
#define EINVAL
Invalid argument.
Definition errno.h:142
#define errno
Error number variable.
Definition errno.h:27
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
void ps2_mouse_deinit(ps2_device_info_t *info)
Deinitialize a PS/2 mouse device.
Definition ps2_mouse.c:160
uint64_t ps2_mouse_irq_register(ps2_device_info_t *info)
Register the IRQ handler for a PS/2 mouse device.
Definition ps2_mouse.c:141
uint64_t ps2_mouse_init(ps2_device_info_t *info)
Initialize a PS/2 mouse device.
Definition ps2_mouse.c:110
@ PS2_PACKET_DELTA_Y
Definition ps2_mouse.h:53
@ PS2_PACKET_DELTA_X
Definition ps2_mouse.h:52
@ PS2_PACKET_FLAGS
Definition ps2_mouse.h:51
@ PS2_PACKET_X_OVERFLOW
Definition ps2_mouse.h:28
@ PS2_PACKET_Y_OVERFLOW
Definition ps2_mouse.h:29
@ PS2_PACKET_ALWAYS_ONE
Definition ps2_mouse.h:25
@ PS2_PACKET_BUTTON_RIGHT
Definition ps2_mouse.h:23
@ PS2_PACKET_BUTTON_MIDDLE
Definition ps2_mouse.h:24
@ PS2_PACKET_BUTTON_LEFT
Definition ps2_mouse.h:22
uint64_t ps2_read_no_wait(void)
Read from the PS/2 controllers data port without waiting.
Definition ps2.c:375
uint64_t ps2_device_cmd(ps2_device_t device, ps2_device_cmd_t command)
Send a command to a PS/2 device without reading response.
Definition ps2.c:488
@ PS2_DEV_CMD_SET_DEFAULTS
Definition ps2.h:121
static void ps2_mouse_irq(irq_func_data_t *data)
Definition ps2_mouse.c:59
static void ps2_mouse_handle_packet(mouse_t *mouse, ps2_mouse_t *ps2)
Definition ps2_mouse.c:8
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__INT8_TYPE__ int8_t
Definition stdint.h:10
_PUBLIC void * calloc(size_t nmemb, size_t size)
Definition calloc.c:6
_PUBLIC void free(void *ptr)
Definition free.c:11
Data passed to IRQ functions.
Definition irq.h:64
Mouse structure.
Definition mouse.h:71
PS/2 device information structure.
Definition ps2.h:202
irq_virt_t irq
IRQ assigned to the device by ACPI.
Definition ps2.h:206
ps2_device_t device
Device port.
Definition ps2.h:203
const char * name
Human-readable name of the device.
Definition ps2.h:205
void * private
Driver-specific private data.
Definition ps2.h:209
PS/2 mouse private data.
Definition ps2_mouse.h:62