blob: 3517fadcbf2f7074aff628961c7303567db255d8 [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
Egbert Eiche7b8d902011-05-10 20:00:19 +0000116 setenv("EGL_PLATFORM", "x11", 1);
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100117 c->base.display = eglGetDisplay(c->dpy);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400118 if (c->base.display == NULL) {
119 fprintf(stderr, "failed to create display\n");
120 return -1;
121 }
122
123 if (!eglInitialize(c->base.display, &major, &minor)) {
124 fprintf(stderr, "failed to initialize display\n");
125 return -1;
126 }
127
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400128 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
129 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
130 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400131 return -1;
132 }
133
Darxus55973f22010-11-22 21:24:39 -0500134 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
135 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
136 return -1;
137 }
Benjamin Franzke84290d02011-03-02 10:07:59 +0100138 if (!eglChooseConfig(c->base.display, config_attribs,
139 &c->base.config, 1, &n) || n == 0) {
140 fprintf(stderr, "failed to choose config: %d\n", n);
141 return -1;
142 }
Darxus55973f22010-11-22 21:24:39 -0500143
Benjamin Franzke84290d02011-03-02 10:07:59 +0100144 c->base.context = eglCreateContext(c->base.display, c->base.config,
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400145 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400146 if (c->base.context == NULL) {
147 fprintf(stderr, "failed to create context\n");
148 return -1;
149 }
150
151 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
152 EGL_NO_SURFACE, c->base.context)) {
153 fprintf(stderr, "failed to make context current\n");
154 return -1;
155 }
156
157 return 0;
158}
159
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100160static int
161x11_output_prepare_render(struct wlsc_output *output_base)
162{
163 struct x11_output *output = (struct x11_output *) output_base;
164 struct wlsc_compositor *ec = output->base.compositor;
165
166 if (!eglMakeCurrent(ec->display, output->egl_surface,
167 output->egl_surface, ec->context)) {
168 fprintf(stderr, "failed to make current\n");
169 return -1;
170 }
171
172 return 0;
173}
174
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100175static int
176x11_output_present(struct wlsc_output *output_base)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400177{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100178 struct x11_output *output = (struct x11_output *) output_base;
179 struct wlsc_compositor *ec = output->base.compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400180 struct timeval tv;
181 uint32_t msec;
182
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100183 if (x11_output_prepare_render(&output->base))
184 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100185
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100186 eglSwapBuffers(ec->display, output->egl_surface);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400187
188 gettimeofday(&tv, NULL);
189 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100190 wlsc_output_finish_frame(&output->base, msec);
191
192 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400193}
194
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200195static int
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200196x11_output_prepare_scanout_surface(struct wlsc_output *output_base,
197 struct wlsc_surface *es)
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200198{
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200199 return -1;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200200}
201
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200202static int
203x11_output_set_cursor(struct wlsc_output *output_base,
Kristian Høgsberge4c40a42011-05-06 14:04:21 -0400204 struct wlsc_input_device *input)
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200205{
206 return -1;
207}
208
209
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400210static void
211x11_output_set_wm_protocols(struct x11_output *output)
212{
213 xcb_atom_t list[1];
214 struct x11_compositor *c =
215 (struct x11_compositor *) output->base.compositor;
216
217 list[0] = c->atom.wm_delete_window;
218 xcb_change_property (c->conn,
219 XCB_PROP_MODE_REPLACE,
220 output->window,
221 c->atom.wm_protocols,
222 XCB_ATOM_ATOM,
223 32,
224 ARRAY_SIZE(list),
225 list);
226}
227
228struct wm_normal_hints {
229 uint32_t flags;
230 uint32_t pad[4];
231 int32_t min_width, min_height;
232 int32_t max_width, max_height;
233 int32_t width_inc, height_inc;
234 int32_t min_aspect_x, min_aspect_y;
235 int32_t max_aspect_x, max_aspect_y;
236 int32_t base_width, base_height;
237 int32_t win_gravity;
238};
239
240#define WM_NORMAL_HINTS_MIN_SIZE 16
241#define WM_NORMAL_HINTS_MAX_SIZE 32
242
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500243static void
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400244x11_output_set_icon(struct x11_compositor *c,
245 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500246{
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400247 uint32_t *icon, *pixels, stride;
248 int32_t width, height;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500249
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400250 pixels = wlsc_load_image(filename, &width, &height, &stride);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500251 if (!pixels)
252 return;
253 icon = malloc(width * height * 4 + 8);
254 if (!icon) {
255 free(pixels);
256 return;
257 }
258
259 icon[0] = width;
260 icon[1] = height;
261 memcpy(icon + 2, pixels, width * height * 4);
262 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
263 c->atom.net_wm_icon, c->atom.cardinal, 32,
264 width * height + 2, icon);
265 free(icon);
266 free(pixels);
267}
268
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400269static int
270x11_compositor_create_output(struct x11_compositor *c, int width, int height)
271{
272 static const char name[] = "Wayland Compositor";
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500273 static const char class[] = "wayland-1\0Wayland Compositor";
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400274 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400275 xcb_screen_iterator_t iter;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400276 struct wm_normal_hints normal_hints;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400277 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
278 uint32_t values[2] = {
279 XCB_EVENT_MASK_KEY_PRESS |
280 XCB_EVENT_MASK_KEY_RELEASE |
281 XCB_EVENT_MASK_BUTTON_PRESS |
282 XCB_EVENT_MASK_BUTTON_RELEASE |
283 XCB_EVENT_MASK_POINTER_MOTION |
284 XCB_EVENT_MASK_EXPOSURE |
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400285 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
286 XCB_EVENT_MASK_ENTER_WINDOW |
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500287 XCB_EVENT_MASK_LEAVE_WINDOW |
288 XCB_EVENT_MASK_KEYMAP_STATE |
289 XCB_EVENT_MASK_FOCUS_CHANGE,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400290 0
291 };
292
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400293 output = malloc(sizeof *output);
294 if (output == NULL)
295 return -1;
296
297 memset(output, 0, sizeof *output);
Benjamin Franzke84290d02011-03-02 10:07:59 +0100298 wlsc_output_init(&output->base, &c->base, 0, 0, width, height,
299 WL_OUTPUT_FLIPPED);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400300
301 values[1] = c->null_cursor;
302 output->window = xcb_generate_id(c->conn);
303 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
304 xcb_create_window(c->conn,
305 XCB_COPY_FROM_PARENT,
306 output->window,
307 iter.data->root,
308 0, 0,
309 width, height,
310 0,
311 XCB_WINDOW_CLASS_INPUT_OUTPUT,
312 iter.data->root_visual,
313 mask, values);
314
315 /* Don't resize me. */
316 memset(&normal_hints, 0, sizeof normal_hints);
317 normal_hints.flags =
318 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
319 normal_hints.min_width = width;
320 normal_hints.min_height = height;
321 normal_hints.max_width = width;
322 normal_hints.max_height = height;
323 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
324 c->atom.wm_normal_hints,
325 c->atom.wm_size_hints, 32,
326 sizeof normal_hints / 4,
327 (uint8_t *) &normal_hints);
328
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400329 /* Set window name. Don't bother with non-EWMH WMs. */
330 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
331 c->atom.net_wm_name, c->atom.utf8_string, 8,
332 strlen(name), name);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500333 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
334 c->atom.wm_class, c->atom.string, 8,
335 sizeof class, class);
336
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400337 x11_output_set_icon(c, output, DATADIR "/wayland/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500338
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500339 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400340
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400341 x11_output_set_wm_protocols(output);
342
Benjamin Franzke84290d02011-03-02 10:07:59 +0100343 output->egl_surface =
344 eglCreateWindowSurface(c->base.display, c->base.config,
345 output->window, NULL);
346 if (!output->egl_surface) {
347 fprintf(stderr, "failed to create window surface\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400348 return -1;
349 }
Benjamin Franzke84290d02011-03-02 10:07:59 +0100350 if (!eglMakeCurrent(c->base.display, output->egl_surface,
351 output->egl_surface, c->base.context)) {
352 fprintf(stderr, "failed to make surface current\n");
353 return -1;
354 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400355
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100356 output->base.prepare_render = x11_output_prepare_render;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100357 output->base.present = x11_output_present;
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200358 output->base.prepare_scanout_surface =
359 x11_output_prepare_scanout_surface;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200360 output->base.set_hardware_cursor = x11_output_set_cursor;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100361
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400362 wl_list_insert(c->base.output_list.prev, &output->base.link);
363
364 return 0;
365}
366
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400367static struct x11_output *
368x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
369{
370 struct x11_output *output;
371
372 wl_list_for_each(output, &c->base.output_list, base.link) {
373 if (output->window == window)
374 return output;
375 }
376
377 return NULL;
378}
379
Kristian Høgsbergee724822011-04-21 14:51:44 -0400380static int
381x11_compositor_next_event(struct x11_compositor *c,
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400382 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -0400383{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400384 if (mask & WL_EVENT_READABLE) {
385 *event = xcb_poll_for_event(c->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -0400386 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400387#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400388 *event = xcb_poll_for_queued_event(c->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400389#else
390 *event = xcb_poll_for_event(c->conn);
391#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -0400392 }
393
394 return *event != NULL;
395}
396
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400397static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400398x11_compositor_handle_event(int fd, uint32_t mask, void *data)
399{
400 struct x11_compositor *c = data;
401 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400402 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400403 xcb_client_message_event_t *client_message;
404 xcb_motion_notify_event_t *motion_notify;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500405 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400406 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400407 xcb_button_press_event_t *button_press;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500408 xcb_keymap_notify_event_t *keymap_notify;
409 xcb_focus_in_event_t *focus_in;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400410 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500411 uint32_t *k;
412 int i, set;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400413
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400414 prev = NULL;
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400415 while (x11_compositor_next_event(c, &event, mask)) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400416 switch (prev ? prev->response_type & ~0x80 : 0x80) {
417 case XCB_KEY_RELEASE:
418 key_release = (xcb_key_press_event_t *) prev;
419 key_press = (xcb_key_press_event_t *) event;
420 if ((event->response_type & ~0x80) == XCB_KEY_PRESS &&
421 key_release->time == key_press->time) {
422 /* Don't deliver the held key release
423 * event or the new key press event. */
424 free(event);
425 free(prev);
426 prev = NULL;
427 continue;
428 } else {
429 /* Deliver the held key release now
430 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400431 * event below. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400432 notify_key(c->base.input_device,
433 key_release->time,
434 key_release->detail - 8, 0);
435 free(prev);
436 prev = NULL;
437 break;
438 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400439
440 case XCB_FOCUS_IN:
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400441 /* assert event is keymap_notify */
442 focus_in = (xcb_focus_in_event_t *) prev;
443 keymap_notify = (xcb_keymap_notify_event_t *) event;
444 c->keys.size = 0;
445 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
446 set = keymap_notify->keys[i >> 3] &
447 (1 << (i & 7));
448 if (set) {
449 k = wl_array_add(&c->keys, sizeof *k);
450 *k = i;
451 }
452 }
453
454 output = x11_compositor_find_output(c, focus_in->event);
455 notify_keyboard_focus(c->base.input_device,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400456 wlsc_compositor_get_time(),
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400457 &output->base, &c->keys);
458
459 free(prev);
460 prev = NULL;
461 break;
462
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400463 default:
464 /* No previous event held */
465 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400466 }
467
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400468 switch (event->response_type & ~0x80) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400469 case XCB_KEY_PRESS:
470 key_press = (xcb_key_press_event_t *) event;
471 notify_key(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400472 key_press->time, key_press->detail - 8, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400473 break;
474 case XCB_KEY_RELEASE:
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400475 prev = event;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400476 break;
477 case XCB_BUTTON_PRESS:
478 button_press = (xcb_button_press_event_t *) event;
479 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400480 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400481 button_press->detail + BTN_LEFT - 1, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400482 break;
483 case XCB_BUTTON_RELEASE:
484 button_press = (xcb_button_press_event_t *) event;
485 notify_button(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400486 button_press->time,
Kristian Høgsbergf9112b22010-06-14 12:53:43 -0400487 button_press->detail + BTN_LEFT - 1, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400488 break;
489
490 case XCB_MOTION_NOTIFY:
491 motion_notify = (xcb_motion_notify_event_t *) event;
492 notify_motion(c->base.input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400493 motion_notify->time,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400494 motion_notify->event_x,
495 motion_notify->event_y);
496 break;
497
498 case XCB_EXPOSE:
Benjamin Franzke84290d02011-03-02 10:07:59 +0100499 /* FIXME: schedule output repaint */
500 /* output = x11_compositor_find_output(c, expose->window); */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400501
Benjamin Franzke84290d02011-03-02 10:07:59 +0100502 wlsc_compositor_schedule_repaint(&c->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400503 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400504
505 case XCB_ENTER_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500506 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500507 if (enter_notify->state >= Button1Mask)
508 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500509 output = x11_compositor_find_output(c, enter_notify->event);
510 notify_pointer_focus(c->base.input_device,
511 enter_notify->time,
512 &output->base,
513 enter_notify->event_x,
514 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400515 break;
516
517 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500518 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500519 if (enter_notify->state >= Button1Mask)
520 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500521 notify_pointer_focus(c->base.input_device,
522 enter_notify->time,
523 NULL,
524 enter_notify->event_x,
525 enter_notify->event_y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400526 break;
527
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400528 case XCB_CLIENT_MESSAGE:
529 client_message = (xcb_client_message_event_t *) event;
530 atom = client_message->data.data32[0];
531 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500532 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400533 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400534
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500535 case XCB_FOCUS_IN:
536 focus_in = (xcb_focus_in_event_t *) event;
537 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
538 break;
539
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400540 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500541 break;
542
543 case XCB_FOCUS_OUT:
544 focus_in = (xcb_focus_in_event_t *) event;
545 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
546 break;
547 notify_keyboard_focus(c->base.input_device,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400548 wlsc_compositor_get_time(),
549 NULL, NULL);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500550 break;
551
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500552 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400553 break;
554 }
555
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400556 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400557 free (event);
558 }
559
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400560 switch (prev ? prev->response_type & ~0x80 : 0x80) {
561 case XCB_KEY_RELEASE:
562 key_release = (xcb_key_press_event_t *) prev;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400563 notify_key(c->base.input_device,
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400564 key_release->time, key_release->detail - 8, 0);
565 free(prev);
566 prev = NULL;
567 break;
568 default:
569 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500570 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400571
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400572 return event != NULL;
Kristian Høgsbergee724822011-04-21 14:51:44 -0400573}
574
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400575#define F(field) offsetof(struct x11_compositor, field)
576
577static void
578x11_compositor_get_resources(struct x11_compositor *c)
579{
580 static const struct { const char *name; int offset; } atoms[] = {
581 { "WM_PROTOCOLS", F(atom.wm_protocols) },
582 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
583 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
584 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500585 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400586 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500587 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500588 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400589 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500590 { "CARDINAL", F(atom.cardinal) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400591 };
592
593 xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
594 xcb_intern_atom_reply_t *reply;
595 xcb_pixmap_t pixmap;
596 xcb_gc_t gc;
597 int i;
598 uint8_t data[] = { 0, 0, 0, 0 };
599
600 for (i = 0; i < ARRAY_SIZE(atoms); i++)
601 cookies[i] = xcb_intern_atom (c->conn, 0,
602 strlen(atoms[i].name),
603 atoms[i].name);
604
605 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
606 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
607 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
608 free(reply);
609 }
610
611 pixmap = xcb_generate_id(c->conn);
612 gc = xcb_generate_id(c->conn);
613 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
614 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
615 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
616 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
617 c->null_cursor = xcb_generate_id(c->conn);
618 xcb_create_cursor (c->conn, c->null_cursor,
619 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
620 xcb_free_gc(c->conn, gc);
621 xcb_free_pixmap(c->conn, pixmap);
622}
623
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500624static void
625x11_destroy(struct wlsc_compositor *ec)
626{
627 free(ec);
628}
629
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400630static struct wlsc_compositor *
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400631x11_compositor_create(struct wl_display *display, int width, int height)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400632{
633 struct x11_compositor *c;
634 struct wl_event_loop *loop;
635 xcb_screen_iterator_t s;
636
637 c = malloc(sizeof *c);
638 if (c == NULL)
639 return NULL;
640
641 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100642
643 c->dpy = XOpenDisplay(NULL);
Cyril Brulebois20798292011-04-06 18:05:40 +0200644 if (c->dpy == NULL)
645 return NULL;
646
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100647 c->conn = XGetXCBConnection(c->dpy);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +0100648 XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
649
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400650 if (xcb_connection_has_error(c->conn))
651 return NULL;
652
653 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
654 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500655 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400656
657 x11_compositor_get_resources(c);
658
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400659 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +0200660 if (x11_compositor_init_egl(c) < 0)
Darxus55973f22010-11-22 21:24:39 -0500661 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400662
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500663 c->base.destroy = x11_destroy;
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500664
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400665 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400666 if (wlsc_compositor_init(&c->base, display) < 0)
667 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400668
Darxus55973f22010-11-22 21:24:39 -0500669 if (x11_compositor_create_output(c, width, height) < 0)
670 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400671
Darxus55973f22010-11-22 21:24:39 -0500672 if (x11_input_create(c) < 0)
673 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400674
675 loop = wl_display_get_event_loop(c->base.wl_display);
676
677 c->xcb_source =
678 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
679 WL_EVENT_READABLE,
680 x11_compositor_handle_event, c);
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400681 wl_event_source_check(c->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400682
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400683 return &c->base;
684}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400685
686struct wlsc_compositor *
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400687backend_init(struct wl_display *display, char *options);
688
689WL_EXPORT struct wlsc_compositor *
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400690backend_init(struct wl_display *display, char *options)
691{
692 int width = 1024, height = 640, i;
693 char *p, *value;
694
695 static char * const tokens[] = { "width", "height", NULL };
696
697 p = options;
698 while (i = getsubopt(&p, tokens, &value), i != -1) {
699 switch (i) {
700 case 0:
701 width = strtol(value, NULL, 0);
702 break;
703 case 1:
704 height = strtol(value, NULL, 0);
705 break;
706 }
707 }
708
709 return x11_compositor_create(display, width, height);
710}