blob: 9755c0ada621292c487ba18d08c667f2b4bc48e9 [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
Benjamin Franzkebe014562011-02-18 17:04:24 +010031#include <wayland-client.h>
32#include <wayland-egl.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010033
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010034#include <GLES2/gl2.h>
35#include <GLES2/gl2ext.h>
36#include <EGL/egl.h>
37#include <EGL/eglext.h>
38
39#include "compositor.h"
40
41struct wayland_compositor {
42 struct wlsc_compositor base;
43
44 struct {
45 struct wl_display *display;
46 struct wl_compositor *compositor;
47 struct wl_shell *shell;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010048 struct wl_output *output;
49
50 struct {
51 int32_t x, y, width, height;
52 } screen_allocation;
53
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010054 struct wl_event_source *wl_source;
55 uint32_t event_mask;
56 } parent;
57
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010058 struct wl_list input_list;
59};
60
61struct wayland_output {
62 struct wlsc_output base;
63
64 struct {
65 struct wl_surface *surface;
Benjamin Franzkebe014562011-02-18 17:04:24 +010066 struct wl_egl_window *egl_window;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010067 } parent;
Benjamin Franzkebe014562011-02-18 17:04:24 +010068 EGLSurface egl_surface;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040069 struct wlsc_mode mode;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010070};
71
72struct wayland_input {
73 struct wayland_compositor *compositor;
74 struct wl_input_device *input_device;
75 struct wl_list link;
76};
77
78static int
79wayland_input_create(struct wayland_compositor *c)
80{
81 struct wlsc_input_device *input;
82
83 input = malloc(sizeof *input);
84 if (input == NULL)
85 return -1;
86
87 memset(input, 0, sizeof *input);
88 wlsc_input_device_init(input, &c->base);
89
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -050090 c->base.input_device = &input->input_device;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010091
92 return 0;
93}
94
95static int
96wayland_compositor_init_egl(struct wayland_compositor *c)
97{
98 EGLint major, minor;
Benjamin Franzkebe014562011-02-18 17:04:24 +010099 EGLint n;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100100 const char *extensions;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100101 EGLint config_attribs[] = {
102 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
103 EGL_RED_SIZE, 1,
104 EGL_GREEN_SIZE, 1,
105 EGL_BLUE_SIZE, 1,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400106 EGL_ALPHA_SIZE, 0,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100107 EGL_DEPTH_SIZE, 1,
Kristian Høgsbergd28ab362011-03-02 11:36:30 -0500108 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100109 EGL_NONE
110 };
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100111 static const EGLint context_attribs[] = {
112 EGL_CONTEXT_CLIENT_VERSION, 2,
113 EGL_NONE
114 };
115
Egbert Eiche7b8d902011-05-10 20:00:19 +0000116 setenv("EGL_PLATFORM", "wayland", 1);
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400117 c->base.display = eglGetDisplay(c->parent.display);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100118 if (c->base.display == NULL) {
119 fprintf(stderr, "failed to create display\n");
120 return -1;
121 }
122
123 if (!eglInitialize(c->base.display, &major, &minor)) {
124 fprintf(stderr, "failed to initialize display\n");
125 return -1;
126 }
127
128 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
129 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
130 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
131 return -1;
132 }
133
134 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
135 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
136 return -1;
137 }
Benjamin Franzkebe014562011-02-18 17:04:24 +0100138 if (!eglChooseConfig(c->base.display, config_attribs,
139 &c->base.config, 1, &n) || n == 0) {
140 fprintf(stderr, "failed to choose config: %d\n", n);
141 return -1;
142 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100143
Benjamin Franzkebe014562011-02-18 17:04:24 +0100144 c->base.context = eglCreateContext(c->base.display, c->base.config,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100145 EGL_NO_CONTEXT, context_attribs);
146 if (c->base.context == NULL) {
147 fprintf(stderr, "failed to create context\n");
148 return -1;
149 }
150
151 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
152 EGL_NO_SURFACE, c->base.context)) {
153 fprintf(stderr, "failed to make context current\n");
154 return -1;
155 }
156
157 return 0;
158}
159
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100160static int
161wayland_output_prepare_render(struct wlsc_output *output_base)
162{
163 struct wayland_output *output = (struct wayland_output *) output_base;
164 struct wlsc_compositor *ec = output->base.compositor;
165
166 if (!eglMakeCurrent(ec->display, output->egl_surface,
167 output->egl_surface, ec->context)) {
168 fprintf(stderr, "failed to make current\n");
169 return -1;
170 }
171
172 return 0;
173}
174
Kristian Høgsberg33418202011-08-16 23:01:28 -0400175static void
176frame_done(void *data, struct wl_callback *wl_callback, uint32_t time)
177{
178 struct wlsc_output *output = data;
179
180 wlsc_output_finish_frame(output, time);
181}
182
183static const struct wl_callback_listener frame_listener = {
184 frame_done
185};
186
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100187static int
188wayland_output_present(struct wlsc_output *output_base)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100189{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100190 struct wayland_output *output = (struct wayland_output *) output_base;
191 struct wayland_compositor *c =
192 (struct wayland_compositor *) output->base.compositor;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400193 struct wl_callback *callback;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100194
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100195 if (wayland_output_prepare_render(&output->base))
196 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100197
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100198 eglSwapBuffers(c->base.display, output->egl_surface);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400199 callback = wl_surface_frame(output->parent.surface);
200 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100201
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100202 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100203}
204
205static int
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200206wayland_output_prepare_scanout_surface(struct wlsc_output *output_base,
207 struct wlsc_surface *es)
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200208{
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200209 return -1;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200210}
211
212static int
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200213wayland_output_set_cursor(struct wlsc_output *output_base,
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400214 struct wlsc_input_device *input)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200215{
216 return -1;
217}
218
Matt Roper361d2ad2011-08-29 13:52:23 -0700219static void
220wayland_output_destroy(struct wlsc_output *output_base)
221{
222 struct wayland_output *output = (struct wayland_output *) output_base;
223 struct wlsc_compositor *ec = output->base.compositor;
224
225 eglDestroySurface(ec->display, output->egl_surface);
226 wl_egl_window_destroy(output->parent.egl_window);
227 free(output);
228
229 return;
230}
231
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200232static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100233wayland_compositor_create_output(struct wayland_compositor *c,
234 int width, int height)
235{
236 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100237
238 output = malloc(sizeof *output);
239 if (output == NULL)
240 return -1;
241 memset(output, 0, sizeof *output);
242
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400243 output->mode.flags =
244 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
245 output->mode.width = width;
246 output->mode.height = height;
247 output->mode.refresh = 60;
248 wl_list_init(&output->base.mode_list);
249 wl_list_insert(&output->base.mode_list, &output->mode.link);
250
251 output->base.current = &output->mode;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100252 wlsc_output_init(&output->base, &c->base, 0, 0, width, height,
253 WL_OUTPUT_FLIPPED);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400254
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100255 output->parent.surface =
256 wl_compositor_create_surface(c->parent.compositor);
257 wl_surface_set_user_data(output->parent.surface, output);
258
Benjamin Franzkebe014562011-02-18 17:04:24 +0100259 output->parent.egl_window =
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400260 wl_egl_window_create(output->parent.surface, width, height);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100261 if (!output->parent.egl_window) {
262 fprintf(stderr, "failure to create wl_egl_window\n");
263 goto cleanup_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100264 }
265
Benjamin Franzkebe014562011-02-18 17:04:24 +0100266 output->egl_surface =
267 eglCreateWindowSurface(c->base.display, c->base.config,
268 output->parent.egl_window, NULL);
269 if (!output->egl_surface) {
270 fprintf(stderr, "failed to create window surface\n");
271 goto cleanup_window;
272 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100273
Benjamin Franzkebe014562011-02-18 17:04:24 +0100274 if (!eglMakeCurrent(c->base.display, output->egl_surface,
275 output->egl_surface, c->base.context)) {
276 fprintf(stderr, "failed to make surface current\n");
277 goto cleanup_surface;
278 return -1;
279 }
280
Kristian Høgsberg7c476672011-06-18 06:22:05 -0400281 wl_shell_set_toplevel(c->parent.shell, output->parent.surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100282
283 glClearColor(0, 0, 0, 0.5);
284
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100285 output->base.prepare_render = wayland_output_prepare_render;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100286 output->base.present = wayland_output_present;
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200287 output->base.prepare_scanout_surface =
288 wayland_output_prepare_scanout_surface;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200289 output->base.set_hardware_cursor = wayland_output_set_cursor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700290 output->base.destroy = wayland_output_destroy;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100291
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100292 wl_list_insert(c->base.output_list.prev, &output->base.link);
293
294 return 0;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100295
296cleanup_surface:
297 eglDestroySurface(c->base.display, output->egl_surface);
298cleanup_window:
299 wl_egl_window_destroy(output->parent.egl_window);
300cleanup_output:
301 /* FIXME: cleanup wlsc_output */
302 free(output);
303
304 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100305}
306
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100307/* Events received from the wayland-server this compositor is client of: */
308
309/* parent output interface */
310static void
311display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400312 struct wl_output *wl_output,
313 int x,
314 int y,
315 int physical_width,
316 int physical_height,
317 int subpixel,
318 const char *make,
319 const char *model)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100320{
321 struct wayland_compositor *c = data;
322
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500323 c->parent.screen_allocation.x = x;
324 c->parent.screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400325}
326
327static void
328display_handle_mode(void *data,
329 struct wl_output *wl_output,
330 uint32_t flags,
331 int width,
332 int height,
333 int refresh)
334{
335 struct wayland_compositor *c = data;
336
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100337 c->parent.screen_allocation.width = width;
338 c->parent.screen_allocation.height = height;
339}
340
341static const struct wl_output_listener output_listener = {
342 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400343 display_handle_mode
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100344};
345
346/* parent shell interface */
347static void
348handle_configure(void *data, struct wl_shell *shell,
349 uint32_t time, uint32_t edges,
Kristian Høgsbergcbe6f042010-12-17 09:54:45 -0500350 struct wl_surface *surface, int32_t width, int32_t height)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100351{
352#if 0
353 struct output *output = wl_surface_get_user_data(surface);
354
355 /* FIXME: add resize? */
356#endif
357}
358
359static const struct wl_shell_listener shell_listener = {
360 handle_configure,
361};
362
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100363/* parent input interface */
364static void
365input_handle_motion(void *data, struct wl_input_device *input_device,
366 uint32_t time,
367 int32_t x, int32_t y, int32_t sx, int32_t sy)
368{
369 struct wayland_input *input = data;
370 struct wayland_compositor *c = input->compositor;
371
372 notify_motion(c->base.input_device, time, sx, sy);
373}
374
375static void
376input_handle_button(void *data,
377 struct wl_input_device *input_device,
378 uint32_t time, uint32_t button, uint32_t state)
379{
380 struct wayland_input *input = data;
381 struct wayland_compositor *c = input->compositor;
382
383 notify_button(c->base.input_device, time, button, state);
384}
385
386static void
387input_handle_key(void *data, struct wl_input_device *input_device,
388 uint32_t time, uint32_t key, uint32_t state)
389{
390 struct wayland_input *input = data;
391 struct wayland_compositor *c = input->compositor;
392
393 notify_key(c->base.input_device, time, key, state);
394}
395
396static void
397input_handle_pointer_focus(void *data,
398 struct wl_input_device *input_device,
399 uint32_t time, struct wl_surface *surface,
400 int32_t x, int32_t y, int32_t sx, int32_t sy)
401{
402 struct wayland_input *input = data;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500403 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100404 struct wayland_compositor *c = input->compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100405
406 if (surface) {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500407 output = wl_surface_get_user_data(surface);
408 notify_pointer_focus(c->base.input_device,
409 time, &output->base, sx, sy);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100410 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500411 notify_pointer_focus(c->base.input_device, time, NULL, 0, 0);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100412 }
413}
414
415static void
416input_handle_keyboard_focus(void *data,
417 struct wl_input_device *input_device,
418 uint32_t time,
419 struct wl_surface *surface,
420 struct wl_array *keys)
421{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500422 struct wayland_input *input = data;
423 struct wayland_compositor *c = input->compositor;
424 struct wayland_output *output;
425
426 if (surface) {
427 output = wl_surface_get_user_data(surface);
428 notify_keyboard_focus(c->base.input_device,
429 time, &output->base, keys);
430 } else {
431 notify_keyboard_focus(c->base.input_device, time, NULL, NULL);
432 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100433}
434
435static const struct wl_input_device_listener input_device_listener = {
436 input_handle_motion,
437 input_handle_button,
438 input_handle_key,
439 input_handle_pointer_focus,
440 input_handle_keyboard_focus,
441};
442
443static void
444display_add_input(struct wayland_compositor *c, uint32_t id)
445{
446 struct wayland_input *input;
447
448 input = malloc(sizeof *input);
449 if (input == NULL)
450 return;
451
452 memset(input, 0, sizeof *input);
453
454 input->compositor = c;
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400455 input->input_device = wl_display_bind(c->parent.display,
456 id, &wl_input_device_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100457 wl_list_insert(c->input_list.prev, &input->link);
458
459 wl_input_device_add_listener(input->input_device,
460 &input_device_listener, input);
461 wl_input_device_set_user_data(input->input_device, input);
462}
463
464static void
465display_handle_global(struct wl_display *display, uint32_t id,
466 const char *interface, uint32_t version, void *data)
467{
468 struct wayland_compositor *c = data;
469
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200470 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400471 c->parent.compositor =
472 wl_display_bind(display, id, &wl_compositor_interface);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200473 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400474 c->parent.output =
475 wl_display_bind(display, id, &wl_output_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100476 wl_output_add_listener(c->parent.output, &output_listener, c);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200477 } else if (strcmp(interface, "wl_input_device") == 0) {
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100478 display_add_input(c, id);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200479 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400480 c->parent.shell =
481 wl_display_bind(display, id, &wl_shell_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100482 wl_shell_add_listener(c->parent.shell, &shell_listener, c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100483 }
484}
485
486static int
487update_event_mask(uint32_t mask, void *data)
488{
489 struct wayland_compositor *c = data;
490
491 c->parent.event_mask = mask;
492 if (c->parent.wl_source)
493 wl_event_source_fd_update(c->parent.wl_source, mask);
494
495 return 0;
496}
497
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400498static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100499wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
500{
501 struct wayland_compositor *c = data;
502
503 if (mask & WL_EVENT_READABLE)
504 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
505 if (mask & WL_EVENT_WRITEABLE)
506 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400507
508 return 1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100509}
510
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500511static void
512wayland_destroy(struct wlsc_compositor *ec)
513{
Matt Roper361d2ad2011-08-29 13:52:23 -0700514 wlsc_compositor_shutdown(ec);
515
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500516 free(ec);
517}
518
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400519static struct wlsc_compositor *
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100520wayland_compositor_create(struct wl_display *display, int width, int height)
521{
522 struct wayland_compositor *c;
523 struct wl_event_loop *loop;
524 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100525
526 c = malloc(sizeof *c);
527 if (c == NULL)
528 return NULL;
529
530 memset(c, 0, sizeof *c);
531
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500532 c->parent.display = wl_display_connect(NULL);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100533
534 if (c->parent.display == NULL) {
535 fprintf(stderr, "failed to create display: %m\n");
536 return NULL;
537 }
538
539 wl_list_init(&c->input_list);
540 wl_display_add_global_listener(c->parent.display,
541 display_handle_global, c);
542
543 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
544
545 c->base.wl_display = display;
546 if (wayland_compositor_init_egl(c) < 0)
547 return NULL;
548
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100549 c->base.destroy = wayland_destroy;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100550
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100551 /* Can't init base class until we have a current egl context */
552 if (wlsc_compositor_init(&c->base, display) < 0)
553 return NULL;
554
555 if (wayland_compositor_create_output(c, width, height) < 0)
556 return NULL;
557
558 if (wayland_input_create(c) < 0)
559 return NULL;
560
561 loop = wl_display_get_event_loop(c->base.wl_display);
562
563 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
564 c->parent.wl_source =
565 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
566 wayland_compositor_handle_event, c);
567 if (c->parent.wl_source == NULL)
568 return NULL;
569
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100570 return &c->base;
571}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400572
573struct wlsc_compositor *
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400574backend_init(struct wl_display *display, char *options);
575
576WL_EXPORT struct wlsc_compositor *
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400577backend_init(struct wl_display *display, char *options)
578{
579 int width = 1024, height = 640, i;
580 char *p, *value;
581
582 static char * const tokens[] = { "width", "height", NULL };
583
584 p = options;
585 while (i = getsubopt(&p, tokens, &value), i != -1) {
586 switch (i) {
587 case 0:
588 width = strtol(value, NULL, 0);
589 break;
590 case 1:
591 height = strtol(value, NULL, 0);
592 break;
593 }
594 }
595
596 return wayland_compositor_create(display, width, height);
597}