blob: 9b4e0886bfa22080786a41b381af597510aec6c9 [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"
45
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070046struct wm_size_hints {
Bryce Harringtone00554b2015-06-12 10:17:39 -070047 uint32_t flags;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070048 int32_t x, y;
49 int32_t width, height; /* should set so old wm's don't mess up */
50 int32_t min_width, min_height;
51 int32_t max_width, max_height;
Bryce Harringtone00554b2015-06-12 10:17:39 -070052 int32_t width_inc, height_inc;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070053 struct {
54 int32_t x;
55 int32_t y;
56 } min_aspect, max_aspect;
57 int32_t base_width, base_height;
58 int32_t win_gravity;
59};
60
61#define USPosition (1L << 0)
62#define USSize (1L << 1)
63#define PPosition (1L << 2)
64#define PSize (1L << 3)
65#define PMinSize (1L << 4)
66#define PMaxSize (1L << 5)
67#define PResizeInc (1L << 6)
68#define PAspect (1L << 7)
69#define PBaseSize (1L << 8)
70#define PWinGravity (1L << 9)
71
Kristian Høgsberg380deee2012-05-21 17:12:41 -040072struct motif_wm_hints {
73 uint32_t flags;
74 uint32_t functions;
75 uint32_t decorations;
76 int32_t input_mode;
77 uint32_t status;
78};
79
80#define MWM_HINTS_FUNCTIONS (1L << 0)
81#define MWM_HINTS_DECORATIONS (1L << 1)
82#define MWM_HINTS_INPUT_MODE (1L << 2)
83#define MWM_HINTS_STATUS (1L << 3)
84
85#define MWM_FUNC_ALL (1L << 0)
86#define MWM_FUNC_RESIZE (1L << 1)
87#define MWM_FUNC_MOVE (1L << 2)
88#define MWM_FUNC_MINIMIZE (1L << 3)
89#define MWM_FUNC_MAXIMIZE (1L << 4)
90#define MWM_FUNC_CLOSE (1L << 5)
91
92#define MWM_DECOR_ALL (1L << 0)
93#define MWM_DECOR_BORDER (1L << 1)
94#define MWM_DECOR_RESIZEH (1L << 2)
95#define MWM_DECOR_TITLE (1L << 3)
96#define MWM_DECOR_MENU (1L << 4)
97#define MWM_DECOR_MINIMIZE (1L << 5)
98#define MWM_DECOR_MAXIMIZE (1L << 6)
99
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700100#define MWM_DECOR_EVERYTHING \
101 (MWM_DECOR_BORDER | MWM_DECOR_RESIZEH | MWM_DECOR_TITLE | \
102 MWM_DECOR_MENU | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE)
103
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400104#define MWM_INPUT_MODELESS 0
105#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
106#define MWM_INPUT_SYSTEM_MODAL 2
107#define MWM_INPUT_FULL_APPLICATION_MODAL 3
108#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
109
110#define MWM_TEAROFF_WINDOW (1L<<0)
111
112#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
113#define _NET_WM_MOVERESIZE_SIZE_TOP 1
114#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
115#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
116#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
117#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
118#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
119#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
120#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
121#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
122#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
123#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
124
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400125struct weston_wm_window {
126 struct weston_wm *wm;
127 xcb_window_t id;
128 xcb_window_t frame_id;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500129 struct frame *frame;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400130 cairo_surface_t *cairo_surface;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700131 uint32_t surface_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400132 struct weston_surface *surface;
133 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500134 struct weston_view *view;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400135 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
165weston_wm_window_schedule_repaint(struct weston_wm_window *window);
166
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700167static void
168xserver_map_shell_surface(struct weston_wm_window *window,
169 struct weston_surface *surface);
170
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400171static int __attribute__ ((format (printf, 1, 2)))
172wm_log(const char *fmt, ...)
173{
174#ifdef WM_DEBUG
175 int l;
176 va_list argp;
177
178 va_start(argp, fmt);
179 l = weston_vlog(fmt, argp);
180 va_end(argp);
181
182 return l;
183#else
184 return 0;
185#endif
186}
187
188static int __attribute__ ((format (printf, 1, 2)))
189wm_log_continue(const char *fmt, ...)
190{
191#ifdef WM_DEBUG
192 int l;
193 va_list argp;
194
195 va_start(argp, fmt);
196 l = weston_vlog_continue(fmt, argp);
197 va_end(argp);
198
199 return l;
200#else
201 return 0;
202#endif
203}
204
Derek Foreman49372142015-04-09 10:51:22 -0500205static bool __attribute__ ((warn_unused_result))
206wm_lookup_window(struct weston_wm *wm, xcb_window_t hash,
207 struct weston_wm_window **window)
208{
209 *window = hash_table_lookup(wm->window_hash, hash);
210 if (*window)
211 return true;
212 return false;
213}
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400214
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400215const char *
216get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
217{
218 xcb_get_atom_name_cookie_t cookie;
219 xcb_get_atom_name_reply_t *reply;
220 xcb_generic_error_t *e;
221 static char buffer[64];
222
223 if (atom == XCB_ATOM_NONE)
224 return "None";
225
226 cookie = xcb_get_atom_name (c, atom);
227 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500228
229 if(reply) {
230 snprintf(buffer, sizeof buffer, "%.*s",
231 xcb_get_atom_name_name_length (reply),
232 xcb_get_atom_name_name (reply));
233 } else {
234 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
235 }
236
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400237 free(reply);
238
239 return buffer;
240}
241
Tiago Vignatti90fada42012-07-16 12:02:08 -0400242static xcb_cursor_t
243xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
244{
245 xcb_connection_t *c = wm->conn;
246 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
247 xcb_screen_t *screen = s.data;
248 xcb_gcontext_t gc;
249 xcb_pixmap_t pix;
250 xcb_render_picture_t pic;
251 xcb_cursor_t cursor;
252 int stride = img->width * 4;
253
254 pix = xcb_generate_id(c);
255 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
256
257 pic = xcb_generate_id(c);
258 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
259
260 gc = xcb_generate_id(c);
261 xcb_create_gc(c, gc, pix, 0, 0);
262
263 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
264 img->width, img->height, 0, 0, 0, 32,
265 stride * img->height, (uint8_t *) img->pixels);
266 xcb_free_gc(c, gc);
267
268 cursor = xcb_generate_id(c);
269 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
270
271 xcb_render_free_picture(c, pic);
272 xcb_free_pixmap(c, pix);
273
274 return cursor;
275}
276
277static xcb_cursor_t
278xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
279{
280 /* TODO: treat animated cursors as well */
281 if (images->nimage != 1)
282 return -1;
283
284 return xcb_cursor_image_load_cursor(wm, images->images[0]);
285}
286
287static xcb_cursor_t
288xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
289{
290 xcb_cursor_t cursor;
291 XcursorImages *images;
292 char *v = NULL;
293 int size = 0;
294
295 if (!file)
296 return 0;
297
298 v = getenv ("XCURSOR_SIZE");
299 if (v)
300 size = atoi(v);
301
302 if (!size)
303 size = 32;
304
305 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300306 if (!images)
307 return -1;
308
Tiago Vignatti90fada42012-07-16 12:02:08 -0400309 cursor = xcb_cursor_images_load_cursor (wm, images);
310 XcursorImagesDestroy (images);
311
312 return cursor;
313}
314
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400315void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400316dump_property(struct weston_wm *wm,
317 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400318{
319 int32_t *incr_value;
320 const char *text_value, *name;
321 xcb_atom_t *atom_value;
322 int width, len;
323 uint32_t i;
324
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400325 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400326 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400327 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400328 return;
329 }
330
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400331 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
332 get_atom_name(wm->conn, reply->type),
333 reply->format,
334 xcb_get_property_value_length(reply),
335 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400336
337 if (reply->type == wm->atom.incr) {
338 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400339 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400340 } else if (reply->type == wm->atom.utf8_string ||
341 reply->type == wm->atom.string) {
342 text_value = xcb_get_property_value(reply);
343 if (reply->value_len > 40)
344 len = 40;
345 else
346 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400347 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400348 } else if (reply->type == XCB_ATOM_ATOM) {
349 atom_value = xcb_get_property_value(reply);
350 for (i = 0; i < reply->value_len; i++) {
351 name = get_atom_name(wm->conn, atom_value[i]);
352 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400353 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400354 width = 4;
355 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400356 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400357 }
358
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400359 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400360 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400361 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400362 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400363 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400364 }
365}
366
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200367static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400368read_and_dump_property(struct weston_wm *wm,
369 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400370{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400371 xcb_get_property_reply_t *reply;
372 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400373
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400374 cookie = xcb_get_property(wm->conn, 0, window,
375 property, XCB_ATOM_ANY, 0, 2048);
376 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400377
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400378 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400379
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400380 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400381}
382
383/* We reuse some predefined, but otherwise useles atoms */
384#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
385#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500386#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700387#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400388
389static void
390weston_wm_window_read_properties(struct weston_wm_window *window)
391{
392 struct weston_wm *wm = window->wm;
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200393 struct weston_shell_interface *shell_interface =
394 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400395
396#define F(field) offsetof(struct weston_wm_window, field)
397 const struct {
398 xcb_atom_t atom;
399 xcb_atom_t type;
400 int offset;
401 } props[] = {
402 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
403 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
404 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
405 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700406 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500407 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400408 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
409 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300410 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400411 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300412 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400413 };
414#undef F
415
416 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
417 xcb_get_property_reply_t *reply;
418 void *p;
419 uint32_t *xid;
420 xcb_atom_t *atom;
421 uint32_t i;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200422 char name[1024];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400423
424 if (!window->properties_dirty)
425 return;
426 window->properties_dirty = 0;
427
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400428 for (i = 0; i < ARRAY_LENGTH(props); i++)
429 cookie[i] = xcb_get_property(wm->conn,
430 0, /* delete */
431 window->id,
432 props[i].atom,
433 XCB_ATOM_ANY, 0, 2048);
434
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700435 window->decorate = window->override_redirect ? 0 : MWM_DECOR_EVERYTHING;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700436 window->size_hints.flags = 0;
437 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700438 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700439
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400440 for (i = 0; i < ARRAY_LENGTH(props); i++) {
441 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
442 if (!reply)
443 /* Bad window, typically */
444 continue;
445 if (reply->type == XCB_ATOM_NONE) {
446 /* No such property */
447 free(reply);
448 continue;
449 }
450
451 p = ((char *) window + props[i].offset);
452
453 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300454 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400455 case XCB_ATOM_STRING:
456 /* FIXME: We're using this for both string and
457 utf8_string */
458 if (*(char **) p)
459 free(*(char **) p);
460
461 *(char **) p =
462 strndup(xcb_get_property_value(reply),
463 xcb_get_property_value_length(reply));
464 break;
465 case XCB_ATOM_WINDOW:
466 xid = xcb_get_property_value(reply);
Derek Foreman49372142015-04-09 10:51:22 -0500467 if (!wm_lookup_window(wm, *xid, p))
468 weston_log("XCB_ATOM_WINDOW contains window"
469 " id not found in hash table.\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400470 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300471 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400472 case XCB_ATOM_ATOM:
473 atom = xcb_get_property_value(reply);
474 *(xcb_atom_t *) p = *atom;
475 break;
476 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700477 atom = xcb_get_property_value(reply);
478 for (i = 0; i < reply->value_len; i++)
Derek Foremanb4deec62015-04-07 12:12:13 -0500479 if (atom[i] == wm->atom.wm_delete_window) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700480 window->delete_window = 1;
Derek Foremanb4deec62015-04-07 12:12:13 -0500481 break;
482 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400483 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700484 case TYPE_WM_NORMAL_HINTS:
485 memcpy(&window->size_hints,
486 xcb_get_property_value(reply),
487 sizeof window->size_hints);
488 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500489 case TYPE_NET_WM_STATE:
490 window->fullscreen = 0;
491 atom = xcb_get_property_value(reply);
Ryo Munakataf3744f52015-03-11 17:36:30 +0900492 for (i = 0; i < reply->value_len; i++) {
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500493 if (atom[i] == wm->atom.net_wm_state_fullscreen)
494 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200495 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
496 window->maximized_vert = 1;
497 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
498 window->maximized_horz = 1;
Ryo Munakataf3744f52015-03-11 17:36:30 +0900499 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500500 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400501 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700502 memcpy(&window->motif_hints,
503 xcb_get_property_value(reply),
504 sizeof window->motif_hints);
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700505 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) {
506 if (window->motif_hints.decorations & MWM_DECOR_ALL)
507 /* MWM_DECOR_ALL means all except the other values listed. */
508 window->decorate =
509 MWM_DECOR_EVERYTHING & (~window->motif_hints.decorations);
510 else
511 window->decorate =
512 window->motif_hints.decorations;
513 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400514 break;
515 default:
516 break;
517 }
518 free(reply);
519 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200520
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200521 if (window->pid > 0) {
522 gethostname(name, sizeof(name));
523 for (i = 0; i < sizeof(name); i++) {
524 if (name[i] == '\0')
525 break;
526 }
527 if (i == sizeof(name))
528 name[0] = '\0'; /* ignore stupid hostnames */
529
530 /* this is only one heuristic to guess the PID of a client is
531 * valid, assuming it's compliant with icccm and ewmh.
532 * Non-compliants and remote applications of course fail. */
533 if (!window->machine || strcmp(window->machine, name))
534 window->pid = 0;
535 }
536
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200537 if (window->shsurf && window->name)
538 shell_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500539 if (window->frame && window->name)
540 frame_set_title(window->frame, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200541 if (window->shsurf && window->pid > 0)
542 shell_interface->set_pid(window->shsurf, window->pid);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400543}
544
545static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400546weston_wm_window_get_frame_size(struct weston_wm_window *window,
547 int *width, int *height)
548{
549 struct theme *t = window->wm->theme;
550
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500551 if (window->fullscreen) {
552 *width = window->width;
553 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800554 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500555 *width = frame_width(window->frame);
556 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400557 } else {
558 *width = window->width + t->margin * 2;
559 *height = window->height + t->margin * 2;
560 }
561}
562
563static void
564weston_wm_window_get_child_position(struct weston_wm_window *window,
565 int *x, int *y)
566{
567 struct theme *t = window->wm->theme;
568
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500569 if (window->fullscreen) {
570 *x = 0;
571 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800572 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500573 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400574 } else {
575 *x = t->margin;
576 *y = t->margin;
577 }
578}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400579
580static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500581weston_wm_window_send_configure_notify(struct weston_wm_window *window)
582{
583 xcb_configure_notify_event_t configure_notify;
584 struct weston_wm *wm = window->wm;
585 int x, y;
586
587 weston_wm_window_get_child_position(window, &x, &y);
588 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
589 configure_notify.pad0 = 0;
590 configure_notify.event = window->id;
591 configure_notify.window = window->id;
592 configure_notify.above_sibling = XCB_WINDOW_NONE;
593 configure_notify.x = x;
594 configure_notify.y = y;
595 configure_notify.width = window->width;
596 configure_notify.height = window->height;
597 configure_notify.border_width = 0;
598 configure_notify.override_redirect = 0;
599 configure_notify.pad1 = 0;
600
Murray Calavera883ac022015-06-06 13:02:22 +0000601 xcb_send_event(wm->conn, 0, window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500602 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
603 (char *) &configure_notify);
604}
605
606static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400607weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
608{
Murray Calavera883ac022015-06-06 13:02:22 +0000609 xcb_configure_request_event_t *configure_request =
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400610 (xcb_configure_request_event_t *) event;
611 struct weston_wm_window *window;
612 uint32_t mask, values[16];
613 int x, y, width, height, i = 0;
614
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400615 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
616 configure_request->window,
617 configure_request->x, configure_request->y,
618 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400619
Derek Foreman49372142015-04-09 10:51:22 -0500620 if (!wm_lookup_window(wm, configure_request->window, &window))
621 return;
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400622
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500623 if (window->fullscreen) {
624 weston_wm_window_send_configure_notify(window);
625 return;
626 }
627
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400628 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
629 window->width = configure_request->width;
630 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
631 window->height = configure_request->height;
632
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500633 if (window->frame)
634 frame_resize_inside(window->frame, window->width, window->height);
635
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400636 weston_wm_window_get_child_position(window, &x, &y);
637 values[i++] = x;
638 values[i++] = y;
639 values[i++] = window->width;
640 values[i++] = window->height;
641 values[i++] = 0;
642 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
643 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
644 XCB_CONFIG_WINDOW_BORDER_WIDTH;
645 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
646 values[i++] = configure_request->sibling;
647 mask |= XCB_CONFIG_WINDOW_SIBLING;
648 }
649 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
650 values[i++] = configure_request->stack_mode;
651 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
652 }
653
654 xcb_configure_window(wm->conn, window->id, mask, values);
655
656 weston_wm_window_get_frame_size(window, &width, &height);
657 values[0] = width;
658 values[1] = height;
659 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
660 xcb_configure_window(wm->conn, window->frame_id, mask, values);
661
662 weston_wm_window_schedule_repaint(window);
663}
664
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400665static int
666our_resource(struct weston_wm *wm, uint32_t id)
667{
668 const xcb_setup_t *setup;
669
670 setup = xcb_get_setup(wm->conn);
671
672 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
673}
674
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400675static void
676weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
677{
Murray Calavera883ac022015-06-06 13:02:22 +0000678 xcb_configure_notify_event_t *configure_notify =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400679 (xcb_configure_notify_event_t *) event;
680 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400681
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400682 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400683 configure_notify->window,
684 configure_notify->x, configure_notify->y,
685 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300686
Derek Foreman49372142015-04-09 10:51:22 -0500687 if (!wm_lookup_window(wm, configure_notify->window, &window))
688 return;
689
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700690 window->x = configure_notify->x;
691 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700692 if (window->override_redirect) {
693 window->width = configure_notify->width;
694 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500695 if (window->frame)
696 frame_resize_inside(window->frame,
697 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700698 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400699}
700
701static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300702weston_wm_kill_client(struct wl_listener *listener, void *data)
703{
704 struct weston_surface *surface = data;
705 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300706 if (!window)
707 return;
708
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200709 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300710 kill(window->pid, SIGKILL);
711}
712
713static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700714weston_wm_create_surface(struct wl_listener *listener, void *data)
715{
716 struct weston_surface *surface = data;
717 struct weston_wm *wm =
718 container_of(listener,
719 struct weston_wm, create_surface_listener);
720 struct weston_wm_window *window;
721
722 if (wl_resource_get_client(surface->resource) != wm->server->client)
723 return;
724
725 wl_list_for_each(window, &wm->unpaired_window_list, link)
726 if (window->surface_id ==
727 wl_resource_get_id(surface->resource)) {
728 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700729 window->surface_id = 0;
730 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700731 break;
Murray Calavera883ac022015-06-06 13:02:22 +0000732 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700733}
734
735static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400736weston_wm_window_activate(struct wl_listener *listener, void *data)
737{
738 struct weston_surface *surface = data;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200739 struct weston_wm_window *window = NULL;
Scott Moreau85ecac02012-05-21 15:49:14 -0600740 struct weston_wm *wm =
741 container_of(listener, struct weston_wm, activate_listener);
742 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400743
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200744 if (surface) {
745 window = get_wm_window(surface);
746 }
747
Scott Moreau85ecac02012-05-21 15:49:14 -0600748 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700749 uint32_t values[1];
750
Boyan Dingb9f863c2014-08-30 10:33:23 +0800751 if (window->override_redirect)
752 return;
753
Scott Moreau85ecac02012-05-21 15:49:14 -0600754 client_message.response_type = XCB_CLIENT_MESSAGE;
755 client_message.format = 32;
756 client_message.window = window->id;
757 client_message.type = wm->atom.wm_protocols;
758 client_message.data.data32[0] = wm->atom.wm_take_focus;
759 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
760
Murray Calavera883ac022015-06-06 13:02:22 +0000761 xcb_send_event(wm->conn, 0, window->id,
Scott Moreau85ecac02012-05-21 15:49:14 -0600762 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
763 (char *) &client_message);
764
765 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
766 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700767
768 values[0] = XCB_STACK_MODE_ABOVE;
769 xcb_configure_window (wm->conn, window->frame_id,
770 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600771 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400772 xcb_set_input_focus (wm->conn,
773 XCB_INPUT_FOCUS_POINTER_ROOT,
774 XCB_NONE,
775 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600776 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400777
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500778 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800779 if (wm->focus_window->frame)
780 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400781 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500782 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400783 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500784 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800785 if (wm->focus_window->frame)
786 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400787 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500788 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400789}
790
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300791static void
792weston_wm_window_transform(struct wl_listener *listener, void *data)
793{
794 struct weston_surface *surface = data;
795 struct weston_wm_window *window = get_wm_window(surface);
796 struct weston_wm *wm =
797 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300798 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300799
800 if (!window || !wm)
801 return;
802
Jason Ekstranda7af7042013-10-12 22:38:11 -0500803 if (!window->view || !weston_view_is_mapped(window->view))
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300804 return;
805
Jason Ekstranda7af7042013-10-12 22:38:11 -0500806 if (window->x != window->view->geometry.x ||
807 window->y != window->view->geometry.y) {
808 values[0] = window->view->geometry.x;
809 values[1] = window->view->geometry.y;
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700810 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300811
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700812 xcb_configure_window(wm->conn, window->frame_id, mask, values);
813 xcb_flush(wm->conn);
814 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300815}
816
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400817#define ICCCM_WITHDRAWN_STATE 0
818#define ICCCM_NORMAL_STATE 1
819#define ICCCM_ICONIC_STATE 3
820
821static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500822weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400823{
824 struct weston_wm *wm = window->wm;
825 uint32_t property[2];
826
827 property[0] = state;
828 property[1] = XCB_WINDOW_NONE;
829
830 xcb_change_property(wm->conn,
831 XCB_PROP_MODE_REPLACE,
832 window->id,
833 wm->atom.wm_state,
834 wm->atom.wm_state,
835 32, /* format */
836 2, property);
837}
838
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400839static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500840weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
841{
842 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200843 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500844 int i;
845
846 i = 0;
847 if (window->fullscreen)
848 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200849 if (window->maximized_vert)
850 property[i++] = wm->atom.net_wm_state_maximized_vert;
851 if (window->maximized_horz)
852 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500853
854 xcb_change_property(wm->conn,
855 XCB_PROP_MODE_REPLACE,
856 window->id,
857 wm->atom.net_wm_state,
858 XCB_ATOM_ATOM,
859 32, /* format */
860 i, property);
861}
862
863static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700864weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400865{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700866 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400867 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400868 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200869 int buttons = FRAME_BUTTON_CLOSE;
870
871 if (window->decorate & MWM_DECOR_MAXIMIZE)
872 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400873
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500874 window->frame = frame_create(window->wm->theme,
875 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200876 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500877 frame_resize_inside(window->frame, window->width, window->height);
878
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400879 weston_wm_window_get_frame_size(window, &width, &height);
880 weston_wm_window_get_child_position(window, &x, &y);
881
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400882 values[0] = wm->screen->black_pixel;
883 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400884 XCB_EVENT_MASK_KEY_PRESS |
885 XCB_EVENT_MASK_KEY_RELEASE |
886 XCB_EVENT_MASK_BUTTON_PRESS |
887 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400888 XCB_EVENT_MASK_POINTER_MOTION |
889 XCB_EVENT_MASK_ENTER_WINDOW |
890 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400891 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400892 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400893 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400894
895 window->frame_id = xcb_generate_id(wm->conn);
896 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400897 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400898 window->frame_id,
899 wm->screen->root,
900 0, 0,
901 width, height,
902 0,
903 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400904 wm->visual_id,
905 XCB_CW_BORDER_PIXEL |
906 XCB_CW_EVENT_MASK |
907 XCB_CW_COLORMAP, values);
908
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400909 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
910
911 values[0] = 0;
912 xcb_configure_window(wm->conn, window->id,
913 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
914
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400915 window->cairo_surface =
916 cairo_xcb_surface_create_with_xrender_format(wm->conn,
917 wm->screen,
918 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400919 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400920 width, height);
921
922 hash_table_insert(wm->window_hash, window->frame_id, window);
923}
924
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200925/*
926 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
927 * Passing a <0 desktop value deletes the property.
928 */
929static void
930weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
Bryce Harringtone00554b2015-06-12 10:17:39 -0700931 int desktop)
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200932{
933 if (desktop >= 0) {
934 xcb_change_property(window->wm->conn,
935 XCB_PROP_MODE_REPLACE,
936 window->id,
937 window->wm->atom.net_wm_desktop,
938 XCB_ATOM_CARDINAL,
939 32, /* format */
940 1, &desktop);
941 } else {
942 xcb_delete_property(window->wm->conn,
943 window->id,
944 window->wm->atom.net_wm_desktop);
945 }
946}
947
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400948static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700949weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
950{
951 xcb_map_request_event_t *map_request =
952 (xcb_map_request_event_t *) event;
953 struct weston_wm_window *window;
954
955 if (our_resource(wm, map_request->window)) {
956 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
957 map_request->window);
958 return;
959 }
960
Derek Foreman49372142015-04-09 10:51:22 -0500961 if (!wm_lookup_window(wm, map_request->window, &window))
962 return;
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700963
964 weston_wm_window_read_properties(window);
965
966 if (window->frame_id == XCB_WINDOW_NONE)
967 weston_wm_window_create_frame(window);
968
969 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
970 window->id, window, window->frame_id);
971
972 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
973 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200974 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700975
976 xcb_map_window(wm->conn, map_request->window);
977 xcb_map_window(wm->conn, window->frame_id);
978}
979
980static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400981weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
982{
983 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
984
985 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400986 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
987 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400988 return;
989 }
990
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400991 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400992}
993
994static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400995weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
996{
997 xcb_unmap_notify_event_t *unmap_notify =
998 (xcb_unmap_notify_event_t *) event;
999 struct weston_wm_window *window;
1000
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001001 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
1002 unmap_notify->window,
1003 unmap_notify->event,
1004 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001005
1006 if (our_resource(wm, unmap_notify->window))
1007 return;
1008
MoD31700122013-06-11 19:58:55 -05001009 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -04001010 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1011 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001012 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001013
Derek Foreman49372142015-04-09 10:51:22 -05001014 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1015 return;
1016
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001017 if (window->surface_id) {
1018 /* Make sure we're not on the unpaired surface list or we
1019 * could be assigned a surface during surface creation that
1020 * was mapped before this unmap request.
1021 */
1022 wl_list_remove(&window->link);
1023 window->surface_id = 0;
1024 }
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001025 if (wm->focus_window == window)
1026 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001027 if (window->surface)
1028 wl_list_remove(&window->surface_destroy_listener.link);
1029 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +02001030 window->shsurf = NULL;
Dima Ryazanove5a32082013-11-15 02:01:18 -08001031 window->view = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001032
1033 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1034 weston_wm_window_set_virtual_desktop(window, -1);
1035
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001036 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001037}
1038
1039static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001040weston_wm_window_draw_decoration(void *data)
1041{
1042 struct weston_wm_window *window = data;
1043 struct weston_wm *wm = window->wm;
1044 struct theme *t = wm->theme;
1045 cairo_t *cr;
1046 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001047 int32_t input_x, input_y, input_w, input_h;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07001048 struct weston_shell_interface *shell_interface =
1049 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001050 uint32_t flags = 0;
1051
1052 weston_wm_window_read_properties(window);
1053
1054 window->repaint_source = NULL;
1055
1056 weston_wm_window_get_frame_size(window, &width, &height);
1057 weston_wm_window_get_child_position(window, &x, &y);
1058
1059 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1060 cr = cairo_create(window->cairo_surface);
1061
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001062 if (window->fullscreen) {
1063 /* nothing */
1064 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001065 if (wm->focus_window == window)
1066 flags |= THEME_FRAME_ACTIVE;
1067
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001068 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001069 } else {
1070 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1071 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1072 cairo_paint(cr);
1073
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001074 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001075 }
1076
1077 cairo_destroy(cr);
1078
1079 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001080 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -05001081 if(window->has_alpha) {
1082 pixman_region32_init(&window->surface->pending.opaque);
1083 } else {
1084 /* We leave an extra pixel around the X window area to
1085 * make sure we don't sample from the undefined alpha
1086 * channel when filtering. */
Murray Calavera883ac022015-06-06 13:02:22 +00001087 pixman_region32_init_rect(&window->surface->pending.opaque,
MoD384a11a2013-06-22 11:04:21 -05001088 x - 1, y - 1,
1089 window->width + 2,
1090 window->height + 2);
1091 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001092 if (window->view)
1093 weston_view_geometry_dirty(window->view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001094
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001095 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001096
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001097 if (window->decorate && !window->fullscreen) {
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001098 frame_input_rect(window->frame, &input_x, &input_y,
1099 &input_w, &input_h);
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001100 } else {
1101 input_x = x;
1102 input_y = y;
1103 input_w = width;
1104 input_h = height;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001105 }
1106
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001107 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001108 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001109
1110 shell_interface->set_window_geometry(window->shsurf,
1111 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001112 }
1113}
1114
1115static void
1116weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1117{
1118 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001119 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001120
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001121 if (window->frame_id == XCB_WINDOW_NONE) {
1122 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001123 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001124 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -05001125 if(window->has_alpha) {
1126 pixman_region32_init(&window->surface->pending.opaque);
1127 } else {
1128 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1129 width, height);
1130 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001131 if (window->view)
1132 weston_view_geometry_dirty(window->view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001133 }
1134 return;
1135 }
1136
1137 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001138 return;
1139
1140 window->repaint_source =
1141 wl_event_loop_add_idle(wm->server->loop,
1142 weston_wm_window_draw_decoration,
1143 window);
1144}
1145
1146static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001147weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1148{
1149 xcb_property_notify_event_t *property_notify =
1150 (xcb_property_notify_event_t *) event;
1151 struct weston_wm_window *window;
1152
Derek Foreman49372142015-04-09 10:51:22 -05001153 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001154 return;
1155
1156 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001157
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001158 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001159 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001160 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001161 else
1162 read_and_dump_property(wm, property_notify->window,
1163 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001164
1165 if (property_notify->atom == wm->atom.net_wm_name ||
1166 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001167 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001168}
1169
1170static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001171weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001172 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001173{
1174 struct weston_wm_window *window;
1175 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001176 xcb_get_geometry_cookie_t geometry_cookie;
1177 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001178
Peter Huttererf3d62272013-08-08 11:57:05 +10001179 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001180 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001181 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001182 return;
1183 }
1184
MoD384a11a2013-06-22 11:04:21 -05001185 geometry_cookie = xcb_get_geometry(wm->conn, id);
1186
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001187 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1188 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1189
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001190 window->wm = wm;
1191 window->id = id;
1192 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001193 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001194 window->width = width;
1195 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001196 window->x = x;
1197 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001198
MoD384a11a2013-06-22 11:04:21 -05001199 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1200 /* technically we should use XRender and check the visual format's
1201 alpha_mask, but checking depth is simpler and works in all known cases */
1202 if(geometry_reply != NULL)
1203 window->has_alpha = geometry_reply->depth == 32;
1204 free(geometry_reply);
1205
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001206 hash_table_insert(wm->window_hash, id, window);
1207}
1208
1209static void
1210weston_wm_window_destroy(struct weston_wm_window *window)
1211{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001212 struct weston_wm *wm = window->wm;
1213
1214 if (window->repaint_source)
1215 wl_event_source_remove(window->repaint_source);
1216 if (window->cairo_surface)
1217 cairo_surface_destroy(window->cairo_surface);
1218
1219 if (window->frame_id) {
1220 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1221 xcb_destroy_window(wm->conn, window->frame_id);
1222 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001223 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001224 hash_table_remove(wm->window_hash, window->frame_id);
1225 window->frame_id = XCB_WINDOW_NONE;
1226 }
1227
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001228 if (window->surface_id)
1229 wl_list_remove(&window->link);
1230
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001231 if (window->surface)
1232 wl_list_remove(&window->surface_destroy_listener.link);
1233
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001234 hash_table_remove(window->wm->window_hash, window->id);
1235 free(window);
1236}
1237
1238static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001239weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1240{
1241 xcb_create_notify_event_t *create_notify =
1242 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001243
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001244 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1245 create_notify->window,
1246 create_notify->width, create_notify->height,
1247 create_notify->override_redirect ? ", override" : "",
1248 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001249
1250 if (our_resource(wm, create_notify->window))
1251 return;
1252
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001253 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001254 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001255 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001256 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001257}
1258
1259static void
1260weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1261{
1262 xcb_destroy_notify_event_t *destroy_notify =
1263 (xcb_destroy_notify_event_t *) event;
1264 struct weston_wm_window *window;
1265
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001266 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1267 destroy_notify->window,
1268 destroy_notify->event,
1269 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001270
1271 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001272 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001273
Derek Foreman49372142015-04-09 10:51:22 -05001274 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1275 return;
1276
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001277 weston_wm_window_destroy(window);
1278}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001279
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001280static void
1281weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1282{
1283 xcb_reparent_notify_event_t *reparent_notify =
1284 (xcb_reparent_notify_event_t *) event;
1285 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001286
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001287 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1288 reparent_notify->window,
1289 reparent_notify->parent,
1290 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001291
1292 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001293 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001294 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001295 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001296 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001297 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1298 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001299 weston_wm_window_destroy(window);
1300 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001301}
1302
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001303struct weston_seat *
1304weston_wm_pick_seat(struct weston_wm *wm)
1305{
1306 return container_of(wm->server->compositor->seat_list.next,
1307 struct weston_seat, link);
1308}
1309
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001310static struct weston_seat *
1311weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1312{
1313 struct weston_wm *wm = window->wm;
1314 struct weston_seat *seat, *s;
1315
1316 seat = NULL;
1317 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Giulio Camuffoad7305a2014-10-04 13:58:33 +03001318 if (s->pointer != NULL && s->pointer->focus &&
1319 s->pointer->focus->surface == window->surface &&
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001320 s->pointer->button_count > 0 &&
1321 (seat == NULL ||
1322 s->pointer->grab_serial -
1323 seat->pointer->grab_serial < (1 << 30)))
1324 seat = s;
1325 }
1326
1327 return seat;
1328}
1329
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001330static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001331weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1332 xcb_client_message_event_t *client_message)
1333{
1334 static const int map[] = {
1335 THEME_LOCATION_RESIZING_TOP_LEFT,
1336 THEME_LOCATION_RESIZING_TOP,
1337 THEME_LOCATION_RESIZING_TOP_RIGHT,
1338 THEME_LOCATION_RESIZING_RIGHT,
1339 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1340 THEME_LOCATION_RESIZING_BOTTOM,
1341 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1342 THEME_LOCATION_RESIZING_LEFT
1343 };
1344
1345 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001346 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001347 int detail;
1348 struct weston_shell_interface *shell_interface =
1349 &wm->server->compositor->shell_interface;
1350
Boyan Dingc06a1802014-07-06 11:44:58 +08001351 if (seat == NULL || seat->pointer->button_count != 1
Giulio Camuffoad7305a2014-10-04 13:58:33 +03001352 || !seat->pointer->focus
1353 || seat->pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001354 return;
1355
1356 detail = client_message->data.data32[2];
1357 switch (detail) {
1358 case _NET_WM_MOVERESIZE_MOVE:
1359 shell_interface->move(window->shsurf, seat);
1360 break;
1361 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1362 case _NET_WM_MOVERESIZE_SIZE_TOP:
1363 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1364 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1365 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1366 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1367 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1368 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1369 shell_interface->resize(window->shsurf, seat, map[detail]);
1370 break;
1371 case _NET_WM_MOVERESIZE_CANCEL:
1372 break;
1373 }
1374}
1375
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001376#define _NET_WM_STATE_REMOVE 0
1377#define _NET_WM_STATE_ADD 1
1378#define _NET_WM_STATE_TOGGLE 2
1379
1380static int
1381update_state(int action, int *state)
1382{
1383 int new_state, changed;
1384
1385 switch (action) {
1386 case _NET_WM_STATE_REMOVE:
1387 new_state = 0;
1388 break;
1389 case _NET_WM_STATE_ADD:
1390 new_state = 1;
1391 break;
1392 case _NET_WM_STATE_TOGGLE:
1393 new_state = !*state;
1394 break;
1395 default:
1396 return 0;
1397 }
1398
1399 changed = (*state != new_state);
1400 *state = new_state;
1401
1402 return changed;
1403}
1404
1405static void
1406weston_wm_window_configure(void *data);
1407
1408static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001409weston_wm_window_set_toplevel(struct weston_wm_window *window)
1410{
1411 struct weston_shell_interface *shell_interface =
1412 &window->wm->server->compositor->shell_interface;
1413
1414 shell_interface->set_toplevel(window->shsurf);
1415 window->width = window->saved_width;
1416 window->height = window->saved_height;
1417 if (window->frame)
1418 frame_resize_inside(window->frame,
1419 window->width,
1420 window->height);
1421 weston_wm_window_configure(window);
1422}
1423
1424static inline bool
1425weston_wm_window_is_maximized(struct weston_wm_window *window)
1426{
1427 return window->maximized_horz && window->maximized_vert;
1428}
1429
1430static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001431weston_wm_window_handle_state(struct weston_wm_window *window,
1432 xcb_client_message_event_t *client_message)
1433{
1434 struct weston_wm *wm = window->wm;
1435 struct weston_shell_interface *shell_interface =
1436 &wm->server->compositor->shell_interface;
1437 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001438 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001439
1440 action = client_message->data.data32[0];
1441 property = client_message->data.data32[1];
1442
1443 if (property == wm->atom.net_wm_state_fullscreen &&
1444 update_state(action, &window->fullscreen)) {
1445 weston_wm_window_set_net_wm_state(window);
1446 if (window->fullscreen) {
1447 window->saved_width = window->width;
1448 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001449
1450 if (window->shsurf)
1451 shell_interface->set_fullscreen(window->shsurf,
1452 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1453 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001454 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001455 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001456 weston_wm_window_set_toplevel(window);
1457 }
1458 } else {
1459 if (property == wm->atom.net_wm_state_maximized_vert &&
1460 update_state(action, &window->maximized_vert))
1461 weston_wm_window_set_net_wm_state(window);
1462 if (property == wm->atom.net_wm_state_maximized_horz &&
1463 update_state(action, &window->maximized_horz))
1464 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001465
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001466 if (maximized != weston_wm_window_is_maximized(window)) {
1467 if (weston_wm_window_is_maximized(window)) {
1468 window->saved_width = window->width;
1469 window->saved_height = window->height;
1470
1471 if (window->shsurf)
1472 shell_interface->set_maximized(window->shsurf);
1473 } else if (window->shsurf) {
1474 weston_wm_window_set_toplevel(window);
1475 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001476 }
1477 }
1478}
1479
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001480static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001481surface_destroy(struct wl_listener *listener, void *data)
1482{
1483 struct weston_wm_window *window =
1484 container_of(listener,
1485 struct weston_wm_window, surface_destroy_listener);
1486
1487 wm_log("surface for xid %d destroyed\n", window->id);
1488
1489 /* This should have been freed by the shell.
1490 * Don't try to use it later. */
1491 window->shsurf = NULL;
1492 window->surface = NULL;
1493 window->view = NULL;
1494}
1495
1496static void
1497weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1498 xcb_client_message_event_t *client_message)
1499{
1500 struct weston_wm *wm = window->wm;
1501 struct wl_resource *resource;
1502
1503 if (window->surface_id != 0) {
1504 wm_log("already have surface id for window %d\n", window->id);
1505 return;
1506 }
1507
1508 /* Xwayland will send the wayland requests to create the
1509 * wl_surface before sending this client message. Even so, we
1510 * can end up handling the X event before the wayland requests
1511 * and thus when we try to look up the surface ID, the surface
1512 * hasn't been created yet. In that case put the window on
1513 * the unpaired window list and continue when the surface gets
1514 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001515 uint32_t id = client_message->data.data32[0];
1516 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001517 if (resource) {
1518 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001519 xserver_map_shell_surface(window,
1520 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001521 }
1522 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001523 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001524 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001525 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001526}
1527
1528static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001529weston_wm_handle_client_message(struct weston_wm *wm,
1530 xcb_generic_event_t *event)
1531{
1532 xcb_client_message_event_t *client_message =
1533 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001534 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001535
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001536 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1537 get_atom_name(wm->conn, client_message->type),
1538 client_message->data.data32[0],
1539 client_message->data.data32[1],
1540 client_message->data.data32[2],
1541 client_message->data.data32[3],
1542 client_message->data.data32[4],
1543 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001544
Jason Ekstrand0250a742014-07-24 13:17:47 -07001545 /* The window may get created and destroyed before we actually
1546 * handle the message. If it doesn't exist, bail.
1547 */
Derek Foreman49372142015-04-09 10:51:22 -05001548 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001549 return;
1550
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001551 if (client_message->type == wm->atom.net_wm_moveresize)
1552 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001553 else if (client_message->type == wm->atom.net_wm_state)
1554 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001555 else if (client_message->type == wm->atom.wl_surface_id)
1556 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001557}
1558
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001559enum cursor_type {
1560 XWM_CURSOR_TOP,
1561 XWM_CURSOR_BOTTOM,
1562 XWM_CURSOR_LEFT,
1563 XWM_CURSOR_RIGHT,
1564 XWM_CURSOR_TOP_LEFT,
1565 XWM_CURSOR_TOP_RIGHT,
1566 XWM_CURSOR_BOTTOM_LEFT,
1567 XWM_CURSOR_BOTTOM_RIGHT,
1568 XWM_CURSOR_LEFT_PTR,
1569};
1570
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001571/*
1572 * The following correspondences between file names and cursors was copied
1573 * from: https://bugs.kde.org/attachment.cgi?id=67313
1574 */
1575
1576static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001577 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001578 "sw-resize",
1579 "size_bdiag"
1580};
1581
1582static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001583 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001584 "se-resize",
1585 "size_fdiag"
1586};
1587
1588static const char *bottom_sides[] = {
1589 "bottom_side",
1590 "s-resize",
1591 "size_ver"
1592};
1593
1594static const char *left_ptrs[] = {
1595 "left_ptr",
1596 "default",
1597 "top_left_arrow",
1598 "left-arrow"
1599};
1600
1601static const char *left_sides[] = {
1602 "left_side",
1603 "w-resize",
1604 "size_hor"
1605};
1606
1607static const char *right_sides[] = {
1608 "right_side",
1609 "e-resize",
1610 "size_hor"
1611};
1612
1613static const char *top_left_corners[] = {
1614 "top_left_corner",
1615 "nw-resize",
1616 "size_fdiag"
1617};
1618
1619static const char *top_right_corners[] = {
1620 "top_right_corner",
1621 "ne-resize",
1622 "size_bdiag"
1623};
1624
1625static const char *top_sides[] = {
1626 "top_side",
1627 "n-resize",
1628 "size_ver"
1629};
1630
1631struct cursor_alternatives {
1632 const char **names;
1633 size_t count;
1634};
1635
1636static const struct cursor_alternatives cursors[] = {
1637 {top_sides, ARRAY_LENGTH(top_sides)},
1638 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1639 {left_sides, ARRAY_LENGTH(left_sides)},
1640 {right_sides, ARRAY_LENGTH(right_sides)},
1641 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1642 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1643 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1644 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1645 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001646};
1647
1648static void
1649weston_wm_create_cursors(struct weston_wm *wm)
1650{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001651 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001652 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001653 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001654
1655 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1656 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001657 for (j = 0; j < cursors[i].count; j++) {
1658 name = cursors[i].names[j];
1659 wm->cursors[i] =
1660 xcb_cursor_library_load_cursor(wm, name);
1661 if (wm->cursors[i] != (xcb_cursor_t)-1)
1662 break;
1663 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001664 }
1665
1666 wm->last_cursor = -1;
1667}
1668
1669static void
1670weston_wm_destroy_cursors(struct weston_wm *wm)
1671{
1672 uint8_t i;
1673
1674 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1675 xcb_free_cursor(wm->conn, wm->cursors[i]);
1676
1677 free(wm->cursors);
1678}
1679
1680static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001681get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001682{
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001683 // int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001684
1685 switch (location) {
1686 case THEME_LOCATION_RESIZING_TOP:
1687 return XWM_CURSOR_TOP;
1688 case THEME_LOCATION_RESIZING_BOTTOM:
1689 return XWM_CURSOR_BOTTOM;
1690 case THEME_LOCATION_RESIZING_LEFT:
1691 return XWM_CURSOR_LEFT;
1692 case THEME_LOCATION_RESIZING_RIGHT:
1693 return XWM_CURSOR_RIGHT;
1694 case THEME_LOCATION_RESIZING_TOP_LEFT:
1695 return XWM_CURSOR_TOP_LEFT;
1696 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1697 return XWM_CURSOR_TOP_RIGHT;
1698 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1699 return XWM_CURSOR_BOTTOM_LEFT;
1700 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1701 return XWM_CURSOR_BOTTOM_RIGHT;
1702 case THEME_LOCATION_EXTERIOR:
1703 case THEME_LOCATION_TITLEBAR:
1704 default:
1705 return XWM_CURSOR_LEFT_PTR;
1706 }
1707}
1708
1709static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001710weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1711 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001712{
1713 uint32_t cursor_value_list;
1714
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001715 if (wm->last_cursor == cursor)
1716 return;
1717
1718 wm->last_cursor = cursor;
1719
1720 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001721 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001722 XCB_CW_CURSOR, &cursor_value_list);
1723 xcb_flush(wm->conn);
1724}
1725
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001726static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001727weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001728{
1729 xcb_client_message_event_t client_message;
1730
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001731 if (window->delete_window) {
1732 client_message.response_type = XCB_CLIENT_MESSAGE;
1733 client_message.format = 32;
1734 client_message.window = window->id;
1735 client_message.type = window->wm->atom.wm_protocols;
1736 client_message.data.data32[0] =
1737 window->wm->atom.wm_delete_window;
1738 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001739
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001740 xcb_send_event(window->wm->conn, 0, window->id,
1741 XCB_EVENT_MASK_NO_EVENT,
1742 (char *) &client_message);
1743 } else {
1744 xcb_kill_client(window->wm->conn, window->id);
1745 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001746}
1747
1748static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001749weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1750{
1751 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1752 struct weston_shell_interface *shell_interface =
1753 &wm->server->compositor->shell_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001754 struct weston_seat *seat;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001755 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001756 enum theme_location location;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001757 enum frame_button_state button_state;
1758 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001759
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001760 wm_log("XCB_BUTTON_%s (detail %d)\n",
1761 button->response_type == XCB_BUTTON_PRESS ?
1762 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001763
Derek Foreman49372142015-04-09 10:51:22 -05001764 if (!wm_lookup_window(wm, button->event, &window) ||
1765 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001766 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001767
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001768 if (button->detail != 1 && button->detail != 2)
1769 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001770
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001771 seat = weston_wm_pick_seat_for_window(window);
1772
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001773 button_state = button->response_type == XCB_BUTTON_PRESS ?
1774 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1775 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1776
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001777 /* Make sure we're looking at the right location. The frame
1778 * could have received a motion event from a pointer from a
1779 * different wl_seat, but under X it looks like our core
1780 * pointer moved. Move the frame pointer to the button press
1781 * location before deciding what to do. */
1782 location = frame_pointer_motion(window->frame, NULL,
1783 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001784 location = frame_pointer_button(window->frame, NULL,
1785 button_id, button_state);
1786 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1787 weston_wm_window_schedule_repaint(window);
1788
1789 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Boyan Ding3c6d20c2014-09-03 17:25:30 +08001790 if (seat != NULL)
1791 shell_interface->move(window->shsurf, seat);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001792 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1793 }
1794
1795 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Boyan Ding3c6d20c2014-09-03 17:25:30 +08001796 if (seat != NULL)
1797 shell_interface->resize(window->shsurf, seat, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001798 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1799 }
1800
1801 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001802 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001803 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001804 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001805
1806 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1807 window->maximized_horz = !window->maximized_horz;
1808 window->maximized_vert = !window->maximized_vert;
1809 if (weston_wm_window_is_maximized(window)) {
1810 window->saved_width = window->width;
1811 window->saved_height = window->height;
1812 shell_interface->set_maximized(window->shsurf);
1813 } else {
1814 weston_wm_window_set_toplevel(window);
1815 }
1816 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1817 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001818}
1819
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001820static void
1821weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1822{
1823 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1824 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001825 enum theme_location location;
1826 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001827
Derek Foreman49372142015-04-09 10:51:22 -05001828 if (!wm_lookup_window(wm, motion->event, &window) ||
1829 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001830 return;
1831
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001832 location = frame_pointer_motion(window->frame, NULL,
1833 motion->event_x, motion->event_y);
1834 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1835 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001836
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001837 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001838 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001839}
1840
1841static void
1842weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1843{
1844 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1845 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001846 enum theme_location location;
1847 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001848
Derek Foreman49372142015-04-09 10:51:22 -05001849 if (!wm_lookup_window(wm, enter->event, &window) ||
1850 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001851 return;
1852
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001853 location = frame_pointer_enter(window->frame, NULL,
1854 enter->event_x, enter->event_y);
1855 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1856 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001857
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001858 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001859 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001860}
1861
1862static void
1863weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1864{
1865 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1866 struct weston_wm_window *window;
1867
Derek Foreman49372142015-04-09 10:51:22 -05001868 if (!wm_lookup_window(wm, leave->event, &window) ||
1869 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001870 return;
1871
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001872 frame_pointer_leave(window->frame, NULL);
1873 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1874 weston_wm_window_schedule_repaint(window);
1875
Tiago Vignattic1903232012-07-16 12:15:37 -04001876 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001877}
1878
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001879static int
1880weston_wm_handle_event(int fd, uint32_t mask, void *data)
1881{
1882 struct weston_wm *wm = data;
1883 xcb_generic_event_t *event;
1884 int count = 0;
1885
1886 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1887 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001888 free(event);
1889 count++;
1890 continue;
1891 }
1892
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001893 if (weston_wm_handle_dnd_event(wm, event)) {
1894 free(event);
1895 count++;
1896 continue;
1897 }
1898
MoD31700122013-06-11 19:58:55 -05001899 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001900 case XCB_BUTTON_PRESS:
1901 case XCB_BUTTON_RELEASE:
1902 weston_wm_handle_button(wm, event);
1903 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001904 case XCB_ENTER_NOTIFY:
1905 weston_wm_handle_enter(wm, event);
1906 break;
1907 case XCB_LEAVE_NOTIFY:
1908 weston_wm_handle_leave(wm, event);
1909 break;
1910 case XCB_MOTION_NOTIFY:
1911 weston_wm_handle_motion(wm, event);
1912 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001913 case XCB_CREATE_NOTIFY:
1914 weston_wm_handle_create_notify(wm, event);
1915 break;
1916 case XCB_MAP_REQUEST:
1917 weston_wm_handle_map_request(wm, event);
1918 break;
1919 case XCB_MAP_NOTIFY:
1920 weston_wm_handle_map_notify(wm, event);
1921 break;
1922 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001923 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001924 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001925 case XCB_REPARENT_NOTIFY:
1926 weston_wm_handle_reparent_notify(wm, event);
1927 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001928 case XCB_CONFIGURE_REQUEST:
1929 weston_wm_handle_configure_request(wm, event);
1930 break;
1931 case XCB_CONFIGURE_NOTIFY:
1932 weston_wm_handle_configure_notify(wm, event);
1933 break;
1934 case XCB_DESTROY_NOTIFY:
1935 weston_wm_handle_destroy_notify(wm, event);
1936 break;
1937 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001938 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001939 break;
1940 case XCB_PROPERTY_NOTIFY:
1941 weston_wm_handle_property_notify(wm, event);
1942 break;
1943 case XCB_CLIENT_MESSAGE:
1944 weston_wm_handle_client_message(wm, event);
1945 break;
1946 }
1947
1948 free(event);
1949 count++;
1950 }
1951
1952 xcb_flush(wm->conn);
1953
1954 return count;
1955}
1956
1957static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001958weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1959{
1960 xcb_depth_iterator_t d_iter;
1961 xcb_visualtype_iterator_t vt_iter;
1962 xcb_visualtype_t *visualtype;
1963
1964 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1965 visualtype = NULL;
1966 while (d_iter.rem > 0) {
1967 if (d_iter.data->depth == 32) {
1968 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1969 visualtype = vt_iter.data;
1970 break;
1971 }
1972
1973 xcb_depth_next(&d_iter);
1974 }
1975
1976 if (visualtype == NULL) {
1977 weston_log("no 32 bit visualtype\n");
1978 return;
1979 }
1980
1981 wm->visual_id = visualtype->visual_id;
1982 wm->colormap = xcb_generate_id(wm->conn);
1983 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1984 wm->colormap, wm->screen->root, wm->visual_id);
1985}
1986
1987static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001988weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001989{
1990
1991#define F(field) offsetof(struct weston_wm, field)
1992
1993 static const struct { const char *name; int offset; } atoms[] = {
1994 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001995 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001996 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1997 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001998 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001999 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002000 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002001 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002002 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002003 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002004 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2005 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002006 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2007 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002008 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2009 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2010 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02002011 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002012 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2013
2014 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2015 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2016 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2017 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2018 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2019 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2020 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002021 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2022 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002023 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2024 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2025 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2026 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2027 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2028
2029 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2030 { "_NET_SUPPORTING_WM_CHECK",
2031 F(atom.net_supporting_wm_check) },
2032 { "_NET_SUPPORTED", F(atom.net_supported) },
2033 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2034 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002035 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002036 { "TARGETS", F(atom.targets) },
2037 { "UTF8_STRING", F(atom.utf8_string) },
2038 { "_WL_SELECTION", F(atom.wl_selection) },
2039 { "INCR", F(atom.incr) },
2040 { "TIMESTAMP", F(atom.timestamp) },
2041 { "MULTIPLE", F(atom.multiple) },
2042 { "UTF8_STRING" , F(atom.utf8_string) },
2043 { "COMPOUND_TEXT", F(atom.compound_text) },
2044 { "TEXT", F(atom.text) },
2045 { "STRING", F(atom.string) },
2046 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2047 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002048 { "XdndSelection", F(atom.xdnd_selection) },
2049 { "XdndAware", F(atom.xdnd_aware) },
2050 { "XdndEnter", F(atom.xdnd_enter) },
2051 { "XdndLeave", F(atom.xdnd_leave) },
2052 { "XdndDrop", F(atom.xdnd_drop) },
2053 { "XdndStatus", F(atom.xdnd_status) },
2054 { "XdndFinished", F(atom.xdnd_finished) },
2055 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002056 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2057 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002058 };
2059#undef F
2060
2061 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2062 xcb_xfixes_query_version_reply_t *xfixes_reply;
2063 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2064 xcb_intern_atom_reply_t *reply;
2065 xcb_render_query_pict_formats_reply_t *formats_reply;
2066 xcb_render_query_pict_formats_cookie_t formats_cookie;
2067 xcb_render_pictforminfo_t *formats;
2068 uint32_t i;
2069
2070 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002071 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002072
2073 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2074
2075 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2076 cookies[i] = xcb_intern_atom (wm->conn, 0,
2077 strlen(atoms[i].name),
2078 atoms[i].name);
2079
2080 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2081 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2082 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2083 free(reply);
2084 }
2085
2086 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2087 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002088 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002089
2090 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2091 XCB_XFIXES_MAJOR_VERSION,
2092 XCB_XFIXES_MINOR_VERSION);
2093 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2094 xfixes_cookie, NULL);
2095
Martin Minarik6d118362012-06-07 18:01:59 +02002096 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002097 xfixes_reply->major_version, xfixes_reply->minor_version);
2098
2099 free(xfixes_reply);
2100
2101 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2102 formats_cookie, 0);
2103 if (formats_reply == NULL)
2104 return;
2105
2106 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002107 for (i = 0; i < formats_reply->num_formats; i++) {
2108 if (formats[i].direct.red_mask != 0xff &&
2109 formats[i].direct.red_shift != 16)
2110 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002111 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2112 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002113 wm->format_rgb = formats[i];
2114 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2115 formats[i].depth == 32 &&
2116 formats[i].direct.alpha_mask == 0xff &&
2117 formats[i].direct.alpha_shift == 24)
2118 wm->format_rgba = formats[i];
2119 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002120
2121 free(formats_reply);
2122}
2123
2124static void
2125weston_wm_create_wm_window(struct weston_wm *wm)
2126{
2127 static const char name[] = "Weston WM";
2128
2129 wm->wm_window = xcb_generate_id(wm->conn);
2130 xcb_create_window(wm->conn,
2131 XCB_COPY_FROM_PARENT,
2132 wm->wm_window,
2133 wm->screen->root,
2134 0, 0,
2135 10, 10,
2136 0,
2137 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2138 wm->screen->root_visual,
2139 0, NULL);
2140
2141 xcb_change_property(wm->conn,
2142 XCB_PROP_MODE_REPLACE,
2143 wm->wm_window,
2144 wm->atom.net_supporting_wm_check,
2145 XCB_ATOM_WINDOW,
2146 32, /* format */
2147 1, &wm->wm_window);
2148
2149 xcb_change_property(wm->conn,
2150 XCB_PROP_MODE_REPLACE,
2151 wm->wm_window,
2152 wm->atom.net_wm_name,
2153 wm->atom.utf8_string,
2154 8, /* format */
2155 strlen(name), name);
2156
2157 xcb_change_property(wm->conn,
2158 XCB_PROP_MODE_REPLACE,
2159 wm->screen->root,
2160 wm->atom.net_supporting_wm_check,
2161 XCB_ATOM_WINDOW,
2162 32, /* format */
2163 1, &wm->wm_window);
2164
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002165 /* Claim the WM_S0 selection even though we don't suport
2166 * the --replace functionality. */
2167 xcb_set_selection_owner(wm->conn,
2168 wm->wm_window,
2169 wm->atom.wm_s0,
2170 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002171
2172 xcb_set_selection_owner(wm->conn,
2173 wm->wm_window,
2174 wm->atom.net_wm_cm_s0,
2175 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002176}
2177
2178struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002179weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002180{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002181 struct weston_wm *wm;
2182 struct wl_event_loop *loop;
2183 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002184 uint32_t values[1];
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002185 xcb_atom_t supported[5];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002186
Peter Huttererf3d62272013-08-08 11:57:05 +10002187 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002188 if (wm == NULL)
2189 return NULL;
2190
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002191 wm->server = wxs;
2192 wm->window_hash = hash_table_create();
2193 if (wm->window_hash == NULL) {
2194 free(wm);
2195 return NULL;
2196 }
2197
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002198 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002199 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002200 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002201 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002202 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002203 hash_table_destroy(wm->window_hash);
2204 free(wm);
2205 return NULL;
2206 }
2207
2208 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2209 wm->screen = s.data;
2210
2211 loop = wl_display_get_event_loop(wxs->wl_display);
2212 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002213 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002214 WL_EVENT_READABLE,
2215 weston_wm_handle_event, wm);
2216 wl_event_source_check(wm->source);
2217
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002218 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002219 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002220
2221 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002222 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2223 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2224 XCB_EVENT_MASK_PROPERTY_CHANGE;
2225 xcb_change_window_attributes(wm->conn, wm->screen->root,
2226 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002227
2228 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2229 XCB_COMPOSITE_REDIRECT_MANUAL);
2230
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002231 wm->theme = theme_create();
2232
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002233 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002234 supported[1] = wm->atom.net_wm_state;
2235 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002236 supported[3] = wm->atom.net_wm_state_maximized_vert;
2237 supported[4] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002238 xcb_change_property(wm->conn,
2239 XCB_PROP_MODE_REPLACE,
2240 wm->screen->root,
2241 wm->atom.net_supported,
2242 XCB_ATOM_ATOM,
2243 32, /* format */
2244 ARRAY_LENGTH(supported), supported);
2245
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002246 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002247
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002248 weston_wm_dnd_init(wm);
2249
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002250 xcb_flush(wm->conn);
2251
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002252 wm->create_surface_listener.notify = weston_wm_create_surface;
2253 wl_signal_add(&wxs->compositor->create_surface_signal,
2254 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002255 wm->activate_listener.notify = weston_wm_window_activate;
2256 wl_signal_add(&wxs->compositor->activate_signal,
2257 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002258 wm->transform_listener.notify = weston_wm_window_transform;
2259 wl_signal_add(&wxs->compositor->transform_signal,
2260 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002261 wm->kill_listener.notify = weston_wm_kill_client;
2262 wl_signal_add(&wxs->compositor->kill_signal,
2263 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002264 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002265
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002266 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002267 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002268
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002269 /* Create wm window and take WM_S0 selection last, which
2270 * signals to Xwayland that we're done with setup. */
2271 weston_wm_create_wm_window(wm);
2272
2273 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002274
2275 return wm;
2276}
2277
2278void
2279weston_wm_destroy(struct weston_wm *wm)
2280{
2281 /* FIXME: Free windows in hash. */
2282 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002283 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002284 xcb_disconnect(wm->conn);
2285 wl_event_source_remove(wm->source);
2286 wl_list_remove(&wm->selection_listener.link);
2287 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002288 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01002289 wl_list_remove(&wm->transform_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002290 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002291
2292 free(wm);
2293}
2294
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002295static struct weston_wm_window *
2296get_wm_window(struct weston_surface *surface)
2297{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002298 struct wl_listener *listener;
2299
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002300 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002301 if (listener)
2302 return container_of(listener, struct weston_wm_window,
2303 surface_destroy_listener);
2304
2305 return NULL;
2306}
2307
2308static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002309weston_wm_window_configure(void *data)
2310{
2311 struct weston_wm_window *window = data;
2312 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002313 uint32_t values[4];
2314 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002315
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002316 weston_wm_window_get_child_position(window, &x, &y);
2317 values[0] = x;
2318 values[1] = y;
2319 values[2] = window->width;
2320 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002321 xcb_configure_window(wm->conn,
2322 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002323 XCB_CONFIG_WINDOW_X |
2324 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002325 XCB_CONFIG_WINDOW_WIDTH |
2326 XCB_CONFIG_WINDOW_HEIGHT,
2327 values);
2328
2329 weston_wm_window_get_frame_size(window, &width, &height);
2330 values[0] = width;
2331 values[1] = height;
2332 xcb_configure_window(wm->conn,
2333 window->frame_id,
2334 XCB_CONFIG_WINDOW_WIDTH |
2335 XCB_CONFIG_WINDOW_HEIGHT,
2336 values);
2337
2338 window->configure_source = NULL;
2339
2340 weston_wm_window_schedule_repaint(window);
2341}
2342
2343static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002344send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002345{
2346 struct weston_wm_window *window = get_wm_window(surface);
2347 struct weston_wm *wm = window->wm;
2348 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002349 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002350
Marek Chalupae9fe4672014-10-29 13:44:44 +01002351 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002352 hborder = 2 * t->width;
2353 vborder = t->titlebar_height + t->width;
2354 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002355 hborder = 0;
2356 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002357 }
2358
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002359 if (width > hborder)
2360 window->width = width - hborder;
2361 else
2362 window->width = 1;
2363
2364 if (height > vborder)
2365 window->height = height - vborder;
2366 else
2367 window->height = 1;
2368
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002369 if (window->frame)
2370 frame_resize_inside(window->frame, window->width, window->height);
2371
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002372 if (window->configure_source)
2373 return;
2374
2375 window->configure_source =
2376 wl_event_loop_add_idle(wm->server->loop,
2377 weston_wm_window_configure, window);
2378}
2379
2380static const struct weston_shell_client shell_client = {
2381 send_configure
2382};
2383
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002384static int
2385legacy_fullscreen(struct weston_wm *wm,
2386 struct weston_wm_window *window,
2387 struct weston_output **output_ret)
2388{
2389 struct weston_compositor *compositor = wm->server->compositor;
2390 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002391 uint32_t minmax = PMinSize | PMaxSize;
2392 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002393
2394 /* Heuristics for detecting legacy fullscreen windows... */
2395
2396 wl_list_for_each(output, &compositor->output_list, link) {
2397 if (output->x == window->x &&
2398 output->y == window->y &&
2399 output->width == window->width &&
2400 output->height == window->height &&
2401 window->override_redirect) {
2402 *output_ret = output;
2403 return 1;
2404 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002405
2406 matching_size = 0;
2407 if ((window->size_hints.flags & (USSize |PSize)) &&
2408 window->size_hints.width == output->width &&
2409 window->size_hints.height == output->height)
2410 matching_size = 1;
2411 if ((window->size_hints.flags & minmax) == minmax &&
2412 window->size_hints.min_width == output->width &&
2413 window->size_hints.min_height == output->height &&
2414 window->size_hints.max_width == output->width &&
2415 window->size_hints.max_height == output->height)
2416 matching_size = 1;
2417
2418 if (matching_size && !window->decorate &&
2419 (window->size_hints.flags & (USPosition | PPosition)) &&
2420 window->size_hints.x == output->x &&
2421 window->size_hints.y == output->y) {
2422 *output_ret = output;
2423 return 1;
2424 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002425 }
2426
2427 return 0;
2428}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002429
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002430static bool
2431weston_wm_window_type_inactive(struct weston_wm_window *window)
2432{
2433 struct weston_wm *wm = window->wm;
2434
2435 return window->type == wm->atom.net_wm_window_type_tooltip ||
2436 window->type == wm->atom.net_wm_window_type_dropdown ||
2437 window->type == wm->atom.net_wm_window_type_dnd ||
2438 window->type == wm->atom.net_wm_window_type_combo ||
Giulio Camuffo84787ea2015-04-29 19:00:48 +03002439 window->type == wm->atom.net_wm_window_type_popup ||
2440 window->type == wm->atom.net_wm_window_type_utility;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002441}
2442
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002443static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002444xserver_map_shell_surface(struct weston_wm_window *window,
2445 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002446{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002447 struct weston_wm *wm = window->wm;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002448 struct weston_shell_interface *shell_interface =
2449 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002450 struct weston_output *output;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002451 struct weston_wm_window *parent;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002452 int flags = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002453
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002454 weston_wm_window_read_properties(window);
2455
2456 /* A weston_wm_window may have many different surfaces assigned
2457 * throughout its life, so we must make sure to remove the listener
2458 * from the old surface signal list. */
2459 if (window->surface)
2460 wl_list_remove(&window->surface_destroy_listener.link);
2461
2462 window->surface = surface;
2463 window->surface_destroy_listener.notify = surface_destroy;
2464 wl_signal_add(&window->surface->destroy_signal,
2465 &window->surface_destroy_listener);
2466
2467 weston_wm_window_schedule_repaint(window);
2468
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002469 if (!shell_interface->create_shell_surface)
2470 return;
2471
Jason Ekstranda7af7042013-10-12 22:38:11 -05002472 if (!shell_interface->get_primary_view)
2473 return;
2474
Pekka Paalanen50b67472014-10-01 15:02:41 +03002475 if (window->surface->configure) {
2476 weston_log("warning, unexpected in %s: "
2477 "surface's configure hook is already set.\n",
2478 __func__);
2479 return;
2480 }
2481
Murray Calavera883ac022015-06-06 13:02:22 +00002482 window->shsurf =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002483 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002484 window->surface,
2485 &shell_client);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002486 window->view = shell_interface->get_primary_view(shell_interface->shell,
2487 window->shsurf);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002488
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002489 if (window->name)
2490 shell_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002491 if (window->pid > 0)
2492 shell_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002493
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002494 if (window->fullscreen) {
2495 window->saved_width = window->width;
2496 window->saved_height = window->height;
2497 shell_interface->set_fullscreen(window->shsurf,
2498 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2499 0, NULL);
2500 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002501 } else if (legacy_fullscreen(wm, window, &output)) {
2502 window->fullscreen = 1;
2503 shell_interface->set_fullscreen(window->shsurf,
2504 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2505 0, output);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002506 } else if (window->override_redirect) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002507 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002508 window->x,
2509 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002510 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Axel Davye4450f92014-01-12 15:06:05 +01002511 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002512 parent = window->transient_for;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002513 if (weston_wm_window_type_inactive(window))
2514 flags = WL_SHELL_SURFACE_TRANSIENT_INACTIVE;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002515 shell_interface->set_transient(window->shsurf,
2516 parent->surface,
Axel Davyfa506b62014-01-12 15:06:04 +01002517 window->x - parent->x,
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002518 window->y - parent->y, flags);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002519 } else if (weston_wm_window_is_maximized(window)) {
2520 shell_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002521 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002522 if (weston_wm_window_type_inactive(window)) {
2523 shell_interface->set_xwayland(window->shsurf,
2524 window->x,
2525 window->y,
2526 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2527 } else {
2528 shell_interface->set_toplevel(window->shsurf);
2529 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002530 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002531}