blob: ae68a1cb1dd5903ed172c8167824bffc8e2b4bad [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>
34#include <xcb/dri2.h>
35#include <xcb/xfixes.h>
36
Benjamin Franzke3b288af2011-02-20 19:58:42 +010037#include <X11/Xlib.h>
38#include <X11/Xlib-xcb.h>
39
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040040#define GL_GLEXT_PROTOTYPES
41#define EGL_EGLEXT_PROTOTYPES
42#include <GLES2/gl2.h>
43#include <GLES2/gl2ext.h>
44#include <EGL/egl.h>
45#include <EGL/eglext.h>
46
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040047#include "compositor.h"
48
49#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
50
51struct x11_compositor {
52 struct wlsc_compositor base;
53
Benjamin Franzke3b288af2011-02-20 19:58:42 +010054 Display *dpy;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040055 xcb_connection_t *conn;
56 xcb_screen_t *screen;
57 xcb_cursor_t null_cursor;
58 int dri2_major;
59 int dri2_minor;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -050060 struct wl_array keys;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040061 struct wl_event_source *xcb_source;
62 struct {
63 xcb_atom_t wm_protocols;
64 xcb_atom_t wm_normal_hints;
65 xcb_atom_t wm_size_hints;
66 xcb_atom_t wm_delete_window;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050067 xcb_atom_t wm_class;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040068 xcb_atom_t net_wm_name;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050069 xcb_atom_t net_wm_icon;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050070 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040071 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050072 xcb_atom_t cardinal;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040073 } atom;
74};
75
76struct x11_output {
77 struct wlsc_output base;
78
79 xcb_xfixes_region_t region;
80 xcb_window_t window;
81 GLuint rbo;
82 EGLImageKHR image;
83 xcb_rectangle_t damage[16];
84 int damage_count;
85};
86
87struct x11_input {
88 struct wlsc_input_device base;
89};
90
91
Darxus55973f22010-11-22 21:24:39 -050092static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040093x11_input_create(struct x11_compositor *c)
94{
95 struct x11_input *input;
96
97 input = malloc(sizeof *input);
98 if (input == NULL)
Darxus55973f22010-11-22 21:24:39 -050099 return -1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400100
101 memset(input, 0, sizeof *input);
102 wlsc_input_device_init(&input->base, &c->base);
103
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -0500104 c->base.input_device = &input->base.input_device;
Darxus55973f22010-11-22 21:24:39 -0500105
106 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400107}
108
109
110static int
111dri2_connect(struct x11_compositor *c)
112{
113 xcb_xfixes_query_version_reply_t *xfixes_query;
114 xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
115 xcb_dri2_query_version_reply_t *dri2_query;
116 xcb_dri2_query_version_cookie_t dri2_query_cookie;
117 xcb_dri2_connect_reply_t *connect;
118 xcb_dri2_connect_cookie_t connect_cookie;
119 xcb_generic_error_t *error;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400120 char path[256];
121 int fd;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400122
123 xcb_prefetch_extension_data (c->conn, &xcb_xfixes_id);
124 xcb_prefetch_extension_data (c->conn, &xcb_dri2_id);
125
126 xfixes_query_cookie =
127 xcb_xfixes_query_version(c->conn,
128 XCB_XFIXES_MAJOR_VERSION,
129 XCB_XFIXES_MINOR_VERSION);
130
131 dri2_query_cookie =
132 xcb_dri2_query_version (c->conn,
133 XCB_DRI2_MAJOR_VERSION,
134 XCB_DRI2_MINOR_VERSION);
135
136 connect_cookie = xcb_dri2_connect_unchecked (c->conn,
137 c->screen->root,
138 XCB_DRI2_DRIVER_TYPE_DRI);
139
140 xfixes_query =
141 xcb_xfixes_query_version_reply (c->conn,
142 xfixes_query_cookie, &error);
143 if (xfixes_query == NULL ||
144 error != NULL || xfixes_query->major_version < 2) {
145 free(error);
146 return -1;
147 }
148 free(xfixes_query);
149
150 dri2_query =
151 xcb_dri2_query_version_reply (c->conn,
152 dri2_query_cookie, &error);
153 if (dri2_query == NULL || error != NULL) {
Yuval Fledel91b59992010-11-22 21:42:58 +0200154 fprintf(stderr, "DRI2: failed to query version\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400155 free(error);
156 return EGL_FALSE;
157 }
158 c->dri2_major = dri2_query->major_version;
159 c->dri2_minor = dri2_query->minor_version;
160 free(dri2_query);
161
162 connect = xcb_dri2_connect_reply (c->conn,
163 connect_cookie, NULL);
164 if (connect == NULL ||
165 connect->driver_name_length + connect->device_name_length == 0) {
Yuval Fledel91b59992010-11-22 21:42:58 +0200166 fprintf(stderr, "DRI2: failed to connect, DRI2 version: %u.%u\n",
167 c->dri2_major, c->dri2_minor);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400168 return -1;
169 }
170
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +0800171#ifdef XCB_DRI2_CONNECT_DEVICE_NAME_BROKEN
172 {
173 char *driver_name, *device_name;
174
175 driver_name = xcb_dri2_connect_driver_name (connect);
176 device_name = driver_name +
177 ((connect->driver_name_length + 3) & ~3);
178 snprintf(path, sizeof path, "%.*s",
179 xcb_dri2_connect_device_name_length (connect),
180 device_name);
181 }
182#else
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400183 snprintf(path, sizeof path, "%.*s",
184 xcb_dri2_connect_device_name_length (connect),
185 xcb_dri2_connect_device_name (connect));
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +0800186#endif
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400187 free(connect);
188
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400189 fd = open(path, O_RDWR);
190 if (fd < 0) {
191 fprintf(stderr,
Yuval Fledel91b59992010-11-22 21:42:58 +0200192 "DRI2: could not open %s (%s)\n", path, strerror(errno));
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400193 return -1;
194 }
195
196 return wlsc_drm_init(&c->base, fd, path);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400197}
198
199static int
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400200dri2_authenticate(struct x11_compositor *c, uint32_t magic)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400201{
202 xcb_dri2_authenticate_reply_t *authenticate;
203 xcb_dri2_authenticate_cookie_t authenticate_cookie;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400204
205 authenticate_cookie =
206 xcb_dri2_authenticate_unchecked(c->conn,
207 c->screen->root, magic);
208 authenticate =
209 xcb_dri2_authenticate_reply(c->conn,
210 authenticate_cookie, NULL);
211 if (authenticate == NULL || !authenticate->authenticated) {
Yuval Fledel91b59992010-11-22 21:42:58 +0200212 fprintf(stderr, "DRI2: failed to authenticate\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400213 free(authenticate);
214 return -1;
215 }
216
217 free(authenticate);
218
219 return 0;
220}
221
222static int
223x11_compositor_init_egl(struct x11_compositor *c)
224{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400225 EGLint major, minor;
226 const char *extensions;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400227 static const EGLint context_attribs[] = {
228 EGL_CONTEXT_CLIENT_VERSION, 2,
229 EGL_NONE
230 };
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400231
232 if (dri2_connect(c) < 0)
233 return -1;
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -0400234
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100235 c->base.display = eglGetDisplay(c->dpy);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400236 if (c->base.display == NULL) {
237 fprintf(stderr, "failed to create display\n");
238 return -1;
239 }
240
241 if (!eglInitialize(c->base.display, &major, &minor)) {
242 fprintf(stderr, "failed to initialize display\n");
243 return -1;
244 }
245
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400246 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
247 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
248 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400249 return -1;
250 }
251
Darxus55973f22010-11-22 21:24:39 -0500252 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
253 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
254 return -1;
255 }
256
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400257 c->base.context = eglCreateContext(c->base.display, NULL,
258 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400259 if (c->base.context == NULL) {
260 fprintf(stderr, "failed to create context\n");
261 return -1;
262 }
263
264 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
265 EGL_NO_SURFACE, c->base.context)) {
266 fprintf(stderr, "failed to make context current\n");
267 return -1;
268 }
269
270 return 0;
271}
272
273static void
274x11_compositor_present(struct wlsc_compositor *base)
275{
276 struct x11_compositor *c = (struct x11_compositor *) base;
277 struct x11_output *output;
278 xcb_dri2_copy_region_cookie_t cookie;
279 struct timeval tv;
280 uint32_t msec;
281
282 glFlush();
283
284 wl_list_for_each(output, &c->base.output_list, base.link) {
285 cookie = xcb_dri2_copy_region_unchecked(c->conn,
286 output->window,
287 output->region,
288 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
289 XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT);
290 free(xcb_dri2_copy_region_reply(c->conn, cookie, NULL));
Kristian Høgsberg8f66a572011-01-07 08:38:56 -0500291 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400292
293 gettimeofday(&tv, NULL);
294 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
295 wlsc_compositor_finish_frame(&c->base, msec);
296}
297
298static void
299x11_output_set_wm_protocols(struct x11_output *output)
300{
301 xcb_atom_t list[1];
302 struct x11_compositor *c =
303 (struct x11_compositor *) output->base.compositor;
304
305 list[0] = c->atom.wm_delete_window;
306 xcb_change_property (c->conn,
307 XCB_PROP_MODE_REPLACE,
308 output->window,
309 c->atom.wm_protocols,
310 XCB_ATOM_ATOM,
311 32,
312 ARRAY_SIZE(list),
313 list);
314}
315
316struct wm_normal_hints {
317 uint32_t flags;
318 uint32_t pad[4];
319 int32_t min_width, min_height;
320 int32_t max_width, max_height;
321 int32_t width_inc, height_inc;
322 int32_t min_aspect_x, min_aspect_y;
323 int32_t max_aspect_x, max_aspect_y;
324 int32_t base_width, base_height;
325 int32_t win_gravity;
326};
327
328#define WM_NORMAL_HINTS_MIN_SIZE 16
329#define WM_NORMAL_HINTS_MAX_SIZE 32
330
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500331static void
332x11_output_set_icon(struct x11_compositor *c, struct x11_output *output,
333 const char *filename, int width, int height)
334{
335 uint32_t *icon, *pixels;
336
337 pixels = wlsc_load_image(filename, width, height);
338 if (!pixels)
339 return;
340 icon = malloc(width * height * 4 + 8);
341 if (!icon) {
342 free(pixels);
343 return;
344 }
345
346 icon[0] = width;
347 icon[1] = height;
348 memcpy(icon + 2, pixels, width * height * 4);
349 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
350 c->atom.net_wm_icon, c->atom.cardinal, 32,
351 width * height + 2, icon);
352 free(icon);
353 free(pixels);
354}
355
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400356static int
357x11_compositor_create_output(struct x11_compositor *c, int width, int height)
358{
359 static const char name[] = "Wayland Compositor";
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500360 static const char class[] = "wayland-1\0Wayland Compositor";
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400361 struct x11_output *output;
362 xcb_dri2_dri2_buffer_t *buffers;
363 xcb_dri2_get_buffers_reply_t *reply;
364 xcb_dri2_get_buffers_cookie_t cookie;
365 xcb_screen_iterator_t iter;
366 xcb_rectangle_t rectangle;
367 struct wm_normal_hints normal_hints;
368 unsigned int attachments[] =
369 { XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT};
370 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
371 uint32_t values[2] = {
372 XCB_EVENT_MASK_KEY_PRESS |
373 XCB_EVENT_MASK_KEY_RELEASE |
374 XCB_EVENT_MASK_BUTTON_PRESS |
375 XCB_EVENT_MASK_BUTTON_RELEASE |
376 XCB_EVENT_MASK_POINTER_MOTION |
377 XCB_EVENT_MASK_EXPOSURE |
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400378 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
379 XCB_EVENT_MASK_ENTER_WINDOW |
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500380 XCB_EVENT_MASK_LEAVE_WINDOW |
381 XCB_EVENT_MASK_KEYMAP_STATE |
382 XCB_EVENT_MASK_FOCUS_CHANGE,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400383 0
384 };
385
386 EGLint attribs[] = {
387 EGL_WIDTH, 0,
388 EGL_HEIGHT, 0,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400389 EGL_DRM_BUFFER_STRIDE_MESA, 0,
390 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400391 EGL_NONE
392 };
393
394 output = malloc(sizeof *output);
395 if (output == NULL)
396 return -1;
397
398 memset(output, 0, sizeof *output);
399 wlsc_output_init(&output->base, &c->base, 0, 0, width, height);
400
401 values[1] = c->null_cursor;
402 output->window = xcb_generate_id(c->conn);
403 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
404 xcb_create_window(c->conn,
405 XCB_COPY_FROM_PARENT,
406 output->window,
407 iter.data->root,
408 0, 0,
409 width, height,
410 0,
411 XCB_WINDOW_CLASS_INPUT_OUTPUT,
412 iter.data->root_visual,
413 mask, values);
414
415 /* Don't resize me. */
416 memset(&normal_hints, 0, sizeof normal_hints);
417 normal_hints.flags =
418 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
419 normal_hints.min_width = width;
420 normal_hints.min_height = height;
421 normal_hints.max_width = width;
422 normal_hints.max_height = height;
423 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
424 c->atom.wm_normal_hints,
425 c->atom.wm_size_hints, 32,
426 sizeof normal_hints / 4,
427 (uint8_t *) &normal_hints);
428
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400429 /* Set window name. Don't bother with non-EWMH WMs. */
430 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
431 c->atom.net_wm_name, c->atom.utf8_string, 8,
432 strlen(name), name);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500433 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
434 c->atom.wm_class, c->atom.string, 8,
435 sizeof class, class);
436
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500437 x11_output_set_icon(c, output,
438 DATADIR "/wayland/wayland.png", 128, 128);
439
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500440 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400441
442 rectangle.x = 0;
443 rectangle.y = 0;
444 rectangle.width = width;
445 rectangle.height = height;
446 output->region = xcb_generate_id(c->conn);
447 xcb_xfixes_create_region(c->conn, output->region, 1, &rectangle);
448
449 xcb_dri2_create_drawable (c->conn, output->window);
450
451 x11_output_set_wm_protocols(output);
452
453 cookie = xcb_dri2_get_buffers_unchecked (c->conn,
454 output->window,
455 1, 1, attachments);
456 reply = xcb_dri2_get_buffers_reply (c->conn, cookie, NULL);
457 if (reply == NULL)
458 return -1;
459 buffers = xcb_dri2_get_buffers_buffers (reply);
460 if (buffers == NULL)
461 return -1;
462
463 if (reply->count != 1) {
464 fprintf(stderr,
465 "got wrong number of buffers (%d)\n", reply->count);
466 return -1;
467 }
468
469 attribs[1] = reply->width;
470 attribs[3] = reply->height;
471 attribs[5] = buffers[0].pitch / 4;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400472 output->image =
Kristian Høgsberg175e6ce2011-01-06 15:45:19 -0500473 eglCreateImageKHR(c->base.display,
474 EGL_NO_CONTEXT,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400475 EGL_DRM_BUFFER_MESA,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400476 (EGLClientBuffer) buffers[0].name,
477 attribs);
Kristian Høgsberg8f2e6772010-07-29 14:48:13 -0400478 free(reply);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400479
480 glGenRenderbuffers(1, &output->rbo);
481 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo);
482
483 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
484 output->image);
485
486 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
487 GL_COLOR_ATTACHMENT0,
488 GL_RENDERBUFFER,
489 output->rbo);
490
491 wl_list_insert(c->base.output_list.prev, &output->base.link);
492
493 return 0;
494}
495
496static void
497idle_repaint(void *data)
498{
499 struct x11_output *output = data;
500 struct x11_compositor *c =
501 (struct x11_compositor *) output->base.compositor;
502 xcb_xfixes_region_t region;
503 xcb_dri2_copy_region_cookie_t cookie;
504
505 if (output->damage_count <= ARRAY_SIZE(output->damage)) {
506 region = xcb_generate_id(c->conn);
507 xcb_xfixes_create_region(c->conn, region,
508 output->damage_count, output->damage);
509 } else {
510 region = output->region;
511 }
512
513 cookie = xcb_dri2_copy_region_unchecked(c->conn,
514 output->window,
515 region,
516 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
517 XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT);
518
519 if (region != output->region)
520 xcb_xfixes_destroy_region(c->conn, region);
521
522 free(xcb_dri2_copy_region_reply(c->conn, cookie, NULL));
523 output->damage_count = 0;
524}
525
526static struct x11_output *
527x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
528{
529 struct x11_output *output;
530
531 wl_list_for_each(output, &c->base.output_list, base.link) {
532 if (output->window == window)
533 return output;
534 }
535
536 return NULL;
537}
538
539static void
540x11_compositor_handle_event(int fd, uint32_t mask, void *data)
541{
542 struct x11_compositor *c = data;
543 struct x11_output *output;
544 xcb_generic_event_t *event;
545 struct wl_event_loop *loop;
546 xcb_client_message_event_t *client_message;
547 xcb_motion_notify_event_t *motion_notify;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500548 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400549 xcb_key_press_event_t *key_press;
550 xcb_button_press_event_t *button_press;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500551 xcb_keymap_notify_event_t *keymap_notify;
552 xcb_focus_in_event_t *focus_in;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400553 xcb_expose_event_t *expose;
554 xcb_rectangle_t *r;
555 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500556 uint32_t *k;
557 int i, set;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400558
559 loop = wl_display_get_event_loop(c->base.wl_display);
560 while (event = xcb_poll_for_event (c->conn), event != NULL) {
561 switch (event->response_type & ~0x80) {
562
563 case XCB_KEY_PRESS:
564 key_press = (xcb_key_press_event_t *) event;
565 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400566 key_press->time, key_press->detail - 8, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400567 break;
568 case XCB_KEY_RELEASE:
569 key_press = (xcb_key_press_event_t *) event;
570 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400571 key_press->time, key_press->detail - 8, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400572 break;
573 case XCB_BUTTON_PRESS:
574 button_press = (xcb_button_press_event_t *) event;
575 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400576 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400577 button_press->detail + BTN_LEFT - 1, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400578 break;
579 case XCB_BUTTON_RELEASE:
580 button_press = (xcb_button_press_event_t *) event;
581 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400582 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400583 button_press->detail + BTN_LEFT - 1, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400584 break;
585
586 case XCB_MOTION_NOTIFY:
587 motion_notify = (xcb_motion_notify_event_t *) event;
588 notify_motion(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400589 motion_notify->time,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400590 motion_notify->event_x,
591 motion_notify->event_y);
592 break;
593
594 case XCB_EXPOSE:
595 expose = (xcb_expose_event_t *) event;
596 output = x11_compositor_find_output(c, expose->window);
597 if (output->damage_count == 0)
598 wl_event_loop_add_idle(loop,
599 idle_repaint, output);
600
601 r = &output->damage[output->damage_count++];
602 if (output->damage_count > 16)
603 break;
604 r->x = expose->x;
605 r->y = expose->y;
606 r->width = expose->width;
607 r->height = expose->height;
608 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400609
610 case XCB_ENTER_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500611 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500612 if (enter_notify->state >= Button1Mask)
613 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500614 output = x11_compositor_find_output(c, enter_notify->event);
615 notify_pointer_focus(c->base.input_device,
616 enter_notify->time,
617 &output->base,
618 enter_notify->event_x,
619 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400620 break;
621
622 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500623 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500624 if (enter_notify->state >= Button1Mask)
625 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500626 notify_pointer_focus(c->base.input_device,
627 enter_notify->time,
628 NULL,
629 enter_notify->event_x,
630 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400631 break;
632
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400633 case XCB_CLIENT_MESSAGE:
634 client_message = (xcb_client_message_event_t *) event;
635 atom = client_message->data.data32[0];
636 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500637 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400638 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400639
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500640 case XCB_FOCUS_IN:
641 focus_in = (xcb_focus_in_event_t *) event;
642 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
643 break;
644
645 output = x11_compositor_find_output(c, focus_in->event);
646 notify_keyboard_focus(c->base.input_device,
647 get_time(),
648 &output->base, &c->keys);
649 break;
650
651 case XCB_FOCUS_OUT:
652 focus_in = (xcb_focus_in_event_t *) event;
653 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
654 break;
655 notify_keyboard_focus(c->base.input_device,
656 get_time(), NULL, NULL);
657 break;
658
659 case XCB_KEYMAP_NOTIFY:
660 keymap_notify = (xcb_keymap_notify_event_t *) event;
661 c->keys.size = 0;
662 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
663 set = keymap_notify->keys[i >> 3] &
664 (1 << (i & 7));
665 if (set) {
666 k = wl_array_add(&c->keys, sizeof *k);
667 *k = i;
668 }
669 }
670 break;
671 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400672 break;
673 }
674
675 free (event);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500676 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400677}
678
679#define F(field) offsetof(struct x11_compositor, field)
680
681static void
682x11_compositor_get_resources(struct x11_compositor *c)
683{
684 static const struct { const char *name; int offset; } atoms[] = {
685 { "WM_PROTOCOLS", F(atom.wm_protocols) },
686 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
687 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
688 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500689 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400690 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500691 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500692 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400693 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500694 { "CARDINAL", F(atom.cardinal) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400695 };
696
697 xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
698 xcb_intern_atom_reply_t *reply;
699 xcb_pixmap_t pixmap;
700 xcb_gc_t gc;
701 int i;
702 uint8_t data[] = { 0, 0, 0, 0 };
703
704 for (i = 0; i < ARRAY_SIZE(atoms); i++)
705 cookies[i] = xcb_intern_atom (c->conn, 0,
706 strlen(atoms[i].name),
707 atoms[i].name);
708
709 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
710 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
711 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
712 free(reply);
713 }
714
715 pixmap = xcb_generate_id(c->conn);
716 gc = xcb_generate_id(c->conn);
717 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
718 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
719 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
720 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
721 c->null_cursor = xcb_generate_id(c->conn);
722 xcb_create_cursor (c->conn, c->null_cursor,
723 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
724 xcb_free_gc(c->conn, gc);
725 xcb_free_pixmap(c->conn, pixmap);
726}
727
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400728static int
729x11_authenticate(struct wlsc_compositor *c, uint32_t id)
730{
731 return dri2_authenticate((struct x11_compositor *) c, id);
732}
733
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500734static void
735x11_destroy(struct wlsc_compositor *ec)
736{
737 free(ec);
738}
739
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400740struct wlsc_compositor *
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400741x11_compositor_create(struct wl_display *display, int width, int height)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400742{
743 struct x11_compositor *c;
744 struct wl_event_loop *loop;
745 xcb_screen_iterator_t s;
746
747 c = malloc(sizeof *c);
748 if (c == NULL)
749 return NULL;
750
751 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100752
753 c->dpy = XOpenDisplay(NULL);
754 c->conn = XGetXCBConnection(c->dpy);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400755
756 if (xcb_connection_has_error(c->conn))
757 return NULL;
758
759 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
760 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500761 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400762
763 x11_compositor_get_resources(c);
764
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400765 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +0200766 if (x11_compositor_init_egl(c) < 0)
Darxus55973f22010-11-22 21:24:39 -0500767 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400768
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500769 c->base.destroy = x11_destroy;
770 c->base.authenticate = x11_authenticate;
771 c->base.present = x11_compositor_present;
772 c->base.create_buffer = wlsc_drm_buffer_create;
773
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400774 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400775 if (wlsc_compositor_init(&c->base, display) < 0)
776 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400777
Darxus55973f22010-11-22 21:24:39 -0500778 if (x11_compositor_create_output(c, width, height) < 0)
779 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400780
Darxus55973f22010-11-22 21:24:39 -0500781 if (x11_input_create(c) < 0)
782 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400783
784 loop = wl_display_get_event_loop(c->base.wl_display);
785
786 c->xcb_source =
787 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
788 WL_EVENT_READABLE,
789 x11_compositor_handle_event, c);
790
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400791 return &c->base;
792}