blob: ff2d4e62578dac0c7ee0ced16007aa582f8244d3 [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
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040024
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>
Tiago Vignatti90fada42012-07-16 12:02:08 -040034#include <X11/Xcursor/Xcursor.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040035
36#include "xwayland.h"
37
38#include "../../shared/cairo-util.h"
39#include "../compositor.h"
40#include "xserver-server-protocol.h"
41#include "hash.h"
42
43struct motif_wm_hints {
44 uint32_t flags;
45 uint32_t functions;
46 uint32_t decorations;
47 int32_t input_mode;
48 uint32_t status;
49};
50
51#define MWM_HINTS_FUNCTIONS (1L << 0)
52#define MWM_HINTS_DECORATIONS (1L << 1)
53#define MWM_HINTS_INPUT_MODE (1L << 2)
54#define MWM_HINTS_STATUS (1L << 3)
55
56#define MWM_FUNC_ALL (1L << 0)
57#define MWM_FUNC_RESIZE (1L << 1)
58#define MWM_FUNC_MOVE (1L << 2)
59#define MWM_FUNC_MINIMIZE (1L << 3)
60#define MWM_FUNC_MAXIMIZE (1L << 4)
61#define MWM_FUNC_CLOSE (1L << 5)
62
63#define MWM_DECOR_ALL (1L << 0)
64#define MWM_DECOR_BORDER (1L << 1)
65#define MWM_DECOR_RESIZEH (1L << 2)
66#define MWM_DECOR_TITLE (1L << 3)
67#define MWM_DECOR_MENU (1L << 4)
68#define MWM_DECOR_MINIMIZE (1L << 5)
69#define MWM_DECOR_MAXIMIZE (1L << 6)
70
71#define MWM_INPUT_MODELESS 0
72#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
73#define MWM_INPUT_SYSTEM_MODAL 2
74#define MWM_INPUT_FULL_APPLICATION_MODAL 3
75#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
76
77#define MWM_TEAROFF_WINDOW (1L<<0)
78
79#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
80#define _NET_WM_MOVERESIZE_SIZE_TOP 1
81#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
82#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
83#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
84#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
85#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
86#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
87#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
88#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
89#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
90#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
91
MoD31700122013-06-11 19:58:55 -050092#define SEND_EVENT_MASK (0x80)
93#define EVENT_TYPE(event) ((event)->response_type & ~SEND_EVENT_MASK)
94
Kristian Høgsberg380deee2012-05-21 17:12:41 -040095struct weston_wm_window {
96 struct weston_wm *wm;
97 xcb_window_t id;
98 xcb_window_t frame_id;
99 cairo_surface_t *cairo_surface;
100 struct weston_surface *surface;
101 struct shell_surface *shsurf;
102 struct wl_listener surface_destroy_listener;
103 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400104 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400105 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300106 int pid;
107 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400108 char *class;
109 char *name;
110 struct weston_wm_window *transient_for;
111 uint32_t protocols;
112 xcb_atom_t type;
113 int width, height;
114 int x, y;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500115 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400116 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300117 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500118 int fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400119};
120
121static struct weston_wm_window *
122get_wm_window(struct weston_surface *surface);
123
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400124static void
125weston_wm_window_schedule_repaint(struct weston_wm_window *window);
126
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400127static int __attribute__ ((format (printf, 1, 2)))
128wm_log(const char *fmt, ...)
129{
130#ifdef WM_DEBUG
131 int l;
132 va_list argp;
133
134 va_start(argp, fmt);
135 l = weston_vlog(fmt, argp);
136 va_end(argp);
137
138 return l;
139#else
140 return 0;
141#endif
142}
143
144static int __attribute__ ((format (printf, 1, 2)))
145wm_log_continue(const char *fmt, ...)
146{
147#ifdef WM_DEBUG
148 int l;
149 va_list argp;
150
151 va_start(argp, fmt);
152 l = weston_vlog_continue(fmt, argp);
153 va_end(argp);
154
155 return l;
156#else
157 return 0;
158#endif
159}
160
161
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400162const char *
163get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
164{
165 xcb_get_atom_name_cookie_t cookie;
166 xcb_get_atom_name_reply_t *reply;
167 xcb_generic_error_t *e;
168 static char buffer[64];
169
170 if (atom == XCB_ATOM_NONE)
171 return "None";
172
173 cookie = xcb_get_atom_name (c, atom);
174 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500175
176 if(reply) {
177 snprintf(buffer, sizeof buffer, "%.*s",
178 xcb_get_atom_name_name_length (reply),
179 xcb_get_atom_name_name (reply));
180 } else {
181 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
182 }
183
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400184 free(reply);
185
186 return buffer;
187}
188
Tiago Vignatti90fada42012-07-16 12:02:08 -0400189static xcb_cursor_t
190xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
191{
192 xcb_connection_t *c = wm->conn;
193 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
194 xcb_screen_t *screen = s.data;
195 xcb_gcontext_t gc;
196 xcb_pixmap_t pix;
197 xcb_render_picture_t pic;
198 xcb_cursor_t cursor;
199 int stride = img->width * 4;
200
201 pix = xcb_generate_id(c);
202 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
203
204 pic = xcb_generate_id(c);
205 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
206
207 gc = xcb_generate_id(c);
208 xcb_create_gc(c, gc, pix, 0, 0);
209
210 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
211 img->width, img->height, 0, 0, 0, 32,
212 stride * img->height, (uint8_t *) img->pixels);
213 xcb_free_gc(c, gc);
214
215 cursor = xcb_generate_id(c);
216 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
217
218 xcb_render_free_picture(c, pic);
219 xcb_free_pixmap(c, pix);
220
221 return cursor;
222}
223
224static xcb_cursor_t
225xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
226{
227 /* TODO: treat animated cursors as well */
228 if (images->nimage != 1)
229 return -1;
230
231 return xcb_cursor_image_load_cursor(wm, images->images[0]);
232}
233
234static xcb_cursor_t
235xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
236{
237 xcb_cursor_t cursor;
238 XcursorImages *images;
239 char *v = NULL;
240 int size = 0;
241
242 if (!file)
243 return 0;
244
245 v = getenv ("XCURSOR_SIZE");
246 if (v)
247 size = atoi(v);
248
249 if (!size)
250 size = 32;
251
252 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300253 if (!images)
254 return -1;
255
Tiago Vignatti90fada42012-07-16 12:02:08 -0400256 cursor = xcb_cursor_images_load_cursor (wm, images);
257 XcursorImagesDestroy (images);
258
259 return cursor;
260}
261
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400262void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400263dump_property(struct weston_wm *wm,
264 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400265{
266 int32_t *incr_value;
267 const char *text_value, *name;
268 xcb_atom_t *atom_value;
269 int width, len;
270 uint32_t i;
271
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400272 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400273 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400274 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400275 return;
276 }
277
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400278 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
279 get_atom_name(wm->conn, reply->type),
280 reply->format,
281 xcb_get_property_value_length(reply),
282 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400283
284 if (reply->type == wm->atom.incr) {
285 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400286 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400287 } else if (reply->type == wm->atom.utf8_string ||
288 reply->type == wm->atom.string) {
289 text_value = xcb_get_property_value(reply);
290 if (reply->value_len > 40)
291 len = 40;
292 else
293 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400294 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400295 } else if (reply->type == XCB_ATOM_ATOM) {
296 atom_value = xcb_get_property_value(reply);
297 for (i = 0; i < reply->value_len; i++) {
298 name = get_atom_name(wm->conn, atom_value[i]);
299 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400300 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400301 width = 4;
302 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400303 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400304 }
305
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400306 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400307 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400308 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400309 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400310 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400311 }
312}
313
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200314static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400315read_and_dump_property(struct weston_wm *wm,
316 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400317{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400318 xcb_get_property_reply_t *reply;
319 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400320
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400321 cookie = xcb_get_property(wm->conn, 0, window,
322 property, XCB_ATOM_ANY, 0, 2048);
323 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400324
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400325 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400326
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400327 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400328}
329
330/* We reuse some predefined, but otherwise useles atoms */
331#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
332#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500333#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400334
335static void
336weston_wm_window_read_properties(struct weston_wm_window *window)
337{
338 struct weston_wm *wm = window->wm;
339
340#define F(field) offsetof(struct weston_wm_window, field)
341 const struct {
342 xcb_atom_t atom;
343 xcb_atom_t type;
344 int offset;
345 } props[] = {
346 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
347 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
348 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
349 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500350 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400351 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
352 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300353 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400354 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300355 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400356 };
357#undef F
358
359 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
360 xcb_get_property_reply_t *reply;
361 void *p;
362 uint32_t *xid;
363 xcb_atom_t *atom;
364 uint32_t i;
365 struct motif_wm_hints *hints;
366
367 if (!window->properties_dirty)
368 return;
369 window->properties_dirty = 0;
370
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400371 for (i = 0; i < ARRAY_LENGTH(props); i++)
372 cookie[i] = xcb_get_property(wm->conn,
373 0, /* delete */
374 window->id,
375 props[i].atom,
376 XCB_ATOM_ANY, 0, 2048);
377
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300378 window->decorate = !window->override_redirect;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400379 for (i = 0; i < ARRAY_LENGTH(props); i++) {
380 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
381 if (!reply)
382 /* Bad window, typically */
383 continue;
384 if (reply->type == XCB_ATOM_NONE) {
385 /* No such property */
386 free(reply);
387 continue;
388 }
389
390 p = ((char *) window + props[i].offset);
391
392 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300393 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400394 case XCB_ATOM_STRING:
395 /* FIXME: We're using this for both string and
396 utf8_string */
397 if (*(char **) p)
398 free(*(char **) p);
399
400 *(char **) p =
401 strndup(xcb_get_property_value(reply),
402 xcb_get_property_value_length(reply));
403 break;
404 case XCB_ATOM_WINDOW:
405 xid = xcb_get_property_value(reply);
406 *(struct weston_wm_window **) p =
407 hash_table_lookup(wm->window_hash, *xid);
408 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300409 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400410 case XCB_ATOM_ATOM:
411 atom = xcb_get_property_value(reply);
412 *(xcb_atom_t *) p = *atom;
413 break;
414 case TYPE_WM_PROTOCOLS:
415 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500416 case TYPE_NET_WM_STATE:
417 window->fullscreen = 0;
418 atom = xcb_get_property_value(reply);
419 for (i = 0; i < reply->value_len; i++)
420 if (atom[i] == wm->atom.net_wm_state_fullscreen)
421 window->fullscreen = 1;
422 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400423 case TYPE_MOTIF_WM_HINTS:
424 hints = xcb_get_property_value(reply);
425 if (hints->flags & MWM_HINTS_DECORATIONS)
426 window->decorate = hints->decorations > 0;
427 break;
428 default:
429 break;
430 }
431 free(reply);
432 }
433}
434
435static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400436weston_wm_window_get_frame_size(struct weston_wm_window *window,
437 int *width, int *height)
438{
439 struct theme *t = window->wm->theme;
440
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500441 if (window->fullscreen) {
442 *width = window->width;
443 *height = window->height;
444 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400445 *width = window->width + (t->margin + t->width) * 2;
446 *height = window->height +
447 t->margin * 2 + t->width + t->titlebar_height;
448 } else {
449 *width = window->width + t->margin * 2;
450 *height = window->height + t->margin * 2;
451 }
452}
453
454static void
455weston_wm_window_get_child_position(struct weston_wm_window *window,
456 int *x, int *y)
457{
458 struct theme *t = window->wm->theme;
459
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500460 if (window->fullscreen) {
461 *x = 0;
462 *y = 0;
463 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400464 *x = t->margin + t->width;
465 *y = t->margin + t->titlebar_height;
466 } else {
467 *x = t->margin;
468 *y = t->margin;
469 }
470}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400471
472static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500473weston_wm_window_send_configure_notify(struct weston_wm_window *window)
474{
475 xcb_configure_notify_event_t configure_notify;
476 struct weston_wm *wm = window->wm;
477 int x, y;
478
479 weston_wm_window_get_child_position(window, &x, &y);
480 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
481 configure_notify.pad0 = 0;
482 configure_notify.event = window->id;
483 configure_notify.window = window->id;
484 configure_notify.above_sibling = XCB_WINDOW_NONE;
485 configure_notify.x = x;
486 configure_notify.y = y;
487 configure_notify.width = window->width;
488 configure_notify.height = window->height;
489 configure_notify.border_width = 0;
490 configure_notify.override_redirect = 0;
491 configure_notify.pad1 = 0;
492
493 xcb_send_event(wm->conn, 0, window->id,
494 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
495 (char *) &configure_notify);
496}
497
498static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400499weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
500{
501 xcb_configure_request_event_t *configure_request =
502 (xcb_configure_request_event_t *) event;
503 struct weston_wm_window *window;
504 uint32_t mask, values[16];
505 int x, y, width, height, i = 0;
506
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400507 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
508 configure_request->window,
509 configure_request->x, configure_request->y,
510 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400511
512 window = hash_table_lookup(wm->window_hash, configure_request->window);
513
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500514 if (window->fullscreen) {
515 weston_wm_window_send_configure_notify(window);
516 return;
517 }
518
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400519 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
520 window->width = configure_request->width;
521 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
522 window->height = configure_request->height;
523
524 weston_wm_window_get_child_position(window, &x, &y);
525 values[i++] = x;
526 values[i++] = y;
527 values[i++] = window->width;
528 values[i++] = window->height;
529 values[i++] = 0;
530 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
531 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
532 XCB_CONFIG_WINDOW_BORDER_WIDTH;
533 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
534 values[i++] = configure_request->sibling;
535 mask |= XCB_CONFIG_WINDOW_SIBLING;
536 }
537 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
538 values[i++] = configure_request->stack_mode;
539 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
540 }
541
542 xcb_configure_window(wm->conn, window->id, mask, values);
543
544 weston_wm_window_get_frame_size(window, &width, &height);
545 values[0] = width;
546 values[1] = height;
547 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
548 xcb_configure_window(wm->conn, window->frame_id, mask, values);
549
550 weston_wm_window_schedule_repaint(window);
551}
552
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400553static void
554weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
555{
556 xcb_configure_notify_event_t *configure_notify =
557 (xcb_configure_notify_event_t *) event;
558 struct weston_wm_window *window;
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300559 int x, y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400560
561 window = hash_table_lookup(wm->window_hash, configure_notify->window);
562
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400563 wm_log("XCB_CONFIGURE_NOTIFY (%s window %d) %d,%d @ %dx%d\n",
564 configure_notify->window == window->id ? "client" : "frame",
565 configure_notify->window,
566 configure_notify->x, configure_notify->y,
567 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300568
569 /* resize falls here */
570 if (configure_notify->window != window->id)
571 return;
572
573 weston_wm_window_get_child_position(window, &x, &y);
574 window->x = configure_notify->x - x;
575 window->y = configure_notify->y - y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400576}
577
578static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300579weston_wm_kill_client(struct wl_listener *listener, void *data)
580{
581 struct weston_surface *surface = data;
582 struct weston_wm_window *window = get_wm_window(surface);
583 char name[1024];
584
585 if (!window)
586 return;
587
588 gethostname(name, 1024);
589
590 /* this is only one heuristic to guess the PID of a client is valid,
591 * assuming it's compliant with icccm and ewmh. Non-compliants and
592 * remote applications of course fail. */
593 if (!strcmp(window->machine, name) && window->pid != 0)
594 kill(window->pid, SIGKILL);
595}
596
597static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400598weston_wm_window_activate(struct wl_listener *listener, void *data)
599{
600 struct weston_surface *surface = data;
601 struct weston_wm_window *window = get_wm_window(surface);
Scott Moreau85ecac02012-05-21 15:49:14 -0600602 struct weston_wm *wm =
603 container_of(listener, struct weston_wm, activate_listener);
604 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400605
Scott Moreau85ecac02012-05-21 15:49:14 -0600606 if (window) {
607 client_message.response_type = XCB_CLIENT_MESSAGE;
608 client_message.format = 32;
609 client_message.window = window->id;
610 client_message.type = wm->atom.wm_protocols;
611 client_message.data.data32[0] = wm->atom.wm_take_focus;
612 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
613
614 xcb_send_event(wm->conn, 0, window->id,
615 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
616 (char *) &client_message);
617
618 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
619 window->id, XCB_TIME_CURRENT_TIME);
620 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400621 xcb_set_input_focus (wm->conn,
622 XCB_INPUT_FOCUS_POINTER_ROOT,
623 XCB_NONE,
624 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600625 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400626
627 if (wm->focus_window)
628 weston_wm_window_schedule_repaint(wm->focus_window);
629 wm->focus_window = window;
630 if (wm->focus_window)
631 weston_wm_window_schedule_repaint(wm->focus_window);
632}
633
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300634static void
635weston_wm_window_transform(struct wl_listener *listener, void *data)
636{
637 struct weston_surface *surface = data;
638 struct weston_wm_window *window = get_wm_window(surface);
639 struct weston_wm *wm =
640 container_of(listener, struct weston_wm, transform_listener);
641 struct weston_output *output = surface->output;
642 uint32_t mask, values[2];
643 float sxf, syf;
644 int sx, sy;
645 static int old_sx = -1, old_sy = -1;
646
647 if (!window || !wm)
648 return;
649
650 if (!weston_surface_is_mapped(surface))
651 return;
652
653 weston_surface_to_global_float(surface, output->x, output->y,
654 &sxf, &syf);
655
656 sx = (int) sxf;
657 sy = (int) syf;
658
659 if (old_sx == sx && old_sy == sy)
660 return;
661
662 values[0] = sx;
663 values[1] = sy;
664 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
665
666 xcb_configure_window(wm->conn, window->frame_id, mask, values);
667 xcb_flush(wm->conn);
668
669 old_sx = sx;
670 old_sy = sy;
671}
672
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400673static int
674our_resource(struct weston_wm *wm, uint32_t id)
675{
676 const xcb_setup_t *setup;
677
678 setup = xcb_get_setup(wm->conn);
679
680 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
681}
682
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400683#define ICCCM_WITHDRAWN_STATE 0
684#define ICCCM_NORMAL_STATE 1
685#define ICCCM_ICONIC_STATE 3
686
687static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500688weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400689{
690 struct weston_wm *wm = window->wm;
691 uint32_t property[2];
692
693 property[0] = state;
694 property[1] = XCB_WINDOW_NONE;
695
696 xcb_change_property(wm->conn,
697 XCB_PROP_MODE_REPLACE,
698 window->id,
699 wm->atom.wm_state,
700 wm->atom.wm_state,
701 32, /* format */
702 2, property);
703}
704
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400705static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500706weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
707{
708 struct weston_wm *wm = window->wm;
709 uint32_t property[1];
710 int i;
711
712 i = 0;
713 if (window->fullscreen)
714 property[i++] = wm->atom.net_wm_state_fullscreen;
715
716 xcb_change_property(wm->conn,
717 XCB_PROP_MODE_REPLACE,
718 window->id,
719 wm->atom.net_wm_state,
720 XCB_ATOM_ATOM,
721 32, /* format */
722 i, property);
723}
724
725static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400726weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
727{
728 xcb_map_request_event_t *map_request =
729 (xcb_map_request_event_t *) event;
730 struct weston_wm_window *window;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400731 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400732 int x, y, width, height;
733
734 if (our_resource(wm, map_request->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400735 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
736 map_request->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400737 return;
738 }
739
740 window = hash_table_lookup(wm->window_hash, map_request->window);
741
Kristian Høgsbergbc6e1622012-05-30 09:59:56 -0400742 if (window->frame_id)
743 return;
744
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400745 weston_wm_window_read_properties(window);
746
747 weston_wm_window_get_frame_size(window, &width, &height);
748 weston_wm_window_get_child_position(window, &x, &y);
749
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400750 values[0] = wm->screen->black_pixel;
751 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400752 XCB_EVENT_MASK_KEY_PRESS |
753 XCB_EVENT_MASK_KEY_RELEASE |
754 XCB_EVENT_MASK_BUTTON_PRESS |
755 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400756 XCB_EVENT_MASK_POINTER_MOTION |
757 XCB_EVENT_MASK_ENTER_WINDOW |
758 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400759 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400760 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400761 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400762
763 window->frame_id = xcb_generate_id(wm->conn);
764 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400765 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400766 window->frame_id,
767 wm->screen->root,
768 0, 0,
769 width, height,
770 0,
771 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400772 wm->visual_id,
773 XCB_CW_BORDER_PIXEL |
774 XCB_CW_EVENT_MASK |
775 XCB_CW_COLORMAP, values);
776
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400777 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
778
779 values[0] = 0;
780 xcb_configure_window(wm->conn, window->id,
781 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
782
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400783 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
784 window->id, window, window->frame_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400785
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500786 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
787 weston_wm_window_set_net_wm_state(window);
788
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400789 xcb_map_window(wm->conn, map_request->window);
790 xcb_map_window(wm->conn, window->frame_id);
791
792 window->cairo_surface =
793 cairo_xcb_surface_create_with_xrender_format(wm->conn,
794 wm->screen,
795 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400796 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400797 width, height);
798
799 hash_table_insert(wm->window_hash, window->frame_id, window);
800}
801
802static void
803weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
804{
805 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
806
807 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400808 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
809 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400810 return;
811 }
812
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400813 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400814}
815
816static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400817weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
818{
819 xcb_unmap_notify_event_t *unmap_notify =
820 (xcb_unmap_notify_event_t *) event;
821 struct weston_wm_window *window;
822
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400823 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
824 unmap_notify->window,
825 unmap_notify->event,
826 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400827
828 if (our_resource(wm, unmap_notify->window))
829 return;
830
MoD31700122013-06-11 19:58:55 -0500831 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400832 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
833 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400834 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400835
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400836 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400837 if (window->repaint_source)
838 wl_event_source_remove(window->repaint_source);
839 if (window->cairo_surface)
840 cairo_surface_destroy(window->cairo_surface);
841
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400842 if (window->frame_id) {
843 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
844 xcb_destroy_window(wm->conn, window->frame_id);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500845 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Kristian Høgsbergbe375b32012-06-05 11:46:08 -0400846 hash_table_remove(wm->window_hash, window->frame_id);
847 window->frame_id = XCB_WINDOW_NONE;
848 }
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400849
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400850 if (wm->focus_window == window)
851 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400852 if (window->surface)
853 wl_list_remove(&window->surface_destroy_listener.link);
854 window->surface = NULL;
855}
856
857static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400858weston_wm_window_draw_decoration(void *data)
859{
860 struct weston_wm_window *window = data;
861 struct weston_wm *wm = window->wm;
862 struct theme *t = wm->theme;
863 cairo_t *cr;
864 int x, y, width, height;
865 const char *title;
866 uint32_t flags = 0;
867
868 weston_wm_window_read_properties(window);
869
870 window->repaint_source = NULL;
871
872 weston_wm_window_get_frame_size(window, &width, &height);
873 weston_wm_window_get_child_position(window, &x, &y);
874
875 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
876 cr = cairo_create(window->cairo_surface);
877
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500878 if (window->fullscreen) {
879 /* nothing */
880 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400881 if (wm->focus_window == window)
882 flags |= THEME_FRAME_ACTIVE;
883
884 if (window->name)
885 title = window->name;
886 else
887 title = "untitled";
888
889 theme_render_frame(t, cr, width, height, title, flags);
890 } else {
891 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
892 cairo_set_source_rgba(cr, 0, 0, 0, 0);
893 cairo_paint(cr);
894
895 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
896 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
897 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
898 }
899
900 cairo_destroy(cr);
901
902 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700903 pixman_region32_fini(&window->surface->pending.opaque);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400904 /* We leave an extra pixel around the X window area to
905 * make sure we don't sample from the undefined alpha
906 * channel when filtering. */
Kristian Høgsberg25bb6962013-02-14 22:01:04 -0500907 pixman_region32_init_rect(&window->surface->pending.opaque,
908 x - 1, y - 1,
909 window->width + 2,
910 window->height + 2);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200911 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500912 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400913
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500914 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500915 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500916 pixman_region32_init_rect(&window->surface->pending.input,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400917 t->margin, t->margin,
918 width - 2 * t->margin,
919 height - 2 * t->margin);
920 }
921}
922
923static void
924weston_wm_window_schedule_repaint(struct weston_wm_window *window)
925{
926 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300927 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400928
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400929 if (window->frame_id == XCB_WINDOW_NONE) {
930 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300931 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700932 pixman_region32_fini(&window->surface->pending.opaque);
933 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300934 width, height);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200935 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400936 }
937 return;
938 }
939
940 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400941 return;
942
943 window->repaint_source =
944 wl_event_loop_add_idle(wm->server->loop,
945 weston_wm_window_draw_decoration,
946 window);
947}
948
949static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400950weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
951{
952 xcb_property_notify_event_t *property_notify =
953 (xcb_property_notify_event_t *) event;
954 struct weston_wm_window *window;
955
956 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +0000957 if (!window)
958 return;
959
960 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400961
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400962 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400963 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400964 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400965 else
966 read_and_dump_property(wm, property_notify->window,
967 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400968
969 if (property_notify->atom == wm->atom.net_wm_name ||
970 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400971 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400972}
973
974static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400975weston_wm_window_create(struct weston_wm *wm,
Tiago Vignatti771241e2012-06-04 20:01:45 +0300976 xcb_window_t id, int width, int height, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400977{
978 struct weston_wm_window *window;
979 uint32_t values[1];
980
981 window = malloc(sizeof *window);
982 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400983 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400984 return;
985 }
986
987 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
988 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
989
990 memset(window, 0, sizeof *window);
991 window->wm = wm;
992 window->id = id;
993 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300994 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -0400995 window->width = width;
996 window->height = height;
997
998 hash_table_insert(wm->window_hash, id, window);
999}
1000
1001static void
1002weston_wm_window_destroy(struct weston_wm_window *window)
1003{
1004 hash_table_remove(window->wm->window_hash, window->id);
1005 free(window);
1006}
1007
1008static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001009weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1010{
1011 xcb_create_notify_event_t *create_notify =
1012 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001013
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001014 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1015 create_notify->window,
1016 create_notify->width, create_notify->height,
1017 create_notify->override_redirect ? ", override" : "",
1018 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001019
1020 if (our_resource(wm, create_notify->window))
1021 return;
1022
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001023 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001024 create_notify->width, create_notify->height,
1025 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001026}
1027
1028static void
1029weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1030{
1031 xcb_destroy_notify_event_t *destroy_notify =
1032 (xcb_destroy_notify_event_t *) event;
1033 struct weston_wm_window *window;
1034
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001035 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1036 destroy_notify->window,
1037 destroy_notify->event,
1038 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001039
1040 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001041 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001042
1043 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001044 weston_wm_window_destroy(window);
1045}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001046
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001047static void
1048weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1049{
1050 xcb_reparent_notify_event_t *reparent_notify =
1051 (xcb_reparent_notify_event_t *) event;
1052 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001053
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001054 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1055 reparent_notify->window,
1056 reparent_notify->parent,
1057 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001058
1059 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001060 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
1061 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001062 } else if (!our_resource(wm, reparent_notify->parent)) {
1063 window = hash_table_lookup(wm->window_hash,
1064 reparent_notify->window);
1065 weston_wm_window_destroy(window);
1066 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001067}
1068
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001069struct weston_seat *
1070weston_wm_pick_seat(struct weston_wm *wm)
1071{
1072 return container_of(wm->server->compositor->seat_list.next,
1073 struct weston_seat, link);
1074}
1075
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001076static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001077weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1078 xcb_client_message_event_t *client_message)
1079{
1080 static const int map[] = {
1081 THEME_LOCATION_RESIZING_TOP_LEFT,
1082 THEME_LOCATION_RESIZING_TOP,
1083 THEME_LOCATION_RESIZING_TOP_RIGHT,
1084 THEME_LOCATION_RESIZING_RIGHT,
1085 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1086 THEME_LOCATION_RESIZING_BOTTOM,
1087 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1088 THEME_LOCATION_RESIZING_LEFT
1089 };
1090
1091 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001092 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001093 int detail;
1094 struct weston_shell_interface *shell_interface =
1095 &wm->server->compositor->shell_interface;
1096
Kristian Høgsberge3148752013-05-06 23:19:49 -04001097 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001098 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001099 return;
1100
1101 detail = client_message->data.data32[2];
1102 switch (detail) {
1103 case _NET_WM_MOVERESIZE_MOVE:
1104 shell_interface->move(window->shsurf, seat);
1105 break;
1106 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1107 case _NET_WM_MOVERESIZE_SIZE_TOP:
1108 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1109 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1110 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1111 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1112 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1113 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1114 shell_interface->resize(window->shsurf, seat, map[detail]);
1115 break;
1116 case _NET_WM_MOVERESIZE_CANCEL:
1117 break;
1118 }
1119}
1120
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001121#define _NET_WM_STATE_REMOVE 0
1122#define _NET_WM_STATE_ADD 1
1123#define _NET_WM_STATE_TOGGLE 2
1124
1125static int
1126update_state(int action, int *state)
1127{
1128 int new_state, changed;
1129
1130 switch (action) {
1131 case _NET_WM_STATE_REMOVE:
1132 new_state = 0;
1133 break;
1134 case _NET_WM_STATE_ADD:
1135 new_state = 1;
1136 break;
1137 case _NET_WM_STATE_TOGGLE:
1138 new_state = !*state;
1139 break;
1140 default:
1141 return 0;
1142 }
1143
1144 changed = (*state != new_state);
1145 *state = new_state;
1146
1147 return changed;
1148}
1149
1150static void
1151weston_wm_window_configure(void *data);
1152
1153static void
1154weston_wm_window_handle_state(struct weston_wm_window *window,
1155 xcb_client_message_event_t *client_message)
1156{
1157 struct weston_wm *wm = window->wm;
1158 struct weston_shell_interface *shell_interface =
1159 &wm->server->compositor->shell_interface;
1160 uint32_t action, property;
1161
1162 action = client_message->data.data32[0];
1163 property = client_message->data.data32[1];
1164
1165 if (property == wm->atom.net_wm_state_fullscreen &&
1166 update_state(action, &window->fullscreen)) {
1167 weston_wm_window_set_net_wm_state(window);
1168 if (window->fullscreen) {
1169 window->saved_width = window->width;
1170 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001171
1172 if (window->shsurf)
1173 shell_interface->set_fullscreen(window->shsurf,
1174 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1175 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001176 } else {
1177 shell_interface->set_toplevel(window->shsurf);
1178 window->width = window->saved_width;
1179 window->height = window->saved_height;
1180 weston_wm_window_configure(window);
1181 }
1182 }
1183}
1184
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001185static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001186weston_wm_handle_client_message(struct weston_wm *wm,
1187 xcb_generic_event_t *event)
1188{
1189 xcb_client_message_event_t *client_message =
1190 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001191 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001192
1193 window = hash_table_lookup(wm->window_hash, client_message->window);
1194
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001195 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1196 get_atom_name(wm->conn, client_message->type),
1197 client_message->data.data32[0],
1198 client_message->data.data32[1],
1199 client_message->data.data32[2],
1200 client_message->data.data32[3],
1201 client_message->data.data32[4],
1202 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001203
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001204 if (client_message->type == wm->atom.net_wm_moveresize)
1205 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001206 else if (client_message->type == wm->atom.net_wm_state)
1207 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001208}
1209
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001210enum cursor_type {
1211 XWM_CURSOR_TOP,
1212 XWM_CURSOR_BOTTOM,
1213 XWM_CURSOR_LEFT,
1214 XWM_CURSOR_RIGHT,
1215 XWM_CURSOR_TOP_LEFT,
1216 XWM_CURSOR_TOP_RIGHT,
1217 XWM_CURSOR_BOTTOM_LEFT,
1218 XWM_CURSOR_BOTTOM_RIGHT,
1219 XWM_CURSOR_LEFT_PTR,
1220};
1221
1222static const char *cursors[] = {
1223 "top_side",
1224 "bottom_side",
1225 "left_side",
1226 "right_side",
1227 "top_left_corner",
1228 "top_right_corner",
1229 "bottom_left_corner",
1230 "bottom_right_corner",
1231 "left_ptr"
1232};
1233
1234static void
1235weston_wm_create_cursors(struct weston_wm *wm)
1236{
1237 int i, count = ARRAY_LENGTH(cursors);
1238
1239 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1240 for (i = 0; i < count; i++) {
1241 wm->cursors[i] =
1242 xcb_cursor_library_load_cursor(wm, cursors[i]);
1243 }
1244
1245 wm->last_cursor = -1;
1246}
1247
1248static void
1249weston_wm_destroy_cursors(struct weston_wm *wm)
1250{
1251 uint8_t i;
1252
1253 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1254 xcb_free_cursor(wm->conn, wm->cursors[i]);
1255
1256 free(wm->cursors);
1257}
1258
1259static int
1260get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1261{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001262 int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001263
1264 switch (location) {
1265 case THEME_LOCATION_RESIZING_TOP:
1266 return XWM_CURSOR_TOP;
1267 case THEME_LOCATION_RESIZING_BOTTOM:
1268 return XWM_CURSOR_BOTTOM;
1269 case THEME_LOCATION_RESIZING_LEFT:
1270 return XWM_CURSOR_LEFT;
1271 case THEME_LOCATION_RESIZING_RIGHT:
1272 return XWM_CURSOR_RIGHT;
1273 case THEME_LOCATION_RESIZING_TOP_LEFT:
1274 return XWM_CURSOR_TOP_LEFT;
1275 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1276 return XWM_CURSOR_TOP_RIGHT;
1277 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1278 return XWM_CURSOR_BOTTOM_LEFT;
1279 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1280 return XWM_CURSOR_BOTTOM_RIGHT;
1281 case THEME_LOCATION_EXTERIOR:
1282 case THEME_LOCATION_TITLEBAR:
1283 default:
1284 return XWM_CURSOR_LEFT_PTR;
1285 }
1286}
1287
1288static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001289weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1290 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001291{
1292 uint32_t cursor_value_list;
1293
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001294 if (wm->last_cursor == cursor)
1295 return;
1296
1297 wm->last_cursor = cursor;
1298
1299 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001300 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001301 XCB_CW_CURSOR, &cursor_value_list);
1302 xcb_flush(wm->conn);
1303}
1304
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001305static void
1306weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1307{
1308 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1309 struct weston_shell_interface *shell_interface =
1310 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001311 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001312 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001313 enum theme_location location;
1314 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001315 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001316
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001317 wm_log("XCB_BUTTON_%s (detail %d)\n",
1318 button->response_type == XCB_BUTTON_PRESS ?
1319 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001320
1321 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001322 weston_wm_window_get_frame_size(window, &width, &height);
1323
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001324 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001325 button->detail == 1) {
1326 location = theme_get_location(t,
1327 button->event_x,
1328 button->event_y,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001329 width, height, 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001330
1331 switch (location) {
1332 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001333 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001334 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001335 case THEME_LOCATION_RESIZING_TOP:
1336 case THEME_LOCATION_RESIZING_BOTTOM:
1337 case THEME_LOCATION_RESIZING_LEFT:
1338 case THEME_LOCATION_RESIZING_RIGHT:
1339 case THEME_LOCATION_RESIZING_TOP_LEFT:
1340 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1341 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1342 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1343 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001344 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001345 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001346 default:
1347 break;
1348 }
1349 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001350}
1351
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001352static void
1353weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1354{
1355 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1356 struct weston_wm_window *window;
1357 int cursor, width, height;
1358
1359 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001360 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001361 return;
1362
1363 weston_wm_window_get_frame_size(window, &width, &height);
1364 cursor = get_cursor_for_location(wm->theme, width, height,
1365 motion->event_x, motion->event_y);
1366
Tiago Vignattic1903232012-07-16 12:15:37 -04001367 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001368}
1369
1370static void
1371weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1372{
1373 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1374 struct weston_wm_window *window;
1375 int cursor, width, height;
1376
1377 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001378 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001379 return;
1380
1381 weston_wm_window_get_frame_size(window, &width, &height);
1382 cursor = get_cursor_for_location(wm->theme, width, height,
1383 enter->event_x, enter->event_y);
1384
Tiago Vignattic1903232012-07-16 12:15:37 -04001385 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001386}
1387
1388static void
1389weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1390{
1391 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1392 struct weston_wm_window *window;
1393
1394 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001395 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001396 return;
1397
Tiago Vignattic1903232012-07-16 12:15:37 -04001398 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001399}
1400
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001401static int
1402weston_wm_handle_event(int fd, uint32_t mask, void *data)
1403{
1404 struct weston_wm *wm = data;
1405 xcb_generic_event_t *event;
1406 int count = 0;
1407
1408 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1409 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001410 free(event);
1411 count++;
1412 continue;
1413 }
1414
MoD31700122013-06-11 19:58:55 -05001415 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001416 case XCB_BUTTON_PRESS:
1417 case XCB_BUTTON_RELEASE:
1418 weston_wm_handle_button(wm, event);
1419 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001420 case XCB_ENTER_NOTIFY:
1421 weston_wm_handle_enter(wm, event);
1422 break;
1423 case XCB_LEAVE_NOTIFY:
1424 weston_wm_handle_leave(wm, event);
1425 break;
1426 case XCB_MOTION_NOTIFY:
1427 weston_wm_handle_motion(wm, event);
1428 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001429 case XCB_CREATE_NOTIFY:
1430 weston_wm_handle_create_notify(wm, event);
1431 break;
1432 case XCB_MAP_REQUEST:
1433 weston_wm_handle_map_request(wm, event);
1434 break;
1435 case XCB_MAP_NOTIFY:
1436 weston_wm_handle_map_notify(wm, event);
1437 break;
1438 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001439 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001440 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001441 case XCB_REPARENT_NOTIFY:
1442 weston_wm_handle_reparent_notify(wm, event);
1443 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001444 case XCB_CONFIGURE_REQUEST:
1445 weston_wm_handle_configure_request(wm, event);
1446 break;
1447 case XCB_CONFIGURE_NOTIFY:
1448 weston_wm_handle_configure_notify(wm, event);
1449 break;
1450 case XCB_DESTROY_NOTIFY:
1451 weston_wm_handle_destroy_notify(wm, event);
1452 break;
1453 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001454 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001455 break;
1456 case XCB_PROPERTY_NOTIFY:
1457 weston_wm_handle_property_notify(wm, event);
1458 break;
1459 case XCB_CLIENT_MESSAGE:
1460 weston_wm_handle_client_message(wm, event);
1461 break;
1462 }
1463
1464 free(event);
1465 count++;
1466 }
1467
1468 xcb_flush(wm->conn);
1469
1470 return count;
1471}
1472
1473static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001474weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1475{
1476 xcb_depth_iterator_t d_iter;
1477 xcb_visualtype_iterator_t vt_iter;
1478 xcb_visualtype_t *visualtype;
1479
1480 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1481 visualtype = NULL;
1482 while (d_iter.rem > 0) {
1483 if (d_iter.data->depth == 32) {
1484 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1485 visualtype = vt_iter.data;
1486 break;
1487 }
1488
1489 xcb_depth_next(&d_iter);
1490 }
1491
1492 if (visualtype == NULL) {
1493 weston_log("no 32 bit visualtype\n");
1494 return;
1495 }
1496
1497 wm->visual_id = visualtype->visual_id;
1498 wm->colormap = xcb_generate_id(wm->conn);
1499 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1500 wm->colormap, wm->screen->root, wm->visual_id);
1501}
1502
1503static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001504weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001505{
1506
1507#define F(field) offsetof(struct weston_wm, field)
1508
1509 static const struct { const char *name; int offset; } atoms[] = {
1510 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1511 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1512 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001513 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001514 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001515 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001516 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001517 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001518 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1519 { "_NET_WM_STATE", F(atom.net_wm_state) },
1520 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1521 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1522 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1523 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1524
1525 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1526 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1527 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1528 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1529 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1530 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1531 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001532 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1533 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001534 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1535 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1536 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1537 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1538 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1539
1540 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1541 { "_NET_SUPPORTING_WM_CHECK",
1542 F(atom.net_supporting_wm_check) },
1543 { "_NET_SUPPORTED", F(atom.net_supported) },
1544 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1545 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001546 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001547 { "TARGETS", F(atom.targets) },
1548 { "UTF8_STRING", F(atom.utf8_string) },
1549 { "_WL_SELECTION", F(atom.wl_selection) },
1550 { "INCR", F(atom.incr) },
1551 { "TIMESTAMP", F(atom.timestamp) },
1552 { "MULTIPLE", F(atom.multiple) },
1553 { "UTF8_STRING" , F(atom.utf8_string) },
1554 { "COMPOUND_TEXT", F(atom.compound_text) },
1555 { "TEXT", F(atom.text) },
1556 { "STRING", F(atom.string) },
1557 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1558 { "text/plain", F(atom.text_plain) },
1559 };
1560#undef F
1561
1562 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1563 xcb_xfixes_query_version_reply_t *xfixes_reply;
1564 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1565 xcb_intern_atom_reply_t *reply;
1566 xcb_render_query_pict_formats_reply_t *formats_reply;
1567 xcb_render_query_pict_formats_cookie_t formats_cookie;
1568 xcb_render_pictforminfo_t *formats;
1569 uint32_t i;
1570
1571 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1572
1573 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1574
1575 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1576 cookies[i] = xcb_intern_atom (wm->conn, 0,
1577 strlen(atoms[i].name),
1578 atoms[i].name);
1579
1580 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1581 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1582 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1583 free(reply);
1584 }
1585
1586 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1587 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001588 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001589
1590 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1591 XCB_XFIXES_MAJOR_VERSION,
1592 XCB_XFIXES_MINOR_VERSION);
1593 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1594 xfixes_cookie, NULL);
1595
Martin Minarik6d118362012-06-07 18:01:59 +02001596 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001597 xfixes_reply->major_version, xfixes_reply->minor_version);
1598
1599 free(xfixes_reply);
1600
1601 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1602 formats_cookie, 0);
1603 if (formats_reply == NULL)
1604 return;
1605
1606 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001607 for (i = 0; i < formats_reply->num_formats; i++) {
1608 if (formats[i].direct.red_mask != 0xff &&
1609 formats[i].direct.red_shift != 16)
1610 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001611 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1612 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001613 wm->format_rgb = formats[i];
1614 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1615 formats[i].depth == 32 &&
1616 formats[i].direct.alpha_mask == 0xff &&
1617 formats[i].direct.alpha_shift == 24)
1618 wm->format_rgba = formats[i];
1619 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001620
1621 free(formats_reply);
1622}
1623
1624static void
1625weston_wm_create_wm_window(struct weston_wm *wm)
1626{
1627 static const char name[] = "Weston WM";
1628
1629 wm->wm_window = xcb_generate_id(wm->conn);
1630 xcb_create_window(wm->conn,
1631 XCB_COPY_FROM_PARENT,
1632 wm->wm_window,
1633 wm->screen->root,
1634 0, 0,
1635 10, 10,
1636 0,
1637 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1638 wm->screen->root_visual,
1639 0, NULL);
1640
1641 xcb_change_property(wm->conn,
1642 XCB_PROP_MODE_REPLACE,
1643 wm->wm_window,
1644 wm->atom.net_supporting_wm_check,
1645 XCB_ATOM_WINDOW,
1646 32, /* format */
1647 1, &wm->wm_window);
1648
1649 xcb_change_property(wm->conn,
1650 XCB_PROP_MODE_REPLACE,
1651 wm->wm_window,
1652 wm->atom.net_wm_name,
1653 wm->atom.utf8_string,
1654 8, /* format */
1655 strlen(name), name);
1656
1657 xcb_change_property(wm->conn,
1658 XCB_PROP_MODE_REPLACE,
1659 wm->screen->root,
1660 wm->atom.net_supporting_wm_check,
1661 XCB_ATOM_WINDOW,
1662 32, /* format */
1663 1, &wm->wm_window);
1664
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001665 /* Claim the WM_S0 selection even though we don't suport
1666 * the --replace functionality. */
1667 xcb_set_selection_owner(wm->conn,
1668 wm->wm_window,
1669 wm->atom.wm_s0,
1670 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001671}
1672
1673struct weston_wm *
1674weston_wm_create(struct weston_xserver *wxs)
1675{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001676 struct weston_wm *wm;
1677 struct wl_event_loop *loop;
1678 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001679 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001680 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001681 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001682
1683 wm = malloc(sizeof *wm);
1684 if (wm == NULL)
1685 return NULL;
1686
1687 memset(wm, 0, sizeof *wm);
1688 wm->server = wxs;
1689 wm->window_hash = hash_table_create();
1690 if (wm->window_hash == NULL) {
1691 free(wm);
1692 return NULL;
1693 }
1694
1695 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001696 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001697 hash_table_destroy(wm->window_hash);
1698 free(wm);
1699 return NULL;
1700 }
1701
1702 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001703 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001704 close(sv[1]);
1705
1706 /* xcb_connect_to_fd takes ownership of the fd. */
1707 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1708 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001709 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001710 close(sv[0]);
1711 hash_table_destroy(wm->window_hash);
1712 free(wm);
1713 return NULL;
1714 }
1715
1716 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1717 wm->screen = s.data;
1718
1719 loop = wl_display_get_event_loop(wxs->wl_display);
1720 wm->source =
1721 wl_event_loop_add_fd(loop, sv[0],
1722 WL_EVENT_READABLE,
1723 weston_wm_handle_event, wm);
1724 wl_event_source_check(wm->source);
1725
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001726 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001727 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001728
1729 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001730 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1731 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1732 XCB_EVENT_MASK_PROPERTY_CHANGE;
1733 xcb_change_window_attributes(wm->conn, wm->screen->root,
1734 XCB_CW_EVENT_MASK, values);
1735 wm->theme = theme_create();
1736
1737 weston_wm_create_wm_window(wm);
1738
1739 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001740 supported[1] = wm->atom.net_wm_state;
1741 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001742 xcb_change_property(wm->conn,
1743 XCB_PROP_MODE_REPLACE,
1744 wm->screen->root,
1745 wm->atom.net_supported,
1746 XCB_ATOM_ATOM,
1747 32, /* format */
1748 ARRAY_LENGTH(supported), supported);
1749
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001750 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001751
1752 xcb_flush(wm->conn);
1753
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001754 wm->activate_listener.notify = weston_wm_window_activate;
1755 wl_signal_add(&wxs->compositor->activate_signal,
1756 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001757 wm->transform_listener.notify = weston_wm_window_transform;
1758 wl_signal_add(&wxs->compositor->transform_signal,
1759 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001760 wm->kill_listener.notify = weston_wm_kill_client;
1761 wl_signal_add(&wxs->compositor->kill_signal,
1762 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001763
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001764 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001765 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001766
Martin Minarik6d118362012-06-07 18:01:59 +02001767 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001768
1769 return wm;
1770}
1771
1772void
1773weston_wm_destroy(struct weston_wm *wm)
1774{
1775 /* FIXME: Free windows in hash. */
1776 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001777 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001778 xcb_disconnect(wm->conn);
1779 wl_event_source_remove(wm->source);
1780 wl_list_remove(&wm->selection_listener.link);
1781 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001782 wl_list_remove(&wm->kill_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001783
1784 free(wm);
1785}
1786
1787static void
1788surface_destroy(struct wl_listener *listener, void *data)
1789{
1790 struct weston_wm_window *window =
1791 container_of(listener,
1792 struct weston_wm_window, surface_destroy_listener);
1793
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001794 wm_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001795}
1796
1797static struct weston_wm_window *
1798get_wm_window(struct weston_surface *surface)
1799{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001800 struct wl_listener *listener;
1801
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001802 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001803 if (listener)
1804 return container_of(listener, struct weston_wm_window,
1805 surface_destroy_listener);
1806
1807 return NULL;
1808}
1809
1810static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001811weston_wm_window_configure(void *data)
1812{
1813 struct weston_wm_window *window = data;
1814 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001815 uint32_t values[4];
1816 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001817
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001818 weston_wm_window_get_child_position(window, &x, &y);
1819 values[0] = x;
1820 values[1] = y;
1821 values[2] = window->width;
1822 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001823 xcb_configure_window(wm->conn,
1824 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001825 XCB_CONFIG_WINDOW_X |
1826 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001827 XCB_CONFIG_WINDOW_WIDTH |
1828 XCB_CONFIG_WINDOW_HEIGHT,
1829 values);
1830
1831 weston_wm_window_get_frame_size(window, &width, &height);
1832 values[0] = width;
1833 values[1] = height;
1834 xcb_configure_window(wm->conn,
1835 window->frame_id,
1836 XCB_CONFIG_WINDOW_WIDTH |
1837 XCB_CONFIG_WINDOW_HEIGHT,
1838 values);
1839
1840 window->configure_source = NULL;
1841
1842 weston_wm_window_schedule_repaint(window);
1843}
1844
1845static void
1846send_configure(struct weston_surface *surface,
1847 uint32_t edges, int32_t width, int32_t height)
1848{
1849 struct weston_wm_window *window = get_wm_window(surface);
1850 struct weston_wm *wm = window->wm;
1851 struct theme *t = window->wm->theme;
1852
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001853 if (window->fullscreen) {
1854 window->width = width;
1855 window->height = height;
1856 } else if (window->decorate) {
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001857 window->width = width - 2 * (t->margin + t->width);
1858 window->height = height - 2 * t->margin -
1859 t->titlebar_height - t->width;
1860 } else {
1861 window->width = width - 2 * t->margin;
1862 window->height = height - 2 * t->margin;
1863 }
1864
1865 if (window->configure_source)
1866 return;
1867
1868 window->configure_source =
1869 wl_event_loop_add_idle(wm->server->loop,
1870 weston_wm_window_configure, window);
1871}
1872
1873static const struct weston_shell_client shell_client = {
1874 send_configure
1875};
1876
1877static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001878xserver_map_shell_surface(struct weston_wm *wm,
1879 struct weston_wm_window *window)
1880{
1881 struct weston_shell_interface *shell_interface =
1882 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001883 struct theme *t = window->wm->theme;
1884
1885 if (!shell_interface->create_shell_surface)
1886 return;
1887
1888 window->shsurf =
1889 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001890 window->surface,
1891 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001892
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001893 if (window->fullscreen) {
1894 window->saved_width = window->width;
1895 window->saved_height = window->height;
1896 shell_interface->set_fullscreen(window->shsurf,
1897 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1898 0, NULL);
1899 return;
1900 } else if (!window->override_redirect) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001901 shell_interface->set_toplevel(window->shsurf);
1902 return;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001903 } else {
1904 shell_interface->set_xwayland(window->shsurf,
1905 window->x + t->margin,
1906 window->y + t->margin,
1907 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001908 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001909}
1910
1911static void
1912xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
1913 struct wl_resource *surface_resource, uint32_t id)
1914{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001915 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001916 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001917 struct weston_surface *surface =
1918 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001919 struct weston_wm_window *window;
1920
1921 if (client != wxs->client)
1922 return;
1923
1924 window = hash_table_lookup(wm->window_hash, id);
1925 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001926 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001927 return;
1928 }
1929
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001930 wm_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001931
1932 weston_wm_window_read_properties(window);
1933
1934 window->surface = (struct weston_surface *) surface;
1935 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001936 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001937 &window->surface_destroy_listener);
1938
1939 weston_wm_window_schedule_repaint(window);
1940 xserver_map_shell_surface(wm, window);
1941}
1942
1943const struct xserver_interface xserver_implementation = {
1944 xserver_set_window_id
1945};