blob: b6a3aa2058a571d68d984f849b503dc503b89a09 [file] [log] [blame]
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001/*
2 * Copyright © 2011 Intel Corporation
3 *
Bryce Harrington0a007dd2015-06-11 16:22:34 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Kristian Høgsberg380deee2012-05-21 17:12:41 -040011 *
Bryce Harrington0a007dd2015-06-11 16:22:34 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Kristian Høgsberg380deee2012-05-21 17:12:41 -040024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040027
28#include <stdlib.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030029#include <stdint.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040030#include <stdio.h>
31#include <string.h>
32#include <sys/socket.h>
33#include <sys/un.h>
34#include <fcntl.h>
35#include <errno.h>
36#include <unistd.h>
37#include <signal.h>
Tiago Vignatti90fada42012-07-16 12:02:08 -040038#include <X11/Xcursor/Xcursor.h>
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -050039#include <linux/input.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040040
Daniel Stone67fe3db2016-10-31 14:51:18 +000041#include "compositor.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040042#include "xwayland.h"
Quentin Glidic955cec02016-08-12 10:41:35 +020043#include "xwayland-internal-interface.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040044
Kristian Høgsberg2ba10df2013-12-03 16:38:15 -080045#include "cairo-util.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040046#include "hash.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070047#include "shared/helpers.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040048
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070049struct wm_size_hints {
Bryce Harringtone00554b2015-06-12 10:17:39 -070050 uint32_t flags;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070051 int32_t x, y;
52 int32_t width, height; /* should set so old wm's don't mess up */
53 int32_t min_width, min_height;
54 int32_t max_width, max_height;
Bryce Harringtone00554b2015-06-12 10:17:39 -070055 int32_t width_inc, height_inc;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070056 struct {
57 int32_t x;
58 int32_t y;
59 } min_aspect, max_aspect;
60 int32_t base_width, base_height;
61 int32_t win_gravity;
62};
63
64#define USPosition (1L << 0)
65#define USSize (1L << 1)
66#define PPosition (1L << 2)
67#define PSize (1L << 3)
68#define PMinSize (1L << 4)
69#define PMaxSize (1L << 5)
70#define PResizeInc (1L << 6)
71#define PAspect (1L << 7)
72#define PBaseSize (1L << 8)
73#define PWinGravity (1L << 9)
74
Kristian Høgsberg380deee2012-05-21 17:12:41 -040075struct motif_wm_hints {
76 uint32_t flags;
77 uint32_t functions;
78 uint32_t decorations;
79 int32_t input_mode;
80 uint32_t status;
81};
82
83#define MWM_HINTS_FUNCTIONS (1L << 0)
84#define MWM_HINTS_DECORATIONS (1L << 1)
85#define MWM_HINTS_INPUT_MODE (1L << 2)
86#define MWM_HINTS_STATUS (1L << 3)
87
88#define MWM_FUNC_ALL (1L << 0)
89#define MWM_FUNC_RESIZE (1L << 1)
90#define MWM_FUNC_MOVE (1L << 2)
91#define MWM_FUNC_MINIMIZE (1L << 3)
92#define MWM_FUNC_MAXIMIZE (1L << 4)
93#define MWM_FUNC_CLOSE (1L << 5)
94
95#define MWM_DECOR_ALL (1L << 0)
96#define MWM_DECOR_BORDER (1L << 1)
97#define MWM_DECOR_RESIZEH (1L << 2)
98#define MWM_DECOR_TITLE (1L << 3)
99#define MWM_DECOR_MENU (1L << 4)
100#define MWM_DECOR_MINIMIZE (1L << 5)
101#define MWM_DECOR_MAXIMIZE (1L << 6)
102
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700103#define MWM_DECOR_EVERYTHING \
104 (MWM_DECOR_BORDER | MWM_DECOR_RESIZEH | MWM_DECOR_TITLE | \
105 MWM_DECOR_MENU | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE)
106
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400107#define MWM_INPUT_MODELESS 0
108#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
109#define MWM_INPUT_SYSTEM_MODAL 2
110#define MWM_INPUT_FULL_APPLICATION_MODAL 3
111#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
112
113#define MWM_TEAROFF_WINDOW (1L<<0)
114
115#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
116#define _NET_WM_MOVERESIZE_SIZE_TOP 1
117#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
118#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
119#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
120#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
121#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
122#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
123#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
124#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
125#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
126#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
127
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400128struct weston_wm_window {
129 struct weston_wm *wm;
130 xcb_window_t id;
131 xcb_window_t frame_id;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500132 struct frame *frame;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400133 cairo_surface_t *cairo_surface;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700134 uint32_t surface_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400135 struct weston_surface *surface;
Quentin Glidic955cec02016-08-12 10:41:35 +0200136 struct weston_desktop_xwayland_surface *shsurf;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400137 struct wl_listener surface_destroy_listener;
138 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400139 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400140 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300141 int pid;
142 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400143 char *class;
144 char *name;
145 struct weston_wm_window *transient_for;
146 uint32_t protocols;
147 xcb_atom_t type;
148 int width, height;
149 int x, y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200150 bool pos_dirty;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500151 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400152 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300153 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500154 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500155 int has_alpha;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700156 int delete_window;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200157 int maximized_vert;
158 int maximized_horz;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700159 struct wm_size_hints size_hints;
160 struct motif_wm_hints motif_hints;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700161 struct wl_list link;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400162};
163
164static struct weston_wm_window *
165get_wm_window(struct weston_surface *surface);
166
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400167static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200168weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window);
169
170static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400171weston_wm_window_schedule_repaint(struct weston_wm_window *window);
172
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700173static void
174xserver_map_shell_surface(struct weston_wm_window *window,
175 struct weston_surface *surface);
176
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400177static int __attribute__ ((format (printf, 1, 2)))
178wm_log(const char *fmt, ...)
179{
180#ifdef WM_DEBUG
181 int l;
182 va_list argp;
183
184 va_start(argp, fmt);
185 l = weston_vlog(fmt, argp);
186 va_end(argp);
187
188 return l;
189#else
190 return 0;
191#endif
192}
193
194static int __attribute__ ((format (printf, 1, 2)))
195wm_log_continue(const char *fmt, ...)
196{
197#ifdef WM_DEBUG
198 int l;
199 va_list argp;
200
201 va_start(argp, fmt);
202 l = weston_vlog_continue(fmt, argp);
203 va_end(argp);
204
205 return l;
206#else
207 return 0;
208#endif
209}
210
Derek Foreman49372142015-04-09 10:51:22 -0500211static bool __attribute__ ((warn_unused_result))
212wm_lookup_window(struct weston_wm *wm, xcb_window_t hash,
213 struct weston_wm_window **window)
214{
215 *window = hash_table_lookup(wm->window_hash, hash);
216 if (*window)
217 return true;
218 return false;
219}
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400220
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400221const char *
222get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
223{
224 xcb_get_atom_name_cookie_t cookie;
225 xcb_get_atom_name_reply_t *reply;
226 xcb_generic_error_t *e;
227 static char buffer[64];
228
229 if (atom == XCB_ATOM_NONE)
230 return "None";
231
232 cookie = xcb_get_atom_name (c, atom);
233 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500234
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300235 if (reply) {
MoD55375b92013-06-11 19:59:42 -0500236 snprintf(buffer, sizeof buffer, "%.*s",
237 xcb_get_atom_name_name_length (reply),
238 xcb_get_atom_name_name (reply));
239 } else {
240 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
241 }
242
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400243 free(reply);
244
245 return buffer;
246}
247
Tiago Vignatti90fada42012-07-16 12:02:08 -0400248static xcb_cursor_t
249xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
250{
251 xcb_connection_t *c = wm->conn;
252 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
253 xcb_screen_t *screen = s.data;
254 xcb_gcontext_t gc;
255 xcb_pixmap_t pix;
256 xcb_render_picture_t pic;
257 xcb_cursor_t cursor;
258 int stride = img->width * 4;
259
260 pix = xcb_generate_id(c);
261 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
262
263 pic = xcb_generate_id(c);
264 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
265
266 gc = xcb_generate_id(c);
267 xcb_create_gc(c, gc, pix, 0, 0);
268
269 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
270 img->width, img->height, 0, 0, 0, 32,
271 stride * img->height, (uint8_t *) img->pixels);
272 xcb_free_gc(c, gc);
273
274 cursor = xcb_generate_id(c);
275 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
276
277 xcb_render_free_picture(c, pic);
278 xcb_free_pixmap(c, pix);
279
280 return cursor;
281}
282
283static xcb_cursor_t
284xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
285{
286 /* TODO: treat animated cursors as well */
287 if (images->nimage != 1)
288 return -1;
289
290 return xcb_cursor_image_load_cursor(wm, images->images[0]);
291}
292
293static xcb_cursor_t
294xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
295{
296 xcb_cursor_t cursor;
297 XcursorImages *images;
298 char *v = NULL;
299 int size = 0;
300
301 if (!file)
302 return 0;
303
304 v = getenv ("XCURSOR_SIZE");
305 if (v)
306 size = atoi(v);
307
308 if (!size)
309 size = 32;
310
311 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300312 if (!images)
313 return -1;
314
Tiago Vignatti90fada42012-07-16 12:02:08 -0400315 cursor = xcb_cursor_images_load_cursor (wm, images);
316 XcursorImagesDestroy (images);
317
318 return cursor;
319}
320
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400321void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400322dump_property(struct weston_wm *wm,
323 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400324{
325 int32_t *incr_value;
326 const char *text_value, *name;
327 xcb_atom_t *atom_value;
328 int width, len;
329 uint32_t i;
330
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400331 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400332 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400333 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400334 return;
335 }
336
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400337 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
338 get_atom_name(wm->conn, reply->type),
339 reply->format,
340 xcb_get_property_value_length(reply),
341 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400342
343 if (reply->type == wm->atom.incr) {
344 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400345 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400346 } else if (reply->type == wm->atom.utf8_string ||
347 reply->type == wm->atom.string) {
348 text_value = xcb_get_property_value(reply);
349 if (reply->value_len > 40)
350 len = 40;
351 else
352 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400353 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400354 } else if (reply->type == XCB_ATOM_ATOM) {
355 atom_value = xcb_get_property_value(reply);
356 for (i = 0; i < reply->value_len; i++) {
357 name = get_atom_name(wm->conn, atom_value[i]);
358 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400359 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400360 width = 4;
361 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400362 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400363 }
364
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400365 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400366 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400367 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400368 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400369 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400370 }
371}
372
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200373static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400374read_and_dump_property(struct weston_wm *wm,
375 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400376{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400377 xcb_get_property_reply_t *reply;
378 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400379
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400380 cookie = xcb_get_property(wm->conn, 0, window,
381 property, XCB_ATOM_ANY, 0, 2048);
382 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400383
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400384 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400385
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400386 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400387}
388
389/* We reuse some predefined, but otherwise useles atoms */
390#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
391#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500392#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700393#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400394
395static void
396weston_wm_window_read_properties(struct weston_wm_window *window)
397{
398 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +0200399 const struct weston_desktop_xwayland_interface *xwayland_interface =
400 wm->server->compositor->xwayland_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400401
402#define F(field) offsetof(struct weston_wm_window, field)
403 const struct {
404 xcb_atom_t atom;
405 xcb_atom_t type;
406 int offset;
407 } props[] = {
408 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
409 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
410 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
411 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700412 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500413 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400414 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
415 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300416 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400417 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300418 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400419 };
420#undef F
421
422 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
423 xcb_get_property_reply_t *reply;
424 void *p;
425 uint32_t *xid;
426 xcb_atom_t *atom;
427 uint32_t i;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200428 char name[1024];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400429
430 if (!window->properties_dirty)
431 return;
432 window->properties_dirty = 0;
433
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400434 for (i = 0; i < ARRAY_LENGTH(props); i++)
435 cookie[i] = xcb_get_property(wm->conn,
436 0, /* delete */
437 window->id,
438 props[i].atom,
439 XCB_ATOM_ANY, 0, 2048);
440
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700441 window->decorate = window->override_redirect ? 0 : MWM_DECOR_EVERYTHING;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700442 window->size_hints.flags = 0;
443 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700444 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700445
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400446 for (i = 0; i < ARRAY_LENGTH(props); i++) {
447 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
448 if (!reply)
449 /* Bad window, typically */
450 continue;
451 if (reply->type == XCB_ATOM_NONE) {
452 /* No such property */
453 free(reply);
454 continue;
455 }
456
457 p = ((char *) window + props[i].offset);
458
459 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300460 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400461 case XCB_ATOM_STRING:
462 /* FIXME: We're using this for both string and
463 utf8_string */
464 if (*(char **) p)
465 free(*(char **) p);
466
467 *(char **) p =
468 strndup(xcb_get_property_value(reply),
469 xcb_get_property_value_length(reply));
470 break;
471 case XCB_ATOM_WINDOW:
472 xid = xcb_get_property_value(reply);
Derek Foreman49372142015-04-09 10:51:22 -0500473 if (!wm_lookup_window(wm, *xid, p))
474 weston_log("XCB_ATOM_WINDOW contains window"
475 " id not found in hash table.\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400476 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300477 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400478 case XCB_ATOM_ATOM:
479 atom = xcb_get_property_value(reply);
480 *(xcb_atom_t *) p = *atom;
481 break;
482 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700483 atom = xcb_get_property_value(reply);
484 for (i = 0; i < reply->value_len; i++)
Derek Foremanb4deec62015-04-07 12:12:13 -0500485 if (atom[i] == wm->atom.wm_delete_window) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700486 window->delete_window = 1;
Derek Foremanb4deec62015-04-07 12:12:13 -0500487 break;
488 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400489 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700490 case TYPE_WM_NORMAL_HINTS:
491 memcpy(&window->size_hints,
492 xcb_get_property_value(reply),
493 sizeof window->size_hints);
494 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500495 case TYPE_NET_WM_STATE:
496 window->fullscreen = 0;
497 atom = xcb_get_property_value(reply);
Ryo Munakataf3744f52015-03-11 17:36:30 +0900498 for (i = 0; i < reply->value_len; i++) {
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500499 if (atom[i] == wm->atom.net_wm_state_fullscreen)
500 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200501 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
502 window->maximized_vert = 1;
503 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
504 window->maximized_horz = 1;
Ryo Munakataf3744f52015-03-11 17:36:30 +0900505 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500506 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400507 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700508 memcpy(&window->motif_hints,
509 xcb_get_property_value(reply),
510 sizeof window->motif_hints);
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700511 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) {
512 if (window->motif_hints.decorations & MWM_DECOR_ALL)
513 /* MWM_DECOR_ALL means all except the other values listed. */
514 window->decorate =
515 MWM_DECOR_EVERYTHING & (~window->motif_hints.decorations);
516 else
517 window->decorate =
518 window->motif_hints.decorations;
519 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400520 break;
521 default:
522 break;
523 }
524 free(reply);
525 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200526
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200527 if (window->pid > 0) {
528 gethostname(name, sizeof(name));
529 for (i = 0; i < sizeof(name); i++) {
530 if (name[i] == '\0')
531 break;
532 }
533 if (i == sizeof(name))
534 name[0] = '\0'; /* ignore stupid hostnames */
535
536 /* this is only one heuristic to guess the PID of a client is
537 * valid, assuming it's compliant with icccm and ewmh.
538 * Non-compliants and remote applications of course fail. */
539 if (!window->machine || strcmp(window->machine, name))
540 window->pid = 0;
541 }
542
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200543 if (window->shsurf && window->name)
Quentin Glidic955cec02016-08-12 10:41:35 +0200544 xwayland_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500545 if (window->frame && window->name)
546 frame_set_title(window->frame, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200547 if (window->shsurf && window->pid > 0)
Quentin Glidic955cec02016-08-12 10:41:35 +0200548 xwayland_interface->set_pid(window->shsurf, window->pid);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400549}
550
551static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400552weston_wm_window_get_frame_size(struct weston_wm_window *window,
553 int *width, int *height)
554{
555 struct theme *t = window->wm->theme;
556
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500557 if (window->fullscreen) {
558 *width = window->width;
559 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800560 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500561 *width = frame_width(window->frame);
562 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400563 } else {
564 *width = window->width + t->margin * 2;
565 *height = window->height + t->margin * 2;
566 }
567}
568
569static void
570weston_wm_window_get_child_position(struct weston_wm_window *window,
571 int *x, int *y)
572{
573 struct theme *t = window->wm->theme;
574
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500575 if (window->fullscreen) {
576 *x = 0;
577 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800578 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500579 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400580 } else {
581 *x = t->margin;
582 *y = t->margin;
583 }
584}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400585
586static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500587weston_wm_window_send_configure_notify(struct weston_wm_window *window)
588{
589 xcb_configure_notify_event_t configure_notify;
590 struct weston_wm *wm = window->wm;
591 int x, y;
592
593 weston_wm_window_get_child_position(window, &x, &y);
594 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
595 configure_notify.pad0 = 0;
596 configure_notify.event = window->id;
597 configure_notify.window = window->id;
598 configure_notify.above_sibling = XCB_WINDOW_NONE;
599 configure_notify.x = x;
600 configure_notify.y = y;
601 configure_notify.width = window->width;
602 configure_notify.height = window->height;
603 configure_notify.border_width = 0;
604 configure_notify.override_redirect = 0;
605 configure_notify.pad1 = 0;
606
Murray Calavera883ac022015-06-06 13:02:22 +0000607 xcb_send_event(wm->conn, 0, window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500608 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
609 (char *) &configure_notify);
610}
611
612static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400613weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
614{
Murray Calavera883ac022015-06-06 13:02:22 +0000615 xcb_configure_request_event_t *configure_request =
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400616 (xcb_configure_request_event_t *) event;
617 struct weston_wm_window *window;
618 uint32_t mask, values[16];
619 int x, y, width, height, i = 0;
620
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400621 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
622 configure_request->window,
623 configure_request->x, configure_request->y,
624 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400625
Derek Foreman49372142015-04-09 10:51:22 -0500626 if (!wm_lookup_window(wm, configure_request->window, &window))
627 return;
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400628
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500629 if (window->fullscreen) {
630 weston_wm_window_send_configure_notify(window);
631 return;
632 }
633
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400634 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
635 window->width = configure_request->width;
636 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
637 window->height = configure_request->height;
638
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500639 if (window->frame)
640 frame_resize_inside(window->frame, window->width, window->height);
641
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400642 weston_wm_window_get_child_position(window, &x, &y);
643 values[i++] = x;
644 values[i++] = y;
645 values[i++] = window->width;
646 values[i++] = window->height;
647 values[i++] = 0;
648 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
649 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
650 XCB_CONFIG_WINDOW_BORDER_WIDTH;
651 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
652 values[i++] = configure_request->sibling;
653 mask |= XCB_CONFIG_WINDOW_SIBLING;
654 }
655 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
656 values[i++] = configure_request->stack_mode;
657 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
658 }
659
660 xcb_configure_window(wm->conn, window->id, mask, values);
661
662 weston_wm_window_get_frame_size(window, &width, &height);
663 values[0] = width;
664 values[1] = height;
665 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
666 xcb_configure_window(wm->conn, window->frame_id, mask, values);
667
668 weston_wm_window_schedule_repaint(window);
669}
670
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400671static int
672our_resource(struct weston_wm *wm, uint32_t id)
673{
674 const xcb_setup_t *setup;
675
676 setup = xcb_get_setup(wm->conn);
677
678 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
679}
680
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400681static void
682weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
683{
Murray Calavera883ac022015-06-06 13:02:22 +0000684 xcb_configure_notify_event_t *configure_notify =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400685 (xcb_configure_notify_event_t *) event;
686 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400687
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400688 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400689 configure_notify->window,
690 configure_notify->x, configure_notify->y,
691 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300692
Derek Foreman49372142015-04-09 10:51:22 -0500693 if (!wm_lookup_window(wm, configure_notify->window, &window))
694 return;
695
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700696 window->x = configure_notify->x;
697 window->y = configure_notify->y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200698 window->pos_dirty = false;
699
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700700 if (window->override_redirect) {
701 window->width = configure_notify->width;
702 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500703 if (window->frame)
704 frame_resize_inside(window->frame,
705 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700706 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400707}
708
709static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300710weston_wm_kill_client(struct wl_listener *listener, void *data)
711{
712 struct weston_surface *surface = data;
713 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300714 if (!window)
715 return;
716
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200717 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300718 kill(window->pid, SIGKILL);
719}
720
721static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700722weston_wm_create_surface(struct wl_listener *listener, void *data)
723{
724 struct weston_surface *surface = data;
725 struct weston_wm *wm =
726 container_of(listener,
727 struct weston_wm, create_surface_listener);
728 struct weston_wm_window *window;
729
730 if (wl_resource_get_client(surface->resource) != wm->server->client)
731 return;
732
733 wl_list_for_each(window, &wm->unpaired_window_list, link)
734 if (window->surface_id ==
735 wl_resource_get_id(surface->resource)) {
736 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700737 window->surface_id = 0;
738 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700739 break;
Murray Calavera883ac022015-06-06 13:02:22 +0000740 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700741}
742
743static void
Giulio Camuffob18f7882015-03-29 14:20:23 +0300744weston_wm_send_focus_window(struct weston_wm *wm,
745 struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400746{
Scott Moreau85ecac02012-05-21 15:49:14 -0600747 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400748
Scott Moreau85ecac02012-05-21 15:49:14 -0600749 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700750 uint32_t values[1];
751
Boyan Dingb9f863c2014-08-30 10:33:23 +0800752 if (window->override_redirect)
753 return;
754
Scott Moreau85ecac02012-05-21 15:49:14 -0600755 client_message.response_type = XCB_CLIENT_MESSAGE;
756 client_message.format = 32;
757 client_message.window = window->id;
758 client_message.type = wm->atom.wm_protocols;
759 client_message.data.data32[0] = wm->atom.wm_take_focus;
760 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
761
Murray Calavera883ac022015-06-06 13:02:22 +0000762 xcb_send_event(wm->conn, 0, window->id,
Scott Moreau85ecac02012-05-21 15:49:14 -0600763 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
764 (char *) &client_message);
765
766 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
767 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700768
769 values[0] = XCB_STACK_MODE_ABOVE;
770 xcb_configure_window (wm->conn, window->frame_id,
771 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600772 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400773 xcb_set_input_focus (wm->conn,
774 XCB_INPUT_FOCUS_POINTER_ROOT,
775 XCB_NONE,
776 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600777 }
Giulio Camuffob18f7882015-03-29 14:20:23 +0300778}
779
780static void
781weston_wm_window_activate(struct wl_listener *listener, void *data)
782{
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +0800783 struct weston_surface_activation_data *activation_data = data;
784 struct weston_surface *surface = activation_data->surface;
Giulio Camuffob18f7882015-03-29 14:20:23 +0300785 struct weston_wm_window *window = NULL;
786 struct weston_wm *wm =
787 container_of(listener, struct weston_wm, activate_listener);
788
789 if (surface) {
790 window = get_wm_window(surface);
791 }
792
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200793 if (window) {
794 weston_wm_set_net_active_window(wm, window->id);
795 } else {
796 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
797 }
798
Giulio Camuffob18f7882015-03-29 14:20:23 +0300799 weston_wm_send_focus_window(wm, window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400800
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500801 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800802 if (wm->focus_window->frame)
803 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400804 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500805 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400806 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500807 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800808 if (wm->focus_window->frame)
809 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400810 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500811 }
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200812
813 xcb_flush(wm->conn);
814
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400815}
816
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400817#define ICCCM_WITHDRAWN_STATE 0
818#define ICCCM_NORMAL_STATE 1
819#define ICCCM_ICONIC_STATE 3
820
821static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500822weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400823{
824 struct weston_wm *wm = window->wm;
825 uint32_t property[2];
826
827 property[0] = state;
828 property[1] = XCB_WINDOW_NONE;
829
830 xcb_change_property(wm->conn,
831 XCB_PROP_MODE_REPLACE,
832 window->id,
833 wm->atom.wm_state,
834 wm->atom.wm_state,
835 32, /* format */
836 2, property);
837}
838
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400839static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500840weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
841{
842 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200843 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500844 int i;
845
846 i = 0;
847 if (window->fullscreen)
848 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200849 if (window->maximized_vert)
850 property[i++] = wm->atom.net_wm_state_maximized_vert;
851 if (window->maximized_horz)
852 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500853
854 xcb_change_property(wm->conn,
855 XCB_PROP_MODE_REPLACE,
856 window->id,
857 wm->atom.net_wm_state,
858 XCB_ATOM_ATOM,
859 32, /* format */
860 i, property);
861}
862
863static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700864weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400865{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700866 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400867 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400868 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200869 int buttons = FRAME_BUTTON_CLOSE;
870
871 if (window->decorate & MWM_DECOR_MAXIMIZE)
872 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400873
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500874 window->frame = frame_create(window->wm->theme,
875 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200876 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500877 frame_resize_inside(window->frame, window->width, window->height);
878
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400879 weston_wm_window_get_frame_size(window, &width, &height);
880 weston_wm_window_get_child_position(window, &x, &y);
881
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400882 values[0] = wm->screen->black_pixel;
883 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400884 XCB_EVENT_MASK_KEY_PRESS |
885 XCB_EVENT_MASK_KEY_RELEASE |
886 XCB_EVENT_MASK_BUTTON_PRESS |
887 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400888 XCB_EVENT_MASK_POINTER_MOTION |
889 XCB_EVENT_MASK_ENTER_WINDOW |
890 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400891 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400892 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400893 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400894
895 window->frame_id = xcb_generate_id(wm->conn);
896 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400897 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400898 window->frame_id,
899 wm->screen->root,
900 0, 0,
901 width, height,
902 0,
903 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400904 wm->visual_id,
905 XCB_CW_BORDER_PIXEL |
906 XCB_CW_EVENT_MASK |
907 XCB_CW_COLORMAP, values);
908
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400909 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
910
911 values[0] = 0;
912 xcb_configure_window(wm->conn, window->id,
913 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
914
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400915 window->cairo_surface =
916 cairo_xcb_surface_create_with_xrender_format(wm->conn,
917 wm->screen,
918 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400919 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400920 width, height);
921
922 hash_table_insert(wm->window_hash, window->frame_id, window);
923}
924
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200925/*
926 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
927 * Passing a <0 desktop value deletes the property.
928 */
929static void
930weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
Bryce Harringtone00554b2015-06-12 10:17:39 -0700931 int desktop)
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200932{
933 if (desktop >= 0) {
934 xcb_change_property(window->wm->conn,
935 XCB_PROP_MODE_REPLACE,
936 window->id,
937 window->wm->atom.net_wm_desktop,
938 XCB_ATOM_CARDINAL,
939 32, /* format */
940 1, &desktop);
941 } else {
942 xcb_delete_property(window->wm->conn,
943 window->id,
944 window->wm->atom.net_wm_desktop);
945 }
946}
947
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400948static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700949weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
950{
951 xcb_map_request_event_t *map_request =
952 (xcb_map_request_event_t *) event;
953 struct weston_wm_window *window;
954
955 if (our_resource(wm, map_request->window)) {
956 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
957 map_request->window);
958 return;
959 }
960
Derek Foreman49372142015-04-09 10:51:22 -0500961 if (!wm_lookup_window(wm, map_request->window, &window))
962 return;
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700963
964 weston_wm_window_read_properties(window);
965
966 if (window->frame_id == XCB_WINDOW_NONE)
967 weston_wm_window_create_frame(window);
968
969 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
970 window->id, window, window->frame_id);
971
972 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
973 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200974 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700975
976 xcb_map_window(wm->conn, map_request->window);
977 xcb_map_window(wm->conn, window->frame_id);
978}
979
980static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400981weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
982{
983 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
984
985 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400986 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
987 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400988 return;
989 }
990
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400991 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400992}
993
994static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400995weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
996{
997 xcb_unmap_notify_event_t *unmap_notify =
998 (xcb_unmap_notify_event_t *) event;
999 struct weston_wm_window *window;
1000
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001001 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
1002 unmap_notify->window,
1003 unmap_notify->event,
1004 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001005
1006 if (our_resource(wm, unmap_notify->window))
1007 return;
1008
MoD31700122013-06-11 19:58:55 -05001009 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -04001010 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1011 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001012 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001013
Derek Foreman49372142015-04-09 10:51:22 -05001014 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1015 return;
1016
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001017 if (window->surface_id) {
1018 /* Make sure we're not on the unpaired surface list or we
1019 * could be assigned a surface during surface creation that
1020 * was mapped before this unmap request.
1021 */
1022 wl_list_remove(&window->link);
1023 window->surface_id = 0;
1024 }
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001025 if (wm->focus_window == window)
1026 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001027 if (window->surface)
1028 wl_list_remove(&window->surface_destroy_listener.link);
1029 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +02001030 window->shsurf = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001031
1032 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1033 weston_wm_window_set_virtual_desktop(window, -1);
1034
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001035 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001036}
1037
1038static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001039weston_wm_window_draw_decoration(void *data)
1040{
1041 struct weston_wm_window *window = data;
1042 struct weston_wm *wm = window->wm;
1043 struct theme *t = wm->theme;
1044 cairo_t *cr;
1045 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001046 int32_t input_x, input_y, input_w, input_h;
Quentin Glidic955cec02016-08-12 10:41:35 +02001047 const struct weston_desktop_xwayland_interface *xwayland_interface =
1048 wm->server->compositor->xwayland_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001049 uint32_t flags = 0;
Giulio Camuffoaa974782015-02-01 16:18:51 +02001050 struct weston_view *view;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001051
1052 weston_wm_window_read_properties(window);
1053
1054 window->repaint_source = NULL;
1055
1056 weston_wm_window_get_frame_size(window, &width, &height);
1057 weston_wm_window_get_child_position(window, &x, &y);
1058
1059 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1060 cr = cairo_create(window->cairo_surface);
1061
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001062 if (window->fullscreen) {
1063 /* nothing */
1064 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001065 if (wm->focus_window == window)
1066 flags |= THEME_FRAME_ACTIVE;
1067
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001068 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001069 } else {
1070 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1071 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1072 cairo_paint(cr);
1073
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001074 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001075 }
1076
1077 cairo_destroy(cr);
1078
1079 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001080 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001081 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001082 pixman_region32_init(&window->surface->pending.opaque);
1083 } else {
1084 /* We leave an extra pixel around the X window area to
1085 * make sure we don't sample from the undefined alpha
1086 * channel when filtering. */
Murray Calavera883ac022015-06-06 13:02:22 +00001087 pixman_region32_init_rect(&window->surface->pending.opaque,
MoD384a11a2013-06-22 11:04:21 -05001088 x - 1, y - 1,
1089 window->width + 2,
1090 window->height + 2);
1091 }
Giulio Camuffoaa974782015-02-01 16:18:51 +02001092 wl_list_for_each(view, &window->surface->views, surface_link)
1093 weston_view_geometry_dirty(view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001094
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001095 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001096
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001097 if (window->decorate && !window->fullscreen) {
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001098 frame_input_rect(window->frame, &input_x, &input_y,
1099 &input_w, &input_h);
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001100 } else {
1101 input_x = x;
1102 input_y = y;
1103 input_w = width;
1104 input_h = height;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001105 }
1106
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001107 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001108 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001109
Quentin Glidic955cec02016-08-12 10:41:35 +02001110 xwayland_interface->set_window_geometry(window->shsurf,
1111 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001112 }
1113}
1114
1115static void
1116weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1117{
1118 struct weston_wm *wm = window->wm;
Giulio Camuffoaa974782015-02-01 16:18:51 +02001119 struct weston_view *view;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001120 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001121
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001122 if (window->frame_id == XCB_WINDOW_NONE) {
1123 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001124 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001125 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001126 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001127 pixman_region32_init(&window->surface->pending.opaque);
1128 } else {
1129 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1130 width, height);
1131 }
Giulio Camuffoaa974782015-02-01 16:18:51 +02001132 wl_list_for_each(view, &window->surface->views, surface_link)
1133 weston_view_geometry_dirty(view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001134 }
1135 return;
1136 }
1137
1138 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001139 return;
1140
1141 window->repaint_source =
1142 wl_event_loop_add_idle(wm->server->loop,
1143 weston_wm_window_draw_decoration,
1144 window);
1145}
1146
1147static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001148weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1149{
1150 xcb_property_notify_event_t *property_notify =
1151 (xcb_property_notify_event_t *) event;
1152 struct weston_wm_window *window;
1153
Derek Foreman49372142015-04-09 10:51:22 -05001154 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001155 return;
1156
1157 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001158
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001159 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001160 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001161 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001162 else
1163 read_and_dump_property(wm, property_notify->window,
1164 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001165
1166 if (property_notify->atom == wm->atom.net_wm_name ||
1167 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001168 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001169}
1170
1171static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001172weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001173 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001174{
1175 struct weston_wm_window *window;
1176 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001177 xcb_get_geometry_cookie_t geometry_cookie;
1178 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001179
Peter Huttererf3d62272013-08-08 11:57:05 +10001180 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001181 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001182 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001183 return;
1184 }
1185
MoD384a11a2013-06-22 11:04:21 -05001186 geometry_cookie = xcb_get_geometry(wm->conn, id);
1187
Giulio Camuffob18f7882015-03-29 14:20:23 +03001188 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
1189 XCB_EVENT_MASK_FOCUS_CHANGE;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001190 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1191
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001192 window->wm = wm;
1193 window->id = id;
1194 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001195 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001196 window->width = width;
1197 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001198 window->x = x;
1199 window->y = y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001200 window->pos_dirty = false;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001201
MoD384a11a2013-06-22 11:04:21 -05001202 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1203 /* technically we should use XRender and check the visual format's
1204 alpha_mask, but checking depth is simpler and works in all known cases */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001205 if (geometry_reply != NULL)
MoD384a11a2013-06-22 11:04:21 -05001206 window->has_alpha = geometry_reply->depth == 32;
1207 free(geometry_reply);
1208
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001209 hash_table_insert(wm->window_hash, id, window);
1210}
1211
1212static void
1213weston_wm_window_destroy(struct weston_wm_window *window)
1214{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001215 struct weston_wm *wm = window->wm;
1216
1217 if (window->repaint_source)
1218 wl_event_source_remove(window->repaint_source);
1219 if (window->cairo_surface)
1220 cairo_surface_destroy(window->cairo_surface);
1221
1222 if (window->frame_id) {
1223 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1224 xcb_destroy_window(wm->conn, window->frame_id);
1225 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001226 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001227 hash_table_remove(wm->window_hash, window->frame_id);
1228 window->frame_id = XCB_WINDOW_NONE;
1229 }
1230
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001231 if (window->surface_id)
1232 wl_list_remove(&window->link);
1233
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001234 if (window->surface)
1235 wl_list_remove(&window->surface_destroy_listener.link);
1236
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001237 hash_table_remove(window->wm->window_hash, window->id);
1238 free(window);
1239}
1240
1241static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001242weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1243{
1244 xcb_create_notify_event_t *create_notify =
1245 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001246
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001247 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1248 create_notify->window,
1249 create_notify->width, create_notify->height,
1250 create_notify->override_redirect ? ", override" : "",
1251 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001252
1253 if (our_resource(wm, create_notify->window))
1254 return;
1255
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001256 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001257 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001258 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001259 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001260}
1261
1262static void
1263weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1264{
1265 xcb_destroy_notify_event_t *destroy_notify =
1266 (xcb_destroy_notify_event_t *) event;
1267 struct weston_wm_window *window;
1268
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001269 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1270 destroy_notify->window,
1271 destroy_notify->event,
1272 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001273
1274 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001275 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001276
Derek Foreman49372142015-04-09 10:51:22 -05001277 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1278 return;
1279
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001280 weston_wm_window_destroy(window);
1281}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001282
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001283static void
1284weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1285{
1286 xcb_reparent_notify_event_t *reparent_notify =
1287 (xcb_reparent_notify_event_t *) event;
1288 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001289
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001290 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1291 reparent_notify->window,
1292 reparent_notify->parent,
1293 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001294
1295 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001296 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001297 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001298 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001299 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001300 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1301 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001302 weston_wm_window_destroy(window);
1303 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001304}
1305
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001306struct weston_seat *
1307weston_wm_pick_seat(struct weston_wm *wm)
1308{
1309 return container_of(wm->server->compositor->seat_list.next,
1310 struct weston_seat, link);
1311}
1312
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001313static struct weston_seat *
1314weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1315{
1316 struct weston_wm *wm = window->wm;
1317 struct weston_seat *seat, *s;
1318
1319 seat = NULL;
1320 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001321 struct weston_pointer *pointer = weston_seat_get_pointer(s);
1322 struct weston_pointer *old_pointer =
1323 weston_seat_get_pointer(seat);
1324
1325 if (pointer && pointer->focus &&
1326 pointer->focus->surface == window->surface &&
1327 pointer->button_count > 0 &&
1328 (!seat ||
1329 pointer->grab_serial -
1330 old_pointer->grab_serial < (1 << 30)))
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001331 seat = s;
1332 }
1333
1334 return seat;
1335}
1336
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001337static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001338weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1339 xcb_client_message_event_t *client_message)
1340{
1341 static const int map[] = {
1342 THEME_LOCATION_RESIZING_TOP_LEFT,
1343 THEME_LOCATION_RESIZING_TOP,
1344 THEME_LOCATION_RESIZING_TOP_RIGHT,
1345 THEME_LOCATION_RESIZING_RIGHT,
1346 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1347 THEME_LOCATION_RESIZING_BOTTOM,
1348 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1349 THEME_LOCATION_RESIZING_LEFT
1350 };
1351
1352 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001353 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Derek Foreman1281a362015-07-31 16:55:32 -05001354 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001355 int detail;
Quentin Glidic955cec02016-08-12 10:41:35 +02001356 const struct weston_desktop_xwayland_interface *xwayland_interface =
1357 wm->server->compositor->xwayland_interface;
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001358
Derek Foreman1281a362015-07-31 16:55:32 -05001359 if (!pointer || pointer->button_count != 1
1360 || !pointer->focus
1361 || pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001362 return;
1363
1364 detail = client_message->data.data32[2];
1365 switch (detail) {
1366 case _NET_WM_MOVERESIZE_MOVE:
Quentin Glidic955cec02016-08-12 10:41:35 +02001367 xwayland_interface->move(window->shsurf, pointer);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001368 break;
1369 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1370 case _NET_WM_MOVERESIZE_SIZE_TOP:
1371 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1372 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1373 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1374 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1375 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1376 case _NET_WM_MOVERESIZE_SIZE_LEFT:
Quentin Glidic955cec02016-08-12 10:41:35 +02001377 xwayland_interface->resize(window->shsurf, pointer, map[detail]);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001378 break;
1379 case _NET_WM_MOVERESIZE_CANCEL:
1380 break;
1381 }
1382}
1383
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001384#define _NET_WM_STATE_REMOVE 0
1385#define _NET_WM_STATE_ADD 1
1386#define _NET_WM_STATE_TOGGLE 2
1387
1388static int
1389update_state(int action, int *state)
1390{
1391 int new_state, changed;
1392
1393 switch (action) {
1394 case _NET_WM_STATE_REMOVE:
1395 new_state = 0;
1396 break;
1397 case _NET_WM_STATE_ADD:
1398 new_state = 1;
1399 break;
1400 case _NET_WM_STATE_TOGGLE:
1401 new_state = !*state;
1402 break;
1403 default:
1404 return 0;
1405 }
1406
1407 changed = (*state != new_state);
1408 *state = new_state;
1409
1410 return changed;
1411}
1412
1413static void
1414weston_wm_window_configure(void *data);
1415
1416static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001417weston_wm_window_set_toplevel(struct weston_wm_window *window)
1418{
Quentin Glidic955cec02016-08-12 10:41:35 +02001419 const struct weston_desktop_xwayland_interface *xwayland_interface =
1420 window->wm->server->compositor->xwayland_interface;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001421
Quentin Glidic955cec02016-08-12 10:41:35 +02001422 xwayland_interface->set_toplevel(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001423 window->width = window->saved_width;
1424 window->height = window->saved_height;
1425 if (window->frame)
1426 frame_resize_inside(window->frame,
1427 window->width,
1428 window->height);
1429 weston_wm_window_configure(window);
1430}
1431
1432static inline bool
1433weston_wm_window_is_maximized(struct weston_wm_window *window)
1434{
1435 return window->maximized_horz && window->maximized_vert;
1436}
1437
1438static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001439weston_wm_window_handle_state(struct weston_wm_window *window,
1440 xcb_client_message_event_t *client_message)
1441{
1442 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +02001443 const struct weston_desktop_xwayland_interface *xwayland_interface =
1444 wm->server->compositor->xwayland_interface;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001445 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001446 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001447
1448 action = client_message->data.data32[0];
1449 property = client_message->data.data32[1];
1450
1451 if (property == wm->atom.net_wm_state_fullscreen &&
1452 update_state(action, &window->fullscreen)) {
1453 weston_wm_window_set_net_wm_state(window);
1454 if (window->fullscreen) {
1455 window->saved_width = window->width;
1456 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001457
1458 if (window->shsurf)
Quentin Glidic955cec02016-08-12 10:41:35 +02001459 xwayland_interface->set_fullscreen(window->shsurf,
1460 NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001461 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001462 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001463 weston_wm_window_set_toplevel(window);
1464 }
1465 } else {
1466 if (property == wm->atom.net_wm_state_maximized_vert &&
1467 update_state(action, &window->maximized_vert))
1468 weston_wm_window_set_net_wm_state(window);
1469 if (property == wm->atom.net_wm_state_maximized_horz &&
1470 update_state(action, &window->maximized_horz))
1471 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001472
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001473 if (maximized != weston_wm_window_is_maximized(window)) {
1474 if (weston_wm_window_is_maximized(window)) {
1475 window->saved_width = window->width;
1476 window->saved_height = window->height;
1477
1478 if (window->shsurf)
Quentin Glidic955cec02016-08-12 10:41:35 +02001479 xwayland_interface->set_maximized(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001480 } else if (window->shsurf) {
1481 weston_wm_window_set_toplevel(window);
1482 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001483 }
1484 }
1485}
1486
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001487static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001488surface_destroy(struct wl_listener *listener, void *data)
1489{
1490 struct weston_wm_window *window =
1491 container_of(listener,
1492 struct weston_wm_window, surface_destroy_listener);
1493
1494 wm_log("surface for xid %d destroyed\n", window->id);
1495
1496 /* This should have been freed by the shell.
1497 * Don't try to use it later. */
1498 window->shsurf = NULL;
1499 window->surface = NULL;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001500}
1501
1502static void
1503weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1504 xcb_client_message_event_t *client_message)
1505{
1506 struct weston_wm *wm = window->wm;
1507 struct wl_resource *resource;
1508
1509 if (window->surface_id != 0) {
1510 wm_log("already have surface id for window %d\n", window->id);
1511 return;
1512 }
1513
1514 /* Xwayland will send the wayland requests to create the
1515 * wl_surface before sending this client message. Even so, we
1516 * can end up handling the X event before the wayland requests
1517 * and thus when we try to look up the surface ID, the surface
1518 * hasn't been created yet. In that case put the window on
1519 * the unpaired window list and continue when the surface gets
1520 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001521 uint32_t id = client_message->data.data32[0];
1522 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001523 if (resource) {
1524 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001525 xserver_map_shell_surface(window,
1526 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001527 }
1528 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001529 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001530 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001531 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001532}
1533
1534static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001535weston_wm_handle_client_message(struct weston_wm *wm,
1536 xcb_generic_event_t *event)
1537{
1538 xcb_client_message_event_t *client_message =
1539 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001540 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001541
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001542 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1543 get_atom_name(wm->conn, client_message->type),
1544 client_message->data.data32[0],
1545 client_message->data.data32[1],
1546 client_message->data.data32[2],
1547 client_message->data.data32[3],
1548 client_message->data.data32[4],
1549 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001550
Jason Ekstrand0250a742014-07-24 13:17:47 -07001551 /* The window may get created and destroyed before we actually
1552 * handle the message. If it doesn't exist, bail.
1553 */
Derek Foreman49372142015-04-09 10:51:22 -05001554 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001555 return;
1556
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001557 if (client_message->type == wm->atom.net_wm_moveresize)
1558 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001559 else if (client_message->type == wm->atom.net_wm_state)
1560 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001561 else if (client_message->type == wm->atom.wl_surface_id)
1562 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001563}
1564
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001565enum cursor_type {
1566 XWM_CURSOR_TOP,
1567 XWM_CURSOR_BOTTOM,
1568 XWM_CURSOR_LEFT,
1569 XWM_CURSOR_RIGHT,
1570 XWM_CURSOR_TOP_LEFT,
1571 XWM_CURSOR_TOP_RIGHT,
1572 XWM_CURSOR_BOTTOM_LEFT,
1573 XWM_CURSOR_BOTTOM_RIGHT,
1574 XWM_CURSOR_LEFT_PTR,
1575};
1576
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001577/*
1578 * The following correspondences between file names and cursors was copied
1579 * from: https://bugs.kde.org/attachment.cgi?id=67313
1580 */
1581
1582static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001583 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001584 "sw-resize",
1585 "size_bdiag"
1586};
1587
1588static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001589 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001590 "se-resize",
1591 "size_fdiag"
1592};
1593
1594static const char *bottom_sides[] = {
1595 "bottom_side",
1596 "s-resize",
1597 "size_ver"
1598};
1599
1600static const char *left_ptrs[] = {
1601 "left_ptr",
1602 "default",
1603 "top_left_arrow",
1604 "left-arrow"
1605};
1606
1607static const char *left_sides[] = {
1608 "left_side",
1609 "w-resize",
1610 "size_hor"
1611};
1612
1613static const char *right_sides[] = {
1614 "right_side",
1615 "e-resize",
1616 "size_hor"
1617};
1618
1619static const char *top_left_corners[] = {
1620 "top_left_corner",
1621 "nw-resize",
1622 "size_fdiag"
1623};
1624
1625static const char *top_right_corners[] = {
1626 "top_right_corner",
1627 "ne-resize",
1628 "size_bdiag"
1629};
1630
1631static const char *top_sides[] = {
1632 "top_side",
1633 "n-resize",
1634 "size_ver"
1635};
1636
1637struct cursor_alternatives {
1638 const char **names;
1639 size_t count;
1640};
1641
1642static const struct cursor_alternatives cursors[] = {
1643 {top_sides, ARRAY_LENGTH(top_sides)},
1644 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1645 {left_sides, ARRAY_LENGTH(left_sides)},
1646 {right_sides, ARRAY_LENGTH(right_sides)},
1647 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1648 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1649 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1650 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1651 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001652};
1653
1654static void
1655weston_wm_create_cursors(struct weston_wm *wm)
1656{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001657 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001658 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001659 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001660
1661 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1662 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001663 for (j = 0; j < cursors[i].count; j++) {
1664 name = cursors[i].names[j];
1665 wm->cursors[i] =
1666 xcb_cursor_library_load_cursor(wm, name);
1667 if (wm->cursors[i] != (xcb_cursor_t)-1)
1668 break;
1669 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001670 }
1671
1672 wm->last_cursor = -1;
1673}
1674
1675static void
1676weston_wm_destroy_cursors(struct weston_wm *wm)
1677{
1678 uint8_t i;
1679
1680 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1681 xcb_free_cursor(wm->conn, wm->cursors[i]);
1682
1683 free(wm->cursors);
1684}
1685
1686static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001687get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001688{
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001689 switch (location) {
1690 case THEME_LOCATION_RESIZING_TOP:
1691 return XWM_CURSOR_TOP;
1692 case THEME_LOCATION_RESIZING_BOTTOM:
1693 return XWM_CURSOR_BOTTOM;
1694 case THEME_LOCATION_RESIZING_LEFT:
1695 return XWM_CURSOR_LEFT;
1696 case THEME_LOCATION_RESIZING_RIGHT:
1697 return XWM_CURSOR_RIGHT;
1698 case THEME_LOCATION_RESIZING_TOP_LEFT:
1699 return XWM_CURSOR_TOP_LEFT;
1700 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1701 return XWM_CURSOR_TOP_RIGHT;
1702 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1703 return XWM_CURSOR_BOTTOM_LEFT;
1704 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1705 return XWM_CURSOR_BOTTOM_RIGHT;
1706 case THEME_LOCATION_EXTERIOR:
1707 case THEME_LOCATION_TITLEBAR:
1708 default:
1709 return XWM_CURSOR_LEFT_PTR;
1710 }
1711}
1712
1713static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001714weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1715 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001716{
1717 uint32_t cursor_value_list;
1718
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001719 if (wm->last_cursor == cursor)
1720 return;
1721
1722 wm->last_cursor = cursor;
1723
1724 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001725 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001726 XCB_CW_CURSOR, &cursor_value_list);
1727 xcb_flush(wm->conn);
1728}
1729
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001730static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001731weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001732{
1733 xcb_client_message_event_t client_message;
1734
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001735 if (window->delete_window) {
1736 client_message.response_type = XCB_CLIENT_MESSAGE;
1737 client_message.format = 32;
1738 client_message.window = window->id;
1739 client_message.type = window->wm->atom.wm_protocols;
1740 client_message.data.data32[0] =
1741 window->wm->atom.wm_delete_window;
1742 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001743
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001744 xcb_send_event(window->wm->conn, 0, window->id,
1745 XCB_EVENT_MASK_NO_EVENT,
1746 (char *) &client_message);
1747 } else {
1748 xcb_kill_client(window->wm->conn, window->id);
1749 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001750}
1751
1752static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001753weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1754{
1755 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
Quentin Glidic955cec02016-08-12 10:41:35 +02001756 const struct weston_desktop_xwayland_interface *xwayland_interface =
1757 wm->server->compositor->xwayland_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001758 struct weston_seat *seat;
Derek Foremane4d6c832015-08-05 14:48:11 -05001759 struct weston_pointer *pointer;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001760 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001761 enum theme_location location;
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001762 enum wl_pointer_button_state button_state;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001763 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001764
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001765 wm_log("XCB_BUTTON_%s (detail %d)\n",
1766 button->response_type == XCB_BUTTON_PRESS ?
1767 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001768
Derek Foreman49372142015-04-09 10:51:22 -05001769 if (!wm_lookup_window(wm, button->event, &window) ||
1770 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001771 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001772
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001773 if (button->detail != 1 && button->detail != 2)
1774 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001775
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001776 seat = weston_wm_pick_seat_for_window(window);
Derek Foremane4d6c832015-08-05 14:48:11 -05001777 pointer = weston_seat_get_pointer(seat);
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001778
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001779 button_state = button->response_type == XCB_BUTTON_PRESS ?
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001780 WL_POINTER_BUTTON_STATE_PRESSED :
1781 WL_POINTER_BUTTON_STATE_RELEASED;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001782 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1783
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001784 /* Make sure we're looking at the right location. The frame
1785 * could have received a motion event from a pointer from a
1786 * different wl_seat, but under X it looks like our core
1787 * pointer moved. Move the frame pointer to the button press
1788 * location before deciding what to do. */
1789 location = frame_pointer_motion(window->frame, NULL,
1790 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001791 location = frame_pointer_button(window->frame, NULL,
1792 button_id, button_state);
1793 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1794 weston_wm_window_schedule_repaint(window);
1795
1796 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001797 if (pointer)
Quentin Glidic955cec02016-08-12 10:41:35 +02001798 xwayland_interface->move(window->shsurf, pointer);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001799 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1800 }
1801
1802 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001803 if (pointer)
Quentin Glidic955cec02016-08-12 10:41:35 +02001804 xwayland_interface->resize(window->shsurf, pointer, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001805 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1806 }
1807
1808 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001809 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001810 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001811 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001812
1813 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1814 window->maximized_horz = !window->maximized_horz;
1815 window->maximized_vert = !window->maximized_vert;
1816 if (weston_wm_window_is_maximized(window)) {
1817 window->saved_width = window->width;
1818 window->saved_height = window->height;
Quentin Glidic955cec02016-08-12 10:41:35 +02001819 xwayland_interface->set_maximized(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001820 } else {
1821 weston_wm_window_set_toplevel(window);
1822 }
1823 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1824 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001825}
1826
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001827static void
1828weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1829{
1830 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1831 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001832 enum theme_location location;
1833 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001834
Derek Foreman49372142015-04-09 10:51:22 -05001835 if (!wm_lookup_window(wm, motion->event, &window) ||
1836 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001837 return;
1838
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001839 location = frame_pointer_motion(window->frame, NULL,
1840 motion->event_x, motion->event_y);
1841 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1842 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001843
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001844 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001845 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001846}
1847
1848static void
1849weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1850{
1851 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1852 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001853 enum theme_location location;
1854 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001855
Derek Foreman49372142015-04-09 10:51:22 -05001856 if (!wm_lookup_window(wm, enter->event, &window) ||
1857 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001858 return;
1859
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001860 location = frame_pointer_enter(window->frame, NULL,
1861 enter->event_x, enter->event_y);
1862 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1863 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001864
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001865 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001866 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001867}
1868
1869static void
1870weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1871{
1872 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1873 struct weston_wm_window *window;
1874
Derek Foreman49372142015-04-09 10:51:22 -05001875 if (!wm_lookup_window(wm, leave->event, &window) ||
1876 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001877 return;
1878
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001879 frame_pointer_leave(window->frame, NULL);
1880 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1881 weston_wm_window_schedule_repaint(window);
1882
Tiago Vignattic1903232012-07-16 12:15:37 -04001883 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001884}
1885
Giulio Camuffob18f7882015-03-29 14:20:23 +03001886static void
1887weston_wm_handle_focus_in(struct weston_wm *wm, xcb_generic_event_t *event)
1888{
1889 xcb_focus_in_event_t *focus = (xcb_focus_in_event_t *) event;
1890 /* Do not let X clients change the focus behind the compositor's
1891 * back. Reset the focus to the old one if it changed. */
1892 if (!wm->focus_window || focus->event != wm->focus_window->id)
1893 weston_wm_send_focus_window(wm, wm->focus_window);
1894}
1895
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001896static int
1897weston_wm_handle_event(int fd, uint32_t mask, void *data)
1898{
1899 struct weston_wm *wm = data;
1900 xcb_generic_event_t *event;
1901 int count = 0;
1902
1903 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1904 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001905 free(event);
1906 count++;
1907 continue;
1908 }
1909
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001910 if (weston_wm_handle_dnd_event(wm, event)) {
1911 free(event);
1912 count++;
1913 continue;
1914 }
1915
MoD31700122013-06-11 19:58:55 -05001916 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001917 case XCB_BUTTON_PRESS:
1918 case XCB_BUTTON_RELEASE:
1919 weston_wm_handle_button(wm, event);
1920 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001921 case XCB_ENTER_NOTIFY:
1922 weston_wm_handle_enter(wm, event);
1923 break;
1924 case XCB_LEAVE_NOTIFY:
1925 weston_wm_handle_leave(wm, event);
1926 break;
1927 case XCB_MOTION_NOTIFY:
1928 weston_wm_handle_motion(wm, event);
1929 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001930 case XCB_CREATE_NOTIFY:
1931 weston_wm_handle_create_notify(wm, event);
1932 break;
1933 case XCB_MAP_REQUEST:
1934 weston_wm_handle_map_request(wm, event);
1935 break;
1936 case XCB_MAP_NOTIFY:
1937 weston_wm_handle_map_notify(wm, event);
1938 break;
1939 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001940 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001941 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001942 case XCB_REPARENT_NOTIFY:
1943 weston_wm_handle_reparent_notify(wm, event);
1944 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001945 case XCB_CONFIGURE_REQUEST:
1946 weston_wm_handle_configure_request(wm, event);
1947 break;
1948 case XCB_CONFIGURE_NOTIFY:
1949 weston_wm_handle_configure_notify(wm, event);
1950 break;
1951 case XCB_DESTROY_NOTIFY:
1952 weston_wm_handle_destroy_notify(wm, event);
1953 break;
1954 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001955 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001956 break;
1957 case XCB_PROPERTY_NOTIFY:
1958 weston_wm_handle_property_notify(wm, event);
1959 break;
1960 case XCB_CLIENT_MESSAGE:
1961 weston_wm_handle_client_message(wm, event);
1962 break;
Giulio Camuffob18f7882015-03-29 14:20:23 +03001963 case XCB_FOCUS_IN:
1964 weston_wm_handle_focus_in(wm, event);
1965 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001966 }
1967
1968 free(event);
1969 count++;
1970 }
1971
Marek Chalupaa1f3f3c2015-08-12 09:55:12 +02001972 if (count != 0)
1973 xcb_flush(wm->conn);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001974
1975 return count;
1976}
1977
1978static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02001979weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window) {
1980 xcb_change_property(wm->conn, XCB_PROP_MODE_REPLACE,
1981 wm->screen->root, wm->atom.net_active_window,
1982 wm->atom.window, 32, 1, &window);
1983}
1984
1985static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001986weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1987{
1988 xcb_depth_iterator_t d_iter;
1989 xcb_visualtype_iterator_t vt_iter;
1990 xcb_visualtype_t *visualtype;
1991
1992 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1993 visualtype = NULL;
1994 while (d_iter.rem > 0) {
1995 if (d_iter.data->depth == 32) {
1996 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1997 visualtype = vt_iter.data;
1998 break;
1999 }
2000
2001 xcb_depth_next(&d_iter);
2002 }
2003
2004 if (visualtype == NULL) {
2005 weston_log("no 32 bit visualtype\n");
2006 return;
2007 }
2008
2009 wm->visual_id = visualtype->visual_id;
2010 wm->colormap = xcb_generate_id(wm->conn);
2011 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
2012 wm->colormap, wm->screen->root, wm->visual_id);
2013}
2014
2015static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002016weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002017{
2018
2019#define F(field) offsetof(struct weston_wm, field)
2020
2021 static const struct { const char *name; int offset; } atoms[] = {
2022 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002023 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002024 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
2025 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04002026 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002027 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002028 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002029 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002030 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002031 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002032 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2033 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002034 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2035 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002036 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2037 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2038 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02002039 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002040 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2041
2042 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2043 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2044 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2045 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2046 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2047 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2048 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002049 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2050 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002051 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2052 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2053 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2054 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2055 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2056
2057 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2058 { "_NET_SUPPORTING_WM_CHECK",
2059 F(atom.net_supporting_wm_check) },
2060 { "_NET_SUPPORTED", F(atom.net_supported) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002061 { "_NET_ACTIVE_WINDOW", F(atom.net_active_window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002062 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2063 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002064 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002065 { "TARGETS", F(atom.targets) },
2066 { "UTF8_STRING", F(atom.utf8_string) },
2067 { "_WL_SELECTION", F(atom.wl_selection) },
2068 { "INCR", F(atom.incr) },
2069 { "TIMESTAMP", F(atom.timestamp) },
2070 { "MULTIPLE", F(atom.multiple) },
2071 { "UTF8_STRING" , F(atom.utf8_string) },
2072 { "COMPOUND_TEXT", F(atom.compound_text) },
2073 { "TEXT", F(atom.text) },
2074 { "STRING", F(atom.string) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002075 { "WINDOW", F(atom.window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002076 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2077 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002078 { "XdndSelection", F(atom.xdnd_selection) },
2079 { "XdndAware", F(atom.xdnd_aware) },
2080 { "XdndEnter", F(atom.xdnd_enter) },
2081 { "XdndLeave", F(atom.xdnd_leave) },
2082 { "XdndDrop", F(atom.xdnd_drop) },
2083 { "XdndStatus", F(atom.xdnd_status) },
2084 { "XdndFinished", F(atom.xdnd_finished) },
2085 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002086 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2087 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002088 };
2089#undef F
2090
2091 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2092 xcb_xfixes_query_version_reply_t *xfixes_reply;
2093 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2094 xcb_intern_atom_reply_t *reply;
2095 xcb_render_query_pict_formats_reply_t *formats_reply;
2096 xcb_render_query_pict_formats_cookie_t formats_cookie;
2097 xcb_render_pictforminfo_t *formats;
2098 uint32_t i;
2099
2100 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002101 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002102
2103 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2104
2105 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2106 cookies[i] = xcb_intern_atom (wm->conn, 0,
2107 strlen(atoms[i].name),
2108 atoms[i].name);
2109
2110 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2111 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2112 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2113 free(reply);
2114 }
2115
2116 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2117 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002118 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002119
2120 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2121 XCB_XFIXES_MAJOR_VERSION,
2122 XCB_XFIXES_MINOR_VERSION);
2123 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2124 xfixes_cookie, NULL);
2125
Martin Minarik6d118362012-06-07 18:01:59 +02002126 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002127 xfixes_reply->major_version, xfixes_reply->minor_version);
2128
2129 free(xfixes_reply);
2130
2131 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2132 formats_cookie, 0);
2133 if (formats_reply == NULL)
2134 return;
2135
2136 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002137 for (i = 0; i < formats_reply->num_formats; i++) {
2138 if (formats[i].direct.red_mask != 0xff &&
2139 formats[i].direct.red_shift != 16)
2140 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002141 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2142 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002143 wm->format_rgb = formats[i];
2144 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2145 formats[i].depth == 32 &&
2146 formats[i].direct.alpha_mask == 0xff &&
2147 formats[i].direct.alpha_shift == 24)
2148 wm->format_rgba = formats[i];
2149 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002150
2151 free(formats_reply);
2152}
2153
2154static void
2155weston_wm_create_wm_window(struct weston_wm *wm)
2156{
2157 static const char name[] = "Weston WM";
2158
2159 wm->wm_window = xcb_generate_id(wm->conn);
2160 xcb_create_window(wm->conn,
2161 XCB_COPY_FROM_PARENT,
2162 wm->wm_window,
2163 wm->screen->root,
2164 0, 0,
2165 10, 10,
2166 0,
2167 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2168 wm->screen->root_visual,
2169 0, NULL);
2170
2171 xcb_change_property(wm->conn,
2172 XCB_PROP_MODE_REPLACE,
2173 wm->wm_window,
2174 wm->atom.net_supporting_wm_check,
2175 XCB_ATOM_WINDOW,
2176 32, /* format */
2177 1, &wm->wm_window);
2178
2179 xcb_change_property(wm->conn,
2180 XCB_PROP_MODE_REPLACE,
2181 wm->wm_window,
2182 wm->atom.net_wm_name,
2183 wm->atom.utf8_string,
2184 8, /* format */
2185 strlen(name), name);
2186
2187 xcb_change_property(wm->conn,
2188 XCB_PROP_MODE_REPLACE,
2189 wm->screen->root,
2190 wm->atom.net_supporting_wm_check,
2191 XCB_ATOM_WINDOW,
2192 32, /* format */
2193 1, &wm->wm_window);
2194
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002195 /* Claim the WM_S0 selection even though we don't suport
2196 * the --replace functionality. */
2197 xcb_set_selection_owner(wm->conn,
2198 wm->wm_window,
2199 wm->atom.wm_s0,
2200 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002201
2202 xcb_set_selection_owner(wm->conn,
2203 wm->wm_window,
2204 wm->atom.net_wm_cm_s0,
2205 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002206}
2207
2208struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002209weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002210{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002211 struct weston_wm *wm;
2212 struct wl_event_loop *loop;
2213 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002214 uint32_t values[1];
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002215 xcb_atom_t supported[6];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002216
Peter Huttererf3d62272013-08-08 11:57:05 +10002217 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002218 if (wm == NULL)
2219 return NULL;
2220
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002221 wm->server = wxs;
2222 wm->window_hash = hash_table_create();
2223 if (wm->window_hash == NULL) {
2224 free(wm);
2225 return NULL;
2226 }
2227
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002228 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002229 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002230 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002231 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002232 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002233 hash_table_destroy(wm->window_hash);
2234 free(wm);
2235 return NULL;
2236 }
2237
2238 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2239 wm->screen = s.data;
2240
2241 loop = wl_display_get_event_loop(wxs->wl_display);
2242 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002243 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002244 WL_EVENT_READABLE,
2245 weston_wm_handle_event, wm);
2246 wl_event_source_check(wm->source);
2247
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002248 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002249 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002250
2251 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002252 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2253 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2254 XCB_EVENT_MASK_PROPERTY_CHANGE;
2255 xcb_change_window_attributes(wm->conn, wm->screen->root,
2256 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002257
2258 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2259 XCB_COMPOSITE_REDIRECT_MANUAL);
2260
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002261 wm->theme = theme_create();
2262
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002263 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002264 supported[1] = wm->atom.net_wm_state;
2265 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002266 supported[3] = wm->atom.net_wm_state_maximized_vert;
2267 supported[4] = wm->atom.net_wm_state_maximized_horz;
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002268 supported[5] = wm->atom.net_active_window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002269 xcb_change_property(wm->conn,
2270 XCB_PROP_MODE_REPLACE,
2271 wm->screen->root,
2272 wm->atom.net_supported,
2273 XCB_ATOM_ATOM,
2274 32, /* format */
2275 ARRAY_LENGTH(supported), supported);
2276
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002277 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
2278
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002279 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002280
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002281 weston_wm_dnd_init(wm);
2282
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002283 xcb_flush(wm->conn);
2284
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002285 wm->create_surface_listener.notify = weston_wm_create_surface;
2286 wl_signal_add(&wxs->compositor->create_surface_signal,
2287 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002288 wm->activate_listener.notify = weston_wm_window_activate;
2289 wl_signal_add(&wxs->compositor->activate_signal,
2290 &wm->activate_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002291 wm->kill_listener.notify = weston_wm_kill_client;
2292 wl_signal_add(&wxs->compositor->kill_signal,
2293 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002294 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002295
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002296 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002297 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002298
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002299 /* Create wm window and take WM_S0 selection last, which
2300 * signals to Xwayland that we're done with setup. */
2301 weston_wm_create_wm_window(wm);
2302
2303 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002304
2305 return wm;
2306}
2307
2308void
2309weston_wm_destroy(struct weston_wm *wm)
2310{
2311 /* FIXME: Free windows in hash. */
2312 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002313 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002314 xcb_disconnect(wm->conn);
2315 wl_event_source_remove(wm->source);
2316 wl_list_remove(&wm->selection_listener.link);
2317 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002318 wl_list_remove(&wm->kill_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002319 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002320
2321 free(wm);
2322}
2323
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002324static struct weston_wm_window *
2325get_wm_window(struct weston_surface *surface)
2326{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002327 struct wl_listener *listener;
2328
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002329 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002330 if (listener)
2331 return container_of(listener, struct weston_wm_window,
2332 surface_destroy_listener);
2333
2334 return NULL;
2335}
2336
Quentin Glidic955cec02016-08-12 10:41:35 +02002337static bool
2338is_wm_window(struct weston_surface *surface)
2339{
2340 return get_wm_window(surface) != NULL;
2341}
2342
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002343static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002344weston_wm_window_configure(void *data)
2345{
2346 struct weston_wm_window *window = data;
2347 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002348 uint32_t values[4];
2349 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002350
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002351 weston_wm_window_get_child_position(window, &x, &y);
2352 values[0] = x;
2353 values[1] = y;
2354 values[2] = window->width;
2355 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002356 xcb_configure_window(wm->conn,
2357 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002358 XCB_CONFIG_WINDOW_X |
2359 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002360 XCB_CONFIG_WINDOW_WIDTH |
2361 XCB_CONFIG_WINDOW_HEIGHT,
2362 values);
2363
2364 weston_wm_window_get_frame_size(window, &width, &height);
2365 values[0] = width;
2366 values[1] = height;
2367 xcb_configure_window(wm->conn,
2368 window->frame_id,
2369 XCB_CONFIG_WINDOW_WIDTH |
2370 XCB_CONFIG_WINDOW_HEIGHT,
2371 values);
2372
2373 window->configure_source = NULL;
2374
2375 weston_wm_window_schedule_repaint(window);
2376}
2377
2378static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002379send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002380{
2381 struct weston_wm_window *window = get_wm_window(surface);
2382 struct weston_wm *wm = window->wm;
2383 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002384 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002385
Marek Chalupae9fe4672014-10-29 13:44:44 +01002386 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002387 hborder = 2 * t->width;
2388 vborder = t->titlebar_height + t->width;
2389 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002390 hborder = 0;
2391 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002392 }
2393
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002394 if (width > hborder)
2395 window->width = width - hborder;
2396 else
2397 window->width = 1;
2398
2399 if (height > vborder)
2400 window->height = height - vborder;
2401 else
2402 window->height = 1;
2403
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002404 if (window->frame)
2405 frame_resize_inside(window->frame, window->width, window->height);
2406
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002407 if (window->configure_source)
2408 return;
2409
2410 window->configure_source =
2411 wl_event_loop_add_idle(wm->server->loop,
2412 weston_wm_window_configure, window);
2413}
2414
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002415static void
2416send_position(struct weston_surface *surface, int32_t x, int32_t y)
2417{
2418 struct weston_wm_window *window = get_wm_window(surface);
2419 struct weston_wm *wm;
2420 uint32_t mask, values[2];
2421
2422 if (!window || !window->wm)
2423 return;
2424
2425 wm = window->wm;
2426 /* We use pos_dirty to tell whether a configure message is in flight.
2427 * This is needed in case we send two configure events in a very
2428 * short time, since window->x/y is set in after a roundtrip, hence
2429 * we cannot just check if the current x and y are different. */
2430 if (window->x != x || window->y != y || window->pos_dirty) {
2431 window->pos_dirty = true;
2432 values[0] = x;
2433 values[1] = y;
2434 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
2435
2436 xcb_configure_window(wm->conn, window->frame_id, mask, values);
2437 xcb_flush(wm->conn);
2438 }
2439}
2440
Quentin Glidic955cec02016-08-12 10:41:35 +02002441static const struct weston_xwayland_client_interface shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002442 send_configure,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002443};
2444
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002445static int
2446legacy_fullscreen(struct weston_wm *wm,
2447 struct weston_wm_window *window,
2448 struct weston_output **output_ret)
2449{
2450 struct weston_compositor *compositor = wm->server->compositor;
2451 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002452 uint32_t minmax = PMinSize | PMaxSize;
2453 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002454
2455 /* Heuristics for detecting legacy fullscreen windows... */
2456
2457 wl_list_for_each(output, &compositor->output_list, link) {
2458 if (output->x == window->x &&
2459 output->y == window->y &&
2460 output->width == window->width &&
2461 output->height == window->height &&
2462 window->override_redirect) {
2463 *output_ret = output;
2464 return 1;
2465 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002466
2467 matching_size = 0;
2468 if ((window->size_hints.flags & (USSize |PSize)) &&
2469 window->size_hints.width == output->width &&
2470 window->size_hints.height == output->height)
2471 matching_size = 1;
2472 if ((window->size_hints.flags & minmax) == minmax &&
2473 window->size_hints.min_width == output->width &&
2474 window->size_hints.min_height == output->height &&
2475 window->size_hints.max_width == output->width &&
2476 window->size_hints.max_height == output->height)
2477 matching_size = 1;
2478
2479 if (matching_size && !window->decorate &&
2480 (window->size_hints.flags & (USPosition | PPosition)) &&
2481 window->size_hints.x == output->x &&
2482 window->size_hints.y == output->y) {
2483 *output_ret = output;
2484 return 1;
2485 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002486 }
2487
2488 return 0;
2489}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002490
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002491static bool
2492weston_wm_window_type_inactive(struct weston_wm_window *window)
2493{
2494 struct weston_wm *wm = window->wm;
2495
2496 return window->type == wm->atom.net_wm_window_type_tooltip ||
2497 window->type == wm->atom.net_wm_window_type_dropdown ||
2498 window->type == wm->atom.net_wm_window_type_dnd ||
2499 window->type == wm->atom.net_wm_window_type_combo ||
Giulio Camuffo84787ea2015-04-29 19:00:48 +03002500 window->type == wm->atom.net_wm_window_type_popup ||
2501 window->type == wm->atom.net_wm_window_type_utility;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002502}
2503
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002504static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002505xserver_map_shell_surface(struct weston_wm_window *window,
2506 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002507{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002508 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +02002509 struct weston_desktop_xwayland *xwayland =
2510 wm->server->compositor->xwayland;
2511 const struct weston_desktop_xwayland_interface *xwayland_interface =
2512 wm->server->compositor->xwayland_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002513 struct weston_output *output;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002514 struct weston_wm_window *parent;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002515
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002516 weston_wm_window_read_properties(window);
2517
2518 /* A weston_wm_window may have many different surfaces assigned
2519 * throughout its life, so we must make sure to remove the listener
2520 * from the old surface signal list. */
2521 if (window->surface)
2522 wl_list_remove(&window->surface_destroy_listener.link);
2523
2524 window->surface = surface;
2525 window->surface_destroy_listener.notify = surface_destroy;
2526 wl_signal_add(&window->surface->destroy_signal,
2527 &window->surface_destroy_listener);
2528
2529 weston_wm_window_schedule_repaint(window);
2530
Quentin Glidic955cec02016-08-12 10:41:35 +02002531 if (!xwayland_interface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002532 return;
2533
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002534 if (window->surface->committed) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03002535 weston_log("warning, unexpected in %s: "
2536 "surface's configure hook is already set.\n",
2537 __func__);
2538 return;
2539 }
2540
Murray Calavera883ac022015-06-06 13:02:22 +00002541 window->shsurf =
Quentin Glidic955cec02016-08-12 10:41:35 +02002542 xwayland_interface->create_surface(xwayland,
2543 window->surface,
2544 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002545
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002546 if (window->name)
Quentin Glidic955cec02016-08-12 10:41:35 +02002547 xwayland_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002548 if (window->pid > 0)
Quentin Glidic955cec02016-08-12 10:41:35 +02002549 xwayland_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002550
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002551 if (window->fullscreen) {
2552 window->saved_width = window->width;
2553 window->saved_height = window->height;
Quentin Glidic955cec02016-08-12 10:41:35 +02002554 xwayland_interface->set_fullscreen(window->shsurf, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002555 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002556 } else if (legacy_fullscreen(wm, window, &output)) {
2557 window->fullscreen = 1;
Quentin Glidic955cec02016-08-12 10:41:35 +02002558 xwayland_interface->set_fullscreen(window->shsurf, output);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002559 } else if (window->override_redirect) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002560 xwayland_interface->set_xwayland(window->shsurf,
2561 window->x, window->y);
Axel Davye4450f92014-01-12 15:06:05 +01002562 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002563 parent = window->transient_for;
Quentin Glidic955cec02016-08-12 10:41:35 +02002564 if (weston_wm_window_type_inactive(window)) {
2565 xwayland_interface->set_transient(window->shsurf,
2566 parent->surface,
2567 window->x - parent->x,
2568 window->y - parent->y);
2569 } else {
2570 xwayland_interface->set_toplevel(window->shsurf);
2571 xwayland_interface->set_parent(window->shsurf,
2572 parent->surface);
2573 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002574 } else if (weston_wm_window_is_maximized(window)) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002575 xwayland_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002576 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002577 if (weston_wm_window_type_inactive(window)) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002578 xwayland_interface->set_xwayland(window->shsurf,
2579 window->x,
2580 window->y);
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002581 } else {
Quentin Glidic955cec02016-08-12 10:41:35 +02002582 xwayland_interface->set_toplevel(window->shsurf);
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002583 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002584 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002585}
Quentin Glidic955cec02016-08-12 10:41:35 +02002586
2587const struct weston_xwayland_surface_api surface_api = {
2588 is_wm_window,
2589 send_position,
2590};