summaryrefslogtreecommitdiff
path: root/payloads/libpayload/drivers/mouse_cursor.c
blob: 7006b4dee00d79fece0f53cd7f994ce3fba50a65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * This file is part of the libpayload project.
 *
 * Copyright (C) 2017 Patrick Rudolph <siro@das-labor.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <libpayload-config.h>
#include <libpayload.h>
#include <usb/usb.h>

static struct mouse_cursor_input_driver *cursor_in;

static int
mouse_cursor_driver_exists(const struct mouse_cursor_input_driver *const in)
{
	struct mouse_cursor_input_driver *head = cursor_in;

	while (head) {
		if (head == in)
			return 1;
		head = head->next;
	}

	return 0;
}

/** Add new mouse cursor input driver */
void mouse_cursor_add_input_driver(struct mouse_cursor_input_driver *const in)
{
	/* Check if this driver was already added to the console list */
	if (mouse_cursor_driver_exists(in))
		return;
	in->next = cursor_in;
	cursor_in = in;
}

/** Init enabled mouse cursor drivers */
void mouse_cursor_init(void)
{
#if CONFIG(LP_PC_MOUSE)
	i8042_mouse_init();
#endif
}

static u32 mouse_buttons;
static u8 mouse_acceleration = 0x10;
/* Fixed-point 1/256 units */
static int mouse_rel_x;
static int mouse_rel_y;
static int mouse_rel_z;
static u32 mouse_speed = 0x299;

/** Test for fast moving cursor */
static u8 mouse_cusor_is_fast(int x, int y)
{
	return (x * x + y * y) > (mouse_acceleration * mouse_acceleration);
}

/**
 * Poll for mouse data.
 *
 * Polls all drivers for new mouse data.
 * Applies accelerations to relative movement.
 * Logical ORs all buttons states.
 * Call often to prevent driver's queue overrun !
 */
void mouse_cursor_poll(void)
{
	struct mouse_cursor_input_driver *in;
	int rel_x, rel_y, rel_z;
	u32 buttons;

	mouse_buttons = 0;
	/* Iterate over all connected drivers */
	for (in = cursor_in; in != NULL; in = in->next)
		if (in->get_state) {
			in->get_state(&rel_x, &rel_y, &rel_z, &buttons);

			/* Accumulate relative movement */
			if (mouse_cusor_is_fast(rel_x, rel_y)) {
				/* Apply simple cursor acceleration. Default: x2.6 */
				mouse_rel_x += rel_x * mouse_speed;
				mouse_rel_y += rel_y * mouse_speed;
				mouse_rel_z += rel_z * mouse_speed;
			} else {
				mouse_rel_x += rel_x * 256;
				mouse_rel_y += rel_y * 256;
				mouse_rel_z += rel_z * 256;
			}

			/* Logic or all buttons */
			mouse_buttons |= buttons;
		}
}

/**
 * Get relative mouse movement.
 *
 * Returns relative mouse movement with acceleration
 * applied. The internal state will be cleared and stays
 * clear until one of the drivers provide motion input again.
 */
void mouse_cursor_get_rel(int *x, int *y, int *z)
{
	mouse_cursor_poll();

	if (x) {
		*x = mouse_rel_x / 256;
		mouse_rel_x = 0;
	}
	if (y) {
		*y = mouse_rel_y / 256;
		mouse_rel_y = 0;
	}
	if (z) {
		*z = mouse_rel_z / 256;
		mouse_rel_z = 0;
	}
}

/**
 * Get mouse button state.
 *
 * Returns the current button states.
 * There are up to 32 possible buttons,
 * but most devices only implement three buttons.
 */
u32 mouse_cursor_get_buttons(void)
{
	mouse_cursor_poll();

	return mouse_buttons;
}

/**
 * Set cursor speed.
 *
 * Sets the mouse cursor speed coefficient.
 * It is used in case the cursor is moving faster
 * than the mouse acceleration coefficient.
 */
void mouse_cursor_set_speed(u32 val)
{
	mouse_speed = val;
}

/**
 * Get cursor speed.
 *
 * Returns the internal mouse cursor speed in
 * 1/256th units.
 * Default: 0x299 ~ 2.6
 */
u32 mouse_cursor_get_speed(void)
{
	return mouse_speed;
}

/**
 * Set cursor acceleration.
 *
 * Sets the mouse cursor acceleration coefficient.
 * The acceleration is used to compare the raw relative
 * cursor movement against. If greater the raw values are
 * multiplied by mouse_speed to fasten the cursor.
 */
void mouse_cursor_set_acceleration(u8 val)
{
	mouse_acceleration = val;
}

/**
 * Get cursor acceleration.
 *
 * Returns the current cursor acceleration coefficient.
 * Default: 0x10
 */
u8 mouse_cursor_get_acceleration(void)
{
	return mouse_acceleration;
}