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> |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 35 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 36 | #include "compositor.h" |
John Kåre Alsaker | 30d2b1f | 2012-11-13 19:10:28 +0100 | [diff] [blame] | 37 | #include "gl-renderer.h" |
Kristian Høgsberg | 3c2360f | 2013-01-28 16:01:22 -0500 | [diff] [blame] | 38 | #include "../shared/image-loader.h" |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 39 | #include "../shared/os-compatibility.h" |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 40 | |
| 41 | struct wayland_compositor { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 42 | struct weston_compositor base; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 43 | |
| 44 | struct { |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 45 | struct wl_display *wl_display; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 46 | struct wl_registry *registry; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 47 | struct wl_compositor *compositor; |
| 48 | struct wl_shell *shell; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 49 | struct wl_output *output; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 50 | struct wl_shm *shm; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 51 | |
| 52 | struct { |
| 53 | int32_t x, y, width, height; |
| 54 | } screen_allocation; |
| 55 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 56 | struct wl_event_source *wl_source; |
| 57 | uint32_t event_mask; |
| 58 | } parent; |
| 59 | |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 60 | struct { |
| 61 | int32_t top, bottom, left, right; |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 62 | } border; |
| 63 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 64 | struct wl_list input_list; |
| 65 | }; |
| 66 | |
| 67 | struct wayland_output { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 68 | struct weston_output base; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 69 | struct { |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 70 | int draw_initial_frame; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 71 | struct wl_surface *surface; |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 72 | struct wl_shell_surface *shell_surface; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 73 | struct wl_egl_window *egl_window; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 74 | } parent; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 75 | struct weston_mode mode; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | struct wayland_input { |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 79 | struct weston_seat base; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 80 | struct wayland_compositor *compositor; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 81 | struct wl_seat *seat; |
| 82 | struct wl_pointer *pointer; |
| 83 | struct wl_keyboard *keyboard; |
| 84 | struct wl_touch *touch; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 85 | struct wl_list link; |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 86 | uint32_t key_serial; |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 87 | uint32_t enter_serial; |
| 88 | int focus; |
| 89 | struct wayland_output *output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 90 | }; |
| 91 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 92 | struct gl_renderer_interface *gl_renderer; |
| 93 | |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 94 | static void |
| 95 | create_border(struct wayland_compositor *c) |
| 96 | { |
Ander Conselvan de Oliveira | e2d21e8 | 2012-03-16 17:25:09 +0200 | [diff] [blame] | 97 | pixman_image_t *image; |
John Kåre Alsaker | 4415450 | 2012-11-13 19:10:20 +0100 | [diff] [blame] | 98 | int32_t edges[4]; |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 99 | |
Ander Conselvan de Oliveira | e2d21e8 | 2012-03-16 17:25:09 +0200 | [diff] [blame] | 100 | image = load_image(DATADIR "/weston/border.png"); |
| 101 | if (!image) { |
Aaron Faanes | 9cefc64 | 2013-09-30 22:06:39 -0500 | [diff] [blame] | 102 | weston_log("couldn't load border image\n"); |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | |
John Kåre Alsaker | 4415450 | 2012-11-13 19:10:20 +0100 | [diff] [blame] | 106 | edges[0] = c->border.left; |
| 107 | edges[1] = c->border.right; |
| 108 | edges[2] = c->border.top; |
| 109 | edges[3] = c->border.bottom; |
Ander Conselvan de Oliveira | e2d21e8 | 2012-03-16 17:25:09 +0200 | [diff] [blame] | 110 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 111 | gl_renderer->set_border(&c->base, pixman_image_get_width(image), |
John Kåre Alsaker | 4415450 | 2012-11-13 19:10:20 +0100 | [diff] [blame] | 112 | pixman_image_get_height(image), |
| 113 | pixman_image_get_data(image), edges); |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 114 | |
Ander Conselvan de Oliveira | e2d21e8 | 2012-03-16 17:25:09 +0200 | [diff] [blame] | 115 | pixman_image_unref(image); |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 118 | static void |
Kristian Høgsberg | cdd61d0 | 2012-02-07 09:56:15 -0500 | [diff] [blame] | 119 | frame_done(void *data, struct wl_callback *callback, uint32_t time) |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 120 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 121 | struct weston_output *output = data; |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 122 | |
Kristian Høgsberg | cdd61d0 | 2012-02-07 09:56:15 -0500 | [diff] [blame] | 123 | wl_callback_destroy(callback); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 124 | weston_output_finish_frame(output, time); |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static const struct wl_callback_listener frame_listener = { |
| 128 | frame_done |
| 129 | }; |
| 130 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 131 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 132 | buffer_release(void *data, struct wl_buffer *buffer) |
| 133 | { |
| 134 | wl_buffer_destroy(buffer); |
| 135 | } |
| 136 | |
| 137 | static const struct wl_buffer_listener buffer_listener = { |
| 138 | buffer_release |
| 139 | }; |
| 140 | |
| 141 | static void |
| 142 | draw_initial_frame(struct wayland_output *output) |
| 143 | { |
| 144 | struct wayland_compositor *c = |
| 145 | (struct wayland_compositor *) output->base.compositor; |
| 146 | struct wl_shm *shm = c->parent.shm; |
| 147 | struct wl_surface *surface = output->parent.surface; |
| 148 | struct wl_shm_pool *pool; |
| 149 | struct wl_buffer *buffer; |
| 150 | |
| 151 | int width, height, stride; |
| 152 | int size; |
| 153 | int fd; |
| 154 | void *data; |
| 155 | |
| 156 | width = output->mode.width + c->border.left + c->border.right; |
| 157 | height = output->mode.height + c->border.top + c->border.bottom; |
| 158 | stride = width * 4; |
| 159 | size = height * stride; |
| 160 | |
| 161 | fd = os_create_anonymous_file(size); |
| 162 | if (fd < 0) { |
| 163 | perror("os_create_anonymous_file"); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 168 | if (data == MAP_FAILED) { |
| 169 | perror("mmap"); |
| 170 | close(fd); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | pool = wl_shm_create_pool(shm, fd, size); |
| 175 | |
| 176 | buffer = wl_shm_pool_create_buffer(pool, 0, |
| 177 | width, height, |
| 178 | stride, |
| 179 | WL_SHM_FORMAT_ARGB8888); |
| 180 | wl_buffer_add_listener(buffer, &buffer_listener, buffer); |
| 181 | wl_shm_pool_destroy(pool); |
| 182 | close(fd); |
| 183 | |
| 184 | memset(data, 0, size); |
| 185 | |
| 186 | wl_surface_attach(surface, buffer, 0, 0); |
| 187 | |
| 188 | /* We only need to damage some part, as its only transparant |
| 189 | * pixels anyway. */ |
| 190 | wl_surface_damage(surface, 0, 0, 1, 1); |
| 191 | } |
| 192 | |
| 193 | static void |
| 194 | wayland_output_start_repaint_loop(struct weston_output *output_base) |
| 195 | { |
| 196 | struct wayland_output *output = (struct wayland_output *) output_base; |
Jason Ekstrand | 286ff68 | 2013-10-27 22:24:57 -0500 | [diff] [blame^] | 197 | struct wayland_compositor *wc = |
| 198 | (struct wayland_compositor *)output->base.compositor; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 199 | struct wl_callback *callback; |
| 200 | |
| 201 | /* If this is the initial frame, we need to attach a buffer so that |
| 202 | * the compositor can map the surface and include it in its render |
| 203 | * loop. If the surface doesn't end up in the render loop, the frame |
| 204 | * callback won't be invoked. The buffer is transparent and of the |
| 205 | * same size as the future real output buffer. */ |
| 206 | if (output->parent.draw_initial_frame) { |
| 207 | output->parent.draw_initial_frame = 0; |
| 208 | |
| 209 | draw_initial_frame(output); |
| 210 | } |
| 211 | |
| 212 | callback = wl_surface_frame(output->parent.surface); |
| 213 | wl_callback_add_listener(callback, &frame_listener, output); |
| 214 | wl_surface_commit(output->parent.surface); |
Jason Ekstrand | 286ff68 | 2013-10-27 22:24:57 -0500 | [diff] [blame^] | 215 | wl_display_flush(wc->parent.wl_display); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 216 | } |
| 217 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 218 | static int |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 219 | wayland_output_repaint(struct weston_output *output_base, |
| 220 | pixman_region32_t *damage) |
| 221 | { |
| 222 | struct wayland_output *output = (struct wayland_output *) output_base; |
Kristian Høgsberg | fa1be02 | 2012-09-05 22:49:55 -0400 | [diff] [blame] | 223 | struct weston_compositor *ec = output->base.compositor; |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 224 | struct wl_callback *callback; |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 225 | |
Kristian Høgsberg | 3341820 | 2011-08-16 23:01:28 -0400 | [diff] [blame] | 226 | callback = wl_surface_frame(output->parent.surface); |
| 227 | wl_callback_add_listener(callback, &frame_listener, output); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 228 | |
Pekka Paalanen | bc10638 | 2012-10-10 12:49:31 +0300 | [diff] [blame] | 229 | ec->renderer->repaint_output(&output->base, damage); |
Ander Conselvan de Oliveira | 0a88772 | 2012-11-22 15:57:00 +0200 | [diff] [blame] | 230 | |
| 231 | pixman_region32_subtract(&ec->primary_plane.damage, |
| 232 | &ec->primary_plane.damage, damage); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 233 | return 0; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 236 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 237 | wayland_output_destroy(struct weston_output *output_base) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 238 | { |
| 239 | struct wayland_output *output = (struct wayland_output *) output_base; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 240 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 241 | gl_renderer->output_destroy(output_base); |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 242 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 243 | wl_egl_window_destroy(output->parent.egl_window); |
| 244 | free(output); |
| 245 | |
| 246 | return; |
| 247 | } |
| 248 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 249 | static const struct wl_shell_surface_listener shell_surface_listener; |
| 250 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 251 | static int |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 252 | wayland_compositor_create_output(struct wayland_compositor *c, |
| 253 | int width, int height) |
| 254 | { |
| 255 | struct wayland_output *output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 256 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 257 | output = zalloc(sizeof *output); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 258 | if (output == NULL) |
| 259 | return -1; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 260 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 261 | output->mode.flags = |
| 262 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 263 | output->mode.width = width; |
| 264 | output->mode.height = height; |
| 265 | output->mode.refresh = 60; |
| 266 | wl_list_init(&output->base.mode_list); |
| 267 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
| 268 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 269 | output->base.current_mode = &output->mode; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 270 | weston_output_init(&output->base, &c->base, 0, 0, width, height, |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 271 | WL_OUTPUT_TRANSFORM_NORMAL, 1); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 272 | |
Kristian Høgsberg | 90bc88c | 2012-08-13 23:34:04 -0400 | [diff] [blame] | 273 | output->base.make = "waywayland"; |
| 274 | output->base.model = "none"; |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 275 | |
| 276 | weston_output_move(&output->base, 0, 0); |
| 277 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 278 | output->parent.surface = |
| 279 | wl_compositor_create_surface(c->parent.compositor); |
| 280 | wl_surface_set_user_data(output->parent.surface, output); |
| 281 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 282 | output->parent.egl_window = |
Kristian Høgsberg | 546a812 | 2012-02-01 07:45:51 -0500 | [diff] [blame] | 283 | wl_egl_window_create(output->parent.surface, |
| 284 | width + c->border.left + c->border.right, |
| 285 | height + c->border.top + c->border.bottom); |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 286 | if (!output->parent.egl_window) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 287 | weston_log("failure to create wl_egl_window\n"); |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 288 | goto cleanup_output; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 289 | } |
| 290 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 291 | if (gl_renderer->output_create(&output->base, |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 292 | output->parent.egl_window) < 0) |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 293 | goto cleanup_window; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 294 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 295 | output->parent.draw_initial_frame = 1; |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 296 | output->parent.shell_surface = |
| 297 | wl_shell_get_shell_surface(c->parent.shell, |
| 298 | output->parent.surface); |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 299 | wl_shell_surface_add_listener(output->parent.shell_surface, |
| 300 | &shell_surface_listener, output); |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 301 | wl_shell_surface_set_toplevel(output->parent.shell_surface); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 302 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 303 | output->base.start_repaint_loop = wayland_output_start_repaint_loop; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 304 | output->base.repaint = wayland_output_repaint; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 305 | output->base.destroy = wayland_output_destroy; |
Jesse Barnes | 5308a5e | 2012-02-09 13:12:57 -0800 | [diff] [blame] | 306 | output->base.assign_planes = NULL; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 307 | output->base.set_backlight = NULL; |
| 308 | output->base.set_dpms = NULL; |
Alex Wu | 2dda604 | 2012-04-17 17:20:47 +0800 | [diff] [blame] | 309 | output->base.switch_mode = NULL; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 310 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 311 | wl_list_insert(c->base.output_list.prev, &output->base.link); |
| 312 | |
| 313 | return 0; |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 314 | |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 315 | cleanup_window: |
| 316 | wl_egl_window_destroy(output->parent.egl_window); |
| 317 | cleanup_output: |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 318 | /* FIXME: cleanup weston_output */ |
Benjamin Franzke | be01456 | 2011-02-18 17:04:24 +0100 | [diff] [blame] | 319 | free(output); |
| 320 | |
| 321 | return -1; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 322 | } |
| 323 | |
Ander Conselvan de Oliveira | 563c5b8 | 2012-06-18 17:36:21 +0300 | [diff] [blame] | 324 | static void |
| 325 | shell_surface_ping(void *data, struct wl_shell_surface *shell_surface, |
| 326 | uint32_t serial) |
| 327 | { |
| 328 | wl_shell_surface_pong(shell_surface, serial); |
| 329 | } |
| 330 | |
| 331 | static void |
| 332 | shell_surface_configure(void *data, struct wl_shell_surface *shell_surface, |
| 333 | uint32_t edges, int32_t width, int32_t height) |
| 334 | { |
| 335 | /* FIXME: implement resizing */ |
| 336 | } |
| 337 | |
| 338 | static void |
| 339 | shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface) |
| 340 | { |
| 341 | } |
| 342 | |
| 343 | static const struct wl_shell_surface_listener shell_surface_listener = { |
| 344 | shell_surface_ping, |
| 345 | shell_surface_configure, |
| 346 | shell_surface_popup_done |
| 347 | }; |
| 348 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 349 | /* Events received from the wayland-server this compositor is client of: */ |
| 350 | |
| 351 | /* parent output interface */ |
| 352 | static void |
| 353 | display_handle_geometry(void *data, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 354 | struct wl_output *wl_output, |
| 355 | int x, |
| 356 | int y, |
| 357 | int physical_width, |
| 358 | int physical_height, |
| 359 | int subpixel, |
| 360 | const char *make, |
Kristian Høgsberg | 0e69647 | 2012-07-22 15:49:57 -0400 | [diff] [blame] | 361 | const char *model, |
| 362 | int transform) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 363 | { |
| 364 | struct wayland_compositor *c = data; |
| 365 | |
Kristian Høgsberg | f8fc08f | 2010-12-01 20:10:10 -0500 | [diff] [blame] | 366 | c->parent.screen_allocation.x = x; |
| 367 | c->parent.screen_allocation.y = y; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static void |
| 371 | display_handle_mode(void *data, |
| 372 | struct wl_output *wl_output, |
| 373 | uint32_t flags, |
| 374 | int width, |
| 375 | int height, |
| 376 | int refresh) |
| 377 | { |
| 378 | struct wayland_compositor *c = data; |
| 379 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 380 | c->parent.screen_allocation.width = width; |
| 381 | c->parent.screen_allocation.height = height; |
| 382 | } |
| 383 | |
| 384 | static const struct wl_output_listener output_listener = { |
| 385 | display_handle_geometry, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 386 | display_handle_mode |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 387 | }; |
| 388 | |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 389 | static void |
| 390 | check_focus(struct wayland_input *input, wl_fixed_t x, wl_fixed_t y) |
| 391 | { |
| 392 | struct wayland_compositor *c = input->compositor; |
| 393 | int width, height, inside; |
| 394 | |
| 395 | width = input->output->mode.width; |
| 396 | height = input->output->mode.height; |
| 397 | |
| 398 | inside = c->border.left <= wl_fixed_to_int(x) && |
| 399 | wl_fixed_to_int(x) < width + c->border.left && |
| 400 | c->border.top <= wl_fixed_to_int(y) && |
| 401 | wl_fixed_to_int(y) < height + c->border.top; |
| 402 | |
| 403 | if (!input->focus && inside) { |
| 404 | notify_pointer_focus(&input->base, &input->output->base, |
| 405 | x - wl_fixed_from_int(c->border.left), |
| 406 | y = wl_fixed_from_int(c->border.top)); |
| 407 | wl_pointer_set_cursor(input->pointer, |
| 408 | input->enter_serial, NULL, 0, 0); |
| 409 | } else if (input->focus && !inside) { |
| 410 | notify_pointer_focus(&input->base, NULL, 0, 0); |
| 411 | /* FIXME: Should set default cursor here. */ |
| 412 | } |
| 413 | |
| 414 | input->focus = inside; |
| 415 | } |
| 416 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 417 | /* parent input interface */ |
| 418 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 419 | input_handle_pointer_enter(void *data, struct wl_pointer *pointer, |
| 420 | uint32_t serial, struct wl_surface *surface, |
Kristian Høgsberg | e11bbe4 | 2012-05-09 12:19:04 -0400 | [diff] [blame] | 421 | wl_fixed_t x, wl_fixed_t y) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 422 | { |
| 423 | struct wayland_input *input = data; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 424 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 425 | /* XXX: If we get a modifier event immediately before the focus, |
| 426 | * we should try to keep the same serial. */ |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 427 | input->enter_serial = serial; |
| 428 | input->output = wl_surface_get_user_data(surface); |
| 429 | check_focus(input, x, y); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 433 | input_handle_pointer_leave(void *data, struct wl_pointer *pointer, |
| 434 | uint32_t serial, struct wl_surface *surface) |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 435 | { |
| 436 | struct wayland_input *input = data; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 437 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 438 | notify_pointer_focus(&input->base, NULL, 0, 0); |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 439 | input->output = NULL; |
| 440 | input->focus = 0; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 444 | input_handle_motion(void *data, struct wl_pointer *pointer, |
| 445 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 446 | { |
| 447 | struct wayland_input *input = data; |
| 448 | struct wayland_compositor *c = input->compositor; |
| 449 | |
Kristian Høgsberg | 539d85f | 2012-08-13 23:29:53 -0400 | [diff] [blame] | 450 | check_focus(input, x, y); |
| 451 | if (input->focus) |
| 452 | notify_motion(&input->base, time, |
Kristian Høgsberg | 068b61c | 2013-02-25 17:04:47 -0500 | [diff] [blame] | 453 | x - wl_fixed_from_int(c->border.left) - |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 454 | input->base.pointer->x, |
Kristian Høgsberg | 068b61c | 2013-02-25 17:04:47 -0500 | [diff] [blame] | 455 | y - wl_fixed_from_int(c->border.top) - |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 456 | input->base.pointer->y); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | static void |
| 460 | input_handle_button(void *data, struct wl_pointer *pointer, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 461 | uint32_t serial, uint32_t time, uint32_t button, |
| 462 | uint32_t state_w) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 463 | { |
| 464 | struct wayland_input *input = data; |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 465 | enum wl_pointer_button_state state = state_w; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 466 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 467 | notify_button(&input->base, time, button, state); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | static void |
| 471 | input_handle_axis(void *data, struct wl_pointer *pointer, |
Daniel Stone | 2fce402 | 2012-05-30 16:32:00 +0100 | [diff] [blame] | 472 | uint32_t time, uint32_t axis, wl_fixed_t value) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 473 | { |
| 474 | struct wayland_input *input = data; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 475 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 476 | notify_axis(&input->base, time, axis, value); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static const struct wl_pointer_listener pointer_listener = { |
| 480 | input_handle_pointer_enter, |
| 481 | input_handle_pointer_leave, |
| 482 | input_handle_motion, |
| 483 | input_handle_button, |
| 484 | input_handle_axis, |
| 485 | }; |
| 486 | |
| 487 | static void |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 488 | input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, |
| 489 | int fd, uint32_t size) |
| 490 | { |
| 491 | struct wayland_input *input = data; |
| 492 | struct xkb_keymap *keymap; |
| 493 | char *map_str; |
| 494 | |
| 495 | if (!data) { |
| 496 | close(fd); |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { |
| 501 | close(fd); |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); |
| 506 | if (map_str == MAP_FAILED) { |
| 507 | close(fd); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | keymap = xkb_map_new_from_string(input->compositor->base.xkb_context, |
| 512 | map_str, |
| 513 | XKB_KEYMAP_FORMAT_TEXT_V1, |
| 514 | 0); |
| 515 | munmap(map_str, size); |
| 516 | close(fd); |
| 517 | |
| 518 | if (!keymap) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 519 | weston_log("failed to compile keymap\n"); |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 520 | return; |
| 521 | } |
| 522 | |
Rui Matos | 0c194ce | 2013-10-10 19:44:21 +0200 | [diff] [blame] | 523 | if (input->base.keyboard) |
| 524 | weston_seat_update_keymap(&input->base, keymap); |
| 525 | else |
| 526 | weston_seat_init_keyboard(&input->base, keymap); |
| 527 | |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 528 | xkb_map_unref(keymap); |
| 529 | } |
| 530 | |
| 531 | static void |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 532 | input_handle_keyboard_enter(void *data, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 533 | struct wl_keyboard *keyboard, |
| 534 | uint32_t serial, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 535 | struct wl_surface *surface, |
| 536 | struct wl_array *keys) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 537 | { |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 538 | struct wayland_input *input = data; |
Kristian Høgsberg | af82bea | 2011-01-27 20:18:17 -0500 | [diff] [blame] | 539 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 540 | /* XXX: If we get a modifier event immediately before the focus, |
| 541 | * we should try to keep the same serial. */ |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 542 | notify_keyboard_focus_in(&input->base, keys, |
Daniel Stone | d6da09e | 2012-06-22 13:21:29 +0100 | [diff] [blame] | 543 | STATE_UPDATE_AUTOMATIC); |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | static void |
| 547 | input_handle_keyboard_leave(void *data, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 548 | struct wl_keyboard *keyboard, |
| 549 | uint32_t serial, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 550 | struct wl_surface *surface) |
| 551 | { |
| 552 | struct wayland_input *input = data; |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 553 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 554 | notify_keyboard_focus_out(&input->base); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 555 | } |
| 556 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 557 | static void |
| 558 | input_handle_key(void *data, struct wl_keyboard *keyboard, |
| 559 | uint32_t serial, uint32_t time, uint32_t key, uint32_t state) |
| 560 | { |
| 561 | struct wayland_input *input = data; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 562 | |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 563 | input->key_serial = serial; |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 564 | notify_key(&input->base, time, key, |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 565 | state ? WL_KEYBOARD_KEY_STATE_PRESSED : |
Daniel Stone | 1b4e11f | 2012-06-22 13:21:37 +0100 | [diff] [blame] | 566 | WL_KEYBOARD_KEY_STATE_RELEASED, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 567 | STATE_UPDATE_NONE); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 568 | } |
| 569 | |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 570 | static void |
| 571 | input_handle_modifiers(void *data, struct wl_keyboard *keyboard, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 572 | uint32_t serial_in, uint32_t mods_depressed, |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 573 | uint32_t mods_latched, uint32_t mods_locked, |
| 574 | uint32_t group) |
| 575 | { |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 576 | struct wayland_input *input = data; |
| 577 | struct wayland_compositor *c = input->compositor; |
| 578 | uint32_t serial_out; |
| 579 | |
| 580 | /* If we get a key event followed by a modifier event with the |
| 581 | * same serial number, then we try to preserve those semantics by |
| 582 | * reusing the same serial number on the way out too. */ |
| 583 | if (serial_in == input->key_serial) |
| 584 | serial_out = wl_display_get_serial(c->base.wl_display); |
| 585 | else |
| 586 | serial_out = wl_display_next_serial(c->base.wl_display); |
| 587 | |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 588 | xkb_state_update_mask(input->base.xkb_state.state, |
Daniel Stone | 5069280 | 2012-06-22 13:21:41 +0100 | [diff] [blame] | 589 | mods_depressed, mods_latched, |
| 590 | mods_locked, 0, 0, group); |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 591 | notify_modifiers(&input->base, serial_out); |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 592 | } |
| 593 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 594 | static const struct wl_keyboard_listener keyboard_listener = { |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 595 | input_handle_keymap, |
Kristian Høgsberg | 06d58b7 | 2012-02-23 09:59:05 -0500 | [diff] [blame] | 596 | input_handle_keyboard_enter, |
| 597 | input_handle_keyboard_leave, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 598 | input_handle_key, |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 599 | input_handle_modifiers, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 600 | }; |
| 601 | |
| 602 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 603 | input_handle_capabilities(void *data, struct wl_seat *seat, |
| 604 | enum wl_seat_capability caps) |
| 605 | { |
| 606 | struct wayland_input *input = data; |
| 607 | |
| 608 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) { |
| 609 | input->pointer = wl_seat_get_pointer(seat); |
| 610 | wl_pointer_set_user_data(input->pointer, input); |
| 611 | wl_pointer_add_listener(input->pointer, &pointer_listener, |
| 612 | input); |
Kristian Høgsberg | 7af7ced | 2012-08-10 10:01:33 -0400 | [diff] [blame] | 613 | weston_seat_init_pointer(&input->base); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 614 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) { |
| 615 | wl_pointer_destroy(input->pointer); |
| 616 | input->pointer = NULL; |
| 617 | } |
| 618 | |
| 619 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) { |
| 620 | input->keyboard = wl_seat_get_keyboard(seat); |
| 621 | wl_keyboard_set_user_data(input->keyboard, input); |
| 622 | wl_keyboard_add_listener(input->keyboard, &keyboard_listener, |
| 623 | input); |
| 624 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) { |
| 625 | wl_keyboard_destroy(input->keyboard); |
| 626 | input->keyboard = NULL; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | static const struct wl_seat_listener seat_listener = { |
| 631 | input_handle_capabilities, |
| 632 | }; |
| 633 | |
| 634 | static void |
| 635 | display_add_seat(struct wayland_compositor *c, uint32_t id) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 636 | { |
| 637 | struct wayland_input *input; |
| 638 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 639 | input = zalloc(sizeof *input); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 640 | if (input == NULL) |
| 641 | return; |
| 642 | |
Rob Bradford | 9af5f9e | 2013-05-31 18:09:50 +0100 | [diff] [blame] | 643 | weston_seat_init(&input->base, &c->base, "default"); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 644 | input->compositor = c; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 645 | input->seat = wl_registry_bind(c->parent.registry, id, |
| 646 | &wl_seat_interface, 1); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 647 | wl_list_insert(c->input_list.prev, &input->link); |
| 648 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 649 | wl_seat_add_listener(input->seat, &seat_listener, input); |
| 650 | wl_seat_set_user_data(input->seat, input); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 654 | registry_handle_global(void *data, struct wl_registry *registry, uint32_t name, |
| 655 | const char *interface, uint32_t version) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 656 | { |
| 657 | struct wayland_compositor *c = data; |
| 658 | |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 659 | if (strcmp(interface, "wl_compositor") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 660 | c->parent.compositor = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 661 | wl_registry_bind(registry, name, |
| 662 | &wl_compositor_interface, 1); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 663 | } else if (strcmp(interface, "wl_output") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 664 | c->parent.output = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 665 | wl_registry_bind(registry, name, |
| 666 | &wl_output_interface, 1); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 667 | wl_output_add_listener(c->parent.output, &output_listener, c); |
Benjamin Franzke | 080ab6c | 2011-04-30 10:41:27 +0200 | [diff] [blame] | 668 | } else if (strcmp(interface, "wl_shell") == 0) { |
Kristian Høgsberg | f790c79 | 2011-08-19 14:41:57 -0400 | [diff] [blame] | 669 | c->parent.shell = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 670 | wl_registry_bind(registry, name, |
| 671 | &wl_shell_interface, 1); |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 672 | } else if (strcmp(interface, "wl_seat") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 673 | display_add_seat(c, name); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 674 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 675 | c->parent.shm = |
| 676 | wl_registry_bind(registry, name, &wl_shm_interface, 1); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 680 | static const struct wl_registry_listener registry_listener = { |
| 681 | registry_handle_global |
| 682 | }; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 683 | |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 684 | static int |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 685 | wayland_compositor_handle_event(int fd, uint32_t mask, void *data) |
| 686 | { |
| 687 | struct wayland_compositor *c = data; |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 688 | int count = 0; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 689 | |
Kristian Høgsberg | 453de7a | 2013-10-30 23:15:44 -0700 | [diff] [blame] | 690 | if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) { |
| 691 | wl_display_terminate(c->base.wl_display); |
| 692 | return 0; |
| 693 | } |
| 694 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 695 | if (mask & WL_EVENT_READABLE) |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 696 | count = wl_display_dispatch(c->parent.wl_display); |
Kristian Høgsberg | f258a31 | 2011-12-28 22:51:20 -0500 | [diff] [blame] | 697 | if (mask & WL_EVENT_WRITABLE) |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 698 | wl_display_flush(c->parent.wl_display); |
Kristian Høgsberg | 95d843d | 2011-04-22 13:01:26 -0400 | [diff] [blame] | 699 | |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 700 | if (mask == 0) { |
| 701 | count = wl_display_dispatch_pending(c->parent.wl_display); |
| 702 | wl_display_flush(c->parent.wl_display); |
| 703 | } |
| 704 | |
| 705 | return count; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 706 | } |
| 707 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 708 | static void |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 709 | wayland_restore(struct weston_compositor *ec) |
| 710 | { |
| 711 | } |
| 712 | |
| 713 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 714 | wayland_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 715 | { |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 716 | struct wayland_compositor *c = (struct wayland_compositor *) ec; |
| 717 | |
Vasily Khoruzhick | 52cfd61 | 2013-01-08 19:09:01 +0300 | [diff] [blame] | 718 | ec->renderer->destroy(ec); |
Kristian Høgsberg | 3a0de88 | 2012-09-06 21:44:24 -0400 | [diff] [blame] | 719 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 720 | weston_compositor_shutdown(ec); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 721 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 722 | if (c->parent.shm) |
| 723 | wl_shm_destroy(c->parent.shm); |
| 724 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 725 | free(ec); |
| 726 | } |
| 727 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 728 | static struct weston_compositor * |
Kristian Høgsberg | 2e28b10 | 2012-02-07 09:57:25 -0500 | [diff] [blame] | 729 | wayland_compositor_create(struct wl_display *display, |
Daniel Stone | baf4359 | 2012-06-01 11:11:10 -0400 | [diff] [blame] | 730 | int width, int height, const char *display_name, |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 731 | int *argc, char *argv[], |
| 732 | struct weston_config *config) |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 733 | { |
| 734 | struct wayland_compositor *c; |
| 735 | struct wl_event_loop *loop; |
| 736 | int fd; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 737 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 738 | c = zalloc(sizeof *c); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 739 | if (c == NULL) |
| 740 | return NULL; |
| 741 | |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 742 | if (weston_compositor_init(&c->base, display, argc, argv, |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 743 | config) < 0) |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 744 | goto err_free; |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 745 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 746 | c->parent.wl_display = wl_display_connect(display_name); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 747 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 748 | if (c->parent.wl_display == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 749 | weston_log("failed to create display: %m\n"); |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 750 | goto err_compositor; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | wl_list_init(&c->input_list); |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 754 | c->parent.registry = wl_display_get_registry(c->parent.wl_display); |
| 755 | wl_registry_add_listener(c->parent.registry, ®istry_listener, c); |
| 756 | wl_display_dispatch(c->parent.wl_display); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 757 | |
| 758 | c->base.wl_display = display; |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 759 | |
| 760 | gl_renderer = weston_load_module("gl-renderer.so", |
| 761 | "gl_renderer_interface"); |
| 762 | if (!gl_renderer) |
| 763 | goto err_display; |
| 764 | |
| 765 | if (gl_renderer->create(&c->base, c->parent.wl_display, |
| 766 | gl_renderer->alpha_attribs, |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 767 | NULL) < 0) |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 768 | goto err_display; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 769 | |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 770 | c->base.destroy = wayland_destroy; |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 771 | c->base.restore = wayland_restore; |
Benjamin Franzke | ecfb2b9 | 2011-01-15 12:34:48 +0100 | [diff] [blame] | 772 | |
John Kåre Alsaker | c37b32d | 2012-10-04 20:42:17 +0200 | [diff] [blame] | 773 | c->border.top = 30; |
| 774 | c->border.bottom = 24; |
John Kåre Alsaker | b9b8712 | 2012-10-04 20:42:16 +0200 | [diff] [blame] | 775 | c->border.left = 25; |
John Kåre Alsaker | c37b32d | 2012-10-04 20:42:17 +0200 | [diff] [blame] | 776 | c->border.right = 26; |
John Kåre Alsaker | b9b8712 | 2012-10-04 20:42:16 +0200 | [diff] [blame] | 777 | |
| 778 | /* requires border fields */ |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 779 | if (wayland_compositor_create_output(c, width, height) < 0) |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 780 | goto err_gl; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 781 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 782 | /* requires gl_renderer->output_state_create called |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 783 | * by wayland_compositor_create_output */ |
John Kåre Alsaker | 4b3081a | 2012-10-03 18:02:22 +0200 | [diff] [blame] | 784 | create_border(c); |
| 785 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 786 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 787 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 788 | fd = wl_display_get_fd(c->parent.wl_display); |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 789 | c->parent.wl_source = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 790 | wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE, |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 791 | wayland_compositor_handle_event, c); |
| 792 | if (c->parent.wl_source == NULL) |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 793 | goto err_gl; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 794 | |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 795 | wl_event_source_check(c->parent.wl_source); |
| 796 | |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 797 | return &c->base; |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 798 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 799 | err_gl: |
Vasily Khoruzhick | 52cfd61 | 2013-01-08 19:09:01 +0300 | [diff] [blame] | 800 | c->base.renderer->destroy(&c->base); |
Martin Olsson | c5db50f | 2012-07-08 03:03:43 +0200 | [diff] [blame] | 801 | err_display: |
| 802 | wl_display_disconnect(c->parent.wl_display); |
| 803 | err_compositor: |
| 804 | weston_compositor_shutdown(&c->base); |
| 805 | err_free: |
| 806 | free(c); |
| 807 | return NULL; |
Benjamin Franzke | ec2e642 | 2010-11-27 19:04:12 +0100 | [diff] [blame] | 808 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 809 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 810 | WL_EXPORT struct weston_compositor * |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 811 | backend_init(struct wl_display *display, int *argc, char *argv[], |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 812 | struct weston_config *config) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 813 | { |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 814 | int width = 1024, height = 640; |
| 815 | char *display_name = NULL; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 816 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 817 | const struct weston_option wayland_options[] = { |
| 818 | { WESTON_OPTION_INTEGER, "width", 0, &width }, |
| 819 | { WESTON_OPTION_INTEGER, "height", 0, &height }, |
| 820 | { WESTON_OPTION_STRING, "display", 0, &display_name }, |
| 821 | }; |
| 822 | |
| 823 | parse_options(wayland_options, |
| 824 | ARRAY_LENGTH(wayland_options), argc, argv); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 825 | |
Daniel Stone | baf4359 | 2012-06-01 11:11:10 -0400 | [diff] [blame] | 826 | return wayland_compositor_create(display, width, height, display_name, |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 827 | argc, argv, config); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 828 | } |