2 * Copyright © 2013 Red Hat, Inc.
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 #include "libinput-util.h"
35 test_relative_event(struct litest_device *dev, int dx, int dy)
37 struct libinput *li = dev->libinput;
38 struct libinput_event *event;
39 struct libinput_event_pointer *ptrev;
41 litest_event(dev, EV_REL, REL_X, dx);
42 litest_event(dev, EV_REL, REL_Y, dy);
43 litest_event(dev, EV_SYN, SYN_REPORT, 0);
45 libinput_dispatch(li);
47 event = libinput_get_event(li);
48 ck_assert(event != NULL);
49 ck_assert_int_eq(libinput_event_get_type(event), LIBINPUT_EVENT_POINTER_MOTION);
51 ptrev = libinput_event_get_pointer_event(event);
52 ck_assert(ptrev != NULL);
53 ck_assert_int_eq(libinput_event_pointer_get_dx(ptrev), li_fixed_from_int(dx));
54 ck_assert_int_eq(libinput_event_pointer_get_dy(ptrev), li_fixed_from_int(dy));
56 libinput_event_destroy(event);
59 START_TEST(pointer_motion_relative)
61 struct litest_device *dev = litest_current_device();
63 litest_drain_events(dev->libinput);
65 test_relative_event(dev, 1, 0);
66 test_relative_event(dev, 1, 1);
67 test_relative_event(dev, 1, -1);
68 test_relative_event(dev, 0, 1);
70 test_relative_event(dev, -1, 0);
71 test_relative_event(dev, -1, 1);
72 test_relative_event(dev, -1, -1);
73 test_relative_event(dev, 0, -1);
78 test_button_event(struct litest_device *dev, int button, int state)
80 struct libinput *li = dev->libinput;
81 struct libinput_event *event;
82 struct libinput_event_pointer *ptrev;
84 litest_event(dev, EV_KEY, button, state);
85 litest_event(dev, EV_SYN, SYN_REPORT, 0);
87 libinput_dispatch(li);
89 event = libinput_get_event(li);
90 ck_assert(event != NULL);
91 ck_assert_int_eq(libinput_event_get_type(event), LIBINPUT_EVENT_POINTER_BUTTON);
93 ptrev = libinput_event_get_pointer_event(event);
94 ck_assert(ptrev != NULL);
95 ck_assert_int_eq(libinput_event_pointer_get_button(ptrev), button);
96 ck_assert_int_eq(libinput_event_pointer_get_button_state(ptrev),
98 LIBINPUT_POINTER_BUTTON_STATE_PRESSED :
99 LIBINPUT_POINTER_BUTTON_STATE_RELEASED);
100 libinput_event_destroy(event);
103 START_TEST(pointer_button)
105 struct litest_device *dev = litest_current_device();
107 litest_drain_events(dev->libinput);
109 test_button_event(dev, BTN_LEFT, 1);
110 test_button_event(dev, BTN_LEFT, 0);
112 /* press it twice for good measure */
113 test_button_event(dev, BTN_LEFT, 1);
114 test_button_event(dev, BTN_LEFT, 0);
116 if (libevdev_has_event_code(dev->evdev, EV_KEY, BTN_RIGHT)) {
117 test_button_event(dev, BTN_RIGHT, 1);
118 test_button_event(dev, BTN_RIGHT, 0);
121 if (libevdev_has_event_code(dev->evdev, EV_KEY, BTN_MIDDLE)) {
122 test_button_event(dev, BTN_MIDDLE, 1);
123 test_button_event(dev, BTN_MIDDLE, 0);
129 test_wheel_event(struct litest_device *dev, int which, int amount)
131 struct libinput *li = dev->libinput;
132 struct libinput_event *event;
133 struct libinput_event_pointer *ptrev;
135 /* the current evdev implementation scales the scroll wheel events
137 const int scroll_step = 10;
138 int expected = amount * scroll_step;
140 /* mouse scroll wheels are 'upside down' */
141 if (which == REL_WHEEL)
143 litest_event(dev, EV_REL, which, amount);
144 litest_event(dev, EV_SYN, SYN_REPORT, 0);
146 libinput_dispatch(li);
148 event = libinput_get_event(li);
149 ck_assert(event != NULL);
150 ck_assert_int_eq(libinput_event_get_type(event),
151 LIBINPUT_EVENT_POINTER_AXIS);
153 ptrev = libinput_event_get_pointer_event(event);
154 ck_assert(ptrev != NULL);
155 ck_assert_int_eq(libinput_event_pointer_get_axis(ptrev),
157 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL :
158 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL);
159 ck_assert_int_eq(libinput_event_pointer_get_axis_value(ptrev),
160 li_fixed_from_int(expected));
161 libinput_event_destroy(event);
164 START_TEST(pointer_scroll_wheel)
166 struct litest_device *dev = litest_current_device();
168 litest_drain_events(dev->libinput);
170 test_wheel_event(dev, REL_WHEEL, -1);
171 test_wheel_event(dev, REL_WHEEL, 1);
173 test_wheel_event(dev, REL_WHEEL, -5);
174 test_wheel_event(dev, REL_WHEEL, 6);
176 if (libevdev_has_event_code(dev->evdev, EV_REL, REL_HWHEEL)) {
177 test_wheel_event(dev, REL_HWHEEL, -1);
178 test_wheel_event(dev, REL_HWHEEL, 1);
180 test_wheel_event(dev, REL_HWHEEL, -5);
181 test_wheel_event(dev, REL_HWHEEL, 6);
186 int main (int argc, char **argv) {
188 litest_add("pointer:motion", pointer_motion_relative, LITEST_POINTER, LITEST_ANY);
189 litest_add("pointer:button", pointer_button, LITEST_BUTTON, LITEST_ANY);
190 litest_add("pointer:scroll", pointer_scroll_wheel, LITEST_WHEEL, LITEST_ANY);
192 return litest_run(argc, argv);