3 * Copyright (C) 2017 Patrick Rudolph <siro@das-labor.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <libpayload-config.h>
30 #include <libpayload.h>
33 static struct mouse_cursor_input_driver
*cursor_in
;
36 mouse_cursor_driver_exists(const struct mouse_cursor_input_driver
*const in
)
38 struct mouse_cursor_input_driver
*head
= cursor_in
;
49 /** Add new mouse cursor input driver */
50 void mouse_cursor_add_input_driver(struct mouse_cursor_input_driver
*const in
)
52 /* Check if this driver was already added to the console list */
53 if (mouse_cursor_driver_exists(in
))
59 /** Init enabled mouse cursor drivers */
60 void mouse_cursor_init(void)
62 #if CONFIG(LP_PC_MOUSE)
67 static u32 mouse_buttons
;
68 static u8 mouse_acceleration
= 0x10;
69 /* Fixed-point 1/256 units */
70 static int mouse_rel_x
;
71 static int mouse_rel_y
;
72 static int mouse_rel_z
;
73 static u32 mouse_speed
= 0x299;
75 /** Test for fast moving cursor */
76 static u8
mouse_cusor_is_fast(int x
, int y
)
78 return (x
* x
+ y
* y
) > (mouse_acceleration
* mouse_acceleration
);
82 * Poll for mouse data.
84 * Polls all drivers for new mouse data.
85 * Applies accelerations to relative movement.
86 * Logical ORs all buttons states.
87 * Call often to prevent driver's queue overrun !
89 void mouse_cursor_poll(void)
91 struct mouse_cursor_input_driver
*in
;
92 int rel_x
, rel_y
, rel_z
;
96 /* Iterate over all connected drivers */
97 for (in
= cursor_in
; in
!= NULL
; in
= in
->next
)
99 in
->get_state(&rel_x
, &rel_y
, &rel_z
, &buttons
);
101 /* Accumulate relative movement */
102 if (mouse_cusor_is_fast(rel_x
, rel_y
)) {
103 /* Apply simple cursor acceleration. Default: x2.6 */
104 mouse_rel_x
+= rel_x
* mouse_speed
;
105 mouse_rel_y
+= rel_y
* mouse_speed
;
106 mouse_rel_z
+= rel_z
* mouse_speed
;
108 mouse_rel_x
+= rel_x
* 256;
109 mouse_rel_y
+= rel_y
* 256;
110 mouse_rel_z
+= rel_z
* 256;
113 /* Logic or all buttons */
114 mouse_buttons
|= buttons
;
119 * Get relative mouse movement.
121 * Returns relative mouse movement with acceleration
122 * applied. The internal state will be cleared and stays
123 * clear until one of the drivers provide motion input again.
125 void mouse_cursor_get_rel(int *x
, int *y
, int *z
)
130 *x
= mouse_rel_x
/ 256;
134 *y
= mouse_rel_y
/ 256;
138 *z
= mouse_rel_z
/ 256;
144 * Get mouse button state.
146 * Returns the current button states.
147 * There are up to 32 possible buttons,
148 * but most devices only implement three buttons.
150 u32
mouse_cursor_get_buttons(void)
154 return mouse_buttons
;
160 * Sets the mouse cursor speed coefficient.
161 * It is used in case the cursor is moving faster
162 * than the mouse acceleration coefficient.
164 void mouse_cursor_set_speed(u32 val
)
172 * Returns the internal mouse cursor speed in
174 * Default: 0x299 ~ 2.6
176 u32
mouse_cursor_get_speed(void)
182 * Set cursor acceleration.
184 * Sets the mouse cursor acceleration coefficient.
185 * The acceleration is used to compare the raw relative
186 * cursor movement against. If greater the raw values are
187 * multiplied by mouse_speed to fasten the cursor.
189 void mouse_cursor_set_acceleration(u8 val
)
191 mouse_acceleration
= val
;
195 * Get cursor acceleration.
197 * Returns the current cursor acceleration coefficient.
200 u8
mouse_cursor_get_acceleration(void)
202 return mouse_acceleration
;