• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2013 Red Hat, Inc.
4  */
5 
6 #include "config.h"
7 #include <check.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 
12 #include "test-common.h"
13 
test_logfunc_abort_on_error(enum libevdev_log_priority priority,void * data,const char * file,int line,const char * func,const char * format,va_list args)14 void test_logfunc_abort_on_error(enum libevdev_log_priority priority,
15 				 void *data,
16 				 const char *file, int line,
17 				 const char *func,
18 				 const char *format, va_list args)
19 {
20 	vprintf(format, args);
21 	ck_abort();
22 }
23 
test_logfunc_ignore_error(enum libevdev_log_priority priority,void * data,const char * file,int line,const char * func,const char * format,va_list args)24 void test_logfunc_ignore_error(enum libevdev_log_priority priority,
25 			       void *data,
26 			       const char *file, int line,
27 			       const char *func,
28 			       const char *format, va_list args)
29 {
30 }
31 
test_create_device(struct uinput_device ** uidev_return,struct libevdev ** dev_return,...)32 void test_create_device(struct uinput_device **uidev_return,
33 			struct libevdev **dev_return,
34 			...)
35 {
36 	int rc, fd;
37 	struct uinput_device *uidev;
38 	struct libevdev *dev;
39 	va_list args;
40 
41 	va_start(args, dev_return);
42 
43 	rc = uinput_device_new_with_events_v(&uidev, TEST_DEVICE_NAME, DEFAULT_IDS, args);
44 	va_end(args);
45 
46 	ck_assert_msg(rc == 0, "Failed to create uinput device: %s", strerror(-rc));
47 
48 	fd = uinput_device_get_fd(uidev);
49 
50 	rc = libevdev_new_from_fd(fd, &dev);
51 	ck_assert_msg(rc == 0, "Failed to init device device: %s", strerror(-rc));
52 	rc = fcntl(fd, F_SETFL, O_NONBLOCK);
53 	ck_assert_msg(rc == 0, "fcntl failed: %s", strerror(errno));
54 
55 	*uidev_return = uidev;
56 	*dev_return = dev;
57 }
58 
test_create_abs_device(struct uinput_device ** uidev_return,struct libevdev ** dev_return,int nabs,const struct input_absinfo * abs,...)59 void test_create_abs_device(struct uinput_device **uidev_return,
60 			    struct libevdev **dev_return,
61 			    int nabs,
62 			    const struct input_absinfo *abs,
63 			    ...)
64 {
65 	int rc, fd;
66 	struct uinput_device *uidev;
67 	struct libevdev *dev;
68 	va_list args;
69 
70 	uidev = uinput_device_new(TEST_DEVICE_NAME);
71 	ck_assert(uidev != NULL);
72 
73 	va_start(args, abs);
74 	rc = uinput_device_set_event_bits_v(uidev, args);
75 	ck_assert_msg(rc == 0, "Failed to set uinput bits");
76 	va_end(args);
77 
78 	while (--nabs >= 0) {
79 		int code;
80 		struct input_absinfo a;
81 
82 		code = abs[nabs].value;
83 		a = abs[nabs];
84 		a.value = 0;
85 
86 		rc = uinput_device_set_abs_bit(uidev, code, &a);
87 		ck_assert_msg(rc == 0, "for abs field %d\n", nabs);
88 	}
89 
90 	rc = uinput_device_create(uidev);
91 	ck_assert_msg(rc == 0, "Failed to create uinput device: %s", strerror(-rc));
92 
93 	fd = uinput_device_get_fd(uidev);
94 
95 	rc = libevdev_new_from_fd(fd, &dev);
96 	ck_assert_msg(rc == 0, "Failed to init device device: %s", strerror(-rc));
97 	rc = fcntl(fd, F_SETFL, O_NONBLOCK);
98 	ck_assert_msg(rc == 0, "fcntl failed: %s", strerror(errno));
99 
100 	*uidev_return = uidev;
101 	*dev_return = dev;
102 }
103