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 | 318ea37 | 2013-09-03 16:19:18 -0700 | [diff] [blame^] | 756 | weston_wm_window_create_frame(struct weston_wm_window *window) |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 757 | { |
Kristian Høgsberg | 318ea37 | 2013-09-03 16:19:18 -0700 | [diff] [blame^] | 758 | struct weston_wm *wm = window->wm; |
Kristian Høgsberg | f918719 | 2013-05-02 13:43:24 -0400 | [diff] [blame] | 759 | uint32_t values[3]; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 760 | int x, y, width, height; |
| 761 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 762 | weston_wm_window_get_frame_size(window, &width, &height); |
| 763 | weston_wm_window_get_child_position(window, &x, &y); |
| 764 | |
Kristian Høgsberg | f918719 | 2013-05-02 13:43:24 -0400 | [diff] [blame] | 765 | values[0] = wm->screen->black_pixel; |
| 766 | values[1] = |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 767 | XCB_EVENT_MASK_KEY_PRESS | |
| 768 | XCB_EVENT_MASK_KEY_RELEASE | |
| 769 | XCB_EVENT_MASK_BUTTON_PRESS | |
| 770 | XCB_EVENT_MASK_BUTTON_RELEASE | |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 771 | XCB_EVENT_MASK_POINTER_MOTION | |
| 772 | XCB_EVENT_MASK_ENTER_WINDOW | |
| 773 | XCB_EVENT_MASK_LEAVE_WINDOW | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 774 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | |
Kristian Høgsberg | 44c2013 | 2012-05-30 10:09:21 -0400 | [diff] [blame] | 775 | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT; |
Kristian Høgsberg | f918719 | 2013-05-02 13:43:24 -0400 | [diff] [blame] | 776 | values[2] = wm->colormap; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 777 | |
| 778 | window->frame_id = xcb_generate_id(wm->conn); |
| 779 | xcb_create_window(wm->conn, |
Kristian Høgsberg | f918719 | 2013-05-02 13:43:24 -0400 | [diff] [blame] | 780 | 32, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 781 | window->frame_id, |
| 782 | wm->screen->root, |
| 783 | 0, 0, |
| 784 | width, height, |
| 785 | 0, |
| 786 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
Kristian Høgsberg | f918719 | 2013-05-02 13:43:24 -0400 | [diff] [blame] | 787 | wm->visual_id, |
| 788 | XCB_CW_BORDER_PIXEL | |
| 789 | XCB_CW_EVENT_MASK | |
| 790 | XCB_CW_COLORMAP, values); |
| 791 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 792 | xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y); |
| 793 | |
| 794 | values[0] = 0; |
| 795 | xcb_configure_window(wm->conn, window->id, |
| 796 | XCB_CONFIG_WINDOW_BORDER_WIDTH, values); |
| 797 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 798 | window->cairo_surface = |
| 799 | cairo_xcb_surface_create_with_xrender_format(wm->conn, |
| 800 | wm->screen, |
| 801 | window->frame_id, |
Kristian Høgsberg | f918719 | 2013-05-02 13:43:24 -0400 | [diff] [blame] | 802 | &wm->format_rgba, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 803 | width, height); |
| 804 | |
| 805 | hash_table_insert(wm->window_hash, window->frame_id, window); |
| 806 | } |
| 807 | |
| 808 | static void |
Kristian Høgsberg | 318ea37 | 2013-09-03 16:19:18 -0700 | [diff] [blame^] | 809 | weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event) |
| 810 | { |
| 811 | xcb_map_request_event_t *map_request = |
| 812 | (xcb_map_request_event_t *) event; |
| 813 | struct weston_wm_window *window; |
| 814 | |
| 815 | if (our_resource(wm, map_request->window)) { |
| 816 | wm_log("XCB_MAP_REQUEST (window %d, ours)\n", |
| 817 | map_request->window); |
| 818 | return; |
| 819 | } |
| 820 | |
| 821 | window = hash_table_lookup(wm->window_hash, map_request->window); |
| 822 | |
| 823 | weston_wm_window_read_properties(window); |
| 824 | |
| 825 | if (window->frame_id == XCB_WINDOW_NONE) |
| 826 | weston_wm_window_create_frame(window); |
| 827 | |
| 828 | wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n", |
| 829 | window->id, window, window->frame_id); |
| 830 | |
| 831 | weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE); |
| 832 | weston_wm_window_set_net_wm_state(window); |
| 833 | |
| 834 | xcb_map_window(wm->conn, map_request->window); |
| 835 | xcb_map_window(wm->conn, window->frame_id); |
| 836 | } |
| 837 | |
| 838 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 839 | weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 840 | { |
| 841 | xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event; |
| 842 | |
| 843 | if (our_resource(wm, map_notify->window)) { |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 844 | wm_log("XCB_MAP_NOTIFY (window %d, ours)\n", |
| 845 | map_notify->window); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 846 | return; |
| 847 | } |
| 848 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 849 | wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | static void |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 853 | weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 854 | { |
| 855 | xcb_unmap_notify_event_t *unmap_notify = |
| 856 | (xcb_unmap_notify_event_t *) event; |
| 857 | struct weston_wm_window *window; |
| 858 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 859 | wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n", |
| 860 | unmap_notify->window, |
| 861 | unmap_notify->event, |
| 862 | our_resource(wm, unmap_notify->window) ? ", ours" : ""); |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 863 | |
| 864 | if (our_resource(wm, unmap_notify->window)) |
| 865 | return; |
| 866 | |
MoD | 3170012 | 2013-06-11 19:58:55 -0500 | [diff] [blame] | 867 | if (unmap_notify->response_type & SEND_EVENT_MASK) |
Kristian Høgsberg | d64bdf4 | 2012-05-31 10:33:43 -0400 | [diff] [blame] | 868 | /* We just ignore the ICCCM 4.1.4 synthetic unmap notify |
| 869 | * as it may come in after we've destroyed the window. */ |
Kristian Høgsberg | f197e9f | 2012-05-30 11:46:29 -0400 | [diff] [blame] | 870 | return; |
Kristian Høgsberg | f197e9f | 2012-05-30 11:46:29 -0400 | [diff] [blame] | 871 | |
Kristian Høgsberg | d64bdf4 | 2012-05-31 10:33:43 -0400 | [diff] [blame] | 872 | window = hash_table_lookup(wm->window_hash, unmap_notify->window); |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 873 | if (window->repaint_source) |
| 874 | wl_event_source_remove(window->repaint_source); |
| 875 | if (window->cairo_surface) |
| 876 | cairo_surface_destroy(window->cairo_surface); |
| 877 | |
Kristian Høgsberg | be375b3 | 2012-06-05 11:46:08 -0400 | [diff] [blame] | 878 | if (window->frame_id) { |
| 879 | xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0); |
| 880 | xcb_destroy_window(wm->conn, window->frame_id); |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 881 | weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE); |
Kristian Høgsberg | be375b3 | 2012-06-05 11:46:08 -0400 | [diff] [blame] | 882 | hash_table_remove(wm->window_hash, window->frame_id); |
| 883 | window->frame_id = XCB_WINDOW_NONE; |
| 884 | } |
Kristian Høgsberg | a6d9a5e | 2012-05-30 12:15:44 -0400 | [diff] [blame] | 885 | |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 886 | if (wm->focus_window == window) |
| 887 | wm->focus_window = NULL; |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 888 | if (window->surface) |
| 889 | wl_list_remove(&window->surface_destroy_listener.link); |
| 890 | window->surface = NULL; |
| 891 | } |
| 892 | |
| 893 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 894 | weston_wm_window_draw_decoration(void *data) |
| 895 | { |
| 896 | struct weston_wm_window *window = data; |
| 897 | struct weston_wm *wm = window->wm; |
| 898 | struct theme *t = wm->theme; |
| 899 | cairo_t *cr; |
| 900 | int x, y, width, height; |
| 901 | const char *title; |
| 902 | uint32_t flags = 0; |
| 903 | |
| 904 | weston_wm_window_read_properties(window); |
| 905 | |
| 906 | window->repaint_source = NULL; |
| 907 | |
| 908 | weston_wm_window_get_frame_size(window, &width, &height); |
| 909 | weston_wm_window_get_child_position(window, &x, &y); |
| 910 | |
| 911 | cairo_xcb_surface_set_size(window->cairo_surface, width, height); |
| 912 | cr = cairo_create(window->cairo_surface); |
| 913 | |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 914 | if (window->fullscreen) { |
| 915 | /* nothing */ |
| 916 | } else if (window->decorate) { |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 917 | if (wm->focus_window == window) |
| 918 | flags |= THEME_FRAME_ACTIVE; |
| 919 | |
| 920 | if (window->name) |
| 921 | title = window->name; |
| 922 | else |
| 923 | title = "untitled"; |
| 924 | |
| 925 | theme_render_frame(t, cr, width, height, title, flags); |
| 926 | } else { |
| 927 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 928 | cairo_set_source_rgba(cr, 0, 0, 0, 0); |
| 929 | cairo_paint(cr); |
| 930 | |
| 931 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 932 | cairo_set_source_rgba(cr, 0, 0, 0, 0.45); |
| 933 | tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64); |
| 934 | } |
| 935 | |
| 936 | cairo_destroy(cr); |
| 937 | |
| 938 | if (window->surface) { |
Scott Moreau | 76d8fc8 | 2012-11-22 15:35:13 -0700 | [diff] [blame] | 939 | pixman_region32_fini(&window->surface->pending.opaque); |
MoD | 384a11a | 2013-06-22 11:04:21 -0500 | [diff] [blame] | 940 | if(window->has_alpha) { |
| 941 | pixman_region32_init(&window->surface->pending.opaque); |
| 942 | } else { |
| 943 | /* We leave an extra pixel around the X window area to |
| 944 | * make sure we don't sample from the undefined alpha |
| 945 | * channel when filtering. */ |
| 946 | pixman_region32_init_rect(&window->surface->pending.opaque, |
| 947 | x - 1, y - 1, |
| 948 | window->width + 2, |
| 949 | window->height + 2); |
| 950 | } |
Pekka Paalanen | c3ce738 | 2013-03-08 14:56:49 +0200 | [diff] [blame] | 951 | weston_surface_geometry_dirty(window->surface); |
Kristian Høgsberg | d8b617d | 2013-02-14 21:56:32 -0500 | [diff] [blame] | 952 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 953 | |
Kristian Høgsberg | d8b617d | 2013-02-14 21:56:32 -0500 | [diff] [blame] | 954 | if (window->surface && !window->fullscreen) { |
Kristian Høgsberg | 81585e9 | 2013-02-14 22:01:58 -0500 | [diff] [blame] | 955 | pixman_region32_fini(&window->surface->pending.input); |
Kristian Høgsberg | d8b617d | 2013-02-14 21:56:32 -0500 | [diff] [blame] | 956 | pixman_region32_init_rect(&window->surface->pending.input, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 957 | t->margin, t->margin, |
| 958 | width - 2 * t->margin, |
| 959 | height - 2 * t->margin); |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | static void |
| 964 | weston_wm_window_schedule_repaint(struct weston_wm_window *window) |
| 965 | { |
| 966 | struct weston_wm *wm = window->wm; |
Pekka Paalanen | 4f9c07b | 2012-09-03 16:48:41 +0300 | [diff] [blame] | 967 | int width, height; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 968 | |
Kristian Høgsberg | c4063f3 | 2012-07-22 15:32:45 -0400 | [diff] [blame] | 969 | if (window->frame_id == XCB_WINDOW_NONE) { |
| 970 | if (window->surface != NULL) { |
Pekka Paalanen | 4f9c07b | 2012-09-03 16:48:41 +0300 | [diff] [blame] | 971 | weston_wm_window_get_frame_size(window, &width, &height); |
Scott Moreau | 76d8fc8 | 2012-11-22 15:35:13 -0700 | [diff] [blame] | 972 | pixman_region32_fini(&window->surface->pending.opaque); |
MoD | 384a11a | 2013-06-22 11:04:21 -0500 | [diff] [blame] | 973 | if(window->has_alpha) { |
| 974 | pixman_region32_init(&window->surface->pending.opaque); |
| 975 | } else { |
| 976 | pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0, |
| 977 | width, height); |
| 978 | } |
Pekka Paalanen | c3ce738 | 2013-03-08 14:56:49 +0200 | [diff] [blame] | 979 | weston_surface_geometry_dirty(window->surface); |
Kristian Høgsberg | c4063f3 | 2012-07-22 15:32:45 -0400 | [diff] [blame] | 980 | } |
| 981 | return; |
| 982 | } |
| 983 | |
| 984 | if (window->repaint_source) |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 985 | return; |
| 986 | |
| 987 | window->repaint_source = |
| 988 | wl_event_loop_add_idle(wm->server->loop, |
| 989 | weston_wm_window_draw_decoration, |
| 990 | window); |
| 991 | } |
| 992 | |
| 993 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 994 | weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 995 | { |
| 996 | xcb_property_notify_event_t *property_notify = |
| 997 | (xcb_property_notify_event_t *) event; |
| 998 | struct weston_wm_window *window; |
| 999 | |
| 1000 | window = hash_table_lookup(wm->window_hash, property_notify->window); |
Rob Bradford | aa521bd | 2013-01-10 19:48:57 +0000 | [diff] [blame] | 1001 | if (!window) |
| 1002 | return; |
| 1003 | |
| 1004 | window->properties_dirty = 1; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1005 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1006 | wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window); |
Kristian Høgsberg | e244cb0 | 2012-05-30 11:34:35 -0400 | [diff] [blame] | 1007 | if (property_notify->state == XCB_PROPERTY_DELETE) |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1008 | wm_log("deleted\n"); |
Kristian Høgsberg | e244cb0 | 2012-05-30 11:34:35 -0400 | [diff] [blame] | 1009 | else |
| 1010 | read_and_dump_property(wm, property_notify->window, |
| 1011 | property_notify->atom); |
Kristian Høgsberg | 0273b57 | 2012-05-30 09:58:02 -0400 | [diff] [blame] | 1012 | |
| 1013 | if (property_notify->atom == wm->atom.net_wm_name || |
| 1014 | property_notify->atom == XCB_ATOM_WM_NAME) |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1015 | weston_wm_window_schedule_repaint(window); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | static void |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1019 | weston_wm_window_create(struct weston_wm *wm, |
Tiago Vignatti | 771241e | 2012-06-04 20:01:45 +0300 | [diff] [blame] | 1020 | xcb_window_t id, int width, int height, int override) |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1021 | { |
| 1022 | struct weston_wm_window *window; |
| 1023 | uint32_t values[1]; |
MoD | 384a11a | 2013-06-22 11:04:21 -0500 | [diff] [blame] | 1024 | xcb_get_geometry_cookie_t geometry_cookie; |
| 1025 | xcb_get_geometry_reply_t *geometry_reply; |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1026 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 1027 | window = zalloc(sizeof *window); |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1028 | if (window == NULL) { |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1029 | wm_log("failed to allocate window\n"); |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1030 | return; |
| 1031 | } |
| 1032 | |
MoD | 384a11a | 2013-06-22 11:04:21 -0500 | [diff] [blame] | 1033 | geometry_cookie = xcb_get_geometry(wm->conn, id); |
| 1034 | |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1035 | values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE; |
| 1036 | xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values); |
| 1037 | |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1038 | window->wm = wm; |
| 1039 | window->id = id; |
| 1040 | window->properties_dirty = 1; |
Tiago Vignatti | 771241e | 2012-06-04 20:01:45 +0300 | [diff] [blame] | 1041 | window->override_redirect = override; |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1042 | window->width = width; |
| 1043 | window->height = height; |
| 1044 | |
MoD | 384a11a | 2013-06-22 11:04:21 -0500 | [diff] [blame] | 1045 | geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL); |
| 1046 | /* technically we should use XRender and check the visual format's |
| 1047 | alpha_mask, but checking depth is simpler and works in all known cases */ |
| 1048 | if(geometry_reply != NULL) |
| 1049 | window->has_alpha = geometry_reply->depth == 32; |
| 1050 | free(geometry_reply); |
| 1051 | |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1052 | hash_table_insert(wm->window_hash, id, window); |
| 1053 | } |
| 1054 | |
| 1055 | static void |
| 1056 | weston_wm_window_destroy(struct weston_wm_window *window) |
| 1057 | { |
| 1058 | hash_table_remove(window->wm->window_hash, window->id); |
| 1059 | free(window); |
| 1060 | } |
| 1061 | |
| 1062 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1063 | weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 1064 | { |
| 1065 | xcb_create_notify_event_t *create_notify = |
| 1066 | (xcb_create_notify_event_t *) event; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1067 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1068 | wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n", |
| 1069 | create_notify->window, |
| 1070 | create_notify->width, create_notify->height, |
| 1071 | create_notify->override_redirect ? ", override" : "", |
| 1072 | our_resource(wm, create_notify->window) ? ", ours" : ""); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1073 | |
| 1074 | if (our_resource(wm, create_notify->window)) |
| 1075 | return; |
| 1076 | |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1077 | weston_wm_window_create(wm, create_notify->window, |
Tiago Vignatti | 771241e | 2012-06-04 20:01:45 +0300 | [diff] [blame] | 1078 | create_notify->width, create_notify->height, |
| 1079 | create_notify->override_redirect); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | static void |
| 1083 | weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 1084 | { |
| 1085 | xcb_destroy_notify_event_t *destroy_notify = |
| 1086 | (xcb_destroy_notify_event_t *) event; |
| 1087 | struct weston_wm_window *window; |
| 1088 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1089 | wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n", |
| 1090 | destroy_notify->window, |
| 1091 | destroy_notify->event, |
| 1092 | our_resource(wm, destroy_notify->window) ? ", ours" : ""); |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 1093 | |
| 1094 | if (our_resource(wm, destroy_notify->window)) |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1095 | return; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1096 | |
| 1097 | window = hash_table_lookup(wm->window_hash, destroy_notify->window); |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1098 | weston_wm_window_destroy(window); |
| 1099 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1100 | |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1101 | static void |
| 1102 | weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event) |
| 1103 | { |
| 1104 | xcb_reparent_notify_event_t *reparent_notify = |
| 1105 | (xcb_reparent_notify_event_t *) event; |
| 1106 | struct weston_wm_window *window; |
Kristian Høgsberg | c9571fb | 2012-05-29 15:35:29 -0400 | [diff] [blame] | 1107 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1108 | wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n", |
| 1109 | reparent_notify->window, |
| 1110 | reparent_notify->parent, |
| 1111 | reparent_notify->event); |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1112 | |
| 1113 | if (reparent_notify->parent == wm->screen->root) { |
Tiago Vignatti | 771241e | 2012-06-04 20:01:45 +0300 | [diff] [blame] | 1114 | weston_wm_window_create(wm, reparent_notify->window, 10, 10, |
| 1115 | reparent_notify->override_redirect); |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1116 | } else if (!our_resource(wm, reparent_notify->parent)) { |
| 1117 | window = hash_table_lookup(wm->window_hash, |
| 1118 | reparent_notify->window); |
| 1119 | weston_wm_window_destroy(window); |
| 1120 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1121 | } |
| 1122 | |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 1123 | struct weston_seat * |
| 1124 | weston_wm_pick_seat(struct weston_wm *wm) |
| 1125 | { |
| 1126 | return container_of(wm->server->compositor->seat_list.next, |
| 1127 | struct weston_seat, link); |
| 1128 | } |
| 1129 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1130 | static void |
Kristian Høgsberg | e68fd10 | 2012-05-22 17:09:40 -0400 | [diff] [blame] | 1131 | weston_wm_window_handle_moveresize(struct weston_wm_window *window, |
| 1132 | xcb_client_message_event_t *client_message) |
| 1133 | { |
| 1134 | static const int map[] = { |
| 1135 | THEME_LOCATION_RESIZING_TOP_LEFT, |
| 1136 | THEME_LOCATION_RESIZING_TOP, |
| 1137 | THEME_LOCATION_RESIZING_TOP_RIGHT, |
| 1138 | THEME_LOCATION_RESIZING_RIGHT, |
| 1139 | THEME_LOCATION_RESIZING_BOTTOM_RIGHT, |
| 1140 | THEME_LOCATION_RESIZING_BOTTOM, |
| 1141 | THEME_LOCATION_RESIZING_BOTTOM_LEFT, |
| 1142 | THEME_LOCATION_RESIZING_LEFT |
| 1143 | }; |
| 1144 | |
| 1145 | struct weston_wm *wm = window->wm; |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 1146 | struct weston_seat *seat = weston_wm_pick_seat(wm); |
Kristian Høgsberg | e68fd10 | 2012-05-22 17:09:40 -0400 | [diff] [blame] | 1147 | int detail; |
| 1148 | struct weston_shell_interface *shell_interface = |
| 1149 | &wm->server->compositor->shell_interface; |
| 1150 | |
Kristian Høgsberg | e314875 | 2013-05-06 23:19:49 -0400 | [diff] [blame] | 1151 | if (seat->pointer->button_count != 1 || |
Kristian Høgsberg | fe7aa90 | 2013-05-08 09:54:37 -0400 | [diff] [blame] | 1152 | seat->pointer->focus != window->surface) |
Kristian Høgsberg | e68fd10 | 2012-05-22 17:09:40 -0400 | [diff] [blame] | 1153 | return; |
| 1154 | |
| 1155 | detail = client_message->data.data32[2]; |
| 1156 | switch (detail) { |
| 1157 | case _NET_WM_MOVERESIZE_MOVE: |
| 1158 | shell_interface->move(window->shsurf, seat); |
| 1159 | break; |
| 1160 | case _NET_WM_MOVERESIZE_SIZE_TOPLEFT: |
| 1161 | case _NET_WM_MOVERESIZE_SIZE_TOP: |
| 1162 | case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT: |
| 1163 | case _NET_WM_MOVERESIZE_SIZE_RIGHT: |
| 1164 | case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT: |
| 1165 | case _NET_WM_MOVERESIZE_SIZE_BOTTOM: |
| 1166 | case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT: |
| 1167 | case _NET_WM_MOVERESIZE_SIZE_LEFT: |
| 1168 | shell_interface->resize(window->shsurf, seat, map[detail]); |
| 1169 | break; |
| 1170 | case _NET_WM_MOVERESIZE_CANCEL: |
| 1171 | break; |
| 1172 | } |
| 1173 | } |
| 1174 | |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1175 | #define _NET_WM_STATE_REMOVE 0 |
| 1176 | #define _NET_WM_STATE_ADD 1 |
| 1177 | #define _NET_WM_STATE_TOGGLE 2 |
| 1178 | |
| 1179 | static int |
| 1180 | update_state(int action, int *state) |
| 1181 | { |
| 1182 | int new_state, changed; |
| 1183 | |
| 1184 | switch (action) { |
| 1185 | case _NET_WM_STATE_REMOVE: |
| 1186 | new_state = 0; |
| 1187 | break; |
| 1188 | case _NET_WM_STATE_ADD: |
| 1189 | new_state = 1; |
| 1190 | break; |
| 1191 | case _NET_WM_STATE_TOGGLE: |
| 1192 | new_state = !*state; |
| 1193 | break; |
| 1194 | default: |
| 1195 | return 0; |
| 1196 | } |
| 1197 | |
| 1198 | changed = (*state != new_state); |
| 1199 | *state = new_state; |
| 1200 | |
| 1201 | return changed; |
| 1202 | } |
| 1203 | |
| 1204 | static void |
| 1205 | weston_wm_window_configure(void *data); |
| 1206 | |
| 1207 | static void |
| 1208 | weston_wm_window_handle_state(struct weston_wm_window *window, |
| 1209 | xcb_client_message_event_t *client_message) |
| 1210 | { |
| 1211 | struct weston_wm *wm = window->wm; |
| 1212 | struct weston_shell_interface *shell_interface = |
| 1213 | &wm->server->compositor->shell_interface; |
| 1214 | uint32_t action, property; |
| 1215 | |
| 1216 | action = client_message->data.data32[0]; |
| 1217 | property = client_message->data.data32[1]; |
| 1218 | |
| 1219 | if (property == wm->atom.net_wm_state_fullscreen && |
| 1220 | update_state(action, &window->fullscreen)) { |
| 1221 | weston_wm_window_set_net_wm_state(window); |
| 1222 | if (window->fullscreen) { |
| 1223 | window->saved_width = window->width; |
| 1224 | window->saved_height = window->height; |
Kristian Høgsberg | 762b166 | 2013-02-21 20:18:37 -0500 | [diff] [blame] | 1225 | |
| 1226 | if (window->shsurf) |
| 1227 | shell_interface->set_fullscreen(window->shsurf, |
| 1228 | WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, |
| 1229 | 0, NULL); |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1230 | } else { |
| 1231 | shell_interface->set_toplevel(window->shsurf); |
| 1232 | window->width = window->saved_width; |
| 1233 | window->height = window->saved_height; |
| 1234 | weston_wm_window_configure(window); |
| 1235 | } |
| 1236 | } |
| 1237 | } |
| 1238 | |
Kristian Høgsberg | e68fd10 | 2012-05-22 17:09:40 -0400 | [diff] [blame] | 1239 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1240 | weston_wm_handle_client_message(struct weston_wm *wm, |
| 1241 | xcb_generic_event_t *event) |
| 1242 | { |
| 1243 | xcb_client_message_event_t *client_message = |
| 1244 | (xcb_client_message_event_t *) event; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1245 | struct weston_wm_window *window; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1246 | |
| 1247 | window = hash_table_lookup(wm->window_hash, client_message->window); |
| 1248 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1249 | wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n", |
| 1250 | get_atom_name(wm->conn, client_message->type), |
| 1251 | client_message->data.data32[0], |
| 1252 | client_message->data.data32[1], |
| 1253 | client_message->data.data32[2], |
| 1254 | client_message->data.data32[3], |
| 1255 | client_message->data.data32[4], |
| 1256 | client_message->window); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1257 | |
Kristian Høgsberg | e68fd10 | 2012-05-22 17:09:40 -0400 | [diff] [blame] | 1258 | if (client_message->type == wm->atom.net_wm_moveresize) |
| 1259 | weston_wm_window_handle_moveresize(window, client_message); |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1260 | else if (client_message->type == wm->atom.net_wm_state) |
| 1261 | weston_wm_window_handle_state(window, client_message); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1262 | } |
| 1263 | |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1264 | enum cursor_type { |
| 1265 | XWM_CURSOR_TOP, |
| 1266 | XWM_CURSOR_BOTTOM, |
| 1267 | XWM_CURSOR_LEFT, |
| 1268 | XWM_CURSOR_RIGHT, |
| 1269 | XWM_CURSOR_TOP_LEFT, |
| 1270 | XWM_CURSOR_TOP_RIGHT, |
| 1271 | XWM_CURSOR_BOTTOM_LEFT, |
| 1272 | XWM_CURSOR_BOTTOM_RIGHT, |
| 1273 | XWM_CURSOR_LEFT_PTR, |
| 1274 | }; |
| 1275 | |
| 1276 | static const char *cursors[] = { |
| 1277 | "top_side", |
| 1278 | "bottom_side", |
| 1279 | "left_side", |
| 1280 | "right_side", |
| 1281 | "top_left_corner", |
| 1282 | "top_right_corner", |
| 1283 | "bottom_left_corner", |
| 1284 | "bottom_right_corner", |
| 1285 | "left_ptr" |
| 1286 | }; |
| 1287 | |
| 1288 | static void |
| 1289 | weston_wm_create_cursors(struct weston_wm *wm) |
| 1290 | { |
| 1291 | int i, count = ARRAY_LENGTH(cursors); |
| 1292 | |
| 1293 | wm->cursors = malloc(count * sizeof(xcb_cursor_t)); |
| 1294 | for (i = 0; i < count; i++) { |
| 1295 | wm->cursors[i] = |
| 1296 | xcb_cursor_library_load_cursor(wm, cursors[i]); |
| 1297 | } |
| 1298 | |
| 1299 | wm->last_cursor = -1; |
| 1300 | } |
| 1301 | |
| 1302 | static void |
| 1303 | weston_wm_destroy_cursors(struct weston_wm *wm) |
| 1304 | { |
| 1305 | uint8_t i; |
| 1306 | |
| 1307 | for (i = 0; i < ARRAY_LENGTH(cursors); i++) |
| 1308 | xcb_free_cursor(wm->conn, wm->cursors[i]); |
| 1309 | |
| 1310 | free(wm->cursors); |
| 1311 | } |
| 1312 | |
| 1313 | static int |
| 1314 | get_cursor_for_location(struct theme *t, int width, int height, int x, int y) |
| 1315 | { |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 1316 | int location = theme_get_location(t, x, y, width, height, 0); |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1317 | |
| 1318 | switch (location) { |
| 1319 | case THEME_LOCATION_RESIZING_TOP: |
| 1320 | return XWM_CURSOR_TOP; |
| 1321 | case THEME_LOCATION_RESIZING_BOTTOM: |
| 1322 | return XWM_CURSOR_BOTTOM; |
| 1323 | case THEME_LOCATION_RESIZING_LEFT: |
| 1324 | return XWM_CURSOR_LEFT; |
| 1325 | case THEME_LOCATION_RESIZING_RIGHT: |
| 1326 | return XWM_CURSOR_RIGHT; |
| 1327 | case THEME_LOCATION_RESIZING_TOP_LEFT: |
| 1328 | return XWM_CURSOR_TOP_LEFT; |
| 1329 | case THEME_LOCATION_RESIZING_TOP_RIGHT: |
| 1330 | return XWM_CURSOR_TOP_RIGHT; |
| 1331 | case THEME_LOCATION_RESIZING_BOTTOM_LEFT: |
| 1332 | return XWM_CURSOR_BOTTOM_LEFT; |
| 1333 | case THEME_LOCATION_RESIZING_BOTTOM_RIGHT: |
| 1334 | return XWM_CURSOR_BOTTOM_RIGHT; |
| 1335 | case THEME_LOCATION_EXTERIOR: |
| 1336 | case THEME_LOCATION_TITLEBAR: |
| 1337 | default: |
| 1338 | return XWM_CURSOR_LEFT_PTR; |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | static void |
Tiago Vignatti | c190323 | 2012-07-16 12:15:37 -0400 | [diff] [blame] | 1343 | weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id, |
| 1344 | int cursor) |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1345 | { |
| 1346 | uint32_t cursor_value_list; |
| 1347 | |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1348 | if (wm->last_cursor == cursor) |
| 1349 | return; |
| 1350 | |
| 1351 | wm->last_cursor = cursor; |
| 1352 | |
| 1353 | cursor_value_list = wm->cursors[cursor]; |
Tiago Vignatti | c190323 | 2012-07-16 12:15:37 -0400 | [diff] [blame] | 1354 | xcb_change_window_attributes (wm->conn, window_id, |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1355 | XCB_CW_CURSOR, &cursor_value_list); |
| 1356 | xcb_flush(wm->conn); |
| 1357 | } |
| 1358 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1359 | static void |
| 1360 | weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event) |
| 1361 | { |
| 1362 | xcb_button_press_event_t *button = (xcb_button_press_event_t *) event; |
| 1363 | struct weston_shell_interface *shell_interface = |
| 1364 | &wm->server->compositor->shell_interface; |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 1365 | struct weston_seat *seat = weston_wm_pick_seat(wm); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1366 | struct weston_wm_window *window; |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 1367 | enum theme_location location; |
| 1368 | struct theme *t = wm->theme; |
Kristian Høgsberg | c1693f2 | 2012-05-22 16:56:23 -0400 | [diff] [blame] | 1369 | int width, height; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1370 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1371 | wm_log("XCB_BUTTON_%s (detail %d)\n", |
| 1372 | button->response_type == XCB_BUTTON_PRESS ? |
| 1373 | "PRESS" : "RELEASE", button->detail); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1374 | |
| 1375 | window = hash_table_lookup(wm->window_hash, button->event); |
Kristian Høgsberg | c1693f2 | 2012-05-22 16:56:23 -0400 | [diff] [blame] | 1376 | weston_wm_window_get_frame_size(window, &width, &height); |
| 1377 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1378 | if (button->response_type == XCB_BUTTON_PRESS && |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 1379 | button->detail == 1) { |
| 1380 | location = theme_get_location(t, |
| 1381 | button->event_x, |
| 1382 | button->event_y, |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 1383 | width, height, 0); |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 1384 | |
| 1385 | switch (location) { |
| 1386 | case THEME_LOCATION_TITLEBAR: |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 1387 | shell_interface->move(window->shsurf, seat); |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 1388 | break; |
Kristian Høgsberg | c1693f2 | 2012-05-22 16:56:23 -0400 | [diff] [blame] | 1389 | case THEME_LOCATION_RESIZING_TOP: |
| 1390 | case THEME_LOCATION_RESIZING_BOTTOM: |
| 1391 | case THEME_LOCATION_RESIZING_LEFT: |
| 1392 | case THEME_LOCATION_RESIZING_RIGHT: |
| 1393 | case THEME_LOCATION_RESIZING_TOP_LEFT: |
| 1394 | case THEME_LOCATION_RESIZING_TOP_RIGHT: |
| 1395 | case THEME_LOCATION_RESIZING_BOTTOM_LEFT: |
| 1396 | case THEME_LOCATION_RESIZING_BOTTOM_RIGHT: |
| 1397 | shell_interface->resize(window->shsurf, |
Kristian Høgsberg | 5ba3189 | 2012-08-10 10:06:59 -0400 | [diff] [blame] | 1398 | seat, location); |
Kristian Høgsberg | c1693f2 | 2012-05-22 16:56:23 -0400 | [diff] [blame] | 1399 | break; |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 1400 | default: |
| 1401 | break; |
| 1402 | } |
| 1403 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1404 | } |
| 1405 | |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1406 | static void |
| 1407 | weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event) |
| 1408 | { |
| 1409 | xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event; |
| 1410 | struct weston_wm_window *window; |
| 1411 | int cursor, width, height; |
| 1412 | |
| 1413 | window = hash_table_lookup(wm->window_hash, motion->event); |
Tiago Vignatti | 9134b77 | 2012-07-20 19:41:12 +0300 | [diff] [blame] | 1414 | if (!window || !window->decorate) |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1415 | return; |
| 1416 | |
| 1417 | weston_wm_window_get_frame_size(window, &width, &height); |
| 1418 | cursor = get_cursor_for_location(wm->theme, width, height, |
| 1419 | motion->event_x, motion->event_y); |
| 1420 | |
Tiago Vignatti | c190323 | 2012-07-16 12:15:37 -0400 | [diff] [blame] | 1421 | weston_wm_window_set_cursor(wm, window->frame_id, cursor); |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | static void |
| 1425 | weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event) |
| 1426 | { |
| 1427 | xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event; |
| 1428 | struct weston_wm_window *window; |
| 1429 | int cursor, width, height; |
| 1430 | |
| 1431 | window = hash_table_lookup(wm->window_hash, enter->event); |
Tiago Vignatti | 9134b77 | 2012-07-20 19:41:12 +0300 | [diff] [blame] | 1432 | if (!window || !window->decorate) |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1433 | return; |
| 1434 | |
| 1435 | weston_wm_window_get_frame_size(window, &width, &height); |
| 1436 | cursor = get_cursor_for_location(wm->theme, width, height, |
| 1437 | enter->event_x, enter->event_y); |
| 1438 | |
Tiago Vignatti | c190323 | 2012-07-16 12:15:37 -0400 | [diff] [blame] | 1439 | weston_wm_window_set_cursor(wm, window->frame_id, cursor); |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | static void |
| 1443 | weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event) |
| 1444 | { |
| 1445 | xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event; |
| 1446 | struct weston_wm_window *window; |
| 1447 | |
| 1448 | window = hash_table_lookup(wm->window_hash, leave->event); |
Tiago Vignatti | 9134b77 | 2012-07-20 19:41:12 +0300 | [diff] [blame] | 1449 | if (!window || !window->decorate) |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1450 | return; |
| 1451 | |
Tiago Vignatti | c190323 | 2012-07-16 12:15:37 -0400 | [diff] [blame] | 1452 | 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] | 1453 | } |
| 1454 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1455 | static int |
| 1456 | weston_wm_handle_event(int fd, uint32_t mask, void *data) |
| 1457 | { |
| 1458 | struct weston_wm *wm = data; |
| 1459 | xcb_generic_event_t *event; |
| 1460 | int count = 0; |
| 1461 | |
| 1462 | while (event = xcb_poll_for_event(wm->conn), event != NULL) { |
| 1463 | if (weston_wm_handle_selection_event(wm, event)) { |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1464 | free(event); |
| 1465 | count++; |
| 1466 | continue; |
| 1467 | } |
| 1468 | |
MoD | 3170012 | 2013-06-11 19:58:55 -0500 | [diff] [blame] | 1469 | switch (EVENT_TYPE(event)) { |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1470 | case XCB_BUTTON_PRESS: |
| 1471 | case XCB_BUTTON_RELEASE: |
| 1472 | weston_wm_handle_button(wm, event); |
| 1473 | break; |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1474 | case XCB_ENTER_NOTIFY: |
| 1475 | weston_wm_handle_enter(wm, event); |
| 1476 | break; |
| 1477 | case XCB_LEAVE_NOTIFY: |
| 1478 | weston_wm_handle_leave(wm, event); |
| 1479 | break; |
| 1480 | case XCB_MOTION_NOTIFY: |
| 1481 | weston_wm_handle_motion(wm, event); |
| 1482 | break; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1483 | case XCB_CREATE_NOTIFY: |
| 1484 | weston_wm_handle_create_notify(wm, event); |
| 1485 | break; |
| 1486 | case XCB_MAP_REQUEST: |
| 1487 | weston_wm_handle_map_request(wm, event); |
| 1488 | break; |
| 1489 | case XCB_MAP_NOTIFY: |
| 1490 | weston_wm_handle_map_notify(wm, event); |
| 1491 | break; |
| 1492 | case XCB_UNMAP_NOTIFY: |
Kristian Høgsberg | 194ea54 | 2012-05-30 10:05:41 -0400 | [diff] [blame] | 1493 | weston_wm_handle_unmap_notify(wm, event); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1494 | break; |
Kristian Høgsberg | 029539b | 2012-05-30 11:28:24 -0400 | [diff] [blame] | 1495 | case XCB_REPARENT_NOTIFY: |
| 1496 | weston_wm_handle_reparent_notify(wm, event); |
| 1497 | break; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1498 | case XCB_CONFIGURE_REQUEST: |
| 1499 | weston_wm_handle_configure_request(wm, event); |
| 1500 | break; |
| 1501 | case XCB_CONFIGURE_NOTIFY: |
| 1502 | weston_wm_handle_configure_notify(wm, event); |
| 1503 | break; |
| 1504 | case XCB_DESTROY_NOTIFY: |
| 1505 | weston_wm_handle_destroy_notify(wm, event); |
| 1506 | break; |
| 1507 | case XCB_MAPPING_NOTIFY: |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1508 | wm_log("XCB_MAPPING_NOTIFY\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1509 | break; |
| 1510 | case XCB_PROPERTY_NOTIFY: |
| 1511 | weston_wm_handle_property_notify(wm, event); |
| 1512 | break; |
| 1513 | case XCB_CLIENT_MESSAGE: |
| 1514 | weston_wm_handle_client_message(wm, event); |
| 1515 | break; |
| 1516 | } |
| 1517 | |
| 1518 | free(event); |
| 1519 | count++; |
| 1520 | } |
| 1521 | |
| 1522 | xcb_flush(wm->conn); |
| 1523 | |
| 1524 | return count; |
| 1525 | } |
| 1526 | |
| 1527 | static void |
Kristian Høgsberg | f918719 | 2013-05-02 13:43:24 -0400 | [diff] [blame] | 1528 | weston_wm_get_visual_and_colormap(struct weston_wm *wm) |
| 1529 | { |
| 1530 | xcb_depth_iterator_t d_iter; |
| 1531 | xcb_visualtype_iterator_t vt_iter; |
| 1532 | xcb_visualtype_t *visualtype; |
| 1533 | |
| 1534 | d_iter = xcb_screen_allowed_depths_iterator(wm->screen); |
| 1535 | visualtype = NULL; |
| 1536 | while (d_iter.rem > 0) { |
| 1537 | if (d_iter.data->depth == 32) { |
| 1538 | vt_iter = xcb_depth_visuals_iterator(d_iter.data); |
| 1539 | visualtype = vt_iter.data; |
| 1540 | break; |
| 1541 | } |
| 1542 | |
| 1543 | xcb_depth_next(&d_iter); |
| 1544 | } |
| 1545 | |
| 1546 | if (visualtype == NULL) { |
| 1547 | weston_log("no 32 bit visualtype\n"); |
| 1548 | return; |
| 1549 | } |
| 1550 | |
| 1551 | wm->visual_id = visualtype->visual_id; |
| 1552 | wm->colormap = xcb_generate_id(wm->conn); |
| 1553 | xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE, |
| 1554 | wm->colormap, wm->screen->root, wm->visual_id); |
| 1555 | } |
| 1556 | |
| 1557 | static void |
Tiago Vignatti | 9c4ff83 | 2012-11-30 17:19:57 -0200 | [diff] [blame] | 1558 | weston_wm_get_resources(struct weston_wm *wm) |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1559 | { |
| 1560 | |
| 1561 | #define F(field) offsetof(struct weston_wm, field) |
| 1562 | |
| 1563 | static const struct { const char *name; int offset; } atoms[] = { |
| 1564 | { "WM_PROTOCOLS", F(atom.wm_protocols) }, |
Kristian Høgsberg | 1a7a57f | 2013-08-31 00:12:25 -0700 | [diff] [blame] | 1565 | { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) }, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1566 | { "WM_TAKE_FOCUS", F(atom.wm_take_focus) }, |
| 1567 | { "WM_DELETE_WINDOW", F(atom.wm_delete_window) }, |
Kristian Høgsberg | a6d9a5e | 2012-05-30 12:15:44 -0400 | [diff] [blame] | 1568 | { "WM_STATE", F(atom.wm_state) }, |
Kristian Høgsberg | 670b5d3 | 2012-06-04 11:00:40 -0400 | [diff] [blame] | 1569 | { "WM_S0", F(atom.wm_s0) }, |
Tiago Vignatti | 0d20d7c | 2012-09-27 17:48:37 +0300 | [diff] [blame] | 1570 | { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) }, |
Kristian Høgsberg | 69981d9 | 2013-08-21 22:14:58 -0700 | [diff] [blame] | 1571 | { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) }, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1572 | { "_NET_WM_NAME", F(atom.net_wm_name) }, |
Tiago Vignatti | 0d20d7c | 2012-09-27 17:48:37 +0300 | [diff] [blame] | 1573 | { "_NET_WM_PID", F(atom.net_wm_pid) }, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1574 | { "_NET_WM_ICON", F(atom.net_wm_icon) }, |
| 1575 | { "_NET_WM_STATE", F(atom.net_wm_state) }, |
| 1576 | { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) }, |
| 1577 | { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) }, |
| 1578 | { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) }, |
| 1579 | { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) }, |
| 1580 | |
| 1581 | { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) }, |
| 1582 | { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) }, |
| 1583 | { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) }, |
| 1584 | { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) }, |
| 1585 | { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) }, |
| 1586 | { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) }, |
| 1587 | { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) }, |
Tiago Vignatti | bf1e866 | 2012-06-12 14:07:49 +0300 | [diff] [blame] | 1588 | { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) }, |
| 1589 | { "_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] | 1590 | { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) }, |
| 1591 | { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) }, |
| 1592 | { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) }, |
| 1593 | { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) }, |
| 1594 | { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) }, |
| 1595 | |
| 1596 | { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) }, |
| 1597 | { "_NET_SUPPORTING_WM_CHECK", |
| 1598 | F(atom.net_supporting_wm_check) }, |
| 1599 | { "_NET_SUPPORTED", F(atom.net_supported) }, |
| 1600 | { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) }, |
| 1601 | { "CLIPBOARD", F(atom.clipboard) }, |
Kristian Høgsberg | cba022a | 2012-06-04 10:11:45 -0400 | [diff] [blame] | 1602 | { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) }, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1603 | { "TARGETS", F(atom.targets) }, |
| 1604 | { "UTF8_STRING", F(atom.utf8_string) }, |
| 1605 | { "_WL_SELECTION", F(atom.wl_selection) }, |
| 1606 | { "INCR", F(atom.incr) }, |
| 1607 | { "TIMESTAMP", F(atom.timestamp) }, |
| 1608 | { "MULTIPLE", F(atom.multiple) }, |
| 1609 | { "UTF8_STRING" , F(atom.utf8_string) }, |
| 1610 | { "COMPOUND_TEXT", F(atom.compound_text) }, |
| 1611 | { "TEXT", F(atom.text) }, |
| 1612 | { "STRING", F(atom.string) }, |
| 1613 | { "text/plain;charset=utf-8", F(atom.text_plain_utf8) }, |
| 1614 | { "text/plain", F(atom.text_plain) }, |
| 1615 | }; |
| 1616 | #undef F |
| 1617 | |
| 1618 | xcb_xfixes_query_version_cookie_t xfixes_cookie; |
| 1619 | xcb_xfixes_query_version_reply_t *xfixes_reply; |
| 1620 | xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)]; |
| 1621 | xcb_intern_atom_reply_t *reply; |
| 1622 | xcb_render_query_pict_formats_reply_t *formats_reply; |
| 1623 | xcb_render_query_pict_formats_cookie_t formats_cookie; |
| 1624 | xcb_render_pictforminfo_t *formats; |
| 1625 | uint32_t i; |
| 1626 | |
| 1627 | xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id); |
| 1628 | |
| 1629 | formats_cookie = xcb_render_query_pict_formats(wm->conn); |
| 1630 | |
| 1631 | for (i = 0; i < ARRAY_LENGTH(atoms); i++) |
| 1632 | cookies[i] = xcb_intern_atom (wm->conn, 0, |
| 1633 | strlen(atoms[i].name), |
| 1634 | atoms[i].name); |
| 1635 | |
| 1636 | for (i = 0; i < ARRAY_LENGTH(atoms); i++) { |
| 1637 | reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL); |
| 1638 | *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom; |
| 1639 | free(reply); |
| 1640 | } |
| 1641 | |
| 1642 | wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id); |
| 1643 | if (!wm->xfixes || !wm->xfixes->present) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1644 | weston_log("xfixes not available\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1645 | |
| 1646 | xfixes_cookie = xcb_xfixes_query_version(wm->conn, |
| 1647 | XCB_XFIXES_MAJOR_VERSION, |
| 1648 | XCB_XFIXES_MINOR_VERSION); |
| 1649 | xfixes_reply = xcb_xfixes_query_version_reply(wm->conn, |
| 1650 | xfixes_cookie, NULL); |
| 1651 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1652 | weston_log("xfixes version: %d.%d\n", |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1653 | xfixes_reply->major_version, xfixes_reply->minor_version); |
| 1654 | |
| 1655 | free(xfixes_reply); |
| 1656 | |
| 1657 | formats_reply = xcb_render_query_pict_formats_reply(wm->conn, |
| 1658 | formats_cookie, 0); |
| 1659 | if (formats_reply == NULL) |
| 1660 | return; |
| 1661 | |
| 1662 | formats = xcb_render_query_pict_formats_formats(formats_reply); |
Kristian Høgsberg | e89cef3 | 2012-07-16 11:57:08 -0400 | [diff] [blame] | 1663 | for (i = 0; i < formats_reply->num_formats; i++) { |
| 1664 | if (formats[i].direct.red_mask != 0xff && |
| 1665 | formats[i].direct.red_shift != 16) |
| 1666 | continue; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1667 | if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT && |
| 1668 | formats[i].depth == 24) |
Kristian Høgsberg | e89cef3 | 2012-07-16 11:57:08 -0400 | [diff] [blame] | 1669 | wm->format_rgb = formats[i]; |
| 1670 | if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT && |
| 1671 | formats[i].depth == 32 && |
| 1672 | formats[i].direct.alpha_mask == 0xff && |
| 1673 | formats[i].direct.alpha_shift == 24) |
| 1674 | wm->format_rgba = formats[i]; |
| 1675 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1676 | |
| 1677 | free(formats_reply); |
| 1678 | } |
| 1679 | |
| 1680 | static void |
| 1681 | weston_wm_create_wm_window(struct weston_wm *wm) |
| 1682 | { |
| 1683 | static const char name[] = "Weston WM"; |
| 1684 | |
| 1685 | wm->wm_window = xcb_generate_id(wm->conn); |
| 1686 | xcb_create_window(wm->conn, |
| 1687 | XCB_COPY_FROM_PARENT, |
| 1688 | wm->wm_window, |
| 1689 | wm->screen->root, |
| 1690 | 0, 0, |
| 1691 | 10, 10, |
| 1692 | 0, |
| 1693 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 1694 | wm->screen->root_visual, |
| 1695 | 0, NULL); |
| 1696 | |
| 1697 | xcb_change_property(wm->conn, |
| 1698 | XCB_PROP_MODE_REPLACE, |
| 1699 | wm->wm_window, |
| 1700 | wm->atom.net_supporting_wm_check, |
| 1701 | XCB_ATOM_WINDOW, |
| 1702 | 32, /* format */ |
| 1703 | 1, &wm->wm_window); |
| 1704 | |
| 1705 | xcb_change_property(wm->conn, |
| 1706 | XCB_PROP_MODE_REPLACE, |
| 1707 | wm->wm_window, |
| 1708 | wm->atom.net_wm_name, |
| 1709 | wm->atom.utf8_string, |
| 1710 | 8, /* format */ |
| 1711 | strlen(name), name); |
| 1712 | |
| 1713 | xcb_change_property(wm->conn, |
| 1714 | XCB_PROP_MODE_REPLACE, |
| 1715 | wm->screen->root, |
| 1716 | wm->atom.net_supporting_wm_check, |
| 1717 | XCB_ATOM_WINDOW, |
| 1718 | 32, /* format */ |
| 1719 | 1, &wm->wm_window); |
| 1720 | |
Kristian Høgsberg | 670b5d3 | 2012-06-04 11:00:40 -0400 | [diff] [blame] | 1721 | /* Claim the WM_S0 selection even though we don't suport |
| 1722 | * the --replace functionality. */ |
| 1723 | xcb_set_selection_owner(wm->conn, |
| 1724 | wm->wm_window, |
| 1725 | wm->atom.wm_s0, |
| 1726 | XCB_TIME_CURRENT_TIME); |
Kristian Høgsberg | 69981d9 | 2013-08-21 22:14:58 -0700 | [diff] [blame] | 1727 | |
| 1728 | xcb_set_selection_owner(wm->conn, |
| 1729 | wm->wm_window, |
| 1730 | wm->atom.net_wm_cm_s0, |
| 1731 | XCB_TIME_CURRENT_TIME); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | struct weston_wm * |
| 1735 | weston_wm_create(struct weston_xserver *wxs) |
| 1736 | { |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1737 | struct weston_wm *wm; |
| 1738 | struct wl_event_loop *loop; |
| 1739 | xcb_screen_iterator_t s; |
Kristian Høgsberg | 4dec011 | 2012-06-03 09:18:06 -0400 | [diff] [blame] | 1740 | uint32_t values[1]; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1741 | int sv[2]; |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1742 | xcb_atom_t supported[3]; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1743 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 1744 | wm = zalloc(sizeof *wm); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1745 | if (wm == NULL) |
| 1746 | return NULL; |
| 1747 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1748 | wm->server = wxs; |
| 1749 | wm->window_hash = hash_table_create(); |
| 1750 | if (wm->window_hash == NULL) { |
| 1751 | free(wm); |
| 1752 | return NULL; |
| 1753 | } |
| 1754 | |
| 1755 | if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1756 | weston_log("socketpair failed\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1757 | hash_table_destroy(wm->window_hash); |
| 1758 | free(wm); |
| 1759 | return NULL; |
| 1760 | } |
| 1761 | |
| 1762 | xserver_send_client(wxs->resource, sv[1]); |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 1763 | wl_client_flush(wl_resource_get_client(wxs->resource)); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1764 | close(sv[1]); |
| 1765 | |
| 1766 | /* xcb_connect_to_fd takes ownership of the fd. */ |
| 1767 | wm->conn = xcb_connect_to_fd(sv[0], NULL); |
| 1768 | if (xcb_connection_has_error(wm->conn)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1769 | weston_log("xcb_connect_to_fd failed\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1770 | close(sv[0]); |
| 1771 | hash_table_destroy(wm->window_hash); |
| 1772 | free(wm); |
| 1773 | return NULL; |
| 1774 | } |
| 1775 | |
| 1776 | s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn)); |
| 1777 | wm->screen = s.data; |
| 1778 | |
| 1779 | loop = wl_display_get_event_loop(wxs->wl_display); |
| 1780 | wm->source = |
| 1781 | wl_event_loop_add_fd(loop, sv[0], |
| 1782 | WL_EVENT_READABLE, |
| 1783 | weston_wm_handle_event, wm); |
| 1784 | wl_event_source_check(wm->source); |
| 1785 | |
Tiago Vignatti | 9c4ff83 | 2012-11-30 17:19:57 -0200 | [diff] [blame] | 1786 | weston_wm_get_resources(wm); |
Kristian Høgsberg | f918719 | 2013-05-02 13:43:24 -0400 | [diff] [blame] | 1787 | weston_wm_get_visual_and_colormap(wm); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1788 | |
| 1789 | values[0] = |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1790 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | |
| 1791 | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | |
| 1792 | XCB_EVENT_MASK_PROPERTY_CHANGE; |
| 1793 | xcb_change_window_attributes(wm->conn, wm->screen->root, |
| 1794 | XCB_CW_EVENT_MASK, values); |
| 1795 | wm->theme = theme_create(); |
| 1796 | |
| 1797 | weston_wm_create_wm_window(wm); |
| 1798 | |
| 1799 | supported[0] = wm->atom.net_wm_moveresize; |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1800 | supported[1] = wm->atom.net_wm_state; |
| 1801 | supported[2] = wm->atom.net_wm_state_fullscreen; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1802 | xcb_change_property(wm->conn, |
| 1803 | XCB_PROP_MODE_REPLACE, |
| 1804 | wm->screen->root, |
| 1805 | wm->atom.net_supported, |
| 1806 | XCB_ATOM_ATOM, |
| 1807 | 32, /* format */ |
| 1808 | ARRAY_LENGTH(supported), supported); |
| 1809 | |
Kristian Høgsberg | 4dec011 | 2012-06-03 09:18:06 -0400 | [diff] [blame] | 1810 | weston_wm_selection_init(wm); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1811 | |
| 1812 | xcb_flush(wm->conn); |
| 1813 | |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1814 | wm->activate_listener.notify = weston_wm_window_activate; |
| 1815 | wl_signal_add(&wxs->compositor->activate_signal, |
| 1816 | &wm->activate_listener); |
Tiago Vignatti | fb2adba | 2013-06-12 15:43:21 -0300 | [diff] [blame] | 1817 | wm->transform_listener.notify = weston_wm_window_transform; |
| 1818 | wl_signal_add(&wxs->compositor->transform_signal, |
| 1819 | &wm->transform_listener); |
Tiago Vignatti | 0d20d7c | 2012-09-27 17:48:37 +0300 | [diff] [blame] | 1820 | wm->kill_listener.notify = weston_wm_kill_client; |
| 1821 | wl_signal_add(&wxs->compositor->kill_signal, |
| 1822 | &wm->kill_listener); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1823 | |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1824 | weston_wm_create_cursors(wm); |
Tiago Vignatti | c190323 | 2012-07-16 12:15:37 -0400 | [diff] [blame] | 1825 | 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] | 1826 | |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1827 | weston_log("created wm\n"); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1828 | |
| 1829 | return wm; |
| 1830 | } |
| 1831 | |
| 1832 | void |
| 1833 | weston_wm_destroy(struct weston_wm *wm) |
| 1834 | { |
| 1835 | /* FIXME: Free windows in hash. */ |
| 1836 | hash_table_destroy(wm->window_hash); |
Tiago Vignatti | 236b48d | 2012-07-16 12:09:19 -0400 | [diff] [blame] | 1837 | weston_wm_destroy_cursors(wm); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1838 | xcb_disconnect(wm->conn); |
| 1839 | wl_event_source_remove(wm->source); |
| 1840 | wl_list_remove(&wm->selection_listener.link); |
| 1841 | wl_list_remove(&wm->activate_listener.link); |
Tiago Vignatti | 0d20d7c | 2012-09-27 17:48:37 +0300 | [diff] [blame] | 1842 | wl_list_remove(&wm->kill_listener.link); |
Louis-Francis Ratté-Boulianne | dce3dac | 2013-07-20 05:16:45 +0100 | [diff] [blame] | 1843 | wl_list_remove(&wm->transform_listener.link); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1844 | |
| 1845 | free(wm); |
| 1846 | } |
| 1847 | |
| 1848 | static void |
| 1849 | surface_destroy(struct wl_listener *listener, void *data) |
| 1850 | { |
| 1851 | struct weston_wm_window *window = |
| 1852 | container_of(listener, |
| 1853 | struct weston_wm_window, surface_destroy_listener); |
| 1854 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 1855 | wm_log("surface for xid %d destroyed\n", window->id); |
Kristian Høgsberg | 81cadc7 | 2013-09-03 16:15:42 -0700 | [diff] [blame] | 1856 | |
| 1857 | window->surface = NULL; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | static struct weston_wm_window * |
| 1861 | get_wm_window(struct weston_surface *surface) |
| 1862 | { |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1863 | struct wl_listener *listener; |
| 1864 | |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 1865 | listener = wl_signal_get(&surface->destroy_signal, surface_destroy); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1866 | if (listener) |
| 1867 | return container_of(listener, struct weston_wm_window, |
| 1868 | surface_destroy_listener); |
| 1869 | |
| 1870 | return NULL; |
| 1871 | } |
| 1872 | |
| 1873 | static void |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1874 | weston_wm_window_configure(void *data) |
| 1875 | { |
| 1876 | struct weston_wm_window *window = data; |
| 1877 | struct weston_wm *wm = window->wm; |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1878 | uint32_t values[4]; |
| 1879 | int x, y, width, height; |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1880 | |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1881 | weston_wm_window_get_child_position(window, &x, &y); |
| 1882 | values[0] = x; |
| 1883 | values[1] = y; |
| 1884 | values[2] = window->width; |
| 1885 | values[3] = window->height; |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1886 | xcb_configure_window(wm->conn, |
| 1887 | window->id, |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1888 | XCB_CONFIG_WINDOW_X | |
| 1889 | XCB_CONFIG_WINDOW_Y | |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1890 | XCB_CONFIG_WINDOW_WIDTH | |
| 1891 | XCB_CONFIG_WINDOW_HEIGHT, |
| 1892 | values); |
| 1893 | |
| 1894 | weston_wm_window_get_frame_size(window, &width, &height); |
| 1895 | values[0] = width; |
| 1896 | values[1] = height; |
| 1897 | xcb_configure_window(wm->conn, |
| 1898 | window->frame_id, |
| 1899 | XCB_CONFIG_WINDOW_WIDTH | |
| 1900 | XCB_CONFIG_WINDOW_HEIGHT, |
| 1901 | values); |
| 1902 | |
| 1903 | window->configure_source = NULL; |
| 1904 | |
| 1905 | weston_wm_window_schedule_repaint(window); |
| 1906 | } |
| 1907 | |
| 1908 | static void |
| 1909 | send_configure(struct weston_surface *surface, |
| 1910 | uint32_t edges, int32_t width, int32_t height) |
| 1911 | { |
| 1912 | struct weston_wm_window *window = get_wm_window(surface); |
| 1913 | struct weston_wm *wm = window->wm; |
| 1914 | struct theme *t = window->wm->theme; |
Kristian Høgsberg | fa514b4 | 2013-07-08 15:00:25 -0400 | [diff] [blame] | 1915 | int vborder, hborder; |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1916 | |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1917 | if (window->fullscreen) { |
Kristian Høgsberg | fa514b4 | 2013-07-08 15:00:25 -0400 | [diff] [blame] | 1918 | hborder = 0; |
| 1919 | vborder = 0; |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 1920 | } else if (window->decorate) { |
Kristian Høgsberg | fa514b4 | 2013-07-08 15:00:25 -0400 | [diff] [blame] | 1921 | hborder = 2 * (t->margin + t->width); |
| 1922 | vborder = 2 * t->margin + t->titlebar_height + t->width; |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1923 | } else { |
Kristian Høgsberg | fa514b4 | 2013-07-08 15:00:25 -0400 | [diff] [blame] | 1924 | hborder = 2 * t->margin; |
| 1925 | vborder = 2 * t->margin; |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1926 | } |
| 1927 | |
Kristian Høgsberg | fa514b4 | 2013-07-08 15:00:25 -0400 | [diff] [blame] | 1928 | if (width > hborder) |
| 1929 | window->width = width - hborder; |
| 1930 | else |
| 1931 | window->width = 1; |
| 1932 | |
| 1933 | if (height > vborder) |
| 1934 | window->height = height - vborder; |
| 1935 | else |
| 1936 | window->height = 1; |
| 1937 | |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1938 | if (window->configure_source) |
| 1939 | return; |
| 1940 | |
| 1941 | window->configure_source = |
| 1942 | wl_event_loop_add_idle(wm->server->loop, |
| 1943 | weston_wm_window_configure, window); |
| 1944 | } |
| 1945 | |
| 1946 | static const struct weston_shell_client shell_client = { |
| 1947 | send_configure |
| 1948 | }; |
| 1949 | |
Kristian Høgsberg | 59f44c1 | 2013-08-31 00:08:27 -0700 | [diff] [blame] | 1950 | static int |
| 1951 | legacy_fullscreen(struct weston_wm *wm, |
| 1952 | struct weston_wm_window *window, |
| 1953 | struct weston_output **output_ret) |
| 1954 | { |
| 1955 | struct weston_compositor *compositor = wm->server->compositor; |
| 1956 | struct weston_output *output; |
Kristian Høgsberg | 1a7a57f | 2013-08-31 00:12:25 -0700 | [diff] [blame] | 1957 | uint32_t minmax = PMinSize | PMaxSize; |
| 1958 | int matching_size; |
Kristian Høgsberg | 59f44c1 | 2013-08-31 00:08:27 -0700 | [diff] [blame] | 1959 | |
| 1960 | /* Heuristics for detecting legacy fullscreen windows... */ |
| 1961 | |
| 1962 | wl_list_for_each(output, &compositor->output_list, link) { |
| 1963 | if (output->x == window->x && |
| 1964 | output->y == window->y && |
| 1965 | output->width == window->width && |
| 1966 | output->height == window->height && |
| 1967 | window->override_redirect) { |
| 1968 | *output_ret = output; |
| 1969 | return 1; |
| 1970 | } |
Kristian Høgsberg | 1a7a57f | 2013-08-31 00:12:25 -0700 | [diff] [blame] | 1971 | |
| 1972 | matching_size = 0; |
| 1973 | if ((window->size_hints.flags & (USSize |PSize)) && |
| 1974 | window->size_hints.width == output->width && |
| 1975 | window->size_hints.height == output->height) |
| 1976 | matching_size = 1; |
| 1977 | if ((window->size_hints.flags & minmax) == minmax && |
| 1978 | window->size_hints.min_width == output->width && |
| 1979 | window->size_hints.min_height == output->height && |
| 1980 | window->size_hints.max_width == output->width && |
| 1981 | window->size_hints.max_height == output->height) |
| 1982 | matching_size = 1; |
| 1983 | |
| 1984 | if (matching_size && !window->decorate && |
| 1985 | (window->size_hints.flags & (USPosition | PPosition)) && |
| 1986 | window->size_hints.x == output->x && |
| 1987 | window->size_hints.y == output->y) { |
| 1988 | *output_ret = output; |
| 1989 | return 1; |
| 1990 | } |
Kristian Høgsberg | 59f44c1 | 2013-08-31 00:08:27 -0700 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | return 0; |
| 1994 | } |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 1995 | static void |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 1996 | xserver_map_shell_surface(struct weston_wm *wm, |
| 1997 | struct weston_wm_window *window) |
| 1998 | { |
| 1999 | struct weston_shell_interface *shell_interface = |
| 2000 | &wm->server->compositor->shell_interface; |
Kristian Høgsberg | 59f44c1 | 2013-08-31 00:08:27 -0700 | [diff] [blame] | 2001 | struct weston_output *output; |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 2002 | |
| 2003 | if (!shell_interface->create_shell_surface) |
| 2004 | return; |
| 2005 | |
| 2006 | window->shsurf = |
| 2007 | shell_interface->create_shell_surface(shell_interface->shell, |
Kristian Høgsberg | a61ca06 | 2012-05-22 16:05:52 -0400 | [diff] [blame] | 2008 | window->surface, |
| 2009 | &shell_client); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 2010 | |
Kristian Høgsberg | b810eb5 | 2013-02-12 20:07:05 -0500 | [diff] [blame] | 2011 | if (window->fullscreen) { |
| 2012 | window->saved_width = window->width; |
| 2013 | window->saved_height = window->height; |
| 2014 | shell_interface->set_fullscreen(window->shsurf, |
| 2015 | WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, |
| 2016 | 0, NULL); |
| 2017 | return; |
Kristian Høgsberg | 59f44c1 | 2013-08-31 00:08:27 -0700 | [diff] [blame] | 2018 | } else if (legacy_fullscreen(wm, window, &output)) { |
| 2019 | window->fullscreen = 1; |
| 2020 | shell_interface->set_fullscreen(window->shsurf, |
| 2021 | WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, |
| 2022 | 0, output); |
Kristian Høgsberg | 1a7a57f | 2013-08-31 00:12:25 -0700 | [diff] [blame] | 2023 | } else if (!window->override_redirect) { |
| 2024 | shell_interface->set_toplevel(window->shsurf); |
| 2025 | return; |
Tiago Vignatti | fb2adba | 2013-06-12 15:43:21 -0300 | [diff] [blame] | 2026 | } else { |
| 2027 | shell_interface->set_xwayland(window->shsurf, |
Kristian Høgsberg | 146f5ba | 2013-08-22 16:24:15 -0700 | [diff] [blame] | 2028 | window->x, |
| 2029 | window->y, |
Tiago Vignatti | fb2adba | 2013-06-12 15:43:21 -0300 | [diff] [blame] | 2030 | WL_SHELL_SURFACE_TRANSIENT_INACTIVE); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 2031 | } |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | static void |
| 2035 | xserver_set_window_id(struct wl_client *client, struct wl_resource *resource, |
| 2036 | struct wl_resource *surface_resource, uint32_t id) |
| 2037 | { |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 2038 | struct weston_xserver *wxs = wl_resource_get_user_data(resource); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 2039 | struct weston_wm *wm = wxs->wm; |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 2040 | struct weston_surface *surface = |
| 2041 | wl_resource_get_user_data(surface_resource); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 2042 | struct weston_wm_window *window; |
| 2043 | |
| 2044 | if (client != wxs->client) |
| 2045 | return; |
| 2046 | |
| 2047 | window = hash_table_lookup(wm->window_hash, id); |
| 2048 | if (window == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2049 | weston_log("set_window_id for unknown window %d\n", id); |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 2050 | return; |
| 2051 | } |
| 2052 | |
Kristian Høgsberg | 082d58c | 2013-06-18 01:00:27 -0400 | [diff] [blame] | 2053 | 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] | 2054 | |
| 2055 | weston_wm_window_read_properties(window); |
| 2056 | |
| 2057 | window->surface = (struct weston_surface *) surface; |
| 2058 | window->surface_destroy_listener.notify = surface_destroy; |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 2059 | wl_signal_add(&surface->destroy_signal, |
Kristian Høgsberg | 380deee | 2012-05-21 17:12:41 -0400 | [diff] [blame] | 2060 | &window->surface_destroy_listener); |
| 2061 | |
| 2062 | weston_wm_window_schedule_repaint(window); |
| 2063 | xserver_map_shell_surface(wm, window); |
| 2064 | } |
| 2065 | |
| 2066 | const struct xserver_interface xserver_implementation = { |
| 2067 | xserver_set_window_id |
| 2068 | }; |