blob: b79e477cf6cad739c94fa0a0aef2c29d51fcce9f [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"
39#include "xserver-server-protocol.h"
40#include "hash.h"
41
42struct motif_wm_hints {
43 uint32_t flags;
44 uint32_t functions;
45 uint32_t decorations;
46 int32_t input_mode;
47 uint32_t status;
48};
49
50#define MWM_HINTS_FUNCTIONS (1L << 0)
51#define MWM_HINTS_DECORATIONS (1L << 1)
52#define MWM_HINTS_INPUT_MODE (1L << 2)
53#define MWM_HINTS_STATUS (1L << 3)
54
55#define MWM_FUNC_ALL (1L << 0)
56#define MWM_FUNC_RESIZE (1L << 1)
57#define MWM_FUNC_MOVE (1L << 2)
58#define MWM_FUNC_MINIMIZE (1L << 3)
59#define MWM_FUNC_MAXIMIZE (1L << 4)
60#define MWM_FUNC_CLOSE (1L << 5)
61
62#define MWM_DECOR_ALL (1L << 0)
63#define MWM_DECOR_BORDER (1L << 1)
64#define MWM_DECOR_RESIZEH (1L << 2)
65#define MWM_DECOR_TITLE (1L << 3)
66#define MWM_DECOR_MENU (1L << 4)
67#define MWM_DECOR_MINIMIZE (1L << 5)
68#define MWM_DECOR_MAXIMIZE (1L << 6)
69
70#define MWM_INPUT_MODELESS 0
71#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
72#define MWM_INPUT_SYSTEM_MODAL 2
73#define MWM_INPUT_FULL_APPLICATION_MODAL 3
74#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
75
76#define MWM_TEAROFF_WINDOW (1L<<0)
77
78#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
79#define _NET_WM_MOVERESIZE_SIZE_TOP 1
80#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
81#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
82#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
83#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
84#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
85#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
86#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
87#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
88#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
89#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
90
91
92
93struct weston_wm_window {
94 struct weston_wm *wm;
95 xcb_window_t id;
96 xcb_window_t frame_id;
97 cairo_surface_t *cairo_surface;
98 struct weston_surface *surface;
99 struct shell_surface *shsurf;
100 struct wl_listener surface_destroy_listener;
101 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400102 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400103 int properties_dirty;
104 char *class;
105 char *name;
106 struct weston_wm_window *transient_for;
107 uint32_t protocols;
108 xcb_atom_t type;
109 int width, height;
110 int x, y;
111 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300112 int override_redirect;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400113};
114
115static struct weston_wm_window *
116get_wm_window(struct weston_surface *surface);
117
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400118static void
119weston_wm_window_schedule_repaint(struct weston_wm_window *window);
120
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400121const char *
122get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
123{
124 xcb_get_atom_name_cookie_t cookie;
125 xcb_get_atom_name_reply_t *reply;
126 xcb_generic_error_t *e;
127 static char buffer[64];
128
129 if (atom == XCB_ATOM_NONE)
130 return "None";
131
132 cookie = xcb_get_atom_name (c, atom);
133 reply = xcb_get_atom_name_reply (c, cookie, &e);
134 snprintf(buffer, sizeof buffer, "%.*s",
135 xcb_get_atom_name_name_length (reply),
136 xcb_get_atom_name_name (reply));
137 free(reply);
138
139 return buffer;
140}
141
142void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400143dump_property(struct weston_wm *wm,
144 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400145{
146 int32_t *incr_value;
147 const char *text_value, *name;
148 xcb_atom_t *atom_value;
149 int width, len;
150 uint32_t i;
151
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400152 width = fprintf(stderr, "%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400153 if (reply == NULL) {
154 fprintf(stderr, "(no reply)\n");
155 return;
156 }
157
158 width += fprintf(stderr,
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400159 "%s/%d, length %d (value_len %d): ",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400160 get_atom_name(wm->conn, reply->type),
161 reply->format,
162 xcb_get_property_value_length(reply),
163 reply->value_len);
164
165 if (reply->type == wm->atom.incr) {
166 incr_value = xcb_get_property_value(reply);
167 fprintf(stderr, "%d\n", *incr_value);
168 } else if (reply->type == wm->atom.utf8_string ||
169 reply->type == wm->atom.string) {
170 text_value = xcb_get_property_value(reply);
171 if (reply->value_len > 40)
172 len = 40;
173 else
174 len = reply->value_len;
175 fprintf(stderr, "\"%.*s\"\n", len, text_value);
176 } else if (reply->type == XCB_ATOM_ATOM) {
177 atom_value = xcb_get_property_value(reply);
178 for (i = 0; i < reply->value_len; i++) {
179 name = get_atom_name(wm->conn, atom_value[i]);
180 if (width + strlen(name) + 2 > 78) {
181 fprintf(stderr, "\n ");
182 width = 4;
183 } else if (i > 0) {
184 width += fprintf(stderr, ", ");
185 }
186
187 width += fprintf(stderr, "%s", name);
188 }
189 fprintf(stderr, "\n");
190 } else {
191 fprintf(stderr, "huh?\n");
192 }
193}
194
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400195void
196read_and_dump_property(struct weston_wm *wm,
197 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400198{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400199 xcb_get_property_reply_t *reply;
200 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400201
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400202 cookie = xcb_get_property(wm->conn, 0, window,
203 property, XCB_ATOM_ANY, 0, 2048);
204 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400205
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400206 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400207
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400208 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400209}
210
211/* We reuse some predefined, but otherwise useles atoms */
212#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
213#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
214
215static void
216weston_wm_window_read_properties(struct weston_wm_window *window)
217{
218 struct weston_wm *wm = window->wm;
219
220#define F(field) offsetof(struct weston_wm_window, field)
221 const struct {
222 xcb_atom_t atom;
223 xcb_atom_t type;
224 int offset;
225 } props[] = {
226 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
227 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
228 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
229 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
230 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
231 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
232 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
233 };
234#undef F
235
236 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
237 xcb_get_property_reply_t *reply;
238 void *p;
239 uint32_t *xid;
240 xcb_atom_t *atom;
241 uint32_t i;
242 struct motif_wm_hints *hints;
243
244 if (!window->properties_dirty)
245 return;
246 window->properties_dirty = 0;
247
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400248 for (i = 0; i < ARRAY_LENGTH(props); i++)
249 cookie[i] = xcb_get_property(wm->conn,
250 0, /* delete */
251 window->id,
252 props[i].atom,
253 XCB_ATOM_ANY, 0, 2048);
254
255 window->decorate = 1;
256 for (i = 0; i < ARRAY_LENGTH(props); i++) {
257 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
258 if (!reply)
259 /* Bad window, typically */
260 continue;
261 if (reply->type == XCB_ATOM_NONE) {
262 /* No such property */
263 free(reply);
264 continue;
265 }
266
267 p = ((char *) window + props[i].offset);
268
269 switch (props[i].type) {
270 case XCB_ATOM_STRING:
271 /* FIXME: We're using this for both string and
272 utf8_string */
273 if (*(char **) p)
274 free(*(char **) p);
275
276 *(char **) p =
277 strndup(xcb_get_property_value(reply),
278 xcb_get_property_value_length(reply));
279 break;
280 case XCB_ATOM_WINDOW:
281 xid = xcb_get_property_value(reply);
282 *(struct weston_wm_window **) p =
283 hash_table_lookup(wm->window_hash, *xid);
284 break;
285 case XCB_ATOM_ATOM:
286 atom = xcb_get_property_value(reply);
287 *(xcb_atom_t *) p = *atom;
288 break;
289 case TYPE_WM_PROTOCOLS:
290 break;
291 case TYPE_MOTIF_WM_HINTS:
292 hints = xcb_get_property_value(reply);
293 if (hints->flags & MWM_HINTS_DECORATIONS)
294 window->decorate = hints->decorations > 0;
295 break;
296 default:
297 break;
298 }
299 free(reply);
300 }
301}
302
303static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400304weston_wm_window_get_frame_size(struct weston_wm_window *window,
305 int *width, int *height)
306{
307 struct theme *t = window->wm->theme;
308
309 if (window->decorate) {
310 *width = window->width + (t->margin + t->width) * 2;
311 *height = window->height +
312 t->margin * 2 + t->width + t->titlebar_height;
313 } else {
314 *width = window->width + t->margin * 2;
315 *height = window->height + t->margin * 2;
316 }
317}
318
319static void
320weston_wm_window_get_child_position(struct weston_wm_window *window,
321 int *x, int *y)
322{
323 struct theme *t = window->wm->theme;
324
325 if (window->decorate) {
326 *x = t->margin + t->width;
327 *y = t->margin + t->titlebar_height;
328 } else {
329 *x = t->margin;
330 *y = t->margin;
331 }
332}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400333
334static void
335weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
336{
337 xcb_configure_request_event_t *configure_request =
338 (xcb_configure_request_event_t *) event;
339 struct weston_wm_window *window;
340 uint32_t mask, values[16];
341 int x, y, width, height, i = 0;
342
343 fprintf(stderr, "XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
344 configure_request->window,
345 configure_request->x, configure_request->y,
346 configure_request->width, configure_request->height);
347
348 window = hash_table_lookup(wm->window_hash, configure_request->window);
349
350 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
351 window->width = configure_request->width;
352 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
353 window->height = configure_request->height;
354
355 weston_wm_window_get_child_position(window, &x, &y);
356 values[i++] = x;
357 values[i++] = y;
358 values[i++] = window->width;
359 values[i++] = window->height;
360 values[i++] = 0;
361 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
362 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
363 XCB_CONFIG_WINDOW_BORDER_WIDTH;
364 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
365 values[i++] = configure_request->sibling;
366 mask |= XCB_CONFIG_WINDOW_SIBLING;
367 }
368 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
369 values[i++] = configure_request->stack_mode;
370 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
371 }
372
373 xcb_configure_window(wm->conn, window->id, mask, values);
374
375 weston_wm_window_get_frame_size(window, &width, &height);
376 values[0] = width;
377 values[1] = height;
378 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
379 xcb_configure_window(wm->conn, window->frame_id, mask, values);
380
381 weston_wm_window_schedule_repaint(window);
382}
383
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400384static void
385weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
386{
387 xcb_configure_notify_event_t *configure_notify =
388 (xcb_configure_notify_event_t *) event;
389 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400390
391 window = hash_table_lookup(wm->window_hash, configure_notify->window);
392
393 fprintf(stderr, "XCB_CONFIGURE_NOTIFY (%s window %d) %d,%d @ %dx%d\n",
394 configure_notify->window == window->id ? "client" : "frame",
395 configure_notify->window,
396 configure_notify->x, configure_notify->y,
397 configure_notify->width, configure_notify->height);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400398}
399
400static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400401weston_wm_window_activate(struct wl_listener *listener, void *data)
402{
403 struct weston_surface *surface = data;
404 struct weston_wm_window *window = get_wm_window(surface);
Scott Moreau85ecac02012-05-21 15:49:14 -0600405 struct weston_wm *wm =
406 container_of(listener, struct weston_wm, activate_listener);
407 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400408
Scott Moreau85ecac02012-05-21 15:49:14 -0600409 if (window) {
410 client_message.response_type = XCB_CLIENT_MESSAGE;
411 client_message.format = 32;
412 client_message.window = window->id;
413 client_message.type = wm->atom.wm_protocols;
414 client_message.data.data32[0] = wm->atom.wm_take_focus;
415 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
416
417 xcb_send_event(wm->conn, 0, window->id,
418 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
419 (char *) &client_message);
420
421 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
422 window->id, XCB_TIME_CURRENT_TIME);
423 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400424 xcb_set_input_focus (wm->conn,
425 XCB_INPUT_FOCUS_POINTER_ROOT,
426 XCB_NONE,
427 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600428 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400429
430 if (wm->focus_window)
431 weston_wm_window_schedule_repaint(wm->focus_window);
432 wm->focus_window = window;
433 if (wm->focus_window)
434 weston_wm_window_schedule_repaint(wm->focus_window);
435}
436
437static int
438our_resource(struct weston_wm *wm, uint32_t id)
439{
440 const xcb_setup_t *setup;
441
442 setup = xcb_get_setup(wm->conn);
443
444 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
445}
446
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400447#define ICCCM_WITHDRAWN_STATE 0
448#define ICCCM_NORMAL_STATE 1
449#define ICCCM_ICONIC_STATE 3
450
451static void
452weston_wm_window_set_state(struct weston_wm_window *window, int32_t state)
453{
454 struct weston_wm *wm = window->wm;
455 uint32_t property[2];
456
457 property[0] = state;
458 property[1] = XCB_WINDOW_NONE;
459
460 xcb_change_property(wm->conn,
461 XCB_PROP_MODE_REPLACE,
462 window->id,
463 wm->atom.wm_state,
464 wm->atom.wm_state,
465 32, /* format */
466 2, property);
467}
468
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400469static void
470weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
471{
472 xcb_map_request_event_t *map_request =
473 (xcb_map_request_event_t *) event;
474 struct weston_wm_window *window;
475 uint32_t values[1];
476 int x, y, width, height;
477
478 if (our_resource(wm, map_request->window)) {
479 fprintf(stderr, "XCB_MAP_REQUEST (window %d, ours)\n",
480 map_request->window);
481 return;
482 }
483
484 window = hash_table_lookup(wm->window_hash, map_request->window);
485
Kristian Høgsbergbc6e1622012-05-30 09:59:56 -0400486 if (window->frame_id)
487 return;
488
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400489 weston_wm_window_read_properties(window);
490
491 weston_wm_window_get_frame_size(window, &width, &height);
492 weston_wm_window_get_child_position(window, &x, &y);
493
494 values[0] =
495 XCB_EVENT_MASK_KEY_PRESS |
496 XCB_EVENT_MASK_KEY_RELEASE |
497 XCB_EVENT_MASK_BUTTON_PRESS |
498 XCB_EVENT_MASK_BUTTON_RELEASE |
499 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400500 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400501
502 window->frame_id = xcb_generate_id(wm->conn);
503 xcb_create_window(wm->conn,
504 XCB_COPY_FROM_PARENT,
505 window->frame_id,
506 wm->screen->root,
507 0, 0,
508 width, height,
509 0,
510 XCB_WINDOW_CLASS_INPUT_OUTPUT,
511 wm->screen->root_visual,
512 XCB_CW_EVENT_MASK, values);
513 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
514
515 values[0] = 0;
516 xcb_configure_window(wm->conn, window->id,
517 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
518
519 fprintf(stderr, "XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
520 window->id, window, window->frame_id);
521
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400522 xcb_map_window(wm->conn, map_request->window);
523 xcb_map_window(wm->conn, window->frame_id);
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400524 weston_wm_window_set_state(window, ICCCM_NORMAL_STATE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400525
526 window->cairo_surface =
527 cairo_xcb_surface_create_with_xrender_format(wm->conn,
528 wm->screen,
529 window->frame_id,
530 &wm->render_format,
531 width, height);
532
533 hash_table_insert(wm->window_hash, window->frame_id, window);
534}
535
536static void
537weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
538{
539 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
540
541 if (our_resource(wm, map_notify->window)) {
542 fprintf(stderr, "XCB_MAP_NOTIFY (window %d, ours)\n",
543 map_notify->window);
544 return;
545 }
546
547 fprintf(stderr, "XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
548}
549
550static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400551weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
552{
553 xcb_unmap_notify_event_t *unmap_notify =
554 (xcb_unmap_notify_event_t *) event;
555 struct weston_wm_window *window;
556
557 fprintf(stderr,
558 "XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
559 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
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400680 fprintf(stderr, "XCB_PROPERTY_NOTIFY: window %d, ",
681 property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400682 if (property_notify->state == XCB_PROPERTY_DELETE)
683 fprintf(stderr, "deleted\n");
684 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) {
702 fprintf(stderr, "failed to allocate window\n");
703 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
733 fprintf(stderr,
734 "XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
735 create_notify->window,
736 create_notify->width, create_notify->height,
737 create_notify->override_redirect ? ", override" : "",
738 our_resource(wm, create_notify->window) ? ", ours" : "");
739
740 if (our_resource(wm, create_notify->window))
741 return;
742
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400743 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300744 create_notify->width, create_notify->height,
745 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400746}
747
748static void
749weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
750{
751 xcb_destroy_notify_event_t *destroy_notify =
752 (xcb_destroy_notify_event_t *) event;
753 struct weston_wm_window *window;
754
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400755 fprintf(stderr, "XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
756 destroy_notify->window,
757 destroy_notify->event,
758 our_resource(wm, destroy_notify->window) ? ", ours" : "");
759
760 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400761 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400762
763 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400764 weston_wm_window_destroy(window);
765}
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400766
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400767static void
768weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
769{
770 xcb_reparent_notify_event_t *reparent_notify =
771 (xcb_reparent_notify_event_t *) event;
772 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -0400773
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400774 fprintf(stderr,
775 "XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
776 reparent_notify->window,
777 reparent_notify->parent,
778 reparent_notify->event);
779
780 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +0300781 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
782 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400783 } else if (!our_resource(wm, reparent_notify->parent)) {
784 window = hash_table_lookup(wm->window_hash,
785 reparent_notify->window);
786 weston_wm_window_destroy(window);
787 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400788}
789
790static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400791weston_wm_window_handle_moveresize(struct weston_wm_window *window,
792 xcb_client_message_event_t *client_message)
793{
794 static const int map[] = {
795 THEME_LOCATION_RESIZING_TOP_LEFT,
796 THEME_LOCATION_RESIZING_TOP,
797 THEME_LOCATION_RESIZING_TOP_RIGHT,
798 THEME_LOCATION_RESIZING_RIGHT,
799 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
800 THEME_LOCATION_RESIZING_BOTTOM,
801 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
802 THEME_LOCATION_RESIZING_LEFT
803 };
804
805 struct weston_wm *wm = window->wm;
806 struct weston_seat *seat = wm->server->compositor->seat;
807 int detail;
808 struct weston_shell_interface *shell_interface =
809 &wm->server->compositor->shell_interface;
810
811 if (seat->seat.pointer->button_count != 1 ||
812 seat->seat.pointer->focus != &window->surface->surface)
813 return;
814
815 detail = client_message->data.data32[2];
816 switch (detail) {
817 case _NET_WM_MOVERESIZE_MOVE:
818 shell_interface->move(window->shsurf, seat);
819 break;
820 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
821 case _NET_WM_MOVERESIZE_SIZE_TOP:
822 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
823 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
824 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
825 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
826 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
827 case _NET_WM_MOVERESIZE_SIZE_LEFT:
828 shell_interface->resize(window->shsurf, seat, map[detail]);
829 break;
830 case _NET_WM_MOVERESIZE_CANCEL:
831 break;
832 }
833}
834
835static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400836weston_wm_handle_client_message(struct weston_wm *wm,
837 xcb_generic_event_t *event)
838{
839 xcb_client_message_event_t *client_message =
840 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400841 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400842
843 window = hash_table_lookup(wm->window_hash, client_message->window);
844
845 fprintf(stderr, "XCB_CLIENT_MESSAGE (%s %d %d %d %d %d)\n",
846 get_atom_name(wm->conn, client_message->type),
847 client_message->data.data32[0],
848 client_message->data.data32[1],
849 client_message->data.data32[2],
850 client_message->data.data32[3],
851 client_message->data.data32[4]);
852
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400853 if (client_message->type == wm->atom.net_wm_moveresize)
854 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400855}
856
857static void
858weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
859{
860 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
861 struct weston_shell_interface *shell_interface =
862 &wm->server->compositor->shell_interface;
863 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400864 enum theme_location location;
865 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400866 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400867
868 fprintf(stderr, "XCB_BUTTON_%s (detail %d)\n",
869 button->response_type == XCB_BUTTON_PRESS ?
870 "PRESS" : "RELEASE", button->detail);
871
872 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400873 weston_wm_window_get_frame_size(window, &width, &height);
874
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400875 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400876 button->detail == 1) {
877 location = theme_get_location(t,
878 button->event_x,
879 button->event_y,
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400880 width, height);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400881
882 switch (location) {
883 case THEME_LOCATION_TITLEBAR:
884 shell_interface->move(window->shsurf,
885 wm->server->compositor->seat);
886 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400887 case THEME_LOCATION_RESIZING_TOP:
888 case THEME_LOCATION_RESIZING_BOTTOM:
889 case THEME_LOCATION_RESIZING_LEFT:
890 case THEME_LOCATION_RESIZING_RIGHT:
891 case THEME_LOCATION_RESIZING_TOP_LEFT:
892 case THEME_LOCATION_RESIZING_TOP_RIGHT:
893 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
894 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
895 shell_interface->resize(window->shsurf,
896 wm->server->compositor->seat,
897 location);
898 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400899 default:
900 break;
901 }
902 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400903}
904
905static int
906weston_wm_handle_event(int fd, uint32_t mask, void *data)
907{
908 struct weston_wm *wm = data;
909 xcb_generic_event_t *event;
910 int count = 0;
911
912 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
913 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400914 free(event);
915 count++;
916 continue;
917 }
918
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400919 switch (event->response_type & ~0x80) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400920 case XCB_BUTTON_PRESS:
921 case XCB_BUTTON_RELEASE:
922 weston_wm_handle_button(wm, event);
923 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400924 case XCB_CREATE_NOTIFY:
925 weston_wm_handle_create_notify(wm, event);
926 break;
927 case XCB_MAP_REQUEST:
928 weston_wm_handle_map_request(wm, event);
929 break;
930 case XCB_MAP_NOTIFY:
931 weston_wm_handle_map_notify(wm, event);
932 break;
933 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400934 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400935 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400936 case XCB_REPARENT_NOTIFY:
937 weston_wm_handle_reparent_notify(wm, event);
938 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400939 case XCB_CONFIGURE_REQUEST:
940 weston_wm_handle_configure_request(wm, event);
941 break;
942 case XCB_CONFIGURE_NOTIFY:
943 weston_wm_handle_configure_notify(wm, event);
944 break;
945 case XCB_DESTROY_NOTIFY:
946 weston_wm_handle_destroy_notify(wm, event);
947 break;
948 case XCB_MAPPING_NOTIFY:
949 fprintf(stderr, "XCB_MAPPING_NOTIFY\n");
950 break;
951 case XCB_PROPERTY_NOTIFY:
952 weston_wm_handle_property_notify(wm, event);
953 break;
954 case XCB_CLIENT_MESSAGE:
955 weston_wm_handle_client_message(wm, event);
956 break;
957 }
958
959 free(event);
960 count++;
961 }
962
963 xcb_flush(wm->conn);
964
965 return count;
966}
967
968static void
969wxs_wm_get_resources(struct weston_wm *wm)
970{
971
972#define F(field) offsetof(struct weston_wm, field)
973
974 static const struct { const char *name; int offset; } atoms[] = {
975 { "WM_PROTOCOLS", F(atom.wm_protocols) },
976 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
977 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400978 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -0400979 { "WM_S0", F(atom.wm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400980 { "_NET_WM_NAME", F(atom.net_wm_name) },
981 { "_NET_WM_ICON", F(atom.net_wm_icon) },
982 { "_NET_WM_STATE", F(atom.net_wm_state) },
983 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
984 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
985 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
986 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
987
988 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
989 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
990 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
991 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
992 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
993 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
994 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
995 { "_NET_WM_WINDOW_TYPE_DROPDOWN", F(atom.net_wm_window_type_dropdown) },
996 { "_NET_WM_WINDOW_TYPE_POPUP", F(atom.net_wm_window_type_popup) },
997 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
998 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
999 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1000 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1001 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1002
1003 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1004 { "_NET_SUPPORTING_WM_CHECK",
1005 F(atom.net_supporting_wm_check) },
1006 { "_NET_SUPPORTED", F(atom.net_supported) },
1007 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1008 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001009 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001010 { "TARGETS", F(atom.targets) },
1011 { "UTF8_STRING", F(atom.utf8_string) },
1012 { "_WL_SELECTION", F(atom.wl_selection) },
1013 { "INCR", F(atom.incr) },
1014 { "TIMESTAMP", F(atom.timestamp) },
1015 { "MULTIPLE", F(atom.multiple) },
1016 { "UTF8_STRING" , F(atom.utf8_string) },
1017 { "COMPOUND_TEXT", F(atom.compound_text) },
1018 { "TEXT", F(atom.text) },
1019 { "STRING", F(atom.string) },
1020 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1021 { "text/plain", F(atom.text_plain) },
1022 };
1023#undef F
1024
1025 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1026 xcb_xfixes_query_version_reply_t *xfixes_reply;
1027 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1028 xcb_intern_atom_reply_t *reply;
1029 xcb_render_query_pict_formats_reply_t *formats_reply;
1030 xcb_render_query_pict_formats_cookie_t formats_cookie;
1031 xcb_render_pictforminfo_t *formats;
1032 uint32_t i;
1033
1034 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1035
1036 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1037
1038 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1039 cookies[i] = xcb_intern_atom (wm->conn, 0,
1040 strlen(atoms[i].name),
1041 atoms[i].name);
1042
1043 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1044 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1045 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1046 free(reply);
1047 }
1048
1049 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1050 if (!wm->xfixes || !wm->xfixes->present)
1051 fprintf(stderr, "xfixes not available\n");
1052
1053 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1054 XCB_XFIXES_MAJOR_VERSION,
1055 XCB_XFIXES_MINOR_VERSION);
1056 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1057 xfixes_cookie, NULL);
1058
1059 printf("xfixes version: %d.%d\n",
1060 xfixes_reply->major_version, xfixes_reply->minor_version);
1061
1062 free(xfixes_reply);
1063
1064 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1065 formats_cookie, 0);
1066 if (formats_reply == NULL)
1067 return;
1068
1069 formats = xcb_render_query_pict_formats_formats(formats_reply);
1070 for (i = 0; i < formats_reply->length; i++)
1071 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1072 formats[i].depth == 24)
1073 wm->render_format = formats[i];
1074
1075 free(formats_reply);
1076}
1077
1078static void
1079weston_wm_create_wm_window(struct weston_wm *wm)
1080{
1081 static const char name[] = "Weston WM";
1082
1083 wm->wm_window = xcb_generate_id(wm->conn);
1084 xcb_create_window(wm->conn,
1085 XCB_COPY_FROM_PARENT,
1086 wm->wm_window,
1087 wm->screen->root,
1088 0, 0,
1089 10, 10,
1090 0,
1091 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1092 wm->screen->root_visual,
1093 0, NULL);
1094
1095 xcb_change_property(wm->conn,
1096 XCB_PROP_MODE_REPLACE,
1097 wm->wm_window,
1098 wm->atom.net_supporting_wm_check,
1099 XCB_ATOM_WINDOW,
1100 32, /* format */
1101 1, &wm->wm_window);
1102
1103 xcb_change_property(wm->conn,
1104 XCB_PROP_MODE_REPLACE,
1105 wm->wm_window,
1106 wm->atom.net_wm_name,
1107 wm->atom.utf8_string,
1108 8, /* format */
1109 strlen(name), name);
1110
1111 xcb_change_property(wm->conn,
1112 XCB_PROP_MODE_REPLACE,
1113 wm->screen->root,
1114 wm->atom.net_supporting_wm_check,
1115 XCB_ATOM_WINDOW,
1116 32, /* format */
1117 1, &wm->wm_window);
1118
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001119 /* Claim the WM_S0 selection even though we don't suport
1120 * the --replace functionality. */
1121 xcb_set_selection_owner(wm->conn,
1122 wm->wm_window,
1123 wm->atom.wm_s0,
1124 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001125}
1126
1127struct weston_wm *
1128weston_wm_create(struct weston_xserver *wxs)
1129{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001130 struct weston_wm *wm;
1131 struct wl_event_loop *loop;
1132 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001133 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001134 int sv[2];
1135 xcb_atom_t supported[1];
1136
1137 wm = malloc(sizeof *wm);
1138 if (wm == NULL)
1139 return NULL;
1140
1141 memset(wm, 0, sizeof *wm);
1142 wm->server = wxs;
1143 wm->window_hash = hash_table_create();
1144 if (wm->window_hash == NULL) {
1145 free(wm);
1146 return NULL;
1147 }
1148
1149 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
1150 fprintf(stderr, "socketpair failed\n");
1151 hash_table_destroy(wm->window_hash);
1152 free(wm);
1153 return NULL;
1154 }
1155
1156 xserver_send_client(wxs->resource, sv[1]);
1157 wl_client_flush(wxs->resource->client);
1158 close(sv[1]);
1159
1160 /* xcb_connect_to_fd takes ownership of the fd. */
1161 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1162 if (xcb_connection_has_error(wm->conn)) {
1163 fprintf(stderr, "xcb_connect_to_fd failed\n");
1164 close(sv[0]);
1165 hash_table_destroy(wm->window_hash);
1166 free(wm);
1167 return NULL;
1168 }
1169
1170 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1171 wm->screen = s.data;
1172
1173 loop = wl_display_get_event_loop(wxs->wl_display);
1174 wm->source =
1175 wl_event_loop_add_fd(loop, sv[0],
1176 WL_EVENT_READABLE,
1177 weston_wm_handle_event, wm);
1178 wl_event_source_check(wm->source);
1179
1180 wxs_wm_get_resources(wm);
1181
1182 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001183 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1184 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1185 XCB_EVENT_MASK_PROPERTY_CHANGE;
1186 xcb_change_window_attributes(wm->conn, wm->screen->root,
1187 XCB_CW_EVENT_MASK, values);
1188 wm->theme = theme_create();
1189
1190 weston_wm_create_wm_window(wm);
1191
1192 supported[0] = wm->atom.net_wm_moveresize;
1193 xcb_change_property(wm->conn,
1194 XCB_PROP_MODE_REPLACE,
1195 wm->screen->root,
1196 wm->atom.net_supported,
1197 XCB_ATOM_ATOM,
1198 32, /* format */
1199 ARRAY_LENGTH(supported), supported);
1200
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001201 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001202
1203 xcb_flush(wm->conn);
1204
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001205 wm->activate_listener.notify = weston_wm_window_activate;
1206 wl_signal_add(&wxs->compositor->activate_signal,
1207 &wm->activate_listener);
1208
1209 fprintf(stderr, "created wm\n");
1210
1211 return wm;
1212}
1213
1214void
1215weston_wm_destroy(struct weston_wm *wm)
1216{
1217 /* FIXME: Free windows in hash. */
1218 hash_table_destroy(wm->window_hash);
1219 xcb_disconnect(wm->conn);
1220 wl_event_source_remove(wm->source);
1221 wl_list_remove(&wm->selection_listener.link);
1222 wl_list_remove(&wm->activate_listener.link);
1223
1224 free(wm);
1225}
1226
1227static void
1228surface_destroy(struct wl_listener *listener, void *data)
1229{
1230 struct weston_wm_window *window =
1231 container_of(listener,
1232 struct weston_wm_window, surface_destroy_listener);
1233
1234 fprintf(stderr, "surface for xid %d destroyed\n", window->id);
1235}
1236
1237static struct weston_wm_window *
1238get_wm_window(struct weston_surface *surface)
1239{
1240 struct wl_resource *resource = &surface->surface.resource;
1241 struct wl_listener *listener;
1242
1243 listener = wl_signal_get(&resource->destroy_signal, surface_destroy);
1244 if (listener)
1245 return container_of(listener, struct weston_wm_window,
1246 surface_destroy_listener);
1247
1248 return NULL;
1249}
1250
1251static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001252weston_wm_window_configure(void *data)
1253{
1254 struct weston_wm_window *window = data;
1255 struct weston_wm *wm = window->wm;
1256 uint32_t values[2];
1257 int width, height;
1258
1259 values[0] = window->width;
1260 values[1] = window->height;
1261 xcb_configure_window(wm->conn,
1262 window->id,
1263 XCB_CONFIG_WINDOW_WIDTH |
1264 XCB_CONFIG_WINDOW_HEIGHT,
1265 values);
1266
1267 weston_wm_window_get_frame_size(window, &width, &height);
1268 values[0] = width;
1269 values[1] = height;
1270 xcb_configure_window(wm->conn,
1271 window->frame_id,
1272 XCB_CONFIG_WINDOW_WIDTH |
1273 XCB_CONFIG_WINDOW_HEIGHT,
1274 values);
1275
1276 window->configure_source = NULL;
1277
1278 weston_wm_window_schedule_repaint(window);
1279}
1280
1281static void
1282send_configure(struct weston_surface *surface,
1283 uint32_t edges, int32_t width, int32_t height)
1284{
1285 struct weston_wm_window *window = get_wm_window(surface);
1286 struct weston_wm *wm = window->wm;
1287 struct theme *t = window->wm->theme;
1288
1289 if (window->decorate) {
1290 window->width = width - 2 * (t->margin + t->width);
1291 window->height = height - 2 * t->margin -
1292 t->titlebar_height - t->width;
1293 } else {
1294 window->width = width - 2 * t->margin;
1295 window->height = height - 2 * t->margin;
1296 }
1297
1298 if (window->configure_source)
1299 return;
1300
1301 window->configure_source =
1302 wl_event_loop_add_idle(wm->server->loop,
1303 weston_wm_window_configure, window);
1304}
1305
1306static const struct weston_shell_client shell_client = {
1307 send_configure
1308};
1309
1310static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001311xserver_map_shell_surface(struct weston_wm *wm,
1312 struct weston_wm_window *window)
1313{
1314 struct weston_shell_interface *shell_interface =
1315 &wm->server->compositor->shell_interface;
1316 struct weston_wm_window *parent;
1317 struct theme *t = window->wm->theme;
1318
1319 if (!shell_interface->create_shell_surface)
1320 return;
1321
1322 window->shsurf =
1323 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001324 window->surface,
1325 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001326
Tiago Vignatti771241e2012-06-04 20:01:45 +03001327 /* ICCCM 4.1.1 */
1328 if (!window->override_redirect) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001329 shell_interface->set_toplevel(window->shsurf);
1330 return;
1331 }
1332
1333 parent = hash_table_lookup(wm->window_hash, window->transient_for->id);
1334 shell_interface->set_transient(window->shsurf, parent->shsurf,
1335 window->x - parent->x + t->margin + t->width,
1336 window->y - parent->y + t->margin + t->titlebar_height,
1337 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1338}
1339
1340static void
1341xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1342 struct wl_resource *surface_resource, uint32_t id)
1343{
1344 struct weston_xserver *wxs = resource->data;
1345 struct weston_wm *wm = wxs->wm;
1346 struct wl_surface *surface = surface_resource->data;
1347 struct weston_wm_window *window;
1348
1349 if (client != wxs->client)
1350 return;
1351
1352 window = hash_table_lookup(wm->window_hash, id);
1353 if (window == NULL) {
1354 fprintf(stderr, "set_window_id for unknown window %d\n", id);
1355 return;
1356 }
1357
1358 fprintf(stderr, "set_window_id %d for surface %p\n", id, surface);
1359
1360 weston_wm_window_read_properties(window);
1361
1362 window->surface = (struct weston_surface *) surface;
1363 window->surface_destroy_listener.notify = surface_destroy;
1364 wl_signal_add(&surface->resource.destroy_signal,
1365 &window->surface_destroy_listener);
1366
1367 weston_wm_window_schedule_repaint(window);
1368 xserver_map_shell_surface(wm, window);
1369}
1370
1371const struct xserver_interface xserver_implementation = {
1372 xserver_set_window_id
1373};