blob: 32225c37d9ae0d2488c0fad57889ac7789930f26 [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
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200201static int
202x11_output_set_cursor(struct wlsc_output *output_base,
203 struct wl_input_device *input)
204{
205 return -1;
206}
207
208
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400209static void
210x11_output_set_wm_protocols(struct x11_output *output)
211{
212 xcb_atom_t list[1];
213 struct x11_compositor *c =
214 (struct x11_compositor *) output->base.compositor;
215
216 list[0] = c->atom.wm_delete_window;
217 xcb_change_property (c->conn,
218 XCB_PROP_MODE_REPLACE,
219 output->window,
220 c->atom.wm_protocols,
221 XCB_ATOM_ATOM,
222 32,
223 ARRAY_SIZE(list),
224 list);
225}
226
227struct wm_normal_hints {
228 uint32_t flags;
229 uint32_t pad[4];
230 int32_t min_width, min_height;
231 int32_t max_width, max_height;
232 int32_t width_inc, height_inc;
233 int32_t min_aspect_x, min_aspect_y;
234 int32_t max_aspect_x, max_aspect_y;
235 int32_t base_width, base_height;
236 int32_t win_gravity;
237};
238
239#define WM_NORMAL_HINTS_MIN_SIZE 16
240#define WM_NORMAL_HINTS_MAX_SIZE 32
241
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500242static void
243x11_output_set_icon(struct x11_compositor *c, struct x11_output *output,
244 const char *filename, int width, int height)
245{
246 uint32_t *icon, *pixels;
247
248 pixels = wlsc_load_image(filename, width, height);
249 if (!pixels)
250 return;
251 icon = malloc(width * height * 4 + 8);
252 if (!icon) {
253 free(pixels);
254 return;
255 }
256
257 icon[0] = width;
258 icon[1] = height;
259 memcpy(icon + 2, pixels, width * height * 4);
260 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
261 c->atom.net_wm_icon, c->atom.cardinal, 32,
262 width * height + 2, icon);
263 free(icon);
264 free(pixels);
265}
266
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400267static int
268x11_compositor_create_output(struct x11_compositor *c, int width, int height)
269{
270 static const char name[] = "Wayland Compositor";
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500271 static const char class[] = "wayland-1\0Wayland Compositor";
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400272 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400273 xcb_screen_iterator_t iter;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400274 struct wm_normal_hints normal_hints;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400275 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
276 uint32_t values[2] = {
277 XCB_EVENT_MASK_KEY_PRESS |
278 XCB_EVENT_MASK_KEY_RELEASE |
279 XCB_EVENT_MASK_BUTTON_PRESS |
280 XCB_EVENT_MASK_BUTTON_RELEASE |
281 XCB_EVENT_MASK_POINTER_MOTION |
282 XCB_EVENT_MASK_EXPOSURE |
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400283 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
284 XCB_EVENT_MASK_ENTER_WINDOW |
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500285 XCB_EVENT_MASK_LEAVE_WINDOW |
286 XCB_EVENT_MASK_KEYMAP_STATE |
287 XCB_EVENT_MASK_FOCUS_CHANGE,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400288 0
289 };
290
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400291 output = malloc(sizeof *output);
292 if (output == NULL)
293 return -1;
294
295 memset(output, 0, sizeof *output);
Benjamin Franzke84290d02011-03-02 10:07:59 +0100296 wlsc_output_init(&output->base, &c->base, 0, 0, width, height,
297 WL_OUTPUT_FLIPPED);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400298
299 values[1] = c->null_cursor;
300 output->window = xcb_generate_id(c->conn);
301 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
302 xcb_create_window(c->conn,
303 XCB_COPY_FROM_PARENT,
304 output->window,
305 iter.data->root,
306 0, 0,
307 width, height,
308 0,
309 XCB_WINDOW_CLASS_INPUT_OUTPUT,
310 iter.data->root_visual,
311 mask, values);
312
313 /* Don't resize me. */
314 memset(&normal_hints, 0, sizeof normal_hints);
315 normal_hints.flags =
316 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
317 normal_hints.min_width = width;
318 normal_hints.min_height = height;
319 normal_hints.max_width = width;
320 normal_hints.max_height = height;
321 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
322 c->atom.wm_normal_hints,
323 c->atom.wm_size_hints, 32,
324 sizeof normal_hints / 4,
325 (uint8_t *) &normal_hints);
326
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400327 /* Set window name. Don't bother with non-EWMH WMs. */
328 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
329 c->atom.net_wm_name, c->atom.utf8_string, 8,
330 strlen(name), name);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500331 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
332 c->atom.wm_class, c->atom.string, 8,
333 sizeof class, class);
334
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500335 x11_output_set_icon(c, output,
336 DATADIR "/wayland/wayland.png", 128, 128);
337
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500338 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400339
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400340 x11_output_set_wm_protocols(output);
341
Benjamin Franzke84290d02011-03-02 10:07:59 +0100342 output->egl_surface =
343 eglCreateWindowSurface(c->base.display, c->base.config,
344 output->window, NULL);
345 if (!output->egl_surface) {
346 fprintf(stderr, "failed to create window surface\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400347 return -1;
348 }
Benjamin Franzke84290d02011-03-02 10:07:59 +0100349 if (!eglMakeCurrent(c->base.display, output->egl_surface,
350 output->egl_surface, c->base.context)) {
351 fprintf(stderr, "failed to make surface current\n");
352 return -1;
353 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400354
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100355 output->base.prepare_render = x11_output_prepare_render;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100356 output->base.present = x11_output_present;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200357 output->base.image_is_scanoutable = x11_output_image_is_scanoutable;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200358 output->base.set_hardware_cursor = x11_output_set_cursor;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100359
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400360 wl_list_insert(c->base.output_list.prev, &output->base.link);
361
362 return 0;
363}
364
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400365static struct x11_output *
366x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
367{
368 struct x11_output *output;
369
370 wl_list_for_each(output, &c->base.output_list, base.link) {
371 if (output->window == window)
372 return output;
373 }
374
375 return NULL;
376}
377
378static void
379x11_compositor_handle_event(int fd, uint32_t mask, void *data)
380{
381 struct x11_compositor *c = data;
382 struct x11_output *output;
383 xcb_generic_event_t *event;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400384 xcb_client_message_event_t *client_message;
385 xcb_motion_notify_event_t *motion_notify;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500386 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400387 xcb_key_press_event_t *key_press;
388 xcb_button_press_event_t *button_press;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500389 xcb_keymap_notify_event_t *keymap_notify;
390 xcb_focus_in_event_t *focus_in;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400391 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500392 uint32_t *k;
393 int i, set;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400394
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400395 while (event = xcb_poll_for_event (c->conn), event != NULL) {
396 switch (event->response_type & ~0x80) {
397
398 case XCB_KEY_PRESS:
399 key_press = (xcb_key_press_event_t *) event;
400 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400401 key_press->time, key_press->detail - 8, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400402 break;
403 case XCB_KEY_RELEASE:
404 key_press = (xcb_key_press_event_t *) event;
405 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400406 key_press->time, key_press->detail - 8, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400407 break;
408 case XCB_BUTTON_PRESS:
409 button_press = (xcb_button_press_event_t *) event;
410 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400411 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400412 button_press->detail + BTN_LEFT - 1, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400413 break;
414 case XCB_BUTTON_RELEASE:
415 button_press = (xcb_button_press_event_t *) event;
416 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400417 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400418 button_press->detail + BTN_LEFT - 1, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400419 break;
420
421 case XCB_MOTION_NOTIFY:
422 motion_notify = (xcb_motion_notify_event_t *) event;
423 notify_motion(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400424 motion_notify->time,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400425 motion_notify->event_x,
426 motion_notify->event_y);
427 break;
428
429 case XCB_EXPOSE:
Benjamin Franzke84290d02011-03-02 10:07:59 +0100430 /* FIXME: schedule output repaint */
431 /* output = x11_compositor_find_output(c, expose->window); */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400432
Benjamin Franzke84290d02011-03-02 10:07:59 +0100433 wlsc_compositor_schedule_repaint(&c->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400434 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400435
436 case XCB_ENTER_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500437 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500438 if (enter_notify->state >= Button1Mask)
439 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500440 output = x11_compositor_find_output(c, enter_notify->event);
441 notify_pointer_focus(c->base.input_device,
442 enter_notify->time,
443 &output->base,
444 enter_notify->event_x,
445 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400446 break;
447
448 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500449 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500450 if (enter_notify->state >= Button1Mask)
451 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500452 notify_pointer_focus(c->base.input_device,
453 enter_notify->time,
454 NULL,
455 enter_notify->event_x,
456 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400457 break;
458
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400459 case XCB_CLIENT_MESSAGE:
460 client_message = (xcb_client_message_event_t *) event;
461 atom = client_message->data.data32[0];
462 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500463 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400464 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400465
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500466 case XCB_FOCUS_IN:
467 focus_in = (xcb_focus_in_event_t *) event;
468 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
469 break;
470
471 output = x11_compositor_find_output(c, focus_in->event);
472 notify_keyboard_focus(c->base.input_device,
473 get_time(),
474 &output->base, &c->keys);
475 break;
476
477 case XCB_FOCUS_OUT:
478 focus_in = (xcb_focus_in_event_t *) event;
479 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
480 break;
481 notify_keyboard_focus(c->base.input_device,
482 get_time(), NULL, NULL);
483 break;
484
485 case XCB_KEYMAP_NOTIFY:
486 keymap_notify = (xcb_keymap_notify_event_t *) event;
487 c->keys.size = 0;
488 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
489 set = keymap_notify->keys[i >> 3] &
490 (1 << (i & 7));
491 if (set) {
492 k = wl_array_add(&c->keys, sizeof *k);
493 *k = i;
494 }
495 }
496 break;
497 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400498 break;
499 }
500
501 free (event);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500502 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400503}
504
505#define F(field) offsetof(struct x11_compositor, field)
506
507static void
508x11_compositor_get_resources(struct x11_compositor *c)
509{
510 static const struct { const char *name; int offset; } atoms[] = {
511 { "WM_PROTOCOLS", F(atom.wm_protocols) },
512 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
513 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
514 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500515 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400516 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500517 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500518 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400519 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500520 { "CARDINAL", F(atom.cardinal) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400521 };
522
523 xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
524 xcb_intern_atom_reply_t *reply;
525 xcb_pixmap_t pixmap;
526 xcb_gc_t gc;
527 int i;
528 uint8_t data[] = { 0, 0, 0, 0 };
529
530 for (i = 0; i < ARRAY_SIZE(atoms); i++)
531 cookies[i] = xcb_intern_atom (c->conn, 0,
532 strlen(atoms[i].name),
533 atoms[i].name);
534
535 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
536 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
537 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
538 free(reply);
539 }
540
541 pixmap = xcb_generate_id(c->conn);
542 gc = xcb_generate_id(c->conn);
543 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
544 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
545 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
546 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
547 c->null_cursor = xcb_generate_id(c->conn);
548 xcb_create_cursor (c->conn, c->null_cursor,
549 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
550 xcb_free_gc(c->conn, gc);
551 xcb_free_pixmap(c->conn, pixmap);
552}
553
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500554static void
555x11_destroy(struct wlsc_compositor *ec)
556{
557 free(ec);
558}
559
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400560struct wlsc_compositor *
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400561x11_compositor_create(struct wl_display *display, int width, int height)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400562{
563 struct x11_compositor *c;
564 struct wl_event_loop *loop;
565 xcb_screen_iterator_t s;
566
567 c = malloc(sizeof *c);
568 if (c == NULL)
569 return NULL;
570
571 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100572
573 c->dpy = XOpenDisplay(NULL);
Cyril Brulebois20798292011-04-06 18:05:40 +0200574
575 if (c->dpy == NULL)
576 return NULL;
577
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100578 c->conn = XGetXCBConnection(c->dpy);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400579
580 if (xcb_connection_has_error(c->conn))
581 return NULL;
582
583 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
584 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500585 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400586
587 x11_compositor_get_resources(c);
588
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400589 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +0200590 if (x11_compositor_init_egl(c) < 0)
Darxus55973f22010-11-22 21:24:39 -0500591 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400592
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500593 c->base.destroy = x11_destroy;
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500594
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400595 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400596 if (wlsc_compositor_init(&c->base, display) < 0)
597 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400598
Darxus55973f22010-11-22 21:24:39 -0500599 if (x11_compositor_create_output(c, width, height) < 0)
600 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400601
Darxus55973f22010-11-22 21:24:39 -0500602 if (x11_input_create(c) < 0)
603 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400604
605 loop = wl_display_get_event_loop(c->base.wl_display);
606
607 c->xcb_source =
608 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
609 WL_EVENT_READABLE,
610 x11_compositor_handle_event, c);
611
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400612 return &c->base;
613}