blob: bda76386f1c2bb1731518afb2f535b7088e85cbd [file] [log] [blame]
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2010-2011 Intel Corporation
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04005 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040014 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040015 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040022 */
23
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +080024#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040028#include <stddef.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040029#include <stdlib.h>
30#include <string.h>
31#include <fcntl.h>
32#include <unistd.h>
33#include <errno.h>
34#include <sys/time.h>
Kristian Høgsbergf9112b22010-06-14 12:53:43 -040035#include <linux/input.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040036
37#include <xcb/xcb.h>
Daniel Stone62b33b62012-06-22 13:21:35 +010038#ifdef HAVE_XCB_XKB
39#include <xcb/xkb.h>
40#endif
41
Benjamin Franzke3b288af2011-02-20 19:58:42 +010042#include <X11/Xlib.h>
43#include <X11/Xlib-xcb.h>
44
Daniel Stone576cd152012-06-01 12:14:02 +010045#include <xkbcommon/xkbcommon.h>
46
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040047#include <GLES2/gl2.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040048#include <EGL/egl.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040049
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040050#include "compositor.h"
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040051#include "../shared/config-parser.h"
Martin Minarik6d118362012-06-07 18:01:59 +020052#include "log.h"
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040053
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040054struct x11_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050055 struct weston_compositor base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040056
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -040057 EGLSurface dummy_pbuffer;
58
Benjamin Franzke3b288af2011-02-20 19:58:42 +010059 Display *dpy;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040060 xcb_connection_t *conn;
61 xcb_screen_t *screen;
62 xcb_cursor_t null_cursor;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -050063 struct wl_array keys;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040064 struct wl_event_source *xcb_source;
Daniel Stone576cd152012-06-01 12:14:02 +010065 struct xkb_keymap *xkb_keymap;
Daniel Stone62b33b62012-06-22 13:21:35 +010066 unsigned int has_xkb;
67 uint8_t xkb_event_base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040068 struct {
69 xcb_atom_t wm_protocols;
70 xcb_atom_t wm_normal_hints;
71 xcb_atom_t wm_size_hints;
72 xcb_atom_t wm_delete_window;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050073 xcb_atom_t wm_class;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040074 xcb_atom_t net_wm_name;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050075 xcb_atom_t net_wm_icon;
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -040076 xcb_atom_t net_wm_state;
77 xcb_atom_t net_wm_state_fullscreen;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050078 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040079 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050080 xcb_atom_t cardinal;
Daniel Stone855028f2012-05-01 20:37:10 +010081 xcb_atom_t xkb_names;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040082 } atom;
83};
84
85struct x11_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050086 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040087
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040088 xcb_window_t window;
Benjamin Franzke84290d02011-03-02 10:07:59 +010089 EGLSurface egl_surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050090 struct weston_mode mode;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -040091 struct wl_event_source *finish_frame_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040092};
93
94struct x11_input {
Daniel Stone37816df2012-05-16 18:45:18 +010095 struct weston_seat base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040096};
97
Daniel Stone576cd152012-06-01 12:14:02 +010098static struct xkb_keymap *
99x11_compositor_get_keymap(struct x11_compositor *c)
100{
101 xcb_get_property_cookie_t cookie;
102 xcb_get_property_reply_t *reply;
103 xcb_generic_error_t *error;
104 struct xkb_rule_names names;
105 struct xkb_keymap *ret;
106 const char *value_all, *value_part;
107 int length_all, length_part;
108
109 memset(&names, 0, sizeof(names));
110
111 cookie = xcb_get_property(c->conn, 0, c->screen->root,
112 c->atom.xkb_names, c->atom.string, 0, 1024);
113 reply = xcb_get_property_reply(c->conn, cookie, &error);
114 if (reply == NULL)
115 return NULL;
116
117 value_all = xcb_get_property_value(reply);
118 length_all = xcb_get_property_value_length(reply);
119 value_part = value_all;
120
121#define copy_prop_value(to) \
122 length_part = strlen(value_part); \
123 if (value_part + length_part < (value_all + length_all) && \
124 length_part > 0) \
125 names.to = value_part; \
126 value_part += length_part + 1;
127
128 copy_prop_value(rules);
129 copy_prop_value(model);
130 copy_prop_value(layout);
131 copy_prop_value(variant);
132 copy_prop_value(options);
133#undef copy_prop_value
134
135 ret = xkb_map_new_from_names(c->base.xkb_context, &names, 0);
136
137 free(reply);
138 return ret;
139}
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400140
Daniel Stone62b33b62012-06-22 13:21:35 +0100141static void
142x11_compositor_setup_xkb(struct x11_compositor *c)
143{
144#ifndef HAVE_XCB_XKB
145 weston_log("XCB-XKB not available during build\n");
146 c->has_xkb = 0;
147 c->xkb_event_base = 0;
148 return;
149#else
150 const xcb_query_extension_reply_t *ext;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100151 xcb_generic_error_t *error;
152 xcb_xkb_per_client_flags_cookie_t pcf;
153 xcb_xkb_per_client_flags_reply_t *pcf_reply;
Daniel Stone62b33b62012-06-22 13:21:35 +0100154
155 c->has_xkb = 0;
156 c->xkb_event_base = 0;
157
158 ext = xcb_get_extension_data(c->conn, &xcb_xkb_id);
159 if (!ext) {
160 weston_log("XKB extension not available on host X11 server\n");
161 return;
162 }
163 c->xkb_event_base = ext->first_event;
164
Daniel Stone3ee91e12012-06-22 13:21:36 +0100165 pcf = xcb_xkb_per_client_flags(c->conn,
166 XCB_XKB_ID_USE_CORE_KBD,
167 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
168 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
169 0,
170 0,
171 0);
172 pcf_reply = xcb_xkb_per_client_flags_reply(c->conn, pcf, &error);
173 free(pcf_reply);
174 if (error) {
175 weston_log("failed to set XKB per-client flags, not using "
176 "detectable repeat\n");
177 return;
178 }
179
Daniel Stone62b33b62012-06-22 13:21:35 +0100180 c->has_xkb = 1;
181#endif
182}
183
Darxus55973f22010-11-22 21:24:39 -0500184static int
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300185x11_input_create(struct x11_compositor *c, int no_input)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400186{
187 struct x11_input *input;
Daniel Stone576cd152012-06-01 12:14:02 +0100188 struct xkb_keymap *keymap;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400189
190 input = malloc(sizeof *input);
191 if (input == NULL)
Darxus55973f22010-11-22 21:24:39 -0500192 return -1;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400193
194 memset(input, 0, sizeof *input);
Daniel Stone37816df2012-05-16 18:45:18 +0100195 weston_seat_init(&input->base, &c->base);
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300196 c->base.seat = &input->base;
197
198 if (no_input)
199 return 0;
200
Daniel Stone74419a22012-05-30 16:32:02 +0100201 weston_seat_init_pointer(&input->base);
Daniel Stone576cd152012-06-01 12:14:02 +0100202
Daniel Stone62b33b62012-06-22 13:21:35 +0100203 x11_compositor_setup_xkb(c);
204
Daniel Stone576cd152012-06-01 12:14:02 +0100205 keymap = x11_compositor_get_keymap(c);
206 weston_seat_init_keyboard(&input->base, keymap);
207 if (keymap)
208 xkb_map_unref(keymap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400209
Darxus55973f22010-11-22 21:24:39 -0500210 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400211}
212
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200213static void
214x11_input_destroy(struct x11_compositor *compositor)
215{
Daniel Stone37816df2012-05-16 18:45:18 +0100216 struct x11_input *input = container_of(compositor->base.seat,
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200217 struct x11_input,
Daniel Stone37816df2012-05-16 18:45:18 +0100218 base);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200219
Daniel Stone37816df2012-05-16 18:45:18 +0100220 weston_seat_release(&input->base);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200221 free(input);
222}
223
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400224static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400225x11_compositor_init_egl(struct x11_compositor *c)
226{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400227 EGLint major, minor;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100228 EGLint n;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100229 EGLint config_attribs[] = {
230 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
231 EGL_RED_SIZE, 1,
232 EGL_GREEN_SIZE, 1,
233 EGL_BLUE_SIZE, 1,
Benjamin Franzke84290d02011-03-02 10:07:59 +0100234 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
235 EGL_NONE
236 };
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400237 static const EGLint context_attribs[] = {
238 EGL_CONTEXT_CLIENT_VERSION, 2,
239 EGL_NONE
240 };
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400241
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400242 static const EGLint pbuffer_attribs[] = {
243 EGL_WIDTH, 10,
244 EGL_HEIGHT, 10,
245 EGL_NONE
246 };
247
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400248 c->base.egl_display = eglGetDisplay(c->dpy);
249 if (c->base.egl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200250 weston_log("failed to create display\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400251 return -1;
252 }
253
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400254 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200255 weston_log("failed to initialize display\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400256 return -1;
257 }
258
Darxus55973f22010-11-22 21:24:39 -0500259 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200260 weston_log("failed to bind EGL_OPENGL_ES_API\n");
Darxus55973f22010-11-22 21:24:39 -0500261 return -1;
262 }
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400263 if (!eglChooseConfig(c->base.egl_display, config_attribs,
264 &c->base.egl_config, 1, &n) || n == 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200265 weston_log("failed to choose config: %d\n", n);
Benjamin Franzke84290d02011-03-02 10:07:59 +0100266 return -1;
267 }
Darxus55973f22010-11-22 21:24:39 -0500268
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400269 c->base.egl_context =
270 eglCreateContext(c->base.egl_display, c->base.egl_config,
271 EGL_NO_CONTEXT, context_attribs);
272 if (c->base.egl_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200273 weston_log("failed to create context\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400274 return -1;
275 }
276
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400277 c->dummy_pbuffer = eglCreatePbufferSurface(c->base.egl_display,
278 c->base.egl_config,
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400279 pbuffer_attribs);
Kristian Høgsbergc0bc8442012-05-17 09:40:23 -0400280 if (c->dummy_pbuffer == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200281 weston_log("failed to create dummy pbuffer\n");
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -0400282 return -1;
283 }
284
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400285 if (!eglMakeCurrent(c->base.egl_display, c->dummy_pbuffer,
286 c->dummy_pbuffer, c->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200287 weston_log("failed to make context current\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400288 return -1;
289 }
290
291 return 0;
292}
293
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200294static void
295x11_compositor_fini_egl(struct x11_compositor *compositor)
296{
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400297 eglMakeCurrent(compositor->base.egl_display,
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200298 EGL_NO_SURFACE, EGL_NO_SURFACE,
299 EGL_NO_CONTEXT);
300
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400301 eglTerminate(compositor->base.egl_display);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200302 eglReleaseThread();
303}
304
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500305static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500306x11_output_repaint(struct weston_output *output_base,
307 pixman_region32_t *damage)
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500308{
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500309 struct x11_output *output = (struct x11_output *)output_base;
310 struct x11_compositor *compositor =
311 (struct x11_compositor *)output->base.compositor;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500312 struct weston_surface *surface;
313
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400314 if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
315 output->egl_surface,
316 compositor->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200317 weston_log("failed to make current\n");
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500318 return;
319 }
320
321 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500322 weston_surface_draw(surface, &output->base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500323
Kristian Høgsberge0f832b2012-06-20 00:13:18 -0400324 wl_signal_emit(&output->base.frame_signal, output);
Scott Moreau062be7e2012-04-20 13:37:33 -0600325
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400326 eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500327
328 wl_event_source_timer_update(output->finish_frame_timer, 10);
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500329}
330
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100331static int
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400332finish_frame_handler(void *data)
333{
334 struct x11_output *output = data;
335 uint32_t msec;
336 struct timeval tv;
337
338 gettimeofday(&tv, NULL);
339 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500340 weston_output_finish_frame(&output->base, msec);
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400341
342 return 1;
343}
344
Matt Roper361d2ad2011-08-29 13:52:23 -0700345static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500346x11_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700347{
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200348 struct x11_output *output = (struct x11_output *)output_base;
349 struct x11_compositor *compositor =
350 (struct x11_compositor *)output->base.compositor;
351
352 wl_list_remove(&output->base.link);
353 wl_event_source_remove(output->finish_frame_timer);
354
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400355 eglDestroySurface(compositor->base.egl_display, output->egl_surface);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200356
357 xcb_destroy_window(compositor->conn, output->window);
358
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500359 weston_output_destroy(&output->base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200360
361 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700362}
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200363
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400364static void
365x11_output_set_wm_protocols(struct x11_output *output)
366{
367 xcb_atom_t list[1];
368 struct x11_compositor *c =
369 (struct x11_compositor *) output->base.compositor;
370
371 list[0] = c->atom.wm_delete_window;
372 xcb_change_property (c->conn,
373 XCB_PROP_MODE_REPLACE,
374 output->window,
375 c->atom.wm_protocols,
376 XCB_ATOM_ATOM,
377 32,
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500378 ARRAY_LENGTH(list),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400379 list);
380}
381
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400382static void
383x11_output_change_state(struct x11_output *output, int add, xcb_atom_t state)
384{
385 xcb_client_message_event_t event;
386 struct x11_compositor *c =
387 (struct x11_compositor *) output->base.compositor;
388 xcb_screen_iterator_t iter;
389
390#define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
391#define _NET_WM_STATE_ADD 1 /* add/set property */
392#define _NET_WM_STATE_TOGGLE 2 /* toggle property */
393
394 memset(&event, 0, sizeof event);
395 event.response_type = XCB_CLIENT_MESSAGE;
396 event.format = 32;
397 event.window = output->window;
398 event.type = c->atom.net_wm_state;
399
400 event.data.data32[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
401 event.data.data32[1] = state;
402 event.data.data32[2] = 0;
403 event.data.data32[3] = 0;
404 event.data.data32[4] = 0;
405
406 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
407 xcb_send_event(c->conn, 0, iter.data->root,
408 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
409 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
410 (void *) &event);
411}
412
413
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400414struct wm_normal_hints {
415 uint32_t flags;
416 uint32_t pad[4];
417 int32_t min_width, min_height;
418 int32_t max_width, max_height;
419 int32_t width_inc, height_inc;
420 int32_t min_aspect_x, min_aspect_y;
421 int32_t max_aspect_x, max_aspect_y;
422 int32_t base_width, base_height;
423 int32_t win_gravity;
424};
425
426#define WM_NORMAL_HINTS_MIN_SIZE 16
427#define WM_NORMAL_HINTS_MAX_SIZE 32
428
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500429static void
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400430x11_output_set_icon(struct x11_compositor *c,
431 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500432{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400433 uint32_t *icon;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400434 int32_t width, height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400435 pixman_image_t *image;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500436
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400437 image = load_image(filename);
438 if (!image)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500439 return;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400440 width = pixman_image_get_width(image);
441 height = pixman_image_get_height(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500442 icon = malloc(width * height * 4 + 8);
443 if (!icon) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400444 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500445 return;
446 }
447
448 icon[0] = width;
449 icon[1] = height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400450 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500451 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
452 c->atom.net_wm_icon, c->atom.cardinal, 32,
453 width * height + 2, icon);
454 free(icon);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400455 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500456}
457
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400458static int
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700459x11_compositor_create_output(struct x11_compositor *c, int x, int y,
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300460 int width, int height, int fullscreen,
461 int no_input)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400462{
Kristian Høgsberg86000402012-01-03 23:24:14 -0500463 static const char name[] = "Weston Compositor";
464 static const char class[] = "weston-1\0Weston Compositor";
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400465 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400466 xcb_screen_iterator_t iter;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400467 struct wm_normal_hints normal_hints;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400468 struct wl_event_loop *loop;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400469 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300470 uint32_t values[2] = {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400471 XCB_EVENT_MASK_EXPOSURE |
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300472 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400473 0
474 };
475
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300476 if (!no_input)
477 values[0] |=
478 XCB_EVENT_MASK_KEY_PRESS |
479 XCB_EVENT_MASK_KEY_RELEASE |
480 XCB_EVENT_MASK_BUTTON_PRESS |
481 XCB_EVENT_MASK_BUTTON_RELEASE |
482 XCB_EVENT_MASK_POINTER_MOTION |
483 XCB_EVENT_MASK_ENTER_WINDOW |
484 XCB_EVENT_MASK_LEAVE_WINDOW |
485 XCB_EVENT_MASK_KEYMAP_STATE |
486 XCB_EVENT_MASK_FOCUS_CHANGE;
487
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400488 output = malloc(sizeof *output);
489 if (output == NULL)
490 return -1;
491
492 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400493
494 output->mode.flags =
495 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
496 output->mode.width = width;
497 output->mode.height = height;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400498 output->mode.refresh = 60000;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400499 wl_list_init(&output->base.mode_list);
500 wl_list_insert(&output->base.mode_list, &output->mode.link);
501
502 output->base.current = &output->mode;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500503 weston_output_init(&output->base, &c->base, x, y, width, height,
Benjamin Franzke84290d02011-03-02 10:07:59 +0100504 WL_OUTPUT_FLIPPED);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400505
506 values[1] = c->null_cursor;
507 output->window = xcb_generate_id(c->conn);
508 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
509 xcb_create_window(c->conn,
510 XCB_COPY_FROM_PARENT,
511 output->window,
512 iter.data->root,
513 0, 0,
514 width, height,
515 0,
516 XCB_WINDOW_CLASS_INPUT_OUTPUT,
517 iter.data->root_visual,
518 mask, values);
519
520 /* Don't resize me. */
521 memset(&normal_hints, 0, sizeof normal_hints);
522 normal_hints.flags =
523 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
524 normal_hints.min_width = width;
525 normal_hints.min_height = height;
526 normal_hints.max_width = width;
527 normal_hints.max_height = height;
528 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
529 c->atom.wm_normal_hints,
530 c->atom.wm_size_hints, 32,
531 sizeof normal_hints / 4,
532 (uint8_t *) &normal_hints);
533
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400534 /* Set window name. Don't bother with non-EWMH WMs. */
535 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
536 c->atom.net_wm_name, c->atom.utf8_string, 8,
537 strlen(name), name);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500538 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
539 c->atom.wm_class, c->atom.string, 8,
540 sizeof class, class);
541
Kristian Høgsberg86000402012-01-03 23:24:14 -0500542 x11_output_set_icon(c, output, DATADIR "/weston/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500543
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500544 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400545
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400546 x11_output_set_wm_protocols(output);
547
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400548 if (fullscreen)
549 x11_output_change_state(output, 1,
550 c->atom.net_wm_state_fullscreen);
551
Benjamin Franzke84290d02011-03-02 10:07:59 +0100552 output->egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400553 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
Benjamin Franzke84290d02011-03-02 10:07:59 +0100554 output->window, NULL);
555 if (!output->egl_surface) {
Martin Minarik6d118362012-06-07 18:01:59 +0200556 weston_log("failed to create window surface\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400557 return -1;
558 }
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400559 if (!eglMakeCurrent(c->base.egl_display, output->egl_surface,
560 output->egl_surface, c->base.egl_context)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200561 weston_log("failed to make surface current\n");
Benjamin Franzke84290d02011-03-02 10:07:59 +0100562 return -1;
563 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400564
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400565 loop = wl_display_get_event_loop(c->base.wl_display);
566 output->finish_frame_timer =
567 wl_event_loop_add_timer(loop, finish_frame_handler, output);
568
Alex Wubd3354b2012-04-17 17:20:49 +0800569 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500570 output->base.repaint = x11_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700571 output->base.destroy = x11_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800572 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200573 output->base.set_backlight = NULL;
574 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800575 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100576
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400577 wl_list_insert(c->base.output_list.prev, &output->base.link);
578
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400579 weston_log("x11 output %dx%d, window id %d\n",
580 width, height, output->window);
581
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400582 return 0;
583}
584
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400585static struct x11_output *
586x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
587{
588 struct x11_output *output;
589
590 wl_list_for_each(output, &c->base.output_list, base.link) {
591 if (output->window == window)
592 return output;
593 }
594
595 return NULL;
596}
597
Tiago Vignattia3a71622011-12-08 17:03:17 +0200598static void
599x11_compositor_deliver_button_event(struct x11_compositor *c,
600 xcb_generic_event_t *event, int state)
601{
602 xcb_button_press_event_t *button_event =
603 (xcb_button_press_event_t *) event;
Daniel Stone5d663712012-05-04 11:21:55 +0100604 uint32_t button;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200605
606 switch (button_event->detail) {
607 default:
608 button = button_event->detail + BTN_LEFT - 1;
609 break;
610 case 2:
611 button = BTN_MIDDLE;
612 break;
613 case 3:
614 button = BTN_RIGHT;
615 break;
616 case 4:
Scott Moreau210d0792012-03-22 10:47:01 -0600617 if (state)
Daniel Stone37816df2012-05-16 18:45:18 +0100618 notify_axis(&c->base.seat->seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600619 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100620 WL_POINTER_AXIS_VERTICAL_SCROLL,
621 wl_fixed_from_int(1));
Scott Moreau210d0792012-03-22 10:47:01 -0600622 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200623 case 5:
Scott Moreau210d0792012-03-22 10:47:01 -0600624 if (state)
Daniel Stone37816df2012-05-16 18:45:18 +0100625 notify_axis(&c->base.seat->seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600626 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100627 WL_POINTER_AXIS_VERTICAL_SCROLL,
628 wl_fixed_from_int(-1));
Scott Moreau210d0792012-03-22 10:47:01 -0600629 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200630 case 6:
Scott Moreau210d0792012-03-22 10:47:01 -0600631 if (state)
Daniel Stone37816df2012-05-16 18:45:18 +0100632 notify_axis(&c->base.seat->seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600633 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100634 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
635 wl_fixed_from_int(1));
Scott Moreau210d0792012-03-22 10:47:01 -0600636 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200637 case 7:
Scott Moreau210d0792012-03-22 10:47:01 -0600638 if (state)
Daniel Stone37816df2012-05-16 18:45:18 +0100639 notify_axis(&c->base.seat->seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600640 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100641 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
642 wl_fixed_from_int(-1));
Tiago Vignattia3a71622011-12-08 17:03:17 +0200643 return;
644 }
645
Daniel Stone37816df2012-05-16 18:45:18 +0100646 notify_button(&c->base.seat->seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100647 weston_compositor_get_time(), button,
648 state ? WL_POINTER_BUTTON_STATE_PRESSED :
649 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignattia3a71622011-12-08 17:03:17 +0200650}
651
Kristian Høgsbergee724822011-04-21 14:51:44 -0400652static int
653x11_compositor_next_event(struct x11_compositor *c,
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400654 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -0400655{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400656 if (mask & WL_EVENT_READABLE) {
657 *event = xcb_poll_for_event(c->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -0400658 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400659#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400660 *event = xcb_poll_for_queued_event(c->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400661#else
662 *event = xcb_poll_for_event(c->conn);
663#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -0400664 }
665
666 return *event != NULL;
667}
668
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400669static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400670x11_compositor_handle_event(int fd, uint32_t mask, void *data)
671{
672 struct x11_compositor *c = data;
673 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400674 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400675 xcb_client_message_event_t *client_message;
676 xcb_motion_notify_event_t *motion_notify;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500677 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400678 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500679 xcb_keymap_notify_event_t *keymap_notify;
680 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400681 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400682 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500683 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400684 uint32_t i, set;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400685 wl_fixed_t x, y;
Bill Spitzakb715cec2012-06-05 17:08:23 -0400686 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400687
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400688 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -0400689 count = 0;
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400690 while (x11_compositor_next_event(c, &event, mask)) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400691 switch (prev ? prev->response_type & ~0x80 : 0x80) {
692 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +0100693 /* Suppress key repeat events; this is only used if we
694 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400695 key_release = (xcb_key_press_event_t *) prev;
696 key_press = (xcb_key_press_event_t *) event;
697 if ((event->response_type & ~0x80) == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -0700698 key_release->time == key_press->time &&
699 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400700 /* Don't deliver the held key release
701 * event or the new key press event. */
702 free(event);
703 free(prev);
704 prev = NULL;
705 continue;
706 } else {
707 /* Deliver the held key release now
708 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400709 * event below. */
Daniel Stone37816df2012-05-16 18:45:18 +0100710 notify_key(&c->base.seat->seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500711 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100712 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100713 WL_KEYBOARD_KEY_STATE_RELEASED,
714 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400715 free(prev);
716 prev = NULL;
717 break;
718 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400719
720 case XCB_FOCUS_IN:
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400721 /* assert event is keymap_notify */
722 focus_in = (xcb_focus_in_event_t *) prev;
723 keymap_notify = (xcb_keymap_notify_event_t *) event;
724 c->keys.size = 0;
725 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
726 set = keymap_notify->keys[i >> 3] &
727 (1 << (i & 7));
728 if (set) {
729 k = wl_array_add(&c->keys, sizeof *k);
730 *k = i;
731 }
732 }
733
734 output = x11_compositor_find_output(c, focus_in->event);
Daniel Stoned6da09e2012-06-22 13:21:29 +0100735 /* Unfortunately the state only comes with the enter
736 * event, rather than with the focus event. I'm not
737 * sure of the exact semantics around it and whether
738 * we can ensure that we get both? */
739 notify_keyboard_focus_in(&c->base.seat->seat, &c->keys,
740 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400741
742 free(prev);
743 prev = NULL;
744 break;
745
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400746 default:
747 /* No previous event held */
748 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400749 }
750
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400751 switch (event->response_type & ~0x80) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400752 case XCB_KEY_PRESS:
753 key_press = (xcb_key_press_event_t *) event;
Daniel Stone37816df2012-05-16 18:45:18 +0100754 notify_key(&c->base.seat->seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500755 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100756 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100757 WL_KEYBOARD_KEY_STATE_PRESSED,
758 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400759 break;
760 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +0100761 /* If we don't have XKB, we need to use the lame
762 * autorepeat detection above. */
763 if (!c->has_xkb) {
764 prev = event;
765 break;
766 }
767 key_release = (xcb_key_press_event_t *) event;
768 notify_key(&c->base.seat->seat,
769 weston_compositor_get_time(),
770 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100771 WL_KEYBOARD_KEY_STATE_RELEASED,
772 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400773 break;
774 case XCB_BUTTON_PRESS:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200775 x11_compositor_deliver_button_event(c, event, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400776 break;
777 case XCB_BUTTON_RELEASE:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200778 x11_compositor_deliver_button_event(c, event, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400779 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400780 case XCB_MOTION_NOTIFY:
781 motion_notify = (xcb_motion_notify_event_t *) event;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700782 output = x11_compositor_find_output(c, motion_notify->event);
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400783 x = wl_fixed_from_int(output->base.x + motion_notify->event_x);
784 y = wl_fixed_from_int(output->base.y + motion_notify->event_y);
Daniel Stone37816df2012-05-16 18:45:18 +0100785 notify_motion(&c->base.seat->seat,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400786 weston_compositor_get_time(), x, y);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400787 break;
788
789 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400790 expose = (xcb_expose_event_t *) event;
791 output = x11_compositor_find_output(c, expose->window);
792 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400793 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400794
795 case XCB_ENTER_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500796 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500797 if (enter_notify->state >= Button1Mask)
798 break;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500799 output = x11_compositor_find_output(c, enter_notify->event);
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400800 x = wl_fixed_from_int(output->base.x + enter_notify->event_x);
801 y = wl_fixed_from_int(output->base.y + enter_notify->event_y);
Daniel Stone351eb612012-05-31 15:27:47 -0400802 /* XXX notify_modifiers() */
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400803
Daniel Stone37816df2012-05-16 18:45:18 +0100804 notify_pointer_focus(&c->base.seat->seat,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400805 &output->base, x, y);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400806 break;
807
808 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500809 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500810 if (enter_notify->state >= Button1Mask)
811 break;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700812 output = x11_compositor_find_output(c, enter_notify->event);
Daniel Stone37816df2012-05-16 18:45:18 +0100813 notify_pointer_focus(&c->base.seat->seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400814 break;
815
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400816 case XCB_CLIENT_MESSAGE:
817 client_message = (xcb_client_message_event_t *) event;
818 atom = client_message->data.data32[0];
819 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500820 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400821 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400822
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500823 case XCB_FOCUS_IN:
824 focus_in = (xcb_focus_in_event_t *) event;
825 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
826 break;
827
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400828 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500829 break;
830
831 case XCB_FOCUS_OUT:
832 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -0500833 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
834 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500835 break;
Daniel Stoned6da09e2012-06-22 13:21:29 +0100836 notify_keyboard_focus_out(&c->base.seat->seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500837 break;
838
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500839 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400840 break;
841 }
842
Bill Spitzakb715cec2012-06-05 17:08:23 -0400843 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400844 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400845 free (event);
846 }
847
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400848 switch (prev ? prev->response_type & ~0x80 : 0x80) {
849 case XCB_KEY_RELEASE:
850 key_release = (xcb_key_press_event_t *) prev;
Daniel Stone37816df2012-05-16 18:45:18 +0100851 notify_key(&c->base.seat->seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500852 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100853 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100854 WL_KEYBOARD_KEY_STATE_RELEASED,
855 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400856 free(prev);
857 prev = NULL;
858 break;
859 default:
860 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500861 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400862
Bill Spitzakb715cec2012-06-05 17:08:23 -0400863 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -0400864}
865
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400866#define F(field) offsetof(struct x11_compositor, field)
867
868static void
869x11_compositor_get_resources(struct x11_compositor *c)
870{
871 static const struct { const char *name; int offset; } atoms[] = {
872 { "WM_PROTOCOLS", F(atom.wm_protocols) },
873 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
874 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
875 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500876 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400877 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500878 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400879 { "_NET_WM_STATE", F(atom.net_wm_state) },
880 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500881 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400882 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500883 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +0100884 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400885 };
886
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500887 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400888 xcb_intern_atom_reply_t *reply;
889 xcb_pixmap_t pixmap;
890 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400891 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400892 uint8_t data[] = { 0, 0, 0, 0 };
893
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500894 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400895 cookies[i] = xcb_intern_atom (c->conn, 0,
896 strlen(atoms[i].name),
897 atoms[i].name);
898
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500899 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400900 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
901 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
902 free(reply);
903 }
904
905 pixmap = xcb_generate_id(c->conn);
906 gc = xcb_generate_id(c->conn);
907 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
908 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
909 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
910 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
911 c->null_cursor = xcb_generate_id(c->conn);
912 xcb_create_cursor (c->conn, c->null_cursor,
913 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
914 xcb_free_gc(c->conn, gc);
915 xcb_free_pixmap(c->conn, pixmap);
916}
917
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500918static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500919x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500920{
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200921 struct x11_compositor *compositor = (struct x11_compositor *)ec;
Matt Roper361d2ad2011-08-29 13:52:23 -0700922
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200923 wl_event_source_remove(compositor->xcb_source);
924 x11_input_destroy(compositor);
925
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500926 weston_compositor_shutdown(ec); /* destroys outputs, too */
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200927
928 x11_compositor_fini_egl(compositor);
929
930 XCloseDisplay(compositor->dpy);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500931 free(ec);
932}
933
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500934static struct weston_compositor *
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400935x11_compositor_create(struct wl_display *display,
Daniel Stonebaf43592012-06-01 11:11:10 -0400936 int width, int height, int count, int fullscreen,
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300937 int no_input,
Daniel Stonec1be8e52012-06-01 11:14:02 -0400938 int argc, char *argv[], const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400939{
940 struct x11_compositor *c;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400941 xcb_screen_iterator_t s;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700942 int i, x;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400943
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400944 weston_log("initializing x11 backend\n");
945
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400946 c = malloc(sizeof *c);
947 if (c == NULL)
948 return NULL;
949
950 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100951
952 c->dpy = XOpenDisplay(NULL);
Cyril Brulebois20798292011-04-06 18:05:40 +0200953 if (c->dpy == NULL)
954 return NULL;
955
Benjamin Franzke3b288af2011-02-20 19:58:42 +0100956 c->conn = XGetXCBConnection(c->dpy);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +0100957 XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
958
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400959 if (xcb_connection_has_error(c->conn))
960 return NULL;
961
962 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
963 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500964 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400965
966 x11_compositor_get_resources(c);
967
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400968 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +0200969 if (x11_compositor_init_egl(c) < 0)
Darxus55973f22010-11-22 21:24:39 -0500970 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400971
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500972 c->base.destroy = x11_destroy;
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500973
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400974 /* Can't init base class until we have a current egl context */
Daniel Stonec1be8e52012-06-01 11:14:02 -0400975 if (weston_compositor_init(&c->base, display, argc, argv,
976 config_file) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -0400977 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400978
Daniel Stone22815f92012-06-22 13:21:34 +0100979 if (x11_input_create(c, no_input) < 0)
980 return NULL;
981
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700982 for (i = 0, x = 0; i < count; i++) {
983 if (x11_compositor_create_output(c, x, 0, width, height,
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300984 fullscreen, no_input) < 0)
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700985 return NULL;
986 x += width;
987 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400988
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400989 c->xcb_source =
Kristian Høgsberg22ba60e2012-03-12 01:18:24 -0400990 wl_event_loop_add_fd(c->base.input_loop,
991 xcb_get_file_descriptor(c->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400992 WL_EVENT_READABLE,
993 x11_compositor_handle_event, c);
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400994 wl_event_source_check(c->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400995
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400996 return &c->base;
997}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400998
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500999WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -04001000backend_init(struct wl_display *display, int argc, char *argv[],
1001 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001002{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001003 int width = 1024, height = 640, fullscreen = 0, count = 1;
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001004 int no_input = 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001005
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001006 const struct weston_option x11_options[] = {
1007 { WESTON_OPTION_INTEGER, "width", 0, &width },
1008 { WESTON_OPTION_INTEGER, "height", 0, &height },
1009 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
1010 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001011 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001012 };
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001013
1014 parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001015
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001016 return x11_compositor_create(display,
Daniel Stonebaf43592012-06-01 11:11:10 -04001017 width, height, count, fullscreen,
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001018 no_input,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001019 argc, argv, config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001020}