blob: c3a57c19be1095302078172301830754658a435e [file] [log] [blame]
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001/*
2 * Copyright © 2011 Intel Corporation
3 *
Bryce Harrington0a007dd2015-06-11 16:22:34 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Kristian Høgsberg380deee2012-05-21 17:12:41 -040011 *
Bryce Harrington0a007dd2015-06-11 16:22:34 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Kristian Høgsberg380deee2012-05-21 17:12:41 -040024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040027
28#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
31#include <sys/socket.h>
32#include <sys/un.h>
33#include <fcntl.h>
34#include <errno.h>
35#include <unistd.h>
36#include <signal.h>
Tiago Vignatti90fada42012-07-16 12:02:08 -040037#include <X11/Xcursor/Xcursor.h>
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -050038#include <linux/input.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040039
40#include "xwayland.h"
41
Kristian Høgsberg2ba10df2013-12-03 16:38:15 -080042#include "cairo-util.h"
43#include "compositor.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040044#include "hash.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070045#include "shared/helpers.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040046
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070047struct wm_size_hints {
Bryce Harringtone00554b2015-06-12 10:17:39 -070048 uint32_t flags;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070049 int32_t x, y;
50 int32_t width, height; /* should set so old wm's don't mess up */
51 int32_t min_width, min_height;
52 int32_t max_width, max_height;
Bryce Harringtone00554b2015-06-12 10:17:39 -070053 int32_t width_inc, height_inc;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070054 struct {
55 int32_t x;
56 int32_t y;
57 } min_aspect, max_aspect;
58 int32_t base_width, base_height;
59 int32_t win_gravity;
60};
61
62#define USPosition (1L << 0)
63#define USSize (1L << 1)
64#define PPosition (1L << 2)
65#define PSize (1L << 3)
66#define PMinSize (1L << 4)
67#define PMaxSize (1L << 5)
68#define PResizeInc (1L << 6)
69#define PAspect (1L << 7)
70#define PBaseSize (1L << 8)
71#define PWinGravity (1L << 9)
72
Kristian Høgsberg380deee2012-05-21 17:12:41 -040073struct motif_wm_hints {
74 uint32_t flags;
75 uint32_t functions;
76 uint32_t decorations;
77 int32_t input_mode;
78 uint32_t status;
79};
80
81#define MWM_HINTS_FUNCTIONS (1L << 0)
82#define MWM_HINTS_DECORATIONS (1L << 1)
83#define MWM_HINTS_INPUT_MODE (1L << 2)
84#define MWM_HINTS_STATUS (1L << 3)
85
86#define MWM_FUNC_ALL (1L << 0)
87#define MWM_FUNC_RESIZE (1L << 1)
88#define MWM_FUNC_MOVE (1L << 2)
89#define MWM_FUNC_MINIMIZE (1L << 3)
90#define MWM_FUNC_MAXIMIZE (1L << 4)
91#define MWM_FUNC_CLOSE (1L << 5)
92
93#define MWM_DECOR_ALL (1L << 0)
94#define MWM_DECOR_BORDER (1L << 1)
95#define MWM_DECOR_RESIZEH (1L << 2)
96#define MWM_DECOR_TITLE (1L << 3)
97#define MWM_DECOR_MENU (1L << 4)
98#define MWM_DECOR_MINIMIZE (1L << 5)
99#define MWM_DECOR_MAXIMIZE (1L << 6)
100
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700101#define MWM_DECOR_EVERYTHING \
102 (MWM_DECOR_BORDER | MWM_DECOR_RESIZEH | MWM_DECOR_TITLE | \
103 MWM_DECOR_MENU | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE)
104
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400105#define MWM_INPUT_MODELESS 0
106#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
107#define MWM_INPUT_SYSTEM_MODAL 2
108#define MWM_INPUT_FULL_APPLICATION_MODAL 3
109#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
110
111#define MWM_TEAROFF_WINDOW (1L<<0)
112
113#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
114#define _NET_WM_MOVERESIZE_SIZE_TOP 1
115#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
116#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
117#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
118#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
119#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
120#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
121#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
122#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
123#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
124#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
125
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400126struct weston_wm_window {
127 struct weston_wm *wm;
128 xcb_window_t id;
129 xcb_window_t frame_id;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500130 struct frame *frame;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400131 cairo_surface_t *cairo_surface;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700132 uint32_t surface_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400133 struct weston_surface *surface;
134 struct shell_surface *shsurf;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500135 struct weston_view *view;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400136 struct wl_listener surface_destroy_listener;
137 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400138 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400139 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300140 int pid;
141 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400142 char *class;
143 char *name;
144 struct weston_wm_window *transient_for;
145 uint32_t protocols;
146 xcb_atom_t type;
147 int width, height;
148 int x, y;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500149 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400150 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300151 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500152 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500153 int has_alpha;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700154 int delete_window;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200155 int maximized_vert;
156 int maximized_horz;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700157 struct wm_size_hints size_hints;
158 struct motif_wm_hints motif_hints;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700159 struct wl_list link;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400160};
161
162static struct weston_wm_window *
163get_wm_window(struct weston_surface *surface);
164
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400165static void
166weston_wm_window_schedule_repaint(struct weston_wm_window *window);
167
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700168static void
169xserver_map_shell_surface(struct weston_wm_window *window,
170 struct weston_surface *surface);
171
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400172static int __attribute__ ((format (printf, 1, 2)))
173wm_log(const char *fmt, ...)
174{
175#ifdef WM_DEBUG
176 int l;
177 va_list argp;
178
179 va_start(argp, fmt);
180 l = weston_vlog(fmt, argp);
181 va_end(argp);
182
183 return l;
184#else
185 return 0;
186#endif
187}
188
189static int __attribute__ ((format (printf, 1, 2)))
190wm_log_continue(const char *fmt, ...)
191{
192#ifdef WM_DEBUG
193 int l;
194 va_list argp;
195
196 va_start(argp, fmt);
197 l = weston_vlog_continue(fmt, argp);
198 va_end(argp);
199
200 return l;
201#else
202 return 0;
203#endif
204}
205
Derek Foreman49372142015-04-09 10:51:22 -0500206static bool __attribute__ ((warn_unused_result))
207wm_lookup_window(struct weston_wm *wm, xcb_window_t hash,
208 struct weston_wm_window **window)
209{
210 *window = hash_table_lookup(wm->window_hash, hash);
211 if (*window)
212 return true;
213 return false;
214}
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400215
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400216const char *
217get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
218{
219 xcb_get_atom_name_cookie_t cookie;
220 xcb_get_atom_name_reply_t *reply;
221 xcb_generic_error_t *e;
222 static char buffer[64];
223
224 if (atom == XCB_ATOM_NONE)
225 return "None";
226
227 cookie = xcb_get_atom_name (c, atom);
228 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500229
230 if(reply) {
231 snprintf(buffer, sizeof buffer, "%.*s",
232 xcb_get_atom_name_name_length (reply),
233 xcb_get_atom_name_name (reply));
234 } else {
235 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
236 }
237
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400238 free(reply);
239
240 return buffer;
241}
242
Tiago Vignatti90fada42012-07-16 12:02:08 -0400243static xcb_cursor_t
244xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
245{
246 xcb_connection_t *c = wm->conn;
247 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
248 xcb_screen_t *screen = s.data;
249 xcb_gcontext_t gc;
250 xcb_pixmap_t pix;
251 xcb_render_picture_t pic;
252 xcb_cursor_t cursor;
253 int stride = img->width * 4;
254
255 pix = xcb_generate_id(c);
256 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
257
258 pic = xcb_generate_id(c);
259 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
260
261 gc = xcb_generate_id(c);
262 xcb_create_gc(c, gc, pix, 0, 0);
263
264 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
265 img->width, img->height, 0, 0, 0, 32,
266 stride * img->height, (uint8_t *) img->pixels);
267 xcb_free_gc(c, gc);
268
269 cursor = xcb_generate_id(c);
270 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
271
272 xcb_render_free_picture(c, pic);
273 xcb_free_pixmap(c, pix);
274
275 return cursor;
276}
277
278static xcb_cursor_t
279xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
280{
281 /* TODO: treat animated cursors as well */
282 if (images->nimage != 1)
283 return -1;
284
285 return xcb_cursor_image_load_cursor(wm, images->images[0]);
286}
287
288static xcb_cursor_t
289xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
290{
291 xcb_cursor_t cursor;
292 XcursorImages *images;
293 char *v = NULL;
294 int size = 0;
295
296 if (!file)
297 return 0;
298
299 v = getenv ("XCURSOR_SIZE");
300 if (v)
301 size = atoi(v);
302
303 if (!size)
304 size = 32;
305
306 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300307 if (!images)
308 return -1;
309
Tiago Vignatti90fada42012-07-16 12:02:08 -0400310 cursor = xcb_cursor_images_load_cursor (wm, images);
311 XcursorImagesDestroy (images);
312
313 return cursor;
314}
315
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400316void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400317dump_property(struct weston_wm *wm,
318 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400319{
320 int32_t *incr_value;
321 const char *text_value, *name;
322 xcb_atom_t *atom_value;
323 int width, len;
324 uint32_t i;
325
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400326 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400327 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400328 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400329 return;
330 }
331
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400332 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
333 get_atom_name(wm->conn, reply->type),
334 reply->format,
335 xcb_get_property_value_length(reply),
336 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400337
338 if (reply->type == wm->atom.incr) {
339 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400340 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400341 } else if (reply->type == wm->atom.utf8_string ||
342 reply->type == wm->atom.string) {
343 text_value = xcb_get_property_value(reply);
344 if (reply->value_len > 40)
345 len = 40;
346 else
347 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400348 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400349 } else if (reply->type == XCB_ATOM_ATOM) {
350 atom_value = xcb_get_property_value(reply);
351 for (i = 0; i < reply->value_len; i++) {
352 name = get_atom_name(wm->conn, atom_value[i]);
353 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400354 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400355 width = 4;
356 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400357 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400358 }
359
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400360 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400361 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400362 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400363 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400364 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400365 }
366}
367
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200368static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400369read_and_dump_property(struct weston_wm *wm,
370 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400371{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400372 xcb_get_property_reply_t *reply;
373 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400374
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400375 cookie = xcb_get_property(wm->conn, 0, window,
376 property, XCB_ATOM_ANY, 0, 2048);
377 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400378
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400379 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400380
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400381 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400382}
383
384/* We reuse some predefined, but otherwise useles atoms */
385#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
386#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500387#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700388#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400389
390static void
391weston_wm_window_read_properties(struct weston_wm_window *window)
392{
393 struct weston_wm *wm = window->wm;
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200394 struct weston_shell_interface *shell_interface =
395 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400396
397#define F(field) offsetof(struct weston_wm_window, field)
398 const struct {
399 xcb_atom_t atom;
400 xcb_atom_t type;
401 int offset;
402 } props[] = {
403 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
404 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
405 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
406 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700407 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500408 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400409 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
410 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300411 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400412 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300413 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400414 };
415#undef F
416
417 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
418 xcb_get_property_reply_t *reply;
419 void *p;
420 uint32_t *xid;
421 xcb_atom_t *atom;
422 uint32_t i;
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200423 char name[1024];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400424
425 if (!window->properties_dirty)
426 return;
427 window->properties_dirty = 0;
428
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400429 for (i = 0; i < ARRAY_LENGTH(props); i++)
430 cookie[i] = xcb_get_property(wm->conn,
431 0, /* delete */
432 window->id,
433 props[i].atom,
434 XCB_ATOM_ANY, 0, 2048);
435
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700436 window->decorate = window->override_redirect ? 0 : MWM_DECOR_EVERYTHING;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700437 window->size_hints.flags = 0;
438 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700439 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700440
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400441 for (i = 0; i < ARRAY_LENGTH(props); i++) {
442 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
443 if (!reply)
444 /* Bad window, typically */
445 continue;
446 if (reply->type == XCB_ATOM_NONE) {
447 /* No such property */
448 free(reply);
449 continue;
450 }
451
452 p = ((char *) window + props[i].offset);
453
454 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300455 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400456 case XCB_ATOM_STRING:
457 /* FIXME: We're using this for both string and
458 utf8_string */
459 if (*(char **) p)
460 free(*(char **) p);
461
462 *(char **) p =
463 strndup(xcb_get_property_value(reply),
464 xcb_get_property_value_length(reply));
465 break;
466 case XCB_ATOM_WINDOW:
467 xid = xcb_get_property_value(reply);
Derek Foreman49372142015-04-09 10:51:22 -0500468 if (!wm_lookup_window(wm, *xid, p))
469 weston_log("XCB_ATOM_WINDOW contains window"
470 " id not found in hash table.\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400471 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300472 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400473 case XCB_ATOM_ATOM:
474 atom = xcb_get_property_value(reply);
475 *(xcb_atom_t *) p = *atom;
476 break;
477 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700478 atom = xcb_get_property_value(reply);
479 for (i = 0; i < reply->value_len; i++)
Derek Foremanb4deec62015-04-07 12:12:13 -0500480 if (atom[i] == wm->atom.wm_delete_window) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700481 window->delete_window = 1;
Derek Foremanb4deec62015-04-07 12:12:13 -0500482 break;
483 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400484 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700485 case TYPE_WM_NORMAL_HINTS:
486 memcpy(&window->size_hints,
487 xcb_get_property_value(reply),
488 sizeof window->size_hints);
489 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500490 case TYPE_NET_WM_STATE:
491 window->fullscreen = 0;
492 atom = xcb_get_property_value(reply);
Ryo Munakataf3744f52015-03-11 17:36:30 +0900493 for (i = 0; i < reply->value_len; i++) {
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500494 if (atom[i] == wm->atom.net_wm_state_fullscreen)
495 window->fullscreen = 1;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200496 if (atom[i] == wm->atom.net_wm_state_maximized_vert)
497 window->maximized_vert = 1;
498 if (atom[i] == wm->atom.net_wm_state_maximized_horz)
499 window->maximized_horz = 1;
Ryo Munakataf3744f52015-03-11 17:36:30 +0900500 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500501 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400502 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700503 memcpy(&window->motif_hints,
504 xcb_get_property_value(reply),
505 sizeof window->motif_hints);
Dima Ryazanovb0f5a252015-05-03 19:56:37 -0700506 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) {
507 if (window->motif_hints.decorations & MWM_DECOR_ALL)
508 /* MWM_DECOR_ALL means all except the other values listed. */
509 window->decorate =
510 MWM_DECOR_EVERYTHING & (~window->motif_hints.decorations);
511 else
512 window->decorate =
513 window->motif_hints.decorations;
514 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400515 break;
516 default:
517 break;
518 }
519 free(reply);
520 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200521
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200522 if (window->pid > 0) {
523 gethostname(name, sizeof(name));
524 for (i = 0; i < sizeof(name); i++) {
525 if (name[i] == '\0')
526 break;
527 }
528 if (i == sizeof(name))
529 name[0] = '\0'; /* ignore stupid hostnames */
530
531 /* this is only one heuristic to guess the PID of a client is
532 * valid, assuming it's compliant with icccm and ewmh.
533 * Non-compliants and remote applications of course fail. */
534 if (!window->machine || strcmp(window->machine, name))
535 window->pid = 0;
536 }
537
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200538 if (window->shsurf && window->name)
539 shell_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500540 if (window->frame && window->name)
541 frame_set_title(window->frame, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200542 if (window->shsurf && window->pid > 0)
543 shell_interface->set_pid(window->shsurf, window->pid);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400544}
545
546static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400547weston_wm_window_get_frame_size(struct weston_wm_window *window,
548 int *width, int *height)
549{
550 struct theme *t = window->wm->theme;
551
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500552 if (window->fullscreen) {
553 *width = window->width;
554 *height = window->height;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800555 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500556 *width = frame_width(window->frame);
557 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400558 } else {
559 *width = window->width + t->margin * 2;
560 *height = window->height + t->margin * 2;
561 }
562}
563
564static void
565weston_wm_window_get_child_position(struct weston_wm_window *window,
566 int *x, int *y)
567{
568 struct theme *t = window->wm->theme;
569
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500570 if (window->fullscreen) {
571 *x = 0;
572 *y = 0;
Dima Ryazanovcae1f0f2013-11-15 02:02:23 -0800573 } else if (window->decorate && window->frame) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500574 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400575 } else {
576 *x = t->margin;
577 *y = t->margin;
578 }
579}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400580
581static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500582weston_wm_window_send_configure_notify(struct weston_wm_window *window)
583{
584 xcb_configure_notify_event_t configure_notify;
585 struct weston_wm *wm = window->wm;
586 int x, y;
587
588 weston_wm_window_get_child_position(window, &x, &y);
589 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
590 configure_notify.pad0 = 0;
591 configure_notify.event = window->id;
592 configure_notify.window = window->id;
593 configure_notify.above_sibling = XCB_WINDOW_NONE;
594 configure_notify.x = x;
595 configure_notify.y = y;
596 configure_notify.width = window->width;
597 configure_notify.height = window->height;
598 configure_notify.border_width = 0;
599 configure_notify.override_redirect = 0;
600 configure_notify.pad1 = 0;
601
Murray Calavera883ac022015-06-06 13:02:22 +0000602 xcb_send_event(wm->conn, 0, window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500603 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
604 (char *) &configure_notify);
605}
606
607static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400608weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
609{
Murray Calavera883ac022015-06-06 13:02:22 +0000610 xcb_configure_request_event_t *configure_request =
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400611 (xcb_configure_request_event_t *) event;
612 struct weston_wm_window *window;
613 uint32_t mask, values[16];
614 int x, y, width, height, i = 0;
615
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400616 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
617 configure_request->window,
618 configure_request->x, configure_request->y,
619 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400620
Derek Foreman49372142015-04-09 10:51:22 -0500621 if (!wm_lookup_window(wm, configure_request->window, &window))
622 return;
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400623
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500624 if (window->fullscreen) {
625 weston_wm_window_send_configure_notify(window);
626 return;
627 }
628
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400629 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
630 window->width = configure_request->width;
631 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
632 window->height = configure_request->height;
633
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500634 if (window->frame)
635 frame_resize_inside(window->frame, window->width, window->height);
636
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400637 weston_wm_window_get_child_position(window, &x, &y);
638 values[i++] = x;
639 values[i++] = y;
640 values[i++] = window->width;
641 values[i++] = window->height;
642 values[i++] = 0;
643 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
644 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
645 XCB_CONFIG_WINDOW_BORDER_WIDTH;
646 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
647 values[i++] = configure_request->sibling;
648 mask |= XCB_CONFIG_WINDOW_SIBLING;
649 }
650 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
651 values[i++] = configure_request->stack_mode;
652 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
653 }
654
655 xcb_configure_window(wm->conn, window->id, mask, values);
656
657 weston_wm_window_get_frame_size(window, &width, &height);
658 values[0] = width;
659 values[1] = height;
660 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
661 xcb_configure_window(wm->conn, window->frame_id, mask, values);
662
663 weston_wm_window_schedule_repaint(window);
664}
665
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400666static int
667our_resource(struct weston_wm *wm, uint32_t id)
668{
669 const xcb_setup_t *setup;
670
671 setup = xcb_get_setup(wm->conn);
672
673 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
674}
675
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400676static void
677weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
678{
Murray Calavera883ac022015-06-06 13:02:22 +0000679 xcb_configure_notify_event_t *configure_notify =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400680 (xcb_configure_notify_event_t *) event;
681 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400682
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400683 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400684 configure_notify->window,
685 configure_notify->x, configure_notify->y,
686 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300687
Derek Foreman49372142015-04-09 10:51:22 -0500688 if (!wm_lookup_window(wm, configure_notify->window, &window))
689 return;
690
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700691 window->x = configure_notify->x;
692 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700693 if (window->override_redirect) {
694 window->width = configure_notify->width;
695 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500696 if (window->frame)
697 frame_resize_inside(window->frame,
698 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700699 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400700}
701
702static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300703weston_wm_kill_client(struct wl_listener *listener, void *data)
704{
705 struct weston_surface *surface = data;
706 struct weston_wm_window *window = get_wm_window(surface);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300707 if (!window)
708 return;
709
Giulio Camuffoa8e9b412015-01-27 19:10:37 +0200710 if (window->pid > 0)
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300711 kill(window->pid, SIGKILL);
712}
713
714static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700715weston_wm_create_surface(struct wl_listener *listener, void *data)
716{
717 struct weston_surface *surface = data;
718 struct weston_wm *wm =
719 container_of(listener,
720 struct weston_wm, create_surface_listener);
721 struct weston_wm_window *window;
722
723 if (wl_resource_get_client(surface->resource) != wm->server->client)
724 return;
725
726 wl_list_for_each(window, &wm->unpaired_window_list, link)
727 if (window->surface_id ==
728 wl_resource_get_id(surface->resource)) {
729 xserver_map_shell_surface(window, surface);
Kristian Høgsbergba83db22014-04-07 10:16:19 -0700730 window->surface_id = 0;
731 wl_list_remove(&window->link);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700732 break;
Murray Calavera883ac022015-06-06 13:02:22 +0000733 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -0700734}
735
736static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400737weston_wm_window_activate(struct wl_listener *listener, void *data)
738{
739 struct weston_surface *surface = data;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200740 struct weston_wm_window *window = NULL;
Scott Moreau85ecac02012-05-21 15:49:14 -0600741 struct weston_wm *wm =
742 container_of(listener, struct weston_wm, activate_listener);
743 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400744
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200745 if (surface) {
746 window = get_wm_window(surface);
747 }
748
Scott Moreau85ecac02012-05-21 15:49:14 -0600749 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700750 uint32_t values[1];
751
Boyan Dingb9f863c2014-08-30 10:33:23 +0800752 if (window->override_redirect)
753 return;
754
Scott Moreau85ecac02012-05-21 15:49:14 -0600755 client_message.response_type = XCB_CLIENT_MESSAGE;
756 client_message.format = 32;
757 client_message.window = window->id;
758 client_message.type = wm->atom.wm_protocols;
759 client_message.data.data32[0] = wm->atom.wm_take_focus;
760 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
761
Murray Calavera883ac022015-06-06 13:02:22 +0000762 xcb_send_event(wm->conn, 0, window->id,
Scott Moreau85ecac02012-05-21 15:49:14 -0600763 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
764 (char *) &client_message);
765
766 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
767 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700768
769 values[0] = XCB_STACK_MODE_ABOVE;
770 xcb_configure_window (wm->conn, window->frame_id,
771 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600772 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400773 xcb_set_input_focus (wm->conn,
774 XCB_INPUT_FOCUS_POINTER_ROOT,
775 XCB_NONE,
776 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600777 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400778
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500779 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800780 if (wm->focus_window->frame)
781 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400782 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500783 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400784 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500785 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800786 if (wm->focus_window->frame)
787 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400788 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500789 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400790}
791
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300792static void
793weston_wm_window_transform(struct wl_listener *listener, void *data)
794{
795 struct weston_surface *surface = data;
796 struct weston_wm_window *window = get_wm_window(surface);
797 struct weston_wm *wm =
798 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300799 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300800
801 if (!window || !wm)
802 return;
803
Jason Ekstranda7af7042013-10-12 22:38:11 -0500804 if (!window->view || !weston_view_is_mapped(window->view))
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300805 return;
806
Jason Ekstranda7af7042013-10-12 22:38:11 -0500807 if (window->x != window->view->geometry.x ||
808 window->y != window->view->geometry.y) {
809 values[0] = window->view->geometry.x;
810 values[1] = window->view->geometry.y;
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700811 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300812
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700813 xcb_configure_window(wm->conn, window->frame_id, mask, values);
814 xcb_flush(wm->conn);
815 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300816}
817
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400818#define ICCCM_WITHDRAWN_STATE 0
819#define ICCCM_NORMAL_STATE 1
820#define ICCCM_ICONIC_STATE 3
821
822static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500823weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400824{
825 struct weston_wm *wm = window->wm;
826 uint32_t property[2];
827
828 property[0] = state;
829 property[1] = XCB_WINDOW_NONE;
830
831 xcb_change_property(wm->conn,
832 XCB_PROP_MODE_REPLACE,
833 window->id,
834 wm->atom.wm_state,
835 wm->atom.wm_state,
836 32, /* format */
837 2, property);
838}
839
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400840static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500841weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
842{
843 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200844 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500845 int i;
846
847 i = 0;
848 if (window->fullscreen)
849 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200850 if (window->maximized_vert)
851 property[i++] = wm->atom.net_wm_state_maximized_vert;
852 if (window->maximized_horz)
853 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500854
855 xcb_change_property(wm->conn,
856 XCB_PROP_MODE_REPLACE,
857 window->id,
858 wm->atom.net_wm_state,
859 XCB_ATOM_ATOM,
860 32, /* format */
861 i, property);
862}
863
864static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700865weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400866{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700867 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400868 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400869 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200870 int buttons = FRAME_BUTTON_CLOSE;
871
872 if (window->decorate & MWM_DECOR_MAXIMIZE)
873 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400874
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500875 window->frame = frame_create(window->wm->theme,
876 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200877 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500878 frame_resize_inside(window->frame, window->width, window->height);
879
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400880 weston_wm_window_get_frame_size(window, &width, &height);
881 weston_wm_window_get_child_position(window, &x, &y);
882
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400883 values[0] = wm->screen->black_pixel;
884 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400885 XCB_EVENT_MASK_KEY_PRESS |
886 XCB_EVENT_MASK_KEY_RELEASE |
887 XCB_EVENT_MASK_BUTTON_PRESS |
888 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400889 XCB_EVENT_MASK_POINTER_MOTION |
890 XCB_EVENT_MASK_ENTER_WINDOW |
891 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400892 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400893 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400894 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400895
896 window->frame_id = xcb_generate_id(wm->conn);
897 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400898 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400899 window->frame_id,
900 wm->screen->root,
901 0, 0,
902 width, height,
903 0,
904 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400905 wm->visual_id,
906 XCB_CW_BORDER_PIXEL |
907 XCB_CW_EVENT_MASK |
908 XCB_CW_COLORMAP, values);
909
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400910 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
911
912 values[0] = 0;
913 xcb_configure_window(wm->conn, window->id,
914 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
915
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400916 window->cairo_surface =
917 cairo_xcb_surface_create_with_xrender_format(wm->conn,
918 wm->screen,
919 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400920 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400921 width, height);
922
923 hash_table_insert(wm->window_hash, window->frame_id, window);
924}
925
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200926/*
927 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
928 * Passing a <0 desktop value deletes the property.
929 */
930static void
931weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
Bryce Harringtone00554b2015-06-12 10:17:39 -0700932 int desktop)
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200933{
934 if (desktop >= 0) {
935 xcb_change_property(window->wm->conn,
936 XCB_PROP_MODE_REPLACE,
937 window->id,
938 window->wm->atom.net_wm_desktop,
939 XCB_ATOM_CARDINAL,
940 32, /* format */
941 1, &desktop);
942 } else {
943 xcb_delete_property(window->wm->conn,
944 window->id,
945 window->wm->atom.net_wm_desktop);
946 }
947}
948
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400949static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700950weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
951{
952 xcb_map_request_event_t *map_request =
953 (xcb_map_request_event_t *) event;
954 struct weston_wm_window *window;
955
956 if (our_resource(wm, map_request->window)) {
957 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
958 map_request->window);
959 return;
960 }
961
Derek Foreman49372142015-04-09 10:51:22 -0500962 if (!wm_lookup_window(wm, map_request->window, &window))
963 return;
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700964
965 weston_wm_window_read_properties(window);
966
967 if (window->frame_id == XCB_WINDOW_NONE)
968 weston_wm_window_create_frame(window);
969
970 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
971 window->id, window, window->frame_id);
972
973 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
974 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200975 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700976
977 xcb_map_window(wm->conn, map_request->window);
978 xcb_map_window(wm->conn, window->frame_id);
979}
980
981static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400982weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
983{
984 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
985
986 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400987 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
988 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400989 return;
990 }
991
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400992 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400993}
994
995static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400996weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
997{
998 xcb_unmap_notify_event_t *unmap_notify =
999 (xcb_unmap_notify_event_t *) event;
1000 struct weston_wm_window *window;
1001
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001002 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
1003 unmap_notify->window,
1004 unmap_notify->event,
1005 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001006
1007 if (our_resource(wm, unmap_notify->window))
1008 return;
1009
MoD31700122013-06-11 19:58:55 -05001010 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -04001011 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1012 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001013 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001014
Derek Foreman49372142015-04-09 10:51:22 -05001015 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1016 return;
1017
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001018 if (window->surface_id) {
1019 /* Make sure we're not on the unpaired surface list or we
1020 * could be assigned a surface during surface creation that
1021 * was mapped before this unmap request.
1022 */
1023 wl_list_remove(&window->link);
1024 window->surface_id = 0;
1025 }
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001026 if (wm->focus_window == window)
1027 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001028 if (window->surface)
1029 wl_list_remove(&window->surface_destroy_listener.link);
1030 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +02001031 window->shsurf = NULL;
Dima Ryazanove5a32082013-11-15 02:01:18 -08001032 window->view = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001033
1034 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1035 weston_wm_window_set_virtual_desktop(window, -1);
1036
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001037 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001038}
1039
1040static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001041weston_wm_window_draw_decoration(void *data)
1042{
1043 struct weston_wm_window *window = data;
1044 struct weston_wm *wm = window->wm;
1045 struct theme *t = wm->theme;
1046 cairo_t *cr;
1047 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001048 int32_t input_x, input_y, input_w, input_h;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07001049 struct weston_shell_interface *shell_interface =
1050 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001051 uint32_t flags = 0;
1052
1053 weston_wm_window_read_properties(window);
1054
1055 window->repaint_source = NULL;
1056
1057 weston_wm_window_get_frame_size(window, &width, &height);
1058 weston_wm_window_get_child_position(window, &x, &y);
1059
1060 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1061 cr = cairo_create(window->cairo_surface);
1062
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001063 if (window->fullscreen) {
1064 /* nothing */
1065 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001066 if (wm->focus_window == window)
1067 flags |= THEME_FRAME_ACTIVE;
1068
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001069 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001070 } else {
1071 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1072 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1073 cairo_paint(cr);
1074
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001075 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001076 }
1077
1078 cairo_destroy(cr);
1079
1080 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001081 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -05001082 if(window->has_alpha) {
1083 pixman_region32_init(&window->surface->pending.opaque);
1084 } else {
1085 /* We leave an extra pixel around the X window area to
1086 * make sure we don't sample from the undefined alpha
1087 * channel when filtering. */
Murray Calavera883ac022015-06-06 13:02:22 +00001088 pixman_region32_init_rect(&window->surface->pending.opaque,
MoD384a11a2013-06-22 11:04:21 -05001089 x - 1, y - 1,
1090 window->width + 2,
1091 window->height + 2);
1092 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001093 if (window->view)
1094 weston_view_geometry_dirty(window->view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001095
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001096 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001097
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001098 if (window->decorate && !window->fullscreen) {
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001099 frame_input_rect(window->frame, &input_x, &input_y,
1100 &input_w, &input_h);
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001101 } else {
1102 input_x = x;
1103 input_y = y;
1104 input_w = width;
1105 input_h = height;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001106 }
1107
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001108 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001109 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001110
1111 shell_interface->set_window_geometry(window->shsurf,
1112 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001113 }
1114}
1115
1116static void
1117weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1118{
1119 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001120 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001121
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001122 if (window->frame_id == XCB_WINDOW_NONE) {
1123 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001124 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001125 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -05001126 if(window->has_alpha) {
1127 pixman_region32_init(&window->surface->pending.opaque);
1128 } else {
1129 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1130 width, height);
1131 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001132 if (window->view)
1133 weston_view_geometry_dirty(window->view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001134 }
1135 return;
1136 }
1137
1138 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001139 return;
1140
1141 window->repaint_source =
1142 wl_event_loop_add_idle(wm->server->loop,
1143 weston_wm_window_draw_decoration,
1144 window);
1145}
1146
1147static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001148weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1149{
1150 xcb_property_notify_event_t *property_notify =
1151 (xcb_property_notify_event_t *) event;
1152 struct weston_wm_window *window;
1153
Derek Foreman49372142015-04-09 10:51:22 -05001154 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001155 return;
1156
1157 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001158
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001159 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001160 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001161 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001162 else
1163 read_and_dump_property(wm, property_notify->window,
1164 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001165
1166 if (property_notify->atom == wm->atom.net_wm_name ||
1167 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001168 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001169}
1170
1171static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001172weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001173 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001174{
1175 struct weston_wm_window *window;
1176 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001177 xcb_get_geometry_cookie_t geometry_cookie;
1178 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001179
Peter Huttererf3d62272013-08-08 11:57:05 +10001180 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001181 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001182 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001183 return;
1184 }
1185
MoD384a11a2013-06-22 11:04:21 -05001186 geometry_cookie = xcb_get_geometry(wm->conn, id);
1187
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001188 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1189 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1190
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001191 window->wm = wm;
1192 window->id = id;
1193 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001194 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001195 window->width = width;
1196 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001197 window->x = x;
1198 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001199
MoD384a11a2013-06-22 11:04:21 -05001200 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1201 /* technically we should use XRender and check the visual format's
1202 alpha_mask, but checking depth is simpler and works in all known cases */
1203 if(geometry_reply != NULL)
1204 window->has_alpha = geometry_reply->depth == 32;
1205 free(geometry_reply);
1206
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001207 hash_table_insert(wm->window_hash, id, window);
1208}
1209
1210static void
1211weston_wm_window_destroy(struct weston_wm_window *window)
1212{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001213 struct weston_wm *wm = window->wm;
1214
1215 if (window->repaint_source)
1216 wl_event_source_remove(window->repaint_source);
1217 if (window->cairo_surface)
1218 cairo_surface_destroy(window->cairo_surface);
1219
1220 if (window->frame_id) {
1221 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1222 xcb_destroy_window(wm->conn, window->frame_id);
1223 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001224 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001225 hash_table_remove(wm->window_hash, window->frame_id);
1226 window->frame_id = XCB_WINDOW_NONE;
1227 }
1228
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001229 if (window->surface_id)
1230 wl_list_remove(&window->link);
1231
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001232 if (window->surface)
1233 wl_list_remove(&window->surface_destroy_listener.link);
1234
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001235 hash_table_remove(window->wm->window_hash, window->id);
1236 free(window);
1237}
1238
1239static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001240weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1241{
1242 xcb_create_notify_event_t *create_notify =
1243 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001244
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001245 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1246 create_notify->window,
1247 create_notify->width, create_notify->height,
1248 create_notify->override_redirect ? ", override" : "",
1249 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001250
1251 if (our_resource(wm, create_notify->window))
1252 return;
1253
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001254 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001255 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001256 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001257 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001258}
1259
1260static void
1261weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1262{
1263 xcb_destroy_notify_event_t *destroy_notify =
1264 (xcb_destroy_notify_event_t *) event;
1265 struct weston_wm_window *window;
1266
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001267 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1268 destroy_notify->window,
1269 destroy_notify->event,
1270 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001271
1272 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001273 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001274
Derek Foreman49372142015-04-09 10:51:22 -05001275 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1276 return;
1277
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001278 weston_wm_window_destroy(window);
1279}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001280
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001281static void
1282weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1283{
1284 xcb_reparent_notify_event_t *reparent_notify =
1285 (xcb_reparent_notify_event_t *) event;
1286 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001287
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001288 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1289 reparent_notify->window,
1290 reparent_notify->parent,
1291 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001292
1293 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001294 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001295 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001296 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001297 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001298 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1299 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001300 weston_wm_window_destroy(window);
1301 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001302}
1303
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001304struct weston_seat *
1305weston_wm_pick_seat(struct weston_wm *wm)
1306{
1307 return container_of(wm->server->compositor->seat_list.next,
1308 struct weston_seat, link);
1309}
1310
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001311static struct weston_seat *
1312weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1313{
1314 struct weston_wm *wm = window->wm;
1315 struct weston_seat *seat, *s;
1316
1317 seat = NULL;
1318 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Giulio Camuffoad7305a2014-10-04 13:58:33 +03001319 if (s->pointer != NULL && s->pointer->focus &&
1320 s->pointer->focus->surface == window->surface &&
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001321 s->pointer->button_count > 0 &&
1322 (seat == NULL ||
1323 s->pointer->grab_serial -
1324 seat->pointer->grab_serial < (1 << 30)))
1325 seat = s;
1326 }
1327
1328 return seat;
1329}
1330
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001331static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001332weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1333 xcb_client_message_event_t *client_message)
1334{
1335 static const int map[] = {
1336 THEME_LOCATION_RESIZING_TOP_LEFT,
1337 THEME_LOCATION_RESIZING_TOP,
1338 THEME_LOCATION_RESIZING_TOP_RIGHT,
1339 THEME_LOCATION_RESIZING_RIGHT,
1340 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1341 THEME_LOCATION_RESIZING_BOTTOM,
1342 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1343 THEME_LOCATION_RESIZING_LEFT
1344 };
1345
1346 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001347 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001348 int detail;
1349 struct weston_shell_interface *shell_interface =
1350 &wm->server->compositor->shell_interface;
1351
Boyan Dingc06a1802014-07-06 11:44:58 +08001352 if (seat == NULL || seat->pointer->button_count != 1
Giulio Camuffoad7305a2014-10-04 13:58:33 +03001353 || !seat->pointer->focus
1354 || seat->pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001355 return;
1356
1357 detail = client_message->data.data32[2];
1358 switch (detail) {
1359 case _NET_WM_MOVERESIZE_MOVE:
1360 shell_interface->move(window->shsurf, seat);
1361 break;
1362 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1363 case _NET_WM_MOVERESIZE_SIZE_TOP:
1364 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1365 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1366 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1367 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1368 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1369 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1370 shell_interface->resize(window->shsurf, seat, map[detail]);
1371 break;
1372 case _NET_WM_MOVERESIZE_CANCEL:
1373 break;
1374 }
1375}
1376
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001377#define _NET_WM_STATE_REMOVE 0
1378#define _NET_WM_STATE_ADD 1
1379#define _NET_WM_STATE_TOGGLE 2
1380
1381static int
1382update_state(int action, int *state)
1383{
1384 int new_state, changed;
1385
1386 switch (action) {
1387 case _NET_WM_STATE_REMOVE:
1388 new_state = 0;
1389 break;
1390 case _NET_WM_STATE_ADD:
1391 new_state = 1;
1392 break;
1393 case _NET_WM_STATE_TOGGLE:
1394 new_state = !*state;
1395 break;
1396 default:
1397 return 0;
1398 }
1399
1400 changed = (*state != new_state);
1401 *state = new_state;
1402
1403 return changed;
1404}
1405
1406static void
1407weston_wm_window_configure(void *data);
1408
1409static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001410weston_wm_window_set_toplevel(struct weston_wm_window *window)
1411{
1412 struct weston_shell_interface *shell_interface =
1413 &window->wm->server->compositor->shell_interface;
1414
1415 shell_interface->set_toplevel(window->shsurf);
1416 window->width = window->saved_width;
1417 window->height = window->saved_height;
1418 if (window->frame)
1419 frame_resize_inside(window->frame,
1420 window->width,
1421 window->height);
1422 weston_wm_window_configure(window);
1423}
1424
1425static inline bool
1426weston_wm_window_is_maximized(struct weston_wm_window *window)
1427{
1428 return window->maximized_horz && window->maximized_vert;
1429}
1430
1431static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001432weston_wm_window_handle_state(struct weston_wm_window *window,
1433 xcb_client_message_event_t *client_message)
1434{
1435 struct weston_wm *wm = window->wm;
1436 struct weston_shell_interface *shell_interface =
1437 &wm->server->compositor->shell_interface;
1438 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001439 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001440
1441 action = client_message->data.data32[0];
1442 property = client_message->data.data32[1];
1443
1444 if (property == wm->atom.net_wm_state_fullscreen &&
1445 update_state(action, &window->fullscreen)) {
1446 weston_wm_window_set_net_wm_state(window);
1447 if (window->fullscreen) {
1448 window->saved_width = window->width;
1449 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001450
1451 if (window->shsurf)
1452 shell_interface->set_fullscreen(window->shsurf,
1453 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1454 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001455 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001456 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001457 weston_wm_window_set_toplevel(window);
1458 }
1459 } else {
1460 if (property == wm->atom.net_wm_state_maximized_vert &&
1461 update_state(action, &window->maximized_vert))
1462 weston_wm_window_set_net_wm_state(window);
1463 if (property == wm->atom.net_wm_state_maximized_horz &&
1464 update_state(action, &window->maximized_horz))
1465 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001466
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001467 if (maximized != weston_wm_window_is_maximized(window)) {
1468 if (weston_wm_window_is_maximized(window)) {
1469 window->saved_width = window->width;
1470 window->saved_height = window->height;
1471
1472 if (window->shsurf)
1473 shell_interface->set_maximized(window->shsurf);
1474 } else if (window->shsurf) {
1475 weston_wm_window_set_toplevel(window);
1476 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001477 }
1478 }
1479}
1480
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001481static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001482surface_destroy(struct wl_listener *listener, void *data)
1483{
1484 struct weston_wm_window *window =
1485 container_of(listener,
1486 struct weston_wm_window, surface_destroy_listener);
1487
1488 wm_log("surface for xid %d destroyed\n", window->id);
1489
1490 /* This should have been freed by the shell.
1491 * Don't try to use it later. */
1492 window->shsurf = NULL;
1493 window->surface = NULL;
1494 window->view = NULL;
1495}
1496
1497static void
1498weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1499 xcb_client_message_event_t *client_message)
1500{
1501 struct weston_wm *wm = window->wm;
1502 struct wl_resource *resource;
1503
1504 if (window->surface_id != 0) {
1505 wm_log("already have surface id for window %d\n", window->id);
1506 return;
1507 }
1508
1509 /* Xwayland will send the wayland requests to create the
1510 * wl_surface before sending this client message. Even so, we
1511 * can end up handling the X event before the wayland requests
1512 * and thus when we try to look up the surface ID, the surface
1513 * hasn't been created yet. In that case put the window on
1514 * the unpaired window list and continue when the surface gets
1515 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001516 uint32_t id = client_message->data.data32[0];
1517 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001518 if (resource) {
1519 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001520 xserver_map_shell_surface(window,
1521 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001522 }
1523 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001524 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001525 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001526 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001527}
1528
1529static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001530weston_wm_handle_client_message(struct weston_wm *wm,
1531 xcb_generic_event_t *event)
1532{
1533 xcb_client_message_event_t *client_message =
1534 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001535 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001536
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001537 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1538 get_atom_name(wm->conn, client_message->type),
1539 client_message->data.data32[0],
1540 client_message->data.data32[1],
1541 client_message->data.data32[2],
1542 client_message->data.data32[3],
1543 client_message->data.data32[4],
1544 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001545
Jason Ekstrand0250a742014-07-24 13:17:47 -07001546 /* The window may get created and destroyed before we actually
1547 * handle the message. If it doesn't exist, bail.
1548 */
Derek Foreman49372142015-04-09 10:51:22 -05001549 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001550 return;
1551
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001552 if (client_message->type == wm->atom.net_wm_moveresize)
1553 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001554 else if (client_message->type == wm->atom.net_wm_state)
1555 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001556 else if (client_message->type == wm->atom.wl_surface_id)
1557 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001558}
1559
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001560enum cursor_type {
1561 XWM_CURSOR_TOP,
1562 XWM_CURSOR_BOTTOM,
1563 XWM_CURSOR_LEFT,
1564 XWM_CURSOR_RIGHT,
1565 XWM_CURSOR_TOP_LEFT,
1566 XWM_CURSOR_TOP_RIGHT,
1567 XWM_CURSOR_BOTTOM_LEFT,
1568 XWM_CURSOR_BOTTOM_RIGHT,
1569 XWM_CURSOR_LEFT_PTR,
1570};
1571
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001572/*
1573 * The following correspondences between file names and cursors was copied
1574 * from: https://bugs.kde.org/attachment.cgi?id=67313
1575 */
1576
1577static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001578 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001579 "sw-resize",
1580 "size_bdiag"
1581};
1582
1583static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001584 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001585 "se-resize",
1586 "size_fdiag"
1587};
1588
1589static const char *bottom_sides[] = {
1590 "bottom_side",
1591 "s-resize",
1592 "size_ver"
1593};
1594
1595static const char *left_ptrs[] = {
1596 "left_ptr",
1597 "default",
1598 "top_left_arrow",
1599 "left-arrow"
1600};
1601
1602static const char *left_sides[] = {
1603 "left_side",
1604 "w-resize",
1605 "size_hor"
1606};
1607
1608static const char *right_sides[] = {
1609 "right_side",
1610 "e-resize",
1611 "size_hor"
1612};
1613
1614static const char *top_left_corners[] = {
1615 "top_left_corner",
1616 "nw-resize",
1617 "size_fdiag"
1618};
1619
1620static const char *top_right_corners[] = {
1621 "top_right_corner",
1622 "ne-resize",
1623 "size_bdiag"
1624};
1625
1626static const char *top_sides[] = {
1627 "top_side",
1628 "n-resize",
1629 "size_ver"
1630};
1631
1632struct cursor_alternatives {
1633 const char **names;
1634 size_t count;
1635};
1636
1637static const struct cursor_alternatives cursors[] = {
1638 {top_sides, ARRAY_LENGTH(top_sides)},
1639 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1640 {left_sides, ARRAY_LENGTH(left_sides)},
1641 {right_sides, ARRAY_LENGTH(right_sides)},
1642 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1643 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1644 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1645 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1646 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001647};
1648
1649static void
1650weston_wm_create_cursors(struct weston_wm *wm)
1651{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001652 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001653 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001654 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001655
1656 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1657 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001658 for (j = 0; j < cursors[i].count; j++) {
1659 name = cursors[i].names[j];
1660 wm->cursors[i] =
1661 xcb_cursor_library_load_cursor(wm, name);
1662 if (wm->cursors[i] != (xcb_cursor_t)-1)
1663 break;
1664 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001665 }
1666
1667 wm->last_cursor = -1;
1668}
1669
1670static void
1671weston_wm_destroy_cursors(struct weston_wm *wm)
1672{
1673 uint8_t i;
1674
1675 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1676 xcb_free_cursor(wm->conn, wm->cursors[i]);
1677
1678 free(wm->cursors);
1679}
1680
1681static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001682get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001683{
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001684 // int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001685
1686 switch (location) {
1687 case THEME_LOCATION_RESIZING_TOP:
1688 return XWM_CURSOR_TOP;
1689 case THEME_LOCATION_RESIZING_BOTTOM:
1690 return XWM_CURSOR_BOTTOM;
1691 case THEME_LOCATION_RESIZING_LEFT:
1692 return XWM_CURSOR_LEFT;
1693 case THEME_LOCATION_RESIZING_RIGHT:
1694 return XWM_CURSOR_RIGHT;
1695 case THEME_LOCATION_RESIZING_TOP_LEFT:
1696 return XWM_CURSOR_TOP_LEFT;
1697 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1698 return XWM_CURSOR_TOP_RIGHT;
1699 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1700 return XWM_CURSOR_BOTTOM_LEFT;
1701 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1702 return XWM_CURSOR_BOTTOM_RIGHT;
1703 case THEME_LOCATION_EXTERIOR:
1704 case THEME_LOCATION_TITLEBAR:
1705 default:
1706 return XWM_CURSOR_LEFT_PTR;
1707 }
1708}
1709
1710static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001711weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1712 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001713{
1714 uint32_t cursor_value_list;
1715
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001716 if (wm->last_cursor == cursor)
1717 return;
1718
1719 wm->last_cursor = cursor;
1720
1721 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001722 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001723 XCB_CW_CURSOR, &cursor_value_list);
1724 xcb_flush(wm->conn);
1725}
1726
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001727static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001728weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001729{
1730 xcb_client_message_event_t client_message;
1731
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001732 if (window->delete_window) {
1733 client_message.response_type = XCB_CLIENT_MESSAGE;
1734 client_message.format = 32;
1735 client_message.window = window->id;
1736 client_message.type = window->wm->atom.wm_protocols;
1737 client_message.data.data32[0] =
1738 window->wm->atom.wm_delete_window;
1739 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001740
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001741 xcb_send_event(window->wm->conn, 0, window->id,
1742 XCB_EVENT_MASK_NO_EVENT,
1743 (char *) &client_message);
1744 } else {
1745 xcb_kill_client(window->wm->conn, window->id);
1746 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001747}
1748
1749static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001750weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1751{
1752 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1753 struct weston_shell_interface *shell_interface =
1754 &wm->server->compositor->shell_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001755 struct weston_seat *seat;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001756 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001757 enum theme_location location;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001758 enum frame_button_state button_state;
1759 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001760
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001761 wm_log("XCB_BUTTON_%s (detail %d)\n",
1762 button->response_type == XCB_BUTTON_PRESS ?
1763 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001764
Derek Foreman49372142015-04-09 10:51:22 -05001765 if (!wm_lookup_window(wm, button->event, &window) ||
1766 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001767 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001768
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001769 if (button->detail != 1 && button->detail != 2)
1770 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001771
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001772 seat = weston_wm_pick_seat_for_window(window);
1773
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001774 button_state = button->response_type == XCB_BUTTON_PRESS ?
1775 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1776 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1777
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001778 /* Make sure we're looking at the right location. The frame
1779 * could have received a motion event from a pointer from a
1780 * different wl_seat, but under X it looks like our core
1781 * pointer moved. Move the frame pointer to the button press
1782 * location before deciding what to do. */
1783 location = frame_pointer_motion(window->frame, NULL,
1784 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001785 location = frame_pointer_button(window->frame, NULL,
1786 button_id, button_state);
1787 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1788 weston_wm_window_schedule_repaint(window);
1789
1790 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Boyan Ding3c6d20c2014-09-03 17:25:30 +08001791 if (seat != NULL)
1792 shell_interface->move(window->shsurf, seat);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001793 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1794 }
1795
1796 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Boyan Ding3c6d20c2014-09-03 17:25:30 +08001797 if (seat != NULL)
1798 shell_interface->resize(window->shsurf, seat, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001799 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1800 }
1801
1802 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001803 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001804 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001805 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001806
1807 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1808 window->maximized_horz = !window->maximized_horz;
1809 window->maximized_vert = !window->maximized_vert;
1810 if (weston_wm_window_is_maximized(window)) {
1811 window->saved_width = window->width;
1812 window->saved_height = window->height;
1813 shell_interface->set_maximized(window->shsurf);
1814 } else {
1815 weston_wm_window_set_toplevel(window);
1816 }
1817 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1818 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001819}
1820
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001821static void
1822weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1823{
1824 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1825 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001826 enum theme_location location;
1827 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001828
Derek Foreman49372142015-04-09 10:51:22 -05001829 if (!wm_lookup_window(wm, motion->event, &window) ||
1830 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001831 return;
1832
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001833 location = frame_pointer_motion(window->frame, NULL,
1834 motion->event_x, motion->event_y);
1835 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1836 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001837
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001838 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001839 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001840}
1841
1842static void
1843weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1844{
1845 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1846 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001847 enum theme_location location;
1848 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001849
Derek Foreman49372142015-04-09 10:51:22 -05001850 if (!wm_lookup_window(wm, enter->event, &window) ||
1851 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001852 return;
1853
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001854 location = frame_pointer_enter(window->frame, NULL,
1855 enter->event_x, enter->event_y);
1856 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1857 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001858
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001859 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001860 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001861}
1862
1863static void
1864weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1865{
1866 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1867 struct weston_wm_window *window;
1868
Derek Foreman49372142015-04-09 10:51:22 -05001869 if (!wm_lookup_window(wm, leave->event, &window) ||
1870 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001871 return;
1872
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001873 frame_pointer_leave(window->frame, NULL);
1874 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1875 weston_wm_window_schedule_repaint(window);
1876
Tiago Vignattic1903232012-07-16 12:15:37 -04001877 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001878}
1879
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001880static int
1881weston_wm_handle_event(int fd, uint32_t mask, void *data)
1882{
1883 struct weston_wm *wm = data;
1884 xcb_generic_event_t *event;
1885 int count = 0;
1886
1887 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1888 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001889 free(event);
1890 count++;
1891 continue;
1892 }
1893
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001894 if (weston_wm_handle_dnd_event(wm, event)) {
1895 free(event);
1896 count++;
1897 continue;
1898 }
1899
MoD31700122013-06-11 19:58:55 -05001900 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001901 case XCB_BUTTON_PRESS:
1902 case XCB_BUTTON_RELEASE:
1903 weston_wm_handle_button(wm, event);
1904 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001905 case XCB_ENTER_NOTIFY:
1906 weston_wm_handle_enter(wm, event);
1907 break;
1908 case XCB_LEAVE_NOTIFY:
1909 weston_wm_handle_leave(wm, event);
1910 break;
1911 case XCB_MOTION_NOTIFY:
1912 weston_wm_handle_motion(wm, event);
1913 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001914 case XCB_CREATE_NOTIFY:
1915 weston_wm_handle_create_notify(wm, event);
1916 break;
1917 case XCB_MAP_REQUEST:
1918 weston_wm_handle_map_request(wm, event);
1919 break;
1920 case XCB_MAP_NOTIFY:
1921 weston_wm_handle_map_notify(wm, event);
1922 break;
1923 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001924 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001925 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001926 case XCB_REPARENT_NOTIFY:
1927 weston_wm_handle_reparent_notify(wm, event);
1928 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001929 case XCB_CONFIGURE_REQUEST:
1930 weston_wm_handle_configure_request(wm, event);
1931 break;
1932 case XCB_CONFIGURE_NOTIFY:
1933 weston_wm_handle_configure_notify(wm, event);
1934 break;
1935 case XCB_DESTROY_NOTIFY:
1936 weston_wm_handle_destroy_notify(wm, event);
1937 break;
1938 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001939 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001940 break;
1941 case XCB_PROPERTY_NOTIFY:
1942 weston_wm_handle_property_notify(wm, event);
1943 break;
1944 case XCB_CLIENT_MESSAGE:
1945 weston_wm_handle_client_message(wm, event);
1946 break;
1947 }
1948
1949 free(event);
1950 count++;
1951 }
1952
1953 xcb_flush(wm->conn);
1954
1955 return count;
1956}
1957
1958static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001959weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1960{
1961 xcb_depth_iterator_t d_iter;
1962 xcb_visualtype_iterator_t vt_iter;
1963 xcb_visualtype_t *visualtype;
1964
1965 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1966 visualtype = NULL;
1967 while (d_iter.rem > 0) {
1968 if (d_iter.data->depth == 32) {
1969 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1970 visualtype = vt_iter.data;
1971 break;
1972 }
1973
1974 xcb_depth_next(&d_iter);
1975 }
1976
1977 if (visualtype == NULL) {
1978 weston_log("no 32 bit visualtype\n");
1979 return;
1980 }
1981
1982 wm->visual_id = visualtype->visual_id;
1983 wm->colormap = xcb_generate_id(wm->conn);
1984 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1985 wm->colormap, wm->screen->root, wm->visual_id);
1986}
1987
1988static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001989weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001990{
1991
1992#define F(field) offsetof(struct weston_wm, field)
1993
1994 static const struct { const char *name; int offset; } atoms[] = {
1995 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001996 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001997 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1998 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001999 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002000 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002001 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002002 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002003 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002004 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002005 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2006 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002007 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2008 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002009 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2010 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2011 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02002012 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002013 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2014
2015 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2016 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2017 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2018 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2019 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2020 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2021 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002022 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2023 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002024 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2025 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2026 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2027 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2028 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2029
2030 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2031 { "_NET_SUPPORTING_WM_CHECK",
2032 F(atom.net_supporting_wm_check) },
2033 { "_NET_SUPPORTED", F(atom.net_supported) },
2034 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2035 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002036 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002037 { "TARGETS", F(atom.targets) },
2038 { "UTF8_STRING", F(atom.utf8_string) },
2039 { "_WL_SELECTION", F(atom.wl_selection) },
2040 { "INCR", F(atom.incr) },
2041 { "TIMESTAMP", F(atom.timestamp) },
2042 { "MULTIPLE", F(atom.multiple) },
2043 { "UTF8_STRING" , F(atom.utf8_string) },
2044 { "COMPOUND_TEXT", F(atom.compound_text) },
2045 { "TEXT", F(atom.text) },
2046 { "STRING", F(atom.string) },
2047 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2048 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002049 { "XdndSelection", F(atom.xdnd_selection) },
2050 { "XdndAware", F(atom.xdnd_aware) },
2051 { "XdndEnter", F(atom.xdnd_enter) },
2052 { "XdndLeave", F(atom.xdnd_leave) },
2053 { "XdndDrop", F(atom.xdnd_drop) },
2054 { "XdndStatus", F(atom.xdnd_status) },
2055 { "XdndFinished", F(atom.xdnd_finished) },
2056 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002057 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2058 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002059 };
2060#undef F
2061
2062 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2063 xcb_xfixes_query_version_reply_t *xfixes_reply;
2064 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2065 xcb_intern_atom_reply_t *reply;
2066 xcb_render_query_pict_formats_reply_t *formats_reply;
2067 xcb_render_query_pict_formats_cookie_t formats_cookie;
2068 xcb_render_pictforminfo_t *formats;
2069 uint32_t i;
2070
2071 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002072 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002073
2074 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2075
2076 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2077 cookies[i] = xcb_intern_atom (wm->conn, 0,
2078 strlen(atoms[i].name),
2079 atoms[i].name);
2080
2081 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2082 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2083 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2084 free(reply);
2085 }
2086
2087 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2088 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002089 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002090
2091 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2092 XCB_XFIXES_MAJOR_VERSION,
2093 XCB_XFIXES_MINOR_VERSION);
2094 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2095 xfixes_cookie, NULL);
2096
Martin Minarik6d118362012-06-07 18:01:59 +02002097 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002098 xfixes_reply->major_version, xfixes_reply->minor_version);
2099
2100 free(xfixes_reply);
2101
2102 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2103 formats_cookie, 0);
2104 if (formats_reply == NULL)
2105 return;
2106
2107 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002108 for (i = 0; i < formats_reply->num_formats; i++) {
2109 if (formats[i].direct.red_mask != 0xff &&
2110 formats[i].direct.red_shift != 16)
2111 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002112 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2113 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002114 wm->format_rgb = formats[i];
2115 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2116 formats[i].depth == 32 &&
2117 formats[i].direct.alpha_mask == 0xff &&
2118 formats[i].direct.alpha_shift == 24)
2119 wm->format_rgba = formats[i];
2120 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002121
2122 free(formats_reply);
2123}
2124
2125static void
2126weston_wm_create_wm_window(struct weston_wm *wm)
2127{
2128 static const char name[] = "Weston WM";
2129
2130 wm->wm_window = xcb_generate_id(wm->conn);
2131 xcb_create_window(wm->conn,
2132 XCB_COPY_FROM_PARENT,
2133 wm->wm_window,
2134 wm->screen->root,
2135 0, 0,
2136 10, 10,
2137 0,
2138 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2139 wm->screen->root_visual,
2140 0, NULL);
2141
2142 xcb_change_property(wm->conn,
2143 XCB_PROP_MODE_REPLACE,
2144 wm->wm_window,
2145 wm->atom.net_supporting_wm_check,
2146 XCB_ATOM_WINDOW,
2147 32, /* format */
2148 1, &wm->wm_window);
2149
2150 xcb_change_property(wm->conn,
2151 XCB_PROP_MODE_REPLACE,
2152 wm->wm_window,
2153 wm->atom.net_wm_name,
2154 wm->atom.utf8_string,
2155 8, /* format */
2156 strlen(name), name);
2157
2158 xcb_change_property(wm->conn,
2159 XCB_PROP_MODE_REPLACE,
2160 wm->screen->root,
2161 wm->atom.net_supporting_wm_check,
2162 XCB_ATOM_WINDOW,
2163 32, /* format */
2164 1, &wm->wm_window);
2165
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002166 /* Claim the WM_S0 selection even though we don't suport
2167 * the --replace functionality. */
2168 xcb_set_selection_owner(wm->conn,
2169 wm->wm_window,
2170 wm->atom.wm_s0,
2171 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002172
2173 xcb_set_selection_owner(wm->conn,
2174 wm->wm_window,
2175 wm->atom.net_wm_cm_s0,
2176 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002177}
2178
2179struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002180weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002181{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002182 struct weston_wm *wm;
2183 struct wl_event_loop *loop;
2184 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002185 uint32_t values[1];
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002186 xcb_atom_t supported[5];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002187
Peter Huttererf3d62272013-08-08 11:57:05 +10002188 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002189 if (wm == NULL)
2190 return NULL;
2191
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002192 wm->server = wxs;
2193 wm->window_hash = hash_table_create();
2194 if (wm->window_hash == NULL) {
2195 free(wm);
2196 return NULL;
2197 }
2198
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002199 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002200 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002201 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002202 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002203 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002204 hash_table_destroy(wm->window_hash);
2205 free(wm);
2206 return NULL;
2207 }
2208
2209 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2210 wm->screen = s.data;
2211
2212 loop = wl_display_get_event_loop(wxs->wl_display);
2213 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002214 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002215 WL_EVENT_READABLE,
2216 weston_wm_handle_event, wm);
2217 wl_event_source_check(wm->source);
2218
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002219 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002220 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002221
2222 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002223 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2224 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2225 XCB_EVENT_MASK_PROPERTY_CHANGE;
2226 xcb_change_window_attributes(wm->conn, wm->screen->root,
2227 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002228
2229 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2230 XCB_COMPOSITE_REDIRECT_MANUAL);
2231
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002232 wm->theme = theme_create();
2233
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002234 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002235 supported[1] = wm->atom.net_wm_state;
2236 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002237 supported[3] = wm->atom.net_wm_state_maximized_vert;
2238 supported[4] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002239 xcb_change_property(wm->conn,
2240 XCB_PROP_MODE_REPLACE,
2241 wm->screen->root,
2242 wm->atom.net_supported,
2243 XCB_ATOM_ATOM,
2244 32, /* format */
2245 ARRAY_LENGTH(supported), supported);
2246
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002247 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002248
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002249 weston_wm_dnd_init(wm);
2250
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002251 xcb_flush(wm->conn);
2252
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002253 wm->create_surface_listener.notify = weston_wm_create_surface;
2254 wl_signal_add(&wxs->compositor->create_surface_signal,
2255 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002256 wm->activate_listener.notify = weston_wm_window_activate;
2257 wl_signal_add(&wxs->compositor->activate_signal,
2258 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002259 wm->transform_listener.notify = weston_wm_window_transform;
2260 wl_signal_add(&wxs->compositor->transform_signal,
2261 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002262 wm->kill_listener.notify = weston_wm_kill_client;
2263 wl_signal_add(&wxs->compositor->kill_signal,
2264 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002265 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002266
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002267 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002268 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002269
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002270 /* Create wm window and take WM_S0 selection last, which
2271 * signals to Xwayland that we're done with setup. */
2272 weston_wm_create_wm_window(wm);
2273
2274 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002275
2276 return wm;
2277}
2278
2279void
2280weston_wm_destroy(struct weston_wm *wm)
2281{
2282 /* FIXME: Free windows in hash. */
2283 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002284 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002285 xcb_disconnect(wm->conn);
2286 wl_event_source_remove(wm->source);
2287 wl_list_remove(&wm->selection_listener.link);
2288 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002289 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01002290 wl_list_remove(&wm->transform_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002291 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002292
2293 free(wm);
2294}
2295
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002296static struct weston_wm_window *
2297get_wm_window(struct weston_surface *surface)
2298{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002299 struct wl_listener *listener;
2300
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002301 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002302 if (listener)
2303 return container_of(listener, struct weston_wm_window,
2304 surface_destroy_listener);
2305
2306 return NULL;
2307}
2308
2309static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002310weston_wm_window_configure(void *data)
2311{
2312 struct weston_wm_window *window = data;
2313 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002314 uint32_t values[4];
2315 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002316
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002317 weston_wm_window_get_child_position(window, &x, &y);
2318 values[0] = x;
2319 values[1] = y;
2320 values[2] = window->width;
2321 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002322 xcb_configure_window(wm->conn,
2323 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002324 XCB_CONFIG_WINDOW_X |
2325 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002326 XCB_CONFIG_WINDOW_WIDTH |
2327 XCB_CONFIG_WINDOW_HEIGHT,
2328 values);
2329
2330 weston_wm_window_get_frame_size(window, &width, &height);
2331 values[0] = width;
2332 values[1] = height;
2333 xcb_configure_window(wm->conn,
2334 window->frame_id,
2335 XCB_CONFIG_WINDOW_WIDTH |
2336 XCB_CONFIG_WINDOW_HEIGHT,
2337 values);
2338
2339 window->configure_source = NULL;
2340
2341 weston_wm_window_schedule_repaint(window);
2342}
2343
2344static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002345send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002346{
2347 struct weston_wm_window *window = get_wm_window(surface);
2348 struct weston_wm *wm = window->wm;
2349 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002350 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002351
Marek Chalupae9fe4672014-10-29 13:44:44 +01002352 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002353 hborder = 2 * t->width;
2354 vborder = t->titlebar_height + t->width;
2355 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002356 hborder = 0;
2357 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002358 }
2359
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002360 if (width > hborder)
2361 window->width = width - hborder;
2362 else
2363 window->width = 1;
2364
2365 if (height > vborder)
2366 window->height = height - vborder;
2367 else
2368 window->height = 1;
2369
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002370 if (window->frame)
2371 frame_resize_inside(window->frame, window->width, window->height);
2372
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002373 if (window->configure_source)
2374 return;
2375
2376 window->configure_source =
2377 wl_event_loop_add_idle(wm->server->loop,
2378 weston_wm_window_configure, window);
2379}
2380
2381static const struct weston_shell_client shell_client = {
2382 send_configure
2383};
2384
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002385static int
2386legacy_fullscreen(struct weston_wm *wm,
2387 struct weston_wm_window *window,
2388 struct weston_output **output_ret)
2389{
2390 struct weston_compositor *compositor = wm->server->compositor;
2391 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002392 uint32_t minmax = PMinSize | PMaxSize;
2393 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002394
2395 /* Heuristics for detecting legacy fullscreen windows... */
2396
2397 wl_list_for_each(output, &compositor->output_list, link) {
2398 if (output->x == window->x &&
2399 output->y == window->y &&
2400 output->width == window->width &&
2401 output->height == window->height &&
2402 window->override_redirect) {
2403 *output_ret = output;
2404 return 1;
2405 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002406
2407 matching_size = 0;
2408 if ((window->size_hints.flags & (USSize |PSize)) &&
2409 window->size_hints.width == output->width &&
2410 window->size_hints.height == output->height)
2411 matching_size = 1;
2412 if ((window->size_hints.flags & minmax) == minmax &&
2413 window->size_hints.min_width == output->width &&
2414 window->size_hints.min_height == output->height &&
2415 window->size_hints.max_width == output->width &&
2416 window->size_hints.max_height == output->height)
2417 matching_size = 1;
2418
2419 if (matching_size && !window->decorate &&
2420 (window->size_hints.flags & (USPosition | PPosition)) &&
2421 window->size_hints.x == output->x &&
2422 window->size_hints.y == output->y) {
2423 *output_ret = output;
2424 return 1;
2425 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002426 }
2427
2428 return 0;
2429}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002430
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002431static bool
2432weston_wm_window_type_inactive(struct weston_wm_window *window)
2433{
2434 struct weston_wm *wm = window->wm;
2435
2436 return window->type == wm->atom.net_wm_window_type_tooltip ||
2437 window->type == wm->atom.net_wm_window_type_dropdown ||
2438 window->type == wm->atom.net_wm_window_type_dnd ||
2439 window->type == wm->atom.net_wm_window_type_combo ||
Giulio Camuffo84787ea2015-04-29 19:00:48 +03002440 window->type == wm->atom.net_wm_window_type_popup ||
2441 window->type == wm->atom.net_wm_window_type_utility;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002442}
2443
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002444static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002445xserver_map_shell_surface(struct weston_wm_window *window,
2446 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002447{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002448 struct weston_wm *wm = window->wm;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002449 struct weston_shell_interface *shell_interface =
2450 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002451 struct weston_output *output;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002452 struct weston_wm_window *parent;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002453 int flags = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002454
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002455 weston_wm_window_read_properties(window);
2456
2457 /* A weston_wm_window may have many different surfaces assigned
2458 * throughout its life, so we must make sure to remove the listener
2459 * from the old surface signal list. */
2460 if (window->surface)
2461 wl_list_remove(&window->surface_destroy_listener.link);
2462
2463 window->surface = surface;
2464 window->surface_destroy_listener.notify = surface_destroy;
2465 wl_signal_add(&window->surface->destroy_signal,
2466 &window->surface_destroy_listener);
2467
2468 weston_wm_window_schedule_repaint(window);
2469
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002470 if (!shell_interface->create_shell_surface)
2471 return;
2472
Jason Ekstranda7af7042013-10-12 22:38:11 -05002473 if (!shell_interface->get_primary_view)
2474 return;
2475
Pekka Paalanen50b67472014-10-01 15:02:41 +03002476 if (window->surface->configure) {
2477 weston_log("warning, unexpected in %s: "
2478 "surface's configure hook is already set.\n",
2479 __func__);
2480 return;
2481 }
2482
Murray Calavera883ac022015-06-06 13:02:22 +00002483 window->shsurf =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002484 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002485 window->surface,
2486 &shell_client);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002487 window->view = shell_interface->get_primary_view(shell_interface->shell,
2488 window->shsurf);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002489
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002490 if (window->name)
2491 shell_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002492 if (window->pid > 0)
2493 shell_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002494
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002495 if (window->fullscreen) {
2496 window->saved_width = window->width;
2497 window->saved_height = window->height;
2498 shell_interface->set_fullscreen(window->shsurf,
2499 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2500 0, NULL);
2501 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002502 } else if (legacy_fullscreen(wm, window, &output)) {
2503 window->fullscreen = 1;
2504 shell_interface->set_fullscreen(window->shsurf,
2505 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2506 0, output);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002507 } else if (window->override_redirect) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002508 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002509 window->x,
2510 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002511 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Axel Davye4450f92014-01-12 15:06:05 +01002512 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002513 parent = window->transient_for;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002514 if (weston_wm_window_type_inactive(window))
2515 flags = WL_SHELL_SURFACE_TRANSIENT_INACTIVE;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002516 shell_interface->set_transient(window->shsurf,
2517 parent->surface,
Axel Davyfa506b62014-01-12 15:06:04 +01002518 window->x - parent->x,
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002519 window->y - parent->y, flags);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002520 } else if (weston_wm_window_is_maximized(window)) {
2521 shell_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002522 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002523 if (weston_wm_window_type_inactive(window)) {
2524 shell_interface->set_xwayland(window->shsurf,
2525 window->x,
2526 window->y,
2527 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2528 } else {
2529 shell_interface->set_toplevel(window->shsurf);
2530 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002531 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002532}