blob: ed8a2babfc297f607733d03c66aa9d05ccb8c80d [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
63 struct wl_list window_list;
64 struct wl_list input_list;
65};
66
67struct wayland_output {
68 struct wlsc_output base;
69
70 struct {
71 struct wl_surface *surface;
72 struct wl_buffer *buffer[2];
73 } parent;
74 EGLImageKHR image[2];
75 GLuint rbo[2];
76 uint32_t fb_id[2];
77 uint32_t current;
78};
79
80struct wayland_input {
81 struct wayland_compositor *compositor;
82 struct wl_input_device *input_device;
83 struct wl_list link;
84};
85
86static int
87wayland_input_create(struct wayland_compositor *c)
88{
89 struct wlsc_input_device *input;
90
91 input = malloc(sizeof *input);
92 if (input == NULL)
93 return -1;
94
95 memset(input, 0, sizeof *input);
96 wlsc_input_device_init(input, &c->base);
97
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -050098 c->base.input_device = &input->input_device;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010099
100 return 0;
101}
102
103static int
104wayland_compositor_init_egl(struct wayland_compositor *c)
105{
106 EGLint major, minor;
107 const char *extensions;
108 drm_magic_t magic;
109 int fd;
110 static const EGLint context_attribs[] = {
111 EGL_CONTEXT_CLIENT_VERSION, 2,
112 EGL_NONE
113 };
114
115 fd = open(c->parent.device_name, O_RDWR);
116 if (fd < 0) {
117 fprintf(stderr, "drm open failed: %m\n");
118 return -1;
119 }
120
121 wlsc_drm_init(&c->base, fd, c->parent.device_name);
122
123 if (drmGetMagic(fd, &magic)) {
124 fprintf(stderr, "DRI2: failed to get drm magic");
125 return -1;
126 }
127
128 wl_drm_authenticate(c->parent.drm, magic);
129 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
130 while (!c->parent.authenticated)
131 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
132
133 c->base.display = eglGetDRMDisplayMESA(fd);
134 if (c->base.display == NULL) {
135 fprintf(stderr, "failed to create display\n");
136 return -1;
137 }
138
139 if (!eglInitialize(c->base.display, &major, &minor)) {
140 fprintf(stderr, "failed to initialize display\n");
141 return -1;
142 }
143
144 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
145 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
146 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
147 return -1;
148 }
149
150 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
151 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
152 return -1;
153 }
154
155 c->base.context = eglCreateContext(c->base.display, NULL,
156 EGL_NO_CONTEXT, context_attribs);
157 if (c->base.context == NULL) {
158 fprintf(stderr, "failed to create context\n");
159 return -1;
160 }
161
162 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
163 EGL_NO_SURFACE, c->base.context)) {
164 fprintf(stderr, "failed to make context current\n");
165 return -1;
166 }
167
168 return 0;
169}
170
171static void
Kristian Høgsberg3ada7ec2010-12-01 09:42:10 -0500172frame_callback(void *data, uint32_t time)
173{
174 struct wayland_compositor *c = (struct wayland_compositor *) data;
175
176 wlsc_compositor_finish_frame(&c->base, time);
177}
178
179static void
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100180wayland_compositor_present(struct wlsc_compositor *base)
181{
182 struct wayland_compositor *c = (struct wayland_compositor *) base;
183 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100184
Kristian Høgsberg4203df12010-12-01 09:40:58 -0500185 glFlush();
186
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100187 wl_list_for_each(output, &base->output_list, base.link) {
188 output->current ^= 1;
189
190 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
191 GL_COLOR_ATTACHMENT0,
192 GL_RENDERBUFFER,
193 output->rbo[output->current]);
194
195 wl_surface_attach(output->parent.surface,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500196 output->parent.buffer[output->current ^ 1],
197 0, 0);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100198 wl_surface_damage(output->parent.surface, 0, 0,
199 output->base.width, output->base.height);
200 }
201
Kristian Høgsberg3ada7ec2010-12-01 09:42:10 -0500202 wl_display_frame_callback(c->parent.display, frame_callback, c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100203}
204
205static int
206wayland_authenticate(struct wlsc_compositor *ec, uint32_t id)
207{
208 struct wayland_compositor *c = (struct wayland_compositor *) ec;
209
210 wl_drm_authenticate(c->parent.drm, id);
211 /* FIXME: recv drm_authenticated event from parent? */
212 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
213
214 return 0;
215}
216
217static int
218wayland_compositor_create_output(struct wayland_compositor *c,
219 int width, int height)
220{
221 struct wayland_output *output;
222 struct wl_visual *visual;
223 int i;
224 EGLint name, stride, attribs[] = {
225 EGL_WIDTH, 0,
226 EGL_HEIGHT, 0,
227 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
228 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
229 EGL_NONE
230 };
231
232 output = malloc(sizeof *output);
233 if (output == NULL)
234 return -1;
235 memset(output, 0, sizeof *output);
236
237 wlsc_output_init(&output->base, &c->base, 0, 0, width, height);
238 output->parent.surface =
239 wl_compositor_create_surface(c->parent.compositor);
240 wl_surface_set_user_data(output->parent.surface, output);
241
242 glGenRenderbuffers(2, output->rbo);
243 visual = wl_display_get_premultiplied_argb_visual(c->parent.display);
244 for (i = 0; i < 2; i++) {
245 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
246
247 attribs[1] = width;
248 attribs[3] = height;
249 output->image[i] =
250 eglCreateDRMImageMESA(c->base.display, attribs);
251 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
252 output->image[i]);
253 eglExportDRMImageMESA(c->base.display, output->image[i],
254 &name, NULL, &stride);
255 output->parent.buffer[i] =
256 wl_drm_create_buffer(c->parent.drm, name,
257 width, height,
258 stride, visual);
259 }
260
261 output->current = 0;
262 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
263 GL_COLOR_ATTACHMENT0,
264 GL_RENDERBUFFER,
265 output->rbo[output->current]);
266
267 wl_surface_attach(output->parent.surface,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500268 output->parent.buffer[output->current], 0, 0);
269 wl_surface_map_toplevel(output->parent.surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100270
271 glClearColor(0, 0, 0, 0.5);
272
273 wl_list_insert(c->base.output_list.prev, &output->base.link);
274
275 return 0;
276}
277
278static struct wl_buffer *
279create_invisible_pointer(struct wayland_compositor *c)
280{
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100281 struct wl_buffer *buffer;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100282 struct wl_visual *visual;
283 GLuint texture;
284 const int width = 1, height = 1;
285 const GLubyte data[] = { 0, 0, 0, 0 };
286
287 glGenTextures(1, &texture);
288 glBindTexture(GL_TEXTURE_2D, texture);
289 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
290 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
291 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
292 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
293
294 visual = wl_display_get_premultiplied_argb_visual(c->parent.display);
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500295 buffer = c->base.create_buffer(&c->base, width, height, visual, data);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100296
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100297 return buffer;
298}
299
300/* Events received from the wayland-server this compositor is client of: */
301
302/* parent output interface */
303static void
304display_handle_geometry(void *data,
305 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500306 int32_t x, int32_t y,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100307 int32_t width, int32_t height)
308{
309 struct wayland_compositor *c = data;
310
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500311 c->parent.screen_allocation.x = x;
312 c->parent.screen_allocation.y = y;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100313 c->parent.screen_allocation.width = width;
314 c->parent.screen_allocation.height = height;
315}
316
317static const struct wl_output_listener output_listener = {
318 display_handle_geometry,
319};
320
321/* parent shell interface */
322static void
323handle_configure(void *data, struct wl_shell *shell,
324 uint32_t time, uint32_t edges,
Kristian Høgsbergcbe6f042010-12-17 09:54:45 -0500325 struct wl_surface *surface, int32_t width, int32_t height)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100326{
327#if 0
328 struct output *output = wl_surface_get_user_data(surface);
329
330 /* FIXME: add resize? */
331#endif
332}
333
334static const struct wl_shell_listener shell_listener = {
335 handle_configure,
336};
337
338/* parent drm interface */
339static void
340drm_handle_device(void *data, struct wl_drm *drm, const char *device)
341{
342 struct wayland_compositor *c = data;
343
344 c->parent.device_name = strdup(device);
345}
346
347static void drm_handle_authenticated(void *data, struct wl_drm *drm)
348{
349 struct wayland_compositor *c = data;
350
351 c->parent.authenticated = 1;
352}
353
354static const struct wl_drm_listener drm_listener = {
355 drm_handle_device,
356 drm_handle_authenticated
357};
358
359/* parent input interface */
360static void
361input_handle_motion(void *data, struct wl_input_device *input_device,
362 uint32_t time,
363 int32_t x, int32_t y, int32_t sx, int32_t sy)
364{
365 struct wayland_input *input = data;
366 struct wayland_compositor *c = input->compositor;
367
368 notify_motion(c->base.input_device, time, sx, sy);
369}
370
371static void
372input_handle_button(void *data,
373 struct wl_input_device *input_device,
374 uint32_t time, uint32_t button, uint32_t state)
375{
376 struct wayland_input *input = data;
377 struct wayland_compositor *c = input->compositor;
378
379 notify_button(c->base.input_device, time, button, state);
380}
381
382static void
383input_handle_key(void *data, struct wl_input_device *input_device,
384 uint32_t time, uint32_t key, uint32_t state)
385{
386 struct wayland_input *input = data;
387 struct wayland_compositor *c = input->compositor;
388
389 notify_key(c->base.input_device, time, key, state);
390}
391
392static void
393input_handle_pointer_focus(void *data,
394 struct wl_input_device *input_device,
395 uint32_t time, struct wl_surface *surface,
396 int32_t x, int32_t y, int32_t sx, int32_t sy)
397{
398 struct wayland_input *input = data;
399 struct wayland_compositor *c = input->compositor;
400 static struct wl_buffer *pntr_buffer = NULL;
401
402 if (surface) {
403 c->base.focus = 1;
404 /* FIXME: extend protocol to allow hiding the cursor? */
405 if (pntr_buffer == NULL)
406 pntr_buffer = create_invisible_pointer(c);
407 wl_input_device_attach(input_device, time, pntr_buffer, x, y);
408 } else {
409 /* FIXME. hide our own pointer */
410 c->base.focus = 0;
411 }
412}
413
414static void
415input_handle_keyboard_focus(void *data,
416 struct wl_input_device *input_device,
417 uint32_t time,
418 struct wl_surface *surface,
419 struct wl_array *keys)
420{
421 /* FIXME: sth to be done here? */
422}
423
424static const struct wl_input_device_listener input_device_listener = {
425 input_handle_motion,
426 input_handle_button,
427 input_handle_key,
428 input_handle_pointer_focus,
429 input_handle_keyboard_focus,
430};
431
432static void
433display_add_input(struct wayland_compositor *c, uint32_t id)
434{
435 struct wayland_input *input;
436
437 input = malloc(sizeof *input);
438 if (input == NULL)
439 return;
440
441 memset(input, 0, sizeof *input);
442
443 input->compositor = c;
444 input->input_device = wl_input_device_create(c->parent.display, id);
445 wl_list_insert(c->input_list.prev, &input->link);
446
447 wl_input_device_add_listener(input->input_device,
448 &input_device_listener, input);
449 wl_input_device_set_user_data(input->input_device, input);
450}
451
452static void
453display_handle_global(struct wl_display *display, uint32_t id,
454 const char *interface, uint32_t version, void *data)
455{
456 struct wayland_compositor *c = data;
457
458 if (strcmp(interface, "compositor") == 0) {
459 c->parent.compositor = wl_compositor_create(display, id);
460 } else if (strcmp(interface, "output") == 0) {
461 c->parent.output = wl_output_create(display, id);
462 wl_output_add_listener(c->parent.output, &output_listener, c);
463 } else if (strcmp(interface, "input_device") == 0) {
464 display_add_input(c, id);
465 } else if (strcmp(interface, "shell") == 0) {
466 c->parent.shell = wl_shell_create(display, id);
467 wl_shell_add_listener(c->parent.shell, &shell_listener, c);
468 } else if (strcmp(interface, "drm") == 0) {
469 c->parent.drm = wl_drm_create(display, id);
470 wl_drm_add_listener(c->parent.drm, &drm_listener, c);
471 }
472}
473
474static int
475update_event_mask(uint32_t mask, void *data)
476{
477 struct wayland_compositor *c = data;
478
479 c->parent.event_mask = mask;
480 if (c->parent.wl_source)
481 wl_event_source_fd_update(c->parent.wl_source, mask);
482
483 return 0;
484}
485
486static void
487wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
488{
489 struct wayland_compositor *c = data;
490
491 if (mask & WL_EVENT_READABLE)
492 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
493 if (mask & WL_EVENT_WRITEABLE)
494 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
495}
496
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500497static void
498wayland_destroy(struct wlsc_compositor *ec)
499{
500 free(ec);
501}
502
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100503struct wlsc_compositor *
504wayland_compositor_create(struct wl_display *display, int width, int height)
505{
506 struct wayland_compositor *c;
507 struct wl_event_loop *loop;
508 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100509
510 c = malloc(sizeof *c);
511 if (c == NULL)
512 return NULL;
513
514 memset(c, 0, sizeof *c);
515
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500516 c->parent.display = wl_display_connect(NULL);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100517
518 if (c->parent.display == NULL) {
519 fprintf(stderr, "failed to create display: %m\n");
520 return NULL;
521 }
522
523 wl_list_init(&c->input_list);
524 wl_display_add_global_listener(c->parent.display,
525 display_handle_global, c);
526
527 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
528
529 c->base.wl_display = display;
530 if (wayland_compositor_init_egl(c) < 0)
531 return NULL;
532
533 /* Can't init base class until we have a current egl context */
534 if (wlsc_compositor_init(&c->base, display) < 0)
535 return NULL;
536
537 if (wayland_compositor_create_output(c, width, height) < 0)
538 return NULL;
539
540 if (wayland_input_create(c) < 0)
541 return NULL;
542
543 loop = wl_display_get_event_loop(c->base.wl_display);
544
545 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
546 c->parent.wl_source =
547 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
548 wayland_compositor_handle_event, c);
549 if (c->parent.wl_source == NULL)
550 return NULL;
551
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500552 c->base.destroy = wayland_destroy;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100553 c->base.authenticate = wayland_authenticate;
554 c->base.present = wayland_compositor_present;
555
556 return &c->base;
557}