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