blob: 108955f22c0aa03c5a83662f30dfc9861b38d01b [file] [log] [blame]
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001/*
2 * Copyright © 2008-2010 Kristian Høgsberg
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +080019#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040023#include <stddef.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <errno.h>
30#include <sys/time.h>
Kristian Høgsbergf9112b22010-06-14 12:53:43 -040031#include <linux/input.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040032
33#include <xcb/xcb.h>
Benjamin Franzke3b288af2011-02-20 19:58:42 +010034#include <X11/Xlib.h>
35#include <X11/Xlib-xcb.h>
36
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040037#include <GLES2/gl2.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040038#include <EGL/egl.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040039
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040040#include "compositor.h"
41
42#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
43
44struct x11_compositor {
45 struct wlsc_compositor base;
46
Benjamin Franzke3b288af2011-02-20 19:58:42 +010047 Display *dpy;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040048 xcb_connection_t *conn;
49 xcb_screen_t *screen;
50 xcb_cursor_t null_cursor;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -050051 struct wl_array keys;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040052 struct wl_event_source *xcb_source;
53 struct {
54 xcb_atom_t wm_protocols;
55 xcb_atom_t wm_normal_hints;
56 xcb_atom_t wm_size_hints;
57 xcb_atom_t wm_delete_window;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050058 xcb_atom_t wm_class;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040059 xcb_atom_t net_wm_name;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050060 xcb_atom_t net_wm_icon;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050061 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040062 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050063 xcb_atom_t cardinal;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040064 } atom;
65};
66
67struct x11_output {
68 struct wlsc_output base;
69
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040070 xcb_window_t window;
Benjamin Franzke84290d02011-03-02 10:07:59 +010071 EGLSurface egl_surface;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040072};
73
74struct x11_input {
75 struct wlsc_input_device base;
76};
77
78
Darxus55973f22010-11-22 21:24:39 -050079static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040080x11_input_create(struct x11_compositor *c)
81{
82 struct x11_input *input;
83
84 input = malloc(sizeof *input);
85 if (input == NULL)
Darxus55973f22010-11-22 21:24:39 -050086 return -1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040087
88 memset(input, 0, sizeof *input);
89 wlsc_input_device_init(&input->base, &c->base);
90
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -050091 c->base.input_device = &input->base.input_device;
Darxus55973f22010-11-22 21:24:39 -050092
93 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040094}
95
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040096static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040097x11_compositor_init_egl(struct x11_compositor *c)
98{
Kristian Høgsberg379b6782010-07-28 22:52:28 -040099 EGLint major, minor;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100100 EGLint n;
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400101 const char *extensions;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100102 EGLint config_attribs[] = {
103 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
104 EGL_RED_SIZE, 1,
105 EGL_GREEN_SIZE, 1,
106 EGL_BLUE_SIZE, 1,
107 EGL_DEPTH_SIZE, 1,
108 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
109 EGL_NONE
110 };
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400111 static const EGLint context_attribs[] = {
112 EGL_CONTEXT_CLIENT_VERSION, 2,
113 EGL_NONE
114 };
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400115
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100116 c->base.display = eglGetDisplay(c->dpy);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400117 if (c->base.display == NULL) {
118 fprintf(stderr, "failed to create display\n");
119 return -1;
120 }
121
122 if (!eglInitialize(c->base.display, &major, &minor)) {
123 fprintf(stderr, "failed to initialize display\n");
124 return -1;
125 }
126
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400127 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
128 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
129 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400130 return -1;
131 }
132
Darxus55973f22010-11-22 21:24:39 -0500133 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
134 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
135 return -1;
136 }
Benjamin Franzke84290d02011-03-02 10:07:59 +0100137 if (!eglChooseConfig(c->base.display, config_attribs,
138 &c->base.config, 1, &n) || n == 0) {
139 fprintf(stderr, "failed to choose config: %d\n", n);
140 return -1;
141 }
Darxus55973f22010-11-22 21:24:39 -0500142
Benjamin Franzke84290d02011-03-02 10:07:59 +0100143 c->base.context = eglCreateContext(c->base.display, c->base.config,
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400144 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400145 if (c->base.context == NULL) {
146 fprintf(stderr, "failed to create context\n");
147 return -1;
148 }
149
150 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
151 EGL_NO_SURFACE, c->base.context)) {
152 fprintf(stderr, "failed to make context current\n");
153 return -1;
154 }
155
156 return 0;
157}
158
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100159static int
160x11_output_prepare_render(struct wlsc_output *output_base)
161{
162 struct x11_output *output = (struct x11_output *) output_base;
163 struct wlsc_compositor *ec = output->base.compositor;
164
165 if (!eglMakeCurrent(ec->display, output->egl_surface,
166 output->egl_surface, ec->context)) {
167 fprintf(stderr, "failed to make current\n");
168 return -1;
169 }
170
171 return 0;
172}
173
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100174static int
175x11_output_present(struct wlsc_output *output_base)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400176{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100177 struct x11_output *output = (struct x11_output *) output_base;
178 struct wlsc_compositor *ec = output->base.compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400179 struct timeval tv;
180 uint32_t msec;
181
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100182 if (x11_output_prepare_render(&output->base))
183 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100184
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100185 eglSwapBuffers(ec->display, output->egl_surface);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400186
187 gettimeofday(&tv, NULL);
188 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100189 wlsc_output_finish_frame(&output->base, msec);
190
191 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400192}
193
194static void
195x11_output_set_wm_protocols(struct x11_output *output)
196{
197 xcb_atom_t list[1];
198 struct x11_compositor *c =
199 (struct x11_compositor *) output->base.compositor;
200
201 list[0] = c->atom.wm_delete_window;
202 xcb_change_property (c->conn,
203 XCB_PROP_MODE_REPLACE,
204 output->window,
205 c->atom.wm_protocols,
206 XCB_ATOM_ATOM,
207 32,
208 ARRAY_SIZE(list),
209 list);
210}
211
212struct wm_normal_hints {
213 uint32_t flags;
214 uint32_t pad[4];
215 int32_t min_width, min_height;
216 int32_t max_width, max_height;
217 int32_t width_inc, height_inc;
218 int32_t min_aspect_x, min_aspect_y;
219 int32_t max_aspect_x, max_aspect_y;
220 int32_t base_width, base_height;
221 int32_t win_gravity;
222};
223
224#define WM_NORMAL_HINTS_MIN_SIZE 16
225#define WM_NORMAL_HINTS_MAX_SIZE 32
226
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500227static void
228x11_output_set_icon(struct x11_compositor *c, struct x11_output *output,
229 const char *filename, int width, int height)
230{
231 uint32_t *icon, *pixels;
232
233 pixels = wlsc_load_image(filename, width, height);
234 if (!pixels)
235 return;
236 icon = malloc(width * height * 4 + 8);
237 if (!icon) {
238 free(pixels);
239 return;
240 }
241
242 icon[0] = width;
243 icon[1] = height;
244 memcpy(icon + 2, pixels, width * height * 4);
245 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
246 c->atom.net_wm_icon, c->atom.cardinal, 32,
247 width * height + 2, icon);
248 free(icon);
249 free(pixels);
250}
251
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400252static int
253x11_compositor_create_output(struct x11_compositor *c, int width, int height)
254{
255 static const char name[] = "Wayland Compositor";
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500256 static const char class[] = "wayland-1\0Wayland Compositor";
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400257 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400258 xcb_screen_iterator_t iter;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400259 struct wm_normal_hints normal_hints;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400260 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
261 uint32_t values[2] = {
262 XCB_EVENT_MASK_KEY_PRESS |
263 XCB_EVENT_MASK_KEY_RELEASE |
264 XCB_EVENT_MASK_BUTTON_PRESS |
265 XCB_EVENT_MASK_BUTTON_RELEASE |
266 XCB_EVENT_MASK_POINTER_MOTION |
267 XCB_EVENT_MASK_EXPOSURE |
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400268 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
269 XCB_EVENT_MASK_ENTER_WINDOW |
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500270 XCB_EVENT_MASK_LEAVE_WINDOW |
271 XCB_EVENT_MASK_KEYMAP_STATE |
272 XCB_EVENT_MASK_FOCUS_CHANGE,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400273 0
274 };
275
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400276 output = malloc(sizeof *output);
277 if (output == NULL)
278 return -1;
279
280 memset(output, 0, sizeof *output);
Benjamin Franzke84290d02011-03-02 10:07:59 +0100281 wlsc_output_init(&output->base, &c->base, 0, 0, width, height,
282 WL_OUTPUT_FLIPPED);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400283
284 values[1] = c->null_cursor;
285 output->window = xcb_generate_id(c->conn);
286 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
287 xcb_create_window(c->conn,
288 XCB_COPY_FROM_PARENT,
289 output->window,
290 iter.data->root,
291 0, 0,
292 width, height,
293 0,
294 XCB_WINDOW_CLASS_INPUT_OUTPUT,
295 iter.data->root_visual,
296 mask, values);
297
298 /* Don't resize me. */
299 memset(&normal_hints, 0, sizeof normal_hints);
300 normal_hints.flags =
301 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
302 normal_hints.min_width = width;
303 normal_hints.min_height = height;
304 normal_hints.max_width = width;
305 normal_hints.max_height = height;
306 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
307 c->atom.wm_normal_hints,
308 c->atom.wm_size_hints, 32,
309 sizeof normal_hints / 4,
310 (uint8_t *) &normal_hints);
311
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400312 /* Set window name. Don't bother with non-EWMH WMs. */
313 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
314 c->atom.net_wm_name, c->atom.utf8_string, 8,
315 strlen(name), name);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500316 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
317 c->atom.wm_class, c->atom.string, 8,
318 sizeof class, class);
319
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500320 x11_output_set_icon(c, output,
321 DATADIR "/wayland/wayland.png", 128, 128);
322
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500323 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400324
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400325 x11_output_set_wm_protocols(output);
326
Benjamin Franzke84290d02011-03-02 10:07:59 +0100327 output->egl_surface =
328 eglCreateWindowSurface(c->base.display, c->base.config,
329 output->window, NULL);
330 if (!output->egl_surface) {
331 fprintf(stderr, "failed to create window surface\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400332 return -1;
333 }
Benjamin Franzke84290d02011-03-02 10:07:59 +0100334 if (!eglMakeCurrent(c->base.display, output->egl_surface,
335 output->egl_surface, c->base.context)) {
336 fprintf(stderr, "failed to make surface current\n");
337 return -1;
338 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400339
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100340 output->base.prepare_render = x11_output_prepare_render;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100341 output->base.present = x11_output_present;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100342
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400343 wl_list_insert(c->base.output_list.prev, &output->base.link);
344
345 return 0;
346}
347
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400348static struct x11_output *
349x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
350{
351 struct x11_output *output;
352
353 wl_list_for_each(output, &c->base.output_list, base.link) {
354 if (output->window == window)
355 return output;
356 }
357
358 return NULL;
359}
360
361static void
362x11_compositor_handle_event(int fd, uint32_t mask, void *data)
363{
364 struct x11_compositor *c = data;
365 struct x11_output *output;
366 xcb_generic_event_t *event;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400367 xcb_client_message_event_t *client_message;
368 xcb_motion_notify_event_t *motion_notify;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500369 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400370 xcb_key_press_event_t *key_press, *prev_release;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400371 xcb_button_press_event_t *button_press;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500372 xcb_keymap_notify_event_t *keymap_notify;
373 xcb_focus_in_event_t *focus_in;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400374 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500375 uint32_t *k;
376 int i, set;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400377
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400378 prev_release = NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400379 while (event = xcb_poll_for_event (c->conn), event != NULL) {
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400380 key_press = (xcb_key_press_event_t *) event;
381 if (prev_release &&
382 (event->response_type & ~0x80) == XCB_KEY_PRESS &&
383 prev_release->time == key_press->time) {
384 free(prev_release);
385 free(event);
386 prev_release = NULL;
387 continue;
388 } else if (prev_release) {
389 notify_key(c->base.input_device,
390 prev_release->time,
391 prev_release->detail - 8, 0);
392 free(prev_release);
393 prev_release = NULL;
394 }
395
396
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400397 switch (event->response_type & ~0x80) {
398
399 case XCB_KEY_PRESS:
400 key_press = (xcb_key_press_event_t *) event;
401 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400402 key_press->time, key_press->detail - 8, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400403 break;
404 case XCB_KEY_RELEASE:
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400405 prev_release = (xcb_key_press_event_t *) event;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400406 break;
407 case XCB_BUTTON_PRESS:
408 button_press = (xcb_button_press_event_t *) event;
409 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400410 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400411 button_press->detail + BTN_LEFT - 1, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400412 break;
413 case XCB_BUTTON_RELEASE:
414 button_press = (xcb_button_press_event_t *) event;
415 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400416 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400417 button_press->detail + BTN_LEFT - 1, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400418 break;
419
420 case XCB_MOTION_NOTIFY:
421 motion_notify = (xcb_motion_notify_event_t *) event;
422 notify_motion(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400423 motion_notify->time,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400424 motion_notify->event_x,
425 motion_notify->event_y);
426 break;
427
428 case XCB_EXPOSE:
Benjamin Franzke84290d02011-03-02 10:07:59 +0100429 /* FIXME: schedule output repaint */
430 /* output = x11_compositor_find_output(c, expose->window); */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400431
Benjamin Franzke84290d02011-03-02 10:07:59 +0100432 wlsc_compositor_schedule_repaint(&c->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400433 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400434
435 case XCB_ENTER_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500436 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500437 if (enter_notify->state >= Button1Mask)
438 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500439 output = x11_compositor_find_output(c, enter_notify->event);
440 notify_pointer_focus(c->base.input_device,
441 enter_notify->time,
442 &output->base,
443 enter_notify->event_x,
444 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400445 break;
446
447 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500448 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500449 if (enter_notify->state >= Button1Mask)
450 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500451 notify_pointer_focus(c->base.input_device,
452 enter_notify->time,
453 NULL,
454 enter_notify->event_x,
455 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400456 break;
457
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400458 case XCB_CLIENT_MESSAGE:
459 client_message = (xcb_client_message_event_t *) event;
460 atom = client_message->data.data32[0];
461 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500462 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400463 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400464
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500465 case XCB_FOCUS_IN:
466 focus_in = (xcb_focus_in_event_t *) event;
467 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
468 break;
469
470 output = x11_compositor_find_output(c, focus_in->event);
471 notify_keyboard_focus(c->base.input_device,
472 get_time(),
473 &output->base, &c->keys);
474 break;
475
476 case XCB_FOCUS_OUT:
477 focus_in = (xcb_focus_in_event_t *) event;
478 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
479 break;
480 notify_keyboard_focus(c->base.input_device,
481 get_time(), NULL, NULL);
482 break;
483
484 case XCB_KEYMAP_NOTIFY:
485 keymap_notify = (xcb_keymap_notify_event_t *) event;
486 c->keys.size = 0;
487 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
488 set = keymap_notify->keys[i >> 3] &
489 (1 << (i & 7));
490 if (set) {
491 k = wl_array_add(&c->keys, sizeof *k);
492 *k = i;
493 }
494 }
495 break;
496 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400497 break;
498 }
499
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400500 if ((xcb_generic_event_t *) prev_release != event)
501 free (event);
502 }
503
504 if (prev_release) {
505 key_press = (xcb_key_press_event_t *) prev_release;
506 notify_key(c->base.input_device,
507 prev_release->time, prev_release->detail - 8, 0);
508 free(prev_release);
509 prev_release = NULL;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500510 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400511}
512
513#define F(field) offsetof(struct x11_compositor, field)
514
515static void
516x11_compositor_get_resources(struct x11_compositor *c)
517{
518 static const struct { const char *name; int offset; } atoms[] = {
519 { "WM_PROTOCOLS", F(atom.wm_protocols) },
520 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
521 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
522 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500523 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400524 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500525 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500526 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400527 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500528 { "CARDINAL", F(atom.cardinal) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400529 };
530
531 xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
532 xcb_intern_atom_reply_t *reply;
533 xcb_pixmap_t pixmap;
534 xcb_gc_t gc;
535 int i;
536 uint8_t data[] = { 0, 0, 0, 0 };
537
538 for (i = 0; i < ARRAY_SIZE(atoms); i++)
539 cookies[i] = xcb_intern_atom (c->conn, 0,
540 strlen(atoms[i].name),
541 atoms[i].name);
542
543 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
544 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
545 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
546 free(reply);
547 }
548
549 pixmap = xcb_generate_id(c->conn);
550 gc = xcb_generate_id(c->conn);
551 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
552 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
553 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
554 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
555 c->null_cursor = xcb_generate_id(c->conn);
556 xcb_create_cursor (c->conn, c->null_cursor,
557 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
558 xcb_free_gc(c->conn, gc);
559 xcb_free_pixmap(c->conn, pixmap);
560}
561
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500562static void
563x11_destroy(struct wlsc_compositor *ec)
564{
565 free(ec);
566}
567
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400568struct wlsc_compositor *
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400569x11_compositor_create(struct wl_display *display, int width, int height)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400570{
571 struct x11_compositor *c;
572 struct wl_event_loop *loop;
573 xcb_screen_iterator_t s;
574
575 c = malloc(sizeof *c);
576 if (c == NULL)
577 return NULL;
578
579 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100580
581 c->dpy = XOpenDisplay(NULL);
Cyril Brulebois20798292011-04-06 18:05:40 +0200582
583 if (c->dpy == NULL)
584 return NULL;
585
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100586 c->conn = XGetXCBConnection(c->dpy);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400587
588 if (xcb_connection_has_error(c->conn))
589 return NULL;
590
591 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
592 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500593 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400594
595 x11_compositor_get_resources(c);
596
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400597 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +0200598 if (x11_compositor_init_egl(c) < 0)
Darxus55973f22010-11-22 21:24:39 -0500599 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400600
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500601 c->base.destroy = x11_destroy;
Benjamin Franzkec649a922011-03-02 11:56:04 +0100602 c->base.create_buffer = wlsc_shm_buffer_create;
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500603
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400604 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400605 if (wlsc_compositor_init(&c->base, display) < 0)
606 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400607
Darxus55973f22010-11-22 21:24:39 -0500608 if (x11_compositor_create_output(c, width, height) < 0)
609 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400610
Darxus55973f22010-11-22 21:24:39 -0500611 if (x11_input_create(c) < 0)
612 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400613
614 loop = wl_display_get_event_loop(c->base.wl_display);
615
616 c->xcb_source =
617 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
618 WL_EVENT_READABLE,
619 x11_compositor_handle_event, c);
620
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400621 return &c->base;
622}