blob: 6eb0cd0c02e62e0cd0355f40acf78295817adf28 [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 {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050046 struct weston_compositor base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010047
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 {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050066 struct weston_output base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010067
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øgsberg8334bc12012-01-03 10:29:47 -050074 struct weston_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{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050086 struct weston_input_device *input;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010087
88 input = malloc(sizeof *input);
89 if (input == NULL)
90 return -1;
91
92 memset(input, 0, sizeof *input);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050093 weston_input_device_init(input, &c->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010094
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
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500164static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400165frame_done(void *data, struct wl_callback *wl_callback, uint32_t time)
166{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500167 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400168
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500169 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400170}
171
172static const struct wl_callback_listener frame_listener = {
173 frame_done
174};
175
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500176static void
177wayland_output_repaint(struct weston_output *output_base)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100178{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100179 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500180 struct wayland_compositor *compositor =
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100181 (struct wayland_compositor *) output->base.compositor;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400182 struct wl_callback *callback;
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500183 struct weston_surface *surface;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100184
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500185 if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
186 output->egl_surface, compositor->base.context)) {
187 fprintf(stderr, "failed to make current\n");
188 return;
189 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100190
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500191 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
192 weston_surface_draw(surface, &output->base);
193
194 eglSwapBuffers(compositor->base.display, output->egl_surface);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400195 callback = wl_surface_frame(output->parent.surface);
196 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100197
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500198 return;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100199}
200
201static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500202wayland_output_set_cursor(struct weston_output *output_base,
203 struct weston_input_device *input)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200204{
205 return -1;
206}
207
Matt Roper361d2ad2011-08-29 13:52:23 -0700208static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500209wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700210{
211 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500212 struct weston_compositor *ec = output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700213
214 eglDestroySurface(ec->display, output->egl_surface);
215 wl_egl_window_destroy(output->parent.egl_window);
216 free(output);
217
218 return;
219}
220
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200221static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100222wayland_compositor_create_output(struct wayland_compositor *c,
223 int width, int height)
224{
225 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100226
227 output = malloc(sizeof *output);
228 if (output == NULL)
229 return -1;
230 memset(output, 0, sizeof *output);
231
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400232 output->mode.flags =
233 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
234 output->mode.width = width;
235 output->mode.height = height;
236 output->mode.refresh = 60;
237 wl_list_init(&output->base.mode_list);
238 wl_list_insert(&output->base.mode_list, &output->mode.link);
239
240 output->base.current = &output->mode;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500241 weston_output_init(&output->base, &c->base, 0, 0, width, height,
Benjamin Franzkebe014562011-02-18 17:04:24 +0100242 WL_OUTPUT_FLIPPED);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400243
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100244 output->parent.surface =
245 wl_compositor_create_surface(c->parent.compositor);
246 wl_surface_set_user_data(output->parent.surface, output);
247
Benjamin Franzkebe014562011-02-18 17:04:24 +0100248 output->parent.egl_window =
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400249 wl_egl_window_create(output->parent.surface, width, height);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100250 if (!output->parent.egl_window) {
251 fprintf(stderr, "failure to create wl_egl_window\n");
252 goto cleanup_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100253 }
254
Benjamin Franzkebe014562011-02-18 17:04:24 +0100255 output->egl_surface =
256 eglCreateWindowSurface(c->base.display, c->base.config,
257 output->parent.egl_window, NULL);
258 if (!output->egl_surface) {
259 fprintf(stderr, "failed to create window surface\n");
260 goto cleanup_window;
261 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100262
Benjamin Franzkebe014562011-02-18 17:04:24 +0100263 if (!eglMakeCurrent(c->base.display, output->egl_surface,
264 output->egl_surface, c->base.context)) {
265 fprintf(stderr, "failed to make surface current\n");
266 goto cleanup_surface;
267 return -1;
268 }
269
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200270 output->parent.shell_surface =
271 wl_shell_get_shell_surface(c->parent.shell,
272 output->parent.surface);
273 /* FIXME: add shell_surface listener for resizing */
274 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100275
276 glClearColor(0, 0, 0, 0.5);
277
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500278 output->base.repaint = wayland_output_repaint;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200279 output->base.set_hardware_cursor = wayland_output_set_cursor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700280 output->base.destroy = wayland_output_destroy;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100281
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100282 wl_list_insert(c->base.output_list.prev, &output->base.link);
283
284 return 0;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100285
286cleanup_surface:
287 eglDestroySurface(c->base.display, output->egl_surface);
288cleanup_window:
289 wl_egl_window_destroy(output->parent.egl_window);
290cleanup_output:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500291 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +0100292 free(output);
293
294 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100295}
296
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100297/* Events received from the wayland-server this compositor is client of: */
298
299/* parent output interface */
300static void
301display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400302 struct wl_output *wl_output,
303 int x,
304 int y,
305 int physical_width,
306 int physical_height,
307 int subpixel,
308 const char *make,
309 const char *model)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100310{
311 struct wayland_compositor *c = data;
312
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500313 c->parent.screen_allocation.x = x;
314 c->parent.screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400315}
316
317static void
318display_handle_mode(void *data,
319 struct wl_output *wl_output,
320 uint32_t flags,
321 int width,
322 int height,
323 int refresh)
324{
325 struct wayland_compositor *c = data;
326
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100327 c->parent.screen_allocation.width = width;
328 c->parent.screen_allocation.height = height;
329}
330
331static const struct wl_output_listener output_listener = {
332 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400333 display_handle_mode
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100334};
335
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100336/* parent input interface */
337static void
338input_handle_motion(void *data, struct wl_input_device *input_device,
339 uint32_t time,
340 int32_t x, int32_t y, int32_t sx, int32_t sy)
341{
342 struct wayland_input *input = data;
343 struct wayland_compositor *c = input->compositor;
344
345 notify_motion(c->base.input_device, time, sx, sy);
346}
347
348static void
349input_handle_button(void *data,
350 struct wl_input_device *input_device,
351 uint32_t time, uint32_t button, uint32_t state)
352{
353 struct wayland_input *input = data;
354 struct wayland_compositor *c = input->compositor;
355
356 notify_button(c->base.input_device, time, button, state);
357}
358
359static void
360input_handle_key(void *data, struct wl_input_device *input_device,
361 uint32_t time, uint32_t key, uint32_t state)
362{
363 struct wayland_input *input = data;
364 struct wayland_compositor *c = input->compositor;
365
366 notify_key(c->base.input_device, time, key, state);
367}
368
369static void
370input_handle_pointer_focus(void *data,
371 struct wl_input_device *input_device,
372 uint32_t time, struct wl_surface *surface,
373 int32_t x, int32_t y, int32_t sx, int32_t sy)
374{
375 struct wayland_input *input = data;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500376 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100377 struct wayland_compositor *c = input->compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100378
379 if (surface) {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500380 output = wl_surface_get_user_data(surface);
381 notify_pointer_focus(c->base.input_device,
382 time, &output->base, sx, sy);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100383 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500384 notify_pointer_focus(c->base.input_device, time, NULL, 0, 0);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100385 }
386}
387
388static void
389input_handle_keyboard_focus(void *data,
390 struct wl_input_device *input_device,
391 uint32_t time,
392 struct wl_surface *surface,
393 struct wl_array *keys)
394{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500395 struct wayland_input *input = data;
396 struct wayland_compositor *c = input->compositor;
397 struct wayland_output *output;
398
399 if (surface) {
400 output = wl_surface_get_user_data(surface);
401 notify_keyboard_focus(c->base.input_device,
402 time, &output->base, keys);
403 } else {
404 notify_keyboard_focus(c->base.input_device, time, NULL, NULL);
405 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100406}
407
408static const struct wl_input_device_listener input_device_listener = {
409 input_handle_motion,
410 input_handle_button,
411 input_handle_key,
412 input_handle_pointer_focus,
413 input_handle_keyboard_focus,
414};
415
416static void
417display_add_input(struct wayland_compositor *c, uint32_t id)
418{
419 struct wayland_input *input;
420
421 input = malloc(sizeof *input);
422 if (input == NULL)
423 return;
424
425 memset(input, 0, sizeof *input);
426
427 input->compositor = c;
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400428 input->input_device = wl_display_bind(c->parent.display,
429 id, &wl_input_device_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100430 wl_list_insert(c->input_list.prev, &input->link);
431
432 wl_input_device_add_listener(input->input_device,
433 &input_device_listener, input);
434 wl_input_device_set_user_data(input->input_device, input);
435}
436
437static void
438display_handle_global(struct wl_display *display, uint32_t id,
439 const char *interface, uint32_t version, void *data)
440{
441 struct wayland_compositor *c = data;
442
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200443 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400444 c->parent.compositor =
445 wl_display_bind(display, id, &wl_compositor_interface);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200446 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400447 c->parent.output =
448 wl_display_bind(display, id, &wl_output_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100449 wl_output_add_listener(c->parent.output, &output_listener, c);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200450 } else if (strcmp(interface, "wl_input_device") == 0) {
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100451 display_add_input(c, id);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200452 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400453 c->parent.shell =
454 wl_display_bind(display, id, &wl_shell_interface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100455 }
456}
457
458static int
459update_event_mask(uint32_t mask, void *data)
460{
461 struct wayland_compositor *c = data;
462
463 c->parent.event_mask = mask;
464 if (c->parent.wl_source)
465 wl_event_source_fd_update(c->parent.wl_source, mask);
466
467 return 0;
468}
469
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400470static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100471wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
472{
473 struct wayland_compositor *c = data;
474
475 if (mask & WL_EVENT_READABLE)
476 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
Kristian Høgsbergf258a312011-12-28 22:51:20 -0500477 if (mask & WL_EVENT_WRITABLE)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100478 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400479
480 return 1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100481}
482
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500483static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500484wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500485{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500486 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -0700487
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500488 free(ec);
489}
490
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500491static struct weston_compositor *
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100492wayland_compositor_create(struct wl_display *display, int width, int height)
493{
494 struct wayland_compositor *c;
495 struct wl_event_loop *loop;
496 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100497
498 c = malloc(sizeof *c);
499 if (c == NULL)
500 return NULL;
501
502 memset(c, 0, sizeof *c);
503
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500504 c->parent.display = wl_display_connect(NULL);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100505
506 if (c->parent.display == NULL) {
507 fprintf(stderr, "failed to create display: %m\n");
508 return NULL;
509 }
510
511 wl_list_init(&c->input_list);
512 wl_display_add_global_listener(c->parent.display,
513 display_handle_global, c);
514
515 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
516
517 c->base.wl_display = display;
518 if (wayland_compositor_init_egl(c) < 0)
519 return NULL;
520
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100521 c->base.destroy = wayland_destroy;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100522
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100523 /* Can't init base class until we have a current egl context */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500524 if (weston_compositor_init(&c->base, display) < 0)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100525 return NULL;
526
527 if (wayland_compositor_create_output(c, width, height) < 0)
528 return NULL;
529
530 if (wayland_input_create(c) < 0)
531 return NULL;
532
533 loop = wl_display_get_event_loop(c->base.wl_display);
534
535 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
536 c->parent.wl_source =
537 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
538 wayland_compositor_handle_event, c);
539 if (c->parent.wl_source == NULL)
540 return NULL;
541
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100542 return &c->base;
543}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400544
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500545struct weston_compositor *
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400546backend_init(struct wl_display *display, char *options);
547
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500548WL_EXPORT struct weston_compositor *
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400549backend_init(struct wl_display *display, char *options)
550{
551 int width = 1024, height = 640, i;
552 char *p, *value;
553
554 static char * const tokens[] = { "width", "height", NULL };
555
556 p = options;
557 while (i = getsubopt(&p, tokens, &value), i != -1) {
558 switch (i) {
559 case 0:
560 width = strtol(value, NULL, 0);
561 break;
562 case 1:
563 height = strtol(value, NULL, 0);
564 break;
565 }
566 }
567
568 return wayland_compositor_create(display, width, height);
569}