blob: ba593c14ceb70b7271f6f2bb8b5f4319b54a1932 [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
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400174static void
175x11_compositor_present(struct wlsc_compositor *base)
176{
177 struct x11_compositor *c = (struct x11_compositor *) base;
178 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400179 struct timeval tv;
180 uint32_t msec;
181
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400182 wl_list_for_each(output, &c->base.output_list, base.link) {
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100183 if (x11_output_prepare_render(&output->base))
Benjamin Franzke84290d02011-03-02 10:07:59 +0100184 continue;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100185
Benjamin Franzke84290d02011-03-02 10:07:59 +0100186 eglSwapBuffers(c->base.display, output->egl_surface);
Kristian Høgsberg8f66a572011-01-07 08:38:56 -0500187 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400188
189 gettimeofday(&tv, NULL);
190 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
191 wlsc_compositor_finish_frame(&c->base, msec);
192}
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;
341
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400342 wl_list_insert(c->base.output_list.prev, &output->base.link);
343
344 return 0;
345}
346
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400347static struct x11_output *
348x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
349{
350 struct x11_output *output;
351
352 wl_list_for_each(output, &c->base.output_list, base.link) {
353 if (output->window == window)
354 return output;
355 }
356
357 return NULL;
358}
359
360static void
361x11_compositor_handle_event(int fd, uint32_t mask, void *data)
362{
363 struct x11_compositor *c = data;
364 struct x11_output *output;
365 xcb_generic_event_t *event;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400366 xcb_client_message_event_t *client_message;
367 xcb_motion_notify_event_t *motion_notify;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500368 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400369 xcb_key_press_event_t *key_press;
370 xcb_button_press_event_t *button_press;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500371 xcb_keymap_notify_event_t *keymap_notify;
372 xcb_focus_in_event_t *focus_in;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400373 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500374 uint32_t *k;
375 int i, set;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400376
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400377 while (event = xcb_poll_for_event (c->conn), event != NULL) {
378 switch (event->response_type & ~0x80) {
379
380 case XCB_KEY_PRESS:
381 key_press = (xcb_key_press_event_t *) event;
382 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400383 key_press->time, key_press->detail - 8, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400384 break;
385 case XCB_KEY_RELEASE:
386 key_press = (xcb_key_press_event_t *) event;
387 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400388 key_press->time, key_press->detail - 8, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400389 break;
390 case XCB_BUTTON_PRESS:
391 button_press = (xcb_button_press_event_t *) event;
392 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400393 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400394 button_press->detail + BTN_LEFT - 1, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400395 break;
396 case XCB_BUTTON_RELEASE:
397 button_press = (xcb_button_press_event_t *) event;
398 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400399 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400400 button_press->detail + BTN_LEFT - 1, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400401 break;
402
403 case XCB_MOTION_NOTIFY:
404 motion_notify = (xcb_motion_notify_event_t *) event;
405 notify_motion(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400406 motion_notify->time,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400407 motion_notify->event_x,
408 motion_notify->event_y);
409 break;
410
411 case XCB_EXPOSE:
Benjamin Franzke84290d02011-03-02 10:07:59 +0100412 /* FIXME: schedule output repaint */
413 /* output = x11_compositor_find_output(c, expose->window); */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400414
Benjamin Franzke84290d02011-03-02 10:07:59 +0100415 wlsc_compositor_schedule_repaint(&c->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400416 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400417
418 case XCB_ENTER_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500419 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500420 if (enter_notify->state >= Button1Mask)
421 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500422 output = x11_compositor_find_output(c, enter_notify->event);
423 notify_pointer_focus(c->base.input_device,
424 enter_notify->time,
425 &output->base,
426 enter_notify->event_x,
427 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400428 break;
429
430 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500431 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500432 if (enter_notify->state >= Button1Mask)
433 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500434 notify_pointer_focus(c->base.input_device,
435 enter_notify->time,
436 NULL,
437 enter_notify->event_x,
438 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400439 break;
440
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400441 case XCB_CLIENT_MESSAGE:
442 client_message = (xcb_client_message_event_t *) event;
443 atom = client_message->data.data32[0];
444 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500445 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400446 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400447
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500448 case XCB_FOCUS_IN:
449 focus_in = (xcb_focus_in_event_t *) event;
450 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
451 break;
452
453 output = x11_compositor_find_output(c, focus_in->event);
454 notify_keyboard_focus(c->base.input_device,
455 get_time(),
456 &output->base, &c->keys);
457 break;
458
459 case XCB_FOCUS_OUT:
460 focus_in = (xcb_focus_in_event_t *) event;
461 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
462 break;
463 notify_keyboard_focus(c->base.input_device,
464 get_time(), NULL, NULL);
465 break;
466
467 case XCB_KEYMAP_NOTIFY:
468 keymap_notify = (xcb_keymap_notify_event_t *) event;
469 c->keys.size = 0;
470 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
471 set = keymap_notify->keys[i >> 3] &
472 (1 << (i & 7));
473 if (set) {
474 k = wl_array_add(&c->keys, sizeof *k);
475 *k = i;
476 }
477 }
478 break;
479 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400480 break;
481 }
482
483 free (event);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500484 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400485}
486
487#define F(field) offsetof(struct x11_compositor, field)
488
489static void
490x11_compositor_get_resources(struct x11_compositor *c)
491{
492 static const struct { const char *name; int offset; } atoms[] = {
493 { "WM_PROTOCOLS", F(atom.wm_protocols) },
494 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
495 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
496 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500497 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400498 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500499 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500500 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400501 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500502 { "CARDINAL", F(atom.cardinal) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400503 };
504
505 xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
506 xcb_intern_atom_reply_t *reply;
507 xcb_pixmap_t pixmap;
508 xcb_gc_t gc;
509 int i;
510 uint8_t data[] = { 0, 0, 0, 0 };
511
512 for (i = 0; i < ARRAY_SIZE(atoms); i++)
513 cookies[i] = xcb_intern_atom (c->conn, 0,
514 strlen(atoms[i].name),
515 atoms[i].name);
516
517 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
518 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
519 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
520 free(reply);
521 }
522
523 pixmap = xcb_generate_id(c->conn);
524 gc = xcb_generate_id(c->conn);
525 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
526 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
527 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
528 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
529 c->null_cursor = xcb_generate_id(c->conn);
530 xcb_create_cursor (c->conn, c->null_cursor,
531 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
532 xcb_free_gc(c->conn, gc);
533 xcb_free_pixmap(c->conn, pixmap);
534}
535
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500536static void
537x11_destroy(struct wlsc_compositor *ec)
538{
539 free(ec);
540}
541
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400542struct wlsc_compositor *
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400543x11_compositor_create(struct wl_display *display, int width, int height)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400544{
545 struct x11_compositor *c;
546 struct wl_event_loop *loop;
547 xcb_screen_iterator_t s;
548
549 c = malloc(sizeof *c);
550 if (c == NULL)
551 return NULL;
552
553 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100554
555 c->dpy = XOpenDisplay(NULL);
556 c->conn = XGetXCBConnection(c->dpy);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400557
558 if (xcb_connection_has_error(c->conn))
559 return NULL;
560
561 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
562 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500563 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400564
565 x11_compositor_get_resources(c);
566
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400567 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +0200568 if (x11_compositor_init_egl(c) < 0)
Darxus55973f22010-11-22 21:24:39 -0500569 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400570
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500571 c->base.destroy = x11_destroy;
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500572 c->base.present = x11_compositor_present;
Benjamin Franzkec649a922011-03-02 11:56:04 +0100573 c->base.create_buffer = wlsc_shm_buffer_create;
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500574
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400575 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400576 if (wlsc_compositor_init(&c->base, display) < 0)
577 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400578
Darxus55973f22010-11-22 21:24:39 -0500579 if (x11_compositor_create_output(c, width, height) < 0)
580 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400581
Darxus55973f22010-11-22 21:24:39 -0500582 if (x11_input_create(c) < 0)
583 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400584
585 loop = wl_display_get_event_loop(c->base.wl_display);
586
587 c->xcb_source =
588 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
589 WL_EVENT_READABLE,
590 x11_compositor_handle_event, c);
591
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400592 return &c->base;
593}