blob: 1ea2c4c1e3482738696aa0bffe2a3f97fba94ea3 [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
Dima Ryazanovb0f5a252015-05-03 19:56:37 -070097#define MWM_DECOR_EVERYTHING \
98 (MWM_DECOR_BORDER | MWM_DECOR_RESIZEH | MWM_DECOR_TITLE | \
99 MWM_DECOR_MENU | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE)
100
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400101#define MWM_INPUT_MODELESS 0
102#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
103#define MWM_INPUT_SYSTEM_MODAL 2
104#define MWM_INPUT_FULL_APPLICATION_MODAL 3
105#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
106
107#define MWM_TEAROFF_WINDOW (1L<<0)
108
109#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
110#define _NET_WM_MOVERESIZE_SIZE_TOP 1
111#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
112#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
113#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
114#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
115#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
116#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
117#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
118#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
119#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
120#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
121
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400122struct weston_wm_window {
123 struct weston_wm *wm;
124 xcb_window_t id;
125 xcb_window_t frame_id;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500126 struct frame *frame;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400127 cairo_surface_t *cairo_surface;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700128 uint32_t surface_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400129 struct weston_surface *surface;
130 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500131 struct weston_view *view;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400132 struct wl_listener surface_destroy_listener;
133 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400134 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400135 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300136 int pid;
137 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400138 char *class;
139 char *name;
140 struct weston_wm_window *transient_for;
141 uint32_t protocols;
142 xcb_atom_t type;
143 int width, height;
144 int x, y;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500145 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400146 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300147 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500148 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500149 int has_alpha;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700150 int delete_window;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200151 int maximized_vert;
152 int maximized_horz;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700153 struct wm_size_hints size_hints;
154 struct motif_wm_hints motif_hints;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700155 struct wl_list link;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400156};
157
158static struct weston_wm_window *
159get_wm_window(struct weston_surface *surface);
160
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400161static void
162weston_wm_window_schedule_repaint(struct weston_wm_window *window);
163
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700164static void
165xserver_map_shell_surface(struct weston_wm_window *window,
166 struct weston_surface *surface);
167
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400168static int __attribute__ ((format (printf, 1, 2)))
169wm_log(const char *fmt, ...)
170{
171#ifdef WM_DEBUG
172 int l;
173 va_list argp;
174
175 va_start(argp, fmt);
176 l = weston_vlog(fmt, argp);
177 va_end(argp);
178
179 return l;
180#else
181 return 0;
182#endif
183}
184
185static int __attribute__ ((format (printf, 1, 2)))
186wm_log_continue(const char *fmt, ...)
187{
188#ifdef WM_DEBUG
189 int l;
190 va_list argp;
191
192 va_start(argp, fmt);
193 l = weston_vlog_continue(fmt, argp);
194 va_end(argp);
195
196 return l;
197#else
198 return 0;
199#endif
200}
201
Derek Foreman49372142015-04-09 10:51:22 -0500202static bool __attribute__ ((warn_unused_result))
203wm_lookup_window(struct weston_wm *wm, xcb_window_t hash,
204 struct weston_wm_window **window)
205{
206 *window = hash_table_lookup(wm->window_hash, hash);
207 if (*window)
208 return true;
209 return false;
210}
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400211
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400212const char *
213get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
214{
215 xcb_get_atom_name_cookie_t cookie;
216 xcb_get_atom_name_reply_t *reply;
217 xcb_generic_error_t *e;
218 static char buffer[64];
219
220 if (atom == XCB_ATOM_NONE)
221 return "None";
222
223 cookie = xcb_get_atom_name (c, atom);
224 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500225
226 if(reply) {
227 snprintf(buffer, sizeof buffer, "%.*s",
228 xcb_get_atom_name_name_length (reply),
229 xcb_get_atom_name_name (reply));
230 } else {
231 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
232 }
233
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400234 free(reply);
235
236 return buffer;
237}
238
Tiago Vignatti90fada42012-07-16 12:02:08 -0400239static xcb_cursor_t
240xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
241{
242 xcb_connection_t *c = wm->conn;
243 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
244 xcb_screen_t *screen = s.data;
245 xcb_gcontext_t gc;
246 xcb_pixmap_t pix;
247 xcb_render_picture_t pic;
248 xcb_cursor_t cursor;
249 int stride = img->width * 4;
250
251 pix = xcb_generate_id(c);
252 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
253
254 pic = xcb_generate_id(c);
255 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
256
257 gc = xcb_generate_id(c);
258 xcb_create_gc(c, gc, pix, 0, 0);
259
260 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
261 img->width, img->height, 0, 0, 0, 32,
262 stride * img->height, (uint8_t *) img->pixels);
263 xcb_free_gc(c, gc);
264
265 cursor = xcb_generate_id(c);
266 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
267
268 xcb_render_free_picture(c, pic);
269 xcb_free_pixmap(c, pix);
270
271 return cursor;
272}
273
274static xcb_cursor_t
275xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
276{
277 /* TODO: treat animated cursors as well */
278 if (images->nimage != 1)
279 return -1;
280
281 return xcb_cursor_image_load_cursor(wm, images->images[0]);
282}
283
284static xcb_cursor_t
285xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
286{
287 xcb_cursor_t cursor;
288 XcursorImages *images;
289 char *v = NULL;
290 int size = 0;
291
292 if (!file)
293 return 0;
294
295 v = getenv ("XCURSOR_SIZE");
296 if (v)
297 size = atoi(v);
298
299 if (!size)
300 size = 32;
301
302 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300303 if (!images)
304 return -1;
305
Tiago Vignatti90fada42012-07-16 12:02:08 -0400306 cursor = xcb_cursor_images_load_cursor (wm, images);
307 XcursorImagesDestroy (images);
308
309 return cursor;
310}
311
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400312void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400313dump_property(struct weston_wm *wm,
314 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400315{
316 int32_t *incr_value;
317 const char *text_value, *name;
318 xcb_atom_t *atom_value;
319 int width, len;
320 uint32_t i;
321
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400322 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400323 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400324 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400325 return;
326 }
327
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400328 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
329 get_atom_name(wm->conn, reply->type),
330 reply->format,
331 xcb_get_property_value_length(reply),
332 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400333
334 if (reply->type == wm->atom.incr) {
335 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400336 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400337 } else if (reply->type == wm->atom.utf8_string ||
338 reply->type == wm->atom.string) {
339 text_value = xcb_get_property_value(reply);
340 if (reply->value_len > 40)
341 len = 40;
342 else
343 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400344 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400345 } else if (reply->type == XCB_ATOM_ATOM) {
346 atom_value = xcb_get_property_value(reply);
347 for (i = 0; i < reply->value_len; i++) {
348 name = get_atom_name(wm->conn, atom_value[i]);
349 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400350 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400351 width = 4;
352 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400353 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400354 }
355
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400356 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400357 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400358 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400359 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400360 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400361 }
362}
363
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200364static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400365read_and_dump_property(struct weston_wm *wm,
366 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400367{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400368 xcb_get_property_reply_t *reply;
369 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400370
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400371 cookie = xcb_get_property(wm->conn, 0, window,
372 property, XCB_ATOM_ANY, 0, 2048);
373 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400374
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400375 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400376
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400377 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400378}
379
380/* We reuse some predefined, but otherwise useles atoms */
381#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
382#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500383#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700384#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400385
386static void
387weston_wm_window_read_properties(struct weston_wm_window *window)
388{
389 struct weston_wm *wm = window->wm;
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200390 struct weston_shell_interface *shell_interface =
391 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400392
393#define F(field) offsetof(struct weston_wm_window, field)
394 const struct {
395 xcb_atom_t atom;
396 xcb_atom_t type;
397 int offset;
398 } props[] = {
399 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
400 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
401 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
402 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700403 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500404 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400405 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
406 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300407 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400408 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300409 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400410 };
411#undef F
412
413 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
414 xcb_get_property_reply_t *reply;
415 void *p;
416 uint32_t *xid;
417 xcb_atom_t *atom;
418 uint32_t i;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200419 char name[1024];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400420
421 if (!window->properties_dirty)
422 return;
423 window->properties_dirty = 0;
424
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400425 for (i = 0; i < ARRAY_LENGTH(props); i++)
426 cookie[i] = xcb_get_property(wm->conn,
427 0, /* delete */
428 window->id,
429 props[i].atom,
430 XCB_ATOM_ANY, 0, 2048);
431
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700432 window->decorate = window->override_redirect ? 0 : MWM_DECOR_EVERYTHING;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700433 window->size_hints.flags = 0;
434 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700435 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700436
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400437 for (i = 0; i < ARRAY_LENGTH(props); i++) {
438 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
439 if (!reply)
440 /* Bad window, typically */
441 continue;
442 if (reply->type == XCB_ATOM_NONE) {
443 /* No such property */
444 free(reply);
445 continue;
446 }
447
448 p = ((char *) window + props[i].offset);
449
450 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300451 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400452 case XCB_ATOM_STRING:
453 /* FIXME: We're using this for both string and
454 utf8_string */
455 if (*(char **) p)
456 free(*(char **) p);
457
458 *(char **) p =
459 strndup(xcb_get_property_value(reply),
460 xcb_get_property_value_length(reply));
461 break;
462 case XCB_ATOM_WINDOW:
463 xid = xcb_get_property_value(reply);
Derek Foreman49372142015-04-09 10:51:22 -0500464 if (!wm_lookup_window(wm, *xid, p))
465 weston_log("XCB_ATOM_WINDOW contains window"
466 " id not found in hash table.\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400467 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300468 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400469 case XCB_ATOM_ATOM:
470 atom = xcb_get_property_value(reply);
471 *(xcb_atom_t *) p = *atom;
472 break;
473 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700474 atom = xcb_get_property_value(reply);
475 for (i = 0; i < reply->value_len; i++)
Derek Foremanb4deec62015-04-07 12:12:13 -0500476 if (atom[i] == wm->atom.wm_delete_window) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700477 window->delete_window = 1;
Derek Foremanb4deec62015-04-07 12:12:13 -0500478 break;
479 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400480 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700481 case TYPE_WM_NORMAL_HINTS:
482 memcpy(&window->size_hints,
483 xcb_get_property_value(reply),
484 sizeof window->size_hints);
485 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500486 case TYPE_NET_WM_STATE:
487 window->fullscreen = 0;
488 atom = xcb_get_property_value(reply);
Ryo Munakataf3744f52015-03-11 17:36:30 +0900489 for (i = 0; i < reply->value_len; i++) {
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500490 if (atom[i] == wm->atom.net_wm_state_fullscreen)
491 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200492 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
493 window->maximized_vert = 1;
494 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
495 window->maximized_horz = 1;
Ryo Munakataf3744f52015-03-11 17:36:30 +0900496 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500497 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400498 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700499 memcpy(&window->motif_hints,
500 xcb_get_property_value(reply),
501 sizeof window->motif_hints);
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700502 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) {
503 if (window->motif_hints.decorations & MWM_DECOR_ALL)
504 /* MWM_DECOR_ALL means all except the other values listed. */
505 window->decorate =
506 MWM_DECOR_EVERYTHING & (~window->motif_hints.decorations);
507 else
508 window->decorate =
509 window->motif_hints.decorations;
510 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400511 break;
512 default:
513 break;
514 }
515 free(reply);
516 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200517
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200518 if (window->pid > 0) {
519 gethostname(name, sizeof(name));
520 for (i = 0; i < sizeof(name); i++) {
521 if (name[i] == '\0')
522 break;
523 }
524 if (i == sizeof(name))
525 name[0] = '\0'; /* ignore stupid hostnames */
526
527 /* this is only one heuristic to guess the PID of a client is
528 * valid, assuming it's compliant with icccm and ewmh.
529 * Non-compliants and remote applications of course fail. */
530 if (!window->machine || strcmp(window->machine, name))
531 window->pid = 0;
532 }
533
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200534 if (window->shsurf && window->name)
535 shell_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500536 if (window->frame && window->name)
537 frame_set_title(window->frame, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200538 if (window->shsurf && window->pid > 0)
539 shell_interface->set_pid(window->shsurf, window->pid);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400540}
541
542static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400543weston_wm_window_get_frame_size(struct weston_wm_window *window,
544 int *width, int *height)
545{
546 struct theme *t = window->wm->theme;
547
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500548 if (window->fullscreen) {
549 *width = window->width;
550 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800551 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500552 *width = frame_width(window->frame);
553 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400554 } else {
555 *width = window->width + t->margin * 2;
556 *height = window->height + t->margin * 2;
557 }
558}
559
560static void
561weston_wm_window_get_child_position(struct weston_wm_window *window,
562 int *x, int *y)
563{
564 struct theme *t = window->wm->theme;
565
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500566 if (window->fullscreen) {
567 *x = 0;
568 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800569 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500570 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400571 } else {
572 *x = t->margin;
573 *y = t->margin;
574 }
575}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400576
577static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500578weston_wm_window_send_configure_notify(struct weston_wm_window *window)
579{
580 xcb_configure_notify_event_t configure_notify;
581 struct weston_wm *wm = window->wm;
582 int x, y;
583
584 weston_wm_window_get_child_position(window, &x, &y);
585 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
586 configure_notify.pad0 = 0;
587 configure_notify.event = window->id;
588 configure_notify.window = window->id;
589 configure_notify.above_sibling = XCB_WINDOW_NONE;
590 configure_notify.x = x;
591 configure_notify.y = y;
592 configure_notify.width = window->width;
593 configure_notify.height = window->height;
594 configure_notify.border_width = 0;
595 configure_notify.override_redirect = 0;
596 configure_notify.pad1 = 0;
597
598 xcb_send_event(wm->conn, 0, window->id,
599 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
600 (char *) &configure_notify);
601}
602
603static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400604weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
605{
606 xcb_configure_request_event_t *configure_request =
607 (xcb_configure_request_event_t *) event;
608 struct weston_wm_window *window;
609 uint32_t mask, values[16];
610 int x, y, width, height, i = 0;
611
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400612 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
613 configure_request->window,
614 configure_request->x, configure_request->y,
615 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400616
Derek Foreman49372142015-04-09 10:51:22 -0500617 if (!wm_lookup_window(wm, configure_request->window, &window))
618 return;
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400619
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500620 if (window->fullscreen) {
621 weston_wm_window_send_configure_notify(window);
622 return;
623 }
624
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400625 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
626 window->width = configure_request->width;
627 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
628 window->height = configure_request->height;
629
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500630 if (window->frame)
631 frame_resize_inside(window->frame, window->width, window->height);
632
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400633 weston_wm_window_get_child_position(window, &x, &y);
634 values[i++] = x;
635 values[i++] = y;
636 values[i++] = window->width;
637 values[i++] = window->height;
638 values[i++] = 0;
639 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
640 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
641 XCB_CONFIG_WINDOW_BORDER_WIDTH;
642 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
643 values[i++] = configure_request->sibling;
644 mask |= XCB_CONFIG_WINDOW_SIBLING;
645 }
646 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
647 values[i++] = configure_request->stack_mode;
648 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
649 }
650
651 xcb_configure_window(wm->conn, window->id, mask, values);
652
653 weston_wm_window_get_frame_size(window, &width, &height);
654 values[0] = width;
655 values[1] = height;
656 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
657 xcb_configure_window(wm->conn, window->frame_id, mask, values);
658
659 weston_wm_window_schedule_repaint(window);
660}
661
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400662static int
663our_resource(struct weston_wm *wm, uint32_t id)
664{
665 const xcb_setup_t *setup;
666
667 setup = xcb_get_setup(wm->conn);
668
669 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
670}
671
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400672static void
673weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
674{
675 xcb_configure_notify_event_t *configure_notify =
676 (xcb_configure_notify_event_t *) event;
677 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400678
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400679 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400680 configure_notify->window,
681 configure_notify->x, configure_notify->y,
682 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300683
Derek Foreman49372142015-04-09 10:51:22 -0500684 if (!wm_lookup_window(wm, configure_notify->window, &window))
685 return;
686
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700687 window->x = configure_notify->x;
688 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700689 if (window->override_redirect) {
690 window->width = configure_notify->width;
691 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500692 if (window->frame)
693 frame_resize_inside(window->frame,
694 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700695 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400696}
697
698static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300699weston_wm_kill_client(struct wl_listener *listener, void *data)
700{
701 struct weston_surface *surface = data;
702 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300703 if (!window)
704 return;
705
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200706 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300707 kill(window->pid, SIGKILL);
708}
709
710static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700711weston_wm_create_surface(struct wl_listener *listener, void *data)
712{
713 struct weston_surface *surface = data;
714 struct weston_wm *wm =
715 container_of(listener,
716 struct weston_wm, create_surface_listener);
717 struct weston_wm_window *window;
718
719 if (wl_resource_get_client(surface->resource) != wm->server->client)
720 return;
721
722 wl_list_for_each(window, &wm->unpaired_window_list, link)
723 if (window->surface_id ==
724 wl_resource_get_id(surface->resource)) {
725 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700726 window->surface_id = 0;
727 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700728 break;
729 }
730}
731
732static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400733weston_wm_window_activate(struct wl_listener *listener, void *data)
734{
735 struct weston_surface *surface = data;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200736 struct weston_wm_window *window = NULL;
Scott Moreau85ecac02012-05-21 15:49:14 -0600737 struct weston_wm *wm =
738 container_of(listener, struct weston_wm, activate_listener);
739 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400740
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200741 if (surface) {
742 window = get_wm_window(surface);
743 }
744
Scott Moreau85ecac02012-05-21 15:49:14 -0600745 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700746 uint32_t values[1];
747
Boyan Dingb9f863c2014-08-30 10:33:23 +0800748 if (window->override_redirect)
749 return;
750
Scott Moreau85ecac02012-05-21 15:49:14 -0600751 client_message.response_type = XCB_CLIENT_MESSAGE;
752 client_message.format = 32;
753 client_message.window = window->id;
754 client_message.type = wm->atom.wm_protocols;
755 client_message.data.data32[0] = wm->atom.wm_take_focus;
756 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
757
758 xcb_send_event(wm->conn, 0, window->id,
759 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
760 (char *) &client_message);
761
762 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
763 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700764
765 values[0] = XCB_STACK_MODE_ABOVE;
766 xcb_configure_window (wm->conn, window->frame_id,
767 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600768 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400769 xcb_set_input_focus (wm->conn,
770 XCB_INPUT_FOCUS_POINTER_ROOT,
771 XCB_NONE,
772 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600773 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400774
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500775 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800776 if (wm->focus_window->frame)
777 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400778 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500779 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400780 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500781 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800782 if (wm->focus_window->frame)
783 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400784 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500785 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400786}
787
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300788static void
789weston_wm_window_transform(struct wl_listener *listener, void *data)
790{
791 struct weston_surface *surface = data;
792 struct weston_wm_window *window = get_wm_window(surface);
793 struct weston_wm *wm =
794 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300795 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300796
797 if (!window || !wm)
798 return;
799
Jason Ekstranda7af7042013-10-12 22:38:11 -0500800 if (!window->view || !weston_view_is_mapped(window->view))
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300801 return;
802
Jason Ekstranda7af7042013-10-12 22:38:11 -0500803 if (window->x != window->view->geometry.x ||
804 window->y != window->view->geometry.y) {
805 values[0] = window->view->geometry.x;
806 values[1] = window->view->geometry.y;
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700807 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300808
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700809 xcb_configure_window(wm->conn, window->frame_id, mask, values);
810 xcb_flush(wm->conn);
811 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300812}
813
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400814#define ICCCM_WITHDRAWN_STATE 0
815#define ICCCM_NORMAL_STATE 1
816#define ICCCM_ICONIC_STATE 3
817
818static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500819weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400820{
821 struct weston_wm *wm = window->wm;
822 uint32_t property[2];
823
824 property[0] = state;
825 property[1] = XCB_WINDOW_NONE;
826
827 xcb_change_property(wm->conn,
828 XCB_PROP_MODE_REPLACE,
829 window->id,
830 wm->atom.wm_state,
831 wm->atom.wm_state,
832 32, /* format */
833 2, property);
834}
835
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400836static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500837weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
838{
839 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200840 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500841 int i;
842
843 i = 0;
844 if (window->fullscreen)
845 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200846 if (window->maximized_vert)
847 property[i++] = wm->atom.net_wm_state_maximized_vert;
848 if (window->maximized_horz)
849 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500850
851 xcb_change_property(wm->conn,
852 XCB_PROP_MODE_REPLACE,
853 window->id,
854 wm->atom.net_wm_state,
855 XCB_ATOM_ATOM,
856 32, /* format */
857 i, property);
858}
859
860static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700861weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400862{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700863 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400864 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400865 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200866 int buttons = FRAME_BUTTON_CLOSE;
867
868 if (window->decorate & MWM_DECOR_MAXIMIZE)
869 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400870
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500871 window->frame = frame_create(window->wm->theme,
872 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200873 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500874 frame_resize_inside(window->frame, window->width, window->height);
875
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400876 weston_wm_window_get_frame_size(window, &width, &height);
877 weston_wm_window_get_child_position(window, &x, &y);
878
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400879 values[0] = wm->screen->black_pixel;
880 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400881 XCB_EVENT_MASK_KEY_PRESS |
882 XCB_EVENT_MASK_KEY_RELEASE |
883 XCB_EVENT_MASK_BUTTON_PRESS |
884 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400885 XCB_EVENT_MASK_POINTER_MOTION |
886 XCB_EVENT_MASK_ENTER_WINDOW |
887 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400888 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400889 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400890 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400891
892 window->frame_id = xcb_generate_id(wm->conn);
893 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400894 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400895 window->frame_id,
896 wm->screen->root,
897 0, 0,
898 width, height,
899 0,
900 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400901 wm->visual_id,
902 XCB_CW_BORDER_PIXEL |
903 XCB_CW_EVENT_MASK |
904 XCB_CW_COLORMAP, values);
905
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400906 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
907
908 values[0] = 0;
909 xcb_configure_window(wm->conn, window->id,
910 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
911
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400912 window->cairo_surface =
913 cairo_xcb_surface_create_with_xrender_format(wm->conn,
914 wm->screen,
915 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400916 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400917 width, height);
918
919 hash_table_insert(wm->window_hash, window->frame_id, window);
920}
921
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200922/*
923 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
924 * Passing a <0 desktop value deletes the property.
925 */
926static void
927weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
928 int desktop)
929{
930 if (desktop >= 0) {
931 xcb_change_property(window->wm->conn,
932 XCB_PROP_MODE_REPLACE,
933 window->id,
934 window->wm->atom.net_wm_desktop,
935 XCB_ATOM_CARDINAL,
936 32, /* format */
937 1, &desktop);
938 } else {
939 xcb_delete_property(window->wm->conn,
940 window->id,
941 window->wm->atom.net_wm_desktop);
942 }
943}
944
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400945static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700946weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
947{
948 xcb_map_request_event_t *map_request =
949 (xcb_map_request_event_t *) event;
950 struct weston_wm_window *window;
951
952 if (our_resource(wm, map_request->window)) {
953 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
954 map_request->window);
955 return;
956 }
957
Derek Foreman49372142015-04-09 10:51:22 -0500958 if (!wm_lookup_window(wm, map_request->window, &window))
959 return;
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700960
961 weston_wm_window_read_properties(window);
962
963 if (window->frame_id == XCB_WINDOW_NONE)
964 weston_wm_window_create_frame(window);
965
966 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
967 window->id, window, window->frame_id);
968
969 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
970 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200971 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700972
973 xcb_map_window(wm->conn, map_request->window);
974 xcb_map_window(wm->conn, window->frame_id);
975}
976
977static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400978weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
979{
980 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
981
982 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400983 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
984 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400985 return;
986 }
987
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400988 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400989}
990
991static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400992weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
993{
994 xcb_unmap_notify_event_t *unmap_notify =
995 (xcb_unmap_notify_event_t *) event;
996 struct weston_wm_window *window;
997
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400998 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
999 unmap_notify->window,
1000 unmap_notify->event,
1001 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001002
1003 if (our_resource(wm, unmap_notify->window))
1004 return;
1005
MoD31700122013-06-11 19:58:55 -05001006 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -04001007 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1008 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001009 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001010
Derek Foreman49372142015-04-09 10:51:22 -05001011 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1012 return;
1013
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001014 if (window->surface_id) {
1015 /* Make sure we're not on the unpaired surface list or we
1016 * could be assigned a surface during surface creation that
1017 * was mapped before this unmap request.
1018 */
1019 wl_list_remove(&window->link);
1020 window->surface_id = 0;
1021 }
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001022 if (wm->focus_window == window)
1023 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001024 if (window->surface)
1025 wl_list_remove(&window->surface_destroy_listener.link);
1026 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +02001027 window->shsurf = NULL;
Dima Ryazanove5a32082013-11-15 02:01:18 -08001028 window->view = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001029
1030 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1031 weston_wm_window_set_virtual_desktop(window, -1);
1032
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001033 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001034}
1035
1036static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001037weston_wm_window_draw_decoration(void *data)
1038{
1039 struct weston_wm_window *window = data;
1040 struct weston_wm *wm = window->wm;
1041 struct theme *t = wm->theme;
1042 cairo_t *cr;
1043 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001044 int32_t input_x, input_y, input_w, input_h;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07001045 struct weston_shell_interface *shell_interface =
1046 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001047 uint32_t flags = 0;
1048
1049 weston_wm_window_read_properties(window);
1050
1051 window->repaint_source = NULL;
1052
1053 weston_wm_window_get_frame_size(window, &width, &height);
1054 weston_wm_window_get_child_position(window, &x, &y);
1055
1056 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1057 cr = cairo_create(window->cairo_surface);
1058
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001059 if (window->fullscreen) {
1060 /* nothing */
1061 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001062 if (wm->focus_window == window)
1063 flags |= THEME_FRAME_ACTIVE;
1064
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001065 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001066 } else {
1067 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1068 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1069 cairo_paint(cr);
1070
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001071 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001072 }
1073
1074 cairo_destroy(cr);
1075
1076 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001077 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -05001078 if(window->has_alpha) {
1079 pixman_region32_init(&window->surface->pending.opaque);
1080 } else {
1081 /* We leave an extra pixel around the X window area to
1082 * make sure we don't sample from the undefined alpha
1083 * channel when filtering. */
1084 pixman_region32_init_rect(&window->surface->pending.opaque,
1085 x - 1, y - 1,
1086 window->width + 2,
1087 window->height + 2);
1088 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001089 if (window->view)
1090 weston_view_geometry_dirty(window->view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001091
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001092 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001093
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001094 if (window->decorate && !window->fullscreen) {
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001095 frame_input_rect(window->frame, &input_x, &input_y,
1096 &input_w, &input_h);
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001097 } else {
1098 input_x = x;
1099 input_y = y;
1100 input_w = width;
1101 input_h = height;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001102 }
1103
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001104 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001105 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001106
1107 shell_interface->set_window_geometry(window->shsurf,
1108 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001109 }
1110}
1111
1112static void
1113weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1114{
1115 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001116 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001117
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001118 if (window->frame_id == XCB_WINDOW_NONE) {
1119 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001120 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001121 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -05001122 if(window->has_alpha) {
1123 pixman_region32_init(&window->surface->pending.opaque);
1124 } else {
1125 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1126 width, height);
1127 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001128 if (window->view)
1129 weston_view_geometry_dirty(window->view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001130 }
1131 return;
1132 }
1133
1134 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001135 return;
1136
1137 window->repaint_source =
1138 wl_event_loop_add_idle(wm->server->loop,
1139 weston_wm_window_draw_decoration,
1140 window);
1141}
1142
1143static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001144weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1145{
1146 xcb_property_notify_event_t *property_notify =
1147 (xcb_property_notify_event_t *) event;
1148 struct weston_wm_window *window;
1149
Derek Foreman49372142015-04-09 10:51:22 -05001150 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001151 return;
1152
1153 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001154
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001155 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001156 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001157 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001158 else
1159 read_and_dump_property(wm, property_notify->window,
1160 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001161
1162 if (property_notify->atom == wm->atom.net_wm_name ||
1163 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001164 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001165}
1166
1167static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001168weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001169 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001170{
1171 struct weston_wm_window *window;
1172 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001173 xcb_get_geometry_cookie_t geometry_cookie;
1174 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001175
Peter Huttererf3d62272013-08-08 11:57:05 +10001176 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001177 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001178 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001179 return;
1180 }
1181
MoD384a11a2013-06-22 11:04:21 -05001182 geometry_cookie = xcb_get_geometry(wm->conn, id);
1183
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001184 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1185 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1186
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001187 window->wm = wm;
1188 window->id = id;
1189 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001190 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001191 window->width = width;
1192 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001193 window->x = x;
1194 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001195
MoD384a11a2013-06-22 11:04:21 -05001196 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1197 /* technically we should use XRender and check the visual format's
1198 alpha_mask, but checking depth is simpler and works in all known cases */
1199 if(geometry_reply != NULL)
1200 window->has_alpha = geometry_reply->depth == 32;
1201 free(geometry_reply);
1202
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001203 hash_table_insert(wm->window_hash, id, window);
1204}
1205
1206static void
1207weston_wm_window_destroy(struct weston_wm_window *window)
1208{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001209 struct weston_wm *wm = window->wm;
1210
1211 if (window->repaint_source)
1212 wl_event_source_remove(window->repaint_source);
1213 if (window->cairo_surface)
1214 cairo_surface_destroy(window->cairo_surface);
1215
1216 if (window->frame_id) {
1217 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1218 xcb_destroy_window(wm->conn, window->frame_id);
1219 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001220 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001221 hash_table_remove(wm->window_hash, window->frame_id);
1222 window->frame_id = XCB_WINDOW_NONE;
1223 }
1224
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001225 if (window->surface_id)
1226 wl_list_remove(&window->link);
1227
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001228 if (window->surface)
1229 wl_list_remove(&window->surface_destroy_listener.link);
1230
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001231 hash_table_remove(window->wm->window_hash, window->id);
1232 free(window);
1233}
1234
1235static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001236weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1237{
1238 xcb_create_notify_event_t *create_notify =
1239 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001240
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001241 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1242 create_notify->window,
1243 create_notify->width, create_notify->height,
1244 create_notify->override_redirect ? ", override" : "",
1245 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001246
1247 if (our_resource(wm, create_notify->window))
1248 return;
1249
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001250 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001251 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001252 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001253 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001254}
1255
1256static void
1257weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1258{
1259 xcb_destroy_notify_event_t *destroy_notify =
1260 (xcb_destroy_notify_event_t *) event;
1261 struct weston_wm_window *window;
1262
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001263 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1264 destroy_notify->window,
1265 destroy_notify->event,
1266 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001267
1268 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001269 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001270
Derek Foreman49372142015-04-09 10:51:22 -05001271 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1272 return;
1273
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001274 weston_wm_window_destroy(window);
1275}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001276
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001277static void
1278weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1279{
1280 xcb_reparent_notify_event_t *reparent_notify =
1281 (xcb_reparent_notify_event_t *) event;
1282 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001283
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001284 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1285 reparent_notify->window,
1286 reparent_notify->parent,
1287 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001288
1289 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001290 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001291 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001292 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001293 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001294 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1295 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001296 weston_wm_window_destroy(window);
1297 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001298}
1299
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001300struct weston_seat *
1301weston_wm_pick_seat(struct weston_wm *wm)
1302{
1303 return container_of(wm->server->compositor->seat_list.next,
1304 struct weston_seat, link);
1305}
1306
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001307static struct weston_seat *
1308weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1309{
1310 struct weston_wm *wm = window->wm;
1311 struct weston_seat *seat, *s;
1312
1313 seat = NULL;
1314 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Giulio Camuffoad7305a2014-10-04 13:58:33 +03001315 if (s->pointer != NULL && s->pointer->focus &&
1316 s->pointer->focus->surface == window->surface &&
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001317 s->pointer->button_count > 0 &&
1318 (seat == NULL ||
1319 s->pointer->grab_serial -
1320 seat->pointer->grab_serial < (1 << 30)))
1321 seat = s;
1322 }
1323
1324 return seat;
1325}
1326
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001327static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001328weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1329 xcb_client_message_event_t *client_message)
1330{
1331 static const int map[] = {
1332 THEME_LOCATION_RESIZING_TOP_LEFT,
1333 THEME_LOCATION_RESIZING_TOP,
1334 THEME_LOCATION_RESIZING_TOP_RIGHT,
1335 THEME_LOCATION_RESIZING_RIGHT,
1336 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1337 THEME_LOCATION_RESIZING_BOTTOM,
1338 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1339 THEME_LOCATION_RESIZING_LEFT
1340 };
1341
1342 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001343 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001344 int detail;
1345 struct weston_shell_interface *shell_interface =
1346 &wm->server->compositor->shell_interface;
1347
Boyan Dingc06a1802014-07-06 11:44:58 +08001348 if (seat == NULL || seat->pointer->button_count != 1
Giulio Camuffoad7305a2014-10-04 13:58:33 +03001349 || !seat->pointer->focus
1350 || seat->pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001351 return;
1352
1353 detail = client_message->data.data32[2];
1354 switch (detail) {
1355 case _NET_WM_MOVERESIZE_MOVE:
1356 shell_interface->move(window->shsurf, seat);
1357 break;
1358 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1359 case _NET_WM_MOVERESIZE_SIZE_TOP:
1360 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1361 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1362 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1363 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1364 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1365 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1366 shell_interface->resize(window->shsurf, seat, map[detail]);
1367 break;
1368 case _NET_WM_MOVERESIZE_CANCEL:
1369 break;
1370 }
1371}
1372
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001373#define _NET_WM_STATE_REMOVE 0
1374#define _NET_WM_STATE_ADD 1
1375#define _NET_WM_STATE_TOGGLE 2
1376
1377static int
1378update_state(int action, int *state)
1379{
1380 int new_state, changed;
1381
1382 switch (action) {
1383 case _NET_WM_STATE_REMOVE:
1384 new_state = 0;
1385 break;
1386 case _NET_WM_STATE_ADD:
1387 new_state = 1;
1388 break;
1389 case _NET_WM_STATE_TOGGLE:
1390 new_state = !*state;
1391 break;
1392 default:
1393 return 0;
1394 }
1395
1396 changed = (*state != new_state);
1397 *state = new_state;
1398
1399 return changed;
1400}
1401
1402static void
1403weston_wm_window_configure(void *data);
1404
1405static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001406weston_wm_window_set_toplevel(struct weston_wm_window *window)
1407{
1408 struct weston_shell_interface *shell_interface =
1409 &window->wm->server->compositor->shell_interface;
1410
1411 shell_interface->set_toplevel(window->shsurf);
1412 window->width = window->saved_width;
1413 window->height = window->saved_height;
1414 if (window->frame)
1415 frame_resize_inside(window->frame,
1416 window->width,
1417 window->height);
1418 weston_wm_window_configure(window);
1419}
1420
1421static inline bool
1422weston_wm_window_is_maximized(struct weston_wm_window *window)
1423{
1424 return window->maximized_horz && window->maximized_vert;
1425}
1426
1427static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001428weston_wm_window_handle_state(struct weston_wm_window *window,
1429 xcb_client_message_event_t *client_message)
1430{
1431 struct weston_wm *wm = window->wm;
1432 struct weston_shell_interface *shell_interface =
1433 &wm->server->compositor->shell_interface;
1434 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001435 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001436
1437 action = client_message->data.data32[0];
1438 property = client_message->data.data32[1];
1439
1440 if (property == wm->atom.net_wm_state_fullscreen &&
1441 update_state(action, &window->fullscreen)) {
1442 weston_wm_window_set_net_wm_state(window);
1443 if (window->fullscreen) {
1444 window->saved_width = window->width;
1445 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001446
1447 if (window->shsurf)
1448 shell_interface->set_fullscreen(window->shsurf,
1449 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1450 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001451 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001452 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001453 weston_wm_window_set_toplevel(window);
1454 }
1455 } else {
1456 if (property == wm->atom.net_wm_state_maximized_vert &&
1457 update_state(action, &window->maximized_vert))
1458 weston_wm_window_set_net_wm_state(window);
1459 if (property == wm->atom.net_wm_state_maximized_horz &&
1460 update_state(action, &window->maximized_horz))
1461 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001462
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001463 if (maximized != weston_wm_window_is_maximized(window)) {
1464 if (weston_wm_window_is_maximized(window)) {
1465 window->saved_width = window->width;
1466 window->saved_height = window->height;
1467
1468 if (window->shsurf)
1469 shell_interface->set_maximized(window->shsurf);
1470 } else if (window->shsurf) {
1471 weston_wm_window_set_toplevel(window);
1472 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001473 }
1474 }
1475}
1476
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001477static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001478surface_destroy(struct wl_listener *listener, void *data)
1479{
1480 struct weston_wm_window *window =
1481 container_of(listener,
1482 struct weston_wm_window, surface_destroy_listener);
1483
1484 wm_log("surface for xid %d destroyed\n", window->id);
1485
1486 /* This should have been freed by the shell.
1487 * Don't try to use it later. */
1488 window->shsurf = NULL;
1489 window->surface = NULL;
1490 window->view = NULL;
1491}
1492
1493static void
1494weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1495 xcb_client_message_event_t *client_message)
1496{
1497 struct weston_wm *wm = window->wm;
1498 struct wl_resource *resource;
1499
1500 if (window->surface_id != 0) {
1501 wm_log("already have surface id for window %d\n", window->id);
1502 return;
1503 }
1504
1505 /* Xwayland will send the wayland requests to create the
1506 * wl_surface before sending this client message. Even so, we
1507 * can end up handling the X event before the wayland requests
1508 * and thus when we try to look up the surface ID, the surface
1509 * hasn't been created yet. In that case put the window on
1510 * the unpaired window list and continue when the surface gets
1511 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001512 uint32_t id = client_message->data.data32[0];
1513 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001514 if (resource) {
1515 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001516 xserver_map_shell_surface(window,
1517 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001518 }
1519 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001520 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001521 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001522 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001523}
1524
1525static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001526weston_wm_handle_client_message(struct weston_wm *wm,
1527 xcb_generic_event_t *event)
1528{
1529 xcb_client_message_event_t *client_message =
1530 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001531 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001532
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001533 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1534 get_atom_name(wm->conn, client_message->type),
1535 client_message->data.data32[0],
1536 client_message->data.data32[1],
1537 client_message->data.data32[2],
1538 client_message->data.data32[3],
1539 client_message->data.data32[4],
1540 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001541
Jason Ekstrand0250a742014-07-24 13:17:47 -07001542 /* The window may get created and destroyed before we actually
1543 * handle the message. If it doesn't exist, bail.
1544 */
Derek Foreman49372142015-04-09 10:51:22 -05001545 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001546 return;
1547
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001548 if (client_message->type == wm->atom.net_wm_moveresize)
1549 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001550 else if (client_message->type == wm->atom.net_wm_state)
1551 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001552 else if (client_message->type == wm->atom.wl_surface_id)
1553 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001554}
1555
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001556enum cursor_type {
1557 XWM_CURSOR_TOP,
1558 XWM_CURSOR_BOTTOM,
1559 XWM_CURSOR_LEFT,
1560 XWM_CURSOR_RIGHT,
1561 XWM_CURSOR_TOP_LEFT,
1562 XWM_CURSOR_TOP_RIGHT,
1563 XWM_CURSOR_BOTTOM_LEFT,
1564 XWM_CURSOR_BOTTOM_RIGHT,
1565 XWM_CURSOR_LEFT_PTR,
1566};
1567
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001568/*
1569 * The following correspondences between file names and cursors was copied
1570 * from: https://bugs.kde.org/attachment.cgi?id=67313
1571 */
1572
1573static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001574 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001575 "sw-resize",
1576 "size_bdiag"
1577};
1578
1579static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001580 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001581 "se-resize",
1582 "size_fdiag"
1583};
1584
1585static const char *bottom_sides[] = {
1586 "bottom_side",
1587 "s-resize",
1588 "size_ver"
1589};
1590
1591static const char *left_ptrs[] = {
1592 "left_ptr",
1593 "default",
1594 "top_left_arrow",
1595 "left-arrow"
1596};
1597
1598static const char *left_sides[] = {
1599 "left_side",
1600 "w-resize",
1601 "size_hor"
1602};
1603
1604static const char *right_sides[] = {
1605 "right_side",
1606 "e-resize",
1607 "size_hor"
1608};
1609
1610static const char *top_left_corners[] = {
1611 "top_left_corner",
1612 "nw-resize",
1613 "size_fdiag"
1614};
1615
1616static const char *top_right_corners[] = {
1617 "top_right_corner",
1618 "ne-resize",
1619 "size_bdiag"
1620};
1621
1622static const char *top_sides[] = {
1623 "top_side",
1624 "n-resize",
1625 "size_ver"
1626};
1627
1628struct cursor_alternatives {
1629 const char **names;
1630 size_t count;
1631};
1632
1633static const struct cursor_alternatives cursors[] = {
1634 {top_sides, ARRAY_LENGTH(top_sides)},
1635 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1636 {left_sides, ARRAY_LENGTH(left_sides)},
1637 {right_sides, ARRAY_LENGTH(right_sides)},
1638 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1639 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1640 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1641 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1642 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001643};
1644
1645static void
1646weston_wm_create_cursors(struct weston_wm *wm)
1647{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001648 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001649 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001650 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001651
1652 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1653 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001654 for (j = 0; j < cursors[i].count; j++) {
1655 name = cursors[i].names[j];
1656 wm->cursors[i] =
1657 xcb_cursor_library_load_cursor(wm, name);
1658 if (wm->cursors[i] != (xcb_cursor_t)-1)
1659 break;
1660 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001661 }
1662
1663 wm->last_cursor = -1;
1664}
1665
1666static void
1667weston_wm_destroy_cursors(struct weston_wm *wm)
1668{
1669 uint8_t i;
1670
1671 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1672 xcb_free_cursor(wm->conn, wm->cursors[i]);
1673
1674 free(wm->cursors);
1675}
1676
1677static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001678get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001679{
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001680 // int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001681
1682 switch (location) {
1683 case THEME_LOCATION_RESIZING_TOP:
1684 return XWM_CURSOR_TOP;
1685 case THEME_LOCATION_RESIZING_BOTTOM:
1686 return XWM_CURSOR_BOTTOM;
1687 case THEME_LOCATION_RESIZING_LEFT:
1688 return XWM_CURSOR_LEFT;
1689 case THEME_LOCATION_RESIZING_RIGHT:
1690 return XWM_CURSOR_RIGHT;
1691 case THEME_LOCATION_RESIZING_TOP_LEFT:
1692 return XWM_CURSOR_TOP_LEFT;
1693 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1694 return XWM_CURSOR_TOP_RIGHT;
1695 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1696 return XWM_CURSOR_BOTTOM_LEFT;
1697 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1698 return XWM_CURSOR_BOTTOM_RIGHT;
1699 case THEME_LOCATION_EXTERIOR:
1700 case THEME_LOCATION_TITLEBAR:
1701 default:
1702 return XWM_CURSOR_LEFT_PTR;
1703 }
1704}
1705
1706static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001707weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1708 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001709{
1710 uint32_t cursor_value_list;
1711
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001712 if (wm->last_cursor == cursor)
1713 return;
1714
1715 wm->last_cursor = cursor;
1716
1717 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001718 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001719 XCB_CW_CURSOR, &cursor_value_list);
1720 xcb_flush(wm->conn);
1721}
1722
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001723static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001724weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001725{
1726 xcb_client_message_event_t client_message;
1727
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001728 if (window->delete_window) {
1729 client_message.response_type = XCB_CLIENT_MESSAGE;
1730 client_message.format = 32;
1731 client_message.window = window->id;
1732 client_message.type = window->wm->atom.wm_protocols;
1733 client_message.data.data32[0] =
1734 window->wm->atom.wm_delete_window;
1735 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001736
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001737 xcb_send_event(window->wm->conn, 0, window->id,
1738 XCB_EVENT_MASK_NO_EVENT,
1739 (char *) &client_message);
1740 } else {
1741 xcb_kill_client(window->wm->conn, window->id);
1742 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001743}
1744
1745static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001746weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1747{
1748 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1749 struct weston_shell_interface *shell_interface =
1750 &wm->server->compositor->shell_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001751 struct weston_seat *seat;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001752 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001753 enum theme_location location;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001754 enum frame_button_state button_state;
1755 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001756
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001757 wm_log("XCB_BUTTON_%s (detail %d)\n",
1758 button->response_type == XCB_BUTTON_PRESS ?
1759 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001760
Derek Foreman49372142015-04-09 10:51:22 -05001761 if (!wm_lookup_window(wm, button->event, &window) ||
1762 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001763 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001764
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001765 if (button->detail != 1 && button->detail != 2)
1766 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001767
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001768 seat = weston_wm_pick_seat_for_window(window);
1769
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001770 button_state = button->response_type == XCB_BUTTON_PRESS ?
1771 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1772 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1773
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001774 /* Make sure we're looking at the right location. The frame
1775 * could have received a motion event from a pointer from a
1776 * different wl_seat, but under X it looks like our core
1777 * pointer moved. Move the frame pointer to the button press
1778 * location before deciding what to do. */
1779 location = frame_pointer_motion(window->frame, NULL,
1780 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001781 location = frame_pointer_button(window->frame, NULL,
1782 button_id, button_state);
1783 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1784 weston_wm_window_schedule_repaint(window);
1785
1786 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Boyan Ding3c6d20c2014-09-03 17:25:30 +08001787 if (seat != NULL)
1788 shell_interface->move(window->shsurf, seat);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001789 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1790 }
1791
1792 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Boyan Ding3c6d20c2014-09-03 17:25:30 +08001793 if (seat != NULL)
1794 shell_interface->resize(window->shsurf, seat, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001795 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1796 }
1797
1798 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001799 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001800 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001801 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001802
1803 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1804 window->maximized_horz = !window->maximized_horz;
1805 window->maximized_vert = !window->maximized_vert;
1806 if (weston_wm_window_is_maximized(window)) {
1807 window->saved_width = window->width;
1808 window->saved_height = window->height;
1809 shell_interface->set_maximized(window->shsurf);
1810 } else {
1811 weston_wm_window_set_toplevel(window);
1812 }
1813 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1814 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001815}
1816
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001817static void
1818weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1819{
1820 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1821 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001822 enum theme_location location;
1823 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001824
Derek Foreman49372142015-04-09 10:51:22 -05001825 if (!wm_lookup_window(wm, motion->event, &window) ||
1826 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001827 return;
1828
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001829 location = frame_pointer_motion(window->frame, NULL,
1830 motion->event_x, motion->event_y);
1831 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1832 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001833
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001834 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001835 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001836}
1837
1838static void
1839weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1840{
1841 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1842 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001843 enum theme_location location;
1844 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001845
Derek Foreman49372142015-04-09 10:51:22 -05001846 if (!wm_lookup_window(wm, enter->event, &window) ||
1847 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001848 return;
1849
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001850 location = frame_pointer_enter(window->frame, NULL,
1851 enter->event_x, enter->event_y);
1852 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1853 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001854
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001855 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001856 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001857}
1858
1859static void
1860weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1861{
1862 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1863 struct weston_wm_window *window;
1864
Derek Foreman49372142015-04-09 10:51:22 -05001865 if (!wm_lookup_window(wm, leave->event, &window) ||
1866 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001867 return;
1868
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001869 frame_pointer_leave(window->frame, NULL);
1870 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1871 weston_wm_window_schedule_repaint(window);
1872
Tiago Vignattic1903232012-07-16 12:15:37 -04001873 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001874}
1875
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001876static int
1877weston_wm_handle_event(int fd, uint32_t mask, void *data)
1878{
1879 struct weston_wm *wm = data;
1880 xcb_generic_event_t *event;
1881 int count = 0;
1882
1883 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1884 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001885 free(event);
1886 count++;
1887 continue;
1888 }
1889
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001890 if (weston_wm_handle_dnd_event(wm, event)) {
1891 free(event);
1892 count++;
1893 continue;
1894 }
1895
MoD31700122013-06-11 19:58:55 -05001896 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001897 case XCB_BUTTON_PRESS:
1898 case XCB_BUTTON_RELEASE:
1899 weston_wm_handle_button(wm, event);
1900 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001901 case XCB_ENTER_NOTIFY:
1902 weston_wm_handle_enter(wm, event);
1903 break;
1904 case XCB_LEAVE_NOTIFY:
1905 weston_wm_handle_leave(wm, event);
1906 break;
1907 case XCB_MOTION_NOTIFY:
1908 weston_wm_handle_motion(wm, event);
1909 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001910 case XCB_CREATE_NOTIFY:
1911 weston_wm_handle_create_notify(wm, event);
1912 break;
1913 case XCB_MAP_REQUEST:
1914 weston_wm_handle_map_request(wm, event);
1915 break;
1916 case XCB_MAP_NOTIFY:
1917 weston_wm_handle_map_notify(wm, event);
1918 break;
1919 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001920 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001921 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001922 case XCB_REPARENT_NOTIFY:
1923 weston_wm_handle_reparent_notify(wm, event);
1924 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001925 case XCB_CONFIGURE_REQUEST:
1926 weston_wm_handle_configure_request(wm, event);
1927 break;
1928 case XCB_CONFIGURE_NOTIFY:
1929 weston_wm_handle_configure_notify(wm, event);
1930 break;
1931 case XCB_DESTROY_NOTIFY:
1932 weston_wm_handle_destroy_notify(wm, event);
1933 break;
1934 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001935 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001936 break;
1937 case XCB_PROPERTY_NOTIFY:
1938 weston_wm_handle_property_notify(wm, event);
1939 break;
1940 case XCB_CLIENT_MESSAGE:
1941 weston_wm_handle_client_message(wm, event);
1942 break;
1943 }
1944
1945 free(event);
1946 count++;
1947 }
1948
1949 xcb_flush(wm->conn);
1950
1951 return count;
1952}
1953
1954static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001955weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1956{
1957 xcb_depth_iterator_t d_iter;
1958 xcb_visualtype_iterator_t vt_iter;
1959 xcb_visualtype_t *visualtype;
1960
1961 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1962 visualtype = NULL;
1963 while (d_iter.rem > 0) {
1964 if (d_iter.data->depth == 32) {
1965 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1966 visualtype = vt_iter.data;
1967 break;
1968 }
1969
1970 xcb_depth_next(&d_iter);
1971 }
1972
1973 if (visualtype == NULL) {
1974 weston_log("no 32 bit visualtype\n");
1975 return;
1976 }
1977
1978 wm->visual_id = visualtype->visual_id;
1979 wm->colormap = xcb_generate_id(wm->conn);
1980 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1981 wm->colormap, wm->screen->root, wm->visual_id);
1982}
1983
1984static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001985weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001986{
1987
1988#define F(field) offsetof(struct weston_wm, field)
1989
1990 static const struct { const char *name; int offset; } atoms[] = {
1991 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001992 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001993 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1994 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001995 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001996 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001997 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001998 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001999 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002000 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002001 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2002 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002003 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2004 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002005 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2006 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2007 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02002008 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002009 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2010
2011 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2012 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2013 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2014 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2015 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2016 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2017 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002018 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2019 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002020 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2021 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2022 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2023 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2024 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2025
2026 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2027 { "_NET_SUPPORTING_WM_CHECK",
2028 F(atom.net_supporting_wm_check) },
2029 { "_NET_SUPPORTED", F(atom.net_supported) },
2030 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2031 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002032 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002033 { "TARGETS", F(atom.targets) },
2034 { "UTF8_STRING", F(atom.utf8_string) },
2035 { "_WL_SELECTION", F(atom.wl_selection) },
2036 { "INCR", F(atom.incr) },
2037 { "TIMESTAMP", F(atom.timestamp) },
2038 { "MULTIPLE", F(atom.multiple) },
2039 { "UTF8_STRING" , F(atom.utf8_string) },
2040 { "COMPOUND_TEXT", F(atom.compound_text) },
2041 { "TEXT", F(atom.text) },
2042 { "STRING", F(atom.string) },
2043 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2044 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002045 { "XdndSelection", F(atom.xdnd_selection) },
2046 { "XdndAware", F(atom.xdnd_aware) },
2047 { "XdndEnter", F(atom.xdnd_enter) },
2048 { "XdndLeave", F(atom.xdnd_leave) },
2049 { "XdndDrop", F(atom.xdnd_drop) },
2050 { "XdndStatus", F(atom.xdnd_status) },
2051 { "XdndFinished", F(atom.xdnd_finished) },
2052 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002053 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2054 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002055 };
2056#undef F
2057
2058 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2059 xcb_xfixes_query_version_reply_t *xfixes_reply;
2060 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2061 xcb_intern_atom_reply_t *reply;
2062 xcb_render_query_pict_formats_reply_t *formats_reply;
2063 xcb_render_query_pict_formats_cookie_t formats_cookie;
2064 xcb_render_pictforminfo_t *formats;
2065 uint32_t i;
2066
2067 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002068 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002069
2070 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2071
2072 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2073 cookies[i] = xcb_intern_atom (wm->conn, 0,
2074 strlen(atoms[i].name),
2075 atoms[i].name);
2076
2077 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2078 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2079 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2080 free(reply);
2081 }
2082
2083 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2084 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002085 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002086
2087 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2088 XCB_XFIXES_MAJOR_VERSION,
2089 XCB_XFIXES_MINOR_VERSION);
2090 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2091 xfixes_cookie, NULL);
2092
Martin Minarik6d118362012-06-07 18:01:59 +02002093 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002094 xfixes_reply->major_version, xfixes_reply->minor_version);
2095
2096 free(xfixes_reply);
2097
2098 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2099 formats_cookie, 0);
2100 if (formats_reply == NULL)
2101 return;
2102
2103 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002104 for (i = 0; i < formats_reply->num_formats; i++) {
2105 if (formats[i].direct.red_mask != 0xff &&
2106 formats[i].direct.red_shift != 16)
2107 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002108 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2109 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002110 wm->format_rgb = formats[i];
2111 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2112 formats[i].depth == 32 &&
2113 formats[i].direct.alpha_mask == 0xff &&
2114 formats[i].direct.alpha_shift == 24)
2115 wm->format_rgba = formats[i];
2116 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002117
2118 free(formats_reply);
2119}
2120
2121static void
2122weston_wm_create_wm_window(struct weston_wm *wm)
2123{
2124 static const char name[] = "Weston WM";
2125
2126 wm->wm_window = xcb_generate_id(wm->conn);
2127 xcb_create_window(wm->conn,
2128 XCB_COPY_FROM_PARENT,
2129 wm->wm_window,
2130 wm->screen->root,
2131 0, 0,
2132 10, 10,
2133 0,
2134 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2135 wm->screen->root_visual,
2136 0, NULL);
2137
2138 xcb_change_property(wm->conn,
2139 XCB_PROP_MODE_REPLACE,
2140 wm->wm_window,
2141 wm->atom.net_supporting_wm_check,
2142 XCB_ATOM_WINDOW,
2143 32, /* format */
2144 1, &wm->wm_window);
2145
2146 xcb_change_property(wm->conn,
2147 XCB_PROP_MODE_REPLACE,
2148 wm->wm_window,
2149 wm->atom.net_wm_name,
2150 wm->atom.utf8_string,
2151 8, /* format */
2152 strlen(name), name);
2153
2154 xcb_change_property(wm->conn,
2155 XCB_PROP_MODE_REPLACE,
2156 wm->screen->root,
2157 wm->atom.net_supporting_wm_check,
2158 XCB_ATOM_WINDOW,
2159 32, /* format */
2160 1, &wm->wm_window);
2161
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002162 /* Claim the WM_S0 selection even though we don't suport
2163 * the --replace functionality. */
2164 xcb_set_selection_owner(wm->conn,
2165 wm->wm_window,
2166 wm->atom.wm_s0,
2167 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002168
2169 xcb_set_selection_owner(wm->conn,
2170 wm->wm_window,
2171 wm->atom.net_wm_cm_s0,
2172 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002173}
2174
2175struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002176weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002177{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002178 struct weston_wm *wm;
2179 struct wl_event_loop *loop;
2180 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002181 uint32_t values[1];
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002182 xcb_atom_t supported[5];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002183
Peter Huttererf3d62272013-08-08 11:57:05 +10002184 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002185 if (wm == NULL)
2186 return NULL;
2187
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002188 wm->server = wxs;
2189 wm->window_hash = hash_table_create();
2190 if (wm->window_hash == NULL) {
2191 free(wm);
2192 return NULL;
2193 }
2194
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002195 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002196 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002197 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002198 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002199 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002200 hash_table_destroy(wm->window_hash);
2201 free(wm);
2202 return NULL;
2203 }
2204
2205 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2206 wm->screen = s.data;
2207
2208 loop = wl_display_get_event_loop(wxs->wl_display);
2209 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002210 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002211 WL_EVENT_READABLE,
2212 weston_wm_handle_event, wm);
2213 wl_event_source_check(wm->source);
2214
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002215 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002216 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002217
2218 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002219 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2220 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2221 XCB_EVENT_MASK_PROPERTY_CHANGE;
2222 xcb_change_window_attributes(wm->conn, wm->screen->root,
2223 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002224
2225 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2226 XCB_COMPOSITE_REDIRECT_MANUAL);
2227
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002228 wm->theme = theme_create();
2229
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002230 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002231 supported[1] = wm->atom.net_wm_state;
2232 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002233 supported[3] = wm->atom.net_wm_state_maximized_vert;
2234 supported[4] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002235 xcb_change_property(wm->conn,
2236 XCB_PROP_MODE_REPLACE,
2237 wm->screen->root,
2238 wm->atom.net_supported,
2239 XCB_ATOM_ATOM,
2240 32, /* format */
2241 ARRAY_LENGTH(supported), supported);
2242
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002243 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002244
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002245 weston_wm_dnd_init(wm);
2246
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002247 xcb_flush(wm->conn);
2248
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002249 wm->create_surface_listener.notify = weston_wm_create_surface;
2250 wl_signal_add(&wxs->compositor->create_surface_signal,
2251 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002252 wm->activate_listener.notify = weston_wm_window_activate;
2253 wl_signal_add(&wxs->compositor->activate_signal,
2254 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002255 wm->transform_listener.notify = weston_wm_window_transform;
2256 wl_signal_add(&wxs->compositor->transform_signal,
2257 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002258 wm->kill_listener.notify = weston_wm_kill_client;
2259 wl_signal_add(&wxs->compositor->kill_signal,
2260 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002261 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002262
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002263 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002264 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002265
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002266 /* Create wm window and take WM_S0 selection last, which
2267 * signals to Xwayland that we're done with setup. */
2268 weston_wm_create_wm_window(wm);
2269
2270 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002271
2272 return wm;
2273}
2274
2275void
2276weston_wm_destroy(struct weston_wm *wm)
2277{
2278 /* FIXME: Free windows in hash. */
2279 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002280 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002281 xcb_disconnect(wm->conn);
2282 wl_event_source_remove(wm->source);
2283 wl_list_remove(&wm->selection_listener.link);
2284 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002285 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01002286 wl_list_remove(&wm->transform_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002287 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002288
2289 free(wm);
2290}
2291
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002292static struct weston_wm_window *
2293get_wm_window(struct weston_surface *surface)
2294{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002295 struct wl_listener *listener;
2296
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002297 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002298 if (listener)
2299 return container_of(listener, struct weston_wm_window,
2300 surface_destroy_listener);
2301
2302 return NULL;
2303}
2304
2305static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002306weston_wm_window_configure(void *data)
2307{
2308 struct weston_wm_window *window = data;
2309 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002310 uint32_t values[4];
2311 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002312
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002313 weston_wm_window_get_child_position(window, &x, &y);
2314 values[0] = x;
2315 values[1] = y;
2316 values[2] = window->width;
2317 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002318 xcb_configure_window(wm->conn,
2319 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002320 XCB_CONFIG_WINDOW_X |
2321 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002322 XCB_CONFIG_WINDOW_WIDTH |
2323 XCB_CONFIG_WINDOW_HEIGHT,
2324 values);
2325
2326 weston_wm_window_get_frame_size(window, &width, &height);
2327 values[0] = width;
2328 values[1] = height;
2329 xcb_configure_window(wm->conn,
2330 window->frame_id,
2331 XCB_CONFIG_WINDOW_WIDTH |
2332 XCB_CONFIG_WINDOW_HEIGHT,
2333 values);
2334
2335 window->configure_source = NULL;
2336
2337 weston_wm_window_schedule_repaint(window);
2338}
2339
2340static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002341send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002342{
2343 struct weston_wm_window *window = get_wm_window(surface);
2344 struct weston_wm *wm = window->wm;
2345 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002346 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002347
Marek Chalupae9fe4672014-10-29 13:44:44 +01002348 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002349 hborder = 2 * t->width;
2350 vborder = t->titlebar_height + t->width;
2351 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002352 hborder = 0;
2353 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002354 }
2355
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002356 if (width > hborder)
2357 window->width = width - hborder;
2358 else
2359 window->width = 1;
2360
2361 if (height > vborder)
2362 window->height = height - vborder;
2363 else
2364 window->height = 1;
2365
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002366 if (window->frame)
2367 frame_resize_inside(window->frame, window->width, window->height);
2368
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002369 if (window->configure_source)
2370 return;
2371
2372 window->configure_source =
2373 wl_event_loop_add_idle(wm->server->loop,
2374 weston_wm_window_configure, window);
2375}
2376
2377static const struct weston_shell_client shell_client = {
2378 send_configure
2379};
2380
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002381static int
2382legacy_fullscreen(struct weston_wm *wm,
2383 struct weston_wm_window *window,
2384 struct weston_output **output_ret)
2385{
2386 struct weston_compositor *compositor = wm->server->compositor;
2387 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002388 uint32_t minmax = PMinSize | PMaxSize;
2389 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002390
2391 /* Heuristics for detecting legacy fullscreen windows... */
2392
2393 wl_list_for_each(output, &compositor->output_list, link) {
2394 if (output->x == window->x &&
2395 output->y == window->y &&
2396 output->width == window->width &&
2397 output->height == window->height &&
2398 window->override_redirect) {
2399 *output_ret = output;
2400 return 1;
2401 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002402
2403 matching_size = 0;
2404 if ((window->size_hints.flags & (USSize |PSize)) &&
2405 window->size_hints.width == output->width &&
2406 window->size_hints.height == output->height)
2407 matching_size = 1;
2408 if ((window->size_hints.flags & minmax) == minmax &&
2409 window->size_hints.min_width == output->width &&
2410 window->size_hints.min_height == output->height &&
2411 window->size_hints.max_width == output->width &&
2412 window->size_hints.max_height == output->height)
2413 matching_size = 1;
2414
2415 if (matching_size && !window->decorate &&
2416 (window->size_hints.flags & (USPosition | PPosition)) &&
2417 window->size_hints.x == output->x &&
2418 window->size_hints.y == output->y) {
2419 *output_ret = output;
2420 return 1;
2421 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002422 }
2423
2424 return 0;
2425}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002426
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002427static bool
2428weston_wm_window_type_inactive(struct weston_wm_window *window)
2429{
2430 struct weston_wm *wm = window->wm;
2431
2432 return window->type == wm->atom.net_wm_window_type_tooltip ||
2433 window->type == wm->atom.net_wm_window_type_dropdown ||
2434 window->type == wm->atom.net_wm_window_type_dnd ||
2435 window->type == wm->atom.net_wm_window_type_combo ||
Giulio Camuffo84787ea2015-04-29 19:00:48 +03002436 window->type == wm->atom.net_wm_window_type_popup ||
2437 window->type == wm->atom.net_wm_window_type_utility;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002438}
2439
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002440static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002441xserver_map_shell_surface(struct weston_wm_window *window,
2442 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002443{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002444 struct weston_wm *wm = window->wm;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002445 struct weston_shell_interface *shell_interface =
2446 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002447 struct weston_output *output;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002448 struct weston_wm_window *parent;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002449 int flags = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002450
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002451 weston_wm_window_read_properties(window);
2452
2453 /* A weston_wm_window may have many different surfaces assigned
2454 * throughout its life, so we must make sure to remove the listener
2455 * from the old surface signal list. */
2456 if (window->surface)
2457 wl_list_remove(&window->surface_destroy_listener.link);
2458
2459 window->surface = surface;
2460 window->surface_destroy_listener.notify = surface_destroy;
2461 wl_signal_add(&window->surface->destroy_signal,
2462 &window->surface_destroy_listener);
2463
2464 weston_wm_window_schedule_repaint(window);
2465
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002466 if (!shell_interface->create_shell_surface)
2467 return;
2468
Jason Ekstranda7af7042013-10-12 22:38:11 -05002469 if (!shell_interface->get_primary_view)
2470 return;
2471
Pekka Paalanen50b67472014-10-01 15:02:41 +03002472 if (window->surface->configure) {
2473 weston_log("warning, unexpected in %s: "
2474 "surface's configure hook is already set.\n",
2475 __func__);
2476 return;
2477 }
2478
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002479 window->shsurf =
2480 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002481 window->surface,
2482 &shell_client);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002483 window->view = shell_interface->get_primary_view(shell_interface->shell,
2484 window->shsurf);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002485
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002486 if (window->name)
2487 shell_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002488 if (window->pid > 0)
2489 shell_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002490
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002491 if (window->fullscreen) {
2492 window->saved_width = window->width;
2493 window->saved_height = window->height;
2494 shell_interface->set_fullscreen(window->shsurf,
2495 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2496 0, NULL);
2497 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002498 } else if (legacy_fullscreen(wm, window, &output)) {
2499 window->fullscreen = 1;
2500 shell_interface->set_fullscreen(window->shsurf,
2501 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2502 0, output);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002503 } else if (window->override_redirect) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002504 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002505 window->x,
2506 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002507 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Axel Davye4450f92014-01-12 15:06:05 +01002508 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002509 parent = window->transient_for;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002510 if (weston_wm_window_type_inactive(window))
2511 flags = WL_SHELL_SURFACE_TRANSIENT_INACTIVE;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002512 shell_interface->set_transient(window->shsurf,
2513 parent->surface,
Axel Davyfa506b62014-01-12 15:06:04 +01002514 window->x - parent->x,
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002515 window->y - parent->y, flags);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002516 } else if (weston_wm_window_is_maximized(window)) {
2517 shell_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002518 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002519 if (weston_wm_window_type_inactive(window)) {
2520 shell_interface->set_xwayland(window->shsurf,
2521 window->x,
2522 window->y,
2523 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2524 } else {
2525 shell_interface->set_toplevel(window->shsurf);
2526 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002527 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002528}