blob: da4a4afc712916de23ad76ae2d71bd023c701727 [file] [log] [blame]
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
23#define _GNU_SOURCE
24
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <sys/socket.h>
29#include <sys/un.h>
30#include <fcntl.h>
31#include <errno.h>
32#include <unistd.h>
33#include <signal.h>
34
35#include "xwayland.h"
36
37#include "../../shared/cairo-util.h"
38#include "../compositor.h"
Martin Minarik6d118362012-06-07 18:01:59 +020039#include "../log.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040040#include "xserver-server-protocol.h"
41#include "hash.h"
42
43struct motif_wm_hints {
44 uint32_t flags;
45 uint32_t functions;
46 uint32_t decorations;
47 int32_t input_mode;
48 uint32_t status;
49};
50
51#define MWM_HINTS_FUNCTIONS (1L << 0)
52#define MWM_HINTS_DECORATIONS (1L << 1)
53#define MWM_HINTS_INPUT_MODE (1L << 2)
54#define MWM_HINTS_STATUS (1L << 3)
55
56#define MWM_FUNC_ALL (1L << 0)
57#define MWM_FUNC_RESIZE (1L << 1)
58#define MWM_FUNC_MOVE (1L << 2)
59#define MWM_FUNC_MINIMIZE (1L << 3)
60#define MWM_FUNC_MAXIMIZE (1L << 4)
61#define MWM_FUNC_CLOSE (1L << 5)
62
63#define MWM_DECOR_ALL (1L << 0)
64#define MWM_DECOR_BORDER (1L << 1)
65#define MWM_DECOR_RESIZEH (1L << 2)
66#define MWM_DECOR_TITLE (1L << 3)
67#define MWM_DECOR_MENU (1L << 4)
68#define MWM_DECOR_MINIMIZE (1L << 5)
69#define MWM_DECOR_MAXIMIZE (1L << 6)
70
71#define MWM_INPUT_MODELESS 0
72#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
73#define MWM_INPUT_SYSTEM_MODAL 2
74#define MWM_INPUT_FULL_APPLICATION_MODAL 3
75#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
76
77#define MWM_TEAROFF_WINDOW (1L<<0)
78
79#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
80#define _NET_WM_MOVERESIZE_SIZE_TOP 1
81#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
82#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
83#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
84#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
85#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
86#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
87#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
88#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
89#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
90#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
91
92
93
94struct weston_wm_window {
95 struct weston_wm *wm;
96 xcb_window_t id;
97 xcb_window_t frame_id;
98 cairo_surface_t *cairo_surface;
99 struct weston_surface *surface;
100 struct shell_surface *shsurf;
101 struct wl_listener surface_destroy_listener;
102 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400103 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400104 int properties_dirty;
105 char *class;
106 char *name;
107 struct weston_wm_window *transient_for;
108 uint32_t protocols;
109 xcb_atom_t type;
110 int width, height;
111 int x, y;
112 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300113 int override_redirect;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400114};
115
116static struct weston_wm_window *
117get_wm_window(struct weston_surface *surface);
118
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400119static void
120weston_wm_window_schedule_repaint(struct weston_wm_window *window);
121
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400122const char *
123get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
124{
125 xcb_get_atom_name_cookie_t cookie;
126 xcb_get_atom_name_reply_t *reply;
127 xcb_generic_error_t *e;
128 static char buffer[64];
129
130 if (atom == XCB_ATOM_NONE)
131 return "None";
132
133 cookie = xcb_get_atom_name (c, atom);
134 reply = xcb_get_atom_name_reply (c, cookie, &e);
135 snprintf(buffer, sizeof buffer, "%.*s",
136 xcb_get_atom_name_name_length (reply),
137 xcb_get_atom_name_name (reply));
138 free(reply);
139
140 return buffer;
141}
142
143void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400144dump_property(struct weston_wm *wm,
145 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400146{
147 int32_t *incr_value;
148 const char *text_value, *name;
149 xcb_atom_t *atom_value;
150 int width, len;
151 uint32_t i;
152
Martin Minarik6d118362012-06-07 18:01:59 +0200153 width = weston_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400154 if (reply == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200155 weston_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400156 return;
157 }
158
Martin Minarik6d118362012-06-07 18:01:59 +0200159 width += weston_log_continue(
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400160 "%s/%d, length %d (value_len %d): ",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400161 get_atom_name(wm->conn, reply->type),
162 reply->format,
163 xcb_get_property_value_length(reply),
164 reply->value_len);
165
166 if (reply->type == wm->atom.incr) {
167 incr_value = xcb_get_property_value(reply);
Martin Minarik6d118362012-06-07 18:01:59 +0200168 weston_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400169 } else if (reply->type == wm->atom.utf8_string ||
170 reply->type == wm->atom.string) {
171 text_value = xcb_get_property_value(reply);
172 if (reply->value_len > 40)
173 len = 40;
174 else
175 len = reply->value_len;
Martin Minarik6d118362012-06-07 18:01:59 +0200176 weston_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400177 } else if (reply->type == XCB_ATOM_ATOM) {
178 atom_value = xcb_get_property_value(reply);
179 for (i = 0; i < reply->value_len; i++) {
180 name = get_atom_name(wm->conn, atom_value[i]);
181 if (width + strlen(name) + 2 > 78) {
Martin Minarik6d118362012-06-07 18:01:59 +0200182 weston_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400183 width = 4;
184 } else if (i > 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200185 width += weston_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400186 }
187
Martin Minarik6d118362012-06-07 18:01:59 +0200188 width += weston_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400189 }
Martin Minarik6d118362012-06-07 18:01:59 +0200190 weston_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400191 } else {
Martin Minarik6d118362012-06-07 18:01:59 +0200192 weston_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400193 }
194}
195
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400196void
197read_and_dump_property(struct weston_wm *wm,
198 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400199{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400200 xcb_get_property_reply_t *reply;
201 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400202
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400203 cookie = xcb_get_property(wm->conn, 0, window,
204 property, XCB_ATOM_ANY, 0, 2048);
205 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400206
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400207 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400208
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400209 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400210}
211
212/* We reuse some predefined, but otherwise useles atoms */
213#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
214#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
215
216static void
217weston_wm_window_read_properties(struct weston_wm_window *window)
218{
219 struct weston_wm *wm = window->wm;
220
221#define F(field) offsetof(struct weston_wm_window, field)
222 const struct {
223 xcb_atom_t atom;
224 xcb_atom_t type;
225 int offset;
226 } props[] = {
227 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
228 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
229 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
230 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
231 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
232 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
233 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
234 };
235#undef F
236
237 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
238 xcb_get_property_reply_t *reply;
239 void *p;
240 uint32_t *xid;
241 xcb_atom_t *atom;
242 uint32_t i;
243 struct motif_wm_hints *hints;
244
245 if (!window->properties_dirty)
246 return;
247 window->properties_dirty = 0;
248
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400249 for (i = 0; i < ARRAY_LENGTH(props); i++)
250 cookie[i] = xcb_get_property(wm->conn,
251 0, /* delete */
252 window->id,
253 props[i].atom,
254 XCB_ATOM_ANY, 0, 2048);
255
256 window->decorate = 1;
257 for (i = 0; i < ARRAY_LENGTH(props); i++) {
258 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
259 if (!reply)
260 /* Bad window, typically */
261 continue;
262 if (reply->type == XCB_ATOM_NONE) {
263 /* No such property */
264 free(reply);
265 continue;
266 }
267
268 p = ((char *) window + props[i].offset);
269
270 switch (props[i].type) {
271 case XCB_ATOM_STRING:
272 /* FIXME: We're using this for both string and
273 utf8_string */
274 if (*(char **) p)
275 free(*(char **) p);
276
277 *(char **) p =
278 strndup(xcb_get_property_value(reply),
279 xcb_get_property_value_length(reply));
280 break;
281 case XCB_ATOM_WINDOW:
282 xid = xcb_get_property_value(reply);
283 *(struct weston_wm_window **) p =
284 hash_table_lookup(wm->window_hash, *xid);
285 break;
286 case XCB_ATOM_ATOM:
287 atom = xcb_get_property_value(reply);
288 *(xcb_atom_t *) p = *atom;
289 break;
290 case TYPE_WM_PROTOCOLS:
291 break;
292 case TYPE_MOTIF_WM_HINTS:
293 hints = xcb_get_property_value(reply);
294 if (hints->flags & MWM_HINTS_DECORATIONS)
295 window->decorate = hints->decorations > 0;
296 break;
297 default:
298 break;
299 }
300 free(reply);
301 }
302}
303
304static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400305weston_wm_window_get_frame_size(struct weston_wm_window *window,
306 int *width, int *height)
307{
308 struct theme *t = window->wm->theme;
309
310 if (window->decorate) {
311 *width = window->width + (t->margin + t->width) * 2;
312 *height = window->height +
313 t->margin * 2 + t->width + t->titlebar_height;
314 } else {
315 *width = window->width + t->margin * 2;
316 *height = window->height + t->margin * 2;
317 }
318}
319
320static void
321weston_wm_window_get_child_position(struct weston_wm_window *window,
322 int *x, int *y)
323{
324 struct theme *t = window->wm->theme;
325
326 if (window->decorate) {
327 *x = t->margin + t->width;
328 *y = t->margin + t->titlebar_height;
329 } else {
330 *x = t->margin;
331 *y = t->margin;
332 }
333}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400334
335static void
336weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
337{
338 xcb_configure_request_event_t *configure_request =
339 (xcb_configure_request_event_t *) event;
340 struct weston_wm_window *window;
341 uint32_t mask, values[16];
342 int x, y, width, height, i = 0;
343
Martin Minarik6d118362012-06-07 18:01:59 +0200344 weston_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400345 configure_request->window,
346 configure_request->x, configure_request->y,
347 configure_request->width, configure_request->height);
348
349 window = hash_table_lookup(wm->window_hash, configure_request->window);
350
351 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
352 window->width = configure_request->width;
353 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
354 window->height = configure_request->height;
355
356 weston_wm_window_get_child_position(window, &x, &y);
357 values[i++] = x;
358 values[i++] = y;
359 values[i++] = window->width;
360 values[i++] = window->height;
361 values[i++] = 0;
362 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
363 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
364 XCB_CONFIG_WINDOW_BORDER_WIDTH;
365 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
366 values[i++] = configure_request->sibling;
367 mask |= XCB_CONFIG_WINDOW_SIBLING;
368 }
369 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
370 values[i++] = configure_request->stack_mode;
371 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
372 }
373
374 xcb_configure_window(wm->conn, window->id, mask, values);
375
376 weston_wm_window_get_frame_size(window, &width, &height);
377 values[0] = width;
378 values[1] = height;
379 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
380 xcb_configure_window(wm->conn, window->frame_id, mask, values);
381
382 weston_wm_window_schedule_repaint(window);
383}
384
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400385static void
386weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
387{
388 xcb_configure_notify_event_t *configure_notify =
389 (xcb_configure_notify_event_t *) event;
390 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400391
392 window = hash_table_lookup(wm->window_hash, configure_notify->window);
393
Martin Minarik6d118362012-06-07 18:01:59 +0200394 weston_log("XCB_CONFIGURE_NOTIFY (%s window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400395 configure_notify->window == window->id ? "client" : "frame",
396 configure_notify->window,
397 configure_notify->x, configure_notify->y,
398 configure_notify->width, configure_notify->height);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400399}
400
401static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400402weston_wm_window_activate(struct wl_listener *listener, void *data)
403{
404 struct weston_surface *surface = data;
405 struct weston_wm_window *window = get_wm_window(surface);
Scott Moreau85ecac02012-05-21 15:49:14 -0600406 struct weston_wm *wm =
407 container_of(listener, struct weston_wm, activate_listener);
408 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400409
Scott Moreau85ecac02012-05-21 15:49:14 -0600410 if (window) {
411 client_message.response_type = XCB_CLIENT_MESSAGE;
412 client_message.format = 32;
413 client_message.window = window->id;
414 client_message.type = wm->atom.wm_protocols;
415 client_message.data.data32[0] = wm->atom.wm_take_focus;
416 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
417
418 xcb_send_event(wm->conn, 0, window->id,
419 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
420 (char *) &client_message);
421
422 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
423 window->id, XCB_TIME_CURRENT_TIME);
424 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400425 xcb_set_input_focus (wm->conn,
426 XCB_INPUT_FOCUS_POINTER_ROOT,
427 XCB_NONE,
428 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600429 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400430
431 if (wm->focus_window)
432 weston_wm_window_schedule_repaint(wm->focus_window);
433 wm->focus_window = window;
434 if (wm->focus_window)
435 weston_wm_window_schedule_repaint(wm->focus_window);
436}
437
438static int
439our_resource(struct weston_wm *wm, uint32_t id)
440{
441 const xcb_setup_t *setup;
442
443 setup = xcb_get_setup(wm->conn);
444
445 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
446}
447
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400448#define ICCCM_WITHDRAWN_STATE 0
449#define ICCCM_NORMAL_STATE 1
450#define ICCCM_ICONIC_STATE 3
451
452static void
453weston_wm_window_set_state(struct weston_wm_window *window, int32_t state)
454{
455 struct weston_wm *wm = window->wm;
456 uint32_t property[2];
457
458 property[0] = state;
459 property[1] = XCB_WINDOW_NONE;
460
461 xcb_change_property(wm->conn,
462 XCB_PROP_MODE_REPLACE,
463 window->id,
464 wm->atom.wm_state,
465 wm->atom.wm_state,
466 32, /* format */
467 2, property);
468}
469
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400470static void
471weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
472{
473 xcb_map_request_event_t *map_request =
474 (xcb_map_request_event_t *) event;
475 struct weston_wm_window *window;
476 uint32_t values[1];
477 int x, y, width, height;
478
479 if (our_resource(wm, map_request->window)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200480 weston_log("XCB_MAP_REQUEST (window %d, ours)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400481 map_request->window);
482 return;
483 }
484
485 window = hash_table_lookup(wm->window_hash, map_request->window);
486
Kristian Høgsbergbc6e1622012-05-30 09:59:56 -0400487 if (window->frame_id)
488 return;
489
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400490 weston_wm_window_read_properties(window);
491
492 weston_wm_window_get_frame_size(window, &width, &height);
493 weston_wm_window_get_child_position(window, &x, &y);
494
495 values[0] =
496 XCB_EVENT_MASK_KEY_PRESS |
497 XCB_EVENT_MASK_KEY_RELEASE |
498 XCB_EVENT_MASK_BUTTON_PRESS |
499 XCB_EVENT_MASK_BUTTON_RELEASE |
500 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400501 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400502
503 window->frame_id = xcb_generate_id(wm->conn);
504 xcb_create_window(wm->conn,
505 XCB_COPY_FROM_PARENT,
506 window->frame_id,
507 wm->screen->root,
508 0, 0,
509 width, height,
510 0,
511 XCB_WINDOW_CLASS_INPUT_OUTPUT,
512 wm->screen->root_visual,
513 XCB_CW_EVENT_MASK, values);
514 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
515
516 values[0] = 0;
517 xcb_configure_window(wm->conn, window->id,
518 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
519
Martin Minarik6d118362012-06-07 18:01:59 +0200520 weston_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400521 window->id, window, window->frame_id);
522
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400523 xcb_map_window(wm->conn, map_request->window);
524 xcb_map_window(wm->conn, window->frame_id);
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400525 weston_wm_window_set_state(window, ICCCM_NORMAL_STATE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400526
527 window->cairo_surface =
528 cairo_xcb_surface_create_with_xrender_format(wm->conn,
529 wm->screen,
530 window->frame_id,
531 &wm->render_format,
532 width, height);
533
534 hash_table_insert(wm->window_hash, window->frame_id, window);
535}
536
537static void
538weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
539{
540 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
541
542 if (our_resource(wm, map_notify->window)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200543 weston_log("XCB_MAP_NOTIFY (window %d, ours)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400544 map_notify->window);
545 return;
546 }
547
Martin Minarik6d118362012-06-07 18:01:59 +0200548 weston_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400549}
550
551static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400552weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
553{
554 xcb_unmap_notify_event_t *unmap_notify =
555 (xcb_unmap_notify_event_t *) event;
556 struct weston_wm_window *window;
557
Martin Minarik6d118362012-06-07 18:01:59 +0200558 weston_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400559 unmap_notify->window,
560 unmap_notify->event,
561 our_resource(wm, unmap_notify->window) ? ", ours" : "");
562
563 if (our_resource(wm, unmap_notify->window))
564 return;
565
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400566 if (unmap_notify->response_type & 0x80)
567 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
568 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400569 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400570
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400571 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400572 if (window->repaint_source)
573 wl_event_source_remove(window->repaint_source);
574 if (window->cairo_surface)
575 cairo_surface_destroy(window->cairo_surface);
576
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400577 if (window->frame_id) {
578 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
579 xcb_destroy_window(wm->conn, window->frame_id);
580 weston_wm_window_set_state(window, ICCCM_WITHDRAWN_STATE);
581 hash_table_remove(wm->window_hash, window->frame_id);
582 window->frame_id = XCB_WINDOW_NONE;
583 }
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400584
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400585 if (wm->focus_window == window)
586 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400587 if (window->surface)
588 wl_list_remove(&window->surface_destroy_listener.link);
589 window->surface = NULL;
590}
591
592static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400593weston_wm_window_draw_decoration(void *data)
594{
595 struct weston_wm_window *window = data;
596 struct weston_wm *wm = window->wm;
597 struct theme *t = wm->theme;
598 cairo_t *cr;
599 int x, y, width, height;
600 const char *title;
601 uint32_t flags = 0;
602
603 weston_wm_window_read_properties(window);
604
605 window->repaint_source = NULL;
606
607 weston_wm_window_get_frame_size(window, &width, &height);
608 weston_wm_window_get_child_position(window, &x, &y);
609
610 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
611 cr = cairo_create(window->cairo_surface);
612
613 if (window->decorate) {
614 if (wm->focus_window == window)
615 flags |= THEME_FRAME_ACTIVE;
616
617 if (window->name)
618 title = window->name;
619 else
620 title = "untitled";
621
622 theme_render_frame(t, cr, width, height, title, flags);
623 } else {
624 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
625 cairo_set_source_rgba(cr, 0, 0, 0, 0);
626 cairo_paint(cr);
627
628 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
629 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
630 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
631 }
632
633 cairo_destroy(cr);
634
635 if (window->surface) {
636 /* We leave an extra pixel around the X window area to
637 * make sure we don't sample from the undefined alpha
638 * channel when filtering. */
639 window->surface->opaque_rect[0] =
640 (double) (x - 1) / width;
641 window->surface->opaque_rect[1] =
642 (double) (x + window->width + 1) / width;
643 window->surface->opaque_rect[2] =
644 (double) (y - 1) / height;
645 window->surface->opaque_rect[3] =
646 (double) (y + window->height + 1) / height;
647
648 pixman_region32_init_rect(&window->surface->input,
649 t->margin, t->margin,
650 width - 2 * t->margin,
651 height - 2 * t->margin);
652 }
653}
654
655static void
656weston_wm_window_schedule_repaint(struct weston_wm_window *window)
657{
658 struct weston_wm *wm = window->wm;
659
660 if (window->frame_id == XCB_WINDOW_NONE || window->repaint_source)
661 return;
662
663 window->repaint_source =
664 wl_event_loop_add_idle(wm->server->loop,
665 weston_wm_window_draw_decoration,
666 window);
667}
668
669static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400670weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
671{
672 xcb_property_notify_event_t *property_notify =
673 (xcb_property_notify_event_t *) event;
674 struct weston_wm_window *window;
675
676 window = hash_table_lookup(wm->window_hash, property_notify->window);
677 if (window)
678 window->properties_dirty = 1;
679
Martin Minarik6d118362012-06-07 18:01:59 +0200680 weston_log("XCB_PROPERTY_NOTIFY: window %d, ",
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400681 property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400682 if (property_notify->state == XCB_PROPERTY_DELETE)
Martin Minarik6d118362012-06-07 18:01:59 +0200683 weston_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400684 else
685 read_and_dump_property(wm, property_notify->window,
686 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400687
688 if (property_notify->atom == wm->atom.net_wm_name ||
689 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400690 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400691}
692
693static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400694weston_wm_window_create(struct weston_wm *wm,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300695 xcb_window_t id, int width, int height, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400696{
697 struct weston_wm_window *window;
698 uint32_t values[1];
699
700 window = malloc(sizeof *window);
701 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200702 weston_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400703 return;
704 }
705
706 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
707 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
708
709 memset(window, 0, sizeof *window);
710 window->wm = wm;
711 window->id = id;
712 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300713 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400714 window->width = width;
715 window->height = height;
716
717 hash_table_insert(wm->window_hash, id, window);
718}
719
720static void
721weston_wm_window_destroy(struct weston_wm_window *window)
722{
723 hash_table_remove(window->wm->window_hash, window->id);
724 free(window);
725}
726
727static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400728weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
729{
730 xcb_create_notify_event_t *create_notify =
731 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400732
Martin Minarik6d118362012-06-07 18:01:59 +0200733 weston_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400734 create_notify->window,
735 create_notify->width, create_notify->height,
736 create_notify->override_redirect ? ", override" : "",
737 our_resource(wm, create_notify->window) ? ", ours" : "");
738
739 if (our_resource(wm, create_notify->window))
740 return;
741
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400742 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300743 create_notify->width, create_notify->height,
744 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400745}
746
747static void
748weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
749{
750 xcb_destroy_notify_event_t *destroy_notify =
751 (xcb_destroy_notify_event_t *) event;
752 struct weston_wm_window *window;
753
Martin Minarik6d118362012-06-07 18:01:59 +0200754 weston_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400755 destroy_notify->window,
756 destroy_notify->event,
757 our_resource(wm, destroy_notify->window) ? ", ours" : "");
758
759 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400760 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400761
762 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400763 weston_wm_window_destroy(window);
764}
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400765
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400766static void
767weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
768{
769 xcb_reparent_notify_event_t *reparent_notify =
770 (xcb_reparent_notify_event_t *) event;
771 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -0400772
Martin Minarik6d118362012-06-07 18:01:59 +0200773 weston_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400774 reparent_notify->window,
775 reparent_notify->parent,
776 reparent_notify->event);
777
778 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +0300779 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
780 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400781 } else if (!our_resource(wm, reparent_notify->parent)) {
782 window = hash_table_lookup(wm->window_hash,
783 reparent_notify->window);
784 weston_wm_window_destroy(window);
785 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400786}
787
788static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400789weston_wm_window_handle_moveresize(struct weston_wm_window *window,
790 xcb_client_message_event_t *client_message)
791{
792 static const int map[] = {
793 THEME_LOCATION_RESIZING_TOP_LEFT,
794 THEME_LOCATION_RESIZING_TOP,
795 THEME_LOCATION_RESIZING_TOP_RIGHT,
796 THEME_LOCATION_RESIZING_RIGHT,
797 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
798 THEME_LOCATION_RESIZING_BOTTOM,
799 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
800 THEME_LOCATION_RESIZING_LEFT
801 };
802
803 struct weston_wm *wm = window->wm;
804 struct weston_seat *seat = wm->server->compositor->seat;
805 int detail;
806 struct weston_shell_interface *shell_interface =
807 &wm->server->compositor->shell_interface;
808
809 if (seat->seat.pointer->button_count != 1 ||
810 seat->seat.pointer->focus != &window->surface->surface)
811 return;
812
813 detail = client_message->data.data32[2];
814 switch (detail) {
815 case _NET_WM_MOVERESIZE_MOVE:
816 shell_interface->move(window->shsurf, seat);
817 break;
818 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
819 case _NET_WM_MOVERESIZE_SIZE_TOP:
820 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
821 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
822 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
823 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
824 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
825 case _NET_WM_MOVERESIZE_SIZE_LEFT:
826 shell_interface->resize(window->shsurf, seat, map[detail]);
827 break;
828 case _NET_WM_MOVERESIZE_CANCEL:
829 break;
830 }
831}
832
833static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400834weston_wm_handle_client_message(struct weston_wm *wm,
835 xcb_generic_event_t *event)
836{
837 xcb_client_message_event_t *client_message =
838 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400839 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400840
841 window = hash_table_lookup(wm->window_hash, client_message->window);
842
Martin Minarik6d118362012-06-07 18:01:59 +0200843 weston_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400844 get_atom_name(wm->conn, client_message->type),
845 client_message->data.data32[0],
846 client_message->data.data32[1],
847 client_message->data.data32[2],
848 client_message->data.data32[3],
849 client_message->data.data32[4]);
850
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400851 if (client_message->type == wm->atom.net_wm_moveresize)
852 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400853}
854
855static void
856weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
857{
858 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
859 struct weston_shell_interface *shell_interface =
860 &wm->server->compositor->shell_interface;
861 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400862 enum theme_location location;
863 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400864 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400865
Martin Minarik6d118362012-06-07 18:01:59 +0200866 weston_log("XCB_BUTTON_%s (detail %d)\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400867 button->response_type == XCB_BUTTON_PRESS ?
868 "PRESS" : "RELEASE", button->detail);
869
870 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400871 weston_wm_window_get_frame_size(window, &width, &height);
872
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400873 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400874 button->detail == 1) {
875 location = theme_get_location(t,
876 button->event_x,
877 button->event_y,
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400878 width, height);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400879
880 switch (location) {
881 case THEME_LOCATION_TITLEBAR:
882 shell_interface->move(window->shsurf,
883 wm->server->compositor->seat);
884 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400885 case THEME_LOCATION_RESIZING_TOP:
886 case THEME_LOCATION_RESIZING_BOTTOM:
887 case THEME_LOCATION_RESIZING_LEFT:
888 case THEME_LOCATION_RESIZING_RIGHT:
889 case THEME_LOCATION_RESIZING_TOP_LEFT:
890 case THEME_LOCATION_RESIZING_TOP_RIGHT:
891 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
892 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
893 shell_interface->resize(window->shsurf,
894 wm->server->compositor->seat,
895 location);
896 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400897 default:
898 break;
899 }
900 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400901}
902
903static int
904weston_wm_handle_event(int fd, uint32_t mask, void *data)
905{
906 struct weston_wm *wm = data;
907 xcb_generic_event_t *event;
908 int count = 0;
909
910 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
911 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400912 free(event);
913 count++;
914 continue;
915 }
916
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400917 switch (event->response_type & ~0x80) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400918 case XCB_BUTTON_PRESS:
919 case XCB_BUTTON_RELEASE:
920 weston_wm_handle_button(wm, event);
921 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400922 case XCB_CREATE_NOTIFY:
923 weston_wm_handle_create_notify(wm, event);
924 break;
925 case XCB_MAP_REQUEST:
926 weston_wm_handle_map_request(wm, event);
927 break;
928 case XCB_MAP_NOTIFY:
929 weston_wm_handle_map_notify(wm, event);
930 break;
931 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400932 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400933 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400934 case XCB_REPARENT_NOTIFY:
935 weston_wm_handle_reparent_notify(wm, event);
936 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400937 case XCB_CONFIGURE_REQUEST:
938 weston_wm_handle_configure_request(wm, event);
939 break;
940 case XCB_CONFIGURE_NOTIFY:
941 weston_wm_handle_configure_notify(wm, event);
942 break;
943 case XCB_DESTROY_NOTIFY:
944 weston_wm_handle_destroy_notify(wm, event);
945 break;
946 case XCB_MAPPING_NOTIFY:
Martin Minarik6d118362012-06-07 18:01:59 +0200947 weston_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400948 break;
949 case XCB_PROPERTY_NOTIFY:
950 weston_wm_handle_property_notify(wm, event);
951 break;
952 case XCB_CLIENT_MESSAGE:
953 weston_wm_handle_client_message(wm, event);
954 break;
955 }
956
957 free(event);
958 count++;
959 }
960
961 xcb_flush(wm->conn);
962
963 return count;
964}
965
966static void
967wxs_wm_get_resources(struct weston_wm *wm)
968{
969
970#define F(field) offsetof(struct weston_wm, field)
971
972 static const struct { const char *name; int offset; } atoms[] = {
973 { "WM_PROTOCOLS", F(atom.wm_protocols) },
974 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
975 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400976 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -0400977 { "WM_S0", F(atom.wm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400978 { "_NET_WM_NAME", F(atom.net_wm_name) },
979 { "_NET_WM_ICON", F(atom.net_wm_icon) },
980 { "_NET_WM_STATE", F(atom.net_wm_state) },
981 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
982 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
983 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
984 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
985
986 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
987 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
988 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
989 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
990 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
991 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
992 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +0300993 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
994 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400995 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
996 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
997 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
998 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
999 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1000
1001 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1002 { "_NET_SUPPORTING_WM_CHECK",
1003 F(atom.net_supporting_wm_check) },
1004 { "_NET_SUPPORTED", F(atom.net_supported) },
1005 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1006 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001007 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001008 { "TARGETS", F(atom.targets) },
1009 { "UTF8_STRING", F(atom.utf8_string) },
1010 { "_WL_SELECTION", F(atom.wl_selection) },
1011 { "INCR", F(atom.incr) },
1012 { "TIMESTAMP", F(atom.timestamp) },
1013 { "MULTIPLE", F(atom.multiple) },
1014 { "UTF8_STRING" , F(atom.utf8_string) },
1015 { "COMPOUND_TEXT", F(atom.compound_text) },
1016 { "TEXT", F(atom.text) },
1017 { "STRING", F(atom.string) },
1018 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1019 { "text/plain", F(atom.text_plain) },
1020 };
1021#undef F
1022
1023 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1024 xcb_xfixes_query_version_reply_t *xfixes_reply;
1025 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1026 xcb_intern_atom_reply_t *reply;
1027 xcb_render_query_pict_formats_reply_t *formats_reply;
1028 xcb_render_query_pict_formats_cookie_t formats_cookie;
1029 xcb_render_pictforminfo_t *formats;
1030 uint32_t i;
1031
1032 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1033
1034 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1035
1036 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1037 cookies[i] = xcb_intern_atom (wm->conn, 0,
1038 strlen(atoms[i].name),
1039 atoms[i].name);
1040
1041 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1042 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1043 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1044 free(reply);
1045 }
1046
1047 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1048 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001049 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001050
1051 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1052 XCB_XFIXES_MAJOR_VERSION,
1053 XCB_XFIXES_MINOR_VERSION);
1054 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1055 xfixes_cookie, NULL);
1056
Martin Minarik6d118362012-06-07 18:01:59 +02001057 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001058 xfixes_reply->major_version, xfixes_reply->minor_version);
1059
1060 free(xfixes_reply);
1061
1062 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1063 formats_cookie, 0);
1064 if (formats_reply == NULL)
1065 return;
1066
1067 formats = xcb_render_query_pict_formats_formats(formats_reply);
1068 for (i = 0; i < formats_reply->length; i++)
1069 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1070 formats[i].depth == 24)
1071 wm->render_format = formats[i];
1072
1073 free(formats_reply);
1074}
1075
1076static void
1077weston_wm_create_wm_window(struct weston_wm *wm)
1078{
1079 static const char name[] = "Weston WM";
1080
1081 wm->wm_window = xcb_generate_id(wm->conn);
1082 xcb_create_window(wm->conn,
1083 XCB_COPY_FROM_PARENT,
1084 wm->wm_window,
1085 wm->screen->root,
1086 0, 0,
1087 10, 10,
1088 0,
1089 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1090 wm->screen->root_visual,
1091 0, NULL);
1092
1093 xcb_change_property(wm->conn,
1094 XCB_PROP_MODE_REPLACE,
1095 wm->wm_window,
1096 wm->atom.net_supporting_wm_check,
1097 XCB_ATOM_WINDOW,
1098 32, /* format */
1099 1, &wm->wm_window);
1100
1101 xcb_change_property(wm->conn,
1102 XCB_PROP_MODE_REPLACE,
1103 wm->wm_window,
1104 wm->atom.net_wm_name,
1105 wm->atom.utf8_string,
1106 8, /* format */
1107 strlen(name), name);
1108
1109 xcb_change_property(wm->conn,
1110 XCB_PROP_MODE_REPLACE,
1111 wm->screen->root,
1112 wm->atom.net_supporting_wm_check,
1113 XCB_ATOM_WINDOW,
1114 32, /* format */
1115 1, &wm->wm_window);
1116
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001117 /* Claim the WM_S0 selection even though we don't suport
1118 * the --replace functionality. */
1119 xcb_set_selection_owner(wm->conn,
1120 wm->wm_window,
1121 wm->atom.wm_s0,
1122 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001123}
1124
1125struct weston_wm *
1126weston_wm_create(struct weston_xserver *wxs)
1127{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001128 struct weston_wm *wm;
1129 struct wl_event_loop *loop;
1130 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001131 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001132 int sv[2];
1133 xcb_atom_t supported[1];
1134
1135 wm = malloc(sizeof *wm);
1136 if (wm == NULL)
1137 return NULL;
1138
1139 memset(wm, 0, sizeof *wm);
1140 wm->server = wxs;
1141 wm->window_hash = hash_table_create();
1142 if (wm->window_hash == NULL) {
1143 free(wm);
1144 return NULL;
1145 }
1146
1147 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001148 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001149 hash_table_destroy(wm->window_hash);
1150 free(wm);
1151 return NULL;
1152 }
1153
1154 xserver_send_client(wxs->resource, sv[1]);
1155 wl_client_flush(wxs->resource->client);
1156 close(sv[1]);
1157
1158 /* xcb_connect_to_fd takes ownership of the fd. */
1159 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1160 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001161 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001162 close(sv[0]);
1163 hash_table_destroy(wm->window_hash);
1164 free(wm);
1165 return NULL;
1166 }
1167
1168 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1169 wm->screen = s.data;
1170
1171 loop = wl_display_get_event_loop(wxs->wl_display);
1172 wm->source =
1173 wl_event_loop_add_fd(loop, sv[0],
1174 WL_EVENT_READABLE,
1175 weston_wm_handle_event, wm);
1176 wl_event_source_check(wm->source);
1177
1178 wxs_wm_get_resources(wm);
1179
1180 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001181 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1182 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1183 XCB_EVENT_MASK_PROPERTY_CHANGE;
1184 xcb_change_window_attributes(wm->conn, wm->screen->root,
1185 XCB_CW_EVENT_MASK, values);
1186 wm->theme = theme_create();
1187
1188 weston_wm_create_wm_window(wm);
1189
1190 supported[0] = wm->atom.net_wm_moveresize;
1191 xcb_change_property(wm->conn,
1192 XCB_PROP_MODE_REPLACE,
1193 wm->screen->root,
1194 wm->atom.net_supported,
1195 XCB_ATOM_ATOM,
1196 32, /* format */
1197 ARRAY_LENGTH(supported), supported);
1198
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001199 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001200
1201 xcb_flush(wm->conn);
1202
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001203 wm->activate_listener.notify = weston_wm_window_activate;
1204 wl_signal_add(&wxs->compositor->activate_signal,
1205 &wm->activate_listener);
1206
Martin Minarik6d118362012-06-07 18:01:59 +02001207 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001208
1209 return wm;
1210}
1211
1212void
1213weston_wm_destroy(struct weston_wm *wm)
1214{
1215 /* FIXME: Free windows in hash. */
1216 hash_table_destroy(wm->window_hash);
1217 xcb_disconnect(wm->conn);
1218 wl_event_source_remove(wm->source);
1219 wl_list_remove(&wm->selection_listener.link);
1220 wl_list_remove(&wm->activate_listener.link);
1221
1222 free(wm);
1223}
1224
1225static void
1226surface_destroy(struct wl_listener *listener, void *data)
1227{
1228 struct weston_wm_window *window =
1229 container_of(listener,
1230 struct weston_wm_window, surface_destroy_listener);
1231
Martin Minarik6d118362012-06-07 18:01:59 +02001232 weston_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001233}
1234
1235static struct weston_wm_window *
1236get_wm_window(struct weston_surface *surface)
1237{
1238 struct wl_resource *resource = &surface->surface.resource;
1239 struct wl_listener *listener;
1240
1241 listener = wl_signal_get(&resource->destroy_signal, surface_destroy);
1242 if (listener)
1243 return container_of(listener, struct weston_wm_window,
1244 surface_destroy_listener);
1245
1246 return NULL;
1247}
1248
1249static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001250weston_wm_window_configure(void *data)
1251{
1252 struct weston_wm_window *window = data;
1253 struct weston_wm *wm = window->wm;
1254 uint32_t values[2];
1255 int width, height;
1256
1257 values[0] = window->width;
1258 values[1] = window->height;
1259 xcb_configure_window(wm->conn,
1260 window->id,
1261 XCB_CONFIG_WINDOW_WIDTH |
1262 XCB_CONFIG_WINDOW_HEIGHT,
1263 values);
1264
1265 weston_wm_window_get_frame_size(window, &width, &height);
1266 values[0] = width;
1267 values[1] = height;
1268 xcb_configure_window(wm->conn,
1269 window->frame_id,
1270 XCB_CONFIG_WINDOW_WIDTH |
1271 XCB_CONFIG_WINDOW_HEIGHT,
1272 values);
1273
1274 window->configure_source = NULL;
1275
1276 weston_wm_window_schedule_repaint(window);
1277}
1278
1279static void
1280send_configure(struct weston_surface *surface,
1281 uint32_t edges, int32_t width, int32_t height)
1282{
1283 struct weston_wm_window *window = get_wm_window(surface);
1284 struct weston_wm *wm = window->wm;
1285 struct theme *t = window->wm->theme;
1286
1287 if (window->decorate) {
1288 window->width = width - 2 * (t->margin + t->width);
1289 window->height = height - 2 * t->margin -
1290 t->titlebar_height - t->width;
1291 } else {
1292 window->width = width - 2 * t->margin;
1293 window->height = height - 2 * t->margin;
1294 }
1295
1296 if (window->configure_source)
1297 return;
1298
1299 window->configure_source =
1300 wl_event_loop_add_idle(wm->server->loop,
1301 weston_wm_window_configure, window);
1302}
1303
1304static const struct weston_shell_client shell_client = {
1305 send_configure
1306};
1307
1308static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001309xserver_map_shell_surface(struct weston_wm *wm,
1310 struct weston_wm_window *window)
1311{
1312 struct weston_shell_interface *shell_interface =
1313 &wm->server->compositor->shell_interface;
1314 struct weston_wm_window *parent;
1315 struct theme *t = window->wm->theme;
1316
1317 if (!shell_interface->create_shell_surface)
1318 return;
1319
1320 window->shsurf =
1321 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001322 window->surface,
1323 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001324
Tiago Vignatti771241e2012-06-04 20:01:45 +03001325 /* ICCCM 4.1.1 */
1326 if (!window->override_redirect) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001327 shell_interface->set_toplevel(window->shsurf);
1328 return;
1329 }
1330
1331 parent = hash_table_lookup(wm->window_hash, window->transient_for->id);
1332 shell_interface->set_transient(window->shsurf, parent->shsurf,
1333 window->x - parent->x + t->margin + t->width,
1334 window->y - parent->y + t->margin + t->titlebar_height,
1335 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1336}
1337
1338static void
1339xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1340 struct wl_resource *surface_resource, uint32_t id)
1341{
1342 struct weston_xserver *wxs = resource->data;
1343 struct weston_wm *wm = wxs->wm;
1344 struct wl_surface *surface = surface_resource->data;
1345 struct weston_wm_window *window;
1346
1347 if (client != wxs->client)
1348 return;
1349
1350 window = hash_table_lookup(wm->window_hash, id);
1351 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001352 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001353 return;
1354 }
1355
Martin Minarik6d118362012-06-07 18:01:59 +02001356 weston_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001357
1358 weston_wm_window_read_properties(window);
1359
1360 window->surface = (struct weston_surface *) surface;
1361 window->surface_destroy_listener.notify = surface_destroy;
1362 wl_signal_add(&surface->resource.destroy_signal,
1363 &window->surface_destroy_listener);
1364
1365 weston_wm_window_schedule_repaint(window);
1366 xserver_map_shell_surface(wm, window);
1367}
1368
1369const struct xserver_interface xserver_implementation = {
1370 xserver_set_window_id
1371};