blob: 02ebc6cf79ffce83ddfe0d149cdbee9a77eb015b [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>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010034
Benjamin Franzkebe014562011-02-18 17:04:24 +010035#include <wayland-client.h>
36#include <wayland-egl.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010037
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010038#include <GLES2/gl2.h>
39#include <GLES2/gl2ext.h>
40#include <EGL/egl.h>
41#include <EGL/eglext.h>
42
43#include "compositor.h"
44
45struct wayland_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050046 struct weston_compositor base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010047
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -040048 struct wl_egl_pixmap *dummy_pixmap;
Kristian Høgsbergb71302e2012-05-10 12:28:35 -040049 EGLSurface dummy_egl_surface;
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -040050
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010051 struct {
52 struct wl_display *display;
53 struct wl_compositor *compositor;
54 struct wl_shell *shell;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010055 struct wl_output *output;
56
57 struct {
58 int32_t x, y, width, height;
59 } screen_allocation;
60
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010061 struct wl_event_source *wl_source;
62 uint32_t event_mask;
63 } parent;
64
Kristian Høgsberg546a8122012-02-01 07:45:51 -050065 struct {
66 int32_t top, bottom, left, right;
67 GLuint texture;
68 int32_t width, height;
69 } border;
70
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010071 struct wl_list input_list;
72};
73
74struct wayland_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050075 struct weston_output base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010076
77 struct {
78 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020079 struct wl_shell_surface *shell_surface;
Benjamin Franzkebe014562011-02-18 17:04:24 +010080 struct wl_egl_window *egl_window;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010081 } parent;
Benjamin Franzkebe014562011-02-18 17:04:24 +010082 EGLSurface egl_surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050083 struct weston_mode mode;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010084};
85
86struct wayland_input {
87 struct wayland_compositor *compositor;
Daniel Stone37816df2012-05-16 18:45:18 +010088 struct wl_seat *seat;
89 struct wl_pointer *pointer;
90 struct wl_keyboard *keyboard;
91 struct wl_touch *touch;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010092 struct wl_list link;
93};
94
Kristian Høgsberg546a8122012-02-01 07:45:51 -050095
96static int
97texture_border(struct wayland_output *output)
98{
99 struct wayland_compositor *c =
100 (struct wayland_compositor *) output->base.compositor;
101 GLfloat *d;
102 unsigned int *p;
103 int i, j, k, n;
104 GLfloat x[4], y[4], u[4], v[4];
105
106 x[0] = -c->border.left;
107 x[1] = 0;
108 x[2] = output->base.current->width;
109 x[3] = output->base.current->width + c->border.right;
110
111 y[0] = -c->border.top;
112 y[1] = 0;
113 y[2] = output->base.current->height;
114 y[3] = output->base.current->height + c->border.bottom;
115
116 u[0] = 0.0;
117 u[1] = (GLfloat) c->border.left / c->border.width;
118 u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
119 u[3] = 1.0;
120
121 v[0] = 0.0;
122 v[1] = (GLfloat) c->border.top / c->border.height;
123 v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
124 v[3] = 1.0;
125
126 n = 8;
127 d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
128 p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
129
130 k = 0;
131 for (i = 0; i < 3; i++)
132 for (j = 0; j < 3; j++) {
133
134 if (i == 1 && j == 1)
135 continue;
136
137 d[ 0] = x[i];
138 d[ 1] = y[j];
139 d[ 2] = u[i];
140 d[ 3] = v[j];
141
142 d[ 4] = x[i];
143 d[ 5] = y[j + 1];
144 d[ 6] = u[i];
145 d[ 7] = v[j + 1];
146
147 d[ 8] = x[i + 1];
148 d[ 9] = y[j];
149 d[10] = u[i + 1];
150 d[11] = v[j];
151
152 d[12] = x[i + 1];
153 d[13] = y[j + 1];
154 d[14] = u[i + 1];
155 d[15] = v[j + 1];
156
157 p[0] = k + 0;
158 p[1] = k + 1;
159 p[2] = k + 2;
160 p[3] = k + 2;
161 p[4] = k + 1;
162 p[5] = k + 3;
163
164 d += 16;
165 p += 6;
166 k += 4;
167 }
168
169 return k / 4;
170}
171
172static void
173draw_border(struct wayland_output *output)
174{
175 struct wayland_compositor *c =
176 (struct wayland_compositor *) output->base.compositor;
177 struct weston_shader *shader = &c->base.texture_shader;
178 GLfloat *v;
179 int n;
180
181 glDisable(GL_BLEND);
182 glUseProgram(shader->program);
183 c->base.current_shader = shader;
184
185 glUniformMatrix4fv(shader->proj_uniform,
186 1, GL_FALSE, output->base.matrix.d);
187
188 glUniform1i(shader->tex_uniform, 0);
189 glUniform1f(shader->alpha_uniform, 1);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500190 glUniform1f(shader->texwidth_uniform, 1);
191
192 n = texture_border(output);
193
194 glBindTexture(GL_TEXTURE_2D, c->border.texture);
195
196 v = c->base.vertices.data;
197 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
198 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
199 glEnableVertexAttribArray(0);
200 glEnableVertexAttribArray(1);
201
202 glDrawElements(GL_TRIANGLES, n * 6,
203 GL_UNSIGNED_INT, c->base.indices.data);
204
205 glDisableVertexAttribArray(1);
206 glDisableVertexAttribArray(0);
207
208 c->base.vertices.size = 0;
209 c->base.indices.size = 0;
210}
211
212static void
213create_border(struct wayland_compositor *c)
214{
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200215 pixman_image_t *image;
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500216
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200217 image = load_image(DATADIR "/weston/border.png");
218 if (!image) {
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500219 fprintf(stderr, "could'nt load border image\n");
220 return;
221 }
222
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200223 c->border.width = pixman_image_get_width(image);
224 c->border.height = pixman_image_get_height(image);
225
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500226 glGenTextures(1, &c->border.texture);
227 glBindTexture(GL_TEXTURE_2D, c->border.texture);
228 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
229 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
232
233 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
234 c->border.width,
235 c->border.height,
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200236 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
237 pixman_image_get_data(image));
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500238
239 c->border.top = 25;
240 c->border.bottom = 50;
241 c->border.left = 25;
242 c->border.right = 25;
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200243
244 pixman_image_unref(image);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500245}
246
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100247static int
248wayland_input_create(struct wayland_compositor *c)
249{
Daniel Stone37816df2012-05-16 18:45:18 +0100250 struct weston_seat *seat;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100251
Daniel Stone37816df2012-05-16 18:45:18 +0100252 seat = malloc(sizeof *seat);
253 if (seat == NULL)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100254 return -1;
255
Daniel Stone37816df2012-05-16 18:45:18 +0100256 memset(seat, 0, sizeof *seat);
257 weston_seat_init(seat, &c->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100258
Daniel Stone37816df2012-05-16 18:45:18 +0100259 c->base.seat = seat;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100260
261 return 0;
262}
263
264static int
265wayland_compositor_init_egl(struct wayland_compositor *c)
266{
267 EGLint major, minor;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100268 EGLint n;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100269 EGLint config_attribs[] = {
270 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
271 EGL_RED_SIZE, 1,
272 EGL_GREEN_SIZE, 1,
273 EGL_BLUE_SIZE, 1,
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500274 EGL_ALPHA_SIZE, 1,
Kristian Høgsbergd28ab362011-03-02 11:36:30 -0500275 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100276 EGL_NONE
277 };
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100278 static const EGLint context_attribs[] = {
279 EGL_CONTEXT_CLIENT_VERSION, 2,
280 EGL_NONE
281 };
282
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400283 c->base.display = eglGetDisplay(c->parent.display);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100284 if (c->base.display == NULL) {
285 fprintf(stderr, "failed to create display\n");
286 return -1;
287 }
288
289 if (!eglInitialize(c->base.display, &major, &minor)) {
290 fprintf(stderr, "failed to initialize display\n");
291 return -1;
292 }
293
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100294 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
295 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
296 return -1;
297 }
Benjamin Franzkebe014562011-02-18 17:04:24 +0100298 if (!eglChooseConfig(c->base.display, config_attribs,
299 &c->base.config, 1, &n) || n == 0) {
300 fprintf(stderr, "failed to choose config: %d\n", n);
301 return -1;
302 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100303
Benjamin Franzkebe014562011-02-18 17:04:24 +0100304 c->base.context = eglCreateContext(c->base.display, c->base.config,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100305 EGL_NO_CONTEXT, context_attribs);
306 if (c->base.context == NULL) {
307 fprintf(stderr, "failed to create context\n");
308 return -1;
309 }
310
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400311 c->dummy_pixmap = wl_egl_pixmap_create(10, 10, 0);
312 if (!c->dummy_pixmap) {
313 fprintf(stderr, "failure to create dummy_pixmap\n");
314 return -1;
315 }
316
317 c->dummy_egl_surface =
318 eglCreatePixmapSurface(c->base.display, c->base.config,
319 c->dummy_pixmap, NULL);
320 if (!eglMakeCurrent(c->base.display, c->dummy_egl_surface,
321 c->dummy_egl_surface, c->base.context)) {
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100322 fprintf(stderr, "failed to make context current\n");
323 return -1;
324 }
325
326 return 0;
327}
328
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500329static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500330frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400331{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500332 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400333
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500334 wl_callback_destroy(callback);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500335 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400336}
337
338static const struct wl_callback_listener frame_listener = {
339 frame_done
340};
341
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500342static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500343wayland_output_repaint(struct weston_output *output_base,
344 pixman_region32_t *damage)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100345{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100346 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500347 struct wayland_compositor *compositor =
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100348 (struct wayland_compositor *) output->base.compositor;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400349 struct wl_callback *callback;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500350 struct weston_surface *surface;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100351
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500352 if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
353 output->egl_surface, compositor->base.context)) {
354 fprintf(stderr, "failed to make current\n");
355 return;
356 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100357
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500358 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500359 weston_surface_draw(surface, &output->base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500360
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500361 draw_border(output);
362
Scott Moreau062be7e2012-04-20 13:37:33 -0600363 weston_output_do_read_pixels(&output->base);
364
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500365 eglSwapBuffers(compositor->base.display, output->egl_surface);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400366 callback = wl_surface_frame(output->parent.surface);
367 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100368
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500369 return;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100370}
371
Matt Roper361d2ad2011-08-29 13:52:23 -0700372static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500373wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700374{
375 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500376 struct weston_compositor *ec = output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700377
378 eglDestroySurface(ec->display, output->egl_surface);
379 wl_egl_window_destroy(output->parent.egl_window);
380 free(output);
381
382 return;
383}
384
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200385static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100386wayland_compositor_create_output(struct wayland_compositor *c,
387 int width, int height)
388{
389 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100390
391 output = malloc(sizeof *output);
392 if (output == NULL)
393 return -1;
394 memset(output, 0, sizeof *output);
395
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400396 output->mode.flags =
397 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
398 output->mode.width = width;
399 output->mode.height = height;
400 output->mode.refresh = 60;
401 wl_list_init(&output->base.mode_list);
402 wl_list_insert(&output->base.mode_list, &output->mode.link);
403
404 output->base.current = &output->mode;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500405 weston_output_init(&output->base, &c->base, 0, 0, width, height,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100406 WL_OUTPUT_FLIPPED);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400407
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500408 output->base.border.top = c->border.top;
409 output->base.border.bottom = c->border.bottom;
410 output->base.border.left = c->border.left;
411 output->base.border.right = c->border.right;
412
413 weston_output_move(&output->base, 0, 0);
414
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100415 output->parent.surface =
416 wl_compositor_create_surface(c->parent.compositor);
417 wl_surface_set_user_data(output->parent.surface, output);
418
Benjamin Franzkebe014562011-02-18 17:04:24 +0100419 output->parent.egl_window =
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500420 wl_egl_window_create(output->parent.surface,
421 width + c->border.left + c->border.right,
422 height + c->border.top + c->border.bottom);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100423 if (!output->parent.egl_window) {
424 fprintf(stderr, "failure to create wl_egl_window\n");
425 goto cleanup_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100426 }
427
Benjamin Franzkebe014562011-02-18 17:04:24 +0100428 output->egl_surface =
429 eglCreateWindowSurface(c->base.display, c->base.config,
430 output->parent.egl_window, NULL);
431 if (!output->egl_surface) {
432 fprintf(stderr, "failed to create window surface\n");
433 goto cleanup_window;
434 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100435
Benjamin Franzkebe014562011-02-18 17:04:24 +0100436 if (!eglMakeCurrent(c->base.display, output->egl_surface,
437 output->egl_surface, c->base.context)) {
438 fprintf(stderr, "failed to make surface current\n");
439 goto cleanup_surface;
440 return -1;
441 }
442
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200443 output->parent.shell_surface =
444 wl_shell_get_shell_surface(c->parent.shell,
445 output->parent.surface);
446 /* FIXME: add shell_surface listener for resizing */
447 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100448
Alex Wubd3354b2012-04-17 17:20:49 +0800449 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500450 output->base.repaint = wayland_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700451 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800452 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200453 output->base.set_backlight = NULL;
454 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800455 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100456
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100457 wl_list_insert(c->base.output_list.prev, &output->base.link);
458
459 return 0;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100460
461cleanup_surface:
462 eglDestroySurface(c->base.display, output->egl_surface);
463cleanup_window:
464 wl_egl_window_destroy(output->parent.egl_window);
465cleanup_output:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500466 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +0100467 free(output);
468
469 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100470}
471
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100472/* Events received from the wayland-server this compositor is client of: */
473
474/* parent output interface */
475static void
476display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400477 struct wl_output *wl_output,
478 int x,
479 int y,
480 int physical_width,
481 int physical_height,
482 int subpixel,
483 const char *make,
484 const char *model)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100485{
486 struct wayland_compositor *c = data;
487
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500488 c->parent.screen_allocation.x = x;
489 c->parent.screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400490}
491
492static void
493display_handle_mode(void *data,
494 struct wl_output *wl_output,
495 uint32_t flags,
496 int width,
497 int height,
498 int refresh)
499{
500 struct wayland_compositor *c = data;
501
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100502 c->parent.screen_allocation.width = width;
503 c->parent.screen_allocation.height = height;
504}
505
506static const struct wl_output_listener output_listener = {
507 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400508 display_handle_mode
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100509};
510
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100511/* parent input interface */
512static void
Daniel Stone37816df2012-05-16 18:45:18 +0100513input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
514 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400515 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100516{
517 struct wayland_input *input = data;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500518 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100519 struct wayland_compositor *c = input->compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100520
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500521 output = wl_surface_get_user_data(surface);
Daniel Stone37816df2012-05-16 18:45:18 +0100522 notify_pointer_focus(&c->base.seat->seat, &output->base, x, y);
523 wl_pointer_attach(input->pointer, serial, NULL, 0, 0);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100524}
525
526static void
Daniel Stone37816df2012-05-16 18:45:18 +0100527input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
528 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500529{
530 struct wayland_input *input = data;
531 struct wayland_compositor *c = input->compositor;
532
Daniel Stone37816df2012-05-16 18:45:18 +0100533 notify_pointer_focus(&c->base.seat->seat, NULL, 0, 0);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500534}
535
536static void
Daniel Stone37816df2012-05-16 18:45:18 +0100537input_handle_motion(void *data, struct wl_pointer *pointer,
538 uint32_t time, wl_fixed_t x, wl_fixed_t y)
539{
540 struct wayland_input *input = data;
541 struct wayland_compositor *c = input->compositor;
542
543 notify_motion(&c->base.seat->seat, time,
544 x - wl_fixed_from_int(c->border.left),
545 y - wl_fixed_from_int(c->border.top));
546}
547
548static void
549input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100550 uint32_t serial, uint32_t time, uint32_t button,
551 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +0100552{
553 struct wayland_input *input = data;
554 struct wayland_compositor *c = input->compositor;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100555 enum wl_pointer_button_state state = state_w;
Daniel Stone37816df2012-05-16 18:45:18 +0100556
557 notify_button(&c->base.seat->seat, time, button, state);
558}
559
560static void
561input_handle_axis(void *data, struct wl_pointer *pointer,
562 uint32_t time, uint32_t axis, int32_t value)
563{
564 struct wayland_input *input = data;
565 struct wayland_compositor *c = input->compositor;
566
567 notify_axis(&c->base.seat->seat, time, axis, value);
568}
569
570static const struct wl_pointer_listener pointer_listener = {
571 input_handle_pointer_enter,
572 input_handle_pointer_leave,
573 input_handle_motion,
574 input_handle_button,
575 input_handle_axis,
576};
577
578static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500579input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100580 struct wl_keyboard *keyboard,
581 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500582 struct wl_surface *surface,
583 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100584{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500585 struct wayland_input *input = data;
586 struct wayland_compositor *c = input->compositor;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500587
Daniel Stone37816df2012-05-16 18:45:18 +0100588 notify_keyboard_focus(&c->base.seat->seat, keys);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500589}
590
591static void
592input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100593 struct wl_keyboard *keyboard,
594 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500595 struct wl_surface *surface)
596{
597 struct wayland_input *input = data;
598 struct wayland_compositor *c = input->compositor;
599
Daniel Stone37816df2012-05-16 18:45:18 +0100600 notify_keyboard_focus(&c->base.seat->seat, NULL);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100601}
602
Daniel Stone37816df2012-05-16 18:45:18 +0100603static void
604input_handle_key(void *data, struct wl_keyboard *keyboard,
605 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
606{
607 struct wayland_input *input = data;
608 struct wayland_compositor *c = input->compositor;
609
610 notify_key(&c->base.seat->seat, time, key, state);
611}
612
Daniel Stone351eb612012-05-31 15:27:47 -0400613static void
614input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
615 uint32_t serial, uint32_t mods_depressed,
616 uint32_t mods_latched, uint32_t mods_locked,
617 uint32_t group)
618{
619 /* XXX notify_modifiers(); */
620}
621
Daniel Stone37816df2012-05-16 18:45:18 +0100622static const struct wl_keyboard_listener keyboard_listener = {
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500623 input_handle_keyboard_enter,
624 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +0100625 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -0400626 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100627};
628
629static void
Daniel Stone37816df2012-05-16 18:45:18 +0100630input_handle_capabilities(void *data, struct wl_seat *seat,
631 enum wl_seat_capability caps)
632{
633 struct wayland_input *input = data;
634
635 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
636 input->pointer = wl_seat_get_pointer(seat);
637 wl_pointer_set_user_data(input->pointer, input);
638 wl_pointer_add_listener(input->pointer, &pointer_listener,
639 input);
640 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
641 wl_pointer_destroy(input->pointer);
642 input->pointer = NULL;
643 }
644
645 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
646 input->keyboard = wl_seat_get_keyboard(seat);
647 wl_keyboard_set_user_data(input->keyboard, input);
648 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
649 input);
650 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
651 wl_keyboard_destroy(input->keyboard);
652 input->keyboard = NULL;
653 }
654}
655
656static const struct wl_seat_listener seat_listener = {
657 input_handle_capabilities,
658};
659
660static void
661display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100662{
663 struct wayland_input *input;
664
665 input = malloc(sizeof *input);
666 if (input == NULL)
667 return;
668
669 memset(input, 0, sizeof *input);
670
671 input->compositor = c;
Daniel Stone37816df2012-05-16 18:45:18 +0100672 input->seat = wl_display_bind(c->parent.display, id,
673 &wl_seat_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100674 wl_list_insert(c->input_list.prev, &input->link);
675
Daniel Stone37816df2012-05-16 18:45:18 +0100676 wl_seat_add_listener(input->seat, &seat_listener, input);
677 wl_seat_set_user_data(input->seat, input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100678}
679
680static void
681display_handle_global(struct wl_display *display, uint32_t id,
682 const char *interface, uint32_t version, void *data)
683{
684 struct wayland_compositor *c = data;
685
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200686 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400687 c->parent.compositor =
688 wl_display_bind(display, id, &wl_compositor_interface);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200689 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400690 c->parent.output =
691 wl_display_bind(display, id, &wl_output_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100692 wl_output_add_listener(c->parent.output, &output_listener, c);
Daniel Stone37816df2012-05-16 18:45:18 +0100693 } else if (strcmp(interface, "wl_seat") == 0) {
694 display_add_seat(c, id);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200695 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400696 c->parent.shell =
697 wl_display_bind(display, id, &wl_shell_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100698 }
699}
700
701static int
702update_event_mask(uint32_t mask, void *data)
703{
704 struct wayland_compositor *c = data;
705
706 c->parent.event_mask = mask;
707 if (c->parent.wl_source)
708 wl_event_source_fd_update(c->parent.wl_source, mask);
709
710 return 0;
711}
712
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400713static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100714wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
715{
716 struct wayland_compositor *c = data;
717
718 if (mask & WL_EVENT_READABLE)
719 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
Kristian Høgsbergf258a312011-12-28 22:51:20 -0500720 if (mask & WL_EVENT_WRITABLE)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100721 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400722
723 return 1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100724}
725
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500726static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500727wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500728{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500729 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -0700730
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500731 free(ec);
732}
733
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500734static struct weston_compositor *
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500735wayland_compositor_create(struct wl_display *display,
736 int width, int height, const char *display_name)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100737{
738 struct wayland_compositor *c;
739 struct wl_event_loop *loop;
740 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100741
742 c = malloc(sizeof *c);
743 if (c == NULL)
744 return NULL;
745
746 memset(c, 0, sizeof *c);
747
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500748 c->parent.display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100749
750 if (c->parent.display == NULL) {
751 fprintf(stderr, "failed to create display: %m\n");
752 return NULL;
753 }
754
755 wl_list_init(&c->input_list);
756 wl_display_add_global_listener(c->parent.display,
757 display_handle_global, c);
758
759 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
760
761 c->base.wl_display = display;
762 if (wayland_compositor_init_egl(c) < 0)
763 return NULL;
764
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100765 c->base.destroy = wayland_destroy;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100766
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100767 /* Can't init base class until we have a current egl context */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500768 if (weston_compositor_init(&c->base, display) < 0)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100769 return NULL;
770
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500771 create_border(c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100772 if (wayland_compositor_create_output(c, width, height) < 0)
773 return NULL;
774
775 if (wayland_input_create(c) < 0)
776 return NULL;
777
778 loop = wl_display_get_event_loop(c->base.wl_display);
779
780 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
781 c->parent.wl_source =
782 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
783 wayland_compositor_handle_event, c);
784 if (c->parent.wl_source == NULL)
785 return NULL;
786
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100787 return &c->base;
788}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400789
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500790WL_EXPORT struct weston_compositor *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400791backend_init(struct wl_display *display, int argc, char *argv[])
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400792{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400793 int width = 1024, height = 640;
794 char *display_name = NULL;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400795
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400796 const struct weston_option wayland_options[] = {
797 { WESTON_OPTION_INTEGER, "width", 0, &width },
798 { WESTON_OPTION_INTEGER, "height", 0, &height },
799 { WESTON_OPTION_STRING, "display", 0, &display_name },
800 };
801
802 parse_options(wayland_options,
803 ARRAY_LENGTH(wayland_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400804
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500805 return wayland_compositor_create(display, width, height, display_name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400806}