Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1 | /* |
| 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 Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 23 | #include "config.h" |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 24 | |
| 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 Vignatti | 90fada4 | 2012-07-16 12:02:08 -0400 | [diff] [blame] | 34 | #include <X11/Xcursor/Xcursor.h> |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 35 | |
| 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øgsberg | 1a7a57f | 2013-08-31 00:12:25 -0700 | [diff] [blame] | 43 | struct 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øgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 69 | struct 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 | |
MoD | 3170012 | 2013-06-11 19:58:55 -0500 | [diff] [blame] | 118 | #define SEND_EVENT_MASK (0x80) |
| 119 | #define EVENT_TYPE(event) ((event)->response_type & ~SEND_EVENT_MASK) |
| 120 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 121 | struct weston_wm_window { |
| 122 | struct weston_wm *wm; |
| 123 | xcb_window_t id; |
| 124 | xcb_window_t frame_id; |
| 125 | cairo_surface_t *cairo_surface; |
| 126 | struct weston_surface *surface; |
| 127 | struct shell_surface *shsurf; |
| 128 | struct wl_listener surface_destroy_listener; |
| 129 | struct wl_event_source *repaint_source; |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 130 | struct wl_event_source *configure_source; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 131 | int properties_dirty; |
Tiago Vignatti | 0d20d7c | 2012-09-27 17:48:37 +0300 | [diff] [blame] | 132 | int pid; |
| 133 | char *machine; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 134 | char *class; |
| 135 | char *name; |
| 136 | struct weston_wm_window *transient_for; |
| 137 | uint32_t protocols; |
| 138 | xcb_atom_t type; |
| 139 | int width, height; |
| 140 | int x, y; |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 141 | int saved_width, saved_height; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 142 | int decorate; |
Tiago Vignatti | 771241e | 2012-06-04 20:01:45 +0300 | [diff] [blame] | 143 | int override_redirect; |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 144 | int fullscreen; |
MoD | 384a11a | 2013-06-22 11:04:21 -0500 | [diff] [blame] | 145 | int has_alpha; |
Kristian Høgsberg | 1a7a57f | 2013-08-31 00:12:25 -0700 | [diff] [blame] | 146 | struct wm_size_hints size_hints; |
| 147 | struct motif_wm_hints motif_hints; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | static struct weston_wm_window * |
| 151 | get_wm_window(struct weston_surface *surface); |
| 152 | |
Kristian Høgsberg | eaee784 | 2012-05-22 10:04:20 -0400 | [diff] [blame] | 153 | static void |
| 154 | weston_wm_window_schedule_repaint(struct weston_wm_window *window); |
| 155 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 156 | static int __attribute__ ((format (printf, 1, 2))) |
| 157 | wm_log(const char *fmt, ...) |
| 158 | { |
| 159 | #ifdef WM_DEBUG |
| 160 | int l; |
| 161 | va_list argp; |
| 162 | |
| 163 | va_start(argp, fmt); |
| 164 | l = weston_vlog(fmt, argp); |
| 165 | va_end(argp); |
| 166 | |
| 167 | return l; |
| 168 | #else |
| 169 | return 0; |
| 170 | #endif |
| 171 | } |
| 172 | |
| 173 | static int __attribute__ ((format (printf, 1, 2))) |
| 174 | wm_log_continue(const char *fmt, ...) |
| 175 | { |
| 176 | #ifdef WM_DEBUG |
| 177 | int l; |
| 178 | va_list argp; |
| 179 | |
| 180 | va_start(argp, fmt); |
| 181 | l = weston_vlog_continue(fmt, argp); |
| 182 | va_end(argp); |
| 183 | |
| 184 | return l; |
| 185 | #else |
| 186 | return 0; |
| 187 | #endif |
| 188 | } |
| 189 | |
| 190 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 191 | const char * |
| 192 | get_atom_name(xcb_connection_t *c, xcb_atom_t atom) |
| 193 | { |
| 194 | xcb_get_atom_name_cookie_t cookie; |
| 195 | xcb_get_atom_name_reply_t *reply; |
| 196 | xcb_generic_error_t *e; |
| 197 | static char buffer[64]; |
| 198 | |
| 199 | if (atom == XCB_ATOM_NONE) |
| 200 | return "None"; |
| 201 | |
| 202 | cookie = xcb_get_atom_name (c, atom); |
| 203 | reply = xcb_get_atom_name_reply (c, cookie, &e); |
MoD | 55375b9 | 2013-06-11 19:59:42 -0500 | [diff] [blame] | 204 | |
| 205 | if(reply) { |
| 206 | snprintf(buffer, sizeof buffer, "%.*s", |
| 207 | xcb_get_atom_name_name_length (reply), |
| 208 | xcb_get_atom_name_name (reply)); |
| 209 | } else { |
| 210 | snprintf(buffer, sizeof buffer, "(atom %u)", atom); |
| 211 | } |
| 212 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 213 | free(reply); |
| 214 | |
| 215 | return buffer; |
| 216 | } |
| 217 | |
Tiago Vignatti | 90fada4 | 2012-07-16 12:02:08 -0400 | [diff] [blame] | 218 | static xcb_cursor_t |
| 219 | xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img) |
| 220 | { |
| 221 | xcb_connection_t *c = wm->conn; |
| 222 | xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c)); |
| 223 | xcb_screen_t *screen = s.data; |
| 224 | xcb_gcontext_t gc; |
| 225 | xcb_pixmap_t pix; |
| 226 | xcb_render_picture_t pic; |
| 227 | xcb_cursor_t cursor; |
| 228 | int stride = img->width * 4; |
| 229 | |
| 230 | pix = xcb_generate_id(c); |
| 231 | xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height); |
| 232 | |
| 233 | pic = xcb_generate_id(c); |
| 234 | xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0); |
| 235 | |
| 236 | gc = xcb_generate_id(c); |
| 237 | xcb_create_gc(c, gc, pix, 0, 0); |
| 238 | |
| 239 | xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc, |
| 240 | img->width, img->height, 0, 0, 0, 32, |
| 241 | stride * img->height, (uint8_t *) img->pixels); |
| 242 | xcb_free_gc(c, gc); |
| 243 | |
| 244 | cursor = xcb_generate_id(c); |
| 245 | xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot); |
| 246 | |
| 247 | xcb_render_free_picture(c, pic); |
| 248 | xcb_free_pixmap(c, pix); |
| 249 | |
| 250 | return cursor; |
| 251 | } |
| 252 | |
| 253 | static xcb_cursor_t |
| 254 | xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images) |
| 255 | { |
| 256 | /* TODO: treat animated cursors as well */ |
| 257 | if (images->nimage != 1) |
| 258 | return -1; |
| 259 | |
| 260 | return xcb_cursor_image_load_cursor(wm, images->images[0]); |
| 261 | } |
| 262 | |
| 263 | static xcb_cursor_t |
| 264 | xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file) |
| 265 | { |
| 266 | xcb_cursor_t cursor; |
| 267 | XcursorImages *images; |
| 268 | char *v = NULL; |
| 269 | int size = 0; |
| 270 | |
| 271 | if (!file) |
| 272 | return 0; |
| 273 | |
| 274 | v = getenv ("XCURSOR_SIZE"); |
| 275 | if (v) |
| 276 | size = atoi(v); |
| 277 | |
| 278 | if (!size) |
| 279 | size = 32; |
| 280 | |
| 281 | images = XcursorLibraryLoadImages (file, NULL, size); |
Tiago Vignatti | ac78bb1 | 2012-09-28 16:29:46 +0300 | [diff] [blame] | 282 | if (!images) |
| 283 | return -1; |
| 284 | |
Tiago Vignatti | 90fada4 | 2012-07-16 12:02:08 -0400 | [diff] [blame] | 285 | cursor = xcb_cursor_images_load_cursor (wm, images); |
| 286 | XcursorImagesDestroy (images); |
| 287 | |
| 288 | return cursor; |
| 289 | } |
| 290 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 291 | void |
Kristian Høgsberg | 0273b57 | 2012-05-30 09:58:02 -0400 | [diff] [blame] | 292 | dump_property(struct weston_wm *wm, |
| 293 | xcb_atom_t property, xcb_get_property_reply_t *reply) |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 294 | { |
| 295 | int32_t *incr_value; |
| 296 | const char *text_value, *name; |
| 297 | xcb_atom_t *atom_value; |
| 298 | int width, len; |
| 299 | uint32_t i; |
| 300 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 301 | width = wm_log_continue("%s: ", get_atom_name(wm->conn, property)); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 302 | if (reply == NULL) { |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 303 | wm_log_continue("(no reply)\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 304 | return; |
| 305 | } |
| 306 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 307 | width += wm_log_continue("%s/%d, length %d (value_len %d): ", |
| 308 | get_atom_name(wm->conn, reply->type), |
| 309 | reply->format, |
| 310 | xcb_get_property_value_length(reply), |
| 311 | reply->value_len); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 312 | |
| 313 | if (reply->type == wm->atom.incr) { |
| 314 | incr_value = xcb_get_property_value(reply); |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 315 | wm_log_continue("%d\n", *incr_value); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 316 | } else if (reply->type == wm->atom.utf8_string || |
| 317 | reply->type == wm->atom.string) { |
| 318 | text_value = xcb_get_property_value(reply); |
| 319 | if (reply->value_len > 40) |
| 320 | len = 40; |
| 321 | else |
| 322 | len = reply->value_len; |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 323 | wm_log_continue("\"%.*s\"\n", len, text_value); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 324 | } else if (reply->type == XCB_ATOM_ATOM) { |
| 325 | atom_value = xcb_get_property_value(reply); |
| 326 | for (i = 0; i < reply->value_len; i++) { |
| 327 | name = get_atom_name(wm->conn, atom_value[i]); |
| 328 | if (width + strlen(name) + 2 > 78) { |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 329 | wm_log_continue("\n "); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 330 | width = 4; |
| 331 | } else if (i > 0) { |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 332 | width += wm_log_continue(", "); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 333 | } |
| 334 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 335 | width += wm_log_continue("%s", name); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 336 | } |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 337 | wm_log_continue("\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 338 | } else { |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 339 | wm_log_continue("huh?\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Tiago Vignatti | 2d129f1 | 2012-11-30 17:19:59 -0200 | [diff] [blame] | 343 | static void |
Kristian Høgsberg | 0273b57 | 2012-05-30 09:58:02 -0400 | [diff] [blame] | 344 | read_and_dump_property(struct weston_wm *wm, |
| 345 | xcb_window_t window, xcb_atom_t property) |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 346 | { |
Kristian Høgsberg | 0273b57 | 2012-05-30 09:58:02 -0400 | [diff] [blame] | 347 | xcb_get_property_reply_t *reply; |
| 348 | xcb_get_property_cookie_t cookie; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 349 | |
Kristian Høgsberg | 0273b57 | 2012-05-30 09:58:02 -0400 | [diff] [blame] | 350 | cookie = xcb_get_property(wm->conn, 0, window, |
| 351 | property, XCB_ATOM_ANY, 0, 2048); |
| 352 | reply = xcb_get_property_reply(wm->conn, cookie, NULL); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 353 | |
Kristian Høgsberg | 0273b57 | 2012-05-30 09:58:02 -0400 | [diff] [blame] | 354 | dump_property(wm, property, reply); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 355 | |
Kristian Høgsberg | 0273b57 | 2012-05-30 09:58:02 -0400 | [diff] [blame] | 356 | free(reply); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* We reuse some predefined, but otherwise useles atoms */ |
| 360 | #define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0 |
| 361 | #define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1 |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 362 | #define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2 |
Kristian Høgsberg | 1a7a57f | 2013-08-31 00:12:25 -0700 | [diff] [blame] | 363 | #define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3 |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 364 | |
| 365 | static void |
| 366 | weston_wm_window_read_properties(struct weston_wm_window *window) |
| 367 | { |
| 368 | struct weston_wm *wm = window->wm; |
| 369 | |
| 370 | #define F(field) offsetof(struct weston_wm_window, field) |
| 371 | const struct { |
| 372 | xcb_atom_t atom; |
| 373 | xcb_atom_t type; |
| 374 | int offset; |
| 375 | } props[] = { |
| 376 | { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) }, |
| 377 | { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) }, |
| 378 | { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) }, |
| 379 | { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) }, |
Kristian Høgsberg | 1a7a57f | 2013-08-31 00:12:25 -0700 | [diff] [blame] | 380 | { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) }, |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 381 | { wm->atom.net_wm_state, TYPE_NET_WM_STATE }, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 382 | { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) }, |
| 383 | { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) }, |
Tiago Vignatti | 0d20d7c | 2012-09-27 17:48:37 +0300 | [diff] [blame] | 384 | { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) }, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 385 | { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 }, |
Tiago Vignatti | 0d20d7c | 2012-09-27 17:48:37 +0300 | [diff] [blame] | 386 | { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) }, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 387 | }; |
| 388 | #undef F |
| 389 | |
| 390 | xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)]; |
| 391 | xcb_get_property_reply_t *reply; |
| 392 | void *p; |
| 393 | uint32_t *xid; |
| 394 | xcb_atom_t *atom; |
| 395 | uint32_t i; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 396 | |
| 397 | if (!window->properties_dirty) |
| 398 | return; |
| 399 | window->properties_dirty = 0; |
| 400 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 401 | for (i = 0; i < ARRAY_LENGTH(props); i++) |
| 402 | cookie[i] = xcb_get_property(wm->conn, |
| 403 | 0, /* delete */ |
| 404 | window->id, |
| 405 | props[i].atom, |
| 406 | XCB_ATOM_ANY, 0, 2048); |
| 407 | |
Tiago Vignatti | 2ea74d9 | 2012-07-20 23:09:53 +0300 | [diff] [blame] | 408 | window->decorate = !window->override_redirect; |
Kristian Høgsberg | 1a7a57f | 2013-08-31 00:12:25 -0700 | [diff] [blame] | 409 | window->size_hints.flags = 0; |
| 410 | window->motif_hints.flags = 0; |
| 411 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 412 | for (i = 0; i < ARRAY_LENGTH(props); i++) { |
| 413 | reply = xcb_get_property_reply(wm->conn, cookie[i], NULL); |
| 414 | if (!reply) |
| 415 | /* Bad window, typically */ |
| 416 | continue; |
| 417 | if (reply->type == XCB_ATOM_NONE) { |
| 418 | /* No such property */ |
| 419 | free(reply); |
| 420 | continue; |
| 421 | } |
| 422 | |
| 423 | p = ((char *) window + props[i].offset); |
| 424 | |
| 425 | switch (props[i].type) { |
Tiago Vignatti | 0d20d7c | 2012-09-27 17:48:37 +0300 | [diff] [blame] | 426 | case XCB_ATOM_WM_CLIENT_MACHINE: |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 427 | case XCB_ATOM_STRING: |
| 428 | /* FIXME: We're using this for both string and |
| 429 | utf8_string */ |
| 430 | if (*(char **) p) |
| 431 | free(*(char **) p); |
| 432 | |
| 433 | *(char **) p = |
| 434 | strndup(xcb_get_property_value(reply), |
| 435 | xcb_get_property_value_length(reply)); |
| 436 | break; |
| 437 | case XCB_ATOM_WINDOW: |
| 438 | xid = xcb_get_property_value(reply); |
| 439 | *(struct weston_wm_window **) p = |
| 440 | hash_table_lookup(wm->window_hash, *xid); |
| 441 | break; |
Tiago Vignatti | 0d20d7c | 2012-09-27 17:48:37 +0300 | [diff] [blame] | 442 | case XCB_ATOM_CARDINAL: |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 443 | case XCB_ATOM_ATOM: |
| 444 | atom = xcb_get_property_value(reply); |
| 445 | *(xcb_atom_t *) p = *atom; |
| 446 | break; |
| 447 | case TYPE_WM_PROTOCOLS: |
| 448 | break; |
Kristian Høgsberg | 1a7a57f | 2013-08-31 00:12:25 -0700 | [diff] [blame] | 449 | case TYPE_WM_NORMAL_HINTS: |
| 450 | memcpy(&window->size_hints, |
| 451 | xcb_get_property_value(reply), |
| 452 | sizeof window->size_hints); |
| 453 | break; |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 454 | case TYPE_NET_WM_STATE: |
| 455 | window->fullscreen = 0; |
| 456 | atom = xcb_get_property_value(reply); |
| 457 | for (i = 0; i < reply->value_len; i++) |
| 458 | if (atom[i] == wm->atom.net_wm_state_fullscreen) |
| 459 | window->fullscreen = 1; |
| 460 | break; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 461 | case TYPE_MOTIF_WM_HINTS: |
Kristian Høgsberg | 1a7a57f | 2013-08-31 00:12:25 -0700 | [diff] [blame] | 462 | memcpy(&window->motif_hints, |
| 463 | xcb_get_property_value(reply), |
| 464 | sizeof window->motif_hints); |
| 465 | if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) |
| 466 | window->decorate = |
| 467 | window->motif_hints.decorations > 0; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 468 | break; |
| 469 | default: |
| 470 | break; |
| 471 | } |
| 472 | free(reply); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 477 | weston_wm_window_get_frame_size(struct weston_wm_window *window, |
| 478 | int *width, int *height) |
| 479 | { |
| 480 | struct theme *t = window->wm->theme; |
| 481 | |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 482 | if (window->fullscreen) { |
| 483 | *width = window->width; |
| 484 | *height = window->height; |
| 485 | } else if (window->decorate) { |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 486 | *width = window->width + (t->margin + t->width) * 2; |
| 487 | *height = window->height + |
| 488 | t->margin * 2 + t->width + t->titlebar_height; |
| 489 | } else { |
| 490 | *width = window->width + t->margin * 2; |
| 491 | *height = window->height + t->margin * 2; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | static void |
| 496 | weston_wm_window_get_child_position(struct weston_wm_window *window, |
| 497 | int *x, int *y) |
| 498 | { |
| 499 | struct theme *t = window->wm->theme; |
| 500 | |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 501 | if (window->fullscreen) { |
| 502 | *x = 0; |
| 503 | *y = 0; |
| 504 | } else if (window->decorate) { |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 505 | *x = t->margin + t->width; |
| 506 | *y = t->margin + t->titlebar_height; |
| 507 | } else { |
| 508 | *x = t->margin; |
| 509 | *y = t->margin; |
| 510 | } |
| 511 | } |
Kristian Høgsberg | eaee784 | 2012-05-22 10:04:20 -0400 | [diff] [blame] | 512 | |
| 513 | static void |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 514 | weston_wm_window_send_configure_notify(struct weston_wm_window *window) |
| 515 | { |
| 516 | xcb_configure_notify_event_t configure_notify; |
| 517 | struct weston_wm *wm = window->wm; |
| 518 | int x, y; |
| 519 | |
| 520 | weston_wm_window_get_child_position(window, &x, &y); |
| 521 | configure_notify.response_type = XCB_CONFIGURE_NOTIFY; |
| 522 | configure_notify.pad0 = 0; |
| 523 | configure_notify.event = window->id; |
| 524 | configure_notify.window = window->id; |
| 525 | configure_notify.above_sibling = XCB_WINDOW_NONE; |
| 526 | configure_notify.x = x; |
| 527 | configure_notify.y = y; |
| 528 | configure_notify.width = window->width; |
| 529 | configure_notify.height = window->height; |
| 530 | configure_notify.border_width = 0; |
| 531 | configure_notify.override_redirect = 0; |
| 532 | configure_notify.pad1 = 0; |
| 533 | |
| 534 | xcb_send_event(wm->conn, 0, window->id, |
| 535 | XCB_EVENT_MASK_STRUCTURE_NOTIFY, |
| 536 | (char *) &configure_notify); |
| 537 | } |
| 538 | |
| 539 | static void |
Kristian Høgsberg | eaee784 | 2012-05-22 10:04:20 -0400 | [diff] [blame] | 540 | weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event) |
| 541 | { |
| 542 | xcb_configure_request_event_t *configure_request = |
| 543 | (xcb_configure_request_event_t *) event; |
| 544 | struct weston_wm_window *window; |
| 545 | uint32_t mask, values[16]; |
| 546 | int x, y, width, height, i = 0; |
| 547 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 548 | wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n", |
| 549 | configure_request->window, |
| 550 | configure_request->x, configure_request->y, |
| 551 | configure_request->width, configure_request->height); |
Kristian Høgsberg | eaee784 | 2012-05-22 10:04:20 -0400 | [diff] [blame] | 552 | |
| 553 | window = hash_table_lookup(wm->window_hash, configure_request->window); |
| 554 | |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 555 | if (window->fullscreen) { |
| 556 | weston_wm_window_send_configure_notify(window); |
| 557 | return; |
| 558 | } |
| 559 | |
Kristian Høgsberg | eaee784 | 2012-05-22 10:04:20 -0400 | [diff] [blame] | 560 | if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH) |
| 561 | window->width = configure_request->width; |
| 562 | if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT) |
| 563 | window->height = configure_request->height; |
| 564 | |
| 565 | weston_wm_window_get_child_position(window, &x, &y); |
| 566 | values[i++] = x; |
| 567 | values[i++] = y; |
| 568 | values[i++] = window->width; |
| 569 | values[i++] = window->height; |
| 570 | values[i++] = 0; |
| 571 | mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | |
| 572 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT | |
| 573 | XCB_CONFIG_WINDOW_BORDER_WIDTH; |
| 574 | if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) { |
| 575 | values[i++] = configure_request->sibling; |
| 576 | mask |= XCB_CONFIG_WINDOW_SIBLING; |
| 577 | } |
| 578 | if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) { |
| 579 | values[i++] = configure_request->stack_mode; |
| 580 | mask |= XCB_CONFIG_WINDOW_STACK_MODE; |
| 581 | } |
| 582 | |
| 583 | xcb_configure_window(wm->conn, window->id, mask, values); |
| 584 | |
| 585 | weston_wm_window_get_frame_size(window, &width, &height); |
| 586 | values[0] = width; |
| 587 | values[1] = height; |
| 588 | mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT; |
| 589 | xcb_configure_window(wm->conn, window->frame_id, mask, values); |
| 590 | |
| 591 | weston_wm_window_schedule_repaint(window); |
| 592 | } |
| 593 | |
Kristian Høgsberg | 00db2ee | 2013-07-04 02:29:32 -0400 | [diff] [blame] | 594 | static int |
| 595 | our_resource(struct weston_wm *wm, uint32_t id) |
| 596 | { |
| 597 | const xcb_setup_t *setup; |
| 598 | |
| 599 | setup = xcb_get_setup(wm->conn); |
| 600 | |
| 601 | return (id & ~setup->resource_id_mask) == setup->resource_id_base; |
| 602 | } |
| 603 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 604 | static void |
| 605 | weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 606 | { |
| 607 | xcb_configure_notify_event_t *configure_notify = |
| 608 | (xcb_configure_notify_event_t *) event; |
| 609 | struct weston_wm_window *window; |
Tiago Vignatti | e66fcee | 2012-07-20 23:09:54 +0300 | [diff] [blame] | 610 | int x, y; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 611 | |
Kristian Høgsberg | 00db2ee | 2013-07-04 02:29:32 -0400 | [diff] [blame] | 612 | wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n", |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 613 | configure_notify->window, |
| 614 | configure_notify->x, configure_notify->y, |
| 615 | configure_notify->width, configure_notify->height); |
Tiago Vignatti | e66fcee | 2012-07-20 23:09:54 +0300 | [diff] [blame] | 616 | |
Kristian Høgsberg | 00db2ee | 2013-07-04 02:29:32 -0400 | [diff] [blame] | 617 | window = hash_table_lookup(wm->window_hash, configure_notify->window); |
Tiago Vignatti | e66fcee | 2012-07-20 23:09:54 +0300 | [diff] [blame] | 618 | weston_wm_window_get_child_position(window, &x, &y); |
Kristian Høgsberg | 122877d | 2013-08-22 16:18:17 -0700 | [diff] [blame] | 619 | window->x = configure_notify->x; |
| 620 | window->y = configure_notify->y; |
Kristian Høgsberg | 1b6fed4 | 2013-08-31 00:00:57 -0700 | [diff] [blame] | 621 | if (window->override_redirect) { |
| 622 | window->width = configure_notify->width; |
| 623 | window->height = configure_notify->height; |
| 624 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static void |
Tiago Vignatti | 0d20d7c | 2012-09-27 17:48:37 +0300 | [diff] [blame] | 628 | weston_wm_kill_client(struct wl_listener *listener, void *data) |
| 629 | { |
| 630 | struct weston_surface *surface = data; |
| 631 | struct weston_wm_window *window = get_wm_window(surface); |
| 632 | char name[1024]; |
| 633 | |
| 634 | if (!window) |
| 635 | return; |
| 636 | |
| 637 | gethostname(name, 1024); |
| 638 | |
| 639 | /* this is only one heuristic to guess the PID of a client is valid, |
| 640 | * assuming it's compliant with icccm and ewmh. Non-compliants and |
| 641 | * remote applications of course fail. */ |
| 642 | if (!strcmp(window->machine, name) && window->pid != 0) |
| 643 | kill(window->pid, SIGKILL); |
| 644 | } |
| 645 | |
| 646 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 647 | weston_wm_window_activate(struct wl_listener *listener, void *data) |
| 648 | { |
| 649 | struct weston_surface *surface = data; |
Giulio Camuffo | 9f42e50 | 2013-08-13 11:42:02 +0200 | [diff] [blame] | 650 | struct weston_wm_window *window = NULL; |
Scott Moreau | 85ecac0 | 2012-05-21 15:49:14 -0600 | [diff] [blame] | 651 | struct weston_wm *wm = |
| 652 | container_of(listener, struct weston_wm, activate_listener); |
| 653 | xcb_client_message_event_t client_message; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 654 | |
Giulio Camuffo | 9f42e50 | 2013-08-13 11:42:02 +0200 | [diff] [blame] | 655 | if (surface) { |
| 656 | window = get_wm_window(surface); |
| 657 | } |
| 658 | |
Scott Moreau | 85ecac0 | 2012-05-21 15:49:14 -0600 | [diff] [blame] | 659 | if (window) { |
| 660 | client_message.response_type = XCB_CLIENT_MESSAGE; |
| 661 | client_message.format = 32; |
| 662 | client_message.window = window->id; |
| 663 | client_message.type = wm->atom.wm_protocols; |
| 664 | client_message.data.data32[0] = wm->atom.wm_take_focus; |
| 665 | client_message.data.data32[1] = XCB_TIME_CURRENT_TIME; |
| 666 | |
| 667 | xcb_send_event(wm->conn, 0, window->id, |
| 668 | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, |
| 669 | (char *) &client_message); |
| 670 | |
| 671 | xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT, |
| 672 | window->id, XCB_TIME_CURRENT_TIME); |
| 673 | } else { |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 674 | xcb_set_input_focus (wm->conn, |
| 675 | XCB_INPUT_FOCUS_POINTER_ROOT, |
| 676 | XCB_NONE, |
| 677 | XCB_TIME_CURRENT_TIME); |
Scott Moreau | 85ecac0 | 2012-05-21 15:49:14 -0600 | [diff] [blame] | 678 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 679 | |
| 680 | if (wm->focus_window) |
| 681 | weston_wm_window_schedule_repaint(wm->focus_window); |
| 682 | wm->focus_window = window; |
| 683 | if (wm->focus_window) |
| 684 | weston_wm_window_schedule_repaint(wm->focus_window); |
| 685 | } |
| 686 | |
Tiago Vignatti | fb2adba | 2013-06-12 15:43:21 -0300 | [diff] [blame] | 687 | static void |
| 688 | weston_wm_window_transform(struct wl_listener *listener, void *data) |
| 689 | { |
| 690 | struct weston_surface *surface = data; |
| 691 | struct weston_wm_window *window = get_wm_window(surface); |
| 692 | struct weston_wm *wm = |
| 693 | container_of(listener, struct weston_wm, transform_listener); |
Tiago Vignatti | fb2adba | 2013-06-12 15:43:21 -0300 | [diff] [blame] | 694 | uint32_t mask, values[2]; |
Tiago Vignatti | fb2adba | 2013-06-12 15:43:21 -0300 | [diff] [blame] | 695 | |
| 696 | if (!window || !wm) |
| 697 | return; |
| 698 | |
| 699 | if (!weston_surface_is_mapped(surface)) |
| 700 | return; |
| 701 | |
Kristian Høgsberg | e89a8b6 | 2013-08-22 16:20:44 -0700 | [diff] [blame] | 702 | if (window->x != surface->geometry.x || |
| 703 | window->y != surface->geometry.y) { |
| 704 | values[0] = surface->geometry.x; |
| 705 | values[1] = surface->geometry.y; |
| 706 | mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y; |
Tiago Vignatti | fb2adba | 2013-06-12 15:43:21 -0300 | [diff] [blame] | 707 | |
Kristian Høgsberg | e89a8b6 | 2013-08-22 16:20:44 -0700 | [diff] [blame] | 708 | xcb_configure_window(wm->conn, window->frame_id, mask, values); |
| 709 | xcb_flush(wm->conn); |
| 710 | } |
Tiago Vignatti | fb2adba | 2013-06-12 15:43:21 -0300 | [diff] [blame] | 711 | } |
| 712 | |
Kristian Høgsberg | a6d9a5e | 2012-05-30 12:15:44 -0400 | [diff] [blame] | 713 | #define ICCCM_WITHDRAWN_STATE 0 |
| 714 | #define ICCCM_NORMAL_STATE 1 |
| 715 | #define ICCCM_ICONIC_STATE 3 |
| 716 | |
| 717 | static void |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 718 | weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state) |
Kristian Høgsberg | a6d9a5e | 2012-05-30 12:15:44 -0400 | [diff] [blame] | 719 | { |
| 720 | struct weston_wm *wm = window->wm; |
| 721 | uint32_t property[2]; |
| 722 | |
| 723 | property[0] = state; |
| 724 | property[1] = XCB_WINDOW_NONE; |
| 725 | |
| 726 | xcb_change_property(wm->conn, |
| 727 | XCB_PROP_MODE_REPLACE, |
| 728 | window->id, |
| 729 | wm->atom.wm_state, |
| 730 | wm->atom.wm_state, |
| 731 | 32, /* format */ |
| 732 | 2, property); |
| 733 | } |
| 734 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 735 | static void |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 736 | weston_wm_window_set_net_wm_state(struct weston_wm_window *window) |
| 737 | { |
| 738 | struct weston_wm *wm = window->wm; |
| 739 | uint32_t property[1]; |
| 740 | int i; |
| 741 | |
| 742 | i = 0; |
| 743 | if (window->fullscreen) |
| 744 | property[i++] = wm->atom.net_wm_state_fullscreen; |
| 745 | |
| 746 | xcb_change_property(wm->conn, |
| 747 | XCB_PROP_MODE_REPLACE, |
| 748 | window->id, |
| 749 | wm->atom.net_wm_state, |
| 750 | XCB_ATOM_ATOM, |
| 751 | 32, /* format */ |
| 752 | i, property); |
| 753 | } |
| 754 | |
| 755 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 756 | weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event) |
| 757 | { |
| 758 | xcb_map_request_event_t *map_request = |
| 759 | (xcb_map_request_event_t *) event; |
| 760 | struct weston_wm_window *window; |
Kristian Høgsberg | f918719 | 2013-05-02 13:43:24 -0400 | [diff] [blame] | 761 | uint32_t values[3]; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 762 | int x, y, width, height; |
| 763 | |
| 764 | if (our_resource(wm, map_request->window)) { |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 765 | wm_log("XCB_MAP_REQUEST (window %d, ours)\n", |
| 766 | map_request->window); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 767 | return; |
| 768 | } |
| 769 | |
| 770 | window = hash_table_lookup(wm->window_hash, map_request->window); |
| 771 | |
Kristian Høgsberg | bc6e162 | 2012-05-30 09:59:56 -0400 | [diff] [blame] | 772 | if (window->frame_id) |
| 773 | return; |
| 774 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 775 | weston_wm_window_read_properties(window); |
| 776 | |
| 777 | weston_wm_window_get_frame_size(window, &width, &height); |
| 778 | weston_wm_window_get_child_position(window, &x, &y); |
| 779 | |
Kristian Høgsberg | f918719 | 2013-05-02 13:43:24 -0400 | [diff] [blame] | 780 | values[0] = wm->screen->black_pixel; |
| 781 | values[1] = |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 782 | XCB_EVENT_MASK_KEY_PRESS | |
| 783 | XCB_EVENT_MASK_KEY_RELEASE | |
| 784 | XCB_EVENT_MASK_BUTTON_PRESS | |
| 785 | XCB_EVENT_MASK_BUTTON_RELEASE | |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 786 | XCB_EVENT_MASK_POINTER_MOTION | |
| 787 | XCB_EVENT_MASK_ENTER_WINDOW | |
| 788 | XCB_EVENT_MASK_LEAVE_WINDOW | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 789 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | |
Kristian Høgsberg | 44c2013 | 2012-05-30 10:09:21 -0400 | [diff] [blame] | 790 | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT; |
Kristian Høgsberg | f918719 | 2013-05-02 13:43:24 -0400 | [diff] [blame] | 791 | values[2] = wm->colormap; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 792 | |
| 793 | window->frame_id = xcb_generate_id(wm->conn); |
| 794 | xcb_create_window(wm->conn, |
Kristian Høgsberg | f918719 | 2013-05-02 13:43:24 -0400 | [diff] [blame] | 795 | 32, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 796 | window->frame_id, |
| 797 | wm->screen->root, |
| 798 | 0, 0, |
| 799 | width, height, |
| 800 | 0, |
| 801 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
Kristian Høgsberg | f918719 | 2013-05-02 13:43:24 -0400 | [diff] [blame] | 802 | wm->visual_id, |
| 803 | XCB_CW_BORDER_PIXEL | |
| 804 | XCB_CW_EVENT_MASK | |
| 805 | XCB_CW_COLORMAP, values); |
| 806 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 807 | xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y); |
| 808 | |
| 809 | values[0] = 0; |
| 810 | xcb_configure_window(wm->conn, window->id, |
| 811 | XCB_CONFIG_WINDOW_BORDER_WIDTH, values); |
| 812 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 813 | wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n", |
| 814 | window->id, window, window->frame_id); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 815 | |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 816 | weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE); |
| 817 | weston_wm_window_set_net_wm_state(window); |
| 818 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 819 | xcb_map_window(wm->conn, map_request->window); |
| 820 | xcb_map_window(wm->conn, window->frame_id); |
| 821 | |
| 822 | window->cairo_surface = |
| 823 | cairo_xcb_surface_create_with_xrender_format(wm->conn, |
| 824 | wm->screen, |
| 825 | window->frame_id, |
Kristian Høgsberg | f918719 | 2013-05-02 13:43:24 -0400 | [diff] [blame] | 826 | &wm->format_rgba, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 827 | width, height); |
| 828 | |
| 829 | hash_table_insert(wm->window_hash, window->frame_id, window); |
| 830 | } |
| 831 | |
| 832 | static void |
| 833 | weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 834 | { |
| 835 | xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event; |
| 836 | |
| 837 | if (our_resource(wm, map_notify->window)) { |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 838 | wm_log("XCB_MAP_NOTIFY (window %d, ours)\n", |
| 839 | map_notify->window); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 840 | return; |
| 841 | } |
| 842 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 843 | wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | static void |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 847 | weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 848 | { |
| 849 | xcb_unmap_notify_event_t *unmap_notify = |
| 850 | (xcb_unmap_notify_event_t *) event; |
| 851 | struct weston_wm_window *window; |
| 852 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 853 | wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n", |
| 854 | unmap_notify->window, |
| 855 | unmap_notify->event, |
| 856 | our_resource(wm, unmap_notify->window) ? ", ours" : ""); |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 857 | |
| 858 | if (our_resource(wm, unmap_notify->window)) |
| 859 | return; |
| 860 | |
MoD | 3170012 | 2013-06-11 19:58:55 -0500 | [diff] [blame] | 861 | if (unmap_notify->response_type & SEND_EVENT_MASK) |
Kristian Høgsberg | d64bdf4 | 2012-05-31 10:33:43 -0400 | [diff] [blame] | 862 | /* We just ignore the ICCCM 4.1.4 synthetic unmap notify |
| 863 | * as it may come in after we've destroyed the window. */ |
Kristian Høgsberg | f197e9f | 2012-05-30 11:46:29 -0400 | [diff] [blame] | 864 | return; |
Kristian Høgsberg | f197e9f | 2012-05-30 11:46:29 -0400 | [diff] [blame] | 865 | |
Kristian Høgsberg | d64bdf4 | 2012-05-31 10:33:43 -0400 | [diff] [blame] | 866 | window = hash_table_lookup(wm->window_hash, unmap_notify->window); |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 867 | if (window->repaint_source) |
| 868 | wl_event_source_remove(window->repaint_source); |
| 869 | if (window->cairo_surface) |
| 870 | cairo_surface_destroy(window->cairo_surface); |
| 871 | |
Kristian Høgsberg | be375b3 | 2012-06-05 11:46:08 -0400 | [diff] [blame] | 872 | if (window->frame_id) { |
| 873 | xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0); |
| 874 | xcb_destroy_window(wm->conn, window->frame_id); |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 875 | weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE); |
Kristian Høgsberg | be375b3 | 2012-06-05 11:46:08 -0400 | [diff] [blame] | 876 | hash_table_remove(wm->window_hash, window->frame_id); |
| 877 | window->frame_id = XCB_WINDOW_NONE; |
| 878 | } |
Kristian Høgsberg | a6d9a5e | 2012-05-30 12:15:44 -0400 | [diff] [blame] | 879 | |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 880 | if (wm->focus_window == window) |
| 881 | wm->focus_window = NULL; |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 882 | if (window->surface) |
| 883 | wl_list_remove(&window->surface_destroy_listener.link); |
| 884 | window->surface = NULL; |
| 885 | } |
| 886 | |
| 887 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 888 | weston_wm_window_draw_decoration(void *data) |
| 889 | { |
| 890 | struct weston_wm_window *window = data; |
| 891 | struct weston_wm *wm = window->wm; |
| 892 | struct theme *t = wm->theme; |
| 893 | cairo_t *cr; |
| 894 | int x, y, width, height; |
| 895 | const char *title; |
| 896 | uint32_t flags = 0; |
| 897 | |
| 898 | weston_wm_window_read_properties(window); |
| 899 | |
| 900 | window->repaint_source = NULL; |
| 901 | |
| 902 | weston_wm_window_get_frame_size(window, &width, &height); |
| 903 | weston_wm_window_get_child_position(window, &x, &y); |
| 904 | |
| 905 | cairo_xcb_surface_set_size(window->cairo_surface, width, height); |
| 906 | cr = cairo_create(window->cairo_surface); |
| 907 | |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 908 | if (window->fullscreen) { |
| 909 | /* nothing */ |
| 910 | } else if (window->decorate) { |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 911 | if (wm->focus_window == window) |
| 912 | flags |= THEME_FRAME_ACTIVE; |
| 913 | |
| 914 | if (window->name) |
| 915 | title = window->name; |
| 916 | else |
| 917 | title = "untitled"; |
| 918 | |
| 919 | theme_render_frame(t, cr, width, height, title, flags); |
| 920 | } else { |
| 921 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 922 | cairo_set_source_rgba(cr, 0, 0, 0, 0); |
| 923 | cairo_paint(cr); |
| 924 | |
| 925 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 926 | cairo_set_source_rgba(cr, 0, 0, 0, 0.45); |
| 927 | tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64); |
| 928 | } |
| 929 | |
| 930 | cairo_destroy(cr); |
| 931 | |
| 932 | if (window->surface) { |
Scott Moreau | 76d8fc8 | 2012-11-22 15:35:13 -0700 | [diff] [blame] | 933 | pixman_region32_fini(&window->surface->pending.opaque); |
MoD | 384a11a | 2013-06-22 11:04:21 -0500 | [diff] [blame] | 934 | if(window->has_alpha) { |
| 935 | pixman_region32_init(&window->surface->pending.opaque); |
| 936 | } else { |
| 937 | /* We leave an extra pixel around the X window area to |
| 938 | * make sure we don't sample from the undefined alpha |
| 939 | * channel when filtering. */ |
| 940 | pixman_region32_init_rect(&window->surface->pending.opaque, |
| 941 | x - 1, y - 1, |
| 942 | window->width + 2, |
| 943 | window->height + 2); |
| 944 | } |
Pekka Paalanen | c3ce738 | 2013-03-08 14:56:49 +0200 | [diff] [blame] | 945 | weston_surface_geometry_dirty(window->surface); |
Kristian Høgsberg | d8b617d | 2013-02-14 21:56:32 -0500 | [diff] [blame] | 946 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 947 | |
Kristian Høgsberg | d8b617d | 2013-02-14 21:56:32 -0500 | [diff] [blame] | 948 | if (window->surface && !window->fullscreen) { |
Kristian Høgsberg | 81585e9 | 2013-02-14 22:01:58 -0500 | [diff] [blame] | 949 | pixman_region32_fini(&window->surface->pending.input); |
Kristian Høgsberg | d8b617d | 2013-02-14 21:56:32 -0500 | [diff] [blame] | 950 | pixman_region32_init_rect(&window->surface->pending.input, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 951 | t->margin, t->margin, |
| 952 | width - 2 * t->margin, |
| 953 | height - 2 * t->margin); |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | static void |
| 958 | weston_wm_window_schedule_repaint(struct weston_wm_window *window) |
| 959 | { |
| 960 | struct weston_wm *wm = window->wm; |
Pekka Paalanen | 4f9c07b | 2012-09-03 16:48:41 +0300 | [diff] [blame] | 961 | int width, height; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 962 | |
Kristian Høgsberg | c4063f3 | 2012-07-22 15:32:45 -0400 | [diff] [blame] | 963 | if (window->frame_id == XCB_WINDOW_NONE) { |
| 964 | if (window->surface != NULL) { |
Pekka Paalanen | 4f9c07b | 2012-09-03 16:48:41 +0300 | [diff] [blame] | 965 | weston_wm_window_get_frame_size(window, &width, &height); |
Scott Moreau | 76d8fc8 | 2012-11-22 15:35:13 -0700 | [diff] [blame] | 966 | pixman_region32_fini(&window->surface->pending.opaque); |
MoD | 384a11a | 2013-06-22 11:04:21 -0500 | [diff] [blame] | 967 | if(window->has_alpha) { |
| 968 | pixman_region32_init(&window->surface->pending.opaque); |
| 969 | } else { |
| 970 | pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0, |
| 971 | width, height); |
| 972 | } |
Pekka Paalanen | c3ce738 | 2013-03-08 14:56:49 +0200 | [diff] [blame] | 973 | weston_surface_geometry_dirty(window->surface); |
Kristian Høgsberg | c4063f3 | 2012-07-22 15:32:45 -0400 | [diff] [blame] | 974 | } |
| 975 | return; |
| 976 | } |
| 977 | |
| 978 | if (window->repaint_source) |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 979 | return; |
| 980 | |
| 981 | window->repaint_source = |
| 982 | wl_event_loop_add_idle(wm->server->loop, |
| 983 | weston_wm_window_draw_decoration, |
| 984 | window); |
| 985 | } |
| 986 | |
| 987 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 988 | weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 989 | { |
| 990 | xcb_property_notify_event_t *property_notify = |
| 991 | (xcb_property_notify_event_t *) event; |
| 992 | struct weston_wm_window *window; |
| 993 | |
| 994 | window = hash_table_lookup(wm->window_hash, property_notify->window); |
Rob Bradford | aa521bd | 2013-01-10 19:48:57 +0000 | [diff] [blame] | 995 | if (!window) |
| 996 | return; |
| 997 | |
| 998 | window->properties_dirty = 1; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 999 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1000 | wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window); |
Kristian Høgsberg | e244cb0 | 2012-05-30 11:34:35 -0400 | [diff] [blame] | 1001 | if (property_notify->state == XCB_PROPERTY_DELETE) |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1002 | wm_log("deleted\n"); |
Kristian Høgsberg | e244cb0 | 2012-05-30 11:34:35 -0400 | [diff] [blame] | 1003 | else |
| 1004 | read_and_dump_property(wm, property_notify->window, |
| 1005 | property_notify->atom); |
Kristian Høgsberg | 0273b57 | 2012-05-30 09:58:02 -0400 | [diff] [blame] | 1006 | |
| 1007 | if (property_notify->atom == wm->atom.net_wm_name || |
| 1008 | property_notify->atom == XCB_ATOM_WM_NAME) |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1009 | weston_wm_window_schedule_repaint(window); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | static void |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1013 | weston_wm_window_create(struct weston_wm *wm, |
Tiago Vignatti | 771241e | 2012-06-04 20:01:45 +0300 | [diff] [blame] | 1014 | xcb_window_t id, int width, int height, int override) |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1015 | { |
| 1016 | struct weston_wm_window *window; |
| 1017 | uint32_t values[1]; |
MoD | 384a11a | 2013-06-22 11:04:21 -0500 | [diff] [blame] | 1018 | xcb_get_geometry_cookie_t geometry_cookie; |
| 1019 | xcb_get_geometry_reply_t *geometry_reply; |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1020 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 1021 | window = zalloc(sizeof *window); |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1022 | if (window == NULL) { |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1023 | wm_log("failed to allocate window\n"); |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1024 | return; |
| 1025 | } |
| 1026 | |
MoD | 384a11a | 2013-06-22 11:04:21 -0500 | [diff] [blame] | 1027 | geometry_cookie = xcb_get_geometry(wm->conn, id); |
| 1028 | |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1029 | values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE; |
| 1030 | xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values); |
| 1031 | |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1032 | window->wm = wm; |
| 1033 | window->id = id; |
| 1034 | window->properties_dirty = 1; |
Tiago Vignatti | 771241e | 2012-06-04 20:01:45 +0300 | [diff] [blame] | 1035 | window->override_redirect = override; |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1036 | window->width = width; |
| 1037 | window->height = height; |
| 1038 | |
MoD | 384a11a | 2013-06-22 11:04:21 -0500 | [diff] [blame] | 1039 | geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL); |
| 1040 | /* technically we should use XRender and check the visual format's |
| 1041 | alpha_mask, but checking depth is simpler and works in all known cases */ |
| 1042 | if(geometry_reply != NULL) |
| 1043 | window->has_alpha = geometry_reply->depth == 32; |
| 1044 | free(geometry_reply); |
| 1045 | |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1046 | hash_table_insert(wm->window_hash, id, window); |
| 1047 | } |
| 1048 | |
| 1049 | static void |
| 1050 | weston_wm_window_destroy(struct weston_wm_window *window) |
| 1051 | { |
| 1052 | hash_table_remove(window->wm->window_hash, window->id); |
| 1053 | free(window); |
| 1054 | } |
| 1055 | |
| 1056 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1057 | weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 1058 | { |
| 1059 | xcb_create_notify_event_t *create_notify = |
| 1060 | (xcb_create_notify_event_t *) event; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1061 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1062 | wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n", |
| 1063 | create_notify->window, |
| 1064 | create_notify->width, create_notify->height, |
| 1065 | create_notify->override_redirect ? ", override" : "", |
| 1066 | our_resource(wm, create_notify->window) ? ", ours" : ""); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1067 | |
| 1068 | if (our_resource(wm, create_notify->window)) |
| 1069 | return; |
| 1070 | |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1071 | weston_wm_window_create(wm, create_notify->window, |
Tiago Vignatti | 771241e | 2012-06-04 20:01:45 +0300 | [diff] [blame] | 1072 | create_notify->width, create_notify->height, |
| 1073 | create_notify->override_redirect); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | static void |
| 1077 | weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 1078 | { |
| 1079 | xcb_destroy_notify_event_t *destroy_notify = |
| 1080 | (xcb_destroy_notify_event_t *) event; |
| 1081 | struct weston_wm_window *window; |
| 1082 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1083 | wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n", |
| 1084 | destroy_notify->window, |
| 1085 | destroy_notify->event, |
| 1086 | our_resource(wm, destroy_notify->window) ? ", ours" : ""); |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 1087 | |
| 1088 | if (our_resource(wm, destroy_notify->window)) |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1089 | return; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1090 | |
| 1091 | window = hash_table_lookup(wm->window_hash, destroy_notify->window); |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1092 | weston_wm_window_destroy(window); |
| 1093 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1094 | |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1095 | static void |
| 1096 | weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 1097 | { |
| 1098 | xcb_reparent_notify_event_t *reparent_notify = |
| 1099 | (xcb_reparent_notify_event_t *) event; |
| 1100 | struct weston_wm_window *window; |
Kristian Høgsberg | c9571fb | 2012-05-29 15:35:29 -0400 | [diff] [blame] | 1101 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1102 | wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n", |
| 1103 | reparent_notify->window, |
| 1104 | reparent_notify->parent, |
| 1105 | reparent_notify->event); |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1106 | |
| 1107 | if (reparent_notify->parent == wm->screen->root) { |
Tiago Vignatti | 771241e | 2012-06-04 20:01:45 +0300 | [diff] [blame] | 1108 | weston_wm_window_create(wm, reparent_notify->window, 10, 10, |
| 1109 | reparent_notify->override_redirect); |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1110 | } else if (!our_resource(wm, reparent_notify->parent)) { |
| 1111 | window = hash_table_lookup(wm->window_hash, |
| 1112 | reparent_notify->window); |
| 1113 | weston_wm_window_destroy(window); |
| 1114 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1115 | } |
| 1116 | |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 1117 | struct weston_seat * |
| 1118 | weston_wm_pick_seat(struct weston_wm *wm) |
| 1119 | { |
| 1120 | return container_of(wm->server->compositor->seat_list.next, |
| 1121 | struct weston_seat, link); |
| 1122 | } |
| 1123 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1124 | static void |
Kristian Høgsberg | e68fd10 | 2012-05-22 17:09:40 -0400 | [diff] [blame] | 1125 | weston_wm_window_handle_moveresize(struct weston_wm_window *window, |
| 1126 | xcb_client_message_event_t *client_message) |
| 1127 | { |
| 1128 | static const int map[] = { |
| 1129 | THEME_LOCATION_RESIZING_TOP_LEFT, |
| 1130 | THEME_LOCATION_RESIZING_TOP, |
| 1131 | THEME_LOCATION_RESIZING_TOP_RIGHT, |
| 1132 | THEME_LOCATION_RESIZING_RIGHT, |
| 1133 | THEME_LOCATION_RESIZING_BOTTOM_RIGHT, |
| 1134 | THEME_LOCATION_RESIZING_BOTTOM, |
| 1135 | THEME_LOCATION_RESIZING_BOTTOM_LEFT, |
| 1136 | THEME_LOCATION_RESIZING_LEFT |
| 1137 | }; |
| 1138 | |
| 1139 | struct weston_wm *wm = window->wm; |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 1140 | struct weston_seat *seat = weston_wm_pick_seat(wm); |
Kristian Høgsberg | e68fd10 | 2012-05-22 17:09:40 -0400 | [diff] [blame] | 1141 | int detail; |
| 1142 | struct weston_shell_interface *shell_interface = |
| 1143 | &wm->server->compositor->shell_interface; |
| 1144 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1145 | if (seat->pointer->button_count != 1 || |
Kristian Høgsberg | fe7aa90 | 2013-05-08 09:54:37 -0400 | [diff] [blame] | 1146 | seat->pointer->focus != window->surface) |
Kristian Høgsberg | e68fd10 | 2012-05-22 17:09:40 -0400 | [diff] [blame] | 1147 | return; |
| 1148 | |
| 1149 | detail = client_message->data.data32[2]; |
| 1150 | switch (detail) { |
| 1151 | case _NET_WM_MOVERESIZE_MOVE: |
| 1152 | shell_interface->move(window->shsurf, seat); |
| 1153 | break; |
| 1154 | case _NET_WM_MOVERESIZE_SIZE_TOPLEFT: |
| 1155 | case _NET_WM_MOVERESIZE_SIZE_TOP: |
| 1156 | case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT: |
| 1157 | case _NET_WM_MOVERESIZE_SIZE_RIGHT: |
| 1158 | case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT: |
| 1159 | case _NET_WM_MOVERESIZE_SIZE_BOTTOM: |
| 1160 | case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT: |
| 1161 | case _NET_WM_MOVERESIZE_SIZE_LEFT: |
| 1162 | shell_interface->resize(window->shsurf, seat, map[detail]); |
| 1163 | break; |
| 1164 | case _NET_WM_MOVERESIZE_CANCEL: |
| 1165 | break; |
| 1166 | } |
| 1167 | } |
| 1168 | |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1169 | #define _NET_WM_STATE_REMOVE 0 |
| 1170 | #define _NET_WM_STATE_ADD 1 |
| 1171 | #define _NET_WM_STATE_TOGGLE 2 |
| 1172 | |
| 1173 | static int |
| 1174 | update_state(int action, int *state) |
| 1175 | { |
| 1176 | int new_state, changed; |
| 1177 | |
| 1178 | switch (action) { |
| 1179 | case _NET_WM_STATE_REMOVE: |
| 1180 | new_state = 0; |
| 1181 | break; |
| 1182 | case _NET_WM_STATE_ADD: |
| 1183 | new_state = 1; |
| 1184 | break; |
| 1185 | case _NET_WM_STATE_TOGGLE: |
| 1186 | new_state = !*state; |
| 1187 | break; |
| 1188 | default: |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
| 1192 | changed = (*state != new_state); |
| 1193 | *state = new_state; |
| 1194 | |
| 1195 | return changed; |
| 1196 | } |
| 1197 | |
| 1198 | static void |
| 1199 | weston_wm_window_configure(void *data); |
| 1200 | |
| 1201 | static void |
| 1202 | weston_wm_window_handle_state(struct weston_wm_window *window, |
| 1203 | xcb_client_message_event_t *client_message) |
| 1204 | { |
| 1205 | struct weston_wm *wm = window->wm; |
| 1206 | struct weston_shell_interface *shell_interface = |
| 1207 | &wm->server->compositor->shell_interface; |
| 1208 | uint32_t action, property; |
| 1209 | |
| 1210 | action = client_message->data.data32[0]; |
| 1211 | property = client_message->data.data32[1]; |
| 1212 | |
| 1213 | if (property == wm->atom.net_wm_state_fullscreen && |
| 1214 | update_state(action, &window->fullscreen)) { |
| 1215 | weston_wm_window_set_net_wm_state(window); |
| 1216 | if (window->fullscreen) { |
| 1217 | window->saved_width = window->width; |
| 1218 | window->saved_height = window->height; |
Kristian Høgsberg | 762b166 | 2013-02-21 20:18:37 -0500 | [diff] [blame] | 1219 | |
| 1220 | if (window->shsurf) |
| 1221 | shell_interface->set_fullscreen(window->shsurf, |
| 1222 | WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, |
| 1223 | 0, NULL); |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1224 | } else { |
| 1225 | shell_interface->set_toplevel(window->shsurf); |
| 1226 | window->width = window->saved_width; |
| 1227 | window->height = window->saved_height; |
| 1228 | weston_wm_window_configure(window); |
| 1229 | } |
| 1230 | } |
| 1231 | } |
| 1232 | |
Kristian Høgsberg | e68fd10 | 2012-05-22 17:09:40 -0400 | [diff] [blame] | 1233 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1234 | weston_wm_handle_client_message(struct weston_wm *wm, |
| 1235 | xcb_generic_event_t *event) |
| 1236 | { |
| 1237 | xcb_client_message_event_t *client_message = |
| 1238 | (xcb_client_message_event_t *) event; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1239 | struct weston_wm_window *window; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1240 | |
| 1241 | window = hash_table_lookup(wm->window_hash, client_message->window); |
| 1242 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1243 | wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n", |
| 1244 | get_atom_name(wm->conn, client_message->type), |
| 1245 | client_message->data.data32[0], |
| 1246 | client_message->data.data32[1], |
| 1247 | client_message->data.data32[2], |
| 1248 | client_message->data.data32[3], |
| 1249 | client_message->data.data32[4], |
| 1250 | client_message->window); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1251 | |
Kristian Høgsberg | e68fd10 | 2012-05-22 17:09:40 -0400 | [diff] [blame] | 1252 | if (client_message->type == wm->atom.net_wm_moveresize) |
| 1253 | weston_wm_window_handle_moveresize(window, client_message); |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1254 | else if (client_message->type == wm->atom.net_wm_state) |
| 1255 | weston_wm_window_handle_state(window, client_message); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1256 | } |
| 1257 | |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1258 | enum cursor_type { |
| 1259 | XWM_CURSOR_TOP, |
| 1260 | XWM_CURSOR_BOTTOM, |
| 1261 | XWM_CURSOR_LEFT, |
| 1262 | XWM_CURSOR_RIGHT, |
| 1263 | XWM_CURSOR_TOP_LEFT, |
| 1264 | XWM_CURSOR_TOP_RIGHT, |
| 1265 | XWM_CURSOR_BOTTOM_LEFT, |
| 1266 | XWM_CURSOR_BOTTOM_RIGHT, |
| 1267 | XWM_CURSOR_LEFT_PTR, |
| 1268 | }; |
| 1269 | |
| 1270 | static const char *cursors[] = { |
| 1271 | "top_side", |
| 1272 | "bottom_side", |
| 1273 | "left_side", |
| 1274 | "right_side", |
| 1275 | "top_left_corner", |
| 1276 | "top_right_corner", |
| 1277 | "bottom_left_corner", |
| 1278 | "bottom_right_corner", |
| 1279 | "left_ptr" |
| 1280 | }; |
| 1281 | |
| 1282 | static void |
| 1283 | weston_wm_create_cursors(struct weston_wm *wm) |
| 1284 | { |
| 1285 | int i, count = ARRAY_LENGTH(cursors); |
| 1286 | |
| 1287 | wm->cursors = malloc(count * sizeof(xcb_cursor_t)); |
| 1288 | for (i = 0; i < count; i++) { |
| 1289 | wm->cursors[i] = |
| 1290 | xcb_cursor_library_load_cursor(wm, cursors[i]); |
| 1291 | } |
| 1292 | |
| 1293 | wm->last_cursor = -1; |
| 1294 | } |
| 1295 | |
| 1296 | static void |
| 1297 | weston_wm_destroy_cursors(struct weston_wm *wm) |
| 1298 | { |
| 1299 | uint8_t i; |
| 1300 | |
| 1301 | for (i = 0; i < ARRAY_LENGTH(cursors); i++) |
| 1302 | xcb_free_cursor(wm->conn, wm->cursors[i]); |
| 1303 | |
| 1304 | free(wm->cursors); |
| 1305 | } |
| 1306 | |
| 1307 | static int |
| 1308 | get_cursor_for_location(struct theme *t, int width, int height, int x, int y) |
| 1309 | { |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 1310 | int location = theme_get_location(t, x, y, width, height, 0); |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1311 | |
| 1312 | switch (location) { |
| 1313 | case THEME_LOCATION_RESIZING_TOP: |
| 1314 | return XWM_CURSOR_TOP; |
| 1315 | case THEME_LOCATION_RESIZING_BOTTOM: |
| 1316 | return XWM_CURSOR_BOTTOM; |
| 1317 | case THEME_LOCATION_RESIZING_LEFT: |
| 1318 | return XWM_CURSOR_LEFT; |
| 1319 | case THEME_LOCATION_RESIZING_RIGHT: |
| 1320 | return XWM_CURSOR_RIGHT; |
| 1321 | case THEME_LOCATION_RESIZING_TOP_LEFT: |
| 1322 | return XWM_CURSOR_TOP_LEFT; |
| 1323 | case THEME_LOCATION_RESIZING_TOP_RIGHT: |
| 1324 | return XWM_CURSOR_TOP_RIGHT; |
| 1325 | case THEME_LOCATION_RESIZING_BOTTOM_LEFT: |
| 1326 | return XWM_CURSOR_BOTTOM_LEFT; |
| 1327 | case THEME_LOCATION_RESIZING_BOTTOM_RIGHT: |
| 1328 | return XWM_CURSOR_BOTTOM_RIGHT; |
| 1329 | case THEME_LOCATION_EXTERIOR: |
| 1330 | case THEME_LOCATION_TITLEBAR: |
| 1331 | default: |
| 1332 | return XWM_CURSOR_LEFT_PTR; |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | static void |
Tiago Vignatti | c190323 | 2012-07-16 12:15:37 -0400 | [diff] [blame] | 1337 | weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id, |
| 1338 | int cursor) |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1339 | { |
| 1340 | uint32_t cursor_value_list; |
| 1341 | |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1342 | if (wm->last_cursor == cursor) |
| 1343 | return; |
| 1344 | |
| 1345 | wm->last_cursor = cursor; |
| 1346 | |
| 1347 | cursor_value_list = wm->cursors[cursor]; |
Tiago Vignatti | c190323 | 2012-07-16 12:15:37 -0400 | [diff] [blame] | 1348 | xcb_change_window_attributes (wm->conn, window_id, |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1349 | XCB_CW_CURSOR, &cursor_value_list); |
| 1350 | xcb_flush(wm->conn); |
| 1351 | } |
| 1352 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1353 | static void |
| 1354 | weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event) |
| 1355 | { |
| 1356 | xcb_button_press_event_t *button = (xcb_button_press_event_t *) event; |
| 1357 | struct weston_shell_interface *shell_interface = |
| 1358 | &wm->server->compositor->shell_interface; |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 1359 | struct weston_seat *seat = weston_wm_pick_seat(wm); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1360 | struct weston_wm_window *window; |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 1361 | enum theme_location location; |
| 1362 | struct theme *t = wm->theme; |
Kristian Høgsberg | c1693f2 | 2012-05-22 16:56:23 -0400 | [diff] [blame] | 1363 | int width, height; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1364 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1365 | wm_log("XCB_BUTTON_%s (detail %d)\n", |
| 1366 | button->response_type == XCB_BUTTON_PRESS ? |
| 1367 | "PRESS" : "RELEASE", button->detail); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1368 | |
| 1369 | window = hash_table_lookup(wm->window_hash, button->event); |
Kristian Høgsberg | c1693f2 | 2012-05-22 16:56:23 -0400 | [diff] [blame] | 1370 | weston_wm_window_get_frame_size(window, &width, &height); |
| 1371 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1372 | if (button->response_type == XCB_BUTTON_PRESS && |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 1373 | button->detail == 1) { |
| 1374 | location = theme_get_location(t, |
| 1375 | button->event_x, |
| 1376 | button->event_y, |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 1377 | width, height, 0); |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 1378 | |
| 1379 | switch (location) { |
| 1380 | case THEME_LOCATION_TITLEBAR: |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 1381 | shell_interface->move(window->shsurf, seat); |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 1382 | break; |
Kristian Høgsberg | c1693f2 | 2012-05-22 16:56:23 -0400 | [diff] [blame] | 1383 | case THEME_LOCATION_RESIZING_TOP: |
| 1384 | case THEME_LOCATION_RESIZING_BOTTOM: |
| 1385 | case THEME_LOCATION_RESIZING_LEFT: |
| 1386 | case THEME_LOCATION_RESIZING_RIGHT: |
| 1387 | case THEME_LOCATION_RESIZING_TOP_LEFT: |
| 1388 | case THEME_LOCATION_RESIZING_TOP_RIGHT: |
| 1389 | case THEME_LOCATION_RESIZING_BOTTOM_LEFT: |
| 1390 | case THEME_LOCATION_RESIZING_BOTTOM_RIGHT: |
| 1391 | shell_interface->resize(window->shsurf, |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 1392 | seat, location); |
Kristian Høgsberg | c1693f2 | 2012-05-22 16:56:23 -0400 | [diff] [blame] | 1393 | break; |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 1394 | default: |
| 1395 | break; |
| 1396 | } |
| 1397 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1398 | } |
| 1399 | |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1400 | static void |
| 1401 | weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event) |
| 1402 | { |
| 1403 | xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event; |
| 1404 | struct weston_wm_window *window; |
| 1405 | int cursor, width, height; |
| 1406 | |
| 1407 | window = hash_table_lookup(wm->window_hash, motion->event); |
Tiago Vignatti | 9134b77 | 2012-07-20 19:41:12 +0300 | [diff] [blame] | 1408 | if (!window || !window->decorate) |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1409 | return; |
| 1410 | |
| 1411 | weston_wm_window_get_frame_size(window, &width, &height); |
| 1412 | cursor = get_cursor_for_location(wm->theme, width, height, |
| 1413 | motion->event_x, motion->event_y); |
| 1414 | |
Tiago Vignatti | c190323 | 2012-07-16 12:15:37 -0400 | [diff] [blame] | 1415 | weston_wm_window_set_cursor(wm, window->frame_id, cursor); |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | static void |
| 1419 | weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event) |
| 1420 | { |
| 1421 | xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event; |
| 1422 | struct weston_wm_window *window; |
| 1423 | int cursor, width, height; |
| 1424 | |
| 1425 | window = hash_table_lookup(wm->window_hash, enter->event); |
Tiago Vignatti | 9134b77 | 2012-07-20 19:41:12 +0300 | [diff] [blame] | 1426 | if (!window || !window->decorate) |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1427 | return; |
| 1428 | |
| 1429 | weston_wm_window_get_frame_size(window, &width, &height); |
| 1430 | cursor = get_cursor_for_location(wm->theme, width, height, |
| 1431 | enter->event_x, enter->event_y); |
| 1432 | |
Tiago Vignatti | c190323 | 2012-07-16 12:15:37 -0400 | [diff] [blame] | 1433 | weston_wm_window_set_cursor(wm, window->frame_id, cursor); |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | static void |
| 1437 | weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event) |
| 1438 | { |
| 1439 | xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event; |
| 1440 | struct weston_wm_window *window; |
| 1441 | |
| 1442 | window = hash_table_lookup(wm->window_hash, leave->event); |
Tiago Vignatti | 9134b77 | 2012-07-20 19:41:12 +0300 | [diff] [blame] | 1443 | if (!window || !window->decorate) |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1444 | return; |
| 1445 | |
Tiago Vignatti | c190323 | 2012-07-16 12:15:37 -0400 | [diff] [blame] | 1446 | weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR); |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1447 | } |
| 1448 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1449 | static int |
| 1450 | weston_wm_handle_event(int fd, uint32_t mask, void *data) |
| 1451 | { |
| 1452 | struct weston_wm *wm = data; |
| 1453 | xcb_generic_event_t *event; |
| 1454 | int count = 0; |
| 1455 | |
| 1456 | while (event = xcb_poll_for_event(wm->conn), event != NULL) { |
| 1457 | if (weston_wm_handle_selection_event(wm, event)) { |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1458 | free(event); |
| 1459 | count++; |
| 1460 | continue; |
| 1461 | } |
| 1462 | |
MoD | 3170012 | 2013-06-11 19:58:55 -0500 | [diff] [blame] | 1463 | switch (EVENT_TYPE(event)) { |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1464 | case XCB_BUTTON_PRESS: |
| 1465 | case XCB_BUTTON_RELEASE: |
| 1466 | weston_wm_handle_button(wm, event); |
| 1467 | break; |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1468 | case XCB_ENTER_NOTIFY: |
| 1469 | weston_wm_handle_enter(wm, event); |
| 1470 | break; |
| 1471 | case XCB_LEAVE_NOTIFY: |
| 1472 | weston_wm_handle_leave(wm, event); |
| 1473 | break; |
| 1474 | case XCB_MOTION_NOTIFY: |
| 1475 | weston_wm_handle_motion(wm, event); |
| 1476 | break; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1477 | case XCB_CREATE_NOTIFY: |
| 1478 | weston_wm_handle_create_notify(wm, event); |
| 1479 | break; |
| 1480 | case XCB_MAP_REQUEST: |
| 1481 | weston_wm_handle_map_request(wm, event); |
| 1482 | break; |
| 1483 | case XCB_MAP_NOTIFY: |
| 1484 | weston_wm_handle_map_notify(wm, event); |
| 1485 | break; |
| 1486 | case XCB_UNMAP_NOTIFY: |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 1487 | weston_wm_handle_unmap_notify(wm, event); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1488 | break; |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1489 | case XCB_REPARENT_NOTIFY: |
| 1490 | weston_wm_handle_reparent_notify(wm, event); |
| 1491 | break; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1492 | case XCB_CONFIGURE_REQUEST: |
| 1493 | weston_wm_handle_configure_request(wm, event); |
| 1494 | break; |
| 1495 | case XCB_CONFIGURE_NOTIFY: |
| 1496 | weston_wm_handle_configure_notify(wm, event); |
| 1497 | break; |
| 1498 | case XCB_DESTROY_NOTIFY: |
| 1499 | weston_wm_handle_destroy_notify(wm, event); |
| 1500 | break; |
| 1501 | case XCB_MAPPING_NOTIFY: |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1502 | wm_log("XCB_MAPPING_NOTIFY\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1503 | break; |
| 1504 | case XCB_PROPERTY_NOTIFY: |
| 1505 | weston_wm_handle_property_notify(wm, event); |
| 1506 | break; |
| 1507 | case XCB_CLIENT_MESSAGE: |
| 1508 | weston_wm_handle_client_message(wm, event); |
| 1509 | break; |
| 1510 | } |
| 1511 | |
| 1512 | free(event); |
| 1513 | count++; |
| 1514 | } |
| 1515 | |
| 1516 | xcb_flush(wm->conn); |
| 1517 | |
| 1518 | return count; |
| 1519 | } |
| 1520 | |
| 1521 | static void |
Kristian Høgsberg | f918719 | 2013-05-02 13:43:24 -0400 | [diff] [blame] | 1522 | weston_wm_get_visual_and_colormap(struct weston_wm *wm) |
| 1523 | { |
| 1524 | xcb_depth_iterator_t d_iter; |
| 1525 | xcb_visualtype_iterator_t vt_iter; |
| 1526 | xcb_visualtype_t *visualtype; |
| 1527 | |
| 1528 | d_iter = xcb_screen_allowed_depths_iterator(wm->screen); |
| 1529 | visualtype = NULL; |
| 1530 | while (d_iter.rem > 0) { |
| 1531 | if (d_iter.data->depth == 32) { |
| 1532 | vt_iter = xcb_depth_visuals_iterator(d_iter.data); |
| 1533 | visualtype = vt_iter.data; |
| 1534 | break; |
| 1535 | } |
| 1536 | |
| 1537 | xcb_depth_next(&d_iter); |
| 1538 | } |
| 1539 | |
| 1540 | if (visualtype == NULL) { |
| 1541 | weston_log("no 32 bit visualtype\n"); |
| 1542 | return; |
| 1543 | } |
| 1544 | |
| 1545 | wm->visual_id = visualtype->visual_id; |
| 1546 | wm->colormap = xcb_generate_id(wm->conn); |
| 1547 | xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE, |
| 1548 | wm->colormap, wm->screen->root, wm->visual_id); |
| 1549 | } |
| 1550 | |
| 1551 | static void |
Tiago Vignatti | 9c4ff83 | 2012-11-30 17:19:57 -0200 | [diff] [blame] | 1552 | weston_wm_get_resources(struct weston_wm *wm) |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1553 | { |
| 1554 | |
| 1555 | #define F(field) offsetof(struct weston_wm, field) |
| 1556 | |
| 1557 | static const struct { const char *name; int offset; } atoms[] = { |
| 1558 | { "WM_PROTOCOLS", F(atom.wm_protocols) }, |
Kristian Høgsberg | 1a7a57f | 2013-08-31 00:12:25 -0700 | [diff] [blame] | 1559 | { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) }, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1560 | { "WM_TAKE_FOCUS", F(atom.wm_take_focus) }, |
| 1561 | { "WM_DELETE_WINDOW", F(atom.wm_delete_window) }, |
Kristian Høgsberg | a6d9a5e | 2012-05-30 12:15:44 -0400 | [diff] [blame] | 1562 | { "WM_STATE", F(atom.wm_state) }, |
Kristian Høgsberg | 670b5d3 | 2012-06-04 11:00:40 -0400 | [diff] [blame] | 1563 | { "WM_S0", F(atom.wm_s0) }, |
Tiago Vignatti | 0d20d7c | 2012-09-27 17:48:37 +0300 | [diff] [blame] | 1564 | { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) }, |
Kristian Høgsberg | 69981d9 | 2013-08-21 22:14:58 -0700 | [diff] [blame] | 1565 | { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) }, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1566 | { "_NET_WM_NAME", F(atom.net_wm_name) }, |
Tiago Vignatti | 0d20d7c | 2012-09-27 17:48:37 +0300 | [diff] [blame] | 1567 | { "_NET_WM_PID", F(atom.net_wm_pid) }, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1568 | { "_NET_WM_ICON", F(atom.net_wm_icon) }, |
| 1569 | { "_NET_WM_STATE", F(atom.net_wm_state) }, |
| 1570 | { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) }, |
| 1571 | { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) }, |
| 1572 | { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) }, |
| 1573 | { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) }, |
| 1574 | |
| 1575 | { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) }, |
| 1576 | { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) }, |
| 1577 | { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) }, |
| 1578 | { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) }, |
| 1579 | { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) }, |
| 1580 | { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) }, |
| 1581 | { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) }, |
Tiago Vignatti | bf1e866 | 2012-06-12 14:07:49 +0300 | [diff] [blame] | 1582 | { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) }, |
| 1583 | { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) }, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1584 | { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) }, |
| 1585 | { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) }, |
| 1586 | { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) }, |
| 1587 | { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) }, |
| 1588 | { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) }, |
| 1589 | |
| 1590 | { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) }, |
| 1591 | { "_NET_SUPPORTING_WM_CHECK", |
| 1592 | F(atom.net_supporting_wm_check) }, |
| 1593 | { "_NET_SUPPORTED", F(atom.net_supported) }, |
| 1594 | { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) }, |
| 1595 | { "CLIPBOARD", F(atom.clipboard) }, |
Kristian Høgsberg | cba022a | 2012-06-04 10:11:45 -0400 | [diff] [blame] | 1596 | { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) }, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1597 | { "TARGETS", F(atom.targets) }, |
| 1598 | { "UTF8_STRING", F(atom.utf8_string) }, |
| 1599 | { "_WL_SELECTION", F(atom.wl_selection) }, |
| 1600 | { "INCR", F(atom.incr) }, |
| 1601 | { "TIMESTAMP", F(atom.timestamp) }, |
| 1602 | { "MULTIPLE", F(atom.multiple) }, |
| 1603 | { "UTF8_STRING" , F(atom.utf8_string) }, |
| 1604 | { "COMPOUND_TEXT", F(atom.compound_text) }, |
| 1605 | { "TEXT", F(atom.text) }, |
| 1606 | { "STRING", F(atom.string) }, |
| 1607 | { "text/plain;charset=utf-8", F(atom.text_plain_utf8) }, |
| 1608 | { "text/plain", F(atom.text_plain) }, |
| 1609 | }; |
| 1610 | #undef F |
| 1611 | |
| 1612 | xcb_xfixes_query_version_cookie_t xfixes_cookie; |
| 1613 | xcb_xfixes_query_version_reply_t *xfixes_reply; |
| 1614 | xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)]; |
| 1615 | xcb_intern_atom_reply_t *reply; |
| 1616 | xcb_render_query_pict_formats_reply_t *formats_reply; |
| 1617 | xcb_render_query_pict_formats_cookie_t formats_cookie; |
| 1618 | xcb_render_pictforminfo_t *formats; |
| 1619 | uint32_t i; |
| 1620 | |
| 1621 | xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id); |
| 1622 | |
| 1623 | formats_cookie = xcb_render_query_pict_formats(wm->conn); |
| 1624 | |
| 1625 | for (i = 0; i < ARRAY_LENGTH(atoms); i++) |
| 1626 | cookies[i] = xcb_intern_atom (wm->conn, 0, |
| 1627 | strlen(atoms[i].name), |
| 1628 | atoms[i].name); |
| 1629 | |
| 1630 | for (i = 0; i < ARRAY_LENGTH(atoms); i++) { |
| 1631 | reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL); |
| 1632 | *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom; |
| 1633 | free(reply); |
| 1634 | } |
| 1635 | |
| 1636 | wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id); |
| 1637 | if (!wm->xfixes || !wm->xfixes->present) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1638 | weston_log("xfixes not available\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1639 | |
| 1640 | xfixes_cookie = xcb_xfixes_query_version(wm->conn, |
| 1641 | XCB_XFIXES_MAJOR_VERSION, |
| 1642 | XCB_XFIXES_MINOR_VERSION); |
| 1643 | xfixes_reply = xcb_xfixes_query_version_reply(wm->conn, |
| 1644 | xfixes_cookie, NULL); |
| 1645 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1646 | weston_log("xfixes version: %d.%d\n", |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1647 | xfixes_reply->major_version, xfixes_reply->minor_version); |
| 1648 | |
| 1649 | free(xfixes_reply); |
| 1650 | |
| 1651 | formats_reply = xcb_render_query_pict_formats_reply(wm->conn, |
| 1652 | formats_cookie, 0); |
| 1653 | if (formats_reply == NULL) |
| 1654 | return; |
| 1655 | |
| 1656 | formats = xcb_render_query_pict_formats_formats(formats_reply); |
Kristian Høgsberg | e89cef3 | 2012-07-16 11:57:08 -0400 | [diff] [blame] | 1657 | for (i = 0; i < formats_reply->num_formats; i++) { |
| 1658 | if (formats[i].direct.red_mask != 0xff && |
| 1659 | formats[i].direct.red_shift != 16) |
| 1660 | continue; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1661 | if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT && |
| 1662 | formats[i].depth == 24) |
Kristian Høgsberg | e89cef3 | 2012-07-16 11:57:08 -0400 | [diff] [blame] | 1663 | wm->format_rgb = formats[i]; |
| 1664 | if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT && |
| 1665 | formats[i].depth == 32 && |
| 1666 | formats[i].direct.alpha_mask == 0xff && |
| 1667 | formats[i].direct.alpha_shift == 24) |
| 1668 | wm->format_rgba = formats[i]; |
| 1669 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1670 | |
| 1671 | free(formats_reply); |
| 1672 | } |
| 1673 | |
| 1674 | static void |
| 1675 | weston_wm_create_wm_window(struct weston_wm *wm) |
| 1676 | { |
| 1677 | static const char name[] = "Weston WM"; |
| 1678 | |
| 1679 | wm->wm_window = xcb_generate_id(wm->conn); |
| 1680 | xcb_create_window(wm->conn, |
| 1681 | XCB_COPY_FROM_PARENT, |
| 1682 | wm->wm_window, |
| 1683 | wm->screen->root, |
| 1684 | 0, 0, |
| 1685 | 10, 10, |
| 1686 | 0, |
| 1687 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 1688 | wm->screen->root_visual, |
| 1689 | 0, NULL); |
| 1690 | |
| 1691 | xcb_change_property(wm->conn, |
| 1692 | XCB_PROP_MODE_REPLACE, |
| 1693 | wm->wm_window, |
| 1694 | wm->atom.net_supporting_wm_check, |
| 1695 | XCB_ATOM_WINDOW, |
| 1696 | 32, /* format */ |
| 1697 | 1, &wm->wm_window); |
| 1698 | |
| 1699 | xcb_change_property(wm->conn, |
| 1700 | XCB_PROP_MODE_REPLACE, |
| 1701 | wm->wm_window, |
| 1702 | wm->atom.net_wm_name, |
| 1703 | wm->atom.utf8_string, |
| 1704 | 8, /* format */ |
| 1705 | strlen(name), name); |
| 1706 | |
| 1707 | xcb_change_property(wm->conn, |
| 1708 | XCB_PROP_MODE_REPLACE, |
| 1709 | wm->screen->root, |
| 1710 | wm->atom.net_supporting_wm_check, |
| 1711 | XCB_ATOM_WINDOW, |
| 1712 | 32, /* format */ |
| 1713 | 1, &wm->wm_window); |
| 1714 | |
Kristian Høgsberg | 670b5d3 | 2012-06-04 11:00:40 -0400 | [diff] [blame] | 1715 | /* Claim the WM_S0 selection even though we don't suport |
| 1716 | * the --replace functionality. */ |
| 1717 | xcb_set_selection_owner(wm->conn, |
| 1718 | wm->wm_window, |
| 1719 | wm->atom.wm_s0, |
| 1720 | XCB_TIME_CURRENT_TIME); |
Kristian Høgsberg | 69981d9 | 2013-08-21 22:14:58 -0700 | [diff] [blame] | 1721 | |
| 1722 | xcb_set_selection_owner(wm->conn, |
| 1723 | wm->wm_window, |
| 1724 | wm->atom.net_wm_cm_s0, |
| 1725 | XCB_TIME_CURRENT_TIME); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | struct weston_wm * |
| 1729 | weston_wm_create(struct weston_xserver *wxs) |
| 1730 | { |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1731 | struct weston_wm *wm; |
| 1732 | struct wl_event_loop *loop; |
| 1733 | xcb_screen_iterator_t s; |
Kristian Høgsberg | 4dec011 | 2012-06-03 09:18:06 -0400 | [diff] [blame] | 1734 | uint32_t values[1]; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1735 | int sv[2]; |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1736 | xcb_atom_t supported[3]; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1737 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 1738 | wm = zalloc(sizeof *wm); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1739 | if (wm == NULL) |
| 1740 | return NULL; |
| 1741 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1742 | wm->server = wxs; |
| 1743 | wm->window_hash = hash_table_create(); |
| 1744 | if (wm->window_hash == NULL) { |
| 1745 | free(wm); |
| 1746 | return NULL; |
| 1747 | } |
| 1748 | |
| 1749 | if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1750 | weston_log("socketpair failed\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1751 | hash_table_destroy(wm->window_hash); |
| 1752 | free(wm); |
| 1753 | return NULL; |
| 1754 | } |
| 1755 | |
| 1756 | xserver_send_client(wxs->resource, sv[1]); |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 1757 | wl_client_flush(wl_resource_get_client(wxs->resource)); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1758 | close(sv[1]); |
| 1759 | |
| 1760 | /* xcb_connect_to_fd takes ownership of the fd. */ |
| 1761 | wm->conn = xcb_connect_to_fd(sv[0], NULL); |
| 1762 | if (xcb_connection_has_error(wm->conn)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1763 | weston_log("xcb_connect_to_fd failed\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1764 | close(sv[0]); |
| 1765 | hash_table_destroy(wm->window_hash); |
| 1766 | free(wm); |
| 1767 | return NULL; |
| 1768 | } |
| 1769 | |
| 1770 | s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn)); |
| 1771 | wm->screen = s.data; |
| 1772 | |
| 1773 | loop = wl_display_get_event_loop(wxs->wl_display); |
| 1774 | wm->source = |
| 1775 | wl_event_loop_add_fd(loop, sv[0], |
| 1776 | WL_EVENT_READABLE, |
| 1777 | weston_wm_handle_event, wm); |
| 1778 | wl_event_source_check(wm->source); |
| 1779 | |
Tiago Vignatti | 9c4ff83 | 2012-11-30 17:19:57 -0200 | [diff] [blame] | 1780 | weston_wm_get_resources(wm); |
Kristian Høgsberg | f918719 | 2013-05-02 13:43:24 -0400 | [diff] [blame] | 1781 | weston_wm_get_visual_and_colormap(wm); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1782 | |
| 1783 | values[0] = |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1784 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | |
| 1785 | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | |
| 1786 | XCB_EVENT_MASK_PROPERTY_CHANGE; |
| 1787 | xcb_change_window_attributes(wm->conn, wm->screen->root, |
| 1788 | XCB_CW_EVENT_MASK, values); |
| 1789 | wm->theme = theme_create(); |
| 1790 | |
| 1791 | weston_wm_create_wm_window(wm); |
| 1792 | |
| 1793 | supported[0] = wm->atom.net_wm_moveresize; |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1794 | supported[1] = wm->atom.net_wm_state; |
| 1795 | supported[2] = wm->atom.net_wm_state_fullscreen; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1796 | xcb_change_property(wm->conn, |
| 1797 | XCB_PROP_MODE_REPLACE, |
| 1798 | wm->screen->root, |
| 1799 | wm->atom.net_supported, |
| 1800 | XCB_ATOM_ATOM, |
| 1801 | 32, /* format */ |
| 1802 | ARRAY_LENGTH(supported), supported); |
| 1803 | |
Kristian Høgsberg | 4dec011 | 2012-06-03 09:18:06 -0400 | [diff] [blame] | 1804 | weston_wm_selection_init(wm); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1805 | |
| 1806 | xcb_flush(wm->conn); |
| 1807 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1808 | wm->activate_listener.notify = weston_wm_window_activate; |
| 1809 | wl_signal_add(&wxs->compositor->activate_signal, |
| 1810 | &wm->activate_listener); |
Tiago Vignatti | fb2adba | 2013-06-12 15:43:21 -0300 | [diff] [blame] | 1811 | wm->transform_listener.notify = weston_wm_window_transform; |
| 1812 | wl_signal_add(&wxs->compositor->transform_signal, |
| 1813 | &wm->transform_listener); |
Tiago Vignatti | 0d20d7c | 2012-09-27 17:48:37 +0300 | [diff] [blame] | 1814 | wm->kill_listener.notify = weston_wm_kill_client; |
| 1815 | wl_signal_add(&wxs->compositor->kill_signal, |
| 1816 | &wm->kill_listener); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1817 | |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1818 | weston_wm_create_cursors(wm); |
Tiago Vignatti | c190323 | 2012-07-16 12:15:37 -0400 | [diff] [blame] | 1819 | weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR); |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1820 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1821 | weston_log("created wm\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1822 | |
| 1823 | return wm; |
| 1824 | } |
| 1825 | |
| 1826 | void |
| 1827 | weston_wm_destroy(struct weston_wm *wm) |
| 1828 | { |
| 1829 | /* FIXME: Free windows in hash. */ |
| 1830 | hash_table_destroy(wm->window_hash); |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1831 | weston_wm_destroy_cursors(wm); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1832 | xcb_disconnect(wm->conn); |
| 1833 | wl_event_source_remove(wm->source); |
| 1834 | wl_list_remove(&wm->selection_listener.link); |
| 1835 | wl_list_remove(&wm->activate_listener.link); |
Tiago Vignatti | 0d20d7c | 2012-09-27 17:48:37 +0300 | [diff] [blame] | 1836 | wl_list_remove(&wm->kill_listener.link); |
Louis-Francis Ratté-Boulianne | dce3dac | 2013-07-20 05:16:45 +0100 | [diff] [blame] | 1837 | wl_list_remove(&wm->transform_listener.link); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1838 | |
| 1839 | free(wm); |
| 1840 | } |
| 1841 | |
| 1842 | static void |
| 1843 | surface_destroy(struct wl_listener *listener, void *data) |
| 1844 | { |
| 1845 | struct weston_wm_window *window = |
| 1846 | container_of(listener, |
| 1847 | struct weston_wm_window, surface_destroy_listener); |
| 1848 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1849 | wm_log("surface for xid %d destroyed\n", window->id); |
Kristian Høgsberg | 81cadc7 | 2013-09-03 16:15:42 -0700 | [diff] [blame^] | 1850 | |
| 1851 | window->surface = NULL; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | static struct weston_wm_window * |
| 1855 | get_wm_window(struct weston_surface *surface) |
| 1856 | { |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1857 | struct wl_listener *listener; |
| 1858 | |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 1859 | listener = wl_signal_get(&surface->destroy_signal, surface_destroy); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1860 | if (listener) |
| 1861 | return container_of(listener, struct weston_wm_window, |
| 1862 | surface_destroy_listener); |
| 1863 | |
| 1864 | return NULL; |
| 1865 | } |
| 1866 | |
| 1867 | static void |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1868 | weston_wm_window_configure(void *data) |
| 1869 | { |
| 1870 | struct weston_wm_window *window = data; |
| 1871 | struct weston_wm *wm = window->wm; |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1872 | uint32_t values[4]; |
| 1873 | int x, y, width, height; |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1874 | |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1875 | weston_wm_window_get_child_position(window, &x, &y); |
| 1876 | values[0] = x; |
| 1877 | values[1] = y; |
| 1878 | values[2] = window->width; |
| 1879 | values[3] = window->height; |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1880 | xcb_configure_window(wm->conn, |
| 1881 | window->id, |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1882 | XCB_CONFIG_WINDOW_X | |
| 1883 | XCB_CONFIG_WINDOW_Y | |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1884 | XCB_CONFIG_WINDOW_WIDTH | |
| 1885 | XCB_CONFIG_WINDOW_HEIGHT, |
| 1886 | values); |
| 1887 | |
| 1888 | weston_wm_window_get_frame_size(window, &width, &height); |
| 1889 | values[0] = width; |
| 1890 | values[1] = height; |
| 1891 | xcb_configure_window(wm->conn, |
| 1892 | window->frame_id, |
| 1893 | XCB_CONFIG_WINDOW_WIDTH | |
| 1894 | XCB_CONFIG_WINDOW_HEIGHT, |
| 1895 | values); |
| 1896 | |
| 1897 | window->configure_source = NULL; |
| 1898 | |
| 1899 | weston_wm_window_schedule_repaint(window); |
| 1900 | } |
| 1901 | |
| 1902 | static void |
| 1903 | send_configure(struct weston_surface *surface, |
| 1904 | uint32_t edges, int32_t width, int32_t height) |
| 1905 | { |
| 1906 | struct weston_wm_window *window = get_wm_window(surface); |
| 1907 | struct weston_wm *wm = window->wm; |
| 1908 | struct theme *t = window->wm->theme; |
Kristian Høgsberg | fa514b4 | 2013-07-08 15:00:25 -0400 | [diff] [blame] | 1909 | int vborder, hborder; |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1910 | |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1911 | if (window->fullscreen) { |
Kristian Høgsberg | fa514b4 | 2013-07-08 15:00:25 -0400 | [diff] [blame] | 1912 | hborder = 0; |
| 1913 | vborder = 0; |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1914 | } else if (window->decorate) { |
Kristian Høgsberg | fa514b4 | 2013-07-08 15:00:25 -0400 | [diff] [blame] | 1915 | hborder = 2 * (t->margin + t->width); |
| 1916 | vborder = 2 * t->margin + t->titlebar_height + t->width; |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1917 | } else { |
Kristian Høgsberg | fa514b4 | 2013-07-08 15:00:25 -0400 | [diff] [blame] | 1918 | hborder = 2 * t->margin; |
| 1919 | vborder = 2 * t->margin; |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1920 | } |
| 1921 | |
Kristian Høgsberg | fa514b4 | 2013-07-08 15:00:25 -0400 | [diff] [blame] | 1922 | if (width > hborder) |
| 1923 | window->width = width - hborder; |
| 1924 | else |
| 1925 | window->width = 1; |
| 1926 | |
| 1927 | if (height > vborder) |
| 1928 | window->height = height - vborder; |
| 1929 | else |
| 1930 | window->height = 1; |
| 1931 | |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1932 | if (window->configure_source) |
| 1933 | return; |
| 1934 | |
| 1935 | window->configure_source = |
| 1936 | wl_event_loop_add_idle(wm->server->loop, |
| 1937 | weston_wm_window_configure, window); |
| 1938 | } |
| 1939 | |
| 1940 | static const struct weston_shell_client shell_client = { |
| 1941 | send_configure |
| 1942 | }; |
| 1943 | |
Kristian Høgsberg | 59f44c1 | 2013-08-31 00:08:27 -0700 | [diff] [blame] | 1944 | static int |
| 1945 | legacy_fullscreen(struct weston_wm *wm, |
| 1946 | struct weston_wm_window *window, |
| 1947 | struct weston_output **output_ret) |
| 1948 | { |
| 1949 | struct weston_compositor *compositor = wm->server->compositor; |
| 1950 | struct weston_output *output; |
Kristian Høgsberg | 1a7a57f | 2013-08-31 00:12:25 -0700 | [diff] [blame] | 1951 | uint32_t minmax = PMinSize | PMaxSize; |
| 1952 | int matching_size; |
Kristian Høgsberg | 59f44c1 | 2013-08-31 00:08:27 -0700 | [diff] [blame] | 1953 | |
| 1954 | /* Heuristics for detecting legacy fullscreen windows... */ |
| 1955 | |
| 1956 | wl_list_for_each(output, &compositor->output_list, link) { |
| 1957 | if (output->x == window->x && |
| 1958 | output->y == window->y && |
| 1959 | output->width == window->width && |
| 1960 | output->height == window->height && |
| 1961 | window->override_redirect) { |
| 1962 | *output_ret = output; |
| 1963 | return 1; |
| 1964 | } |
Kristian Høgsberg | 1a7a57f | 2013-08-31 00:12:25 -0700 | [diff] [blame] | 1965 | |
| 1966 | matching_size = 0; |
| 1967 | if ((window->size_hints.flags & (USSize |PSize)) && |
| 1968 | window->size_hints.width == output->width && |
| 1969 | window->size_hints.height == output->height) |
| 1970 | matching_size = 1; |
| 1971 | if ((window->size_hints.flags & minmax) == minmax && |
| 1972 | window->size_hints.min_width == output->width && |
| 1973 | window->size_hints.min_height == output->height && |
| 1974 | window->size_hints.max_width == output->width && |
| 1975 | window->size_hints.max_height == output->height) |
| 1976 | matching_size = 1; |
| 1977 | |
| 1978 | if (matching_size && !window->decorate && |
| 1979 | (window->size_hints.flags & (USPosition | PPosition)) && |
| 1980 | window->size_hints.x == output->x && |
| 1981 | window->size_hints.y == output->y) { |
| 1982 | *output_ret = output; |
| 1983 | return 1; |
| 1984 | } |
Kristian Høgsberg | 59f44c1 | 2013-08-31 00:08:27 -0700 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | return 0; |
| 1988 | } |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1989 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1990 | xserver_map_shell_surface(struct weston_wm *wm, |
| 1991 | struct weston_wm_window *window) |
| 1992 | { |
| 1993 | struct weston_shell_interface *shell_interface = |
| 1994 | &wm->server->compositor->shell_interface; |
Kristian Høgsberg | 59f44c1 | 2013-08-31 00:08:27 -0700 | [diff] [blame] | 1995 | struct weston_output *output; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1996 | |
| 1997 | if (!shell_interface->create_shell_surface) |
| 1998 | return; |
| 1999 | |
| 2000 | window->shsurf = |
| 2001 | shell_interface->create_shell_surface(shell_interface->shell, |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 2002 | window->surface, |
| 2003 | &shell_client); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 2004 | |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 2005 | if (window->fullscreen) { |
| 2006 | window->saved_width = window->width; |
| 2007 | window->saved_height = window->height; |
| 2008 | shell_interface->set_fullscreen(window->shsurf, |
| 2009 | WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, |
| 2010 | 0, NULL); |
| 2011 | return; |
Kristian Høgsberg | 59f44c1 | 2013-08-31 00:08:27 -0700 | [diff] [blame] | 2012 | } else if (legacy_fullscreen(wm, window, &output)) { |
| 2013 | window->fullscreen = 1; |
| 2014 | shell_interface->set_fullscreen(window->shsurf, |
| 2015 | WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, |
| 2016 | 0, output); |
Kristian Høgsberg | 1a7a57f | 2013-08-31 00:12:25 -0700 | [diff] [blame] | 2017 | } else if (!window->override_redirect) { |
| 2018 | shell_interface->set_toplevel(window->shsurf); |
| 2019 | return; |
Tiago Vignatti | fb2adba | 2013-06-12 15:43:21 -0300 | [diff] [blame] | 2020 | } else { |
| 2021 | shell_interface->set_xwayland(window->shsurf, |
Kristian Høgsberg | 146f5ba | 2013-08-22 16:24:15 -0700 | [diff] [blame] | 2022 | window->x, |
| 2023 | window->y, |
Tiago Vignatti | fb2adba | 2013-06-12 15:43:21 -0300 | [diff] [blame] | 2024 | WL_SHELL_SURFACE_TRANSIENT_INACTIVE); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 2025 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | static void |
| 2029 | xserver_set_window_id(struct wl_client *client, struct wl_resource *resource, |
| 2030 | struct wl_resource *surface_resource, uint32_t id) |
| 2031 | { |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 2032 | struct weston_xserver *wxs = wl_resource_get_user_data(resource); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 2033 | struct weston_wm *wm = wxs->wm; |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 2034 | struct weston_surface *surface = |
| 2035 | wl_resource_get_user_data(surface_resource); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 2036 | struct weston_wm_window *window; |
| 2037 | |
| 2038 | if (client != wxs->client) |
| 2039 | return; |
| 2040 | |
| 2041 | window = hash_table_lookup(wm->window_hash, id); |
| 2042 | if (window == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2043 | weston_log("set_window_id for unknown window %d\n", id); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 2044 | return; |
| 2045 | } |
| 2046 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 2047 | wm_log("set_window_id %d for surface %p\n", id, surface); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 2048 | |
| 2049 | weston_wm_window_read_properties(window); |
| 2050 | |
| 2051 | window->surface = (struct weston_surface *) surface; |
| 2052 | window->surface_destroy_listener.notify = surface_destroy; |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 2053 | wl_signal_add(&surface->destroy_signal, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 2054 | &window->surface_destroy_listener); |
| 2055 | |
| 2056 | weston_wm_window_schedule_repaint(window); |
| 2057 | xserver_map_shell_surface(wm, window); |
| 2058 | } |
| 2059 | |
| 2060 | const struct xserver_interface xserver_implementation = { |
| 2061 | xserver_set_window_id |
| 2062 | }; |