blob: f7757347aa8be0acfcf5740cc141d414feb55c29 [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>
Kristian Høgsberg380deee2012-05-21 17:12:41 -040035
36#include "xwayland.h"
37
38#include "../../shared/cairo-util.h"
39#include "../compositor.h"
40#include "xserver-server-protocol.h"
41#include "hash.h"
42
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -070043struct wm_size_hints {
44 uint32_t flags;
45 int32_t x, y;
46 int32_t width, height; /* should set so old wm's don't mess up */
47 int32_t min_width, min_height;
48 int32_t max_width, max_height;
49 int32_t width_inc, height_inc;
50 struct {
51 int32_t x;
52 int32_t y;
53 } min_aspect, max_aspect;
54 int32_t base_width, base_height;
55 int32_t win_gravity;
56};
57
58#define USPosition (1L << 0)
59#define USSize (1L << 1)
60#define PPosition (1L << 2)
61#define PSize (1L << 3)
62#define PMinSize (1L << 4)
63#define PMaxSize (1L << 5)
64#define PResizeInc (1L << 6)
65#define PAspect (1L << 7)
66#define PBaseSize (1L << 8)
67#define PWinGravity (1L << 9)
68
Kristian Høgsberg380deee2012-05-21 17:12:41 -040069struct motif_wm_hints {
70 uint32_t flags;
71 uint32_t functions;
72 uint32_t decorations;
73 int32_t input_mode;
74 uint32_t status;
75};
76
77#define MWM_HINTS_FUNCTIONS (1L << 0)
78#define MWM_HINTS_DECORATIONS (1L << 1)
79#define MWM_HINTS_INPUT_MODE (1L << 2)
80#define MWM_HINTS_STATUS (1L << 3)
81
82#define MWM_FUNC_ALL (1L << 0)
83#define MWM_FUNC_RESIZE (1L << 1)
84#define MWM_FUNC_MOVE (1L << 2)
85#define MWM_FUNC_MINIMIZE (1L << 3)
86#define MWM_FUNC_MAXIMIZE (1L << 4)
87#define MWM_FUNC_CLOSE (1L << 5)
88
89#define MWM_DECOR_ALL (1L << 0)
90#define MWM_DECOR_BORDER (1L << 1)
91#define MWM_DECOR_RESIZEH (1L << 2)
92#define MWM_DECOR_TITLE (1L << 3)
93#define MWM_DECOR_MENU (1L << 4)
94#define MWM_DECOR_MINIMIZE (1L << 5)
95#define MWM_DECOR_MAXIMIZE (1L << 6)
96
97#define MWM_INPUT_MODELESS 0
98#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
99#define MWM_INPUT_SYSTEM_MODAL 2
100#define MWM_INPUT_FULL_APPLICATION_MODAL 3
101#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
102
103#define MWM_TEAROFF_WINDOW (1L<<0)
104
105#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
106#define _NET_WM_MOVERESIZE_SIZE_TOP 1
107#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
108#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
109#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
110#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
111#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
112#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
113#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
114#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
115#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
116#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
117
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400118struct weston_wm_window {
119 struct weston_wm *wm;
120 xcb_window_t id;
121 xcb_window_t frame_id;
122 cairo_surface_t *cairo_surface;
123 struct weston_surface *surface;
124 struct shell_surface *shsurf;
125 struct wl_listener surface_destroy_listener;
126 struct wl_event_source *repaint_source;
Kristian Høgsberga61ca062012-05-22 16:05:52 -0400127 struct wl_event_source *configure_source;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400128 int properties_dirty;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300129 int pid;
130 char *machine;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400131 char *class;
132 char *name;
133 struct weston_wm_window *transient_for;
134 uint32_t protocols;
135 xcb_atom_t type;
136 int width, height;
137 int x, y;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500138 int saved_width, saved_height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400139 int decorate;
Tiago Vignatti771241e2012-06-04 20:01:45 +0300140 int override_redirect;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500141 int fullscreen;
MoD384a11a2013-06-22 11:04:21 -0500142 int has_alpha;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700143 struct wm_size_hints size_hints;
144 struct motif_wm_hints motif_hints;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400145};
146
147static struct weston_wm_window *
148get_wm_window(struct weston_surface *surface);
149
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400150static void
151weston_wm_window_schedule_repaint(struct weston_wm_window *window);
152
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400153static int __attribute__ ((format (printf, 1, 2)))
154wm_log(const char *fmt, ...)
155{
156#ifdef WM_DEBUG
157 int l;
158 va_list argp;
159
160 va_start(argp, fmt);
161 l = weston_vlog(fmt, argp);
162 va_end(argp);
163
164 return l;
165#else
166 return 0;
167#endif
168}
169
170static int __attribute__ ((format (printf, 1, 2)))
171wm_log_continue(const char *fmt, ...)
172{
173#ifdef WM_DEBUG
174 int l;
175 va_list argp;
176
177 va_start(argp, fmt);
178 l = weston_vlog_continue(fmt, argp);
179 va_end(argp);
180
181 return l;
182#else
183 return 0;
184#endif
185}
186
187
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400188const char *
189get_atom_name(xcb_connection_t *c, xcb_atom_t atom)
190{
191 xcb_get_atom_name_cookie_t cookie;
192 xcb_get_atom_name_reply_t *reply;
193 xcb_generic_error_t *e;
194 static char buffer[64];
195
196 if (atom == XCB_ATOM_NONE)
197 return "None";
198
199 cookie = xcb_get_atom_name (c, atom);
200 reply = xcb_get_atom_name_reply (c, cookie, &e);
MoD55375b92013-06-11 19:59:42 -0500201
202 if(reply) {
203 snprintf(buffer, sizeof buffer, "%.*s",
204 xcb_get_atom_name_name_length (reply),
205 xcb_get_atom_name_name (reply));
206 } else {
207 snprintf(buffer, sizeof buffer, "(atom %u)", atom);
208 }
209
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400210 free(reply);
211
212 return buffer;
213}
214
Tiago Vignatti90fada42012-07-16 12:02:08 -0400215static xcb_cursor_t
216xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img)
217{
218 xcb_connection_t *c = wm->conn;
219 xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c));
220 xcb_screen_t *screen = s.data;
221 xcb_gcontext_t gc;
222 xcb_pixmap_t pix;
223 xcb_render_picture_t pic;
224 xcb_cursor_t cursor;
225 int stride = img->width * 4;
226
227 pix = xcb_generate_id(c);
228 xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height);
229
230 pic = xcb_generate_id(c);
231 xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0);
232
233 gc = xcb_generate_id(c);
234 xcb_create_gc(c, gc, pix, 0, 0);
235
236 xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
237 img->width, img->height, 0, 0, 0, 32,
238 stride * img->height, (uint8_t *) img->pixels);
239 xcb_free_gc(c, gc);
240
241 cursor = xcb_generate_id(c);
242 xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot);
243
244 xcb_render_free_picture(c, pic);
245 xcb_free_pixmap(c, pix);
246
247 return cursor;
248}
249
250static xcb_cursor_t
251xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images)
252{
253 /* TODO: treat animated cursors as well */
254 if (images->nimage != 1)
255 return -1;
256
257 return xcb_cursor_image_load_cursor(wm, images->images[0]);
258}
259
260static xcb_cursor_t
261xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file)
262{
263 xcb_cursor_t cursor;
264 XcursorImages *images;
265 char *v = NULL;
266 int size = 0;
267
268 if (!file)
269 return 0;
270
271 v = getenv ("XCURSOR_SIZE");
272 if (v)
273 size = atoi(v);
274
275 if (!size)
276 size = 32;
277
278 images = XcursorLibraryLoadImages (file, NULL, size);
Tiago Vignattiac78bb12012-09-28 16:29:46 +0300279 if (!images)
280 return -1;
281
Tiago Vignatti90fada42012-07-16 12:02:08 -0400282 cursor = xcb_cursor_images_load_cursor (wm, images);
283 XcursorImagesDestroy (images);
284
285 return cursor;
286}
287
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400288void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400289dump_property(struct weston_wm *wm,
290 xcb_atom_t property, xcb_get_property_reply_t *reply)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400291{
292 int32_t *incr_value;
293 const char *text_value, *name;
294 xcb_atom_t *atom_value;
295 int width, len;
296 uint32_t i;
297
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400298 width = wm_log_continue("%s: ", get_atom_name(wm->conn, property));
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400299 if (reply == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400300 wm_log_continue("(no reply)\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400301 return;
302 }
303
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400304 width += wm_log_continue("%s/%d, length %d (value_len %d): ",
305 get_atom_name(wm->conn, reply->type),
306 reply->format,
307 xcb_get_property_value_length(reply),
308 reply->value_len);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400309
310 if (reply->type == wm->atom.incr) {
311 incr_value = xcb_get_property_value(reply);
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400312 wm_log_continue("%d\n", *incr_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400313 } else if (reply->type == wm->atom.utf8_string ||
314 reply->type == wm->atom.string) {
315 text_value = xcb_get_property_value(reply);
316 if (reply->value_len > 40)
317 len = 40;
318 else
319 len = reply->value_len;
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400320 wm_log_continue("\"%.*s\"\n", len, text_value);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400321 } else if (reply->type == XCB_ATOM_ATOM) {
322 atom_value = xcb_get_property_value(reply);
323 for (i = 0; i < reply->value_len; i++) {
324 name = get_atom_name(wm->conn, atom_value[i]);
325 if (width + strlen(name) + 2 > 78) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400326 wm_log_continue("\n ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400327 width = 4;
328 } else if (i > 0) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400329 width += wm_log_continue(", ");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400330 }
331
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400332 width += wm_log_continue("%s", name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400333 }
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400334 wm_log_continue("\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400335 } else {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400336 wm_log_continue("huh?\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400337 }
338}
339
Tiago Vignatti2d129f12012-11-30 17:19:59 -0200340static void
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400341read_and_dump_property(struct weston_wm *wm,
342 xcb_window_t window, xcb_atom_t property)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400343{
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400344 xcb_get_property_reply_t *reply;
345 xcb_get_property_cookie_t cookie;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400346
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400347 cookie = xcb_get_property(wm->conn, 0, window,
348 property, XCB_ATOM_ANY, 0, 2048);
349 reply = xcb_get_property_reply(wm->conn, cookie, NULL);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400350
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400351 dump_property(wm, property, reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400352
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400353 free(reply);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400354}
355
356/* We reuse some predefined, but otherwise useles atoms */
357#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0
358#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500359#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700360#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400361
362static void
363weston_wm_window_read_properties(struct weston_wm_window *window)
364{
365 struct weston_wm *wm = window->wm;
366
367#define F(field) offsetof(struct weston_wm_window, field)
368 const struct {
369 xcb_atom_t atom;
370 xcb_atom_t type;
371 int offset;
372 } props[] = {
373 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
374 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
375 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
376 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700377 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500378 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400379 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
380 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300381 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400382 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300383 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400384 };
385#undef F
386
387 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
388 xcb_get_property_reply_t *reply;
389 void *p;
390 uint32_t *xid;
391 xcb_atom_t *atom;
392 uint32_t i;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400393
394 if (!window->properties_dirty)
395 return;
396 window->properties_dirty = 0;
397
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400398 for (i = 0; i < ARRAY_LENGTH(props); i++)
399 cookie[i] = xcb_get_property(wm->conn,
400 0, /* delete */
401 window->id,
402 props[i].atom,
403 XCB_ATOM_ANY, 0, 2048);
404
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300405 window->decorate = !window->override_redirect;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700406 window->size_hints.flags = 0;
407 window->motif_hints.flags = 0;
408
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400409 for (i = 0; i < ARRAY_LENGTH(props); i++) {
410 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
411 if (!reply)
412 /* Bad window, typically */
413 continue;
414 if (reply->type == XCB_ATOM_NONE) {
415 /* No such property */
416 free(reply);
417 continue;
418 }
419
420 p = ((char *) window + props[i].offset);
421
422 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300423 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400424 case XCB_ATOM_STRING:
425 /* FIXME: We're using this for both string and
426 utf8_string */
427 if (*(char **) p)
428 free(*(char **) p);
429
430 *(char **) p =
431 strndup(xcb_get_property_value(reply),
432 xcb_get_property_value_length(reply));
433 break;
434 case XCB_ATOM_WINDOW:
435 xid = xcb_get_property_value(reply);
436 *(struct weston_wm_window **) p =
437 hash_table_lookup(wm->window_hash, *xid);
438 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300439 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400440 case XCB_ATOM_ATOM:
441 atom = xcb_get_property_value(reply);
442 *(xcb_atom_t *) p = *atom;
443 break;
444 case TYPE_WM_PROTOCOLS:
445 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700446 case TYPE_WM_NORMAL_HINTS:
447 memcpy(&window->size_hints,
448 xcb_get_property_value(reply),
449 sizeof window->size_hints);
450 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500451 case TYPE_NET_WM_STATE:
452 window->fullscreen = 0;
453 atom = xcb_get_property_value(reply);
454 for (i = 0; i < reply->value_len; i++)
455 if (atom[i] == wm->atom.net_wm_state_fullscreen)
456 window->fullscreen = 1;
457 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400458 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700459 memcpy(&window->motif_hints,
460 xcb_get_property_value(reply),
461 sizeof window->motif_hints);
462 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS)
463 window->decorate =
464 window->motif_hints.decorations > 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400465 break;
466 default:
467 break;
468 }
469 free(reply);
470 }
471}
472
473static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400474weston_wm_window_get_frame_size(struct weston_wm_window *window,
475 int *width, int *height)
476{
477 struct theme *t = window->wm->theme;
478
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500479 if (window->fullscreen) {
480 *width = window->width;
481 *height = window->height;
482 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400483 *width = window->width + (t->margin + t->width) * 2;
484 *height = window->height +
485 t->margin * 2 + t->width + t->titlebar_height;
486 } else {
487 *width = window->width + t->margin * 2;
488 *height = window->height + t->margin * 2;
489 }
490}
491
492static void
493weston_wm_window_get_child_position(struct weston_wm_window *window,
494 int *x, int *y)
495{
496 struct theme *t = window->wm->theme;
497
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500498 if (window->fullscreen) {
499 *x = 0;
500 *y = 0;
501 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400502 *x = t->margin + t->width;
503 *y = t->margin + t->titlebar_height;
504 } else {
505 *x = t->margin;
506 *y = t->margin;
507 }
508}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400509
510static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500511weston_wm_window_send_configure_notify(struct weston_wm_window *window)
512{
513 xcb_configure_notify_event_t configure_notify;
514 struct weston_wm *wm = window->wm;
515 int x, y;
516
517 weston_wm_window_get_child_position(window, &x, &y);
518 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
519 configure_notify.pad0 = 0;
520 configure_notify.event = window->id;
521 configure_notify.window = window->id;
522 configure_notify.above_sibling = XCB_WINDOW_NONE;
523 configure_notify.x = x;
524 configure_notify.y = y;
525 configure_notify.width = window->width;
526 configure_notify.height = window->height;
527 configure_notify.border_width = 0;
528 configure_notify.override_redirect = 0;
529 configure_notify.pad1 = 0;
530
531 xcb_send_event(wm->conn, 0, window->id,
532 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
533 (char *) &configure_notify);
534}
535
536static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400537weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
538{
539 xcb_configure_request_event_t *configure_request =
540 (xcb_configure_request_event_t *) event;
541 struct weston_wm_window *window;
542 uint32_t mask, values[16];
543 int x, y, width, height, i = 0;
544
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400545 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
546 configure_request->window,
547 configure_request->x, configure_request->y,
548 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400549
550 window = hash_table_lookup(wm->window_hash, configure_request->window);
551
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500552 if (window->fullscreen) {
553 weston_wm_window_send_configure_notify(window);
554 return;
555 }
556
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400557 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
558 window->width = configure_request->width;
559 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
560 window->height = configure_request->height;
561
562 weston_wm_window_get_child_position(window, &x, &y);
563 values[i++] = x;
564 values[i++] = y;
565 values[i++] = window->width;
566 values[i++] = window->height;
567 values[i++] = 0;
568 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
569 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
570 XCB_CONFIG_WINDOW_BORDER_WIDTH;
571 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
572 values[i++] = configure_request->sibling;
573 mask |= XCB_CONFIG_WINDOW_SIBLING;
574 }
575 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
576 values[i++] = configure_request->stack_mode;
577 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
578 }
579
580 xcb_configure_window(wm->conn, window->id, mask, values);
581
582 weston_wm_window_get_frame_size(window, &width, &height);
583 values[0] = width;
584 values[1] = height;
585 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
586 xcb_configure_window(wm->conn, window->frame_id, mask, values);
587
588 weston_wm_window_schedule_repaint(window);
589}
590
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400591static int
592our_resource(struct weston_wm *wm, uint32_t id)
593{
594 const xcb_setup_t *setup;
595
596 setup = xcb_get_setup(wm->conn);
597
598 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
599}
600
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400601static void
602weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
603{
604 xcb_configure_notify_event_t *configure_notify =
605 (xcb_configure_notify_event_t *) event;
606 struct weston_wm_window *window;
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300607 int x, y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400608
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400609 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400610 configure_notify->window,
611 configure_notify->x, configure_notify->y,
612 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300613
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400614 window = hash_table_lookup(wm->window_hash, configure_notify->window);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300615 weston_wm_window_get_child_position(window, &x, &y);
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700616 window->x = configure_notify->x;
617 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700618 if (window->override_redirect) {
619 window->width = configure_notify->width;
620 window->height = configure_notify->height;
621 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400622}
623
624static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300625weston_wm_kill_client(struct wl_listener *listener, void *data)
626{
627 struct weston_surface *surface = data;
628 struct weston_wm_window *window = get_wm_window(surface);
629 char name[1024];
630
631 if (!window)
632 return;
633
634 gethostname(name, 1024);
635
636 /* this is only one heuristic to guess the PID of a client is valid,
637 * assuming it's compliant with icccm and ewmh. Non-compliants and
638 * remote applications of course fail. */
639 if (!strcmp(window->machine, name) && window->pid != 0)
640 kill(window->pid, SIGKILL);
641}
642
643static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400644weston_wm_window_activate(struct wl_listener *listener, void *data)
645{
646 struct weston_surface *surface = data;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200647 struct weston_wm_window *window = NULL;
Scott Moreau85ecac02012-05-21 15:49:14 -0600648 struct weston_wm *wm =
649 container_of(listener, struct weston_wm, activate_listener);
650 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400651
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200652 if (surface) {
653 window = get_wm_window(surface);
654 }
655
Scott Moreau85ecac02012-05-21 15:49:14 -0600656 if (window) {
657 client_message.response_type = XCB_CLIENT_MESSAGE;
658 client_message.format = 32;
659 client_message.window = window->id;
660 client_message.type = wm->atom.wm_protocols;
661 client_message.data.data32[0] = wm->atom.wm_take_focus;
662 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
663
664 xcb_send_event(wm->conn, 0, window->id,
665 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
666 (char *) &client_message);
667
668 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
669 window->id, XCB_TIME_CURRENT_TIME);
670 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400671 xcb_set_input_focus (wm->conn,
672 XCB_INPUT_FOCUS_POINTER_ROOT,
673 XCB_NONE,
674 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600675 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400676
677 if (wm->focus_window)
678 weston_wm_window_schedule_repaint(wm->focus_window);
679 wm->focus_window = window;
680 if (wm->focus_window)
681 weston_wm_window_schedule_repaint(wm->focus_window);
682}
683
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300684static void
685weston_wm_window_transform(struct wl_listener *listener, void *data)
686{
687 struct weston_surface *surface = data;
688 struct weston_wm_window *window = get_wm_window(surface);
689 struct weston_wm *wm =
690 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300691 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300692
693 if (!window || !wm)
694 return;
695
696 if (!weston_surface_is_mapped(surface))
697 return;
698
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700699 if (window->x != surface->geometry.x ||
700 window->y != surface->geometry.y) {
701 values[0] = surface->geometry.x;
702 values[1] = surface->geometry.y;
703 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300704
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700705 xcb_configure_window(wm->conn, window->frame_id, mask, values);
706 xcb_flush(wm->conn);
707 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300708}
709
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400710#define ICCCM_WITHDRAWN_STATE 0
711#define ICCCM_NORMAL_STATE 1
712#define ICCCM_ICONIC_STATE 3
713
714static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500715weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400716{
717 struct weston_wm *wm = window->wm;
718 uint32_t property[2];
719
720 property[0] = state;
721 property[1] = XCB_WINDOW_NONE;
722
723 xcb_change_property(wm->conn,
724 XCB_PROP_MODE_REPLACE,
725 window->id,
726 wm->atom.wm_state,
727 wm->atom.wm_state,
728 32, /* format */
729 2, property);
730}
731
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400732static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500733weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
734{
735 struct weston_wm *wm = window->wm;
736 uint32_t property[1];
737 int i;
738
739 i = 0;
740 if (window->fullscreen)
741 property[i++] = wm->atom.net_wm_state_fullscreen;
742
743 xcb_change_property(wm->conn,
744 XCB_PROP_MODE_REPLACE,
745 window->id,
746 wm->atom.net_wm_state,
747 XCB_ATOM_ATOM,
748 32, /* format */
749 i, property);
750}
751
752static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700753weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400754{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700755 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400756 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400757 int x, y, width, height;
758
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400759 weston_wm_window_get_frame_size(window, &width, &height);
760 weston_wm_window_get_child_position(window, &x, &y);
761
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400762 values[0] = wm->screen->black_pixel;
763 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400764 XCB_EVENT_MASK_KEY_PRESS |
765 XCB_EVENT_MASK_KEY_RELEASE |
766 XCB_EVENT_MASK_BUTTON_PRESS |
767 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400768 XCB_EVENT_MASK_POINTER_MOTION |
769 XCB_EVENT_MASK_ENTER_WINDOW |
770 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400771 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400772 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400773 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400774
775 window->frame_id = xcb_generate_id(wm->conn);
776 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400777 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400778 window->frame_id,
779 wm->screen->root,
780 0, 0,
781 width, height,
782 0,
783 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400784 wm->visual_id,
785 XCB_CW_BORDER_PIXEL |
786 XCB_CW_EVENT_MASK |
787 XCB_CW_COLORMAP, values);
788
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400789 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
790
791 values[0] = 0;
792 xcb_configure_window(wm->conn, window->id,
793 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
794
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400795 window->cairo_surface =
796 cairo_xcb_surface_create_with_xrender_format(wm->conn,
797 wm->screen,
798 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400799 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400800 width, height);
801
802 hash_table_insert(wm->window_hash, window->frame_id, window);
803}
804
805static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700806weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
807{
808 xcb_map_request_event_t *map_request =
809 (xcb_map_request_event_t *) event;
810 struct weston_wm_window *window;
811
812 if (our_resource(wm, map_request->window)) {
813 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
814 map_request->window);
815 return;
816 }
817
818 window = hash_table_lookup(wm->window_hash, map_request->window);
819
820 weston_wm_window_read_properties(window);
821
822 if (window->frame_id == XCB_WINDOW_NONE)
823 weston_wm_window_create_frame(window);
824
825 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
826 window->id, window, window->frame_id);
827
828 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
829 weston_wm_window_set_net_wm_state(window);
830
831 xcb_map_window(wm->conn, map_request->window);
832 xcb_map_window(wm->conn, window->frame_id);
833}
834
835static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400836weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
837{
838 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
839
840 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400841 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
842 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400843 return;
844 }
845
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400846 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400847}
848
849static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400850weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
851{
852 xcb_unmap_notify_event_t *unmap_notify =
853 (xcb_unmap_notify_event_t *) event;
854 struct weston_wm_window *window;
855
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400856 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
857 unmap_notify->window,
858 unmap_notify->event,
859 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400860
861 if (our_resource(wm, unmap_notify->window))
862 return;
863
MoD31700122013-06-11 19:58:55 -0500864 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400865 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
866 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400867 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400868
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400869 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400870 if (wm->focus_window == window)
871 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400872 if (window->surface)
873 wl_list_remove(&window->surface_destroy_listener.link);
874 window->surface = NULL;
Kristian Høgsbergab6d6672013-09-03 16:38:51 -0700875 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400876}
877
878static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400879weston_wm_window_draw_decoration(void *data)
880{
881 struct weston_wm_window *window = data;
882 struct weston_wm *wm = window->wm;
883 struct theme *t = wm->theme;
884 cairo_t *cr;
885 int x, y, width, height;
886 const char *title;
887 uint32_t flags = 0;
888
889 weston_wm_window_read_properties(window);
890
891 window->repaint_source = NULL;
892
893 weston_wm_window_get_frame_size(window, &width, &height);
894 weston_wm_window_get_child_position(window, &x, &y);
895
896 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
897 cr = cairo_create(window->cairo_surface);
898
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500899 if (window->fullscreen) {
900 /* nothing */
901 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400902 if (wm->focus_window == window)
903 flags |= THEME_FRAME_ACTIVE;
904
905 if (window->name)
906 title = window->name;
907 else
908 title = "untitled";
909
910 theme_render_frame(t, cr, width, height, title, flags);
911 } else {
912 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
913 cairo_set_source_rgba(cr, 0, 0, 0, 0);
914 cairo_paint(cr);
915
916 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
917 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
918 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
919 }
920
921 cairo_destroy(cr);
922
923 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700924 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500925 if(window->has_alpha) {
926 pixman_region32_init(&window->surface->pending.opaque);
927 } else {
928 /* We leave an extra pixel around the X window area to
929 * make sure we don't sample from the undefined alpha
930 * channel when filtering. */
931 pixman_region32_init_rect(&window->surface->pending.opaque,
932 x - 1, y - 1,
933 window->width + 2,
934 window->height + 2);
935 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200936 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500937 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400938
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500939 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500940 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500941 pixman_region32_init_rect(&window->surface->pending.input,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400942 t->margin, t->margin,
943 width - 2 * t->margin,
944 height - 2 * t->margin);
945 }
946}
947
948static void
949weston_wm_window_schedule_repaint(struct weston_wm_window *window)
950{
951 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300952 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400953
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400954 if (window->frame_id == XCB_WINDOW_NONE) {
955 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300956 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700957 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500958 if(window->has_alpha) {
959 pixman_region32_init(&window->surface->pending.opaque);
960 } else {
961 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
962 width, height);
963 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200964 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400965 }
966 return;
967 }
968
969 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400970 return;
971
972 window->repaint_source =
973 wl_event_loop_add_idle(wm->server->loop,
974 weston_wm_window_draw_decoration,
975 window);
976}
977
978static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400979weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
980{
981 xcb_property_notify_event_t *property_notify =
982 (xcb_property_notify_event_t *) event;
983 struct weston_wm_window *window;
984
985 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +0000986 if (!window)
987 return;
988
989 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400990
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400991 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400992 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400993 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400994 else
995 read_and_dump_property(wm, property_notify->window,
996 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -0400997
998 if (property_notify->atom == wm->atom.net_wm_name ||
999 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001000 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001001}
1002
1003static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001004weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001005 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001006{
1007 struct weston_wm_window *window;
1008 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001009 xcb_get_geometry_cookie_t geometry_cookie;
1010 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001011
Peter Huttererf3d62272013-08-08 11:57:05 +10001012 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001013 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001014 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001015 return;
1016 }
1017
MoD384a11a2013-06-22 11:04:21 -05001018 geometry_cookie = xcb_get_geometry(wm->conn, id);
1019
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001020 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1021 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1022
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001023 window->wm = wm;
1024 window->id = id;
1025 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001026 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001027 window->width = width;
1028 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001029 window->x = x;
1030 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001031
MoD384a11a2013-06-22 11:04:21 -05001032 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1033 /* technically we should use XRender and check the visual format's
1034 alpha_mask, but checking depth is simpler and works in all known cases */
1035 if(geometry_reply != NULL)
1036 window->has_alpha = geometry_reply->depth == 32;
1037 free(geometry_reply);
1038
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001039 hash_table_insert(wm->window_hash, id, window);
1040}
1041
1042static void
1043weston_wm_window_destroy(struct weston_wm_window *window)
1044{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001045 struct weston_wm *wm = window->wm;
1046
1047 if (window->repaint_source)
1048 wl_event_source_remove(window->repaint_source);
1049 if (window->cairo_surface)
1050 cairo_surface_destroy(window->cairo_surface);
1051
1052 if (window->frame_id) {
1053 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1054 xcb_destroy_window(wm->conn, window->frame_id);
1055 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1056 hash_table_remove(wm->window_hash, window->frame_id);
1057 window->frame_id = XCB_WINDOW_NONE;
1058 }
1059
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001060 hash_table_remove(window->wm->window_hash, window->id);
1061 free(window);
1062}
1063
1064static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001065weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1066{
1067 xcb_create_notify_event_t *create_notify =
1068 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001069
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001070 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1071 create_notify->window,
1072 create_notify->width, create_notify->height,
1073 create_notify->override_redirect ? ", override" : "",
1074 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001075
1076 if (our_resource(wm, create_notify->window))
1077 return;
1078
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001079 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001080 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001081 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001082 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001083}
1084
1085static void
1086weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1087{
1088 xcb_destroy_notify_event_t *destroy_notify =
1089 (xcb_destroy_notify_event_t *) event;
1090 struct weston_wm_window *window;
1091
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001092 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1093 destroy_notify->window,
1094 destroy_notify->event,
1095 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001096
1097 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001098 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001099
1100 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001101 weston_wm_window_destroy(window);
1102}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001103
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001104static void
1105weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1106{
1107 xcb_reparent_notify_event_t *reparent_notify =
1108 (xcb_reparent_notify_event_t *) event;
1109 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001110
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001111 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1112 reparent_notify->window,
1113 reparent_notify->parent,
1114 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001115
1116 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001117 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001118 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001119 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001120 } else if (!our_resource(wm, reparent_notify->parent)) {
1121 window = hash_table_lookup(wm->window_hash,
1122 reparent_notify->window);
1123 weston_wm_window_destroy(window);
1124 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001125}
1126
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001127struct weston_seat *
1128weston_wm_pick_seat(struct weston_wm *wm)
1129{
1130 return container_of(wm->server->compositor->seat_list.next,
1131 struct weston_seat, link);
1132}
1133
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001134static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001135weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1136 xcb_client_message_event_t *client_message)
1137{
1138 static const int map[] = {
1139 THEME_LOCATION_RESIZING_TOP_LEFT,
1140 THEME_LOCATION_RESIZING_TOP,
1141 THEME_LOCATION_RESIZING_TOP_RIGHT,
1142 THEME_LOCATION_RESIZING_RIGHT,
1143 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1144 THEME_LOCATION_RESIZING_BOTTOM,
1145 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1146 THEME_LOCATION_RESIZING_LEFT
1147 };
1148
1149 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001150 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001151 int detail;
1152 struct weston_shell_interface *shell_interface =
1153 &wm->server->compositor->shell_interface;
1154
Kristian Høgsberge3148752013-05-06 23:19:49 -04001155 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001156 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001157 return;
1158
1159 detail = client_message->data.data32[2];
1160 switch (detail) {
1161 case _NET_WM_MOVERESIZE_MOVE:
1162 shell_interface->move(window->shsurf, seat);
1163 break;
1164 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1165 case _NET_WM_MOVERESIZE_SIZE_TOP:
1166 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1167 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1168 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1169 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1170 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1171 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1172 shell_interface->resize(window->shsurf, seat, map[detail]);
1173 break;
1174 case _NET_WM_MOVERESIZE_CANCEL:
1175 break;
1176 }
1177}
1178
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001179#define _NET_WM_STATE_REMOVE 0
1180#define _NET_WM_STATE_ADD 1
1181#define _NET_WM_STATE_TOGGLE 2
1182
1183static int
1184update_state(int action, int *state)
1185{
1186 int new_state, changed;
1187
1188 switch (action) {
1189 case _NET_WM_STATE_REMOVE:
1190 new_state = 0;
1191 break;
1192 case _NET_WM_STATE_ADD:
1193 new_state = 1;
1194 break;
1195 case _NET_WM_STATE_TOGGLE:
1196 new_state = !*state;
1197 break;
1198 default:
1199 return 0;
1200 }
1201
1202 changed = (*state != new_state);
1203 *state = new_state;
1204
1205 return changed;
1206}
1207
1208static void
1209weston_wm_window_configure(void *data);
1210
1211static void
1212weston_wm_window_handle_state(struct weston_wm_window *window,
1213 xcb_client_message_event_t *client_message)
1214{
1215 struct weston_wm *wm = window->wm;
1216 struct weston_shell_interface *shell_interface =
1217 &wm->server->compositor->shell_interface;
1218 uint32_t action, property;
1219
1220 action = client_message->data.data32[0];
1221 property = client_message->data.data32[1];
1222
1223 if (property == wm->atom.net_wm_state_fullscreen &&
1224 update_state(action, &window->fullscreen)) {
1225 weston_wm_window_set_net_wm_state(window);
1226 if (window->fullscreen) {
1227 window->saved_width = window->width;
1228 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001229
1230 if (window->shsurf)
1231 shell_interface->set_fullscreen(window->shsurf,
1232 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1233 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001234 } else {
1235 shell_interface->set_toplevel(window->shsurf);
1236 window->width = window->saved_width;
1237 window->height = window->saved_height;
1238 weston_wm_window_configure(window);
1239 }
1240 }
1241}
1242
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001243static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001244weston_wm_handle_client_message(struct weston_wm *wm,
1245 xcb_generic_event_t *event)
1246{
1247 xcb_client_message_event_t *client_message =
1248 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001249 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001250
1251 window = hash_table_lookup(wm->window_hash, client_message->window);
1252
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001253 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1254 get_atom_name(wm->conn, client_message->type),
1255 client_message->data.data32[0],
1256 client_message->data.data32[1],
1257 client_message->data.data32[2],
1258 client_message->data.data32[3],
1259 client_message->data.data32[4],
1260 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001261
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001262 if (client_message->type == wm->atom.net_wm_moveresize)
1263 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001264 else if (client_message->type == wm->atom.net_wm_state)
1265 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001266}
1267
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001268enum cursor_type {
1269 XWM_CURSOR_TOP,
1270 XWM_CURSOR_BOTTOM,
1271 XWM_CURSOR_LEFT,
1272 XWM_CURSOR_RIGHT,
1273 XWM_CURSOR_TOP_LEFT,
1274 XWM_CURSOR_TOP_RIGHT,
1275 XWM_CURSOR_BOTTOM_LEFT,
1276 XWM_CURSOR_BOTTOM_RIGHT,
1277 XWM_CURSOR_LEFT_PTR,
1278};
1279
1280static const char *cursors[] = {
1281 "top_side",
1282 "bottom_side",
1283 "left_side",
1284 "right_side",
1285 "top_left_corner",
1286 "top_right_corner",
1287 "bottom_left_corner",
1288 "bottom_right_corner",
1289 "left_ptr"
1290};
1291
1292static void
1293weston_wm_create_cursors(struct weston_wm *wm)
1294{
1295 int i, count = ARRAY_LENGTH(cursors);
1296
1297 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1298 for (i = 0; i < count; i++) {
1299 wm->cursors[i] =
1300 xcb_cursor_library_load_cursor(wm, cursors[i]);
1301 }
1302
1303 wm->last_cursor = -1;
1304}
1305
1306static void
1307weston_wm_destroy_cursors(struct weston_wm *wm)
1308{
1309 uint8_t i;
1310
1311 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1312 xcb_free_cursor(wm->conn, wm->cursors[i]);
1313
1314 free(wm->cursors);
1315}
1316
1317static int
1318get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1319{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001320 int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001321
1322 switch (location) {
1323 case THEME_LOCATION_RESIZING_TOP:
1324 return XWM_CURSOR_TOP;
1325 case THEME_LOCATION_RESIZING_BOTTOM:
1326 return XWM_CURSOR_BOTTOM;
1327 case THEME_LOCATION_RESIZING_LEFT:
1328 return XWM_CURSOR_LEFT;
1329 case THEME_LOCATION_RESIZING_RIGHT:
1330 return XWM_CURSOR_RIGHT;
1331 case THEME_LOCATION_RESIZING_TOP_LEFT:
1332 return XWM_CURSOR_TOP_LEFT;
1333 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1334 return XWM_CURSOR_TOP_RIGHT;
1335 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1336 return XWM_CURSOR_BOTTOM_LEFT;
1337 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1338 return XWM_CURSOR_BOTTOM_RIGHT;
1339 case THEME_LOCATION_EXTERIOR:
1340 case THEME_LOCATION_TITLEBAR:
1341 default:
1342 return XWM_CURSOR_LEFT_PTR;
1343 }
1344}
1345
1346static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001347weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1348 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001349{
1350 uint32_t cursor_value_list;
1351
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001352 if (wm->last_cursor == cursor)
1353 return;
1354
1355 wm->last_cursor = cursor;
1356
1357 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001358 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001359 XCB_CW_CURSOR, &cursor_value_list);
1360 xcb_flush(wm->conn);
1361}
1362
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001363static void
1364weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1365{
1366 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1367 struct weston_shell_interface *shell_interface =
1368 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001369 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001370 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001371 enum theme_location location;
1372 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001373 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001374
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001375 wm_log("XCB_BUTTON_%s (detail %d)\n",
1376 button->response_type == XCB_BUTTON_PRESS ?
1377 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001378
1379 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001380 weston_wm_window_get_frame_size(window, &width, &height);
1381
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001382 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001383 button->detail == 1) {
1384 location = theme_get_location(t,
1385 button->event_x,
1386 button->event_y,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001387 width, height, 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001388
1389 switch (location) {
1390 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001391 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001392 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001393 case THEME_LOCATION_RESIZING_TOP:
1394 case THEME_LOCATION_RESIZING_BOTTOM:
1395 case THEME_LOCATION_RESIZING_LEFT:
1396 case THEME_LOCATION_RESIZING_RIGHT:
1397 case THEME_LOCATION_RESIZING_TOP_LEFT:
1398 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1399 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1400 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1401 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001402 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001403 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001404 default:
1405 break;
1406 }
1407 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001408}
1409
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001410static void
1411weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1412{
1413 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1414 struct weston_wm_window *window;
1415 int cursor, width, height;
1416
1417 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001418 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001419 return;
1420
1421 weston_wm_window_get_frame_size(window, &width, &height);
1422 cursor = get_cursor_for_location(wm->theme, width, height,
1423 motion->event_x, motion->event_y);
1424
Tiago Vignattic1903232012-07-16 12:15:37 -04001425 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001426}
1427
1428static void
1429weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1430{
1431 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1432 struct weston_wm_window *window;
1433 int cursor, width, height;
1434
1435 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001436 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001437 return;
1438
1439 weston_wm_window_get_frame_size(window, &width, &height);
1440 cursor = get_cursor_for_location(wm->theme, width, height,
1441 enter->event_x, enter->event_y);
1442
Tiago Vignattic1903232012-07-16 12:15:37 -04001443 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001444}
1445
1446static void
1447weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1448{
1449 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1450 struct weston_wm_window *window;
1451
1452 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001453 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001454 return;
1455
Tiago Vignattic1903232012-07-16 12:15:37 -04001456 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001457}
1458
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001459static int
1460weston_wm_handle_event(int fd, uint32_t mask, void *data)
1461{
1462 struct weston_wm *wm = data;
1463 xcb_generic_event_t *event;
1464 int count = 0;
1465
1466 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1467 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001468 free(event);
1469 count++;
1470 continue;
1471 }
1472
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001473 if (weston_wm_handle_dnd_event(wm, event)) {
1474 free(event);
1475 count++;
1476 continue;
1477 }
1478
MoD31700122013-06-11 19:58:55 -05001479 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001480 case XCB_BUTTON_PRESS:
1481 case XCB_BUTTON_RELEASE:
1482 weston_wm_handle_button(wm, event);
1483 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001484 case XCB_ENTER_NOTIFY:
1485 weston_wm_handle_enter(wm, event);
1486 break;
1487 case XCB_LEAVE_NOTIFY:
1488 weston_wm_handle_leave(wm, event);
1489 break;
1490 case XCB_MOTION_NOTIFY:
1491 weston_wm_handle_motion(wm, event);
1492 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001493 case XCB_CREATE_NOTIFY:
1494 weston_wm_handle_create_notify(wm, event);
1495 break;
1496 case XCB_MAP_REQUEST:
1497 weston_wm_handle_map_request(wm, event);
1498 break;
1499 case XCB_MAP_NOTIFY:
1500 weston_wm_handle_map_notify(wm, event);
1501 break;
1502 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001503 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001504 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001505 case XCB_REPARENT_NOTIFY:
1506 weston_wm_handle_reparent_notify(wm, event);
1507 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001508 case XCB_CONFIGURE_REQUEST:
1509 weston_wm_handle_configure_request(wm, event);
1510 break;
1511 case XCB_CONFIGURE_NOTIFY:
1512 weston_wm_handle_configure_notify(wm, event);
1513 break;
1514 case XCB_DESTROY_NOTIFY:
1515 weston_wm_handle_destroy_notify(wm, event);
1516 break;
1517 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001518 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001519 break;
1520 case XCB_PROPERTY_NOTIFY:
1521 weston_wm_handle_property_notify(wm, event);
1522 break;
1523 case XCB_CLIENT_MESSAGE:
1524 weston_wm_handle_client_message(wm, event);
1525 break;
1526 }
1527
1528 free(event);
1529 count++;
1530 }
1531
1532 xcb_flush(wm->conn);
1533
1534 return count;
1535}
1536
1537static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001538weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1539{
1540 xcb_depth_iterator_t d_iter;
1541 xcb_visualtype_iterator_t vt_iter;
1542 xcb_visualtype_t *visualtype;
1543
1544 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1545 visualtype = NULL;
1546 while (d_iter.rem > 0) {
1547 if (d_iter.data->depth == 32) {
1548 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1549 visualtype = vt_iter.data;
1550 break;
1551 }
1552
1553 xcb_depth_next(&d_iter);
1554 }
1555
1556 if (visualtype == NULL) {
1557 weston_log("no 32 bit visualtype\n");
1558 return;
1559 }
1560
1561 wm->visual_id = visualtype->visual_id;
1562 wm->colormap = xcb_generate_id(wm->conn);
1563 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1564 wm->colormap, wm->screen->root, wm->visual_id);
1565}
1566
1567static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001568weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001569{
1570
1571#define F(field) offsetof(struct weston_wm, field)
1572
1573 static const struct { const char *name; int offset; } atoms[] = {
1574 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001575 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001576 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1577 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001578 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001579 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001580 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001581 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001582 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001583 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001584 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1585 { "_NET_WM_STATE", F(atom.net_wm_state) },
1586 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1587 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1588 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1589 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1590
1591 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1592 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1593 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1594 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1595 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1596 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1597 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001598 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1599 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001600 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1601 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1602 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1603 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1604 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1605
1606 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1607 { "_NET_SUPPORTING_WM_CHECK",
1608 F(atom.net_supporting_wm_check) },
1609 { "_NET_SUPPORTED", F(atom.net_supported) },
1610 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1611 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001612 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001613 { "TARGETS", F(atom.targets) },
1614 { "UTF8_STRING", F(atom.utf8_string) },
1615 { "_WL_SELECTION", F(atom.wl_selection) },
1616 { "INCR", F(atom.incr) },
1617 { "TIMESTAMP", F(atom.timestamp) },
1618 { "MULTIPLE", F(atom.multiple) },
1619 { "UTF8_STRING" , F(atom.utf8_string) },
1620 { "COMPOUND_TEXT", F(atom.compound_text) },
1621 { "TEXT", F(atom.text) },
1622 { "STRING", F(atom.string) },
1623 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1624 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001625 { "XdndSelection", F(atom.xdnd_selection) },
1626 { "XdndAware", F(atom.xdnd_aware) },
1627 { "XdndEnter", F(atom.xdnd_enter) },
1628 { "XdndLeave", F(atom.xdnd_leave) },
1629 { "XdndDrop", F(atom.xdnd_drop) },
1630 { "XdndStatus", F(atom.xdnd_status) },
1631 { "XdndFinished", F(atom.xdnd_finished) },
1632 { "XdndTypeList", F(atom.xdnd_type_list) },
1633 { "XdndActionCopy", F(atom.xdnd_action_copy) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001634 };
1635#undef F
1636
1637 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1638 xcb_xfixes_query_version_reply_t *xfixes_reply;
1639 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1640 xcb_intern_atom_reply_t *reply;
1641 xcb_render_query_pict_formats_reply_t *formats_reply;
1642 xcb_render_query_pict_formats_cookie_t formats_cookie;
1643 xcb_render_pictforminfo_t *formats;
1644 uint32_t i;
1645
1646 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1647
1648 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1649
1650 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1651 cookies[i] = xcb_intern_atom (wm->conn, 0,
1652 strlen(atoms[i].name),
1653 atoms[i].name);
1654
1655 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1656 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1657 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1658 free(reply);
1659 }
1660
1661 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1662 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001663 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001664
1665 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1666 XCB_XFIXES_MAJOR_VERSION,
1667 XCB_XFIXES_MINOR_VERSION);
1668 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1669 xfixes_cookie, NULL);
1670
Martin Minarik6d118362012-06-07 18:01:59 +02001671 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001672 xfixes_reply->major_version, xfixes_reply->minor_version);
1673
1674 free(xfixes_reply);
1675
1676 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1677 formats_cookie, 0);
1678 if (formats_reply == NULL)
1679 return;
1680
1681 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001682 for (i = 0; i < formats_reply->num_formats; i++) {
1683 if (formats[i].direct.red_mask != 0xff &&
1684 formats[i].direct.red_shift != 16)
1685 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001686 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1687 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001688 wm->format_rgb = formats[i];
1689 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1690 formats[i].depth == 32 &&
1691 formats[i].direct.alpha_mask == 0xff &&
1692 formats[i].direct.alpha_shift == 24)
1693 wm->format_rgba = formats[i];
1694 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001695
1696 free(formats_reply);
1697}
1698
1699static void
1700weston_wm_create_wm_window(struct weston_wm *wm)
1701{
1702 static const char name[] = "Weston WM";
1703
1704 wm->wm_window = xcb_generate_id(wm->conn);
1705 xcb_create_window(wm->conn,
1706 XCB_COPY_FROM_PARENT,
1707 wm->wm_window,
1708 wm->screen->root,
1709 0, 0,
1710 10, 10,
1711 0,
1712 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1713 wm->screen->root_visual,
1714 0, NULL);
1715
1716 xcb_change_property(wm->conn,
1717 XCB_PROP_MODE_REPLACE,
1718 wm->wm_window,
1719 wm->atom.net_supporting_wm_check,
1720 XCB_ATOM_WINDOW,
1721 32, /* format */
1722 1, &wm->wm_window);
1723
1724 xcb_change_property(wm->conn,
1725 XCB_PROP_MODE_REPLACE,
1726 wm->wm_window,
1727 wm->atom.net_wm_name,
1728 wm->atom.utf8_string,
1729 8, /* format */
1730 strlen(name), name);
1731
1732 xcb_change_property(wm->conn,
1733 XCB_PROP_MODE_REPLACE,
1734 wm->screen->root,
1735 wm->atom.net_supporting_wm_check,
1736 XCB_ATOM_WINDOW,
1737 32, /* format */
1738 1, &wm->wm_window);
1739
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001740 /* Claim the WM_S0 selection even though we don't suport
1741 * the --replace functionality. */
1742 xcb_set_selection_owner(wm->conn,
1743 wm->wm_window,
1744 wm->atom.wm_s0,
1745 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001746
1747 xcb_set_selection_owner(wm->conn,
1748 wm->wm_window,
1749 wm->atom.net_wm_cm_s0,
1750 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001751}
1752
1753struct weston_wm *
1754weston_wm_create(struct weston_xserver *wxs)
1755{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001756 struct weston_wm *wm;
1757 struct wl_event_loop *loop;
1758 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001759 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001760 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001761 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001762
Peter Huttererf3d62272013-08-08 11:57:05 +10001763 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001764 if (wm == NULL)
1765 return NULL;
1766
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001767 wm->server = wxs;
1768 wm->window_hash = hash_table_create();
1769 if (wm->window_hash == NULL) {
1770 free(wm);
1771 return NULL;
1772 }
1773
1774 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001775 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001776 hash_table_destroy(wm->window_hash);
1777 free(wm);
1778 return NULL;
1779 }
1780
1781 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001782 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001783 close(sv[1]);
1784
1785 /* xcb_connect_to_fd takes ownership of the fd. */
1786 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1787 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001788 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001789 close(sv[0]);
1790 hash_table_destroy(wm->window_hash);
1791 free(wm);
1792 return NULL;
1793 }
1794
1795 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1796 wm->screen = s.data;
1797
1798 loop = wl_display_get_event_loop(wxs->wl_display);
1799 wm->source =
1800 wl_event_loop_add_fd(loop, sv[0],
1801 WL_EVENT_READABLE,
1802 weston_wm_handle_event, wm);
1803 wl_event_source_check(wm->source);
1804
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001805 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001806 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001807
1808 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001809 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1810 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1811 XCB_EVENT_MASK_PROPERTY_CHANGE;
1812 xcb_change_window_attributes(wm->conn, wm->screen->root,
1813 XCB_CW_EVENT_MASK, values);
1814 wm->theme = theme_create();
1815
1816 weston_wm_create_wm_window(wm);
1817
1818 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001819 supported[1] = wm->atom.net_wm_state;
1820 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001821 xcb_change_property(wm->conn,
1822 XCB_PROP_MODE_REPLACE,
1823 wm->screen->root,
1824 wm->atom.net_supported,
1825 XCB_ATOM_ATOM,
1826 32, /* format */
1827 ARRAY_LENGTH(supported), supported);
1828
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001829 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001830
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001831 weston_wm_dnd_init(wm);
1832
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001833 xcb_flush(wm->conn);
1834
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001835 wm->activate_listener.notify = weston_wm_window_activate;
1836 wl_signal_add(&wxs->compositor->activate_signal,
1837 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001838 wm->transform_listener.notify = weston_wm_window_transform;
1839 wl_signal_add(&wxs->compositor->transform_signal,
1840 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001841 wm->kill_listener.notify = weston_wm_kill_client;
1842 wl_signal_add(&wxs->compositor->kill_signal,
1843 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001844
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001845 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001846 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001847
Martin Minarik6d118362012-06-07 18:01:59 +02001848 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001849
1850 return wm;
1851}
1852
1853void
1854weston_wm_destroy(struct weston_wm *wm)
1855{
1856 /* FIXME: Free windows in hash. */
1857 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001858 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001859 xcb_disconnect(wm->conn);
1860 wl_event_source_remove(wm->source);
1861 wl_list_remove(&wm->selection_listener.link);
1862 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001863 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01001864 wl_list_remove(&wm->transform_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001865
1866 free(wm);
1867}
1868
1869static void
1870surface_destroy(struct wl_listener *listener, void *data)
1871{
1872 struct weston_wm_window *window =
1873 container_of(listener,
1874 struct weston_wm_window, surface_destroy_listener);
1875
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001876 wm_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07001877
1878 window->surface = NULL;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001879}
1880
1881static struct weston_wm_window *
1882get_wm_window(struct weston_surface *surface)
1883{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001884 struct wl_listener *listener;
1885
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001886 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001887 if (listener)
1888 return container_of(listener, struct weston_wm_window,
1889 surface_destroy_listener);
1890
1891 return NULL;
1892}
1893
1894static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001895weston_wm_window_configure(void *data)
1896{
1897 struct weston_wm_window *window = data;
1898 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001899 uint32_t values[4];
1900 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001901
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001902 weston_wm_window_get_child_position(window, &x, &y);
1903 values[0] = x;
1904 values[1] = y;
1905 values[2] = window->width;
1906 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001907 xcb_configure_window(wm->conn,
1908 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001909 XCB_CONFIG_WINDOW_X |
1910 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001911 XCB_CONFIG_WINDOW_WIDTH |
1912 XCB_CONFIG_WINDOW_HEIGHT,
1913 values);
1914
1915 weston_wm_window_get_frame_size(window, &width, &height);
1916 values[0] = width;
1917 values[1] = height;
1918 xcb_configure_window(wm->conn,
1919 window->frame_id,
1920 XCB_CONFIG_WINDOW_WIDTH |
1921 XCB_CONFIG_WINDOW_HEIGHT,
1922 values);
1923
1924 window->configure_source = NULL;
1925
1926 weston_wm_window_schedule_repaint(window);
1927}
1928
1929static void
1930send_configure(struct weston_surface *surface,
1931 uint32_t edges, int32_t width, int32_t height)
1932{
1933 struct weston_wm_window *window = get_wm_window(surface);
1934 struct weston_wm *wm = window->wm;
1935 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001936 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001937
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001938 if (window->fullscreen) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001939 hborder = 0;
1940 vborder = 0;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001941 } else if (window->decorate) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001942 hborder = 2 * (t->margin + t->width);
1943 vborder = 2 * t->margin + t->titlebar_height + t->width;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001944 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001945 hborder = 2 * t->margin;
1946 vborder = 2 * t->margin;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001947 }
1948
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001949 if (width > hborder)
1950 window->width = width - hborder;
1951 else
1952 window->width = 1;
1953
1954 if (height > vborder)
1955 window->height = height - vborder;
1956 else
1957 window->height = 1;
1958
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001959 if (window->configure_source)
1960 return;
1961
1962 window->configure_source =
1963 wl_event_loop_add_idle(wm->server->loop,
1964 weston_wm_window_configure, window);
1965}
1966
1967static const struct weston_shell_client shell_client = {
1968 send_configure
1969};
1970
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07001971static int
1972legacy_fullscreen(struct weston_wm *wm,
1973 struct weston_wm_window *window,
1974 struct weston_output **output_ret)
1975{
1976 struct weston_compositor *compositor = wm->server->compositor;
1977 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001978 uint32_t minmax = PMinSize | PMaxSize;
1979 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07001980
1981 /* Heuristics for detecting legacy fullscreen windows... */
1982
1983 wl_list_for_each(output, &compositor->output_list, link) {
1984 if (output->x == window->x &&
1985 output->y == window->y &&
1986 output->width == window->width &&
1987 output->height == window->height &&
1988 window->override_redirect) {
1989 *output_ret = output;
1990 return 1;
1991 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001992
1993 matching_size = 0;
1994 if ((window->size_hints.flags & (USSize |PSize)) &&
1995 window->size_hints.width == output->width &&
1996 window->size_hints.height == output->height)
1997 matching_size = 1;
1998 if ((window->size_hints.flags & minmax) == minmax &&
1999 window->size_hints.min_width == output->width &&
2000 window->size_hints.min_height == output->height &&
2001 window->size_hints.max_width == output->width &&
2002 window->size_hints.max_height == output->height)
2003 matching_size = 1;
2004
2005 if (matching_size && !window->decorate &&
2006 (window->size_hints.flags & (USPosition | PPosition)) &&
2007 window->size_hints.x == output->x &&
2008 window->size_hints.y == output->y) {
2009 *output_ret = output;
2010 return 1;
2011 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002012 }
2013
2014 return 0;
2015}
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002016static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002017xserver_map_shell_surface(struct weston_wm *wm,
2018 struct weston_wm_window *window)
2019{
2020 struct weston_shell_interface *shell_interface =
2021 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002022 struct weston_output *output;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002023
2024 if (!shell_interface->create_shell_surface)
2025 return;
2026
2027 window->shsurf =
2028 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002029 window->surface,
2030 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002031
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002032 if (window->fullscreen) {
2033 window->saved_width = window->width;
2034 window->saved_height = window->height;
2035 shell_interface->set_fullscreen(window->shsurf,
2036 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2037 0, NULL);
2038 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002039 } else if (legacy_fullscreen(wm, window, &output)) {
2040 window->fullscreen = 1;
2041 shell_interface->set_fullscreen(window->shsurf,
2042 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2043 0, output);
Giulio Camuffoca43f092013-09-11 17:49:13 +02002044 } else if (!window->override_redirect && !window->transient_for) {
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002045 shell_interface->set_toplevel(window->shsurf);
2046 return;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002047 } else {
2048 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002049 window->x,
2050 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002051 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002052 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002053}
2054
2055static void
2056xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
2057 struct wl_resource *surface_resource, uint32_t id)
2058{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002059 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002060 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002061 struct weston_surface *surface =
2062 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002063 struct weston_wm_window *window;
2064
2065 if (client != wxs->client)
2066 return;
2067
2068 window = hash_table_lookup(wm->window_hash, id);
2069 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002070 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002071 return;
2072 }
2073
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002074 wm_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002075
2076 weston_wm_window_read_properties(window);
2077
2078 window->surface = (struct weston_surface *) surface;
2079 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002080 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002081 &window->surface_destroy_listener);
2082
2083 weston_wm_window_schedule_repaint(window);
2084 xserver_map_shell_surface(wm, window);
2085}
2086
2087const struct xserver_interface xserver_implementation = {
2088 xserver_set_window_id
2089};