blob: f6f92bd5fcbeb0c86f62da74d14f8002983d2f44 [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>
29#include <stdio.h>
30#include <string.h>
31#include <sys/socket.h>
32#include <sys/un.h>
33#include <fcntl.h>
34#include <errno.h>
35#include <unistd.h>
36#include <signal.h>
Tiago Vignatti90fada42012-07-16 12:02:08 -040037#include <X11/Xcursor/Xcursor.h>
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -050038#include <linux/input.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040039
40#include "xwayland.h"
41
Kristian Høgsberg2ba10df2013-12-03 16:38:15 -080042#include "cairo-util.h"
43#include "compositor.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040044#include "hash.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070045#include "shared/helpers.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040046
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070047struct wm_size_hints {
Bryce Harringtone00554b2015-06-12 10:17:39 -070048 uint32_t flags;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070049 int32_t x, y;
50 int32_t width, height; /* should set so old wm's don't mess up */
51 int32_t min_width, min_height;
52 int32_t max_width, max_height;
Bryce Harringtone00554b2015-06-12 10:17:39 -070053 int32_t width_inc, height_inc;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070054 struct {
55 int32_t x;
56 int32_t y;
57 } min_aspect, max_aspect;
58 int32_t base_width, base_height;
59 int32_t win_gravity;
60};
61
62#define USPosition (1L << 0)
63#define USSize (1L << 1)
64#define PPosition (1L << 2)
65#define PSize (1L << 3)
66#define PMinSize (1L << 4)
67#define PMaxSize (1L << 5)
68#define PResizeInc (1L << 6)
69#define PAspect (1L << 7)
70#define PBaseSize (1L << 8)
71#define PWinGravity (1L << 9)
72
Kristian Høgsberg380deee2012-05-21 17:12:41 -040073struct motif_wm_hints {
74 uint32_t flags;
75 uint32_t functions;
76 uint32_t decorations;
77 int32_t input_mode;
78 uint32_t status;
79};
80
81#define MWM_HINTS_FUNCTIONS (1L << 0)
82#define MWM_HINTS_DECORATIONS (1L << 1)
83#define MWM_HINTS_INPUT_MODE (1L << 2)
84#define MWM_HINTS_STATUS (1L << 3)
85
86#define MWM_FUNC_ALL (1L << 0)
87#define MWM_FUNC_RESIZE (1L << 1)
88#define MWM_FUNC_MOVE (1L << 2)
89#define MWM_FUNC_MINIMIZE (1L << 3)
90#define MWM_FUNC_MAXIMIZE (1L << 4)
91#define MWM_FUNC_CLOSE (1L << 5)
92
93#define MWM_DECOR_ALL (1L << 0)
94#define MWM_DECOR_BORDER (1L << 1)
95#define MWM_DECOR_RESIZEH (1L << 2)
96#define MWM_DECOR_TITLE (1L << 3)
97#define MWM_DECOR_MENU (1L << 4)
98#define MWM_DECOR_MINIMIZE (1L << 5)
99#define MWM_DECOR_MAXIMIZE (1L << 6)
100
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700101#define MWM_DECOR_EVERYTHING \
102 (MWM_DECOR_BORDER | MWM_DECOR_RESIZEH | MWM_DECOR_TITLE | \
103 MWM_DECOR_MENU | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE)
104
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400105#define MWM_INPUT_MODELESS 0
106#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
107#define MWM_INPUT_SYSTEM_MODAL 2
108#define MWM_INPUT_FULL_APPLICATION_MODAL 3
109#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
110
111#define MWM_TEAROFF_WINDOW (1L<<0)
112
113#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
114#define _NET_WM_MOVERESIZE_SIZE_TOP 1
115#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
116#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
117#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
118#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
119#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
120#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
121#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
122#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
123#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
124#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
125
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400126struct weston_wm_window {
127 struct weston_wm *wm;
128 xcb_window_t id;
129 xcb_window_t frame_id;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500130 struct frame *frame;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400131 cairo_surface_t *cairo_surface;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700132 uint32_t surface_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400133 struct weston_surface *surface;
134 struct shell_surface *shsurf;
135 struct wl_listener surface_destroy_listener;
136 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400137 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400138 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300139 int pid;
140 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400141 char *class;
142 char *name;
143 struct weston_wm_window *transient_for;
144 uint32_t protocols;
145 xcb_atom_t type;
146 int width, height;
147 int x, y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200148 bool pos_dirty;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500149 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400150 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300151 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500152 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500153 int has_alpha;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700154 int delete_window;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200155 int maximized_vert;
156 int maximized_horz;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700157 struct wm_size_hints size_hints;
158 struct motif_wm_hints motif_hints;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700159 struct wl_list link;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400160};
161
162static struct weston_wm_window *
163get_wm_window(struct weston_surface *surface);
164
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400165static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200166weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window);
167
168static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400169weston_wm_window_schedule_repaint(struct weston_wm_window *window);
170
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700171static void
172xserver_map_shell_surface(struct weston_wm_window *window,
173 struct weston_surface *surface);
174
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400175static int __attribute__ ((format (printf, 1, 2)))
176wm_log(const char *fmt, ...)
177{
178#ifdef WM_DEBUG
179 int l;
180 va_list argp;
181
182 va_start(argp, fmt);
183 l = weston_vlog(fmt, argp);
184 va_end(argp);
185
186 return l;
187#else
188 return 0;
189#endif
190}
191
192static int __attribute__ ((format (printf, 1, 2)))
193wm_log_continue(const char *fmt, ...)
194{
195#ifdef WM_DEBUG
196 int l;
197 va_list argp;
198
199 va_start(argp, fmt);
200 l = weston_vlog_continue(fmt, argp);
201 va_end(argp);
202
203 return l;
204#else
205 return 0;
206#endif
207}
208
Derek Foreman49372142015-04-09 10:51:22 -0500209static bool __attribute__ ((warn_unused_result))
210wm_lookup_window(struct weston_wm *wm, xcb_window_t hash,
211 struct weston_wm_window **window)
212{
213 *window = hash_table_lookup(wm->window_hash, hash);
214 if (*window)
215 return true;
216 return false;
217}
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400218
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400219const char *
220get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
221{
222 xcb_get_atom_name_cookie_t cookie;
223 xcb_get_atom_name_reply_t *reply;
224 xcb_generic_error_t *e;
225 static char buffer[64];
226
227 if (atom == XCB_ATOM_NONE)
228 return "None";
229
230 cookie = xcb_get_atom_name (c, atom);
231 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500232
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300233 if (reply) {
MoD55375b92013-06-11 19:59:42 -0500234 snprintf(buffer, sizeof buffer, "%.*s",
235 xcb_get_atom_name_name_length (reply),
236 xcb_get_atom_name_name (reply));
237 } else {
238 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
239 }
240
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400241 free(reply);
242
243 return buffer;
244}
245
Tiago Vignatti90fada42012-07-16 12:02:08 -0400246static xcb_cursor_t
247xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
248{
249 xcb_connection_t *c = wm->conn;
250 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
251 xcb_screen_t *screen = s.data;
252 xcb_gcontext_t gc;
253 xcb_pixmap_t pix;
254 xcb_render_picture_t pic;
255 xcb_cursor_t cursor;
256 int stride = img->width * 4;
257
258 pix = xcb_generate_id(c);
259 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
260
261 pic = xcb_generate_id(c);
262 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
263
264 gc = xcb_generate_id(c);
265 xcb_create_gc(c, gc, pix, 0, 0);
266
267 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
268 img->width, img->height, 0, 0, 0, 32,
269 stride * img->height, (uint8_t *) img->pixels);
270 xcb_free_gc(c, gc);
271
272 cursor = xcb_generate_id(c);
273 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
274
275 xcb_render_free_picture(c, pic);
276 xcb_free_pixmap(c, pix);
277
278 return cursor;
279}
280
281static xcb_cursor_t
282xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
283{
284 /* TODO: treat animated cursors as well */
285 if (images->nimage != 1)
286 return -1;
287
288 return xcb_cursor_image_load_cursor(wm, images->images[0]);
289}
290
291static xcb_cursor_t
292xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
293{
294 xcb_cursor_t cursor;
295 XcursorImages *images;
296 char *v = NULL;
297 int size = 0;
298
299 if (!file)
300 return 0;
301
302 v = getenv ("XCURSOR_SIZE");
303 if (v)
304 size = atoi(v);
305
306 if (!size)
307 size = 32;
308
309 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300310 if (!images)
311 return -1;
312
Tiago Vignatti90fada42012-07-16 12:02:08 -0400313 cursor = xcb_cursor_images_load_cursor (wm, images);
314 XcursorImagesDestroy (images);
315
316 return cursor;
317}
318
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400319void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400320dump_property(struct weston_wm *wm,
321 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400322{
323 int32_t *incr_value;
324 const char *text_value, *name;
325 xcb_atom_t *atom_value;
326 int width, len;
327 uint32_t i;
328
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400329 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400330 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400331 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400332 return;
333 }
334
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400335 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
336 get_atom_name(wm->conn, reply->type),
337 reply->format,
338 xcb_get_property_value_length(reply),
339 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400340
341 if (reply->type == wm->atom.incr) {
342 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400343 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400344 } else if (reply->type == wm->atom.utf8_string ||
345 reply->type == wm->atom.string) {
346 text_value = xcb_get_property_value(reply);
347 if (reply->value_len > 40)
348 len = 40;
349 else
350 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400351 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400352 } else if (reply->type == XCB_ATOM_ATOM) {
353 atom_value = xcb_get_property_value(reply);
354 for (i = 0; i < reply->value_len; i++) {
355 name = get_atom_name(wm->conn, atom_value[i]);
356 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400357 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400358 width = 4;
359 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400360 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400361 }
362
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400363 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400364 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400365 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400366 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400367 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400368 }
369}
370
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200371static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400372read_and_dump_property(struct weston_wm *wm,
373 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400374{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400375 xcb_get_property_reply_t *reply;
376 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400377
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400378 cookie = xcb_get_property(wm->conn, 0, window,
379 property, XCB_ATOM_ANY, 0, 2048);
380 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400381
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400382 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400383
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400384 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400385}
386
387/* We reuse some predefined, but otherwise useles atoms */
388#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
389#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500390#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700391#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400392
393static void
394weston_wm_window_read_properties(struct weston_wm_window *window)
395{
396 struct weston_wm *wm = window->wm;
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200397 struct weston_shell_interface *shell_interface =
398 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400399
400#define F(field) offsetof(struct weston_wm_window, field)
401 const struct {
402 xcb_atom_t atom;
403 xcb_atom_t type;
404 int offset;
405 } props[] = {
406 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
407 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
408 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
409 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700410 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500411 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400412 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
413 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300414 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400415 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300416 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400417 };
418#undef F
419
420 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
421 xcb_get_property_reply_t *reply;
422 void *p;
423 uint32_t *xid;
424 xcb_atom_t *atom;
425 uint32_t i;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200426 char name[1024];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400427
428 if (!window->properties_dirty)
429 return;
430 window->properties_dirty = 0;
431
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400432 for (i = 0; i < ARRAY_LENGTH(props); i++)
433 cookie[i] = xcb_get_property(wm->conn,
434 0, /* delete */
435 window->id,
436 props[i].atom,
437 XCB_ATOM_ANY, 0, 2048);
438
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700439 window->decorate = window->override_redirect ? 0 : MWM_DECOR_EVERYTHING;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700440 window->size_hints.flags = 0;
441 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700442 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700443
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400444 for (i = 0; i < ARRAY_LENGTH(props); i++) {
445 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
446 if (!reply)
447 /* Bad window, typically */
448 continue;
449 if (reply->type == XCB_ATOM_NONE) {
450 /* No such property */
451 free(reply);
452 continue;
453 }
454
455 p = ((char *) window + props[i].offset);
456
457 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300458 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400459 case XCB_ATOM_STRING:
460 /* FIXME: We're using this for both string and
461 utf8_string */
462 if (*(char **) p)
463 free(*(char **) p);
464
465 *(char **) p =
466 strndup(xcb_get_property_value(reply),
467 xcb_get_property_value_length(reply));
468 break;
469 case XCB_ATOM_WINDOW:
470 xid = xcb_get_property_value(reply);
Derek Foreman49372142015-04-09 10:51:22 -0500471 if (!wm_lookup_window(wm, *xid, p))
472 weston_log("XCB_ATOM_WINDOW contains window"
473 " id not found in hash table.\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400474 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300475 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400476 case XCB_ATOM_ATOM:
477 atom = xcb_get_property_value(reply);
478 *(xcb_atom_t *) p = *atom;
479 break;
480 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700481 atom = xcb_get_property_value(reply);
482 for (i = 0; i < reply->value_len; i++)
Derek Foremanb4deec62015-04-07 12:12:13 -0500483 if (atom[i] == wm->atom.wm_delete_window) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700484 window->delete_window = 1;
Derek Foremanb4deec62015-04-07 12:12:13 -0500485 break;
486 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400487 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700488 case TYPE_WM_NORMAL_HINTS:
489 memcpy(&window->size_hints,
490 xcb_get_property_value(reply),
491 sizeof window->size_hints);
492 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500493 case TYPE_NET_WM_STATE:
494 window->fullscreen = 0;
495 atom = xcb_get_property_value(reply);
Ryo Munakataf3744f52015-03-11 17:36:30 +0900496 for (i = 0; i < reply->value_len; i++) {
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500497 if (atom[i] == wm->atom.net_wm_state_fullscreen)
498 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200499 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
500 window->maximized_vert = 1;
501 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
502 window->maximized_horz = 1;
Ryo Munakataf3744f52015-03-11 17:36:30 +0900503 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500504 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400505 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700506 memcpy(&window->motif_hints,
507 xcb_get_property_value(reply),
508 sizeof window->motif_hints);
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700509 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) {
510 if (window->motif_hints.decorations & MWM_DECOR_ALL)
511 /* MWM_DECOR_ALL means all except the other values listed. */
512 window->decorate =
513 MWM_DECOR_EVERYTHING & (~window->motif_hints.decorations);
514 else
515 window->decorate =
516 window->motif_hints.decorations;
517 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400518 break;
519 default:
520 break;
521 }
522 free(reply);
523 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200524
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200525 if (window->pid > 0) {
526 gethostname(name, sizeof(name));
527 for (i = 0; i < sizeof(name); i++) {
528 if (name[i] == '\0')
529 break;
530 }
531 if (i == sizeof(name))
532 name[0] = '\0'; /* ignore stupid hostnames */
533
534 /* this is only one heuristic to guess the PID of a client is
535 * valid, assuming it's compliant with icccm and ewmh.
536 * Non-compliants and remote applications of course fail. */
537 if (!window->machine || strcmp(window->machine, name))
538 window->pid = 0;
539 }
540
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200541 if (window->shsurf && window->name)
542 shell_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500543 if (window->frame && window->name)
544 frame_set_title(window->frame, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200545 if (window->shsurf && window->pid > 0)
546 shell_interface->set_pid(window->shsurf, window->pid);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400547}
548
549static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400550weston_wm_window_get_frame_size(struct weston_wm_window *window,
551 int *width, int *height)
552{
553 struct theme *t = window->wm->theme;
554
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500555 if (window->fullscreen) {
556 *width = window->width;
557 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800558 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500559 *width = frame_width(window->frame);
560 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400561 } else {
562 *width = window->width + t->margin * 2;
563 *height = window->height + t->margin * 2;
564 }
565}
566
567static void
568weston_wm_window_get_child_position(struct weston_wm_window *window,
569 int *x, int *y)
570{
571 struct theme *t = window->wm->theme;
572
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500573 if (window->fullscreen) {
574 *x = 0;
575 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800576 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500577 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400578 } else {
579 *x = t->margin;
580 *y = t->margin;
581 }
582}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400583
584static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500585weston_wm_window_send_configure_notify(struct weston_wm_window *window)
586{
587 xcb_configure_notify_event_t configure_notify;
588 struct weston_wm *wm = window->wm;
589 int x, y;
590
591 weston_wm_window_get_child_position(window, &x, &y);
592 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
593 configure_notify.pad0 = 0;
594 configure_notify.event = window->id;
595 configure_notify.window = window->id;
596 configure_notify.above_sibling = XCB_WINDOW_NONE;
597 configure_notify.x = x;
598 configure_notify.y = y;
599 configure_notify.width = window->width;
600 configure_notify.height = window->height;
601 configure_notify.border_width = 0;
602 configure_notify.override_redirect = 0;
603 configure_notify.pad1 = 0;
604
Murray Calavera883ac022015-06-06 13:02:22 +0000605 xcb_send_event(wm->conn, 0, window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500606 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
607 (char *) &configure_notify);
608}
609
610static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400611weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
612{
Murray Calavera883ac022015-06-06 13:02:22 +0000613 xcb_configure_request_event_t *configure_request =
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400614 (xcb_configure_request_event_t *) event;
615 struct weston_wm_window *window;
616 uint32_t mask, values[16];
617 int x, y, width, height, i = 0;
618
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400619 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
620 configure_request->window,
621 configure_request->x, configure_request->y,
622 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400623
Derek Foreman49372142015-04-09 10:51:22 -0500624 if (!wm_lookup_window(wm, configure_request->window, &window))
625 return;
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400626
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500627 if (window->fullscreen) {
628 weston_wm_window_send_configure_notify(window);
629 return;
630 }
631
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400632 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
633 window->width = configure_request->width;
634 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
635 window->height = configure_request->height;
636
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500637 if (window->frame)
638 frame_resize_inside(window->frame, window->width, window->height);
639
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400640 weston_wm_window_get_child_position(window, &x, &y);
641 values[i++] = x;
642 values[i++] = y;
643 values[i++] = window->width;
644 values[i++] = window->height;
645 values[i++] = 0;
646 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
647 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
648 XCB_CONFIG_WINDOW_BORDER_WIDTH;
649 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
650 values[i++] = configure_request->sibling;
651 mask |= XCB_CONFIG_WINDOW_SIBLING;
652 }
653 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
654 values[i++] = configure_request->stack_mode;
655 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
656 }
657
658 xcb_configure_window(wm->conn, window->id, mask, values);
659
660 weston_wm_window_get_frame_size(window, &width, &height);
661 values[0] = width;
662 values[1] = height;
663 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
664 xcb_configure_window(wm->conn, window->frame_id, mask, values);
665
666 weston_wm_window_schedule_repaint(window);
667}
668
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400669static int
670our_resource(struct weston_wm *wm, uint32_t id)
671{
672 const xcb_setup_t *setup;
673
674 setup = xcb_get_setup(wm->conn);
675
676 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
677}
678
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400679static void
680weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
681{
Murray Calavera883ac022015-06-06 13:02:22 +0000682 xcb_configure_notify_event_t *configure_notify =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400683 (xcb_configure_notify_event_t *) event;
684 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400685
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400686 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400687 configure_notify->window,
688 configure_notify->x, configure_notify->y,
689 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300690
Derek Foreman49372142015-04-09 10:51:22 -0500691 if (!wm_lookup_window(wm, configure_notify->window, &window))
692 return;
693
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700694 window->x = configure_notify->x;
695 window->y = configure_notify->y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +0200696 window->pos_dirty = false;
697
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700698 if (window->override_redirect) {
699 window->width = configure_notify->width;
700 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500701 if (window->frame)
702 frame_resize_inside(window->frame,
703 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700704 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400705}
706
707static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300708weston_wm_kill_client(struct wl_listener *listener, void *data)
709{
710 struct weston_surface *surface = data;
711 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300712 if (!window)
713 return;
714
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200715 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300716 kill(window->pid, SIGKILL);
717}
718
719static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700720weston_wm_create_surface(struct wl_listener *listener, void *data)
721{
722 struct weston_surface *surface = data;
723 struct weston_wm *wm =
724 container_of(listener,
725 struct weston_wm, create_surface_listener);
726 struct weston_wm_window *window;
727
728 if (wl_resource_get_client(surface->resource) != wm->server->client)
729 return;
730
731 wl_list_for_each(window, &wm->unpaired_window_list, link)
732 if (window->surface_id ==
733 wl_resource_get_id(surface->resource)) {
734 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700735 window->surface_id = 0;
736 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700737 break;
Murray Calavera883ac022015-06-06 13:02:22 +0000738 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700739}
740
741static void
Giulio Camuffob18f7882015-03-29 14:20:23 +0300742weston_wm_send_focus_window(struct weston_wm *wm,
743 struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400744{
Scott Moreau85ecac02012-05-21 15:49:14 -0600745 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400746
Scott Moreau85ecac02012-05-21 15:49:14 -0600747 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700748 uint32_t values[1];
749
Boyan Dingb9f863c2014-08-30 10:33:23 +0800750 if (window->override_redirect)
751 return;
752
Scott Moreau85ecac02012-05-21 15:49:14 -0600753 client_message.response_type = XCB_CLIENT_MESSAGE;
754 client_message.format = 32;
755 client_message.window = window->id;
756 client_message.type = wm->atom.wm_protocols;
757 client_message.data.data32[0] = wm->atom.wm_take_focus;
758 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
759
Murray Calavera883ac022015-06-06 13:02:22 +0000760 xcb_send_event(wm->conn, 0, window->id,
Scott Moreau85ecac02012-05-21 15:49:14 -0600761 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
762 (char *) &client_message);
763
764 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
765 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700766
767 values[0] = XCB_STACK_MODE_ABOVE;
768 xcb_configure_window (wm->conn, window->frame_id,
769 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600770 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400771 xcb_set_input_focus (wm->conn,
772 XCB_INPUT_FOCUS_POINTER_ROOT,
773 XCB_NONE,
774 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600775 }
Giulio Camuffob18f7882015-03-29 14:20:23 +0300776}
777
778static void
779weston_wm_window_activate(struct wl_listener *listener, void *data)
780{
781 struct weston_surface *surface = data;
782 struct weston_wm_window *window = NULL;
783 struct weston_wm *wm =
784 container_of(listener, struct weston_wm, activate_listener);
785
786 if (surface) {
787 window = get_wm_window(surface);
788 }
789
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200790 if (window) {
791 weston_wm_set_net_active_window(wm, window->id);
792 } else {
793 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
794 }
795
Giulio Camuffob18f7882015-03-29 14:20:23 +0300796 weston_wm_send_focus_window(wm, window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400797
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500798 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800799 if (wm->focus_window->frame)
800 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400801 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500802 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400803 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500804 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800805 if (wm->focus_window->frame)
806 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400807 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500808 }
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200809
810 xcb_flush(wm->conn);
811
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400812}
813
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400814#define ICCCM_WITHDRAWN_STATE 0
815#define ICCCM_NORMAL_STATE 1
816#define ICCCM_ICONIC_STATE 3
817
818static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500819weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400820{
821 struct weston_wm *wm = window->wm;
822 uint32_t property[2];
823
824 property[0] = state;
825 property[1] = XCB_WINDOW_NONE;
826
827 xcb_change_property(wm->conn,
828 XCB_PROP_MODE_REPLACE,
829 window->id,
830 wm->atom.wm_state,
831 wm->atom.wm_state,
832 32, /* format */
833 2, property);
834}
835
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400836static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500837weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
838{
839 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200840 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500841 int i;
842
843 i = 0;
844 if (window->fullscreen)
845 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200846 if (window->maximized_vert)
847 property[i++] = wm->atom.net_wm_state_maximized_vert;
848 if (window->maximized_horz)
849 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500850
851 xcb_change_property(wm->conn,
852 XCB_PROP_MODE_REPLACE,
853 window->id,
854 wm->atom.net_wm_state,
855 XCB_ATOM_ATOM,
856 32, /* format */
857 i, property);
858}
859
860static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700861weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400862{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700863 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400864 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400865 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200866 int buttons = FRAME_BUTTON_CLOSE;
867
868 if (window->decorate & MWM_DECOR_MAXIMIZE)
869 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400870
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500871 window->frame = frame_create(window->wm->theme,
872 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200873 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500874 frame_resize_inside(window->frame, window->width, window->height);
875
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400876 weston_wm_window_get_frame_size(window, &width, &height);
877 weston_wm_window_get_child_position(window, &x, &y);
878
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400879 values[0] = wm->screen->black_pixel;
880 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400881 XCB_EVENT_MASK_KEY_PRESS |
882 XCB_EVENT_MASK_KEY_RELEASE |
883 XCB_EVENT_MASK_BUTTON_PRESS |
884 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400885 XCB_EVENT_MASK_POINTER_MOTION |
886 XCB_EVENT_MASK_ENTER_WINDOW |
887 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400888 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400889 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400890 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400891
892 window->frame_id = xcb_generate_id(wm->conn);
893 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400894 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400895 window->frame_id,
896 wm->screen->root,
897 0, 0,
898 width, height,
899 0,
900 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400901 wm->visual_id,
902 XCB_CW_BORDER_PIXEL |
903 XCB_CW_EVENT_MASK |
904 XCB_CW_COLORMAP, values);
905
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400906 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
907
908 values[0] = 0;
909 xcb_configure_window(wm->conn, window->id,
910 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
911
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400912 window->cairo_surface =
913 cairo_xcb_surface_create_with_xrender_format(wm->conn,
914 wm->screen,
915 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400916 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400917 width, height);
918
919 hash_table_insert(wm->window_hash, window->frame_id, window);
920}
921
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200922/*
923 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
924 * Passing a <0 desktop value deletes the property.
925 */
926static void
927weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
Bryce Harringtone00554b2015-06-12 10:17:39 -0700928 int desktop)
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200929{
930 if (desktop >= 0) {
931 xcb_change_property(window->wm->conn,
932 XCB_PROP_MODE_REPLACE,
933 window->id,
934 window->wm->atom.net_wm_desktop,
935 XCB_ATOM_CARDINAL,
936 32, /* format */
937 1, &desktop);
938 } else {
939 xcb_delete_property(window->wm->conn,
940 window->id,
941 window->wm->atom.net_wm_desktop);
942 }
943}
944
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400945static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700946weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
947{
948 xcb_map_request_event_t *map_request =
949 (xcb_map_request_event_t *) event;
950 struct weston_wm_window *window;
951
952 if (our_resource(wm, map_request->window)) {
953 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
954 map_request->window);
955 return;
956 }
957
Derek Foreman49372142015-04-09 10:51:22 -0500958 if (!wm_lookup_window(wm, map_request->window, &window))
959 return;
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700960
961 weston_wm_window_read_properties(window);
962
963 if (window->frame_id == XCB_WINDOW_NONE)
964 weston_wm_window_create_frame(window);
965
966 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
967 window->id, window, window->frame_id);
968
969 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
970 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200971 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700972
973 xcb_map_window(wm->conn, map_request->window);
974 xcb_map_window(wm->conn, window->frame_id);
975}
976
977static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400978weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
979{
980 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
981
982 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400983 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
984 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400985 return;
986 }
987
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400988 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400989}
990
991static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400992weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
993{
994 xcb_unmap_notify_event_t *unmap_notify =
995 (xcb_unmap_notify_event_t *) event;
996 struct weston_wm_window *window;
997
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400998 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
999 unmap_notify->window,
1000 unmap_notify->event,
1001 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001002
1003 if (our_resource(wm, unmap_notify->window))
1004 return;
1005
MoD31700122013-06-11 19:58:55 -05001006 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -04001007 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1008 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001009 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001010
Derek Foreman49372142015-04-09 10:51:22 -05001011 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1012 return;
1013
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001014 if (window->surface_id) {
1015 /* Make sure we're not on the unpaired surface list or we
1016 * could be assigned a surface during surface creation that
1017 * was mapped before this unmap request.
1018 */
1019 wl_list_remove(&window->link);
1020 window->surface_id = 0;
1021 }
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001022 if (wm->focus_window == window)
1023 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001024 if (window->surface)
1025 wl_list_remove(&window->surface_destroy_listener.link);
1026 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +02001027 window->shsurf = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001028
1029 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1030 weston_wm_window_set_virtual_desktop(window, -1);
1031
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001032 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001033}
1034
1035static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001036weston_wm_window_draw_decoration(void *data)
1037{
1038 struct weston_wm_window *window = data;
1039 struct weston_wm *wm = window->wm;
1040 struct theme *t = wm->theme;
1041 cairo_t *cr;
1042 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001043 int32_t input_x, input_y, input_w, input_h;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07001044 struct weston_shell_interface *shell_interface =
1045 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001046 uint32_t flags = 0;
Giulio Camuffoaa974782015-02-01 16:18:51 +02001047 struct weston_view *view;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001048
1049 weston_wm_window_read_properties(window);
1050
1051 window->repaint_source = NULL;
1052
1053 weston_wm_window_get_frame_size(window, &width, &height);
1054 weston_wm_window_get_child_position(window, &x, &y);
1055
1056 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1057 cr = cairo_create(window->cairo_surface);
1058
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001059 if (window->fullscreen) {
1060 /* nothing */
1061 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001062 if (wm->focus_window == window)
1063 flags |= THEME_FRAME_ACTIVE;
1064
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001065 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001066 } else {
1067 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1068 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1069 cairo_paint(cr);
1070
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001071 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001072 }
1073
1074 cairo_destroy(cr);
1075
1076 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001077 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001078 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001079 pixman_region32_init(&window->surface->pending.opaque);
1080 } else {
1081 /* We leave an extra pixel around the X window area to
1082 * make sure we don't sample from the undefined alpha
1083 * channel when filtering. */
Murray Calavera883ac022015-06-06 13:02:22 +00001084 pixman_region32_init_rect(&window->surface->pending.opaque,
MoD384a11a2013-06-22 11:04:21 -05001085 x - 1, y - 1,
1086 window->width + 2,
1087 window->height + 2);
1088 }
Giulio Camuffoaa974782015-02-01 16:18:51 +02001089 wl_list_for_each(view, &window->surface->views, surface_link)
1090 weston_view_geometry_dirty(view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001091
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001092 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001093
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001094 if (window->decorate && !window->fullscreen) {
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001095 frame_input_rect(window->frame, &input_x, &input_y,
1096 &input_w, &input_h);
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001097 } else {
1098 input_x = x;
1099 input_y = y;
1100 input_w = width;
1101 input_h = height;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001102 }
1103
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001104 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001105 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001106
1107 shell_interface->set_window_geometry(window->shsurf,
1108 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001109 }
1110}
1111
1112static void
1113weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1114{
1115 struct weston_wm *wm = window->wm;
Giulio Camuffoaa974782015-02-01 16:18:51 +02001116 struct weston_view *view;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001117 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001118
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001119 if (window->frame_id == XCB_WINDOW_NONE) {
1120 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001121 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001122 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001123 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001124 pixman_region32_init(&window->surface->pending.opaque);
1125 } else {
1126 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1127 width, height);
1128 }
Giulio Camuffoaa974782015-02-01 16:18:51 +02001129 wl_list_for_each(view, &window->surface->views, surface_link)
1130 weston_view_geometry_dirty(view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001131 }
1132 return;
1133 }
1134
1135 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001136 return;
1137
1138 window->repaint_source =
1139 wl_event_loop_add_idle(wm->server->loop,
1140 weston_wm_window_draw_decoration,
1141 window);
1142}
1143
1144static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001145weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1146{
1147 xcb_property_notify_event_t *property_notify =
1148 (xcb_property_notify_event_t *) event;
1149 struct weston_wm_window *window;
1150
Derek Foreman49372142015-04-09 10:51:22 -05001151 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001152 return;
1153
1154 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001155
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001156 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001157 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001158 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001159 else
1160 read_and_dump_property(wm, property_notify->window,
1161 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001162
1163 if (property_notify->atom == wm->atom.net_wm_name ||
1164 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001165 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001166}
1167
1168static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001169weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001170 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001171{
1172 struct weston_wm_window *window;
1173 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001174 xcb_get_geometry_cookie_t geometry_cookie;
1175 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001176
Peter Huttererf3d62272013-08-08 11:57:05 +10001177 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001178 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001179 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001180 return;
1181 }
1182
MoD384a11a2013-06-22 11:04:21 -05001183 geometry_cookie = xcb_get_geometry(wm->conn, id);
1184
Giulio Camuffob18f7882015-03-29 14:20:23 +03001185 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
1186 XCB_EVENT_MASK_FOCUS_CHANGE;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001187 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1188
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001189 window->wm = wm;
1190 window->id = id;
1191 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001192 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001193 window->width = width;
1194 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001195 window->x = x;
1196 window->y = y;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02001197 window->pos_dirty = false;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001198
MoD384a11a2013-06-22 11:04:21 -05001199 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1200 /* technically we should use XRender and check the visual format's
1201 alpha_mask, but checking depth is simpler and works in all known cases */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001202 if (geometry_reply != NULL)
MoD384a11a2013-06-22 11:04:21 -05001203 window->has_alpha = geometry_reply->depth == 32;
1204 free(geometry_reply);
1205
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001206 hash_table_insert(wm->window_hash, id, window);
1207}
1208
1209static void
1210weston_wm_window_destroy(struct weston_wm_window *window)
1211{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001212 struct weston_wm *wm = window->wm;
1213
1214 if (window->repaint_source)
1215 wl_event_source_remove(window->repaint_source);
1216 if (window->cairo_surface)
1217 cairo_surface_destroy(window->cairo_surface);
1218
1219 if (window->frame_id) {
1220 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1221 xcb_destroy_window(wm->conn, window->frame_id);
1222 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001223 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001224 hash_table_remove(wm->window_hash, window->frame_id);
1225 window->frame_id = XCB_WINDOW_NONE;
1226 }
1227
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001228 if (window->surface_id)
1229 wl_list_remove(&window->link);
1230
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001231 if (window->surface)
1232 wl_list_remove(&window->surface_destroy_listener.link);
1233
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001234 hash_table_remove(window->wm->window_hash, window->id);
1235 free(window);
1236}
1237
1238static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001239weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1240{
1241 xcb_create_notify_event_t *create_notify =
1242 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001243
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001244 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1245 create_notify->window,
1246 create_notify->width, create_notify->height,
1247 create_notify->override_redirect ? ", override" : "",
1248 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001249
1250 if (our_resource(wm, create_notify->window))
1251 return;
1252
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001253 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001254 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001255 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001256 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001257}
1258
1259static void
1260weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1261{
1262 xcb_destroy_notify_event_t *destroy_notify =
1263 (xcb_destroy_notify_event_t *) event;
1264 struct weston_wm_window *window;
1265
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001266 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1267 destroy_notify->window,
1268 destroy_notify->event,
1269 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001270
1271 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001272 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001273
Derek Foreman49372142015-04-09 10:51:22 -05001274 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1275 return;
1276
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001277 weston_wm_window_destroy(window);
1278}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001279
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001280static void
1281weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1282{
1283 xcb_reparent_notify_event_t *reparent_notify =
1284 (xcb_reparent_notify_event_t *) event;
1285 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001286
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001287 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1288 reparent_notify->window,
1289 reparent_notify->parent,
1290 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001291
1292 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001293 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001294 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001295 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001296 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001297 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1298 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001299 weston_wm_window_destroy(window);
1300 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001301}
1302
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001303struct weston_seat *
1304weston_wm_pick_seat(struct weston_wm *wm)
1305{
1306 return container_of(wm->server->compositor->seat_list.next,
1307 struct weston_seat, link);
1308}
1309
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001310static struct weston_seat *
1311weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1312{
1313 struct weston_wm *wm = window->wm;
1314 struct weston_seat *seat, *s;
1315
1316 seat = NULL;
1317 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001318 struct weston_pointer *pointer = weston_seat_get_pointer(s);
1319 struct weston_pointer *old_pointer =
1320 weston_seat_get_pointer(seat);
1321
1322 if (pointer && pointer->focus &&
1323 pointer->focus->surface == window->surface &&
1324 pointer->button_count > 0 &&
1325 (!seat ||
1326 pointer->grab_serial -
1327 old_pointer->grab_serial < (1 << 30)))
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001328 seat = s;
1329 }
1330
1331 return seat;
1332}
1333
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001334static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001335weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1336 xcb_client_message_event_t *client_message)
1337{
1338 static const int map[] = {
1339 THEME_LOCATION_RESIZING_TOP_LEFT,
1340 THEME_LOCATION_RESIZING_TOP,
1341 THEME_LOCATION_RESIZING_TOP_RIGHT,
1342 THEME_LOCATION_RESIZING_RIGHT,
1343 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1344 THEME_LOCATION_RESIZING_BOTTOM,
1345 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1346 THEME_LOCATION_RESIZING_LEFT
1347 };
1348
1349 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001350 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Derek Foreman1281a362015-07-31 16:55:32 -05001351 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001352 int detail;
1353 struct weston_shell_interface *shell_interface =
1354 &wm->server->compositor->shell_interface;
1355
Derek Foreman1281a362015-07-31 16:55:32 -05001356 if (!pointer || pointer->button_count != 1
1357 || !pointer->focus
1358 || pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001359 return;
1360
1361 detail = client_message->data.data32[2];
1362 switch (detail) {
1363 case _NET_WM_MOVERESIZE_MOVE:
Derek Foremane4d6c832015-08-05 14:48:11 -05001364 shell_interface->move(window->shsurf, pointer);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001365 break;
1366 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1367 case _NET_WM_MOVERESIZE_SIZE_TOP:
1368 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1369 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1370 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1371 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1372 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1373 case _NET_WM_MOVERESIZE_SIZE_LEFT:
Derek Foremane4d6c832015-08-05 14:48:11 -05001374 shell_interface->resize(window->shsurf, pointer, map[detail]);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001375 break;
1376 case _NET_WM_MOVERESIZE_CANCEL:
1377 break;
1378 }
1379}
1380
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001381#define _NET_WM_STATE_REMOVE 0
1382#define _NET_WM_STATE_ADD 1
1383#define _NET_WM_STATE_TOGGLE 2
1384
1385static int
1386update_state(int action, int *state)
1387{
1388 int new_state, changed;
1389
1390 switch (action) {
1391 case _NET_WM_STATE_REMOVE:
1392 new_state = 0;
1393 break;
1394 case _NET_WM_STATE_ADD:
1395 new_state = 1;
1396 break;
1397 case _NET_WM_STATE_TOGGLE:
1398 new_state = !*state;
1399 break;
1400 default:
1401 return 0;
1402 }
1403
1404 changed = (*state != new_state);
1405 *state = new_state;
1406
1407 return changed;
1408}
1409
1410static void
1411weston_wm_window_configure(void *data);
1412
1413static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001414weston_wm_window_set_toplevel(struct weston_wm_window *window)
1415{
1416 struct weston_shell_interface *shell_interface =
1417 &window->wm->server->compositor->shell_interface;
1418
1419 shell_interface->set_toplevel(window->shsurf);
1420 window->width = window->saved_width;
1421 window->height = window->saved_height;
1422 if (window->frame)
1423 frame_resize_inside(window->frame,
1424 window->width,
1425 window->height);
1426 weston_wm_window_configure(window);
1427}
1428
1429static inline bool
1430weston_wm_window_is_maximized(struct weston_wm_window *window)
1431{
1432 return window->maximized_horz && window->maximized_vert;
1433}
1434
1435static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001436weston_wm_window_handle_state(struct weston_wm_window *window,
1437 xcb_client_message_event_t *client_message)
1438{
1439 struct weston_wm *wm = window->wm;
1440 struct weston_shell_interface *shell_interface =
1441 &wm->server->compositor->shell_interface;
1442 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001443 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001444
1445 action = client_message->data.data32[0];
1446 property = client_message->data.data32[1];
1447
1448 if (property == wm->atom.net_wm_state_fullscreen &&
1449 update_state(action, &window->fullscreen)) {
1450 weston_wm_window_set_net_wm_state(window);
1451 if (window->fullscreen) {
1452 window->saved_width = window->width;
1453 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001454
1455 if (window->shsurf)
1456 shell_interface->set_fullscreen(window->shsurf,
1457 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1458 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001459 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001460 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001461 weston_wm_window_set_toplevel(window);
1462 }
1463 } else {
1464 if (property == wm->atom.net_wm_state_maximized_vert &&
1465 update_state(action, &window->maximized_vert))
1466 weston_wm_window_set_net_wm_state(window);
1467 if (property == wm->atom.net_wm_state_maximized_horz &&
1468 update_state(action, &window->maximized_horz))
1469 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001470
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001471 if (maximized != weston_wm_window_is_maximized(window)) {
1472 if (weston_wm_window_is_maximized(window)) {
1473 window->saved_width = window->width;
1474 window->saved_height = window->height;
1475
1476 if (window->shsurf)
1477 shell_interface->set_maximized(window->shsurf);
1478 } else if (window->shsurf) {
1479 weston_wm_window_set_toplevel(window);
1480 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001481 }
1482 }
1483}
1484
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001485static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001486surface_destroy(struct wl_listener *listener, void *data)
1487{
1488 struct weston_wm_window *window =
1489 container_of(listener,
1490 struct weston_wm_window, surface_destroy_listener);
1491
1492 wm_log("surface for xid %d destroyed\n", window->id);
1493
1494 /* This should have been freed by the shell.
1495 * Don't try to use it later. */
1496 window->shsurf = NULL;
1497 window->surface = NULL;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001498}
1499
1500static void
1501weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1502 xcb_client_message_event_t *client_message)
1503{
1504 struct weston_wm *wm = window->wm;
1505 struct wl_resource *resource;
1506
1507 if (window->surface_id != 0) {
1508 wm_log("already have surface id for window %d\n", window->id);
1509 return;
1510 }
1511
1512 /* Xwayland will send the wayland requests to create the
1513 * wl_surface before sending this client message. Even so, we
1514 * can end up handling the X event before the wayland requests
1515 * and thus when we try to look up the surface ID, the surface
1516 * hasn't been created yet. In that case put the window on
1517 * the unpaired window list and continue when the surface gets
1518 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001519 uint32_t id = client_message->data.data32[0];
1520 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001521 if (resource) {
1522 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001523 xserver_map_shell_surface(window,
1524 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001525 }
1526 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001527 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001528 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001529 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001530}
1531
1532static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001533weston_wm_handle_client_message(struct weston_wm *wm,
1534 xcb_generic_event_t *event)
1535{
1536 xcb_client_message_event_t *client_message =
1537 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001538 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001539
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001540 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1541 get_atom_name(wm->conn, client_message->type),
1542 client_message->data.data32[0],
1543 client_message->data.data32[1],
1544 client_message->data.data32[2],
1545 client_message->data.data32[3],
1546 client_message->data.data32[4],
1547 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001548
Jason Ekstrand0250a742014-07-24 13:17:47 -07001549 /* The window may get created and destroyed before we actually
1550 * handle the message. If it doesn't exist, bail.
1551 */
Derek Foreman49372142015-04-09 10:51:22 -05001552 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001553 return;
1554
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001555 if (client_message->type == wm->atom.net_wm_moveresize)
1556 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001557 else if (client_message->type == wm->atom.net_wm_state)
1558 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001559 else if (client_message->type == wm->atom.wl_surface_id)
1560 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001561}
1562
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001563enum cursor_type {
1564 XWM_CURSOR_TOP,
1565 XWM_CURSOR_BOTTOM,
1566 XWM_CURSOR_LEFT,
1567 XWM_CURSOR_RIGHT,
1568 XWM_CURSOR_TOP_LEFT,
1569 XWM_CURSOR_TOP_RIGHT,
1570 XWM_CURSOR_BOTTOM_LEFT,
1571 XWM_CURSOR_BOTTOM_RIGHT,
1572 XWM_CURSOR_LEFT_PTR,
1573};
1574
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001575/*
1576 * The following correspondences between file names and cursors was copied
1577 * from: https://bugs.kde.org/attachment.cgi?id=67313
1578 */
1579
1580static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001581 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001582 "sw-resize",
1583 "size_bdiag"
1584};
1585
1586static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001587 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001588 "se-resize",
1589 "size_fdiag"
1590};
1591
1592static const char *bottom_sides[] = {
1593 "bottom_side",
1594 "s-resize",
1595 "size_ver"
1596};
1597
1598static const char *left_ptrs[] = {
1599 "left_ptr",
1600 "default",
1601 "top_left_arrow",
1602 "left-arrow"
1603};
1604
1605static const char *left_sides[] = {
1606 "left_side",
1607 "w-resize",
1608 "size_hor"
1609};
1610
1611static const char *right_sides[] = {
1612 "right_side",
1613 "e-resize",
1614 "size_hor"
1615};
1616
1617static const char *top_left_corners[] = {
1618 "top_left_corner",
1619 "nw-resize",
1620 "size_fdiag"
1621};
1622
1623static const char *top_right_corners[] = {
1624 "top_right_corner",
1625 "ne-resize",
1626 "size_bdiag"
1627};
1628
1629static const char *top_sides[] = {
1630 "top_side",
1631 "n-resize",
1632 "size_ver"
1633};
1634
1635struct cursor_alternatives {
1636 const char **names;
1637 size_t count;
1638};
1639
1640static const struct cursor_alternatives cursors[] = {
1641 {top_sides, ARRAY_LENGTH(top_sides)},
1642 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1643 {left_sides, ARRAY_LENGTH(left_sides)},
1644 {right_sides, ARRAY_LENGTH(right_sides)},
1645 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1646 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1647 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1648 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1649 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001650};
1651
1652static void
1653weston_wm_create_cursors(struct weston_wm *wm)
1654{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001655 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001656 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001657 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001658
1659 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1660 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001661 for (j = 0; j < cursors[i].count; j++) {
1662 name = cursors[i].names[j];
1663 wm->cursors[i] =
1664 xcb_cursor_library_load_cursor(wm, name);
1665 if (wm->cursors[i] != (xcb_cursor_t)-1)
1666 break;
1667 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001668 }
1669
1670 wm->last_cursor = -1;
1671}
1672
1673static void
1674weston_wm_destroy_cursors(struct weston_wm *wm)
1675{
1676 uint8_t i;
1677
1678 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1679 xcb_free_cursor(wm->conn, wm->cursors[i]);
1680
1681 free(wm->cursors);
1682}
1683
1684static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001685get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001686{
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001687 switch (location) {
1688 case THEME_LOCATION_RESIZING_TOP:
1689 return XWM_CURSOR_TOP;
1690 case THEME_LOCATION_RESIZING_BOTTOM:
1691 return XWM_CURSOR_BOTTOM;
1692 case THEME_LOCATION_RESIZING_LEFT:
1693 return XWM_CURSOR_LEFT;
1694 case THEME_LOCATION_RESIZING_RIGHT:
1695 return XWM_CURSOR_RIGHT;
1696 case THEME_LOCATION_RESIZING_TOP_LEFT:
1697 return XWM_CURSOR_TOP_LEFT;
1698 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1699 return XWM_CURSOR_TOP_RIGHT;
1700 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1701 return XWM_CURSOR_BOTTOM_LEFT;
1702 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1703 return XWM_CURSOR_BOTTOM_RIGHT;
1704 case THEME_LOCATION_EXTERIOR:
1705 case THEME_LOCATION_TITLEBAR:
1706 default:
1707 return XWM_CURSOR_LEFT_PTR;
1708 }
1709}
1710
1711static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001712weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1713 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001714{
1715 uint32_t cursor_value_list;
1716
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001717 if (wm->last_cursor == cursor)
1718 return;
1719
1720 wm->last_cursor = cursor;
1721
1722 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001723 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001724 XCB_CW_CURSOR, &cursor_value_list);
1725 xcb_flush(wm->conn);
1726}
1727
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001728static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001729weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001730{
1731 xcb_client_message_event_t client_message;
1732
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001733 if (window->delete_window) {
1734 client_message.response_type = XCB_CLIENT_MESSAGE;
1735 client_message.format = 32;
1736 client_message.window = window->id;
1737 client_message.type = window->wm->atom.wm_protocols;
1738 client_message.data.data32[0] =
1739 window->wm->atom.wm_delete_window;
1740 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001741
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001742 xcb_send_event(window->wm->conn, 0, window->id,
1743 XCB_EVENT_MASK_NO_EVENT,
1744 (char *) &client_message);
1745 } else {
1746 xcb_kill_client(window->wm->conn, window->id);
1747 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001748}
1749
1750static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001751weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1752{
1753 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1754 struct weston_shell_interface *shell_interface =
1755 &wm->server->compositor->shell_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001756 struct weston_seat *seat;
Derek Foremane4d6c832015-08-05 14:48:11 -05001757 struct weston_pointer *pointer;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001758 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001759 enum theme_location location;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001760 enum frame_button_state button_state;
1761 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001762
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001763 wm_log("XCB_BUTTON_%s (detail %d)\n",
1764 button->response_type == XCB_BUTTON_PRESS ?
1765 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001766
Derek Foreman49372142015-04-09 10:51:22 -05001767 if (!wm_lookup_window(wm, button->event, &window) ||
1768 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001769 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001770
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001771 if (button->detail != 1 && button->detail != 2)
1772 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001773
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001774 seat = weston_wm_pick_seat_for_window(window);
Derek Foremane4d6c832015-08-05 14:48:11 -05001775 pointer = weston_seat_get_pointer(seat);
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001776
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001777 button_state = button->response_type == XCB_BUTTON_PRESS ?
1778 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1779 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1780
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001781 /* Make sure we're looking at the right location. The frame
1782 * could have received a motion event from a pointer from a
1783 * different wl_seat, but under X it looks like our core
1784 * pointer moved. Move the frame pointer to the button press
1785 * location before deciding what to do. */
1786 location = frame_pointer_motion(window->frame, NULL,
1787 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001788 location = frame_pointer_button(window->frame, NULL,
1789 button_id, button_state);
1790 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1791 weston_wm_window_schedule_repaint(window);
1792
1793 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001794 if (pointer)
1795 shell_interface->move(window->shsurf, pointer);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001796 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1797 }
1798
1799 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001800 if (pointer)
1801 shell_interface->resize(window->shsurf, pointer, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001802 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1803 }
1804
1805 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001806 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001807 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001808 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001809
1810 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1811 window->maximized_horz = !window->maximized_horz;
1812 window->maximized_vert = !window->maximized_vert;
1813 if (weston_wm_window_is_maximized(window)) {
1814 window->saved_width = window->width;
1815 window->saved_height = window->height;
1816 shell_interface->set_maximized(window->shsurf);
1817 } else {
1818 weston_wm_window_set_toplevel(window);
1819 }
1820 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1821 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001822}
1823
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001824static void
1825weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1826{
1827 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1828 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001829 enum theme_location location;
1830 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001831
Derek Foreman49372142015-04-09 10:51:22 -05001832 if (!wm_lookup_window(wm, motion->event, &window) ||
1833 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001834 return;
1835
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001836 location = frame_pointer_motion(window->frame, NULL,
1837 motion->event_x, motion->event_y);
1838 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1839 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001840
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001841 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001842 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001843}
1844
1845static void
1846weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1847{
1848 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1849 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001850 enum theme_location location;
1851 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001852
Derek Foreman49372142015-04-09 10:51:22 -05001853 if (!wm_lookup_window(wm, enter->event, &window) ||
1854 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001855 return;
1856
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001857 location = frame_pointer_enter(window->frame, NULL,
1858 enter->event_x, enter->event_y);
1859 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1860 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001861
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001862 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001863 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001864}
1865
1866static void
1867weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1868{
1869 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1870 struct weston_wm_window *window;
1871
Derek Foreman49372142015-04-09 10:51:22 -05001872 if (!wm_lookup_window(wm, leave->event, &window) ||
1873 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001874 return;
1875
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001876 frame_pointer_leave(window->frame, NULL);
1877 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1878 weston_wm_window_schedule_repaint(window);
1879
Tiago Vignattic1903232012-07-16 12:15:37 -04001880 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001881}
1882
Giulio Camuffob18f7882015-03-29 14:20:23 +03001883static void
1884weston_wm_handle_focus_in(struct weston_wm *wm, xcb_generic_event_t *event)
1885{
1886 xcb_focus_in_event_t *focus = (xcb_focus_in_event_t *) event;
1887 /* Do not let X clients change the focus behind the compositor's
1888 * back. Reset the focus to the old one if it changed. */
1889 if (!wm->focus_window || focus->event != wm->focus_window->id)
1890 weston_wm_send_focus_window(wm, wm->focus_window);
1891}
1892
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001893static int
1894weston_wm_handle_event(int fd, uint32_t mask, void *data)
1895{
1896 struct weston_wm *wm = data;
1897 xcb_generic_event_t *event;
1898 int count = 0;
1899
1900 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1901 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001902 free(event);
1903 count++;
1904 continue;
1905 }
1906
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001907 if (weston_wm_handle_dnd_event(wm, event)) {
1908 free(event);
1909 count++;
1910 continue;
1911 }
1912
MoD31700122013-06-11 19:58:55 -05001913 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001914 case XCB_BUTTON_PRESS:
1915 case XCB_BUTTON_RELEASE:
1916 weston_wm_handle_button(wm, event);
1917 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001918 case XCB_ENTER_NOTIFY:
1919 weston_wm_handle_enter(wm, event);
1920 break;
1921 case XCB_LEAVE_NOTIFY:
1922 weston_wm_handle_leave(wm, event);
1923 break;
1924 case XCB_MOTION_NOTIFY:
1925 weston_wm_handle_motion(wm, event);
1926 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001927 case XCB_CREATE_NOTIFY:
1928 weston_wm_handle_create_notify(wm, event);
1929 break;
1930 case XCB_MAP_REQUEST:
1931 weston_wm_handle_map_request(wm, event);
1932 break;
1933 case XCB_MAP_NOTIFY:
1934 weston_wm_handle_map_notify(wm, event);
1935 break;
1936 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001937 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001938 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001939 case XCB_REPARENT_NOTIFY:
1940 weston_wm_handle_reparent_notify(wm, event);
1941 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001942 case XCB_CONFIGURE_REQUEST:
1943 weston_wm_handle_configure_request(wm, event);
1944 break;
1945 case XCB_CONFIGURE_NOTIFY:
1946 weston_wm_handle_configure_notify(wm, event);
1947 break;
1948 case XCB_DESTROY_NOTIFY:
1949 weston_wm_handle_destroy_notify(wm, event);
1950 break;
1951 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001952 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001953 break;
1954 case XCB_PROPERTY_NOTIFY:
1955 weston_wm_handle_property_notify(wm, event);
1956 break;
1957 case XCB_CLIENT_MESSAGE:
1958 weston_wm_handle_client_message(wm, event);
1959 break;
Giulio Camuffob18f7882015-03-29 14:20:23 +03001960 case XCB_FOCUS_IN:
1961 weston_wm_handle_focus_in(wm, event);
1962 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001963 }
1964
1965 free(event);
1966 count++;
1967 }
1968
Marek Chalupaa1f3f3c2015-08-12 09:55:12 +02001969 if (count != 0)
1970 xcb_flush(wm->conn);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001971
1972 return count;
1973}
1974
1975static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02001976weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window) {
1977 xcb_change_property(wm->conn, XCB_PROP_MODE_REPLACE,
1978 wm->screen->root, wm->atom.net_active_window,
1979 wm->atom.window, 32, 1, &window);
1980}
1981
1982static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001983weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1984{
1985 xcb_depth_iterator_t d_iter;
1986 xcb_visualtype_iterator_t vt_iter;
1987 xcb_visualtype_t *visualtype;
1988
1989 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1990 visualtype = NULL;
1991 while (d_iter.rem > 0) {
1992 if (d_iter.data->depth == 32) {
1993 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1994 visualtype = vt_iter.data;
1995 break;
1996 }
1997
1998 xcb_depth_next(&d_iter);
1999 }
2000
2001 if (visualtype == NULL) {
2002 weston_log("no 32 bit visualtype\n");
2003 return;
2004 }
2005
2006 wm->visual_id = visualtype->visual_id;
2007 wm->colormap = xcb_generate_id(wm->conn);
2008 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
2009 wm->colormap, wm->screen->root, wm->visual_id);
2010}
2011
2012static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002013weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002014{
2015
2016#define F(field) offsetof(struct weston_wm, field)
2017
2018 static const struct { const char *name; int offset; } atoms[] = {
2019 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002020 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002021 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
2022 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04002023 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002024 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002025 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002026 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002027 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002028 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002029 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2030 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002031 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2032 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002033 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2034 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2035 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02002036 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002037 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2038
2039 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2040 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2041 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2042 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2043 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2044 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2045 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002046 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2047 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002048 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2049 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2050 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2051 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2052 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2053
2054 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2055 { "_NET_SUPPORTING_WM_CHECK",
2056 F(atom.net_supporting_wm_check) },
2057 { "_NET_SUPPORTED", F(atom.net_supported) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002058 { "_NET_ACTIVE_WINDOW", F(atom.net_active_window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002059 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2060 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002061 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002062 { "TARGETS", F(atom.targets) },
2063 { "UTF8_STRING", F(atom.utf8_string) },
2064 { "_WL_SELECTION", F(atom.wl_selection) },
2065 { "INCR", F(atom.incr) },
2066 { "TIMESTAMP", F(atom.timestamp) },
2067 { "MULTIPLE", F(atom.multiple) },
2068 { "UTF8_STRING" , F(atom.utf8_string) },
2069 { "COMPOUND_TEXT", F(atom.compound_text) },
2070 { "TEXT", F(atom.text) },
2071 { "STRING", F(atom.string) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002072 { "WINDOW", F(atom.window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002073 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2074 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002075 { "XdndSelection", F(atom.xdnd_selection) },
2076 { "XdndAware", F(atom.xdnd_aware) },
2077 { "XdndEnter", F(atom.xdnd_enter) },
2078 { "XdndLeave", F(atom.xdnd_leave) },
2079 { "XdndDrop", F(atom.xdnd_drop) },
2080 { "XdndStatus", F(atom.xdnd_status) },
2081 { "XdndFinished", F(atom.xdnd_finished) },
2082 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002083 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2084 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002085 };
2086#undef F
2087
2088 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2089 xcb_xfixes_query_version_reply_t *xfixes_reply;
2090 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2091 xcb_intern_atom_reply_t *reply;
2092 xcb_render_query_pict_formats_reply_t *formats_reply;
2093 xcb_render_query_pict_formats_cookie_t formats_cookie;
2094 xcb_render_pictforminfo_t *formats;
2095 uint32_t i;
2096
2097 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002098 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002099
2100 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2101
2102 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2103 cookies[i] = xcb_intern_atom (wm->conn, 0,
2104 strlen(atoms[i].name),
2105 atoms[i].name);
2106
2107 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2108 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2109 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2110 free(reply);
2111 }
2112
2113 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2114 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002115 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002116
2117 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2118 XCB_XFIXES_MAJOR_VERSION,
2119 XCB_XFIXES_MINOR_VERSION);
2120 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2121 xfixes_cookie, NULL);
2122
Martin Minarik6d118362012-06-07 18:01:59 +02002123 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002124 xfixes_reply->major_version, xfixes_reply->minor_version);
2125
2126 free(xfixes_reply);
2127
2128 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2129 formats_cookie, 0);
2130 if (formats_reply == NULL)
2131 return;
2132
2133 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002134 for (i = 0; i < formats_reply->num_formats; i++) {
2135 if (formats[i].direct.red_mask != 0xff &&
2136 formats[i].direct.red_shift != 16)
2137 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002138 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2139 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002140 wm->format_rgb = formats[i];
2141 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2142 formats[i].depth == 32 &&
2143 formats[i].direct.alpha_mask == 0xff &&
2144 formats[i].direct.alpha_shift == 24)
2145 wm->format_rgba = formats[i];
2146 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002147
2148 free(formats_reply);
2149}
2150
2151static void
2152weston_wm_create_wm_window(struct weston_wm *wm)
2153{
2154 static const char name[] = "Weston WM";
2155
2156 wm->wm_window = xcb_generate_id(wm->conn);
2157 xcb_create_window(wm->conn,
2158 XCB_COPY_FROM_PARENT,
2159 wm->wm_window,
2160 wm->screen->root,
2161 0, 0,
2162 10, 10,
2163 0,
2164 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2165 wm->screen->root_visual,
2166 0, NULL);
2167
2168 xcb_change_property(wm->conn,
2169 XCB_PROP_MODE_REPLACE,
2170 wm->wm_window,
2171 wm->atom.net_supporting_wm_check,
2172 XCB_ATOM_WINDOW,
2173 32, /* format */
2174 1, &wm->wm_window);
2175
2176 xcb_change_property(wm->conn,
2177 XCB_PROP_MODE_REPLACE,
2178 wm->wm_window,
2179 wm->atom.net_wm_name,
2180 wm->atom.utf8_string,
2181 8, /* format */
2182 strlen(name), name);
2183
2184 xcb_change_property(wm->conn,
2185 XCB_PROP_MODE_REPLACE,
2186 wm->screen->root,
2187 wm->atom.net_supporting_wm_check,
2188 XCB_ATOM_WINDOW,
2189 32, /* format */
2190 1, &wm->wm_window);
2191
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002192 /* Claim the WM_S0 selection even though we don't suport
2193 * the --replace functionality. */
2194 xcb_set_selection_owner(wm->conn,
2195 wm->wm_window,
2196 wm->atom.wm_s0,
2197 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002198
2199 xcb_set_selection_owner(wm->conn,
2200 wm->wm_window,
2201 wm->atom.net_wm_cm_s0,
2202 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002203}
2204
2205struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002206weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002207{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002208 struct weston_wm *wm;
2209 struct wl_event_loop *loop;
2210 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002211 uint32_t values[1];
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002212 xcb_atom_t supported[6];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002213
Peter Huttererf3d62272013-08-08 11:57:05 +10002214 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002215 if (wm == NULL)
2216 return NULL;
2217
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002218 wm->server = wxs;
2219 wm->window_hash = hash_table_create();
2220 if (wm->window_hash == NULL) {
2221 free(wm);
2222 return NULL;
2223 }
2224
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002225 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002226 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002227 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002228 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002229 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002230 hash_table_destroy(wm->window_hash);
2231 free(wm);
2232 return NULL;
2233 }
2234
2235 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2236 wm->screen = s.data;
2237
2238 loop = wl_display_get_event_loop(wxs->wl_display);
2239 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002240 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002241 WL_EVENT_READABLE,
2242 weston_wm_handle_event, wm);
2243 wl_event_source_check(wm->source);
2244
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002245 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002246 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002247
2248 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002249 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2250 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2251 XCB_EVENT_MASK_PROPERTY_CHANGE;
2252 xcb_change_window_attributes(wm->conn, wm->screen->root,
2253 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002254
2255 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2256 XCB_COMPOSITE_REDIRECT_MANUAL);
2257
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002258 wm->theme = theme_create();
2259
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002260 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002261 supported[1] = wm->atom.net_wm_state;
2262 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002263 supported[3] = wm->atom.net_wm_state_maximized_vert;
2264 supported[4] = wm->atom.net_wm_state_maximized_horz;
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002265 supported[5] = wm->atom.net_active_window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002266 xcb_change_property(wm->conn,
2267 XCB_PROP_MODE_REPLACE,
2268 wm->screen->root,
2269 wm->atom.net_supported,
2270 XCB_ATOM_ATOM,
2271 32, /* format */
2272 ARRAY_LENGTH(supported), supported);
2273
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002274 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
2275
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002276 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002277
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002278 weston_wm_dnd_init(wm);
2279
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002280 xcb_flush(wm->conn);
2281
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002282 wm->create_surface_listener.notify = weston_wm_create_surface;
2283 wl_signal_add(&wxs->compositor->create_surface_signal,
2284 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002285 wm->activate_listener.notify = weston_wm_window_activate;
2286 wl_signal_add(&wxs->compositor->activate_signal,
2287 &wm->activate_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002288 wm->kill_listener.notify = weston_wm_kill_client;
2289 wl_signal_add(&wxs->compositor->kill_signal,
2290 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002291 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002292
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002293 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002294 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002295
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002296 /* Create wm window and take WM_S0 selection last, which
2297 * signals to Xwayland that we're done with setup. */
2298 weston_wm_create_wm_window(wm);
2299
2300 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002301
2302 return wm;
2303}
2304
2305void
2306weston_wm_destroy(struct weston_wm *wm)
2307{
2308 /* FIXME: Free windows in hash. */
2309 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002310 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002311 xcb_disconnect(wm->conn);
2312 wl_event_source_remove(wm->source);
2313 wl_list_remove(&wm->selection_listener.link);
2314 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002315 wl_list_remove(&wm->kill_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002316 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002317
2318 free(wm);
2319}
2320
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002321static struct weston_wm_window *
2322get_wm_window(struct weston_surface *surface)
2323{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002324 struct wl_listener *listener;
2325
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002326 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002327 if (listener)
2328 return container_of(listener, struct weston_wm_window,
2329 surface_destroy_listener);
2330
2331 return NULL;
2332}
2333
2334static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002335weston_wm_window_configure(void *data)
2336{
2337 struct weston_wm_window *window = data;
2338 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002339 uint32_t values[4];
2340 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002341
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002342 weston_wm_window_get_child_position(window, &x, &y);
2343 values[0] = x;
2344 values[1] = y;
2345 values[2] = window->width;
2346 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002347 xcb_configure_window(wm->conn,
2348 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002349 XCB_CONFIG_WINDOW_X |
2350 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002351 XCB_CONFIG_WINDOW_WIDTH |
2352 XCB_CONFIG_WINDOW_HEIGHT,
2353 values);
2354
2355 weston_wm_window_get_frame_size(window, &width, &height);
2356 values[0] = width;
2357 values[1] = height;
2358 xcb_configure_window(wm->conn,
2359 window->frame_id,
2360 XCB_CONFIG_WINDOW_WIDTH |
2361 XCB_CONFIG_WINDOW_HEIGHT,
2362 values);
2363
2364 window->configure_source = NULL;
2365
2366 weston_wm_window_schedule_repaint(window);
2367}
2368
2369static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002370send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002371{
2372 struct weston_wm_window *window = get_wm_window(surface);
2373 struct weston_wm *wm = window->wm;
2374 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002375 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002376
Marek Chalupae9fe4672014-10-29 13:44:44 +01002377 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002378 hborder = 2 * t->width;
2379 vborder = t->titlebar_height + t->width;
2380 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002381 hborder = 0;
2382 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002383 }
2384
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002385 if (width > hborder)
2386 window->width = width - hborder;
2387 else
2388 window->width = 1;
2389
2390 if (height > vborder)
2391 window->height = height - vborder;
2392 else
2393 window->height = 1;
2394
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002395 if (window->frame)
2396 frame_resize_inside(window->frame, window->width, window->height);
2397
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002398 if (window->configure_source)
2399 return;
2400
2401 window->configure_source =
2402 wl_event_loop_add_idle(wm->server->loop,
2403 weston_wm_window_configure, window);
2404}
2405
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002406static void
2407send_position(struct weston_surface *surface, int32_t x, int32_t y)
2408{
2409 struct weston_wm_window *window = get_wm_window(surface);
2410 struct weston_wm *wm;
2411 uint32_t mask, values[2];
2412
2413 if (!window || !window->wm)
2414 return;
2415
2416 wm = window->wm;
2417 /* We use pos_dirty to tell whether a configure message is in flight.
2418 * This is needed in case we send two configure events in a very
2419 * short time, since window->x/y is set in after a roundtrip, hence
2420 * we cannot just check if the current x and y are different. */
2421 if (window->x != x || window->y != y || window->pos_dirty) {
2422 window->pos_dirty = true;
2423 values[0] = x;
2424 values[1] = y;
2425 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
2426
2427 xcb_configure_window(wm->conn, window->frame_id, mask, values);
2428 xcb_flush(wm->conn);
2429 }
2430}
2431
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002432static const struct weston_shell_client shell_client = {
Giulio Camuffof05d18f2015-12-11 20:57:05 +02002433 send_configure,
2434 send_position
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002435};
2436
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002437static int
2438legacy_fullscreen(struct weston_wm *wm,
2439 struct weston_wm_window *window,
2440 struct weston_output **output_ret)
2441{
2442 struct weston_compositor *compositor = wm->server->compositor;
2443 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002444 uint32_t minmax = PMinSize | PMaxSize;
2445 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002446
2447 /* Heuristics for detecting legacy fullscreen windows... */
2448
2449 wl_list_for_each(output, &compositor->output_list, link) {
2450 if (output->x == window->x &&
2451 output->y == window->y &&
2452 output->width == window->width &&
2453 output->height == window->height &&
2454 window->override_redirect) {
2455 *output_ret = output;
2456 return 1;
2457 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002458
2459 matching_size = 0;
2460 if ((window->size_hints.flags & (USSize |PSize)) &&
2461 window->size_hints.width == output->width &&
2462 window->size_hints.height == output->height)
2463 matching_size = 1;
2464 if ((window->size_hints.flags & minmax) == minmax &&
2465 window->size_hints.min_width == output->width &&
2466 window->size_hints.min_height == output->height &&
2467 window->size_hints.max_width == output->width &&
2468 window->size_hints.max_height == output->height)
2469 matching_size = 1;
2470
2471 if (matching_size && !window->decorate &&
2472 (window->size_hints.flags & (USPosition | PPosition)) &&
2473 window->size_hints.x == output->x &&
2474 window->size_hints.y == output->y) {
2475 *output_ret = output;
2476 return 1;
2477 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002478 }
2479
2480 return 0;
2481}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002482
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002483static bool
2484weston_wm_window_type_inactive(struct weston_wm_window *window)
2485{
2486 struct weston_wm *wm = window->wm;
2487
2488 return window->type == wm->atom.net_wm_window_type_tooltip ||
2489 window->type == wm->atom.net_wm_window_type_dropdown ||
2490 window->type == wm->atom.net_wm_window_type_dnd ||
2491 window->type == wm->atom.net_wm_window_type_combo ||
Giulio Camuffo84787ea2015-04-29 19:00:48 +03002492 window->type == wm->atom.net_wm_window_type_popup ||
2493 window->type == wm->atom.net_wm_window_type_utility;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002494}
2495
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002496static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002497xserver_map_shell_surface(struct weston_wm_window *window,
2498 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002499{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002500 struct weston_wm *wm = window->wm;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002501 struct weston_shell_interface *shell_interface =
2502 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002503 struct weston_output *output;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002504 struct weston_wm_window *parent;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002505 int flags = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002506
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002507 weston_wm_window_read_properties(window);
2508
2509 /* A weston_wm_window may have many different surfaces assigned
2510 * throughout its life, so we must make sure to remove the listener
2511 * from the old surface signal list. */
2512 if (window->surface)
2513 wl_list_remove(&window->surface_destroy_listener.link);
2514
2515 window->surface = surface;
2516 window->surface_destroy_listener.notify = surface_destroy;
2517 wl_signal_add(&window->surface->destroy_signal,
2518 &window->surface_destroy_listener);
2519
2520 weston_wm_window_schedule_repaint(window);
2521
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002522 if (!shell_interface->create_shell_surface)
2523 return;
2524
Pekka Paalanen50b67472014-10-01 15:02:41 +03002525 if (window->surface->configure) {
2526 weston_log("warning, unexpected in %s: "
2527 "surface's configure hook is already set.\n",
2528 __func__);
2529 return;
2530 }
2531
Murray Calavera883ac022015-06-06 13:02:22 +00002532 window->shsurf =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002533 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002534 window->surface,
2535 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002536
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002537 if (window->name)
2538 shell_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002539 if (window->pid > 0)
2540 shell_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002541
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002542 if (window->fullscreen) {
2543 window->saved_width = window->width;
2544 window->saved_height = window->height;
2545 shell_interface->set_fullscreen(window->shsurf,
2546 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2547 0, NULL);
2548 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002549 } else if (legacy_fullscreen(wm, window, &output)) {
2550 window->fullscreen = 1;
2551 shell_interface->set_fullscreen(window->shsurf,
2552 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2553 0, output);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002554 } else if (window->override_redirect) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002555 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002556 window->x,
2557 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002558 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Axel Davye4450f92014-01-12 15:06:05 +01002559 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002560 parent = window->transient_for;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002561 if (weston_wm_window_type_inactive(window))
2562 flags = WL_SHELL_SURFACE_TRANSIENT_INACTIVE;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002563 shell_interface->set_transient(window->shsurf,
2564 parent->surface,
Axel Davyfa506b62014-01-12 15:06:04 +01002565 window->x - parent->x,
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002566 window->y - parent->y, flags);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002567 } else if (weston_wm_window_is_maximized(window)) {
2568 shell_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002569 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002570 if (weston_wm_window_type_inactive(window)) {
2571 shell_interface->set_xwayland(window->shsurf,
2572 window->x,
2573 window->y,
2574 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2575 } else {
2576 shell_interface->set_toplevel(window->shsurf);
2577 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002578 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002579}