blob: 5f86d8a01883f3001d4d9e9ee41e1dce0d91e623 [file] [log] [blame]
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001/*
2 * Copyright © 2011 Intel Corporation
3 *
Bryce Harrington0a007dd2015-06-11 16:22:34 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Kristian Høgsberg380deee2012-05-21 17:12:41 -040011 *
Bryce Harrington0a007dd2015-06-11 16:22:34 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Kristian Høgsberg380deee2012-05-21 17:12:41 -040024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040027
28#include <stdlib.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030029#include <stdint.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040030#include <stdio.h>
31#include <string.h>
32#include <sys/socket.h>
33#include <sys/un.h>
34#include <fcntl.h>
35#include <errno.h>
36#include <unistd.h>
37#include <signal.h>
Tiago Vignatti90fada42012-07-16 12:02:08 -040038#include <X11/Xcursor/Xcursor.h>
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -050039#include <linux/input.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040040
41#include "xwayland.h"
42
Kristian Høgsberg2ba10df2013-12-03 16:38:15 -080043#include "cairo-util.h"
44#include "compositor.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040045#include "hash.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070046#include "shared/helpers.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040047
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070048struct wm_size_hints {
Bryce Harringtone00554b2015-06-12 10:17:39 -070049 uint32_t flags;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070050 int32_t x, y;
51 int32_t width, height; /* should set so old wm's don't mess up */
52 int32_t min_width, min_height;
53 int32_t max_width, max_height;
Bryce Harringtone00554b2015-06-12 10:17:39 -070054 int32_t width_inc, height_inc;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070055 struct {
56 int32_t x;
57 int32_t y;
58 } min_aspect, max_aspect;
59 int32_t base_width, base_height;
60 int32_t win_gravity;
61};
62
63#define USPosition (1L << 0)
64#define USSize (1L << 1)
65#define PPosition (1L << 2)
66#define PSize (1L << 3)
67#define PMinSize (1L << 4)
68#define PMaxSize (1L << 5)
69#define PResizeInc (1L << 6)
70#define PAspect (1L << 7)
71#define PBaseSize (1L << 8)
72#define PWinGravity (1L << 9)
73
Kristian Høgsberg380deee2012-05-21 17:12:41 -040074struct motif_wm_hints {
75 uint32_t flags;
76 uint32_t functions;
77 uint32_t decorations;
78 int32_t input_mode;
79 uint32_t status;
80};
81
82#define MWM_HINTS_FUNCTIONS (1L << 0)
83#define MWM_HINTS_DECORATIONS (1L << 1)
84#define MWM_HINTS_INPUT_MODE (1L << 2)
85#define MWM_HINTS_STATUS (1L << 3)
86
87#define MWM_FUNC_ALL (1L << 0)
88#define MWM_FUNC_RESIZE (1L << 1)
89#define MWM_FUNC_MOVE (1L << 2)
90#define MWM_FUNC_MINIMIZE (1L << 3)
91#define MWM_FUNC_MAXIMIZE (1L << 4)
92#define MWM_FUNC_CLOSE (1L << 5)
93
94#define MWM_DECOR_ALL (1L << 0)
95#define MWM_DECOR_BORDER (1L << 1)
96#define MWM_DECOR_RESIZEH (1L << 2)
97#define MWM_DECOR_TITLE (1L << 3)
98#define MWM_DECOR_MENU (1L << 4)
99#define MWM_DECOR_MINIMIZE (1L << 5)
100#define MWM_DECOR_MAXIMIZE (1L << 6)
101
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700102#define MWM_DECOR_EVERYTHING \
103 (MWM_DECOR_BORDER | MWM_DECOR_RESIZEH | MWM_DECOR_TITLE | \
104 MWM_DECOR_MENU | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE)
105
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400106#define MWM_INPUT_MODELESS 0
107#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
108#define MWM_INPUT_SYSTEM_MODAL 2
109#define MWM_INPUT_FULL_APPLICATION_MODAL 3
110#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
111
112#define MWM_TEAROFF_WINDOW (1L<<0)
113
114#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
115#define _NET_WM_MOVERESIZE_SIZE_TOP 1
116#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
117#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
118#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
119#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
120#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
121#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
122#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
123#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
124#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
125#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
126
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400127struct weston_wm_window {
128 struct weston_wm *wm;
129 xcb_window_t id;
130 xcb_window_t frame_id;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500131 struct frame *frame;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400132 cairo_surface_t *cairo_surface;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700133 uint32_t surface_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400134 struct weston_surface *surface;
135 struct shell_surface *shsurf;
136 struct wl_listener surface_destroy_listener;
137 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400138 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400139 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300140 int pid;
141 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400142 char *class;
143 char *name;
144 struct weston_wm_window *transient_for;
145 uint32_t protocols;
146 xcb_atom_t type;
147 int width, height;
148 int x, y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200149 bool pos_dirty;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500150 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400151 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300152 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500153 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500154 int has_alpha;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700155 int delete_window;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200156 int maximized_vert;
157 int maximized_horz;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700158 struct wm_size_hints size_hints;
159 struct motif_wm_hints motif_hints;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700160 struct wl_list link;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400161};
162
163static struct weston_wm_window *
164get_wm_window(struct weston_surface *surface);
165
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400166static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200167weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window);
168
169static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400170weston_wm_window_schedule_repaint(struct weston_wm_window *window);
171
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700172static void
173xserver_map_shell_surface(struct weston_wm_window *window,
174 struct weston_surface *surface);
175
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400176static int __attribute__ ((format (printf, 1, 2)))
177wm_log(const char *fmt, ...)
178{
179#ifdef WM_DEBUG
180 int l;
181 va_list argp;
182
183 va_start(argp, fmt);
184 l = weston_vlog(fmt, argp);
185 va_end(argp);
186
187 return l;
188#else
189 return 0;
190#endif
191}
192
193static int __attribute__ ((format (printf, 1, 2)))
194wm_log_continue(const char *fmt, ...)
195{
196#ifdef WM_DEBUG
197 int l;
198 va_list argp;
199
200 va_start(argp, fmt);
201 l = weston_vlog_continue(fmt, argp);
202 va_end(argp);
203
204 return l;
205#else
206 return 0;
207#endif
208}
209
Derek Foreman49372142015-04-09 10:51:22 -0500210static bool __attribute__ ((warn_unused_result))
211wm_lookup_window(struct weston_wm *wm, xcb_window_t hash,
212 struct weston_wm_window **window)
213{
214 *window = hash_table_lookup(wm->window_hash, hash);
215 if (*window)
216 return true;
217 return false;
218}
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400219
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400220const char *
221get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
222{
223 xcb_get_atom_name_cookie_t cookie;
224 xcb_get_atom_name_reply_t *reply;
225 xcb_generic_error_t *e;
226 static char buffer[64];
227
228 if (atom == XCB_ATOM_NONE)
229 return "None";
230
231 cookie = xcb_get_atom_name (c, atom);
232 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500233
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300234 if (reply) {
MoD55375b92013-06-11 19:59:42 -0500235 snprintf(buffer, sizeof buffer, "%.*s",
236 xcb_get_atom_name_name_length (reply),
237 xcb_get_atom_name_name (reply));
238 } else {
239 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
240 }
241
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400242 free(reply);
243
244 return buffer;
245}
246
Tiago Vignatti90fada42012-07-16 12:02:08 -0400247static xcb_cursor_t
248xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
249{
250 xcb_connection_t *c = wm->conn;
251 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
252 xcb_screen_t *screen = s.data;
253 xcb_gcontext_t gc;
254 xcb_pixmap_t pix;
255 xcb_render_picture_t pic;
256 xcb_cursor_t cursor;
257 int stride = img->width * 4;
258
259 pix = xcb_generate_id(c);
260 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
261
262 pic = xcb_generate_id(c);
263 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
264
265 gc = xcb_generate_id(c);
266 xcb_create_gc(c, gc, pix, 0, 0);
267
268 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
269 img->width, img->height, 0, 0, 0, 32,
270 stride * img->height, (uint8_t *) img->pixels);
271 xcb_free_gc(c, gc);
272
273 cursor = xcb_generate_id(c);
274 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
275
276 xcb_render_free_picture(c, pic);
277 xcb_free_pixmap(c, pix);
278
279 return cursor;
280}
281
282static xcb_cursor_t
283xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
284{
285 /* TODO: treat animated cursors as well */
286 if (images->nimage != 1)
287 return -1;
288
289 return xcb_cursor_image_load_cursor(wm, images->images[0]);
290}
291
292static xcb_cursor_t
293xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
294{
295 xcb_cursor_t cursor;
296 XcursorImages *images;
297 char *v = NULL;
298 int size = 0;
299
300 if (!file)
301 return 0;
302
303 v = getenv ("XCURSOR_SIZE");
304 if (v)
305 size = atoi(v);
306
307 if (!size)
308 size = 32;
309
310 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300311 if (!images)
312 return -1;
313
Tiago Vignatti90fada42012-07-16 12:02:08 -0400314 cursor = xcb_cursor_images_load_cursor (wm, images);
315 XcursorImagesDestroy (images);
316
317 return cursor;
318}
319
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400320void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400321dump_property(struct weston_wm *wm,
322 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400323{
324 int32_t *incr_value;
325 const char *text_value, *name;
326 xcb_atom_t *atom_value;
327 int width, len;
328 uint32_t i;
329
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400330 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400331 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400332 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400333 return;
334 }
335
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400336 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
337 get_atom_name(wm->conn, reply->type),
338 reply->format,
339 xcb_get_property_value_length(reply),
340 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400341
342 if (reply->type == wm->atom.incr) {
343 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400344 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400345 } else if (reply->type == wm->atom.utf8_string ||
346 reply->type == wm->atom.string) {
347 text_value = xcb_get_property_value(reply);
348 if (reply->value_len > 40)
349 len = 40;
350 else
351 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400352 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400353 } else if (reply->type == XCB_ATOM_ATOM) {
354 atom_value = xcb_get_property_value(reply);
355 for (i = 0; i < reply->value_len; i++) {
356 name = get_atom_name(wm->conn, atom_value[i]);
357 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400358 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400359 width = 4;
360 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400361 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400362 }
363
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400364 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400365 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400366 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400367 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400368 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400369 }
370}
371
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200372static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400373read_and_dump_property(struct weston_wm *wm,
374 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400375{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400376 xcb_get_property_reply_t *reply;
377 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400378
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400379 cookie = xcb_get_property(wm->conn, 0, window,
380 property, XCB_ATOM_ANY, 0, 2048);
381 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400382
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400383 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400384
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400385 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400386}
387
388/* We reuse some predefined, but otherwise useles atoms */
389#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
390#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500391#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700392#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400393
394static void
395weston_wm_window_read_properties(struct weston_wm_window *window)
396{
397 struct weston_wm *wm = window->wm;
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200398 struct weston_shell_interface *shell_interface =
399 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400400
401#define F(field) offsetof(struct weston_wm_window, field)
402 const struct {
403 xcb_atom_t atom;
404 xcb_atom_t type;
405 int offset;
406 } props[] = {
407 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
408 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
409 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
410 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700411 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500412 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400413 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
414 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300415 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400416 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300417 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400418 };
419#undef F
420
421 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
422 xcb_get_property_reply_t *reply;
423 void *p;
424 uint32_t *xid;
425 xcb_atom_t *atom;
426 uint32_t i;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200427 char name[1024];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400428
429 if (!window->properties_dirty)
430 return;
431 window->properties_dirty = 0;
432
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400433 for (i = 0; i < ARRAY_LENGTH(props); i++)
434 cookie[i] = xcb_get_property(wm->conn,
435 0, /* delete */
436 window->id,
437 props[i].atom,
438 XCB_ATOM_ANY, 0, 2048);
439
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700440 window->decorate = window->override_redirect ? 0 : MWM_DECOR_EVERYTHING;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700441 window->size_hints.flags = 0;
442 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700443 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700444
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400445 for (i = 0; i < ARRAY_LENGTH(props); i++) {
446 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
447 if (!reply)
448 /* Bad window, typically */
449 continue;
450 if (reply->type == XCB_ATOM_NONE) {
451 /* No such property */
452 free(reply);
453 continue;
454 }
455
456 p = ((char *) window + props[i].offset);
457
458 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300459 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400460 case XCB_ATOM_STRING:
461 /* FIXME: We're using this for both string and
462 utf8_string */
463 if (*(char **) p)
464 free(*(char **) p);
465
466 *(char **) p =
467 strndup(xcb_get_property_value(reply),
468 xcb_get_property_value_length(reply));
469 break;
470 case XCB_ATOM_WINDOW:
471 xid = xcb_get_property_value(reply);
Derek Foreman49372142015-04-09 10:51:22 -0500472 if (!wm_lookup_window(wm, *xid, p))
473 weston_log("XCB_ATOM_WINDOW contains window"
474 " id not found in hash table.\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400475 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300476 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400477 case XCB_ATOM_ATOM:
478 atom = xcb_get_property_value(reply);
479 *(xcb_atom_t *) p = *atom;
480 break;
481 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700482 atom = xcb_get_property_value(reply);
483 for (i = 0; i < reply->value_len; i++)
Derek Foremanb4deec62015-04-07 12:12:13 -0500484 if (atom[i] == wm->atom.wm_delete_window) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700485 window->delete_window = 1;
Derek Foremanb4deec62015-04-07 12:12:13 -0500486 break;
487 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400488 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700489 case TYPE_WM_NORMAL_HINTS:
490 memcpy(&window->size_hints,
491 xcb_get_property_value(reply),
492 sizeof window->size_hints);
493 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500494 case TYPE_NET_WM_STATE:
495 window->fullscreen = 0;
496 atom = xcb_get_property_value(reply);
Ryo Munakataf3744f52015-03-11 17:36:30 +0900497 for (i = 0; i < reply->value_len; i++) {
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500498 if (atom[i] == wm->atom.net_wm_state_fullscreen)
499 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200500 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
501 window->maximized_vert = 1;
502 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
503 window->maximized_horz = 1;
Ryo Munakataf3744f52015-03-11 17:36:30 +0900504 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500505 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400506 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700507 memcpy(&window->motif_hints,
508 xcb_get_property_value(reply),
509 sizeof window->motif_hints);
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700510 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) {
511 if (window->motif_hints.decorations & MWM_DECOR_ALL)
512 /* MWM_DECOR_ALL means all except the other values listed. */
513 window->decorate =
514 MWM_DECOR_EVERYTHING & (~window->motif_hints.decorations);
515 else
516 window->decorate =
517 window->motif_hints.decorations;
518 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400519 break;
520 default:
521 break;
522 }
523 free(reply);
524 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200525
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200526 if (window->pid > 0) {
527 gethostname(name, sizeof(name));
528 for (i = 0; i < sizeof(name); i++) {
529 if (name[i] == '\0')
530 break;
531 }
532 if (i == sizeof(name))
533 name[0] = '\0'; /* ignore stupid hostnames */
534
535 /* this is only one heuristic to guess the PID of a client is
536 * valid, assuming it's compliant with icccm and ewmh.
537 * Non-compliants and remote applications of course fail. */
538 if (!window->machine || strcmp(window->machine, name))
539 window->pid = 0;
540 }
541
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200542 if (window->shsurf && window->name)
543 shell_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500544 if (window->frame && window->name)
545 frame_set_title(window->frame, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200546 if (window->shsurf && window->pid > 0)
547 shell_interface->set_pid(window->shsurf, window->pid);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400548}
549
550static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400551weston_wm_window_get_frame_size(struct weston_wm_window *window,
552 int *width, int *height)
553{
554 struct theme *t = window->wm->theme;
555
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500556 if (window->fullscreen) {
557 *width = window->width;
558 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800559 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500560 *width = frame_width(window->frame);
561 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400562 } else {
563 *width = window->width + t->margin * 2;
564 *height = window->height + t->margin * 2;
565 }
566}
567
568static void
569weston_wm_window_get_child_position(struct weston_wm_window *window,
570 int *x, int *y)
571{
572 struct theme *t = window->wm->theme;
573
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500574 if (window->fullscreen) {
575 *x = 0;
576 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800577 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500578 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400579 } else {
580 *x = t->margin;
581 *y = t->margin;
582 }
583}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400584
585static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500586weston_wm_window_send_configure_notify(struct weston_wm_window *window)
587{
588 xcb_configure_notify_event_t configure_notify;
589 struct weston_wm *wm = window->wm;
590 int x, y;
591
592 weston_wm_window_get_child_position(window, &x, &y);
593 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
594 configure_notify.pad0 = 0;
595 configure_notify.event = window->id;
596 configure_notify.window = window->id;
597 configure_notify.above_sibling = XCB_WINDOW_NONE;
598 configure_notify.x = x;
599 configure_notify.y = y;
600 configure_notify.width = window->width;
601 configure_notify.height = window->height;
602 configure_notify.border_width = 0;
603 configure_notify.override_redirect = 0;
604 configure_notify.pad1 = 0;
605
Murray Calavera883ac022015-06-06 13:02:22 +0000606 xcb_send_event(wm->conn, 0, window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500607 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
608 (char *) &configure_notify);
609}
610
611static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400612weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
613{
Murray Calavera883ac022015-06-06 13:02:22 +0000614 xcb_configure_request_event_t *configure_request =
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400615 (xcb_configure_request_event_t *) event;
616 struct weston_wm_window *window;
617 uint32_t mask, values[16];
618 int x, y, width, height, i = 0;
619
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400620 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
621 configure_request->window,
622 configure_request->x, configure_request->y,
623 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400624
Derek Foreman49372142015-04-09 10:51:22 -0500625 if (!wm_lookup_window(wm, configure_request->window, &window))
626 return;
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400627
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500628 if (window->fullscreen) {
629 weston_wm_window_send_configure_notify(window);
630 return;
631 }
632
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400633 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
634 window->width = configure_request->width;
635 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
636 window->height = configure_request->height;
637
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500638 if (window->frame)
639 frame_resize_inside(window->frame, window->width, window->height);
640
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400641 weston_wm_window_get_child_position(window, &x, &y);
642 values[i++] = x;
643 values[i++] = y;
644 values[i++] = window->width;
645 values[i++] = window->height;
646 values[i++] = 0;
647 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
648 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
649 XCB_CONFIG_WINDOW_BORDER_WIDTH;
650 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
651 values[i++] = configure_request->sibling;
652 mask |= XCB_CONFIG_WINDOW_SIBLING;
653 }
654 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
655 values[i++] = configure_request->stack_mode;
656 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
657 }
658
659 xcb_configure_window(wm->conn, window->id, mask, values);
660
661 weston_wm_window_get_frame_size(window, &width, &height);
662 values[0] = width;
663 values[1] = height;
664 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
665 xcb_configure_window(wm->conn, window->frame_id, mask, values);
666
667 weston_wm_window_schedule_repaint(window);
668}
669
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400670static int
671our_resource(struct weston_wm *wm, uint32_t id)
672{
673 const xcb_setup_t *setup;
674
675 setup = xcb_get_setup(wm->conn);
676
677 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
678}
679
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400680static void
681weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
682{
Murray Calavera883ac022015-06-06 13:02:22 +0000683 xcb_configure_notify_event_t *configure_notify =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400684 (xcb_configure_notify_event_t *) event;
685 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400686
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400687 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400688 configure_notify->window,
689 configure_notify->x, configure_notify->y,
690 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300691
Derek Foreman49372142015-04-09 10:51:22 -0500692 if (!wm_lookup_window(wm, configure_notify->window, &window))
693 return;
694
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700695 window->x = configure_notify->x;
696 window->y = configure_notify->y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200697 window->pos_dirty = false;
698
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700699 if (window->override_redirect) {
700 window->width = configure_notify->width;
701 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500702 if (window->frame)
703 frame_resize_inside(window->frame,
704 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700705 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400706}
707
708static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300709weston_wm_kill_client(struct wl_listener *listener, void *data)
710{
711 struct weston_surface *surface = data;
712 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300713 if (!window)
714 return;
715
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200716 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300717 kill(window->pid, SIGKILL);
718}
719
720static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700721weston_wm_create_surface(struct wl_listener *listener, void *data)
722{
723 struct weston_surface *surface = data;
724 struct weston_wm *wm =
725 container_of(listener,
726 struct weston_wm, create_surface_listener);
727 struct weston_wm_window *window;
728
729 if (wl_resource_get_client(surface->resource) != wm->server->client)
730 return;
731
732 wl_list_for_each(window, &wm->unpaired_window_list, link)
733 if (window->surface_id ==
734 wl_resource_get_id(surface->resource)) {
735 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700736 window->surface_id = 0;
737 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700738 break;
Murray Calavera883ac022015-06-06 13:02:22 +0000739 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700740}
741
742static void
Giulio Camuffob18f7882015-03-29 14:20:23 +0300743weston_wm_send_focus_window(struct weston_wm *wm,
744 struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400745{
Scott Moreau85ecac02012-05-21 15:49:14 -0600746 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400747
Scott Moreau85ecac02012-05-21 15:49:14 -0600748 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700749 uint32_t values[1];
750
Boyan Dingb9f863c2014-08-30 10:33:23 +0800751 if (window->override_redirect)
752 return;
753
Scott Moreau85ecac02012-05-21 15:49:14 -0600754 client_message.response_type = XCB_CLIENT_MESSAGE;
755 client_message.format = 32;
756 client_message.window = window->id;
757 client_message.type = wm->atom.wm_protocols;
758 client_message.data.data32[0] = wm->atom.wm_take_focus;
759 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
760
Murray Calavera883ac022015-06-06 13:02:22 +0000761 xcb_send_event(wm->conn, 0, window->id,
Scott Moreau85ecac02012-05-21 15:49:14 -0600762 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
763 (char *) &client_message);
764
765 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
766 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700767
768 values[0] = XCB_STACK_MODE_ABOVE;
769 xcb_configure_window (wm->conn, window->frame_id,
770 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600771 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400772 xcb_set_input_focus (wm->conn,
773 XCB_INPUT_FOCUS_POINTER_ROOT,
774 XCB_NONE,
775 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600776 }
Giulio Camuffob18f7882015-03-29 14:20:23 +0300777}
778
779static void
780weston_wm_window_activate(struct wl_listener *listener, void *data)
781{
Jonas Ådahlf7deb6a2016-07-22 17:50:26 +0800782 struct weston_surface_activation_data *activation_data = data;
783 struct weston_surface *surface = activation_data->surface;
Giulio Camuffob18f7882015-03-29 14:20:23 +0300784 struct weston_wm_window *window = NULL;
785 struct weston_wm *wm =
786 container_of(listener, struct weston_wm, activate_listener);
787
788 if (surface) {
789 window = get_wm_window(surface);
790 }
791
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200792 if (window) {
793 weston_wm_set_net_active_window(wm, window->id);
794 } else {
795 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
796 }
797
Giulio Camuffob18f7882015-03-29 14:20:23 +0300798 weston_wm_send_focus_window(wm, window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400799
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500800 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800801 if (wm->focus_window->frame)
802 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400803 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500804 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400805 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500806 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800807 if (wm->focus_window->frame)
808 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400809 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500810 }
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200811
812 xcb_flush(wm->conn);
813
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400814}
815
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400816#define ICCCM_WITHDRAWN_STATE 0
817#define ICCCM_NORMAL_STATE 1
818#define ICCCM_ICONIC_STATE 3
819
820static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500821weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400822{
823 struct weston_wm *wm = window->wm;
824 uint32_t property[2];
825
826 property[0] = state;
827 property[1] = XCB_WINDOW_NONE;
828
829 xcb_change_property(wm->conn,
830 XCB_PROP_MODE_REPLACE,
831 window->id,
832 wm->atom.wm_state,
833 wm->atom.wm_state,
834 32, /* format */
835 2, property);
836}
837
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400838static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500839weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
840{
841 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200842 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500843 int i;
844
845 i = 0;
846 if (window->fullscreen)
847 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200848 if (window->maximized_vert)
849 property[i++] = wm->atom.net_wm_state_maximized_vert;
850 if (window->maximized_horz)
851 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500852
853 xcb_change_property(wm->conn,
854 XCB_PROP_MODE_REPLACE,
855 window->id,
856 wm->atom.net_wm_state,
857 XCB_ATOM_ATOM,
858 32, /* format */
859 i, property);
860}
861
862static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700863weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400864{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700865 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400866 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400867 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200868 int buttons = FRAME_BUTTON_CLOSE;
869
870 if (window->decorate & MWM_DECOR_MAXIMIZE)
871 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400872
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500873 window->frame = frame_create(window->wm->theme,
874 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200875 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500876 frame_resize_inside(window->frame, window->width, window->height);
877
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400878 weston_wm_window_get_frame_size(window, &width, &height);
879 weston_wm_window_get_child_position(window, &x, &y);
880
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400881 values[0] = wm->screen->black_pixel;
882 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400883 XCB_EVENT_MASK_KEY_PRESS |
884 XCB_EVENT_MASK_KEY_RELEASE |
885 XCB_EVENT_MASK_BUTTON_PRESS |
886 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400887 XCB_EVENT_MASK_POINTER_MOTION |
888 XCB_EVENT_MASK_ENTER_WINDOW |
889 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400890 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400891 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400892 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400893
894 window->frame_id = xcb_generate_id(wm->conn);
895 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400896 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400897 window->frame_id,
898 wm->screen->root,
899 0, 0,
900 width, height,
901 0,
902 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400903 wm->visual_id,
904 XCB_CW_BORDER_PIXEL |
905 XCB_CW_EVENT_MASK |
906 XCB_CW_COLORMAP, values);
907
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400908 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
909
910 values[0] = 0;
911 xcb_configure_window(wm->conn, window->id,
912 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
913
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400914 window->cairo_surface =
915 cairo_xcb_surface_create_with_xrender_format(wm->conn,
916 wm->screen,
917 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400918 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400919 width, height);
920
921 hash_table_insert(wm->window_hash, window->frame_id, window);
922}
923
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200924/*
925 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
926 * Passing a <0 desktop value deletes the property.
927 */
928static void
929weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
Bryce Harringtone00554b2015-06-12 10:17:39 -0700930 int desktop)
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200931{
932 if (desktop >= 0) {
933 xcb_change_property(window->wm->conn,
934 XCB_PROP_MODE_REPLACE,
935 window->id,
936 window->wm->atom.net_wm_desktop,
937 XCB_ATOM_CARDINAL,
938 32, /* format */
939 1, &desktop);
940 } else {
941 xcb_delete_property(window->wm->conn,
942 window->id,
943 window->wm->atom.net_wm_desktop);
944 }
945}
946
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400947static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700948weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
949{
950 xcb_map_request_event_t *map_request =
951 (xcb_map_request_event_t *) event;
952 struct weston_wm_window *window;
953
954 if (our_resource(wm, map_request->window)) {
955 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
956 map_request->window);
957 return;
958 }
959
Derek Foreman49372142015-04-09 10:51:22 -0500960 if (!wm_lookup_window(wm, map_request->window, &window))
961 return;
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700962
963 weston_wm_window_read_properties(window);
964
965 if (window->frame_id == XCB_WINDOW_NONE)
966 weston_wm_window_create_frame(window);
967
968 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
969 window->id, window, window->frame_id);
970
971 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
972 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200973 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700974
975 xcb_map_window(wm->conn, map_request->window);
976 xcb_map_window(wm->conn, window->frame_id);
977}
978
979static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400980weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
981{
982 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
983
984 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400985 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
986 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400987 return;
988 }
989
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400990 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400991}
992
993static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400994weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
995{
996 xcb_unmap_notify_event_t *unmap_notify =
997 (xcb_unmap_notify_event_t *) event;
998 struct weston_wm_window *window;
999
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001000 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
1001 unmap_notify->window,
1002 unmap_notify->event,
1003 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001004
1005 if (our_resource(wm, unmap_notify->window))
1006 return;
1007
MoD31700122013-06-11 19:58:55 -05001008 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -04001009 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1010 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001011 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001012
Derek Foreman49372142015-04-09 10:51:22 -05001013 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1014 return;
1015
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001016 if (window->surface_id) {
1017 /* Make sure we're not on the unpaired surface list or we
1018 * could be assigned a surface during surface creation that
1019 * was mapped before this unmap request.
1020 */
1021 wl_list_remove(&window->link);
1022 window->surface_id = 0;
1023 }
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001024 if (wm->focus_window == window)
1025 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001026 if (window->surface)
1027 wl_list_remove(&window->surface_destroy_listener.link);
1028 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +02001029 window->shsurf = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001030
1031 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1032 weston_wm_window_set_virtual_desktop(window, -1);
1033
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001034 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001035}
1036
1037static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001038weston_wm_window_draw_decoration(void *data)
1039{
1040 struct weston_wm_window *window = data;
1041 struct weston_wm *wm = window->wm;
1042 struct theme *t = wm->theme;
1043 cairo_t *cr;
1044 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001045 int32_t input_x, input_y, input_w, input_h;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07001046 struct weston_shell_interface *shell_interface =
1047 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001048 uint32_t flags = 0;
Giulio Camuffoaa974782015-02-01 16:18:51 +02001049 struct weston_view *view;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001050
1051 weston_wm_window_read_properties(window);
1052
1053 window->repaint_source = NULL;
1054
1055 weston_wm_window_get_frame_size(window, &width, &height);
1056 weston_wm_window_get_child_position(window, &x, &y);
1057
1058 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1059 cr = cairo_create(window->cairo_surface);
1060
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001061 if (window->fullscreen) {
1062 /* nothing */
1063 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001064 if (wm->focus_window == window)
1065 flags |= THEME_FRAME_ACTIVE;
1066
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001067 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001068 } else {
1069 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1070 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1071 cairo_paint(cr);
1072
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001073 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001074 }
1075
1076 cairo_destroy(cr);
1077
1078 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001079 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001080 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001081 pixman_region32_init(&window->surface->pending.opaque);
1082 } else {
1083 /* We leave an extra pixel around the X window area to
1084 * make sure we don't sample from the undefined alpha
1085 * channel when filtering. */
Murray Calavera883ac022015-06-06 13:02:22 +00001086 pixman_region32_init_rect(&window->surface->pending.opaque,
MoD384a11a2013-06-22 11:04:21 -05001087 x - 1, y - 1,
1088 window->width + 2,
1089 window->height + 2);
1090 }
Giulio Camuffoaa974782015-02-01 16:18:51 +02001091 wl_list_for_each(view, &window->surface->views, surface_link)
1092 weston_view_geometry_dirty(view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001093
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001094 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001095
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001096 if (window->decorate && !window->fullscreen) {
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001097 frame_input_rect(window->frame, &input_x, &input_y,
1098 &input_w, &input_h);
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001099 } else {
1100 input_x = x;
1101 input_y = y;
1102 input_w = width;
1103 input_h = height;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001104 }
1105
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001106 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001107 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001108
1109 shell_interface->set_window_geometry(window->shsurf,
1110 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001111 }
1112}
1113
1114static void
1115weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1116{
1117 struct weston_wm *wm = window->wm;
Giulio Camuffoaa974782015-02-01 16:18:51 +02001118 struct weston_view *view;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001119 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001120
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001121 if (window->frame_id == XCB_WINDOW_NONE) {
1122 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001123 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001124 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001125 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001126 pixman_region32_init(&window->surface->pending.opaque);
1127 } else {
1128 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1129 width, height);
1130 }
Giulio Camuffoaa974782015-02-01 16:18:51 +02001131 wl_list_for_each(view, &window->surface->views, surface_link)
1132 weston_view_geometry_dirty(view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001133 }
1134 return;
1135 }
1136
1137 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001138 return;
1139
1140 window->repaint_source =
1141 wl_event_loop_add_idle(wm->server->loop,
1142 weston_wm_window_draw_decoration,
1143 window);
1144}
1145
1146static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001147weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1148{
1149 xcb_property_notify_event_t *property_notify =
1150 (xcb_property_notify_event_t *) event;
1151 struct weston_wm_window *window;
1152
Derek Foreman49372142015-04-09 10:51:22 -05001153 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001154 return;
1155
1156 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001157
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001158 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001159 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001160 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001161 else
1162 read_and_dump_property(wm, property_notify->window,
1163 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001164
1165 if (property_notify->atom == wm->atom.net_wm_name ||
1166 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001167 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001168}
1169
1170static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001171weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001172 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001173{
1174 struct weston_wm_window *window;
1175 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001176 xcb_get_geometry_cookie_t geometry_cookie;
1177 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001178
Peter Huttererf3d62272013-08-08 11:57:05 +10001179 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001180 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001181 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001182 return;
1183 }
1184
MoD384a11a2013-06-22 11:04:21 -05001185 geometry_cookie = xcb_get_geometry(wm->conn, id);
1186
Giulio Camuffob18f7882015-03-29 14:20:23 +03001187 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
1188 XCB_EVENT_MASK_FOCUS_CHANGE;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001189 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1190
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001191 window->wm = wm;
1192 window->id = id;
1193 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001194 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001195 window->width = width;
1196 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001197 window->x = x;
1198 window->y = y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001199 window->pos_dirty = false;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001200
MoD384a11a2013-06-22 11:04:21 -05001201 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1202 /* technically we should use XRender and check the visual format's
1203 alpha_mask, but checking depth is simpler and works in all known cases */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001204 if (geometry_reply != NULL)
MoD384a11a2013-06-22 11:04:21 -05001205 window->has_alpha = geometry_reply->depth == 32;
1206 free(geometry_reply);
1207
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001208 hash_table_insert(wm->window_hash, id, window);
1209}
1210
1211static void
1212weston_wm_window_destroy(struct weston_wm_window *window)
1213{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001214 struct weston_wm *wm = window->wm;
1215
1216 if (window->repaint_source)
1217 wl_event_source_remove(window->repaint_source);
1218 if (window->cairo_surface)
1219 cairo_surface_destroy(window->cairo_surface);
1220
1221 if (window->frame_id) {
1222 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1223 xcb_destroy_window(wm->conn, window->frame_id);
1224 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001225 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001226 hash_table_remove(wm->window_hash, window->frame_id);
1227 window->frame_id = XCB_WINDOW_NONE;
1228 }
1229
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001230 if (window->surface_id)
1231 wl_list_remove(&window->link);
1232
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001233 if (window->surface)
1234 wl_list_remove(&window->surface_destroy_listener.link);
1235
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001236 hash_table_remove(window->wm->window_hash, window->id);
1237 free(window);
1238}
1239
1240static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001241weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1242{
1243 xcb_create_notify_event_t *create_notify =
1244 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001245
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001246 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1247 create_notify->window,
1248 create_notify->width, create_notify->height,
1249 create_notify->override_redirect ? ", override" : "",
1250 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001251
1252 if (our_resource(wm, create_notify->window))
1253 return;
1254
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001255 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001256 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001257 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001258 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001259}
1260
1261static void
1262weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1263{
1264 xcb_destroy_notify_event_t *destroy_notify =
1265 (xcb_destroy_notify_event_t *) event;
1266 struct weston_wm_window *window;
1267
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001268 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1269 destroy_notify->window,
1270 destroy_notify->event,
1271 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001272
1273 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001274 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001275
Derek Foreman49372142015-04-09 10:51:22 -05001276 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1277 return;
1278
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001279 weston_wm_window_destroy(window);
1280}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001281
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001282static void
1283weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1284{
1285 xcb_reparent_notify_event_t *reparent_notify =
1286 (xcb_reparent_notify_event_t *) event;
1287 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001288
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001289 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1290 reparent_notify->window,
1291 reparent_notify->parent,
1292 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001293
1294 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001295 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001296 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001297 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001298 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001299 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1300 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001301 weston_wm_window_destroy(window);
1302 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001303}
1304
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001305struct weston_seat *
1306weston_wm_pick_seat(struct weston_wm *wm)
1307{
1308 return container_of(wm->server->compositor->seat_list.next,
1309 struct weston_seat, link);
1310}
1311
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001312static struct weston_seat *
1313weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1314{
1315 struct weston_wm *wm = window->wm;
1316 struct weston_seat *seat, *s;
1317
1318 seat = NULL;
1319 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001320 struct weston_pointer *pointer = weston_seat_get_pointer(s);
1321 struct weston_pointer *old_pointer =
1322 weston_seat_get_pointer(seat);
1323
1324 if (pointer && pointer->focus &&
1325 pointer->focus->surface == window->surface &&
1326 pointer->button_count > 0 &&
1327 (!seat ||
1328 pointer->grab_serial -
1329 old_pointer->grab_serial < (1 << 30)))
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001330 seat = s;
1331 }
1332
1333 return seat;
1334}
1335
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001336static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001337weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1338 xcb_client_message_event_t *client_message)
1339{
1340 static const int map[] = {
1341 THEME_LOCATION_RESIZING_TOP_LEFT,
1342 THEME_LOCATION_RESIZING_TOP,
1343 THEME_LOCATION_RESIZING_TOP_RIGHT,
1344 THEME_LOCATION_RESIZING_RIGHT,
1345 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1346 THEME_LOCATION_RESIZING_BOTTOM,
1347 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1348 THEME_LOCATION_RESIZING_LEFT
1349 };
1350
1351 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001352 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Derek Foreman1281a362015-07-31 16:55:32 -05001353 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001354 int detail;
1355 struct weston_shell_interface *shell_interface =
1356 &wm->server->compositor->shell_interface;
1357
Derek Foreman1281a362015-07-31 16:55:32 -05001358 if (!pointer || pointer->button_count != 1
1359 || !pointer->focus
1360 || pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001361 return;
1362
1363 detail = client_message->data.data32[2];
1364 switch (detail) {
1365 case _NET_WM_MOVERESIZE_MOVE:
Derek Foremane4d6c832015-08-05 14:48:11 -05001366 shell_interface->move(window->shsurf, pointer);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001367 break;
1368 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1369 case _NET_WM_MOVERESIZE_SIZE_TOP:
1370 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1371 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1372 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1373 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1374 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1375 case _NET_WM_MOVERESIZE_SIZE_LEFT:
Derek Foremane4d6c832015-08-05 14:48:11 -05001376 shell_interface->resize(window->shsurf, pointer, map[detail]);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001377 break;
1378 case _NET_WM_MOVERESIZE_CANCEL:
1379 break;
1380 }
1381}
1382
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001383#define _NET_WM_STATE_REMOVE 0
1384#define _NET_WM_STATE_ADD 1
1385#define _NET_WM_STATE_TOGGLE 2
1386
1387static int
1388update_state(int action, int *state)
1389{
1390 int new_state, changed;
1391
1392 switch (action) {
1393 case _NET_WM_STATE_REMOVE:
1394 new_state = 0;
1395 break;
1396 case _NET_WM_STATE_ADD:
1397 new_state = 1;
1398 break;
1399 case _NET_WM_STATE_TOGGLE:
1400 new_state = !*state;
1401 break;
1402 default:
1403 return 0;
1404 }
1405
1406 changed = (*state != new_state);
1407 *state = new_state;
1408
1409 return changed;
1410}
1411
1412static void
1413weston_wm_window_configure(void *data);
1414
1415static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001416weston_wm_window_set_toplevel(struct weston_wm_window *window)
1417{
1418 struct weston_shell_interface *shell_interface =
1419 &window->wm->server->compositor->shell_interface;
1420
1421 shell_interface->set_toplevel(window->shsurf);
1422 window->width = window->saved_width;
1423 window->height = window->saved_height;
1424 if (window->frame)
1425 frame_resize_inside(window->frame,
1426 window->width,
1427 window->height);
1428 weston_wm_window_configure(window);
1429}
1430
1431static inline bool
1432weston_wm_window_is_maximized(struct weston_wm_window *window)
1433{
1434 return window->maximized_horz && window->maximized_vert;
1435}
1436
1437static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001438weston_wm_window_handle_state(struct weston_wm_window *window,
1439 xcb_client_message_event_t *client_message)
1440{
1441 struct weston_wm *wm = window->wm;
1442 struct weston_shell_interface *shell_interface =
1443 &wm->server->compositor->shell_interface;
1444 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001445 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001446
1447 action = client_message->data.data32[0];
1448 property = client_message->data.data32[1];
1449
1450 if (property == wm->atom.net_wm_state_fullscreen &&
1451 update_state(action, &window->fullscreen)) {
1452 weston_wm_window_set_net_wm_state(window);
1453 if (window->fullscreen) {
1454 window->saved_width = window->width;
1455 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001456
1457 if (window->shsurf)
1458 shell_interface->set_fullscreen(window->shsurf,
1459 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1460 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001461 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001462 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001463 weston_wm_window_set_toplevel(window);
1464 }
1465 } else {
1466 if (property == wm->atom.net_wm_state_maximized_vert &&
1467 update_state(action, &window->maximized_vert))
1468 weston_wm_window_set_net_wm_state(window);
1469 if (property == wm->atom.net_wm_state_maximized_horz &&
1470 update_state(action, &window->maximized_horz))
1471 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001472
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001473 if (maximized != weston_wm_window_is_maximized(window)) {
1474 if (weston_wm_window_is_maximized(window)) {
1475 window->saved_width = window->width;
1476 window->saved_height = window->height;
1477
1478 if (window->shsurf)
1479 shell_interface->set_maximized(window->shsurf);
1480 } else if (window->shsurf) {
1481 weston_wm_window_set_toplevel(window);
1482 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001483 }
1484 }
1485}
1486
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001487static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001488surface_destroy(struct wl_listener *listener, void *data)
1489{
1490 struct weston_wm_window *window =
1491 container_of(listener,
1492 struct weston_wm_window, surface_destroy_listener);
1493
1494 wm_log("surface for xid %d destroyed\n", window->id);
1495
1496 /* This should have been freed by the shell.
1497 * Don't try to use it later. */
1498 window->shsurf = NULL;
1499 window->surface = NULL;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001500}
1501
1502static void
1503weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1504 xcb_client_message_event_t *client_message)
1505{
1506 struct weston_wm *wm = window->wm;
1507 struct wl_resource *resource;
1508
1509 if (window->surface_id != 0) {
1510 wm_log("already have surface id for window %d\n", window->id);
1511 return;
1512 }
1513
1514 /* Xwayland will send the wayland requests to create the
1515 * wl_surface before sending this client message. Even so, we
1516 * can end up handling the X event before the wayland requests
1517 * and thus when we try to look up the surface ID, the surface
1518 * hasn't been created yet. In that case put the window on
1519 * the unpaired window list and continue when the surface gets
1520 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001521 uint32_t id = client_message->data.data32[0];
1522 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001523 if (resource) {
1524 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001525 xserver_map_shell_surface(window,
1526 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001527 }
1528 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001529 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001530 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001531 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001532}
1533
1534static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001535weston_wm_handle_client_message(struct weston_wm *wm,
1536 xcb_generic_event_t *event)
1537{
1538 xcb_client_message_event_t *client_message =
1539 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001540 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001541
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001542 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1543 get_atom_name(wm->conn, client_message->type),
1544 client_message->data.data32[0],
1545 client_message->data.data32[1],
1546 client_message->data.data32[2],
1547 client_message->data.data32[3],
1548 client_message->data.data32[4],
1549 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001550
Jason Ekstrand0250a742014-07-24 13:17:47 -07001551 /* The window may get created and destroyed before we actually
1552 * handle the message. If it doesn't exist, bail.
1553 */
Derek Foreman49372142015-04-09 10:51:22 -05001554 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001555 return;
1556
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001557 if (client_message->type == wm->atom.net_wm_moveresize)
1558 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001559 else if (client_message->type == wm->atom.net_wm_state)
1560 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001561 else if (client_message->type == wm->atom.wl_surface_id)
1562 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001563}
1564
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001565enum cursor_type {
1566 XWM_CURSOR_TOP,
1567 XWM_CURSOR_BOTTOM,
1568 XWM_CURSOR_LEFT,
1569 XWM_CURSOR_RIGHT,
1570 XWM_CURSOR_TOP_LEFT,
1571 XWM_CURSOR_TOP_RIGHT,
1572 XWM_CURSOR_BOTTOM_LEFT,
1573 XWM_CURSOR_BOTTOM_RIGHT,
1574 XWM_CURSOR_LEFT_PTR,
1575};
1576
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001577/*
1578 * The following correspondences between file names and cursors was copied
1579 * from: https://bugs.kde.org/attachment.cgi?id=67313
1580 */
1581
1582static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001583 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001584 "sw-resize",
1585 "size_bdiag"
1586};
1587
1588static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001589 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001590 "se-resize",
1591 "size_fdiag"
1592};
1593
1594static const char *bottom_sides[] = {
1595 "bottom_side",
1596 "s-resize",
1597 "size_ver"
1598};
1599
1600static const char *left_ptrs[] = {
1601 "left_ptr",
1602 "default",
1603 "top_left_arrow",
1604 "left-arrow"
1605};
1606
1607static const char *left_sides[] = {
1608 "left_side",
1609 "w-resize",
1610 "size_hor"
1611};
1612
1613static const char *right_sides[] = {
1614 "right_side",
1615 "e-resize",
1616 "size_hor"
1617};
1618
1619static const char *top_left_corners[] = {
1620 "top_left_corner",
1621 "nw-resize",
1622 "size_fdiag"
1623};
1624
1625static const char *top_right_corners[] = {
1626 "top_right_corner",
1627 "ne-resize",
1628 "size_bdiag"
1629};
1630
1631static const char *top_sides[] = {
1632 "top_side",
1633 "n-resize",
1634 "size_ver"
1635};
1636
1637struct cursor_alternatives {
1638 const char **names;
1639 size_t count;
1640};
1641
1642static const struct cursor_alternatives cursors[] = {
1643 {top_sides, ARRAY_LENGTH(top_sides)},
1644 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1645 {left_sides, ARRAY_LENGTH(left_sides)},
1646 {right_sides, ARRAY_LENGTH(right_sides)},
1647 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1648 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1649 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1650 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1651 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001652};
1653
1654static void
1655weston_wm_create_cursors(struct weston_wm *wm)
1656{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001657 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001658 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001659 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001660
1661 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1662 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001663 for (j = 0; j < cursors[i].count; j++) {
1664 name = cursors[i].names[j];
1665 wm->cursors[i] =
1666 xcb_cursor_library_load_cursor(wm, name);
1667 if (wm->cursors[i] != (xcb_cursor_t)-1)
1668 break;
1669 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001670 }
1671
1672 wm->last_cursor = -1;
1673}
1674
1675static void
1676weston_wm_destroy_cursors(struct weston_wm *wm)
1677{
1678 uint8_t i;
1679
1680 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1681 xcb_free_cursor(wm->conn, wm->cursors[i]);
1682
1683 free(wm->cursors);
1684}
1685
1686static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001687get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001688{
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001689 switch (location) {
1690 case THEME_LOCATION_RESIZING_TOP:
1691 return XWM_CURSOR_TOP;
1692 case THEME_LOCATION_RESIZING_BOTTOM:
1693 return XWM_CURSOR_BOTTOM;
1694 case THEME_LOCATION_RESIZING_LEFT:
1695 return XWM_CURSOR_LEFT;
1696 case THEME_LOCATION_RESIZING_RIGHT:
1697 return XWM_CURSOR_RIGHT;
1698 case THEME_LOCATION_RESIZING_TOP_LEFT:
1699 return XWM_CURSOR_TOP_LEFT;
1700 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1701 return XWM_CURSOR_TOP_RIGHT;
1702 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1703 return XWM_CURSOR_BOTTOM_LEFT;
1704 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1705 return XWM_CURSOR_BOTTOM_RIGHT;
1706 case THEME_LOCATION_EXTERIOR:
1707 case THEME_LOCATION_TITLEBAR:
1708 default:
1709 return XWM_CURSOR_LEFT_PTR;
1710 }
1711}
1712
1713static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001714weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1715 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001716{
1717 uint32_t cursor_value_list;
1718
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001719 if (wm->last_cursor == cursor)
1720 return;
1721
1722 wm->last_cursor = cursor;
1723
1724 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001725 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001726 XCB_CW_CURSOR, &cursor_value_list);
1727 xcb_flush(wm->conn);
1728}
1729
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001730static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001731weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001732{
1733 xcb_client_message_event_t client_message;
1734
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001735 if (window->delete_window) {
1736 client_message.response_type = XCB_CLIENT_MESSAGE;
1737 client_message.format = 32;
1738 client_message.window = window->id;
1739 client_message.type = window->wm->atom.wm_protocols;
1740 client_message.data.data32[0] =
1741 window->wm->atom.wm_delete_window;
1742 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001743
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001744 xcb_send_event(window->wm->conn, 0, window->id,
1745 XCB_EVENT_MASK_NO_EVENT,
1746 (char *) &client_message);
1747 } else {
1748 xcb_kill_client(window->wm->conn, window->id);
1749 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001750}
1751
1752static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001753weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1754{
1755 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1756 struct weston_shell_interface *shell_interface =
1757 &wm->server->compositor->shell_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001758 struct weston_seat *seat;
Derek Foremane4d6c832015-08-05 14:48:11 -05001759 struct weston_pointer *pointer;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001760 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001761 enum theme_location location;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001762 enum frame_button_state button_state;
1763 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001764
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001765 wm_log("XCB_BUTTON_%s (detail %d)\n",
1766 button->response_type == XCB_BUTTON_PRESS ?
1767 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001768
Derek Foreman49372142015-04-09 10:51:22 -05001769 if (!wm_lookup_window(wm, button->event, &window) ||
1770 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001771 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001772
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001773 if (button->detail != 1 && button->detail != 2)
1774 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001775
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001776 seat = weston_wm_pick_seat_for_window(window);
Derek Foremane4d6c832015-08-05 14:48:11 -05001777 pointer = weston_seat_get_pointer(seat);
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001778
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001779 button_state = button->response_type == XCB_BUTTON_PRESS ?
1780 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1781 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1782
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001783 /* Make sure we're looking at the right location. The frame
1784 * could have received a motion event from a pointer from a
1785 * different wl_seat, but under X it looks like our core
1786 * pointer moved. Move the frame pointer to the button press
1787 * location before deciding what to do. */
1788 location = frame_pointer_motion(window->frame, NULL,
1789 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001790 location = frame_pointer_button(window->frame, NULL,
1791 button_id, button_state);
1792 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1793 weston_wm_window_schedule_repaint(window);
1794
1795 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001796 if (pointer)
1797 shell_interface->move(window->shsurf, pointer);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001798 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1799 }
1800
1801 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001802 if (pointer)
1803 shell_interface->resize(window->shsurf, pointer, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001804 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1805 }
1806
1807 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001808 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001809 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001810 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001811
1812 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1813 window->maximized_horz = !window->maximized_horz;
1814 window->maximized_vert = !window->maximized_vert;
1815 if (weston_wm_window_is_maximized(window)) {
1816 window->saved_width = window->width;
1817 window->saved_height = window->height;
1818 shell_interface->set_maximized(window->shsurf);
1819 } else {
1820 weston_wm_window_set_toplevel(window);
1821 }
1822 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1823 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001824}
1825
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001826static void
1827weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1828{
1829 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1830 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001831 enum theme_location location;
1832 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001833
Derek Foreman49372142015-04-09 10:51:22 -05001834 if (!wm_lookup_window(wm, motion->event, &window) ||
1835 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001836 return;
1837
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001838 location = frame_pointer_motion(window->frame, NULL,
1839 motion->event_x, motion->event_y);
1840 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1841 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001842
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001843 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001844 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001845}
1846
1847static void
1848weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1849{
1850 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1851 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001852 enum theme_location location;
1853 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001854
Derek Foreman49372142015-04-09 10:51:22 -05001855 if (!wm_lookup_window(wm, enter->event, &window) ||
1856 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001857 return;
1858
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001859 location = frame_pointer_enter(window->frame, NULL,
1860 enter->event_x, enter->event_y);
1861 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1862 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001863
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001864 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001865 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001866}
1867
1868static void
1869weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1870{
1871 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1872 struct weston_wm_window *window;
1873
Derek Foreman49372142015-04-09 10:51:22 -05001874 if (!wm_lookup_window(wm, leave->event, &window) ||
1875 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001876 return;
1877
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001878 frame_pointer_leave(window->frame, NULL);
1879 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1880 weston_wm_window_schedule_repaint(window);
1881
Tiago Vignattic1903232012-07-16 12:15:37 -04001882 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001883}
1884
Giulio Camuffob18f7882015-03-29 14:20:23 +03001885static void
1886weston_wm_handle_focus_in(struct weston_wm *wm, xcb_generic_event_t *event)
1887{
1888 xcb_focus_in_event_t *focus = (xcb_focus_in_event_t *) event;
1889 /* Do not let X clients change the focus behind the compositor's
1890 * back. Reset the focus to the old one if it changed. */
1891 if (!wm->focus_window || focus->event != wm->focus_window->id)
1892 weston_wm_send_focus_window(wm, wm->focus_window);
1893}
1894
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001895static int
1896weston_wm_handle_event(int fd, uint32_t mask, void *data)
1897{
1898 struct weston_wm *wm = data;
1899 xcb_generic_event_t *event;
1900 int count = 0;
1901
1902 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1903 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001904 free(event);
1905 count++;
1906 continue;
1907 }
1908
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001909 if (weston_wm_handle_dnd_event(wm, event)) {
1910 free(event);
1911 count++;
1912 continue;
1913 }
1914
MoD31700122013-06-11 19:58:55 -05001915 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001916 case XCB_BUTTON_PRESS:
1917 case XCB_BUTTON_RELEASE:
1918 weston_wm_handle_button(wm, event);
1919 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001920 case XCB_ENTER_NOTIFY:
1921 weston_wm_handle_enter(wm, event);
1922 break;
1923 case XCB_LEAVE_NOTIFY:
1924 weston_wm_handle_leave(wm, event);
1925 break;
1926 case XCB_MOTION_NOTIFY:
1927 weston_wm_handle_motion(wm, event);
1928 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001929 case XCB_CREATE_NOTIFY:
1930 weston_wm_handle_create_notify(wm, event);
1931 break;
1932 case XCB_MAP_REQUEST:
1933 weston_wm_handle_map_request(wm, event);
1934 break;
1935 case XCB_MAP_NOTIFY:
1936 weston_wm_handle_map_notify(wm, event);
1937 break;
1938 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001939 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001940 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001941 case XCB_REPARENT_NOTIFY:
1942 weston_wm_handle_reparent_notify(wm, event);
1943 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001944 case XCB_CONFIGURE_REQUEST:
1945 weston_wm_handle_configure_request(wm, event);
1946 break;
1947 case XCB_CONFIGURE_NOTIFY:
1948 weston_wm_handle_configure_notify(wm, event);
1949 break;
1950 case XCB_DESTROY_NOTIFY:
1951 weston_wm_handle_destroy_notify(wm, event);
1952 break;
1953 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001954 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001955 break;
1956 case XCB_PROPERTY_NOTIFY:
1957 weston_wm_handle_property_notify(wm, event);
1958 break;
1959 case XCB_CLIENT_MESSAGE:
1960 weston_wm_handle_client_message(wm, event);
1961 break;
Giulio Camuffob18f7882015-03-29 14:20:23 +03001962 case XCB_FOCUS_IN:
1963 weston_wm_handle_focus_in(wm, event);
1964 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001965 }
1966
1967 free(event);
1968 count++;
1969 }
1970
Marek Chalupaa1f3f3c2015-08-12 09:55:12 +02001971 if (count != 0)
1972 xcb_flush(wm->conn);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001973
1974 return count;
1975}
1976
1977static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02001978weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window) {
1979 xcb_change_property(wm->conn, XCB_PROP_MODE_REPLACE,
1980 wm->screen->root, wm->atom.net_active_window,
1981 wm->atom.window, 32, 1, &window);
1982}
1983
1984static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001985weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1986{
1987 xcb_depth_iterator_t d_iter;
1988 xcb_visualtype_iterator_t vt_iter;
1989 xcb_visualtype_t *visualtype;
1990
1991 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1992 visualtype = NULL;
1993 while (d_iter.rem > 0) {
1994 if (d_iter.data->depth == 32) {
1995 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1996 visualtype = vt_iter.data;
1997 break;
1998 }
1999
2000 xcb_depth_next(&d_iter);
2001 }
2002
2003 if (visualtype == NULL) {
2004 weston_log("no 32 bit visualtype\n");
2005 return;
2006 }
2007
2008 wm->visual_id = visualtype->visual_id;
2009 wm->colormap = xcb_generate_id(wm->conn);
2010 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
2011 wm->colormap, wm->screen->root, wm->visual_id);
2012}
2013
2014static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002015weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002016{
2017
2018#define F(field) offsetof(struct weston_wm, field)
2019
2020 static const struct { const char *name; int offset; } atoms[] = {
2021 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002022 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002023 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
2024 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04002025 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002026 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002027 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002028 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002029 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002030 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002031 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2032 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002033 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2034 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002035 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2036 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2037 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02002038 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002039 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2040
2041 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2042 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2043 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2044 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2045 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2046 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2047 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002048 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2049 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002050 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2051 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2052 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2053 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2054 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2055
2056 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2057 { "_NET_SUPPORTING_WM_CHECK",
2058 F(atom.net_supporting_wm_check) },
2059 { "_NET_SUPPORTED", F(atom.net_supported) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002060 { "_NET_ACTIVE_WINDOW", F(atom.net_active_window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002061 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2062 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002063 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002064 { "TARGETS", F(atom.targets) },
2065 { "UTF8_STRING", F(atom.utf8_string) },
2066 { "_WL_SELECTION", F(atom.wl_selection) },
2067 { "INCR", F(atom.incr) },
2068 { "TIMESTAMP", F(atom.timestamp) },
2069 { "MULTIPLE", F(atom.multiple) },
2070 { "UTF8_STRING" , F(atom.utf8_string) },
2071 { "COMPOUND_TEXT", F(atom.compound_text) },
2072 { "TEXT", F(atom.text) },
2073 { "STRING", F(atom.string) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002074 { "WINDOW", F(atom.window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002075 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2076 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002077 { "XdndSelection", F(atom.xdnd_selection) },
2078 { "XdndAware", F(atom.xdnd_aware) },
2079 { "XdndEnter", F(atom.xdnd_enter) },
2080 { "XdndLeave", F(atom.xdnd_leave) },
2081 { "XdndDrop", F(atom.xdnd_drop) },
2082 { "XdndStatus", F(atom.xdnd_status) },
2083 { "XdndFinished", F(atom.xdnd_finished) },
2084 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002085 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2086 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002087 };
2088#undef F
2089
2090 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2091 xcb_xfixes_query_version_reply_t *xfixes_reply;
2092 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2093 xcb_intern_atom_reply_t *reply;
2094 xcb_render_query_pict_formats_reply_t *formats_reply;
2095 xcb_render_query_pict_formats_cookie_t formats_cookie;
2096 xcb_render_pictforminfo_t *formats;
2097 uint32_t i;
2098
2099 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002100 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002101
2102 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2103
2104 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2105 cookies[i] = xcb_intern_atom (wm->conn, 0,
2106 strlen(atoms[i].name),
2107 atoms[i].name);
2108
2109 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2110 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2111 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2112 free(reply);
2113 }
2114
2115 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2116 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002117 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002118
2119 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2120 XCB_XFIXES_MAJOR_VERSION,
2121 XCB_XFIXES_MINOR_VERSION);
2122 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2123 xfixes_cookie, NULL);
2124
Martin Minarik6d118362012-06-07 18:01:59 +02002125 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002126 xfixes_reply->major_version, xfixes_reply->minor_version);
2127
2128 free(xfixes_reply);
2129
2130 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2131 formats_cookie, 0);
2132 if (formats_reply == NULL)
2133 return;
2134
2135 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002136 for (i = 0; i < formats_reply->num_formats; i++) {
2137 if (formats[i].direct.red_mask != 0xff &&
2138 formats[i].direct.red_shift != 16)
2139 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002140 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2141 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002142 wm->format_rgb = formats[i];
2143 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2144 formats[i].depth == 32 &&
2145 formats[i].direct.alpha_mask == 0xff &&
2146 formats[i].direct.alpha_shift == 24)
2147 wm->format_rgba = formats[i];
2148 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002149
2150 free(formats_reply);
2151}
2152
2153static void
2154weston_wm_create_wm_window(struct weston_wm *wm)
2155{
2156 static const char name[] = "Weston WM";
2157
2158 wm->wm_window = xcb_generate_id(wm->conn);
2159 xcb_create_window(wm->conn,
2160 XCB_COPY_FROM_PARENT,
2161 wm->wm_window,
2162 wm->screen->root,
2163 0, 0,
2164 10, 10,
2165 0,
2166 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2167 wm->screen->root_visual,
2168 0, NULL);
2169
2170 xcb_change_property(wm->conn,
2171 XCB_PROP_MODE_REPLACE,
2172 wm->wm_window,
2173 wm->atom.net_supporting_wm_check,
2174 XCB_ATOM_WINDOW,
2175 32, /* format */
2176 1, &wm->wm_window);
2177
2178 xcb_change_property(wm->conn,
2179 XCB_PROP_MODE_REPLACE,
2180 wm->wm_window,
2181 wm->atom.net_wm_name,
2182 wm->atom.utf8_string,
2183 8, /* format */
2184 strlen(name), name);
2185
2186 xcb_change_property(wm->conn,
2187 XCB_PROP_MODE_REPLACE,
2188 wm->screen->root,
2189 wm->atom.net_supporting_wm_check,
2190 XCB_ATOM_WINDOW,
2191 32, /* format */
2192 1, &wm->wm_window);
2193
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002194 /* Claim the WM_S0 selection even though we don't suport
2195 * the --replace functionality. */
2196 xcb_set_selection_owner(wm->conn,
2197 wm->wm_window,
2198 wm->atom.wm_s0,
2199 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002200
2201 xcb_set_selection_owner(wm->conn,
2202 wm->wm_window,
2203 wm->atom.net_wm_cm_s0,
2204 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002205}
2206
2207struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002208weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002209{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002210 struct weston_wm *wm;
2211 struct wl_event_loop *loop;
2212 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002213 uint32_t values[1];
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002214 xcb_atom_t supported[6];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002215
Peter Huttererf3d62272013-08-08 11:57:05 +10002216 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002217 if (wm == NULL)
2218 return NULL;
2219
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002220 wm->server = wxs;
2221 wm->window_hash = hash_table_create();
2222 if (wm->window_hash == NULL) {
2223 free(wm);
2224 return NULL;
2225 }
2226
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002227 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002228 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002229 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002230 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002231 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002232 hash_table_destroy(wm->window_hash);
2233 free(wm);
2234 return NULL;
2235 }
2236
2237 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2238 wm->screen = s.data;
2239
2240 loop = wl_display_get_event_loop(wxs->wl_display);
2241 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002242 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002243 WL_EVENT_READABLE,
2244 weston_wm_handle_event, wm);
2245 wl_event_source_check(wm->source);
2246
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002247 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002248 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002249
2250 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002251 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2252 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2253 XCB_EVENT_MASK_PROPERTY_CHANGE;
2254 xcb_change_window_attributes(wm->conn, wm->screen->root,
2255 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002256
2257 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2258 XCB_COMPOSITE_REDIRECT_MANUAL);
2259
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002260 wm->theme = theme_create();
2261
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002262 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002263 supported[1] = wm->atom.net_wm_state;
2264 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002265 supported[3] = wm->atom.net_wm_state_maximized_vert;
2266 supported[4] = wm->atom.net_wm_state_maximized_horz;
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002267 supported[5] = wm->atom.net_active_window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002268 xcb_change_property(wm->conn,
2269 XCB_PROP_MODE_REPLACE,
2270 wm->screen->root,
2271 wm->atom.net_supported,
2272 XCB_ATOM_ATOM,
2273 32, /* format */
2274 ARRAY_LENGTH(supported), supported);
2275
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002276 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
2277
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002278 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002279
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002280 weston_wm_dnd_init(wm);
2281
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002282 xcb_flush(wm->conn);
2283
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002284 wm->create_surface_listener.notify = weston_wm_create_surface;
2285 wl_signal_add(&wxs->compositor->create_surface_signal,
2286 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002287 wm->activate_listener.notify = weston_wm_window_activate;
2288 wl_signal_add(&wxs->compositor->activate_signal,
2289 &wm->activate_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002290 wm->kill_listener.notify = weston_wm_kill_client;
2291 wl_signal_add(&wxs->compositor->kill_signal,
2292 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002293 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002294
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002295 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002296 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002297
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002298 /* Create wm window and take WM_S0 selection last, which
2299 * signals to Xwayland that we're done with setup. */
2300 weston_wm_create_wm_window(wm);
2301
2302 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002303
2304 return wm;
2305}
2306
2307void
2308weston_wm_destroy(struct weston_wm *wm)
2309{
2310 /* FIXME: Free windows in hash. */
2311 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002312 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002313 xcb_disconnect(wm->conn);
2314 wl_event_source_remove(wm->source);
2315 wl_list_remove(&wm->selection_listener.link);
2316 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002317 wl_list_remove(&wm->kill_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002318 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002319
2320 free(wm);
2321}
2322
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002323static struct weston_wm_window *
2324get_wm_window(struct weston_surface *surface)
2325{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002326 struct wl_listener *listener;
2327
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002328 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002329 if (listener)
2330 return container_of(listener, struct weston_wm_window,
2331 surface_destroy_listener);
2332
2333 return NULL;
2334}
2335
2336static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002337weston_wm_window_configure(void *data)
2338{
2339 struct weston_wm_window *window = data;
2340 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002341 uint32_t values[4];
2342 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002343
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002344 weston_wm_window_get_child_position(window, &x, &y);
2345 values[0] = x;
2346 values[1] = y;
2347 values[2] = window->width;
2348 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002349 xcb_configure_window(wm->conn,
2350 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002351 XCB_CONFIG_WINDOW_X |
2352 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002353 XCB_CONFIG_WINDOW_WIDTH |
2354 XCB_CONFIG_WINDOW_HEIGHT,
2355 values);
2356
2357 weston_wm_window_get_frame_size(window, &width, &height);
2358 values[0] = width;
2359 values[1] = height;
2360 xcb_configure_window(wm->conn,
2361 window->frame_id,
2362 XCB_CONFIG_WINDOW_WIDTH |
2363 XCB_CONFIG_WINDOW_HEIGHT,
2364 values);
2365
2366 window->configure_source = NULL;
2367
2368 weston_wm_window_schedule_repaint(window);
2369}
2370
2371static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002372send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002373{
2374 struct weston_wm_window *window = get_wm_window(surface);
2375 struct weston_wm *wm = window->wm;
2376 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002377 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002378
Marek Chalupae9fe4672014-10-29 13:44:44 +01002379 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002380 hborder = 2 * t->width;
2381 vborder = t->titlebar_height + t->width;
2382 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002383 hborder = 0;
2384 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002385 }
2386
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002387 if (width > hborder)
2388 window->width = width - hborder;
2389 else
2390 window->width = 1;
2391
2392 if (height > vborder)
2393 window->height = height - vborder;
2394 else
2395 window->height = 1;
2396
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002397 if (window->frame)
2398 frame_resize_inside(window->frame, window->width, window->height);
2399
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002400 if (window->configure_source)
2401 return;
2402
2403 window->configure_source =
2404 wl_event_loop_add_idle(wm->server->loop,
2405 weston_wm_window_configure, window);
2406}
2407
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002408static void
2409send_position(struct weston_surface *surface, int32_t x, int32_t y)
2410{
2411 struct weston_wm_window *window = get_wm_window(surface);
2412 struct weston_wm *wm;
2413 uint32_t mask, values[2];
2414
2415 if (!window || !window->wm)
2416 return;
2417
2418 wm = window->wm;
2419 /* We use pos_dirty to tell whether a configure message is in flight.
2420 * This is needed in case we send two configure events in a very
2421 * short time, since window->x/y is set in after a roundtrip, hence
2422 * we cannot just check if the current x and y are different. */
2423 if (window->x != x || window->y != y || window->pos_dirty) {
2424 window->pos_dirty = true;
2425 values[0] = x;
2426 values[1] = y;
2427 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
2428
2429 xcb_configure_window(wm->conn, window->frame_id, mask, values);
2430 xcb_flush(wm->conn);
2431 }
2432}
2433
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002434static const struct weston_shell_client shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002435 send_configure,
2436 send_position
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002437};
2438
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002439static int
2440legacy_fullscreen(struct weston_wm *wm,
2441 struct weston_wm_window *window,
2442 struct weston_output **output_ret)
2443{
2444 struct weston_compositor *compositor = wm->server->compositor;
2445 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002446 uint32_t minmax = PMinSize | PMaxSize;
2447 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002448
2449 /* Heuristics for detecting legacy fullscreen windows... */
2450
2451 wl_list_for_each(output, &compositor->output_list, link) {
2452 if (output->x == window->x &&
2453 output->y == window->y &&
2454 output->width == window->width &&
2455 output->height == window->height &&
2456 window->override_redirect) {
2457 *output_ret = output;
2458 return 1;
2459 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002460
2461 matching_size = 0;
2462 if ((window->size_hints.flags & (USSize |PSize)) &&
2463 window->size_hints.width == output->width &&
2464 window->size_hints.height == output->height)
2465 matching_size = 1;
2466 if ((window->size_hints.flags & minmax) == minmax &&
2467 window->size_hints.min_width == output->width &&
2468 window->size_hints.min_height == output->height &&
2469 window->size_hints.max_width == output->width &&
2470 window->size_hints.max_height == output->height)
2471 matching_size = 1;
2472
2473 if (matching_size && !window->decorate &&
2474 (window->size_hints.flags & (USPosition | PPosition)) &&
2475 window->size_hints.x == output->x &&
2476 window->size_hints.y == output->y) {
2477 *output_ret = output;
2478 return 1;
2479 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002480 }
2481
2482 return 0;
2483}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002484
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002485static bool
2486weston_wm_window_type_inactive(struct weston_wm_window *window)
2487{
2488 struct weston_wm *wm = window->wm;
2489
2490 return window->type == wm->atom.net_wm_window_type_tooltip ||
2491 window->type == wm->atom.net_wm_window_type_dropdown ||
2492 window->type == wm->atom.net_wm_window_type_dnd ||
2493 window->type == wm->atom.net_wm_window_type_combo ||
Giulio Camuffo84787ea2015-04-29 19:00:48 +03002494 window->type == wm->atom.net_wm_window_type_popup ||
2495 window->type == wm->atom.net_wm_window_type_utility;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002496}
2497
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002498static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002499xserver_map_shell_surface(struct weston_wm_window *window,
2500 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002501{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002502 struct weston_wm *wm = window->wm;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002503 struct weston_shell_interface *shell_interface =
2504 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002505 struct weston_output *output;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002506 struct weston_wm_window *parent;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002507 int flags = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002508
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002509 weston_wm_window_read_properties(window);
2510
2511 /* A weston_wm_window may have many different surfaces assigned
2512 * throughout its life, so we must make sure to remove the listener
2513 * from the old surface signal list. */
2514 if (window->surface)
2515 wl_list_remove(&window->surface_destroy_listener.link);
2516
2517 window->surface = surface;
2518 window->surface_destroy_listener.notify = surface_destroy;
2519 wl_signal_add(&window->surface->destroy_signal,
2520 &window->surface_destroy_listener);
2521
2522 weston_wm_window_schedule_repaint(window);
2523
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002524 if (!shell_interface->create_shell_surface)
2525 return;
2526
Pekka Paalanen50b67472014-10-01 15:02:41 +03002527 if (window->surface->configure) {
2528 weston_log("warning, unexpected in %s: "
2529 "surface's configure hook is already set.\n",
2530 __func__);
2531 return;
2532 }
2533
Murray Calavera883ac022015-06-06 13:02:22 +00002534 window->shsurf =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002535 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002536 window->surface,
2537 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002538
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002539 if (window->name)
2540 shell_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002541 if (window->pid > 0)
2542 shell_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002543
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002544 if (window->fullscreen) {
2545 window->saved_width = window->width;
2546 window->saved_height = window->height;
2547 shell_interface->set_fullscreen(window->shsurf,
2548 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2549 0, NULL);
2550 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002551 } else if (legacy_fullscreen(wm, window, &output)) {
2552 window->fullscreen = 1;
2553 shell_interface->set_fullscreen(window->shsurf,
2554 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2555 0, output);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002556 } else if (window->override_redirect) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002557 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002558 window->x,
2559 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002560 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Axel Davye4450f92014-01-12 15:06:05 +01002561 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002562 parent = window->transient_for;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002563 if (weston_wm_window_type_inactive(window))
2564 flags = WL_SHELL_SURFACE_TRANSIENT_INACTIVE;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002565 shell_interface->set_transient(window->shsurf,
2566 parent->surface,
Axel Davyfa506b62014-01-12 15:06:04 +01002567 window->x - parent->x,
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002568 window->y - parent->y, flags);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002569 } else if (weston_wm_window_is_maximized(window)) {
2570 shell_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002571 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002572 if (weston_wm_window_type_inactive(window)) {
2573 shell_interface->set_xwayland(window->shsurf,
2574 window->x,
2575 window->y,
2576 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2577 } else {
2578 shell_interface->set_toplevel(window->shsurf);
2579 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002580 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002581}