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 | * |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 5 | * Permission to use, copy, modify, distribute, and sell this software and |
| 6 | * its documentation for any purpose is hereby granted without fee, provided |
| 7 | * that the above copyright notice appear in all copies and that both that |
| 8 | * copyright notice and this permission notice appear in supporting |
| 9 | * documentation, and that the name of the copyright holders not be used in |
| 10 | * advertising or publicity pertaining to distribution of the software |
| 11 | * without specific, written prior permission. The copyright holders make |
| 12 | * no representations about the suitability of this software for any |
| 13 | * purpose. It is provided "as is" without express or implied warranty. |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 14 | * |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 16 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 19 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 20 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 21 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 22 | */ |
| 23 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 24 | #include "config.h" |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 25 | |
| 26 | #include <stddef.h> |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <unistd.h> |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 32 | #include <sys/mman.h> |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 33 | #include <linux/input.h> |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 34 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 35 | #include <wayland-client.h> |
| 36 | #include <wayland-egl.h> |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 37 | #include <wayland-cursor.h> |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 38 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 39 | #include "compositor.h" |
John Kåre Alsaker | 30d2b1f | 2012-11-13 19:10:28 +0100 | [diff] [blame] | 40 | #include "gl-renderer.h" |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 41 | #include "pixman-renderer.h" |
Kristian Høgsberg | 3c2360f | 2013-01-28 16:01:22 -0500 | [diff] [blame] | 42 | #include "../shared/image-loader.h" |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 43 | #include "../shared/os-compatibility.h" |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 44 | #include "../shared/cairo-util.h" |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 45 | #include "fullscreen-shell-client-protocol.h" |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 46 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 47 | #define WINDOW_TITLE "Weston Compositor" |
| 48 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 49 | struct wayland_compositor { |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 50 | struct weston_compositor base; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 51 | |
| 52 | struct { |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 53 | struct wl_display *wl_display; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 54 | struct wl_registry *registry; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 55 | struct wl_compositor *compositor; |
| 56 | struct wl_shell *shell; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 57 | struct _wl_fullscreen_shell *fshell; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 58 | struct wl_shm *shm; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 59 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 60 | struct wl_list output_list; |
| 61 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 62 | struct wl_event_source *wl_source; |
| 63 | uint32_t event_mask; |
| 64 | } parent; |
| 65 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 66 | int use_pixman; |
Jason Ekstrand | e4ca8b0 | 2014-04-02 19:53:55 -0500 | [diff] [blame] | 67 | int sprawl_across_outputs; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 68 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 69 | struct theme *theme; |
| 70 | cairo_device_t *frame_device; |
| 71 | struct wl_cursor_theme *cursor_theme; |
| 72 | struct wl_cursor *cursor; |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 73 | |
Jason Ekstrand | 06ced80 | 2013-11-07 20:13:29 -0600 | [diff] [blame] | 74 | struct wl_list input_list; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | struct wayland_output { |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 78 | struct weston_output base; |
| 79 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 80 | struct { |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 81 | int draw_initial_frame; |
| 82 | struct wl_surface *surface; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 83 | |
| 84 | struct wl_output *output; |
| 85 | uint32_t global_id; |
| 86 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 87 | struct wl_shell_surface *shell_surface; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 88 | int configure_width, configure_height; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 89 | } parent; |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 90 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 91 | int keyboard_count; |
| 92 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 93 | char *name; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 94 | struct frame *frame; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 95 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 96 | struct { |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 97 | struct wl_egl_window *egl_window; |
| 98 | struct { |
| 99 | cairo_surface_t *top; |
| 100 | cairo_surface_t *left; |
| 101 | cairo_surface_t *right; |
| 102 | cairo_surface_t *bottom; |
| 103 | } border; |
| 104 | } gl; |
| 105 | |
| 106 | struct { |
| 107 | struct wl_list buffers; |
| 108 | struct wl_list free_buffers; |
| 109 | } shm; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 110 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 111 | struct weston_mode mode; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 112 | uint32_t scale; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 113 | }; |
| 114 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 115 | struct wayland_parent_output { |
| 116 | struct wayland_output *output; |
| 117 | struct wl_list link; |
| 118 | |
| 119 | struct wl_output *global; |
| 120 | uint32_t id; |
| 121 | |
| 122 | struct { |
| 123 | char *make; |
| 124 | char *model; |
| 125 | int32_t width, height; |
| 126 | uint32_t subpixel; |
| 127 | } physical; |
| 128 | |
| 129 | int32_t x, y; |
| 130 | uint32_t transform; |
| 131 | uint32_t scale; |
| 132 | |
| 133 | struct wl_list mode_list; |
| 134 | struct weston_mode *preferred_mode; |
| 135 | struct weston_mode *current_mode; |
| 136 | }; |
| 137 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 138 | struct wayland_shm_buffer { |
| 139 | struct wayland_output *output; |
| 140 | struct wl_list link; |
| 141 | struct wl_list free_link; |
| 142 | |
| 143 | struct wl_buffer *buffer; |
| 144 | void *data; |
| 145 | size_t size; |
| 146 | pixman_region32_t damage; |
| 147 | int frame_damaged; |
| 148 | |
| 149 | pixman_image_t *pm_image; |
| 150 | cairo_surface_t *c_surface; |
| 151 | }; |
| 152 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 153 | struct wayland_input { |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 154 | struct weston_seat base; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 155 | struct wayland_compositor *compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 156 | struct wl_list link; |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 157 | |
| 158 | struct { |
| 159 | struct wl_seat *seat; |
| 160 | struct wl_pointer *pointer; |
| 161 | struct wl_keyboard *keyboard; |
| 162 | struct wl_touch *touch; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 163 | |
| 164 | struct { |
| 165 | struct wl_surface *surface; |
| 166 | int32_t hx, hy; |
| 167 | } cursor; |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 168 | } parent; |
| 169 | |
Jason Ekstrand | b7d9f2e | 2014-04-02 19:53:57 -0500 | [diff] [blame] | 170 | enum weston_key_state_update keyboard_state_update; |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 171 | uint32_t key_serial; |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 172 | uint32_t enter_serial; |
| 173 | int focus; |
| 174 | struct wayland_output *output; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 175 | struct wayland_output *keyboard_focus; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 176 | }; |
| 177 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 178 | struct gl_renderer_interface *gl_renderer; |
| 179 | |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 180 | static void |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 181 | wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer) |
| 182 | { |
| 183 | cairo_surface_destroy(buffer->c_surface); |
| 184 | pixman_image_unref(buffer->pm_image); |
| 185 | |
| 186 | wl_buffer_destroy(buffer->buffer); |
| 187 | munmap(buffer->data, buffer->size); |
| 188 | |
| 189 | pixman_region32_fini(&buffer->damage); |
| 190 | |
| 191 | wl_list_remove(&buffer->link); |
| 192 | wl_list_remove(&buffer->free_link); |
| 193 | free(buffer); |
| 194 | } |
| 195 | |
| 196 | static void |
| 197 | buffer_release(void *data, struct wl_buffer *buffer) |
| 198 | { |
| 199 | struct wayland_shm_buffer *sb = data; |
| 200 | |
| 201 | if (sb->output) { |
| 202 | wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link); |
| 203 | } else { |
| 204 | wayland_shm_buffer_destroy(sb); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | static const struct wl_buffer_listener buffer_listener = { |
| 209 | buffer_release |
| 210 | }; |
| 211 | |
| 212 | static struct wayland_shm_buffer * |
| 213 | wayland_output_get_shm_buffer(struct wayland_output *output) |
| 214 | { |
| 215 | struct wayland_compositor *c = |
| 216 | (struct wayland_compositor *) output->base.compositor; |
| 217 | struct wl_shm *shm = c->parent.shm; |
| 218 | struct wayland_shm_buffer *sb; |
| 219 | |
| 220 | struct wl_shm_pool *pool; |
| 221 | int width, height, stride; |
| 222 | int32_t fx, fy; |
| 223 | int fd; |
| 224 | unsigned char *data; |
| 225 | |
| 226 | if (!wl_list_empty(&output->shm.free_buffers)) { |
| 227 | sb = container_of(output->shm.free_buffers.next, |
| 228 | struct wayland_shm_buffer, free_link); |
| 229 | wl_list_remove(&sb->free_link); |
| 230 | wl_list_init(&sb->free_link); |
| 231 | |
| 232 | return sb; |
| 233 | } |
| 234 | |
| 235 | if (output->frame) { |
| 236 | width = frame_width(output->frame); |
| 237 | height = frame_height(output->frame); |
| 238 | } else { |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 239 | width = output->base.current_mode->width; |
| 240 | height = output->base.current_mode->height; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width); |
| 244 | |
| 245 | fd = os_create_anonymous_file(height * stride); |
| 246 | if (fd < 0) { |
Bryce W. Harrington | a093502 | 2014-03-21 05:54:02 +0000 | [diff] [blame] | 247 | weston_log("could not create an anonymous file buffer: %m\n"); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 248 | return NULL; |
| 249 | } |
| 250 | |
| 251 | data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 252 | if (data == MAP_FAILED) { |
Bryce W. Harrington | a093502 | 2014-03-21 05:54:02 +0000 | [diff] [blame] | 253 | 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] | 254 | close(fd); |
| 255 | return NULL; |
| 256 | } |
| 257 | |
| 258 | sb = zalloc(sizeof *sb); |
| 259 | |
| 260 | sb->output = output; |
| 261 | wl_list_init(&sb->free_link); |
| 262 | wl_list_insert(&output->shm.buffers, &sb->link); |
| 263 | |
| 264 | pixman_region32_init_rect(&sb->damage, 0, 0, |
| 265 | output->base.width, output->base.height); |
| 266 | sb->frame_damaged = 1; |
| 267 | |
| 268 | sb->data = data; |
| 269 | sb->size = height * stride; |
| 270 | |
| 271 | pool = wl_shm_create_pool(shm, fd, sb->size); |
| 272 | |
| 273 | sb->buffer = wl_shm_pool_create_buffer(pool, 0, |
| 274 | width, height, |
| 275 | stride, |
| 276 | WL_SHM_FORMAT_ARGB8888); |
| 277 | wl_buffer_add_listener(sb->buffer, &buffer_listener, sb); |
| 278 | wl_shm_pool_destroy(pool); |
| 279 | close(fd); |
| 280 | |
| 281 | memset(data, 0, sb->size); |
| 282 | |
| 283 | sb->c_surface = |
| 284 | cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, |
| 285 | width, height, stride); |
| 286 | |
| 287 | fx = 0; |
| 288 | fy = 0; |
| 289 | if (output->frame) |
| 290 | frame_interior(output->frame, &fx, &fy, 0, 0); |
| 291 | sb->pm_image = |
| 292 | pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height, |
| 293 | (uint32_t *)(data + fy * stride) + fx, |
| 294 | stride); |
| 295 | |
| 296 | return sb; |
| 297 | } |
| 298 | |
| 299 | static void |
Kristian Høgsberg | cdd61d0 | 2012-02-07 09:56:15 -0500 | [diff] [blame] | 300 | frame_done(void *data, struct wl_callback *callback, uint32_t time) |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 301 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 302 | struct weston_output *output = data; |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 303 | |
Kristian Høgsberg | cdd61d0 | 2012-02-07 09:56:15 -0500 | [diff] [blame] | 304 | wl_callback_destroy(callback); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 305 | weston_output_finish_frame(output, time); |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static const struct wl_callback_listener frame_listener = { |
| 309 | frame_done |
| 310 | }; |
| 311 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 312 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 313 | draw_initial_frame(struct wayland_output *output) |
| 314 | { |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 315 | struct wayland_shm_buffer *sb; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 316 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 317 | sb = wayland_output_get_shm_buffer(output); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 318 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 319 | /* If we are rendering with GL, then orphan it so that it gets |
| 320 | * destroyed immediately */ |
| 321 | if (output->gl.egl_window) |
| 322 | sb->output = NULL; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 323 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 324 | wl_surface_attach(output->parent.surface, sb->buffer, 0, 0); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 325 | wl_surface_damage(output->parent.surface, 0, 0, |
| 326 | output->base.current_mode->width, |
| 327 | output->base.current_mode->height); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static void |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 331 | wayland_output_update_gl_border(struct wayland_output *output) |
| 332 | { |
| 333 | int32_t ix, iy, iwidth, iheight, fwidth, fheight; |
| 334 | cairo_t *cr; |
| 335 | |
| 336 | if (!output->frame) |
| 337 | return; |
| 338 | if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT)) |
| 339 | return; |
| 340 | |
| 341 | fwidth = frame_width(output->frame); |
| 342 | fheight = frame_height(output->frame); |
| 343 | frame_interior(output->frame, &ix, &iy, &iwidth, &iheight); |
| 344 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 345 | if (!output->gl.border.top) |
| 346 | output->gl.border.top = |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 347 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 348 | fwidth, iy); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 349 | cr = cairo_create(output->gl.border.top); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 350 | frame_repaint(output->frame, cr); |
| 351 | cairo_destroy(cr); |
| 352 | gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP, |
| 353 | fwidth, iy, |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 354 | cairo_image_surface_get_stride(output->gl.border.top) / 4, |
| 355 | cairo_image_surface_get_data(output->gl.border.top)); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 356 | |
| 357 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 358 | if (!output->gl.border.left) |
| 359 | output->gl.border.left = |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 360 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 361 | ix, 1); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 362 | cr = cairo_create(output->gl.border.left); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 363 | cairo_translate(cr, 0, -iy); |
| 364 | frame_repaint(output->frame, cr); |
| 365 | cairo_destroy(cr); |
| 366 | gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT, |
| 367 | ix, 1, |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 368 | cairo_image_surface_get_stride(output->gl.border.left) / 4, |
| 369 | cairo_image_surface_get_data(output->gl.border.left)); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 370 | |
| 371 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 372 | if (!output->gl.border.right) |
| 373 | output->gl.border.right = |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 374 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 375 | fwidth - (ix + iwidth), 1); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 376 | cr = cairo_create(output->gl.border.right); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 377 | cairo_translate(cr, -(iwidth + ix), -iy); |
| 378 | frame_repaint(output->frame, cr); |
| 379 | cairo_destroy(cr); |
| 380 | gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT, |
| 381 | fwidth - (ix + iwidth), 1, |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 382 | cairo_image_surface_get_stride(output->gl.border.right) / 4, |
| 383 | cairo_image_surface_get_data(output->gl.border.right)); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 384 | |
| 385 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 386 | if (!output->gl.border.bottom) |
| 387 | output->gl.border.bottom = |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 388 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 389 | fwidth, fheight - (iy + iheight)); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 390 | cr = cairo_create(output->gl.border.bottom); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 391 | cairo_translate(cr, 0, -(iy + iheight)); |
| 392 | frame_repaint(output->frame, cr); |
| 393 | cairo_destroy(cr); |
| 394 | gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM, |
| 395 | fwidth, fheight - (iy + iheight), |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 396 | cairo_image_surface_get_stride(output->gl.border.bottom) / 4, |
| 397 | cairo_image_surface_get_data(output->gl.border.bottom)); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 401 | wayland_output_start_repaint_loop(struct weston_output *output_base) |
| 402 | { |
| 403 | struct wayland_output *output = (struct wayland_output *) output_base; |
Jason Ekstrand | 286ff68 | 2013-10-27 22:24:57 -0500 | [diff] [blame] | 404 | struct wayland_compositor *wc = |
| 405 | (struct wayland_compositor *)output->base.compositor; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 406 | struct wl_callback *callback; |
| 407 | |
| 408 | /* If this is the initial frame, we need to attach a buffer so that |
| 409 | * the compositor can map the surface and include it in its render |
| 410 | * loop. If the surface doesn't end up in the render loop, the frame |
| 411 | * callback won't be invoked. The buffer is transparent and of the |
| 412 | * same size as the future real output buffer. */ |
| 413 | if (output->parent.draw_initial_frame) { |
| 414 | output->parent.draw_initial_frame = 0; |
| 415 | |
| 416 | draw_initial_frame(output); |
| 417 | } |
| 418 | |
| 419 | callback = wl_surface_frame(output->parent.surface); |
| 420 | wl_callback_add_listener(callback, &frame_listener, output); |
| 421 | wl_surface_commit(output->parent.surface); |
Jason Ekstrand | 286ff68 | 2013-10-27 22:24:57 -0500 | [diff] [blame] | 422 | wl_display_flush(wc->parent.wl_display); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 423 | } |
| 424 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 425 | static int |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 426 | wayland_output_repaint_gl(struct weston_output *output_base, |
| 427 | pixman_region32_t *damage) |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 428 | { |
| 429 | struct wayland_output *output = (struct wayland_output *) output_base; |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame] | 430 | struct weston_compositor *ec = output->base.compositor; |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 431 | struct wl_callback *callback; |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 432 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 433 | callback = wl_surface_frame(output->parent.surface); |
| 434 | wl_callback_add_listener(callback, &frame_listener, output); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 435 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 436 | wayland_output_update_gl_border(output); |
| 437 | |
Pekka Paalanen | bc10638 | 2012-10-10 12:49:31 +0300 | [diff] [blame] | 438 | ec->renderer->repaint_output(&output->base, damage); |
Ander Conselvan de Oliveira | 0a88772 | 2012-11-22 15:57:00 +0200 | [diff] [blame] | 439 | |
| 440 | pixman_region32_subtract(&ec->primary_plane.damage, |
| 441 | &ec->primary_plane.damage, damage); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 442 | return 0; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 443 | } |
| 444 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 445 | static void |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 446 | wayland_output_update_shm_border(struct wayland_shm_buffer *buffer) |
| 447 | { |
| 448 | int32_t ix, iy, iwidth, iheight, fwidth, fheight; |
| 449 | cairo_t *cr; |
| 450 | |
| 451 | if (!buffer->output->frame || !buffer->frame_damaged) |
| 452 | return; |
| 453 | |
| 454 | cr = cairo_create(buffer->c_surface); |
| 455 | |
| 456 | frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight); |
| 457 | fwidth = frame_width(buffer->output->frame); |
| 458 | fheight = frame_height(buffer->output->frame); |
| 459 | |
| 460 | /* Set the clip so we don't unnecisaraly damage the surface */ |
| 461 | cairo_move_to(cr, ix, iy); |
| 462 | cairo_rel_line_to(cr, iwidth, 0); |
| 463 | cairo_rel_line_to(cr, 0, iheight); |
| 464 | cairo_rel_line_to(cr, -iwidth, 0); |
| 465 | cairo_line_to(cr, ix, iy); |
| 466 | cairo_line_to(cr, 0, iy); |
| 467 | cairo_line_to(cr, 0, fheight); |
| 468 | cairo_line_to(cr, fwidth, fheight); |
| 469 | cairo_line_to(cr, fwidth, 0); |
| 470 | cairo_line_to(cr, 0, 0); |
| 471 | cairo_line_to(cr, 0, iy); |
| 472 | cairo_close_path(cr); |
| 473 | cairo_clip(cr); |
| 474 | |
| 475 | /* Draw using a pattern so that the final result gets clipped */ |
| 476 | cairo_push_group(cr); |
| 477 | frame_repaint(buffer->output->frame, cr); |
| 478 | cairo_pop_group_to_source(cr); |
| 479 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 480 | cairo_paint(cr); |
| 481 | |
| 482 | cairo_destroy(cr); |
| 483 | } |
| 484 | |
| 485 | static void |
| 486 | wayland_shm_buffer_attach(struct wayland_shm_buffer *sb) |
| 487 | { |
| 488 | pixman_region32_t damage; |
| 489 | pixman_box32_t *rects; |
| 490 | int32_t ix, iy, iwidth, iheight, fwidth, fheight; |
| 491 | int i, n; |
| 492 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 493 | pixman_region32_init(&damage); |
| 494 | weston_transformed_region(sb->output->base.width, |
| 495 | sb->output->base.height, |
| 496 | sb->output->base.transform, |
| 497 | sb->output->base.current_scale, |
| 498 | &sb->damage, &damage); |
| 499 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 500 | if (sb->output->frame) { |
| 501 | frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight); |
| 502 | fwidth = frame_width(sb->output->frame); |
| 503 | fheight = frame_height(sb->output->frame); |
| 504 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 505 | pixman_region32_translate(&damage, ix, iy); |
| 506 | |
| 507 | if (sb->frame_damaged) { |
| 508 | pixman_region32_union_rect(&damage, &damage, |
| 509 | 0, 0, fwidth, iy); |
| 510 | pixman_region32_union_rect(&damage, &damage, |
| 511 | 0, iy, ix, iheight); |
| 512 | pixman_region32_union_rect(&damage, &damage, |
| 513 | ix + iwidth, iy, |
| 514 | fwidth - (ix + iwidth), iheight); |
| 515 | pixman_region32_union_rect(&damage, &damage, |
| 516 | 0, iy + iheight, |
| 517 | fwidth, fheight - (iy + iheight)); |
| 518 | } |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 519 | } |
| 520 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 521 | rects = pixman_region32_rectangles(&damage, &n); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 522 | wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0); |
| 523 | for (i = 0; i < n; ++i) |
| 524 | wl_surface_damage(sb->output->parent.surface, rects[i].x1, |
| 525 | rects[i].y1, rects[i].x2 - rects[i].x1, |
| 526 | rects[i].y2 - rects[i].y1); |
| 527 | |
| 528 | if (sb->output->frame) |
| 529 | pixman_region32_fini(&damage); |
| 530 | } |
| 531 | |
| 532 | static int |
| 533 | wayland_output_repaint_pixman(struct weston_output *output_base, |
| 534 | pixman_region32_t *damage) |
| 535 | { |
| 536 | struct wayland_output *output = (struct wayland_output *) output_base; |
| 537 | struct wayland_compositor *c = |
| 538 | (struct wayland_compositor *)output->base.compositor; |
| 539 | struct wl_callback *callback; |
| 540 | struct wayland_shm_buffer *sb; |
| 541 | |
| 542 | if (output->frame) { |
| 543 | if (frame_status(output->frame) & FRAME_STATUS_REPAINT) |
| 544 | wl_list_for_each(sb, &output->shm.buffers, link) |
| 545 | sb->frame_damaged = 1; |
| 546 | } |
| 547 | |
| 548 | wl_list_for_each(sb, &output->shm.buffers, link) |
| 549 | pixman_region32_union(&sb->damage, &sb->damage, damage); |
| 550 | |
| 551 | sb = wayland_output_get_shm_buffer(output); |
| 552 | |
| 553 | wayland_output_update_shm_border(sb); |
| 554 | pixman_renderer_output_set_buffer(output_base, sb->pm_image); |
| 555 | c->base.renderer->repaint_output(output_base, &sb->damage); |
| 556 | |
| 557 | wayland_shm_buffer_attach(sb); |
| 558 | |
| 559 | callback = wl_surface_frame(output->parent.surface); |
| 560 | wl_callback_add_listener(callback, &frame_listener, output); |
| 561 | wl_surface_commit(output->parent.surface); |
| 562 | wl_display_flush(c->parent.wl_display); |
| 563 | |
| 564 | pixman_region32_fini(&sb->damage); |
| 565 | pixman_region32_init(&sb->damage); |
| 566 | sb->frame_damaged = 0; |
| 567 | |
| 568 | pixman_region32_subtract(&c->base.primary_plane.damage, |
| 569 | &c->base.primary_plane.damage, damage); |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 574 | wayland_output_destroy(struct weston_output *output_base) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 575 | { |
| 576 | struct wayland_output *output = (struct wayland_output *) output_base; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 577 | struct wayland_compositor *c = |
| 578 | (struct wayland_compositor *) output->base.compositor; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 579 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 580 | if (c->use_pixman) { |
| 581 | pixman_renderer_output_destroy(output_base); |
| 582 | } else { |
| 583 | gl_renderer->output_destroy(output_base); |
| 584 | } |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 585 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 586 | wl_egl_window_destroy(output->gl.egl_window); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 587 | wl_surface_destroy(output->parent.surface); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 588 | if (output->parent.shell_surface) |
| 589 | wl_shell_surface_destroy(output->parent.shell_surface); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 590 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 591 | if (output->frame) |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 592 | frame_destroy(output->frame); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 593 | |
| 594 | cairo_surface_destroy(output->gl.border.top); |
| 595 | cairo_surface_destroy(output->gl.border.left); |
| 596 | cairo_surface_destroy(output->gl.border.right); |
| 597 | cairo_surface_destroy(output->gl.border.bottom); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 598 | |
| 599 | weston_output_destroy(&output->base); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 600 | free(output); |
| 601 | |
| 602 | return; |
| 603 | } |
| 604 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 605 | static const struct wl_shell_surface_listener shell_surface_listener; |
| 606 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 607 | static int |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 608 | wayland_output_init_gl_renderer(struct wayland_output *output) |
| 609 | { |
Jason Ekstrand | 00b8428 | 2013-10-27 22:24:59 -0500 | [diff] [blame] | 610 | int32_t fwidth = 0, fheight = 0; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 611 | |
| 612 | if (output->frame) { |
| 613 | fwidth = frame_width(output->frame); |
| 614 | fheight = frame_height(output->frame); |
| 615 | } else { |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 616 | fwidth = output->base.current_mode->width; |
| 617 | fheight = output->base.current_mode->height; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | output->gl.egl_window = |
| 621 | wl_egl_window_create(output->parent.surface, |
| 622 | fwidth, fheight); |
| 623 | if (!output->gl.egl_window) { |
| 624 | weston_log("failure to create wl_egl_window\n"); |
| 625 | return -1; |
| 626 | } |
| 627 | |
| 628 | if (gl_renderer->output_create(&output->base, |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 629 | output->gl.egl_window, |
| 630 | gl_renderer->alpha_attribs, |
| 631 | NULL) < 0) |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 632 | goto cleanup_window; |
| 633 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 634 | return 0; |
| 635 | |
| 636 | cleanup_window: |
| 637 | wl_egl_window_destroy(output->gl.egl_window); |
| 638 | return -1; |
| 639 | } |
| 640 | |
| 641 | static int |
| 642 | wayland_output_init_pixman_renderer(struct wayland_output *output) |
| 643 | { |
| 644 | return pixman_renderer_output_create(&output->base); |
| 645 | } |
| 646 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 647 | static void |
| 648 | wayland_output_resize_surface(struct wayland_output *output) |
| 649 | { |
| 650 | struct wayland_compositor *c = |
| 651 | (struct wayland_compositor *)output->base.compositor; |
| 652 | struct wayland_shm_buffer *buffer, *next; |
| 653 | int32_t ix, iy, iwidth, iheight; |
| 654 | int32_t width, height; |
| 655 | struct wl_region *region; |
| 656 | |
| 657 | width = output->base.current_mode->width; |
| 658 | height = output->base.current_mode->height; |
| 659 | |
| 660 | if (output->frame) { |
| 661 | frame_resize_inside(output->frame, width, height); |
| 662 | |
| 663 | frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight); |
| 664 | region = wl_compositor_create_region(c->parent.compositor); |
| 665 | wl_region_add(region, ix, iy, iwidth, iheight); |
| 666 | wl_surface_set_input_region(output->parent.surface, region); |
| 667 | wl_region_destroy(region); |
| 668 | |
| 669 | frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight); |
| 670 | region = wl_compositor_create_region(c->parent.compositor); |
| 671 | wl_region_add(region, ix, iy, iwidth, iheight); |
| 672 | wl_surface_set_opaque_region(output->parent.surface, region); |
| 673 | wl_region_destroy(region); |
| 674 | |
| 675 | width = frame_width(output->frame); |
| 676 | height = frame_height(output->frame); |
| 677 | } else { |
| 678 | region = wl_compositor_create_region(c->parent.compositor); |
| 679 | wl_region_add(region, 0, 0, width, height); |
| 680 | wl_surface_set_input_region(output->parent.surface, region); |
| 681 | wl_region_destroy(region); |
| 682 | |
| 683 | region = wl_compositor_create_region(c->parent.compositor); |
| 684 | wl_region_add(region, 0, 0, width, height); |
| 685 | wl_surface_set_opaque_region(output->parent.surface, region); |
| 686 | wl_region_destroy(region); |
| 687 | } |
| 688 | |
| 689 | if (output->gl.egl_window) { |
| 690 | wl_egl_window_resize(output->gl.egl_window, |
| 691 | width, height, 0, 0); |
| 692 | |
| 693 | /* These will need to be re-created due to the resize */ |
| 694 | gl_renderer->output_set_border(&output->base, |
| 695 | GL_RENDERER_BORDER_TOP, |
| 696 | 0, 0, 0, NULL); |
| 697 | cairo_surface_destroy(output->gl.border.top); |
| 698 | output->gl.border.top = NULL; |
| 699 | gl_renderer->output_set_border(&output->base, |
| 700 | GL_RENDERER_BORDER_LEFT, |
| 701 | 0, 0, 0, NULL); |
| 702 | cairo_surface_destroy(output->gl.border.left); |
| 703 | output->gl.border.left = NULL; |
| 704 | gl_renderer->output_set_border(&output->base, |
| 705 | GL_RENDERER_BORDER_RIGHT, |
| 706 | 0, 0, 0, NULL); |
| 707 | cairo_surface_destroy(output->gl.border.right); |
| 708 | output->gl.border.right = NULL; |
| 709 | gl_renderer->output_set_border(&output->base, |
| 710 | GL_RENDERER_BORDER_BOTTOM, |
| 711 | 0, 0, 0, NULL); |
| 712 | cairo_surface_destroy(output->gl.border.bottom); |
| 713 | output->gl.border.bottom = NULL; |
| 714 | } |
| 715 | |
| 716 | /* Throw away any remaining SHM buffers */ |
| 717 | wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link) |
| 718 | wayland_shm_buffer_destroy(buffer); |
| 719 | /* These will get thrown away when they get released */ |
| 720 | wl_list_for_each(buffer, &output->shm.buffers, link) |
| 721 | buffer->output = NULL; |
| 722 | } |
| 723 | |
| 724 | static int |
| 725 | wayland_output_set_windowed(struct wayland_output *output) |
| 726 | { |
| 727 | struct wayland_compositor *c = |
| 728 | (struct wayland_compositor *)output->base.compositor; |
| 729 | int tlen; |
| 730 | char *title; |
| 731 | |
| 732 | if (output->frame) |
| 733 | return 0; |
| 734 | |
| 735 | if (output->name) { |
| 736 | tlen = strlen(output->name) + strlen(WINDOW_TITLE " - "); |
| 737 | title = malloc(tlen + 1); |
| 738 | if (!title) |
| 739 | return -1; |
| 740 | |
| 741 | snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name); |
| 742 | } else { |
| 743 | title = strdup(WINDOW_TITLE); |
| 744 | } |
| 745 | |
| 746 | if (!c->theme) { |
| 747 | c->theme = theme_create(); |
| 748 | if (!c->theme) { |
| 749 | free(title); |
| 750 | return -1; |
| 751 | } |
| 752 | } |
| 753 | output->frame = frame_create(c->theme, 100, 100, |
| 754 | FRAME_BUTTON_CLOSE, title); |
| 755 | free(title); |
| 756 | if (!output->frame) |
| 757 | return -1; |
| 758 | |
| 759 | if (output->keyboard_count) |
| 760 | frame_set_flag(output->frame, FRAME_FLAG_ACTIVE); |
| 761 | |
| 762 | wayland_output_resize_surface(output); |
| 763 | |
| 764 | wl_shell_surface_set_toplevel(output->parent.shell_surface); |
| 765 | |
| 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | static void |
| 770 | wayland_output_set_fullscreen(struct wayland_output *output, |
| 771 | enum wl_shell_surface_fullscreen_method method, |
| 772 | uint32_t framerate, struct wl_output *target) |
| 773 | { |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 774 | struct wayland_compositor *c = |
| 775 | (struct wayland_compositor *)output->base.compositor; |
| 776 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 777 | if (output->frame) { |
| 778 | frame_destroy(output->frame); |
| 779 | output->frame = NULL; |
| 780 | } |
| 781 | |
| 782 | wayland_output_resize_surface(output); |
| 783 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 784 | if (output->parent.shell_surface) { |
| 785 | wl_shell_surface_set_fullscreen(output->parent.shell_surface, |
| 786 | method, framerate, target); |
| 787 | } else if (c->parent.fshell) { |
| 788 | _wl_fullscreen_shell_present_surface(c->parent.fshell, |
| 789 | output->parent.surface, |
| 790 | method, target); |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | static struct weston_mode * |
| 795 | wayland_output_choose_mode(struct wayland_output *output, |
| 796 | struct weston_mode *ref_mode) |
| 797 | { |
| 798 | struct weston_mode *mode; |
| 799 | |
| 800 | /* First look for an exact match */ |
| 801 | wl_list_for_each(mode, &output->base.mode_list, link) |
| 802 | if (mode->width == ref_mode->width && |
| 803 | mode->height == ref_mode->height && |
| 804 | mode->refresh == ref_mode->refresh) |
| 805 | return mode; |
| 806 | |
| 807 | /* If we can't find an exact match, ignore refresh and try again */ |
| 808 | wl_list_for_each(mode, &output->base.mode_list, link) |
| 809 | if (mode->width == ref_mode->width && |
| 810 | mode->height == ref_mode->height) |
| 811 | return mode; |
| 812 | |
| 813 | /* Yeah, we failed */ |
| 814 | return NULL; |
| 815 | } |
| 816 | |
| 817 | enum mode_status { |
| 818 | MODE_STATUS_UNKNOWN, |
| 819 | MODE_STATUS_SUCCESS, |
| 820 | MODE_STATUS_FAIL, |
| 821 | MODE_STATUS_CANCEL, |
| 822 | }; |
| 823 | |
| 824 | static void |
| 825 | mode_feedback_successful(void *data, |
| 826 | struct _wl_fullscreen_shell_mode_feedback *fb) |
| 827 | { |
| 828 | enum mode_status *value = data; |
| 829 | |
| 830 | printf("Mode switch successful\n"); |
| 831 | |
| 832 | *value = MODE_STATUS_SUCCESS; |
| 833 | } |
| 834 | |
| 835 | static void |
| 836 | mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb) |
| 837 | { |
| 838 | enum mode_status *value = data; |
| 839 | |
| 840 | printf("Mode switch failed\n"); |
| 841 | |
| 842 | *value = MODE_STATUS_FAIL; |
| 843 | } |
| 844 | |
| 845 | static void |
| 846 | mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb) |
| 847 | { |
| 848 | enum mode_status *value = data; |
| 849 | |
| 850 | printf("Mode switch cancelled\n"); |
| 851 | |
| 852 | *value = MODE_STATUS_CANCEL; |
| 853 | } |
| 854 | |
| 855 | struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = { |
| 856 | mode_feedback_successful, |
| 857 | mode_feedback_failed, |
| 858 | mode_feedback_cancelled, |
| 859 | }; |
| 860 | |
| 861 | static int |
| 862 | wayland_output_switch_mode(struct weston_output *output_base, |
| 863 | struct weston_mode *mode) |
| 864 | { |
| 865 | struct wayland_output *output = (struct wayland_output *) output_base; |
| 866 | struct wayland_compositor *c; |
| 867 | struct wl_surface *old_surface; |
| 868 | struct weston_mode *old_mode; |
| 869 | struct _wl_fullscreen_shell_mode_feedback *mode_feedback; |
| 870 | enum mode_status mode_status; |
| 871 | int ret = 0; |
| 872 | |
| 873 | if (output_base == NULL) { |
| 874 | weston_log("output is NULL.\n"); |
| 875 | return -1; |
| 876 | } |
| 877 | |
| 878 | if (mode == NULL) { |
| 879 | weston_log("mode is NULL.\n"); |
| 880 | return -1; |
| 881 | } |
| 882 | |
| 883 | c = (struct wayland_compositor *)output_base->compositor; |
| 884 | |
| 885 | if (output->parent.shell_surface || !c->parent.fshell) |
| 886 | return -1; |
| 887 | |
| 888 | mode = wayland_output_choose_mode(output, mode); |
| 889 | if (mode == NULL) |
| 890 | return -1; |
| 891 | |
| 892 | if (output->base.current_mode == mode) |
| 893 | return 0; |
| 894 | |
| 895 | old_mode = output->base.current_mode; |
| 896 | old_surface = output->parent.surface; |
| 897 | output->base.current_mode = mode; |
| 898 | output->parent.surface = |
| 899 | wl_compositor_create_surface(c->parent.compositor); |
| 900 | wl_surface_set_user_data(output->parent.surface, output); |
| 901 | |
| 902 | /* Blow the old buffers because we changed size/surfaces */ |
| 903 | wayland_output_resize_surface(output); |
| 904 | |
| 905 | mode_feedback = |
| 906 | _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell, |
| 907 | output->parent.surface, |
| 908 | output->parent.output, |
| 909 | mode->refresh); |
| 910 | _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback, |
| 911 | &mode_feedback_listener, |
| 912 | &mode_status); |
| 913 | |
| 914 | /* This should kick-start things again */ |
| 915 | output->parent.draw_initial_frame = 1; |
| 916 | wayland_output_start_repaint_loop(&output->base); |
| 917 | |
| 918 | mode_status = MODE_STATUS_UNKNOWN; |
| 919 | while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0) |
| 920 | ret = wl_display_dispatch(c->parent.wl_display); |
| 921 | |
| 922 | _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback); |
| 923 | |
| 924 | if (mode_status == MODE_STATUS_FAIL) { |
| 925 | output->base.current_mode = old_mode; |
| 926 | wl_surface_destroy(output->parent.surface); |
| 927 | output->parent.surface = old_surface; |
| 928 | wayland_output_resize_surface(output); |
| 929 | |
| 930 | return -1; |
| 931 | } |
| 932 | |
| 933 | old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT; |
| 934 | output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT; |
| 935 | |
| 936 | if (c->use_pixman) { |
| 937 | pixman_renderer_output_destroy(output_base); |
| 938 | if (wayland_output_init_pixman_renderer(output) < 0) |
| 939 | goto err_output; |
| 940 | } else { |
| 941 | gl_renderer->output_destroy(output_base); |
| 942 | wl_egl_window_destroy(output->gl.egl_window); |
| 943 | if (wayland_output_init_gl_renderer(output) < 0) |
| 944 | goto err_output; |
| 945 | } |
| 946 | wl_surface_destroy(old_surface); |
| 947 | |
| 948 | weston_output_schedule_repaint(&output->base); |
| 949 | |
| 950 | return 0; |
| 951 | |
| 952 | err_output: |
| 953 | /* XXX */ |
| 954 | return -1; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 955 | } |
| 956 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 957 | static struct wayland_output * |
| 958 | wayland_output_create(struct wayland_compositor *c, int x, int y, |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 959 | int width, int height, const char *name, int fullscreen, |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 960 | uint32_t transform, int32_t scale) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 961 | { |
| 962 | struct wayland_output *output; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 963 | int output_width, output_height; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 964 | |
| 965 | weston_log("Creating %dx%d wayland output at (%d, %d)\n", |
| 966 | width, height, x, y); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 967 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 968 | output = zalloc(sizeof *output); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 969 | if (output == NULL) |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 970 | return NULL; |
| 971 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 972 | output->name = name ? strdup(name) : NULL; |
| 973 | output->base.make = "waywayland"; |
| 974 | output->base.model = "none"; |
| 975 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 976 | output_width = width * scale; |
| 977 | output_height = height * scale; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 978 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 979 | output->parent.surface = |
| 980 | wl_compositor_create_surface(c->parent.compositor); |
| 981 | if (!output->parent.surface) |
| 982 | goto err_name; |
| 983 | wl_surface_set_user_data(output->parent.surface, output); |
| 984 | |
| 985 | output->parent.draw_initial_frame = 1; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 986 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 987 | if (c->parent.shell) { |
| 988 | output->parent.shell_surface = |
| 989 | wl_shell_get_shell_surface(c->parent.shell, |
| 990 | output->parent.surface); |
| 991 | if (!output->parent.shell_surface) |
| 992 | goto err_surface; |
| 993 | wl_shell_surface_add_listener(output->parent.shell_surface, |
| 994 | &shell_surface_listener, output); |
| 995 | } |
| 996 | |
| 997 | if (fullscreen && c->parent.shell) { |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 998 | wl_shell_surface_set_fullscreen(output->parent.shell_surface, |
| 999 | 0, 0, NULL); |
| 1000 | wl_display_roundtrip(c->parent.wl_display); |
| 1001 | if (!width) |
| 1002 | output_width = output->parent.configure_width; |
| 1003 | if (!height) |
| 1004 | output_height = output->parent.configure_height; |
| 1005 | } |
| 1006 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1007 | output->mode.flags = |
| 1008 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1009 | output->mode.width = output_width; |
| 1010 | output->mode.height = output_height; |
| 1011 | output->mode.refresh = 60000; |
| 1012 | output->scale = scale; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1013 | wl_list_init(&output->base.mode_list); |
| 1014 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1015 | output->base.current_mode = &output->mode; |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 1016 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 1017 | wl_list_init(&output->shm.buffers); |
| 1018 | wl_list_init(&output->shm.free_buffers); |
| 1019 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1020 | weston_output_init(&output->base, &c->base, x, y, width, height, |
| 1021 | transform, scale); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1022 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 1023 | if (c->use_pixman) { |
| 1024 | if (wayland_output_init_pixman_renderer(output) < 0) |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1025 | goto err_output; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 1026 | output->base.repaint = wayland_output_repaint_pixman; |
| 1027 | } else { |
| 1028 | if (wayland_output_init_gl_renderer(output) < 0) |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1029 | goto err_output; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 1030 | output->base.repaint = wayland_output_repaint_gl; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1031 | } |
| 1032 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 1033 | output->base.start_repaint_loop = wayland_output_start_repaint_loop; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1034 | output->base.destroy = wayland_output_destroy; |
Jesse Barnes | 5308a5e | 2012-02-09 13:12:57 -0800 | [diff] [blame] | 1035 | output->base.assign_planes = NULL; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1036 | output->base.set_backlight = NULL; |
| 1037 | output->base.set_dpms = NULL; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1038 | output->base.switch_mode = wayland_output_switch_mode; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1039 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1040 | wl_list_insert(c->base.output_list.prev, &output->base.link); |
| 1041 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1042 | return output; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 1043 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1044 | err_output: |
| 1045 | weston_output_destroy(&output->base); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1046 | if (output->parent.shell_surface) |
| 1047 | wl_shell_surface_destroy(output->parent.shell_surface); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1048 | err_surface: |
| 1049 | wl_surface_destroy(output->parent.surface); |
| 1050 | err_name: |
| 1051 | free(output->name); |
| 1052 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1053 | /* FIXME: cleanup weston_output */ |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 1054 | free(output); |
| 1055 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1056 | return NULL; |
| 1057 | } |
| 1058 | |
| 1059 | static struct wayland_output * |
| 1060 | wayland_output_create_for_config(struct wayland_compositor *c, |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1061 | struct weston_config_section *config_section, |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1062 | int option_width, int option_height, |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame] | 1063 | int option_scale, int32_t x, int32_t y) |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1064 | { |
| 1065 | struct wayland_output *output; |
| 1066 | char *mode, *t, *name, *str; |
| 1067 | int width, height, scale; |
| 1068 | uint32_t transform; |
| 1069 | unsigned int i, slen; |
| 1070 | |
| 1071 | static const struct { const char *name; uint32_t token; } transform_names[] = { |
| 1072 | { "normal", WL_OUTPUT_TRANSFORM_NORMAL }, |
| 1073 | { "90", WL_OUTPUT_TRANSFORM_90 }, |
| 1074 | { "180", WL_OUTPUT_TRANSFORM_180 }, |
| 1075 | { "270", WL_OUTPUT_TRANSFORM_270 }, |
| 1076 | { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED }, |
| 1077 | { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 }, |
| 1078 | { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 }, |
| 1079 | { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 }, |
| 1080 | }; |
| 1081 | |
| 1082 | weston_config_section_get_string(config_section, "name", &name, NULL); |
| 1083 | if (name) { |
| 1084 | slen = strlen(name); |
| 1085 | slen += strlen(WINDOW_TITLE " - "); |
| 1086 | str = malloc(slen + 1); |
| 1087 | if (str) |
| 1088 | snprintf(str, slen + 1, WINDOW_TITLE " - %s", name); |
| 1089 | free(name); |
| 1090 | name = str; |
| 1091 | } |
| 1092 | if (!name) |
U. Artie Eoff | 1a08d11 | 2014-01-17 12:22:50 -0800 | [diff] [blame] | 1093 | name = strdup(WINDOW_TITLE); |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1094 | |
| 1095 | weston_config_section_get_string(config_section, |
| 1096 | "mode", &mode, "1024x600"); |
| 1097 | if (sscanf(mode, "%dx%d", &width, &height) != 2) { |
| 1098 | weston_log("Invalid mode \"%s\" for output %s\n", |
| 1099 | mode, name); |
| 1100 | width = 1024; |
| 1101 | height = 640; |
| 1102 | } |
| 1103 | free(mode); |
| 1104 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1105 | if (option_width) |
| 1106 | width = option_width; |
| 1107 | if (option_height) |
| 1108 | height = option_height; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1109 | |
| 1110 | weston_config_section_get_int(config_section, "scale", &scale, 1); |
| 1111 | |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame] | 1112 | if (option_scale) |
| 1113 | scale = option_scale; |
| 1114 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1115 | weston_config_section_get_string(config_section, |
| 1116 | "transform", &t, "normal"); |
| 1117 | transform = WL_OUTPUT_TRANSFORM_NORMAL; |
| 1118 | for (i = 0; i < ARRAY_LENGTH(transform_names); i++) { |
| 1119 | if (strcmp(transform_names[i].name, t) == 0) { |
| 1120 | transform = transform_names[i].token; |
| 1121 | break; |
| 1122 | } |
| 1123 | } |
| 1124 | if (i >= ARRAY_LENGTH(transform_names)) |
| 1125 | weston_log("Invalid transform \"%s\" for output %s\n", t, name); |
| 1126 | free(t); |
| 1127 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1128 | output = wayland_output_create(c, x, y, width, height, name, 0, |
| 1129 | transform, scale); |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1130 | free(name); |
| 1131 | |
| 1132 | return output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1133 | } |
| 1134 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1135 | static struct wayland_output * |
| 1136 | wayland_output_create_for_parent_output(struct wayland_compositor *c, |
| 1137 | struct wayland_parent_output *poutput) |
| 1138 | { |
| 1139 | struct wayland_output *output; |
| 1140 | struct weston_mode *mode; |
| 1141 | int32_t x; |
| 1142 | |
| 1143 | if (poutput->current_mode) { |
| 1144 | mode = poutput->current_mode; |
| 1145 | } else if (poutput->preferred_mode) { |
| 1146 | mode = poutput->current_mode; |
| 1147 | } else if (!wl_list_empty(&poutput->mode_list)) { |
| 1148 | mode = container_of(poutput->mode_list.next, |
| 1149 | struct weston_mode, link); |
| 1150 | } else { |
| 1151 | weston_log("No valid modes found. Skipping output"); |
| 1152 | return NULL; |
| 1153 | } |
| 1154 | |
| 1155 | if (!wl_list_empty(&c->base.output_list)) { |
| 1156 | output = container_of(c->base.output_list.prev, |
| 1157 | struct wayland_output, base.link); |
| 1158 | x = output->base.x + output->base.current_mode->width; |
| 1159 | } else { |
| 1160 | x = 0; |
| 1161 | } |
| 1162 | |
| 1163 | output = wayland_output_create(c, x, 0, mode->width, mode->height, |
| 1164 | NULL, 0, |
| 1165 | WL_OUTPUT_TRANSFORM_NORMAL, 1); |
| 1166 | if (!output) |
| 1167 | return NULL; |
| 1168 | |
| 1169 | output->parent.output = poutput->global; |
| 1170 | |
| 1171 | output->base.make = poutput->physical.make; |
| 1172 | output->base.model = poutput->physical.model; |
| 1173 | wl_list_init(&output->base.mode_list); |
| 1174 | wl_list_insert_list(&output->base.mode_list, &poutput->mode_list); |
| 1175 | wl_list_init(&poutput->mode_list); |
| 1176 | |
| 1177 | wayland_output_set_fullscreen(output, |
| 1178 | WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER, |
| 1179 | mode->refresh, poutput->global); |
| 1180 | |
| 1181 | if (output->parent.shell_surface) { |
| 1182 | wl_shell_surface_set_fullscreen(output->parent.shell_surface, |
| 1183 | WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER, |
| 1184 | mode->refresh, poutput->global); |
| 1185 | } else if (c->parent.fshell) { |
| 1186 | _wl_fullscreen_shell_present_surface(c->parent.fshell, |
| 1187 | output->parent.surface, |
| 1188 | _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER, |
| 1189 | poutput->global); |
| 1190 | _wl_fullscreen_shell_mode_feedback_destroy( |
| 1191 | _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell, |
| 1192 | output->parent.surface, |
| 1193 | poutput->global, |
| 1194 | mode->refresh)); |
| 1195 | } |
| 1196 | |
| 1197 | return output; |
| 1198 | } |
| 1199 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 1200 | static void |
| 1201 | shell_surface_ping(void *data, struct wl_shell_surface *shell_surface, |
| 1202 | uint32_t serial) |
| 1203 | { |
| 1204 | wl_shell_surface_pong(shell_surface, serial); |
| 1205 | } |
| 1206 | |
| 1207 | static void |
| 1208 | shell_surface_configure(void *data, struct wl_shell_surface *shell_surface, |
| 1209 | uint32_t edges, int32_t width, int32_t height) |
| 1210 | { |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1211 | struct wayland_output *output = data; |
| 1212 | |
| 1213 | output->parent.configure_width = width; |
| 1214 | output->parent.configure_height = height; |
| 1215 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 1216 | /* FIXME: implement resizing */ |
| 1217 | } |
| 1218 | |
| 1219 | static void |
| 1220 | shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface) |
| 1221 | { |
| 1222 | } |
| 1223 | |
| 1224 | static const struct wl_shell_surface_listener shell_surface_listener = { |
| 1225 | shell_surface_ping, |
| 1226 | shell_surface_configure, |
| 1227 | shell_surface_popup_done |
| 1228 | }; |
| 1229 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1230 | /* Events received from the wayland-server this compositor is client of: */ |
| 1231 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1232 | /* parent input interface */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1233 | static void |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1234 | input_set_cursor(struct wayland_input *input) |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1235 | { |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1236 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1237 | struct wl_buffer *buffer; |
| 1238 | struct wl_cursor_image *image; |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1239 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1240 | if (!input->compositor->cursor) |
| 1241 | return; /* Couldn't load the cursor. Can't set it */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1242 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1243 | image = input->compositor->cursor->images[0]; |
| 1244 | buffer = wl_cursor_image_get_buffer(image); |
Hardening | 842a36a | 2014-03-18 14:12:50 +0100 | [diff] [blame] | 1245 | if (!buffer) |
| 1246 | return; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1247 | |
| 1248 | wl_pointer_set_cursor(input->parent.pointer, input->enter_serial, |
| 1249 | input->parent.cursor.surface, |
| 1250 | image->hotspot_x, image->hotspot_y); |
| 1251 | |
| 1252 | wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0); |
| 1253 | wl_surface_damage(input->parent.cursor.surface, 0, 0, |
| 1254 | image->width, image->height); |
| 1255 | wl_surface_commit(input->parent.cursor.surface); |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1256 | } |
| 1257 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1258 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1259 | input_handle_pointer_enter(void *data, struct wl_pointer *pointer, |
| 1260 | uint32_t serial, struct wl_surface *surface, |
Kristian Høgsberg | e11bbe4 | 2012-05-09 12:19:04 -0400 | [diff] [blame] | 1261 | wl_fixed_t x, wl_fixed_t y) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1262 | { |
| 1263 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1264 | int32_t fx, fy; |
| 1265 | enum theme_location location; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1266 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1267 | /* XXX: If we get a modifier event immediately before the focus, |
| 1268 | * we should try to keep the same serial. */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1269 | input->enter_serial = serial; |
| 1270 | input->output = wl_surface_get_user_data(surface); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1271 | |
| 1272 | if (input->output->frame) { |
| 1273 | location = frame_pointer_enter(input->output->frame, input, |
| 1274 | wl_fixed_to_int(x), |
| 1275 | wl_fixed_to_int(y)); |
| 1276 | frame_interior(input->output->frame, &fx, &fy, NULL, NULL); |
| 1277 | x -= wl_fixed_from_int(fx); |
| 1278 | y -= wl_fixed_from_int(fy); |
| 1279 | |
| 1280 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 1281 | weston_output_schedule_repaint(&input->output->base); |
| 1282 | } else { |
| 1283 | location = THEME_LOCATION_CLIENT_AREA; |
| 1284 | } |
| 1285 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1286 | weston_output_transform_coordinate(&input->output->base, x, y, &x, &y); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1287 | |
| 1288 | if (location == THEME_LOCATION_CLIENT_AREA) { |
| 1289 | input->focus = 1; |
| 1290 | notify_pointer_focus(&input->base, &input->output->base, x, y); |
| 1291 | wl_pointer_set_cursor(input->parent.pointer, |
| 1292 | input->enter_serial, NULL, 0, 0); |
| 1293 | } else { |
| 1294 | input->focus = 0; |
| 1295 | notify_pointer_focus(&input->base, NULL, 0, 0); |
| 1296 | input_set_cursor(input); |
| 1297 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1301 | input_handle_pointer_leave(void *data, struct wl_pointer *pointer, |
| 1302 | uint32_t serial, struct wl_surface *surface) |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1303 | { |
| 1304 | struct wayland_input *input = data; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1305 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1306 | if (input->output->frame) { |
| 1307 | frame_pointer_leave(input->output->frame, input); |
| 1308 | |
| 1309 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 1310 | weston_output_schedule_repaint(&input->output->base); |
| 1311 | } |
| 1312 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1313 | notify_pointer_focus(&input->base, NULL, 0, 0); |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1314 | input->output = NULL; |
| 1315 | input->focus = 0; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1319 | input_handle_motion(void *data, struct wl_pointer *pointer, |
| 1320 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 1321 | { |
| 1322 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1323 | int32_t fx, fy; |
| 1324 | enum theme_location location; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1325 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1326 | if (input->output->frame) { |
| 1327 | location = frame_pointer_motion(input->output->frame, input, |
| 1328 | wl_fixed_to_int(x), |
| 1329 | wl_fixed_to_int(y)); |
| 1330 | frame_interior(input->output->frame, &fx, &fy, NULL, NULL); |
| 1331 | x -= wl_fixed_from_int(fx); |
| 1332 | y -= wl_fixed_from_int(fy); |
| 1333 | |
| 1334 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 1335 | weston_output_schedule_repaint(&input->output->base); |
| 1336 | } else { |
| 1337 | location = THEME_LOCATION_CLIENT_AREA; |
| 1338 | } |
| 1339 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1340 | weston_output_transform_coordinate(&input->output->base, x, y, &x, &y); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1341 | |
| 1342 | if (input->focus && location != THEME_LOCATION_CLIENT_AREA) { |
| 1343 | input_set_cursor(input); |
| 1344 | notify_pointer_focus(&input->base, NULL, 0, 0); |
| 1345 | input->focus = 0; |
| 1346 | } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) { |
| 1347 | wl_pointer_set_cursor(input->parent.pointer, |
| 1348 | input->enter_serial, NULL, 0, 0); |
| 1349 | notify_pointer_focus(&input->base, &input->output->base, x, y); |
| 1350 | input->focus = 1; |
| 1351 | } |
| 1352 | |
| 1353 | if (location == THEME_LOCATION_CLIENT_AREA) |
| 1354 | notify_motion_absolute(&input->base, time, x, y); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | static void |
| 1358 | input_handle_button(void *data, struct wl_pointer *pointer, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 1359 | uint32_t serial, uint32_t time, uint32_t button, |
| 1360 | uint32_t state_w) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1361 | { |
| 1362 | struct wayland_input *input = data; |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 1363 | enum wl_pointer_button_state state = state_w; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1364 | enum frame_button_state fstate; |
| 1365 | enum theme_location location; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1366 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1367 | if (input->output->frame) { |
| 1368 | fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ? |
| 1369 | FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED; |
| 1370 | |
| 1371 | location = frame_pointer_button(input->output->frame, input, |
| 1372 | button, fstate); |
| 1373 | |
| 1374 | if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) { |
| 1375 | |
| 1376 | wl_shell_surface_move(input->output->parent.shell_surface, |
| 1377 | input->parent.seat, serial); |
| 1378 | frame_status_clear(input->output->frame, |
| 1379 | FRAME_STATUS_MOVE); |
| 1380 | return; |
| 1381 | } |
| 1382 | |
| 1383 | if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) |
| 1384 | wl_display_terminate(input->compositor->base.wl_display); |
| 1385 | |
| 1386 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 1387 | weston_output_schedule_repaint(&input->output->base); |
| 1388 | } else { |
| 1389 | location = THEME_LOCATION_CLIENT_AREA; |
| 1390 | } |
| 1391 | |
| 1392 | if (location == THEME_LOCATION_CLIENT_AREA) |
| 1393 | notify_button(&input->base, time, button, state); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | static void |
| 1397 | input_handle_axis(void *data, struct wl_pointer *pointer, |
Daniel Stone | 2fce402 | 2012-05-30 16:32:00 +0100 | [diff] [blame] | 1398 | uint32_t time, uint32_t axis, wl_fixed_t value) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1399 | { |
| 1400 | struct wayland_input *input = data; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1401 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1402 | notify_axis(&input->base, time, axis, value); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | static const struct wl_pointer_listener pointer_listener = { |
| 1406 | input_handle_pointer_enter, |
| 1407 | input_handle_pointer_leave, |
| 1408 | input_handle_motion, |
| 1409 | input_handle_button, |
| 1410 | input_handle_axis, |
| 1411 | }; |
| 1412 | |
| 1413 | static void |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1414 | input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, |
| 1415 | int fd, uint32_t size) |
| 1416 | { |
| 1417 | struct wayland_input *input = data; |
| 1418 | struct xkb_keymap *keymap; |
| 1419 | char *map_str; |
| 1420 | |
Jason Ekstrand | b7d9f2e | 2014-04-02 19:53:57 -0500 | [diff] [blame] | 1421 | if (!data) |
| 1422 | goto error; |
| 1423 | |
| 1424 | if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { |
| 1425 | map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); |
| 1426 | if (map_str == MAP_FAILED) { |
| 1427 | weston_log("mmap failed: %m\n"); |
| 1428 | goto error; |
| 1429 | } |
| 1430 | |
| 1431 | keymap = xkb_map_new_from_string(input->compositor->base.xkb_context, |
| 1432 | map_str, |
| 1433 | XKB_KEYMAP_FORMAT_TEXT_V1, |
| 1434 | 0); |
| 1435 | munmap(map_str, size); |
| 1436 | |
| 1437 | if (!keymap) { |
| 1438 | weston_log("failed to compile keymap\n"); |
| 1439 | goto error; |
| 1440 | } |
| 1441 | |
| 1442 | input->keyboard_state_update = STATE_UPDATE_NONE; |
| 1443 | } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) { |
| 1444 | weston_log("No keymap provided; falling back to defalt\n"); |
| 1445 | keymap = NULL; |
| 1446 | input->keyboard_state_update = STATE_UPDATE_AUTOMATIC; |
| 1447 | } else { |
| 1448 | weston_log("Invalid keymap\n"); |
| 1449 | goto error; |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1450 | } |
| 1451 | |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1452 | close(fd); |
| 1453 | |
Rui Matos | 0c194ce | 2013-10-10 19:44:21 +0200 | [diff] [blame] | 1454 | if (input->base.keyboard) |
| 1455 | weston_seat_update_keymap(&input->base, keymap); |
| 1456 | else |
| 1457 | weston_seat_init_keyboard(&input->base, keymap); |
| 1458 | |
Jason Ekstrand | b7d9f2e | 2014-04-02 19:53:57 -0500 | [diff] [blame] | 1459 | if (keymap) |
| 1460 | xkb_map_unref(keymap); |
| 1461 | |
| 1462 | return; |
| 1463 | |
| 1464 | error: |
| 1465 | wl_keyboard_release(input->parent.keyboard); |
| 1466 | close(fd); |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | static void |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1470 | input_handle_keyboard_enter(void *data, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1471 | struct wl_keyboard *keyboard, |
| 1472 | uint32_t serial, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1473 | struct wl_surface *surface, |
| 1474 | struct wl_array *keys) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1475 | { |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 1476 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1477 | struct wayland_output *focus; |
| 1478 | |
| 1479 | focus = input->keyboard_focus; |
| 1480 | if (focus) { |
| 1481 | /* This shouldn't happen */ |
| 1482 | focus->keyboard_count--; |
| 1483 | if (!focus->keyboard_count && focus->frame) |
| 1484 | frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE); |
| 1485 | if (frame_status(focus->frame) & FRAME_STATUS_REPAINT) |
| 1486 | weston_output_schedule_repaint(&focus->base); |
| 1487 | } |
| 1488 | |
| 1489 | input->keyboard_focus = wl_surface_get_user_data(surface); |
| 1490 | input->keyboard_focus->keyboard_count++; |
| 1491 | |
| 1492 | focus = input->keyboard_focus; |
| 1493 | if (focus->frame) { |
| 1494 | frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE); |
| 1495 | if (frame_status(focus->frame) & FRAME_STATUS_REPAINT) |
| 1496 | weston_output_schedule_repaint(&focus->base); |
| 1497 | } |
| 1498 | |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 1499 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1500 | /* XXX: If we get a modifier event immediately before the focus, |
| 1501 | * we should try to keep the same serial. */ |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1502 | notify_keyboard_focus_in(&input->base, keys, |
Daniel Stone | d6da09e | 2012-06-22 13:21:29 +0100 | [diff] [blame] | 1503 | STATE_UPDATE_AUTOMATIC); |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | static void |
| 1507 | input_handle_keyboard_leave(void *data, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1508 | struct wl_keyboard *keyboard, |
| 1509 | uint32_t serial, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1510 | struct wl_surface *surface) |
| 1511 | { |
| 1512 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1513 | struct wayland_output *focus; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1514 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1515 | notify_keyboard_focus_out(&input->base); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1516 | |
| 1517 | focus = input->keyboard_focus; |
| 1518 | if (!focus) |
| 1519 | return; /* This shouldn't happen */ |
| 1520 | |
| 1521 | focus->keyboard_count--; |
| 1522 | if (!focus->keyboard_count && focus->frame) { |
| 1523 | frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE); |
| 1524 | if (frame_status(focus->frame) & FRAME_STATUS_REPAINT) |
| 1525 | weston_output_schedule_repaint(&focus->base); |
| 1526 | } |
| 1527 | |
| 1528 | input->keyboard_focus = NULL; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1529 | } |
| 1530 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1531 | static void |
| 1532 | input_handle_key(void *data, struct wl_keyboard *keyboard, |
| 1533 | uint32_t serial, uint32_t time, uint32_t key, uint32_t state) |
| 1534 | { |
| 1535 | struct wayland_input *input = data; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1536 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1537 | input->key_serial = serial; |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1538 | notify_key(&input->base, time, key, |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 1539 | state ? WL_KEYBOARD_KEY_STATE_PRESSED : |
Daniel Stone | 1b4e11f | 2012-06-22 13:21:37 +0100 | [diff] [blame] | 1540 | WL_KEYBOARD_KEY_STATE_RELEASED, |
Jason Ekstrand | b7d9f2e | 2014-04-02 19:53:57 -0500 | [diff] [blame] | 1541 | input->keyboard_state_update); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1542 | } |
| 1543 | |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1544 | static void |
| 1545 | input_handle_modifiers(void *data, struct wl_keyboard *keyboard, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1546 | uint32_t serial_in, uint32_t mods_depressed, |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1547 | uint32_t mods_latched, uint32_t mods_locked, |
| 1548 | uint32_t group) |
| 1549 | { |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1550 | struct wayland_input *input = data; |
| 1551 | struct wayland_compositor *c = input->compositor; |
| 1552 | uint32_t serial_out; |
| 1553 | |
| 1554 | /* If we get a key event followed by a modifier event with the |
| 1555 | * same serial number, then we try to preserve those semantics by |
| 1556 | * reusing the same serial number on the way out too. */ |
| 1557 | if (serial_in == input->key_serial) |
| 1558 | serial_out = wl_display_get_serial(c->base.wl_display); |
| 1559 | else |
| 1560 | serial_out = wl_display_next_serial(c->base.wl_display); |
| 1561 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1562 | xkb_state_update_mask(input->base.keyboard->xkb_state.state, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1563 | mods_depressed, mods_latched, |
| 1564 | mods_locked, 0, 0, group); |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1565 | notify_modifiers(&input->base, serial_out); |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1566 | } |
| 1567 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1568 | static const struct wl_keyboard_listener keyboard_listener = { |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1569 | input_handle_keymap, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1570 | input_handle_keyboard_enter, |
| 1571 | input_handle_keyboard_leave, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1572 | input_handle_key, |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1573 | input_handle_modifiers, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1574 | }; |
| 1575 | |
| 1576 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1577 | input_handle_capabilities(void *data, struct wl_seat *seat, |
| 1578 | enum wl_seat_capability caps) |
| 1579 | { |
| 1580 | struct wayland_input *input = data; |
| 1581 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1582 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) { |
| 1583 | input->parent.pointer = wl_seat_get_pointer(seat); |
| 1584 | wl_pointer_set_user_data(input->parent.pointer, input); |
| 1585 | wl_pointer_add_listener(input->parent.pointer, |
| 1586 | &pointer_listener, input); |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 1587 | weston_seat_init_pointer(&input->base); |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1588 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) { |
| 1589 | wl_pointer_destroy(input->parent.pointer); |
| 1590 | input->parent.pointer = NULL; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1591 | } |
| 1592 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1593 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) { |
| 1594 | input->parent.keyboard = wl_seat_get_keyboard(seat); |
| 1595 | wl_keyboard_set_user_data(input->parent.keyboard, input); |
| 1596 | wl_keyboard_add_listener(input->parent.keyboard, |
| 1597 | &keyboard_listener, input); |
| 1598 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) { |
| 1599 | wl_keyboard_destroy(input->parent.keyboard); |
| 1600 | input->parent.keyboard = NULL; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | static const struct wl_seat_listener seat_listener = { |
| 1605 | input_handle_capabilities, |
| 1606 | }; |
| 1607 | |
| 1608 | static void |
| 1609 | display_add_seat(struct wayland_compositor *c, uint32_t id) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1610 | { |
| 1611 | struct wayland_input *input; |
| 1612 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 1613 | input = zalloc(sizeof *input); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1614 | if (input == NULL) |
| 1615 | return; |
| 1616 | |
Rob Bradford | 9af5f9e | 2013-05-31 18:09:50 +0100 | [diff] [blame] | 1617 | weston_seat_init(&input->base, &c->base, "default"); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1618 | input->compositor = c; |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1619 | input->parent.seat = wl_registry_bind(c->parent.registry, id, |
| 1620 | &wl_seat_interface, 1); |
Jason Ekstrand | 06ced80 | 2013-11-07 20:13:29 -0600 | [diff] [blame] | 1621 | wl_list_insert(c->input_list.prev, &input->link); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1622 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1623 | wl_seat_add_listener(input->parent.seat, &seat_listener, input); |
| 1624 | wl_seat_set_user_data(input->parent.seat, input); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1625 | |
| 1626 | input->parent.cursor.surface = |
| 1627 | wl_compositor_create_surface(c->parent.compositor); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | static void |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1631 | wayland_parent_output_geometry(void *data, struct wl_output *output_proxy, |
| 1632 | int32_t x, int32_t y, |
| 1633 | int32_t physical_width, int32_t physical_height, |
| 1634 | int32_t subpixel, const char *make, |
| 1635 | const char *model, int32_t transform) |
| 1636 | { |
| 1637 | struct wayland_parent_output *output = data; |
| 1638 | |
| 1639 | output->x = x; |
| 1640 | output->y = y; |
| 1641 | output->physical.width = physical_width; |
| 1642 | output->physical.height = physical_height; |
| 1643 | output->physical.subpixel = subpixel; |
| 1644 | |
| 1645 | free(output->physical.make); |
| 1646 | output->physical.make = strdup(make); |
| 1647 | free(output->physical.model); |
| 1648 | output->physical.model = strdup(model); |
| 1649 | |
| 1650 | output->transform = transform; |
| 1651 | } |
| 1652 | |
| 1653 | static struct weston_mode * |
| 1654 | find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh) |
| 1655 | { |
| 1656 | struct weston_mode *mode; |
| 1657 | |
| 1658 | wl_list_for_each(mode, list, link) { |
| 1659 | if (mode->width == width && mode->height == height && |
| 1660 | mode->refresh == refresh) |
| 1661 | return mode; |
| 1662 | } |
| 1663 | |
| 1664 | mode = zalloc(sizeof *mode); |
| 1665 | if (!mode) |
| 1666 | return NULL; |
| 1667 | |
| 1668 | mode->width = width; |
| 1669 | mode->height = height; |
| 1670 | mode->refresh = refresh; |
| 1671 | wl_list_insert(list, &mode->link); |
| 1672 | |
| 1673 | return mode; |
| 1674 | } |
| 1675 | |
| 1676 | static void |
| 1677 | wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy, |
| 1678 | uint32_t flags, int32_t width, int32_t height, |
| 1679 | int32_t refresh) |
| 1680 | { |
| 1681 | struct wayland_parent_output *output = data; |
| 1682 | struct weston_mode *mode; |
| 1683 | |
| 1684 | if (output->output) { |
| 1685 | mode = find_mode(&output->output->base.mode_list, |
| 1686 | width, height, refresh); |
| 1687 | if (!mode) |
| 1688 | return; |
| 1689 | mode->flags = flags; |
| 1690 | /* Do a mode-switch on current mode change? */ |
| 1691 | } else { |
| 1692 | mode = find_mode(&output->mode_list, width, height, refresh); |
| 1693 | if (!mode) |
| 1694 | return; |
| 1695 | mode->flags = flags; |
| 1696 | if (flags & WL_OUTPUT_MODE_CURRENT) |
| 1697 | output->current_mode = mode; |
| 1698 | if (flags & WL_OUTPUT_MODE_PREFERRED) |
| 1699 | output->preferred_mode = mode; |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | static const struct wl_output_listener output_listener = { |
| 1704 | wayland_parent_output_geometry, |
| 1705 | wayland_parent_output_mode |
| 1706 | }; |
| 1707 | |
| 1708 | static void |
| 1709 | wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id) |
| 1710 | { |
| 1711 | struct wayland_parent_output *output; |
| 1712 | |
| 1713 | output = zalloc(sizeof *output); |
| 1714 | if (!output) |
| 1715 | return; |
| 1716 | |
| 1717 | output->id = id; |
| 1718 | output->global = wl_registry_bind(c->parent.registry, id, |
| 1719 | &wl_output_interface, 1); |
| 1720 | if (!output->global) |
| 1721 | return; |
| 1722 | wl_output_add_listener(output->global, &output_listener, output); |
| 1723 | |
| 1724 | output->scale = 0; |
| 1725 | output->transform = WL_OUTPUT_TRANSFORM_NORMAL; |
| 1726 | output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 1727 | wl_list_init(&output->mode_list); |
| 1728 | wl_list_insert(&c->parent.output_list, &output->link); |
| 1729 | |
Jason Ekstrand | e4ca8b0 | 2014-04-02 19:53:55 -0500 | [diff] [blame] | 1730 | if (c->sprawl_across_outputs) { |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1731 | wl_display_roundtrip(c->parent.wl_display); |
| 1732 | wayland_output_create_for_parent_output(c, output); |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | static void |
| 1737 | wayland_parent_output_destroy(struct wayland_parent_output *output) |
| 1738 | { |
| 1739 | struct weston_mode *mode, *next; |
| 1740 | |
| 1741 | if (output->output) |
| 1742 | wayland_output_destroy(&output->output->base); |
| 1743 | |
| 1744 | wl_output_destroy(output->global); |
| 1745 | free(output->physical.make); |
| 1746 | free(output->physical.model); |
| 1747 | |
| 1748 | wl_list_for_each_safe(mode, next, &output->mode_list, link) { |
| 1749 | wl_list_remove(&mode->link); |
| 1750 | free(mode); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1755 | registry_handle_global(void *data, struct wl_registry *registry, uint32_t name, |
| 1756 | const char *interface, uint32_t version) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1757 | { |
| 1758 | struct wayland_compositor *c = data; |
| 1759 | |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 1760 | if (strcmp(interface, "wl_compositor") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 1761 | c->parent.compositor = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1762 | wl_registry_bind(registry, name, |
| 1763 | &wl_compositor_interface, 1); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 1764 | } else if (strcmp(interface, "wl_shell") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 1765 | c->parent.shell = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1766 | wl_registry_bind(registry, name, |
| 1767 | &wl_shell_interface, 1); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1768 | } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) { |
| 1769 | c->parent.fshell = |
| 1770 | wl_registry_bind(registry, name, |
| 1771 | &_wl_fullscreen_shell_interface, 1); |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1772 | } else if (strcmp(interface, "wl_seat") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1773 | display_add_seat(c, name); |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1774 | } else if (strcmp(interface, "wl_output") == 0) { |
| 1775 | wayland_compositor_register_output(c, name); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 1776 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 1777 | c->parent.shm = |
| 1778 | wl_registry_bind(registry, name, &wl_shm_interface, 1); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1779 | } |
| 1780 | } |
| 1781 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1782 | static void |
| 1783 | registry_handle_global_remove(void *data, struct wl_registry *registry, |
| 1784 | uint32_t name) |
| 1785 | { |
| 1786 | struct wayland_compositor *c = data; |
| 1787 | struct wayland_parent_output *output; |
| 1788 | |
| 1789 | wl_list_for_each(output, &c->parent.output_list, link) |
| 1790 | if (output->id == name) |
| 1791 | wayland_parent_output_destroy(output); |
| 1792 | } |
| 1793 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1794 | static const struct wl_registry_listener registry_listener = { |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1795 | registry_handle_global, |
| 1796 | registry_handle_global_remove |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1797 | }; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1798 | |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 1799 | static int |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1800 | wayland_compositor_handle_event(int fd, uint32_t mask, void *data) |
| 1801 | { |
| 1802 | struct wayland_compositor *c = data; |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 1803 | int count = 0; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1804 | |
Kristian Høgsberg | 453de7a | 2013-10-30 23:15:44 -0700 | [diff] [blame] | 1805 | if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) { |
| 1806 | wl_display_terminate(c->base.wl_display); |
| 1807 | return 0; |
| 1808 | } |
| 1809 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1810 | if (mask & WL_EVENT_READABLE) |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 1811 | count = wl_display_dispatch(c->parent.wl_display); |
Kristian Høgsberg | f258a31 | 2011-12-28 22:51:20 -0500 | [diff] [blame] | 1812 | if (mask & WL_EVENT_WRITABLE) |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1813 | wl_display_flush(c->parent.wl_display); |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 1814 | |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 1815 | if (mask == 0) { |
| 1816 | count = wl_display_dispatch_pending(c->parent.wl_display); |
| 1817 | wl_display_flush(c->parent.wl_display); |
| 1818 | } |
| 1819 | |
| 1820 | return count; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1821 | } |
| 1822 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1823 | static void |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 1824 | wayland_restore(struct weston_compositor *ec) |
| 1825 | { |
| 1826 | } |
| 1827 | |
| 1828 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1829 | wayland_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1830 | { |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 1831 | struct wayland_compositor *c = (struct wayland_compositor *) ec; |
| 1832 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1833 | weston_compositor_shutdown(ec); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1834 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 1835 | if (c->parent.shm) |
| 1836 | wl_shm_destroy(c->parent.shm); |
| 1837 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1838 | free(ec); |
| 1839 | } |
| 1840 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1841 | static const char *left_ptrs[] = { |
| 1842 | "left_ptr", |
| 1843 | "default", |
| 1844 | "top_left_arrow", |
| 1845 | "left-arrow" |
| 1846 | }; |
| 1847 | |
| 1848 | static void |
| 1849 | create_cursor(struct wayland_compositor *c, struct weston_config *config) |
| 1850 | { |
| 1851 | struct weston_config_section *s; |
| 1852 | int size; |
| 1853 | char *theme = NULL; |
| 1854 | unsigned int i; |
| 1855 | |
| 1856 | s = weston_config_get_section(config, "shell", NULL, NULL); |
| 1857 | weston_config_section_get_string(s, "cursor-theme", &theme, NULL); |
| 1858 | weston_config_section_get_int(s, "cursor-size", &size, 32); |
| 1859 | |
| 1860 | c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm); |
Hardening | 842a36a | 2014-03-18 14:12:50 +0100 | [diff] [blame] | 1861 | if (!c->cursor_theme) { |
| 1862 | fprintf(stderr, "could not load cursor theme\n"); |
| 1863 | return; |
| 1864 | } |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1865 | |
U. Artie Eoff | ff75500 | 2014-01-17 12:36:58 -0800 | [diff] [blame] | 1866 | free(theme); |
| 1867 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1868 | c->cursor = NULL; |
| 1869 | for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i) |
| 1870 | c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme, |
| 1871 | left_ptrs[i]); |
| 1872 | if (!c->cursor) { |
| 1873 | fprintf(stderr, "could not load left cursor\n"); |
| 1874 | return; |
| 1875 | } |
| 1876 | } |
| 1877 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1878 | static void |
| 1879 | fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key, |
| 1880 | void *data) |
| 1881 | { |
| 1882 | struct wayland_compositor *c = data; |
| 1883 | struct wayland_input *input = NULL; |
| 1884 | |
| 1885 | wl_list_for_each(input, &c->input_list, link) |
| 1886 | if (&input->base == seat_base) |
| 1887 | break; |
| 1888 | |
| 1889 | if (!input || !input->output) |
| 1890 | return; |
| 1891 | |
| 1892 | if (input->output->frame) |
| 1893 | wayland_output_set_fullscreen(input->output, 0, 0, NULL); |
| 1894 | else |
| 1895 | wayland_output_set_windowed(input->output); |
| 1896 | |
| 1897 | weston_output_schedule_repaint(&input->output->base); |
| 1898 | } |
| 1899 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1900 | static struct wayland_compositor * |
| 1901 | wayland_compositor_create(struct wl_display *display, int use_pixman, |
| 1902 | const char *display_name, int *argc, char *argv[], |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 1903 | struct weston_config *config) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1904 | { |
| 1905 | struct wayland_compositor *c; |
| 1906 | struct wl_event_loop *loop; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1907 | int fd; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1908 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 1909 | c = zalloc(sizeof *c); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1910 | if (c == NULL) |
| 1911 | return NULL; |
| 1912 | |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1913 | if (weston_compositor_init(&c->base, display, argc, argv, |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 1914 | config) < 0) |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 1915 | goto err_free; |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1916 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1917 | c->parent.wl_display = wl_display_connect(display_name); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1918 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1919 | if (c->parent.wl_display == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1920 | weston_log("failed to create display: %m\n"); |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 1921 | goto err_compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1922 | } |
| 1923 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 1924 | wl_list_init(&c->parent.output_list); |
Jason Ekstrand | 06ced80 | 2013-11-07 20:13:29 -0600 | [diff] [blame] | 1925 | wl_list_init(&c->input_list); |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1926 | c->parent.registry = wl_display_get_registry(c->parent.wl_display); |
| 1927 | wl_registry_add_listener(c->parent.registry, ®istry_listener, c); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1928 | wl_display_roundtrip(c->parent.wl_display); |
| 1929 | |
| 1930 | create_cursor(c, config); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1931 | |
| 1932 | c->base.wl_display = display; |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 1933 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1934 | c->use_pixman = use_pixman; |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 1935 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1936 | if (!c->use_pixman) { |
| 1937 | gl_renderer = weston_load_module("gl-renderer.so", |
| 1938 | "gl_renderer_interface"); |
| 1939 | if (!gl_renderer) |
| 1940 | c->use_pixman = 1; |
| 1941 | } |
| 1942 | |
| 1943 | if (!c->use_pixman) { |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 1944 | if (gl_renderer->create(&c->base, c->parent.wl_display, |
| 1945 | gl_renderer->alpha_attribs, |
| 1946 | NULL) < 0) { |
| 1947 | weston_log("Failed to initialize the GL renderer; " |
| 1948 | "falling back to pixman.\n"); |
| 1949 | c->use_pixman = 1; |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | if (c->use_pixman) { |
| 1954 | if (pixman_renderer_init(&c->base) < 0) { |
| 1955 | weston_log("Failed to initialize pixman renderer\n"); |
| 1956 | goto err_display; |
| 1957 | } |
| 1958 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1959 | |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 1960 | c->base.destroy = wayland_destroy; |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 1961 | c->base.restore = wayland_restore; |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 1962 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1963 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 1964 | |
| 1965 | fd = wl_display_get_fd(c->parent.wl_display); |
| 1966 | c->parent.wl_source = |
| 1967 | wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE, |
| 1968 | wayland_compositor_handle_event, c); |
| 1969 | if (c->parent.wl_source == NULL) |
| 1970 | goto err_renderer; |
| 1971 | |
| 1972 | wl_event_source_check(c->parent.wl_source); |
| 1973 | |
| 1974 | return c; |
| 1975 | err_renderer: |
| 1976 | c->base.renderer->destroy(&c->base); |
| 1977 | err_display: |
| 1978 | wl_display_disconnect(c->parent.wl_display); |
| 1979 | err_compositor: |
| 1980 | weston_compositor_shutdown(&c->base); |
| 1981 | err_free: |
| 1982 | free(c); |
| 1983 | return NULL; |
| 1984 | } |
| 1985 | |
| 1986 | static void |
| 1987 | wayland_compositor_destroy(struct wayland_compositor *c) |
| 1988 | { |
| 1989 | struct weston_output *output; |
| 1990 | |
| 1991 | wl_list_for_each(output, &c->base.output_list, link) |
| 1992 | wayland_output_destroy(output); |
| 1993 | |
| 1994 | c->base.renderer->destroy(&c->base); |
| 1995 | wl_display_disconnect(c->parent.wl_display); |
| 1996 | |
| 1997 | if (c->theme) |
| 1998 | theme_destroy(c->theme); |
| 1999 | if (c->frame_device) |
| 2000 | cairo_device_destroy(c->frame_device); |
| 2001 | wl_cursor_theme_destroy(c->cursor_theme); |
| 2002 | |
| 2003 | weston_compositor_shutdown(&c->base); |
| 2004 | free(c); |
| 2005 | } |
| 2006 | |
| 2007 | WL_EXPORT struct weston_compositor * |
| 2008 | backend_init(struct wl_display *display, int *argc, char *argv[], |
| 2009 | struct weston_config *config) |
| 2010 | { |
| 2011 | struct wayland_compositor *c; |
| 2012 | struct wayland_output *output; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2013 | struct wayland_parent_output *poutput; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2014 | struct weston_config_section *section; |
Jason Ekstrand | e4ca8b0 | 2014-04-02 19:53:55 -0500 | [diff] [blame] | 2015 | int x, count, width, height, scale, use_pixman, fullscreen, sprawl; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2016 | const char *section_name, *display_name; |
| 2017 | char *name; |
| 2018 | |
| 2019 | const struct weston_option wayland_options[] = { |
| 2020 | { WESTON_OPTION_INTEGER, "width", 0, &width }, |
| 2021 | { WESTON_OPTION_INTEGER, "height", 0, &height }, |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame] | 2022 | { WESTON_OPTION_INTEGER, "scale", 0, &scale }, |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2023 | { WESTON_OPTION_STRING, "display", 0, &display_name }, |
| 2024 | { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman }, |
| 2025 | { WESTON_OPTION_INTEGER, "output-count", 0, &count }, |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 2026 | { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen }, |
Jason Ekstrand | e4ca8b0 | 2014-04-02 19:53:55 -0500 | [diff] [blame] | 2027 | { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl }, |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2028 | }; |
| 2029 | |
| 2030 | width = 0; |
| 2031 | height = 0; |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame] | 2032 | scale = 0; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2033 | display_name = NULL; |
| 2034 | use_pixman = 0; |
| 2035 | count = 1; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 2036 | fullscreen = 0; |
Jason Ekstrand | e4ca8b0 | 2014-04-02 19:53:55 -0500 | [diff] [blame] | 2037 | sprawl = 0; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2038 | parse_options(wayland_options, |
| 2039 | ARRAY_LENGTH(wayland_options), argc, argv); |
| 2040 | |
| 2041 | c = wayland_compositor_create(display, use_pixman, display_name, |
| 2042 | argc, argv, config); |
| 2043 | if (!c) |
| 2044 | return NULL; |
| 2045 | |
Jason Ekstrand | e4ca8b0 | 2014-04-02 19:53:55 -0500 | [diff] [blame] | 2046 | if (sprawl || c->parent.fshell) { |
| 2047 | c->sprawl_across_outputs = 1; |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2048 | wl_display_roundtrip(c->parent.wl_display); |
| 2049 | |
| 2050 | wl_list_for_each(poutput, &c->parent.output_list, link) |
| 2051 | wayland_output_create_for_parent_output(c, poutput); |
| 2052 | |
| 2053 | return &c->base; |
| 2054 | } |
| 2055 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 2056 | if (fullscreen) { |
| 2057 | output = wayland_output_create(c, 0, 0, width, height, |
| 2058 | NULL, 1, 0, 1); |
| 2059 | if (!output) |
| 2060 | goto err_outputs; |
| 2061 | |
Axel Davy | dd8b88d | 2013-11-17 21:34:16 +0100 | [diff] [blame] | 2062 | wayland_output_set_fullscreen(output, 0, 0, NULL); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 2063 | return &c->base; |
| 2064 | } |
| 2065 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 2066 | section = NULL; |
| 2067 | x = 0; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 2068 | while (weston_config_next_section(config, §ion, §ion_name)) { |
| 2069 | if (!section_name || strcmp(section_name, "output") != 0) |
| 2070 | continue; |
| 2071 | weston_config_section_get_string(section, "name", &name, NULL); |
| 2072 | if (name == NULL) |
| 2073 | continue; |
| 2074 | |
| 2075 | if (name[0] != 'W' || name[1] != 'L') { |
| 2076 | free(name); |
| 2077 | continue; |
| 2078 | } |
| 2079 | free(name); |
| 2080 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2081 | output = wayland_output_create_for_config(c, section, width, |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame] | 2082 | height, scale, x, 0); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2083 | if (!output) |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 2084 | goto err_outputs; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 2085 | if (wayland_output_set_windowed(output)) |
| 2086 | goto err_outputs; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2087 | |
| 2088 | x += output->base.width; |
| 2089 | --count; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 2090 | } |
| 2091 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2092 | if (!width) |
| 2093 | width = 1024; |
| 2094 | if (!height) |
| 2095 | height = 640; |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame] | 2096 | if (!scale) |
| 2097 | scale = 1; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2098 | while (count > 0) { |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame] | 2099 | output = wayland_output_create(c, x, 0, width, height, |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 2100 | NULL, 0, 0, scale); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2101 | if (!output) |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 2102 | goto err_outputs; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 2103 | if (wayland_output_set_windowed(output)) |
| 2104 | goto err_outputs; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2105 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 2106 | x += width; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2107 | --count; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 2108 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2109 | |
Jason Ekstrand | 53ee0dc | 2014-04-02 19:53:54 -0500 | [diff] [blame] | 2110 | weston_compositor_add_key_binding(&c->base, KEY_F, |
| 2111 | MODIFIER_CTRL | MODIFIER_ALT, |
| 2112 | fullscreen_binding, c); |
| 2113 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2114 | return &c->base; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2115 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 2116 | err_outputs: |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 2117 | wayland_compositor_destroy(c); |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 2118 | return NULL; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 2119 | } |