| 1 |
/* |
| 2 |
* Copyright © 2014 Red Hat, Inc. |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice (including the next |
| 12 |
* paragraph) shall be included in all copies or substantial portions of the |
| 13 |
* Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 |
* DEALINGS IN THE SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <config.h> |
| 25 |
|
| 26 |
#include <errno.h> |
| 27 |
#include <fcntl.h> |
| 28 |
#include <libinput-util.h> |
| 29 |
#include <libinput.h> |
| 30 |
#include <stdarg.h> |
| 31 |
#include <unistd.h> |
| 32 |
|
| 33 |
#include "libinput-util.h" |
| 34 |
#include "litest.h" |
| 35 |
|
| 36 |
static int |
| 37 |
open_restricted(const char *path, int flags, void *data) |
| 38 |
{ |
| 39 |
int fd = open(path, flags); |
| 40 |
return fd < 0 ? -errno : fd; |
| 41 |
} |
| 42 |
static void |
| 43 |
close_restricted(int fd, void *data) |
| 44 |
{ |
| 45 |
close(fd); |
| 46 |
} |
| 47 |
|
| 48 |
static const struct libinput_interface simple_interface = { |
| 49 |
.open_restricted = open_restricted, |
| 50 |
.close_restricted = close_restricted, |
| 51 |
}; |
| 52 |
|
| 53 |
static struct libevdev_uinput * |
| 54 |
create_simple_test_device(const char *name, ...) |
| 55 |
{ |
| 56 |
va_list args; |
| 57 |
struct libevdev_uinput *uinput; |
| 58 |
struct libevdev *evdev; |
| 59 |
unsigned int type, code; |
| 60 |
int rc; |
| 61 |
struct input_absinfo abs = { |
| 62 |
.value = -1, |
| 63 |
.minimum = 0, |
| 64 |
.maximum = 100, |
| 65 |
.fuzz = 0, |
| 66 |
.flat = 0, |
| 67 |
.resolution = 100, |
| 68 |
}; |
| 69 |
|
| 70 |
evdev = libevdev_new(); |
| 71 |
litest_assert_notnull(evdev); |
| 72 |
libevdev_set_name(evdev, name); |
| 73 |
|
| 74 |
va_start(args, name); |
| 75 |
|
| 76 |
while ((type = va_arg(args, unsigned int)) != (unsigned int)-1 && |
| 77 |
(code = va_arg(args, unsigned int)) != (unsigned int)-1) { |
| 78 |
const struct input_absinfo *a = NULL; |
| 79 |
if (type == EV_ABS) |
| 80 |
a = &abs; |
| 81 |
libevdev_enable_event_code(evdev, type, code, a); |
| 82 |
} |
| 83 |
|
| 84 |
va_end(args); |
| 85 |
|
| 86 |
rc = libevdev_uinput_create_from_device(evdev, |
| 87 |
LIBEVDEV_UINPUT_OPEN_MANAGED, |
| 88 |
&uinput); |
| 89 |
litest_assert_neg_errno_success(rc); |
| 90 |
libevdev_free(evdev); |
| 91 |
|
| 92 |
return uinput; |
| 93 |
} |
| 94 |
|
| 95 |
START_TEST(event_conversion_device_notify) |
| 96 |
{ |
| 97 |
struct libevdev_uinput *uinput; |
| 98 |
struct libinput_event *event; |
| 99 |
int device_added = 0, device_removed = 0; |
| 100 |
|
| 101 |
/* clang-format off */ |
| 102 |
uinput = create_simple_test_device("litest test device", |
| 103 |
EV_REL, REL_X, |
| 104 |
EV_REL, REL_Y, |
| 105 |
EV_KEY, BTN_LEFT, |
| 106 |
EV_KEY, BTN_MIDDLE, |
| 107 |
EV_KEY, BTN_LEFT, |
| 108 |
-1, -1); |
| 109 |
/* clang-format on */ |
| 110 |
_litest_context_destroy_ struct libinput *li = litest_create_context(); |
| 111 |
litest_restore_log_handler(li); /* use the default litest handler */ |
| 112 |
libinput_path_add_device(li, libevdev_uinput_get_devnode(uinput)); |
| 113 |
|
| 114 |
litest_dispatch(li); |
| 115 |
libinput_suspend(li); |
| 116 |
libinput_resume(li); |
| 117 |
|
| 118 |
while ((event = libinput_get_event(li))) { |
| 119 |
enum libinput_event_type type; |
| 120 |
type = libinput_event_get_type(event); |
| 121 |
|
| 122 |
if (type == LIBINPUT_EVENT_DEVICE_ADDED || |
| 123 |
type == LIBINPUT_EVENT_DEVICE_REMOVED) { |
| 124 |
struct libinput_event_device_notify *dn; |
| 125 |
struct libinput_event *base; |
| 126 |
dn = libinput_event_get_device_notify_event(event); |
| 127 |
base = libinput_event_device_notify_get_base_event(dn); |
| 128 |
litest_assert(event == base); |
| 129 |
|
| 130 |
if (type == LIBINPUT_EVENT_DEVICE_ADDED) |
| 131 |
device_added++; |
| 132 |
else if (type == LIBINPUT_EVENT_DEVICE_REMOVED) |
| 133 |
device_removed++; |
| 134 |
|
| 135 |
litest_disable_log_handler(li); |
| 136 |
litest_assert(libinput_event_get_pointer_event(event) == NULL); |
| 137 |
litest_assert(libinput_event_get_keyboard_event(event) == NULL); |
| 138 |
litest_assert(libinput_event_get_touch_event(event) == NULL); |
| 139 |
litest_assert(libinput_event_get_gesture_event(event) == NULL); |
| 140 |
litest_assert(libinput_event_get_tablet_tool_event(event) == |
| 141 |
NULL); |
| 142 |
litest_assert(libinput_event_get_tablet_pad_event(event) == |
| 143 |
NULL); |
| 144 |
litest_assert(libinput_event_get_switch_event(event) == NULL); |
| 145 |
litest_restore_log_handler(li); |
| 146 |
} |
| 147 |
|
| 148 |
libinput_event_destroy(event); |
| 149 |
} |
| 150 |
|
| 151 |
libevdev_uinput_destroy(uinput); |
| 152 |
|
| 153 |
litest_assert_int_gt(device_added, 0); |
| 154 |
litest_assert_int_gt(device_removed, 0); |
| 155 |
} |
| 156 |
END_TEST |
| 157 |
|
| 158 |
START_TEST(event_conversion_pointer) |
| 159 |
{ |
| 160 |
struct litest_device *dev = litest_current_device(); |
| 161 |
struct libinput *li = dev->libinput; |
| 162 |
struct libinput_event *event; |
| 163 |
int motion = 0, button = 0; |
| 164 |
|
| 165 |
/* Queue at least two relative motion events as the first one may |
| 166 |
* be absorbed by the pointer acceleration filter. */ |
| 167 |
litest_event(dev, EV_REL, REL_X, -1); |
| 168 |
litest_event(dev, EV_REL, REL_Y, -1); |
| 169 |
litest_event(dev, EV_SYN, SYN_REPORT, 0); |
| 170 |
litest_event(dev, EV_REL, REL_X, -1); |
| 171 |
litest_event(dev, EV_REL, REL_Y, -1); |
| 172 |
litest_event(dev, EV_KEY, BTN_LEFT, 1); |
| 173 |
litest_event(dev, EV_SYN, SYN_REPORT, 0); |
| 174 |
|
| 175 |
litest_dispatch(li); |
| 176 |
|
| 177 |
while ((event = libinput_get_event(li))) { |
| 178 |
enum libinput_event_type type; |
| 179 |
type = libinput_event_get_type(event); |
| 180 |
|
| 181 |
if (type == LIBINPUT_EVENT_POINTER_MOTION || |
| 182 |
type == LIBINPUT_EVENT_POINTER_BUTTON) { |
| 183 |
struct libinput_event_pointer *p; |
| 184 |
struct libinput_event *base; |
| 185 |
p = libinput_event_get_pointer_event(event); |
| 186 |
base = libinput_event_pointer_get_base_event(p); |
| 187 |
litest_assert(event == base); |
| 188 |
|
| 189 |
if (type == LIBINPUT_EVENT_POINTER_MOTION) |
| 190 |
motion++; |
| 191 |
else if (type == LIBINPUT_EVENT_POINTER_BUTTON) |
| 192 |
button++; |
| 193 |
|
| 194 |
litest_disable_log_handler(li); |
| 195 |
litest_assert(libinput_event_get_device_notify_event(event) == |
| 196 |
NULL); |
| 197 |
litest_assert(libinput_event_get_keyboard_event(event) == NULL); |
| 198 |
litest_assert(libinput_event_get_touch_event(event) == NULL); |
| 199 |
litest_assert(libinput_event_get_gesture_event(event) == NULL); |
| 200 |
litest_assert(libinput_event_get_tablet_tool_event(event) == |
| 201 |
NULL); |
| 202 |
litest_assert(libinput_event_get_tablet_pad_event(event) == |
| 203 |
NULL); |
| 204 |
litest_assert(libinput_event_get_switch_event(event) == NULL); |
| 205 |
litest_restore_log_handler(li); |
| 206 |
} |
| 207 |
libinput_event_destroy(event); |
| 208 |
} |
| 209 |
|
| 210 |
litest_assert_int_gt(motion, 0); |
| 211 |
litest_assert_int_gt(button, 0); |
| 212 |
} |
| 213 |
END_TEST |
| 214 |
|
| 215 |
START_TEST(event_conversion_pointer_abs) |
| 216 |
{ |
| 217 |
struct litest_device *dev = litest_current_device(); |
| 218 |
struct libinput *li = dev->libinput; |
| 219 |
struct libinput_event *event; |
| 220 |
int motion = 0, button = 0; |
| 221 |
|
| 222 |
litest_event(dev, EV_ABS, ABS_X, 10); |
| 223 |
litest_event(dev, EV_ABS, ABS_Y, 50); |
| 224 |
litest_event(dev, EV_KEY, BTN_LEFT, 1); |
| 225 |
litest_event(dev, EV_SYN, SYN_REPORT, 0); |
| 226 |
litest_event(dev, EV_ABS, ABS_X, 30); |
| 227 |
litest_event(dev, EV_ABS, ABS_Y, 30); |
| 228 |
litest_event(dev, EV_SYN, SYN_REPORT, 0); |
| 229 |
|
| 230 |
litest_dispatch(li); |
| 231 |
|
| 232 |
while ((event = libinput_get_event(li))) { |
| 233 |
enum libinput_event_type type; |
| 234 |
type = libinput_event_get_type(event); |
| 235 |
|
| 236 |
if (type == LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE || |
| 237 |
type == LIBINPUT_EVENT_POINTER_BUTTON) { |
| 238 |
struct libinput_event_pointer *p; |
| 239 |
struct libinput_event *base; |
| 240 |
p = libinput_event_get_pointer_event(event); |
| 241 |
base = libinput_event_pointer_get_base_event(p); |
| 242 |
litest_assert(event == base); |
| 243 |
|
| 244 |
if (type == LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE) |
| 245 |
motion++; |
| 246 |
else if (type == LIBINPUT_EVENT_POINTER_BUTTON) |
| 247 |
button++; |
| 248 |
|
| 249 |
litest_disable_log_handler(li); |
| 250 |
litest_assert(libinput_event_get_device_notify_event(event) == |
| 251 |
NULL); |
| 252 |
litest_assert(libinput_event_get_keyboard_event(event) == NULL); |
| 253 |
litest_assert(libinput_event_get_touch_event(event) == NULL); |
| 254 |
litest_assert(libinput_event_get_gesture_event(event) == NULL); |
| 255 |
litest_assert(libinput_event_get_tablet_tool_event(event) == |
| 256 |
NULL); |
| 257 |
litest_assert(libinput_event_get_tablet_pad_event(event) == |
| 258 |
NULL); |
| 259 |
litest_assert(libinput_event_get_switch_event(event) == NULL); |
| 260 |
litest_restore_log_handler(li); |
| 261 |
} |
| 262 |
libinput_event_destroy(event); |
| 263 |
} |
| 264 |
|
| 265 |
litest_assert_int_gt(motion, 0); |
| 266 |
litest_assert_int_gt(button, 0); |
| 267 |
} |
| 268 |
END_TEST |
| 269 |
|
| 270 |
START_TEST(event_conversion_key) |
| 271 |
{ |
| 272 |
struct litest_device *dev = litest_current_device(); |
| 273 |
struct libinput *li = dev->libinput; |
| 274 |
struct libinput_event *event; |
| 275 |
int key = 0; |
| 276 |
|
| 277 |
litest_event(dev, EV_KEY, KEY_A, 1); |
| 278 |
litest_event(dev, EV_SYN, SYN_REPORT, 0); |
| 279 |
litest_event(dev, EV_KEY, KEY_A, 0); |
| 280 |
litest_event(dev, EV_SYN, SYN_REPORT, 0); |
| 281 |
|
| 282 |
litest_dispatch(li); |
| 283 |
|
| 284 |
while ((event = libinput_get_event(li))) { |
| 285 |
enum libinput_event_type type; |
| 286 |
type = libinput_event_get_type(event); |
| 287 |
|
| 288 |
if (type == LIBINPUT_EVENT_KEYBOARD_KEY) { |
| 289 |
struct libinput_event_keyboard *k; |
| 290 |
struct libinput_event *base; |
| 291 |
k = libinput_event_get_keyboard_event(event); |
| 292 |
base = libinput_event_keyboard_get_base_event(k); |
| 293 |
litest_assert(event == base); |
| 294 |
|
| 295 |
key++; |
| 296 |
|
| 297 |
litest_disable_log_handler(li); |
| 298 |
litest_assert(libinput_event_get_device_notify_event(event) == |
| 299 |
NULL); |
| 300 |
litest_assert(libinput_event_get_pointer_event(event) == NULL); |
| 301 |
litest_assert(libinput_event_get_touch_event(event) == NULL); |
| 302 |
litest_assert(libinput_event_get_gesture_event(event) == NULL); |
| 303 |
litest_assert(libinput_event_get_tablet_tool_event(event) == |
| 304 |
NULL); |
| 305 |
litest_assert(libinput_event_get_tablet_pad_event(event) == |
| 306 |
NULL); |
| 307 |
litest_assert(libinput_event_get_switch_event(event) == NULL); |
| 308 |
litest_restore_log_handler(li); |
| 309 |
} |
| 310 |
libinput_event_destroy(event); |
| 311 |
} |
| 312 |
|
| 313 |
litest_assert_int_gt(key, 0); |
| 314 |
} |
| 315 |
END_TEST |
| 316 |
|
| 317 |
START_TEST(event_conversion_touch) |
| 318 |
{ |
| 319 |
struct litest_device *dev = litest_current_device(); |
| 320 |
struct libinput *li = dev->libinput; |
| 321 |
struct libinput_event *event; |
| 322 |
int touch = 0; |
| 323 |
|
| 324 |
litest_dispatch(li); |
| 325 |
|
| 326 |
litest_event(dev, EV_KEY, BTN_TOOL_FINGER, 1); |
| 327 |
litest_event(dev, EV_KEY, BTN_TOUCH, 1); |
| 328 |
litest_event(dev, EV_ABS, ABS_X, 10); |
| 329 |
litest_event(dev, EV_ABS, ABS_Y, 10); |
| 330 |
litest_event(dev, EV_ABS, ABS_MT_SLOT, 0); |
| 331 |
litest_event(dev, EV_ABS, ABS_MT_TRACKING_ID, 1); |
| 332 |
litest_event(dev, EV_ABS, ABS_MT_POSITION_X, 10); |
| 333 |
litest_event(dev, EV_ABS, ABS_MT_POSITION_Y, 10); |
| 334 |
litest_event(dev, EV_SYN, SYN_REPORT, 0); |
| 335 |
|
| 336 |
litest_dispatch(li); |
| 337 |
|
| 338 |
while ((event = libinput_get_event(li))) { |
| 339 |
enum libinput_event_type type; |
| 340 |
type = libinput_event_get_type(event); |
| 341 |
|
| 342 |
if (type >= LIBINPUT_EVENT_TOUCH_DOWN && |
| 343 |
type <= LIBINPUT_EVENT_TOUCH_FRAME) { |
| 344 |
struct libinput_event_touch *t; |
| 345 |
struct libinput_event *base; |
| 346 |
t = libinput_event_get_touch_event(event); |
| 347 |
base = libinput_event_touch_get_base_event(t); |
| 348 |
litest_assert(event == base); |
| 349 |
|
| 350 |
touch++; |
| 351 |
|
| 352 |
litest_disable_log_handler(li); |
| 353 |
litest_assert(libinput_event_get_device_notify_event(event) == |
| 354 |
NULL); |
| 355 |
litest_assert(libinput_event_get_pointer_event(event) == NULL); |
| 356 |
litest_assert(libinput_event_get_keyboard_event(event) == NULL); |
| 357 |
litest_assert(libinput_event_get_gesture_event(event) == NULL); |
| 358 |
litest_assert(libinput_event_get_tablet_tool_event(event) == |
| 359 |
NULL); |
| 360 |
litest_assert(libinput_event_get_tablet_pad_event(event) == |
| 361 |
NULL); |
| 362 |
litest_assert(libinput_event_get_switch_event(event) == NULL); |
| 363 |
litest_restore_log_handler(li); |
| 364 |
} |
| 365 |
libinput_event_destroy(event); |
| 366 |
} |
| 367 |
|
| 368 |
litest_assert_int_gt(touch, 0); |
| 369 |
} |
| 370 |
END_TEST |
| 371 |
|
| 372 |
START_TEST(event_conversion_gesture) |
| 373 |
{ |
| 374 |
struct litest_device *dev = litest_current_device(); |
| 375 |
struct libinput *li = dev->libinput; |
| 376 |
struct libinput_event *event; |
| 377 |
int gestures = 0; |
| 378 |
int i; |
| 379 |
|
| 380 |
litest_dispatch(li); |
| 381 |
|
| 382 |
litest_touch_down(dev, 0, 70, 30); |
| 383 |
litest_touch_down(dev, 1, 30, 70); |
| 384 |
litest_timeout_gesture_hold(li); |
| 385 |
|
| 386 |
for (i = 0; i < 8; i++) { |
| 387 |
litest_push_event_frame(dev); |
| 388 |
litest_touch_move(dev, 0, 70 - i * 5, 30 + i * 5); |
| 389 |
litest_touch_move(dev, 1, 30 + i * 5, 70 - i * 5); |
| 390 |
litest_pop_event_frame(dev); |
| 391 |
litest_dispatch(li); |
| 392 |
} |
| 393 |
|
| 394 |
while ((event = libinput_get_event(li))) { |
| 395 |
enum libinput_event_type type; |
| 396 |
type = libinput_event_get_type(event); |
| 397 |
|
| 398 |
if (type >= LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN && |
| 399 |
type <= LIBINPUT_EVENT_GESTURE_HOLD_END) { |
| 400 |
struct libinput_event_gesture *g; |
| 401 |
struct libinput_event *base; |
| 402 |
g = libinput_event_get_gesture_event(event); |
| 403 |
base = libinput_event_gesture_get_base_event(g); |
| 404 |
litest_assert(event == base); |
| 405 |
|
| 406 |
gestures++; |
| 407 |
|
| 408 |
litest_disable_log_handler(li); |
| 409 |
litest_assert(libinput_event_get_device_notify_event(event) == |
| 410 |
NULL); |
| 411 |
litest_assert(libinput_event_get_pointer_event(event) == NULL); |
| 412 |
litest_assert(libinput_event_get_keyboard_event(event) == NULL); |
| 413 |
litest_assert(libinput_event_get_touch_event(event) == NULL); |
| 414 |
litest_assert(libinput_event_get_tablet_pad_event(event) == |
| 415 |
NULL); |
| 416 |
litest_assert(libinput_event_get_switch_event(event) == NULL); |
| 417 |
litest_restore_log_handler(li); |
| 418 |
} |
| 419 |
libinput_event_destroy(event); |
| 420 |
} |
| 421 |
|
| 422 |
litest_assert_int_gt(gestures, 0); |
| 423 |
} |
| 424 |
END_TEST |
| 425 |
|
| 426 |
START_TEST(event_conversion_tablet) |
| 427 |
{ |
| 428 |
struct litest_device *dev = litest_current_device(); |
| 429 |
struct libinput *li = dev->libinput; |
| 430 |
struct libinput_event *event; |
| 431 |
int events = 0; |
| 432 |
struct axis_replacement axes[] = { |
| 433 |
{ ABS_DISTANCE, 10 }, |
| 434 |
{ -1, -1 }, |
| 435 |
}; |
| 436 |
|
| 437 |
litest_tablet_proximity_in(dev, 50, 50, axes); |
| 438 |
litest_tablet_motion(dev, 60, 50, axes); |
| 439 |
litest_button_click(dev, BTN_STYLUS, true); |
| 440 |
litest_button_click(dev, BTN_STYLUS, false); |
| 441 |
|
| 442 |
litest_dispatch(li); |
| 443 |
|
| 444 |
while ((event = libinput_get_event(li))) { |
| 445 |
enum libinput_event_type type; |
| 446 |
type = libinput_event_get_type(event); |
| 447 |
|
| 448 |
if (type >= LIBINPUT_EVENT_TABLET_TOOL_AXIS && |
| 449 |
type <= LIBINPUT_EVENT_TABLET_TOOL_BUTTON) { |
| 450 |
struct libinput_event_tablet_tool *t; |
| 451 |
struct libinput_event *base; |
| 452 |
t = libinput_event_get_tablet_tool_event(event); |
| 453 |
base = libinput_event_tablet_tool_get_base_event(t); |
| 454 |
litest_assert(event == base); |
| 455 |
|
| 456 |
events++; |
| 457 |
|
| 458 |
litest_disable_log_handler(li); |
| 459 |
litest_assert(libinput_event_get_device_notify_event(event) == |
| 460 |
NULL); |
| 461 |
litest_assert(libinput_event_get_pointer_event(event) == NULL); |
| 462 |
litest_assert(libinput_event_get_keyboard_event(event) == NULL); |
| 463 |
litest_assert(libinput_event_get_touch_event(event) == NULL); |
| 464 |
litest_assert(libinput_event_get_tablet_pad_event(event) == |
| 465 |
NULL); |
| 466 |
litest_assert(libinput_event_get_switch_event(event) == NULL); |
| 467 |
litest_restore_log_handler(li); |
| 468 |
} |
| 469 |
libinput_event_destroy(event); |
| 470 |
} |
| 471 |
|
| 472 |
litest_assert_int_gt(events, 0); |
| 473 |
} |
| 474 |
END_TEST |
| 475 |
|
| 476 |
START_TEST(event_conversion_tablet_pad) |
| 477 |
{ |
| 478 |
struct litest_device *dev = litest_current_device(); |
| 479 |
struct libinput *li = dev->libinput; |
| 480 |
struct libinput_event *event; |
| 481 |
int events = 0; |
| 482 |
|
| 483 |
litest_button_click(dev, BTN_0, true); |
| 484 |
litest_pad_ring_start(dev, 10); |
| 485 |
litest_pad_ring_end(dev); |
| 486 |
|
| 487 |
litest_dispatch(li); |
| 488 |
|
| 489 |
while ((event = libinput_get_event(li))) { |
| 490 |
enum libinput_event_type type; |
| 491 |
type = libinput_event_get_type(event); |
| 492 |
|
| 493 |
if (type >= LIBINPUT_EVENT_TABLET_PAD_BUTTON && |
| 494 |
type <= LIBINPUT_EVENT_TABLET_PAD_STRIP) { |
| 495 |
struct libinput_event_tablet_pad *p; |
| 496 |
struct libinput_event *base; |
| 497 |
|
| 498 |
p = libinput_event_get_tablet_pad_event(event); |
| 499 |
base = libinput_event_tablet_pad_get_base_event(p); |
| 500 |
litest_assert(event == base); |
| 501 |
|
| 502 |
events++; |
| 503 |
|
| 504 |
litest_disable_log_handler(li); |
| 505 |
litest_assert(libinput_event_get_device_notify_event(event) == |
| 506 |
NULL); |
| 507 |
litest_assert(libinput_event_get_pointer_event(event) == NULL); |
| 508 |
litest_assert(libinput_event_get_keyboard_event(event) == NULL); |
| 509 |
litest_assert(libinput_event_get_touch_event(event) == NULL); |
| 510 |
litest_assert(libinput_event_get_tablet_tool_event(event) == |
| 511 |
NULL); |
| 512 |
litest_assert(libinput_event_get_switch_event(event) == NULL); |
| 513 |
litest_restore_log_handler(li); |
| 514 |
} |
| 515 |
libinput_event_destroy(event); |
| 516 |
} |
| 517 |
|
| 518 |
litest_assert_int_gt(events, 0); |
| 519 |
} |
| 520 |
END_TEST |
| 521 |
|
| 522 |
START_TEST(event_conversion_switch) |
| 523 |
{ |
| 524 |
struct litest_device *dev = litest_current_device(); |
| 525 |
struct libinput *li = dev->libinput; |
| 526 |
struct libinput_event *event; |
| 527 |
int sw = 0; |
| 528 |
|
| 529 |
litest_switch_action(dev, LIBINPUT_SWITCH_LID, LIBINPUT_SWITCH_STATE_ON); |
| 530 |
litest_switch_action(dev, LIBINPUT_SWITCH_LID, LIBINPUT_SWITCH_STATE_OFF); |
| 531 |
litest_switch_action(dev, |
| 532 |
LIBINPUT_SWITCH_KEYPAD_SLIDE, |
| 533 |
LIBINPUT_SWITCH_STATE_ON); |
| 534 |
litest_switch_action(dev, |
| 535 |
LIBINPUT_SWITCH_KEYPAD_SLIDE, |
| 536 |
LIBINPUT_SWITCH_STATE_OFF); |
| 537 |
litest_dispatch(li); |
| 538 |
|
| 539 |
while ((event = libinput_get_event(li))) { |
| 540 |
enum libinput_event_type type; |
| 541 |
type = libinput_event_get_type(event); |
| 542 |
|
| 543 |
if (type == LIBINPUT_EVENT_SWITCH_TOGGLE) { |
| 544 |
struct libinput_event_switch *s; |
| 545 |
struct libinput_event *base; |
| 546 |
s = libinput_event_get_switch_event(event); |
| 547 |
base = libinput_event_switch_get_base_event(s); |
| 548 |
litest_assert(event == base); |
| 549 |
|
| 550 |
sw++; |
| 551 |
|
| 552 |
litest_disable_log_handler(li); |
| 553 |
litest_assert(libinput_event_get_device_notify_event(event) == |
| 554 |
NULL); |
| 555 |
litest_assert(libinput_event_get_keyboard_event(event) == NULL); |
| 556 |
litest_assert(libinput_event_get_pointer_event(event) == NULL); |
| 557 |
litest_assert(libinput_event_get_touch_event(event) == NULL); |
| 558 |
litest_assert(libinput_event_get_gesture_event(event) == NULL); |
| 559 |
litest_assert(libinput_event_get_tablet_tool_event(event) == |
| 560 |
NULL); |
| 561 |
litest_assert(libinput_event_get_tablet_pad_event(event) == |
| 562 |
NULL); |
| 563 |
litest_restore_log_handler(li); |
| 564 |
} |
| 565 |
libinput_event_destroy(event); |
| 566 |
} |
| 567 |
|
| 568 |
litest_assert_int_gt(sw, 0); |
| 569 |
} |
| 570 |
END_TEST |
| 571 |
|
| 572 |
START_TEST(context_ref_counting) |
| 573 |
{ |
| 574 |
struct libinput *li; |
| 575 |
|
| 576 |
/* These tests rely on valgrind to detect memory leak and use after |
| 577 |
* free errors. */ |
| 578 |
|
| 579 |
li = libinput_path_create_context(&simple_interface, NULL); |
| 580 |
litest_assert_notnull(li); |
| 581 |
litest_assert_ptr_eq(libinput_unref(li), NULL); |
| 582 |
|
| 583 |
li = libinput_path_create_context(&simple_interface, NULL); |
| 584 |
litest_assert_notnull(li); |
| 585 |
litest_assert_ptr_eq(libinput_ref(li), li); |
| 586 |
litest_assert_ptr_eq(libinput_unref(li), li); |
| 587 |
litest_assert_ptr_eq(libinput_unref(li), NULL); |
| 588 |
} |
| 589 |
END_TEST |
| 590 |
|
| 591 |
START_TEST(config_status_string) |
| 592 |
{ |
| 593 |
const char *strs[3]; |
| 594 |
const char *invalid; |
| 595 |
size_t i, j; |
| 596 |
|
| 597 |
strs[0] = libinput_config_status_to_str(LIBINPUT_CONFIG_STATUS_SUCCESS); |
| 598 |
strs[1] = libinput_config_status_to_str(LIBINPUT_CONFIG_STATUS_UNSUPPORTED); |
| 599 |
strs[2] = libinput_config_status_to_str(LIBINPUT_CONFIG_STATUS_INVALID); |
| 600 |
|
| 601 |
for (i = 0; i < ARRAY_LENGTH(strs) - 1; i++) |
| 602 |
for (j = i + 1; j < ARRAY_LENGTH(strs); j++) |
| 603 |
litest_assert_str_ne(strs[i], strs[j]); |
| 604 |
|
| 605 |
invalid = libinput_config_status_to_str(LIBINPUT_CONFIG_STATUS_INVALID + 1); |
| 606 |
litest_assert(invalid == NULL); |
| 607 |
invalid = libinput_config_status_to_str(LIBINPUT_CONFIG_STATUS_SUCCESS - 1); |
| 608 |
litest_assert(invalid == NULL); |
| 609 |
} |
| 610 |
END_TEST |
| 611 |
|
| 612 |
static int |
| 613 |
open_restricted_leak(const char *path, int flags, void *data) |
| 614 |
{ |
| 615 |
return *(int *)data; |
| 616 |
} |
| 617 |
|
| 618 |
static void |
| 619 |
close_restricted_leak(int fd, void *data) |
| 620 |
{ |
| 621 |
/* noop */ |
| 622 |
} |
| 623 |
|
| 624 |
static const struct libinput_interface leak_interface = { |
| 625 |
.open_restricted = open_restricted_leak, |
| 626 |
.close_restricted = close_restricted_leak, |
| 627 |
}; |
| 628 |
|
| 629 |
START_TEST(fd_no_event_leak) |
| 630 |
{ |
| 631 |
struct libevdev_uinput *uinput; |
| 632 |
struct libinput *li; |
| 633 |
struct libinput_device *device; |
| 634 |
int fd = -1; |
| 635 |
const char *path; |
| 636 |
struct libinput_event *event; |
| 637 |
|
| 638 |
/* clang-format off */ |
| 639 |
uinput = create_simple_test_device("litest test device", |
| 640 |
EV_REL, REL_X, |
| 641 |
EV_REL, REL_Y, |
| 642 |
EV_KEY, BTN_LEFT, |
| 643 |
EV_KEY, BTN_MIDDLE, |
| 644 |
EV_KEY, BTN_LEFT, |
| 645 |
-1, -1); |
| 646 |
/* clang-format on */ |
| 647 |
path = libevdev_uinput_get_devnode(uinput); |
| 648 |
|
| 649 |
fd = open(path, O_RDWR | O_NONBLOCK | O_CLOEXEC); |
| 650 |
litest_assert_int_gt(fd, -1); |
| 651 |
|
| 652 |
li = libinput_path_create_context(&leak_interface, &fd); |
| 653 |
litest_restore_log_handler(li); /* use the default litest handler */ |
| 654 |
|
| 655 |
/* Add the device, trigger an event, then remove it again. |
| 656 |
* Without it, we get a SYN_DROPPED immediately and no events. |
| 657 |
*/ |
| 658 |
device = libinput_path_add_device(li, path); |
| 659 |
libevdev_uinput_write_event(uinput, EV_REL, REL_X, 1); |
| 660 |
libevdev_uinput_write_event(uinput, EV_SYN, SYN_REPORT, 0); |
| 661 |
libinput_path_remove_device(device); |
| 662 |
litest_dispatch(li); |
| 663 |
litest_drain_events(li); |
| 664 |
|
| 665 |
/* Device is removed, but fd is still open. Queue an event, add a |
| 666 |
* new device with the same fd, the queued event must be discarded |
| 667 |
* by libinput */ |
| 668 |
libevdev_uinput_write_event(uinput, EV_REL, REL_Y, 1); |
| 669 |
libevdev_uinput_write_event(uinput, EV_SYN, SYN_REPORT, 0); |
| 670 |
litest_dispatch(li); |
| 671 |
|
| 672 |
libinput_path_add_device(li, path); |
| 673 |
litest_dispatch(li); |
| 674 |
event = libinput_get_event(li); |
| 675 |
litest_assert_event_type(event, LIBINPUT_EVENT_DEVICE_ADDED); |
| 676 |
libinput_event_destroy(event); |
| 677 |
|
| 678 |
litest_assert_empty_queue(li); |
| 679 |
|
| 680 |
close(fd); |
| 681 |
libinput_unref(li); |
| 682 |
libevdev_uinput_destroy(uinput); |
| 683 |
} |
| 684 |
END_TEST |
| 685 |
|
| 686 |
static void |
| 687 |
timer_offset_warning(struct libinput *libinput, |
| 688 |
enum libinput_log_priority priority, |
| 689 |
const char *format, |
| 690 |
va_list args) |
| 691 |
{ |
| 692 |
struct litest_user_data *user_data = libinput_get_user_data(libinput); |
| 693 |
int *warning_triggered = user_data->private; |
| 694 |
|
| 695 |
if (priority == LIBINPUT_LOG_PRIORITY_ERROR && |
| 696 |
strstr(format, "scheduled expiry is in the past")) |
| 697 |
(*warning_triggered)++; |
| 698 |
} |
| 699 |
|
| 700 |
START_TEST(timer_offset_bug_warning) |
| 701 |
{ |
| 702 |
struct litest_device *dev = litest_current_device(); |
| 703 |
struct libinput *li = dev->libinput; |
| 704 |
int warning_triggered = 0; |
| 705 |
struct litest_user_data *user_data = libinput_get_user_data(li); |
| 706 |
|
| 707 |
litest_enable_tap(dev->libinput_device); |
| 708 |
litest_drain_events(li); |
| 709 |
|
| 710 |
litest_touch_down(dev, 0, 50, 50); |
| 711 |
litest_touch_up(dev, 0); |
| 712 |
|
| 713 |
litest_timeout_tap(NULL); |
| 714 |
|
| 715 |
user_data->private = &warning_triggered; |
| 716 |
libinput_log_set_handler(li, timer_offset_warning); |
| 717 |
litest_dispatch(li); |
| 718 |
|
| 719 |
/* triggered for touch down and touch up */ |
| 720 |
litest_assert_int_eq(warning_triggered, 2); |
| 721 |
litest_restore_log_handler(li); |
| 722 |
} |
| 723 |
END_TEST |
| 724 |
|
| 725 |
static void |
| 726 |
timer_delay_warning(struct libinput *libinput, |
| 727 |
enum libinput_log_priority priority, |
| 728 |
const char *format, |
| 729 |
va_list args) |
| 730 |
{ |
| 731 |
struct litest_user_data *user_data = libinput_get_user_data(libinput); |
| 732 |
int *warning_triggered = user_data->private; |
| 733 |
|
| 734 |
if (priority == LIBINPUT_LOG_PRIORITY_ERROR && |
| 735 |
strstr(format, "event processing lagging behind by")) |
| 736 |
(*warning_triggered)++; |
| 737 |
} |
| 738 |
|
| 739 |
START_TEST(timer_delay_bug_warning) |
| 740 |
{ |
| 741 |
struct litest_device *dev = litest_current_device(); |
| 742 |
struct libinput *li = dev->libinput; |
| 743 |
int warning_triggered = 0; |
| 744 |
struct litest_user_data *user_data = libinput_get_user_data(li); |
| 745 |
|
| 746 |
litest_drain_events(li); |
| 747 |
|
| 748 |
user_data->private = &warning_triggered; |
| 749 |
libinput_log_set_handler(li, timer_delay_warning); |
| 750 |
|
| 751 |
for (int i = 0; i < 20; i++) { |
| 752 |
litest_event(dev, EV_REL, REL_X, -1); |
| 753 |
litest_event(dev, EV_SYN, SYN_REPORT, 0); |
| 754 |
msleep(21); |
| 755 |
litest_dispatch(li); |
| 756 |
} |
| 757 |
|
| 758 |
litest_assert_int_ge(warning_triggered, 1); |
| 759 |
litest_restore_log_handler(li); |
| 760 |
} |
| 761 |
END_TEST |
| 762 |
|
| 763 |
START_TEST(timer_flush) |
| 764 |
{ |
| 765 |
_litest_context_destroy_ struct libinput *li = litest_create_context(); |
| 766 |
|
| 767 |
_destroy_(litest_device) *touchpad = |
| 768 |
litest_add_device(li, LITEST_SYNAPTICS_TOUCHPAD); |
| 769 |
litest_enable_tap(touchpad->libinput_device); |
| 770 |
litest_dispatch(li); |
| 771 |
_destroy_(litest_device) *keyboard = litest_add_device(li, LITEST_KEYBOARD); |
| 772 |
litest_dispatch(li); |
| 773 |
litest_drain_events(li); |
| 774 |
|
| 775 |
/* make sure tapping works */ |
| 776 |
litest_touch_down(touchpad, 0, 50, 50); |
| 777 |
litest_touch_up(touchpad, 0); |
| 778 |
litest_timeout_tap(li); |
| 779 |
|
| 780 |
litest_assert_button_event(li, BTN_LEFT, LIBINPUT_BUTTON_STATE_PRESSED); |
| 781 |
litest_assert_button_event(li, BTN_LEFT, LIBINPUT_BUTTON_STATE_RELEASED); |
| 782 |
litest_assert_empty_queue(li); |
| 783 |
|
| 784 |
/* make sure dwt-tap is ignored */ |
| 785 |
litest_keyboard_key(keyboard, KEY_A, true); |
| 786 |
litest_keyboard_key(keyboard, KEY_A, false); |
| 787 |
litest_dispatch(li); |
| 788 |
litest_touch_down(touchpad, 0, 50, 50); |
| 789 |
litest_touch_up(touchpad, 0); |
| 790 |
litest_timeout_tap(li); |
| 791 |
litest_assert_only_typed_events(li, LIBINPUT_EVENT_KEYBOARD_KEY); |
| 792 |
|
| 793 |
/* Ignore 'timer offset negative' warnings */ |
| 794 |
litest_disable_log_handler(li); |
| 795 |
|
| 796 |
/* now mess with the timing |
| 797 |
- send a key event |
| 798 |
- expire dwt |
| 799 |
- send a tap |
| 800 |
and then call litest_dispatch(). libinput should notice that |
| 801 |
the tap event came in after the timeout and thus acknowledge the |
| 802 |
tap. |
| 803 |
*/ |
| 804 |
litest_keyboard_key(keyboard, KEY_A, true); |
| 805 |
litest_keyboard_key(keyboard, KEY_A, false); |
| 806 |
litest_timeout_dwt_long(li); |
| 807 |
litest_touch_down(touchpad, 0, 50, 50); |
| 808 |
litest_touch_up(touchpad, 0); |
| 809 |
litest_timeout_tap(li); |
| 810 |
litest_restore_log_handler(li); |
| 811 |
|
| 812 |
litest_assert_key_event(li, KEY_A, LIBINPUT_KEY_STATE_PRESSED); |
| 813 |
litest_assert_key_event(li, KEY_A, LIBINPUT_KEY_STATE_RELEASED); |
| 814 |
litest_assert_button_event(li, BTN_LEFT, LIBINPUT_BUTTON_STATE_PRESSED); |
| 815 |
litest_assert_button_event(li, BTN_LEFT, LIBINPUT_BUTTON_STATE_RELEASED); |
| 816 |
} |
| 817 |
END_TEST |
| 818 |
|
| 819 |
START_TEST(udev_absinfo_override) |
| 820 |
{ |
| 821 |
struct litest_device *dev = litest_current_device(); |
| 822 |
struct libevdev *evdev = dev->evdev; |
| 823 |
const struct input_absinfo *abs; |
| 824 |
struct udev_device *ud; |
| 825 |
struct udev_list_entry *entry; |
| 826 |
bool found_x = false, found_y = false, found_mt_x = false, found_mt_y = false; |
| 827 |
|
| 828 |
ud = libinput_device_get_udev_device(dev->libinput_device); |
| 829 |
litest_assert_notnull(ud); |
| 830 |
|
| 831 |
/* Custom checks for this special litest device only */ |
| 832 |
|
| 833 |
entry = udev_device_get_properties_list_entry(ud); |
| 834 |
while (entry) { |
| 835 |
const char *key, *value; |
| 836 |
|
| 837 |
key = udev_list_entry_get_name(entry); |
| 838 |
value = udev_list_entry_get_value(entry); |
| 839 |
|
| 840 |
if (streq(key, "EVDEV_ABS_00")) { |
| 841 |
found_x = true; |
| 842 |
litest_assert(streq(value, "1:1000:100:10")); |
| 843 |
} |
| 844 |
if (streq(key, "EVDEV_ABS_01")) { |
| 845 |
found_y = true; |
| 846 |
litest_assert(streq(value, "2:2000:200:20")); |
| 847 |
} |
| 848 |
if (streq(key, "EVDEV_ABS_35")) { |
| 849 |
found_mt_x = true; |
| 850 |
litest_assert(streq(value, "3:3000:300:30")); |
| 851 |
} |
| 852 |
if (streq(key, "EVDEV_ABS_36")) { |
| 853 |
found_mt_y = true; |
| 854 |
litest_assert(streq(value, "4:4000:400:40")); |
| 855 |
} |
| 856 |
|
| 857 |
entry = udev_list_entry_get_next(entry); |
| 858 |
} |
| 859 |
udev_device_unref(ud); |
| 860 |
|
| 861 |
litest_assert(found_x); |
| 862 |
litest_assert(found_y); |
| 863 |
litest_assert(found_mt_x); |
| 864 |
litest_assert(found_mt_y); |
| 865 |
|
| 866 |
abs = libevdev_get_abs_info(evdev, ABS_X); |
| 867 |
litest_assert_int_eq(abs->minimum, 1); |
| 868 |
litest_assert_int_eq(abs->maximum, 1000); |
| 869 |
litest_assert_int_eq(abs->resolution, 100); |
| 870 |
/* if everything goes well, we override the fuzz to 0 */ |
| 871 |
litest_assert_int_eq(abs->fuzz, 0); |
| 872 |
|
| 873 |
abs = libevdev_get_abs_info(evdev, ABS_Y); |
| 874 |
litest_assert_int_eq(abs->minimum, 2); |
| 875 |
litest_assert_int_eq(abs->maximum, 2000); |
| 876 |
litest_assert_int_eq(abs->resolution, 200); |
| 877 |
/* if everything goes well, we override the fuzz to 0 */ |
| 878 |
litest_assert_int_eq(abs->fuzz, 0); |
| 879 |
|
| 880 |
abs = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X); |
| 881 |
litest_assert_int_eq(abs->minimum, 3); |
| 882 |
litest_assert_int_eq(abs->maximum, 3000); |
| 883 |
litest_assert_int_eq(abs->resolution, 300); |
| 884 |
/* if everything goes well, we override the fuzz to 0 */ |
| 885 |
litest_assert_int_eq(abs->fuzz, 0); |
| 886 |
|
| 887 |
abs = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y); |
| 888 |
litest_assert_int_eq(abs->minimum, 4); |
| 889 |
litest_assert_int_eq(abs->maximum, 4000); |
| 890 |
litest_assert_int_eq(abs->resolution, 400); |
| 891 |
/* if everything goes well, we override the fuzz to 0 */ |
| 892 |
litest_assert_int_eq(abs->fuzz, 0); |
| 893 |
} |
| 894 |
END_TEST |
| 895 |
|
| 896 |
TEST_COLLECTION(misc) |
| 897 |
{ |
| 898 |
/* clang-format off */ |
| 899 |
litest_add_no_device(event_conversion_device_notify); |
| 900 |
litest_add_for_device(event_conversion_pointer, LITEST_MOUSE); |
| 901 |
litest_add_for_device(event_conversion_pointer_abs, LITEST_XEN_VIRTUAL_POINTER); |
| 902 |
litest_add_for_device(event_conversion_key, LITEST_KEYBOARD); |
| 903 |
litest_add_for_device(event_conversion_touch, LITEST_WACOM_ISDV4_E6_FINGER); |
| 904 |
litest_add_for_device(event_conversion_gesture, LITEST_BCM5974); |
| 905 |
litest_add_for_device(event_conversion_tablet, LITEST_WACOM_CINTIQ_12WX_PEN); |
| 906 |
litest_add_for_device(event_conversion_tablet_pad, LITEST_WACOM_INTUOS5_PAD); |
| 907 |
litest_add_for_device(event_conversion_switch, LITEST_LID_SWITCH); |
| 908 |
litest_add_for_device(event_conversion_switch, LITEST_KEYPAD_SLIDE_SWITCH); |
| 909 |
|
| 910 |
litest_add_deviceless(context_ref_counting); |
| 911 |
litest_add_deviceless(config_status_string); |
| 912 |
|
| 913 |
litest_add_for_device(timer_offset_bug_warning, LITEST_SYNAPTICS_TOUCHPAD); |
| 914 |
litest_add_for_device(timer_delay_bug_warning, LITEST_MOUSE); |
| 915 |
litest_add_no_device(timer_flush); |
| 916 |
|
| 917 |
litest_add_no_device(fd_no_event_leak); |
| 918 |
|
| 919 |
litest_add_for_device(udev_absinfo_override, LITEST_ABSINFO_OVERRIDE); |
| 920 |
/* clang-format on */ |
| 921 |
} |