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