blob: 53b578d1d6082568ffb06ebe62ffbb9ff6d6048e [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
446static void
447weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
448{
449 xcb_map_request_event_t *map_request =
450 (xcb_map_request_event_t *) event;
451 struct weston_wm_window *window;
452 uint32_t values[1];
453 int x, y, width, height;
454
455 if (our_resource(wm, map_request->window)) {
456 fprintf(stderr, "XCB_MAP_REQUEST (window %d, ours)\n",
457 map_request->window);
458 return;
459 }
460
461 window = hash_table_lookup(wm->window_hash, map_request->window);
462
Kristian Høgsbergbc6e1622012-05-30 09:59:56 -0400463 if (window->frame_id)
464 return;
465
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400466 weston_wm_window_read_properties(window);
467
468 weston_wm_window_get_frame_size(window, &width, &height);
469 weston_wm_window_get_child_position(window, &x, &y);
470
471 values[0] =
472 XCB_EVENT_MASK_KEY_PRESS |
473 XCB_EVENT_MASK_KEY_RELEASE |
474 XCB_EVENT_MASK_BUTTON_PRESS |
475 XCB_EVENT_MASK_BUTTON_RELEASE |
476 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400477 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400478
479 window->frame_id = xcb_generate_id(wm->conn);
480 xcb_create_window(wm->conn,
481 XCB_COPY_FROM_PARENT,
482 window->frame_id,
483 wm->screen->root,
484 0, 0,
485 width, height,
486 0,
487 XCB_WINDOW_CLASS_INPUT_OUTPUT,
488 wm->screen->root_visual,
489 XCB_CW_EVENT_MASK, values);
490 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
491
492 values[0] = 0;
493 xcb_configure_window(wm->conn, window->id,
494 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
495
496 fprintf(stderr, "XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
497 window->id, window, window->frame_id);
498
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400499 xcb_map_window(wm->conn, map_request->window);
500 xcb_map_window(wm->conn, window->frame_id);
501
502 window->cairo_surface =
503 cairo_xcb_surface_create_with_xrender_format(wm->conn,
504 wm->screen,
505 window->frame_id,
506 &wm->render_format,
507 width, height);
508
509 hash_table_insert(wm->window_hash, window->frame_id, window);
510}
511
512static void
513weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
514{
515 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
516
517 if (our_resource(wm, map_notify->window)) {
518 fprintf(stderr, "XCB_MAP_NOTIFY (window %d, ours)\n",
519 map_notify->window);
520 return;
521 }
522
523 fprintf(stderr, "XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
524}
525
526static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400527weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
528{
529 xcb_unmap_notify_event_t *unmap_notify =
530 (xcb_unmap_notify_event_t *) event;
531 struct weston_wm_window *window;
532
533 fprintf(stderr,
534 "XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
535 unmap_notify->window,
536 unmap_notify->event,
537 our_resource(wm, unmap_notify->window) ? ", ours" : "");
538
539 if (our_resource(wm, unmap_notify->window))
540 return;
541
542 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400543 if (window->frame_id == XCB_WINDOW_NONE) {
544 /* We already withdrew this window on the real unmap
545 * notify and this is the synthetic unmap notify from
546 * the client, or the other way around (ICCCM 4.1.4).
547 * Either way, we're already done so just return. */
548 return;
549 }
550
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400551 if (window->repaint_source)
552 wl_event_source_remove(window->repaint_source);
553 if (window->cairo_surface)
554 cairo_surface_destroy(window->cairo_surface);
555
556 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
557 xcb_destroy_window(wm->conn, window->frame_id);
558 window->frame_id = XCB_WINDOW_NONE;
559 if (wm->focus_window == window)
560 wm->focus_window = NULL;
561 hash_table_remove(wm->window_hash, window->frame_id);
562 if (window->surface)
563 wl_list_remove(&window->surface_destroy_listener.link);
564 window->surface = NULL;
565}
566
567static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400568weston_wm_window_draw_decoration(void *data)
569{
570 struct weston_wm_window *window = data;
571 struct weston_wm *wm = window->wm;
572 struct theme *t = wm->theme;
573 cairo_t *cr;
574 int x, y, width, height;
575 const char *title;
576 uint32_t flags = 0;
577
578 weston_wm_window_read_properties(window);
579
580 window->repaint_source = NULL;
581
582 weston_wm_window_get_frame_size(window, &width, &height);
583 weston_wm_window_get_child_position(window, &x, &y);
584
585 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
586 cr = cairo_create(window->cairo_surface);
587
588 if (window->decorate) {
589 if (wm->focus_window == window)
590 flags |= THEME_FRAME_ACTIVE;
591
592 if (window->name)
593 title = window->name;
594 else
595 title = "untitled";
596
597 theme_render_frame(t, cr, width, height, title, flags);
598 } else {
599 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
600 cairo_set_source_rgba(cr, 0, 0, 0, 0);
601 cairo_paint(cr);
602
603 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
604 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
605 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
606 }
607
608 cairo_destroy(cr);
609
610 if (window->surface) {
611 /* We leave an extra pixel around the X window area to
612 * make sure we don't sample from the undefined alpha
613 * channel when filtering. */
614 window->surface->opaque_rect[0] =
615 (double) (x - 1) / width;
616 window->surface->opaque_rect[1] =
617 (double) (x + window->width + 1) / width;
618 window->surface->opaque_rect[2] =
619 (double) (y - 1) / height;
620 window->surface->opaque_rect[3] =
621 (double) (y + window->height + 1) / height;
622
623 pixman_region32_init_rect(&window->surface->input,
624 t->margin, t->margin,
625 width - 2 * t->margin,
626 height - 2 * t->margin);
627 }
628}
629
630static void
631weston_wm_window_schedule_repaint(struct weston_wm_window *window)
632{
633 struct weston_wm *wm = window->wm;
634
635 if (window->frame_id == XCB_WINDOW_NONE || window->repaint_source)
636 return;
637
638 window->repaint_source =
639 wl_event_loop_add_idle(wm->server->loop,
640 weston_wm_window_draw_decoration,
641 window);
642}
643
644static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400645weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
646{
647 xcb_property_notify_event_t *property_notify =
648 (xcb_property_notify_event_t *) event;
649 struct weston_wm_window *window;
650
651 window = hash_table_lookup(wm->window_hash, property_notify->window);
652 if (window)
653 window->properties_dirty = 1;
654
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400655 fprintf(stderr, "XCB_PROPERTY_NOTIFY: window %d, ",
656 property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400657 if (property_notify->state == XCB_PROPERTY_DELETE)
658 fprintf(stderr, "deleted\n");
659 else
660 read_and_dump_property(wm, property_notify->window,
661 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400662
663 if (property_notify->atom == wm->atom.net_wm_name ||
664 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400665 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400666}
667
668static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400669weston_wm_window_create(struct weston_wm *wm,
670 xcb_window_t id, int width, int height)
671{
672 struct weston_wm_window *window;
673 uint32_t values[1];
674
675 window = malloc(sizeof *window);
676 if (window == NULL) {
677 fprintf(stderr, "failed to allocate window\n");
678 return;
679 }
680
681 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
682 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
683
684 memset(window, 0, sizeof *window);
685 window->wm = wm;
686 window->id = id;
687 window->properties_dirty = 1;
688
689 window->width = width;
690 window->height = height;
691
692 hash_table_insert(wm->window_hash, id, window);
693}
694
695static void
696weston_wm_window_destroy(struct weston_wm_window *window)
697{
698 hash_table_remove(window->wm->window_hash, window->id);
699 free(window);
700}
701
702static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400703weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
704{
705 xcb_create_notify_event_t *create_notify =
706 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400707
708 fprintf(stderr,
709 "XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
710 create_notify->window,
711 create_notify->width, create_notify->height,
712 create_notify->override_redirect ? ", override" : "",
713 our_resource(wm, create_notify->window) ? ", ours" : "");
714
715 if (our_resource(wm, create_notify->window))
716 return;
717
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400718 weston_wm_window_create(wm, create_notify->window,
719 create_notify->width, create_notify->height);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400720}
721
722static void
723weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
724{
725 xcb_destroy_notify_event_t *destroy_notify =
726 (xcb_destroy_notify_event_t *) event;
727 struct weston_wm_window *window;
728
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400729 fprintf(stderr, "XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
730 destroy_notify->window,
731 destroy_notify->event,
732 our_resource(wm, destroy_notify->window) ? ", ours" : "");
733
734 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400735 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400736
737 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400738 weston_wm_window_destroy(window);
739}
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400740
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400741static void
742weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
743{
744 xcb_reparent_notify_event_t *reparent_notify =
745 (xcb_reparent_notify_event_t *) event;
746 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -0400747
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400748 fprintf(stderr,
749 "XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
750 reparent_notify->window,
751 reparent_notify->parent,
752 reparent_notify->event);
753
754 if (reparent_notify->parent == wm->screen->root) {
755 weston_wm_window_create(wm, reparent_notify->window, 10, 10);
756 } else if (!our_resource(wm, reparent_notify->parent)) {
757 window = hash_table_lookup(wm->window_hash,
758 reparent_notify->window);
759 weston_wm_window_destroy(window);
760 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400761}
762
763static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400764weston_wm_window_handle_moveresize(struct weston_wm_window *window,
765 xcb_client_message_event_t *client_message)
766{
767 static const int map[] = {
768 THEME_LOCATION_RESIZING_TOP_LEFT,
769 THEME_LOCATION_RESIZING_TOP,
770 THEME_LOCATION_RESIZING_TOP_RIGHT,
771 THEME_LOCATION_RESIZING_RIGHT,
772 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
773 THEME_LOCATION_RESIZING_BOTTOM,
774 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
775 THEME_LOCATION_RESIZING_LEFT
776 };
777
778 struct weston_wm *wm = window->wm;
779 struct weston_seat *seat = wm->server->compositor->seat;
780 int detail;
781 struct weston_shell_interface *shell_interface =
782 &wm->server->compositor->shell_interface;
783
784 if (seat->seat.pointer->button_count != 1 ||
785 seat->seat.pointer->focus != &window->surface->surface)
786 return;
787
788 detail = client_message->data.data32[2];
789 switch (detail) {
790 case _NET_WM_MOVERESIZE_MOVE:
791 shell_interface->move(window->shsurf, seat);
792 break;
793 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
794 case _NET_WM_MOVERESIZE_SIZE_TOP:
795 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
796 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
797 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
798 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
799 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
800 case _NET_WM_MOVERESIZE_SIZE_LEFT:
801 shell_interface->resize(window->shsurf, seat, map[detail]);
802 break;
803 case _NET_WM_MOVERESIZE_CANCEL:
804 break;
805 }
806}
807
808static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400809weston_wm_handle_client_message(struct weston_wm *wm,
810 xcb_generic_event_t *event)
811{
812 xcb_client_message_event_t *client_message =
813 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400814 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400815
816 window = hash_table_lookup(wm->window_hash, client_message->window);
817
818 fprintf(stderr, "XCB_CLIENT_MESSAGE (%s %d %d %d %d %d)\n",
819 get_atom_name(wm->conn, client_message->type),
820 client_message->data.data32[0],
821 client_message->data.data32[1],
822 client_message->data.data32[2],
823 client_message->data.data32[3],
824 client_message->data.data32[4]);
825
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400826 if (client_message->type == wm->atom.net_wm_moveresize)
827 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400828}
829
830static void
831weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
832{
833 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
834 struct weston_shell_interface *shell_interface =
835 &wm->server->compositor->shell_interface;
836 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400837 enum theme_location location;
838 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400839 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400840
841 fprintf(stderr, "XCB_BUTTON_%s (detail %d)\n",
842 button->response_type == XCB_BUTTON_PRESS ?
843 "PRESS" : "RELEASE", button->detail);
844
845 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400846 weston_wm_window_get_frame_size(window, &width, &height);
847
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400848 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400849 button->detail == 1) {
850 location = theme_get_location(t,
851 button->event_x,
852 button->event_y,
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400853 width, height);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400854
855 switch (location) {
856 case THEME_LOCATION_TITLEBAR:
857 shell_interface->move(window->shsurf,
858 wm->server->compositor->seat);
859 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400860 case THEME_LOCATION_RESIZING_TOP:
861 case THEME_LOCATION_RESIZING_BOTTOM:
862 case THEME_LOCATION_RESIZING_LEFT:
863 case THEME_LOCATION_RESIZING_RIGHT:
864 case THEME_LOCATION_RESIZING_TOP_LEFT:
865 case THEME_LOCATION_RESIZING_TOP_RIGHT:
866 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
867 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
868 shell_interface->resize(window->shsurf,
869 wm->server->compositor->seat,
870 location);
871 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400872 default:
873 break;
874 }
875 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400876}
877
878static int
879weston_wm_handle_event(int fd, uint32_t mask, void *data)
880{
881 struct weston_wm *wm = data;
882 xcb_generic_event_t *event;
883 int count = 0;
884
885 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
886 if (weston_wm_handle_selection_event(wm, event)) {
887 fprintf(stderr, "handled by selection code\n");
888 free(event);
889 count++;
890 continue;
891 }
892
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400893 switch (event->response_type & ~0x80) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400894 case XCB_BUTTON_PRESS:
895 case XCB_BUTTON_RELEASE:
896 weston_wm_handle_button(wm, event);
897 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400898 case XCB_CREATE_NOTIFY:
899 weston_wm_handle_create_notify(wm, event);
900 break;
901 case XCB_MAP_REQUEST:
902 weston_wm_handle_map_request(wm, event);
903 break;
904 case XCB_MAP_NOTIFY:
905 weston_wm_handle_map_notify(wm, event);
906 break;
907 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400908 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400909 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400910 case XCB_REPARENT_NOTIFY:
911 weston_wm_handle_reparent_notify(wm, event);
912 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400913 case XCB_CONFIGURE_REQUEST:
914 weston_wm_handle_configure_request(wm, event);
915 break;
916 case XCB_CONFIGURE_NOTIFY:
917 weston_wm_handle_configure_notify(wm, event);
918 break;
919 case XCB_DESTROY_NOTIFY:
920 weston_wm_handle_destroy_notify(wm, event);
921 break;
922 case XCB_MAPPING_NOTIFY:
923 fprintf(stderr, "XCB_MAPPING_NOTIFY\n");
924 break;
925 case XCB_PROPERTY_NOTIFY:
926 weston_wm_handle_property_notify(wm, event);
927 break;
928 case XCB_CLIENT_MESSAGE:
929 weston_wm_handle_client_message(wm, event);
930 break;
931 }
932
933 free(event);
934 count++;
935 }
936
937 xcb_flush(wm->conn);
938
939 return count;
940}
941
942static void
943wxs_wm_get_resources(struct weston_wm *wm)
944{
945
946#define F(field) offsetof(struct weston_wm, field)
947
948 static const struct { const char *name; int offset; } atoms[] = {
949 { "WM_PROTOCOLS", F(atom.wm_protocols) },
950 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
951 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
952 { "_NET_WM_NAME", F(atom.net_wm_name) },
953 { "_NET_WM_ICON", F(atom.net_wm_icon) },
954 { "_NET_WM_STATE", F(atom.net_wm_state) },
955 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
956 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
957 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
958 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
959
960 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
961 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
962 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
963 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
964 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
965 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
966 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
967 { "_NET_WM_WINDOW_TYPE_DROPDOWN", F(atom.net_wm_window_type_dropdown) },
968 { "_NET_WM_WINDOW_TYPE_POPUP", F(atom.net_wm_window_type_popup) },
969 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
970 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
971 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
972 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
973 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
974
975 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
976 { "_NET_SUPPORTING_WM_CHECK",
977 F(atom.net_supporting_wm_check) },
978 { "_NET_SUPPORTED", F(atom.net_supported) },
979 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
980 { "CLIPBOARD", F(atom.clipboard) },
981 { "TARGETS", F(atom.targets) },
982 { "UTF8_STRING", F(atom.utf8_string) },
983 { "_WL_SELECTION", F(atom.wl_selection) },
984 { "INCR", F(atom.incr) },
985 { "TIMESTAMP", F(atom.timestamp) },
986 { "MULTIPLE", F(atom.multiple) },
987 { "UTF8_STRING" , F(atom.utf8_string) },
988 { "COMPOUND_TEXT", F(atom.compound_text) },
989 { "TEXT", F(atom.text) },
990 { "STRING", F(atom.string) },
991 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
992 { "text/plain", F(atom.text_plain) },
993 };
994#undef F
995
996 xcb_xfixes_query_version_cookie_t xfixes_cookie;
997 xcb_xfixes_query_version_reply_t *xfixes_reply;
998 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
999 xcb_intern_atom_reply_t *reply;
1000 xcb_render_query_pict_formats_reply_t *formats_reply;
1001 xcb_render_query_pict_formats_cookie_t formats_cookie;
1002 xcb_render_pictforminfo_t *formats;
1003 uint32_t i;
1004
1005 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1006
1007 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1008
1009 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1010 cookies[i] = xcb_intern_atom (wm->conn, 0,
1011 strlen(atoms[i].name),
1012 atoms[i].name);
1013
1014 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1015 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1016 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1017 free(reply);
1018 }
1019
1020 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1021 if (!wm->xfixes || !wm->xfixes->present)
1022 fprintf(stderr, "xfixes not available\n");
1023
1024 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1025 XCB_XFIXES_MAJOR_VERSION,
1026 XCB_XFIXES_MINOR_VERSION);
1027 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1028 xfixes_cookie, NULL);
1029
1030 printf("xfixes version: %d.%d\n",
1031 xfixes_reply->major_version, xfixes_reply->minor_version);
1032
1033 free(xfixes_reply);
1034
1035 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1036 formats_cookie, 0);
1037 if (formats_reply == NULL)
1038 return;
1039
1040 formats = xcb_render_query_pict_formats_formats(formats_reply);
1041 for (i = 0; i < formats_reply->length; i++)
1042 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1043 formats[i].depth == 24)
1044 wm->render_format = formats[i];
1045
1046 free(formats_reply);
1047}
1048
1049static void
1050weston_wm_create_wm_window(struct weston_wm *wm)
1051{
1052 static const char name[] = "Weston WM";
1053
1054 wm->wm_window = xcb_generate_id(wm->conn);
1055 xcb_create_window(wm->conn,
1056 XCB_COPY_FROM_PARENT,
1057 wm->wm_window,
1058 wm->screen->root,
1059 0, 0,
1060 10, 10,
1061 0,
1062 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1063 wm->screen->root_visual,
1064 0, NULL);
1065
1066 xcb_change_property(wm->conn,
1067 XCB_PROP_MODE_REPLACE,
1068 wm->wm_window,
1069 wm->atom.net_supporting_wm_check,
1070 XCB_ATOM_WINDOW,
1071 32, /* format */
1072 1, &wm->wm_window);
1073
1074 xcb_change_property(wm->conn,
1075 XCB_PROP_MODE_REPLACE,
1076 wm->wm_window,
1077 wm->atom.net_wm_name,
1078 wm->atom.utf8_string,
1079 8, /* format */
1080 strlen(name), name);
1081
1082 xcb_change_property(wm->conn,
1083 XCB_PROP_MODE_REPLACE,
1084 wm->screen->root,
1085 wm->atom.net_supporting_wm_check,
1086 XCB_ATOM_WINDOW,
1087 32, /* format */
1088 1, &wm->wm_window);
1089
1090}
1091
1092struct weston_wm *
1093weston_wm_create(struct weston_xserver *wxs)
1094{
1095 struct wl_seat *seat;
1096 struct weston_wm *wm;
1097 struct wl_event_loop *loop;
1098 xcb_screen_iterator_t s;
1099 uint32_t values[1], mask;
1100 int sv[2];
1101 xcb_atom_t supported[1];
1102
1103 wm = malloc(sizeof *wm);
1104 if (wm == NULL)
1105 return NULL;
1106
1107 memset(wm, 0, sizeof *wm);
1108 wm->server = wxs;
1109 wm->window_hash = hash_table_create();
1110 if (wm->window_hash == NULL) {
1111 free(wm);
1112 return NULL;
1113 }
1114
1115 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
1116 fprintf(stderr, "socketpair failed\n");
1117 hash_table_destroy(wm->window_hash);
1118 free(wm);
1119 return NULL;
1120 }
1121
1122 xserver_send_client(wxs->resource, sv[1]);
1123 wl_client_flush(wxs->resource->client);
1124 close(sv[1]);
1125
1126 /* xcb_connect_to_fd takes ownership of the fd. */
1127 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1128 if (xcb_connection_has_error(wm->conn)) {
1129 fprintf(stderr, "xcb_connect_to_fd failed\n");
1130 close(sv[0]);
1131 hash_table_destroy(wm->window_hash);
1132 free(wm);
1133 return NULL;
1134 }
1135
1136 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1137 wm->screen = s.data;
1138
1139 loop = wl_display_get_event_loop(wxs->wl_display);
1140 wm->source =
1141 wl_event_loop_add_fd(loop, sv[0],
1142 WL_EVENT_READABLE,
1143 weston_wm_handle_event, wm);
1144 wl_event_source_check(wm->source);
1145
1146 wxs_wm_get_resources(wm);
1147
1148 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001149 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1150 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1151 XCB_EVENT_MASK_PROPERTY_CHANGE;
1152 xcb_change_window_attributes(wm->conn, wm->screen->root,
1153 XCB_CW_EVENT_MASK, values);
1154 wm->theme = theme_create();
1155
1156 weston_wm_create_wm_window(wm);
1157
1158 supported[0] = wm->atom.net_wm_moveresize;
1159 xcb_change_property(wm->conn,
1160 XCB_PROP_MODE_REPLACE,
1161 wm->screen->root,
1162 wm->atom.net_supported,
1163 XCB_ATOM_ATOM,
1164 32, /* format */
1165 ARRAY_LENGTH(supported), supported);
1166
1167 wm->selection_request.requestor = XCB_NONE;
1168
1169 wm->selection_window = xcb_generate_id(wm->conn);
1170 xcb_create_window(wm->conn,
1171 XCB_COPY_FROM_PARENT,
1172 wm->selection_window,
1173 wm->screen->root,
1174 0, 0,
1175 10, 10,
1176 0,
1177 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1178 wm->screen->root_visual,
1179 XCB_CW_EVENT_MASK, values);
1180
1181 mask =
1182 XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER |
1183 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY |
1184 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE;
1185
1186 xcb_xfixes_select_selection_input(wm->conn, wm->selection_window,
1187 wm->atom.clipboard, mask);
1188
1189 xcb_flush(wm->conn);
1190
1191 seat = &wxs->compositor->seat->seat;
1192 wm->selection_listener.notify = weston_wm_set_selection;
1193 wl_signal_add(&seat->selection_signal, &wm->selection_listener);
1194
1195 wm->activate_listener.notify = weston_wm_window_activate;
1196 wl_signal_add(&wxs->compositor->activate_signal,
1197 &wm->activate_listener);
1198
1199 fprintf(stderr, "created wm\n");
1200
1201 return wm;
1202}
1203
1204void
1205weston_wm_destroy(struct weston_wm *wm)
1206{
1207 /* FIXME: Free windows in hash. */
1208 hash_table_destroy(wm->window_hash);
1209 xcb_disconnect(wm->conn);
1210 wl_event_source_remove(wm->source);
1211 wl_list_remove(&wm->selection_listener.link);
1212 wl_list_remove(&wm->activate_listener.link);
1213
1214 free(wm);
1215}
1216
1217static void
1218surface_destroy(struct wl_listener *listener, void *data)
1219{
1220 struct weston_wm_window *window =
1221 container_of(listener,
1222 struct weston_wm_window, surface_destroy_listener);
1223
1224 fprintf(stderr, "surface for xid %d destroyed\n", window->id);
1225}
1226
1227static struct weston_wm_window *
1228get_wm_window(struct weston_surface *surface)
1229{
1230 struct wl_resource *resource = &surface->surface.resource;
1231 struct wl_listener *listener;
1232
1233 listener = wl_signal_get(&resource->destroy_signal, surface_destroy);
1234 if (listener)
1235 return container_of(listener, struct weston_wm_window,
1236 surface_destroy_listener);
1237
1238 return NULL;
1239}
1240
1241static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001242weston_wm_window_configure(void *data)
1243{
1244 struct weston_wm_window *window = data;
1245 struct weston_wm *wm = window->wm;
1246 uint32_t values[2];
1247 int width, height;
1248
1249 values[0] = window->width;
1250 values[1] = window->height;
1251 xcb_configure_window(wm->conn,
1252 window->id,
1253 XCB_CONFIG_WINDOW_WIDTH |
1254 XCB_CONFIG_WINDOW_HEIGHT,
1255 values);
1256
1257 weston_wm_window_get_frame_size(window, &width, &height);
1258 values[0] = width;
1259 values[1] = height;
1260 xcb_configure_window(wm->conn,
1261 window->frame_id,
1262 XCB_CONFIG_WINDOW_WIDTH |
1263 XCB_CONFIG_WINDOW_HEIGHT,
1264 values);
1265
1266 window->configure_source = NULL;
1267
1268 weston_wm_window_schedule_repaint(window);
1269}
1270
1271static void
1272send_configure(struct weston_surface *surface,
1273 uint32_t edges, int32_t width, int32_t height)
1274{
1275 struct weston_wm_window *window = get_wm_window(surface);
1276 struct weston_wm *wm = window->wm;
1277 struct theme *t = window->wm->theme;
1278
1279 if (window->decorate) {
1280 window->width = width - 2 * (t->margin + t->width);
1281 window->height = height - 2 * t->margin -
1282 t->titlebar_height - t->width;
1283 } else {
1284 window->width = width - 2 * t->margin;
1285 window->height = height - 2 * t->margin;
1286 }
1287
1288 if (window->configure_source)
1289 return;
1290
1291 window->configure_source =
1292 wl_event_loop_add_idle(wm->server->loop,
1293 weston_wm_window_configure, window);
1294}
1295
1296static const struct weston_shell_client shell_client = {
1297 send_configure
1298};
1299
1300static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001301xserver_map_shell_surface(struct weston_wm *wm,
1302 struct weston_wm_window *window)
1303{
1304 struct weston_shell_interface *shell_interface =
1305 &wm->server->compositor->shell_interface;
1306 struct weston_wm_window *parent;
1307 struct theme *t = window->wm->theme;
1308
1309 if (!shell_interface->create_shell_surface)
1310 return;
1311
1312 window->shsurf =
1313 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001314 window->surface,
1315 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001316
1317 if (!window->transient_for) {
1318 shell_interface->set_toplevel(window->shsurf);
1319 return;
1320 }
1321
1322 parent = hash_table_lookup(wm->window_hash, window->transient_for->id);
1323 shell_interface->set_transient(window->shsurf, parent->shsurf,
1324 window->x - parent->x + t->margin + t->width,
1325 window->y - parent->y + t->margin + t->titlebar_height,
1326 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1327}
1328
1329static void
1330xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1331 struct wl_resource *surface_resource, uint32_t id)
1332{
1333 struct weston_xserver *wxs = resource->data;
1334 struct weston_wm *wm = wxs->wm;
1335 struct wl_surface *surface = surface_resource->data;
1336 struct weston_wm_window *window;
1337
1338 if (client != wxs->client)
1339 return;
1340
1341 window = hash_table_lookup(wm->window_hash, id);
1342 if (window == NULL) {
1343 fprintf(stderr, "set_window_id for unknown window %d\n", id);
1344 return;
1345 }
1346
1347 fprintf(stderr, "set_window_id %d for surface %p\n", id, surface);
1348
1349 weston_wm_window_read_properties(window);
1350
1351 window->surface = (struct weston_surface *) surface;
1352 window->surface_destroy_listener.notify = surface_destroy;
1353 wl_signal_add(&surface->resource.destroy_signal,
1354 &window->surface_destroy_listener);
1355
1356 weston_wm_window_schedule_repaint(window);
1357 xserver_map_shell_surface(wm, window);
1358}
1359
1360const struct xserver_interface xserver_implementation = {
1361 xserver_set_window_id
1362};