blob: db6b4379f6a677877b3d8888d8e817e816459df4 [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
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300230 if (reply) {
MoD55375b92013-06-11 19:59:42 -0500231 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
Giulio Camuffob18f7882015-03-29 14:20:23 +0300737weston_wm_send_focus_window(struct weston_wm *wm,
738 struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400739{
Scott Moreau85ecac02012-05-21 15:49:14 -0600740 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400741
Scott Moreau85ecac02012-05-21 15:49:14 -0600742 if (window) {
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700743 uint32_t values[1];
744
Boyan Dingb9f863c2014-08-30 10:33:23 +0800745 if (window->override_redirect)
746 return;
747
Scott Moreau85ecac02012-05-21 15:49:14 -0600748 client_message.response_type = XCB_CLIENT_MESSAGE;
749 client_message.format = 32;
750 client_message.window = window->id;
751 client_message.type = wm->atom.wm_protocols;
752 client_message.data.data32[0] = wm->atom.wm_take_focus;
753 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
754
Murray Calavera883ac022015-06-06 13:02:22 +0000755 xcb_send_event(wm->conn, 0, window->id,
Scott Moreau85ecac02012-05-21 15:49:14 -0600756 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
757 (char *) &client_message);
758
759 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
760 window->id, XCB_TIME_CURRENT_TIME);
Jasper St. Pierref30af4e2015-03-22 10:14:49 -0700761
762 values[0] = XCB_STACK_MODE_ABOVE;
763 xcb_configure_window (wm->conn, window->frame_id,
764 XCB_CONFIG_WINDOW_STACK_MODE, values);
Scott Moreau85ecac02012-05-21 15:49:14 -0600765 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400766 xcb_set_input_focus (wm->conn,
767 XCB_INPUT_FOCUS_POINTER_ROOT,
768 XCB_NONE,
769 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600770 }
Giulio Camuffob18f7882015-03-29 14:20:23 +0300771}
772
773static void
774weston_wm_window_activate(struct wl_listener *listener, void *data)
775{
776 struct weston_surface *surface = data;
777 struct weston_wm_window *window = NULL;
778 struct weston_wm *wm =
779 container_of(listener, struct weston_wm, activate_listener);
780
781 if (surface) {
782 window = get_wm_window(surface);
783 }
784
785 weston_wm_send_focus_window(wm, window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400786
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500787 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800788 if (wm->focus_window->frame)
789 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400790 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500791 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400792 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500793 if (wm->focus_window) {
Dima Ryazanovb03b87f2013-11-15 02:01:19 -0800794 if (wm->focus_window->frame)
795 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400796 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500797 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400798}
799
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300800static void
801weston_wm_window_transform(struct wl_listener *listener, void *data)
802{
803 struct weston_surface *surface = data;
804 struct weston_wm_window *window = get_wm_window(surface);
805 struct weston_wm *wm =
806 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300807 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300808
809 if (!window || !wm)
810 return;
811
Jason Ekstranda7af7042013-10-12 22:38:11 -0500812 if (!window->view || !weston_view_is_mapped(window->view))
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300813 return;
814
Jason Ekstranda7af7042013-10-12 22:38:11 -0500815 if (window->x != window->view->geometry.x ||
816 window->y != window->view->geometry.y) {
817 values[0] = window->view->geometry.x;
818 values[1] = window->view->geometry.y;
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700819 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300820
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700821 xcb_configure_window(wm->conn, window->frame_id, mask, values);
822 xcb_flush(wm->conn);
823 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300824}
825
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400826#define ICCCM_WITHDRAWN_STATE 0
827#define ICCCM_NORMAL_STATE 1
828#define ICCCM_ICONIC_STATE 3
829
830static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500831weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400832{
833 struct weston_wm *wm = window->wm;
834 uint32_t property[2];
835
836 property[0] = state;
837 property[1] = XCB_WINDOW_NONE;
838
839 xcb_change_property(wm->conn,
840 XCB_PROP_MODE_REPLACE,
841 window->id,
842 wm->atom.wm_state,
843 wm->atom.wm_state,
844 32, /* format */
845 2, property);
846}
847
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400848static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500849weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
850{
851 struct weston_wm *wm = window->wm;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200852 uint32_t property[3];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500853 int i;
854
855 i = 0;
856 if (window->fullscreen)
857 property[i++] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200858 if (window->maximized_vert)
859 property[i++] = wm->atom.net_wm_state_maximized_vert;
860 if (window->maximized_horz)
861 property[i++] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500862
863 xcb_change_property(wm->conn,
864 XCB_PROP_MODE_REPLACE,
865 window->id,
866 wm->atom.net_wm_state,
867 XCB_ATOM_ATOM,
868 32, /* format */
869 i, property);
870}
871
872static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700873weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400874{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700875 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400876 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400877 int x, y, width, height;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200878 int buttons = FRAME_BUTTON_CLOSE;
879
880 if (window->decorate & MWM_DECOR_MAXIMIZE)
881 buttons |= FRAME_BUTTON_MAXIMIZE;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400882
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500883 window->frame = frame_create(window->wm->theme,
884 window->width, window->height,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +0200885 buttons, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500886 frame_resize_inside(window->frame, window->width, window->height);
887
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400888 weston_wm_window_get_frame_size(window, &width, &height);
889 weston_wm_window_get_child_position(window, &x, &y);
890
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400891 values[0] = wm->screen->black_pixel;
892 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400893 XCB_EVENT_MASK_KEY_PRESS |
894 XCB_EVENT_MASK_KEY_RELEASE |
895 XCB_EVENT_MASK_BUTTON_PRESS |
896 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400897 XCB_EVENT_MASK_POINTER_MOTION |
898 XCB_EVENT_MASK_ENTER_WINDOW |
899 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400900 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400901 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400902 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400903
904 window->frame_id = xcb_generate_id(wm->conn);
905 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400906 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400907 window->frame_id,
908 wm->screen->root,
909 0, 0,
910 width, height,
911 0,
912 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400913 wm->visual_id,
914 XCB_CW_BORDER_PIXEL |
915 XCB_CW_EVENT_MASK |
916 XCB_CW_COLORMAP, values);
917
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400918 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
919
920 values[0] = 0;
921 xcb_configure_window(wm->conn, window->id,
922 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
923
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400924 window->cairo_surface =
925 cairo_xcb_surface_create_with_xrender_format(wm->conn,
926 wm->screen,
927 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400928 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400929 width, height);
930
931 hash_table_insert(wm->window_hash, window->frame_id, window);
932}
933
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200934/*
935 * Sets the _NET_WM_DESKTOP property for the window to 'desktop'.
936 * Passing a <0 desktop value deletes the property.
937 */
938static void
939weston_wm_window_set_virtual_desktop(struct weston_wm_window *window,
Bryce Harringtone00554b2015-06-12 10:17:39 -0700940 int desktop)
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200941{
942 if (desktop >= 0) {
943 xcb_change_property(window->wm->conn,
944 XCB_PROP_MODE_REPLACE,
945 window->id,
946 window->wm->atom.net_wm_desktop,
947 XCB_ATOM_CARDINAL,
948 32, /* format */
949 1, &desktop);
950 } else {
951 xcb_delete_property(window->wm->conn,
952 window->id,
953 window->wm->atom.net_wm_desktop);
954 }
955}
956
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400957static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700958weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
959{
960 xcb_map_request_event_t *map_request =
961 (xcb_map_request_event_t *) event;
962 struct weston_wm_window *window;
963
964 if (our_resource(wm, map_request->window)) {
965 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
966 map_request->window);
967 return;
968 }
969
Derek Foreman49372142015-04-09 10:51:22 -0500970 if (!wm_lookup_window(wm, map_request->window, &window))
971 return;
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700972
973 weston_wm_window_read_properties(window);
974
975 if (window->frame_id == XCB_WINDOW_NONE)
976 weston_wm_window_create_frame(window);
977
978 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
979 window->id, window, window->frame_id);
980
981 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
982 weston_wm_window_set_net_wm_state(window);
Giulio Camuffoe90ea442014-12-13 18:06:34 +0200983 weston_wm_window_set_virtual_desktop(window, 0);
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700984
985 xcb_map_window(wm->conn, map_request->window);
986 xcb_map_window(wm->conn, window->frame_id);
987}
988
989static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400990weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
991{
992 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
993
994 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400995 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
996 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400997 return;
998 }
999
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001000 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001001}
1002
1003static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001004weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1005{
1006 xcb_unmap_notify_event_t *unmap_notify =
1007 (xcb_unmap_notify_event_t *) event;
1008 struct weston_wm_window *window;
1009
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001010 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
1011 unmap_notify->window,
1012 unmap_notify->event,
1013 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001014
1015 if (our_resource(wm, unmap_notify->window))
1016 return;
1017
MoD31700122013-06-11 19:58:55 -05001018 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -04001019 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
1020 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001021 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -04001022
Derek Foreman49372142015-04-09 10:51:22 -05001023 if (!wm_lookup_window(wm, unmap_notify->window, &window))
1024 return;
1025
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001026 if (window->surface_id) {
1027 /* Make sure we're not on the unpaired surface list or we
1028 * could be assigned a surface during surface creation that
1029 * was mapped before this unmap request.
1030 */
1031 wl_list_remove(&window->link);
1032 window->surface_id = 0;
1033 }
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001034 if (wm->focus_window == window)
1035 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001036 if (window->surface)
1037 wl_list_remove(&window->surface_destroy_listener.link);
1038 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +02001039 window->shsurf = NULL;
Dima Ryazanove5a32082013-11-15 02:01:18 -08001040 window->view = NULL;
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001041
1042 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1043 weston_wm_window_set_virtual_desktop(window, -1);
1044
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001045 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001046}
1047
1048static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001049weston_wm_window_draw_decoration(void *data)
1050{
1051 struct weston_wm_window *window = data;
1052 struct weston_wm *wm = window->wm;
1053 struct theme *t = wm->theme;
1054 cairo_t *cr;
1055 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001056 int32_t input_x, input_y, input_w, input_h;
Kristian Høgsberge5c1ae92014-04-30 16:28:41 -07001057 struct weston_shell_interface *shell_interface =
1058 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001059 uint32_t flags = 0;
1060
1061 weston_wm_window_read_properties(window);
1062
1063 window->repaint_source = NULL;
1064
1065 weston_wm_window_get_frame_size(window, &width, &height);
1066 weston_wm_window_get_child_position(window, &x, &y);
1067
1068 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
1069 cr = cairo_create(window->cairo_surface);
1070
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001071 if (window->fullscreen) {
1072 /* nothing */
1073 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001074 if (wm->focus_window == window)
1075 flags |= THEME_FRAME_ACTIVE;
1076
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001077 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001078 } else {
1079 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1080 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1081 cairo_paint(cr);
1082
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +01001083 render_shadow(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001084 }
1085
1086 cairo_destroy(cr);
1087
1088 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -07001089 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001090 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001091 pixman_region32_init(&window->surface->pending.opaque);
1092 } else {
1093 /* We leave an extra pixel around the X window area to
1094 * make sure we don't sample from the undefined alpha
1095 * channel when filtering. */
Murray Calavera883ac022015-06-06 13:02:22 +00001096 pixman_region32_init_rect(&window->surface->pending.opaque,
MoD384a11a2013-06-22 11:04:21 -05001097 x - 1, y - 1,
1098 window->width + 2,
1099 window->height + 2);
1100 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001101 if (window->view)
1102 weston_view_geometry_dirty(window->view);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001103
Kristian Høgsberg81585e92013-02-14 22:01:58 -05001104 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001105
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001106 if (window->decorate && !window->fullscreen) {
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001107 frame_input_rect(window->frame, &input_x, &input_y,
1108 &input_w, &input_h);
Jasper St. Pierred19e9b02015-03-21 21:24:43 -07001109 } else {
1110 input_x = x;
1111 input_y = y;
1112 input_w = width;
1113 input_h = height;
Kristian Høgsberg1d75c7d2013-10-30 23:46:08 -07001114 }
1115
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -05001116 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001117 input_x, input_y, input_w, input_h);
Jasper St. Pierreccf48fb2014-05-02 10:21:38 -04001118
1119 shell_interface->set_window_geometry(window->shsurf,
1120 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001121 }
1122}
1123
1124static void
1125weston_wm_window_schedule_repaint(struct weston_wm_window *window)
1126{
1127 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001128 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001129
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001130 if (window->frame_id == XCB_WINDOW_NONE) {
1131 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +03001132 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -07001133 pixman_region32_fini(&window->surface->pending.opaque);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001134 if (window->has_alpha) {
MoD384a11a2013-06-22 11:04:21 -05001135 pixman_region32_init(&window->surface->pending.opaque);
1136 } else {
1137 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
1138 width, height);
1139 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001140 if (window->view)
1141 weston_view_geometry_dirty(window->view);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -04001142 }
1143 return;
1144 }
1145
1146 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001147 return;
1148
1149 window->repaint_source =
1150 wl_event_loop_add_idle(wm->server->loop,
1151 weston_wm_window_draw_decoration,
1152 window);
1153}
1154
1155static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001156weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1157{
1158 xcb_property_notify_event_t *property_notify =
1159 (xcb_property_notify_event_t *) event;
1160 struct weston_wm_window *window;
1161
Derek Foreman49372142015-04-09 10:51:22 -05001162 if (!wm_lookup_window(wm, property_notify->window, &window))
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001163 return;
1164
1165 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001166
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001167 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001168 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001169 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001170 else
1171 read_and_dump_property(wm, property_notify->window,
1172 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001173
1174 if (property_notify->atom == wm->atom.net_wm_name ||
1175 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001176 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001177}
1178
1179static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001180weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001181 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001182{
1183 struct weston_wm_window *window;
1184 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001185 xcb_get_geometry_cookie_t geometry_cookie;
1186 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001187
Peter Huttererf3d62272013-08-08 11:57:05 +10001188 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001189 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001190 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001191 return;
1192 }
1193
MoD384a11a2013-06-22 11:04:21 -05001194 geometry_cookie = xcb_get_geometry(wm->conn, id);
1195
Giulio Camuffob18f7882015-03-29 14:20:23 +03001196 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
1197 XCB_EVENT_MASK_FOCUS_CHANGE;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001198 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1199
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001200 window->wm = wm;
1201 window->id = id;
1202 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001203 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001204 window->width = width;
1205 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001206 window->x = x;
1207 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001208
MoD384a11a2013-06-22 11:04:21 -05001209 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1210 /* technically we should use XRender and check the visual format's
1211 alpha_mask, but checking depth is simpler and works in all known cases */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001212 if (geometry_reply != NULL)
MoD384a11a2013-06-22 11:04:21 -05001213 window->has_alpha = geometry_reply->depth == 32;
1214 free(geometry_reply);
1215
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001216 hash_table_insert(wm->window_hash, id, window);
1217}
1218
1219static void
1220weston_wm_window_destroy(struct weston_wm_window *window)
1221{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001222 struct weston_wm *wm = window->wm;
1223
1224 if (window->repaint_source)
1225 wl_event_source_remove(window->repaint_source);
1226 if (window->cairo_surface)
1227 cairo_surface_destroy(window->cairo_surface);
1228
1229 if (window->frame_id) {
1230 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1231 xcb_destroy_window(wm->conn, window->frame_id);
1232 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
Giulio Camuffoe90ea442014-12-13 18:06:34 +02001233 weston_wm_window_set_virtual_desktop(window, -1);
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001234 hash_table_remove(wm->window_hash, window->frame_id);
1235 window->frame_id = XCB_WINDOW_NONE;
1236 }
1237
Kristian Høgsbergba83db22014-04-07 10:16:19 -07001238 if (window->surface_id)
1239 wl_list_remove(&window->link);
1240
Derek Foreman9a0b2b52015-04-09 14:48:22 -05001241 if (window->surface)
1242 wl_list_remove(&window->surface_destroy_listener.link);
1243
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001244 hash_table_remove(window->wm->window_hash, window->id);
1245 free(window);
1246}
1247
1248static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001249weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1250{
1251 xcb_create_notify_event_t *create_notify =
1252 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001253
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001254 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1255 create_notify->window,
1256 create_notify->width, create_notify->height,
1257 create_notify->override_redirect ? ", override" : "",
1258 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001259
1260 if (our_resource(wm, create_notify->window))
1261 return;
1262
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001263 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001264 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001265 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001266 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001267}
1268
1269static void
1270weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1271{
1272 xcb_destroy_notify_event_t *destroy_notify =
1273 (xcb_destroy_notify_event_t *) event;
1274 struct weston_wm_window *window;
1275
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001276 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1277 destroy_notify->window,
1278 destroy_notify->event,
1279 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001280
1281 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001282 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001283
Derek Foreman49372142015-04-09 10:51:22 -05001284 if (!wm_lookup_window(wm, destroy_notify->window, &window))
1285 return;
1286
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001287 weston_wm_window_destroy(window);
1288}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001289
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001290static void
1291weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1292{
1293 xcb_reparent_notify_event_t *reparent_notify =
1294 (xcb_reparent_notify_event_t *) event;
1295 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001296
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001297 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1298 reparent_notify->window,
1299 reparent_notify->parent,
1300 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001301
1302 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001303 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001304 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001305 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001306 } else if (!our_resource(wm, reparent_notify->parent)) {
Derek Foreman49372142015-04-09 10:51:22 -05001307 if (!wm_lookup_window(wm, reparent_notify->window, &window))
1308 return;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001309 weston_wm_window_destroy(window);
1310 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001311}
1312
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001313struct weston_seat *
1314weston_wm_pick_seat(struct weston_wm *wm)
1315{
1316 return container_of(wm->server->compositor->seat_list.next,
1317 struct weston_seat, link);
1318}
1319
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001320static struct weston_seat *
1321weston_wm_pick_seat_for_window(struct weston_wm_window *window)
1322{
1323 struct weston_wm *wm = window->wm;
1324 struct weston_seat *seat, *s;
1325
1326 seat = NULL;
1327 wl_list_for_each(s, &wm->server->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001328 struct weston_pointer *pointer = weston_seat_get_pointer(s);
1329 struct weston_pointer *old_pointer =
1330 weston_seat_get_pointer(seat);
1331
1332 if (pointer && pointer->focus &&
1333 pointer->focus->surface == window->surface &&
1334 pointer->button_count > 0 &&
1335 (!seat ||
1336 pointer->grab_serial -
1337 old_pointer->grab_serial < (1 << 30)))
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001338 seat = s;
1339 }
1340
1341 return seat;
1342}
1343
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001344static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001345weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1346 xcb_client_message_event_t *client_message)
1347{
1348 static const int map[] = {
1349 THEME_LOCATION_RESIZING_TOP_LEFT,
1350 THEME_LOCATION_RESIZING_TOP,
1351 THEME_LOCATION_RESIZING_TOP_RIGHT,
1352 THEME_LOCATION_RESIZING_RIGHT,
1353 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1354 THEME_LOCATION_RESIZING_BOTTOM,
1355 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1356 THEME_LOCATION_RESIZING_LEFT
1357 };
1358
1359 struct weston_wm *wm = window->wm;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001360 struct weston_seat *seat = weston_wm_pick_seat_for_window(window);
Derek Foreman1281a362015-07-31 16:55:32 -05001361 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001362 int detail;
1363 struct weston_shell_interface *shell_interface =
1364 &wm->server->compositor->shell_interface;
1365
Derek Foreman1281a362015-07-31 16:55:32 -05001366 if (!pointer || pointer->button_count != 1
1367 || !pointer->focus
1368 || pointer->focus->surface != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001369 return;
1370
1371 detail = client_message->data.data32[2];
1372 switch (detail) {
1373 case _NET_WM_MOVERESIZE_MOVE:
Derek Foremane4d6c832015-08-05 14:48:11 -05001374 shell_interface->move(window->shsurf, pointer);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001375 break;
1376 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1377 case _NET_WM_MOVERESIZE_SIZE_TOP:
1378 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1379 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1380 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1381 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1382 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1383 case _NET_WM_MOVERESIZE_SIZE_LEFT:
Derek Foremane4d6c832015-08-05 14:48:11 -05001384 shell_interface->resize(window->shsurf, pointer, map[detail]);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001385 break;
1386 case _NET_WM_MOVERESIZE_CANCEL:
1387 break;
1388 }
1389}
1390
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001391#define _NET_WM_STATE_REMOVE 0
1392#define _NET_WM_STATE_ADD 1
1393#define _NET_WM_STATE_TOGGLE 2
1394
1395static int
1396update_state(int action, int *state)
1397{
1398 int new_state, changed;
1399
1400 switch (action) {
1401 case _NET_WM_STATE_REMOVE:
1402 new_state = 0;
1403 break;
1404 case _NET_WM_STATE_ADD:
1405 new_state = 1;
1406 break;
1407 case _NET_WM_STATE_TOGGLE:
1408 new_state = !*state;
1409 break;
1410 default:
1411 return 0;
1412 }
1413
1414 changed = (*state != new_state);
1415 *state = new_state;
1416
1417 return changed;
1418}
1419
1420static void
1421weston_wm_window_configure(void *data);
1422
1423static void
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001424weston_wm_window_set_toplevel(struct weston_wm_window *window)
1425{
1426 struct weston_shell_interface *shell_interface =
1427 &window->wm->server->compositor->shell_interface;
1428
1429 shell_interface->set_toplevel(window->shsurf);
1430 window->width = window->saved_width;
1431 window->height = window->saved_height;
1432 if (window->frame)
1433 frame_resize_inside(window->frame,
1434 window->width,
1435 window->height);
1436 weston_wm_window_configure(window);
1437}
1438
1439static inline bool
1440weston_wm_window_is_maximized(struct weston_wm_window *window)
1441{
1442 return window->maximized_horz && window->maximized_vert;
1443}
1444
1445static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001446weston_wm_window_handle_state(struct weston_wm_window *window,
1447 xcb_client_message_event_t *client_message)
1448{
1449 struct weston_wm *wm = window->wm;
1450 struct weston_shell_interface *shell_interface =
1451 &wm->server->compositor->shell_interface;
1452 uint32_t action, property;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001453 int maximized = weston_wm_window_is_maximized(window);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001454
1455 action = client_message->data.data32[0];
1456 property = client_message->data.data32[1];
1457
1458 if (property == wm->atom.net_wm_state_fullscreen &&
1459 update_state(action, &window->fullscreen)) {
1460 weston_wm_window_set_net_wm_state(window);
1461 if (window->fullscreen) {
1462 window->saved_width = window->width;
1463 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001464
1465 if (window->shsurf)
1466 shell_interface->set_fullscreen(window->shsurf,
1467 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1468 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001469 } else {
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001470 if (window->shsurf)
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001471 weston_wm_window_set_toplevel(window);
1472 }
1473 } else {
1474 if (property == wm->atom.net_wm_state_maximized_vert &&
1475 update_state(action, &window->maximized_vert))
1476 weston_wm_window_set_net_wm_state(window);
1477 if (property == wm->atom.net_wm_state_maximized_horz &&
1478 update_state(action, &window->maximized_horz))
1479 weston_wm_window_set_net_wm_state(window);
Andrew Engelbrecht4c5a6f72014-12-02 12:18:44 -05001480
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001481 if (maximized != weston_wm_window_is_maximized(window)) {
1482 if (weston_wm_window_is_maximized(window)) {
1483 window->saved_width = window->width;
1484 window->saved_height = window->height;
1485
1486 if (window->shsurf)
1487 shell_interface->set_maximized(window->shsurf);
1488 } else if (window->shsurf) {
1489 weston_wm_window_set_toplevel(window);
1490 }
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001491 }
1492 }
1493}
1494
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001495static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001496surface_destroy(struct wl_listener *listener, void *data)
1497{
1498 struct weston_wm_window *window =
1499 container_of(listener,
1500 struct weston_wm_window, surface_destroy_listener);
1501
1502 wm_log("surface for xid %d destroyed\n", window->id);
1503
1504 /* This should have been freed by the shell.
1505 * Don't try to use it later. */
1506 window->shsurf = NULL;
1507 window->surface = NULL;
1508 window->view = NULL;
1509}
1510
1511static void
1512weston_wm_window_handle_surface_id(struct weston_wm_window *window,
1513 xcb_client_message_event_t *client_message)
1514{
1515 struct weston_wm *wm = window->wm;
1516 struct wl_resource *resource;
1517
1518 if (window->surface_id != 0) {
1519 wm_log("already have surface id for window %d\n", window->id);
1520 return;
1521 }
1522
1523 /* Xwayland will send the wayland requests to create the
1524 * wl_surface before sending this client message. Even so, we
1525 * can end up handling the X event before the wayland requests
1526 * and thus when we try to look up the surface ID, the surface
1527 * hasn't been created yet. In that case put the window on
1528 * the unpaired window list and continue when the surface gets
1529 * created. */
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001530 uint32_t id = client_message->data.data32[0];
1531 resource = wl_client_get_object(wm->server->client, id);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001532 if (resource) {
1533 window->surface_id = 0;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001534 xserver_map_shell_surface(window,
1535 wl_resource_get_user_data(resource));
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001536 }
1537 else {
Jason Ekstrand4ff4d922014-07-24 13:16:58 -07001538 window->surface_id = id;
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001539 wl_list_insert(&wm->unpaired_window_list, &window->link);
Tyler Venesscf4c13a2014-07-02 15:00:44 -07001540 }
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001541}
1542
1543static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001544weston_wm_handle_client_message(struct weston_wm *wm,
1545 xcb_generic_event_t *event)
1546{
1547 xcb_client_message_event_t *client_message =
1548 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001549 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001550
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001551 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1552 get_atom_name(wm->conn, client_message->type),
1553 client_message->data.data32[0],
1554 client_message->data.data32[1],
1555 client_message->data.data32[2],
1556 client_message->data.data32[3],
1557 client_message->data.data32[4],
1558 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001559
Jason Ekstrand0250a742014-07-24 13:17:47 -07001560 /* The window may get created and destroyed before we actually
1561 * handle the message. If it doesn't exist, bail.
1562 */
Derek Foreman49372142015-04-09 10:51:22 -05001563 if (!wm_lookup_window(wm, client_message->window, &window))
Jason Ekstrand0250a742014-07-24 13:17:47 -07001564 return;
1565
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001566 if (client_message->type == wm->atom.net_wm_moveresize)
1567 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001568 else if (client_message->type == wm->atom.net_wm_state)
1569 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07001570 else if (client_message->type == wm->atom.wl_surface_id)
1571 weston_wm_window_handle_surface_id(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001572}
1573
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001574enum cursor_type {
1575 XWM_CURSOR_TOP,
1576 XWM_CURSOR_BOTTOM,
1577 XWM_CURSOR_LEFT,
1578 XWM_CURSOR_RIGHT,
1579 XWM_CURSOR_TOP_LEFT,
1580 XWM_CURSOR_TOP_RIGHT,
1581 XWM_CURSOR_BOTTOM_LEFT,
1582 XWM_CURSOR_BOTTOM_RIGHT,
1583 XWM_CURSOR_LEFT_PTR,
1584};
1585
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001586/*
1587 * The following correspondences between file names and cursors was copied
1588 * from: https://bugs.kde.org/attachment.cgi?id=67313
1589 */
1590
1591static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001592 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001593 "sw-resize",
1594 "size_bdiag"
1595};
1596
1597static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001598 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001599 "se-resize",
1600 "size_fdiag"
1601};
1602
1603static const char *bottom_sides[] = {
1604 "bottom_side",
1605 "s-resize",
1606 "size_ver"
1607};
1608
1609static const char *left_ptrs[] = {
1610 "left_ptr",
1611 "default",
1612 "top_left_arrow",
1613 "left-arrow"
1614};
1615
1616static const char *left_sides[] = {
1617 "left_side",
1618 "w-resize",
1619 "size_hor"
1620};
1621
1622static const char *right_sides[] = {
1623 "right_side",
1624 "e-resize",
1625 "size_hor"
1626};
1627
1628static const char *top_left_corners[] = {
1629 "top_left_corner",
1630 "nw-resize",
1631 "size_fdiag"
1632};
1633
1634static const char *top_right_corners[] = {
1635 "top_right_corner",
1636 "ne-resize",
1637 "size_bdiag"
1638};
1639
1640static const char *top_sides[] = {
1641 "top_side",
1642 "n-resize",
1643 "size_ver"
1644};
1645
1646struct cursor_alternatives {
1647 const char **names;
1648 size_t count;
1649};
1650
1651static const struct cursor_alternatives cursors[] = {
1652 {top_sides, ARRAY_LENGTH(top_sides)},
1653 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1654 {left_sides, ARRAY_LENGTH(left_sides)},
1655 {right_sides, ARRAY_LENGTH(right_sides)},
1656 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1657 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1658 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1659 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1660 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001661};
1662
1663static void
1664weston_wm_create_cursors(struct weston_wm *wm)
1665{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001666 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001667 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001668 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001669
1670 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1671 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001672 for (j = 0; j < cursors[i].count; j++) {
1673 name = cursors[i].names[j];
1674 wm->cursors[i] =
1675 xcb_cursor_library_load_cursor(wm, name);
1676 if (wm->cursors[i] != (xcb_cursor_t)-1)
1677 break;
1678 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001679 }
1680
1681 wm->last_cursor = -1;
1682}
1683
1684static void
1685weston_wm_destroy_cursors(struct weston_wm *wm)
1686{
1687 uint8_t i;
1688
1689 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1690 xcb_free_cursor(wm->conn, wm->cursors[i]);
1691
1692 free(wm->cursors);
1693}
1694
1695static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001696get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001697{
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001698 // int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001699
1700 switch (location) {
1701 case THEME_LOCATION_RESIZING_TOP:
1702 return XWM_CURSOR_TOP;
1703 case THEME_LOCATION_RESIZING_BOTTOM:
1704 return XWM_CURSOR_BOTTOM;
1705 case THEME_LOCATION_RESIZING_LEFT:
1706 return XWM_CURSOR_LEFT;
1707 case THEME_LOCATION_RESIZING_RIGHT:
1708 return XWM_CURSOR_RIGHT;
1709 case THEME_LOCATION_RESIZING_TOP_LEFT:
1710 return XWM_CURSOR_TOP_LEFT;
1711 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1712 return XWM_CURSOR_TOP_RIGHT;
1713 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1714 return XWM_CURSOR_BOTTOM_LEFT;
1715 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1716 return XWM_CURSOR_BOTTOM_RIGHT;
1717 case THEME_LOCATION_EXTERIOR:
1718 case THEME_LOCATION_TITLEBAR:
1719 default:
1720 return XWM_CURSOR_LEFT_PTR;
1721 }
1722}
1723
1724static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001725weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1726 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001727{
1728 uint32_t cursor_value_list;
1729
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001730 if (wm->last_cursor == cursor)
1731 return;
1732
1733 wm->last_cursor = cursor;
1734
1735 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001736 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001737 XCB_CW_CURSOR, &cursor_value_list);
1738 xcb_flush(wm->conn);
1739}
1740
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001741static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001742weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001743{
1744 xcb_client_message_event_t client_message;
1745
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001746 if (window->delete_window) {
1747 client_message.response_type = XCB_CLIENT_MESSAGE;
1748 client_message.format = 32;
1749 client_message.window = window->id;
1750 client_message.type = window->wm->atom.wm_protocols;
1751 client_message.data.data32[0] =
1752 window->wm->atom.wm_delete_window;
1753 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001754
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001755 xcb_send_event(window->wm->conn, 0, window->id,
1756 XCB_EVENT_MASK_NO_EVENT,
1757 (char *) &client_message);
1758 } else {
1759 xcb_kill_client(window->wm->conn, window->id);
1760 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001761}
1762
1763static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001764weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1765{
1766 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1767 struct weston_shell_interface *shell_interface =
1768 &wm->server->compositor->shell_interface;
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001769 struct weston_seat *seat;
Derek Foremane4d6c832015-08-05 14:48:11 -05001770 struct weston_pointer *pointer;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001771 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001772 enum theme_location location;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001773 enum frame_button_state button_state;
1774 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001775
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001776 wm_log("XCB_BUTTON_%s (detail %d)\n",
1777 button->response_type == XCB_BUTTON_PRESS ?
1778 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001779
Derek Foreman49372142015-04-09 10:51:22 -05001780 if (!wm_lookup_window(wm, button->event, &window) ||
1781 !window->decorate)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001782 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001783
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001784 if (button->detail != 1 && button->detail != 2)
1785 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001786
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001787 seat = weston_wm_pick_seat_for_window(window);
Derek Foremane4d6c832015-08-05 14:48:11 -05001788 pointer = weston_seat_get_pointer(seat);
Kristian Høgsberg052ef4e2014-04-30 16:10:14 -07001789
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001790 button_state = button->response_type == XCB_BUTTON_PRESS ?
1791 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1792 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1793
Kristian Høgsberg8c3c7382014-04-30 16:52:30 -07001794 /* Make sure we're looking at the right location. The frame
1795 * could have received a motion event from a pointer from a
1796 * different wl_seat, but under X it looks like our core
1797 * pointer moved. Move the frame pointer to the button press
1798 * location before deciding what to do. */
1799 location = frame_pointer_motion(window->frame, NULL,
1800 button->event_x, button->event_y);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001801 location = frame_pointer_button(window->frame, NULL,
1802 button_id, button_state);
1803 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1804 weston_wm_window_schedule_repaint(window);
1805
1806 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001807 if (pointer)
1808 shell_interface->move(window->shsurf, pointer);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001809 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1810 }
1811
1812 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
Derek Foremane4d6c832015-08-05 14:48:11 -05001813 if (pointer)
1814 shell_interface->resize(window->shsurf, pointer, location);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001815 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1816 }
1817
1818 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001819 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001820 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001821 }
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02001822
1823 if (frame_status(window->frame) & FRAME_STATUS_MAXIMIZE) {
1824 window->maximized_horz = !window->maximized_horz;
1825 window->maximized_vert = !window->maximized_vert;
1826 if (weston_wm_window_is_maximized(window)) {
1827 window->saved_width = window->width;
1828 window->saved_height = window->height;
1829 shell_interface->set_maximized(window->shsurf);
1830 } else {
1831 weston_wm_window_set_toplevel(window);
1832 }
1833 frame_status_clear(window->frame, FRAME_STATUS_MAXIMIZE);
1834 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001835}
1836
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001837static void
1838weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1839{
1840 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1841 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001842 enum theme_location location;
1843 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001844
Derek Foreman49372142015-04-09 10:51:22 -05001845 if (!wm_lookup_window(wm, motion->event, &window) ||
1846 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001847 return;
1848
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001849 location = frame_pointer_motion(window->frame, NULL,
1850 motion->event_x, motion->event_y);
1851 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1852 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001853
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001854 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001855 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001856}
1857
1858static void
1859weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1860{
1861 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1862 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001863 enum theme_location location;
1864 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001865
Derek Foreman49372142015-04-09 10:51:22 -05001866 if (!wm_lookup_window(wm, enter->event, &window) ||
1867 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001868 return;
1869
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001870 location = frame_pointer_enter(window->frame, NULL,
1871 enter->event_x, enter->event_y);
1872 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1873 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001874
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001875 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001876 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001877}
1878
1879static void
1880weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1881{
1882 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1883 struct weston_wm_window *window;
1884
Derek Foreman49372142015-04-09 10:51:22 -05001885 if (!wm_lookup_window(wm, leave->event, &window) ||
1886 !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001887 return;
1888
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001889 frame_pointer_leave(window->frame, NULL);
1890 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1891 weston_wm_window_schedule_repaint(window);
1892
Tiago Vignattic1903232012-07-16 12:15:37 -04001893 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001894}
1895
Giulio Camuffob18f7882015-03-29 14:20:23 +03001896static void
1897weston_wm_handle_focus_in(struct weston_wm *wm, xcb_generic_event_t *event)
1898{
1899 xcb_focus_in_event_t *focus = (xcb_focus_in_event_t *) event;
1900 /* Do not let X clients change the focus behind the compositor's
1901 * back. Reset the focus to the old one if it changed. */
1902 if (!wm->focus_window || focus->event != wm->focus_window->id)
1903 weston_wm_send_focus_window(wm, wm->focus_window);
1904}
1905
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001906static int
1907weston_wm_handle_event(int fd, uint32_t mask, void *data)
1908{
1909 struct weston_wm *wm = data;
1910 xcb_generic_event_t *event;
1911 int count = 0;
1912
1913 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1914 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001915 free(event);
1916 count++;
1917 continue;
1918 }
1919
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001920 if (weston_wm_handle_dnd_event(wm, event)) {
1921 free(event);
1922 count++;
1923 continue;
1924 }
1925
MoD31700122013-06-11 19:58:55 -05001926 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001927 case XCB_BUTTON_PRESS:
1928 case XCB_BUTTON_RELEASE:
1929 weston_wm_handle_button(wm, event);
1930 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001931 case XCB_ENTER_NOTIFY:
1932 weston_wm_handle_enter(wm, event);
1933 break;
1934 case XCB_LEAVE_NOTIFY:
1935 weston_wm_handle_leave(wm, event);
1936 break;
1937 case XCB_MOTION_NOTIFY:
1938 weston_wm_handle_motion(wm, event);
1939 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001940 case XCB_CREATE_NOTIFY:
1941 weston_wm_handle_create_notify(wm, event);
1942 break;
1943 case XCB_MAP_REQUEST:
1944 weston_wm_handle_map_request(wm, event);
1945 break;
1946 case XCB_MAP_NOTIFY:
1947 weston_wm_handle_map_notify(wm, event);
1948 break;
1949 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001950 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001951 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001952 case XCB_REPARENT_NOTIFY:
1953 weston_wm_handle_reparent_notify(wm, event);
1954 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001955 case XCB_CONFIGURE_REQUEST:
1956 weston_wm_handle_configure_request(wm, event);
1957 break;
1958 case XCB_CONFIGURE_NOTIFY:
1959 weston_wm_handle_configure_notify(wm, event);
1960 break;
1961 case XCB_DESTROY_NOTIFY:
1962 weston_wm_handle_destroy_notify(wm, event);
1963 break;
1964 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001965 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001966 break;
1967 case XCB_PROPERTY_NOTIFY:
1968 weston_wm_handle_property_notify(wm, event);
1969 break;
1970 case XCB_CLIENT_MESSAGE:
1971 weston_wm_handle_client_message(wm, event);
1972 break;
Giulio Camuffob18f7882015-03-29 14:20:23 +03001973 case XCB_FOCUS_IN:
1974 weston_wm_handle_focus_in(wm, event);
1975 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001976 }
1977
1978 free(event);
1979 count++;
1980 }
1981
Marek Chalupaa1f3f3c2015-08-12 09:55:12 +02001982 if (count != 0)
1983 xcb_flush(wm->conn);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001984
1985 return count;
1986}
1987
1988static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001989weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1990{
1991 xcb_depth_iterator_t d_iter;
1992 xcb_visualtype_iterator_t vt_iter;
1993 xcb_visualtype_t *visualtype;
1994
1995 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1996 visualtype = NULL;
1997 while (d_iter.rem > 0) {
1998 if (d_iter.data->depth == 32) {
1999 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
2000 visualtype = vt_iter.data;
2001 break;
2002 }
2003
2004 xcb_depth_next(&d_iter);
2005 }
2006
2007 if (visualtype == NULL) {
2008 weston_log("no 32 bit visualtype\n");
2009 return;
2010 }
2011
2012 wm->visual_id = visualtype->visual_id;
2013 wm->colormap = xcb_generate_id(wm->conn);
2014 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
2015 wm->colormap, wm->screen->root, wm->visual_id);
2016}
2017
2018static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002019weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002020{
2021
2022#define F(field) offsetof(struct weston_wm, field)
2023
2024 static const struct { const char *name; int offset; } atoms[] = {
2025 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002026 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002027 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
2028 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04002029 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002030 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002031 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002032 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002033 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002034 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002035 { "_NET_WM_ICON", F(atom.net_wm_icon) },
2036 { "_NET_WM_STATE", F(atom.net_wm_state) },
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002037 { "_NET_WM_STATE_MAXIMIZED_VERT", F(atom.net_wm_state_maximized_vert) },
2038 { "_NET_WM_STATE_MAXIMIZED_HORZ", F(atom.net_wm_state_maximized_horz) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002039 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
2040 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
2041 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
Giulio Camuffoe90ea442014-12-13 18:06:34 +02002042 { "_NET_WM_DESKTOP", F(atom.net_wm_desktop) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002043 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
2044
2045 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
2046 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
2047 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
2048 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
2049 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
2050 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
2051 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03002052 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
2053 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002054 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
2055 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
2056 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
2057 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
2058 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
2059
2060 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
2061 { "_NET_SUPPORTING_WM_CHECK",
2062 F(atom.net_supporting_wm_check) },
2063 { "_NET_SUPPORTED", F(atom.net_supported) },
2064 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
2065 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04002066 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002067 { "TARGETS", F(atom.targets) },
2068 { "UTF8_STRING", F(atom.utf8_string) },
2069 { "_WL_SELECTION", F(atom.wl_selection) },
2070 { "INCR", F(atom.incr) },
2071 { "TIMESTAMP", F(atom.timestamp) },
2072 { "MULTIPLE", F(atom.multiple) },
2073 { "UTF8_STRING" , F(atom.utf8_string) },
2074 { "COMPOUND_TEXT", F(atom.compound_text) },
2075 { "TEXT", F(atom.text) },
2076 { "STRING", F(atom.string) },
2077 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
2078 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002079 { "XdndSelection", F(atom.xdnd_selection) },
2080 { "XdndAware", F(atom.xdnd_aware) },
2081 { "XdndEnter", F(atom.xdnd_enter) },
2082 { "XdndLeave", F(atom.xdnd_leave) },
2083 { "XdndDrop", F(atom.xdnd_drop) },
2084 { "XdndStatus", F(atom.xdnd_status) },
2085 { "XdndFinished", F(atom.xdnd_finished) },
2086 { "XdndTypeList", F(atom.xdnd_type_list) },
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002087 { "XdndActionCopy", F(atom.xdnd_action_copy) },
2088 { "WL_SURFACE_ID", F(atom.wl_surface_id) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002089 };
2090#undef F
2091
2092 xcb_xfixes_query_version_cookie_t xfixes_cookie;
2093 xcb_xfixes_query_version_reply_t *xfixes_reply;
2094 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
2095 xcb_intern_atom_reply_t *reply;
2096 xcb_render_query_pict_formats_reply_t *formats_reply;
2097 xcb_render_query_pict_formats_cookie_t formats_cookie;
2098 xcb_render_pictforminfo_t *formats;
2099 uint32_t i;
2100
2101 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002102 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002103
2104 formats_cookie = xcb_render_query_pict_formats(wm->conn);
2105
2106 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
2107 cookies[i] = xcb_intern_atom (wm->conn, 0,
2108 strlen(atoms[i].name),
2109 atoms[i].name);
2110
2111 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
2112 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
2113 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
2114 free(reply);
2115 }
2116
2117 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
2118 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02002119 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002120
2121 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
2122 XCB_XFIXES_MAJOR_VERSION,
2123 XCB_XFIXES_MINOR_VERSION);
2124 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
2125 xfixes_cookie, NULL);
2126
Martin Minarik6d118362012-06-07 18:01:59 +02002127 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002128 xfixes_reply->major_version, xfixes_reply->minor_version);
2129
2130 free(xfixes_reply);
2131
2132 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
2133 formats_cookie, 0);
2134 if (formats_reply == NULL)
2135 return;
2136
2137 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002138 for (i = 0; i < formats_reply->num_formats; i++) {
2139 if (formats[i].direct.red_mask != 0xff &&
2140 formats[i].direct.red_shift != 16)
2141 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002142 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2143 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04002144 wm->format_rgb = formats[i];
2145 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
2146 formats[i].depth == 32 &&
2147 formats[i].direct.alpha_mask == 0xff &&
2148 formats[i].direct.alpha_shift == 24)
2149 wm->format_rgba = formats[i];
2150 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002151
2152 free(formats_reply);
2153}
2154
2155static void
2156weston_wm_create_wm_window(struct weston_wm *wm)
2157{
2158 static const char name[] = "Weston WM";
2159
2160 wm->wm_window = xcb_generate_id(wm->conn);
2161 xcb_create_window(wm->conn,
2162 XCB_COPY_FROM_PARENT,
2163 wm->wm_window,
2164 wm->screen->root,
2165 0, 0,
2166 10, 10,
2167 0,
2168 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2169 wm->screen->root_visual,
2170 0, NULL);
2171
2172 xcb_change_property(wm->conn,
2173 XCB_PROP_MODE_REPLACE,
2174 wm->wm_window,
2175 wm->atom.net_supporting_wm_check,
2176 XCB_ATOM_WINDOW,
2177 32, /* format */
2178 1, &wm->wm_window);
2179
2180 xcb_change_property(wm->conn,
2181 XCB_PROP_MODE_REPLACE,
2182 wm->wm_window,
2183 wm->atom.net_wm_name,
2184 wm->atom.utf8_string,
2185 8, /* format */
2186 strlen(name), name);
2187
2188 xcb_change_property(wm->conn,
2189 XCB_PROP_MODE_REPLACE,
2190 wm->screen->root,
2191 wm->atom.net_supporting_wm_check,
2192 XCB_ATOM_WINDOW,
2193 32, /* format */
2194 1, &wm->wm_window);
2195
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04002196 /* Claim the WM_S0 selection even though we don't suport
2197 * the --replace functionality. */
2198 xcb_set_selection_owner(wm->conn,
2199 wm->wm_window,
2200 wm->atom.wm_s0,
2201 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07002202
2203 xcb_set_selection_owner(wm->conn,
2204 wm->wm_window,
2205 wm->atom.net_wm_cm_s0,
2206 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002207}
2208
2209struct weston_wm *
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002210weston_wm_create(struct weston_xserver *wxs, int fd)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002211{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002212 struct weston_wm *wm;
2213 struct wl_event_loop *loop;
2214 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002215 uint32_t values[1];
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002216 xcb_atom_t supported[5];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002217
Peter Huttererf3d62272013-08-08 11:57:05 +10002218 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002219 if (wm == NULL)
2220 return NULL;
2221
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002222 wm->server = wxs;
2223 wm->window_hash = hash_table_create();
2224 if (wm->window_hash == NULL) {
2225 free(wm);
2226 return NULL;
2227 }
2228
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002229 /* xcb_connect_to_fd takes ownership of the fd. */
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002230 wm->conn = xcb_connect_to_fd(fd, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002231 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002232 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002233 close(fd);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002234 hash_table_destroy(wm->window_hash);
2235 free(wm);
2236 return NULL;
2237 }
2238
2239 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
2240 wm->screen = s.data;
2241
2242 loop = wl_display_get_event_loop(wxs->wl_display);
2243 wm->source =
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002244 wl_event_loop_add_fd(loop, fd,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002245 WL_EVENT_READABLE,
2246 weston_wm_handle_event, wm);
2247 wl_event_source_check(wm->source);
2248
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02002249 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04002250 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002251
2252 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002253 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2254 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
2255 XCB_EVENT_MASK_PROPERTY_CHANGE;
2256 xcb_change_window_attributes(wm->conn, wm->screen->root,
2257 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07002258
2259 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
2260 XCB_COMPOSITE_REDIRECT_MANUAL);
2261
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002262 wm->theme = theme_create();
2263
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002264 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002265 supported[1] = wm->atom.net_wm_state;
2266 supported[2] = wm->atom.net_wm_state_fullscreen;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002267 supported[3] = wm->atom.net_wm_state_maximized_vert;
2268 supported[4] = wm->atom.net_wm_state_maximized_horz;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002269 xcb_change_property(wm->conn,
2270 XCB_PROP_MODE_REPLACE,
2271 wm->screen->root,
2272 wm->atom.net_supported,
2273 XCB_ATOM_ATOM,
2274 32, /* format */
2275 ARRAY_LENGTH(supported), supported);
2276
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04002277 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002278
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07002279 weston_wm_dnd_init(wm);
2280
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002281 xcb_flush(wm->conn);
2282
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002283 wm->create_surface_listener.notify = weston_wm_create_surface;
2284 wl_signal_add(&wxs->compositor->create_surface_signal,
2285 &wm->create_surface_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002286 wm->activate_listener.notify = weston_wm_window_activate;
2287 wl_signal_add(&wxs->compositor->activate_signal,
2288 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002289 wm->transform_listener.notify = weston_wm_window_transform;
2290 wl_signal_add(&wxs->compositor->transform_signal,
2291 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002292 wm->kill_listener.notify = weston_wm_kill_client;
2293 wl_signal_add(&wxs->compositor->kill_signal,
2294 &wm->kill_listener);
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002295 wl_list_init(&wm->unpaired_window_list);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002296
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002297 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04002298 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002299
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002300 /* Create wm window and take WM_S0 selection last, which
2301 * signals to Xwayland that we're done with setup. */
2302 weston_wm_create_wm_window(wm);
2303
2304 weston_log("created wm, root %d\n", wm->screen->root);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002305
2306 return wm;
2307}
2308
2309void
2310weston_wm_destroy(struct weston_wm *wm)
2311{
2312 /* FIXME: Free windows in hash. */
2313 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04002314 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002315 xcb_disconnect(wm->conn);
2316 wl_event_source_remove(wm->source);
2317 wl_list_remove(&wm->selection_listener.link);
2318 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002319 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01002320 wl_list_remove(&wm->transform_listener.link);
Derek Foremanf10e06c2015-02-03 11:05:10 -06002321 wl_list_remove(&wm->create_surface_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002322
2323 free(wm);
2324}
2325
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002326static struct weston_wm_window *
2327get_wm_window(struct weston_surface *surface)
2328{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002329 struct wl_listener *listener;
2330
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002331 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002332 if (listener)
2333 return container_of(listener, struct weston_wm_window,
2334 surface_destroy_listener);
2335
2336 return NULL;
2337}
2338
2339static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002340weston_wm_window_configure(void *data)
2341{
2342 struct weston_wm_window *window = data;
2343 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002344 uint32_t values[4];
2345 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002346
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002347 weston_wm_window_get_child_position(window, &x, &y);
2348 values[0] = x;
2349 values[1] = y;
2350 values[2] = window->width;
2351 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002352 xcb_configure_window(wm->conn,
2353 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002354 XCB_CONFIG_WINDOW_X |
2355 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002356 XCB_CONFIG_WINDOW_WIDTH |
2357 XCB_CONFIG_WINDOW_HEIGHT,
2358 values);
2359
2360 weston_wm_window_get_frame_size(window, &width, &height);
2361 values[0] = width;
2362 values[1] = height;
2363 xcb_configure_window(wm->conn,
2364 window->frame_id,
2365 XCB_CONFIG_WINDOW_WIDTH |
2366 XCB_CONFIG_WINDOW_HEIGHT,
2367 values);
2368
2369 window->configure_source = NULL;
2370
2371 weston_wm_window_schedule_repaint(window);
2372}
2373
2374static void
Jasper St. Pierreac985be2014-04-28 11:19:28 -04002375send_configure(struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002376{
2377 struct weston_wm_window *window = get_wm_window(surface);
2378 struct weston_wm *wm = window->wm;
2379 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002380 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002381
Marek Chalupae9fe4672014-10-29 13:44:44 +01002382 if (window->decorate && !window->fullscreen) {
Jasper St. Pierre8ffd38b2014-08-27 09:38:33 -04002383 hborder = 2 * t->width;
2384 vborder = t->titlebar_height + t->width;
2385 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002386 hborder = 0;
2387 vborder = 0;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002388 }
2389
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002390 if (width > hborder)
2391 window->width = width - hborder;
2392 else
2393 window->width = 1;
2394
2395 if (height > vborder)
2396 window->height = height - vborder;
2397 else
2398 window->height = 1;
2399
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002400 if (window->frame)
2401 frame_resize_inside(window->frame, window->width, window->height);
2402
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002403 if (window->configure_source)
2404 return;
2405
2406 window->configure_source =
2407 wl_event_loop_add_idle(wm->server->loop,
2408 weston_wm_window_configure, window);
2409}
2410
2411static const struct weston_shell_client shell_client = {
2412 send_configure
2413};
2414
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002415static int
2416legacy_fullscreen(struct weston_wm *wm,
2417 struct weston_wm_window *window,
2418 struct weston_output **output_ret)
2419{
2420 struct weston_compositor *compositor = wm->server->compositor;
2421 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002422 uint32_t minmax = PMinSize | PMaxSize;
2423 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002424
2425 /* Heuristics for detecting legacy fullscreen windows... */
2426
2427 wl_list_for_each(output, &compositor->output_list, link) {
2428 if (output->x == window->x &&
2429 output->y == window->y &&
2430 output->width == window->width &&
2431 output->height == window->height &&
2432 window->override_redirect) {
2433 *output_ret = output;
2434 return 1;
2435 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002436
2437 matching_size = 0;
2438 if ((window->size_hints.flags & (USSize |PSize)) &&
2439 window->size_hints.width == output->width &&
2440 window->size_hints.height == output->height)
2441 matching_size = 1;
2442 if ((window->size_hints.flags & minmax) == minmax &&
2443 window->size_hints.min_width == output->width &&
2444 window->size_hints.min_height == output->height &&
2445 window->size_hints.max_width == output->width &&
2446 window->size_hints.max_height == output->height)
2447 matching_size = 1;
2448
2449 if (matching_size && !window->decorate &&
2450 (window->size_hints.flags & (USPosition | PPosition)) &&
2451 window->size_hints.x == output->x &&
2452 window->size_hints.y == output->y) {
2453 *output_ret = output;
2454 return 1;
2455 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002456 }
2457
2458 return 0;
2459}
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002460
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002461static bool
2462weston_wm_window_type_inactive(struct weston_wm_window *window)
2463{
2464 struct weston_wm *wm = window->wm;
2465
2466 return window->type == wm->atom.net_wm_window_type_tooltip ||
2467 window->type == wm->atom.net_wm_window_type_dropdown ||
2468 window->type == wm->atom.net_wm_window_type_dnd ||
2469 window->type == wm->atom.net_wm_window_type_combo ||
Giulio Camuffo84787ea2015-04-29 19:00:48 +03002470 window->type == wm->atom.net_wm_window_type_popup ||
2471 window->type == wm->atom.net_wm_window_type_utility;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002472}
2473
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002474static void
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002475xserver_map_shell_surface(struct weston_wm_window *window,
2476 struct weston_surface *surface)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002477{
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002478 struct weston_wm *wm = window->wm;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002479 struct weston_shell_interface *shell_interface =
2480 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002481 struct weston_output *output;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002482 struct weston_wm_window *parent;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002483 int flags = 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002484
Kristian Høgsberg757d8af2014-03-17 22:33:29 -07002485 weston_wm_window_read_properties(window);
2486
2487 /* A weston_wm_window may have many different surfaces assigned
2488 * throughout its life, so we must make sure to remove the listener
2489 * from the old surface signal list. */
2490 if (window->surface)
2491 wl_list_remove(&window->surface_destroy_listener.link);
2492
2493 window->surface = surface;
2494 window->surface_destroy_listener.notify = surface_destroy;
2495 wl_signal_add(&window->surface->destroy_signal,
2496 &window->surface_destroy_listener);
2497
2498 weston_wm_window_schedule_repaint(window);
2499
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002500 if (!shell_interface->create_shell_surface)
2501 return;
2502
Jason Ekstranda7af7042013-10-12 22:38:11 -05002503 if (!shell_interface->get_primary_view)
2504 return;
2505
Pekka Paalanen50b67472014-10-01 15:02:41 +03002506 if (window->surface->configure) {
2507 weston_log("warning, unexpected in %s: "
2508 "surface's configure hook is already set.\n",
2509 __func__);
2510 return;
2511 }
2512
Murray Calavera883ac022015-06-06 13:02:22 +00002513 window->shsurf =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002514 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002515 window->surface,
2516 &shell_client);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002517 window->view = shell_interface->get_primary_view(shell_interface->shell,
2518 window->shsurf);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002519
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002520 if (window->name)
2521 shell_interface->set_title(window->shsurf, window->name);
Giulio Camuffoa8e9b412015-01-27 19:10:37 +02002522 if (window->pid > 0)
2523 shell_interface->set_pid(window->shsurf, window->pid);
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002524
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002525 if (window->fullscreen) {
2526 window->saved_width = window->width;
2527 window->saved_height = window->height;
2528 shell_interface->set_fullscreen(window->shsurf,
2529 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2530 0, NULL);
2531 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002532 } else if (legacy_fullscreen(wm, window, &output)) {
2533 window->fullscreen = 1;
2534 shell_interface->set_fullscreen(window->shsurf,
2535 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2536 0, output);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002537 } else if (window->override_redirect) {
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002538 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002539 window->x,
2540 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002541 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Axel Davye4450f92014-01-12 15:06:05 +01002542 } else if (window->transient_for && window->transient_for->surface) {
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002543 parent = window->transient_for;
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002544 if (weston_wm_window_type_inactive(window))
2545 flags = WL_SHELL_SURFACE_TRANSIENT_INACTIVE;
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002546 shell_interface->set_transient(window->shsurf,
2547 parent->surface,
Axel Davyfa506b62014-01-12 15:06:04 +01002548 window->x - parent->x,
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002549 window->y - parent->y, flags);
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002550 } else if (weston_wm_window_is_maximized(window)) {
2551 shell_interface->set_maximized(window->shsurf);
Kristian Høgsberg9f7e3312014-01-02 22:40:37 -08002552 } else {
Giulio Camuffo836b9c72015-01-24 17:54:21 +02002553 if (weston_wm_window_type_inactive(window)) {
2554 shell_interface->set_xwayland(window->shsurf,
2555 window->x,
2556 window->y,
2557 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2558 } else {
2559 shell_interface->set_toplevel(window->shsurf);
2560 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002561 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002562}