blob: 5631f69685415070d1537a3fe0386355127c745c [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
159static void
160x11_compositor_present(struct wlsc_compositor *base)
161{
162 struct x11_compositor *c = (struct x11_compositor *) base;
163 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400164 struct timeval tv;
165 uint32_t msec;
166
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400167 wl_list_for_each(output, &c->base.output_list, base.link) {
Benjamin Franzke84290d02011-03-02 10:07:59 +0100168 if (!eglMakeCurrent(c->base.display, output->egl_surface,
169 output->egl_surface, c->base.context)) {
170 fprintf(stderr, "failed to make current\n");
171 continue;
172 }
173 eglSwapBuffers(c->base.display, output->egl_surface);
Kristian Høgsberg8f66a572011-01-07 08:38:56 -0500174 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400175
176 gettimeofday(&tv, NULL);
177 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
178 wlsc_compositor_finish_frame(&c->base, msec);
179}
180
181static void
182x11_output_set_wm_protocols(struct x11_output *output)
183{
184 xcb_atom_t list[1];
185 struct x11_compositor *c =
186 (struct x11_compositor *) output->base.compositor;
187
188 list[0] = c->atom.wm_delete_window;
189 xcb_change_property (c->conn,
190 XCB_PROP_MODE_REPLACE,
191 output->window,
192 c->atom.wm_protocols,
193 XCB_ATOM_ATOM,
194 32,
195 ARRAY_SIZE(list),
196 list);
197}
198
199struct wm_normal_hints {
200 uint32_t flags;
201 uint32_t pad[4];
202 int32_t min_width, min_height;
203 int32_t max_width, max_height;
204 int32_t width_inc, height_inc;
205 int32_t min_aspect_x, min_aspect_y;
206 int32_t max_aspect_x, max_aspect_y;
207 int32_t base_width, base_height;
208 int32_t win_gravity;
209};
210
211#define WM_NORMAL_HINTS_MIN_SIZE 16
212#define WM_NORMAL_HINTS_MAX_SIZE 32
213
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500214static void
215x11_output_set_icon(struct x11_compositor *c, struct x11_output *output,
216 const char *filename, int width, int height)
217{
218 uint32_t *icon, *pixels;
219
220 pixels = wlsc_load_image(filename, width, height);
221 if (!pixels)
222 return;
223 icon = malloc(width * height * 4 + 8);
224 if (!icon) {
225 free(pixels);
226 return;
227 }
228
229 icon[0] = width;
230 icon[1] = height;
231 memcpy(icon + 2, pixels, width * height * 4);
232 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
233 c->atom.net_wm_icon, c->atom.cardinal, 32,
234 width * height + 2, icon);
235 free(icon);
236 free(pixels);
237}
238
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400239static int
240x11_compositor_create_output(struct x11_compositor *c, int width, int height)
241{
242 static const char name[] = "Wayland Compositor";
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500243 static const char class[] = "wayland-1\0Wayland Compositor";
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400244 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400245 xcb_screen_iterator_t iter;
246 xcb_rectangle_t rectangle;
247 struct wm_normal_hints normal_hints;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400248 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
249 uint32_t values[2] = {
250 XCB_EVENT_MASK_KEY_PRESS |
251 XCB_EVENT_MASK_KEY_RELEASE |
252 XCB_EVENT_MASK_BUTTON_PRESS |
253 XCB_EVENT_MASK_BUTTON_RELEASE |
254 XCB_EVENT_MASK_POINTER_MOTION |
255 XCB_EVENT_MASK_EXPOSURE |
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400256 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
257 XCB_EVENT_MASK_ENTER_WINDOW |
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500258 XCB_EVENT_MASK_LEAVE_WINDOW |
259 XCB_EVENT_MASK_KEYMAP_STATE |
260 XCB_EVENT_MASK_FOCUS_CHANGE,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400261 0
262 };
263
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400264 output = malloc(sizeof *output);
265 if (output == NULL)
266 return -1;
267
268 memset(output, 0, sizeof *output);
Benjamin Franzke84290d02011-03-02 10:07:59 +0100269 wlsc_output_init(&output->base, &c->base, 0, 0, width, height,
270 WL_OUTPUT_FLIPPED);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400271
272 values[1] = c->null_cursor;
273 output->window = xcb_generate_id(c->conn);
274 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
275 xcb_create_window(c->conn,
276 XCB_COPY_FROM_PARENT,
277 output->window,
278 iter.data->root,
279 0, 0,
280 width, height,
281 0,
282 XCB_WINDOW_CLASS_INPUT_OUTPUT,
283 iter.data->root_visual,
284 mask, values);
285
286 /* Don't resize me. */
287 memset(&normal_hints, 0, sizeof normal_hints);
288 normal_hints.flags =
289 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
290 normal_hints.min_width = width;
291 normal_hints.min_height = height;
292 normal_hints.max_width = width;
293 normal_hints.max_height = height;
294 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
295 c->atom.wm_normal_hints,
296 c->atom.wm_size_hints, 32,
297 sizeof normal_hints / 4,
298 (uint8_t *) &normal_hints);
299
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400300 /* Set window name. Don't bother with non-EWMH WMs. */
301 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
302 c->atom.net_wm_name, c->atom.utf8_string, 8,
303 strlen(name), name);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500304 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
305 c->atom.wm_class, c->atom.string, 8,
306 sizeof class, class);
307
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500308 x11_output_set_icon(c, output,
309 DATADIR "/wayland/wayland.png", 128, 128);
310
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500311 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400312
313 rectangle.x = 0;
314 rectangle.y = 0;
315 rectangle.width = width;
316 rectangle.height = height;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400317
318 x11_output_set_wm_protocols(output);
319
Benjamin Franzke84290d02011-03-02 10:07:59 +0100320 output->egl_surface =
321 eglCreateWindowSurface(c->base.display, c->base.config,
322 output->window, NULL);
323 if (!output->egl_surface) {
324 fprintf(stderr, "failed to create window surface\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400325 return -1;
326 }
Benjamin Franzke84290d02011-03-02 10:07:59 +0100327 if (!eglMakeCurrent(c->base.display, output->egl_surface,
328 output->egl_surface, c->base.context)) {
329 fprintf(stderr, "failed to make surface current\n");
330 return -1;
331 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400332
333 wl_list_insert(c->base.output_list.prev, &output->base.link);
334
335 return 0;
336}
337
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400338static struct x11_output *
339x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
340{
341 struct x11_output *output;
342
343 wl_list_for_each(output, &c->base.output_list, base.link) {
344 if (output->window == window)
345 return output;
346 }
347
348 return NULL;
349}
350
351static void
352x11_compositor_handle_event(int fd, uint32_t mask, void *data)
353{
354 struct x11_compositor *c = data;
355 struct x11_output *output;
356 xcb_generic_event_t *event;
357 struct wl_event_loop *loop;
358 xcb_client_message_event_t *client_message;
359 xcb_motion_notify_event_t *motion_notify;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500360 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400361 xcb_key_press_event_t *key_press;
362 xcb_button_press_event_t *button_press;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500363 xcb_keymap_notify_event_t *keymap_notify;
364 xcb_focus_in_event_t *focus_in;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400365 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400366 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500367 uint32_t *k;
368 int i, set;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400369
370 loop = wl_display_get_event_loop(c->base.wl_display);
371 while (event = xcb_poll_for_event (c->conn), event != NULL) {
372 switch (event->response_type & ~0x80) {
373
374 case XCB_KEY_PRESS:
375 key_press = (xcb_key_press_event_t *) event;
376 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400377 key_press->time, key_press->detail - 8, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400378 break;
379 case XCB_KEY_RELEASE:
380 key_press = (xcb_key_press_event_t *) event;
381 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400382 key_press->time, key_press->detail - 8, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400383 break;
384 case XCB_BUTTON_PRESS:
385 button_press = (xcb_button_press_event_t *) event;
386 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400387 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400388 button_press->detail + BTN_LEFT - 1, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400389 break;
390 case XCB_BUTTON_RELEASE:
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, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400395 break;
396
397 case XCB_MOTION_NOTIFY:
398 motion_notify = (xcb_motion_notify_event_t *) event;
399 notify_motion(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400400 motion_notify->time,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400401 motion_notify->event_x,
402 motion_notify->event_y);
403 break;
404
405 case XCB_EXPOSE:
406 expose = (xcb_expose_event_t *) event;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100407
408 /* FIXME: schedule output repaint */
409 /* output = x11_compositor_find_output(c, expose->window); */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400410
Benjamin Franzke84290d02011-03-02 10:07:59 +0100411 wlsc_compositor_schedule_repaint(&c->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400412 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400413
414 case XCB_ENTER_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500415 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500416 if (enter_notify->state >= Button1Mask)
417 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500418 output = x11_compositor_find_output(c, enter_notify->event);
419 notify_pointer_focus(c->base.input_device,
420 enter_notify->time,
421 &output->base,
422 enter_notify->event_x,
423 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400424 break;
425
426 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500427 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500428 if (enter_notify->state >= Button1Mask)
429 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500430 notify_pointer_focus(c->base.input_device,
431 enter_notify->time,
432 NULL,
433 enter_notify->event_x,
434 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400435 break;
436
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400437 case XCB_CLIENT_MESSAGE:
438 client_message = (xcb_client_message_event_t *) event;
439 atom = client_message->data.data32[0];
440 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500441 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400442 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400443
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500444 case XCB_FOCUS_IN:
445 focus_in = (xcb_focus_in_event_t *) event;
446 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
447 break;
448
449 output = x11_compositor_find_output(c, focus_in->event);
450 notify_keyboard_focus(c->base.input_device,
451 get_time(),
452 &output->base, &c->keys);
453 break;
454
455 case XCB_FOCUS_OUT:
456 focus_in = (xcb_focus_in_event_t *) event;
457 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
458 break;
459 notify_keyboard_focus(c->base.input_device,
460 get_time(), NULL, NULL);
461 break;
462
463 case XCB_KEYMAP_NOTIFY:
464 keymap_notify = (xcb_keymap_notify_event_t *) event;
465 c->keys.size = 0;
466 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
467 set = keymap_notify->keys[i >> 3] &
468 (1 << (i & 7));
469 if (set) {
470 k = wl_array_add(&c->keys, sizeof *k);
471 *k = i;
472 }
473 }
474 break;
475 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400476 break;
477 }
478
479 free (event);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500480 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400481}
482
483#define F(field) offsetof(struct x11_compositor, field)
484
485static void
486x11_compositor_get_resources(struct x11_compositor *c)
487{
488 static const struct { const char *name; int offset; } atoms[] = {
489 { "WM_PROTOCOLS", F(atom.wm_protocols) },
490 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
491 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
492 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500493 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400494 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500495 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500496 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400497 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500498 { "CARDINAL", F(atom.cardinal) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400499 };
500
501 xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
502 xcb_intern_atom_reply_t *reply;
503 xcb_pixmap_t pixmap;
504 xcb_gc_t gc;
505 int i;
506 uint8_t data[] = { 0, 0, 0, 0 };
507
508 for (i = 0; i < ARRAY_SIZE(atoms); i++)
509 cookies[i] = xcb_intern_atom (c->conn, 0,
510 strlen(atoms[i].name),
511 atoms[i].name);
512
513 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
514 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
515 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
516 free(reply);
517 }
518
519 pixmap = xcb_generate_id(c->conn);
520 gc = xcb_generate_id(c->conn);
521 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
522 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
523 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
524 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
525 c->null_cursor = xcb_generate_id(c->conn);
526 xcb_create_cursor (c->conn, c->null_cursor,
527 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
528 xcb_free_gc(c->conn, gc);
529 xcb_free_pixmap(c->conn, pixmap);
530}
531
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500532static void
533x11_destroy(struct wlsc_compositor *ec)
534{
535 free(ec);
536}
537
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400538struct wlsc_compositor *
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400539x11_compositor_create(struct wl_display *display, int width, int height)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400540{
541 struct x11_compositor *c;
542 struct wl_event_loop *loop;
543 xcb_screen_iterator_t s;
544
545 c = malloc(sizeof *c);
546 if (c == NULL)
547 return NULL;
548
549 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100550
551 c->dpy = XOpenDisplay(NULL);
552 c->conn = XGetXCBConnection(c->dpy);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400553
554 if (xcb_connection_has_error(c->conn))
555 return NULL;
556
557 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
558 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500559 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400560
561 x11_compositor_get_resources(c);
562
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400563 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +0200564 if (x11_compositor_init_egl(c) < 0)
Darxus55973f22010-11-22 21:24:39 -0500565 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400566
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500567 c->base.destroy = x11_destroy;
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500568 c->base.present = x11_compositor_present;
569 c->base.create_buffer = wlsc_drm_buffer_create;
570
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400571 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400572 if (wlsc_compositor_init(&c->base, display) < 0)
573 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400574
Darxus55973f22010-11-22 21:24:39 -0500575 if (x11_compositor_create_output(c, width, height) < 0)
576 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400577
Darxus55973f22010-11-22 21:24:39 -0500578 if (x11_input_create(c) < 0)
579 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400580
581 loop = wl_display_get_event_loop(c->base.wl_display);
582
583 c->xcb_source =
584 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
585 WL_EVENT_READABLE,
586 x11_compositor_handle_event, c);
587
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400588 return &c->base;
589}