blob: 1f45def9d6937597c18c88eb179759f73a89fa27 [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;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010077
78 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;
Benjamin Franzkebe014562011-02-18 17:04:24 +010083 EGLSurface egl_surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050084 struct weston_mode mode;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010085};
86
87struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -040088 struct weston_seat base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010089 struct wayland_compositor *compositor;
Daniel Stone37816df2012-05-16 18:45:18 +010090 struct wl_seat *seat;
91 struct wl_pointer *pointer;
92 struct wl_keyboard *keyboard;
93 struct wl_touch *touch;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010094 struct wl_list link;
Daniel Stone50692802012-06-22 13:21:41 +010095 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -040096 uint32_t enter_serial;
97 int focus;
98 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010099};
100
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500101
102static int
103texture_border(struct wayland_output *output)
104{
105 struct wayland_compositor *c =
106 (struct wayland_compositor *) output->base.compositor;
107 GLfloat *d;
108 unsigned int *p;
109 int i, j, k, n;
110 GLfloat x[4], y[4], u[4], v[4];
111
112 x[0] = -c->border.left;
113 x[1] = 0;
114 x[2] = output->base.current->width;
115 x[3] = output->base.current->width + c->border.right;
116
117 y[0] = -c->border.top;
118 y[1] = 0;
119 y[2] = output->base.current->height;
120 y[3] = output->base.current->height + c->border.bottom;
121
122 u[0] = 0.0;
123 u[1] = (GLfloat) c->border.left / c->border.width;
124 u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
125 u[3] = 1.0;
126
127 v[0] = 0.0;
128 v[1] = (GLfloat) c->border.top / c->border.height;
129 v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
130 v[3] = 1.0;
131
132 n = 8;
133 d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
134 p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
135
136 k = 0;
137 for (i = 0; i < 3; i++)
138 for (j = 0; j < 3; j++) {
139
140 if (i == 1 && j == 1)
141 continue;
142
143 d[ 0] = x[i];
144 d[ 1] = y[j];
145 d[ 2] = u[i];
146 d[ 3] = v[j];
147
148 d[ 4] = x[i];
149 d[ 5] = y[j + 1];
150 d[ 6] = u[i];
151 d[ 7] = v[j + 1];
152
153 d[ 8] = x[i + 1];
154 d[ 9] = y[j];
155 d[10] = u[i + 1];
156 d[11] = v[j];
157
158 d[12] = x[i + 1];
159 d[13] = y[j + 1];
160 d[14] = u[i + 1];
161 d[15] = v[j + 1];
162
163 p[0] = k + 0;
164 p[1] = k + 1;
165 p[2] = k + 2;
166 p[3] = k + 2;
167 p[4] = k + 1;
168 p[5] = k + 3;
169
170 d += 16;
171 p += 6;
172 k += 4;
173 }
174
175 return k / 4;
176}
177
178static void
179draw_border(struct wayland_output *output)
180{
181 struct wayland_compositor *c =
182 (struct wayland_compositor *) output->base.compositor;
Gwenole Beauchesne6d030492012-04-20 11:15:51 +0200183 struct weston_shader *shader = &c->base.texture_shader_rgba;
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500184 GLfloat *v;
185 int n;
186
187 glDisable(GL_BLEND);
188 glUseProgram(shader->program);
189 c->base.current_shader = shader;
190
191 glUniformMatrix4fv(shader->proj_uniform,
192 1, GL_FALSE, output->base.matrix.d);
193
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200194 glUniform1i(shader->tex_uniforms[0], 0);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500195 glUniform1f(shader->alpha_uniform, 1);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500196
197 n = texture_border(output);
198
199 glBindTexture(GL_TEXTURE_2D, c->border.texture);
200
201 v = c->base.vertices.data;
202 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
203 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
204 glEnableVertexAttribArray(0);
205 glEnableVertexAttribArray(1);
206
207 glDrawElements(GL_TRIANGLES, n * 6,
208 GL_UNSIGNED_INT, c->base.indices.data);
209
210 glDisableVertexAttribArray(1);
211 glDisableVertexAttribArray(0);
212
213 c->base.vertices.size = 0;
214 c->base.indices.size = 0;
215}
216
217static void
218create_border(struct wayland_compositor *c)
219{
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200220 pixman_image_t *image;
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500221
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200222 image = load_image(DATADIR "/weston/border.png");
223 if (!image) {
Martin Minarik6d118362012-06-07 18:01:59 +0200224 weston_log("could'nt load border image\n");
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500225 return;
226 }
227
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200228 c->border.width = pixman_image_get_width(image);
229 c->border.height = pixman_image_get_height(image);
230
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500231 glGenTextures(1, &c->border.texture);
232 glBindTexture(GL_TEXTURE_2D, c->border.texture);
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
235 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
236 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
237
238 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
239 c->border.width,
240 c->border.height,
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200241 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
242 pixman_image_get_data(image));
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500243
244 c->border.top = 25;
245 c->border.bottom = 50;
246 c->border.left = 25;
247 c->border.right = 25;
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200248
249 pixman_image_unref(image);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500250}
251
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100252static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100253wayland_compositor_init_egl(struct wayland_compositor *c)
254{
255 EGLint major, minor;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100256 EGLint n;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100257 EGLint config_attribs[] = {
258 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
259 EGL_RED_SIZE, 1,
260 EGL_GREEN_SIZE, 1,
261 EGL_BLUE_SIZE, 1,
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500262 EGL_ALPHA_SIZE, 1,
Kristian Høgsbergd28ab362011-03-02 11:36:30 -0500263 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100264 EGL_NONE
265 };
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100266 static const EGLint context_attribs[] = {
267 EGL_CONTEXT_CLIENT_VERSION, 2,
268 EGL_NONE
269 };
270
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400271 c->base.egl_display = eglGetDisplay(c->parent.wl_display);
272 if (c->base.egl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200273 weston_log("failed to create display\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100274 return -1;
275 }
276
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400277 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200278 weston_log("failed to initialize display\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100279 return -1;
280 }
281
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100282 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200283 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100284 return -1;
285 }
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400286 if (!eglChooseConfig(c->base.egl_display, config_attribs,
287 &c->base.egl_config, 1, &n) || n == 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200288 weston_log("failed to choose config: %d\n", n);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100289 return -1;
290 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100291
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400292 c->base.egl_context =
293 eglCreateContext(c->base.egl_display, c->base.egl_config,
294 EGL_NO_CONTEXT, context_attribs);
295 if (c->base.egl_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200296 weston_log("failed to create context\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100297 return -1;
298 }
299
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400300 c->dummy_pixmap = wl_egl_pixmap_create(10, 10, 0);
301 if (!c->dummy_pixmap) {
Martin Minarik6d118362012-06-07 18:01:59 +0200302 weston_log("failure to create dummy_pixmap\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400303 return -1;
304 }
305
306 c->dummy_egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400307 eglCreatePixmapSurface(c->base.egl_display, c->base.egl_config,
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400308 c->dummy_pixmap, NULL);
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400309 if (!eglMakeCurrent(c->base.egl_display, c->dummy_egl_surface,
310 c->dummy_egl_surface, c->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200311 weston_log("failed to make context current\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100312 return -1;
313 }
314
315 return 0;
316}
317
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500318static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500319frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400320{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500321 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400322
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500323 wl_callback_destroy(callback);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500324 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400325}
326
327static const struct wl_callback_listener frame_listener = {
328 frame_done
329};
330
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500331static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500332wayland_output_repaint(struct weston_output *output_base,
Pekka Paalanen07c91f82012-08-30 16:47:21 -0500333 pixman_region32_t *damage, int flip)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100334{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100335 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500336 struct wayland_compositor *compositor =
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100337 (struct wayland_compositor *) output->base.compositor;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400338 struct wl_callback *callback;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500339 struct weston_surface *surface;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100340
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400341 if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
342 output->egl_surface,
343 compositor->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200344 weston_log("failed to make current\n");
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500345 return;
346 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100347
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500348 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500349 weston_surface_draw(surface, &output->base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500350
Pekka Paalanen07c91f82012-08-30 16:47:21 -0500351 if (!flip)
352 return;
353
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500354 draw_border(output);
355
Kristian Høgsberge0f832b2012-06-20 00:13:18 -0400356 wl_signal_emit(&output->base.frame_signal, output);
Scott Moreau062be7e2012-04-20 13:37:33 -0600357
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400358 eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400359 callback = wl_surface_frame(output->parent.surface);
360 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100361
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500362 return;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100363}
364
Matt Roper361d2ad2011-08-29 13:52:23 -0700365static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500366wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700367{
368 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500369 struct weston_compositor *ec = output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700370
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400371 eglDestroySurface(ec->egl_display, output->egl_surface);
Matt Roper361d2ad2011-08-29 13:52:23 -0700372 wl_egl_window_destroy(output->parent.egl_window);
373 free(output);
374
375 return;
376}
377
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300378static const struct wl_shell_surface_listener shell_surface_listener;
379
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200380static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100381wayland_compositor_create_output(struct wayland_compositor *c,
382 int width, int height)
383{
384 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100385
386 output = malloc(sizeof *output);
387 if (output == NULL)
388 return -1;
389 memset(output, 0, sizeof *output);
390
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400391 output->mode.flags =
392 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
393 output->mode.width = width;
394 output->mode.height = height;
395 output->mode.refresh = 60;
396 wl_list_init(&output->base.mode_list);
397 wl_list_insert(&output->base.mode_list, &output->mode.link);
398
399 output->base.current = &output->mode;
Scott Moreau1bad5db2012-08-18 01:04:05 -0600400 weston_output_init(&output->base, &c->base, 0, 0, width, height,
401 WL_OUTPUT_TRANSFORM_NORMAL);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400402
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500403 output->base.border.top = c->border.top;
404 output->base.border.bottom = c->border.bottom;
405 output->base.border.left = c->border.left;
406 output->base.border.right = c->border.right;
Kristian Høgsberg90bc88c2012-08-13 23:34:04 -0400407 output->base.make = "waywayland";
408 output->base.model = "none";
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500409
410 weston_output_move(&output->base, 0, 0);
411
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100412 output->parent.surface =
413 wl_compositor_create_surface(c->parent.compositor);
414 wl_surface_set_user_data(output->parent.surface, output);
415
Benjamin Franzkebe014562011-02-18 17:04:24 +0100416 output->parent.egl_window =
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500417 wl_egl_window_create(output->parent.surface,
418 width + c->border.left + c->border.right,
419 height + c->border.top + c->border.bottom);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100420 if (!output->parent.egl_window) {
Martin Minarik6d118362012-06-07 18:01:59 +0200421 weston_log("failure to create wl_egl_window\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100422 goto cleanup_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100423 }
424
Benjamin Franzkebe014562011-02-18 17:04:24 +0100425 output->egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400426 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100427 output->parent.egl_window, NULL);
428 if (!output->egl_surface) {
Martin Minarik6d118362012-06-07 18:01:59 +0200429 weston_log("failed to create window surface\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100430 goto cleanup_window;
431 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100432
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400433 if (!eglMakeCurrent(c->base.egl_display, output->egl_surface,
434 output->egl_surface, c->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200435 weston_log("failed to make surface current\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100436 goto cleanup_surface;
437 return -1;
438 }
439
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200440 output->parent.shell_surface =
441 wl_shell_get_shell_surface(c->parent.shell,
442 output->parent.surface);
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300443 wl_shell_surface_add_listener(output->parent.shell_surface,
444 &shell_surface_listener, output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200445 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100446
Alex Wubd3354b2012-04-17 17:20:49 +0800447 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500448 output->base.repaint = wayland_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700449 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800450 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200451 output->base.set_backlight = NULL;
452 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800453 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100454
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100455 wl_list_insert(c->base.output_list.prev, &output->base.link);
456
457 return 0;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100458
459cleanup_surface:
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400460 eglDestroySurface(c->base.egl_display, output->egl_surface);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100461cleanup_window:
462 wl_egl_window_destroy(output->parent.egl_window);
463cleanup_output:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500464 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +0100465 free(output);
466
467 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100468}
469
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300470static void
471shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
472 uint32_t serial)
473{
474 wl_shell_surface_pong(shell_surface, serial);
475}
476
477static void
478shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
479 uint32_t edges, int32_t width, int32_t height)
480{
481 /* FIXME: implement resizing */
482}
483
484static void
485shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
486{
487}
488
489static const struct wl_shell_surface_listener shell_surface_listener = {
490 shell_surface_ping,
491 shell_surface_configure,
492 shell_surface_popup_done
493};
494
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100495/* Events received from the wayland-server this compositor is client of: */
496
497/* parent output interface */
498static void
499display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400500 struct wl_output *wl_output,
501 int x,
502 int y,
503 int physical_width,
504 int physical_height,
505 int subpixel,
506 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -0400507 const char *model,
508 int transform)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100509{
510 struct wayland_compositor *c = data;
511
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500512 c->parent.screen_allocation.x = x;
513 c->parent.screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400514}
515
516static void
517display_handle_mode(void *data,
518 struct wl_output *wl_output,
519 uint32_t flags,
520 int width,
521 int height,
522 int refresh)
523{
524 struct wayland_compositor *c = data;
525
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100526 c->parent.screen_allocation.width = width;
527 c->parent.screen_allocation.height = height;
528}
529
530static const struct wl_output_listener output_listener = {
531 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400532 display_handle_mode
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100533};
534
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400535static void
536check_focus(struct wayland_input *input, wl_fixed_t x, wl_fixed_t y)
537{
538 struct wayland_compositor *c = input->compositor;
539 int width, height, inside;
540
541 width = input->output->mode.width;
542 height = input->output->mode.height;
543
544 inside = c->border.left <= wl_fixed_to_int(x) &&
545 wl_fixed_to_int(x) < width + c->border.left &&
546 c->border.top <= wl_fixed_to_int(y) &&
547 wl_fixed_to_int(y) < height + c->border.top;
548
549 if (!input->focus && inside) {
550 notify_pointer_focus(&input->base, &input->output->base,
551 x - wl_fixed_from_int(c->border.left),
552 y = wl_fixed_from_int(c->border.top));
553 wl_pointer_set_cursor(input->pointer,
554 input->enter_serial, NULL, 0, 0);
555 } else if (input->focus && !inside) {
556 notify_pointer_focus(&input->base, NULL, 0, 0);
557 /* FIXME: Should set default cursor here. */
558 }
559
560 input->focus = inside;
561}
562
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100563/* parent input interface */
564static void
Daniel Stone37816df2012-05-16 18:45:18 +0100565input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
566 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400567 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100568{
569 struct wayland_input *input = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100570
Daniel Stone50692802012-06-22 13:21:41 +0100571 /* XXX: If we get a modifier event immediately before the focus,
572 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400573 input->enter_serial = serial;
574 input->output = wl_surface_get_user_data(surface);
575 check_focus(input, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100576}
577
578static void
Daniel Stone37816df2012-05-16 18:45:18 +0100579input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
580 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500581{
582 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500583
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400584 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400585 input->output = NULL;
586 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500587}
588
589static void
Daniel Stone37816df2012-05-16 18:45:18 +0100590input_handle_motion(void *data, struct wl_pointer *pointer,
591 uint32_t time, wl_fixed_t x, wl_fixed_t y)
592{
593 struct wayland_input *input = data;
594 struct wayland_compositor *c = input->compositor;
595
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400596 check_focus(input, x, y);
597 if (input->focus)
598 notify_motion(&input->base, time,
599 x - wl_fixed_from_int(c->border.left),
600 y - wl_fixed_from_int(c->border.top));
Daniel Stone37816df2012-05-16 18:45:18 +0100601}
602
603static void
604input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100605 uint32_t serial, uint32_t time, uint32_t button,
606 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +0100607{
608 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100609 enum wl_pointer_button_state state = state_w;
Daniel Stone37816df2012-05-16 18:45:18 +0100610
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400611 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +0100612}
613
614static void
615input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100616 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +0100617{
618 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100619
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400620 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +0100621}
622
623static const struct wl_pointer_listener pointer_listener = {
624 input_handle_pointer_enter,
625 input_handle_pointer_leave,
626 input_handle_motion,
627 input_handle_button,
628 input_handle_axis,
629};
630
631static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100632input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
633 int fd, uint32_t size)
634{
635 struct wayland_input *input = data;
636 struct xkb_keymap *keymap;
637 char *map_str;
638
639 if (!data) {
640 close(fd);
641 return;
642 }
643
644 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
645 close(fd);
646 return;
647 }
648
649 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
650 if (map_str == MAP_FAILED) {
651 close(fd);
652 return;
653 }
654
655 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
656 map_str,
657 XKB_KEYMAP_FORMAT_TEXT_V1,
658 0);
659 munmap(map_str, size);
660 close(fd);
661
662 if (!keymap) {
Martin Minarik6d118362012-06-07 18:01:59 +0200663 weston_log("failed to compile keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100664 return;
665 }
666
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400667 weston_seat_init_keyboard(&input->base, keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100668 xkb_map_unref(keymap);
669}
670
671static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500672input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100673 struct wl_keyboard *keyboard,
674 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500675 struct wl_surface *surface,
676 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100677{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500678 struct wayland_input *input = data;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500679
Daniel Stone50692802012-06-22 13:21:41 +0100680 /* XXX: If we get a modifier event immediately before the focus,
681 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400682 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +0100683 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500684}
685
686static void
687input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100688 struct wl_keyboard *keyboard,
689 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500690 struct wl_surface *surface)
691{
692 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500693
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400694 notify_keyboard_focus_out(&input->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100695}
696
Daniel Stone37816df2012-05-16 18:45:18 +0100697static void
698input_handle_key(void *data, struct wl_keyboard *keyboard,
699 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
700{
701 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100702
Daniel Stone50692802012-06-22 13:21:41 +0100703 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400704 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100705 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100706 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stone50692802012-06-22 13:21:41 +0100707 STATE_UPDATE_NONE);
Daniel Stone37816df2012-05-16 18:45:18 +0100708}
709
Daniel Stone351eb612012-05-31 15:27:47 -0400710static void
711input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +0100712 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -0400713 uint32_t mods_latched, uint32_t mods_locked,
714 uint32_t group)
715{
Daniel Stone50692802012-06-22 13:21:41 +0100716 struct wayland_input *input = data;
717 struct wayland_compositor *c = input->compositor;
718 uint32_t serial_out;
719
720 /* If we get a key event followed by a modifier event with the
721 * same serial number, then we try to preserve those semantics by
722 * reusing the same serial number on the way out too. */
723 if (serial_in == input->key_serial)
724 serial_out = wl_display_get_serial(c->base.wl_display);
725 else
726 serial_out = wl_display_next_serial(c->base.wl_display);
727
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400728 xkb_state_update_mask(input->base.xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +0100729 mods_depressed, mods_latched,
730 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400731 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -0400732}
733
Daniel Stone37816df2012-05-16 18:45:18 +0100734static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100735 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500736 input_handle_keyboard_enter,
737 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +0100738 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -0400739 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100740};
741
742static void
Daniel Stone37816df2012-05-16 18:45:18 +0100743input_handle_capabilities(void *data, struct wl_seat *seat,
744 enum wl_seat_capability caps)
745{
746 struct wayland_input *input = data;
747
748 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
749 input->pointer = wl_seat_get_pointer(seat);
750 wl_pointer_set_user_data(input->pointer, input);
751 wl_pointer_add_listener(input->pointer, &pointer_listener,
752 input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400753 weston_seat_init_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +0100754 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
755 wl_pointer_destroy(input->pointer);
756 input->pointer = NULL;
757 }
758
759 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
760 input->keyboard = wl_seat_get_keyboard(seat);
761 wl_keyboard_set_user_data(input->keyboard, input);
762 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
763 input);
764 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
765 wl_keyboard_destroy(input->keyboard);
766 input->keyboard = NULL;
767 }
768}
769
770static const struct wl_seat_listener seat_listener = {
771 input_handle_capabilities,
772};
773
774static void
775display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100776{
777 struct wayland_input *input;
778
779 input = malloc(sizeof *input);
780 if (input == NULL)
781 return;
782
783 memset(input, 0, sizeof *input);
784
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400785 weston_seat_init(&input->base, &c->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100786 input->compositor = c;
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400787 input->seat = wl_display_bind(c->parent.wl_display, id,
Daniel Stone37816df2012-05-16 18:45:18 +0100788 &wl_seat_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100789 wl_list_insert(c->input_list.prev, &input->link);
790
Daniel Stone37816df2012-05-16 18:45:18 +0100791 wl_seat_add_listener(input->seat, &seat_listener, input);
792 wl_seat_set_user_data(input->seat, input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100793}
794
795static void
796display_handle_global(struct wl_display *display, uint32_t id,
797 const char *interface, uint32_t version, void *data)
798{
799 struct wayland_compositor *c = data;
800
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200801 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400802 c->parent.compositor =
803 wl_display_bind(display, id, &wl_compositor_interface);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200804 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400805 c->parent.output =
806 wl_display_bind(display, id, &wl_output_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100807 wl_output_add_listener(c->parent.output, &output_listener, c);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200808 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400809 c->parent.shell =
810 wl_display_bind(display, id, &wl_shell_interface);
Daniel Stone725c2c32012-06-22 14:04:36 +0100811 } else if (strcmp(interface, "wl_seat") == 0) {
812 display_add_seat(c, id);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100813 }
814}
815
816static int
817update_event_mask(uint32_t mask, void *data)
818{
819 struct wayland_compositor *c = data;
820
821 c->parent.event_mask = mask;
822 if (c->parent.wl_source)
823 wl_event_source_fd_update(c->parent.wl_source, mask);
824
825 return 0;
826}
827
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400828static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100829wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
830{
831 struct wayland_compositor *c = data;
832
833 if (mask & WL_EVENT_READABLE)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400834 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_READABLE);
Kristian Høgsbergf258a312011-12-28 22:51:20 -0500835 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400836 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_WRITABLE);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400837
838 return 1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100839}
840
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500841static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -0400842wayland_restore(struct weston_compositor *ec)
843{
844}
845
846static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500847wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500848{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500849 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -0700850
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500851 free(ec);
852}
853
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500854static struct weston_compositor *
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500855wayland_compositor_create(struct wl_display *display,
Daniel Stonebaf43592012-06-01 11:11:10 -0400856 int width, int height, const char *display_name,
Daniel Stonec1be8e52012-06-01 11:14:02 -0400857 int argc, char *argv[], const char *config_file)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100858{
859 struct wayland_compositor *c;
860 struct wl_event_loop *loop;
861 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100862
863 c = malloc(sizeof *c);
864 if (c == NULL)
865 return NULL;
866
867 memset(c, 0, sizeof *c);
868
Daniel Stone725c2c32012-06-22 14:04:36 +0100869 if (weston_compositor_init(&c->base, display, argc, argv,
870 config_file) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200871 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +0100872
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400873 c->parent.wl_display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100874
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400875 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200876 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +0200877 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100878 }
879
880 wl_list_init(&c->input_list);
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400881 wl_display_add_global_listener(c->parent.wl_display,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100882 display_handle_global, c);
883
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400884 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_READABLE);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100885
886 c->base.wl_display = display;
887 if (wayland_compositor_init_egl(c) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200888 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100889
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100890 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -0400891 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100892
Daniel Stone725c2c32012-06-22 14:04:36 +0100893 if (weston_compositor_init_gl(&c->base) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200894 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100895
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500896 create_border(c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100897 if (wayland_compositor_create_output(c, width, height) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200898 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100899
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100900 loop = wl_display_get_event_loop(c->base.wl_display);
901
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400902 fd = wl_display_get_fd(c->parent.wl_display, update_event_mask, c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100903 c->parent.wl_source =
904 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
905 wayland_compositor_handle_event, c);
906 if (c->parent.wl_source == NULL)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200907 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100908
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100909 return &c->base;
Martin Olssonc5db50f2012-07-08 03:03:43 +0200910
911err_display:
912 wl_display_disconnect(c->parent.wl_display);
913err_compositor:
914 weston_compositor_shutdown(&c->base);
915err_free:
916 free(c);
917 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100918}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400919
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500920WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -0400921backend_init(struct wl_display *display, int argc, char *argv[],
922 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400923{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400924 int width = 1024, height = 640;
925 char *display_name = NULL;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400926
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400927 const struct weston_option wayland_options[] = {
928 { WESTON_OPTION_INTEGER, "width", 0, &width },
929 { WESTON_OPTION_INTEGER, "height", 0, &height },
930 { WESTON_OPTION_STRING, "display", 0, &display_name },
931 };
932
933 parse_options(wayland_options,
934 ARRAY_LENGTH(wayland_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400935
Daniel Stonebaf43592012-06-01 11:11:10 -0400936 return wayland_compositor_create(display, width, height, display_name,
Daniel Stonec1be8e52012-06-01 11:14:02 -0400937 argc, argv, config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400938}