blob: b4f64d30ca89ddec70d0550a4f8664e4b6f9bcf0 [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;
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200366 struct weston_shell_interface *shell_interface =
367 &wm->server->compositor->shell_interface;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400368
369#define F(field) offsetof(struct weston_wm_window, field)
370 const struct {
371 xcb_atom_t atom;
372 xcb_atom_t type;
373 int offset;
374 } props[] = {
375 { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) },
376 { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) },
377 { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) },
378 { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700379 { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) },
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500380 { wm->atom.net_wm_state, TYPE_NET_WM_STATE },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400381 { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) },
382 { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300383 { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400384 { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300385 { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400386 };
387#undef F
388
389 xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)];
390 xcb_get_property_reply_t *reply;
391 void *p;
392 uint32_t *xid;
393 xcb_atom_t *atom;
394 uint32_t i;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400395
396 if (!window->properties_dirty)
397 return;
398 window->properties_dirty = 0;
399
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400400 for (i = 0; i < ARRAY_LENGTH(props); i++)
401 cookie[i] = xcb_get_property(wm->conn,
402 0, /* delete */
403 window->id,
404 props[i].atom,
405 XCB_ATOM_ANY, 0, 2048);
406
Tiago Vignatti2ea74d92012-07-20 23:09:53 +0300407 window->decorate = !window->override_redirect;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700408 window->size_hints.flags = 0;
409 window->motif_hints.flags = 0;
410
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400411 for (i = 0; i < ARRAY_LENGTH(props); i++) {
412 reply = xcb_get_property_reply(wm->conn, cookie[i], NULL);
413 if (!reply)
414 /* Bad window, typically */
415 continue;
416 if (reply->type == XCB_ATOM_NONE) {
417 /* No such property */
418 free(reply);
419 continue;
420 }
421
422 p = ((char *) window + props[i].offset);
423
424 switch (props[i].type) {
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300425 case XCB_ATOM_WM_CLIENT_MACHINE:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400426 case XCB_ATOM_STRING:
427 /* FIXME: We're using this for both string and
428 utf8_string */
429 if (*(char **) p)
430 free(*(char **) p);
431
432 *(char **) p =
433 strndup(xcb_get_property_value(reply),
434 xcb_get_property_value_length(reply));
435 break;
436 case XCB_ATOM_WINDOW:
437 xid = xcb_get_property_value(reply);
438 *(struct weston_wm_window **) p =
439 hash_table_lookup(wm->window_hash, *xid);
440 break;
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300441 case XCB_ATOM_CARDINAL:
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400442 case XCB_ATOM_ATOM:
443 atom = xcb_get_property_value(reply);
444 *(xcb_atom_t *) p = *atom;
445 break;
446 case TYPE_WM_PROTOCOLS:
447 break;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700448 case TYPE_WM_NORMAL_HINTS:
449 memcpy(&window->size_hints,
450 xcb_get_property_value(reply),
451 sizeof window->size_hints);
452 break;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500453 case TYPE_NET_WM_STATE:
454 window->fullscreen = 0;
455 atom = xcb_get_property_value(reply);
456 for (i = 0; i < reply->value_len; i++)
457 if (atom[i] == wm->atom.net_wm_state_fullscreen)
458 window->fullscreen = 1;
459 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400460 case TYPE_MOTIF_WM_HINTS:
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -0700461 memcpy(&window->motif_hints,
462 xcb_get_property_value(reply),
463 sizeof window->motif_hints);
464 if (window->motif_hints.flags & MWM_HINTS_DECORATIONS)
465 window->decorate =
466 window->motif_hints.decorations > 0;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400467 break;
468 default:
469 break;
470 }
471 free(reply);
472 }
Giulio Camuffo62942ad2013-09-11 18:20:47 +0200473
474 if (window->shsurf && window->name)
475 shell_interface->set_title(window->shsurf, window->name);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400476}
477
478static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400479weston_wm_window_get_frame_size(struct weston_wm_window *window,
480 int *width, int *height)
481{
482 struct theme *t = window->wm->theme;
483
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500484 if (window->fullscreen) {
485 *width = window->width;
486 *height = window->height;
487 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400488 *width = window->width + (t->margin + t->width) * 2;
489 *height = window->height +
490 t->margin * 2 + t->width + t->titlebar_height;
491 } else {
492 *width = window->width + t->margin * 2;
493 *height = window->height + t->margin * 2;
494 }
495}
496
497static void
498weston_wm_window_get_child_position(struct weston_wm_window *window,
499 int *x, int *y)
500{
501 struct theme *t = window->wm->theme;
502
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500503 if (window->fullscreen) {
504 *x = 0;
505 *y = 0;
506 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400507 *x = t->margin + t->width;
508 *y = t->margin + t->titlebar_height;
509 } else {
510 *x = t->margin;
511 *y = t->margin;
512 }
513}
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400514
515static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500516weston_wm_window_send_configure_notify(struct weston_wm_window *window)
517{
518 xcb_configure_notify_event_t configure_notify;
519 struct weston_wm *wm = window->wm;
520 int x, y;
521
522 weston_wm_window_get_child_position(window, &x, &y);
523 configure_notify.response_type = XCB_CONFIGURE_NOTIFY;
524 configure_notify.pad0 = 0;
525 configure_notify.event = window->id;
526 configure_notify.window = window->id;
527 configure_notify.above_sibling = XCB_WINDOW_NONE;
528 configure_notify.x = x;
529 configure_notify.y = y;
530 configure_notify.width = window->width;
531 configure_notify.height = window->height;
532 configure_notify.border_width = 0;
533 configure_notify.override_redirect = 0;
534 configure_notify.pad1 = 0;
535
536 xcb_send_event(wm->conn, 0, window->id,
537 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
538 (char *) &configure_notify);
539}
540
541static void
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400542weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event)
543{
544 xcb_configure_request_event_t *configure_request =
545 (xcb_configure_request_event_t *) event;
546 struct weston_wm_window *window;
547 uint32_t mask, values[16];
548 int x, y, width, height, i = 0;
549
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400550 wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n",
551 configure_request->window,
552 configure_request->x, configure_request->y,
553 configure_request->width, configure_request->height);
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400554
555 window = hash_table_lookup(wm->window_hash, configure_request->window);
556
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500557 if (window->fullscreen) {
558 weston_wm_window_send_configure_notify(window);
559 return;
560 }
561
Kristian Høgsbergeaee7842012-05-22 10:04:20 -0400562 if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH)
563 window->width = configure_request->width;
564 if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
565 window->height = configure_request->height;
566
567 weston_wm_window_get_child_position(window, &x, &y);
568 values[i++] = x;
569 values[i++] = y;
570 values[i++] = window->width;
571 values[i++] = window->height;
572 values[i++] = 0;
573 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
574 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
575 XCB_CONFIG_WINDOW_BORDER_WIDTH;
576 if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
577 values[i++] = configure_request->sibling;
578 mask |= XCB_CONFIG_WINDOW_SIBLING;
579 }
580 if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
581 values[i++] = configure_request->stack_mode;
582 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
583 }
584
585 xcb_configure_window(wm->conn, window->id, mask, values);
586
587 weston_wm_window_get_frame_size(window, &width, &height);
588 values[0] = width;
589 values[1] = height;
590 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
591 xcb_configure_window(wm->conn, window->frame_id, mask, values);
592
593 weston_wm_window_schedule_repaint(window);
594}
595
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400596static int
597our_resource(struct weston_wm *wm, uint32_t id)
598{
599 const xcb_setup_t *setup;
600
601 setup = xcb_get_setup(wm->conn);
602
603 return (id & ~setup->resource_id_mask) == setup->resource_id_base;
604}
605
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400606static void
607weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event)
608{
609 xcb_configure_notify_event_t *configure_notify =
610 (xcb_configure_notify_event_t *) event;
611 struct weston_wm_window *window;
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300612 int x, y;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400613
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400614 wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n",
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400615 configure_notify->window,
616 configure_notify->x, configure_notify->y,
617 configure_notify->width, configure_notify->height);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300618
Kristian Høgsberg00db2ee2013-07-04 02:29:32 -0400619 window = hash_table_lookup(wm->window_hash, configure_notify->window);
Tiago Vignattie66fcee2012-07-20 23:09:54 +0300620 weston_wm_window_get_child_position(window, &x, &y);
Kristian Høgsberg122877d2013-08-22 16:18:17 -0700621 window->x = configure_notify->x;
622 window->y = configure_notify->y;
Kristian Høgsberg1b6fed42013-08-31 00:00:57 -0700623 if (window->override_redirect) {
624 window->width = configure_notify->width;
625 window->height = configure_notify->height;
626 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400627}
628
629static void
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +0300630weston_wm_kill_client(struct wl_listener *listener, void *data)
631{
632 struct weston_surface *surface = data;
633 struct weston_wm_window *window = get_wm_window(surface);
634 char name[1024];
635
636 if (!window)
637 return;
638
639 gethostname(name, 1024);
640
641 /* this is only one heuristic to guess the PID of a client is valid,
642 * assuming it's compliant with icccm and ewmh. Non-compliants and
643 * remote applications of course fail. */
644 if (!strcmp(window->machine, name) && window->pid != 0)
645 kill(window->pid, SIGKILL);
646}
647
648static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400649weston_wm_window_activate(struct wl_listener *listener, void *data)
650{
651 struct weston_surface *surface = data;
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200652 struct weston_wm_window *window = NULL;
Scott Moreau85ecac02012-05-21 15:49:14 -0600653 struct weston_wm *wm =
654 container_of(listener, struct weston_wm, activate_listener);
655 xcb_client_message_event_t client_message;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400656
Giulio Camuffo9f42e502013-08-13 11:42:02 +0200657 if (surface) {
658 window = get_wm_window(surface);
659 }
660
Scott Moreau85ecac02012-05-21 15:49:14 -0600661 if (window) {
662 client_message.response_type = XCB_CLIENT_MESSAGE;
663 client_message.format = 32;
664 client_message.window = window->id;
665 client_message.type = wm->atom.wm_protocols;
666 client_message.data.data32[0] = wm->atom.wm_take_focus;
667 client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
668
669 xcb_send_event(wm->conn, 0, window->id,
670 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
671 (char *) &client_message);
672
673 xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
674 window->id, XCB_TIME_CURRENT_TIME);
675 } else {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400676 xcb_set_input_focus (wm->conn,
677 XCB_INPUT_FOCUS_POINTER_ROOT,
678 XCB_NONE,
679 XCB_TIME_CURRENT_TIME);
Scott Moreau85ecac02012-05-21 15:49:14 -0600680 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400681
682 if (wm->focus_window)
683 weston_wm_window_schedule_repaint(wm->focus_window);
684 wm->focus_window = window;
685 if (wm->focus_window)
686 weston_wm_window_schedule_repaint(wm->focus_window);
687}
688
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300689static void
690weston_wm_window_transform(struct wl_listener *listener, void *data)
691{
692 struct weston_surface *surface = data;
693 struct weston_wm_window *window = get_wm_window(surface);
694 struct weston_wm *wm =
695 container_of(listener, struct weston_wm, transform_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300696 uint32_t mask, values[2];
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300697
698 if (!window || !wm)
699 return;
700
701 if (!weston_surface_is_mapped(surface))
702 return;
703
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700704 if (window->x != surface->geometry.x ||
705 window->y != surface->geometry.y) {
706 values[0] = surface->geometry.x;
707 values[1] = surface->geometry.y;
708 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300709
Kristian Høgsberge89a8b62013-08-22 16:20:44 -0700710 xcb_configure_window(wm->conn, window->frame_id, mask, values);
711 xcb_flush(wm->conn);
712 }
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300713}
714
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400715#define ICCCM_WITHDRAWN_STATE 0
716#define ICCCM_NORMAL_STATE 1
717#define ICCCM_ICONIC_STATE 3
718
719static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500720weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state)
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -0400721{
722 struct weston_wm *wm = window->wm;
723 uint32_t property[2];
724
725 property[0] = state;
726 property[1] = XCB_WINDOW_NONE;
727
728 xcb_change_property(wm->conn,
729 XCB_PROP_MODE_REPLACE,
730 window->id,
731 wm->atom.wm_state,
732 wm->atom.wm_state,
733 32, /* format */
734 2, property);
735}
736
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400737static void
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500738weston_wm_window_set_net_wm_state(struct weston_wm_window *window)
739{
740 struct weston_wm *wm = window->wm;
741 uint32_t property[1];
742 int i;
743
744 i = 0;
745 if (window->fullscreen)
746 property[i++] = wm->atom.net_wm_state_fullscreen;
747
748 xcb_change_property(wm->conn,
749 XCB_PROP_MODE_REPLACE,
750 window->id,
751 wm->atom.net_wm_state,
752 XCB_ATOM_ATOM,
753 32, /* format */
754 i, property);
755}
756
757static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700758weston_wm_window_create_frame(struct weston_wm_window *window)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400759{
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700760 struct weston_wm *wm = window->wm;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400761 uint32_t values[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400762 int x, y, width, height;
763
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400764 weston_wm_window_get_frame_size(window, &width, &height);
765 weston_wm_window_get_child_position(window, &x, &y);
766
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400767 values[0] = wm->screen->black_pixel;
768 values[1] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400769 XCB_EVENT_MASK_KEY_PRESS |
770 XCB_EVENT_MASK_KEY_RELEASE |
771 XCB_EVENT_MASK_BUTTON_PRESS |
772 XCB_EVENT_MASK_BUTTON_RELEASE |
Tiago Vignatti236b48d2012-07-16 12:09:19 -0400773 XCB_EVENT_MASK_POINTER_MOTION |
774 XCB_EVENT_MASK_ENTER_WINDOW |
775 XCB_EVENT_MASK_LEAVE_WINDOW |
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400776 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
Kristian Høgsberg44c20132012-05-30 10:09:21 -0400777 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400778 values[2] = wm->colormap;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400779
780 window->frame_id = xcb_generate_id(wm->conn);
781 xcb_create_window(wm->conn,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400782 32,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400783 window->frame_id,
784 wm->screen->root,
785 0, 0,
786 width, height,
787 0,
788 XCB_WINDOW_CLASS_INPUT_OUTPUT,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400789 wm->visual_id,
790 XCB_CW_BORDER_PIXEL |
791 XCB_CW_EVENT_MASK |
792 XCB_CW_COLORMAP, values);
793
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400794 xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y);
795
796 values[0] = 0;
797 xcb_configure_window(wm->conn, window->id,
798 XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
799
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400800 window->cairo_surface =
801 cairo_xcb_surface_create_with_xrender_format(wm->conn,
802 wm->screen,
803 window->frame_id,
Kristian Høgsbergf9187192013-05-02 13:43:24 -0400804 &wm->format_rgba,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400805 width, height);
806
807 hash_table_insert(wm->window_hash, window->frame_id, window);
808}
809
810static void
Kristian Høgsberg318ea372013-09-03 16:19:18 -0700811weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event)
812{
813 xcb_map_request_event_t *map_request =
814 (xcb_map_request_event_t *) event;
815 struct weston_wm_window *window;
816
817 if (our_resource(wm, map_request->window)) {
818 wm_log("XCB_MAP_REQUEST (window %d, ours)\n",
819 map_request->window);
820 return;
821 }
822
823 window = hash_table_lookup(wm->window_hash, map_request->window);
824
825 weston_wm_window_read_properties(window);
826
827 if (window->frame_id == XCB_WINDOW_NONE)
828 weston_wm_window_create_frame(window);
829
830 wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n",
831 window->id, window, window->frame_id);
832
833 weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE);
834 weston_wm_window_set_net_wm_state(window);
835
836 xcb_map_window(wm->conn, map_request->window);
837 xcb_map_window(wm->conn, window->frame_id);
838}
839
840static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400841weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event)
842{
843 xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event;
844
845 if (our_resource(wm, map_notify->window)) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400846 wm_log("XCB_MAP_NOTIFY (window %d, ours)\n",
847 map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400848 return;
849 }
850
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400851 wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400852}
853
854static void
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400855weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event)
856{
857 xcb_unmap_notify_event_t *unmap_notify =
858 (xcb_unmap_notify_event_t *) event;
859 struct weston_wm_window *window;
860
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400861 wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n",
862 unmap_notify->window,
863 unmap_notify->event,
864 our_resource(wm, unmap_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400865
866 if (our_resource(wm, unmap_notify->window))
867 return;
868
MoD31700122013-06-11 19:58:55 -0500869 if (unmap_notify->response_type & SEND_EVENT_MASK)
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400870 /* We just ignore the ICCCM 4.1.4 synthetic unmap notify
871 * as it may come in after we've destroyed the window. */
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400872 return;
Kristian Høgsbergf197e9f2012-05-30 11:46:29 -0400873
Kristian Høgsbergd64bdf42012-05-31 10:33:43 -0400874 window = hash_table_lookup(wm->window_hash, unmap_notify->window);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400875 if (wm->focus_window == window)
876 wm->focus_window = NULL;
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400877 if (window->surface)
878 wl_list_remove(&window->surface_destroy_listener.link);
879 window->surface = NULL;
Kristian Høgsbergab6d6672013-09-03 16:38:51 -0700880 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400881}
882
883static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400884weston_wm_window_draw_decoration(void *data)
885{
886 struct weston_wm_window *window = data;
887 struct weston_wm *wm = window->wm;
888 struct theme *t = wm->theme;
889 cairo_t *cr;
890 int x, y, width, height;
891 const char *title;
892 uint32_t flags = 0;
893
894 weston_wm_window_read_properties(window);
895
896 window->repaint_source = NULL;
897
898 weston_wm_window_get_frame_size(window, &width, &height);
899 weston_wm_window_get_child_position(window, &x, &y);
900
901 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
902 cr = cairo_create(window->cairo_surface);
903
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500904 if (window->fullscreen) {
905 /* nothing */
906 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400907 if (wm->focus_window == window)
908 flags |= THEME_FRAME_ACTIVE;
909
910 if (window->name)
911 title = window->name;
912 else
913 title = "untitled";
914
915 theme_render_frame(t, cr, width, height, title, flags);
916 } else {
917 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
918 cairo_set_source_rgba(cr, 0, 0, 0, 0);
919 cairo_paint(cr);
920
921 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
922 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
923 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
924 }
925
926 cairo_destroy(cr);
927
928 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700929 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500930 if(window->has_alpha) {
931 pixman_region32_init(&window->surface->pending.opaque);
932 } else {
933 /* We leave an extra pixel around the X window area to
934 * make sure we don't sample from the undefined alpha
935 * channel when filtering. */
936 pixman_region32_init_rect(&window->surface->pending.opaque,
937 x - 1, y - 1,
938 window->width + 2,
939 window->height + 2);
940 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200941 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500942 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400943
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500944 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500945 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500946 pixman_region32_init_rect(&window->surface->pending.input,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400947 t->margin, t->margin,
948 width - 2 * t->margin,
949 height - 2 * t->margin);
950 }
951}
952
953static void
954weston_wm_window_schedule_repaint(struct weston_wm_window *window)
955{
956 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300957 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400958
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400959 if (window->frame_id == XCB_WINDOW_NONE) {
960 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300961 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700962 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500963 if(window->has_alpha) {
964 pixman_region32_init(&window->surface->pending.opaque);
965 } else {
966 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
967 width, height);
968 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200969 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400970 }
971 return;
972 }
973
974 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400975 return;
976
977 window->repaint_source =
978 wl_event_loop_add_idle(wm->server->loop,
979 weston_wm_window_draw_decoration,
980 window);
981}
982
983static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400984weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
985{
986 xcb_property_notify_event_t *property_notify =
987 (xcb_property_notify_event_t *) event;
988 struct weston_wm_window *window;
989
990 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +0000991 if (!window)
992 return;
993
994 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400995
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400996 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400997 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400998 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400999 else
1000 read_and_dump_property(wm, property_notify->window,
1001 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001002
1003 if (property_notify->atom == wm->atom.net_wm_name ||
1004 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001005 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001006}
1007
1008static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001009weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001010 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001011{
1012 struct weston_wm_window *window;
1013 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001014 xcb_get_geometry_cookie_t geometry_cookie;
1015 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001016
Peter Huttererf3d62272013-08-08 11:57:05 +10001017 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001018 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001019 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001020 return;
1021 }
1022
MoD384a11a2013-06-22 11:04:21 -05001023 geometry_cookie = xcb_get_geometry(wm->conn, id);
1024
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001025 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1026 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1027
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001028 window->wm = wm;
1029 window->id = id;
1030 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001031 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001032 window->width = width;
1033 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001034 window->x = x;
1035 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001036
MoD384a11a2013-06-22 11:04:21 -05001037 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1038 /* technically we should use XRender and check the visual format's
1039 alpha_mask, but checking depth is simpler and works in all known cases */
1040 if(geometry_reply != NULL)
1041 window->has_alpha = geometry_reply->depth == 32;
1042 free(geometry_reply);
1043
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001044 hash_table_insert(wm->window_hash, id, window);
1045}
1046
1047static void
1048weston_wm_window_destroy(struct weston_wm_window *window)
1049{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001050 struct weston_wm *wm = window->wm;
1051
1052 if (window->repaint_source)
1053 wl_event_source_remove(window->repaint_source);
1054 if (window->cairo_surface)
1055 cairo_surface_destroy(window->cairo_surface);
1056
1057 if (window->frame_id) {
1058 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1059 xcb_destroy_window(wm->conn, window->frame_id);
1060 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1061 hash_table_remove(wm->window_hash, window->frame_id);
1062 window->frame_id = XCB_WINDOW_NONE;
1063 }
1064
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001065 hash_table_remove(window->wm->window_hash, window->id);
1066 free(window);
1067}
1068
1069static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001070weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1071{
1072 xcb_create_notify_event_t *create_notify =
1073 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001074
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001075 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1076 create_notify->window,
1077 create_notify->width, create_notify->height,
1078 create_notify->override_redirect ? ", override" : "",
1079 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001080
1081 if (our_resource(wm, create_notify->window))
1082 return;
1083
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001084 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001085 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001086 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001087 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001088}
1089
1090static void
1091weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1092{
1093 xcb_destroy_notify_event_t *destroy_notify =
1094 (xcb_destroy_notify_event_t *) event;
1095 struct weston_wm_window *window;
1096
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001097 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1098 destroy_notify->window,
1099 destroy_notify->event,
1100 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001101
1102 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001103 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001104
1105 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001106 weston_wm_window_destroy(window);
1107}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001108
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001109static void
1110weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1111{
1112 xcb_reparent_notify_event_t *reparent_notify =
1113 (xcb_reparent_notify_event_t *) event;
1114 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001115
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001116 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1117 reparent_notify->window,
1118 reparent_notify->parent,
1119 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001120
1121 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001122 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001123 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001124 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001125 } else if (!our_resource(wm, reparent_notify->parent)) {
1126 window = hash_table_lookup(wm->window_hash,
1127 reparent_notify->window);
1128 weston_wm_window_destroy(window);
1129 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001130}
1131
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001132struct weston_seat *
1133weston_wm_pick_seat(struct weston_wm *wm)
1134{
1135 return container_of(wm->server->compositor->seat_list.next,
1136 struct weston_seat, link);
1137}
1138
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001139static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001140weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1141 xcb_client_message_event_t *client_message)
1142{
1143 static const int map[] = {
1144 THEME_LOCATION_RESIZING_TOP_LEFT,
1145 THEME_LOCATION_RESIZING_TOP,
1146 THEME_LOCATION_RESIZING_TOP_RIGHT,
1147 THEME_LOCATION_RESIZING_RIGHT,
1148 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1149 THEME_LOCATION_RESIZING_BOTTOM,
1150 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1151 THEME_LOCATION_RESIZING_LEFT
1152 };
1153
1154 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001155 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001156 int detail;
1157 struct weston_shell_interface *shell_interface =
1158 &wm->server->compositor->shell_interface;
1159
Kristian Høgsberge3148752013-05-06 23:19:49 -04001160 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001161 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001162 return;
1163
1164 detail = client_message->data.data32[2];
1165 switch (detail) {
1166 case _NET_WM_MOVERESIZE_MOVE:
1167 shell_interface->move(window->shsurf, seat);
1168 break;
1169 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1170 case _NET_WM_MOVERESIZE_SIZE_TOP:
1171 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1172 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1173 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1174 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1175 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1176 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1177 shell_interface->resize(window->shsurf, seat, map[detail]);
1178 break;
1179 case _NET_WM_MOVERESIZE_CANCEL:
1180 break;
1181 }
1182}
1183
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001184#define _NET_WM_STATE_REMOVE 0
1185#define _NET_WM_STATE_ADD 1
1186#define _NET_WM_STATE_TOGGLE 2
1187
1188static int
1189update_state(int action, int *state)
1190{
1191 int new_state, changed;
1192
1193 switch (action) {
1194 case _NET_WM_STATE_REMOVE:
1195 new_state = 0;
1196 break;
1197 case _NET_WM_STATE_ADD:
1198 new_state = 1;
1199 break;
1200 case _NET_WM_STATE_TOGGLE:
1201 new_state = !*state;
1202 break;
1203 default:
1204 return 0;
1205 }
1206
1207 changed = (*state != new_state);
1208 *state = new_state;
1209
1210 return changed;
1211}
1212
1213static void
1214weston_wm_window_configure(void *data);
1215
1216static void
1217weston_wm_window_handle_state(struct weston_wm_window *window,
1218 xcb_client_message_event_t *client_message)
1219{
1220 struct weston_wm *wm = window->wm;
1221 struct weston_shell_interface *shell_interface =
1222 &wm->server->compositor->shell_interface;
1223 uint32_t action, property;
1224
1225 action = client_message->data.data32[0];
1226 property = client_message->data.data32[1];
1227
1228 if (property == wm->atom.net_wm_state_fullscreen &&
1229 update_state(action, &window->fullscreen)) {
1230 weston_wm_window_set_net_wm_state(window);
1231 if (window->fullscreen) {
1232 window->saved_width = window->width;
1233 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001234
1235 if (window->shsurf)
1236 shell_interface->set_fullscreen(window->shsurf,
1237 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1238 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001239 } else {
1240 shell_interface->set_toplevel(window->shsurf);
1241 window->width = window->saved_width;
1242 window->height = window->saved_height;
1243 weston_wm_window_configure(window);
1244 }
1245 }
1246}
1247
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001248static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001249weston_wm_handle_client_message(struct weston_wm *wm,
1250 xcb_generic_event_t *event)
1251{
1252 xcb_client_message_event_t *client_message =
1253 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001254 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001255
1256 window = hash_table_lookup(wm->window_hash, client_message->window);
1257
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001258 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1259 get_atom_name(wm->conn, client_message->type),
1260 client_message->data.data32[0],
1261 client_message->data.data32[1],
1262 client_message->data.data32[2],
1263 client_message->data.data32[3],
1264 client_message->data.data32[4],
1265 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001266
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001267 if (client_message->type == wm->atom.net_wm_moveresize)
1268 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001269 else if (client_message->type == wm->atom.net_wm_state)
1270 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001271}
1272
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001273enum cursor_type {
1274 XWM_CURSOR_TOP,
1275 XWM_CURSOR_BOTTOM,
1276 XWM_CURSOR_LEFT,
1277 XWM_CURSOR_RIGHT,
1278 XWM_CURSOR_TOP_LEFT,
1279 XWM_CURSOR_TOP_RIGHT,
1280 XWM_CURSOR_BOTTOM_LEFT,
1281 XWM_CURSOR_BOTTOM_RIGHT,
1282 XWM_CURSOR_LEFT_PTR,
1283};
1284
1285static const char *cursors[] = {
1286 "top_side",
1287 "bottom_side",
1288 "left_side",
1289 "right_side",
1290 "top_left_corner",
1291 "top_right_corner",
1292 "bottom_left_corner",
1293 "bottom_right_corner",
1294 "left_ptr"
1295};
1296
1297static void
1298weston_wm_create_cursors(struct weston_wm *wm)
1299{
1300 int i, count = ARRAY_LENGTH(cursors);
1301
1302 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1303 for (i = 0; i < count; i++) {
1304 wm->cursors[i] =
1305 xcb_cursor_library_load_cursor(wm, cursors[i]);
1306 }
1307
1308 wm->last_cursor = -1;
1309}
1310
1311static void
1312weston_wm_destroy_cursors(struct weston_wm *wm)
1313{
1314 uint8_t i;
1315
1316 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1317 xcb_free_cursor(wm->conn, wm->cursors[i]);
1318
1319 free(wm->cursors);
1320}
1321
1322static int
1323get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1324{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001325 int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001326
1327 switch (location) {
1328 case THEME_LOCATION_RESIZING_TOP:
1329 return XWM_CURSOR_TOP;
1330 case THEME_LOCATION_RESIZING_BOTTOM:
1331 return XWM_CURSOR_BOTTOM;
1332 case THEME_LOCATION_RESIZING_LEFT:
1333 return XWM_CURSOR_LEFT;
1334 case THEME_LOCATION_RESIZING_RIGHT:
1335 return XWM_CURSOR_RIGHT;
1336 case THEME_LOCATION_RESIZING_TOP_LEFT:
1337 return XWM_CURSOR_TOP_LEFT;
1338 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1339 return XWM_CURSOR_TOP_RIGHT;
1340 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1341 return XWM_CURSOR_BOTTOM_LEFT;
1342 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1343 return XWM_CURSOR_BOTTOM_RIGHT;
1344 case THEME_LOCATION_EXTERIOR:
1345 case THEME_LOCATION_TITLEBAR:
1346 default:
1347 return XWM_CURSOR_LEFT_PTR;
1348 }
1349}
1350
1351static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001352weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1353 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001354{
1355 uint32_t cursor_value_list;
1356
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001357 if (wm->last_cursor == cursor)
1358 return;
1359
1360 wm->last_cursor = cursor;
1361
1362 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001363 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001364 XCB_CW_CURSOR, &cursor_value_list);
1365 xcb_flush(wm->conn);
1366}
1367
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001368static void
1369weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1370{
1371 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1372 struct weston_shell_interface *shell_interface =
1373 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001374 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001375 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001376 enum theme_location location;
1377 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001378 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001379
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001380 wm_log("XCB_BUTTON_%s (detail %d)\n",
1381 button->response_type == XCB_BUTTON_PRESS ?
1382 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001383
1384 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001385 weston_wm_window_get_frame_size(window, &width, &height);
1386
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001387 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001388 button->detail == 1) {
1389 location = theme_get_location(t,
1390 button->event_x,
1391 button->event_y,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001392 width, height, 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001393
1394 switch (location) {
1395 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001396 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001397 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001398 case THEME_LOCATION_RESIZING_TOP:
1399 case THEME_LOCATION_RESIZING_BOTTOM:
1400 case THEME_LOCATION_RESIZING_LEFT:
1401 case THEME_LOCATION_RESIZING_RIGHT:
1402 case THEME_LOCATION_RESIZING_TOP_LEFT:
1403 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1404 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1405 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1406 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001407 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001408 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001409 default:
1410 break;
1411 }
1412 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001413}
1414
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001415static void
1416weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1417{
1418 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1419 struct weston_wm_window *window;
1420 int cursor, width, height;
1421
1422 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001423 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001424 return;
1425
1426 weston_wm_window_get_frame_size(window, &width, &height);
1427 cursor = get_cursor_for_location(wm->theme, width, height,
1428 motion->event_x, motion->event_y);
1429
Tiago Vignattic1903232012-07-16 12:15:37 -04001430 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001431}
1432
1433static void
1434weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1435{
1436 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1437 struct weston_wm_window *window;
1438 int cursor, width, height;
1439
1440 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001441 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001442 return;
1443
1444 weston_wm_window_get_frame_size(window, &width, &height);
1445 cursor = get_cursor_for_location(wm->theme, width, height,
1446 enter->event_x, enter->event_y);
1447
Tiago Vignattic1903232012-07-16 12:15:37 -04001448 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001449}
1450
1451static void
1452weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1453{
1454 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1455 struct weston_wm_window *window;
1456
1457 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001458 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001459 return;
1460
Tiago Vignattic1903232012-07-16 12:15:37 -04001461 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001462}
1463
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001464static int
1465weston_wm_handle_event(int fd, uint32_t mask, void *data)
1466{
1467 struct weston_wm *wm = data;
1468 xcb_generic_event_t *event;
1469 int count = 0;
1470
1471 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1472 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001473 free(event);
1474 count++;
1475 continue;
1476 }
1477
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001478 if (weston_wm_handle_dnd_event(wm, event)) {
1479 free(event);
1480 count++;
1481 continue;
1482 }
1483
MoD31700122013-06-11 19:58:55 -05001484 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001485 case XCB_BUTTON_PRESS:
1486 case XCB_BUTTON_RELEASE:
1487 weston_wm_handle_button(wm, event);
1488 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001489 case XCB_ENTER_NOTIFY:
1490 weston_wm_handle_enter(wm, event);
1491 break;
1492 case XCB_LEAVE_NOTIFY:
1493 weston_wm_handle_leave(wm, event);
1494 break;
1495 case XCB_MOTION_NOTIFY:
1496 weston_wm_handle_motion(wm, event);
1497 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001498 case XCB_CREATE_NOTIFY:
1499 weston_wm_handle_create_notify(wm, event);
1500 break;
1501 case XCB_MAP_REQUEST:
1502 weston_wm_handle_map_request(wm, event);
1503 break;
1504 case XCB_MAP_NOTIFY:
1505 weston_wm_handle_map_notify(wm, event);
1506 break;
1507 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001508 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001509 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001510 case XCB_REPARENT_NOTIFY:
1511 weston_wm_handle_reparent_notify(wm, event);
1512 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001513 case XCB_CONFIGURE_REQUEST:
1514 weston_wm_handle_configure_request(wm, event);
1515 break;
1516 case XCB_CONFIGURE_NOTIFY:
1517 weston_wm_handle_configure_notify(wm, event);
1518 break;
1519 case XCB_DESTROY_NOTIFY:
1520 weston_wm_handle_destroy_notify(wm, event);
1521 break;
1522 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001523 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001524 break;
1525 case XCB_PROPERTY_NOTIFY:
1526 weston_wm_handle_property_notify(wm, event);
1527 break;
1528 case XCB_CLIENT_MESSAGE:
1529 weston_wm_handle_client_message(wm, event);
1530 break;
1531 }
1532
1533 free(event);
1534 count++;
1535 }
1536
1537 xcb_flush(wm->conn);
1538
1539 return count;
1540}
1541
1542static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001543weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1544{
1545 xcb_depth_iterator_t d_iter;
1546 xcb_visualtype_iterator_t vt_iter;
1547 xcb_visualtype_t *visualtype;
1548
1549 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1550 visualtype = NULL;
1551 while (d_iter.rem > 0) {
1552 if (d_iter.data->depth == 32) {
1553 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1554 visualtype = vt_iter.data;
1555 break;
1556 }
1557
1558 xcb_depth_next(&d_iter);
1559 }
1560
1561 if (visualtype == NULL) {
1562 weston_log("no 32 bit visualtype\n");
1563 return;
1564 }
1565
1566 wm->visual_id = visualtype->visual_id;
1567 wm->colormap = xcb_generate_id(wm->conn);
1568 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1569 wm->colormap, wm->screen->root, wm->visual_id);
1570}
1571
1572static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001573weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001574{
1575
1576#define F(field) offsetof(struct weston_wm, field)
1577
1578 static const struct { const char *name; int offset; } atoms[] = {
1579 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001580 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001581 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1582 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001583 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001584 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001585 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001586 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001587 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001588 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001589 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1590 { "_NET_WM_STATE", F(atom.net_wm_state) },
1591 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1592 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1593 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1594 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1595
1596 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1597 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1598 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1599 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1600 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1601 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1602 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001603 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1604 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001605 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1606 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1607 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1608 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1609 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1610
1611 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1612 { "_NET_SUPPORTING_WM_CHECK",
1613 F(atom.net_supporting_wm_check) },
1614 { "_NET_SUPPORTED", F(atom.net_supported) },
1615 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1616 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001617 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001618 { "TARGETS", F(atom.targets) },
1619 { "UTF8_STRING", F(atom.utf8_string) },
1620 { "_WL_SELECTION", F(atom.wl_selection) },
1621 { "INCR", F(atom.incr) },
1622 { "TIMESTAMP", F(atom.timestamp) },
1623 { "MULTIPLE", F(atom.multiple) },
1624 { "UTF8_STRING" , F(atom.utf8_string) },
1625 { "COMPOUND_TEXT", F(atom.compound_text) },
1626 { "TEXT", F(atom.text) },
1627 { "STRING", F(atom.string) },
1628 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1629 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001630 { "XdndSelection", F(atom.xdnd_selection) },
1631 { "XdndAware", F(atom.xdnd_aware) },
1632 { "XdndEnter", F(atom.xdnd_enter) },
1633 { "XdndLeave", F(atom.xdnd_leave) },
1634 { "XdndDrop", F(atom.xdnd_drop) },
1635 { "XdndStatus", F(atom.xdnd_status) },
1636 { "XdndFinished", F(atom.xdnd_finished) },
1637 { "XdndTypeList", F(atom.xdnd_type_list) },
1638 { "XdndActionCopy", F(atom.xdnd_action_copy) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001639 };
1640#undef F
1641
1642 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1643 xcb_xfixes_query_version_reply_t *xfixes_reply;
1644 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1645 xcb_intern_atom_reply_t *reply;
1646 xcb_render_query_pict_formats_reply_t *formats_reply;
1647 xcb_render_query_pict_formats_cookie_t formats_cookie;
1648 xcb_render_pictforminfo_t *formats;
1649 uint32_t i;
1650
1651 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1652
1653 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1654
1655 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1656 cookies[i] = xcb_intern_atom (wm->conn, 0,
1657 strlen(atoms[i].name),
1658 atoms[i].name);
1659
1660 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1661 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1662 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1663 free(reply);
1664 }
1665
1666 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1667 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001668 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001669
1670 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1671 XCB_XFIXES_MAJOR_VERSION,
1672 XCB_XFIXES_MINOR_VERSION);
1673 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1674 xfixes_cookie, NULL);
1675
Martin Minarik6d118362012-06-07 18:01:59 +02001676 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001677 xfixes_reply->major_version, xfixes_reply->minor_version);
1678
1679 free(xfixes_reply);
1680
1681 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1682 formats_cookie, 0);
1683 if (formats_reply == NULL)
1684 return;
1685
1686 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001687 for (i = 0; i < formats_reply->num_formats; i++) {
1688 if (formats[i].direct.red_mask != 0xff &&
1689 formats[i].direct.red_shift != 16)
1690 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001691 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1692 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001693 wm->format_rgb = formats[i];
1694 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1695 formats[i].depth == 32 &&
1696 formats[i].direct.alpha_mask == 0xff &&
1697 formats[i].direct.alpha_shift == 24)
1698 wm->format_rgba = formats[i];
1699 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001700
1701 free(formats_reply);
1702}
1703
1704static void
1705weston_wm_create_wm_window(struct weston_wm *wm)
1706{
1707 static const char name[] = "Weston WM";
1708
1709 wm->wm_window = xcb_generate_id(wm->conn);
1710 xcb_create_window(wm->conn,
1711 XCB_COPY_FROM_PARENT,
1712 wm->wm_window,
1713 wm->screen->root,
1714 0, 0,
1715 10, 10,
1716 0,
1717 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1718 wm->screen->root_visual,
1719 0, NULL);
1720
1721 xcb_change_property(wm->conn,
1722 XCB_PROP_MODE_REPLACE,
1723 wm->wm_window,
1724 wm->atom.net_supporting_wm_check,
1725 XCB_ATOM_WINDOW,
1726 32, /* format */
1727 1, &wm->wm_window);
1728
1729 xcb_change_property(wm->conn,
1730 XCB_PROP_MODE_REPLACE,
1731 wm->wm_window,
1732 wm->atom.net_wm_name,
1733 wm->atom.utf8_string,
1734 8, /* format */
1735 strlen(name), name);
1736
1737 xcb_change_property(wm->conn,
1738 XCB_PROP_MODE_REPLACE,
1739 wm->screen->root,
1740 wm->atom.net_supporting_wm_check,
1741 XCB_ATOM_WINDOW,
1742 32, /* format */
1743 1, &wm->wm_window);
1744
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001745 /* Claim the WM_S0 selection even though we don't suport
1746 * the --replace functionality. */
1747 xcb_set_selection_owner(wm->conn,
1748 wm->wm_window,
1749 wm->atom.wm_s0,
1750 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001751
1752 xcb_set_selection_owner(wm->conn,
1753 wm->wm_window,
1754 wm->atom.net_wm_cm_s0,
1755 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001756}
1757
1758struct weston_wm *
1759weston_wm_create(struct weston_xserver *wxs)
1760{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001761 struct weston_wm *wm;
1762 struct wl_event_loop *loop;
1763 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001764 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001765 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001766 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001767
Peter Huttererf3d62272013-08-08 11:57:05 +10001768 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001769 if (wm == NULL)
1770 return NULL;
1771
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001772 wm->server = wxs;
1773 wm->window_hash = hash_table_create();
1774 if (wm->window_hash == NULL) {
1775 free(wm);
1776 return NULL;
1777 }
1778
1779 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001780 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001781 hash_table_destroy(wm->window_hash);
1782 free(wm);
1783 return NULL;
1784 }
1785
1786 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001787 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001788 close(sv[1]);
1789
1790 /* xcb_connect_to_fd takes ownership of the fd. */
1791 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1792 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001793 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001794 close(sv[0]);
1795 hash_table_destroy(wm->window_hash);
1796 free(wm);
1797 return NULL;
1798 }
1799
1800 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1801 wm->screen = s.data;
1802
1803 loop = wl_display_get_event_loop(wxs->wl_display);
1804 wm->source =
1805 wl_event_loop_add_fd(loop, sv[0],
1806 WL_EVENT_READABLE,
1807 weston_wm_handle_event, wm);
1808 wl_event_source_check(wm->source);
1809
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001810 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001811 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001812
1813 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001814 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1815 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1816 XCB_EVENT_MASK_PROPERTY_CHANGE;
1817 xcb_change_window_attributes(wm->conn, wm->screen->root,
1818 XCB_CW_EVENT_MASK, values);
1819 wm->theme = theme_create();
1820
1821 weston_wm_create_wm_window(wm);
1822
1823 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001824 supported[1] = wm->atom.net_wm_state;
1825 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001826 xcb_change_property(wm->conn,
1827 XCB_PROP_MODE_REPLACE,
1828 wm->screen->root,
1829 wm->atom.net_supported,
1830 XCB_ATOM_ATOM,
1831 32, /* format */
1832 ARRAY_LENGTH(supported), supported);
1833
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001834 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001835
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001836 weston_wm_dnd_init(wm);
1837
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001838 xcb_flush(wm->conn);
1839
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001840 wm->activate_listener.notify = weston_wm_window_activate;
1841 wl_signal_add(&wxs->compositor->activate_signal,
1842 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001843 wm->transform_listener.notify = weston_wm_window_transform;
1844 wl_signal_add(&wxs->compositor->transform_signal,
1845 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001846 wm->kill_listener.notify = weston_wm_kill_client;
1847 wl_signal_add(&wxs->compositor->kill_signal,
1848 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001849
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001850 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001851 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001852
Martin Minarik6d118362012-06-07 18:01:59 +02001853 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001854
1855 return wm;
1856}
1857
1858void
1859weston_wm_destroy(struct weston_wm *wm)
1860{
1861 /* FIXME: Free windows in hash. */
1862 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001863 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001864 xcb_disconnect(wm->conn);
1865 wl_event_source_remove(wm->source);
1866 wl_list_remove(&wm->selection_listener.link);
1867 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001868 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01001869 wl_list_remove(&wm->transform_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001870
1871 free(wm);
1872}
1873
1874static void
1875surface_destroy(struct wl_listener *listener, void *data)
1876{
1877 struct weston_wm_window *window =
1878 container_of(listener,
1879 struct weston_wm_window, surface_destroy_listener);
1880
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001881 wm_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07001882
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001883 /* This should have been freed by the shell.
1884 Don't try to use it later. */
1885 window->shsurf = NULL;
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07001886 window->surface = NULL;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001887}
1888
1889static struct weston_wm_window *
1890get_wm_window(struct weston_surface *surface)
1891{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001892 struct wl_listener *listener;
1893
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001894 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001895 if (listener)
1896 return container_of(listener, struct weston_wm_window,
1897 surface_destroy_listener);
1898
1899 return NULL;
1900}
1901
1902static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001903weston_wm_window_configure(void *data)
1904{
1905 struct weston_wm_window *window = data;
1906 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001907 uint32_t values[4];
1908 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001909
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001910 weston_wm_window_get_child_position(window, &x, &y);
1911 values[0] = x;
1912 values[1] = y;
1913 values[2] = window->width;
1914 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001915 xcb_configure_window(wm->conn,
1916 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001917 XCB_CONFIG_WINDOW_X |
1918 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001919 XCB_CONFIG_WINDOW_WIDTH |
1920 XCB_CONFIG_WINDOW_HEIGHT,
1921 values);
1922
1923 weston_wm_window_get_frame_size(window, &width, &height);
1924 values[0] = width;
1925 values[1] = height;
1926 xcb_configure_window(wm->conn,
1927 window->frame_id,
1928 XCB_CONFIG_WINDOW_WIDTH |
1929 XCB_CONFIG_WINDOW_HEIGHT,
1930 values);
1931
1932 window->configure_source = NULL;
1933
1934 weston_wm_window_schedule_repaint(window);
1935}
1936
1937static void
1938send_configure(struct weston_surface *surface,
1939 uint32_t edges, int32_t width, int32_t height)
1940{
1941 struct weston_wm_window *window = get_wm_window(surface);
1942 struct weston_wm *wm = window->wm;
1943 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001944 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001945
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001946 if (window->fullscreen) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001947 hborder = 0;
1948 vborder = 0;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001949 } else if (window->decorate) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001950 hborder = 2 * (t->margin + t->width);
1951 vborder = 2 * t->margin + t->titlebar_height + t->width;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001952 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001953 hborder = 2 * t->margin;
1954 vborder = 2 * t->margin;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001955 }
1956
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04001957 if (width > hborder)
1958 window->width = width - hborder;
1959 else
1960 window->width = 1;
1961
1962 if (height > vborder)
1963 window->height = height - vborder;
1964 else
1965 window->height = 1;
1966
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001967 if (window->configure_source)
1968 return;
1969
1970 window->configure_source =
1971 wl_event_loop_add_idle(wm->server->loop,
1972 weston_wm_window_configure, window);
1973}
1974
1975static const struct weston_shell_client shell_client = {
1976 send_configure
1977};
1978
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07001979static int
1980legacy_fullscreen(struct weston_wm *wm,
1981 struct weston_wm_window *window,
1982 struct weston_output **output_ret)
1983{
1984 struct weston_compositor *compositor = wm->server->compositor;
1985 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001986 uint32_t minmax = PMinSize | PMaxSize;
1987 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07001988
1989 /* Heuristics for detecting legacy fullscreen windows... */
1990
1991 wl_list_for_each(output, &compositor->output_list, link) {
1992 if (output->x == window->x &&
1993 output->y == window->y &&
1994 output->width == window->width &&
1995 output->height == window->height &&
1996 window->override_redirect) {
1997 *output_ret = output;
1998 return 1;
1999 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002000
2001 matching_size = 0;
2002 if ((window->size_hints.flags & (USSize |PSize)) &&
2003 window->size_hints.width == output->width &&
2004 window->size_hints.height == output->height)
2005 matching_size = 1;
2006 if ((window->size_hints.flags & minmax) == minmax &&
2007 window->size_hints.min_width == output->width &&
2008 window->size_hints.min_height == output->height &&
2009 window->size_hints.max_width == output->width &&
2010 window->size_hints.max_height == output->height)
2011 matching_size = 1;
2012
2013 if (matching_size && !window->decorate &&
2014 (window->size_hints.flags & (USPosition | PPosition)) &&
2015 window->size_hints.x == output->x &&
2016 window->size_hints.y == output->y) {
2017 *output_ret = output;
2018 return 1;
2019 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002020 }
2021
2022 return 0;
2023}
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002024static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002025xserver_map_shell_surface(struct weston_wm *wm,
2026 struct weston_wm_window *window)
2027{
2028 struct weston_shell_interface *shell_interface =
2029 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002030 struct weston_output *output;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002031
2032 if (!shell_interface->create_shell_surface)
2033 return;
2034
2035 window->shsurf =
2036 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002037 window->surface,
2038 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002039
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002040 if (window->name)
2041 shell_interface->set_title(window->shsurf, window->name);
2042
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002043 if (window->fullscreen) {
2044 window->saved_width = window->width;
2045 window->saved_height = window->height;
2046 shell_interface->set_fullscreen(window->shsurf,
2047 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2048 0, NULL);
2049 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002050 } else if (legacy_fullscreen(wm, window, &output)) {
2051 window->fullscreen = 1;
2052 shell_interface->set_fullscreen(window->shsurf,
2053 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2054 0, output);
Giulio Camuffoca43f092013-09-11 17:49:13 +02002055 } else if (!window->override_redirect && !window->transient_for) {
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002056 shell_interface->set_toplevel(window->shsurf);
2057 return;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002058 } else {
2059 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002060 window->x,
2061 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002062 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002063 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002064}
2065
2066static void
2067xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
2068 struct wl_resource *surface_resource, uint32_t id)
2069{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002070 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002071 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002072 struct weston_surface *surface =
2073 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002074 struct weston_wm_window *window;
2075
2076 if (client != wxs->client)
2077 return;
2078
2079 window = hash_table_lookup(wm->window_hash, id);
2080 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002081 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002082 return;
2083 }
2084
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002085 wm_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002086
2087 weston_wm_window_read_properties(window);
2088
2089 window->surface = (struct weston_surface *) surface;
2090 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002091 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002092 &window->surface_destroy_listener);
2093
2094 weston_wm_window_schedule_repaint(window);
2095 xserver_map_shell_surface(wm, window);
2096}
2097
2098const struct xserver_interface xserver_implementation = {
2099 xserver_set_window_id
2100};