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