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