blob: 334b5842477902d0d11fd8a99537dbff8cc20aff [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
142dump_property(struct weston_wm *wm, xcb_atom_t property,
143 xcb_get_property_reply_t *reply)
144{
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
151 width = fprintf(stderr, " %s: ", get_atom_name(wm->conn, property));
152 if (reply == NULL) {
153 fprintf(stderr, "(no reply)\n");
154 return;
155 }
156
157 width += fprintf(stderr,
158 "type %s, format %d, length %d (value_len %d): ",
159 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
194static void
195dump_window_properties(struct weston_wm *wm, xcb_window_t window)
196{
197 xcb_list_properties_cookie_t list_cookie;
198 xcb_list_properties_reply_t *list_reply;
199 xcb_get_property_cookie_t property_cookie;
200 xcb_get_property_reply_t *property_reply;
201 xcb_atom_t *atoms;
202 int i, length;
203
204 list_cookie = xcb_list_properties(wm->conn, window);
205 list_reply = xcb_list_properties_reply(wm->conn, list_cookie, NULL);
206 if (!list_reply)
207 /* Bad window, typically */
208 return;
209
210 length = xcb_list_properties_atoms_length(list_reply);
211 atoms = xcb_list_properties_atoms(list_reply);
212
213 for (i = 0; i < length; i++) {
214 property_cookie =
215 xcb_get_property(wm->conn,
216 0, /* delete */
217 window,
218 atoms[i],
219 XCB_ATOM_ANY,
220 0, 2048);
221
222 property_reply = xcb_get_property_reply(wm->conn,
223 property_cookie, NULL);
224 dump_property(wm, atoms[i], property_reply);
225
226 free(property_reply);
227 }
228
229 free(list_reply);
230}
231
232/* We reuse some predefined, but otherwise useles atoms */
233#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
234#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
235
236static void
237weston_wm_window_read_properties(struct weston_wm_window *window)
238{
239 struct weston_wm *wm = window->wm;
240
241#define F(field) offsetof(struct weston_wm_window, field)
242 const struct {
243 xcb_atom_t atom;
244 xcb_atom_t type;
245 int offset;
246 } props[] = {
247 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
248 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
249 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
250 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
251 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
252 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
253 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
254 };
255#undef F
256
257 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
258 xcb_get_property_reply_t *reply;
259 void *p;
260 uint32_t *xid;
261 xcb_atom_t *atom;
262 uint32_t i;
263 struct motif_wm_hints *hints;
264
265 if (!window->properties_dirty)
266 return;
267 window->properties_dirty = 0;
268
269 dump_window_properties(wm, window->id);
270
271 for (i = 0; i < ARRAY_LENGTH(props); i++)
272 cookie[i] = xcb_get_property(wm->conn,
273 0, /* delete */
274 window->id,
275 props[i].atom,
276 XCB_ATOM_ANY, 0, 2048);
277
278 window->decorate = 1;
279 for (i = 0; i < ARRAY_LENGTH(props); i++) {
280 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
281 if (!reply)
282 /* Bad window, typically */
283 continue;
284 if (reply->type == XCB_ATOM_NONE) {
285 /* No such property */
286 free(reply);
287 continue;
288 }
289
290 p = ((char *) window + props[i].offset);
291
292 switch (props[i].type) {
293 case XCB_ATOM_STRING:
294 /* FIXME: We're using this for both string and
295 utf8_string */
296 if (*(char **) p)
297 free(*(char **) p);
298
299 *(char **) p =
300 strndup(xcb_get_property_value(reply),
301 xcb_get_property_value_length(reply));
302 break;
303 case XCB_ATOM_WINDOW:
304 xid = xcb_get_property_value(reply);
305 *(struct weston_wm_window **) p =
306 hash_table_lookup(wm->window_hash, *xid);
307 break;
308 case XCB_ATOM_ATOM:
309 atom = xcb_get_property_value(reply);
310 *(xcb_atom_t *) p = *atom;
311 break;
312 case TYPE_WM_PROTOCOLS:
313 break;
314 case TYPE_MOTIF_WM_HINTS:
315 hints = xcb_get_property_value(reply);
316 if (hints->flags & MWM_HINTS_DECORATIONS)
317 window->decorate = hints->decorations > 0;
318 break;
319 default:
320 break;
321 }
322 free(reply);
323 }
324}
325
326static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400327weston_wm_window_get_frame_size(struct weston_wm_window *window,
328 int *width, int *height)
329{
330 struct theme *t = window->wm->theme;
331
332 if (window->decorate) {
333 *width = window->width + (t->margin + t->width) * 2;
334 *height = window->height +
335 t->margin * 2 + t->width + t->titlebar_height;
336 } else {
337 *width = window->width + t->margin * 2;
338 *height = window->height + t->margin * 2;
339 }
340}
341
342static void
343weston_wm_window_get_child_position(struct weston_wm_window *window,
344 int *x, int *y)
345{
346 struct theme *t = window->wm->theme;
347
348 if (window->decorate) {
349 *x = t->margin + t->width;
350 *y = t->margin + t->titlebar_height;
351 } else {
352 *x = t->margin;
353 *y = t->margin;
354 }
355}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400356
357static void
358weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
359{
360 xcb_configure_request_event_t *configure_request =
361 (xcb_configure_request_event_t *) event;
362 struct weston_wm_window *window;
363 uint32_t mask, values[16];
364 int x, y, width, height, i = 0;
365
366 fprintf(stderr, "XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
367 configure_request->window,
368 configure_request->x, configure_request->y,
369 configure_request->width, configure_request->height);
370
371 window = hash_table_lookup(wm->window_hash, configure_request->window);
372
373 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
374 window->width = configure_request->width;
375 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
376 window->height = configure_request->height;
377
378 weston_wm_window_get_child_position(window, &x, &y);
379 values[i++] = x;
380 values[i++] = y;
381 values[i++] = window->width;
382 values[i++] = window->height;
383 values[i++] = 0;
384 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
385 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
386 XCB_CONFIG_WINDOW_BORDER_WIDTH;
387 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
388 values[i++] = configure_request->sibling;
389 mask |= XCB_CONFIG_WINDOW_SIBLING;
390 }
391 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
392 values[i++] = configure_request->stack_mode;
393 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
394 }
395
396 xcb_configure_window(wm->conn, window->id, mask, values);
397
398 weston_wm_window_get_frame_size(window, &width, &height);
399 values[0] = width;
400 values[1] = height;
401 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
402 xcb_configure_window(wm->conn, window->frame_id, mask, values);
403
404 weston_wm_window_schedule_repaint(window);
405}
406
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400407static void
408weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
409{
410 xcb_configure_notify_event_t *configure_notify =
411 (xcb_configure_notify_event_t *) event;
412 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400413
414 window = hash_table_lookup(wm->window_hash, configure_notify->window);
415
416 fprintf(stderr, "XCB_CONFIGURE_NOTIFY (%s window %d) %d,%d @ %dx%d\n",
417 configure_notify->window == window->id ? "client" : "frame",
418 configure_notify->window,
419 configure_notify->x, configure_notify->y,
420 configure_notify->width, configure_notify->height);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400421}
422
423static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400424weston_wm_window_activate(struct wl_listener *listener, void *data)
425{
426 struct weston_surface *surface = data;
427 struct weston_wm_window *window = get_wm_window(surface);
Scott Moreau85ecac02012-05-21 15:49:14 -0600428 struct weston_wm *wm =
429 container_of(listener, struct weston_wm, activate_listener);
430 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400431
Scott Moreau85ecac02012-05-21 15:49:14 -0600432 if (window) {
433 client_message.response_type = XCB_CLIENT_MESSAGE;
434 client_message.format = 32;
435 client_message.window = window->id;
436 client_message.type = wm->atom.wm_protocols;
437 client_message.data.data32[0] = wm->atom.wm_take_focus;
438 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
439
440 xcb_send_event(wm->conn, 0, window->id,
441 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
442 (char *) &client_message);
443
444 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
445 window->id, XCB_TIME_CURRENT_TIME);
446 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400447 xcb_set_input_focus (wm->conn,
448 XCB_INPUT_FOCUS_POINTER_ROOT,
449 XCB_NONE,
450 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600451 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400452
453 if (wm->focus_window)
454 weston_wm_window_schedule_repaint(wm->focus_window);
455 wm->focus_window = window;
456 if (wm->focus_window)
457 weston_wm_window_schedule_repaint(wm->focus_window);
458}
459
460static int
461our_resource(struct weston_wm *wm, uint32_t id)
462{
463 const xcb_setup_t *setup;
464
465 setup = xcb_get_setup(wm->conn);
466
467 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
468}
469
470static void
471weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
472{
473 xcb_map_request_event_t *map_request =
474 (xcb_map_request_event_t *) event;
475 struct weston_wm_window *window;
476 uint32_t values[1];
477 int x, y, width, height;
478
479 if (our_resource(wm, map_request->window)) {
480 fprintf(stderr, "XCB_MAP_REQUEST (window %d, ours)\n",
481 map_request->window);
482 return;
483 }
484
485 window = hash_table_lookup(wm->window_hash, map_request->window);
486
487 weston_wm_window_read_properties(window);
488
489 weston_wm_window_get_frame_size(window, &width, &height);
490 weston_wm_window_get_child_position(window, &x, &y);
491
492 values[0] =
493 XCB_EVENT_MASK_KEY_PRESS |
494 XCB_EVENT_MASK_KEY_RELEASE |
495 XCB_EVENT_MASK_BUTTON_PRESS |
496 XCB_EVENT_MASK_BUTTON_RELEASE |
497 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400498 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
499 XCB_EVENT_MASK_RESIZE_REDIRECT |
500 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400501 XCB_EVENT_MASK_EXPOSURE;
502
503 window->frame_id = xcb_generate_id(wm->conn);
504 xcb_create_window(wm->conn,
505 XCB_COPY_FROM_PARENT,
506 window->frame_id,
507 wm->screen->root,
508 0, 0,
509 width, height,
510 0,
511 XCB_WINDOW_CLASS_INPUT_OUTPUT,
512 wm->screen->root_visual,
513 XCB_CW_EVENT_MASK, values);
514 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
515
516 values[0] = 0;
517 xcb_configure_window(wm->conn, window->id,
518 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
519
520 fprintf(stderr, "XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
521 window->id, window, window->frame_id);
522
523 xcb_change_save_set(wm->conn, XCB_SET_MODE_DELETE, window->id);
524
525 xcb_map_window(wm->conn, map_request->window);
526 xcb_map_window(wm->conn, window->frame_id);
527
528 window->cairo_surface =
529 cairo_xcb_surface_create_with_xrender_format(wm->conn,
530 wm->screen,
531 window->frame_id,
532 &wm->render_format,
533 width, height);
534
535 hash_table_insert(wm->window_hash, window->frame_id, window);
536}
537
538static void
539weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
540{
541 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
542
543 if (our_resource(wm, map_notify->window)) {
544 fprintf(stderr, "XCB_MAP_NOTIFY (window %d, ours)\n",
545 map_notify->window);
546 return;
547 }
548
549 fprintf(stderr, "XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
550}
551
552static void
553weston_wm_window_draw_decoration(void *data)
554{
555 struct weston_wm_window *window = data;
556 struct weston_wm *wm = window->wm;
557 struct theme *t = wm->theme;
558 cairo_t *cr;
559 int x, y, width, height;
560 const char *title;
561 uint32_t flags = 0;
562
563 weston_wm_window_read_properties(window);
564
565 window->repaint_source = NULL;
566
567 weston_wm_window_get_frame_size(window, &width, &height);
568 weston_wm_window_get_child_position(window, &x, &y);
569
570 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
571 cr = cairo_create(window->cairo_surface);
572
573 if (window->decorate) {
574 if (wm->focus_window == window)
575 flags |= THEME_FRAME_ACTIVE;
576
577 if (window->name)
578 title = window->name;
579 else
580 title = "untitled";
581
582 theme_render_frame(t, cr, width, height, title, flags);
583 } else {
584 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
585 cairo_set_source_rgba(cr, 0, 0, 0, 0);
586 cairo_paint(cr);
587
588 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
589 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
590 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
591 }
592
593 cairo_destroy(cr);
594
595 if (window->surface) {
596 /* We leave an extra pixel around the X window area to
597 * make sure we don't sample from the undefined alpha
598 * channel when filtering. */
599 window->surface->opaque_rect[0] =
600 (double) (x - 1) / width;
601 window->surface->opaque_rect[1] =
602 (double) (x + window->width + 1) / width;
603 window->surface->opaque_rect[2] =
604 (double) (y - 1) / height;
605 window->surface->opaque_rect[3] =
606 (double) (y + window->height + 1) / height;
607
608 pixman_region32_init_rect(&window->surface->input,
609 t->margin, t->margin,
610 width - 2 * t->margin,
611 height - 2 * t->margin);
612 }
613}
614
615static void
616weston_wm_window_schedule_repaint(struct weston_wm_window *window)
617{
618 struct weston_wm *wm = window->wm;
619
620 if (window->frame_id == XCB_WINDOW_NONE || window->repaint_source)
621 return;
622
623 window->repaint_source =
624 wl_event_loop_add_idle(wm->server->loop,
625 weston_wm_window_draw_decoration,
626 window);
627}
628
629static void
630weston_wm_handle_expose(struct weston_wm *wm, xcb_generic_event_t *event)
631{
632 struct weston_wm_window *window;
633 xcb_expose_event_t *expose = (xcb_expose_event_t *) event;
634
635 window = hash_table_lookup(wm->window_hash, expose->window);
636 fprintf(stderr, "XCB_EXPOSE (window %d, title %s, surface %p)\n",
637 window->id, window->name, window->surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400638}
639
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400640static void
641weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
642{
643 xcb_property_notify_event_t *property_notify =
644 (xcb_property_notify_event_t *) event;
645 struct weston_wm_window *window;
646
647 window = hash_table_lookup(wm->window_hash, property_notify->window);
648 if (window)
649 window->properties_dirty = 1;
650
651 if (property_notify->atom == XCB_ATOM_WM_CLASS) {
652 fprintf(stderr, "wm_class changed\n");
653 } else if (property_notify->atom == XCB_ATOM_WM_TRANSIENT_FOR) {
654 fprintf(stderr, "wm_transient_for changed\n");
655 } else if (property_notify->atom == wm->atom.wm_protocols) {
656 fprintf(stderr, "wm_protocols changed\n");
657 } else if (property_notify->atom == wm->atom.net_wm_name) {
658 fprintf(stderr, "_net_wm_name changed\n");
659 weston_wm_window_schedule_repaint(window);
660 } else if (property_notify->atom == wm->atom.net_wm_user_time) {
661 fprintf(stderr, "_net_wm_user_time changed\n");
662 } else if (property_notify->atom == wm->atom.net_wm_icon_name) {
663 fprintf(stderr, "_net_wm_icon_name changed\n");
664 } else if (property_notify->atom == XCB_ATOM_WM_NAME) {
665 fprintf(stderr, "wm_name changed\n");
666 weston_wm_window_schedule_repaint(window);
667 } else if (property_notify->atom == XCB_ATOM_WM_ICON_NAME) {
668 fprintf(stderr, "wm_icon_name changed\n");
669 } else {
670 fprintf(stderr, "XCB_PROPERTY_NOTIFY: "
671 "unhandled property change: %s\n",
672 get_atom_name(wm->conn, property_notify->atom));
673 }
674}
675
676static void
677weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
678{
679 xcb_create_notify_event_t *create_notify =
680 (xcb_create_notify_event_t *) event;
681 struct weston_wm_window *window;
682 uint32_t values[1];
683
684 fprintf(stderr,
685 "XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
686 create_notify->window,
687 create_notify->width, create_notify->height,
688 create_notify->override_redirect ? ", override" : "",
689 our_resource(wm, create_notify->window) ? ", ours" : "");
690
691 if (our_resource(wm, create_notify->window))
692 return;
693
694 window = malloc(sizeof *window);
695 if (window == NULL) {
696 fprintf(stderr, "failed to allocate window\n");
697 return;
698 }
699
700 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
701 xcb_change_window_attributes(wm->conn, create_notify->window,
702 XCB_CW_EVENT_MASK, values);
703
704 memset(window, 0, sizeof *window);
705 window->wm = wm;
706 window->id = create_notify->window;
707 window->properties_dirty = 1;
708
709 window->width = create_notify->width;
710 window->height = create_notify->height;
711
712 hash_table_insert(wm->window_hash, window->id, window);
713}
714
715static void
716weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
717{
718 xcb_destroy_notify_event_t *destroy_notify =
719 (xcb_destroy_notify_event_t *) event;
720 struct weston_wm_window *window;
721
722 if (our_resource(wm, destroy_notify->window)) {
723 fprintf(stderr, "XCB_DESTROY_NOTIFY, win %d (ours)\n",
724 destroy_notify->window);
725 return;
726 }
727
728 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
729
730 fprintf(stderr, "XCB_DESTROY_NOTIFY, win %d (%p)\n",
731 destroy_notify->window, window);
732
733 if (window->repaint_source)
734 wl_event_source_remove(window->repaint_source);
735 if (window->cairo_surface)
736 cairo_surface_destroy(window->cairo_surface);
737
738 hash_table_remove(wm->window_hash, window->id);
739 hash_table_remove(wm->window_hash, window->frame_id);
740 xcb_destroy_window(wm->conn, window->frame_id);
741 if (window->surface)
742 wl_list_remove(&window->surface_destroy_listener.link);
743 free(window);
744}
745
746static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400747weston_wm_window_handle_moveresize(struct weston_wm_window *window,
748 xcb_client_message_event_t *client_message)
749{
750 static const int map[] = {
751 THEME_LOCATION_RESIZING_TOP_LEFT,
752 THEME_LOCATION_RESIZING_TOP,
753 THEME_LOCATION_RESIZING_TOP_RIGHT,
754 THEME_LOCATION_RESIZING_RIGHT,
755 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
756 THEME_LOCATION_RESIZING_BOTTOM,
757 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
758 THEME_LOCATION_RESIZING_LEFT
759 };
760
761 struct weston_wm *wm = window->wm;
762 struct weston_seat *seat = wm->server->compositor->seat;
763 int detail;
764 struct weston_shell_interface *shell_interface =
765 &wm->server->compositor->shell_interface;
766
767 if (seat->seat.pointer->button_count != 1 ||
768 seat->seat.pointer->focus != &window->surface->surface)
769 return;
770
771 detail = client_message->data.data32[2];
772 switch (detail) {
773 case _NET_WM_MOVERESIZE_MOVE:
774 shell_interface->move(window->shsurf, seat);
775 break;
776 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
777 case _NET_WM_MOVERESIZE_SIZE_TOP:
778 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
779 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
780 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
781 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
782 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
783 case _NET_WM_MOVERESIZE_SIZE_LEFT:
784 shell_interface->resize(window->shsurf, seat, map[detail]);
785 break;
786 case _NET_WM_MOVERESIZE_CANCEL:
787 break;
788 }
789}
790
791static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400792weston_wm_handle_client_message(struct weston_wm *wm,
793 xcb_generic_event_t *event)
794{
795 xcb_client_message_event_t *client_message =
796 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400797 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400798
799 window = hash_table_lookup(wm->window_hash, client_message->window);
800
801 fprintf(stderr, "XCB_CLIENT_MESSAGE (%s %d %d %d %d %d)\n",
802 get_atom_name(wm->conn, client_message->type),
803 client_message->data.data32[0],
804 client_message->data.data32[1],
805 client_message->data.data32[2],
806 client_message->data.data32[3],
807 client_message->data.data32[4]);
808
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400809 if (client_message->type == wm->atom.net_wm_moveresize)
810 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400811}
812
813static void
814weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
815{
816 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
817 struct weston_shell_interface *shell_interface =
818 &wm->server->compositor->shell_interface;
819 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400820 enum theme_location location;
821 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400822 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400823
824 fprintf(stderr, "XCB_BUTTON_%s (detail %d)\n",
825 button->response_type == XCB_BUTTON_PRESS ?
826 "PRESS" : "RELEASE", button->detail);
827
828 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400829 weston_wm_window_get_frame_size(window, &width, &height);
830
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400831 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400832 button->detail == 1) {
833 location = theme_get_location(t,
834 button->event_x,
835 button->event_y,
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400836 width, height);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400837
838 switch (location) {
839 case THEME_LOCATION_TITLEBAR:
840 shell_interface->move(window->shsurf,
841 wm->server->compositor->seat);
842 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400843 case THEME_LOCATION_RESIZING_TOP:
844 case THEME_LOCATION_RESIZING_BOTTOM:
845 case THEME_LOCATION_RESIZING_LEFT:
846 case THEME_LOCATION_RESIZING_RIGHT:
847 case THEME_LOCATION_RESIZING_TOP_LEFT:
848 case THEME_LOCATION_RESIZING_TOP_RIGHT:
849 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
850 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
851 shell_interface->resize(window->shsurf,
852 wm->server->compositor->seat,
853 location);
854 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400855 default:
856 break;
857 }
858 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400859}
860
861static int
862weston_wm_handle_event(int fd, uint32_t mask, void *data)
863{
864 struct weston_wm *wm = data;
865 xcb_generic_event_t *event;
866 int count = 0;
867
868 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
869 if (weston_wm_handle_selection_event(wm, event)) {
870 fprintf(stderr, "handled by selection code\n");
871 free(event);
872 count++;
873 continue;
874 }
875
876 switch (event->response_type & ~0x80) {
877 case XCB_BUTTON_PRESS:
878 case XCB_BUTTON_RELEASE:
879 weston_wm_handle_button(wm, event);
880 break;
881 case XCB_EXPOSE:
882 weston_wm_handle_expose(wm, event);
883 break;
884 case XCB_CREATE_NOTIFY:
885 weston_wm_handle_create_notify(wm, event);
886 break;
887 case XCB_MAP_REQUEST:
888 weston_wm_handle_map_request(wm, event);
889 break;
890 case XCB_MAP_NOTIFY:
891 weston_wm_handle_map_notify(wm, event);
892 break;
893 case XCB_UNMAP_NOTIFY:
894 fprintf(stderr, "XCB_UNMAP_NOTIFY\n");
895 break;
896 case XCB_CONFIGURE_REQUEST:
897 weston_wm_handle_configure_request(wm, event);
898 break;
899 case XCB_CONFIGURE_NOTIFY:
900 weston_wm_handle_configure_notify(wm, event);
901 break;
902 case XCB_DESTROY_NOTIFY:
903 weston_wm_handle_destroy_notify(wm, event);
904 break;
905 case XCB_MAPPING_NOTIFY:
906 fprintf(stderr, "XCB_MAPPING_NOTIFY\n");
907 break;
908 case XCB_PROPERTY_NOTIFY:
909 weston_wm_handle_property_notify(wm, event);
910 break;
911 case XCB_CLIENT_MESSAGE:
912 weston_wm_handle_client_message(wm, event);
913 break;
914 }
915
916 free(event);
917 count++;
918 }
919
920 xcb_flush(wm->conn);
921
922 return count;
923}
924
925static void
926wxs_wm_get_resources(struct weston_wm *wm)
927{
928
929#define F(field) offsetof(struct weston_wm, field)
930
931 static const struct { const char *name; int offset; } atoms[] = {
932 { "WM_PROTOCOLS", F(atom.wm_protocols) },
933 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
934 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
935 { "_NET_WM_NAME", F(atom.net_wm_name) },
936 { "_NET_WM_ICON", F(atom.net_wm_icon) },
937 { "_NET_WM_STATE", F(atom.net_wm_state) },
938 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
939 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
940 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
941 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
942
943 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
944 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
945 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
946 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
947 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
948 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
949 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
950 { "_NET_WM_WINDOW_TYPE_DROPDOWN", F(atom.net_wm_window_type_dropdown) },
951 { "_NET_WM_WINDOW_TYPE_POPUP", F(atom.net_wm_window_type_popup) },
952 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
953 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
954 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
955 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
956 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
957
958 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
959 { "_NET_SUPPORTING_WM_CHECK",
960 F(atom.net_supporting_wm_check) },
961 { "_NET_SUPPORTED", F(atom.net_supported) },
962 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
963 { "CLIPBOARD", F(atom.clipboard) },
964 { "TARGETS", F(atom.targets) },
965 { "UTF8_STRING", F(atom.utf8_string) },
966 { "_WL_SELECTION", F(atom.wl_selection) },
967 { "INCR", F(atom.incr) },
968 { "TIMESTAMP", F(atom.timestamp) },
969 { "MULTIPLE", F(atom.multiple) },
970 { "UTF8_STRING" , F(atom.utf8_string) },
971 { "COMPOUND_TEXT", F(atom.compound_text) },
972 { "TEXT", F(atom.text) },
973 { "STRING", F(atom.string) },
974 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
975 { "text/plain", F(atom.text_plain) },
976 };
977#undef F
978
979 xcb_xfixes_query_version_cookie_t xfixes_cookie;
980 xcb_xfixes_query_version_reply_t *xfixes_reply;
981 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
982 xcb_intern_atom_reply_t *reply;
983 xcb_render_query_pict_formats_reply_t *formats_reply;
984 xcb_render_query_pict_formats_cookie_t formats_cookie;
985 xcb_render_pictforminfo_t *formats;
986 uint32_t i;
987
988 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
989
990 formats_cookie = xcb_render_query_pict_formats(wm->conn);
991
992 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
993 cookies[i] = xcb_intern_atom (wm->conn, 0,
994 strlen(atoms[i].name),
995 atoms[i].name);
996
997 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
998 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
999 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1000 free(reply);
1001 }
1002
1003 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1004 if (!wm->xfixes || !wm->xfixes->present)
1005 fprintf(stderr, "xfixes not available\n");
1006
1007 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1008 XCB_XFIXES_MAJOR_VERSION,
1009 XCB_XFIXES_MINOR_VERSION);
1010 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1011 xfixes_cookie, NULL);
1012
1013 printf("xfixes version: %d.%d\n",
1014 xfixes_reply->major_version, xfixes_reply->minor_version);
1015
1016 free(xfixes_reply);
1017
1018 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1019 formats_cookie, 0);
1020 if (formats_reply == NULL)
1021 return;
1022
1023 formats = xcb_render_query_pict_formats_formats(formats_reply);
1024 for (i = 0; i < formats_reply->length; i++)
1025 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1026 formats[i].depth == 24)
1027 wm->render_format = formats[i];
1028
1029 free(formats_reply);
1030}
1031
1032static void
1033weston_wm_create_wm_window(struct weston_wm *wm)
1034{
1035 static const char name[] = "Weston WM";
1036
1037 wm->wm_window = xcb_generate_id(wm->conn);
1038 xcb_create_window(wm->conn,
1039 XCB_COPY_FROM_PARENT,
1040 wm->wm_window,
1041 wm->screen->root,
1042 0, 0,
1043 10, 10,
1044 0,
1045 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1046 wm->screen->root_visual,
1047 0, NULL);
1048
1049 xcb_change_property(wm->conn,
1050 XCB_PROP_MODE_REPLACE,
1051 wm->wm_window,
1052 wm->atom.net_supporting_wm_check,
1053 XCB_ATOM_WINDOW,
1054 32, /* format */
1055 1, &wm->wm_window);
1056
1057 xcb_change_property(wm->conn,
1058 XCB_PROP_MODE_REPLACE,
1059 wm->wm_window,
1060 wm->atom.net_wm_name,
1061 wm->atom.utf8_string,
1062 8, /* format */
1063 strlen(name), name);
1064
1065 xcb_change_property(wm->conn,
1066 XCB_PROP_MODE_REPLACE,
1067 wm->screen->root,
1068 wm->atom.net_supporting_wm_check,
1069 XCB_ATOM_WINDOW,
1070 32, /* format */
1071 1, &wm->wm_window);
1072
1073}
1074
1075struct weston_wm *
1076weston_wm_create(struct weston_xserver *wxs)
1077{
1078 struct wl_seat *seat;
1079 struct weston_wm *wm;
1080 struct wl_event_loop *loop;
1081 xcb_screen_iterator_t s;
1082 uint32_t values[1], mask;
1083 int sv[2];
1084 xcb_atom_t supported[1];
1085
1086 wm = malloc(sizeof *wm);
1087 if (wm == NULL)
1088 return NULL;
1089
1090 memset(wm, 0, sizeof *wm);
1091 wm->server = wxs;
1092 wm->window_hash = hash_table_create();
1093 if (wm->window_hash == NULL) {
1094 free(wm);
1095 return NULL;
1096 }
1097
1098 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
1099 fprintf(stderr, "socketpair failed\n");
1100 hash_table_destroy(wm->window_hash);
1101 free(wm);
1102 return NULL;
1103 }
1104
1105 xserver_send_client(wxs->resource, sv[1]);
1106 wl_client_flush(wxs->resource->client);
1107 close(sv[1]);
1108
1109 /* xcb_connect_to_fd takes ownership of the fd. */
1110 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1111 if (xcb_connection_has_error(wm->conn)) {
1112 fprintf(stderr, "xcb_connect_to_fd failed\n");
1113 close(sv[0]);
1114 hash_table_destroy(wm->window_hash);
1115 free(wm);
1116 return NULL;
1117 }
1118
1119 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1120 wm->screen = s.data;
1121
1122 loop = wl_display_get_event_loop(wxs->wl_display);
1123 wm->source =
1124 wl_event_loop_add_fd(loop, sv[0],
1125 WL_EVENT_READABLE,
1126 weston_wm_handle_event, wm);
1127 wl_event_source_check(wm->source);
1128
1129 wxs_wm_get_resources(wm);
1130
1131 values[0] =
1132 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
1133 XCB_EVENT_MASK_RESIZE_REDIRECT |
1134 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1135 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1136 XCB_EVENT_MASK_PROPERTY_CHANGE;
1137 xcb_change_window_attributes(wm->conn, wm->screen->root,
1138 XCB_CW_EVENT_MASK, values);
1139 wm->theme = theme_create();
1140
1141 weston_wm_create_wm_window(wm);
1142
1143 supported[0] = wm->atom.net_wm_moveresize;
1144 xcb_change_property(wm->conn,
1145 XCB_PROP_MODE_REPLACE,
1146 wm->screen->root,
1147 wm->atom.net_supported,
1148 XCB_ATOM_ATOM,
1149 32, /* format */
1150 ARRAY_LENGTH(supported), supported);
1151
1152 wm->selection_request.requestor = XCB_NONE;
1153
1154 wm->selection_window = xcb_generate_id(wm->conn);
1155 xcb_create_window(wm->conn,
1156 XCB_COPY_FROM_PARENT,
1157 wm->selection_window,
1158 wm->screen->root,
1159 0, 0,
1160 10, 10,
1161 0,
1162 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1163 wm->screen->root_visual,
1164 XCB_CW_EVENT_MASK, values);
1165
1166 mask =
1167 XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER |
1168 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY |
1169 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE;
1170
1171 xcb_xfixes_select_selection_input(wm->conn, wm->selection_window,
1172 wm->atom.clipboard, mask);
1173
1174 xcb_flush(wm->conn);
1175
1176 seat = &wxs->compositor->seat->seat;
1177 wm->selection_listener.notify = weston_wm_set_selection;
1178 wl_signal_add(&seat->selection_signal, &wm->selection_listener);
1179
1180 wm->activate_listener.notify = weston_wm_window_activate;
1181 wl_signal_add(&wxs->compositor->activate_signal,
1182 &wm->activate_listener);
1183
1184 fprintf(stderr, "created wm\n");
1185
1186 return wm;
1187}
1188
1189void
1190weston_wm_destroy(struct weston_wm *wm)
1191{
1192 /* FIXME: Free windows in hash. */
1193 hash_table_destroy(wm->window_hash);
1194 xcb_disconnect(wm->conn);
1195 wl_event_source_remove(wm->source);
1196 wl_list_remove(&wm->selection_listener.link);
1197 wl_list_remove(&wm->activate_listener.link);
1198
1199 free(wm);
1200}
1201
1202static void
1203surface_destroy(struct wl_listener *listener, void *data)
1204{
1205 struct weston_wm_window *window =
1206 container_of(listener,
1207 struct weston_wm_window, surface_destroy_listener);
1208
1209 fprintf(stderr, "surface for xid %d destroyed\n", window->id);
1210}
1211
1212static struct weston_wm_window *
1213get_wm_window(struct weston_surface *surface)
1214{
1215 struct wl_resource *resource = &surface->surface.resource;
1216 struct wl_listener *listener;
1217
1218 listener = wl_signal_get(&resource->destroy_signal, surface_destroy);
1219 if (listener)
1220 return container_of(listener, struct weston_wm_window,
1221 surface_destroy_listener);
1222
1223 return NULL;
1224}
1225
1226static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001227weston_wm_window_configure(void *data)
1228{
1229 struct weston_wm_window *window = data;
1230 struct weston_wm *wm = window->wm;
1231 uint32_t values[2];
1232 int width, height;
1233
1234 values[0] = window->width;
1235 values[1] = window->height;
1236 xcb_configure_window(wm->conn,
1237 window->id,
1238 XCB_CONFIG_WINDOW_WIDTH |
1239 XCB_CONFIG_WINDOW_HEIGHT,
1240 values);
1241
1242 weston_wm_window_get_frame_size(window, &width, &height);
1243 values[0] = width;
1244 values[1] = height;
1245 xcb_configure_window(wm->conn,
1246 window->frame_id,
1247 XCB_CONFIG_WINDOW_WIDTH |
1248 XCB_CONFIG_WINDOW_HEIGHT,
1249 values);
1250
1251 window->configure_source = NULL;
1252
1253 weston_wm_window_schedule_repaint(window);
1254}
1255
1256static void
1257send_configure(struct weston_surface *surface,
1258 uint32_t edges, int32_t width, int32_t height)
1259{
1260 struct weston_wm_window *window = get_wm_window(surface);
1261 struct weston_wm *wm = window->wm;
1262 struct theme *t = window->wm->theme;
1263
1264 if (window->decorate) {
1265 window->width = width - 2 * (t->margin + t->width);
1266 window->height = height - 2 * t->margin -
1267 t->titlebar_height - t->width;
1268 } else {
1269 window->width = width - 2 * t->margin;
1270 window->height = height - 2 * t->margin;
1271 }
1272
1273 if (window->configure_source)
1274 return;
1275
1276 window->configure_source =
1277 wl_event_loop_add_idle(wm->server->loop,
1278 weston_wm_window_configure, window);
1279}
1280
1281static const struct weston_shell_client shell_client = {
1282 send_configure
1283};
1284
1285static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001286xserver_map_shell_surface(struct weston_wm *wm,
1287 struct weston_wm_window *window)
1288{
1289 struct weston_shell_interface *shell_interface =
1290 &wm->server->compositor->shell_interface;
1291 struct weston_wm_window *parent;
1292 struct theme *t = window->wm->theme;
1293
1294 if (!shell_interface->create_shell_surface)
1295 return;
1296
1297 window->shsurf =
1298 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001299 window->surface,
1300 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001301
1302 if (!window->transient_for) {
1303 shell_interface->set_toplevel(window->shsurf);
1304 return;
1305 }
1306
1307 parent = hash_table_lookup(wm->window_hash, window->transient_for->id);
1308 shell_interface->set_transient(window->shsurf, parent->shsurf,
1309 window->x - parent->x + t->margin + t->width,
1310 window->y - parent->y + t->margin + t->titlebar_height,
1311 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1312}
1313
1314static void
1315xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1316 struct wl_resource *surface_resource, uint32_t id)
1317{
1318 struct weston_xserver *wxs = resource->data;
1319 struct weston_wm *wm = wxs->wm;
1320 struct wl_surface *surface = surface_resource->data;
1321 struct weston_wm_window *window;
1322
1323 if (client != wxs->client)
1324 return;
1325
1326 window = hash_table_lookup(wm->window_hash, id);
1327 if (window == NULL) {
1328 fprintf(stderr, "set_window_id for unknown window %d\n", id);
1329 return;
1330 }
1331
1332 fprintf(stderr, "set_window_id %d for surface %p\n", id, surface);
1333
1334 weston_wm_window_read_properties(window);
1335
1336 window->surface = (struct weston_surface *) surface;
1337 window->surface_destroy_listener.notify = surface_destroy;
1338 wl_signal_add(&surface->resource.destroy_signal,
1339 &window->surface_destroy_listener);
1340
1341 weston_wm_window_schedule_repaint(window);
1342 xserver_map_shell_surface(wm, window);
1343}
1344
1345const struct xserver_interface xserver_implementation = {
1346 xserver_set_window_id
1347};