blob: 750e3e7e8e6f5716ffa297789341691a518517b0 [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;
Kristian Høgsberg7c476672011-06-18 06:22:05 -040049 struct wl_visual *visual;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010050
51 struct {
52 int32_t x, y, width, height;
53 } screen_allocation;
54
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010055 struct wl_event_source *wl_source;
56 uint32_t event_mask;
57 } parent;
58
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010059 struct wl_list input_list;
60};
61
62struct wayland_output {
63 struct wlsc_output base;
64
65 struct {
66 struct wl_surface *surface;
Benjamin Franzkebe014562011-02-18 17:04:24 +010067 struct wl_egl_window *egl_window;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010068 } parent;
Benjamin Franzkebe014562011-02-18 17:04:24 +010069 EGLSurface egl_surface;
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,
106 EGL_DEPTH_SIZE, 1,
Kristian Høgsbergd28ab362011-03-02 11:36:30 -0500107 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100108 EGL_NONE
109 };
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100110 static const EGLint context_attribs[] = {
111 EGL_CONTEXT_CLIENT_VERSION, 2,
112 EGL_NONE
113 };
114
Egbert Eiche7b8d902011-05-10 20:00:19 +0000115 setenv("EGL_PLATFORM", "wayland", 1);
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400116 c->base.display = eglGetDisplay(c->parent.display);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100117 if (c->base.display == NULL) {
118 fprintf(stderr, "failed to create display\n");
119 return -1;
120 }
121
122 if (!eglInitialize(c->base.display, &major, &minor)) {
123 fprintf(stderr, "failed to initialize display\n");
124 return -1;
125 }
126
127 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
128 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
129 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
130 return -1;
131 }
132
133 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
134 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
135 return -1;
136 }
Benjamin Franzkebe014562011-02-18 17:04:24 +0100137 if (!eglChooseConfig(c->base.display, config_attribs,
138 &c->base.config, 1, &n) || n == 0) {
139 fprintf(stderr, "failed to choose config: %d\n", n);
140 return -1;
141 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100142
Benjamin Franzkebe014562011-02-18 17:04:24 +0100143 c->base.context = eglCreateContext(c->base.display, c->base.config,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100144 EGL_NO_CONTEXT, context_attribs);
145 if (c->base.context == NULL) {
146 fprintf(stderr, "failed to create context\n");
147 return -1;
148 }
149
150 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
151 EGL_NO_SURFACE, c->base.context)) {
152 fprintf(stderr, "failed to make context current\n");
153 return -1;
154 }
155
156 return 0;
157}
158
159static void
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100160frame_callback(struct wl_surface *surface, void *data, uint32_t time)
Kristian Høgsberg3ada7ec2010-12-01 09:42:10 -0500161{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100162 struct wlsc_output *output = data;
Kristian Høgsberg3ada7ec2010-12-01 09:42:10 -0500163
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100164 wlsc_output_finish_frame(output, time);
Kristian Høgsberg3ada7ec2010-12-01 09:42:10 -0500165}
166
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100167static int
168wayland_output_prepare_render(struct wlsc_output *output_base)
169{
170 struct wayland_output *output = (struct wayland_output *) output_base;
171 struct wlsc_compositor *ec = output->base.compositor;
172
173 if (!eglMakeCurrent(ec->display, output->egl_surface,
174 output->egl_surface, ec->context)) {
175 fprintf(stderr, "failed to make current\n");
176 return -1;
177 }
178
179 return 0;
180}
181
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100182static int
183wayland_output_present(struct wlsc_output *output_base)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100184{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100185 struct wayland_output *output = (struct wayland_output *) output_base;
186 struct wayland_compositor *c =
187 (struct wayland_compositor *) output->base.compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100188
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100189 if (wayland_output_prepare_render(&output->base))
190 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100191
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100192 eglSwapBuffers(c->base.display, output->egl_surface);
193 wl_display_frame_callback(c->parent.display,
194 output->parent.surface,
195 frame_callback, &output->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100196
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100197 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100198}
199
200static int
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200201wayland_output_prepare_scanout_surface(struct wlsc_output *output_base,
202 struct wlsc_surface *es)
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200203{
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200204 return -1;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200205}
206
207static int
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200208wayland_output_set_cursor(struct wlsc_output *output_base,
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400209 struct wlsc_input_device *input)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200210{
211 return -1;
212}
213
214static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100215wayland_compositor_create_output(struct wayland_compositor *c,
216 int width, int height)
217{
218 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100219
220 output = malloc(sizeof *output);
221 if (output == NULL)
222 return -1;
223 memset(output, 0, sizeof *output);
224
Benjamin Franzkebe014562011-02-18 17:04:24 +0100225 wlsc_output_init(&output->base, &c->base, 0, 0, width, height,
226 WL_OUTPUT_FLIPPED);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100227 output->parent.surface =
228 wl_compositor_create_surface(c->parent.compositor);
229 wl_surface_set_user_data(output->parent.surface, output);
230
Benjamin Franzkebe014562011-02-18 17:04:24 +0100231 output->parent.egl_window =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400232 wl_egl_window_create(output->parent.surface,
Kristian Høgsberg7c476672011-06-18 06:22:05 -0400233 width, height, c->parent.visual);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100234 if (!output->parent.egl_window) {
235 fprintf(stderr, "failure to create wl_egl_window\n");
236 goto cleanup_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100237 }
238
Benjamin Franzkebe014562011-02-18 17:04:24 +0100239 output->egl_surface =
240 eglCreateWindowSurface(c->base.display, c->base.config,
241 output->parent.egl_window, NULL);
242 if (!output->egl_surface) {
243 fprintf(stderr, "failed to create window surface\n");
244 goto cleanup_window;
245 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100246
Benjamin Franzkebe014562011-02-18 17:04:24 +0100247 if (!eglMakeCurrent(c->base.display, output->egl_surface,
248 output->egl_surface, c->base.context)) {
249 fprintf(stderr, "failed to make surface current\n");
250 goto cleanup_surface;
251 return -1;
252 }
253
Kristian Høgsberg7c476672011-06-18 06:22:05 -0400254 wl_shell_set_toplevel(c->parent.shell, output->parent.surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100255
256 glClearColor(0, 0, 0, 0.5);
257
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100258 output->base.prepare_render = wayland_output_prepare_render;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100259 output->base.present = wayland_output_present;
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200260 output->base.prepare_scanout_surface =
261 wayland_output_prepare_scanout_surface;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200262 output->base.set_hardware_cursor = wayland_output_set_cursor;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100263
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100264 wl_list_insert(c->base.output_list.prev, &output->base.link);
265
266 return 0;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100267
268cleanup_surface:
269 eglDestroySurface(c->base.display, output->egl_surface);
270cleanup_window:
271 wl_egl_window_destroy(output->parent.egl_window);
272cleanup_output:
273 /* FIXME: cleanup wlsc_output */
274 free(output);
275
276 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100277}
278
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100279/* Events received from the wayland-server this compositor is client of: */
280
281/* parent output interface */
282static void
283display_handle_geometry(void *data,
284 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500285 int32_t x, int32_t y,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100286 int32_t width, int32_t height)
287{
288 struct wayland_compositor *c = data;
289
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500290 c->parent.screen_allocation.x = x;
291 c->parent.screen_allocation.y = y;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100292 c->parent.screen_allocation.width = width;
293 c->parent.screen_allocation.height = height;
294}
295
296static const struct wl_output_listener output_listener = {
297 display_handle_geometry,
298};
299
300/* parent shell interface */
301static void
302handle_configure(void *data, struct wl_shell *shell,
303 uint32_t time, uint32_t edges,
Kristian Høgsbergcbe6f042010-12-17 09:54:45 -0500304 struct wl_surface *surface, int32_t width, int32_t height)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100305{
306#if 0
307 struct output *output = wl_surface_get_user_data(surface);
308
309 /* FIXME: add resize? */
310#endif
311}
312
313static const struct wl_shell_listener shell_listener = {
314 handle_configure,
315};
316
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100317/* parent input interface */
318static void
319input_handle_motion(void *data, struct wl_input_device *input_device,
320 uint32_t time,
321 int32_t x, int32_t y, int32_t sx, int32_t sy)
322{
323 struct wayland_input *input = data;
324 struct wayland_compositor *c = input->compositor;
325
326 notify_motion(c->base.input_device, time, sx, sy);
327}
328
329static void
330input_handle_button(void *data,
331 struct wl_input_device *input_device,
332 uint32_t time, uint32_t button, uint32_t state)
333{
334 struct wayland_input *input = data;
335 struct wayland_compositor *c = input->compositor;
336
337 notify_button(c->base.input_device, time, button, state);
338}
339
340static void
341input_handle_key(void *data, struct wl_input_device *input_device,
342 uint32_t time, uint32_t key, uint32_t state)
343{
344 struct wayland_input *input = data;
345 struct wayland_compositor *c = input->compositor;
346
347 notify_key(c->base.input_device, time, key, state);
348}
349
350static void
351input_handle_pointer_focus(void *data,
352 struct wl_input_device *input_device,
353 uint32_t time, struct wl_surface *surface,
354 int32_t x, int32_t y, int32_t sx, int32_t sy)
355{
356 struct wayland_input *input = data;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500357 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100358 struct wayland_compositor *c = input->compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100359
360 if (surface) {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500361 output = wl_surface_get_user_data(surface);
362 notify_pointer_focus(c->base.input_device,
363 time, &output->base, sx, sy);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100364 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500365 notify_pointer_focus(c->base.input_device, time, NULL, 0, 0);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100366 }
367}
368
369static void
370input_handle_keyboard_focus(void *data,
371 struct wl_input_device *input_device,
372 uint32_t time,
373 struct wl_surface *surface,
374 struct wl_array *keys)
375{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500376 struct wayland_input *input = data;
377 struct wayland_compositor *c = input->compositor;
378 struct wayland_output *output;
379
380 if (surface) {
381 output = wl_surface_get_user_data(surface);
382 notify_keyboard_focus(c->base.input_device,
383 time, &output->base, keys);
384 } else {
385 notify_keyboard_focus(c->base.input_device, time, NULL, NULL);
386 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100387}
388
389static const struct wl_input_device_listener input_device_listener = {
390 input_handle_motion,
391 input_handle_button,
392 input_handle_key,
393 input_handle_pointer_focus,
394 input_handle_keyboard_focus,
395};
396
397static void
398display_add_input(struct wayland_compositor *c, uint32_t id)
399{
400 struct wayland_input *input;
401
402 input = malloc(sizeof *input);
403 if (input == NULL)
404 return;
405
406 memset(input, 0, sizeof *input);
407
408 input->compositor = c;
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400409 input->input_device = wl_input_device_create(c->parent.display, id, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100410 wl_list_insert(c->input_list.prev, &input->link);
411
412 wl_input_device_add_listener(input->input_device,
413 &input_device_listener, input);
414 wl_input_device_set_user_data(input->input_device, input);
415}
416
417static void
Kristian Høgsberg7c476672011-06-18 06:22:05 -0400418compositor_handle_visual(void *data,
419 struct wl_compositor *compositor,
420 uint32_t id, uint32_t token)
421{
422 struct wayland_compositor *c = data;
423
424 switch (token) {
425 case WL_COMPOSITOR_VISUAL_ARGB32:
426 c->parent.visual = wl_visual_create(c->parent.display, id, 1);
427 break;
428 }
429}
430
431static const struct wl_compositor_listener compositor_listener = {
432 compositor_handle_visual,
433};
434
435static void
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100436display_handle_global(struct wl_display *display, uint32_t id,
437 const char *interface, uint32_t version, void *data)
438{
439 struct wayland_compositor *c = data;
440
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200441 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400442 c->parent.compositor = wl_compositor_create(display, id, 1);
Kristian Høgsberg7c476672011-06-18 06:22:05 -0400443 wl_compositor_add_listener(c->parent.compositor,
444 &compositor_listener, c);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200445 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400446 c->parent.output = wl_output_create(display, id, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100447 wl_output_add_listener(c->parent.output, &output_listener, c);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200448 } else if (strcmp(interface, "wl_input_device") == 0) {
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100449 display_add_input(c, id);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200450 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400451 c->parent.shell = wl_shell_create(display, id, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100452 wl_shell_add_listener(c->parent.shell, &shell_listener, c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100453 }
454}
455
456static int
457update_event_mask(uint32_t mask, void *data)
458{
459 struct wayland_compositor *c = data;
460
461 c->parent.event_mask = mask;
462 if (c->parent.wl_source)
463 wl_event_source_fd_update(c->parent.wl_source, mask);
464
465 return 0;
466}
467
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400468static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100469wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
470{
471 struct wayland_compositor *c = data;
472
473 if (mask & WL_EVENT_READABLE)
474 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
475 if (mask & WL_EVENT_WRITEABLE)
476 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400477
478 return 1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100479}
480
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500481static void
482wayland_destroy(struct wlsc_compositor *ec)
483{
484 free(ec);
485}
486
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400487static struct wlsc_compositor *
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100488wayland_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;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100518
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100519 /* Can't init base class until we have a current egl context */
520 if (wlsc_compositor_init(&c->base, display) < 0)
521 return NULL;
522
523 if (wayland_compositor_create_output(c, width, height) < 0)
524 return NULL;
525
526 if (wayland_input_create(c) < 0)
527 return NULL;
528
529 loop = wl_display_get_event_loop(c->base.wl_display);
530
531 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
532 c->parent.wl_source =
533 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
534 wayland_compositor_handle_event, c);
535 if (c->parent.wl_source == NULL)
536 return NULL;
537
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100538 return &c->base;
539}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400540
541struct wlsc_compositor *
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400542backend_init(struct wl_display *display, char *options);
543
544WL_EXPORT struct wlsc_compositor *
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400545backend_init(struct wl_display *display, char *options)
546{
547 int width = 1024, height = 640, i;
548 char *p, *value;
549
550 static char * const tokens[] = { "width", "height", NULL };
551
552 p = options;
553 while (i = getsubopt(&p, tokens, &value), i != -1) {
554 switch (i) {
555 case 0:
556 width = strtol(value, NULL, 0);
557 break;
558 case 1:
559 height = strtol(value, NULL, 0);
560 break;
561 }
562 }
563
564 return wayland_compositor_create(display, width, height);
565}