blob: bffb5ffe735f3116a94e55cbe3641ae2a50d5bc5 [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
747weston_wm_handle_client_message(struct weston_wm *wm,
748 xcb_generic_event_t *event)
749{
750 xcb_client_message_event_t *client_message =
751 (xcb_client_message_event_t *) event;
752 struct weston_shell_interface *shell_interface =
753 &wm->server->compositor->shell_interface;
754 struct weston_wm_window *window;
755 struct weston_seat *seat;
756
757 window = hash_table_lookup(wm->window_hash, client_message->window);
758
759 fprintf(stderr, "XCB_CLIENT_MESSAGE (%s %d %d %d %d %d)\n",
760 get_atom_name(wm->conn, client_message->type),
761 client_message->data.data32[0],
762 client_message->data.data32[1],
763 client_message->data.data32[2],
764 client_message->data.data32[3],
765 client_message->data.data32[4]);
766
767 seat = wm->server->compositor->seat;
768 if (client_message->type == wm->atom.net_wm_moveresize &&
769 client_message->data.data32[2] == _NET_WM_MOVERESIZE_MOVE &&
770 seat->seat.pointer->button_count == 1 &&
771 seat->seat.pointer->focus == &window->surface->surface)
772 shell_interface->move(window->shsurf, seat);
773}
774
775static void
776weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
777{
778 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
779 struct weston_shell_interface *shell_interface =
780 &wm->server->compositor->shell_interface;
781 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400782 enum theme_location location;
783 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400784 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400785
786 fprintf(stderr, "XCB_BUTTON_%s (detail %d)\n",
787 button->response_type == XCB_BUTTON_PRESS ?
788 "PRESS" : "RELEASE", button->detail);
789
790 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400791 weston_wm_window_get_frame_size(window, &width, &height);
792
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400793 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400794 button->detail == 1) {
795 location = theme_get_location(t,
796 button->event_x,
797 button->event_y,
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400798 width, height);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400799
800 switch (location) {
801 case THEME_LOCATION_TITLEBAR:
802 shell_interface->move(window->shsurf,
803 wm->server->compositor->seat);
804 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400805 case THEME_LOCATION_RESIZING_TOP:
806 case THEME_LOCATION_RESIZING_BOTTOM:
807 case THEME_LOCATION_RESIZING_LEFT:
808 case THEME_LOCATION_RESIZING_RIGHT:
809 case THEME_LOCATION_RESIZING_TOP_LEFT:
810 case THEME_LOCATION_RESIZING_TOP_RIGHT:
811 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
812 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
813 shell_interface->resize(window->shsurf,
814 wm->server->compositor->seat,
815 location);
816 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400817 default:
818 break;
819 }
820 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400821}
822
823static int
824weston_wm_handle_event(int fd, uint32_t mask, void *data)
825{
826 struct weston_wm *wm = data;
827 xcb_generic_event_t *event;
828 int count = 0;
829
830 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
831 if (weston_wm_handle_selection_event(wm, event)) {
832 fprintf(stderr, "handled by selection code\n");
833 free(event);
834 count++;
835 continue;
836 }
837
838 switch (event->response_type & ~0x80) {
839 case XCB_BUTTON_PRESS:
840 case XCB_BUTTON_RELEASE:
841 weston_wm_handle_button(wm, event);
842 break;
843 case XCB_EXPOSE:
844 weston_wm_handle_expose(wm, event);
845 break;
846 case XCB_CREATE_NOTIFY:
847 weston_wm_handle_create_notify(wm, event);
848 break;
849 case XCB_MAP_REQUEST:
850 weston_wm_handle_map_request(wm, event);
851 break;
852 case XCB_MAP_NOTIFY:
853 weston_wm_handle_map_notify(wm, event);
854 break;
855 case XCB_UNMAP_NOTIFY:
856 fprintf(stderr, "XCB_UNMAP_NOTIFY\n");
857 break;
858 case XCB_CONFIGURE_REQUEST:
859 weston_wm_handle_configure_request(wm, event);
860 break;
861 case XCB_CONFIGURE_NOTIFY:
862 weston_wm_handle_configure_notify(wm, event);
863 break;
864 case XCB_DESTROY_NOTIFY:
865 weston_wm_handle_destroy_notify(wm, event);
866 break;
867 case XCB_MAPPING_NOTIFY:
868 fprintf(stderr, "XCB_MAPPING_NOTIFY\n");
869 break;
870 case XCB_PROPERTY_NOTIFY:
871 weston_wm_handle_property_notify(wm, event);
872 break;
873 case XCB_CLIENT_MESSAGE:
874 weston_wm_handle_client_message(wm, event);
875 break;
876 }
877
878 free(event);
879 count++;
880 }
881
882 xcb_flush(wm->conn);
883
884 return count;
885}
886
887static void
888wxs_wm_get_resources(struct weston_wm *wm)
889{
890
891#define F(field) offsetof(struct weston_wm, field)
892
893 static const struct { const char *name; int offset; } atoms[] = {
894 { "WM_PROTOCOLS", F(atom.wm_protocols) },
895 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
896 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
897 { "_NET_WM_NAME", F(atom.net_wm_name) },
898 { "_NET_WM_ICON", F(atom.net_wm_icon) },
899 { "_NET_WM_STATE", F(atom.net_wm_state) },
900 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
901 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
902 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
903 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
904
905 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
906 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
907 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
908 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
909 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
910 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
911 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
912 { "_NET_WM_WINDOW_TYPE_DROPDOWN", F(atom.net_wm_window_type_dropdown) },
913 { "_NET_WM_WINDOW_TYPE_POPUP", F(atom.net_wm_window_type_popup) },
914 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
915 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
916 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
917 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
918 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
919
920 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
921 { "_NET_SUPPORTING_WM_CHECK",
922 F(atom.net_supporting_wm_check) },
923 { "_NET_SUPPORTED", F(atom.net_supported) },
924 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
925 { "CLIPBOARD", F(atom.clipboard) },
926 { "TARGETS", F(atom.targets) },
927 { "UTF8_STRING", F(atom.utf8_string) },
928 { "_WL_SELECTION", F(atom.wl_selection) },
929 { "INCR", F(atom.incr) },
930 { "TIMESTAMP", F(atom.timestamp) },
931 { "MULTIPLE", F(atom.multiple) },
932 { "UTF8_STRING" , F(atom.utf8_string) },
933 { "COMPOUND_TEXT", F(atom.compound_text) },
934 { "TEXT", F(atom.text) },
935 { "STRING", F(atom.string) },
936 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
937 { "text/plain", F(atom.text_plain) },
938 };
939#undef F
940
941 xcb_xfixes_query_version_cookie_t xfixes_cookie;
942 xcb_xfixes_query_version_reply_t *xfixes_reply;
943 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
944 xcb_intern_atom_reply_t *reply;
945 xcb_render_query_pict_formats_reply_t *formats_reply;
946 xcb_render_query_pict_formats_cookie_t formats_cookie;
947 xcb_render_pictforminfo_t *formats;
948 uint32_t i;
949
950 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
951
952 formats_cookie = xcb_render_query_pict_formats(wm->conn);
953
954 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
955 cookies[i] = xcb_intern_atom (wm->conn, 0,
956 strlen(atoms[i].name),
957 atoms[i].name);
958
959 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
960 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
961 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
962 free(reply);
963 }
964
965 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
966 if (!wm->xfixes || !wm->xfixes->present)
967 fprintf(stderr, "xfixes not available\n");
968
969 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
970 XCB_XFIXES_MAJOR_VERSION,
971 XCB_XFIXES_MINOR_VERSION);
972 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
973 xfixes_cookie, NULL);
974
975 printf("xfixes version: %d.%d\n",
976 xfixes_reply->major_version, xfixes_reply->minor_version);
977
978 free(xfixes_reply);
979
980 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
981 formats_cookie, 0);
982 if (formats_reply == NULL)
983 return;
984
985 formats = xcb_render_query_pict_formats_formats(formats_reply);
986 for (i = 0; i < formats_reply->length; i++)
987 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
988 formats[i].depth == 24)
989 wm->render_format = formats[i];
990
991 free(formats_reply);
992}
993
994static void
995weston_wm_create_wm_window(struct weston_wm *wm)
996{
997 static const char name[] = "Weston WM";
998
999 wm->wm_window = xcb_generate_id(wm->conn);
1000 xcb_create_window(wm->conn,
1001 XCB_COPY_FROM_PARENT,
1002 wm->wm_window,
1003 wm->screen->root,
1004 0, 0,
1005 10, 10,
1006 0,
1007 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1008 wm->screen->root_visual,
1009 0, NULL);
1010
1011 xcb_change_property(wm->conn,
1012 XCB_PROP_MODE_REPLACE,
1013 wm->wm_window,
1014 wm->atom.net_supporting_wm_check,
1015 XCB_ATOM_WINDOW,
1016 32, /* format */
1017 1, &wm->wm_window);
1018
1019 xcb_change_property(wm->conn,
1020 XCB_PROP_MODE_REPLACE,
1021 wm->wm_window,
1022 wm->atom.net_wm_name,
1023 wm->atom.utf8_string,
1024 8, /* format */
1025 strlen(name), name);
1026
1027 xcb_change_property(wm->conn,
1028 XCB_PROP_MODE_REPLACE,
1029 wm->screen->root,
1030 wm->atom.net_supporting_wm_check,
1031 XCB_ATOM_WINDOW,
1032 32, /* format */
1033 1, &wm->wm_window);
1034
1035}
1036
1037struct weston_wm *
1038weston_wm_create(struct weston_xserver *wxs)
1039{
1040 struct wl_seat *seat;
1041 struct weston_wm *wm;
1042 struct wl_event_loop *loop;
1043 xcb_screen_iterator_t s;
1044 uint32_t values[1], mask;
1045 int sv[2];
1046 xcb_atom_t supported[1];
1047
1048 wm = malloc(sizeof *wm);
1049 if (wm == NULL)
1050 return NULL;
1051
1052 memset(wm, 0, sizeof *wm);
1053 wm->server = wxs;
1054 wm->window_hash = hash_table_create();
1055 if (wm->window_hash == NULL) {
1056 free(wm);
1057 return NULL;
1058 }
1059
1060 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
1061 fprintf(stderr, "socketpair failed\n");
1062 hash_table_destroy(wm->window_hash);
1063 free(wm);
1064 return NULL;
1065 }
1066
1067 xserver_send_client(wxs->resource, sv[1]);
1068 wl_client_flush(wxs->resource->client);
1069 close(sv[1]);
1070
1071 /* xcb_connect_to_fd takes ownership of the fd. */
1072 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1073 if (xcb_connection_has_error(wm->conn)) {
1074 fprintf(stderr, "xcb_connect_to_fd failed\n");
1075 close(sv[0]);
1076 hash_table_destroy(wm->window_hash);
1077 free(wm);
1078 return NULL;
1079 }
1080
1081 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1082 wm->screen = s.data;
1083
1084 loop = wl_display_get_event_loop(wxs->wl_display);
1085 wm->source =
1086 wl_event_loop_add_fd(loop, sv[0],
1087 WL_EVENT_READABLE,
1088 weston_wm_handle_event, wm);
1089 wl_event_source_check(wm->source);
1090
1091 wxs_wm_get_resources(wm);
1092
1093 values[0] =
1094 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
1095 XCB_EVENT_MASK_RESIZE_REDIRECT |
1096 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1097 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1098 XCB_EVENT_MASK_PROPERTY_CHANGE;
1099 xcb_change_window_attributes(wm->conn, wm->screen->root,
1100 XCB_CW_EVENT_MASK, values);
1101 wm->theme = theme_create();
1102
1103 weston_wm_create_wm_window(wm);
1104
1105 supported[0] = wm->atom.net_wm_moveresize;
1106 xcb_change_property(wm->conn,
1107 XCB_PROP_MODE_REPLACE,
1108 wm->screen->root,
1109 wm->atom.net_supported,
1110 XCB_ATOM_ATOM,
1111 32, /* format */
1112 ARRAY_LENGTH(supported), supported);
1113
1114 wm->selection_request.requestor = XCB_NONE;
1115
1116 wm->selection_window = xcb_generate_id(wm->conn);
1117 xcb_create_window(wm->conn,
1118 XCB_COPY_FROM_PARENT,
1119 wm->selection_window,
1120 wm->screen->root,
1121 0, 0,
1122 10, 10,
1123 0,
1124 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1125 wm->screen->root_visual,
1126 XCB_CW_EVENT_MASK, values);
1127
1128 mask =
1129 XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER |
1130 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY |
1131 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE;
1132
1133 xcb_xfixes_select_selection_input(wm->conn, wm->selection_window,
1134 wm->atom.clipboard, mask);
1135
1136 xcb_flush(wm->conn);
1137
1138 seat = &wxs->compositor->seat->seat;
1139 wm->selection_listener.notify = weston_wm_set_selection;
1140 wl_signal_add(&seat->selection_signal, &wm->selection_listener);
1141
1142 wm->activate_listener.notify = weston_wm_window_activate;
1143 wl_signal_add(&wxs->compositor->activate_signal,
1144 &wm->activate_listener);
1145
1146 fprintf(stderr, "created wm\n");
1147
1148 return wm;
1149}
1150
1151void
1152weston_wm_destroy(struct weston_wm *wm)
1153{
1154 /* FIXME: Free windows in hash. */
1155 hash_table_destroy(wm->window_hash);
1156 xcb_disconnect(wm->conn);
1157 wl_event_source_remove(wm->source);
1158 wl_list_remove(&wm->selection_listener.link);
1159 wl_list_remove(&wm->activate_listener.link);
1160
1161 free(wm);
1162}
1163
1164static void
1165surface_destroy(struct wl_listener *listener, void *data)
1166{
1167 struct weston_wm_window *window =
1168 container_of(listener,
1169 struct weston_wm_window, surface_destroy_listener);
1170
1171 fprintf(stderr, "surface for xid %d destroyed\n", window->id);
1172}
1173
1174static struct weston_wm_window *
1175get_wm_window(struct weston_surface *surface)
1176{
1177 struct wl_resource *resource = &surface->surface.resource;
1178 struct wl_listener *listener;
1179
1180 listener = wl_signal_get(&resource->destroy_signal, surface_destroy);
1181 if (listener)
1182 return container_of(listener, struct weston_wm_window,
1183 surface_destroy_listener);
1184
1185 return NULL;
1186}
1187
1188static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001189weston_wm_window_configure(void *data)
1190{
1191 struct weston_wm_window *window = data;
1192 struct weston_wm *wm = window->wm;
1193 uint32_t values[2];
1194 int width, height;
1195
1196 values[0] = window->width;
1197 values[1] = window->height;
1198 xcb_configure_window(wm->conn,
1199 window->id,
1200 XCB_CONFIG_WINDOW_WIDTH |
1201 XCB_CONFIG_WINDOW_HEIGHT,
1202 values);
1203
1204 weston_wm_window_get_frame_size(window, &width, &height);
1205 values[0] = width;
1206 values[1] = height;
1207 xcb_configure_window(wm->conn,
1208 window->frame_id,
1209 XCB_CONFIG_WINDOW_WIDTH |
1210 XCB_CONFIG_WINDOW_HEIGHT,
1211 values);
1212
1213 window->configure_source = NULL;
1214
1215 weston_wm_window_schedule_repaint(window);
1216}
1217
1218static void
1219send_configure(struct weston_surface *surface,
1220 uint32_t edges, int32_t width, int32_t height)
1221{
1222 struct weston_wm_window *window = get_wm_window(surface);
1223 struct weston_wm *wm = window->wm;
1224 struct theme *t = window->wm->theme;
1225
1226 if (window->decorate) {
1227 window->width = width - 2 * (t->margin + t->width);
1228 window->height = height - 2 * t->margin -
1229 t->titlebar_height - t->width;
1230 } else {
1231 window->width = width - 2 * t->margin;
1232 window->height = height - 2 * t->margin;
1233 }
1234
1235 if (window->configure_source)
1236 return;
1237
1238 window->configure_source =
1239 wl_event_loop_add_idle(wm->server->loop,
1240 weston_wm_window_configure, window);
1241}
1242
1243static const struct weston_shell_client shell_client = {
1244 send_configure
1245};
1246
1247static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001248xserver_map_shell_surface(struct weston_wm *wm,
1249 struct weston_wm_window *window)
1250{
1251 struct weston_shell_interface *shell_interface =
1252 &wm->server->compositor->shell_interface;
1253 struct weston_wm_window *parent;
1254 struct theme *t = window->wm->theme;
1255
1256 if (!shell_interface->create_shell_surface)
1257 return;
1258
1259 window->shsurf =
1260 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001261 window->surface,
1262 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001263
1264 if (!window->transient_for) {
1265 shell_interface->set_toplevel(window->shsurf);
1266 return;
1267 }
1268
1269 parent = hash_table_lookup(wm->window_hash, window->transient_for->id);
1270 shell_interface->set_transient(window->shsurf, parent->shsurf,
1271 window->x - parent->x + t->margin + t->width,
1272 window->y - parent->y + t->margin + t->titlebar_height,
1273 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1274}
1275
1276static void
1277xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1278 struct wl_resource *surface_resource, uint32_t id)
1279{
1280 struct weston_xserver *wxs = resource->data;
1281 struct weston_wm *wm = wxs->wm;
1282 struct wl_surface *surface = surface_resource->data;
1283 struct weston_wm_window *window;
1284
1285 if (client != wxs->client)
1286 return;
1287
1288 window = hash_table_lookup(wm->window_hash, id);
1289 if (window == NULL) {
1290 fprintf(stderr, "set_window_id for unknown window %d\n", id);
1291 return;
1292 }
1293
1294 fprintf(stderr, "set_window_id %d for surface %p\n", id, surface);
1295
1296 weston_wm_window_read_properties(window);
1297
1298 window->surface = (struct weston_surface *) surface;
1299 window->surface_destroy_listener.notify = surface_destroy;
1300 wl_signal_add(&surface->resource.destroy_signal,
1301 &window->surface_destroy_listener);
1302
1303 weston_wm_window_schedule_repaint(window);
1304 xserver_map_shell_surface(wm, window);
1305}
1306
1307const struct xserver_interface xserver_implementation = {
1308 xserver_set_window_id
1309};