blob: d143a60b6331b6fe3dc6fdc2008ee6f9d4d74226 [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
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
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100159static int
160wayland_output_prepare_render(struct wlsc_output *output_base)
161{
162 struct wayland_output *output = (struct wayland_output *) output_base;
163 struct wlsc_compositor *ec = output->base.compositor;
164
165 if (!eglMakeCurrent(ec->display, output->egl_surface,
166 output->egl_surface, ec->context)) {
167 fprintf(stderr, "failed to make current\n");
168 return -1;
169 }
170
171 return 0;
172}
173
Kristian Høgsberg33418202011-08-16 23:01:28 -0400174static void
175frame_done(void *data, struct wl_callback *wl_callback, uint32_t time)
176{
177 struct wlsc_output *output = data;
178
179 wlsc_output_finish_frame(output, time);
180}
181
182static const struct wl_callback_listener frame_listener = {
183 frame_done
184};
185
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100186static int
187wayland_output_present(struct wlsc_output *output_base)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100188{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100189 struct wayland_output *output = (struct wayland_output *) output_base;
190 struct wayland_compositor *c =
191 (struct wayland_compositor *) output->base.compositor;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400192 struct wl_callback *callback;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100193
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100194 if (wayland_output_prepare_render(&output->base))
195 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100196
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100197 eglSwapBuffers(c->base.display, output->egl_surface);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400198 callback = wl_surface_frame(output->parent.surface);
199 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100200
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100201 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100202}
203
204static int
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200205wayland_output_prepare_scanout_surface(struct wlsc_output *output_base,
206 struct wlsc_surface *es)
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200207{
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200208 return -1;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200209}
210
211static int
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200212wayland_output_set_cursor(struct wlsc_output *output_base,
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400213 struct wlsc_input_device *input)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200214{
215 return -1;
216}
217
Matt Roper361d2ad2011-08-29 13:52:23 -0700218static void
219wayland_output_destroy(struct wlsc_output *output_base)
220{
221 struct wayland_output *output = (struct wayland_output *) output_base;
222 struct wlsc_compositor *ec = output->base.compositor;
223
224 eglDestroySurface(ec->display, output->egl_surface);
225 wl_egl_window_destroy(output->parent.egl_window);
226 free(output);
227
228 return;
229}
230
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200231static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100232wayland_compositor_create_output(struct wayland_compositor *c,
233 int width, int height)
234{
235 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100236
237 output = malloc(sizeof *output);
238 if (output == NULL)
239 return -1;
240 memset(output, 0, sizeof *output);
241
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400242 output->mode.flags =
243 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
244 output->mode.width = width;
245 output->mode.height = height;
246 output->mode.refresh = 60;
247 wl_list_init(&output->base.mode_list);
248 wl_list_insert(&output->base.mode_list, &output->mode.link);
249
250 output->base.current = &output->mode;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100251 wlsc_output_init(&output->base, &c->base, 0, 0, width, height,
252 WL_OUTPUT_FLIPPED);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400253
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100254 output->parent.surface =
255 wl_compositor_create_surface(c->parent.compositor);
256 wl_surface_set_user_data(output->parent.surface, output);
257
Benjamin Franzkebe014562011-02-18 17:04:24 +0100258 output->parent.egl_window =
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400259 wl_egl_window_create(output->parent.surface, width, height);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100260 if (!output->parent.egl_window) {
261 fprintf(stderr, "failure to create wl_egl_window\n");
262 goto cleanup_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100263 }
264
Benjamin Franzkebe014562011-02-18 17:04:24 +0100265 output->egl_surface =
266 eglCreateWindowSurface(c->base.display, c->base.config,
267 output->parent.egl_window, NULL);
268 if (!output->egl_surface) {
269 fprintf(stderr, "failed to create window surface\n");
270 goto cleanup_window;
271 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100272
Benjamin Franzkebe014562011-02-18 17:04:24 +0100273 if (!eglMakeCurrent(c->base.display, output->egl_surface,
274 output->egl_surface, c->base.context)) {
275 fprintf(stderr, "failed to make surface current\n");
276 goto cleanup_surface;
277 return -1;
278 }
279
Kristian Høgsberg7c476672011-06-18 06:22:05 -0400280 wl_shell_set_toplevel(c->parent.shell, output->parent.surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100281
282 glClearColor(0, 0, 0, 0.5);
283
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100284 output->base.prepare_render = wayland_output_prepare_render;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100285 output->base.present = wayland_output_present;
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200286 output->base.prepare_scanout_surface =
287 wayland_output_prepare_scanout_surface;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200288 output->base.set_hardware_cursor = wayland_output_set_cursor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700289 output->base.destroy = wayland_output_destroy;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100290
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100291 wl_list_insert(c->base.output_list.prev, &output->base.link);
292
293 return 0;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100294
295cleanup_surface:
296 eglDestroySurface(c->base.display, output->egl_surface);
297cleanup_window:
298 wl_egl_window_destroy(output->parent.egl_window);
299cleanup_output:
300 /* FIXME: cleanup wlsc_output */
301 free(output);
302
303 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100304}
305
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100306/* Events received from the wayland-server this compositor is client of: */
307
308/* parent output interface */
309static void
310display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400311 struct wl_output *wl_output,
312 int x,
313 int y,
314 int physical_width,
315 int physical_height,
316 int subpixel,
317 const char *make,
318 const char *model)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100319{
320 struct wayland_compositor *c = data;
321
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500322 c->parent.screen_allocation.x = x;
323 c->parent.screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400324}
325
326static void
327display_handle_mode(void *data,
328 struct wl_output *wl_output,
329 uint32_t flags,
330 int width,
331 int height,
332 int refresh)
333{
334 struct wayland_compositor *c = data;
335
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100336 c->parent.screen_allocation.width = width;
337 c->parent.screen_allocation.height = height;
338}
339
340static const struct wl_output_listener output_listener = {
341 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400342 display_handle_mode
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100343};
344
345/* parent shell interface */
346static void
347handle_configure(void *data, struct wl_shell *shell,
348 uint32_t time, uint32_t edges,
Kristian Høgsbergcbe6f042010-12-17 09:54:45 -0500349 struct wl_surface *surface, int32_t width, int32_t height)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100350{
351#if 0
352 struct output *output = wl_surface_get_user_data(surface);
353
354 /* FIXME: add resize? */
355#endif
356}
357
358static const struct wl_shell_listener shell_listener = {
359 handle_configure,
360};
361
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100362/* parent input interface */
363static void
364input_handle_motion(void *data, struct wl_input_device *input_device,
365 uint32_t time,
366 int32_t x, int32_t y, int32_t sx, int32_t sy)
367{
368 struct wayland_input *input = data;
369 struct wayland_compositor *c = input->compositor;
370
371 notify_motion(c->base.input_device, time, sx, sy);
372}
373
374static void
375input_handle_button(void *data,
376 struct wl_input_device *input_device,
377 uint32_t time, uint32_t button, uint32_t state)
378{
379 struct wayland_input *input = data;
380 struct wayland_compositor *c = input->compositor;
381
382 notify_button(c->base.input_device, time, button, state);
383}
384
385static void
386input_handle_key(void *data, struct wl_input_device *input_device,
387 uint32_t time, uint32_t key, uint32_t state)
388{
389 struct wayland_input *input = data;
390 struct wayland_compositor *c = input->compositor;
391
392 notify_key(c->base.input_device, time, key, state);
393}
394
395static void
396input_handle_pointer_focus(void *data,
397 struct wl_input_device *input_device,
398 uint32_t time, struct wl_surface *surface,
399 int32_t x, int32_t y, int32_t sx, int32_t sy)
400{
401 struct wayland_input *input = data;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500402 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100403 struct wayland_compositor *c = input->compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100404
405 if (surface) {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500406 output = wl_surface_get_user_data(surface);
407 notify_pointer_focus(c->base.input_device,
408 time, &output->base, sx, sy);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100409 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500410 notify_pointer_focus(c->base.input_device, time, NULL, 0, 0);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100411 }
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{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500421 struct wayland_input *input = data;
422 struct wayland_compositor *c = input->compositor;
423 struct wayland_output *output;
424
425 if (surface) {
426 output = wl_surface_get_user_data(surface);
427 notify_keyboard_focus(c->base.input_device,
428 time, &output->base, keys);
429 } else {
430 notify_keyboard_focus(c->base.input_device, time, NULL, NULL);
431 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100432}
433
434static const struct wl_input_device_listener input_device_listener = {
435 input_handle_motion,
436 input_handle_button,
437 input_handle_key,
438 input_handle_pointer_focus,
439 input_handle_keyboard_focus,
440};
441
442static void
443display_add_input(struct wayland_compositor *c, uint32_t id)
444{
445 struct wayland_input *input;
446
447 input = malloc(sizeof *input);
448 if (input == NULL)
449 return;
450
451 memset(input, 0, sizeof *input);
452
453 input->compositor = c;
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400454 input->input_device = wl_display_bind(c->parent.display,
455 id, &wl_input_device_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100456 wl_list_insert(c->input_list.prev, &input->link);
457
458 wl_input_device_add_listener(input->input_device,
459 &input_device_listener, input);
460 wl_input_device_set_user_data(input->input_device, input);
461}
462
463static void
464display_handle_global(struct wl_display *display, uint32_t id,
465 const char *interface, uint32_t version, void *data)
466{
467 struct wayland_compositor *c = data;
468
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200469 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400470 c->parent.compositor =
471 wl_display_bind(display, id, &wl_compositor_interface);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200472 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400473 c->parent.output =
474 wl_display_bind(display, id, &wl_output_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100475 wl_output_add_listener(c->parent.output, &output_listener, c);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200476 } else if (strcmp(interface, "wl_input_device") == 0) {
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100477 display_add_input(c, id);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200478 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400479 c->parent.shell =
480 wl_display_bind(display, id, &wl_shell_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100481 wl_shell_add_listener(c->parent.shell, &shell_listener, c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100482 }
483}
484
485static int
486update_event_mask(uint32_t mask, void *data)
487{
488 struct wayland_compositor *c = data;
489
490 c->parent.event_mask = mask;
491 if (c->parent.wl_source)
492 wl_event_source_fd_update(c->parent.wl_source, mask);
493
494 return 0;
495}
496
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400497static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100498wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
499{
500 struct wayland_compositor *c = data;
501
502 if (mask & WL_EVENT_READABLE)
503 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
504 if (mask & WL_EVENT_WRITEABLE)
505 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400506
507 return 1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100508}
509
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500510static void
511wayland_destroy(struct wlsc_compositor *ec)
512{
Matt Roper361d2ad2011-08-29 13:52:23 -0700513 wlsc_compositor_shutdown(ec);
514
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500515 free(ec);
516}
517
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400518static struct wlsc_compositor *
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100519wayland_compositor_create(struct wl_display *display, int width, int height)
520{
521 struct wayland_compositor *c;
522 struct wl_event_loop *loop;
523 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100524
525 c = malloc(sizeof *c);
526 if (c == NULL)
527 return NULL;
528
529 memset(c, 0, sizeof *c);
530
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500531 c->parent.display = wl_display_connect(NULL);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100532
533 if (c->parent.display == NULL) {
534 fprintf(stderr, "failed to create display: %m\n");
535 return NULL;
536 }
537
538 wl_list_init(&c->input_list);
539 wl_display_add_global_listener(c->parent.display,
540 display_handle_global, c);
541
542 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
543
544 c->base.wl_display = display;
545 if (wayland_compositor_init_egl(c) < 0)
546 return NULL;
547
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100548 c->base.destroy = wayland_destroy;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100549
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100550 /* Can't init base class until we have a current egl context */
551 if (wlsc_compositor_init(&c->base, display) < 0)
552 return NULL;
553
554 if (wayland_compositor_create_output(c, width, height) < 0)
555 return NULL;
556
557 if (wayland_input_create(c) < 0)
558 return NULL;
559
560 loop = wl_display_get_event_loop(c->base.wl_display);
561
562 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
563 c->parent.wl_source =
564 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
565 wayland_compositor_handle_event, c);
566 if (c->parent.wl_source == NULL)
567 return NULL;
568
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100569 return &c->base;
570}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400571
572struct wlsc_compositor *
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400573backend_init(struct wl_display *display, char *options);
574
575WL_EXPORT struct wlsc_compositor *
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400576backend_init(struct wl_display *display, char *options)
577{
578 int width = 1024, height = 640, i;
579 char *p, *value;
580
581 static char * const tokens[] = { "width", "height", NULL };
582
583 p = options;
584 while (i = getsubopt(&p, tokens, &value), i != -1) {
585 switch (i) {
586 case 0:
587 width = strtol(value, NULL, 0);
588 break;
589 case 1:
590 height = strtol(value, NULL, 0);
591 break;
592 }
593 }
594
595 return wayland_compositor_create(display, width, height);
596}