blob: 3e8c4c7c59adda6115811a24c5ad0a6afdc2efcd [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>
Pekka Paalanen882aff02016-11-16 14:15:18 +020038#include <limits.h>
Pekka Paalanen505237e2016-12-01 15:41:11 +020039#include <assert.h>
Tiago Vignatti90fada42012-07-16 12:02:08 -040040#include <X11/Xcursor/Xcursor.h>
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -050041#include <linux/input.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040042
Daniel Stone67fe3db2016-10-31 14:51:18 +000043#include "compositor.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040044#include "xwayland.h"
Quentin Glidic955cec02016-08-12 10:41:35 +020045#include "xwayland-internal-interface.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040046
Kristian Høgsberg2ba10df2013-12-03 16:38:15 -080047#include "cairo-util.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040048#include "hash.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070049#include "shared/helpers.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040050
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070051struct wm_size_hints {
Bryce Harringtone00554b2015-06-12 10:17:39 -070052 uint32_t flags;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070053 int32_t x, y;
54 int32_t width, height; /* should set so old wm's don't mess up */
55 int32_t min_width, min_height;
56 int32_t max_width, max_height;
Bryce Harringtone00554b2015-06-12 10:17:39 -070057 int32_t width_inc, height_inc;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070058 struct {
59 int32_t x;
60 int32_t y;
61 } min_aspect, max_aspect;
62 int32_t base_width, base_height;
63 int32_t win_gravity;
64};
65
66#define USPosition (1L << 0)
67#define USSize (1L << 1)
68#define PPosition (1L << 2)
69#define PSize (1L << 3)
70#define PMinSize (1L << 4)
71#define PMaxSize (1L << 5)
72#define PResizeInc (1L << 6)
73#define PAspect (1L << 7)
74#define PBaseSize (1L << 8)
75#define PWinGravity (1L << 9)
76
Kristian Høgsberg380deee2012-05-21 17:12:41 -040077struct motif_wm_hints {
78 uint32_t flags;
79 uint32_t functions;
80 uint32_t decorations;
81 int32_t input_mode;
82 uint32_t status;
83};
84
85#define MWM_HINTS_FUNCTIONS (1L << 0)
86#define MWM_HINTS_DECORATIONS (1L << 1)
87#define MWM_HINTS_INPUT_MODE (1L << 2)
88#define MWM_HINTS_STATUS (1L << 3)
89
90#define MWM_FUNC_ALL (1L << 0)
91#define MWM_FUNC_RESIZE (1L << 1)
92#define MWM_FUNC_MOVE (1L << 2)
93#define MWM_FUNC_MINIMIZE (1L << 3)
94#define MWM_FUNC_MAXIMIZE (1L << 4)
95#define MWM_FUNC_CLOSE (1L << 5)
96
97#define MWM_DECOR_ALL (1L << 0)
98#define MWM_DECOR_BORDER (1L << 1)
99#define MWM_DECOR_RESIZEH (1L << 2)
100#define MWM_DECOR_TITLE (1L << 3)
101#define MWM_DECOR_MENU (1L << 4)
102#define MWM_DECOR_MINIMIZE (1L << 5)
103#define MWM_DECOR_MAXIMIZE (1L << 6)
104
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700105#define MWM_DECOR_EVERYTHING \
106 (MWM_DECOR_BORDER | MWM_DECOR_RESIZEH | MWM_DECOR_TITLE | \
107 MWM_DECOR_MENU | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE)
108
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400109#define MWM_INPUT_MODELESS 0
110#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
111#define MWM_INPUT_SYSTEM_MODAL 2
112#define MWM_INPUT_FULL_APPLICATION_MODAL 3
113#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
114
115#define MWM_TEAROFF_WINDOW (1L<<0)
116
117#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
118#define _NET_WM_MOVERESIZE_SIZE_TOP 1
119#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
120#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
121#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
122#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
123#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
124#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
125#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
126#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
127#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
128#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
129
Pekka Paalanen505237e2016-12-01 15:41:11 +0200130struct weston_output_weak_ref {
131 struct weston_output *output;
132 struct wl_listener destroy_listener;
133};
134
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400135struct weston_wm_window {
136 struct weston_wm *wm;
137 xcb_window_t id;
138 xcb_window_t frame_id;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500139 struct frame *frame;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400140 cairo_surface_t *cairo_surface;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700141 uint32_t surface_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400142 struct weston_surface *surface;
Quentin Glidic955cec02016-08-12 10:41:35 +0200143 struct weston_desktop_xwayland_surface *shsurf;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400144 struct wl_listener surface_destroy_listener;
145 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400146 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400147 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300148 int pid;
149 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400150 char *class;
151 char *name;
152 struct weston_wm_window *transient_for;
153 uint32_t protocols;
154 xcb_atom_t type;
155 int width, height;
Pekka Paalanen882aff02016-11-16 14:15:18 +0200156 int x;
157 int y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200158 bool pos_dirty;
Pekka Paalanen882aff02016-11-16 14:15:18 +0200159 int map_request_x;
160 int map_request_y;
Pekka Paalanen505237e2016-12-01 15:41:11 +0200161 struct weston_output_weak_ref legacy_fullscreen_output;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500162 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400163 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300164 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500165 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500166 int has_alpha;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700167 int delete_window;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200168 int maximized_vert;
169 int maximized_horz;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700170 struct wm_size_hints size_hints;
171 struct motif_wm_hints motif_hints;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700172 struct wl_list link;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400173};
174
175static struct weston_wm_window *
176get_wm_window(struct weston_surface *surface);
177
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400178static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200179weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window);
180
181static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400182weston_wm_window_schedule_repaint(struct weston_wm_window *window);
183
Pekka Paalanen505237e2016-12-01 15:41:11 +0200184static int
185legacy_fullscreen(struct weston_wm *wm,
186 struct weston_wm_window *window,
187 struct weston_output **output_ret);
188
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700189static void
190xserver_map_shell_surface(struct weston_wm_window *window,
191 struct weston_surface *surface);
192
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400193static int __attribute__ ((format (printf, 1, 2)))
194wm_log(const char *fmt, ...)
195{
196#ifdef WM_DEBUG
197 int l;
198 va_list argp;
199
200 va_start(argp, fmt);
201 l = weston_vlog(fmt, argp);
202 va_end(argp);
203
204 return l;
205#else
206 return 0;
207#endif
208}
209
210static int __attribute__ ((format (printf, 1, 2)))
211wm_log_continue(const char *fmt, ...)
212{
213#ifdef WM_DEBUG
214 int l;
215 va_list argp;
216
217 va_start(argp, fmt);
218 l = weston_vlog_continue(fmt, argp);
219 va_end(argp);
220
221 return l;
222#else
223 return 0;
224#endif
225}
226
Pekka Paalanen505237e2016-12-01 15:41:11 +0200227static void
228weston_output_weak_ref_init(struct weston_output_weak_ref *ref)
229{
230 ref->output = NULL;
231}
232
233static void
234weston_output_weak_ref_clear(struct weston_output_weak_ref *ref)
235{
236 if (!ref->output)
237 return;
238
239 wl_list_remove(&ref->destroy_listener.link);
240 ref->output = NULL;
241}
242
243static void
244weston_output_weak_ref_handle_destroy(struct wl_listener *listener, void *data)
245{
246 struct weston_output_weak_ref *ref;
247
248 ref = wl_container_of(listener, ref, destroy_listener);
249 assert(ref->output == data);
250
251 weston_output_weak_ref_clear(ref);
252}
253
254static void
255weston_output_weak_ref_set(struct weston_output_weak_ref *ref,
256 struct weston_output *output)
257{
258 weston_output_weak_ref_clear(ref);
259
260 if (!output)
261 return;
262
263 ref->destroy_listener.notify = weston_output_weak_ref_handle_destroy;
264 wl_signal_add(&output->destroy_signal, &ref->destroy_listener);
265 ref->output = output;
266}
267
Derek Foreman49372142015-04-09 10:51:22 -0500268static bool __attribute__ ((warn_unused_result))
269wm_lookup_window(struct weston_wm *wm, xcb_window_t hash,
270 struct weston_wm_window **window)
271{
272 *window = hash_table_lookup(wm->window_hash, hash);
273 if (*window)
274 return true;
275 return false;
276}
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400277
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400278const char *
279get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
280{
281 xcb_get_atom_name_cookie_t cookie;
282 xcb_get_atom_name_reply_t *reply;
283 xcb_generic_error_t *e;
284 static char buffer[64];
285
286 if (atom == XCB_ATOM_NONE)
287 return "None";
288
289 cookie = xcb_get_atom_name (c, atom);
290 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500291
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300292 if (reply) {
MoD55375b92013-06-11 19:59:42 -0500293 snprintf(buffer, sizeof buffer, "%.*s",
294 xcb_get_atom_name_name_length (reply),
295 xcb_get_atom_name_name (reply));
296 } else {
297 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
298 }
299
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400300 free(reply);
301
302 return buffer;
303}
304
Tiago Vignatti90fada42012-07-16 12:02:08 -0400305static xcb_cursor_t
306xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
307{
308 xcb_connection_t *c = wm->conn;
309 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
310 xcb_screen_t *screen = s.data;
311 xcb_gcontext_t gc;
312 xcb_pixmap_t pix;
313 xcb_render_picture_t pic;
314 xcb_cursor_t cursor;
315 int stride = img->width * 4;
316
317 pix = xcb_generate_id(c);
318 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
319
320 pic = xcb_generate_id(c);
321 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
322
323 gc = xcb_generate_id(c);
324 xcb_create_gc(c, gc, pix, 0, 0);
325
326 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
327 img->width, img->height, 0, 0, 0, 32,
328 stride * img->height, (uint8_t *) img->pixels);
329 xcb_free_gc(c, gc);
330
331 cursor = xcb_generate_id(c);
332 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
333
334 xcb_render_free_picture(c, pic);
335 xcb_free_pixmap(c, pix);
336
337 return cursor;
338}
339
340static xcb_cursor_t
341xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
342{
343 /* TODO: treat animated cursors as well */
344 if (images->nimage != 1)
345 return -1;
346
347 return xcb_cursor_image_load_cursor(wm, images->images[0]);
348}
349
350static xcb_cursor_t
351xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
352{
353 xcb_cursor_t cursor;
354 XcursorImages *images;
355 char *v = NULL;
356 int size = 0;
357
358 if (!file)
359 return 0;
360
361 v = getenv ("XCURSOR_SIZE");
362 if (v)
363 size = atoi(v);
364
365 if (!size)
366 size = 32;
367
368 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300369 if (!images)
370 return -1;
371
Tiago Vignatti90fada42012-07-16 12:02:08 -0400372 cursor = xcb_cursor_images_load_cursor (wm, images);
373 XcursorImagesDestroy (images);
374
375 return cursor;
376}
377
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400378void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400379dump_property(struct weston_wm *wm,
380 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400381{
382 int32_t *incr_value;
383 const char *text_value, *name;
384 xcb_atom_t *atom_value;
385 int width, len;
386 uint32_t i;
387
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400388 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400389 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400390 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400391 return;
392 }
393
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400394 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
395 get_atom_name(wm->conn, reply->type),
396 reply->format,
397 xcb_get_property_value_length(reply),
398 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400399
400 if (reply->type == wm->atom.incr) {
401 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400402 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400403 } else if (reply->type == wm->atom.utf8_string ||
404 reply->type == wm->atom.string) {
405 text_value = xcb_get_property_value(reply);
406 if (reply->value_len > 40)
407 len = 40;
408 else
409 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400410 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400411 } else if (reply->type == XCB_ATOM_ATOM) {
412 atom_value = xcb_get_property_value(reply);
413 for (i = 0; i < reply->value_len; i++) {
414 name = get_atom_name(wm->conn, atom_value[i]);
415 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400416 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400417 width = 4;
418 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400419 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400420 }
421
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400422 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400423 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400424 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400425 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400426 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400427 }
428}
429
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200430static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400431read_and_dump_property(struct weston_wm *wm,
432 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400433{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400434 xcb_get_property_reply_t *reply;
435 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400436
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400437 cookie = xcb_get_property(wm->conn, 0, window,
438 property, XCB_ATOM_ANY, 0, 2048);
439 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400440
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400441 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400442
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400443 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400444}
445
Pekka Paalanen20111d52016-12-01 17:07:12 +0200446/* We reuse some predefined, but otherwise useles atoms
447 * as local type placeholders that never touch the X11 server,
448 * to make weston_wm_window_read_properties() less exceptional.
449 */
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400450#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
451#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500452#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700453#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400454
455static void
456weston_wm_window_read_properties(struct weston_wm_window *window)
457{
458 struct weston_wm *wm = window->wm;
459
Pekka Paalanen20111d52016-12-01 17:07:12 +0200460#define F(field) (&window->field)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400461 const struct {
462 xcb_atom_t atom;
463 xcb_atom_t type;
Pekka Paalanen20111d52016-12-01 17:07:12 +0200464 void *ptr;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400465 } props[] = {
Pekka Paalanen20111d52016-12-01 17:07:12 +0200466 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
467 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
468 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
469 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, NULL },
470 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, NULL },
471 { wm->atom.net_wm_state, TYPE_NET_WM_STATE, NULL },
472 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
473 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
474 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
475 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, NULL },
476 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400477 };
478#undef F
479
480 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
481 xcb_get_property_reply_t *reply;
482 void *p;
483 uint32_t *xid;
484 xcb_atom_t *atom;
485 uint32_t i;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200486 char name[1024];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400487
488 if (!window->properties_dirty)
489 return;
490 window->properties_dirty = 0;
491
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400492 for (i = 0; i < ARRAY_LENGTH(props); i++)
493 cookie[i] = xcb_get_property(wm->conn,
494 0, /* delete */
495 window->id,
496 props[i].atom,
497 XCB_ATOM_ANY, 0, 2048);
498
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700499 window->decorate = window->override_redirect ? 0 : MWM_DECOR_EVERYTHING;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700500 window->size_hints.flags = 0;
501 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700502 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700503
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400504 for (i = 0; i < ARRAY_LENGTH(props); i++) {
505 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
506 if (!reply)
507 /* Bad window, typically */
508 continue;
509 if (reply->type == XCB_ATOM_NONE) {
510 /* No such property */
511 free(reply);
512 continue;
513 }
514
Pekka Paalanen20111d52016-12-01 17:07:12 +0200515 p = props[i].ptr;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400516
517 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300518 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400519 case XCB_ATOM_STRING:
520 /* FIXME: We're using this for both string and
521 utf8_string */
522 if (*(char **) p)
523 free(*(char **) p);
524
525 *(char **) p =
526 strndup(xcb_get_property_value(reply),
527 xcb_get_property_value_length(reply));
528 break;
529 case XCB_ATOM_WINDOW:
530 xid = xcb_get_property_value(reply);
Derek Foreman49372142015-04-09 10:51:22 -0500531 if (!wm_lookup_window(wm, *xid, p))
532 weston_log("XCB_ATOM_WINDOW contains window"
533 " id not found in hash table.\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400534 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300535 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400536 case XCB_ATOM_ATOM:
537 atom = xcb_get_property_value(reply);
538 *(xcb_atom_t *) p = *atom;
539 break;
540 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700541 atom = xcb_get_property_value(reply);
542 for (i = 0; i < reply->value_len; i++)
Derek Foremanb4deec62015-04-07 12:12:13 -0500543 if (atom[i] == wm->atom.wm_delete_window) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700544 window->delete_window = 1;
Derek Foremanb4deec62015-04-07 12:12:13 -0500545 break;
546 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400547 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700548 case TYPE_WM_NORMAL_HINTS:
549 memcpy(&window->size_hints,
550 xcb_get_property_value(reply),
551 sizeof window->size_hints);
552 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500553 case TYPE_NET_WM_STATE:
554 window->fullscreen = 0;
555 atom = xcb_get_property_value(reply);
Ryo Munakataf3744f52015-03-11 17:36:30 +0900556 for (i = 0; i < reply->value_len; i++) {
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500557 if (atom[i] == wm->atom.net_wm_state_fullscreen)
558 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200559 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
560 window->maximized_vert = 1;
561 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
562 window->maximized_horz = 1;
Ryo Munakataf3744f52015-03-11 17:36:30 +0900563 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500564 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400565 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700566 memcpy(&window->motif_hints,
567 xcb_get_property_value(reply),
568 sizeof window->motif_hints);
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700569 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) {
570 if (window->motif_hints.decorations & MWM_DECOR_ALL)
571 /* MWM_DECOR_ALL means all except the other values listed. */
572 window->decorate =
573 MWM_DECOR_EVERYTHING & (~window->motif_hints.decorations);
574 else
575 window->decorate =
576 window->motif_hints.decorations;
577 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400578 break;
579 default:
580 break;
581 }
582 free(reply);
583 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200584
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200585 if (window->pid > 0) {
586 gethostname(name, sizeof(name));
587 for (i = 0; i < sizeof(name); i++) {
588 if (name[i] == '\0')
589 break;
590 }
591 if (i == sizeof(name))
592 name[0] = '\0'; /* ignore stupid hostnames */
593
594 /* this is only one heuristic to guess the PID of a client is
595 * valid, assuming it's compliant with icccm and ewmh.
596 * Non-compliants and remote applications of course fail. */
597 if (!window->machine || strcmp(window->machine, name))
598 window->pid = 0;
599 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400600}
601
Pekka Paalanen20111d52016-12-01 17:07:12 +0200602#undef TYPE_WM_PROTOCOLS
603#undef TYPE_MOTIF_WM_HINTS
604#undef TYPE_NET_WM_STATE
605#undef TYPE_WM_NORMAL_HINTS
606
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400607static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400608weston_wm_window_get_frame_size(struct weston_wm_window *window,
609 int *width, int *height)
610{
611 struct theme *t = window->wm->theme;
612
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500613 if (window->fullscreen) {
614 *width = window->width;
615 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800616 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500617 *width = frame_width(window->frame);
618 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400619 } else {
620 *width = window->width + t->margin * 2;
621 *height = window->height + t->margin * 2;
622 }
623}
624
625static void
626weston_wm_window_get_child_position(struct weston_wm_window *window,
627 int *x, int *y)
628{
629 struct theme *t = window->wm->theme;
630
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500631 if (window->fullscreen) {
632 *x = 0;
633 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800634 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500635 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400636 } else {
637 *x = t->margin;
638 *y = t->margin;
639 }
640}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400641
642static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500643weston_wm_window_send_configure_notify(struct weston_wm_window *window)
644{
645 xcb_configure_notify_event_t configure_notify;
646 struct weston_wm *wm = window->wm;
647 int x, y;
648
649 weston_wm_window_get_child_position(window, &x, &y);
650 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
651 configure_notify.pad0 = 0;
652 configure_notify.event = window->id;
653 configure_notify.window = window->id;
654 configure_notify.above_sibling = XCB_WINDOW_NONE;
655 configure_notify.x = x;
656 configure_notify.y = y;
657 configure_notify.width = window->width;
658 configure_notify.height = window->height;
659 configure_notify.border_width = 0;
660 configure_notify.override_redirect = 0;
661 configure_notify.pad1 = 0;
662
Murray Calavera883ac022015-06-06 13:02:22 +0000663 xcb_send_event(wm->conn, 0, window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500664 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
665 (char *) &configure_notify);
666}
667
668static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400669weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
670{
Murray Calavera883ac022015-06-06 13:02:22 +0000671 xcb_configure_request_event_t *configure_request =
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400672 (xcb_configure_request_event_t *) event;
673 struct weston_wm_window *window;
674 uint32_t mask, values[16];
675 int x, y, width, height, i = 0;
676
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400677 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
678 configure_request->window,
679 configure_request->x, configure_request->y,
680 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400681
Derek Foreman49372142015-04-09 10:51:22 -0500682 if (!wm_lookup_window(wm, configure_request->window, &window))
683 return;
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400684
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500685 if (window->fullscreen) {
686 weston_wm_window_send_configure_notify(window);
687 return;
688 }
689
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400690 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
691 window->width = configure_request->width;
692 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
693 window->height = configure_request->height;
694
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500695 if (window->frame)
696 frame_resize_inside(window->frame, window->width, window->height);
697
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400698 weston_wm_window_get_child_position(window, &x, &y);
699 values[i++] = x;
700 values[i++] = y;
701 values[i++] = window->width;
702 values[i++] = window->height;
703 values[i++] = 0;
704 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
705 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
706 XCB_CONFIG_WINDOW_BORDER_WIDTH;
707 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
708 values[i++] = configure_request->sibling;
709 mask |= XCB_CONFIG_WINDOW_SIBLING;
710 }
711 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
712 values[i++] = configure_request->stack_mode;
713 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
714 }
715
716 xcb_configure_window(wm->conn, window->id, mask, values);
717
718 weston_wm_window_get_frame_size(window, &width, &height);
719 values[0] = width;
720 values[1] = height;
721 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
722 xcb_configure_window(wm->conn, window->frame_id, mask, values);
723
724 weston_wm_window_schedule_repaint(window);
725}
726
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400727static int
728our_resource(struct weston_wm *wm, uint32_t id)
729{
730 const xcb_setup_t *setup;
731
732 setup = xcb_get_setup(wm->conn);
733
734 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
735}
736
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400737static void
738weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
739{
Murray Calavera883ac022015-06-06 13:02:22 +0000740 xcb_configure_notify_event_t *configure_notify =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400741 (xcb_configure_notify_event_t *) event;
Ilia Bozhinov3e5303d2017-06-28 00:08:40 +0300742 const struct weston_desktop_xwayland_interface *xwayland_api =
743 wm->server->compositor->xwayland_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400744 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400745
Pekka Paalanen73428a82016-12-19 15:10:01 +0200746 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d%s\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400747 configure_notify->window,
748 configure_notify->x, configure_notify->y,
Pekka Paalanen73428a82016-12-19 15:10:01 +0200749 configure_notify->width, configure_notify->height,
750 configure_notify->override_redirect ? ", override" : "");
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300751
Derek Foreman49372142015-04-09 10:51:22 -0500752 if (!wm_lookup_window(wm, configure_notify->window, &window))
753 return;
754
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700755 window->x = configure_notify->x;
756 window->y = configure_notify->y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200757 window->pos_dirty = false;
758
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700759 if (window->override_redirect) {
760 window->width = configure_notify->width;
761 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500762 if (window->frame)
763 frame_resize_inside(window->frame,
764 window->width, window->height);
Ilia Bozhinov3e5303d2017-06-28 00:08:40 +0300765
766 /* We should check if shsurf has been created because sometimes
767 * there are races
768 * (configure_notify is sent before xserver_map_surface) */
769 if (window->shsurf)
770 xwayland_api->set_xwayland(window->shsurf,
771 window->x, window->y);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700772 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400773}
774
775static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300776weston_wm_kill_client(struct wl_listener *listener, void *data)
777{
778 struct weston_surface *surface = data;
779 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300780 if (!window)
781 return;
782
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200783 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300784 kill(window->pid, SIGKILL);
785}
786
787static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700788weston_wm_create_surface(struct wl_listener *listener, void *data)
789{
790 struct weston_surface *surface = data;
791 struct weston_wm *wm =
792 container_of(listener,
793 struct weston_wm, create_surface_listener);
794 struct weston_wm_window *window;
795
796 if (wl_resource_get_client(surface->resource) != wm->server->client)
797 return;
798
Pekka Paalanen9a5fab02016-12-20 16:15:02 +0200799 wm_log("XWM: create weston_surface %p\n", surface);
800
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700801 wl_list_for_each(window, &wm->unpaired_window_list, link)
802 if (window->surface_id ==
803 wl_resource_get_id(surface->resource)) {
804 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700805 window->surface_id = 0;
806 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700807 break;
Murray Calavera883ac022015-06-06 13:02:22 +0000808 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700809}
810
811static void
Giulio Camuffob18f7882015-03-29 14:20:23 +0300812weston_wm_send_focus_window(struct weston_wm *wm,
813 struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400814{
Scott Moreau85ecac02012-05-21 15:49:14 -0600815 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400816
Scott Moreau85ecac02012-05-21 15:49:14 -0600817 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700818 uint32_t values[1];
819
Boyan Dingb9f863c2014-08-30 10:33:23 +0800820 if (window->override_redirect)
821 return;
822
Scott Moreau85ecac02012-05-21 15:49:14 -0600823 client_message.response_type = XCB_CLIENT_MESSAGE;
824 client_message.format = 32;
825 client_message.window = window->id;
826 client_message.type = wm->atom.wm_protocols;
827 client_message.data.data32[0] = wm->atom.wm_take_focus;
828 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
829
Murray Calavera883ac022015-06-06 13:02:22 +0000830 xcb_send_event(wm->conn, 0, window->id,
Scott Moreau85ecac02012-05-21 15:49:14 -0600831 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
832 (char *) &client_message);
833
834 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
835 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700836
837 values[0] = XCB_STACK_MODE_ABOVE;
838 xcb_configure_window (wm->conn, window->frame_id,
839 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600840 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400841 xcb_set_input_focus (wm->conn,
842 XCB_INPUT_FOCUS_POINTER_ROOT,
843 XCB_NONE,
844 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600845 }
Giulio Camuffob18f7882015-03-29 14:20:23 +0300846}
847
848static void
849weston_wm_window_activate(struct wl_listener *listener, void *data)
850{
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +0800851 struct weston_surface_activation_data *activation_data = data;
852 struct weston_surface *surface = activation_data->surface;
Giulio Camuffob18f7882015-03-29 14:20:23 +0300853 struct weston_wm_window *window = NULL;
854 struct weston_wm *wm =
855 container_of(listener, struct weston_wm, activate_listener);
856
857 if (surface) {
858 window = get_wm_window(surface);
859 }
860
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200861 if (window) {
862 weston_wm_set_net_active_window(wm, window->id);
863 } else {
864 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
865 }
866
Giulio Camuffob18f7882015-03-29 14:20:23 +0300867 weston_wm_send_focus_window(wm, window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400868
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500869 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800870 if (wm->focus_window->frame)
871 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400872 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500873 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400874 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500875 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800876 if (wm->focus_window->frame)
877 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400878 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500879 }
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200880
881 xcb_flush(wm->conn);
882
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400883}
884
Pekka Paalanen7ace8312017-01-18 15:37:58 +0200885/** Control Xwayland wl_surface.commit behaviour
886 *
887 * This function sets the "_XWAYLAND_ALLOW_COMMITS" property of the frame window
888 * (not the content window!) to \p allow.
889 *
890 * If the property is set to \c true, Xwayland will commit whenever it likes.
891 * If the property is set to \c false, Xwayland will not commit.
892 * If the property is not set at all, Xwayland assumes it is \c true.
893 *
894 * \param window The XWM window to control.
895 * \param allow Whether Xwayland is allowed to wl_surface.commit for the window.
896 */
897static void
898weston_wm_window_set_allow_commits(struct weston_wm_window *window, bool allow)
899{
900 struct weston_wm *wm = window->wm;
901 uint32_t property[1];
902
903 assert(window->frame_id != XCB_WINDOW_NONE);
904
905 property[0] = allow ? 1 : 0;
906
907 xcb_change_property(wm->conn,
908 XCB_PROP_MODE_REPLACE,
909 window->frame_id,
910 wm->atom.allow_commits,
911 XCB_ATOM_CARDINAL,
912 32, /* format */
913 1, property);
914}
915
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400916#define ICCCM_WITHDRAWN_STATE 0
917#define ICCCM_NORMAL_STATE 1
918#define ICCCM_ICONIC_STATE 3
919
920static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500921weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400922{
923 struct weston_wm *wm = window->wm;
924 uint32_t property[2];
925
926 property[0] = state;
927 property[1] = XCB_WINDOW_NONE;
928
929 xcb_change_property(wm->conn,
930 XCB_PROP_MODE_REPLACE,
931 window->id,
932 wm->atom.wm_state,
933 wm->atom.wm_state,
934 32, /* format */
935 2, property);
936}
937
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400938static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500939weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
940{
941 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200942 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500943 int i;
944
945 i = 0;
946 if (window->fullscreen)
947 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200948 if (window->maximized_vert)
949 property[i++] = wm->atom.net_wm_state_maximized_vert;
950 if (window->maximized_horz)
951 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500952
953 xcb_change_property(wm->conn,
954 XCB_PROP_MODE_REPLACE,
955 window->id,
956 wm->atom.net_wm_state,
957 XCB_ATOM_ATOM,
958 32, /* format */
959 i, property);
960}
961
962static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700963weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400964{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700965 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400966 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400967 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200968 int buttons = FRAME_BUTTON_CLOSE;
969
970 if (window->decorate & MWM_DECOR_MAXIMIZE)
971 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400972
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500973 window->frame = frame_create(window->wm->theme,
974 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200975 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500976 frame_resize_inside(window->frame, window->width, window->height);
977
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400978 weston_wm_window_get_frame_size(window, &width, &height);
979 weston_wm_window_get_child_position(window, &x, &y);
980
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400981 values[0] = wm->screen->black_pixel;
982 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400983 XCB_EVENT_MASK_KEY_PRESS |
984 XCB_EVENT_MASK_KEY_RELEASE |
985 XCB_EVENT_MASK_BUTTON_PRESS |
986 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400987 XCB_EVENT_MASK_POINTER_MOTION |
988 XCB_EVENT_MASK_ENTER_WINDOW |
989 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400990 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400991 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400992 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400993
994 window->frame_id = xcb_generate_id(wm->conn);
995 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400996 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400997 window->frame_id,
998 wm->screen->root,
999 0, 0,
1000 width, height,
1001 0,
1002 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001003 wm->visual_id,
1004 XCB_CW_BORDER_PIXEL |
1005 XCB_CW_EVENT_MASK |
1006 XCB_CW_COLORMAP, values);
1007
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001008 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
1009
1010 values[0] = 0;
1011 xcb_configure_window(wm->conn, window->id,
1012 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
1013
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001014 window->cairo_surface =
1015 cairo_xcb_surface_create_with_xrender_format(wm->conn,
1016 wm->screen,
1017 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001018 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001019 width, height);
1020
1021 hash_table_insert(wm->window_hash, window->frame_id, window);
1022}
1023
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001024/*
1025 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
1026 * Passing a <0 desktop value deletes the property.
1027 */
1028static void
1029weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
Bryce Harringtone00554b2015-06-12 10:17:39 -07001030 int desktop)
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001031{
1032 if (desktop >= 0) {
1033 xcb_change_property(window->wm->conn,
1034 XCB_PROP_MODE_REPLACE,
1035 window->id,
1036 window->wm->atom.net_wm_desktop,
1037 XCB_ATOM_CARDINAL,
1038 32, /* format */
1039 1, &desktop);
1040 } else {
1041 xcb_delete_property(window->wm->conn,
1042 window->id,
1043 window->wm->atom.net_wm_desktop);
1044 }
1045}
1046
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001047static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001048weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
1049{
1050 xcb_map_request_event_t *map_request =
1051 (xcb_map_request_event_t *) event;
1052 struct weston_wm_window *window;
Pekka Paalanen505237e2016-12-01 15:41:11 +02001053 struct weston_output *output;
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001054
1055 if (our_resource(wm, map_request->window)) {
1056 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
1057 map_request->window);
1058 return;
1059 }
1060
Derek Foreman49372142015-04-09 10:51:22 -05001061 if (!wm_lookup_window(wm, map_request->window, &window))
1062 return;
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001063
1064 weston_wm_window_read_properties(window);
1065
Pekka Paalanenc9ca2c02016-12-15 16:04:14 +02001066 /* For a new Window, MapRequest happens before the Window is realized
1067 * in Xwayland. We do the real xcb_map_window() here as a response to
1068 * MapRequest. The Window will get realized (wl_surface created in
1069 * Wayland and WL_SURFACE_ID sent in X11) when it has been mapped for
1070 * real.
1071 *
1072 * MapRequest only happens for (X11) unmapped Windows. On UnmapNotify,
1073 * we reset shsurf to NULL, so even if X11 connection races far ahead
1074 * of the Wayland connection and the X11 client is repeatedly mapping
1075 * and unmapping, we will never have shsurf set on MapRequest.
1076 */
1077 assert(!window->shsurf);
1078
Pekka Paalanen882aff02016-11-16 14:15:18 +02001079 window->map_request_x = window->x;
1080 window->map_request_y = window->y;
1081
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001082 if (window->frame_id == XCB_WINDOW_NONE)
Pekka Paalanen9f5a1032016-12-20 15:31:15 +02001083 weston_wm_window_create_frame(window); /* sets frame_id */
1084 assert(window->frame_id != XCB_WINDOW_NONE);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001085
Pekka Paalanen44660c32016-11-14 15:38:43 +02001086 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d, %dx%d @ %d,%d)\n",
1087 window->id, window, window->frame_id,
Pekka Paalanen882aff02016-11-16 14:15:18 +02001088 window->width, window->height,
1089 window->map_request_x, window->map_request_y);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001090
Pekka Paalanen7ace8312017-01-18 15:37:58 +02001091 weston_wm_window_set_allow_commits(window, false);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001092 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
1093 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001094 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001095
Pekka Paalanen505237e2016-12-01 15:41:11 +02001096 if (legacy_fullscreen(wm, window, &output)) {
1097 window->fullscreen = 1;
1098 weston_output_weak_ref_set(&window->legacy_fullscreen_output,
1099 output);
1100 }
1101
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001102 xcb_map_window(wm->conn, map_request->window);
1103 xcb_map_window(wm->conn, window->frame_id);
Pekka Paalanen9f5a1032016-12-20 15:31:15 +02001104
1105 /* Mapped in the X server, we can draw immediately.
1106 * Cannot set pending state though, no weston_surface until
1107 * xserver_map_shell_surface() time. */
1108 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001109}
1110
1111static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001112weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1113{
1114 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
1115
1116 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001117 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
1118 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001119 return;
1120 }
1121
Pekka Paalanen73428a82016-12-19 15:10:01 +02001122 wm_log("XCB_MAP_NOTIFY (window %d%s)\n", map_notify->window,
1123 map_notify->override_redirect ? ", override" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001124}
1125
1126static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001127weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1128{
1129 xcb_unmap_notify_event_t *unmap_notify =
1130 (xcb_unmap_notify_event_t *) event;
1131 struct weston_wm_window *window;
1132
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001133 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
1134 unmap_notify->window,
1135 unmap_notify->event,
1136 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001137
1138 if (our_resource(wm, unmap_notify->window))
1139 return;
1140
MoD31700122013-06-11 19:58:55 -05001141 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -04001142 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1143 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001144 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001145
Derek Foreman49372142015-04-09 10:51:22 -05001146 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1147 return;
1148
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001149 if (window->surface_id) {
1150 /* Make sure we're not on the unpaired surface list or we
1151 * could be assigned a surface during surface creation that
1152 * was mapped before this unmap request.
1153 */
1154 wl_list_remove(&window->link);
1155 window->surface_id = 0;
1156 }
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001157 if (wm->focus_window == window)
1158 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001159 if (window->surface)
1160 wl_list_remove(&window->surface_destroy_listener.link);
1161 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +02001162 window->shsurf = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001163
1164 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1165 weston_wm_window_set_virtual_desktop(window, -1);
1166
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001167 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001168}
1169
1170static void
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001171weston_wm_window_draw_decoration(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001172{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001173 cairo_t *cr;
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001174 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001175
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001176 wm_log("XWM: draw decoration, win %d\n", window->id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001177
1178 weston_wm_window_get_frame_size(window, &width, &height);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001179
1180 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1181 cr = cairo_create(window->cairo_surface);
1182
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001183 if (window->fullscreen) {
1184 /* nothing */
1185 } else if (window->decorate) {
Pekka Paalanen9a330e12016-12-15 15:37:24 +02001186 frame_set_title(window->frame, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001187 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001188 } else {
1189 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1190 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1191 cairo_paint(cr);
1192
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001193 render_shadow(cr, window->wm->theme->shadow,
1194 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001195 }
1196
1197 cairo_destroy(cr);
Pekka Paalanendb7b9f32017-01-16 17:01:11 +02001198 cairo_surface_flush(window->cairo_surface);
1199 xcb_flush(window->wm->conn);
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001200}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001201
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001202static void
1203weston_wm_window_set_pending_state(struct weston_wm_window *window)
1204{
1205 int x, y, width, height;
1206 int32_t input_x, input_y, input_w, input_h;
1207 const struct weston_desktop_xwayland_interface *xwayland_interface =
1208 window->wm->server->compositor->xwayland_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001209
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001210 if (!window->surface)
1211 return;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001212
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001213 weston_wm_window_get_frame_size(window, &width, &height);
1214 weston_wm_window_get_child_position(window, &x, &y);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001215
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001216 pixman_region32_fini(&window->surface->pending.opaque);
1217 if (window->has_alpha) {
1218 pixman_region32_init(&window->surface->pending.opaque);
1219 } else {
1220 /* We leave an extra pixel around the X window area to
1221 * make sure we don't sample from the undefined alpha
1222 * channel when filtering. */
1223 pixman_region32_init_rect(&window->surface->pending.opaque,
1224 x - 1, y - 1,
1225 window->width + 2,
1226 window->height + 2);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001227 }
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001228
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001229 if (window->decorate && !window->fullscreen) {
1230 frame_input_rect(window->frame, &input_x, &input_y,
1231 &input_w, &input_h);
1232 } else {
1233 input_x = x;
1234 input_y = y;
1235 input_w = width;
1236 input_h = height;
1237 }
1238
1239 wm_log("XWM: win %d geometry: %d,%d %dx%d\n",
1240 window->id, input_x, input_y, input_w, input_h);
1241
Pekka Paalanen83626b92016-12-20 14:01:42 +02001242 pixman_region32_fini(&window->surface->pending.input);
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001243 pixman_region32_init_rect(&window->surface->pending.input,
1244 input_x, input_y, input_w, input_h);
1245
1246 xwayland_interface->set_window_geometry(window->shsurf,
Pekka Paalanen83626b92016-12-20 14:01:42 +02001247 input_x, input_y,
1248 input_w, input_h);
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001249 if (window->name)
1250 xwayland_interface->set_title(window->shsurf, window->name);
1251 if (window->pid > 0)
1252 xwayland_interface->set_pid(window->shsurf, window->pid);
1253}
1254
1255static void
1256weston_wm_window_do_repaint(void *data)
1257{
1258 struct weston_wm_window *window = data;
1259
1260 window->repaint_source = NULL;
1261
1262 weston_wm_window_read_properties(window);
1263
1264 weston_wm_window_draw_decoration(window);
1265 weston_wm_window_set_pending_state(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001266}
1267
1268static void
Pekka Paalanened568832016-12-20 14:05:03 +02001269weston_wm_window_set_pending_state_OR(struct weston_wm_window *window)
1270{
1271 int width, height;
1272
1273 /* for override-redirect windows */
1274 assert(window->frame_id == XCB_WINDOW_NONE);
1275
1276 if (!window->surface)
1277 return;
1278
1279 weston_wm_window_get_frame_size(window, &width, &height);
1280 pixman_region32_fini(&window->surface->pending.opaque);
1281 if (window->has_alpha) {
1282 pixman_region32_init(&window->surface->pending.opaque);
1283 } else {
1284 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1285 width, height);
1286 }
1287}
1288
1289static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001290weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1291{
1292 struct weston_wm *wm = window->wm;
1293
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001294 if (window->frame_id == XCB_WINDOW_NONE) {
Pekka Paalanened568832016-12-20 14:05:03 +02001295 /* Override-redirect windows go through here, but we
1296 * cannot assert(window->override_redirect); because
1297 * we do not deal with changing OR flag yet.
1298 * XXX: handle OR flag changes in message handlers
1299 */
1300 weston_wm_window_set_pending_state_OR(window);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001301 return;
1302 }
1303
1304 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001305 return;
1306
Pekka Paalanena04eacc2016-11-28 16:42:25 +02001307 wm_log("XWM: schedule repaint, win %d\n", window->id);
1308
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001309 window->repaint_source =
1310 wl_event_loop_add_idle(wm->server->loop,
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001311 weston_wm_window_do_repaint, window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001312}
1313
1314static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001315weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1316{
1317 xcb_property_notify_event_t *property_notify =
1318 (xcb_property_notify_event_t *) event;
1319 struct weston_wm_window *window;
1320
Derek Foreman49372142015-04-09 10:51:22 -05001321 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001322 return;
1323
1324 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001325
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001326 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001327 if (property_notify->state == XCB_PROPERTY_DELETE)
Pekka Paalanen8cc153b2016-12-19 15:18:45 +02001328 wm_log_continue("deleted %s\n",
1329 get_atom_name(wm->conn, property_notify->atom));
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001330 else
1331 read_and_dump_property(wm, property_notify->window,
1332 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001333
1334 if (property_notify->atom == wm->atom.net_wm_name ||
1335 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001336 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001337}
1338
1339static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001340weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001341 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001342{
1343 struct weston_wm_window *window;
1344 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001345 xcb_get_geometry_cookie_t geometry_cookie;
1346 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001347
Peter Huttererf3d62272013-08-08 11:57:05 +10001348 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001349 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001350 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001351 return;
1352 }
1353
MoD384a11a2013-06-22 11:04:21 -05001354 geometry_cookie = xcb_get_geometry(wm->conn, id);
1355
Giulio Camuffob18f7882015-03-29 14:20:23 +03001356 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
1357 XCB_EVENT_MASK_FOCUS_CHANGE;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001358 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1359
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001360 window->wm = wm;
1361 window->id = id;
1362 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001363 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001364 window->width = width;
1365 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001366 window->x = x;
1367 window->y = y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001368 window->pos_dirty = false;
Pekka Paalanen882aff02016-11-16 14:15:18 +02001369 window->map_request_x = INT_MIN; /* out of range for valid positions */
1370 window->map_request_y = INT_MIN; /* out of range for valid positions */
Pekka Paalanen505237e2016-12-01 15:41:11 +02001371 weston_output_weak_ref_init(&window->legacy_fullscreen_output);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001372
MoD384a11a2013-06-22 11:04:21 -05001373 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1374 /* technically we should use XRender and check the visual format's
1375 alpha_mask, but checking depth is simpler and works in all known cases */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001376 if (geometry_reply != NULL)
MoD384a11a2013-06-22 11:04:21 -05001377 window->has_alpha = geometry_reply->depth == 32;
1378 free(geometry_reply);
1379
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001380 hash_table_insert(wm->window_hash, id, window);
1381}
1382
1383static void
1384weston_wm_window_destroy(struct weston_wm_window *window)
1385{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001386 struct weston_wm *wm = window->wm;
1387
Pekka Paalanen505237e2016-12-01 15:41:11 +02001388 weston_output_weak_ref_clear(&window->legacy_fullscreen_output);
1389
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001390 if (window->repaint_source)
1391 wl_event_source_remove(window->repaint_source);
1392 if (window->cairo_surface)
1393 cairo_surface_destroy(window->cairo_surface);
1394
1395 if (window->frame_id) {
1396 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1397 xcb_destroy_window(wm->conn, window->frame_id);
1398 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001399 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001400 hash_table_remove(wm->window_hash, window->frame_id);
1401 window->frame_id = XCB_WINDOW_NONE;
1402 }
1403
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001404 if (window->surface_id)
1405 wl_list_remove(&window->link);
1406
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001407 if (window->surface)
1408 wl_list_remove(&window->surface_destroy_listener.link);
1409
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001410 hash_table_remove(window->wm->window_hash, window->id);
1411 free(window);
1412}
1413
1414static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001415weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1416{
1417 xcb_create_notify_event_t *create_notify =
1418 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001419
Pekka Paalanen7db6c432016-11-14 14:30:57 +02001420 wm_log("XCB_CREATE_NOTIFY (window %d, at (%d, %d), width %d, height %d%s%s)\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001421 create_notify->window,
Pekka Paalanen7db6c432016-11-14 14:30:57 +02001422 create_notify->x, create_notify->y,
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001423 create_notify->width, create_notify->height,
1424 create_notify->override_redirect ? ", override" : "",
1425 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001426
1427 if (our_resource(wm, create_notify->window))
1428 return;
1429
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001430 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001431 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001432 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001433 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001434}
1435
1436static void
1437weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1438{
1439 xcb_destroy_notify_event_t *destroy_notify =
1440 (xcb_destroy_notify_event_t *) event;
1441 struct weston_wm_window *window;
1442
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001443 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1444 destroy_notify->window,
1445 destroy_notify->event,
1446 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001447
1448 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001449 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001450
Derek Foreman49372142015-04-09 10:51:22 -05001451 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1452 return;
1453
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001454 weston_wm_window_destroy(window);
1455}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001456
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001457static void
1458weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1459{
1460 xcb_reparent_notify_event_t *reparent_notify =
1461 (xcb_reparent_notify_event_t *) event;
1462 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001463
Pekka Paalanen73428a82016-12-19 15:10:01 +02001464 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d%s)\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001465 reparent_notify->window,
1466 reparent_notify->parent,
Pekka Paalanen73428a82016-12-19 15:10:01 +02001467 reparent_notify->event,
1468 reparent_notify->override_redirect ? ", override" : "");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001469
1470 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001471 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001472 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001473 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001474 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001475 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1476 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001477 weston_wm_window_destroy(window);
1478 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001479}
1480
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001481struct weston_seat *
1482weston_wm_pick_seat(struct weston_wm *wm)
1483{
Tom Hochsteine7fff212016-11-01 14:14:00 -05001484 struct wl_list *seats = wm->server->compositor->seat_list.next;
1485 if (wl_list_empty(seats))
1486 return NULL;
1487 return container_of(seats, struct weston_seat, link);
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001488}
1489
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001490static struct weston_seat *
1491weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1492{
1493 struct weston_wm *wm = window->wm;
1494 struct weston_seat *seat, *s;
1495
1496 seat = NULL;
1497 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001498 struct weston_pointer *pointer = weston_seat_get_pointer(s);
1499 struct weston_pointer *old_pointer =
1500 weston_seat_get_pointer(seat);
1501
1502 if (pointer && pointer->focus &&
1503 pointer->focus->surface == window->surface &&
1504 pointer->button_count > 0 &&
1505 (!seat ||
1506 pointer->grab_serial -
1507 old_pointer->grab_serial < (1 << 30)))
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001508 seat = s;
1509 }
1510
1511 return seat;
1512}
1513
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001514static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001515weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1516 xcb_client_message_event_t *client_message)
1517{
1518 static const int map[] = {
1519 THEME_LOCATION_RESIZING_TOP_LEFT,
1520 THEME_LOCATION_RESIZING_TOP,
1521 THEME_LOCATION_RESIZING_TOP_RIGHT,
1522 THEME_LOCATION_RESIZING_RIGHT,
1523 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1524 THEME_LOCATION_RESIZING_BOTTOM,
1525 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1526 THEME_LOCATION_RESIZING_LEFT
1527 };
1528
1529 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001530 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Derek Foreman1281a362015-07-31 16:55:32 -05001531 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001532 int detail;
Quentin Glidic955cec02016-08-12 10:41:35 +02001533 const struct weston_desktop_xwayland_interface *xwayland_interface =
1534 wm->server->compositor->xwayland_interface;
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001535
Derek Foreman1281a362015-07-31 16:55:32 -05001536 if (!pointer || pointer->button_count != 1
1537 || !pointer->focus
1538 || pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001539 return;
1540
1541 detail = client_message->data.data32[2];
1542 switch (detail) {
1543 case _NET_WM_MOVERESIZE_MOVE:
Quentin Glidic955cec02016-08-12 10:41:35 +02001544 xwayland_interface->move(window->shsurf, pointer);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001545 break;
1546 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1547 case _NET_WM_MOVERESIZE_SIZE_TOP:
1548 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1549 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1550 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1551 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1552 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1553 case _NET_WM_MOVERESIZE_SIZE_LEFT:
Quentin Glidic955cec02016-08-12 10:41:35 +02001554 xwayland_interface->resize(window->shsurf, pointer, map[detail]);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001555 break;
1556 case _NET_WM_MOVERESIZE_CANCEL:
1557 break;
1558 }
1559}
1560
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001561#define _NET_WM_STATE_REMOVE 0
1562#define _NET_WM_STATE_ADD 1
1563#define _NET_WM_STATE_TOGGLE 2
1564
1565static int
1566update_state(int action, int *state)
1567{
1568 int new_state, changed;
1569
1570 switch (action) {
1571 case _NET_WM_STATE_REMOVE:
1572 new_state = 0;
1573 break;
1574 case _NET_WM_STATE_ADD:
1575 new_state = 1;
1576 break;
1577 case _NET_WM_STATE_TOGGLE:
1578 new_state = !*state;
1579 break;
1580 default:
1581 return 0;
1582 }
1583
1584 changed = (*state != new_state);
1585 *state = new_state;
1586
1587 return changed;
1588}
1589
1590static void
1591weston_wm_window_configure(void *data);
1592
1593static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001594weston_wm_window_set_toplevel(struct weston_wm_window *window)
1595{
Quentin Glidic955cec02016-08-12 10:41:35 +02001596 const struct weston_desktop_xwayland_interface *xwayland_interface =
1597 window->wm->server->compositor->xwayland_interface;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001598
Quentin Glidic955cec02016-08-12 10:41:35 +02001599 xwayland_interface->set_toplevel(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001600 window->width = window->saved_width;
1601 window->height = window->saved_height;
1602 if (window->frame)
1603 frame_resize_inside(window->frame,
1604 window->width,
1605 window->height);
1606 weston_wm_window_configure(window);
1607}
1608
1609static inline bool
1610weston_wm_window_is_maximized(struct weston_wm_window *window)
1611{
1612 return window->maximized_horz && window->maximized_vert;
1613}
1614
1615static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001616weston_wm_window_handle_state(struct weston_wm_window *window,
1617 xcb_client_message_event_t *client_message)
1618{
1619 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +02001620 const struct weston_desktop_xwayland_interface *xwayland_interface =
1621 wm->server->compositor->xwayland_interface;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001622 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001623 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001624
1625 action = client_message->data.data32[0];
1626 property = client_message->data.data32[1];
1627
1628 if (property == wm->atom.net_wm_state_fullscreen &&
1629 update_state(action, &window->fullscreen)) {
1630 weston_wm_window_set_net_wm_state(window);
1631 if (window->fullscreen) {
1632 window->saved_width = window->width;
1633 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001634
1635 if (window->shsurf)
Quentin Glidic955cec02016-08-12 10:41:35 +02001636 xwayland_interface->set_fullscreen(window->shsurf,
1637 NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001638 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001639 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001640 weston_wm_window_set_toplevel(window);
1641 }
1642 } else {
1643 if (property == wm->atom.net_wm_state_maximized_vert &&
1644 update_state(action, &window->maximized_vert))
1645 weston_wm_window_set_net_wm_state(window);
1646 if (property == wm->atom.net_wm_state_maximized_horz &&
1647 update_state(action, &window->maximized_horz))
1648 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001649
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001650 if (maximized != weston_wm_window_is_maximized(window)) {
1651 if (weston_wm_window_is_maximized(window)) {
1652 window->saved_width = window->width;
1653 window->saved_height = window->height;
1654
1655 if (window->shsurf)
Quentin Glidic955cec02016-08-12 10:41:35 +02001656 xwayland_interface->set_maximized(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001657 } else if (window->shsurf) {
1658 weston_wm_window_set_toplevel(window);
1659 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001660 }
1661 }
1662}
1663
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001664static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001665surface_destroy(struct wl_listener *listener, void *data)
1666{
1667 struct weston_wm_window *window =
1668 container_of(listener,
1669 struct weston_wm_window, surface_destroy_listener);
1670
1671 wm_log("surface for xid %d destroyed\n", window->id);
1672
1673 /* This should have been freed by the shell.
1674 * Don't try to use it later. */
1675 window->shsurf = NULL;
1676 window->surface = NULL;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001677}
1678
1679static void
1680weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1681 xcb_client_message_event_t *client_message)
1682{
1683 struct weston_wm *wm = window->wm;
1684 struct wl_resource *resource;
1685
1686 if (window->surface_id != 0) {
1687 wm_log("already have surface id for window %d\n", window->id);
1688 return;
1689 }
1690
1691 /* Xwayland will send the wayland requests to create the
1692 * wl_surface before sending this client message. Even so, we
1693 * can end up handling the X event before the wayland requests
1694 * and thus when we try to look up the surface ID, the surface
1695 * hasn't been created yet. In that case put the window on
1696 * the unpaired window list and continue when the surface gets
1697 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001698 uint32_t id = client_message->data.data32[0];
1699 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001700 if (resource) {
1701 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001702 xserver_map_shell_surface(window,
1703 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001704 }
1705 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001706 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001707 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001708 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001709}
1710
1711static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001712weston_wm_handle_client_message(struct weston_wm *wm,
1713 xcb_generic_event_t *event)
1714{
1715 xcb_client_message_event_t *client_message =
1716 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001717 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001718
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001719 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1720 get_atom_name(wm->conn, client_message->type),
1721 client_message->data.data32[0],
1722 client_message->data.data32[1],
1723 client_message->data.data32[2],
1724 client_message->data.data32[3],
1725 client_message->data.data32[4],
1726 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001727
Jason Ekstrand0250a742014-07-24 13:17:47 -07001728 /* The window may get created and destroyed before we actually
1729 * handle the message. If it doesn't exist, bail.
1730 */
Derek Foreman49372142015-04-09 10:51:22 -05001731 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001732 return;
1733
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001734 if (client_message->type == wm->atom.net_wm_moveresize)
1735 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001736 else if (client_message->type == wm->atom.net_wm_state)
1737 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001738 else if (client_message->type == wm->atom.wl_surface_id)
1739 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001740}
1741
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001742enum cursor_type {
1743 XWM_CURSOR_TOP,
1744 XWM_CURSOR_BOTTOM,
1745 XWM_CURSOR_LEFT,
1746 XWM_CURSOR_RIGHT,
1747 XWM_CURSOR_TOP_LEFT,
1748 XWM_CURSOR_TOP_RIGHT,
1749 XWM_CURSOR_BOTTOM_LEFT,
1750 XWM_CURSOR_BOTTOM_RIGHT,
1751 XWM_CURSOR_LEFT_PTR,
1752};
1753
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001754/*
1755 * The following correspondences between file names and cursors was copied
1756 * from: https://bugs.kde.org/attachment.cgi?id=67313
1757 */
1758
1759static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001760 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001761 "sw-resize",
1762 "size_bdiag"
1763};
1764
1765static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001766 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001767 "se-resize",
1768 "size_fdiag"
1769};
1770
1771static const char *bottom_sides[] = {
1772 "bottom_side",
1773 "s-resize",
1774 "size_ver"
1775};
1776
1777static const char *left_ptrs[] = {
1778 "left_ptr",
1779 "default",
1780 "top_left_arrow",
1781 "left-arrow"
1782};
1783
1784static const char *left_sides[] = {
1785 "left_side",
1786 "w-resize",
1787 "size_hor"
1788};
1789
1790static const char *right_sides[] = {
1791 "right_side",
1792 "e-resize",
1793 "size_hor"
1794};
1795
1796static const char *top_left_corners[] = {
1797 "top_left_corner",
1798 "nw-resize",
1799 "size_fdiag"
1800};
1801
1802static const char *top_right_corners[] = {
1803 "top_right_corner",
1804 "ne-resize",
1805 "size_bdiag"
1806};
1807
1808static const char *top_sides[] = {
1809 "top_side",
1810 "n-resize",
1811 "size_ver"
1812};
1813
1814struct cursor_alternatives {
1815 const char **names;
1816 size_t count;
1817};
1818
1819static const struct cursor_alternatives cursors[] = {
1820 {top_sides, ARRAY_LENGTH(top_sides)},
1821 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1822 {left_sides, ARRAY_LENGTH(left_sides)},
1823 {right_sides, ARRAY_LENGTH(right_sides)},
1824 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1825 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1826 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1827 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1828 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001829};
1830
1831static void
1832weston_wm_create_cursors(struct weston_wm *wm)
1833{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001834 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001835 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001836 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001837
1838 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1839 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001840 for (j = 0; j < cursors[i].count; j++) {
1841 name = cursors[i].names[j];
1842 wm->cursors[i] =
1843 xcb_cursor_library_load_cursor(wm, name);
1844 if (wm->cursors[i] != (xcb_cursor_t)-1)
1845 break;
1846 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001847 }
1848
1849 wm->last_cursor = -1;
1850}
1851
1852static void
1853weston_wm_destroy_cursors(struct weston_wm *wm)
1854{
1855 uint8_t i;
1856
1857 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1858 xcb_free_cursor(wm->conn, wm->cursors[i]);
1859
1860 free(wm->cursors);
1861}
1862
1863static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001864get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001865{
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001866 switch (location) {
1867 case THEME_LOCATION_RESIZING_TOP:
1868 return XWM_CURSOR_TOP;
1869 case THEME_LOCATION_RESIZING_BOTTOM:
1870 return XWM_CURSOR_BOTTOM;
1871 case THEME_LOCATION_RESIZING_LEFT:
1872 return XWM_CURSOR_LEFT;
1873 case THEME_LOCATION_RESIZING_RIGHT:
1874 return XWM_CURSOR_RIGHT;
1875 case THEME_LOCATION_RESIZING_TOP_LEFT:
1876 return XWM_CURSOR_TOP_LEFT;
1877 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1878 return XWM_CURSOR_TOP_RIGHT;
1879 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1880 return XWM_CURSOR_BOTTOM_LEFT;
1881 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1882 return XWM_CURSOR_BOTTOM_RIGHT;
1883 case THEME_LOCATION_EXTERIOR:
1884 case THEME_LOCATION_TITLEBAR:
1885 default:
1886 return XWM_CURSOR_LEFT_PTR;
1887 }
1888}
1889
1890static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001891weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1892 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001893{
1894 uint32_t cursor_value_list;
1895
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001896 if (wm->last_cursor == cursor)
1897 return;
1898
1899 wm->last_cursor = cursor;
1900
1901 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001902 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001903 XCB_CW_CURSOR, &cursor_value_list);
1904 xcb_flush(wm->conn);
1905}
1906
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001907static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001908weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001909{
1910 xcb_client_message_event_t client_message;
1911
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001912 if (window->delete_window) {
1913 client_message.response_type = XCB_CLIENT_MESSAGE;
1914 client_message.format = 32;
1915 client_message.window = window->id;
1916 client_message.type = window->wm->atom.wm_protocols;
1917 client_message.data.data32[0] =
1918 window->wm->atom.wm_delete_window;
1919 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001920
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001921 xcb_send_event(window->wm->conn, 0, window->id,
1922 XCB_EVENT_MASK_NO_EVENT,
1923 (char *) &client_message);
1924 } else {
1925 xcb_kill_client(window->wm->conn, window->id);
1926 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001927}
1928
1929static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001930weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1931{
1932 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
Quentin Glidic955cec02016-08-12 10:41:35 +02001933 const struct weston_desktop_xwayland_interface *xwayland_interface =
1934 wm->server->compositor->xwayland_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001935 struct weston_seat *seat;
Derek Foremane4d6c832015-08-05 14:48:11 -05001936 struct weston_pointer *pointer;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001937 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001938 enum theme_location location;
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001939 enum wl_pointer_button_state button_state;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001940 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001941
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001942 wm_log("XCB_BUTTON_%s (detail %d)\n",
1943 button->response_type == XCB_BUTTON_PRESS ?
1944 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001945
Derek Foreman49372142015-04-09 10:51:22 -05001946 if (!wm_lookup_window(wm, button->event, &window) ||
1947 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001948 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001949
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001950 if (button->detail != 1 && button->detail != 2)
1951 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001952
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001953 seat = weston_wm_pick_seat_for_window(window);
Derek Foremane4d6c832015-08-05 14:48:11 -05001954 pointer = weston_seat_get_pointer(seat);
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001955
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001956 button_state = button->response_type == XCB_BUTTON_PRESS ?
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001957 WL_POINTER_BUTTON_STATE_PRESSED :
1958 WL_POINTER_BUTTON_STATE_RELEASED;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001959 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1960
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001961 /* Make sure we're looking at the right location. The frame
1962 * could have received a motion event from a pointer from a
1963 * different wl_seat, but under X it looks like our core
1964 * pointer moved. Move the frame pointer to the button press
1965 * location before deciding what to do. */
1966 location = frame_pointer_motion(window->frame, NULL,
1967 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001968 location = frame_pointer_button(window->frame, NULL,
1969 button_id, button_state);
1970 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1971 weston_wm_window_schedule_repaint(window);
1972
1973 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001974 if (pointer)
Quentin Glidic955cec02016-08-12 10:41:35 +02001975 xwayland_interface->move(window->shsurf, pointer);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001976 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1977 }
1978
1979 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001980 if (pointer)
Quentin Glidic955cec02016-08-12 10:41:35 +02001981 xwayland_interface->resize(window->shsurf, pointer, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001982 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1983 }
1984
1985 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001986 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001987 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001988 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001989
1990 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1991 window->maximized_horz = !window->maximized_horz;
1992 window->maximized_vert = !window->maximized_vert;
1993 if (weston_wm_window_is_maximized(window)) {
1994 window->saved_width = window->width;
1995 window->saved_height = window->height;
Quentin Glidic955cec02016-08-12 10:41:35 +02001996 xwayland_interface->set_maximized(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001997 } else {
1998 weston_wm_window_set_toplevel(window);
1999 }
2000 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
2001 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002002}
2003
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002004static void
2005weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
2006{
2007 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
2008 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002009 enum theme_location location;
2010 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002011
Derek Foreman49372142015-04-09 10:51:22 -05002012 if (!wm_lookup_window(wm, motion->event, &window) ||
2013 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002014 return;
2015
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002016 location = frame_pointer_motion(window->frame, NULL,
2017 motion->event_x, motion->event_y);
2018 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
2019 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002020
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002021 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04002022 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002023}
2024
2025static void
2026weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
2027{
2028 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
2029 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002030 enum theme_location location;
2031 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002032
Derek Foreman49372142015-04-09 10:51:22 -05002033 if (!wm_lookup_window(wm, enter->event, &window) ||
2034 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002035 return;
2036
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002037 location = frame_pointer_enter(window->frame, NULL,
2038 enter->event_x, enter->event_y);
2039 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
2040 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002041
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002042 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04002043 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002044}
2045
2046static void
2047weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
2048{
2049 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
2050 struct weston_wm_window *window;
2051
Derek Foreman49372142015-04-09 10:51:22 -05002052 if (!wm_lookup_window(wm, leave->event, &window) ||
2053 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002054 return;
2055
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002056 frame_pointer_leave(window->frame, NULL);
2057 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
2058 weston_wm_window_schedule_repaint(window);
2059
Tiago Vignattic1903232012-07-16 12:15:37 -04002060 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002061}
2062
Giulio Camuffob18f7882015-03-29 14:20:23 +03002063static void
2064weston_wm_handle_focus_in(struct weston_wm *wm, xcb_generic_event_t *event)
2065{
2066 xcb_focus_in_event_t *focus = (xcb_focus_in_event_t *) event;
Olivier Fourdan9e07d252017-05-15 13:32:01 +02002067
2068 /* Do not interfere with grabs */
2069 if (focus->mode == XCB_NOTIFY_MODE_GRAB ||
2070 focus->mode == XCB_NOTIFY_MODE_UNGRAB)
2071 return;
2072
Giulio Camuffob18f7882015-03-29 14:20:23 +03002073 /* Do not let X clients change the focus behind the compositor's
2074 * back. Reset the focus to the old one if it changed. */
2075 if (!wm->focus_window || focus->event != wm->focus_window->id)
2076 weston_wm_send_focus_window(wm, wm->focus_window);
2077}
2078
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002079static int
2080weston_wm_handle_event(int fd, uint32_t mask, void *data)
2081{
2082 struct weston_wm *wm = data;
2083 xcb_generic_event_t *event;
2084 int count = 0;
2085
2086 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
2087 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002088 free(event);
2089 count++;
2090 continue;
2091 }
2092
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002093 if (weston_wm_handle_dnd_event(wm, event)) {
2094 free(event);
2095 count++;
2096 continue;
2097 }
2098
MoD31700122013-06-11 19:58:55 -05002099 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002100 case XCB_BUTTON_PRESS:
2101 case XCB_BUTTON_RELEASE:
2102 weston_wm_handle_button(wm, event);
2103 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002104 case XCB_ENTER_NOTIFY:
2105 weston_wm_handle_enter(wm, event);
2106 break;
2107 case XCB_LEAVE_NOTIFY:
2108 weston_wm_handle_leave(wm, event);
2109 break;
2110 case XCB_MOTION_NOTIFY:
2111 weston_wm_handle_motion(wm, event);
2112 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002113 case XCB_CREATE_NOTIFY:
2114 weston_wm_handle_create_notify(wm, event);
2115 break;
2116 case XCB_MAP_REQUEST:
2117 weston_wm_handle_map_request(wm, event);
2118 break;
2119 case XCB_MAP_NOTIFY:
2120 weston_wm_handle_map_notify(wm, event);
2121 break;
2122 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04002123 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002124 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04002125 case XCB_REPARENT_NOTIFY:
2126 weston_wm_handle_reparent_notify(wm, event);
2127 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002128 case XCB_CONFIGURE_REQUEST:
2129 weston_wm_handle_configure_request(wm, event);
2130 break;
2131 case XCB_CONFIGURE_NOTIFY:
2132 weston_wm_handle_configure_notify(wm, event);
2133 break;
2134 case XCB_DESTROY_NOTIFY:
2135 weston_wm_handle_destroy_notify(wm, event);
2136 break;
2137 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002138 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002139 break;
2140 case XCB_PROPERTY_NOTIFY:
2141 weston_wm_handle_property_notify(wm, event);
2142 break;
2143 case XCB_CLIENT_MESSAGE:
2144 weston_wm_handle_client_message(wm, event);
2145 break;
Giulio Camuffob18f7882015-03-29 14:20:23 +03002146 case XCB_FOCUS_IN:
2147 weston_wm_handle_focus_in(wm, event);
2148 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002149 }
2150
2151 free(event);
2152 count++;
2153 }
2154
Marek Chalupaa1f3f3c2015-08-12 09:55:12 +02002155 if (count != 0)
2156 xcb_flush(wm->conn);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002157
2158 return count;
2159}
2160
2161static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002162weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window) {
2163 xcb_change_property(wm->conn, XCB_PROP_MODE_REPLACE,
2164 wm->screen->root, wm->atom.net_active_window,
2165 wm->atom.window, 32, 1, &window);
2166}
2167
2168static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002169weston_wm_get_visual_and_colormap(struct weston_wm *wm)
2170{
2171 xcb_depth_iterator_t d_iter;
2172 xcb_visualtype_iterator_t vt_iter;
2173 xcb_visualtype_t *visualtype;
2174
2175 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
2176 visualtype = NULL;
2177 while (d_iter.rem > 0) {
2178 if (d_iter.data->depth == 32) {
2179 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
2180 visualtype = vt_iter.data;
2181 break;
2182 }
2183
2184 xcb_depth_next(&d_iter);
2185 }
2186
2187 if (visualtype == NULL) {
2188 weston_log("no 32 bit visualtype\n");
2189 return;
2190 }
2191
2192 wm->visual_id = visualtype->visual_id;
2193 wm->colormap = xcb_generate_id(wm->conn);
2194 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
2195 wm->colormap, wm->screen->root, wm->visual_id);
2196}
2197
2198static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002199weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002200{
2201
2202#define F(field) offsetof(struct weston_wm, field)
2203
2204 static const struct { const char *name; int offset; } atoms[] = {
2205 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002206 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002207 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
2208 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04002209 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002210 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002211 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002212 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002213 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002214 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002215 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2216 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002217 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2218 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002219 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2220 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2221 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02002222 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002223 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2224
2225 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2226 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2227 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2228 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2229 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2230 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2231 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002232 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2233 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002234 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2235 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2236 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2237 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2238 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2239
2240 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2241 { "_NET_SUPPORTING_WM_CHECK",
2242 F(atom.net_supporting_wm_check) },
2243 { "_NET_SUPPORTED", F(atom.net_supported) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002244 { "_NET_ACTIVE_WINDOW", F(atom.net_active_window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002245 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2246 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002247 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002248 { "TARGETS", F(atom.targets) },
2249 { "UTF8_STRING", F(atom.utf8_string) },
2250 { "_WL_SELECTION", F(atom.wl_selection) },
2251 { "INCR", F(atom.incr) },
2252 { "TIMESTAMP", F(atom.timestamp) },
2253 { "MULTIPLE", F(atom.multiple) },
2254 { "UTF8_STRING" , F(atom.utf8_string) },
2255 { "COMPOUND_TEXT", F(atom.compound_text) },
2256 { "TEXT", F(atom.text) },
2257 { "STRING", F(atom.string) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002258 { "WINDOW", F(atom.window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002259 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2260 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002261 { "XdndSelection", F(atom.xdnd_selection) },
2262 { "XdndAware", F(atom.xdnd_aware) },
2263 { "XdndEnter", F(atom.xdnd_enter) },
2264 { "XdndLeave", F(atom.xdnd_leave) },
2265 { "XdndDrop", F(atom.xdnd_drop) },
2266 { "XdndStatus", F(atom.xdnd_status) },
2267 { "XdndFinished", F(atom.xdnd_finished) },
2268 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002269 { "XdndActionCopy", F(atom.xdnd_action_copy) },
Pekka Paalanen7ace8312017-01-18 15:37:58 +02002270 { "_XWAYLAND_ALLOW_COMMITS", F(atom.allow_commits) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002271 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002272 };
2273#undef F
2274
2275 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2276 xcb_xfixes_query_version_reply_t *xfixes_reply;
2277 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2278 xcb_intern_atom_reply_t *reply;
2279 xcb_render_query_pict_formats_reply_t *formats_reply;
2280 xcb_render_query_pict_formats_cookie_t formats_cookie;
2281 xcb_render_pictforminfo_t *formats;
2282 uint32_t i;
2283
2284 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002285 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002286
2287 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2288
2289 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2290 cookies[i] = xcb_intern_atom (wm->conn, 0,
2291 strlen(atoms[i].name),
2292 atoms[i].name);
2293
2294 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2295 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2296 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2297 free(reply);
2298 }
2299
2300 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2301 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002302 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002303
2304 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2305 XCB_XFIXES_MAJOR_VERSION,
2306 XCB_XFIXES_MINOR_VERSION);
2307 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2308 xfixes_cookie, NULL);
2309
Martin Minarik6d118362012-06-07 18:01:59 +02002310 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002311 xfixes_reply->major_version, xfixes_reply->minor_version);
2312
2313 free(xfixes_reply);
2314
2315 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2316 formats_cookie, 0);
2317 if (formats_reply == NULL)
2318 return;
2319
2320 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002321 for (i = 0; i < formats_reply->num_formats; i++) {
2322 if (formats[i].direct.red_mask != 0xff &&
2323 formats[i].direct.red_shift != 16)
2324 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002325 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2326 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002327 wm->format_rgb = formats[i];
2328 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2329 formats[i].depth == 32 &&
2330 formats[i].direct.alpha_mask == 0xff &&
2331 formats[i].direct.alpha_shift == 24)
2332 wm->format_rgba = formats[i];
2333 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002334
2335 free(formats_reply);
2336}
2337
2338static void
2339weston_wm_create_wm_window(struct weston_wm *wm)
2340{
2341 static const char name[] = "Weston WM";
2342
2343 wm->wm_window = xcb_generate_id(wm->conn);
2344 xcb_create_window(wm->conn,
2345 XCB_COPY_FROM_PARENT,
2346 wm->wm_window,
2347 wm->screen->root,
2348 0, 0,
2349 10, 10,
2350 0,
2351 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2352 wm->screen->root_visual,
2353 0, NULL);
2354
2355 xcb_change_property(wm->conn,
2356 XCB_PROP_MODE_REPLACE,
2357 wm->wm_window,
2358 wm->atom.net_supporting_wm_check,
2359 XCB_ATOM_WINDOW,
2360 32, /* format */
2361 1, &wm->wm_window);
2362
2363 xcb_change_property(wm->conn,
2364 XCB_PROP_MODE_REPLACE,
2365 wm->wm_window,
2366 wm->atom.net_wm_name,
2367 wm->atom.utf8_string,
2368 8, /* format */
2369 strlen(name), name);
2370
2371 xcb_change_property(wm->conn,
2372 XCB_PROP_MODE_REPLACE,
2373 wm->screen->root,
2374 wm->atom.net_supporting_wm_check,
2375 XCB_ATOM_WINDOW,
2376 32, /* format */
2377 1, &wm->wm_window);
2378
Abdur Rehmanb8b150b2017-01-01 19:46:46 +05002379 /* Claim the WM_S0 selection even though we don't support
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002380 * the --replace functionality. */
2381 xcb_set_selection_owner(wm->conn,
2382 wm->wm_window,
2383 wm->atom.wm_s0,
2384 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002385
2386 xcb_set_selection_owner(wm->conn,
2387 wm->wm_window,
2388 wm->atom.net_wm_cm_s0,
2389 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002390}
2391
2392struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002393weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002394{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002395 struct weston_wm *wm;
2396 struct wl_event_loop *loop;
2397 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002398 uint32_t values[1];
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002399 xcb_atom_t supported[6];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002400
Peter Huttererf3d62272013-08-08 11:57:05 +10002401 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002402 if (wm == NULL)
2403 return NULL;
2404
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002405 wm->server = wxs;
2406 wm->window_hash = hash_table_create();
2407 if (wm->window_hash == NULL) {
2408 free(wm);
2409 return NULL;
2410 }
2411
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002412 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002413 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002414 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002415 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002416 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002417 hash_table_destroy(wm->window_hash);
2418 free(wm);
2419 return NULL;
2420 }
2421
2422 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2423 wm->screen = s.data;
2424
2425 loop = wl_display_get_event_loop(wxs->wl_display);
2426 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002427 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002428 WL_EVENT_READABLE,
2429 weston_wm_handle_event, wm);
2430 wl_event_source_check(wm->source);
2431
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002432 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002433 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002434
2435 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002436 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2437 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2438 XCB_EVENT_MASK_PROPERTY_CHANGE;
2439 xcb_change_window_attributes(wm->conn, wm->screen->root,
2440 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002441
2442 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2443 XCB_COMPOSITE_REDIRECT_MANUAL);
2444
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002445 wm->theme = theme_create();
2446
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002447 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002448 supported[1] = wm->atom.net_wm_state;
2449 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002450 supported[3] = wm->atom.net_wm_state_maximized_vert;
2451 supported[4] = wm->atom.net_wm_state_maximized_horz;
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002452 supported[5] = wm->atom.net_active_window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002453 xcb_change_property(wm->conn,
2454 XCB_PROP_MODE_REPLACE,
2455 wm->screen->root,
2456 wm->atom.net_supported,
2457 XCB_ATOM_ATOM,
2458 32, /* format */
2459 ARRAY_LENGTH(supported), supported);
2460
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002461 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
2462
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002463 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002464
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002465 weston_wm_dnd_init(wm);
2466
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002467 xcb_flush(wm->conn);
2468
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002469 wm->create_surface_listener.notify = weston_wm_create_surface;
2470 wl_signal_add(&wxs->compositor->create_surface_signal,
2471 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002472 wm->activate_listener.notify = weston_wm_window_activate;
2473 wl_signal_add(&wxs->compositor->activate_signal,
2474 &wm->activate_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002475 wm->kill_listener.notify = weston_wm_kill_client;
2476 wl_signal_add(&wxs->compositor->kill_signal,
2477 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002478 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002479
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002480 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002481 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002482
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002483 /* Create wm window and take WM_S0 selection last, which
2484 * signals to Xwayland that we're done with setup. */
2485 weston_wm_create_wm_window(wm);
2486
2487 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002488
2489 return wm;
2490}
2491
2492void
2493weston_wm_destroy(struct weston_wm *wm)
2494{
2495 /* FIXME: Free windows in hash. */
2496 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002497 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002498 xcb_disconnect(wm->conn);
2499 wl_event_source_remove(wm->source);
2500 wl_list_remove(&wm->selection_listener.link);
2501 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002502 wl_list_remove(&wm->kill_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002503 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002504
2505 free(wm);
2506}
2507
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002508static struct weston_wm_window *
2509get_wm_window(struct weston_surface *surface)
2510{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002511 struct wl_listener *listener;
2512
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002513 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002514 if (listener)
2515 return container_of(listener, struct weston_wm_window,
2516 surface_destroy_listener);
2517
2518 return NULL;
2519}
2520
Quentin Glidic955cec02016-08-12 10:41:35 +02002521static bool
2522is_wm_window(struct weston_surface *surface)
2523{
2524 return get_wm_window(surface) != NULL;
2525}
2526
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002527static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002528weston_wm_window_configure(void *data)
2529{
2530 struct weston_wm_window *window = data;
2531 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002532 uint32_t values[4];
2533 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002534
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002535 weston_wm_window_get_child_position(window, &x, &y);
2536 values[0] = x;
2537 values[1] = y;
2538 values[2] = window->width;
2539 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002540 xcb_configure_window(wm->conn,
2541 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002542 XCB_CONFIG_WINDOW_X |
2543 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002544 XCB_CONFIG_WINDOW_WIDTH |
2545 XCB_CONFIG_WINDOW_HEIGHT,
2546 values);
2547
2548 weston_wm_window_get_frame_size(window, &width, &height);
2549 values[0] = width;
2550 values[1] = height;
2551 xcb_configure_window(wm->conn,
2552 window->frame_id,
2553 XCB_CONFIG_WINDOW_WIDTH |
2554 XCB_CONFIG_WINDOW_HEIGHT,
2555 values);
2556
2557 window->configure_source = NULL;
2558
2559 weston_wm_window_schedule_repaint(window);
2560}
2561
2562static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002563send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002564{
2565 struct weston_wm_window *window = get_wm_window(surface);
2566 struct weston_wm *wm = window->wm;
2567 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002568 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002569
Marek Chalupae9fe4672014-10-29 13:44:44 +01002570 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002571 hborder = 2 * t->width;
2572 vborder = t->titlebar_height + t->width;
2573 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002574 hborder = 0;
2575 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002576 }
2577
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002578 if (width > hborder)
2579 window->width = width - hborder;
2580 else
2581 window->width = 1;
2582
2583 if (height > vborder)
2584 window->height = height - vborder;
2585 else
2586 window->height = 1;
2587
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002588 if (window->frame)
2589 frame_resize_inside(window->frame, window->width, window->height);
2590
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002591 if (window->configure_source)
2592 return;
2593
2594 window->configure_source =
2595 wl_event_loop_add_idle(wm->server->loop,
2596 weston_wm_window_configure, window);
2597}
2598
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002599static void
2600send_position(struct weston_surface *surface, int32_t x, int32_t y)
2601{
2602 struct weston_wm_window *window = get_wm_window(surface);
2603 struct weston_wm *wm;
2604 uint32_t mask, values[2];
2605
2606 if (!window || !window->wm)
2607 return;
2608
2609 wm = window->wm;
2610 /* We use pos_dirty to tell whether a configure message is in flight.
2611 * This is needed in case we send two configure events in a very
2612 * short time, since window->x/y is set in after a roundtrip, hence
2613 * we cannot just check if the current x and y are different. */
2614 if (window->x != x || window->y != y || window->pos_dirty) {
2615 window->pos_dirty = true;
2616 values[0] = x;
2617 values[1] = y;
2618 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
2619
2620 xcb_configure_window(wm->conn, window->frame_id, mask, values);
2621 xcb_flush(wm->conn);
2622 }
2623}
2624
Quentin Glidic955cec02016-08-12 10:41:35 +02002625static const struct weston_xwayland_client_interface shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002626 send_configure,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002627};
2628
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002629static int
2630legacy_fullscreen(struct weston_wm *wm,
2631 struct weston_wm_window *window,
2632 struct weston_output **output_ret)
2633{
2634 struct weston_compositor *compositor = wm->server->compositor;
2635 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002636 uint32_t minmax = PMinSize | PMaxSize;
2637 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002638
2639 /* Heuristics for detecting legacy fullscreen windows... */
2640
2641 wl_list_for_each(output, &compositor->output_list, link) {
2642 if (output->x == window->x &&
2643 output->y == window->y &&
2644 output->width == window->width &&
2645 output->height == window->height &&
2646 window->override_redirect) {
2647 *output_ret = output;
2648 return 1;
2649 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002650
2651 matching_size = 0;
2652 if ((window->size_hints.flags & (USSize |PSize)) &&
2653 window->size_hints.width == output->width &&
2654 window->size_hints.height == output->height)
2655 matching_size = 1;
2656 if ((window->size_hints.flags & minmax) == minmax &&
2657 window->size_hints.min_width == output->width &&
2658 window->size_hints.min_height == output->height &&
2659 window->size_hints.max_width == output->width &&
2660 window->size_hints.max_height == output->height)
2661 matching_size = 1;
2662
2663 if (matching_size && !window->decorate &&
2664 (window->size_hints.flags & (USPosition | PPosition)) &&
2665 window->size_hints.x == output->x &&
2666 window->size_hints.y == output->y) {
2667 *output_ret = output;
2668 return 1;
2669 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002670 }
2671
2672 return 0;
2673}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002674
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002675static bool
Pekka Paalanen882aff02016-11-16 14:15:18 +02002676weston_wm_window_is_positioned(struct weston_wm_window *window)
2677{
2678 if (window->map_request_x == INT_MIN ||
2679 window->map_request_y == INT_MIN)
2680 weston_log("XWM warning: win %d did not see map request\n",
2681 window->id);
2682
2683 return window->map_request_x != 0 || window->map_request_y != 0;
2684}
2685
2686static bool
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002687weston_wm_window_type_inactive(struct weston_wm_window *window)
2688{
2689 struct weston_wm *wm = window->wm;
2690
2691 return window->type == wm->atom.net_wm_window_type_tooltip ||
2692 window->type == wm->atom.net_wm_window_type_dropdown ||
2693 window->type == wm->atom.net_wm_window_type_dnd ||
2694 window->type == wm->atom.net_wm_window_type_combo ||
Giulio Camuffo84787ea2015-04-29 19:00:48 +03002695 window->type == wm->atom.net_wm_window_type_popup ||
2696 window->type == wm->atom.net_wm_window_type_utility;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002697}
2698
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002699static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002700xserver_map_shell_surface(struct weston_wm_window *window,
2701 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002702{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002703 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +02002704 struct weston_desktop_xwayland *xwayland =
2705 wm->server->compositor->xwayland;
2706 const struct weston_desktop_xwayland_interface *xwayland_interface =
2707 wm->server->compositor->xwayland_interface;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002708 struct weston_wm_window *parent;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002709
Pekka Paalanen0bbe6242016-12-21 13:41:34 +02002710 /* This should be necessary only for override-redirected windows,
2711 * because otherwise MapRequest handler would have already updated
2712 * the properties. However, if X11 clients set properties after
2713 * sending MapWindow, here we can still process them. The decorations
2714 * have already been drawn once with the old property values, so if the
2715 * app changes something affecting decor after MapWindow, we glitch.
2716 * We only hit xserver_map_shell_surface() once per MapWindow and
2717 * wl_surface, so better ensure we get the window type right.
2718 */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002719 weston_wm_window_read_properties(window);
2720
2721 /* A weston_wm_window may have many different surfaces assigned
2722 * throughout its life, so we must make sure to remove the listener
2723 * from the old surface signal list. */
2724 if (window->surface)
2725 wl_list_remove(&window->surface_destroy_listener.link);
2726
2727 window->surface = surface;
2728 window->surface_destroy_listener.notify = surface_destroy;
2729 wl_signal_add(&window->surface->destroy_signal,
2730 &window->surface_destroy_listener);
2731
Quentin Glidic955cec02016-08-12 10:41:35 +02002732 if (!xwayland_interface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002733 return;
2734
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002735 if (window->surface->committed) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03002736 weston_log("warning, unexpected in %s: "
2737 "surface's configure hook is already set.\n",
2738 __func__);
2739 return;
2740 }
2741
Murray Calavera883ac022015-06-06 13:02:22 +00002742 window->shsurf =
Quentin Glidic955cec02016-08-12 10:41:35 +02002743 xwayland_interface->create_surface(xwayland,
2744 window->surface,
2745 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002746
Pekka Paalanen9a5fab02016-12-20 16:15:02 +02002747 wm_log("XWM: map shell surface, win %d, weston_surface %p, xwayland surface %p\n",
2748 window->id, window->surface, window->shsurf);
Pekka Paalanena04eacc2016-11-28 16:42:25 +02002749
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002750 if (window->name)
Quentin Glidic955cec02016-08-12 10:41:35 +02002751 xwayland_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002752 if (window->pid > 0)
Quentin Glidic955cec02016-08-12 10:41:35 +02002753 xwayland_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002754
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002755 if (window->fullscreen) {
2756 window->saved_width = window->width;
2757 window->saved_height = window->height;
Pekka Paalanen505237e2016-12-01 15:41:11 +02002758 xwayland_interface->set_fullscreen(window->shsurf,
2759 window->legacy_fullscreen_output.output);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002760 return;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002761 } else if (window->override_redirect) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002762 xwayland_interface->set_xwayland(window->shsurf,
2763 window->x, window->y);
Axel Davye4450f92014-01-12 15:06:05 +01002764 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002765 parent = window->transient_for;
Quentin Glidic955cec02016-08-12 10:41:35 +02002766 if (weston_wm_window_type_inactive(window)) {
2767 xwayland_interface->set_transient(window->shsurf,
2768 parent->surface,
2769 window->x - parent->x,
2770 window->y - parent->y);
2771 } else {
2772 xwayland_interface->set_toplevel(window->shsurf);
2773 xwayland_interface->set_parent(window->shsurf,
2774 parent->surface);
2775 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002776 } else if (weston_wm_window_is_maximized(window)) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002777 xwayland_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002778 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002779 if (weston_wm_window_type_inactive(window)) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002780 xwayland_interface->set_xwayland(window->shsurf,
2781 window->x,
2782 window->y);
Pekka Paalanen882aff02016-11-16 14:15:18 +02002783 } else if (weston_wm_window_is_positioned(window)) {
2784 xwayland_interface->set_toplevel_with_position(window->shsurf,
2785 window->map_request_x,
2786 window->map_request_y);
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002787 } else {
Quentin Glidic955cec02016-08-12 10:41:35 +02002788 xwayland_interface->set_toplevel(window->shsurf);
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002789 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002790 }
Pekka Paalanenad0da452017-01-18 15:37:57 +02002791
Pekka Paalanen7ace8312017-01-18 15:37:58 +02002792 if (window->frame_id == XCB_WINDOW_NONE) {
Pekka Paalanenad0da452017-01-18 15:37:57 +02002793 weston_wm_window_set_pending_state_OR(window);
Pekka Paalanen7ace8312017-01-18 15:37:58 +02002794 } else {
Pekka Paalanenad0da452017-01-18 15:37:57 +02002795 weston_wm_window_set_pending_state(window);
Pekka Paalanen7ace8312017-01-18 15:37:58 +02002796 weston_wm_window_set_allow_commits(window, true);
2797 xcb_flush(wm->conn);
2798 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002799}
Quentin Glidic955cec02016-08-12 10:41:35 +02002800
2801const struct weston_xwayland_surface_api surface_api = {
2802 is_wm_window,
2803 send_position,
2804};