blob: fea189b1a6a2edabd3b61c498e10b37ed2a3b2cf [file] [log] [blame]
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001/*
2 * Copyright © 2010 Benjamin Franzke
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
23#include <stddef.h>
24#define _GNU_SOURCE
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <fcntl.h>
29#include <unistd.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010030
31#include "wayland-client.h"
32
33#define GL_GLEXT_PROTOTYPES
34#define EGL_EGLEXT_PROTOTYPES
35#include <GLES2/gl2.h>
36#include <GLES2/gl2ext.h>
37#include <EGL/egl.h>
38#include <EGL/eglext.h>
39
40#include "compositor.h"
41
42struct wayland_compositor {
43 struct wlsc_compositor base;
44
45 struct {
46 struct wl_display *display;
47 struct wl_compositor *compositor;
48 struct wl_shell *shell;
49 struct wl_drm *drm;
50 struct wl_output *output;
51
52 struct {
53 int32_t x, y, width, height;
54 } screen_allocation;
55
56 char *device_name;
57 int authenticated;
58
59 struct wl_event_source *wl_source;
60 uint32_t event_mask;
61 } parent;
62
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010063 struct wl_list input_list;
64};
65
66struct wayland_output {
67 struct wlsc_output base;
68
69 struct {
70 struct wl_surface *surface;
71 struct wl_buffer *buffer[2];
72 } parent;
73 EGLImageKHR image[2];
74 GLuint rbo[2];
75 uint32_t fb_id[2];
76 uint32_t current;
77};
78
79struct wayland_input {
80 struct wayland_compositor *compositor;
81 struct wl_input_device *input_device;
82 struct wl_list link;
83};
84
85static int
86wayland_input_create(struct wayland_compositor *c)
87{
88 struct wlsc_input_device *input;
89
90 input = malloc(sizeof *input);
91 if (input == NULL)
92 return -1;
93
94 memset(input, 0, sizeof *input);
95 wlsc_input_device_init(input, &c->base);
96
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -050097 c->base.input_device = &input->input_device;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010098
99 return 0;
100}
101
102static int
103wayland_compositor_init_egl(struct wayland_compositor *c)
104{
105 EGLint major, minor;
106 const char *extensions;
107 drm_magic_t magic;
108 int fd;
109 static const EGLint context_attribs[] = {
110 EGL_CONTEXT_CLIENT_VERSION, 2,
111 EGL_NONE
112 };
113
114 fd = open(c->parent.device_name, O_RDWR);
115 if (fd < 0) {
116 fprintf(stderr, "drm open failed: %m\n");
117 return -1;
118 }
119
120 wlsc_drm_init(&c->base, fd, c->parent.device_name);
121
122 if (drmGetMagic(fd, &magic)) {
123 fprintf(stderr, "DRI2: failed to get drm magic");
124 return -1;
125 }
126
127 wl_drm_authenticate(c->parent.drm, magic);
128 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
129 while (!c->parent.authenticated)
130 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
131
132 c->base.display = eglGetDRMDisplayMESA(fd);
133 if (c->base.display == NULL) {
134 fprintf(stderr, "failed to create display\n");
135 return -1;
136 }
137
138 if (!eglInitialize(c->base.display, &major, &minor)) {
139 fprintf(stderr, "failed to initialize display\n");
140 return -1;
141 }
142
143 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
144 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
145 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
146 return -1;
147 }
148
149 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
150 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
151 return -1;
152 }
153
154 c->base.context = eglCreateContext(c->base.display, NULL,
155 EGL_NO_CONTEXT, context_attribs);
156 if (c->base.context == NULL) {
157 fprintf(stderr, "failed to create context\n");
158 return -1;
159 }
160
161 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
162 EGL_NO_SURFACE, c->base.context)) {
163 fprintf(stderr, "failed to make context current\n");
164 return -1;
165 }
166
167 return 0;
168}
169
170static void
Kristian Høgsberg3ada7ec2010-12-01 09:42:10 -0500171frame_callback(void *data, uint32_t time)
172{
173 struct wayland_compositor *c = (struct wayland_compositor *) data;
174
175 wlsc_compositor_finish_frame(&c->base, time);
176}
177
178static void
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100179wayland_compositor_present(struct wlsc_compositor *base)
180{
181 struct wayland_compositor *c = (struct wayland_compositor *) base;
182 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100183
Kristian Høgsberg4203df12010-12-01 09:40:58 -0500184 glFlush();
185
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100186 wl_list_for_each(output, &base->output_list, base.link) {
187 output->current ^= 1;
188
189 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
190 GL_COLOR_ATTACHMENT0,
191 GL_RENDERBUFFER,
192 output->rbo[output->current]);
193
194 wl_surface_attach(output->parent.surface,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500195 output->parent.buffer[output->current ^ 1],
196 0, 0);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100197 wl_surface_damage(output->parent.surface, 0, 0,
198 output->base.width, output->base.height);
199 }
200
Kristian Høgsberg3ada7ec2010-12-01 09:42:10 -0500201 wl_display_frame_callback(c->parent.display, frame_callback, c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100202}
203
204static int
205wayland_authenticate(struct wlsc_compositor *ec, uint32_t id)
206{
207 struct wayland_compositor *c = (struct wayland_compositor *) ec;
208
209 wl_drm_authenticate(c->parent.drm, id);
210 /* FIXME: recv drm_authenticated event from parent? */
211 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
212
213 return 0;
214}
215
216static int
217wayland_compositor_create_output(struct wayland_compositor *c,
218 int width, int height)
219{
220 struct wayland_output *output;
221 struct wl_visual *visual;
222 int i;
223 EGLint name, stride, attribs[] = {
224 EGL_WIDTH, 0,
225 EGL_HEIGHT, 0,
226 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
227 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
228 EGL_NONE
229 };
230
231 output = malloc(sizeof *output);
232 if (output == NULL)
233 return -1;
234 memset(output, 0, sizeof *output);
235
Benjamin Franzke1b765ff2011-02-18 16:51:37 +0100236 wlsc_output_init(&output->base, &c->base, 0, 0, width, height, 0);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100237 output->parent.surface =
238 wl_compositor_create_surface(c->parent.compositor);
239 wl_surface_set_user_data(output->parent.surface, output);
240
241 glGenRenderbuffers(2, output->rbo);
242 visual = wl_display_get_premultiplied_argb_visual(c->parent.display);
243 for (i = 0; i < 2; i++) {
244 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
245
246 attribs[1] = width;
247 attribs[3] = height;
248 output->image[i] =
249 eglCreateDRMImageMESA(c->base.display, attribs);
250 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
251 output->image[i]);
252 eglExportDRMImageMESA(c->base.display, output->image[i],
253 &name, NULL, &stride);
254 output->parent.buffer[i] =
255 wl_drm_create_buffer(c->parent.drm, name,
256 width, height,
257 stride, visual);
258 }
259
260 output->current = 0;
261 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
262 GL_COLOR_ATTACHMENT0,
263 GL_RENDERBUFFER,
264 output->rbo[output->current]);
265
266 wl_surface_attach(output->parent.surface,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500267 output->parent.buffer[output->current], 0, 0);
268 wl_surface_map_toplevel(output->parent.surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100269
270 glClearColor(0, 0, 0, 0.5);
271
272 wl_list_insert(c->base.output_list.prev, &output->base.link);
273
274 return 0;
275}
276
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100277/* Events received from the wayland-server this compositor is client of: */
278
279/* parent output interface */
280static void
281display_handle_geometry(void *data,
282 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500283 int32_t x, int32_t y,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100284 int32_t width, int32_t height)
285{
286 struct wayland_compositor *c = data;
287
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500288 c->parent.screen_allocation.x = x;
289 c->parent.screen_allocation.y = y;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100290 c->parent.screen_allocation.width = width;
291 c->parent.screen_allocation.height = height;
292}
293
294static const struct wl_output_listener output_listener = {
295 display_handle_geometry,
296};
297
298/* parent shell interface */
299static void
300handle_configure(void *data, struct wl_shell *shell,
301 uint32_t time, uint32_t edges,
Kristian Høgsbergcbe6f042010-12-17 09:54:45 -0500302 struct wl_surface *surface, int32_t width, int32_t height)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100303{
304#if 0
305 struct output *output = wl_surface_get_user_data(surface);
306
307 /* FIXME: add resize? */
308#endif
309}
310
311static const struct wl_shell_listener shell_listener = {
312 handle_configure,
313};
314
315/* parent drm interface */
316static void
317drm_handle_device(void *data, struct wl_drm *drm, const char *device)
318{
319 struct wayland_compositor *c = data;
320
321 c->parent.device_name = strdup(device);
322}
323
324static void drm_handle_authenticated(void *data, struct wl_drm *drm)
325{
326 struct wayland_compositor *c = data;
327
328 c->parent.authenticated = 1;
329}
330
331static const struct wl_drm_listener drm_listener = {
332 drm_handle_device,
333 drm_handle_authenticated
334};
335
336/* parent input interface */
337static void
338input_handle_motion(void *data, struct wl_input_device *input_device,
339 uint32_t time,
340 int32_t x, int32_t y, int32_t sx, int32_t sy)
341{
342 struct wayland_input *input = data;
343 struct wayland_compositor *c = input->compositor;
344
345 notify_motion(c->base.input_device, time, sx, sy);
346}
347
348static void
349input_handle_button(void *data,
350 struct wl_input_device *input_device,
351 uint32_t time, uint32_t button, uint32_t state)
352{
353 struct wayland_input *input = data;
354 struct wayland_compositor *c = input->compositor;
355
356 notify_button(c->base.input_device, time, button, state);
357}
358
359static void
360input_handle_key(void *data, struct wl_input_device *input_device,
361 uint32_t time, uint32_t key, uint32_t state)
362{
363 struct wayland_input *input = data;
364 struct wayland_compositor *c = input->compositor;
365
366 notify_key(c->base.input_device, time, key, state);
367}
368
369static void
370input_handle_pointer_focus(void *data,
371 struct wl_input_device *input_device,
372 uint32_t time, struct wl_surface *surface,
373 int32_t x, int32_t y, int32_t sx, int32_t sy)
374{
375 struct wayland_input *input = data;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500376 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100377 struct wayland_compositor *c = input->compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100378
379 if (surface) {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500380 output = wl_surface_get_user_data(surface);
381 notify_pointer_focus(c->base.input_device,
382 time, &output->base, sx, sy);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100383 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500384 notify_pointer_focus(c->base.input_device, time, NULL, 0, 0);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100385 }
386}
387
388static void
389input_handle_keyboard_focus(void *data,
390 struct wl_input_device *input_device,
391 uint32_t time,
392 struct wl_surface *surface,
393 struct wl_array *keys)
394{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500395 struct wayland_input *input = data;
396 struct wayland_compositor *c = input->compositor;
397 struct wayland_output *output;
398
399 if (surface) {
400 output = wl_surface_get_user_data(surface);
401 notify_keyboard_focus(c->base.input_device,
402 time, &output->base, keys);
403 } else {
404 notify_keyboard_focus(c->base.input_device, time, NULL, NULL);
405 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100406}
407
408static const struct wl_input_device_listener input_device_listener = {
409 input_handle_motion,
410 input_handle_button,
411 input_handle_key,
412 input_handle_pointer_focus,
413 input_handle_keyboard_focus,
414};
415
416static void
417display_add_input(struct wayland_compositor *c, uint32_t id)
418{
419 struct wayland_input *input;
420
421 input = malloc(sizeof *input);
422 if (input == NULL)
423 return;
424
425 memset(input, 0, sizeof *input);
426
427 input->compositor = c;
428 input->input_device = wl_input_device_create(c->parent.display, id);
429 wl_list_insert(c->input_list.prev, &input->link);
430
431 wl_input_device_add_listener(input->input_device,
432 &input_device_listener, input);
433 wl_input_device_set_user_data(input->input_device, input);
434}
435
436static void
437display_handle_global(struct wl_display *display, uint32_t id,
438 const char *interface, uint32_t version, void *data)
439{
440 struct wayland_compositor *c = data;
441
442 if (strcmp(interface, "compositor") == 0) {
443 c->parent.compositor = wl_compositor_create(display, id);
444 } else if (strcmp(interface, "output") == 0) {
445 c->parent.output = wl_output_create(display, id);
446 wl_output_add_listener(c->parent.output, &output_listener, c);
447 } else if (strcmp(interface, "input_device") == 0) {
448 display_add_input(c, id);
449 } else if (strcmp(interface, "shell") == 0) {
450 c->parent.shell = wl_shell_create(display, id);
451 wl_shell_add_listener(c->parent.shell, &shell_listener, c);
452 } else if (strcmp(interface, "drm") == 0) {
453 c->parent.drm = wl_drm_create(display, id);
454 wl_drm_add_listener(c->parent.drm, &drm_listener, c);
455 }
456}
457
458static int
459update_event_mask(uint32_t mask, void *data)
460{
461 struct wayland_compositor *c = data;
462
463 c->parent.event_mask = mask;
464 if (c->parent.wl_source)
465 wl_event_source_fd_update(c->parent.wl_source, mask);
466
467 return 0;
468}
469
470static void
471wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
472{
473 struct wayland_compositor *c = data;
474
475 if (mask & WL_EVENT_READABLE)
476 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
477 if (mask & WL_EVENT_WRITEABLE)
478 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
479}
480
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500481static void
482wayland_destroy(struct wlsc_compositor *ec)
483{
484 free(ec);
485}
486
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100487struct wlsc_compositor *
488wayland_compositor_create(struct wl_display *display, int width, int height)
489{
490 struct wayland_compositor *c;
491 struct wl_event_loop *loop;
492 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100493
494 c = malloc(sizeof *c);
495 if (c == NULL)
496 return NULL;
497
498 memset(c, 0, sizeof *c);
499
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500500 c->parent.display = wl_display_connect(NULL);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100501
502 if (c->parent.display == NULL) {
503 fprintf(stderr, "failed to create display: %m\n");
504 return NULL;
505 }
506
507 wl_list_init(&c->input_list);
508 wl_display_add_global_listener(c->parent.display,
509 display_handle_global, c);
510
511 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
512
513 c->base.wl_display = display;
514 if (wayland_compositor_init_egl(c) < 0)
515 return NULL;
516
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100517 c->base.destroy = wayland_destroy;
518 c->base.authenticate = wayland_authenticate;
519 c->base.present = wayland_compositor_present;
520 c->base.create_buffer = wlsc_drm_buffer_create;
521
Benjamin Franzke5b2cb6f2011-02-18 16:54:55 +0100522 glGenFramebuffers(1, &c->base.fbo);
523 glBindFramebuffer(GL_FRAMEBUFFER, c->base.fbo);
524
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100525 /* Can't init base class until we have a current egl context */
526 if (wlsc_compositor_init(&c->base, display) < 0)
527 return NULL;
528
529 if (wayland_compositor_create_output(c, width, height) < 0)
530 return NULL;
531
532 if (wayland_input_create(c) < 0)
533 return NULL;
534
535 loop = wl_display_get_event_loop(c->base.wl_display);
536
537 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
538 c->parent.wl_source =
539 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
540 wayland_compositor_handle_event, c);
541 if (c->parent.wl_source == NULL)
542 return NULL;
543
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100544 return &c->base;
545}