blob: b33bb03c6e2224cc744ea15879f065ac6d868624 [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
442/* We reuse some predefined, but otherwise useles atoms */
443#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
444#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500445#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700446#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400447
448static void
449weston_wm_window_read_properties(struct weston_wm_window *window)
450{
451 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +0200452 const struct weston_desktop_xwayland_interface *xwayland_interface =
453 wm->server->compositor->xwayland_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400454
455#define F(field) offsetof(struct weston_wm_window, field)
456 const struct {
457 xcb_atom_t atom;
458 xcb_atom_t type;
459 int offset;
460 } props[] = {
461 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
462 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
463 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
464 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700465 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500466 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400467 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
468 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300469 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400470 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300471 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400472 };
473#undef F
474
475 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
476 xcb_get_property_reply_t *reply;
477 void *p;
478 uint32_t *xid;
479 xcb_atom_t *atom;
480 uint32_t i;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200481 char name[1024];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400482
483 if (!window->properties_dirty)
484 return;
485 window->properties_dirty = 0;
486
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400487 for (i = 0; i < ARRAY_LENGTH(props); i++)
488 cookie[i] = xcb_get_property(wm->conn,
489 0, /* delete */
490 window->id,
491 props[i].atom,
492 XCB_ATOM_ANY, 0, 2048);
493
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700494 window->decorate = window->override_redirect ? 0 : MWM_DECOR_EVERYTHING;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700495 window->size_hints.flags = 0;
496 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700497 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700498
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400499 for (i = 0; i < ARRAY_LENGTH(props); i++) {
500 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
501 if (!reply)
502 /* Bad window, typically */
503 continue;
504 if (reply->type == XCB_ATOM_NONE) {
505 /* No such property */
506 free(reply);
507 continue;
508 }
509
510 p = ((char *) window + props[i].offset);
511
512 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300513 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400514 case XCB_ATOM_STRING:
515 /* FIXME: We're using this for both string and
516 utf8_string */
517 if (*(char **) p)
518 free(*(char **) p);
519
520 *(char **) p =
521 strndup(xcb_get_property_value(reply),
522 xcb_get_property_value_length(reply));
523 break;
524 case XCB_ATOM_WINDOW:
525 xid = xcb_get_property_value(reply);
Derek Foreman49372142015-04-09 10:51:22 -0500526 if (!wm_lookup_window(wm, *xid, p))
527 weston_log("XCB_ATOM_WINDOW contains window"
528 " id not found in hash table.\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400529 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300530 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400531 case XCB_ATOM_ATOM:
532 atom = xcb_get_property_value(reply);
533 *(xcb_atom_t *) p = *atom;
534 break;
535 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700536 atom = xcb_get_property_value(reply);
537 for (i = 0; i < reply->value_len; i++)
Derek Foremanb4deec62015-04-07 12:12:13 -0500538 if (atom[i] == wm->atom.wm_delete_window) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700539 window->delete_window = 1;
Derek Foremanb4deec62015-04-07 12:12:13 -0500540 break;
541 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400542 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700543 case TYPE_WM_NORMAL_HINTS:
544 memcpy(&window->size_hints,
545 xcb_get_property_value(reply),
546 sizeof window->size_hints);
547 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500548 case TYPE_NET_WM_STATE:
549 window->fullscreen = 0;
550 atom = xcb_get_property_value(reply);
Ryo Munakataf3744f52015-03-11 17:36:30 +0900551 for (i = 0; i < reply->value_len; i++) {
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500552 if (atom[i] == wm->atom.net_wm_state_fullscreen)
553 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200554 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
555 window->maximized_vert = 1;
556 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
557 window->maximized_horz = 1;
Ryo Munakataf3744f52015-03-11 17:36:30 +0900558 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500559 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400560 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700561 memcpy(&window->motif_hints,
562 xcb_get_property_value(reply),
563 sizeof window->motif_hints);
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700564 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) {
565 if (window->motif_hints.decorations & MWM_DECOR_ALL)
566 /* MWM_DECOR_ALL means all except the other values listed. */
567 window->decorate =
568 MWM_DECOR_EVERYTHING & (~window->motif_hints.decorations);
569 else
570 window->decorate =
571 window->motif_hints.decorations;
572 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400573 break;
574 default:
575 break;
576 }
577 free(reply);
578 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200579
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200580 if (window->pid > 0) {
581 gethostname(name, sizeof(name));
582 for (i = 0; i < sizeof(name); i++) {
583 if (name[i] == '\0')
584 break;
585 }
586 if (i == sizeof(name))
587 name[0] = '\0'; /* ignore stupid hostnames */
588
589 /* this is only one heuristic to guess the PID of a client is
590 * valid, assuming it's compliant with icccm and ewmh.
591 * Non-compliants and remote applications of course fail. */
592 if (!window->machine || strcmp(window->machine, name))
593 window->pid = 0;
594 }
595
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200596 if (window->shsurf && window->name)
Quentin Glidic955cec02016-08-12 10:41:35 +0200597 xwayland_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500598 if (window->frame && window->name)
599 frame_set_title(window->frame, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200600 if (window->shsurf && window->pid > 0)
Quentin Glidic955cec02016-08-12 10:41:35 +0200601 xwayland_interface->set_pid(window->shsurf, window->pid);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400602}
603
604static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400605weston_wm_window_get_frame_size(struct weston_wm_window *window,
606 int *width, int *height)
607{
608 struct theme *t = window->wm->theme;
609
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500610 if (window->fullscreen) {
611 *width = window->width;
612 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800613 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500614 *width = frame_width(window->frame);
615 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400616 } else {
617 *width = window->width + t->margin * 2;
618 *height = window->height + t->margin * 2;
619 }
620}
621
622static void
623weston_wm_window_get_child_position(struct weston_wm_window *window,
624 int *x, int *y)
625{
626 struct theme *t = window->wm->theme;
627
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500628 if (window->fullscreen) {
629 *x = 0;
630 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800631 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500632 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400633 } else {
634 *x = t->margin;
635 *y = t->margin;
636 }
637}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400638
639static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500640weston_wm_window_send_configure_notify(struct weston_wm_window *window)
641{
642 xcb_configure_notify_event_t configure_notify;
643 struct weston_wm *wm = window->wm;
644 int x, y;
645
646 weston_wm_window_get_child_position(window, &x, &y);
647 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
648 configure_notify.pad0 = 0;
649 configure_notify.event = window->id;
650 configure_notify.window = window->id;
651 configure_notify.above_sibling = XCB_WINDOW_NONE;
652 configure_notify.x = x;
653 configure_notify.y = y;
654 configure_notify.width = window->width;
655 configure_notify.height = window->height;
656 configure_notify.border_width = 0;
657 configure_notify.override_redirect = 0;
658 configure_notify.pad1 = 0;
659
Murray Calavera883ac022015-06-06 13:02:22 +0000660 xcb_send_event(wm->conn, 0, window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500661 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
662 (char *) &configure_notify);
663}
664
665static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400666weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
667{
Murray Calavera883ac022015-06-06 13:02:22 +0000668 xcb_configure_request_event_t *configure_request =
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400669 (xcb_configure_request_event_t *) event;
670 struct weston_wm_window *window;
671 uint32_t mask, values[16];
672 int x, y, width, height, i = 0;
673
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400674 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
675 configure_request->window,
676 configure_request->x, configure_request->y,
677 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400678
Derek Foreman49372142015-04-09 10:51:22 -0500679 if (!wm_lookup_window(wm, configure_request->window, &window))
680 return;
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400681
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500682 if (window->fullscreen) {
683 weston_wm_window_send_configure_notify(window);
684 return;
685 }
686
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400687 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
688 window->width = configure_request->width;
689 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
690 window->height = configure_request->height;
691
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500692 if (window->frame)
693 frame_resize_inside(window->frame, window->width, window->height);
694
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400695 weston_wm_window_get_child_position(window, &x, &y);
696 values[i++] = x;
697 values[i++] = y;
698 values[i++] = window->width;
699 values[i++] = window->height;
700 values[i++] = 0;
701 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
702 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
703 XCB_CONFIG_WINDOW_BORDER_WIDTH;
704 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
705 values[i++] = configure_request->sibling;
706 mask |= XCB_CONFIG_WINDOW_SIBLING;
707 }
708 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
709 values[i++] = configure_request->stack_mode;
710 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
711 }
712
713 xcb_configure_window(wm->conn, window->id, mask, values);
714
715 weston_wm_window_get_frame_size(window, &width, &height);
716 values[0] = width;
717 values[1] = height;
718 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
719 xcb_configure_window(wm->conn, window->frame_id, mask, values);
720
721 weston_wm_window_schedule_repaint(window);
722}
723
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400724static int
725our_resource(struct weston_wm *wm, uint32_t id)
726{
727 const xcb_setup_t *setup;
728
729 setup = xcb_get_setup(wm->conn);
730
731 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
732}
733
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400734static void
735weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
736{
Murray Calavera883ac022015-06-06 13:02:22 +0000737 xcb_configure_notify_event_t *configure_notify =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400738 (xcb_configure_notify_event_t *) event;
739 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400740
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400741 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400742 configure_notify->window,
743 configure_notify->x, configure_notify->y,
744 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300745
Derek Foreman49372142015-04-09 10:51:22 -0500746 if (!wm_lookup_window(wm, configure_notify->window, &window))
747 return;
748
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700749 window->x = configure_notify->x;
750 window->y = configure_notify->y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200751 window->pos_dirty = false;
752
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700753 if (window->override_redirect) {
754 window->width = configure_notify->width;
755 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500756 if (window->frame)
757 frame_resize_inside(window->frame,
758 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700759 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400760}
761
762static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300763weston_wm_kill_client(struct wl_listener *listener, void *data)
764{
765 struct weston_surface *surface = data;
766 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300767 if (!window)
768 return;
769
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200770 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300771 kill(window->pid, SIGKILL);
772}
773
774static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700775weston_wm_create_surface(struct wl_listener *listener, void *data)
776{
777 struct weston_surface *surface = data;
778 struct weston_wm *wm =
779 container_of(listener,
780 struct weston_wm, create_surface_listener);
781 struct weston_wm_window *window;
782
783 if (wl_resource_get_client(surface->resource) != wm->server->client)
784 return;
785
786 wl_list_for_each(window, &wm->unpaired_window_list, link)
787 if (window->surface_id ==
788 wl_resource_get_id(surface->resource)) {
789 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700790 window->surface_id = 0;
791 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700792 break;
Murray Calavera883ac022015-06-06 13:02:22 +0000793 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700794}
795
796static void
Giulio Camuffob18f7882015-03-29 14:20:23 +0300797weston_wm_send_focus_window(struct weston_wm *wm,
798 struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400799{
Scott Moreau85ecac02012-05-21 15:49:14 -0600800 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400801
Scott Moreau85ecac02012-05-21 15:49:14 -0600802 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700803 uint32_t values[1];
804
Boyan Dingb9f863c2014-08-30 10:33:23 +0800805 if (window->override_redirect)
806 return;
807
Scott Moreau85ecac02012-05-21 15:49:14 -0600808 client_message.response_type = XCB_CLIENT_MESSAGE;
809 client_message.format = 32;
810 client_message.window = window->id;
811 client_message.type = wm->atom.wm_protocols;
812 client_message.data.data32[0] = wm->atom.wm_take_focus;
813 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
814
Murray Calavera883ac022015-06-06 13:02:22 +0000815 xcb_send_event(wm->conn, 0, window->id,
Scott Moreau85ecac02012-05-21 15:49:14 -0600816 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
817 (char *) &client_message);
818
819 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
820 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700821
822 values[0] = XCB_STACK_MODE_ABOVE;
823 xcb_configure_window (wm->conn, window->frame_id,
824 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600825 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400826 xcb_set_input_focus (wm->conn,
827 XCB_INPUT_FOCUS_POINTER_ROOT,
828 XCB_NONE,
829 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600830 }
Giulio Camuffob18f7882015-03-29 14:20:23 +0300831}
832
833static void
834weston_wm_window_activate(struct wl_listener *listener, void *data)
835{
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +0800836 struct weston_surface_activation_data *activation_data = data;
837 struct weston_surface *surface = activation_data->surface;
Giulio Camuffob18f7882015-03-29 14:20:23 +0300838 struct weston_wm_window *window = NULL;
839 struct weston_wm *wm =
840 container_of(listener, struct weston_wm, activate_listener);
841
842 if (surface) {
843 window = get_wm_window(surface);
844 }
845
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200846 if (window) {
847 weston_wm_set_net_active_window(wm, window->id);
848 } else {
849 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
850 }
851
Giulio Camuffob18f7882015-03-29 14:20:23 +0300852 weston_wm_send_focus_window(wm, window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400853
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500854 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800855 if (wm->focus_window->frame)
856 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400857 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500858 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400859 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500860 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800861 if (wm->focus_window->frame)
862 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400863 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500864 }
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200865
866 xcb_flush(wm->conn);
867
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400868}
869
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400870#define ICCCM_WITHDRAWN_STATE 0
871#define ICCCM_NORMAL_STATE 1
872#define ICCCM_ICONIC_STATE 3
873
874static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500875weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400876{
877 struct weston_wm *wm = window->wm;
878 uint32_t property[2];
879
880 property[0] = state;
881 property[1] = XCB_WINDOW_NONE;
882
883 xcb_change_property(wm->conn,
884 XCB_PROP_MODE_REPLACE,
885 window->id,
886 wm->atom.wm_state,
887 wm->atom.wm_state,
888 32, /* format */
889 2, property);
890}
891
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400892static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500893weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
894{
895 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200896 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500897 int i;
898
899 i = 0;
900 if (window->fullscreen)
901 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200902 if (window->maximized_vert)
903 property[i++] = wm->atom.net_wm_state_maximized_vert;
904 if (window->maximized_horz)
905 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500906
907 xcb_change_property(wm->conn,
908 XCB_PROP_MODE_REPLACE,
909 window->id,
910 wm->atom.net_wm_state,
911 XCB_ATOM_ATOM,
912 32, /* format */
913 i, property);
914}
915
916static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700917weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400918{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700919 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400920 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400921 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200922 int buttons = FRAME_BUTTON_CLOSE;
923
924 if (window->decorate & MWM_DECOR_MAXIMIZE)
925 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400926
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500927 window->frame = frame_create(window->wm->theme,
928 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200929 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500930 frame_resize_inside(window->frame, window->width, window->height);
931
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400932 weston_wm_window_get_frame_size(window, &width, &height);
933 weston_wm_window_get_child_position(window, &x, &y);
934
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400935 values[0] = wm->screen->black_pixel;
936 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400937 XCB_EVENT_MASK_KEY_PRESS |
938 XCB_EVENT_MASK_KEY_RELEASE |
939 XCB_EVENT_MASK_BUTTON_PRESS |
940 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400941 XCB_EVENT_MASK_POINTER_MOTION |
942 XCB_EVENT_MASK_ENTER_WINDOW |
943 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400944 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400945 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400946 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400947
948 window->frame_id = xcb_generate_id(wm->conn);
949 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400950 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400951 window->frame_id,
952 wm->screen->root,
953 0, 0,
954 width, height,
955 0,
956 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400957 wm->visual_id,
958 XCB_CW_BORDER_PIXEL |
959 XCB_CW_EVENT_MASK |
960 XCB_CW_COLORMAP, values);
961
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400962 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
963
964 values[0] = 0;
965 xcb_configure_window(wm->conn, window->id,
966 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
967
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400968 window->cairo_surface =
969 cairo_xcb_surface_create_with_xrender_format(wm->conn,
970 wm->screen,
971 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400972 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400973 width, height);
974
975 hash_table_insert(wm->window_hash, window->frame_id, window);
976}
977
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200978/*
979 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
980 * Passing a <0 desktop value deletes the property.
981 */
982static void
983weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
Bryce Harringtone00554b2015-06-12 10:17:39 -0700984 int desktop)
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200985{
986 if (desktop >= 0) {
987 xcb_change_property(window->wm->conn,
988 XCB_PROP_MODE_REPLACE,
989 window->id,
990 window->wm->atom.net_wm_desktop,
991 XCB_ATOM_CARDINAL,
992 32, /* format */
993 1, &desktop);
994 } else {
995 xcb_delete_property(window->wm->conn,
996 window->id,
997 window->wm->atom.net_wm_desktop);
998 }
999}
1000
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001001static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001002weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
1003{
1004 xcb_map_request_event_t *map_request =
1005 (xcb_map_request_event_t *) event;
1006 struct weston_wm_window *window;
Pekka Paalanen505237e2016-12-01 15:41:11 +02001007 struct weston_output *output;
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001008
1009 if (our_resource(wm, map_request->window)) {
1010 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
1011 map_request->window);
1012 return;
1013 }
1014
Derek Foreman49372142015-04-09 10:51:22 -05001015 if (!wm_lookup_window(wm, map_request->window, &window))
1016 return;
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001017
1018 weston_wm_window_read_properties(window);
1019
1020 if (window->frame_id == XCB_WINDOW_NONE)
1021 weston_wm_window_create_frame(window);
1022
Pekka Paalanen44660c32016-11-14 15:38:43 +02001023 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d, %dx%d @ %d,%d)\n",
1024 window->id, window, window->frame_id,
1025 window->width, window->height, window->x, window->y);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001026
1027 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
1028 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001029 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001030
Pekka Paalanen505237e2016-12-01 15:41:11 +02001031 if (legacy_fullscreen(wm, window, &output)) {
1032 window->fullscreen = 1;
1033 weston_output_weak_ref_set(&window->legacy_fullscreen_output,
1034 output);
1035 }
1036
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001037 xcb_map_window(wm->conn, map_request->window);
1038 xcb_map_window(wm->conn, window->frame_id);
1039}
1040
1041static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001042weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1043{
1044 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
1045
1046 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001047 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
1048 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001049 return;
1050 }
1051
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001052 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001053}
1054
1055static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001056weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1057{
1058 xcb_unmap_notify_event_t *unmap_notify =
1059 (xcb_unmap_notify_event_t *) event;
1060 struct weston_wm_window *window;
1061
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001062 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
1063 unmap_notify->window,
1064 unmap_notify->event,
1065 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001066
1067 if (our_resource(wm, unmap_notify->window))
1068 return;
1069
MoD31700122013-06-11 19:58:55 -05001070 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -04001071 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1072 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001073 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001074
Derek Foreman49372142015-04-09 10:51:22 -05001075 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1076 return;
1077
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001078 if (window->surface_id) {
1079 /* Make sure we're not on the unpaired surface list or we
1080 * could be assigned a surface during surface creation that
1081 * was mapped before this unmap request.
1082 */
1083 wl_list_remove(&window->link);
1084 window->surface_id = 0;
1085 }
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001086 if (wm->focus_window == window)
1087 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001088 if (window->surface)
1089 wl_list_remove(&window->surface_destroy_listener.link);
1090 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +02001091 window->shsurf = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001092
1093 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1094 weston_wm_window_set_virtual_desktop(window, -1);
1095
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001096 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001097}
1098
1099static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001100weston_wm_window_draw_decoration(void *data)
1101{
1102 struct weston_wm_window *window = data;
1103 struct weston_wm *wm = window->wm;
1104 struct theme *t = wm->theme;
1105 cairo_t *cr;
1106 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001107 int32_t input_x, input_y, input_w, input_h;
Quentin Glidic955cec02016-08-12 10:41:35 +02001108 const struct weston_desktop_xwayland_interface *xwayland_interface =
1109 wm->server->compositor->xwayland_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001110 uint32_t flags = 0;
Giulio Camuffoaa974782015-02-01 16:18:51 +02001111 struct weston_view *view;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001112
Pekka Paalanena04eacc2016-11-28 16:42:25 +02001113 wm_log("XWM: start draw decoration, win %d\n", window->id);
1114
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001115 weston_wm_window_read_properties(window);
1116
1117 window->repaint_source = NULL;
1118
1119 weston_wm_window_get_frame_size(window, &width, &height);
1120 weston_wm_window_get_child_position(window, &x, &y);
1121
1122 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1123 cr = cairo_create(window->cairo_surface);
1124
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001125 if (window->fullscreen) {
1126 /* nothing */
1127 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001128 if (wm->focus_window == window)
1129 flags |= THEME_FRAME_ACTIVE;
1130
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001131 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001132 } else {
1133 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1134 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1135 cairo_paint(cr);
1136
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001137 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001138 }
1139
1140 cairo_destroy(cr);
1141
1142 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001143 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001144 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001145 pixman_region32_init(&window->surface->pending.opaque);
1146 } else {
1147 /* We leave an extra pixel around the X window area to
1148 * make sure we don't sample from the undefined alpha
1149 * channel when filtering. */
Murray Calavera883ac022015-06-06 13:02:22 +00001150 pixman_region32_init_rect(&window->surface->pending.opaque,
MoD384a11a2013-06-22 11:04:21 -05001151 x - 1, y - 1,
1152 window->width + 2,
1153 window->height + 2);
1154 }
Giulio Camuffoaa974782015-02-01 16:18:51 +02001155 wl_list_for_each(view, &window->surface->views, surface_link)
1156 weston_view_geometry_dirty(view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001157
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001158 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001159
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001160 if (window->decorate && !window->fullscreen) {
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001161 frame_input_rect(window->frame, &input_x, &input_y,
1162 &input_w, &input_h);
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001163 } else {
1164 input_x = x;
1165 input_y = y;
1166 input_w = width;
1167 input_h = height;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001168 }
1169
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001170 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001171 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001172
Pekka Paalanena04eacc2016-11-28 16:42:25 +02001173 wm_log("XWM: draw decoration, win %d geometry: %d,%d %dx%d\n",
1174 window->id, input_x, input_y, input_w, input_h);
1175
Quentin Glidic955cec02016-08-12 10:41:35 +02001176 xwayland_interface->set_window_geometry(window->shsurf,
1177 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001178 }
1179}
1180
1181static void
1182weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1183{
1184 struct weston_wm *wm = window->wm;
Giulio Camuffoaa974782015-02-01 16:18:51 +02001185 struct weston_view *view;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001186 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001187
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001188 if (window->frame_id == XCB_WINDOW_NONE) {
1189 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001190 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001191 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001192 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001193 pixman_region32_init(&window->surface->pending.opaque);
1194 } else {
1195 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1196 width, height);
1197 }
Giulio Camuffoaa974782015-02-01 16:18:51 +02001198 wl_list_for_each(view, &window->surface->views, surface_link)
1199 weston_view_geometry_dirty(view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001200 }
1201 return;
1202 }
1203
1204 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001205 return;
1206
Pekka Paalanena04eacc2016-11-28 16:42:25 +02001207 wm_log("XWM: schedule repaint, win %d\n", window->id);
1208
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001209 window->repaint_source =
1210 wl_event_loop_add_idle(wm->server->loop,
1211 weston_wm_window_draw_decoration,
1212 window);
1213}
1214
1215static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001216weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1217{
1218 xcb_property_notify_event_t *property_notify =
1219 (xcb_property_notify_event_t *) event;
1220 struct weston_wm_window *window;
1221
Derek Foreman49372142015-04-09 10:51:22 -05001222 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001223 return;
1224
1225 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001226
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001227 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001228 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001229 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001230 else
1231 read_and_dump_property(wm, property_notify->window,
1232 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001233
1234 if (property_notify->atom == wm->atom.net_wm_name ||
1235 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001236 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001237}
1238
1239static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001240weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001241 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001242{
1243 struct weston_wm_window *window;
1244 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001245 xcb_get_geometry_cookie_t geometry_cookie;
1246 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001247
Peter Huttererf3d62272013-08-08 11:57:05 +10001248 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001249 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001250 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001251 return;
1252 }
1253
MoD384a11a2013-06-22 11:04:21 -05001254 geometry_cookie = xcb_get_geometry(wm->conn, id);
1255
Giulio Camuffob18f7882015-03-29 14:20:23 +03001256 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
1257 XCB_EVENT_MASK_FOCUS_CHANGE;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001258 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1259
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001260 window->wm = wm;
1261 window->id = id;
1262 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001263 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001264 window->width = width;
1265 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001266 window->x = x;
1267 window->y = y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001268 window->pos_dirty = false;
Pekka Paalanen505237e2016-12-01 15:41:11 +02001269 weston_output_weak_ref_init(&window->legacy_fullscreen_output);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001270
MoD384a11a2013-06-22 11:04:21 -05001271 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1272 /* technically we should use XRender and check the visual format's
1273 alpha_mask, but checking depth is simpler and works in all known cases */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001274 if (geometry_reply != NULL)
MoD384a11a2013-06-22 11:04:21 -05001275 window->has_alpha = geometry_reply->depth == 32;
1276 free(geometry_reply);
1277
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001278 hash_table_insert(wm->window_hash, id, window);
1279}
1280
1281static void
1282weston_wm_window_destroy(struct weston_wm_window *window)
1283{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001284 struct weston_wm *wm = window->wm;
1285
Pekka Paalanen505237e2016-12-01 15:41:11 +02001286 weston_output_weak_ref_clear(&window->legacy_fullscreen_output);
1287
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001288 if (window->repaint_source)
1289 wl_event_source_remove(window->repaint_source);
1290 if (window->cairo_surface)
1291 cairo_surface_destroy(window->cairo_surface);
1292
1293 if (window->frame_id) {
1294 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1295 xcb_destroy_window(wm->conn, window->frame_id);
1296 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001297 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001298 hash_table_remove(wm->window_hash, window->frame_id);
1299 window->frame_id = XCB_WINDOW_NONE;
1300 }
1301
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001302 if (window->surface_id)
1303 wl_list_remove(&window->link);
1304
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001305 if (window->surface)
1306 wl_list_remove(&window->surface_destroy_listener.link);
1307
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001308 hash_table_remove(window->wm->window_hash, window->id);
1309 free(window);
1310}
1311
1312static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001313weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1314{
1315 xcb_create_notify_event_t *create_notify =
1316 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001317
Pekka Paalanen7db6c432016-11-14 14:30:57 +02001318 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 -04001319 create_notify->window,
Pekka Paalanen7db6c432016-11-14 14:30:57 +02001320 create_notify->x, create_notify->y,
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001321 create_notify->width, create_notify->height,
1322 create_notify->override_redirect ? ", override" : "",
1323 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001324
1325 if (our_resource(wm, create_notify->window))
1326 return;
1327
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001328 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001329 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001330 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001331 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001332}
1333
1334static void
1335weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1336{
1337 xcb_destroy_notify_event_t *destroy_notify =
1338 (xcb_destroy_notify_event_t *) event;
1339 struct weston_wm_window *window;
1340
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001341 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1342 destroy_notify->window,
1343 destroy_notify->event,
1344 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001345
1346 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001347 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001348
Derek Foreman49372142015-04-09 10:51:22 -05001349 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1350 return;
1351
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001352 weston_wm_window_destroy(window);
1353}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001354
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001355static void
1356weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1357{
1358 xcb_reparent_notify_event_t *reparent_notify =
1359 (xcb_reparent_notify_event_t *) event;
1360 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001361
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001362 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1363 reparent_notify->window,
1364 reparent_notify->parent,
1365 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001366
1367 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001368 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001369 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001370 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001371 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001372 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1373 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001374 weston_wm_window_destroy(window);
1375 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001376}
1377
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001378struct weston_seat *
1379weston_wm_pick_seat(struct weston_wm *wm)
1380{
Tom Hochsteine7fff212016-11-01 14:14:00 -05001381 struct wl_list *seats = wm->server->compositor->seat_list.next;
1382 if (wl_list_empty(seats))
1383 return NULL;
1384 return container_of(seats, struct weston_seat, link);
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001385}
1386
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001387static struct weston_seat *
1388weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1389{
1390 struct weston_wm *wm = window->wm;
1391 struct weston_seat *seat, *s;
1392
1393 seat = NULL;
1394 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001395 struct weston_pointer *pointer = weston_seat_get_pointer(s);
1396 struct weston_pointer *old_pointer =
1397 weston_seat_get_pointer(seat);
1398
1399 if (pointer && pointer->focus &&
1400 pointer->focus->surface == window->surface &&
1401 pointer->button_count > 0 &&
1402 (!seat ||
1403 pointer->grab_serial -
1404 old_pointer->grab_serial < (1 << 30)))
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001405 seat = s;
1406 }
1407
1408 return seat;
1409}
1410
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001411static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001412weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1413 xcb_client_message_event_t *client_message)
1414{
1415 static const int map[] = {
1416 THEME_LOCATION_RESIZING_TOP_LEFT,
1417 THEME_LOCATION_RESIZING_TOP,
1418 THEME_LOCATION_RESIZING_TOP_RIGHT,
1419 THEME_LOCATION_RESIZING_RIGHT,
1420 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1421 THEME_LOCATION_RESIZING_BOTTOM,
1422 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1423 THEME_LOCATION_RESIZING_LEFT
1424 };
1425
1426 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001427 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Derek Foreman1281a362015-07-31 16:55:32 -05001428 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001429 int detail;
Quentin Glidic955cec02016-08-12 10:41:35 +02001430 const struct weston_desktop_xwayland_interface *xwayland_interface =
1431 wm->server->compositor->xwayland_interface;
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001432
Derek Foreman1281a362015-07-31 16:55:32 -05001433 if (!pointer || pointer->button_count != 1
1434 || !pointer->focus
1435 || pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001436 return;
1437
1438 detail = client_message->data.data32[2];
1439 switch (detail) {
1440 case _NET_WM_MOVERESIZE_MOVE:
Quentin Glidic955cec02016-08-12 10:41:35 +02001441 xwayland_interface->move(window->shsurf, pointer);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001442 break;
1443 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1444 case _NET_WM_MOVERESIZE_SIZE_TOP:
1445 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1446 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1447 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1448 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1449 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1450 case _NET_WM_MOVERESIZE_SIZE_LEFT:
Quentin Glidic955cec02016-08-12 10:41:35 +02001451 xwayland_interface->resize(window->shsurf, pointer, map[detail]);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001452 break;
1453 case _NET_WM_MOVERESIZE_CANCEL:
1454 break;
1455 }
1456}
1457
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001458#define _NET_WM_STATE_REMOVE 0
1459#define _NET_WM_STATE_ADD 1
1460#define _NET_WM_STATE_TOGGLE 2
1461
1462static int
1463update_state(int action, int *state)
1464{
1465 int new_state, changed;
1466
1467 switch (action) {
1468 case _NET_WM_STATE_REMOVE:
1469 new_state = 0;
1470 break;
1471 case _NET_WM_STATE_ADD:
1472 new_state = 1;
1473 break;
1474 case _NET_WM_STATE_TOGGLE:
1475 new_state = !*state;
1476 break;
1477 default:
1478 return 0;
1479 }
1480
1481 changed = (*state != new_state);
1482 *state = new_state;
1483
1484 return changed;
1485}
1486
1487static void
1488weston_wm_window_configure(void *data);
1489
1490static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001491weston_wm_window_set_toplevel(struct weston_wm_window *window)
1492{
Quentin Glidic955cec02016-08-12 10:41:35 +02001493 const struct weston_desktop_xwayland_interface *xwayland_interface =
1494 window->wm->server->compositor->xwayland_interface;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001495
Quentin Glidic955cec02016-08-12 10:41:35 +02001496 xwayland_interface->set_toplevel(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001497 window->width = window->saved_width;
1498 window->height = window->saved_height;
1499 if (window->frame)
1500 frame_resize_inside(window->frame,
1501 window->width,
1502 window->height);
1503 weston_wm_window_configure(window);
1504}
1505
1506static inline bool
1507weston_wm_window_is_maximized(struct weston_wm_window *window)
1508{
1509 return window->maximized_horz && window->maximized_vert;
1510}
1511
1512static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001513weston_wm_window_handle_state(struct weston_wm_window *window,
1514 xcb_client_message_event_t *client_message)
1515{
1516 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +02001517 const struct weston_desktop_xwayland_interface *xwayland_interface =
1518 wm->server->compositor->xwayland_interface;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001519 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001520 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001521
1522 action = client_message->data.data32[0];
1523 property = client_message->data.data32[1];
1524
1525 if (property == wm->atom.net_wm_state_fullscreen &&
1526 update_state(action, &window->fullscreen)) {
1527 weston_wm_window_set_net_wm_state(window);
1528 if (window->fullscreen) {
1529 window->saved_width = window->width;
1530 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001531
1532 if (window->shsurf)
Quentin Glidic955cec02016-08-12 10:41:35 +02001533 xwayland_interface->set_fullscreen(window->shsurf,
1534 NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001535 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001536 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001537 weston_wm_window_set_toplevel(window);
1538 }
1539 } else {
1540 if (property == wm->atom.net_wm_state_maximized_vert &&
1541 update_state(action, &window->maximized_vert))
1542 weston_wm_window_set_net_wm_state(window);
1543 if (property == wm->atom.net_wm_state_maximized_horz &&
1544 update_state(action, &window->maximized_horz))
1545 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001546
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001547 if (maximized != weston_wm_window_is_maximized(window)) {
1548 if (weston_wm_window_is_maximized(window)) {
1549 window->saved_width = window->width;
1550 window->saved_height = window->height;
1551
1552 if (window->shsurf)
Quentin Glidic955cec02016-08-12 10:41:35 +02001553 xwayland_interface->set_maximized(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001554 } else if (window->shsurf) {
1555 weston_wm_window_set_toplevel(window);
1556 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001557 }
1558 }
1559}
1560
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001561static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001562surface_destroy(struct wl_listener *listener, void *data)
1563{
1564 struct weston_wm_window *window =
1565 container_of(listener,
1566 struct weston_wm_window, surface_destroy_listener);
1567
1568 wm_log("surface for xid %d destroyed\n", window->id);
1569
1570 /* This should have been freed by the shell.
1571 * Don't try to use it later. */
1572 window->shsurf = NULL;
1573 window->surface = NULL;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001574}
1575
1576static void
1577weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1578 xcb_client_message_event_t *client_message)
1579{
1580 struct weston_wm *wm = window->wm;
1581 struct wl_resource *resource;
1582
1583 if (window->surface_id != 0) {
1584 wm_log("already have surface id for window %d\n", window->id);
1585 return;
1586 }
1587
1588 /* Xwayland will send the wayland requests to create the
1589 * wl_surface before sending this client message. Even so, we
1590 * can end up handling the X event before the wayland requests
1591 * and thus when we try to look up the surface ID, the surface
1592 * hasn't been created yet. In that case put the window on
1593 * the unpaired window list and continue when the surface gets
1594 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001595 uint32_t id = client_message->data.data32[0];
1596 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001597 if (resource) {
1598 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001599 xserver_map_shell_surface(window,
1600 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001601 }
1602 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001603 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001604 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001605 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001606}
1607
1608static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001609weston_wm_handle_client_message(struct weston_wm *wm,
1610 xcb_generic_event_t *event)
1611{
1612 xcb_client_message_event_t *client_message =
1613 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001614 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001615
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001616 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1617 get_atom_name(wm->conn, client_message->type),
1618 client_message->data.data32[0],
1619 client_message->data.data32[1],
1620 client_message->data.data32[2],
1621 client_message->data.data32[3],
1622 client_message->data.data32[4],
1623 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001624
Jason Ekstrand0250a742014-07-24 13:17:47 -07001625 /* The window may get created and destroyed before we actually
1626 * handle the message. If it doesn't exist, bail.
1627 */
Derek Foreman49372142015-04-09 10:51:22 -05001628 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001629 return;
1630
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001631 if (client_message->type == wm->atom.net_wm_moveresize)
1632 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001633 else if (client_message->type == wm->atom.net_wm_state)
1634 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001635 else if (client_message->type == wm->atom.wl_surface_id)
1636 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001637}
1638
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001639enum cursor_type {
1640 XWM_CURSOR_TOP,
1641 XWM_CURSOR_BOTTOM,
1642 XWM_CURSOR_LEFT,
1643 XWM_CURSOR_RIGHT,
1644 XWM_CURSOR_TOP_LEFT,
1645 XWM_CURSOR_TOP_RIGHT,
1646 XWM_CURSOR_BOTTOM_LEFT,
1647 XWM_CURSOR_BOTTOM_RIGHT,
1648 XWM_CURSOR_LEFT_PTR,
1649};
1650
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001651/*
1652 * The following correspondences between file names and cursors was copied
1653 * from: https://bugs.kde.org/attachment.cgi?id=67313
1654 */
1655
1656static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001657 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001658 "sw-resize",
1659 "size_bdiag"
1660};
1661
1662static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001663 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001664 "se-resize",
1665 "size_fdiag"
1666};
1667
1668static const char *bottom_sides[] = {
1669 "bottom_side",
1670 "s-resize",
1671 "size_ver"
1672};
1673
1674static const char *left_ptrs[] = {
1675 "left_ptr",
1676 "default",
1677 "top_left_arrow",
1678 "left-arrow"
1679};
1680
1681static const char *left_sides[] = {
1682 "left_side",
1683 "w-resize",
1684 "size_hor"
1685};
1686
1687static const char *right_sides[] = {
1688 "right_side",
1689 "e-resize",
1690 "size_hor"
1691};
1692
1693static const char *top_left_corners[] = {
1694 "top_left_corner",
1695 "nw-resize",
1696 "size_fdiag"
1697};
1698
1699static const char *top_right_corners[] = {
1700 "top_right_corner",
1701 "ne-resize",
1702 "size_bdiag"
1703};
1704
1705static const char *top_sides[] = {
1706 "top_side",
1707 "n-resize",
1708 "size_ver"
1709};
1710
1711struct cursor_alternatives {
1712 const char **names;
1713 size_t count;
1714};
1715
1716static const struct cursor_alternatives cursors[] = {
1717 {top_sides, ARRAY_LENGTH(top_sides)},
1718 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1719 {left_sides, ARRAY_LENGTH(left_sides)},
1720 {right_sides, ARRAY_LENGTH(right_sides)},
1721 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1722 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1723 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1724 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1725 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001726};
1727
1728static void
1729weston_wm_create_cursors(struct weston_wm *wm)
1730{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001731 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001732 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001733 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001734
1735 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1736 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001737 for (j = 0; j < cursors[i].count; j++) {
1738 name = cursors[i].names[j];
1739 wm->cursors[i] =
1740 xcb_cursor_library_load_cursor(wm, name);
1741 if (wm->cursors[i] != (xcb_cursor_t)-1)
1742 break;
1743 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001744 }
1745
1746 wm->last_cursor = -1;
1747}
1748
1749static void
1750weston_wm_destroy_cursors(struct weston_wm *wm)
1751{
1752 uint8_t i;
1753
1754 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1755 xcb_free_cursor(wm->conn, wm->cursors[i]);
1756
1757 free(wm->cursors);
1758}
1759
1760static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001761get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001762{
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001763 switch (location) {
1764 case THEME_LOCATION_RESIZING_TOP:
1765 return XWM_CURSOR_TOP;
1766 case THEME_LOCATION_RESIZING_BOTTOM:
1767 return XWM_CURSOR_BOTTOM;
1768 case THEME_LOCATION_RESIZING_LEFT:
1769 return XWM_CURSOR_LEFT;
1770 case THEME_LOCATION_RESIZING_RIGHT:
1771 return XWM_CURSOR_RIGHT;
1772 case THEME_LOCATION_RESIZING_TOP_LEFT:
1773 return XWM_CURSOR_TOP_LEFT;
1774 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1775 return XWM_CURSOR_TOP_RIGHT;
1776 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1777 return XWM_CURSOR_BOTTOM_LEFT;
1778 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1779 return XWM_CURSOR_BOTTOM_RIGHT;
1780 case THEME_LOCATION_EXTERIOR:
1781 case THEME_LOCATION_TITLEBAR:
1782 default:
1783 return XWM_CURSOR_LEFT_PTR;
1784 }
1785}
1786
1787static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001788weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1789 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001790{
1791 uint32_t cursor_value_list;
1792
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001793 if (wm->last_cursor == cursor)
1794 return;
1795
1796 wm->last_cursor = cursor;
1797
1798 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001799 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001800 XCB_CW_CURSOR, &cursor_value_list);
1801 xcb_flush(wm->conn);
1802}
1803
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001804static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001805weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001806{
1807 xcb_client_message_event_t client_message;
1808
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001809 if (window->delete_window) {
1810 client_message.response_type = XCB_CLIENT_MESSAGE;
1811 client_message.format = 32;
1812 client_message.window = window->id;
1813 client_message.type = window->wm->atom.wm_protocols;
1814 client_message.data.data32[0] =
1815 window->wm->atom.wm_delete_window;
1816 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001817
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001818 xcb_send_event(window->wm->conn, 0, window->id,
1819 XCB_EVENT_MASK_NO_EVENT,
1820 (char *) &client_message);
1821 } else {
1822 xcb_kill_client(window->wm->conn, window->id);
1823 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001824}
1825
1826static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001827weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1828{
1829 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
Quentin Glidic955cec02016-08-12 10:41:35 +02001830 const struct weston_desktop_xwayland_interface *xwayland_interface =
1831 wm->server->compositor->xwayland_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001832 struct weston_seat *seat;
Derek Foremane4d6c832015-08-05 14:48:11 -05001833 struct weston_pointer *pointer;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001834 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001835 enum theme_location location;
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001836 enum wl_pointer_button_state button_state;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001837 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001838
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001839 wm_log("XCB_BUTTON_%s (detail %d)\n",
1840 button->response_type == XCB_BUTTON_PRESS ?
1841 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001842
Derek Foreman49372142015-04-09 10:51:22 -05001843 if (!wm_lookup_window(wm, button->event, &window) ||
1844 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001845 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001846
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001847 if (button->detail != 1 && button->detail != 2)
1848 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001849
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001850 seat = weston_wm_pick_seat_for_window(window);
Derek Foremane4d6c832015-08-05 14:48:11 -05001851 pointer = weston_seat_get_pointer(seat);
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001852
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001853 button_state = button->response_type == XCB_BUTTON_PRESS ?
Quentin Glidicd8b17bc2016-07-10 11:00:55 +02001854 WL_POINTER_BUTTON_STATE_PRESSED :
1855 WL_POINTER_BUTTON_STATE_RELEASED;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001856 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1857
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001858 /* Make sure we're looking at the right location. The frame
1859 * could have received a motion event from a pointer from a
1860 * different wl_seat, but under X it looks like our core
1861 * pointer moved. Move the frame pointer to the button press
1862 * location before deciding what to do. */
1863 location = frame_pointer_motion(window->frame, NULL,
1864 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001865 location = frame_pointer_button(window->frame, NULL,
1866 button_id, button_state);
1867 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1868 weston_wm_window_schedule_repaint(window);
1869
1870 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001871 if (pointer)
Quentin Glidic955cec02016-08-12 10:41:35 +02001872 xwayland_interface->move(window->shsurf, pointer);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001873 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1874 }
1875
1876 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001877 if (pointer)
Quentin Glidic955cec02016-08-12 10:41:35 +02001878 xwayland_interface->resize(window->shsurf, pointer, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001879 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1880 }
1881
1882 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001883 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001884 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001885 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001886
1887 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1888 window->maximized_horz = !window->maximized_horz;
1889 window->maximized_vert = !window->maximized_vert;
1890 if (weston_wm_window_is_maximized(window)) {
1891 window->saved_width = window->width;
1892 window->saved_height = window->height;
Quentin Glidic955cec02016-08-12 10:41:35 +02001893 xwayland_interface->set_maximized(window->shsurf);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001894 } else {
1895 weston_wm_window_set_toplevel(window);
1896 }
1897 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1898 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001899}
1900
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001901static void
1902weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1903{
1904 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1905 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001906 enum theme_location location;
1907 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001908
Derek Foreman49372142015-04-09 10:51:22 -05001909 if (!wm_lookup_window(wm, motion->event, &window) ||
1910 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001911 return;
1912
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001913 location = frame_pointer_motion(window->frame, NULL,
1914 motion->event_x, motion->event_y);
1915 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1916 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001917
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001918 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001919 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001920}
1921
1922static void
1923weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1924{
1925 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1926 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001927 enum theme_location location;
1928 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001929
Derek Foreman49372142015-04-09 10:51:22 -05001930 if (!wm_lookup_window(wm, enter->event, &window) ||
1931 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001932 return;
1933
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001934 location = frame_pointer_enter(window->frame, NULL,
1935 enter->event_x, enter->event_y);
1936 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1937 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001938
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001939 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001940 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001941}
1942
1943static void
1944weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1945{
1946 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1947 struct weston_wm_window *window;
1948
Derek Foreman49372142015-04-09 10:51:22 -05001949 if (!wm_lookup_window(wm, leave->event, &window) ||
1950 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001951 return;
1952
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001953 frame_pointer_leave(window->frame, NULL);
1954 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1955 weston_wm_window_schedule_repaint(window);
1956
Tiago Vignattic1903232012-07-16 12:15:37 -04001957 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001958}
1959
Giulio Camuffob18f7882015-03-29 14:20:23 +03001960static void
1961weston_wm_handle_focus_in(struct weston_wm *wm, xcb_generic_event_t *event)
1962{
1963 xcb_focus_in_event_t *focus = (xcb_focus_in_event_t *) event;
1964 /* Do not let X clients change the focus behind the compositor's
1965 * back. Reset the focus to the old one if it changed. */
1966 if (!wm->focus_window || focus->event != wm->focus_window->id)
1967 weston_wm_send_focus_window(wm, wm->focus_window);
1968}
1969
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001970static int
1971weston_wm_handle_event(int fd, uint32_t mask, void *data)
1972{
1973 struct weston_wm *wm = data;
1974 xcb_generic_event_t *event;
1975 int count = 0;
1976
1977 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1978 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001979 free(event);
1980 count++;
1981 continue;
1982 }
1983
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001984 if (weston_wm_handle_dnd_event(wm, event)) {
1985 free(event);
1986 count++;
1987 continue;
1988 }
1989
MoD31700122013-06-11 19:58:55 -05001990 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001991 case XCB_BUTTON_PRESS:
1992 case XCB_BUTTON_RELEASE:
1993 weston_wm_handle_button(wm, event);
1994 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001995 case XCB_ENTER_NOTIFY:
1996 weston_wm_handle_enter(wm, event);
1997 break;
1998 case XCB_LEAVE_NOTIFY:
1999 weston_wm_handle_leave(wm, event);
2000 break;
2001 case XCB_MOTION_NOTIFY:
2002 weston_wm_handle_motion(wm, event);
2003 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002004 case XCB_CREATE_NOTIFY:
2005 weston_wm_handle_create_notify(wm, event);
2006 break;
2007 case XCB_MAP_REQUEST:
2008 weston_wm_handle_map_request(wm, event);
2009 break;
2010 case XCB_MAP_NOTIFY:
2011 weston_wm_handle_map_notify(wm, event);
2012 break;
2013 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04002014 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002015 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04002016 case XCB_REPARENT_NOTIFY:
2017 weston_wm_handle_reparent_notify(wm, event);
2018 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002019 case XCB_CONFIGURE_REQUEST:
2020 weston_wm_handle_configure_request(wm, event);
2021 break;
2022 case XCB_CONFIGURE_NOTIFY:
2023 weston_wm_handle_configure_notify(wm, event);
2024 break;
2025 case XCB_DESTROY_NOTIFY:
2026 weston_wm_handle_destroy_notify(wm, event);
2027 break;
2028 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002029 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002030 break;
2031 case XCB_PROPERTY_NOTIFY:
2032 weston_wm_handle_property_notify(wm, event);
2033 break;
2034 case XCB_CLIENT_MESSAGE:
2035 weston_wm_handle_client_message(wm, event);
2036 break;
Giulio Camuffob18f7882015-03-29 14:20:23 +03002037 case XCB_FOCUS_IN:
2038 weston_wm_handle_focus_in(wm, event);
2039 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002040 }
2041
2042 free(event);
2043 count++;
2044 }
2045
Marek Chalupaa1f3f3c2015-08-12 09:55:12 +02002046 if (count != 0)
2047 xcb_flush(wm->conn);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002048
2049 return count;
2050}
2051
2052static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002053weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window) {
2054 xcb_change_property(wm->conn, XCB_PROP_MODE_REPLACE,
2055 wm->screen->root, wm->atom.net_active_window,
2056 wm->atom.window, 32, 1, &window);
2057}
2058
2059static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002060weston_wm_get_visual_and_colormap(struct weston_wm *wm)
2061{
2062 xcb_depth_iterator_t d_iter;
2063 xcb_visualtype_iterator_t vt_iter;
2064 xcb_visualtype_t *visualtype;
2065
2066 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
2067 visualtype = NULL;
2068 while (d_iter.rem > 0) {
2069 if (d_iter.data->depth == 32) {
2070 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
2071 visualtype = vt_iter.data;
2072 break;
2073 }
2074
2075 xcb_depth_next(&d_iter);
2076 }
2077
2078 if (visualtype == NULL) {
2079 weston_log("no 32 bit visualtype\n");
2080 return;
2081 }
2082
2083 wm->visual_id = visualtype->visual_id;
2084 wm->colormap = xcb_generate_id(wm->conn);
2085 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
2086 wm->colormap, wm->screen->root, wm->visual_id);
2087}
2088
2089static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002090weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002091{
2092
2093#define F(field) offsetof(struct weston_wm, field)
2094
2095 static const struct { const char *name; int offset; } atoms[] = {
2096 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002097 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002098 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
2099 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04002100 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002101 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002102 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002103 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002104 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002105 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002106 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2107 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002108 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2109 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002110 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2111 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2112 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02002113 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002114 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2115
2116 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2117 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2118 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2119 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2120 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2121 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2122 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002123 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2124 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002125 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2126 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2127 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2128 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2129 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2130
2131 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2132 { "_NET_SUPPORTING_WM_CHECK",
2133 F(atom.net_supporting_wm_check) },
2134 { "_NET_SUPPORTED", F(atom.net_supported) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002135 { "_NET_ACTIVE_WINDOW", F(atom.net_active_window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002136 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2137 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002138 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002139 { "TARGETS", F(atom.targets) },
2140 { "UTF8_STRING", F(atom.utf8_string) },
2141 { "_WL_SELECTION", F(atom.wl_selection) },
2142 { "INCR", F(atom.incr) },
2143 { "TIMESTAMP", F(atom.timestamp) },
2144 { "MULTIPLE", F(atom.multiple) },
2145 { "UTF8_STRING" , F(atom.utf8_string) },
2146 { "COMPOUND_TEXT", F(atom.compound_text) },
2147 { "TEXT", F(atom.text) },
2148 { "STRING", F(atom.string) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002149 { "WINDOW", F(atom.window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002150 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2151 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002152 { "XdndSelection", F(atom.xdnd_selection) },
2153 { "XdndAware", F(atom.xdnd_aware) },
2154 { "XdndEnter", F(atom.xdnd_enter) },
2155 { "XdndLeave", F(atom.xdnd_leave) },
2156 { "XdndDrop", F(atom.xdnd_drop) },
2157 { "XdndStatus", F(atom.xdnd_status) },
2158 { "XdndFinished", F(atom.xdnd_finished) },
2159 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002160 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2161 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002162 };
2163#undef F
2164
2165 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2166 xcb_xfixes_query_version_reply_t *xfixes_reply;
2167 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2168 xcb_intern_atom_reply_t *reply;
2169 xcb_render_query_pict_formats_reply_t *formats_reply;
2170 xcb_render_query_pict_formats_cookie_t formats_cookie;
2171 xcb_render_pictforminfo_t *formats;
2172 uint32_t i;
2173
2174 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002175 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002176
2177 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2178
2179 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2180 cookies[i] = xcb_intern_atom (wm->conn, 0,
2181 strlen(atoms[i].name),
2182 atoms[i].name);
2183
2184 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2185 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2186 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2187 free(reply);
2188 }
2189
2190 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2191 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002192 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002193
2194 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2195 XCB_XFIXES_MAJOR_VERSION,
2196 XCB_XFIXES_MINOR_VERSION);
2197 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2198 xfixes_cookie, NULL);
2199
Martin Minarik6d118362012-06-07 18:01:59 +02002200 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002201 xfixes_reply->major_version, xfixes_reply->minor_version);
2202
2203 free(xfixes_reply);
2204
2205 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2206 formats_cookie, 0);
2207 if (formats_reply == NULL)
2208 return;
2209
2210 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002211 for (i = 0; i < formats_reply->num_formats; i++) {
2212 if (formats[i].direct.red_mask != 0xff &&
2213 formats[i].direct.red_shift != 16)
2214 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002215 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2216 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002217 wm->format_rgb = formats[i];
2218 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2219 formats[i].depth == 32 &&
2220 formats[i].direct.alpha_mask == 0xff &&
2221 formats[i].direct.alpha_shift == 24)
2222 wm->format_rgba = formats[i];
2223 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002224
2225 free(formats_reply);
2226}
2227
2228static void
2229weston_wm_create_wm_window(struct weston_wm *wm)
2230{
2231 static const char name[] = "Weston WM";
2232
2233 wm->wm_window = xcb_generate_id(wm->conn);
2234 xcb_create_window(wm->conn,
2235 XCB_COPY_FROM_PARENT,
2236 wm->wm_window,
2237 wm->screen->root,
2238 0, 0,
2239 10, 10,
2240 0,
2241 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2242 wm->screen->root_visual,
2243 0, NULL);
2244
2245 xcb_change_property(wm->conn,
2246 XCB_PROP_MODE_REPLACE,
2247 wm->wm_window,
2248 wm->atom.net_supporting_wm_check,
2249 XCB_ATOM_WINDOW,
2250 32, /* format */
2251 1, &wm->wm_window);
2252
2253 xcb_change_property(wm->conn,
2254 XCB_PROP_MODE_REPLACE,
2255 wm->wm_window,
2256 wm->atom.net_wm_name,
2257 wm->atom.utf8_string,
2258 8, /* format */
2259 strlen(name), name);
2260
2261 xcb_change_property(wm->conn,
2262 XCB_PROP_MODE_REPLACE,
2263 wm->screen->root,
2264 wm->atom.net_supporting_wm_check,
2265 XCB_ATOM_WINDOW,
2266 32, /* format */
2267 1, &wm->wm_window);
2268
Abdur Rehmanb8b150b2017-01-01 19:46:46 +05002269 /* Claim the WM_S0 selection even though we don't support
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002270 * the --replace functionality. */
2271 xcb_set_selection_owner(wm->conn,
2272 wm->wm_window,
2273 wm->atom.wm_s0,
2274 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002275
2276 xcb_set_selection_owner(wm->conn,
2277 wm->wm_window,
2278 wm->atom.net_wm_cm_s0,
2279 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002280}
2281
2282struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002283weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002284{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002285 struct weston_wm *wm;
2286 struct wl_event_loop *loop;
2287 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002288 uint32_t values[1];
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002289 xcb_atom_t supported[6];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002290
Peter Huttererf3d62272013-08-08 11:57:05 +10002291 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002292 if (wm == NULL)
2293 return NULL;
2294
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002295 wm->server = wxs;
2296 wm->window_hash = hash_table_create();
2297 if (wm->window_hash == NULL) {
2298 free(wm);
2299 return NULL;
2300 }
2301
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002302 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002303 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002304 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002305 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002306 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002307 hash_table_destroy(wm->window_hash);
2308 free(wm);
2309 return NULL;
2310 }
2311
2312 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2313 wm->screen = s.data;
2314
2315 loop = wl_display_get_event_loop(wxs->wl_display);
2316 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002317 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002318 WL_EVENT_READABLE,
2319 weston_wm_handle_event, wm);
2320 wl_event_source_check(wm->source);
2321
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002322 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002323 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002324
2325 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002326 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2327 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2328 XCB_EVENT_MASK_PROPERTY_CHANGE;
2329 xcb_change_window_attributes(wm->conn, wm->screen->root,
2330 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002331
2332 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2333 XCB_COMPOSITE_REDIRECT_MANUAL);
2334
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002335 wm->theme = theme_create();
2336
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002337 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002338 supported[1] = wm->atom.net_wm_state;
2339 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002340 supported[3] = wm->atom.net_wm_state_maximized_vert;
2341 supported[4] = wm->atom.net_wm_state_maximized_horz;
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002342 supported[5] = wm->atom.net_active_window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002343 xcb_change_property(wm->conn,
2344 XCB_PROP_MODE_REPLACE,
2345 wm->screen->root,
2346 wm->atom.net_supported,
2347 XCB_ATOM_ATOM,
2348 32, /* format */
2349 ARRAY_LENGTH(supported), supported);
2350
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002351 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
2352
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002353 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002354
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002355 weston_wm_dnd_init(wm);
2356
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002357 xcb_flush(wm->conn);
2358
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002359 wm->create_surface_listener.notify = weston_wm_create_surface;
2360 wl_signal_add(&wxs->compositor->create_surface_signal,
2361 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002362 wm->activate_listener.notify = weston_wm_window_activate;
2363 wl_signal_add(&wxs->compositor->activate_signal,
2364 &wm->activate_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002365 wm->kill_listener.notify = weston_wm_kill_client;
2366 wl_signal_add(&wxs->compositor->kill_signal,
2367 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002368 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002369
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002370 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002371 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002372
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002373 /* Create wm window and take WM_S0 selection last, which
2374 * signals to Xwayland that we're done with setup. */
2375 weston_wm_create_wm_window(wm);
2376
2377 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002378
2379 return wm;
2380}
2381
2382void
2383weston_wm_destroy(struct weston_wm *wm)
2384{
2385 /* FIXME: Free windows in hash. */
2386 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002387 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002388 xcb_disconnect(wm->conn);
2389 wl_event_source_remove(wm->source);
2390 wl_list_remove(&wm->selection_listener.link);
2391 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002392 wl_list_remove(&wm->kill_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002393 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002394
2395 free(wm);
2396}
2397
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002398static struct weston_wm_window *
2399get_wm_window(struct weston_surface *surface)
2400{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002401 struct wl_listener *listener;
2402
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002403 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002404 if (listener)
2405 return container_of(listener, struct weston_wm_window,
2406 surface_destroy_listener);
2407
2408 return NULL;
2409}
2410
Quentin Glidic955cec02016-08-12 10:41:35 +02002411static bool
2412is_wm_window(struct weston_surface *surface)
2413{
2414 return get_wm_window(surface) != NULL;
2415}
2416
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002417static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002418weston_wm_window_configure(void *data)
2419{
2420 struct weston_wm_window *window = data;
2421 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002422 uint32_t values[4];
2423 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002424
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002425 weston_wm_window_get_child_position(window, &x, &y);
2426 values[0] = x;
2427 values[1] = y;
2428 values[2] = window->width;
2429 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002430 xcb_configure_window(wm->conn,
2431 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002432 XCB_CONFIG_WINDOW_X |
2433 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002434 XCB_CONFIG_WINDOW_WIDTH |
2435 XCB_CONFIG_WINDOW_HEIGHT,
2436 values);
2437
2438 weston_wm_window_get_frame_size(window, &width, &height);
2439 values[0] = width;
2440 values[1] = height;
2441 xcb_configure_window(wm->conn,
2442 window->frame_id,
2443 XCB_CONFIG_WINDOW_WIDTH |
2444 XCB_CONFIG_WINDOW_HEIGHT,
2445 values);
2446
2447 window->configure_source = NULL;
2448
2449 weston_wm_window_schedule_repaint(window);
2450}
2451
2452static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002453send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002454{
2455 struct weston_wm_window *window = get_wm_window(surface);
2456 struct weston_wm *wm = window->wm;
2457 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002458 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002459
Marek Chalupae9fe4672014-10-29 13:44:44 +01002460 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002461 hborder = 2 * t->width;
2462 vborder = t->titlebar_height + t->width;
2463 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002464 hborder = 0;
2465 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002466 }
2467
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002468 if (width > hborder)
2469 window->width = width - hborder;
2470 else
2471 window->width = 1;
2472
2473 if (height > vborder)
2474 window->height = height - vborder;
2475 else
2476 window->height = 1;
2477
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002478 if (window->frame)
2479 frame_resize_inside(window->frame, window->width, window->height);
2480
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002481 if (window->configure_source)
2482 return;
2483
2484 window->configure_source =
2485 wl_event_loop_add_idle(wm->server->loop,
2486 weston_wm_window_configure, window);
2487}
2488
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002489static void
2490send_position(struct weston_surface *surface, int32_t x, int32_t y)
2491{
2492 struct weston_wm_window *window = get_wm_window(surface);
2493 struct weston_wm *wm;
2494 uint32_t mask, values[2];
2495
2496 if (!window || !window->wm)
2497 return;
2498
2499 wm = window->wm;
2500 /* We use pos_dirty to tell whether a configure message is in flight.
2501 * This is needed in case we send two configure events in a very
2502 * short time, since window->x/y is set in after a roundtrip, hence
2503 * we cannot just check if the current x and y are different. */
2504 if (window->x != x || window->y != y || window->pos_dirty) {
2505 window->pos_dirty = true;
2506 values[0] = x;
2507 values[1] = y;
2508 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
2509
2510 xcb_configure_window(wm->conn, window->frame_id, mask, values);
2511 xcb_flush(wm->conn);
2512 }
2513}
2514
Quentin Glidic955cec02016-08-12 10:41:35 +02002515static const struct weston_xwayland_client_interface shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002516 send_configure,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002517};
2518
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002519static int
2520legacy_fullscreen(struct weston_wm *wm,
2521 struct weston_wm_window *window,
2522 struct weston_output **output_ret)
2523{
2524 struct weston_compositor *compositor = wm->server->compositor;
2525 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002526 uint32_t minmax = PMinSize | PMaxSize;
2527 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002528
2529 /* Heuristics for detecting legacy fullscreen windows... */
2530
2531 wl_list_for_each(output, &compositor->output_list, link) {
2532 if (output->x == window->x &&
2533 output->y == window->y &&
2534 output->width == window->width &&
2535 output->height == window->height &&
2536 window->override_redirect) {
2537 *output_ret = output;
2538 return 1;
2539 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002540
2541 matching_size = 0;
2542 if ((window->size_hints.flags & (USSize |PSize)) &&
2543 window->size_hints.width == output->width &&
2544 window->size_hints.height == output->height)
2545 matching_size = 1;
2546 if ((window->size_hints.flags & minmax) == minmax &&
2547 window->size_hints.min_width == output->width &&
2548 window->size_hints.min_height == output->height &&
2549 window->size_hints.max_width == output->width &&
2550 window->size_hints.max_height == output->height)
2551 matching_size = 1;
2552
2553 if (matching_size && !window->decorate &&
2554 (window->size_hints.flags & (USPosition | PPosition)) &&
2555 window->size_hints.x == output->x &&
2556 window->size_hints.y == output->y) {
2557 *output_ret = output;
2558 return 1;
2559 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002560 }
2561
2562 return 0;
2563}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002564
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002565static bool
2566weston_wm_window_type_inactive(struct weston_wm_window *window)
2567{
2568 struct weston_wm *wm = window->wm;
2569
2570 return window->type == wm->atom.net_wm_window_type_tooltip ||
2571 window->type == wm->atom.net_wm_window_type_dropdown ||
2572 window->type == wm->atom.net_wm_window_type_dnd ||
2573 window->type == wm->atom.net_wm_window_type_combo ||
Giulio Camuffo84787ea2015-04-29 19:00:48 +03002574 window->type == wm->atom.net_wm_window_type_popup ||
2575 window->type == wm->atom.net_wm_window_type_utility;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002576}
2577
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002578static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002579xserver_map_shell_surface(struct weston_wm_window *window,
2580 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002581{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002582 struct weston_wm *wm = window->wm;
Quentin Glidic955cec02016-08-12 10:41:35 +02002583 struct weston_desktop_xwayland *xwayland =
2584 wm->server->compositor->xwayland;
2585 const struct weston_desktop_xwayland_interface *xwayland_interface =
2586 wm->server->compositor->xwayland_interface;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002587 struct weston_wm_window *parent;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002588
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002589 weston_wm_window_read_properties(window);
2590
2591 /* A weston_wm_window may have many different surfaces assigned
2592 * throughout its life, so we must make sure to remove the listener
2593 * from the old surface signal list. */
2594 if (window->surface)
2595 wl_list_remove(&window->surface_destroy_listener.link);
2596
2597 window->surface = surface;
2598 window->surface_destroy_listener.notify = surface_destroy;
2599 wl_signal_add(&window->surface->destroy_signal,
2600 &window->surface_destroy_listener);
2601
2602 weston_wm_window_schedule_repaint(window);
2603
Quentin Glidic955cec02016-08-12 10:41:35 +02002604 if (!xwayland_interface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002605 return;
2606
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002607 if (window->surface->committed) {
Pekka Paalanen50b67472014-10-01 15:02:41 +03002608 weston_log("warning, unexpected in %s: "
2609 "surface's configure hook is already set.\n",
2610 __func__);
2611 return;
2612 }
2613
Murray Calavera883ac022015-06-06 13:02:22 +00002614 window->shsurf =
Quentin Glidic955cec02016-08-12 10:41:35 +02002615 xwayland_interface->create_surface(xwayland,
2616 window->surface,
2617 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002618
Pekka Paalanena04eacc2016-11-28 16:42:25 +02002619 wm_log("XWM: map shell surface, win %d, xwayland surface %p\n",
2620 window->id, window->shsurf);
2621
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002622 if (window->name)
Quentin Glidic955cec02016-08-12 10:41:35 +02002623 xwayland_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002624 if (window->pid > 0)
Quentin Glidic955cec02016-08-12 10:41:35 +02002625 xwayland_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002626
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002627 if (window->fullscreen) {
2628 window->saved_width = window->width;
2629 window->saved_height = window->height;
Pekka Paalanen505237e2016-12-01 15:41:11 +02002630 xwayland_interface->set_fullscreen(window->shsurf,
2631 window->legacy_fullscreen_output.output);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002632 return;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002633 } else if (window->override_redirect) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002634 xwayland_interface->set_xwayland(window->shsurf,
2635 window->x, window->y);
Axel Davye4450f92014-01-12 15:06:05 +01002636 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002637 parent = window->transient_for;
Quentin Glidic955cec02016-08-12 10:41:35 +02002638 if (weston_wm_window_type_inactive(window)) {
2639 xwayland_interface->set_transient(window->shsurf,
2640 parent->surface,
2641 window->x - parent->x,
2642 window->y - parent->y);
2643 } else {
2644 xwayland_interface->set_toplevel(window->shsurf);
2645 xwayland_interface->set_parent(window->shsurf,
2646 parent->surface);
2647 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002648 } else if (weston_wm_window_is_maximized(window)) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002649 xwayland_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002650 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002651 if (weston_wm_window_type_inactive(window)) {
Quentin Glidic955cec02016-08-12 10:41:35 +02002652 xwayland_interface->set_xwayland(window->shsurf,
2653 window->x,
2654 window->y);
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002655 } else {
Quentin Glidic955cec02016-08-12 10:41:35 +02002656 xwayland_interface->set_toplevel(window->shsurf);
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002657 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002658 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002659}
Quentin Glidic955cec02016-08-12 10:41:35 +02002660
2661const struct weston_xwayland_surface_api surface_api = {
2662 is_wm_window,
2663 send_position,
2664};