blob: 3094601b3d86fe7069d2aee0bb5b33a746cb9bc2 [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;
112};
113
114static struct weston_wm_window *
115get_wm_window(struct weston_surface *surface);
116
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400117static void
118weston_wm_window_schedule_repaint(struct weston_wm_window *window);
119
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400120const char *
121get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
122{
123 xcb_get_atom_name_cookie_t cookie;
124 xcb_get_atom_name_reply_t *reply;
125 xcb_generic_error_t *e;
126 static char buffer[64];
127
128 if (atom == XCB_ATOM_NONE)
129 return "None";
130
131 cookie = xcb_get_atom_name (c, atom);
132 reply = xcb_get_atom_name_reply (c, cookie, &e);
133 snprintf(buffer, sizeof buffer, "%.*s",
134 xcb_get_atom_name_name_length (reply),
135 xcb_get_atom_name_name (reply));
136 free(reply);
137
138 return buffer;
139}
140
141void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400142dump_property(struct weston_wm *wm,
143 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400144{
145 int32_t *incr_value;
146 const char *text_value, *name;
147 xcb_atom_t *atom_value;
148 int width, len;
149 uint32_t i;
150
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400151 width = fprintf(stderr, "%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400152 if (reply == NULL) {
153 fprintf(stderr, "(no reply)\n");
154 return;
155 }
156
157 width += fprintf(stderr,
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400158 "%s/%d, length %d (value_len %d): ",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400159 get_atom_name(wm->conn, reply->type),
160 reply->format,
161 xcb_get_property_value_length(reply),
162 reply->value_len);
163
164 if (reply->type == wm->atom.incr) {
165 incr_value = xcb_get_property_value(reply);
166 fprintf(stderr, "%d\n", *incr_value);
167 } else if (reply->type == wm->atom.utf8_string ||
168 reply->type == wm->atom.string) {
169 text_value = xcb_get_property_value(reply);
170 if (reply->value_len > 40)
171 len = 40;
172 else
173 len = reply->value_len;
174 fprintf(stderr, "\"%.*s\"\n", len, text_value);
175 } else if (reply->type == XCB_ATOM_ATOM) {
176 atom_value = xcb_get_property_value(reply);
177 for (i = 0; i < reply->value_len; i++) {
178 name = get_atom_name(wm->conn, atom_value[i]);
179 if (width + strlen(name) + 2 > 78) {
180 fprintf(stderr, "\n ");
181 width = 4;
182 } else if (i > 0) {
183 width += fprintf(stderr, ", ");
184 }
185
186 width += fprintf(stderr, "%s", name);
187 }
188 fprintf(stderr, "\n");
189 } else {
190 fprintf(stderr, "huh?\n");
191 }
192}
193
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400194void
195read_and_dump_property(struct weston_wm *wm,
196 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400197{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400198 xcb_get_property_reply_t *reply;
199 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400200
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400201 cookie = xcb_get_property(wm->conn, 0, window,
202 property, XCB_ATOM_ANY, 0, 2048);
203 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400204
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400205 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400206
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400207 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400208}
209
210/* We reuse some predefined, but otherwise useles atoms */
211#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
212#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
213
214static void
215weston_wm_window_read_properties(struct weston_wm_window *window)
216{
217 struct weston_wm *wm = window->wm;
218
219#define F(field) offsetof(struct weston_wm_window, field)
220 const struct {
221 xcb_atom_t atom;
222 xcb_atom_t type;
223 int offset;
224 } props[] = {
225 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
226 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
227 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
228 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
229 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
230 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
231 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
232 };
233#undef F
234
235 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
236 xcb_get_property_reply_t *reply;
237 void *p;
238 uint32_t *xid;
239 xcb_atom_t *atom;
240 uint32_t i;
241 struct motif_wm_hints *hints;
242
243 if (!window->properties_dirty)
244 return;
245 window->properties_dirty = 0;
246
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400247 for (i = 0; i < ARRAY_LENGTH(props); i++)
248 cookie[i] = xcb_get_property(wm->conn,
249 0, /* delete */
250 window->id,
251 props[i].atom,
252 XCB_ATOM_ANY, 0, 2048);
253
254 window->decorate = 1;
255 for (i = 0; i < ARRAY_LENGTH(props); i++) {
256 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
257 if (!reply)
258 /* Bad window, typically */
259 continue;
260 if (reply->type == XCB_ATOM_NONE) {
261 /* No such property */
262 free(reply);
263 continue;
264 }
265
266 p = ((char *) window + props[i].offset);
267
268 switch (props[i].type) {
269 case XCB_ATOM_STRING:
270 /* FIXME: We're using this for both string and
271 utf8_string */
272 if (*(char **) p)
273 free(*(char **) p);
274
275 *(char **) p =
276 strndup(xcb_get_property_value(reply),
277 xcb_get_property_value_length(reply));
278 break;
279 case XCB_ATOM_WINDOW:
280 xid = xcb_get_property_value(reply);
281 *(struct weston_wm_window **) p =
282 hash_table_lookup(wm->window_hash, *xid);
283 break;
284 case XCB_ATOM_ATOM:
285 atom = xcb_get_property_value(reply);
286 *(xcb_atom_t *) p = *atom;
287 break;
288 case TYPE_WM_PROTOCOLS:
289 break;
290 case TYPE_MOTIF_WM_HINTS:
291 hints = xcb_get_property_value(reply);
292 if (hints->flags & MWM_HINTS_DECORATIONS)
293 window->decorate = hints->decorations > 0;
294 break;
295 default:
296 break;
297 }
298 free(reply);
299 }
300}
301
302static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400303weston_wm_window_get_frame_size(struct weston_wm_window *window,
304 int *width, int *height)
305{
306 struct theme *t = window->wm->theme;
307
308 if (window->decorate) {
309 *width = window->width + (t->margin + t->width) * 2;
310 *height = window->height +
311 t->margin * 2 + t->width + t->titlebar_height;
312 } else {
313 *width = window->width + t->margin * 2;
314 *height = window->height + t->margin * 2;
315 }
316}
317
318static void
319weston_wm_window_get_child_position(struct weston_wm_window *window,
320 int *x, int *y)
321{
322 struct theme *t = window->wm->theme;
323
324 if (window->decorate) {
325 *x = t->margin + t->width;
326 *y = t->margin + t->titlebar_height;
327 } else {
328 *x = t->margin;
329 *y = t->margin;
330 }
331}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400332
333static void
334weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
335{
336 xcb_configure_request_event_t *configure_request =
337 (xcb_configure_request_event_t *) event;
338 struct weston_wm_window *window;
339 uint32_t mask, values[16];
340 int x, y, width, height, i = 0;
341
342 fprintf(stderr, "XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
343 configure_request->window,
344 configure_request->x, configure_request->y,
345 configure_request->width, configure_request->height);
346
347 window = hash_table_lookup(wm->window_hash, configure_request->window);
348
349 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
350 window->width = configure_request->width;
351 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
352 window->height = configure_request->height;
353
354 weston_wm_window_get_child_position(window, &x, &y);
355 values[i++] = x;
356 values[i++] = y;
357 values[i++] = window->width;
358 values[i++] = window->height;
359 values[i++] = 0;
360 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
361 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
362 XCB_CONFIG_WINDOW_BORDER_WIDTH;
363 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
364 values[i++] = configure_request->sibling;
365 mask |= XCB_CONFIG_WINDOW_SIBLING;
366 }
367 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
368 values[i++] = configure_request->stack_mode;
369 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
370 }
371
372 xcb_configure_window(wm->conn, window->id, mask, values);
373
374 weston_wm_window_get_frame_size(window, &width, &height);
375 values[0] = width;
376 values[1] = height;
377 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
378 xcb_configure_window(wm->conn, window->frame_id, mask, values);
379
380 weston_wm_window_schedule_repaint(window);
381}
382
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400383static void
384weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
385{
386 xcb_configure_notify_event_t *configure_notify =
387 (xcb_configure_notify_event_t *) event;
388 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400389
390 window = hash_table_lookup(wm->window_hash, configure_notify->window);
391
392 fprintf(stderr, "XCB_CONFIGURE_NOTIFY (%s window %d) %d,%d @ %dx%d\n",
393 configure_notify->window == window->id ? "client" : "frame",
394 configure_notify->window,
395 configure_notify->x, configure_notify->y,
396 configure_notify->width, configure_notify->height);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400397}
398
399static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400400weston_wm_window_activate(struct wl_listener *listener, void *data)
401{
402 struct weston_surface *surface = data;
403 struct weston_wm_window *window = get_wm_window(surface);
Scott Moreau85ecac02012-05-21 15:49:14 -0600404 struct weston_wm *wm =
405 container_of(listener, struct weston_wm, activate_listener);
406 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400407
Scott Moreau85ecac02012-05-21 15:49:14 -0600408 if (window) {
409 client_message.response_type = XCB_CLIENT_MESSAGE;
410 client_message.format = 32;
411 client_message.window = window->id;
412 client_message.type = wm->atom.wm_protocols;
413 client_message.data.data32[0] = wm->atom.wm_take_focus;
414 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
415
416 xcb_send_event(wm->conn, 0, window->id,
417 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
418 (char *) &client_message);
419
420 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
421 window->id, XCB_TIME_CURRENT_TIME);
422 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400423 xcb_set_input_focus (wm->conn,
424 XCB_INPUT_FOCUS_POINTER_ROOT,
425 XCB_NONE,
426 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600427 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400428
429 if (wm->focus_window)
430 weston_wm_window_schedule_repaint(wm->focus_window);
431 wm->focus_window = window;
432 if (wm->focus_window)
433 weston_wm_window_schedule_repaint(wm->focus_window);
434}
435
436static int
437our_resource(struct weston_wm *wm, uint32_t id)
438{
439 const xcb_setup_t *setup;
440
441 setup = xcb_get_setup(wm->conn);
442
443 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
444}
445
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400446#define ICCCM_WITHDRAWN_STATE 0
447#define ICCCM_NORMAL_STATE 1
448#define ICCCM_ICONIC_STATE 3
449
450static void
451weston_wm_window_set_state(struct weston_wm_window *window, int32_t state)
452{
453 struct weston_wm *wm = window->wm;
454 uint32_t property[2];
455
456 property[0] = state;
457 property[1] = XCB_WINDOW_NONE;
458
459 xcb_change_property(wm->conn,
460 XCB_PROP_MODE_REPLACE,
461 window->id,
462 wm->atom.wm_state,
463 wm->atom.wm_state,
464 32, /* format */
465 2, property);
466}
467
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400468static void
469weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
470{
471 xcb_map_request_event_t *map_request =
472 (xcb_map_request_event_t *) event;
473 struct weston_wm_window *window;
474 uint32_t values[1];
475 int x, y, width, height;
476
477 if (our_resource(wm, map_request->window)) {
478 fprintf(stderr, "XCB_MAP_REQUEST (window %d, ours)\n",
479 map_request->window);
480 return;
481 }
482
483 window = hash_table_lookup(wm->window_hash, map_request->window);
484
Kristian Høgsbergbc6e1622012-05-30 09:59:56 -0400485 if (window->frame_id)
486 return;
487
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400488 weston_wm_window_read_properties(window);
489
490 weston_wm_window_get_frame_size(window, &width, &height);
491 weston_wm_window_get_child_position(window, &x, &y);
492
493 values[0] =
494 XCB_EVENT_MASK_KEY_PRESS |
495 XCB_EVENT_MASK_KEY_RELEASE |
496 XCB_EVENT_MASK_BUTTON_PRESS |
497 XCB_EVENT_MASK_BUTTON_RELEASE |
498 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400499 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400500
501 window->frame_id = xcb_generate_id(wm->conn);
502 xcb_create_window(wm->conn,
503 XCB_COPY_FROM_PARENT,
504 window->frame_id,
505 wm->screen->root,
506 0, 0,
507 width, height,
508 0,
509 XCB_WINDOW_CLASS_INPUT_OUTPUT,
510 wm->screen->root_visual,
511 XCB_CW_EVENT_MASK, values);
512 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
513
514 values[0] = 0;
515 xcb_configure_window(wm->conn, window->id,
516 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
517
518 fprintf(stderr, "XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
519 window->id, window, window->frame_id);
520
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400521 xcb_map_window(wm->conn, map_request->window);
522 xcb_map_window(wm->conn, window->frame_id);
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400523 weston_wm_window_set_state(window, ICCCM_NORMAL_STATE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400524
525 window->cairo_surface =
526 cairo_xcb_surface_create_with_xrender_format(wm->conn,
527 wm->screen,
528 window->frame_id,
529 &wm->render_format,
530 width, height);
531
532 hash_table_insert(wm->window_hash, window->frame_id, window);
533}
534
535static void
536weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
537{
538 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
539
540 if (our_resource(wm, map_notify->window)) {
541 fprintf(stderr, "XCB_MAP_NOTIFY (window %d, ours)\n",
542 map_notify->window);
543 return;
544 }
545
546 fprintf(stderr, "XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
547}
548
549static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400550weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
551{
552 xcb_unmap_notify_event_t *unmap_notify =
553 (xcb_unmap_notify_event_t *) event;
554 struct weston_wm_window *window;
555
556 fprintf(stderr,
557 "XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
558 unmap_notify->window,
559 unmap_notify->event,
560 our_resource(wm, unmap_notify->window) ? ", ours" : "");
561
562 if (our_resource(wm, unmap_notify->window))
563 return;
564
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400565 if (unmap_notify->response_type & 0x80)
566 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
567 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400568 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400569
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400570 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400571 if (window->repaint_source)
572 wl_event_source_remove(window->repaint_source);
573 if (window->cairo_surface)
574 cairo_surface_destroy(window->cairo_surface);
575
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400576 if (window->frame_id) {
577 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
578 xcb_destroy_window(wm->conn, window->frame_id);
579 weston_wm_window_set_state(window, ICCCM_WITHDRAWN_STATE);
580 hash_table_remove(wm->window_hash, window->frame_id);
581 window->frame_id = XCB_WINDOW_NONE;
582 }
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400583
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400584 if (wm->focus_window == window)
585 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400586 if (window->surface)
587 wl_list_remove(&window->surface_destroy_listener.link);
588 window->surface = NULL;
589}
590
591static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400592weston_wm_window_draw_decoration(void *data)
593{
594 struct weston_wm_window *window = data;
595 struct weston_wm *wm = window->wm;
596 struct theme *t = wm->theme;
597 cairo_t *cr;
598 int x, y, width, height;
599 const char *title;
600 uint32_t flags = 0;
601
602 weston_wm_window_read_properties(window);
603
604 window->repaint_source = NULL;
605
606 weston_wm_window_get_frame_size(window, &width, &height);
607 weston_wm_window_get_child_position(window, &x, &y);
608
609 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
610 cr = cairo_create(window->cairo_surface);
611
612 if (window->decorate) {
613 if (wm->focus_window == window)
614 flags |= THEME_FRAME_ACTIVE;
615
616 if (window->name)
617 title = window->name;
618 else
619 title = "untitled";
620
621 theme_render_frame(t, cr, width, height, title, flags);
622 } else {
623 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
624 cairo_set_source_rgba(cr, 0, 0, 0, 0);
625 cairo_paint(cr);
626
627 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
628 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
629 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
630 }
631
632 cairo_destroy(cr);
633
634 if (window->surface) {
635 /* We leave an extra pixel around the X window area to
636 * make sure we don't sample from the undefined alpha
637 * channel when filtering. */
638 window->surface->opaque_rect[0] =
639 (double) (x - 1) / width;
640 window->surface->opaque_rect[1] =
641 (double) (x + window->width + 1) / width;
642 window->surface->opaque_rect[2] =
643 (double) (y - 1) / height;
644 window->surface->opaque_rect[3] =
645 (double) (y + window->height + 1) / height;
646
647 pixman_region32_init_rect(&window->surface->input,
648 t->margin, t->margin,
649 width - 2 * t->margin,
650 height - 2 * t->margin);
651 }
652}
653
654static void
655weston_wm_window_schedule_repaint(struct weston_wm_window *window)
656{
657 struct weston_wm *wm = window->wm;
658
659 if (window->frame_id == XCB_WINDOW_NONE || window->repaint_source)
660 return;
661
662 window->repaint_source =
663 wl_event_loop_add_idle(wm->server->loop,
664 weston_wm_window_draw_decoration,
665 window);
666}
667
668static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400669weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
670{
671 xcb_property_notify_event_t *property_notify =
672 (xcb_property_notify_event_t *) event;
673 struct weston_wm_window *window;
674
675 window = hash_table_lookup(wm->window_hash, property_notify->window);
676 if (window)
677 window->properties_dirty = 1;
678
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400679 fprintf(stderr, "XCB_PROPERTY_NOTIFY: window %d, ",
680 property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400681 if (property_notify->state == XCB_PROPERTY_DELETE)
682 fprintf(stderr, "deleted\n");
683 else
684 read_and_dump_property(wm, property_notify->window,
685 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400686
687 if (property_notify->atom == wm->atom.net_wm_name ||
688 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400689 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400690}
691
692static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400693weston_wm_window_create(struct weston_wm *wm,
694 xcb_window_t id, int width, int height)
695{
696 struct weston_wm_window *window;
697 uint32_t values[1];
698
699 window = malloc(sizeof *window);
700 if (window == NULL) {
701 fprintf(stderr, "failed to allocate window\n");
702 return;
703 }
704
705 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
706 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
707
708 memset(window, 0, sizeof *window);
709 window->wm = wm;
710 window->id = id;
711 window->properties_dirty = 1;
712
713 window->width = width;
714 window->height = height;
715
716 hash_table_insert(wm->window_hash, id, window);
717}
718
719static void
720weston_wm_window_destroy(struct weston_wm_window *window)
721{
722 hash_table_remove(window->wm->window_hash, window->id);
723 free(window);
724}
725
726static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400727weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
728{
729 xcb_create_notify_event_t *create_notify =
730 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400731
732 fprintf(stderr,
733 "XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
734 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,
743 create_notify->width, create_notify->height);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400744}
745
746static void
747weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
748{
749 xcb_destroy_notify_event_t *destroy_notify =
750 (xcb_destroy_notify_event_t *) event;
751 struct weston_wm_window *window;
752
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400753 fprintf(stderr, "XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
754 destroy_notify->window,
755 destroy_notify->event,
756 our_resource(wm, destroy_notify->window) ? ", ours" : "");
757
758 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400759 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400760
761 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400762 weston_wm_window_destroy(window);
763}
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400764
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400765static void
766weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
767{
768 xcb_reparent_notify_event_t *reparent_notify =
769 (xcb_reparent_notify_event_t *) event;
770 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -0400771
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400772 fprintf(stderr,
773 "XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
774 reparent_notify->window,
775 reparent_notify->parent,
776 reparent_notify->event);
777
778 if (reparent_notify->parent == wm->screen->root) {
779 weston_wm_window_create(wm, reparent_notify->window, 10, 10);
780 } else if (!our_resource(wm, reparent_notify->parent)) {
781 window = hash_table_lookup(wm->window_hash,
782 reparent_notify->window);
783 weston_wm_window_destroy(window);
784 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400785}
786
787static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400788weston_wm_window_handle_moveresize(struct weston_wm_window *window,
789 xcb_client_message_event_t *client_message)
790{
791 static const int map[] = {
792 THEME_LOCATION_RESIZING_TOP_LEFT,
793 THEME_LOCATION_RESIZING_TOP,
794 THEME_LOCATION_RESIZING_TOP_RIGHT,
795 THEME_LOCATION_RESIZING_RIGHT,
796 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
797 THEME_LOCATION_RESIZING_BOTTOM,
798 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
799 THEME_LOCATION_RESIZING_LEFT
800 };
801
802 struct weston_wm *wm = window->wm;
803 struct weston_seat *seat = wm->server->compositor->seat;
804 int detail;
805 struct weston_shell_interface *shell_interface =
806 &wm->server->compositor->shell_interface;
807
808 if (seat->seat.pointer->button_count != 1 ||
809 seat->seat.pointer->focus != &window->surface->surface)
810 return;
811
812 detail = client_message->data.data32[2];
813 switch (detail) {
814 case _NET_WM_MOVERESIZE_MOVE:
815 shell_interface->move(window->shsurf, seat);
816 break;
817 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
818 case _NET_WM_MOVERESIZE_SIZE_TOP:
819 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
820 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
821 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
822 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
823 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
824 case _NET_WM_MOVERESIZE_SIZE_LEFT:
825 shell_interface->resize(window->shsurf, seat, map[detail]);
826 break;
827 case _NET_WM_MOVERESIZE_CANCEL:
828 break;
829 }
830}
831
832static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400833weston_wm_handle_client_message(struct weston_wm *wm,
834 xcb_generic_event_t *event)
835{
836 xcb_client_message_event_t *client_message =
837 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400838 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400839
840 window = hash_table_lookup(wm->window_hash, client_message->window);
841
842 fprintf(stderr, "XCB_CLIENT_MESSAGE (%s %d %d %d %d %d)\n",
843 get_atom_name(wm->conn, client_message->type),
844 client_message->data.data32[0],
845 client_message->data.data32[1],
846 client_message->data.data32[2],
847 client_message->data.data32[3],
848 client_message->data.data32[4]);
849
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400850 if (client_message->type == wm->atom.net_wm_moveresize)
851 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400852}
853
854static void
855weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
856{
857 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
858 struct weston_shell_interface *shell_interface =
859 &wm->server->compositor->shell_interface;
860 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400861 enum theme_location location;
862 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400863 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400864
865 fprintf(stderr, "XCB_BUTTON_%s (detail %d)\n",
866 button->response_type == XCB_BUTTON_PRESS ?
867 "PRESS" : "RELEASE", button->detail);
868
869 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400870 weston_wm_window_get_frame_size(window, &width, &height);
871
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400872 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400873 button->detail == 1) {
874 location = theme_get_location(t,
875 button->event_x,
876 button->event_y,
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400877 width, height);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400878
879 switch (location) {
880 case THEME_LOCATION_TITLEBAR:
881 shell_interface->move(window->shsurf,
882 wm->server->compositor->seat);
883 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400884 case THEME_LOCATION_RESIZING_TOP:
885 case THEME_LOCATION_RESIZING_BOTTOM:
886 case THEME_LOCATION_RESIZING_LEFT:
887 case THEME_LOCATION_RESIZING_RIGHT:
888 case THEME_LOCATION_RESIZING_TOP_LEFT:
889 case THEME_LOCATION_RESIZING_TOP_RIGHT:
890 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
891 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
892 shell_interface->resize(window->shsurf,
893 wm->server->compositor->seat,
894 location);
895 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400896 default:
897 break;
898 }
899 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400900}
901
902static int
903weston_wm_handle_event(int fd, uint32_t mask, void *data)
904{
905 struct weston_wm *wm = data;
906 xcb_generic_event_t *event;
907 int count = 0;
908
909 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
910 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400911 free(event);
912 count++;
913 continue;
914 }
915
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400916 switch (event->response_type & ~0x80) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400917 case XCB_BUTTON_PRESS:
918 case XCB_BUTTON_RELEASE:
919 weston_wm_handle_button(wm, event);
920 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400921 case XCB_CREATE_NOTIFY:
922 weston_wm_handle_create_notify(wm, event);
923 break;
924 case XCB_MAP_REQUEST:
925 weston_wm_handle_map_request(wm, event);
926 break;
927 case XCB_MAP_NOTIFY:
928 weston_wm_handle_map_notify(wm, event);
929 break;
930 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400931 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400932 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400933 case XCB_REPARENT_NOTIFY:
934 weston_wm_handle_reparent_notify(wm, event);
935 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400936 case XCB_CONFIGURE_REQUEST:
937 weston_wm_handle_configure_request(wm, event);
938 break;
939 case XCB_CONFIGURE_NOTIFY:
940 weston_wm_handle_configure_notify(wm, event);
941 break;
942 case XCB_DESTROY_NOTIFY:
943 weston_wm_handle_destroy_notify(wm, event);
944 break;
945 case XCB_MAPPING_NOTIFY:
946 fprintf(stderr, "XCB_MAPPING_NOTIFY\n");
947 break;
948 case XCB_PROPERTY_NOTIFY:
949 weston_wm_handle_property_notify(wm, event);
950 break;
951 case XCB_CLIENT_MESSAGE:
952 weston_wm_handle_client_message(wm, event);
953 break;
954 }
955
956 free(event);
957 count++;
958 }
959
960 xcb_flush(wm->conn);
961
962 return count;
963}
964
965static void
966wxs_wm_get_resources(struct weston_wm *wm)
967{
968
969#define F(field) offsetof(struct weston_wm, field)
970
971 static const struct { const char *name; int offset; } atoms[] = {
972 { "WM_PROTOCOLS", F(atom.wm_protocols) },
973 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
974 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400975 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -0400976 { "WM_S0", F(atom.wm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400977 { "_NET_WM_NAME", F(atom.net_wm_name) },
978 { "_NET_WM_ICON", F(atom.net_wm_icon) },
979 { "_NET_WM_STATE", F(atom.net_wm_state) },
980 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
981 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
982 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
983 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
984
985 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
986 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
987 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
988 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
989 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
990 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
991 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
992 { "_NET_WM_WINDOW_TYPE_DROPDOWN", F(atom.net_wm_window_type_dropdown) },
993 { "_NET_WM_WINDOW_TYPE_POPUP", F(atom.net_wm_window_type_popup) },
994 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
995 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
996 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
997 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
998 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
999
1000 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1001 { "_NET_SUPPORTING_WM_CHECK",
1002 F(atom.net_supporting_wm_check) },
1003 { "_NET_SUPPORTED", F(atom.net_supported) },
1004 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1005 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001006 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001007 { "TARGETS", F(atom.targets) },
1008 { "UTF8_STRING", F(atom.utf8_string) },
1009 { "_WL_SELECTION", F(atom.wl_selection) },
1010 { "INCR", F(atom.incr) },
1011 { "TIMESTAMP", F(atom.timestamp) },
1012 { "MULTIPLE", F(atom.multiple) },
1013 { "UTF8_STRING" , F(atom.utf8_string) },
1014 { "COMPOUND_TEXT", F(atom.compound_text) },
1015 { "TEXT", F(atom.text) },
1016 { "STRING", F(atom.string) },
1017 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1018 { "text/plain", F(atom.text_plain) },
1019 };
1020#undef F
1021
1022 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1023 xcb_xfixes_query_version_reply_t *xfixes_reply;
1024 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1025 xcb_intern_atom_reply_t *reply;
1026 xcb_render_query_pict_formats_reply_t *formats_reply;
1027 xcb_render_query_pict_formats_cookie_t formats_cookie;
1028 xcb_render_pictforminfo_t *formats;
1029 uint32_t i;
1030
1031 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1032
1033 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1034
1035 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1036 cookies[i] = xcb_intern_atom (wm->conn, 0,
1037 strlen(atoms[i].name),
1038 atoms[i].name);
1039
1040 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1041 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1042 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1043 free(reply);
1044 }
1045
1046 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1047 if (!wm->xfixes || !wm->xfixes->present)
1048 fprintf(stderr, "xfixes not available\n");
1049
1050 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1051 XCB_XFIXES_MAJOR_VERSION,
1052 XCB_XFIXES_MINOR_VERSION);
1053 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1054 xfixes_cookie, NULL);
1055
1056 printf("xfixes version: %d.%d\n",
1057 xfixes_reply->major_version, xfixes_reply->minor_version);
1058
1059 free(xfixes_reply);
1060
1061 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1062 formats_cookie, 0);
1063 if (formats_reply == NULL)
1064 return;
1065
1066 formats = xcb_render_query_pict_formats_formats(formats_reply);
1067 for (i = 0; i < formats_reply->length; i++)
1068 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1069 formats[i].depth == 24)
1070 wm->render_format = formats[i];
1071
1072 free(formats_reply);
1073}
1074
1075static void
1076weston_wm_create_wm_window(struct weston_wm *wm)
1077{
1078 static const char name[] = "Weston WM";
1079
1080 wm->wm_window = xcb_generate_id(wm->conn);
1081 xcb_create_window(wm->conn,
1082 XCB_COPY_FROM_PARENT,
1083 wm->wm_window,
1084 wm->screen->root,
1085 0, 0,
1086 10, 10,
1087 0,
1088 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1089 wm->screen->root_visual,
1090 0, NULL);
1091
1092 xcb_change_property(wm->conn,
1093 XCB_PROP_MODE_REPLACE,
1094 wm->wm_window,
1095 wm->atom.net_supporting_wm_check,
1096 XCB_ATOM_WINDOW,
1097 32, /* format */
1098 1, &wm->wm_window);
1099
1100 xcb_change_property(wm->conn,
1101 XCB_PROP_MODE_REPLACE,
1102 wm->wm_window,
1103 wm->atom.net_wm_name,
1104 wm->atom.utf8_string,
1105 8, /* format */
1106 strlen(name), name);
1107
1108 xcb_change_property(wm->conn,
1109 XCB_PROP_MODE_REPLACE,
1110 wm->screen->root,
1111 wm->atom.net_supporting_wm_check,
1112 XCB_ATOM_WINDOW,
1113 32, /* format */
1114 1, &wm->wm_window);
1115
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001116 /* Claim the WM_S0 selection even though we don't suport
1117 * the --replace functionality. */
1118 xcb_set_selection_owner(wm->conn,
1119 wm->wm_window,
1120 wm->atom.wm_s0,
1121 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001122}
1123
1124struct weston_wm *
1125weston_wm_create(struct weston_xserver *wxs)
1126{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001127 struct weston_wm *wm;
1128 struct wl_event_loop *loop;
1129 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001130 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001131 int sv[2];
1132 xcb_atom_t supported[1];
1133
1134 wm = malloc(sizeof *wm);
1135 if (wm == NULL)
1136 return NULL;
1137
1138 memset(wm, 0, sizeof *wm);
1139 wm->server = wxs;
1140 wm->window_hash = hash_table_create();
1141 if (wm->window_hash == NULL) {
1142 free(wm);
1143 return NULL;
1144 }
1145
1146 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
1147 fprintf(stderr, "socketpair failed\n");
1148 hash_table_destroy(wm->window_hash);
1149 free(wm);
1150 return NULL;
1151 }
1152
1153 xserver_send_client(wxs->resource, sv[1]);
1154 wl_client_flush(wxs->resource->client);
1155 close(sv[1]);
1156
1157 /* xcb_connect_to_fd takes ownership of the fd. */
1158 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1159 if (xcb_connection_has_error(wm->conn)) {
1160 fprintf(stderr, "xcb_connect_to_fd failed\n");
1161 close(sv[0]);
1162 hash_table_destroy(wm->window_hash);
1163 free(wm);
1164 return NULL;
1165 }
1166
1167 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1168 wm->screen = s.data;
1169
1170 loop = wl_display_get_event_loop(wxs->wl_display);
1171 wm->source =
1172 wl_event_loop_add_fd(loop, sv[0],
1173 WL_EVENT_READABLE,
1174 weston_wm_handle_event, wm);
1175 wl_event_source_check(wm->source);
1176
1177 wxs_wm_get_resources(wm);
1178
1179 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001180 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1181 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1182 XCB_EVENT_MASK_PROPERTY_CHANGE;
1183 xcb_change_window_attributes(wm->conn, wm->screen->root,
1184 XCB_CW_EVENT_MASK, values);
1185 wm->theme = theme_create();
1186
1187 weston_wm_create_wm_window(wm);
1188
1189 supported[0] = wm->atom.net_wm_moveresize;
1190 xcb_change_property(wm->conn,
1191 XCB_PROP_MODE_REPLACE,
1192 wm->screen->root,
1193 wm->atom.net_supported,
1194 XCB_ATOM_ATOM,
1195 32, /* format */
1196 ARRAY_LENGTH(supported), supported);
1197
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001198 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001199
1200 xcb_flush(wm->conn);
1201
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001202 wm->activate_listener.notify = weston_wm_window_activate;
1203 wl_signal_add(&wxs->compositor->activate_signal,
1204 &wm->activate_listener);
1205
1206 fprintf(stderr, "created wm\n");
1207
1208 return wm;
1209}
1210
1211void
1212weston_wm_destroy(struct weston_wm *wm)
1213{
1214 /* FIXME: Free windows in hash. */
1215 hash_table_destroy(wm->window_hash);
1216 xcb_disconnect(wm->conn);
1217 wl_event_source_remove(wm->source);
1218 wl_list_remove(&wm->selection_listener.link);
1219 wl_list_remove(&wm->activate_listener.link);
1220
1221 free(wm);
1222}
1223
1224static void
1225surface_destroy(struct wl_listener *listener, void *data)
1226{
1227 struct weston_wm_window *window =
1228 container_of(listener,
1229 struct weston_wm_window, surface_destroy_listener);
1230
1231 fprintf(stderr, "surface for xid %d destroyed\n", window->id);
1232}
1233
1234static struct weston_wm_window *
1235get_wm_window(struct weston_surface *surface)
1236{
1237 struct wl_resource *resource = &surface->surface.resource;
1238 struct wl_listener *listener;
1239
1240 listener = wl_signal_get(&resource->destroy_signal, surface_destroy);
1241 if (listener)
1242 return container_of(listener, struct weston_wm_window,
1243 surface_destroy_listener);
1244
1245 return NULL;
1246}
1247
1248static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001249weston_wm_window_configure(void *data)
1250{
1251 struct weston_wm_window *window = data;
1252 struct weston_wm *wm = window->wm;
1253 uint32_t values[2];
1254 int width, height;
1255
1256 values[0] = window->width;
1257 values[1] = window->height;
1258 xcb_configure_window(wm->conn,
1259 window->id,
1260 XCB_CONFIG_WINDOW_WIDTH |
1261 XCB_CONFIG_WINDOW_HEIGHT,
1262 values);
1263
1264 weston_wm_window_get_frame_size(window, &width, &height);
1265 values[0] = width;
1266 values[1] = height;
1267 xcb_configure_window(wm->conn,
1268 window->frame_id,
1269 XCB_CONFIG_WINDOW_WIDTH |
1270 XCB_CONFIG_WINDOW_HEIGHT,
1271 values);
1272
1273 window->configure_source = NULL;
1274
1275 weston_wm_window_schedule_repaint(window);
1276}
1277
1278static void
1279send_configure(struct weston_surface *surface,
1280 uint32_t edges, int32_t width, int32_t height)
1281{
1282 struct weston_wm_window *window = get_wm_window(surface);
1283 struct weston_wm *wm = window->wm;
1284 struct theme *t = window->wm->theme;
1285
1286 if (window->decorate) {
1287 window->width = width - 2 * (t->margin + t->width);
1288 window->height = height - 2 * t->margin -
1289 t->titlebar_height - t->width;
1290 } else {
1291 window->width = width - 2 * t->margin;
1292 window->height = height - 2 * t->margin;
1293 }
1294
1295 if (window->configure_source)
1296 return;
1297
1298 window->configure_source =
1299 wl_event_loop_add_idle(wm->server->loop,
1300 weston_wm_window_configure, window);
1301}
1302
1303static const struct weston_shell_client shell_client = {
1304 send_configure
1305};
1306
1307static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001308xserver_map_shell_surface(struct weston_wm *wm,
1309 struct weston_wm_window *window)
1310{
1311 struct weston_shell_interface *shell_interface =
1312 &wm->server->compositor->shell_interface;
1313 struct weston_wm_window *parent;
1314 struct theme *t = window->wm->theme;
1315
1316 if (!shell_interface->create_shell_surface)
1317 return;
1318
1319 window->shsurf =
1320 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001321 window->surface,
1322 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001323
1324 if (!window->transient_for) {
1325 shell_interface->set_toplevel(window->shsurf);
1326 return;
1327 }
1328
1329 parent = hash_table_lookup(wm->window_hash, window->transient_for->id);
1330 shell_interface->set_transient(window->shsurf, parent->shsurf,
1331 window->x - parent->x + t->margin + t->width,
1332 window->y - parent->y + t->margin + t->titlebar_height,
1333 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1334}
1335
1336static void
1337xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1338 struct wl_resource *surface_resource, uint32_t id)
1339{
1340 struct weston_xserver *wxs = resource->data;
1341 struct weston_wm *wm = wxs->wm;
1342 struct wl_surface *surface = surface_resource->data;
1343 struct weston_wm_window *window;
1344
1345 if (client != wxs->client)
1346 return;
1347
1348 window = hash_table_lookup(wm->window_hash, id);
1349 if (window == NULL) {
1350 fprintf(stderr, "set_window_id for unknown window %d\n", id);
1351 return;
1352 }
1353
1354 fprintf(stderr, "set_window_id %d for surface %p\n", id, surface);
1355
1356 weston_wm_window_read_properties(window);
1357
1358 window->surface = (struct weston_surface *) surface;
1359 window->surface_destroy_listener.notify = surface_destroy;
1360 wl_signal_add(&surface->resource.destroy_signal,
1361 &window->surface_destroy_listener);
1362
1363 weston_wm_window_schedule_repaint(window);
1364 xserver_map_shell_surface(wm, window);
1365}
1366
1367const struct xserver_interface xserver_implementation = {
1368 xserver_set_window_id
1369};