blob: a99ca3a637b597374fa1354283ad6d1f13fb226c [file] [log] [blame]
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Benjamin Franzke
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01003 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04004 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010013 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040014 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010021 */
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
27#include <stddef.h>
28#define _GNU_SOURCE
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <fcntl.h>
33#include <unistd.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010034
Benjamin Franzkebe014562011-02-18 17:04:24 +010035#include <wayland-client.h>
36#include <wayland-egl.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010037
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010038#include <GLES2/gl2.h>
39#include <GLES2/gl2ext.h>
40#include <EGL/egl.h>
41#include <EGL/eglext.h>
42
43#include "compositor.h"
44
45struct wayland_compositor {
46 struct wlsc_compositor base;
47
48 struct {
49 struct wl_display *display;
50 struct wl_compositor *compositor;
51 struct wl_shell *shell;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010052 struct wl_output *output;
53
54 struct {
55 int32_t x, y, width, height;
56 } screen_allocation;
57
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010058 struct wl_event_source *wl_source;
59 uint32_t event_mask;
60 } parent;
61
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010062 struct wl_list input_list;
63};
64
65struct wayland_output {
66 struct wlsc_output base;
67
68 struct {
69 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020070 struct wl_shell_surface *shell_surface;
Benjamin Franzkebe014562011-02-18 17:04:24 +010071 struct wl_egl_window *egl_window;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010072 } parent;
Benjamin Franzkebe014562011-02-18 17:04:24 +010073 EGLSurface egl_surface;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040074 struct wlsc_mode mode;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010075};
76
77struct wayland_input {
78 struct wayland_compositor *compositor;
79 struct wl_input_device *input_device;
80 struct wl_list link;
81};
82
83static int
84wayland_input_create(struct wayland_compositor *c)
85{
86 struct wlsc_input_device *input;
87
88 input = malloc(sizeof *input);
89 if (input == NULL)
90 return -1;
91
92 memset(input, 0, sizeof *input);
93 wlsc_input_device_init(input, &c->base);
94
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -050095 c->base.input_device = &input->input_device;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010096
97 return 0;
98}
99
100static int
101wayland_compositor_init_egl(struct wayland_compositor *c)
102{
103 EGLint major, minor;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100104 EGLint n;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100105 const char *extensions;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100106 EGLint config_attribs[] = {
107 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
108 EGL_RED_SIZE, 1,
109 EGL_GREEN_SIZE, 1,
110 EGL_BLUE_SIZE, 1,
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400111 EGL_ALPHA_SIZE, 0,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100112 EGL_DEPTH_SIZE, 1,
Kristian Høgsbergd28ab362011-03-02 11:36:30 -0500113 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100114 EGL_NONE
115 };
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100116 static const EGLint context_attribs[] = {
117 EGL_CONTEXT_CLIENT_VERSION, 2,
118 EGL_NONE
119 };
120
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400121 c->base.display = eglGetDisplay(c->parent.display);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100122 if (c->base.display == NULL) {
123 fprintf(stderr, "failed to create display\n");
124 return -1;
125 }
126
127 if (!eglInitialize(c->base.display, &major, &minor)) {
128 fprintf(stderr, "failed to initialize display\n");
129 return -1;
130 }
131
132 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
Ander Conselvan de Oliveiraef7c8d92011-11-01 16:37:41 +0200133 if (!strstr(extensions, "EGL_KHR_surfaceless_gles2")) {
134 fprintf(stderr, "EGL_KHR_surfaceless_gles2 not available\n");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100135 return -1;
136 }
137
138 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
139 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
140 return -1;
141 }
Benjamin Franzkebe014562011-02-18 17:04:24 +0100142 if (!eglChooseConfig(c->base.display, config_attribs,
143 &c->base.config, 1, &n) || n == 0) {
144 fprintf(stderr, "failed to choose config: %d\n", n);
145 return -1;
146 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100147
Benjamin Franzkebe014562011-02-18 17:04:24 +0100148 c->base.context = eglCreateContext(c->base.display, c->base.config,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100149 EGL_NO_CONTEXT, context_attribs);
150 if (c->base.context == NULL) {
151 fprintf(stderr, "failed to create context\n");
152 return -1;
153 }
154
155 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
156 EGL_NO_SURFACE, c->base.context)) {
157 fprintf(stderr, "failed to make context current\n");
158 return -1;
159 }
160
161 return 0;
162}
163
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100164static int
165wayland_output_prepare_render(struct wlsc_output *output_base)
166{
167 struct wayland_output *output = (struct wayland_output *) output_base;
168 struct wlsc_compositor *ec = output->base.compositor;
169
170 if (!eglMakeCurrent(ec->display, output->egl_surface,
171 output->egl_surface, ec->context)) {
172 fprintf(stderr, "failed to make current\n");
173 return -1;
174 }
175
176 return 0;
177}
178
Kristian Høgsberg33418202011-08-16 23:01:28 -0400179static void
180frame_done(void *data, struct wl_callback *wl_callback, uint32_t time)
181{
182 struct wlsc_output *output = data;
183
184 wlsc_output_finish_frame(output, time);
185}
186
187static const struct wl_callback_listener frame_listener = {
188 frame_done
189};
190
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100191static int
192wayland_output_present(struct wlsc_output *output_base)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100193{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100194 struct wayland_output *output = (struct wayland_output *) output_base;
195 struct wayland_compositor *c =
196 (struct wayland_compositor *) output->base.compositor;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400197 struct wl_callback *callback;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100198
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100199 if (wayland_output_prepare_render(&output->base))
200 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100201
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100202 eglSwapBuffers(c->base.display, output->egl_surface);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400203 callback = wl_surface_frame(output->parent.surface);
204 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100205
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100206 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100207}
208
209static int
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200210wayland_output_prepare_scanout_surface(struct wlsc_output *output_base,
211 struct wlsc_surface *es)
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200212{
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200213 return -1;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200214}
215
216static int
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200217wayland_output_set_cursor(struct wlsc_output *output_base,
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400218 struct wlsc_input_device *input)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200219{
220 return -1;
221}
222
Matt Roper361d2ad2011-08-29 13:52:23 -0700223static void
224wayland_output_destroy(struct wlsc_output *output_base)
225{
226 struct wayland_output *output = (struct wayland_output *) output_base;
227 struct wlsc_compositor *ec = output->base.compositor;
228
229 eglDestroySurface(ec->display, output->egl_surface);
230 wl_egl_window_destroy(output->parent.egl_window);
231 free(output);
232
233 return;
234}
235
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200236static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100237wayland_compositor_create_output(struct wayland_compositor *c,
238 int width, int height)
239{
240 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100241
242 output = malloc(sizeof *output);
243 if (output == NULL)
244 return -1;
245 memset(output, 0, sizeof *output);
246
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400247 output->mode.flags =
248 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
249 output->mode.width = width;
250 output->mode.height = height;
251 output->mode.refresh = 60;
252 wl_list_init(&output->base.mode_list);
253 wl_list_insert(&output->base.mode_list, &output->mode.link);
254
255 output->base.current = &output->mode;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100256 wlsc_output_init(&output->base, &c->base, 0, 0, width, height,
257 WL_OUTPUT_FLIPPED);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400258
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100259 output->parent.surface =
260 wl_compositor_create_surface(c->parent.compositor);
261 wl_surface_set_user_data(output->parent.surface, output);
262
Benjamin Franzkebe014562011-02-18 17:04:24 +0100263 output->parent.egl_window =
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400264 wl_egl_window_create(output->parent.surface, width, height);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100265 if (!output->parent.egl_window) {
266 fprintf(stderr, "failure to create wl_egl_window\n");
267 goto cleanup_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100268 }
269
Benjamin Franzkebe014562011-02-18 17:04:24 +0100270 output->egl_surface =
271 eglCreateWindowSurface(c->base.display, c->base.config,
272 output->parent.egl_window, NULL);
273 if (!output->egl_surface) {
274 fprintf(stderr, "failed to create window surface\n");
275 goto cleanup_window;
276 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100277
Benjamin Franzkebe014562011-02-18 17:04:24 +0100278 if (!eglMakeCurrent(c->base.display, output->egl_surface,
279 output->egl_surface, c->base.context)) {
280 fprintf(stderr, "failed to make surface current\n");
281 goto cleanup_surface;
282 return -1;
283 }
284
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200285 output->parent.shell_surface =
286 wl_shell_get_shell_surface(c->parent.shell,
287 output->parent.surface);
288 /* FIXME: add shell_surface listener for resizing */
289 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100290
291 glClearColor(0, 0, 0, 0.5);
292
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100293 output->base.prepare_render = wayland_output_prepare_render;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100294 output->base.present = wayland_output_present;
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200295 output->base.prepare_scanout_surface =
296 wayland_output_prepare_scanout_surface;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200297 output->base.set_hardware_cursor = wayland_output_set_cursor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700298 output->base.destroy = wayland_output_destroy;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100299
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100300 wl_list_insert(c->base.output_list.prev, &output->base.link);
301
302 return 0;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100303
304cleanup_surface:
305 eglDestroySurface(c->base.display, output->egl_surface);
306cleanup_window:
307 wl_egl_window_destroy(output->parent.egl_window);
308cleanup_output:
309 /* FIXME: cleanup wlsc_output */
310 free(output);
311
312 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100313}
314
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100315/* Events received from the wayland-server this compositor is client of: */
316
317/* parent output interface */
318static void
319display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400320 struct wl_output *wl_output,
321 int x,
322 int y,
323 int physical_width,
324 int physical_height,
325 int subpixel,
326 const char *make,
327 const char *model)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100328{
329 struct wayland_compositor *c = data;
330
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500331 c->parent.screen_allocation.x = x;
332 c->parent.screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400333}
334
335static void
336display_handle_mode(void *data,
337 struct wl_output *wl_output,
338 uint32_t flags,
339 int width,
340 int height,
341 int refresh)
342{
343 struct wayland_compositor *c = data;
344
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100345 c->parent.screen_allocation.width = width;
346 c->parent.screen_allocation.height = height;
347}
348
349static const struct wl_output_listener output_listener = {
350 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400351 display_handle_mode
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100352};
353
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100354/* parent input interface */
355static void
356input_handle_motion(void *data, struct wl_input_device *input_device,
357 uint32_t time,
358 int32_t x, int32_t y, int32_t sx, int32_t sy)
359{
360 struct wayland_input *input = data;
361 struct wayland_compositor *c = input->compositor;
362
363 notify_motion(c->base.input_device, time, sx, sy);
364}
365
366static void
367input_handle_button(void *data,
368 struct wl_input_device *input_device,
369 uint32_t time, uint32_t button, uint32_t state)
370{
371 struct wayland_input *input = data;
372 struct wayland_compositor *c = input->compositor;
373
374 notify_button(c->base.input_device, time, button, state);
375}
376
377static void
378input_handle_key(void *data, struct wl_input_device *input_device,
379 uint32_t time, uint32_t key, uint32_t state)
380{
381 struct wayland_input *input = data;
382 struct wayland_compositor *c = input->compositor;
383
384 notify_key(c->base.input_device, time, key, state);
385}
386
387static void
388input_handle_pointer_focus(void *data,
389 struct wl_input_device *input_device,
390 uint32_t time, struct wl_surface *surface,
391 int32_t x, int32_t y, int32_t sx, int32_t sy)
392{
393 struct wayland_input *input = data;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500394 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100395 struct wayland_compositor *c = input->compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100396
397 if (surface) {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500398 output = wl_surface_get_user_data(surface);
399 notify_pointer_focus(c->base.input_device,
400 time, &output->base, sx, sy);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100401 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500402 notify_pointer_focus(c->base.input_device, time, NULL, 0, 0);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100403 }
404}
405
406static void
407input_handle_keyboard_focus(void *data,
408 struct wl_input_device *input_device,
409 uint32_t time,
410 struct wl_surface *surface,
411 struct wl_array *keys)
412{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500413 struct wayland_input *input = data;
414 struct wayland_compositor *c = input->compositor;
415 struct wayland_output *output;
416
417 if (surface) {
418 output = wl_surface_get_user_data(surface);
419 notify_keyboard_focus(c->base.input_device,
420 time, &output->base, keys);
421 } else {
422 notify_keyboard_focus(c->base.input_device, time, NULL, NULL);
423 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100424}
425
426static const struct wl_input_device_listener input_device_listener = {
427 input_handle_motion,
428 input_handle_button,
429 input_handle_key,
430 input_handle_pointer_focus,
431 input_handle_keyboard_focus,
432};
433
434static void
435display_add_input(struct wayland_compositor *c, uint32_t id)
436{
437 struct wayland_input *input;
438
439 input = malloc(sizeof *input);
440 if (input == NULL)
441 return;
442
443 memset(input, 0, sizeof *input);
444
445 input->compositor = c;
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400446 input->input_device = wl_display_bind(c->parent.display,
447 id, &wl_input_device_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100448 wl_list_insert(c->input_list.prev, &input->link);
449
450 wl_input_device_add_listener(input->input_device,
451 &input_device_listener, input);
452 wl_input_device_set_user_data(input->input_device, input);
453}
454
455static void
456display_handle_global(struct wl_display *display, uint32_t id,
457 const char *interface, uint32_t version, void *data)
458{
459 struct wayland_compositor *c = data;
460
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200461 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400462 c->parent.compositor =
463 wl_display_bind(display, id, &wl_compositor_interface);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200464 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400465 c->parent.output =
466 wl_display_bind(display, id, &wl_output_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100467 wl_output_add_listener(c->parent.output, &output_listener, c);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200468 } else if (strcmp(interface, "wl_input_device") == 0) {
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100469 display_add_input(c, id);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200470 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400471 c->parent.shell =
472 wl_display_bind(display, id, &wl_shell_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100473 }
474}
475
476static int
477update_event_mask(uint32_t mask, void *data)
478{
479 struct wayland_compositor *c = data;
480
481 c->parent.event_mask = mask;
482 if (c->parent.wl_source)
483 wl_event_source_fd_update(c->parent.wl_source, mask);
484
485 return 0;
486}
487
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400488static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100489wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
490{
491 struct wayland_compositor *c = data;
492
493 if (mask & WL_EVENT_READABLE)
494 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
Kristian Høgsbergf258a312011-12-28 22:51:20 -0500495 if (mask & WL_EVENT_WRITABLE)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100496 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400497
498 return 1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100499}
500
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500501static void
502wayland_destroy(struct wlsc_compositor *ec)
503{
Matt Roper361d2ad2011-08-29 13:52:23 -0700504 wlsc_compositor_shutdown(ec);
505
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500506 free(ec);
507}
508
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400509static struct wlsc_compositor *
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100510wayland_compositor_create(struct wl_display *display, int width, int height)
511{
512 struct wayland_compositor *c;
513 struct wl_event_loop *loop;
514 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100515
516 c = malloc(sizeof *c);
517 if (c == NULL)
518 return NULL;
519
520 memset(c, 0, sizeof *c);
521
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500522 c->parent.display = wl_display_connect(NULL);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100523
524 if (c->parent.display == NULL) {
525 fprintf(stderr, "failed to create display: %m\n");
526 return NULL;
527 }
528
529 wl_list_init(&c->input_list);
530 wl_display_add_global_listener(c->parent.display,
531 display_handle_global, c);
532
533 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
534
535 c->base.wl_display = display;
536 if (wayland_compositor_init_egl(c) < 0)
537 return NULL;
538
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100539 c->base.destroy = wayland_destroy;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100540
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100541 /* Can't init base class until we have a current egl context */
542 if (wlsc_compositor_init(&c->base, display) < 0)
543 return NULL;
544
545 if (wayland_compositor_create_output(c, width, height) < 0)
546 return NULL;
547
548 if (wayland_input_create(c) < 0)
549 return NULL;
550
551 loop = wl_display_get_event_loop(c->base.wl_display);
552
553 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
554 c->parent.wl_source =
555 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
556 wayland_compositor_handle_event, c);
557 if (c->parent.wl_source == NULL)
558 return NULL;
559
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100560 return &c->base;
561}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400562
563struct wlsc_compositor *
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400564backend_init(struct wl_display *display, char *options);
565
566WL_EXPORT struct wlsc_compositor *
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400567backend_init(struct wl_display *display, char *options)
568{
569 int width = 1024, height = 640, i;
570 char *p, *value;
571
572 static char * const tokens[] = { "width", "height", NULL };
573
574 p = options;
575 while (i = getsubopt(&p, tokens, &value), i != -1) {
576 switch (i) {
577 case 0:
578 width = strtol(value, NULL, 0);
579 break;
580 case 1:
581 height = strtol(value, NULL, 0);
582 break;
583 }
584 }
585
586 return wayland_compositor_create(display, width, height);
587}