blob: 5cacb3fa1114552b97d6e1776fcee75f98340868 [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øgsberg24ed6212011-01-26 14:02:31 -050064 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040065 xcb_atom_t utf8_string;
66 } atom;
67};
68
69struct x11_output {
70 struct wlsc_output base;
71
72 xcb_xfixes_region_t region;
73 xcb_window_t window;
74 GLuint rbo;
75 EGLImageKHR image;
76 xcb_rectangle_t damage[16];
77 int damage_count;
78};
79
80struct x11_input {
81 struct wlsc_input_device base;
82};
83
84
Darxus55973f22010-11-22 21:24:39 -050085static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040086x11_input_create(struct x11_compositor *c)
87{
88 struct x11_input *input;
89
90 input = malloc(sizeof *input);
91 if (input == NULL)
Darxus55973f22010-11-22 21:24:39 -050092 return -1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040093
94 memset(input, 0, sizeof *input);
95 wlsc_input_device_init(&input->base, &c->base);
96
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -050097 c->base.input_device = &input->base.input_device;
Darxus55973f22010-11-22 21:24:39 -050098
99 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400100}
101
102
103static int
104dri2_connect(struct x11_compositor *c)
105{
106 xcb_xfixes_query_version_reply_t *xfixes_query;
107 xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
108 xcb_dri2_query_version_reply_t *dri2_query;
109 xcb_dri2_query_version_cookie_t dri2_query_cookie;
110 xcb_dri2_connect_reply_t *connect;
111 xcb_dri2_connect_cookie_t connect_cookie;
112 xcb_generic_error_t *error;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400113 char path[256];
114 int fd;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400115
116 xcb_prefetch_extension_data (c->conn, &xcb_xfixes_id);
117 xcb_prefetch_extension_data (c->conn, &xcb_dri2_id);
118
119 xfixes_query_cookie =
120 xcb_xfixes_query_version(c->conn,
121 XCB_XFIXES_MAJOR_VERSION,
122 XCB_XFIXES_MINOR_VERSION);
123
124 dri2_query_cookie =
125 xcb_dri2_query_version (c->conn,
126 XCB_DRI2_MAJOR_VERSION,
127 XCB_DRI2_MINOR_VERSION);
128
129 connect_cookie = xcb_dri2_connect_unchecked (c->conn,
130 c->screen->root,
131 XCB_DRI2_DRIVER_TYPE_DRI);
132
133 xfixes_query =
134 xcb_xfixes_query_version_reply (c->conn,
135 xfixes_query_cookie, &error);
136 if (xfixes_query == NULL ||
137 error != NULL || xfixes_query->major_version < 2) {
138 free(error);
139 return -1;
140 }
141 free(xfixes_query);
142
143 dri2_query =
144 xcb_dri2_query_version_reply (c->conn,
145 dri2_query_cookie, &error);
146 if (dri2_query == NULL || error != NULL) {
Yuval Fledel91b59992010-11-22 21:42:58 +0200147 fprintf(stderr, "DRI2: failed to query version\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400148 free(error);
149 return EGL_FALSE;
150 }
151 c->dri2_major = dri2_query->major_version;
152 c->dri2_minor = dri2_query->minor_version;
153 free(dri2_query);
154
155 connect = xcb_dri2_connect_reply (c->conn,
156 connect_cookie, NULL);
157 if (connect == NULL ||
158 connect->driver_name_length + connect->device_name_length == 0) {
Yuval Fledel91b59992010-11-22 21:42:58 +0200159 fprintf(stderr, "DRI2: failed to connect, DRI2 version: %u.%u\n",
160 c->dri2_major, c->dri2_minor);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400161 return -1;
162 }
163
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +0800164#ifdef XCB_DRI2_CONNECT_DEVICE_NAME_BROKEN
165 {
166 char *driver_name, *device_name;
167
168 driver_name = xcb_dri2_connect_driver_name (connect);
169 device_name = driver_name +
170 ((connect->driver_name_length + 3) & ~3);
171 snprintf(path, sizeof path, "%.*s",
172 xcb_dri2_connect_device_name_length (connect),
173 device_name);
174 }
175#else
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400176 snprintf(path, sizeof path, "%.*s",
177 xcb_dri2_connect_device_name_length (connect),
178 xcb_dri2_connect_device_name (connect));
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +0800179#endif
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400180 free(connect);
181
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400182 fd = open(path, O_RDWR);
183 if (fd < 0) {
184 fprintf(stderr,
Yuval Fledel91b59992010-11-22 21:42:58 +0200185 "DRI2: could not open %s (%s)\n", path, strerror(errno));
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400186 return -1;
187 }
188
189 return wlsc_drm_init(&c->base, fd, path);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400190}
191
192static int
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400193dri2_authenticate(struct x11_compositor *c, uint32_t magic)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400194{
195 xcb_dri2_authenticate_reply_t *authenticate;
196 xcb_dri2_authenticate_cookie_t authenticate_cookie;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400197
198 authenticate_cookie =
199 xcb_dri2_authenticate_unchecked(c->conn,
200 c->screen->root, magic);
201 authenticate =
202 xcb_dri2_authenticate_reply(c->conn,
203 authenticate_cookie, NULL);
204 if (authenticate == NULL || !authenticate->authenticated) {
Yuval Fledel91b59992010-11-22 21:42:58 +0200205 fprintf(stderr, "DRI2: failed to authenticate\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400206 free(authenticate);
207 return -1;
208 }
209
210 free(authenticate);
211
212 return 0;
213}
214
215static int
216x11_compositor_init_egl(struct x11_compositor *c)
217{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400218 EGLint major, minor;
219 const char *extensions;
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400220 drm_magic_t magic;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400221 static const EGLint context_attribs[] = {
222 EGL_CONTEXT_CLIENT_VERSION, 2,
223 EGL_NONE
224 };
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400225
226 if (dri2_connect(c) < 0)
227 return -1;
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -0400228
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400229 if (drmGetMagic(c->base.drm.fd, &magic)) {
Yuval Fledel91b59992010-11-22 21:42:58 +0200230 fprintf(stderr, "DRI2: failed to get drm magic\n");
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400231 return -1;
232 }
233
234 if (dri2_authenticate(c, magic) < 0)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400235 return -1;
236
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400237 c->base.display = eglGetDRMDisplayMESA(c->base.drm.fd);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400238 if (c->base.display == NULL) {
239 fprintf(stderr, "failed to create display\n");
240 return -1;
241 }
242
243 if (!eglInitialize(c->base.display, &major, &minor)) {
244 fprintf(stderr, "failed to initialize display\n");
245 return -1;
246 }
247
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400248 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
249 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
250 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400251 return -1;
252 }
253
Darxus55973f22010-11-22 21:24:39 -0500254 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
255 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
256 return -1;
257 }
258
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400259 c->base.context = eglCreateContext(c->base.display, NULL,
260 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400261 if (c->base.context == NULL) {
262 fprintf(stderr, "failed to create context\n");
263 return -1;
264 }
265
266 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
267 EGL_NO_SURFACE, c->base.context)) {
268 fprintf(stderr, "failed to make context current\n");
269 return -1;
270 }
271
272 return 0;
273}
274
275static void
276x11_compositor_present(struct wlsc_compositor *base)
277{
278 struct x11_compositor *c = (struct x11_compositor *) base;
279 struct x11_output *output;
280 xcb_dri2_copy_region_cookie_t cookie;
281 struct timeval tv;
282 uint32_t msec;
283
284 glFlush();
285
286 wl_list_for_each(output, &c->base.output_list, base.link) {
287 cookie = xcb_dri2_copy_region_unchecked(c->conn,
288 output->window,
289 output->region,
290 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
291 XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT);
292 free(xcb_dri2_copy_region_reply(c->conn, cookie, NULL));
Kristian Høgsberg8f66a572011-01-07 08:38:56 -0500293 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400294
295 gettimeofday(&tv, NULL);
296 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
297 wlsc_compositor_finish_frame(&c->base, msec);
298}
299
300static void
301x11_output_set_wm_protocols(struct x11_output *output)
302{
303 xcb_atom_t list[1];
304 struct x11_compositor *c =
305 (struct x11_compositor *) output->base.compositor;
306
307 list[0] = c->atom.wm_delete_window;
308 xcb_change_property (c->conn,
309 XCB_PROP_MODE_REPLACE,
310 output->window,
311 c->atom.wm_protocols,
312 XCB_ATOM_ATOM,
313 32,
314 ARRAY_SIZE(list),
315 list);
316}
317
318struct wm_normal_hints {
319 uint32_t flags;
320 uint32_t pad[4];
321 int32_t min_width, min_height;
322 int32_t max_width, max_height;
323 int32_t width_inc, height_inc;
324 int32_t min_aspect_x, min_aspect_y;
325 int32_t max_aspect_x, max_aspect_y;
326 int32_t base_width, base_height;
327 int32_t win_gravity;
328};
329
330#define WM_NORMAL_HINTS_MIN_SIZE 16
331#define WM_NORMAL_HINTS_MAX_SIZE 32
332
333static int
334x11_compositor_create_output(struct x11_compositor *c, int width, int height)
335{
336 static const char name[] = "Wayland Compositor";
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500337 static const char class[] = "wayland-1\0Wayland Compositor";
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400338 struct x11_output *output;
339 xcb_dri2_dri2_buffer_t *buffers;
340 xcb_dri2_get_buffers_reply_t *reply;
341 xcb_dri2_get_buffers_cookie_t cookie;
342 xcb_screen_iterator_t iter;
343 xcb_rectangle_t rectangle;
344 struct wm_normal_hints normal_hints;
345 unsigned int attachments[] =
346 { XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT};
347 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
348 uint32_t values[2] = {
349 XCB_EVENT_MASK_KEY_PRESS |
350 XCB_EVENT_MASK_KEY_RELEASE |
351 XCB_EVENT_MASK_BUTTON_PRESS |
352 XCB_EVENT_MASK_BUTTON_RELEASE |
353 XCB_EVENT_MASK_POINTER_MOTION |
354 XCB_EVENT_MASK_EXPOSURE |
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400355 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
356 XCB_EVENT_MASK_ENTER_WINDOW |
357 XCB_EVENT_MASK_LEAVE_WINDOW,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400358 0
359 };
360
361 EGLint attribs[] = {
362 EGL_WIDTH, 0,
363 EGL_HEIGHT, 0,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400364 EGL_DRM_BUFFER_STRIDE_MESA, 0,
365 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400366 EGL_NONE
367 };
368
369 output = malloc(sizeof *output);
370 if (output == NULL)
371 return -1;
372
373 memset(output, 0, sizeof *output);
374 wlsc_output_init(&output->base, &c->base, 0, 0, width, height);
375
376 values[1] = c->null_cursor;
377 output->window = xcb_generate_id(c->conn);
378 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
379 xcb_create_window(c->conn,
380 XCB_COPY_FROM_PARENT,
381 output->window,
382 iter.data->root,
383 0, 0,
384 width, height,
385 0,
386 XCB_WINDOW_CLASS_INPUT_OUTPUT,
387 iter.data->root_visual,
388 mask, values);
389
390 /* Don't resize me. */
391 memset(&normal_hints, 0, sizeof normal_hints);
392 normal_hints.flags =
393 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
394 normal_hints.min_width = width;
395 normal_hints.min_height = height;
396 normal_hints.max_width = width;
397 normal_hints.max_height = height;
398 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
399 c->atom.wm_normal_hints,
400 c->atom.wm_size_hints, 32,
401 sizeof normal_hints / 4,
402 (uint8_t *) &normal_hints);
403
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400404 /* Set window name. Don't bother with non-EWMH WMs. */
405 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
406 c->atom.net_wm_name, c->atom.utf8_string, 8,
407 strlen(name), name);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500408 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
409 c->atom.wm_class, c->atom.string, 8,
410 sizeof class, class);
411
412 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400413
414 rectangle.x = 0;
415 rectangle.y = 0;
416 rectangle.width = width;
417 rectangle.height = height;
418 output->region = xcb_generate_id(c->conn);
419 xcb_xfixes_create_region(c->conn, output->region, 1, &rectangle);
420
421 xcb_dri2_create_drawable (c->conn, output->window);
422
423 x11_output_set_wm_protocols(output);
424
425 cookie = xcb_dri2_get_buffers_unchecked (c->conn,
426 output->window,
427 1, 1, attachments);
428 reply = xcb_dri2_get_buffers_reply (c->conn, cookie, NULL);
429 if (reply == NULL)
430 return -1;
431 buffers = xcb_dri2_get_buffers_buffers (reply);
432 if (buffers == NULL)
433 return -1;
434
435 if (reply->count != 1) {
436 fprintf(stderr,
437 "got wrong number of buffers (%d)\n", reply->count);
438 return -1;
439 }
440
441 attribs[1] = reply->width;
442 attribs[3] = reply->height;
443 attribs[5] = buffers[0].pitch / 4;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400444 output->image =
Kristian Høgsberg175e6ce2011-01-06 15:45:19 -0500445 eglCreateImageKHR(c->base.display,
446 EGL_NO_CONTEXT,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400447 EGL_DRM_BUFFER_MESA,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400448 (EGLClientBuffer) buffers[0].name,
449 attribs);
Kristian Høgsberg8f2e6772010-07-29 14:48:13 -0400450 free(reply);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400451
452 glGenRenderbuffers(1, &output->rbo);
453 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo);
454
455 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
456 output->image);
457
458 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
459 GL_COLOR_ATTACHMENT0,
460 GL_RENDERBUFFER,
461 output->rbo);
462
463 wl_list_insert(c->base.output_list.prev, &output->base.link);
464
465 return 0;
466}
467
468static void
469idle_repaint(void *data)
470{
471 struct x11_output *output = data;
472 struct x11_compositor *c =
473 (struct x11_compositor *) output->base.compositor;
474 xcb_xfixes_region_t region;
475 xcb_dri2_copy_region_cookie_t cookie;
476
477 if (output->damage_count <= ARRAY_SIZE(output->damage)) {
478 region = xcb_generate_id(c->conn);
479 xcb_xfixes_create_region(c->conn, region,
480 output->damage_count, output->damage);
481 } else {
482 region = output->region;
483 }
484
485 cookie = xcb_dri2_copy_region_unchecked(c->conn,
486 output->window,
487 region,
488 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
489 XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT);
490
491 if (region != output->region)
492 xcb_xfixes_destroy_region(c->conn, region);
493
494 free(xcb_dri2_copy_region_reply(c->conn, cookie, NULL));
495 output->damage_count = 0;
496}
497
498static struct x11_output *
499x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
500{
501 struct x11_output *output;
502
503 wl_list_for_each(output, &c->base.output_list, base.link) {
504 if (output->window == window)
505 return output;
506 }
507
508 return NULL;
509}
510
511static void
512x11_compositor_handle_event(int fd, uint32_t mask, void *data)
513{
514 struct x11_compositor *c = data;
515 struct x11_output *output;
516 xcb_generic_event_t *event;
517 struct wl_event_loop *loop;
518 xcb_client_message_event_t *client_message;
519 xcb_motion_notify_event_t *motion_notify;
520 xcb_key_press_event_t *key_press;
521 xcb_button_press_event_t *button_press;
522 xcb_expose_event_t *expose;
523 xcb_rectangle_t *r;
524 xcb_atom_t atom;
525
526 loop = wl_display_get_event_loop(c->base.wl_display);
527 while (event = xcb_poll_for_event (c->conn), event != NULL) {
528 switch (event->response_type & ~0x80) {
529
530 case XCB_KEY_PRESS:
531 key_press = (xcb_key_press_event_t *) event;
532 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400533 key_press->time, key_press->detail - 8, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400534 break;
535 case XCB_KEY_RELEASE:
536 key_press = (xcb_key_press_event_t *) event;
537 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400538 key_press->time, key_press->detail - 8, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400539 break;
540 case XCB_BUTTON_PRESS:
541 button_press = (xcb_button_press_event_t *) event;
542 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400543 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400544 button_press->detail + BTN_LEFT - 1, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400545 break;
546 case XCB_BUTTON_RELEASE:
547 button_press = (xcb_button_press_event_t *) event;
548 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400549 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400550 button_press->detail + BTN_LEFT - 1, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400551 break;
552
553 case XCB_MOTION_NOTIFY:
554 motion_notify = (xcb_motion_notify_event_t *) event;
555 notify_motion(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400556 motion_notify->time,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400557 motion_notify->event_x,
558 motion_notify->event_y);
559 break;
560
561 case XCB_EXPOSE:
562 expose = (xcb_expose_event_t *) event;
563 output = x11_compositor_find_output(c, expose->window);
564 if (output->damage_count == 0)
565 wl_event_loop_add_idle(loop,
566 idle_repaint, output);
567
568 r = &output->damage[output->damage_count++];
569 if (output->damage_count > 16)
570 break;
571 r->x = expose->x;
572 r->y = expose->y;
573 r->width = expose->width;
574 r->height = expose->height;
575 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400576
577 case XCB_ENTER_NOTIFY:
578 c->base.focus = 1;
579 wlsc_compositor_schedule_repaint(&c->base);
580 break;
581
582 case XCB_LEAVE_NOTIFY:
583 c->base.focus = 0;
584 wlsc_compositor_schedule_repaint(&c->base);
585 break;
586
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400587 case XCB_CLIENT_MESSAGE:
588 client_message = (xcb_client_message_event_t *) event;
589 atom = client_message->data.data32[0];
590 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500591 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400592 break;
593 default:
594
595 break;
596 }
597
598 free (event);
599 }
600}
601
602#define F(field) offsetof(struct x11_compositor, field)
603
604static void
605x11_compositor_get_resources(struct x11_compositor *c)
606{
607 static const struct { const char *name; int offset; } atoms[] = {
608 { "WM_PROTOCOLS", F(atom.wm_protocols) },
609 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
610 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
611 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500612 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400613 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500614 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400615 { "UTF8_STRING", F(atom.utf8_string) },
616 };
617
618 xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
619 xcb_intern_atom_reply_t *reply;
620 xcb_pixmap_t pixmap;
621 xcb_gc_t gc;
622 int i;
623 uint8_t data[] = { 0, 0, 0, 0 };
624
625 for (i = 0; i < ARRAY_SIZE(atoms); i++)
626 cookies[i] = xcb_intern_atom (c->conn, 0,
627 strlen(atoms[i].name),
628 atoms[i].name);
629
630 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
631 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
632 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
633 free(reply);
634 }
635
636 pixmap = xcb_generate_id(c->conn);
637 gc = xcb_generate_id(c->conn);
638 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
639 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
640 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
641 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
642 c->null_cursor = xcb_generate_id(c->conn);
643 xcb_create_cursor (c->conn, c->null_cursor,
644 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
645 xcb_free_gc(c->conn, gc);
646 xcb_free_pixmap(c->conn, pixmap);
647}
648
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400649static int
650x11_authenticate(struct wlsc_compositor *c, uint32_t id)
651{
652 return dri2_authenticate((struct x11_compositor *) c, id);
653}
654
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500655static void
656x11_destroy(struct wlsc_compositor *ec)
657{
658 free(ec);
659}
660
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400661struct wlsc_compositor *
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400662x11_compositor_create(struct wl_display *display, int width, int height)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400663{
664 struct x11_compositor *c;
665 struct wl_event_loop *loop;
666 xcb_screen_iterator_t s;
667
668 c = malloc(sizeof *c);
669 if (c == NULL)
670 return NULL;
671
672 memset(c, 0, sizeof *c);
673 c->conn = xcb_connect(0, 0);
674
675 if (xcb_connection_has_error(c->conn))
676 return NULL;
677
678 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
679 c->screen = s.data;
680
681 x11_compositor_get_resources(c);
682
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400683 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +0200684 if (x11_compositor_init_egl(c) < 0)
Darxus55973f22010-11-22 21:24:39 -0500685 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400686
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500687 c->base.destroy = x11_destroy;
688 c->base.authenticate = x11_authenticate;
689 c->base.present = x11_compositor_present;
690 c->base.create_buffer = wlsc_drm_buffer_create;
691
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400692 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400693 if (wlsc_compositor_init(&c->base, display) < 0)
694 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400695
Darxus55973f22010-11-22 21:24:39 -0500696 if (x11_compositor_create_output(c, width, height) < 0)
697 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400698
Darxus55973f22010-11-22 21:24:39 -0500699 if (x11_input_create(c) < 0)
700 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400701
702 loop = wl_display_get_event_loop(c->base.wl_display);
703
704 c->xcb_source =
705 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
706 WL_EVENT_READABLE,
707 x11_compositor_handle_event, c);
708
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400709 return &c->base;
710}