blob: 378daeee18ca687beb1b32f965c4ca2336f739b2 [file] [log] [blame]
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
23#define _GNU_SOURCE
24
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <sys/socket.h>
29#include <sys/un.h>
30#include <fcntl.h>
31#include <errno.h>
32#include <unistd.h>
33#include <signal.h>
34
35#include "xwayland.h"
36
37#include "../../shared/cairo-util.h"
38#include "../compositor.h"
39#include "xserver-server-protocol.h"
40#include "hash.h"
41
42struct motif_wm_hints {
43 uint32_t flags;
44 uint32_t functions;
45 uint32_t decorations;
46 int32_t input_mode;
47 uint32_t status;
48};
49
50#define MWM_HINTS_FUNCTIONS (1L << 0)
51#define MWM_HINTS_DECORATIONS (1L << 1)
52#define MWM_HINTS_INPUT_MODE (1L << 2)
53#define MWM_HINTS_STATUS (1L << 3)
54
55#define MWM_FUNC_ALL (1L << 0)
56#define MWM_FUNC_RESIZE (1L << 1)
57#define MWM_FUNC_MOVE (1L << 2)
58#define MWM_FUNC_MINIMIZE (1L << 3)
59#define MWM_FUNC_MAXIMIZE (1L << 4)
60#define MWM_FUNC_CLOSE (1L << 5)
61
62#define MWM_DECOR_ALL (1L << 0)
63#define MWM_DECOR_BORDER (1L << 1)
64#define MWM_DECOR_RESIZEH (1L << 2)
65#define MWM_DECOR_TITLE (1L << 3)
66#define MWM_DECOR_MENU (1L << 4)
67#define MWM_DECOR_MINIMIZE (1L << 5)
68#define MWM_DECOR_MAXIMIZE (1L << 6)
69
70#define MWM_INPUT_MODELESS 0
71#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
72#define MWM_INPUT_SYSTEM_MODAL 2
73#define MWM_INPUT_FULL_APPLICATION_MODAL 3
74#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
75
76#define MWM_TEAROFF_WINDOW (1L<<0)
77
78#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
79#define _NET_WM_MOVERESIZE_SIZE_TOP 1
80#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
81#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
82#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
83#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
84#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
85#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
86#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
87#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
88#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
89#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
90
91
92
93struct weston_wm_window {
94 struct weston_wm *wm;
95 xcb_window_t id;
96 xcb_window_t frame_id;
97 cairo_surface_t *cairo_surface;
98 struct weston_surface *surface;
99 struct shell_surface *shsurf;
100 struct wl_listener surface_destroy_listener;
101 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400102 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400103 int properties_dirty;
104 char *class;
105 char *name;
106 struct weston_wm_window *transient_for;
107 uint32_t protocols;
108 xcb_atom_t type;
109 int width, height;
110 int x, y;
111 int decorate;
112};
113
114static struct weston_wm_window *
115get_wm_window(struct weston_surface *surface);
116
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400117static void
118weston_wm_window_schedule_repaint(struct weston_wm_window *window);
119
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400120const char *
121get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
122{
123 xcb_get_atom_name_cookie_t cookie;
124 xcb_get_atom_name_reply_t *reply;
125 xcb_generic_error_t *e;
126 static char buffer[64];
127
128 if (atom == XCB_ATOM_NONE)
129 return "None";
130
131 cookie = xcb_get_atom_name (c, atom);
132 reply = xcb_get_atom_name_reply (c, cookie, &e);
133 snprintf(buffer, sizeof buffer, "%.*s",
134 xcb_get_atom_name_name_length (reply),
135 xcb_get_atom_name_name (reply));
136 free(reply);
137
138 return buffer;
139}
140
141void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400142dump_property(struct weston_wm *wm,
143 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400144{
145 int32_t *incr_value;
146 const char *text_value, *name;
147 xcb_atom_t *atom_value;
148 int width, len;
149 uint32_t i;
150
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400151 width = fprintf(stderr, "%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400152 if (reply == NULL) {
153 fprintf(stderr, "(no reply)\n");
154 return;
155 }
156
157 width += fprintf(stderr,
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400158 "%s/%d, length %d (value_len %d): ",
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400159 get_atom_name(wm->conn, reply->type),
160 reply->format,
161 xcb_get_property_value_length(reply),
162 reply->value_len);
163
164 if (reply->type == wm->atom.incr) {
165 incr_value = xcb_get_property_value(reply);
166 fprintf(stderr, "%d\n", *incr_value);
167 } else if (reply->type == wm->atom.utf8_string ||
168 reply->type == wm->atom.string) {
169 text_value = xcb_get_property_value(reply);
170 if (reply->value_len > 40)
171 len = 40;
172 else
173 len = reply->value_len;
174 fprintf(stderr, "\"%.*s\"\n", len, text_value);
175 } else if (reply->type == XCB_ATOM_ATOM) {
176 atom_value = xcb_get_property_value(reply);
177 for (i = 0; i < reply->value_len; i++) {
178 name = get_atom_name(wm->conn, atom_value[i]);
179 if (width + strlen(name) + 2 > 78) {
180 fprintf(stderr, "\n ");
181 width = 4;
182 } else if (i > 0) {
183 width += fprintf(stderr, ", ");
184 }
185
186 width += fprintf(stderr, "%s", name);
187 }
188 fprintf(stderr, "\n");
189 } else {
190 fprintf(stderr, "huh?\n");
191 }
192}
193
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400194void
195read_and_dump_property(struct weston_wm *wm,
196 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400197{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400198 xcb_get_property_reply_t *reply;
199 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400200
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400201 cookie = xcb_get_property(wm->conn, 0, window,
202 property, XCB_ATOM_ANY, 0, 2048);
203 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400204
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400205 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400206
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400207 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400208}
209
210/* We reuse some predefined, but otherwise useles atoms */
211#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
212#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
213
214static void
215weston_wm_window_read_properties(struct weston_wm_window *window)
216{
217 struct weston_wm *wm = window->wm;
218
219#define F(field) offsetof(struct weston_wm_window, field)
220 const struct {
221 xcb_atom_t atom;
222 xcb_atom_t type;
223 int offset;
224 } props[] = {
225 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
226 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
227 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
228 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
229 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
230 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
231 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
232 };
233#undef F
234
235 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
236 xcb_get_property_reply_t *reply;
237 void *p;
238 uint32_t *xid;
239 xcb_atom_t *atom;
240 uint32_t i;
241 struct motif_wm_hints *hints;
242
243 if (!window->properties_dirty)
244 return;
245 window->properties_dirty = 0;
246
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400247 for (i = 0; i < ARRAY_LENGTH(props); i++)
248 cookie[i] = xcb_get_property(wm->conn,
249 0, /* delete */
250 window->id,
251 props[i].atom,
252 XCB_ATOM_ANY, 0, 2048);
253
254 window->decorate = 1;
255 for (i = 0; i < ARRAY_LENGTH(props); i++) {
256 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
257 if (!reply)
258 /* Bad window, typically */
259 continue;
260 if (reply->type == XCB_ATOM_NONE) {
261 /* No such property */
262 free(reply);
263 continue;
264 }
265
266 p = ((char *) window + props[i].offset);
267
268 switch (props[i].type) {
269 case XCB_ATOM_STRING:
270 /* FIXME: We're using this for both string and
271 utf8_string */
272 if (*(char **) p)
273 free(*(char **) p);
274
275 *(char **) p =
276 strndup(xcb_get_property_value(reply),
277 xcb_get_property_value_length(reply));
278 break;
279 case XCB_ATOM_WINDOW:
280 xid = xcb_get_property_value(reply);
281 *(struct weston_wm_window **) p =
282 hash_table_lookup(wm->window_hash, *xid);
283 break;
284 case XCB_ATOM_ATOM:
285 atom = xcb_get_property_value(reply);
286 *(xcb_atom_t *) p = *atom;
287 break;
288 case TYPE_WM_PROTOCOLS:
289 break;
290 case TYPE_MOTIF_WM_HINTS:
291 hints = xcb_get_property_value(reply);
292 if (hints->flags & MWM_HINTS_DECORATIONS)
293 window->decorate = hints->decorations > 0;
294 break;
295 default:
296 break;
297 }
298 free(reply);
299 }
300}
301
302static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400303weston_wm_window_get_frame_size(struct weston_wm_window *window,
304 int *width, int *height)
305{
306 struct theme *t = window->wm->theme;
307
308 if (window->decorate) {
309 *width = window->width + (t->margin + t->width) * 2;
310 *height = window->height +
311 t->margin * 2 + t->width + t->titlebar_height;
312 } else {
313 *width = window->width + t->margin * 2;
314 *height = window->height + t->margin * 2;
315 }
316}
317
318static void
319weston_wm_window_get_child_position(struct weston_wm_window *window,
320 int *x, int *y)
321{
322 struct theme *t = window->wm->theme;
323
324 if (window->decorate) {
325 *x = t->margin + t->width;
326 *y = t->margin + t->titlebar_height;
327 } else {
328 *x = t->margin;
329 *y = t->margin;
330 }
331}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400332
333static void
334weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
335{
336 xcb_configure_request_event_t *configure_request =
337 (xcb_configure_request_event_t *) event;
338 struct weston_wm_window *window;
339 uint32_t mask, values[16];
340 int x, y, width, height, i = 0;
341
342 fprintf(stderr, "XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
343 configure_request->window,
344 configure_request->x, configure_request->y,
345 configure_request->width, configure_request->height);
346
347 window = hash_table_lookup(wm->window_hash, configure_request->window);
348
349 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
350 window->width = configure_request->width;
351 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
352 window->height = configure_request->height;
353
354 weston_wm_window_get_child_position(window, &x, &y);
355 values[i++] = x;
356 values[i++] = y;
357 values[i++] = window->width;
358 values[i++] = window->height;
359 values[i++] = 0;
360 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
361 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
362 XCB_CONFIG_WINDOW_BORDER_WIDTH;
363 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
364 values[i++] = configure_request->sibling;
365 mask |= XCB_CONFIG_WINDOW_SIBLING;
366 }
367 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
368 values[i++] = configure_request->stack_mode;
369 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
370 }
371
372 xcb_configure_window(wm->conn, window->id, mask, values);
373
374 weston_wm_window_get_frame_size(window, &width, &height);
375 values[0] = width;
376 values[1] = height;
377 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
378 xcb_configure_window(wm->conn, window->frame_id, mask, values);
379
380 weston_wm_window_schedule_repaint(window);
381}
382
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400383static void
384weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
385{
386 xcb_configure_notify_event_t *configure_notify =
387 (xcb_configure_notify_event_t *) event;
388 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400389
390 window = hash_table_lookup(wm->window_hash, configure_notify->window);
391
392 fprintf(stderr, "XCB_CONFIGURE_NOTIFY (%s window %d) %d,%d @ %dx%d\n",
393 configure_notify->window == window->id ? "client" : "frame",
394 configure_notify->window,
395 configure_notify->x, configure_notify->y,
396 configure_notify->width, configure_notify->height);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400397}
398
399static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400400weston_wm_window_activate(struct wl_listener *listener, void *data)
401{
402 struct weston_surface *surface = data;
403 struct weston_wm_window *window = get_wm_window(surface);
Scott Moreau85ecac02012-05-21 15:49:14 -0600404 struct weston_wm *wm =
405 container_of(listener, struct weston_wm, activate_listener);
406 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400407
Scott Moreau85ecac02012-05-21 15:49:14 -0600408 if (window) {
409 client_message.response_type = XCB_CLIENT_MESSAGE;
410 client_message.format = 32;
411 client_message.window = window->id;
412 client_message.type = wm->atom.wm_protocols;
413 client_message.data.data32[0] = wm->atom.wm_take_focus;
414 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
415
416 xcb_send_event(wm->conn, 0, window->id,
417 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
418 (char *) &client_message);
419
420 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
421 window->id, XCB_TIME_CURRENT_TIME);
422 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400423 xcb_set_input_focus (wm->conn,
424 XCB_INPUT_FOCUS_POINTER_ROOT,
425 XCB_NONE,
426 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600427 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400428
429 if (wm->focus_window)
430 weston_wm_window_schedule_repaint(wm->focus_window);
431 wm->focus_window = window;
432 if (wm->focus_window)
433 weston_wm_window_schedule_repaint(wm->focus_window);
434}
435
436static int
437our_resource(struct weston_wm *wm, uint32_t id)
438{
439 const xcb_setup_t *setup;
440
441 setup = xcb_get_setup(wm->conn);
442
443 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
444}
445
446static void
447weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
448{
449 xcb_map_request_event_t *map_request =
450 (xcb_map_request_event_t *) event;
451 struct weston_wm_window *window;
452 uint32_t values[1];
453 int x, y, width, height;
454
455 if (our_resource(wm, map_request->window)) {
456 fprintf(stderr, "XCB_MAP_REQUEST (window %d, ours)\n",
457 map_request->window);
458 return;
459 }
460
461 window = hash_table_lookup(wm->window_hash, map_request->window);
462
463 weston_wm_window_read_properties(window);
464
465 weston_wm_window_get_frame_size(window, &width, &height);
466 weston_wm_window_get_child_position(window, &x, &y);
467
468 values[0] =
469 XCB_EVENT_MASK_KEY_PRESS |
470 XCB_EVENT_MASK_KEY_RELEASE |
471 XCB_EVENT_MASK_BUTTON_PRESS |
472 XCB_EVENT_MASK_BUTTON_RELEASE |
473 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400474 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
475 XCB_EVENT_MASK_RESIZE_REDIRECT |
476 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400477 XCB_EVENT_MASK_EXPOSURE;
478
479 window->frame_id = xcb_generate_id(wm->conn);
480 xcb_create_window(wm->conn,
481 XCB_COPY_FROM_PARENT,
482 window->frame_id,
483 wm->screen->root,
484 0, 0,
485 width, height,
486 0,
487 XCB_WINDOW_CLASS_INPUT_OUTPUT,
488 wm->screen->root_visual,
489 XCB_CW_EVENT_MASK, values);
490 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
491
492 values[0] = 0;
493 xcb_configure_window(wm->conn, window->id,
494 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
495
496 fprintf(stderr, "XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
497 window->id, window, window->frame_id);
498
499 xcb_change_save_set(wm->conn, XCB_SET_MODE_DELETE, window->id);
500
501 xcb_map_window(wm->conn, map_request->window);
502 xcb_map_window(wm->conn, window->frame_id);
503
504 window->cairo_surface =
505 cairo_xcb_surface_create_with_xrender_format(wm->conn,
506 wm->screen,
507 window->frame_id,
508 &wm->render_format,
509 width, height);
510
511 hash_table_insert(wm->window_hash, window->frame_id, window);
512}
513
514static void
515weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
516{
517 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
518
519 if (our_resource(wm, map_notify->window)) {
520 fprintf(stderr, "XCB_MAP_NOTIFY (window %d, ours)\n",
521 map_notify->window);
522 return;
523 }
524
525 fprintf(stderr, "XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
526}
527
528static void
529weston_wm_window_draw_decoration(void *data)
530{
531 struct weston_wm_window *window = data;
532 struct weston_wm *wm = window->wm;
533 struct theme *t = wm->theme;
534 cairo_t *cr;
535 int x, y, width, height;
536 const char *title;
537 uint32_t flags = 0;
538
539 weston_wm_window_read_properties(window);
540
541 window->repaint_source = NULL;
542
543 weston_wm_window_get_frame_size(window, &width, &height);
544 weston_wm_window_get_child_position(window, &x, &y);
545
546 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
547 cr = cairo_create(window->cairo_surface);
548
549 if (window->decorate) {
550 if (wm->focus_window == window)
551 flags |= THEME_FRAME_ACTIVE;
552
553 if (window->name)
554 title = window->name;
555 else
556 title = "untitled";
557
558 theme_render_frame(t, cr, width, height, title, flags);
559 } else {
560 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
561 cairo_set_source_rgba(cr, 0, 0, 0, 0);
562 cairo_paint(cr);
563
564 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
565 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
566 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
567 }
568
569 cairo_destroy(cr);
570
571 if (window->surface) {
572 /* We leave an extra pixel around the X window area to
573 * make sure we don't sample from the undefined alpha
574 * channel when filtering. */
575 window->surface->opaque_rect[0] =
576 (double) (x - 1) / width;
577 window->surface->opaque_rect[1] =
578 (double) (x + window->width + 1) / width;
579 window->surface->opaque_rect[2] =
580 (double) (y - 1) / height;
581 window->surface->opaque_rect[3] =
582 (double) (y + window->height + 1) / height;
583
584 pixman_region32_init_rect(&window->surface->input,
585 t->margin, t->margin,
586 width - 2 * t->margin,
587 height - 2 * t->margin);
588 }
589}
590
591static void
592weston_wm_window_schedule_repaint(struct weston_wm_window *window)
593{
594 struct weston_wm *wm = window->wm;
595
596 if (window->frame_id == XCB_WINDOW_NONE || window->repaint_source)
597 return;
598
599 window->repaint_source =
600 wl_event_loop_add_idle(wm->server->loop,
601 weston_wm_window_draw_decoration,
602 window);
603}
604
605static void
606weston_wm_handle_expose(struct weston_wm *wm, xcb_generic_event_t *event)
607{
608 struct weston_wm_window *window;
609 xcb_expose_event_t *expose = (xcb_expose_event_t *) event;
610
611 window = hash_table_lookup(wm->window_hash, expose->window);
612 fprintf(stderr, "XCB_EXPOSE (window %d, title %s, surface %p)\n",
613 window->id, window->name, window->surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400614}
615
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400616static void
617weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
618{
619 xcb_property_notify_event_t *property_notify =
620 (xcb_property_notify_event_t *) event;
621 struct weston_wm_window *window;
622
623 window = hash_table_lookup(wm->window_hash, property_notify->window);
624 if (window)
625 window->properties_dirty = 1;
626
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400627 fprintf(stderr, "XCB_PROPERTY_NOTIFY: window %d, ",
628 property_notify->window);
629 read_and_dump_property(wm, property_notify->window,
630 property_notify->atom);
631
632 if (property_notify->atom == wm->atom.net_wm_name ||
633 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400634 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400635}
636
637static void
638weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
639{
640 xcb_create_notify_event_t *create_notify =
641 (xcb_create_notify_event_t *) event;
642 struct weston_wm_window *window;
643 uint32_t values[1];
644
645 fprintf(stderr,
646 "XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
647 create_notify->window,
648 create_notify->width, create_notify->height,
649 create_notify->override_redirect ? ", override" : "",
650 our_resource(wm, create_notify->window) ? ", ours" : "");
651
652 if (our_resource(wm, create_notify->window))
653 return;
654
655 window = malloc(sizeof *window);
656 if (window == NULL) {
657 fprintf(stderr, "failed to allocate window\n");
658 return;
659 }
660
661 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
662 xcb_change_window_attributes(wm->conn, create_notify->window,
663 XCB_CW_EVENT_MASK, values);
664
665 memset(window, 0, sizeof *window);
666 window->wm = wm;
667 window->id = create_notify->window;
668 window->properties_dirty = 1;
669
670 window->width = create_notify->width;
671 window->height = create_notify->height;
672
673 hash_table_insert(wm->window_hash, window->id, window);
674}
675
676static void
677weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
678{
679 xcb_destroy_notify_event_t *destroy_notify =
680 (xcb_destroy_notify_event_t *) event;
681 struct weston_wm_window *window;
682
683 if (our_resource(wm, destroy_notify->window)) {
684 fprintf(stderr, "XCB_DESTROY_NOTIFY, win %d (ours)\n",
685 destroy_notify->window);
686 return;
687 }
688
689 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
690
691 fprintf(stderr, "XCB_DESTROY_NOTIFY, win %d (%p)\n",
692 destroy_notify->window, window);
693
694 if (window->repaint_source)
695 wl_event_source_remove(window->repaint_source);
696 if (window->cairo_surface)
697 cairo_surface_destroy(window->cairo_surface);
698
699 hash_table_remove(wm->window_hash, window->id);
700 hash_table_remove(wm->window_hash, window->frame_id);
701 xcb_destroy_window(wm->conn, window->frame_id);
702 if (window->surface)
703 wl_list_remove(&window->surface_destroy_listener.link);
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -0400704 if (wm->focus_window == window)
705 wm->focus_window = NULL;
706
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400707 free(window);
708}
709
710static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400711weston_wm_window_handle_moveresize(struct weston_wm_window *window,
712 xcb_client_message_event_t *client_message)
713{
714 static const int map[] = {
715 THEME_LOCATION_RESIZING_TOP_LEFT,
716 THEME_LOCATION_RESIZING_TOP,
717 THEME_LOCATION_RESIZING_TOP_RIGHT,
718 THEME_LOCATION_RESIZING_RIGHT,
719 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
720 THEME_LOCATION_RESIZING_BOTTOM,
721 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
722 THEME_LOCATION_RESIZING_LEFT
723 };
724
725 struct weston_wm *wm = window->wm;
726 struct weston_seat *seat = wm->server->compositor->seat;
727 int detail;
728 struct weston_shell_interface *shell_interface =
729 &wm->server->compositor->shell_interface;
730
731 if (seat->seat.pointer->button_count != 1 ||
732 seat->seat.pointer->focus != &window->surface->surface)
733 return;
734
735 detail = client_message->data.data32[2];
736 switch (detail) {
737 case _NET_WM_MOVERESIZE_MOVE:
738 shell_interface->move(window->shsurf, seat);
739 break;
740 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
741 case _NET_WM_MOVERESIZE_SIZE_TOP:
742 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
743 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
744 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
745 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
746 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
747 case _NET_WM_MOVERESIZE_SIZE_LEFT:
748 shell_interface->resize(window->shsurf, seat, map[detail]);
749 break;
750 case _NET_WM_MOVERESIZE_CANCEL:
751 break;
752 }
753}
754
755static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400756weston_wm_handle_client_message(struct weston_wm *wm,
757 xcb_generic_event_t *event)
758{
759 xcb_client_message_event_t *client_message =
760 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400761 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400762
763 window = hash_table_lookup(wm->window_hash, client_message->window);
764
765 fprintf(stderr, "XCB_CLIENT_MESSAGE (%s %d %d %d %d %d)\n",
766 get_atom_name(wm->conn, client_message->type),
767 client_message->data.data32[0],
768 client_message->data.data32[1],
769 client_message->data.data32[2],
770 client_message->data.data32[3],
771 client_message->data.data32[4]);
772
Kristian Høgsberge68fd102012-05-22 17:09:40 -0400773 if (client_message->type == wm->atom.net_wm_moveresize)
774 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400775}
776
777static void
778weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
779{
780 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
781 struct weston_shell_interface *shell_interface =
782 &wm->server->compositor->shell_interface;
783 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400784 enum theme_location location;
785 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400786 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400787
788 fprintf(stderr, "XCB_BUTTON_%s (detail %d)\n",
789 button->response_type == XCB_BUTTON_PRESS ?
790 "PRESS" : "RELEASE", button->detail);
791
792 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400793 weston_wm_window_get_frame_size(window, &width, &height);
794
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400795 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400796 button->detail == 1) {
797 location = theme_get_location(t,
798 button->event_x,
799 button->event_y,
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400800 width, height);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400801
802 switch (location) {
803 case THEME_LOCATION_TITLEBAR:
804 shell_interface->move(window->shsurf,
805 wm->server->compositor->seat);
806 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -0400807 case THEME_LOCATION_RESIZING_TOP:
808 case THEME_LOCATION_RESIZING_BOTTOM:
809 case THEME_LOCATION_RESIZING_LEFT:
810 case THEME_LOCATION_RESIZING_RIGHT:
811 case THEME_LOCATION_RESIZING_TOP_LEFT:
812 case THEME_LOCATION_RESIZING_TOP_RIGHT:
813 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
814 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
815 shell_interface->resize(window->shsurf,
816 wm->server->compositor->seat,
817 location);
818 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400819 default:
820 break;
821 }
822 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400823}
824
825static int
826weston_wm_handle_event(int fd, uint32_t mask, void *data)
827{
828 struct weston_wm *wm = data;
829 xcb_generic_event_t *event;
830 int count = 0;
831
832 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
833 if (weston_wm_handle_selection_event(wm, event)) {
834 fprintf(stderr, "handled by selection code\n");
835 free(event);
836 count++;
837 continue;
838 }
839
840 switch (event->response_type & ~0x80) {
841 case XCB_BUTTON_PRESS:
842 case XCB_BUTTON_RELEASE:
843 weston_wm_handle_button(wm, event);
844 break;
845 case XCB_EXPOSE:
846 weston_wm_handle_expose(wm, event);
847 break;
848 case XCB_CREATE_NOTIFY:
849 weston_wm_handle_create_notify(wm, event);
850 break;
851 case XCB_MAP_REQUEST:
852 weston_wm_handle_map_request(wm, event);
853 break;
854 case XCB_MAP_NOTIFY:
855 weston_wm_handle_map_notify(wm, event);
856 break;
857 case XCB_UNMAP_NOTIFY:
858 fprintf(stderr, "XCB_UNMAP_NOTIFY\n");
859 break;
860 case XCB_CONFIGURE_REQUEST:
861 weston_wm_handle_configure_request(wm, event);
862 break;
863 case XCB_CONFIGURE_NOTIFY:
864 weston_wm_handle_configure_notify(wm, event);
865 break;
866 case XCB_DESTROY_NOTIFY:
867 weston_wm_handle_destroy_notify(wm, event);
868 break;
869 case XCB_MAPPING_NOTIFY:
870 fprintf(stderr, "XCB_MAPPING_NOTIFY\n");
871 break;
872 case XCB_PROPERTY_NOTIFY:
873 weston_wm_handle_property_notify(wm, event);
874 break;
875 case XCB_CLIENT_MESSAGE:
876 weston_wm_handle_client_message(wm, event);
877 break;
878 }
879
880 free(event);
881 count++;
882 }
883
884 xcb_flush(wm->conn);
885
886 return count;
887}
888
889static void
890wxs_wm_get_resources(struct weston_wm *wm)
891{
892
893#define F(field) offsetof(struct weston_wm, field)
894
895 static const struct { const char *name; int offset; } atoms[] = {
896 { "WM_PROTOCOLS", F(atom.wm_protocols) },
897 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
898 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
899 { "_NET_WM_NAME", F(atom.net_wm_name) },
900 { "_NET_WM_ICON", F(atom.net_wm_icon) },
901 { "_NET_WM_STATE", F(atom.net_wm_state) },
902 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
903 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
904 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
905 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
906
907 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
908 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
909 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
910 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
911 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
912 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
913 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
914 { "_NET_WM_WINDOW_TYPE_DROPDOWN", F(atom.net_wm_window_type_dropdown) },
915 { "_NET_WM_WINDOW_TYPE_POPUP", F(atom.net_wm_window_type_popup) },
916 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
917 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
918 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
919 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
920 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
921
922 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
923 { "_NET_SUPPORTING_WM_CHECK",
924 F(atom.net_supporting_wm_check) },
925 { "_NET_SUPPORTED", F(atom.net_supported) },
926 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
927 { "CLIPBOARD", F(atom.clipboard) },
928 { "TARGETS", F(atom.targets) },
929 { "UTF8_STRING", F(atom.utf8_string) },
930 { "_WL_SELECTION", F(atom.wl_selection) },
931 { "INCR", F(atom.incr) },
932 { "TIMESTAMP", F(atom.timestamp) },
933 { "MULTIPLE", F(atom.multiple) },
934 { "UTF8_STRING" , F(atom.utf8_string) },
935 { "COMPOUND_TEXT", F(atom.compound_text) },
936 { "TEXT", F(atom.text) },
937 { "STRING", F(atom.string) },
938 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
939 { "text/plain", F(atom.text_plain) },
940 };
941#undef F
942
943 xcb_xfixes_query_version_cookie_t xfixes_cookie;
944 xcb_xfixes_query_version_reply_t *xfixes_reply;
945 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
946 xcb_intern_atom_reply_t *reply;
947 xcb_render_query_pict_formats_reply_t *formats_reply;
948 xcb_render_query_pict_formats_cookie_t formats_cookie;
949 xcb_render_pictforminfo_t *formats;
950 uint32_t i;
951
952 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
953
954 formats_cookie = xcb_render_query_pict_formats(wm->conn);
955
956 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
957 cookies[i] = xcb_intern_atom (wm->conn, 0,
958 strlen(atoms[i].name),
959 atoms[i].name);
960
961 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
962 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
963 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
964 free(reply);
965 }
966
967 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
968 if (!wm->xfixes || !wm->xfixes->present)
969 fprintf(stderr, "xfixes not available\n");
970
971 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
972 XCB_XFIXES_MAJOR_VERSION,
973 XCB_XFIXES_MINOR_VERSION);
974 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
975 xfixes_cookie, NULL);
976
977 printf("xfixes version: %d.%d\n",
978 xfixes_reply->major_version, xfixes_reply->minor_version);
979
980 free(xfixes_reply);
981
982 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
983 formats_cookie, 0);
984 if (formats_reply == NULL)
985 return;
986
987 formats = xcb_render_query_pict_formats_formats(formats_reply);
988 for (i = 0; i < formats_reply->length; i++)
989 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
990 formats[i].depth == 24)
991 wm->render_format = formats[i];
992
993 free(formats_reply);
994}
995
996static void
997weston_wm_create_wm_window(struct weston_wm *wm)
998{
999 static const char name[] = "Weston WM";
1000
1001 wm->wm_window = xcb_generate_id(wm->conn);
1002 xcb_create_window(wm->conn,
1003 XCB_COPY_FROM_PARENT,
1004 wm->wm_window,
1005 wm->screen->root,
1006 0, 0,
1007 10, 10,
1008 0,
1009 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1010 wm->screen->root_visual,
1011 0, NULL);
1012
1013 xcb_change_property(wm->conn,
1014 XCB_PROP_MODE_REPLACE,
1015 wm->wm_window,
1016 wm->atom.net_supporting_wm_check,
1017 XCB_ATOM_WINDOW,
1018 32, /* format */
1019 1, &wm->wm_window);
1020
1021 xcb_change_property(wm->conn,
1022 XCB_PROP_MODE_REPLACE,
1023 wm->wm_window,
1024 wm->atom.net_wm_name,
1025 wm->atom.utf8_string,
1026 8, /* format */
1027 strlen(name), name);
1028
1029 xcb_change_property(wm->conn,
1030 XCB_PROP_MODE_REPLACE,
1031 wm->screen->root,
1032 wm->atom.net_supporting_wm_check,
1033 XCB_ATOM_WINDOW,
1034 32, /* format */
1035 1, &wm->wm_window);
1036
1037}
1038
1039struct weston_wm *
1040weston_wm_create(struct weston_xserver *wxs)
1041{
1042 struct wl_seat *seat;
1043 struct weston_wm *wm;
1044 struct wl_event_loop *loop;
1045 xcb_screen_iterator_t s;
1046 uint32_t values[1], mask;
1047 int sv[2];
1048 xcb_atom_t supported[1];
1049
1050 wm = malloc(sizeof *wm);
1051 if (wm == NULL)
1052 return NULL;
1053
1054 memset(wm, 0, sizeof *wm);
1055 wm->server = wxs;
1056 wm->window_hash = hash_table_create();
1057 if (wm->window_hash == NULL) {
1058 free(wm);
1059 return NULL;
1060 }
1061
1062 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
1063 fprintf(stderr, "socketpair failed\n");
1064 hash_table_destroy(wm->window_hash);
1065 free(wm);
1066 return NULL;
1067 }
1068
1069 xserver_send_client(wxs->resource, sv[1]);
1070 wl_client_flush(wxs->resource->client);
1071 close(sv[1]);
1072
1073 /* xcb_connect_to_fd takes ownership of the fd. */
1074 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1075 if (xcb_connection_has_error(wm->conn)) {
1076 fprintf(stderr, "xcb_connect_to_fd failed\n");
1077 close(sv[0]);
1078 hash_table_destroy(wm->window_hash);
1079 free(wm);
1080 return NULL;
1081 }
1082
1083 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1084 wm->screen = s.data;
1085
1086 loop = wl_display_get_event_loop(wxs->wl_display);
1087 wm->source =
1088 wl_event_loop_add_fd(loop, sv[0],
1089 WL_EVENT_READABLE,
1090 weston_wm_handle_event, wm);
1091 wl_event_source_check(wm->source);
1092
1093 wxs_wm_get_resources(wm);
1094
1095 values[0] =
1096 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
1097 XCB_EVENT_MASK_RESIZE_REDIRECT |
1098 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1099 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1100 XCB_EVENT_MASK_PROPERTY_CHANGE;
1101 xcb_change_window_attributes(wm->conn, wm->screen->root,
1102 XCB_CW_EVENT_MASK, values);
1103 wm->theme = theme_create();
1104
1105 weston_wm_create_wm_window(wm);
1106
1107 supported[0] = wm->atom.net_wm_moveresize;
1108 xcb_change_property(wm->conn,
1109 XCB_PROP_MODE_REPLACE,
1110 wm->screen->root,
1111 wm->atom.net_supported,
1112 XCB_ATOM_ATOM,
1113 32, /* format */
1114 ARRAY_LENGTH(supported), supported);
1115
1116 wm->selection_request.requestor = XCB_NONE;
1117
1118 wm->selection_window = xcb_generate_id(wm->conn);
1119 xcb_create_window(wm->conn,
1120 XCB_COPY_FROM_PARENT,
1121 wm->selection_window,
1122 wm->screen->root,
1123 0, 0,
1124 10, 10,
1125 0,
1126 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1127 wm->screen->root_visual,
1128 XCB_CW_EVENT_MASK, values);
1129
1130 mask =
1131 XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER |
1132 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY |
1133 XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE;
1134
1135 xcb_xfixes_select_selection_input(wm->conn, wm->selection_window,
1136 wm->atom.clipboard, mask);
1137
1138 xcb_flush(wm->conn);
1139
1140 seat = &wxs->compositor->seat->seat;
1141 wm->selection_listener.notify = weston_wm_set_selection;
1142 wl_signal_add(&seat->selection_signal, &wm->selection_listener);
1143
1144 wm->activate_listener.notify = weston_wm_window_activate;
1145 wl_signal_add(&wxs->compositor->activate_signal,
1146 &wm->activate_listener);
1147
1148 fprintf(stderr, "created wm\n");
1149
1150 return wm;
1151}
1152
1153void
1154weston_wm_destroy(struct weston_wm *wm)
1155{
1156 /* FIXME: Free windows in hash. */
1157 hash_table_destroy(wm->window_hash);
1158 xcb_disconnect(wm->conn);
1159 wl_event_source_remove(wm->source);
1160 wl_list_remove(&wm->selection_listener.link);
1161 wl_list_remove(&wm->activate_listener.link);
1162
1163 free(wm);
1164}
1165
1166static void
1167surface_destroy(struct wl_listener *listener, void *data)
1168{
1169 struct weston_wm_window *window =
1170 container_of(listener,
1171 struct weston_wm_window, surface_destroy_listener);
1172
1173 fprintf(stderr, "surface for xid %d destroyed\n", window->id);
1174}
1175
1176static struct weston_wm_window *
1177get_wm_window(struct weston_surface *surface)
1178{
1179 struct wl_resource *resource = &surface->surface.resource;
1180 struct wl_listener *listener;
1181
1182 listener = wl_signal_get(&resource->destroy_signal, surface_destroy);
1183 if (listener)
1184 return container_of(listener, struct weston_wm_window,
1185 surface_destroy_listener);
1186
1187 return NULL;
1188}
1189
1190static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001191weston_wm_window_configure(void *data)
1192{
1193 struct weston_wm_window *window = data;
1194 struct weston_wm *wm = window->wm;
1195 uint32_t values[2];
1196 int width, height;
1197
1198 values[0] = window->width;
1199 values[1] = window->height;
1200 xcb_configure_window(wm->conn,
1201 window->id,
1202 XCB_CONFIG_WINDOW_WIDTH |
1203 XCB_CONFIG_WINDOW_HEIGHT,
1204 values);
1205
1206 weston_wm_window_get_frame_size(window, &width, &height);
1207 values[0] = width;
1208 values[1] = height;
1209 xcb_configure_window(wm->conn,
1210 window->frame_id,
1211 XCB_CONFIG_WINDOW_WIDTH |
1212 XCB_CONFIG_WINDOW_HEIGHT,
1213 values);
1214
1215 window->configure_source = NULL;
1216
1217 weston_wm_window_schedule_repaint(window);
1218}
1219
1220static void
1221send_configure(struct weston_surface *surface,
1222 uint32_t edges, int32_t width, int32_t height)
1223{
1224 struct weston_wm_window *window = get_wm_window(surface);
1225 struct weston_wm *wm = window->wm;
1226 struct theme *t = window->wm->theme;
1227
1228 if (window->decorate) {
1229 window->width = width - 2 * (t->margin + t->width);
1230 window->height = height - 2 * t->margin -
1231 t->titlebar_height - t->width;
1232 } else {
1233 window->width = width - 2 * t->margin;
1234 window->height = height - 2 * t->margin;
1235 }
1236
1237 if (window->configure_source)
1238 return;
1239
1240 window->configure_source =
1241 wl_event_loop_add_idle(wm->server->loop,
1242 weston_wm_window_configure, window);
1243}
1244
1245static const struct weston_shell_client shell_client = {
1246 send_configure
1247};
1248
1249static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001250xserver_map_shell_surface(struct weston_wm *wm,
1251 struct weston_wm_window *window)
1252{
1253 struct weston_shell_interface *shell_interface =
1254 &wm->server->compositor->shell_interface;
1255 struct weston_wm_window *parent;
1256 struct theme *t = window->wm->theme;
1257
1258 if (!shell_interface->create_shell_surface)
1259 return;
1260
1261 window->shsurf =
1262 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001263 window->surface,
1264 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001265
1266 if (!window->transient_for) {
1267 shell_interface->set_toplevel(window->shsurf);
1268 return;
1269 }
1270
1271 parent = hash_table_lookup(wm->window_hash, window->transient_for->id);
1272 shell_interface->set_transient(window->shsurf, parent->shsurf,
1273 window->x - parent->x + t->margin + t->width,
1274 window->y - parent->y + t->margin + t->titlebar_height,
1275 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1276}
1277
1278static void
1279xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1280 struct wl_resource *surface_resource, uint32_t id)
1281{
1282 struct weston_xserver *wxs = resource->data;
1283 struct weston_wm *wm = wxs->wm;
1284 struct wl_surface *surface = surface_resource->data;
1285 struct weston_wm_window *window;
1286
1287 if (client != wxs->client)
1288 return;
1289
1290 window = hash_table_lookup(wm->window_hash, id);
1291 if (window == NULL) {
1292 fprintf(stderr, "set_window_id for unknown window %d\n", id);
1293 return;
1294 }
1295
1296 fprintf(stderr, "set_window_id %d for surface %p\n", id, surface);
1297
1298 weston_wm_window_read_properties(window);
1299
1300 window->surface = (struct weston_surface *) surface;
1301 window->surface_destroy_listener.notify = surface_destroy;
1302 wl_signal_add(&surface->resource.destroy_signal,
1303 &window->surface_destroy_listener);
1304
1305 weston_wm_window_schedule_repaint(window);
1306 xserver_map_shell_surface(wm, window);
1307}
1308
1309const struct xserver_interface xserver_implementation = {
1310 xserver_set_window_id
1311};