Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2013 Intel Corporation |
| 3 | * |
| 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. |
| 13 | * |
| 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. |
| 21 | */ |
| 22 | |
Daniel Stone | 8e7a8bd | 2013-08-15 01:10:24 +0100 | [diff] [blame] | 23 | #include "config.h" |
| 24 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <stdint.h> |
| 27 | #include <string.h> |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 28 | #include <sys/mman.h> |
| 29 | #include <assert.h> |
| 30 | #include <unistd.h> |
Matt Roper | 01a9273 | 2013-06-24 16:52:44 +0100 | [diff] [blame] | 31 | #include <fcntl.h> |
Ander Conselvan de Oliveira | 54e90c7 | 2013-12-13 22:10:56 +0200 | [diff] [blame] | 32 | #include <limits.h> |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 33 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 34 | #include "../shared/os-compatibility.h" |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 35 | #include "compositor.h" |
| 36 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 37 | static void |
| 38 | empty_region(pixman_region32_t *region) |
| 39 | { |
| 40 | pixman_region32_fini(region); |
| 41 | pixman_region32_init(region); |
| 42 | } |
| 43 | |
| 44 | static void unbind_resource(struct wl_resource *resource) |
| 45 | { |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 46 | wl_list_remove(wl_resource_get_link(resource)); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Jonas Ådahl | 3042ffe | 2013-10-17 23:04:08 +0200 | [diff] [blame] | 49 | WL_EXPORT void |
Kristian Høgsberg | a71e8b2 | 2013-05-06 21:51:21 -0400 | [diff] [blame] | 50 | weston_seat_repick(struct weston_seat *seat) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 51 | { |
Kristian Høgsberg | da751b8 | 2013-07-04 00:58:07 -0400 | [diff] [blame] | 52 | const struct weston_pointer *pointer = seat->pointer; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 53 | |
Kristian Høgsberg | da751b8 | 2013-07-04 00:58:07 -0400 | [diff] [blame] | 54 | if (pointer == NULL) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 55 | return; |
| 56 | |
Kristian Høgsberg | da751b8 | 2013-07-04 00:58:07 -0400 | [diff] [blame] | 57 | pointer->grab->interface->focus(seat->pointer->grab); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static void |
| 61 | weston_compositor_idle_inhibit(struct weston_compositor *compositor) |
| 62 | { |
| 63 | weston_compositor_wake(compositor); |
| 64 | compositor->idle_inhibit++; |
| 65 | } |
| 66 | |
| 67 | static void |
| 68 | weston_compositor_idle_release(struct weston_compositor *compositor) |
| 69 | { |
| 70 | compositor->idle_inhibit--; |
| 71 | weston_compositor_wake(compositor); |
| 72 | } |
| 73 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 74 | static void |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 75 | pointer_focus_view_destroyed(struct wl_listener *listener, void *data) |
| 76 | { |
| 77 | struct weston_pointer *pointer = |
| 78 | container_of(listener, struct weston_pointer, |
| 79 | focus_view_listener); |
| 80 | |
| 81 | weston_pointer_set_focus(pointer, NULL, 0, 0); |
| 82 | } |
| 83 | |
| 84 | static void |
| 85 | pointer_focus_resource_destroyed(struct wl_listener *listener, void *data) |
| 86 | { |
| 87 | struct weston_pointer *pointer = |
| 88 | container_of(listener, struct weston_pointer, |
| 89 | focus_resource_listener); |
| 90 | |
| 91 | weston_pointer_set_focus(pointer, NULL, 0, 0); |
| 92 | } |
| 93 | |
| 94 | static void |
| 95 | keyboard_focus_resource_destroyed(struct wl_listener *listener, void *data) |
| 96 | { |
| 97 | struct weston_keyboard *keyboard = |
| 98 | container_of(listener, struct weston_keyboard, |
| 99 | focus_resource_listener); |
| 100 | |
| 101 | weston_keyboard_set_focus(keyboard, NULL); |
| 102 | } |
| 103 | |
| 104 | static void |
| 105 | touch_focus_view_destroyed(struct wl_listener *listener, void *data) |
| 106 | { |
| 107 | struct weston_touch *touch = |
| 108 | container_of(listener, struct weston_touch, |
| 109 | focus_view_listener); |
| 110 | |
| 111 | weston_touch_set_focus(touch->seat, NULL); |
| 112 | } |
| 113 | |
| 114 | static void |
| 115 | touch_focus_resource_destroyed(struct wl_listener *listener, void *data) |
| 116 | { |
| 117 | struct weston_touch *touch = |
| 118 | container_of(listener, struct weston_touch, |
| 119 | focus_resource_listener); |
| 120 | |
| 121 | weston_touch_set_focus(touch->seat, NULL); |
| 122 | } |
| 123 | |
| 124 | static void |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 125 | move_resources(struct wl_list *destination, struct wl_list *source) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 126 | { |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 127 | wl_list_insert_list(destination, source); |
| 128 | wl_list_init(source); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static void |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 132 | move_resources_for_client(struct wl_list *destination, |
| 133 | struct wl_list *source, |
| 134 | struct wl_client *client) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 135 | { |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 136 | struct wl_resource *resource, *tmp; |
| 137 | wl_resource_for_each_safe(resource, tmp, source) { |
| 138 | if (wl_resource_get_client(resource) == client) { |
| 139 | wl_list_remove(wl_resource_get_link(resource)); |
| 140 | wl_list_insert(destination, |
| 141 | wl_resource_get_link(resource)); |
| 142 | } |
| 143 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static void |
Kristian Høgsberg | b27901c | 2013-10-28 15:32:02 -0700 | [diff] [blame] | 147 | default_grab_pointer_focus(struct weston_pointer_grab *grab) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 148 | { |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 149 | struct weston_pointer *pointer = grab->pointer; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 150 | struct weston_view *view; |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 151 | wl_fixed_t sx, sy; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 152 | |
| 153 | if (pointer->button_count > 0) |
| 154 | return; |
| 155 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 156 | view = weston_compositor_pick_view(pointer->seat->compositor, |
| 157 | pointer->x, pointer->y, |
| 158 | &sx, &sy); |
Kristian Høgsberg | 6848c25 | 2013-05-08 22:02:59 -0400 | [diff] [blame] | 159 | |
Kristian Høgsberg | db1fccb | 2014-02-05 17:14:42 -0800 | [diff] [blame] | 160 | if (pointer->focus != view || pointer->sx != sx || pointer->sy != sy) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 161 | weston_pointer_set_focus(pointer, view, sx, sy); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static void |
Giulio Camuffo | 1959ab8 | 2013-11-14 23:42:52 +0100 | [diff] [blame] | 165 | default_grab_pointer_motion(struct weston_pointer_grab *grab, uint32_t time, |
| 166 | wl_fixed_t x, wl_fixed_t y) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 167 | { |
Kristian Høgsberg | be6403e | 2013-05-08 21:03:21 -0400 | [diff] [blame] | 168 | struct weston_pointer *pointer = grab->pointer; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 169 | struct wl_list *resource_list; |
| 170 | struct wl_resource *resource; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 171 | |
Kristian Høgsberg | db1fccb | 2014-02-05 17:14:42 -0800 | [diff] [blame] | 172 | if (pointer->focus) |
| 173 | weston_view_from_global_fixed(pointer->focus, x, y, |
| 174 | &pointer->sx, &pointer->sy); |
| 175 | |
Giulio Camuffo | 1959ab8 | 2013-11-14 23:42:52 +0100 | [diff] [blame] | 176 | weston_pointer_move(pointer, x, y); |
| 177 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 178 | resource_list = &pointer->focus_resource_list; |
| 179 | wl_resource_for_each(resource, resource_list) { |
Kristian Høgsberg | db1fccb | 2014-02-05 17:14:42 -0800 | [diff] [blame] | 180 | wl_pointer_send_motion(resource, time, |
| 181 | pointer->sx, pointer->sy); |
Kristian Høgsberg | be6403e | 2013-05-08 21:03:21 -0400 | [diff] [blame] | 182 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static void |
Kristian Høgsberg | b27901c | 2013-10-28 15:32:02 -0700 | [diff] [blame] | 186 | default_grab_pointer_button(struct weston_pointer_grab *grab, |
| 187 | uint32_t time, uint32_t button, uint32_t state_w) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 188 | { |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 189 | struct weston_pointer *pointer = grab->pointer; |
Kristian Høgsberg | e122b7b | 2013-05-08 16:47:00 -0400 | [diff] [blame] | 190 | struct weston_compositor *compositor = pointer->seat->compositor; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 191 | struct weston_view *view; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 192 | struct wl_resource *resource; |
| 193 | uint32_t serial; |
| 194 | enum wl_pointer_button_state state = state_w; |
Rob Bradford | 880ebc7 | 2013-07-22 17:31:38 +0100 | [diff] [blame] | 195 | struct wl_display *display = compositor->wl_display; |
Kristian Høgsberg | e122b7b | 2013-05-08 16:47:00 -0400 | [diff] [blame] | 196 | wl_fixed_t sx, sy; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 197 | struct wl_list *resource_list; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 198 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 199 | resource_list = &pointer->focus_resource_list; |
| 200 | if (!wl_list_empty(resource_list)) { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 201 | serial = wl_display_next_serial(display); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 202 | wl_resource_for_each(resource, resource_list) |
| 203 | wl_pointer_send_button(resource, |
| 204 | serial, |
| 205 | time, |
| 206 | button, |
| 207 | state_w); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | if (pointer->button_count == 0 && |
Kristian Høgsberg | e122b7b | 2013-05-08 16:47:00 -0400 | [diff] [blame] | 211 | state == WL_POINTER_BUTTON_STATE_RELEASED) { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 212 | view = weston_compositor_pick_view(compositor, |
| 213 | pointer->x, pointer->y, |
| 214 | &sx, &sy); |
Kristian Høgsberg | e122b7b | 2013-05-08 16:47:00 -0400 | [diff] [blame] | 215 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 216 | weston_pointer_set_focus(pointer, view, sx, sy); |
Kristian Høgsberg | e122b7b | 2013-05-08 16:47:00 -0400 | [diff] [blame] | 217 | } |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 220 | static void |
| 221 | default_grab_pointer_cancel(struct weston_pointer_grab *grab) |
| 222 | { |
| 223 | } |
| 224 | |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 225 | static const struct weston_pointer_grab_interface |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 226 | default_pointer_grab_interface = { |
Kristian Høgsberg | b27901c | 2013-10-28 15:32:02 -0700 | [diff] [blame] | 227 | default_grab_pointer_focus, |
| 228 | default_grab_pointer_motion, |
| 229 | default_grab_pointer_button, |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 230 | default_grab_pointer_cancel, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 231 | }; |
| 232 | |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 233 | static void |
| 234 | default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time, |
| 235 | int touch_id, wl_fixed_t sx, wl_fixed_t sy) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 236 | { |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 237 | struct weston_touch *touch = grab->touch; |
Rob Bradford | 880ebc7 | 2013-07-22 17:31:38 +0100 | [diff] [blame] | 238 | struct wl_display *display = touch->seat->compositor->wl_display; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 239 | uint32_t serial; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 240 | struct wl_resource *resource; |
| 241 | struct wl_list *resource_list; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 242 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 243 | resource_list = &touch->focus_resource_list; |
| 244 | |
| 245 | if (!wl_list_empty(resource_list) && touch->focus) { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 246 | serial = wl_display_next_serial(display); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 247 | wl_resource_for_each(resource, resource_list) |
| 248 | wl_touch_send_down(resource, serial, time, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 249 | touch->focus->surface->resource, |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 250 | touch_id, sx, sy); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 254 | static void |
| 255 | default_grab_touch_up(struct weston_touch_grab *grab, |
| 256 | uint32_t time, int touch_id) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 257 | { |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 258 | struct weston_touch *touch = grab->touch; |
Rob Bradford | 880ebc7 | 2013-07-22 17:31:38 +0100 | [diff] [blame] | 259 | struct wl_display *display = touch->seat->compositor->wl_display; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 260 | uint32_t serial; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 261 | struct wl_resource *resource; |
| 262 | struct wl_list *resource_list; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 263 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 264 | resource_list = &touch->focus_resource_list; |
| 265 | |
| 266 | if (!wl_list_empty(resource_list)) { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 267 | serial = wl_display_next_serial(display); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 268 | wl_resource_for_each(resource, resource_list) |
| 269 | wl_touch_send_up(resource, serial, time, touch_id); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 273 | static void |
| 274 | default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time, |
| 275 | int touch_id, wl_fixed_t sx, wl_fixed_t sy) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 276 | { |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 277 | struct weston_touch *touch = grab->touch; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 278 | struct wl_resource *resource; |
| 279 | struct wl_list *resource_list; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 280 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 281 | resource_list = &touch->focus_resource_list; |
| 282 | |
| 283 | wl_resource_for_each(resource, resource_list) { |
| 284 | wl_touch_send_motion(resource, time, |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 285 | touch_id, sx, sy); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 289 | static void |
Jonas Ådahl | 1679f23 | 2014-04-12 09:39:51 +0200 | [diff] [blame] | 290 | default_grab_touch_frame(struct weston_touch_grab *grab) |
| 291 | { |
| 292 | struct wl_resource *resource; |
| 293 | |
| 294 | wl_resource_for_each(resource, &grab->touch->focus_resource_list) |
| 295 | wl_touch_send_frame(resource); |
| 296 | } |
| 297 | |
| 298 | static void |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 299 | default_grab_touch_cancel(struct weston_touch_grab *grab) |
| 300 | { |
| 301 | } |
| 302 | |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 303 | static const struct weston_touch_grab_interface default_touch_grab_interface = { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 304 | default_grab_touch_down, |
| 305 | default_grab_touch_up, |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 306 | default_grab_touch_motion, |
Jonas Ådahl | 1679f23 | 2014-04-12 09:39:51 +0200 | [diff] [blame] | 307 | default_grab_touch_frame, |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 308 | default_grab_touch_cancel, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 309 | }; |
| 310 | |
| 311 | static void |
Kristian Høgsberg | b27901c | 2013-10-28 15:32:02 -0700 | [diff] [blame] | 312 | default_grab_keyboard_key(struct weston_keyboard_grab *grab, |
| 313 | uint32_t time, uint32_t key, uint32_t state) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 314 | { |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 315 | struct weston_keyboard *keyboard = grab->keyboard; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 316 | struct wl_resource *resource; |
Rob Bradford | 880ebc7 | 2013-07-22 17:31:38 +0100 | [diff] [blame] | 317 | struct wl_display *display = keyboard->seat->compositor->wl_display; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 318 | uint32_t serial; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 319 | struct wl_list *resource_list; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 320 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 321 | resource_list = &keyboard->focus_resource_list; |
| 322 | if (!wl_list_empty(resource_list)) { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 323 | serial = wl_display_next_serial(display); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 324 | wl_resource_for_each(resource, resource_list) |
| 325 | wl_keyboard_send_key(resource, |
| 326 | serial, |
| 327 | time, |
| 328 | key, |
| 329 | state); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | static void |
| 334 | send_modifiers_to_resource(struct weston_keyboard *keyboard, |
| 335 | struct wl_resource *resource, |
| 336 | uint32_t serial) |
| 337 | { |
| 338 | wl_keyboard_send_modifiers(resource, |
| 339 | serial, |
| 340 | keyboard->modifiers.mods_depressed, |
| 341 | keyboard->modifiers.mods_latched, |
| 342 | keyboard->modifiers.mods_locked, |
| 343 | keyboard->modifiers.group); |
| 344 | } |
| 345 | |
| 346 | static void |
| 347 | send_modifiers_to_client_in_list(struct wl_client *client, |
| 348 | struct wl_list *list, |
| 349 | uint32_t serial, |
| 350 | struct weston_keyboard *keyboard) |
| 351 | { |
| 352 | struct wl_resource *resource; |
| 353 | |
| 354 | wl_resource_for_each(resource, list) { |
| 355 | if (wl_resource_get_client(resource) == client) |
| 356 | send_modifiers_to_resource(keyboard, |
| 357 | resource, |
| 358 | serial); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
| 362 | static struct wl_resource * |
Kristian Høgsberg | fe7aa90 | 2013-05-08 09:54:37 -0400 | [diff] [blame] | 363 | find_resource_for_surface(struct wl_list *list, struct weston_surface *surface) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 364 | { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 365 | if (!surface) |
| 366 | return NULL; |
| 367 | |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 368 | if (!surface->resource) |
| 369 | return NULL; |
Stefan Schmidt | fda2652 | 2013-09-17 10:54:09 +0100 | [diff] [blame] | 370 | |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 371 | return wl_resource_find_for_client(list, wl_resource_get_client(surface->resource)); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 372 | } |
| 373 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 374 | static struct wl_resource * |
| 375 | find_resource_for_view(struct wl_list *list, struct weston_view *view) |
| 376 | { |
| 377 | if (!view) |
| 378 | return NULL; |
| 379 | |
| 380 | return find_resource_for_surface(list, view->surface); |
| 381 | } |
| 382 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 383 | static void |
Kristian Høgsberg | b27901c | 2013-10-28 15:32:02 -0700 | [diff] [blame] | 384 | default_grab_keyboard_modifiers(struct weston_keyboard_grab *grab, |
| 385 | uint32_t serial, uint32_t mods_depressed, |
| 386 | uint32_t mods_latched, |
| 387 | uint32_t mods_locked, uint32_t group) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 388 | { |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 389 | struct weston_keyboard *keyboard = grab->keyboard; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 390 | struct weston_pointer *pointer = grab->keyboard->seat->pointer; |
| 391 | struct wl_resource *resource; |
| 392 | struct wl_list *resource_list; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 393 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 394 | resource_list = &keyboard->focus_resource_list; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 395 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 396 | wl_resource_for_each(resource, resource_list) { |
| 397 | wl_keyboard_send_modifiers(resource, serial, mods_depressed, |
| 398 | mods_latched, mods_locked, group); |
| 399 | } |
Jason Ekstrand | 42133d4 | 2013-11-14 20:06:16 -0600 | [diff] [blame] | 400 | if (pointer && pointer->focus && pointer->focus->surface->resource && |
| 401 | pointer->focus->surface != keyboard->focus) { |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 402 | struct wl_client *pointer_client = |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 403 | wl_resource_get_client(pointer->focus->surface->resource); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 404 | send_modifiers_to_client_in_list(pointer_client, |
| 405 | &keyboard->resource_list, |
| 406 | serial, |
| 407 | keyboard); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 411 | static void |
| 412 | default_grab_keyboard_cancel(struct weston_keyboard_grab *grab) |
| 413 | { |
| 414 | } |
| 415 | |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 416 | static const struct weston_keyboard_grab_interface |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 417 | default_keyboard_grab_interface = { |
Kristian Høgsberg | b27901c | 2013-10-28 15:32:02 -0700 | [diff] [blame] | 418 | default_grab_keyboard_key, |
| 419 | default_grab_keyboard_modifiers, |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 420 | default_grab_keyboard_cancel, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 421 | }; |
| 422 | |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 423 | static void |
| 424 | pointer_unmap_sprite(struct weston_pointer *pointer) |
| 425 | { |
Pekka Paalanen | c557ff7 | 2014-11-12 16:42:52 +0200 | [diff] [blame] | 426 | struct weston_surface *surface = pointer->sprite->surface; |
| 427 | |
| 428 | if (weston_surface_is_mapped(surface)) |
| 429 | weston_surface_unmap(surface); |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 430 | |
| 431 | wl_list_remove(&pointer->sprite_destroy_listener.link); |
Pekka Paalanen | c557ff7 | 2014-11-12 16:42:52 +0200 | [diff] [blame] | 432 | surface->configure = NULL; |
| 433 | surface->configure_private = NULL; |
Pekka Paalanen | 8274d90 | 2014-08-06 19:36:51 +0300 | [diff] [blame] | 434 | weston_surface_set_label_func(surface, NULL); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 435 | weston_view_destroy(pointer->sprite); |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 436 | pointer->sprite = NULL; |
| 437 | } |
| 438 | |
| 439 | static void |
| 440 | pointer_handle_sprite_destroy(struct wl_listener *listener, void *data) |
| 441 | { |
| 442 | struct weston_pointer *pointer = |
| 443 | container_of(listener, struct weston_pointer, |
| 444 | sprite_destroy_listener); |
| 445 | |
| 446 | pointer->sprite = NULL; |
| 447 | } |
| 448 | |
Jonas Ådahl | 3e12e63 | 2013-12-02 22:05:05 +0100 | [diff] [blame] | 449 | static void |
| 450 | weston_pointer_reset_state(struct weston_pointer *pointer) |
| 451 | { |
| 452 | pointer->button_count = 0; |
| 453 | } |
| 454 | |
Ander Conselvan de Oliveira | f84327a | 2014-01-29 18:47:51 +0200 | [diff] [blame] | 455 | static void |
| 456 | weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data); |
| 457 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 458 | WL_EXPORT struct weston_pointer * |
Giulio Camuffo | cdb4d29 | 2013-11-14 23:42:53 +0100 | [diff] [blame] | 459 | weston_pointer_create(struct weston_seat *seat) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 460 | { |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 461 | struct weston_pointer *pointer; |
| 462 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 463 | pointer = zalloc(sizeof *pointer); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 464 | if (pointer == NULL) |
| 465 | return NULL; |
| 466 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 467 | wl_list_init(&pointer->resource_list); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 468 | wl_list_init(&pointer->focus_resource_list); |
Giulio Camuffo | cdb4d29 | 2013-11-14 23:42:53 +0100 | [diff] [blame] | 469 | weston_pointer_set_default_grab(pointer, |
| 470 | seat->compositor->default_pointer_grab); |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 471 | wl_list_init(&pointer->focus_resource_listener.link); |
| 472 | pointer->focus_resource_listener.notify = pointer_focus_resource_destroyed; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 473 | pointer->default_grab.pointer = pointer; |
| 474 | pointer->grab = &pointer->default_grab; |
Giulio Camuffo | 6fcb378 | 2013-11-14 23:42:50 +0100 | [diff] [blame] | 475 | wl_signal_init(&pointer->motion_signal); |
Emilio Pozuelo Monfort | aa7a476 | 2013-11-19 11:37:15 +0100 | [diff] [blame] | 476 | wl_signal_init(&pointer->focus_signal); |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 477 | wl_list_init(&pointer->focus_view_listener.link); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 478 | |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 479 | pointer->sprite_destroy_listener.notify = pointer_handle_sprite_destroy; |
| 480 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 481 | /* FIXME: Pick better co-ords. */ |
| 482 | pointer->x = wl_fixed_from_int(100); |
| 483 | pointer->y = wl_fixed_from_int(100); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 484 | |
Ander Conselvan de Oliveira | f84327a | 2014-01-29 18:47:51 +0200 | [diff] [blame] | 485 | pointer->output_destroy_listener.notify = |
| 486 | weston_pointer_handle_output_destroy; |
| 487 | wl_signal_add(&seat->compositor->output_destroyed_signal, |
| 488 | &pointer->output_destroy_listener); |
| 489 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 490 | return pointer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | WL_EXPORT void |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 494 | weston_pointer_destroy(struct weston_pointer *pointer) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 495 | { |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 496 | if (pointer->sprite) |
| 497 | pointer_unmap_sprite(pointer); |
| 498 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 499 | /* XXX: What about pointer->resource_list? */ |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 500 | |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 501 | wl_list_remove(&pointer->focus_resource_listener.link); |
| 502 | wl_list_remove(&pointer->focus_view_listener.link); |
Ander Conselvan de Oliveira | f84327a | 2014-01-29 18:47:51 +0200 | [diff] [blame] | 503 | wl_list_remove(&pointer->output_destroy_listener.link); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 504 | free(pointer); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 505 | } |
| 506 | |
Giulio Camuffo | cdb4d29 | 2013-11-14 23:42:53 +0100 | [diff] [blame] | 507 | void |
| 508 | weston_pointer_set_default_grab(struct weston_pointer *pointer, |
| 509 | const struct weston_pointer_grab_interface *interface) |
| 510 | { |
| 511 | if (interface) |
| 512 | pointer->default_grab.interface = interface; |
| 513 | else |
| 514 | pointer->default_grab.interface = |
| 515 | &default_pointer_grab_interface; |
| 516 | } |
| 517 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 518 | WL_EXPORT struct weston_keyboard * |
| 519 | weston_keyboard_create(void) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 520 | { |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 521 | struct weston_keyboard *keyboard; |
| 522 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 523 | keyboard = zalloc(sizeof *keyboard); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 524 | if (keyboard == NULL) |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 525 | return NULL; |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 526 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 527 | wl_list_init(&keyboard->resource_list); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 528 | wl_list_init(&keyboard->focus_resource_list); |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 529 | wl_list_init(&keyboard->focus_resource_listener.link); |
| 530 | keyboard->focus_resource_listener.notify = keyboard_focus_resource_destroyed; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 531 | wl_array_init(&keyboard->keys); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 532 | keyboard->default_grab.interface = &default_keyboard_grab_interface; |
| 533 | keyboard->default_grab.keyboard = keyboard; |
| 534 | keyboard->grab = &keyboard->default_grab; |
| 535 | wl_signal_init(&keyboard->focus_signal); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 536 | |
| 537 | return keyboard; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 538 | } |
| 539 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 540 | static void |
| 541 | weston_xkb_info_destroy(struct weston_xkb_info *xkb_info); |
| 542 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 543 | WL_EXPORT void |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 544 | weston_keyboard_destroy(struct weston_keyboard *keyboard) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 545 | { |
| 546 | /* XXX: What about keyboard->resource_list? */ |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 547 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 548 | #ifdef ENABLE_XKBCOMMON |
| 549 | if (keyboard->seat->compositor->use_xkbcommon) { |
Ran Benita | c9c7415 | 2014-08-19 23:59:52 +0300 | [diff] [blame] | 550 | xkb_state_unref(keyboard->xkb_state.state); |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 551 | if (keyboard->xkb_info) |
| 552 | weston_xkb_info_destroy(keyboard->xkb_info); |
Ran Benita | c9c7415 | 2014-08-19 23:59:52 +0300 | [diff] [blame] | 553 | xkb_keymap_unref(keyboard->pending_keymap); |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 554 | } |
| 555 | #endif |
| 556 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 557 | wl_array_release(&keyboard->keys); |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 558 | wl_list_remove(&keyboard->focus_resource_listener.link); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 559 | free(keyboard); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 560 | } |
| 561 | |
Jonas Ådahl | cbfa7f7 | 2013-12-02 22:05:04 +0100 | [diff] [blame] | 562 | static void |
| 563 | weston_touch_reset_state(struct weston_touch *touch) |
| 564 | { |
| 565 | touch->num_tp = 0; |
| 566 | } |
| 567 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 568 | WL_EXPORT struct weston_touch * |
| 569 | weston_touch_create(void) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 570 | { |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 571 | struct weston_touch *touch; |
| 572 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 573 | touch = zalloc(sizeof *touch); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 574 | if (touch == NULL) |
| 575 | return NULL; |
| 576 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 577 | wl_list_init(&touch->resource_list); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 578 | wl_list_init(&touch->focus_resource_list); |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 579 | wl_list_init(&touch->focus_view_listener.link); |
| 580 | touch->focus_view_listener.notify = touch_focus_view_destroyed; |
| 581 | wl_list_init(&touch->focus_resource_listener.link); |
| 582 | touch->focus_resource_listener.notify = touch_focus_resource_destroyed; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 583 | touch->default_grab.interface = &default_touch_grab_interface; |
| 584 | touch->default_grab.touch = touch; |
| 585 | touch->grab = &touch->default_grab; |
| 586 | wl_signal_init(&touch->focus_signal); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 587 | |
| 588 | return touch; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | WL_EXPORT void |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 592 | weston_touch_destroy(struct weston_touch *touch) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 593 | { |
| 594 | /* XXX: What about touch->resource_list? */ |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 595 | |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 596 | wl_list_remove(&touch->focus_view_listener.link); |
| 597 | wl_list_remove(&touch->focus_resource_listener.link); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 598 | free(touch); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 599 | } |
| 600 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 601 | static void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 602 | seat_send_updated_caps(struct weston_seat *seat) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 603 | { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 604 | enum wl_seat_capability caps = 0; |
Rob Bradford | 6e737f5 | 2013-09-06 17:48:19 +0100 | [diff] [blame] | 605 | struct wl_resource *resource; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 606 | |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 607 | if (seat->pointer_device_count > 0) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 608 | caps |= WL_SEAT_CAPABILITY_POINTER; |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 609 | if (seat->keyboard_device_count > 0) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 610 | caps |= WL_SEAT_CAPABILITY_KEYBOARD; |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 611 | if (seat->touch_device_count > 0) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 612 | caps |= WL_SEAT_CAPABILITY_TOUCH; |
| 613 | |
Rob Bradford | 6e737f5 | 2013-09-06 17:48:19 +0100 | [diff] [blame] | 614 | wl_resource_for_each(resource, &seat->base_resource_list) { |
| 615 | wl_seat_send_capabilities(resource, caps); |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 616 | } |
Jason Ekstrand | a4ab542 | 2014-04-02 19:53:45 -0500 | [diff] [blame] | 617 | wl_signal_emit(&seat->updated_caps_signal, seat); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | WL_EXPORT void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 621 | weston_pointer_set_focus(struct weston_pointer *pointer, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 622 | struct weston_view *view, |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 623 | wl_fixed_t sx, wl_fixed_t sy) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 624 | { |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 625 | struct weston_keyboard *kbd = pointer->seat->keyboard; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 626 | struct wl_resource *resource; |
Rob Bradford | 880ebc7 | 2013-07-22 17:31:38 +0100 | [diff] [blame] | 627 | struct wl_display *display = pointer->seat->compositor->wl_display; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 628 | uint32_t serial; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 629 | struct wl_list *focus_resource_list; |
Kristian Høgsberg | db1fccb | 2014-02-05 17:14:42 -0800 | [diff] [blame] | 630 | int refocus = 0; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 631 | |
| 632 | if ((!pointer->focus && view) || |
| 633 | (pointer->focus && !view) || |
Kristian Høgsberg | db1fccb | 2014-02-05 17:14:42 -0800 | [diff] [blame] | 634 | (pointer->focus && pointer->focus->surface != view->surface) || |
| 635 | pointer->sx != sx || pointer->sy != sy) |
| 636 | refocus = 1; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 637 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 638 | focus_resource_list = &pointer->focus_resource_list; |
| 639 | |
Kristian Høgsberg | db1fccb | 2014-02-05 17:14:42 -0800 | [diff] [blame] | 640 | if (!wl_list_empty(focus_resource_list) && refocus) { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 641 | serial = wl_display_next_serial(display); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 642 | wl_resource_for_each(resource, focus_resource_list) { |
| 643 | wl_pointer_send_leave(resource, serial, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 644 | pointer->focus->surface->resource); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | move_resources(&pointer->resource_list, focus_resource_list); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 648 | } |
| 649 | |
Kristian Høgsberg | db1fccb | 2014-02-05 17:14:42 -0800 | [diff] [blame] | 650 | if (find_resource_for_view(&pointer->resource_list, view) && refocus) { |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 651 | struct wl_client *surface_client = |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 652 | wl_resource_get_client(view->surface->resource); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 653 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 654 | serial = wl_display_next_serial(display); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 655 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 656 | if (kbd && kbd->focus != view->surface) |
Kristian Høgsberg | cb406f1 | 2013-10-09 10:54:03 -0700 | [diff] [blame] | 657 | send_modifiers_to_client_in_list(surface_client, |
| 658 | &kbd->resource_list, |
| 659 | serial, |
| 660 | kbd); |
| 661 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 662 | move_resources_for_client(focus_resource_list, |
| 663 | &pointer->resource_list, |
| 664 | surface_client); |
| 665 | |
| 666 | wl_resource_for_each(resource, focus_resource_list) { |
| 667 | wl_pointer_send_enter(resource, |
| 668 | serial, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 669 | view->surface->resource, |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 670 | sx, sy); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 671 | } |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 672 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 673 | pointer->focus_serial = serial; |
| 674 | } |
| 675 | |
Giulio Camuffo | 65a07f8 | 2013-12-06 12:46:27 +0100 | [diff] [blame] | 676 | wl_list_remove(&pointer->focus_view_listener.link); |
| 677 | wl_list_init(&pointer->focus_view_listener.link); |
| 678 | wl_list_remove(&pointer->focus_resource_listener.link); |
| 679 | wl_list_init(&pointer->focus_resource_listener.link); |
Emilio Pozuelo Monfort | aa7a476 | 2013-11-19 11:37:15 +0100 | [diff] [blame] | 680 | if (view) |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 681 | wl_signal_add(&view->destroy_signal, &pointer->focus_view_listener); |
Giulio Camuffo | 65a07f8 | 2013-12-06 12:46:27 +0100 | [diff] [blame] | 682 | if (view && view->surface->resource) |
| 683 | wl_resource_add_destroy_listener(view->surface->resource, |
| 684 | &pointer->focus_resource_listener); |
| 685 | |
| 686 | pointer->focus = view; |
| 687 | pointer->focus_view_listener.notify = pointer_focus_view_destroyed; |
Kristian Høgsberg | db1fccb | 2014-02-05 17:14:42 -0800 | [diff] [blame] | 688 | pointer->sx = sx; |
| 689 | pointer->sy = sy; |
| 690 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 691 | wl_signal_emit(&pointer->focus_signal, pointer); |
| 692 | } |
| 693 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 694 | static void |
| 695 | send_enter_to_resource_list(struct wl_list *list, |
| 696 | struct weston_keyboard *keyboard, |
| 697 | struct weston_surface *surface, |
| 698 | uint32_t serial) |
| 699 | { |
| 700 | struct wl_resource *resource; |
| 701 | |
| 702 | wl_resource_for_each(resource, list) { |
| 703 | send_modifiers_to_resource(keyboard, resource, serial); |
| 704 | wl_keyboard_send_enter(resource, serial, |
| 705 | surface->resource, |
| 706 | &keyboard->keys); |
| 707 | } |
| 708 | } |
| 709 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 710 | WL_EXPORT void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 711 | weston_keyboard_set_focus(struct weston_keyboard *keyboard, |
Kristian Høgsberg | fe7aa90 | 2013-05-08 09:54:37 -0400 | [diff] [blame] | 712 | struct weston_surface *surface) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 713 | { |
| 714 | struct wl_resource *resource; |
Rob Bradford | 880ebc7 | 2013-07-22 17:31:38 +0100 | [diff] [blame] | 715 | struct wl_display *display = keyboard->seat->compositor->wl_display; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 716 | uint32_t serial; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 717 | struct wl_list *focus_resource_list; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 718 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 719 | focus_resource_list = &keyboard->focus_resource_list; |
| 720 | |
| 721 | if (!wl_list_empty(focus_resource_list) && keyboard->focus != surface) { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 722 | serial = wl_display_next_serial(display); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 723 | wl_resource_for_each(resource, focus_resource_list) { |
| 724 | wl_keyboard_send_leave(resource, serial, |
| 725 | keyboard->focus->resource); |
| 726 | } |
| 727 | move_resources(&keyboard->resource_list, focus_resource_list); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 728 | } |
| 729 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 730 | if (find_resource_for_surface(&keyboard->resource_list, surface) && |
| 731 | keyboard->focus != surface) { |
| 732 | struct wl_client *surface_client = |
| 733 | wl_resource_get_client(surface->resource); |
| 734 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 735 | serial = wl_display_next_serial(display); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 736 | |
| 737 | move_resources_for_client(focus_resource_list, |
| 738 | &keyboard->resource_list, |
| 739 | surface_client); |
| 740 | send_enter_to_resource_list(focus_resource_list, |
| 741 | keyboard, |
| 742 | surface, |
| 743 | serial); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 744 | keyboard->focus_serial = serial; |
Giulio Camuffo | 65a07f8 | 2013-12-06 12:46:27 +0100 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | wl_list_remove(&keyboard->focus_resource_listener.link); |
| 748 | wl_list_init(&keyboard->focus_resource_listener.link); |
| 749 | if (surface && surface->resource) |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 750 | wl_resource_add_destroy_listener(surface->resource, |
| 751 | &keyboard->focus_resource_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 752 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 753 | keyboard->focus = surface; |
| 754 | wl_signal_emit(&keyboard->focus_signal, keyboard); |
| 755 | } |
| 756 | |
Giulio Camuffo | a20ca81 | 2014-11-22 11:16:56 +0200 | [diff] [blame] | 757 | /* Users of this function must manually manage the keyboard focus */ |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 758 | WL_EXPORT void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 759 | weston_keyboard_start_grab(struct weston_keyboard *keyboard, |
| 760 | struct weston_keyboard_grab *grab) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 761 | { |
| 762 | keyboard->grab = grab; |
| 763 | grab->keyboard = keyboard; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | WL_EXPORT void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 767 | weston_keyboard_end_grab(struct weston_keyboard *keyboard) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 768 | { |
| 769 | keyboard->grab = &keyboard->default_grab; |
| 770 | } |
| 771 | |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 772 | static void |
| 773 | weston_keyboard_cancel_grab(struct weston_keyboard *keyboard) |
| 774 | { |
| 775 | keyboard->grab->interface->cancel(keyboard->grab); |
| 776 | } |
| 777 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 778 | WL_EXPORT void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 779 | weston_pointer_start_grab(struct weston_pointer *pointer, |
| 780 | struct weston_pointer_grab *grab) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 781 | { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 782 | pointer->grab = grab; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 783 | grab->pointer = pointer; |
Kristian Høgsberg | da751b8 | 2013-07-04 00:58:07 -0400 | [diff] [blame] | 784 | pointer->grab->interface->focus(pointer->grab); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | WL_EXPORT void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 788 | weston_pointer_end_grab(struct weston_pointer *pointer) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 789 | { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 790 | pointer->grab = &pointer->default_grab; |
Kristian Høgsberg | da751b8 | 2013-07-04 00:58:07 -0400 | [diff] [blame] | 791 | pointer->grab->interface->focus(pointer->grab); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 792 | } |
| 793 | |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 794 | static void |
| 795 | weston_pointer_cancel_grab(struct weston_pointer *pointer) |
| 796 | { |
| 797 | pointer->grab->interface->cancel(pointer->grab); |
| 798 | } |
| 799 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 800 | WL_EXPORT void |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 801 | weston_touch_start_grab(struct weston_touch *touch, struct weston_touch_grab *grab) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 802 | { |
| 803 | touch->grab = grab; |
| 804 | grab->touch = touch; |
| 805 | } |
| 806 | |
| 807 | WL_EXPORT void |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 808 | weston_touch_end_grab(struct weston_touch *touch) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 809 | { |
| 810 | touch->grab = &touch->default_grab; |
| 811 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 812 | |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 813 | static void |
| 814 | weston_touch_cancel_grab(struct weston_touch *touch) |
| 815 | { |
| 816 | touch->grab->interface->cancel(touch->grab); |
| 817 | } |
| 818 | |
Ander Conselvan de Oliveira | 54e90c7 | 2013-12-13 22:10:56 +0200 | [diff] [blame] | 819 | static void |
| 820 | weston_pointer_clamp_for_output(struct weston_pointer *pointer, |
| 821 | struct weston_output *output, |
| 822 | wl_fixed_t *fx, wl_fixed_t *fy) |
| 823 | { |
| 824 | int x, y; |
| 825 | |
| 826 | x = wl_fixed_to_int(*fx); |
| 827 | y = wl_fixed_to_int(*fy); |
| 828 | |
| 829 | if (x < output->x) |
| 830 | *fx = wl_fixed_from_int(output->x); |
| 831 | else if (x >= output->x + output->width) |
| 832 | *fx = wl_fixed_from_int(output->x + |
| 833 | output->width - 1); |
| 834 | if (y < output->y) |
| 835 | *fy = wl_fixed_from_int(output->y); |
| 836 | else if (y >= output->y + output->height) |
| 837 | *fy = wl_fixed_from_int(output->y + |
| 838 | output->height - 1); |
| 839 | } |
| 840 | |
Rob Bradford | 806d8c0 | 2013-06-25 18:56:41 +0100 | [diff] [blame] | 841 | WL_EXPORT void |
| 842 | weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t *fy) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 843 | { |
Rob Bradford | 806d8c0 | 2013-06-25 18:56:41 +0100 | [diff] [blame] | 844 | struct weston_compositor *ec = pointer->seat->compositor; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 845 | struct weston_output *output, *prev = NULL; |
| 846 | int x, y, old_x, old_y, valid = 0; |
| 847 | |
| 848 | x = wl_fixed_to_int(*fx); |
| 849 | y = wl_fixed_to_int(*fy); |
Rob Bradford | 806d8c0 | 2013-06-25 18:56:41 +0100 | [diff] [blame] | 850 | old_x = wl_fixed_to_int(pointer->x); |
| 851 | old_y = wl_fixed_to_int(pointer->y); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 852 | |
| 853 | wl_list_for_each(output, &ec->output_list, link) { |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 854 | if (pointer->seat->output && pointer->seat->output != output) |
| 855 | continue; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 856 | if (pixman_region32_contains_point(&output->region, |
| 857 | x, y, NULL)) |
| 858 | valid = 1; |
| 859 | if (pixman_region32_contains_point(&output->region, |
| 860 | old_x, old_y, NULL)) |
| 861 | prev = output; |
| 862 | } |
| 863 | |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 864 | if (!prev) |
| 865 | prev = pointer->seat->output; |
| 866 | |
Ander Conselvan de Oliveira | 54e90c7 | 2013-12-13 22:10:56 +0200 | [diff] [blame] | 867 | if (prev && !valid) |
| 868 | weston_pointer_clamp_for_output(pointer, prev, fx, fy); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | /* Takes absolute values */ |
Giulio Camuffo | 1959ab8 | 2013-11-14 23:42:52 +0100 | [diff] [blame] | 872 | WL_EXPORT void |
| 873 | weston_pointer_move(struct weston_pointer *pointer, wl_fixed_t x, wl_fixed_t y) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 874 | { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 875 | int32_t ix, iy; |
| 876 | |
Rob Bradford | 806d8c0 | 2013-06-25 18:56:41 +0100 | [diff] [blame] | 877 | weston_pointer_clamp (pointer, &x, &y); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 878 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 879 | pointer->x = x; |
| 880 | pointer->y = y; |
| 881 | |
| 882 | ix = wl_fixed_to_int(x); |
| 883 | iy = wl_fixed_to_int(y); |
| 884 | |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 885 | if (pointer->sprite) { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 886 | weston_view_set_position(pointer->sprite, |
| 887 | ix - pointer->hotspot_x, |
| 888 | iy - pointer->hotspot_y); |
| 889 | weston_view_schedule_repaint(pointer->sprite); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 890 | } |
Giulio Camuffo | 6fcb378 | 2013-11-14 23:42:50 +0100 | [diff] [blame] | 891 | |
Giulio Camuffo | 1959ab8 | 2013-11-14 23:42:52 +0100 | [diff] [blame] | 892 | pointer->grab->interface->focus(pointer->grab); |
Giulio Camuffo | 6fcb378 | 2013-11-14 23:42:50 +0100 | [diff] [blame] | 893 | wl_signal_emit(&pointer->motion_signal, pointer); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 894 | } |
| 895 | |
Ander Conselvan de Oliveira | 54e90c7 | 2013-12-13 22:10:56 +0200 | [diff] [blame] | 896 | /** Verify if the pointer is in a valid position and move it if it isn't. |
| 897 | */ |
Ander Conselvan de Oliveira | f84327a | 2014-01-29 18:47:51 +0200 | [diff] [blame] | 898 | static void |
| 899 | weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data) |
Ander Conselvan de Oliveira | 54e90c7 | 2013-12-13 22:10:56 +0200 | [diff] [blame] | 900 | { |
Ander Conselvan de Oliveira | f84327a | 2014-01-29 18:47:51 +0200 | [diff] [blame] | 901 | struct weston_pointer *pointer; |
| 902 | struct weston_compositor *ec; |
Ander Conselvan de Oliveira | 54e90c7 | 2013-12-13 22:10:56 +0200 | [diff] [blame] | 903 | struct weston_output *output, *closest = NULL; |
| 904 | int x, y, distance, min = INT_MAX; |
| 905 | wl_fixed_t fx, fy; |
| 906 | |
Ander Conselvan de Oliveira | f84327a | 2014-01-29 18:47:51 +0200 | [diff] [blame] | 907 | pointer = container_of(listener, struct weston_pointer, |
| 908 | output_destroy_listener); |
| 909 | ec = pointer->seat->compositor; |
| 910 | |
Ander Conselvan de Oliveira | 54e90c7 | 2013-12-13 22:10:56 +0200 | [diff] [blame] | 911 | x = wl_fixed_to_int(pointer->x); |
| 912 | y = wl_fixed_to_int(pointer->y); |
| 913 | |
| 914 | wl_list_for_each(output, &ec->output_list, link) { |
| 915 | if (pixman_region32_contains_point(&output->region, |
| 916 | x, y, NULL)) |
| 917 | return; |
| 918 | |
| 919 | /* Aproximante the distance from the pointer to the center of |
| 920 | * the output. */ |
| 921 | distance = abs(output->x + output->width / 2 - x) + |
| 922 | abs(output->y + output->height / 2 - y); |
| 923 | if (distance < min) { |
| 924 | min = distance; |
| 925 | closest = output; |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | /* Nothing to do if there's no output left. */ |
| 930 | if (!closest) |
| 931 | return; |
| 932 | |
| 933 | fx = pointer->x; |
| 934 | fy = pointer->y; |
| 935 | |
| 936 | weston_pointer_clamp_for_output(pointer, closest, &fx, &fy); |
| 937 | weston_pointer_move(pointer, fx, fy); |
| 938 | } |
| 939 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 940 | WL_EXPORT void |
| 941 | notify_motion(struct weston_seat *seat, |
| 942 | uint32_t time, wl_fixed_t dx, wl_fixed_t dy) |
| 943 | { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 944 | struct weston_compositor *ec = seat->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 945 | struct weston_pointer *pointer = seat->pointer; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 946 | |
| 947 | weston_compositor_wake(ec); |
Giulio Camuffo | 1959ab8 | 2013-11-14 23:42:52 +0100 | [diff] [blame] | 948 | pointer->grab->interface->motion(pointer->grab, time, pointer->x + dx, pointer->y + dy); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 949 | } |
| 950 | |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 951 | static void |
| 952 | run_modifier_bindings(struct weston_seat *seat, uint32_t old, uint32_t new) |
| 953 | { |
| 954 | struct weston_compositor *compositor = seat->compositor; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 955 | struct weston_keyboard *keyboard = seat->keyboard; |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 956 | uint32_t diff; |
| 957 | unsigned int i; |
| 958 | struct { |
| 959 | uint32_t xkb; |
| 960 | enum weston_keyboard_modifier weston; |
| 961 | } mods[] = { |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 962 | { keyboard->xkb_info->ctrl_mod, MODIFIER_CTRL }, |
| 963 | { keyboard->xkb_info->alt_mod, MODIFIER_ALT }, |
| 964 | { keyboard->xkb_info->super_mod, MODIFIER_SUPER }, |
| 965 | { keyboard->xkb_info->shift_mod, MODIFIER_SHIFT }, |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 966 | }; |
| 967 | |
| 968 | diff = new & ~old; |
| 969 | for (i = 0; i < ARRAY_LENGTH(mods); i++) { |
| 970 | if (diff & (1 << mods[i].xkb)) |
| 971 | weston_compositor_run_modifier_binding(compositor, |
| 972 | seat, |
| 973 | mods[i].weston, |
| 974 | WL_KEYBOARD_KEY_STATE_PRESSED); |
| 975 | } |
| 976 | |
| 977 | diff = old & ~new; |
| 978 | for (i = 0; i < ARRAY_LENGTH(mods); i++) { |
| 979 | if (diff & (1 << mods[i].xkb)) |
| 980 | weston_compositor_run_modifier_binding(compositor, |
| 981 | seat, |
| 982 | mods[i].weston, |
| 983 | WL_KEYBOARD_KEY_STATE_RELEASED); |
| 984 | } |
| 985 | } |
| 986 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 987 | WL_EXPORT void |
| 988 | notify_motion_absolute(struct weston_seat *seat, |
| 989 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 990 | { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 991 | struct weston_compositor *ec = seat->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 992 | struct weston_pointer *pointer = seat->pointer; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 993 | |
| 994 | weston_compositor_wake(ec); |
Giulio Camuffo | 1959ab8 | 2013-11-14 23:42:52 +0100 | [diff] [blame] | 995 | pointer->grab->interface->motion(pointer->grab, time, x, y); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | WL_EXPORT void |
| 999 | weston_surface_activate(struct weston_surface *surface, |
| 1000 | struct weston_seat *seat) |
| 1001 | { |
| 1002 | struct weston_compositor *compositor = seat->compositor; |
| 1003 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1004 | if (seat->keyboard) { |
Kristian Høgsberg | fe7aa90 | 2013-05-08 09:54:37 -0400 | [diff] [blame] | 1005 | weston_keyboard_set_focus(seat->keyboard, surface); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1006 | wl_data_device_set_keyboard_focus(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | wl_signal_emit(&compositor->activate_signal, surface); |
| 1010 | } |
| 1011 | |
| 1012 | WL_EXPORT void |
| 1013 | notify_button(struct weston_seat *seat, uint32_t time, int32_t button, |
| 1014 | enum wl_pointer_button_state state) |
| 1015 | { |
| 1016 | struct weston_compositor *compositor = seat->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1017 | struct weston_pointer *pointer = seat->pointer; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1018 | |
| 1019 | if (state == WL_POINTER_BUTTON_STATE_PRESSED) { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1020 | weston_compositor_idle_inhibit(compositor); |
| 1021 | if (pointer->button_count == 0) { |
| 1022 | pointer->grab_button = button; |
| 1023 | pointer->grab_time = time; |
| 1024 | pointer->grab_x = pointer->x; |
| 1025 | pointer->grab_y = pointer->y; |
| 1026 | } |
| 1027 | pointer->button_count++; |
| 1028 | } else { |
| 1029 | weston_compositor_idle_release(compositor); |
| 1030 | pointer->button_count--; |
| 1031 | } |
| 1032 | |
| 1033 | weston_compositor_run_button_binding(compositor, seat, time, button, |
| 1034 | state); |
| 1035 | |
| 1036 | pointer->grab->interface->button(pointer->grab, time, button, state); |
| 1037 | |
| 1038 | if (pointer->button_count == 1) |
| 1039 | pointer->grab_serial = |
| 1040 | wl_display_get_serial(compositor->wl_display); |
| 1041 | } |
| 1042 | |
| 1043 | WL_EXPORT void |
| 1044 | notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis, |
| 1045 | wl_fixed_t value) |
| 1046 | { |
| 1047 | struct weston_compositor *compositor = seat->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1048 | struct weston_pointer *pointer = seat->pointer; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1049 | struct wl_resource *resource; |
| 1050 | struct wl_list *resource_list; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1051 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1052 | weston_compositor_wake(compositor); |
| 1053 | |
| 1054 | if (!value) |
| 1055 | return; |
| 1056 | |
| 1057 | if (weston_compositor_run_axis_binding(compositor, seat, |
| 1058 | time, axis, value)) |
| 1059 | return; |
| 1060 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1061 | resource_list = &pointer->focus_resource_list; |
| 1062 | wl_resource_for_each(resource, resource_list) |
| 1063 | wl_pointer_send_axis(resource, time, axis, |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1064 | value); |
| 1065 | } |
| 1066 | |
Giulio Camuffo | 6ef444d | 2014-08-28 19:44:09 +0300 | [diff] [blame] | 1067 | WL_EXPORT int |
| 1068 | weston_keyboard_set_locks(struct weston_keyboard *keyboard, |
| 1069 | uint32_t mask, uint32_t value) |
| 1070 | { |
| 1071 | #ifdef ENABLE_XKBCOMMON |
| 1072 | uint32_t serial; |
| 1073 | xkb_mod_mask_t mods_depressed, mods_latched, mods_locked, group; |
| 1074 | xkb_mod_mask_t num, caps; |
| 1075 | |
| 1076 | /* We don't want the leds to go out of sync with the actual state |
| 1077 | * so if the backend has no way to change the leds don't try to |
| 1078 | * change the state */ |
| 1079 | if (!keyboard->seat->led_update) |
| 1080 | return -1; |
| 1081 | |
| 1082 | mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state, |
| 1083 | XKB_STATE_DEPRESSED); |
| 1084 | mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state, |
| 1085 | XKB_STATE_LATCHED); |
| 1086 | mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state, |
| 1087 | XKB_STATE_LOCKED); |
| 1088 | group = xkb_state_serialize_group(keyboard->xkb_state.state, |
| 1089 | XKB_STATE_EFFECTIVE); |
| 1090 | |
| 1091 | num = (1 << keyboard->xkb_info->mod2_mod); |
| 1092 | caps = (1 << keyboard->xkb_info->caps_mod); |
| 1093 | if (mask & WESTON_NUM_LOCK) { |
| 1094 | if (value & WESTON_NUM_LOCK) |
| 1095 | mods_locked |= num; |
| 1096 | else |
| 1097 | mods_locked &= ~num; |
| 1098 | } |
| 1099 | if (mask & WESTON_CAPS_LOCK) { |
| 1100 | if (value & WESTON_CAPS_LOCK) |
| 1101 | mods_locked |= caps; |
| 1102 | else |
| 1103 | mods_locked &= ~caps; |
| 1104 | } |
| 1105 | |
| 1106 | xkb_state_update_mask(keyboard->xkb_state.state, mods_depressed, |
| 1107 | mods_latched, mods_locked, 0, 0, group); |
| 1108 | |
| 1109 | serial = wl_display_next_serial( |
| 1110 | keyboard->seat->compositor->wl_display); |
| 1111 | notify_modifiers(keyboard->seat, serial); |
| 1112 | |
| 1113 | return 0; |
| 1114 | #else |
| 1115 | return -1; |
| 1116 | #endif |
| 1117 | } |
| 1118 | |
Rob Bradford | 382ff46 | 2013-06-24 16:52:45 +0100 | [diff] [blame] | 1119 | #ifdef ENABLE_XKBCOMMON |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1120 | WL_EXPORT void |
| 1121 | notify_modifiers(struct weston_seat *seat, uint32_t serial) |
| 1122 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1123 | struct weston_keyboard *keyboard = seat->keyboard; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1124 | struct weston_keyboard_grab *grab = keyboard->grab; |
| 1125 | uint32_t mods_depressed, mods_latched, mods_locked, group; |
| 1126 | uint32_t mods_lookup; |
| 1127 | enum weston_led leds = 0; |
| 1128 | int changed = 0; |
| 1129 | |
| 1130 | /* Serialize and update our internal state, checking to see if it's |
| 1131 | * different to the previous state. */ |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1132 | mods_depressed = xkb_state_serialize_mods(keyboard->xkb_state.state, |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 1133 | XKB_STATE_MODS_DEPRESSED); |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1134 | mods_latched = xkb_state_serialize_mods(keyboard->xkb_state.state, |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 1135 | XKB_STATE_MODS_LATCHED); |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1136 | mods_locked = xkb_state_serialize_mods(keyboard->xkb_state.state, |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 1137 | XKB_STATE_MODS_LOCKED); |
| 1138 | group = xkb_state_serialize_layout(keyboard->xkb_state.state, |
| 1139 | XKB_STATE_LAYOUT_EFFECTIVE); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1140 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1141 | if (mods_depressed != seat->keyboard->modifiers.mods_depressed || |
| 1142 | mods_latched != seat->keyboard->modifiers.mods_latched || |
| 1143 | mods_locked != seat->keyboard->modifiers.mods_locked || |
| 1144 | group != seat->keyboard->modifiers.group) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1145 | changed = 1; |
| 1146 | |
Daniel Stone | 96d47c0 | 2013-11-19 11:37:12 +0100 | [diff] [blame] | 1147 | run_modifier_bindings(seat, seat->keyboard->modifiers.mods_depressed, |
| 1148 | mods_depressed); |
| 1149 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1150 | seat->keyboard->modifiers.mods_depressed = mods_depressed; |
| 1151 | seat->keyboard->modifiers.mods_latched = mods_latched; |
| 1152 | seat->keyboard->modifiers.mods_locked = mods_locked; |
| 1153 | seat->keyboard->modifiers.group = group; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1154 | |
| 1155 | /* And update the modifier_state for bindings. */ |
| 1156 | mods_lookup = mods_depressed | mods_latched; |
| 1157 | seat->modifier_state = 0; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1158 | if (mods_lookup & (1 << keyboard->xkb_info->ctrl_mod)) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1159 | seat->modifier_state |= MODIFIER_CTRL; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1160 | if (mods_lookup & (1 << keyboard->xkb_info->alt_mod)) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1161 | seat->modifier_state |= MODIFIER_ALT; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1162 | if (mods_lookup & (1 << keyboard->xkb_info->super_mod)) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1163 | seat->modifier_state |= MODIFIER_SUPER; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1164 | if (mods_lookup & (1 << keyboard->xkb_info->shift_mod)) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1165 | seat->modifier_state |= MODIFIER_SHIFT; |
| 1166 | |
| 1167 | /* Finally, notify the compositor that LEDs have changed. */ |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1168 | if (xkb_state_led_index_is_active(keyboard->xkb_state.state, |
| 1169 | keyboard->xkb_info->num_led)) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1170 | leds |= LED_NUM_LOCK; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1171 | if (xkb_state_led_index_is_active(keyboard->xkb_state.state, |
| 1172 | keyboard->xkb_info->caps_led)) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1173 | leds |= LED_CAPS_LOCK; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1174 | if (xkb_state_led_index_is_active(keyboard->xkb_state.state, |
| 1175 | keyboard->xkb_info->scroll_led)) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1176 | leds |= LED_SCROLL_LOCK; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1177 | if (leds != keyboard->xkb_state.leds && seat->led_update) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1178 | seat->led_update(seat, leds); |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1179 | keyboard->xkb_state.leds = leds; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1180 | |
| 1181 | if (changed) { |
| 1182 | grab->interface->modifiers(grab, |
| 1183 | serial, |
| 1184 | keyboard->modifiers.mods_depressed, |
| 1185 | keyboard->modifiers.mods_latched, |
| 1186 | keyboard->modifiers.mods_locked, |
| 1187 | keyboard->modifiers.group); |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | static void |
| 1192 | update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key, |
| 1193 | enum wl_keyboard_key_state state) |
| 1194 | { |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1195 | struct weston_keyboard *keyboard = seat->keyboard; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1196 | enum xkb_key_direction direction; |
| 1197 | |
Matt Roper | 01a9273 | 2013-06-24 16:52:44 +0100 | [diff] [blame] | 1198 | /* Keyboard modifiers don't exist in raw keyboard mode */ |
| 1199 | if (!seat->compositor->use_xkbcommon) |
| 1200 | return; |
| 1201 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1202 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) |
| 1203 | direction = XKB_KEY_DOWN; |
| 1204 | else |
| 1205 | direction = XKB_KEY_UP; |
| 1206 | |
| 1207 | /* Offset the keycode by 8, as the evdev XKB rules reflect X's |
| 1208 | * broken keycode system, which starts at 8. */ |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1209 | xkb_state_update_key(keyboard->xkb_state.state, key + 8, direction); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1210 | |
| 1211 | notify_modifiers(seat, serial); |
| 1212 | } |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1213 | |
| 1214 | static void |
| 1215 | send_keymap(struct wl_resource *resource, struct weston_xkb_info *xkb_info) |
| 1216 | { |
| 1217 | wl_keyboard_send_keymap(resource, |
| 1218 | WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, |
| 1219 | xkb_info->keymap_fd, |
| 1220 | xkb_info->keymap_size); |
| 1221 | } |
| 1222 | |
| 1223 | static void |
| 1224 | send_modifiers(struct wl_resource *resource, uint32_t serial, struct weston_keyboard *keyboard) |
| 1225 | { |
| 1226 | wl_keyboard_send_modifiers(resource, serial, |
| 1227 | keyboard->modifiers.mods_depressed, |
| 1228 | keyboard->modifiers.mods_latched, |
| 1229 | keyboard->modifiers.mods_locked, |
| 1230 | keyboard->modifiers.group); |
| 1231 | } |
| 1232 | |
| 1233 | static struct weston_xkb_info * |
| 1234 | weston_xkb_info_create(struct xkb_keymap *keymap); |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1235 | |
| 1236 | static void |
| 1237 | update_keymap(struct weston_seat *seat) |
| 1238 | { |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1239 | struct weston_keyboard *keyboard = seat->keyboard; |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1240 | struct wl_resource *resource; |
| 1241 | struct weston_xkb_info *xkb_info; |
| 1242 | struct xkb_state *state; |
| 1243 | xkb_mod_mask_t latched_mods; |
| 1244 | xkb_mod_mask_t locked_mods; |
| 1245 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1246 | xkb_info = weston_xkb_info_create(keyboard->pending_keymap); |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1247 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1248 | xkb_keymap_unref(keyboard->pending_keymap); |
| 1249 | keyboard->pending_keymap = NULL; |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1250 | |
| 1251 | if (!xkb_info) { |
| 1252 | weston_log("failed to create XKB info\n"); |
| 1253 | return; |
| 1254 | } |
| 1255 | |
| 1256 | state = xkb_state_new(xkb_info->keymap); |
| 1257 | if (!state) { |
| 1258 | weston_log("failed to initialise XKB state\n"); |
| 1259 | weston_xkb_info_destroy(xkb_info); |
| 1260 | return; |
| 1261 | } |
| 1262 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1263 | latched_mods = xkb_state_serialize_mods(keyboard->xkb_state.state, |
| 1264 | XKB_STATE_MODS_LATCHED); |
| 1265 | locked_mods = xkb_state_serialize_mods(keyboard->xkb_state.state, |
| 1266 | XKB_STATE_MODS_LOCKED); |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1267 | xkb_state_update_mask(state, |
| 1268 | 0, /* depressed */ |
| 1269 | latched_mods, |
| 1270 | locked_mods, |
| 1271 | 0, 0, 0); |
| 1272 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1273 | weston_xkb_info_destroy(keyboard->xkb_info); |
| 1274 | keyboard->xkb_info = xkb_info; |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1275 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1276 | xkb_state_unref(keyboard->xkb_state.state); |
| 1277 | keyboard->xkb_state.state = state; |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1278 | |
| 1279 | wl_resource_for_each(resource, &seat->keyboard->resource_list) |
| 1280 | send_keymap(resource, xkb_info); |
| 1281 | wl_resource_for_each(resource, &seat->keyboard->focus_resource_list) |
| 1282 | send_keymap(resource, xkb_info); |
| 1283 | |
| 1284 | notify_modifiers(seat, wl_display_next_serial(seat->compositor->wl_display)); |
| 1285 | |
| 1286 | if (!latched_mods && !locked_mods) |
| 1287 | return; |
| 1288 | |
| 1289 | wl_resource_for_each(resource, &seat->keyboard->resource_list) |
| 1290 | send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard); |
| 1291 | wl_resource_for_each(resource, &seat->keyboard->focus_resource_list) |
| 1292 | send_modifiers(resource, wl_display_get_serial(seat->compositor->wl_display), seat->keyboard); |
| 1293 | } |
Rob Bradford | 382ff46 | 2013-06-24 16:52:45 +0100 | [diff] [blame] | 1294 | #else |
| 1295 | WL_EXPORT void |
| 1296 | notify_modifiers(struct weston_seat *seat, uint32_t serial) |
| 1297 | { |
| 1298 | } |
| 1299 | |
| 1300 | static void |
| 1301 | update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key, |
| 1302 | enum wl_keyboard_key_state state) |
| 1303 | { |
| 1304 | } |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1305 | |
| 1306 | static void |
| 1307 | update_keymap(struct weston_seat *seat) |
| 1308 | { |
| 1309 | } |
Rob Bradford | 382ff46 | 2013-06-24 16:52:45 +0100 | [diff] [blame] | 1310 | #endif |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1311 | |
| 1312 | WL_EXPORT void |
| 1313 | notify_key(struct weston_seat *seat, uint32_t time, uint32_t key, |
| 1314 | enum wl_keyboard_key_state state, |
| 1315 | enum weston_key_state_update update_state) |
| 1316 | { |
| 1317 | struct weston_compositor *compositor = seat->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1318 | struct weston_keyboard *keyboard = seat->keyboard; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1319 | struct weston_keyboard_grab *grab = keyboard->grab; |
Pekka Paalanen | 86b5396 | 2014-11-19 13:43:32 +0200 | [diff] [blame] | 1320 | uint32_t *k, *end; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1321 | |
| 1322 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1323 | weston_compositor_idle_inhibit(compositor); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1324 | } else { |
| 1325 | weston_compositor_idle_release(compositor); |
| 1326 | } |
| 1327 | |
Pekka Paalanen | 86b5396 | 2014-11-19 13:43:32 +0200 | [diff] [blame] | 1328 | end = keyboard->keys.data + keyboard->keys.size; |
| 1329 | for (k = keyboard->keys.data; k < end; k++) { |
| 1330 | if (*k == key) { |
| 1331 | /* Ignore server-generated repeats. */ |
| 1332 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) |
| 1333 | return; |
| 1334 | *k = *--end; |
| 1335 | } |
| 1336 | } |
| 1337 | keyboard->keys.size = (void *) end - keyboard->keys.data; |
| 1338 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 1339 | k = wl_array_add(&keyboard->keys, sizeof *k); |
| 1340 | *k = key; |
| 1341 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1342 | |
| 1343 | if (grab == &keyboard->default_grab || |
| 1344 | grab == &keyboard->input_method_grab) { |
Pekka Paalanen | 86b5396 | 2014-11-19 13:43:32 +0200 | [diff] [blame] | 1345 | weston_compositor_run_key_binding(compositor, seat, time, key, |
| 1346 | state); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1347 | grab = keyboard->grab; |
| 1348 | } |
| 1349 | |
| 1350 | grab->interface->key(grab, time, key, state); |
| 1351 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1352 | if (keyboard->pending_keymap && |
Pekka Paalanen | 86b5396 | 2014-11-19 13:43:32 +0200 | [diff] [blame] | 1353 | keyboard->keys.size == 0) |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 1354 | update_keymap(seat); |
| 1355 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1356 | if (update_state == STATE_UPDATE_AUTOMATIC) { |
| 1357 | update_modifier_state(seat, |
| 1358 | wl_display_get_serial(compositor->wl_display), |
| 1359 | key, |
| 1360 | state); |
| 1361 | } |
Giulio Camuffo | b6ddf6c | 2015-02-06 19:06:54 +0200 | [diff] [blame] | 1362 | |
| 1363 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 1364 | keyboard->grab_serial = |
| 1365 | wl_display_get_serial(compositor->wl_display); |
| 1366 | keyboard->grab_time = time; |
| 1367 | keyboard->grab_key = key; |
| 1368 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | WL_EXPORT void |
| 1372 | notify_pointer_focus(struct weston_seat *seat, struct weston_output *output, |
| 1373 | wl_fixed_t x, wl_fixed_t y) |
| 1374 | { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1375 | if (output) { |
Giulio Camuffo | 1959ab8 | 2013-11-14 23:42:52 +0100 | [diff] [blame] | 1376 | weston_pointer_move(seat->pointer, x, y); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1377 | } else { |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 1378 | /* FIXME: We should call weston_pointer_set_focus(seat, |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1379 | * NULL) here, but somehow that breaks re-entry... */ |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | static void |
| 1384 | destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data) |
| 1385 | { |
| 1386 | struct weston_seat *ws; |
| 1387 | |
| 1388 | ws = container_of(listener, struct weston_seat, |
| 1389 | saved_kbd_focus_listener); |
| 1390 | |
| 1391 | ws->saved_kbd_focus = NULL; |
| 1392 | } |
| 1393 | |
| 1394 | WL_EXPORT void |
| 1395 | notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys, |
| 1396 | enum weston_key_state_update update_state) |
| 1397 | { |
| 1398 | struct weston_compositor *compositor = seat->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1399 | struct weston_keyboard *keyboard = seat->keyboard; |
Kristian Høgsberg | fe7aa90 | 2013-05-08 09:54:37 -0400 | [diff] [blame] | 1400 | struct weston_surface *surface; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1401 | uint32_t *k, serial; |
| 1402 | |
| 1403 | serial = wl_display_next_serial(compositor->wl_display); |
| 1404 | wl_array_copy(&keyboard->keys, keys); |
| 1405 | wl_array_for_each(k, &keyboard->keys) { |
| 1406 | weston_compositor_idle_inhibit(compositor); |
| 1407 | if (update_state == STATE_UPDATE_AUTOMATIC) |
| 1408 | update_modifier_state(seat, serial, *k, |
| 1409 | WL_KEYBOARD_KEY_STATE_PRESSED); |
| 1410 | } |
| 1411 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1412 | surface = seat->saved_kbd_focus; |
| 1413 | |
| 1414 | if (surface) { |
| 1415 | wl_list_remove(&seat->saved_kbd_focus_listener.link); |
| 1416 | weston_keyboard_set_focus(keyboard, surface); |
| 1417 | seat->saved_kbd_focus = NULL; |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | WL_EXPORT void |
| 1422 | notify_keyboard_focus_out(struct weston_seat *seat) |
| 1423 | { |
| 1424 | struct weston_compositor *compositor = seat->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1425 | struct weston_keyboard *keyboard = seat->keyboard; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1426 | uint32_t *k, serial; |
| 1427 | |
| 1428 | serial = wl_display_next_serial(compositor->wl_display); |
| 1429 | wl_array_for_each(k, &keyboard->keys) { |
| 1430 | weston_compositor_idle_release(compositor); |
| 1431 | update_modifier_state(seat, serial, *k, |
| 1432 | WL_KEYBOARD_KEY_STATE_RELEASED); |
| 1433 | } |
| 1434 | |
| 1435 | seat->modifier_state = 0; |
| 1436 | |
| 1437 | if (keyboard->focus) { |
| 1438 | seat->saved_kbd_focus = keyboard->focus; |
| 1439 | seat->saved_kbd_focus_listener.notify = |
| 1440 | destroy_device_saved_kbd_focus; |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 1441 | wl_signal_add(&keyboard->focus->destroy_signal, |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1442 | &seat->saved_kbd_focus_listener); |
| 1443 | } |
| 1444 | |
| 1445 | weston_keyboard_set_focus(keyboard, NULL); |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 1446 | weston_keyboard_cancel_grab(keyboard); |
Kristian Høgsberg | ebd5fd4 | 2013-11-22 21:12:19 -0800 | [diff] [blame] | 1447 | if (seat->pointer) |
| 1448 | weston_pointer_cancel_grab(seat->pointer); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1449 | } |
| 1450 | |
Michael Fu | a2bb791 | 2013-07-23 15:51:06 +0800 | [diff] [blame] | 1451 | WL_EXPORT void |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1452 | weston_touch_set_focus(struct weston_seat *seat, struct weston_view *view) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1453 | { |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1454 | struct wl_list *focus_resource_list; |
| 1455 | |
| 1456 | focus_resource_list = &seat->touch->focus_resource_list; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1457 | |
Kristian Høgsberg | c3244d7 | 2013-10-24 14:21:53 -0700 | [diff] [blame] | 1458 | if (view && seat->touch->focus && |
| 1459 | seat->touch->focus->surface == view->surface) { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1460 | seat->touch->focus = view; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1461 | return; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1462 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1463 | |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 1464 | wl_list_remove(&seat->touch->focus_resource_listener.link); |
| 1465 | wl_list_init(&seat->touch->focus_resource_listener.link); |
| 1466 | wl_list_remove(&seat->touch->focus_view_listener.link); |
| 1467 | wl_list_init(&seat->touch->focus_view_listener.link); |
| 1468 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1469 | if (!wl_list_empty(focus_resource_list)) { |
| 1470 | move_resources(&seat->touch->resource_list, |
| 1471 | focus_resource_list); |
| 1472 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1473 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1474 | if (view) { |
Derek Foreman | 362656b | 2014-09-04 10:23:05 -0500 | [diff] [blame] | 1475 | struct wl_client *surface_client; |
| 1476 | |
| 1477 | if (!view->surface->resource) { |
| 1478 | seat->touch->focus = NULL; |
| 1479 | return; |
| 1480 | } |
| 1481 | |
| 1482 | surface_client = wl_resource_get_client(view->surface->resource); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1483 | move_resources_for_client(focus_resource_list, |
| 1484 | &seat->touch->resource_list, |
| 1485 | surface_client); |
Giulio Camuffo | 576fe2a | 2013-11-20 18:00:24 +0100 | [diff] [blame] | 1486 | wl_resource_add_destroy_listener(view->surface->resource, |
| 1487 | &seat->touch->focus_resource_listener); |
| 1488 | wl_signal_add(&view->destroy_signal, &seat->touch->focus_view_listener); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1489 | } |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1490 | seat->touch->focus = view; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | /** |
| 1494 | * notify_touch - emulates button touches and notifies surfaces accordingly. |
| 1495 | * |
| 1496 | * It assumes always the correct cycle sequence until it gets here: touch_down |
| 1497 | * → touch_update → ... → touch_update → touch_end. The driver is responsible |
| 1498 | * for sending along such order. |
| 1499 | * |
| 1500 | */ |
| 1501 | WL_EXPORT void |
| 1502 | notify_touch(struct weston_seat *seat, uint32_t time, int touch_id, |
| 1503 | wl_fixed_t x, wl_fixed_t y, int touch_type) |
| 1504 | { |
| 1505 | struct weston_compositor *ec = seat->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1506 | struct weston_touch *touch = seat->touch; |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 1507 | struct weston_touch_grab *grab = touch->grab; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1508 | struct weston_view *ev; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1509 | wl_fixed_t sx, sy; |
| 1510 | |
| 1511 | /* Update grab's global coordinates. */ |
Neil Roberts | 306fe08 | 2013-10-03 16:43:06 +0100 | [diff] [blame] | 1512 | if (touch_id == touch->grab_touch_id && touch_type != WL_TOUCH_UP) { |
| 1513 | touch->grab_x = x; |
| 1514 | touch->grab_y = y; |
| 1515 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1516 | |
| 1517 | switch (touch_type) { |
| 1518 | case WL_TOUCH_DOWN: |
| 1519 | weston_compositor_idle_inhibit(ec); |
| 1520 | |
Jonas Ådahl | 9484b69 | 2013-12-02 22:05:03 +0100 | [diff] [blame] | 1521 | touch->num_tp++; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1522 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1523 | /* the first finger down picks the view, and all further go |
| 1524 | * to that view for the remainder of the touch session i.e. |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1525 | * until all touch points are up again. */ |
Jonas Ådahl | 9484b69 | 2013-12-02 22:05:03 +0100 | [diff] [blame] | 1526 | if (touch->num_tp == 1) { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1527 | ev = weston_compositor_pick_view(ec, x, y, &sx, &sy); |
| 1528 | weston_touch_set_focus(seat, ev); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1529 | } else if (touch->focus) { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1530 | ev = touch->focus; |
| 1531 | weston_view_from_global_fixed(ev, x, y, &sx, &sy); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1532 | } else { |
| 1533 | /* Unexpected condition: We have non-initial touch but |
| 1534 | * there is no focused surface. |
| 1535 | */ |
| 1536 | weston_log("touch event received with %d points down" |
Jonas Ådahl | 9484b69 | 2013-12-02 22:05:03 +0100 | [diff] [blame] | 1537 | "but no surface focused\n", touch->num_tp); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1538 | return; |
| 1539 | } |
| 1540 | |
Kristian Høgsberg | c896401 | 2014-02-05 14:25:18 -0800 | [diff] [blame] | 1541 | weston_compositor_run_touch_binding(ec, seat, |
| 1542 | time, touch_type); |
| 1543 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1544 | grab->interface->down(grab, time, touch_id, sx, sy); |
Jonas Ådahl | 9484b69 | 2013-12-02 22:05:03 +0100 | [diff] [blame] | 1545 | if (touch->num_tp == 1) { |
Rusty Lynch | f1407ff | 2013-08-08 21:13:57 -0700 | [diff] [blame] | 1546 | touch->grab_serial = |
| 1547 | wl_display_get_serial(ec->wl_display); |
Neil Roberts | 306fe08 | 2013-10-03 16:43:06 +0100 | [diff] [blame] | 1548 | touch->grab_touch_id = touch_id; |
Rusty Lynch | f1407ff | 2013-08-08 21:13:57 -0700 | [diff] [blame] | 1549 | touch->grab_time = time; |
| 1550 | touch->grab_x = x; |
| 1551 | touch->grab_y = y; |
| 1552 | } |
| 1553 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1554 | break; |
| 1555 | case WL_TOUCH_MOTION: |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1556 | ev = touch->focus; |
| 1557 | if (!ev) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1558 | break; |
| 1559 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1560 | weston_view_from_global_fixed(ev, x, y, &sx, &sy); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1561 | grab->interface->motion(grab, time, touch_id, sx, sy); |
| 1562 | break; |
| 1563 | case WL_TOUCH_UP: |
Kristian Høgsberg | a30e29a | 2014-01-08 22:29:20 -0800 | [diff] [blame] | 1564 | if (touch->num_tp == 0) { |
| 1565 | /* This can happen if we start out with one or |
| 1566 | * more fingers on the touch screen, in which |
| 1567 | * case we didn't get the corresponding down |
| 1568 | * event. */ |
| 1569 | weston_log("unmatched touch up event\n"); |
| 1570 | break; |
| 1571 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1572 | weston_compositor_idle_release(ec); |
Jonas Ådahl | 9484b69 | 2013-12-02 22:05:03 +0100 | [diff] [blame] | 1573 | touch->num_tp--; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1574 | |
| 1575 | grab->interface->up(grab, time, touch_id); |
Jonas Ådahl | 9484b69 | 2013-12-02 22:05:03 +0100 | [diff] [blame] | 1576 | if (touch->num_tp == 0) |
Michael Fu | a2bb791 | 2013-07-23 15:51:06 +0800 | [diff] [blame] | 1577 | weston_touch_set_focus(seat, NULL); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1578 | break; |
| 1579 | } |
| 1580 | } |
| 1581 | |
Jonas Ådahl | 1679f23 | 2014-04-12 09:39:51 +0200 | [diff] [blame] | 1582 | WL_EXPORT void |
| 1583 | notify_touch_frame(struct weston_seat *seat) |
| 1584 | { |
| 1585 | struct weston_touch *touch = seat->touch; |
| 1586 | struct weston_touch_grab *grab = touch->grab; |
| 1587 | |
| 1588 | grab->interface->frame(grab); |
| 1589 | } |
| 1590 | |
Pekka Paalanen | 8274d90 | 2014-08-06 19:36:51 +0300 | [diff] [blame] | 1591 | static int |
| 1592 | pointer_cursor_surface_get_label(struct weston_surface *surface, |
| 1593 | char *buf, size_t len) |
| 1594 | { |
| 1595 | return snprintf(buf, len, "cursor"); |
| 1596 | } |
| 1597 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1598 | static void |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1599 | pointer_cursor_surface_configure(struct weston_surface *es, |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 1600 | int32_t dx, int32_t dy) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1601 | { |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 1602 | struct weston_pointer *pointer = es->configure_private; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1603 | int x, y; |
| 1604 | |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 1605 | if (es->width == 0) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1606 | return; |
| 1607 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1608 | assert(es == pointer->sprite->surface); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1609 | |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 1610 | pointer->hotspot_x -= dx; |
| 1611 | pointer->hotspot_y -= dy; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1612 | |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 1613 | x = wl_fixed_to_int(pointer->x) - pointer->hotspot_x; |
| 1614 | y = wl_fixed_to_int(pointer->y) - pointer->hotspot_y; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1615 | |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 1616 | weston_view_set_position(pointer->sprite, x, y); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1617 | |
| 1618 | empty_region(&es->pending.input); |
Ander Conselvan de Oliveira | 23900f7 | 2014-01-31 16:07:51 +0200 | [diff] [blame] | 1619 | empty_region(&es->input); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1620 | |
| 1621 | if (!weston_surface_is_mapped(es)) { |
Giulio Camuffo | 412e6a5 | 2014-07-09 22:12:56 +0300 | [diff] [blame] | 1622 | weston_layer_entry_insert(&es->compositor->cursor_layer.view_list, |
| 1623 | &pointer->sprite->layer_link); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1624 | weston_view_update_transform(pointer->sprite); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | static void |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1629 | pointer_set_cursor(struct wl_client *client, struct wl_resource *resource, |
| 1630 | uint32_t serial, struct wl_resource *surface_resource, |
| 1631 | int32_t x, int32_t y) |
| 1632 | { |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 1633 | struct weston_pointer *pointer = wl_resource_get_user_data(resource); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1634 | struct weston_surface *surface = NULL; |
| 1635 | |
| 1636 | if (surface_resource) |
Jason Ekstrand | 0f2ef7e | 2013-06-14 10:07:53 -0500 | [diff] [blame] | 1637 | surface = wl_resource_get_user_data(surface_resource); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1638 | |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 1639 | if (pointer->focus == NULL) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1640 | return; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1641 | /* pointer->focus->surface->resource can be NULL. Surfaces like the |
Giulio Camuffo | 1fd4b01 | 2013-06-20 18:13:07 +0200 | [diff] [blame] | 1642 | black_surface used in shell.c for fullscreen don't have |
| 1643 | a resource, but can still have focus */ |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1644 | if (pointer->focus->surface->resource == NULL) |
Giulio Camuffo | 1fd4b01 | 2013-06-20 18:13:07 +0200 | [diff] [blame] | 1645 | return; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1646 | if (wl_resource_get_client(pointer->focus->surface->resource) != client) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1647 | return; |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 1648 | if (pointer->focus_serial - serial > UINT32_MAX / 2) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1649 | return; |
| 1650 | |
Derek Foreman | 4e53c53 | 2015-03-23 10:55:32 -0500 | [diff] [blame^] | 1651 | if (!surface) { |
| 1652 | if (pointer->sprite) |
| 1653 | pointer_unmap_sprite(pointer); |
| 1654 | return; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1655 | } |
| 1656 | |
Jonas Ådahl | b407024 | 2015-03-18 15:08:03 +0800 | [diff] [blame] | 1657 | if (pointer->sprite && pointer->sprite->surface == surface && |
| 1658 | pointer->hotspot_x == x && pointer->hotspot_y == y) |
| 1659 | return; |
| 1660 | |
Derek Foreman | 4e53c53 | 2015-03-23 10:55:32 -0500 | [diff] [blame^] | 1661 | if (!pointer->sprite || pointer->sprite->surface != surface) { |
| 1662 | if (weston_surface_set_role(surface, "wl_pointer-cursor", |
| 1663 | resource, |
| 1664 | WL_POINTER_ERROR_ROLE) < 0) |
| 1665 | return; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1666 | |
Derek Foreman | 4e53c53 | 2015-03-23 10:55:32 -0500 | [diff] [blame^] | 1667 | if (pointer->sprite) |
| 1668 | pointer_unmap_sprite(pointer); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1669 | |
Derek Foreman | 4e53c53 | 2015-03-23 10:55:32 -0500 | [diff] [blame^] | 1670 | wl_signal_add(&surface->destroy_signal, |
| 1671 | &pointer->sprite_destroy_listener); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1672 | |
Derek Foreman | 4e53c53 | 2015-03-23 10:55:32 -0500 | [diff] [blame^] | 1673 | surface->configure = pointer_cursor_surface_configure; |
| 1674 | surface->configure_private = pointer; |
| 1675 | weston_surface_set_label_func(surface, |
| 1676 | pointer_cursor_surface_get_label); |
| 1677 | pointer->sprite = weston_view_create(surface); |
| 1678 | } |
| 1679 | |
Kristian Høgsberg | 195b869 | 2013-05-08 15:02:05 -0400 | [diff] [blame] | 1680 | pointer->hotspot_x = x; |
| 1681 | pointer->hotspot_y = y; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1682 | |
Jonas Ådahl | 16fe4dc | 2014-09-08 19:33:41 +0200 | [diff] [blame] | 1683 | if (surface->buffer_ref.buffer) { |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 1684 | pointer_cursor_surface_configure(surface, 0, 0); |
Jonas Ådahl | 16fe4dc | 2014-09-08 19:33:41 +0200 | [diff] [blame] | 1685 | weston_view_schedule_repaint(pointer->sprite); |
| 1686 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1687 | } |
| 1688 | |
Kristian Høgsberg | 69e25fc | 2013-08-13 20:11:02 +0100 | [diff] [blame] | 1689 | static void |
| 1690 | pointer_release(struct wl_client *client, struct wl_resource *resource) |
| 1691 | { |
| 1692 | wl_resource_destroy(resource); |
| 1693 | } |
| 1694 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1695 | static const struct wl_pointer_interface pointer_interface = { |
Kristian Høgsberg | 69e25fc | 2013-08-13 20:11:02 +0100 | [diff] [blame] | 1696 | pointer_set_cursor, |
| 1697 | pointer_release |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1698 | }; |
| 1699 | |
| 1700 | static void |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1701 | seat_get_pointer(struct wl_client *client, struct wl_resource *resource, |
| 1702 | uint32_t id) |
| 1703 | { |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 1704 | struct weston_seat *seat = wl_resource_get_user_data(resource); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1705 | struct wl_resource *cr; |
| 1706 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1707 | if (!seat->pointer) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1708 | return; |
| 1709 | |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 1710 | cr = wl_resource_create(client, &wl_pointer_interface, |
| 1711 | wl_resource_get_version(resource), id); |
Kristian Høgsberg | 0ff7908 | 2013-08-06 16:46:25 -0700 | [diff] [blame] | 1712 | if (cr == NULL) { |
| 1713 | wl_client_post_no_memory(client); |
| 1714 | return; |
| 1715 | } |
| 1716 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1717 | /* May be moved to focused list later by either |
| 1718 | * weston_pointer_set_focus or directly if this client is already |
| 1719 | * focused */ |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 1720 | wl_list_insert(&seat->pointer->resource_list, wl_resource_get_link(cr)); |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 1721 | wl_resource_set_implementation(cr, &pointer_interface, seat->pointer, |
| 1722 | unbind_resource); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1723 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1724 | if (seat->pointer->focus && seat->pointer->focus->surface->resource && |
| 1725 | wl_resource_get_client(seat->pointer->focus->surface->resource) == client) { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1726 | wl_fixed_t sx, sy; |
| 1727 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1728 | weston_view_from_global_fixed(seat->pointer->focus, |
| 1729 | seat->pointer->x, |
| 1730 | seat->pointer->y, |
| 1731 | &sx, &sy); |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1732 | |
| 1733 | wl_list_remove(wl_resource_get_link(cr)); |
| 1734 | wl_list_insert(&seat->pointer->focus_resource_list, |
| 1735 | wl_resource_get_link(cr)); |
| 1736 | wl_pointer_send_enter(cr, |
| 1737 | seat->pointer->focus_serial, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1738 | seat->pointer->focus->surface->resource, |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1739 | sx, sy); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | static void |
Kristian Høgsberg | 69e25fc | 2013-08-13 20:11:02 +0100 | [diff] [blame] | 1744 | keyboard_release(struct wl_client *client, struct wl_resource *resource) |
| 1745 | { |
| 1746 | wl_resource_destroy(resource); |
| 1747 | } |
| 1748 | |
| 1749 | static const struct wl_keyboard_interface keyboard_interface = { |
| 1750 | keyboard_release |
| 1751 | }; |
| 1752 | |
Derek Foreman | 280e7dd | 2014-10-03 13:13:42 -0500 | [diff] [blame] | 1753 | static bool |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1754 | should_send_modifiers_to_client(struct weston_seat *seat, |
| 1755 | struct wl_client *client) |
| 1756 | { |
| 1757 | if (seat->keyboard && |
| 1758 | seat->keyboard->focus && |
Jason Ekstrand | 42133d4 | 2013-11-14 20:06:16 -0600 | [diff] [blame] | 1759 | seat->keyboard->focus->resource && |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1760 | wl_resource_get_client(seat->keyboard->focus->resource) == client) |
Derek Foreman | 280e7dd | 2014-10-03 13:13:42 -0500 | [diff] [blame] | 1761 | return true; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1762 | |
| 1763 | if (seat->pointer && |
| 1764 | seat->pointer->focus && |
Jason Ekstrand | 42133d4 | 2013-11-14 20:06:16 -0600 | [diff] [blame] | 1765 | seat->pointer->focus->surface->resource && |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1766 | wl_resource_get_client(seat->pointer->focus->surface->resource) == client) |
Derek Foreman | 280e7dd | 2014-10-03 13:13:42 -0500 | [diff] [blame] | 1767 | return true; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1768 | |
Derek Foreman | 280e7dd | 2014-10-03 13:13:42 -0500 | [diff] [blame] | 1769 | return false; |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1770 | } |
| 1771 | |
Kristian Høgsberg | 69e25fc | 2013-08-13 20:11:02 +0100 | [diff] [blame] | 1772 | static void |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1773 | seat_get_keyboard(struct wl_client *client, struct wl_resource *resource, |
| 1774 | uint32_t id) |
| 1775 | { |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 1776 | struct weston_seat *seat = wl_resource_get_user_data(resource); |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1777 | struct weston_keyboard *keyboard = seat->keyboard; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1778 | struct wl_resource *cr; |
| 1779 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1780 | if (!seat->keyboard) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1781 | return; |
| 1782 | |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 1783 | cr = wl_resource_create(client, &wl_keyboard_interface, |
| 1784 | wl_resource_get_version(resource), id); |
Kristian Høgsberg | 0ff7908 | 2013-08-06 16:46:25 -0700 | [diff] [blame] | 1785 | if (cr == NULL) { |
| 1786 | wl_client_post_no_memory(client); |
| 1787 | return; |
| 1788 | } |
| 1789 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1790 | /* May be moved to focused list later by either |
| 1791 | * weston_keyboard_set_focus or directly if this client is already |
| 1792 | * focused */ |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 1793 | wl_list_insert(&seat->keyboard->resource_list, wl_resource_get_link(cr)); |
Kristian Høgsberg | 69e25fc | 2013-08-13 20:11:02 +0100 | [diff] [blame] | 1794 | wl_resource_set_implementation(cr, &keyboard_interface, |
| 1795 | seat, unbind_resource); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1796 | |
Jonny Lamb | 66a41a0 | 2014-08-12 14:58:25 +0200 | [diff] [blame] | 1797 | if (wl_resource_get_version(cr) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) { |
| 1798 | wl_keyboard_send_repeat_info(cr, |
| 1799 | seat->compositor->kb_repeat_rate, |
| 1800 | seat->compositor->kb_repeat_delay); |
| 1801 | } |
Jasper St. Pierre | d8c6aeb | 2014-08-04 13:43:24 -0400 | [diff] [blame] | 1802 | |
Matt Roper | 01a9273 | 2013-06-24 16:52:44 +0100 | [diff] [blame] | 1803 | if (seat->compositor->use_xkbcommon) { |
| 1804 | wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1805 | keyboard->xkb_info->keymap_fd, |
| 1806 | keyboard->xkb_info->keymap_size); |
Matt Roper | 01a9273 | 2013-06-24 16:52:44 +0100 | [diff] [blame] | 1807 | } else { |
| 1808 | int null_fd = open("/dev/null", O_RDONLY); |
| 1809 | wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP, |
| 1810 | null_fd, |
| 1811 | 0); |
| 1812 | close(null_fd); |
| 1813 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1814 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1815 | if (should_send_modifiers_to_client(seat, client)) { |
| 1816 | send_modifiers_to_resource(seat->keyboard, |
| 1817 | cr, |
| 1818 | seat->keyboard->focus_serial); |
| 1819 | } |
| 1820 | |
Giulio Camuffo | de77e32 | 2014-10-10 23:46:50 +0300 | [diff] [blame] | 1821 | if (seat->keyboard->focus && seat->keyboard->focus->resource && |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 1822 | wl_resource_get_client(seat->keyboard->focus->resource) == client) { |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1823 | struct weston_surface *surface = |
| 1824 | (struct weston_surface *) seat->keyboard->focus; |
| 1825 | |
| 1826 | wl_list_remove(wl_resource_get_link(cr)); |
| 1827 | wl_list_insert(&seat->keyboard->focus_resource_list, |
| 1828 | wl_resource_get_link(cr)); |
| 1829 | wl_keyboard_send_enter(cr, |
| 1830 | seat->keyboard->focus_serial, |
| 1831 | surface->resource, |
| 1832 | &seat->keyboard->keys); |
| 1833 | |
| 1834 | /* If this is the first keyboard resource for this |
| 1835 | * client... */ |
| 1836 | if (seat->keyboard->focus_resource_list.prev == |
| 1837 | wl_resource_get_link(cr)) |
| 1838 | wl_data_device_set_keyboard_focus(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | static void |
Kristian Høgsberg | 69e25fc | 2013-08-13 20:11:02 +0100 | [diff] [blame] | 1843 | touch_release(struct wl_client *client, struct wl_resource *resource) |
| 1844 | { |
| 1845 | wl_resource_destroy(resource); |
| 1846 | } |
| 1847 | |
| 1848 | static const struct wl_touch_interface touch_interface = { |
| 1849 | touch_release |
| 1850 | }; |
| 1851 | |
| 1852 | static void |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1853 | seat_get_touch(struct wl_client *client, struct wl_resource *resource, |
| 1854 | uint32_t id) |
| 1855 | { |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 1856 | struct weston_seat *seat = wl_resource_get_user_data(resource); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1857 | struct wl_resource *cr; |
| 1858 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1859 | if (!seat->touch) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1860 | return; |
| 1861 | |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 1862 | cr = wl_resource_create(client, &wl_touch_interface, |
| 1863 | wl_resource_get_version(resource), id); |
Kristian Høgsberg | 0ff7908 | 2013-08-06 16:46:25 -0700 | [diff] [blame] | 1864 | if (cr == NULL) { |
| 1865 | wl_client_post_no_memory(client); |
| 1866 | return; |
| 1867 | } |
| 1868 | |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1869 | if (seat->touch->focus && |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1870 | wl_resource_get_client(seat->touch->focus->surface->resource) == client) { |
Neil Roberts | 96d790e | 2013-09-19 17:32:00 +0100 | [diff] [blame] | 1871 | wl_list_insert(&seat->touch->resource_list, |
| 1872 | wl_resource_get_link(cr)); |
| 1873 | } else { |
| 1874 | wl_list_insert(&seat->touch->focus_resource_list, |
| 1875 | wl_resource_get_link(cr)); |
| 1876 | } |
Kristian Høgsberg | 69e25fc | 2013-08-13 20:11:02 +0100 | [diff] [blame] | 1877 | wl_resource_set_implementation(cr, &touch_interface, |
| 1878 | seat, unbind_resource); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | static const struct wl_seat_interface seat_interface = { |
| 1882 | seat_get_pointer, |
| 1883 | seat_get_keyboard, |
| 1884 | seat_get_touch, |
| 1885 | }; |
| 1886 | |
| 1887 | static void |
| 1888 | bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id) |
| 1889 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1890 | struct weston_seat *seat = data; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1891 | struct wl_resource *resource; |
| 1892 | enum wl_seat_capability caps = 0; |
| 1893 | |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 1894 | resource = wl_resource_create(client, |
Jonny Lamb | 66a41a0 | 2014-08-12 14:58:25 +0200 | [diff] [blame] | 1895 | &wl_seat_interface, MIN(version, 4), id); |
Jason Ekstrand | 44a3863 | 2013-06-14 10:08:00 -0500 | [diff] [blame] | 1896 | wl_list_insert(&seat->base_resource_list, wl_resource_get_link(resource)); |
Jason Ekstrand | a85118c | 2013-06-27 20:17:02 -0500 | [diff] [blame] | 1897 | wl_resource_set_implementation(resource, &seat_interface, data, |
| 1898 | unbind_resource); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1899 | |
| 1900 | if (seat->pointer) |
| 1901 | caps |= WL_SEAT_CAPABILITY_POINTER; |
| 1902 | if (seat->keyboard) |
| 1903 | caps |= WL_SEAT_CAPABILITY_KEYBOARD; |
| 1904 | if (seat->touch) |
| 1905 | caps |= WL_SEAT_CAPABILITY_TOUCH; |
| 1906 | |
| 1907 | wl_seat_send_capabilities(resource, caps); |
Jasper St. Pierre | 0013a29 | 2014-08-07 16:43:11 -0400 | [diff] [blame] | 1908 | if (version >= WL_SEAT_NAME_SINCE_VERSION) |
Rob Bradford | e445ae6 | 2013-05-31 18:09:51 +0100 | [diff] [blame] | 1909 | wl_seat_send_name(resource, seat->seat_name); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1910 | } |
| 1911 | |
Rob Bradford | 382ff46 | 2013-06-24 16:52:45 +0100 | [diff] [blame] | 1912 | #ifdef ENABLE_XKBCOMMON |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1913 | int |
| 1914 | weston_compositor_xkb_init(struct weston_compositor *ec, |
| 1915 | struct xkb_rule_names *names) |
| 1916 | { |
Rob Bradford | 382ff46 | 2013-06-24 16:52:45 +0100 | [diff] [blame] | 1917 | ec->use_xkbcommon = 1; |
Matt Roper | 01a9273 | 2013-06-24 16:52:44 +0100 | [diff] [blame] | 1918 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1919 | if (ec->xkb_context == NULL) { |
| 1920 | ec->xkb_context = xkb_context_new(0); |
| 1921 | if (ec->xkb_context == NULL) { |
| 1922 | weston_log("failed to create XKB context\n"); |
| 1923 | return -1; |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | if (names) |
| 1928 | ec->xkb_names = *names; |
| 1929 | if (!ec->xkb_names.rules) |
| 1930 | ec->xkb_names.rules = strdup("evdev"); |
| 1931 | if (!ec->xkb_names.model) |
| 1932 | ec->xkb_names.model = strdup("pc105"); |
| 1933 | if (!ec->xkb_names.layout) |
| 1934 | ec->xkb_names.layout = strdup("us"); |
| 1935 | |
| 1936 | return 0; |
| 1937 | } |
| 1938 | |
Stefan Schmidt | fda2652 | 2013-09-17 10:54:09 +0100 | [diff] [blame] | 1939 | static void |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 1940 | weston_xkb_info_destroy(struct weston_xkb_info *xkb_info) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1941 | { |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 1942 | if (--xkb_info->ref_count > 0) |
| 1943 | return; |
| 1944 | |
Ran Benita | c9c7415 | 2014-08-19 23:59:52 +0300 | [diff] [blame] | 1945 | xkb_keymap_unref(xkb_info->keymap); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1946 | |
| 1947 | if (xkb_info->keymap_area) |
| 1948 | munmap(xkb_info->keymap_area, xkb_info->keymap_size); |
| 1949 | if (xkb_info->keymap_fd >= 0) |
| 1950 | close(xkb_info->keymap_fd); |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 1951 | free(xkb_info); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | void |
| 1955 | weston_compositor_xkb_destroy(struct weston_compositor *ec) |
| 1956 | { |
Matt Roper | 01a9273 | 2013-06-24 16:52:44 +0100 | [diff] [blame] | 1957 | /* |
| 1958 | * If we're operating in raw keyboard mode, we never initialized |
| 1959 | * libxkbcommon so there's no cleanup to do either. |
| 1960 | */ |
| 1961 | if (!ec->use_xkbcommon) |
| 1962 | return; |
| 1963 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1964 | free((char *) ec->xkb_names.rules); |
| 1965 | free((char *) ec->xkb_names.model); |
| 1966 | free((char *) ec->xkb_names.layout); |
| 1967 | free((char *) ec->xkb_names.variant); |
| 1968 | free((char *) ec->xkb_names.options); |
Stefan Schmidt | fda2652 | 2013-09-17 10:54:09 +0100 | [diff] [blame] | 1969 | |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 1970 | if (ec->xkb_info) |
| 1971 | weston_xkb_info_destroy(ec->xkb_info); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1972 | xkb_context_unref(ec->xkb_context); |
| 1973 | } |
| 1974 | |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 1975 | static struct weston_xkb_info * |
| 1976 | weston_xkb_info_create(struct xkb_keymap *keymap) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1977 | { |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 1978 | struct weston_xkb_info *xkb_info = zalloc(sizeof *xkb_info); |
| 1979 | if (xkb_info == NULL) |
| 1980 | return NULL; |
| 1981 | |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 1982 | xkb_info->keymap = xkb_keymap_ref(keymap); |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 1983 | xkb_info->ref_count = 1; |
| 1984 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1985 | char *keymap_str; |
| 1986 | |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 1987 | xkb_info->shift_mod = xkb_keymap_mod_get_index(xkb_info->keymap, |
| 1988 | XKB_MOD_NAME_SHIFT); |
| 1989 | xkb_info->caps_mod = xkb_keymap_mod_get_index(xkb_info->keymap, |
| 1990 | XKB_MOD_NAME_CAPS); |
| 1991 | xkb_info->ctrl_mod = xkb_keymap_mod_get_index(xkb_info->keymap, |
| 1992 | XKB_MOD_NAME_CTRL); |
| 1993 | xkb_info->alt_mod = xkb_keymap_mod_get_index(xkb_info->keymap, |
| 1994 | XKB_MOD_NAME_ALT); |
| 1995 | xkb_info->mod2_mod = xkb_keymap_mod_get_index(xkb_info->keymap, |
| 1996 | "Mod2"); |
| 1997 | xkb_info->mod3_mod = xkb_keymap_mod_get_index(xkb_info->keymap, |
| 1998 | "Mod3"); |
| 1999 | xkb_info->super_mod = xkb_keymap_mod_get_index(xkb_info->keymap, |
| 2000 | XKB_MOD_NAME_LOGO); |
| 2001 | xkb_info->mod5_mod = xkb_keymap_mod_get_index(xkb_info->keymap, |
| 2002 | "Mod5"); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2003 | |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 2004 | xkb_info->num_led = xkb_keymap_led_get_index(xkb_info->keymap, |
| 2005 | XKB_LED_NAME_NUM); |
| 2006 | xkb_info->caps_led = xkb_keymap_led_get_index(xkb_info->keymap, |
| 2007 | XKB_LED_NAME_CAPS); |
| 2008 | xkb_info->scroll_led = xkb_keymap_led_get_index(xkb_info->keymap, |
| 2009 | XKB_LED_NAME_SCROLL); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2010 | |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 2011 | keymap_str = xkb_keymap_get_as_string(xkb_info->keymap, |
| 2012 | XKB_KEYMAP_FORMAT_TEXT_V1); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2013 | if (keymap_str == NULL) { |
| 2014 | weston_log("failed to get string version of keymap\n"); |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2015 | goto err_keymap; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2016 | } |
| 2017 | xkb_info->keymap_size = strlen(keymap_str) + 1; |
| 2018 | |
| 2019 | xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size); |
| 2020 | if (xkb_info->keymap_fd < 0) { |
| 2021 | weston_log("creating a keymap file for %lu bytes failed: %m\n", |
| 2022 | (unsigned long) xkb_info->keymap_size); |
| 2023 | goto err_keymap_str; |
| 2024 | } |
| 2025 | |
| 2026 | xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size, |
| 2027 | PROT_READ | PROT_WRITE, |
| 2028 | MAP_SHARED, xkb_info->keymap_fd, 0); |
| 2029 | if (xkb_info->keymap_area == MAP_FAILED) { |
| 2030 | weston_log("failed to mmap() %lu bytes\n", |
| 2031 | (unsigned long) xkb_info->keymap_size); |
| 2032 | goto err_dev_zero; |
| 2033 | } |
| 2034 | strcpy(xkb_info->keymap_area, keymap_str); |
| 2035 | free(keymap_str); |
| 2036 | |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2037 | return xkb_info; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2038 | |
| 2039 | err_dev_zero: |
| 2040 | close(xkb_info->keymap_fd); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2041 | err_keymap_str: |
| 2042 | free(keymap_str); |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2043 | err_keymap: |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 2044 | xkb_keymap_unref(xkb_info->keymap); |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2045 | free(xkb_info); |
| 2046 | return NULL; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | static int |
| 2050 | weston_compositor_build_global_keymap(struct weston_compositor *ec) |
| 2051 | { |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2052 | struct xkb_keymap *keymap; |
| 2053 | |
| 2054 | if (ec->xkb_info != NULL) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2055 | return 0; |
| 2056 | |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 2057 | keymap = xkb_keymap_new_from_names(ec->xkb_context, |
| 2058 | &ec->xkb_names, |
| 2059 | 0); |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2060 | if (keymap == NULL) { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2061 | weston_log("failed to compile global XKB keymap\n"); |
| 2062 | weston_log(" tried rules %s, model %s, layout %s, variant %s, " |
| 2063 | "options %s\n", |
| 2064 | ec->xkb_names.rules, ec->xkb_names.model, |
| 2065 | ec->xkb_names.layout, ec->xkb_names.variant, |
| 2066 | ec->xkb_names.options); |
| 2067 | return -1; |
| 2068 | } |
| 2069 | |
Andrew Wedgbury | 9a6f02a | 2013-09-05 13:31:40 +0000 | [diff] [blame] | 2070 | ec->xkb_info = weston_xkb_info_create(keymap); |
Rui Matos | 73d9395 | 2013-10-24 19:28:41 +0200 | [diff] [blame] | 2071 | xkb_keymap_unref(keymap); |
Stefan Schmidt | fda2652 | 2013-09-17 10:54:09 +0100 | [diff] [blame] | 2072 | if (ec->xkb_info == NULL) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2073 | return -1; |
| 2074 | |
| 2075 | return 0; |
| 2076 | } |
Rob Bradford | 382ff46 | 2013-06-24 16:52:45 +0100 | [diff] [blame] | 2077 | #else |
| 2078 | int |
| 2079 | weston_compositor_xkb_init(struct weston_compositor *ec, |
| 2080 | struct xkb_rule_names *names) |
| 2081 | { |
| 2082 | return 0; |
| 2083 | } |
| 2084 | |
| 2085 | void |
| 2086 | weston_compositor_xkb_destroy(struct weston_compositor *ec) |
| 2087 | { |
| 2088 | } |
| 2089 | #endif |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2090 | |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 2091 | WL_EXPORT void |
| 2092 | weston_seat_update_keymap(struct weston_seat *seat, struct xkb_keymap *keymap) |
| 2093 | { |
| 2094 | if (!seat->keyboard || !keymap) |
| 2095 | return; |
| 2096 | |
| 2097 | #ifdef ENABLE_XKBCOMMON |
| 2098 | if (!seat->compositor->use_xkbcommon) |
| 2099 | return; |
| 2100 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 2101 | xkb_keymap_unref(seat->keyboard->pending_keymap); |
| 2102 | seat->keyboard->pending_keymap = xkb_keymap_ref(keymap); |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 2103 | |
Pekka Paalanen | 86b5396 | 2014-11-19 13:43:32 +0200 | [diff] [blame] | 2104 | if (seat->keyboard->keys.size == 0) |
Rui Matos | 65196bc | 2013-10-10 19:44:19 +0200 | [diff] [blame] | 2105 | update_keymap(seat); |
| 2106 | #endif |
| 2107 | } |
| 2108 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2109 | WL_EXPORT int |
| 2110 | weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap) |
| 2111 | { |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2112 | struct weston_keyboard *keyboard; |
| 2113 | |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2114 | if (seat->keyboard) { |
| 2115 | seat->keyboard_device_count += 1; |
| 2116 | if (seat->keyboard_device_count == 1) |
| 2117 | seat_send_updated_caps(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2118 | return 0; |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2119 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2120 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2121 | keyboard = weston_keyboard_create(); |
| 2122 | if (keyboard == NULL) { |
| 2123 | weston_log("failed to allocate weston keyboard struct\n"); |
| 2124 | return -1; |
| 2125 | } |
| 2126 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 2127 | #ifdef ENABLE_XKBCOMMON |
| 2128 | if (seat->compositor->use_xkbcommon) { |
| 2129 | if (keymap != NULL) { |
| 2130 | keyboard->xkb_info = weston_xkb_info_create(keymap); |
| 2131 | if (keyboard->xkb_info == NULL) |
Ander Conselvan de Oliveira | 4d363cf | 2014-01-31 17:35:45 +0200 | [diff] [blame] | 2132 | goto err; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 2133 | } else { |
| 2134 | if (weston_compositor_build_global_keymap(seat->compositor) < 0) |
Ander Conselvan de Oliveira | 4d363cf | 2014-01-31 17:35:45 +0200 | [diff] [blame] | 2135 | goto err; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 2136 | keyboard->xkb_info = seat->compositor->xkb_info; |
| 2137 | keyboard->xkb_info->ref_count++; |
| 2138 | } |
| 2139 | |
| 2140 | keyboard->xkb_state.state = xkb_state_new(keyboard->xkb_info->keymap); |
| 2141 | if (keyboard->xkb_state.state == NULL) { |
| 2142 | weston_log("failed to initialise XKB state\n"); |
Ander Conselvan de Oliveira | 4d363cf | 2014-01-31 17:35:45 +0200 | [diff] [blame] | 2143 | goto err; |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | keyboard->xkb_state.leds = 0; |
| 2147 | } |
| 2148 | #endif |
| 2149 | |
Ander Conselvan de Oliveira | 4d363cf | 2014-01-31 17:35:45 +0200 | [diff] [blame] | 2150 | seat->keyboard = keyboard; |
| 2151 | seat->keyboard_device_count = 1; |
| 2152 | keyboard->seat = seat; |
| 2153 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2154 | seat_send_updated_caps(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2155 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2156 | return 0; |
Ander Conselvan de Oliveira | 4d363cf | 2014-01-31 17:35:45 +0200 | [diff] [blame] | 2157 | |
| 2158 | err: |
| 2159 | if (keyboard->xkb_info) |
| 2160 | weston_xkb_info_destroy(keyboard->xkb_info); |
| 2161 | free(keyboard); |
| 2162 | |
| 2163 | return -1; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2164 | } |
| 2165 | |
Jonas Ådahl | 91fed54 | 2013-12-03 09:14:27 +0100 | [diff] [blame] | 2166 | static void |
| 2167 | weston_keyboard_reset_state(struct weston_keyboard *keyboard) |
| 2168 | { |
| 2169 | struct weston_seat *seat = keyboard->seat; |
| 2170 | struct xkb_state *state; |
| 2171 | |
| 2172 | #ifdef ENABLE_XKBCOMMON |
| 2173 | if (seat->compositor->use_xkbcommon) { |
| 2174 | state = xkb_state_new(keyboard->xkb_info->keymap); |
| 2175 | if (!state) { |
| 2176 | weston_log("failed to reset XKB state\n"); |
| 2177 | return; |
| 2178 | } |
| 2179 | xkb_state_unref(keyboard->xkb_state.state); |
| 2180 | keyboard->xkb_state.state = state; |
| 2181 | |
| 2182 | keyboard->xkb_state.leds = 0; |
| 2183 | } |
| 2184 | #endif |
| 2185 | |
| 2186 | seat->modifier_state = 0; |
| 2187 | } |
| 2188 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2189 | WL_EXPORT void |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2190 | weston_seat_release_keyboard(struct weston_seat *seat) |
| 2191 | { |
| 2192 | seat->keyboard_device_count--; |
Derek Foreman | d621df2 | 2014-11-19 11:04:12 -0600 | [diff] [blame] | 2193 | assert(seat->keyboard_device_count >= 0); |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2194 | if (seat->keyboard_device_count == 0) { |
Jonas Ådahl | 630bae8 | 2013-10-17 23:04:06 +0200 | [diff] [blame] | 2195 | weston_keyboard_set_focus(seat->keyboard, NULL); |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 2196 | weston_keyboard_cancel_grab(seat->keyboard); |
Jonas Ådahl | 91fed54 | 2013-12-03 09:14:27 +0100 | [diff] [blame] | 2197 | weston_keyboard_reset_state(seat->keyboard); |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2198 | seat_send_updated_caps(seat); |
| 2199 | } |
| 2200 | } |
| 2201 | |
| 2202 | WL_EXPORT void |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2203 | weston_seat_init_pointer(struct weston_seat *seat) |
| 2204 | { |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2205 | struct weston_pointer *pointer; |
| 2206 | |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2207 | if (seat->pointer) { |
| 2208 | seat->pointer_device_count += 1; |
| 2209 | if (seat->pointer_device_count == 1) |
| 2210 | seat_send_updated_caps(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2211 | return; |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2212 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2213 | |
Giulio Camuffo | cdb4d29 | 2013-11-14 23:42:53 +0100 | [diff] [blame] | 2214 | pointer = weston_pointer_create(seat); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2215 | if (pointer == NULL) |
| 2216 | return; |
| 2217 | |
| 2218 | seat->pointer = pointer; |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2219 | seat->pointer_device_count = 1; |
| 2220 | pointer->seat = seat; |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2221 | |
| 2222 | seat_send_updated_caps(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | WL_EXPORT void |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2226 | weston_seat_release_pointer(struct weston_seat *seat) |
| 2227 | { |
| 2228 | struct weston_pointer *pointer = seat->pointer; |
| 2229 | |
| 2230 | seat->pointer_device_count--; |
| 2231 | if (seat->pointer_device_count == 0) { |
Jonas Ådahl | 630bae8 | 2013-10-17 23:04:06 +0200 | [diff] [blame] | 2232 | weston_pointer_set_focus(pointer, NULL, |
| 2233 | wl_fixed_from_int(0), |
| 2234 | wl_fixed_from_int(0)); |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 2235 | weston_pointer_cancel_grab(pointer); |
Jonas Ådahl | 630bae8 | 2013-10-17 23:04:06 +0200 | [diff] [blame] | 2236 | |
Jonas Ådahl | a493274 | 2013-10-17 23:04:07 +0200 | [diff] [blame] | 2237 | if (pointer->sprite) |
| 2238 | pointer_unmap_sprite(pointer); |
| 2239 | |
Jonas Ådahl | 3e12e63 | 2013-12-02 22:05:05 +0100 | [diff] [blame] | 2240 | weston_pointer_reset_state(pointer); |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2241 | seat_send_updated_caps(seat); |
Derek Foreman | fd5ca51 | 2015-01-07 15:00:25 -0600 | [diff] [blame] | 2242 | |
| 2243 | /* seat->pointer is intentionally not destroyed so that |
| 2244 | * a newly attached pointer on this seat will retain |
| 2245 | * the previous cursor co-ordinates. |
| 2246 | */ |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | WL_EXPORT void |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2251 | weston_seat_init_touch(struct weston_seat *seat) |
| 2252 | { |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2253 | struct weston_touch *touch; |
| 2254 | |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2255 | if (seat->touch) { |
| 2256 | seat->touch_device_count += 1; |
| 2257 | if (seat->touch_device_count == 1) |
| 2258 | seat_send_updated_caps(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2259 | return; |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2260 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2261 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2262 | touch = weston_touch_create(); |
| 2263 | if (touch == NULL) |
| 2264 | return; |
| 2265 | |
| 2266 | seat->touch = touch; |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2267 | seat->touch_device_count = 1; |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2268 | touch->seat = seat; |
| 2269 | |
| 2270 | seat_send_updated_caps(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | WL_EXPORT void |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2274 | weston_seat_release_touch(struct weston_seat *seat) |
| 2275 | { |
| 2276 | seat->touch_device_count--; |
| 2277 | if (seat->touch_device_count == 0) { |
Jonas Ådahl | 630bae8 | 2013-10-17 23:04:06 +0200 | [diff] [blame] | 2278 | weston_touch_set_focus(seat, NULL); |
Jonas Ådahl | 1ea343e | 2013-10-25 23:18:05 +0200 | [diff] [blame] | 2279 | weston_touch_cancel_grab(seat->touch); |
Jonas Ådahl | cbfa7f7 | 2013-12-02 22:05:04 +0100 | [diff] [blame] | 2280 | weston_touch_reset_state(seat->touch); |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 2281 | seat_send_updated_caps(seat); |
| 2282 | } |
| 2283 | } |
| 2284 | |
| 2285 | WL_EXPORT void |
Rob Bradford | 9af5f9e | 2013-05-31 18:09:50 +0100 | [diff] [blame] | 2286 | weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec, |
| 2287 | const char *seat_name) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2288 | { |
Kristian Høgsberg | 4a2a274 | 2013-05-06 22:24:50 -0400 | [diff] [blame] | 2289 | memset(seat, 0, sizeof *seat); |
| 2290 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 2291 | seat->selection_data_source = NULL; |
| 2292 | wl_list_init(&seat->base_resource_list); |
| 2293 | wl_signal_init(&seat->selection_signal); |
| 2294 | wl_list_init(&seat->drag_resource_list); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 2295 | wl_signal_init(&seat->destroy_signal); |
Jason Ekstrand | a4ab542 | 2014-04-02 19:53:45 -0500 | [diff] [blame] | 2296 | wl_signal_init(&seat->updated_caps_signal); |
Kristian Høgsberg | 4a2a274 | 2013-05-06 22:24:50 -0400 | [diff] [blame] | 2297 | |
Jasper St. Pierre | d8c6aeb | 2014-08-04 13:43:24 -0400 | [diff] [blame] | 2298 | seat->global = wl_global_create(ec->wl_display, &wl_seat_interface, 4, |
Kristian Høgsberg | 919cddb | 2013-07-08 19:03:57 -0400 | [diff] [blame] | 2299 | seat, bind_seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2300 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2301 | seat->compositor = ec; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2302 | seat->modifier_state = 0; |
Rob Bradford | 9af5f9e | 2013-05-31 18:09:50 +0100 | [diff] [blame] | 2303 | seat->seat_name = strdup(seat_name); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2304 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2305 | wl_list_insert(ec->seat_list.prev, &seat->link); |
| 2306 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2307 | clipboard_create(seat); |
| 2308 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2309 | wl_signal_emit(&ec->seat_created_signal, seat); |
| 2310 | } |
| 2311 | |
| 2312 | WL_EXPORT void |
| 2313 | weston_seat_release(struct weston_seat *seat) |
| 2314 | { |
| 2315 | wl_list_remove(&seat->link); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2316 | |
Jonas Ådahl | 1afb238 | 2014-01-03 19:46:51 +0100 | [diff] [blame] | 2317 | if (seat->saved_kbd_focus) |
| 2318 | wl_list_remove(&seat->saved_kbd_focus_listener.link); |
| 2319 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 2320 | if (seat->pointer) |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2321 | weston_pointer_destroy(seat->pointer); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 2322 | if (seat->keyboard) |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2323 | weston_keyboard_destroy(seat->keyboard); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 2324 | if (seat->touch) |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame] | 2325 | weston_touch_destroy(seat->touch); |
Kristian Høgsberg | 4a2a274 | 2013-05-06 22:24:50 -0400 | [diff] [blame] | 2326 | |
Rob Bradford | 9af5f9e | 2013-05-31 18:09:50 +0100 | [diff] [blame] | 2327 | free (seat->seat_name); |
| 2328 | |
Kristian Høgsberg | 919cddb | 2013-07-08 19:03:57 -0400 | [diff] [blame] | 2329 | wl_global_destroy(seat->global); |
Kristian Høgsberg | aaadc77 | 2013-07-08 16:20:31 -0400 | [diff] [blame] | 2330 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 2331 | wl_signal_emit(&seat->destroy_signal, seat); |
| 2332 | } |