blob: 260807593d7b0a37bbe8597c0d91d4bc03b0f5bf [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;
742 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400743
Pekka Paalanen73428a82016-12-19 15:10:01 +0200744 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d%s\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400745 configure_notify->window,
746 configure_notify->x, configure_notify->y,
Pekka Paalanen73428a82016-12-19 15:10:01 +0200747 configure_notify->width, configure_notify->height,
748 configure_notify->override_redirect ? ", override" : "");
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300749
Derek Foreman49372142015-04-09 10:51:22 -0500750 if (!wm_lookup_window(wm, configure_notify->window, &window))
751 return;
752
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700753 window->x = configure_notify->x;
754 window->y = configure_notify->y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200755 window->pos_dirty = false;
756
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700757 if (window->override_redirect) {
758 window->width = configure_notify->width;
759 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500760 if (window->frame)
761 frame_resize_inside(window->frame,
762 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700763 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400764}
765
766static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300767weston_wm_kill_client(struct wl_listener *listener, void *data)
768{
769 struct weston_surface *surface = data;
770 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300771 if (!window)
772 return;
773
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200774 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300775 kill(window->pid, SIGKILL);
776}
777
778static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700779weston_wm_create_surface(struct wl_listener *listener, void *data)
780{
781 struct weston_surface *surface = data;
782 struct weston_wm *wm =
783 container_of(listener,
784 struct weston_wm, create_surface_listener);
785 struct weston_wm_window *window;
786
787 if (wl_resource_get_client(surface->resource) != wm->server->client)
788 return;
789
Pekka Paalanen9a5fab02016-12-20 16:15:02 +0200790 wm_log("XWM: create weston_surface %p\n", surface);
791
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700792 wl_list_for_each(window, &wm->unpaired_window_list, link)
793 if (window->surface_id ==
794 wl_resource_get_id(surface->resource)) {
795 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700796 window->surface_id = 0;
797 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700798 break;
Murray Calavera883ac022015-06-06 13:02:22 +0000799 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700800}
801
802static void
Giulio Camuffob18f7882015-03-29 14:20:23 +0300803weston_wm_send_focus_window(struct weston_wm *wm,
804 struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400805{
Scott Moreau85ecac02012-05-21 15:49:14 -0600806 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400807
Scott Moreau85ecac02012-05-21 15:49:14 -0600808 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700809 uint32_t values[1];
810
Boyan Dingb9f863c2014-08-30 10:33:23 +0800811 if (window->override_redirect)
812 return;
813
Scott Moreau85ecac02012-05-21 15:49:14 -0600814 client_message.response_type = XCB_CLIENT_MESSAGE;
815 client_message.format = 32;
816 client_message.window = window->id;
817 client_message.type = wm->atom.wm_protocols;
818 client_message.data.data32[0] = wm->atom.wm_take_focus;
819 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
820
Murray Calavera883ac022015-06-06 13:02:22 +0000821 xcb_send_event(wm->conn, 0, window->id,
Scott Moreau85ecac02012-05-21 15:49:14 -0600822 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
823 (char *) &client_message);
824
825 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
826 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700827
828 values[0] = XCB_STACK_MODE_ABOVE;
829 xcb_configure_window (wm->conn, window->frame_id,
830 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600831 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400832 xcb_set_input_focus (wm->conn,
833 XCB_INPUT_FOCUS_POINTER_ROOT,
834 XCB_NONE,
835 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600836 }
Giulio Camuffob18f7882015-03-29 14:20:23 +0300837}
838
839static void
840weston_wm_window_activate(struct wl_listener *listener, void *data)
841{
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +0800842 struct weston_surface_activation_data *activation_data = data;
843 struct weston_surface *surface = activation_data->surface;
Giulio Camuffob18f7882015-03-29 14:20:23 +0300844 struct weston_wm_window *window = NULL;
845 struct weston_wm *wm =
846 container_of(listener, struct weston_wm, activate_listener);
847
848 if (surface) {
849 window = get_wm_window(surface);
850 }
851
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200852 if (window) {
853 weston_wm_set_net_active_window(wm, window->id);
854 } else {
855 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
856 }
857
Giulio Camuffob18f7882015-03-29 14:20:23 +0300858 weston_wm_send_focus_window(wm, window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400859
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500860 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800861 if (wm->focus_window->frame)
862 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400863 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500864 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400865 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500866 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800867 if (wm->focus_window->frame)
868 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400869 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500870 }
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200871
872 xcb_flush(wm->conn);
873
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400874}
875
Pekka Paalanen7ace8312017-01-18 15:37:58 +0200876/** Control Xwayland wl_surface.commit behaviour
877 *
878 * This function sets the "_XWAYLAND_ALLOW_COMMITS" property of the frame window
879 * (not the content window!) to \p allow.
880 *
881 * If the property is set to \c true, Xwayland will commit whenever it likes.
882 * If the property is set to \c false, Xwayland will not commit.
883 * If the property is not set at all, Xwayland assumes it is \c true.
884 *
885 * \param window The XWM window to control.
886 * \param allow Whether Xwayland is allowed to wl_surface.commit for the window.
887 */
888static void
889weston_wm_window_set_allow_commits(struct weston_wm_window *window, bool allow)
890{
891 struct weston_wm *wm = window->wm;
892 uint32_t property[1];
893
894 assert(window->frame_id != XCB_WINDOW_NONE);
895
896 property[0] = allow ? 1 : 0;
897
898 xcb_change_property(wm->conn,
899 XCB_PROP_MODE_REPLACE,
900 window->frame_id,
901 wm->atom.allow_commits,
902 XCB_ATOM_CARDINAL,
903 32, /* format */
904 1, property);
905}
906
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400907#define ICCCM_WITHDRAWN_STATE 0
908#define ICCCM_NORMAL_STATE 1
909#define ICCCM_ICONIC_STATE 3
910
911static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500912weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400913{
914 struct weston_wm *wm = window->wm;
915 uint32_t property[2];
916
917 property[0] = state;
918 property[1] = XCB_WINDOW_NONE;
919
920 xcb_change_property(wm->conn,
921 XCB_PROP_MODE_REPLACE,
922 window->id,
923 wm->atom.wm_state,
924 wm->atom.wm_state,
925 32, /* format */
926 2, property);
927}
928
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400929static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500930weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
931{
932 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200933 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500934 int i;
935
936 i = 0;
937 if (window->fullscreen)
938 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200939 if (window->maximized_vert)
940 property[i++] = wm->atom.net_wm_state_maximized_vert;
941 if (window->maximized_horz)
942 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500943
944 xcb_change_property(wm->conn,
945 XCB_PROP_MODE_REPLACE,
946 window->id,
947 wm->atom.net_wm_state,
948 XCB_ATOM_ATOM,
949 32, /* format */
950 i, property);
951}
952
953static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700954weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400955{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700956 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400957 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400958 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200959 int buttons = FRAME_BUTTON_CLOSE;
960
961 if (window->decorate & MWM_DECOR_MAXIMIZE)
962 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400963
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500964 window->frame = frame_create(window->wm->theme,
965 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200966 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500967 frame_resize_inside(window->frame, window->width, window->height);
968
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400969 weston_wm_window_get_frame_size(window, &width, &height);
970 weston_wm_window_get_child_position(window, &x, &y);
971
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400972 values[0] = wm->screen->black_pixel;
973 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400974 XCB_EVENT_MASK_KEY_PRESS |
975 XCB_EVENT_MASK_KEY_RELEASE |
976 XCB_EVENT_MASK_BUTTON_PRESS |
977 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400978 XCB_EVENT_MASK_POINTER_MOTION |
979 XCB_EVENT_MASK_ENTER_WINDOW |
980 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400981 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400982 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400983 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400984
985 window->frame_id = xcb_generate_id(wm->conn);
986 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400987 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400988 window->frame_id,
989 wm->screen->root,
990 0, 0,
991 width, height,
992 0,
993 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400994 wm->visual_id,
995 XCB_CW_BORDER_PIXEL |
996 XCB_CW_EVENT_MASK |
997 XCB_CW_COLORMAP, values);
998
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400999 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
1000
1001 values[0] = 0;
1002 xcb_configure_window(wm->conn, window->id,
1003 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
1004
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001005 window->cairo_surface =
1006 cairo_xcb_surface_create_with_xrender_format(wm->conn,
1007 wm->screen,
1008 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001009 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001010 width, height);
1011
1012 hash_table_insert(wm->window_hash, window->frame_id, window);
1013}
1014
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001015/*
1016 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
1017 * Passing a <0 desktop value deletes the property.
1018 */
1019static void
1020weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
Bryce Harringtone00554b2015-06-12 10:17:39 -07001021 int desktop)
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001022{
1023 if (desktop >= 0) {
1024 xcb_change_property(window->wm->conn,
1025 XCB_PROP_MODE_REPLACE,
1026 window->id,
1027 window->wm->atom.net_wm_desktop,
1028 XCB_ATOM_CARDINAL,
1029 32, /* format */
1030 1, &desktop);
1031 } else {
1032 xcb_delete_property(window->wm->conn,
1033 window->id,
1034 window->wm->atom.net_wm_desktop);
1035 }
1036}
1037
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001038static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001039weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
1040{
1041 xcb_map_request_event_t *map_request =
1042 (xcb_map_request_event_t *) event;
1043 struct weston_wm_window *window;
Pekka Paalanen505237e2016-12-01 15:41:11 +02001044 struct weston_output *output;
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001045
1046 if (our_resource(wm, map_request->window)) {
1047 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
1048 map_request->window);
1049 return;
1050 }
1051
Derek Foreman49372142015-04-09 10:51:22 -05001052 if (!wm_lookup_window(wm, map_request->window, &window))
1053 return;
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001054
1055 weston_wm_window_read_properties(window);
1056
Pekka Paalanenc9ca2c02016-12-15 16:04:14 +02001057 /* For a new Window, MapRequest happens before the Window is realized
1058 * in Xwayland. We do the real xcb_map_window() here as a response to
1059 * MapRequest. The Window will get realized (wl_surface created in
1060 * Wayland and WL_SURFACE_ID sent in X11) when it has been mapped for
1061 * real.
1062 *
1063 * MapRequest only happens for (X11) unmapped Windows. On UnmapNotify,
1064 * we reset shsurf to NULL, so even if X11 connection races far ahead
1065 * of the Wayland connection and the X11 client is repeatedly mapping
1066 * and unmapping, we will never have shsurf set on MapRequest.
1067 */
1068 assert(!window->shsurf);
1069
Pekka Paalanen882aff02016-11-16 14:15:18 +02001070 window->map_request_x = window->x;
1071 window->map_request_y = window->y;
1072
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001073 if (window->frame_id == XCB_WINDOW_NONE)
Pekka Paalanen9f5a1032016-12-20 15:31:15 +02001074 weston_wm_window_create_frame(window); /* sets frame_id */
1075 assert(window->frame_id != XCB_WINDOW_NONE);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001076
Pekka Paalanen44660c32016-11-14 15:38:43 +02001077 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d, %dx%d @ %d,%d)\n",
1078 window->id, window, window->frame_id,
Pekka Paalanen882aff02016-11-16 14:15:18 +02001079 window->width, window->height,
1080 window->map_request_x, window->map_request_y);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001081
Pekka Paalanen7ace8312017-01-18 15:37:58 +02001082 weston_wm_window_set_allow_commits(window, false);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001083 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
1084 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001085 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001086
Pekka Paalanen505237e2016-12-01 15:41:11 +02001087 if (legacy_fullscreen(wm, window, &output)) {
1088 window->fullscreen = 1;
1089 weston_output_weak_ref_set(&window->legacy_fullscreen_output,
1090 output);
1091 }
1092
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001093 xcb_map_window(wm->conn, map_request->window);
1094 xcb_map_window(wm->conn, window->frame_id);
Pekka Paalanen9f5a1032016-12-20 15:31:15 +02001095
1096 /* Mapped in the X server, we can draw immediately.
1097 * Cannot set pending state though, no weston_surface until
1098 * xserver_map_shell_surface() time. */
1099 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001100}
1101
1102static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001103weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1104{
1105 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
1106
1107 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001108 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
1109 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001110 return;
1111 }
1112
Pekka Paalanen73428a82016-12-19 15:10:01 +02001113 wm_log("XCB_MAP_NOTIFY (window %d%s)\n", map_notify->window,
1114 map_notify->override_redirect ? ", override" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001115}
1116
1117static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001118weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1119{
1120 xcb_unmap_notify_event_t *unmap_notify =
1121 (xcb_unmap_notify_event_t *) event;
1122 struct weston_wm_window *window;
1123
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001124 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
1125 unmap_notify->window,
1126 unmap_notify->event,
1127 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001128
1129 if (our_resource(wm, unmap_notify->window))
1130 return;
1131
MoD31700122013-06-11 19:58:55 -05001132 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -04001133 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1134 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001135 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001136
Derek Foreman49372142015-04-09 10:51:22 -05001137 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1138 return;
1139
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001140 if (window->surface_id) {
1141 /* Make sure we're not on the unpaired surface list or we
1142 * could be assigned a surface during surface creation that
1143 * was mapped before this unmap request.
1144 */
1145 wl_list_remove(&window->link);
1146 window->surface_id = 0;
1147 }
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001148 if (wm->focus_window == window)
1149 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001150 if (window->surface)
1151 wl_list_remove(&window->surface_destroy_listener.link);
1152 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +02001153 window->shsurf = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001154
1155 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1156 weston_wm_window_set_virtual_desktop(window, -1);
1157
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001158 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001159}
1160
1161static void
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001162weston_wm_window_draw_decoration(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001163{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001164 cairo_t *cr;
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001165 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001166
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001167 wm_log("XWM: draw decoration, win %d\n", window->id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001168
1169 weston_wm_window_get_frame_size(window, &width, &height);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001170
1171 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1172 cr = cairo_create(window->cairo_surface);
1173
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001174 if (window->fullscreen) {
1175 /* nothing */
1176 } else if (window->decorate) {
Pekka Paalanen9a330e12016-12-15 15:37:24 +02001177 frame_set_title(window->frame, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001178 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001179 } else {
1180 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1181 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1182 cairo_paint(cr);
1183
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001184 render_shadow(cr, window->wm->theme->shadow,
1185 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001186 }
1187
1188 cairo_destroy(cr);
Pekka Paalanendb7b9f32017-01-16 17:01:11 +02001189 cairo_surface_flush(window->cairo_surface);
1190 xcb_flush(window->wm->conn);
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001191}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001192
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001193static void
1194weston_wm_window_set_pending_state(struct weston_wm_window *window)
1195{
1196 int x, y, width, height;
1197 int32_t input_x, input_y, input_w, input_h;
1198 const struct weston_desktop_xwayland_interface *xwayland_interface =
1199 window->wm->server->compositor->xwayland_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001200
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001201 if (!window->surface)
1202 return;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001203
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001204 weston_wm_window_get_frame_size(window, &width, &height);
1205 weston_wm_window_get_child_position(window, &x, &y);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001206
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001207 pixman_region32_fini(&window->surface->pending.opaque);
1208 if (window->has_alpha) {
1209 pixman_region32_init(&window->surface->pending.opaque);
1210 } else {
1211 /* We leave an extra pixel around the X window area to
1212 * make sure we don't sample from the undefined alpha
1213 * channel when filtering. */
1214 pixman_region32_init_rect(&window->surface->pending.opaque,
1215 x - 1, y - 1,
1216 window->width + 2,
1217 window->height + 2);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001218 }
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001219
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001220 if (window->decorate && !window->fullscreen) {
1221 frame_input_rect(window->frame, &input_x, &input_y,
1222 &input_w, &input_h);
1223 } else {
1224 input_x = x;
1225 input_y = y;
1226 input_w = width;
1227 input_h = height;
1228 }
1229
1230 wm_log("XWM: win %d geometry: %d,%d %dx%d\n",
1231 window->id, input_x, input_y, input_w, input_h);
1232
Pekka Paalanen83626b92016-12-20 14:01:42 +02001233 pixman_region32_fini(&window->surface->pending.input);
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001234 pixman_region32_init_rect(&window->surface->pending.input,
1235 input_x, input_y, input_w, input_h);
1236
1237 xwayland_interface->set_window_geometry(window->shsurf,
Pekka Paalanen83626b92016-12-20 14:01:42 +02001238 input_x, input_y,
1239 input_w, input_h);
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001240 if (window->name)
1241 xwayland_interface->set_title(window->shsurf, window->name);
1242 if (window->pid > 0)
1243 xwayland_interface->set_pid(window->shsurf, window->pid);
1244}
1245
1246static void
1247weston_wm_window_do_repaint(void *data)
1248{
1249 struct weston_wm_window *window = data;
1250
1251 window->repaint_source = NULL;
1252
1253 weston_wm_window_read_properties(window);
1254
1255 weston_wm_window_draw_decoration(window);
1256 weston_wm_window_set_pending_state(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001257}
1258
1259static void
Pekka Paalanened568832016-12-20 14:05:03 +02001260weston_wm_window_set_pending_state_OR(struct weston_wm_window *window)
1261{
1262 int width, height;
1263
1264 /* for override-redirect windows */
1265 assert(window->frame_id == XCB_WINDOW_NONE);
1266
1267 if (!window->surface)
1268 return;
1269
1270 weston_wm_window_get_frame_size(window, &width, &height);
1271 pixman_region32_fini(&window->surface->pending.opaque);
1272 if (window->has_alpha) {
1273 pixman_region32_init(&window->surface->pending.opaque);
1274 } else {
1275 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1276 width, height);
1277 }
1278}
1279
1280static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001281weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1282{
1283 struct weston_wm *wm = window->wm;
1284
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001285 if (window->frame_id == XCB_WINDOW_NONE) {
Pekka Paalanened568832016-12-20 14:05:03 +02001286 /* Override-redirect windows go through here, but we
1287 * cannot assert(window->override_redirect); because
1288 * we do not deal with changing OR flag yet.
1289 * XXX: handle OR flag changes in message handlers
1290 */
1291 weston_wm_window_set_pending_state_OR(window);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001292 return;
1293 }
1294
1295 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001296 return;
1297
Pekka Paalanena04eacc2016-11-28 16:42:25 +02001298 wm_log("XWM: schedule repaint, win %d\n", window->id);
1299
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001300 window->repaint_source =
1301 wl_event_loop_add_idle(wm->server->loop,
Pekka Paalanenaabf43d2016-12-19 17:10:25 +02001302 weston_wm_window_do_repaint, window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001303}
1304
1305static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001306weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1307{
1308 xcb_property_notify_event_t *property_notify =
1309 (xcb_property_notify_event_t *) event;
1310 struct weston_wm_window *window;
1311
Derek Foreman49372142015-04-09 10:51:22 -05001312 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001313 return;
1314
1315 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001316
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001317 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001318 if (property_notify->state == XCB_PROPERTY_DELETE)
Pekka Paalanen8cc153b2016-12-19 15:18:45 +02001319 wm_log_continue("deleted %s\n",
1320 get_atom_name(wm->conn, property_notify->atom));
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001321 else
1322 read_and_dump_property(wm, property_notify->window,
1323 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001324
1325 if (property_notify->atom == wm->atom.net_wm_name ||
1326 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001327 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001328}
1329
1330static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001331weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001332 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001333{
1334 struct weston_wm_window *window;
1335 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001336 xcb_get_geometry_cookie_t geometry_cookie;
1337 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001338
Peter Huttererf3d62272013-08-08 11:57:05 +10001339 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001340 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001341 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001342 return;
1343 }
1344
MoD384a11a2013-06-22 11:04:21 -05001345 geometry_cookie = xcb_get_geometry(wm->conn, id);
1346
Giulio Camuffob18f7882015-03-29 14:20:23 +03001347 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
1348 XCB_EVENT_MASK_FOCUS_CHANGE;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001349 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1350
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001351 window->wm = wm;
1352 window->id = id;
1353 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001354 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001355 window->width = width;
1356 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001357 window->x = x;
1358 window->y = y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001359 window->pos_dirty = false;
Pekka Paalanen882aff02016-11-16 14:15:18 +02001360 window->map_request_x = INT_MIN; /* out of range for valid positions */
1361 window->map_request_y = INT_MIN; /* out of range for valid positions */
Pekka Paalanen505237e2016-12-01 15:41:11 +02001362 weston_output_weak_ref_init(&window->legacy_fullscreen_output);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001363
MoD384a11a2013-06-22 11:04:21 -05001364 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1365 /* technically we should use XRender and check the visual format's
1366 alpha_mask, but checking depth is simpler and works in all known cases */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001367 if (geometry_reply != NULL)
MoD384a11a2013-06-22 11:04:21 -05001368 window->has_alpha = geometry_reply->depth == 32;
1369 free(geometry_reply);
1370
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001371 hash_table_insert(wm->window_hash, id, window);
1372}
1373
1374static void
1375weston_wm_window_destroy(struct weston_wm_window *window)
1376{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001377 struct weston_wm *wm = window->wm;
1378
Pekka Paalanen505237e2016-12-01 15:41:11 +02001379 weston_output_weak_ref_clear(&window->legacy_fullscreen_output);
1380
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001381 if (window->repaint_source)
1382 wl_event_source_remove(window->repaint_source);
1383 if (window->cairo_surface)
1384 cairo_surface_destroy(window->cairo_surface);
1385
1386 if (window->frame_id) {
1387 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1388 xcb_destroy_window(wm->conn, window->frame_id);
1389 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001390 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001391 hash_table_remove(wm->window_hash, window->frame_id);
1392 window->frame_id = XCB_WINDOW_NONE;
1393 }
1394
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001395 if (window->surface_id)
1396 wl_list_remove(&window->link);
1397
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001398 if (window->surface)
1399 wl_list_remove(&window->surface_destroy_listener.link);
1400
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001401 hash_table_remove(window->wm->window_hash, window->id);
1402 free(window);
1403}
1404
1405static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001406weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1407{
1408 xcb_create_notify_event_t *create_notify =
1409 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001410
Pekka Paalanen7db6c432016-11-14 14:30:57 +02001411 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 -04001412 create_notify->window,
Pekka Paalanen7db6c432016-11-14 14:30:57 +02001413 create_notify->x, create_notify->y,
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001414 create_notify->width, create_notify->height,
1415 create_notify->override_redirect ? ", override" : "",
1416 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001417
1418 if (our_resource(wm, create_notify->window))
1419 return;
1420
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001421 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001422 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001423 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001424 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001425}
1426
1427static void
1428weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1429{
1430 xcb_destroy_notify_event_t *destroy_notify =
1431 (xcb_destroy_notify_event_t *) event;
1432 struct weston_wm_window *window;
1433
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001434 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1435 destroy_notify->window,
1436 destroy_notify->event,
1437 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001438
1439 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001440 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001441
Derek Foreman49372142015-04-09 10:51:22 -05001442 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1443 return;
1444
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001445 weston_wm_window_destroy(window);
1446}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001447
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001448static void
1449weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1450{
1451 xcb_reparent_notify_event_t *reparent_notify =
1452 (xcb_reparent_notify_event_t *) event;
1453 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001454
Pekka Paalanen73428a82016-12-19 15:10:01 +02001455 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d%s)\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001456 reparent_notify->window,
1457 reparent_notify->parent,
Pekka Paalanen73428a82016-12-19 15:10:01 +02001458 reparent_notify->event,
1459 reparent_notify->override_redirect ? ", override" : "");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001460
1461 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001462 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001463 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001464 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001465 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001466 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1467 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001468 weston_wm_window_destroy(window);
1469 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001470}
1471
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001472struct weston_seat *
1473weston_wm_pick_seat(struct weston_wm *wm)
1474{
Tom Hochsteine7fff212016-11-01 14:14:00 -05001475 struct wl_list *seats = wm->server->compositor->seat_list.next;
1476 if (wl_list_empty(seats))
1477 return NULL;
1478 return container_of(seats, struct weston_seat, link);
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001479}
1480
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001481static struct weston_seat *
1482weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1483{
1484 struct weston_wm *wm = window->wm;
1485 struct weston_seat *seat, *s;
1486
1487 seat = NULL;
1488 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001489 struct weston_pointer *pointer = weston_seat_get_pointer(s);
1490 struct weston_pointer *old_pointer =
1491 weston_seat_get_pointer(seat);
1492
1493 if (pointer && pointer->focus &&
1494 pointer->focus->surface == window->surface &&
1495 pointer->button_count > 0 &&
1496 (!seat ||
1497 pointer->grab_serial -
1498 old_pointer->grab_serial < (1 << 30)))
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001499 seat = s;
1500 }
1501
1502 return seat;
1503}
1504
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001505static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001506weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1507 xcb_client_message_event_t *client_message)
1508{
1509 static const int map[] = {
1510 THEME_LOCATION_RESIZING_TOP_LEFT,
1511 THEME_LOCATION_RESIZING_TOP,
1512 THEME_LOCATION_RESIZING_TOP_RIGHT,
1513 THEME_LOCATION_RESIZING_RIGHT,
1514 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1515 THEME_LOCATION_RESIZING_BOTTOM,
1516 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1517 THEME_LOCATION_RESIZING_LEFT
1518 };
1519
1520 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001521 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Derek Foreman1281a362015-07-31 16:55:32 -05001522 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001523 int detail;
Quentin Glidic955cec02016-08-12 10:41:35 +02001524 const struct weston_desktop_xwayland_interface *xwayland_interface =
1525 wm->server->compositor->xwayland_interface;
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001526
Derek Foreman1281a362015-07-31 16:55:32 -05001527 if (!pointer || pointer->button_count != 1
1528 || !pointer->focus
1529 || pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001530 return;
1531
1532 detail = client_message->data.data32[2];
1533 switch (detail) {
1534 case _NET_WM_MOVERESIZE_MOVE:
Quentin Glidic955cec02016-08-12 10:41:35 +02001535 xwayland_interface->move(window->shsurf, pointer);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001536 break;
1537 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1538 case _NET_WM_MOVERESIZE_SIZE_TOP:
1539 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1540 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1541 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1542 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1543 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1544 case _NET_WM_MOVERESIZE_SIZE_LEFT:
Quentin Glidic955cec02016-08-12 10:41:35 +02001545 xwayland_interface->resize(window->shsurf, pointer, map[detail]);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001546 break;
1547 case _NET_WM_MOVERESIZE_CANCEL:
1548 break;
1549 }
1550}
1551
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001552#define _NET_WM_STATE_REMOVE 0
1553#define _NET_WM_STATE_ADD 1
1554#define _NET_WM_STATE_TOGGLE 2
1555
1556static int
1557update_state(int action, int *state)
1558{
1559 int new_state, changed;
1560
1561 switch (action) {
1562 case _NET_WM_STATE_REMOVE:
1563 new_state = 0;
1564 break;
1565 case _NET_WM_STATE_ADD:
1566 new_state = 1;
1567 break;
1568 case _NET_WM_STATE_TOGGLE:
1569 new_state = !*state;
1570 break;
1571 default:
1572 return 0;
1573 }
1574
1575 changed = (*state != new_state);
1576 *state = new_state;
1577
1578 return changed;
1579}
1580
1581static void
1582weston_wm_window_configure(void *data);
1583
1584static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001585weston_wm_window_set_toplevel(struct weston_wm_window *window)
1586{
Quentin Glidic955cec02016-08-12 10:41:35 +02001587 const struct weston_desktop_xwayland_interface *xwayland_interface =
1588 window->wm->server->compositor->xwayland_interface;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001589
Quentin Glidic955cec02016-08-12 10:41:35 +02001590 xwayland_interface->set_toplevel(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001591 window->width = window->saved_width;
1592 window->height = window->saved_height;
1593 if (window->frame)
1594 frame_resize_inside(window->frame,
1595 window->width,
1596 window->height);
1597 weston_wm_window_configure(window);
1598}
1599
1600static inline bool
1601weston_wm_window_is_maximized(struct weston_wm_window *window)
1602{
1603 return window->maximized_horz && window->maximized_vert;
1604}
1605
1606static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001607weston_wm_window_handle_state(struct weston_wm_window *window,
1608 xcb_client_message_event_t *client_message)
1609{
1610 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +02001611 const struct weston_desktop_xwayland_interface *xwayland_interface =
1612 wm->server->compositor->xwayland_interface;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001613 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001614 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001615
1616 action = client_message->data.data32[0];
1617 property = client_message->data.data32[1];
1618
1619 if (property == wm->atom.net_wm_state_fullscreen &&
1620 update_state(action, &window->fullscreen)) {
1621 weston_wm_window_set_net_wm_state(window);
1622 if (window->fullscreen) {
1623 window->saved_width = window->width;
1624 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001625
1626 if (window->shsurf)
Quentin Glidic955cec02016-08-12 10:41:35 +02001627 xwayland_interface->set_fullscreen(window->shsurf,
1628 NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001629 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001630 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001631 weston_wm_window_set_toplevel(window);
1632 }
1633 } else {
1634 if (property == wm->atom.net_wm_state_maximized_vert &&
1635 update_state(action, &window->maximized_vert))
1636 weston_wm_window_set_net_wm_state(window);
1637 if (property == wm->atom.net_wm_state_maximized_horz &&
1638 update_state(action, &window->maximized_horz))
1639 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001640
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001641 if (maximized != weston_wm_window_is_maximized(window)) {
1642 if (weston_wm_window_is_maximized(window)) {
1643 window->saved_width = window->width;
1644 window->saved_height = window->height;
1645
1646 if (window->shsurf)
Quentin Glidic955cec02016-08-12 10:41:35 +02001647 xwayland_interface->set_maximized(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001648 } else if (window->shsurf) {
1649 weston_wm_window_set_toplevel(window);
1650 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001651 }
1652 }
1653}
1654
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001655static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001656surface_destroy(struct wl_listener *listener, void *data)
1657{
1658 struct weston_wm_window *window =
1659 container_of(listener,
1660 struct weston_wm_window, surface_destroy_listener);
1661
1662 wm_log("surface for xid %d destroyed\n", window->id);
1663
1664 /* This should have been freed by the shell.
1665 * Don't try to use it later. */
1666 window->shsurf = NULL;
1667 window->surface = NULL;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001668}
1669
1670static void
1671weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1672 xcb_client_message_event_t *client_message)
1673{
1674 struct weston_wm *wm = window->wm;
1675 struct wl_resource *resource;
1676
1677 if (window->surface_id != 0) {
1678 wm_log("already have surface id for window %d\n", window->id);
1679 return;
1680 }
1681
1682 /* Xwayland will send the wayland requests to create the
1683 * wl_surface before sending this client message. Even so, we
1684 * can end up handling the X event before the wayland requests
1685 * and thus when we try to look up the surface ID, the surface
1686 * hasn't been created yet. In that case put the window on
1687 * the unpaired window list and continue when the surface gets
1688 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001689 uint32_t id = client_message->data.data32[0];
1690 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001691 if (resource) {
1692 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001693 xserver_map_shell_surface(window,
1694 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001695 }
1696 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001697 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001698 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001699 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001700}
1701
1702static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001703weston_wm_handle_client_message(struct weston_wm *wm,
1704 xcb_generic_event_t *event)
1705{
1706 xcb_client_message_event_t *client_message =
1707 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001708 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001709
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001710 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1711 get_atom_name(wm->conn, client_message->type),
1712 client_message->data.data32[0],
1713 client_message->data.data32[1],
1714 client_message->data.data32[2],
1715 client_message->data.data32[3],
1716 client_message->data.data32[4],
1717 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001718
Jason Ekstrand0250a742014-07-24 13:17:47 -07001719 /* The window may get created and destroyed before we actually
1720 * handle the message. If it doesn't exist, bail.
1721 */
Derek Foreman49372142015-04-09 10:51:22 -05001722 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001723 return;
1724
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001725 if (client_message->type == wm->atom.net_wm_moveresize)
1726 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001727 else if (client_message->type == wm->atom.net_wm_state)
1728 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001729 else if (client_message->type == wm->atom.wl_surface_id)
1730 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001731}
1732
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001733enum cursor_type {
1734 XWM_CURSOR_TOP,
1735 XWM_CURSOR_BOTTOM,
1736 XWM_CURSOR_LEFT,
1737 XWM_CURSOR_RIGHT,
1738 XWM_CURSOR_TOP_LEFT,
1739 XWM_CURSOR_TOP_RIGHT,
1740 XWM_CURSOR_BOTTOM_LEFT,
1741 XWM_CURSOR_BOTTOM_RIGHT,
1742 XWM_CURSOR_LEFT_PTR,
1743};
1744
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001745/*
1746 * The following correspondences between file names and cursors was copied
1747 * from: https://bugs.kde.org/attachment.cgi?id=67313
1748 */
1749
1750static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001751 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001752 "sw-resize",
1753 "size_bdiag"
1754};
1755
1756static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001757 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001758 "se-resize",
1759 "size_fdiag"
1760};
1761
1762static const char *bottom_sides[] = {
1763 "bottom_side",
1764 "s-resize",
1765 "size_ver"
1766};
1767
1768static const char *left_ptrs[] = {
1769 "left_ptr",
1770 "default",
1771 "top_left_arrow",
1772 "left-arrow"
1773};
1774
1775static const char *left_sides[] = {
1776 "left_side",
1777 "w-resize",
1778 "size_hor"
1779};
1780
1781static const char *right_sides[] = {
1782 "right_side",
1783 "e-resize",
1784 "size_hor"
1785};
1786
1787static const char *top_left_corners[] = {
1788 "top_left_corner",
1789 "nw-resize",
1790 "size_fdiag"
1791};
1792
1793static const char *top_right_corners[] = {
1794 "top_right_corner",
1795 "ne-resize",
1796 "size_bdiag"
1797};
1798
1799static const char *top_sides[] = {
1800 "top_side",
1801 "n-resize",
1802 "size_ver"
1803};
1804
1805struct cursor_alternatives {
1806 const char **names;
1807 size_t count;
1808};
1809
1810static const struct cursor_alternatives cursors[] = {
1811 {top_sides, ARRAY_LENGTH(top_sides)},
1812 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1813 {left_sides, ARRAY_LENGTH(left_sides)},
1814 {right_sides, ARRAY_LENGTH(right_sides)},
1815 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1816 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1817 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1818 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1819 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001820};
1821
1822static void
1823weston_wm_create_cursors(struct weston_wm *wm)
1824{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001825 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001826 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001827 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001828
1829 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1830 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001831 for (j = 0; j < cursors[i].count; j++) {
1832 name = cursors[i].names[j];
1833 wm->cursors[i] =
1834 xcb_cursor_library_load_cursor(wm, name);
1835 if (wm->cursors[i] != (xcb_cursor_t)-1)
1836 break;
1837 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001838 }
1839
1840 wm->last_cursor = -1;
1841}
1842
1843static void
1844weston_wm_destroy_cursors(struct weston_wm *wm)
1845{
1846 uint8_t i;
1847
1848 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1849 xcb_free_cursor(wm->conn, wm->cursors[i]);
1850
1851 free(wm->cursors);
1852}
1853
1854static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001855get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001856{
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001857 switch (location) {
1858 case THEME_LOCATION_RESIZING_TOP:
1859 return XWM_CURSOR_TOP;
1860 case THEME_LOCATION_RESIZING_BOTTOM:
1861 return XWM_CURSOR_BOTTOM;
1862 case THEME_LOCATION_RESIZING_LEFT:
1863 return XWM_CURSOR_LEFT;
1864 case THEME_LOCATION_RESIZING_RIGHT:
1865 return XWM_CURSOR_RIGHT;
1866 case THEME_LOCATION_RESIZING_TOP_LEFT:
1867 return XWM_CURSOR_TOP_LEFT;
1868 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1869 return XWM_CURSOR_TOP_RIGHT;
1870 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1871 return XWM_CURSOR_BOTTOM_LEFT;
1872 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1873 return XWM_CURSOR_BOTTOM_RIGHT;
1874 case THEME_LOCATION_EXTERIOR:
1875 case THEME_LOCATION_TITLEBAR:
1876 default:
1877 return XWM_CURSOR_LEFT_PTR;
1878 }
1879}
1880
1881static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001882weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1883 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001884{
1885 uint32_t cursor_value_list;
1886
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001887 if (wm->last_cursor == cursor)
1888 return;
1889
1890 wm->last_cursor = cursor;
1891
1892 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001893 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001894 XCB_CW_CURSOR, &cursor_value_list);
1895 xcb_flush(wm->conn);
1896}
1897
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001898static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001899weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001900{
1901 xcb_client_message_event_t client_message;
1902
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001903 if (window->delete_window) {
1904 client_message.response_type = XCB_CLIENT_MESSAGE;
1905 client_message.format = 32;
1906 client_message.window = window->id;
1907 client_message.type = window->wm->atom.wm_protocols;
1908 client_message.data.data32[0] =
1909 window->wm->atom.wm_delete_window;
1910 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001911
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001912 xcb_send_event(window->wm->conn, 0, window->id,
1913 XCB_EVENT_MASK_NO_EVENT,
1914 (char *) &client_message);
1915 } else {
1916 xcb_kill_client(window->wm->conn, window->id);
1917 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001918}
1919
1920static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001921weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1922{
1923 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
Quentin Glidic955cec02016-08-12 10:41:35 +02001924 const struct weston_desktop_xwayland_interface *xwayland_interface =
1925 wm->server->compositor->xwayland_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001926 struct weston_seat *seat;
Derek Foremane4d6c832015-08-05 14:48:11 -05001927 struct weston_pointer *pointer;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001928 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001929 enum theme_location location;
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001930 enum wl_pointer_button_state button_state;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001931 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001932
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001933 wm_log("XCB_BUTTON_%s (detail %d)\n",
1934 button->response_type == XCB_BUTTON_PRESS ?
1935 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001936
Derek Foreman49372142015-04-09 10:51:22 -05001937 if (!wm_lookup_window(wm, button->event, &window) ||
1938 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001939 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001940
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001941 if (button->detail != 1 && button->detail != 2)
1942 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001943
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001944 seat = weston_wm_pick_seat_for_window(window);
Derek Foremane4d6c832015-08-05 14:48:11 -05001945 pointer = weston_seat_get_pointer(seat);
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001946
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001947 button_state = button->response_type == XCB_BUTTON_PRESS ?
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001948 WL_POINTER_BUTTON_STATE_PRESSED :
1949 WL_POINTER_BUTTON_STATE_RELEASED;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001950 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1951
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001952 /* Make sure we're looking at the right location. The frame
1953 * could have received a motion event from a pointer from a
1954 * different wl_seat, but under X it looks like our core
1955 * pointer moved. Move the frame pointer to the button press
1956 * location before deciding what to do. */
1957 location = frame_pointer_motion(window->frame, NULL,
1958 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001959 location = frame_pointer_button(window->frame, NULL,
1960 button_id, button_state);
1961 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1962 weston_wm_window_schedule_repaint(window);
1963
1964 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001965 if (pointer)
Quentin Glidic955cec02016-08-12 10:41:35 +02001966 xwayland_interface->move(window->shsurf, pointer);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001967 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1968 }
1969
1970 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001971 if (pointer)
Quentin Glidic955cec02016-08-12 10:41:35 +02001972 xwayland_interface->resize(window->shsurf, pointer, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001973 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1974 }
1975
1976 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001977 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001978 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001979 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001980
1981 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1982 window->maximized_horz = !window->maximized_horz;
1983 window->maximized_vert = !window->maximized_vert;
1984 if (weston_wm_window_is_maximized(window)) {
1985 window->saved_width = window->width;
1986 window->saved_height = window->height;
Quentin Glidic955cec02016-08-12 10:41:35 +02001987 xwayland_interface->set_maximized(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001988 } else {
1989 weston_wm_window_set_toplevel(window);
1990 }
1991 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1992 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001993}
1994
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001995static void
1996weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1997{
1998 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1999 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002000 enum theme_location location;
2001 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002002
Derek Foreman49372142015-04-09 10:51:22 -05002003 if (!wm_lookup_window(wm, motion->event, &window) ||
2004 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002005 return;
2006
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002007 location = frame_pointer_motion(window->frame, NULL,
2008 motion->event_x, motion->event_y);
2009 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
2010 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002011
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002012 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04002013 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002014}
2015
2016static void
2017weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
2018{
2019 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
2020 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002021 enum theme_location location;
2022 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002023
Derek Foreman49372142015-04-09 10:51:22 -05002024 if (!wm_lookup_window(wm, enter->event, &window) ||
2025 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002026 return;
2027
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002028 location = frame_pointer_enter(window->frame, NULL,
2029 enter->event_x, enter->event_y);
2030 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
2031 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002032
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002033 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04002034 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002035}
2036
2037static void
2038weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
2039{
2040 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
2041 struct weston_wm_window *window;
2042
Derek Foreman49372142015-04-09 10:51:22 -05002043 if (!wm_lookup_window(wm, leave->event, &window) ||
2044 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002045 return;
2046
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002047 frame_pointer_leave(window->frame, NULL);
2048 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
2049 weston_wm_window_schedule_repaint(window);
2050
Tiago Vignattic1903232012-07-16 12:15:37 -04002051 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002052}
2053
Giulio Camuffob18f7882015-03-29 14:20:23 +03002054static void
2055weston_wm_handle_focus_in(struct weston_wm *wm, xcb_generic_event_t *event)
2056{
2057 xcb_focus_in_event_t *focus = (xcb_focus_in_event_t *) event;
2058 /* Do not let X clients change the focus behind the compositor's
2059 * back. Reset the focus to the old one if it changed. */
2060 if (!wm->focus_window || focus->event != wm->focus_window->id)
2061 weston_wm_send_focus_window(wm, wm->focus_window);
2062}
2063
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002064static int
2065weston_wm_handle_event(int fd, uint32_t mask, void *data)
2066{
2067 struct weston_wm *wm = data;
2068 xcb_generic_event_t *event;
2069 int count = 0;
2070
2071 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
2072 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002073 free(event);
2074 count++;
2075 continue;
2076 }
2077
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002078 if (weston_wm_handle_dnd_event(wm, event)) {
2079 free(event);
2080 count++;
2081 continue;
2082 }
2083
MoD31700122013-06-11 19:58:55 -05002084 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002085 case XCB_BUTTON_PRESS:
2086 case XCB_BUTTON_RELEASE:
2087 weston_wm_handle_button(wm, event);
2088 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002089 case XCB_ENTER_NOTIFY:
2090 weston_wm_handle_enter(wm, event);
2091 break;
2092 case XCB_LEAVE_NOTIFY:
2093 weston_wm_handle_leave(wm, event);
2094 break;
2095 case XCB_MOTION_NOTIFY:
2096 weston_wm_handle_motion(wm, event);
2097 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002098 case XCB_CREATE_NOTIFY:
2099 weston_wm_handle_create_notify(wm, event);
2100 break;
2101 case XCB_MAP_REQUEST:
2102 weston_wm_handle_map_request(wm, event);
2103 break;
2104 case XCB_MAP_NOTIFY:
2105 weston_wm_handle_map_notify(wm, event);
2106 break;
2107 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04002108 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002109 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04002110 case XCB_REPARENT_NOTIFY:
2111 weston_wm_handle_reparent_notify(wm, event);
2112 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002113 case XCB_CONFIGURE_REQUEST:
2114 weston_wm_handle_configure_request(wm, event);
2115 break;
2116 case XCB_CONFIGURE_NOTIFY:
2117 weston_wm_handle_configure_notify(wm, event);
2118 break;
2119 case XCB_DESTROY_NOTIFY:
2120 weston_wm_handle_destroy_notify(wm, event);
2121 break;
2122 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002123 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002124 break;
2125 case XCB_PROPERTY_NOTIFY:
2126 weston_wm_handle_property_notify(wm, event);
2127 break;
2128 case XCB_CLIENT_MESSAGE:
2129 weston_wm_handle_client_message(wm, event);
2130 break;
Giulio Camuffob18f7882015-03-29 14:20:23 +03002131 case XCB_FOCUS_IN:
2132 weston_wm_handle_focus_in(wm, event);
2133 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002134 }
2135
2136 free(event);
2137 count++;
2138 }
2139
Marek Chalupaa1f3f3c2015-08-12 09:55:12 +02002140 if (count != 0)
2141 xcb_flush(wm->conn);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002142
2143 return count;
2144}
2145
2146static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002147weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window) {
2148 xcb_change_property(wm->conn, XCB_PROP_MODE_REPLACE,
2149 wm->screen->root, wm->atom.net_active_window,
2150 wm->atom.window, 32, 1, &window);
2151}
2152
2153static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002154weston_wm_get_visual_and_colormap(struct weston_wm *wm)
2155{
2156 xcb_depth_iterator_t d_iter;
2157 xcb_visualtype_iterator_t vt_iter;
2158 xcb_visualtype_t *visualtype;
2159
2160 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
2161 visualtype = NULL;
2162 while (d_iter.rem > 0) {
2163 if (d_iter.data->depth == 32) {
2164 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
2165 visualtype = vt_iter.data;
2166 break;
2167 }
2168
2169 xcb_depth_next(&d_iter);
2170 }
2171
2172 if (visualtype == NULL) {
2173 weston_log("no 32 bit visualtype\n");
2174 return;
2175 }
2176
2177 wm->visual_id = visualtype->visual_id;
2178 wm->colormap = xcb_generate_id(wm->conn);
2179 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
2180 wm->colormap, wm->screen->root, wm->visual_id);
2181}
2182
2183static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002184weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002185{
2186
2187#define F(field) offsetof(struct weston_wm, field)
2188
2189 static const struct { const char *name; int offset; } atoms[] = {
2190 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002191 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002192 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
2193 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04002194 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002195 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002196 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002197 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002198 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002199 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002200 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2201 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002202 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2203 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002204 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2205 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2206 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02002207 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002208 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2209
2210 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2211 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2212 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2213 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2214 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2215 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2216 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002217 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2218 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002219 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2220 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2221 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2222 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2223 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2224
2225 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2226 { "_NET_SUPPORTING_WM_CHECK",
2227 F(atom.net_supporting_wm_check) },
2228 { "_NET_SUPPORTED", F(atom.net_supported) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002229 { "_NET_ACTIVE_WINDOW", F(atom.net_active_window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002230 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2231 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002232 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002233 { "TARGETS", F(atom.targets) },
2234 { "UTF8_STRING", F(atom.utf8_string) },
2235 { "_WL_SELECTION", F(atom.wl_selection) },
2236 { "INCR", F(atom.incr) },
2237 { "TIMESTAMP", F(atom.timestamp) },
2238 { "MULTIPLE", F(atom.multiple) },
2239 { "UTF8_STRING" , F(atom.utf8_string) },
2240 { "COMPOUND_TEXT", F(atom.compound_text) },
2241 { "TEXT", F(atom.text) },
2242 { "STRING", F(atom.string) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002243 { "WINDOW", F(atom.window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002244 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2245 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002246 { "XdndSelection", F(atom.xdnd_selection) },
2247 { "XdndAware", F(atom.xdnd_aware) },
2248 { "XdndEnter", F(atom.xdnd_enter) },
2249 { "XdndLeave", F(atom.xdnd_leave) },
2250 { "XdndDrop", F(atom.xdnd_drop) },
2251 { "XdndStatus", F(atom.xdnd_status) },
2252 { "XdndFinished", F(atom.xdnd_finished) },
2253 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002254 { "XdndActionCopy", F(atom.xdnd_action_copy) },
Pekka Paalanen7ace8312017-01-18 15:37:58 +02002255 { "_XWAYLAND_ALLOW_COMMITS", F(atom.allow_commits) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002256 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002257 };
2258#undef F
2259
2260 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2261 xcb_xfixes_query_version_reply_t *xfixes_reply;
2262 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2263 xcb_intern_atom_reply_t *reply;
2264 xcb_render_query_pict_formats_reply_t *formats_reply;
2265 xcb_render_query_pict_formats_cookie_t formats_cookie;
2266 xcb_render_pictforminfo_t *formats;
2267 uint32_t i;
2268
2269 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002270 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002271
2272 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2273
2274 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2275 cookies[i] = xcb_intern_atom (wm->conn, 0,
2276 strlen(atoms[i].name),
2277 atoms[i].name);
2278
2279 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2280 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2281 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2282 free(reply);
2283 }
2284
2285 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2286 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002287 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002288
2289 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2290 XCB_XFIXES_MAJOR_VERSION,
2291 XCB_XFIXES_MINOR_VERSION);
2292 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2293 xfixes_cookie, NULL);
2294
Martin Minarik6d118362012-06-07 18:01:59 +02002295 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002296 xfixes_reply->major_version, xfixes_reply->minor_version);
2297
2298 free(xfixes_reply);
2299
2300 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2301 formats_cookie, 0);
2302 if (formats_reply == NULL)
2303 return;
2304
2305 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002306 for (i = 0; i < formats_reply->num_formats; i++) {
2307 if (formats[i].direct.red_mask != 0xff &&
2308 formats[i].direct.red_shift != 16)
2309 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002310 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2311 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002312 wm->format_rgb = formats[i];
2313 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2314 formats[i].depth == 32 &&
2315 formats[i].direct.alpha_mask == 0xff &&
2316 formats[i].direct.alpha_shift == 24)
2317 wm->format_rgba = formats[i];
2318 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002319
2320 free(formats_reply);
2321}
2322
2323static void
2324weston_wm_create_wm_window(struct weston_wm *wm)
2325{
2326 static const char name[] = "Weston WM";
2327
2328 wm->wm_window = xcb_generate_id(wm->conn);
2329 xcb_create_window(wm->conn,
2330 XCB_COPY_FROM_PARENT,
2331 wm->wm_window,
2332 wm->screen->root,
2333 0, 0,
2334 10, 10,
2335 0,
2336 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2337 wm->screen->root_visual,
2338 0, NULL);
2339
2340 xcb_change_property(wm->conn,
2341 XCB_PROP_MODE_REPLACE,
2342 wm->wm_window,
2343 wm->atom.net_supporting_wm_check,
2344 XCB_ATOM_WINDOW,
2345 32, /* format */
2346 1, &wm->wm_window);
2347
2348 xcb_change_property(wm->conn,
2349 XCB_PROP_MODE_REPLACE,
2350 wm->wm_window,
2351 wm->atom.net_wm_name,
2352 wm->atom.utf8_string,
2353 8, /* format */
2354 strlen(name), name);
2355
2356 xcb_change_property(wm->conn,
2357 XCB_PROP_MODE_REPLACE,
2358 wm->screen->root,
2359 wm->atom.net_supporting_wm_check,
2360 XCB_ATOM_WINDOW,
2361 32, /* format */
2362 1, &wm->wm_window);
2363
Abdur Rehmanb8b150b2017-01-01 19:46:46 +05002364 /* Claim the WM_S0 selection even though we don't support
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002365 * the --replace functionality. */
2366 xcb_set_selection_owner(wm->conn,
2367 wm->wm_window,
2368 wm->atom.wm_s0,
2369 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002370
2371 xcb_set_selection_owner(wm->conn,
2372 wm->wm_window,
2373 wm->atom.net_wm_cm_s0,
2374 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002375}
2376
2377struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002378weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002379{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002380 struct weston_wm *wm;
2381 struct wl_event_loop *loop;
2382 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002383 uint32_t values[1];
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002384 xcb_atom_t supported[6];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002385
Peter Huttererf3d62272013-08-08 11:57:05 +10002386 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002387 if (wm == NULL)
2388 return NULL;
2389
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002390 wm->server = wxs;
2391 wm->window_hash = hash_table_create();
2392 if (wm->window_hash == NULL) {
2393 free(wm);
2394 return NULL;
2395 }
2396
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002397 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002398 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002399 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002400 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002401 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002402 hash_table_destroy(wm->window_hash);
2403 free(wm);
2404 return NULL;
2405 }
2406
2407 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2408 wm->screen = s.data;
2409
2410 loop = wl_display_get_event_loop(wxs->wl_display);
2411 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002412 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002413 WL_EVENT_READABLE,
2414 weston_wm_handle_event, wm);
2415 wl_event_source_check(wm->source);
2416
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002417 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002418 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002419
2420 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002421 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2422 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2423 XCB_EVENT_MASK_PROPERTY_CHANGE;
2424 xcb_change_window_attributes(wm->conn, wm->screen->root,
2425 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002426
2427 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2428 XCB_COMPOSITE_REDIRECT_MANUAL);
2429
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002430 wm->theme = theme_create();
2431
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002432 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002433 supported[1] = wm->atom.net_wm_state;
2434 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002435 supported[3] = wm->atom.net_wm_state_maximized_vert;
2436 supported[4] = wm->atom.net_wm_state_maximized_horz;
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002437 supported[5] = wm->atom.net_active_window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002438 xcb_change_property(wm->conn,
2439 XCB_PROP_MODE_REPLACE,
2440 wm->screen->root,
2441 wm->atom.net_supported,
2442 XCB_ATOM_ATOM,
2443 32, /* format */
2444 ARRAY_LENGTH(supported), supported);
2445
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002446 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
2447
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002448 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002449
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002450 weston_wm_dnd_init(wm);
2451
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002452 xcb_flush(wm->conn);
2453
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002454 wm->create_surface_listener.notify = weston_wm_create_surface;
2455 wl_signal_add(&wxs->compositor->create_surface_signal,
2456 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002457 wm->activate_listener.notify = weston_wm_window_activate;
2458 wl_signal_add(&wxs->compositor->activate_signal,
2459 &wm->activate_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002460 wm->kill_listener.notify = weston_wm_kill_client;
2461 wl_signal_add(&wxs->compositor->kill_signal,
2462 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002463 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002464
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002465 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002466 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002467
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002468 /* Create wm window and take WM_S0 selection last, which
2469 * signals to Xwayland that we're done with setup. */
2470 weston_wm_create_wm_window(wm);
2471
2472 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002473
2474 return wm;
2475}
2476
2477void
2478weston_wm_destroy(struct weston_wm *wm)
2479{
2480 /* FIXME: Free windows in hash. */
2481 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002482 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002483 xcb_disconnect(wm->conn);
2484 wl_event_source_remove(wm->source);
2485 wl_list_remove(&wm->selection_listener.link);
2486 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002487 wl_list_remove(&wm->kill_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002488 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002489
2490 free(wm);
2491}
2492
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002493static struct weston_wm_window *
2494get_wm_window(struct weston_surface *surface)
2495{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002496 struct wl_listener *listener;
2497
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002498 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002499 if (listener)
2500 return container_of(listener, struct weston_wm_window,
2501 surface_destroy_listener);
2502
2503 return NULL;
2504}
2505
Quentin Glidic955cec02016-08-12 10:41:35 +02002506static bool
2507is_wm_window(struct weston_surface *surface)
2508{
2509 return get_wm_window(surface) != NULL;
2510}
2511
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002512static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002513weston_wm_window_configure(void *data)
2514{
2515 struct weston_wm_window *window = data;
2516 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002517 uint32_t values[4];
2518 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002519
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002520 weston_wm_window_get_child_position(window, &x, &y);
2521 values[0] = x;
2522 values[1] = y;
2523 values[2] = window->width;
2524 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002525 xcb_configure_window(wm->conn,
2526 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002527 XCB_CONFIG_WINDOW_X |
2528 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002529 XCB_CONFIG_WINDOW_WIDTH |
2530 XCB_CONFIG_WINDOW_HEIGHT,
2531 values);
2532
2533 weston_wm_window_get_frame_size(window, &width, &height);
2534 values[0] = width;
2535 values[1] = height;
2536 xcb_configure_window(wm->conn,
2537 window->frame_id,
2538 XCB_CONFIG_WINDOW_WIDTH |
2539 XCB_CONFIG_WINDOW_HEIGHT,
2540 values);
2541
2542 window->configure_source = NULL;
2543
2544 weston_wm_window_schedule_repaint(window);
2545}
2546
2547static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002548send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002549{
2550 struct weston_wm_window *window = get_wm_window(surface);
2551 struct weston_wm *wm = window->wm;
2552 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002553 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002554
Marek Chalupae9fe4672014-10-29 13:44:44 +01002555 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002556 hborder = 2 * t->width;
2557 vborder = t->titlebar_height + t->width;
2558 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002559 hborder = 0;
2560 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002561 }
2562
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002563 if (width > hborder)
2564 window->width = width - hborder;
2565 else
2566 window->width = 1;
2567
2568 if (height > vborder)
2569 window->height = height - vborder;
2570 else
2571 window->height = 1;
2572
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002573 if (window->frame)
2574 frame_resize_inside(window->frame, window->width, window->height);
2575
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002576 if (window->configure_source)
2577 return;
2578
2579 window->configure_source =
2580 wl_event_loop_add_idle(wm->server->loop,
2581 weston_wm_window_configure, window);
2582}
2583
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002584static void
2585send_position(struct weston_surface *surface, int32_t x, int32_t y)
2586{
2587 struct weston_wm_window *window = get_wm_window(surface);
2588 struct weston_wm *wm;
2589 uint32_t mask, values[2];
2590
2591 if (!window || !window->wm)
2592 return;
2593
2594 wm = window->wm;
2595 /* We use pos_dirty to tell whether a configure message is in flight.
2596 * This is needed in case we send two configure events in a very
2597 * short time, since window->x/y is set in after a roundtrip, hence
2598 * we cannot just check if the current x and y are different. */
2599 if (window->x != x || window->y != y || window->pos_dirty) {
2600 window->pos_dirty = true;
2601 values[0] = x;
2602 values[1] = y;
2603 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
2604
2605 xcb_configure_window(wm->conn, window->frame_id, mask, values);
2606 xcb_flush(wm->conn);
2607 }
2608}
2609
Quentin Glidic955cec02016-08-12 10:41:35 +02002610static const struct weston_xwayland_client_interface shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002611 send_configure,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002612};
2613
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002614static int
2615legacy_fullscreen(struct weston_wm *wm,
2616 struct weston_wm_window *window,
2617 struct weston_output **output_ret)
2618{
2619 struct weston_compositor *compositor = wm->server->compositor;
2620 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002621 uint32_t minmax = PMinSize | PMaxSize;
2622 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002623
2624 /* Heuristics for detecting legacy fullscreen windows... */
2625
2626 wl_list_for_each(output, &compositor->output_list, link) {
2627 if (output->x == window->x &&
2628 output->y == window->y &&
2629 output->width == window->width &&
2630 output->height == window->height &&
2631 window->override_redirect) {
2632 *output_ret = output;
2633 return 1;
2634 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002635
2636 matching_size = 0;
2637 if ((window->size_hints.flags & (USSize |PSize)) &&
2638 window->size_hints.width == output->width &&
2639 window->size_hints.height == output->height)
2640 matching_size = 1;
2641 if ((window->size_hints.flags & minmax) == minmax &&
2642 window->size_hints.min_width == output->width &&
2643 window->size_hints.min_height == output->height &&
2644 window->size_hints.max_width == output->width &&
2645 window->size_hints.max_height == output->height)
2646 matching_size = 1;
2647
2648 if (matching_size && !window->decorate &&
2649 (window->size_hints.flags & (USPosition | PPosition)) &&
2650 window->size_hints.x == output->x &&
2651 window->size_hints.y == output->y) {
2652 *output_ret = output;
2653 return 1;
2654 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002655 }
2656
2657 return 0;
2658}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002659
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002660static bool
Pekka Paalanen882aff02016-11-16 14:15:18 +02002661weston_wm_window_is_positioned(struct weston_wm_window *window)
2662{
2663 if (window->map_request_x == INT_MIN ||
2664 window->map_request_y == INT_MIN)
2665 weston_log("XWM warning: win %d did not see map request\n",
2666 window->id);
2667
2668 return window->map_request_x != 0 || window->map_request_y != 0;
2669}
2670
2671static bool
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002672weston_wm_window_type_inactive(struct weston_wm_window *window)
2673{
2674 struct weston_wm *wm = window->wm;
2675
2676 return window->type == wm->atom.net_wm_window_type_tooltip ||
2677 window->type == wm->atom.net_wm_window_type_dropdown ||
2678 window->type == wm->atom.net_wm_window_type_dnd ||
2679 window->type == wm->atom.net_wm_window_type_combo ||
Giulio Camuffo84787ea2015-04-29 19:00:48 +03002680 window->type == wm->atom.net_wm_window_type_popup ||
2681 window->type == wm->atom.net_wm_window_type_utility;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002682}
2683
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002684static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002685xserver_map_shell_surface(struct weston_wm_window *window,
2686 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002687{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002688 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +02002689 struct weston_desktop_xwayland *xwayland =
2690 wm->server->compositor->xwayland;
2691 const struct weston_desktop_xwayland_interface *xwayland_interface =
2692 wm->server->compositor->xwayland_interface;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002693 struct weston_wm_window *parent;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002694
Pekka Paalanen0bbe6242016-12-21 13:41:34 +02002695 /* This should be necessary only for override-redirected windows,
2696 * because otherwise MapRequest handler would have already updated
2697 * the properties. However, if X11 clients set properties after
2698 * sending MapWindow, here we can still process them. The decorations
2699 * have already been drawn once with the old property values, so if the
2700 * app changes something affecting decor after MapWindow, we glitch.
2701 * We only hit xserver_map_shell_surface() once per MapWindow and
2702 * wl_surface, so better ensure we get the window type right.
2703 */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002704 weston_wm_window_read_properties(window);
2705
2706 /* A weston_wm_window may have many different surfaces assigned
2707 * throughout its life, so we must make sure to remove the listener
2708 * from the old surface signal list. */
2709 if (window->surface)
2710 wl_list_remove(&window->surface_destroy_listener.link);
2711
2712 window->surface = surface;
2713 window->surface_destroy_listener.notify = surface_destroy;
2714 wl_signal_add(&window->surface->destroy_signal,
2715 &window->surface_destroy_listener);
2716
Quentin Glidic955cec02016-08-12 10:41:35 +02002717 if (!xwayland_interface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002718 return;
2719
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002720 if (window->surface->committed) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03002721 weston_log("warning, unexpected in %s: "
2722 "surface's configure hook is already set.\n",
2723 __func__);
2724 return;
2725 }
2726
Murray Calavera883ac022015-06-06 13:02:22 +00002727 window->shsurf =
Quentin Glidic955cec02016-08-12 10:41:35 +02002728 xwayland_interface->create_surface(xwayland,
2729 window->surface,
2730 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002731
Pekka Paalanen9a5fab02016-12-20 16:15:02 +02002732 wm_log("XWM: map shell surface, win %d, weston_surface %p, xwayland surface %p\n",
2733 window->id, window->surface, window->shsurf);
Pekka Paalanena04eacc2016-11-28 16:42:25 +02002734
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002735 if (window->name)
Quentin Glidic955cec02016-08-12 10:41:35 +02002736 xwayland_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002737 if (window->pid > 0)
Quentin Glidic955cec02016-08-12 10:41:35 +02002738 xwayland_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002739
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002740 if (window->fullscreen) {
2741 window->saved_width = window->width;
2742 window->saved_height = window->height;
Pekka Paalanen505237e2016-12-01 15:41:11 +02002743 xwayland_interface->set_fullscreen(window->shsurf,
2744 window->legacy_fullscreen_output.output);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002745 return;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002746 } else if (window->override_redirect) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002747 xwayland_interface->set_xwayland(window->shsurf,
2748 window->x, window->y);
Axel Davye4450f92014-01-12 15:06:05 +01002749 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002750 parent = window->transient_for;
Quentin Glidic955cec02016-08-12 10:41:35 +02002751 if (weston_wm_window_type_inactive(window)) {
2752 xwayland_interface->set_transient(window->shsurf,
2753 parent->surface,
2754 window->x - parent->x,
2755 window->y - parent->y);
2756 } else {
2757 xwayland_interface->set_toplevel(window->shsurf);
2758 xwayland_interface->set_parent(window->shsurf,
2759 parent->surface);
2760 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002761 } else if (weston_wm_window_is_maximized(window)) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002762 xwayland_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002763 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002764 if (weston_wm_window_type_inactive(window)) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002765 xwayland_interface->set_xwayland(window->shsurf,
2766 window->x,
2767 window->y);
Pekka Paalanen882aff02016-11-16 14:15:18 +02002768 } else if (weston_wm_window_is_positioned(window)) {
2769 xwayland_interface->set_toplevel_with_position(window->shsurf,
2770 window->map_request_x,
2771 window->map_request_y);
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002772 } else {
Quentin Glidic955cec02016-08-12 10:41:35 +02002773 xwayland_interface->set_toplevel(window->shsurf);
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002774 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002775 }
Pekka Paalanenad0da452017-01-18 15:37:57 +02002776
Pekka Paalanen7ace8312017-01-18 15:37:58 +02002777 if (window->frame_id == XCB_WINDOW_NONE) {
Pekka Paalanenad0da452017-01-18 15:37:57 +02002778 weston_wm_window_set_pending_state_OR(window);
Pekka Paalanen7ace8312017-01-18 15:37:58 +02002779 } else {
Pekka Paalanenad0da452017-01-18 15:37:57 +02002780 weston_wm_window_set_pending_state(window);
Pekka Paalanen7ace8312017-01-18 15:37:58 +02002781 weston_wm_window_set_allow_commits(window, true);
2782 xcb_flush(wm->conn);
2783 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002784}
Quentin Glidic955cec02016-08-12 10:41:35 +02002785
2786const struct weston_xwayland_surface_api surface_api = {
2787 is_wm_window,
2788 send_position,
2789};