1 /*
2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2013 Jonas Ådahl
4 * Copyright © 2013-2017 Red Hat, Inc.
5 * Copyright © 2017 James Ye <jye836@gmail.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 */
26
27 #include "config.h"
28
29 #ifndef EVDEV_FALLBACK_H
30 #define EVDEV_FALLBACK_H
31
32 #include "evdev.h"
33
34 enum debounce_state {
35 DEBOUNCE_STATE_IS_UP = 100,
36 DEBOUNCE_STATE_IS_DOWN,
37 DEBOUNCE_STATE_IS_DOWN_WAITING,
38 DEBOUNCE_STATE_IS_UP_DELAYING,
39 DEBOUNCE_STATE_IS_UP_DELAYING_SPURIOUS,
40 DEBOUNCE_STATE_IS_UP_DETECTING_SPURIOUS,
41 DEBOUNCE_STATE_IS_DOWN_DETECTING_SPURIOUS,
42 DEBOUNCE_STATE_IS_UP_WAITING,
43 DEBOUNCE_STATE_IS_DOWN_DELAYING,
44
45 DEBOUNCE_STATE_DISABLED = 999,
46 };
47
48 enum mt_slot_state {
49 SLOT_STATE_NONE,
50 SLOT_STATE_BEGIN,
51 SLOT_STATE_UPDATE,
52 SLOT_STATE_END,
53 };
54
55 enum palm_state {
56 PALM_NONE,
57 PALM_NEW,
58 PALM_IS_PALM,
59 PALM_WAS_PALM, /* this touch sequence was a palm but isn't now */
60 };
61
62 struct mt_slot {
63 bool dirty;
64 enum mt_slot_state state;
65 int32_t seat_slot;
66 struct device_coords point;
67 struct device_coords hysteresis_center;
68 enum palm_state palm_state;
69 };
70
71 struct fallback_dispatch {
72 struct evdev_dispatch base;
73 struct evdev_device *device;
74
75 struct libinput_device_config_calibration calibration;
76
77 struct {
78 bool is_enabled;
79 int angle;
80 struct matrix matrix;
81 struct libinput_device_config_rotation config;
82 } rotation;
83
84 struct {
85 struct device_coords point;
86 int32_t seat_slot;
87 } abs;
88
89 struct {
90 int slot;
91 struct mt_slot *slots;
92 size_t slots_len;
93 bool want_hysteresis;
94 struct device_coords hysteresis_margin;
95 bool has_palm;
96 } mt;
97
98 struct device_coords rel;
99
100 struct {
101 struct device_coords lo_res;
102 struct device_coords hi_res;
103 bool emulate_hi_res_wheel;
104 bool is_inhibited;
105 bool hi_res_event_received;
106 } wheel;
107
108 struct {
109 /* The struct for the tablet mode switch device itself */
110 struct {
111 int state;
112 } sw;
113 /* The struct for other devices listening to the tablet mode
114 switch */
115 struct {
116 struct evdev_device *sw_device;
117 struct libinput_event_listener listener;
118 } other;
119 } tablet_mode;
120
121 /* Bitmask of pressed keys used to ignore initial release events from
122 * the kernel. */
123 unsigned long hw_key_mask[NLONGS(KEY_CNT)];
124 unsigned long last_hw_key_mask[NLONGS(KEY_CNT)];
125
126 enum evdev_event_type pending_event;
127
128 struct {
129 unsigned int button_code;
130 uint64_t button_time;
131 struct libinput_timer timer;
132 struct libinput_timer timer_short;
133 enum debounce_state state;
134 bool spurious_enabled;
135 } debounce;
136
137 struct {
138 enum switch_reliability reliability;
139
140 bool is_closed;
141 bool is_closed_client_state;
142
143 /* We allow multiple paired keyboards for the lid switch
144 * listener. Only one keyboard should exist, but that can
145 * have more than one event node. And it's a list because
146 * otherwise the test suite run fails too often.
147 */
148 struct list paired_keyboard_list;
149 } lid;
150
151 /* pen/touch arbitration has a delayed state,
152 * in_arbitration is what decides when to filter.
153 */
154 struct {
155 enum evdev_arbitration_state state;
156 bool in_arbitration;
157 struct device_coord_rect rect;
158 struct libinput_timer arbitration_timer;
159 } arbitration;
160 };
161
162 static inline struct fallback_dispatch*
fallback_dispatch(struct evdev_dispatch * dispatch)163 fallback_dispatch(struct evdev_dispatch *dispatch)
164 {
165 evdev_verify_dispatch_type(dispatch, DISPATCH_FALLBACK);
166
167 return container_of(dispatch, struct fallback_dispatch, base);
168 }
169
170 enum key_type {
171 KEY_TYPE_NONE,
172 KEY_TYPE_KEY,
173 KEY_TYPE_BUTTON,
174 };
175
176 static inline enum key_type
get_key_type(uint16_t code)177 get_key_type(uint16_t code)
178 {
179 switch (code) {
180 case BTN_TOOL_PEN:
181 case BTN_TOOL_RUBBER:
182 case BTN_TOOL_BRUSH:
183 case BTN_TOOL_PENCIL:
184 case BTN_TOOL_AIRBRUSH:
185 case BTN_TOOL_MOUSE:
186 case BTN_TOOL_LENS:
187 case BTN_TOOL_QUINTTAP:
188 case BTN_TOOL_DOUBLETAP:
189 case BTN_TOOL_TRIPLETAP:
190 case BTN_TOOL_QUADTAP:
191 case BTN_TOOL_FINGER:
192 case BTN_TOUCH:
193 return KEY_TYPE_NONE;
194 }
195
196 if (code >= KEY_ESC && code <= KEY_MICMUTE)
197 return KEY_TYPE_KEY;
198 if (code >= BTN_MISC && code <= BTN_GEAR_UP)
199 return KEY_TYPE_BUTTON;
200 if (code >= KEY_OK && code <= KEY_LIGHTS_TOGGLE)
201 return KEY_TYPE_KEY;
202 if (code >= BTN_DPAD_UP && code <= BTN_DPAD_RIGHT)
203 return KEY_TYPE_BUTTON;
204 if (code >= KEY_ALS_TOGGLE && code < BTN_TRIGGER_HAPPY)
205 return KEY_TYPE_KEY;
206 if (code >= BTN_TRIGGER_HAPPY && code <= BTN_TRIGGER_HAPPY40)
207 return KEY_TYPE_BUTTON;
208 return KEY_TYPE_NONE;
209 }
210
211 static inline void
hw_set_key_down(struct fallback_dispatch * dispatch,int code,int pressed)212 hw_set_key_down(struct fallback_dispatch *dispatch, int code, int pressed)
213 {
214 long_set_bit_state(dispatch->hw_key_mask, code, pressed);
215 }
216
217 static inline bool
hw_key_has_changed(struct fallback_dispatch * dispatch,int code)218 hw_key_has_changed(struct fallback_dispatch *dispatch, int code)
219 {
220 return long_bit_is_set(dispatch->hw_key_mask, code) !=
221 long_bit_is_set(dispatch->last_hw_key_mask, code);
222 }
223
224 static inline void
hw_key_update_last_state(struct fallback_dispatch * dispatch)225 hw_key_update_last_state(struct fallback_dispatch *dispatch)
226 {
227 static_assert(sizeof(dispatch->hw_key_mask) ==
228 sizeof(dispatch->last_hw_key_mask),
229 "Mismatching key mask size");
230
231 memcpy(dispatch->last_hw_key_mask,
232 dispatch->hw_key_mask,
233 sizeof(dispatch->hw_key_mask));
234 }
235
236 static inline bool
hw_is_key_down(struct fallback_dispatch * dispatch,int code)237 hw_is_key_down(struct fallback_dispatch *dispatch, int code)
238 {
239 return long_bit_is_set(dispatch->hw_key_mask, code);
240 }
241
242 static inline int
get_key_down_count(struct evdev_device * device,int code)243 get_key_down_count(struct evdev_device *device, int code)
244 {
245 return device->key_count[code];
246 }
247
248 void fallback_init_debounce(struct fallback_dispatch *dispatch);
249 void fallback_debounce_handle_state(struct fallback_dispatch *dispatch,
250 uint64_t time);
251 void
252 fallback_notify_physical_button(struct fallback_dispatch *dispatch,
253 struct evdev_device *device,
254 uint64_t time,
255 int button,
256 enum libinput_button_state state);
257
258 #endif
259