blob: ddb30193eed6575f1037c3fc4c0579ce532ad9d8 [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);
543 if (window->repaint_source)
544 wl_event_source_remove(window->repaint_source);
545 if (window->cairo_surface)
546 cairo_surface_destroy(window->cairo_surface);
547
548 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
549 xcb_destroy_window(wm->conn, window->frame_id);
550 window->frame_id = XCB_WINDOW_NONE;
551 if (wm->focus_window == window)
552 wm->focus_window = NULL;
553 hash_table_remove(wm->window_hash, window->frame_id);
554 if (window->surface)
555 wl_list_remove(&window->surface_destroy_listener.link);
556 window->surface = NULL;
557}
558
559static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400560weston_wm_window_draw_decoration(void *data)
561{
562 struct weston_wm_window *window = data;
563 struct weston_wm *wm = window->wm;
564 struct theme *t = wm->theme;
565 cairo_t *cr;
566 int x, y, width, height;
567 const char *title;
568 uint32_t flags = 0;
569
570 weston_wm_window_read_properties(window);
571
572 window->repaint_source = NULL;
573
574 weston_wm_window_get_frame_size(window, &width, &height);
575 weston_wm_window_get_child_position(window, &x, &y);
576
577 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
578 cr = cairo_create(window->cairo_surface);
579
580 if (window->decorate) {
581 if (wm->focus_window == window)
582 flags |= THEME_FRAME_ACTIVE;
583
584 if (window->name)
585 title = window->name;
586 else
587 title = "untitled";
588
589 theme_render_frame(t, cr, width, height, title, flags);
590 } else {
591 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
592 cairo_set_source_rgba(cr, 0, 0, 0, 0);
593 cairo_paint(cr);
594
595 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
596 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
597 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
598 }
599
600 cairo_destroy(cr);
601
602 if (window->surface) {
603 /* We leave an extra pixel around the X window area to
604 * make sure we don't sample from the undefined alpha
605 * channel when filtering. */
606 window->surface->opaque_rect[0] =
607 (double) (x - 1) / width;
608 window->surface->opaque_rect[1] =
609 (double) (x + window->width + 1) / width;
610 window->surface->opaque_rect[2] =
611 (double) (y - 1) / height;
612 window->surface->opaque_rect[3] =
613 (double) (y + window->height + 1) / height;
614
615 pixman_region32_init_rect(&window->surface->input,
616 t->margin, t->margin,
617 width - 2 * t->margin,
618 height - 2 * t->margin);
619 }
620}
621
622static void
623weston_wm_window_schedule_repaint(struct weston_wm_window *window)
624{
625 struct weston_wm *wm = window->wm;
626
627 if (window->frame_id == XCB_WINDOW_NONE || window->repaint_source)
628 return;
629
630 window->repaint_source =
631 wl_event_loop_add_idle(wm->server->loop,
632 weston_wm_window_draw_decoration,
633 window);
634}
635
636static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400637weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
638{
639 xcb_property_notify_event_t *property_notify =
640 (xcb_property_notify_event_t *) event;
641 struct weston_wm_window *window;
642
643 window = hash_table_lookup(wm->window_hash, property_notify->window);
644 if (window)
645 window->properties_dirty = 1;
646
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400647 fprintf(stderr, "XCB_PROPERTY_NOTIFY: window %d, ",
648 property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400649 if (property_notify->state == XCB_PROPERTY_DELETE)
650 fprintf(stderr, "deleted\n");
651 else
652 read_and_dump_property(wm, property_notify->window,
653 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400654
655 if (property_notify->atom == wm->atom.net_wm_name ||
656 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400657 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400658}
659
660static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400661weston_wm_window_create(struct weston_wm *wm,
662 xcb_window_t id, int width, int height)
663{
664 struct weston_wm_window *window;
665 uint32_t values[1];
666
667 window = malloc(sizeof *window);
668 if (window == NULL) {
669 fprintf(stderr, "failed to allocate window\n");
670 return;
671 }
672
673 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
674 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
675
676 memset(window, 0, sizeof *window);
677 window->wm = wm;
678 window->id = id;
679 window->properties_dirty = 1;
680
681 window->width = width;
682 window->height = height;
683
684 hash_table_insert(wm->window_hash, id, window);
685}
686
687static void
688weston_wm_window_destroy(struct weston_wm_window *window)
689{
690 hash_table_remove(window->wm->window_hash, window->id);
691 free(window);
692}
693
694static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400695weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
696{
697 xcb_create_notify_event_t *create_notify =
698 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400699
700 fprintf(stderr,
701 "XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
702 create_notify->window,
703 create_notify->width, create_notify->height,
704 create_notify->override_redirect ? ", override" : "",
705 our_resource(wm, create_notify->window) ? ", ours" : "");
706
707 if (our_resource(wm, create_notify->window))
708 return;
709
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400710 weston_wm_window_create(wm, create_notify->window,
711 create_notify->width, create_notify->height);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400712}
713
714static void
715weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
716{
717 xcb_destroy_notify_event_t *destroy_notify =
718 (xcb_destroy_notify_event_t *) event;
719 struct weston_wm_window *window;
720
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400721 fprintf(stderr, "XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
722 destroy_notify->window,
723 destroy_notify->event,
724 our_resource(wm, destroy_notify->window) ? ", ours" : "");
725
726 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400727 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400728
729 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400730 weston_wm_window_destroy(window);
731}
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400732
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400733static void
734weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
735{
736 xcb_reparent_notify_event_t *reparent_notify =
737 (xcb_reparent_notify_event_t *) event;
738 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -0400739
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400740 fprintf(stderr,
741 "XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
742 reparent_notify->window,
743 reparent_notify->parent,
744 reparent_notify->event);
745
746 if (reparent_notify->parent == wm->screen->root) {
747 weston_wm_window_create(wm, reparent_notify->window, 10, 10);
748 } else if (!our_resource(wm, reparent_notify->parent)) {
749 window = hash_table_lookup(wm->window_hash,
750 reparent_notify->window);
751 weston_wm_window_destroy(window);
752 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400753}
754
755static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400756weston_wm_window_handle_moveresize(struct weston_wm_window *window,
757 xcb_client_message_event_t *client_message)
758{
759 static const int map[] = {
760 THEME_LOCATION_RESIZING_TOP_LEFT,
761 THEME_LOCATION_RESIZING_TOP,
762 THEME_LOCATION_RESIZING_TOP_RIGHT,
763 THEME_LOCATION_RESIZING_RIGHT,
764 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
765 THEME_LOCATION_RESIZING_BOTTOM,
766 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
767 THEME_LOCATION_RESIZING_LEFT
768 };
769
770 struct weston_wm *wm = window->wm;
771 struct weston_seat *seat = wm->server->compositor->seat;
772 int detail;
773 struct weston_shell_interface *shell_interface =
774 &wm->server->compositor->shell_interface;
775
776 if (seat->seat.pointer->button_count != 1 ||
777 seat->seat.pointer->focus != &window->surface->surface)
778 return;
779
780 detail = client_message->data.data32[2];
781 switch (detail) {
782 case _NET_WM_MOVERESIZE_MOVE:
783 shell_interface->move(window->shsurf, seat);
784 break;
785 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
786 case _NET_WM_MOVERESIZE_SIZE_TOP:
787 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
788 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
789 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
790 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
791 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
792 case _NET_WM_MOVERESIZE_SIZE_LEFT:
793 shell_interface->resize(window->shsurf, seat, map[detail]);
794 break;
795 case _NET_WM_MOVERESIZE_CANCEL:
796 break;
797 }
798}
799
800static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400801weston_wm_handle_client_message(struct weston_wm *wm,
802 xcb_generic_event_t *event)
803{
804 xcb_client_message_event_t *client_message =
805 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400806 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400807
808 window = hash_table_lookup(wm->window_hash, client_message->window);
809
810 fprintf(stderr, "XCB_CLIENT_MESSAGE (%s %d %d %d %d %d)\n",
811 get_atom_name(wm->conn, client_message->type),
812 client_message->data.data32[0],
813 client_message->data.data32[1],
814 client_message->data.data32[2],
815 client_message->data.data32[3],
816 client_message->data.data32[4]);
817
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400818 if (client_message->type == wm->atom.net_wm_moveresize)
819 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400820}
821
822static void
823weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
824{
825 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
826 struct weston_shell_interface *shell_interface =
827 &wm->server->compositor->shell_interface;
828 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400829 enum theme_location location;
830 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400831 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400832
833 fprintf(stderr, "XCB_BUTTON_%s (detail %d)\n",
834 button->response_type == XCB_BUTTON_PRESS ?
835 "PRESS" : "RELEASE", button->detail);
836
837 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400838 weston_wm_window_get_frame_size(window, &width, &height);
839
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400840 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400841 button->detail == 1) {
842 location = theme_get_location(t,
843 button->event_x,
844 button->event_y,
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400845 width, height);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400846
847 switch (location) {
848 case THEME_LOCATION_TITLEBAR:
849 shell_interface->move(window->shsurf,
850 wm->server->compositor->seat);
851 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400852 case THEME_LOCATION_RESIZING_TOP:
853 case THEME_LOCATION_RESIZING_BOTTOM:
854 case THEME_LOCATION_RESIZING_LEFT:
855 case THEME_LOCATION_RESIZING_RIGHT:
856 case THEME_LOCATION_RESIZING_TOP_LEFT:
857 case THEME_LOCATION_RESIZING_TOP_RIGHT:
858 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
859 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
860 shell_interface->resize(window->shsurf,
861 wm->server->compositor->seat,
862 location);
863 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400864 default:
865 break;
866 }
867 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400868}
869
870static int
871weston_wm_handle_event(int fd, uint32_t mask, void *data)
872{
873 struct weston_wm *wm = data;
874 xcb_generic_event_t *event;
875 int count = 0;
876
877 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
878 if (weston_wm_handle_selection_event(wm, event)) {
879 fprintf(stderr, "handled by selection code\n");
880 free(event);
881 count++;
882 continue;
883 }
884
Kristian Høgsberg8d1aa7d2012-05-30 10:06:59 -0400885 switch (event->response_type) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400886 case XCB_BUTTON_PRESS:
887 case XCB_BUTTON_RELEASE:
888 weston_wm_handle_button(wm, event);
889 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400890 case XCB_CREATE_NOTIFY:
891 weston_wm_handle_create_notify(wm, event);
892 break;
893 case XCB_MAP_REQUEST:
894 weston_wm_handle_map_request(wm, event);
895 break;
896 case XCB_MAP_NOTIFY:
897 weston_wm_handle_map_notify(wm, event);
898 break;
899 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400900 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400901 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400902 case XCB_REPARENT_NOTIFY:
903 weston_wm_handle_reparent_notify(wm, event);
904 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400905 case XCB_CONFIGURE_REQUEST:
906 weston_wm_handle_configure_request(wm, event);
907 break;
908 case XCB_CONFIGURE_NOTIFY:
909 weston_wm_handle_configure_notify(wm, event);
910 break;
911 case XCB_DESTROY_NOTIFY:
912 weston_wm_handle_destroy_notify(wm, event);
913 break;
914 case XCB_MAPPING_NOTIFY:
915 fprintf(stderr, "XCB_MAPPING_NOTIFY\n");
916 break;
917 case XCB_PROPERTY_NOTIFY:
918 weston_wm_handle_property_notify(wm, event);
919 break;
920 case XCB_CLIENT_MESSAGE:
921 weston_wm_handle_client_message(wm, event);
922 break;
923 }
924
925 free(event);
926 count++;
927 }
928
929 xcb_flush(wm->conn);
930
931 return count;
932}
933
934static void
935wxs_wm_get_resources(struct weston_wm *wm)
936{
937
938#define F(field) offsetof(struct weston_wm, field)
939
940 static const struct { const char *name; int offset; } atoms[] = {
941 { "WM_PROTOCOLS", F(atom.wm_protocols) },
942 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
943 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
944 { "_NET_WM_NAME", F(atom.net_wm_name) },
945 { "_NET_WM_ICON", F(atom.net_wm_icon) },
946 { "_NET_WM_STATE", F(atom.net_wm_state) },
947 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
948 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
949 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
950 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
951
952 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
953 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
954 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
955 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
956 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
957 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
958 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
959 { "_NET_WM_WINDOW_TYPE_DROPDOWN", F(atom.net_wm_window_type_dropdown) },
960 { "_NET_WM_WINDOW_TYPE_POPUP", F(atom.net_wm_window_type_popup) },
961 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
962 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
963 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
964 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
965 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
966
967 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
968 { "_NET_SUPPORTING_WM_CHECK",
969 F(atom.net_supporting_wm_check) },
970 { "_NET_SUPPORTED", F(atom.net_supported) },
971 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
972 { "CLIPBOARD", F(atom.clipboard) },
973 { "TARGETS", F(atom.targets) },
974 { "UTF8_STRING", F(atom.utf8_string) },
975 { "_WL_SELECTION", F(atom.wl_selection) },
976 { "INCR", F(atom.incr) },
977 { "TIMESTAMP", F(atom.timestamp) },
978 { "MULTIPLE", F(atom.multiple) },
979 { "UTF8_STRING" , F(atom.utf8_string) },
980 { "COMPOUND_TEXT", F(atom.compound_text) },
981 { "TEXT", F(atom.text) },
982 { "STRING", F(atom.string) },
983 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
984 { "text/plain", F(atom.text_plain) },
985 };
986#undef F
987
988 xcb_xfixes_query_version_cookie_t xfixes_cookie;
989 xcb_xfixes_query_version_reply_t *xfixes_reply;
990 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
991 xcb_intern_atom_reply_t *reply;
992 xcb_render_query_pict_formats_reply_t *formats_reply;
993 xcb_render_query_pict_formats_cookie_t formats_cookie;
994 xcb_render_pictforminfo_t *formats;
995 uint32_t i;
996
997 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
998
999 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1000
1001 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1002 cookies[i] = xcb_intern_atom (wm->conn, 0,
1003 strlen(atoms[i].name),
1004 atoms[i].name);
1005
1006 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1007 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1008 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1009 free(reply);
1010 }
1011
1012 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1013 if (!wm->xfixes || !wm->xfixes->present)
1014 fprintf(stderr, "xfixes not available\n");
1015
1016 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1017 XCB_XFIXES_MAJOR_VERSION,
1018 XCB_XFIXES_MINOR_VERSION);
1019 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1020 xfixes_cookie, NULL);
1021
1022 printf("xfixes version: %d.%d\n",
1023 xfixes_reply->major_version, xfixes_reply->minor_version);
1024
1025 free(xfixes_reply);
1026
1027 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1028 formats_cookie, 0);
1029 if (formats_reply == NULL)
1030 return;
1031
1032 formats = xcb_render_query_pict_formats_formats(formats_reply);
1033 for (i = 0; i < formats_reply->length; i++)
1034 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1035 formats[i].depth == 24)
1036 wm->render_format = formats[i];
1037
1038 free(formats_reply);
1039}
1040
1041static void
1042weston_wm_create_wm_window(struct weston_wm *wm)
1043{
1044 static const char name[] = "Weston WM";
1045
1046 wm->wm_window = xcb_generate_id(wm->conn);
1047 xcb_create_window(wm->conn,
1048 XCB_COPY_FROM_PARENT,
1049 wm->wm_window,
1050 wm->screen->root,
1051 0, 0,
1052 10, 10,
1053 0,
1054 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1055 wm->screen->root_visual,
1056 0, NULL);
1057
1058 xcb_change_property(wm->conn,
1059 XCB_PROP_MODE_REPLACE,
1060 wm->wm_window,
1061 wm->atom.net_supporting_wm_check,
1062 XCB_ATOM_WINDOW,
1063 32, /* format */
1064 1, &wm->wm_window);
1065
1066 xcb_change_property(wm->conn,
1067 XCB_PROP_MODE_REPLACE,
1068 wm->wm_window,
1069 wm->atom.net_wm_name,
1070 wm->atom.utf8_string,
1071 8, /* format */
1072 strlen(name), name);
1073
1074 xcb_change_property(wm->conn,
1075 XCB_PROP_MODE_REPLACE,
1076 wm->screen->root,
1077 wm->atom.net_supporting_wm_check,
1078 XCB_ATOM_WINDOW,
1079 32, /* format */
1080 1, &wm->wm_window);
1081
1082}
1083
1084struct weston_wm *
1085weston_wm_create(struct weston_xserver *wxs)
1086{
1087 struct wl_seat *seat;
1088 struct weston_wm *wm;
1089 struct wl_event_loop *loop;
1090 xcb_screen_iterator_t s;
1091 uint32_t values[1], mask;
1092 int sv[2];
1093 xcb_atom_t supported[1];
1094
1095 wm = malloc(sizeof *wm);
1096 if (wm == NULL)
1097 return NULL;
1098
1099 memset(wm, 0, sizeof *wm);
1100 wm->server = wxs;
1101 wm->window_hash = hash_table_create();
1102 if (wm->window_hash == NULL) {
1103 free(wm);
1104 return NULL;
1105 }
1106
1107 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
1108 fprintf(stderr, "socketpair failed\n");
1109 hash_table_destroy(wm->window_hash);
1110 free(wm);
1111 return NULL;
1112 }
1113
1114 xserver_send_client(wxs->resource, sv[1]);
1115 wl_client_flush(wxs->resource->client);
1116 close(sv[1]);
1117
1118 /* xcb_connect_to_fd takes ownership of the fd. */
1119 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1120 if (xcb_connection_has_error(wm->conn)) {
1121 fprintf(stderr, "xcb_connect_to_fd failed\n");
1122 close(sv[0]);
1123 hash_table_destroy(wm->window_hash);
1124 free(wm);
1125 return NULL;
1126 }
1127
1128 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1129 wm->screen = s.data;
1130
1131 loop = wl_display_get_event_loop(wxs->wl_display);
1132 wm->source =
1133 wl_event_loop_add_fd(loop, sv[0],
1134 WL_EVENT_READABLE,
1135 weston_wm_handle_event, wm);
1136 wl_event_source_check(wm->source);
1137
1138 wxs_wm_get_resources(wm);
1139
1140 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001141 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1142 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1143 XCB_EVENT_MASK_PROPERTY_CHANGE;
1144 xcb_change_window_attributes(wm->conn, wm->screen->root,
1145 XCB_CW_EVENT_MASK, values);
1146 wm->theme = theme_create();
1147
1148 weston_wm_create_wm_window(wm);
1149
1150 supported[0] = wm->atom.net_wm_moveresize;
1151 xcb_change_property(wm->conn,
1152 XCB_PROP_MODE_REPLACE,
1153 wm->screen->root,
1154 wm->atom.net_supported,
1155 XCB_ATOM_ATOM,
1156 32, /* format */
1157 ARRAY_LENGTH(supported), supported);
1158
1159 wm->selection_request.requestor = XCB_NONE;
1160
1161 wm->selection_window = xcb_generate_id(wm->conn);
1162 xcb_create_window(wm->conn,
1163 XCB_COPY_FROM_PARENT,
1164 wm->selection_window,
1165 wm->screen->root,
1166 0, 0,
1167 10, 10,
1168 0,
1169 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1170 wm->screen->root_visual,
1171 XCB_CW_EVENT_MASK, values);
1172
1173 mask =
1174 XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER |
1175 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY |
1176 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE;
1177
1178 xcb_xfixes_select_selection_input(wm->conn, wm->selection_window,
1179 wm->atom.clipboard, mask);
1180
1181 xcb_flush(wm->conn);
1182
1183 seat = &wxs->compositor->seat->seat;
1184 wm->selection_listener.notify = weston_wm_set_selection;
1185 wl_signal_add(&seat->selection_signal, &wm->selection_listener);
1186
1187 wm->activate_listener.notify = weston_wm_window_activate;
1188 wl_signal_add(&wxs->compositor->activate_signal,
1189 &wm->activate_listener);
1190
1191 fprintf(stderr, "created wm\n");
1192
1193 return wm;
1194}
1195
1196void
1197weston_wm_destroy(struct weston_wm *wm)
1198{
1199 /* FIXME: Free windows in hash. */
1200 hash_table_destroy(wm->window_hash);
1201 xcb_disconnect(wm->conn);
1202 wl_event_source_remove(wm->source);
1203 wl_list_remove(&wm->selection_listener.link);
1204 wl_list_remove(&wm->activate_listener.link);
1205
1206 free(wm);
1207}
1208
1209static void
1210surface_destroy(struct wl_listener *listener, void *data)
1211{
1212 struct weston_wm_window *window =
1213 container_of(listener,
1214 struct weston_wm_window, surface_destroy_listener);
1215
1216 fprintf(stderr, "surface for xid %d destroyed\n", window->id);
1217}
1218
1219static struct weston_wm_window *
1220get_wm_window(struct weston_surface *surface)
1221{
1222 struct wl_resource *resource = &surface->surface.resource;
1223 struct wl_listener *listener;
1224
1225 listener = wl_signal_get(&resource->destroy_signal, surface_destroy);
1226 if (listener)
1227 return container_of(listener, struct weston_wm_window,
1228 surface_destroy_listener);
1229
1230 return NULL;
1231}
1232
1233static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001234weston_wm_window_configure(void *data)
1235{
1236 struct weston_wm_window *window = data;
1237 struct weston_wm *wm = window->wm;
1238 uint32_t values[2];
1239 int width, height;
1240
1241 values[0] = window->width;
1242 values[1] = window->height;
1243 xcb_configure_window(wm->conn,
1244 window->id,
1245 XCB_CONFIG_WINDOW_WIDTH |
1246 XCB_CONFIG_WINDOW_HEIGHT,
1247 values);
1248
1249 weston_wm_window_get_frame_size(window, &width, &height);
1250 values[0] = width;
1251 values[1] = height;
1252 xcb_configure_window(wm->conn,
1253 window->frame_id,
1254 XCB_CONFIG_WINDOW_WIDTH |
1255 XCB_CONFIG_WINDOW_HEIGHT,
1256 values);
1257
1258 window->configure_source = NULL;
1259
1260 weston_wm_window_schedule_repaint(window);
1261}
1262
1263static void
1264send_configure(struct weston_surface *surface,
1265 uint32_t edges, int32_t width, int32_t height)
1266{
1267 struct weston_wm_window *window = get_wm_window(surface);
1268 struct weston_wm *wm = window->wm;
1269 struct theme *t = window->wm->theme;
1270
1271 if (window->decorate) {
1272 window->width = width - 2 * (t->margin + t->width);
1273 window->height = height - 2 * t->margin -
1274 t->titlebar_height - t->width;
1275 } else {
1276 window->width = width - 2 * t->margin;
1277 window->height = height - 2 * t->margin;
1278 }
1279
1280 if (window->configure_source)
1281 return;
1282
1283 window->configure_source =
1284 wl_event_loop_add_idle(wm->server->loop,
1285 weston_wm_window_configure, window);
1286}
1287
1288static const struct weston_shell_client shell_client = {
1289 send_configure
1290};
1291
1292static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001293xserver_map_shell_surface(struct weston_wm *wm,
1294 struct weston_wm_window *window)
1295{
1296 struct weston_shell_interface *shell_interface =
1297 &wm->server->compositor->shell_interface;
1298 struct weston_wm_window *parent;
1299 struct theme *t = window->wm->theme;
1300
1301 if (!shell_interface->create_shell_surface)
1302 return;
1303
1304 window->shsurf =
1305 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001306 window->surface,
1307 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001308
1309 if (!window->transient_for) {
1310 shell_interface->set_toplevel(window->shsurf);
1311 return;
1312 }
1313
1314 parent = hash_table_lookup(wm->window_hash, window->transient_for->id);
1315 shell_interface->set_transient(window->shsurf, parent->shsurf,
1316 window->x - parent->x + t->margin + t->width,
1317 window->y - parent->y + t->margin + t->titlebar_height,
1318 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1319}
1320
1321static void
1322xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1323 struct wl_resource *surface_resource, uint32_t id)
1324{
1325 struct weston_xserver *wxs = resource->data;
1326 struct weston_wm *wm = wxs->wm;
1327 struct wl_surface *surface = surface_resource->data;
1328 struct weston_wm_window *window;
1329
1330 if (client != wxs->client)
1331 return;
1332
1333 window = hash_table_lookup(wm->window_hash, id);
1334 if (window == NULL) {
1335 fprintf(stderr, "set_window_id for unknown window %d\n", id);
1336 return;
1337 }
1338
1339 fprintf(stderr, "set_window_id %d for surface %p\n", id, surface);
1340
1341 weston_wm_window_read_properties(window);
1342
1343 window->surface = (struct weston_surface *) surface;
1344 window->surface_destroy_listener.notify = surface_destroy;
1345 wl_signal_add(&surface->resource.destroy_signal,
1346 &window->surface_destroy_listener);
1347
1348 weston_wm_window_schedule_repaint(window);
1349 xserver_map_shell_surface(wm, window);
1350}
1351
1352const struct xserver_interface xserver_implementation = {
1353 xserver_set_window_id
1354};