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, |
| 596 | output->gl.egl_window) < 0) |
| 597 | goto cleanup_window; |
| 598 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 599 | return 0; |
| 600 | |
| 601 | cleanup_window: |
| 602 | wl_egl_window_destroy(output->gl.egl_window); |
| 603 | return -1; |
| 604 | } |
| 605 | |
| 606 | static int |
| 607 | wayland_output_init_pixman_renderer(struct wayland_output *output) |
| 608 | { |
| 609 | return pixman_renderer_output_create(&output->base); |
| 610 | } |
| 611 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 612 | static void |
| 613 | wayland_output_resize_surface(struct wayland_output *output) |
| 614 | { |
| 615 | struct wayland_compositor *c = |
| 616 | (struct wayland_compositor *)output->base.compositor; |
| 617 | struct wayland_shm_buffer *buffer, *next; |
| 618 | int32_t ix, iy, iwidth, iheight; |
| 619 | int32_t width, height; |
| 620 | struct wl_region *region; |
| 621 | |
| 622 | width = output->base.current_mode->width; |
| 623 | height = output->base.current_mode->height; |
| 624 | |
| 625 | if (output->frame) { |
| 626 | frame_resize_inside(output->frame, width, height); |
| 627 | |
| 628 | frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight); |
| 629 | region = wl_compositor_create_region(c->parent.compositor); |
| 630 | wl_region_add(region, ix, iy, iwidth, iheight); |
| 631 | wl_surface_set_input_region(output->parent.surface, region); |
| 632 | wl_region_destroy(region); |
| 633 | |
| 634 | frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight); |
| 635 | region = wl_compositor_create_region(c->parent.compositor); |
| 636 | wl_region_add(region, ix, iy, iwidth, iheight); |
| 637 | wl_surface_set_opaque_region(output->parent.surface, region); |
| 638 | wl_region_destroy(region); |
| 639 | |
| 640 | width = frame_width(output->frame); |
| 641 | height = frame_height(output->frame); |
| 642 | } else { |
| 643 | region = wl_compositor_create_region(c->parent.compositor); |
| 644 | wl_region_add(region, 0, 0, width, height); |
| 645 | wl_surface_set_input_region(output->parent.surface, region); |
| 646 | wl_region_destroy(region); |
| 647 | |
| 648 | region = wl_compositor_create_region(c->parent.compositor); |
| 649 | wl_region_add(region, 0, 0, width, height); |
| 650 | wl_surface_set_opaque_region(output->parent.surface, region); |
| 651 | wl_region_destroy(region); |
| 652 | } |
| 653 | |
| 654 | if (output->gl.egl_window) { |
| 655 | wl_egl_window_resize(output->gl.egl_window, |
| 656 | width, height, 0, 0); |
| 657 | |
| 658 | /* These will need to be re-created due to the resize */ |
| 659 | gl_renderer->output_set_border(&output->base, |
| 660 | GL_RENDERER_BORDER_TOP, |
| 661 | 0, 0, 0, NULL); |
| 662 | cairo_surface_destroy(output->gl.border.top); |
| 663 | output->gl.border.top = NULL; |
| 664 | gl_renderer->output_set_border(&output->base, |
| 665 | GL_RENDERER_BORDER_LEFT, |
| 666 | 0, 0, 0, NULL); |
| 667 | cairo_surface_destroy(output->gl.border.left); |
| 668 | output->gl.border.left = NULL; |
| 669 | gl_renderer->output_set_border(&output->base, |
| 670 | GL_RENDERER_BORDER_RIGHT, |
| 671 | 0, 0, 0, NULL); |
| 672 | cairo_surface_destroy(output->gl.border.right); |
| 673 | output->gl.border.right = NULL; |
| 674 | gl_renderer->output_set_border(&output->base, |
| 675 | GL_RENDERER_BORDER_BOTTOM, |
| 676 | 0, 0, 0, NULL); |
| 677 | cairo_surface_destroy(output->gl.border.bottom); |
| 678 | output->gl.border.bottom = NULL; |
| 679 | } |
| 680 | |
| 681 | /* Throw away any remaining SHM buffers */ |
| 682 | wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link) |
| 683 | wayland_shm_buffer_destroy(buffer); |
| 684 | /* These will get thrown away when they get released */ |
| 685 | wl_list_for_each(buffer, &output->shm.buffers, link) |
| 686 | buffer->output = NULL; |
| 687 | } |
| 688 | |
| 689 | static int |
| 690 | wayland_output_set_windowed(struct wayland_output *output) |
| 691 | { |
| 692 | struct wayland_compositor *c = |
| 693 | (struct wayland_compositor *)output->base.compositor; |
| 694 | int tlen; |
| 695 | char *title; |
| 696 | |
| 697 | if (output->frame) |
| 698 | return 0; |
| 699 | |
| 700 | if (output->name) { |
| 701 | tlen = strlen(output->name) + strlen(WINDOW_TITLE " - "); |
| 702 | title = malloc(tlen + 1); |
| 703 | if (!title) |
| 704 | return -1; |
| 705 | |
| 706 | snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name); |
| 707 | } else { |
| 708 | title = strdup(WINDOW_TITLE); |
| 709 | } |
| 710 | |
| 711 | if (!c->theme) { |
| 712 | c->theme = theme_create(); |
| 713 | if (!c->theme) { |
| 714 | free(title); |
| 715 | return -1; |
| 716 | } |
| 717 | } |
| 718 | output->frame = frame_create(c->theme, 100, 100, |
| 719 | FRAME_BUTTON_CLOSE, title); |
| 720 | free(title); |
| 721 | if (!output->frame) |
| 722 | return -1; |
| 723 | |
| 724 | if (output->keyboard_count) |
| 725 | frame_set_flag(output->frame, FRAME_FLAG_ACTIVE); |
| 726 | |
| 727 | wayland_output_resize_surface(output); |
| 728 | |
| 729 | wl_shell_surface_set_toplevel(output->parent.shell_surface); |
| 730 | |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | static void |
| 735 | wayland_output_set_fullscreen(struct wayland_output *output, |
| 736 | enum wl_shell_surface_fullscreen_method method, |
| 737 | uint32_t framerate, struct wl_output *target) |
| 738 | { |
| 739 | if (output->frame) { |
| 740 | frame_destroy(output->frame); |
| 741 | output->frame = NULL; |
| 742 | } |
| 743 | |
| 744 | wayland_output_resize_surface(output); |
| 745 | |
| 746 | wl_shell_surface_set_fullscreen(output->parent.shell_surface, |
| 747 | method, framerate, target); |
| 748 | } |
| 749 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 750 | static struct wayland_output * |
| 751 | wayland_output_create(struct wayland_compositor *c, int x, int y, |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 752 | int width, int height, const char *name, int fullscreen, |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 753 | uint32_t transform, int32_t scale) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 754 | { |
| 755 | struct wayland_output *output; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 756 | int output_width, output_height; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 757 | |
| 758 | weston_log("Creating %dx%d wayland output at (%d, %d)\n", |
| 759 | width, height, x, y); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 760 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 761 | output = zalloc(sizeof *output); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 762 | if (output == NULL) |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 763 | return NULL; |
| 764 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 765 | output->name = name ? strdup(name) : NULL; |
| 766 | output->base.make = "waywayland"; |
| 767 | output->base.model = "none"; |
| 768 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 769 | output_width = width * scale; |
| 770 | output_height = height * scale; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 771 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 772 | output->parent.surface = |
| 773 | wl_compositor_create_surface(c->parent.compositor); |
| 774 | if (!output->parent.surface) |
| 775 | goto err_name; |
| 776 | wl_surface_set_user_data(output->parent.surface, output); |
| 777 | |
| 778 | output->parent.draw_initial_frame = 1; |
| 779 | output->parent.shell_surface = |
| 780 | wl_shell_get_shell_surface(c->parent.shell, |
| 781 | output->parent.surface); |
| 782 | if (!output->parent.shell_surface) |
| 783 | goto err_surface; |
| 784 | wl_shell_surface_add_listener(output->parent.shell_surface, |
| 785 | &shell_surface_listener, output); |
| 786 | |
| 787 | if (fullscreen) { |
| 788 | wl_shell_surface_set_fullscreen(output->parent.shell_surface, |
| 789 | 0, 0, NULL); |
| 790 | wl_display_roundtrip(c->parent.wl_display); |
| 791 | if (!width) |
| 792 | output_width = output->parent.configure_width; |
| 793 | if (!height) |
| 794 | output_height = output->parent.configure_height; |
| 795 | } |
| 796 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 797 | output->mode.flags = |
| 798 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 799 | output->mode.width = output_width; |
| 800 | output->mode.height = output_height; |
| 801 | output->mode.refresh = 60000; |
| 802 | output->scale = scale; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 803 | wl_list_init(&output->base.mode_list); |
| 804 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 805 | output->base.current_mode = &output->mode; |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 806 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 807 | wl_list_init(&output->shm.buffers); |
| 808 | wl_list_init(&output->shm.free_buffers); |
| 809 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 810 | weston_output_init(&output->base, &c->base, x, y, width, height, |
| 811 | transform, scale); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 812 | |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 813 | if (c->use_pixman) { |
| 814 | if (wayland_output_init_pixman_renderer(output) < 0) |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 815 | goto err_output; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 816 | output->base.repaint = wayland_output_repaint_pixman; |
| 817 | } else { |
| 818 | if (wayland_output_init_gl_renderer(output) < 0) |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 819 | goto err_output; |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 820 | output->base.repaint = wayland_output_repaint_gl; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 821 | } |
| 822 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 823 | output->base.start_repaint_loop = wayland_output_start_repaint_loop; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 824 | output->base.destroy = wayland_output_destroy; |
Jesse Barnes | 5308a5e | 2012-02-09 13:12:57 -0800 | [diff] [blame] | 825 | output->base.assign_planes = NULL; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 826 | output->base.set_backlight = NULL; |
| 827 | output->base.set_dpms = NULL; |
Alex Wu | 2dda604 | 2012-04-17 17:20:47 +0800 | [diff] [blame] | 828 | output->base.switch_mode = NULL; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 829 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 830 | wl_list_insert(c->base.output_list.prev, &output->base.link); |
| 831 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 832 | return output; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 833 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 834 | err_output: |
| 835 | weston_output_destroy(&output->base); |
| 836 | wl_shell_surface_destroy(output->parent.shell_surface); |
| 837 | err_surface: |
| 838 | wl_surface_destroy(output->parent.surface); |
| 839 | err_name: |
| 840 | free(output->name); |
| 841 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 842 | /* FIXME: cleanup weston_output */ |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 843 | free(output); |
| 844 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 845 | return NULL; |
| 846 | } |
| 847 | |
| 848 | static struct wayland_output * |
| 849 | wayland_output_create_for_config(struct wayland_compositor *c, |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 850 | struct weston_config_section *config_section, |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 851 | int option_width, int option_height, |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame] | 852 | int option_scale, int32_t x, int32_t y) |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 853 | { |
| 854 | struct wayland_output *output; |
| 855 | char *mode, *t, *name, *str; |
| 856 | int width, height, scale; |
| 857 | uint32_t transform; |
| 858 | unsigned int i, slen; |
| 859 | |
| 860 | static const struct { const char *name; uint32_t token; } transform_names[] = { |
| 861 | { "normal", WL_OUTPUT_TRANSFORM_NORMAL }, |
| 862 | { "90", WL_OUTPUT_TRANSFORM_90 }, |
| 863 | { "180", WL_OUTPUT_TRANSFORM_180 }, |
| 864 | { "270", WL_OUTPUT_TRANSFORM_270 }, |
| 865 | { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED }, |
| 866 | { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 }, |
| 867 | { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 }, |
| 868 | { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 }, |
| 869 | }; |
| 870 | |
| 871 | weston_config_section_get_string(config_section, "name", &name, NULL); |
| 872 | if (name) { |
| 873 | slen = strlen(name); |
| 874 | slen += strlen(WINDOW_TITLE " - "); |
| 875 | str = malloc(slen + 1); |
| 876 | if (str) |
| 877 | snprintf(str, slen + 1, WINDOW_TITLE " - %s", name); |
| 878 | free(name); |
| 879 | name = str; |
| 880 | } |
| 881 | if (!name) |
U. Artie Eoff | 1a08d11 | 2014-01-17 12:22:50 -0800 | [diff] [blame^] | 882 | name = strdup(WINDOW_TITLE); |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 883 | |
| 884 | weston_config_section_get_string(config_section, |
| 885 | "mode", &mode, "1024x600"); |
| 886 | if (sscanf(mode, "%dx%d", &width, &height) != 2) { |
| 887 | weston_log("Invalid mode \"%s\" for output %s\n", |
| 888 | mode, name); |
| 889 | width = 1024; |
| 890 | height = 640; |
| 891 | } |
| 892 | free(mode); |
| 893 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 894 | if (option_width) |
| 895 | width = option_width; |
| 896 | if (option_height) |
| 897 | height = option_height; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 898 | |
| 899 | weston_config_section_get_int(config_section, "scale", &scale, 1); |
| 900 | |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame] | 901 | if (option_scale) |
| 902 | scale = option_scale; |
| 903 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 904 | weston_config_section_get_string(config_section, |
| 905 | "transform", &t, "normal"); |
| 906 | transform = WL_OUTPUT_TRANSFORM_NORMAL; |
| 907 | for (i = 0; i < ARRAY_LENGTH(transform_names); i++) { |
| 908 | if (strcmp(transform_names[i].name, t) == 0) { |
| 909 | transform = transform_names[i].token; |
| 910 | break; |
| 911 | } |
| 912 | } |
| 913 | if (i >= ARRAY_LENGTH(transform_names)) |
| 914 | weston_log("Invalid transform \"%s\" for output %s\n", t, name); |
| 915 | free(t); |
| 916 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 917 | output = wayland_output_create(c, x, y, width, height, name, 0, |
| 918 | transform, scale); |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 919 | free(name); |
| 920 | |
| 921 | return output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 922 | } |
| 923 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 924 | static void |
| 925 | shell_surface_ping(void *data, struct wl_shell_surface *shell_surface, |
| 926 | uint32_t serial) |
| 927 | { |
| 928 | wl_shell_surface_pong(shell_surface, serial); |
| 929 | } |
| 930 | |
| 931 | static void |
| 932 | shell_surface_configure(void *data, struct wl_shell_surface *shell_surface, |
| 933 | uint32_t edges, int32_t width, int32_t height) |
| 934 | { |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 935 | struct wayland_output *output = data; |
| 936 | |
| 937 | output->parent.configure_width = width; |
| 938 | output->parent.configure_height = height; |
| 939 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 940 | /* FIXME: implement resizing */ |
| 941 | } |
| 942 | |
| 943 | static void |
| 944 | shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface) |
| 945 | { |
| 946 | } |
| 947 | |
| 948 | static const struct wl_shell_surface_listener shell_surface_listener = { |
| 949 | shell_surface_ping, |
| 950 | shell_surface_configure, |
| 951 | shell_surface_popup_done |
| 952 | }; |
| 953 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 954 | /* Events received from the wayland-server this compositor is client of: */ |
| 955 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 956 | /* parent input interface */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 957 | static void |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 958 | input_set_cursor(struct wayland_input *input) |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 959 | { |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 960 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 961 | struct wl_buffer *buffer; |
| 962 | struct wl_cursor_image *image; |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 963 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 964 | if (!input->compositor->cursor) |
| 965 | return; /* Couldn't load the cursor. Can't set it */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 966 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 967 | image = input->compositor->cursor->images[0]; |
| 968 | buffer = wl_cursor_image_get_buffer(image); |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 969 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 970 | |
| 971 | wl_pointer_set_cursor(input->parent.pointer, input->enter_serial, |
| 972 | input->parent.cursor.surface, |
| 973 | image->hotspot_x, image->hotspot_y); |
| 974 | |
| 975 | wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0); |
| 976 | wl_surface_damage(input->parent.cursor.surface, 0, 0, |
| 977 | image->width, image->height); |
| 978 | wl_surface_commit(input->parent.cursor.surface); |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 979 | } |
| 980 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 981 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 982 | input_handle_pointer_enter(void *data, struct wl_pointer *pointer, |
| 983 | uint32_t serial, struct wl_surface *surface, |
Kristian Høgsberg | e11bbe4 | 2012-05-09 12:19:04 -0400 | [diff] [blame] | 984 | wl_fixed_t x, wl_fixed_t y) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 985 | { |
| 986 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 987 | int32_t fx, fy; |
| 988 | enum theme_location location; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 989 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 990 | /* XXX: If we get a modifier event immediately before the focus, |
| 991 | * we should try to keep the same serial. */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 992 | input->enter_serial = serial; |
| 993 | input->output = wl_surface_get_user_data(surface); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 994 | |
| 995 | if (input->output->frame) { |
| 996 | location = frame_pointer_enter(input->output->frame, input, |
| 997 | wl_fixed_to_int(x), |
| 998 | wl_fixed_to_int(y)); |
| 999 | frame_interior(input->output->frame, &fx, &fy, NULL, NULL); |
| 1000 | x -= wl_fixed_from_int(fx); |
| 1001 | y -= wl_fixed_from_int(fy); |
| 1002 | |
| 1003 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 1004 | weston_output_schedule_repaint(&input->output->base); |
| 1005 | } else { |
| 1006 | location = THEME_LOCATION_CLIENT_AREA; |
| 1007 | } |
| 1008 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1009 | weston_output_transform_coordinate(&input->output->base, x, y, &x, &y); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1010 | |
| 1011 | if (location == THEME_LOCATION_CLIENT_AREA) { |
| 1012 | input->focus = 1; |
| 1013 | notify_pointer_focus(&input->base, &input->output->base, x, y); |
| 1014 | wl_pointer_set_cursor(input->parent.pointer, |
| 1015 | input->enter_serial, NULL, 0, 0); |
| 1016 | } else { |
| 1017 | input->focus = 0; |
| 1018 | notify_pointer_focus(&input->base, NULL, 0, 0); |
| 1019 | input_set_cursor(input); |
| 1020 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1024 | input_handle_pointer_leave(void *data, struct wl_pointer *pointer, |
| 1025 | uint32_t serial, struct wl_surface *surface) |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1026 | { |
| 1027 | struct wayland_input *input = data; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1028 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1029 | if (input->output->frame) { |
| 1030 | frame_pointer_leave(input->output->frame, input); |
| 1031 | |
| 1032 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 1033 | weston_output_schedule_repaint(&input->output->base); |
| 1034 | } |
| 1035 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1036 | notify_pointer_focus(&input->base, NULL, 0, 0); |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 1037 | input->output = NULL; |
| 1038 | input->focus = 0; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1042 | input_handle_motion(void *data, struct wl_pointer *pointer, |
| 1043 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 1044 | { |
| 1045 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1046 | int32_t fx, fy; |
| 1047 | enum theme_location location; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1048 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1049 | if (input->output->frame) { |
| 1050 | location = frame_pointer_motion(input->output->frame, input, |
| 1051 | wl_fixed_to_int(x), |
| 1052 | wl_fixed_to_int(y)); |
| 1053 | frame_interior(input->output->frame, &fx, &fy, NULL, NULL); |
| 1054 | x -= wl_fixed_from_int(fx); |
| 1055 | y -= wl_fixed_from_int(fy); |
| 1056 | |
| 1057 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 1058 | weston_output_schedule_repaint(&input->output->base); |
| 1059 | } else { |
| 1060 | location = THEME_LOCATION_CLIENT_AREA; |
| 1061 | } |
| 1062 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1063 | weston_output_transform_coordinate(&input->output->base, x, y, &x, &y); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1064 | |
| 1065 | if (input->focus && location != THEME_LOCATION_CLIENT_AREA) { |
| 1066 | input_set_cursor(input); |
| 1067 | notify_pointer_focus(&input->base, NULL, 0, 0); |
| 1068 | input->focus = 0; |
| 1069 | } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) { |
| 1070 | wl_pointer_set_cursor(input->parent.pointer, |
| 1071 | input->enter_serial, NULL, 0, 0); |
| 1072 | notify_pointer_focus(&input->base, &input->output->base, x, y); |
| 1073 | input->focus = 1; |
| 1074 | } |
| 1075 | |
| 1076 | if (location == THEME_LOCATION_CLIENT_AREA) |
| 1077 | notify_motion_absolute(&input->base, time, x, y); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | static void |
| 1081 | input_handle_button(void *data, struct wl_pointer *pointer, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 1082 | uint32_t serial, uint32_t time, uint32_t button, |
| 1083 | uint32_t state_w) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1084 | { |
| 1085 | struct wayland_input *input = data; |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 1086 | enum wl_pointer_button_state state = state_w; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1087 | enum frame_button_state fstate; |
| 1088 | enum theme_location location; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1089 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1090 | if (input->output->frame) { |
| 1091 | fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ? |
| 1092 | FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED; |
| 1093 | |
| 1094 | location = frame_pointer_button(input->output->frame, input, |
| 1095 | button, fstate); |
| 1096 | |
| 1097 | if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) { |
| 1098 | |
| 1099 | wl_shell_surface_move(input->output->parent.shell_surface, |
| 1100 | input->parent.seat, serial); |
| 1101 | frame_status_clear(input->output->frame, |
| 1102 | FRAME_STATUS_MOVE); |
| 1103 | return; |
| 1104 | } |
| 1105 | |
| 1106 | if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) |
| 1107 | wl_display_terminate(input->compositor->base.wl_display); |
| 1108 | |
| 1109 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 1110 | weston_output_schedule_repaint(&input->output->base); |
| 1111 | } else { |
| 1112 | location = THEME_LOCATION_CLIENT_AREA; |
| 1113 | } |
| 1114 | |
| 1115 | if (location == THEME_LOCATION_CLIENT_AREA) |
| 1116 | notify_button(&input->base, time, button, state); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | static void |
| 1120 | input_handle_axis(void *data, struct wl_pointer *pointer, |
Daniel Stone | 2fce402 | 2012-05-30 16:32:00 +0100 | [diff] [blame] | 1121 | uint32_t time, uint32_t axis, wl_fixed_t value) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1122 | { |
| 1123 | struct wayland_input *input = data; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1124 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1125 | notify_axis(&input->base, time, axis, value); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | static const struct wl_pointer_listener pointer_listener = { |
| 1129 | input_handle_pointer_enter, |
| 1130 | input_handle_pointer_leave, |
| 1131 | input_handle_motion, |
| 1132 | input_handle_button, |
| 1133 | input_handle_axis, |
| 1134 | }; |
| 1135 | |
| 1136 | static void |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1137 | input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, |
| 1138 | int fd, uint32_t size) |
| 1139 | { |
| 1140 | struct wayland_input *input = data; |
| 1141 | struct xkb_keymap *keymap; |
| 1142 | char *map_str; |
| 1143 | |
| 1144 | if (!data) { |
| 1145 | close(fd); |
| 1146 | return; |
| 1147 | } |
| 1148 | |
| 1149 | if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { |
| 1150 | close(fd); |
| 1151 | return; |
| 1152 | } |
| 1153 | |
| 1154 | map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); |
| 1155 | if (map_str == MAP_FAILED) { |
| 1156 | close(fd); |
| 1157 | return; |
| 1158 | } |
| 1159 | |
| 1160 | keymap = xkb_map_new_from_string(input->compositor->base.xkb_context, |
| 1161 | map_str, |
| 1162 | XKB_KEYMAP_FORMAT_TEXT_V1, |
| 1163 | 0); |
| 1164 | munmap(map_str, size); |
| 1165 | close(fd); |
| 1166 | |
| 1167 | if (!keymap) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1168 | weston_log("failed to compile keymap\n"); |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1169 | return; |
| 1170 | } |
| 1171 | |
Rui Matos | 0c194ce | 2013-10-10 19:44:21 +0200 | [diff] [blame] | 1172 | if (input->base.keyboard) |
| 1173 | weston_seat_update_keymap(&input->base, keymap); |
| 1174 | else |
| 1175 | weston_seat_init_keyboard(&input->base, keymap); |
| 1176 | |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1177 | xkb_map_unref(keymap); |
| 1178 | } |
| 1179 | |
| 1180 | static void |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1181 | input_handle_keyboard_enter(void *data, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1182 | struct wl_keyboard *keyboard, |
| 1183 | uint32_t serial, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1184 | struct wl_surface *surface, |
| 1185 | struct wl_array *keys) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1186 | { |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 1187 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1188 | struct wayland_output *focus; |
| 1189 | |
| 1190 | focus = input->keyboard_focus; |
| 1191 | if (focus) { |
| 1192 | /* This shouldn't happen */ |
| 1193 | focus->keyboard_count--; |
| 1194 | if (!focus->keyboard_count && focus->frame) |
| 1195 | frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE); |
| 1196 | if (frame_status(focus->frame) & FRAME_STATUS_REPAINT) |
| 1197 | weston_output_schedule_repaint(&focus->base); |
| 1198 | } |
| 1199 | |
| 1200 | input->keyboard_focus = wl_surface_get_user_data(surface); |
| 1201 | input->keyboard_focus->keyboard_count++; |
| 1202 | |
| 1203 | focus = input->keyboard_focus; |
| 1204 | if (focus->frame) { |
| 1205 | frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE); |
| 1206 | if (frame_status(focus->frame) & FRAME_STATUS_REPAINT) |
| 1207 | weston_output_schedule_repaint(&focus->base); |
| 1208 | } |
| 1209 | |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 1210 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1211 | /* XXX: If we get a modifier event immediately before the focus, |
| 1212 | * we should try to keep the same serial. */ |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1213 | notify_keyboard_focus_in(&input->base, keys, |
Daniel Stone | d6da09e | 2012-06-22 13:21:29 +0100 | [diff] [blame] | 1214 | STATE_UPDATE_AUTOMATIC); |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | static void |
| 1218 | input_handle_keyboard_leave(void *data, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1219 | struct wl_keyboard *keyboard, |
| 1220 | uint32_t serial, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1221 | struct wl_surface *surface) |
| 1222 | { |
| 1223 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1224 | struct wayland_output *focus; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1225 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1226 | notify_keyboard_focus_out(&input->base); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1227 | |
| 1228 | focus = input->keyboard_focus; |
| 1229 | if (!focus) |
| 1230 | return; /* This shouldn't happen */ |
| 1231 | |
| 1232 | focus->keyboard_count--; |
| 1233 | if (!focus->keyboard_count && focus->frame) { |
| 1234 | frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE); |
| 1235 | if (frame_status(focus->frame) & FRAME_STATUS_REPAINT) |
| 1236 | weston_output_schedule_repaint(&focus->base); |
| 1237 | } |
| 1238 | |
| 1239 | input->keyboard_focus = NULL; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1240 | } |
| 1241 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1242 | static void |
| 1243 | input_handle_key(void *data, struct wl_keyboard *keyboard, |
| 1244 | uint32_t serial, uint32_t time, uint32_t key, uint32_t state) |
| 1245 | { |
| 1246 | struct wayland_input *input = data; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1247 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1248 | input->key_serial = serial; |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1249 | notify_key(&input->base, time, key, |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 1250 | state ? WL_KEYBOARD_KEY_STATE_PRESSED : |
Daniel Stone | 1b4e11f | 2012-06-22 13:21:37 +0100 | [diff] [blame] | 1251 | WL_KEYBOARD_KEY_STATE_RELEASED, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1252 | STATE_UPDATE_NONE); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1253 | } |
| 1254 | |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1255 | static void |
| 1256 | input_handle_modifiers(void *data, struct wl_keyboard *keyboard, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1257 | uint32_t serial_in, uint32_t mods_depressed, |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1258 | uint32_t mods_latched, uint32_t mods_locked, |
| 1259 | uint32_t group) |
| 1260 | { |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1261 | struct wayland_input *input = data; |
| 1262 | struct wayland_compositor *c = input->compositor; |
| 1263 | uint32_t serial_out; |
| 1264 | |
| 1265 | /* If we get a key event followed by a modifier event with the |
| 1266 | * same serial number, then we try to preserve those semantics by |
| 1267 | * reusing the same serial number on the way out too. */ |
| 1268 | if (serial_in == input->key_serial) |
| 1269 | serial_out = wl_display_get_serial(c->base.wl_display); |
| 1270 | else |
| 1271 | serial_out = wl_display_next_serial(c->base.wl_display); |
| 1272 | |
Jonas Ådahl | 7395ea0 | 2013-12-03 09:14:26 +0100 | [diff] [blame] | 1273 | xkb_state_update_mask(input->base.keyboard->xkb_state.state, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 1274 | mods_depressed, mods_latched, |
| 1275 | mods_locked, 0, 0, group); |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1276 | notify_modifiers(&input->base, serial_out); |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1277 | } |
| 1278 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1279 | static const struct wl_keyboard_listener keyboard_listener = { |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 1280 | input_handle_keymap, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 1281 | input_handle_keyboard_enter, |
| 1282 | input_handle_keyboard_leave, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1283 | input_handle_key, |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 1284 | input_handle_modifiers, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1285 | }; |
| 1286 | |
| 1287 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1288 | input_handle_capabilities(void *data, struct wl_seat *seat, |
| 1289 | enum wl_seat_capability caps) |
| 1290 | { |
| 1291 | struct wayland_input *input = data; |
| 1292 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1293 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) { |
| 1294 | input->parent.pointer = wl_seat_get_pointer(seat); |
| 1295 | wl_pointer_set_user_data(input->parent.pointer, input); |
| 1296 | wl_pointer_add_listener(input->parent.pointer, |
| 1297 | &pointer_listener, input); |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 1298 | weston_seat_init_pointer(&input->base); |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1299 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) { |
| 1300 | wl_pointer_destroy(input->parent.pointer); |
| 1301 | input->parent.pointer = NULL; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1302 | } |
| 1303 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1304 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) { |
| 1305 | input->parent.keyboard = wl_seat_get_keyboard(seat); |
| 1306 | wl_keyboard_set_user_data(input->parent.keyboard, input); |
| 1307 | wl_keyboard_add_listener(input->parent.keyboard, |
| 1308 | &keyboard_listener, input); |
| 1309 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) { |
| 1310 | wl_keyboard_destroy(input->parent.keyboard); |
| 1311 | input->parent.keyboard = NULL; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | static const struct wl_seat_listener seat_listener = { |
| 1316 | input_handle_capabilities, |
| 1317 | }; |
| 1318 | |
| 1319 | static void |
| 1320 | display_add_seat(struct wayland_compositor *c, uint32_t id) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1321 | { |
| 1322 | struct wayland_input *input; |
| 1323 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 1324 | input = zalloc(sizeof *input); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1325 | if (input == NULL) |
| 1326 | return; |
| 1327 | |
Rob Bradford | 9af5f9e | 2013-05-31 18:09:50 +0100 | [diff] [blame] | 1328 | weston_seat_init(&input->base, &c->base, "default"); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1329 | input->compositor = c; |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1330 | input->parent.seat = wl_registry_bind(c->parent.registry, id, |
| 1331 | &wl_seat_interface, 1); |
Jason Ekstrand | 06ced80 | 2013-11-07 20:13:29 -0600 | [diff] [blame] | 1332 | wl_list_insert(c->input_list.prev, &input->link); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1333 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1334 | wl_seat_add_listener(input->parent.seat, &seat_listener, input); |
| 1335 | wl_seat_set_user_data(input->parent.seat, input); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1336 | |
| 1337 | input->parent.cursor.surface = |
| 1338 | wl_compositor_create_surface(c->parent.compositor); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1342 | registry_handle_global(void *data, struct wl_registry *registry, uint32_t name, |
| 1343 | const char *interface, uint32_t version) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1344 | { |
| 1345 | struct wayland_compositor *c = data; |
| 1346 | |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 1347 | if (strcmp(interface, "wl_compositor") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 1348 | c->parent.compositor = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1349 | wl_registry_bind(registry, name, |
| 1350 | &wl_compositor_interface, 1); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 1351 | } else if (strcmp(interface, "wl_shell") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 1352 | c->parent.shell = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1353 | wl_registry_bind(registry, name, |
| 1354 | &wl_shell_interface, 1); |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1355 | } else if (strcmp(interface, "wl_seat") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1356 | display_add_seat(c, name); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 1357 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 1358 | c->parent.shm = |
| 1359 | wl_registry_bind(registry, name, &wl_shm_interface, 1); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1360 | } |
| 1361 | } |
| 1362 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1363 | static const struct wl_registry_listener registry_listener = { |
| 1364 | registry_handle_global |
| 1365 | }; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1366 | |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 1367 | static int |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1368 | wayland_compositor_handle_event(int fd, uint32_t mask, void *data) |
| 1369 | { |
| 1370 | struct wayland_compositor *c = data; |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 1371 | int count = 0; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1372 | |
Kristian Høgsberg | 453de7a | 2013-10-30 23:15:44 -0700 | [diff] [blame] | 1373 | if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) { |
| 1374 | wl_display_terminate(c->base.wl_display); |
| 1375 | return 0; |
| 1376 | } |
| 1377 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1378 | if (mask & WL_EVENT_READABLE) |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 1379 | count = wl_display_dispatch(c->parent.wl_display); |
Kristian Høgsberg | f258a31 | 2011-12-28 22:51:20 -0500 | [diff] [blame] | 1380 | if (mask & WL_EVENT_WRITABLE) |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1381 | wl_display_flush(c->parent.wl_display); |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 1382 | |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 1383 | if (mask == 0) { |
| 1384 | count = wl_display_dispatch_pending(c->parent.wl_display); |
| 1385 | wl_display_flush(c->parent.wl_display); |
| 1386 | } |
| 1387 | |
| 1388 | return count; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1389 | } |
| 1390 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1391 | static void |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 1392 | wayland_restore(struct weston_compositor *ec) |
| 1393 | { |
| 1394 | } |
| 1395 | |
| 1396 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1397 | wayland_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1398 | { |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 1399 | struct wayland_compositor *c = (struct wayland_compositor *) ec; |
| 1400 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1401 | weston_compositor_shutdown(ec); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1402 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 1403 | if (c->parent.shm) |
| 1404 | wl_shm_destroy(c->parent.shm); |
| 1405 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1406 | free(ec); |
| 1407 | } |
| 1408 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1409 | static const char *left_ptrs[] = { |
| 1410 | "left_ptr", |
| 1411 | "default", |
| 1412 | "top_left_arrow", |
| 1413 | "left-arrow" |
| 1414 | }; |
| 1415 | |
| 1416 | static void |
| 1417 | create_cursor(struct wayland_compositor *c, struct weston_config *config) |
| 1418 | { |
| 1419 | struct weston_config_section *s; |
| 1420 | int size; |
| 1421 | char *theme = NULL; |
| 1422 | unsigned int i; |
| 1423 | |
| 1424 | s = weston_config_get_section(config, "shell", NULL, NULL); |
| 1425 | weston_config_section_get_string(s, "cursor-theme", &theme, NULL); |
| 1426 | weston_config_section_get_int(s, "cursor-size", &size, 32); |
| 1427 | |
| 1428 | c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm); |
| 1429 | |
| 1430 | c->cursor = NULL; |
| 1431 | for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i) |
| 1432 | c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme, |
| 1433 | left_ptrs[i]); |
| 1434 | if (!c->cursor) { |
| 1435 | fprintf(stderr, "could not load left cursor\n"); |
| 1436 | return; |
| 1437 | } |
| 1438 | } |
| 1439 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1440 | static void |
| 1441 | fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key, |
| 1442 | void *data) |
| 1443 | { |
| 1444 | struct wayland_compositor *c = data; |
| 1445 | struct wayland_input *input = NULL; |
| 1446 | |
| 1447 | wl_list_for_each(input, &c->input_list, link) |
| 1448 | if (&input->base == seat_base) |
| 1449 | break; |
| 1450 | |
| 1451 | if (!input || !input->output) |
| 1452 | return; |
| 1453 | |
| 1454 | if (input->output->frame) |
| 1455 | wayland_output_set_fullscreen(input->output, 0, 0, NULL); |
| 1456 | else |
| 1457 | wayland_output_set_windowed(input->output); |
| 1458 | |
| 1459 | weston_output_schedule_repaint(&input->output->base); |
| 1460 | } |
| 1461 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1462 | static struct wayland_compositor * |
| 1463 | wayland_compositor_create(struct wl_display *display, int use_pixman, |
| 1464 | const char *display_name, int *argc, char *argv[], |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 1465 | struct weston_config *config) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1466 | { |
| 1467 | struct wayland_compositor *c; |
| 1468 | struct wl_event_loop *loop; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1469 | int fd; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1470 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 1471 | c = zalloc(sizeof *c); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1472 | if (c == NULL) |
| 1473 | return NULL; |
| 1474 | |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1475 | if (weston_compositor_init(&c->base, display, argc, argv, |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 1476 | config) < 0) |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 1477 | goto err_free; |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1478 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1479 | c->parent.wl_display = wl_display_connect(display_name); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1480 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1481 | if (c->parent.wl_display == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1482 | weston_log("failed to create display: %m\n"); |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 1483 | goto err_compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1484 | } |
| 1485 | |
Jason Ekstrand | 06ced80 | 2013-11-07 20:13:29 -0600 | [diff] [blame] | 1486 | wl_list_init(&c->input_list); |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1487 | c->parent.registry = wl_display_get_registry(c->parent.wl_display); |
| 1488 | wl_registry_add_listener(c->parent.registry, ®istry_listener, c); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame] | 1489 | wl_display_roundtrip(c->parent.wl_display); |
| 1490 | |
| 1491 | create_cursor(c, config); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1492 | |
| 1493 | c->base.wl_display = display; |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 1494 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1495 | c->use_pixman = use_pixman; |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 1496 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1497 | if (!c->use_pixman) { |
| 1498 | gl_renderer = weston_load_module("gl-renderer.so", |
| 1499 | "gl_renderer_interface"); |
| 1500 | if (!gl_renderer) |
| 1501 | c->use_pixman = 1; |
| 1502 | } |
| 1503 | |
| 1504 | if (!c->use_pixman) { |
Jason Ekstrand | ff2fd46 | 2013-10-27 22:24:58 -0500 | [diff] [blame] | 1505 | if (gl_renderer->create(&c->base, c->parent.wl_display, |
| 1506 | gl_renderer->alpha_attribs, |
| 1507 | NULL) < 0) { |
| 1508 | weston_log("Failed to initialize the GL renderer; " |
| 1509 | "falling back to pixman.\n"); |
| 1510 | c->use_pixman = 1; |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | if (c->use_pixman) { |
| 1515 | if (pixman_renderer_init(&c->base) < 0) { |
| 1516 | weston_log("Failed to initialize pixman renderer\n"); |
| 1517 | goto err_display; |
| 1518 | } |
| 1519 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1520 | |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 1521 | c->base.destroy = wayland_destroy; |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 1522 | c->base.restore = wayland_restore; |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 1523 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1524 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 1525 | |
| 1526 | fd = wl_display_get_fd(c->parent.wl_display); |
| 1527 | c->parent.wl_source = |
| 1528 | wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE, |
| 1529 | wayland_compositor_handle_event, c); |
| 1530 | if (c->parent.wl_source == NULL) |
| 1531 | goto err_renderer; |
| 1532 | |
| 1533 | wl_event_source_check(c->parent.wl_source); |
| 1534 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1535 | weston_compositor_add_key_binding(&c->base, KEY_F, |
| 1536 | MODIFIER_CTRL | MODIFIER_ALT, |
| 1537 | fullscreen_binding, c); |
| 1538 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1539 | return c; |
| 1540 | err_renderer: |
| 1541 | c->base.renderer->destroy(&c->base); |
| 1542 | err_display: |
| 1543 | wl_display_disconnect(c->parent.wl_display); |
| 1544 | err_compositor: |
| 1545 | weston_compositor_shutdown(&c->base); |
| 1546 | err_free: |
| 1547 | free(c); |
| 1548 | return NULL; |
| 1549 | } |
| 1550 | |
| 1551 | static void |
| 1552 | wayland_compositor_destroy(struct wayland_compositor *c) |
| 1553 | { |
| 1554 | struct weston_output *output; |
| 1555 | |
| 1556 | wl_list_for_each(output, &c->base.output_list, link) |
| 1557 | wayland_output_destroy(output); |
| 1558 | |
| 1559 | c->base.renderer->destroy(&c->base); |
| 1560 | wl_display_disconnect(c->parent.wl_display); |
| 1561 | |
| 1562 | if (c->theme) |
| 1563 | theme_destroy(c->theme); |
| 1564 | if (c->frame_device) |
| 1565 | cairo_device_destroy(c->frame_device); |
| 1566 | wl_cursor_theme_destroy(c->cursor_theme); |
| 1567 | |
| 1568 | weston_compositor_shutdown(&c->base); |
| 1569 | free(c); |
| 1570 | } |
| 1571 | |
| 1572 | WL_EXPORT struct weston_compositor * |
| 1573 | backend_init(struct wl_display *display, int *argc, char *argv[], |
| 1574 | struct weston_config *config) |
| 1575 | { |
| 1576 | struct wayland_compositor *c; |
| 1577 | struct wayland_output *output; |
| 1578 | struct weston_config_section *section; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1579 | int x, count, width, height, scale, use_pixman, fullscreen; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1580 | const char *section_name, *display_name; |
| 1581 | char *name; |
| 1582 | |
| 1583 | const struct weston_option wayland_options[] = { |
| 1584 | { WESTON_OPTION_INTEGER, "width", 0, &width }, |
| 1585 | { WESTON_OPTION_INTEGER, "height", 0, &height }, |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame] | 1586 | { WESTON_OPTION_INTEGER, "scale", 0, &scale }, |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1587 | { WESTON_OPTION_STRING, "display", 0, &display_name }, |
| 1588 | { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman }, |
| 1589 | { WESTON_OPTION_INTEGER, "output-count", 0, &count }, |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1590 | { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen }, |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1591 | }; |
| 1592 | |
| 1593 | width = 0; |
| 1594 | height = 0; |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame] | 1595 | scale = 0; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1596 | display_name = NULL; |
| 1597 | use_pixman = 0; |
| 1598 | count = 1; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1599 | fullscreen = 0; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1600 | parse_options(wayland_options, |
| 1601 | ARRAY_LENGTH(wayland_options), argc, argv); |
| 1602 | |
| 1603 | c = wayland_compositor_create(display, use_pixman, display_name, |
| 1604 | argc, argv, config); |
| 1605 | if (!c) |
| 1606 | return NULL; |
| 1607 | |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1608 | if (fullscreen) { |
| 1609 | output = wayland_output_create(c, 0, 0, width, height, |
| 1610 | NULL, 1, 0, 1); |
| 1611 | if (!output) |
| 1612 | goto err_outputs; |
| 1613 | |
Axel Davy | dd8b88d | 2013-11-17 21:34:16 +0100 | [diff] [blame] | 1614 | wayland_output_set_fullscreen(output, 0, 0, NULL); |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1615 | return &c->base; |
| 1616 | } |
| 1617 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1618 | section = NULL; |
| 1619 | x = 0; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1620 | while (weston_config_next_section(config, §ion, §ion_name)) { |
| 1621 | if (!section_name || strcmp(section_name, "output") != 0) |
| 1622 | continue; |
| 1623 | weston_config_section_get_string(section, "name", &name, NULL); |
| 1624 | if (name == NULL) |
| 1625 | continue; |
| 1626 | |
| 1627 | if (name[0] != 'W' || name[1] != 'L') { |
| 1628 | free(name); |
| 1629 | continue; |
| 1630 | } |
| 1631 | free(name); |
| 1632 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1633 | output = wayland_output_create_for_config(c, section, width, |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame] | 1634 | height, scale, x, 0); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1635 | if (!output) |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1636 | goto err_outputs; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1637 | if (wayland_output_set_windowed(output)) |
| 1638 | goto err_outputs; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1639 | |
| 1640 | x += output->base.width; |
| 1641 | --count; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1642 | } |
| 1643 | |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1644 | if (!width) |
| 1645 | width = 1024; |
| 1646 | if (!height) |
| 1647 | height = 640; |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame] | 1648 | if (!scale) |
| 1649 | scale = 1; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1650 | while (count > 0) { |
Jason Ekstrand | 12c6dd9 | 2013-11-07 20:13:32 -0600 | [diff] [blame] | 1651 | output = wayland_output_create(c, x, 0, width, height, |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1652 | NULL, 0, 0, scale); |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1653 | if (!output) |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1654 | goto err_outputs; |
Jason Ekstrand | 5ea0480 | 2013-11-07 20:13:33 -0600 | [diff] [blame] | 1655 | if (wayland_output_set_windowed(output)) |
| 1656 | goto err_outputs; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1657 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1658 | x += width; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1659 | --count; |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1660 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1661 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1662 | return &c->base; |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1663 | |
Jason Ekstrand | 48ce421 | 2013-10-27 22:25:02 -0500 | [diff] [blame] | 1664 | err_outputs: |
Jason Ekstrand | 0cf3935 | 2013-11-07 20:13:31 -0600 | [diff] [blame] | 1665 | wayland_compositor_destroy(c); |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 1666 | return NULL; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1667 | } |