blob: 8136bffef3018ac1e864fd29572a41b8bc86e40b [file] [log] [blame]
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Benjamin Franzke
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01003 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04004 * 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 Franzkeec2e6422010-11-27 19:04:12 +010013 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040014 * 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 Franzkeec2e6422010-11-27 19:04:12 +010021 */
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 Stoneb7452fe2012-06-01 12:14:06 +010034#include <sys/mman.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010035
Benjamin Franzkebe014562011-02-18 17:04:24 +010036#include <wayland-client.h>
37#include <wayland-egl.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010038
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010039#include <GLES2/gl2.h>
40#include <GLES2/gl2ext.h>
41#include <EGL/egl.h>
42#include <EGL/eglext.h>
43
44#include "compositor.h"
45
46struct wayland_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050047 struct weston_compositor base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010048
49 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040050 struct wl_display *wl_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010051 struct wl_compositor *compositor;
52 struct wl_shell *shell;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010053 struct wl_output *output;
54
55 struct {
56 int32_t x, y, width, height;
57 } screen_allocation;
58
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010059 struct wl_event_source *wl_source;
60 uint32_t event_mask;
61 } parent;
62
Kristian Høgsberg546a8122012-02-01 07:45:51 -050063 struct {
64 int32_t top, bottom, left, right;
65 GLuint texture;
66 int32_t width, height;
67 } border;
68
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010069 struct wl_list input_list;
70};
71
72struct wayland_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050073 struct weston_output base;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040074 struct wl_listener frame_listener;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010075 struct {
76 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020077 struct wl_shell_surface *shell_surface;
Benjamin Franzkebe014562011-02-18 17:04:24 +010078 struct wl_egl_window *egl_window;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010079 } parent;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050080 struct weston_mode mode;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010081};
82
83struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -040084 struct weston_seat base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010085 struct wayland_compositor *compositor;
Daniel Stone37816df2012-05-16 18:45:18 +010086 struct wl_seat *seat;
87 struct wl_pointer *pointer;
88 struct wl_keyboard *keyboard;
89 struct wl_touch *touch;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010090 struct wl_list link;
Daniel Stone50692802012-06-22 13:21:41 +010091 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -040092 uint32_t enter_serial;
93 int focus;
94 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010095};
96
Kristian Høgsberg546a8122012-02-01 07:45:51 -050097
98static int
99texture_border(struct wayland_output *output)
100{
101 struct wayland_compositor *c =
102 (struct wayland_compositor *) output->base.compositor;
103 GLfloat *d;
104 unsigned int *p;
105 int i, j, k, n;
106 GLfloat x[4], y[4], u[4], v[4];
107
108 x[0] = -c->border.left;
109 x[1] = 0;
110 x[2] = output->base.current->width;
111 x[3] = output->base.current->width + c->border.right;
112
113 y[0] = -c->border.top;
114 y[1] = 0;
115 y[2] = output->base.current->height;
116 y[3] = output->base.current->height + c->border.bottom;
117
118 u[0] = 0.0;
119 u[1] = (GLfloat) c->border.left / c->border.width;
120 u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
121 u[3] = 1.0;
122
123 v[0] = 0.0;
124 v[1] = (GLfloat) c->border.top / c->border.height;
125 v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
126 v[3] = 1.0;
127
128 n = 8;
129 d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
130 p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
131
132 k = 0;
133 for (i = 0; i < 3; i++)
134 for (j = 0; j < 3; j++) {
135
136 if (i == 1 && j == 1)
137 continue;
138
139 d[ 0] = x[i];
140 d[ 1] = y[j];
141 d[ 2] = u[i];
142 d[ 3] = v[j];
143
144 d[ 4] = x[i];
145 d[ 5] = y[j + 1];
146 d[ 6] = u[i];
147 d[ 7] = v[j + 1];
148
149 d[ 8] = x[i + 1];
150 d[ 9] = y[j];
151 d[10] = u[i + 1];
152 d[11] = v[j];
153
154 d[12] = x[i + 1];
155 d[13] = y[j + 1];
156 d[14] = u[i + 1];
157 d[15] = v[j + 1];
158
159 p[0] = k + 0;
160 p[1] = k + 1;
161 p[2] = k + 2;
162 p[3] = k + 2;
163 p[4] = k + 1;
164 p[5] = k + 3;
165
166 d += 16;
167 p += 6;
168 k += 4;
169 }
170
171 return k / 4;
172}
173
174static void
175draw_border(struct wayland_output *output)
176{
177 struct wayland_compositor *c =
178 (struct wayland_compositor *) output->base.compositor;
Gwenole Beauchesne6d030492012-04-20 11:15:51 +0200179 struct weston_shader *shader = &c->base.texture_shader_rgba;
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500180 GLfloat *v;
181 int n;
182
183 glDisable(GL_BLEND);
184 glUseProgram(shader->program);
185 c->base.current_shader = shader;
186
187 glUniformMatrix4fv(shader->proj_uniform,
188 1, GL_FALSE, output->base.matrix.d);
189
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200190 glUniform1i(shader->tex_uniforms[0], 0);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500191 glUniform1f(shader->alpha_uniform, 1);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500192
193 n = texture_border(output);
194
195 glBindTexture(GL_TEXTURE_2D, c->border.texture);
196
197 v = c->base.vertices.data;
198 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
199 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
200 glEnableVertexAttribArray(0);
201 glEnableVertexAttribArray(1);
202
203 glDrawElements(GL_TRIANGLES, n * 6,
204 GL_UNSIGNED_INT, c->base.indices.data);
205
206 glDisableVertexAttribArray(1);
207 glDisableVertexAttribArray(0);
208
209 c->base.vertices.size = 0;
210 c->base.indices.size = 0;
211}
212
213static void
214create_border(struct wayland_compositor *c)
215{
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200216 pixman_image_t *image;
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500217
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200218 image = load_image(DATADIR "/weston/border.png");
219 if (!image) {
Martin Minarik6d118362012-06-07 18:01:59 +0200220 weston_log("could'nt load border image\n");
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500221 return;
222 }
223
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200224 c->border.width = pixman_image_get_width(image);
225 c->border.height = pixman_image_get_height(image);
226
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500227 glGenTextures(1, &c->border.texture);
228 glBindTexture(GL_TEXTURE_2D, c->border.texture);
229 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
233
234 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
235 c->border.width,
236 c->border.height,
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200237 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
238 pixman_image_get_data(image));
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500239
240 c->border.top = 25;
241 c->border.bottom = 50;
242 c->border.left = 25;
243 c->border.right = 25;
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200244
245 pixman_image_unref(image);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500246}
247
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100248static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100249wayland_compositor_init_egl(struct wayland_compositor *c)
250{
251 EGLint major, minor;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100252 EGLint n;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100253 EGLint config_attribs[] = {
254 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
255 EGL_RED_SIZE, 1,
256 EGL_GREEN_SIZE, 1,
257 EGL_BLUE_SIZE, 1,
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500258 EGL_ALPHA_SIZE, 1,
Kristian Høgsbergd28ab362011-03-02 11:36:30 -0500259 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100260 EGL_NONE
261 };
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100262 static const EGLint context_attribs[] = {
263 EGL_CONTEXT_CLIENT_VERSION, 2,
264 EGL_NONE
265 };
266
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400267 c->base.egl_display = eglGetDisplay(c->parent.wl_display);
268 if (c->base.egl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200269 weston_log("failed to create display\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100270 return -1;
271 }
272
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400273 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200274 weston_log("failed to initialize display\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100275 return -1;
276 }
277
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100278 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200279 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100280 return -1;
281 }
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400282 if (!eglChooseConfig(c->base.egl_display, config_attribs,
283 &c->base.egl_config, 1, &n) || n == 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200284 weston_log("failed to choose config: %d\n", n);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100285 return -1;
286 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100287
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400288 c->base.egl_context =
289 eglCreateContext(c->base.egl_display, c->base.egl_config,
290 EGL_NO_CONTEXT, context_attribs);
291 if (c->base.egl_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200292 weston_log("failed to create context\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100293 return -1;
294 }
295
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100296 return 0;
297}
298
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500299static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500300frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400301{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500302 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400303
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500304 wl_callback_destroy(callback);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500305 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400306}
307
308static const struct wl_callback_listener frame_listener = {
309 frame_done
310};
311
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500312static void
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400313wayland_output_frame_notify(struct wl_listener *listener, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100314{
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400315 struct wayland_output *output =
316 container_of(listener,
317 struct wayland_output, frame_listener);
Pekka Paalanen07c91f82012-08-30 16:47:21 -0500318
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500319 draw_border(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400320}
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500321
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400322static void
323wayland_output_repaint(struct weston_output *output_base,
324 pixman_region32_t *damage)
325{
326 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400327 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400328 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600329
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400330 ec->renderer->repaint_output(&output->base, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400331
Kristian Høgsberg33418202011-08-16 23:01:28 -0400332 callback = wl_surface_frame(output->parent.surface);
333 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100334
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500335 return;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100336}
337
Matt Roper361d2ad2011-08-29 13:52:23 -0700338static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500339wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700340{
341 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500342 struct weston_compositor *ec = output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700343
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400344 eglDestroySurface(ec->egl_display, output->base.egl_surface);
Matt Roper361d2ad2011-08-29 13:52:23 -0700345 wl_egl_window_destroy(output->parent.egl_window);
346 free(output);
347
348 return;
349}
350
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300351static const struct wl_shell_surface_listener shell_surface_listener;
352
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200353static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100354wayland_compositor_create_output(struct wayland_compositor *c,
355 int width, int height)
356{
357 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100358
359 output = malloc(sizeof *output);
360 if (output == NULL)
361 return -1;
362 memset(output, 0, sizeof *output);
363
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400364 output->mode.flags =
365 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
366 output->mode.width = width;
367 output->mode.height = height;
368 output->mode.refresh = 60;
369 wl_list_init(&output->base.mode_list);
370 wl_list_insert(&output->base.mode_list, &output->mode.link);
371
372 output->base.current = &output->mode;
Scott Moreau1bad5db2012-08-18 01:04:05 -0600373 weston_output_init(&output->base, &c->base, 0, 0, width, height,
374 WL_OUTPUT_TRANSFORM_NORMAL);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400375
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500376 output->base.border.top = c->border.top;
377 output->base.border.bottom = c->border.bottom;
378 output->base.border.left = c->border.left;
379 output->base.border.right = c->border.right;
Kristian Høgsberg90bc88c2012-08-13 23:34:04 -0400380 output->base.make = "waywayland";
381 output->base.model = "none";
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500382
383 weston_output_move(&output->base, 0, 0);
384
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100385 output->parent.surface =
386 wl_compositor_create_surface(c->parent.compositor);
387 wl_surface_set_user_data(output->parent.surface, output);
388
Benjamin Franzkebe014562011-02-18 17:04:24 +0100389 output->parent.egl_window =
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500390 wl_egl_window_create(output->parent.surface,
391 width + c->border.left + c->border.right,
392 height + c->border.top + c->border.bottom);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100393 if (!output->parent.egl_window) {
Martin Minarik6d118362012-06-07 18:01:59 +0200394 weston_log("failure to create wl_egl_window\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100395 goto cleanup_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100396 }
397
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400398 output->base.egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400399 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100400 output->parent.egl_window, NULL);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400401 if (!output->base.egl_surface) {
Martin Minarik6d118362012-06-07 18:01:59 +0200402 weston_log("failed to create window surface\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100403 goto cleanup_window;
404 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100405
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400406 if (!eglMakeCurrent(c->base.egl_display, output->base.egl_surface,
407 output->base.egl_surface, c->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200408 weston_log("failed to make surface current\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100409 goto cleanup_surface;
410 return -1;
411 }
412
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200413 output->parent.shell_surface =
414 wl_shell_get_shell_surface(c->parent.shell,
415 output->parent.surface);
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300416 wl_shell_surface_add_listener(output->parent.shell_surface,
417 &shell_surface_listener, output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200418 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100419
Alex Wubd3354b2012-04-17 17:20:49 +0800420 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500421 output->base.repaint = wayland_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700422 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800423 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200424 output->base.set_backlight = NULL;
425 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800426 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100427
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100428 wl_list_insert(c->base.output_list.prev, &output->base.link);
429
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400430 output->frame_listener.notify = wayland_output_frame_notify;
431 wl_signal_add(&output->base.frame_signal, &output->frame_listener);
432
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100433 return 0;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100434
435cleanup_surface:
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400436 eglDestroySurface(c->base.egl_display, output->base.egl_surface);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100437cleanup_window:
438 wl_egl_window_destroy(output->parent.egl_window);
439cleanup_output:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500440 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +0100441 free(output);
442
443 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100444}
445
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300446static void
447shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
448 uint32_t serial)
449{
450 wl_shell_surface_pong(shell_surface, serial);
451}
452
453static void
454shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
455 uint32_t edges, int32_t width, int32_t height)
456{
457 /* FIXME: implement resizing */
458}
459
460static void
461shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
462{
463}
464
465static const struct wl_shell_surface_listener shell_surface_listener = {
466 shell_surface_ping,
467 shell_surface_configure,
468 shell_surface_popup_done
469};
470
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100471/* Events received from the wayland-server this compositor is client of: */
472
473/* parent output interface */
474static void
475display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400476 struct wl_output *wl_output,
477 int x,
478 int y,
479 int physical_width,
480 int physical_height,
481 int subpixel,
482 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -0400483 const char *model,
484 int transform)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100485{
486 struct wayland_compositor *c = data;
487
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500488 c->parent.screen_allocation.x = x;
489 c->parent.screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400490}
491
492static void
493display_handle_mode(void *data,
494 struct wl_output *wl_output,
495 uint32_t flags,
496 int width,
497 int height,
498 int refresh)
499{
500 struct wayland_compositor *c = data;
501
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100502 c->parent.screen_allocation.width = width;
503 c->parent.screen_allocation.height = height;
504}
505
506static const struct wl_output_listener output_listener = {
507 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400508 display_handle_mode
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100509};
510
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400511static void
512check_focus(struct wayland_input *input, wl_fixed_t x, wl_fixed_t y)
513{
514 struct wayland_compositor *c = input->compositor;
515 int width, height, inside;
516
517 width = input->output->mode.width;
518 height = input->output->mode.height;
519
520 inside = c->border.left <= wl_fixed_to_int(x) &&
521 wl_fixed_to_int(x) < width + c->border.left &&
522 c->border.top <= wl_fixed_to_int(y) &&
523 wl_fixed_to_int(y) < height + c->border.top;
524
525 if (!input->focus && inside) {
526 notify_pointer_focus(&input->base, &input->output->base,
527 x - wl_fixed_from_int(c->border.left),
528 y = wl_fixed_from_int(c->border.top));
529 wl_pointer_set_cursor(input->pointer,
530 input->enter_serial, NULL, 0, 0);
531 } else if (input->focus && !inside) {
532 notify_pointer_focus(&input->base, NULL, 0, 0);
533 /* FIXME: Should set default cursor here. */
534 }
535
536 input->focus = inside;
537}
538
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100539/* parent input interface */
540static void
Daniel Stone37816df2012-05-16 18:45:18 +0100541input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
542 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400543 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100544{
545 struct wayland_input *input = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100546
Daniel Stone50692802012-06-22 13:21:41 +0100547 /* XXX: If we get a modifier event immediately before the focus,
548 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400549 input->enter_serial = serial;
550 input->output = wl_surface_get_user_data(surface);
551 check_focus(input, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100552}
553
554static void
Daniel Stone37816df2012-05-16 18:45:18 +0100555input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
556 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500557{
558 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500559
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400560 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400561 input->output = NULL;
562 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500563}
564
565static void
Daniel Stone37816df2012-05-16 18:45:18 +0100566input_handle_motion(void *data, struct wl_pointer *pointer,
567 uint32_t time, wl_fixed_t x, wl_fixed_t y)
568{
569 struct wayland_input *input = data;
570 struct wayland_compositor *c = input->compositor;
571
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400572 check_focus(input, x, y);
573 if (input->focus)
574 notify_motion(&input->base, time,
575 x - wl_fixed_from_int(c->border.left),
576 y - wl_fixed_from_int(c->border.top));
Daniel Stone37816df2012-05-16 18:45:18 +0100577}
578
579static void
580input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100581 uint32_t serial, uint32_t time, uint32_t button,
582 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +0100583{
584 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100585 enum wl_pointer_button_state state = state_w;
Daniel Stone37816df2012-05-16 18:45:18 +0100586
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400587 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +0100588}
589
590static void
591input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100592 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +0100593{
594 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100595
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400596 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +0100597}
598
599static const struct wl_pointer_listener pointer_listener = {
600 input_handle_pointer_enter,
601 input_handle_pointer_leave,
602 input_handle_motion,
603 input_handle_button,
604 input_handle_axis,
605};
606
607static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100608input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
609 int fd, uint32_t size)
610{
611 struct wayland_input *input = data;
612 struct xkb_keymap *keymap;
613 char *map_str;
614
615 if (!data) {
616 close(fd);
617 return;
618 }
619
620 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
621 close(fd);
622 return;
623 }
624
625 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
626 if (map_str == MAP_FAILED) {
627 close(fd);
628 return;
629 }
630
631 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
632 map_str,
633 XKB_KEYMAP_FORMAT_TEXT_V1,
634 0);
635 munmap(map_str, size);
636 close(fd);
637
638 if (!keymap) {
Martin Minarik6d118362012-06-07 18:01:59 +0200639 weston_log("failed to compile keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100640 return;
641 }
642
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400643 weston_seat_init_keyboard(&input->base, keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100644 xkb_map_unref(keymap);
645}
646
647static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500648input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100649 struct wl_keyboard *keyboard,
650 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500651 struct wl_surface *surface,
652 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100653{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500654 struct wayland_input *input = data;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500655
Daniel Stone50692802012-06-22 13:21:41 +0100656 /* XXX: If we get a modifier event immediately before the focus,
657 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400658 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +0100659 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500660}
661
662static void
663input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100664 struct wl_keyboard *keyboard,
665 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500666 struct wl_surface *surface)
667{
668 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500669
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400670 notify_keyboard_focus_out(&input->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100671}
672
Daniel Stone37816df2012-05-16 18:45:18 +0100673static void
674input_handle_key(void *data, struct wl_keyboard *keyboard,
675 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
676{
677 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100678
Daniel Stone50692802012-06-22 13:21:41 +0100679 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400680 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100681 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100682 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stone50692802012-06-22 13:21:41 +0100683 STATE_UPDATE_NONE);
Daniel Stone37816df2012-05-16 18:45:18 +0100684}
685
Daniel Stone351eb612012-05-31 15:27:47 -0400686static void
687input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +0100688 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -0400689 uint32_t mods_latched, uint32_t mods_locked,
690 uint32_t group)
691{
Daniel Stone50692802012-06-22 13:21:41 +0100692 struct wayland_input *input = data;
693 struct wayland_compositor *c = input->compositor;
694 uint32_t serial_out;
695
696 /* If we get a key event followed by a modifier event with the
697 * same serial number, then we try to preserve those semantics by
698 * reusing the same serial number on the way out too. */
699 if (serial_in == input->key_serial)
700 serial_out = wl_display_get_serial(c->base.wl_display);
701 else
702 serial_out = wl_display_next_serial(c->base.wl_display);
703
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400704 xkb_state_update_mask(input->base.xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +0100705 mods_depressed, mods_latched,
706 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400707 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -0400708}
709
Daniel Stone37816df2012-05-16 18:45:18 +0100710static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100711 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500712 input_handle_keyboard_enter,
713 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +0100714 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -0400715 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100716};
717
718static void
Daniel Stone37816df2012-05-16 18:45:18 +0100719input_handle_capabilities(void *data, struct wl_seat *seat,
720 enum wl_seat_capability caps)
721{
722 struct wayland_input *input = data;
723
724 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
725 input->pointer = wl_seat_get_pointer(seat);
726 wl_pointer_set_user_data(input->pointer, input);
727 wl_pointer_add_listener(input->pointer, &pointer_listener,
728 input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400729 weston_seat_init_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +0100730 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
731 wl_pointer_destroy(input->pointer);
732 input->pointer = NULL;
733 }
734
735 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
736 input->keyboard = wl_seat_get_keyboard(seat);
737 wl_keyboard_set_user_data(input->keyboard, input);
738 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
739 input);
740 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
741 wl_keyboard_destroy(input->keyboard);
742 input->keyboard = NULL;
743 }
744}
745
746static const struct wl_seat_listener seat_listener = {
747 input_handle_capabilities,
748};
749
750static void
751display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100752{
753 struct wayland_input *input;
754
755 input = malloc(sizeof *input);
756 if (input == NULL)
757 return;
758
759 memset(input, 0, sizeof *input);
760
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400761 weston_seat_init(&input->base, &c->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100762 input->compositor = c;
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400763 input->seat = wl_display_bind(c->parent.wl_display, id,
Daniel Stone37816df2012-05-16 18:45:18 +0100764 &wl_seat_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100765 wl_list_insert(c->input_list.prev, &input->link);
766
Daniel Stone37816df2012-05-16 18:45:18 +0100767 wl_seat_add_listener(input->seat, &seat_listener, input);
768 wl_seat_set_user_data(input->seat, input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100769}
770
771static void
772display_handle_global(struct wl_display *display, uint32_t id,
773 const char *interface, uint32_t version, void *data)
774{
775 struct wayland_compositor *c = data;
776
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200777 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400778 c->parent.compositor =
779 wl_display_bind(display, id, &wl_compositor_interface);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200780 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400781 c->parent.output =
782 wl_display_bind(display, id, &wl_output_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100783 wl_output_add_listener(c->parent.output, &output_listener, c);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200784 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400785 c->parent.shell =
786 wl_display_bind(display, id, &wl_shell_interface);
Daniel Stone725c2c32012-06-22 14:04:36 +0100787 } else if (strcmp(interface, "wl_seat") == 0) {
788 display_add_seat(c, id);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100789 }
790}
791
792static int
793update_event_mask(uint32_t mask, void *data)
794{
795 struct wayland_compositor *c = data;
796
797 c->parent.event_mask = mask;
798 if (c->parent.wl_source)
799 wl_event_source_fd_update(c->parent.wl_source, mask);
800
801 return 0;
802}
803
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400804static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100805wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
806{
807 struct wayland_compositor *c = data;
808
809 if (mask & WL_EVENT_READABLE)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400810 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_READABLE);
Kristian Høgsbergf258a312011-12-28 22:51:20 -0500811 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400812 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_WRITABLE);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400813
814 return 1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100815}
816
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500817static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -0400818wayland_restore(struct weston_compositor *ec)
819{
820}
821
822static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500823wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500824{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500825 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -0700826
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500827 free(ec);
828}
829
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500830static struct weston_compositor *
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500831wayland_compositor_create(struct wl_display *display,
Daniel Stonebaf43592012-06-01 11:11:10 -0400832 int width, int height, const char *display_name,
Daniel Stonec1be8e52012-06-01 11:14:02 -0400833 int argc, char *argv[], const char *config_file)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100834{
835 struct wayland_compositor *c;
836 struct wl_event_loop *loop;
837 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100838
839 c = malloc(sizeof *c);
840 if (c == NULL)
841 return NULL;
842
843 memset(c, 0, sizeof *c);
844
Daniel Stone725c2c32012-06-22 14:04:36 +0100845 if (weston_compositor_init(&c->base, display, argc, argv,
846 config_file) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200847 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +0100848
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400849 c->parent.wl_display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100850
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400851 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200852 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +0200853 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100854 }
855
856 wl_list_init(&c->input_list);
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400857 wl_display_add_global_listener(c->parent.wl_display,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100858 display_handle_global, c);
859
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400860 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_READABLE);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100861
862 c->base.wl_display = display;
863 if (wayland_compositor_init_egl(c) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200864 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100865
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100866 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -0400867 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100868
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500869 create_border(c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100870 if (wayland_compositor_create_output(c, width, height) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200871 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100872
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -0400873 if (gles2_renderer_init(&c->base) < 0)
874 goto err_display;
875
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100876 loop = wl_display_get_event_loop(c->base.wl_display);
877
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400878 fd = wl_display_get_fd(c->parent.wl_display, update_event_mask, c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100879 c->parent.wl_source =
880 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
881 wayland_compositor_handle_event, c);
882 if (c->parent.wl_source == NULL)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200883 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100884
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100885 return &c->base;
Martin Olssonc5db50f2012-07-08 03:03:43 +0200886
887err_display:
888 wl_display_disconnect(c->parent.wl_display);
889err_compositor:
890 weston_compositor_shutdown(&c->base);
891err_free:
892 free(c);
893 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100894}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400895
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500896WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -0400897backend_init(struct wl_display *display, int argc, char *argv[],
898 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400899{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400900 int width = 1024, height = 640;
901 char *display_name = NULL;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400902
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400903 const struct weston_option wayland_options[] = {
904 { WESTON_OPTION_INTEGER, "width", 0, &width },
905 { WESTON_OPTION_INTEGER, "height", 0, &height },
906 { WESTON_OPTION_STRING, "display", 0, &display_name },
907 };
908
909 parse_options(wayland_options,
910 ARRAY_LENGTH(wayland_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400911
Daniel Stonebaf43592012-06-01 11:11:10 -0400912 return wayland_compositor_create(display, width, height, display_name,
Daniel Stonec1be8e52012-06-01 11:14:02 -0400913 argc, argv, config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400914}