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