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