Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
| 3 | * Copyright © 2013 Jonas Ådahl |
| 4 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 5 | * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | * a copy of this software and associated documentation files (the |
| 7 | * "Software"), to deal in the Software without restriction, including |
| 8 | * without limitation the rights to use, copy, modify, merge, publish, |
| 9 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | * permit persons to whom the Software is furnished to do so, subject to |
| 11 | * the following conditions: |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 12 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 13 | * The above copyright notice and this permission notice (including the |
| 14 | * next paragraph) shall be included in all copies or substantial |
| 15 | * portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | * SOFTWARE. |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | |
| 29 | #include <errno.h> |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 30 | #include <stdint.h> |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <linux/input.h> |
| 34 | #include <unistd.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <mtdev.h> |
| 37 | #include <assert.h> |
| 38 | #include <libinput.h> |
| 39 | |
| 40 | #include "compositor.h" |
| 41 | #include "libinput-device.h" |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 42 | #include "shared/helpers.h" |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 43 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 44 | void |
| 45 | evdev_led_update(struct evdev_device *device, enum weston_led weston_leds) |
| 46 | { |
| 47 | enum libinput_led leds = 0; |
| 48 | |
| 49 | if (weston_leds & LED_NUM_LOCK) |
| 50 | leds |= LIBINPUT_LED_NUM_LOCK; |
| 51 | if (weston_leds & LED_CAPS_LOCK) |
| 52 | leds |= LIBINPUT_LED_CAPS_LOCK; |
| 53 | if (weston_leds & LED_SCROLL_LOCK) |
| 54 | leds |= LIBINPUT_LED_SCROLL_LOCK; |
| 55 | |
| 56 | libinput_device_led_update(device->device, leds); |
| 57 | } |
| 58 | |
| 59 | static void |
| 60 | handle_keyboard_key(struct libinput_device *libinput_device, |
| 61 | struct libinput_event_keyboard *keyboard_event) |
| 62 | { |
| 63 | struct evdev_device *device = |
| 64 | libinput_device_get_user_data(libinput_device); |
Jonas Ådahl | 90d1ac8 | 2015-01-30 12:23:00 +0800 | [diff] [blame] | 65 | int key_state = |
| 66 | libinput_event_keyboard_get_key_state(keyboard_event); |
| 67 | int seat_key_count = |
| 68 | libinput_event_keyboard_get_seat_key_count(keyboard_event); |
| 69 | |
| 70 | /* Ignore key events that are not seat wide state changes. */ |
| 71 | if ((key_state == LIBINPUT_KEY_STATE_PRESSED && |
| 72 | seat_key_count != 1) || |
| 73 | (key_state == LIBINPUT_KEY_STATE_RELEASED && |
| 74 | seat_key_count != 0)) |
| 75 | return; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 76 | |
| 77 | notify_key(device->seat, |
| 78 | libinput_event_keyboard_get_time(keyboard_event), |
| 79 | libinput_event_keyboard_get_key(keyboard_event), |
Chris Michael | 7e7f793 | 2016-02-22 08:47:24 -0500 | [diff] [blame] | 80 | key_state, STATE_UPDATE_AUTOMATIC); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 83 | static bool |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 84 | handle_pointer_motion(struct libinput_device *libinput_device, |
| 85 | struct libinput_event_pointer *pointer_event) |
| 86 | { |
| 87 | struct evdev_device *device = |
| 88 | libinput_device_get_user_data(libinput_device); |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 89 | struct weston_pointer_motion_event event = { 0 }; |
Jonas Ådahl | de1ed2e | 2015-07-29 14:18:02 +0800 | [diff] [blame] | 90 | uint64_t time_usec = |
| 91 | libinput_event_pointer_get_time_usec(pointer_event); |
Jonas Ådahl | 3845b32 | 2014-10-21 23:51:29 +0200 | [diff] [blame] | 92 | double dx_unaccel, dy_unaccel; |
| 93 | |
| 94 | dx_unaccel = libinput_event_pointer_get_dx_unaccelerated(pointer_event); |
| 95 | dy_unaccel = libinput_event_pointer_get_dy_unaccelerated(pointer_event); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 96 | |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 97 | event = (struct weston_pointer_motion_event) { |
Jonas Ådahl | 3845b32 | 2014-10-21 23:51:29 +0200 | [diff] [blame] | 98 | .mask = WESTON_POINTER_MOTION_REL | |
| 99 | WESTON_POINTER_MOTION_REL_UNACCEL, |
Jonas Ådahl | de1ed2e | 2015-07-29 14:18:02 +0800 | [diff] [blame] | 100 | .time_usec = time_usec, |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 101 | .dx = libinput_event_pointer_get_dx(pointer_event), |
| 102 | .dy = libinput_event_pointer_get_dy(pointer_event), |
Jonas Ådahl | 3845b32 | 2014-10-21 23:51:29 +0200 | [diff] [blame] | 103 | .dx_unaccel = dx_unaccel, |
| 104 | .dy_unaccel = dy_unaccel, |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 105 | }; |
| 106 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 107 | notify_motion(device->seat, |
| 108 | libinput_event_pointer_get_time(pointer_event), |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 109 | &event); |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 110 | |
| 111 | return true; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 114 | static bool |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 115 | handle_pointer_motion_absolute( |
| 116 | struct libinput_device *libinput_device, |
| 117 | struct libinput_event_pointer *pointer_event) |
| 118 | { |
| 119 | struct evdev_device *device = |
| 120 | libinput_device_get_user_data(libinput_device); |
| 121 | struct weston_output *output = device->output; |
| 122 | uint32_t time; |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 123 | double x, y; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 124 | uint32_t width, height; |
| 125 | |
| 126 | if (!output) |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 127 | return false; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 128 | |
| 129 | time = libinput_event_pointer_get_time(pointer_event); |
| 130 | width = device->output->current_mode->width; |
| 131 | height = device->output->current_mode->height; |
| 132 | |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 133 | x = libinput_event_pointer_get_absolute_x_transformed(pointer_event, |
| 134 | width); |
| 135 | y = libinput_event_pointer_get_absolute_y_transformed(pointer_event, |
| 136 | height); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 137 | |
| 138 | weston_output_transform_coordinate(device->output, x, y, &x, &y); |
| 139 | notify_motion_absolute(device->seat, time, x, y); |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 140 | |
| 141 | return true; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 144 | static bool |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 145 | handle_pointer_button(struct libinput_device *libinput_device, |
| 146 | struct libinput_event_pointer *pointer_event) |
| 147 | { |
| 148 | struct evdev_device *device = |
| 149 | libinput_device_get_user_data(libinput_device); |
Jonas Ådahl | e90b9e9 | 2015-01-30 12:22:59 +0800 | [diff] [blame] | 150 | int button_state = |
| 151 | libinput_event_pointer_get_button_state(pointer_event); |
| 152 | int seat_button_count = |
| 153 | libinput_event_pointer_get_seat_button_count(pointer_event); |
| 154 | |
| 155 | /* Ignore button events that are not seat wide state changes. */ |
| 156 | if ((button_state == LIBINPUT_BUTTON_STATE_PRESSED && |
| 157 | seat_button_count != 1) || |
| 158 | (button_state == LIBINPUT_BUTTON_STATE_RELEASED && |
| 159 | seat_button_count != 0)) |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 160 | return false; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 161 | |
| 162 | notify_button(device->seat, |
| 163 | libinput_event_pointer_get_time(pointer_event), |
| 164 | libinput_event_pointer_get_button(pointer_event), |
Christopher Michael | e1719c7 | 2016-02-19 11:07:00 -0500 | [diff] [blame] | 165 | button_state); |
| 166 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 167 | return true; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Peter Hutterer | 5dddd41 | 2015-01-15 13:14:43 +1000 | [diff] [blame] | 170 | static double |
| 171 | normalize_scroll(struct libinput_event_pointer *pointer_event, |
| 172 | enum libinput_pointer_axis axis) |
| 173 | { |
Peter Hutterer | 5dddd41 | 2015-01-15 13:14:43 +1000 | [diff] [blame] | 174 | enum libinput_pointer_axis_source source; |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 175 | double value = 0.0; |
Peter Hutterer | 5dddd41 | 2015-01-15 13:14:43 +1000 | [diff] [blame] | 176 | |
| 177 | source = libinput_event_pointer_get_axis_source(pointer_event); |
| 178 | /* libinput < 0.8 sent wheel click events with value 10. Since 0.8 |
| 179 | the value is the angle of the click in degrees. To keep |
| 180 | backwards-compat with existing clients, we just send multiples of |
| 181 | the click count. |
| 182 | */ |
| 183 | switch (source) { |
| 184 | case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL: |
| 185 | value = 10 * libinput_event_pointer_get_axis_value_discrete( |
| 186 | pointer_event, |
| 187 | axis); |
| 188 | break; |
| 189 | case LIBINPUT_POINTER_AXIS_SOURCE_FINGER: |
| 190 | case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS: |
| 191 | value = libinput_event_pointer_get_axis_value(pointer_event, |
| 192 | axis); |
| 193 | break; |
Daniel Stone | 4933ca5 | 2017-03-14 17:24:04 +0000 | [diff] [blame] | 194 | default: |
| 195 | assert(!"unhandled event source in normalize_scroll"); |
Peter Hutterer | 5dddd41 | 2015-01-15 13:14:43 +1000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return value; |
| 199 | } |
| 200 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 201 | static int32_t |
| 202 | get_axis_discrete(struct libinput_event_pointer *pointer_event, |
| 203 | enum libinput_pointer_axis axis) |
| 204 | { |
| 205 | enum libinput_pointer_axis_source source; |
| 206 | |
| 207 | source = libinput_event_pointer_get_axis_source(pointer_event); |
| 208 | |
| 209 | if (source != LIBINPUT_POINTER_AXIS_SOURCE_WHEEL) |
| 210 | return 0; |
| 211 | |
| 212 | return libinput_event_pointer_get_axis_value_discrete(pointer_event, |
| 213 | axis); |
| 214 | } |
| 215 | |
| 216 | static bool |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 217 | handle_pointer_axis(struct libinput_device *libinput_device, |
| 218 | struct libinput_event_pointer *pointer_event) |
| 219 | { |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 220 | static int warned; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 221 | struct evdev_device *device = |
| 222 | libinput_device_get_user_data(libinput_device); |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 223 | double vert, horiz; |
| 224 | int32_t vert_discrete, horiz_discrete; |
Peter Hutterer | c54f23d | 2015-01-13 11:55:37 +1000 | [diff] [blame] | 225 | enum libinput_pointer_axis axis; |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 226 | struct weston_pointer_axis_event weston_event; |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 227 | enum libinput_pointer_axis_source source; |
| 228 | uint32_t wl_axis_source; |
| 229 | bool has_vert, has_horiz; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 230 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 231 | has_vert = libinput_event_pointer_has_axis(pointer_event, |
| 232 | LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL); |
| 233 | has_horiz = libinput_event_pointer_has_axis(pointer_event, |
| 234 | LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL); |
| 235 | |
| 236 | if (!has_vert && !has_horiz) |
| 237 | return false; |
| 238 | |
| 239 | source = libinput_event_pointer_get_axis_source(pointer_event); |
| 240 | switch (source) { |
| 241 | case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL: |
| 242 | wl_axis_source = WL_POINTER_AXIS_SOURCE_WHEEL; |
| 243 | break; |
| 244 | case LIBINPUT_POINTER_AXIS_SOURCE_FINGER: |
| 245 | wl_axis_source = WL_POINTER_AXIS_SOURCE_FINGER; |
| 246 | break; |
| 247 | case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS: |
| 248 | wl_axis_source = WL_POINTER_AXIS_SOURCE_CONTINUOUS; |
| 249 | break; |
| 250 | default: |
| 251 | if (warned < 5) { |
| 252 | weston_log("Unknown scroll source %d.\n", source); |
| 253 | warned++; |
| 254 | } |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | notify_axis_source(device->seat, wl_axis_source); |
| 259 | |
| 260 | if (has_vert) { |
| 261 | axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL; |
| 262 | vert_discrete = get_axis_discrete(pointer_event, axis); |
| 263 | vert = normalize_scroll(pointer_event, axis); |
| 264 | |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 265 | weston_event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL; |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 266 | weston_event.value = vert; |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 267 | weston_event.discrete = vert_discrete; |
| 268 | weston_event.has_discrete = (vert_discrete != 0); |
| 269 | |
Peter Hutterer | c54f23d | 2015-01-13 11:55:37 +1000 | [diff] [blame] | 270 | notify_axis(device->seat, |
| 271 | libinput_event_pointer_get_time(pointer_event), |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 272 | &weston_event); |
Peter Hutterer | c54f23d | 2015-01-13 11:55:37 +1000 | [diff] [blame] | 273 | } |
| 274 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 275 | if (has_horiz) { |
| 276 | axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL; |
| 277 | horiz_discrete = get_axis_discrete(pointer_event, axis); |
| 278 | horiz = normalize_scroll(pointer_event, axis); |
| 279 | |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 280 | weston_event.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL; |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 281 | weston_event.value = horiz; |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 282 | weston_event.discrete = horiz_discrete; |
| 283 | weston_event.has_discrete = (horiz_discrete != 0); |
| 284 | |
Peter Hutterer | c54f23d | 2015-01-13 11:55:37 +1000 | [diff] [blame] | 285 | notify_axis(device->seat, |
| 286 | libinput_event_pointer_get_time(pointer_event), |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 287 | &weston_event); |
Peter Hutterer | c54f23d | 2015-01-13 11:55:37 +1000 | [diff] [blame] | 288 | } |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 289 | |
| 290 | return true; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static void |
| 294 | handle_touch_with_coords(struct libinput_device *libinput_device, |
| 295 | struct libinput_event_touch *touch_event, |
| 296 | int touch_type) |
| 297 | { |
| 298 | struct evdev_device *device = |
| 299 | libinput_device_get_user_data(libinput_device); |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 300 | double x; |
| 301 | double y; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 302 | uint32_t width, height; |
| 303 | uint32_t time; |
| 304 | int32_t slot; |
| 305 | |
Ander Conselvan de Oliveira | f957dfb | 2014-04-24 15:11:14 +0300 | [diff] [blame] | 306 | if (!device->output) |
| 307 | return; |
| 308 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 309 | time = libinput_event_touch_get_time(touch_event); |
| 310 | slot = libinput_event_touch_get_seat_slot(touch_event); |
| 311 | |
| 312 | width = device->output->current_mode->width; |
| 313 | height = device->output->current_mode->height; |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 314 | x = libinput_event_touch_get_x_transformed(touch_event, width); |
| 315 | y = libinput_event_touch_get_y_transformed(touch_event, height); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 316 | |
| 317 | weston_output_transform_coordinate(device->output, |
| 318 | x, y, &x, &y); |
| 319 | |
| 320 | notify_touch(device->seat, time, slot, x, y, touch_type); |
| 321 | } |
| 322 | |
| 323 | static void |
| 324 | handle_touch_down(struct libinput_device *device, |
| 325 | struct libinput_event_touch *touch_event) |
| 326 | { |
| 327 | handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN); |
| 328 | } |
| 329 | |
| 330 | static void |
| 331 | handle_touch_motion(struct libinput_device *device, |
| 332 | struct libinput_event_touch *touch_event) |
| 333 | { |
| 334 | handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION); |
| 335 | } |
| 336 | |
| 337 | static void |
| 338 | handle_touch_up(struct libinput_device *libinput_device, |
| 339 | struct libinput_event_touch *touch_event) |
| 340 | { |
| 341 | struct evdev_device *device = |
| 342 | libinput_device_get_user_data(libinput_device); |
| 343 | uint32_t time = libinput_event_touch_get_time(touch_event); |
| 344 | int32_t slot = libinput_event_touch_get_seat_slot(touch_event); |
| 345 | |
| 346 | notify_touch(device->seat, time, slot, 0, 0, WL_TOUCH_UP); |
| 347 | } |
| 348 | |
Jonas Ådahl | 1679f23 | 2014-04-12 09:39:51 +0200 | [diff] [blame] | 349 | static void |
| 350 | handle_touch_frame(struct libinput_device *libinput_device, |
| 351 | struct libinput_event_touch *touch_event) |
| 352 | { |
| 353 | struct evdev_device *device = |
| 354 | libinput_device_get_user_data(libinput_device); |
| 355 | struct weston_seat *seat = device->seat; |
| 356 | |
| 357 | notify_touch_frame(seat); |
| 358 | } |
| 359 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 360 | int |
| 361 | evdev_device_process_event(struct libinput_event *event) |
| 362 | { |
| 363 | struct libinput_device *libinput_device = |
| 364 | libinput_event_get_device(event); |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 365 | struct evdev_device *device = |
| 366 | libinput_device_get_user_data(libinput_device); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 367 | int handled = 1; |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 368 | bool need_frame = false; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 369 | |
| 370 | switch (libinput_event_get_type(event)) { |
| 371 | case LIBINPUT_EVENT_KEYBOARD_KEY: |
| 372 | handle_keyboard_key(libinput_device, |
| 373 | libinput_event_get_keyboard_event(event)); |
| 374 | break; |
| 375 | case LIBINPUT_EVENT_POINTER_MOTION: |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 376 | need_frame = handle_pointer_motion(libinput_device, |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 377 | libinput_event_get_pointer_event(event)); |
| 378 | break; |
| 379 | case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE: |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 380 | need_frame = handle_pointer_motion_absolute( |
| 381 | libinput_device, |
| 382 | libinput_event_get_pointer_event(event)); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 383 | break; |
| 384 | case LIBINPUT_EVENT_POINTER_BUTTON: |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 385 | need_frame = handle_pointer_button(libinput_device, |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 386 | libinput_event_get_pointer_event(event)); |
| 387 | break; |
| 388 | case LIBINPUT_EVENT_POINTER_AXIS: |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 389 | need_frame = handle_pointer_axis( |
| 390 | libinput_device, |
| 391 | libinput_event_get_pointer_event(event)); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 392 | break; |
| 393 | case LIBINPUT_EVENT_TOUCH_DOWN: |
| 394 | handle_touch_down(libinput_device, |
| 395 | libinput_event_get_touch_event(event)); |
| 396 | break; |
| 397 | case LIBINPUT_EVENT_TOUCH_MOTION: |
| 398 | handle_touch_motion(libinput_device, |
| 399 | libinput_event_get_touch_event(event)); |
| 400 | break; |
| 401 | case LIBINPUT_EVENT_TOUCH_UP: |
| 402 | handle_touch_up(libinput_device, |
| 403 | libinput_event_get_touch_event(event)); |
U. Artie Eoff | cd9e545 | 2014-04-17 07:53:24 -0700 | [diff] [blame] | 404 | break; |
Jonas Ådahl | 1679f23 | 2014-04-12 09:39:51 +0200 | [diff] [blame] | 405 | case LIBINPUT_EVENT_TOUCH_FRAME: |
| 406 | handle_touch_frame(libinput_device, |
| 407 | libinput_event_get_touch_event(event)); |
| 408 | break; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 409 | default: |
| 410 | handled = 0; |
| 411 | weston_log("unknown libinput event %d\n", |
| 412 | libinput_event_get_type(event)); |
| 413 | } |
| 414 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 415 | if (need_frame) |
| 416 | notify_pointer_frame(device->seat); |
| 417 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 418 | return handled; |
| 419 | } |
| 420 | |
| 421 | static void |
| 422 | notify_output_destroy(struct wl_listener *listener, void *data) |
| 423 | { |
| 424 | struct evdev_device *device = |
| 425 | container_of(listener, |
| 426 | struct evdev_device, output_destroy_listener); |
| 427 | struct weston_compositor *c = device->seat->compositor; |
| 428 | struct weston_output *output; |
| 429 | |
Ander Conselvan de Oliveira | a7caef9 | 2014-04-24 15:11:17 +0300 | [diff] [blame] | 430 | if (!device->output_name && !wl_list_empty(&c->output_list)) { |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 431 | output = container_of(c->output_list.next, |
| 432 | struct weston_output, link); |
| 433 | evdev_device_set_output(device, output); |
| 434 | } else { |
| 435 | device->output = NULL; |
| 436 | } |
| 437 | } |
| 438 | |
Peter Hutterer | 3fbba49 | 2014-09-09 13:02:25 +1000 | [diff] [blame] | 439 | /** |
| 440 | * The WL_CALIBRATION property requires a pixel-specific matrix to be |
| 441 | * applied after scaling device coordinates to screen coordinates. libinput |
| 442 | * can't do that, so we need to convert the calibration to the normalized |
| 443 | * format libinput expects. |
| 444 | */ |
Giulio Camuffo | 8aedf7b | 2016-06-02 21:48:12 +0300 | [diff] [blame] | 445 | void |
Peter Hutterer | 3fbba49 | 2014-09-09 13:02:25 +1000 | [diff] [blame] | 446 | evdev_device_set_calibration(struct evdev_device *device) |
| 447 | { |
| 448 | struct udev *udev; |
| 449 | struct udev_device *udev_device = NULL; |
| 450 | const char *sysname = libinput_device_get_sysname(device->device); |
| 451 | const char *calibration_values; |
| 452 | uint32_t width, height; |
| 453 | float calibration[6]; |
| 454 | enum libinput_config_status status; |
| 455 | |
| 456 | if (!device->output) |
| 457 | return; |
| 458 | |
| 459 | width = device->output->width; |
| 460 | height = device->output->height; |
| 461 | if (width == 0 || height == 0) |
| 462 | return; |
| 463 | |
| 464 | /* If libinput has a pre-set calibration matrix, don't override it */ |
| 465 | if (!libinput_device_config_calibration_has_matrix(device->device) || |
| 466 | libinput_device_config_calibration_get_default_matrix( |
| 467 | device->device, |
| 468 | calibration) != 0) |
| 469 | return; |
| 470 | |
| 471 | udev = udev_new(); |
| 472 | if (!udev) |
| 473 | return; |
| 474 | |
| 475 | udev_device = udev_device_new_from_subsystem_sysname(udev, |
| 476 | "input", |
| 477 | sysname); |
| 478 | if (!udev_device) |
| 479 | goto out; |
| 480 | |
| 481 | calibration_values = |
| 482 | udev_device_get_property_value(udev_device, |
| 483 | "WL_CALIBRATION"); |
| 484 | |
| 485 | if (!calibration_values || sscanf(calibration_values, |
| 486 | "%f %f %f %f %f %f", |
| 487 | &calibration[0], |
| 488 | &calibration[1], |
| 489 | &calibration[2], |
| 490 | &calibration[3], |
| 491 | &calibration[4], |
| 492 | &calibration[5]) != 6) |
| 493 | goto out; |
| 494 | |
| 495 | weston_log("Applying calibration: %f %f %f %f %f %f " |
| 496 | "(normalized %f %f)\n", |
| 497 | calibration[0], |
| 498 | calibration[1], |
| 499 | calibration[2], |
| 500 | calibration[3], |
| 501 | calibration[4], |
| 502 | calibration[5], |
| 503 | calibration[2] / width, |
| 504 | calibration[5] / height); |
| 505 | |
| 506 | /* normalize to a format libinput can use. There is a chance of |
| 507 | this being wrong if the width/height don't match the device |
| 508 | width/height but I'm not sure how to fix that */ |
| 509 | calibration[2] /= width; |
| 510 | calibration[5] /= height; |
| 511 | |
| 512 | status = libinput_device_config_calibration_set_matrix(device->device, |
| 513 | calibration); |
| 514 | if (status != LIBINPUT_CONFIG_STATUS_SUCCESS) |
| 515 | weston_log("Failed to apply calibration.\n"); |
| 516 | |
| 517 | out: |
| 518 | if (udev_device) |
| 519 | udev_device_unref(udev_device); |
| 520 | udev_unref(udev); |
| 521 | } |
| 522 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 523 | void |
| 524 | evdev_device_set_output(struct evdev_device *device, |
| 525 | struct weston_output *output) |
| 526 | { |
U. Artie Eoff | 161c6c5 | 2014-04-17 07:53:25 -0700 | [diff] [blame] | 527 | if (device->output_destroy_listener.notify) { |
| 528 | wl_list_remove(&device->output_destroy_listener.link); |
| 529 | device->output_destroy_listener.notify = NULL; |
| 530 | } |
| 531 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 532 | device->output = output; |
| 533 | device->output_destroy_listener.notify = notify_output_destroy; |
| 534 | wl_signal_add(&output->destroy_signal, |
| 535 | &device->output_destroy_listener); |
Peter Hutterer | 3fbba49 | 2014-09-09 13:02:25 +1000 | [diff] [blame] | 536 | evdev_device_set_calibration(device); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | struct evdev_device * |
| 540 | evdev_device_create(struct libinput_device *libinput_device, |
| 541 | struct weston_seat *seat) |
| 542 | { |
| 543 | struct evdev_device *device; |
| 544 | |
| 545 | device = zalloc(sizeof *device); |
| 546 | if (device == NULL) |
| 547 | return NULL; |
| 548 | |
| 549 | device->seat = seat; |
| 550 | wl_list_init(&device->link); |
| 551 | device->device = libinput_device; |
| 552 | |
| 553 | if (libinput_device_has_capability(libinput_device, |
| 554 | LIBINPUT_DEVICE_CAP_KEYBOARD)) { |
| 555 | weston_seat_init_keyboard(seat, NULL); |
| 556 | device->seat_caps |= EVDEV_SEAT_KEYBOARD; |
| 557 | } |
| 558 | if (libinput_device_has_capability(libinput_device, |
| 559 | LIBINPUT_DEVICE_CAP_POINTER)) { |
| 560 | weston_seat_init_pointer(seat); |
| 561 | device->seat_caps |= EVDEV_SEAT_POINTER; |
| 562 | } |
| 563 | if (libinput_device_has_capability(libinput_device, |
| 564 | LIBINPUT_DEVICE_CAP_TOUCH)) { |
| 565 | weston_seat_init_touch(seat); |
| 566 | device->seat_caps |= EVDEV_SEAT_TOUCH; |
| 567 | } |
| 568 | |
| 569 | libinput_device_set_user_data(libinput_device, device); |
| 570 | libinput_device_ref(libinput_device); |
| 571 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 572 | return device; |
| 573 | } |
| 574 | |
| 575 | void |
| 576 | evdev_device_destroy(struct evdev_device *device) |
| 577 | { |
| 578 | if (device->seat_caps & EVDEV_SEAT_POINTER) |
| 579 | weston_seat_release_pointer(device->seat); |
| 580 | if (device->seat_caps & EVDEV_SEAT_KEYBOARD) |
| 581 | weston_seat_release_keyboard(device->seat); |
| 582 | if (device->seat_caps & EVDEV_SEAT_TOUCH) |
| 583 | weston_seat_release_touch(device->seat); |
| 584 | |
| 585 | if (device->output) |
| 586 | wl_list_remove(&device->output_destroy_listener.link); |
| 587 | wl_list_remove(&device->link); |
| 588 | libinput_device_unref(device->device); |
| 589 | free(device->devnode); |
| 590 | free(device->output_name); |
| 591 | free(device); |
| 592 | } |
| 593 | |
| 594 | void |
| 595 | evdev_notify_keyboard_focus(struct weston_seat *seat, |
| 596 | struct wl_list *evdev_devices) |
| 597 | { |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 598 | struct wl_array keys; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 599 | |
Derek Foreman | d621df2 | 2014-11-19 11:04:12 -0600 | [diff] [blame] | 600 | if (seat->keyboard_device_count == 0) |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 601 | return; |
| 602 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 603 | wl_array_init(&keys); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 604 | notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 605 | wl_array_release(&keys); |
| 606 | } |