Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1 | /* |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 2 | * Copyright © 2010-2011 Benjamin Franzke |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 3 | * Copyright © 2013 Jason Ekstrand |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 4 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 5 | * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | * a copy of this software and associated documentation files (the |
| 7 | * "Software"), to deal in the Software without restriction, including |
| 8 | * without limitation the rights to use, copy, modify, merge, publish, |
| 9 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | * permit persons to whom the Software is furnished to do so, subject to |
| 11 | * the following conditions: |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 12 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 13 | * The above copyright notice and this permission notice (including the |
| 14 | * next paragraph) shall be included in all copies or substantial |
| 15 | * portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | * SOFTWARE. |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 25 | */ |
| 26 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 27 | #include "config.h" |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 28 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 29 | #include <assert.h> |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 30 | #include <stddef.h> |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 31 | #include <stdint.h> |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <unistd.h> |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 37 | #include <sys/mman.h> |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 38 | #include <linux/input.h> |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 39 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 40 | #include <wayland-client.h> |
| 41 | #include <wayland-egl.h> |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 42 | #include <wayland-cursor.h> |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 43 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 44 | #include "compositor.h" |
Benoit Gschwind | 639322a | 2016-04-28 20:33:15 +0200 | [diff] [blame] | 45 | #include "compositor-wayland.h" |
John Kåre Alsaker | 30d2b1f | 2012-11-13 19:10:28 +0100 | [diff] [blame] | 46 | #include "gl-renderer.h" |
Vincent Abriou | c950667 | 2016-10-05 16:14:07 +0200 | [diff] [blame] | 47 | #include "weston-egl-ext.h" |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 48 | #include "pixman-renderer.h" |
Jon Cruz | 35b2eaa | 2015-06-15 15:37:08 -0700 | [diff] [blame] | 49 | #include "shared/helpers.h" |
Jon Cruz | 4678bab | 2015-06-15 15:37:07 -0700 | [diff] [blame] | 50 | #include "shared/image-loader.h" |
| 51 | #include "shared/os-compatibility.h" |
| 52 | #include "shared/cairo-util.h" |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 53 | #include "fullscreen-shell-unstable-v1-client-protocol.h" |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 54 | #include "presentation-time-server-protocol.h" |
Emmanuel Gil Peyrot | c59f18e | 2015-09-25 11:58:40 +0200 | [diff] [blame] | 55 | #include "linux-dmabuf.h" |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 56 | #include "windowed-output-api.h" |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 57 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 58 | #define WINDOW_TITLE "Weston Compositor" |
| 59 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 60 | struct wayland_backend { |
| 61 | struct weston_backend base; |
| 62 | struct weston_compositor *compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 63 | |
| 64 | struct { |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 65 | struct wl_display *wl_display; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 66 | struct wl_registry *registry; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 67 | struct wl_compositor *compositor; |
| 68 | struct wl_shell *shell; |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 69 | struct zwp_fullscreen_shell_v1 *fshell; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 70 | struct wl_shm *shm; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 71 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 72 | struct wl_list output_list; |
| 73 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 74 | struct wl_event_source *wl_source; |
| 75 | uint32_t event_mask; |
| 76 | } parent; |
| 77 | |
Armin Krezović | 7e71b87 | 2016-10-09 17:30:22 +0200 | [diff] [blame] | 78 | bool use_pixman; |
Armin Krezović | 7f1c0b8 | 2016-10-09 17:30:23 +0200 | [diff] [blame] | 79 | bool sprawl_across_outputs; |
Armin Krezović | 2045016 | 2016-10-13 12:01:43 +0200 | [diff] [blame] | 80 | bool fullscreen; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 81 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 82 | struct theme *theme; |
| 83 | cairo_device_t *frame_device; |
| 84 | struct wl_cursor_theme *cursor_theme; |
| 85 | struct wl_cursor *cursor; |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 86 | |
Jason Ekstrand | 06ced80 | 2013-11-07 20:13:29 -0600 | [diff] [blame] | 87 | struct wl_list input_list; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | struct wayland_output { |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 91 | struct weston_output base; |
| 92 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 93 | struct { |
Armin Krezović | 2d321e3 | 2016-10-09 17:30:25 +0200 | [diff] [blame] | 94 | bool draw_initial_frame; |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 95 | struct wl_surface *surface; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 96 | |
| 97 | struct wl_output *output; |
| 98 | uint32_t global_id; |
| 99 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 100 | struct wl_shell_surface *shell_surface; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 101 | int configure_width, configure_height; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 102 | } parent; |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 103 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 104 | int keyboard_count; |
| 105 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 106 | char *name; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 107 | struct frame *frame; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 108 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 109 | struct { |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 110 | struct wl_egl_window *egl_window; |
| 111 | struct { |
| 112 | cairo_surface_t *top; |
| 113 | cairo_surface_t *left; |
| 114 | cairo_surface_t *right; |
| 115 | cairo_surface_t *bottom; |
| 116 | } border; |
| 117 | } gl; |
| 118 | |
| 119 | struct { |
| 120 | struct wl_list buffers; |
| 121 | struct wl_list free_buffers; |
| 122 | } shm; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 123 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 124 | struct weston_mode mode; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 125 | uint32_t scale; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 126 | }; |
| 127 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 128 | struct wayland_parent_output { |
| 129 | struct wayland_output *output; |
| 130 | struct wl_list link; |
| 131 | |
| 132 | struct wl_output *global; |
| 133 | uint32_t id; |
| 134 | |
| 135 | struct { |
| 136 | char *make; |
| 137 | char *model; |
| 138 | int32_t width, height; |
| 139 | uint32_t subpixel; |
| 140 | } physical; |
| 141 | |
| 142 | int32_t x, y; |
| 143 | uint32_t transform; |
| 144 | uint32_t scale; |
| 145 | |
| 146 | struct wl_list mode_list; |
| 147 | struct weston_mode *preferred_mode; |
| 148 | struct weston_mode *current_mode; |
| 149 | }; |
| 150 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 151 | struct wayland_shm_buffer { |
| 152 | struct wayland_output *output; |
| 153 | struct wl_list link; |
| 154 | struct wl_list free_link; |
| 155 | |
| 156 | struct wl_buffer *buffer; |
| 157 | void *data; |
| 158 | size_t size; |
| 159 | pixman_region32_t damage; |
| 160 | int frame_damaged; |
| 161 | |
| 162 | pixman_image_t *pm_image; |
| 163 | cairo_surface_t *c_surface; |
| 164 | }; |
| 165 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 166 | struct wayland_input { |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 167 | struct weston_seat base; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 168 | struct wayland_backend *backend; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 169 | struct wl_list link; |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 170 | |
| 171 | struct { |
| 172 | struct wl_seat *seat; |
| 173 | struct wl_pointer *pointer; |
| 174 | struct wl_keyboard *keyboard; |
| 175 | struct wl_touch *touch; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 176 | |
| 177 | struct { |
| 178 | struct wl_surface *surface; |
| 179 | int32_t hx, hy; |
| 180 | } cursor; |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 181 | } parent; |
| 182 | |
Jason Ekstrand | b7d9f2e | 2014-04-02 19:53:57 -0500 | [diff] [blame] | 183 | enum weston_key_state_update keyboard_state_update; |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 184 | uint32_t key_serial; |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 185 | uint32_t enter_serial; |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 186 | uint32_t touch_points; |
| 187 | bool touch_active; |
Derek Foreman | 4bcc54d | 2015-11-06 15:56:06 -0600 | [diff] [blame] | 188 | bool has_focus; |
Derek Foreman | cfce7d0 | 2015-11-06 15:56:08 -0600 | [diff] [blame] | 189 | int seat_version; |
Derek Foreman | 4bcc54d | 2015-11-06 15:56:06 -0600 | [diff] [blame] | 190 | |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 191 | struct wayland_output *output; |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 192 | struct wayland_output *touch_focus; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 193 | struct wayland_output *keyboard_focus; |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 194 | |
| 195 | struct weston_pointer_axis_event vert, horiz; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 196 | }; |
| 197 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 198 | struct gl_renderer_interface *gl_renderer; |
| 199 | |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 200 | static inline struct wayland_output * |
| 201 | to_wayland_output(struct weston_output *base) |
| 202 | { |
| 203 | return container_of(base, struct wayland_output, base); |
| 204 | } |
| 205 | |
| 206 | static inline struct wayland_backend * |
| 207 | to_wayland_backend(struct weston_compositor *base) |
| 208 | { |
| 209 | return container_of(base->backend, struct wayland_backend, base); |
| 210 | } |
| 211 | |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 212 | static void |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 213 | wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer) |
| 214 | { |
| 215 | cairo_surface_destroy(buffer->c_surface); |
| 216 | pixman_image_unref(buffer->pm_image); |
| 217 | |
| 218 | wl_buffer_destroy(buffer->buffer); |
| 219 | munmap(buffer->data, buffer->size); |
| 220 | |
| 221 | pixman_region32_fini(&buffer->damage); |
| 222 | |
| 223 | wl_list_remove(&buffer->link); |
| 224 | wl_list_remove(&buffer->free_link); |
| 225 | free(buffer); |
| 226 | } |
| 227 | |
| 228 | static void |
| 229 | buffer_release(void *data, struct wl_buffer *buffer) |
| 230 | { |
| 231 | struct wayland_shm_buffer *sb = data; |
| 232 | |
| 233 | if (sb->output) { |
| 234 | wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link); |
| 235 | } else { |
| 236 | wayland_shm_buffer_destroy(sb); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | static const struct wl_buffer_listener buffer_listener = { |
| 241 | buffer_release |
| 242 | }; |
| 243 | |
| 244 | static struct wayland_shm_buffer * |
| 245 | wayland_output_get_shm_buffer(struct wayland_output *output) |
| 246 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 247 | struct wayland_backend *b = |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 248 | to_wayland_backend(output->base.compositor); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 249 | struct wl_shm *shm = b->parent.shm; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 250 | struct wayland_shm_buffer *sb; |
| 251 | |
| 252 | struct wl_shm_pool *pool; |
| 253 | int width, height, stride; |
| 254 | int32_t fx, fy; |
| 255 | int fd; |
| 256 | unsigned char *data; |
| 257 | |
| 258 | if (!wl_list_empty(&output->shm.free_buffers)) { |
| 259 | sb = container_of(output->shm.free_buffers.next, |
| 260 | struct wayland_shm_buffer, free_link); |
| 261 | wl_list_remove(&sb->free_link); |
| 262 | wl_list_init(&sb->free_link); |
| 263 | |
| 264 | return sb; |
| 265 | } |
| 266 | |
| 267 | if (output->frame) { |
| 268 | width = frame_width(output->frame); |
| 269 | height = frame_height(output->frame); |
| 270 | } else { |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 271 | width = output->base.current_mode->width; |
| 272 | height = output->base.current_mode->height; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width); |
| 276 | |
| 277 | fd = os_create_anonymous_file(height * stride); |
| 278 | if (fd < 0) { |
Bryce W. Harrington | a093502 | 2014-03-21 05:54:02 +0000 | [diff] [blame] | 279 | weston_log("could not create an anonymous file buffer: %m\n"); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 280 | return NULL; |
| 281 | } |
| 282 | |
| 283 | data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 284 | if (data == MAP_FAILED) { |
Bryce W. Harrington | a093502 | 2014-03-21 05:54:02 +0000 | [diff] [blame] | 285 | weston_log("could not mmap %d memory for data: %m\n", height * stride); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 286 | close(fd); |
| 287 | return NULL; |
| 288 | } |
| 289 | |
| 290 | sb = zalloc(sizeof *sb); |
Bryce W. Harrington | bfd74f4 | 2014-04-21 23:51:02 +0000 | [diff] [blame] | 291 | if (sb == NULL) { |
Thierry Reding | 6ac60c1 | 2014-05-27 09:08:29 +0200 | [diff] [blame] | 292 | weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb); |
Bryce W. Harrington | bfd74f4 | 2014-04-21 23:51:02 +0000 | [diff] [blame] | 293 | close(fd); |
| 294 | free(data); |
| 295 | return NULL; |
| 296 | } |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 297 | |
| 298 | sb->output = output; |
| 299 | wl_list_init(&sb->free_link); |
| 300 | wl_list_insert(&output->shm.buffers, &sb->link); |
| 301 | |
| 302 | pixman_region32_init_rect(&sb->damage, 0, 0, |
| 303 | output->base.width, output->base.height); |
| 304 | sb->frame_damaged = 1; |
| 305 | |
| 306 | sb->data = data; |
| 307 | sb->size = height * stride; |
| 308 | |
| 309 | pool = wl_shm_create_pool(shm, fd, sb->size); |
| 310 | |
| 311 | sb->buffer = wl_shm_pool_create_buffer(pool, 0, |
| 312 | width, height, |
| 313 | stride, |
| 314 | WL_SHM_FORMAT_ARGB8888); |
| 315 | wl_buffer_add_listener(sb->buffer, &buffer_listener, sb); |
| 316 | wl_shm_pool_destroy(pool); |
| 317 | close(fd); |
| 318 | |
| 319 | memset(data, 0, sb->size); |
| 320 | |
| 321 | sb->c_surface = |
| 322 | cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, |
| 323 | width, height, stride); |
| 324 | |
| 325 | fx = 0; |
| 326 | fy = 0; |
| 327 | if (output->frame) |
| 328 | frame_interior(output->frame, &fx, &fy, 0, 0); |
| 329 | sb->pm_image = |
| 330 | pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height, |
| 331 | (uint32_t *)(data + fy * stride) + fx, |
| 332 | stride); |
| 333 | |
| 334 | return sb; |
| 335 | } |
| 336 | |
| 337 | static void |
Kristian Høgsberg | cdd61d0 | 2012-02-07 09:56:15 -0500 | [diff] [blame] | 338 | frame_done(void *data, struct wl_callback *callback, uint32_t time) |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 339 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 340 | struct weston_output *output = data; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 341 | struct timespec ts; |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 342 | |
Kristian Høgsberg | cdd61d0 | 2012-02-07 09:56:15 -0500 | [diff] [blame] | 343 | wl_callback_destroy(callback); |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 344 | |
| 345 | /* XXX: use the presentation extension for proper timings */ |
Pekka Paalanen | 04f8a9b | 2015-04-02 16:26:06 +0300 | [diff] [blame] | 346 | |
| 347 | /* |
| 348 | * This is the fallback case, where Presentation extension is not |
| 349 | * available from the parent compositor. We do not know the base for |
| 350 | * 'time', so we cannot feed it to finish_frame(). Do the only thing |
| 351 | * we can, and pretend finish_frame time is when we process this |
| 352 | * event. |
| 353 | */ |
| 354 | weston_compositor_read_presentation_clock(output->compositor, &ts); |
Pekka Paalanen | 363aa7b | 2014-12-17 16:20:40 +0200 | [diff] [blame] | 355 | weston_output_finish_frame(output, &ts, 0); |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static const struct wl_callback_listener frame_listener = { |
| 359 | frame_done |
| 360 | }; |
| 361 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 362 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 363 | draw_initial_frame(struct wayland_output *output) |
| 364 | { |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 365 | struct wayland_shm_buffer *sb; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 366 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 367 | sb = wayland_output_get_shm_buffer(output); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 368 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 369 | /* If we are rendering with GL, then orphan it so that it gets |
| 370 | * destroyed immediately */ |
| 371 | if (output->gl.egl_window) |
| 372 | sb->output = NULL; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 373 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 374 | wl_surface_attach(output->parent.surface, sb->buffer, 0, 0); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 375 | wl_surface_damage(output->parent.surface, 0, 0, |
| 376 | output->base.current_mode->width, |
| 377 | output->base.current_mode->height); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static void |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 381 | wayland_output_update_gl_border(struct wayland_output *output) |
| 382 | { |
| 383 | int32_t ix, iy, iwidth, iheight, fwidth, fheight; |
| 384 | cairo_t *cr; |
| 385 | |
| 386 | if (!output->frame) |
| 387 | return; |
| 388 | if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT)) |
| 389 | return; |
| 390 | |
| 391 | fwidth = frame_width(output->frame); |
| 392 | fheight = frame_height(output->frame); |
| 393 | frame_interior(output->frame, &ix, &iy, &iwidth, &iheight); |
| 394 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 395 | if (!output->gl.border.top) |
| 396 | output->gl.border.top = |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 397 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 398 | fwidth, iy); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 399 | cr = cairo_create(output->gl.border.top); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 400 | frame_repaint(output->frame, cr); |
| 401 | cairo_destroy(cr); |
| 402 | gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP, |
| 403 | fwidth, iy, |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 404 | cairo_image_surface_get_stride(output->gl.border.top) / 4, |
| 405 | cairo_image_surface_get_data(output->gl.border.top)); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 406 | |
| 407 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 408 | if (!output->gl.border.left) |
| 409 | output->gl.border.left = |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 410 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 411 | ix, 1); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 412 | cr = cairo_create(output->gl.border.left); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 413 | cairo_translate(cr, 0, -iy); |
| 414 | frame_repaint(output->frame, cr); |
| 415 | cairo_destroy(cr); |
| 416 | gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT, |
| 417 | ix, 1, |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 418 | cairo_image_surface_get_stride(output->gl.border.left) / 4, |
| 419 | cairo_image_surface_get_data(output->gl.border.left)); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 420 | |
| 421 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 422 | if (!output->gl.border.right) |
| 423 | output->gl.border.right = |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 424 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 425 | fwidth - (ix + iwidth), 1); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 426 | cr = cairo_create(output->gl.border.right); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 427 | cairo_translate(cr, -(iwidth + ix), -iy); |
| 428 | frame_repaint(output->frame, cr); |
| 429 | cairo_destroy(cr); |
| 430 | gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT, |
| 431 | fwidth - (ix + iwidth), 1, |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 432 | cairo_image_surface_get_stride(output->gl.border.right) / 4, |
| 433 | cairo_image_surface_get_data(output->gl.border.right)); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 434 | |
| 435 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 436 | if (!output->gl.border.bottom) |
| 437 | output->gl.border.bottom = |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 438 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 439 | fwidth, fheight - (iy + iheight)); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 440 | cr = cairo_create(output->gl.border.bottom); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 441 | cairo_translate(cr, 0, -(iy + iheight)); |
| 442 | frame_repaint(output->frame, cr); |
| 443 | cairo_destroy(cr); |
| 444 | gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM, |
| 445 | fwidth, fheight - (iy + iheight), |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 446 | cairo_image_surface_get_stride(output->gl.border.bottom) / 4, |
| 447 | cairo_image_surface_get_data(output->gl.border.bottom)); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 451 | wayland_output_start_repaint_loop(struct weston_output *output_base) |
| 452 | { |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 453 | struct wayland_output *output = to_wayland_output(output_base); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 454 | struct wayland_backend *wb = |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 455 | to_wayland_backend(output->base.compositor); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 456 | struct wl_callback *callback; |
| 457 | |
| 458 | /* If this is the initial frame, we need to attach a buffer so that |
| 459 | * the compositor can map the surface and include it in its render |
| 460 | * loop. If the surface doesn't end up in the render loop, the frame |
| 461 | * callback won't be invoked. The buffer is transparent and of the |
| 462 | * same size as the future real output buffer. */ |
| 463 | if (output->parent.draw_initial_frame) { |
Armin Krezović | 2d321e3 | 2016-10-09 17:30:25 +0200 | [diff] [blame] | 464 | output->parent.draw_initial_frame = false; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 465 | |
| 466 | draw_initial_frame(output); |
| 467 | } |
| 468 | |
| 469 | callback = wl_surface_frame(output->parent.surface); |
| 470 | wl_callback_add_listener(callback, &frame_listener, output); |
| 471 | wl_surface_commit(output->parent.surface); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 472 | wl_display_flush(wb->parent.wl_display); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 473 | } |
| 474 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 475 | static int |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 476 | wayland_output_repaint_gl(struct weston_output *output_base, |
| 477 | pixman_region32_t *damage) |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 478 | { |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 479 | struct wayland_output *output = to_wayland_output(output_base); |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame] | 480 | struct weston_compositor *ec = output->base.compositor; |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 481 | struct wl_callback *callback; |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 482 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 483 | callback = wl_surface_frame(output->parent.surface); |
| 484 | wl_callback_add_listener(callback, &frame_listener, output); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 485 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 486 | wayland_output_update_gl_border(output); |
| 487 | |
Pekka Paalanen | bc10638 | 2012-10-10 12:49:31 +0300 | [diff] [blame] | 488 | ec->renderer->repaint_output(&output->base, damage); |
Ander Conselvan de Oliveira | 0a88772 | 2012-11-22 15:57:00 +0200 | [diff] [blame] | 489 | |
| 490 | pixman_region32_subtract(&ec->primary_plane.damage, |
| 491 | &ec->primary_plane.damage, damage); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 492 | return 0; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 493 | } |
| 494 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 495 | static void |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 496 | wayland_output_update_shm_border(struct wayland_shm_buffer *buffer) |
| 497 | { |
| 498 | int32_t ix, iy, iwidth, iheight, fwidth, fheight; |
| 499 | cairo_t *cr; |
| 500 | |
| 501 | if (!buffer->output->frame || !buffer->frame_damaged) |
| 502 | return; |
| 503 | |
| 504 | cr = cairo_create(buffer->c_surface); |
| 505 | |
| 506 | frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight); |
| 507 | fwidth = frame_width(buffer->output->frame); |
| 508 | fheight = frame_height(buffer->output->frame); |
| 509 | |
| 510 | /* Set the clip so we don't unnecisaraly damage the surface */ |
| 511 | cairo_move_to(cr, ix, iy); |
| 512 | cairo_rel_line_to(cr, iwidth, 0); |
| 513 | cairo_rel_line_to(cr, 0, iheight); |
| 514 | cairo_rel_line_to(cr, -iwidth, 0); |
| 515 | cairo_line_to(cr, ix, iy); |
| 516 | cairo_line_to(cr, 0, iy); |
| 517 | cairo_line_to(cr, 0, fheight); |
| 518 | cairo_line_to(cr, fwidth, fheight); |
| 519 | cairo_line_to(cr, fwidth, 0); |
| 520 | cairo_line_to(cr, 0, 0); |
| 521 | cairo_line_to(cr, 0, iy); |
| 522 | cairo_close_path(cr); |
| 523 | cairo_clip(cr); |
| 524 | |
| 525 | /* Draw using a pattern so that the final result gets clipped */ |
| 526 | cairo_push_group(cr); |
| 527 | frame_repaint(buffer->output->frame, cr); |
| 528 | cairo_pop_group_to_source(cr); |
| 529 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 530 | cairo_paint(cr); |
| 531 | |
| 532 | cairo_destroy(cr); |
| 533 | } |
| 534 | |
| 535 | static void |
| 536 | wayland_shm_buffer_attach(struct wayland_shm_buffer *sb) |
| 537 | { |
| 538 | pixman_region32_t damage; |
| 539 | pixman_box32_t *rects; |
| 540 | int32_t ix, iy, iwidth, iheight, fwidth, fheight; |
| 541 | int i, n; |
| 542 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 543 | pixman_region32_init(&damage); |
| 544 | weston_transformed_region(sb->output->base.width, |
| 545 | sb->output->base.height, |
| 546 | sb->output->base.transform, |
| 547 | sb->output->base.current_scale, |
| 548 | &sb->damage, &damage); |
| 549 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 550 | if (sb->output->frame) { |
| 551 | frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight); |
| 552 | fwidth = frame_width(sb->output->frame); |
| 553 | fheight = frame_height(sb->output->frame); |
| 554 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 555 | pixman_region32_translate(&damage, ix, iy); |
| 556 | |
| 557 | if (sb->frame_damaged) { |
| 558 | pixman_region32_union_rect(&damage, &damage, |
| 559 | 0, 0, fwidth, iy); |
| 560 | pixman_region32_union_rect(&damage, &damage, |
| 561 | 0, iy, ix, iheight); |
| 562 | pixman_region32_union_rect(&damage, &damage, |
| 563 | ix + iwidth, iy, |
| 564 | fwidth - (ix + iwidth), iheight); |
| 565 | pixman_region32_union_rect(&damage, &damage, |
| 566 | 0, iy + iheight, |
| 567 | fwidth, fheight - (iy + iheight)); |
| 568 | } |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 569 | } |
| 570 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 571 | rects = pixman_region32_rectangles(&damage, &n); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 572 | wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0); |
| 573 | for (i = 0; i < n; ++i) |
| 574 | wl_surface_damage(sb->output->parent.surface, rects[i].x1, |
| 575 | rects[i].y1, rects[i].x2 - rects[i].x1, |
| 576 | rects[i].y2 - rects[i].y1); |
| 577 | |
| 578 | if (sb->output->frame) |
| 579 | pixman_region32_fini(&damage); |
| 580 | } |
| 581 | |
| 582 | static int |
| 583 | wayland_output_repaint_pixman(struct weston_output *output_base, |
| 584 | pixman_region32_t *damage) |
| 585 | { |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 586 | struct wayland_output *output = to_wayland_output(output_base); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 587 | struct wayland_backend *b = |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 588 | to_wayland_backend(output->base.compositor); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 589 | struct wl_callback *callback; |
| 590 | struct wayland_shm_buffer *sb; |
| 591 | |
| 592 | if (output->frame) { |
| 593 | if (frame_status(output->frame) & FRAME_STATUS_REPAINT) |
| 594 | wl_list_for_each(sb, &output->shm.buffers, link) |
| 595 | sb->frame_damaged = 1; |
| 596 | } |
| 597 | |
| 598 | wl_list_for_each(sb, &output->shm.buffers, link) |
| 599 | pixman_region32_union(&sb->damage, &sb->damage, damage); |
| 600 | |
| 601 | sb = wayland_output_get_shm_buffer(output); |
| 602 | |
| 603 | wayland_output_update_shm_border(sb); |
| 604 | pixman_renderer_output_set_buffer(output_base, sb->pm_image); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 605 | b->compositor->renderer->repaint_output(output_base, &sb->damage); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 606 | |
| 607 | wayland_shm_buffer_attach(sb); |
| 608 | |
| 609 | callback = wl_surface_frame(output->parent.surface); |
| 610 | wl_callback_add_listener(callback, &frame_listener, output); |
| 611 | wl_surface_commit(output->parent.surface); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 612 | wl_display_flush(b->parent.wl_display); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 613 | |
| 614 | pixman_region32_fini(&sb->damage); |
| 615 | pixman_region32_init(&sb->damage); |
| 616 | sb->frame_damaged = 0; |
| 617 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 618 | pixman_region32_subtract(&b->compositor->primary_plane.damage, |
| 619 | &b->compositor->primary_plane.damage, damage); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | static void |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 624 | wayland_backend_destroy_output_surface(struct wayland_output *output) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 625 | { |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 626 | if (output->parent.shell_surface) |
| 627 | wl_shell_surface_destroy(output->parent.shell_surface); |
| 628 | |
| 629 | wl_surface_destroy(output->parent.surface); |
| 630 | } |
| 631 | |
| 632 | static int |
| 633 | wayland_output_disable(struct weston_output *base) |
| 634 | { |
| 635 | struct wayland_output *output = to_wayland_output(base); |
| 636 | struct wayland_backend *b = to_wayland_backend(base->compositor); |
| 637 | |
| 638 | if (!output->base.enabled) |
| 639 | return 0; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 640 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 641 | if (b->use_pixman) { |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 642 | pixman_renderer_output_destroy(&output->base); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 643 | } else { |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 644 | gl_renderer->output_destroy(&output->base); |
Emmanuel Gil Peyrot | 85571a3 | 2016-09-01 15:19:46 +0100 | [diff] [blame] | 645 | wl_egl_window_destroy(output->gl.egl_window); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 646 | } |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 647 | |
Armin Krezović | f054d35 | 2016-10-09 17:30:27 +0200 | [diff] [blame] | 648 | wayland_backend_destroy_output_surface(output); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 649 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 650 | if (output->frame) |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 651 | frame_destroy(output->frame); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 652 | |
| 653 | cairo_surface_destroy(output->gl.border.top); |
| 654 | cairo_surface_destroy(output->gl.border.left); |
| 655 | cairo_surface_destroy(output->gl.border.right); |
| 656 | cairo_surface_destroy(output->gl.border.bottom); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 657 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 658 | return 0; |
| 659 | } |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 660 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 661 | static void |
| 662 | wayland_output_destroy(struct weston_output *base) |
| 663 | { |
| 664 | struct wayland_output *output = to_wayland_output(base); |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 665 | |
| 666 | wayland_output_disable(&output->base); |
| 667 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 668 | weston_output_destroy(&output->base); |
| 669 | |
| 670 | free(output); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 673 | static const struct wl_shell_surface_listener shell_surface_listener; |
| 674 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 675 | static int |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 676 | wayland_output_init_gl_renderer(struct wayland_output *output) |
| 677 | { |
Jason Ekstrand | 00b8428 | 2013-10-27 22:24:59 -0500 | [diff] [blame] | 678 | int32_t fwidth = 0, fheight = 0; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 679 | |
| 680 | if (output->frame) { |
| 681 | fwidth = frame_width(output->frame); |
| 682 | fheight = frame_height(output->frame); |
| 683 | } else { |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 684 | fwidth = output->base.current_mode->width; |
| 685 | fheight = output->base.current_mode->height; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | output->gl.egl_window = |
| 689 | wl_egl_window_create(output->parent.surface, |
| 690 | fwidth, fheight); |
| 691 | if (!output->gl.egl_window) { |
| 692 | weston_log("failure to create wl_egl_window\n"); |
| 693 | return -1; |
| 694 | } |
| 695 | |
Miguel A. Vico | c095cde | 2016-05-18 17:43:00 +0200 | [diff] [blame] | 696 | if (gl_renderer->output_window_create(&output->base, |
| 697 | output->gl.egl_window, |
| 698 | output->gl.egl_window, |
| 699 | gl_renderer->alpha_attribs, |
| 700 | NULL, |
| 701 | 0) < 0) |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 702 | goto cleanup_window; |
| 703 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 704 | return 0; |
| 705 | |
| 706 | cleanup_window: |
| 707 | wl_egl_window_destroy(output->gl.egl_window); |
| 708 | return -1; |
| 709 | } |
| 710 | |
| 711 | static int |
| 712 | wayland_output_init_pixman_renderer(struct wayland_output *output) |
| 713 | { |
| 714 | return pixman_renderer_output_create(&output->base); |
| 715 | } |
| 716 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 717 | static void |
| 718 | wayland_output_resize_surface(struct wayland_output *output) |
| 719 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 720 | struct wayland_backend *b = |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 721 | to_wayland_backend(output->base.compositor); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 722 | struct wayland_shm_buffer *buffer, *next; |
| 723 | int32_t ix, iy, iwidth, iheight; |
| 724 | int32_t width, height; |
| 725 | struct wl_region *region; |
| 726 | |
| 727 | width = output->base.current_mode->width; |
| 728 | height = output->base.current_mode->height; |
| 729 | |
| 730 | if (output->frame) { |
| 731 | frame_resize_inside(output->frame, width, height); |
| 732 | |
| 733 | frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 734 | region = wl_compositor_create_region(b->parent.compositor); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 735 | wl_region_add(region, ix, iy, iwidth, iheight); |
| 736 | wl_surface_set_input_region(output->parent.surface, region); |
| 737 | wl_region_destroy(region); |
| 738 | |
| 739 | frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 740 | region = wl_compositor_create_region(b->parent.compositor); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 741 | wl_region_add(region, ix, iy, iwidth, iheight); |
| 742 | wl_surface_set_opaque_region(output->parent.surface, region); |
| 743 | wl_region_destroy(region); |
| 744 | |
| 745 | width = frame_width(output->frame); |
| 746 | height = frame_height(output->frame); |
| 747 | } else { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 748 | region = wl_compositor_create_region(b->parent.compositor); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 749 | wl_region_add(region, 0, 0, width, height); |
| 750 | wl_surface_set_input_region(output->parent.surface, region); |
| 751 | wl_region_destroy(region); |
| 752 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 753 | region = wl_compositor_create_region(b->parent.compositor); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 754 | wl_region_add(region, 0, 0, width, height); |
| 755 | wl_surface_set_opaque_region(output->parent.surface, region); |
| 756 | wl_region_destroy(region); |
| 757 | } |
| 758 | |
| 759 | if (output->gl.egl_window) { |
| 760 | wl_egl_window_resize(output->gl.egl_window, |
| 761 | width, height, 0, 0); |
| 762 | |
| 763 | /* These will need to be re-created due to the resize */ |
| 764 | gl_renderer->output_set_border(&output->base, |
| 765 | GL_RENDERER_BORDER_TOP, |
| 766 | 0, 0, 0, NULL); |
| 767 | cairo_surface_destroy(output->gl.border.top); |
| 768 | output->gl.border.top = NULL; |
| 769 | gl_renderer->output_set_border(&output->base, |
| 770 | GL_RENDERER_BORDER_LEFT, |
| 771 | 0, 0, 0, NULL); |
| 772 | cairo_surface_destroy(output->gl.border.left); |
| 773 | output->gl.border.left = NULL; |
| 774 | gl_renderer->output_set_border(&output->base, |
| 775 | GL_RENDERER_BORDER_RIGHT, |
| 776 | 0, 0, 0, NULL); |
| 777 | cairo_surface_destroy(output->gl.border.right); |
| 778 | output->gl.border.right = NULL; |
| 779 | gl_renderer->output_set_border(&output->base, |
| 780 | GL_RENDERER_BORDER_BOTTOM, |
| 781 | 0, 0, 0, NULL); |
| 782 | cairo_surface_destroy(output->gl.border.bottom); |
| 783 | output->gl.border.bottom = NULL; |
| 784 | } |
| 785 | |
| 786 | /* Throw away any remaining SHM buffers */ |
Derek Foreman | f53beb8 | 2015-05-29 16:32:52 -0500 | [diff] [blame] | 787 | wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, free_link) |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 788 | wayland_shm_buffer_destroy(buffer); |
| 789 | /* These will get thrown away when they get released */ |
| 790 | wl_list_for_each(buffer, &output->shm.buffers, link) |
| 791 | buffer->output = NULL; |
| 792 | } |
| 793 | |
| 794 | static int |
| 795 | wayland_output_set_windowed(struct wayland_output *output) |
| 796 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 797 | struct wayland_backend *b = |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 798 | to_wayland_backend(output->base.compositor); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 799 | int tlen; |
| 800 | char *title; |
| 801 | |
| 802 | if (output->frame) |
| 803 | return 0; |
| 804 | |
| 805 | if (output->name) { |
| 806 | tlen = strlen(output->name) + strlen(WINDOW_TITLE " - "); |
| 807 | title = malloc(tlen + 1); |
| 808 | if (!title) |
| 809 | return -1; |
| 810 | |
| 811 | snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name); |
| 812 | } else { |
| 813 | title = strdup(WINDOW_TITLE); |
| 814 | } |
| 815 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 816 | if (!b->theme) { |
| 817 | b->theme = theme_create(); |
| 818 | if (!b->theme) { |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 819 | free(title); |
| 820 | return -1; |
| 821 | } |
| 822 | } |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 823 | output->frame = frame_create(b->theme, 100, 100, |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 824 | FRAME_BUTTON_CLOSE, title); |
| 825 | free(title); |
| 826 | if (!output->frame) |
| 827 | return -1; |
| 828 | |
| 829 | if (output->keyboard_count) |
| 830 | frame_set_flag(output->frame, FRAME_FLAG_ACTIVE); |
| 831 | |
| 832 | wayland_output_resize_surface(output); |
| 833 | |
| 834 | wl_shell_surface_set_toplevel(output->parent.shell_surface); |
| 835 | |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | static void |
| 840 | wayland_output_set_fullscreen(struct wayland_output *output, |
| 841 | enum wl_shell_surface_fullscreen_method method, |
| 842 | uint32_t framerate, struct wl_output *target) |
| 843 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 844 | struct wayland_backend *b = |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 845 | to_wayland_backend(output->base.compositor); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 846 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 847 | if (output->frame) { |
| 848 | frame_destroy(output->frame); |
| 849 | output->frame = NULL; |
| 850 | } |
| 851 | |
| 852 | wayland_output_resize_surface(output); |
| 853 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 854 | if (output->parent.shell_surface) { |
| 855 | wl_shell_surface_set_fullscreen(output->parent.shell_surface, |
| 856 | method, framerate, target); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 857 | } else if (b->parent.fshell) { |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 858 | zwp_fullscreen_shell_v1_present_surface(b->parent.fshell, |
| 859 | output->parent.surface, |
| 860 | method, target); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | |
| 864 | static struct weston_mode * |
| 865 | wayland_output_choose_mode(struct wayland_output *output, |
| 866 | struct weston_mode *ref_mode) |
| 867 | { |
| 868 | struct weston_mode *mode; |
| 869 | |
| 870 | /* First look for an exact match */ |
| 871 | wl_list_for_each(mode, &output->base.mode_list, link) |
| 872 | if (mode->width == ref_mode->width && |
| 873 | mode->height == ref_mode->height && |
| 874 | mode->refresh == ref_mode->refresh) |
| 875 | return mode; |
| 876 | |
| 877 | /* If we can't find an exact match, ignore refresh and try again */ |
| 878 | wl_list_for_each(mode, &output->base.mode_list, link) |
| 879 | if (mode->width == ref_mode->width && |
| 880 | mode->height == ref_mode->height) |
| 881 | return mode; |
| 882 | |
| 883 | /* Yeah, we failed */ |
| 884 | return NULL; |
| 885 | } |
| 886 | |
| 887 | enum mode_status { |
| 888 | MODE_STATUS_UNKNOWN, |
| 889 | MODE_STATUS_SUCCESS, |
| 890 | MODE_STATUS_FAIL, |
| 891 | MODE_STATUS_CANCEL, |
| 892 | }; |
| 893 | |
| 894 | static void |
| 895 | mode_feedback_successful(void *data, |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 896 | struct zwp_fullscreen_shell_mode_feedback_v1 *fb) |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 897 | { |
| 898 | enum mode_status *value = data; |
| 899 | |
| 900 | printf("Mode switch successful\n"); |
| 901 | |
| 902 | *value = MODE_STATUS_SUCCESS; |
| 903 | } |
| 904 | |
| 905 | static void |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 906 | mode_feedback_failed(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb) |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 907 | { |
| 908 | enum mode_status *value = data; |
| 909 | |
| 910 | printf("Mode switch failed\n"); |
| 911 | |
| 912 | *value = MODE_STATUS_FAIL; |
| 913 | } |
| 914 | |
| 915 | static void |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 916 | mode_feedback_cancelled(void *data, struct zwp_fullscreen_shell_mode_feedback_v1 *fb) |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 917 | { |
| 918 | enum mode_status *value = data; |
| 919 | |
| 920 | printf("Mode switch cancelled\n"); |
| 921 | |
| 922 | *value = MODE_STATUS_CANCEL; |
| 923 | } |
| 924 | |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 925 | struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = { |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 926 | mode_feedback_successful, |
| 927 | mode_feedback_failed, |
| 928 | mode_feedback_cancelled, |
| 929 | }; |
| 930 | |
| 931 | static int |
| 932 | wayland_output_switch_mode(struct weston_output *output_base, |
| 933 | struct weston_mode *mode) |
| 934 | { |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 935 | struct wayland_output *output = to_wayland_output(output_base); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 936 | struct wayland_backend *b; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 937 | struct wl_surface *old_surface; |
| 938 | struct weston_mode *old_mode; |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 939 | struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 940 | enum mode_status mode_status; |
| 941 | int ret = 0; |
| 942 | |
| 943 | if (output_base == NULL) { |
| 944 | weston_log("output is NULL.\n"); |
| 945 | return -1; |
| 946 | } |
| 947 | |
| 948 | if (mode == NULL) { |
| 949 | weston_log("mode is NULL.\n"); |
| 950 | return -1; |
| 951 | } |
| 952 | |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 953 | b = to_wayland_backend(output_base->compositor); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 954 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 955 | if (output->parent.shell_surface || !b->parent.fshell) |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 956 | return -1; |
| 957 | |
| 958 | mode = wayland_output_choose_mode(output, mode); |
| 959 | if (mode == NULL) |
| 960 | return -1; |
| 961 | |
| 962 | if (output->base.current_mode == mode) |
| 963 | return 0; |
| 964 | |
| 965 | old_mode = output->base.current_mode; |
| 966 | old_surface = output->parent.surface; |
| 967 | output->base.current_mode = mode; |
| 968 | output->parent.surface = |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 969 | wl_compositor_create_surface(b->parent.compositor); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 970 | wl_surface_set_user_data(output->parent.surface, output); |
| 971 | |
| 972 | /* Blow the old buffers because we changed size/surfaces */ |
| 973 | wayland_output_resize_surface(output); |
| 974 | |
| 975 | mode_feedback = |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 976 | zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell, |
| 977 | output->parent.surface, |
| 978 | output->parent.output, |
| 979 | mode->refresh); |
| 980 | zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback, |
| 981 | &mode_feedback_listener, |
| 982 | &mode_status); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 983 | |
| 984 | /* This should kick-start things again */ |
Armin Krezović | 2d321e3 | 2016-10-09 17:30:25 +0200 | [diff] [blame] | 985 | output->parent.draw_initial_frame = true; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 986 | wayland_output_start_repaint_loop(&output->base); |
| 987 | |
| 988 | mode_status = MODE_STATUS_UNKNOWN; |
| 989 | while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 990 | ret = wl_display_dispatch(b->parent.wl_display); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 991 | |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 992 | zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 993 | |
| 994 | if (mode_status == MODE_STATUS_FAIL) { |
| 995 | output->base.current_mode = old_mode; |
| 996 | wl_surface_destroy(output->parent.surface); |
| 997 | output->parent.surface = old_surface; |
| 998 | wayland_output_resize_surface(output); |
| 999 | |
| 1000 | return -1; |
| 1001 | } |
| 1002 | |
| 1003 | old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT; |
| 1004 | output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT; |
| 1005 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1006 | if (b->use_pixman) { |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1007 | pixman_renderer_output_destroy(output_base); |
| 1008 | if (wayland_output_init_pixman_renderer(output) < 0) |
| 1009 | goto err_output; |
| 1010 | } else { |
| 1011 | gl_renderer->output_destroy(output_base); |
| 1012 | wl_egl_window_destroy(output->gl.egl_window); |
| 1013 | if (wayland_output_init_gl_renderer(output) < 0) |
| 1014 | goto err_output; |
| 1015 | } |
| 1016 | wl_surface_destroy(old_surface); |
| 1017 | |
| 1018 | weston_output_schedule_repaint(&output->base); |
| 1019 | |
| 1020 | return 0; |
| 1021 | |
| 1022 | err_output: |
| 1023 | /* XXX */ |
| 1024 | return -1; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1025 | } |
| 1026 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1027 | static int |
| 1028 | wayland_backend_create_output_surface(struct wayland_output *output) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1029 | { |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1030 | struct wayland_backend *b = to_wayland_backend(output->base.compositor); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1031 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1032 | output->parent.surface = |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1033 | wl_compositor_create_surface(b->parent.compositor); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1034 | if (!output->parent.surface) |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1035 | return -1; |
| 1036 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1037 | wl_surface_set_user_data(output->parent.surface, output); |
| 1038 | |
Armin Krezović | 2d321e3 | 2016-10-09 17:30:25 +0200 | [diff] [blame] | 1039 | output->parent.draw_initial_frame = true; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1040 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1041 | if (b->parent.shell) { |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1042 | output->parent.shell_surface = |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1043 | wl_shell_get_shell_surface(b->parent.shell, |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1044 | output->parent.surface); |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1045 | if (!output->parent.shell_surface) { |
| 1046 | wl_surface_destroy(output->parent.surface); |
| 1047 | return -1; |
| 1048 | } |
| 1049 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1050 | wl_shell_surface_add_listener(output->parent.shell_surface, |
| 1051 | &shell_surface_listener, output); |
| 1052 | } |
| 1053 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1054 | return 0; |
| 1055 | } |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1056 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1057 | static int |
| 1058 | wayland_output_enable(struct weston_output *base) |
| 1059 | { |
| 1060 | struct wayland_output *output = to_wayland_output(base); |
| 1061 | struct wayland_backend *b = to_wayland_backend(base->compositor); |
| 1062 | int ret = 0; |
| 1063 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1064 | weston_log("Creating %dx%d wayland output at (%d, %d)\n", |
| 1065 | output->base.current_mode->width, |
| 1066 | output->base.current_mode->height, |
| 1067 | output->base.x, output->base.y); |
| 1068 | |
Armin Krezović | f054d35 | 2016-10-09 17:30:27 +0200 | [diff] [blame] | 1069 | if (!output->parent.surface) |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1070 | ret = wayland_backend_create_output_surface(output); |
| 1071 | |
| 1072 | if (ret < 0) |
| 1073 | return -1; |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 1074 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 1075 | wl_list_init(&output->shm.buffers); |
| 1076 | wl_list_init(&output->shm.free_buffers); |
| 1077 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1078 | if (b->use_pixman) { |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 1079 | if (wayland_output_init_pixman_renderer(output) < 0) |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1080 | goto err_output; |
Armin Krezović | f16de17 | 2016-10-09 17:30:26 +0200 | [diff] [blame] | 1081 | |
| 1082 | output->base.repaint = wayland_output_repaint_pixman; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 1083 | } else { |
| 1084 | if (wayland_output_init_gl_renderer(output) < 0) |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1085 | goto err_output; |
Armin Krezović | f16de17 | 2016-10-09 17:30:26 +0200 | [diff] [blame] | 1086 | |
| 1087 | output->base.repaint = wayland_output_repaint_gl; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1088 | } |
| 1089 | |
Armin Krezović | f16de17 | 2016-10-09 17:30:26 +0200 | [diff] [blame] | 1090 | output->base.start_repaint_loop = wayland_output_start_repaint_loop; |
| 1091 | output->base.assign_planes = NULL; |
| 1092 | output->base.set_backlight = NULL; |
| 1093 | output->base.set_dpms = NULL; |
| 1094 | output->base.switch_mode = wayland_output_switch_mode; |
| 1095 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1096 | if (b->sprawl_across_outputs) { |
| 1097 | wayland_output_set_fullscreen(output, |
| 1098 | WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER, |
| 1099 | output->mode.refresh, output->parent.output); |
| 1100 | |
| 1101 | if (output->parent.shell_surface) { |
| 1102 | wl_shell_surface_set_fullscreen(output->parent.shell_surface, |
| 1103 | WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER, |
| 1104 | output->mode.refresh, output->parent.output); |
| 1105 | } else if (b->parent.fshell) { |
| 1106 | zwp_fullscreen_shell_v1_present_surface(b->parent.fshell, |
| 1107 | output->parent.surface, |
| 1108 | ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER, |
| 1109 | output->parent.output); |
| 1110 | zwp_fullscreen_shell_mode_feedback_v1_destroy( |
| 1111 | zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell, |
| 1112 | output->parent.surface, |
| 1113 | output->parent.output, |
| 1114 | output->mode.refresh)); |
| 1115 | } |
| 1116 | } else if (b->fullscreen) { |
| 1117 | wayland_output_set_fullscreen(output, 0, 0, NULL); |
| 1118 | } else { |
| 1119 | wayland_output_set_windowed(output); |
| 1120 | } |
| 1121 | |
| 1122 | return 0; |
| 1123 | |
| 1124 | err_output: |
Armin Krezović | f054d35 | 2016-10-09 17:30:27 +0200 | [diff] [blame] | 1125 | wayland_backend_destroy_output_surface(output); |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1126 | |
| 1127 | return -1; |
| 1128 | } |
| 1129 | |
| 1130 | static struct wayland_output * |
| 1131 | wayland_output_create_common(void) |
| 1132 | { |
| 1133 | struct wayland_output *output; |
| 1134 | |
| 1135 | output = zalloc(sizeof *output); |
| 1136 | if (output == NULL) { |
| 1137 | perror("zalloc"); |
| 1138 | return NULL; |
| 1139 | } |
| 1140 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1141 | output->base.destroy = wayland_output_destroy; |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1142 | output->base.disable = wayland_output_disable; |
| 1143 | output->base.enable = wayland_output_enable; |
| 1144 | |
| 1145 | return output; |
| 1146 | } |
| 1147 | |
| 1148 | static int |
| 1149 | wayland_output_create(struct weston_compositor *compositor, const char *name) |
| 1150 | { |
| 1151 | struct wayland_output *output = wayland_output_create_common(); |
| 1152 | |
| 1153 | /* name can't be NULL. */ |
| 1154 | assert(name); |
| 1155 | |
| 1156 | output->base.name = strdup(name); |
| 1157 | |
Armin Krezović | 4008740 | 2016-09-30 14:11:12 +0200 | [diff] [blame] | 1158 | weston_output_init(&output->base, compositor); |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1159 | weston_compositor_add_pending_output(&output->base, compositor); |
| 1160 | |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
| 1164 | static int |
| 1165 | wayland_output_set_size(struct weston_output *base, int width, int height) |
| 1166 | { |
| 1167 | struct wayland_output *output = to_wayland_output(base); |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1168 | int output_width, output_height; |
| 1169 | |
| 1170 | /* We can only be called once. */ |
| 1171 | assert(!output->base.current_mode); |
| 1172 | |
| 1173 | /* Make sure we have scale set. */ |
| 1174 | assert(output->base.scale); |
| 1175 | |
| 1176 | if (width < 1) { |
| 1177 | weston_log("Invalid width \"%d\" for output %s\n", |
| 1178 | width, output->base.name); |
| 1179 | return -1; |
| 1180 | } |
| 1181 | |
| 1182 | if (height < 1) { |
| 1183 | weston_log("Invalid height \"%d\" for output %s\n", |
| 1184 | height, output->base.name); |
| 1185 | return -1; |
| 1186 | } |
| 1187 | |
| 1188 | output_width = width * output->base.scale; |
| 1189 | output_height = height * output->base.scale; |
| 1190 | |
| 1191 | output->mode.flags = |
| 1192 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 1193 | |
| 1194 | output->mode.width = output_width; |
| 1195 | output->mode.height = output_height; |
| 1196 | output->mode.refresh = 60000; |
| 1197 | output->scale = output->base.scale; |
| 1198 | wl_list_init(&output->base.mode_list); |
| 1199 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
| 1200 | |
| 1201 | output->base.current_mode = &output->mode; |
| 1202 | output->base.make = "wayland"; |
| 1203 | output->base.model = "none"; |
| 1204 | |
| 1205 | /* XXX: Calculate proper size. */ |
| 1206 | output->base.mm_width = width; |
| 1207 | output->base.mm_height = height; |
| 1208 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1209 | return 0; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1210 | } |
| 1211 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1212 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1213 | wayland_output_create_for_parent_output(struct wayland_backend *b, |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1214 | struct wayland_parent_output *poutput) |
| 1215 | { |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1216 | struct wayland_output *output = wayland_output_create_common(); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1217 | struct weston_mode *mode; |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1218 | |
| 1219 | output->base.name = strdup("wlparent"); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1220 | |
| 1221 | if (poutput->current_mode) { |
| 1222 | mode = poutput->current_mode; |
| 1223 | } else if (poutput->preferred_mode) { |
U. Artie Eoff | 67072d0 | 2014-05-06 14:50:02 -0700 | [diff] [blame] | 1224 | mode = poutput->preferred_mode; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1225 | } else if (!wl_list_empty(&poutput->mode_list)) { |
| 1226 | mode = container_of(poutput->mode_list.next, |
| 1227 | struct weston_mode, link); |
| 1228 | } else { |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1229 | weston_log("No valid modes found. Skipping output.\n"); |
| 1230 | goto out; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1231 | } |
| 1232 | |
Armin Krezović | 4008740 | 2016-09-30 14:11:12 +0200 | [diff] [blame] | 1233 | weston_output_init(&output->base, b->compositor); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1234 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1235 | output->base.scale = 1; |
| 1236 | output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1237 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1238 | if (wayland_output_set_size(&output->base, mode->width, mode->height) < 0) |
| 1239 | goto out; |
| 1240 | |
| 1241 | output->mode = *mode; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1242 | output->parent.output = poutput->global; |
| 1243 | |
| 1244 | output->base.make = poutput->physical.make; |
| 1245 | output->base.model = poutput->physical.model; |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1246 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1247 | wl_list_init(&output->base.mode_list); |
| 1248 | wl_list_insert_list(&output->base.mode_list, &poutput->mode_list); |
| 1249 | wl_list_init(&poutput->mode_list); |
| 1250 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1251 | weston_compositor_add_pending_output(&output->base, b->compositor); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1252 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1253 | return 0; |
| 1254 | |
| 1255 | out: |
| 1256 | free(output->name); |
| 1257 | free(output); |
| 1258 | |
| 1259 | return -1; |
| 1260 | } |
| 1261 | |
| 1262 | static int |
| 1263 | wayland_output_create_fullscreen(struct wayland_backend *b) |
| 1264 | { |
| 1265 | struct wayland_output *output = wayland_output_create_common(); |
| 1266 | int width = 0, height = 0; |
| 1267 | |
| 1268 | output->base.name = strdup("wayland-fullscreen"); |
| 1269 | |
Armin Krezović | 4008740 | 2016-09-30 14:11:12 +0200 | [diff] [blame] | 1270 | weston_output_init(&output->base, b->compositor); |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1271 | |
| 1272 | output->base.scale = 1; |
| 1273 | output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL; |
| 1274 | |
| 1275 | if (wayland_backend_create_output_surface(output) < 0) |
| 1276 | goto err_surface; |
| 1277 | |
| 1278 | /* What should size be set if conditional is false? */ |
| 1279 | if (b->parent.shell) { |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1280 | wl_shell_surface_set_fullscreen(output->parent.shell_surface, |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1281 | 0, 0, NULL); |
| 1282 | wl_display_roundtrip(b->parent.wl_display); |
| 1283 | |
| 1284 | width = output->parent.configure_width; |
| 1285 | height = output->parent.configure_height; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1286 | } |
| 1287 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1288 | if (wayland_output_set_size(&output->base, width, height) < 0) |
| 1289 | goto err_set_size; |
| 1290 | |
| 1291 | weston_compositor_add_pending_output(&output->base, b->compositor); |
| 1292 | |
| 1293 | return 0; |
| 1294 | |
| 1295 | err_set_size: |
| 1296 | wayland_backend_destroy_output_surface(output); |
| 1297 | err_surface: |
| 1298 | free(output->name); |
| 1299 | free(output); |
| 1300 | |
| 1301 | return -1; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1302 | } |
| 1303 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 1304 | static void |
| 1305 | shell_surface_ping(void *data, struct wl_shell_surface *shell_surface, |
| 1306 | uint32_t serial) |
| 1307 | { |
| 1308 | wl_shell_surface_pong(shell_surface, serial); |
| 1309 | } |
| 1310 | |
| 1311 | static void |
| 1312 | shell_surface_configure(void *data, struct wl_shell_surface *shell_surface, |
| 1313 | uint32_t edges, int32_t width, int32_t height) |
| 1314 | { |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1315 | struct wayland_output *output = data; |
| 1316 | |
| 1317 | output->parent.configure_width = width; |
| 1318 | output->parent.configure_height = height; |
| 1319 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 1320 | /* FIXME: implement resizing */ |
| 1321 | } |
| 1322 | |
| 1323 | static void |
| 1324 | shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface) |
| 1325 | { |
| 1326 | } |
| 1327 | |
| 1328 | static const struct wl_shell_surface_listener shell_surface_listener = { |
| 1329 | shell_surface_ping, |
| 1330 | shell_surface_configure, |
| 1331 | shell_surface_popup_done |
| 1332 | }; |
| 1333 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1334 | /* Events received from the wayland-server this compositor is client of: */ |
| 1335 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1336 | /* parent input interface */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1337 | static void |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1338 | input_set_cursor(struct wayland_input *input) |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1339 | { |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1340 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1341 | struct wl_buffer *buffer; |
| 1342 | struct wl_cursor_image *image; |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1343 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1344 | if (!input->backend->cursor) |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1345 | return; /* Couldn't load the cursor. Can't set it */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1346 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1347 | image = input->backend->cursor->images[0]; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1348 | buffer = wl_cursor_image_get_buffer(image); |
Hardening | 842a36a | 2014-03-18 14:12:50 +0100 | [diff] [blame] | 1349 | if (!buffer) |
| 1350 | return; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1351 | |
| 1352 | wl_pointer_set_cursor(input->parent.pointer, input->enter_serial, |
| 1353 | input->parent.cursor.surface, |
| 1354 | image->hotspot_x, image->hotspot_y); |
| 1355 | |
| 1356 | wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0); |
| 1357 | wl_surface_damage(input->parent.cursor.surface, 0, 0, |
| 1358 | image->width, image->height); |
| 1359 | wl_surface_commit(input->parent.cursor.surface); |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1360 | } |
| 1361 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1362 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1363 | input_handle_pointer_enter(void *data, struct wl_pointer *pointer, |
| 1364 | uint32_t serial, struct wl_surface *surface, |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1365 | wl_fixed_t fixed_x, wl_fixed_t fixed_y) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1366 | { |
| 1367 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1368 | int32_t fx, fy; |
| 1369 | enum theme_location location; |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1370 | double x, y; |
| 1371 | |
| 1372 | x = wl_fixed_to_double(fixed_x); |
| 1373 | y = wl_fixed_to_double(fixed_y); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1374 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1375 | /* XXX: If we get a modifier event immediately before the focus, |
| 1376 | * we should try to keep the same serial. */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1377 | input->enter_serial = serial; |
| 1378 | input->output = wl_surface_get_user_data(surface); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1379 | |
| 1380 | if (input->output->frame) { |
| 1381 | location = frame_pointer_enter(input->output->frame, input, |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1382 | x, y); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1383 | frame_interior(input->output->frame, &fx, &fy, NULL, NULL); |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1384 | x -= fx; |
| 1385 | y -= fy; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1386 | |
| 1387 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 1388 | weston_output_schedule_repaint(&input->output->base); |
| 1389 | } else { |
| 1390 | location = THEME_LOCATION_CLIENT_AREA; |
| 1391 | } |
| 1392 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1393 | weston_output_transform_coordinate(&input->output->base, x, y, &x, &y); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1394 | |
| 1395 | if (location == THEME_LOCATION_CLIENT_AREA) { |
Derek Foreman | 4bcc54d | 2015-11-06 15:56:06 -0600 | [diff] [blame] | 1396 | input->has_focus = true; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1397 | notify_pointer_focus(&input->base, &input->output->base, x, y); |
| 1398 | wl_pointer_set_cursor(input->parent.pointer, |
| 1399 | input->enter_serial, NULL, 0, 0); |
| 1400 | } else { |
Derek Foreman | 4bcc54d | 2015-11-06 15:56:06 -0600 | [diff] [blame] | 1401 | input->has_focus = false; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1402 | notify_pointer_focus(&input->base, NULL, 0, 0); |
| 1403 | input_set_cursor(input); |
| 1404 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1408 | input_handle_pointer_leave(void *data, struct wl_pointer *pointer, |
| 1409 | uint32_t serial, struct wl_surface *surface) |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1410 | { |
| 1411 | struct wayland_input *input = data; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1412 | |
Dima Ryazanov | 01d5c02 | 2015-05-18 23:14:16 -0700 | [diff] [blame] | 1413 | if (!input->output) |
| 1414 | return; |
| 1415 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1416 | if (input->output->frame) { |
| 1417 | frame_pointer_leave(input->output->frame, input); |
| 1418 | |
| 1419 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 1420 | weston_output_schedule_repaint(&input->output->base); |
| 1421 | } |
| 1422 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1423 | notify_pointer_focus(&input->base, NULL, 0, 0); |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1424 | input->output = NULL; |
Derek Foreman | 4bcc54d | 2015-11-06 15:56:06 -0600 | [diff] [blame] | 1425 | input->has_focus = false; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1429 | input_handle_motion(void *data, struct wl_pointer *pointer, |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1430 | uint32_t time, wl_fixed_t fixed_x, wl_fixed_t fixed_y) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1431 | { |
| 1432 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1433 | int32_t fx, fy; |
| 1434 | enum theme_location location; |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1435 | bool want_frame = false; |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1436 | double x, y; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1437 | |
Dima Ryazanov | 01d5c02 | 2015-05-18 23:14:16 -0700 | [diff] [blame] | 1438 | if (!input->output) |
| 1439 | return; |
| 1440 | |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1441 | x = wl_fixed_to_double(fixed_x); |
| 1442 | y = wl_fixed_to_double(fixed_y); |
| 1443 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1444 | if (input->output->frame) { |
| 1445 | location = frame_pointer_motion(input->output->frame, input, |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1446 | x, y); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1447 | frame_interior(input->output->frame, &fx, &fy, NULL, NULL); |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1448 | x -= fx; |
| 1449 | y -= fy; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1450 | |
| 1451 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 1452 | weston_output_schedule_repaint(&input->output->base); |
| 1453 | } else { |
| 1454 | location = THEME_LOCATION_CLIENT_AREA; |
| 1455 | } |
| 1456 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1457 | weston_output_transform_coordinate(&input->output->base, x, y, &x, &y); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1458 | |
Derek Foreman | 4bcc54d | 2015-11-06 15:56:06 -0600 | [diff] [blame] | 1459 | if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) { |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1460 | input_set_cursor(input); |
| 1461 | notify_pointer_focus(&input->base, NULL, 0, 0); |
Derek Foreman | 4bcc54d | 2015-11-06 15:56:06 -0600 | [diff] [blame] | 1462 | input->has_focus = false; |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1463 | want_frame = true; |
Derek Foreman | 4bcc54d | 2015-11-06 15:56:06 -0600 | [diff] [blame] | 1464 | } else if (!input->has_focus && |
| 1465 | location == THEME_LOCATION_CLIENT_AREA) { |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1466 | wl_pointer_set_cursor(input->parent.pointer, |
| 1467 | input->enter_serial, NULL, 0, 0); |
| 1468 | notify_pointer_focus(&input->base, &input->output->base, x, y); |
Derek Foreman | 4bcc54d | 2015-11-06 15:56:06 -0600 | [diff] [blame] | 1469 | input->has_focus = true; |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1470 | want_frame = true; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1471 | } |
| 1472 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1473 | if (location == THEME_LOCATION_CLIENT_AREA) { |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1474 | notify_motion_absolute(&input->base, time, x, y); |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1475 | want_frame = true; |
| 1476 | } |
| 1477 | |
| 1478 | if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION) |
| 1479 | notify_pointer_frame(&input->base); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | static void |
| 1483 | input_handle_button(void *data, struct wl_pointer *pointer, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 1484 | uint32_t serial, uint32_t time, uint32_t button, |
Quentin Glidic | d8b17bc | 2016-07-10 11:00:55 +0200 | [diff] [blame] | 1485 | enum wl_pointer_button_state state) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1486 | { |
| 1487 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1488 | enum theme_location location; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1489 | |
Dima Ryazanov | 01d5c02 | 2015-05-18 23:14:16 -0700 | [diff] [blame] | 1490 | if (!input->output) |
| 1491 | return; |
| 1492 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1493 | if (input->output->frame) { |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1494 | location = frame_pointer_button(input->output->frame, input, |
Quentin Glidic | d8b17bc | 2016-07-10 11:00:55 +0200 | [diff] [blame] | 1495 | button, state); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1496 | |
| 1497 | if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) { |
| 1498 | |
| 1499 | wl_shell_surface_move(input->output->parent.shell_surface, |
| 1500 | input->parent.seat, serial); |
| 1501 | frame_status_clear(input->output->frame, |
| 1502 | FRAME_STATUS_MOVE); |
| 1503 | return; |
| 1504 | } |
| 1505 | |
Dima Ryazanov | 01d5c02 | 2015-05-18 23:14:16 -0700 | [diff] [blame] | 1506 | if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) { |
| 1507 | wayland_output_destroy(&input->output->base); |
Dima Ryazanov | b7e70af | 2015-05-20 01:03:53 -0700 | [diff] [blame] | 1508 | input->output = NULL; |
| 1509 | input->keyboard_focus = NULL; |
Dima Ryazanov | 01d5c02 | 2015-05-18 23:14:16 -0700 | [diff] [blame] | 1510 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1511 | if (wl_list_empty(&input->backend->compositor->output_list)) |
Giulio Camuffo | 459137b | 2014-10-11 23:56:24 +0300 | [diff] [blame] | 1512 | weston_compositor_exit(input->backend->compositor); |
Dima Ryazanov | 01d5c02 | 2015-05-18 23:14:16 -0700 | [diff] [blame] | 1513 | |
| 1514 | return; |
| 1515 | } |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1516 | |
| 1517 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 1518 | weston_output_schedule_repaint(&input->output->base); |
| 1519 | } else { |
| 1520 | location = THEME_LOCATION_CLIENT_AREA; |
| 1521 | } |
| 1522 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1523 | if (location == THEME_LOCATION_CLIENT_AREA) { |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1524 | notify_button(&input->base, time, button, state); |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1525 | if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION) |
| 1526 | notify_pointer_frame(&input->base); |
| 1527 | } |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | static void |
| 1531 | input_handle_axis(void *data, struct wl_pointer *pointer, |
Daniel Stone | 2fce402 | 2012-05-30 16:32:00 +0100 | [diff] [blame] | 1532 | uint32_t time, uint32_t axis, wl_fixed_t value) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1533 | { |
| 1534 | struct wayland_input *input = data; |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 1535 | struct weston_pointer_axis_event weston_event; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1536 | |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 1537 | weston_event.axis = axis; |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1538 | weston_event.value = wl_fixed_to_double(value); |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 1539 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1540 | if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL && |
| 1541 | input->vert.has_discrete) { |
| 1542 | weston_event.has_discrete = true; |
| 1543 | weston_event.discrete = input->vert.discrete; |
| 1544 | input->vert.has_discrete = false; |
| 1545 | } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL && |
| 1546 | input->horiz.has_discrete) { |
| 1547 | weston_event.has_discrete = true; |
| 1548 | weston_event.discrete = input->horiz.discrete; |
| 1549 | input->horiz.has_discrete = false; |
| 1550 | } |
| 1551 | |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 1552 | notify_axis(&input->base, time, &weston_event); |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1553 | |
| 1554 | if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION) |
| 1555 | notify_pointer_frame(&input->base); |
| 1556 | } |
| 1557 | |
| 1558 | static void |
| 1559 | input_handle_frame(void *data, struct wl_pointer *pointer) |
| 1560 | { |
| 1561 | struct wayland_input *input = data; |
| 1562 | |
| 1563 | notify_pointer_frame(&input->base); |
| 1564 | } |
| 1565 | |
| 1566 | static void |
| 1567 | input_handle_axis_source(void *data, struct wl_pointer *pointer, |
| 1568 | uint32_t source) |
| 1569 | { |
| 1570 | struct wayland_input *input = data; |
| 1571 | |
| 1572 | notify_axis_source(&input->base, source); |
| 1573 | } |
| 1574 | |
| 1575 | static void |
| 1576 | input_handle_axis_stop(void *data, struct wl_pointer *pointer, |
| 1577 | uint32_t time, uint32_t axis) |
| 1578 | { |
| 1579 | struct wayland_input *input = data; |
| 1580 | struct weston_pointer_axis_event weston_event; |
| 1581 | |
| 1582 | weston_event.axis = axis; |
| 1583 | weston_event.value = 0; |
| 1584 | |
| 1585 | notify_axis(&input->base, time, &weston_event); |
| 1586 | } |
| 1587 | |
| 1588 | static void |
| 1589 | input_handle_axis_discrete(void *data, struct wl_pointer *pointer, |
| 1590 | uint32_t axis, int32_t discrete) |
| 1591 | { |
| 1592 | struct wayland_input *input = data; |
| 1593 | |
| 1594 | if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) { |
| 1595 | input->vert.has_discrete = true; |
| 1596 | input->vert.discrete = discrete; |
| 1597 | } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) { |
| 1598 | input->horiz.has_discrete = true; |
| 1599 | input->horiz.discrete = discrete; |
| 1600 | } |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | static const struct wl_pointer_listener pointer_listener = { |
| 1604 | input_handle_pointer_enter, |
| 1605 | input_handle_pointer_leave, |
| 1606 | input_handle_motion, |
| 1607 | input_handle_button, |
| 1608 | input_handle_axis, |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1609 | input_handle_frame, |
| 1610 | input_handle_axis_source, |
| 1611 | input_handle_axis_stop, |
| 1612 | input_handle_axis_discrete, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1613 | }; |
| 1614 | |
| 1615 | static void |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1616 | input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, |
| 1617 | int fd, uint32_t size) |
| 1618 | { |
| 1619 | struct wayland_input *input = data; |
| 1620 | struct xkb_keymap *keymap; |
| 1621 | char *map_str; |
| 1622 | |
U. Artie Eoff | d8d4701 | 2014-05-06 14:50:03 -0700 | [diff] [blame] | 1623 | if (!data) { |
| 1624 | close(fd); |
| 1625 | return; |
| 1626 | } |
Jason Ekstrand | b7d9f2e | 2014-04-02 19:53:57 -0500 | [diff] [blame] | 1627 | |
| 1628 | if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { |
| 1629 | map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); |
| 1630 | if (map_str == MAP_FAILED) { |
| 1631 | weston_log("mmap failed: %m\n"); |
| 1632 | goto error; |
| 1633 | } |
| 1634 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1635 | keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context, |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 1636 | map_str, |
| 1637 | XKB_KEYMAP_FORMAT_TEXT_V1, |
| 1638 | 0); |
Jason Ekstrand | b7d9f2e | 2014-04-02 19:53:57 -0500 | [diff] [blame] | 1639 | munmap(map_str, size); |
| 1640 | |
| 1641 | if (!keymap) { |
| 1642 | weston_log("failed to compile keymap\n"); |
| 1643 | goto error; |
| 1644 | } |
| 1645 | |
| 1646 | input->keyboard_state_update = STATE_UPDATE_NONE; |
| 1647 | } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) { |
| 1648 | weston_log("No keymap provided; falling back to defalt\n"); |
| 1649 | keymap = NULL; |
| 1650 | input->keyboard_state_update = STATE_UPDATE_AUTOMATIC; |
| 1651 | } else { |
| 1652 | weston_log("Invalid keymap\n"); |
| 1653 | goto error; |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1654 | } |
| 1655 | |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1656 | close(fd); |
| 1657 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1658 | if (weston_seat_get_keyboard(&input->base)) |
Rui Matos | 0c194ce | 2013-10-10 19:44:21 +0200 | [diff] [blame] | 1659 | weston_seat_update_keymap(&input->base, keymap); |
| 1660 | else |
| 1661 | weston_seat_init_keyboard(&input->base, keymap); |
| 1662 | |
Ran Benita | c9c7415 | 2014-08-19 23:59:52 +0300 | [diff] [blame] | 1663 | xkb_keymap_unref(keymap); |
Jason Ekstrand | b7d9f2e | 2014-04-02 19:53:57 -0500 | [diff] [blame] | 1664 | |
| 1665 | return; |
| 1666 | |
| 1667 | error: |
| 1668 | wl_keyboard_release(input->parent.keyboard); |
| 1669 | close(fd); |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | static void |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1673 | input_handle_keyboard_enter(void *data, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1674 | struct wl_keyboard *keyboard, |
| 1675 | uint32_t serial, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1676 | struct wl_surface *surface, |
| 1677 | struct wl_array *keys) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1678 | { |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 1679 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1680 | struct wayland_output *focus; |
| 1681 | |
| 1682 | focus = input->keyboard_focus; |
| 1683 | if (focus) { |
| 1684 | /* This shouldn't happen */ |
| 1685 | focus->keyboard_count--; |
| 1686 | if (!focus->keyboard_count && focus->frame) |
| 1687 | frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE); |
| 1688 | if (frame_status(focus->frame) & FRAME_STATUS_REPAINT) |
| 1689 | weston_output_schedule_repaint(&focus->base); |
| 1690 | } |
| 1691 | |
| 1692 | input->keyboard_focus = wl_surface_get_user_data(surface); |
| 1693 | input->keyboard_focus->keyboard_count++; |
| 1694 | |
| 1695 | focus = input->keyboard_focus; |
| 1696 | if (focus->frame) { |
| 1697 | frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE); |
| 1698 | if (frame_status(focus->frame) & FRAME_STATUS_REPAINT) |
| 1699 | weston_output_schedule_repaint(&focus->base); |
| 1700 | } |
| 1701 | |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 1702 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1703 | /* XXX: If we get a modifier event immediately before the focus, |
| 1704 | * we should try to keep the same serial. */ |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1705 | notify_keyboard_focus_in(&input->base, keys, |
Daniel Stone | d6da09e | 2012-06-22 13:21:29 +0100 | [diff] [blame] | 1706 | STATE_UPDATE_AUTOMATIC); |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | static void |
| 1710 | input_handle_keyboard_leave(void *data, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1711 | struct wl_keyboard *keyboard, |
| 1712 | uint32_t serial, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1713 | struct wl_surface *surface) |
| 1714 | { |
| 1715 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1716 | struct wayland_output *focus; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1717 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1718 | notify_keyboard_focus_out(&input->base); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1719 | |
| 1720 | focus = input->keyboard_focus; |
| 1721 | if (!focus) |
Dima Ryazanov | 01d5c02 | 2015-05-18 23:14:16 -0700 | [diff] [blame] | 1722 | return; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1723 | |
| 1724 | focus->keyboard_count--; |
| 1725 | if (!focus->keyboard_count && focus->frame) { |
| 1726 | frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE); |
| 1727 | if (frame_status(focus->frame) & FRAME_STATUS_REPAINT) |
| 1728 | weston_output_schedule_repaint(&focus->base); |
| 1729 | } |
| 1730 | |
| 1731 | input->keyboard_focus = NULL; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1732 | } |
| 1733 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1734 | static void |
| 1735 | input_handle_key(void *data, struct wl_keyboard *keyboard, |
| 1736 | uint32_t serial, uint32_t time, uint32_t key, uint32_t state) |
| 1737 | { |
| 1738 | struct wayland_input *input = data; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1739 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1740 | input->key_serial = serial; |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1741 | notify_key(&input->base, time, key, |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 1742 | state ? WL_KEYBOARD_KEY_STATE_PRESSED : |
Daniel Stone | 1b4e11f | 2012-06-22 13:21:37 +0100 | [diff] [blame] | 1743 | WL_KEYBOARD_KEY_STATE_RELEASED, |
Jason Ekstrand | b7d9f2e | 2014-04-02 19:53:57 -0500 | [diff] [blame] | 1744 | input->keyboard_state_update); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1745 | } |
| 1746 | |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1747 | static void |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1748 | input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1749 | uint32_t serial_in, uint32_t mods_depressed, |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1750 | uint32_t mods_latched, uint32_t mods_locked, |
| 1751 | uint32_t group) |
| 1752 | { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1753 | struct weston_keyboard *keyboard; |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1754 | struct wayland_input *input = data; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1755 | struct wayland_backend *b = input->backend; |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1756 | uint32_t serial_out; |
| 1757 | |
| 1758 | /* If we get a key event followed by a modifier event with the |
| 1759 | * same serial number, then we try to preserve those semantics by |
| 1760 | * reusing the same serial number on the way out too. */ |
| 1761 | if (serial_in == input->key_serial) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1762 | serial_out = wl_display_get_serial(b->compositor->wl_display); |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1763 | else |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1764 | serial_out = wl_display_next_serial(b->compositor->wl_display); |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1765 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1766 | keyboard = weston_seat_get_keyboard(&input->base); |
| 1767 | xkb_state_update_mask(keyboard->xkb_state.state, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1768 | mods_depressed, mods_latched, |
| 1769 | mods_locked, 0, 0, group); |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1770 | notify_modifiers(&input->base, serial_out); |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1771 | } |
| 1772 | |
Jonny Lamb | 497994a | 2014-08-12 14:58:26 +0200 | [diff] [blame] | 1773 | static void |
| 1774 | input_handle_repeat_info(void *data, struct wl_keyboard *keyboard, |
| 1775 | int32_t rate, int32_t delay) |
| 1776 | { |
| 1777 | struct wayland_input *input = data; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1778 | struct wayland_backend *b = input->backend; |
Jonny Lamb | 497994a | 2014-08-12 14:58:26 +0200 | [diff] [blame] | 1779 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1780 | b->compositor->kb_repeat_rate = rate; |
| 1781 | b->compositor->kb_repeat_delay = delay; |
Jonny Lamb | 497994a | 2014-08-12 14:58:26 +0200 | [diff] [blame] | 1782 | } |
| 1783 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1784 | static const struct wl_keyboard_listener keyboard_listener = { |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1785 | input_handle_keymap, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1786 | input_handle_keyboard_enter, |
| 1787 | input_handle_keyboard_leave, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1788 | input_handle_key, |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1789 | input_handle_modifiers, |
Jonny Lamb | 497994a | 2014-08-12 14:58:26 +0200 | [diff] [blame] | 1790 | input_handle_repeat_info, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1791 | }; |
| 1792 | |
| 1793 | static void |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 1794 | input_handle_touch_down(void *data, struct wl_touch *wl_touch, |
| 1795 | uint32_t serial, uint32_t time, |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1796 | struct wl_surface *surface, int32_t id, |
| 1797 | wl_fixed_t fixed_x, wl_fixed_t fixed_y) |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 1798 | { |
| 1799 | struct wayland_input *input = data; |
| 1800 | struct wayland_output *output; |
| 1801 | enum theme_location location; |
| 1802 | bool first_touch; |
| 1803 | int32_t fx, fy; |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1804 | double x, y; |
| 1805 | |
| 1806 | x = wl_fixed_to_double(fixed_x); |
| 1807 | y = wl_fixed_to_double(fixed_y); |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 1808 | |
| 1809 | first_touch = (input->touch_points == 0); |
| 1810 | input->touch_points++; |
| 1811 | |
| 1812 | input->touch_focus = wl_surface_get_user_data(surface); |
| 1813 | output = input->touch_focus; |
| 1814 | if (!first_touch && !input->touch_active) |
| 1815 | return; |
| 1816 | |
| 1817 | if (output->frame) { |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1818 | location = frame_touch_down(output->frame, input, id, x, y); |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 1819 | |
| 1820 | frame_interior(output->frame, &fx, &fy, NULL, NULL); |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1821 | x -= fx; |
| 1822 | y -= fy; |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 1823 | |
| 1824 | if (frame_status(output->frame) & FRAME_STATUS_REPAINT) |
| 1825 | weston_output_schedule_repaint(&output->base); |
| 1826 | |
| 1827 | if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) { |
| 1828 | input->touch_points--; |
| 1829 | wl_shell_surface_move(output->parent.shell_surface, |
| 1830 | input->parent.seat, serial); |
| 1831 | frame_status_clear(output->frame, |
| 1832 | FRAME_STATUS_MOVE); |
| 1833 | return; |
| 1834 | } |
| 1835 | |
| 1836 | if (first_touch && location != THEME_LOCATION_CLIENT_AREA) |
| 1837 | return; |
| 1838 | } |
| 1839 | |
| 1840 | weston_output_transform_coordinate(&output->base, x, y, &x, &y); |
| 1841 | |
| 1842 | notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN); |
| 1843 | input->touch_active = true; |
| 1844 | } |
| 1845 | |
| 1846 | static void |
| 1847 | input_handle_touch_up(void *data, struct wl_touch *wl_touch, |
| 1848 | uint32_t serial, uint32_t time, int32_t id) |
| 1849 | { |
| 1850 | struct wayland_input *input = data; |
| 1851 | struct wayland_output *output = input->touch_focus; |
| 1852 | bool active = input->touch_active; |
| 1853 | |
| 1854 | input->touch_points--; |
| 1855 | if (input->touch_points == 0) { |
| 1856 | input->touch_focus = NULL; |
| 1857 | input->touch_active = false; |
| 1858 | } |
| 1859 | |
| 1860 | if (!output) |
| 1861 | return; |
| 1862 | |
| 1863 | if (output->frame) { |
| 1864 | frame_touch_up(output->frame, input, id); |
| 1865 | |
| 1866 | if (frame_status(output->frame) & FRAME_STATUS_CLOSE) { |
| 1867 | wayland_output_destroy(&output->base); |
| 1868 | input->touch_focus = NULL; |
| 1869 | input->keyboard_focus = NULL; |
| 1870 | if (wl_list_empty(&input->backend->compositor->output_list)) |
| 1871 | weston_compositor_exit(input->backend->compositor); |
| 1872 | |
| 1873 | return; |
| 1874 | } |
| 1875 | if (frame_status(output->frame) & FRAME_STATUS_REPAINT) |
| 1876 | weston_output_schedule_repaint(&output->base); |
| 1877 | } |
| 1878 | |
| 1879 | if (active) |
| 1880 | notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP); |
| 1881 | } |
| 1882 | |
| 1883 | static void |
| 1884 | input_handle_touch_motion(void *data, struct wl_touch *wl_touch, |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1885 | uint32_t time, int32_t id, |
| 1886 | wl_fixed_t fixed_x, wl_fixed_t fixed_y) |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 1887 | { |
| 1888 | struct wayland_input *input = data; |
| 1889 | struct wayland_output *output = input->touch_focus; |
| 1890 | int32_t fx, fy; |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1891 | double x, y; |
| 1892 | |
| 1893 | x = wl_fixed_to_double(fixed_x); |
| 1894 | y = wl_fixed_to_double(fixed_y); |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 1895 | |
| 1896 | if (!output || !input->touch_active) |
| 1897 | return; |
| 1898 | |
| 1899 | if (output->frame) { |
| 1900 | frame_interior(output->frame, &fx, &fy, NULL, NULL); |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1901 | x -= fx; |
| 1902 | y -= fy; |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | weston_output_transform_coordinate(&output->base, x, y, &x, &y); |
| 1906 | |
| 1907 | notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION); |
| 1908 | } |
| 1909 | |
| 1910 | static void |
| 1911 | input_handle_touch_frame(void *data, struct wl_touch *wl_touch) |
| 1912 | { |
| 1913 | struct wayland_input *input = data; |
| 1914 | |
| 1915 | if (!input->touch_focus || !input->touch_active) |
| 1916 | return; |
| 1917 | |
| 1918 | notify_touch_frame(&input->base); |
| 1919 | } |
| 1920 | |
| 1921 | static void |
| 1922 | input_handle_touch_cancel(void *data, struct wl_touch *wl_touch) |
| 1923 | { |
| 1924 | struct wayland_input *input = data; |
| 1925 | |
| 1926 | if (!input->touch_focus || !input->touch_active) |
| 1927 | return; |
| 1928 | |
| 1929 | notify_touch_cancel(&input->base); |
| 1930 | } |
| 1931 | |
| 1932 | static const struct wl_touch_listener touch_listener = { |
| 1933 | input_handle_touch_down, |
| 1934 | input_handle_touch_up, |
| 1935 | input_handle_touch_motion, |
| 1936 | input_handle_touch_frame, |
| 1937 | input_handle_touch_cancel, |
| 1938 | }; |
| 1939 | |
| 1940 | |
| 1941 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1942 | input_handle_capabilities(void *data, struct wl_seat *seat, |
| 1943 | enum wl_seat_capability caps) |
| 1944 | { |
| 1945 | struct wayland_input *input = data; |
| 1946 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1947 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) { |
| 1948 | input->parent.pointer = wl_seat_get_pointer(seat); |
| 1949 | wl_pointer_set_user_data(input->parent.pointer, input); |
| 1950 | wl_pointer_add_listener(input->parent.pointer, |
| 1951 | &pointer_listener, input); |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 1952 | weston_seat_init_pointer(&input->base); |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1953 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) { |
Derek Foreman | cfce7d0 | 2015-11-06 15:56:08 -0600 | [diff] [blame] | 1954 | if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) |
| 1955 | wl_pointer_release(input->parent.pointer); |
| 1956 | else |
| 1957 | wl_pointer_destroy(input->parent.pointer); |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1958 | input->parent.pointer = NULL; |
Derek Foreman | cfce7d0 | 2015-11-06 15:56:08 -0600 | [diff] [blame] | 1959 | weston_seat_release_pointer(&input->base); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1960 | } |
| 1961 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1962 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) { |
| 1963 | input->parent.keyboard = wl_seat_get_keyboard(seat); |
| 1964 | wl_keyboard_set_user_data(input->parent.keyboard, input); |
| 1965 | wl_keyboard_add_listener(input->parent.keyboard, |
| 1966 | &keyboard_listener, input); |
| 1967 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) { |
Derek Foreman | cfce7d0 | 2015-11-06 15:56:08 -0600 | [diff] [blame] | 1968 | if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION) |
| 1969 | wl_keyboard_release(input->parent.keyboard); |
| 1970 | else |
| 1971 | wl_keyboard_destroy(input->parent.keyboard); |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1972 | input->parent.keyboard = NULL; |
Derek Foreman | cfce7d0 | 2015-11-06 15:56:08 -0600 | [diff] [blame] | 1973 | weston_seat_release_keyboard(&input->base); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1974 | } |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 1975 | |
| 1976 | if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) { |
| 1977 | input->parent.touch = wl_seat_get_touch(seat); |
| 1978 | wl_touch_set_user_data(input->parent.touch, input); |
| 1979 | wl_touch_add_listener(input->parent.touch, |
| 1980 | &touch_listener, input); |
| 1981 | weston_seat_init_touch(&input->base); |
| 1982 | } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) { |
| 1983 | if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION) |
| 1984 | wl_touch_release(input->parent.touch); |
| 1985 | else |
| 1986 | wl_touch_destroy(input->parent.touch); |
| 1987 | input->parent.touch = NULL; |
| 1988 | weston_seat_release_touch(&input->base); |
| 1989 | } |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1990 | } |
| 1991 | |
Jonny Lamb | 497994a | 2014-08-12 14:58:26 +0200 | [diff] [blame] | 1992 | static void |
| 1993 | input_handle_name(void *data, struct wl_seat *seat, |
| 1994 | const char *name) |
| 1995 | { |
| 1996 | } |
| 1997 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1998 | static const struct wl_seat_listener seat_listener = { |
| 1999 | input_handle_capabilities, |
Jonny Lamb | 497994a | 2014-08-12 14:58:26 +0200 | [diff] [blame] | 2000 | input_handle_name, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 2001 | }; |
| 2002 | |
| 2003 | static void |
Derek Foreman | cfce7d0 | 2015-11-06 15:56:08 -0600 | [diff] [blame] | 2004 | display_add_seat(struct wayland_backend *b, uint32_t id, uint32_t available_version) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2005 | { |
| 2006 | struct wayland_input *input; |
Derek Foreman | cfce7d0 | 2015-11-06 15:56:08 -0600 | [diff] [blame] | 2007 | uint32_t version = MIN(available_version, 4); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2008 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 2009 | input = zalloc(sizeof *input); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2010 | if (input == NULL) |
| 2011 | return; |
| 2012 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2013 | weston_seat_init(&input->base, b->compositor, "default"); |
| 2014 | input->backend = b; |
| 2015 | input->parent.seat = wl_registry_bind(b->parent.registry, id, |
Derek Foreman | cfce7d0 | 2015-11-06 15:56:08 -0600 | [diff] [blame] | 2016 | &wl_seat_interface, version); |
| 2017 | input->seat_version = version; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2018 | wl_list_insert(b->input_list.prev, &input->link); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2019 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 2020 | wl_seat_add_listener(input->parent.seat, &seat_listener, input); |
| 2021 | wl_seat_set_user_data(input->parent.seat, input); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 2022 | |
| 2023 | input->parent.cursor.surface = |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2024 | wl_compositor_create_surface(b->parent.compositor); |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 2025 | |
| 2026 | input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL; |
| 2027 | input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | static void |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2031 | wayland_parent_output_geometry(void *data, struct wl_output *output_proxy, |
| 2032 | int32_t x, int32_t y, |
| 2033 | int32_t physical_width, int32_t physical_height, |
| 2034 | int32_t subpixel, const char *make, |
| 2035 | const char *model, int32_t transform) |
| 2036 | { |
| 2037 | struct wayland_parent_output *output = data; |
| 2038 | |
| 2039 | output->x = x; |
| 2040 | output->y = y; |
| 2041 | output->physical.width = physical_width; |
| 2042 | output->physical.height = physical_height; |
| 2043 | output->physical.subpixel = subpixel; |
| 2044 | |
| 2045 | free(output->physical.make); |
| 2046 | output->physical.make = strdup(make); |
| 2047 | free(output->physical.model); |
| 2048 | output->physical.model = strdup(model); |
| 2049 | |
| 2050 | output->transform = transform; |
| 2051 | } |
| 2052 | |
| 2053 | static struct weston_mode * |
| 2054 | find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh) |
| 2055 | { |
| 2056 | struct weston_mode *mode; |
| 2057 | |
| 2058 | wl_list_for_each(mode, list, link) { |
| 2059 | if (mode->width == width && mode->height == height && |
| 2060 | mode->refresh == refresh) |
| 2061 | return mode; |
| 2062 | } |
| 2063 | |
| 2064 | mode = zalloc(sizeof *mode); |
| 2065 | if (!mode) |
| 2066 | return NULL; |
| 2067 | |
| 2068 | mode->width = width; |
| 2069 | mode->height = height; |
| 2070 | mode->refresh = refresh; |
| 2071 | wl_list_insert(list, &mode->link); |
| 2072 | |
| 2073 | return mode; |
| 2074 | } |
| 2075 | |
| 2076 | static void |
| 2077 | wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy, |
| 2078 | uint32_t flags, int32_t width, int32_t height, |
| 2079 | int32_t refresh) |
| 2080 | { |
| 2081 | struct wayland_parent_output *output = data; |
| 2082 | struct weston_mode *mode; |
| 2083 | |
| 2084 | if (output->output) { |
| 2085 | mode = find_mode(&output->output->base.mode_list, |
| 2086 | width, height, refresh); |
| 2087 | if (!mode) |
| 2088 | return; |
| 2089 | mode->flags = flags; |
| 2090 | /* Do a mode-switch on current mode change? */ |
| 2091 | } else { |
| 2092 | mode = find_mode(&output->mode_list, width, height, refresh); |
| 2093 | if (!mode) |
| 2094 | return; |
| 2095 | mode->flags = flags; |
| 2096 | if (flags & WL_OUTPUT_MODE_CURRENT) |
| 2097 | output->current_mode = mode; |
| 2098 | if (flags & WL_OUTPUT_MODE_PREFERRED) |
| 2099 | output->preferred_mode = mode; |
| 2100 | } |
| 2101 | } |
| 2102 | |
| 2103 | static const struct wl_output_listener output_listener = { |
| 2104 | wayland_parent_output_geometry, |
| 2105 | wayland_parent_output_mode |
| 2106 | }; |
| 2107 | |
| 2108 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2109 | wayland_backend_register_output(struct wayland_backend *b, uint32_t id) |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2110 | { |
| 2111 | struct wayland_parent_output *output; |
| 2112 | |
| 2113 | output = zalloc(sizeof *output); |
| 2114 | if (!output) |
| 2115 | return; |
| 2116 | |
| 2117 | output->id = id; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2118 | output->global = wl_registry_bind(b->parent.registry, id, |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2119 | &wl_output_interface, 1); |
U. Artie Eoff | 8cbd8f3 | 2014-05-06 14:50:01 -0700 | [diff] [blame] | 2120 | if (!output->global) { |
| 2121 | free(output); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2122 | return; |
U. Artie Eoff | 8cbd8f3 | 2014-05-06 14:50:01 -0700 | [diff] [blame] | 2123 | } |
| 2124 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2125 | wl_output_add_listener(output->global, &output_listener, output); |
| 2126 | |
| 2127 | output->scale = 0; |
| 2128 | output->transform = WL_OUTPUT_TRANSFORM_NORMAL; |
| 2129 | output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 2130 | wl_list_init(&output->mode_list); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2131 | wl_list_insert(&b->parent.output_list, &output->link); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2132 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2133 | if (b->sprawl_across_outputs) { |
| 2134 | wl_display_roundtrip(b->parent.wl_display); |
| 2135 | wayland_output_create_for_parent_output(b, output); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | static void |
| 2140 | wayland_parent_output_destroy(struct wayland_parent_output *output) |
| 2141 | { |
| 2142 | struct weston_mode *mode, *next; |
| 2143 | |
| 2144 | if (output->output) |
| 2145 | wayland_output_destroy(&output->output->base); |
| 2146 | |
| 2147 | wl_output_destroy(output->global); |
| 2148 | free(output->physical.make); |
| 2149 | free(output->physical.model); |
| 2150 | |
| 2151 | wl_list_for_each_safe(mode, next, &output->mode_list, link) { |
| 2152 | wl_list_remove(&mode->link); |
| 2153 | free(mode); |
| 2154 | } |
| 2155 | } |
| 2156 | |
| 2157 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 2158 | registry_handle_global(void *data, struct wl_registry *registry, uint32_t name, |
| 2159 | const char *interface, uint32_t version) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2160 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2161 | struct wayland_backend *b = data; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2162 | |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 2163 | if (strcmp(interface, "wl_compositor") == 0) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2164 | b->parent.compositor = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 2165 | wl_registry_bind(registry, name, |
| 2166 | &wl_compositor_interface, 1); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 2167 | } else if (strcmp(interface, "wl_shell") == 0) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2168 | b->parent.shell = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 2169 | wl_registry_bind(registry, name, |
| 2170 | &wl_shell_interface, 1); |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 2171 | } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2172 | b->parent.fshell = |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2173 | wl_registry_bind(registry, name, |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 2174 | &zwp_fullscreen_shell_v1_interface, 1); |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 2175 | } else if (strcmp(interface, "wl_seat") == 0) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2176 | display_add_seat(b, name, version); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2177 | } else if (strcmp(interface, "wl_output") == 0) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2178 | wayland_backend_register_output(b, name); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2179 | } else if (strcmp(interface, "wl_shm") == 0) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2180 | b->parent.shm = |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2181 | wl_registry_bind(registry, name, &wl_shm_interface, 1); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2182 | } |
| 2183 | } |
| 2184 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2185 | static void |
| 2186 | registry_handle_global_remove(void *data, struct wl_registry *registry, |
| 2187 | uint32_t name) |
| 2188 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2189 | struct wayland_backend *b = data; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2190 | struct wayland_parent_output *output; |
| 2191 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2192 | wl_list_for_each(output, &b->parent.output_list, link) |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2193 | if (output->id == name) |
| 2194 | wayland_parent_output_destroy(output); |
| 2195 | } |
| 2196 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 2197 | static const struct wl_registry_listener registry_listener = { |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2198 | registry_handle_global, |
| 2199 | registry_handle_global_remove |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 2200 | }; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2201 | |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 2202 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2203 | wayland_backend_handle_event(int fd, uint32_t mask, void *data) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2204 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2205 | struct wayland_backend *b = data; |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 2206 | int count = 0; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2207 | |
Kristian Høgsberg | 453de7a | 2013-10-30 23:15:44 -0700 | [diff] [blame] | 2208 | if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) { |
Giulio Camuffo | 459137b | 2014-10-11 23:56:24 +0300 | [diff] [blame] | 2209 | weston_compositor_exit(b->compositor); |
Kristian Høgsberg | 453de7a | 2013-10-30 23:15:44 -0700 | [diff] [blame] | 2210 | return 0; |
| 2211 | } |
| 2212 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2213 | if (mask & WL_EVENT_READABLE) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2214 | count = wl_display_dispatch(b->parent.wl_display); |
Kristian Høgsberg | f258a31 | 2011-12-28 22:51:20 -0500 | [diff] [blame] | 2215 | if (mask & WL_EVENT_WRITABLE) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2216 | wl_display_flush(b->parent.wl_display); |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 2217 | |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 2218 | if (mask == 0) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2219 | count = wl_display_dispatch_pending(b->parent.wl_display); |
| 2220 | wl_display_flush(b->parent.wl_display); |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | return count; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2224 | } |
| 2225 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2226 | static void |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 2227 | wayland_restore(struct weston_compositor *ec) |
| 2228 | { |
| 2229 | } |
| 2230 | |
| 2231 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 2232 | wayland_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2233 | { |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 2234 | struct wayland_backend *b = to_wayland_backend(ec); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2235 | |
Armin Krezović | 78895c5 | 2016-10-09 17:30:28 +0200 | [diff] [blame^] | 2236 | wl_event_source_remove(b->parent.wl_source); |
| 2237 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 2238 | weston_compositor_shutdown(ec); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 2239 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2240 | if (b->parent.shm) |
| 2241 | wl_shm_destroy(b->parent.shm); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2242 | |
Armin Krezović | 78895c5 | 2016-10-09 17:30:28 +0200 | [diff] [blame^] | 2243 | if (b->parent.fshell) |
| 2244 | zwp_fullscreen_shell_v1_release(b->parent.fshell); |
| 2245 | |
| 2246 | if (b->parent.compositor) |
| 2247 | wl_compositor_destroy(b->parent.compositor); |
| 2248 | |
| 2249 | wl_registry_destroy(b->parent.registry); |
| 2250 | wl_display_flush(b->parent.wl_display); |
| 2251 | wl_display_disconnect(b->parent.wl_display); |
| 2252 | |
| 2253 | if (b->theme) |
| 2254 | theme_destroy(b->theme); |
| 2255 | |
| 2256 | if (b->frame_device) |
| 2257 | cairo_device_destroy(b->frame_device); |
| 2258 | |
| 2259 | wl_cursor_theme_destroy(b->cursor_theme); |
| 2260 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2261 | free(b); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2262 | } |
| 2263 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 2264 | static const char *left_ptrs[] = { |
| 2265 | "left_ptr", |
| 2266 | "default", |
| 2267 | "top_left_arrow", |
| 2268 | "left-arrow" |
| 2269 | }; |
| 2270 | |
| 2271 | static void |
Benoit Gschwind | 244ff79 | 2016-04-28 20:33:11 +0200 | [diff] [blame] | 2272 | create_cursor(struct wayland_backend *b, |
| 2273 | struct weston_wayland_backend_config *config) |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 2274 | { |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 2275 | unsigned int i; |
| 2276 | |
Benoit Gschwind | 244ff79 | 2016-04-28 20:33:11 +0200 | [diff] [blame] | 2277 | b->cursor_theme = wl_cursor_theme_load(config->cursor_theme, |
| 2278 | config->cursor_size, |
| 2279 | b->parent.shm); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2280 | if (!b->cursor_theme) { |
Hardening | 842a36a | 2014-03-18 14:12:50 +0100 | [diff] [blame] | 2281 | fprintf(stderr, "could not load cursor theme\n"); |
| 2282 | return; |
| 2283 | } |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 2284 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2285 | b->cursor = NULL; |
| 2286 | for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i) |
| 2287 | b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme, |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 2288 | left_ptrs[i]); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2289 | if (!b->cursor) { |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 2290 | fprintf(stderr, "could not load left cursor\n"); |
| 2291 | return; |
| 2292 | } |
| 2293 | } |
| 2294 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 2295 | static void |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 2296 | fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time, |
| 2297 | uint32_t key, void *data) |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 2298 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2299 | struct wayland_backend *b = data; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 2300 | struct wayland_input *input = NULL; |
| 2301 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2302 | wl_list_for_each(input, &b->input_list, link) |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 2303 | if (&input->base == keyboard->seat) |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 2304 | break; |
| 2305 | |
| 2306 | if (!input || !input->output) |
| 2307 | return; |
| 2308 | |
| 2309 | if (input->output->frame) |
| 2310 | wayland_output_set_fullscreen(input->output, 0, 0, NULL); |
| 2311 | else |
| 2312 | wayland_output_set_windowed(input->output); |
| 2313 | |
| 2314 | weston_output_schedule_repaint(&input->output->base); |
| 2315 | } |
| 2316 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2317 | static struct wayland_backend * |
Benoit Gschwind | 3a49b51 | 2016-04-28 20:33:10 +0200 | [diff] [blame] | 2318 | wayland_backend_create(struct weston_compositor *compositor, |
Pekka Paalanen | a256c5e | 2016-06-03 14:56:18 +0300 | [diff] [blame] | 2319 | struct weston_wayland_backend_config *new_config) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2320 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2321 | struct wayland_backend *b; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2322 | struct wl_event_loop *loop; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2323 | int fd; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2324 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2325 | b = zalloc(sizeof *b); |
| 2326 | if (b == NULL) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2327 | return NULL; |
| 2328 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2329 | b->compositor = compositor; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2330 | if (weston_compositor_set_presentation_clock_software(compositor) < 0) |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 2331 | goto err_compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2332 | |
Benoit Gschwind | 3a49b51 | 2016-04-28 20:33:10 +0200 | [diff] [blame] | 2333 | b->parent.wl_display = wl_display_connect(new_config->display_name); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2334 | if (b->parent.wl_display == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2335 | weston_log("failed to create display: %m\n"); |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 2336 | goto err_compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2337 | } |
| 2338 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2339 | wl_list_init(&b->parent.output_list); |
| 2340 | wl_list_init(&b->input_list); |
| 2341 | b->parent.registry = wl_display_get_registry(b->parent.wl_display); |
| 2342 | wl_registry_add_listener(b->parent.registry, ®istry_listener, b); |
| 2343 | wl_display_roundtrip(b->parent.wl_display); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 2344 | |
Benoit Gschwind | 244ff79 | 2016-04-28 20:33:11 +0200 | [diff] [blame] | 2345 | create_cursor(b, new_config); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2346 | |
Benoit Gschwind | 3a49b51 | 2016-04-28 20:33:10 +0200 | [diff] [blame] | 2347 | b->use_pixman = new_config->use_pixman; |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 2348 | b->fullscreen = new_config->fullscreen; |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 2349 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2350 | if (!b->use_pixman) { |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 2351 | gl_renderer = weston_load_module("gl-renderer.so", |
| 2352 | "gl_renderer_interface"); |
| 2353 | if (!gl_renderer) |
Armin Krezović | 7e71b87 | 2016-10-09 17:30:22 +0200 | [diff] [blame] | 2354 | b->use_pixman = true; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 2355 | } |
| 2356 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2357 | if (!b->use_pixman) { |
Miguel A. Vico | dddc670 | 2016-05-18 17:41:07 +0200 | [diff] [blame] | 2358 | if (gl_renderer->display_create(compositor, |
| 2359 | EGL_PLATFORM_WAYLAND_KHR, |
| 2360 | b->parent.wl_display, |
Miguel A. Vico | 41700e3 | 2016-05-18 17:47:59 +0200 | [diff] [blame] | 2361 | NULL, |
Miguel A. Vico | dddc670 | 2016-05-18 17:41:07 +0200 | [diff] [blame] | 2362 | gl_renderer->alpha_attribs, |
| 2363 | NULL, |
| 2364 | 0) < 0) { |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 2365 | weston_log("Failed to initialize the GL renderer; " |
| 2366 | "falling back to pixman.\n"); |
Armin Krezović | 7e71b87 | 2016-10-09 17:30:22 +0200 | [diff] [blame] | 2367 | b->use_pixman = true; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 2368 | } |
| 2369 | } |
| 2370 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2371 | if (b->use_pixman) { |
| 2372 | if (pixman_renderer_init(compositor) < 0) { |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 2373 | weston_log("Failed to initialize pixman renderer\n"); |
| 2374 | goto err_display; |
| 2375 | } |
| 2376 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2377 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2378 | b->base.destroy = wayland_destroy; |
| 2379 | b->base.restore = wayland_restore; |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 2380 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2381 | loop = wl_display_get_event_loop(compositor->wl_display); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2382 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2383 | fd = wl_display_get_fd(b->parent.wl_display); |
| 2384 | b->parent.wl_source = |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2385 | wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2386 | wayland_backend_handle_event, b); |
| 2387 | if (b->parent.wl_source == NULL) |
Dawid Gajownik | 82d4925 | 2015-07-31 00:02:28 -0300 | [diff] [blame] | 2388 | goto err_display; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2389 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2390 | wl_event_source_check(b->parent.wl_source); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2391 | |
Emmanuel Gil Peyrot | c59f18e | 2015-09-25 11:58:40 +0200 | [diff] [blame] | 2392 | if (compositor->renderer->import_dmabuf) { |
| 2393 | if (linux_dmabuf_setup(compositor) < 0) |
| 2394 | weston_log("Error: initializing dmabuf " |
| 2395 | "support failed.\n"); |
| 2396 | } |
| 2397 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2398 | compositor->backend = &b->base; |
| 2399 | return b; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2400 | err_display: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2401 | wl_display_disconnect(b->parent.wl_display); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2402 | err_compositor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2403 | weston_compositor_shutdown(compositor); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2404 | free(b); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2405 | return NULL; |
| 2406 | } |
| 2407 | |
| 2408 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2409 | wayland_backend_destroy(struct wayland_backend *b) |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2410 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2411 | wl_display_disconnect(b->parent.wl_display); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2412 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2413 | if (b->theme) |
| 2414 | theme_destroy(b->theme); |
| 2415 | if (b->frame_device) |
| 2416 | cairo_device_destroy(b->frame_device); |
| 2417 | wl_cursor_theme_destroy(b->cursor_theme); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2418 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2419 | weston_compositor_shutdown(b->compositor); |
| 2420 | free(b); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2421 | } |
| 2422 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 2423 | static const struct weston_windowed_output_api windowed_api = { |
| 2424 | wayland_output_set_size, |
| 2425 | wayland_output_create, |
| 2426 | }; |
| 2427 | |
Benoit Gschwind | cd63b3e | 2016-04-28 20:33:14 +0200 | [diff] [blame] | 2428 | static void |
Benoit Gschwind | e091b45 | 2016-05-10 22:47:48 +0200 | [diff] [blame] | 2429 | config_init_to_defaults(struct weston_wayland_backend_config *config) |
| 2430 | { |
| 2431 | } |
| 2432 | |
Benoit Gschwind | cd63b3e | 2016-04-28 20:33:14 +0200 | [diff] [blame] | 2433 | WL_EXPORT int |
Pekka Paalanen | a256c5e | 2016-06-03 14:56:18 +0300 | [diff] [blame] | 2434 | backend_init(struct weston_compositor *compositor, |
Benoit Gschwind | cd63b3e | 2016-04-28 20:33:14 +0200 | [diff] [blame] | 2435 | struct weston_backend_config *config_base) |
| 2436 | { |
| 2437 | struct wayland_backend *b; |
Benoit Gschwind | cd63b3e | 2016-04-28 20:33:14 +0200 | [diff] [blame] | 2438 | struct wayland_parent_output *poutput; |
| 2439 | struct weston_wayland_backend_config new_config; |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 2440 | int ret; |
Benoit Gschwind | cd63b3e | 2016-04-28 20:33:14 +0200 | [diff] [blame] | 2441 | |
Benoit Gschwind | e091b45 | 2016-05-10 22:47:48 +0200 | [diff] [blame] | 2442 | if (config_base == NULL || |
| 2443 | config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION || |
| 2444 | config_base->struct_size > sizeof(struct weston_wayland_backend_config)) { |
| 2445 | weston_log("wayland backend config structure is invalid\n"); |
Benoit Gschwind | e091b45 | 2016-05-10 22:47:48 +0200 | [diff] [blame] | 2446 | return -1; |
| 2447 | } |
| 2448 | |
| 2449 | config_init_to_defaults(&new_config); |
| 2450 | memcpy(&new_config, config_base, config_base->struct_size); |
| 2451 | |
Pekka Paalanen | a256c5e | 2016-06-03 14:56:18 +0300 | [diff] [blame] | 2452 | b = wayland_backend_create(compositor, &new_config); |
Benoit Gschwind | 37a6807 | 2016-04-28 20:33:08 +0200 | [diff] [blame] | 2453 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2454 | if (!b) |
| 2455 | return -1; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2456 | |
Benoit Gschwind | 37a6807 | 2016-04-28 20:33:08 +0200 | [diff] [blame] | 2457 | if (new_config.sprawl || b->parent.fshell) { |
Armin Krezović | 7f1c0b8 | 2016-10-09 17:30:23 +0200 | [diff] [blame] | 2458 | b->sprawl_across_outputs = true; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2459 | wl_display_roundtrip(b->parent.wl_display); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2460 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2461 | wl_list_for_each(poutput, &b->parent.output_list, link) |
| 2462 | wayland_output_create_for_parent_output(b, poutput); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2463 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2464 | return 0; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2465 | } |
| 2466 | |
Benoit Gschwind | 37a6807 | 2016-04-28 20:33:08 +0200 | [diff] [blame] | 2467 | if (new_config.fullscreen) { |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 2468 | if (wayland_output_create_fullscreen(b) < 0) { |
| 2469 | weston_log("Unable to create a fullscreen output.\n"); |
Benoit Gschwind | cd63b3e | 2016-04-28 20:33:14 +0200 | [diff] [blame] | 2470 | goto err_outputs; |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 2471 | } |
Benoit Gschwind | 830b788 | 2016-04-28 20:33:12 +0200 | [diff] [blame] | 2472 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2473 | return 0; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 2474 | } |
| 2475 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 2476 | ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME, |
| 2477 | &windowed_api, sizeof(windowed_api)); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2478 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 2479 | if (ret < 0) { |
| 2480 | weston_log("Failed to register output API.\n"); |
| 2481 | wayland_backend_destroy(b); |
| 2482 | return -1; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 2483 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2484 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2485 | weston_compositor_add_key_binding(compositor, KEY_F, |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2486 | MODIFIER_CTRL | MODIFIER_ALT, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2487 | fullscreen_binding, b); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2488 | return 0; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2489 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 2490 | err_outputs: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2491 | wayland_backend_destroy(b); |
| 2492 | return -1; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2493 | } |