blob: 49087982bb74232baaa372f3aa6e1929a9f4e1e9 [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
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -040049 struct wl_egl_pixmap *dummy_pixmap;
Kristian Høgsbergb71302e2012-05-10 12:28:35 -040050 EGLSurface dummy_egl_surface;
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -040051
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010052 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040053 struct wl_display *wl_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010054 struct wl_compositor *compositor;
55 struct wl_shell *shell;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010056 struct wl_output *output;
57
58 struct {
59 int32_t x, y, width, height;
60 } screen_allocation;
61
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010062 struct wl_event_source *wl_source;
63 uint32_t event_mask;
64 } parent;
65
Kristian Høgsberg546a8122012-02-01 07:45:51 -050066 struct {
67 int32_t top, bottom, left, right;
68 GLuint texture;
69 int32_t width, height;
70 } border;
71
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010072 struct wl_list input_list;
73};
74
75struct wayland_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050076 struct weston_output base;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -040077 struct wl_listener frame_listener;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010078 struct {
79 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020080 struct wl_shell_surface *shell_surface;
Benjamin Franzkebe014562011-02-18 17:04:24 +010081 struct wl_egl_window *egl_window;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010082 } parent;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050083 struct weston_mode mode;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010084};
85
86struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -040087 struct weston_seat base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010088 struct wayland_compositor *compositor;
Daniel Stone37816df2012-05-16 18:45:18 +010089 struct wl_seat *seat;
90 struct wl_pointer *pointer;
91 struct wl_keyboard *keyboard;
92 struct wl_touch *touch;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010093 struct wl_list link;
Daniel Stone50692802012-06-22 13:21:41 +010094 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -040095 uint32_t enter_serial;
96 int focus;
97 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010098};
99
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500100
101static int
102texture_border(struct wayland_output *output)
103{
104 struct wayland_compositor *c =
105 (struct wayland_compositor *) output->base.compositor;
106 GLfloat *d;
107 unsigned int *p;
108 int i, j, k, n;
109 GLfloat x[4], y[4], u[4], v[4];
110
111 x[0] = -c->border.left;
112 x[1] = 0;
113 x[2] = output->base.current->width;
114 x[3] = output->base.current->width + c->border.right;
115
116 y[0] = -c->border.top;
117 y[1] = 0;
118 y[2] = output->base.current->height;
119 y[3] = output->base.current->height + c->border.bottom;
120
121 u[0] = 0.0;
122 u[1] = (GLfloat) c->border.left / c->border.width;
123 u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
124 u[3] = 1.0;
125
126 v[0] = 0.0;
127 v[1] = (GLfloat) c->border.top / c->border.height;
128 v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
129 v[3] = 1.0;
130
131 n = 8;
132 d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
133 p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
134
135 k = 0;
136 for (i = 0; i < 3; i++)
137 for (j = 0; j < 3; j++) {
138
139 if (i == 1 && j == 1)
140 continue;
141
142 d[ 0] = x[i];
143 d[ 1] = y[j];
144 d[ 2] = u[i];
145 d[ 3] = v[j];
146
147 d[ 4] = x[i];
148 d[ 5] = y[j + 1];
149 d[ 6] = u[i];
150 d[ 7] = v[j + 1];
151
152 d[ 8] = x[i + 1];
153 d[ 9] = y[j];
154 d[10] = u[i + 1];
155 d[11] = v[j];
156
157 d[12] = x[i + 1];
158 d[13] = y[j + 1];
159 d[14] = u[i + 1];
160 d[15] = v[j + 1];
161
162 p[0] = k + 0;
163 p[1] = k + 1;
164 p[2] = k + 2;
165 p[3] = k + 2;
166 p[4] = k + 1;
167 p[5] = k + 3;
168
169 d += 16;
170 p += 6;
171 k += 4;
172 }
173
174 return k / 4;
175}
176
177static void
178draw_border(struct wayland_output *output)
179{
180 struct wayland_compositor *c =
181 (struct wayland_compositor *) output->base.compositor;
Gwenole Beauchesne6d030492012-04-20 11:15:51 +0200182 struct weston_shader *shader = &c->base.texture_shader_rgba;
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500183 GLfloat *v;
184 int n;
185
186 glDisable(GL_BLEND);
187 glUseProgram(shader->program);
188 c->base.current_shader = shader;
189
190 glUniformMatrix4fv(shader->proj_uniform,
191 1, GL_FALSE, output->base.matrix.d);
192
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200193 glUniform1i(shader->tex_uniforms[0], 0);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500194 glUniform1f(shader->alpha_uniform, 1);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500195
196 n = texture_border(output);
197
198 glBindTexture(GL_TEXTURE_2D, c->border.texture);
199
200 v = c->base.vertices.data;
201 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
202 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
203 glEnableVertexAttribArray(0);
204 glEnableVertexAttribArray(1);
205
206 glDrawElements(GL_TRIANGLES, n * 6,
207 GL_UNSIGNED_INT, c->base.indices.data);
208
209 glDisableVertexAttribArray(1);
210 glDisableVertexAttribArray(0);
211
212 c->base.vertices.size = 0;
213 c->base.indices.size = 0;
214}
215
216static void
217create_border(struct wayland_compositor *c)
218{
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200219 pixman_image_t *image;
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500220
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200221 image = load_image(DATADIR "/weston/border.png");
222 if (!image) {
Martin Minarik6d118362012-06-07 18:01:59 +0200223 weston_log("could'nt load border image\n");
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500224 return;
225 }
226
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200227 c->border.width = pixman_image_get_width(image);
228 c->border.height = pixman_image_get_height(image);
229
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500230 glGenTextures(1, &c->border.texture);
231 glBindTexture(GL_TEXTURE_2D, c->border.texture);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
235 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
236
237 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
238 c->border.width,
239 c->border.height,
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200240 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
241 pixman_image_get_data(image));
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500242
243 c->border.top = 25;
244 c->border.bottom = 50;
245 c->border.left = 25;
246 c->border.right = 25;
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200247
248 pixman_image_unref(image);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500249}
250
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100251static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100252wayland_compositor_init_egl(struct wayland_compositor *c)
253{
254 EGLint major, minor;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100255 EGLint n;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100256 EGLint config_attribs[] = {
257 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
258 EGL_RED_SIZE, 1,
259 EGL_GREEN_SIZE, 1,
260 EGL_BLUE_SIZE, 1,
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500261 EGL_ALPHA_SIZE, 1,
Kristian Høgsbergd28ab362011-03-02 11:36:30 -0500262 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100263 EGL_NONE
264 };
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100265 static const EGLint context_attribs[] = {
266 EGL_CONTEXT_CLIENT_VERSION, 2,
267 EGL_NONE
268 };
269
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400270 c->base.egl_display = eglGetDisplay(c->parent.wl_display);
271 if (c->base.egl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200272 weston_log("failed to create display\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100273 return -1;
274 }
275
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400276 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200277 weston_log("failed to initialize display\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100278 return -1;
279 }
280
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100281 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200282 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100283 return -1;
284 }
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400285 if (!eglChooseConfig(c->base.egl_display, config_attribs,
286 &c->base.egl_config, 1, &n) || n == 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200287 weston_log("failed to choose config: %d\n", n);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100288 return -1;
289 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100290
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400291 c->base.egl_context =
292 eglCreateContext(c->base.egl_display, c->base.egl_config,
293 EGL_NO_CONTEXT, context_attribs);
294 if (c->base.egl_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200295 weston_log("failed to create context\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100296 return -1;
297 }
298
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400299 c->dummy_pixmap = wl_egl_pixmap_create(10, 10, 0);
300 if (!c->dummy_pixmap) {
Martin Minarik6d118362012-06-07 18:01:59 +0200301 weston_log("failure to create dummy_pixmap\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400302 return -1;
303 }
304
305 c->dummy_egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400306 eglCreatePixmapSurface(c->base.egl_display, c->base.egl_config,
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400307 c->dummy_pixmap, NULL);
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400308 if (!eglMakeCurrent(c->base.egl_display, c->dummy_egl_surface,
309 c->dummy_egl_surface, c->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200310 weston_log("failed to make context current\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100311 return -1;
312 }
313
314 return 0;
315}
316
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500317static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500318frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400319{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500320 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400321
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500322 wl_callback_destroy(callback);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500323 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400324}
325
326static const struct wl_callback_listener frame_listener = {
327 frame_done
328};
329
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500330static void
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400331wayland_output_frame_notify(struct wl_listener *listener, void *data)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100332{
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400333 struct wayland_output *output =
334 container_of(listener,
335 struct wayland_output, frame_listener);
Pekka Paalanen07c91f82012-08-30 16:47:21 -0500336
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500337 draw_border(output);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400338}
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500339
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400340static void
341wayland_output_repaint(struct weston_output *output_base,
342 pixman_region32_t *damage)
343{
344 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400345 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400346 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600347
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400348 ec->renderer->repaint_output(&output->base, damage);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400349
Kristian Høgsberg33418202011-08-16 23:01:28 -0400350 callback = wl_surface_frame(output->parent.surface);
351 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100352
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500353 return;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100354}
355
Matt Roper361d2ad2011-08-29 13:52:23 -0700356static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500357wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700358{
359 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500360 struct weston_compositor *ec = output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700361
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400362 eglDestroySurface(ec->egl_display, output->base.egl_surface);
Matt Roper361d2ad2011-08-29 13:52:23 -0700363 wl_egl_window_destroy(output->parent.egl_window);
364 free(output);
365
366 return;
367}
368
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300369static const struct wl_shell_surface_listener shell_surface_listener;
370
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200371static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100372wayland_compositor_create_output(struct wayland_compositor *c,
373 int width, int height)
374{
375 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100376
377 output = malloc(sizeof *output);
378 if (output == NULL)
379 return -1;
380 memset(output, 0, sizeof *output);
381
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400382 output->mode.flags =
383 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
384 output->mode.width = width;
385 output->mode.height = height;
386 output->mode.refresh = 60;
387 wl_list_init(&output->base.mode_list);
388 wl_list_insert(&output->base.mode_list, &output->mode.link);
389
390 output->base.current = &output->mode;
Scott Moreau1bad5db2012-08-18 01:04:05 -0600391 weston_output_init(&output->base, &c->base, 0, 0, width, height,
392 WL_OUTPUT_TRANSFORM_NORMAL);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400393
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500394 output->base.border.top = c->border.top;
395 output->base.border.bottom = c->border.bottom;
396 output->base.border.left = c->border.left;
397 output->base.border.right = c->border.right;
Kristian Høgsberg90bc88c2012-08-13 23:34:04 -0400398 output->base.make = "waywayland";
399 output->base.model = "none";
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500400
401 weston_output_move(&output->base, 0, 0);
402
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100403 output->parent.surface =
404 wl_compositor_create_surface(c->parent.compositor);
405 wl_surface_set_user_data(output->parent.surface, output);
406
Benjamin Franzkebe014562011-02-18 17:04:24 +0100407 output->parent.egl_window =
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500408 wl_egl_window_create(output->parent.surface,
409 width + c->border.left + c->border.right,
410 height + c->border.top + c->border.bottom);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100411 if (!output->parent.egl_window) {
Martin Minarik6d118362012-06-07 18:01:59 +0200412 weston_log("failure to create wl_egl_window\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100413 goto cleanup_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100414 }
415
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400416 output->base.egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400417 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100418 output->parent.egl_window, NULL);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400419 if (!output->base.egl_surface) {
Martin Minarik6d118362012-06-07 18:01:59 +0200420 weston_log("failed to create window surface\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100421 goto cleanup_window;
422 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100423
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400424 if (!eglMakeCurrent(c->base.egl_display, output->base.egl_surface,
425 output->base.egl_surface, c->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200426 weston_log("failed to make surface current\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100427 goto cleanup_surface;
428 return -1;
429 }
430
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200431 output->parent.shell_surface =
432 wl_shell_get_shell_surface(c->parent.shell,
433 output->parent.surface);
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300434 wl_shell_surface_add_listener(output->parent.shell_surface,
435 &shell_surface_listener, output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200436 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100437
Alex Wubd3354b2012-04-17 17:20:49 +0800438 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500439 output->base.repaint = wayland_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700440 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800441 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200442 output->base.set_backlight = NULL;
443 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800444 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100445
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100446 wl_list_insert(c->base.output_list.prev, &output->base.link);
447
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400448 output->frame_listener.notify = wayland_output_frame_notify;
449 wl_signal_add(&output->base.frame_signal, &output->frame_listener);
450
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100451 return 0;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100452
453cleanup_surface:
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400454 eglDestroySurface(c->base.egl_display, output->base.egl_surface);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100455cleanup_window:
456 wl_egl_window_destroy(output->parent.egl_window);
457cleanup_output:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500458 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +0100459 free(output);
460
461 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100462}
463
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300464static void
465shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
466 uint32_t serial)
467{
468 wl_shell_surface_pong(shell_surface, serial);
469}
470
471static void
472shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
473 uint32_t edges, int32_t width, int32_t height)
474{
475 /* FIXME: implement resizing */
476}
477
478static void
479shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
480{
481}
482
483static const struct wl_shell_surface_listener shell_surface_listener = {
484 shell_surface_ping,
485 shell_surface_configure,
486 shell_surface_popup_done
487};
488
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100489/* Events received from the wayland-server this compositor is client of: */
490
491/* parent output interface */
492static void
493display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400494 struct wl_output *wl_output,
495 int x,
496 int y,
497 int physical_width,
498 int physical_height,
499 int subpixel,
500 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -0400501 const char *model,
502 int transform)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100503{
504 struct wayland_compositor *c = data;
505
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500506 c->parent.screen_allocation.x = x;
507 c->parent.screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400508}
509
510static void
511display_handle_mode(void *data,
512 struct wl_output *wl_output,
513 uint32_t flags,
514 int width,
515 int height,
516 int refresh)
517{
518 struct wayland_compositor *c = data;
519
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100520 c->parent.screen_allocation.width = width;
521 c->parent.screen_allocation.height = height;
522}
523
524static const struct wl_output_listener output_listener = {
525 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400526 display_handle_mode
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100527};
528
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400529static void
530check_focus(struct wayland_input *input, wl_fixed_t x, wl_fixed_t y)
531{
532 struct wayland_compositor *c = input->compositor;
533 int width, height, inside;
534
535 width = input->output->mode.width;
536 height = input->output->mode.height;
537
538 inside = c->border.left <= wl_fixed_to_int(x) &&
539 wl_fixed_to_int(x) < width + c->border.left &&
540 c->border.top <= wl_fixed_to_int(y) &&
541 wl_fixed_to_int(y) < height + c->border.top;
542
543 if (!input->focus && inside) {
544 notify_pointer_focus(&input->base, &input->output->base,
545 x - wl_fixed_from_int(c->border.left),
546 y = wl_fixed_from_int(c->border.top));
547 wl_pointer_set_cursor(input->pointer,
548 input->enter_serial, NULL, 0, 0);
549 } else if (input->focus && !inside) {
550 notify_pointer_focus(&input->base, NULL, 0, 0);
551 /* FIXME: Should set default cursor here. */
552 }
553
554 input->focus = inside;
555}
556
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100557/* parent input interface */
558static void
Daniel Stone37816df2012-05-16 18:45:18 +0100559input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
560 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400561 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100562{
563 struct wayland_input *input = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100564
Daniel Stone50692802012-06-22 13:21:41 +0100565 /* XXX: If we get a modifier event immediately before the focus,
566 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400567 input->enter_serial = serial;
568 input->output = wl_surface_get_user_data(surface);
569 check_focus(input, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100570}
571
572static void
Daniel Stone37816df2012-05-16 18:45:18 +0100573input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
574 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500575{
576 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500577
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400578 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400579 input->output = NULL;
580 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500581}
582
583static void
Daniel Stone37816df2012-05-16 18:45:18 +0100584input_handle_motion(void *data, struct wl_pointer *pointer,
585 uint32_t time, wl_fixed_t x, wl_fixed_t y)
586{
587 struct wayland_input *input = data;
588 struct wayland_compositor *c = input->compositor;
589
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400590 check_focus(input, x, y);
591 if (input->focus)
592 notify_motion(&input->base, time,
593 x - wl_fixed_from_int(c->border.left),
594 y - wl_fixed_from_int(c->border.top));
Daniel Stone37816df2012-05-16 18:45:18 +0100595}
596
597static void
598input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100599 uint32_t serial, uint32_t time, uint32_t button,
600 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +0100601{
602 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100603 enum wl_pointer_button_state state = state_w;
Daniel Stone37816df2012-05-16 18:45:18 +0100604
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400605 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +0100606}
607
608static void
609input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100610 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +0100611{
612 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100613
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400614 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +0100615}
616
617static const struct wl_pointer_listener pointer_listener = {
618 input_handle_pointer_enter,
619 input_handle_pointer_leave,
620 input_handle_motion,
621 input_handle_button,
622 input_handle_axis,
623};
624
625static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100626input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
627 int fd, uint32_t size)
628{
629 struct wayland_input *input = data;
630 struct xkb_keymap *keymap;
631 char *map_str;
632
633 if (!data) {
634 close(fd);
635 return;
636 }
637
638 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
639 close(fd);
640 return;
641 }
642
643 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
644 if (map_str == MAP_FAILED) {
645 close(fd);
646 return;
647 }
648
649 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
650 map_str,
651 XKB_KEYMAP_FORMAT_TEXT_V1,
652 0);
653 munmap(map_str, size);
654 close(fd);
655
656 if (!keymap) {
Martin Minarik6d118362012-06-07 18:01:59 +0200657 weston_log("failed to compile keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100658 return;
659 }
660
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400661 weston_seat_init_keyboard(&input->base, keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100662 xkb_map_unref(keymap);
663}
664
665static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500666input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100667 struct wl_keyboard *keyboard,
668 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500669 struct wl_surface *surface,
670 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100671{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500672 struct wayland_input *input = data;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500673
Daniel Stone50692802012-06-22 13:21:41 +0100674 /* XXX: If we get a modifier event immediately before the focus,
675 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400676 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +0100677 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500678}
679
680static void
681input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100682 struct wl_keyboard *keyboard,
683 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500684 struct wl_surface *surface)
685{
686 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500687
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400688 notify_keyboard_focus_out(&input->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100689}
690
Daniel Stone37816df2012-05-16 18:45:18 +0100691static void
692input_handle_key(void *data, struct wl_keyboard *keyboard,
693 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
694{
695 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100696
Daniel Stone50692802012-06-22 13:21:41 +0100697 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400698 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100699 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100700 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stone50692802012-06-22 13:21:41 +0100701 STATE_UPDATE_NONE);
Daniel Stone37816df2012-05-16 18:45:18 +0100702}
703
Daniel Stone351eb612012-05-31 15:27:47 -0400704static void
705input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +0100706 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -0400707 uint32_t mods_latched, uint32_t mods_locked,
708 uint32_t group)
709{
Daniel Stone50692802012-06-22 13:21:41 +0100710 struct wayland_input *input = data;
711 struct wayland_compositor *c = input->compositor;
712 uint32_t serial_out;
713
714 /* If we get a key event followed by a modifier event with the
715 * same serial number, then we try to preserve those semantics by
716 * reusing the same serial number on the way out too. */
717 if (serial_in == input->key_serial)
718 serial_out = wl_display_get_serial(c->base.wl_display);
719 else
720 serial_out = wl_display_next_serial(c->base.wl_display);
721
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400722 xkb_state_update_mask(input->base.xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +0100723 mods_depressed, mods_latched,
724 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400725 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -0400726}
727
Daniel Stone37816df2012-05-16 18:45:18 +0100728static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100729 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500730 input_handle_keyboard_enter,
731 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +0100732 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -0400733 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100734};
735
736static void
Daniel Stone37816df2012-05-16 18:45:18 +0100737input_handle_capabilities(void *data, struct wl_seat *seat,
738 enum wl_seat_capability caps)
739{
740 struct wayland_input *input = data;
741
742 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
743 input->pointer = wl_seat_get_pointer(seat);
744 wl_pointer_set_user_data(input->pointer, input);
745 wl_pointer_add_listener(input->pointer, &pointer_listener,
746 input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400747 weston_seat_init_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +0100748 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
749 wl_pointer_destroy(input->pointer);
750 input->pointer = NULL;
751 }
752
753 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
754 input->keyboard = wl_seat_get_keyboard(seat);
755 wl_keyboard_set_user_data(input->keyboard, input);
756 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
757 input);
758 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
759 wl_keyboard_destroy(input->keyboard);
760 input->keyboard = NULL;
761 }
762}
763
764static const struct wl_seat_listener seat_listener = {
765 input_handle_capabilities,
766};
767
768static void
769display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100770{
771 struct wayland_input *input;
772
773 input = malloc(sizeof *input);
774 if (input == NULL)
775 return;
776
777 memset(input, 0, sizeof *input);
778
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400779 weston_seat_init(&input->base, &c->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100780 input->compositor = c;
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400781 input->seat = wl_display_bind(c->parent.wl_display, id,
Daniel Stone37816df2012-05-16 18:45:18 +0100782 &wl_seat_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100783 wl_list_insert(c->input_list.prev, &input->link);
784
Daniel Stone37816df2012-05-16 18:45:18 +0100785 wl_seat_add_listener(input->seat, &seat_listener, input);
786 wl_seat_set_user_data(input->seat, input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100787}
788
789static void
790display_handle_global(struct wl_display *display, uint32_t id,
791 const char *interface, uint32_t version, void *data)
792{
793 struct wayland_compositor *c = data;
794
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200795 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400796 c->parent.compositor =
797 wl_display_bind(display, id, &wl_compositor_interface);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200798 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400799 c->parent.output =
800 wl_display_bind(display, id, &wl_output_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100801 wl_output_add_listener(c->parent.output, &output_listener, c);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200802 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400803 c->parent.shell =
804 wl_display_bind(display, id, &wl_shell_interface);
Daniel Stone725c2c32012-06-22 14:04:36 +0100805 } else if (strcmp(interface, "wl_seat") == 0) {
806 display_add_seat(c, id);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100807 }
808}
809
810static int
811update_event_mask(uint32_t mask, void *data)
812{
813 struct wayland_compositor *c = data;
814
815 c->parent.event_mask = mask;
816 if (c->parent.wl_source)
817 wl_event_source_fd_update(c->parent.wl_source, mask);
818
819 return 0;
820}
821
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400822static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100823wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
824{
825 struct wayland_compositor *c = data;
826
827 if (mask & WL_EVENT_READABLE)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400828 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_READABLE);
Kristian Høgsbergf258a312011-12-28 22:51:20 -0500829 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400830 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_WRITABLE);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400831
832 return 1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100833}
834
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500835static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -0400836wayland_restore(struct weston_compositor *ec)
837{
838}
839
840static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500841wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500842{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500843 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -0700844
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500845 free(ec);
846}
847
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500848static struct weston_compositor *
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500849wayland_compositor_create(struct wl_display *display,
Daniel Stonebaf43592012-06-01 11:11:10 -0400850 int width, int height, const char *display_name,
Daniel Stonec1be8e52012-06-01 11:14:02 -0400851 int argc, char *argv[], const char *config_file)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100852{
853 struct wayland_compositor *c;
854 struct wl_event_loop *loop;
855 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100856
857 c = malloc(sizeof *c);
858 if (c == NULL)
859 return NULL;
860
861 memset(c, 0, sizeof *c);
862
Daniel Stone725c2c32012-06-22 14:04:36 +0100863 if (weston_compositor_init(&c->base, display, argc, argv,
864 config_file) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200865 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +0100866
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400867 c->parent.wl_display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100868
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400869 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200870 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +0200871 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100872 }
873
874 wl_list_init(&c->input_list);
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400875 wl_display_add_global_listener(c->parent.wl_display,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100876 display_handle_global, c);
877
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400878 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_READABLE);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100879
880 c->base.wl_display = display;
881 if (wayland_compositor_init_egl(c) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200882 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100883
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100884 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -0400885 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100886
Kristian Høgsberg25894fc2012-09-05 22:06:26 -0400887 if (gles2_renderer_init(&c->base) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200888 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100889
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500890 create_border(c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100891 if (wayland_compositor_create_output(c, width, height) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200892 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100893
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100894 loop = wl_display_get_event_loop(c->base.wl_display);
895
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400896 fd = wl_display_get_fd(c->parent.wl_display, update_event_mask, c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100897 c->parent.wl_source =
898 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
899 wayland_compositor_handle_event, c);
900 if (c->parent.wl_source == NULL)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200901 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100902
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100903 return &c->base;
Martin Olssonc5db50f2012-07-08 03:03:43 +0200904
905err_display:
906 wl_display_disconnect(c->parent.wl_display);
907err_compositor:
908 weston_compositor_shutdown(&c->base);
909err_free:
910 free(c);
911 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100912}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400913
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500914WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -0400915backend_init(struct wl_display *display, int argc, char *argv[],
916 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400917{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400918 int width = 1024, height = 640;
919 char *display_name = NULL;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400920
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400921 const struct weston_option wayland_options[] = {
922 { WESTON_OPTION_INTEGER, "width", 0, &width },
923 { WESTON_OPTION_INTEGER, "height", 0, &height },
924 { WESTON_OPTION_STRING, "display", 0, &display_name },
925 };
926
927 parse_options(wayland_options,
928 ARRAY_LENGTH(wayland_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400929
Daniel Stonebaf43592012-06-01 11:11:10 -0400930 return wayland_compositor_create(display, width, height, display_name,
Daniel Stonec1be8e52012-06-01 11:14:02 -0400931 argc, argv, config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400932}