blob: b4c55a8274663bf7301ee0ea9684cdabdb5cff1f [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
48 struct {
49 struct wl_display *display;
50 struct wl_compositor *compositor;
51 struct wl_shell *shell;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010052 struct wl_output *output;
53
54 struct {
55 int32_t x, y, width, height;
56 } screen_allocation;
57
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010058 struct wl_event_source *wl_source;
59 uint32_t event_mask;
60 } parent;
61
Kristian Høgsberg546a8122012-02-01 07:45:51 -050062 struct {
63 int32_t top, bottom, left, right;
64 GLuint texture;
65 int32_t width, height;
66 } border;
67
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010068 struct wl_list input_list;
69};
70
71struct wayland_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050072 struct weston_output base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010073
74 struct {
75 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020076 struct wl_shell_surface *shell_surface;
Benjamin Franzkebe014562011-02-18 17:04:24 +010077 struct wl_egl_window *egl_window;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010078 } parent;
Benjamin Franzkebe014562011-02-18 17:04:24 +010079 EGLSurface egl_surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050080 struct weston_mode mode;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010081};
82
83struct wayland_input {
84 struct wayland_compositor *compositor;
85 struct wl_input_device *input_device;
86 struct wl_list link;
87};
88
Kristian Høgsberg546a8122012-02-01 07:45:51 -050089
90static int
91texture_border(struct wayland_output *output)
92{
93 struct wayland_compositor *c =
94 (struct wayland_compositor *) output->base.compositor;
95 GLfloat *d;
96 unsigned int *p;
97 int i, j, k, n;
98 GLfloat x[4], y[4], u[4], v[4];
99
100 x[0] = -c->border.left;
101 x[1] = 0;
102 x[2] = output->base.current->width;
103 x[3] = output->base.current->width + c->border.right;
104
105 y[0] = -c->border.top;
106 y[1] = 0;
107 y[2] = output->base.current->height;
108 y[3] = output->base.current->height + c->border.bottom;
109
110 u[0] = 0.0;
111 u[1] = (GLfloat) c->border.left / c->border.width;
112 u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
113 u[3] = 1.0;
114
115 v[0] = 0.0;
116 v[1] = (GLfloat) c->border.top / c->border.height;
117 v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
118 v[3] = 1.0;
119
120 n = 8;
121 d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
122 p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
123
124 k = 0;
125 for (i = 0; i < 3; i++)
126 for (j = 0; j < 3; j++) {
127
128 if (i == 1 && j == 1)
129 continue;
130
131 d[ 0] = x[i];
132 d[ 1] = y[j];
133 d[ 2] = u[i];
134 d[ 3] = v[j];
135
136 d[ 4] = x[i];
137 d[ 5] = y[j + 1];
138 d[ 6] = u[i];
139 d[ 7] = v[j + 1];
140
141 d[ 8] = x[i + 1];
142 d[ 9] = y[j];
143 d[10] = u[i + 1];
144 d[11] = v[j];
145
146 d[12] = x[i + 1];
147 d[13] = y[j + 1];
148 d[14] = u[i + 1];
149 d[15] = v[j + 1];
150
151 p[0] = k + 0;
152 p[1] = k + 1;
153 p[2] = k + 2;
154 p[3] = k + 2;
155 p[4] = k + 1;
156 p[5] = k + 3;
157
158 d += 16;
159 p += 6;
160 k += 4;
161 }
162
163 return k / 4;
164}
165
166static void
167draw_border(struct wayland_output *output)
168{
169 struct wayland_compositor *c =
170 (struct wayland_compositor *) output->base.compositor;
171 struct weston_shader *shader = &c->base.texture_shader;
172 GLfloat *v;
173 int n;
174
175 glDisable(GL_BLEND);
176 glUseProgram(shader->program);
177 c->base.current_shader = shader;
178
179 glUniformMatrix4fv(shader->proj_uniform,
180 1, GL_FALSE, output->base.matrix.d);
181
182 glUniform1i(shader->tex_uniform, 0);
183 glUniform1f(shader->alpha_uniform, 1);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500184 glUniform1f(shader->texwidth_uniform, 1);
185
186 n = texture_border(output);
187
188 glBindTexture(GL_TEXTURE_2D, c->border.texture);
189
190 v = c->base.vertices.data;
191 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
192 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
193 glEnableVertexAttribArray(0);
194 glEnableVertexAttribArray(1);
195
196 glDrawElements(GL_TRIANGLES, n * 6,
197 GL_UNSIGNED_INT, c->base.indices.data);
198
199 glDisableVertexAttribArray(1);
200 glDisableVertexAttribArray(0);
201
202 c->base.vertices.size = 0;
203 c->base.indices.size = 0;
204}
205
206static void
207create_border(struct wayland_compositor *c)
208{
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200209 pixman_image_t *image;
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500210
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200211 image = load_image(DATADIR "/weston/border.png");
212 if (!image) {
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500213 fprintf(stderr, "could'nt load border image\n");
214 return;
215 }
216
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200217 c->border.width = pixman_image_get_width(image);
218 c->border.height = pixman_image_get_height(image);
219
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500220 glGenTextures(1, &c->border.texture);
221 glBindTexture(GL_TEXTURE_2D, c->border.texture);
222 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
223 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
224 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
225 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
226
227 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
228 c->border.width,
229 c->border.height,
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200230 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
231 pixman_image_get_data(image));
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500232
233 c->border.top = 25;
234 c->border.bottom = 50;
235 c->border.left = 25;
236 c->border.right = 25;
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200237
238 pixman_image_unref(image);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500239}
240
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100241static int
242wayland_input_create(struct wayland_compositor *c)
243{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500244 struct weston_input_device *input;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100245
246 input = malloc(sizeof *input);
247 if (input == NULL)
248 return -1;
249
250 memset(input, 0, sizeof *input);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500251 weston_input_device_init(input, &c->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100252
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500253 c->base.input_device = &input->input_device;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100254
255 return 0;
256}
257
258static int
259wayland_compositor_init_egl(struct wayland_compositor *c)
260{
261 EGLint major, minor;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100262 EGLint n;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100263 const char *extensions;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100264 EGLint config_attribs[] = {
265 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
266 EGL_RED_SIZE, 1,
267 EGL_GREEN_SIZE, 1,
268 EGL_BLUE_SIZE, 1,
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500269 EGL_ALPHA_SIZE, 1,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100270 EGL_DEPTH_SIZE, 1,
Kristian Høgsbergd28ab362011-03-02 11:36:30 -0500271 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100272 EGL_NONE
273 };
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100274 static const EGLint context_attribs[] = {
275 EGL_CONTEXT_CLIENT_VERSION, 2,
276 EGL_NONE
277 };
278
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400279 c->base.display = eglGetDisplay(c->parent.display);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100280 if (c->base.display == NULL) {
281 fprintf(stderr, "failed to create display\n");
282 return -1;
283 }
284
285 if (!eglInitialize(c->base.display, &major, &minor)) {
286 fprintf(stderr, "failed to initialize display\n");
287 return -1;
288 }
289
290 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
Ander Conselvan de Oliveiraef7c8d92011-11-01 16:37:41 +0200291 if (!strstr(extensions, "EGL_KHR_surfaceless_gles2")) {
292 fprintf(stderr, "EGL_KHR_surfaceless_gles2 not available\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100293 return -1;
294 }
295
296 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
297 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
298 return -1;
299 }
Benjamin Franzkebe014562011-02-18 17:04:24 +0100300 if (!eglChooseConfig(c->base.display, config_attribs,
301 &c->base.config, 1, &n) || n == 0) {
302 fprintf(stderr, "failed to choose config: %d\n", n);
303 return -1;
304 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100305
Benjamin Franzkebe014562011-02-18 17:04:24 +0100306 c->base.context = eglCreateContext(c->base.display, c->base.config,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100307 EGL_NO_CONTEXT, context_attribs);
308 if (c->base.context == NULL) {
309 fprintf(stderr, "failed to create context\n");
310 return -1;
311 }
312
313 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
314 EGL_NO_SURFACE, c->base.context)) {
315 fprintf(stderr, "failed to make context current\n");
316 return -1;
317 }
318
319 return 0;
320}
321
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500322static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500323frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400324{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500325 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400326
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500327 wl_callback_destroy(callback);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500328 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400329}
330
331static const struct wl_callback_listener frame_listener = {
332 frame_done
333};
334
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500335static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500336wayland_output_repaint(struct weston_output *output_base,
337 pixman_region32_t *damage)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100338{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100339 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500340 struct wayland_compositor *compositor =
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100341 (struct wayland_compositor *) output->base.compositor;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400342 struct wl_callback *callback;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500343 struct weston_surface *surface;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100344
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500345 if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
346 output->egl_surface, compositor->base.context)) {
347 fprintf(stderr, "failed to make current\n");
348 return;
349 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100350
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500351 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500352 weston_surface_draw(surface, &output->base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500353
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500354 draw_border(output);
355
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500356 eglSwapBuffers(compositor->base.display, output->egl_surface);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400357 callback = wl_surface_frame(output->parent.surface);
358 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100359
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500360 return;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100361}
362
Matt Roper361d2ad2011-08-29 13:52:23 -0700363static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500364wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700365{
366 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500367 struct weston_compositor *ec = output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700368
369 eglDestroySurface(ec->display, output->egl_surface);
370 wl_egl_window_destroy(output->parent.egl_window);
371 free(output);
372
373 return;
374}
375
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200376static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100377wayland_compositor_create_output(struct wayland_compositor *c,
378 int width, int height)
379{
380 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100381
382 output = malloc(sizeof *output);
383 if (output == NULL)
384 return -1;
385 memset(output, 0, sizeof *output);
386
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400387 output->mode.flags =
388 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
389 output->mode.width = width;
390 output->mode.height = height;
391 output->mode.refresh = 60;
392 wl_list_init(&output->base.mode_list);
393 wl_list_insert(&output->base.mode_list, &output->mode.link);
394
395 output->base.current = &output->mode;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500396 weston_output_init(&output->base, &c->base, 0, 0, width, height,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100397 WL_OUTPUT_FLIPPED);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400398
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500399 output->base.border.top = c->border.top;
400 output->base.border.bottom = c->border.bottom;
401 output->base.border.left = c->border.left;
402 output->base.border.right = c->border.right;
403
404 weston_output_move(&output->base, 0, 0);
405
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100406 output->parent.surface =
407 wl_compositor_create_surface(c->parent.compositor);
408 wl_surface_set_user_data(output->parent.surface, output);
409
Benjamin Franzkebe014562011-02-18 17:04:24 +0100410 output->parent.egl_window =
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500411 wl_egl_window_create(output->parent.surface,
412 width + c->border.left + c->border.right,
413 height + c->border.top + c->border.bottom);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100414 if (!output->parent.egl_window) {
415 fprintf(stderr, "failure to create wl_egl_window\n");
416 goto cleanup_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100417 }
418
Benjamin Franzkebe014562011-02-18 17:04:24 +0100419 output->egl_surface =
420 eglCreateWindowSurface(c->base.display, c->base.config,
421 output->parent.egl_window, NULL);
422 if (!output->egl_surface) {
423 fprintf(stderr, "failed to create window surface\n");
424 goto cleanup_window;
425 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100426
Benjamin Franzkebe014562011-02-18 17:04:24 +0100427 if (!eglMakeCurrent(c->base.display, output->egl_surface,
428 output->egl_surface, c->base.context)) {
429 fprintf(stderr, "failed to make surface current\n");
430 goto cleanup_surface;
431 return -1;
432 }
433
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200434 output->parent.shell_surface =
435 wl_shell_get_shell_surface(c->parent.shell,
436 output->parent.surface);
437 /* FIXME: add shell_surface listener for resizing */
438 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100439
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500440 output->base.repaint = wayland_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700441 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800442 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200443 output->base.set_backlight = NULL;
444 output->base.set_dpms = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100445
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100446 wl_list_insert(c->base.output_list.prev, &output->base.link);
447
448 return 0;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100449
450cleanup_surface:
451 eglDestroySurface(c->base.display, output->egl_surface);
452cleanup_window:
453 wl_egl_window_destroy(output->parent.egl_window);
454cleanup_output:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500455 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +0100456 free(output);
457
458 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100459}
460
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100461/* Events received from the wayland-server this compositor is client of: */
462
463/* parent output interface */
464static void
465display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400466 struct wl_output *wl_output,
467 int x,
468 int y,
469 int physical_width,
470 int physical_height,
471 int subpixel,
472 const char *make,
473 const char *model)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100474{
475 struct wayland_compositor *c = data;
476
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500477 c->parent.screen_allocation.x = x;
478 c->parent.screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400479}
480
481static void
482display_handle_mode(void *data,
483 struct wl_output *wl_output,
484 uint32_t flags,
485 int width,
486 int height,
487 int refresh)
488{
489 struct wayland_compositor *c = data;
490
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100491 c->parent.screen_allocation.width = width;
492 c->parent.screen_allocation.height = height;
493}
494
495static const struct wl_output_listener output_listener = {
496 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400497 display_handle_mode
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100498};
499
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100500/* parent input interface */
501static void
502input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200503 uint32_t time, int32_t sx, int32_t sy)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100504{
505 struct wayland_input *input = data;
506 struct wayland_compositor *c = input->compositor;
507
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500508 notify_motion(c->base.input_device, time,
509 sx - c->border.left, sy - c->border.top);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100510}
511
512static void
513input_handle_button(void *data,
514 struct wl_input_device *input_device,
515 uint32_t time, uint32_t button, uint32_t state)
516{
517 struct wayland_input *input = data;
518 struct wayland_compositor *c = input->compositor;
519
520 notify_button(c->base.input_device, time, button, state);
521}
522
523static void
524input_handle_key(void *data, struct wl_input_device *input_device,
525 uint32_t time, uint32_t key, uint32_t state)
526{
527 struct wayland_input *input = data;
528 struct wayland_compositor *c = input->compositor;
529
530 notify_key(c->base.input_device, time, key, state);
531}
532
533static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500534input_handle_pointer_enter(void *data,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200535 struct wl_input_device *input_device,
536 uint32_t time, struct wl_surface *surface,
537 int32_t sx, int32_t sy)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100538{
539 struct wayland_input *input = data;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500540 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100541 struct wayland_compositor *c = input->compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100542
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500543 output = wl_surface_get_user_data(surface);
544 notify_pointer_focus(c->base.input_device,
545 time, &output->base, sx, sy);
546 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100547}
548
549static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500550input_handle_pointer_leave(void *data,
551 struct wl_input_device *input_device,
552 uint32_t time, struct wl_surface *surface)
553{
554 struct wayland_input *input = data;
555 struct wayland_compositor *c = input->compositor;
556
557 notify_pointer_focus(c->base.input_device, time, NULL, 0, 0);
558}
559
560static void
561input_handle_keyboard_enter(void *data,
562 struct wl_input_device *input_device,
563 uint32_t time,
564 struct wl_surface *surface,
565 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100566{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500567 struct wayland_input *input = data;
568 struct wayland_compositor *c = input->compositor;
569 struct wayland_output *output;
570
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500571 output = wl_surface_get_user_data(surface);
572 notify_keyboard_focus(c->base.input_device, time, &output->base, keys);
573}
574
575static void
576input_handle_keyboard_leave(void *data,
577 struct wl_input_device *input_device,
578 uint32_t time,
579 struct wl_surface *surface)
580{
581 struct wayland_input *input = data;
582 struct wayland_compositor *c = input->compositor;
583
584 notify_keyboard_focus(c->base.input_device, time, NULL, NULL);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100585}
586
587static const struct wl_input_device_listener input_device_listener = {
588 input_handle_motion,
589 input_handle_button,
590 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500591 input_handle_pointer_enter,
592 input_handle_pointer_leave,
593 input_handle_keyboard_enter,
594 input_handle_keyboard_leave,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100595};
596
597static void
598display_add_input(struct wayland_compositor *c, uint32_t id)
599{
600 struct wayland_input *input;
601
602 input = malloc(sizeof *input);
603 if (input == NULL)
604 return;
605
606 memset(input, 0, sizeof *input);
607
608 input->compositor = c;
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400609 input->input_device = wl_display_bind(c->parent.display,
610 id, &wl_input_device_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100611 wl_list_insert(c->input_list.prev, &input->link);
612
613 wl_input_device_add_listener(input->input_device,
614 &input_device_listener, input);
615 wl_input_device_set_user_data(input->input_device, input);
616}
617
618static void
619display_handle_global(struct wl_display *display, uint32_t id,
620 const char *interface, uint32_t version, void *data)
621{
622 struct wayland_compositor *c = data;
623
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200624 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400625 c->parent.compositor =
626 wl_display_bind(display, id, &wl_compositor_interface);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200627 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400628 c->parent.output =
629 wl_display_bind(display, id, &wl_output_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100630 wl_output_add_listener(c->parent.output, &output_listener, c);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200631 } else if (strcmp(interface, "wl_input_device") == 0) {
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100632 display_add_input(c, id);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200633 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400634 c->parent.shell =
635 wl_display_bind(display, id, &wl_shell_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100636 }
637}
638
639static int
640update_event_mask(uint32_t mask, void *data)
641{
642 struct wayland_compositor *c = data;
643
644 c->parent.event_mask = mask;
645 if (c->parent.wl_source)
646 wl_event_source_fd_update(c->parent.wl_source, mask);
647
648 return 0;
649}
650
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400651static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100652wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
653{
654 struct wayland_compositor *c = data;
655
656 if (mask & WL_EVENT_READABLE)
657 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
Kristian Høgsbergf258a312011-12-28 22:51:20 -0500658 if (mask & WL_EVENT_WRITABLE)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100659 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400660
661 return 1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100662}
663
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500664static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500665wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500666{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500667 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -0700668
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500669 free(ec);
670}
671
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500672static struct weston_compositor *
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500673wayland_compositor_create(struct wl_display *display,
674 int width, int height, const char *display_name)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100675{
676 struct wayland_compositor *c;
677 struct wl_event_loop *loop;
678 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100679
680 c = malloc(sizeof *c);
681 if (c == NULL)
682 return NULL;
683
684 memset(c, 0, sizeof *c);
685
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500686 c->parent.display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100687
688 if (c->parent.display == NULL) {
689 fprintf(stderr, "failed to create display: %m\n");
690 return NULL;
691 }
692
693 wl_list_init(&c->input_list);
694 wl_display_add_global_listener(c->parent.display,
695 display_handle_global, c);
696
697 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
698
699 c->base.wl_display = display;
700 if (wayland_compositor_init_egl(c) < 0)
701 return NULL;
702
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100703 c->base.destroy = wayland_destroy;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100704
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100705 /* Can't init base class until we have a current egl context */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500706 if (weston_compositor_init(&c->base, display) < 0)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100707 return NULL;
708
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500709 create_border(c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100710 if (wayland_compositor_create_output(c, width, height) < 0)
711 return NULL;
712
713 if (wayland_input_create(c) < 0)
714 return NULL;
715
716 loop = wl_display_get_event_loop(c->base.wl_display);
717
718 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
719 c->parent.wl_source =
720 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
721 wayland_compositor_handle_event, c);
722 if (c->parent.wl_source == NULL)
723 return NULL;
724
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100725 return &c->base;
726}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400727
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500728WL_EXPORT struct weston_compositor *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400729backend_init(struct wl_display *display, int argc, char *argv[])
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400730{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400731 int width = 1024, height = 640;
732 char *display_name = NULL;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400733
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400734 const struct weston_option wayland_options[] = {
735 { WESTON_OPTION_INTEGER, "width", 0, &width },
736 { WESTON_OPTION_INTEGER, "height", 0, &height },
737 { WESTON_OPTION_STRING, "display", 0, &display_name },
738 };
739
740 parse_options(wayland_options,
741 ARRAY_LENGTH(wayland_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400742
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500743 return wayland_compositor_create(display, width, height, display_name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400744}