blob: 3260c8e038145942abc6456d3bf20e90596f95bb [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;
49 EGLSurface dummy_egl_surface;;
50
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;
88 struct wl_input_device *input_device;
89 struct wl_list link;
90};
91
Kristian Høgsberg546a8122012-02-01 07:45:51 -050092
93static int
94texture_border(struct wayland_output *output)
95{
96 struct wayland_compositor *c =
97 (struct wayland_compositor *) output->base.compositor;
98 GLfloat *d;
99 unsigned int *p;
100 int i, j, k, n;
101 GLfloat x[4], y[4], u[4], v[4];
102
103 x[0] = -c->border.left;
104 x[1] = 0;
105 x[2] = output->base.current->width;
106 x[3] = output->base.current->width + c->border.right;
107
108 y[0] = -c->border.top;
109 y[1] = 0;
110 y[2] = output->base.current->height;
111 y[3] = output->base.current->height + c->border.bottom;
112
113 u[0] = 0.0;
114 u[1] = (GLfloat) c->border.left / c->border.width;
115 u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
116 u[3] = 1.0;
117
118 v[0] = 0.0;
119 v[1] = (GLfloat) c->border.top / c->border.height;
120 v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
121 v[3] = 1.0;
122
123 n = 8;
124 d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
125 p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
126
127 k = 0;
128 for (i = 0; i < 3; i++)
129 for (j = 0; j < 3; j++) {
130
131 if (i == 1 && j == 1)
132 continue;
133
134 d[ 0] = x[i];
135 d[ 1] = y[j];
136 d[ 2] = u[i];
137 d[ 3] = v[j];
138
139 d[ 4] = x[i];
140 d[ 5] = y[j + 1];
141 d[ 6] = u[i];
142 d[ 7] = v[j + 1];
143
144 d[ 8] = x[i + 1];
145 d[ 9] = y[j];
146 d[10] = u[i + 1];
147 d[11] = v[j];
148
149 d[12] = x[i + 1];
150 d[13] = y[j + 1];
151 d[14] = u[i + 1];
152 d[15] = v[j + 1];
153
154 p[0] = k + 0;
155 p[1] = k + 1;
156 p[2] = k + 2;
157 p[3] = k + 2;
158 p[4] = k + 1;
159 p[5] = k + 3;
160
161 d += 16;
162 p += 6;
163 k += 4;
164 }
165
166 return k / 4;
167}
168
169static void
170draw_border(struct wayland_output *output)
171{
172 struct wayland_compositor *c =
173 (struct wayland_compositor *) output->base.compositor;
174 struct weston_shader *shader = &c->base.texture_shader;
175 GLfloat *v;
176 int n;
177
178 glDisable(GL_BLEND);
179 glUseProgram(shader->program);
180 c->base.current_shader = shader;
181
182 glUniformMatrix4fv(shader->proj_uniform,
183 1, GL_FALSE, output->base.matrix.d);
184
185 glUniform1i(shader->tex_uniform, 0);
186 glUniform1f(shader->alpha_uniform, 1);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500187 glUniform1f(shader->texwidth_uniform, 1);
188
189 n = texture_border(output);
190
191 glBindTexture(GL_TEXTURE_2D, c->border.texture);
192
193 v = c->base.vertices.data;
194 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
195 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
196 glEnableVertexAttribArray(0);
197 glEnableVertexAttribArray(1);
198
199 glDrawElements(GL_TRIANGLES, n * 6,
200 GL_UNSIGNED_INT, c->base.indices.data);
201
202 glDisableVertexAttribArray(1);
203 glDisableVertexAttribArray(0);
204
205 c->base.vertices.size = 0;
206 c->base.indices.size = 0;
207}
208
209static void
210create_border(struct wayland_compositor *c)
211{
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200212 pixman_image_t *image;
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500213
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200214 image = load_image(DATADIR "/weston/border.png");
215 if (!image) {
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500216 fprintf(stderr, "could'nt load border image\n");
217 return;
218 }
219
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200220 c->border.width = pixman_image_get_width(image);
221 c->border.height = pixman_image_get_height(image);
222
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500223 glGenTextures(1, &c->border.texture);
224 glBindTexture(GL_TEXTURE_2D, c->border.texture);
225 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
226 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
227 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
228 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
229
230 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
231 c->border.width,
232 c->border.height,
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200233 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
234 pixman_image_get_data(image));
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500235
236 c->border.top = 25;
237 c->border.bottom = 50;
238 c->border.left = 25;
239 c->border.right = 25;
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200240
241 pixman_image_unref(image);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500242}
243
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100244static int
245wayland_input_create(struct wayland_compositor *c)
246{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500247 struct weston_input_device *input;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100248
249 input = malloc(sizeof *input);
250 if (input == NULL)
251 return -1;
252
253 memset(input, 0, sizeof *input);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500254 weston_input_device_init(input, &c->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100255
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500256 c->base.input_device = &input->input_device;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100257
258 return 0;
259}
260
261static int
262wayland_compositor_init_egl(struct wayland_compositor *c)
263{
264 EGLint major, minor;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100265 EGLint n;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100266 EGLint config_attribs[] = {
267 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
268 EGL_RED_SIZE, 1,
269 EGL_GREEN_SIZE, 1,
270 EGL_BLUE_SIZE, 1,
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500271 EGL_ALPHA_SIZE, 1,
Kristian Høgsbergd28ab362011-03-02 11:36:30 -0500272 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100273 EGL_NONE
274 };
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100275 static const EGLint context_attribs[] = {
276 EGL_CONTEXT_CLIENT_VERSION, 2,
277 EGL_NONE
278 };
279
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400280 c->base.display = eglGetDisplay(c->parent.display);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100281 if (c->base.display == NULL) {
282 fprintf(stderr, "failed to create display\n");
283 return -1;
284 }
285
286 if (!eglInitialize(c->base.display, &major, &minor)) {
287 fprintf(stderr, "failed to initialize display\n");
288 return -1;
289 }
290
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100291 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
292 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
293 return -1;
294 }
Benjamin Franzkebe014562011-02-18 17:04:24 +0100295 if (!eglChooseConfig(c->base.display, config_attribs,
296 &c->base.config, 1, &n) || n == 0) {
297 fprintf(stderr, "failed to choose config: %d\n", n);
298 return -1;
299 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100300
Benjamin Franzkebe014562011-02-18 17:04:24 +0100301 c->base.context = eglCreateContext(c->base.display, c->base.config,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100302 EGL_NO_CONTEXT, context_attribs);
303 if (c->base.context == NULL) {
304 fprintf(stderr, "failed to create context\n");
305 return -1;
306 }
307
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400308 c->dummy_pixmap = wl_egl_pixmap_create(10, 10, 0);
309 if (!c->dummy_pixmap) {
310 fprintf(stderr, "failure to create dummy_pixmap\n");
311 return -1;
312 }
313
314 c->dummy_egl_surface =
315 eglCreatePixmapSurface(c->base.display, c->base.config,
316 c->dummy_pixmap, NULL);
317 if (!eglMakeCurrent(c->base.display, c->dummy_egl_surface,
318 c->dummy_egl_surface, c->base.context)) {
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100319 fprintf(stderr, "failed to make context current\n");
320 return -1;
321 }
322
323 return 0;
324}
325
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500326static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500327frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400328{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500329 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400330
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500331 wl_callback_destroy(callback);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500332 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400333}
334
335static const struct wl_callback_listener frame_listener = {
336 frame_done
337};
338
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500339static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500340wayland_output_repaint(struct weston_output *output_base,
341 pixman_region32_t *damage)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100342{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100343 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500344 struct wayland_compositor *compositor =
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100345 (struct wayland_compositor *) output->base.compositor;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400346 struct wl_callback *callback;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500347 struct weston_surface *surface;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100348
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500349 if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
350 output->egl_surface, compositor->base.context)) {
351 fprintf(stderr, "failed to make current\n");
352 return;
353 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100354
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500355 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500356 weston_surface_draw(surface, &output->base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500357
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500358 draw_border(output);
359
Scott Moreau062be7e2012-04-20 13:37:33 -0600360 weston_output_do_read_pixels(&output->base);
361
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500362 eglSwapBuffers(compositor->base.display, output->egl_surface);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400363 callback = wl_surface_frame(output->parent.surface);
364 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100365
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500366 return;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100367}
368
Matt Roper361d2ad2011-08-29 13:52:23 -0700369static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500370wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700371{
372 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500373 struct weston_compositor *ec = output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700374
375 eglDestroySurface(ec->display, output->egl_surface);
376 wl_egl_window_destroy(output->parent.egl_window);
377 free(output);
378
379 return;
380}
381
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200382static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100383wayland_compositor_create_output(struct wayland_compositor *c,
384 int width, int height)
385{
386 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100387
388 output = malloc(sizeof *output);
389 if (output == NULL)
390 return -1;
391 memset(output, 0, sizeof *output);
392
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400393 output->mode.flags =
394 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
395 output->mode.width = width;
396 output->mode.height = height;
397 output->mode.refresh = 60;
398 wl_list_init(&output->base.mode_list);
399 wl_list_insert(&output->base.mode_list, &output->mode.link);
400
401 output->base.current = &output->mode;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500402 weston_output_init(&output->base, &c->base, 0, 0, width, height,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100403 WL_OUTPUT_FLIPPED);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400404
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500405 output->base.border.top = c->border.top;
406 output->base.border.bottom = c->border.bottom;
407 output->base.border.left = c->border.left;
408 output->base.border.right = c->border.right;
409
410 weston_output_move(&output->base, 0, 0);
411
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100412 output->parent.surface =
413 wl_compositor_create_surface(c->parent.compositor);
414 wl_surface_set_user_data(output->parent.surface, output);
415
Benjamin Franzkebe014562011-02-18 17:04:24 +0100416 output->parent.egl_window =
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500417 wl_egl_window_create(output->parent.surface,
418 width + c->border.left + c->border.right,
419 height + c->border.top + c->border.bottom);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100420 if (!output->parent.egl_window) {
421 fprintf(stderr, "failure to create wl_egl_window\n");
422 goto cleanup_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100423 }
424
Benjamin Franzkebe014562011-02-18 17:04:24 +0100425 output->egl_surface =
426 eglCreateWindowSurface(c->base.display, c->base.config,
427 output->parent.egl_window, NULL);
428 if (!output->egl_surface) {
429 fprintf(stderr, "failed to create window surface\n");
430 goto cleanup_window;
431 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100432
Benjamin Franzkebe014562011-02-18 17:04:24 +0100433 if (!eglMakeCurrent(c->base.display, output->egl_surface,
434 output->egl_surface, c->base.context)) {
435 fprintf(stderr, "failed to make surface current\n");
436 goto cleanup_surface;
437 return -1;
438 }
439
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200440 output->parent.shell_surface =
441 wl_shell_get_shell_surface(c->parent.shell,
442 output->parent.surface);
443 /* FIXME: add shell_surface listener for resizing */
444 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100445
Alex Wubd3354b2012-04-17 17:20:49 +0800446 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500447 output->base.repaint = wayland_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700448 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800449 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200450 output->base.set_backlight = NULL;
451 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800452 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100453
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100454 wl_list_insert(c->base.output_list.prev, &output->base.link);
455
456 return 0;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100457
458cleanup_surface:
459 eglDestroySurface(c->base.display, output->egl_surface);
460cleanup_window:
461 wl_egl_window_destroy(output->parent.egl_window);
462cleanup_output:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500463 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +0100464 free(output);
465
466 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100467}
468
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100469/* Events received from the wayland-server this compositor is client of: */
470
471/* parent output interface */
472static void
473display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400474 struct wl_output *wl_output,
475 int x,
476 int y,
477 int physical_width,
478 int physical_height,
479 int subpixel,
480 const char *make,
481 const char *model)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100482{
483 struct wayland_compositor *c = data;
484
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500485 c->parent.screen_allocation.x = x;
486 c->parent.screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400487}
488
489static void
490display_handle_mode(void *data,
491 struct wl_output *wl_output,
492 uint32_t flags,
493 int width,
494 int height,
495 int refresh)
496{
497 struct wayland_compositor *c = data;
498
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100499 c->parent.screen_allocation.width = width;
500 c->parent.screen_allocation.height = height;
501}
502
503static const struct wl_output_listener output_listener = {
504 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400505 display_handle_mode
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100506};
507
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100508/* parent input interface */
509static void
510input_handle_motion(void *data, struct wl_input_device *input_device,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200511 uint32_t time, int32_t sx, int32_t sy)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100512{
513 struct wayland_input *input = data;
514 struct wayland_compositor *c = input->compositor;
515
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500516 notify_motion(c->base.input_device, time,
517 sx - c->border.left, sy - c->border.top);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100518}
519
520static void
521input_handle_button(void *data,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400522 struct wl_input_device *input_device,
523 uint32_t serial, uint32_t time, uint32_t button, uint32_t state)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100524{
525 struct wayland_input *input = data;
526 struct wayland_compositor *c = input->compositor;
527
528 notify_button(c->base.input_device, time, button, state);
529}
530
531static void
Scott Moreau210d0792012-03-22 10:47:01 -0600532input_handle_axis(void *data, struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400533 uint32_t time, uint32_t axis, int32_t value)
Scott Moreau210d0792012-03-22 10:47:01 -0600534{
535 struct wayland_input *input = data;
536 struct wayland_compositor *c = input->compositor;
537
538 notify_axis(c->base.input_device, time, axis, value);
539}
540
541static void
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100542input_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400543 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100544{
545 struct wayland_input *input = data;
546 struct wayland_compositor *c = input->compositor;
547
548 notify_key(c->base.input_device, time, key, state);
549}
550
551static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500552input_handle_pointer_enter(void *data,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200553 struct wl_input_device *input_device,
554 uint32_t time, struct wl_surface *surface,
555 int32_t sx, int32_t sy)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100556{
557 struct wayland_input *input = data;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500558 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100559 struct wayland_compositor *c = input->compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100560
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500561 output = wl_surface_get_user_data(surface);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400562 notify_pointer_focus(c->base.input_device, &output->base, sx, sy);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500563 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100564}
565
566static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500567input_handle_pointer_leave(void *data,
568 struct wl_input_device *input_device,
569 uint32_t time, struct wl_surface *surface)
570{
571 struct wayland_input *input = data;
572 struct wayland_compositor *c = input->compositor;
573
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400574 notify_pointer_focus(c->base.input_device, NULL, 0, 0);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500575}
576
577static void
578input_handle_keyboard_enter(void *data,
579 struct wl_input_device *input_device,
580 uint32_t time,
581 struct wl_surface *surface,
582 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100583{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500584 struct wayland_input *input = data;
585 struct wayland_compositor *c = input->compositor;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500586
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400587 notify_keyboard_focus(c->base.input_device, keys);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500588}
589
590static void
591input_handle_keyboard_leave(void *data,
592 struct wl_input_device *input_device,
593 uint32_t time,
594 struct wl_surface *surface)
595{
596 struct wayland_input *input = data;
597 struct wayland_compositor *c = input->compositor;
598
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400599 notify_keyboard_focus(c->base.input_device, NULL);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100600}
601
602static const struct wl_input_device_listener input_device_listener = {
603 input_handle_motion,
604 input_handle_button,
Scott Moreau210d0792012-03-22 10:47:01 -0600605 input_handle_axis,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100606 input_handle_key,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500607 input_handle_pointer_enter,
608 input_handle_pointer_leave,
609 input_handle_keyboard_enter,
610 input_handle_keyboard_leave,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100611};
612
613static void
614display_add_input(struct wayland_compositor *c, uint32_t id)
615{
616 struct wayland_input *input;
617
618 input = malloc(sizeof *input);
619 if (input == NULL)
620 return;
621
622 memset(input, 0, sizeof *input);
623
624 input->compositor = c;
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400625 input->input_device = wl_display_bind(c->parent.display,
626 id, &wl_input_device_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100627 wl_list_insert(c->input_list.prev, &input->link);
628
629 wl_input_device_add_listener(input->input_device,
630 &input_device_listener, input);
631 wl_input_device_set_user_data(input->input_device, input);
632}
633
634static void
635display_handle_global(struct wl_display *display, uint32_t id,
636 const char *interface, uint32_t version, void *data)
637{
638 struct wayland_compositor *c = data;
639
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200640 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400641 c->parent.compositor =
642 wl_display_bind(display, id, &wl_compositor_interface);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200643 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400644 c->parent.output =
645 wl_display_bind(display, id, &wl_output_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100646 wl_output_add_listener(c->parent.output, &output_listener, c);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200647 } else if (strcmp(interface, "wl_input_device") == 0) {
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100648 display_add_input(c, id);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200649 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400650 c->parent.shell =
651 wl_display_bind(display, id, &wl_shell_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100652 }
653}
654
655static int
656update_event_mask(uint32_t mask, void *data)
657{
658 struct wayland_compositor *c = data;
659
660 c->parent.event_mask = mask;
661 if (c->parent.wl_source)
662 wl_event_source_fd_update(c->parent.wl_source, mask);
663
664 return 0;
665}
666
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400667static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100668wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
669{
670 struct wayland_compositor *c = data;
671
672 if (mask & WL_EVENT_READABLE)
673 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
Kristian Høgsbergf258a312011-12-28 22:51:20 -0500674 if (mask & WL_EVENT_WRITABLE)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100675 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400676
677 return 1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100678}
679
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500680static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500681wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500682{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500683 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -0700684
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500685 free(ec);
686}
687
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500688static struct weston_compositor *
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500689wayland_compositor_create(struct wl_display *display,
690 int width, int height, const char *display_name)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100691{
692 struct wayland_compositor *c;
693 struct wl_event_loop *loop;
694 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100695
696 c = malloc(sizeof *c);
697 if (c == NULL)
698 return NULL;
699
700 memset(c, 0, sizeof *c);
701
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500702 c->parent.display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100703
704 if (c->parent.display == NULL) {
705 fprintf(stderr, "failed to create display: %m\n");
706 return NULL;
707 }
708
709 wl_list_init(&c->input_list);
710 wl_display_add_global_listener(c->parent.display,
711 display_handle_global, c);
712
713 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
714
715 c->base.wl_display = display;
716 if (wayland_compositor_init_egl(c) < 0)
717 return NULL;
718
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100719 c->base.destroy = wayland_destroy;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100720
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100721 /* Can't init base class until we have a current egl context */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500722 if (weston_compositor_init(&c->base, display) < 0)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100723 return NULL;
724
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500725 create_border(c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100726 if (wayland_compositor_create_output(c, width, height) < 0)
727 return NULL;
728
729 if (wayland_input_create(c) < 0)
730 return NULL;
731
732 loop = wl_display_get_event_loop(c->base.wl_display);
733
734 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
735 c->parent.wl_source =
736 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
737 wayland_compositor_handle_event, c);
738 if (c->parent.wl_source == NULL)
739 return NULL;
740
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100741 return &c->base;
742}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400743
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500744WL_EXPORT struct weston_compositor *
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400745backend_init(struct wl_display *display, int argc, char *argv[])
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400746{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400747 int width = 1024, height = 640;
748 char *display_name = NULL;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400749
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400750 const struct weston_option wayland_options[] = {
751 { WESTON_OPTION_INTEGER, "width", 0, &width },
752 { WESTON_OPTION_INTEGER, "height", 0, &height },
753 { WESTON_OPTION_STRING, "display", 0, &display_name },
754 };
755
756 parse_options(wayland_options,
757 ARRAY_LENGTH(wayland_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400758
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500759 return wayland_compositor_create(display, width, height, display_name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400760}