blob: 9e8ce75114b85f00dd5e8a0114bbfde5cfb7799f [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øgsbergee724822011-04-21 14:51:44 -040051 xcb_generic_event_t *next_event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -050052 struct wl_array keys;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040053 struct wl_event_source *xcb_source;
54 struct {
55 xcb_atom_t wm_protocols;
56 xcb_atom_t wm_normal_hints;
57 xcb_atom_t wm_size_hints;
58 xcb_atom_t wm_delete_window;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050059 xcb_atom_t wm_class;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040060 xcb_atom_t net_wm_name;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050061 xcb_atom_t net_wm_icon;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050062 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040063 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050064 xcb_atom_t cardinal;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040065 } atom;
66};
67
68struct x11_output {
69 struct wlsc_output base;
70
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040071 xcb_window_t window;
Benjamin Franzke84290d02011-03-02 10:07:59 +010072 EGLSurface egl_surface;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040073};
74
75struct x11_input {
76 struct wlsc_input_device base;
77};
78
79
Darxus55973f22010-11-22 21:24:39 -050080static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040081x11_input_create(struct x11_compositor *c)
82{
83 struct x11_input *input;
84
85 input = malloc(sizeof *input);
86 if (input == NULL)
Darxus55973f22010-11-22 21:24:39 -050087 return -1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040088
89 memset(input, 0, sizeof *input);
90 wlsc_input_device_init(&input->base, &c->base);
91
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -050092 c->base.input_device = &input->base.input_device;
Darxus55973f22010-11-22 21:24:39 -050093
94 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040095}
96
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040097static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040098x11_compositor_init_egl(struct x11_compositor *c)
99{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400100 EGLint major, minor;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100101 EGLint n;
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400102 const char *extensions;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100103 EGLint config_attribs[] = {
104 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
105 EGL_RED_SIZE, 1,
106 EGL_GREEN_SIZE, 1,
107 EGL_BLUE_SIZE, 1,
108 EGL_DEPTH_SIZE, 1,
109 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
110 EGL_NONE
111 };
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400112 static const EGLint context_attribs[] = {
113 EGL_CONTEXT_CLIENT_VERSION, 2,
114 EGL_NONE
115 };
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400116
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100117 c->base.display = eglGetDisplay(c->dpy);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400118 if (c->base.display == NULL) {
119 fprintf(stderr, "failed to create display\n");
120 return -1;
121 }
122
123 if (!eglInitialize(c->base.display, &major, &minor)) {
124 fprintf(stderr, "failed to initialize display\n");
125 return -1;
126 }
127
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400128 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
129 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
130 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400131 return -1;
132 }
133
Darxus55973f22010-11-22 21:24:39 -0500134 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
135 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
136 return -1;
137 }
Benjamin Franzke84290d02011-03-02 10:07:59 +0100138 if (!eglChooseConfig(c->base.display, config_attribs,
139 &c->base.config, 1, &n) || n == 0) {
140 fprintf(stderr, "failed to choose config: %d\n", n);
141 return -1;
142 }
Darxus55973f22010-11-22 21:24:39 -0500143
Benjamin Franzke84290d02011-03-02 10:07:59 +0100144 c->base.context = eglCreateContext(c->base.display, c->base.config,
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400145 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400146 if (c->base.context == NULL) {
147 fprintf(stderr, "failed to create context\n");
148 return -1;
149 }
150
151 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
152 EGL_NO_SURFACE, c->base.context)) {
153 fprintf(stderr, "failed to make context current\n");
154 return -1;
155 }
156
157 return 0;
158}
159
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100160static int
161x11_output_prepare_render(struct wlsc_output *output_base)
162{
163 struct x11_output *output = (struct x11_output *) output_base;
164 struct wlsc_compositor *ec = output->base.compositor;
165
166 if (!eglMakeCurrent(ec->display, output->egl_surface,
167 output->egl_surface, ec->context)) {
168 fprintf(stderr, "failed to make current\n");
169 return -1;
170 }
171
172 return 0;
173}
174
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100175static int
176x11_output_present(struct wlsc_output *output_base)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400177{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100178 struct x11_output *output = (struct x11_output *) output_base;
179 struct wlsc_compositor *ec = output->base.compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400180 struct timeval tv;
181 uint32_t msec;
182
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100183 if (x11_output_prepare_render(&output->base))
184 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100185
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100186 eglSwapBuffers(ec->display, output->egl_surface);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400187
188 gettimeofday(&tv, NULL);
189 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100190 wlsc_output_finish_frame(&output->base, msec);
191
192 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400193}
194
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200195static int
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200196x11_output_prepare_scanout_surface(struct wlsc_output *output_base,
197 struct wlsc_surface *es)
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200198{
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200199 return -1;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200200}
201
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200202static int
203x11_output_set_cursor(struct wlsc_output *output_base,
204 struct wl_input_device *input)
205{
206 return -1;
207}
208
209
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400210static void
211x11_output_set_wm_protocols(struct x11_output *output)
212{
213 xcb_atom_t list[1];
214 struct x11_compositor *c =
215 (struct x11_compositor *) output->base.compositor;
216
217 list[0] = c->atom.wm_delete_window;
218 xcb_change_property (c->conn,
219 XCB_PROP_MODE_REPLACE,
220 output->window,
221 c->atom.wm_protocols,
222 XCB_ATOM_ATOM,
223 32,
224 ARRAY_SIZE(list),
225 list);
226}
227
228struct wm_normal_hints {
229 uint32_t flags;
230 uint32_t pad[4];
231 int32_t min_width, min_height;
232 int32_t max_width, max_height;
233 int32_t width_inc, height_inc;
234 int32_t min_aspect_x, min_aspect_y;
235 int32_t max_aspect_x, max_aspect_y;
236 int32_t base_width, base_height;
237 int32_t win_gravity;
238};
239
240#define WM_NORMAL_HINTS_MIN_SIZE 16
241#define WM_NORMAL_HINTS_MAX_SIZE 32
242
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500243static void
244x11_output_set_icon(struct x11_compositor *c, struct x11_output *output,
245 const char *filename, int width, int height)
246{
247 uint32_t *icon, *pixels;
248
249 pixels = wlsc_load_image(filename, width, height);
250 if (!pixels)
251 return;
252 icon = malloc(width * height * 4 + 8);
253 if (!icon) {
254 free(pixels);
255 return;
256 }
257
258 icon[0] = width;
259 icon[1] = height;
260 memcpy(icon + 2, pixels, width * height * 4);
261 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
262 c->atom.net_wm_icon, c->atom.cardinal, 32,
263 width * height + 2, icon);
264 free(icon);
265 free(pixels);
266}
267
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400268static int
269x11_compositor_create_output(struct x11_compositor *c, int width, int height)
270{
271 static const char name[] = "Wayland Compositor";
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500272 static const char class[] = "wayland-1\0Wayland Compositor";
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400273 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400274 xcb_screen_iterator_t iter;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400275 struct wm_normal_hints normal_hints;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400276 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
277 uint32_t values[2] = {
278 XCB_EVENT_MASK_KEY_PRESS |
279 XCB_EVENT_MASK_KEY_RELEASE |
280 XCB_EVENT_MASK_BUTTON_PRESS |
281 XCB_EVENT_MASK_BUTTON_RELEASE |
282 XCB_EVENT_MASK_POINTER_MOTION |
283 XCB_EVENT_MASK_EXPOSURE |
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400284 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
285 XCB_EVENT_MASK_ENTER_WINDOW |
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500286 XCB_EVENT_MASK_LEAVE_WINDOW |
287 XCB_EVENT_MASK_KEYMAP_STATE |
288 XCB_EVENT_MASK_FOCUS_CHANGE,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400289 0
290 };
291
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400292 output = malloc(sizeof *output);
293 if (output == NULL)
294 return -1;
295
296 memset(output, 0, sizeof *output);
Benjamin Franzke84290d02011-03-02 10:07:59 +0100297 wlsc_output_init(&output->base, &c->base, 0, 0, width, height,
298 WL_OUTPUT_FLIPPED);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400299
300 values[1] = c->null_cursor;
301 output->window = xcb_generate_id(c->conn);
302 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
303 xcb_create_window(c->conn,
304 XCB_COPY_FROM_PARENT,
305 output->window,
306 iter.data->root,
307 0, 0,
308 width, height,
309 0,
310 XCB_WINDOW_CLASS_INPUT_OUTPUT,
311 iter.data->root_visual,
312 mask, values);
313
314 /* Don't resize me. */
315 memset(&normal_hints, 0, sizeof normal_hints);
316 normal_hints.flags =
317 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
318 normal_hints.min_width = width;
319 normal_hints.min_height = height;
320 normal_hints.max_width = width;
321 normal_hints.max_height = height;
322 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
323 c->atom.wm_normal_hints,
324 c->atom.wm_size_hints, 32,
325 sizeof normal_hints / 4,
326 (uint8_t *) &normal_hints);
327
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400328 /* Set window name. Don't bother with non-EWMH WMs. */
329 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
330 c->atom.net_wm_name, c->atom.utf8_string, 8,
331 strlen(name), name);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500332 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
333 c->atom.wm_class, c->atom.string, 8,
334 sizeof class, class);
335
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500336 x11_output_set_icon(c, output,
337 DATADIR "/wayland/wayland.png", 128, 128);
338
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500339 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400340
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400341 x11_output_set_wm_protocols(output);
342
Benjamin Franzke84290d02011-03-02 10:07:59 +0100343 output->egl_surface =
344 eglCreateWindowSurface(c->base.display, c->base.config,
345 output->window, NULL);
346 if (!output->egl_surface) {
347 fprintf(stderr, "failed to create window surface\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400348 return -1;
349 }
Benjamin Franzke84290d02011-03-02 10:07:59 +0100350 if (!eglMakeCurrent(c->base.display, output->egl_surface,
351 output->egl_surface, c->base.context)) {
352 fprintf(stderr, "failed to make surface current\n");
353 return -1;
354 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400355
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100356 output->base.prepare_render = x11_output_prepare_render;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100357 output->base.present = x11_output_present;
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200358 output->base.prepare_scanout_surface =
359 x11_output_prepare_scanout_surface;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200360 output->base.set_hardware_cursor = x11_output_set_cursor;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100361
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400362 wl_list_insert(c->base.output_list.prev, &output->base.link);
363
364 return 0;
365}
366
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400367static struct x11_output *
368x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
369{
370 struct x11_output *output;
371
372 wl_list_for_each(output, &c->base.output_list, base.link) {
373 if (output->window == window)
374 return output;
375 }
376
377 return NULL;
378}
379
Kristian Høgsbergee724822011-04-21 14:51:44 -0400380static int
381x11_compositor_next_event(struct x11_compositor *c,
382 xcb_generic_event_t **event)
383{
384 if (c->next_event) {
385 *event = c->next_event;
386 c->next_event = NULL;
387 } else {
388 *event = xcb_poll_for_event (c->conn);
389 }
390
391 return *event != NULL;
392}
393
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400394static void
395x11_compositor_handle_event(int fd, uint32_t mask, void *data)
396{
397 struct x11_compositor *c = data;
398 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400399 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400400 xcb_client_message_event_t *client_message;
401 xcb_motion_notify_event_t *motion_notify;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500402 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400403 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400404 xcb_button_press_event_t *button_press;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500405 xcb_keymap_notify_event_t *keymap_notify;
406 xcb_focus_in_event_t *focus_in;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400407 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500408 uint32_t *k;
409 int i, set;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400410
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400411 prev = NULL;
Kristian Høgsbergee724822011-04-21 14:51:44 -0400412 while (x11_compositor_next_event(c, &event)) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400413 switch (prev ? prev->response_type & ~0x80 : 0x80) {
414 case XCB_KEY_RELEASE:
415 key_release = (xcb_key_press_event_t *) prev;
416 key_press = (xcb_key_press_event_t *) event;
417 if ((event->response_type & ~0x80) == XCB_KEY_PRESS &&
418 key_release->time == key_press->time) {
419 /* Don't deliver the held key release
420 * event or the new key press event. */
421 free(event);
422 free(prev);
423 prev = NULL;
424 continue;
425 } else {
426 /* Deliver the held key release now
427 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400428 * event below. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400429 notify_key(c->base.input_device,
430 key_release->time,
431 key_release->detail - 8, 0);
432 free(prev);
433 prev = NULL;
434 break;
435 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400436
437 case XCB_FOCUS_IN:
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400438 /* assert event is keymap_notify */
439 focus_in = (xcb_focus_in_event_t *) prev;
440 keymap_notify = (xcb_keymap_notify_event_t *) event;
441 c->keys.size = 0;
442 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
443 set = keymap_notify->keys[i >> 3] &
444 (1 << (i & 7));
445 if (set) {
446 k = wl_array_add(&c->keys, sizeof *k);
447 *k = i;
448 }
449 }
450
451 output = x11_compositor_find_output(c, focus_in->event);
452 notify_keyboard_focus(c->base.input_device,
453 get_time(),
454 &output->base, &c->keys);
455
456 free(prev);
457 prev = NULL;
458 break;
459
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400460 default:
461 /* No previous event held */
462 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400463 }
464
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400465 switch (event->response_type & ~0x80) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400466 case XCB_KEY_PRESS:
467 key_press = (xcb_key_press_event_t *) event;
468 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400469 key_press->time, key_press->detail - 8, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400470 break;
471 case XCB_KEY_RELEASE:
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400472 prev = event;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400473 break;
474 case XCB_BUTTON_PRESS:
475 button_press = (xcb_button_press_event_t *) event;
476 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400477 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400478 button_press->detail + BTN_LEFT - 1, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400479 break;
480 case XCB_BUTTON_RELEASE:
481 button_press = (xcb_button_press_event_t *) event;
482 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400483 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400484 button_press->detail + BTN_LEFT - 1, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400485 break;
486
487 case XCB_MOTION_NOTIFY:
488 motion_notify = (xcb_motion_notify_event_t *) event;
489 notify_motion(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400490 motion_notify->time,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400491 motion_notify->event_x,
492 motion_notify->event_y);
493 break;
494
495 case XCB_EXPOSE:
Benjamin Franzke84290d02011-03-02 10:07:59 +0100496 /* FIXME: schedule output repaint */
497 /* output = x11_compositor_find_output(c, expose->window); */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400498
Benjamin Franzke84290d02011-03-02 10:07:59 +0100499 wlsc_compositor_schedule_repaint(&c->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400500 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400501
502 case XCB_ENTER_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500503 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500504 if (enter_notify->state >= Button1Mask)
505 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500506 output = x11_compositor_find_output(c, enter_notify->event);
507 notify_pointer_focus(c->base.input_device,
508 enter_notify->time,
509 &output->base,
510 enter_notify->event_x,
511 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400512 break;
513
514 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500515 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500516 if (enter_notify->state >= Button1Mask)
517 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500518 notify_pointer_focus(c->base.input_device,
519 enter_notify->time,
520 NULL,
521 enter_notify->event_x,
522 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400523 break;
524
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400525 case XCB_CLIENT_MESSAGE:
526 client_message = (xcb_client_message_event_t *) event;
527 atom = client_message->data.data32[0];
528 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500529 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400530 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400531
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500532 case XCB_FOCUS_IN:
533 focus_in = (xcb_focus_in_event_t *) event;
534 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
535 break;
536
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400537 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500538 break;
539
540 case XCB_FOCUS_OUT:
541 focus_in = (xcb_focus_in_event_t *) event;
542 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
543 break;
544 notify_keyboard_focus(c->base.input_device,
545 get_time(), NULL, NULL);
546 break;
547
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500548 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400549 break;
550 }
551
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400552 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400553 free (event);
554 }
555
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400556 switch (prev ? prev->response_type & ~0x80 : 0x80) {
557 case XCB_KEY_RELEASE:
558 key_release = (xcb_key_press_event_t *) prev;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400559 notify_key(c->base.input_device,
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400560 key_release->time, key_release->detail - 8, 0);
561 free(prev);
562 prev = NULL;
563 break;
564 default:
565 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500566 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400567}
568
Kristian Høgsbergee724822011-04-21 14:51:44 -0400569static int
570x11_compositor_check_source(struct wl_event_source *source, void *data)
571{
572 struct x11_compositor *c = data;
573
574 /* Really? xcb doesn't have a "get queue length" function that
575 * doesn't pop an event? */
576 c->next_event = xcb_poll_for_queued_event(c->conn);
577
578 return c->next_event != NULL;
579}
580
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400581#define F(field) offsetof(struct x11_compositor, field)
582
583static void
584x11_compositor_get_resources(struct x11_compositor *c)
585{
586 static const struct { const char *name; int offset; } atoms[] = {
587 { "WM_PROTOCOLS", F(atom.wm_protocols) },
588 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
589 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
590 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500591 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400592 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500593 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500594 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400595 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500596 { "CARDINAL", F(atom.cardinal) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400597 };
598
599 xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
600 xcb_intern_atom_reply_t *reply;
601 xcb_pixmap_t pixmap;
602 xcb_gc_t gc;
603 int i;
604 uint8_t data[] = { 0, 0, 0, 0 };
605
606 for (i = 0; i < ARRAY_SIZE(atoms); i++)
607 cookies[i] = xcb_intern_atom (c->conn, 0,
608 strlen(atoms[i].name),
609 atoms[i].name);
610
611 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
612 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
613 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
614 free(reply);
615 }
616
617 pixmap = xcb_generate_id(c->conn);
618 gc = xcb_generate_id(c->conn);
619 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
620 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
621 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
622 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
623 c->null_cursor = xcb_generate_id(c->conn);
624 xcb_create_cursor (c->conn, c->null_cursor,
625 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
626 xcb_free_gc(c->conn, gc);
627 xcb_free_pixmap(c->conn, pixmap);
628}
629
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500630static void
631x11_destroy(struct wlsc_compositor *ec)
632{
633 free(ec);
634}
635
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400636struct wlsc_compositor *
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400637x11_compositor_create(struct wl_display *display, int width, int height)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400638{
639 struct x11_compositor *c;
640 struct wl_event_loop *loop;
641 xcb_screen_iterator_t s;
642
643 c = malloc(sizeof *c);
644 if (c == NULL)
645 return NULL;
646
647 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100648
649 c->dpy = XOpenDisplay(NULL);
Cyril Brulebois20798292011-04-06 18:05:40 +0200650 if (c->dpy == NULL)
651 return NULL;
652
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100653 c->conn = XGetXCBConnection(c->dpy);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +0100654 XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
655
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400656 if (xcb_connection_has_error(c->conn))
657 return NULL;
658
Kristian Høgsbergee724822011-04-21 14:51:44 -0400659 c->next_event = NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400660 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
661 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500662 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400663
664 x11_compositor_get_resources(c);
665
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400666 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +0200667 if (x11_compositor_init_egl(c) < 0)
Darxus55973f22010-11-22 21:24:39 -0500668 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400669
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500670 c->base.destroy = x11_destroy;
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500671
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400672 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400673 if (wlsc_compositor_init(&c->base, display) < 0)
674 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400675
Darxus55973f22010-11-22 21:24:39 -0500676 if (x11_compositor_create_output(c, width, height) < 0)
677 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400678
Darxus55973f22010-11-22 21:24:39 -0500679 if (x11_input_create(c) < 0)
680 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400681
682 loop = wl_display_get_event_loop(c->base.wl_display);
683
684 c->xcb_source =
685 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
686 WL_EVENT_READABLE,
687 x11_compositor_handle_event, c);
Kristian Høgsbergee724822011-04-21 14:51:44 -0400688 wl_event_source_check(c->xcb_source, x11_compositor_check_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400689
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400690 return &c->base;
691}