blob: ee3def1ee1cfdf6c216a68e521e6612c6ddddbb1 [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"
Martin Minarik6d118362012-06-07 18:01:59 +020045#include "log.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010046
47struct wayland_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050048 struct weston_compositor base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010049
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -040050 struct wl_egl_pixmap *dummy_pixmap;
Kristian Høgsbergb71302e2012-05-10 12:28:35 -040051 EGLSurface dummy_egl_surface;
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -040052
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010053 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040054 struct wl_display *wl_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010055 struct wl_compositor *compositor;
56 struct wl_shell *shell;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010057 struct wl_output *output;
58
59 struct {
60 int32_t x, y, width, height;
61 } screen_allocation;
62
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010063 struct wl_event_source *wl_source;
64 uint32_t event_mask;
65 } parent;
66
Kristian Høgsberg546a8122012-02-01 07:45:51 -050067 struct {
68 int32_t top, bottom, left, right;
69 GLuint texture;
70 int32_t width, height;
71 } border;
72
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010073 struct wl_list input_list;
74};
75
76struct wayland_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050077 struct weston_output base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010078
79 struct {
80 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020081 struct wl_shell_surface *shell_surface;
Benjamin Franzkebe014562011-02-18 17:04:24 +010082 struct wl_egl_window *egl_window;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010083 } parent;
Benjamin Franzkebe014562011-02-18 17:04:24 +010084 EGLSurface egl_surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050085 struct weston_mode mode;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010086};
87
88struct wayland_input {
89 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;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010096};
97
Kristian Høgsberg546a8122012-02-01 07:45:51 -050098
99static int
100texture_border(struct wayland_output *output)
101{
102 struct wayland_compositor *c =
103 (struct wayland_compositor *) output->base.compositor;
104 GLfloat *d;
105 unsigned int *p;
106 int i, j, k, n;
107 GLfloat x[4], y[4], u[4], v[4];
108
109 x[0] = -c->border.left;
110 x[1] = 0;
111 x[2] = output->base.current->width;
112 x[3] = output->base.current->width + c->border.right;
113
114 y[0] = -c->border.top;
115 y[1] = 0;
116 y[2] = output->base.current->height;
117 y[3] = output->base.current->height + c->border.bottom;
118
119 u[0] = 0.0;
120 u[1] = (GLfloat) c->border.left / c->border.width;
121 u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
122 u[3] = 1.0;
123
124 v[0] = 0.0;
125 v[1] = (GLfloat) c->border.top / c->border.height;
126 v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
127 v[3] = 1.0;
128
129 n = 8;
130 d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
131 p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
132
133 k = 0;
134 for (i = 0; i < 3; i++)
135 for (j = 0; j < 3; j++) {
136
137 if (i == 1 && j == 1)
138 continue;
139
140 d[ 0] = x[i];
141 d[ 1] = y[j];
142 d[ 2] = u[i];
143 d[ 3] = v[j];
144
145 d[ 4] = x[i];
146 d[ 5] = y[j + 1];
147 d[ 6] = u[i];
148 d[ 7] = v[j + 1];
149
150 d[ 8] = x[i + 1];
151 d[ 9] = y[j];
152 d[10] = u[i + 1];
153 d[11] = v[j];
154
155 d[12] = x[i + 1];
156 d[13] = y[j + 1];
157 d[14] = u[i + 1];
158 d[15] = v[j + 1];
159
160 p[0] = k + 0;
161 p[1] = k + 1;
162 p[2] = k + 2;
163 p[3] = k + 2;
164 p[4] = k + 1;
165 p[5] = k + 3;
166
167 d += 16;
168 p += 6;
169 k += 4;
170 }
171
172 return k / 4;
173}
174
175static void
176draw_border(struct wayland_output *output)
177{
178 struct wayland_compositor *c =
179 (struct wayland_compositor *) output->base.compositor;
Gwenole Beauchesne6d030492012-04-20 11:15:51 +0200180 struct weston_shader *shader = &c->base.texture_shader_rgba;
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500181 GLfloat *v;
182 int n;
183
184 glDisable(GL_BLEND);
185 glUseProgram(shader->program);
186 c->base.current_shader = shader;
187
188 glUniformMatrix4fv(shader->proj_uniform,
189 1, GL_FALSE, output->base.matrix.d);
190
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200191 glUniform1i(shader->tex_uniforms[0], 0);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500192 glUniform1f(shader->alpha_uniform, 1);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500193 glUniform1f(shader->texwidth_uniform, 1);
194
195 n = texture_border(output);
196
197 glBindTexture(GL_TEXTURE_2D, c->border.texture);
198
199 v = c->base.vertices.data;
200 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
201 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
202 glEnableVertexAttribArray(0);
203 glEnableVertexAttribArray(1);
204
205 glDrawElements(GL_TRIANGLES, n * 6,
206 GL_UNSIGNED_INT, c->base.indices.data);
207
208 glDisableVertexAttribArray(1);
209 glDisableVertexAttribArray(0);
210
211 c->base.vertices.size = 0;
212 c->base.indices.size = 0;
213}
214
215static void
216create_border(struct wayland_compositor *c)
217{
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200218 pixman_image_t *image;
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500219
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200220 image = load_image(DATADIR "/weston/border.png");
221 if (!image) {
Martin Minarik6d118362012-06-07 18:01:59 +0200222 weston_log("could'nt load border image\n");
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500223 return;
224 }
225
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200226 c->border.width = pixman_image_get_width(image);
227 c->border.height = pixman_image_get_height(image);
228
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500229 glGenTextures(1, &c->border.texture);
230 glBindTexture(GL_TEXTURE_2D, c->border.texture);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
235
236 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
237 c->border.width,
238 c->border.height,
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200239 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
240 pixman_image_get_data(image));
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500241
242 c->border.top = 25;
243 c->border.bottom = 50;
244 c->border.left = 25;
245 c->border.right = 25;
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200246
247 pixman_image_unref(image);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500248}
249
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100250static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100251wayland_compositor_init_egl(struct wayland_compositor *c)
252{
253 EGLint major, minor;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100254 EGLint n;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100255 EGLint config_attribs[] = {
256 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
257 EGL_RED_SIZE, 1,
258 EGL_GREEN_SIZE, 1,
259 EGL_BLUE_SIZE, 1,
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500260 EGL_ALPHA_SIZE, 1,
Kristian Høgsbergd28ab362011-03-02 11:36:30 -0500261 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100262 EGL_NONE
263 };
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100264 static const EGLint context_attribs[] = {
265 EGL_CONTEXT_CLIENT_VERSION, 2,
266 EGL_NONE
267 };
268
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400269 c->base.egl_display = eglGetDisplay(c->parent.wl_display);
270 if (c->base.egl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200271 weston_log("failed to create display\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100272 return -1;
273 }
274
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400275 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200276 weston_log("failed to initialize display\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100277 return -1;
278 }
279
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100280 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200281 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100282 return -1;
283 }
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400284 if (!eglChooseConfig(c->base.egl_display, config_attribs,
285 &c->base.egl_config, 1, &n) || n == 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200286 weston_log("failed to choose config: %d\n", n);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100287 return -1;
288 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100289
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400290 c->base.egl_context =
291 eglCreateContext(c->base.egl_display, c->base.egl_config,
292 EGL_NO_CONTEXT, context_attribs);
293 if (c->base.egl_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("failed to create context\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100295 return -1;
296 }
297
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400298 c->dummy_pixmap = wl_egl_pixmap_create(10, 10, 0);
299 if (!c->dummy_pixmap) {
Martin Minarik6d118362012-06-07 18:01:59 +0200300 weston_log("failure to create dummy_pixmap\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400301 return -1;
302 }
303
304 c->dummy_egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400305 eglCreatePixmapSurface(c->base.egl_display, c->base.egl_config,
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400306 c->dummy_pixmap, NULL);
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400307 if (!eglMakeCurrent(c->base.egl_display, c->dummy_egl_surface,
308 c->dummy_egl_surface, c->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200309 weston_log("failed to make context current\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100310 return -1;
311 }
312
313 return 0;
314}
315
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500316static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500317frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400318{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500319 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400320
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500321 wl_callback_destroy(callback);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500322 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400323}
324
325static const struct wl_callback_listener frame_listener = {
326 frame_done
327};
328
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500329static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500330wayland_output_repaint(struct weston_output *output_base,
331 pixman_region32_t *damage)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100332{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100333 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500334 struct wayland_compositor *compositor =
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100335 (struct wayland_compositor *) output->base.compositor;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400336 struct wl_callback *callback;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500337 struct weston_surface *surface;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100338
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400339 if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
340 output->egl_surface,
341 compositor->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200342 weston_log("failed to make current\n");
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500343 return;
344 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100345
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500346 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500347 weston_surface_draw(surface, &output->base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500348
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500349 draw_border(output);
350
Kristian Høgsberge0f832b2012-06-20 00:13:18 -0400351 wl_signal_emit(&output->base.frame_signal, output);
Scott Moreau062be7e2012-04-20 13:37:33 -0600352
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400353 eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400354 callback = wl_surface_frame(output->parent.surface);
355 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100356
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500357 return;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100358}
359
Matt Roper361d2ad2011-08-29 13:52:23 -0700360static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500361wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700362{
363 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500364 struct weston_compositor *ec = output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700365
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400366 eglDestroySurface(ec->egl_display, output->egl_surface);
Matt Roper361d2ad2011-08-29 13:52:23 -0700367 wl_egl_window_destroy(output->parent.egl_window);
368 free(output);
369
370 return;
371}
372
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300373static const struct wl_shell_surface_listener shell_surface_listener;
374
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200375static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100376wayland_compositor_create_output(struct wayland_compositor *c,
377 int width, int height)
378{
379 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100380
381 output = malloc(sizeof *output);
382 if (output == NULL)
383 return -1;
384 memset(output, 0, sizeof *output);
385
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400386 output->mode.flags =
387 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
388 output->mode.width = width;
389 output->mode.height = height;
390 output->mode.refresh = 60;
391 wl_list_init(&output->base.mode_list);
392 wl_list_insert(&output->base.mode_list, &output->mode.link);
393
394 output->base.current = &output->mode;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500395 weston_output_init(&output->base, &c->base, 0, 0, width, height,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100396 WL_OUTPUT_FLIPPED);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400397
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500398 output->base.border.top = c->border.top;
399 output->base.border.bottom = c->border.bottom;
400 output->base.border.left = c->border.left;
401 output->base.border.right = c->border.right;
402
403 weston_output_move(&output->base, 0, 0);
404
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100405 output->parent.surface =
406 wl_compositor_create_surface(c->parent.compositor);
407 wl_surface_set_user_data(output->parent.surface, output);
408
Benjamin Franzkebe014562011-02-18 17:04:24 +0100409 output->parent.egl_window =
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500410 wl_egl_window_create(output->parent.surface,
411 width + c->border.left + c->border.right,
412 height + c->border.top + c->border.bottom);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100413 if (!output->parent.egl_window) {
Martin Minarik6d118362012-06-07 18:01:59 +0200414 weston_log("failure to create wl_egl_window\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100415 goto cleanup_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100416 }
417
Benjamin Franzkebe014562011-02-18 17:04:24 +0100418 output->egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400419 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100420 output->parent.egl_window, NULL);
421 if (!output->egl_surface) {
Martin Minarik6d118362012-06-07 18:01:59 +0200422 weston_log("failed to create window surface\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100423 goto cleanup_window;
424 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100425
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400426 if (!eglMakeCurrent(c->base.egl_display, output->egl_surface,
427 output->egl_surface, c->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200428 weston_log("failed to make surface current\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100429 goto cleanup_surface;
430 return -1;
431 }
432
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200433 output->parent.shell_surface =
434 wl_shell_get_shell_surface(c->parent.shell,
435 output->parent.surface);
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300436 wl_shell_surface_add_listener(output->parent.shell_surface,
437 &shell_surface_listener, output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200438 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100439
Alex Wubd3354b2012-04-17 17:20:49 +0800440 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500441 output->base.repaint = wayland_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700442 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800443 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200444 output->base.set_backlight = NULL;
445 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800446 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100447
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100448 wl_list_insert(c->base.output_list.prev, &output->base.link);
449
450 return 0;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100451
452cleanup_surface:
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400453 eglDestroySurface(c->base.egl_display, output->egl_surface);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100454cleanup_window:
455 wl_egl_window_destroy(output->parent.egl_window);
456cleanup_output:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500457 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +0100458 free(output);
459
460 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100461}
462
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300463static void
464shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
465 uint32_t serial)
466{
467 wl_shell_surface_pong(shell_surface, serial);
468}
469
470static void
471shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
472 uint32_t edges, int32_t width, int32_t height)
473{
474 /* FIXME: implement resizing */
475}
476
477static void
478shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
479{
480}
481
482static const struct wl_shell_surface_listener shell_surface_listener = {
483 shell_surface_ping,
484 shell_surface_configure,
485 shell_surface_popup_done
486};
487
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100488/* Events received from the wayland-server this compositor is client of: */
489
490/* parent output interface */
491static void
492display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400493 struct wl_output *wl_output,
494 int x,
495 int y,
496 int physical_width,
497 int physical_height,
498 int subpixel,
499 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -0400500 const char *model,
501 int transform)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100502{
503 struct wayland_compositor *c = data;
504
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500505 c->parent.screen_allocation.x = x;
506 c->parent.screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400507}
508
509static void
510display_handle_mode(void *data,
511 struct wl_output *wl_output,
512 uint32_t flags,
513 int width,
514 int height,
515 int refresh)
516{
517 struct wayland_compositor *c = data;
518
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100519 c->parent.screen_allocation.width = width;
520 c->parent.screen_allocation.height = height;
521}
522
523static const struct wl_output_listener output_listener = {
524 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400525 display_handle_mode
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100526};
527
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100528/* parent input interface */
529static void
Daniel Stone37816df2012-05-16 18:45:18 +0100530input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
531 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400532 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100533{
534 struct wayland_input *input = data;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500535 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100536 struct wayland_compositor *c = input->compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100537
Daniel Stone50692802012-06-22 13:21:41 +0100538 /* XXX: If we get a modifier event immediately before the focus,
539 * we should try to keep the same serial. */
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500540 output = wl_surface_get_user_data(surface);
Daniel Stone37816df2012-05-16 18:45:18 +0100541 notify_pointer_focus(&c->base.seat->seat, &output->base, x, y);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300542 wl_pointer_set_cursor(input->pointer, serial, NULL, 0, 0);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100543}
544
545static void
Daniel Stone37816df2012-05-16 18:45:18 +0100546input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
547 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500548{
549 struct wayland_input *input = data;
550 struct wayland_compositor *c = input->compositor;
551
Daniel Stone37816df2012-05-16 18:45:18 +0100552 notify_pointer_focus(&c->base.seat->seat, NULL, 0, 0);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500553}
554
555static void
Daniel Stone37816df2012-05-16 18:45:18 +0100556input_handle_motion(void *data, struct wl_pointer *pointer,
557 uint32_t time, wl_fixed_t x, wl_fixed_t y)
558{
559 struct wayland_input *input = data;
560 struct wayland_compositor *c = input->compositor;
561
562 notify_motion(&c->base.seat->seat, time,
563 x - wl_fixed_from_int(c->border.left),
564 y - wl_fixed_from_int(c->border.top));
565}
566
567static void
568input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100569 uint32_t serial, uint32_t time, uint32_t button,
570 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +0100571{
572 struct wayland_input *input = data;
573 struct wayland_compositor *c = input->compositor;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100574 enum wl_pointer_button_state state = state_w;
Daniel Stone37816df2012-05-16 18:45:18 +0100575
576 notify_button(&c->base.seat->seat, time, button, state);
577}
578
579static void
580input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100581 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +0100582{
583 struct wayland_input *input = data;
584 struct wayland_compositor *c = input->compositor;
585
Daniel Stone2fce4022012-05-30 16:32:00 +0100586 notify_axis(&c->base.seat->seat, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +0100587}
588
589static const struct wl_pointer_listener pointer_listener = {
590 input_handle_pointer_enter,
591 input_handle_pointer_leave,
592 input_handle_motion,
593 input_handle_button,
594 input_handle_axis,
595};
596
597static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100598input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
599 int fd, uint32_t size)
600{
601 struct wayland_input *input = data;
602 struct xkb_keymap *keymap;
603 char *map_str;
604
605 if (!data) {
606 close(fd);
607 return;
608 }
609
610 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
611 close(fd);
612 return;
613 }
614
615 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
616 if (map_str == MAP_FAILED) {
617 close(fd);
618 return;
619 }
620
621 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
622 map_str,
623 XKB_KEYMAP_FORMAT_TEXT_V1,
624 0);
625 munmap(map_str, size);
626 close(fd);
627
628 if (!keymap) {
Martin Minarik6d118362012-06-07 18:01:59 +0200629 weston_log("failed to compile keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100630 return;
631 }
632
633 weston_seat_init_keyboard(input->compositor->base.seat, keymap);
634 xkb_map_unref(keymap);
635}
636
637static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500638input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100639 struct wl_keyboard *keyboard,
640 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500641 struct wl_surface *surface,
642 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100643{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500644 struct wayland_input *input = data;
645 struct wayland_compositor *c = input->compositor;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500646
Daniel Stone50692802012-06-22 13:21:41 +0100647 /* XXX: If we get a modifier event immediately before the focus,
648 * we should try to keep the same serial. */
Daniel Stoned6da09e2012-06-22 13:21:29 +0100649 notify_keyboard_focus_in(&c->base.seat->seat, keys,
650 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500651}
652
653static void
654input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100655 struct wl_keyboard *keyboard,
656 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500657 struct wl_surface *surface)
658{
659 struct wayland_input *input = data;
660 struct wayland_compositor *c = input->compositor;
661
Daniel Stoned6da09e2012-06-22 13:21:29 +0100662 notify_keyboard_focus_out(&c->base.seat->seat);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100663}
664
Daniel Stone37816df2012-05-16 18:45:18 +0100665static void
666input_handle_key(void *data, struct wl_keyboard *keyboard,
667 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
668{
669 struct wayland_input *input = data;
670 struct wayland_compositor *c = input->compositor;
671
Daniel Stone50692802012-06-22 13:21:41 +0100672 input->key_serial = serial;
Daniel Stonec9785ea2012-05-30 16:31:52 +0100673 notify_key(&c->base.seat->seat, time, key,
674 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100675 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stone50692802012-06-22 13:21:41 +0100676 STATE_UPDATE_NONE);
Daniel Stone37816df2012-05-16 18:45:18 +0100677}
678
Daniel Stone351eb612012-05-31 15:27:47 -0400679static void
680input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +0100681 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -0400682 uint32_t mods_latched, uint32_t mods_locked,
683 uint32_t group)
684{
Daniel Stone50692802012-06-22 13:21:41 +0100685 struct wayland_input *input = data;
686 struct wayland_compositor *c = input->compositor;
687 uint32_t serial_out;
688
689 /* If we get a key event followed by a modifier event with the
690 * same serial number, then we try to preserve those semantics by
691 * reusing the same serial number on the way out too. */
692 if (serial_in == input->key_serial)
693 serial_out = wl_display_get_serial(c->base.wl_display);
694 else
695 serial_out = wl_display_next_serial(c->base.wl_display);
696
697 xkb_state_update_mask(c->base.seat->xkb_state.state,
698 mods_depressed, mods_latched,
699 mods_locked, 0, 0, group);
700 notify_modifiers(&c->base.seat->seat, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -0400701}
702
Daniel Stone37816df2012-05-16 18:45:18 +0100703static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100704 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500705 input_handle_keyboard_enter,
706 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +0100707 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -0400708 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100709};
710
711static void
Daniel Stone37816df2012-05-16 18:45:18 +0100712input_handle_capabilities(void *data, struct wl_seat *seat,
713 enum wl_seat_capability caps)
714{
715 struct wayland_input *input = data;
716
717 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
718 input->pointer = wl_seat_get_pointer(seat);
719 wl_pointer_set_user_data(input->pointer, input);
720 wl_pointer_add_listener(input->pointer, &pointer_listener,
721 input);
Daniel Stonecd981dd2012-06-01 12:14:00 +0100722 weston_seat_init_pointer(input->compositor->base.seat);
Daniel Stone37816df2012-05-16 18:45:18 +0100723 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
724 wl_pointer_destroy(input->pointer);
725 input->pointer = NULL;
726 }
727
728 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
729 input->keyboard = wl_seat_get_keyboard(seat);
730 wl_keyboard_set_user_data(input->keyboard, input);
731 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
732 input);
733 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
734 wl_keyboard_destroy(input->keyboard);
735 input->keyboard = NULL;
736 }
737}
738
739static const struct wl_seat_listener seat_listener = {
740 input_handle_capabilities,
741};
742
743static void
744display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100745{
746 struct wayland_input *input;
747
748 input = malloc(sizeof *input);
749 if (input == NULL)
750 return;
751
752 memset(input, 0, sizeof *input);
753
754 input->compositor = c;
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400755 input->seat = wl_display_bind(c->parent.wl_display, id,
Daniel Stone37816df2012-05-16 18:45:18 +0100756 &wl_seat_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100757 wl_list_insert(c->input_list.prev, &input->link);
758
Daniel Stone37816df2012-05-16 18:45:18 +0100759 wl_seat_add_listener(input->seat, &seat_listener, input);
760 wl_seat_set_user_data(input->seat, input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100761}
762
763static void
764display_handle_global(struct wl_display *display, uint32_t id,
765 const char *interface, uint32_t version, void *data)
766{
767 struct wayland_compositor *c = data;
768
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200769 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400770 c->parent.compositor =
771 wl_display_bind(display, id, &wl_compositor_interface);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200772 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400773 c->parent.output =
774 wl_display_bind(display, id, &wl_output_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100775 wl_output_add_listener(c->parent.output, &output_listener, c);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200776 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400777 c->parent.shell =
778 wl_display_bind(display, id, &wl_shell_interface);
Daniel Stone725c2c32012-06-22 14:04:36 +0100779 } else if (strcmp(interface, "wl_seat") == 0) {
780 display_add_seat(c, id);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100781 }
782}
783
784static int
785update_event_mask(uint32_t mask, void *data)
786{
787 struct wayland_compositor *c = data;
788
789 c->parent.event_mask = mask;
790 if (c->parent.wl_source)
791 wl_event_source_fd_update(c->parent.wl_source, mask);
792
793 return 0;
794}
795
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400796static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100797wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
798{
799 struct wayland_compositor *c = data;
800
801 if (mask & WL_EVENT_READABLE)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400802 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_READABLE);
Kristian Høgsbergf258a312011-12-28 22:51:20 -0500803 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400804 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_WRITABLE);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400805
806 return 1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100807}
808
Daniel Stonea497d572012-06-04 11:40:46 +0100809static int
810wayland_input_create(struct wayland_compositor *c)
811{
812 struct weston_seat *seat;
813
814 seat = malloc(sizeof *seat);
815 if (seat == NULL)
816 return -1;
817
818 memset(seat, 0, sizeof *seat);
819 weston_seat_init(seat, &c->base);
820
821 c->base.seat = seat;
822
Daniel Stonea497d572012-06-04 11:40:46 +0100823 return 0;
824}
825
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500826static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500827wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500828{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500829 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -0700830
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500831 free(ec);
832}
833
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500834static struct weston_compositor *
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500835wayland_compositor_create(struct wl_display *display,
Daniel Stonebaf43592012-06-01 11:11:10 -0400836 int width, int height, const char *display_name,
Daniel Stonec1be8e52012-06-01 11:14:02 -0400837 int argc, char *argv[], const char *config_file)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100838{
839 struct wayland_compositor *c;
840 struct wl_event_loop *loop;
841 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100842
843 c = malloc(sizeof *c);
844 if (c == NULL)
845 return NULL;
846
847 memset(c, 0, sizeof *c);
848
Daniel Stone725c2c32012-06-22 14:04:36 +0100849 if (weston_compositor_init(&c->base, display, argc, argv,
850 config_file) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200851 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +0100852
853 if (wayland_input_create(c) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200854 goto err_compositor;
Daniel Stone725c2c32012-06-22 14:04:36 +0100855
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400856 c->parent.wl_display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100857
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400858 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200859 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +0200860 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100861 }
862
863 wl_list_init(&c->input_list);
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400864 wl_display_add_global_listener(c->parent.wl_display,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100865 display_handle_global, c);
866
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400867 wl_display_iterate(c->parent.wl_display, WL_DISPLAY_READABLE);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100868
869 c->base.wl_display = display;
870 if (wayland_compositor_init_egl(c) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200871 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100872
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100873 c->base.destroy = wayland_destroy;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100874
Daniel Stone725c2c32012-06-22 14:04:36 +0100875 if (weston_compositor_init_gl(&c->base) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200876 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100877
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500878 create_border(c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100879 if (wayland_compositor_create_output(c, width, height) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200880 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100881
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100882 loop = wl_display_get_event_loop(c->base.wl_display);
883
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400884 fd = wl_display_get_fd(c->parent.wl_display, update_event_mask, c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100885 c->parent.wl_source =
886 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
887 wayland_compositor_handle_event, c);
888 if (c->parent.wl_source == NULL)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200889 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100890
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100891 return &c->base;
Martin Olssonc5db50f2012-07-08 03:03:43 +0200892
893err_display:
894 wl_display_disconnect(c->parent.wl_display);
895err_compositor:
896 weston_compositor_shutdown(&c->base);
897err_free:
898 free(c);
899 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100900}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400901
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500902WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -0400903backend_init(struct wl_display *display, int argc, char *argv[],
904 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400905{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400906 int width = 1024, height = 640;
907 char *display_name = NULL;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400908
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400909 const struct weston_option wayland_options[] = {
910 { WESTON_OPTION_INTEGER, "width", 0, &width },
911 { WESTON_OPTION_INTEGER, "height", 0, &height },
912 { WESTON_OPTION_STRING, "display", 0, &display_name },
913 };
914
915 parse_options(wayland_options,
916 ARRAY_LENGTH(wayland_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400917
Daniel Stonebaf43592012-06-01 11:11:10 -0400918 return wayland_compositor_create(display, width, height, display_name,
Daniel Stonec1be8e52012-06-01 11:14:02 -0400919 argc, argv, config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400920}