blob: 4a5f10bbde957baf73cd947fe8b0a53a14c6dcaa [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
37#define GL_GLEXT_PROTOTYPES
38#define EGL_EGLEXT_PROTOTYPES
39#include <GLES2/gl2.h>
40#include <GLES2/gl2ext.h>
41#include <EGL/egl.h>
42#include <EGL/eglext.h>
43
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040044#include "compositor.h"
45
46#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
47
48struct x11_compositor {
49 struct wlsc_compositor base;
50
51 xcb_connection_t *conn;
52 xcb_screen_t *screen;
53 xcb_cursor_t null_cursor;
54 int dri2_major;
55 int dri2_minor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040056 struct wl_event_source *xcb_source;
57 struct {
58 xcb_atom_t wm_protocols;
59 xcb_atom_t wm_normal_hints;
60 xcb_atom_t wm_size_hints;
61 xcb_atom_t wm_delete_window;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050062 xcb_atom_t wm_class;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040063 xcb_atom_t net_wm_name;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050064 xcb_atom_t net_wm_icon;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050065 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040066 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050067 xcb_atom_t cardinal;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040068 } atom;
69};
70
71struct x11_output {
72 struct wlsc_output base;
73
74 xcb_xfixes_region_t region;
75 xcb_window_t window;
76 GLuint rbo;
77 EGLImageKHR image;
78 xcb_rectangle_t damage[16];
79 int damage_count;
80};
81
82struct x11_input {
83 struct wlsc_input_device base;
84};
85
86
Darxus55973f22010-11-22 21:24:39 -050087static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040088x11_input_create(struct x11_compositor *c)
89{
90 struct x11_input *input;
91
92 input = malloc(sizeof *input);
93 if (input == NULL)
Darxus55973f22010-11-22 21:24:39 -050094 return -1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040095
96 memset(input, 0, sizeof *input);
97 wlsc_input_device_init(&input->base, &c->base);
98
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -050099 c->base.input_device = &input->base.input_device;
Darxus55973f22010-11-22 21:24:39 -0500100
101 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400102}
103
104
105static int
106dri2_connect(struct x11_compositor *c)
107{
108 xcb_xfixes_query_version_reply_t *xfixes_query;
109 xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
110 xcb_dri2_query_version_reply_t *dri2_query;
111 xcb_dri2_query_version_cookie_t dri2_query_cookie;
112 xcb_dri2_connect_reply_t *connect;
113 xcb_dri2_connect_cookie_t connect_cookie;
114 xcb_generic_error_t *error;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400115 char path[256];
116 int fd;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400117
118 xcb_prefetch_extension_data (c->conn, &xcb_xfixes_id);
119 xcb_prefetch_extension_data (c->conn, &xcb_dri2_id);
120
121 xfixes_query_cookie =
122 xcb_xfixes_query_version(c->conn,
123 XCB_XFIXES_MAJOR_VERSION,
124 XCB_XFIXES_MINOR_VERSION);
125
126 dri2_query_cookie =
127 xcb_dri2_query_version (c->conn,
128 XCB_DRI2_MAJOR_VERSION,
129 XCB_DRI2_MINOR_VERSION);
130
131 connect_cookie = xcb_dri2_connect_unchecked (c->conn,
132 c->screen->root,
133 XCB_DRI2_DRIVER_TYPE_DRI);
134
135 xfixes_query =
136 xcb_xfixes_query_version_reply (c->conn,
137 xfixes_query_cookie, &error);
138 if (xfixes_query == NULL ||
139 error != NULL || xfixes_query->major_version < 2) {
140 free(error);
141 return -1;
142 }
143 free(xfixes_query);
144
145 dri2_query =
146 xcb_dri2_query_version_reply (c->conn,
147 dri2_query_cookie, &error);
148 if (dri2_query == NULL || error != NULL) {
Yuval Fledel91b59992010-11-22 21:42:58 +0200149 fprintf(stderr, "DRI2: failed to query version\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400150 free(error);
151 return EGL_FALSE;
152 }
153 c->dri2_major = dri2_query->major_version;
154 c->dri2_minor = dri2_query->minor_version;
155 free(dri2_query);
156
157 connect = xcb_dri2_connect_reply (c->conn,
158 connect_cookie, NULL);
159 if (connect == NULL ||
160 connect->driver_name_length + connect->device_name_length == 0) {
Yuval Fledel91b59992010-11-22 21:42:58 +0200161 fprintf(stderr, "DRI2: failed to connect, DRI2 version: %u.%u\n",
162 c->dri2_major, c->dri2_minor);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400163 return -1;
164 }
165
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +0800166#ifdef XCB_DRI2_CONNECT_DEVICE_NAME_BROKEN
167 {
168 char *driver_name, *device_name;
169
170 driver_name = xcb_dri2_connect_driver_name (connect);
171 device_name = driver_name +
172 ((connect->driver_name_length + 3) & ~3);
173 snprintf(path, sizeof path, "%.*s",
174 xcb_dri2_connect_device_name_length (connect),
175 device_name);
176 }
177#else
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400178 snprintf(path, sizeof path, "%.*s",
179 xcb_dri2_connect_device_name_length (connect),
180 xcb_dri2_connect_device_name (connect));
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +0800181#endif
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400182 free(connect);
183
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400184 fd = open(path, O_RDWR);
185 if (fd < 0) {
186 fprintf(stderr,
Yuval Fledel91b59992010-11-22 21:42:58 +0200187 "DRI2: could not open %s (%s)\n", path, strerror(errno));
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400188 return -1;
189 }
190
191 return wlsc_drm_init(&c->base, fd, path);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400192}
193
194static int
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400195dri2_authenticate(struct x11_compositor *c, uint32_t magic)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400196{
197 xcb_dri2_authenticate_reply_t *authenticate;
198 xcb_dri2_authenticate_cookie_t authenticate_cookie;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400199
200 authenticate_cookie =
201 xcb_dri2_authenticate_unchecked(c->conn,
202 c->screen->root, magic);
203 authenticate =
204 xcb_dri2_authenticate_reply(c->conn,
205 authenticate_cookie, NULL);
206 if (authenticate == NULL || !authenticate->authenticated) {
Yuval Fledel91b59992010-11-22 21:42:58 +0200207 fprintf(stderr, "DRI2: failed to authenticate\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400208 free(authenticate);
209 return -1;
210 }
211
212 free(authenticate);
213
214 return 0;
215}
216
217static int
218x11_compositor_init_egl(struct x11_compositor *c)
219{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400220 EGLint major, minor;
221 const char *extensions;
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400222 drm_magic_t magic;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400223 static const EGLint context_attribs[] = {
224 EGL_CONTEXT_CLIENT_VERSION, 2,
225 EGL_NONE
226 };
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400227
228 if (dri2_connect(c) < 0)
229 return -1;
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -0400230
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400231 if (drmGetMagic(c->base.drm.fd, &magic)) {
Yuval Fledel91b59992010-11-22 21:42:58 +0200232 fprintf(stderr, "DRI2: failed to get drm magic\n");
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400233 return -1;
234 }
235
236 if (dri2_authenticate(c, magic) < 0)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400237 return -1;
238
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400239 c->base.display = eglGetDRMDisplayMESA(c->base.drm.fd);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400240 if (c->base.display == NULL) {
241 fprintf(stderr, "failed to create display\n");
242 return -1;
243 }
244
245 if (!eglInitialize(c->base.display, &major, &minor)) {
246 fprintf(stderr, "failed to initialize display\n");
247 return -1;
248 }
249
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400250 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
251 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
252 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400253 return -1;
254 }
255
Darxus55973f22010-11-22 21:24:39 -0500256 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
257 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
258 return -1;
259 }
260
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400261 c->base.context = eglCreateContext(c->base.display, NULL,
262 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400263 if (c->base.context == NULL) {
264 fprintf(stderr, "failed to create context\n");
265 return -1;
266 }
267
268 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
269 EGL_NO_SURFACE, c->base.context)) {
270 fprintf(stderr, "failed to make context current\n");
271 return -1;
272 }
273
274 return 0;
275}
276
277static void
278x11_compositor_present(struct wlsc_compositor *base)
279{
280 struct x11_compositor *c = (struct x11_compositor *) base;
281 struct x11_output *output;
282 xcb_dri2_copy_region_cookie_t cookie;
283 struct timeval tv;
284 uint32_t msec;
285
286 glFlush();
287
288 wl_list_for_each(output, &c->base.output_list, base.link) {
289 cookie = xcb_dri2_copy_region_unchecked(c->conn,
290 output->window,
291 output->region,
292 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
293 XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT);
294 free(xcb_dri2_copy_region_reply(c->conn, cookie, NULL));
Kristian Høgsberg8f66a572011-01-07 08:38:56 -0500295 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400296
297 gettimeofday(&tv, NULL);
298 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
299 wlsc_compositor_finish_frame(&c->base, msec);
300}
301
302static void
303x11_output_set_wm_protocols(struct x11_output *output)
304{
305 xcb_atom_t list[1];
306 struct x11_compositor *c =
307 (struct x11_compositor *) output->base.compositor;
308
309 list[0] = c->atom.wm_delete_window;
310 xcb_change_property (c->conn,
311 XCB_PROP_MODE_REPLACE,
312 output->window,
313 c->atom.wm_protocols,
314 XCB_ATOM_ATOM,
315 32,
316 ARRAY_SIZE(list),
317 list);
318}
319
320struct wm_normal_hints {
321 uint32_t flags;
322 uint32_t pad[4];
323 int32_t min_width, min_height;
324 int32_t max_width, max_height;
325 int32_t width_inc, height_inc;
326 int32_t min_aspect_x, min_aspect_y;
327 int32_t max_aspect_x, max_aspect_y;
328 int32_t base_width, base_height;
329 int32_t win_gravity;
330};
331
332#define WM_NORMAL_HINTS_MIN_SIZE 16
333#define WM_NORMAL_HINTS_MAX_SIZE 32
334
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500335static void
336x11_output_set_icon(struct x11_compositor *c, struct x11_output *output,
337 const char *filename, int width, int height)
338{
339 uint32_t *icon, *pixels;
340
341 pixels = wlsc_load_image(filename, width, height);
342 if (!pixels)
343 return;
344 icon = malloc(width * height * 4 + 8);
345 if (!icon) {
346 free(pixels);
347 return;
348 }
349
350 icon[0] = width;
351 icon[1] = height;
352 memcpy(icon + 2, pixels, width * height * 4);
353 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
354 c->atom.net_wm_icon, c->atom.cardinal, 32,
355 width * height + 2, icon);
356 free(icon);
357 free(pixels);
358}
359
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400360static int
361x11_compositor_create_output(struct x11_compositor *c, int width, int height)
362{
363 static const char name[] = "Wayland Compositor";
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500364 static const char class[] = "wayland-1\0Wayland Compositor";
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400365 struct x11_output *output;
366 xcb_dri2_dri2_buffer_t *buffers;
367 xcb_dri2_get_buffers_reply_t *reply;
368 xcb_dri2_get_buffers_cookie_t cookie;
369 xcb_screen_iterator_t iter;
370 xcb_rectangle_t rectangle;
371 struct wm_normal_hints normal_hints;
372 unsigned int attachments[] =
373 { XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT};
374 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
375 uint32_t values[2] = {
376 XCB_EVENT_MASK_KEY_PRESS |
377 XCB_EVENT_MASK_KEY_RELEASE |
378 XCB_EVENT_MASK_BUTTON_PRESS |
379 XCB_EVENT_MASK_BUTTON_RELEASE |
380 XCB_EVENT_MASK_POINTER_MOTION |
381 XCB_EVENT_MASK_EXPOSURE |
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400382 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
383 XCB_EVENT_MASK_ENTER_WINDOW |
384 XCB_EVENT_MASK_LEAVE_WINDOW,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400385 0
386 };
387
388 EGLint attribs[] = {
389 EGL_WIDTH, 0,
390 EGL_HEIGHT, 0,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400391 EGL_DRM_BUFFER_STRIDE_MESA, 0,
392 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400393 EGL_NONE
394 };
395
396 output = malloc(sizeof *output);
397 if (output == NULL)
398 return -1;
399
400 memset(output, 0, sizeof *output);
401 wlsc_output_init(&output->base, &c->base, 0, 0, width, height);
402
403 values[1] = c->null_cursor;
404 output->window = xcb_generate_id(c->conn);
405 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
406 xcb_create_window(c->conn,
407 XCB_COPY_FROM_PARENT,
408 output->window,
409 iter.data->root,
410 0, 0,
411 width, height,
412 0,
413 XCB_WINDOW_CLASS_INPUT_OUTPUT,
414 iter.data->root_visual,
415 mask, values);
416
417 /* Don't resize me. */
418 memset(&normal_hints, 0, sizeof normal_hints);
419 normal_hints.flags =
420 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
421 normal_hints.min_width = width;
422 normal_hints.min_height = height;
423 normal_hints.max_width = width;
424 normal_hints.max_height = height;
425 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
426 c->atom.wm_normal_hints,
427 c->atom.wm_size_hints, 32,
428 sizeof normal_hints / 4,
429 (uint8_t *) &normal_hints);
430
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400431 /* Set window name. Don't bother with non-EWMH WMs. */
432 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
433 c->atom.net_wm_name, c->atom.utf8_string, 8,
434 strlen(name), name);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500435 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
436 c->atom.wm_class, c->atom.string, 8,
437 sizeof class, class);
438
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500439 x11_output_set_icon(c, output,
440 DATADIR "/wayland/wayland.png", 128, 128);
441
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500442 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400443
444 rectangle.x = 0;
445 rectangle.y = 0;
446 rectangle.width = width;
447 rectangle.height = height;
448 output->region = xcb_generate_id(c->conn);
449 xcb_xfixes_create_region(c->conn, output->region, 1, &rectangle);
450
451 xcb_dri2_create_drawable (c->conn, output->window);
452
453 x11_output_set_wm_protocols(output);
454
455 cookie = xcb_dri2_get_buffers_unchecked (c->conn,
456 output->window,
457 1, 1, attachments);
458 reply = xcb_dri2_get_buffers_reply (c->conn, cookie, NULL);
459 if (reply == NULL)
460 return -1;
461 buffers = xcb_dri2_get_buffers_buffers (reply);
462 if (buffers == NULL)
463 return -1;
464
465 if (reply->count != 1) {
466 fprintf(stderr,
467 "got wrong number of buffers (%d)\n", reply->count);
468 return -1;
469 }
470
471 attribs[1] = reply->width;
472 attribs[3] = reply->height;
473 attribs[5] = buffers[0].pitch / 4;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400474 output->image =
Kristian Høgsberg175e6ce2011-01-06 15:45:19 -0500475 eglCreateImageKHR(c->base.display,
476 EGL_NO_CONTEXT,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400477 EGL_DRM_BUFFER_MESA,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400478 (EGLClientBuffer) buffers[0].name,
479 attribs);
Kristian Høgsberg8f2e6772010-07-29 14:48:13 -0400480 free(reply);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400481
482 glGenRenderbuffers(1, &output->rbo);
483 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo);
484
485 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
486 output->image);
487
488 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
489 GL_COLOR_ATTACHMENT0,
490 GL_RENDERBUFFER,
491 output->rbo);
492
493 wl_list_insert(c->base.output_list.prev, &output->base.link);
494
495 return 0;
496}
497
498static void
499idle_repaint(void *data)
500{
501 struct x11_output *output = data;
502 struct x11_compositor *c =
503 (struct x11_compositor *) output->base.compositor;
504 xcb_xfixes_region_t region;
505 xcb_dri2_copy_region_cookie_t cookie;
506
507 if (output->damage_count <= ARRAY_SIZE(output->damage)) {
508 region = xcb_generate_id(c->conn);
509 xcb_xfixes_create_region(c->conn, region,
510 output->damage_count, output->damage);
511 } else {
512 region = output->region;
513 }
514
515 cookie = xcb_dri2_copy_region_unchecked(c->conn,
516 output->window,
517 region,
518 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
519 XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT);
520
521 if (region != output->region)
522 xcb_xfixes_destroy_region(c->conn, region);
523
524 free(xcb_dri2_copy_region_reply(c->conn, cookie, NULL));
525 output->damage_count = 0;
526}
527
528static struct x11_output *
529x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
530{
531 struct x11_output *output;
532
533 wl_list_for_each(output, &c->base.output_list, base.link) {
534 if (output->window == window)
535 return output;
536 }
537
538 return NULL;
539}
540
541static void
542x11_compositor_handle_event(int fd, uint32_t mask, void *data)
543{
544 struct x11_compositor *c = data;
545 struct x11_output *output;
546 xcb_generic_event_t *event;
547 struct wl_event_loop *loop;
548 xcb_client_message_event_t *client_message;
549 xcb_motion_notify_event_t *motion_notify;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500550 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400551 xcb_key_press_event_t *key_press;
552 xcb_button_press_event_t *button_press;
553 xcb_expose_event_t *expose;
554 xcb_rectangle_t *r;
555 xcb_atom_t atom;
556
557 loop = wl_display_get_event_loop(c->base.wl_display);
558 while (event = xcb_poll_for_event (c->conn), event != NULL) {
559 switch (event->response_type & ~0x80) {
560
561 case XCB_KEY_PRESS:
562 key_press = (xcb_key_press_event_t *) event;
563 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400564 key_press->time, key_press->detail - 8, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400565 break;
566 case XCB_KEY_RELEASE:
567 key_press = (xcb_key_press_event_t *) event;
568 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400569 key_press->time, key_press->detail - 8, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400570 break;
571 case XCB_BUTTON_PRESS:
572 button_press = (xcb_button_press_event_t *) event;
573 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400574 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400575 button_press->detail + BTN_LEFT - 1, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400576 break;
577 case XCB_BUTTON_RELEASE:
578 button_press = (xcb_button_press_event_t *) event;
579 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400580 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400581 button_press->detail + BTN_LEFT - 1, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400582 break;
583
584 case XCB_MOTION_NOTIFY:
585 motion_notify = (xcb_motion_notify_event_t *) event;
586 notify_motion(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400587 motion_notify->time,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400588 motion_notify->event_x,
589 motion_notify->event_y);
590 break;
591
592 case XCB_EXPOSE:
593 expose = (xcb_expose_event_t *) event;
594 output = x11_compositor_find_output(c, expose->window);
595 if (output->damage_count == 0)
596 wl_event_loop_add_idle(loop,
597 idle_repaint, output);
598
599 r = &output->damage[output->damage_count++];
600 if (output->damage_count > 16)
601 break;
602 r->x = expose->x;
603 r->y = expose->y;
604 r->width = expose->width;
605 r->height = expose->height;
606 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400607
608 case XCB_ENTER_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500609 enter_notify = (xcb_enter_notify_event_t *) event;
610 output = x11_compositor_find_output(c, enter_notify->event);
611 notify_pointer_focus(c->base.input_device,
612 enter_notify->time,
613 &output->base,
614 enter_notify->event_x,
615 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400616 break;
617
618 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500619 enter_notify = (xcb_enter_notify_event_t *) event;
620 notify_pointer_focus(c->base.input_device,
621 enter_notify->time,
622 NULL,
623 enter_notify->event_x,
624 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400625 break;
626
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400627 case XCB_CLIENT_MESSAGE:
628 client_message = (xcb_client_message_event_t *) event;
629 atom = client_message->data.data32[0];
630 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500631 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400632 break;
633 default:
634
635 break;
636 }
637
638 free (event);
639 }
640}
641
642#define F(field) offsetof(struct x11_compositor, field)
643
644static void
645x11_compositor_get_resources(struct x11_compositor *c)
646{
647 static const struct { const char *name; int offset; } atoms[] = {
648 { "WM_PROTOCOLS", F(atom.wm_protocols) },
649 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
650 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
651 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500652 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400653 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500654 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500655 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400656 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500657 { "CARDINAL", F(atom.cardinal) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400658 };
659
660 xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
661 xcb_intern_atom_reply_t *reply;
662 xcb_pixmap_t pixmap;
663 xcb_gc_t gc;
664 int i;
665 uint8_t data[] = { 0, 0, 0, 0 };
666
667 for (i = 0; i < ARRAY_SIZE(atoms); i++)
668 cookies[i] = xcb_intern_atom (c->conn, 0,
669 strlen(atoms[i].name),
670 atoms[i].name);
671
672 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
673 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
674 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
675 free(reply);
676 }
677
678 pixmap = xcb_generate_id(c->conn);
679 gc = xcb_generate_id(c->conn);
680 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
681 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
682 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
683 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
684 c->null_cursor = xcb_generate_id(c->conn);
685 xcb_create_cursor (c->conn, c->null_cursor,
686 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
687 xcb_free_gc(c->conn, gc);
688 xcb_free_pixmap(c->conn, pixmap);
689}
690
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400691static int
692x11_authenticate(struct wlsc_compositor *c, uint32_t id)
693{
694 return dri2_authenticate((struct x11_compositor *) c, id);
695}
696
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500697static void
698x11_destroy(struct wlsc_compositor *ec)
699{
700 free(ec);
701}
702
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400703struct wlsc_compositor *
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400704x11_compositor_create(struct wl_display *display, int width, int height)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400705{
706 struct x11_compositor *c;
707 struct wl_event_loop *loop;
708 xcb_screen_iterator_t s;
709
710 c = malloc(sizeof *c);
711 if (c == NULL)
712 return NULL;
713
714 memset(c, 0, sizeof *c);
715 c->conn = xcb_connect(0, 0);
716
717 if (xcb_connection_has_error(c->conn))
718 return NULL;
719
720 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
721 c->screen = s.data;
722
723 x11_compositor_get_resources(c);
724
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400725 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +0200726 if (x11_compositor_init_egl(c) < 0)
Darxus55973f22010-11-22 21:24:39 -0500727 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400728
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500729 c->base.destroy = x11_destroy;
730 c->base.authenticate = x11_authenticate;
731 c->base.present = x11_compositor_present;
732 c->base.create_buffer = wlsc_drm_buffer_create;
733
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400734 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400735 if (wlsc_compositor_init(&c->base, display) < 0)
736 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400737
Darxus55973f22010-11-22 21:24:39 -0500738 if (x11_compositor_create_output(c, width, height) < 0)
739 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400740
Darxus55973f22010-11-22 21:24:39 -0500741 if (x11_input_create(c) < 0)
742 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400743
744 loop = wl_display_get_event_loop(c->base.wl_display);
745
746 c->xcb_source =
747 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
748 WL_EVENT_READABLE,
749 x11_compositor_handle_event, c);
750
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400751 return &c->base;
752}