blob: 9398d24ef88ece423c976b87c6e40e7d941df9b0 [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;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500148 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400149 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300150 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500151 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500152 int has_alpha;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700153 int delete_window;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200154 int maximized_vert;
155 int maximized_horz;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700156 struct wm_size_hints size_hints;
157 struct motif_wm_hints motif_hints;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700158 struct wl_list link;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400159};
160
161static struct weston_wm_window *
162get_wm_window(struct weston_surface *surface);
163
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400164static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200165weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window);
166
167static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400168weston_wm_window_schedule_repaint(struct weston_wm_window *window);
169
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700170static void
171xserver_map_shell_surface(struct weston_wm_window *window,
172 struct weston_surface *surface);
173
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400174static int __attribute__ ((format (printf, 1, 2)))
175wm_log(const char *fmt, ...)
176{
177#ifdef WM_DEBUG
178 int l;
179 va_list argp;
180
181 va_start(argp, fmt);
182 l = weston_vlog(fmt, argp);
183 va_end(argp);
184
185 return l;
186#else
187 return 0;
188#endif
189}
190
191static int __attribute__ ((format (printf, 1, 2)))
192wm_log_continue(const char *fmt, ...)
193{
194#ifdef WM_DEBUG
195 int l;
196 va_list argp;
197
198 va_start(argp, fmt);
199 l = weston_vlog_continue(fmt, argp);
200 va_end(argp);
201
202 return l;
203#else
204 return 0;
205#endif
206}
207
Derek Foreman49372142015-04-09 10:51:22 -0500208static bool __attribute__ ((warn_unused_result))
209wm_lookup_window(struct weston_wm *wm, xcb_window_t hash,
210 struct weston_wm_window **window)
211{
212 *window = hash_table_lookup(wm->window_hash, hash);
213 if (*window)
214 return true;
215 return false;
216}
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400217
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400218const char *
219get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
220{
221 xcb_get_atom_name_cookie_t cookie;
222 xcb_get_atom_name_reply_t *reply;
223 xcb_generic_error_t *e;
224 static char buffer[64];
225
226 if (atom == XCB_ATOM_NONE)
227 return "None";
228
229 cookie = xcb_get_atom_name (c, atom);
230 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500231
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300232 if (reply) {
MoD55375b92013-06-11 19:59:42 -0500233 snprintf(buffer, sizeof buffer, "%.*s",
234 xcb_get_atom_name_name_length (reply),
235 xcb_get_atom_name_name (reply));
236 } else {
237 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
238 }
239
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400240 free(reply);
241
242 return buffer;
243}
244
Tiago Vignatti90fada42012-07-16 12:02:08 -0400245static xcb_cursor_t
246xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
247{
248 xcb_connection_t *c = wm->conn;
249 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
250 xcb_screen_t *screen = s.data;
251 xcb_gcontext_t gc;
252 xcb_pixmap_t pix;
253 xcb_render_picture_t pic;
254 xcb_cursor_t cursor;
255 int stride = img->width * 4;
256
257 pix = xcb_generate_id(c);
258 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
259
260 pic = xcb_generate_id(c);
261 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
262
263 gc = xcb_generate_id(c);
264 xcb_create_gc(c, gc, pix, 0, 0);
265
266 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
267 img->width, img->height, 0, 0, 0, 32,
268 stride * img->height, (uint8_t *) img->pixels);
269 xcb_free_gc(c, gc);
270
271 cursor = xcb_generate_id(c);
272 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
273
274 xcb_render_free_picture(c, pic);
275 xcb_free_pixmap(c, pix);
276
277 return cursor;
278}
279
280static xcb_cursor_t
281xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
282{
283 /* TODO: treat animated cursors as well */
284 if (images->nimage != 1)
285 return -1;
286
287 return xcb_cursor_image_load_cursor(wm, images->images[0]);
288}
289
290static xcb_cursor_t
291xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
292{
293 xcb_cursor_t cursor;
294 XcursorImages *images;
295 char *v = NULL;
296 int size = 0;
297
298 if (!file)
299 return 0;
300
301 v = getenv ("XCURSOR_SIZE");
302 if (v)
303 size = atoi(v);
304
305 if (!size)
306 size = 32;
307
308 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300309 if (!images)
310 return -1;
311
Tiago Vignatti90fada42012-07-16 12:02:08 -0400312 cursor = xcb_cursor_images_load_cursor (wm, images);
313 XcursorImagesDestroy (images);
314
315 return cursor;
316}
317
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400318void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400319dump_property(struct weston_wm *wm,
320 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400321{
322 int32_t *incr_value;
323 const char *text_value, *name;
324 xcb_atom_t *atom_value;
325 int width, len;
326 uint32_t i;
327
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400328 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400329 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400330 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400331 return;
332 }
333
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400334 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
335 get_atom_name(wm->conn, reply->type),
336 reply->format,
337 xcb_get_property_value_length(reply),
338 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400339
340 if (reply->type == wm->atom.incr) {
341 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400342 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400343 } else if (reply->type == wm->atom.utf8_string ||
344 reply->type == wm->atom.string) {
345 text_value = xcb_get_property_value(reply);
346 if (reply->value_len > 40)
347 len = 40;
348 else
349 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400350 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400351 } else if (reply->type == XCB_ATOM_ATOM) {
352 atom_value = xcb_get_property_value(reply);
353 for (i = 0; i < reply->value_len; i++) {
354 name = get_atom_name(wm->conn, atom_value[i]);
355 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400356 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400357 width = 4;
358 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400359 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400360 }
361
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400362 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400363 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400364 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400365 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400366 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400367 }
368}
369
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200370static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400371read_and_dump_property(struct weston_wm *wm,
372 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400373{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400374 xcb_get_property_reply_t *reply;
375 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400376
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400377 cookie = xcb_get_property(wm->conn, 0, window,
378 property, XCB_ATOM_ANY, 0, 2048);
379 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400380
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400381 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400382
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400383 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400384}
385
386/* We reuse some predefined, but otherwise useles atoms */
387#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
388#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500389#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700390#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400391
392static void
393weston_wm_window_read_properties(struct weston_wm_window *window)
394{
395 struct weston_wm *wm = window->wm;
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200396 struct weston_shell_interface *shell_interface =
397 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400398
399#define F(field) offsetof(struct weston_wm_window, field)
400 const struct {
401 xcb_atom_t atom;
402 xcb_atom_t type;
403 int offset;
404 } props[] = {
405 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
406 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
407 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
408 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700409 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500410 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400411 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
412 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300413 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400414 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300415 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400416 };
417#undef F
418
419 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
420 xcb_get_property_reply_t *reply;
421 void *p;
422 uint32_t *xid;
423 xcb_atom_t *atom;
424 uint32_t i;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200425 char name[1024];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400426
427 if (!window->properties_dirty)
428 return;
429 window->properties_dirty = 0;
430
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400431 for (i = 0; i < ARRAY_LENGTH(props); i++)
432 cookie[i] = xcb_get_property(wm->conn,
433 0, /* delete */
434 window->id,
435 props[i].atom,
436 XCB_ATOM_ANY, 0, 2048);
437
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700438 window->decorate = window->override_redirect ? 0 : MWM_DECOR_EVERYTHING;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700439 window->size_hints.flags = 0;
440 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700441 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700442
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400443 for (i = 0; i < ARRAY_LENGTH(props); i++) {
444 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
445 if (!reply)
446 /* Bad window, typically */
447 continue;
448 if (reply->type == XCB_ATOM_NONE) {
449 /* No such property */
450 free(reply);
451 continue;
452 }
453
454 p = ((char *) window + props[i].offset);
455
456 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300457 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400458 case XCB_ATOM_STRING:
459 /* FIXME: We're using this for both string and
460 utf8_string */
461 if (*(char **) p)
462 free(*(char **) p);
463
464 *(char **) p =
465 strndup(xcb_get_property_value(reply),
466 xcb_get_property_value_length(reply));
467 break;
468 case XCB_ATOM_WINDOW:
469 xid = xcb_get_property_value(reply);
Derek Foreman49372142015-04-09 10:51:22 -0500470 if (!wm_lookup_window(wm, *xid, p))
471 weston_log("XCB_ATOM_WINDOW contains window"
472 " id not found in hash table.\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400473 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300474 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400475 case XCB_ATOM_ATOM:
476 atom = xcb_get_property_value(reply);
477 *(xcb_atom_t *) p = *atom;
478 break;
479 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700480 atom = xcb_get_property_value(reply);
481 for (i = 0; i < reply->value_len; i++)
Derek Foremanb4deec62015-04-07 12:12:13 -0500482 if (atom[i] == wm->atom.wm_delete_window) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700483 window->delete_window = 1;
Derek Foremanb4deec62015-04-07 12:12:13 -0500484 break;
485 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400486 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700487 case TYPE_WM_NORMAL_HINTS:
488 memcpy(&window->size_hints,
489 xcb_get_property_value(reply),
490 sizeof window->size_hints);
491 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500492 case TYPE_NET_WM_STATE:
493 window->fullscreen = 0;
494 atom = xcb_get_property_value(reply);
Ryo Munakataf3744f52015-03-11 17:36:30 +0900495 for (i = 0; i < reply->value_len; i++) {
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500496 if (atom[i] == wm->atom.net_wm_state_fullscreen)
497 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200498 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
499 window->maximized_vert = 1;
500 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
501 window->maximized_horz = 1;
Ryo Munakataf3744f52015-03-11 17:36:30 +0900502 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500503 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400504 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700505 memcpy(&window->motif_hints,
506 xcb_get_property_value(reply),
507 sizeof window->motif_hints);
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700508 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) {
509 if (window->motif_hints.decorations & MWM_DECOR_ALL)
510 /* MWM_DECOR_ALL means all except the other values listed. */
511 window->decorate =
512 MWM_DECOR_EVERYTHING & (~window->motif_hints.decorations);
513 else
514 window->decorate =
515 window->motif_hints.decorations;
516 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400517 break;
518 default:
519 break;
520 }
521 free(reply);
522 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200523
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200524 if (window->pid > 0) {
525 gethostname(name, sizeof(name));
526 for (i = 0; i < sizeof(name); i++) {
527 if (name[i] == '\0')
528 break;
529 }
530 if (i == sizeof(name))
531 name[0] = '\0'; /* ignore stupid hostnames */
532
533 /* this is only one heuristic to guess the PID of a client is
534 * valid, assuming it's compliant with icccm and ewmh.
535 * Non-compliants and remote applications of course fail. */
536 if (!window->machine || strcmp(window->machine, name))
537 window->pid = 0;
538 }
539
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200540 if (window->shsurf && window->name)
541 shell_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500542 if (window->frame && window->name)
543 frame_set_title(window->frame, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200544 if (window->shsurf && window->pid > 0)
545 shell_interface->set_pid(window->shsurf, window->pid);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400546}
547
548static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400549weston_wm_window_get_frame_size(struct weston_wm_window *window,
550 int *width, int *height)
551{
552 struct theme *t = window->wm->theme;
553
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500554 if (window->fullscreen) {
555 *width = window->width;
556 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800557 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500558 *width = frame_width(window->frame);
559 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400560 } else {
561 *width = window->width + t->margin * 2;
562 *height = window->height + t->margin * 2;
563 }
564}
565
566static void
567weston_wm_window_get_child_position(struct weston_wm_window *window,
568 int *x, int *y)
569{
570 struct theme *t = window->wm->theme;
571
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500572 if (window->fullscreen) {
573 *x = 0;
574 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800575 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500576 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400577 } else {
578 *x = t->margin;
579 *y = t->margin;
580 }
581}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400582
583static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500584weston_wm_window_send_configure_notify(struct weston_wm_window *window)
585{
586 xcb_configure_notify_event_t configure_notify;
587 struct weston_wm *wm = window->wm;
588 int x, y;
589
590 weston_wm_window_get_child_position(window, &x, &y);
591 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
592 configure_notify.pad0 = 0;
593 configure_notify.event = window->id;
594 configure_notify.window = window->id;
595 configure_notify.above_sibling = XCB_WINDOW_NONE;
596 configure_notify.x = x;
597 configure_notify.y = y;
598 configure_notify.width = window->width;
599 configure_notify.height = window->height;
600 configure_notify.border_width = 0;
601 configure_notify.override_redirect = 0;
602 configure_notify.pad1 = 0;
603
Murray Calavera883ac022015-06-06 13:02:22 +0000604 xcb_send_event(wm->conn, 0, window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500605 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
606 (char *) &configure_notify);
607}
608
609static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400610weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
611{
Murray Calavera883ac022015-06-06 13:02:22 +0000612 xcb_configure_request_event_t *configure_request =
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400613 (xcb_configure_request_event_t *) event;
614 struct weston_wm_window *window;
615 uint32_t mask, values[16];
616 int x, y, width, height, i = 0;
617
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400618 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
619 configure_request->window,
620 configure_request->x, configure_request->y,
621 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400622
Derek Foreman49372142015-04-09 10:51:22 -0500623 if (!wm_lookup_window(wm, configure_request->window, &window))
624 return;
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400625
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500626 if (window->fullscreen) {
627 weston_wm_window_send_configure_notify(window);
628 return;
629 }
630
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400631 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
632 window->width = configure_request->width;
633 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
634 window->height = configure_request->height;
635
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500636 if (window->frame)
637 frame_resize_inside(window->frame, window->width, window->height);
638
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400639 weston_wm_window_get_child_position(window, &x, &y);
640 values[i++] = x;
641 values[i++] = y;
642 values[i++] = window->width;
643 values[i++] = window->height;
644 values[i++] = 0;
645 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
646 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
647 XCB_CONFIG_WINDOW_BORDER_WIDTH;
648 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
649 values[i++] = configure_request->sibling;
650 mask |= XCB_CONFIG_WINDOW_SIBLING;
651 }
652 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
653 values[i++] = configure_request->stack_mode;
654 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
655 }
656
657 xcb_configure_window(wm->conn, window->id, mask, values);
658
659 weston_wm_window_get_frame_size(window, &width, &height);
660 values[0] = width;
661 values[1] = height;
662 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
663 xcb_configure_window(wm->conn, window->frame_id, mask, values);
664
665 weston_wm_window_schedule_repaint(window);
666}
667
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400668static int
669our_resource(struct weston_wm *wm, uint32_t id)
670{
671 const xcb_setup_t *setup;
672
673 setup = xcb_get_setup(wm->conn);
674
675 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
676}
677
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400678static void
679weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
680{
Murray Calavera883ac022015-06-06 13:02:22 +0000681 xcb_configure_notify_event_t *configure_notify =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400682 (xcb_configure_notify_event_t *) event;
683 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400684
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400685 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400686 configure_notify->window,
687 configure_notify->x, configure_notify->y,
688 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300689
Derek Foreman49372142015-04-09 10:51:22 -0500690 if (!wm_lookup_window(wm, configure_notify->window, &window))
691 return;
692
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700693 window->x = configure_notify->x;
694 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700695 if (window->override_redirect) {
696 window->width = configure_notify->width;
697 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500698 if (window->frame)
699 frame_resize_inside(window->frame,
700 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700701 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400702}
703
704static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300705weston_wm_kill_client(struct wl_listener *listener, void *data)
706{
707 struct weston_surface *surface = data;
708 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300709 if (!window)
710 return;
711
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200712 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300713 kill(window->pid, SIGKILL);
714}
715
716static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700717weston_wm_create_surface(struct wl_listener *listener, void *data)
718{
719 struct weston_surface *surface = data;
720 struct weston_wm *wm =
721 container_of(listener,
722 struct weston_wm, create_surface_listener);
723 struct weston_wm_window *window;
724
725 if (wl_resource_get_client(surface->resource) != wm->server->client)
726 return;
727
728 wl_list_for_each(window, &wm->unpaired_window_list, link)
729 if (window->surface_id ==
730 wl_resource_get_id(surface->resource)) {
731 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700732 window->surface_id = 0;
733 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700734 break;
Murray Calavera883ac022015-06-06 13:02:22 +0000735 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700736}
737
738static void
Giulio Camuffob18f7882015-03-29 14:20:23 +0300739weston_wm_send_focus_window(struct weston_wm *wm,
740 struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400741{
Scott Moreau85ecac02012-05-21 15:49:14 -0600742 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400743
Scott Moreau85ecac02012-05-21 15:49:14 -0600744 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700745 uint32_t values[1];
746
Boyan Dingb9f863c2014-08-30 10:33:23 +0800747 if (window->override_redirect)
748 return;
749
Scott Moreau85ecac02012-05-21 15:49:14 -0600750 client_message.response_type = XCB_CLIENT_MESSAGE;
751 client_message.format = 32;
752 client_message.window = window->id;
753 client_message.type = wm->atom.wm_protocols;
754 client_message.data.data32[0] = wm->atom.wm_take_focus;
755 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
756
Murray Calavera883ac022015-06-06 13:02:22 +0000757 xcb_send_event(wm->conn, 0, window->id,
Scott Moreau85ecac02012-05-21 15:49:14 -0600758 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
759 (char *) &client_message);
760
761 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
762 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700763
764 values[0] = XCB_STACK_MODE_ABOVE;
765 xcb_configure_window (wm->conn, window->frame_id,
766 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600767 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400768 xcb_set_input_focus (wm->conn,
769 XCB_INPUT_FOCUS_POINTER_ROOT,
770 XCB_NONE,
771 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600772 }
Giulio Camuffob18f7882015-03-29 14:20:23 +0300773}
774
775static void
776weston_wm_window_activate(struct wl_listener *listener, void *data)
777{
778 struct weston_surface *surface = data;
779 struct weston_wm_window *window = NULL;
780 struct weston_wm *wm =
781 container_of(listener, struct weston_wm, activate_listener);
782
783 if (surface) {
784 window = get_wm_window(surface);
785 }
786
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200787 if (window) {
788 weston_wm_set_net_active_window(wm, window->id);
789 } else {
790 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
791 }
792
Giulio Camuffob18f7882015-03-29 14:20:23 +0300793 weston_wm_send_focus_window(wm, window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400794
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500795 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800796 if (wm->focus_window->frame)
797 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400798 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500799 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400800 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500801 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800802 if (wm->focus_window->frame)
803 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400804 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500805 }
Benoit Gschwind1a42ca12015-09-25 21:26:04 +0200806
807 xcb_flush(wm->conn);
808
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400809}
810
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300811static void
812weston_wm_window_transform(struct wl_listener *listener, void *data)
813{
814 struct weston_surface *surface = data;
815 struct weston_wm_window *window = get_wm_window(surface);
816 struct weston_wm *wm =
817 container_of(listener, struct weston_wm, transform_listener);
Giulio Camuffoaa974782015-02-01 16:18:51 +0200818 struct weston_view *view;
819 struct weston_shell_interface *shell_interface =
820 &wm->server->compositor->shell_interface;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300821 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300822
Giulio Camuffoaa974782015-02-01 16:18:51 +0200823 if (!window || !wm || !window->shsurf)
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300824 return;
825
Giulio Camuffoaa974782015-02-01 16:18:51 +0200826 if (!shell_interface->get_primary_view)
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300827 return;
828
Giulio Camuffoaa974782015-02-01 16:18:51 +0200829 view = shell_interface->get_primary_view(shell_interface->shell,
830 window->shsurf);
831
832 if (!view || !weston_view_is_mapped(view))
833 return;
834
835 if (window->x != view->geometry.x ||
836 window->y != view->geometry.y) {
837 values[0] = view->geometry.x;
838 values[1] = view->geometry.y;
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700839 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300840
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700841 xcb_configure_window(wm->conn, window->frame_id, mask, values);
842 xcb_flush(wm->conn);
843 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300844}
845
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400846#define ICCCM_WITHDRAWN_STATE 0
847#define ICCCM_NORMAL_STATE 1
848#define ICCCM_ICONIC_STATE 3
849
850static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500851weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400852{
853 struct weston_wm *wm = window->wm;
854 uint32_t property[2];
855
856 property[0] = state;
857 property[1] = XCB_WINDOW_NONE;
858
859 xcb_change_property(wm->conn,
860 XCB_PROP_MODE_REPLACE,
861 window->id,
862 wm->atom.wm_state,
863 wm->atom.wm_state,
864 32, /* format */
865 2, property);
866}
867
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400868static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500869weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
870{
871 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200872 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500873 int i;
874
875 i = 0;
876 if (window->fullscreen)
877 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200878 if (window->maximized_vert)
879 property[i++] = wm->atom.net_wm_state_maximized_vert;
880 if (window->maximized_horz)
881 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500882
883 xcb_change_property(wm->conn,
884 XCB_PROP_MODE_REPLACE,
885 window->id,
886 wm->atom.net_wm_state,
887 XCB_ATOM_ATOM,
888 32, /* format */
889 i, property);
890}
891
892static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700893weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400894{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700895 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400896 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400897 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200898 int buttons = FRAME_BUTTON_CLOSE;
899
900 if (window->decorate & MWM_DECOR_MAXIMIZE)
901 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400902
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500903 window->frame = frame_create(window->wm->theme,
904 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200905 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500906 frame_resize_inside(window->frame, window->width, window->height);
907
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400908 weston_wm_window_get_frame_size(window, &width, &height);
909 weston_wm_window_get_child_position(window, &x, &y);
910
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400911 values[0] = wm->screen->black_pixel;
912 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400913 XCB_EVENT_MASK_KEY_PRESS |
914 XCB_EVENT_MASK_KEY_RELEASE |
915 XCB_EVENT_MASK_BUTTON_PRESS |
916 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400917 XCB_EVENT_MASK_POINTER_MOTION |
918 XCB_EVENT_MASK_ENTER_WINDOW |
919 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400920 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400921 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400922 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400923
924 window->frame_id = xcb_generate_id(wm->conn);
925 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400926 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400927 window->frame_id,
928 wm->screen->root,
929 0, 0,
930 width, height,
931 0,
932 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400933 wm->visual_id,
934 XCB_CW_BORDER_PIXEL |
935 XCB_CW_EVENT_MASK |
936 XCB_CW_COLORMAP, values);
937
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400938 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
939
940 values[0] = 0;
941 xcb_configure_window(wm->conn, window->id,
942 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
943
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400944 window->cairo_surface =
945 cairo_xcb_surface_create_with_xrender_format(wm->conn,
946 wm->screen,
947 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400948 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400949 width, height);
950
951 hash_table_insert(wm->window_hash, window->frame_id, window);
952}
953
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200954/*
955 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
956 * Passing a <0 desktop value deletes the property.
957 */
958static void
959weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
Bryce Harringtone00554b2015-06-12 10:17:39 -0700960 int desktop)
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200961{
962 if (desktop >= 0) {
963 xcb_change_property(window->wm->conn,
964 XCB_PROP_MODE_REPLACE,
965 window->id,
966 window->wm->atom.net_wm_desktop,
967 XCB_ATOM_CARDINAL,
968 32, /* format */
969 1, &desktop);
970 } else {
971 xcb_delete_property(window->wm->conn,
972 window->id,
973 window->wm->atom.net_wm_desktop);
974 }
975}
976
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400977static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700978weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
979{
980 xcb_map_request_event_t *map_request =
981 (xcb_map_request_event_t *) event;
982 struct weston_wm_window *window;
983
984 if (our_resource(wm, map_request->window)) {
985 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
986 map_request->window);
987 return;
988 }
989
Derek Foreman49372142015-04-09 10:51:22 -0500990 if (!wm_lookup_window(wm, map_request->window, &window))
991 return;
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700992
993 weston_wm_window_read_properties(window);
994
995 if (window->frame_id == XCB_WINDOW_NONE)
996 weston_wm_window_create_frame(window);
997
998 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
999 window->id, window, window->frame_id);
1000
1001 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
1002 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001003 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -07001004
1005 xcb_map_window(wm->conn, map_request->window);
1006 xcb_map_window(wm->conn, window->frame_id);
1007}
1008
1009static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001010weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1011{
1012 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
1013
1014 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001015 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
1016 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001017 return;
1018 }
1019
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001020 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001021}
1022
1023static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001024weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1025{
1026 xcb_unmap_notify_event_t *unmap_notify =
1027 (xcb_unmap_notify_event_t *) event;
1028 struct weston_wm_window *window;
1029
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001030 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
1031 unmap_notify->window,
1032 unmap_notify->event,
1033 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001034
1035 if (our_resource(wm, unmap_notify->window))
1036 return;
1037
MoD31700122013-06-11 19:58:55 -05001038 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -04001039 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1040 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001041 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001042
Derek Foreman49372142015-04-09 10:51:22 -05001043 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1044 return;
1045
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001046 if (window->surface_id) {
1047 /* Make sure we're not on the unpaired surface list or we
1048 * could be assigned a surface during surface creation that
1049 * was mapped before this unmap request.
1050 */
1051 wl_list_remove(&window->link);
1052 window->surface_id = 0;
1053 }
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001054 if (wm->focus_window == window)
1055 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001056 if (window->surface)
1057 wl_list_remove(&window->surface_destroy_listener.link);
1058 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +02001059 window->shsurf = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001060
1061 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1062 weston_wm_window_set_virtual_desktop(window, -1);
1063
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001064 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001065}
1066
1067static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001068weston_wm_window_draw_decoration(void *data)
1069{
1070 struct weston_wm_window *window = data;
1071 struct weston_wm *wm = window->wm;
1072 struct theme *t = wm->theme;
1073 cairo_t *cr;
1074 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001075 int32_t input_x, input_y, input_w, input_h;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07001076 struct weston_shell_interface *shell_interface =
1077 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001078 uint32_t flags = 0;
Giulio Camuffoaa974782015-02-01 16:18:51 +02001079 struct weston_view *view;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001080
1081 weston_wm_window_read_properties(window);
1082
1083 window->repaint_source = NULL;
1084
1085 weston_wm_window_get_frame_size(window, &width, &height);
1086 weston_wm_window_get_child_position(window, &x, &y);
1087
1088 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1089 cr = cairo_create(window->cairo_surface);
1090
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001091 if (window->fullscreen) {
1092 /* nothing */
1093 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001094 if (wm->focus_window == window)
1095 flags |= THEME_FRAME_ACTIVE;
1096
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001097 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001098 } else {
1099 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1100 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1101 cairo_paint(cr);
1102
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001103 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001104 }
1105
1106 cairo_destroy(cr);
1107
1108 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001109 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001110 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001111 pixman_region32_init(&window->surface->pending.opaque);
1112 } else {
1113 /* We leave an extra pixel around the X window area to
1114 * make sure we don't sample from the undefined alpha
1115 * channel when filtering. */
Murray Calavera883ac022015-06-06 13:02:22 +00001116 pixman_region32_init_rect(&window->surface->pending.opaque,
MoD384a11a2013-06-22 11:04:21 -05001117 x - 1, y - 1,
1118 window->width + 2,
1119 window->height + 2);
1120 }
Giulio Camuffoaa974782015-02-01 16:18:51 +02001121 wl_list_for_each(view, &window->surface->views, surface_link)
1122 weston_view_geometry_dirty(view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001123
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001124 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001125
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001126 if (window->decorate && !window->fullscreen) {
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001127 frame_input_rect(window->frame, &input_x, &input_y,
1128 &input_w, &input_h);
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001129 } else {
1130 input_x = x;
1131 input_y = y;
1132 input_w = width;
1133 input_h = height;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001134 }
1135
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001136 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001137 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001138
1139 shell_interface->set_window_geometry(window->shsurf,
1140 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001141 }
1142}
1143
1144static void
1145weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1146{
1147 struct weston_wm *wm = window->wm;
Giulio Camuffoaa974782015-02-01 16:18:51 +02001148 struct weston_view *view;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001149 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001150
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001151 if (window->frame_id == XCB_WINDOW_NONE) {
1152 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001153 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001154 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001155 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001156 pixman_region32_init(&window->surface->pending.opaque);
1157 } else {
1158 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1159 width, height);
1160 }
Giulio Camuffoaa974782015-02-01 16:18:51 +02001161 wl_list_for_each(view, &window->surface->views, surface_link)
1162 weston_view_geometry_dirty(view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001163 }
1164 return;
1165 }
1166
1167 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001168 return;
1169
1170 window->repaint_source =
1171 wl_event_loop_add_idle(wm->server->loop,
1172 weston_wm_window_draw_decoration,
1173 window);
1174}
1175
1176static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001177weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1178{
1179 xcb_property_notify_event_t *property_notify =
1180 (xcb_property_notify_event_t *) event;
1181 struct weston_wm_window *window;
1182
Derek Foreman49372142015-04-09 10:51:22 -05001183 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001184 return;
1185
1186 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001187
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001188 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001189 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001190 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001191 else
1192 read_and_dump_property(wm, property_notify->window,
1193 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001194
1195 if (property_notify->atom == wm->atom.net_wm_name ||
1196 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001197 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001198}
1199
1200static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001201weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001202 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001203{
1204 struct weston_wm_window *window;
1205 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001206 xcb_get_geometry_cookie_t geometry_cookie;
1207 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001208
Peter Huttererf3d62272013-08-08 11:57:05 +10001209 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001210 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001211 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001212 return;
1213 }
1214
MoD384a11a2013-06-22 11:04:21 -05001215 geometry_cookie = xcb_get_geometry(wm->conn, id);
1216
Giulio Camuffob18f7882015-03-29 14:20:23 +03001217 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
1218 XCB_EVENT_MASK_FOCUS_CHANGE;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001219 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1220
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001221 window->wm = wm;
1222 window->id = id;
1223 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001224 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001225 window->width = width;
1226 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001227 window->x = x;
1228 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001229
MoD384a11a2013-06-22 11:04:21 -05001230 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1231 /* technically we should use XRender and check the visual format's
1232 alpha_mask, but checking depth is simpler and works in all known cases */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001233 if (geometry_reply != NULL)
MoD384a11a2013-06-22 11:04:21 -05001234 window->has_alpha = geometry_reply->depth == 32;
1235 free(geometry_reply);
1236
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001237 hash_table_insert(wm->window_hash, id, window);
1238}
1239
1240static void
1241weston_wm_window_destroy(struct weston_wm_window *window)
1242{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001243 struct weston_wm *wm = window->wm;
1244
1245 if (window->repaint_source)
1246 wl_event_source_remove(window->repaint_source);
1247 if (window->cairo_surface)
1248 cairo_surface_destroy(window->cairo_surface);
1249
1250 if (window->frame_id) {
1251 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1252 xcb_destroy_window(wm->conn, window->frame_id);
1253 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001254 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001255 hash_table_remove(wm->window_hash, window->frame_id);
1256 window->frame_id = XCB_WINDOW_NONE;
1257 }
1258
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001259 if (window->surface_id)
1260 wl_list_remove(&window->link);
1261
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001262 if (window->surface)
1263 wl_list_remove(&window->surface_destroy_listener.link);
1264
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001265 hash_table_remove(window->wm->window_hash, window->id);
1266 free(window);
1267}
1268
1269static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001270weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1271{
1272 xcb_create_notify_event_t *create_notify =
1273 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001274
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001275 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1276 create_notify->window,
1277 create_notify->width, create_notify->height,
1278 create_notify->override_redirect ? ", override" : "",
1279 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001280
1281 if (our_resource(wm, create_notify->window))
1282 return;
1283
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001284 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001285 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001286 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001287 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001288}
1289
1290static void
1291weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1292{
1293 xcb_destroy_notify_event_t *destroy_notify =
1294 (xcb_destroy_notify_event_t *) event;
1295 struct weston_wm_window *window;
1296
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001297 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1298 destroy_notify->window,
1299 destroy_notify->event,
1300 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001301
1302 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001303 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001304
Derek Foreman49372142015-04-09 10:51:22 -05001305 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1306 return;
1307
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001308 weston_wm_window_destroy(window);
1309}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001310
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001311static void
1312weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1313{
1314 xcb_reparent_notify_event_t *reparent_notify =
1315 (xcb_reparent_notify_event_t *) event;
1316 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001317
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001318 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1319 reparent_notify->window,
1320 reparent_notify->parent,
1321 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001322
1323 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001324 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001325 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001326 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001327 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001328 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1329 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001330 weston_wm_window_destroy(window);
1331 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001332}
1333
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001334struct weston_seat *
1335weston_wm_pick_seat(struct weston_wm *wm)
1336{
1337 return container_of(wm->server->compositor->seat_list.next,
1338 struct weston_seat, link);
1339}
1340
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001341static struct weston_seat *
1342weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1343{
1344 struct weston_wm *wm = window->wm;
1345 struct weston_seat *seat, *s;
1346
1347 seat = NULL;
1348 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001349 struct weston_pointer *pointer = weston_seat_get_pointer(s);
1350 struct weston_pointer *old_pointer =
1351 weston_seat_get_pointer(seat);
1352
1353 if (pointer && pointer->focus &&
1354 pointer->focus->surface == window->surface &&
1355 pointer->button_count > 0 &&
1356 (!seat ||
1357 pointer->grab_serial -
1358 old_pointer->grab_serial < (1 << 30)))
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001359 seat = s;
1360 }
1361
1362 return seat;
1363}
1364
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001365static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001366weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1367 xcb_client_message_event_t *client_message)
1368{
1369 static const int map[] = {
1370 THEME_LOCATION_RESIZING_TOP_LEFT,
1371 THEME_LOCATION_RESIZING_TOP,
1372 THEME_LOCATION_RESIZING_TOP_RIGHT,
1373 THEME_LOCATION_RESIZING_RIGHT,
1374 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1375 THEME_LOCATION_RESIZING_BOTTOM,
1376 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1377 THEME_LOCATION_RESIZING_LEFT
1378 };
1379
1380 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001381 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Derek Foreman1281a362015-07-31 16:55:32 -05001382 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001383 int detail;
1384 struct weston_shell_interface *shell_interface =
1385 &wm->server->compositor->shell_interface;
1386
Derek Foreman1281a362015-07-31 16:55:32 -05001387 if (!pointer || pointer->button_count != 1
1388 || !pointer->focus
1389 || pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001390 return;
1391
1392 detail = client_message->data.data32[2];
1393 switch (detail) {
1394 case _NET_WM_MOVERESIZE_MOVE:
Derek Foremane4d6c832015-08-05 14:48:11 -05001395 shell_interface->move(window->shsurf, pointer);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001396 break;
1397 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1398 case _NET_WM_MOVERESIZE_SIZE_TOP:
1399 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1400 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1401 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1402 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1403 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1404 case _NET_WM_MOVERESIZE_SIZE_LEFT:
Derek Foremane4d6c832015-08-05 14:48:11 -05001405 shell_interface->resize(window->shsurf, pointer, map[detail]);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001406 break;
1407 case _NET_WM_MOVERESIZE_CANCEL:
1408 break;
1409 }
1410}
1411
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001412#define _NET_WM_STATE_REMOVE 0
1413#define _NET_WM_STATE_ADD 1
1414#define _NET_WM_STATE_TOGGLE 2
1415
1416static int
1417update_state(int action, int *state)
1418{
1419 int new_state, changed;
1420
1421 switch (action) {
1422 case _NET_WM_STATE_REMOVE:
1423 new_state = 0;
1424 break;
1425 case _NET_WM_STATE_ADD:
1426 new_state = 1;
1427 break;
1428 case _NET_WM_STATE_TOGGLE:
1429 new_state = !*state;
1430 break;
1431 default:
1432 return 0;
1433 }
1434
1435 changed = (*state != new_state);
1436 *state = new_state;
1437
1438 return changed;
1439}
1440
1441static void
1442weston_wm_window_configure(void *data);
1443
1444static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001445weston_wm_window_set_toplevel(struct weston_wm_window *window)
1446{
1447 struct weston_shell_interface *shell_interface =
1448 &window->wm->server->compositor->shell_interface;
1449
1450 shell_interface->set_toplevel(window->shsurf);
1451 window->width = window->saved_width;
1452 window->height = window->saved_height;
1453 if (window->frame)
1454 frame_resize_inside(window->frame,
1455 window->width,
1456 window->height);
1457 weston_wm_window_configure(window);
1458}
1459
1460static inline bool
1461weston_wm_window_is_maximized(struct weston_wm_window *window)
1462{
1463 return window->maximized_horz && window->maximized_vert;
1464}
1465
1466static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001467weston_wm_window_handle_state(struct weston_wm_window *window,
1468 xcb_client_message_event_t *client_message)
1469{
1470 struct weston_wm *wm = window->wm;
1471 struct weston_shell_interface *shell_interface =
1472 &wm->server->compositor->shell_interface;
1473 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001474 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001475
1476 action = client_message->data.data32[0];
1477 property = client_message->data.data32[1];
1478
1479 if (property == wm->atom.net_wm_state_fullscreen &&
1480 update_state(action, &window->fullscreen)) {
1481 weston_wm_window_set_net_wm_state(window);
1482 if (window->fullscreen) {
1483 window->saved_width = window->width;
1484 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001485
1486 if (window->shsurf)
1487 shell_interface->set_fullscreen(window->shsurf,
1488 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1489 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001490 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001491 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001492 weston_wm_window_set_toplevel(window);
1493 }
1494 } else {
1495 if (property == wm->atom.net_wm_state_maximized_vert &&
1496 update_state(action, &window->maximized_vert))
1497 weston_wm_window_set_net_wm_state(window);
1498 if (property == wm->atom.net_wm_state_maximized_horz &&
1499 update_state(action, &window->maximized_horz))
1500 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001501
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001502 if (maximized != weston_wm_window_is_maximized(window)) {
1503 if (weston_wm_window_is_maximized(window)) {
1504 window->saved_width = window->width;
1505 window->saved_height = window->height;
1506
1507 if (window->shsurf)
1508 shell_interface->set_maximized(window->shsurf);
1509 } else if (window->shsurf) {
1510 weston_wm_window_set_toplevel(window);
1511 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001512 }
1513 }
1514}
1515
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001516static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001517surface_destroy(struct wl_listener *listener, void *data)
1518{
1519 struct weston_wm_window *window =
1520 container_of(listener,
1521 struct weston_wm_window, surface_destroy_listener);
1522
1523 wm_log("surface for xid %d destroyed\n", window->id);
1524
1525 /* This should have been freed by the shell.
1526 * Don't try to use it later. */
1527 window->shsurf = NULL;
1528 window->surface = NULL;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001529}
1530
1531static void
1532weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1533 xcb_client_message_event_t *client_message)
1534{
1535 struct weston_wm *wm = window->wm;
1536 struct wl_resource *resource;
1537
1538 if (window->surface_id != 0) {
1539 wm_log("already have surface id for window %d\n", window->id);
1540 return;
1541 }
1542
1543 /* Xwayland will send the wayland requests to create the
1544 * wl_surface before sending this client message. Even so, we
1545 * can end up handling the X event before the wayland requests
1546 * and thus when we try to look up the surface ID, the surface
1547 * hasn't been created yet. In that case put the window on
1548 * the unpaired window list and continue when the surface gets
1549 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001550 uint32_t id = client_message->data.data32[0];
1551 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001552 if (resource) {
1553 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001554 xserver_map_shell_surface(window,
1555 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001556 }
1557 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001558 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001559 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001560 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001561}
1562
1563static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001564weston_wm_handle_client_message(struct weston_wm *wm,
1565 xcb_generic_event_t *event)
1566{
1567 xcb_client_message_event_t *client_message =
1568 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001569 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001570
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001571 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1572 get_atom_name(wm->conn, client_message->type),
1573 client_message->data.data32[0],
1574 client_message->data.data32[1],
1575 client_message->data.data32[2],
1576 client_message->data.data32[3],
1577 client_message->data.data32[4],
1578 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001579
Jason Ekstrand0250a742014-07-24 13:17:47 -07001580 /* The window may get created and destroyed before we actually
1581 * handle the message. If it doesn't exist, bail.
1582 */
Derek Foreman49372142015-04-09 10:51:22 -05001583 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001584 return;
1585
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001586 if (client_message->type == wm->atom.net_wm_moveresize)
1587 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001588 else if (client_message->type == wm->atom.net_wm_state)
1589 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001590 else if (client_message->type == wm->atom.wl_surface_id)
1591 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001592}
1593
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001594enum cursor_type {
1595 XWM_CURSOR_TOP,
1596 XWM_CURSOR_BOTTOM,
1597 XWM_CURSOR_LEFT,
1598 XWM_CURSOR_RIGHT,
1599 XWM_CURSOR_TOP_LEFT,
1600 XWM_CURSOR_TOP_RIGHT,
1601 XWM_CURSOR_BOTTOM_LEFT,
1602 XWM_CURSOR_BOTTOM_RIGHT,
1603 XWM_CURSOR_LEFT_PTR,
1604};
1605
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001606/*
1607 * The following correspondences between file names and cursors was copied
1608 * from: https://bugs.kde.org/attachment.cgi?id=67313
1609 */
1610
1611static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001612 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001613 "sw-resize",
1614 "size_bdiag"
1615};
1616
1617static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001618 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001619 "se-resize",
1620 "size_fdiag"
1621};
1622
1623static const char *bottom_sides[] = {
1624 "bottom_side",
1625 "s-resize",
1626 "size_ver"
1627};
1628
1629static const char *left_ptrs[] = {
1630 "left_ptr",
1631 "default",
1632 "top_left_arrow",
1633 "left-arrow"
1634};
1635
1636static const char *left_sides[] = {
1637 "left_side",
1638 "w-resize",
1639 "size_hor"
1640};
1641
1642static const char *right_sides[] = {
1643 "right_side",
1644 "e-resize",
1645 "size_hor"
1646};
1647
1648static const char *top_left_corners[] = {
1649 "top_left_corner",
1650 "nw-resize",
1651 "size_fdiag"
1652};
1653
1654static const char *top_right_corners[] = {
1655 "top_right_corner",
1656 "ne-resize",
1657 "size_bdiag"
1658};
1659
1660static const char *top_sides[] = {
1661 "top_side",
1662 "n-resize",
1663 "size_ver"
1664};
1665
1666struct cursor_alternatives {
1667 const char **names;
1668 size_t count;
1669};
1670
1671static const struct cursor_alternatives cursors[] = {
1672 {top_sides, ARRAY_LENGTH(top_sides)},
1673 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1674 {left_sides, ARRAY_LENGTH(left_sides)},
1675 {right_sides, ARRAY_LENGTH(right_sides)},
1676 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1677 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1678 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1679 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1680 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001681};
1682
1683static void
1684weston_wm_create_cursors(struct weston_wm *wm)
1685{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001686 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001687 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001688 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001689
1690 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1691 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001692 for (j = 0; j < cursors[i].count; j++) {
1693 name = cursors[i].names[j];
1694 wm->cursors[i] =
1695 xcb_cursor_library_load_cursor(wm, name);
1696 if (wm->cursors[i] != (xcb_cursor_t)-1)
1697 break;
1698 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001699 }
1700
1701 wm->last_cursor = -1;
1702}
1703
1704static void
1705weston_wm_destroy_cursors(struct weston_wm *wm)
1706{
1707 uint8_t i;
1708
1709 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1710 xcb_free_cursor(wm->conn, wm->cursors[i]);
1711
1712 free(wm->cursors);
1713}
1714
1715static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001716get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001717{
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001718 // int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001719
1720 switch (location) {
1721 case THEME_LOCATION_RESIZING_TOP:
1722 return XWM_CURSOR_TOP;
1723 case THEME_LOCATION_RESIZING_BOTTOM:
1724 return XWM_CURSOR_BOTTOM;
1725 case THEME_LOCATION_RESIZING_LEFT:
1726 return XWM_CURSOR_LEFT;
1727 case THEME_LOCATION_RESIZING_RIGHT:
1728 return XWM_CURSOR_RIGHT;
1729 case THEME_LOCATION_RESIZING_TOP_LEFT:
1730 return XWM_CURSOR_TOP_LEFT;
1731 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1732 return XWM_CURSOR_TOP_RIGHT;
1733 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1734 return XWM_CURSOR_BOTTOM_LEFT;
1735 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1736 return XWM_CURSOR_BOTTOM_RIGHT;
1737 case THEME_LOCATION_EXTERIOR:
1738 case THEME_LOCATION_TITLEBAR:
1739 default:
1740 return XWM_CURSOR_LEFT_PTR;
1741 }
1742}
1743
1744static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001745weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1746 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001747{
1748 uint32_t cursor_value_list;
1749
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001750 if (wm->last_cursor == cursor)
1751 return;
1752
1753 wm->last_cursor = cursor;
1754
1755 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001756 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001757 XCB_CW_CURSOR, &cursor_value_list);
1758 xcb_flush(wm->conn);
1759}
1760
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001761static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001762weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001763{
1764 xcb_client_message_event_t client_message;
1765
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001766 if (window->delete_window) {
1767 client_message.response_type = XCB_CLIENT_MESSAGE;
1768 client_message.format = 32;
1769 client_message.window = window->id;
1770 client_message.type = window->wm->atom.wm_protocols;
1771 client_message.data.data32[0] =
1772 window->wm->atom.wm_delete_window;
1773 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001774
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001775 xcb_send_event(window->wm->conn, 0, window->id,
1776 XCB_EVENT_MASK_NO_EVENT,
1777 (char *) &client_message);
1778 } else {
1779 xcb_kill_client(window->wm->conn, window->id);
1780 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001781}
1782
1783static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001784weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1785{
1786 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1787 struct weston_shell_interface *shell_interface =
1788 &wm->server->compositor->shell_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001789 struct weston_seat *seat;
Derek Foremane4d6c832015-08-05 14:48:11 -05001790 struct weston_pointer *pointer;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001791 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001792 enum theme_location location;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001793 enum frame_button_state button_state;
1794 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001795
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001796 wm_log("XCB_BUTTON_%s (detail %d)\n",
1797 button->response_type == XCB_BUTTON_PRESS ?
1798 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001799
Derek Foreman49372142015-04-09 10:51:22 -05001800 if (!wm_lookup_window(wm, button->event, &window) ||
1801 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001802 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001803
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001804 if (button->detail != 1 && button->detail != 2)
1805 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001806
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001807 seat = weston_wm_pick_seat_for_window(window);
Derek Foremane4d6c832015-08-05 14:48:11 -05001808 pointer = weston_seat_get_pointer(seat);
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001809
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001810 button_state = button->response_type == XCB_BUTTON_PRESS ?
1811 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1812 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1813
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001814 /* Make sure we're looking at the right location. The frame
1815 * could have received a motion event from a pointer from a
1816 * different wl_seat, but under X it looks like our core
1817 * pointer moved. Move the frame pointer to the button press
1818 * location before deciding what to do. */
1819 location = frame_pointer_motion(window->frame, NULL,
1820 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001821 location = frame_pointer_button(window->frame, NULL,
1822 button_id, button_state);
1823 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1824 weston_wm_window_schedule_repaint(window);
1825
1826 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001827 if (pointer)
1828 shell_interface->move(window->shsurf, pointer);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001829 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1830 }
1831
1832 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001833 if (pointer)
1834 shell_interface->resize(window->shsurf, pointer, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001835 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1836 }
1837
1838 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001839 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001840 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001841 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001842
1843 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1844 window->maximized_horz = !window->maximized_horz;
1845 window->maximized_vert = !window->maximized_vert;
1846 if (weston_wm_window_is_maximized(window)) {
1847 window->saved_width = window->width;
1848 window->saved_height = window->height;
1849 shell_interface->set_maximized(window->shsurf);
1850 } else {
1851 weston_wm_window_set_toplevel(window);
1852 }
1853 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1854 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001855}
1856
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001857static void
1858weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1859{
1860 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1861 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001862 enum theme_location location;
1863 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001864
Derek Foreman49372142015-04-09 10:51:22 -05001865 if (!wm_lookup_window(wm, motion->event, &window) ||
1866 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001867 return;
1868
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001869 location = frame_pointer_motion(window->frame, NULL,
1870 motion->event_x, motion->event_y);
1871 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1872 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001873
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001874 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001875 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001876}
1877
1878static void
1879weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1880{
1881 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1882 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001883 enum theme_location location;
1884 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001885
Derek Foreman49372142015-04-09 10:51:22 -05001886 if (!wm_lookup_window(wm, enter->event, &window) ||
1887 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001888 return;
1889
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001890 location = frame_pointer_enter(window->frame, NULL,
1891 enter->event_x, enter->event_y);
1892 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1893 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001894
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001895 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001896 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001897}
1898
1899static void
1900weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1901{
1902 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1903 struct weston_wm_window *window;
1904
Derek Foreman49372142015-04-09 10:51:22 -05001905 if (!wm_lookup_window(wm, leave->event, &window) ||
1906 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001907 return;
1908
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001909 frame_pointer_leave(window->frame, NULL);
1910 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1911 weston_wm_window_schedule_repaint(window);
1912
Tiago Vignattic1903232012-07-16 12:15:37 -04001913 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001914}
1915
Giulio Camuffob18f7882015-03-29 14:20:23 +03001916static void
1917weston_wm_handle_focus_in(struct weston_wm *wm, xcb_generic_event_t *event)
1918{
1919 xcb_focus_in_event_t *focus = (xcb_focus_in_event_t *) event;
1920 /* Do not let X clients change the focus behind the compositor's
1921 * back. Reset the focus to the old one if it changed. */
1922 if (!wm->focus_window || focus->event != wm->focus_window->id)
1923 weston_wm_send_focus_window(wm, wm->focus_window);
1924}
1925
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001926static int
1927weston_wm_handle_event(int fd, uint32_t mask, void *data)
1928{
1929 struct weston_wm *wm = data;
1930 xcb_generic_event_t *event;
1931 int count = 0;
1932
1933 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1934 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001935 free(event);
1936 count++;
1937 continue;
1938 }
1939
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001940 if (weston_wm_handle_dnd_event(wm, event)) {
1941 free(event);
1942 count++;
1943 continue;
1944 }
1945
MoD31700122013-06-11 19:58:55 -05001946 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001947 case XCB_BUTTON_PRESS:
1948 case XCB_BUTTON_RELEASE:
1949 weston_wm_handle_button(wm, event);
1950 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001951 case XCB_ENTER_NOTIFY:
1952 weston_wm_handle_enter(wm, event);
1953 break;
1954 case XCB_LEAVE_NOTIFY:
1955 weston_wm_handle_leave(wm, event);
1956 break;
1957 case XCB_MOTION_NOTIFY:
1958 weston_wm_handle_motion(wm, event);
1959 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001960 case XCB_CREATE_NOTIFY:
1961 weston_wm_handle_create_notify(wm, event);
1962 break;
1963 case XCB_MAP_REQUEST:
1964 weston_wm_handle_map_request(wm, event);
1965 break;
1966 case XCB_MAP_NOTIFY:
1967 weston_wm_handle_map_notify(wm, event);
1968 break;
1969 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001970 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001971 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001972 case XCB_REPARENT_NOTIFY:
1973 weston_wm_handle_reparent_notify(wm, event);
1974 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001975 case XCB_CONFIGURE_REQUEST:
1976 weston_wm_handle_configure_request(wm, event);
1977 break;
1978 case XCB_CONFIGURE_NOTIFY:
1979 weston_wm_handle_configure_notify(wm, event);
1980 break;
1981 case XCB_DESTROY_NOTIFY:
1982 weston_wm_handle_destroy_notify(wm, event);
1983 break;
1984 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001985 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001986 break;
1987 case XCB_PROPERTY_NOTIFY:
1988 weston_wm_handle_property_notify(wm, event);
1989 break;
1990 case XCB_CLIENT_MESSAGE:
1991 weston_wm_handle_client_message(wm, event);
1992 break;
Giulio Camuffob18f7882015-03-29 14:20:23 +03001993 case XCB_FOCUS_IN:
1994 weston_wm_handle_focus_in(wm, event);
1995 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001996 }
1997
1998 free(event);
1999 count++;
2000 }
2001
Marek Chalupaa1f3f3c2015-08-12 09:55:12 +02002002 if (count != 0)
2003 xcb_flush(wm->conn);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002004
2005 return count;
2006}
2007
2008static void
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002009weston_wm_set_net_active_window(struct weston_wm *wm, xcb_window_t window) {
2010 xcb_change_property(wm->conn, XCB_PROP_MODE_REPLACE,
2011 wm->screen->root, wm->atom.net_active_window,
2012 wm->atom.window, 32, 1, &window);
2013}
2014
2015static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002016weston_wm_get_visual_and_colormap(struct weston_wm *wm)
2017{
2018 xcb_depth_iterator_t d_iter;
2019 xcb_visualtype_iterator_t vt_iter;
2020 xcb_visualtype_t *visualtype;
2021
2022 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
2023 visualtype = NULL;
2024 while (d_iter.rem > 0) {
2025 if (d_iter.data->depth == 32) {
2026 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
2027 visualtype = vt_iter.data;
2028 break;
2029 }
2030
2031 xcb_depth_next(&d_iter);
2032 }
2033
2034 if (visualtype == NULL) {
2035 weston_log("no 32 bit visualtype\n");
2036 return;
2037 }
2038
2039 wm->visual_id = visualtype->visual_id;
2040 wm->colormap = xcb_generate_id(wm->conn);
2041 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
2042 wm->colormap, wm->screen->root, wm->visual_id);
2043}
2044
2045static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002046weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002047{
2048
2049#define F(field) offsetof(struct weston_wm, field)
2050
2051 static const struct { const char *name; int offset; } atoms[] = {
2052 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002053 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002054 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
2055 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04002056 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002057 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002058 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002059 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002060 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002061 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002062 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2063 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002064 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2065 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002066 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2067 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2068 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02002069 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002070 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2071
2072 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2073 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2074 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2075 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2076 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2077 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2078 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002079 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2080 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002081 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2082 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2083 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2084 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2085 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2086
2087 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2088 { "_NET_SUPPORTING_WM_CHECK",
2089 F(atom.net_supporting_wm_check) },
2090 { "_NET_SUPPORTED", F(atom.net_supported) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002091 { "_NET_ACTIVE_WINDOW", F(atom.net_active_window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002092 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2093 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002094 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002095 { "TARGETS", F(atom.targets) },
2096 { "UTF8_STRING", F(atom.utf8_string) },
2097 { "_WL_SELECTION", F(atom.wl_selection) },
2098 { "INCR", F(atom.incr) },
2099 { "TIMESTAMP", F(atom.timestamp) },
2100 { "MULTIPLE", F(atom.multiple) },
2101 { "UTF8_STRING" , F(atom.utf8_string) },
2102 { "COMPOUND_TEXT", F(atom.compound_text) },
2103 { "TEXT", F(atom.text) },
2104 { "STRING", F(atom.string) },
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002105 { "WINDOW", F(atom.window) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002106 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2107 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002108 { "XdndSelection", F(atom.xdnd_selection) },
2109 { "XdndAware", F(atom.xdnd_aware) },
2110 { "XdndEnter", F(atom.xdnd_enter) },
2111 { "XdndLeave", F(atom.xdnd_leave) },
2112 { "XdndDrop", F(atom.xdnd_drop) },
2113 { "XdndStatus", F(atom.xdnd_status) },
2114 { "XdndFinished", F(atom.xdnd_finished) },
2115 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002116 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2117 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002118 };
2119#undef F
2120
2121 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2122 xcb_xfixes_query_version_reply_t *xfixes_reply;
2123 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2124 xcb_intern_atom_reply_t *reply;
2125 xcb_render_query_pict_formats_reply_t *formats_reply;
2126 xcb_render_query_pict_formats_cookie_t formats_cookie;
2127 xcb_render_pictforminfo_t *formats;
2128 uint32_t i;
2129
2130 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002131 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002132
2133 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2134
2135 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2136 cookies[i] = xcb_intern_atom (wm->conn, 0,
2137 strlen(atoms[i].name),
2138 atoms[i].name);
2139
2140 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2141 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2142 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2143 free(reply);
2144 }
2145
2146 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2147 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002148 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002149
2150 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2151 XCB_XFIXES_MAJOR_VERSION,
2152 XCB_XFIXES_MINOR_VERSION);
2153 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2154 xfixes_cookie, NULL);
2155
Martin Minarik6d118362012-06-07 18:01:59 +02002156 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002157 xfixes_reply->major_version, xfixes_reply->minor_version);
2158
2159 free(xfixes_reply);
2160
2161 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2162 formats_cookie, 0);
2163 if (formats_reply == NULL)
2164 return;
2165
2166 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002167 for (i = 0; i < formats_reply->num_formats; i++) {
2168 if (formats[i].direct.red_mask != 0xff &&
2169 formats[i].direct.red_shift != 16)
2170 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002171 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2172 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002173 wm->format_rgb = formats[i];
2174 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2175 formats[i].depth == 32 &&
2176 formats[i].direct.alpha_mask == 0xff &&
2177 formats[i].direct.alpha_shift == 24)
2178 wm->format_rgba = formats[i];
2179 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002180
2181 free(formats_reply);
2182}
2183
2184static void
2185weston_wm_create_wm_window(struct weston_wm *wm)
2186{
2187 static const char name[] = "Weston WM";
2188
2189 wm->wm_window = xcb_generate_id(wm->conn);
2190 xcb_create_window(wm->conn,
2191 XCB_COPY_FROM_PARENT,
2192 wm->wm_window,
2193 wm->screen->root,
2194 0, 0,
2195 10, 10,
2196 0,
2197 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2198 wm->screen->root_visual,
2199 0, NULL);
2200
2201 xcb_change_property(wm->conn,
2202 XCB_PROP_MODE_REPLACE,
2203 wm->wm_window,
2204 wm->atom.net_supporting_wm_check,
2205 XCB_ATOM_WINDOW,
2206 32, /* format */
2207 1, &wm->wm_window);
2208
2209 xcb_change_property(wm->conn,
2210 XCB_PROP_MODE_REPLACE,
2211 wm->wm_window,
2212 wm->atom.net_wm_name,
2213 wm->atom.utf8_string,
2214 8, /* format */
2215 strlen(name), name);
2216
2217 xcb_change_property(wm->conn,
2218 XCB_PROP_MODE_REPLACE,
2219 wm->screen->root,
2220 wm->atom.net_supporting_wm_check,
2221 XCB_ATOM_WINDOW,
2222 32, /* format */
2223 1, &wm->wm_window);
2224
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002225 /* Claim the WM_S0 selection even though we don't suport
2226 * the --replace functionality. */
2227 xcb_set_selection_owner(wm->conn,
2228 wm->wm_window,
2229 wm->atom.wm_s0,
2230 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002231
2232 xcb_set_selection_owner(wm->conn,
2233 wm->wm_window,
2234 wm->atom.net_wm_cm_s0,
2235 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002236}
2237
2238struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002239weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002240{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002241 struct weston_wm *wm;
2242 struct wl_event_loop *loop;
2243 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002244 uint32_t values[1];
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002245 xcb_atom_t supported[6];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002246
Peter Huttererf3d62272013-08-08 11:57:05 +10002247 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002248 if (wm == NULL)
2249 return NULL;
2250
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002251 wm->server = wxs;
2252 wm->window_hash = hash_table_create();
2253 if (wm->window_hash == NULL) {
2254 free(wm);
2255 return NULL;
2256 }
2257
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002258 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002259 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002260 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002261 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002262 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002263 hash_table_destroy(wm->window_hash);
2264 free(wm);
2265 return NULL;
2266 }
2267
2268 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2269 wm->screen = s.data;
2270
2271 loop = wl_display_get_event_loop(wxs->wl_display);
2272 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002273 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002274 WL_EVENT_READABLE,
2275 weston_wm_handle_event, wm);
2276 wl_event_source_check(wm->source);
2277
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002278 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002279 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002280
2281 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002282 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2283 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2284 XCB_EVENT_MASK_PROPERTY_CHANGE;
2285 xcb_change_window_attributes(wm->conn, wm->screen->root,
2286 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002287
2288 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2289 XCB_COMPOSITE_REDIRECT_MANUAL);
2290
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002291 wm->theme = theme_create();
2292
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002293 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002294 supported[1] = wm->atom.net_wm_state;
2295 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002296 supported[3] = wm->atom.net_wm_state_maximized_vert;
2297 supported[4] = wm->atom.net_wm_state_maximized_horz;
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002298 supported[5] = wm->atom.net_active_window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002299 xcb_change_property(wm->conn,
2300 XCB_PROP_MODE_REPLACE,
2301 wm->screen->root,
2302 wm->atom.net_supported,
2303 XCB_ATOM_ATOM,
2304 32, /* format */
2305 ARRAY_LENGTH(supported), supported);
2306
Benoit Gschwind1a42ca12015-09-25 21:26:04 +02002307 weston_wm_set_net_active_window(wm, XCB_WINDOW_NONE);
2308
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002309 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002310
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002311 weston_wm_dnd_init(wm);
2312
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002313 xcb_flush(wm->conn);
2314
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002315 wm->create_surface_listener.notify = weston_wm_create_surface;
2316 wl_signal_add(&wxs->compositor->create_surface_signal,
2317 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002318 wm->activate_listener.notify = weston_wm_window_activate;
2319 wl_signal_add(&wxs->compositor->activate_signal,
2320 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002321 wm->transform_listener.notify = weston_wm_window_transform;
2322 wl_signal_add(&wxs->compositor->transform_signal,
2323 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002324 wm->kill_listener.notify = weston_wm_kill_client;
2325 wl_signal_add(&wxs->compositor->kill_signal,
2326 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002327 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002328
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002329 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002330 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002331
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002332 /* Create wm window and take WM_S0 selection last, which
2333 * signals to Xwayland that we're done with setup. */
2334 weston_wm_create_wm_window(wm);
2335
2336 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002337
2338 return wm;
2339}
2340
2341void
2342weston_wm_destroy(struct weston_wm *wm)
2343{
2344 /* FIXME: Free windows in hash. */
2345 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002346 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002347 xcb_disconnect(wm->conn);
2348 wl_event_source_remove(wm->source);
2349 wl_list_remove(&wm->selection_listener.link);
2350 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002351 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01002352 wl_list_remove(&wm->transform_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002353 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002354
2355 free(wm);
2356}
2357
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002358static struct weston_wm_window *
2359get_wm_window(struct weston_surface *surface)
2360{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002361 struct wl_listener *listener;
2362
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002363 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002364 if (listener)
2365 return container_of(listener, struct weston_wm_window,
2366 surface_destroy_listener);
2367
2368 return NULL;
2369}
2370
2371static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002372weston_wm_window_configure(void *data)
2373{
2374 struct weston_wm_window *window = data;
2375 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002376 uint32_t values[4];
2377 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002378
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002379 weston_wm_window_get_child_position(window, &x, &y);
2380 values[0] = x;
2381 values[1] = y;
2382 values[2] = window->width;
2383 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002384 xcb_configure_window(wm->conn,
2385 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002386 XCB_CONFIG_WINDOW_X |
2387 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002388 XCB_CONFIG_WINDOW_WIDTH |
2389 XCB_CONFIG_WINDOW_HEIGHT,
2390 values);
2391
2392 weston_wm_window_get_frame_size(window, &width, &height);
2393 values[0] = width;
2394 values[1] = height;
2395 xcb_configure_window(wm->conn,
2396 window->frame_id,
2397 XCB_CONFIG_WINDOW_WIDTH |
2398 XCB_CONFIG_WINDOW_HEIGHT,
2399 values);
2400
2401 window->configure_source = NULL;
2402
2403 weston_wm_window_schedule_repaint(window);
2404}
2405
2406static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002407send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002408{
2409 struct weston_wm_window *window = get_wm_window(surface);
2410 struct weston_wm *wm = window->wm;
2411 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002412 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002413
Marek Chalupae9fe4672014-10-29 13:44:44 +01002414 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002415 hborder = 2 * t->width;
2416 vborder = t->titlebar_height + t->width;
2417 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002418 hborder = 0;
2419 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002420 }
2421
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002422 if (width > hborder)
2423 window->width = width - hborder;
2424 else
2425 window->width = 1;
2426
2427 if (height > vborder)
2428 window->height = height - vborder;
2429 else
2430 window->height = 1;
2431
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002432 if (window->frame)
2433 frame_resize_inside(window->frame, window->width, window->height);
2434
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002435 if (window->configure_source)
2436 return;
2437
2438 window->configure_source =
2439 wl_event_loop_add_idle(wm->server->loop,
2440 weston_wm_window_configure, window);
2441}
2442
2443static const struct weston_shell_client shell_client = {
2444 send_configure
2445};
2446
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002447static int
2448legacy_fullscreen(struct weston_wm *wm,
2449 struct weston_wm_window *window,
2450 struct weston_output **output_ret)
2451{
2452 struct weston_compositor *compositor = wm->server->compositor;
2453 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002454 uint32_t minmax = PMinSize | PMaxSize;
2455 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002456
2457 /* Heuristics for detecting legacy fullscreen windows... */
2458
2459 wl_list_for_each(output, &compositor->output_list, link) {
2460 if (output->x == window->x &&
2461 output->y == window->y &&
2462 output->width == window->width &&
2463 output->height == window->height &&
2464 window->override_redirect) {
2465 *output_ret = output;
2466 return 1;
2467 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002468
2469 matching_size = 0;
2470 if ((window->size_hints.flags & (USSize |PSize)) &&
2471 window->size_hints.width == output->width &&
2472 window->size_hints.height == output->height)
2473 matching_size = 1;
2474 if ((window->size_hints.flags & minmax) == minmax &&
2475 window->size_hints.min_width == output->width &&
2476 window->size_hints.min_height == output->height &&
2477 window->size_hints.max_width == output->width &&
2478 window->size_hints.max_height == output->height)
2479 matching_size = 1;
2480
2481 if (matching_size && !window->decorate &&
2482 (window->size_hints.flags & (USPosition | PPosition)) &&
2483 window->size_hints.x == output->x &&
2484 window->size_hints.y == output->y) {
2485 *output_ret = output;
2486 return 1;
2487 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002488 }
2489
2490 return 0;
2491}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002492
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002493static bool
2494weston_wm_window_type_inactive(struct weston_wm_window *window)
2495{
2496 struct weston_wm *wm = window->wm;
2497
2498 return window->type == wm->atom.net_wm_window_type_tooltip ||
2499 window->type == wm->atom.net_wm_window_type_dropdown ||
2500 window->type == wm->atom.net_wm_window_type_dnd ||
2501 window->type == wm->atom.net_wm_window_type_combo ||
Giulio Camuffo84787ea2015-04-29 19:00:48 +03002502 window->type == wm->atom.net_wm_window_type_popup ||
2503 window->type == wm->atom.net_wm_window_type_utility;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002504}
2505
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002506static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002507xserver_map_shell_surface(struct weston_wm_window *window,
2508 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002509{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002510 struct weston_wm *wm = window->wm;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002511 struct weston_shell_interface *shell_interface =
2512 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002513 struct weston_output *output;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002514 struct weston_wm_window *parent;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002515 int flags = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002516
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002517 weston_wm_window_read_properties(window);
2518
2519 /* A weston_wm_window may have many different surfaces assigned
2520 * throughout its life, so we must make sure to remove the listener
2521 * from the old surface signal list. */
2522 if (window->surface)
2523 wl_list_remove(&window->surface_destroy_listener.link);
2524
2525 window->surface = surface;
2526 window->surface_destroy_listener.notify = surface_destroy;
2527 wl_signal_add(&window->surface->destroy_signal,
2528 &window->surface_destroy_listener);
2529
2530 weston_wm_window_schedule_repaint(window);
2531
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002532 if (!shell_interface->create_shell_surface)
2533 return;
2534
Pekka Paalanen50b67472014-10-01 15:02:41 +03002535 if (window->surface->configure) {
2536 weston_log("warning, unexpected in %s: "
2537 "surface's configure hook is already set.\n",
2538 __func__);
2539 return;
2540 }
2541
Murray Calavera883ac022015-06-06 13:02:22 +00002542 window->shsurf =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002543 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002544 window->surface,
2545 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002546
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002547 if (window->name)
2548 shell_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002549 if (window->pid > 0)
2550 shell_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002551
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002552 if (window->fullscreen) {
2553 window->saved_width = window->width;
2554 window->saved_height = window->height;
2555 shell_interface->set_fullscreen(window->shsurf,
2556 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2557 0, NULL);
2558 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002559 } else if (legacy_fullscreen(wm, window, &output)) {
2560 window->fullscreen = 1;
2561 shell_interface->set_fullscreen(window->shsurf,
2562 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2563 0, output);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002564 } else if (window->override_redirect) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002565 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002566 window->x,
2567 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002568 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Axel Davye4450f92014-01-12 15:06:05 +01002569 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002570 parent = window->transient_for;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002571 if (weston_wm_window_type_inactive(window))
2572 flags = WL_SHELL_SURFACE_TRANSIENT_INACTIVE;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002573 shell_interface->set_transient(window->shsurf,
2574 parent->surface,
Axel Davyfa506b62014-01-12 15:06:04 +01002575 window->x - parent->x,
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002576 window->y - parent->y, flags);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002577 } else if (weston_wm_window_is_maximized(window)) {
2578 shell_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002579 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002580 if (weston_wm_window_type_inactive(window)) {
2581 shell_interface->set_xwayland(window->shsurf,
2582 window->x,
2583 window->y,
2584 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2585 } else {
2586 shell_interface->set_toplevel(window->shsurf);
2587 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002588 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002589}