blob: e63d6be23349151d26b815f88e0d477f3a26c445 [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
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200194static int
195x11_output_image_is_scanoutable(struct wlsc_output *output_base,
196 EGLImageKHR image)
197{
198 return 0;
199}
200
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400201static void
202x11_output_set_wm_protocols(struct x11_output *output)
203{
204 xcb_atom_t list[1];
205 struct x11_compositor *c =
206 (struct x11_compositor *) output->base.compositor;
207
208 list[0] = c->atom.wm_delete_window;
209 xcb_change_property (c->conn,
210 XCB_PROP_MODE_REPLACE,
211 output->window,
212 c->atom.wm_protocols,
213 XCB_ATOM_ATOM,
214 32,
215 ARRAY_SIZE(list),
216 list);
217}
218
219struct wm_normal_hints {
220 uint32_t flags;
221 uint32_t pad[4];
222 int32_t min_width, min_height;
223 int32_t max_width, max_height;
224 int32_t width_inc, height_inc;
225 int32_t min_aspect_x, min_aspect_y;
226 int32_t max_aspect_x, max_aspect_y;
227 int32_t base_width, base_height;
228 int32_t win_gravity;
229};
230
231#define WM_NORMAL_HINTS_MIN_SIZE 16
232#define WM_NORMAL_HINTS_MAX_SIZE 32
233
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500234static void
235x11_output_set_icon(struct x11_compositor *c, struct x11_output *output,
236 const char *filename, int width, int height)
237{
238 uint32_t *icon, *pixels;
239
240 pixels = wlsc_load_image(filename, width, height);
241 if (!pixels)
242 return;
243 icon = malloc(width * height * 4 + 8);
244 if (!icon) {
245 free(pixels);
246 return;
247 }
248
249 icon[0] = width;
250 icon[1] = height;
251 memcpy(icon + 2, pixels, width * height * 4);
252 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
253 c->atom.net_wm_icon, c->atom.cardinal, 32,
254 width * height + 2, icon);
255 free(icon);
256 free(pixels);
257}
258
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400259static int
260x11_compositor_create_output(struct x11_compositor *c, int width, int height)
261{
262 static const char name[] = "Wayland Compositor";
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500263 static const char class[] = "wayland-1\0Wayland Compositor";
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400264 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400265 xcb_screen_iterator_t iter;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400266 struct wm_normal_hints normal_hints;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400267 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
268 uint32_t values[2] = {
269 XCB_EVENT_MASK_KEY_PRESS |
270 XCB_EVENT_MASK_KEY_RELEASE |
271 XCB_EVENT_MASK_BUTTON_PRESS |
272 XCB_EVENT_MASK_BUTTON_RELEASE |
273 XCB_EVENT_MASK_POINTER_MOTION |
274 XCB_EVENT_MASK_EXPOSURE |
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400275 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
276 XCB_EVENT_MASK_ENTER_WINDOW |
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500277 XCB_EVENT_MASK_LEAVE_WINDOW |
278 XCB_EVENT_MASK_KEYMAP_STATE |
279 XCB_EVENT_MASK_FOCUS_CHANGE,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400280 0
281 };
282
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400283 output = malloc(sizeof *output);
284 if (output == NULL)
285 return -1;
286
287 memset(output, 0, sizeof *output);
Benjamin Franzke84290d02011-03-02 10:07:59 +0100288 wlsc_output_init(&output->base, &c->base, 0, 0, width, height,
289 WL_OUTPUT_FLIPPED);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400290
291 values[1] = c->null_cursor;
292 output->window = xcb_generate_id(c->conn);
293 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
294 xcb_create_window(c->conn,
295 XCB_COPY_FROM_PARENT,
296 output->window,
297 iter.data->root,
298 0, 0,
299 width, height,
300 0,
301 XCB_WINDOW_CLASS_INPUT_OUTPUT,
302 iter.data->root_visual,
303 mask, values);
304
305 /* Don't resize me. */
306 memset(&normal_hints, 0, sizeof normal_hints);
307 normal_hints.flags =
308 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
309 normal_hints.min_width = width;
310 normal_hints.min_height = height;
311 normal_hints.max_width = width;
312 normal_hints.max_height = height;
313 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
314 c->atom.wm_normal_hints,
315 c->atom.wm_size_hints, 32,
316 sizeof normal_hints / 4,
317 (uint8_t *) &normal_hints);
318
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400319 /* Set window name. Don't bother with non-EWMH WMs. */
320 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
321 c->atom.net_wm_name, c->atom.utf8_string, 8,
322 strlen(name), name);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500323 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
324 c->atom.wm_class, c->atom.string, 8,
325 sizeof class, class);
326
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500327 x11_output_set_icon(c, output,
328 DATADIR "/wayland/wayland.png", 128, 128);
329
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500330 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400331
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400332 x11_output_set_wm_protocols(output);
333
Benjamin Franzke84290d02011-03-02 10:07:59 +0100334 output->egl_surface =
335 eglCreateWindowSurface(c->base.display, c->base.config,
336 output->window, NULL);
337 if (!output->egl_surface) {
338 fprintf(stderr, "failed to create window surface\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400339 return -1;
340 }
Benjamin Franzke84290d02011-03-02 10:07:59 +0100341 if (!eglMakeCurrent(c->base.display, output->egl_surface,
342 output->egl_surface, c->base.context)) {
343 fprintf(stderr, "failed to make surface current\n");
344 return -1;
345 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400346
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100347 output->base.prepare_render = x11_output_prepare_render;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100348 output->base.present = x11_output_present;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200349 output->base.image_is_scanoutable = x11_output_image_is_scanoutable;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100350
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400351 wl_list_insert(c->base.output_list.prev, &output->base.link);
352
353 return 0;
354}
355
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400356static struct x11_output *
357x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
358{
359 struct x11_output *output;
360
361 wl_list_for_each(output, &c->base.output_list, base.link) {
362 if (output->window == window)
363 return output;
364 }
365
366 return NULL;
367}
368
369static void
370x11_compositor_handle_event(int fd, uint32_t mask, void *data)
371{
372 struct x11_compositor *c = data;
373 struct x11_output *output;
374 xcb_generic_event_t *event;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400375 xcb_client_message_event_t *client_message;
376 xcb_motion_notify_event_t *motion_notify;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500377 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400378 xcb_key_press_event_t *key_press;
379 xcb_button_press_event_t *button_press;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500380 xcb_keymap_notify_event_t *keymap_notify;
381 xcb_focus_in_event_t *focus_in;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400382 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500383 uint32_t *k;
384 int i, set;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400385
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400386 while (event = xcb_poll_for_event (c->conn), event != NULL) {
387 switch (event->response_type & ~0x80) {
388
389 case XCB_KEY_PRESS:
390 key_press = (xcb_key_press_event_t *) event;
391 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400392 key_press->time, key_press->detail - 8, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400393 break;
394 case XCB_KEY_RELEASE:
395 key_press = (xcb_key_press_event_t *) event;
396 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400397 key_press->time, key_press->detail - 8, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400398 break;
399 case XCB_BUTTON_PRESS:
400 button_press = (xcb_button_press_event_t *) event;
401 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400402 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400403 button_press->detail + BTN_LEFT - 1, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400404 break;
405 case XCB_BUTTON_RELEASE:
406 button_press = (xcb_button_press_event_t *) event;
407 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400408 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400409 button_press->detail + BTN_LEFT - 1, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400410 break;
411
412 case XCB_MOTION_NOTIFY:
413 motion_notify = (xcb_motion_notify_event_t *) event;
414 notify_motion(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400415 motion_notify->time,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400416 motion_notify->event_x,
417 motion_notify->event_y);
418 break;
419
420 case XCB_EXPOSE:
Benjamin Franzke84290d02011-03-02 10:07:59 +0100421 /* FIXME: schedule output repaint */
422 /* output = x11_compositor_find_output(c, expose->window); */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400423
Benjamin Franzke84290d02011-03-02 10:07:59 +0100424 wlsc_compositor_schedule_repaint(&c->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400425 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400426
427 case XCB_ENTER_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500428 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500429 if (enter_notify->state >= Button1Mask)
430 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500431 output = x11_compositor_find_output(c, enter_notify->event);
432 notify_pointer_focus(c->base.input_device,
433 enter_notify->time,
434 &output->base,
435 enter_notify->event_x,
436 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400437 break;
438
439 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500440 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500441 if (enter_notify->state >= Button1Mask)
442 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500443 notify_pointer_focus(c->base.input_device,
444 enter_notify->time,
445 NULL,
446 enter_notify->event_x,
447 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400448 break;
449
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400450 case XCB_CLIENT_MESSAGE:
451 client_message = (xcb_client_message_event_t *) event;
452 atom = client_message->data.data32[0];
453 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500454 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400455 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400456
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500457 case XCB_FOCUS_IN:
458 focus_in = (xcb_focus_in_event_t *) event;
459 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
460 break;
461
462 output = x11_compositor_find_output(c, focus_in->event);
463 notify_keyboard_focus(c->base.input_device,
464 get_time(),
465 &output->base, &c->keys);
466 break;
467
468 case XCB_FOCUS_OUT:
469 focus_in = (xcb_focus_in_event_t *) event;
470 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
471 break;
472 notify_keyboard_focus(c->base.input_device,
473 get_time(), NULL, NULL);
474 break;
475
476 case XCB_KEYMAP_NOTIFY:
477 keymap_notify = (xcb_keymap_notify_event_t *) event;
478 c->keys.size = 0;
479 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
480 set = keymap_notify->keys[i >> 3] &
481 (1 << (i & 7));
482 if (set) {
483 k = wl_array_add(&c->keys, sizeof *k);
484 *k = i;
485 }
486 }
487 break;
488 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400489 break;
490 }
491
492 free (event);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500493 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400494}
495
496#define F(field) offsetof(struct x11_compositor, field)
497
498static void
499x11_compositor_get_resources(struct x11_compositor *c)
500{
501 static const struct { const char *name; int offset; } atoms[] = {
502 { "WM_PROTOCOLS", F(atom.wm_protocols) },
503 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
504 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
505 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500506 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400507 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500508 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500509 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400510 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500511 { "CARDINAL", F(atom.cardinal) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400512 };
513
514 xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
515 xcb_intern_atom_reply_t *reply;
516 xcb_pixmap_t pixmap;
517 xcb_gc_t gc;
518 int i;
519 uint8_t data[] = { 0, 0, 0, 0 };
520
521 for (i = 0; i < ARRAY_SIZE(atoms); i++)
522 cookies[i] = xcb_intern_atom (c->conn, 0,
523 strlen(atoms[i].name),
524 atoms[i].name);
525
526 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
527 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
528 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
529 free(reply);
530 }
531
532 pixmap = xcb_generate_id(c->conn);
533 gc = xcb_generate_id(c->conn);
534 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
535 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
536 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
537 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
538 c->null_cursor = xcb_generate_id(c->conn);
539 xcb_create_cursor (c->conn, c->null_cursor,
540 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
541 xcb_free_gc(c->conn, gc);
542 xcb_free_pixmap(c->conn, pixmap);
543}
544
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500545static void
546x11_destroy(struct wlsc_compositor *ec)
547{
548 free(ec);
549}
550
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400551struct wlsc_compositor *
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400552x11_compositor_create(struct wl_display *display, int width, int height)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400553{
554 struct x11_compositor *c;
555 struct wl_event_loop *loop;
556 xcb_screen_iterator_t s;
557
558 c = malloc(sizeof *c);
559 if (c == NULL)
560 return NULL;
561
562 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100563
564 c->dpy = XOpenDisplay(NULL);
Cyril Brulebois20798292011-04-06 18:05:40 +0200565
566 if (c->dpy == NULL)
567 return NULL;
568
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100569 c->conn = XGetXCBConnection(c->dpy);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400570
571 if (xcb_connection_has_error(c->conn))
572 return NULL;
573
574 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
575 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500576 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400577
578 x11_compositor_get_resources(c);
579
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400580 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +0200581 if (x11_compositor_init_egl(c) < 0)
Darxus55973f22010-11-22 21:24:39 -0500582 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400583
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500584 c->base.destroy = x11_destroy;
Benjamin Franzkec649a922011-03-02 11:56:04 +0100585 c->base.create_buffer = wlsc_shm_buffer_create;
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500586
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400587 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400588 if (wlsc_compositor_init(&c->base, display) < 0)
589 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400590
Darxus55973f22010-11-22 21:24:39 -0500591 if (x11_compositor_create_output(c, width, height) < 0)
592 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400593
Darxus55973f22010-11-22 21:24:39 -0500594 if (x11_input_create(c) < 0)
595 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400596
597 loop = wl_display_get_event_loop(c->base.wl_display);
598
599 c->xcb_source =
600 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
601 WL_EVENT_READABLE,
602 x11_compositor_handle_event, c);
603
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400604 return &c->base;
605}