blob: a889278627d52d77d92b9f42c535b0e5b84cc419 [file] [log] [blame]
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
Kristian Høgsberg380deee2012-05-21 17:12:41 -040024
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <sys/socket.h>
29#include <sys/un.h>
30#include <fcntl.h>
31#include <errno.h>
32#include <unistd.h>
33#include <signal.h>
Tiago Vignatti90fada42012-07-16 12:02:08 -040034#include <X11/Xcursor/Xcursor.h>
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -050035#include <linux/input.h>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040036
37#include "xwayland.h"
38
39#include "../../shared/cairo-util.h"
40#include "../compositor.h"
41#include "xserver-server-protocol.h"
42#include "hash.h"
43
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070044struct wm_size_hints {
45 uint32_t flags;
46 int32_t x, y;
47 int32_t width, height; /* should set so old wm's don't mess up */
48 int32_t min_width, min_height;
49 int32_t max_width, max_height;
50 int32_t width_inc, height_inc;
51 struct {
52 int32_t x;
53 int32_t y;
54 } min_aspect, max_aspect;
55 int32_t base_width, base_height;
56 int32_t win_gravity;
57};
58
59#define USPosition (1L << 0)
60#define USSize (1L << 1)
61#define PPosition (1L << 2)
62#define PSize (1L << 3)
63#define PMinSize (1L << 4)
64#define PMaxSize (1L << 5)
65#define PResizeInc (1L << 6)
66#define PAspect (1L << 7)
67#define PBaseSize (1L << 8)
68#define PWinGravity (1L << 9)
69
Kristian Høgsberg380deee2012-05-21 17:12:41 -040070struct motif_wm_hints {
71 uint32_t flags;
72 uint32_t functions;
73 uint32_t decorations;
74 int32_t input_mode;
75 uint32_t status;
76};
77
78#define MWM_HINTS_FUNCTIONS (1L << 0)
79#define MWM_HINTS_DECORATIONS (1L << 1)
80#define MWM_HINTS_INPUT_MODE (1L << 2)
81#define MWM_HINTS_STATUS (1L << 3)
82
83#define MWM_FUNC_ALL (1L << 0)
84#define MWM_FUNC_RESIZE (1L << 1)
85#define MWM_FUNC_MOVE (1L << 2)
86#define MWM_FUNC_MINIMIZE (1L << 3)
87#define MWM_FUNC_MAXIMIZE (1L << 4)
88#define MWM_FUNC_CLOSE (1L << 5)
89
90#define MWM_DECOR_ALL (1L << 0)
91#define MWM_DECOR_BORDER (1L << 1)
92#define MWM_DECOR_RESIZEH (1L << 2)
93#define MWM_DECOR_TITLE (1L << 3)
94#define MWM_DECOR_MENU (1L << 4)
95#define MWM_DECOR_MINIMIZE (1L << 5)
96#define MWM_DECOR_MAXIMIZE (1L << 6)
97
98#define MWM_INPUT_MODELESS 0
99#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
100#define MWM_INPUT_SYSTEM_MODAL 2
101#define MWM_INPUT_FULL_APPLICATION_MODAL 3
102#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
103
104#define MWM_TEAROFF_WINDOW (1L<<0)
105
106#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
107#define _NET_WM_MOVERESIZE_SIZE_TOP 1
108#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
109#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
110#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
111#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
112#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
113#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
114#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
115#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
116#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
117#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
118
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400119struct weston_wm_window {
120 struct weston_wm *wm;
121 xcb_window_t id;
122 xcb_window_t frame_id;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500123 struct frame *frame;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400124 cairo_surface_t *cairo_surface;
125 struct weston_surface *surface;
126 struct shell_surface *shsurf;
127 struct wl_listener surface_destroy_listener;
128 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400129 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400130 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300131 int pid;
132 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400133 char *class;
134 char *name;
135 struct weston_wm_window *transient_for;
136 uint32_t protocols;
137 xcb_atom_t type;
138 int width, height;
139 int x, y;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500140 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400141 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300142 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500143 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500144 int has_alpha;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700145 int delete_window;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700146 struct wm_size_hints size_hints;
147 struct motif_wm_hints motif_hints;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400148};
149
150static struct weston_wm_window *
151get_wm_window(struct weston_surface *surface);
152
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400153static void
154weston_wm_window_schedule_repaint(struct weston_wm_window *window);
155
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400156static int __attribute__ ((format (printf, 1, 2)))
157wm_log(const char *fmt, ...)
158{
159#ifdef WM_DEBUG
160 int l;
161 va_list argp;
162
163 va_start(argp, fmt);
164 l = weston_vlog(fmt, argp);
165 va_end(argp);
166
167 return l;
168#else
169 return 0;
170#endif
171}
172
173static int __attribute__ ((format (printf, 1, 2)))
174wm_log_continue(const char *fmt, ...)
175{
176#ifdef WM_DEBUG
177 int l;
178 va_list argp;
179
180 va_start(argp, fmt);
181 l = weston_vlog_continue(fmt, argp);
182 va_end(argp);
183
184 return l;
185#else
186 return 0;
187#endif
188}
189
190
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400191const char *
192get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
193{
194 xcb_get_atom_name_cookie_t cookie;
195 xcb_get_atom_name_reply_t *reply;
196 xcb_generic_error_t *e;
197 static char buffer[64];
198
199 if (atom == XCB_ATOM_NONE)
200 return "None";
201
202 cookie = xcb_get_atom_name (c, atom);
203 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500204
205 if(reply) {
206 snprintf(buffer, sizeof buffer, "%.*s",
207 xcb_get_atom_name_name_length (reply),
208 xcb_get_atom_name_name (reply));
209 } else {
210 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
211 }
212
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400213 free(reply);
214
215 return buffer;
216}
217
Tiago Vignatti90fada42012-07-16 12:02:08 -0400218static xcb_cursor_t
219xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
220{
221 xcb_connection_t *c = wm->conn;
222 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
223 xcb_screen_t *screen = s.data;
224 xcb_gcontext_t gc;
225 xcb_pixmap_t pix;
226 xcb_render_picture_t pic;
227 xcb_cursor_t cursor;
228 int stride = img->width * 4;
229
230 pix = xcb_generate_id(c);
231 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
232
233 pic = xcb_generate_id(c);
234 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
235
236 gc = xcb_generate_id(c);
237 xcb_create_gc(c, gc, pix, 0, 0);
238
239 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
240 img->width, img->height, 0, 0, 0, 32,
241 stride * img->height, (uint8_t *) img->pixels);
242 xcb_free_gc(c, gc);
243
244 cursor = xcb_generate_id(c);
245 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
246
247 xcb_render_free_picture(c, pic);
248 xcb_free_pixmap(c, pix);
249
250 return cursor;
251}
252
253static xcb_cursor_t
254xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
255{
256 /* TODO: treat animated cursors as well */
257 if (images->nimage != 1)
258 return -1;
259
260 return xcb_cursor_image_load_cursor(wm, images->images[0]);
261}
262
263static xcb_cursor_t
264xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
265{
266 xcb_cursor_t cursor;
267 XcursorImages *images;
268 char *v = NULL;
269 int size = 0;
270
271 if (!file)
272 return 0;
273
274 v = getenv ("XCURSOR_SIZE");
275 if (v)
276 size = atoi(v);
277
278 if (!size)
279 size = 32;
280
281 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300282 if (!images)
283 return -1;
284
Tiago Vignatti90fada42012-07-16 12:02:08 -0400285 cursor = xcb_cursor_images_load_cursor (wm, images);
286 XcursorImagesDestroy (images);
287
288 return cursor;
289}
290
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400291void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400292dump_property(struct weston_wm *wm,
293 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400294{
295 int32_t *incr_value;
296 const char *text_value, *name;
297 xcb_atom_t *atom_value;
298 int width, len;
299 uint32_t i;
300
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400301 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400302 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400303 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400304 return;
305 }
306
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400307 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
308 get_atom_name(wm->conn, reply->type),
309 reply->format,
310 xcb_get_property_value_length(reply),
311 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400312
313 if (reply->type == wm->atom.incr) {
314 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400315 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400316 } else if (reply->type == wm->atom.utf8_string ||
317 reply->type == wm->atom.string) {
318 text_value = xcb_get_property_value(reply);
319 if (reply->value_len > 40)
320 len = 40;
321 else
322 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400323 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400324 } else if (reply->type == XCB_ATOM_ATOM) {
325 atom_value = xcb_get_property_value(reply);
326 for (i = 0; i < reply->value_len; i++) {
327 name = get_atom_name(wm->conn, atom_value[i]);
328 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400329 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400330 width = 4;
331 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400332 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400333 }
334
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400335 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400336 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400337 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400338 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400339 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400340 }
341}
342
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200343static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400344read_and_dump_property(struct weston_wm *wm,
345 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400346{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400347 xcb_get_property_reply_t *reply;
348 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400349
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400350 cookie = xcb_get_property(wm->conn, 0, window,
351 property, XCB_ATOM_ANY, 0, 2048);
352 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400353
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400354 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400355
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400356 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400357}
358
359/* We reuse some predefined, but otherwise useles atoms */
360#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
361#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500362#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700363#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400364
365static void
366weston_wm_window_read_properties(struct weston_wm_window *window)
367{
368 struct weston_wm *wm = window->wm;
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200369 struct weston_shell_interface *shell_interface =
370 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400371
372#define F(field) offsetof(struct weston_wm_window, field)
373 const struct {
374 xcb_atom_t atom;
375 xcb_atom_t type;
376 int offset;
377 } props[] = {
378 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
379 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
380 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
381 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700382 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500383 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400384 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
385 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300386 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400387 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300388 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400389 };
390#undef F
391
392 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
393 xcb_get_property_reply_t *reply;
394 void *p;
395 uint32_t *xid;
396 xcb_atom_t *atom;
397 uint32_t i;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400398
399 if (!window->properties_dirty)
400 return;
401 window->properties_dirty = 0;
402
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400403 for (i = 0; i < ARRAY_LENGTH(props); i++)
404 cookie[i] = xcb_get_property(wm->conn,
405 0, /* delete */
406 window->id,
407 props[i].atom,
408 XCB_ATOM_ANY, 0, 2048);
409
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300410 window->decorate = !window->override_redirect;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700411 window->size_hints.flags = 0;
412 window->motif_hints.flags = 0;
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700413 window->delete_window = 0;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700414
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400415 for (i = 0; i < ARRAY_LENGTH(props); i++) {
416 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
417 if (!reply)
418 /* Bad window, typically */
419 continue;
420 if (reply->type == XCB_ATOM_NONE) {
421 /* No such property */
422 free(reply);
423 continue;
424 }
425
426 p = ((char *) window + props[i].offset);
427
428 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300429 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400430 case XCB_ATOM_STRING:
431 /* FIXME: We're using this for both string and
432 utf8_string */
433 if (*(char **) p)
434 free(*(char **) p);
435
436 *(char **) p =
437 strndup(xcb_get_property_value(reply),
438 xcb_get_property_value_length(reply));
439 break;
440 case XCB_ATOM_WINDOW:
441 xid = xcb_get_property_value(reply);
442 *(struct weston_wm_window **) p =
443 hash_table_lookup(wm->window_hash, *xid);
444 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300445 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400446 case XCB_ATOM_ATOM:
447 atom = xcb_get_property_value(reply);
448 *(xcb_atom_t *) p = *atom;
449 break;
450 case TYPE_WM_PROTOCOLS:
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -0700451 atom = xcb_get_property_value(reply);
452 for (i = 0; i < reply->value_len; i++)
453 if (atom[i] == wm->atom.wm_delete_window)
454 window->delete_window = 1;
455 break;
456
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400457 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700458 case TYPE_WM_NORMAL_HINTS:
459 memcpy(&window->size_hints,
460 xcb_get_property_value(reply),
461 sizeof window->size_hints);
462 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500463 case TYPE_NET_WM_STATE:
464 window->fullscreen = 0;
465 atom = xcb_get_property_value(reply);
466 for (i = 0; i < reply->value_len; i++)
467 if (atom[i] == wm->atom.net_wm_state_fullscreen)
468 window->fullscreen = 1;
469 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400470 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700471 memcpy(&window->motif_hints,
472 xcb_get_property_value(reply),
473 sizeof window->motif_hints);
474 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS)
475 window->decorate =
476 window->motif_hints.decorations > 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400477 break;
478 default:
479 break;
480 }
481 free(reply);
482 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200483
484 if (window->shsurf && window->name)
485 shell_interface->set_title(window->shsurf, window->name);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500486 if (window->frame && window->name)
487 frame_set_title(window->frame, window->name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400488}
489
490static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400491weston_wm_window_get_frame_size(struct weston_wm_window *window,
492 int *width, int *height)
493{
494 struct theme *t = window->wm->theme;
495
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500496 if (window->fullscreen) {
497 *width = window->width;
498 *height = window->height;
499 } else if (window->decorate) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500500 *width = frame_width(window->frame);
501 *height = frame_height(window->frame);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400502 } else {
503 *width = window->width + t->margin * 2;
504 *height = window->height + t->margin * 2;
505 }
506}
507
508static void
509weston_wm_window_get_child_position(struct weston_wm_window *window,
510 int *x, int *y)
511{
512 struct theme *t = window->wm->theme;
513
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500514 if (window->fullscreen) {
515 *x = 0;
516 *y = 0;
517 } else if (window->decorate) {
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500518 frame_interior(window->frame, x, y, NULL, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400519 } else {
520 *x = t->margin;
521 *y = t->margin;
522 }
523}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400524
525static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500526weston_wm_window_send_configure_notify(struct weston_wm_window *window)
527{
528 xcb_configure_notify_event_t configure_notify;
529 struct weston_wm *wm = window->wm;
530 int x, y;
531
532 weston_wm_window_get_child_position(window, &x, &y);
533 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
534 configure_notify.pad0 = 0;
535 configure_notify.event = window->id;
536 configure_notify.window = window->id;
537 configure_notify.above_sibling = XCB_WINDOW_NONE;
538 configure_notify.x = x;
539 configure_notify.y = y;
540 configure_notify.width = window->width;
541 configure_notify.height = window->height;
542 configure_notify.border_width = 0;
543 configure_notify.override_redirect = 0;
544 configure_notify.pad1 = 0;
545
546 xcb_send_event(wm->conn, 0, window->id,
547 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
548 (char *) &configure_notify);
549}
550
551static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400552weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
553{
554 xcb_configure_request_event_t *configure_request =
555 (xcb_configure_request_event_t *) event;
556 struct weston_wm_window *window;
557 uint32_t mask, values[16];
558 int x, y, width, height, i = 0;
559
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400560 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
561 configure_request->window,
562 configure_request->x, configure_request->y,
563 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400564
565 window = hash_table_lookup(wm->window_hash, configure_request->window);
566
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500567 if (window->fullscreen) {
568 weston_wm_window_send_configure_notify(window);
569 return;
570 }
571
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400572 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
573 window->width = configure_request->width;
574 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
575 window->height = configure_request->height;
576
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500577 if (window->frame)
578 frame_resize_inside(window->frame, window->width, window->height);
579
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400580 weston_wm_window_get_child_position(window, &x, &y);
581 values[i++] = x;
582 values[i++] = y;
583 values[i++] = window->width;
584 values[i++] = window->height;
585 values[i++] = 0;
586 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
587 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
588 XCB_CONFIG_WINDOW_BORDER_WIDTH;
589 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
590 values[i++] = configure_request->sibling;
591 mask |= XCB_CONFIG_WINDOW_SIBLING;
592 }
593 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
594 values[i++] = configure_request->stack_mode;
595 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
596 }
597
598 xcb_configure_window(wm->conn, window->id, mask, values);
599
600 weston_wm_window_get_frame_size(window, &width, &height);
601 values[0] = width;
602 values[1] = height;
603 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
604 xcb_configure_window(wm->conn, window->frame_id, mask, values);
605
606 weston_wm_window_schedule_repaint(window);
607}
608
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400609static int
610our_resource(struct weston_wm *wm, uint32_t id)
611{
612 const xcb_setup_t *setup;
613
614 setup = xcb_get_setup(wm->conn);
615
616 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
617}
618
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400619static void
620weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
621{
622 xcb_configure_notify_event_t *configure_notify =
623 (xcb_configure_notify_event_t *) event;
624 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400625
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400626 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400627 configure_notify->window,
628 configure_notify->x, configure_notify->y,
629 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300630
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400631 window = hash_table_lookup(wm->window_hash, configure_notify->window);
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700632 window->x = configure_notify->x;
633 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700634 if (window->override_redirect) {
635 window->width = configure_notify->width;
636 window->height = configure_notify->height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500637 if (window->frame)
638 frame_resize_inside(window->frame,
639 window->width, window->height);
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700640 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400641}
642
643static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300644weston_wm_kill_client(struct wl_listener *listener, void *data)
645{
646 struct weston_surface *surface = data;
647 struct weston_wm_window *window = get_wm_window(surface);
648 char name[1024];
649
650 if (!window)
651 return;
652
653 gethostname(name, 1024);
654
655 /* this is only one heuristic to guess the PID of a client is valid,
656 * assuming it's compliant with icccm and ewmh. Non-compliants and
657 * remote applications of course fail. */
658 if (!strcmp(window->machine, name) && window->pid != 0)
659 kill(window->pid, SIGKILL);
660}
661
662static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400663weston_wm_window_activate(struct wl_listener *listener, void *data)
664{
665 struct weston_surface *surface = data;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200666 struct weston_wm_window *window = NULL;
Scott Moreau85ecac02012-05-21 15:49:14 -0600667 struct weston_wm *wm =
668 container_of(listener, struct weston_wm, activate_listener);
669 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400670
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200671 if (surface) {
672 window = get_wm_window(surface);
673 }
674
Scott Moreau85ecac02012-05-21 15:49:14 -0600675 if (window) {
676 client_message.response_type = XCB_CLIENT_MESSAGE;
677 client_message.format = 32;
678 client_message.window = window->id;
679 client_message.type = wm->atom.wm_protocols;
680 client_message.data.data32[0] = wm->atom.wm_take_focus;
681 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
682
683 xcb_send_event(wm->conn, 0, window->id,
684 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
685 (char *) &client_message);
686
687 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
688 window->id, XCB_TIME_CURRENT_TIME);
689 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400690 xcb_set_input_focus (wm->conn,
691 XCB_INPUT_FOCUS_POINTER_ROOT,
692 XCB_NONE,
693 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600694 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400695
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500696 if (wm->focus_window) {
697 frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400698 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500699 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400700 wm->focus_window = window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500701 if (wm->focus_window) {
702 frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400703 weston_wm_window_schedule_repaint(wm->focus_window);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500704 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400705}
706
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300707static void
708weston_wm_window_transform(struct wl_listener *listener, void *data)
709{
710 struct weston_surface *surface = data;
711 struct weston_wm_window *window = get_wm_window(surface);
712 struct weston_wm *wm =
713 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300714 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300715
716 if (!window || !wm)
717 return;
718
719 if (!weston_surface_is_mapped(surface))
720 return;
721
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700722 if (window->x != surface->geometry.x ||
723 window->y != surface->geometry.y) {
724 values[0] = surface->geometry.x;
725 values[1] = surface->geometry.y;
726 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300727
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700728 xcb_configure_window(wm->conn, window->frame_id, mask, values);
729 xcb_flush(wm->conn);
730 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300731}
732
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400733#define ICCCM_WITHDRAWN_STATE 0
734#define ICCCM_NORMAL_STATE 1
735#define ICCCM_ICONIC_STATE 3
736
737static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500738weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400739{
740 struct weston_wm *wm = window->wm;
741 uint32_t property[2];
742
743 property[0] = state;
744 property[1] = XCB_WINDOW_NONE;
745
746 xcb_change_property(wm->conn,
747 XCB_PROP_MODE_REPLACE,
748 window->id,
749 wm->atom.wm_state,
750 wm->atom.wm_state,
751 32, /* format */
752 2, property);
753}
754
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400755static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500756weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
757{
758 struct weston_wm *wm = window->wm;
759 uint32_t property[1];
760 int i;
761
762 i = 0;
763 if (window->fullscreen)
764 property[i++] = wm->atom.net_wm_state_fullscreen;
765
766 xcb_change_property(wm->conn,
767 XCB_PROP_MODE_REPLACE,
768 window->id,
769 wm->atom.net_wm_state,
770 XCB_ATOM_ATOM,
771 32, /* format */
772 i, property);
773}
774
775static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700776weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400777{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700778 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400779 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400780 int x, y, width, height;
781
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500782 window->frame = frame_create(window->wm->theme,
783 window->width, window->height,
784 FRAME_BUTTON_CLOSE, window->name);
785 frame_resize_inside(window->frame, window->width, window->height);
786
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400787 weston_wm_window_get_frame_size(window, &width, &height);
788 weston_wm_window_get_child_position(window, &x, &y);
789
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400790 values[0] = wm->screen->black_pixel;
791 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400792 XCB_EVENT_MASK_KEY_PRESS |
793 XCB_EVENT_MASK_KEY_RELEASE |
794 XCB_EVENT_MASK_BUTTON_PRESS |
795 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400796 XCB_EVENT_MASK_POINTER_MOTION |
797 XCB_EVENT_MASK_ENTER_WINDOW |
798 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400799 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400800 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400801 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400802
803 window->frame_id = xcb_generate_id(wm->conn);
804 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400805 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400806 window->frame_id,
807 wm->screen->root,
808 0, 0,
809 width, height,
810 0,
811 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400812 wm->visual_id,
813 XCB_CW_BORDER_PIXEL |
814 XCB_CW_EVENT_MASK |
815 XCB_CW_COLORMAP, values);
816
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400817 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
818
819 values[0] = 0;
820 xcb_configure_window(wm->conn, window->id,
821 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
822
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400823 window->cairo_surface =
824 cairo_xcb_surface_create_with_xrender_format(wm->conn,
825 wm->screen,
826 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400827 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400828 width, height);
829
830 hash_table_insert(wm->window_hash, window->frame_id, window);
831}
832
833static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700834weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
835{
836 xcb_map_request_event_t *map_request =
837 (xcb_map_request_event_t *) event;
838 struct weston_wm_window *window;
839
840 if (our_resource(wm, map_request->window)) {
841 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
842 map_request->window);
843 return;
844 }
845
846 window = hash_table_lookup(wm->window_hash, map_request->window);
847
848 weston_wm_window_read_properties(window);
849
850 if (window->frame_id == XCB_WINDOW_NONE)
851 weston_wm_window_create_frame(window);
852
853 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
854 window->id, window, window->frame_id);
855
856 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
857 weston_wm_window_set_net_wm_state(window);
858
859 xcb_map_window(wm->conn, map_request->window);
860 xcb_map_window(wm->conn, window->frame_id);
861}
862
863static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400864weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
865{
866 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
867
868 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400869 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
870 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400871 return;
872 }
873
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400874 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400875}
876
877static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400878weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
879{
880 xcb_unmap_notify_event_t *unmap_notify =
881 (xcb_unmap_notify_event_t *) event;
882 struct weston_wm_window *window;
883
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400884 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
885 unmap_notify->window,
886 unmap_notify->event,
887 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400888
889 if (our_resource(wm, unmap_notify->window))
890 return;
891
MoD31700122013-06-11 19:58:55 -0500892 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400893 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
894 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400895 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400896
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400897 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400898 if (wm->focus_window == window)
899 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400900 if (window->surface)
901 wl_list_remove(&window->surface_destroy_listener.link);
902 window->surface = NULL;
Giulio Camuffo85739ea2013-09-17 16:43:45 +0200903 window->shsurf = NULL;
Kristian Høgsbergab6d6672013-09-03 16:38:51 -0700904 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400905}
906
907static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400908weston_wm_window_draw_decoration(void *data)
909{
910 struct weston_wm_window *window = data;
911 struct weston_wm *wm = window->wm;
912 struct theme *t = wm->theme;
913 cairo_t *cr;
914 int x, y, width, height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500915 int32_t input_x, input_y, input_w, input_h;
916
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400917 uint32_t flags = 0;
918
919 weston_wm_window_read_properties(window);
920
921 window->repaint_source = NULL;
922
923 weston_wm_window_get_frame_size(window, &width, &height);
924 weston_wm_window_get_child_position(window, &x, &y);
925
926 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
927 cr = cairo_create(window->cairo_surface);
928
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500929 if (window->fullscreen) {
930 /* nothing */
931 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400932 if (wm->focus_window == window)
933 flags |= THEME_FRAME_ACTIVE;
934
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500935 frame_repaint(window->frame, cr);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400936 } else {
937 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
938 cairo_set_source_rgba(cr, 0, 0, 0, 0);
939 cairo_paint(cr);
940
941 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
942 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
943 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
944 }
945
946 cairo_destroy(cr);
947
948 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700949 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500950 if(window->has_alpha) {
951 pixman_region32_init(&window->surface->pending.opaque);
952 } else {
953 /* We leave an extra pixel around the X window area to
954 * make sure we don't sample from the undefined alpha
955 * channel when filtering. */
956 pixman_region32_init_rect(&window->surface->pending.opaque,
957 x - 1, y - 1,
958 window->width + 2,
959 window->height + 2);
960 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200961 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500962 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400963
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500964 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500965 pixman_region32_fini(&window->surface->pending.input);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500966 frame_input_rect(window->frame, &input_x, &input_y,
967 &input_w, &input_h);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500968 pixman_region32_init_rect(&window->surface->pending.input,
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -0500969 input_x, input_y, input_w, input_h);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400970 }
971}
972
973static void
974weston_wm_window_schedule_repaint(struct weston_wm_window *window)
975{
976 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300977 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400978
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400979 if (window->frame_id == XCB_WINDOW_NONE) {
980 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300981 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700982 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500983 if(window->has_alpha) {
984 pixman_region32_init(&window->surface->pending.opaque);
985 } else {
986 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
987 width, height);
988 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200989 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400990 }
991 return;
992 }
993
994 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400995 return;
996
997 window->repaint_source =
998 wl_event_loop_add_idle(wm->server->loop,
999 weston_wm_window_draw_decoration,
1000 window);
1001}
1002
1003static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001004weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1005{
1006 xcb_property_notify_event_t *property_notify =
1007 (xcb_property_notify_event_t *) event;
1008 struct weston_wm_window *window;
1009
1010 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +00001011 if (!window)
1012 return;
1013
1014 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001015
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001016 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001017 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001018 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001019 else
1020 read_and_dump_property(wm, property_notify->window,
1021 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001022
1023 if (property_notify->atom == wm->atom.net_wm_name ||
1024 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001025 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001026}
1027
1028static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001029weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001030 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001031{
1032 struct weston_wm_window *window;
1033 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001034 xcb_get_geometry_cookie_t geometry_cookie;
1035 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001036
Peter Huttererf3d62272013-08-08 11:57:05 +10001037 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001038 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001039 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001040 return;
1041 }
1042
MoD384a11a2013-06-22 11:04:21 -05001043 geometry_cookie = xcb_get_geometry(wm->conn, id);
1044
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001045 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1046 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1047
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001048 window->wm = wm;
1049 window->id = id;
1050 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001051 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001052 window->width = width;
1053 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001054 window->x = x;
1055 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001056
MoD384a11a2013-06-22 11:04:21 -05001057 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1058 /* technically we should use XRender and check the visual format's
1059 alpha_mask, but checking depth is simpler and works in all known cases */
1060 if(geometry_reply != NULL)
1061 window->has_alpha = geometry_reply->depth == 32;
1062 free(geometry_reply);
1063
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001064 hash_table_insert(wm->window_hash, id, window);
1065}
1066
1067static void
1068weston_wm_window_destroy(struct weston_wm_window *window)
1069{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001070 struct weston_wm *wm = window->wm;
1071
1072 if (window->repaint_source)
1073 wl_event_source_remove(window->repaint_source);
1074 if (window->cairo_surface)
1075 cairo_surface_destroy(window->cairo_surface);
1076
1077 if (window->frame_id) {
1078 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1079 xcb_destroy_window(wm->conn, window->frame_id);
1080 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1081 hash_table_remove(wm->window_hash, window->frame_id);
1082 window->frame_id = XCB_WINDOW_NONE;
1083 }
1084
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001085 hash_table_remove(window->wm->window_hash, window->id);
1086 free(window);
1087}
1088
1089static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001090weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1091{
1092 xcb_create_notify_event_t *create_notify =
1093 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001094
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001095 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1096 create_notify->window,
1097 create_notify->width, create_notify->height,
1098 create_notify->override_redirect ? ", override" : "",
1099 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001100
1101 if (our_resource(wm, create_notify->window))
1102 return;
1103
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001104 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001105 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001106 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001107 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001108}
1109
1110static void
1111weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1112{
1113 xcb_destroy_notify_event_t *destroy_notify =
1114 (xcb_destroy_notify_event_t *) event;
1115 struct weston_wm_window *window;
1116
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001117 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1118 destroy_notify->window,
1119 destroy_notify->event,
1120 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001121
1122 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001123 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001124
1125 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001126 weston_wm_window_destroy(window);
1127}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001128
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001129static void
1130weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1131{
1132 xcb_reparent_notify_event_t *reparent_notify =
1133 (xcb_reparent_notify_event_t *) event;
1134 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001135
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001136 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1137 reparent_notify->window,
1138 reparent_notify->parent,
1139 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001140
1141 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001142 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001143 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001144 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001145 } else if (!our_resource(wm, reparent_notify->parent)) {
1146 window = hash_table_lookup(wm->window_hash,
1147 reparent_notify->window);
1148 weston_wm_window_destroy(window);
1149 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001150}
1151
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001152struct weston_seat *
1153weston_wm_pick_seat(struct weston_wm *wm)
1154{
1155 return container_of(wm->server->compositor->seat_list.next,
1156 struct weston_seat, link);
1157}
1158
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001159static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001160weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1161 xcb_client_message_event_t *client_message)
1162{
1163 static const int map[] = {
1164 THEME_LOCATION_RESIZING_TOP_LEFT,
1165 THEME_LOCATION_RESIZING_TOP,
1166 THEME_LOCATION_RESIZING_TOP_RIGHT,
1167 THEME_LOCATION_RESIZING_RIGHT,
1168 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1169 THEME_LOCATION_RESIZING_BOTTOM,
1170 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1171 THEME_LOCATION_RESIZING_LEFT
1172 };
1173
1174 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001175 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001176 int detail;
1177 struct weston_shell_interface *shell_interface =
1178 &wm->server->compositor->shell_interface;
1179
Kristian Høgsberge3148752013-05-06 23:19:49 -04001180 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001181 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001182 return;
1183
1184 detail = client_message->data.data32[2];
1185 switch (detail) {
1186 case _NET_WM_MOVERESIZE_MOVE:
1187 shell_interface->move(window->shsurf, seat);
1188 break;
1189 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1190 case _NET_WM_MOVERESIZE_SIZE_TOP:
1191 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1192 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1193 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1194 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1195 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1196 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1197 shell_interface->resize(window->shsurf, seat, map[detail]);
1198 break;
1199 case _NET_WM_MOVERESIZE_CANCEL:
1200 break;
1201 }
1202}
1203
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001204#define _NET_WM_STATE_REMOVE 0
1205#define _NET_WM_STATE_ADD 1
1206#define _NET_WM_STATE_TOGGLE 2
1207
1208static int
1209update_state(int action, int *state)
1210{
1211 int new_state, changed;
1212
1213 switch (action) {
1214 case _NET_WM_STATE_REMOVE:
1215 new_state = 0;
1216 break;
1217 case _NET_WM_STATE_ADD:
1218 new_state = 1;
1219 break;
1220 case _NET_WM_STATE_TOGGLE:
1221 new_state = !*state;
1222 break;
1223 default:
1224 return 0;
1225 }
1226
1227 changed = (*state != new_state);
1228 *state = new_state;
1229
1230 return changed;
1231}
1232
1233static void
1234weston_wm_window_configure(void *data);
1235
1236static void
1237weston_wm_window_handle_state(struct weston_wm_window *window,
1238 xcb_client_message_event_t *client_message)
1239{
1240 struct weston_wm *wm = window->wm;
1241 struct weston_shell_interface *shell_interface =
1242 &wm->server->compositor->shell_interface;
1243 uint32_t action, property;
1244
1245 action = client_message->data.data32[0];
1246 property = client_message->data.data32[1];
1247
1248 if (property == wm->atom.net_wm_state_fullscreen &&
1249 update_state(action, &window->fullscreen)) {
1250 weston_wm_window_set_net_wm_state(window);
1251 if (window->fullscreen) {
1252 window->saved_width = window->width;
1253 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001254
1255 if (window->shsurf)
1256 shell_interface->set_fullscreen(window->shsurf,
1257 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1258 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001259 } else {
1260 shell_interface->set_toplevel(window->shsurf);
1261 window->width = window->saved_width;
1262 window->height = window->saved_height;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001263 if (window->frame)
1264 frame_resize_inside(window->frame,
1265 window->width,
1266 window->height);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001267 weston_wm_window_configure(window);
1268 }
1269 }
1270}
1271
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001272static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001273weston_wm_handle_client_message(struct weston_wm *wm,
1274 xcb_generic_event_t *event)
1275{
1276 xcb_client_message_event_t *client_message =
1277 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001278 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001279
1280 window = hash_table_lookup(wm->window_hash, client_message->window);
1281
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001282 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1283 get_atom_name(wm->conn, client_message->type),
1284 client_message->data.data32[0],
1285 client_message->data.data32[1],
1286 client_message->data.data32[2],
1287 client_message->data.data32[3],
1288 client_message->data.data32[4],
1289 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001290
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001291 if (client_message->type == wm->atom.net_wm_moveresize)
1292 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001293 else if (client_message->type == wm->atom.net_wm_state)
1294 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001295}
1296
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001297enum cursor_type {
1298 XWM_CURSOR_TOP,
1299 XWM_CURSOR_BOTTOM,
1300 XWM_CURSOR_LEFT,
1301 XWM_CURSOR_RIGHT,
1302 XWM_CURSOR_TOP_LEFT,
1303 XWM_CURSOR_TOP_RIGHT,
1304 XWM_CURSOR_BOTTOM_LEFT,
1305 XWM_CURSOR_BOTTOM_RIGHT,
1306 XWM_CURSOR_LEFT_PTR,
1307};
1308
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001309/*
1310 * The following correspondences between file names and cursors was copied
1311 * from: https://bugs.kde.org/attachment.cgi?id=67313
1312 */
1313
1314static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001315 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001316 "sw-resize",
1317 "size_bdiag"
1318};
1319
1320static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001321 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001322 "se-resize",
1323 "size_fdiag"
1324};
1325
1326static const char *bottom_sides[] = {
1327 "bottom_side",
1328 "s-resize",
1329 "size_ver"
1330};
1331
1332static const char *left_ptrs[] = {
1333 "left_ptr",
1334 "default",
1335 "top_left_arrow",
1336 "left-arrow"
1337};
1338
1339static const char *left_sides[] = {
1340 "left_side",
1341 "w-resize",
1342 "size_hor"
1343};
1344
1345static const char *right_sides[] = {
1346 "right_side",
1347 "e-resize",
1348 "size_hor"
1349};
1350
1351static const char *top_left_corners[] = {
1352 "top_left_corner",
1353 "nw-resize",
1354 "size_fdiag"
1355};
1356
1357static const char *top_right_corners[] = {
1358 "top_right_corner",
1359 "ne-resize",
1360 "size_bdiag"
1361};
1362
1363static const char *top_sides[] = {
1364 "top_side",
1365 "n-resize",
1366 "size_ver"
1367};
1368
1369struct cursor_alternatives {
1370 const char **names;
1371 size_t count;
1372};
1373
1374static const struct cursor_alternatives cursors[] = {
1375 {top_sides, ARRAY_LENGTH(top_sides)},
1376 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1377 {left_sides, ARRAY_LENGTH(left_sides)},
1378 {right_sides, ARRAY_LENGTH(right_sides)},
1379 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1380 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1381 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1382 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1383 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001384};
1385
1386static void
1387weston_wm_create_cursors(struct weston_wm *wm)
1388{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001389 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001390 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001391 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001392
1393 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1394 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001395 for (j = 0; j < cursors[i].count; j++) {
1396 name = cursors[i].names[j];
1397 wm->cursors[i] =
1398 xcb_cursor_library_load_cursor(wm, name);
1399 if (wm->cursors[i] != (xcb_cursor_t)-1)
1400 break;
1401 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001402 }
1403
1404 wm->last_cursor = -1;
1405}
1406
1407static void
1408weston_wm_destroy_cursors(struct weston_wm *wm)
1409{
1410 uint8_t i;
1411
1412 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1413 xcb_free_cursor(wm->conn, wm->cursors[i]);
1414
1415 free(wm->cursors);
1416}
1417
1418static int
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001419get_cursor_for_location(enum theme_location location)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001420{
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001421 // int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001422
1423 switch (location) {
1424 case THEME_LOCATION_RESIZING_TOP:
1425 return XWM_CURSOR_TOP;
1426 case THEME_LOCATION_RESIZING_BOTTOM:
1427 return XWM_CURSOR_BOTTOM;
1428 case THEME_LOCATION_RESIZING_LEFT:
1429 return XWM_CURSOR_LEFT;
1430 case THEME_LOCATION_RESIZING_RIGHT:
1431 return XWM_CURSOR_RIGHT;
1432 case THEME_LOCATION_RESIZING_TOP_LEFT:
1433 return XWM_CURSOR_TOP_LEFT;
1434 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1435 return XWM_CURSOR_TOP_RIGHT;
1436 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1437 return XWM_CURSOR_BOTTOM_LEFT;
1438 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1439 return XWM_CURSOR_BOTTOM_RIGHT;
1440 case THEME_LOCATION_EXTERIOR:
1441 case THEME_LOCATION_TITLEBAR:
1442 default:
1443 return XWM_CURSOR_LEFT_PTR;
1444 }
1445}
1446
1447static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001448weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1449 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001450{
1451 uint32_t cursor_value_list;
1452
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001453 if (wm->last_cursor == cursor)
1454 return;
1455
1456 wm->last_cursor = cursor;
1457
1458 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001459 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001460 XCB_CW_CURSOR, &cursor_value_list);
1461 xcb_flush(wm->conn);
1462}
1463
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001464static void
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001465weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time)
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001466{
1467 xcb_client_message_event_t client_message;
1468
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001469 if (window->delete_window) {
1470 client_message.response_type = XCB_CLIENT_MESSAGE;
1471 client_message.format = 32;
1472 client_message.window = window->id;
1473 client_message.type = window->wm->atom.wm_protocols;
1474 client_message.data.data32[0] =
1475 window->wm->atom.wm_delete_window;
1476 client_message.data.data32[1] = time;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001477
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001478 xcb_send_event(window->wm->conn, 0, window->id,
1479 XCB_EVENT_MASK_NO_EVENT,
1480 (char *) &client_message);
1481 } else {
1482 xcb_kill_client(window->wm->conn, window->id);
1483 }
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001484}
1485
1486static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001487weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1488{
1489 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1490 struct weston_shell_interface *shell_interface =
1491 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001492 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001493 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001494 enum theme_location location;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001495 enum frame_button_state button_state;
1496 uint32_t button_id;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001497
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001498 wm_log("XCB_BUTTON_%s (detail %d)\n",
1499 button->response_type == XCB_BUTTON_PRESS ?
1500 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001501
1502 window = hash_table_lookup(wm->window_hash, button->event);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001503 if (!window || !window->decorate)
1504 return;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001505
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001506 if (button->detail != 1 && button->detail != 2)
1507 return;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001508
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001509 button_state = button->response_type == XCB_BUTTON_PRESS ?
1510 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1511 button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT;
1512
1513 location = frame_pointer_button(window->frame, NULL,
1514 button_id, button_state);
1515 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1516 weston_wm_window_schedule_repaint(window);
1517
1518 if (frame_status(window->frame) & FRAME_STATUS_MOVE) {
1519 shell_interface->move(window->shsurf, seat);
1520 frame_status_clear(window->frame, FRAME_STATUS_MOVE);
1521 }
1522
1523 if (frame_status(window->frame) & FRAME_STATUS_RESIZE) {
1524 shell_interface->resize(window->shsurf, seat, location);
1525 frame_status_clear(window->frame, FRAME_STATUS_RESIZE);
1526 }
1527
1528 if (frame_status(window->frame) & FRAME_STATUS_CLOSE) {
Kristian Høgsberg2cd6da12013-10-13 22:11:07 -07001529 weston_wm_window_close(window, button->time);
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001530 frame_status_clear(window->frame, FRAME_STATUS_CLOSE);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001531 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001532}
1533
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001534static void
1535weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1536{
1537 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1538 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001539 enum theme_location location;
1540 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001541
1542 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001543 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001544 return;
1545
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001546 location = frame_pointer_motion(window->frame, NULL,
1547 motion->event_x, motion->event_y);
1548 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1549 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001550
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001551 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001552 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001553}
1554
1555static void
1556weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1557{
1558 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1559 struct weston_wm_window *window;
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001560 enum theme_location location;
1561 int cursor;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001562
1563 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001564 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001565 return;
1566
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001567 location = frame_pointer_enter(window->frame, NULL,
1568 enter->event_x, enter->event_y);
1569 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1570 weston_wm_window_schedule_repaint(window);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001571
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001572 cursor = get_cursor_for_location(location);
Tiago Vignattic1903232012-07-16 12:15:37 -04001573 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001574}
1575
1576static void
1577weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1578{
1579 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1580 struct weston_wm_window *window;
1581
1582 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001583 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001584 return;
1585
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05001586 frame_pointer_leave(window->frame, NULL);
1587 if (frame_status(window->frame) & FRAME_STATUS_REPAINT)
1588 weston_wm_window_schedule_repaint(window);
1589
Tiago Vignattic1903232012-07-16 12:15:37 -04001590 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001591}
1592
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001593static int
1594weston_wm_handle_event(int fd, uint32_t mask, void *data)
1595{
1596 struct weston_wm *wm = data;
1597 xcb_generic_event_t *event;
1598 int count = 0;
1599
1600 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1601 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001602 free(event);
1603 count++;
1604 continue;
1605 }
1606
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001607 if (weston_wm_handle_dnd_event(wm, event)) {
1608 free(event);
1609 count++;
1610 continue;
1611 }
1612
MoD31700122013-06-11 19:58:55 -05001613 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001614 case XCB_BUTTON_PRESS:
1615 case XCB_BUTTON_RELEASE:
1616 weston_wm_handle_button(wm, event);
1617 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001618 case XCB_ENTER_NOTIFY:
1619 weston_wm_handle_enter(wm, event);
1620 break;
1621 case XCB_LEAVE_NOTIFY:
1622 weston_wm_handle_leave(wm, event);
1623 break;
1624 case XCB_MOTION_NOTIFY:
1625 weston_wm_handle_motion(wm, event);
1626 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001627 case XCB_CREATE_NOTIFY:
1628 weston_wm_handle_create_notify(wm, event);
1629 break;
1630 case XCB_MAP_REQUEST:
1631 weston_wm_handle_map_request(wm, event);
1632 break;
1633 case XCB_MAP_NOTIFY:
1634 weston_wm_handle_map_notify(wm, event);
1635 break;
1636 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001637 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001638 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001639 case XCB_REPARENT_NOTIFY:
1640 weston_wm_handle_reparent_notify(wm, event);
1641 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001642 case XCB_CONFIGURE_REQUEST:
1643 weston_wm_handle_configure_request(wm, event);
1644 break;
1645 case XCB_CONFIGURE_NOTIFY:
1646 weston_wm_handle_configure_notify(wm, event);
1647 break;
1648 case XCB_DESTROY_NOTIFY:
1649 weston_wm_handle_destroy_notify(wm, event);
1650 break;
1651 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001652 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001653 break;
1654 case XCB_PROPERTY_NOTIFY:
1655 weston_wm_handle_property_notify(wm, event);
1656 break;
1657 case XCB_CLIENT_MESSAGE:
1658 weston_wm_handle_client_message(wm, event);
1659 break;
1660 }
1661
1662 free(event);
1663 count++;
1664 }
1665
1666 xcb_flush(wm->conn);
1667
1668 return count;
1669}
1670
1671static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001672weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1673{
1674 xcb_depth_iterator_t d_iter;
1675 xcb_visualtype_iterator_t vt_iter;
1676 xcb_visualtype_t *visualtype;
1677
1678 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1679 visualtype = NULL;
1680 while (d_iter.rem > 0) {
1681 if (d_iter.data->depth == 32) {
1682 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1683 visualtype = vt_iter.data;
1684 break;
1685 }
1686
1687 xcb_depth_next(&d_iter);
1688 }
1689
1690 if (visualtype == NULL) {
1691 weston_log("no 32 bit visualtype\n");
1692 return;
1693 }
1694
1695 wm->visual_id = visualtype->visual_id;
1696 wm->colormap = xcb_generate_id(wm->conn);
1697 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1698 wm->colormap, wm->screen->root, wm->visual_id);
1699}
1700
1701static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001702weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001703{
1704
1705#define F(field) offsetof(struct weston_wm, field)
1706
1707 static const struct { const char *name; int offset; } atoms[] = {
1708 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001709 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001710 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1711 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001712 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001713 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001714 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001715 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001716 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001717 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001718 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1719 { "_NET_WM_STATE", F(atom.net_wm_state) },
1720 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1721 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1722 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1723 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1724
1725 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1726 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1727 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1728 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1729 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1730 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1731 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001732 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1733 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001734 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1735 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1736 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1737 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1738 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1739
1740 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1741 { "_NET_SUPPORTING_WM_CHECK",
1742 F(atom.net_supporting_wm_check) },
1743 { "_NET_SUPPORTED", F(atom.net_supported) },
1744 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1745 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001746 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001747 { "TARGETS", F(atom.targets) },
1748 { "UTF8_STRING", F(atom.utf8_string) },
1749 { "_WL_SELECTION", F(atom.wl_selection) },
1750 { "INCR", F(atom.incr) },
1751 { "TIMESTAMP", F(atom.timestamp) },
1752 { "MULTIPLE", F(atom.multiple) },
1753 { "UTF8_STRING" , F(atom.utf8_string) },
1754 { "COMPOUND_TEXT", F(atom.compound_text) },
1755 { "TEXT", F(atom.text) },
1756 { "STRING", F(atom.string) },
1757 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1758 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001759 { "XdndSelection", F(atom.xdnd_selection) },
1760 { "XdndAware", F(atom.xdnd_aware) },
1761 { "XdndEnter", F(atom.xdnd_enter) },
1762 { "XdndLeave", F(atom.xdnd_leave) },
1763 { "XdndDrop", F(atom.xdnd_drop) },
1764 { "XdndStatus", F(atom.xdnd_status) },
1765 { "XdndFinished", F(atom.xdnd_finished) },
1766 { "XdndTypeList", F(atom.xdnd_type_list) },
1767 { "XdndActionCopy", F(atom.xdnd_action_copy) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001768 };
1769#undef F
1770
1771 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1772 xcb_xfixes_query_version_reply_t *xfixes_reply;
1773 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1774 xcb_intern_atom_reply_t *reply;
1775 xcb_render_query_pict_formats_reply_t *formats_reply;
1776 xcb_render_query_pict_formats_cookie_t formats_cookie;
1777 xcb_render_pictforminfo_t *formats;
1778 uint32_t i;
1779
1780 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07001781 xcb_prefetch_extension_data (wm->conn, &xcb_composite_id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001782
1783 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1784
1785 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1786 cookies[i] = xcb_intern_atom (wm->conn, 0,
1787 strlen(atoms[i].name),
1788 atoms[i].name);
1789
1790 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1791 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1792 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1793 free(reply);
1794 }
1795
1796 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1797 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001798 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001799
1800 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1801 XCB_XFIXES_MAJOR_VERSION,
1802 XCB_XFIXES_MINOR_VERSION);
1803 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1804 xfixes_cookie, NULL);
1805
Martin Minarik6d118362012-06-07 18:01:59 +02001806 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001807 xfixes_reply->major_version, xfixes_reply->minor_version);
1808
1809 free(xfixes_reply);
1810
1811 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1812 formats_cookie, 0);
1813 if (formats_reply == NULL)
1814 return;
1815
1816 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001817 for (i = 0; i < formats_reply->num_formats; i++) {
1818 if (formats[i].direct.red_mask != 0xff &&
1819 formats[i].direct.red_shift != 16)
1820 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001821 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1822 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001823 wm->format_rgb = formats[i];
1824 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1825 formats[i].depth == 32 &&
1826 formats[i].direct.alpha_mask == 0xff &&
1827 formats[i].direct.alpha_shift == 24)
1828 wm->format_rgba = formats[i];
1829 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001830
1831 free(formats_reply);
1832}
1833
1834static void
1835weston_wm_create_wm_window(struct weston_wm *wm)
1836{
1837 static const char name[] = "Weston WM";
1838
1839 wm->wm_window = xcb_generate_id(wm->conn);
1840 xcb_create_window(wm->conn,
1841 XCB_COPY_FROM_PARENT,
1842 wm->wm_window,
1843 wm->screen->root,
1844 0, 0,
1845 10, 10,
1846 0,
1847 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1848 wm->screen->root_visual,
1849 0, NULL);
1850
1851 xcb_change_property(wm->conn,
1852 XCB_PROP_MODE_REPLACE,
1853 wm->wm_window,
1854 wm->atom.net_supporting_wm_check,
1855 XCB_ATOM_WINDOW,
1856 32, /* format */
1857 1, &wm->wm_window);
1858
1859 xcb_change_property(wm->conn,
1860 XCB_PROP_MODE_REPLACE,
1861 wm->wm_window,
1862 wm->atom.net_wm_name,
1863 wm->atom.utf8_string,
1864 8, /* format */
1865 strlen(name), name);
1866
1867 xcb_change_property(wm->conn,
1868 XCB_PROP_MODE_REPLACE,
1869 wm->screen->root,
1870 wm->atom.net_supporting_wm_check,
1871 XCB_ATOM_WINDOW,
1872 32, /* format */
1873 1, &wm->wm_window);
1874
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001875 /* Claim the WM_S0 selection even though we don't suport
1876 * the --replace functionality. */
1877 xcb_set_selection_owner(wm->conn,
1878 wm->wm_window,
1879 wm->atom.wm_s0,
1880 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001881
1882 xcb_set_selection_owner(wm->conn,
1883 wm->wm_window,
1884 wm->atom.net_wm_cm_s0,
1885 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001886}
1887
1888struct weston_wm *
1889weston_wm_create(struct weston_xserver *wxs)
1890{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001891 struct weston_wm *wm;
1892 struct wl_event_loop *loop;
1893 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001894 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001895 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001896 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001897
Peter Huttererf3d62272013-08-08 11:57:05 +10001898 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001899 if (wm == NULL)
1900 return NULL;
1901
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001902 wm->server = wxs;
1903 wm->window_hash = hash_table_create();
1904 if (wm->window_hash == NULL) {
1905 free(wm);
1906 return NULL;
1907 }
1908
1909 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001910 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001911 hash_table_destroy(wm->window_hash);
1912 free(wm);
1913 return NULL;
1914 }
1915
1916 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001917 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001918 close(sv[1]);
1919
1920 /* xcb_connect_to_fd takes ownership of the fd. */
1921 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1922 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001923 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001924 close(sv[0]);
1925 hash_table_destroy(wm->window_hash);
1926 free(wm);
1927 return NULL;
1928 }
1929
1930 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1931 wm->screen = s.data;
1932
1933 loop = wl_display_get_event_loop(wxs->wl_display);
1934 wm->source =
1935 wl_event_loop_add_fd(loop, sv[0],
1936 WL_EVENT_READABLE,
1937 weston_wm_handle_event, wm);
1938 wl_event_source_check(wm->source);
1939
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001940 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001941 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001942
1943 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001944 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1945 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1946 XCB_EVENT_MASK_PROPERTY_CHANGE;
1947 xcb_change_window_attributes(wm->conn, wm->screen->root,
1948 XCB_CW_EVENT_MASK, values);
Kristian Høgsbergbcfd07b2013-10-11 16:48:19 -07001949
1950 xcb_composite_redirect_subwindows(wm->conn, wm->screen->root,
1951 XCB_COMPOSITE_REDIRECT_MANUAL);
1952
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001953 wm->theme = theme_create();
1954
1955 weston_wm_create_wm_window(wm);
1956
1957 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001958 supported[1] = wm->atom.net_wm_state;
1959 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001960 xcb_change_property(wm->conn,
1961 XCB_PROP_MODE_REPLACE,
1962 wm->screen->root,
1963 wm->atom.net_supported,
1964 XCB_ATOM_ATOM,
1965 32, /* format */
1966 ARRAY_LENGTH(supported), supported);
1967
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001968 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001969
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001970 weston_wm_dnd_init(wm);
1971
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001972 xcb_flush(wm->conn);
1973
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001974 wm->activate_listener.notify = weston_wm_window_activate;
1975 wl_signal_add(&wxs->compositor->activate_signal,
1976 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001977 wm->transform_listener.notify = weston_wm_window_transform;
1978 wl_signal_add(&wxs->compositor->transform_signal,
1979 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001980 wm->kill_listener.notify = weston_wm_kill_client;
1981 wl_signal_add(&wxs->compositor->kill_signal,
1982 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001983
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001984 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001985 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001986
Martin Minarik6d118362012-06-07 18:01:59 +02001987 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001988
1989 return wm;
1990}
1991
1992void
1993weston_wm_destroy(struct weston_wm *wm)
1994{
1995 /* FIXME: Free windows in hash. */
1996 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001997 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001998 xcb_disconnect(wm->conn);
1999 wl_event_source_remove(wm->source);
2000 wl_list_remove(&wm->selection_listener.link);
2001 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03002002 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01002003 wl_list_remove(&wm->transform_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002004
2005 free(wm);
2006}
2007
2008static void
2009surface_destroy(struct wl_listener *listener, void *data)
2010{
2011 struct weston_wm_window *window =
2012 container_of(listener,
2013 struct weston_wm_window, surface_destroy_listener);
2014
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002015 wm_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07002016
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002017 /* This should have been freed by the shell.
2018 Don't try to use it later. */
2019 window->shsurf = NULL;
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07002020 window->surface = NULL;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002021}
2022
2023static struct weston_wm_window *
2024get_wm_window(struct weston_surface *surface)
2025{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002026 struct wl_listener *listener;
2027
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002028 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002029 if (listener)
2030 return container_of(listener, struct weston_wm_window,
2031 surface_destroy_listener);
2032
2033 return NULL;
2034}
2035
2036static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002037weston_wm_window_configure(void *data)
2038{
2039 struct weston_wm_window *window = data;
2040 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002041 uint32_t values[4];
2042 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002043
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002044 weston_wm_window_get_child_position(window, &x, &y);
2045 values[0] = x;
2046 values[1] = y;
2047 values[2] = window->width;
2048 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002049 xcb_configure_window(wm->conn,
2050 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002051 XCB_CONFIG_WINDOW_X |
2052 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002053 XCB_CONFIG_WINDOW_WIDTH |
2054 XCB_CONFIG_WINDOW_HEIGHT,
2055 values);
2056
2057 weston_wm_window_get_frame_size(window, &width, &height);
2058 values[0] = width;
2059 values[1] = height;
2060 xcb_configure_window(wm->conn,
2061 window->frame_id,
2062 XCB_CONFIG_WINDOW_WIDTH |
2063 XCB_CONFIG_WINDOW_HEIGHT,
2064 values);
2065
2066 window->configure_source = NULL;
2067
2068 weston_wm_window_schedule_repaint(window);
2069}
2070
2071static void
2072send_configure(struct weston_surface *surface,
2073 uint32_t edges, int32_t width, int32_t height)
2074{
2075 struct weston_wm_window *window = get_wm_window(surface);
2076 struct weston_wm *wm = window->wm;
2077 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002078 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002079
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002080 if (window->fullscreen) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002081 hborder = 0;
2082 vborder = 0;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002083 } else if (window->decorate) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002084 hborder = 2 * (t->margin + t->width);
2085 vborder = 2 * t->margin + t->titlebar_height + t->width;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002086 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002087 hborder = 2 * t->margin;
2088 vborder = 2 * t->margin;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002089 }
2090
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002091 if (width > hborder)
2092 window->width = width - hborder;
2093 else
2094 window->width = 1;
2095
2096 if (height > vborder)
2097 window->height = height - vborder;
2098 else
2099 window->height = 1;
2100
Jason Ekstrandd14c4ca2013-10-13 19:08:41 -05002101 if (window->frame)
2102 frame_resize_inside(window->frame, window->width, window->height);
2103
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002104 if (window->configure_source)
2105 return;
2106
2107 window->configure_source =
2108 wl_event_loop_add_idle(wm->server->loop,
2109 weston_wm_window_configure, window);
2110}
2111
2112static const struct weston_shell_client shell_client = {
2113 send_configure
2114};
2115
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002116static int
2117legacy_fullscreen(struct weston_wm *wm,
2118 struct weston_wm_window *window,
2119 struct weston_output **output_ret)
2120{
2121 struct weston_compositor *compositor = wm->server->compositor;
2122 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002123 uint32_t minmax = PMinSize | PMaxSize;
2124 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002125
2126 /* Heuristics for detecting legacy fullscreen windows... */
2127
2128 wl_list_for_each(output, &compositor->output_list, link) {
2129 if (output->x == window->x &&
2130 output->y == window->y &&
2131 output->width == window->width &&
2132 output->height == window->height &&
2133 window->override_redirect) {
2134 *output_ret = output;
2135 return 1;
2136 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002137
2138 matching_size = 0;
2139 if ((window->size_hints.flags & (USSize |PSize)) &&
2140 window->size_hints.width == output->width &&
2141 window->size_hints.height == output->height)
2142 matching_size = 1;
2143 if ((window->size_hints.flags & minmax) == minmax &&
2144 window->size_hints.min_width == output->width &&
2145 window->size_hints.min_height == output->height &&
2146 window->size_hints.max_width == output->width &&
2147 window->size_hints.max_height == output->height)
2148 matching_size = 1;
2149
2150 if (matching_size && !window->decorate &&
2151 (window->size_hints.flags & (USPosition | PPosition)) &&
2152 window->size_hints.x == output->x &&
2153 window->size_hints.y == output->y) {
2154 *output_ret = output;
2155 return 1;
2156 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002157 }
2158
2159 return 0;
2160}
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002161static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002162xserver_map_shell_surface(struct weston_wm *wm,
2163 struct weston_wm_window *window)
2164{
2165 struct weston_shell_interface *shell_interface =
2166 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002167 struct weston_output *output;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002168
2169 if (!shell_interface->create_shell_surface)
2170 return;
2171
2172 window->shsurf =
2173 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002174 window->surface,
2175 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002176
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002177 if (window->name)
2178 shell_interface->set_title(window->shsurf, window->name);
2179
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002180 if (window->fullscreen) {
2181 window->saved_width = window->width;
2182 window->saved_height = window->height;
2183 shell_interface->set_fullscreen(window->shsurf,
2184 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2185 0, NULL);
2186 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002187 } else if (legacy_fullscreen(wm, window, &output)) {
2188 window->fullscreen = 1;
2189 shell_interface->set_fullscreen(window->shsurf,
2190 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2191 0, output);
Giulio Camuffoca43f092013-09-11 17:49:13 +02002192 } else if (!window->override_redirect && !window->transient_for) {
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002193 shell_interface->set_toplevel(window->shsurf);
2194 return;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002195 } else {
2196 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002197 window->x,
2198 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002199 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002200 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002201}
2202
2203static void
2204xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
2205 struct wl_resource *surface_resource, uint32_t id)
2206{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002207 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002208 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002209 struct weston_surface *surface =
2210 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002211 struct weston_wm_window *window;
2212
2213 if (client != wxs->client)
2214 return;
2215
2216 window = hash_table_lookup(wm->window_hash, id);
2217 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002218 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002219 return;
2220 }
2221
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002222 wm_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002223
2224 weston_wm_window_read_properties(window);
2225
Giulio Camuffo1cf329b2013-09-20 16:16:06 +02002226 /* A weston_wm_window may have many different surfaces assigned
2227 * throughout its life, so we must make sure to remove the listener
2228 * from the old surface signal list. */
2229 if (window->surface)
2230 wl_list_remove(&window->surface_destroy_listener.link);
2231
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002232 window->surface = (struct weston_surface *) surface;
2233 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002234 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002235 &window->surface_destroy_listener);
2236
2237 weston_wm_window_schedule_repaint(window);
2238 xserver_map_shell_surface(wm, window);
2239}
2240
2241const struct xserver_interface xserver_implementation = {
2242 xserver_set_window_id
2243};