blob: 9667c6f61b78a8d43ca6b3a97412385daf6de264 [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;
Giulio Camuffo85739ea2013-09-17 16:43:45 +0200880 window->shsurf = NULL;
Kristian Høgsbergab6d6672013-09-03 16:38:51 -0700881 xcb_unmap_window(wm->conn, window->frame_id);
Kristian Høgsberg194ea542012-05-30 10:05:41 -0400882}
883
884static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400885weston_wm_window_draw_decoration(void *data)
886{
887 struct weston_wm_window *window = data;
888 struct weston_wm *wm = window->wm;
889 struct theme *t = wm->theme;
890 cairo_t *cr;
891 int x, y, width, height;
892 const char *title;
893 uint32_t flags = 0;
894
895 weston_wm_window_read_properties(window);
896
897 window->repaint_source = NULL;
898
899 weston_wm_window_get_frame_size(window, &width, &height);
900 weston_wm_window_get_child_position(window, &x, &y);
901
902 cairo_xcb_surface_set_size(window->cairo_surface, width, height);
903 cr = cairo_create(window->cairo_surface);
904
Kristian Høgsbergb810eb52013-02-12 20:07:05 -0500905 if (window->fullscreen) {
906 /* nothing */
907 } else if (window->decorate) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400908 if (wm->focus_window == window)
909 flags |= THEME_FRAME_ACTIVE;
910
911 if (window->name)
912 title = window->name;
913 else
914 title = "untitled";
915
916 theme_render_frame(t, cr, width, height, title, flags);
917 } else {
918 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
919 cairo_set_source_rgba(cr, 0, 0, 0, 0);
920 cairo_paint(cr);
921
922 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
923 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
924 tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64);
925 }
926
927 cairo_destroy(cr);
928
929 if (window->surface) {
Scott Moreau76d8fc82012-11-22 15:35:13 -0700930 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500931 if(window->has_alpha) {
932 pixman_region32_init(&window->surface->pending.opaque);
933 } else {
934 /* We leave an extra pixel around the X window area to
935 * make sure we don't sample from the undefined alpha
936 * channel when filtering. */
937 pixman_region32_init_rect(&window->surface->pending.opaque,
938 x - 1, y - 1,
939 window->width + 2,
940 window->height + 2);
941 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200942 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500943 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400944
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500945 if (window->surface && !window->fullscreen) {
Kristian Høgsberg81585e92013-02-14 22:01:58 -0500946 pixman_region32_fini(&window->surface->pending.input);
Kristian Høgsbergd8b617d2013-02-14 21:56:32 -0500947 pixman_region32_init_rect(&window->surface->pending.input,
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400948 t->margin, t->margin,
949 width - 2 * t->margin,
950 height - 2 * t->margin);
951 }
952}
953
954static void
955weston_wm_window_schedule_repaint(struct weston_wm_window *window)
956{
957 struct weston_wm *wm = window->wm;
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300958 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400959
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400960 if (window->frame_id == XCB_WINDOW_NONE) {
961 if (window->surface != NULL) {
Pekka Paalanen4f9c07b2012-09-03 16:48:41 +0300962 weston_wm_window_get_frame_size(window, &width, &height);
Scott Moreau76d8fc82012-11-22 15:35:13 -0700963 pixman_region32_fini(&window->surface->pending.opaque);
MoD384a11a2013-06-22 11:04:21 -0500964 if(window->has_alpha) {
965 pixman_region32_init(&window->surface->pending.opaque);
966 } else {
967 pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0,
968 width, height);
969 }
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200970 weston_surface_geometry_dirty(window->surface);
Kristian Høgsbergc4063f32012-07-22 15:32:45 -0400971 }
972 return;
973 }
974
975 if (window->repaint_source)
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400976 return;
977
978 window->repaint_source =
979 wl_event_loop_add_idle(wm->server->loop,
980 weston_wm_window_draw_decoration,
981 window);
982}
983
984static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400985weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event)
986{
987 xcb_property_notify_event_t *property_notify =
988 (xcb_property_notify_event_t *) event;
989 struct weston_wm_window *window;
990
991 window = hash_table_lookup(wm->window_hash, property_notify->window);
Rob Bradfordaa521bd2013-01-10 19:48:57 +0000992 if (!window)
993 return;
994
995 window->properties_dirty = 1;
Kristian Høgsberg380deee2012-05-21 17:12:41 -0400996
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400997 wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window);
Kristian Høgsberge244cb02012-05-30 11:34:35 -0400998 if (property_notify->state == XCB_PROPERTY_DELETE)
Kristian Høgsberg082d58c2013-06-18 01:00:27 -0400999 wm_log("deleted\n");
Kristian Høgsberge244cb02012-05-30 11:34:35 -04001000 else
1001 read_and_dump_property(wm, property_notify->window,
1002 property_notify->atom);
Kristian Høgsberg0273b572012-05-30 09:58:02 -04001003
1004 if (property_notify->atom == wm->atom.net_wm_name ||
1005 property_notify->atom == XCB_ATOM_WM_NAME)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001006 weston_wm_window_schedule_repaint(window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001007}
1008
1009static void
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001010weston_wm_window_create(struct weston_wm *wm,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001011 xcb_window_t id, int width, int height, int x, int y, int override)
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001012{
1013 struct weston_wm_window *window;
1014 uint32_t values[1];
MoD384a11a2013-06-22 11:04:21 -05001015 xcb_get_geometry_cookie_t geometry_cookie;
1016 xcb_get_geometry_reply_t *geometry_reply;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001017
Peter Huttererf3d62272013-08-08 11:57:05 +10001018 window = zalloc(sizeof *window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001019 if (window == NULL) {
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001020 wm_log("failed to allocate window\n");
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001021 return;
1022 }
1023
MoD384a11a2013-06-22 11:04:21 -05001024 geometry_cookie = xcb_get_geometry(wm->conn, id);
1025
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001026 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
1027 xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values);
1028
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001029 window->wm = wm;
1030 window->id = id;
1031 window->properties_dirty = 1;
Tiago Vignatti771241e2012-06-04 20:01:45 +03001032 window->override_redirect = override;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001033 window->width = width;
1034 window->height = height;
Giulio Camuffoca43f092013-09-11 17:49:13 +02001035 window->x = x;
1036 window->y = y;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001037
MoD384a11a2013-06-22 11:04:21 -05001038 geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL);
1039 /* technically we should use XRender and check the visual format's
1040 alpha_mask, but checking depth is simpler and works in all known cases */
1041 if(geometry_reply != NULL)
1042 window->has_alpha = geometry_reply->depth == 32;
1043 free(geometry_reply);
1044
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001045 hash_table_insert(wm->window_hash, id, window);
1046}
1047
1048static void
1049weston_wm_window_destroy(struct weston_wm_window *window)
1050{
Kristian Høgsbergab6d6672013-09-03 16:38:51 -07001051 struct weston_wm *wm = window->wm;
1052
1053 if (window->repaint_source)
1054 wl_event_source_remove(window->repaint_source);
1055 if (window->cairo_surface)
1056 cairo_surface_destroy(window->cairo_surface);
1057
1058 if (window->frame_id) {
1059 xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0);
1060 xcb_destroy_window(wm->conn, window->frame_id);
1061 weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE);
1062 hash_table_remove(wm->window_hash, window->frame_id);
1063 window->frame_id = XCB_WINDOW_NONE;
1064 }
1065
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001066 hash_table_remove(window->wm->window_hash, window->id);
1067 free(window);
1068}
1069
1070static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001071weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1072{
1073 xcb_create_notify_event_t *create_notify =
1074 (xcb_create_notify_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001075
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001076 wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n",
1077 create_notify->window,
1078 create_notify->width, create_notify->height,
1079 create_notify->override_redirect ? ", override" : "",
1080 our_resource(wm, create_notify->window) ? ", ours" : "");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001081
1082 if (our_resource(wm, create_notify->window))
1083 return;
1084
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001085 weston_wm_window_create(wm, create_notify->window,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001086 create_notify->width, create_notify->height,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001087 create_notify->x, create_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001088 create_notify->override_redirect);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001089}
1090
1091static void
1092weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1093{
1094 xcb_destroy_notify_event_t *destroy_notify =
1095 (xcb_destroy_notify_event_t *) event;
1096 struct weston_wm_window *window;
1097
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001098 wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n",
1099 destroy_notify->window,
1100 destroy_notify->event,
1101 our_resource(wm, destroy_notify->window) ? ", ours" : "");
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001102
1103 if (our_resource(wm, destroy_notify->window))
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001104 return;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001105
1106 window = hash_table_lookup(wm->window_hash, destroy_notify->window);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001107 weston_wm_window_destroy(window);
1108}
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001109
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001110static void
1111weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event)
1112{
1113 xcb_reparent_notify_event_t *reparent_notify =
1114 (xcb_reparent_notify_event_t *) event;
1115 struct weston_wm_window *window;
Kristian Høgsbergc9571fb2012-05-29 15:35:29 -04001116
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001117 wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n",
1118 reparent_notify->window,
1119 reparent_notify->parent,
1120 reparent_notify->event);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001121
1122 if (reparent_notify->parent == wm->screen->root) {
Tiago Vignatti771241e2012-06-04 20:01:45 +03001123 weston_wm_window_create(wm, reparent_notify->window, 10, 10,
Giulio Camuffoca43f092013-09-11 17:49:13 +02001124 reparent_notify->x, reparent_notify->y,
Tiago Vignatti771241e2012-06-04 20:01:45 +03001125 reparent_notify->override_redirect);
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001126 } else if (!our_resource(wm, reparent_notify->parent)) {
1127 window = hash_table_lookup(wm->window_hash,
1128 reparent_notify->window);
1129 weston_wm_window_destroy(window);
1130 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001131}
1132
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001133struct weston_seat *
1134weston_wm_pick_seat(struct weston_wm *wm)
1135{
1136 return container_of(wm->server->compositor->seat_list.next,
1137 struct weston_seat, link);
1138}
1139
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001140static void
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001141weston_wm_window_handle_moveresize(struct weston_wm_window *window,
1142 xcb_client_message_event_t *client_message)
1143{
1144 static const int map[] = {
1145 THEME_LOCATION_RESIZING_TOP_LEFT,
1146 THEME_LOCATION_RESIZING_TOP,
1147 THEME_LOCATION_RESIZING_TOP_RIGHT,
1148 THEME_LOCATION_RESIZING_RIGHT,
1149 THEME_LOCATION_RESIZING_BOTTOM_RIGHT,
1150 THEME_LOCATION_RESIZING_BOTTOM,
1151 THEME_LOCATION_RESIZING_BOTTOM_LEFT,
1152 THEME_LOCATION_RESIZING_LEFT
1153 };
1154
1155 struct weston_wm *wm = window->wm;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001156 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001157 int detail;
1158 struct weston_shell_interface *shell_interface =
1159 &wm->server->compositor->shell_interface;
1160
Kristian Høgsberge3148752013-05-06 23:19:49 -04001161 if (seat->pointer->button_count != 1 ||
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001162 seat->pointer->focus != window->surface)
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001163 return;
1164
1165 detail = client_message->data.data32[2];
1166 switch (detail) {
1167 case _NET_WM_MOVERESIZE_MOVE:
1168 shell_interface->move(window->shsurf, seat);
1169 break;
1170 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
1171 case _NET_WM_MOVERESIZE_SIZE_TOP:
1172 case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
1173 case _NET_WM_MOVERESIZE_SIZE_RIGHT:
1174 case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
1175 case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
1176 case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
1177 case _NET_WM_MOVERESIZE_SIZE_LEFT:
1178 shell_interface->resize(window->shsurf, seat, map[detail]);
1179 break;
1180 case _NET_WM_MOVERESIZE_CANCEL:
1181 break;
1182 }
1183}
1184
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001185#define _NET_WM_STATE_REMOVE 0
1186#define _NET_WM_STATE_ADD 1
1187#define _NET_WM_STATE_TOGGLE 2
1188
1189static int
1190update_state(int action, int *state)
1191{
1192 int new_state, changed;
1193
1194 switch (action) {
1195 case _NET_WM_STATE_REMOVE:
1196 new_state = 0;
1197 break;
1198 case _NET_WM_STATE_ADD:
1199 new_state = 1;
1200 break;
1201 case _NET_WM_STATE_TOGGLE:
1202 new_state = !*state;
1203 break;
1204 default:
1205 return 0;
1206 }
1207
1208 changed = (*state != new_state);
1209 *state = new_state;
1210
1211 return changed;
1212}
1213
1214static void
1215weston_wm_window_configure(void *data);
1216
1217static void
1218weston_wm_window_handle_state(struct weston_wm_window *window,
1219 xcb_client_message_event_t *client_message)
1220{
1221 struct weston_wm *wm = window->wm;
1222 struct weston_shell_interface *shell_interface =
1223 &wm->server->compositor->shell_interface;
1224 uint32_t action, property;
1225
1226 action = client_message->data.data32[0];
1227 property = client_message->data.data32[1];
1228
1229 if (property == wm->atom.net_wm_state_fullscreen &&
1230 update_state(action, &window->fullscreen)) {
1231 weston_wm_window_set_net_wm_state(window);
1232 if (window->fullscreen) {
1233 window->saved_width = window->width;
1234 window->saved_height = window->height;
Kristian Høgsberg762b1662013-02-21 20:18:37 -05001235
1236 if (window->shsurf)
1237 shell_interface->set_fullscreen(window->shsurf,
1238 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
1239 0, NULL);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001240 } else {
1241 shell_interface->set_toplevel(window->shsurf);
1242 window->width = window->saved_width;
1243 window->height = window->saved_height;
1244 weston_wm_window_configure(window);
1245 }
1246 }
1247}
1248
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001249static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001250weston_wm_handle_client_message(struct weston_wm *wm,
1251 xcb_generic_event_t *event)
1252{
1253 xcb_client_message_event_t *client_message =
1254 (xcb_client_message_event_t *) event;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001255 struct weston_wm_window *window;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001256
1257 window = hash_table_lookup(wm->window_hash, client_message->window);
1258
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001259 wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n",
1260 get_atom_name(wm->conn, client_message->type),
1261 client_message->data.data32[0],
1262 client_message->data.data32[1],
1263 client_message->data.data32[2],
1264 client_message->data.data32[3],
1265 client_message->data.data32[4],
1266 client_message->window);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001267
Kristian Høgsberge68fd102012-05-22 17:09:40 -04001268 if (client_message->type == wm->atom.net_wm_moveresize)
1269 weston_wm_window_handle_moveresize(window, client_message);
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001270 else if (client_message->type == wm->atom.net_wm_state)
1271 weston_wm_window_handle_state(window, client_message);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001272}
1273
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001274enum cursor_type {
1275 XWM_CURSOR_TOP,
1276 XWM_CURSOR_BOTTOM,
1277 XWM_CURSOR_LEFT,
1278 XWM_CURSOR_RIGHT,
1279 XWM_CURSOR_TOP_LEFT,
1280 XWM_CURSOR_TOP_RIGHT,
1281 XWM_CURSOR_BOTTOM_LEFT,
1282 XWM_CURSOR_BOTTOM_RIGHT,
1283 XWM_CURSOR_LEFT_PTR,
1284};
1285
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001286/*
1287 * The following correspondences between file names and cursors was copied
1288 * from: https://bugs.kde.org/attachment.cgi?id=67313
1289 */
1290
1291static const char *bottom_left_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001292 "bottom_left_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001293 "sw-resize",
1294 "size_bdiag"
1295};
1296
1297static const char *bottom_right_corners[] = {
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001298 "bottom_right_corner",
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001299 "se-resize",
1300 "size_fdiag"
1301};
1302
1303static const char *bottom_sides[] = {
1304 "bottom_side",
1305 "s-resize",
1306 "size_ver"
1307};
1308
1309static const char *left_ptrs[] = {
1310 "left_ptr",
1311 "default",
1312 "top_left_arrow",
1313 "left-arrow"
1314};
1315
1316static const char *left_sides[] = {
1317 "left_side",
1318 "w-resize",
1319 "size_hor"
1320};
1321
1322static const char *right_sides[] = {
1323 "right_side",
1324 "e-resize",
1325 "size_hor"
1326};
1327
1328static const char *top_left_corners[] = {
1329 "top_left_corner",
1330 "nw-resize",
1331 "size_fdiag"
1332};
1333
1334static const char *top_right_corners[] = {
1335 "top_right_corner",
1336 "ne-resize",
1337 "size_bdiag"
1338};
1339
1340static const char *top_sides[] = {
1341 "top_side",
1342 "n-resize",
1343 "size_ver"
1344};
1345
1346struct cursor_alternatives {
1347 const char **names;
1348 size_t count;
1349};
1350
1351static const struct cursor_alternatives cursors[] = {
1352 {top_sides, ARRAY_LENGTH(top_sides)},
1353 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1354 {left_sides, ARRAY_LENGTH(left_sides)},
1355 {right_sides, ARRAY_LENGTH(right_sides)},
1356 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1357 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1358 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1359 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1360 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001361};
1362
1363static void
1364weston_wm_create_cursors(struct weston_wm *wm)
1365{
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001366 const char *name;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001367 int i, count = ARRAY_LENGTH(cursors);
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001368 size_t j;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001369
1370 wm->cursors = malloc(count * sizeof(xcb_cursor_t));
1371 for (i = 0; i < count; i++) {
Giulio Camuffoa2df51c2013-09-18 15:20:04 +02001372 for (j = 0; j < cursors[i].count; j++) {
1373 name = cursors[i].names[j];
1374 wm->cursors[i] =
1375 xcb_cursor_library_load_cursor(wm, name);
1376 if (wm->cursors[i] != (xcb_cursor_t)-1)
1377 break;
1378 }
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001379 }
1380
1381 wm->last_cursor = -1;
1382}
1383
1384static void
1385weston_wm_destroy_cursors(struct weston_wm *wm)
1386{
1387 uint8_t i;
1388
1389 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
1390 xcb_free_cursor(wm->conn, wm->cursors[i]);
1391
1392 free(wm->cursors);
1393}
1394
1395static int
1396get_cursor_for_location(struct theme *t, int width, int height, int x, int y)
1397{
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001398 int location = theme_get_location(t, x, y, width, height, 0);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001399
1400 switch (location) {
1401 case THEME_LOCATION_RESIZING_TOP:
1402 return XWM_CURSOR_TOP;
1403 case THEME_LOCATION_RESIZING_BOTTOM:
1404 return XWM_CURSOR_BOTTOM;
1405 case THEME_LOCATION_RESIZING_LEFT:
1406 return XWM_CURSOR_LEFT;
1407 case THEME_LOCATION_RESIZING_RIGHT:
1408 return XWM_CURSOR_RIGHT;
1409 case THEME_LOCATION_RESIZING_TOP_LEFT:
1410 return XWM_CURSOR_TOP_LEFT;
1411 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1412 return XWM_CURSOR_TOP_RIGHT;
1413 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1414 return XWM_CURSOR_BOTTOM_LEFT;
1415 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1416 return XWM_CURSOR_BOTTOM_RIGHT;
1417 case THEME_LOCATION_EXTERIOR:
1418 case THEME_LOCATION_TITLEBAR:
1419 default:
1420 return XWM_CURSOR_LEFT_PTR;
1421 }
1422}
1423
1424static void
Tiago Vignattic1903232012-07-16 12:15:37 -04001425weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id,
1426 int cursor)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001427{
1428 uint32_t cursor_value_list;
1429
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001430 if (wm->last_cursor == cursor)
1431 return;
1432
1433 wm->last_cursor = cursor;
1434
1435 cursor_value_list = wm->cursors[cursor];
Tiago Vignattic1903232012-07-16 12:15:37 -04001436 xcb_change_window_attributes (wm->conn, window_id,
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001437 XCB_CW_CURSOR, &cursor_value_list);
1438 xcb_flush(wm->conn);
1439}
1440
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001441static void
1442weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event)
1443{
1444 xcb_button_press_event_t *button = (xcb_button_press_event_t *) event;
1445 struct weston_shell_interface *shell_interface =
1446 &wm->server->compositor->shell_interface;
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001447 struct weston_seat *seat = weston_wm_pick_seat(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001448 struct weston_wm_window *window;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001449 enum theme_location location;
1450 struct theme *t = wm->theme;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001451 int width, height;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001452
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001453 wm_log("XCB_BUTTON_%s (detail %d)\n",
1454 button->response_type == XCB_BUTTON_PRESS ?
1455 "PRESS" : "RELEASE", button->detail);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001456
1457 window = hash_table_lookup(wm->window_hash, button->event);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001458 weston_wm_window_get_frame_size(window, &width, &height);
1459
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001460 if (button->response_type == XCB_BUTTON_PRESS &&
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001461 button->detail == 1) {
1462 location = theme_get_location(t,
1463 button->event_x,
1464 button->event_y,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06001465 width, height, 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001466
1467 switch (location) {
1468 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001469 shell_interface->move(window->shsurf, seat);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001470 break;
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001471 case THEME_LOCATION_RESIZING_TOP:
1472 case THEME_LOCATION_RESIZING_BOTTOM:
1473 case THEME_LOCATION_RESIZING_LEFT:
1474 case THEME_LOCATION_RESIZING_RIGHT:
1475 case THEME_LOCATION_RESIZING_TOP_LEFT:
1476 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1477 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1478 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1479 shell_interface->resize(window->shsurf,
Kristian Høgsberg5ba31892012-08-10 10:06:59 -04001480 seat, location);
Kristian Høgsbergc1693f22012-05-22 16:56:23 -04001481 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04001482 default:
1483 break;
1484 }
1485 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001486}
1487
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001488static void
1489weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event)
1490{
1491 xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
1492 struct weston_wm_window *window;
1493 int cursor, width, height;
1494
1495 window = hash_table_lookup(wm->window_hash, motion->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001496 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001497 return;
1498
1499 weston_wm_window_get_frame_size(window, &width, &height);
1500 cursor = get_cursor_for_location(wm->theme, width, height,
1501 motion->event_x, motion->event_y);
1502
Tiago Vignattic1903232012-07-16 12:15:37 -04001503 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001504}
1505
1506static void
1507weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event)
1508{
1509 xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event;
1510 struct weston_wm_window *window;
1511 int cursor, width, height;
1512
1513 window = hash_table_lookup(wm->window_hash, enter->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001514 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001515 return;
1516
1517 weston_wm_window_get_frame_size(window, &width, &height);
1518 cursor = get_cursor_for_location(wm->theme, width, height,
1519 enter->event_x, enter->event_y);
1520
Tiago Vignattic1903232012-07-16 12:15:37 -04001521 weston_wm_window_set_cursor(wm, window->frame_id, cursor);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001522}
1523
1524static void
1525weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event)
1526{
1527 xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event;
1528 struct weston_wm_window *window;
1529
1530 window = hash_table_lookup(wm->window_hash, leave->event);
Tiago Vignatti9134b772012-07-20 19:41:12 +03001531 if (!window || !window->decorate)
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001532 return;
1533
Tiago Vignattic1903232012-07-16 12:15:37 -04001534 weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001535}
1536
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001537static int
1538weston_wm_handle_event(int fd, uint32_t mask, void *data)
1539{
1540 struct weston_wm *wm = data;
1541 xcb_generic_event_t *event;
1542 int count = 0;
1543
1544 while (event = xcb_poll_for_event(wm->conn), event != NULL) {
1545 if (weston_wm_handle_selection_event(wm, event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001546 free(event);
1547 count++;
1548 continue;
1549 }
1550
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001551 if (weston_wm_handle_dnd_event(wm, event)) {
1552 free(event);
1553 count++;
1554 continue;
1555 }
1556
MoD31700122013-06-11 19:58:55 -05001557 switch (EVENT_TYPE(event)) {
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001558 case XCB_BUTTON_PRESS:
1559 case XCB_BUTTON_RELEASE:
1560 weston_wm_handle_button(wm, event);
1561 break;
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001562 case XCB_ENTER_NOTIFY:
1563 weston_wm_handle_enter(wm, event);
1564 break;
1565 case XCB_LEAVE_NOTIFY:
1566 weston_wm_handle_leave(wm, event);
1567 break;
1568 case XCB_MOTION_NOTIFY:
1569 weston_wm_handle_motion(wm, event);
1570 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001571 case XCB_CREATE_NOTIFY:
1572 weston_wm_handle_create_notify(wm, event);
1573 break;
1574 case XCB_MAP_REQUEST:
1575 weston_wm_handle_map_request(wm, event);
1576 break;
1577 case XCB_MAP_NOTIFY:
1578 weston_wm_handle_map_notify(wm, event);
1579 break;
1580 case XCB_UNMAP_NOTIFY:
Kristian Høgsberg194ea542012-05-30 10:05:41 -04001581 weston_wm_handle_unmap_notify(wm, event);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001582 break;
Kristian Høgsberg029539b2012-05-30 11:28:24 -04001583 case XCB_REPARENT_NOTIFY:
1584 weston_wm_handle_reparent_notify(wm, event);
1585 break;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001586 case XCB_CONFIGURE_REQUEST:
1587 weston_wm_handle_configure_request(wm, event);
1588 break;
1589 case XCB_CONFIGURE_NOTIFY:
1590 weston_wm_handle_configure_notify(wm, event);
1591 break;
1592 case XCB_DESTROY_NOTIFY:
1593 weston_wm_handle_destroy_notify(wm, event);
1594 break;
1595 case XCB_MAPPING_NOTIFY:
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001596 wm_log("XCB_MAPPING_NOTIFY\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001597 break;
1598 case XCB_PROPERTY_NOTIFY:
1599 weston_wm_handle_property_notify(wm, event);
1600 break;
1601 case XCB_CLIENT_MESSAGE:
1602 weston_wm_handle_client_message(wm, event);
1603 break;
1604 }
1605
1606 free(event);
1607 count++;
1608 }
1609
1610 xcb_flush(wm->conn);
1611
1612 return count;
1613}
1614
1615static void
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001616weston_wm_get_visual_and_colormap(struct weston_wm *wm)
1617{
1618 xcb_depth_iterator_t d_iter;
1619 xcb_visualtype_iterator_t vt_iter;
1620 xcb_visualtype_t *visualtype;
1621
1622 d_iter = xcb_screen_allowed_depths_iterator(wm->screen);
1623 visualtype = NULL;
1624 while (d_iter.rem > 0) {
1625 if (d_iter.data->depth == 32) {
1626 vt_iter = xcb_depth_visuals_iterator(d_iter.data);
1627 visualtype = vt_iter.data;
1628 break;
1629 }
1630
1631 xcb_depth_next(&d_iter);
1632 }
1633
1634 if (visualtype == NULL) {
1635 weston_log("no 32 bit visualtype\n");
1636 return;
1637 }
1638
1639 wm->visual_id = visualtype->visual_id;
1640 wm->colormap = xcb_generate_id(wm->conn);
1641 xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE,
1642 wm->colormap, wm->screen->root, wm->visual_id);
1643}
1644
1645static void
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001646weston_wm_get_resources(struct weston_wm *wm)
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001647{
1648
1649#define F(field) offsetof(struct weston_wm, field)
1650
1651 static const struct { const char *name; int offset; } atoms[] = {
1652 { "WM_PROTOCOLS", F(atom.wm_protocols) },
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07001653 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001654 { "WM_TAKE_FOCUS", F(atom.wm_take_focus) },
1655 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberga6d9a5e2012-05-30 12:15:44 -04001656 { "WM_STATE", F(atom.wm_state) },
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001657 { "WM_S0", F(atom.wm_s0) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001658 { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) },
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001659 { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001660 { "_NET_WM_NAME", F(atom.net_wm_name) },
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001661 { "_NET_WM_PID", F(atom.net_wm_pid) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001662 { "_NET_WM_ICON", F(atom.net_wm_icon) },
1663 { "_NET_WM_STATE", F(atom.net_wm_state) },
1664 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1665 { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) },
1666 { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) },
1667 { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) },
1668
1669 { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) },
1670 { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) },
1671 { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) },
1672 { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) },
1673 { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) },
1674 { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) },
1675 { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) },
Tiago Vignattibf1e8662012-06-12 14:07:49 +03001676 { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) },
1677 { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001678 { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) },
1679 { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) },
1680 { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) },
1681 { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) },
1682 { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) },
1683
1684 { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) },
1685 { "_NET_SUPPORTING_WM_CHECK",
1686 F(atom.net_supporting_wm_check) },
1687 { "_NET_SUPPORTED", F(atom.net_supported) },
1688 { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) },
1689 { "CLIPBOARD", F(atom.clipboard) },
Kristian Høgsbergcba022a2012-06-04 10:11:45 -04001690 { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) },
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001691 { "TARGETS", F(atom.targets) },
1692 { "UTF8_STRING", F(atom.utf8_string) },
1693 { "_WL_SELECTION", F(atom.wl_selection) },
1694 { "INCR", F(atom.incr) },
1695 { "TIMESTAMP", F(atom.timestamp) },
1696 { "MULTIPLE", F(atom.multiple) },
1697 { "UTF8_STRING" , F(atom.utf8_string) },
1698 { "COMPOUND_TEXT", F(atom.compound_text) },
1699 { "TEXT", F(atom.text) },
1700 { "STRING", F(atom.string) },
1701 { "text/plain;charset=utf-8", F(atom.text_plain_utf8) },
1702 { "text/plain", F(atom.text_plain) },
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001703 { "XdndSelection", F(atom.xdnd_selection) },
1704 { "XdndAware", F(atom.xdnd_aware) },
1705 { "XdndEnter", F(atom.xdnd_enter) },
1706 { "XdndLeave", F(atom.xdnd_leave) },
1707 { "XdndDrop", F(atom.xdnd_drop) },
1708 { "XdndStatus", F(atom.xdnd_status) },
1709 { "XdndFinished", F(atom.xdnd_finished) },
1710 { "XdndTypeList", F(atom.xdnd_type_list) },
1711 { "XdndActionCopy", F(atom.xdnd_action_copy) }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001712 };
1713#undef F
1714
1715 xcb_xfixes_query_version_cookie_t xfixes_cookie;
1716 xcb_xfixes_query_version_reply_t *xfixes_reply;
1717 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1718 xcb_intern_atom_reply_t *reply;
1719 xcb_render_query_pict_formats_reply_t *formats_reply;
1720 xcb_render_query_pict_formats_cookie_t formats_cookie;
1721 xcb_render_pictforminfo_t *formats;
1722 uint32_t i;
1723
1724 xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id);
1725
1726 formats_cookie = xcb_render_query_pict_formats(wm->conn);
1727
1728 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1729 cookies[i] = xcb_intern_atom (wm->conn, 0,
1730 strlen(atoms[i].name),
1731 atoms[i].name);
1732
1733 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1734 reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL);
1735 *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom;
1736 free(reply);
1737 }
1738
1739 wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id);
1740 if (!wm->xfixes || !wm->xfixes->present)
Martin Minarik6d118362012-06-07 18:01:59 +02001741 weston_log("xfixes not available\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001742
1743 xfixes_cookie = xcb_xfixes_query_version(wm->conn,
1744 XCB_XFIXES_MAJOR_VERSION,
1745 XCB_XFIXES_MINOR_VERSION);
1746 xfixes_reply = xcb_xfixes_query_version_reply(wm->conn,
1747 xfixes_cookie, NULL);
1748
Martin Minarik6d118362012-06-07 18:01:59 +02001749 weston_log("xfixes version: %d.%d\n",
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001750 xfixes_reply->major_version, xfixes_reply->minor_version);
1751
1752 free(xfixes_reply);
1753
1754 formats_reply = xcb_render_query_pict_formats_reply(wm->conn,
1755 formats_cookie, 0);
1756 if (formats_reply == NULL)
1757 return;
1758
1759 formats = xcb_render_query_pict_formats_formats(formats_reply);
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001760 for (i = 0; i < formats_reply->num_formats; i++) {
1761 if (formats[i].direct.red_mask != 0xff &&
1762 formats[i].direct.red_shift != 16)
1763 continue;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001764 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1765 formats[i].depth == 24)
Kristian Høgsberge89cef32012-07-16 11:57:08 -04001766 wm->format_rgb = formats[i];
1767 if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT &&
1768 formats[i].depth == 32 &&
1769 formats[i].direct.alpha_mask == 0xff &&
1770 formats[i].direct.alpha_shift == 24)
1771 wm->format_rgba = formats[i];
1772 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001773
1774 free(formats_reply);
1775}
1776
1777static void
1778weston_wm_create_wm_window(struct weston_wm *wm)
1779{
1780 static const char name[] = "Weston WM";
1781
1782 wm->wm_window = xcb_generate_id(wm->conn);
1783 xcb_create_window(wm->conn,
1784 XCB_COPY_FROM_PARENT,
1785 wm->wm_window,
1786 wm->screen->root,
1787 0, 0,
1788 10, 10,
1789 0,
1790 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1791 wm->screen->root_visual,
1792 0, NULL);
1793
1794 xcb_change_property(wm->conn,
1795 XCB_PROP_MODE_REPLACE,
1796 wm->wm_window,
1797 wm->atom.net_supporting_wm_check,
1798 XCB_ATOM_WINDOW,
1799 32, /* format */
1800 1, &wm->wm_window);
1801
1802 xcb_change_property(wm->conn,
1803 XCB_PROP_MODE_REPLACE,
1804 wm->wm_window,
1805 wm->atom.net_wm_name,
1806 wm->atom.utf8_string,
1807 8, /* format */
1808 strlen(name), name);
1809
1810 xcb_change_property(wm->conn,
1811 XCB_PROP_MODE_REPLACE,
1812 wm->screen->root,
1813 wm->atom.net_supporting_wm_check,
1814 XCB_ATOM_WINDOW,
1815 32, /* format */
1816 1, &wm->wm_window);
1817
Kristian Høgsberg670b5d32012-06-04 11:00:40 -04001818 /* Claim the WM_S0 selection even though we don't suport
1819 * the --replace functionality. */
1820 xcb_set_selection_owner(wm->conn,
1821 wm->wm_window,
1822 wm->atom.wm_s0,
1823 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg69981d92013-08-21 22:14:58 -07001824
1825 xcb_set_selection_owner(wm->conn,
1826 wm->wm_window,
1827 wm->atom.net_wm_cm_s0,
1828 XCB_TIME_CURRENT_TIME);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001829}
1830
1831struct weston_wm *
1832weston_wm_create(struct weston_xserver *wxs)
1833{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001834 struct weston_wm *wm;
1835 struct wl_event_loop *loop;
1836 xcb_screen_iterator_t s;
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001837 uint32_t values[1];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001838 int sv[2];
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001839 xcb_atom_t supported[3];
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001840
Peter Huttererf3d62272013-08-08 11:57:05 +10001841 wm = zalloc(sizeof *wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001842 if (wm == NULL)
1843 return NULL;
1844
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001845 wm->server = wxs;
1846 wm->window_hash = hash_table_create();
1847 if (wm->window_hash == NULL) {
1848 free(wm);
1849 return NULL;
1850 }
1851
1852 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001853 weston_log("socketpair failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001854 hash_table_destroy(wm->window_hash);
1855 free(wm);
1856 return NULL;
1857 }
1858
1859 xserver_send_client(wxs->resource, sv[1]);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001860 wl_client_flush(wl_resource_get_client(wxs->resource));
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001861 close(sv[1]);
1862
1863 /* xcb_connect_to_fd takes ownership of the fd. */
1864 wm->conn = xcb_connect_to_fd(sv[0], NULL);
1865 if (xcb_connection_has_error(wm->conn)) {
Martin Minarik6d118362012-06-07 18:01:59 +02001866 weston_log("xcb_connect_to_fd failed\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001867 close(sv[0]);
1868 hash_table_destroy(wm->window_hash);
1869 free(wm);
1870 return NULL;
1871 }
1872
1873 s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn));
1874 wm->screen = s.data;
1875
1876 loop = wl_display_get_event_loop(wxs->wl_display);
1877 wm->source =
1878 wl_event_loop_add_fd(loop, sv[0],
1879 WL_EVENT_READABLE,
1880 weston_wm_handle_event, wm);
1881 wl_event_source_check(wm->source);
1882
Tiago Vignatti9c4ff832012-11-30 17:19:57 -02001883 weston_wm_get_resources(wm);
Kristian Høgsbergf9187192013-05-02 13:43:24 -04001884 weston_wm_get_visual_and_colormap(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001885
1886 values[0] =
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001887 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
1888 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1889 XCB_EVENT_MASK_PROPERTY_CHANGE;
1890 xcb_change_window_attributes(wm->conn, wm->screen->root,
1891 XCB_CW_EVENT_MASK, values);
1892 wm->theme = theme_create();
1893
1894 weston_wm_create_wm_window(wm);
1895
1896 supported[0] = wm->atom.net_wm_moveresize;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001897 supported[1] = wm->atom.net_wm_state;
1898 supported[2] = wm->atom.net_wm_state_fullscreen;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001899 xcb_change_property(wm->conn,
1900 XCB_PROP_MODE_REPLACE,
1901 wm->screen->root,
1902 wm->atom.net_supported,
1903 XCB_ATOM_ATOM,
1904 32, /* format */
1905 ARRAY_LENGTH(supported), supported);
1906
Kristian Høgsberg4dec0112012-06-03 09:18:06 -04001907 weston_wm_selection_init(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001908
Kristian Høgsbergf9cb3b12013-09-04 21:12:26 -07001909 weston_wm_dnd_init(wm);
1910
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001911 xcb_flush(wm->conn);
1912
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001913 wm->activate_listener.notify = weston_wm_window_activate;
1914 wl_signal_add(&wxs->compositor->activate_signal,
1915 &wm->activate_listener);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001916 wm->transform_listener.notify = weston_wm_window_transform;
1917 wl_signal_add(&wxs->compositor->transform_signal,
1918 &wm->transform_listener);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001919 wm->kill_listener.notify = weston_wm_kill_client;
1920 wl_signal_add(&wxs->compositor->kill_signal,
1921 &wm->kill_listener);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001922
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001923 weston_wm_create_cursors(wm);
Tiago Vignattic1903232012-07-16 12:15:37 -04001924 weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001925
Martin Minarik6d118362012-06-07 18:01:59 +02001926 weston_log("created wm\n");
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001927
1928 return wm;
1929}
1930
1931void
1932weston_wm_destroy(struct weston_wm *wm)
1933{
1934 /* FIXME: Free windows in hash. */
1935 hash_table_destroy(wm->window_hash);
Tiago Vignatti236b48d2012-07-16 12:09:19 -04001936 weston_wm_destroy_cursors(wm);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001937 xcb_disconnect(wm->conn);
1938 wl_event_source_remove(wm->source);
1939 wl_list_remove(&wm->selection_listener.link);
1940 wl_list_remove(&wm->activate_listener.link);
Tiago Vignatti0d20d7c2012-09-27 17:48:37 +03001941 wl_list_remove(&wm->kill_listener.link);
Louis-Francis Ratté-Bouliannedce3dac2013-07-20 05:16:45 +01001942 wl_list_remove(&wm->transform_listener.link);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001943
1944 free(wm);
1945}
1946
1947static void
1948surface_destroy(struct wl_listener *listener, void *data)
1949{
1950 struct weston_wm_window *window =
1951 container_of(listener,
1952 struct weston_wm_window, surface_destroy_listener);
1953
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04001954 wm_log("surface for xid %d destroyed\n", window->id);
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07001955
Giulio Camuffo62942ad2013-09-11 18:20:47 +02001956 /* This should have been freed by the shell.
1957 Don't try to use it later. */
1958 window->shsurf = NULL;
Kristian Høgsberg81cadc72013-09-03 16:15:42 -07001959 window->surface = NULL;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001960}
1961
1962static struct weston_wm_window *
1963get_wm_window(struct weston_surface *surface)
1964{
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001965 struct wl_listener *listener;
1966
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001967 listener = wl_signal_get(&surface->destroy_signal, surface_destroy);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04001968 if (listener)
1969 return container_of(listener, struct weston_wm_window,
1970 surface_destroy_listener);
1971
1972 return NULL;
1973}
1974
1975static void
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001976weston_wm_window_configure(void *data)
1977{
1978 struct weston_wm_window *window = data;
1979 struct weston_wm *wm = window->wm;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001980 uint32_t values[4];
1981 int x, y, width, height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001982
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001983 weston_wm_window_get_child_position(window, &x, &y);
1984 values[0] = x;
1985 values[1] = y;
1986 values[2] = window->width;
1987 values[3] = window->height;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001988 xcb_configure_window(wm->conn,
1989 window->id,
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05001990 XCB_CONFIG_WINDOW_X |
1991 XCB_CONFIG_WINDOW_Y |
Kristian Høgsberga61ca062012-05-22 16:05:52 -04001992 XCB_CONFIG_WINDOW_WIDTH |
1993 XCB_CONFIG_WINDOW_HEIGHT,
1994 values);
1995
1996 weston_wm_window_get_frame_size(window, &width, &height);
1997 values[0] = width;
1998 values[1] = height;
1999 xcb_configure_window(wm->conn,
2000 window->frame_id,
2001 XCB_CONFIG_WINDOW_WIDTH |
2002 XCB_CONFIG_WINDOW_HEIGHT,
2003 values);
2004
2005 window->configure_source = NULL;
2006
2007 weston_wm_window_schedule_repaint(window);
2008}
2009
2010static void
2011send_configure(struct weston_surface *surface,
2012 uint32_t edges, int32_t width, int32_t height)
2013{
2014 struct weston_wm_window *window = get_wm_window(surface);
2015 struct weston_wm *wm = window->wm;
2016 struct theme *t = window->wm->theme;
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002017 int vborder, hborder;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002018
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002019 if (window->fullscreen) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002020 hborder = 0;
2021 vborder = 0;
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002022 } else if (window->decorate) {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002023 hborder = 2 * (t->margin + t->width);
2024 vborder = 2 * t->margin + t->titlebar_height + t->width;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002025 } else {
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002026 hborder = 2 * t->margin;
2027 vborder = 2 * t->margin;
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002028 }
2029
Kristian Høgsbergfa514b42013-07-08 15:00:25 -04002030 if (width > hborder)
2031 window->width = width - hborder;
2032 else
2033 window->width = 1;
2034
2035 if (height > vborder)
2036 window->height = height - vborder;
2037 else
2038 window->height = 1;
2039
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002040 if (window->configure_source)
2041 return;
2042
2043 window->configure_source =
2044 wl_event_loop_add_idle(wm->server->loop,
2045 weston_wm_window_configure, window);
2046}
2047
2048static const struct weston_shell_client shell_client = {
2049 send_configure
2050};
2051
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002052static int
2053legacy_fullscreen(struct weston_wm *wm,
2054 struct weston_wm_window *window,
2055 struct weston_output **output_ret)
2056{
2057 struct weston_compositor *compositor = wm->server->compositor;
2058 struct weston_output *output;
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002059 uint32_t minmax = PMinSize | PMaxSize;
2060 int matching_size;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002061
2062 /* Heuristics for detecting legacy fullscreen windows... */
2063
2064 wl_list_for_each(output, &compositor->output_list, link) {
2065 if (output->x == window->x &&
2066 output->y == window->y &&
2067 output->width == window->width &&
2068 output->height == window->height &&
2069 window->override_redirect) {
2070 *output_ret = output;
2071 return 1;
2072 }
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002073
2074 matching_size = 0;
2075 if ((window->size_hints.flags & (USSize |PSize)) &&
2076 window->size_hints.width == output->width &&
2077 window->size_hints.height == output->height)
2078 matching_size = 1;
2079 if ((window->size_hints.flags & minmax) == minmax &&
2080 window->size_hints.min_width == output->width &&
2081 window->size_hints.min_height == output->height &&
2082 window->size_hints.max_width == output->width &&
2083 window->size_hints.max_height == output->height)
2084 matching_size = 1;
2085
2086 if (matching_size && !window->decorate &&
2087 (window->size_hints.flags & (USPosition | PPosition)) &&
2088 window->size_hints.x == output->x &&
2089 window->size_hints.y == output->y) {
2090 *output_ret = output;
2091 return 1;
2092 }
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002093 }
2094
2095 return 0;
2096}
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002097static void
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002098xserver_map_shell_surface(struct weston_wm *wm,
2099 struct weston_wm_window *window)
2100{
2101 struct weston_shell_interface *shell_interface =
2102 &wm->server->compositor->shell_interface;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002103 struct weston_output *output;
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002104
2105 if (!shell_interface->create_shell_surface)
2106 return;
2107
2108 window->shsurf =
2109 shell_interface->create_shell_surface(shell_interface->shell,
Kristian Høgsberga61ca062012-05-22 16:05:52 -04002110 window->surface,
2111 &shell_client);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002112
Giulio Camuffo62942ad2013-09-11 18:20:47 +02002113 if (window->name)
2114 shell_interface->set_title(window->shsurf, window->name);
2115
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002116 if (window->fullscreen) {
2117 window->saved_width = window->width;
2118 window->saved_height = window->height;
2119 shell_interface->set_fullscreen(window->shsurf,
2120 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2121 0, NULL);
2122 return;
Kristian Høgsberg59f44c12013-08-31 00:08:27 -07002123 } else if (legacy_fullscreen(wm, window, &output)) {
2124 window->fullscreen = 1;
2125 shell_interface->set_fullscreen(window->shsurf,
2126 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2127 0, output);
Giulio Camuffoca43f092013-09-11 17:49:13 +02002128 } else if (!window->override_redirect && !window->transient_for) {
Kristian Høgsberg1a7a57f2013-08-31 00:12:25 -07002129 shell_interface->set_toplevel(window->shsurf);
2130 return;
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002131 } else {
2132 shell_interface->set_xwayland(window->shsurf,
Kristian Høgsberg146f5ba2013-08-22 16:24:15 -07002133 window->x,
2134 window->y,
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002135 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002136 }
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002137}
2138
2139static void
2140xserver_set_window_id(struct wl_client *client, struct wl_resource *resource,
2141 struct wl_resource *surface_resource, uint32_t id)
2142{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002143 struct weston_xserver *wxs = wl_resource_get_user_data(resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002144 struct weston_wm *wm = wxs->wm;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002145 struct weston_surface *surface =
2146 wl_resource_get_user_data(surface_resource);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002147 struct weston_wm_window *window;
2148
2149 if (client != wxs->client)
2150 return;
2151
2152 window = hash_table_lookup(wm->window_hash, id);
2153 if (window == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002154 weston_log("set_window_id for unknown window %d\n", id);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002155 return;
2156 }
2157
Kristian Høgsberg082d58c2013-06-18 01:00:27 -04002158 wm_log("set_window_id %d for surface %p\n", id, surface);
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002159
2160 weston_wm_window_read_properties(window);
2161
2162 window->surface = (struct weston_surface *) surface;
2163 window->surface_destroy_listener.notify = surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002164 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg380deee2012-05-21 17:12:41 -04002165 &window->surface_destroy_listener);
2166
2167 weston_wm_window_schedule_repaint(window);
2168 xserver_map_shell_surface(wm, window);
2169}
2170
2171const struct xserver_interface xserver_implementation = {
2172 xserver_set_window_id
2173};