blob: 771bf69dfd65b05ddb95a025fdbc31a56fb965bb [file] [log] [blame]
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2010-2011 Intel Corporation
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04005 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040014 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040015 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040022 */
23
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +080024#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040028#include <stddef.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <fcntl.h>
33#include <unistd.h>
34#include <errno.h>
35#include <sys/time.h>
Kristian Høgsbergf9112b22010-06-14 12:53:43 -040036#include <linux/input.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040037
38#include <xcb/xcb.h>
Benjamin Franzke3b288af2011-02-20 19:58:42 +010039#include <X11/Xlib.h>
40#include <X11/Xlib-xcb.h>
41
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040042#include <GLES2/gl2.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040043#include <EGL/egl.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040044
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040045#include "compositor.h"
46
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040047struct x11_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050048 struct weston_compositor base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040049
Benjamin Franzke3b288af2011-02-20 19:58:42 +010050 Display *dpy;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040051 xcb_connection_t *conn;
52 xcb_screen_t *screen;
53 xcb_cursor_t null_cursor;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -050054 struct wl_array keys;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040055 struct wl_event_source *xcb_source;
56 struct {
57 xcb_atom_t wm_protocols;
58 xcb_atom_t wm_normal_hints;
59 xcb_atom_t wm_size_hints;
60 xcb_atom_t wm_delete_window;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050061 xcb_atom_t wm_class;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040062 xcb_atom_t net_wm_name;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050063 xcb_atom_t net_wm_icon;
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -040064 xcb_atom_t net_wm_state;
65 xcb_atom_t net_wm_state_fullscreen;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050066 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040067 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050068 xcb_atom_t cardinal;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040069 } atom;
70};
71
72struct x11_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050073 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040074
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040075 xcb_window_t window;
Benjamin Franzke84290d02011-03-02 10:07:59 +010076 EGLSurface egl_surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050077 struct weston_mode mode;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -040078 struct wl_event_source *finish_frame_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040079};
80
81struct x11_input {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050082 struct weston_input_device base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040083};
84
85
Darxus55973f22010-11-22 21:24:39 -050086static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040087x11_input_create(struct x11_compositor *c)
88{
89 struct x11_input *input;
90
91 input = malloc(sizeof *input);
92 if (input == NULL)
Darxus55973f22010-11-22 21:24:39 -050093 return -1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040094
95 memset(input, 0, sizeof *input);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050096 weston_input_device_init(&input->base, &c->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040097
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -050098 c->base.input_device = &input->base.input_device;
Darxus55973f22010-11-22 21:24:39 -050099
100 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400101}
102
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200103static void
104x11_input_destroy(struct x11_compositor *compositor)
105{
106 struct x11_input *input = container_of(compositor->base.input_device,
107 struct x11_input,
108 base.input_device);
109
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500110 weston_input_device_release(&input->base);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200111 free(input);
112}
113
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400114static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400115x11_compositor_init_egl(struct x11_compositor *c)
116{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400117 EGLint major, minor;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100118 EGLint n;
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400119 const char *extensions;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100120 EGLint config_attribs[] = {
121 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
122 EGL_RED_SIZE, 1,
123 EGL_GREEN_SIZE, 1,
124 EGL_BLUE_SIZE, 1,
125 EGL_DEPTH_SIZE, 1,
126 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
127 EGL_NONE
128 };
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400129 static const EGLint context_attribs[] = {
130 EGL_CONTEXT_CLIENT_VERSION, 2,
131 EGL_NONE
132 };
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400133
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100134 c->base.display = eglGetDisplay(c->dpy);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400135 if (c->base.display == NULL) {
136 fprintf(stderr, "failed to create display\n");
137 return -1;
138 }
139
140 if (!eglInitialize(c->base.display, &major, &minor)) {
141 fprintf(stderr, "failed to initialize display\n");
142 return -1;
143 }
144
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400145 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
Ander Conselvan de Oliveiraef7c8d92011-11-01 16:37:41 +0200146 if (!strstr(extensions, "EGL_KHR_surfaceless_gles2")) {
147 fprintf(stderr, "EGL_KHR_surfaceless_gles2 not available\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400148 return -1;
149 }
150
Darxus55973f22010-11-22 21:24:39 -0500151 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
152 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
153 return -1;
154 }
Benjamin Franzke84290d02011-03-02 10:07:59 +0100155 if (!eglChooseConfig(c->base.display, config_attribs,
156 &c->base.config, 1, &n) || n == 0) {
157 fprintf(stderr, "failed to choose config: %d\n", n);
158 return -1;
159 }
Darxus55973f22010-11-22 21:24:39 -0500160
Benjamin Franzke84290d02011-03-02 10:07:59 +0100161 c->base.context = eglCreateContext(c->base.display, c->base.config,
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400162 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400163 if (c->base.context == NULL) {
164 fprintf(stderr, "failed to create context\n");
165 return -1;
166 }
167
168 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
169 EGL_NO_SURFACE, c->base.context)) {
170 fprintf(stderr, "failed to make context current\n");
171 return -1;
172 }
173
174 return 0;
175}
176
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200177static void
178x11_compositor_fini_egl(struct x11_compositor *compositor)
179{
180 eglMakeCurrent(compositor->base.display,
181 EGL_NO_SURFACE, EGL_NO_SURFACE,
182 EGL_NO_CONTEXT);
183
184 eglTerminate(compositor->base.display);
185 eglReleaseThread();
186}
187
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100188static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500189x11_output_prepare_render(struct weston_output *output_base)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100190{
191 struct x11_output *output = (struct x11_output *) output_base;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500192 struct weston_compositor *ec = output->base.compositor;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100193
194 if (!eglMakeCurrent(ec->display, output->egl_surface,
195 output->egl_surface, ec->context)) {
196 fprintf(stderr, "failed to make current\n");
197 return -1;
198 }
199
200 return 0;
201}
202
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500203static void
204x11_output_repaint(struct weston_output *output)
205{
206 struct weston_compositor *compositor = output->compositor;
207 struct weston_surface *surface;
208
209 wl_list_for_each_reverse(surface, &compositor->surface_list, link)
210 weston_surface_draw(surface, output);
211}
212
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100213static int
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400214finish_frame_handler(void *data)
215{
216 struct x11_output *output = data;
217 uint32_t msec;
218 struct timeval tv;
219
220 gettimeofday(&tv, NULL);
221 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500222 weston_output_finish_frame(&output->base, msec);
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400223
224 return 1;
225}
226
227static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500228x11_output_present(struct weston_output *output_base)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400229{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100230 struct x11_output *output = (struct x11_output *) output_base;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500231 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400232
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100233 if (x11_output_prepare_render(&output->base))
234 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100235
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100236 eglSwapBuffers(ec->display, output->egl_surface);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400237
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400238 wl_event_source_timer_update(output->finish_frame_timer, 10);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100239
240 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400241}
242
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200243static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500244x11_output_prepare_scanout_surface(struct weston_output *output_base,
245 struct weston_surface *es)
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200246{
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200247 return -1;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200248}
249
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200250static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500251x11_output_set_cursor(struct weston_output *output_base,
252 struct weston_input_device *input)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200253{
254 return -1;
255}
256
Matt Roper361d2ad2011-08-29 13:52:23 -0700257static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500258x11_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700259{
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200260 struct x11_output *output = (struct x11_output *)output_base;
261 struct x11_compositor *compositor =
262 (struct x11_compositor *)output->base.compositor;
263
264 wl_list_remove(&output->base.link);
265 wl_event_source_remove(output->finish_frame_timer);
266
267 eglDestroySurface(compositor->base.display, output->egl_surface);
268
269 xcb_destroy_window(compositor->conn, output->window);
270
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500271 weston_output_destroy(&output->base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200272
273 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700274}
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200275
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400276static void
277x11_output_set_wm_protocols(struct x11_output *output)
278{
279 xcb_atom_t list[1];
280 struct x11_compositor *c =
281 (struct x11_compositor *) output->base.compositor;
282
283 list[0] = c->atom.wm_delete_window;
284 xcb_change_property (c->conn,
285 XCB_PROP_MODE_REPLACE,
286 output->window,
287 c->atom.wm_protocols,
288 XCB_ATOM_ATOM,
289 32,
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500290 ARRAY_LENGTH(list),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400291 list);
292}
293
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400294static void
295x11_output_change_state(struct x11_output *output, int add, xcb_atom_t state)
296{
297 xcb_client_message_event_t event;
298 struct x11_compositor *c =
299 (struct x11_compositor *) output->base.compositor;
300 xcb_screen_iterator_t iter;
301
302#define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
303#define _NET_WM_STATE_ADD 1 /* add/set property */
304#define _NET_WM_STATE_TOGGLE 2 /* toggle property */
305
306 memset(&event, 0, sizeof event);
307 event.response_type = XCB_CLIENT_MESSAGE;
308 event.format = 32;
309 event.window = output->window;
310 event.type = c->atom.net_wm_state;
311
312 event.data.data32[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
313 event.data.data32[1] = state;
314 event.data.data32[2] = 0;
315 event.data.data32[3] = 0;
316 event.data.data32[4] = 0;
317
318 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
319 xcb_send_event(c->conn, 0, iter.data->root,
320 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
321 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
322 (void *) &event);
323}
324
325
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400326struct wm_normal_hints {
327 uint32_t flags;
328 uint32_t pad[4];
329 int32_t min_width, min_height;
330 int32_t max_width, max_height;
331 int32_t width_inc, height_inc;
332 int32_t min_aspect_x, min_aspect_y;
333 int32_t max_aspect_x, max_aspect_y;
334 int32_t base_width, base_height;
335 int32_t win_gravity;
336};
337
338#define WM_NORMAL_HINTS_MIN_SIZE 16
339#define WM_NORMAL_HINTS_MAX_SIZE 32
340
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500341static void
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400342x11_output_set_icon(struct x11_compositor *c,
343 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500344{
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400345 uint32_t *icon, *pixels, stride;
346 int32_t width, height;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500347
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500348 pixels = weston_load_image(filename, &width, &height, &stride);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500349 if (!pixels)
350 return;
351 icon = malloc(width * height * 4 + 8);
352 if (!icon) {
353 free(pixels);
354 return;
355 }
356
357 icon[0] = width;
358 icon[1] = height;
359 memcpy(icon + 2, pixels, width * height * 4);
360 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
361 c->atom.net_wm_icon, c->atom.cardinal, 32,
362 width * height + 2, icon);
363 free(icon);
364 free(pixels);
365}
366
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400367static int
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700368x11_compositor_create_output(struct x11_compositor *c, int x, int y,
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400369 int width, int height, int fullscreen)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400370{
Kristian Høgsberg86000402012-01-03 23:24:14 -0500371 static const char name[] = "Weston Compositor";
372 static const char class[] = "weston-1\0Weston Compositor";
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400373 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400374 xcb_screen_iterator_t iter;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400375 struct wm_normal_hints normal_hints;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400376 struct wl_event_loop *loop;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400377 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
378 uint32_t values[2] = {
379 XCB_EVENT_MASK_KEY_PRESS |
380 XCB_EVENT_MASK_KEY_RELEASE |
381 XCB_EVENT_MASK_BUTTON_PRESS |
382 XCB_EVENT_MASK_BUTTON_RELEASE |
383 XCB_EVENT_MASK_POINTER_MOTION |
384 XCB_EVENT_MASK_EXPOSURE |
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400385 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
386 XCB_EVENT_MASK_ENTER_WINDOW |
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500387 XCB_EVENT_MASK_LEAVE_WINDOW |
388 XCB_EVENT_MASK_KEYMAP_STATE |
389 XCB_EVENT_MASK_FOCUS_CHANGE,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400390 0
391 };
392
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400393 output = malloc(sizeof *output);
394 if (output == NULL)
395 return -1;
396
397 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400398
399 output->mode.flags =
400 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
401 output->mode.width = width;
402 output->mode.height = height;
403 output->mode.refresh = 60;
404 wl_list_init(&output->base.mode_list);
405 wl_list_insert(&output->base.mode_list, &output->mode.link);
406
407 output->base.current = &output->mode;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500408 weston_output_init(&output->base, &c->base, x, y, width, height,
Benjamin Franzke84290d02011-03-02 10:07:59 +0100409 WL_OUTPUT_FLIPPED);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400410
411 values[1] = c->null_cursor;
412 output->window = xcb_generate_id(c->conn);
413 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
414 xcb_create_window(c->conn,
415 XCB_COPY_FROM_PARENT,
416 output->window,
417 iter.data->root,
418 0, 0,
419 width, height,
420 0,
421 XCB_WINDOW_CLASS_INPUT_OUTPUT,
422 iter.data->root_visual,
423 mask, values);
424
425 /* Don't resize me. */
426 memset(&normal_hints, 0, sizeof normal_hints);
427 normal_hints.flags =
428 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
429 normal_hints.min_width = width;
430 normal_hints.min_height = height;
431 normal_hints.max_width = width;
432 normal_hints.max_height = height;
433 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
434 c->atom.wm_normal_hints,
435 c->atom.wm_size_hints, 32,
436 sizeof normal_hints / 4,
437 (uint8_t *) &normal_hints);
438
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400439 /* Set window name. Don't bother with non-EWMH WMs. */
440 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
441 c->atom.net_wm_name, c->atom.utf8_string, 8,
442 strlen(name), name);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500443 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
444 c->atom.wm_class, c->atom.string, 8,
445 sizeof class, class);
446
Kristian Høgsberg86000402012-01-03 23:24:14 -0500447 x11_output_set_icon(c, output, DATADIR "/weston/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500448
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500449 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400450
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400451 x11_output_set_wm_protocols(output);
452
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400453 if (fullscreen)
454 x11_output_change_state(output, 1,
455 c->atom.net_wm_state_fullscreen);
456
Benjamin Franzke84290d02011-03-02 10:07:59 +0100457 output->egl_surface =
458 eglCreateWindowSurface(c->base.display, c->base.config,
459 output->window, NULL);
460 if (!output->egl_surface) {
461 fprintf(stderr, "failed to create window surface\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400462 return -1;
463 }
Benjamin Franzke84290d02011-03-02 10:07:59 +0100464 if (!eglMakeCurrent(c->base.display, output->egl_surface,
465 output->egl_surface, c->base.context)) {
466 fprintf(stderr, "failed to make surface current\n");
467 return -1;
468 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400469
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400470 loop = wl_display_get_event_loop(c->base.wl_display);
471 output->finish_frame_timer =
472 wl_event_loop_add_timer(loop, finish_frame_handler, output);
473
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100474 output->base.prepare_render = x11_output_prepare_render;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500475 output->base.repaint = x11_output_repaint;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100476 output->base.present = x11_output_present;
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200477 output->base.prepare_scanout_surface =
478 x11_output_prepare_scanout_surface;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200479 output->base.set_hardware_cursor = x11_output_set_cursor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700480 output->base.destroy = x11_output_destroy;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100481
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400482 wl_list_insert(c->base.output_list.prev, &output->base.link);
483
484 return 0;
485}
486
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400487static struct x11_output *
488x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
489{
490 struct x11_output *output;
491
492 wl_list_for_each(output, &c->base.output_list, base.link) {
493 if (output->window == window)
494 return output;
495 }
496
497 return NULL;
498}
499
Tiago Vignattia3a71622011-12-08 17:03:17 +0200500static void
501x11_compositor_deliver_button_event(struct x11_compositor *c,
502 xcb_generic_event_t *event, int state)
503{
504 xcb_button_press_event_t *button_event =
505 (xcb_button_press_event_t *) event;
506 int button;
507
508 switch (button_event->detail) {
509 default:
510 button = button_event->detail + BTN_LEFT - 1;
511 break;
512 case 2:
513 button = BTN_MIDDLE;
514 break;
515 case 3:
516 button = BTN_RIGHT;
517 break;
518 case 4:
519 case 5:
520 case 6:
521 case 7:
522 /* X11 sends wheel events as buttons events. But
523 * linux input treats as REL_WHEEL, therefore not
524 * button type at all. When we update the input
525 * protocol and get the 'axis' event, we'll send
526 * scroll events as axis events. */
527 return;
528 }
529
530 notify_button(c->base.input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500531 weston_compositor_get_time(), button, state);
Tiago Vignattia3a71622011-12-08 17:03:17 +0200532}
533
Kristian Høgsbergee724822011-04-21 14:51:44 -0400534static int
535x11_compositor_next_event(struct x11_compositor *c,
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400536 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -0400537{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400538 if (mask & WL_EVENT_READABLE) {
539 *event = xcb_poll_for_event(c->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -0400540 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400541#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400542 *event = xcb_poll_for_queued_event(c->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400543#else
544 *event = xcb_poll_for_event(c->conn);
545#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -0400546 }
547
548 return *event != NULL;
549}
550
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400551static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400552x11_compositor_handle_event(int fd, uint32_t mask, void *data)
553{
554 struct x11_compositor *c = data;
555 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400556 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400557 xcb_client_message_event_t *client_message;
558 xcb_motion_notify_event_t *motion_notify;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500559 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400560 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500561 xcb_keymap_notify_event_t *keymap_notify;
562 xcb_focus_in_event_t *focus_in;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400563 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500564 uint32_t *k;
565 int i, set;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400566
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400567 prev = NULL;
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400568 while (x11_compositor_next_event(c, &event, mask)) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400569 switch (prev ? prev->response_type & ~0x80 : 0x80) {
570 case XCB_KEY_RELEASE:
571 key_release = (xcb_key_press_event_t *) prev;
572 key_press = (xcb_key_press_event_t *) event;
573 if ((event->response_type & ~0x80) == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -0700574 key_release->time == key_press->time &&
575 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400576 /* Don't deliver the held key release
577 * event or the new key press event. */
578 free(event);
579 free(prev);
580 prev = NULL;
581 continue;
582 } else {
583 /* Deliver the held key release now
584 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400585 * event below. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400586 notify_key(c->base.input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500587 weston_compositor_get_time(),
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400588 key_release->detail - 8, 0);
589 free(prev);
590 prev = NULL;
591 break;
592 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400593
594 case XCB_FOCUS_IN:
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400595 /* assert event is keymap_notify */
596 focus_in = (xcb_focus_in_event_t *) prev;
597 keymap_notify = (xcb_keymap_notify_event_t *) event;
598 c->keys.size = 0;
599 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
600 set = keymap_notify->keys[i >> 3] &
601 (1 << (i & 7));
602 if (set) {
603 k = wl_array_add(&c->keys, sizeof *k);
604 *k = i;
605 }
606 }
607
608 output = x11_compositor_find_output(c, focus_in->event);
609 notify_keyboard_focus(c->base.input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500610 weston_compositor_get_time(),
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400611 &output->base, &c->keys);
612
613 free(prev);
614 prev = NULL;
615 break;
616
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400617 default:
618 /* No previous event held */
619 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400620 }
621
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400622 switch (event->response_type & ~0x80) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400623 case XCB_KEY_PRESS:
624 key_press = (xcb_key_press_event_t *) event;
625 notify_key(c->base.input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500626 weston_compositor_get_time(),
Kristian Høgsberg293af262011-10-11 17:23:02 -0400627 key_press->detail - 8, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400628 break;
629 case XCB_KEY_RELEASE:
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400630 prev = event;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400631 break;
632 case XCB_BUTTON_PRESS:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200633 x11_compositor_deliver_button_event(c, event, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400634 break;
635 case XCB_BUTTON_RELEASE:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200636 x11_compositor_deliver_button_event(c, event, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400637 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400638 case XCB_MOTION_NOTIFY:
639 motion_notify = (xcb_motion_notify_event_t *) event;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700640 output = x11_compositor_find_output(c, motion_notify->event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400641 notify_motion(c->base.input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500642 weston_compositor_get_time(),
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700643 output->base.x + motion_notify->event_x,
644 output->base.y + motion_notify->event_y);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400645 break;
646
647 case XCB_EXPOSE:
Benjamin Franzke84290d02011-03-02 10:07:59 +0100648 /* FIXME: schedule output repaint */
649 /* output = x11_compositor_find_output(c, expose->window); */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400650
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500651 weston_compositor_schedule_repaint(&c->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400652 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400653
654 case XCB_ENTER_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500655 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500656 if (enter_notify->state >= Button1Mask)
657 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500658 output = x11_compositor_find_output(c, enter_notify->event);
659 notify_pointer_focus(c->base.input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500660 weston_compositor_get_time(),
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500661 &output->base,
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700662 output->base.x + enter_notify->event_x,
663 output->base.y + enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400664 break;
665
666 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500667 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500668 if (enter_notify->state >= Button1Mask)
669 break;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700670 output = x11_compositor_find_output(c, enter_notify->event);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500671 notify_pointer_focus(c->base.input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500672 weston_compositor_get_time(),
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500673 NULL,
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700674 output->base.x + enter_notify->event_x,
675 output->base.y + enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400676 break;
677
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400678 case XCB_CLIENT_MESSAGE:
679 client_message = (xcb_client_message_event_t *) event;
680 atom = client_message->data.data32[0];
681 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500682 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400683 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400684
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500685 case XCB_FOCUS_IN:
686 focus_in = (xcb_focus_in_event_t *) event;
687 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
688 break;
689
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400690 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500691 break;
692
693 case XCB_FOCUS_OUT:
694 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -0500695 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
696 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500697 break;
698 notify_keyboard_focus(c->base.input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500699 weston_compositor_get_time(),
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400700 NULL, NULL);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500701 break;
702
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500703 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400704 break;
705 }
706
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400707 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400708 free (event);
709 }
710
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400711 switch (prev ? prev->response_type & ~0x80 : 0x80) {
712 case XCB_KEY_RELEASE:
713 key_release = (xcb_key_press_event_t *) prev;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400714 notify_key(c->base.input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500715 weston_compositor_get_time(),
Kristian Høgsberg293af262011-10-11 17:23:02 -0400716 key_release->detail - 8, 0);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400717 free(prev);
718 prev = NULL;
719 break;
720 default:
721 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500722 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400723
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400724 return event != NULL;
Kristian Høgsbergee724822011-04-21 14:51:44 -0400725}
726
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400727#define F(field) offsetof(struct x11_compositor, field)
728
729static void
730x11_compositor_get_resources(struct x11_compositor *c)
731{
732 static const struct { const char *name; int offset; } atoms[] = {
733 { "WM_PROTOCOLS", F(atom.wm_protocols) },
734 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
735 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
736 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500737 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400738 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500739 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400740 { "_NET_WM_STATE", F(atom.net_wm_state) },
741 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500742 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400743 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500744 { "CARDINAL", F(atom.cardinal) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400745 };
746
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500747 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400748 xcb_intern_atom_reply_t *reply;
749 xcb_pixmap_t pixmap;
750 xcb_gc_t gc;
751 int i;
752 uint8_t data[] = { 0, 0, 0, 0 };
753
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500754 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400755 cookies[i] = xcb_intern_atom (c->conn, 0,
756 strlen(atoms[i].name),
757 atoms[i].name);
758
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500759 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400760 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
761 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
762 free(reply);
763 }
764
765 pixmap = xcb_generate_id(c->conn);
766 gc = xcb_generate_id(c->conn);
767 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
768 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
769 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
770 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
771 c->null_cursor = xcb_generate_id(c->conn);
772 xcb_create_cursor (c->conn, c->null_cursor,
773 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
774 xcb_free_gc(c->conn, gc);
775 xcb_free_pixmap(c->conn, pixmap);
776}
777
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500778static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500779x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500780{
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200781 struct x11_compositor *compositor = (struct x11_compositor *)ec;
Matt Roper361d2ad2011-08-29 13:52:23 -0700782
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200783 wl_event_source_remove(compositor->xcb_source);
784 x11_input_destroy(compositor);
785
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500786 weston_compositor_shutdown(ec); /* destroys outputs, too */
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200787
788 x11_compositor_fini_egl(compositor);
789
790 XCloseDisplay(compositor->dpy);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500791 free(ec);
792}
793
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500794static struct weston_compositor *
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400795x11_compositor_create(struct wl_display *display,
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700796 int width, int height, int count, int fullscreen)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400797{
798 struct x11_compositor *c;
799 struct wl_event_loop *loop;
800 xcb_screen_iterator_t s;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700801 int i, x;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400802
803 c = malloc(sizeof *c);
804 if (c == NULL)
805 return NULL;
806
807 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100808
809 c->dpy = XOpenDisplay(NULL);
Cyril Brulebois20798292011-04-06 18:05:40 +0200810 if (c->dpy == NULL)
811 return NULL;
812
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100813 c->conn = XGetXCBConnection(c->dpy);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +0100814 XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
815
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400816 if (xcb_connection_has_error(c->conn))
817 return NULL;
818
819 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
820 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500821 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400822
823 x11_compositor_get_resources(c);
824
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400825 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +0200826 if (x11_compositor_init_egl(c) < 0)
Darxus55973f22010-11-22 21:24:39 -0500827 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400828
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500829 c->base.destroy = x11_destroy;
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500830
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400831 /* Can't init base class until we have a current egl context */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500832 if (weston_compositor_init(&c->base, display) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -0400833 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400834
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700835 for (i = 0, x = 0; i < count; i++) {
836 if (x11_compositor_create_output(c, x, 0, width, height,
837 fullscreen) < 0)
838 return NULL;
839 x += width;
840 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400841
Darxus55973f22010-11-22 21:24:39 -0500842 if (x11_input_create(c) < 0)
843 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400844
845 loop = wl_display_get_event_loop(c->base.wl_display);
846
847 c->xcb_source =
848 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
849 WL_EVENT_READABLE,
850 x11_compositor_handle_event, c);
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400851 wl_event_source_check(c->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400852
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400853 return &c->base;
854}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400855
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500856struct weston_compositor *
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400857backend_init(struct wl_display *display, char *options);
858
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500859WL_EXPORT struct weston_compositor *
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400860backend_init(struct wl_display *display, char *options)
861{
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700862 int width = 1024, height = 640, fullscreen = 0, count = 1, i;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400863 char *p, *value;
864
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400865 static char * const tokens[] = {
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700866 "width", "height", "fullscreen", "output-count", NULL
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400867 };
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400868
869 p = options;
870 while (i = getsubopt(&p, tokens, &value), i != -1) {
871 switch (i) {
872 case 0:
873 width = strtol(value, NULL, 0);
874 break;
875 case 1:
876 height = strtol(value, NULL, 0);
877 break;
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400878 case 2:
879 fullscreen = 1;
880 break;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700881 case 3:
882 count = strtol(value, NULL, 0);
883 break;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400884 }
885 }
886
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700887 return x11_compositor_create(display,
888 width, height, count, fullscreen);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400889}