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