blob: 0825f1a128cc5227cf5c8e5f1ab42ff2c39653a7 [file] [log] [blame]
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001/*
2 * Copyright © 2008-2010 Kristian Høgsberg
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
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +080019#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040023#include <stddef.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <errno.h>
30#include <sys/time.h>
Kristian Høgsbergf9112b22010-06-14 12:53:43 -040031#include <linux/input.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040032
33#include <xcb/xcb.h>
Benjamin Franzke3b288af2011-02-20 19:58:42 +010034#include <X11/Xlib.h>
35#include <X11/Xlib-xcb.h>
36
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040037#include <GLES2/gl2.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040038#include <EGL/egl.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040039
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040040#include "compositor.h"
41
42#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
43
44struct x11_compositor {
45 struct wlsc_compositor base;
46
Benjamin Franzke3b288af2011-02-20 19:58:42 +010047 Display *dpy;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040048 xcb_connection_t *conn;
49 xcb_screen_t *screen;
50 xcb_cursor_t null_cursor;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -050051 struct wl_array keys;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040052 struct wl_event_source *xcb_source;
53 struct {
54 xcb_atom_t wm_protocols;
55 xcb_atom_t wm_normal_hints;
56 xcb_atom_t wm_size_hints;
57 xcb_atom_t wm_delete_window;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050058 xcb_atom_t wm_class;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040059 xcb_atom_t net_wm_name;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050060 xcb_atom_t net_wm_icon;
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -040061 xcb_atom_t net_wm_state;
62 xcb_atom_t net_wm_state_fullscreen;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050063 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040064 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050065 xcb_atom_t cardinal;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040066 } atom;
67};
68
69struct x11_output {
70 struct wlsc_output base;
71
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040072 xcb_window_t window;
Benjamin Franzke84290d02011-03-02 10:07:59 +010073 EGLSurface egl_surface;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -040074 struct wlsc_mode mode;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040075};
76
77struct x11_input {
78 struct wlsc_input_device base;
79};
80
81
Darxus55973f22010-11-22 21:24:39 -050082static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040083x11_input_create(struct x11_compositor *c)
84{
85 struct x11_input *input;
86
87 input = malloc(sizeof *input);
88 if (input == NULL)
Darxus55973f22010-11-22 21:24:39 -050089 return -1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040090
91 memset(input, 0, sizeof *input);
92 wlsc_input_device_init(&input->base, &c->base);
93
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -050094 c->base.input_device = &input->base.input_device;
Darxus55973f22010-11-22 21:24:39 -050095
96 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040097}
98
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040099static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400100x11_compositor_init_egl(struct x11_compositor *c)
101{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400102 EGLint major, minor;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100103 EGLint n;
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400104 const char *extensions;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100105 EGLint config_attribs[] = {
106 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
107 EGL_RED_SIZE, 1,
108 EGL_GREEN_SIZE, 1,
109 EGL_BLUE_SIZE, 1,
110 EGL_DEPTH_SIZE, 1,
111 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
112 EGL_NONE
113 };
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400114 static const EGLint context_attribs[] = {
115 EGL_CONTEXT_CLIENT_VERSION, 2,
116 EGL_NONE
117 };
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400118
Egbert Eiche7b8d902011-05-10 20:00:19 +0000119 setenv("EGL_PLATFORM", "x11", 1);
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100120 c->base.display = eglGetDisplay(c->dpy);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400121 if (c->base.display == NULL) {
122 fprintf(stderr, "failed to create display\n");
123 return -1;
124 }
125
126 if (!eglInitialize(c->base.display, &major, &minor)) {
127 fprintf(stderr, "failed to initialize display\n");
128 return -1;
129 }
130
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400131 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
132 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
133 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400134 return -1;
135 }
136
Darxus55973f22010-11-22 21:24:39 -0500137 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
138 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
139 return -1;
140 }
Benjamin Franzke84290d02011-03-02 10:07:59 +0100141 if (!eglChooseConfig(c->base.display, config_attribs,
142 &c->base.config, 1, &n) || n == 0) {
143 fprintf(stderr, "failed to choose config: %d\n", n);
144 return -1;
145 }
Darxus55973f22010-11-22 21:24:39 -0500146
Benjamin Franzke84290d02011-03-02 10:07:59 +0100147 c->base.context = eglCreateContext(c->base.display, c->base.config,
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400148 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400149 if (c->base.context == NULL) {
150 fprintf(stderr, "failed to create context\n");
151 return -1;
152 }
153
154 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
155 EGL_NO_SURFACE, c->base.context)) {
156 fprintf(stderr, "failed to make context current\n");
157 return -1;
158 }
159
160 return 0;
161}
162
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100163static int
164x11_output_prepare_render(struct wlsc_output *output_base)
165{
166 struct x11_output *output = (struct x11_output *) output_base;
167 struct wlsc_compositor *ec = output->base.compositor;
168
169 if (!eglMakeCurrent(ec->display, output->egl_surface,
170 output->egl_surface, ec->context)) {
171 fprintf(stderr, "failed to make current\n");
172 return -1;
173 }
174
175 return 0;
176}
177
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100178static int
179x11_output_present(struct wlsc_output *output_base)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400180{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100181 struct x11_output *output = (struct x11_output *) output_base;
182 struct wlsc_compositor *ec = output->base.compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400183 struct timeval tv;
184 uint32_t msec;
185
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100186 if (x11_output_prepare_render(&output->base))
187 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100188
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100189 eglSwapBuffers(ec->display, output->egl_surface);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400190
191 gettimeofday(&tv, NULL);
192 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100193 wlsc_output_finish_frame(&output->base, msec);
194
195 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400196}
197
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200198static int
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200199x11_output_prepare_scanout_surface(struct wlsc_output *output_base,
200 struct wlsc_surface *es)
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200201{
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200202 return -1;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200203}
204
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200205static int
206x11_output_set_cursor(struct wlsc_output *output_base,
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400207 struct wlsc_input_device *input)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200208{
209 return -1;
210}
211
212
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400213static void
214x11_output_set_wm_protocols(struct x11_output *output)
215{
216 xcb_atom_t list[1];
217 struct x11_compositor *c =
218 (struct x11_compositor *) output->base.compositor;
219
220 list[0] = c->atom.wm_delete_window;
221 xcb_change_property (c->conn,
222 XCB_PROP_MODE_REPLACE,
223 output->window,
224 c->atom.wm_protocols,
225 XCB_ATOM_ATOM,
226 32,
227 ARRAY_SIZE(list),
228 list);
229}
230
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400231static void
232x11_output_change_state(struct x11_output *output, int add, xcb_atom_t state)
233{
234 xcb_client_message_event_t event;
235 struct x11_compositor *c =
236 (struct x11_compositor *) output->base.compositor;
237 xcb_screen_iterator_t iter;
238
239#define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
240#define _NET_WM_STATE_ADD 1 /* add/set property */
241#define _NET_WM_STATE_TOGGLE 2 /* toggle property */
242
243 memset(&event, 0, sizeof event);
244 event.response_type = XCB_CLIENT_MESSAGE;
245 event.format = 32;
246 event.window = output->window;
247 event.type = c->atom.net_wm_state;
248
249 event.data.data32[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
250 event.data.data32[1] = state;
251 event.data.data32[2] = 0;
252 event.data.data32[3] = 0;
253 event.data.data32[4] = 0;
254
255 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
256 xcb_send_event(c->conn, 0, iter.data->root,
257 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
258 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
259 (void *) &event);
260}
261
262
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400263struct wm_normal_hints {
264 uint32_t flags;
265 uint32_t pad[4];
266 int32_t min_width, min_height;
267 int32_t max_width, max_height;
268 int32_t width_inc, height_inc;
269 int32_t min_aspect_x, min_aspect_y;
270 int32_t max_aspect_x, max_aspect_y;
271 int32_t base_width, base_height;
272 int32_t win_gravity;
273};
274
275#define WM_NORMAL_HINTS_MIN_SIZE 16
276#define WM_NORMAL_HINTS_MAX_SIZE 32
277
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500278static void
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400279x11_output_set_icon(struct x11_compositor *c,
280 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500281{
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400282 uint32_t *icon, *pixels, stride;
283 int32_t width, height;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500284
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400285 pixels = wlsc_load_image(filename, &width, &height, &stride);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500286 if (!pixels)
287 return;
288 icon = malloc(width * height * 4 + 8);
289 if (!icon) {
290 free(pixels);
291 return;
292 }
293
294 icon[0] = width;
295 icon[1] = height;
296 memcpy(icon + 2, pixels, width * height * 4);
297 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
298 c->atom.net_wm_icon, c->atom.cardinal, 32,
299 width * height + 2, icon);
300 free(icon);
301 free(pixels);
302}
303
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400304static int
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700305x11_compositor_create_output(struct x11_compositor *c, int x, int y,
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400306 int width, int height, int fullscreen)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400307{
308 static const char name[] = "Wayland Compositor";
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500309 static const char class[] = "wayland-1\0Wayland Compositor";
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400310 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400311 xcb_screen_iterator_t iter;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400312 struct wm_normal_hints normal_hints;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400313 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
314 uint32_t values[2] = {
315 XCB_EVENT_MASK_KEY_PRESS |
316 XCB_EVENT_MASK_KEY_RELEASE |
317 XCB_EVENT_MASK_BUTTON_PRESS |
318 XCB_EVENT_MASK_BUTTON_RELEASE |
319 XCB_EVENT_MASK_POINTER_MOTION |
320 XCB_EVENT_MASK_EXPOSURE |
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400321 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
322 XCB_EVENT_MASK_ENTER_WINDOW |
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500323 XCB_EVENT_MASK_LEAVE_WINDOW |
324 XCB_EVENT_MASK_KEYMAP_STATE |
325 XCB_EVENT_MASK_FOCUS_CHANGE,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400326 0
327 };
328
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400329 output = malloc(sizeof *output);
330 if (output == NULL)
331 return -1;
332
333 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400334
335 output->mode.flags =
336 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
337 output->mode.width = width;
338 output->mode.height = height;
339 output->mode.refresh = 60;
340 wl_list_init(&output->base.mode_list);
341 wl_list_insert(&output->base.mode_list, &output->mode.link);
342
343 output->base.current = &output->mode;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700344 wlsc_output_init(&output->base, &c->base, x, y, width, height,
Benjamin Franzke84290d02011-03-02 10:07:59 +0100345 WL_OUTPUT_FLIPPED);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400346
347 values[1] = c->null_cursor;
348 output->window = xcb_generate_id(c->conn);
349 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
350 xcb_create_window(c->conn,
351 XCB_COPY_FROM_PARENT,
352 output->window,
353 iter.data->root,
354 0, 0,
355 width, height,
356 0,
357 XCB_WINDOW_CLASS_INPUT_OUTPUT,
358 iter.data->root_visual,
359 mask, values);
360
361 /* Don't resize me. */
362 memset(&normal_hints, 0, sizeof normal_hints);
363 normal_hints.flags =
364 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
365 normal_hints.min_width = width;
366 normal_hints.min_height = height;
367 normal_hints.max_width = width;
368 normal_hints.max_height = height;
369 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
370 c->atom.wm_normal_hints,
371 c->atom.wm_size_hints, 32,
372 sizeof normal_hints / 4,
373 (uint8_t *) &normal_hints);
374
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400375 /* Set window name. Don't bother with non-EWMH WMs. */
376 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
377 c->atom.net_wm_name, c->atom.utf8_string, 8,
378 strlen(name), name);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500379 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
380 c->atom.wm_class, c->atom.string, 8,
381 sizeof class, class);
382
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400383 x11_output_set_icon(c, output, DATADIR "/wayland/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500384
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500385 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400386
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400387 x11_output_set_wm_protocols(output);
388
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400389 if (fullscreen)
390 x11_output_change_state(output, 1,
391 c->atom.net_wm_state_fullscreen);
392
Benjamin Franzke84290d02011-03-02 10:07:59 +0100393 output->egl_surface =
394 eglCreateWindowSurface(c->base.display, c->base.config,
395 output->window, NULL);
396 if (!output->egl_surface) {
397 fprintf(stderr, "failed to create window surface\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400398 return -1;
399 }
Benjamin Franzke84290d02011-03-02 10:07:59 +0100400 if (!eglMakeCurrent(c->base.display, output->egl_surface,
401 output->egl_surface, c->base.context)) {
402 fprintf(stderr, "failed to make surface current\n");
403 return -1;
404 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400405
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100406 output->base.prepare_render = x11_output_prepare_render;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100407 output->base.present = x11_output_present;
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200408 output->base.prepare_scanout_surface =
409 x11_output_prepare_scanout_surface;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200410 output->base.set_hardware_cursor = x11_output_set_cursor;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100411
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400412 wl_list_insert(c->base.output_list.prev, &output->base.link);
413
414 return 0;
415}
416
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400417static struct x11_output *
418x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
419{
420 struct x11_output *output;
421
422 wl_list_for_each(output, &c->base.output_list, base.link) {
423 if (output->window == window)
424 return output;
425 }
426
427 return NULL;
428}
429
Kristian Høgsbergee724822011-04-21 14:51:44 -0400430static int
431x11_compositor_next_event(struct x11_compositor *c,
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400432 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -0400433{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400434 if (mask & WL_EVENT_READABLE) {
435 *event = xcb_poll_for_event(c->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -0400436 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400437#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400438 *event = xcb_poll_for_queued_event(c->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400439#else
440 *event = xcb_poll_for_event(c->conn);
441#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -0400442 }
443
444 return *event != NULL;
445}
446
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400447static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400448x11_compositor_handle_event(int fd, uint32_t mask, void *data)
449{
450 struct x11_compositor *c = data;
451 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400452 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400453 xcb_client_message_event_t *client_message;
454 xcb_motion_notify_event_t *motion_notify;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500455 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400456 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400457 xcb_button_press_event_t *button_press;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500458 xcb_keymap_notify_event_t *keymap_notify;
459 xcb_focus_in_event_t *focus_in;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400460 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500461 uint32_t *k;
462 int i, set;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400463
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400464 prev = NULL;
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400465 while (x11_compositor_next_event(c, &event, mask)) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400466 switch (prev ? prev->response_type & ~0x80 : 0x80) {
467 case XCB_KEY_RELEASE:
468 key_release = (xcb_key_press_event_t *) prev;
469 key_press = (xcb_key_press_event_t *) event;
470 if ((event->response_type & ~0x80) == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -0700471 key_release->time == key_press->time &&
472 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400473 /* Don't deliver the held key release
474 * event or the new key press event. */
475 free(event);
476 free(prev);
477 prev = NULL;
478 continue;
479 } else {
480 /* Deliver the held key release now
481 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400482 * event below. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400483 notify_key(c->base.input_device,
484 key_release->time,
485 key_release->detail - 8, 0);
486 free(prev);
487 prev = NULL;
488 break;
489 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400490
491 case XCB_FOCUS_IN:
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400492 /* assert event is keymap_notify */
493 focus_in = (xcb_focus_in_event_t *) prev;
494 keymap_notify = (xcb_keymap_notify_event_t *) event;
495 c->keys.size = 0;
496 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
497 set = keymap_notify->keys[i >> 3] &
498 (1 << (i & 7));
499 if (set) {
500 k = wl_array_add(&c->keys, sizeof *k);
501 *k = i;
502 }
503 }
504
505 output = x11_compositor_find_output(c, focus_in->event);
506 notify_keyboard_focus(c->base.input_device,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400507 wlsc_compositor_get_time(),
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400508 &output->base, &c->keys);
509
510 free(prev);
511 prev = NULL;
512 break;
513
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400514 default:
515 /* No previous event held */
516 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400517 }
518
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400519 switch (event->response_type & ~0x80) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400520 case XCB_KEY_PRESS:
521 key_press = (xcb_key_press_event_t *) event;
522 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400523 key_press->time, key_press->detail - 8, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400524 break;
525 case XCB_KEY_RELEASE:
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400526 prev = event;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400527 break;
528 case XCB_BUTTON_PRESS:
529 button_press = (xcb_button_press_event_t *) event;
530 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400531 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400532 button_press->detail + BTN_LEFT - 1, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400533 break;
534 case XCB_BUTTON_RELEASE:
535 button_press = (xcb_button_press_event_t *) event;
536 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400537 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400538 button_press->detail + BTN_LEFT - 1, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400539 break;
540
541 case XCB_MOTION_NOTIFY:
542 motion_notify = (xcb_motion_notify_event_t *) event;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700543 output = x11_compositor_find_output(c, motion_notify->event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400544 notify_motion(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400545 motion_notify->time,
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700546 output->base.x + motion_notify->event_x,
547 output->base.y + motion_notify->event_y);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400548 break;
549
550 case XCB_EXPOSE:
Benjamin Franzke84290d02011-03-02 10:07:59 +0100551 /* FIXME: schedule output repaint */
552 /* output = x11_compositor_find_output(c, expose->window); */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400553
Benjamin Franzke84290d02011-03-02 10:07:59 +0100554 wlsc_compositor_schedule_repaint(&c->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400555 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400556
557 case XCB_ENTER_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500558 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500559 if (enter_notify->state >= Button1Mask)
560 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500561 output = x11_compositor_find_output(c, enter_notify->event);
562 notify_pointer_focus(c->base.input_device,
563 enter_notify->time,
564 &output->base,
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700565 output->base.x + enter_notify->event_x,
566 output->base.y + enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400567 break;
568
569 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500570 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500571 if (enter_notify->state >= Button1Mask)
572 break;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700573 output = x11_compositor_find_output(c, enter_notify->event);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500574 notify_pointer_focus(c->base.input_device,
575 enter_notify->time,
576 NULL,
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700577 output->base.x + enter_notify->event_x,
578 output->base.y + enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400579 break;
580
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400581 case XCB_CLIENT_MESSAGE:
582 client_message = (xcb_client_message_event_t *) event;
583 atom = client_message->data.data32[0];
584 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500585 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400586 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400587
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500588 case XCB_FOCUS_IN:
589 focus_in = (xcb_focus_in_event_t *) event;
590 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
591 break;
592
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400593 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500594 break;
595
596 case XCB_FOCUS_OUT:
597 focus_in = (xcb_focus_in_event_t *) event;
598 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
599 break;
600 notify_keyboard_focus(c->base.input_device,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400601 wlsc_compositor_get_time(),
602 NULL, NULL);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500603 break;
604
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500605 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400606 break;
607 }
608
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400609 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400610 free (event);
611 }
612
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400613 switch (prev ? prev->response_type & ~0x80 : 0x80) {
614 case XCB_KEY_RELEASE:
615 key_release = (xcb_key_press_event_t *) prev;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400616 notify_key(c->base.input_device,
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400617 key_release->time, key_release->detail - 8, 0);
618 free(prev);
619 prev = NULL;
620 break;
621 default:
622 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500623 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400624
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400625 return event != NULL;
Kristian Høgsbergee724822011-04-21 14:51:44 -0400626}
627
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400628#define F(field) offsetof(struct x11_compositor, field)
629
630static void
631x11_compositor_get_resources(struct x11_compositor *c)
632{
633 static const struct { const char *name; int offset; } atoms[] = {
634 { "WM_PROTOCOLS", F(atom.wm_protocols) },
635 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
636 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
637 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500638 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400639 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500640 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400641 { "_NET_WM_STATE", F(atom.net_wm_state) },
642 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500643 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400644 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500645 { "CARDINAL", F(atom.cardinal) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400646 };
647
648 xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
649 xcb_intern_atom_reply_t *reply;
650 xcb_pixmap_t pixmap;
651 xcb_gc_t gc;
652 int i;
653 uint8_t data[] = { 0, 0, 0, 0 };
654
655 for (i = 0; i < ARRAY_SIZE(atoms); i++)
656 cookies[i] = xcb_intern_atom (c->conn, 0,
657 strlen(atoms[i].name),
658 atoms[i].name);
659
660 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
661 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
662 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
663 free(reply);
664 }
665
666 pixmap = xcb_generate_id(c->conn);
667 gc = xcb_generate_id(c->conn);
668 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
669 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
670 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
671 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
672 c->null_cursor = xcb_generate_id(c->conn);
673 xcb_create_cursor (c->conn, c->null_cursor,
674 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
675 xcb_free_gc(c->conn, gc);
676 xcb_free_pixmap(c->conn, pixmap);
677}
678
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500679static void
680x11_destroy(struct wlsc_compositor *ec)
681{
682 free(ec);
683}
684
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400685static struct wlsc_compositor *
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400686x11_compositor_create(struct wl_display *display,
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700687 int width, int height, int count, int fullscreen)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400688{
689 struct x11_compositor *c;
690 struct wl_event_loop *loop;
691 xcb_screen_iterator_t s;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700692 int i, x;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400693
694 c = malloc(sizeof *c);
695 if (c == NULL)
696 return NULL;
697
698 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100699
700 c->dpy = XOpenDisplay(NULL);
Cyril Brulebois20798292011-04-06 18:05:40 +0200701 if (c->dpy == NULL)
702 return NULL;
703
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100704 c->conn = XGetXCBConnection(c->dpy);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +0100705 XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
706
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400707 if (xcb_connection_has_error(c->conn))
708 return NULL;
709
710 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
711 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500712 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400713
714 x11_compositor_get_resources(c);
715
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400716 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +0200717 if (x11_compositor_init_egl(c) < 0)
Darxus55973f22010-11-22 21:24:39 -0500718 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400719
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500720 c->base.destroy = x11_destroy;
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500721
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400722 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400723 if (wlsc_compositor_init(&c->base, display) < 0)
724 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400725
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700726 for (i = 0, x = 0; i < count; i++) {
727 if (x11_compositor_create_output(c, x, 0, width, height,
728 fullscreen) < 0)
729 return NULL;
730 x += width;
731 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400732
Darxus55973f22010-11-22 21:24:39 -0500733 if (x11_input_create(c) < 0)
734 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400735
736 loop = wl_display_get_event_loop(c->base.wl_display);
737
738 c->xcb_source =
739 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
740 WL_EVENT_READABLE,
741 x11_compositor_handle_event, c);
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400742 wl_event_source_check(c->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400743
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400744 return &c->base;
745}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400746
747struct wlsc_compositor *
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400748backend_init(struct wl_display *display, char *options);
749
750WL_EXPORT struct wlsc_compositor *
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400751backend_init(struct wl_display *display, char *options)
752{
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700753 int width = 1024, height = 640, fullscreen = 0, count = 1, i;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400754 char *p, *value;
755
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400756 static char * const tokens[] = {
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700757 "width", "height", "fullscreen", "output-count", NULL
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400758 };
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400759
760 p = options;
761 while (i = getsubopt(&p, tokens, &value), i != -1) {
762 switch (i) {
763 case 0:
764 width = strtol(value, NULL, 0);
765 break;
766 case 1:
767 height = strtol(value, NULL, 0);
768 break;
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400769 case 2:
770 fullscreen = 1;
771 break;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700772 case 3:
773 count = strtol(value, NULL, 0);
774 break;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400775 }
776 }
777
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700778 return x11_compositor_create(display,
779 width, height, count, fullscreen);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400780}