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 struct device_coords wheel;
100
101 struct {
102 /* The struct for the tablet mode switch device itself */
103 struct {
104 int state;
105 } sw;
106 /* The struct for other devices listening to the tablet mode
107 switch */
108 struct {
109 struct evdev_device *sw_device;
110 struct libinput_event_listener listener;
111 } other;
112 } tablet_mode;
113
114 /* Bitmask of pressed keys used to ignore initial release events from
115 * the kernel. */
116 unsigned long hw_key_mask[NLONGS(KEY_CNT)];
117 unsigned long last_hw_key_mask[NLONGS(KEY_CNT)];
118
119 enum evdev_event_type pending_event;
120
121 struct {
122 unsigned int button_code;
123 uint64_t button_time;
124 struct libinput_timer timer;
125 struct libinput_timer timer_short;
126 enum debounce_state state;
127 bool spurious_enabled;
128 } debounce;
129
130 struct {
131 enum switch_reliability reliability;
132
133 bool is_closed;
134 bool is_closed_client_state;
135
136 /* We allow multiple paired keyboards for the lid switch
137 * listener. Only one keyboard should exist, but that can
138 * have more than one event node. And it's a list because
139 * otherwise the test suite run fails too often.
140 */
141 struct list paired_keyboard_list;
142 } lid;
143
144 /* pen/touch arbitration has a delayed state,
145 * in_arbitration is what decides when to filter.
146 */
147 struct {
148 enum evdev_arbitration_state state;
149 bool in_arbitration;
150 struct device_coord_rect rect;
151 struct libinput_timer arbitration_timer;
152 } arbitration;
153 };
154
155 static inline struct fallback_dispatch*
fallback_dispatch(struct evdev_dispatch * dispatch)156 fallback_dispatch(struct evdev_dispatch *dispatch)
157 {
158 evdev_verify_dispatch_type(dispatch, DISPATCH_FALLBACK);
159
160 return container_of(dispatch, struct fallback_dispatch, base);
161 }
162
163 enum key_type {
164 KEY_TYPE_NONE,
165 KEY_TYPE_KEY,
166 KEY_TYPE_BUTTON,
167 };
168
169 static inline enum key_type
get_key_type(uint16_t code)170 get_key_type(uint16_t code)
171 {
172 switch (code) {
173 case BTN_TOOL_PEN:
174 case BTN_TOOL_RUBBER:
175 case BTN_TOOL_BRUSH:
176 case BTN_TOOL_PENCIL:
177 case BTN_TOOL_AIRBRUSH:
178 case BTN_TOOL_MOUSE:
179 case BTN_TOOL_LENS:
180 case BTN_TOOL_QUINTTAP:
181 case BTN_TOOL_DOUBLETAP:
182 case BTN_TOOL_TRIPLETAP:
183 case BTN_TOOL_QUADTAP:
184 case BTN_TOOL_FINGER:
185 case BTN_TOUCH:
186 return KEY_TYPE_NONE;
187 }
188
189 if (code >= KEY_ESC && code <= KEY_MICMUTE)
190 return KEY_TYPE_KEY;
191 if (code >= BTN_MISC && code <= BTN_GEAR_UP)
192 return KEY_TYPE_BUTTON;
193 if (code >= KEY_OK && code <= KEY_LIGHTS_TOGGLE)
194 return KEY_TYPE_KEY;
195 if (code >= BTN_DPAD_UP && code <= BTN_DPAD_RIGHT)
196 return KEY_TYPE_BUTTON;
197 if (code >= KEY_ALS_TOGGLE && code < BTN_TRIGGER_HAPPY)
198 return KEY_TYPE_KEY;
199 if (code >= BTN_TRIGGER_HAPPY && code <= BTN_TRIGGER_HAPPY40)
200 return KEY_TYPE_BUTTON;
201 return KEY_TYPE_NONE;
202 }
203
204 static inline void
hw_set_key_down(struct fallback_dispatch * dispatch,int code,int pressed)205 hw_set_key_down(struct fallback_dispatch *dispatch, int code, int pressed)
206 {
207 long_set_bit_state(dispatch->hw_key_mask, code, pressed);
208 }
209
210 static inline bool
hw_key_has_changed(struct fallback_dispatch * dispatch,int code)211 hw_key_has_changed(struct fallback_dispatch *dispatch, int code)
212 {
213 return long_bit_is_set(dispatch->hw_key_mask, code) !=
214 long_bit_is_set(dispatch->last_hw_key_mask, code);
215 }
216
217 static inline void
hw_key_update_last_state(struct fallback_dispatch * dispatch)218 hw_key_update_last_state(struct fallback_dispatch *dispatch)
219 {
220 static_assert(sizeof(dispatch->hw_key_mask) ==
221 sizeof(dispatch->last_hw_key_mask),
222 "Mismatching key mask size");
223
224 memcpy(dispatch->last_hw_key_mask,
225 dispatch->hw_key_mask,
226 sizeof(dispatch->hw_key_mask));
227 }
228
229 static inline bool
hw_is_key_down(struct fallback_dispatch * dispatch,int code)230 hw_is_key_down(struct fallback_dispatch *dispatch, int code)
231 {
232 return long_bit_is_set(dispatch->hw_key_mask, code);
233 }
234
235 static inline int
get_key_down_count(struct evdev_device * device,int code)236 get_key_down_count(struct evdev_device *device, int code)
237 {
238 return device->key_count[code];
239 }
240
241 void fallback_init_debounce(struct fallback_dispatch *dispatch);
242 void fallback_debounce_handle_state(struct fallback_dispatch *dispatch,
243 uint64_t time);
244
245 #endif
246