blob: 5e94bdc368fce2ad417573a7a02aced5e4a9577b [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 Paalanen505237e2016-12-01 15:41:11 +020038#include <assert.h>
Tiago Vignatti90fada42012-07-16 12:02:08 -040039#include <X11/Xcursor/Xcursor.h>
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -050040#include <linux/input.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040041
Daniel Stone67fe3db2016-10-31 14:51:18 +000042#include "compositor.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040043#include "xwayland.h"
Quentin Glidic955cec02016-08-12 10:41:35 +020044#include "xwayland-internal-interface.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040045
Kristian Høgsberg2ba10df2013-12-03 16:38:15 -080046#include "cairo-util.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040047#include "hash.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070048#include "shared/helpers.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040049
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070050struct wm_size_hints {
Bryce Harringtone00554b2015-06-12 10:17:39 -070051 uint32_t flags;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070052 int32_t x, y;
53 int32_t width, height; /* should set so old wm's don't mess up */
54 int32_t min_width, min_height;
55 int32_t max_width, max_height;
Bryce Harringtone00554b2015-06-12 10:17:39 -070056 int32_t width_inc, height_inc;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070057 struct {
58 int32_t x;
59 int32_t y;
60 } min_aspect, max_aspect;
61 int32_t base_width, base_height;
62 int32_t win_gravity;
63};
64
65#define USPosition (1L << 0)
66#define USSize (1L << 1)
67#define PPosition (1L << 2)
68#define PSize (1L << 3)
69#define PMinSize (1L << 4)
70#define PMaxSize (1L << 5)
71#define PResizeInc (1L << 6)
72#define PAspect (1L << 7)
73#define PBaseSize (1L << 8)
74#define PWinGravity (1L << 9)
75
Kristian Høgsberg380deee2012-05-21 17:12:41 -040076struct motif_wm_hints {
77 uint32_t flags;
78 uint32_t functions;
79 uint32_t decorations;
80 int32_t input_mode;
81 uint32_t status;
82};
83
84#define MWM_HINTS_FUNCTIONS (1L << 0)
85#define MWM_HINTS_DECORATIONS (1L << 1)
86#define MWM_HINTS_INPUT_MODE (1L << 2)
87#define MWM_HINTS_STATUS (1L << 3)
88
89#define MWM_FUNC_ALL (1L << 0)
90#define MWM_FUNC_RESIZE (1L << 1)
91#define MWM_FUNC_MOVE (1L << 2)
92#define MWM_FUNC_MINIMIZE (1L << 3)
93#define MWM_FUNC_MAXIMIZE (1L << 4)
94#define MWM_FUNC_CLOSE (1L << 5)
95
96#define MWM_DECOR_ALL (1L << 0)
97#define MWM_DECOR_BORDER (1L << 1)
98#define MWM_DECOR_RESIZEH (1L << 2)
99#define MWM_DECOR_TITLE (1L << 3)
100#define MWM_DECOR_MENU (1L << 4)
101#define MWM_DECOR_MINIMIZE (1L << 5)
102#define MWM_DECOR_MAXIMIZE (1L << 6)
103
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700104#define MWM_DECOR_EVERYTHING \
105 (MWM_DECOR_BORDER | MWM_DECOR_RESIZEH | MWM_DECOR_TITLE | \
106 MWM_DECOR_MENU | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE)
107
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400108#define MWM_INPUT_MODELESS 0
109#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
110#define MWM_INPUT_SYSTEM_MODAL 2
111#define MWM_INPUT_FULL_APPLICATION_MODAL 3
112#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
113
114#define MWM_TEAROFF_WINDOW (1L<<0)
115
116#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
117#define _NET_WM_MOVERESIZE_SIZE_TOP 1
118#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
119#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
120#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
121#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
122#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
123#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
124#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
125#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
126#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
127#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
128
Pekka Paalanen505237e2016-12-01 15:41:11 +0200129struct weston_output_weak_ref {
130 struct weston_output *output;
131 struct wl_listener destroy_listener;
132};
133
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400134struct weston_wm_window {
135 struct weston_wm *wm;
136 xcb_window_t id;
137 xcb_window_t frame_id;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500138 struct frame *frame;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400139 cairo_surface_t *cairo_surface;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700140 uint32_t surface_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400141 struct weston_surface *surface;
Quentin Glidic955cec02016-08-12 10:41:35 +0200142 struct weston_desktop_xwayland_surface *shsurf;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400143 struct wl_listener surface_destroy_listener;
144 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400145 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400146 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300147 int pid;
148 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400149 char *class;
150 char *name;
151 struct weston_wm_window *transient_for;
152 uint32_t protocols;
153 xcb_atom_t type;
154 int width, height;
155 int x, y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200156 bool pos_dirty;
Pekka Paalanen505237e2016-12-01 15:41:11 +0200157 struct weston_output_weak_ref legacy_fullscreen_output;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500158 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400159 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300160 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500161 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500162 int has_alpha;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700163 int delete_window;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200164 int maximized_vert;
165 int maximized_horz;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700166 struct wm_size_hints size_hints;
167 struct motif_wm_hints motif_hints;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700168 struct wl_list link;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400169};
170
171static struct weston_wm_window *
172get_wm_window(struct weston_surface *surface);
173
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400174static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200175weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window);
176
177static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400178weston_wm_window_schedule_repaint(struct weston_wm_window *window);
179
Pekka Paalanen505237e2016-12-01 15:41:11 +0200180static int
181legacy_fullscreen(struct weston_wm *wm,
182 struct weston_wm_window *window,
183 struct weston_output **output_ret);
184
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700185static void
186xserver_map_shell_surface(struct weston_wm_window *window,
187 struct weston_surface *surface);
188
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400189static int __attribute__ ((format (printf, 1, 2)))
190wm_log(const char *fmt, ...)
191{
192#ifdef WM_DEBUG
193 int l;
194 va_list argp;
195
196 va_start(argp, fmt);
197 l = weston_vlog(fmt, argp);
198 va_end(argp);
199
200 return l;
201#else
202 return 0;
203#endif
204}
205
206static int __attribute__ ((format (printf, 1, 2)))
207wm_log_continue(const char *fmt, ...)
208{
209#ifdef WM_DEBUG
210 int l;
211 va_list argp;
212
213 va_start(argp, fmt);
214 l = weston_vlog_continue(fmt, argp);
215 va_end(argp);
216
217 return l;
218#else
219 return 0;
220#endif
221}
222
Pekka Paalanen505237e2016-12-01 15:41:11 +0200223static void
224weston_output_weak_ref_init(struct weston_output_weak_ref *ref)
225{
226 ref->output = NULL;
227}
228
229static void
230weston_output_weak_ref_clear(struct weston_output_weak_ref *ref)
231{
232 if (!ref->output)
233 return;
234
235 wl_list_remove(&ref->destroy_listener.link);
236 ref->output = NULL;
237}
238
239static void
240weston_output_weak_ref_handle_destroy(struct wl_listener *listener, void *data)
241{
242 struct weston_output_weak_ref *ref;
243
244 ref = wl_container_of(listener, ref, destroy_listener);
245 assert(ref->output == data);
246
247 weston_output_weak_ref_clear(ref);
248}
249
250static void
251weston_output_weak_ref_set(struct weston_output_weak_ref *ref,
252 struct weston_output *output)
253{
254 weston_output_weak_ref_clear(ref);
255
256 if (!output)
257 return;
258
259 ref->destroy_listener.notify = weston_output_weak_ref_handle_destroy;
260 wl_signal_add(&output->destroy_signal, &ref->destroy_listener);
261 ref->output = output;
262}
263
Derek Foreman49372142015-04-09 10:51:22 -0500264static bool __attribute__ ((warn_unused_result))
265wm_lookup_window(struct weston_wm *wm, xcb_window_t hash,
266 struct weston_wm_window **window)
267{
268 *window = hash_table_lookup(wm->window_hash, hash);
269 if (*window)
270 return true;
271 return false;
272}
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400273
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400274const char *
275get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
276{
277 xcb_get_atom_name_cookie_t cookie;
278 xcb_get_atom_name_reply_t *reply;
279 xcb_generic_error_t *e;
280 static char buffer[64];
281
282 if (atom == XCB_ATOM_NONE)
283 return "None";
284
285 cookie = xcb_get_atom_name (c, atom);
286 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500287
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300288 if (reply) {
MoD55375b92013-06-11 19:59:42 -0500289 snprintf(buffer, sizeof buffer, "%.*s",
290 xcb_get_atom_name_name_length (reply),
291 xcb_get_atom_name_name (reply));
292 } else {
293 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
294 }
295
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400296 free(reply);
297
298 return buffer;
299}
300
Tiago Vignatti90fada42012-07-16 12:02:08 -0400301static xcb_cursor_t
302xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
303{
304 xcb_connection_t *c = wm->conn;
305 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
306 xcb_screen_t *screen = s.data;
307 xcb_gcontext_t gc;
308 xcb_pixmap_t pix;
309 xcb_render_picture_t pic;
310 xcb_cursor_t cursor;
311 int stride = img->width * 4;
312
313 pix = xcb_generate_id(c);
314 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
315
316 pic = xcb_generate_id(c);
317 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
318
319 gc = xcb_generate_id(c);
320 xcb_create_gc(c, gc, pix, 0, 0);
321
322 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
323 img->width, img->height, 0, 0, 0, 32,
324 stride * img->height, (uint8_t *) img->pixels);
325 xcb_free_gc(c, gc);
326
327 cursor = xcb_generate_id(c);
328 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
329
330 xcb_render_free_picture(c, pic);
331 xcb_free_pixmap(c, pix);
332
333 return cursor;
334}
335
336static xcb_cursor_t
337xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
338{
339 /* TODO: treat animated cursors as well */
340 if (images->nimage != 1)
341 return -1;
342
343 return xcb_cursor_image_load_cursor(wm, images->images[0]);
344}
345
346static xcb_cursor_t
347xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
348{
349 xcb_cursor_t cursor;
350 XcursorImages *images;
351 char *v = NULL;
352 int size = 0;
353
354 if (!file)
355 return 0;
356
357 v = getenv ("XCURSOR_SIZE");
358 if (v)
359 size = atoi(v);
360
361 if (!size)
362 size = 32;
363
364 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300365 if (!images)
366 return -1;
367
Tiago Vignatti90fada42012-07-16 12:02:08 -0400368 cursor = xcb_cursor_images_load_cursor (wm, images);
369 XcursorImagesDestroy (images);
370
371 return cursor;
372}
373
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400374void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400375dump_property(struct weston_wm *wm,
376 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400377{
378 int32_t *incr_value;
379 const char *text_value, *name;
380 xcb_atom_t *atom_value;
381 int width, len;
382 uint32_t i;
383
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400384 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400385 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400386 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400387 return;
388 }
389
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400390 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
391 get_atom_name(wm->conn, reply->type),
392 reply->format,
393 xcb_get_property_value_length(reply),
394 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400395
396 if (reply->type == wm->atom.incr) {
397 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400398 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400399 } else if (reply->type == wm->atom.utf8_string ||
400 reply->type == wm->atom.string) {
401 text_value = xcb_get_property_value(reply);
402 if (reply->value_len > 40)
403 len = 40;
404 else
405 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400406 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400407 } else if (reply->type == XCB_ATOM_ATOM) {
408 atom_value = xcb_get_property_value(reply);
409 for (i = 0; i < reply->value_len; i++) {
410 name = get_atom_name(wm->conn, atom_value[i]);
411 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400412 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400413 width = 4;
414 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400415 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400416 }
417
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400418 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400419 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400420 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400421 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400422 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400423 }
424}
425
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200426static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400427read_and_dump_property(struct weston_wm *wm,
428 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400429{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400430 xcb_get_property_reply_t *reply;
431 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400432
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400433 cookie = xcb_get_property(wm->conn, 0, window,
434 property, XCB_ATOM_ANY, 0, 2048);
435 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400436
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400437 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400438
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400439 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400440}
441
Pekka Paalanen20111d52016-12-01 17:07:12 +0200442/* We reuse some predefined, but otherwise useles atoms
443 * as local type placeholders that never touch the X11 server,
444 * to make weston_wm_window_read_properties() less exceptional.
445 */
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400446#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
447#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500448#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700449#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400450
451static void
452weston_wm_window_read_properties(struct weston_wm_window *window)
453{
454 struct weston_wm *wm = window->wm;
455
Pekka Paalanen20111d52016-12-01 17:07:12 +0200456#define F(field) (&window->field)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400457 const struct {
458 xcb_atom_t atom;
459 xcb_atom_t type;
Pekka Paalanen20111d52016-12-01 17:07:12 +0200460 void *ptr;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400461 } props[] = {
Pekka Paalanen20111d52016-12-01 17:07:12 +0200462 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
463 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
464 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
465 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, NULL },
466 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, NULL },
467 { wm->atom.net_wm_state, TYPE_NET_WM_STATE, NULL },
468 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
469 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
470 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
471 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, NULL },
472 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400473 };
474#undef F
475
476 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
477 xcb_get_property_reply_t *reply;
478 void *p;
479 uint32_t *xid;
480 xcb_atom_t *atom;
481 uint32_t i;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200482 char name[1024];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400483
484 if (!window->properties_dirty)
485 return;
486 window->properties_dirty = 0;
487
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400488 for (i = 0; i < ARRAY_LENGTH(props); i++)
489 cookie[i] = xcb_get_property(wm->conn,
490 0, /* delete */
491 window->id,
492 props[i].atom,
493 XCB_ATOM_ANY, 0, 2048);
494
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700495 window->decorate = window->override_redirect ? 0 : MWM_DECOR_EVERYTHING;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700496 window->size_hints.flags = 0;
497 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700498 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700499
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400500 for (i = 0; i < ARRAY_LENGTH(props); i++) {
501 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
502 if (!reply)
503 /* Bad window, typically */
504 continue;
505 if (reply->type == XCB_ATOM_NONE) {
506 /* No such property */
507 free(reply);
508 continue;
509 }
510
Pekka Paalanen20111d52016-12-01 17:07:12 +0200511 p = props[i].ptr;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400512
513 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300514 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400515 case XCB_ATOM_STRING:
516 /* FIXME: We're using this for both string and
517 utf8_string */
518 if (*(char **) p)
519 free(*(char **) p);
520
521 *(char **) p =
522 strndup(xcb_get_property_value(reply),
523 xcb_get_property_value_length(reply));
524 break;
525 case XCB_ATOM_WINDOW:
526 xid = xcb_get_property_value(reply);
Derek Foreman49372142015-04-09 10:51:22 -0500527 if (!wm_lookup_window(wm, *xid, p))
528 weston_log("XCB_ATOM_WINDOW contains window"
529 " id not found in hash table.\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400530 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300531 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400532 case XCB_ATOM_ATOM:
533 atom = xcb_get_property_value(reply);
534 *(xcb_atom_t *) p = *atom;
535 break;
536 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700537 atom = xcb_get_property_value(reply);
538 for (i = 0; i < reply->value_len; i++)
Derek Foremanb4deec62015-04-07 12:12:13 -0500539 if (atom[i] == wm->atom.wm_delete_window) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700540 window->delete_window = 1;
Derek Foremanb4deec62015-04-07 12:12:13 -0500541 break;
542 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400543 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700544 case TYPE_WM_NORMAL_HINTS:
545 memcpy(&window->size_hints,
546 xcb_get_property_value(reply),
547 sizeof window->size_hints);
548 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500549 case TYPE_NET_WM_STATE:
550 window->fullscreen = 0;
551 atom = xcb_get_property_value(reply);
Ryo Munakataf3744f52015-03-11 17:36:30 +0900552 for (i = 0; i < reply->value_len; i++) {
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500553 if (atom[i] == wm->atom.net_wm_state_fullscreen)
554 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200555 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
556 window->maximized_vert = 1;
557 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
558 window->maximized_horz = 1;
Ryo Munakataf3744f52015-03-11 17:36:30 +0900559 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500560 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400561 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700562 memcpy(&window->motif_hints,
563 xcb_get_property_value(reply),
564 sizeof window->motif_hints);
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700565 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) {
566 if (window->motif_hints.decorations & MWM_DECOR_ALL)
567 /* MWM_DECOR_ALL means all except the other values listed. */
568 window->decorate =
569 MWM_DECOR_EVERYTHING & (~window->motif_hints.decorations);
570 else
571 window->decorate =
572 window->motif_hints.decorations;
573 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400574 break;
575 default:
576 break;
577 }
578 free(reply);
579 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200580
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200581 if (window->pid > 0) {
582 gethostname(name, sizeof(name));
583 for (i = 0; i < sizeof(name); i++) {
584 if (name[i] == '\0')
585 break;
586 }
587 if (i == sizeof(name))
588 name[0] = '\0'; /* ignore stupid hostnames */
589
590 /* this is only one heuristic to guess the PID of a client is
591 * valid, assuming it's compliant with icccm and ewmh.
592 * Non-compliants and remote applications of course fail. */
593 if (!window->machine || strcmp(window->machine, name))
594 window->pid = 0;
595 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400596}
597
Pekka Paalanen20111d52016-12-01 17:07:12 +0200598#undef TYPE_WM_PROTOCOLS
599#undef TYPE_MOTIF_WM_HINTS
600#undef TYPE_NET_WM_STATE
601#undef TYPE_WM_NORMAL_HINTS
602
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400603static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400604weston_wm_window_get_frame_size(struct weston_wm_window *window,
605 int *width, int *height)
606{
607 struct theme *t = window->wm->theme;
608
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500609 if (window->fullscreen) {
610 *width = window->width;
611 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800612 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500613 *width = frame_width(window->frame);
614 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400615 } else {
616 *width = window->width + t->margin * 2;
617 *height = window->height + t->margin * 2;
618 }
619}
620
621static void
622weston_wm_window_get_child_position(struct weston_wm_window *window,
623 int *x, int *y)
624{
625 struct theme *t = window->wm->theme;
626
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500627 if (window->fullscreen) {
628 *x = 0;
629 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800630 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500631 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400632 } else {
633 *x = t->margin;
634 *y = t->margin;
635 }
636}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400637
638static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500639weston_wm_window_send_configure_notify(struct weston_wm_window *window)
640{
641 xcb_configure_notify_event_t configure_notify;
642 struct weston_wm *wm = window->wm;
643 int x, y;
644
645 weston_wm_window_get_child_position(window, &x, &y);
646 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
647 configure_notify.pad0 = 0;
648 configure_notify.event = window->id;
649 configure_notify.window = window->id;
650 configure_notify.above_sibling = XCB_WINDOW_NONE;
651 configure_notify.x = x;
652 configure_notify.y = y;
653 configure_notify.width = window->width;
654 configure_notify.height = window->height;
655 configure_notify.border_width = 0;
656 configure_notify.override_redirect = 0;
657 configure_notify.pad1 = 0;
658
Murray Calavera883ac022015-06-06 13:02:22 +0000659 xcb_send_event(wm->conn, 0, window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500660 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
661 (char *) &configure_notify);
662}
663
664static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400665weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
666{
Murray Calavera883ac022015-06-06 13:02:22 +0000667 xcb_configure_request_event_t *configure_request =
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400668 (xcb_configure_request_event_t *) event;
669 struct weston_wm_window *window;
670 uint32_t mask, values[16];
671 int x, y, width, height, i = 0;
672
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400673 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
674 configure_request->window,
675 configure_request->x, configure_request->y,
676 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400677
Derek Foreman49372142015-04-09 10:51:22 -0500678 if (!wm_lookup_window(wm, configure_request->window, &window))
679 return;
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400680
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500681 if (window->fullscreen) {
682 weston_wm_window_send_configure_notify(window);
683 return;
684 }
685
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400686 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
687 window->width = configure_request->width;
688 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
689 window->height = configure_request->height;
690
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500691 if (window->frame)
692 frame_resize_inside(window->frame, window->width, window->height);
693
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400694 weston_wm_window_get_child_position(window, &x, &y);
695 values[i++] = x;
696 values[i++] = y;
697 values[i++] = window->width;
698 values[i++] = window->height;
699 values[i++] = 0;
700 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
701 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
702 XCB_CONFIG_WINDOW_BORDER_WIDTH;
703 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
704 values[i++] = configure_request->sibling;
705 mask |= XCB_CONFIG_WINDOW_SIBLING;
706 }
707 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
708 values[i++] = configure_request->stack_mode;
709 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
710 }
711
712 xcb_configure_window(wm->conn, window->id, mask, values);
713
714 weston_wm_window_get_frame_size(window, &width, &height);
715 values[0] = width;
716 values[1] = height;
717 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
718 xcb_configure_window(wm->conn, window->frame_id, mask, values);
719
720 weston_wm_window_schedule_repaint(window);
721}
722
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400723static int
724our_resource(struct weston_wm *wm, uint32_t id)
725{
726 const xcb_setup_t *setup;
727
728 setup = xcb_get_setup(wm->conn);
729
730 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
731}
732
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400733static void
734weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
735{
Murray Calavera883ac022015-06-06 13:02:22 +0000736 xcb_configure_notify_event_t *configure_notify =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400737 (xcb_configure_notify_event_t *) event;
738 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400739
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400740 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400741 configure_notify->window,
742 configure_notify->x, configure_notify->y,
743 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300744
Derek Foreman49372142015-04-09 10:51:22 -0500745 if (!wm_lookup_window(wm, configure_notify->window, &window))
746 return;
747
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700748 window->x = configure_notify->x;
749 window->y = configure_notify->y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200750 window->pos_dirty = false;
751
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700752 if (window->override_redirect) {
753 window->width = configure_notify->width;
754 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500755 if (window->frame)
756 frame_resize_inside(window->frame,
757 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700758 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400759}
760
761static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300762weston_wm_kill_client(struct wl_listener *listener, void *data)
763{
764 struct weston_surface *surface = data;
765 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300766 if (!window)
767 return;
768
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200769 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300770 kill(window->pid, SIGKILL);
771}
772
773static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700774weston_wm_create_surface(struct wl_listener *listener, void *data)
775{
776 struct weston_surface *surface = data;
777 struct weston_wm *wm =
778 container_of(listener,
779 struct weston_wm, create_surface_listener);
780 struct weston_wm_window *window;
781
782 if (wl_resource_get_client(surface->resource) != wm->server->client)
783 return;
784
785 wl_list_for_each(window, &wm->unpaired_window_list, link)
786 if (window->surface_id ==
787 wl_resource_get_id(surface->resource)) {
788 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700789 window->surface_id = 0;
790 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700791 break;
Murray Calavera883ac022015-06-06 13:02:22 +0000792 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700793}
794
795static void
Giulio Camuffob18f7882015-03-29 14:20:23 +0300796weston_wm_send_focus_window(struct weston_wm *wm,
797 struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400798{
Scott Moreau85ecac02012-05-21 15:49:14 -0600799 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400800
Scott Moreau85ecac02012-05-21 15:49:14 -0600801 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700802 uint32_t values[1];
803
Boyan Dingb9f863c2014-08-30 10:33:23 +0800804 if (window->override_redirect)
805 return;
806
Scott Moreau85ecac02012-05-21 15:49:14 -0600807 client_message.response_type = XCB_CLIENT_MESSAGE;
808 client_message.format = 32;
809 client_message.window = window->id;
810 client_message.type = wm->atom.wm_protocols;
811 client_message.data.data32[0] = wm->atom.wm_take_focus;
812 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
813
Murray Calavera883ac022015-06-06 13:02:22 +0000814 xcb_send_event(wm->conn, 0, window->id,
Scott Moreau85ecac02012-05-21 15:49:14 -0600815 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
816 (char *) &client_message);
817
818 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
819 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700820
821 values[0] = XCB_STACK_MODE_ABOVE;
822 xcb_configure_window (wm->conn, window->frame_id,
823 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600824 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400825 xcb_set_input_focus (wm->conn,
826 XCB_INPUT_FOCUS_POINTER_ROOT,
827 XCB_NONE,
828 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600829 }
Giulio Camuffob18f7882015-03-29 14:20:23 +0300830}
831
832static void
833weston_wm_window_activate(struct wl_listener *listener, void *data)
834{
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +0800835 struct weston_surface_activation_data *activation_data = data;
836 struct weston_surface *surface = activation_data->surface;
Giulio Camuffob18f7882015-03-29 14:20:23 +0300837 struct weston_wm_window *window = NULL;
838 struct weston_wm *wm =
839 container_of(listener, struct weston_wm, activate_listener);
840
841 if (surface) {
842 window = get_wm_window(surface);
843 }
844
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200845 if (window) {
846 weston_wm_set_net_active_window(wm, window->id);
847 } else {
848 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
849 }
850
Giulio Camuffob18f7882015-03-29 14:20:23 +0300851 weston_wm_send_focus_window(wm, window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400852
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500853 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800854 if (wm->focus_window->frame)
855 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400856 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500857 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400858 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500859 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800860 if (wm->focus_window->frame)
861 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400862 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500863 }
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200864
865 xcb_flush(wm->conn);
866
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400867}
868
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400869#define ICCCM_WITHDRAWN_STATE 0
870#define ICCCM_NORMAL_STATE 1
871#define ICCCM_ICONIC_STATE 3
872
873static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500874weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400875{
876 struct weston_wm *wm = window->wm;
877 uint32_t property[2];
878
879 property[0] = state;
880 property[1] = XCB_WINDOW_NONE;
881
882 xcb_change_property(wm->conn,
883 XCB_PROP_MODE_REPLACE,
884 window->id,
885 wm->atom.wm_state,
886 wm->atom.wm_state,
887 32, /* format */
888 2, property);
889}
890
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400891static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500892weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
893{
894 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200895 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500896 int i;
897
898 i = 0;
899 if (window->fullscreen)
900 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200901 if (window->maximized_vert)
902 property[i++] = wm->atom.net_wm_state_maximized_vert;
903 if (window->maximized_horz)
904 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500905
906 xcb_change_property(wm->conn,
907 XCB_PROP_MODE_REPLACE,
908 window->id,
909 wm->atom.net_wm_state,
910 XCB_ATOM_ATOM,
911 32, /* format */
912 i, property);
913}
914
915static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700916weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400917{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700918 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400919 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400920 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200921 int buttons = FRAME_BUTTON_CLOSE;
922
923 if (window->decorate & MWM_DECOR_MAXIMIZE)
924 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400925
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500926 window->frame = frame_create(window->wm->theme,
927 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200928 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500929 frame_resize_inside(window->frame, window->width, window->height);
930
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400931 weston_wm_window_get_frame_size(window, &width, &height);
932 weston_wm_window_get_child_position(window, &x, &y);
933
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400934 values[0] = wm->screen->black_pixel;
935 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400936 XCB_EVENT_MASK_KEY_PRESS |
937 XCB_EVENT_MASK_KEY_RELEASE |
938 XCB_EVENT_MASK_BUTTON_PRESS |
939 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400940 XCB_EVENT_MASK_POINTER_MOTION |
941 XCB_EVENT_MASK_ENTER_WINDOW |
942 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400943 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400944 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400945 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400946
947 window->frame_id = xcb_generate_id(wm->conn);
948 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400949 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400950 window->frame_id,
951 wm->screen->root,
952 0, 0,
953 width, height,
954 0,
955 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400956 wm->visual_id,
957 XCB_CW_BORDER_PIXEL |
958 XCB_CW_EVENT_MASK |
959 XCB_CW_COLORMAP, values);
960
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400961 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
962
963 values[0] = 0;
964 xcb_configure_window(wm->conn, window->id,
965 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
966
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400967 window->cairo_surface =
968 cairo_xcb_surface_create_with_xrender_format(wm->conn,
969 wm->screen,
970 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400971 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400972 width, height);
973
974 hash_table_insert(wm->window_hash, window->frame_id, window);
975}
976
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200977/*
978 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
979 * Passing a <0 desktop value deletes the property.
980 */
981static void
982weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
Bryce Harringtone00554b2015-06-12 10:17:39 -0700983 int desktop)
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200984{
985 if (desktop >= 0) {
986 xcb_change_property(window->wm->conn,
987 XCB_PROP_MODE_REPLACE,
988 window->id,
989 window->wm->atom.net_wm_desktop,
990 XCB_ATOM_CARDINAL,
991 32, /* format */
992 1, &desktop);
993 } else {
994 xcb_delete_property(window->wm->conn,
995 window->id,
996 window->wm->atom.net_wm_desktop);
997 }
998}
999
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001000static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001001weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
1002{
1003 xcb_map_request_event_t *map_request =
1004 (xcb_map_request_event_t *) event;
1005 struct weston_wm_window *window;
Pekka Paalanen505237e2016-12-01 15:41:11 +02001006 struct weston_output *output;
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001007
1008 if (our_resource(wm, map_request->window)) {
1009 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
1010 map_request->window);
1011 return;
1012 }
1013
Derek Foreman49372142015-04-09 10:51:22 -05001014 if (!wm_lookup_window(wm, map_request->window, &window))
1015 return;
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001016
1017 weston_wm_window_read_properties(window);
1018
Pekka Paalanenc9ca2c02016-12-15 16:04:14 +02001019 /* For a new Window, MapRequest happens before the Window is realized
1020 * in Xwayland. We do the real xcb_map_window() here as a response to
1021 * MapRequest. The Window will get realized (wl_surface created in
1022 * Wayland and WL_SURFACE_ID sent in X11) when it has been mapped for
1023 * real.
1024 *
1025 * MapRequest only happens for (X11) unmapped Windows. On UnmapNotify,
1026 * we reset shsurf to NULL, so even if X11 connection races far ahead
1027 * of the Wayland connection and the X11 client is repeatedly mapping
1028 * and unmapping, we will never have shsurf set on MapRequest.
1029 */
1030 assert(!window->shsurf);
1031
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001032 if (window->frame_id == XCB_WINDOW_NONE)
1033 weston_wm_window_create_frame(window);
1034
Pekka Paalanen44660c32016-11-14 15:38:43 +02001035 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d, %dx%d @ %d,%d)\n",
1036 window->id, window, window->frame_id,
1037 window->width, window->height, window->x, window->y);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001038
1039 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
1040 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001041 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001042
Pekka Paalanen505237e2016-12-01 15:41:11 +02001043 if (legacy_fullscreen(wm, window, &output)) {
1044 window->fullscreen = 1;
1045 weston_output_weak_ref_set(&window->legacy_fullscreen_output,
1046 output);
1047 }
1048
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001049 xcb_map_window(wm->conn, map_request->window);
1050 xcb_map_window(wm->conn, window->frame_id);
1051}
1052
1053static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001054weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1055{
1056 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
1057
1058 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001059 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
1060 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001061 return;
1062 }
1063
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001064 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001065}
1066
1067static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001068weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1069{
1070 xcb_unmap_notify_event_t *unmap_notify =
1071 (xcb_unmap_notify_event_t *) event;
1072 struct weston_wm_window *window;
1073
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001074 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
1075 unmap_notify->window,
1076 unmap_notify->event,
1077 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001078
1079 if (our_resource(wm, unmap_notify->window))
1080 return;
1081
MoD31700122013-06-11 19:58:55 -05001082 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -04001083 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1084 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001085 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001086
Derek Foreman49372142015-04-09 10:51:22 -05001087 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1088 return;
1089
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001090 if (window->surface_id) {
1091 /* Make sure we're not on the unpaired surface list or we
1092 * could be assigned a surface during surface creation that
1093 * was mapped before this unmap request.
1094 */
1095 wl_list_remove(&window->link);
1096 window->surface_id = 0;
1097 }
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001098 if (wm->focus_window == window)
1099 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001100 if (window->surface)
1101 wl_list_remove(&window->surface_destroy_listener.link);
1102 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +02001103 window->shsurf = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001104
1105 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1106 weston_wm_window_set_virtual_desktop(window, -1);
1107
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001108 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001109}
1110
1111static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001112weston_wm_window_draw_decoration(void *data)
1113{
1114 struct weston_wm_window *window = data;
1115 struct weston_wm *wm = window->wm;
1116 struct theme *t = wm->theme;
1117 cairo_t *cr;
1118 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001119 int32_t input_x, input_y, input_w, input_h;
Quentin Glidic955cec02016-08-12 10:41:35 +02001120 const struct weston_desktop_xwayland_interface *xwayland_interface =
1121 wm->server->compositor->xwayland_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001122 uint32_t flags = 0;
Giulio Camuffoaa974782015-02-01 16:18:51 +02001123 struct weston_view *view;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001124
Pekka Paalanena04eacc2016-11-28 16:42:25 +02001125 wm_log("XWM: start draw decoration, win %d\n", window->id);
1126
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001127 weston_wm_window_read_properties(window);
1128
1129 window->repaint_source = NULL;
1130
1131 weston_wm_window_get_frame_size(window, &width, &height);
1132 weston_wm_window_get_child_position(window, &x, &y);
1133
1134 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1135 cr = cairo_create(window->cairo_surface);
1136
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001137 if (window->fullscreen) {
1138 /* nothing */
1139 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001140 if (wm->focus_window == window)
1141 flags |= THEME_FRAME_ACTIVE;
1142
Pekka Paalanen9a330e12016-12-15 15:37:24 +02001143 frame_set_title(window->frame, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001144 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001145 } else {
1146 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1147 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1148 cairo_paint(cr);
1149
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001150 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001151 }
1152
1153 cairo_destroy(cr);
1154
1155 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001156 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001157 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001158 pixman_region32_init(&window->surface->pending.opaque);
1159 } else {
1160 /* We leave an extra pixel around the X window area to
1161 * make sure we don't sample from the undefined alpha
1162 * channel when filtering. */
Murray Calavera883ac022015-06-06 13:02:22 +00001163 pixman_region32_init_rect(&window->surface->pending.opaque,
MoD384a11a2013-06-22 11:04:21 -05001164 x - 1, y - 1,
1165 window->width + 2,
1166 window->height + 2);
1167 }
Giulio Camuffoaa974782015-02-01 16:18:51 +02001168 wl_list_for_each(view, &window->surface->views, surface_link)
1169 weston_view_geometry_dirty(view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001170
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001171 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001172
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001173 if (window->decorate && !window->fullscreen) {
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001174 frame_input_rect(window->frame, &input_x, &input_y,
1175 &input_w, &input_h);
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001176 } else {
1177 input_x = x;
1178 input_y = y;
1179 input_w = width;
1180 input_h = height;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001181 }
1182
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001183 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001184 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001185
Pekka Paalanena04eacc2016-11-28 16:42:25 +02001186 wm_log("XWM: draw decoration, win %d geometry: %d,%d %dx%d\n",
1187 window->id, input_x, input_y, input_w, input_h);
1188
Quentin Glidic955cec02016-08-12 10:41:35 +02001189 xwayland_interface->set_window_geometry(window->shsurf,
1190 input_x, input_y, input_w, input_h);
Pekka Paalanenc9ca2c02016-12-15 16:04:14 +02001191 if (window->name)
1192 xwayland_interface->set_title(window->shsurf, window->name);
1193 if (window->pid > 0)
1194 xwayland_interface->set_pid(window->shsurf, window->pid);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001195 }
1196}
1197
1198static void
1199weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1200{
1201 struct weston_wm *wm = window->wm;
Giulio Camuffoaa974782015-02-01 16:18:51 +02001202 struct weston_view *view;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001203 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001204
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001205 if (window->frame_id == XCB_WINDOW_NONE) {
1206 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001207 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001208 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001209 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001210 pixman_region32_init(&window->surface->pending.opaque);
1211 } else {
1212 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1213 width, height);
1214 }
Giulio Camuffoaa974782015-02-01 16:18:51 +02001215 wl_list_for_each(view, &window->surface->views, surface_link)
1216 weston_view_geometry_dirty(view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001217 }
1218 return;
1219 }
1220
1221 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001222 return;
1223
Pekka Paalanena04eacc2016-11-28 16:42:25 +02001224 wm_log("XWM: schedule repaint, win %d\n", window->id);
1225
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001226 window->repaint_source =
1227 wl_event_loop_add_idle(wm->server->loop,
1228 weston_wm_window_draw_decoration,
1229 window);
1230}
1231
1232static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001233weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1234{
1235 xcb_property_notify_event_t *property_notify =
1236 (xcb_property_notify_event_t *) event;
1237 struct weston_wm_window *window;
1238
Derek Foreman49372142015-04-09 10:51:22 -05001239 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001240 return;
1241
1242 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001243
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001244 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001245 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001246 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001247 else
1248 read_and_dump_property(wm, property_notify->window,
1249 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001250
1251 if (property_notify->atom == wm->atom.net_wm_name ||
1252 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001253 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001254}
1255
1256static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001257weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001258 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001259{
1260 struct weston_wm_window *window;
1261 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001262 xcb_get_geometry_cookie_t geometry_cookie;
1263 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001264
Peter Huttererf3d62272013-08-08 11:57:05 +10001265 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001266 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001267 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001268 return;
1269 }
1270
MoD384a11a2013-06-22 11:04:21 -05001271 geometry_cookie = xcb_get_geometry(wm->conn, id);
1272
Giulio Camuffob18f7882015-03-29 14:20:23 +03001273 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
1274 XCB_EVENT_MASK_FOCUS_CHANGE;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001275 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1276
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001277 window->wm = wm;
1278 window->id = id;
1279 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001280 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001281 window->width = width;
1282 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001283 window->x = x;
1284 window->y = y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001285 window->pos_dirty = false;
Pekka Paalanen505237e2016-12-01 15:41:11 +02001286 weston_output_weak_ref_init(&window->legacy_fullscreen_output);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001287
MoD384a11a2013-06-22 11:04:21 -05001288 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1289 /* technically we should use XRender and check the visual format's
1290 alpha_mask, but checking depth is simpler and works in all known cases */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001291 if (geometry_reply != NULL)
MoD384a11a2013-06-22 11:04:21 -05001292 window->has_alpha = geometry_reply->depth == 32;
1293 free(geometry_reply);
1294
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001295 hash_table_insert(wm->window_hash, id, window);
1296}
1297
1298static void
1299weston_wm_window_destroy(struct weston_wm_window *window)
1300{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001301 struct weston_wm *wm = window->wm;
1302
Pekka Paalanen505237e2016-12-01 15:41:11 +02001303 weston_output_weak_ref_clear(&window->legacy_fullscreen_output);
1304
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001305 if (window->repaint_source)
1306 wl_event_source_remove(window->repaint_source);
1307 if (window->cairo_surface)
1308 cairo_surface_destroy(window->cairo_surface);
1309
1310 if (window->frame_id) {
1311 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1312 xcb_destroy_window(wm->conn, window->frame_id);
1313 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001314 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001315 hash_table_remove(wm->window_hash, window->frame_id);
1316 window->frame_id = XCB_WINDOW_NONE;
1317 }
1318
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001319 if (window->surface_id)
1320 wl_list_remove(&window->link);
1321
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001322 if (window->surface)
1323 wl_list_remove(&window->surface_destroy_listener.link);
1324
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001325 hash_table_remove(window->wm->window_hash, window->id);
1326 free(window);
1327}
1328
1329static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001330weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1331{
1332 xcb_create_notify_event_t *create_notify =
1333 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001334
Pekka Paalanen7db6c432016-11-14 14:30:57 +02001335 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 -04001336 create_notify->window,
Pekka Paalanen7db6c432016-11-14 14:30:57 +02001337 create_notify->x, create_notify->y,
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001338 create_notify->width, create_notify->height,
1339 create_notify->override_redirect ? ", override" : "",
1340 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001341
1342 if (our_resource(wm, create_notify->window))
1343 return;
1344
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001345 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001346 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001347 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001348 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001349}
1350
1351static void
1352weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1353{
1354 xcb_destroy_notify_event_t *destroy_notify =
1355 (xcb_destroy_notify_event_t *) event;
1356 struct weston_wm_window *window;
1357
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001358 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1359 destroy_notify->window,
1360 destroy_notify->event,
1361 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001362
1363 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001364 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001365
Derek Foreman49372142015-04-09 10:51:22 -05001366 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1367 return;
1368
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001369 weston_wm_window_destroy(window);
1370}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001371
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001372static void
1373weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1374{
1375 xcb_reparent_notify_event_t *reparent_notify =
1376 (xcb_reparent_notify_event_t *) event;
1377 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001378
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001379 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1380 reparent_notify->window,
1381 reparent_notify->parent,
1382 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001383
1384 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001385 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001386 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001387 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001388 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001389 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1390 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001391 weston_wm_window_destroy(window);
1392 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001393}
1394
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001395struct weston_seat *
1396weston_wm_pick_seat(struct weston_wm *wm)
1397{
Tom Hochsteine7fff212016-11-01 14:14:00 -05001398 struct wl_list *seats = wm->server->compositor->seat_list.next;
1399 if (wl_list_empty(seats))
1400 return NULL;
1401 return container_of(seats, struct weston_seat, link);
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001402}
1403
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001404static struct weston_seat *
1405weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1406{
1407 struct weston_wm *wm = window->wm;
1408 struct weston_seat *seat, *s;
1409
1410 seat = NULL;
1411 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001412 struct weston_pointer *pointer = weston_seat_get_pointer(s);
1413 struct weston_pointer *old_pointer =
1414 weston_seat_get_pointer(seat);
1415
1416 if (pointer && pointer->focus &&
1417 pointer->focus->surface == window->surface &&
1418 pointer->button_count > 0 &&
1419 (!seat ||
1420 pointer->grab_serial -
1421 old_pointer->grab_serial < (1 << 30)))
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001422 seat = s;
1423 }
1424
1425 return seat;
1426}
1427
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001428static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001429weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1430 xcb_client_message_event_t *client_message)
1431{
1432 static const int map[] = {
1433 THEME_LOCATION_RESIZING_TOP_LEFT,
1434 THEME_LOCATION_RESIZING_TOP,
1435 THEME_LOCATION_RESIZING_TOP_RIGHT,
1436 THEME_LOCATION_RESIZING_RIGHT,
1437 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1438 THEME_LOCATION_RESIZING_BOTTOM,
1439 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1440 THEME_LOCATION_RESIZING_LEFT
1441 };
1442
1443 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001444 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Derek Foreman1281a362015-07-31 16:55:32 -05001445 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001446 int detail;
Quentin Glidic955cec02016-08-12 10:41:35 +02001447 const struct weston_desktop_xwayland_interface *xwayland_interface =
1448 wm->server->compositor->xwayland_interface;
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001449
Derek Foreman1281a362015-07-31 16:55:32 -05001450 if (!pointer || pointer->button_count != 1
1451 || !pointer->focus
1452 || pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001453 return;
1454
1455 detail = client_message->data.data32[2];
1456 switch (detail) {
1457 case _NET_WM_MOVERESIZE_MOVE:
Quentin Glidic955cec02016-08-12 10:41:35 +02001458 xwayland_interface->move(window->shsurf, pointer);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001459 break;
1460 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1461 case _NET_WM_MOVERESIZE_SIZE_TOP:
1462 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1463 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1464 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1465 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1466 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1467 case _NET_WM_MOVERESIZE_SIZE_LEFT:
Quentin Glidic955cec02016-08-12 10:41:35 +02001468 xwayland_interface->resize(window->shsurf, pointer, map[detail]);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001469 break;
1470 case _NET_WM_MOVERESIZE_CANCEL:
1471 break;
1472 }
1473}
1474
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001475#define _NET_WM_STATE_REMOVE 0
1476#define _NET_WM_STATE_ADD 1
1477#define _NET_WM_STATE_TOGGLE 2
1478
1479static int
1480update_state(int action, int *state)
1481{
1482 int new_state, changed;
1483
1484 switch (action) {
1485 case _NET_WM_STATE_REMOVE:
1486 new_state = 0;
1487 break;
1488 case _NET_WM_STATE_ADD:
1489 new_state = 1;
1490 break;
1491 case _NET_WM_STATE_TOGGLE:
1492 new_state = !*state;
1493 break;
1494 default:
1495 return 0;
1496 }
1497
1498 changed = (*state != new_state);
1499 *state = new_state;
1500
1501 return changed;
1502}
1503
1504static void
1505weston_wm_window_configure(void *data);
1506
1507static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001508weston_wm_window_set_toplevel(struct weston_wm_window *window)
1509{
Quentin Glidic955cec02016-08-12 10:41:35 +02001510 const struct weston_desktop_xwayland_interface *xwayland_interface =
1511 window->wm->server->compositor->xwayland_interface;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001512
Quentin Glidic955cec02016-08-12 10:41:35 +02001513 xwayland_interface->set_toplevel(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001514 window->width = window->saved_width;
1515 window->height = window->saved_height;
1516 if (window->frame)
1517 frame_resize_inside(window->frame,
1518 window->width,
1519 window->height);
1520 weston_wm_window_configure(window);
1521}
1522
1523static inline bool
1524weston_wm_window_is_maximized(struct weston_wm_window *window)
1525{
1526 return window->maximized_horz && window->maximized_vert;
1527}
1528
1529static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001530weston_wm_window_handle_state(struct weston_wm_window *window,
1531 xcb_client_message_event_t *client_message)
1532{
1533 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +02001534 const struct weston_desktop_xwayland_interface *xwayland_interface =
1535 wm->server->compositor->xwayland_interface;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001536 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001537 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001538
1539 action = client_message->data.data32[0];
1540 property = client_message->data.data32[1];
1541
1542 if (property == wm->atom.net_wm_state_fullscreen &&
1543 update_state(action, &window->fullscreen)) {
1544 weston_wm_window_set_net_wm_state(window);
1545 if (window->fullscreen) {
1546 window->saved_width = window->width;
1547 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001548
1549 if (window->shsurf)
Quentin Glidic955cec02016-08-12 10:41:35 +02001550 xwayland_interface->set_fullscreen(window->shsurf,
1551 NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001552 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001553 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001554 weston_wm_window_set_toplevel(window);
1555 }
1556 } else {
1557 if (property == wm->atom.net_wm_state_maximized_vert &&
1558 update_state(action, &window->maximized_vert))
1559 weston_wm_window_set_net_wm_state(window);
1560 if (property == wm->atom.net_wm_state_maximized_horz &&
1561 update_state(action, &window->maximized_horz))
1562 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001563
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001564 if (maximized != weston_wm_window_is_maximized(window)) {
1565 if (weston_wm_window_is_maximized(window)) {
1566 window->saved_width = window->width;
1567 window->saved_height = window->height;
1568
1569 if (window->shsurf)
Quentin Glidic955cec02016-08-12 10:41:35 +02001570 xwayland_interface->set_maximized(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001571 } else if (window->shsurf) {
1572 weston_wm_window_set_toplevel(window);
1573 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001574 }
1575 }
1576}
1577
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001578static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001579surface_destroy(struct wl_listener *listener, void *data)
1580{
1581 struct weston_wm_window *window =
1582 container_of(listener,
1583 struct weston_wm_window, surface_destroy_listener);
1584
1585 wm_log("surface for xid %d destroyed\n", window->id);
1586
1587 /* This should have been freed by the shell.
1588 * Don't try to use it later. */
1589 window->shsurf = NULL;
1590 window->surface = NULL;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001591}
1592
1593static void
1594weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1595 xcb_client_message_event_t *client_message)
1596{
1597 struct weston_wm *wm = window->wm;
1598 struct wl_resource *resource;
1599
1600 if (window->surface_id != 0) {
1601 wm_log("already have surface id for window %d\n", window->id);
1602 return;
1603 }
1604
1605 /* Xwayland will send the wayland requests to create the
1606 * wl_surface before sending this client message. Even so, we
1607 * can end up handling the X event before the wayland requests
1608 * and thus when we try to look up the surface ID, the surface
1609 * hasn't been created yet. In that case put the window on
1610 * the unpaired window list and continue when the surface gets
1611 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001612 uint32_t id = client_message->data.data32[0];
1613 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001614 if (resource) {
1615 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001616 xserver_map_shell_surface(window,
1617 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001618 }
1619 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001620 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001621 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001622 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001623}
1624
1625static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001626weston_wm_handle_client_message(struct weston_wm *wm,
1627 xcb_generic_event_t *event)
1628{
1629 xcb_client_message_event_t *client_message =
1630 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001631 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001632
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001633 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1634 get_atom_name(wm->conn, client_message->type),
1635 client_message->data.data32[0],
1636 client_message->data.data32[1],
1637 client_message->data.data32[2],
1638 client_message->data.data32[3],
1639 client_message->data.data32[4],
1640 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001641
Jason Ekstrand0250a742014-07-24 13:17:47 -07001642 /* The window may get created and destroyed before we actually
1643 * handle the message. If it doesn't exist, bail.
1644 */
Derek Foreman49372142015-04-09 10:51:22 -05001645 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001646 return;
1647
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001648 if (client_message->type == wm->atom.net_wm_moveresize)
1649 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001650 else if (client_message->type == wm->atom.net_wm_state)
1651 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001652 else if (client_message->type == wm->atom.wl_surface_id)
1653 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001654}
1655
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001656enum cursor_type {
1657 XWM_CURSOR_TOP,
1658 XWM_CURSOR_BOTTOM,
1659 XWM_CURSOR_LEFT,
1660 XWM_CURSOR_RIGHT,
1661 XWM_CURSOR_TOP_LEFT,
1662 XWM_CURSOR_TOP_RIGHT,
1663 XWM_CURSOR_BOTTOM_LEFT,
1664 XWM_CURSOR_BOTTOM_RIGHT,
1665 XWM_CURSOR_LEFT_PTR,
1666};
1667
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001668/*
1669 * The following correspondences between file names and cursors was copied
1670 * from: https://bugs.kde.org/attachment.cgi?id=67313
1671 */
1672
1673static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001674 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001675 "sw-resize",
1676 "size_bdiag"
1677};
1678
1679static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001680 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001681 "se-resize",
1682 "size_fdiag"
1683};
1684
1685static const char *bottom_sides[] = {
1686 "bottom_side",
1687 "s-resize",
1688 "size_ver"
1689};
1690
1691static const char *left_ptrs[] = {
1692 "left_ptr",
1693 "default",
1694 "top_left_arrow",
1695 "left-arrow"
1696};
1697
1698static const char *left_sides[] = {
1699 "left_side",
1700 "w-resize",
1701 "size_hor"
1702};
1703
1704static const char *right_sides[] = {
1705 "right_side",
1706 "e-resize",
1707 "size_hor"
1708};
1709
1710static const char *top_left_corners[] = {
1711 "top_left_corner",
1712 "nw-resize",
1713 "size_fdiag"
1714};
1715
1716static const char *top_right_corners[] = {
1717 "top_right_corner",
1718 "ne-resize",
1719 "size_bdiag"
1720};
1721
1722static const char *top_sides[] = {
1723 "top_side",
1724 "n-resize",
1725 "size_ver"
1726};
1727
1728struct cursor_alternatives {
1729 const char **names;
1730 size_t count;
1731};
1732
1733static const struct cursor_alternatives cursors[] = {
1734 {top_sides, ARRAY_LENGTH(top_sides)},
1735 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1736 {left_sides, ARRAY_LENGTH(left_sides)},
1737 {right_sides, ARRAY_LENGTH(right_sides)},
1738 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1739 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1740 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1741 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1742 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001743};
1744
1745static void
1746weston_wm_create_cursors(struct weston_wm *wm)
1747{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001748 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001749 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001750 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001751
1752 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1753 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001754 for (j = 0; j < cursors[i].count; j++) {
1755 name = cursors[i].names[j];
1756 wm->cursors[i] =
1757 xcb_cursor_library_load_cursor(wm, name);
1758 if (wm->cursors[i] != (xcb_cursor_t)-1)
1759 break;
1760 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001761 }
1762
1763 wm->last_cursor = -1;
1764}
1765
1766static void
1767weston_wm_destroy_cursors(struct weston_wm *wm)
1768{
1769 uint8_t i;
1770
1771 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1772 xcb_free_cursor(wm->conn, wm->cursors[i]);
1773
1774 free(wm->cursors);
1775}
1776
1777static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001778get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001779{
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001780 switch (location) {
1781 case THEME_LOCATION_RESIZING_TOP:
1782 return XWM_CURSOR_TOP;
1783 case THEME_LOCATION_RESIZING_BOTTOM:
1784 return XWM_CURSOR_BOTTOM;
1785 case THEME_LOCATION_RESIZING_LEFT:
1786 return XWM_CURSOR_LEFT;
1787 case THEME_LOCATION_RESIZING_RIGHT:
1788 return XWM_CURSOR_RIGHT;
1789 case THEME_LOCATION_RESIZING_TOP_LEFT:
1790 return XWM_CURSOR_TOP_LEFT;
1791 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1792 return XWM_CURSOR_TOP_RIGHT;
1793 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1794 return XWM_CURSOR_BOTTOM_LEFT;
1795 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1796 return XWM_CURSOR_BOTTOM_RIGHT;
1797 case THEME_LOCATION_EXTERIOR:
1798 case THEME_LOCATION_TITLEBAR:
1799 default:
1800 return XWM_CURSOR_LEFT_PTR;
1801 }
1802}
1803
1804static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001805weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1806 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001807{
1808 uint32_t cursor_value_list;
1809
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001810 if (wm->last_cursor == cursor)
1811 return;
1812
1813 wm->last_cursor = cursor;
1814
1815 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001816 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001817 XCB_CW_CURSOR, &cursor_value_list);
1818 xcb_flush(wm->conn);
1819}
1820
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001821static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001822weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001823{
1824 xcb_client_message_event_t client_message;
1825
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001826 if (window->delete_window) {
1827 client_message.response_type = XCB_CLIENT_MESSAGE;
1828 client_message.format = 32;
1829 client_message.window = window->id;
1830 client_message.type = window->wm->atom.wm_protocols;
1831 client_message.data.data32[0] =
1832 window->wm->atom.wm_delete_window;
1833 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001834
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001835 xcb_send_event(window->wm->conn, 0, window->id,
1836 XCB_EVENT_MASK_NO_EVENT,
1837 (char *) &client_message);
1838 } else {
1839 xcb_kill_client(window->wm->conn, window->id);
1840 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001841}
1842
1843static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001844weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1845{
1846 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
Quentin Glidic955cec02016-08-12 10:41:35 +02001847 const struct weston_desktop_xwayland_interface *xwayland_interface =
1848 wm->server->compositor->xwayland_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001849 struct weston_seat *seat;
Derek Foremane4d6c832015-08-05 14:48:11 -05001850 struct weston_pointer *pointer;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001851 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001852 enum theme_location location;
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001853 enum wl_pointer_button_state button_state;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001854 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001855
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001856 wm_log("XCB_BUTTON_%s (detail %d)\n",
1857 button->response_type == XCB_BUTTON_PRESS ?
1858 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001859
Derek Foreman49372142015-04-09 10:51:22 -05001860 if (!wm_lookup_window(wm, button->event, &window) ||
1861 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001862 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001863
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001864 if (button->detail != 1 && button->detail != 2)
1865 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001866
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001867 seat = weston_wm_pick_seat_for_window(window);
Derek Foremane4d6c832015-08-05 14:48:11 -05001868 pointer = weston_seat_get_pointer(seat);
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001869
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001870 button_state = button->response_type == XCB_BUTTON_PRESS ?
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001871 WL_POINTER_BUTTON_STATE_PRESSED :
1872 WL_POINTER_BUTTON_STATE_RELEASED;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001873 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1874
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001875 /* Make sure we're looking at the right location. The frame
1876 * could have received a motion event from a pointer from a
1877 * different wl_seat, but under X it looks like our core
1878 * pointer moved. Move the frame pointer to the button press
1879 * location before deciding what to do. */
1880 location = frame_pointer_motion(window->frame, NULL,
1881 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001882 location = frame_pointer_button(window->frame, NULL,
1883 button_id, button_state);
1884 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1885 weston_wm_window_schedule_repaint(window);
1886
1887 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001888 if (pointer)
Quentin Glidic955cec02016-08-12 10:41:35 +02001889 xwayland_interface->move(window->shsurf, pointer);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001890 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1891 }
1892
1893 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001894 if (pointer)
Quentin Glidic955cec02016-08-12 10:41:35 +02001895 xwayland_interface->resize(window->shsurf, pointer, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001896 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1897 }
1898
1899 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001900 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001901 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001902 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001903
1904 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1905 window->maximized_horz = !window->maximized_horz;
1906 window->maximized_vert = !window->maximized_vert;
1907 if (weston_wm_window_is_maximized(window)) {
1908 window->saved_width = window->width;
1909 window->saved_height = window->height;
Quentin Glidic955cec02016-08-12 10:41:35 +02001910 xwayland_interface->set_maximized(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001911 } else {
1912 weston_wm_window_set_toplevel(window);
1913 }
1914 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1915 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001916}
1917
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001918static void
1919weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1920{
1921 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1922 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001923 enum theme_location location;
1924 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001925
Derek Foreman49372142015-04-09 10:51:22 -05001926 if (!wm_lookup_window(wm, motion->event, &window) ||
1927 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001928 return;
1929
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001930 location = frame_pointer_motion(window->frame, NULL,
1931 motion->event_x, motion->event_y);
1932 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1933 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001934
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001935 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001936 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001937}
1938
1939static void
1940weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1941{
1942 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1943 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001944 enum theme_location location;
1945 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001946
Derek Foreman49372142015-04-09 10:51:22 -05001947 if (!wm_lookup_window(wm, enter->event, &window) ||
1948 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001949 return;
1950
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001951 location = frame_pointer_enter(window->frame, NULL,
1952 enter->event_x, enter->event_y);
1953 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1954 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001955
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001956 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001957 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001958}
1959
1960static void
1961weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1962{
1963 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1964 struct weston_wm_window *window;
1965
Derek Foreman49372142015-04-09 10:51:22 -05001966 if (!wm_lookup_window(wm, leave->event, &window) ||
1967 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001968 return;
1969
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001970 frame_pointer_leave(window->frame, NULL);
1971 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1972 weston_wm_window_schedule_repaint(window);
1973
Tiago Vignattic1903232012-07-16 12:15:37 -04001974 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001975}
1976
Giulio Camuffob18f7882015-03-29 14:20:23 +03001977static void
1978weston_wm_handle_focus_in(struct weston_wm *wm, xcb_generic_event_t *event)
1979{
1980 xcb_focus_in_event_t *focus = (xcb_focus_in_event_t *) event;
1981 /* Do not let X clients change the focus behind the compositor's
1982 * back. Reset the focus to the old one if it changed. */
1983 if (!wm->focus_window || focus->event != wm->focus_window->id)
1984 weston_wm_send_focus_window(wm, wm->focus_window);
1985}
1986
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001987static int
1988weston_wm_handle_event(int fd, uint32_t mask, void *data)
1989{
1990 struct weston_wm *wm = data;
1991 xcb_generic_event_t *event;
1992 int count = 0;
1993
1994 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1995 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001996 free(event);
1997 count++;
1998 continue;
1999 }
2000
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002001 if (weston_wm_handle_dnd_event(wm, event)) {
2002 free(event);
2003 count++;
2004 continue;
2005 }
2006
MoD31700122013-06-11 19:58:55 -05002007 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002008 case XCB_BUTTON_PRESS:
2009 case XCB_BUTTON_RELEASE:
2010 weston_wm_handle_button(wm, event);
2011 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002012 case XCB_ENTER_NOTIFY:
2013 weston_wm_handle_enter(wm, event);
2014 break;
2015 case XCB_LEAVE_NOTIFY:
2016 weston_wm_handle_leave(wm, event);
2017 break;
2018 case XCB_MOTION_NOTIFY:
2019 weston_wm_handle_motion(wm, event);
2020 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002021 case XCB_CREATE_NOTIFY:
2022 weston_wm_handle_create_notify(wm, event);
2023 break;
2024 case XCB_MAP_REQUEST:
2025 weston_wm_handle_map_request(wm, event);
2026 break;
2027 case XCB_MAP_NOTIFY:
2028 weston_wm_handle_map_notify(wm, event);
2029 break;
2030 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04002031 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002032 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04002033 case XCB_REPARENT_NOTIFY:
2034 weston_wm_handle_reparent_notify(wm, event);
2035 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002036 case XCB_CONFIGURE_REQUEST:
2037 weston_wm_handle_configure_request(wm, event);
2038 break;
2039 case XCB_CONFIGURE_NOTIFY:
2040 weston_wm_handle_configure_notify(wm, event);
2041 break;
2042 case XCB_DESTROY_NOTIFY:
2043 weston_wm_handle_destroy_notify(wm, event);
2044 break;
2045 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002046 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002047 break;
2048 case XCB_PROPERTY_NOTIFY:
2049 weston_wm_handle_property_notify(wm, event);
2050 break;
2051 case XCB_CLIENT_MESSAGE:
2052 weston_wm_handle_client_message(wm, event);
2053 break;
Giulio Camuffob18f7882015-03-29 14:20:23 +03002054 case XCB_FOCUS_IN:
2055 weston_wm_handle_focus_in(wm, event);
2056 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002057 }
2058
2059 free(event);
2060 count++;
2061 }
2062
Marek Chalupaa1f3f3c2015-08-12 09:55:12 +02002063 if (count != 0)
2064 xcb_flush(wm->conn);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002065
2066 return count;
2067}
2068
2069static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002070weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window) {
2071 xcb_change_property(wm->conn, XCB_PROP_MODE_REPLACE,
2072 wm->screen->root, wm->atom.net_active_window,
2073 wm->atom.window, 32, 1, &window);
2074}
2075
2076static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002077weston_wm_get_visual_and_colormap(struct weston_wm *wm)
2078{
2079 xcb_depth_iterator_t d_iter;
2080 xcb_visualtype_iterator_t vt_iter;
2081 xcb_visualtype_t *visualtype;
2082
2083 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
2084 visualtype = NULL;
2085 while (d_iter.rem > 0) {
2086 if (d_iter.data->depth == 32) {
2087 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
2088 visualtype = vt_iter.data;
2089 break;
2090 }
2091
2092 xcb_depth_next(&d_iter);
2093 }
2094
2095 if (visualtype == NULL) {
2096 weston_log("no 32 bit visualtype\n");
2097 return;
2098 }
2099
2100 wm->visual_id = visualtype->visual_id;
2101 wm->colormap = xcb_generate_id(wm->conn);
2102 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
2103 wm->colormap, wm->screen->root, wm->visual_id);
2104}
2105
2106static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002107weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002108{
2109
2110#define F(field) offsetof(struct weston_wm, field)
2111
2112 static const struct { const char *name; int offset; } atoms[] = {
2113 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002114 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002115 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
2116 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04002117 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002118 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002119 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002120 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002121 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002122 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002123 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2124 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002125 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2126 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002127 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2128 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2129 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02002130 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002131 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2132
2133 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2134 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2135 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2136 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2137 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2138 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2139 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002140 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2141 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002142 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2143 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2144 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2145 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2146 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2147
2148 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2149 { "_NET_SUPPORTING_WM_CHECK",
2150 F(atom.net_supporting_wm_check) },
2151 { "_NET_SUPPORTED", F(atom.net_supported) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002152 { "_NET_ACTIVE_WINDOW", F(atom.net_active_window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002153 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2154 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002155 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002156 { "TARGETS", F(atom.targets) },
2157 { "UTF8_STRING", F(atom.utf8_string) },
2158 { "_WL_SELECTION", F(atom.wl_selection) },
2159 { "INCR", F(atom.incr) },
2160 { "TIMESTAMP", F(atom.timestamp) },
2161 { "MULTIPLE", F(atom.multiple) },
2162 { "UTF8_STRING" , F(atom.utf8_string) },
2163 { "COMPOUND_TEXT", F(atom.compound_text) },
2164 { "TEXT", F(atom.text) },
2165 { "STRING", F(atom.string) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002166 { "WINDOW", F(atom.window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002167 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2168 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002169 { "XdndSelection", F(atom.xdnd_selection) },
2170 { "XdndAware", F(atom.xdnd_aware) },
2171 { "XdndEnter", F(atom.xdnd_enter) },
2172 { "XdndLeave", F(atom.xdnd_leave) },
2173 { "XdndDrop", F(atom.xdnd_drop) },
2174 { "XdndStatus", F(atom.xdnd_status) },
2175 { "XdndFinished", F(atom.xdnd_finished) },
2176 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002177 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2178 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002179 };
2180#undef F
2181
2182 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2183 xcb_xfixes_query_version_reply_t *xfixes_reply;
2184 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2185 xcb_intern_atom_reply_t *reply;
2186 xcb_render_query_pict_formats_reply_t *formats_reply;
2187 xcb_render_query_pict_formats_cookie_t formats_cookie;
2188 xcb_render_pictforminfo_t *formats;
2189 uint32_t i;
2190
2191 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002192 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002193
2194 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2195
2196 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2197 cookies[i] = xcb_intern_atom (wm->conn, 0,
2198 strlen(atoms[i].name),
2199 atoms[i].name);
2200
2201 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2202 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2203 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2204 free(reply);
2205 }
2206
2207 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2208 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002209 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002210
2211 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2212 XCB_XFIXES_MAJOR_VERSION,
2213 XCB_XFIXES_MINOR_VERSION);
2214 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2215 xfixes_cookie, NULL);
2216
Martin Minarik6d118362012-06-07 18:01:59 +02002217 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002218 xfixes_reply->major_version, xfixes_reply->minor_version);
2219
2220 free(xfixes_reply);
2221
2222 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2223 formats_cookie, 0);
2224 if (formats_reply == NULL)
2225 return;
2226
2227 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002228 for (i = 0; i < formats_reply->num_formats; i++) {
2229 if (formats[i].direct.red_mask != 0xff &&
2230 formats[i].direct.red_shift != 16)
2231 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002232 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2233 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002234 wm->format_rgb = formats[i];
2235 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2236 formats[i].depth == 32 &&
2237 formats[i].direct.alpha_mask == 0xff &&
2238 formats[i].direct.alpha_shift == 24)
2239 wm->format_rgba = formats[i];
2240 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002241
2242 free(formats_reply);
2243}
2244
2245static void
2246weston_wm_create_wm_window(struct weston_wm *wm)
2247{
2248 static const char name[] = "Weston WM";
2249
2250 wm->wm_window = xcb_generate_id(wm->conn);
2251 xcb_create_window(wm->conn,
2252 XCB_COPY_FROM_PARENT,
2253 wm->wm_window,
2254 wm->screen->root,
2255 0, 0,
2256 10, 10,
2257 0,
2258 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2259 wm->screen->root_visual,
2260 0, NULL);
2261
2262 xcb_change_property(wm->conn,
2263 XCB_PROP_MODE_REPLACE,
2264 wm->wm_window,
2265 wm->atom.net_supporting_wm_check,
2266 XCB_ATOM_WINDOW,
2267 32, /* format */
2268 1, &wm->wm_window);
2269
2270 xcb_change_property(wm->conn,
2271 XCB_PROP_MODE_REPLACE,
2272 wm->wm_window,
2273 wm->atom.net_wm_name,
2274 wm->atom.utf8_string,
2275 8, /* format */
2276 strlen(name), name);
2277
2278 xcb_change_property(wm->conn,
2279 XCB_PROP_MODE_REPLACE,
2280 wm->screen->root,
2281 wm->atom.net_supporting_wm_check,
2282 XCB_ATOM_WINDOW,
2283 32, /* format */
2284 1, &wm->wm_window);
2285
Abdur Rehmanb8b150b2017-01-01 19:46:46 +05002286 /* Claim the WM_S0 selection even though we don't support
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002287 * the --replace functionality. */
2288 xcb_set_selection_owner(wm->conn,
2289 wm->wm_window,
2290 wm->atom.wm_s0,
2291 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002292
2293 xcb_set_selection_owner(wm->conn,
2294 wm->wm_window,
2295 wm->atom.net_wm_cm_s0,
2296 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002297}
2298
2299struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002300weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002301{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002302 struct weston_wm *wm;
2303 struct wl_event_loop *loop;
2304 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002305 uint32_t values[1];
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002306 xcb_atom_t supported[6];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002307
Peter Huttererf3d62272013-08-08 11:57:05 +10002308 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002309 if (wm == NULL)
2310 return NULL;
2311
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002312 wm->server = wxs;
2313 wm->window_hash = hash_table_create();
2314 if (wm->window_hash == NULL) {
2315 free(wm);
2316 return NULL;
2317 }
2318
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002319 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002320 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002321 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002322 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002323 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002324 hash_table_destroy(wm->window_hash);
2325 free(wm);
2326 return NULL;
2327 }
2328
2329 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2330 wm->screen = s.data;
2331
2332 loop = wl_display_get_event_loop(wxs->wl_display);
2333 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002334 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002335 WL_EVENT_READABLE,
2336 weston_wm_handle_event, wm);
2337 wl_event_source_check(wm->source);
2338
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002339 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002340 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002341
2342 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002343 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2344 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2345 XCB_EVENT_MASK_PROPERTY_CHANGE;
2346 xcb_change_window_attributes(wm->conn, wm->screen->root,
2347 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002348
2349 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2350 XCB_COMPOSITE_REDIRECT_MANUAL);
2351
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002352 wm->theme = theme_create();
2353
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002354 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002355 supported[1] = wm->atom.net_wm_state;
2356 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002357 supported[3] = wm->atom.net_wm_state_maximized_vert;
2358 supported[4] = wm->atom.net_wm_state_maximized_horz;
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002359 supported[5] = wm->atom.net_active_window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002360 xcb_change_property(wm->conn,
2361 XCB_PROP_MODE_REPLACE,
2362 wm->screen->root,
2363 wm->atom.net_supported,
2364 XCB_ATOM_ATOM,
2365 32, /* format */
2366 ARRAY_LENGTH(supported), supported);
2367
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002368 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
2369
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002370 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002371
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002372 weston_wm_dnd_init(wm);
2373
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002374 xcb_flush(wm->conn);
2375
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002376 wm->create_surface_listener.notify = weston_wm_create_surface;
2377 wl_signal_add(&wxs->compositor->create_surface_signal,
2378 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002379 wm->activate_listener.notify = weston_wm_window_activate;
2380 wl_signal_add(&wxs->compositor->activate_signal,
2381 &wm->activate_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002382 wm->kill_listener.notify = weston_wm_kill_client;
2383 wl_signal_add(&wxs->compositor->kill_signal,
2384 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002385 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002386
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002387 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002388 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002389
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002390 /* Create wm window and take WM_S0 selection last, which
2391 * signals to Xwayland that we're done with setup. */
2392 weston_wm_create_wm_window(wm);
2393
2394 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002395
2396 return wm;
2397}
2398
2399void
2400weston_wm_destroy(struct weston_wm *wm)
2401{
2402 /* FIXME: Free windows in hash. */
2403 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002404 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002405 xcb_disconnect(wm->conn);
2406 wl_event_source_remove(wm->source);
2407 wl_list_remove(&wm->selection_listener.link);
2408 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002409 wl_list_remove(&wm->kill_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002410 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002411
2412 free(wm);
2413}
2414
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002415static struct weston_wm_window *
2416get_wm_window(struct weston_surface *surface)
2417{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002418 struct wl_listener *listener;
2419
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002420 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002421 if (listener)
2422 return container_of(listener, struct weston_wm_window,
2423 surface_destroy_listener);
2424
2425 return NULL;
2426}
2427
Quentin Glidic955cec02016-08-12 10:41:35 +02002428static bool
2429is_wm_window(struct weston_surface *surface)
2430{
2431 return get_wm_window(surface) != NULL;
2432}
2433
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002434static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002435weston_wm_window_configure(void *data)
2436{
2437 struct weston_wm_window *window = data;
2438 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002439 uint32_t values[4];
2440 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002441
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002442 weston_wm_window_get_child_position(window, &x, &y);
2443 values[0] = x;
2444 values[1] = y;
2445 values[2] = window->width;
2446 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002447 xcb_configure_window(wm->conn,
2448 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002449 XCB_CONFIG_WINDOW_X |
2450 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002451 XCB_CONFIG_WINDOW_WIDTH |
2452 XCB_CONFIG_WINDOW_HEIGHT,
2453 values);
2454
2455 weston_wm_window_get_frame_size(window, &width, &height);
2456 values[0] = width;
2457 values[1] = height;
2458 xcb_configure_window(wm->conn,
2459 window->frame_id,
2460 XCB_CONFIG_WINDOW_WIDTH |
2461 XCB_CONFIG_WINDOW_HEIGHT,
2462 values);
2463
2464 window->configure_source = NULL;
2465
2466 weston_wm_window_schedule_repaint(window);
2467}
2468
2469static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002470send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002471{
2472 struct weston_wm_window *window = get_wm_window(surface);
2473 struct weston_wm *wm = window->wm;
2474 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002475 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002476
Marek Chalupae9fe4672014-10-29 13:44:44 +01002477 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002478 hborder = 2 * t->width;
2479 vborder = t->titlebar_height + t->width;
2480 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002481 hborder = 0;
2482 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002483 }
2484
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002485 if (width > hborder)
2486 window->width = width - hborder;
2487 else
2488 window->width = 1;
2489
2490 if (height > vborder)
2491 window->height = height - vborder;
2492 else
2493 window->height = 1;
2494
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002495 if (window->frame)
2496 frame_resize_inside(window->frame, window->width, window->height);
2497
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002498 if (window->configure_source)
2499 return;
2500
2501 window->configure_source =
2502 wl_event_loop_add_idle(wm->server->loop,
2503 weston_wm_window_configure, window);
2504}
2505
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002506static void
2507send_position(struct weston_surface *surface, int32_t x, int32_t y)
2508{
2509 struct weston_wm_window *window = get_wm_window(surface);
2510 struct weston_wm *wm;
2511 uint32_t mask, values[2];
2512
2513 if (!window || !window->wm)
2514 return;
2515
2516 wm = window->wm;
2517 /* We use pos_dirty to tell whether a configure message is in flight.
2518 * This is needed in case we send two configure events in a very
2519 * short time, since window->x/y is set in after a roundtrip, hence
2520 * we cannot just check if the current x and y are different. */
2521 if (window->x != x || window->y != y || window->pos_dirty) {
2522 window->pos_dirty = true;
2523 values[0] = x;
2524 values[1] = y;
2525 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
2526
2527 xcb_configure_window(wm->conn, window->frame_id, mask, values);
2528 xcb_flush(wm->conn);
2529 }
2530}
2531
Quentin Glidic955cec02016-08-12 10:41:35 +02002532static const struct weston_xwayland_client_interface shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002533 send_configure,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002534};
2535
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002536static int
2537legacy_fullscreen(struct weston_wm *wm,
2538 struct weston_wm_window *window,
2539 struct weston_output **output_ret)
2540{
2541 struct weston_compositor *compositor = wm->server->compositor;
2542 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002543 uint32_t minmax = PMinSize | PMaxSize;
2544 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002545
2546 /* Heuristics for detecting legacy fullscreen windows... */
2547
2548 wl_list_for_each(output, &compositor->output_list, link) {
2549 if (output->x == window->x &&
2550 output->y == window->y &&
2551 output->width == window->width &&
2552 output->height == window->height &&
2553 window->override_redirect) {
2554 *output_ret = output;
2555 return 1;
2556 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002557
2558 matching_size = 0;
2559 if ((window->size_hints.flags & (USSize |PSize)) &&
2560 window->size_hints.width == output->width &&
2561 window->size_hints.height == output->height)
2562 matching_size = 1;
2563 if ((window->size_hints.flags & minmax) == minmax &&
2564 window->size_hints.min_width == output->width &&
2565 window->size_hints.min_height == output->height &&
2566 window->size_hints.max_width == output->width &&
2567 window->size_hints.max_height == output->height)
2568 matching_size = 1;
2569
2570 if (matching_size && !window->decorate &&
2571 (window->size_hints.flags & (USPosition | PPosition)) &&
2572 window->size_hints.x == output->x &&
2573 window->size_hints.y == output->y) {
2574 *output_ret = output;
2575 return 1;
2576 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002577 }
2578
2579 return 0;
2580}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002581
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002582static bool
2583weston_wm_window_type_inactive(struct weston_wm_window *window)
2584{
2585 struct weston_wm *wm = window->wm;
2586
2587 return window->type == wm->atom.net_wm_window_type_tooltip ||
2588 window->type == wm->atom.net_wm_window_type_dropdown ||
2589 window->type == wm->atom.net_wm_window_type_dnd ||
2590 window->type == wm->atom.net_wm_window_type_combo ||
Giulio Camuffo84787ea2015-04-29 19:00:48 +03002591 window->type == wm->atom.net_wm_window_type_popup ||
2592 window->type == wm->atom.net_wm_window_type_utility;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002593}
2594
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002595static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002596xserver_map_shell_surface(struct weston_wm_window *window,
2597 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002598{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002599 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +02002600 struct weston_desktop_xwayland *xwayland =
2601 wm->server->compositor->xwayland;
2602 const struct weston_desktop_xwayland_interface *xwayland_interface =
2603 wm->server->compositor->xwayland_interface;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002604 struct weston_wm_window *parent;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002605
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002606 weston_wm_window_read_properties(window);
2607
2608 /* A weston_wm_window may have many different surfaces assigned
2609 * throughout its life, so we must make sure to remove the listener
2610 * from the old surface signal list. */
2611 if (window->surface)
2612 wl_list_remove(&window->surface_destroy_listener.link);
2613
2614 window->surface = surface;
2615 window->surface_destroy_listener.notify = surface_destroy;
2616 wl_signal_add(&window->surface->destroy_signal,
2617 &window->surface_destroy_listener);
2618
2619 weston_wm_window_schedule_repaint(window);
2620
Quentin Glidic955cec02016-08-12 10:41:35 +02002621 if (!xwayland_interface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002622 return;
2623
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002624 if (window->surface->committed) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03002625 weston_log("warning, unexpected in %s: "
2626 "surface's configure hook is already set.\n",
2627 __func__);
2628 return;
2629 }
2630
Murray Calavera883ac022015-06-06 13:02:22 +00002631 window->shsurf =
Quentin Glidic955cec02016-08-12 10:41:35 +02002632 xwayland_interface->create_surface(xwayland,
2633 window->surface,
2634 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002635
Pekka Paalanena04eacc2016-11-28 16:42:25 +02002636 wm_log("XWM: map shell surface, win %d, xwayland surface %p\n",
2637 window->id, window->shsurf);
2638
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002639 if (window->name)
Quentin Glidic955cec02016-08-12 10:41:35 +02002640 xwayland_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002641 if (window->pid > 0)
Quentin Glidic955cec02016-08-12 10:41:35 +02002642 xwayland_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002643
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002644 if (window->fullscreen) {
2645 window->saved_width = window->width;
2646 window->saved_height = window->height;
Pekka Paalanen505237e2016-12-01 15:41:11 +02002647 xwayland_interface->set_fullscreen(window->shsurf,
2648 window->legacy_fullscreen_output.output);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002649 return;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002650 } else if (window->override_redirect) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002651 xwayland_interface->set_xwayland(window->shsurf,
2652 window->x, window->y);
Axel Davye4450f92014-01-12 15:06:05 +01002653 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002654 parent = window->transient_for;
Quentin Glidic955cec02016-08-12 10:41:35 +02002655 if (weston_wm_window_type_inactive(window)) {
2656 xwayland_interface->set_transient(window->shsurf,
2657 parent->surface,
2658 window->x - parent->x,
2659 window->y - parent->y);
2660 } else {
2661 xwayland_interface->set_toplevel(window->shsurf);
2662 xwayland_interface->set_parent(window->shsurf,
2663 parent->surface);
2664 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002665 } else if (weston_wm_window_is_maximized(window)) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002666 xwayland_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002667 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002668 if (weston_wm_window_type_inactive(window)) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002669 xwayland_interface->set_xwayland(window->shsurf,
2670 window->x,
2671 window->y);
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002672 } else {
Quentin Glidic955cec02016-08-12 10:41:35 +02002673 xwayland_interface->set_toplevel(window->shsurf);
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002674 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002675 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002676}
Quentin Glidic955cec02016-08-12 10:41:35 +02002677
2678const struct weston_xwayland_surface_api surface_api = {
2679 is_wm_window,
2680 send_position,
2681};