blob: fc1917d71e3e70aa65ace45b75fabc411319e890 [file] [log] [blame]
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Benjamin Franzke
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01003 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04004 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010013 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040014 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010021 */
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
27#include <stddef.h>
28#define _GNU_SOURCE
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <fcntl.h>
33#include <unistd.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010034#include <sys/mman.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010035
Benjamin Franzkebe014562011-02-18 17:04:24 +010036#include <wayland-client.h>
37#include <wayland-egl.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010038
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010039#include <GLES2/gl2.h>
40#include <GLES2/gl2ext.h>
41#include <EGL/egl.h>
42#include <EGL/eglext.h>
43
44#include "compositor.h"
Martin Minarik6d118362012-06-07 18:01:59 +020045#include "log.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010046
47struct wayland_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050048 struct weston_compositor base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010049
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -040050 struct wl_egl_pixmap *dummy_pixmap;
Kristian Høgsbergb71302e2012-05-10 12:28:35 -040051 EGLSurface dummy_egl_surface;
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -040052
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010053 struct {
54 struct wl_display *display;
55 struct wl_compositor *compositor;
56 struct wl_shell *shell;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010057 struct wl_output *output;
58
59 struct {
60 int32_t x, y, width, height;
61 } screen_allocation;
62
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010063 struct wl_event_source *wl_source;
64 uint32_t event_mask;
65 } parent;
66
Kristian Høgsberg546a8122012-02-01 07:45:51 -050067 struct {
68 int32_t top, bottom, left, right;
69 GLuint texture;
70 int32_t width, height;
71 } border;
72
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010073 struct wl_list input_list;
74};
75
76struct wayland_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050077 struct weston_output base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010078
79 struct {
80 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020081 struct wl_shell_surface *shell_surface;
Benjamin Franzkebe014562011-02-18 17:04:24 +010082 struct wl_egl_window *egl_window;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010083 } parent;
Benjamin Franzkebe014562011-02-18 17:04:24 +010084 EGLSurface egl_surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050085 struct weston_mode mode;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010086};
87
88struct wayland_input {
89 struct wayland_compositor *compositor;
Daniel Stone37816df2012-05-16 18:45:18 +010090 struct wl_seat *seat;
91 struct wl_pointer *pointer;
92 struct wl_keyboard *keyboard;
93 struct wl_touch *touch;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010094 struct wl_list link;
95};
96
Kristian Høgsberg546a8122012-02-01 07:45:51 -050097
98static int
99texture_border(struct wayland_output *output)
100{
101 struct wayland_compositor *c =
102 (struct wayland_compositor *) output->base.compositor;
103 GLfloat *d;
104 unsigned int *p;
105 int i, j, k, n;
106 GLfloat x[4], y[4], u[4], v[4];
107
108 x[0] = -c->border.left;
109 x[1] = 0;
110 x[2] = output->base.current->width;
111 x[3] = output->base.current->width + c->border.right;
112
113 y[0] = -c->border.top;
114 y[1] = 0;
115 y[2] = output->base.current->height;
116 y[3] = output->base.current->height + c->border.bottom;
117
118 u[0] = 0.0;
119 u[1] = (GLfloat) c->border.left / c->border.width;
120 u[2] = (GLfloat) (c->border.width - c->border.right) / c->border.width;
121 u[3] = 1.0;
122
123 v[0] = 0.0;
124 v[1] = (GLfloat) c->border.top / c->border.height;
125 v[2] = (GLfloat) (c->border.height - c->border.bottom) / c->border.height;
126 v[3] = 1.0;
127
128 n = 8;
129 d = wl_array_add(&c->base.vertices, n * 16 * sizeof *d);
130 p = wl_array_add(&c->base.indices, n * 6 * sizeof *p);
131
132 k = 0;
133 for (i = 0; i < 3; i++)
134 for (j = 0; j < 3; j++) {
135
136 if (i == 1 && j == 1)
137 continue;
138
139 d[ 0] = x[i];
140 d[ 1] = y[j];
141 d[ 2] = u[i];
142 d[ 3] = v[j];
143
144 d[ 4] = x[i];
145 d[ 5] = y[j + 1];
146 d[ 6] = u[i];
147 d[ 7] = v[j + 1];
148
149 d[ 8] = x[i + 1];
150 d[ 9] = y[j];
151 d[10] = u[i + 1];
152 d[11] = v[j];
153
154 d[12] = x[i + 1];
155 d[13] = y[j + 1];
156 d[14] = u[i + 1];
157 d[15] = v[j + 1];
158
159 p[0] = k + 0;
160 p[1] = k + 1;
161 p[2] = k + 2;
162 p[3] = k + 2;
163 p[4] = k + 1;
164 p[5] = k + 3;
165
166 d += 16;
167 p += 6;
168 k += 4;
169 }
170
171 return k / 4;
172}
173
174static void
175draw_border(struct wayland_output *output)
176{
177 struct wayland_compositor *c =
178 (struct wayland_compositor *) output->base.compositor;
179 struct weston_shader *shader = &c->base.texture_shader;
180 GLfloat *v;
181 int n;
182
183 glDisable(GL_BLEND);
184 glUseProgram(shader->program);
185 c->base.current_shader = shader;
186
187 glUniformMatrix4fv(shader->proj_uniform,
188 1, GL_FALSE, output->base.matrix.d);
189
190 glUniform1i(shader->tex_uniform, 0);
191 glUniform1f(shader->alpha_uniform, 1);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500192 glUniform1f(shader->texwidth_uniform, 1);
193
194 n = texture_border(output);
195
196 glBindTexture(GL_TEXTURE_2D, c->border.texture);
197
198 v = c->base.vertices.data;
199 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
200 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
201 glEnableVertexAttribArray(0);
202 glEnableVertexAttribArray(1);
203
204 glDrawElements(GL_TRIANGLES, n * 6,
205 GL_UNSIGNED_INT, c->base.indices.data);
206
207 glDisableVertexAttribArray(1);
208 glDisableVertexAttribArray(0);
209
210 c->base.vertices.size = 0;
211 c->base.indices.size = 0;
212}
213
214static void
215create_border(struct wayland_compositor *c)
216{
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200217 pixman_image_t *image;
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500218
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200219 image = load_image(DATADIR "/weston/border.png");
220 if (!image) {
Martin Minarik6d118362012-06-07 18:01:59 +0200221 weston_log("could'nt load border image\n");
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500222 return;
223 }
224
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200225 c->border.width = pixman_image_get_width(image);
226 c->border.height = pixman_image_get_height(image);
227
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500228 glGenTextures(1, &c->border.texture);
229 glBindTexture(GL_TEXTURE_2D, c->border.texture);
230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
234
235 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
236 c->border.width,
237 c->border.height,
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200238 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
239 pixman_image_get_data(image));
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500240
241 c->border.top = 25;
242 c->border.bottom = 50;
243 c->border.left = 25;
244 c->border.right = 25;
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200245
246 pixman_image_unref(image);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500247}
248
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100249static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100250wayland_compositor_init_egl(struct wayland_compositor *c)
251{
252 EGLint major, minor;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100253 EGLint n;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100254 EGLint config_attribs[] = {
255 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
256 EGL_RED_SIZE, 1,
257 EGL_GREEN_SIZE, 1,
258 EGL_BLUE_SIZE, 1,
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500259 EGL_ALPHA_SIZE, 1,
Kristian Høgsbergd28ab362011-03-02 11:36:30 -0500260 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100261 EGL_NONE
262 };
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100263 static const EGLint context_attribs[] = {
264 EGL_CONTEXT_CLIENT_VERSION, 2,
265 EGL_NONE
266 };
267
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400268 c->base.display = eglGetDisplay(c->parent.display);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100269 if (c->base.display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200270 weston_log("failed to create display\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100271 return -1;
272 }
273
274 if (!eglInitialize(c->base.display, &major, &minor)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200275 weston_log("failed to initialize display\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100276 return -1;
277 }
278
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100279 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200280 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100281 return -1;
282 }
Benjamin Franzkebe014562011-02-18 17:04:24 +0100283 if (!eglChooseConfig(c->base.display, config_attribs,
284 &c->base.config, 1, &n) || n == 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200285 weston_log("failed to choose config: %d\n", n);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100286 return -1;
287 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100288
Benjamin Franzkebe014562011-02-18 17:04:24 +0100289 c->base.context = eglCreateContext(c->base.display, c->base.config,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100290 EGL_NO_CONTEXT, context_attribs);
291 if (c->base.context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200292 weston_log("failed to create context\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100293 return -1;
294 }
295
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400296 c->dummy_pixmap = wl_egl_pixmap_create(10, 10, 0);
297 if (!c->dummy_pixmap) {
Martin Minarik6d118362012-06-07 18:01:59 +0200298 weston_log("failure to create dummy_pixmap\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400299 return -1;
300 }
301
302 c->dummy_egl_surface =
303 eglCreatePixmapSurface(c->base.display, c->base.config,
304 c->dummy_pixmap, NULL);
305 if (!eglMakeCurrent(c->base.display, c->dummy_egl_surface,
306 c->dummy_egl_surface, c->base.context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200307 weston_log("failed to make context current\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100308 return -1;
309 }
310
311 return 0;
312}
313
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500314static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500315frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400316{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500317 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400318
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500319 wl_callback_destroy(callback);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500320 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400321}
322
323static const struct wl_callback_listener frame_listener = {
324 frame_done
325};
326
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500327static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500328wayland_output_repaint(struct weston_output *output_base,
329 pixman_region32_t *damage)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100330{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100331 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500332 struct wayland_compositor *compositor =
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100333 (struct wayland_compositor *) output->base.compositor;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400334 struct wl_callback *callback;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500335 struct weston_surface *surface;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100336
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500337 if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
338 output->egl_surface, compositor->base.context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200339 weston_log("failed to make current\n");
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500340 return;
341 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100342
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500343 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500344 weston_surface_draw(surface, &output->base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500345
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500346 draw_border(output);
347
Scott Moreau062be7e2012-04-20 13:37:33 -0600348 weston_output_do_read_pixels(&output->base);
349
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500350 eglSwapBuffers(compositor->base.display, output->egl_surface);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400351 callback = wl_surface_frame(output->parent.surface);
352 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100353
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500354 return;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100355}
356
Matt Roper361d2ad2011-08-29 13:52:23 -0700357static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500358wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700359{
360 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500361 struct weston_compositor *ec = output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700362
363 eglDestroySurface(ec->display, output->egl_surface);
364 wl_egl_window_destroy(output->parent.egl_window);
365 free(output);
366
367 return;
368}
369
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300370static const struct wl_shell_surface_listener shell_surface_listener;
371
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200372static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100373wayland_compositor_create_output(struct wayland_compositor *c,
374 int width, int height)
375{
376 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100377
378 output = malloc(sizeof *output);
379 if (output == NULL)
380 return -1;
381 memset(output, 0, sizeof *output);
382
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400383 output->mode.flags =
384 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
385 output->mode.width = width;
386 output->mode.height = height;
387 output->mode.refresh = 60;
388 wl_list_init(&output->base.mode_list);
389 wl_list_insert(&output->base.mode_list, &output->mode.link);
390
391 output->base.current = &output->mode;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500392 weston_output_init(&output->base, &c->base, 0, 0, width, height,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100393 WL_OUTPUT_FLIPPED);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400394
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500395 output->base.border.top = c->border.top;
396 output->base.border.bottom = c->border.bottom;
397 output->base.border.left = c->border.left;
398 output->base.border.right = c->border.right;
399
400 weston_output_move(&output->base, 0, 0);
401
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100402 output->parent.surface =
403 wl_compositor_create_surface(c->parent.compositor);
404 wl_surface_set_user_data(output->parent.surface, output);
405
Benjamin Franzkebe014562011-02-18 17:04:24 +0100406 output->parent.egl_window =
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500407 wl_egl_window_create(output->parent.surface,
408 width + c->border.left + c->border.right,
409 height + c->border.top + c->border.bottom);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100410 if (!output->parent.egl_window) {
Martin Minarik6d118362012-06-07 18:01:59 +0200411 weston_log("failure to create wl_egl_window\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100412 goto cleanup_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100413 }
414
Benjamin Franzkebe014562011-02-18 17:04:24 +0100415 output->egl_surface =
416 eglCreateWindowSurface(c->base.display, c->base.config,
417 output->parent.egl_window, NULL);
418 if (!output->egl_surface) {
Martin Minarik6d118362012-06-07 18:01:59 +0200419 weston_log("failed to create window surface\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100420 goto cleanup_window;
421 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100422
Benjamin Franzkebe014562011-02-18 17:04:24 +0100423 if (!eglMakeCurrent(c->base.display, output->egl_surface,
424 output->egl_surface, c->base.context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200425 weston_log("failed to make surface current\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100426 goto cleanup_surface;
427 return -1;
428 }
429
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200430 output->parent.shell_surface =
431 wl_shell_get_shell_surface(c->parent.shell,
432 output->parent.surface);
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300433 wl_shell_surface_add_listener(output->parent.shell_surface,
434 &shell_surface_listener, output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200435 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100436
Alex Wubd3354b2012-04-17 17:20:49 +0800437 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500438 output->base.repaint = wayland_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700439 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800440 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200441 output->base.set_backlight = NULL;
442 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800443 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100444
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100445 wl_list_insert(c->base.output_list.prev, &output->base.link);
446
447 return 0;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100448
449cleanup_surface:
450 eglDestroySurface(c->base.display, output->egl_surface);
451cleanup_window:
452 wl_egl_window_destroy(output->parent.egl_window);
453cleanup_output:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500454 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +0100455 free(output);
456
457 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100458}
459
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300460static void
461shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
462 uint32_t serial)
463{
464 wl_shell_surface_pong(shell_surface, serial);
465}
466
467static void
468shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
469 uint32_t edges, int32_t width, int32_t height)
470{
471 /* FIXME: implement resizing */
472}
473
474static void
475shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
476{
477}
478
479static const struct wl_shell_surface_listener shell_surface_listener = {
480 shell_surface_ping,
481 shell_surface_configure,
482 shell_surface_popup_done
483};
484
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100485/* Events received from the wayland-server this compositor is client of: */
486
487/* parent output interface */
488static void
489display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400490 struct wl_output *wl_output,
491 int x,
492 int y,
493 int physical_width,
494 int physical_height,
495 int subpixel,
496 const char *make,
497 const char *model)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100498{
499 struct wayland_compositor *c = data;
500
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500501 c->parent.screen_allocation.x = x;
502 c->parent.screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400503}
504
505static void
506display_handle_mode(void *data,
507 struct wl_output *wl_output,
508 uint32_t flags,
509 int width,
510 int height,
511 int refresh)
512{
513 struct wayland_compositor *c = data;
514
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100515 c->parent.screen_allocation.width = width;
516 c->parent.screen_allocation.height = height;
517}
518
519static const struct wl_output_listener output_listener = {
520 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400521 display_handle_mode
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100522};
523
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100524/* parent input interface */
525static void
Daniel Stone37816df2012-05-16 18:45:18 +0100526input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
527 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400528 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100529{
530 struct wayland_input *input = data;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500531 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100532 struct wayland_compositor *c = input->compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100533
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500534 output = wl_surface_get_user_data(surface);
Daniel Stone37816df2012-05-16 18:45:18 +0100535 notify_pointer_focus(&c->base.seat->seat, &output->base, x, y);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300536 wl_pointer_set_cursor(input->pointer, serial, NULL, 0, 0);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100537}
538
539static void
Daniel Stone37816df2012-05-16 18:45:18 +0100540input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
541 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500542{
543 struct wayland_input *input = data;
544 struct wayland_compositor *c = input->compositor;
545
Daniel Stone37816df2012-05-16 18:45:18 +0100546 notify_pointer_focus(&c->base.seat->seat, NULL, 0, 0);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500547}
548
549static void
Daniel Stone37816df2012-05-16 18:45:18 +0100550input_handle_motion(void *data, struct wl_pointer *pointer,
551 uint32_t time, wl_fixed_t x, wl_fixed_t y)
552{
553 struct wayland_input *input = data;
554 struct wayland_compositor *c = input->compositor;
555
556 notify_motion(&c->base.seat->seat, time,
557 x - wl_fixed_from_int(c->border.left),
558 y - wl_fixed_from_int(c->border.top));
559}
560
561static void
562input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100563 uint32_t serial, uint32_t time, uint32_t button,
564 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +0100565{
566 struct wayland_input *input = data;
567 struct wayland_compositor *c = input->compositor;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100568 enum wl_pointer_button_state state = state_w;
Daniel Stone37816df2012-05-16 18:45:18 +0100569
570 notify_button(&c->base.seat->seat, time, button, state);
571}
572
573static void
574input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100575 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +0100576{
577 struct wayland_input *input = data;
578 struct wayland_compositor *c = input->compositor;
579
Daniel Stone2fce4022012-05-30 16:32:00 +0100580 notify_axis(&c->base.seat->seat, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +0100581}
582
583static const struct wl_pointer_listener pointer_listener = {
584 input_handle_pointer_enter,
585 input_handle_pointer_leave,
586 input_handle_motion,
587 input_handle_button,
588 input_handle_axis,
589};
590
591static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100592input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
593 int fd, uint32_t size)
594{
595 struct wayland_input *input = data;
596 struct xkb_keymap *keymap;
597 char *map_str;
598
599 if (!data) {
600 close(fd);
601 return;
602 }
603
604 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
605 close(fd);
606 return;
607 }
608
609 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
610 if (map_str == MAP_FAILED) {
611 close(fd);
612 return;
613 }
614
615 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
616 map_str,
617 XKB_KEYMAP_FORMAT_TEXT_V1,
618 0);
619 munmap(map_str, size);
620 close(fd);
621
622 if (!keymap) {
Martin Minarik6d118362012-06-07 18:01:59 +0200623 weston_log("failed to compile keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100624 return;
625 }
626
627 weston_seat_init_keyboard(input->compositor->base.seat, keymap);
628 xkb_map_unref(keymap);
629}
630
631static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500632input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100633 struct wl_keyboard *keyboard,
634 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500635 struct wl_surface *surface,
636 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100637{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500638 struct wayland_input *input = data;
639 struct wayland_compositor *c = input->compositor;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500640
Daniel Stone37816df2012-05-16 18:45:18 +0100641 notify_keyboard_focus(&c->base.seat->seat, keys);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500642}
643
644static void
645input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100646 struct wl_keyboard *keyboard,
647 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500648 struct wl_surface *surface)
649{
650 struct wayland_input *input = data;
651 struct wayland_compositor *c = input->compositor;
652
Daniel Stone37816df2012-05-16 18:45:18 +0100653 notify_keyboard_focus(&c->base.seat->seat, NULL);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100654}
655
Daniel Stone37816df2012-05-16 18:45:18 +0100656static void
657input_handle_key(void *data, struct wl_keyboard *keyboard,
658 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
659{
660 struct wayland_input *input = data;
661 struct wayland_compositor *c = input->compositor;
662
Daniel Stonec9785ea2012-05-30 16:31:52 +0100663 notify_key(&c->base.seat->seat, time, key,
664 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
665 WL_KEYBOARD_KEY_STATE_RELEASED);
Daniel Stone37816df2012-05-16 18:45:18 +0100666}
667
Daniel Stone351eb612012-05-31 15:27:47 -0400668static void
669input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
670 uint32_t serial, uint32_t mods_depressed,
671 uint32_t mods_latched, uint32_t mods_locked,
672 uint32_t group)
673{
674 /* XXX notify_modifiers(); */
675}
676
Daniel Stone37816df2012-05-16 18:45:18 +0100677static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100678 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500679 input_handle_keyboard_enter,
680 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +0100681 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -0400682 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100683};
684
685static void
Daniel Stone37816df2012-05-16 18:45:18 +0100686input_handle_capabilities(void *data, struct wl_seat *seat,
687 enum wl_seat_capability caps)
688{
689 struct wayland_input *input = data;
690
691 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
692 input->pointer = wl_seat_get_pointer(seat);
693 wl_pointer_set_user_data(input->pointer, input);
694 wl_pointer_add_listener(input->pointer, &pointer_listener,
695 input);
Daniel Stonecd981dd2012-06-01 12:14:00 +0100696 weston_seat_init_pointer(input->compositor->base.seat);
Daniel Stone37816df2012-05-16 18:45:18 +0100697 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
698 wl_pointer_destroy(input->pointer);
699 input->pointer = NULL;
700 }
701
702 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
703 input->keyboard = wl_seat_get_keyboard(seat);
704 wl_keyboard_set_user_data(input->keyboard, input);
705 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
706 input);
707 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
708 wl_keyboard_destroy(input->keyboard);
709 input->keyboard = NULL;
710 }
711}
712
713static const struct wl_seat_listener seat_listener = {
714 input_handle_capabilities,
715};
716
717static void
718display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100719{
720 struct wayland_input *input;
721
722 input = malloc(sizeof *input);
723 if (input == NULL)
724 return;
725
726 memset(input, 0, sizeof *input);
727
728 input->compositor = c;
Daniel Stone37816df2012-05-16 18:45:18 +0100729 input->seat = wl_display_bind(c->parent.display, id,
730 &wl_seat_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100731 wl_list_insert(c->input_list.prev, &input->link);
732
Daniel Stone37816df2012-05-16 18:45:18 +0100733 wl_seat_add_listener(input->seat, &seat_listener, input);
734 wl_seat_set_user_data(input->seat, input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100735}
736
Daniel Stonea497d572012-06-04 11:40:46 +0100737/* We can't start adding input devices until weston_compositor_init, but
738 * also can't call weston_compositor_init until we've handled some of the
739 * base display code first. So, we have a separate global handler for
740 * seats. */
741static void
742display_handle_global_input(struct wl_display *display, uint32_t id,
743 const char *interface, uint32_t version,
744 void *data)
745{
746 struct wayland_compositor *c = data;
747
748 if (strcmp(interface, "wl_seat") == 0)
749 display_add_seat(c, id);
750}
751
752
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100753static void
754display_handle_global(struct wl_display *display, uint32_t id,
755 const char *interface, uint32_t version, void *data)
756{
757 struct wayland_compositor *c = data;
758
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200759 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400760 c->parent.compositor =
761 wl_display_bind(display, id, &wl_compositor_interface);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200762 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400763 c->parent.output =
764 wl_display_bind(display, id, &wl_output_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100765 wl_output_add_listener(c->parent.output, &output_listener, c);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200766 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400767 c->parent.shell =
768 wl_display_bind(display, id, &wl_shell_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100769 }
770}
771
772static int
773update_event_mask(uint32_t mask, void *data)
774{
775 struct wayland_compositor *c = data;
776
777 c->parent.event_mask = mask;
778 if (c->parent.wl_source)
779 wl_event_source_fd_update(c->parent.wl_source, mask);
780
781 return 0;
782}
783
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400784static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100785wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
786{
787 struct wayland_compositor *c = data;
788
789 if (mask & WL_EVENT_READABLE)
790 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
Kristian Høgsbergf258a312011-12-28 22:51:20 -0500791 if (mask & WL_EVENT_WRITABLE)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100792 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400793
794 return 1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100795}
796
Daniel Stonea497d572012-06-04 11:40:46 +0100797static int
798wayland_input_create(struct wayland_compositor *c)
799{
800 struct weston_seat *seat;
801
802 seat = malloc(sizeof *seat);
803 if (seat == NULL)
804 return -1;
805
806 memset(seat, 0, sizeof *seat);
807 weston_seat_init(seat, &c->base);
808
809 c->base.seat = seat;
810
811 wl_display_add_global_listener(c->parent.display,
812 display_handle_global_input,
813 c);
814
815 return 0;
816}
817
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500818static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500819wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500820{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500821 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -0700822
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500823 free(ec);
824}
825
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500826static struct weston_compositor *
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500827wayland_compositor_create(struct wl_display *display,
Daniel Stonebaf43592012-06-01 11:11:10 -0400828 int width, int height, const char *display_name,
Daniel Stonec1be8e52012-06-01 11:14:02 -0400829 int argc, char *argv[], const char *config_file)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100830{
831 struct wayland_compositor *c;
832 struct wl_event_loop *loop;
833 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100834
835 c = malloc(sizeof *c);
836 if (c == NULL)
837 return NULL;
838
839 memset(c, 0, sizeof *c);
840
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500841 c->parent.display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100842
843 if (c->parent.display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200844 weston_log("failed to create display: %m\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100845 return NULL;
846 }
847
848 wl_list_init(&c->input_list);
849 wl_display_add_global_listener(c->parent.display,
850 display_handle_global, c);
851
852 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
853
854 c->base.wl_display = display;
855 if (wayland_compositor_init_egl(c) < 0)
856 return NULL;
857
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100858 c->base.destroy = wayland_destroy;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100859
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100860 /* Can't init base class until we have a current egl context */
Daniel Stonec1be8e52012-06-01 11:14:02 -0400861 if (weston_compositor_init(&c->base, display, argc, argv,
862 config_file) < 0)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100863 return NULL;
864
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500865 create_border(c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100866 if (wayland_compositor_create_output(c, width, height) < 0)
867 return NULL;
868
869 if (wayland_input_create(c) < 0)
870 return NULL;
871
872 loop = wl_display_get_event_loop(c->base.wl_display);
873
874 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
875 c->parent.wl_source =
876 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
877 wayland_compositor_handle_event, c);
878 if (c->parent.wl_source == NULL)
879 return NULL;
880
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100881 return &c->base;
882}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400883
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500884WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -0400885backend_init(struct wl_display *display, int argc, char *argv[],
886 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400887{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400888 int width = 1024, height = 640;
889 char *display_name = NULL;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400890
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400891 const struct weston_option wayland_options[] = {
892 { WESTON_OPTION_INTEGER, "width", 0, &width },
893 { WESTON_OPTION_INTEGER, "height", 0, &height },
894 { WESTON_OPTION_STRING, "display", 0, &display_name },
895 };
896
897 parse_options(wayland_options,
898 ARRAY_LENGTH(wayland_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400899
Daniel Stonebaf43592012-06-01 11:11:10 -0400900 return wayland_compositor_create(display, width, height, display_name,
Daniel Stonec1be8e52012-06-01 11:14:02 -0400901 argc, argv, config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400902}