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> |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 33 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 34 | #include <wayland-client.h> |
| 35 | #include <wayland-egl.h> |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 36 | #include <wayland-cursor.h> |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 37 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 38 | #include "compositor.h" |
John Kåre Alsaker | 30d2b1f | 2012-11-13 19:10:28 +0100 | [diff] [blame] | 39 | #include "gl-renderer.h" |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 40 | #include "pixman-renderer.h" |
Kristian Høgsberg | 3c2360f | 2013-01-28 16:01:22 -0500 | [diff] [blame] | 41 | #include "../shared/image-loader.h" |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 42 | #include "../shared/os-compatibility.h" |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 43 | #include "../shared/cairo-util.h" |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 44 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 45 | #define WINDOW_TITLE "Weston Compositor" |
| 46 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 47 | struct wayland_compositor { |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 48 | struct weston_compositor base; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 49 | |
| 50 | struct { |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 51 | struct wl_display *wl_display; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 52 | struct wl_registry *registry; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 53 | struct wl_compositor *compositor; |
| 54 | struct wl_shell *shell; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 55 | struct wl_shm *shm; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 56 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 57 | struct wl_event_source *wl_source; |
| 58 | uint32_t event_mask; |
| 59 | } parent; |
| 60 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 61 | int use_pixman; |
| 62 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 63 | struct theme *theme; |
| 64 | cairo_device_t *frame_device; |
| 65 | struct wl_cursor_theme *cursor_theme; |
| 66 | struct wl_cursor *cursor; |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 67 | |
Jason Ekstrand | 06ced80 | 2013-11-07 20:13:29 -0600 | [diff] [blame] | 68 | struct wl_list input_list; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | struct wayland_output { |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 72 | struct weston_output base; |
| 73 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 74 | struct { |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 75 | int draw_initial_frame; |
| 76 | struct wl_surface *surface; |
| 77 | struct wl_shell_surface *shell_surface; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 78 | } parent; |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 79 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 80 | int keyboard_count; |
| 81 | |
| 82 | struct frame *frame; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 83 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 84 | struct { |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 85 | struct wl_egl_window *egl_window; |
| 86 | struct { |
| 87 | cairo_surface_t *top; |
| 88 | cairo_surface_t *left; |
| 89 | cairo_surface_t *right; |
| 90 | cairo_surface_t *bottom; |
| 91 | } border; |
| 92 | } gl; |
| 93 | |
| 94 | struct { |
| 95 | struct wl_list buffers; |
| 96 | struct wl_list free_buffers; |
| 97 | } shm; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 98 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 99 | struct weston_mode mode; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 100 | uint32_t scale; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 101 | }; |
| 102 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 103 | struct wayland_shm_buffer { |
| 104 | struct wayland_output *output; |
| 105 | struct wl_list link; |
| 106 | struct wl_list free_link; |
| 107 | |
| 108 | struct wl_buffer *buffer; |
| 109 | void *data; |
| 110 | size_t size; |
| 111 | pixman_region32_t damage; |
| 112 | int frame_damaged; |
| 113 | |
| 114 | pixman_image_t *pm_image; |
| 115 | cairo_surface_t *c_surface; |
| 116 | }; |
| 117 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 118 | struct wayland_input { |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 119 | struct weston_seat base; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 120 | struct wayland_compositor *compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 121 | struct wl_list link; |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 122 | |
| 123 | struct { |
| 124 | struct wl_seat *seat; |
| 125 | struct wl_pointer *pointer; |
| 126 | struct wl_keyboard *keyboard; |
| 127 | struct wl_touch *touch; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 128 | |
| 129 | struct { |
| 130 | struct wl_surface *surface; |
| 131 | int32_t hx, hy; |
| 132 | } cursor; |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 133 | } parent; |
| 134 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 135 | uint32_t key_serial; |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 136 | uint32_t enter_serial; |
| 137 | int focus; |
| 138 | struct wayland_output *output; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 139 | struct wayland_output *keyboard_focus; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 140 | }; |
| 141 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 142 | struct gl_renderer_interface *gl_renderer; |
| 143 | |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 144 | static void |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 145 | wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer) |
| 146 | { |
| 147 | cairo_surface_destroy(buffer->c_surface); |
| 148 | pixman_image_unref(buffer->pm_image); |
| 149 | |
| 150 | wl_buffer_destroy(buffer->buffer); |
| 151 | munmap(buffer->data, buffer->size); |
| 152 | |
| 153 | pixman_region32_fini(&buffer->damage); |
| 154 | |
| 155 | wl_list_remove(&buffer->link); |
| 156 | wl_list_remove(&buffer->free_link); |
| 157 | free(buffer); |
| 158 | } |
| 159 | |
| 160 | static void |
| 161 | buffer_release(void *data, struct wl_buffer *buffer) |
| 162 | { |
| 163 | struct wayland_shm_buffer *sb = data; |
| 164 | |
| 165 | if (sb->output) { |
| 166 | wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link); |
| 167 | } else { |
| 168 | wayland_shm_buffer_destroy(sb); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | static const struct wl_buffer_listener buffer_listener = { |
| 173 | buffer_release |
| 174 | }; |
| 175 | |
| 176 | static struct wayland_shm_buffer * |
| 177 | wayland_output_get_shm_buffer(struct wayland_output *output) |
| 178 | { |
| 179 | struct wayland_compositor *c = |
| 180 | (struct wayland_compositor *) output->base.compositor; |
| 181 | struct wl_shm *shm = c->parent.shm; |
| 182 | struct wayland_shm_buffer *sb; |
| 183 | |
| 184 | struct wl_shm_pool *pool; |
| 185 | int width, height, stride; |
| 186 | int32_t fx, fy; |
| 187 | int fd; |
| 188 | unsigned char *data; |
| 189 | |
| 190 | if (!wl_list_empty(&output->shm.free_buffers)) { |
| 191 | sb = container_of(output->shm.free_buffers.next, |
| 192 | struct wayland_shm_buffer, free_link); |
| 193 | wl_list_remove(&sb->free_link); |
| 194 | wl_list_init(&sb->free_link); |
| 195 | |
| 196 | return sb; |
| 197 | } |
| 198 | |
| 199 | if (output->frame) { |
| 200 | width = frame_width(output->frame); |
| 201 | height = frame_height(output->frame); |
| 202 | } else { |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 203 | width = output->base.current_mode->width; |
| 204 | height = output->base.current_mode->height; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width); |
| 208 | |
| 209 | fd = os_create_anonymous_file(height * stride); |
| 210 | if (fd < 0) { |
| 211 | perror("os_create_anonymous_file"); |
| 212 | return NULL; |
| 213 | } |
| 214 | |
| 215 | data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 216 | if (data == MAP_FAILED) { |
| 217 | perror("mmap"); |
| 218 | close(fd); |
| 219 | return NULL; |
| 220 | } |
| 221 | |
| 222 | sb = zalloc(sizeof *sb); |
| 223 | |
| 224 | sb->output = output; |
| 225 | wl_list_init(&sb->free_link); |
| 226 | wl_list_insert(&output->shm.buffers, &sb->link); |
| 227 | |
| 228 | pixman_region32_init_rect(&sb->damage, 0, 0, |
| 229 | output->base.width, output->base.height); |
| 230 | sb->frame_damaged = 1; |
| 231 | |
| 232 | sb->data = data; |
| 233 | sb->size = height * stride; |
| 234 | |
| 235 | pool = wl_shm_create_pool(shm, fd, sb->size); |
| 236 | |
| 237 | sb->buffer = wl_shm_pool_create_buffer(pool, 0, |
| 238 | width, height, |
| 239 | stride, |
| 240 | WL_SHM_FORMAT_ARGB8888); |
| 241 | wl_buffer_add_listener(sb->buffer, &buffer_listener, sb); |
| 242 | wl_shm_pool_destroy(pool); |
| 243 | close(fd); |
| 244 | |
| 245 | memset(data, 0, sb->size); |
| 246 | |
| 247 | sb->c_surface = |
| 248 | cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, |
| 249 | width, height, stride); |
| 250 | |
| 251 | fx = 0; |
| 252 | fy = 0; |
| 253 | if (output->frame) |
| 254 | frame_interior(output->frame, &fx, &fy, 0, 0); |
| 255 | sb->pm_image = |
| 256 | pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height, |
| 257 | (uint32_t *)(data + fy * stride) + fx, |
| 258 | stride); |
| 259 | |
| 260 | return sb; |
| 261 | } |
| 262 | |
| 263 | static void |
Kristian Høgsberg | cdd61d0 | 2012-02-07 09:56:15 -0500 | [diff] [blame] | 264 | frame_done(void *data, struct wl_callback *callback, uint32_t time) |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 265 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 266 | struct weston_output *output = data; |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 267 | |
Kristian Høgsberg | cdd61d0 | 2012-02-07 09:56:15 -0500 | [diff] [blame] | 268 | wl_callback_destroy(callback); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 269 | weston_output_finish_frame(output, time); |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static const struct wl_callback_listener frame_listener = { |
| 273 | frame_done |
| 274 | }; |
| 275 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 276 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 277 | draw_initial_frame(struct wayland_output *output) |
| 278 | { |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 279 | struct wayland_shm_buffer *sb; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 280 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 281 | sb = wayland_output_get_shm_buffer(output); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 282 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 283 | /* If we are rendering with GL, then orphan it so that it gets |
| 284 | * destroyed immediately */ |
| 285 | if (output->gl.egl_window) |
| 286 | sb->output = NULL; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 287 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 288 | wl_surface_attach(output->parent.surface, sb->buffer, 0, 0); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 289 | |
| 290 | /* We only need to damage some part, as its only transparant |
| 291 | * pixels anyway. */ |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 292 | wl_surface_damage(output->parent.surface, 0, 0, 1, 1); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static void |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 296 | wayland_output_update_gl_border(struct wayland_output *output) |
| 297 | { |
| 298 | int32_t ix, iy, iwidth, iheight, fwidth, fheight; |
| 299 | cairo_t *cr; |
| 300 | |
| 301 | if (!output->frame) |
| 302 | return; |
| 303 | if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT)) |
| 304 | return; |
| 305 | |
| 306 | fwidth = frame_width(output->frame); |
| 307 | fheight = frame_height(output->frame); |
| 308 | frame_interior(output->frame, &ix, &iy, &iwidth, &iheight); |
| 309 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 310 | if (!output->gl.border.top) |
| 311 | output->gl.border.top = |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 312 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 313 | fwidth, iy); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 314 | cr = cairo_create(output->gl.border.top); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 315 | frame_repaint(output->frame, cr); |
| 316 | cairo_destroy(cr); |
| 317 | gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP, |
| 318 | fwidth, iy, |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 319 | cairo_image_surface_get_stride(output->gl.border.top) / 4, |
| 320 | cairo_image_surface_get_data(output->gl.border.top)); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 321 | |
| 322 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 323 | if (!output->gl.border.left) |
| 324 | output->gl.border.left = |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 325 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 326 | ix, 1); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 327 | cr = cairo_create(output->gl.border.left); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 328 | cairo_translate(cr, 0, -iy); |
| 329 | frame_repaint(output->frame, cr); |
| 330 | cairo_destroy(cr); |
| 331 | gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT, |
| 332 | ix, 1, |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 333 | cairo_image_surface_get_stride(output->gl.border.left) / 4, |
| 334 | cairo_image_surface_get_data(output->gl.border.left)); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 335 | |
| 336 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 337 | if (!output->gl.border.right) |
| 338 | output->gl.border.right = |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 339 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 340 | fwidth - (ix + iwidth), 1); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 341 | cr = cairo_create(output->gl.border.right); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 342 | cairo_translate(cr, -(iwidth + ix), -iy); |
| 343 | frame_repaint(output->frame, cr); |
| 344 | cairo_destroy(cr); |
| 345 | gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT, |
| 346 | fwidth - (ix + iwidth), 1, |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 347 | cairo_image_surface_get_stride(output->gl.border.right) / 4, |
| 348 | cairo_image_surface_get_data(output->gl.border.right)); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 349 | |
| 350 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 351 | if (!output->gl.border.bottom) |
| 352 | output->gl.border.bottom = |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 353 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 354 | fwidth, fheight - (iy + iheight)); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 355 | cr = cairo_create(output->gl.border.bottom); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 356 | cairo_translate(cr, 0, -(iy + iheight)); |
| 357 | frame_repaint(output->frame, cr); |
| 358 | cairo_destroy(cr); |
| 359 | gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM, |
| 360 | fwidth, fheight - (iy + iheight), |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 361 | cairo_image_surface_get_stride(output->gl.border.bottom) / 4, |
| 362 | cairo_image_surface_get_data(output->gl.border.bottom)); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 366 | wayland_output_start_repaint_loop(struct weston_output *output_base) |
| 367 | { |
| 368 | struct wayland_output *output = (struct wayland_output *) output_base; |
Jason Ekstrand | 286ff68 | 2013-10-27 22:24:57 -0500 | [diff] [blame] | 369 | struct wayland_compositor *wc = |
| 370 | (struct wayland_compositor *)output->base.compositor; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 371 | struct wl_callback *callback; |
| 372 | |
| 373 | /* If this is the initial frame, we need to attach a buffer so that |
| 374 | * the compositor can map the surface and include it in its render |
| 375 | * loop. If the surface doesn't end up in the render loop, the frame |
| 376 | * callback won't be invoked. The buffer is transparent and of the |
| 377 | * same size as the future real output buffer. */ |
| 378 | if (output->parent.draw_initial_frame) { |
| 379 | output->parent.draw_initial_frame = 0; |
| 380 | |
| 381 | draw_initial_frame(output); |
| 382 | } |
| 383 | |
| 384 | callback = wl_surface_frame(output->parent.surface); |
| 385 | wl_callback_add_listener(callback, &frame_listener, output); |
| 386 | wl_surface_commit(output->parent.surface); |
Jason Ekstrand | 286ff68 | 2013-10-27 22:24:57 -0500 | [diff] [blame] | 387 | wl_display_flush(wc->parent.wl_display); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 388 | } |
| 389 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 390 | static int |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 391 | wayland_output_repaint_gl(struct weston_output *output_base, |
| 392 | pixman_region32_t *damage) |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 393 | { |
| 394 | struct wayland_output *output = (struct wayland_output *) output_base; |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame] | 395 | struct weston_compositor *ec = output->base.compositor; |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 396 | struct wl_callback *callback; |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 397 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 398 | callback = wl_surface_frame(output->parent.surface); |
| 399 | wl_callback_add_listener(callback, &frame_listener, output); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 400 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 401 | wayland_output_update_gl_border(output); |
| 402 | |
Pekka Paalanen | bc10638 | 2012-10-10 12:49:31 +0300 | [diff] [blame] | 403 | ec->renderer->repaint_output(&output->base, damage); |
Ander Conselvan de Oliveira | 0a88772 | 2012-11-22 15:57:00 +0200 | [diff] [blame] | 404 | |
| 405 | pixman_region32_subtract(&ec->primary_plane.damage, |
| 406 | &ec->primary_plane.damage, damage); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 407 | return 0; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 408 | } |
| 409 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 410 | static void |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 411 | wayland_output_update_shm_border(struct wayland_shm_buffer *buffer) |
| 412 | { |
| 413 | int32_t ix, iy, iwidth, iheight, fwidth, fheight; |
| 414 | cairo_t *cr; |
| 415 | |
| 416 | if (!buffer->output->frame || !buffer->frame_damaged) |
| 417 | return; |
| 418 | |
| 419 | cr = cairo_create(buffer->c_surface); |
| 420 | |
| 421 | frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight); |
| 422 | fwidth = frame_width(buffer->output->frame); |
| 423 | fheight = frame_height(buffer->output->frame); |
| 424 | |
| 425 | /* Set the clip so we don't unnecisaraly damage the surface */ |
| 426 | cairo_move_to(cr, ix, iy); |
| 427 | cairo_rel_line_to(cr, iwidth, 0); |
| 428 | cairo_rel_line_to(cr, 0, iheight); |
| 429 | cairo_rel_line_to(cr, -iwidth, 0); |
| 430 | cairo_line_to(cr, ix, iy); |
| 431 | cairo_line_to(cr, 0, iy); |
| 432 | cairo_line_to(cr, 0, fheight); |
| 433 | cairo_line_to(cr, fwidth, fheight); |
| 434 | cairo_line_to(cr, fwidth, 0); |
| 435 | cairo_line_to(cr, 0, 0); |
| 436 | cairo_line_to(cr, 0, iy); |
| 437 | cairo_close_path(cr); |
| 438 | cairo_clip(cr); |
| 439 | |
| 440 | /* Draw using a pattern so that the final result gets clipped */ |
| 441 | cairo_push_group(cr); |
| 442 | frame_repaint(buffer->output->frame, cr); |
| 443 | cairo_pop_group_to_source(cr); |
| 444 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 445 | cairo_paint(cr); |
| 446 | |
| 447 | cairo_destroy(cr); |
| 448 | } |
| 449 | |
| 450 | static void |
| 451 | wayland_shm_buffer_attach(struct wayland_shm_buffer *sb) |
| 452 | { |
| 453 | pixman_region32_t damage; |
| 454 | pixman_box32_t *rects; |
| 455 | int32_t ix, iy, iwidth, iheight, fwidth, fheight; |
| 456 | int i, n; |
| 457 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 458 | pixman_region32_init(&damage); |
| 459 | weston_transformed_region(sb->output->base.width, |
| 460 | sb->output->base.height, |
| 461 | sb->output->base.transform, |
| 462 | sb->output->base.current_scale, |
| 463 | &sb->damage, &damage); |
| 464 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 465 | if (sb->output->frame) { |
| 466 | frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight); |
| 467 | fwidth = frame_width(sb->output->frame); |
| 468 | fheight = frame_height(sb->output->frame); |
| 469 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 470 | pixman_region32_translate(&damage, ix, iy); |
| 471 | |
| 472 | if (sb->frame_damaged) { |
| 473 | pixman_region32_union_rect(&damage, &damage, |
| 474 | 0, 0, fwidth, iy); |
| 475 | pixman_region32_union_rect(&damage, &damage, |
| 476 | 0, iy, ix, iheight); |
| 477 | pixman_region32_union_rect(&damage, &damage, |
| 478 | ix + iwidth, iy, |
| 479 | fwidth - (ix + iwidth), iheight); |
| 480 | pixman_region32_union_rect(&damage, &damage, |
| 481 | 0, iy + iheight, |
| 482 | fwidth, fheight - (iy + iheight)); |
| 483 | } |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 484 | } |
| 485 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 486 | rects = pixman_region32_rectangles(&damage, &n); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 487 | wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0); |
| 488 | for (i = 0; i < n; ++i) |
| 489 | wl_surface_damage(sb->output->parent.surface, rects[i].x1, |
| 490 | rects[i].y1, rects[i].x2 - rects[i].x1, |
| 491 | rects[i].y2 - rects[i].y1); |
| 492 | |
| 493 | if (sb->output->frame) |
| 494 | pixman_region32_fini(&damage); |
| 495 | } |
| 496 | |
| 497 | static int |
| 498 | wayland_output_repaint_pixman(struct weston_output *output_base, |
| 499 | pixman_region32_t *damage) |
| 500 | { |
| 501 | struct wayland_output *output = (struct wayland_output *) output_base; |
| 502 | struct wayland_compositor *c = |
| 503 | (struct wayland_compositor *)output->base.compositor; |
| 504 | struct wl_callback *callback; |
| 505 | struct wayland_shm_buffer *sb; |
| 506 | |
| 507 | if (output->frame) { |
| 508 | if (frame_status(output->frame) & FRAME_STATUS_REPAINT) |
| 509 | wl_list_for_each(sb, &output->shm.buffers, link) |
| 510 | sb->frame_damaged = 1; |
| 511 | } |
| 512 | |
| 513 | wl_list_for_each(sb, &output->shm.buffers, link) |
| 514 | pixman_region32_union(&sb->damage, &sb->damage, damage); |
| 515 | |
| 516 | sb = wayland_output_get_shm_buffer(output); |
| 517 | |
| 518 | wayland_output_update_shm_border(sb); |
| 519 | pixman_renderer_output_set_buffer(output_base, sb->pm_image); |
| 520 | c->base.renderer->repaint_output(output_base, &sb->damage); |
| 521 | |
| 522 | wayland_shm_buffer_attach(sb); |
| 523 | |
| 524 | callback = wl_surface_frame(output->parent.surface); |
| 525 | wl_callback_add_listener(callback, &frame_listener, output); |
| 526 | wl_surface_commit(output->parent.surface); |
| 527 | wl_display_flush(c->parent.wl_display); |
| 528 | |
| 529 | pixman_region32_fini(&sb->damage); |
| 530 | pixman_region32_init(&sb->damage); |
| 531 | sb->frame_damaged = 0; |
| 532 | |
| 533 | pixman_region32_subtract(&c->base.primary_plane.damage, |
| 534 | &c->base.primary_plane.damage, damage); |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 539 | wayland_output_destroy(struct weston_output *output_base) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 540 | { |
| 541 | struct wayland_output *output = (struct wayland_output *) output_base; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 542 | struct wayland_compositor *c = |
| 543 | (struct wayland_compositor *) output->base.compositor; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 544 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 545 | if (c->use_pixman) { |
| 546 | pixman_renderer_output_destroy(output_base); |
| 547 | } else { |
| 548 | gl_renderer->output_destroy(output_base); |
| 549 | } |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 550 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 551 | wl_egl_window_destroy(output->gl.egl_window); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 552 | wl_surface_destroy(output->parent.surface); |
| 553 | wl_shell_surface_destroy(output->parent.shell_surface); |
| 554 | |
| 555 | if (output->frame) { |
| 556 | frame_destroy(output->frame); |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 557 | cairo_surface_destroy(output->gl.border.top); |
| 558 | cairo_surface_destroy(output->gl.border.left); |
| 559 | cairo_surface_destroy(output->gl.border.right); |
| 560 | cairo_surface_destroy(output->gl.border.bottom); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | weston_output_destroy(&output->base); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 564 | free(output); |
| 565 | |
| 566 | return; |
| 567 | } |
| 568 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 569 | static const struct wl_shell_surface_listener shell_surface_listener; |
| 570 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 571 | static int |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 572 | wayland_output_init_gl_renderer(struct wayland_output *output) |
| 573 | { |
Jason Ekstrand | 00b8428 | 2013-10-27 22:24:59 -0500 | [diff] [blame] | 574 | int32_t fwidth = 0, fheight = 0; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 575 | |
| 576 | if (output->frame) { |
| 577 | fwidth = frame_width(output->frame); |
| 578 | fheight = frame_height(output->frame); |
| 579 | } else { |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 580 | fwidth = output->base.current_mode->width; |
| 581 | fheight = output->base.current_mode->height; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | output->gl.egl_window = |
| 585 | wl_egl_window_create(output->parent.surface, |
| 586 | fwidth, fheight); |
| 587 | if (!output->gl.egl_window) { |
| 588 | weston_log("failure to create wl_egl_window\n"); |
| 589 | return -1; |
| 590 | } |
| 591 | |
| 592 | if (gl_renderer->output_create(&output->base, |
| 593 | output->gl.egl_window) < 0) |
| 594 | goto cleanup_window; |
| 595 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 596 | return 0; |
| 597 | |
| 598 | cleanup_window: |
| 599 | wl_egl_window_destroy(output->gl.egl_window); |
| 600 | return -1; |
| 601 | } |
| 602 | |
| 603 | static int |
| 604 | wayland_output_init_pixman_renderer(struct wayland_output *output) |
| 605 | { |
| 606 | return pixman_renderer_output_create(&output->base); |
| 607 | } |
| 608 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 609 | static struct wayland_output * |
| 610 | wayland_output_create(struct wayland_compositor *c, int x, int y, |
| 611 | int width, int height, const char *name, |
| 612 | uint32_t transform, int32_t scale) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 613 | { |
| 614 | struct wayland_output *output; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 615 | int32_t fx, fy, fwidth, fheight; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 616 | int output_width, output_height; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 617 | struct wl_region *region; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 618 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 619 | output = zalloc(sizeof *output); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 620 | if (output == NULL) |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 621 | return NULL; |
| 622 | |
| 623 | output_width = width * scale; |
| 624 | output_height = height * scale; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 625 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 626 | output->mode.flags = |
| 627 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 628 | output->mode.width = output_width; |
| 629 | output->mode.height = output_height; |
| 630 | output->mode.refresh = 60000; |
| 631 | output->scale = scale; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 632 | wl_list_init(&output->base.mode_list); |
| 633 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
| 634 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 635 | weston_log("Creating %dx%d wayland output at (%d, %d)\n", |
| 636 | width, height, x, y); |
| 637 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 638 | output->base.current_mode = &output->mode; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 639 | weston_output_init(&output->base, &c->base, x, y, width, height, |
| 640 | transform, scale); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 641 | |
Kristian Høgsberg | 90bc88c | 2012-08-13 23:34:04 -0400 | [diff] [blame] | 642 | output->base.make = "waywayland"; |
| 643 | output->base.model = "none"; |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 644 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 645 | wl_list_init(&output->shm.buffers); |
| 646 | wl_list_init(&output->shm.free_buffers); |
| 647 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 648 | if (!c->theme) |
| 649 | c->theme = theme_create(); |
| 650 | output->frame = frame_create(c->theme, width, height, |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 651 | FRAME_BUTTON_CLOSE, name); |
| 652 | frame_resize_inside(output->frame, output_width, output_height); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 653 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 654 | output->parent.surface = |
| 655 | wl_compositor_create_surface(c->parent.compositor); |
| 656 | wl_surface_set_user_data(output->parent.surface, output); |
| 657 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 658 | if (c->use_pixman) { |
| 659 | if (wayland_output_init_pixman_renderer(output) < 0) |
| 660 | goto cleanup_output; |
| 661 | output->base.repaint = wayland_output_repaint_pixman; |
| 662 | } else { |
| 663 | if (wayland_output_init_gl_renderer(output) < 0) |
| 664 | goto cleanup_output; |
| 665 | output->base.repaint = wayland_output_repaint_gl; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 666 | } |
| 667 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 668 | frame_input_rect(output->frame, &fx, &fy, &fwidth, &fheight); |
| 669 | region = wl_compositor_create_region(c->parent.compositor); |
| 670 | wl_region_add(region, fx, fy, fwidth, fheight); |
| 671 | wl_surface_set_input_region(output->parent.surface, region); |
| 672 | wl_region_destroy(region); |
| 673 | |
| 674 | frame_opaque_rect(output->frame, &fx, &fy, &fwidth, &fheight); |
| 675 | region = wl_compositor_create_region(c->parent.compositor); |
| 676 | wl_region_add(region, fx, fy, fwidth, fheight); |
| 677 | wl_surface_set_opaque_region(output->parent.surface, region); |
| 678 | wl_region_destroy(region); |
| 679 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 680 | output->parent.draw_initial_frame = 1; |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 681 | output->parent.shell_surface = |
| 682 | wl_shell_get_shell_surface(c->parent.shell, |
| 683 | output->parent.surface); |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 684 | wl_shell_surface_add_listener(output->parent.shell_surface, |
| 685 | &shell_surface_listener, output); |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 686 | wl_shell_surface_set_toplevel(output->parent.shell_surface); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 687 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 688 | output->base.start_repaint_loop = wayland_output_start_repaint_loop; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 689 | output->base.destroy = wayland_output_destroy; |
Jesse Barnes | 5308a5e | 2012-02-09 13:12:57 -0800 | [diff] [blame] | 690 | output->base.assign_planes = NULL; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 691 | output->base.set_backlight = NULL; |
| 692 | output->base.set_dpms = NULL; |
Alex Wu | 2dda604 | 2012-04-17 17:20:47 +0800 | [diff] [blame] | 693 | output->base.switch_mode = NULL; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 694 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 695 | wl_list_insert(c->base.output_list.prev, &output->base.link); |
| 696 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 697 | return output; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 698 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 699 | cleanup_output: |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 700 | /* FIXME: cleanup weston_output */ |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 701 | free(output); |
| 702 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 703 | return NULL; |
| 704 | } |
| 705 | |
| 706 | static struct wayland_output * |
| 707 | wayland_output_create_for_config(struct wayland_compositor *c, |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 708 | struct weston_config_section *config_section, |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 709 | int option_width, int option_height, |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame^] | 710 | int option_scale, int32_t x, int32_t y) |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 711 | { |
| 712 | struct wayland_output *output; |
| 713 | char *mode, *t, *name, *str; |
| 714 | int width, height, scale; |
| 715 | uint32_t transform; |
| 716 | unsigned int i, slen; |
| 717 | |
| 718 | static const struct { const char *name; uint32_t token; } transform_names[] = { |
| 719 | { "normal", WL_OUTPUT_TRANSFORM_NORMAL }, |
| 720 | { "90", WL_OUTPUT_TRANSFORM_90 }, |
| 721 | { "180", WL_OUTPUT_TRANSFORM_180 }, |
| 722 | { "270", WL_OUTPUT_TRANSFORM_270 }, |
| 723 | { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED }, |
| 724 | { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 }, |
| 725 | { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 }, |
| 726 | { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 }, |
| 727 | }; |
| 728 | |
| 729 | weston_config_section_get_string(config_section, "name", &name, NULL); |
| 730 | if (name) { |
| 731 | slen = strlen(name); |
| 732 | slen += strlen(WINDOW_TITLE " - "); |
| 733 | str = malloc(slen + 1); |
| 734 | if (str) |
| 735 | snprintf(str, slen + 1, WINDOW_TITLE " - %s", name); |
| 736 | free(name); |
| 737 | name = str; |
| 738 | } |
| 739 | if (!name) |
| 740 | name = WINDOW_TITLE; |
| 741 | |
| 742 | weston_config_section_get_string(config_section, |
| 743 | "mode", &mode, "1024x600"); |
| 744 | if (sscanf(mode, "%dx%d", &width, &height) != 2) { |
| 745 | weston_log("Invalid mode \"%s\" for output %s\n", |
| 746 | mode, name); |
| 747 | width = 1024; |
| 748 | height = 640; |
| 749 | } |
| 750 | free(mode); |
| 751 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 752 | if (option_width) |
| 753 | width = option_width; |
| 754 | if (option_height) |
| 755 | height = option_height; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 756 | |
| 757 | weston_config_section_get_int(config_section, "scale", &scale, 1); |
| 758 | |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame^] | 759 | if (option_scale) |
| 760 | scale = option_scale; |
| 761 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 762 | weston_config_section_get_string(config_section, |
| 763 | "transform", &t, "normal"); |
| 764 | transform = WL_OUTPUT_TRANSFORM_NORMAL; |
| 765 | for (i = 0; i < ARRAY_LENGTH(transform_names); i++) { |
| 766 | if (strcmp(transform_names[i].name, t) == 0) { |
| 767 | transform = transform_names[i].token; |
| 768 | break; |
| 769 | } |
| 770 | } |
| 771 | if (i >= ARRAY_LENGTH(transform_names)) |
| 772 | weston_log("Invalid transform \"%s\" for output %s\n", t, name); |
| 773 | free(t); |
| 774 | |
| 775 | output = wayland_output_create(c, x, y, width, height, name, transform, scale); |
| 776 | free(name); |
| 777 | |
| 778 | return output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 779 | } |
| 780 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 781 | static void |
| 782 | shell_surface_ping(void *data, struct wl_shell_surface *shell_surface, |
| 783 | uint32_t serial) |
| 784 | { |
| 785 | wl_shell_surface_pong(shell_surface, serial); |
| 786 | } |
| 787 | |
| 788 | static void |
| 789 | shell_surface_configure(void *data, struct wl_shell_surface *shell_surface, |
| 790 | uint32_t edges, int32_t width, int32_t height) |
| 791 | { |
| 792 | /* FIXME: implement resizing */ |
| 793 | } |
| 794 | |
| 795 | static void |
| 796 | shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface) |
| 797 | { |
| 798 | } |
| 799 | |
| 800 | static const struct wl_shell_surface_listener shell_surface_listener = { |
| 801 | shell_surface_ping, |
| 802 | shell_surface_configure, |
| 803 | shell_surface_popup_done |
| 804 | }; |
| 805 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 806 | /* Events received from the wayland-server this compositor is client of: */ |
| 807 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 808 | /* parent input interface */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 809 | static void |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 810 | input_set_cursor(struct wayland_input *input) |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 811 | { |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 812 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 813 | struct wl_buffer *buffer; |
| 814 | struct wl_cursor_image *image; |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 815 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 816 | if (!input->compositor->cursor) |
| 817 | return; /* Couldn't load the cursor. Can't set it */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 818 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 819 | image = input->compositor->cursor->images[0]; |
| 820 | buffer = wl_cursor_image_get_buffer(image); |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 821 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 822 | |
| 823 | wl_pointer_set_cursor(input->parent.pointer, input->enter_serial, |
| 824 | input->parent.cursor.surface, |
| 825 | image->hotspot_x, image->hotspot_y); |
| 826 | |
| 827 | wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0); |
| 828 | wl_surface_damage(input->parent.cursor.surface, 0, 0, |
| 829 | image->width, image->height); |
| 830 | wl_surface_commit(input->parent.cursor.surface); |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 831 | } |
| 832 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 833 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 834 | input_handle_pointer_enter(void *data, struct wl_pointer *pointer, |
| 835 | uint32_t serial, struct wl_surface *surface, |
Kristian Høgsberg | e11bbe4 | 2012-05-09 12:19:04 -0400 | [diff] [blame] | 836 | wl_fixed_t x, wl_fixed_t y) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 837 | { |
| 838 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 839 | int32_t fx, fy; |
| 840 | enum theme_location location; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 841 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 842 | /* XXX: If we get a modifier event immediately before the focus, |
| 843 | * we should try to keep the same serial. */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 844 | input->enter_serial = serial; |
| 845 | input->output = wl_surface_get_user_data(surface); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 846 | |
| 847 | if (input->output->frame) { |
| 848 | location = frame_pointer_enter(input->output->frame, input, |
| 849 | wl_fixed_to_int(x), |
| 850 | wl_fixed_to_int(y)); |
| 851 | frame_interior(input->output->frame, &fx, &fy, NULL, NULL); |
| 852 | x -= wl_fixed_from_int(fx); |
| 853 | y -= wl_fixed_from_int(fy); |
| 854 | |
| 855 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 856 | weston_output_schedule_repaint(&input->output->base); |
| 857 | } else { |
| 858 | location = THEME_LOCATION_CLIENT_AREA; |
| 859 | } |
| 860 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 861 | weston_output_transform_coordinate(&input->output->base, x, y, &x, &y); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 862 | |
| 863 | if (location == THEME_LOCATION_CLIENT_AREA) { |
| 864 | input->focus = 1; |
| 865 | notify_pointer_focus(&input->base, &input->output->base, x, y); |
| 866 | wl_pointer_set_cursor(input->parent.pointer, |
| 867 | input->enter_serial, NULL, 0, 0); |
| 868 | } else { |
| 869 | input->focus = 0; |
| 870 | notify_pointer_focus(&input->base, NULL, 0, 0); |
| 871 | input_set_cursor(input); |
| 872 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 876 | input_handle_pointer_leave(void *data, struct wl_pointer *pointer, |
| 877 | uint32_t serial, struct wl_surface *surface) |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 878 | { |
| 879 | struct wayland_input *input = data; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 880 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 881 | if (input->output->frame) { |
| 882 | frame_pointer_leave(input->output->frame, input); |
| 883 | |
| 884 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 885 | weston_output_schedule_repaint(&input->output->base); |
| 886 | } |
| 887 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 888 | notify_pointer_focus(&input->base, NULL, 0, 0); |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 889 | input->output = NULL; |
| 890 | input->focus = 0; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 894 | input_handle_motion(void *data, struct wl_pointer *pointer, |
| 895 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 896 | { |
| 897 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 898 | int32_t fx, fy; |
| 899 | enum theme_location location; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 900 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 901 | if (input->output->frame) { |
| 902 | location = frame_pointer_motion(input->output->frame, input, |
| 903 | wl_fixed_to_int(x), |
| 904 | wl_fixed_to_int(y)); |
| 905 | frame_interior(input->output->frame, &fx, &fy, NULL, NULL); |
| 906 | x -= wl_fixed_from_int(fx); |
| 907 | y -= wl_fixed_from_int(fy); |
| 908 | |
| 909 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 910 | weston_output_schedule_repaint(&input->output->base); |
| 911 | } else { |
| 912 | location = THEME_LOCATION_CLIENT_AREA; |
| 913 | } |
| 914 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 915 | weston_output_transform_coordinate(&input->output->base, x, y, &x, &y); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 916 | |
| 917 | if (input->focus && location != THEME_LOCATION_CLIENT_AREA) { |
| 918 | input_set_cursor(input); |
| 919 | notify_pointer_focus(&input->base, NULL, 0, 0); |
| 920 | input->focus = 0; |
| 921 | } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) { |
| 922 | wl_pointer_set_cursor(input->parent.pointer, |
| 923 | input->enter_serial, NULL, 0, 0); |
| 924 | notify_pointer_focus(&input->base, &input->output->base, x, y); |
| 925 | input->focus = 1; |
| 926 | } |
| 927 | |
| 928 | if (location == THEME_LOCATION_CLIENT_AREA) |
| 929 | notify_motion_absolute(&input->base, time, x, y); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | static void |
| 933 | input_handle_button(void *data, struct wl_pointer *pointer, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 934 | uint32_t serial, uint32_t time, uint32_t button, |
| 935 | uint32_t state_w) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 936 | { |
| 937 | struct wayland_input *input = data; |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 938 | enum wl_pointer_button_state state = state_w; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 939 | enum frame_button_state fstate; |
| 940 | enum theme_location location; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 941 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 942 | if (input->output->frame) { |
| 943 | fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ? |
| 944 | FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED; |
| 945 | |
| 946 | location = frame_pointer_button(input->output->frame, input, |
| 947 | button, fstate); |
| 948 | |
| 949 | if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) { |
| 950 | |
| 951 | wl_shell_surface_move(input->output->parent.shell_surface, |
| 952 | input->parent.seat, serial); |
| 953 | frame_status_clear(input->output->frame, |
| 954 | FRAME_STATUS_MOVE); |
| 955 | return; |
| 956 | } |
| 957 | |
| 958 | if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) |
| 959 | wl_display_terminate(input->compositor->base.wl_display); |
| 960 | |
| 961 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 962 | weston_output_schedule_repaint(&input->output->base); |
| 963 | } else { |
| 964 | location = THEME_LOCATION_CLIENT_AREA; |
| 965 | } |
| 966 | |
| 967 | if (location == THEME_LOCATION_CLIENT_AREA) |
| 968 | notify_button(&input->base, time, button, state); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | static void |
| 972 | input_handle_axis(void *data, struct wl_pointer *pointer, |
Daniel Stone | 2fce402 | 2012-05-30 16:32:00 +0100 | [diff] [blame] | 973 | uint32_t time, uint32_t axis, wl_fixed_t value) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 974 | { |
| 975 | struct wayland_input *input = data; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 976 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 977 | notify_axis(&input->base, time, axis, value); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | static const struct wl_pointer_listener pointer_listener = { |
| 981 | input_handle_pointer_enter, |
| 982 | input_handle_pointer_leave, |
| 983 | input_handle_motion, |
| 984 | input_handle_button, |
| 985 | input_handle_axis, |
| 986 | }; |
| 987 | |
| 988 | static void |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 989 | input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, |
| 990 | int fd, uint32_t size) |
| 991 | { |
| 992 | struct wayland_input *input = data; |
| 993 | struct xkb_keymap *keymap; |
| 994 | char *map_str; |
| 995 | |
| 996 | if (!data) { |
| 997 | close(fd); |
| 998 | return; |
| 999 | } |
| 1000 | |
| 1001 | if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { |
| 1002 | close(fd); |
| 1003 | return; |
| 1004 | } |
| 1005 | |
| 1006 | map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); |
| 1007 | if (map_str == MAP_FAILED) { |
| 1008 | close(fd); |
| 1009 | return; |
| 1010 | } |
| 1011 | |
| 1012 | keymap = xkb_map_new_from_string(input->compositor->base.xkb_context, |
| 1013 | map_str, |
| 1014 | XKB_KEYMAP_FORMAT_TEXT_V1, |
| 1015 | 0); |
| 1016 | munmap(map_str, size); |
| 1017 | close(fd); |
| 1018 | |
| 1019 | if (!keymap) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1020 | weston_log("failed to compile keymap\n"); |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1021 | return; |
| 1022 | } |
| 1023 | |
Rui Matos | 0c194ce | 2013-10-10 19:44:21 +0200 | [diff] [blame] | 1024 | if (input->base.keyboard) |
| 1025 | weston_seat_update_keymap(&input->base, keymap); |
| 1026 | else |
| 1027 | weston_seat_init_keyboard(&input->base, keymap); |
| 1028 | |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1029 | xkb_map_unref(keymap); |
| 1030 | } |
| 1031 | |
| 1032 | static void |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1033 | input_handle_keyboard_enter(void *data, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1034 | struct wl_keyboard *keyboard, |
| 1035 | uint32_t serial, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1036 | struct wl_surface *surface, |
| 1037 | struct wl_array *keys) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1038 | { |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 1039 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1040 | struct wayland_output *focus; |
| 1041 | |
| 1042 | focus = input->keyboard_focus; |
| 1043 | if (focus) { |
| 1044 | /* This shouldn't happen */ |
| 1045 | focus->keyboard_count--; |
| 1046 | if (!focus->keyboard_count && focus->frame) |
| 1047 | frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE); |
| 1048 | if (frame_status(focus->frame) & FRAME_STATUS_REPAINT) |
| 1049 | weston_output_schedule_repaint(&focus->base); |
| 1050 | } |
| 1051 | |
| 1052 | input->keyboard_focus = wl_surface_get_user_data(surface); |
| 1053 | input->keyboard_focus->keyboard_count++; |
| 1054 | |
| 1055 | focus = input->keyboard_focus; |
| 1056 | if (focus->frame) { |
| 1057 | frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE); |
| 1058 | if (frame_status(focus->frame) & FRAME_STATUS_REPAINT) |
| 1059 | weston_output_schedule_repaint(&focus->base); |
| 1060 | } |
| 1061 | |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 1062 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1063 | /* XXX: If we get a modifier event immediately before the focus, |
| 1064 | * we should try to keep the same serial. */ |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1065 | notify_keyboard_focus_in(&input->base, keys, |
Daniel Stone | d6da09e | 2012-06-22 13:21:29 +0100 | [diff] [blame] | 1066 | STATE_UPDATE_AUTOMATIC); |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | static void |
| 1070 | input_handle_keyboard_leave(void *data, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1071 | struct wl_keyboard *keyboard, |
| 1072 | uint32_t serial, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1073 | struct wl_surface *surface) |
| 1074 | { |
| 1075 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1076 | struct wayland_output *focus; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1077 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1078 | notify_keyboard_focus_out(&input->base); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1079 | |
| 1080 | focus = input->keyboard_focus; |
| 1081 | if (!focus) |
| 1082 | return; /* This shouldn't happen */ |
| 1083 | |
| 1084 | focus->keyboard_count--; |
| 1085 | if (!focus->keyboard_count && focus->frame) { |
| 1086 | frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE); |
| 1087 | if (frame_status(focus->frame) & FRAME_STATUS_REPAINT) |
| 1088 | weston_output_schedule_repaint(&focus->base); |
| 1089 | } |
| 1090 | |
| 1091 | input->keyboard_focus = NULL; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1092 | } |
| 1093 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1094 | static void |
| 1095 | input_handle_key(void *data, struct wl_keyboard *keyboard, |
| 1096 | uint32_t serial, uint32_t time, uint32_t key, uint32_t state) |
| 1097 | { |
| 1098 | struct wayland_input *input = data; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1099 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1100 | input->key_serial = serial; |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1101 | notify_key(&input->base, time, key, |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 1102 | state ? WL_KEYBOARD_KEY_STATE_PRESSED : |
Daniel Stone | 1b4e11f | 2012-06-22 13:21:37 +0100 | [diff] [blame] | 1103 | WL_KEYBOARD_KEY_STATE_RELEASED, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1104 | STATE_UPDATE_NONE); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1105 | } |
| 1106 | |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1107 | static void |
| 1108 | input_handle_modifiers(void *data, struct wl_keyboard *keyboard, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1109 | uint32_t serial_in, uint32_t mods_depressed, |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1110 | uint32_t mods_latched, uint32_t mods_locked, |
| 1111 | uint32_t group) |
| 1112 | { |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1113 | struct wayland_input *input = data; |
| 1114 | struct wayland_compositor *c = input->compositor; |
| 1115 | uint32_t serial_out; |
| 1116 | |
| 1117 | /* If we get a key event followed by a modifier event with the |
| 1118 | * same serial number, then we try to preserve those semantics by |
| 1119 | * reusing the same serial number on the way out too. */ |
| 1120 | if (serial_in == input->key_serial) |
| 1121 | serial_out = wl_display_get_serial(c->base.wl_display); |
| 1122 | else |
| 1123 | serial_out = wl_display_next_serial(c->base.wl_display); |
| 1124 | |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 1125 | xkb_state_update_mask(input->base.xkb_state.state, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1126 | mods_depressed, mods_latched, |
| 1127 | mods_locked, 0, 0, group); |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1128 | notify_modifiers(&input->base, serial_out); |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1129 | } |
| 1130 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1131 | static const struct wl_keyboard_listener keyboard_listener = { |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1132 | input_handle_keymap, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1133 | input_handle_keyboard_enter, |
| 1134 | input_handle_keyboard_leave, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1135 | input_handle_key, |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1136 | input_handle_modifiers, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1137 | }; |
| 1138 | |
| 1139 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1140 | input_handle_capabilities(void *data, struct wl_seat *seat, |
| 1141 | enum wl_seat_capability caps) |
| 1142 | { |
| 1143 | struct wayland_input *input = data; |
| 1144 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1145 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) { |
| 1146 | input->parent.pointer = wl_seat_get_pointer(seat); |
| 1147 | wl_pointer_set_user_data(input->parent.pointer, input); |
| 1148 | wl_pointer_add_listener(input->parent.pointer, |
| 1149 | &pointer_listener, input); |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 1150 | weston_seat_init_pointer(&input->base); |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1151 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) { |
| 1152 | wl_pointer_destroy(input->parent.pointer); |
| 1153 | input->parent.pointer = NULL; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1154 | } |
| 1155 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1156 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) { |
| 1157 | input->parent.keyboard = wl_seat_get_keyboard(seat); |
| 1158 | wl_keyboard_set_user_data(input->parent.keyboard, input); |
| 1159 | wl_keyboard_add_listener(input->parent.keyboard, |
| 1160 | &keyboard_listener, input); |
| 1161 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) { |
| 1162 | wl_keyboard_destroy(input->parent.keyboard); |
| 1163 | input->parent.keyboard = NULL; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | static const struct wl_seat_listener seat_listener = { |
| 1168 | input_handle_capabilities, |
| 1169 | }; |
| 1170 | |
| 1171 | static void |
| 1172 | display_add_seat(struct wayland_compositor *c, uint32_t id) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1173 | { |
| 1174 | struct wayland_input *input; |
| 1175 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 1176 | input = zalloc(sizeof *input); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1177 | if (input == NULL) |
| 1178 | return; |
| 1179 | |
Rob Bradford | 9af5f9e | 2013-05-31 18:09:50 +0100 | [diff] [blame] | 1180 | weston_seat_init(&input->base, &c->base, "default"); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1181 | input->compositor = c; |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1182 | input->parent.seat = wl_registry_bind(c->parent.registry, id, |
| 1183 | &wl_seat_interface, 1); |
Jason Ekstrand | 06ced80 | 2013-11-07 20:13:29 -0600 | [diff] [blame] | 1184 | wl_list_insert(c->input_list.prev, &input->link); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1185 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1186 | wl_seat_add_listener(input->parent.seat, &seat_listener, input); |
| 1187 | wl_seat_set_user_data(input->parent.seat, input); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1188 | |
| 1189 | input->parent.cursor.surface = |
| 1190 | wl_compositor_create_surface(c->parent.compositor); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1194 | registry_handle_global(void *data, struct wl_registry *registry, uint32_t name, |
| 1195 | const char *interface, uint32_t version) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1196 | { |
| 1197 | struct wayland_compositor *c = data; |
| 1198 | |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 1199 | if (strcmp(interface, "wl_compositor") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 1200 | c->parent.compositor = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1201 | wl_registry_bind(registry, name, |
| 1202 | &wl_compositor_interface, 1); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 1203 | } else if (strcmp(interface, "wl_shell") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 1204 | c->parent.shell = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1205 | wl_registry_bind(registry, name, |
| 1206 | &wl_shell_interface, 1); |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1207 | } else if (strcmp(interface, "wl_seat") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1208 | display_add_seat(c, name); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 1209 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 1210 | c->parent.shm = |
| 1211 | wl_registry_bind(registry, name, &wl_shm_interface, 1); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1212 | } |
| 1213 | } |
| 1214 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1215 | static const struct wl_registry_listener registry_listener = { |
| 1216 | registry_handle_global |
| 1217 | }; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1218 | |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 1219 | static int |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1220 | wayland_compositor_handle_event(int fd, uint32_t mask, void *data) |
| 1221 | { |
| 1222 | struct wayland_compositor *c = data; |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 1223 | int count = 0; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1224 | |
Kristian Høgsberg | 453de7a | 2013-10-30 23:15:44 -0700 | [diff] [blame] | 1225 | if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) { |
| 1226 | wl_display_terminate(c->base.wl_display); |
| 1227 | return 0; |
| 1228 | } |
| 1229 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1230 | if (mask & WL_EVENT_READABLE) |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 1231 | count = wl_display_dispatch(c->parent.wl_display); |
Kristian Høgsberg | f258a31 | 2011-12-28 22:51:20 -0500 | [diff] [blame] | 1232 | if (mask & WL_EVENT_WRITABLE) |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1233 | wl_display_flush(c->parent.wl_display); |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 1234 | |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 1235 | if (mask == 0) { |
| 1236 | count = wl_display_dispatch_pending(c->parent.wl_display); |
| 1237 | wl_display_flush(c->parent.wl_display); |
| 1238 | } |
| 1239 | |
| 1240 | return count; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1241 | } |
| 1242 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1243 | static void |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 1244 | wayland_restore(struct weston_compositor *ec) |
| 1245 | { |
| 1246 | } |
| 1247 | |
| 1248 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1249 | wayland_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1250 | { |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 1251 | struct wayland_compositor *c = (struct wayland_compositor *) ec; |
| 1252 | |
Vasily Khoruzhick | 52cfd61 | 2013-01-08 19:09:01 +0300 | [diff] [blame] | 1253 | ec->renderer->destroy(ec); |
Kristian Høgsberg | 3a0de88 | 2012-09-06 21:44:24 -0400 | [diff] [blame] | 1254 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1255 | weston_compositor_shutdown(ec); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1256 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 1257 | if (c->parent.shm) |
| 1258 | wl_shm_destroy(c->parent.shm); |
| 1259 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1260 | free(ec); |
| 1261 | } |
| 1262 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1263 | static const char *left_ptrs[] = { |
| 1264 | "left_ptr", |
| 1265 | "default", |
| 1266 | "top_left_arrow", |
| 1267 | "left-arrow" |
| 1268 | }; |
| 1269 | |
| 1270 | static void |
| 1271 | create_cursor(struct wayland_compositor *c, struct weston_config *config) |
| 1272 | { |
| 1273 | struct weston_config_section *s; |
| 1274 | int size; |
| 1275 | char *theme = NULL; |
| 1276 | unsigned int i; |
| 1277 | |
| 1278 | s = weston_config_get_section(config, "shell", NULL, NULL); |
| 1279 | weston_config_section_get_string(s, "cursor-theme", &theme, NULL); |
| 1280 | weston_config_section_get_int(s, "cursor-size", &size, 32); |
| 1281 | |
| 1282 | c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm); |
| 1283 | |
| 1284 | c->cursor = NULL; |
| 1285 | for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i) |
| 1286 | c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme, |
| 1287 | left_ptrs[i]); |
| 1288 | if (!c->cursor) { |
| 1289 | fprintf(stderr, "could not load left cursor\n"); |
| 1290 | return; |
| 1291 | } |
| 1292 | } |
| 1293 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1294 | static struct wayland_compositor * |
| 1295 | wayland_compositor_create(struct wl_display *display, int use_pixman, |
| 1296 | const char *display_name, int *argc, char *argv[], |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 1297 | struct weston_config *config) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1298 | { |
| 1299 | struct wayland_compositor *c; |
| 1300 | struct wl_event_loop *loop; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1301 | int fd; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1302 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 1303 | c = zalloc(sizeof *c); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1304 | if (c == NULL) |
| 1305 | return NULL; |
| 1306 | |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1307 | if (weston_compositor_init(&c->base, display, argc, argv, |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 1308 | config) < 0) |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 1309 | goto err_free; |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1310 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1311 | c->parent.wl_display = wl_display_connect(display_name); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1312 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1313 | if (c->parent.wl_display == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1314 | weston_log("failed to create display: %m\n"); |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 1315 | goto err_compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1316 | } |
| 1317 | |
Jason Ekstrand | 06ced80 | 2013-11-07 20:13:29 -0600 | [diff] [blame] | 1318 | wl_list_init(&c->input_list); |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1319 | c->parent.registry = wl_display_get_registry(c->parent.wl_display); |
| 1320 | wl_registry_add_listener(c->parent.registry, ®istry_listener, c); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1321 | wl_display_roundtrip(c->parent.wl_display); |
| 1322 | |
| 1323 | create_cursor(c, config); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1324 | |
| 1325 | c->base.wl_display = display; |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 1326 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1327 | c->use_pixman = use_pixman; |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 1328 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1329 | if (!c->use_pixman) { |
| 1330 | gl_renderer = weston_load_module("gl-renderer.so", |
| 1331 | "gl_renderer_interface"); |
| 1332 | if (!gl_renderer) |
| 1333 | c->use_pixman = 1; |
| 1334 | } |
| 1335 | |
| 1336 | if (!c->use_pixman) { |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 1337 | if (gl_renderer->create(&c->base, c->parent.wl_display, |
| 1338 | gl_renderer->alpha_attribs, |
| 1339 | NULL) < 0) { |
| 1340 | weston_log("Failed to initialize the GL renderer; " |
| 1341 | "falling back to pixman.\n"); |
| 1342 | c->use_pixman = 1; |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | if (c->use_pixman) { |
| 1347 | if (pixman_renderer_init(&c->base) < 0) { |
| 1348 | weston_log("Failed to initialize pixman renderer\n"); |
| 1349 | goto err_display; |
| 1350 | } |
| 1351 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1352 | |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 1353 | c->base.destroy = wayland_destroy; |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 1354 | c->base.restore = wayland_restore; |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 1355 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1356 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 1357 | |
| 1358 | fd = wl_display_get_fd(c->parent.wl_display); |
| 1359 | c->parent.wl_source = |
| 1360 | wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE, |
| 1361 | wayland_compositor_handle_event, c); |
| 1362 | if (c->parent.wl_source == NULL) |
| 1363 | goto err_renderer; |
| 1364 | |
| 1365 | wl_event_source_check(c->parent.wl_source); |
| 1366 | |
| 1367 | return c; |
| 1368 | err_renderer: |
| 1369 | c->base.renderer->destroy(&c->base); |
| 1370 | err_display: |
| 1371 | wl_display_disconnect(c->parent.wl_display); |
| 1372 | err_compositor: |
| 1373 | weston_compositor_shutdown(&c->base); |
| 1374 | err_free: |
| 1375 | free(c); |
| 1376 | return NULL; |
| 1377 | } |
| 1378 | |
| 1379 | static void |
| 1380 | wayland_compositor_destroy(struct wayland_compositor *c) |
| 1381 | { |
| 1382 | struct weston_output *output; |
| 1383 | |
| 1384 | wl_list_for_each(output, &c->base.output_list, link) |
| 1385 | wayland_output_destroy(output); |
| 1386 | |
| 1387 | c->base.renderer->destroy(&c->base); |
| 1388 | wl_display_disconnect(c->parent.wl_display); |
| 1389 | |
| 1390 | if (c->theme) |
| 1391 | theme_destroy(c->theme); |
| 1392 | if (c->frame_device) |
| 1393 | cairo_device_destroy(c->frame_device); |
| 1394 | wl_cursor_theme_destroy(c->cursor_theme); |
| 1395 | |
| 1396 | weston_compositor_shutdown(&c->base); |
| 1397 | free(c); |
| 1398 | } |
| 1399 | |
| 1400 | WL_EXPORT struct weston_compositor * |
| 1401 | backend_init(struct wl_display *display, int *argc, char *argv[], |
| 1402 | struct weston_config *config) |
| 1403 | { |
| 1404 | struct wayland_compositor *c; |
| 1405 | struct wayland_output *output; |
| 1406 | struct weston_config_section *section; |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame^] | 1407 | int x, count, width, height, scale, use_pixman; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1408 | const char *section_name, *display_name; |
| 1409 | char *name; |
| 1410 | |
| 1411 | const struct weston_option wayland_options[] = { |
| 1412 | { WESTON_OPTION_INTEGER, "width", 0, &width }, |
| 1413 | { WESTON_OPTION_INTEGER, "height", 0, &height }, |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame^] | 1414 | { WESTON_OPTION_INTEGER, "scale", 0, &scale }, |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1415 | { WESTON_OPTION_STRING, "display", 0, &display_name }, |
| 1416 | { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman }, |
| 1417 | { WESTON_OPTION_INTEGER, "output-count", 0, &count }, |
| 1418 | }; |
| 1419 | |
| 1420 | width = 0; |
| 1421 | height = 0; |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame^] | 1422 | scale = 0; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1423 | display_name = NULL; |
| 1424 | use_pixman = 0; |
| 1425 | count = 1; |
| 1426 | |
| 1427 | parse_options(wayland_options, |
| 1428 | ARRAY_LENGTH(wayland_options), argc, argv); |
| 1429 | |
| 1430 | c = wayland_compositor_create(display, use_pixman, display_name, |
| 1431 | argc, argv, config); |
| 1432 | if (!c) |
| 1433 | return NULL; |
| 1434 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1435 | section = NULL; |
| 1436 | x = 0; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1437 | while (weston_config_next_section(config, §ion, §ion_name)) { |
| 1438 | if (!section_name || strcmp(section_name, "output") != 0) |
| 1439 | continue; |
| 1440 | weston_config_section_get_string(section, "name", &name, NULL); |
| 1441 | if (name == NULL) |
| 1442 | continue; |
| 1443 | |
| 1444 | if (name[0] != 'W' || name[1] != 'L') { |
| 1445 | free(name); |
| 1446 | continue; |
| 1447 | } |
| 1448 | free(name); |
| 1449 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1450 | output = wayland_output_create_for_config(c, section, width, |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame^] | 1451 | height, scale, x, 0); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1452 | if (!output) |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1453 | goto err_outputs; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1454 | |
| 1455 | x += output->base.width; |
| 1456 | --count; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1457 | } |
| 1458 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1459 | if (!width) |
| 1460 | width = 1024; |
| 1461 | if (!height) |
| 1462 | height = 640; |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame^] | 1463 | if (!scale) |
| 1464 | scale = 1; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1465 | while (count > 0) { |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame^] | 1466 | output = wayland_output_create(c, x, 0, width, height, |
| 1467 | NULL, 0, scale); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1468 | if (!output) |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1469 | goto err_outputs; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1470 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1471 | x += width; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1472 | --count; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1473 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1474 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1475 | return &c->base; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1476 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1477 | err_outputs: |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1478 | wayland_compositor_destroy(c); |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 1479 | return NULL; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1480 | } |