blob: bb766d3b26a366b2d84eb6959bde1300c6ddb1e3 [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,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001005 xcb_window_t id, int width, int height, 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;
1029
MoD384a11a2013-06-22 11:04:21 -05001030 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1031 /* technically we should use XRender and check the visual format's
1032 alpha_mask, but checking depth is simpler and works in all known cases */
1033 if(geometry_reply != NULL)
1034 window->has_alpha = geometry_reply->depth == 32;
1035 free(geometry_reply);
1036
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001037 hash_table_insert(wm->window_hash, id, window);
1038}
1039
1040static void
1041weston_wm_window_destroy(struct weston_wm_window *window)
1042{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001043 struct weston_wm *wm = window->wm;
1044
1045 if (window->repaint_source)
1046 wl_event_source_remove(window->repaint_source);
1047 if (window->cairo_surface)
1048 cairo_surface_destroy(window->cairo_surface);
1049
1050 if (window->frame_id) {
1051 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1052 xcb_destroy_window(wm->conn, window->frame_id);
1053 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1054 hash_table_remove(wm->window_hash, window->frame_id);
1055 window->frame_id = XCB_WINDOW_NONE;
1056 }
1057
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001058 hash_table_remove(window->wm->window_hash, window->id);
1059 free(window);
1060}
1061
1062static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001063weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1064{
1065 xcb_create_notify_event_t *create_notify =
1066 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001067
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001068 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1069 create_notify->window,
1070 create_notify->width, create_notify->height,
1071 create_notify->override_redirect ? ", override" : "",
1072 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001073
1074 if (our_resource(wm, create_notify->window))
1075 return;
1076
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001077 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001078 create_notify->width, create_notify->height,
1079 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001080}
1081
1082static void
1083weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1084{
1085 xcb_destroy_notify_event_t *destroy_notify =
1086 (xcb_destroy_notify_event_t *) event;
1087 struct weston_wm_window *window;
1088
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001089 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1090 destroy_notify->window,
1091 destroy_notify->event,
1092 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001093
1094 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001095 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001096
1097 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001098 weston_wm_window_destroy(window);
1099}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001100
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001101static void
1102weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1103{
1104 xcb_reparent_notify_event_t *reparent_notify =
1105 (xcb_reparent_notify_event_t *) event;
1106 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001107
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001108 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1109 reparent_notify->window,
1110 reparent_notify->parent,
1111 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001112
1113 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001114 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
1115 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001116 } else if (!our_resource(wm, reparent_notify->parent)) {
1117 window = hash_table_lookup(wm->window_hash,
1118 reparent_notify->window);
1119 weston_wm_window_destroy(window);
1120 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001121}
1122
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001123struct weston_seat *
1124weston_wm_pick_seat(struct weston_wm *wm)
1125{
1126 return container_of(wm->server->compositor->seat_list.next,
1127 struct weston_seat, link);
1128}
1129
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001130static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001131weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1132 xcb_client_message_event_t *client_message)
1133{
1134 static const int map[] = {
1135 THEME_LOCATION_RESIZING_TOP_LEFT,
1136 THEME_LOCATION_RESIZING_TOP,
1137 THEME_LOCATION_RESIZING_TOP_RIGHT,
1138 THEME_LOCATION_RESIZING_RIGHT,
1139 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1140 THEME_LOCATION_RESIZING_BOTTOM,
1141 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1142 THEME_LOCATION_RESIZING_LEFT
1143 };
1144
1145 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001146 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001147 int detail;
1148 struct weston_shell_interface *shell_interface =
1149 &wm->server->compositor->shell_interface;
1150
Kristian Høgsberge3148752013-05-06 23:19:49 -04001151 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001152 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001153 return;
1154
1155 detail = client_message->data.data32[2];
1156 switch (detail) {
1157 case _NET_WM_MOVERESIZE_MOVE:
1158 shell_interface->move(window->shsurf, seat);
1159 break;
1160 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1161 case _NET_WM_MOVERESIZE_SIZE_TOP:
1162 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1163 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1164 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1165 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1166 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1167 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1168 shell_interface->resize(window->shsurf, seat, map[detail]);
1169 break;
1170 case _NET_WM_MOVERESIZE_CANCEL:
1171 break;
1172 }
1173}
1174
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001175#define _NET_WM_STATE_REMOVE 0
1176#define _NET_WM_STATE_ADD 1
1177#define _NET_WM_STATE_TOGGLE 2
1178
1179static int
1180update_state(int action, int *state)
1181{
1182 int new_state, changed;
1183
1184 switch (action) {
1185 case _NET_WM_STATE_REMOVE:
1186 new_state = 0;
1187 break;
1188 case _NET_WM_STATE_ADD:
1189 new_state = 1;
1190 break;
1191 case _NET_WM_STATE_TOGGLE:
1192 new_state = !*state;
1193 break;
1194 default:
1195 return 0;
1196 }
1197
1198 changed = (*state != new_state);
1199 *state = new_state;
1200
1201 return changed;
1202}
1203
1204static void
1205weston_wm_window_configure(void *data);
1206
1207static void
1208weston_wm_window_handle_state(struct weston_wm_window *window,
1209 xcb_client_message_event_t *client_message)
1210{
1211 struct weston_wm *wm = window->wm;
1212 struct weston_shell_interface *shell_interface =
1213 &wm->server->compositor->shell_interface;
1214 uint32_t action, property;
1215
1216 action = client_message->data.data32[0];
1217 property = client_message->data.data32[1];
1218
1219 if (property == wm->atom.net_wm_state_fullscreen &&
1220 update_state(action, &window->fullscreen)) {
1221 weston_wm_window_set_net_wm_state(window);
1222 if (window->fullscreen) {
1223 window->saved_width = window->width;
1224 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001225
1226 if (window->shsurf)
1227 shell_interface->set_fullscreen(window->shsurf,
1228 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1229 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001230 } else {
1231 shell_interface->set_toplevel(window->shsurf);
1232 window->width = window->saved_width;
1233 window->height = window->saved_height;
1234 weston_wm_window_configure(window);
1235 }
1236 }
1237}
1238
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001239static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001240weston_wm_handle_client_message(struct weston_wm *wm,
1241 xcb_generic_event_t *event)
1242{
1243 xcb_client_message_event_t *client_message =
1244 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001245 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001246
1247 window = hash_table_lookup(wm->window_hash, client_message->window);
1248
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001249 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1250 get_atom_name(wm->conn, client_message->type),
1251 client_message->data.data32[0],
1252 client_message->data.data32[1],
1253 client_message->data.data32[2],
1254 client_message->data.data32[3],
1255 client_message->data.data32[4],
1256 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001257
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001258 if (client_message->type == wm->atom.net_wm_moveresize)
1259 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001260 else if (client_message->type == wm->atom.net_wm_state)
1261 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001262}
1263
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001264enum cursor_type {
1265 XWM_CURSOR_TOP,
1266 XWM_CURSOR_BOTTOM,
1267 XWM_CURSOR_LEFT,
1268 XWM_CURSOR_RIGHT,
1269 XWM_CURSOR_TOP_LEFT,
1270 XWM_CURSOR_TOP_RIGHT,
1271 XWM_CURSOR_BOTTOM_LEFT,
1272 XWM_CURSOR_BOTTOM_RIGHT,
1273 XWM_CURSOR_LEFT_PTR,
1274};
1275
1276static const char *cursors[] = {
1277 "top_side",
1278 "bottom_side",
1279 "left_side",
1280 "right_side",
1281 "top_left_corner",
1282 "top_right_corner",
1283 "bottom_left_corner",
1284 "bottom_right_corner",
1285 "left_ptr"
1286};
1287
1288static void
1289weston_wm_create_cursors(struct weston_wm *wm)
1290{
1291 int i, count = ARRAY_LENGTH(cursors);
1292
1293 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1294 for (i = 0; i < count; i++) {
1295 wm->cursors[i] =
1296 xcb_cursor_library_load_cursor(wm, cursors[i]);
1297 }
1298
1299 wm->last_cursor = -1;
1300}
1301
1302static void
1303weston_wm_destroy_cursors(struct weston_wm *wm)
1304{
1305 uint8_t i;
1306
1307 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1308 xcb_free_cursor(wm->conn, wm->cursors[i]);
1309
1310 free(wm->cursors);
1311}
1312
1313static int
1314get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1315{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001316 int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001317
1318 switch (location) {
1319 case THEME_LOCATION_RESIZING_TOP:
1320 return XWM_CURSOR_TOP;
1321 case THEME_LOCATION_RESIZING_BOTTOM:
1322 return XWM_CURSOR_BOTTOM;
1323 case THEME_LOCATION_RESIZING_LEFT:
1324 return XWM_CURSOR_LEFT;
1325 case THEME_LOCATION_RESIZING_RIGHT:
1326 return XWM_CURSOR_RIGHT;
1327 case THEME_LOCATION_RESIZING_TOP_LEFT:
1328 return XWM_CURSOR_TOP_LEFT;
1329 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1330 return XWM_CURSOR_TOP_RIGHT;
1331 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1332 return XWM_CURSOR_BOTTOM_LEFT;
1333 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1334 return XWM_CURSOR_BOTTOM_RIGHT;
1335 case THEME_LOCATION_EXTERIOR:
1336 case THEME_LOCATION_TITLEBAR:
1337 default:
1338 return XWM_CURSOR_LEFT_PTR;
1339 }
1340}
1341
1342static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001343weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1344 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001345{
1346 uint32_t cursor_value_list;
1347
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001348 if (wm->last_cursor == cursor)
1349 return;
1350
1351 wm->last_cursor = cursor;
1352
1353 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001354 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001355 XCB_CW_CURSOR, &cursor_value_list);
1356 xcb_flush(wm->conn);
1357}
1358
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001359static void
1360weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1361{
1362 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1363 struct weston_shell_interface *shell_interface =
1364 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001365 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001366 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001367 enum theme_location location;
1368 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001369 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001370
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001371 wm_log("XCB_BUTTON_%s (detail %d)\n",
1372 button->response_type == XCB_BUTTON_PRESS ?
1373 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001374
1375 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001376 weston_wm_window_get_frame_size(window, &width, &height);
1377
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001378 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001379 button->detail == 1) {
1380 location = theme_get_location(t,
1381 button->event_x,
1382 button->event_y,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001383 width, height, 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001384
1385 switch (location) {
1386 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001387 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001388 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001389 case THEME_LOCATION_RESIZING_TOP:
1390 case THEME_LOCATION_RESIZING_BOTTOM:
1391 case THEME_LOCATION_RESIZING_LEFT:
1392 case THEME_LOCATION_RESIZING_RIGHT:
1393 case THEME_LOCATION_RESIZING_TOP_LEFT:
1394 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1395 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1396 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1397 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001398 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001399 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001400 default:
1401 break;
1402 }
1403 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001404}
1405
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001406static void
1407weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1408{
1409 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1410 struct weston_wm_window *window;
1411 int cursor, width, height;
1412
1413 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001414 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001415 return;
1416
1417 weston_wm_window_get_frame_size(window, &width, &height);
1418 cursor = get_cursor_for_location(wm->theme, width, height,
1419 motion->event_x, motion->event_y);
1420
Tiago Vignattic1903232012-07-16 12:15:37 -04001421 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001422}
1423
1424static void
1425weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1426{
1427 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1428 struct weston_wm_window *window;
1429 int cursor, width, height;
1430
1431 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001432 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001433 return;
1434
1435 weston_wm_window_get_frame_size(window, &width, &height);
1436 cursor = get_cursor_for_location(wm->theme, width, height,
1437 enter->event_x, enter->event_y);
1438
Tiago Vignattic1903232012-07-16 12:15:37 -04001439 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001440}
1441
1442static void
1443weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1444{
1445 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1446 struct weston_wm_window *window;
1447
1448 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001449 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001450 return;
1451
Tiago Vignattic1903232012-07-16 12:15:37 -04001452 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001453}
1454
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001455static int
1456weston_wm_handle_event(int fd, uint32_t mask, void *data)
1457{
1458 struct weston_wm *wm = data;
1459 xcb_generic_event_t *event;
1460 int count = 0;
1461
1462 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1463 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001464 free(event);
1465 count++;
1466 continue;
1467 }
1468
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001469 if (weston_wm_handle_dnd_event(wm, event)) {
1470 free(event);
1471 count++;
1472 continue;
1473 }
1474
MoD31700122013-06-11 19:58:55 -05001475 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001476 case XCB_BUTTON_PRESS:
1477 case XCB_BUTTON_RELEASE:
1478 weston_wm_handle_button(wm, event);
1479 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001480 case XCB_ENTER_NOTIFY:
1481 weston_wm_handle_enter(wm, event);
1482 break;
1483 case XCB_LEAVE_NOTIFY:
1484 weston_wm_handle_leave(wm, event);
1485 break;
1486 case XCB_MOTION_NOTIFY:
1487 weston_wm_handle_motion(wm, event);
1488 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001489 case XCB_CREATE_NOTIFY:
1490 weston_wm_handle_create_notify(wm, event);
1491 break;
1492 case XCB_MAP_REQUEST:
1493 weston_wm_handle_map_request(wm, event);
1494 break;
1495 case XCB_MAP_NOTIFY:
1496 weston_wm_handle_map_notify(wm, event);
1497 break;
1498 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001499 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001500 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001501 case XCB_REPARENT_NOTIFY:
1502 weston_wm_handle_reparent_notify(wm, event);
1503 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001504 case XCB_CONFIGURE_REQUEST:
1505 weston_wm_handle_configure_request(wm, event);
1506 break;
1507 case XCB_CONFIGURE_NOTIFY:
1508 weston_wm_handle_configure_notify(wm, event);
1509 break;
1510 case XCB_DESTROY_NOTIFY:
1511 weston_wm_handle_destroy_notify(wm, event);
1512 break;
1513 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001514 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001515 break;
1516 case XCB_PROPERTY_NOTIFY:
1517 weston_wm_handle_property_notify(wm, event);
1518 break;
1519 case XCB_CLIENT_MESSAGE:
1520 weston_wm_handle_client_message(wm, event);
1521 break;
1522 }
1523
1524 free(event);
1525 count++;
1526 }
1527
1528 xcb_flush(wm->conn);
1529
1530 return count;
1531}
1532
1533static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001534weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1535{
1536 xcb_depth_iterator_t d_iter;
1537 xcb_visualtype_iterator_t vt_iter;
1538 xcb_visualtype_t *visualtype;
1539
1540 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1541 visualtype = NULL;
1542 while (d_iter.rem > 0) {
1543 if (d_iter.data->depth == 32) {
1544 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1545 visualtype = vt_iter.data;
1546 break;
1547 }
1548
1549 xcb_depth_next(&d_iter);
1550 }
1551
1552 if (visualtype == NULL) {
1553 weston_log("no 32 bit visualtype\n");
1554 return;
1555 }
1556
1557 wm->visual_id = visualtype->visual_id;
1558 wm->colormap = xcb_generate_id(wm->conn);
1559 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1560 wm->colormap, wm->screen->root, wm->visual_id);
1561}
1562
1563static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001564weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001565{
1566
1567#define F(field) offsetof(struct weston_wm, field)
1568
1569 static const struct { const char *name; int offset; } atoms[] = {
1570 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001571 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001572 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1573 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001574 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001575 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001576 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001577 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001578 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001579 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001580 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1581 { "_NET_WM_STATE", F(atom.net_wm_state) },
1582 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1583 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1584 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1585 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1586
1587 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1588 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1589 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1590 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1591 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1592 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1593 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001594 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1595 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001596 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1597 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1598 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1599 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1600 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1601
1602 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1603 { "_NET_SUPPORTING_WM_CHECK",
1604 F(atom.net_supporting_wm_check) },
1605 { "_NET_SUPPORTED", F(atom.net_supported) },
1606 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1607 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001608 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001609 { "TARGETS", F(atom.targets) },
1610 { "UTF8_STRING", F(atom.utf8_string) },
1611 { "_WL_SELECTION", F(atom.wl_selection) },
1612 { "INCR", F(atom.incr) },
1613 { "TIMESTAMP", F(atom.timestamp) },
1614 { "MULTIPLE", F(atom.multiple) },
1615 { "UTF8_STRING" , F(atom.utf8_string) },
1616 { "COMPOUND_TEXT", F(atom.compound_text) },
1617 { "TEXT", F(atom.text) },
1618 { "STRING", F(atom.string) },
1619 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1620 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001621 { "XdndSelection", F(atom.xdnd_selection) },
1622 { "XdndAware", F(atom.xdnd_aware) },
1623 { "XdndEnter", F(atom.xdnd_enter) },
1624 { "XdndLeave", F(atom.xdnd_leave) },
1625 { "XdndDrop", F(atom.xdnd_drop) },
1626 { "XdndStatus", F(atom.xdnd_status) },
1627 { "XdndFinished", F(atom.xdnd_finished) },
1628 { "XdndTypeList", F(atom.xdnd_type_list) },
1629 { "XdndActionCopy", F(atom.xdnd_action_copy) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001630 };
1631#undef F
1632
1633 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1634 xcb_xfixes_query_version_reply_t *xfixes_reply;
1635 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1636 xcb_intern_atom_reply_t *reply;
1637 xcb_render_query_pict_formats_reply_t *formats_reply;
1638 xcb_render_query_pict_formats_cookie_t formats_cookie;
1639 xcb_render_pictforminfo_t *formats;
1640 uint32_t i;
1641
1642 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1643
1644 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1645
1646 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1647 cookies[i] = xcb_intern_atom (wm->conn, 0,
1648 strlen(atoms[i].name),
1649 atoms[i].name);
1650
1651 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1652 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1653 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1654 free(reply);
1655 }
1656
1657 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1658 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001659 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001660
1661 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1662 XCB_XFIXES_MAJOR_VERSION,
1663 XCB_XFIXES_MINOR_VERSION);
1664 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1665 xfixes_cookie, NULL);
1666
Martin Minarik6d118362012-06-07 18:01:59 +02001667 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001668 xfixes_reply->major_version, xfixes_reply->minor_version);
1669
1670 free(xfixes_reply);
1671
1672 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1673 formats_cookie, 0);
1674 if (formats_reply == NULL)
1675 return;
1676
1677 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001678 for (i = 0; i < formats_reply->num_formats; i++) {
1679 if (formats[i].direct.red_mask != 0xff &&
1680 formats[i].direct.red_shift != 16)
1681 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001682 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1683 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001684 wm->format_rgb = formats[i];
1685 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1686 formats[i].depth == 32 &&
1687 formats[i].direct.alpha_mask == 0xff &&
1688 formats[i].direct.alpha_shift == 24)
1689 wm->format_rgba = formats[i];
1690 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001691
1692 free(formats_reply);
1693}
1694
1695static void
1696weston_wm_create_wm_window(struct weston_wm *wm)
1697{
1698 static const char name[] = "Weston WM";
1699
1700 wm->wm_window = xcb_generate_id(wm->conn);
1701 xcb_create_window(wm->conn,
1702 XCB_COPY_FROM_PARENT,
1703 wm->wm_window,
1704 wm->screen->root,
1705 0, 0,
1706 10, 10,
1707 0,
1708 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1709 wm->screen->root_visual,
1710 0, NULL);
1711
1712 xcb_change_property(wm->conn,
1713 XCB_PROP_MODE_REPLACE,
1714 wm->wm_window,
1715 wm->atom.net_supporting_wm_check,
1716 XCB_ATOM_WINDOW,
1717 32, /* format */
1718 1, &wm->wm_window);
1719
1720 xcb_change_property(wm->conn,
1721 XCB_PROP_MODE_REPLACE,
1722 wm->wm_window,
1723 wm->atom.net_wm_name,
1724 wm->atom.utf8_string,
1725 8, /* format */
1726 strlen(name), name);
1727
1728 xcb_change_property(wm->conn,
1729 XCB_PROP_MODE_REPLACE,
1730 wm->screen->root,
1731 wm->atom.net_supporting_wm_check,
1732 XCB_ATOM_WINDOW,
1733 32, /* format */
1734 1, &wm->wm_window);
1735
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001736 /* Claim the WM_S0 selection even though we don't suport
1737 * the --replace functionality. */
1738 xcb_set_selection_owner(wm->conn,
1739 wm->wm_window,
1740 wm->atom.wm_s0,
1741 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001742
1743 xcb_set_selection_owner(wm->conn,
1744 wm->wm_window,
1745 wm->atom.net_wm_cm_s0,
1746 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001747}
1748
1749struct weston_wm *
1750weston_wm_create(struct weston_xserver *wxs)
1751{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001752 struct weston_wm *wm;
1753 struct wl_event_loop *loop;
1754 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001755 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001756 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001757 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001758
Peter Huttererf3d62272013-08-08 11:57:05 +10001759 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001760 if (wm == NULL)
1761 return NULL;
1762
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001763 wm->server = wxs;
1764 wm->window_hash = hash_table_create();
1765 if (wm->window_hash == NULL) {
1766 free(wm);
1767 return NULL;
1768 }
1769
1770 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001771 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001772 hash_table_destroy(wm->window_hash);
1773 free(wm);
1774 return NULL;
1775 }
1776
1777 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001778 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001779 close(sv[1]);
1780
1781 /* xcb_connect_to_fd takes ownership of the fd. */
1782 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1783 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001784 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001785 close(sv[0]);
1786 hash_table_destroy(wm->window_hash);
1787 free(wm);
1788 return NULL;
1789 }
1790
1791 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1792 wm->screen = s.data;
1793
1794 loop = wl_display_get_event_loop(wxs->wl_display);
1795 wm->source =
1796 wl_event_loop_add_fd(loop, sv[0],
1797 WL_EVENT_READABLE,
1798 weston_wm_handle_event, wm);
1799 wl_event_source_check(wm->source);
1800
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001801 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001802 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001803
1804 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001805 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1806 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1807 XCB_EVENT_MASK_PROPERTY_CHANGE;
1808 xcb_change_window_attributes(wm->conn, wm->screen->root,
1809 XCB_CW_EVENT_MASK, values);
1810 wm->theme = theme_create();
1811
1812 weston_wm_create_wm_window(wm);
1813
1814 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001815 supported[1] = wm->atom.net_wm_state;
1816 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001817 xcb_change_property(wm->conn,
1818 XCB_PROP_MODE_REPLACE,
1819 wm->screen->root,
1820 wm->atom.net_supported,
1821 XCB_ATOM_ATOM,
1822 32, /* format */
1823 ARRAY_LENGTH(supported), supported);
1824
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001825 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001826
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001827 weston_wm_dnd_init(wm);
1828
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001829 xcb_flush(wm->conn);
1830
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001831 wm->activate_listener.notify = weston_wm_window_activate;
1832 wl_signal_add(&wxs->compositor->activate_signal,
1833 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001834 wm->transform_listener.notify = weston_wm_window_transform;
1835 wl_signal_add(&wxs->compositor->transform_signal,
1836 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001837 wm->kill_listener.notify = weston_wm_kill_client;
1838 wl_signal_add(&wxs->compositor->kill_signal,
1839 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001840
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001841 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001842 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001843
Martin Minarik6d118362012-06-07 18:01:59 +02001844 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001845
1846 return wm;
1847}
1848
1849void
1850weston_wm_destroy(struct weston_wm *wm)
1851{
1852 /* FIXME: Free windows in hash. */
1853 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001854 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001855 xcb_disconnect(wm->conn);
1856 wl_event_source_remove(wm->source);
1857 wl_list_remove(&wm->selection_listener.link);
1858 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001859 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01001860 wl_list_remove(&wm->transform_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001861
1862 free(wm);
1863}
1864
1865static void
1866surface_destroy(struct wl_listener *listener, void *data)
1867{
1868 struct weston_wm_window *window =
1869 container_of(listener,
1870 struct weston_wm_window, surface_destroy_listener);
1871
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001872 wm_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07001873
1874 window->surface = NULL;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001875}
1876
1877static struct weston_wm_window *
1878get_wm_window(struct weston_surface *surface)
1879{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001880 struct wl_listener *listener;
1881
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001882 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001883 if (listener)
1884 return container_of(listener, struct weston_wm_window,
1885 surface_destroy_listener);
1886
1887 return NULL;
1888}
1889
1890static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001891weston_wm_window_configure(void *data)
1892{
1893 struct weston_wm_window *window = data;
1894 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001895 uint32_t values[4];
1896 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001897
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001898 weston_wm_window_get_child_position(window, &x, &y);
1899 values[0] = x;
1900 values[1] = y;
1901 values[2] = window->width;
1902 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001903 xcb_configure_window(wm->conn,
1904 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001905 XCB_CONFIG_WINDOW_X |
1906 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001907 XCB_CONFIG_WINDOW_WIDTH |
1908 XCB_CONFIG_WINDOW_HEIGHT,
1909 values);
1910
1911 weston_wm_window_get_frame_size(window, &width, &height);
1912 values[0] = width;
1913 values[1] = height;
1914 xcb_configure_window(wm->conn,
1915 window->frame_id,
1916 XCB_CONFIG_WINDOW_WIDTH |
1917 XCB_CONFIG_WINDOW_HEIGHT,
1918 values);
1919
1920 window->configure_source = NULL;
1921
1922 weston_wm_window_schedule_repaint(window);
1923}
1924
1925static void
1926send_configure(struct weston_surface *surface,
1927 uint32_t edges, int32_t width, int32_t height)
1928{
1929 struct weston_wm_window *window = get_wm_window(surface);
1930 struct weston_wm *wm = window->wm;
1931 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001932 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001933
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001934 if (window->fullscreen) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001935 hborder = 0;
1936 vborder = 0;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001937 } else if (window->decorate) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001938 hborder = 2 * (t->margin + t->width);
1939 vborder = 2 * t->margin + t->titlebar_height + t->width;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001940 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001941 hborder = 2 * t->margin;
1942 vborder = 2 * t->margin;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001943 }
1944
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001945 if (width > hborder)
1946 window->width = width - hborder;
1947 else
1948 window->width = 1;
1949
1950 if (height > vborder)
1951 window->height = height - vborder;
1952 else
1953 window->height = 1;
1954
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001955 if (window->configure_source)
1956 return;
1957
1958 window->configure_source =
1959 wl_event_loop_add_idle(wm->server->loop,
1960 weston_wm_window_configure, window);
1961}
1962
1963static const struct weston_shell_client shell_client = {
1964 send_configure
1965};
1966
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07001967static int
1968legacy_fullscreen(struct weston_wm *wm,
1969 struct weston_wm_window *window,
1970 struct weston_output **output_ret)
1971{
1972 struct weston_compositor *compositor = wm->server->compositor;
1973 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001974 uint32_t minmax = PMinSize | PMaxSize;
1975 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07001976
1977 /* Heuristics for detecting legacy fullscreen windows... */
1978
1979 wl_list_for_each(output, &compositor->output_list, link) {
1980 if (output->x == window->x &&
1981 output->y == window->y &&
1982 output->width == window->width &&
1983 output->height == window->height &&
1984 window->override_redirect) {
1985 *output_ret = output;
1986 return 1;
1987 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001988
1989 matching_size = 0;
1990 if ((window->size_hints.flags & (USSize |PSize)) &&
1991 window->size_hints.width == output->width &&
1992 window->size_hints.height == output->height)
1993 matching_size = 1;
1994 if ((window->size_hints.flags & minmax) == minmax &&
1995 window->size_hints.min_width == output->width &&
1996 window->size_hints.min_height == output->height &&
1997 window->size_hints.max_width == output->width &&
1998 window->size_hints.max_height == output->height)
1999 matching_size = 1;
2000
2001 if (matching_size && !window->decorate &&
2002 (window->size_hints.flags & (USPosition | PPosition)) &&
2003 window->size_hints.x == output->x &&
2004 window->size_hints.y == output->y) {
2005 *output_ret = output;
2006 return 1;
2007 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002008 }
2009
2010 return 0;
2011}
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002012static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002013xserver_map_shell_surface(struct weston_wm *wm,
2014 struct weston_wm_window *window)
2015{
2016 struct weston_shell_interface *shell_interface =
2017 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002018 struct weston_output *output;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002019
2020 if (!shell_interface->create_shell_surface)
2021 return;
2022
2023 window->shsurf =
2024 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002025 window->surface,
2026 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002027
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002028 if (window->fullscreen) {
2029 window->saved_width = window->width;
2030 window->saved_height = window->height;
2031 shell_interface->set_fullscreen(window->shsurf,
2032 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2033 0, NULL);
2034 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002035 } else if (legacy_fullscreen(wm, window, &output)) {
2036 window->fullscreen = 1;
2037 shell_interface->set_fullscreen(window->shsurf,
2038 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2039 0, output);
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002040 } else if (!window->override_redirect) {
2041 shell_interface->set_toplevel(window->shsurf);
2042 return;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002043 } else {
2044 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002045 window->x,
2046 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002047 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002048 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002049}
2050
2051static void
2052xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
2053 struct wl_resource *surface_resource, uint32_t id)
2054{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002055 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002056 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002057 struct weston_surface *surface =
2058 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002059 struct weston_wm_window *window;
2060
2061 if (client != wxs->client)
2062 return;
2063
2064 window = hash_table_lookup(wm->window_hash, id);
2065 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002066 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002067 return;
2068 }
2069
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002070 wm_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002071
2072 weston_wm_window_read_properties(window);
2073
2074 window->surface = (struct weston_surface *) surface;
2075 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002076 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002077 &window->surface_destroy_listener);
2078
2079 weston_wm_window_schedule_repaint(window);
2080 xserver_map_shell_surface(wm, window);
2081}
2082
2083const struct xserver_interface xserver_implementation = {
2084 xserver_set_window_id
2085};