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 |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 3 | * |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 13 | * |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 21 | */ |
| 22 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 23 | #include "config.h" |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 24 | |
| 25 | #include <stddef.h> |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <unistd.h> |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 31 | #include <sys/mman.h> |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 32 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 33 | #include <wayland-client.h> |
| 34 | #include <wayland-egl.h> |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 35 | #include <wayland-cursor.h> |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 36 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 37 | #include "compositor.h" |
John Kåre Alsaker | 30d2b1f | 2012-11-13 19:10:28 +0100 | [diff] [blame] | 38 | #include "gl-renderer.h" |
Kristian Høgsberg | 3c2360f | 2013-01-28 16:01:22 -0500 | [diff] [blame] | 39 | #include "../shared/image-loader.h" |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 40 | #include "../shared/os-compatibility.h" |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 41 | #include "../shared/cairo-util.h" |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 42 | |
| 43 | struct wayland_compositor { |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 44 | struct weston_compositor base; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 45 | |
| 46 | struct { |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 47 | struct wl_display *wl_display; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 48 | struct wl_registry *registry; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 49 | struct wl_compositor *compositor; |
| 50 | struct wl_shell *shell; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 51 | struct wl_output *output; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 52 | struct wl_shm *shm; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 53 | |
| 54 | struct { |
| 55 | int32_t x, y, width, height; |
| 56 | } screen_allocation; |
| 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 | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 62 | struct theme *theme; |
| 63 | cairo_device_t *frame_device; |
| 64 | struct wl_cursor_theme *cursor_theme; |
| 65 | struct wl_cursor *cursor; |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 66 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 67 | struct wl_list inputs; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | struct wayland_output { |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 71 | struct weston_output base; |
| 72 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 73 | struct { |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 74 | int draw_initial_frame; |
| 75 | struct wl_surface *surface; |
| 76 | struct wl_shell_surface *shell_surface; |
| 77 | struct wl_egl_window *egl_window; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 78 | } parent; |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 79 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 80 | int keyboard_count; |
| 81 | |
| 82 | struct frame *frame; |
| 83 | struct { |
| 84 | cairo_surface_t *top; |
| 85 | cairo_surface_t *left; |
| 86 | cairo_surface_t *right; |
| 87 | cairo_surface_t *bottom; |
| 88 | } border; |
| 89 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 90 | struct weston_mode mode; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | struct wayland_input { |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 94 | struct weston_seat base; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 95 | struct wayland_compositor *compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 96 | struct wl_list link; |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 97 | |
| 98 | struct { |
| 99 | struct wl_seat *seat; |
| 100 | struct wl_pointer *pointer; |
| 101 | struct wl_keyboard *keyboard; |
| 102 | struct wl_touch *touch; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 103 | |
| 104 | struct { |
| 105 | struct wl_surface *surface; |
| 106 | int32_t hx, hy; |
| 107 | } cursor; |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 108 | } parent; |
| 109 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 110 | uint32_t key_serial; |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 111 | uint32_t enter_serial; |
| 112 | int focus; |
| 113 | struct wayland_output *output; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 114 | struct wayland_output *keyboard_focus; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 115 | }; |
| 116 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 117 | struct gl_renderer_interface *gl_renderer; |
| 118 | |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 119 | static void |
Kristian Høgsberg | cdd61d0 | 2012-02-07 09:56:15 -0500 | [diff] [blame] | 120 | frame_done(void *data, struct wl_callback *callback, uint32_t time) |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 121 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 122 | struct weston_output *output = data; |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 123 | |
Kristian Høgsberg | cdd61d0 | 2012-02-07 09:56:15 -0500 | [diff] [blame] | 124 | wl_callback_destroy(callback); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 125 | weston_output_finish_frame(output, time); |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static const struct wl_callback_listener frame_listener = { |
| 129 | frame_done |
| 130 | }; |
| 131 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 132 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 133 | buffer_release(void *data, struct wl_buffer *buffer) |
| 134 | { |
| 135 | wl_buffer_destroy(buffer); |
| 136 | } |
| 137 | |
| 138 | static const struct wl_buffer_listener buffer_listener = { |
| 139 | buffer_release |
| 140 | }; |
| 141 | |
| 142 | static void |
| 143 | draw_initial_frame(struct wayland_output *output) |
| 144 | { |
| 145 | struct wayland_compositor *c = |
| 146 | (struct wayland_compositor *) output->base.compositor; |
| 147 | struct wl_shm *shm = c->parent.shm; |
| 148 | struct wl_surface *surface = output->parent.surface; |
| 149 | struct wl_shm_pool *pool; |
| 150 | struct wl_buffer *buffer; |
| 151 | |
| 152 | int width, height, stride; |
| 153 | int size; |
| 154 | int fd; |
| 155 | void *data; |
| 156 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 157 | if (output->frame) { |
| 158 | width = frame_width(output->frame); |
| 159 | height = frame_height(output->frame); |
| 160 | } else { |
| 161 | width = output->mode.width; |
| 162 | height = output->mode.height; |
| 163 | } |
| 164 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 165 | stride = width * 4; |
| 166 | size = height * stride; |
| 167 | |
| 168 | fd = os_create_anonymous_file(size); |
| 169 | if (fd < 0) { |
| 170 | perror("os_create_anonymous_file"); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 175 | if (data == MAP_FAILED) { |
| 176 | perror("mmap"); |
| 177 | close(fd); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | pool = wl_shm_create_pool(shm, fd, size); |
| 182 | |
| 183 | buffer = wl_shm_pool_create_buffer(pool, 0, |
| 184 | width, height, |
| 185 | stride, |
| 186 | WL_SHM_FORMAT_ARGB8888); |
| 187 | wl_buffer_add_listener(buffer, &buffer_listener, buffer); |
| 188 | wl_shm_pool_destroy(pool); |
| 189 | close(fd); |
| 190 | |
| 191 | memset(data, 0, size); |
| 192 | |
| 193 | wl_surface_attach(surface, buffer, 0, 0); |
| 194 | |
| 195 | /* We only need to damage some part, as its only transparant |
| 196 | * pixels anyway. */ |
| 197 | wl_surface_damage(surface, 0, 0, 1, 1); |
| 198 | } |
| 199 | |
| 200 | static void |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 201 | wayland_output_update_gl_border(struct wayland_output *output) |
| 202 | { |
| 203 | int32_t ix, iy, iwidth, iheight, fwidth, fheight; |
| 204 | cairo_t *cr; |
| 205 | |
| 206 | if (!output->frame) |
| 207 | return; |
| 208 | if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT)) |
| 209 | return; |
| 210 | |
| 211 | fwidth = frame_width(output->frame); |
| 212 | fheight = frame_height(output->frame); |
| 213 | frame_interior(output->frame, &ix, &iy, &iwidth, &iheight); |
| 214 | |
| 215 | if (!output->border.top) |
| 216 | output->border.top = |
| 217 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 218 | fwidth, iy); |
| 219 | cr = cairo_create(output->border.top); |
| 220 | frame_repaint(output->frame, cr); |
| 221 | cairo_destroy(cr); |
| 222 | gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP, |
| 223 | fwidth, iy, |
| 224 | cairo_image_surface_get_stride(output->border.top) / 4, |
| 225 | cairo_image_surface_get_data(output->border.top)); |
| 226 | |
| 227 | |
| 228 | if (!output->border.left) |
| 229 | output->border.left = |
| 230 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 231 | ix, 1); |
| 232 | cr = cairo_create(output->border.left); |
| 233 | cairo_translate(cr, 0, -iy); |
| 234 | frame_repaint(output->frame, cr); |
| 235 | cairo_destroy(cr); |
| 236 | gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT, |
| 237 | ix, 1, |
| 238 | cairo_image_surface_get_stride(output->border.left) / 4, |
| 239 | cairo_image_surface_get_data(output->border.left)); |
| 240 | |
| 241 | |
| 242 | if (!output->border.right) |
| 243 | output->border.right = |
| 244 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 245 | fwidth - (ix + iwidth), 1); |
| 246 | cr = cairo_create(output->border.right); |
| 247 | cairo_translate(cr, -(iwidth + ix), -iy); |
| 248 | frame_repaint(output->frame, cr); |
| 249 | cairo_destroy(cr); |
| 250 | gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT, |
| 251 | fwidth - (ix + iwidth), 1, |
| 252 | cairo_image_surface_get_stride(output->border.right) / 4, |
| 253 | cairo_image_surface_get_data(output->border.right)); |
| 254 | |
| 255 | |
| 256 | if (!output->border.bottom) |
| 257 | output->border.bottom = |
| 258 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 259 | fwidth, fheight - (iy + iheight)); |
| 260 | cr = cairo_create(output->border.bottom); |
| 261 | cairo_translate(cr, 0, -(iy + iheight)); |
| 262 | frame_repaint(output->frame, cr); |
| 263 | cairo_destroy(cr); |
| 264 | gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM, |
| 265 | fwidth, fheight - (iy + iheight), |
| 266 | cairo_image_surface_get_stride(output->border.bottom) / 4, |
| 267 | cairo_image_surface_get_data(output->border.bottom)); |
| 268 | } |
| 269 | |
| 270 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 271 | wayland_output_start_repaint_loop(struct weston_output *output_base) |
| 272 | { |
| 273 | struct wayland_output *output = (struct wayland_output *) output_base; |
Jason Ekstrand | 286ff68 | 2013-10-27 22:24:57 -0500 | [diff] [blame] | 274 | struct wayland_compositor *wc = |
| 275 | (struct wayland_compositor *)output->base.compositor; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 276 | struct wl_callback *callback; |
| 277 | |
| 278 | /* If this is the initial frame, we need to attach a buffer so that |
| 279 | * the compositor can map the surface and include it in its render |
| 280 | * loop. If the surface doesn't end up in the render loop, the frame |
| 281 | * callback won't be invoked. The buffer is transparent and of the |
| 282 | * same size as the future real output buffer. */ |
| 283 | if (output->parent.draw_initial_frame) { |
| 284 | output->parent.draw_initial_frame = 0; |
| 285 | |
| 286 | draw_initial_frame(output); |
| 287 | } |
| 288 | |
| 289 | callback = wl_surface_frame(output->parent.surface); |
| 290 | wl_callback_add_listener(callback, &frame_listener, output); |
| 291 | wl_surface_commit(output->parent.surface); |
Jason Ekstrand | 286ff68 | 2013-10-27 22:24:57 -0500 | [diff] [blame] | 292 | wl_display_flush(wc->parent.wl_display); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 293 | } |
| 294 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 295 | static int |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 296 | wayland_output_repaint(struct weston_output *output_base, |
| 297 | pixman_region32_t *damage) |
| 298 | { |
| 299 | struct wayland_output *output = (struct wayland_output *) output_base; |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame] | 300 | struct weston_compositor *ec = output->base.compositor; |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 301 | struct wl_callback *callback; |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 302 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 303 | callback = wl_surface_frame(output->parent.surface); |
| 304 | wl_callback_add_listener(callback, &frame_listener, output); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 305 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 306 | wayland_output_update_gl_border(output); |
| 307 | |
Pekka Paalanen | bc10638 | 2012-10-10 12:49:31 +0300 | [diff] [blame] | 308 | ec->renderer->repaint_output(&output->base, damage); |
Ander Conselvan de Oliveira | 0a88772 | 2012-11-22 15:57:00 +0200 | [diff] [blame] | 309 | |
| 310 | pixman_region32_subtract(&ec->primary_plane.damage, |
| 311 | &ec->primary_plane.damage, damage); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 312 | return 0; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 313 | } |
| 314 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 315 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 316 | wayland_output_destroy(struct weston_output *output_base) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 317 | { |
| 318 | struct wayland_output *output = (struct wayland_output *) output_base; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 319 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 320 | gl_renderer->output_destroy(output_base); |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 321 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 322 | wl_egl_window_destroy(output->parent.egl_window); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 323 | wl_surface_destroy(output->parent.surface); |
| 324 | wl_shell_surface_destroy(output->parent.shell_surface); |
| 325 | |
| 326 | if (output->frame) { |
| 327 | frame_destroy(output->frame); |
| 328 | cairo_surface_destroy(output->border.top); |
| 329 | cairo_surface_destroy(output->border.left); |
| 330 | cairo_surface_destroy(output->border.right); |
| 331 | cairo_surface_destroy(output->border.bottom); |
| 332 | } |
| 333 | |
| 334 | weston_output_destroy(&output->base); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 335 | free(output); |
| 336 | |
| 337 | return; |
| 338 | } |
| 339 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 340 | static const struct wl_shell_surface_listener shell_surface_listener; |
| 341 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 342 | static int |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 343 | wayland_compositor_create_output(struct wayland_compositor *c, |
| 344 | int width, int height) |
| 345 | { |
| 346 | struct wayland_output *output; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 347 | int32_t fx, fy, fwidth, fheight; |
| 348 | struct wl_region *region; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 349 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 350 | output = zalloc(sizeof *output); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 351 | if (output == NULL) |
| 352 | return -1; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 353 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 354 | output->mode.flags = |
| 355 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 356 | output->mode.width = width; |
| 357 | output->mode.height = height; |
| 358 | output->mode.refresh = 60; |
| 359 | wl_list_init(&output->base.mode_list); |
| 360 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
| 361 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 362 | output->base.current_mode = &output->mode; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 363 | weston_output_init(&output->base, &c->base, 0, 0, width, height, |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 364 | WL_OUTPUT_TRANSFORM_NORMAL, 1); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 365 | |
Kristian Høgsberg | 90bc88c | 2012-08-13 23:34:04 -0400 | [diff] [blame] | 366 | output->base.make = "waywayland"; |
| 367 | output->base.model = "none"; |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 368 | |
| 369 | weston_output_move(&output->base, 0, 0); |
| 370 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 371 | if (!c->theme) |
| 372 | c->theme = theme_create(); |
| 373 | output->frame = frame_create(c->theme, width, height, |
| 374 | FRAME_BUTTON_CLOSE, "Weston"); |
| 375 | frame_resize_inside(output->frame, width, height); |
| 376 | frame_interior(output->frame, &fx, &fy, NULL, NULL); |
| 377 | fwidth = frame_width(output->frame); |
| 378 | fheight = frame_height(output->frame); |
| 379 | |
| 380 | weston_log("Creating %dx%d wayland output (%dx%d actual)\n", |
| 381 | width, height, fwidth, fheight); |
| 382 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 383 | output->parent.surface = |
| 384 | wl_compositor_create_surface(c->parent.compositor); |
| 385 | wl_surface_set_user_data(output->parent.surface, output); |
| 386 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 387 | output->parent.egl_window = |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 388 | wl_egl_window_create(output->parent.surface, |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 389 | fwidth, fheight); |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 390 | if (!output->parent.egl_window) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 391 | weston_log("failure to create wl_egl_window\n"); |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 392 | goto cleanup_output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 393 | } |
| 394 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 395 | if (gl_renderer->output_create(&output->base, |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 396 | output->parent.egl_window) < 0) |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 397 | goto cleanup_window; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 398 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 399 | output->base.border.left = fx; |
| 400 | output->base.border.top = fy; |
| 401 | output->base.border.right = fwidth - width - fx; |
| 402 | output->base.border.bottom = fheight - height - fy; |
| 403 | |
| 404 | frame_input_rect(output->frame, &fx, &fy, &fwidth, &fheight); |
| 405 | region = wl_compositor_create_region(c->parent.compositor); |
| 406 | wl_region_add(region, fx, fy, fwidth, fheight); |
| 407 | wl_surface_set_input_region(output->parent.surface, region); |
| 408 | wl_region_destroy(region); |
| 409 | |
| 410 | frame_opaque_rect(output->frame, &fx, &fy, &fwidth, &fheight); |
| 411 | region = wl_compositor_create_region(c->parent.compositor); |
| 412 | wl_region_add(region, fx, fy, fwidth, fheight); |
| 413 | wl_surface_set_opaque_region(output->parent.surface, region); |
| 414 | wl_region_destroy(region); |
| 415 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 416 | output->parent.draw_initial_frame = 1; |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 417 | output->parent.shell_surface = |
| 418 | wl_shell_get_shell_surface(c->parent.shell, |
| 419 | output->parent.surface); |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 420 | wl_shell_surface_add_listener(output->parent.shell_surface, |
| 421 | &shell_surface_listener, output); |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 422 | wl_shell_surface_set_toplevel(output->parent.shell_surface); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 423 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 424 | output->base.start_repaint_loop = wayland_output_start_repaint_loop; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 425 | output->base.repaint = wayland_output_repaint; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 426 | output->base.destroy = wayland_output_destroy; |
Jesse Barnes | 5308a5e | 2012-02-09 13:12:57 -0800 | [diff] [blame] | 427 | output->base.assign_planes = NULL; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 428 | output->base.set_backlight = NULL; |
| 429 | output->base.set_dpms = NULL; |
Alex Wu | 2dda604 | 2012-04-17 17:20:47 +0800 | [diff] [blame] | 430 | output->base.switch_mode = NULL; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 431 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 432 | wl_list_insert(c->base.output_list.prev, &output->base.link); |
| 433 | |
| 434 | return 0; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 435 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 436 | cleanup_window: |
| 437 | wl_egl_window_destroy(output->parent.egl_window); |
| 438 | cleanup_output: |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 439 | /* FIXME: cleanup weston_output */ |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 440 | free(output); |
| 441 | |
| 442 | return -1; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 443 | } |
| 444 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 445 | static void |
| 446 | shell_surface_ping(void *data, struct wl_shell_surface *shell_surface, |
| 447 | uint32_t serial) |
| 448 | { |
| 449 | wl_shell_surface_pong(shell_surface, serial); |
| 450 | } |
| 451 | |
| 452 | static void |
| 453 | shell_surface_configure(void *data, struct wl_shell_surface *shell_surface, |
| 454 | uint32_t edges, int32_t width, int32_t height) |
| 455 | { |
| 456 | /* FIXME: implement resizing */ |
| 457 | } |
| 458 | |
| 459 | static void |
| 460 | shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface) |
| 461 | { |
| 462 | } |
| 463 | |
| 464 | static const struct wl_shell_surface_listener shell_surface_listener = { |
| 465 | shell_surface_ping, |
| 466 | shell_surface_configure, |
| 467 | shell_surface_popup_done |
| 468 | }; |
| 469 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 470 | /* Events received from the wayland-server this compositor is client of: */ |
| 471 | |
| 472 | /* parent output interface */ |
| 473 | static void |
| 474 | display_handle_geometry(void *data, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 475 | struct wl_output *wl_output, |
| 476 | int x, |
| 477 | int y, |
| 478 | int physical_width, |
| 479 | int physical_height, |
| 480 | int subpixel, |
| 481 | const char *make, |
Kristian Høgsberg | 0e69647 | 2012-07-22 15:49:57 -0400 | [diff] [blame] | 482 | const char *model, |
| 483 | int transform) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 484 | { |
| 485 | struct wayland_compositor *c = data; |
| 486 | |
Kristian Høgsberg | f8fc08f | 2010-12-01 20:10:10 -0500 | [diff] [blame] | 487 | c->parent.screen_allocation.x = x; |
| 488 | c->parent.screen_allocation.y = y; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | static void |
| 492 | display_handle_mode(void *data, |
| 493 | struct wl_output *wl_output, |
| 494 | uint32_t flags, |
| 495 | int width, |
| 496 | int height, |
| 497 | int refresh) |
| 498 | { |
| 499 | struct wayland_compositor *c = data; |
| 500 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 501 | c->parent.screen_allocation.width = width; |
| 502 | c->parent.screen_allocation.height = height; |
| 503 | } |
| 504 | |
| 505 | static const struct wl_output_listener output_listener = { |
| 506 | display_handle_geometry, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 507 | display_handle_mode |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 508 | }; |
| 509 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 510 | /* parent input interface */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 511 | static void |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 512 | input_set_cursor(struct wayland_input *input) |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 513 | { |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 514 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 515 | struct wl_buffer *buffer; |
| 516 | struct wl_cursor_image *image; |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 517 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 518 | if (!input->compositor->cursor) |
| 519 | return; /* Couldn't load the cursor. Can't set it */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 520 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 521 | image = input->compositor->cursor->images[0]; |
| 522 | buffer = wl_cursor_image_get_buffer(image); |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 523 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 524 | |
| 525 | wl_pointer_set_cursor(input->parent.pointer, input->enter_serial, |
| 526 | input->parent.cursor.surface, |
| 527 | image->hotspot_x, image->hotspot_y); |
| 528 | |
| 529 | wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0); |
| 530 | wl_surface_damage(input->parent.cursor.surface, 0, 0, |
| 531 | image->width, image->height); |
| 532 | wl_surface_commit(input->parent.cursor.surface); |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 533 | } |
| 534 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 535 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 536 | input_handle_pointer_enter(void *data, struct wl_pointer *pointer, |
| 537 | uint32_t serial, struct wl_surface *surface, |
Kristian Høgsberg | e11bbe4 | 2012-05-09 12:19:04 -0400 | [diff] [blame] | 538 | wl_fixed_t x, wl_fixed_t y) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 539 | { |
| 540 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 541 | int32_t fx, fy; |
| 542 | enum theme_location location; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 543 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 544 | /* XXX: If we get a modifier event immediately before the focus, |
| 545 | * we should try to keep the same serial. */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 546 | input->enter_serial = serial; |
| 547 | input->output = wl_surface_get_user_data(surface); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 548 | |
| 549 | if (input->output->frame) { |
| 550 | location = frame_pointer_enter(input->output->frame, input, |
| 551 | wl_fixed_to_int(x), |
| 552 | wl_fixed_to_int(y)); |
| 553 | frame_interior(input->output->frame, &fx, &fy, NULL, NULL); |
| 554 | x -= wl_fixed_from_int(fx); |
| 555 | y -= wl_fixed_from_int(fy); |
| 556 | |
| 557 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 558 | weston_output_schedule_repaint(&input->output->base); |
| 559 | } else { |
| 560 | location = THEME_LOCATION_CLIENT_AREA; |
| 561 | } |
| 562 | |
| 563 | x += wl_fixed_from_int(input->output->base.x); |
| 564 | y += wl_fixed_from_int(input->output->base.y); |
| 565 | |
| 566 | if (location == THEME_LOCATION_CLIENT_AREA) { |
| 567 | input->focus = 1; |
| 568 | notify_pointer_focus(&input->base, &input->output->base, x, y); |
| 569 | wl_pointer_set_cursor(input->parent.pointer, |
| 570 | input->enter_serial, NULL, 0, 0); |
| 571 | } else { |
| 572 | input->focus = 0; |
| 573 | notify_pointer_focus(&input->base, NULL, 0, 0); |
| 574 | input_set_cursor(input); |
| 575 | } |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 579 | input_handle_pointer_leave(void *data, struct wl_pointer *pointer, |
| 580 | uint32_t serial, struct wl_surface *surface) |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 581 | { |
| 582 | struct wayland_input *input = data; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 583 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 584 | if (input->output->frame) { |
| 585 | frame_pointer_leave(input->output->frame, input); |
| 586 | |
| 587 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 588 | weston_output_schedule_repaint(&input->output->base); |
| 589 | } |
| 590 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 591 | notify_pointer_focus(&input->base, NULL, 0, 0); |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 592 | input->output = NULL; |
| 593 | input->focus = 0; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 597 | input_handle_motion(void *data, struct wl_pointer *pointer, |
| 598 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 599 | { |
| 600 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 601 | int32_t fx, fy; |
| 602 | enum theme_location location; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 603 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 604 | if (input->output->frame) { |
| 605 | location = frame_pointer_motion(input->output->frame, input, |
| 606 | wl_fixed_to_int(x), |
| 607 | wl_fixed_to_int(y)); |
| 608 | frame_interior(input->output->frame, &fx, &fy, NULL, NULL); |
| 609 | x -= wl_fixed_from_int(fx); |
| 610 | y -= wl_fixed_from_int(fy); |
| 611 | |
| 612 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 613 | weston_output_schedule_repaint(&input->output->base); |
| 614 | } else { |
| 615 | location = THEME_LOCATION_CLIENT_AREA; |
| 616 | } |
| 617 | |
| 618 | x += wl_fixed_from_int(input->output->base.x); |
| 619 | y += wl_fixed_from_int(input->output->base.y); |
| 620 | |
| 621 | if (input->focus && location != THEME_LOCATION_CLIENT_AREA) { |
| 622 | input_set_cursor(input); |
| 623 | notify_pointer_focus(&input->base, NULL, 0, 0); |
| 624 | input->focus = 0; |
| 625 | } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) { |
| 626 | wl_pointer_set_cursor(input->parent.pointer, |
| 627 | input->enter_serial, NULL, 0, 0); |
| 628 | notify_pointer_focus(&input->base, &input->output->base, x, y); |
| 629 | input->focus = 1; |
| 630 | } |
| 631 | |
| 632 | if (location == THEME_LOCATION_CLIENT_AREA) |
| 633 | notify_motion_absolute(&input->base, time, x, y); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | static void |
| 637 | input_handle_button(void *data, struct wl_pointer *pointer, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 638 | uint32_t serial, uint32_t time, uint32_t button, |
| 639 | uint32_t state_w) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 640 | { |
| 641 | struct wayland_input *input = data; |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 642 | enum wl_pointer_button_state state = state_w; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 643 | enum frame_button_state fstate; |
| 644 | enum theme_location location; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 645 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 646 | if (input->output->frame) { |
| 647 | fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ? |
| 648 | FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED; |
| 649 | |
| 650 | location = frame_pointer_button(input->output->frame, input, |
| 651 | button, fstate); |
| 652 | |
| 653 | if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) { |
| 654 | |
| 655 | wl_shell_surface_move(input->output->parent.shell_surface, |
| 656 | input->parent.seat, serial); |
| 657 | frame_status_clear(input->output->frame, |
| 658 | FRAME_STATUS_MOVE); |
| 659 | return; |
| 660 | } |
| 661 | |
| 662 | if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) |
| 663 | wl_display_terminate(input->compositor->base.wl_display); |
| 664 | |
| 665 | if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT) |
| 666 | weston_output_schedule_repaint(&input->output->base); |
| 667 | } else { |
| 668 | location = THEME_LOCATION_CLIENT_AREA; |
| 669 | } |
| 670 | |
| 671 | if (location == THEME_LOCATION_CLIENT_AREA) |
| 672 | notify_button(&input->base, time, button, state); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | static void |
| 676 | input_handle_axis(void *data, struct wl_pointer *pointer, |
Daniel Stone | 2fce402 | 2012-05-30 16:32:00 +0100 | [diff] [blame] | 677 | uint32_t time, uint32_t axis, wl_fixed_t value) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 678 | { |
| 679 | struct wayland_input *input = data; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 680 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 681 | notify_axis(&input->base, time, axis, value); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | static const struct wl_pointer_listener pointer_listener = { |
| 685 | input_handle_pointer_enter, |
| 686 | input_handle_pointer_leave, |
| 687 | input_handle_motion, |
| 688 | input_handle_button, |
| 689 | input_handle_axis, |
| 690 | }; |
| 691 | |
| 692 | static void |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 693 | input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, |
| 694 | int fd, uint32_t size) |
| 695 | { |
| 696 | struct wayland_input *input = data; |
| 697 | struct xkb_keymap *keymap; |
| 698 | char *map_str; |
| 699 | |
| 700 | if (!data) { |
| 701 | close(fd); |
| 702 | return; |
| 703 | } |
| 704 | |
| 705 | if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { |
| 706 | close(fd); |
| 707 | return; |
| 708 | } |
| 709 | |
| 710 | map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); |
| 711 | if (map_str == MAP_FAILED) { |
| 712 | close(fd); |
| 713 | return; |
| 714 | } |
| 715 | |
| 716 | keymap = xkb_map_new_from_string(input->compositor->base.xkb_context, |
| 717 | map_str, |
| 718 | XKB_KEYMAP_FORMAT_TEXT_V1, |
| 719 | 0); |
| 720 | munmap(map_str, size); |
| 721 | close(fd); |
| 722 | |
| 723 | if (!keymap) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 724 | weston_log("failed to compile keymap\n"); |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 725 | return; |
| 726 | } |
| 727 | |
Rui Matos | 0c194ce | 2013-10-10 19:44:21 +0200 | [diff] [blame] | 728 | if (input->base.keyboard) |
| 729 | weston_seat_update_keymap(&input->base, keymap); |
| 730 | else |
| 731 | weston_seat_init_keyboard(&input->base, keymap); |
| 732 | |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 733 | xkb_map_unref(keymap); |
| 734 | } |
| 735 | |
| 736 | static void |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 737 | input_handle_keyboard_enter(void *data, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 738 | struct wl_keyboard *keyboard, |
| 739 | uint32_t serial, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 740 | struct wl_surface *surface, |
| 741 | struct wl_array *keys) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 742 | { |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 743 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 744 | struct wayland_output *focus; |
| 745 | |
| 746 | focus = input->keyboard_focus; |
| 747 | if (focus) { |
| 748 | /* This shouldn't happen */ |
| 749 | focus->keyboard_count--; |
| 750 | if (!focus->keyboard_count && focus->frame) |
| 751 | frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE); |
| 752 | if (frame_status(focus->frame) & FRAME_STATUS_REPAINT) |
| 753 | weston_output_schedule_repaint(&focus->base); |
| 754 | } |
| 755 | |
| 756 | input->keyboard_focus = wl_surface_get_user_data(surface); |
| 757 | input->keyboard_focus->keyboard_count++; |
| 758 | |
| 759 | focus = input->keyboard_focus; |
| 760 | if (focus->frame) { |
| 761 | frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE); |
| 762 | if (frame_status(focus->frame) & FRAME_STATUS_REPAINT) |
| 763 | weston_output_schedule_repaint(&focus->base); |
| 764 | } |
| 765 | |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 766 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 767 | /* XXX: If we get a modifier event immediately before the focus, |
| 768 | * we should try to keep the same serial. */ |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 769 | notify_keyboard_focus_in(&input->base, keys, |
Daniel Stone | d6da09e | 2012-06-22 13:21:29 +0100 | [diff] [blame] | 770 | STATE_UPDATE_AUTOMATIC); |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | static void |
| 774 | input_handle_keyboard_leave(void *data, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 775 | struct wl_keyboard *keyboard, |
| 776 | uint32_t serial, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 777 | struct wl_surface *surface) |
| 778 | { |
| 779 | struct wayland_input *input = data; |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 780 | struct wayland_output *focus; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 781 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 782 | notify_keyboard_focus_out(&input->base); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 783 | |
| 784 | focus = input->keyboard_focus; |
| 785 | if (!focus) |
| 786 | return; /* This shouldn't happen */ |
| 787 | |
| 788 | focus->keyboard_count--; |
| 789 | if (!focus->keyboard_count && focus->frame) { |
| 790 | frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE); |
| 791 | if (frame_status(focus->frame) & FRAME_STATUS_REPAINT) |
| 792 | weston_output_schedule_repaint(&focus->base); |
| 793 | } |
| 794 | |
| 795 | input->keyboard_focus = NULL; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 796 | } |
| 797 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 798 | static void |
| 799 | input_handle_key(void *data, struct wl_keyboard *keyboard, |
| 800 | uint32_t serial, uint32_t time, uint32_t key, uint32_t state) |
| 801 | { |
| 802 | struct wayland_input *input = data; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 803 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 804 | input->key_serial = serial; |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 805 | notify_key(&input->base, time, key, |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 806 | state ? WL_KEYBOARD_KEY_STATE_PRESSED : |
Daniel Stone | 1b4e11f | 2012-06-22 13:21:37 +0100 | [diff] [blame] | 807 | WL_KEYBOARD_KEY_STATE_RELEASED, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 808 | STATE_UPDATE_NONE); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 809 | } |
| 810 | |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 811 | static void |
| 812 | input_handle_modifiers(void *data, struct wl_keyboard *keyboard, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 813 | uint32_t serial_in, uint32_t mods_depressed, |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 814 | uint32_t mods_latched, uint32_t mods_locked, |
| 815 | uint32_t group) |
| 816 | { |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 817 | struct wayland_input *input = data; |
| 818 | struct wayland_compositor *c = input->compositor; |
| 819 | uint32_t serial_out; |
| 820 | |
| 821 | /* If we get a key event followed by a modifier event with the |
| 822 | * same serial number, then we try to preserve those semantics by |
| 823 | * reusing the same serial number on the way out too. */ |
| 824 | if (serial_in == input->key_serial) |
| 825 | serial_out = wl_display_get_serial(c->base.wl_display); |
| 826 | else |
| 827 | serial_out = wl_display_next_serial(c->base.wl_display); |
| 828 | |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 829 | xkb_state_update_mask(input->base.xkb_state.state, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 830 | mods_depressed, mods_latched, |
| 831 | mods_locked, 0, 0, group); |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 832 | notify_modifiers(&input->base, serial_out); |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 833 | } |
| 834 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 835 | static const struct wl_keyboard_listener keyboard_listener = { |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 836 | input_handle_keymap, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 837 | input_handle_keyboard_enter, |
| 838 | input_handle_keyboard_leave, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 839 | input_handle_key, |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 840 | input_handle_modifiers, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 841 | }; |
| 842 | |
| 843 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 844 | input_handle_capabilities(void *data, struct wl_seat *seat, |
| 845 | enum wl_seat_capability caps) |
| 846 | { |
| 847 | struct wayland_input *input = data; |
| 848 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 849 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) { |
| 850 | input->parent.pointer = wl_seat_get_pointer(seat); |
| 851 | wl_pointer_set_user_data(input->parent.pointer, input); |
| 852 | wl_pointer_add_listener(input->parent.pointer, |
| 853 | &pointer_listener, input); |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 854 | weston_seat_init_pointer(&input->base); |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 855 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) { |
| 856 | wl_pointer_destroy(input->parent.pointer); |
| 857 | input->parent.pointer = NULL; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 858 | } |
| 859 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 860 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) { |
| 861 | input->parent.keyboard = wl_seat_get_keyboard(seat); |
| 862 | wl_keyboard_set_user_data(input->parent.keyboard, input); |
| 863 | wl_keyboard_add_listener(input->parent.keyboard, |
| 864 | &keyboard_listener, input); |
| 865 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) { |
| 866 | wl_keyboard_destroy(input->parent.keyboard); |
| 867 | input->parent.keyboard = NULL; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | |
| 871 | static const struct wl_seat_listener seat_listener = { |
| 872 | input_handle_capabilities, |
| 873 | }; |
| 874 | |
| 875 | static void |
| 876 | display_add_seat(struct wayland_compositor *c, uint32_t id) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 877 | { |
| 878 | struct wayland_input *input; |
| 879 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 880 | input = zalloc(sizeof *input); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 881 | if (input == NULL) |
| 882 | return; |
| 883 | |
Rob Bradford | 9af5f9e | 2013-05-31 18:09:50 +0100 | [diff] [blame] | 884 | weston_seat_init(&input->base, &c->base, "default"); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 885 | input->compositor = c; |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 886 | input->parent.seat = wl_registry_bind(c->parent.registry, id, |
| 887 | &wl_seat_interface, 1); |
| 888 | wl_list_insert(c->inputs.prev, &input->link); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 889 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 890 | wl_seat_add_listener(input->parent.seat, &seat_listener, input); |
| 891 | wl_seat_set_user_data(input->parent.seat, input); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 892 | |
| 893 | input->parent.cursor.surface = |
| 894 | wl_compositor_create_surface(c->parent.compositor); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 898 | registry_handle_global(void *data, struct wl_registry *registry, uint32_t name, |
| 899 | const char *interface, uint32_t version) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 900 | { |
| 901 | struct wayland_compositor *c = data; |
| 902 | |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 903 | if (strcmp(interface, "wl_compositor") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 904 | c->parent.compositor = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 905 | wl_registry_bind(registry, name, |
| 906 | &wl_compositor_interface, 1); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 907 | } else if (strcmp(interface, "wl_output") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 908 | c->parent.output = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 909 | wl_registry_bind(registry, name, |
| 910 | &wl_output_interface, 1); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 911 | wl_output_add_listener(c->parent.output, &output_listener, c); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 912 | } else if (strcmp(interface, "wl_shell") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 913 | c->parent.shell = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 914 | wl_registry_bind(registry, name, |
| 915 | &wl_shell_interface, 1); |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 916 | } else if (strcmp(interface, "wl_seat") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 917 | display_add_seat(c, name); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 918 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 919 | c->parent.shm = |
| 920 | wl_registry_bind(registry, name, &wl_shm_interface, 1); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 924 | static const struct wl_registry_listener registry_listener = { |
| 925 | registry_handle_global |
| 926 | }; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 927 | |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 928 | static int |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 929 | wayland_compositor_handle_event(int fd, uint32_t mask, void *data) |
| 930 | { |
| 931 | struct wayland_compositor *c = data; |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 932 | int count = 0; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 933 | |
Kristian Høgsberg | 453de7a | 2013-10-30 23:15:44 -0700 | [diff] [blame] | 934 | if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) { |
| 935 | wl_display_terminate(c->base.wl_display); |
| 936 | return 0; |
| 937 | } |
| 938 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 939 | if (mask & WL_EVENT_READABLE) |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 940 | count = wl_display_dispatch(c->parent.wl_display); |
Kristian Høgsberg | f258a31 | 2011-12-28 22:51:20 -0500 | [diff] [blame] | 941 | if (mask & WL_EVENT_WRITABLE) |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 942 | wl_display_flush(c->parent.wl_display); |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 943 | |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 944 | if (mask == 0) { |
| 945 | count = wl_display_dispatch_pending(c->parent.wl_display); |
| 946 | wl_display_flush(c->parent.wl_display); |
| 947 | } |
| 948 | |
| 949 | return count; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 950 | } |
| 951 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 952 | static void |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 953 | wayland_restore(struct weston_compositor *ec) |
| 954 | { |
| 955 | } |
| 956 | |
| 957 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 958 | wayland_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 959 | { |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 960 | struct wayland_compositor *c = (struct wayland_compositor *) ec; |
| 961 | |
Vasily Khoruzhick | 52cfd61 | 2013-01-08 19:09:01 +0300 | [diff] [blame] | 962 | ec->renderer->destroy(ec); |
Kristian Høgsberg | 3a0de88 | 2012-09-06 21:44:24 -0400 | [diff] [blame] | 963 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 964 | weston_compositor_shutdown(ec); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 965 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 966 | if (c->parent.shm) |
| 967 | wl_shm_destroy(c->parent.shm); |
| 968 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 969 | free(ec); |
| 970 | } |
| 971 | |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 972 | static const char *left_ptrs[] = { |
| 973 | "left_ptr", |
| 974 | "default", |
| 975 | "top_left_arrow", |
| 976 | "left-arrow" |
| 977 | }; |
| 978 | |
| 979 | static void |
| 980 | create_cursor(struct wayland_compositor *c, struct weston_config *config) |
| 981 | { |
| 982 | struct weston_config_section *s; |
| 983 | int size; |
| 984 | char *theme = NULL; |
| 985 | unsigned int i; |
| 986 | |
| 987 | s = weston_config_get_section(config, "shell", NULL, NULL); |
| 988 | weston_config_section_get_string(s, "cursor-theme", &theme, NULL); |
| 989 | weston_config_section_get_int(s, "cursor-size", &size, 32); |
| 990 | |
| 991 | c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm); |
| 992 | |
| 993 | c->cursor = NULL; |
| 994 | for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i) |
| 995 | c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme, |
| 996 | left_ptrs[i]); |
| 997 | if (!c->cursor) { |
| 998 | fprintf(stderr, "could not load left cursor\n"); |
| 999 | return; |
| 1000 | } |
| 1001 | } |
| 1002 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1003 | static struct weston_compositor * |
Kristian Høgsberg | 2e28b10 | 2012-02-07 09:57:25 -0500 | [diff] [blame] | 1004 | wayland_compositor_create(struct wl_display *display, |
Daniel Stone | baf4359 | 2012-06-01 11:11:10 -0400 | [diff] [blame] | 1005 | int width, int height, const char *display_name, |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 1006 | int *argc, char *argv[], |
| 1007 | struct weston_config *config) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1008 | { |
| 1009 | struct wayland_compositor *c; |
| 1010 | struct wl_event_loop *loop; |
| 1011 | int fd; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1012 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 1013 | c = zalloc(sizeof *c); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1014 | if (c == NULL) |
| 1015 | return NULL; |
| 1016 | |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1017 | if (weston_compositor_init(&c->base, display, argc, argv, |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 1018 | config) < 0) |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 1019 | goto err_free; |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1020 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1021 | c->parent.wl_display = wl_display_connect(display_name); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1022 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1023 | if (c->parent.wl_display == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1024 | weston_log("failed to create display: %m\n"); |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 1025 | goto err_compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1026 | } |
| 1027 | |
Jason Ekstrand | 8f89fcb | 2013-10-27 22:24:53 -0500 | [diff] [blame] | 1028 | wl_list_init(&c->inputs); |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1029 | c->parent.registry = wl_display_get_registry(c->parent.wl_display); |
| 1030 | wl_registry_add_listener(c->parent.registry, ®istry_listener, c); |
Jason Ekstrand | 7744f71 | 2013-10-27 22:24:55 -0500 | [diff] [blame^] | 1031 | wl_display_roundtrip(c->parent.wl_display); |
| 1032 | |
| 1033 | create_cursor(c, config); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1034 | |
| 1035 | c->base.wl_display = display; |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 1036 | |
| 1037 | gl_renderer = weston_load_module("gl-renderer.so", |
| 1038 | "gl_renderer_interface"); |
| 1039 | if (!gl_renderer) |
| 1040 | goto err_display; |
| 1041 | |
| 1042 | if (gl_renderer->create(&c->base, c->parent.wl_display, |
| 1043 | gl_renderer->alpha_attribs, |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 1044 | NULL) < 0) |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 1045 | goto err_display; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1046 | |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 1047 | c->base.destroy = wayland_destroy; |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 1048 | c->base.restore = wayland_restore; |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 1049 | |
John Kåre Alsaker | b9b8712 | 2012-10-04 20:42:16 +0200 | [diff] [blame] | 1050 | /* requires border fields */ |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1051 | if (wayland_compositor_create_output(c, width, height) < 0) |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 1052 | goto err_gl; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1053 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1054 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 1055 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1056 | fd = wl_display_get_fd(c->parent.wl_display); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1057 | c->parent.wl_source = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 1058 | wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1059 | wayland_compositor_handle_event, c); |
| 1060 | if (c->parent.wl_source == NULL) |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 1061 | goto err_gl; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1062 | |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 1063 | wl_event_source_check(c->parent.wl_source); |
| 1064 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1065 | return &c->base; |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 1066 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 1067 | err_gl: |
Vasily Khoruzhick | 52cfd61 | 2013-01-08 19:09:01 +0300 | [diff] [blame] | 1068 | c->base.renderer->destroy(&c->base); |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 1069 | err_display: |
| 1070 | wl_display_disconnect(c->parent.wl_display); |
| 1071 | err_compositor: |
| 1072 | weston_compositor_shutdown(&c->base); |
| 1073 | err_free: |
| 1074 | free(c); |
| 1075 | return NULL; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 1076 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1077 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1078 | WL_EXPORT struct weston_compositor * |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 1079 | backend_init(struct wl_display *display, int *argc, char *argv[], |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 1080 | struct weston_config *config) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1081 | { |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1082 | int width = 1024, height = 640; |
| 1083 | char *display_name = NULL; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1084 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1085 | const struct weston_option wayland_options[] = { |
| 1086 | { WESTON_OPTION_INTEGER, "width", 0, &width }, |
| 1087 | { WESTON_OPTION_INTEGER, "height", 0, &height }, |
| 1088 | { WESTON_OPTION_STRING, "display", 0, &display_name }, |
| 1089 | }; |
| 1090 | |
| 1091 | parse_options(wayland_options, |
| 1092 | ARRAY_LENGTH(wayland_options), argc, argv); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1093 | |
Daniel Stone | baf4359 | 2012-06-01 11:11:10 -0400 | [diff] [blame] | 1094 | return wayland_compositor_create(display, width, height, display_name, |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 1095 | argc, argv, config); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1096 | } |