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 | |
| 23 | #include <stdlib.h> |
| 24 | #include <stdint.h> |
| 25 | #include <string.h> |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 26 | #include <sys/mman.h> |
| 27 | #include <assert.h> |
| 28 | #include <unistd.h> |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 29 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 30 | #include "../shared/os-compatibility.h" |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 31 | #include "compositor.h" |
| 32 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 33 | static void |
| 34 | empty_region(pixman_region32_t *region) |
| 35 | { |
| 36 | pixman_region32_fini(region); |
| 37 | pixman_region32_init(region); |
| 38 | } |
| 39 | |
| 40 | static void unbind_resource(struct wl_resource *resource) |
| 41 | { |
| 42 | wl_list_remove(&resource->link); |
| 43 | free(resource); |
| 44 | } |
| 45 | |
| 46 | void |
Kristian Høgsberg | a71e8b2 | 2013-05-06 21:51:21 -0400 | [diff] [blame] | 47 | weston_seat_repick(struct weston_seat *seat) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 48 | { |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 49 | const struct weston_pointer_grab_interface *interface; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 50 | struct weston_surface *surface, *focus; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 51 | struct weston_pointer *pointer = seat->pointer; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 52 | |
| 53 | if (!pointer) |
| 54 | return; |
| 55 | |
| 56 | surface = weston_compositor_pick_surface(seat->compositor, |
| 57 | pointer->x, |
| 58 | pointer->y, |
| 59 | &pointer->current_x, |
| 60 | &pointer->current_y); |
| 61 | |
| 62 | if (&surface->surface != pointer->current) { |
| 63 | interface = pointer->grab->interface; |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 64 | weston_pointer_set_current(pointer, &surface->surface); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 65 | interface->focus(pointer->grab, &surface->surface, |
| 66 | pointer->current_x, |
| 67 | pointer->current_y); |
| 68 | } |
| 69 | |
| 70 | focus = (struct weston_surface *) pointer->grab->focus; |
| 71 | if (focus) |
| 72 | weston_surface_from_global_fixed(focus, |
| 73 | pointer->x, |
| 74 | pointer->y, |
| 75 | &pointer->grab->x, |
| 76 | &pointer->grab->y); |
| 77 | } |
| 78 | |
| 79 | static void |
| 80 | weston_compositor_idle_inhibit(struct weston_compositor *compositor) |
| 81 | { |
| 82 | weston_compositor_wake(compositor); |
| 83 | compositor->idle_inhibit++; |
| 84 | } |
| 85 | |
| 86 | static void |
| 87 | weston_compositor_idle_release(struct weston_compositor *compositor) |
| 88 | { |
| 89 | compositor->idle_inhibit--; |
| 90 | weston_compositor_wake(compositor); |
| 91 | } |
| 92 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 93 | static void |
| 94 | lose_pointer_focus(struct wl_listener *listener, void *data) |
| 95 | { |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 96 | struct weston_pointer *pointer = |
| 97 | container_of(listener, struct weston_pointer, focus_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 98 | |
| 99 | pointer->focus_resource = NULL; |
| 100 | } |
| 101 | |
| 102 | static void |
| 103 | lose_keyboard_focus(struct wl_listener *listener, void *data) |
| 104 | { |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 105 | struct weston_keyboard *keyboard = |
| 106 | container_of(listener, struct weston_keyboard, focus_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 107 | |
| 108 | keyboard->focus_resource = NULL; |
| 109 | } |
| 110 | |
| 111 | static void |
| 112 | lose_touch_focus(struct wl_listener *listener, void *data) |
| 113 | { |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 114 | struct weston_touch *touch = |
| 115 | container_of(listener, struct weston_touch, focus_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 116 | |
| 117 | touch->focus_resource = NULL; |
| 118 | } |
| 119 | |
| 120 | static void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 121 | default_grab_focus(struct weston_pointer_grab *grab, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 122 | struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y) |
| 123 | { |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 124 | struct weston_pointer *pointer = grab->pointer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 125 | |
| 126 | if (pointer->button_count > 0) |
| 127 | return; |
| 128 | |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 129 | weston_pointer_set_focus(pointer, surface, x, y); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 133 | default_grab_motion(struct weston_pointer_grab *grab, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 134 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 135 | { |
| 136 | struct wl_resource *resource; |
| 137 | |
| 138 | resource = grab->pointer->focus_resource; |
| 139 | if (resource) |
| 140 | wl_pointer_send_motion(resource, time, x, y); |
| 141 | } |
| 142 | |
| 143 | static void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 144 | default_grab_button(struct weston_pointer_grab *grab, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 145 | uint32_t time, uint32_t button, uint32_t state_w) |
| 146 | { |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 147 | struct weston_pointer *pointer = grab->pointer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 148 | struct wl_resource *resource; |
| 149 | uint32_t serial; |
| 150 | enum wl_pointer_button_state state = state_w; |
| 151 | struct wl_display *display; |
| 152 | |
| 153 | resource = pointer->focus_resource; |
| 154 | if (resource) { |
| 155 | display = wl_client_get_display(resource->client); |
| 156 | serial = wl_display_next_serial(display); |
| 157 | wl_pointer_send_button(resource, serial, time, button, state_w); |
| 158 | } |
| 159 | |
| 160 | if (pointer->button_count == 0 && |
| 161 | state == WL_POINTER_BUTTON_STATE_RELEASED) |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 162 | weston_pointer_set_focus(pointer, pointer->current, |
| 163 | pointer->current_x, |
| 164 | pointer->current_y); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 167 | static const struct weston_pointer_grab_interface |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 168 | default_pointer_grab_interface = { |
| 169 | default_grab_focus, |
| 170 | default_grab_motion, |
| 171 | default_grab_button |
| 172 | }; |
| 173 | |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 174 | static void |
| 175 | default_grab_touch_down(struct weston_touch_grab *grab, uint32_t time, |
| 176 | int touch_id, wl_fixed_t sx, wl_fixed_t sy) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 177 | { |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 178 | struct weston_touch *touch = grab->touch; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 179 | struct wl_display *display; |
| 180 | uint32_t serial; |
| 181 | |
| 182 | if (touch->focus_resource && touch->focus) { |
| 183 | display = wl_client_get_display(touch->focus_resource->client); |
| 184 | serial = wl_display_next_serial(display); |
| 185 | wl_touch_send_down(touch->focus_resource, serial, time, |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 186 | &touch->focus->resource, |
| 187 | touch_id, sx, sy); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 191 | static void |
| 192 | default_grab_touch_up(struct weston_touch_grab *grab, |
| 193 | uint32_t time, int touch_id) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 194 | { |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 195 | struct weston_touch *touch = grab->touch; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 196 | struct wl_display *display; |
| 197 | uint32_t serial; |
| 198 | |
| 199 | if (touch->focus_resource) { |
| 200 | display = wl_client_get_display(touch->focus_resource->client); |
| 201 | serial = wl_display_next_serial(display); |
| 202 | wl_touch_send_up(touch->focus_resource, serial, time, touch_id); |
| 203 | } |
| 204 | } |
| 205 | |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 206 | static void |
| 207 | default_grab_touch_motion(struct weston_touch_grab *grab, uint32_t time, |
| 208 | int touch_id, wl_fixed_t sx, wl_fixed_t sy) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 209 | { |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 210 | struct weston_touch *touch = grab->touch; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 211 | |
| 212 | if (touch->focus_resource) { |
| 213 | wl_touch_send_motion(touch->focus_resource, time, |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 214 | touch_id, sx, sy); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 218 | static const struct weston_touch_grab_interface default_touch_grab_interface = { |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 219 | default_grab_touch_down, |
| 220 | default_grab_touch_up, |
| 221 | default_grab_touch_motion |
| 222 | }; |
| 223 | |
| 224 | static void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 225 | default_grab_key(struct weston_keyboard_grab *grab, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 226 | uint32_t time, uint32_t key, uint32_t state) |
| 227 | { |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 228 | struct weston_keyboard *keyboard = grab->keyboard; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 229 | struct wl_resource *resource; |
| 230 | struct wl_display *display; |
| 231 | uint32_t serial; |
| 232 | |
| 233 | resource = keyboard->focus_resource; |
| 234 | if (resource) { |
| 235 | display = wl_client_get_display(resource->client); |
| 236 | serial = wl_display_next_serial(display); |
| 237 | wl_keyboard_send_key(resource, serial, time, key, state); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | static struct wl_resource * |
| 242 | find_resource_for_surface(struct wl_list *list, struct wl_surface *surface) |
| 243 | { |
| 244 | struct wl_resource *r; |
| 245 | |
| 246 | if (!surface) |
| 247 | return NULL; |
| 248 | |
| 249 | wl_list_for_each(r, list, link) { |
| 250 | if (r->client == surface->resource.client) |
| 251 | return r; |
| 252 | } |
| 253 | |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | static void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 258 | default_grab_modifiers(struct weston_keyboard_grab *grab, uint32_t serial, |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 259 | uint32_t mods_depressed, uint32_t mods_latched, |
| 260 | uint32_t mods_locked, uint32_t group) |
| 261 | { |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 262 | struct weston_keyboard *keyboard = grab->keyboard; |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 263 | struct weston_pointer *pointer = keyboard->seat->pointer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 264 | struct wl_resource *resource, *pr; |
| 265 | |
| 266 | resource = keyboard->focus_resource; |
| 267 | if (!resource) |
| 268 | return; |
| 269 | |
| 270 | wl_keyboard_send_modifiers(resource, serial, mods_depressed, |
| 271 | mods_latched, mods_locked, group); |
| 272 | |
| 273 | if (pointer && pointer->focus && pointer->focus != keyboard->focus) { |
| 274 | pr = find_resource_for_surface(&keyboard->resource_list, |
| 275 | pointer->focus); |
| 276 | if (pr) { |
| 277 | wl_keyboard_send_modifiers(pr, |
| 278 | serial, |
| 279 | keyboard->modifiers.mods_depressed, |
| 280 | keyboard->modifiers.mods_latched, |
| 281 | keyboard->modifiers.mods_locked, |
| 282 | keyboard->modifiers.group); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 287 | static const struct weston_keyboard_grab_interface |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 288 | default_keyboard_grab_interface = { |
| 289 | default_grab_key, |
| 290 | default_grab_modifiers, |
| 291 | }; |
| 292 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 293 | WL_EXPORT struct weston_pointer * |
| 294 | weston_pointer_create(void) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 295 | { |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 296 | struct weston_pointer *pointer; |
| 297 | |
| 298 | pointer = malloc(sizeof *pointer); |
| 299 | if (pointer == NULL) |
| 300 | return NULL; |
| 301 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 302 | memset(pointer, 0, sizeof *pointer); |
| 303 | wl_list_init(&pointer->resource_list); |
| 304 | pointer->focus_listener.notify = lose_pointer_focus; |
| 305 | pointer->default_grab.interface = &default_pointer_grab_interface; |
| 306 | pointer->default_grab.pointer = pointer; |
| 307 | pointer->grab = &pointer->default_grab; |
| 308 | wl_signal_init(&pointer->focus_signal); |
| 309 | |
| 310 | /* FIXME: Pick better co-ords. */ |
| 311 | pointer->x = wl_fixed_from_int(100); |
| 312 | pointer->y = wl_fixed_from_int(100); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 313 | |
| 314 | return pointer; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | WL_EXPORT void |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 318 | weston_pointer_destroy(struct weston_pointer *pointer) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 319 | { |
| 320 | /* XXX: What about pointer->resource_list? */ |
| 321 | if (pointer->focus_resource) |
| 322 | wl_list_remove(&pointer->focus_listener.link); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 323 | free(pointer); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 324 | } |
| 325 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 326 | WL_EXPORT struct weston_keyboard * |
| 327 | weston_keyboard_create(void) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 328 | { |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 329 | struct weston_keyboard *keyboard; |
| 330 | |
| 331 | keyboard = malloc(sizeof *keyboard); |
| 332 | if (keyboard == NULL) |
| 333 | return NULL; |
| 334 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 335 | memset(keyboard, 0, sizeof *keyboard); |
| 336 | wl_list_init(&keyboard->resource_list); |
| 337 | wl_array_init(&keyboard->keys); |
| 338 | keyboard->focus_listener.notify = lose_keyboard_focus; |
| 339 | keyboard->default_grab.interface = &default_keyboard_grab_interface; |
| 340 | keyboard->default_grab.keyboard = keyboard; |
| 341 | keyboard->grab = &keyboard->default_grab; |
| 342 | wl_signal_init(&keyboard->focus_signal); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 343 | |
| 344 | return keyboard; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | WL_EXPORT void |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 348 | weston_keyboard_destroy(struct weston_keyboard *keyboard) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 349 | { |
| 350 | /* XXX: What about keyboard->resource_list? */ |
| 351 | if (keyboard->focus_resource) |
| 352 | wl_list_remove(&keyboard->focus_listener.link); |
| 353 | wl_array_release(&keyboard->keys); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 354 | free(keyboard); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 355 | } |
| 356 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 357 | WL_EXPORT struct weston_touch * |
| 358 | weston_touch_create(void) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 359 | { |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 360 | struct weston_touch *touch; |
| 361 | |
| 362 | touch = malloc(sizeof *touch); |
| 363 | if (touch == NULL) |
| 364 | return NULL; |
| 365 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 366 | memset(touch, 0, sizeof *touch); |
| 367 | wl_list_init(&touch->resource_list); |
| 368 | touch->focus_listener.notify = lose_touch_focus; |
| 369 | touch->default_grab.interface = &default_touch_grab_interface; |
| 370 | touch->default_grab.touch = touch; |
| 371 | touch->grab = &touch->default_grab; |
| 372 | wl_signal_init(&touch->focus_signal); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 373 | |
| 374 | return touch; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | WL_EXPORT void |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 378 | weston_touch_destroy(struct weston_touch *touch) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 379 | { |
| 380 | /* XXX: What about touch->resource_list? */ |
| 381 | if (touch->focus_resource) |
| 382 | wl_list_remove(&touch->focus_listener.link); |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 383 | free(touch); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 384 | } |
| 385 | |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 386 | static void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 387 | seat_send_updated_caps(struct weston_seat *seat) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 388 | { |
| 389 | struct wl_resource *r; |
| 390 | enum wl_seat_capability caps = 0; |
| 391 | |
| 392 | if (seat->pointer) |
| 393 | caps |= WL_SEAT_CAPABILITY_POINTER; |
| 394 | if (seat->keyboard) |
| 395 | caps |= WL_SEAT_CAPABILITY_KEYBOARD; |
| 396 | if (seat->touch) |
| 397 | caps |= WL_SEAT_CAPABILITY_TOUCH; |
| 398 | |
| 399 | wl_list_for_each(r, &seat->base_resource_list, link) |
| 400 | wl_seat_send_capabilities(r, caps); |
| 401 | } |
| 402 | |
| 403 | WL_EXPORT void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 404 | weston_pointer_set_focus(struct weston_pointer *pointer, |
| 405 | struct wl_surface *surface, |
| 406 | wl_fixed_t sx, wl_fixed_t sy) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 407 | { |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 408 | struct weston_keyboard *kbd = pointer->seat->keyboard; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 409 | struct wl_resource *resource, *kr; |
| 410 | struct wl_display *display; |
| 411 | uint32_t serial; |
| 412 | |
| 413 | resource = pointer->focus_resource; |
| 414 | if (resource && pointer->focus != surface) { |
| 415 | display = wl_client_get_display(resource->client); |
| 416 | serial = wl_display_next_serial(display); |
| 417 | wl_pointer_send_leave(resource, serial, |
| 418 | &pointer->focus->resource); |
| 419 | wl_list_remove(&pointer->focus_listener.link); |
| 420 | } |
| 421 | |
| 422 | resource = find_resource_for_surface(&pointer->resource_list, |
| 423 | surface); |
| 424 | if (resource && |
| 425 | (pointer->focus != surface || |
| 426 | pointer->focus_resource != resource)) { |
| 427 | display = wl_client_get_display(resource->client); |
| 428 | serial = wl_display_next_serial(display); |
| 429 | if (kbd) { |
| 430 | kr = find_resource_for_surface(&kbd->resource_list, |
| 431 | surface); |
| 432 | if (kr) { |
| 433 | wl_keyboard_send_modifiers(kr, |
| 434 | serial, |
| 435 | kbd->modifiers.mods_depressed, |
| 436 | kbd->modifiers.mods_latched, |
| 437 | kbd->modifiers.mods_locked, |
| 438 | kbd->modifiers.group); |
| 439 | } |
| 440 | } |
| 441 | wl_pointer_send_enter(resource, serial, &surface->resource, |
| 442 | sx, sy); |
| 443 | wl_signal_add(&resource->destroy_signal, |
| 444 | &pointer->focus_listener); |
| 445 | pointer->focus_serial = serial; |
| 446 | } |
| 447 | |
| 448 | pointer->focus_resource = resource; |
| 449 | pointer->focus = surface; |
| 450 | pointer->default_grab.focus = surface; |
| 451 | wl_signal_emit(&pointer->focus_signal, pointer); |
| 452 | } |
| 453 | |
| 454 | WL_EXPORT void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 455 | weston_keyboard_set_focus(struct weston_keyboard *keyboard, |
| 456 | struct wl_surface *surface) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 457 | { |
| 458 | struct wl_resource *resource; |
| 459 | struct wl_display *display; |
| 460 | uint32_t serial; |
| 461 | |
| 462 | if (keyboard->focus_resource && keyboard->focus != surface) { |
| 463 | resource = keyboard->focus_resource; |
| 464 | display = wl_client_get_display(resource->client); |
| 465 | serial = wl_display_next_serial(display); |
| 466 | wl_keyboard_send_leave(resource, serial, |
| 467 | &keyboard->focus->resource); |
| 468 | wl_list_remove(&keyboard->focus_listener.link); |
| 469 | } |
| 470 | |
| 471 | resource = find_resource_for_surface(&keyboard->resource_list, |
| 472 | surface); |
| 473 | if (resource && |
| 474 | (keyboard->focus != surface || |
| 475 | keyboard->focus_resource != resource)) { |
| 476 | display = wl_client_get_display(resource->client); |
| 477 | serial = wl_display_next_serial(display); |
| 478 | wl_keyboard_send_modifiers(resource, serial, |
| 479 | keyboard->modifiers.mods_depressed, |
| 480 | keyboard->modifiers.mods_latched, |
| 481 | keyboard->modifiers.mods_locked, |
| 482 | keyboard->modifiers.group); |
| 483 | wl_keyboard_send_enter(resource, serial, &surface->resource, |
| 484 | &keyboard->keys); |
| 485 | wl_signal_add(&resource->destroy_signal, |
| 486 | &keyboard->focus_listener); |
| 487 | keyboard->focus_serial = serial; |
| 488 | } |
| 489 | |
| 490 | keyboard->focus_resource = resource; |
| 491 | keyboard->focus = surface; |
| 492 | wl_signal_emit(&keyboard->focus_signal, keyboard); |
| 493 | } |
| 494 | |
| 495 | WL_EXPORT void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 496 | weston_keyboard_start_grab(struct weston_keyboard *keyboard, |
| 497 | struct weston_keyboard_grab *grab) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 498 | { |
| 499 | keyboard->grab = grab; |
| 500 | grab->keyboard = keyboard; |
| 501 | |
| 502 | /* XXX focus? */ |
| 503 | } |
| 504 | |
| 505 | WL_EXPORT void |
Kristian Høgsberg | 29139d4 | 2013-04-18 15:25:39 -0400 | [diff] [blame] | 506 | weston_keyboard_end_grab(struct weston_keyboard *keyboard) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 507 | { |
| 508 | keyboard->grab = &keyboard->default_grab; |
| 509 | } |
| 510 | |
| 511 | WL_EXPORT void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 512 | weston_pointer_start_grab(struct weston_pointer *pointer, |
| 513 | struct weston_pointer_grab *grab) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 514 | { |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 515 | const struct weston_pointer_grab_interface *interface; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 516 | |
| 517 | pointer->grab = grab; |
| 518 | interface = pointer->grab->interface; |
| 519 | grab->pointer = pointer; |
| 520 | |
| 521 | if (pointer->current) |
| 522 | interface->focus(pointer->grab, pointer->current, |
| 523 | pointer->current_x, pointer->current_y); |
| 524 | } |
| 525 | |
| 526 | WL_EXPORT void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 527 | weston_pointer_end_grab(struct weston_pointer *pointer) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 528 | { |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 529 | const struct weston_pointer_grab_interface *interface; |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 530 | |
| 531 | pointer->grab = &pointer->default_grab; |
| 532 | interface = pointer->grab->interface; |
| 533 | interface->focus(pointer->grab, pointer->current, |
| 534 | pointer->current_x, pointer->current_y); |
| 535 | } |
| 536 | |
| 537 | static void |
| 538 | current_surface_destroy(struct wl_listener *listener, void *data) |
| 539 | { |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 540 | struct weston_pointer *pointer = |
| 541 | container_of(listener, struct weston_pointer, current_listener); |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 542 | |
| 543 | pointer->current = NULL; |
| 544 | } |
| 545 | |
| 546 | WL_EXPORT void |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 547 | weston_pointer_set_current(struct weston_pointer *pointer, |
| 548 | struct wl_surface *surface) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 549 | { |
| 550 | if (pointer->current) |
| 551 | wl_list_remove(&pointer->current_listener.link); |
| 552 | |
| 553 | pointer->current = surface; |
| 554 | |
| 555 | if (!surface) |
| 556 | return; |
| 557 | |
| 558 | wl_signal_add(&surface->resource.destroy_signal, |
| 559 | &pointer->current_listener); |
| 560 | pointer->current_listener.notify = current_surface_destroy; |
| 561 | } |
| 562 | |
| 563 | WL_EXPORT void |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 564 | 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] | 565 | { |
| 566 | touch->grab = grab; |
| 567 | grab->touch = touch; |
| 568 | } |
| 569 | |
| 570 | WL_EXPORT void |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 571 | weston_touch_end_grab(struct weston_touch *touch) |
Kristian Høgsberg | 2158a88 | 2013-04-18 15:07:39 -0400 | [diff] [blame] | 572 | { |
| 573 | touch->grab = &touch->default_grab; |
| 574 | } |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 575 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 576 | static void |
| 577 | clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy) |
| 578 | { |
| 579 | struct weston_compositor *ec = seat->compositor; |
| 580 | struct weston_output *output, *prev = NULL; |
| 581 | int x, y, old_x, old_y, valid = 0; |
| 582 | |
| 583 | x = wl_fixed_to_int(*fx); |
| 584 | y = wl_fixed_to_int(*fy); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 585 | old_x = wl_fixed_to_int(seat->pointer->x); |
| 586 | old_y = wl_fixed_to_int(seat->pointer->y); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 587 | |
| 588 | wl_list_for_each(output, &ec->output_list, link) { |
| 589 | if (pixman_region32_contains_point(&output->region, |
| 590 | x, y, NULL)) |
| 591 | valid = 1; |
| 592 | if (pixman_region32_contains_point(&output->region, |
| 593 | old_x, old_y, NULL)) |
| 594 | prev = output; |
| 595 | } |
| 596 | |
| 597 | if (!valid) { |
| 598 | if (x < prev->x) |
| 599 | *fx = wl_fixed_from_int(prev->x); |
| 600 | else if (x >= prev->x + prev->width) |
| 601 | *fx = wl_fixed_from_int(prev->x + |
| 602 | prev->width - 1); |
| 603 | if (y < prev->y) |
| 604 | *fy = wl_fixed_from_int(prev->y); |
| 605 | else if (y >= prev->y + prev->current->height) |
| 606 | *fy = wl_fixed_from_int(prev->y + |
| 607 | prev->height - 1); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | /* Takes absolute values */ |
| 612 | static void |
| 613 | move_pointer(struct weston_seat *seat, wl_fixed_t x, wl_fixed_t y) |
| 614 | { |
| 615 | struct weston_compositor *ec = seat->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 616 | struct weston_pointer *pointer = seat->pointer; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 617 | struct weston_output *output; |
| 618 | int32_t ix, iy; |
| 619 | |
| 620 | clip_pointer_motion(seat, &x, &y); |
| 621 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 622 | pointer->x = x; |
| 623 | pointer->y = y; |
| 624 | |
| 625 | ix = wl_fixed_to_int(x); |
| 626 | iy = wl_fixed_to_int(y); |
| 627 | |
| 628 | wl_list_for_each(output, &ec->output_list, link) |
| 629 | if (output->zoom.active && |
| 630 | pixman_region32_contains_point(&output->region, |
| 631 | ix, iy, NULL)) |
| 632 | weston_output_update_zoom(output, ZOOM_FOCUS_POINTER); |
| 633 | |
Kristian Høgsberg | a71e8b2 | 2013-05-06 21:51:21 -0400 | [diff] [blame] | 634 | weston_seat_repick(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 635 | |
| 636 | if (seat->sprite) { |
| 637 | weston_surface_set_position(seat->sprite, |
| 638 | ix - seat->hotspot_x, |
| 639 | iy - seat->hotspot_y); |
| 640 | weston_surface_schedule_repaint(seat->sprite); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | WL_EXPORT void |
| 645 | notify_motion(struct weston_seat *seat, |
| 646 | uint32_t time, wl_fixed_t dx, wl_fixed_t dy) |
| 647 | { |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 648 | const struct weston_pointer_grab_interface *interface; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 649 | struct weston_compositor *ec = seat->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 650 | struct weston_pointer *pointer = seat->pointer; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 651 | |
| 652 | weston_compositor_wake(ec); |
| 653 | |
| 654 | move_pointer(seat, pointer->x + dx, pointer->y + dy); |
| 655 | |
| 656 | interface = pointer->grab->interface; |
| 657 | interface->motion(pointer->grab, time, |
| 658 | pointer->grab->x, pointer->grab->y); |
| 659 | } |
| 660 | |
| 661 | WL_EXPORT void |
| 662 | notify_motion_absolute(struct weston_seat *seat, |
| 663 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 664 | { |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 665 | const struct weston_pointer_grab_interface *interface; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 666 | struct weston_compositor *ec = seat->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 667 | struct weston_pointer *pointer = seat->pointer; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 668 | |
| 669 | weston_compositor_wake(ec); |
| 670 | |
| 671 | move_pointer(seat, x, y); |
| 672 | |
| 673 | interface = pointer->grab->interface; |
| 674 | interface->motion(pointer->grab, time, |
| 675 | pointer->grab->x, pointer->grab->y); |
| 676 | } |
| 677 | |
| 678 | WL_EXPORT void |
| 679 | weston_surface_activate(struct weston_surface *surface, |
| 680 | struct weston_seat *seat) |
| 681 | { |
| 682 | struct weston_compositor *compositor = seat->compositor; |
| 683 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 684 | if (seat->keyboard) { |
| 685 | weston_keyboard_set_focus(seat->keyboard, &surface->surface); |
| 686 | wl_data_device_set_keyboard_focus(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | wl_signal_emit(&compositor->activate_signal, surface); |
| 690 | } |
| 691 | |
| 692 | WL_EXPORT void |
| 693 | notify_button(struct weston_seat *seat, uint32_t time, int32_t button, |
| 694 | enum wl_pointer_button_state state) |
| 695 | { |
| 696 | struct weston_compositor *compositor = seat->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 697 | struct weston_pointer *pointer = seat->pointer; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 698 | struct weston_surface *focus = |
| 699 | (struct weston_surface *) pointer->focus; |
| 700 | uint32_t serial = wl_display_next_serial(compositor->wl_display); |
| 701 | |
| 702 | if (state == WL_POINTER_BUTTON_STATE_PRESSED) { |
| 703 | if (compositor->ping_handler && focus) |
| 704 | compositor->ping_handler(focus, serial); |
| 705 | weston_compositor_idle_inhibit(compositor); |
| 706 | if (pointer->button_count == 0) { |
| 707 | pointer->grab_button = button; |
| 708 | pointer->grab_time = time; |
| 709 | pointer->grab_x = pointer->x; |
| 710 | pointer->grab_y = pointer->y; |
| 711 | } |
| 712 | pointer->button_count++; |
| 713 | } else { |
| 714 | weston_compositor_idle_release(compositor); |
| 715 | pointer->button_count--; |
| 716 | } |
| 717 | |
| 718 | weston_compositor_run_button_binding(compositor, seat, time, button, |
| 719 | state); |
| 720 | |
| 721 | pointer->grab->interface->button(pointer->grab, time, button, state); |
| 722 | |
| 723 | if (pointer->button_count == 1) |
| 724 | pointer->grab_serial = |
| 725 | wl_display_get_serial(compositor->wl_display); |
| 726 | } |
| 727 | |
| 728 | WL_EXPORT void |
| 729 | notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis, |
| 730 | wl_fixed_t value) |
| 731 | { |
| 732 | struct weston_compositor *compositor = seat->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 733 | struct weston_pointer *pointer = seat->pointer; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 734 | struct weston_surface *focus = |
| 735 | (struct weston_surface *) pointer->focus; |
| 736 | uint32_t serial = wl_display_next_serial(compositor->wl_display); |
| 737 | |
| 738 | if (compositor->ping_handler && focus) |
| 739 | compositor->ping_handler(focus, serial); |
| 740 | |
| 741 | weston_compositor_wake(compositor); |
| 742 | |
| 743 | if (!value) |
| 744 | return; |
| 745 | |
| 746 | if (weston_compositor_run_axis_binding(compositor, seat, |
| 747 | time, axis, value)) |
| 748 | return; |
| 749 | |
| 750 | if (pointer->focus_resource) |
| 751 | wl_pointer_send_axis(pointer->focus_resource, time, axis, |
| 752 | value); |
| 753 | } |
| 754 | |
| 755 | WL_EXPORT void |
| 756 | notify_modifiers(struct weston_seat *seat, uint32_t serial) |
| 757 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 758 | struct weston_keyboard *keyboard = seat->keyboard; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 759 | struct weston_keyboard_grab *grab = keyboard->grab; |
| 760 | uint32_t mods_depressed, mods_latched, mods_locked, group; |
| 761 | uint32_t mods_lookup; |
| 762 | enum weston_led leds = 0; |
| 763 | int changed = 0; |
| 764 | |
| 765 | /* Serialize and update our internal state, checking to see if it's |
| 766 | * different to the previous state. */ |
| 767 | mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state, |
| 768 | XKB_STATE_DEPRESSED); |
| 769 | mods_latched = xkb_state_serialize_mods(seat->xkb_state.state, |
| 770 | XKB_STATE_LATCHED); |
| 771 | mods_locked = xkb_state_serialize_mods(seat->xkb_state.state, |
| 772 | XKB_STATE_LOCKED); |
| 773 | group = xkb_state_serialize_group(seat->xkb_state.state, |
| 774 | XKB_STATE_EFFECTIVE); |
| 775 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 776 | if (mods_depressed != seat->keyboard->modifiers.mods_depressed || |
| 777 | mods_latched != seat->keyboard->modifiers.mods_latched || |
| 778 | mods_locked != seat->keyboard->modifiers.mods_locked || |
| 779 | group != seat->keyboard->modifiers.group) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 780 | changed = 1; |
| 781 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 782 | seat->keyboard->modifiers.mods_depressed = mods_depressed; |
| 783 | seat->keyboard->modifiers.mods_latched = mods_latched; |
| 784 | seat->keyboard->modifiers.mods_locked = mods_locked; |
| 785 | seat->keyboard->modifiers.group = group; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 786 | |
| 787 | /* And update the modifier_state for bindings. */ |
| 788 | mods_lookup = mods_depressed | mods_latched; |
| 789 | seat->modifier_state = 0; |
| 790 | if (mods_lookup & (1 << seat->xkb_info.ctrl_mod)) |
| 791 | seat->modifier_state |= MODIFIER_CTRL; |
| 792 | if (mods_lookup & (1 << seat->xkb_info.alt_mod)) |
| 793 | seat->modifier_state |= MODIFIER_ALT; |
| 794 | if (mods_lookup & (1 << seat->xkb_info.super_mod)) |
| 795 | seat->modifier_state |= MODIFIER_SUPER; |
| 796 | if (mods_lookup & (1 << seat->xkb_info.shift_mod)) |
| 797 | seat->modifier_state |= MODIFIER_SHIFT; |
| 798 | |
| 799 | /* Finally, notify the compositor that LEDs have changed. */ |
| 800 | if (xkb_state_led_index_is_active(seat->xkb_state.state, |
| 801 | seat->xkb_info.num_led)) |
| 802 | leds |= LED_NUM_LOCK; |
| 803 | if (xkb_state_led_index_is_active(seat->xkb_state.state, |
| 804 | seat->xkb_info.caps_led)) |
| 805 | leds |= LED_CAPS_LOCK; |
| 806 | if (xkb_state_led_index_is_active(seat->xkb_state.state, |
| 807 | seat->xkb_info.scroll_led)) |
| 808 | leds |= LED_SCROLL_LOCK; |
| 809 | if (leds != seat->xkb_state.leds && seat->led_update) |
| 810 | seat->led_update(seat, leds); |
| 811 | seat->xkb_state.leds = leds; |
| 812 | |
| 813 | if (changed) { |
| 814 | grab->interface->modifiers(grab, |
| 815 | serial, |
| 816 | keyboard->modifiers.mods_depressed, |
| 817 | keyboard->modifiers.mods_latched, |
| 818 | keyboard->modifiers.mods_locked, |
| 819 | keyboard->modifiers.group); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | static void |
| 824 | update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key, |
| 825 | enum wl_keyboard_key_state state) |
| 826 | { |
| 827 | enum xkb_key_direction direction; |
| 828 | |
| 829 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) |
| 830 | direction = XKB_KEY_DOWN; |
| 831 | else |
| 832 | direction = XKB_KEY_UP; |
| 833 | |
| 834 | /* Offset the keycode by 8, as the evdev XKB rules reflect X's |
| 835 | * broken keycode system, which starts at 8. */ |
| 836 | xkb_state_update_key(seat->xkb_state.state, key + 8, direction); |
| 837 | |
| 838 | notify_modifiers(seat, serial); |
| 839 | } |
| 840 | |
| 841 | WL_EXPORT void |
| 842 | notify_key(struct weston_seat *seat, uint32_t time, uint32_t key, |
| 843 | enum wl_keyboard_key_state state, |
| 844 | enum weston_key_state_update update_state) |
| 845 | { |
| 846 | struct weston_compositor *compositor = seat->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 847 | struct weston_keyboard *keyboard = seat->keyboard; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 848 | struct weston_surface *focus = |
| 849 | (struct weston_surface *) keyboard->focus; |
| 850 | struct weston_keyboard_grab *grab = keyboard->grab; |
| 851 | uint32_t serial = wl_display_next_serial(compositor->wl_display); |
| 852 | uint32_t *k, *end; |
| 853 | |
| 854 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 855 | if (compositor->ping_handler && focus) |
| 856 | compositor->ping_handler(focus, serial); |
| 857 | |
| 858 | weston_compositor_idle_inhibit(compositor); |
| 859 | keyboard->grab_key = key; |
| 860 | keyboard->grab_time = time; |
| 861 | } else { |
| 862 | weston_compositor_idle_release(compositor); |
| 863 | } |
| 864 | |
| 865 | end = keyboard->keys.data + keyboard->keys.size; |
| 866 | for (k = keyboard->keys.data; k < end; k++) { |
| 867 | if (*k == key) { |
| 868 | /* Ignore server-generated repeats. */ |
| 869 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) |
| 870 | return; |
| 871 | *k = *--end; |
| 872 | } |
| 873 | } |
| 874 | keyboard->keys.size = (void *) end - keyboard->keys.data; |
| 875 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 876 | k = wl_array_add(&keyboard->keys, sizeof *k); |
| 877 | *k = key; |
| 878 | } |
| 879 | |
| 880 | if (grab == &keyboard->default_grab || |
| 881 | grab == &keyboard->input_method_grab) { |
| 882 | weston_compositor_run_key_binding(compositor, seat, time, key, |
| 883 | state); |
| 884 | grab = keyboard->grab; |
| 885 | } |
| 886 | |
| 887 | grab->interface->key(grab, time, key, state); |
| 888 | |
| 889 | if (update_state == STATE_UPDATE_AUTOMATIC) { |
| 890 | update_modifier_state(seat, |
| 891 | wl_display_get_serial(compositor->wl_display), |
| 892 | key, |
| 893 | state); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | WL_EXPORT void |
| 898 | notify_pointer_focus(struct weston_seat *seat, struct weston_output *output, |
| 899 | wl_fixed_t x, wl_fixed_t y) |
| 900 | { |
| 901 | struct weston_compositor *compositor = seat->compositor; |
| 902 | |
| 903 | if (output) { |
| 904 | move_pointer(seat, x, y); |
| 905 | compositor->focus = 1; |
| 906 | } else { |
| 907 | compositor->focus = 0; |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 908 | /* FIXME: We should call weston_pointer_set_focus(seat, |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 909 | * NULL) here, but somehow that breaks re-entry... */ |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | static void |
| 914 | destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data) |
| 915 | { |
| 916 | struct weston_seat *ws; |
| 917 | |
| 918 | ws = container_of(listener, struct weston_seat, |
| 919 | saved_kbd_focus_listener); |
| 920 | |
| 921 | ws->saved_kbd_focus = NULL; |
| 922 | } |
| 923 | |
| 924 | WL_EXPORT void |
| 925 | notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys, |
| 926 | enum weston_key_state_update update_state) |
| 927 | { |
| 928 | struct weston_compositor *compositor = seat->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 929 | struct weston_keyboard *keyboard = seat->keyboard; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 930 | struct wl_surface *surface; |
| 931 | uint32_t *k, serial; |
| 932 | |
| 933 | serial = wl_display_next_serial(compositor->wl_display); |
| 934 | wl_array_copy(&keyboard->keys, keys); |
| 935 | wl_array_for_each(k, &keyboard->keys) { |
| 936 | weston_compositor_idle_inhibit(compositor); |
| 937 | if (update_state == STATE_UPDATE_AUTOMATIC) |
| 938 | update_modifier_state(seat, serial, *k, |
| 939 | WL_KEYBOARD_KEY_STATE_PRESSED); |
| 940 | } |
| 941 | |
| 942 | /* Run key bindings after we've updated the state. */ |
| 943 | wl_array_for_each(k, &keyboard->keys) { |
| 944 | weston_compositor_run_key_binding(compositor, seat, 0, *k, |
| 945 | WL_KEYBOARD_KEY_STATE_PRESSED); |
| 946 | } |
| 947 | |
| 948 | surface = seat->saved_kbd_focus; |
| 949 | |
| 950 | if (surface) { |
| 951 | wl_list_remove(&seat->saved_kbd_focus_listener.link); |
| 952 | weston_keyboard_set_focus(keyboard, surface); |
| 953 | seat->saved_kbd_focus = NULL; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | WL_EXPORT void |
| 958 | notify_keyboard_focus_out(struct weston_seat *seat) |
| 959 | { |
| 960 | struct weston_compositor *compositor = seat->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 961 | struct weston_keyboard *keyboard = seat->keyboard; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 962 | uint32_t *k, serial; |
| 963 | |
| 964 | serial = wl_display_next_serial(compositor->wl_display); |
| 965 | wl_array_for_each(k, &keyboard->keys) { |
| 966 | weston_compositor_idle_release(compositor); |
| 967 | update_modifier_state(seat, serial, *k, |
| 968 | WL_KEYBOARD_KEY_STATE_RELEASED); |
| 969 | } |
| 970 | |
| 971 | seat->modifier_state = 0; |
| 972 | |
| 973 | if (keyboard->focus) { |
| 974 | seat->saved_kbd_focus = keyboard->focus; |
| 975 | seat->saved_kbd_focus_listener.notify = |
| 976 | destroy_device_saved_kbd_focus; |
| 977 | wl_signal_add(&keyboard->focus->resource.destroy_signal, |
| 978 | &seat->saved_kbd_focus_listener); |
| 979 | } |
| 980 | |
| 981 | weston_keyboard_set_focus(keyboard, NULL); |
| 982 | /* FIXME: We really need keyboard grab cancel here to |
| 983 | * let the grab shut down properly. As it is we leak |
| 984 | * the grab data. */ |
| 985 | weston_keyboard_end_grab(keyboard); |
| 986 | } |
| 987 | |
| 988 | static void |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 989 | touch_set_focus(struct weston_seat *seat, struct wl_surface *surface) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 990 | { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 991 | struct wl_resource *resource; |
| 992 | |
| 993 | if (seat->touch->focus == surface) |
| 994 | return; |
| 995 | |
| 996 | if (seat->touch->focus_resource) |
| 997 | wl_list_remove(&seat->touch->focus_listener.link); |
| 998 | seat->touch->focus = NULL; |
| 999 | seat->touch->focus_resource = NULL; |
| 1000 | |
| 1001 | if (surface) { |
| 1002 | resource = |
Kristian Høgsberg | 80fb82d | 2013-05-06 21:49:55 -0400 | [diff] [blame] | 1003 | find_resource_for_surface(&seat->touch->resource_list, |
| 1004 | surface); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1005 | if (!resource) { |
| 1006 | weston_log("couldn't find resource\n"); |
| 1007 | return; |
| 1008 | } |
| 1009 | |
| 1010 | seat->touch->focus = surface; |
| 1011 | seat->touch->focus_resource = resource; |
| 1012 | wl_signal_add(&resource->destroy_signal, |
| 1013 | &seat->touch->focus_listener); |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | /** |
| 1018 | * notify_touch - emulates button touches and notifies surfaces accordingly. |
| 1019 | * |
| 1020 | * It assumes always the correct cycle sequence until it gets here: touch_down |
| 1021 | * → touch_update → ... → touch_update → touch_end. The driver is responsible |
| 1022 | * for sending along such order. |
| 1023 | * |
| 1024 | */ |
| 1025 | WL_EXPORT void |
| 1026 | notify_touch(struct weston_seat *seat, uint32_t time, int touch_id, |
| 1027 | wl_fixed_t x, wl_fixed_t y, int touch_type) |
| 1028 | { |
| 1029 | struct weston_compositor *ec = seat->compositor; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1030 | struct weston_touch *touch = seat->touch; |
Kristian Høgsberg | e329f36 | 2013-05-06 22:19:57 -0400 | [diff] [blame] | 1031 | struct weston_touch_grab *grab = touch->grab; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1032 | struct weston_surface *es; |
| 1033 | wl_fixed_t sx, sy; |
| 1034 | |
| 1035 | /* Update grab's global coordinates. */ |
| 1036 | touch->grab_x = x; |
| 1037 | touch->grab_y = y; |
| 1038 | |
| 1039 | switch (touch_type) { |
| 1040 | case WL_TOUCH_DOWN: |
| 1041 | weston_compositor_idle_inhibit(ec); |
| 1042 | |
| 1043 | seat->num_tp++; |
| 1044 | |
| 1045 | /* the first finger down picks the surface, and all further go |
| 1046 | * to that surface for the remainder of the touch session i.e. |
| 1047 | * until all touch points are up again. */ |
| 1048 | if (seat->num_tp == 1) { |
| 1049 | es = weston_compositor_pick_surface(ec, x, y, &sx, &sy); |
| 1050 | touch_set_focus(seat, &es->surface); |
| 1051 | } else if (touch->focus) { |
| 1052 | es = (struct weston_surface *) touch->focus; |
| 1053 | weston_surface_from_global_fixed(es, x, y, &sx, &sy); |
| 1054 | } else { |
| 1055 | /* Unexpected condition: We have non-initial touch but |
| 1056 | * there is no focused surface. |
| 1057 | */ |
| 1058 | weston_log("touch event received with %d points down" |
| 1059 | "but no surface focused\n", seat->num_tp); |
| 1060 | return; |
| 1061 | } |
| 1062 | |
| 1063 | grab->interface->down(grab, time, touch_id, sx, sy); |
| 1064 | break; |
| 1065 | case WL_TOUCH_MOTION: |
| 1066 | es = (struct weston_surface *) touch->focus; |
| 1067 | if (!es) |
| 1068 | break; |
| 1069 | |
| 1070 | weston_surface_from_global_fixed(es, x, y, &sx, &sy); |
| 1071 | grab->interface->motion(grab, time, touch_id, sx, sy); |
| 1072 | break; |
| 1073 | case WL_TOUCH_UP: |
| 1074 | weston_compositor_idle_release(ec); |
| 1075 | seat->num_tp--; |
| 1076 | |
| 1077 | grab->interface->up(grab, time, touch_id); |
| 1078 | if (seat->num_tp == 0) |
| 1079 | touch_set_focus(seat, NULL); |
| 1080 | break; |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | static void |
| 1085 | pointer_handle_sprite_destroy(struct wl_listener *listener, void *data) |
| 1086 | { |
| 1087 | struct weston_seat *seat = container_of(listener, struct weston_seat, |
| 1088 | sprite_destroy_listener); |
| 1089 | |
| 1090 | seat->sprite = NULL; |
| 1091 | } |
| 1092 | |
| 1093 | static void |
| 1094 | pointer_cursor_surface_configure(struct weston_surface *es, |
| 1095 | int32_t dx, int32_t dy, int32_t width, int32_t height) |
| 1096 | { |
| 1097 | struct weston_seat *seat = es->configure_private; |
| 1098 | int x, y; |
| 1099 | |
| 1100 | if (width == 0) |
| 1101 | return; |
| 1102 | |
| 1103 | assert(es == seat->sprite); |
| 1104 | |
| 1105 | seat->hotspot_x -= dx; |
| 1106 | seat->hotspot_y -= dy; |
| 1107 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1108 | x = wl_fixed_to_int(seat->pointer->x) - seat->hotspot_x; |
| 1109 | y = wl_fixed_to_int(seat->pointer->y) - seat->hotspot_y; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1110 | |
| 1111 | weston_surface_configure(seat->sprite, x, y, |
| 1112 | width, height); |
| 1113 | |
| 1114 | empty_region(&es->pending.input); |
| 1115 | |
| 1116 | if (!weston_surface_is_mapped(es)) { |
| 1117 | wl_list_insert(&es->compositor->cursor_layer.surface_list, |
| 1118 | &es->layer_link); |
| 1119 | weston_surface_update_transform(es); |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | static void |
| 1124 | pointer_unmap_sprite(struct weston_seat *seat) |
| 1125 | { |
| 1126 | if (weston_surface_is_mapped(seat->sprite)) |
| 1127 | weston_surface_unmap(seat->sprite); |
| 1128 | |
| 1129 | wl_list_remove(&seat->sprite_destroy_listener.link); |
| 1130 | seat->sprite->configure = NULL; |
| 1131 | seat->sprite->configure_private = NULL; |
| 1132 | seat->sprite = NULL; |
| 1133 | } |
| 1134 | |
| 1135 | static void |
| 1136 | pointer_set_cursor(struct wl_client *client, struct wl_resource *resource, |
| 1137 | uint32_t serial, struct wl_resource *surface_resource, |
| 1138 | int32_t x, int32_t y) |
| 1139 | { |
| 1140 | struct weston_seat *seat = resource->data; |
| 1141 | struct weston_surface *surface = NULL; |
| 1142 | |
| 1143 | if (surface_resource) |
| 1144 | surface = surface_resource->data; |
| 1145 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1146 | if (seat->pointer->focus == NULL) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1147 | return; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1148 | if (seat->pointer->focus->resource.client != client) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1149 | return; |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1150 | if (seat->pointer->focus_serial - serial > UINT32_MAX / 2) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1151 | return; |
| 1152 | |
| 1153 | if (surface && surface != seat->sprite) { |
| 1154 | if (surface->configure) { |
| 1155 | wl_resource_post_error(&surface->surface.resource, |
| 1156 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 1157 | "surface->configure already " |
| 1158 | "set"); |
| 1159 | return; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | if (seat->sprite) |
| 1164 | pointer_unmap_sprite(seat); |
| 1165 | |
| 1166 | if (!surface) |
| 1167 | return; |
| 1168 | |
| 1169 | wl_signal_add(&surface->surface.resource.destroy_signal, |
| 1170 | &seat->sprite_destroy_listener); |
| 1171 | |
| 1172 | surface->configure = pointer_cursor_surface_configure; |
| 1173 | surface->configure_private = seat; |
| 1174 | seat->sprite = surface; |
| 1175 | seat->hotspot_x = x; |
| 1176 | seat->hotspot_y = y; |
| 1177 | |
| 1178 | if (surface->buffer_ref.buffer) |
| 1179 | pointer_cursor_surface_configure(surface, 0, 0, weston_surface_buffer_width(surface), |
| 1180 | weston_surface_buffer_height(surface)); |
| 1181 | } |
| 1182 | |
| 1183 | static const struct wl_pointer_interface pointer_interface = { |
| 1184 | pointer_set_cursor |
| 1185 | }; |
| 1186 | |
| 1187 | static void |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1188 | seat_get_pointer(struct wl_client *client, struct wl_resource *resource, |
| 1189 | uint32_t id) |
| 1190 | { |
| 1191 | struct weston_seat *seat = resource->data; |
| 1192 | struct wl_resource *cr; |
| 1193 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1194 | if (!seat->pointer) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1195 | return; |
| 1196 | |
| 1197 | cr = wl_client_add_object(client, &wl_pointer_interface, |
| 1198 | &pointer_interface, id, seat); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1199 | wl_list_insert(&seat->pointer->resource_list, &cr->link); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1200 | cr->destroy = unbind_resource; |
| 1201 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1202 | if (seat->pointer->focus && |
| 1203 | seat->pointer->focus->resource.client == client) { |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1204 | struct weston_surface *surface; |
| 1205 | wl_fixed_t sx, sy; |
| 1206 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1207 | surface = (struct weston_surface *) seat->pointer->focus; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1208 | weston_surface_from_global_fixed(surface, |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1209 | seat->pointer->x, |
| 1210 | seat->pointer->y, |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1211 | &sx, |
| 1212 | &sy); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1213 | weston_pointer_set_focus(seat->pointer, |
| 1214 | seat->pointer->focus, |
Kristian Høgsberg | 02bbabb | 2013-05-06 22:15:05 -0400 | [diff] [blame] | 1215 | sx, |
| 1216 | sy); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | static void |
| 1221 | seat_get_keyboard(struct wl_client *client, struct wl_resource *resource, |
| 1222 | uint32_t id) |
| 1223 | { |
| 1224 | struct weston_seat *seat = resource->data; |
| 1225 | struct wl_resource *cr; |
| 1226 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1227 | if (!seat->keyboard) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1228 | return; |
| 1229 | |
| 1230 | cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id, |
| 1231 | seat); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1232 | wl_list_insert(&seat->keyboard->resource_list, &cr->link); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1233 | cr->destroy = unbind_resource; |
| 1234 | |
| 1235 | wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, |
| 1236 | seat->xkb_info.keymap_fd, |
| 1237 | seat->xkb_info.keymap_size); |
| 1238 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1239 | if (seat->keyboard->focus && |
| 1240 | seat->keyboard->focus->resource.client == client) { |
| 1241 | weston_keyboard_set_focus(seat->keyboard, |
| 1242 | seat->keyboard->focus); |
| 1243 | wl_data_device_set_keyboard_focus(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | static void |
| 1248 | seat_get_touch(struct wl_client *client, struct wl_resource *resource, |
| 1249 | uint32_t id) |
| 1250 | { |
| 1251 | struct weston_seat *seat = resource->data; |
| 1252 | struct wl_resource *cr; |
| 1253 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1254 | if (!seat->touch) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1255 | return; |
| 1256 | |
| 1257 | cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1258 | wl_list_insert(&seat->touch->resource_list, &cr->link); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1259 | cr->destroy = unbind_resource; |
| 1260 | } |
| 1261 | |
| 1262 | static const struct wl_seat_interface seat_interface = { |
| 1263 | seat_get_pointer, |
| 1264 | seat_get_keyboard, |
| 1265 | seat_get_touch, |
| 1266 | }; |
| 1267 | |
| 1268 | static void |
| 1269 | bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id) |
| 1270 | { |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1271 | struct weston_seat *seat = data; |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1272 | struct wl_resource *resource; |
| 1273 | enum wl_seat_capability caps = 0; |
| 1274 | |
| 1275 | resource = wl_client_add_object(client, &wl_seat_interface, |
| 1276 | &seat_interface, id, data); |
| 1277 | wl_list_insert(&seat->base_resource_list, &resource->link); |
| 1278 | resource->destroy = unbind_resource; |
| 1279 | |
| 1280 | if (seat->pointer) |
| 1281 | caps |= WL_SEAT_CAPABILITY_POINTER; |
| 1282 | if (seat->keyboard) |
| 1283 | caps |= WL_SEAT_CAPABILITY_KEYBOARD; |
| 1284 | if (seat->touch) |
| 1285 | caps |= WL_SEAT_CAPABILITY_TOUCH; |
| 1286 | |
| 1287 | wl_seat_send_capabilities(resource, caps); |
| 1288 | } |
| 1289 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1290 | int |
| 1291 | weston_compositor_xkb_init(struct weston_compositor *ec, |
| 1292 | struct xkb_rule_names *names) |
| 1293 | { |
| 1294 | if (ec->xkb_context == NULL) { |
| 1295 | ec->xkb_context = xkb_context_new(0); |
| 1296 | if (ec->xkb_context == NULL) { |
| 1297 | weston_log("failed to create XKB context\n"); |
| 1298 | return -1; |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | if (names) |
| 1303 | ec->xkb_names = *names; |
| 1304 | if (!ec->xkb_names.rules) |
| 1305 | ec->xkb_names.rules = strdup("evdev"); |
| 1306 | if (!ec->xkb_names.model) |
| 1307 | ec->xkb_names.model = strdup("pc105"); |
| 1308 | if (!ec->xkb_names.layout) |
| 1309 | ec->xkb_names.layout = strdup("us"); |
| 1310 | |
| 1311 | return 0; |
| 1312 | } |
| 1313 | |
| 1314 | static void xkb_info_destroy(struct weston_xkb_info *xkb_info) |
| 1315 | { |
| 1316 | if (xkb_info->keymap) |
| 1317 | xkb_map_unref(xkb_info->keymap); |
| 1318 | |
| 1319 | if (xkb_info->keymap_area) |
| 1320 | munmap(xkb_info->keymap_area, xkb_info->keymap_size); |
| 1321 | if (xkb_info->keymap_fd >= 0) |
| 1322 | close(xkb_info->keymap_fd); |
| 1323 | } |
| 1324 | |
| 1325 | void |
| 1326 | weston_compositor_xkb_destroy(struct weston_compositor *ec) |
| 1327 | { |
| 1328 | free((char *) ec->xkb_names.rules); |
| 1329 | free((char *) ec->xkb_names.model); |
| 1330 | free((char *) ec->xkb_names.layout); |
| 1331 | free((char *) ec->xkb_names.variant); |
| 1332 | free((char *) ec->xkb_names.options); |
| 1333 | |
| 1334 | xkb_info_destroy(&ec->xkb_info); |
| 1335 | xkb_context_unref(ec->xkb_context); |
| 1336 | } |
| 1337 | |
| 1338 | static int |
| 1339 | weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info) |
| 1340 | { |
| 1341 | char *keymap_str; |
| 1342 | |
| 1343 | xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap, |
| 1344 | XKB_MOD_NAME_SHIFT); |
| 1345 | xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap, |
| 1346 | XKB_MOD_NAME_CAPS); |
| 1347 | xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap, |
| 1348 | XKB_MOD_NAME_CTRL); |
| 1349 | xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap, |
| 1350 | XKB_MOD_NAME_ALT); |
| 1351 | xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2"); |
| 1352 | xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3"); |
| 1353 | xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap, |
| 1354 | XKB_MOD_NAME_LOGO); |
| 1355 | xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5"); |
| 1356 | |
| 1357 | xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap, |
| 1358 | XKB_LED_NAME_NUM); |
| 1359 | xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap, |
| 1360 | XKB_LED_NAME_CAPS); |
| 1361 | xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap, |
| 1362 | XKB_LED_NAME_SCROLL); |
| 1363 | |
| 1364 | keymap_str = xkb_map_get_as_string(xkb_info->keymap); |
| 1365 | if (keymap_str == NULL) { |
| 1366 | weston_log("failed to get string version of keymap\n"); |
| 1367 | return -1; |
| 1368 | } |
| 1369 | xkb_info->keymap_size = strlen(keymap_str) + 1; |
| 1370 | |
| 1371 | xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size); |
| 1372 | if (xkb_info->keymap_fd < 0) { |
| 1373 | weston_log("creating a keymap file for %lu bytes failed: %m\n", |
| 1374 | (unsigned long) xkb_info->keymap_size); |
| 1375 | goto err_keymap_str; |
| 1376 | } |
| 1377 | |
| 1378 | xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size, |
| 1379 | PROT_READ | PROT_WRITE, |
| 1380 | MAP_SHARED, xkb_info->keymap_fd, 0); |
| 1381 | if (xkb_info->keymap_area == MAP_FAILED) { |
| 1382 | weston_log("failed to mmap() %lu bytes\n", |
| 1383 | (unsigned long) xkb_info->keymap_size); |
| 1384 | goto err_dev_zero; |
| 1385 | } |
| 1386 | strcpy(xkb_info->keymap_area, keymap_str); |
| 1387 | free(keymap_str); |
| 1388 | |
| 1389 | return 0; |
| 1390 | |
| 1391 | err_dev_zero: |
| 1392 | close(xkb_info->keymap_fd); |
| 1393 | xkb_info->keymap_fd = -1; |
| 1394 | err_keymap_str: |
| 1395 | free(keymap_str); |
| 1396 | return -1; |
| 1397 | } |
| 1398 | |
| 1399 | static int |
| 1400 | weston_compositor_build_global_keymap(struct weston_compositor *ec) |
| 1401 | { |
| 1402 | if (ec->xkb_info.keymap != NULL) |
| 1403 | return 0; |
| 1404 | |
| 1405 | ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context, |
| 1406 | &ec->xkb_names, |
| 1407 | 0); |
| 1408 | if (ec->xkb_info.keymap == NULL) { |
| 1409 | weston_log("failed to compile global XKB keymap\n"); |
| 1410 | weston_log(" tried rules %s, model %s, layout %s, variant %s, " |
| 1411 | "options %s\n", |
| 1412 | ec->xkb_names.rules, ec->xkb_names.model, |
| 1413 | ec->xkb_names.layout, ec->xkb_names.variant, |
| 1414 | ec->xkb_names.options); |
| 1415 | return -1; |
| 1416 | } |
| 1417 | |
| 1418 | if (weston_xkb_info_new_keymap(&ec->xkb_info) < 0) |
| 1419 | return -1; |
| 1420 | |
| 1421 | return 0; |
| 1422 | } |
| 1423 | |
| 1424 | WL_EXPORT int |
| 1425 | weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap) |
| 1426 | { |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 1427 | struct weston_keyboard *keyboard; |
| 1428 | |
Kristian Høgsberg | 2bf8762 | 2013-05-07 23:17:41 -0400 | [diff] [blame] | 1429 | if (seat->keyboard) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1430 | return 0; |
| 1431 | |
| 1432 | if (keymap != NULL) { |
| 1433 | seat->xkb_info.keymap = xkb_map_ref(keymap); |
| 1434 | if (weston_xkb_info_new_keymap(&seat->xkb_info) < 0) |
| 1435 | return -1; |
| 1436 | } else { |
| 1437 | if (weston_compositor_build_global_keymap(seat->compositor) < 0) |
| 1438 | return -1; |
| 1439 | seat->xkb_info = seat->compositor->xkb_info; |
| 1440 | seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap); |
| 1441 | } |
| 1442 | |
| 1443 | seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap); |
| 1444 | if (seat->xkb_state.state == NULL) { |
| 1445 | weston_log("failed to initialise XKB state\n"); |
| 1446 | return -1; |
| 1447 | } |
| 1448 | |
| 1449 | seat->xkb_state.leds = 0; |
| 1450 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 1451 | keyboard = weston_keyboard_create(); |
| 1452 | if (keyboard == NULL) { |
| 1453 | weston_log("failed to allocate weston keyboard struct\n"); |
| 1454 | return -1; |
| 1455 | } |
| 1456 | |
| 1457 | seat->keyboard = keyboard; |
| 1458 | keyboard->seat = seat; |
| 1459 | |
| 1460 | seat_send_updated_caps(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1461 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1462 | return 0; |
| 1463 | } |
| 1464 | |
| 1465 | WL_EXPORT void |
| 1466 | weston_seat_init_pointer(struct weston_seat *seat) |
| 1467 | { |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 1468 | struct weston_pointer *pointer; |
| 1469 | |
Kristian Høgsberg | 2bf8762 | 2013-05-07 23:17:41 -0400 | [diff] [blame] | 1470 | if (seat->pointer) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1471 | return; |
| 1472 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 1473 | pointer = weston_pointer_create(); |
| 1474 | if (pointer == NULL) |
| 1475 | return; |
| 1476 | |
| 1477 | seat->pointer = pointer; |
| 1478 | pointer->seat = seat; |
| 1479 | |
| 1480 | seat_send_updated_caps(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | WL_EXPORT void |
| 1484 | weston_seat_init_touch(struct weston_seat *seat) |
| 1485 | { |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 1486 | struct weston_touch *touch; |
| 1487 | |
Kristian Høgsberg | 2bf8762 | 2013-05-07 23:17:41 -0400 | [diff] [blame] | 1488 | if (seat->touch) |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1489 | return; |
| 1490 | |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 1491 | touch = weston_touch_create(); |
| 1492 | if (touch == NULL) |
| 1493 | return; |
| 1494 | |
| 1495 | seat->touch = touch; |
| 1496 | touch->seat = seat; |
| 1497 | |
| 1498 | seat_send_updated_caps(seat); |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | WL_EXPORT void |
| 1502 | weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec) |
| 1503 | { |
Kristian Høgsberg | 4a2a274 | 2013-05-06 22:24:50 -0400 | [diff] [blame] | 1504 | memset(seat, 0, sizeof *seat); |
| 1505 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1506 | seat->selection_data_source = NULL; |
| 1507 | wl_list_init(&seat->base_resource_list); |
| 1508 | wl_signal_init(&seat->selection_signal); |
| 1509 | wl_list_init(&seat->drag_resource_list); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1510 | wl_signal_init(&seat->destroy_signal); |
Kristian Høgsberg | 4a2a274 | 2013-05-06 22:24:50 -0400 | [diff] [blame] | 1511 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1512 | wl_display_add_global(ec->wl_display, &wl_seat_interface, seat, |
| 1513 | bind_seat); |
| 1514 | |
| 1515 | seat->sprite = NULL; |
| 1516 | seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy; |
| 1517 | |
| 1518 | seat->compositor = ec; |
| 1519 | seat->hotspot_x = 16; |
| 1520 | seat->hotspot_y = 16; |
| 1521 | seat->modifier_state = 0; |
| 1522 | seat->num_tp = 0; |
| 1523 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1524 | wl_list_insert(ec->seat_list.prev, &seat->link); |
| 1525 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1526 | clipboard_create(seat); |
| 1527 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1528 | wl_signal_emit(&ec->seat_created_signal, seat); |
| 1529 | } |
| 1530 | |
| 1531 | WL_EXPORT void |
| 1532 | weston_seat_release(struct weston_seat *seat) |
| 1533 | { |
| 1534 | wl_list_remove(&seat->link); |
| 1535 | /* The global object is destroyed at wl_display_destroy() time. */ |
| 1536 | |
| 1537 | if (seat->sprite) |
| 1538 | pointer_unmap_sprite(seat); |
| 1539 | |
| 1540 | if (seat->xkb_state.state != NULL) |
| 1541 | xkb_state_unref(seat->xkb_state.state); |
| 1542 | xkb_info_destroy(&seat->xkb_info); |
| 1543 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1544 | if (seat->pointer) |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 1545 | weston_pointer_destroy(seat->pointer); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1546 | if (seat->keyboard) |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 1547 | weston_keyboard_destroy(seat->keyboard); |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1548 | if (seat->touch) |
Kristian Høgsberg | a4036bb | 2013-05-07 23:52:07 -0400 | [diff] [blame^] | 1549 | weston_touch_destroy(seat->touch); |
Kristian Høgsberg | 4a2a274 | 2013-05-06 22:24:50 -0400 | [diff] [blame] | 1550 | |
Kristian Høgsberg | b5e2610 | 2013-04-18 15:40:10 -0400 | [diff] [blame] | 1551 | wl_signal_emit(&seat->destroy_signal, seat); |
| 1552 | } |