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