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