blob: 9e1f8a3dad00e9e0cba881604645bde170a7c306 [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 {
53 struct wl_display *display;
54 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 {
88 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;
94};
95
Kristian Høgsberg546a8122012-02-01 07:45:51 -050096
97static int
98texture_border(struct wayland_output *output)
99{
100 struct wayland_compositor *c =
101 (struct wayland_compositor *) output->base.compositor;
102 GLfloat *d;
103 unsigned int *p;
104 int i, j, k, n;
105 GLfloat x[4], y[4], u[4], v[4];
106
107 x[0] = -c->border.left;
108 x[1] = 0;
109 x[2] = output->base.current->width;
110 x[3] = output->base.current->width + c->border.right;
111
112 y[0] = -c->border.top;
113 y[1] = 0;
114 y[2] = output->base.current->height;
115 y[3] = output->base.current->height + c->border.bottom;
116
117 u[0] = 0.0;
118 u[1] = (GLfloat) c->border.left / c->border.width;
119 u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
120 u[3] = 1.0;
121
122 v[0] = 0.0;
123 v[1] = (GLfloat) c->border.top / c->border.height;
124 v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
125 v[3] = 1.0;
126
127 n = 8;
128 d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
129 p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
130
131 k = 0;
132 for (i = 0; i < 3; i++)
133 for (j = 0; j < 3; j++) {
134
135 if (i == 1 && j == 1)
136 continue;
137
138 d[ 0] = x[i];
139 d[ 1] = y[j];
140 d[ 2] = u[i];
141 d[ 3] = v[j];
142
143 d[ 4] = x[i];
144 d[ 5] = y[j + 1];
145 d[ 6] = u[i];
146 d[ 7] = v[j + 1];
147
148 d[ 8] = x[i + 1];
149 d[ 9] = y[j];
150 d[10] = u[i + 1];
151 d[11] = v[j];
152
153 d[12] = x[i + 1];
154 d[13] = y[j + 1];
155 d[14] = u[i + 1];
156 d[15] = v[j + 1];
157
158 p[0] = k + 0;
159 p[1] = k + 1;
160 p[2] = k + 2;
161 p[3] = k + 2;
162 p[4] = k + 1;
163 p[5] = k + 3;
164
165 d += 16;
166 p += 6;
167 k += 4;
168 }
169
170 return k / 4;
171}
172
173static void
174draw_border(struct wayland_output *output)
175{
176 struct wayland_compositor *c =
177 (struct wayland_compositor *) output->base.compositor;
178 struct weston_shader *shader = &c->base.texture_shader;
179 GLfloat *v;
180 int n;
181
182 glDisable(GL_BLEND);
183 glUseProgram(shader->program);
184 c->base.current_shader = shader;
185
186 glUniformMatrix4fv(shader->proj_uniform,
187 1, GL_FALSE, output->base.matrix.d);
188
189 glUniform1i(shader->tex_uniform, 0);
190 glUniform1f(shader->alpha_uniform, 1);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500191 glUniform1f(shader->texwidth_uniform, 1);
192
193 n = texture_border(output);
194
195 glBindTexture(GL_TEXTURE_2D, c->border.texture);
196
197 v = c->base.vertices.data;
198 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
199 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
200 glEnableVertexAttribArray(0);
201 glEnableVertexAttribArray(1);
202
203 glDrawElements(GL_TRIANGLES, n * 6,
204 GL_UNSIGNED_INT, c->base.indices.data);
205
206 glDisableVertexAttribArray(1);
207 glDisableVertexAttribArray(0);
208
209 c->base.vertices.size = 0;
210 c->base.indices.size = 0;
211}
212
213static void
214create_border(struct wayland_compositor *c)
215{
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200216 pixman_image_t *image;
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500217
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200218 image = load_image(DATADIR "/weston/border.png");
219 if (!image) {
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500220 fprintf(stderr, "could'nt load border image\n");
221 return;
222 }
223
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200224 c->border.width = pixman_image_get_width(image);
225 c->border.height = pixman_image_get_height(image);
226
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500227 glGenTextures(1, &c->border.texture);
228 glBindTexture(GL_TEXTURE_2D, c->border.texture);
229 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
233
234 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
235 c->border.width,
236 c->border.height,
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200237 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
238 pixman_image_get_data(image));
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500239
240 c->border.top = 25;
241 c->border.bottom = 50;
242 c->border.left = 25;
243 c->border.right = 25;
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200244
245 pixman_image_unref(image);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500246}
247
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100248static int
249wayland_input_create(struct wayland_compositor *c)
250{
Daniel Stone37816df2012-05-16 18:45:18 +0100251 struct weston_seat *seat;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100252
Daniel Stone37816df2012-05-16 18:45:18 +0100253 seat = malloc(sizeof *seat);
254 if (seat == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100255 return -1;
256
Daniel Stone37816df2012-05-16 18:45:18 +0100257 memset(seat, 0, sizeof *seat);
258 weston_seat_init(seat, &c->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100259
Daniel Stone37816df2012-05-16 18:45:18 +0100260 c->base.seat = seat;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100261
262 return 0;
263}
264
265static int
266wayland_compositor_init_egl(struct wayland_compositor *c)
267{
268 EGLint major, minor;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100269 EGLint n;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100270 EGLint config_attribs[] = {
271 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
272 EGL_RED_SIZE, 1,
273 EGL_GREEN_SIZE, 1,
274 EGL_BLUE_SIZE, 1,
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500275 EGL_ALPHA_SIZE, 1,
Kristian Høgsbergd28ab362011-03-02 11:36:30 -0500276 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100277 EGL_NONE
278 };
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100279 static const EGLint context_attribs[] = {
280 EGL_CONTEXT_CLIENT_VERSION, 2,
281 EGL_NONE
282 };
283
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400284 c->base.display = eglGetDisplay(c->parent.display);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100285 if (c->base.display == NULL) {
286 fprintf(stderr, "failed to create display\n");
287 return -1;
288 }
289
290 if (!eglInitialize(c->base.display, &major, &minor)) {
291 fprintf(stderr, "failed to initialize display\n");
292 return -1;
293 }
294
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100295 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
296 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
297 return -1;
298 }
Benjamin Franzkebe014562011-02-18 17:04:24 +0100299 if (!eglChooseConfig(c->base.display, config_attribs,
300 &c->base.config, 1, &n) || n == 0) {
301 fprintf(stderr, "failed to choose config: %d\n", n);
302 return -1;
303 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100304
Benjamin Franzkebe014562011-02-18 17:04:24 +0100305 c->base.context = eglCreateContext(c->base.display, c->base.config,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100306 EGL_NO_CONTEXT, context_attribs);
307 if (c->base.context == NULL) {
308 fprintf(stderr, "failed to create context\n");
309 return -1;
310 }
311
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400312 c->dummy_pixmap = wl_egl_pixmap_create(10, 10, 0);
313 if (!c->dummy_pixmap) {
314 fprintf(stderr, "failure to create dummy_pixmap\n");
315 return -1;
316 }
317
318 c->dummy_egl_surface =
319 eglCreatePixmapSurface(c->base.display, c->base.config,
320 c->dummy_pixmap, NULL);
321 if (!eglMakeCurrent(c->base.display, c->dummy_egl_surface,
322 c->dummy_egl_surface, c->base.context)) {
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100323 fprintf(stderr, "failed to make context current\n");
324 return -1;
325 }
326
327 return 0;
328}
329
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500330static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500331frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400332{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500333 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400334
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500335 wl_callback_destroy(callback);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500336 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400337}
338
339static const struct wl_callback_listener frame_listener = {
340 frame_done
341};
342
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500343static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500344wayland_output_repaint(struct weston_output *output_base,
345 pixman_region32_t *damage)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100346{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100347 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500348 struct wayland_compositor *compositor =
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100349 (struct wayland_compositor *) output->base.compositor;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400350 struct wl_callback *callback;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500351 struct weston_surface *surface;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100352
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500353 if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
354 output->egl_surface, compositor->base.context)) {
355 fprintf(stderr, "failed to make current\n");
356 return;
357 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100358
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500359 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500360 weston_surface_draw(surface, &output->base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500361
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500362 draw_border(output);
363
Scott Moreau062be7e2012-04-20 13:37:33 -0600364 weston_output_do_read_pixels(&output->base);
365
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500366 eglSwapBuffers(compositor->base.display, output->egl_surface);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400367 callback = wl_surface_frame(output->parent.surface);
368 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100369
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500370 return;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100371}
372
Matt Roper361d2ad2011-08-29 13:52:23 -0700373static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500374wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700375{
376 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500377 struct weston_compositor *ec = output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700378
379 eglDestroySurface(ec->display, output->egl_surface);
380 wl_egl_window_destroy(output->parent.egl_window);
381 free(output);
382
383 return;
384}
385
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200386static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100387wayland_compositor_create_output(struct wayland_compositor *c,
388 int width, int height)
389{
390 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100391
392 output = malloc(sizeof *output);
393 if (output == NULL)
394 return -1;
395 memset(output, 0, sizeof *output);
396
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400397 output->mode.flags =
398 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
399 output->mode.width = width;
400 output->mode.height = height;
401 output->mode.refresh = 60;
402 wl_list_init(&output->base.mode_list);
403 wl_list_insert(&output->base.mode_list, &output->mode.link);
404
405 output->base.current = &output->mode;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500406 weston_output_init(&output->base, &c->base, 0, 0, width, height,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100407 WL_OUTPUT_FLIPPED);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400408
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500409 output->base.border.top = c->border.top;
410 output->base.border.bottom = c->border.bottom;
411 output->base.border.left = c->border.left;
412 output->base.border.right = c->border.right;
413
414 weston_output_move(&output->base, 0, 0);
415
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100416 output->parent.surface =
417 wl_compositor_create_surface(c->parent.compositor);
418 wl_surface_set_user_data(output->parent.surface, output);
419
Benjamin Franzkebe014562011-02-18 17:04:24 +0100420 output->parent.egl_window =
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500421 wl_egl_window_create(output->parent.surface,
422 width + c->border.left + c->border.right,
423 height + c->border.top + c->border.bottom);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100424 if (!output->parent.egl_window) {
425 fprintf(stderr, "failure to create wl_egl_window\n");
426 goto cleanup_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100427 }
428
Benjamin Franzkebe014562011-02-18 17:04:24 +0100429 output->egl_surface =
430 eglCreateWindowSurface(c->base.display, c->base.config,
431 output->parent.egl_window, NULL);
432 if (!output->egl_surface) {
433 fprintf(stderr, "failed to create window surface\n");
434 goto cleanup_window;
435 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100436
Benjamin Franzkebe014562011-02-18 17:04:24 +0100437 if (!eglMakeCurrent(c->base.display, output->egl_surface,
438 output->egl_surface, c->base.context)) {
439 fprintf(stderr, "failed to make surface current\n");
440 goto cleanup_surface;
441 return -1;
442 }
443
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200444 output->parent.shell_surface =
445 wl_shell_get_shell_surface(c->parent.shell,
446 output->parent.surface);
447 /* FIXME: add shell_surface listener for resizing */
448 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100449
Alex Wubd3354b2012-04-17 17:20:49 +0800450 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500451 output->base.repaint = wayland_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700452 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800453 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200454 output->base.set_backlight = NULL;
455 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800456 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100457
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100458 wl_list_insert(c->base.output_list.prev, &output->base.link);
459
460 return 0;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100461
462cleanup_surface:
463 eglDestroySurface(c->base.display, output->egl_surface);
464cleanup_window:
465 wl_egl_window_destroy(output->parent.egl_window);
466cleanup_output:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500467 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +0100468 free(output);
469
470 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100471}
472
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100473/* Events received from the wayland-server this compositor is client of: */
474
475/* parent output interface */
476static void
477display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400478 struct wl_output *wl_output,
479 int x,
480 int y,
481 int physical_width,
482 int physical_height,
483 int subpixel,
484 const char *make,
485 const char *model)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100486{
487 struct wayland_compositor *c = data;
488
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500489 c->parent.screen_allocation.x = x;
490 c->parent.screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400491}
492
493static void
494display_handle_mode(void *data,
495 struct wl_output *wl_output,
496 uint32_t flags,
497 int width,
498 int height,
499 int refresh)
500{
501 struct wayland_compositor *c = data;
502
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100503 c->parent.screen_allocation.width = width;
504 c->parent.screen_allocation.height = height;
505}
506
507static const struct wl_output_listener output_listener = {
508 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400509 display_handle_mode
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100510};
511
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100512/* parent input interface */
513static void
Daniel Stone37816df2012-05-16 18:45:18 +0100514input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
515 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400516 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100517{
518 struct wayland_input *input = data;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500519 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100520 struct wayland_compositor *c = input->compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100521
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500522 output = wl_surface_get_user_data(surface);
Daniel Stone37816df2012-05-16 18:45:18 +0100523 notify_pointer_focus(&c->base.seat->seat, &output->base, x, y);
524 wl_pointer_attach(input->pointer, serial, NULL, 0, 0);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100525}
526
527static void
Daniel Stone37816df2012-05-16 18:45:18 +0100528input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
529 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500530{
531 struct wayland_input *input = data;
532 struct wayland_compositor *c = input->compositor;
533
Daniel Stone37816df2012-05-16 18:45:18 +0100534 notify_pointer_focus(&c->base.seat->seat, NULL, 0, 0);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500535}
536
537static void
Daniel Stone37816df2012-05-16 18:45:18 +0100538input_handle_motion(void *data, struct wl_pointer *pointer,
539 uint32_t time, wl_fixed_t x, wl_fixed_t y)
540{
541 struct wayland_input *input = data;
542 struct wayland_compositor *c = input->compositor;
543
544 notify_motion(&c->base.seat->seat, time,
545 x - wl_fixed_from_int(c->border.left),
546 y - wl_fixed_from_int(c->border.top));
547}
548
549static void
550input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100551 uint32_t serial, uint32_t time, uint32_t button,
552 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +0100553{
554 struct wayland_input *input = data;
555 struct wayland_compositor *c = input->compositor;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100556 enum wl_pointer_button_state state = state_w;
Daniel Stone37816df2012-05-16 18:45:18 +0100557
558 notify_button(&c->base.seat->seat, time, button, state);
559}
560
561static void
562input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100563 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +0100564{
565 struct wayland_input *input = data;
566 struct wayland_compositor *c = input->compositor;
567
Daniel Stone2fce4022012-05-30 16:32:00 +0100568 notify_axis(&c->base.seat->seat, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +0100569}
570
571static const struct wl_pointer_listener pointer_listener = {
572 input_handle_pointer_enter,
573 input_handle_pointer_leave,
574 input_handle_motion,
575 input_handle_button,
576 input_handle_axis,
577};
578
579static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100580input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
581 int fd, uint32_t size)
582{
583 struct wayland_input *input = data;
584 struct xkb_keymap *keymap;
585 char *map_str;
586
587 if (!data) {
588 close(fd);
589 return;
590 }
591
592 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
593 close(fd);
594 return;
595 }
596
597 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
598 if (map_str == MAP_FAILED) {
599 close(fd);
600 return;
601 }
602
603 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
604 map_str,
605 XKB_KEYMAP_FORMAT_TEXT_V1,
606 0);
607 munmap(map_str, size);
608 close(fd);
609
610 if (!keymap) {
611 fprintf(stderr, "failed to compile keymap\n");
612 return;
613 }
614
615 weston_seat_init_keyboard(input->compositor->base.seat, keymap);
616 xkb_map_unref(keymap);
617}
618
619static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500620input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100621 struct wl_keyboard *keyboard,
622 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500623 struct wl_surface *surface,
624 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100625{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500626 struct wayland_input *input = data;
627 struct wayland_compositor *c = input->compositor;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500628
Daniel Stone37816df2012-05-16 18:45:18 +0100629 notify_keyboard_focus(&c->base.seat->seat, keys);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500630}
631
632static void
633input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100634 struct wl_keyboard *keyboard,
635 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500636 struct wl_surface *surface)
637{
638 struct wayland_input *input = data;
639 struct wayland_compositor *c = input->compositor;
640
Daniel Stone37816df2012-05-16 18:45:18 +0100641 notify_keyboard_focus(&c->base.seat->seat, NULL);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100642}
643
Daniel Stone37816df2012-05-16 18:45:18 +0100644static void
645input_handle_key(void *data, struct wl_keyboard *keyboard,
646 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
647{
648 struct wayland_input *input = data;
649 struct wayland_compositor *c = input->compositor;
650
Daniel Stonec9785ea2012-05-30 16:31:52 +0100651 notify_key(&c->base.seat->seat, time, key,
652 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
653 WL_KEYBOARD_KEY_STATE_RELEASED);
Daniel Stone37816df2012-05-16 18:45:18 +0100654}
655
Daniel Stone351eb612012-05-31 15:27:47 -0400656static void
657input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
658 uint32_t serial, uint32_t mods_depressed,
659 uint32_t mods_latched, uint32_t mods_locked,
660 uint32_t group)
661{
662 /* XXX notify_modifiers(); */
663}
664
Daniel Stone37816df2012-05-16 18:45:18 +0100665static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100666 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500667 input_handle_keyboard_enter,
668 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +0100669 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -0400670 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100671};
672
673static void
Daniel Stone37816df2012-05-16 18:45:18 +0100674input_handle_capabilities(void *data, struct wl_seat *seat,
675 enum wl_seat_capability caps)
676{
677 struct wayland_input *input = data;
678
679 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
680 input->pointer = wl_seat_get_pointer(seat);
681 wl_pointer_set_user_data(input->pointer, input);
682 wl_pointer_add_listener(input->pointer, &pointer_listener,
683 input);
Daniel Stonecd981dd2012-06-01 12:14:00 +0100684 weston_seat_init_pointer(input->compositor->base.seat);
Daniel Stone37816df2012-05-16 18:45:18 +0100685 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
686 wl_pointer_destroy(input->pointer);
687 input->pointer = NULL;
688 }
689
690 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
691 input->keyboard = wl_seat_get_keyboard(seat);
692 wl_keyboard_set_user_data(input->keyboard, input);
693 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
694 input);
695 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
696 wl_keyboard_destroy(input->keyboard);
697 input->keyboard = NULL;
698 }
699}
700
701static const struct wl_seat_listener seat_listener = {
702 input_handle_capabilities,
703};
704
705static void
706display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100707{
708 struct wayland_input *input;
709
710 input = malloc(sizeof *input);
711 if (input == NULL)
712 return;
713
714 memset(input, 0, sizeof *input);
715
716 input->compositor = c;
Daniel Stone37816df2012-05-16 18:45:18 +0100717 input->seat = wl_display_bind(c->parent.display, id,
718 &wl_seat_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100719 wl_list_insert(c->input_list.prev, &input->link);
720
Daniel Stone37816df2012-05-16 18:45:18 +0100721 wl_seat_add_listener(input->seat, &seat_listener, input);
722 wl_seat_set_user_data(input->seat, input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100723}
724
725static void
726display_handle_global(struct wl_display *display, uint32_t id,
727 const char *interface, uint32_t version, void *data)
728{
729 struct wayland_compositor *c = data;
730
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200731 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400732 c->parent.compositor =
733 wl_display_bind(display, id, &wl_compositor_interface);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200734 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400735 c->parent.output =
736 wl_display_bind(display, id, &wl_output_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100737 wl_output_add_listener(c->parent.output, &output_listener, c);
Daniel Stone37816df2012-05-16 18:45:18 +0100738 } else if (strcmp(interface, "wl_seat") == 0) {
739 display_add_seat(c, id);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200740 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400741 c->parent.shell =
742 wl_display_bind(display, id, &wl_shell_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100743 }
744}
745
746static int
747update_event_mask(uint32_t mask, void *data)
748{
749 struct wayland_compositor *c = data;
750
751 c->parent.event_mask = mask;
752 if (c->parent.wl_source)
753 wl_event_source_fd_update(c->parent.wl_source, mask);
754
755 return 0;
756}
757
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400758static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100759wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
760{
761 struct wayland_compositor *c = data;
762
763 if (mask & WL_EVENT_READABLE)
764 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
Kristian Høgsbergf258a312011-12-28 22:51:20 -0500765 if (mask & WL_EVENT_WRITABLE)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100766 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400767
768 return 1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100769}
770
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500771static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500772wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500773{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500774 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -0700775
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500776 free(ec);
777}
778
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500779static struct weston_compositor *
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500780wayland_compositor_create(struct wl_display *display,
Daniel Stonebaf43592012-06-01 11:11:10 -0400781 int width, int height, const char *display_name,
Daniel Stonec1be8e52012-06-01 11:14:02 -0400782 int argc, char *argv[], const char *config_file)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100783{
784 struct wayland_compositor *c;
785 struct wl_event_loop *loop;
786 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100787
788 c = malloc(sizeof *c);
789 if (c == NULL)
790 return NULL;
791
792 memset(c, 0, sizeof *c);
793
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500794 c->parent.display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100795
796 if (c->parent.display == NULL) {
797 fprintf(stderr, "failed to create display: %m\n");
798 return NULL;
799 }
800
801 wl_list_init(&c->input_list);
802 wl_display_add_global_listener(c->parent.display,
803 display_handle_global, c);
804
805 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
806
807 c->base.wl_display = display;
808 if (wayland_compositor_init_egl(c) < 0)
809 return NULL;
810
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100811 c->base.destroy = wayland_destroy;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100812
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100813 /* Can't init base class until we have a current egl context */
Daniel Stonec1be8e52012-06-01 11:14:02 -0400814 if (weston_compositor_init(&c->base, display, argc, argv,
815 config_file) < 0)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100816 return NULL;
817
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500818 create_border(c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100819 if (wayland_compositor_create_output(c, width, height) < 0)
820 return NULL;
821
822 if (wayland_input_create(c) < 0)
823 return NULL;
824
825 loop = wl_display_get_event_loop(c->base.wl_display);
826
827 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
828 c->parent.wl_source =
829 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
830 wayland_compositor_handle_event, c);
831 if (c->parent.wl_source == NULL)
832 return NULL;
833
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100834 return &c->base;
835}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400836
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500837WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -0400838backend_init(struct wl_display *display, int argc, char *argv[],
839 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400840{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400841 int width = 1024, height = 640;
842 char *display_name = NULL;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400843
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400844 const struct weston_option wayland_options[] = {
845 { WESTON_OPTION_INTEGER, "width", 0, &width },
846 { WESTON_OPTION_INTEGER, "height", 0, &height },
847 { WESTON_OPTION_STRING, "display", 0, &display_name },
848 };
849
850 parse_options(wayland_options,
851 ARRAY_LENGTH(wayland_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400852
Daniel Stonebaf43592012-06-01 11:11:10 -0400853 return wayland_compositor_create(display, width, height, display_name,
Daniel Stonec1be8e52012-06-01 11:14:02 -0400854 argc, argv, config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400855}