blob: 52d094bfdaf4b507965ad83da6b110b751c2a197 [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øgsberg83eeacb2011-06-18 04:20:54 -040061 xcb_atom_t net_wm_state;
62 xcb_atom_t net_wm_state_fullscreen;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050063 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040064 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050065 xcb_atom_t cardinal;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040066 } atom;
67};
68
69struct x11_output {
70 struct wlsc_output base;
71
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040072 xcb_window_t window;
Benjamin Franzke84290d02011-03-02 10:07:59 +010073 EGLSurface egl_surface;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040074};
75
76struct x11_input {
77 struct wlsc_input_device base;
78};
79
80
Darxus55973f22010-11-22 21:24:39 -050081static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040082x11_input_create(struct x11_compositor *c)
83{
84 struct x11_input *input;
85
86 input = malloc(sizeof *input);
87 if (input == NULL)
Darxus55973f22010-11-22 21:24:39 -050088 return -1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040089
90 memset(input, 0, sizeof *input);
91 wlsc_input_device_init(&input->base, &c->base);
92
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -050093 c->base.input_device = &input->base.input_device;
Darxus55973f22010-11-22 21:24:39 -050094
95 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040096}
97
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040098static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040099x11_compositor_init_egl(struct x11_compositor *c)
100{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400101 EGLint major, minor;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100102 EGLint n;
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400103 const char *extensions;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100104 EGLint config_attribs[] = {
105 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
106 EGL_RED_SIZE, 1,
107 EGL_GREEN_SIZE, 1,
108 EGL_BLUE_SIZE, 1,
109 EGL_DEPTH_SIZE, 1,
110 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
111 EGL_NONE
112 };
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400113 static const EGLint context_attribs[] = {
114 EGL_CONTEXT_CLIENT_VERSION, 2,
115 EGL_NONE
116 };
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400117
Egbert Eiche7b8d902011-05-10 20:00:19 +0000118 setenv("EGL_PLATFORM", "x11", 1);
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100119 c->base.display = eglGetDisplay(c->dpy);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400120 if (c->base.display == NULL) {
121 fprintf(stderr, "failed to create display\n");
122 return -1;
123 }
124
125 if (!eglInitialize(c->base.display, &major, &minor)) {
126 fprintf(stderr, "failed to initialize display\n");
127 return -1;
128 }
129
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400130 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
131 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
132 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400133 return -1;
134 }
135
Darxus55973f22010-11-22 21:24:39 -0500136 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
137 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
138 return -1;
139 }
Benjamin Franzke84290d02011-03-02 10:07:59 +0100140 if (!eglChooseConfig(c->base.display, config_attribs,
141 &c->base.config, 1, &n) || n == 0) {
142 fprintf(stderr, "failed to choose config: %d\n", n);
143 return -1;
144 }
Darxus55973f22010-11-22 21:24:39 -0500145
Benjamin Franzke84290d02011-03-02 10:07:59 +0100146 c->base.context = eglCreateContext(c->base.display, c->base.config,
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400147 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400148 if (c->base.context == NULL) {
149 fprintf(stderr, "failed to create context\n");
150 return -1;
151 }
152
153 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
154 EGL_NO_SURFACE, c->base.context)) {
155 fprintf(stderr, "failed to make context current\n");
156 return -1;
157 }
158
159 return 0;
160}
161
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100162static int
163x11_output_prepare_render(struct wlsc_output *output_base)
164{
165 struct x11_output *output = (struct x11_output *) output_base;
166 struct wlsc_compositor *ec = output->base.compositor;
167
168 if (!eglMakeCurrent(ec->display, output->egl_surface,
169 output->egl_surface, ec->context)) {
170 fprintf(stderr, "failed to make current\n");
171 return -1;
172 }
173
174 return 0;
175}
176
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100177static int
178x11_output_present(struct wlsc_output *output_base)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400179{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100180 struct x11_output *output = (struct x11_output *) output_base;
181 struct wlsc_compositor *ec = output->base.compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400182 struct timeval tv;
183 uint32_t msec;
184
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100185 if (x11_output_prepare_render(&output->base))
186 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100187
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100188 eglSwapBuffers(ec->display, output->egl_surface);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400189
190 gettimeofday(&tv, NULL);
191 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100192 wlsc_output_finish_frame(&output->base, msec);
193
194 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400195}
196
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200197static int
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200198x11_output_prepare_scanout_surface(struct wlsc_output *output_base,
199 struct wlsc_surface *es)
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200200{
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200201 return -1;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200202}
203
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200204static int
205x11_output_set_cursor(struct wlsc_output *output_base,
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400206 struct wlsc_input_device *input)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200207{
208 return -1;
209}
210
211
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400212static void
213x11_output_set_wm_protocols(struct x11_output *output)
214{
215 xcb_atom_t list[1];
216 struct x11_compositor *c =
217 (struct x11_compositor *) output->base.compositor;
218
219 list[0] = c->atom.wm_delete_window;
220 xcb_change_property (c->conn,
221 XCB_PROP_MODE_REPLACE,
222 output->window,
223 c->atom.wm_protocols,
224 XCB_ATOM_ATOM,
225 32,
226 ARRAY_SIZE(list),
227 list);
228}
229
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400230static void
231x11_output_change_state(struct x11_output *output, int add, xcb_atom_t state)
232{
233 xcb_client_message_event_t event;
234 struct x11_compositor *c =
235 (struct x11_compositor *) output->base.compositor;
236 xcb_screen_iterator_t iter;
237
238#define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
239#define _NET_WM_STATE_ADD 1 /* add/set property */
240#define _NET_WM_STATE_TOGGLE 2 /* toggle property */
241
242 memset(&event, 0, sizeof event);
243 event.response_type = XCB_CLIENT_MESSAGE;
244 event.format = 32;
245 event.window = output->window;
246 event.type = c->atom.net_wm_state;
247
248 event.data.data32[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
249 event.data.data32[1] = state;
250 event.data.data32[2] = 0;
251 event.data.data32[3] = 0;
252 event.data.data32[4] = 0;
253
254 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
255 xcb_send_event(c->conn, 0, iter.data->root,
256 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
257 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
258 (void *) &event);
259}
260
261
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400262struct wm_normal_hints {
263 uint32_t flags;
264 uint32_t pad[4];
265 int32_t min_width, min_height;
266 int32_t max_width, max_height;
267 int32_t width_inc, height_inc;
268 int32_t min_aspect_x, min_aspect_y;
269 int32_t max_aspect_x, max_aspect_y;
270 int32_t base_width, base_height;
271 int32_t win_gravity;
272};
273
274#define WM_NORMAL_HINTS_MIN_SIZE 16
275#define WM_NORMAL_HINTS_MAX_SIZE 32
276
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500277static void
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400278x11_output_set_icon(struct x11_compositor *c,
279 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500280{
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400281 uint32_t *icon, *pixels, stride;
282 int32_t width, height;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500283
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400284 pixels = wlsc_load_image(filename, &width, &height, &stride);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500285 if (!pixels)
286 return;
287 icon = malloc(width * height * 4 + 8);
288 if (!icon) {
289 free(pixels);
290 return;
291 }
292
293 icon[0] = width;
294 icon[1] = height;
295 memcpy(icon + 2, pixels, width * height * 4);
296 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
297 c->atom.net_wm_icon, c->atom.cardinal, 32,
298 width * height + 2, icon);
299 free(icon);
300 free(pixels);
301}
302
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400303static int
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400304x11_compositor_create_output(struct x11_compositor *c,
305 int width, int height, int fullscreen)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400306{
307 static const char name[] = "Wayland Compositor";
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500308 static const char class[] = "wayland-1\0Wayland Compositor";
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400309 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400310 xcb_screen_iterator_t iter;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400311 struct wm_normal_hints normal_hints;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400312 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
313 uint32_t values[2] = {
314 XCB_EVENT_MASK_KEY_PRESS |
315 XCB_EVENT_MASK_KEY_RELEASE |
316 XCB_EVENT_MASK_BUTTON_PRESS |
317 XCB_EVENT_MASK_BUTTON_RELEASE |
318 XCB_EVENT_MASK_POINTER_MOTION |
319 XCB_EVENT_MASK_EXPOSURE |
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400320 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
321 XCB_EVENT_MASK_ENTER_WINDOW |
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500322 XCB_EVENT_MASK_LEAVE_WINDOW |
323 XCB_EVENT_MASK_KEYMAP_STATE |
324 XCB_EVENT_MASK_FOCUS_CHANGE,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400325 0
326 };
327
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400328 output = malloc(sizeof *output);
329 if (output == NULL)
330 return -1;
331
332 memset(output, 0, sizeof *output);
Benjamin Franzke84290d02011-03-02 10:07:59 +0100333 wlsc_output_init(&output->base, &c->base, 0, 0, width, height,
334 WL_OUTPUT_FLIPPED);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400335
336 values[1] = c->null_cursor;
337 output->window = xcb_generate_id(c->conn);
338 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
339 xcb_create_window(c->conn,
340 XCB_COPY_FROM_PARENT,
341 output->window,
342 iter.data->root,
343 0, 0,
344 width, height,
345 0,
346 XCB_WINDOW_CLASS_INPUT_OUTPUT,
347 iter.data->root_visual,
348 mask, values);
349
350 /* Don't resize me. */
351 memset(&normal_hints, 0, sizeof normal_hints);
352 normal_hints.flags =
353 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
354 normal_hints.min_width = width;
355 normal_hints.min_height = height;
356 normal_hints.max_width = width;
357 normal_hints.max_height = height;
358 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
359 c->atom.wm_normal_hints,
360 c->atom.wm_size_hints, 32,
361 sizeof normal_hints / 4,
362 (uint8_t *) &normal_hints);
363
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400364 /* Set window name. Don't bother with non-EWMH WMs. */
365 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
366 c->atom.net_wm_name, c->atom.utf8_string, 8,
367 strlen(name), name);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500368 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
369 c->atom.wm_class, c->atom.string, 8,
370 sizeof class, class);
371
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400372 x11_output_set_icon(c, output, DATADIR "/wayland/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500373
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500374 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400375
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400376 x11_output_set_wm_protocols(output);
377
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400378 if (fullscreen)
379 x11_output_change_state(output, 1,
380 c->atom.net_wm_state_fullscreen);
381
Benjamin Franzke84290d02011-03-02 10:07:59 +0100382 output->egl_surface =
383 eglCreateWindowSurface(c->base.display, c->base.config,
384 output->window, NULL);
385 if (!output->egl_surface) {
386 fprintf(stderr, "failed to create window surface\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400387 return -1;
388 }
Benjamin Franzke84290d02011-03-02 10:07:59 +0100389 if (!eglMakeCurrent(c->base.display, output->egl_surface,
390 output->egl_surface, c->base.context)) {
391 fprintf(stderr, "failed to make surface current\n");
392 return -1;
393 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400394
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100395 output->base.prepare_render = x11_output_prepare_render;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100396 output->base.present = x11_output_present;
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200397 output->base.prepare_scanout_surface =
398 x11_output_prepare_scanout_surface;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200399 output->base.set_hardware_cursor = x11_output_set_cursor;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100400
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400401 wl_list_insert(c->base.output_list.prev, &output->base.link);
402
403 return 0;
404}
405
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400406static struct x11_output *
407x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
408{
409 struct x11_output *output;
410
411 wl_list_for_each(output, &c->base.output_list, base.link) {
412 if (output->window == window)
413 return output;
414 }
415
416 return NULL;
417}
418
Kristian Høgsbergee724822011-04-21 14:51:44 -0400419static int
420x11_compositor_next_event(struct x11_compositor *c,
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400421 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -0400422{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400423 if (mask & WL_EVENT_READABLE) {
424 *event = xcb_poll_for_event(c->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -0400425 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400426#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400427 *event = xcb_poll_for_queued_event(c->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400428#else
429 *event = xcb_poll_for_event(c->conn);
430#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -0400431 }
432
433 return *event != NULL;
434}
435
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400436static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400437x11_compositor_handle_event(int fd, uint32_t mask, void *data)
438{
439 struct x11_compositor *c = data;
440 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400441 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400442 xcb_client_message_event_t *client_message;
443 xcb_motion_notify_event_t *motion_notify;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500444 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400445 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400446 xcb_button_press_event_t *button_press;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500447 xcb_keymap_notify_event_t *keymap_notify;
448 xcb_focus_in_event_t *focus_in;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400449 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500450 uint32_t *k;
451 int i, set;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400452
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400453 prev = NULL;
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400454 while (x11_compositor_next_event(c, &event, mask)) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400455 switch (prev ? prev->response_type & ~0x80 : 0x80) {
456 case XCB_KEY_RELEASE:
457 key_release = (xcb_key_press_event_t *) prev;
458 key_press = (xcb_key_press_event_t *) event;
459 if ((event->response_type & ~0x80) == XCB_KEY_PRESS &&
460 key_release->time == key_press->time) {
461 /* Don't deliver the held key release
462 * event or the new key press event. */
463 free(event);
464 free(prev);
465 prev = NULL;
466 continue;
467 } else {
468 /* Deliver the held key release now
469 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400470 * event below. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400471 notify_key(c->base.input_device,
472 key_release->time,
473 key_release->detail - 8, 0);
474 free(prev);
475 prev = NULL;
476 break;
477 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400478
479 case XCB_FOCUS_IN:
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400480 /* assert event is keymap_notify */
481 focus_in = (xcb_focus_in_event_t *) prev;
482 keymap_notify = (xcb_keymap_notify_event_t *) event;
483 c->keys.size = 0;
484 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
485 set = keymap_notify->keys[i >> 3] &
486 (1 << (i & 7));
487 if (set) {
488 k = wl_array_add(&c->keys, sizeof *k);
489 *k = i;
490 }
491 }
492
493 output = x11_compositor_find_output(c, focus_in->event);
494 notify_keyboard_focus(c->base.input_device,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400495 wlsc_compositor_get_time(),
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400496 &output->base, &c->keys);
497
498 free(prev);
499 prev = NULL;
500 break;
501
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400502 default:
503 /* No previous event held */
504 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400505 }
506
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400507 switch (event->response_type & ~0x80) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400508 case XCB_KEY_PRESS:
509 key_press = (xcb_key_press_event_t *) event;
510 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400511 key_press->time, key_press->detail - 8, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400512 break;
513 case XCB_KEY_RELEASE:
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400514 prev = event;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400515 break;
516 case XCB_BUTTON_PRESS:
517 button_press = (xcb_button_press_event_t *) event;
518 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400519 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400520 button_press->detail + BTN_LEFT - 1, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400521 break;
522 case XCB_BUTTON_RELEASE:
523 button_press = (xcb_button_press_event_t *) event;
524 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400525 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400526 button_press->detail + BTN_LEFT - 1, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400527 break;
528
529 case XCB_MOTION_NOTIFY:
530 motion_notify = (xcb_motion_notify_event_t *) event;
531 notify_motion(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400532 motion_notify->time,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400533 motion_notify->event_x,
534 motion_notify->event_y);
535 break;
536
537 case XCB_EXPOSE:
Benjamin Franzke84290d02011-03-02 10:07:59 +0100538 /* FIXME: schedule output repaint */
539 /* output = x11_compositor_find_output(c, expose->window); */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400540
Benjamin Franzke84290d02011-03-02 10:07:59 +0100541 wlsc_compositor_schedule_repaint(&c->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400542 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400543
544 case XCB_ENTER_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500545 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500546 if (enter_notify->state >= Button1Mask)
547 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500548 output = x11_compositor_find_output(c, enter_notify->event);
549 notify_pointer_focus(c->base.input_device,
550 enter_notify->time,
551 &output->base,
552 enter_notify->event_x,
553 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400554 break;
555
556 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500557 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500558 if (enter_notify->state >= Button1Mask)
559 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500560 notify_pointer_focus(c->base.input_device,
561 enter_notify->time,
562 NULL,
563 enter_notify->event_x,
564 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400565 break;
566
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400567 case XCB_CLIENT_MESSAGE:
568 client_message = (xcb_client_message_event_t *) event;
569 atom = client_message->data.data32[0];
570 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500571 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400572 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400573
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500574 case XCB_FOCUS_IN:
575 focus_in = (xcb_focus_in_event_t *) event;
576 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
577 break;
578
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400579 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500580 break;
581
582 case XCB_FOCUS_OUT:
583 focus_in = (xcb_focus_in_event_t *) event;
584 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
585 break;
586 notify_keyboard_focus(c->base.input_device,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400587 wlsc_compositor_get_time(),
588 NULL, NULL);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500589 break;
590
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500591 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400592 break;
593 }
594
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400595 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400596 free (event);
597 }
598
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400599 switch (prev ? prev->response_type & ~0x80 : 0x80) {
600 case XCB_KEY_RELEASE:
601 key_release = (xcb_key_press_event_t *) prev;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400602 notify_key(c->base.input_device,
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400603 key_release->time, key_release->detail - 8, 0);
604 free(prev);
605 prev = NULL;
606 break;
607 default:
608 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500609 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400610
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400611 return event != NULL;
Kristian Høgsbergee724822011-04-21 14:51:44 -0400612}
613
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400614#define F(field) offsetof(struct x11_compositor, field)
615
616static void
617x11_compositor_get_resources(struct x11_compositor *c)
618{
619 static const struct { const char *name; int offset; } atoms[] = {
620 { "WM_PROTOCOLS", F(atom.wm_protocols) },
621 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
622 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
623 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500624 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400625 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500626 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400627 { "_NET_WM_STATE", F(atom.net_wm_state) },
628 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500629 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400630 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500631 { "CARDINAL", F(atom.cardinal) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400632 };
633
634 xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
635 xcb_intern_atom_reply_t *reply;
636 xcb_pixmap_t pixmap;
637 xcb_gc_t gc;
638 int i;
639 uint8_t data[] = { 0, 0, 0, 0 };
640
641 for (i = 0; i < ARRAY_SIZE(atoms); i++)
642 cookies[i] = xcb_intern_atom (c->conn, 0,
643 strlen(atoms[i].name),
644 atoms[i].name);
645
646 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
647 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
648 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
649 free(reply);
650 }
651
652 pixmap = xcb_generate_id(c->conn);
653 gc = xcb_generate_id(c->conn);
654 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
655 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
656 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
657 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
658 c->null_cursor = xcb_generate_id(c->conn);
659 xcb_create_cursor (c->conn, c->null_cursor,
660 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
661 xcb_free_gc(c->conn, gc);
662 xcb_free_pixmap(c->conn, pixmap);
663}
664
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500665static void
666x11_destroy(struct wlsc_compositor *ec)
667{
668 free(ec);
669}
670
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400671static struct wlsc_compositor *
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400672x11_compositor_create(struct wl_display *display,
673 int width, int height, int fullscreen)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400674{
675 struct x11_compositor *c;
676 struct wl_event_loop *loop;
677 xcb_screen_iterator_t s;
678
679 c = malloc(sizeof *c);
680 if (c == NULL)
681 return NULL;
682
683 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100684
685 c->dpy = XOpenDisplay(NULL);
Cyril Brulebois20798292011-04-06 18:05:40 +0200686 if (c->dpy == NULL)
687 return NULL;
688
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100689 c->conn = XGetXCBConnection(c->dpy);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +0100690 XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
691
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400692 if (xcb_connection_has_error(c->conn))
693 return NULL;
694
695 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
696 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500697 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400698
699 x11_compositor_get_resources(c);
700
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400701 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +0200702 if (x11_compositor_init_egl(c) < 0)
Darxus55973f22010-11-22 21:24:39 -0500703 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400704
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500705 c->base.destroy = x11_destroy;
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500706
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400707 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400708 if (wlsc_compositor_init(&c->base, display) < 0)
709 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400710
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400711 if (x11_compositor_create_output(c, width, height, fullscreen) < 0)
Darxus55973f22010-11-22 21:24:39 -0500712 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400713
Darxus55973f22010-11-22 21:24:39 -0500714 if (x11_input_create(c) < 0)
715 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400716
717 loop = wl_display_get_event_loop(c->base.wl_display);
718
719 c->xcb_source =
720 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
721 WL_EVENT_READABLE,
722 x11_compositor_handle_event, c);
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400723 wl_event_source_check(c->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400724
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400725 return &c->base;
726}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400727
728struct wlsc_compositor *
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400729backend_init(struct wl_display *display, char *options);
730
731WL_EXPORT struct wlsc_compositor *
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400732backend_init(struct wl_display *display, char *options)
733{
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400734 int width = 1024, height = 640, fullscreen = 0, i;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400735 char *p, *value;
736
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400737 static char * const tokens[] = {
738 "width", "height", "fullscreen", NULL
739 };
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400740
741 p = options;
742 while (i = getsubopt(&p, tokens, &value), i != -1) {
743 switch (i) {
744 case 0:
745 width = strtol(value, NULL, 0);
746 break;
747 case 1:
748 height = strtol(value, NULL, 0);
749 break;
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400750 case 2:
751 fullscreen = 1;
752 break;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400753 }
754 }
755
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400756 return x11_compositor_create(display, width, height, fullscreen);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400757}