blob: ad608b718c2fcab61b40656bdcef076ca6bedf5a [file] [log] [blame]
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040024
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <sys/socket.h>
29#include <sys/un.h>
30#include <fcntl.h>
31#include <errno.h>
32#include <unistd.h>
33#include <signal.h>
Tiago Vignatti90fada42012-07-16 12:02:08 -040034#include <X11/Xcursor/Xcursor.h>
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -050035#include <linux/input.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040036
37#include "xwayland.h"
38
Kristian Høgsberg2ba10df2013-12-03 16:38:15 -080039#include "cairo-util.h"
40#include "compositor.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040041#include "hash.h"
42
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070043struct wm_size_hints {
44 uint32_t flags;
45 int32_t x, y;
46 int32_t width, height; /* should set so old wm's don't mess up */
47 int32_t min_width, min_height;
48 int32_t max_width, max_height;
49 int32_t width_inc, height_inc;
50 struct {
51 int32_t x;
52 int32_t y;
53 } min_aspect, max_aspect;
54 int32_t base_width, base_height;
55 int32_t win_gravity;
56};
57
58#define USPosition (1L << 0)
59#define USSize (1L << 1)
60#define PPosition (1L << 2)
61#define PSize (1L << 3)
62#define PMinSize (1L << 4)
63#define PMaxSize (1L << 5)
64#define PResizeInc (1L << 6)
65#define PAspect (1L << 7)
66#define PBaseSize (1L << 8)
67#define PWinGravity (1L << 9)
68
Kristian Høgsberg380deee2012-05-21 17:12:41 -040069struct motif_wm_hints {
70 uint32_t flags;
71 uint32_t functions;
72 uint32_t decorations;
73 int32_t input_mode;
74 uint32_t status;
75};
76
77#define MWM_HINTS_FUNCTIONS (1L << 0)
78#define MWM_HINTS_DECORATIONS (1L << 1)
79#define MWM_HINTS_INPUT_MODE (1L << 2)
80#define MWM_HINTS_STATUS (1L << 3)
81
82#define MWM_FUNC_ALL (1L << 0)
83#define MWM_FUNC_RESIZE (1L << 1)
84#define MWM_FUNC_MOVE (1L << 2)
85#define MWM_FUNC_MINIMIZE (1L << 3)
86#define MWM_FUNC_MAXIMIZE (1L << 4)
87#define MWM_FUNC_CLOSE (1L << 5)
88
89#define MWM_DECOR_ALL (1L << 0)
90#define MWM_DECOR_BORDER (1L << 1)
91#define MWM_DECOR_RESIZEH (1L << 2)
92#define MWM_DECOR_TITLE (1L << 3)
93#define MWM_DECOR_MENU (1L << 4)
94#define MWM_DECOR_MINIMIZE (1L << 5)
95#define MWM_DECOR_MAXIMIZE (1L << 6)
96
97#define MWM_INPUT_MODELESS 0
98#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
99#define MWM_INPUT_SYSTEM_MODAL 2
100#define MWM_INPUT_FULL_APPLICATION_MODAL 3
101#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
102
103#define MWM_TEAROFF_WINDOW (1L<<0)
104
105#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
106#define _NET_WM_MOVERESIZE_SIZE_TOP 1
107#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
108#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
109#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
110#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
111#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
112#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
113#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
114#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
115#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
116#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
117
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400118struct weston_wm_window {
119 struct weston_wm *wm;
120 xcb_window_t id;
121 xcb_window_t frame_id;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500122 struct frame *frame;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400123 cairo_surface_t *cairo_surface;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700124 uint32_t surface_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400125 struct weston_surface *surface;
126 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500127 struct weston_view *view;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400128 struct wl_listener surface_destroy_listener;
129 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400130 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400131 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300132 int pid;
133 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400134 char *class;
135 char *name;
136 struct weston_wm_window *transient_for;
137 uint32_t protocols;
138 xcb_atom_t type;
139 int width, height;
140 int x, y;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500141 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400142 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300143 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500144 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500145 int has_alpha;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700146 int delete_window;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200147 int maximized_vert;
148 int maximized_horz;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700149 struct wm_size_hints size_hints;
150 struct motif_wm_hints motif_hints;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700151 struct wl_list link;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400152};
153
154static struct weston_wm_window *
155get_wm_window(struct weston_surface *surface);
156
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400157static void
158weston_wm_window_schedule_repaint(struct weston_wm_window *window);
159
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700160static void
161xserver_map_shell_surface(struct weston_wm_window *window,
162 struct weston_surface *surface);
163
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400164static int __attribute__ ((format (printf, 1, 2)))
165wm_log(const char *fmt, ...)
166{
167#ifdef WM_DEBUG
168 int l;
169 va_list argp;
170
171 va_start(argp, fmt);
172 l = weston_vlog(fmt, argp);
173 va_end(argp);
174
175 return l;
176#else
177 return 0;
178#endif
179}
180
181static int __attribute__ ((format (printf, 1, 2)))
182wm_log_continue(const char *fmt, ...)
183{
184#ifdef WM_DEBUG
185 int l;
186 va_list argp;
187
188 va_start(argp, fmt);
189 l = weston_vlog_continue(fmt, argp);
190 va_end(argp);
191
192 return l;
193#else
194 return 0;
195#endif
196}
197
198
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400199const char *
200get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
201{
202 xcb_get_atom_name_cookie_t cookie;
203 xcb_get_atom_name_reply_t *reply;
204 xcb_generic_error_t *e;
205 static char buffer[64];
206
207 if (atom == XCB_ATOM_NONE)
208 return "None";
209
210 cookie = xcb_get_atom_name (c, atom);
211 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500212
213 if(reply) {
214 snprintf(buffer, sizeof buffer, "%.*s",
215 xcb_get_atom_name_name_length (reply),
216 xcb_get_atom_name_name (reply));
217 } else {
218 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
219 }
220
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400221 free(reply);
222
223 return buffer;
224}
225
Tiago Vignatti90fada42012-07-16 12:02:08 -0400226static xcb_cursor_t
227xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
228{
229 xcb_connection_t *c = wm->conn;
230 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
231 xcb_screen_t *screen = s.data;
232 xcb_gcontext_t gc;
233 xcb_pixmap_t pix;
234 xcb_render_picture_t pic;
235 xcb_cursor_t cursor;
236 int stride = img->width * 4;
237
238 pix = xcb_generate_id(c);
239 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
240
241 pic = xcb_generate_id(c);
242 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
243
244 gc = xcb_generate_id(c);
245 xcb_create_gc(c, gc, pix, 0, 0);
246
247 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
248 img->width, img->height, 0, 0, 0, 32,
249 stride * img->height, (uint8_t *) img->pixels);
250 xcb_free_gc(c, gc);
251
252 cursor = xcb_generate_id(c);
253 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
254
255 xcb_render_free_picture(c, pic);
256 xcb_free_pixmap(c, pix);
257
258 return cursor;
259}
260
261static xcb_cursor_t
262xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
263{
264 /* TODO: treat animated cursors as well */
265 if (images->nimage != 1)
266 return -1;
267
268 return xcb_cursor_image_load_cursor(wm, images->images[0]);
269}
270
271static xcb_cursor_t
272xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
273{
274 xcb_cursor_t cursor;
275 XcursorImages *images;
276 char *v = NULL;
277 int size = 0;
278
279 if (!file)
280 return 0;
281
282 v = getenv ("XCURSOR_SIZE");
283 if (v)
284 size = atoi(v);
285
286 if (!size)
287 size = 32;
288
289 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300290 if (!images)
291 return -1;
292
Tiago Vignatti90fada42012-07-16 12:02:08 -0400293 cursor = xcb_cursor_images_load_cursor (wm, images);
294 XcursorImagesDestroy (images);
295
296 return cursor;
297}
298
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400299void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400300dump_property(struct weston_wm *wm,
301 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400302{
303 int32_t *incr_value;
304 const char *text_value, *name;
305 xcb_atom_t *atom_value;
306 int width, len;
307 uint32_t i;
308
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400309 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400310 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400311 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400312 return;
313 }
314
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400315 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
316 get_atom_name(wm->conn, reply->type),
317 reply->format,
318 xcb_get_property_value_length(reply),
319 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400320
321 if (reply->type == wm->atom.incr) {
322 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400323 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400324 } else if (reply->type == wm->atom.utf8_string ||
325 reply->type == wm->atom.string) {
326 text_value = xcb_get_property_value(reply);
327 if (reply->value_len > 40)
328 len = 40;
329 else
330 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400331 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400332 } else if (reply->type == XCB_ATOM_ATOM) {
333 atom_value = xcb_get_property_value(reply);
334 for (i = 0; i < reply->value_len; i++) {
335 name = get_atom_name(wm->conn, atom_value[i]);
336 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400337 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400338 width = 4;
339 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400340 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400341 }
342
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400343 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400344 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400345 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400346 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400347 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400348 }
349}
350
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200351static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400352read_and_dump_property(struct weston_wm *wm,
353 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400354{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400355 xcb_get_property_reply_t *reply;
356 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400357
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400358 cookie = xcb_get_property(wm->conn, 0, window,
359 property, XCB_ATOM_ANY, 0, 2048);
360 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400361
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400362 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400363
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400364 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400365}
366
367/* We reuse some predefined, but otherwise useles atoms */
368#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
369#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500370#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700371#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400372
373static void
374weston_wm_window_read_properties(struct weston_wm_window *window)
375{
376 struct weston_wm *wm = window->wm;
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200377 struct weston_shell_interface *shell_interface =
378 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400379
380#define F(field) offsetof(struct weston_wm_window, field)
381 const struct {
382 xcb_atom_t atom;
383 xcb_atom_t type;
384 int offset;
385 } props[] = {
386 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
387 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
388 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
389 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700390 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500391 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400392 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
393 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300394 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400395 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300396 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400397 };
398#undef F
399
400 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
401 xcb_get_property_reply_t *reply;
402 void *p;
403 uint32_t *xid;
404 xcb_atom_t *atom;
405 uint32_t i;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400406
407 if (!window->properties_dirty)
408 return;
409 window->properties_dirty = 0;
410
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400411 for (i = 0; i < ARRAY_LENGTH(props); i++)
412 cookie[i] = xcb_get_property(wm->conn,
413 0, /* delete */
414 window->id,
415 props[i].atom,
416 XCB_ATOM_ANY, 0, 2048);
417
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300418 window->decorate = !window->override_redirect;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700419 window->size_hints.flags = 0;
420 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700421 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700422
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400423 for (i = 0; i < ARRAY_LENGTH(props); i++) {
424 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
425 if (!reply)
426 /* Bad window, typically */
427 continue;
428 if (reply->type == XCB_ATOM_NONE) {
429 /* No such property */
430 free(reply);
431 continue;
432 }
433
434 p = ((char *) window + props[i].offset);
435
436 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300437 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400438 case XCB_ATOM_STRING:
439 /* FIXME: We're using this for both string and
440 utf8_string */
441 if (*(char **) p)
442 free(*(char **) p);
443
444 *(char **) p =
445 strndup(xcb_get_property_value(reply),
446 xcb_get_property_value_length(reply));
447 break;
448 case XCB_ATOM_WINDOW:
449 xid = xcb_get_property_value(reply);
450 *(struct weston_wm_window **) p =
451 hash_table_lookup(wm->window_hash, *xid);
452 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300453 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400454 case XCB_ATOM_ATOM:
455 atom = xcb_get_property_value(reply);
456 *(xcb_atom_t *) p = *atom;
457 break;
458 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700459 atom = xcb_get_property_value(reply);
460 for (i = 0; i < reply->value_len; i++)
461 if (atom[i] == wm->atom.wm_delete_window)
462 window->delete_window = 1;
463 break;
464
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400465 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700466 case TYPE_WM_NORMAL_HINTS:
467 memcpy(&window->size_hints,
468 xcb_get_property_value(reply),
469 sizeof window->size_hints);
470 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500471 case TYPE_NET_WM_STATE:
472 window->fullscreen = 0;
473 atom = xcb_get_property_value(reply);
Ryo Munakataf3744f52015-03-11 17:36:30 +0900474 for (i = 0; i < reply->value_len; i++) {
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500475 if (atom[i] == wm->atom.net_wm_state_fullscreen)
476 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200477 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
478 window->maximized_vert = 1;
479 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
480 window->maximized_horz = 1;
Ryo Munakataf3744f52015-03-11 17:36:30 +0900481 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500482 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400483 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700484 memcpy(&window->motif_hints,
485 xcb_get_property_value(reply),
486 sizeof window->motif_hints);
487 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS)
488 window->decorate =
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200489 window->motif_hints.decorations;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400490 break;
491 default:
492 break;
493 }
494 free(reply);
495 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200496
497 if (window->shsurf && window->name)
498 shell_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500499 if (window->frame && window->name)
500 frame_set_title(window->frame, window->name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400501}
502
503static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400504weston_wm_window_get_frame_size(struct weston_wm_window *window,
505 int *width, int *height)
506{
507 struct theme *t = window->wm->theme;
508
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500509 if (window->fullscreen) {
510 *width = window->width;
511 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800512 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500513 *width = frame_width(window->frame);
514 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400515 } else {
516 *width = window->width + t->margin * 2;
517 *height = window->height + t->margin * 2;
518 }
519}
520
521static void
522weston_wm_window_get_child_position(struct weston_wm_window *window,
523 int *x, int *y)
524{
525 struct theme *t = window->wm->theme;
526
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500527 if (window->fullscreen) {
528 *x = 0;
529 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800530 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500531 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400532 } else {
533 *x = t->margin;
534 *y = t->margin;
535 }
536}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400537
538static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500539weston_wm_window_send_configure_notify(struct weston_wm_window *window)
540{
541 xcb_configure_notify_event_t configure_notify;
542 struct weston_wm *wm = window->wm;
543 int x, y;
544
545 weston_wm_window_get_child_position(window, &x, &y);
546 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
547 configure_notify.pad0 = 0;
548 configure_notify.event = window->id;
549 configure_notify.window = window->id;
550 configure_notify.above_sibling = XCB_WINDOW_NONE;
551 configure_notify.x = x;
552 configure_notify.y = y;
553 configure_notify.width = window->width;
554 configure_notify.height = window->height;
555 configure_notify.border_width = 0;
556 configure_notify.override_redirect = 0;
557 configure_notify.pad1 = 0;
558
559 xcb_send_event(wm->conn, 0, window->id,
560 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
561 (char *) &configure_notify);
562}
563
564static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400565weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
566{
567 xcb_configure_request_event_t *configure_request =
568 (xcb_configure_request_event_t *) event;
569 struct weston_wm_window *window;
570 uint32_t mask, values[16];
571 int x, y, width, height, i = 0;
572
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400573 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
574 configure_request->window,
575 configure_request->x, configure_request->y,
576 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400577
578 window = hash_table_lookup(wm->window_hash, configure_request->window);
579
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500580 if (window->fullscreen) {
581 weston_wm_window_send_configure_notify(window);
582 return;
583 }
584
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400585 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
586 window->width = configure_request->width;
587 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
588 window->height = configure_request->height;
589
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500590 if (window->frame)
591 frame_resize_inside(window->frame, window->width, window->height);
592
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400593 weston_wm_window_get_child_position(window, &x, &y);
594 values[i++] = x;
595 values[i++] = y;
596 values[i++] = window->width;
597 values[i++] = window->height;
598 values[i++] = 0;
599 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
600 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
601 XCB_CONFIG_WINDOW_BORDER_WIDTH;
602 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
603 values[i++] = configure_request->sibling;
604 mask |= XCB_CONFIG_WINDOW_SIBLING;
605 }
606 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
607 values[i++] = configure_request->stack_mode;
608 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
609 }
610
611 xcb_configure_window(wm->conn, window->id, mask, values);
612
613 weston_wm_window_get_frame_size(window, &width, &height);
614 values[0] = width;
615 values[1] = height;
616 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
617 xcb_configure_window(wm->conn, window->frame_id, mask, values);
618
619 weston_wm_window_schedule_repaint(window);
620}
621
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400622static int
623our_resource(struct weston_wm *wm, uint32_t id)
624{
625 const xcb_setup_t *setup;
626
627 setup = xcb_get_setup(wm->conn);
628
629 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
630}
631
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400632static void
633weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
634{
635 xcb_configure_notify_event_t *configure_notify =
636 (xcb_configure_notify_event_t *) event;
637 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400638
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400639 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400640 configure_notify->window,
641 configure_notify->x, configure_notify->y,
642 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300643
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400644 window = hash_table_lookup(wm->window_hash, configure_notify->window);
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700645 window->x = configure_notify->x;
646 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700647 if (window->override_redirect) {
648 window->width = configure_notify->width;
649 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500650 if (window->frame)
651 frame_resize_inside(window->frame,
652 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700653 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400654}
655
656static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300657weston_wm_kill_client(struct wl_listener *listener, void *data)
658{
659 struct weston_surface *surface = data;
660 struct weston_wm_window *window = get_wm_window(surface);
661 char name[1024];
662
663 if (!window)
664 return;
665
666 gethostname(name, 1024);
667
668 /* this is only one heuristic to guess the PID of a client is valid,
669 * assuming it's compliant with icccm and ewmh. Non-compliants and
670 * remote applications of course fail. */
671 if (!strcmp(window->machine, name) && window->pid != 0)
672 kill(window->pid, SIGKILL);
673}
674
675static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700676weston_wm_create_surface(struct wl_listener *listener, void *data)
677{
678 struct weston_surface *surface = data;
679 struct weston_wm *wm =
680 container_of(listener,
681 struct weston_wm, create_surface_listener);
682 struct weston_wm_window *window;
683
684 if (wl_resource_get_client(surface->resource) != wm->server->client)
685 return;
686
687 wl_list_for_each(window, &wm->unpaired_window_list, link)
688 if (window->surface_id ==
689 wl_resource_get_id(surface->resource)) {
690 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700691 window->surface_id = 0;
692 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700693 break;
694 }
695}
696
697static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400698weston_wm_window_activate(struct wl_listener *listener, void *data)
699{
700 struct weston_surface *surface = data;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200701 struct weston_wm_window *window = NULL;
Scott Moreau85ecac02012-05-21 15:49:14 -0600702 struct weston_wm *wm =
703 container_of(listener, struct weston_wm, activate_listener);
704 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400705
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200706 if (surface) {
707 window = get_wm_window(surface);
708 }
709
Scott Moreau85ecac02012-05-21 15:49:14 -0600710 if (window) {
Boyan Dingb9f863c2014-08-30 10:33:23 +0800711 if (window->override_redirect)
712 return;
713
Scott Moreau85ecac02012-05-21 15:49:14 -0600714 client_message.response_type = XCB_CLIENT_MESSAGE;
715 client_message.format = 32;
716 client_message.window = window->id;
717 client_message.type = wm->atom.wm_protocols;
718 client_message.data.data32[0] = wm->atom.wm_take_focus;
719 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
720
721 xcb_send_event(wm->conn, 0, window->id,
722 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
723 (char *) &client_message);
724
725 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
726 window->id, XCB_TIME_CURRENT_TIME);
727 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400728 xcb_set_input_focus (wm->conn,
729 XCB_INPUT_FOCUS_POINTER_ROOT,
730 XCB_NONE,
731 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600732 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400733
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500734 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800735 if (wm->focus_window->frame)
736 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400737 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500738 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400739 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500740 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800741 if (wm->focus_window->frame)
742 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400743 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500744 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400745}
746
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300747static void
748weston_wm_window_transform(struct wl_listener *listener, void *data)
749{
750 struct weston_surface *surface = data;
751 struct weston_wm_window *window = get_wm_window(surface);
752 struct weston_wm *wm =
753 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300754 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300755
756 if (!window || !wm)
757 return;
758
Jason Ekstranda7af7042013-10-12 22:38:11 -0500759 if (!window->view || !weston_view_is_mapped(window->view))
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300760 return;
761
Jason Ekstranda7af7042013-10-12 22:38:11 -0500762 if (window->x != window->view->geometry.x ||
763 window->y != window->view->geometry.y) {
764 values[0] = window->view->geometry.x;
765 values[1] = window->view->geometry.y;
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700766 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300767
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700768 xcb_configure_window(wm->conn, window->frame_id, mask, values);
769 xcb_flush(wm->conn);
770 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300771}
772
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400773#define ICCCM_WITHDRAWN_STATE 0
774#define ICCCM_NORMAL_STATE 1
775#define ICCCM_ICONIC_STATE 3
776
777static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500778weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400779{
780 struct weston_wm *wm = window->wm;
781 uint32_t property[2];
782
783 property[0] = state;
784 property[1] = XCB_WINDOW_NONE;
785
786 xcb_change_property(wm->conn,
787 XCB_PROP_MODE_REPLACE,
788 window->id,
789 wm->atom.wm_state,
790 wm->atom.wm_state,
791 32, /* format */
792 2, property);
793}
794
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400795static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500796weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
797{
798 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200799 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500800 int i;
801
802 i = 0;
803 if (window->fullscreen)
804 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200805 if (window->maximized_vert)
806 property[i++] = wm->atom.net_wm_state_maximized_vert;
807 if (window->maximized_horz)
808 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500809
810 xcb_change_property(wm->conn,
811 XCB_PROP_MODE_REPLACE,
812 window->id,
813 wm->atom.net_wm_state,
814 XCB_ATOM_ATOM,
815 32, /* format */
816 i, property);
817}
818
819static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700820weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400821{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700822 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400823 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400824 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200825 int buttons = FRAME_BUTTON_CLOSE;
826
827 if (window->decorate & MWM_DECOR_MAXIMIZE)
828 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400829
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500830 window->frame = frame_create(window->wm->theme,
831 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200832 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500833 frame_resize_inside(window->frame, window->width, window->height);
834
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400835 weston_wm_window_get_frame_size(window, &width, &height);
836 weston_wm_window_get_child_position(window, &x, &y);
837
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400838 values[0] = wm->screen->black_pixel;
839 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400840 XCB_EVENT_MASK_KEY_PRESS |
841 XCB_EVENT_MASK_KEY_RELEASE |
842 XCB_EVENT_MASK_BUTTON_PRESS |
843 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400844 XCB_EVENT_MASK_POINTER_MOTION |
845 XCB_EVENT_MASK_ENTER_WINDOW |
846 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400847 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400848 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400849 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400850
851 window->frame_id = xcb_generate_id(wm->conn);
852 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400853 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400854 window->frame_id,
855 wm->screen->root,
856 0, 0,
857 width, height,
858 0,
859 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400860 wm->visual_id,
861 XCB_CW_BORDER_PIXEL |
862 XCB_CW_EVENT_MASK |
863 XCB_CW_COLORMAP, values);
864
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400865 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
866
867 values[0] = 0;
868 xcb_configure_window(wm->conn, window->id,
869 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
870
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400871 window->cairo_surface =
872 cairo_xcb_surface_create_with_xrender_format(wm->conn,
873 wm->screen,
874 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400875 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400876 width, height);
877
878 hash_table_insert(wm->window_hash, window->frame_id, window);
879}
880
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200881/*
882 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
883 * Passing a <0 desktop value deletes the property.
884 */
885static void
886weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
887 int desktop)
888{
889 if (desktop >= 0) {
890 xcb_change_property(window->wm->conn,
891 XCB_PROP_MODE_REPLACE,
892 window->id,
893 window->wm->atom.net_wm_desktop,
894 XCB_ATOM_CARDINAL,
895 32, /* format */
896 1, &desktop);
897 } else {
898 xcb_delete_property(window->wm->conn,
899 window->id,
900 window->wm->atom.net_wm_desktop);
901 }
902}
903
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400904static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700905weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
906{
907 xcb_map_request_event_t *map_request =
908 (xcb_map_request_event_t *) event;
909 struct weston_wm_window *window;
910
911 if (our_resource(wm, map_request->window)) {
912 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
913 map_request->window);
914 return;
915 }
916
917 window = hash_table_lookup(wm->window_hash, map_request->window);
918
919 weston_wm_window_read_properties(window);
920
921 if (window->frame_id == XCB_WINDOW_NONE)
922 weston_wm_window_create_frame(window);
923
924 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
925 window->id, window, window->frame_id);
926
927 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
928 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200929 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700930
931 xcb_map_window(wm->conn, map_request->window);
932 xcb_map_window(wm->conn, window->frame_id);
933}
934
935static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400936weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
937{
938 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
939
940 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400941 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
942 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400943 return;
944 }
945
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400946 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400947}
948
949static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400950weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
951{
952 xcb_unmap_notify_event_t *unmap_notify =
953 (xcb_unmap_notify_event_t *) event;
954 struct weston_wm_window *window;
955
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400956 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
957 unmap_notify->window,
958 unmap_notify->event,
959 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400960
961 if (our_resource(wm, unmap_notify->window))
962 return;
963
MoD31700122013-06-11 19:58:55 -0500964 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400965 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
966 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400967 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400968
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400969 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400970 if (wm->focus_window == window)
971 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400972 if (window->surface)
973 wl_list_remove(&window->surface_destroy_listener.link);
974 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +0200975 window->shsurf = NULL;
Dima Ryazanove5a32082013-11-15 02:01:18 -0800976 window->view = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200977
978 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
979 weston_wm_window_set_virtual_desktop(window, -1);
980
Kristian Høgsbergab6d6672013-09-03 16:38:51 -0700981 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400982}
983
984static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400985weston_wm_window_draw_decoration(void *data)
986{
987 struct weston_wm_window *window = data;
988 struct weston_wm *wm = window->wm;
989 struct theme *t = wm->theme;
990 cairo_t *cr;
991 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500992 int32_t input_x, input_y, input_w, input_h;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -0700993 struct weston_shell_interface *shell_interface =
994 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400995 uint32_t flags = 0;
996
997 weston_wm_window_read_properties(window);
998
999 window->repaint_source = NULL;
1000
1001 weston_wm_window_get_frame_size(window, &width, &height);
1002 weston_wm_window_get_child_position(window, &x, &y);
1003
1004 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1005 cr = cairo_create(window->cairo_surface);
1006
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001007 if (window->fullscreen) {
1008 /* nothing */
1009 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001010 if (wm->focus_window == window)
1011 flags |= THEME_FRAME_ACTIVE;
1012
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001013 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001014 } else {
1015 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1016 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1017 cairo_paint(cr);
1018
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001019 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001020 }
1021
1022 cairo_destroy(cr);
1023
1024 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001025 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -05001026 if(window->has_alpha) {
1027 pixman_region32_init(&window->surface->pending.opaque);
1028 } else {
1029 /* We leave an extra pixel around the X window area to
1030 * make sure we don't sample from the undefined alpha
1031 * channel when filtering. */
1032 pixman_region32_init_rect(&window->surface->pending.opaque,
1033 x - 1, y - 1,
1034 window->width + 2,
1035 window->height + 2);
1036 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037 if (window->view)
1038 weston_view_geometry_dirty(window->view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001039
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001040 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001041
1042 if (window->fullscreen) {
1043 input_x = 0;
1044 input_y = 0;
1045 input_w = window->width;
1046 input_h = window->height;
1047 } else if (window->decorate) {
1048 frame_input_rect(window->frame, &input_x, &input_y,
1049 &input_w, &input_h);
1050 }
1051
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001052 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001053 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001054
1055 shell_interface->set_window_geometry(window->shsurf,
1056 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001057 }
1058}
1059
1060static void
1061weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1062{
1063 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001064 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001065
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001066 if (window->frame_id == XCB_WINDOW_NONE) {
1067 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001068 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001069 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -05001070 if(window->has_alpha) {
1071 pixman_region32_init(&window->surface->pending.opaque);
1072 } else {
1073 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1074 width, height);
1075 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001076 if (window->view)
1077 weston_view_geometry_dirty(window->view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001078 }
1079 return;
1080 }
1081
1082 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001083 return;
1084
1085 window->repaint_source =
1086 wl_event_loop_add_idle(wm->server->loop,
1087 weston_wm_window_draw_decoration,
1088 window);
1089}
1090
1091static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001092weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1093{
1094 xcb_property_notify_event_t *property_notify =
1095 (xcb_property_notify_event_t *) event;
1096 struct weston_wm_window *window;
1097
1098 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001099 if (!window)
1100 return;
1101
1102 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001103
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001104 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001105 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001106 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001107 else
1108 read_and_dump_property(wm, property_notify->window,
1109 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001110
1111 if (property_notify->atom == wm->atom.net_wm_name ||
1112 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001113 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001114}
1115
1116static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001117weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001118 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001119{
1120 struct weston_wm_window *window;
1121 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001122 xcb_get_geometry_cookie_t geometry_cookie;
1123 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001124
Peter Huttererf3d62272013-08-08 11:57:05 +10001125 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001126 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001127 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001128 return;
1129 }
1130
MoD384a11a2013-06-22 11:04:21 -05001131 geometry_cookie = xcb_get_geometry(wm->conn, id);
1132
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001133 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1134 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1135
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001136 window->wm = wm;
1137 window->id = id;
1138 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001139 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001140 window->width = width;
1141 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001142 window->x = x;
1143 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001144
MoD384a11a2013-06-22 11:04:21 -05001145 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1146 /* technically we should use XRender and check the visual format's
1147 alpha_mask, but checking depth is simpler and works in all known cases */
1148 if(geometry_reply != NULL)
1149 window->has_alpha = geometry_reply->depth == 32;
1150 free(geometry_reply);
1151
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001152 hash_table_insert(wm->window_hash, id, window);
1153}
1154
1155static void
1156weston_wm_window_destroy(struct weston_wm_window *window)
1157{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001158 struct weston_wm *wm = window->wm;
1159
1160 if (window->repaint_source)
1161 wl_event_source_remove(window->repaint_source);
1162 if (window->cairo_surface)
1163 cairo_surface_destroy(window->cairo_surface);
1164
1165 if (window->frame_id) {
1166 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1167 xcb_destroy_window(wm->conn, window->frame_id);
1168 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001169 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001170 hash_table_remove(wm->window_hash, window->frame_id);
1171 window->frame_id = XCB_WINDOW_NONE;
1172 }
1173
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001174 if (window->surface_id)
1175 wl_list_remove(&window->link);
1176
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001177 hash_table_remove(window->wm->window_hash, window->id);
1178 free(window);
1179}
1180
1181static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001182weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1183{
1184 xcb_create_notify_event_t *create_notify =
1185 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001186
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001187 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1188 create_notify->window,
1189 create_notify->width, create_notify->height,
1190 create_notify->override_redirect ? ", override" : "",
1191 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001192
1193 if (our_resource(wm, create_notify->window))
1194 return;
1195
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001196 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001197 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001198 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001199 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001200}
1201
1202static void
1203weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1204{
1205 xcb_destroy_notify_event_t *destroy_notify =
1206 (xcb_destroy_notify_event_t *) event;
1207 struct weston_wm_window *window;
1208
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001209 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1210 destroy_notify->window,
1211 destroy_notify->event,
1212 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001213
1214 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001215 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001216
1217 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001218 weston_wm_window_destroy(window);
1219}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001220
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001221static void
1222weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1223{
1224 xcb_reparent_notify_event_t *reparent_notify =
1225 (xcb_reparent_notify_event_t *) event;
1226 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001227
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001228 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1229 reparent_notify->window,
1230 reparent_notify->parent,
1231 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001232
1233 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001234 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001235 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001236 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001237 } else if (!our_resource(wm, reparent_notify->parent)) {
1238 window = hash_table_lookup(wm->window_hash,
1239 reparent_notify->window);
1240 weston_wm_window_destroy(window);
1241 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001242}
1243
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001244struct weston_seat *
1245weston_wm_pick_seat(struct weston_wm *wm)
1246{
1247 return container_of(wm->server->compositor->seat_list.next,
1248 struct weston_seat, link);
1249}
1250
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001251static struct weston_seat *
1252weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1253{
1254 struct weston_wm *wm = window->wm;
1255 struct weston_seat *seat, *s;
1256
1257 seat = NULL;
1258 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Giulio Camuffoad7305a2014-10-04 13:58:33 +03001259 if (s->pointer != NULL && s->pointer->focus &&
1260 s->pointer->focus->surface == window->surface &&
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001261 s->pointer->button_count > 0 &&
1262 (seat == NULL ||
1263 s->pointer->grab_serial -
1264 seat->pointer->grab_serial < (1 << 30)))
1265 seat = s;
1266 }
1267
1268 return seat;
1269}
1270
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001271static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001272weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1273 xcb_client_message_event_t *client_message)
1274{
1275 static const int map[] = {
1276 THEME_LOCATION_RESIZING_TOP_LEFT,
1277 THEME_LOCATION_RESIZING_TOP,
1278 THEME_LOCATION_RESIZING_TOP_RIGHT,
1279 THEME_LOCATION_RESIZING_RIGHT,
1280 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1281 THEME_LOCATION_RESIZING_BOTTOM,
1282 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1283 THEME_LOCATION_RESIZING_LEFT
1284 };
1285
1286 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001287 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001288 int detail;
1289 struct weston_shell_interface *shell_interface =
1290 &wm->server->compositor->shell_interface;
1291
Boyan Dingc06a1802014-07-06 11:44:58 +08001292 if (seat == NULL || seat->pointer->button_count != 1
Giulio Camuffoad7305a2014-10-04 13:58:33 +03001293 || !seat->pointer->focus
1294 || seat->pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001295 return;
1296
1297 detail = client_message->data.data32[2];
1298 switch (detail) {
1299 case _NET_WM_MOVERESIZE_MOVE:
1300 shell_interface->move(window->shsurf, seat);
1301 break;
1302 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1303 case _NET_WM_MOVERESIZE_SIZE_TOP:
1304 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1305 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1306 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1307 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1308 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1309 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1310 shell_interface->resize(window->shsurf, seat, map[detail]);
1311 break;
1312 case _NET_WM_MOVERESIZE_CANCEL:
1313 break;
1314 }
1315}
1316
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001317#define _NET_WM_STATE_REMOVE 0
1318#define _NET_WM_STATE_ADD 1
1319#define _NET_WM_STATE_TOGGLE 2
1320
1321static int
1322update_state(int action, int *state)
1323{
1324 int new_state, changed;
1325
1326 switch (action) {
1327 case _NET_WM_STATE_REMOVE:
1328 new_state = 0;
1329 break;
1330 case _NET_WM_STATE_ADD:
1331 new_state = 1;
1332 break;
1333 case _NET_WM_STATE_TOGGLE:
1334 new_state = !*state;
1335 break;
1336 default:
1337 return 0;
1338 }
1339
1340 changed = (*state != new_state);
1341 *state = new_state;
1342
1343 return changed;
1344}
1345
1346static void
1347weston_wm_window_configure(void *data);
1348
1349static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001350weston_wm_window_set_toplevel(struct weston_wm_window *window)
1351{
1352 struct weston_shell_interface *shell_interface =
1353 &window->wm->server->compositor->shell_interface;
1354
1355 shell_interface->set_toplevel(window->shsurf);
1356 window->width = window->saved_width;
1357 window->height = window->saved_height;
1358 if (window->frame)
1359 frame_resize_inside(window->frame,
1360 window->width,
1361 window->height);
1362 weston_wm_window_configure(window);
1363}
1364
1365static inline bool
1366weston_wm_window_is_maximized(struct weston_wm_window *window)
1367{
1368 return window->maximized_horz && window->maximized_vert;
1369}
1370
1371static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001372weston_wm_window_handle_state(struct weston_wm_window *window,
1373 xcb_client_message_event_t *client_message)
1374{
1375 struct weston_wm *wm = window->wm;
1376 struct weston_shell_interface *shell_interface =
1377 &wm->server->compositor->shell_interface;
1378 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001379 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001380
1381 action = client_message->data.data32[0];
1382 property = client_message->data.data32[1];
1383
1384 if (property == wm->atom.net_wm_state_fullscreen &&
1385 update_state(action, &window->fullscreen)) {
1386 weston_wm_window_set_net_wm_state(window);
1387 if (window->fullscreen) {
1388 window->saved_width = window->width;
1389 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001390
1391 if (window->shsurf)
1392 shell_interface->set_fullscreen(window->shsurf,
1393 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1394 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001395 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001396 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001397 weston_wm_window_set_toplevel(window);
1398 }
1399 } else {
1400 if (property == wm->atom.net_wm_state_maximized_vert &&
1401 update_state(action, &window->maximized_vert))
1402 weston_wm_window_set_net_wm_state(window);
1403 if (property == wm->atom.net_wm_state_maximized_horz &&
1404 update_state(action, &window->maximized_horz))
1405 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001406
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001407 if (maximized != weston_wm_window_is_maximized(window)) {
1408 if (weston_wm_window_is_maximized(window)) {
1409 window->saved_width = window->width;
1410 window->saved_height = window->height;
1411
1412 if (window->shsurf)
1413 shell_interface->set_maximized(window->shsurf);
1414 } else if (window->shsurf) {
1415 weston_wm_window_set_toplevel(window);
1416 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001417 }
1418 }
1419}
1420
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001421static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001422surface_destroy(struct wl_listener *listener, void *data)
1423{
1424 struct weston_wm_window *window =
1425 container_of(listener,
1426 struct weston_wm_window, surface_destroy_listener);
1427
1428 wm_log("surface for xid %d destroyed\n", window->id);
1429
1430 /* This should have been freed by the shell.
1431 * Don't try to use it later. */
1432 window->shsurf = NULL;
1433 window->surface = NULL;
1434 window->view = NULL;
1435}
1436
1437static void
1438weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1439 xcb_client_message_event_t *client_message)
1440{
1441 struct weston_wm *wm = window->wm;
1442 struct wl_resource *resource;
1443
1444 if (window->surface_id != 0) {
1445 wm_log("already have surface id for window %d\n", window->id);
1446 return;
1447 }
1448
1449 /* Xwayland will send the wayland requests to create the
1450 * wl_surface before sending this client message. Even so, we
1451 * can end up handling the X event before the wayland requests
1452 * and thus when we try to look up the surface ID, the surface
1453 * hasn't been created yet. In that case put the window on
1454 * the unpaired window list and continue when the surface gets
1455 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001456 uint32_t id = client_message->data.data32[0];
1457 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001458 if (resource) {
1459 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001460 xserver_map_shell_surface(window,
1461 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001462 }
1463 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001464 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001465 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001466 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001467}
1468
1469static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001470weston_wm_handle_client_message(struct weston_wm *wm,
1471 xcb_generic_event_t *event)
1472{
1473 xcb_client_message_event_t *client_message =
1474 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001475 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001476
1477 window = hash_table_lookup(wm->window_hash, client_message->window);
1478
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001479 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1480 get_atom_name(wm->conn, client_message->type),
1481 client_message->data.data32[0],
1482 client_message->data.data32[1],
1483 client_message->data.data32[2],
1484 client_message->data.data32[3],
1485 client_message->data.data32[4],
1486 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001487
Jason Ekstrand0250a742014-07-24 13:17:47 -07001488 /* The window may get created and destroyed before we actually
1489 * handle the message. If it doesn't exist, bail.
1490 */
1491 if (!window)
1492 return;
1493
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001494 if (client_message->type == wm->atom.net_wm_moveresize)
1495 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001496 else if (client_message->type == wm->atom.net_wm_state)
1497 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001498 else if (client_message->type == wm->atom.wl_surface_id)
1499 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001500}
1501
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001502enum cursor_type {
1503 XWM_CURSOR_TOP,
1504 XWM_CURSOR_BOTTOM,
1505 XWM_CURSOR_LEFT,
1506 XWM_CURSOR_RIGHT,
1507 XWM_CURSOR_TOP_LEFT,
1508 XWM_CURSOR_TOP_RIGHT,
1509 XWM_CURSOR_BOTTOM_LEFT,
1510 XWM_CURSOR_BOTTOM_RIGHT,
1511 XWM_CURSOR_LEFT_PTR,
1512};
1513
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001514/*
1515 * The following correspondences between file names and cursors was copied
1516 * from: https://bugs.kde.org/attachment.cgi?id=67313
1517 */
1518
1519static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001520 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001521 "sw-resize",
1522 "size_bdiag"
1523};
1524
1525static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001526 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001527 "se-resize",
1528 "size_fdiag"
1529};
1530
1531static const char *bottom_sides[] = {
1532 "bottom_side",
1533 "s-resize",
1534 "size_ver"
1535};
1536
1537static const char *left_ptrs[] = {
1538 "left_ptr",
1539 "default",
1540 "top_left_arrow",
1541 "left-arrow"
1542};
1543
1544static const char *left_sides[] = {
1545 "left_side",
1546 "w-resize",
1547 "size_hor"
1548};
1549
1550static const char *right_sides[] = {
1551 "right_side",
1552 "e-resize",
1553 "size_hor"
1554};
1555
1556static const char *top_left_corners[] = {
1557 "top_left_corner",
1558 "nw-resize",
1559 "size_fdiag"
1560};
1561
1562static const char *top_right_corners[] = {
1563 "top_right_corner",
1564 "ne-resize",
1565 "size_bdiag"
1566};
1567
1568static const char *top_sides[] = {
1569 "top_side",
1570 "n-resize",
1571 "size_ver"
1572};
1573
1574struct cursor_alternatives {
1575 const char **names;
1576 size_t count;
1577};
1578
1579static const struct cursor_alternatives cursors[] = {
1580 {top_sides, ARRAY_LENGTH(top_sides)},
1581 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1582 {left_sides, ARRAY_LENGTH(left_sides)},
1583 {right_sides, ARRAY_LENGTH(right_sides)},
1584 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1585 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1586 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1587 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1588 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001589};
1590
1591static void
1592weston_wm_create_cursors(struct weston_wm *wm)
1593{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001594 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001595 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001596 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001597
1598 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1599 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001600 for (j = 0; j < cursors[i].count; j++) {
1601 name = cursors[i].names[j];
1602 wm->cursors[i] =
1603 xcb_cursor_library_load_cursor(wm, name);
1604 if (wm->cursors[i] != (xcb_cursor_t)-1)
1605 break;
1606 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001607 }
1608
1609 wm->last_cursor = -1;
1610}
1611
1612static void
1613weston_wm_destroy_cursors(struct weston_wm *wm)
1614{
1615 uint8_t i;
1616
1617 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1618 xcb_free_cursor(wm->conn, wm->cursors[i]);
1619
1620 free(wm->cursors);
1621}
1622
1623static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001624get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001625{
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001626 // int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001627
1628 switch (location) {
1629 case THEME_LOCATION_RESIZING_TOP:
1630 return XWM_CURSOR_TOP;
1631 case THEME_LOCATION_RESIZING_BOTTOM:
1632 return XWM_CURSOR_BOTTOM;
1633 case THEME_LOCATION_RESIZING_LEFT:
1634 return XWM_CURSOR_LEFT;
1635 case THEME_LOCATION_RESIZING_RIGHT:
1636 return XWM_CURSOR_RIGHT;
1637 case THEME_LOCATION_RESIZING_TOP_LEFT:
1638 return XWM_CURSOR_TOP_LEFT;
1639 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1640 return XWM_CURSOR_TOP_RIGHT;
1641 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1642 return XWM_CURSOR_BOTTOM_LEFT;
1643 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1644 return XWM_CURSOR_BOTTOM_RIGHT;
1645 case THEME_LOCATION_EXTERIOR:
1646 case THEME_LOCATION_TITLEBAR:
1647 default:
1648 return XWM_CURSOR_LEFT_PTR;
1649 }
1650}
1651
1652static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001653weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1654 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001655{
1656 uint32_t cursor_value_list;
1657
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001658 if (wm->last_cursor == cursor)
1659 return;
1660
1661 wm->last_cursor = cursor;
1662
1663 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001664 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001665 XCB_CW_CURSOR, &cursor_value_list);
1666 xcb_flush(wm->conn);
1667}
1668
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001669static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001670weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001671{
1672 xcb_client_message_event_t client_message;
1673
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001674 if (window->delete_window) {
1675 client_message.response_type = XCB_CLIENT_MESSAGE;
1676 client_message.format = 32;
1677 client_message.window = window->id;
1678 client_message.type = window->wm->atom.wm_protocols;
1679 client_message.data.data32[0] =
1680 window->wm->atom.wm_delete_window;
1681 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001682
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001683 xcb_send_event(window->wm->conn, 0, window->id,
1684 XCB_EVENT_MASK_NO_EVENT,
1685 (char *) &client_message);
1686 } else {
1687 xcb_kill_client(window->wm->conn, window->id);
1688 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001689}
1690
1691static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001692weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1693{
1694 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1695 struct weston_shell_interface *shell_interface =
1696 &wm->server->compositor->shell_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001697 struct weston_seat *seat;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001698 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001699 enum theme_location location;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001700 enum frame_button_state button_state;
1701 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001702
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001703 wm_log("XCB_BUTTON_%s (detail %d)\n",
1704 button->response_type == XCB_BUTTON_PRESS ?
1705 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001706
1707 window = hash_table_lookup(wm->window_hash, button->event);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001708 if (!window || !window->decorate)
1709 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001710
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001711 if (button->detail != 1 && button->detail != 2)
1712 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001713
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001714 seat = weston_wm_pick_seat_for_window(window);
1715
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001716 button_state = button->response_type == XCB_BUTTON_PRESS ?
1717 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1718 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1719
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001720 /* Make sure we're looking at the right location. The frame
1721 * could have received a motion event from a pointer from a
1722 * different wl_seat, but under X it looks like our core
1723 * pointer moved. Move the frame pointer to the button press
1724 * location before deciding what to do. */
1725 location = frame_pointer_motion(window->frame, NULL,
1726 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001727 location = frame_pointer_button(window->frame, NULL,
1728 button_id, button_state);
1729 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1730 weston_wm_window_schedule_repaint(window);
1731
1732 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Boyan Ding3c6d20c2014-09-03 17:25:30 +08001733 if (seat != NULL)
1734 shell_interface->move(window->shsurf, seat);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001735 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1736 }
1737
1738 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Boyan Ding3c6d20c2014-09-03 17:25:30 +08001739 if (seat != NULL)
1740 shell_interface->resize(window->shsurf, seat, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001741 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1742 }
1743
1744 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001745 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001746 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001747 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001748
1749 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1750 window->maximized_horz = !window->maximized_horz;
1751 window->maximized_vert = !window->maximized_vert;
1752 if (weston_wm_window_is_maximized(window)) {
1753 window->saved_width = window->width;
1754 window->saved_height = window->height;
1755 shell_interface->set_maximized(window->shsurf);
1756 } else {
1757 weston_wm_window_set_toplevel(window);
1758 }
1759 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1760 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001761}
1762
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001763static void
1764weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1765{
1766 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1767 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001768 enum theme_location location;
1769 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001770
1771 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001772 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001773 return;
1774
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001775 location = frame_pointer_motion(window->frame, NULL,
1776 motion->event_x, motion->event_y);
1777 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1778 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001779
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001780 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001781 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001782}
1783
1784static void
1785weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1786{
1787 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1788 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001789 enum theme_location location;
1790 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001791
1792 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001793 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001794 return;
1795
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001796 location = frame_pointer_enter(window->frame, NULL,
1797 enter->event_x, enter->event_y);
1798 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1799 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001800
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001801 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001802 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001803}
1804
1805static void
1806weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1807{
1808 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1809 struct weston_wm_window *window;
1810
1811 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001812 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001813 return;
1814
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001815 frame_pointer_leave(window->frame, NULL);
1816 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1817 weston_wm_window_schedule_repaint(window);
1818
Tiago Vignattic1903232012-07-16 12:15:37 -04001819 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001820}
1821
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001822static int
1823weston_wm_handle_event(int fd, uint32_t mask, void *data)
1824{
1825 struct weston_wm *wm = data;
1826 xcb_generic_event_t *event;
1827 int count = 0;
1828
1829 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1830 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001831 free(event);
1832 count++;
1833 continue;
1834 }
1835
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001836 if (weston_wm_handle_dnd_event(wm, event)) {
1837 free(event);
1838 count++;
1839 continue;
1840 }
1841
MoD31700122013-06-11 19:58:55 -05001842 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001843 case XCB_BUTTON_PRESS:
1844 case XCB_BUTTON_RELEASE:
1845 weston_wm_handle_button(wm, event);
1846 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001847 case XCB_ENTER_NOTIFY:
1848 weston_wm_handle_enter(wm, event);
1849 break;
1850 case XCB_LEAVE_NOTIFY:
1851 weston_wm_handle_leave(wm, event);
1852 break;
1853 case XCB_MOTION_NOTIFY:
1854 weston_wm_handle_motion(wm, event);
1855 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001856 case XCB_CREATE_NOTIFY:
1857 weston_wm_handle_create_notify(wm, event);
1858 break;
1859 case XCB_MAP_REQUEST:
1860 weston_wm_handle_map_request(wm, event);
1861 break;
1862 case XCB_MAP_NOTIFY:
1863 weston_wm_handle_map_notify(wm, event);
1864 break;
1865 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001866 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001867 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001868 case XCB_REPARENT_NOTIFY:
1869 weston_wm_handle_reparent_notify(wm, event);
1870 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001871 case XCB_CONFIGURE_REQUEST:
1872 weston_wm_handle_configure_request(wm, event);
1873 break;
1874 case XCB_CONFIGURE_NOTIFY:
1875 weston_wm_handle_configure_notify(wm, event);
1876 break;
1877 case XCB_DESTROY_NOTIFY:
1878 weston_wm_handle_destroy_notify(wm, event);
1879 break;
1880 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001881 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001882 break;
1883 case XCB_PROPERTY_NOTIFY:
1884 weston_wm_handle_property_notify(wm, event);
1885 break;
1886 case XCB_CLIENT_MESSAGE:
1887 weston_wm_handle_client_message(wm, event);
1888 break;
1889 }
1890
1891 free(event);
1892 count++;
1893 }
1894
1895 xcb_flush(wm->conn);
1896
1897 return count;
1898}
1899
1900static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001901weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1902{
1903 xcb_depth_iterator_t d_iter;
1904 xcb_visualtype_iterator_t vt_iter;
1905 xcb_visualtype_t *visualtype;
1906
1907 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1908 visualtype = NULL;
1909 while (d_iter.rem > 0) {
1910 if (d_iter.data->depth == 32) {
1911 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1912 visualtype = vt_iter.data;
1913 break;
1914 }
1915
1916 xcb_depth_next(&d_iter);
1917 }
1918
1919 if (visualtype == NULL) {
1920 weston_log("no 32 bit visualtype\n");
1921 return;
1922 }
1923
1924 wm->visual_id = visualtype->visual_id;
1925 wm->colormap = xcb_generate_id(wm->conn);
1926 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1927 wm->colormap, wm->screen->root, wm->visual_id);
1928}
1929
1930static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001931weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001932{
1933
1934#define F(field) offsetof(struct weston_wm, field)
1935
1936 static const struct { const char *name; int offset; } atoms[] = {
1937 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001938 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001939 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1940 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001941 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001942 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001943 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001944 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001945 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001946 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001947 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1948 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001949 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
1950 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001951 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1952 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1953 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001954 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001955 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1956
1957 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1958 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1959 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1960 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1961 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1962 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1963 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001964 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1965 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001966 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1967 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1968 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1969 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1970 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1971
1972 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1973 { "_NET_SUPPORTING_WM_CHECK",
1974 F(atom.net_supporting_wm_check) },
1975 { "_NET_SUPPORTED", F(atom.net_supported) },
1976 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1977 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001978 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001979 { "TARGETS", F(atom.targets) },
1980 { "UTF8_STRING", F(atom.utf8_string) },
1981 { "_WL_SELECTION", F(atom.wl_selection) },
1982 { "INCR", F(atom.incr) },
1983 { "TIMESTAMP", F(atom.timestamp) },
1984 { "MULTIPLE", F(atom.multiple) },
1985 { "UTF8_STRING" , F(atom.utf8_string) },
1986 { "COMPOUND_TEXT", F(atom.compound_text) },
1987 { "TEXT", F(atom.text) },
1988 { "STRING", F(atom.string) },
1989 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1990 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001991 { "XdndSelection", F(atom.xdnd_selection) },
1992 { "XdndAware", F(atom.xdnd_aware) },
1993 { "XdndEnter", F(atom.xdnd_enter) },
1994 { "XdndLeave", F(atom.xdnd_leave) },
1995 { "XdndDrop", F(atom.xdnd_drop) },
1996 { "XdndStatus", F(atom.xdnd_status) },
1997 { "XdndFinished", F(atom.xdnd_finished) },
1998 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001999 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2000 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002001 };
2002#undef F
2003
2004 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2005 xcb_xfixes_query_version_reply_t *xfixes_reply;
2006 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2007 xcb_intern_atom_reply_t *reply;
2008 xcb_render_query_pict_formats_reply_t *formats_reply;
2009 xcb_render_query_pict_formats_cookie_t formats_cookie;
2010 xcb_render_pictforminfo_t *formats;
2011 uint32_t i;
2012
2013 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002014 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002015
2016 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2017
2018 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2019 cookies[i] = xcb_intern_atom (wm->conn, 0,
2020 strlen(atoms[i].name),
2021 atoms[i].name);
2022
2023 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2024 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2025 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2026 free(reply);
2027 }
2028
2029 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2030 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002031 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002032
2033 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2034 XCB_XFIXES_MAJOR_VERSION,
2035 XCB_XFIXES_MINOR_VERSION);
2036 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2037 xfixes_cookie, NULL);
2038
Martin Minarik6d118362012-06-07 18:01:59 +02002039 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002040 xfixes_reply->major_version, xfixes_reply->minor_version);
2041
2042 free(xfixes_reply);
2043
2044 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2045 formats_cookie, 0);
2046 if (formats_reply == NULL)
2047 return;
2048
2049 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002050 for (i = 0; i < formats_reply->num_formats; i++) {
2051 if (formats[i].direct.red_mask != 0xff &&
2052 formats[i].direct.red_shift != 16)
2053 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002054 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2055 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002056 wm->format_rgb = formats[i];
2057 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2058 formats[i].depth == 32 &&
2059 formats[i].direct.alpha_mask == 0xff &&
2060 formats[i].direct.alpha_shift == 24)
2061 wm->format_rgba = formats[i];
2062 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002063
2064 free(formats_reply);
2065}
2066
2067static void
2068weston_wm_create_wm_window(struct weston_wm *wm)
2069{
2070 static const char name[] = "Weston WM";
2071
2072 wm->wm_window = xcb_generate_id(wm->conn);
2073 xcb_create_window(wm->conn,
2074 XCB_COPY_FROM_PARENT,
2075 wm->wm_window,
2076 wm->screen->root,
2077 0, 0,
2078 10, 10,
2079 0,
2080 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2081 wm->screen->root_visual,
2082 0, NULL);
2083
2084 xcb_change_property(wm->conn,
2085 XCB_PROP_MODE_REPLACE,
2086 wm->wm_window,
2087 wm->atom.net_supporting_wm_check,
2088 XCB_ATOM_WINDOW,
2089 32, /* format */
2090 1, &wm->wm_window);
2091
2092 xcb_change_property(wm->conn,
2093 XCB_PROP_MODE_REPLACE,
2094 wm->wm_window,
2095 wm->atom.net_wm_name,
2096 wm->atom.utf8_string,
2097 8, /* format */
2098 strlen(name), name);
2099
2100 xcb_change_property(wm->conn,
2101 XCB_PROP_MODE_REPLACE,
2102 wm->screen->root,
2103 wm->atom.net_supporting_wm_check,
2104 XCB_ATOM_WINDOW,
2105 32, /* format */
2106 1, &wm->wm_window);
2107
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002108 /* Claim the WM_S0 selection even though we don't suport
2109 * the --replace functionality. */
2110 xcb_set_selection_owner(wm->conn,
2111 wm->wm_window,
2112 wm->atom.wm_s0,
2113 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002114
2115 xcb_set_selection_owner(wm->conn,
2116 wm->wm_window,
2117 wm->atom.net_wm_cm_s0,
2118 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002119}
2120
2121struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002122weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002123{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002124 struct weston_wm *wm;
2125 struct wl_event_loop *loop;
2126 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002127 uint32_t values[1];
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002128 xcb_atom_t supported[5];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002129
Peter Huttererf3d62272013-08-08 11:57:05 +10002130 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002131 if (wm == NULL)
2132 return NULL;
2133
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002134 wm->server = wxs;
2135 wm->window_hash = hash_table_create();
2136 if (wm->window_hash == NULL) {
2137 free(wm);
2138 return NULL;
2139 }
2140
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002141 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002142 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002143 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002144 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002145 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002146 hash_table_destroy(wm->window_hash);
2147 free(wm);
2148 return NULL;
2149 }
2150
2151 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2152 wm->screen = s.data;
2153
2154 loop = wl_display_get_event_loop(wxs->wl_display);
2155 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002156 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002157 WL_EVENT_READABLE,
2158 weston_wm_handle_event, wm);
2159 wl_event_source_check(wm->source);
2160
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002161 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002162 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002163
2164 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002165 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2166 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2167 XCB_EVENT_MASK_PROPERTY_CHANGE;
2168 xcb_change_window_attributes(wm->conn, wm->screen->root,
2169 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002170
2171 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2172 XCB_COMPOSITE_REDIRECT_MANUAL);
2173
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002174 wm->theme = theme_create();
2175
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002176 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002177 supported[1] = wm->atom.net_wm_state;
2178 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002179 supported[3] = wm->atom.net_wm_state_maximized_vert;
2180 supported[4] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002181 xcb_change_property(wm->conn,
2182 XCB_PROP_MODE_REPLACE,
2183 wm->screen->root,
2184 wm->atom.net_supported,
2185 XCB_ATOM_ATOM,
2186 32, /* format */
2187 ARRAY_LENGTH(supported), supported);
2188
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002189 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002190
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002191 weston_wm_dnd_init(wm);
2192
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002193 xcb_flush(wm->conn);
2194
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002195 wm->create_surface_listener.notify = weston_wm_create_surface;
2196 wl_signal_add(&wxs->compositor->create_surface_signal,
2197 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002198 wm->activate_listener.notify = weston_wm_window_activate;
2199 wl_signal_add(&wxs->compositor->activate_signal,
2200 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002201 wm->transform_listener.notify = weston_wm_window_transform;
2202 wl_signal_add(&wxs->compositor->transform_signal,
2203 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002204 wm->kill_listener.notify = weston_wm_kill_client;
2205 wl_signal_add(&wxs->compositor->kill_signal,
2206 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002207 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002208
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002209 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002210 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002211
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002212 /* Create wm window and take WM_S0 selection last, which
2213 * signals to Xwayland that we're done with setup. */
2214 weston_wm_create_wm_window(wm);
2215
2216 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002217
2218 return wm;
2219}
2220
2221void
2222weston_wm_destroy(struct weston_wm *wm)
2223{
2224 /* FIXME: Free windows in hash. */
2225 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002226 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002227 xcb_disconnect(wm->conn);
2228 wl_event_source_remove(wm->source);
2229 wl_list_remove(&wm->selection_listener.link);
2230 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002231 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01002232 wl_list_remove(&wm->transform_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002233 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002234
2235 free(wm);
2236}
2237
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002238static struct weston_wm_window *
2239get_wm_window(struct weston_surface *surface)
2240{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002241 struct wl_listener *listener;
2242
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002243 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002244 if (listener)
2245 return container_of(listener, struct weston_wm_window,
2246 surface_destroy_listener);
2247
2248 return NULL;
2249}
2250
2251static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002252weston_wm_window_configure(void *data)
2253{
2254 struct weston_wm_window *window = data;
2255 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002256 uint32_t values[4];
2257 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002258
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002259 weston_wm_window_get_child_position(window, &x, &y);
2260 values[0] = x;
2261 values[1] = y;
2262 values[2] = window->width;
2263 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002264 xcb_configure_window(wm->conn,
2265 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002266 XCB_CONFIG_WINDOW_X |
2267 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002268 XCB_CONFIG_WINDOW_WIDTH |
2269 XCB_CONFIG_WINDOW_HEIGHT,
2270 values);
2271
2272 weston_wm_window_get_frame_size(window, &width, &height);
2273 values[0] = width;
2274 values[1] = height;
2275 xcb_configure_window(wm->conn,
2276 window->frame_id,
2277 XCB_CONFIG_WINDOW_WIDTH |
2278 XCB_CONFIG_WINDOW_HEIGHT,
2279 values);
2280
2281 window->configure_source = NULL;
2282
2283 weston_wm_window_schedule_repaint(window);
2284}
2285
2286static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002287send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002288{
2289 struct weston_wm_window *window = get_wm_window(surface);
2290 struct weston_wm *wm = window->wm;
2291 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002292 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002293
Marek Chalupae9fe4672014-10-29 13:44:44 +01002294 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002295 hborder = 2 * t->width;
2296 vborder = t->titlebar_height + t->width;
2297 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002298 hborder = 0;
2299 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002300 }
2301
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002302 if (width > hborder)
2303 window->width = width - hborder;
2304 else
2305 window->width = 1;
2306
2307 if (height > vborder)
2308 window->height = height - vborder;
2309 else
2310 window->height = 1;
2311
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002312 if (window->frame)
2313 frame_resize_inside(window->frame, window->width, window->height);
2314
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002315 if (window->configure_source)
2316 return;
2317
2318 window->configure_source =
2319 wl_event_loop_add_idle(wm->server->loop,
2320 weston_wm_window_configure, window);
2321}
2322
2323static const struct weston_shell_client shell_client = {
2324 send_configure
2325};
2326
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002327static int
2328legacy_fullscreen(struct weston_wm *wm,
2329 struct weston_wm_window *window,
2330 struct weston_output **output_ret)
2331{
2332 struct weston_compositor *compositor = wm->server->compositor;
2333 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002334 uint32_t minmax = PMinSize | PMaxSize;
2335 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002336
2337 /* Heuristics for detecting legacy fullscreen windows... */
2338
2339 wl_list_for_each(output, &compositor->output_list, link) {
2340 if (output->x == window->x &&
2341 output->y == window->y &&
2342 output->width == window->width &&
2343 output->height == window->height &&
2344 window->override_redirect) {
2345 *output_ret = output;
2346 return 1;
2347 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002348
2349 matching_size = 0;
2350 if ((window->size_hints.flags & (USSize |PSize)) &&
2351 window->size_hints.width == output->width &&
2352 window->size_hints.height == output->height)
2353 matching_size = 1;
2354 if ((window->size_hints.flags & minmax) == minmax &&
2355 window->size_hints.min_width == output->width &&
2356 window->size_hints.min_height == output->height &&
2357 window->size_hints.max_width == output->width &&
2358 window->size_hints.max_height == output->height)
2359 matching_size = 1;
2360
2361 if (matching_size && !window->decorate &&
2362 (window->size_hints.flags & (USPosition | PPosition)) &&
2363 window->size_hints.x == output->x &&
2364 window->size_hints.y == output->y) {
2365 *output_ret = output;
2366 return 1;
2367 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002368 }
2369
2370 return 0;
2371}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002372
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002373static bool
2374weston_wm_window_type_inactive(struct weston_wm_window *window)
2375{
2376 struct weston_wm *wm = window->wm;
2377
2378 return window->type == wm->atom.net_wm_window_type_tooltip ||
2379 window->type == wm->atom.net_wm_window_type_dropdown ||
2380 window->type == wm->atom.net_wm_window_type_dnd ||
2381 window->type == wm->atom.net_wm_window_type_combo ||
2382 window->type == wm->atom.net_wm_window_type_popup;
2383}
2384
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002385static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002386xserver_map_shell_surface(struct weston_wm_window *window,
2387 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002388{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002389 struct weston_wm *wm = window->wm;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002390 struct weston_shell_interface *shell_interface =
2391 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002392 struct weston_output *output;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002393 struct weston_wm_window *parent;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002394 int flags = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002395
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002396 weston_wm_window_read_properties(window);
2397
2398 /* A weston_wm_window may have many different surfaces assigned
2399 * throughout its life, so we must make sure to remove the listener
2400 * from the old surface signal list. */
2401 if (window->surface)
2402 wl_list_remove(&window->surface_destroy_listener.link);
2403
2404 window->surface = surface;
2405 window->surface_destroy_listener.notify = surface_destroy;
2406 wl_signal_add(&window->surface->destroy_signal,
2407 &window->surface_destroy_listener);
2408
2409 weston_wm_window_schedule_repaint(window);
2410
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002411 if (!shell_interface->create_shell_surface)
2412 return;
2413
Jason Ekstranda7af7042013-10-12 22:38:11 -05002414 if (!shell_interface->get_primary_view)
2415 return;
2416
Pekka Paalanen50b67472014-10-01 15:02:41 +03002417 if (window->surface->configure) {
2418 weston_log("warning, unexpected in %s: "
2419 "surface's configure hook is already set.\n",
2420 __func__);
2421 return;
2422 }
2423
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002424 window->shsurf =
2425 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002426 window->surface,
2427 &shell_client);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002428 window->view = shell_interface->get_primary_view(shell_interface->shell,
2429 window->shsurf);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002430
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002431 if (window->name)
2432 shell_interface->set_title(window->shsurf, window->name);
2433
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002434 if (window->fullscreen) {
2435 window->saved_width = window->width;
2436 window->saved_height = window->height;
2437 shell_interface->set_fullscreen(window->shsurf,
2438 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2439 0, NULL);
2440 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002441 } else if (legacy_fullscreen(wm, window, &output)) {
2442 window->fullscreen = 1;
2443 shell_interface->set_fullscreen(window->shsurf,
2444 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2445 0, output);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002446 } else if (window->override_redirect) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002447 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002448 window->x,
2449 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002450 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Axel Davye4450f92014-01-12 15:06:05 +01002451 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002452 parent = window->transient_for;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002453 if (weston_wm_window_type_inactive(window))
2454 flags = WL_SHELL_SURFACE_TRANSIENT_INACTIVE;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002455 shell_interface->set_transient(window->shsurf,
2456 parent->surface,
Axel Davyfa506b62014-01-12 15:06:04 +01002457 window->x - parent->x,
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002458 window->y - parent->y, flags);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002459 } else if (weston_wm_window_is_maximized(window)) {
2460 shell_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002461 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002462 if (weston_wm_window_type_inactive(window)) {
2463 shell_interface->set_xwayland(window->shsurf,
2464 window->x,
2465 window->y,
2466 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2467 } else {
2468 shell_interface->set_toplevel(window->shsurf);
2469 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002470 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002471}