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 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 78 | int use_pixman; |
Jason Ekstrand | e4ca8b0 | 2014-04-02 19:53:55 -0500 | [diff] [blame] | 79 | int sprawl_across_outputs; |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 80 | int 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 { |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 94 | int draw_initial_frame; |
| 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) { |
| 464 | output->parent.draw_initial_frame = 0; |
| 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ć | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 648 | /* Done on output->enable when not fullscreen, otherwise |
| 649 | * done in output_create, to get the proper mode */ |
| 650 | if (!b->fullscreen) |
| 651 | wayland_backend_destroy_output_surface(output); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 652 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 653 | if (output->frame) |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 654 | frame_destroy(output->frame); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 655 | |
| 656 | cairo_surface_destroy(output->gl.border.top); |
| 657 | cairo_surface_destroy(output->gl.border.left); |
| 658 | cairo_surface_destroy(output->gl.border.right); |
| 659 | cairo_surface_destroy(output->gl.border.bottom); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 660 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 661 | return 0; |
| 662 | } |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 663 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 664 | static void |
| 665 | wayland_output_destroy(struct weston_output *base) |
| 666 | { |
| 667 | struct wayland_output *output = to_wayland_output(base); |
| 668 | struct wayland_backend *b = to_wayland_backend(base->compositor); |
| 669 | |
| 670 | wayland_output_disable(&output->base); |
| 671 | |
| 672 | if (b->fullscreen) |
| 673 | wayland_backend_destroy_output_surface(output); |
| 674 | |
| 675 | weston_output_destroy(&output->base); |
| 676 | |
| 677 | free(output); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 680 | static const struct wl_shell_surface_listener shell_surface_listener; |
| 681 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 682 | static int |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 683 | wayland_output_init_gl_renderer(struct wayland_output *output) |
| 684 | { |
Jason Ekstrand | 00b8428 | 2013-10-27 22:24:59 -0500 | [diff] [blame] | 685 | int32_t fwidth = 0, fheight = 0; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 686 | |
| 687 | if (output->frame) { |
| 688 | fwidth = frame_width(output->frame); |
| 689 | fheight = frame_height(output->frame); |
| 690 | } else { |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 691 | fwidth = output->base.current_mode->width; |
| 692 | fheight = output->base.current_mode->height; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | output->gl.egl_window = |
| 696 | wl_egl_window_create(output->parent.surface, |
| 697 | fwidth, fheight); |
| 698 | if (!output->gl.egl_window) { |
| 699 | weston_log("failure to create wl_egl_window\n"); |
| 700 | return -1; |
| 701 | } |
| 702 | |
| 703 | if (gl_renderer->output_create(&output->base, |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 704 | output->gl.egl_window, |
Jonny Lamb | 671148f | 2015-03-20 15:26:52 +0100 | [diff] [blame] | 705 | output->gl.egl_window, |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 706 | gl_renderer->alpha_attribs, |
Derek Foreman | e76f185 | 2015-05-15 12:12:39 -0500 | [diff] [blame] | 707 | NULL, |
| 708 | 0) < 0) |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 709 | goto cleanup_window; |
| 710 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 711 | return 0; |
| 712 | |
| 713 | cleanup_window: |
| 714 | wl_egl_window_destroy(output->gl.egl_window); |
| 715 | return -1; |
| 716 | } |
| 717 | |
| 718 | static int |
| 719 | wayland_output_init_pixman_renderer(struct wayland_output *output) |
| 720 | { |
| 721 | return pixman_renderer_output_create(&output->base); |
| 722 | } |
| 723 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 724 | static void |
| 725 | wayland_output_resize_surface(struct wayland_output *output) |
| 726 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 727 | struct wayland_backend *b = |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 728 | to_wayland_backend(output->base.compositor); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 729 | struct wayland_shm_buffer *buffer, *next; |
| 730 | int32_t ix, iy, iwidth, iheight; |
| 731 | int32_t width, height; |
| 732 | struct wl_region *region; |
| 733 | |
| 734 | width = output->base.current_mode->width; |
| 735 | height = output->base.current_mode->height; |
| 736 | |
| 737 | if (output->frame) { |
| 738 | frame_resize_inside(output->frame, width, height); |
| 739 | |
| 740 | frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 741 | region = wl_compositor_create_region(b->parent.compositor); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 742 | wl_region_add(region, ix, iy, iwidth, iheight); |
| 743 | wl_surface_set_input_region(output->parent.surface, region); |
| 744 | wl_region_destroy(region); |
| 745 | |
| 746 | frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 747 | region = wl_compositor_create_region(b->parent.compositor); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 748 | wl_region_add(region, ix, iy, iwidth, iheight); |
| 749 | wl_surface_set_opaque_region(output->parent.surface, region); |
| 750 | wl_region_destroy(region); |
| 751 | |
| 752 | width = frame_width(output->frame); |
| 753 | height = frame_height(output->frame); |
| 754 | } else { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 755 | region = wl_compositor_create_region(b->parent.compositor); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 756 | wl_region_add(region, 0, 0, width, height); |
| 757 | wl_surface_set_input_region(output->parent.surface, region); |
| 758 | wl_region_destroy(region); |
| 759 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 760 | region = wl_compositor_create_region(b->parent.compositor); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 761 | wl_region_add(region, 0, 0, width, height); |
| 762 | wl_surface_set_opaque_region(output->parent.surface, region); |
| 763 | wl_region_destroy(region); |
| 764 | } |
| 765 | |
| 766 | if (output->gl.egl_window) { |
| 767 | wl_egl_window_resize(output->gl.egl_window, |
| 768 | width, height, 0, 0); |
| 769 | |
| 770 | /* These will need to be re-created due to the resize */ |
| 771 | gl_renderer->output_set_border(&output->base, |
| 772 | GL_RENDERER_BORDER_TOP, |
| 773 | 0, 0, 0, NULL); |
| 774 | cairo_surface_destroy(output->gl.border.top); |
| 775 | output->gl.border.top = NULL; |
| 776 | gl_renderer->output_set_border(&output->base, |
| 777 | GL_RENDERER_BORDER_LEFT, |
| 778 | 0, 0, 0, NULL); |
| 779 | cairo_surface_destroy(output->gl.border.left); |
| 780 | output->gl.border.left = NULL; |
| 781 | gl_renderer->output_set_border(&output->base, |
| 782 | GL_RENDERER_BORDER_RIGHT, |
| 783 | 0, 0, 0, NULL); |
| 784 | cairo_surface_destroy(output->gl.border.right); |
| 785 | output->gl.border.right = NULL; |
| 786 | gl_renderer->output_set_border(&output->base, |
| 787 | GL_RENDERER_BORDER_BOTTOM, |
| 788 | 0, 0, 0, NULL); |
| 789 | cairo_surface_destroy(output->gl.border.bottom); |
| 790 | output->gl.border.bottom = NULL; |
| 791 | } |
| 792 | |
| 793 | /* Throw away any remaining SHM buffers */ |
Derek Foreman | f53beb8 | 2015-05-29 16:32:52 -0500 | [diff] [blame] | 794 | 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] | 795 | wayland_shm_buffer_destroy(buffer); |
| 796 | /* These will get thrown away when they get released */ |
| 797 | wl_list_for_each(buffer, &output->shm.buffers, link) |
| 798 | buffer->output = NULL; |
| 799 | } |
| 800 | |
| 801 | static int |
| 802 | wayland_output_set_windowed(struct wayland_output *output) |
| 803 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 804 | struct wayland_backend *b = |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 805 | to_wayland_backend(output->base.compositor); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 806 | int tlen; |
| 807 | char *title; |
| 808 | |
| 809 | if (output->frame) |
| 810 | return 0; |
| 811 | |
| 812 | if (output->name) { |
| 813 | tlen = strlen(output->name) + strlen(WINDOW_TITLE " - "); |
| 814 | title = malloc(tlen + 1); |
| 815 | if (!title) |
| 816 | return -1; |
| 817 | |
| 818 | snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name); |
| 819 | } else { |
| 820 | title = strdup(WINDOW_TITLE); |
| 821 | } |
| 822 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 823 | if (!b->theme) { |
| 824 | b->theme = theme_create(); |
| 825 | if (!b->theme) { |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 826 | free(title); |
| 827 | return -1; |
| 828 | } |
| 829 | } |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 830 | output->frame = frame_create(b->theme, 100, 100, |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 831 | FRAME_BUTTON_CLOSE, title); |
| 832 | free(title); |
| 833 | if (!output->frame) |
| 834 | return -1; |
| 835 | |
| 836 | if (output->keyboard_count) |
| 837 | frame_set_flag(output->frame, FRAME_FLAG_ACTIVE); |
| 838 | |
| 839 | wayland_output_resize_surface(output); |
| 840 | |
| 841 | wl_shell_surface_set_toplevel(output->parent.shell_surface); |
| 842 | |
| 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | static void |
| 847 | wayland_output_set_fullscreen(struct wayland_output *output, |
| 848 | enum wl_shell_surface_fullscreen_method method, |
| 849 | uint32_t framerate, struct wl_output *target) |
| 850 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 851 | struct wayland_backend *b = |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 852 | to_wayland_backend(output->base.compositor); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 853 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 854 | if (output->frame) { |
| 855 | frame_destroy(output->frame); |
| 856 | output->frame = NULL; |
| 857 | } |
| 858 | |
| 859 | wayland_output_resize_surface(output); |
| 860 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 861 | if (output->parent.shell_surface) { |
| 862 | wl_shell_surface_set_fullscreen(output->parent.shell_surface, |
| 863 | method, framerate, target); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 864 | } else if (b->parent.fshell) { |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 865 | zwp_fullscreen_shell_v1_present_surface(b->parent.fshell, |
| 866 | output->parent.surface, |
| 867 | method, target); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | |
| 871 | static struct weston_mode * |
| 872 | wayland_output_choose_mode(struct wayland_output *output, |
| 873 | struct weston_mode *ref_mode) |
| 874 | { |
| 875 | struct weston_mode *mode; |
| 876 | |
| 877 | /* First look for an exact match */ |
| 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 | mode->refresh == ref_mode->refresh) |
| 882 | return mode; |
| 883 | |
| 884 | /* If we can't find an exact match, ignore refresh and try again */ |
| 885 | wl_list_for_each(mode, &output->base.mode_list, link) |
| 886 | if (mode->width == ref_mode->width && |
| 887 | mode->height == ref_mode->height) |
| 888 | return mode; |
| 889 | |
| 890 | /* Yeah, we failed */ |
| 891 | return NULL; |
| 892 | } |
| 893 | |
| 894 | enum mode_status { |
| 895 | MODE_STATUS_UNKNOWN, |
| 896 | MODE_STATUS_SUCCESS, |
| 897 | MODE_STATUS_FAIL, |
| 898 | MODE_STATUS_CANCEL, |
| 899 | }; |
| 900 | |
| 901 | static void |
| 902 | mode_feedback_successful(void *data, |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 903 | struct zwp_fullscreen_shell_mode_feedback_v1 *fb) |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 904 | { |
| 905 | enum mode_status *value = data; |
| 906 | |
| 907 | printf("Mode switch successful\n"); |
| 908 | |
| 909 | *value = MODE_STATUS_SUCCESS; |
| 910 | } |
| 911 | |
| 912 | static void |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 913 | 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] | 914 | { |
| 915 | enum mode_status *value = data; |
| 916 | |
| 917 | printf("Mode switch failed\n"); |
| 918 | |
| 919 | *value = MODE_STATUS_FAIL; |
| 920 | } |
| 921 | |
| 922 | static void |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 923 | 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] | 924 | { |
| 925 | enum mode_status *value = data; |
| 926 | |
| 927 | printf("Mode switch cancelled\n"); |
| 928 | |
| 929 | *value = MODE_STATUS_CANCEL; |
| 930 | } |
| 931 | |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 932 | struct zwp_fullscreen_shell_mode_feedback_v1_listener mode_feedback_listener = { |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 933 | mode_feedback_successful, |
| 934 | mode_feedback_failed, |
| 935 | mode_feedback_cancelled, |
| 936 | }; |
| 937 | |
| 938 | static int |
| 939 | wayland_output_switch_mode(struct weston_output *output_base, |
| 940 | struct weston_mode *mode) |
| 941 | { |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 942 | struct wayland_output *output = to_wayland_output(output_base); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 943 | struct wayland_backend *b; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 944 | struct wl_surface *old_surface; |
| 945 | struct weston_mode *old_mode; |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 946 | struct zwp_fullscreen_shell_mode_feedback_v1 *mode_feedback; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 947 | enum mode_status mode_status; |
| 948 | int ret = 0; |
| 949 | |
| 950 | if (output_base == NULL) { |
| 951 | weston_log("output is NULL.\n"); |
| 952 | return -1; |
| 953 | } |
| 954 | |
| 955 | if (mode == NULL) { |
| 956 | weston_log("mode is NULL.\n"); |
| 957 | return -1; |
| 958 | } |
| 959 | |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 960 | b = to_wayland_backend(output_base->compositor); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 961 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 962 | if (output->parent.shell_surface || !b->parent.fshell) |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 963 | return -1; |
| 964 | |
| 965 | mode = wayland_output_choose_mode(output, mode); |
| 966 | if (mode == NULL) |
| 967 | return -1; |
| 968 | |
| 969 | if (output->base.current_mode == mode) |
| 970 | return 0; |
| 971 | |
| 972 | old_mode = output->base.current_mode; |
| 973 | old_surface = output->parent.surface; |
| 974 | output->base.current_mode = mode; |
| 975 | output->parent.surface = |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 976 | wl_compositor_create_surface(b->parent.compositor); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 977 | wl_surface_set_user_data(output->parent.surface, output); |
| 978 | |
| 979 | /* Blow the old buffers because we changed size/surfaces */ |
| 980 | wayland_output_resize_surface(output); |
| 981 | |
| 982 | mode_feedback = |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 983 | zwp_fullscreen_shell_v1_present_surface_for_mode(b->parent.fshell, |
| 984 | output->parent.surface, |
| 985 | output->parent.output, |
| 986 | mode->refresh); |
| 987 | zwp_fullscreen_shell_mode_feedback_v1_add_listener(mode_feedback, |
| 988 | &mode_feedback_listener, |
| 989 | &mode_status); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 990 | |
| 991 | /* This should kick-start things again */ |
| 992 | output->parent.draw_initial_frame = 1; |
| 993 | wayland_output_start_repaint_loop(&output->base); |
| 994 | |
| 995 | mode_status = MODE_STATUS_UNKNOWN; |
| 996 | while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 997 | ret = wl_display_dispatch(b->parent.wl_display); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 998 | |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 999 | zwp_fullscreen_shell_mode_feedback_v1_destroy(mode_feedback); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1000 | |
| 1001 | if (mode_status == MODE_STATUS_FAIL) { |
| 1002 | output->base.current_mode = old_mode; |
| 1003 | wl_surface_destroy(output->parent.surface); |
| 1004 | output->parent.surface = old_surface; |
| 1005 | wayland_output_resize_surface(output); |
| 1006 | |
| 1007 | return -1; |
| 1008 | } |
| 1009 | |
| 1010 | old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT; |
| 1011 | output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT; |
| 1012 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1013 | if (b->use_pixman) { |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1014 | pixman_renderer_output_destroy(output_base); |
| 1015 | if (wayland_output_init_pixman_renderer(output) < 0) |
| 1016 | goto err_output; |
| 1017 | } else { |
| 1018 | gl_renderer->output_destroy(output_base); |
| 1019 | wl_egl_window_destroy(output->gl.egl_window); |
| 1020 | if (wayland_output_init_gl_renderer(output) < 0) |
| 1021 | goto err_output; |
| 1022 | } |
| 1023 | wl_surface_destroy(old_surface); |
| 1024 | |
| 1025 | weston_output_schedule_repaint(&output->base); |
| 1026 | |
| 1027 | return 0; |
| 1028 | |
| 1029 | err_output: |
| 1030 | /* XXX */ |
| 1031 | return -1; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1032 | } |
| 1033 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1034 | static int |
| 1035 | wayland_backend_create_output_surface(struct wayland_output *output) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1036 | { |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1037 | struct wayland_backend *b = to_wayland_backend(output->base.compositor); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1038 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1039 | output->parent.surface = |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1040 | wl_compositor_create_surface(b->parent.compositor); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1041 | if (!output->parent.surface) |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1042 | return -1; |
| 1043 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1044 | wl_surface_set_user_data(output->parent.surface, output); |
| 1045 | |
| 1046 | output->parent.draw_initial_frame = 1; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1047 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1048 | if (b->parent.shell) { |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1049 | output->parent.shell_surface = |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1050 | wl_shell_get_shell_surface(b->parent.shell, |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1051 | output->parent.surface); |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1052 | if (!output->parent.shell_surface) { |
| 1053 | wl_surface_destroy(output->parent.surface); |
| 1054 | return -1; |
| 1055 | } |
| 1056 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1057 | wl_shell_surface_add_listener(output->parent.shell_surface, |
| 1058 | &shell_surface_listener, output); |
| 1059 | } |
| 1060 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1061 | return 0; |
| 1062 | } |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1063 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1064 | static int |
| 1065 | wayland_output_enable(struct weston_output *base) |
| 1066 | { |
| 1067 | struct wayland_output *output = to_wayland_output(base); |
| 1068 | struct wayland_backend *b = to_wayland_backend(base->compositor); |
| 1069 | int ret = 0; |
| 1070 | |
| 1071 | |
| 1072 | weston_log("Creating %dx%d wayland output at (%d, %d)\n", |
| 1073 | output->base.current_mode->width, |
| 1074 | output->base.current_mode->height, |
| 1075 | output->base.x, output->base.y); |
| 1076 | |
| 1077 | /* If fullscreen was specified, this needs to be done before |
| 1078 | * enable to get the proper mode */ |
| 1079 | if (!b->fullscreen) |
| 1080 | ret = wayland_backend_create_output_surface(output); |
| 1081 | |
| 1082 | if (ret < 0) |
| 1083 | return -1; |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 1084 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 1085 | wl_list_init(&output->shm.buffers); |
| 1086 | wl_list_init(&output->shm.free_buffers); |
| 1087 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1088 | if (b->use_pixman) { |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 1089 | if (wayland_output_init_pixman_renderer(output) < 0) |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1090 | goto err_output; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 1091 | } else { |
| 1092 | if (wayland_output_init_gl_renderer(output) < 0) |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1093 | goto err_output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1094 | } |
| 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: |
| 1125 | if (!b->fullscreen) |
| 1126 | wayland_backend_destroy_output_surface(output); |
| 1127 | |
| 1128 | return -1; |
| 1129 | } |
| 1130 | |
| 1131 | static struct wayland_output * |
| 1132 | wayland_output_create_common(void) |
| 1133 | { |
| 1134 | struct wayland_output *output; |
| 1135 | |
| 1136 | output = zalloc(sizeof *output); |
| 1137 | if (output == NULL) { |
| 1138 | perror("zalloc"); |
| 1139 | return NULL; |
| 1140 | } |
| 1141 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1142 | output->base.destroy = wayland_output_destroy; |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1143 | output->base.disable = wayland_output_disable; |
| 1144 | output->base.enable = wayland_output_enable; |
| 1145 | |
| 1146 | return output; |
| 1147 | } |
| 1148 | |
| 1149 | static int |
| 1150 | wayland_output_create(struct weston_compositor *compositor, const char *name) |
| 1151 | { |
| 1152 | struct wayland_output *output = wayland_output_create_common(); |
| 1153 | |
| 1154 | /* name can't be NULL. */ |
| 1155 | assert(name); |
| 1156 | |
| 1157 | output->base.name = strdup(name); |
| 1158 | |
Armin Krezović | 4008740 | 2016-09-30 14:11:12 +0200 | [diff] [blame] | 1159 | weston_output_init(&output->base, compositor); |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1160 | weston_compositor_add_pending_output(&output->base, compositor); |
| 1161 | |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | static int |
| 1166 | wayland_output_set_size(struct weston_output *base, int width, int height) |
| 1167 | { |
| 1168 | struct wayland_output *output = to_wayland_output(base); |
| 1169 | struct wayland_backend *b = to_wayland_backend(base->compositor); |
| 1170 | int output_width, output_height; |
| 1171 | |
| 1172 | /* We can only be called once. */ |
| 1173 | assert(!output->base.current_mode); |
| 1174 | |
| 1175 | /* Make sure we have scale set. */ |
| 1176 | assert(output->base.scale); |
| 1177 | |
| 1178 | if (width < 1) { |
| 1179 | weston_log("Invalid width \"%d\" for output %s\n", |
| 1180 | width, output->base.name); |
| 1181 | return -1; |
| 1182 | } |
| 1183 | |
| 1184 | if (height < 1) { |
| 1185 | weston_log("Invalid height \"%d\" for output %s\n", |
| 1186 | height, output->base.name); |
| 1187 | return -1; |
| 1188 | } |
| 1189 | |
| 1190 | output_width = width * output->base.scale; |
| 1191 | output_height = height * output->base.scale; |
| 1192 | |
| 1193 | output->mode.flags = |
| 1194 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 1195 | |
| 1196 | output->mode.width = output_width; |
| 1197 | output->mode.height = output_height; |
| 1198 | output->mode.refresh = 60000; |
| 1199 | output->scale = output->base.scale; |
| 1200 | wl_list_init(&output->base.mode_list); |
| 1201 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
| 1202 | |
| 1203 | output->base.current_mode = &output->mode; |
| 1204 | output->base.make = "wayland"; |
| 1205 | output->base.model = "none"; |
| 1206 | |
| 1207 | /* XXX: Calculate proper size. */ |
| 1208 | output->base.mm_width = width; |
| 1209 | output->base.mm_height = height; |
| 1210 | |
| 1211 | if (b->use_pixman) |
| 1212 | output->base.repaint = wayland_output_repaint_pixman; |
| 1213 | else |
| 1214 | output->base.repaint = wayland_output_repaint_gl; |
| 1215 | |
| 1216 | output->base.start_repaint_loop = wayland_output_start_repaint_loop; |
Jesse Barnes | 5308a5e | 2012-02-09 13:12:57 -0800 | [diff] [blame] | 1217 | output->base.assign_planes = NULL; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1218 | output->base.set_backlight = NULL; |
| 1219 | output->base.set_dpms = NULL; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1220 | output->base.switch_mode = wayland_output_switch_mode; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1221 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1222 | return 0; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1223 | } |
| 1224 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1225 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1226 | wayland_output_create_for_parent_output(struct wayland_backend *b, |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1227 | struct wayland_parent_output *poutput) |
| 1228 | { |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1229 | struct wayland_output *output = wayland_output_create_common(); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1230 | struct weston_mode *mode; |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1231 | |
| 1232 | output->base.name = strdup("wlparent"); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1233 | |
| 1234 | if (poutput->current_mode) { |
| 1235 | mode = poutput->current_mode; |
| 1236 | } else if (poutput->preferred_mode) { |
U. Artie Eoff | 67072d0 | 2014-05-06 14:50:02 -0700 | [diff] [blame] | 1237 | mode = poutput->preferred_mode; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1238 | } else if (!wl_list_empty(&poutput->mode_list)) { |
| 1239 | mode = container_of(poutput->mode_list.next, |
| 1240 | struct weston_mode, link); |
| 1241 | } else { |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1242 | weston_log("No valid modes found. Skipping output.\n"); |
| 1243 | goto out; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1244 | } |
| 1245 | |
Armin Krezović | 4008740 | 2016-09-30 14:11:12 +0200 | [diff] [blame] | 1246 | weston_output_init(&output->base, b->compositor); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1247 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1248 | output->base.scale = 1; |
| 1249 | output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1250 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1251 | if (wayland_output_set_size(&output->base, mode->width, mode->height) < 0) |
| 1252 | goto out; |
| 1253 | |
| 1254 | output->mode = *mode; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1255 | output->parent.output = poutput->global; |
| 1256 | |
| 1257 | output->base.make = poutput->physical.make; |
| 1258 | output->base.model = poutput->physical.model; |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1259 | |
| 1260 | /* XXX: Clear previously added values */ |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1261 | wl_list_init(&output->base.mode_list); |
| 1262 | wl_list_insert_list(&output->base.mode_list, &poutput->mode_list); |
| 1263 | wl_list_init(&poutput->mode_list); |
| 1264 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1265 | weston_compositor_add_pending_output(&output->base, b->compositor); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1266 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1267 | return 0; |
| 1268 | |
| 1269 | out: |
| 1270 | free(output->name); |
| 1271 | free(output); |
| 1272 | |
| 1273 | return -1; |
| 1274 | } |
| 1275 | |
| 1276 | static int |
| 1277 | wayland_output_create_fullscreen(struct wayland_backend *b) |
| 1278 | { |
| 1279 | struct wayland_output *output = wayland_output_create_common(); |
| 1280 | int width = 0, height = 0; |
| 1281 | |
| 1282 | output->base.name = strdup("wayland-fullscreen"); |
| 1283 | |
Armin Krezović | 4008740 | 2016-09-30 14:11:12 +0200 | [diff] [blame] | 1284 | weston_output_init(&output->base, b->compositor); |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1285 | |
| 1286 | output->base.scale = 1; |
| 1287 | output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL; |
| 1288 | |
| 1289 | if (wayland_backend_create_output_surface(output) < 0) |
| 1290 | goto err_surface; |
| 1291 | |
| 1292 | /* What should size be set if conditional is false? */ |
| 1293 | if (b->parent.shell) { |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1294 | wl_shell_surface_set_fullscreen(output->parent.shell_surface, |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1295 | 0, 0, NULL); |
| 1296 | wl_display_roundtrip(b->parent.wl_display); |
| 1297 | |
| 1298 | width = output->parent.configure_width; |
| 1299 | height = output->parent.configure_height; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1300 | } |
| 1301 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 1302 | if (wayland_output_set_size(&output->base, width, height) < 0) |
| 1303 | goto err_set_size; |
| 1304 | |
| 1305 | weston_compositor_add_pending_output(&output->base, b->compositor); |
| 1306 | |
| 1307 | return 0; |
| 1308 | |
| 1309 | err_set_size: |
| 1310 | wayland_backend_destroy_output_surface(output); |
| 1311 | err_surface: |
| 1312 | free(output->name); |
| 1313 | free(output); |
| 1314 | |
| 1315 | return -1; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1316 | } |
| 1317 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 1318 | static void |
| 1319 | shell_surface_ping(void *data, struct wl_shell_surface *shell_surface, |
| 1320 | uint32_t serial) |
| 1321 | { |
| 1322 | wl_shell_surface_pong(shell_surface, serial); |
| 1323 | } |
| 1324 | |
| 1325 | static void |
| 1326 | shell_surface_configure(void *data, struct wl_shell_surface *shell_surface, |
| 1327 | uint32_t edges, int32_t width, int32_t height) |
| 1328 | { |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1329 | struct wayland_output *output = data; |
| 1330 | |
| 1331 | output->parent.configure_width = width; |
| 1332 | output->parent.configure_height = height; |
| 1333 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 1334 | /* FIXME: implement resizing */ |
| 1335 | } |
| 1336 | |
| 1337 | static void |
| 1338 | shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface) |
| 1339 | { |
| 1340 | } |
| 1341 | |
| 1342 | static const struct wl_shell_surface_listener shell_surface_listener = { |
| 1343 | shell_surface_ping, |
| 1344 | shell_surface_configure, |
| 1345 | shell_surface_popup_done |
| 1346 | }; |
| 1347 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1348 | /* Events received from the wayland-server this compositor is client of: */ |
| 1349 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1350 | /* parent input interface */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1351 | static void |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1352 | input_set_cursor(struct wayland_input *input) |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1353 | { |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1354 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1355 | struct wl_buffer *buffer; |
| 1356 | struct wl_cursor_image *image; |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1357 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1358 | if (!input->backend->cursor) |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1359 | return; /* Couldn't load the cursor. Can't set it */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1360 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1361 | image = input->backend->cursor->images[0]; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1362 | buffer = wl_cursor_image_get_buffer(image); |
Hardening | 842a36a | 2014-03-18 14:12:50 +0100 | [diff] [blame] | 1363 | if (!buffer) |
| 1364 | return; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1365 | |
| 1366 | wl_pointer_set_cursor(input->parent.pointer, input->enter_serial, |
| 1367 | input->parent.cursor.surface, |
| 1368 | image->hotspot_x, image->hotspot_y); |
| 1369 | |
| 1370 | wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0); |
| 1371 | wl_surface_damage(input->parent.cursor.surface, 0, 0, |
| 1372 | image->width, image->height); |
| 1373 | wl_surface_commit(input->parent.cursor.surface); |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1374 | } |
| 1375 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1376 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1377 | input_handle_pointer_enter(void *data, struct wl_pointer *pointer, |
| 1378 | uint32_t serial, struct wl_surface *surface, |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1379 | wl_fixed_t fixed_x, wl_fixed_t fixed_y) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1380 | { |
| 1381 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1382 | int32_t fx, fy; |
| 1383 | enum theme_location location; |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1384 | double x, y; |
| 1385 | |
| 1386 | x = wl_fixed_to_double(fixed_x); |
| 1387 | y = wl_fixed_to_double(fixed_y); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1388 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1389 | /* XXX: If we get a modifier event immediately before the focus, |
| 1390 | * we should try to keep the same serial. */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1391 | input->enter_serial = serial; |
| 1392 | input->output = wl_surface_get_user_data(surface); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1393 | |
| 1394 | if (input->output->frame) { |
| 1395 | location = frame_pointer_enter(input->output->frame, input, |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1396 | x, y); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1397 | frame_interior(input->output->frame, &fx, &fy, NULL, NULL); |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1398 | x -= fx; |
| 1399 | y -= fy; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1400 | |
| 1401 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 1402 | weston_output_schedule_repaint(&input->output->base); |
| 1403 | } else { |
| 1404 | location = THEME_LOCATION_CLIENT_AREA; |
| 1405 | } |
| 1406 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1407 | weston_output_transform_coordinate(&input->output->base, x, y, &x, &y); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1408 | |
| 1409 | if (location == THEME_LOCATION_CLIENT_AREA) { |
Derek Foreman | 4bcc54d | 2015-11-06 15:56:06 -0600 | [diff] [blame] | 1410 | input->has_focus = true; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1411 | notify_pointer_focus(&input->base, &input->output->base, x, y); |
| 1412 | wl_pointer_set_cursor(input->parent.pointer, |
| 1413 | input->enter_serial, NULL, 0, 0); |
| 1414 | } else { |
Derek Foreman | 4bcc54d | 2015-11-06 15:56:06 -0600 | [diff] [blame] | 1415 | input->has_focus = false; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1416 | notify_pointer_focus(&input->base, NULL, 0, 0); |
| 1417 | input_set_cursor(input); |
| 1418 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1422 | input_handle_pointer_leave(void *data, struct wl_pointer *pointer, |
| 1423 | uint32_t serial, struct wl_surface *surface) |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1424 | { |
| 1425 | struct wayland_input *input = data; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1426 | |
Dima Ryazanov | 01d5c02 | 2015-05-18 23:14:16 -0700 | [diff] [blame] | 1427 | if (!input->output) |
| 1428 | return; |
| 1429 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1430 | if (input->output->frame) { |
| 1431 | frame_pointer_leave(input->output->frame, input); |
| 1432 | |
| 1433 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 1434 | weston_output_schedule_repaint(&input->output->base); |
| 1435 | } |
| 1436 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1437 | notify_pointer_focus(&input->base, NULL, 0, 0); |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1438 | input->output = NULL; |
Derek Foreman | 4bcc54d | 2015-11-06 15:56:06 -0600 | [diff] [blame] | 1439 | input->has_focus = false; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1443 | input_handle_motion(void *data, struct wl_pointer *pointer, |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1444 | 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] | 1445 | { |
| 1446 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1447 | int32_t fx, fy; |
| 1448 | enum theme_location location; |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1449 | bool want_frame = false; |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1450 | double x, y; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1451 | |
Dima Ryazanov | 01d5c02 | 2015-05-18 23:14:16 -0700 | [diff] [blame] | 1452 | if (!input->output) |
| 1453 | return; |
| 1454 | |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1455 | x = wl_fixed_to_double(fixed_x); |
| 1456 | y = wl_fixed_to_double(fixed_y); |
| 1457 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1458 | if (input->output->frame) { |
| 1459 | location = frame_pointer_motion(input->output->frame, input, |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1460 | x, y); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1461 | frame_interior(input->output->frame, &fx, &fy, NULL, NULL); |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1462 | x -= fx; |
| 1463 | y -= fy; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1464 | |
| 1465 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 1466 | weston_output_schedule_repaint(&input->output->base); |
| 1467 | } else { |
| 1468 | location = THEME_LOCATION_CLIENT_AREA; |
| 1469 | } |
| 1470 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1471 | weston_output_transform_coordinate(&input->output->base, x, y, &x, &y); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1472 | |
Derek Foreman | 4bcc54d | 2015-11-06 15:56:06 -0600 | [diff] [blame] | 1473 | if (input->has_focus && location != THEME_LOCATION_CLIENT_AREA) { |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1474 | input_set_cursor(input); |
| 1475 | notify_pointer_focus(&input->base, NULL, 0, 0); |
Derek Foreman | 4bcc54d | 2015-11-06 15:56:06 -0600 | [diff] [blame] | 1476 | input->has_focus = false; |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1477 | want_frame = true; |
Derek Foreman | 4bcc54d | 2015-11-06 15:56:06 -0600 | [diff] [blame] | 1478 | } else if (!input->has_focus && |
| 1479 | location == THEME_LOCATION_CLIENT_AREA) { |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1480 | wl_pointer_set_cursor(input->parent.pointer, |
| 1481 | input->enter_serial, NULL, 0, 0); |
| 1482 | notify_pointer_focus(&input->base, &input->output->base, x, y); |
Derek Foreman | 4bcc54d | 2015-11-06 15:56:06 -0600 | [diff] [blame] | 1483 | input->has_focus = true; |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1484 | want_frame = true; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1485 | } |
| 1486 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1487 | if (location == THEME_LOCATION_CLIENT_AREA) { |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1488 | notify_motion_absolute(&input->base, time, x, y); |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1489 | want_frame = true; |
| 1490 | } |
| 1491 | |
| 1492 | if (want_frame && input->seat_version < WL_POINTER_FRAME_SINCE_VERSION) |
| 1493 | notify_pointer_frame(&input->base); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | static void |
| 1497 | input_handle_button(void *data, struct wl_pointer *pointer, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 1498 | uint32_t serial, uint32_t time, uint32_t button, |
Quentin Glidic | d8b17bc | 2016-07-10 11:00:55 +0200 | [diff] [blame] | 1499 | enum wl_pointer_button_state state) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1500 | { |
| 1501 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1502 | enum theme_location location; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1503 | |
Dima Ryazanov | 01d5c02 | 2015-05-18 23:14:16 -0700 | [diff] [blame] | 1504 | if (!input->output) |
| 1505 | return; |
| 1506 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1507 | if (input->output->frame) { |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1508 | location = frame_pointer_button(input->output->frame, input, |
Quentin Glidic | d8b17bc | 2016-07-10 11:00:55 +0200 | [diff] [blame] | 1509 | button, state); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1510 | |
| 1511 | if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) { |
| 1512 | |
| 1513 | wl_shell_surface_move(input->output->parent.shell_surface, |
| 1514 | input->parent.seat, serial); |
| 1515 | frame_status_clear(input->output->frame, |
| 1516 | FRAME_STATUS_MOVE); |
| 1517 | return; |
| 1518 | } |
| 1519 | |
Dima Ryazanov | 01d5c02 | 2015-05-18 23:14:16 -0700 | [diff] [blame] | 1520 | if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) { |
| 1521 | wayland_output_destroy(&input->output->base); |
Dima Ryazanov | b7e70af | 2015-05-20 01:03:53 -0700 | [diff] [blame] | 1522 | input->output = NULL; |
| 1523 | input->keyboard_focus = NULL; |
Dima Ryazanov | 01d5c02 | 2015-05-18 23:14:16 -0700 | [diff] [blame] | 1524 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1525 | if (wl_list_empty(&input->backend->compositor->output_list)) |
Giulio Camuffo | 459137b | 2014-10-11 23:56:24 +0300 | [diff] [blame] | 1526 | weston_compositor_exit(input->backend->compositor); |
Dima Ryazanov | 01d5c02 | 2015-05-18 23:14:16 -0700 | [diff] [blame] | 1527 | |
| 1528 | return; |
| 1529 | } |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1530 | |
| 1531 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 1532 | weston_output_schedule_repaint(&input->output->base); |
| 1533 | } else { |
| 1534 | location = THEME_LOCATION_CLIENT_AREA; |
| 1535 | } |
| 1536 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1537 | if (location == THEME_LOCATION_CLIENT_AREA) { |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1538 | notify_button(&input->base, time, button, state); |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1539 | if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION) |
| 1540 | notify_pointer_frame(&input->base); |
| 1541 | } |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | static void |
| 1545 | input_handle_axis(void *data, struct wl_pointer *pointer, |
Daniel Stone | 2fce402 | 2012-05-30 16:32:00 +0100 | [diff] [blame] | 1546 | uint32_t time, uint32_t axis, wl_fixed_t value) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1547 | { |
| 1548 | struct wayland_input *input = data; |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 1549 | struct weston_pointer_axis_event weston_event; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1550 | |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 1551 | weston_event.axis = axis; |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1552 | weston_event.value = wl_fixed_to_double(value); |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 1553 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1554 | if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL && |
| 1555 | input->vert.has_discrete) { |
| 1556 | weston_event.has_discrete = true; |
| 1557 | weston_event.discrete = input->vert.discrete; |
| 1558 | input->vert.has_discrete = false; |
| 1559 | } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL && |
| 1560 | input->horiz.has_discrete) { |
| 1561 | weston_event.has_discrete = true; |
| 1562 | weston_event.discrete = input->horiz.discrete; |
| 1563 | input->horiz.has_discrete = false; |
| 1564 | } |
| 1565 | |
Peter Hutterer | 89b6a49 | 2016-01-18 15:58:17 +1000 | [diff] [blame] | 1566 | notify_axis(&input->base, time, &weston_event); |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1567 | |
| 1568 | if (input->seat_version < WL_POINTER_FRAME_SINCE_VERSION) |
| 1569 | notify_pointer_frame(&input->base); |
| 1570 | } |
| 1571 | |
| 1572 | static void |
| 1573 | input_handle_frame(void *data, struct wl_pointer *pointer) |
| 1574 | { |
| 1575 | struct wayland_input *input = data; |
| 1576 | |
| 1577 | notify_pointer_frame(&input->base); |
| 1578 | } |
| 1579 | |
| 1580 | static void |
| 1581 | input_handle_axis_source(void *data, struct wl_pointer *pointer, |
| 1582 | uint32_t source) |
| 1583 | { |
| 1584 | struct wayland_input *input = data; |
| 1585 | |
| 1586 | notify_axis_source(&input->base, source); |
| 1587 | } |
| 1588 | |
| 1589 | static void |
| 1590 | input_handle_axis_stop(void *data, struct wl_pointer *pointer, |
| 1591 | uint32_t time, uint32_t axis) |
| 1592 | { |
| 1593 | struct wayland_input *input = data; |
| 1594 | struct weston_pointer_axis_event weston_event; |
| 1595 | |
| 1596 | weston_event.axis = axis; |
| 1597 | weston_event.value = 0; |
| 1598 | |
| 1599 | notify_axis(&input->base, time, &weston_event); |
| 1600 | } |
| 1601 | |
| 1602 | static void |
| 1603 | input_handle_axis_discrete(void *data, struct wl_pointer *pointer, |
| 1604 | uint32_t axis, int32_t discrete) |
| 1605 | { |
| 1606 | struct wayland_input *input = data; |
| 1607 | |
| 1608 | if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) { |
| 1609 | input->vert.has_discrete = true; |
| 1610 | input->vert.discrete = discrete; |
| 1611 | } else if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) { |
| 1612 | input->horiz.has_discrete = true; |
| 1613 | input->horiz.discrete = discrete; |
| 1614 | } |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | static const struct wl_pointer_listener pointer_listener = { |
| 1618 | input_handle_pointer_enter, |
| 1619 | input_handle_pointer_leave, |
| 1620 | input_handle_motion, |
| 1621 | input_handle_button, |
| 1622 | input_handle_axis, |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 1623 | input_handle_frame, |
| 1624 | input_handle_axis_source, |
| 1625 | input_handle_axis_stop, |
| 1626 | input_handle_axis_discrete, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1627 | }; |
| 1628 | |
| 1629 | static void |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1630 | input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, |
| 1631 | int fd, uint32_t size) |
| 1632 | { |
| 1633 | struct wayland_input *input = data; |
| 1634 | struct xkb_keymap *keymap; |
| 1635 | char *map_str; |
| 1636 | |
U. Artie Eoff | d8d4701 | 2014-05-06 14:50:03 -0700 | [diff] [blame] | 1637 | if (!data) { |
| 1638 | close(fd); |
| 1639 | return; |
| 1640 | } |
Jason Ekstrand | b7d9f2e | 2014-04-02 19:53:57 -0500 | [diff] [blame] | 1641 | |
| 1642 | if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { |
| 1643 | map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); |
| 1644 | if (map_str == MAP_FAILED) { |
| 1645 | weston_log("mmap failed: %m\n"); |
| 1646 | goto error; |
| 1647 | } |
| 1648 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1649 | keymap = xkb_keymap_new_from_string(input->backend->compositor->xkb_context, |
Ran Benita | 2e1968f | 2014-08-19 23:59:51 +0300 | [diff] [blame] | 1650 | map_str, |
| 1651 | XKB_KEYMAP_FORMAT_TEXT_V1, |
| 1652 | 0); |
Jason Ekstrand | b7d9f2e | 2014-04-02 19:53:57 -0500 | [diff] [blame] | 1653 | munmap(map_str, size); |
| 1654 | |
| 1655 | if (!keymap) { |
| 1656 | weston_log("failed to compile keymap\n"); |
| 1657 | goto error; |
| 1658 | } |
| 1659 | |
| 1660 | input->keyboard_state_update = STATE_UPDATE_NONE; |
| 1661 | } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) { |
| 1662 | weston_log("No keymap provided; falling back to defalt\n"); |
| 1663 | keymap = NULL; |
| 1664 | input->keyboard_state_update = STATE_UPDATE_AUTOMATIC; |
| 1665 | } else { |
| 1666 | weston_log("Invalid keymap\n"); |
| 1667 | goto error; |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1668 | } |
| 1669 | |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1670 | close(fd); |
| 1671 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1672 | if (weston_seat_get_keyboard(&input->base)) |
Rui Matos | 0c194ce | 2013-10-10 19:44:21 +0200 | [diff] [blame] | 1673 | weston_seat_update_keymap(&input->base, keymap); |
| 1674 | else |
| 1675 | weston_seat_init_keyboard(&input->base, keymap); |
| 1676 | |
Ran Benita | c9c7415 | 2014-08-19 23:59:52 +0300 | [diff] [blame] | 1677 | xkb_keymap_unref(keymap); |
Jason Ekstrand | b7d9f2e | 2014-04-02 19:53:57 -0500 | [diff] [blame] | 1678 | |
| 1679 | return; |
| 1680 | |
| 1681 | error: |
| 1682 | wl_keyboard_release(input->parent.keyboard); |
| 1683 | close(fd); |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | static void |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1687 | input_handle_keyboard_enter(void *data, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1688 | struct wl_keyboard *keyboard, |
| 1689 | uint32_t serial, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1690 | struct wl_surface *surface, |
| 1691 | struct wl_array *keys) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1692 | { |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 1693 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1694 | struct wayland_output *focus; |
| 1695 | |
| 1696 | focus = input->keyboard_focus; |
| 1697 | if (focus) { |
| 1698 | /* This shouldn't happen */ |
| 1699 | focus->keyboard_count--; |
| 1700 | if (!focus->keyboard_count && focus->frame) |
| 1701 | frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE); |
| 1702 | if (frame_status(focus->frame) & FRAME_STATUS_REPAINT) |
| 1703 | weston_output_schedule_repaint(&focus->base); |
| 1704 | } |
| 1705 | |
| 1706 | input->keyboard_focus = wl_surface_get_user_data(surface); |
| 1707 | input->keyboard_focus->keyboard_count++; |
| 1708 | |
| 1709 | focus = input->keyboard_focus; |
| 1710 | if (focus->frame) { |
| 1711 | frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE); |
| 1712 | if (frame_status(focus->frame) & FRAME_STATUS_REPAINT) |
| 1713 | weston_output_schedule_repaint(&focus->base); |
| 1714 | } |
| 1715 | |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 1716 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1717 | /* XXX: If we get a modifier event immediately before the focus, |
| 1718 | * we should try to keep the same serial. */ |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1719 | notify_keyboard_focus_in(&input->base, keys, |
Daniel Stone | d6da09e | 2012-06-22 13:21:29 +0100 | [diff] [blame] | 1720 | STATE_UPDATE_AUTOMATIC); |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | static void |
| 1724 | input_handle_keyboard_leave(void *data, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1725 | struct wl_keyboard *keyboard, |
| 1726 | uint32_t serial, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1727 | struct wl_surface *surface) |
| 1728 | { |
| 1729 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1730 | struct wayland_output *focus; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1731 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1732 | notify_keyboard_focus_out(&input->base); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1733 | |
| 1734 | focus = input->keyboard_focus; |
| 1735 | if (!focus) |
Dima Ryazanov | 01d5c02 | 2015-05-18 23:14:16 -0700 | [diff] [blame] | 1736 | return; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1737 | |
| 1738 | focus->keyboard_count--; |
| 1739 | if (!focus->keyboard_count && focus->frame) { |
| 1740 | frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE); |
| 1741 | if (frame_status(focus->frame) & FRAME_STATUS_REPAINT) |
| 1742 | weston_output_schedule_repaint(&focus->base); |
| 1743 | } |
| 1744 | |
| 1745 | input->keyboard_focus = NULL; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1746 | } |
| 1747 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1748 | static void |
| 1749 | input_handle_key(void *data, struct wl_keyboard *keyboard, |
| 1750 | uint32_t serial, uint32_t time, uint32_t key, uint32_t state) |
| 1751 | { |
| 1752 | struct wayland_input *input = data; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1753 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1754 | input->key_serial = serial; |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1755 | notify_key(&input->base, time, key, |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 1756 | state ? WL_KEYBOARD_KEY_STATE_PRESSED : |
Daniel Stone | 1b4e11f | 2012-06-22 13:21:37 +0100 | [diff] [blame] | 1757 | WL_KEYBOARD_KEY_STATE_RELEASED, |
Jason Ekstrand | b7d9f2e | 2014-04-02 19:53:57 -0500 | [diff] [blame] | 1758 | input->keyboard_state_update); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1759 | } |
| 1760 | |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1761 | static void |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1762 | input_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1763 | uint32_t serial_in, uint32_t mods_depressed, |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1764 | uint32_t mods_latched, uint32_t mods_locked, |
| 1765 | uint32_t group) |
| 1766 | { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1767 | struct weston_keyboard *keyboard; |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1768 | struct wayland_input *input = data; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1769 | struct wayland_backend *b = input->backend; |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1770 | uint32_t serial_out; |
| 1771 | |
| 1772 | /* If we get a key event followed by a modifier event with the |
| 1773 | * same serial number, then we try to preserve those semantics by |
| 1774 | * reusing the same serial number on the way out too. */ |
| 1775 | if (serial_in == input->key_serial) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1776 | serial_out = wl_display_get_serial(b->compositor->wl_display); |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1777 | else |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1778 | serial_out = wl_display_next_serial(b->compositor->wl_display); |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1779 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 1780 | keyboard = weston_seat_get_keyboard(&input->base); |
| 1781 | xkb_state_update_mask(keyboard->xkb_state.state, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1782 | mods_depressed, mods_latched, |
| 1783 | mods_locked, 0, 0, group); |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1784 | notify_modifiers(&input->base, serial_out); |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1785 | } |
| 1786 | |
Jonny Lamb | 497994a | 2014-08-12 14:58:26 +0200 | [diff] [blame] | 1787 | static void |
| 1788 | input_handle_repeat_info(void *data, struct wl_keyboard *keyboard, |
| 1789 | int32_t rate, int32_t delay) |
| 1790 | { |
| 1791 | struct wayland_input *input = data; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1792 | struct wayland_backend *b = input->backend; |
Jonny Lamb | 497994a | 2014-08-12 14:58:26 +0200 | [diff] [blame] | 1793 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1794 | b->compositor->kb_repeat_rate = rate; |
| 1795 | b->compositor->kb_repeat_delay = delay; |
Jonny Lamb | 497994a | 2014-08-12 14:58:26 +0200 | [diff] [blame] | 1796 | } |
| 1797 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1798 | static const struct wl_keyboard_listener keyboard_listener = { |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1799 | input_handle_keymap, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1800 | input_handle_keyboard_enter, |
| 1801 | input_handle_keyboard_leave, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1802 | input_handle_key, |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1803 | input_handle_modifiers, |
Jonny Lamb | 497994a | 2014-08-12 14:58:26 +0200 | [diff] [blame] | 1804 | input_handle_repeat_info, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1805 | }; |
| 1806 | |
| 1807 | static void |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 1808 | input_handle_touch_down(void *data, struct wl_touch *wl_touch, |
| 1809 | uint32_t serial, uint32_t time, |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1810 | struct wl_surface *surface, int32_t id, |
| 1811 | wl_fixed_t fixed_x, wl_fixed_t fixed_y) |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 1812 | { |
| 1813 | struct wayland_input *input = data; |
| 1814 | struct wayland_output *output; |
| 1815 | enum theme_location location; |
| 1816 | bool first_touch; |
| 1817 | int32_t fx, fy; |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1818 | double x, y; |
| 1819 | |
| 1820 | x = wl_fixed_to_double(fixed_x); |
| 1821 | y = wl_fixed_to_double(fixed_y); |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 1822 | |
| 1823 | first_touch = (input->touch_points == 0); |
| 1824 | input->touch_points++; |
| 1825 | |
| 1826 | input->touch_focus = wl_surface_get_user_data(surface); |
| 1827 | output = input->touch_focus; |
| 1828 | if (!first_touch && !input->touch_active) |
| 1829 | return; |
| 1830 | |
| 1831 | if (output->frame) { |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1832 | location = frame_touch_down(output->frame, input, id, x, y); |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 1833 | |
| 1834 | frame_interior(output->frame, &fx, &fy, NULL, NULL); |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1835 | x -= fx; |
| 1836 | y -= fy; |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 1837 | |
| 1838 | if (frame_status(output->frame) & FRAME_STATUS_REPAINT) |
| 1839 | weston_output_schedule_repaint(&output->base); |
| 1840 | |
| 1841 | if (first_touch && (frame_status(output->frame) & FRAME_STATUS_MOVE)) { |
| 1842 | input->touch_points--; |
| 1843 | wl_shell_surface_move(output->parent.shell_surface, |
| 1844 | input->parent.seat, serial); |
| 1845 | frame_status_clear(output->frame, |
| 1846 | FRAME_STATUS_MOVE); |
| 1847 | return; |
| 1848 | } |
| 1849 | |
| 1850 | if (first_touch && location != THEME_LOCATION_CLIENT_AREA) |
| 1851 | return; |
| 1852 | } |
| 1853 | |
| 1854 | weston_output_transform_coordinate(&output->base, x, y, &x, &y); |
| 1855 | |
| 1856 | notify_touch(&input->base, time, id, x, y, WL_TOUCH_DOWN); |
| 1857 | input->touch_active = true; |
| 1858 | } |
| 1859 | |
| 1860 | static void |
| 1861 | input_handle_touch_up(void *data, struct wl_touch *wl_touch, |
| 1862 | uint32_t serial, uint32_t time, int32_t id) |
| 1863 | { |
| 1864 | struct wayland_input *input = data; |
| 1865 | struct wayland_output *output = input->touch_focus; |
| 1866 | bool active = input->touch_active; |
| 1867 | |
| 1868 | input->touch_points--; |
| 1869 | if (input->touch_points == 0) { |
| 1870 | input->touch_focus = NULL; |
| 1871 | input->touch_active = false; |
| 1872 | } |
| 1873 | |
| 1874 | if (!output) |
| 1875 | return; |
| 1876 | |
| 1877 | if (output->frame) { |
| 1878 | frame_touch_up(output->frame, input, id); |
| 1879 | |
| 1880 | if (frame_status(output->frame) & FRAME_STATUS_CLOSE) { |
| 1881 | wayland_output_destroy(&output->base); |
| 1882 | input->touch_focus = NULL; |
| 1883 | input->keyboard_focus = NULL; |
| 1884 | if (wl_list_empty(&input->backend->compositor->output_list)) |
| 1885 | weston_compositor_exit(input->backend->compositor); |
| 1886 | |
| 1887 | return; |
| 1888 | } |
| 1889 | if (frame_status(output->frame) & FRAME_STATUS_REPAINT) |
| 1890 | weston_output_schedule_repaint(&output->base); |
| 1891 | } |
| 1892 | |
| 1893 | if (active) |
| 1894 | notify_touch(&input->base, time, id, 0, 0, WL_TOUCH_UP); |
| 1895 | } |
| 1896 | |
| 1897 | static void |
| 1898 | input_handle_touch_motion(void *data, struct wl_touch *wl_touch, |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1899 | uint32_t time, int32_t id, |
| 1900 | wl_fixed_t fixed_x, wl_fixed_t fixed_y) |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 1901 | { |
| 1902 | struct wayland_input *input = data; |
| 1903 | struct wayland_output *output = input->touch_focus; |
| 1904 | int32_t fx, fy; |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1905 | double x, y; |
| 1906 | |
| 1907 | x = wl_fixed_to_double(fixed_x); |
| 1908 | y = wl_fixed_to_double(fixed_y); |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 1909 | |
| 1910 | if (!output || !input->touch_active) |
| 1911 | return; |
| 1912 | |
| 1913 | if (output->frame) { |
| 1914 | frame_interior(output->frame, &fx, &fy, NULL, NULL); |
Giulio Camuffo | 90a6fc6 | 2016-03-22 17:44:54 +0200 | [diff] [blame] | 1915 | x -= fx; |
| 1916 | y -= fy; |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | weston_output_transform_coordinate(&output->base, x, y, &x, &y); |
| 1920 | |
| 1921 | notify_touch(&input->base, time, id, x, y, WL_TOUCH_MOTION); |
| 1922 | } |
| 1923 | |
| 1924 | static void |
| 1925 | input_handle_touch_frame(void *data, struct wl_touch *wl_touch) |
| 1926 | { |
| 1927 | struct wayland_input *input = data; |
| 1928 | |
| 1929 | if (!input->touch_focus || !input->touch_active) |
| 1930 | return; |
| 1931 | |
| 1932 | notify_touch_frame(&input->base); |
| 1933 | } |
| 1934 | |
| 1935 | static void |
| 1936 | input_handle_touch_cancel(void *data, struct wl_touch *wl_touch) |
| 1937 | { |
| 1938 | struct wayland_input *input = data; |
| 1939 | |
| 1940 | if (!input->touch_focus || !input->touch_active) |
| 1941 | return; |
| 1942 | |
| 1943 | notify_touch_cancel(&input->base); |
| 1944 | } |
| 1945 | |
| 1946 | static const struct wl_touch_listener touch_listener = { |
| 1947 | input_handle_touch_down, |
| 1948 | input_handle_touch_up, |
| 1949 | input_handle_touch_motion, |
| 1950 | input_handle_touch_frame, |
| 1951 | input_handle_touch_cancel, |
| 1952 | }; |
| 1953 | |
| 1954 | |
| 1955 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1956 | input_handle_capabilities(void *data, struct wl_seat *seat, |
| 1957 | enum wl_seat_capability caps) |
| 1958 | { |
| 1959 | struct wayland_input *input = data; |
| 1960 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1961 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) { |
| 1962 | input->parent.pointer = wl_seat_get_pointer(seat); |
| 1963 | wl_pointer_set_user_data(input->parent.pointer, input); |
| 1964 | wl_pointer_add_listener(input->parent.pointer, |
| 1965 | &pointer_listener, input); |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 1966 | weston_seat_init_pointer(&input->base); |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1967 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) { |
Derek Foreman | cfce7d0 | 2015-11-06 15:56:08 -0600 | [diff] [blame] | 1968 | if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION) |
| 1969 | wl_pointer_release(input->parent.pointer); |
| 1970 | else |
| 1971 | wl_pointer_destroy(input->parent.pointer); |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1972 | input->parent.pointer = NULL; |
Derek Foreman | cfce7d0 | 2015-11-06 15:56:08 -0600 | [diff] [blame] | 1973 | weston_seat_release_pointer(&input->base); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1974 | } |
| 1975 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1976 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) { |
| 1977 | input->parent.keyboard = wl_seat_get_keyboard(seat); |
| 1978 | wl_keyboard_set_user_data(input->parent.keyboard, input); |
| 1979 | wl_keyboard_add_listener(input->parent.keyboard, |
| 1980 | &keyboard_listener, input); |
| 1981 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) { |
Derek Foreman | cfce7d0 | 2015-11-06 15:56:08 -0600 | [diff] [blame] | 1982 | if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION) |
| 1983 | wl_keyboard_release(input->parent.keyboard); |
| 1984 | else |
| 1985 | wl_keyboard_destroy(input->parent.keyboard); |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1986 | input->parent.keyboard = NULL; |
Derek Foreman | cfce7d0 | 2015-11-06 15:56:08 -0600 | [diff] [blame] | 1987 | weston_seat_release_keyboard(&input->base); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1988 | } |
Derek Foreman | 748c695 | 2015-11-06 15:56:10 -0600 | [diff] [blame] | 1989 | |
| 1990 | if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->parent.touch) { |
| 1991 | input->parent.touch = wl_seat_get_touch(seat); |
| 1992 | wl_touch_set_user_data(input->parent.touch, input); |
| 1993 | wl_touch_add_listener(input->parent.touch, |
| 1994 | &touch_listener, input); |
| 1995 | weston_seat_init_touch(&input->base); |
| 1996 | } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->parent.touch) { |
| 1997 | if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION) |
| 1998 | wl_touch_release(input->parent.touch); |
| 1999 | else |
| 2000 | wl_touch_destroy(input->parent.touch); |
| 2001 | input->parent.touch = NULL; |
| 2002 | weston_seat_release_touch(&input->base); |
| 2003 | } |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 2004 | } |
| 2005 | |
Jonny Lamb | 497994a | 2014-08-12 14:58:26 +0200 | [diff] [blame] | 2006 | static void |
| 2007 | input_handle_name(void *data, struct wl_seat *seat, |
| 2008 | const char *name) |
| 2009 | { |
| 2010 | } |
| 2011 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 2012 | static const struct wl_seat_listener seat_listener = { |
| 2013 | input_handle_capabilities, |
Jonny Lamb | 497994a | 2014-08-12 14:58:26 +0200 | [diff] [blame] | 2014 | input_handle_name, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 2015 | }; |
| 2016 | |
| 2017 | static void |
Derek Foreman | cfce7d0 | 2015-11-06 15:56:08 -0600 | [diff] [blame] | 2018 | 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] | 2019 | { |
| 2020 | struct wayland_input *input; |
Derek Foreman | cfce7d0 | 2015-11-06 15:56:08 -0600 | [diff] [blame] | 2021 | uint32_t version = MIN(available_version, 4); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2022 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 2023 | input = zalloc(sizeof *input); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2024 | if (input == NULL) |
| 2025 | return; |
| 2026 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2027 | weston_seat_init(&input->base, b->compositor, "default"); |
| 2028 | input->backend = b; |
| 2029 | input->parent.seat = wl_registry_bind(b->parent.registry, id, |
Derek Foreman | cfce7d0 | 2015-11-06 15:56:08 -0600 | [diff] [blame] | 2030 | &wl_seat_interface, version); |
| 2031 | input->seat_version = version; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2032 | wl_list_insert(b->input_list.prev, &input->link); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2033 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 2034 | wl_seat_add_listener(input->parent.seat, &seat_listener, input); |
| 2035 | wl_seat_set_user_data(input->parent.seat, input); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 2036 | |
| 2037 | input->parent.cursor.surface = |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2038 | wl_compositor_create_surface(b->parent.compositor); |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 2039 | |
| 2040 | input->vert.axis = WL_POINTER_AXIS_VERTICAL_SCROLL; |
| 2041 | input->horiz.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | static void |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2045 | wayland_parent_output_geometry(void *data, struct wl_output *output_proxy, |
| 2046 | int32_t x, int32_t y, |
| 2047 | int32_t physical_width, int32_t physical_height, |
| 2048 | int32_t subpixel, const char *make, |
| 2049 | const char *model, int32_t transform) |
| 2050 | { |
| 2051 | struct wayland_parent_output *output = data; |
| 2052 | |
| 2053 | output->x = x; |
| 2054 | output->y = y; |
| 2055 | output->physical.width = physical_width; |
| 2056 | output->physical.height = physical_height; |
| 2057 | output->physical.subpixel = subpixel; |
| 2058 | |
| 2059 | free(output->physical.make); |
| 2060 | output->physical.make = strdup(make); |
| 2061 | free(output->physical.model); |
| 2062 | output->physical.model = strdup(model); |
| 2063 | |
| 2064 | output->transform = transform; |
| 2065 | } |
| 2066 | |
| 2067 | static struct weston_mode * |
| 2068 | find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh) |
| 2069 | { |
| 2070 | struct weston_mode *mode; |
| 2071 | |
| 2072 | wl_list_for_each(mode, list, link) { |
| 2073 | if (mode->width == width && mode->height == height && |
| 2074 | mode->refresh == refresh) |
| 2075 | return mode; |
| 2076 | } |
| 2077 | |
| 2078 | mode = zalloc(sizeof *mode); |
| 2079 | if (!mode) |
| 2080 | return NULL; |
| 2081 | |
| 2082 | mode->width = width; |
| 2083 | mode->height = height; |
| 2084 | mode->refresh = refresh; |
| 2085 | wl_list_insert(list, &mode->link); |
| 2086 | |
| 2087 | return mode; |
| 2088 | } |
| 2089 | |
| 2090 | static void |
| 2091 | wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy, |
| 2092 | uint32_t flags, int32_t width, int32_t height, |
| 2093 | int32_t refresh) |
| 2094 | { |
| 2095 | struct wayland_parent_output *output = data; |
| 2096 | struct weston_mode *mode; |
| 2097 | |
| 2098 | if (output->output) { |
| 2099 | mode = find_mode(&output->output->base.mode_list, |
| 2100 | width, height, refresh); |
| 2101 | if (!mode) |
| 2102 | return; |
| 2103 | mode->flags = flags; |
| 2104 | /* Do a mode-switch on current mode change? */ |
| 2105 | } else { |
| 2106 | mode = find_mode(&output->mode_list, width, height, refresh); |
| 2107 | if (!mode) |
| 2108 | return; |
| 2109 | mode->flags = flags; |
| 2110 | if (flags & WL_OUTPUT_MODE_CURRENT) |
| 2111 | output->current_mode = mode; |
| 2112 | if (flags & WL_OUTPUT_MODE_PREFERRED) |
| 2113 | output->preferred_mode = mode; |
| 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | static const struct wl_output_listener output_listener = { |
| 2118 | wayland_parent_output_geometry, |
| 2119 | wayland_parent_output_mode |
| 2120 | }; |
| 2121 | |
| 2122 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2123 | wayland_backend_register_output(struct wayland_backend *b, uint32_t id) |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2124 | { |
| 2125 | struct wayland_parent_output *output; |
| 2126 | |
| 2127 | output = zalloc(sizeof *output); |
| 2128 | if (!output) |
| 2129 | return; |
| 2130 | |
| 2131 | output->id = id; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2132 | output->global = wl_registry_bind(b->parent.registry, id, |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2133 | &wl_output_interface, 1); |
U. Artie Eoff | 8cbd8f3 | 2014-05-06 14:50:01 -0700 | [diff] [blame] | 2134 | if (!output->global) { |
| 2135 | free(output); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2136 | return; |
U. Artie Eoff | 8cbd8f3 | 2014-05-06 14:50:01 -0700 | [diff] [blame] | 2137 | } |
| 2138 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2139 | wl_output_add_listener(output->global, &output_listener, output); |
| 2140 | |
| 2141 | output->scale = 0; |
| 2142 | output->transform = WL_OUTPUT_TRANSFORM_NORMAL; |
| 2143 | output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 2144 | wl_list_init(&output->mode_list); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2145 | wl_list_insert(&b->parent.output_list, &output->link); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2146 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2147 | if (b->sprawl_across_outputs) { |
| 2148 | wl_display_roundtrip(b->parent.wl_display); |
| 2149 | wayland_output_create_for_parent_output(b, output); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2150 | } |
| 2151 | } |
| 2152 | |
| 2153 | static void |
| 2154 | wayland_parent_output_destroy(struct wayland_parent_output *output) |
| 2155 | { |
| 2156 | struct weston_mode *mode, *next; |
| 2157 | |
| 2158 | if (output->output) |
| 2159 | wayland_output_destroy(&output->output->base); |
| 2160 | |
| 2161 | wl_output_destroy(output->global); |
| 2162 | free(output->physical.make); |
| 2163 | free(output->physical.model); |
| 2164 | |
| 2165 | wl_list_for_each_safe(mode, next, &output->mode_list, link) { |
| 2166 | wl_list_remove(&mode->link); |
| 2167 | free(mode); |
| 2168 | } |
| 2169 | } |
| 2170 | |
| 2171 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 2172 | registry_handle_global(void *data, struct wl_registry *registry, uint32_t name, |
| 2173 | const char *interface, uint32_t version) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2174 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2175 | struct wayland_backend *b = data; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2176 | |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 2177 | if (strcmp(interface, "wl_compositor") == 0) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2178 | b->parent.compositor = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 2179 | wl_registry_bind(registry, name, |
| 2180 | &wl_compositor_interface, 1); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 2181 | } else if (strcmp(interface, "wl_shell") == 0) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2182 | b->parent.shell = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 2183 | wl_registry_bind(registry, name, |
| 2184 | &wl_shell_interface, 1); |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 2185 | } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2186 | b->parent.fshell = |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2187 | wl_registry_bind(registry, name, |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 2188 | &zwp_fullscreen_shell_v1_interface, 1); |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 2189 | } else if (strcmp(interface, "wl_seat") == 0) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2190 | display_add_seat(b, name, version); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2191 | } else if (strcmp(interface, "wl_output") == 0) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2192 | wayland_backend_register_output(b, name); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2193 | } else if (strcmp(interface, "wl_shm") == 0) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2194 | b->parent.shm = |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2195 | wl_registry_bind(registry, name, &wl_shm_interface, 1); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2196 | } |
| 2197 | } |
| 2198 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2199 | static void |
| 2200 | registry_handle_global_remove(void *data, struct wl_registry *registry, |
| 2201 | uint32_t name) |
| 2202 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2203 | struct wayland_backend *b = data; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2204 | struct wayland_parent_output *output; |
| 2205 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2206 | wl_list_for_each(output, &b->parent.output_list, link) |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2207 | if (output->id == name) |
| 2208 | wayland_parent_output_destroy(output); |
| 2209 | } |
| 2210 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 2211 | static const struct wl_registry_listener registry_listener = { |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2212 | registry_handle_global, |
| 2213 | registry_handle_global_remove |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 2214 | }; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2215 | |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 2216 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2217 | wayland_backend_handle_event(int fd, uint32_t mask, void *data) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2218 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2219 | struct wayland_backend *b = data; |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 2220 | int count = 0; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2221 | |
Kristian Høgsberg | 453de7a | 2013-10-30 23:15:44 -0700 | [diff] [blame] | 2222 | if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) { |
Giulio Camuffo | 459137b | 2014-10-11 23:56:24 +0300 | [diff] [blame] | 2223 | weston_compositor_exit(b->compositor); |
Kristian Høgsberg | 453de7a | 2013-10-30 23:15:44 -0700 | [diff] [blame] | 2224 | return 0; |
| 2225 | } |
| 2226 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2227 | if (mask & WL_EVENT_READABLE) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2228 | count = wl_display_dispatch(b->parent.wl_display); |
Kristian Høgsberg | f258a31 | 2011-12-28 22:51:20 -0500 | [diff] [blame] | 2229 | if (mask & WL_EVENT_WRITABLE) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2230 | wl_display_flush(b->parent.wl_display); |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 2231 | |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 2232 | if (mask == 0) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2233 | count = wl_display_dispatch_pending(b->parent.wl_display); |
| 2234 | wl_display_flush(b->parent.wl_display); |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | return count; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2238 | } |
| 2239 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2240 | static void |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 2241 | wayland_restore(struct weston_compositor *ec) |
| 2242 | { |
| 2243 | } |
| 2244 | |
| 2245 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 2246 | wayland_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2247 | { |
Armin Krezović | 938dc52 | 2016-08-01 19:17:57 +0200 | [diff] [blame] | 2248 | struct wayland_backend *b = to_wayland_backend(ec); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2249 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 2250 | weston_compositor_shutdown(ec); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 2251 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2252 | if (b->parent.shm) |
| 2253 | wl_shm_destroy(b->parent.shm); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2254 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2255 | free(b); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2256 | } |
| 2257 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 2258 | static const char *left_ptrs[] = { |
| 2259 | "left_ptr", |
| 2260 | "default", |
| 2261 | "top_left_arrow", |
| 2262 | "left-arrow" |
| 2263 | }; |
| 2264 | |
| 2265 | static void |
Benoit Gschwind | 244ff79 | 2016-04-28 20:33:11 +0200 | [diff] [blame] | 2266 | create_cursor(struct wayland_backend *b, |
| 2267 | struct weston_wayland_backend_config *config) |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 2268 | { |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 2269 | unsigned int i; |
| 2270 | |
Benoit Gschwind | 244ff79 | 2016-04-28 20:33:11 +0200 | [diff] [blame] | 2271 | b->cursor_theme = wl_cursor_theme_load(config->cursor_theme, |
| 2272 | config->cursor_size, |
| 2273 | b->parent.shm); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2274 | if (!b->cursor_theme) { |
Hardening | 842a36a | 2014-03-18 14:12:50 +0100 | [diff] [blame] | 2275 | fprintf(stderr, "could not load cursor theme\n"); |
| 2276 | return; |
| 2277 | } |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 2278 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2279 | b->cursor = NULL; |
| 2280 | for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i) |
| 2281 | b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme, |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 2282 | left_ptrs[i]); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2283 | if (!b->cursor) { |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 2284 | fprintf(stderr, "could not load left cursor\n"); |
| 2285 | return; |
| 2286 | } |
| 2287 | } |
| 2288 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 2289 | static void |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 2290 | fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time, |
| 2291 | uint32_t key, void *data) |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 2292 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2293 | struct wayland_backend *b = data; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 2294 | struct wayland_input *input = NULL; |
| 2295 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2296 | wl_list_for_each(input, &b->input_list, link) |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 2297 | if (&input->base == keyboard->seat) |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 2298 | break; |
| 2299 | |
| 2300 | if (!input || !input->output) |
| 2301 | return; |
| 2302 | |
| 2303 | if (input->output->frame) |
| 2304 | wayland_output_set_fullscreen(input->output, 0, 0, NULL); |
| 2305 | else |
| 2306 | wayland_output_set_windowed(input->output); |
| 2307 | |
| 2308 | weston_output_schedule_repaint(&input->output->base); |
| 2309 | } |
| 2310 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2311 | static struct wayland_backend * |
Benoit Gschwind | 3a49b51 | 2016-04-28 20:33:10 +0200 | [diff] [blame] | 2312 | wayland_backend_create(struct weston_compositor *compositor, |
Pekka Paalanen | a256c5e | 2016-06-03 14:56:18 +0300 | [diff] [blame] | 2313 | struct weston_wayland_backend_config *new_config) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2314 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2315 | struct wayland_backend *b; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2316 | struct wl_event_loop *loop; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2317 | int fd; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2318 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2319 | b = zalloc(sizeof *b); |
| 2320 | if (b == NULL) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2321 | return NULL; |
| 2322 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2323 | b->compositor = compositor; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2324 | if (weston_compositor_set_presentation_clock_software(compositor) < 0) |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 2325 | goto err_compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2326 | |
Benoit Gschwind | 3a49b51 | 2016-04-28 20:33:10 +0200 | [diff] [blame] | 2327 | b->parent.wl_display = wl_display_connect(new_config->display_name); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2328 | if (b->parent.wl_display == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2329 | weston_log("failed to create display: %m\n"); |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 2330 | goto err_compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2331 | } |
| 2332 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2333 | wl_list_init(&b->parent.output_list); |
| 2334 | wl_list_init(&b->input_list); |
| 2335 | b->parent.registry = wl_display_get_registry(b->parent.wl_display); |
| 2336 | wl_registry_add_listener(b->parent.registry, ®istry_listener, b); |
| 2337 | wl_display_roundtrip(b->parent.wl_display); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 2338 | |
Benoit Gschwind | 244ff79 | 2016-04-28 20:33:11 +0200 | [diff] [blame] | 2339 | create_cursor(b, new_config); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2340 | |
Benoit Gschwind | 3a49b51 | 2016-04-28 20:33:10 +0200 | [diff] [blame] | 2341 | b->use_pixman = new_config->use_pixman; |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 2342 | b->fullscreen = new_config->fullscreen; |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 2343 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2344 | if (!b->use_pixman) { |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 2345 | gl_renderer = weston_load_module("gl-renderer.so", |
| 2346 | "gl_renderer_interface"); |
| 2347 | if (!gl_renderer) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2348 | b->use_pixman = 1; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 2349 | } |
| 2350 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2351 | if (!b->use_pixman) { |
| 2352 | if (gl_renderer->create(compositor, |
Derek Foreman | e76f185 | 2015-05-15 12:12:39 -0500 | [diff] [blame] | 2353 | EGL_PLATFORM_WAYLAND_KHR, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2354 | b->parent.wl_display, |
Derek Foreman | e76f185 | 2015-05-15 12:12:39 -0500 | [diff] [blame] | 2355 | gl_renderer->alpha_attribs, |
| 2356 | NULL, |
| 2357 | 0) < 0) { |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 2358 | weston_log("Failed to initialize the GL renderer; " |
| 2359 | "falling back to pixman.\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2360 | b->use_pixman = 1; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 2361 | } |
| 2362 | } |
| 2363 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2364 | if (b->use_pixman) { |
| 2365 | if (pixman_renderer_init(compositor) < 0) { |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 2366 | weston_log("Failed to initialize pixman renderer\n"); |
| 2367 | goto err_display; |
| 2368 | } |
| 2369 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2370 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2371 | b->base.destroy = wayland_destroy; |
| 2372 | b->base.restore = wayland_restore; |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 2373 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2374 | loop = wl_display_get_event_loop(compositor->wl_display); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2375 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2376 | fd = wl_display_get_fd(b->parent.wl_display); |
| 2377 | b->parent.wl_source = |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2378 | wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2379 | wayland_backend_handle_event, b); |
| 2380 | if (b->parent.wl_source == NULL) |
Dawid Gajownik | 82d4925 | 2015-07-31 00:02:28 -0300 | [diff] [blame] | 2381 | goto err_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 | wl_event_source_check(b->parent.wl_source); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2384 | |
Emmanuel Gil Peyrot | c59f18e | 2015-09-25 11:58:40 +0200 | [diff] [blame] | 2385 | if (compositor->renderer->import_dmabuf) { |
| 2386 | if (linux_dmabuf_setup(compositor) < 0) |
| 2387 | weston_log("Error: initializing dmabuf " |
| 2388 | "support failed.\n"); |
| 2389 | } |
| 2390 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2391 | compositor->backend = &b->base; |
| 2392 | return b; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2393 | err_display: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2394 | wl_display_disconnect(b->parent.wl_display); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2395 | err_compositor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2396 | weston_compositor_shutdown(compositor); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2397 | free(b); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2398 | return NULL; |
| 2399 | } |
| 2400 | |
| 2401 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2402 | wayland_backend_destroy(struct wayland_backend *b) |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2403 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2404 | wl_display_disconnect(b->parent.wl_display); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2405 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2406 | if (b->theme) |
| 2407 | theme_destroy(b->theme); |
| 2408 | if (b->frame_device) |
| 2409 | cairo_device_destroy(b->frame_device); |
| 2410 | wl_cursor_theme_destroy(b->cursor_theme); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2411 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2412 | weston_compositor_shutdown(b->compositor); |
| 2413 | free(b); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2414 | } |
| 2415 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 2416 | static const struct weston_windowed_output_api windowed_api = { |
| 2417 | wayland_output_set_size, |
| 2418 | wayland_output_create, |
| 2419 | }; |
| 2420 | |
Benoit Gschwind | cd63b3e | 2016-04-28 20:33:14 +0200 | [diff] [blame] | 2421 | static void |
Benoit Gschwind | e091b45 | 2016-05-10 22:47:48 +0200 | [diff] [blame] | 2422 | config_init_to_defaults(struct weston_wayland_backend_config *config) |
| 2423 | { |
| 2424 | } |
| 2425 | |
Benoit Gschwind | cd63b3e | 2016-04-28 20:33:14 +0200 | [diff] [blame] | 2426 | WL_EXPORT int |
Pekka Paalanen | a256c5e | 2016-06-03 14:56:18 +0300 | [diff] [blame] | 2427 | backend_init(struct weston_compositor *compositor, |
Benoit Gschwind | cd63b3e | 2016-04-28 20:33:14 +0200 | [diff] [blame] | 2428 | struct weston_backend_config *config_base) |
| 2429 | { |
| 2430 | struct wayland_backend *b; |
Benoit Gschwind | cd63b3e | 2016-04-28 20:33:14 +0200 | [diff] [blame] | 2431 | struct wayland_parent_output *poutput; |
| 2432 | struct weston_wayland_backend_config new_config; |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 2433 | int ret; |
Benoit Gschwind | cd63b3e | 2016-04-28 20:33:14 +0200 | [diff] [blame] | 2434 | |
Benoit Gschwind | e091b45 | 2016-05-10 22:47:48 +0200 | [diff] [blame] | 2435 | if (config_base == NULL || |
| 2436 | config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION || |
| 2437 | config_base->struct_size > sizeof(struct weston_wayland_backend_config)) { |
| 2438 | weston_log("wayland backend config structure is invalid\n"); |
Benoit Gschwind | e091b45 | 2016-05-10 22:47:48 +0200 | [diff] [blame] | 2439 | return -1; |
| 2440 | } |
| 2441 | |
| 2442 | config_init_to_defaults(&new_config); |
| 2443 | memcpy(&new_config, config_base, config_base->struct_size); |
| 2444 | |
Pekka Paalanen | a256c5e | 2016-06-03 14:56:18 +0300 | [diff] [blame] | 2445 | b = wayland_backend_create(compositor, &new_config); |
Benoit Gschwind | 37a6807 | 2016-04-28 20:33:08 +0200 | [diff] [blame] | 2446 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2447 | if (!b) |
| 2448 | return -1; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2449 | |
Benoit Gschwind | 37a6807 | 2016-04-28 20:33:08 +0200 | [diff] [blame] | 2450 | if (new_config.sprawl || b->parent.fshell) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2451 | b->sprawl_across_outputs = 1; |
| 2452 | wl_display_roundtrip(b->parent.wl_display); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2453 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2454 | wl_list_for_each(poutput, &b->parent.output_list, link) |
| 2455 | wayland_output_create_for_parent_output(b, poutput); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2456 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2457 | return 0; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2458 | } |
| 2459 | |
Benoit Gschwind | 37a6807 | 2016-04-28 20:33:08 +0200 | [diff] [blame] | 2460 | if (new_config.fullscreen) { |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 2461 | if (wayland_output_create_fullscreen(b) < 0) { |
| 2462 | weston_log("Unable to create a fullscreen output.\n"); |
Benoit Gschwind | cd63b3e | 2016-04-28 20:33:14 +0200 | [diff] [blame] | 2463 | goto err_outputs; |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 2464 | } |
Benoit Gschwind | 830b788 | 2016-04-28 20:33:12 +0200 | [diff] [blame] | 2465 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2466 | return 0; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 2467 | } |
| 2468 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 2469 | ret = weston_plugin_api_register(compositor, WESTON_WINDOWED_OUTPUT_API_NAME, |
| 2470 | &windowed_api, sizeof(windowed_api)); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2471 | |
Armin Krezović | 174448a | 2016-09-30 14:11:09 +0200 | [diff] [blame] | 2472 | if (ret < 0) { |
| 2473 | weston_log("Failed to register output API.\n"); |
| 2474 | wayland_backend_destroy(b); |
| 2475 | return -1; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 2476 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2477 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2478 | weston_compositor_add_key_binding(compositor, KEY_F, |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2479 | MODIFIER_CTRL | MODIFIER_ALT, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2480 | fullscreen_binding, b); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2481 | return 0; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2482 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 2483 | err_outputs: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2484 | wayland_backend_destroy(b); |
| 2485 | return -1; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2486 | } |