diff options
author | Kristian Høgsberg <krh@bitplanet.net> | 2013-12-03 16:38:15 -0800 |
---|---|---|
committer | Kristian Høgsberg <krh@bitplanet.net> | 2013-12-04 10:20:02 -0800 |
commit | 2ba10df300f91c473e6d282b6d86256f3e3b4c44 (patch) | |
tree | 6e4dac2c1da3c3a084a2452cf049a3e6707b2c1a /xwayland | |
parent | 873b515aeee77ff072f5ae42fef34cab76f8bf11 (diff) | |
download | weston-2ba10df300f91c473e6d282b6d86256f3e3b4c44.tar.gz |
Move xwayland up one directory level
Diffstat (limited to 'xwayland')
-rw-r--r-- | xwayland/Makefile.am | 42 | ||||
-rw-r--r-- | xwayland/dnd.c | 274 | ||||
-rw-r--r-- | xwayland/hash.c | 309 | ||||
-rw-r--r-- | xwayland/hash.h | 49 | ||||
-rw-r--r-- | xwayland/launcher.c | 389 | ||||
-rw-r--r-- | xwayland/selection.c | 712 | ||||
-rw-r--r-- | xwayland/window-manager.c | 2262 | ||||
-rw-r--r-- | xwayland/xwayland.h | 175 |
8 files changed, 4212 insertions, 0 deletions
diff --git a/xwayland/Makefile.am b/xwayland/Makefile.am new file mode 100644 index 00000000..434234aa --- /dev/null +++ b/xwayland/Makefile.am @@ -0,0 +1,42 @@ +AM_CPPFLAGS = \ + -I$(top_srcdir)/shared \ + -I$(top_srcdir)/src \ + -I$(top_builddir)/src \ + -I$(top_builddir)/xwayland \ + -DDATADIR='"$(datadir)"' \ + -DMODULEDIR='"$(moduledir)"' \ + -DLIBEXECDIR='"$(libexecdir)"' \ + -DXSERVER_PATH='"@XSERVER_PATH@"' + +moduledir = @libdir@/weston +module_LTLIBRARIES = xwayland.la + +xwayland = xwayland.la +xwayland_la_LDFLAGS = -module -avoid-version +xwayland_la_LIBADD = \ + $(XWAYLAND_LIBS) \ + $(top_builddir)/shared/libshared-cairo.la +xwayland_la_CFLAGS = \ + $(GCC_CFLAGS) \ + $(COMPOSITOR_CFLAGS) \ + $(PIXMAN_CFLAGS) \ + $(CAIRO_CFLAGS) +xwayland_la_SOURCES = \ + xwayland.h \ + window-manager.c \ + selection.c \ + dnd.c \ + launcher.c \ + xserver-protocol.c \ + xserver-server-protocol.h \ + hash.c \ + hash.h + +BUILT_SOURCES = \ + xserver-protocol.c \ + xserver-server-protocol.h + +CLEANFILES = $(BUILT_SOURCES) + +wayland_protocoldir = $(top_srcdir)/protocol +include $(top_srcdir)/wayland-scanner.mk diff --git a/xwayland/dnd.c b/xwayland/dnd.c new file mode 100644 index 00000000..daeb08de --- /dev/null +++ b/xwayland/dnd.c @@ -0,0 +1,274 @@ +/* + * Copyright © 2013 Intel Corporation + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of the copyright holders not be used in + * advertising or publicity pertaining to distribution of the software + * without specific, written prior permission. The copyright holders make + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "config.h" + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <fcntl.h> +#include <errno.h> +#include <unistd.h> +#include <signal.h> +#include <X11/Xcursor/Xcursor.h> + +#include "xwayland.h" + +#include "cairo-util.h" +#include "compositor.h" +#include "xserver-server-protocol.h" +#include "hash.h" + +static void +weston_dnd_start(struct weston_wm *wm, xcb_window_t owner) +{ + uint32_t values[1], version = 4; + + wm->dnd_window = xcb_generate_id(wm->conn); + values[0] = + XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | + XCB_EVENT_MASK_PROPERTY_CHANGE; + + xcb_create_window(wm->conn, + XCB_COPY_FROM_PARENT, + wm->dnd_window, + wm->screen->root, + 0, 0, + 8192, 8192, + 0, + XCB_WINDOW_CLASS_INPUT_ONLY, + wm->screen->root_visual, + XCB_CW_EVENT_MASK, values); + xcb_change_property(wm->conn, + XCB_PROP_MODE_REPLACE, + wm->dnd_window, + wm->atom.xdnd_aware, + XCB_ATOM_ATOM, + 32, /* format */ + 1, &version); + + xcb_map_window(wm->conn, wm->dnd_window); + wm->dnd_owner = owner; +} + +static void +weston_dnd_stop(struct weston_wm *wm) +{ + xcb_destroy_window(wm->conn, wm->dnd_window); + wm->dnd_window = XCB_WINDOW_NONE; +} + +struct dnd_data_source { + struct weston_data_source base; + struct weston_wm *wm; + int version; + uint32_t window; +}; + +static void +data_source_accept(struct weston_data_source *base, + uint32_t time, const char *mime_type) +{ + struct dnd_data_source *source = (struct dnd_data_source *) base; + xcb_client_message_event_t client_message; + struct weston_wm *wm = source->wm; + + weston_log("got accept, mime-type %s\n", mime_type); + + /* FIXME: If we rewrote UTF8_STRING to + * text/plain;charset=utf-8 and the source doesn't support the + * mime-type, we'll have to rewrite the mime-type back to + * UTF8_STRING here. */ + + client_message.response_type = XCB_CLIENT_MESSAGE; + client_message.format = 32; + client_message.window = wm->dnd_window; + client_message.type = wm->atom.xdnd_status; + client_message.data.data32[0] = wm->dnd_window; + client_message.data.data32[1] = 2; + if (mime_type) + client_message.data.data32[1] |= 1; + client_message.data.data32[2] = 0; + client_message.data.data32[3] = 0; + client_message.data.data32[4] = wm->atom.xdnd_action_copy; + + xcb_send_event(wm->conn, 0, wm->dnd_owner, + XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, + (char *) &client_message); +} + +static void +data_source_send(struct weston_data_source *base, + const char *mime_type, int32_t fd) +{ + struct dnd_data_source *source = (struct dnd_data_source *) base; + struct weston_wm *wm = source->wm; + + weston_log("got send, %s\n", mime_type); + + /* Get data for the utf8_string target */ + xcb_convert_selection(wm->conn, + wm->selection_window, + wm->atom.xdnd_selection, + wm->atom.utf8_string, + wm->atom.wl_selection, + XCB_TIME_CURRENT_TIME); + + xcb_flush(wm->conn); + + fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK); + wm->data_source_fd = fd; +} + +static void +data_source_cancel(struct weston_data_source *source) +{ + weston_log("got cancel\n"); +} + +static void +handle_enter(struct weston_wm *wm, xcb_client_message_event_t *client_message) +{ + struct dnd_data_source *source; + struct weston_seat *seat = weston_wm_pick_seat(wm); + char **p; + const char *name; + uint32_t *types; + int i, length, has_text; + xcb_get_property_cookie_t cookie; + xcb_get_property_reply_t *reply; + + source = malloc(sizeof *source); + if (source == NULL) + return; + + wl_signal_init(&source->base.destroy_signal); + source->base.accept = data_source_accept; + source->base.send = data_source_send; + source->base.cancel = data_source_cancel; + source->wm = wm; + source->window = client_message->data.data32[0]; + source->version = client_message->data.data32[1] >> 24; + + if (client_message->data.data32[1] & 1) { + cookie = xcb_get_property(wm->conn, + 0, /* delete */ + source->window, + wm->atom.xdnd_type_list, + XCB_ATOM_ANY, 0, 2048); + reply = xcb_get_property_reply(wm->conn, cookie, NULL); + types = xcb_get_property_value(reply); + length = reply->value_len; + } else { + reply = NULL; + types = &client_message->data.data32[2]; + length = 3; + } + + wl_array_init(&source->base.mime_types); + has_text = 0; + for (i = 0; i < length; i++) { + if (types[i] == XCB_ATOM_NONE) + continue; + + name = get_atom_name(wm->conn, types[i]); + if (types[i] == wm->atom.utf8_string || + types[i] == wm->atom.text_plain_utf8 || + types[i] == wm->atom.text_plain) { + if (has_text) + continue; + + has_text = 1; + p = wl_array_add(&source->base.mime_types, sizeof *p); + if (p) + *p = strdup("text/plain;charset=utf-8"); + } else if (strchr(name, '/')) { + p = wl_array_add(&source->base.mime_types, sizeof *p); + if (p) + *p = strdup(name); + } + } + + free(reply); + weston_pointer_start_drag(seat->pointer, &source->base, NULL, NULL); +} + +int +weston_wm_handle_dnd_event(struct weston_wm *wm, + xcb_generic_event_t *event) +{ + xcb_xfixes_selection_notify_event_t *xfixes_selection_notify = + (xcb_xfixes_selection_notify_event_t *) event; + xcb_client_message_event_t *client_message = + (xcb_client_message_event_t *) event; + + switch (event->response_type - wm->xfixes->first_event) { + case XCB_XFIXES_SELECTION_NOTIFY: + if (xfixes_selection_notify->selection != wm->atom.xdnd_selection) + return 0; + + weston_log("XdndSelection owner: %d!\n", + xfixes_selection_notify->owner); + + if (xfixes_selection_notify->owner != XCB_WINDOW_NONE) + weston_dnd_start(wm, xfixes_selection_notify->owner); + else + weston_dnd_stop(wm); + + return 1; + } + + switch (EVENT_TYPE(event)) { + case XCB_CLIENT_MESSAGE: + if (client_message->type == wm->atom.xdnd_enter) { + handle_enter(wm, client_message); + return 1; + } else if (client_message->type == wm->atom.xdnd_leave) { + weston_log("got leave!\n"); + return 1; + } else if (client_message->type == wm->atom.xdnd_drop) { + weston_log("got drop!\n"); + return 1; + } else if (client_message->type == wm->atom.xdnd_drop) { + weston_log("got enter!\n"); + return 1; + } + return 0; + } + + return 0; +} + +void +weston_wm_dnd_init(struct weston_wm *wm) +{ + uint32_t mask; + + mask = + XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER | + XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY | + XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE; + xcb_xfixes_select_selection_input(wm->conn, wm->selection_window, + wm->atom.xdnd_selection, mask); +} diff --git a/xwayland/hash.c b/xwayland/hash.c new file mode 100644 index 00000000..54f3de93 --- /dev/null +++ b/xwayland/hash.c @@ -0,0 +1,309 @@ +/* + * Copyright © 2009 Intel Corporation + * Copyright © 1988-2004 Keith Packard and Bart Massey. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * Except as contained in this notice, the names of the authors + * or their institutions shall not be used in advertising or + * otherwise to promote the sale, use or other dealings in this + * Software without prior written authorization from the + * authors. + * + * Authors: + * Eric Anholt <eric@anholt.net> + * Keith Packard <keithp@keithp.com> + */ + +#include <config.h> + +#include <stdlib.h> +#include <stdint.h> + +#include "hash.h" + +struct hash_entry { + uint32_t hash; + void *data; +}; + +struct hash_table { + struct hash_entry *table; + uint32_t size; + uint32_t rehash; + uint32_t max_entries; + uint32_t size_index; + uint32_t entries; + uint32_t deleted_entries; +}; + +#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) + +/* + * From Knuth -- a good choice for hash/rehash values is p, p-2 where + * p and p-2 are both prime. These tables are sized to have an extra 10% + * free to avoid exponential performance degradation as the hash table fills + */ + +static const uint32_t deleted_data; + +static const struct { + uint32_t max_entries, size, rehash; +} hash_sizes[] = { + { 2, 5, 3 }, + { 4, 7, 5 }, + { 8, 13, 11 }, + { 16, 19, 17 }, + { 32, 43, 41 }, + { 64, 73, 71 }, + { 128, 151, 149 }, + { 256, 283, 281 }, + { 512, 571, 569 }, + { 1024, 1153, 1151 }, + { 2048, 2269, 2267 }, + { 4096, 4519, 4517 }, + { 8192, 9013, 9011 }, + { 16384, 18043, 18041 }, + { 32768, 36109, 36107 }, + { 65536, 72091, 72089 }, + { 131072, 144409, 144407 }, + { 262144, 288361, 288359 }, + { 524288, 576883, 576881 }, + { 1048576, 1153459, 1153457 }, + { 2097152, 2307163, 2307161 }, + { 4194304, 4613893, 4613891 }, + { 8388608, 9227641, 9227639 }, + { 16777216, 18455029, 18455027 }, + { 33554432, 36911011, 36911009 }, + { 67108864, 73819861, 73819859 }, + { 134217728, 147639589, 147639587 }, + { 268435456, 295279081, 295279079 }, + { 536870912, 590559793, 590559791 }, + { 1073741824, 1181116273, 1181116271}, + { 2147483648ul, 2362232233ul, 2362232231ul} +}; + +static int +entry_is_free(struct hash_entry *entry) +{ + return entry->data == NULL; +} + +static int +entry_is_deleted(struct hash_entry *entry) +{ + return entry->data == &deleted_data; +} + +static int +entry_is_present(struct hash_entry *entry) +{ + return entry->data != NULL && entry->data != &deleted_data; +} + +struct hash_table * +hash_table_create(void) +{ + struct hash_table *ht; + + ht = malloc(sizeof(*ht)); + if (ht == NULL) + return NULL; + + ht->size_index = 0; + ht->size = hash_sizes[ht->size_index].size; + ht->rehash = hash_sizes[ht->size_index].rehash; + ht->max_entries = hash_sizes[ht->size_index].max_entries; + ht->table = calloc(ht->size, sizeof(*ht->table)); + ht->entries = 0; + ht->deleted_entries = 0; + + if (ht->table == NULL) { + free(ht); + return NULL; + } + + return ht; +} + +/** + * Frees the given hash table. + */ +void +hash_table_destroy(struct hash_table *ht) +{ + if (!ht) + return; + + free(ht->table); + free(ht); +} + +/** + * Finds a hash table entry with the given key and hash of that key. + * + * Returns NULL if no entry is found. Note that the data pointer may be + * modified by the user. + */ +static void * +hash_table_search(struct hash_table *ht, uint32_t hash) +{ + uint32_t hash_address; + + hash_address = hash % ht->size; + do { + uint32_t double_hash; + + struct hash_entry *entry = ht->table + hash_address; + + if (entry_is_free(entry)) { + return NULL; + } else if (entry_is_present(entry) && entry->hash == hash) { + return entry; + } + + double_hash = 1 + hash % ht->rehash; + + hash_address = (hash_address + double_hash) % ht->size; + } while (hash_address != hash % ht->size); + + return NULL; +} + +void +hash_table_for_each(struct hash_table *ht, + hash_table_iterator_func_t func, void *data) +{ + struct hash_entry *entry; + uint32_t i; + + for (i = 0; i < ht->size; i++) { + entry = ht->table + i; + if (entry_is_present(entry)) + func(entry->data, data); + } +} + +void * +hash_table_lookup(struct hash_table *ht, uint32_t hash) +{ + struct hash_entry *entry; + + entry = hash_table_search(ht, hash); + if (entry != NULL) + return entry->data; + + return NULL; +} + +static void +hash_table_rehash(struct hash_table *ht, unsigned int new_size_index) +{ + struct hash_table old_ht; + struct hash_entry *table, *entry; + + if (new_size_index >= ARRAY_SIZE(hash_sizes)) + return; + + table = calloc(hash_sizes[new_size_index].size, sizeof(*ht->table)); + if (table == NULL) + return; + + old_ht = *ht; + + ht->table = table; + ht->size_index = new_size_index; + ht->size = hash_sizes[ht->size_index].size; + ht->rehash = hash_sizes[ht->size_index].rehash; + ht->max_entries = hash_sizes[ht->size_index].max_entries; + ht->entries = 0; + ht->deleted_entries = 0; + + for (entry = old_ht.table; + entry != old_ht.table + old_ht.size; + entry++) { + if (entry_is_present(entry)) { + hash_table_insert(ht, entry->hash, entry->data); + } + } + + free(old_ht.table); +} + +/** + * Inserts the data with the given hash into the table. + * + * Note that insertion may rearrange the table on a resize or rehash, + * so previously found hash_entries are no longer valid after this function. + */ +int +hash_table_insert(struct hash_table *ht, uint32_t hash, void *data) +{ + uint32_t hash_address; + + if (ht->entries >= ht->max_entries) { + hash_table_rehash(ht, ht->size_index + 1); + } else if (ht->deleted_entries + ht->entries >= ht->max_entries) { + hash_table_rehash(ht, ht->size_index); + } + + hash_address = hash % ht->size; + do { + struct hash_entry *entry = ht->table + hash_address; + uint32_t double_hash; + + if (!entry_is_present(entry)) { + if (entry_is_deleted(entry)) + ht->deleted_entries--; + entry->hash = hash; + entry->data = data; + ht->entries++; + return 0; + } + + double_hash = 1 + hash % ht->rehash; + + hash_address = (hash_address + double_hash) % ht->size; + } while (hash_address != hash % ht->size); + + /* We could hit here if a required resize failed. An unchecked-malloc + * application could ignore this result. + */ + return -1; +} + +/** + * This function deletes the given hash table entry. + * + * Note that deletion doesn't otherwise modify the table, so an iteration over + * the table deleting entries is safe. + */ +void +hash_table_remove(struct hash_table *ht, uint32_t hash) +{ + struct hash_entry *entry; + + entry = hash_table_search(ht, hash); + if (entry != NULL) { + entry->data = (void *) &deleted_data; + ht->entries--; + ht->deleted_entries++; + } +} diff --git a/xwayland/hash.h b/xwayland/hash.h new file mode 100644 index 00000000..6e1674e1 --- /dev/null +++ b/xwayland/hash.h @@ -0,0 +1,49 @@ +/* + * Copyright © 2009 Intel Corporation + * Copyright © 1988-2004 Keith Packard and Bart Massey. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * Except as contained in this notice, the names of the authors + * or their institutions shall not be used in advertising or + * otherwise to promote the sale, use or other dealings in this + * Software without prior written authorization from the + * authors. + * + * Authors: + * Eric Anholt <eric@anholt.net> + * Keith Packard <keithp@keithp.com> + */ + +#ifndef HASH_H +#define HASH_H + +struct hash_table; +struct hash_table *hash_table_create(void); +typedef void (*hash_table_iterator_func_t)(void *element, void *data); + +void hash_table_destroy(struct hash_table *ht); +void *hash_table_lookup(struct hash_table *ht, uint32_t hash); +int hash_table_insert(struct hash_table *ht, uint32_t hash, void *data); +void hash_table_remove(struct hash_table *ht, uint32_t hash); +void hash_table_for_each(struct hash_table *ht, + hash_table_iterator_func_t func, void *data); + +#endif diff --git a/xwayland/launcher.c b/xwayland/launcher.c new file mode 100644 index 00000000..8d8e060a --- /dev/null +++ b/xwayland/launcher.c @@ -0,0 +1,389 @@ +/* + * Copyright © 2011 Intel Corporation + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of the copyright holders not be used in + * advertising or publicity pertaining to distribution of the software + * without specific, written prior permission. The copyright holders make + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "config.h" + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <fcntl.h> +#include <errno.h> +#include <unistd.h> +#include <signal.h> + +#include "xwayland.h" +#include "xserver-server-protocol.h" + + +static int +weston_xserver_handle_event(int listen_fd, uint32_t mask, void *data) +{ + struct weston_xserver *wxs = data; + char display[8], s[8]; + int sv[2], client_fd; + char *xserver = NULL; + struct weston_config_section *section; + + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) { + weston_log("socketpair failed\n"); + return 1; + } + + wxs->process.pid = fork(); + switch (wxs->process.pid) { + case 0: + /* SOCK_CLOEXEC closes both ends, so we need to unset + * the flag on the client fd. */ + client_fd = dup(sv[1]); + if (client_fd < 0) + return 1; + + snprintf(s, sizeof s, "%d", client_fd); + setenv("WAYLAND_SOCKET", s, 1); + + snprintf(display, sizeof display, ":%d", wxs->display); + + section = weston_config_get_section(wxs->compositor->config, "xwayland", NULL, NULL); + weston_config_section_get_string(section, "path", &xserver, XSERVER_PATH); + + if (execl(xserver, + xserver, + display, + "-wayland", + "-rootless", + "-retro", + "-nolisten", "all", + "-terminate", + NULL) < 0) + weston_log("exec failed: %m\n"); + free(xserver); + _exit(EXIT_FAILURE); + + default: + weston_log("forked X server, pid %d\n", wxs->process.pid); + + close(sv[1]); + wxs->client = wl_client_create(wxs->wl_display, sv[0]); + + weston_watch_process(&wxs->process); + + wl_event_source_remove(wxs->abstract_source); + wl_event_source_remove(wxs->unix_source); + break; + + case -1: + weston_log( "failed to fork\n"); + break; + } + + return 1; +} + +static void +weston_xserver_shutdown(struct weston_xserver *wxs) +{ + char path[256]; + + snprintf(path, sizeof path, "/tmp/.X%d-lock", wxs->display); + unlink(path); + snprintf(path, sizeof path, "/tmp/.X11-unix/X%d", wxs->display); + unlink(path); + if (wxs->process.pid == 0) { + wl_event_source_remove(wxs->abstract_source); + wl_event_source_remove(wxs->unix_source); + } + close(wxs->abstract_fd); + close(wxs->unix_fd); + if (wxs->wm) + weston_wm_destroy(wxs->wm); + wxs->loop = NULL; +} + +static void +weston_xserver_cleanup(struct weston_process *process, int status) +{ + struct weston_xserver *wxs = + container_of(process, struct weston_xserver, process); + + wxs->process.pid = 0; + wxs->client = NULL; + wxs->resource = NULL; + + wxs->abstract_source = + wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd, + WL_EVENT_READABLE, + weston_xserver_handle_event, wxs); + + wxs->unix_source = + wl_event_loop_add_fd(wxs->loop, wxs->unix_fd, + WL_EVENT_READABLE, + weston_xserver_handle_event, wxs); + + if (wxs->wm) { + weston_log("xserver exited, code %d\n", status); + weston_wm_destroy(wxs->wm); + wxs->wm = NULL; + } else { + /* If the X server crashes before it binds to the + * xserver interface, shut down and don't try + * again. */ + weston_log("xserver crashing too fast: %d\n", status); + weston_xserver_shutdown(wxs); + } +} + +static void +bind_xserver(struct wl_client *client, + void *data, uint32_t version, uint32_t id) +{ + struct weston_xserver *wxs = data; + + /* If it's a different client than the xserver we launched, + * don't start the wm. */ + if (client != wxs->client) + return; + + wxs->resource = + wl_resource_create(client, &xserver_interface, + 1, id); + wl_resource_set_implementation(wxs->resource, &xserver_implementation, + wxs, NULL); + + wxs->wm = weston_wm_create(wxs); + if (wxs->wm == NULL) { + weston_log("failed to create wm\n"); + } + + xserver_send_listen_socket(wxs->resource, wxs->abstract_fd); + xserver_send_listen_socket(wxs->resource, wxs->unix_fd); +} + +static int +bind_to_abstract_socket(int display) +{ + struct sockaddr_un addr; + socklen_t size, name_size; + int fd; + + fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); + if (fd < 0) + return -1; + + addr.sun_family = AF_LOCAL; + name_size = snprintf(addr.sun_path, sizeof addr.sun_path, + "%c/tmp/.X11-unix/X%d", 0, display); + size = offsetof(struct sockaddr_un, sun_path) + name_size; + if (bind(fd, (struct sockaddr *) &addr, size) < 0) { + weston_log("failed to bind to @%s: %s\n", + addr.sun_path + 1, strerror(errno)); + close(fd); + return -1; + } + + if (listen(fd, 1) < 0) { + close(fd); + return -1; + } + + return fd; +} + +static int +bind_to_unix_socket(int display) +{ + struct sockaddr_un addr; + socklen_t size, name_size; + int fd; + + fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); + if (fd < 0) + return -1; + + addr.sun_family = AF_LOCAL; + name_size = snprintf(addr.sun_path, sizeof addr.sun_path, + "/tmp/.X11-unix/X%d", display) + 1; + size = offsetof(struct sockaddr_un, sun_path) + name_size; + unlink(addr.sun_path); + if (bind(fd, (struct sockaddr *) &addr, size) < 0) { + weston_log("failed to bind to %s (%s)\n", + addr.sun_path, strerror(errno)); + close(fd); + return -1; + } + + if (listen(fd, 1) < 0) { + unlink(addr.sun_path); + close(fd); + return -1; + } + + return fd; +} + +static int +create_lockfile(int display, char *lockfile, size_t lsize) +{ + char pid[16], *end; + int fd, size; + pid_t other; + + snprintf(lockfile, lsize, "/tmp/.X%d-lock", display); + fd = open(lockfile, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0444); + if (fd < 0 && errno == EEXIST) { + fd = open(lockfile, O_CLOEXEC, O_RDONLY); + if (fd < 0 || read(fd, pid, 11) != 11) { + weston_log("can't read lock file %s: %s\n", + lockfile, strerror(errno)); + if (fd >= 0) + close (fd); + + errno = EEXIST; + return -1; + } + + other = strtol(pid, &end, 0); + if (end != pid + 10) { + weston_log("can't parse lock file %s\n", + lockfile); + close(fd); + errno = EEXIST; + return -1; + } + + if (kill(other, 0) < 0 && errno == ESRCH) { + /* stale lock file; unlink and try again */ + weston_log("unlinking stale lock file %s\n", lockfile); + close(fd); + if (unlink(lockfile)) + /* If we fail to unlink, return EEXIST + so we try the next display number.*/ + errno = EEXIST; + else + errno = EAGAIN; + return -1; + } + + close(fd); + errno = EEXIST; + return -1; + } else if (fd < 0) { + weston_log("failed to create lock file %s: %s\n", + lockfile, strerror(errno)); + return -1; + } + + /* Subtle detail: we use the pid of the wayland + * compositor, not the xserver in the lock file. */ + size = snprintf(pid, sizeof pid, "%10d\n", getpid()); + if (write(fd, pid, size) != size) { + unlink(lockfile); + close(fd); + return -1; + } + + close(fd); + + return 0; +} + +static void +weston_xserver_destroy(struct wl_listener *l, void *data) +{ + struct weston_xserver *wxs = + container_of(l, struct weston_xserver, destroy_listener); + + if (!wxs) + return; + + if (wxs->loop) + weston_xserver_shutdown(wxs); + + free(wxs); +} + +WL_EXPORT int +module_init(struct weston_compositor *compositor, + int *argc, char *argv[]) + +{ + struct wl_display *display = compositor->wl_display; + struct weston_xserver *wxs; + char lockfile[256], display_name[8]; + + wxs = zalloc(sizeof *wxs); + wxs->process.cleanup = weston_xserver_cleanup; + wxs->wl_display = display; + wxs->compositor = compositor; + + wxs->display = 0; + + retry: + if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) { + if (errno == EAGAIN) { + goto retry; + } else if (errno == EEXIST) { + wxs->display++; + goto retry; + } else { + free(wxs); + return -1; + } + } + + wxs->abstract_fd = bind_to_abstract_socket(wxs->display); + if (wxs->abstract_fd < 0 && errno == EADDRINUSE) { + wxs->display++; + unlink(lockfile); + goto retry; + } + + wxs->unix_fd = bind_to_unix_socket(wxs->display); + if (wxs->unix_fd < 0) { + unlink(lockfile); + close(wxs->abstract_fd); + free(wxs); + return -1; + } + + snprintf(display_name, sizeof display_name, ":%d", wxs->display); + weston_log("xserver listening on display %s\n", display_name); + setenv("DISPLAY", display_name, 1); + + wxs->loop = wl_display_get_event_loop(display); + wxs->abstract_source = + wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd, + WL_EVENT_READABLE, + weston_xserver_handle_event, wxs); + wxs->unix_source = + wl_event_loop_add_fd(wxs->loop, wxs->unix_fd, + WL_EVENT_READABLE, + weston_xserver_handle_event, wxs); + + wl_global_create(display, &xserver_interface, 1, wxs, bind_xserver); + + wxs->destroy_listener.notify = weston_xserver_destroy; + wl_signal_add(&compositor->destroy_signal, &wxs->destroy_listener); + + return 0; +} diff --git a/xwayland/selection.c b/xwayland/selection.c new file mode 100644 index 00000000..b694477e --- /dev/null +++ b/xwayland/selection.c @@ -0,0 +1,712 @@ +/* + * Copyright © 2012 Intel Corporation + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of the copyright holders not be used in + * advertising or publicity pertaining to distribution of the software + * without specific, written prior permission. The copyright holders make + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "config.h" + +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> + +#include "xwayland.h" + +static int +writable_callback(int fd, uint32_t mask, void *data) +{ + struct weston_wm *wm = data; + unsigned char *property; + int len, remainder; + + property = xcb_get_property_value(wm->property_reply); + remainder = xcb_get_property_value_length(wm->property_reply) - + wm->property_start; + + len = write(fd, property + wm->property_start, remainder); + if (len == -1) { + free(wm->property_reply); + wm->property_reply = NULL; + if (wm->property_source) + wl_event_source_remove(wm->property_source); + close(fd); + weston_log("write error to target fd: %m\n"); + return 1; + } + + weston_log("wrote %d (chunk size %d) of %d bytes\n", + wm->property_start + len, + len, xcb_get_property_value_length(wm->property_reply)); + + wm->property_start += len; + if (len == remainder) { + free(wm->property_reply); + wm->property_reply = NULL; + if (wm->property_source) + wl_event_source_remove(wm->property_source); + + if (wm->incr) { + xcb_delete_property(wm->conn, + wm->selection_window, + wm->atom.wl_selection); + } else { + weston_log("transfer complete\n"); + close(fd); + } + } + + return 1; +} + +static void +weston_wm_write_property(struct weston_wm *wm, xcb_get_property_reply_t *reply) +{ + wm->property_start = 0; + wm->property_reply = reply; + writable_callback(wm->data_source_fd, WL_EVENT_WRITABLE, wm); + + if (wm->property_reply) + wm->property_source = + wl_event_loop_add_fd(wm->server->loop, + wm->data_source_fd, + WL_EVENT_WRITABLE, + writable_callback, wm); +} + +static void +weston_wm_get_incr_chunk(struct weston_wm *wm) +{ + xcb_get_property_cookie_t cookie; + xcb_get_property_reply_t *reply; + + cookie = xcb_get_property(wm->conn, + 0, /* delete */ + wm->selection_window, + wm->atom.wl_selection, + XCB_GET_PROPERTY_TYPE_ANY, + 0, /* offset */ + 0x1fffffff /* length */); + + reply = xcb_get_property_reply(wm->conn, cookie, NULL); + + dump_property(wm, wm->atom.wl_selection, reply); + + if (xcb_get_property_value_length(reply) > 0) { + weston_wm_write_property(wm, reply); + } else { + weston_log("transfer complete\n"); + close(wm->data_source_fd); + free(reply); + } +} + +struct x11_data_source { + struct weston_data_source base; + struct weston_wm *wm; +}; + +static void +data_source_accept(struct weston_data_source *source, + uint32_t time, const char *mime_type) +{ +} + +static void +data_source_send(struct weston_data_source *base, + const char *mime_type, int32_t fd) +{ + struct x11_data_source *source = (struct x11_data_source *) base; + struct weston_wm *wm = source->wm; + + if (strcmp(mime_type, "text/plain;charset=utf-8") == 0) { + /* Get data for the utf8_string target */ + xcb_convert_selection(wm->conn, + wm->selection_window, + wm->atom.clipboard, + wm->atom.utf8_string, + wm->atom.wl_selection, + XCB_TIME_CURRENT_TIME); + + xcb_flush(wm->conn); + + fcntl(fd, F_SETFL, O_WRONLY | O_NONBLOCK); + wm->data_source_fd = fd; + } +} + +static void +data_source_cancel(struct weston_data_source *source) +{ +} + +static void +weston_wm_get_selection_targets(struct weston_wm *wm) +{ + struct x11_data_source *source; + struct weston_compositor *compositor; + struct weston_seat *seat = weston_wm_pick_seat(wm); + xcb_get_property_cookie_t cookie; + xcb_get_property_reply_t *reply; + xcb_atom_t *value; + char **p; + uint32_t i; + + cookie = xcb_get_property(wm->conn, + 1, /* delete */ + wm->selection_window, + wm->atom.wl_selection, + XCB_GET_PROPERTY_TYPE_ANY, + 0, /* offset */ + 4096 /* length */); + + reply = xcb_get_property_reply(wm->conn, cookie, NULL); + + dump_property(wm, wm->atom.wl_selection, reply); + + if (reply->type != XCB_ATOM_ATOM) { + free(reply); + return; + } + + source = malloc(sizeof *source); + if (source == NULL) + return; + + wl_signal_init(&source->base.destroy_signal); + source->base.accept = data_source_accept; + source->base.send = data_source_send; + source->base.cancel = data_source_cancel; + source->wm = wm; + + wl_array_init(&source->base.mime_types); + value = xcb_get_property_value(reply); + for (i = 0; i < reply->value_len; i++) { + if (value[i] == wm->atom.utf8_string) { + p = wl_array_add(&source->base.mime_types, sizeof *p); + if (p) + *p = strdup("text/plain;charset=utf-8"); + } + } + + compositor = wm->server->compositor; + weston_seat_set_selection(seat, &source->base, + wl_display_next_serial(compositor->wl_display)); + + free(reply); +} + +static void +weston_wm_get_selection_data(struct weston_wm *wm) +{ + xcb_get_property_cookie_t cookie; + xcb_get_property_reply_t *reply; + + cookie = xcb_get_property(wm->conn, + 1, /* delete */ + wm->selection_window, + wm->atom.wl_selection, + XCB_GET_PROPERTY_TYPE_ANY, + 0, /* offset */ + 0x1fffffff /* length */); + + reply = xcb_get_property_reply(wm->conn, cookie, NULL); + + if (reply->type == wm->atom.incr) { + dump_property(wm, wm->atom.wl_selection, reply); + wm->incr = 1; + free(reply); + } else { + dump_property(wm, wm->atom.wl_selection, reply); + wm->incr = 0; + weston_wm_write_property(wm, reply); + } +} + +static void +weston_wm_handle_selection_notify(struct weston_wm *wm, + xcb_generic_event_t *event) +{ + xcb_selection_notify_event_t *selection_notify = + (xcb_selection_notify_event_t *) event; + + if (selection_notify->property == XCB_ATOM_NONE) { + /* convert selection failed */ + } else if (selection_notify->target == wm->atom.targets) { + weston_wm_get_selection_targets(wm); + } else { + weston_wm_get_selection_data(wm); + } +} + +static const size_t incr_chunk_size = 64 * 1024; + +static void +weston_wm_send_selection_notify(struct weston_wm *wm, xcb_atom_t property) +{ + xcb_selection_notify_event_t selection_notify; + + memset(&selection_notify, 0, sizeof selection_notify); + selection_notify.response_type = XCB_SELECTION_NOTIFY; + selection_notify.sequence = 0; + selection_notify.time = wm->selection_request.time; + selection_notify.requestor = wm->selection_request.requestor; + selection_notify.selection = wm->selection_request.selection; + selection_notify.target = wm->selection_request.target; + selection_notify.property = property; + + xcb_send_event(wm->conn, 0, /* propagate */ + wm->selection_request.requestor, + XCB_EVENT_MASK_NO_EVENT, (char *) &selection_notify); +} + +static void +weston_wm_send_targets(struct weston_wm *wm) +{ + xcb_atom_t targets[] = { + wm->atom.timestamp, + wm->atom.targets, + wm->atom.utf8_string, + /* wm->atom.compound_text, */ + wm->atom.text, + /* wm->atom.string */ + }; + + xcb_change_property(wm->conn, + XCB_PROP_MODE_REPLACE, + wm->selection_request.requestor, + wm->selection_request.property, + XCB_ATOM_ATOM, + 32, /* format */ + ARRAY_LENGTH(targets), targets); + + weston_wm_send_selection_notify(wm, wm->selection_request.property); +} + +static void +weston_wm_send_timestamp(struct weston_wm *wm) +{ + xcb_change_property(wm->conn, + XCB_PROP_MODE_REPLACE, + wm->selection_request.requestor, + wm->selection_request.property, + XCB_ATOM_INTEGER, + 32, /* format */ + 1, &wm->selection_timestamp); + + weston_wm_send_selection_notify(wm, wm->selection_request.property); +} + +static int +weston_wm_flush_source_data(struct weston_wm *wm) +{ + int length; + + xcb_change_property(wm->conn, + XCB_PROP_MODE_REPLACE, + wm->selection_request.requestor, + wm->selection_request.property, + wm->selection_target, + 8, /* format */ + wm->source_data.size, + wm->source_data.data); + wm->selection_property_set = 1; + length = wm->source_data.size; + wm->source_data.size = 0; + + return length; +} + +static int +weston_wm_read_data_source(int fd, uint32_t mask, void *data) +{ + struct weston_wm *wm = data; + int len, current, available; + void *p; + + current = wm->source_data.size; + if (wm->source_data.size < incr_chunk_size) + p = wl_array_add(&wm->source_data, incr_chunk_size); + else + p = (char *) wm->source_data.data + wm->source_data.size; + available = wm->source_data.alloc - current; + + len = read(fd, p, available); + if (len == -1) { + weston_log("read error from data source: %m\n"); + weston_wm_send_selection_notify(wm, XCB_ATOM_NONE); + wl_event_source_remove(wm->property_source); + close(fd); + wl_array_release(&wm->source_data); + } + + weston_log("read %d (available %d, mask 0x%x) bytes: \"%.*s\"\n", + len, available, mask, len, (char *) p); + + wm->source_data.size = current + len; + if (wm->source_data.size >= incr_chunk_size) { + if (!wm->incr) { + weston_log("got %zu bytes, starting incr\n", + wm->source_data.size); + wm->incr = 1; + xcb_change_property(wm->conn, + XCB_PROP_MODE_REPLACE, + wm->selection_request.requestor, + wm->selection_request.property, + wm->atom.incr, + 32, /* format */ + 1, &incr_chunk_size); + wm->selection_property_set = 1; + wm->flush_property_on_delete = 1; + wl_event_source_remove(wm->property_source); + weston_wm_send_selection_notify(wm, wm->selection_request.property); + } else if (wm->selection_property_set) { + weston_log("got %zu bytes, waiting for " + "property delete\n", wm->source_data.size); + + wm->flush_property_on_delete = 1; + wl_event_source_remove(wm->property_source); + } else { + weston_log("got %zu bytes, " + "property deleted, seting new property\n", + wm->source_data.size); + weston_wm_flush_source_data(wm); + } + } else if (len == 0 && !wm->incr) { + weston_log("non-incr transfer complete\n"); + /* Non-incr transfer all done. */ + weston_wm_flush_source_data(wm); + weston_wm_send_selection_notify(wm, wm->selection_request.property); + xcb_flush(wm->conn); + wl_event_source_remove(wm->property_source); + close(fd); + wl_array_release(&wm->source_data); + wm->selection_request.requestor = XCB_NONE; + } else if (len == 0 && wm->incr) { + weston_log("incr transfer complete\n"); + + wm->flush_property_on_delete = 1; + if (wm->selection_property_set) { + weston_log("got %zu bytes, waiting for " + "property delete\n", wm->source_data.size); + } else { + weston_log("got %zu bytes, " + "property deleted, seting new property\n", + wm->source_data.size); + weston_wm_flush_source_data(wm); + } + xcb_flush(wm->conn); + wl_event_source_remove(wm->property_source); + close(wm->data_source_fd); + wm->data_source_fd = -1; + close(fd); + } else { + weston_log("nothing happened, buffered the bytes\n"); + } + + return 1; +} + +static void +weston_wm_send_data(struct weston_wm *wm, xcb_atom_t target, const char *mime_type) +{ + struct weston_data_source *source; + struct weston_seat *seat = weston_wm_pick_seat(wm); + int p[2]; + + if (pipe2(p, O_CLOEXEC | O_NONBLOCK) == -1) { + weston_log("pipe2 failed: %m\n"); + weston_wm_send_selection_notify(wm, XCB_ATOM_NONE); + return; + } + + wl_array_init(&wm->source_data); + wm->selection_target = target; + wm->data_source_fd = p[0]; + wm->property_source = wl_event_loop_add_fd(wm->server->loop, + wm->data_source_fd, + WL_EVENT_READABLE, + weston_wm_read_data_source, + wm); + + source = seat->selection_data_source; + source->send(source, mime_type, p[1]); + close(p[1]); +} + +static void +weston_wm_send_incr_chunk(struct weston_wm *wm) +{ + int length; + + weston_log("property deleted\n"); + + wm->selection_property_set = 0; + if (wm->flush_property_on_delete) { + weston_log("setting new property, %zu bytes\n", + wm->source_data.size); + wm->flush_property_on_delete = 0; + length = weston_wm_flush_source_data(wm); + + if (wm->data_source_fd >= 0) { + wm->property_source = + wl_event_loop_add_fd(wm->server->loop, + wm->data_source_fd, + WL_EVENT_READABLE, + weston_wm_read_data_source, + wm); + } else if (length > 0) { + /* Transfer is all done, but queue a flush for + * the delete of the last chunk so we can set + * the 0 sized propert to signal the end of + * the transfer. */ + wm->flush_property_on_delete = 1; + wl_array_release(&wm->source_data); + } else { + wm->selection_request.requestor = XCB_NONE; + } + } +} + +static int +weston_wm_handle_selection_property_notify(struct weston_wm *wm, + xcb_generic_event_t *event) +{ + xcb_property_notify_event_t *property_notify = + (xcb_property_notify_event_t *) event; + + if (property_notify->window == wm->selection_window) { + if (property_notify->state == XCB_PROPERTY_NEW_VALUE && + property_notify->atom == wm->atom.wl_selection && + wm->incr) + weston_wm_get_incr_chunk(wm); + return 1; + } else if (property_notify->window == wm->selection_request.requestor) { + if (property_notify->state == XCB_PROPERTY_DELETE && + property_notify->atom == wm->selection_request.property && + wm->incr) + weston_wm_send_incr_chunk(wm); + return 1; + } + + return 0; +} + +static void +weston_wm_handle_selection_request(struct weston_wm *wm, + xcb_generic_event_t *event) +{ + xcb_selection_request_event_t *selection_request = + (xcb_selection_request_event_t *) event; + + weston_log("selection request, %s, ", + get_atom_name(wm->conn, selection_request->selection)); + weston_log_continue("target %s, ", + get_atom_name(wm->conn, selection_request->target)); + weston_log_continue("property %s\n", + get_atom_name(wm->conn, selection_request->property)); + + wm->selection_request = *selection_request; + wm->incr = 0; + wm->flush_property_on_delete = 0; + + if (selection_request->selection == wm->atom.clipboard_manager) { + /* The weston clipboard should already have grabbed + * the first target, so just send selection notify + * now. This isn't synchronized with the clipboard + * finishing getting the data, so there's a race here. */ + weston_wm_send_selection_notify(wm, wm->selection_request.property); + return; + } + + if (selection_request->target == wm->atom.targets) { + weston_wm_send_targets(wm); + } else if (selection_request->target == wm->atom.timestamp) { + weston_wm_send_timestamp(wm); + } else if (selection_request->target == wm->atom.utf8_string || + selection_request->target == wm->atom.text) { + weston_wm_send_data(wm, wm->atom.utf8_string, + "text/plain;charset=utf-8"); + } else { + weston_log("can only handle UTF8_STRING targets...\n"); + weston_wm_send_selection_notify(wm, XCB_ATOM_NONE); + } +} + +static int +weston_wm_handle_xfixes_selection_notify(struct weston_wm *wm, + xcb_generic_event_t *event) +{ + xcb_xfixes_selection_notify_event_t *xfixes_selection_notify = + (xcb_xfixes_selection_notify_event_t *) event; + struct weston_compositor *compositor; + struct weston_seat *seat = weston_wm_pick_seat(wm); + uint32_t serial; + + if (xfixes_selection_notify->selection != wm->atom.clipboard) + return 0; + + weston_log("xfixes selection notify event: owner %d\n", + xfixes_selection_notify->owner); + + if (xfixes_selection_notify->owner == XCB_WINDOW_NONE) { + if (wm->selection_owner != wm->selection_window) { + /* A real X client selection went away, not our + * proxy selection. Clear the wayland selection. */ + compositor = wm->server->compositor; + serial = wl_display_next_serial(compositor->wl_display); + weston_seat_set_selection(seat, NULL, serial); + } + + wm->selection_owner = XCB_WINDOW_NONE; + + return 1; + } + + wm->selection_owner = xfixes_selection_notify->owner; + + /* We have to use XCB_TIME_CURRENT_TIME when we claim the + * selection, so grab the actual timestamp here so we can + * answer TIMESTAMP conversion requests correctly. */ + if (xfixes_selection_notify->owner == wm->selection_window) { + wm->selection_timestamp = xfixes_selection_notify->timestamp; + weston_log("our window, skipping\n"); + return 1; + } + + wm->incr = 0; + xcb_convert_selection(wm->conn, wm->selection_window, + wm->atom.clipboard, + wm->atom.targets, + wm->atom.wl_selection, + xfixes_selection_notify->timestamp); + + xcb_flush(wm->conn); + + return 1; +} + +int +weston_wm_handle_selection_event(struct weston_wm *wm, + xcb_generic_event_t *event) +{ + switch (event->response_type & ~0x80) { + case XCB_SELECTION_NOTIFY: + weston_wm_handle_selection_notify(wm, event); + return 1; + case XCB_PROPERTY_NOTIFY: + return weston_wm_handle_selection_property_notify(wm, event); + case XCB_SELECTION_REQUEST: + weston_wm_handle_selection_request(wm, event); + return 1; + } + + switch (event->response_type - wm->xfixes->first_event) { + case XCB_XFIXES_SELECTION_NOTIFY: + return weston_wm_handle_xfixes_selection_notify(wm, event); + } + + return 0; +} + +static void +weston_wm_set_selection(struct wl_listener *listener, void *data) +{ + struct weston_seat *seat = data; + struct weston_wm *wm = + container_of(listener, struct weston_wm, selection_listener); + struct weston_data_source *source = seat->selection_data_source; + const char **p, **end; + int has_text_plain = 0; + + if (source == NULL) { + if (wm->selection_owner == wm->selection_window) + xcb_set_selection_owner(wm->conn, + XCB_ATOM_NONE, + wm->atom.clipboard, + wm->selection_timestamp); + return; + } + + if (source->send == data_source_send) + return; + + p = source->mime_types.data; + end = (const char **) + ((char *) source->mime_types.data + source->mime_types.size); + while (p < end) { + weston_log(" %s\n", *p); + if (strcmp(*p, "text/plain") == 0 || + strcmp(*p, "text/plain;charset=utf-8") == 0) + has_text_plain = 1; + p++; + } + + if (has_text_plain) { + xcb_set_selection_owner(wm->conn, + wm->selection_window, + wm->atom.clipboard, + XCB_TIME_CURRENT_TIME); + } else { + xcb_set_selection_owner(wm->conn, + XCB_ATOM_NONE, + wm->atom.clipboard, + XCB_TIME_CURRENT_TIME); + } +} + +void +weston_wm_selection_init(struct weston_wm *wm) +{ + struct weston_seat *seat; + uint32_t values[1], mask; + + wm->selection_request.requestor = XCB_NONE; + + values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE; + wm->selection_window = xcb_generate_id(wm->conn); + xcb_create_window(wm->conn, + XCB_COPY_FROM_PARENT, + wm->selection_window, + wm->screen->root, + 0, 0, + 10, 10, + 0, + XCB_WINDOW_CLASS_INPUT_OUTPUT, + wm->screen->root_visual, + XCB_CW_EVENT_MASK, values); + + xcb_set_selection_owner(wm->conn, + wm->selection_window, + wm->atom.clipboard_manager, + XCB_TIME_CURRENT_TIME); + + mask = + XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER | + XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_WINDOW_DESTROY | + XCB_XFIXES_SELECTION_EVENT_MASK_SELECTION_CLIENT_CLOSE; + xcb_xfixes_select_selection_input(wm->conn, wm->selection_window, + wm->atom.clipboard, mask); + + seat = weston_wm_pick_seat(wm); + wm->selection_listener.notify = weston_wm_set_selection; + wl_signal_add(&seat->selection_signal, &wm->selection_listener); + + weston_wm_set_selection(&wm->selection_listener, seat); +} diff --git a/xwayland/window-manager.c b/xwayland/window-manager.c new file mode 100644 index 00000000..4bce02dd --- /dev/null +++ b/xwayland/window-manager.c @@ -0,0 +1,2262 @@ +/* + * Copyright © 2011 Intel Corporation + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of the copyright holders not be used in + * advertising or publicity pertaining to distribution of the software + * without specific, written prior permission. The copyright holders make + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "config.h" + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <fcntl.h> +#include <errno.h> +#include <unistd.h> +#include <signal.h> +#include <X11/Xcursor/Xcursor.h> +#include <linux/input.h> + +#include "xwayland.h" + +#include "cairo-util.h" +#include "compositor.h" +#include "xserver-server-protocol.h" +#include "hash.h" + +struct wm_size_hints { + uint32_t flags; + int32_t x, y; + int32_t width, height; /* should set so old wm's don't mess up */ + int32_t min_width, min_height; + int32_t max_width, max_height; + int32_t width_inc, height_inc; + struct { + int32_t x; + int32_t y; + } min_aspect, max_aspect; + int32_t base_width, base_height; + int32_t win_gravity; +}; + +#define USPosition (1L << 0) +#define USSize (1L << 1) +#define PPosition (1L << 2) +#define PSize (1L << 3) +#define PMinSize (1L << 4) +#define PMaxSize (1L << 5) +#define PResizeInc (1L << 6) +#define PAspect (1L << 7) +#define PBaseSize (1L << 8) +#define PWinGravity (1L << 9) + +struct motif_wm_hints { + uint32_t flags; + uint32_t functions; + uint32_t decorations; + int32_t input_mode; + uint32_t status; +}; + +#define MWM_HINTS_FUNCTIONS (1L << 0) +#define MWM_HINTS_DECORATIONS (1L << 1) +#define MWM_HINTS_INPUT_MODE (1L << 2) +#define MWM_HINTS_STATUS (1L << 3) + +#define MWM_FUNC_ALL (1L << 0) +#define MWM_FUNC_RESIZE (1L << 1) +#define MWM_FUNC_MOVE (1L << 2) +#define MWM_FUNC_MINIMIZE (1L << 3) +#define MWM_FUNC_MAXIMIZE (1L << 4) +#define MWM_FUNC_CLOSE (1L << 5) + +#define MWM_DECOR_ALL (1L << 0) +#define MWM_DECOR_BORDER (1L << 1) +#define MWM_DECOR_RESIZEH (1L << 2) +#define MWM_DECOR_TITLE (1L << 3) +#define MWM_DECOR_MENU (1L << 4) +#define MWM_DECOR_MINIMIZE (1L << 5) +#define MWM_DECOR_MAXIMIZE (1L << 6) + +#define MWM_INPUT_MODELESS 0 +#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1 +#define MWM_INPUT_SYSTEM_MODAL 2 +#define MWM_INPUT_FULL_APPLICATION_MODAL 3 +#define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL + +#define MWM_TEAROFF_WINDOW (1L<<0) + +#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0 +#define _NET_WM_MOVERESIZE_SIZE_TOP 1 +#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2 +#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3 +#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4 +#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5 +#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6 +#define _NET_WM_MOVERESIZE_SIZE_LEFT 7 +#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */ +#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */ +#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */ +#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */ + +struct weston_wm_window { + struct weston_wm *wm; + xcb_window_t id; + xcb_window_t frame_id; + struct frame *frame; + cairo_surface_t *cairo_surface; + struct weston_surface *surface; + struct shell_surface *shsurf; + struct weston_view *view; + struct wl_listener surface_destroy_listener; + struct wl_event_source *repaint_source; + struct wl_event_source *configure_source; + int properties_dirty; + int pid; + char *machine; + char *class; + char *name; + struct weston_wm_window *transient_for; + uint32_t protocols; + xcb_atom_t type; + int width, height; + int x, y; + int saved_width, saved_height; + int decorate; + int override_redirect; + int fullscreen; + int has_alpha; + int delete_window; + struct wm_size_hints size_hints; + struct motif_wm_hints motif_hints; +}; + +static struct weston_wm_window * +get_wm_window(struct weston_surface *surface); + +static void +weston_wm_window_schedule_repaint(struct weston_wm_window *window); + +static int __attribute__ ((format (printf, 1, 2))) +wm_log(const char *fmt, ...) +{ +#ifdef WM_DEBUG + int l; + va_list argp; + + va_start(argp, fmt); + l = weston_vlog(fmt, argp); + va_end(argp); + + return l; +#else + return 0; +#endif +} + +static int __attribute__ ((format (printf, 1, 2))) +wm_log_continue(const char *fmt, ...) +{ +#ifdef WM_DEBUG + int l; + va_list argp; + + va_start(argp, fmt); + l = weston_vlog_continue(fmt, argp); + va_end(argp); + + return l; +#else + return 0; +#endif +} + + +const char * +get_atom_name(xcb_connection_t *c, xcb_atom_t atom) +{ + xcb_get_atom_name_cookie_t cookie; + xcb_get_atom_name_reply_t *reply; + xcb_generic_error_t *e; + static char buffer[64]; + + if (atom == XCB_ATOM_NONE) + return "None"; + + cookie = xcb_get_atom_name (c, atom); + reply = xcb_get_atom_name_reply (c, cookie, &e); + + if(reply) { + snprintf(buffer, sizeof buffer, "%.*s", + xcb_get_atom_name_name_length (reply), + xcb_get_atom_name_name (reply)); + } else { + snprintf(buffer, sizeof buffer, "(atom %u)", atom); + } + + free(reply); + + return buffer; +} + +static xcb_cursor_t +xcb_cursor_image_load_cursor(struct weston_wm *wm, const XcursorImage *img) +{ + xcb_connection_t *c = wm->conn; + xcb_screen_iterator_t s = xcb_setup_roots_iterator(xcb_get_setup(c)); + xcb_screen_t *screen = s.data; + xcb_gcontext_t gc; + xcb_pixmap_t pix; + xcb_render_picture_t pic; + xcb_cursor_t cursor; + int stride = img->width * 4; + + pix = xcb_generate_id(c); + xcb_create_pixmap(c, 32, pix, screen->root, img->width, img->height); + + pic = xcb_generate_id(c); + xcb_render_create_picture(c, pic, pix, wm->format_rgba.id, 0, 0); + + gc = xcb_generate_id(c); + xcb_create_gc(c, gc, pix, 0, 0); + + xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc, + img->width, img->height, 0, 0, 0, 32, + stride * img->height, (uint8_t *) img->pixels); + xcb_free_gc(c, gc); + + cursor = xcb_generate_id(c); + xcb_render_create_cursor(c, cursor, pic, img->xhot, img->yhot); + + xcb_render_free_picture(c, pic); + xcb_free_pixmap(c, pix); + + return cursor; +} + +static xcb_cursor_t +xcb_cursor_images_load_cursor(struct weston_wm *wm, const XcursorImages *images) +{ + /* TODO: treat animated cursors as well */ + if (images->nimage != 1) + return -1; + + return xcb_cursor_image_load_cursor(wm, images->images[0]); +} + +static xcb_cursor_t +xcb_cursor_library_load_cursor(struct weston_wm *wm, const char *file) +{ + xcb_cursor_t cursor; + XcursorImages *images; + char *v = NULL; + int size = 0; + + if (!file) + return 0; + + v = getenv ("XCURSOR_SIZE"); + if (v) + size = atoi(v); + + if (!size) + size = 32; + + images = XcursorLibraryLoadImages (file, NULL, size); + if (!images) + return -1; + + cursor = xcb_cursor_images_load_cursor (wm, images); + XcursorImagesDestroy (images); + + return cursor; +} + +void +dump_property(struct weston_wm *wm, + xcb_atom_t property, xcb_get_property_reply_t *reply) +{ + int32_t *incr_value; + const char *text_value, *name; + xcb_atom_t *atom_value; + int width, len; + uint32_t i; + + width = wm_log_continue("%s: ", get_atom_name(wm->conn, property)); + if (reply == NULL) { + wm_log_continue("(no reply)\n"); + return; + } + + width += wm_log_continue("%s/%d, length %d (value_len %d): ", + get_atom_name(wm->conn, reply->type), + reply->format, + xcb_get_property_value_length(reply), + reply->value_len); + + if (reply->type == wm->atom.incr) { + incr_value = xcb_get_property_value(reply); + wm_log_continue("%d\n", *incr_value); + } else if (reply->type == wm->atom.utf8_string || + reply->type == wm->atom.string) { + text_value = xcb_get_property_value(reply); + if (reply->value_len > 40) + len = 40; + else + len = reply->value_len; + wm_log_continue("\"%.*s\"\n", len, text_value); + } else if (reply->type == XCB_ATOM_ATOM) { + atom_value = xcb_get_property_value(reply); + for (i = 0; i < reply->value_len; i++) { + name = get_atom_name(wm->conn, atom_value[i]); + if (width + strlen(name) + 2 > 78) { + wm_log_continue("\n "); + width = 4; + } else if (i > 0) { + width += wm_log_continue(", "); + } + + width += wm_log_continue("%s", name); + } + wm_log_continue("\n"); + } else { + wm_log_continue("huh?\n"); + } +} + +static void +read_and_dump_property(struct weston_wm *wm, + xcb_window_t window, xcb_atom_t property) +{ + xcb_get_property_reply_t *reply; + xcb_get_property_cookie_t cookie; + + cookie = xcb_get_property(wm->conn, 0, window, + property, XCB_ATOM_ANY, 0, 2048); + reply = xcb_get_property_reply(wm->conn, cookie, NULL); + + dump_property(wm, property, reply); + + free(reply); +} + +/* We reuse some predefined, but otherwise useles atoms */ +#define TYPE_WM_PROTOCOLS XCB_ATOM_CUT_BUFFER0 +#define TYPE_MOTIF_WM_HINTS XCB_ATOM_CUT_BUFFER1 +#define TYPE_NET_WM_STATE XCB_ATOM_CUT_BUFFER2 +#define TYPE_WM_NORMAL_HINTS XCB_ATOM_CUT_BUFFER3 + +static void +weston_wm_window_read_properties(struct weston_wm_window *window) +{ + struct weston_wm *wm = window->wm; + struct weston_shell_interface *shell_interface = + &wm->server->compositor->shell_interface; + +#define F(field) offsetof(struct weston_wm_window, field) + const struct { + xcb_atom_t atom; + xcb_atom_t type; + int offset; + } props[] = { + { XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, F(class) }, + { XCB_ATOM_WM_NAME, XCB_ATOM_STRING, F(name) }, + { XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, F(transient_for) }, + { wm->atom.wm_protocols, TYPE_WM_PROTOCOLS, F(protocols) }, + { wm->atom.wm_normal_hints, TYPE_WM_NORMAL_HINTS, F(protocols) }, + { wm->atom.net_wm_state, TYPE_NET_WM_STATE }, + { wm->atom.net_wm_window_type, XCB_ATOM_ATOM, F(type) }, + { wm->atom.net_wm_name, XCB_ATOM_STRING, F(name) }, + { wm->atom.net_wm_pid, XCB_ATOM_CARDINAL, F(pid) }, + { wm->atom.motif_wm_hints, TYPE_MOTIF_WM_HINTS, 0 }, + { wm->atom.wm_client_machine, XCB_ATOM_WM_CLIENT_MACHINE, F(machine) }, + }; +#undef F + + xcb_get_property_cookie_t cookie[ARRAY_LENGTH(props)]; + xcb_get_property_reply_t *reply; + void *p; + uint32_t *xid; + xcb_atom_t *atom; + uint32_t i; + + if (!window->properties_dirty) + return; + window->properties_dirty = 0; + + for (i = 0; i < ARRAY_LENGTH(props); i++) + cookie[i] = xcb_get_property(wm->conn, + 0, /* delete */ + window->id, + props[i].atom, + XCB_ATOM_ANY, 0, 2048); + + window->decorate = !window->override_redirect; + window->size_hints.flags = 0; + window->motif_hints.flags = 0; + window->delete_window = 0; + + for (i = 0; i < ARRAY_LENGTH(props); i++) { + reply = xcb_get_property_reply(wm->conn, cookie[i], NULL); + if (!reply) + /* Bad window, typically */ + continue; + if (reply->type == XCB_ATOM_NONE) { + /* No such property */ + free(reply); + continue; + } + + p = ((char *) window + props[i].offset); + + switch (props[i].type) { + case XCB_ATOM_WM_CLIENT_MACHINE: + case XCB_ATOM_STRING: + /* FIXME: We're using this for both string and + utf8_string */ + if (*(char **) p) + free(*(char **) p); + + *(char **) p = + strndup(xcb_get_property_value(reply), + xcb_get_property_value_length(reply)); + break; + case XCB_ATOM_WINDOW: + xid = xcb_get_property_value(reply); + *(struct weston_wm_window **) p = + hash_table_lookup(wm->window_hash, *xid); + break; + case XCB_ATOM_CARDINAL: + case XCB_ATOM_ATOM: + atom = xcb_get_property_value(reply); + *(xcb_atom_t *) p = *atom; + break; + case TYPE_WM_PROTOCOLS: + atom = xcb_get_property_value(reply); + for (i = 0; i < reply->value_len; i++) + if (atom[i] == wm->atom.wm_delete_window) + window->delete_window = 1; + break; + + break; + case TYPE_WM_NORMAL_HINTS: + memcpy(&window->size_hints, + xcb_get_property_value(reply), + sizeof window->size_hints); + break; + case TYPE_NET_WM_STATE: + window->fullscreen = 0; + atom = xcb_get_property_value(reply); + for (i = 0; i < reply->value_len; i++) + if (atom[i] == wm->atom.net_wm_state_fullscreen) + window->fullscreen = 1; + break; + case TYPE_MOTIF_WM_HINTS: + memcpy(&window->motif_hints, + xcb_get_property_value(reply), + sizeof window->motif_hints); + if (window->motif_hints.flags & MWM_HINTS_DECORATIONS) + window->decorate = + window->motif_hints.decorations > 0; + break; + default: + break; + } + free(reply); + } + + if (window->shsurf && window->name) + shell_interface->set_title(window->shsurf, window->name); + if (window->frame && window->name) + frame_set_title(window->frame, window->name); +} + +static void +weston_wm_window_get_frame_size(struct weston_wm_window *window, + int *width, int *height) +{ + struct theme *t = window->wm->theme; + + if (window->fullscreen) { + *width = window->width; + *height = window->height; + } else if (window->decorate && window->frame) { + *width = frame_width(window->frame); + *height = frame_height(window->frame); + } else { + *width = window->width + t->margin * 2; + *height = window->height + t->margin * 2; + } +} + +static void +weston_wm_window_get_child_position(struct weston_wm_window *window, + int *x, int *y) +{ + struct theme *t = window->wm->theme; + + if (window->fullscreen) { + *x = 0; + *y = 0; + } else if (window->decorate && window->frame) { + frame_interior(window->frame, x, y, NULL, NULL); + } else { + *x = t->margin; + *y = t->margin; + } +} + +static void +weston_wm_window_send_configure_notify(struct weston_wm_window *window) +{ + xcb_configure_notify_event_t configure_notify; + struct weston_wm *wm = window->wm; + int x, y; + + weston_wm_window_get_child_position(window, &x, &y); + configure_notify.response_type = XCB_CONFIGURE_NOTIFY; + configure_notify.pad0 = 0; + configure_notify.event = window->id; + configure_notify.window = window->id; + configure_notify.above_sibling = XCB_WINDOW_NONE; + configure_notify.x = x; + configure_notify.y = y; + configure_notify.width = window->width; + configure_notify.height = window->height; + configure_notify.border_width = 0; + configure_notify.override_redirect = 0; + configure_notify.pad1 = 0; + + xcb_send_event(wm->conn, 0, window->id, + XCB_EVENT_MASK_STRUCTURE_NOTIFY, + (char *) &configure_notify); +} + +static void +weston_wm_handle_configure_request(struct weston_wm *wm, xcb_generic_event_t *event) +{ + xcb_configure_request_event_t *configure_request = + (xcb_configure_request_event_t *) event; + struct weston_wm_window *window; + uint32_t mask, values[16]; + int x, y, width, height, i = 0; + + wm_log("XCB_CONFIGURE_REQUEST (window %d) %d,%d @ %dx%d\n", + configure_request->window, + configure_request->x, configure_request->y, + configure_request->width, configure_request->height); + + window = hash_table_lookup(wm->window_hash, configure_request->window); + + if (window->fullscreen) { + weston_wm_window_send_configure_notify(window); + return; + } + + if (configure_request->value_mask & XCB_CONFIG_WINDOW_WIDTH) + window->width = configure_request->width; + if (configure_request->value_mask & XCB_CONFIG_WINDOW_HEIGHT) + window->height = configure_request->height; + + if (window->frame) + frame_resize_inside(window->frame, window->width, window->height); + + weston_wm_window_get_child_position(window, &x, &y); + values[i++] = x; + values[i++] = y; + values[i++] = window->width; + values[i++] = window->height; + values[i++] = 0; + mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | + XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT | + XCB_CONFIG_WINDOW_BORDER_WIDTH; + if (configure_request->value_mask & XCB_CONFIG_WINDOW_SIBLING) { + values[i++] = configure_request->sibling; + mask |= XCB_CONFIG_WINDOW_SIBLING; + } + if (configure_request->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) { + values[i++] = configure_request->stack_mode; + mask |= XCB_CONFIG_WINDOW_STACK_MODE; + } + + xcb_configure_window(wm->conn, window->id, mask, values); + + weston_wm_window_get_frame_size(window, &width, &height); + values[0] = width; + values[1] = height; + mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT; + xcb_configure_window(wm->conn, window->frame_id, mask, values); + + weston_wm_window_schedule_repaint(window); +} + +static int +our_resource(struct weston_wm *wm, uint32_t id) +{ + const xcb_setup_t *setup; + + setup = xcb_get_setup(wm->conn); + + return (id & ~setup->resource_id_mask) == setup->resource_id_base; +} + +static void +weston_wm_handle_configure_notify(struct weston_wm *wm, xcb_generic_event_t *event) +{ + xcb_configure_notify_event_t *configure_notify = + (xcb_configure_notify_event_t *) event; + struct weston_wm_window *window; + + wm_log("XCB_CONFIGURE_NOTIFY (window %d) %d,%d @ %dx%d\n", + configure_notify->window, + configure_notify->x, configure_notify->y, + configure_notify->width, configure_notify->height); + + window = hash_table_lookup(wm->window_hash, configure_notify->window); + window->x = configure_notify->x; + window->y = configure_notify->y; + if (window->override_redirect) { + window->width = configure_notify->width; + window->height = configure_notify->height; + if (window->frame) + frame_resize_inside(window->frame, + window->width, window->height); + } +} + +static void +weston_wm_kill_client(struct wl_listener *listener, void *data) +{ + struct weston_surface *surface = data; + struct weston_wm_window *window = get_wm_window(surface); + char name[1024]; + + if (!window) + return; + + gethostname(name, 1024); + + /* this is only one heuristic to guess the PID of a client is valid, + * assuming it's compliant with icccm and ewmh. Non-compliants and + * remote applications of course fail. */ + if (!strcmp(window->machine, name) && window->pid != 0) + kill(window->pid, SIGKILL); +} + +static void +weston_wm_window_activate(struct wl_listener *listener, void *data) +{ + struct weston_surface *surface = data; + struct weston_wm_window *window = NULL; + struct weston_wm *wm = + container_of(listener, struct weston_wm, activate_listener); + xcb_client_message_event_t client_message; + + if (surface) { + window = get_wm_window(surface); + } + + if (window) { + client_message.response_type = XCB_CLIENT_MESSAGE; + client_message.format = 32; + client_message.window = window->id; + client_message.type = wm->atom.wm_protocols; + client_message.data.data32[0] = wm->atom.wm_take_focus; + client_message.data.data32[1] = XCB_TIME_CURRENT_TIME; + + xcb_send_event(wm->conn, 0, window->id, + XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, + (char *) &client_message); + + xcb_set_input_focus (wm->conn, XCB_INPUT_FOCUS_POINTER_ROOT, + window->id, XCB_TIME_CURRENT_TIME); + } else { + xcb_set_input_focus (wm->conn, + XCB_INPUT_FOCUS_POINTER_ROOT, + XCB_NONE, + XCB_TIME_CURRENT_TIME); + } + + if (wm->focus_window) { + if (wm->focus_window->frame) + frame_unset_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE); + weston_wm_window_schedule_repaint(wm->focus_window); + } + wm->focus_window = window; + if (wm->focus_window) { + if (wm->focus_window->frame) + frame_set_flag(wm->focus_window->frame, FRAME_FLAG_ACTIVE); + weston_wm_window_schedule_repaint(wm->focus_window); + } +} + +static void +weston_wm_window_transform(struct wl_listener *listener, void *data) +{ + struct weston_surface *surface = data; + struct weston_wm_window *window = get_wm_window(surface); + struct weston_wm *wm = + container_of(listener, struct weston_wm, transform_listener); + uint32_t mask, values[2]; + + if (!window || !wm) + return; + + if (!window->view || !weston_view_is_mapped(window->view)) + return; + + if (window->x != window->view->geometry.x || + window->y != window->view->geometry.y) { + values[0] = window->view->geometry.x; + values[1] = window->view->geometry.y; + mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y; + + xcb_configure_window(wm->conn, window->frame_id, mask, values); + xcb_flush(wm->conn); + } +} + +#define ICCCM_WITHDRAWN_STATE 0 +#define ICCCM_NORMAL_STATE 1 +#define ICCCM_ICONIC_STATE 3 + +static void +weston_wm_window_set_wm_state(struct weston_wm_window *window, int32_t state) +{ + struct weston_wm *wm = window->wm; + uint32_t property[2]; + + property[0] = state; + property[1] = XCB_WINDOW_NONE; + + xcb_change_property(wm->conn, + XCB_PROP_MODE_REPLACE, + window->id, + wm->atom.wm_state, + wm->atom.wm_state, + 32, /* format */ + 2, property); +} + +static void +weston_wm_window_set_net_wm_state(struct weston_wm_window *window) +{ + struct weston_wm *wm = window->wm; + uint32_t property[1]; + int i; + + i = 0; + if (window->fullscreen) + property[i++] = wm->atom.net_wm_state_fullscreen; + + xcb_change_property(wm->conn, + XCB_PROP_MODE_REPLACE, + window->id, + wm->atom.net_wm_state, + XCB_ATOM_ATOM, + 32, /* format */ + i, property); +} + +static void +weston_wm_window_create_frame(struct weston_wm_window *window) +{ + struct weston_wm *wm = window->wm; + uint32_t values[3]; + int x, y, width, height; + + window->frame = frame_create(window->wm->theme, + window->width, window->height, + FRAME_BUTTON_CLOSE, window->name); + frame_resize_inside(window->frame, window->width, window->height); + + weston_wm_window_get_frame_size(window, &width, &height); + weston_wm_window_get_child_position(window, &x, &y); + + values[0] = wm->screen->black_pixel; + values[1] = + XCB_EVENT_MASK_KEY_PRESS | + XCB_EVENT_MASK_KEY_RELEASE | + XCB_EVENT_MASK_BUTTON_PRESS | + XCB_EVENT_MASK_BUTTON_RELEASE | + XCB_EVENT_MASK_POINTER_MOTION | + XCB_EVENT_MASK_ENTER_WINDOW | + XCB_EVENT_MASK_LEAVE_WINDOW | + XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | + XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT; + values[2] = wm->colormap; + + window->frame_id = xcb_generate_id(wm->conn); + xcb_create_window(wm->conn, + 32, + window->frame_id, + wm->screen->root, + 0, 0, + width, height, + 0, + XCB_WINDOW_CLASS_INPUT_OUTPUT, + wm->visual_id, + XCB_CW_BORDER_PIXEL | + XCB_CW_EVENT_MASK | + XCB_CW_COLORMAP, values); + + xcb_reparent_window(wm->conn, window->id, window->frame_id, x, y); + + values[0] = 0; + xcb_configure_window(wm->conn, window->id, + XCB_CONFIG_WINDOW_BORDER_WIDTH, values); + + window->cairo_surface = + cairo_xcb_surface_create_with_xrender_format(wm->conn, + wm->screen, + window->frame_id, + &wm->format_rgba, + width, height); + + hash_table_insert(wm->window_hash, window->frame_id, window); +} + +static void +weston_wm_handle_map_request(struct weston_wm *wm, xcb_generic_event_t *event) +{ + xcb_map_request_event_t *map_request = + (xcb_map_request_event_t *) event; + struct weston_wm_window *window; + + if (our_resource(wm, map_request->window)) { + wm_log("XCB_MAP_REQUEST (window %d, ours)\n", + map_request->window); + return; + } + + window = hash_table_lookup(wm->window_hash, map_request->window); + + weston_wm_window_read_properties(window); + + if (window->frame_id == XCB_WINDOW_NONE) + weston_wm_window_create_frame(window); + + wm_log("XCB_MAP_REQUEST (window %d, %p, frame %d)\n", + window->id, window, window->frame_id); + + weston_wm_window_set_wm_state(window, ICCCM_NORMAL_STATE); + weston_wm_window_set_net_wm_state(window); + + xcb_map_window(wm->conn, map_request->window); + xcb_map_window(wm->conn, window->frame_id); +} + +static void +weston_wm_handle_map_notify(struct weston_wm *wm, xcb_generic_event_t *event) +{ + xcb_map_notify_event_t *map_notify = (xcb_map_notify_event_t *) event; + + if (our_resource(wm, map_notify->window)) { + wm_log("XCB_MAP_NOTIFY (window %d, ours)\n", + map_notify->window); + return; + } + + wm_log("XCB_MAP_NOTIFY (window %d)\n", map_notify->window); +} + +static void +weston_wm_handle_unmap_notify(struct weston_wm *wm, xcb_generic_event_t *event) +{ + xcb_unmap_notify_event_t *unmap_notify = + (xcb_unmap_notify_event_t *) event; + struct weston_wm_window *window; + + wm_log("XCB_UNMAP_NOTIFY (window %d, event %d%s)\n", + unmap_notify->window, + unmap_notify->event, + our_resource(wm, unmap_notify->window) ? ", ours" : ""); + + if (our_resource(wm, unmap_notify->window)) + return; + + if (unmap_notify->response_type & SEND_EVENT_MASK) + /* We just ignore the ICCCM 4.1.4 synthetic unmap notify + * as it may come in after we've destroyed the window. */ + return; + + window = hash_table_lookup(wm->window_hash, unmap_notify->window); + if (wm->focus_window == window) + wm->focus_window = NULL; + if (window->surface) + wl_list_remove(&window->surface_destroy_listener.link); + window->surface = NULL; + window->shsurf = NULL; + window->view = NULL; + xcb_unmap_window(wm->conn, window->frame_id); +} + +static void +weston_wm_window_draw_decoration(void *data) +{ + struct weston_wm_window *window = data; + struct weston_wm *wm = window->wm; + struct theme *t = wm->theme; + cairo_t *cr; + int x, y, width, height; + int32_t input_x, input_y, input_w, input_h; + + uint32_t flags = 0; + + weston_wm_window_read_properties(window); + + window->repaint_source = NULL; + + weston_wm_window_get_frame_size(window, &width, &height); + weston_wm_window_get_child_position(window, &x, &y); + + cairo_xcb_surface_set_size(window->cairo_surface, width, height); + cr = cairo_create(window->cairo_surface); + + if (window->fullscreen) { + /* nothing */ + } else if (window->decorate) { + if (wm->focus_window == window) + flags |= THEME_FRAME_ACTIVE; + + frame_repaint(window->frame, cr); + } else { + cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); + cairo_set_source_rgba(cr, 0, 0, 0, 0); + cairo_paint(cr); + + cairo_set_operator(cr, CAIRO_OPERATOR_OVER); + cairo_set_source_rgba(cr, 0, 0, 0, 0.45); + tile_mask(cr, t->shadow, 2, 2, width + 8, height + 8, 64, 64); + } + + cairo_destroy(cr); + + if (window->surface) { + pixman_region32_fini(&window->surface->pending.opaque); + if(window->has_alpha) { + pixman_region32_init(&window->surface->pending.opaque); + } else { + /* We leave an extra pixel around the X window area to + * make sure we don't sample from the undefined alpha + * channel when filtering. */ + pixman_region32_init_rect(&window->surface->pending.opaque, + x - 1, y - 1, + window->width + 2, + window->height + 2); + } + if (window->view) + weston_view_geometry_dirty(window->view); + + pixman_region32_fini(&window->surface->pending.input); + + if (window->fullscreen) { + input_x = 0; + input_y = 0; + input_w = window->width; + input_h = window->height; + } else if (window->decorate) { + frame_input_rect(window->frame, &input_x, &input_y, + &input_w, &input_h); + } + + pixman_region32_init_rect(&window->surface->pending.input, + input_x, input_y, input_w, input_h); + } +} + +static void +weston_wm_window_schedule_repaint(struct weston_wm_window *window) +{ + struct weston_wm *wm = window->wm; + int width, height; + + if (window->frame_id == XCB_WINDOW_NONE) { + if (window->surface != NULL) { + weston_wm_window_get_frame_size(window, &width, &height); + pixman_region32_fini(&window->surface->pending.opaque); + if(window->has_alpha) { + pixman_region32_init(&window->surface->pending.opaque); + } else { + pixman_region32_init_rect(&window->surface->pending.opaque, 0, 0, + width, height); + } + if (window->view) + weston_view_geometry_dirty(window->view); + } + return; + } + + if (window->repaint_source) + return; + + window->repaint_source = + wl_event_loop_add_idle(wm->server->loop, + weston_wm_window_draw_decoration, + window); +} + +static void +weston_wm_handle_property_notify(struct weston_wm *wm, xcb_generic_event_t *event) +{ + xcb_property_notify_event_t *property_notify = + (xcb_property_notify_event_t *) event; + struct weston_wm_window *window; + + window = hash_table_lookup(wm->window_hash, property_notify->window); + if (!window) + return; + + window->properties_dirty = 1; + + wm_log("XCB_PROPERTY_NOTIFY: window %d, ", property_notify->window); + if (property_notify->state == XCB_PROPERTY_DELETE) + wm_log("deleted\n"); + else + read_and_dump_property(wm, property_notify->window, + property_notify->atom); + + if (property_notify->atom == wm->atom.net_wm_name || + property_notify->atom == XCB_ATOM_WM_NAME) + weston_wm_window_schedule_repaint(window); +} + +static void +weston_wm_window_create(struct weston_wm *wm, + xcb_window_t id, int width, int height, int x, int y, int override) +{ + struct weston_wm_window *window; + uint32_t values[1]; + xcb_get_geometry_cookie_t geometry_cookie; + xcb_get_geometry_reply_t *geometry_reply; + + window = zalloc(sizeof *window); + if (window == NULL) { + wm_log("failed to allocate window\n"); + return; + } + + geometry_cookie = xcb_get_geometry(wm->conn, id); + + values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE; + xcb_change_window_attributes(wm->conn, id, XCB_CW_EVENT_MASK, values); + + window->wm = wm; + window->id = id; + window->properties_dirty = 1; + window->override_redirect = override; + window->width = width; + window->height = height; + window->x = x; + window->y = y; + + geometry_reply = xcb_get_geometry_reply(wm->conn, geometry_cookie, NULL); + /* technically we should use XRender and check the visual format's + alpha_mask, but checking depth is simpler and works in all known cases */ + if(geometry_reply != NULL) + window->has_alpha = geometry_reply->depth == 32; + free(geometry_reply); + + hash_table_insert(wm->window_hash, id, window); +} + +static void +weston_wm_window_destroy(struct weston_wm_window *window) +{ + struct weston_wm *wm = window->wm; + + if (window->repaint_source) + wl_event_source_remove(window->repaint_source); + if (window->cairo_surface) + cairo_surface_destroy(window->cairo_surface); + + if (window->frame_id) { + xcb_reparent_window(wm->conn, window->id, wm->wm_window, 0, 0); + xcb_destroy_window(wm->conn, window->frame_id); + weston_wm_window_set_wm_state(window, ICCCM_WITHDRAWN_STATE); + hash_table_remove(wm->window_hash, window->frame_id); + window->frame_id = XCB_WINDOW_NONE; + } + + hash_table_remove(window->wm->window_hash, window->id); + free(window); +} + +static void +weston_wm_handle_create_notify(struct weston_wm *wm, xcb_generic_event_t *event) +{ + xcb_create_notify_event_t *create_notify = + (xcb_create_notify_event_t *) event; + + wm_log("XCB_CREATE_NOTIFY (window %d, width %d, height %d%s%s)\n", + create_notify->window, + create_notify->width, create_notify->height, + create_notify->override_redirect ? ", override" : "", + our_resource(wm, create_notify->window) ? ", ours" : ""); + + if (our_resource(wm, create_notify->window)) + return; + + weston_wm_window_create(wm, create_notify->window, + create_notify->width, create_notify->height, + create_notify->x, create_notify->y, + create_notify->override_redirect); +} + +static void +weston_wm_handle_destroy_notify(struct weston_wm *wm, xcb_generic_event_t *event) +{ + xcb_destroy_notify_event_t *destroy_notify = + (xcb_destroy_notify_event_t *) event; + struct weston_wm_window *window; + + wm_log("XCB_DESTROY_NOTIFY, win %d, event %d%s\n", + destroy_notify->window, + destroy_notify->event, + our_resource(wm, destroy_notify->window) ? ", ours" : ""); + + if (our_resource(wm, destroy_notify->window)) + return; + + window = hash_table_lookup(wm->window_hash, destroy_notify->window); + weston_wm_window_destroy(window); +} + +static void +weston_wm_handle_reparent_notify(struct weston_wm *wm, xcb_generic_event_t *event) +{ + xcb_reparent_notify_event_t *reparent_notify = + (xcb_reparent_notify_event_t *) event; + struct weston_wm_window *window; + + wm_log("XCB_REPARENT_NOTIFY (window %d, parent %d, event %d)\n", + reparent_notify->window, + reparent_notify->parent, + reparent_notify->event); + + if (reparent_notify->parent == wm->screen->root) { + weston_wm_window_create(wm, reparent_notify->window, 10, 10, + reparent_notify->x, reparent_notify->y, + reparent_notify->override_redirect); + } else if (!our_resource(wm, reparent_notify->parent)) { + window = hash_table_lookup(wm->window_hash, + reparent_notify->window); + weston_wm_window_destroy(window); + } +} + +struct weston_seat * +weston_wm_pick_seat(struct weston_wm *wm) +{ + return container_of(wm->server->compositor->seat_list.next, + struct weston_seat, link); +} + +static void +weston_wm_window_handle_moveresize(struct weston_wm_window *window, + xcb_client_message_event_t *client_message) +{ + static const int map[] = { + THEME_LOCATION_RESIZING_TOP_LEFT, + THEME_LOCATION_RESIZING_TOP, + THEME_LOCATION_RESIZING_TOP_RIGHT, + THEME_LOCATION_RESIZING_RIGHT, + THEME_LOCATION_RESIZING_BOTTOM_RIGHT, + THEME_LOCATION_RESIZING_BOTTOM, + THEME_LOCATION_RESIZING_BOTTOM_LEFT, + THEME_LOCATION_RESIZING_LEFT + }; + + struct weston_wm *wm = window->wm; + struct weston_seat *seat = weston_wm_pick_seat(wm); + int detail; + struct weston_shell_interface *shell_interface = + &wm->server->compositor->shell_interface; + + if (seat->pointer->button_count != 1 || !window->view + || seat->pointer->focus != window->view) + return; + + detail = client_message->data.data32[2]; + switch (detail) { + case _NET_WM_MOVERESIZE_MOVE: + shell_interface->move(window->shsurf, seat); + break; + case _NET_WM_MOVERESIZE_SIZE_TOPLEFT: + case _NET_WM_MOVERESIZE_SIZE_TOP: + case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT: + case _NET_WM_MOVERESIZE_SIZE_RIGHT: + case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT: + case _NET_WM_MOVERESIZE_SIZE_BOTTOM: + case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT: + case _NET_WM_MOVERESIZE_SIZE_LEFT: + shell_interface->resize(window->shsurf, seat, map[detail]); + break; + case _NET_WM_MOVERESIZE_CANCEL: + break; + } +} + +#define _NET_WM_STATE_REMOVE 0 +#define _NET_WM_STATE_ADD 1 +#define _NET_WM_STATE_TOGGLE 2 + +static int +update_state(int action, int *state) +{ + int new_state, changed; + + switch (action) { + case _NET_WM_STATE_REMOVE: + new_state = 0; + break; + case _NET_WM_STATE_ADD: + new_state = 1; + break; + case _NET_WM_STATE_TOGGLE: + new_state = !*state; + break; + default: + return 0; + } + + changed = (*state != new_state); + *state = new_state; + + return changed; +} + +static void +weston_wm_window_configure(void *data); + +static void +weston_wm_window_handle_state(struct weston_wm_window *window, + xcb_client_message_event_t *client_message) +{ + struct weston_wm *wm = window->wm; + struct weston_shell_interface *shell_interface = + &wm->server->compositor->shell_interface; + uint32_t action, property; + + action = client_message->data.data32[0]; + property = client_message->data.data32[1]; + + if (property == wm->atom.net_wm_state_fullscreen && + update_state(action, &window->fullscreen)) { + weston_wm_window_set_net_wm_state(window); + if (window->fullscreen) { + window->saved_width = window->width; + window->saved_height = window->height; + + if (window->shsurf) + shell_interface->set_fullscreen(window->shsurf, + WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, + 0, NULL); + } else { + shell_interface->set_toplevel(window->shsurf); + window->width = window->saved_width; + window->height = window->saved_height; + if (window->frame) + frame_resize_inside(window->frame, + window->width, + window->height); + weston_wm_window_configure(window); + } + } +} + +static void +weston_wm_handle_client_message(struct weston_wm *wm, + xcb_generic_event_t *event) +{ + xcb_client_message_event_t *client_message = + (xcb_client_message_event_t *) event; + struct weston_wm_window *window; + + window = hash_table_lookup(wm->window_hash, client_message->window); + + wm_log("XCB_CLIENT_MESSAGE (%s %d %d %d %d %d win %d)\n", + get_atom_name(wm->conn, client_message->type), + client_message->data.data32[0], + client_message->data.data32[1], + client_message->data.data32[2], + client_message->data.data32[3], + client_message->data.data32[4], + client_message->window); + + if (client_message->type == wm->atom.net_wm_moveresize) + weston_wm_window_handle_moveresize(window, client_message); + else if (client_message->type == wm->atom.net_wm_state) + weston_wm_window_handle_state(window, client_message); +} + +enum cursor_type { + XWM_CURSOR_TOP, + XWM_CURSOR_BOTTOM, + XWM_CURSOR_LEFT, + XWM_CURSOR_RIGHT, + XWM_CURSOR_TOP_LEFT, + XWM_CURSOR_TOP_RIGHT, + XWM_CURSOR_BOTTOM_LEFT, + XWM_CURSOR_BOTTOM_RIGHT, + XWM_CURSOR_LEFT_PTR, +}; + +/* + * The following correspondences between file names and cursors was copied + * from: https://bugs.kde.org/attachment.cgi?id=67313 + */ + +static const char *bottom_left_corners[] = { + "bottom_left_corner", + "sw-resize", + "size_bdiag" +}; + +static const char *bottom_right_corners[] = { + "bottom_right_corner", + "se-resize", + "size_fdiag" +}; + +static const char *bottom_sides[] = { + "bottom_side", + "s-resize", + "size_ver" +}; + +static const char *left_ptrs[] = { + "left_ptr", + "default", + "top_left_arrow", + "left-arrow" +}; + +static const char *left_sides[] = { + "left_side", + "w-resize", + "size_hor" +}; + +static const char *right_sides[] = { + "right_side", + "e-resize", + "size_hor" +}; + +static const char *top_left_corners[] = { + "top_left_corner", + "nw-resize", + "size_fdiag" +}; + +static const char *top_right_corners[] = { + "top_right_corner", + "ne-resize", + "size_bdiag" +}; + +static const char *top_sides[] = { + "top_side", + "n-resize", + "size_ver" +}; + +struct cursor_alternatives { + const char **names; + size_t count; +}; + +static const struct cursor_alternatives cursors[] = { + {top_sides, ARRAY_LENGTH(top_sides)}, + {bottom_sides, ARRAY_LENGTH(bottom_sides)}, + {left_sides, ARRAY_LENGTH(left_sides)}, + {right_sides, ARRAY_LENGTH(right_sides)}, + {top_left_corners, ARRAY_LENGTH(top_left_corners)}, + {top_right_corners, ARRAY_LENGTH(top_right_corners)}, + {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)}, + {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)}, + {left_ptrs, ARRAY_LENGTH(left_ptrs)}, +}; + +static void +weston_wm_create_cursors(struct weston_wm *wm) +{ + const char *name; + int i, count = ARRAY_LENGTH(cursors); + size_t j; + + wm->cursors = malloc(count * sizeof(xcb_cursor_t)); + for (i = 0; i < count; i++) { + for (j = 0; j < cursors[i].count; j++) { + name = cursors[i].names[j]; + wm->cursors[i] = + xcb_cursor_library_load_cursor(wm, name); + if (wm->cursors[i] != (xcb_cursor_t)-1) + break; + } + } + + wm->last_cursor = -1; +} + +static void +weston_wm_destroy_cursors(struct weston_wm *wm) +{ + uint8_t i; + + for (i = 0; i < ARRAY_LENGTH(cursors); i++) + xcb_free_cursor(wm->conn, wm->cursors[i]); + + free(wm->cursors); +} + +static int +get_cursor_for_location(enum theme_location location) +{ + // int location = theme_get_location(t, x, y, width, height, 0); + + switch (location) { + case THEME_LOCATION_RESIZING_TOP: + return XWM_CURSOR_TOP; + case THEME_LOCATION_RESIZING_BOTTOM: + return XWM_CURSOR_BOTTOM; + case THEME_LOCATION_RESIZING_LEFT: + return XWM_CURSOR_LEFT; + case THEME_LOCATION_RESIZING_RIGHT: + return XWM_CURSOR_RIGHT; + case THEME_LOCATION_RESIZING_TOP_LEFT: + return XWM_CURSOR_TOP_LEFT; + case THEME_LOCATION_RESIZING_TOP_RIGHT: + return XWM_CURSOR_TOP_RIGHT; + case THEME_LOCATION_RESIZING_BOTTOM_LEFT: + return XWM_CURSOR_BOTTOM_LEFT; + case THEME_LOCATION_RESIZING_BOTTOM_RIGHT: + return XWM_CURSOR_BOTTOM_RIGHT; + case THEME_LOCATION_EXTERIOR: + case THEME_LOCATION_TITLEBAR: + default: + return XWM_CURSOR_LEFT_PTR; + } +} + +static void +weston_wm_window_set_cursor(struct weston_wm *wm, xcb_window_t window_id, + int cursor) +{ + uint32_t cursor_value_list; + + if (wm->last_cursor == cursor) + return; + + wm->last_cursor = cursor; + + cursor_value_list = wm->cursors[cursor]; + xcb_change_window_attributes (wm->conn, window_id, + XCB_CW_CURSOR, &cursor_value_list); + xcb_flush(wm->conn); +} + +static void +weston_wm_window_close(struct weston_wm_window *window, xcb_timestamp_t time) +{ + xcb_client_message_event_t client_message; + + if (window->delete_window) { + client_message.response_type = XCB_CLIENT_MESSAGE; + client_message.format = 32; + client_message.window = window->id; + client_message.type = window->wm->atom.wm_protocols; + client_message.data.data32[0] = + window->wm->atom.wm_delete_window; + client_message.data.data32[1] = time; + + xcb_send_event(window->wm->conn, 0, window->id, + XCB_EVENT_MASK_NO_EVENT, + (char *) &client_message); + } else { + xcb_kill_client(window->wm->conn, window->id); + } +} + +static void +weston_wm_handle_button(struct weston_wm *wm, xcb_generic_event_t *event) +{ + xcb_button_press_event_t *button = (xcb_button_press_event_t *) event; + struct weston_shell_interface *shell_interface = + &wm->server->compositor->shell_interface; + struct weston_seat *seat = weston_wm_pick_seat(wm); + struct weston_wm_window *window; + enum theme_location location; + enum frame_button_state button_state; + uint32_t button_id; + + wm_log("XCB_BUTTON_%s (detail %d)\n", + button->response_type == XCB_BUTTON_PRESS ? + "PRESS" : "RELEASE", button->detail); + + window = hash_table_lookup(wm->window_hash, button->event); + if (!window || !window->decorate) + return; + + if (button->detail != 1 && button->detail != 2) + return; + + button_state = button->response_type == XCB_BUTTON_PRESS ? + FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED; + button_id = button->detail == 1 ? BTN_LEFT : BTN_RIGHT; + + location = frame_pointer_button(window->frame, NULL, + button_id, button_state); + if (frame_status(window->frame) & FRAME_STATUS_REPAINT) + weston_wm_window_schedule_repaint(window); + + if (frame_status(window->frame) & FRAME_STATUS_MOVE) { + shell_interface->move(window->shsurf, seat); + frame_status_clear(window->frame, FRAME_STATUS_MOVE); + } + + if (frame_status(window->frame) & FRAME_STATUS_RESIZE) { + shell_interface->resize(window->shsurf, seat, location); + frame_status_clear(window->frame, FRAME_STATUS_RESIZE); + } + + if (frame_status(window->frame) & FRAME_STATUS_CLOSE) { + weston_wm_window_close(window, button->time); + frame_status_clear(window->frame, FRAME_STATUS_CLOSE); + } +} + +static void +weston_wm_handle_motion(struct weston_wm *wm, xcb_generic_event_t *event) +{ + xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event; + struct weston_wm_window *window; + enum theme_location location; + int cursor; + + window = hash_table_lookup(wm->window_hash, motion->event); + if (!window || !window->decorate) + return; + + location = frame_pointer_motion(window->frame, NULL, + motion->event_x, motion->event_y); + if (frame_status(window->frame) & FRAME_STATUS_REPAINT) + weston_wm_window_schedule_repaint(window); + + cursor = get_cursor_for_location(location); + weston_wm_window_set_cursor(wm, window->frame_id, cursor); +} + +static void +weston_wm_handle_enter(struct weston_wm *wm, xcb_generic_event_t *event) +{ + xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *) event; + struct weston_wm_window *window; + enum theme_location location; + int cursor; + + window = hash_table_lookup(wm->window_hash, enter->event); + if (!window || !window->decorate) + return; + + location = frame_pointer_enter(window->frame, NULL, + enter->event_x, enter->event_y); + if (frame_status(window->frame) & FRAME_STATUS_REPAINT) + weston_wm_window_schedule_repaint(window); + + cursor = get_cursor_for_location(location); + weston_wm_window_set_cursor(wm, window->frame_id, cursor); +} + +static void +weston_wm_handle_leave(struct weston_wm *wm, xcb_generic_event_t *event) +{ + xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *) event; + struct weston_wm_window *window; + + window = hash_table_lookup(wm->window_hash, leave->event); + if (!window || !window->decorate) + return; + + frame_pointer_leave(window->frame, NULL); + if (frame_status(window->frame) & FRAME_STATUS_REPAINT) + weston_wm_window_schedule_repaint(window); + + weston_wm_window_set_cursor(wm, window->frame_id, XWM_CURSOR_LEFT_PTR); +} + +static int +weston_wm_handle_event(int fd, uint32_t mask, void *data) +{ + struct weston_wm *wm = data; + xcb_generic_event_t *event; + int count = 0; + + while (event = xcb_poll_for_event(wm->conn), event != NULL) { + if (weston_wm_handle_selection_event(wm, event)) { + free(event); + count++; + continue; + } + + if (weston_wm_handle_dnd_event(wm, event)) { + free(event); + count++; + continue; + } + + switch (EVENT_TYPE(event)) { + case XCB_BUTTON_PRESS: + case XCB_BUTTON_RELEASE: + weston_wm_handle_button(wm, event); + break; + case XCB_ENTER_NOTIFY: + weston_wm_handle_enter(wm, event); + break; + case XCB_LEAVE_NOTIFY: + weston_wm_handle_leave(wm, event); + break; + case XCB_MOTION_NOTIFY: + weston_wm_handle_motion(wm, event); + break; + case XCB_CREATE_NOTIFY: + weston_wm_handle_create_notify(wm, event); + break; + case XCB_MAP_REQUEST: + weston_wm_handle_map_request(wm, event); + break; + case XCB_MAP_NOTIFY: + weston_wm_handle_map_notify(wm, event); + break; + case XCB_UNMAP_NOTIFY: + weston_wm_handle_unmap_notify(wm, event); + break; + case XCB_REPARENT_NOTIFY: + weston_wm_handle_reparent_notify(wm, event); + break; + case XCB_CONFIGURE_REQUEST: + weston_wm_handle_configure_request(wm, event); + break; + case XCB_CONFIGURE_NOTIFY: + weston_wm_handle_configure_notify(wm, event); + break; + case XCB_DESTROY_NOTIFY: + weston_wm_handle_destroy_notify(wm, event); + break; + case XCB_MAPPING_NOTIFY: + wm_log("XCB_MAPPING_NOTIFY\n"); + break; + case XCB_PROPERTY_NOTIFY: + weston_wm_handle_property_notify(wm, event); + break; + case XCB_CLIENT_MESSAGE: + weston_wm_handle_client_message(wm, event); + break; + } + + free(event); + count++; + } + + xcb_flush(wm->conn); + + return count; +} + +static void +weston_wm_get_visual_and_colormap(struct weston_wm *wm) +{ + xcb_depth_iterator_t d_iter; + xcb_visualtype_iterator_t vt_iter; + xcb_visualtype_t *visualtype; + + d_iter = xcb_screen_allowed_depths_iterator(wm->screen); + visualtype = NULL; + while (d_iter.rem > 0) { + if (d_iter.data->depth == 32) { + vt_iter = xcb_depth_visuals_iterator(d_iter.data); + visualtype = vt_iter.data; + break; + } + + xcb_depth_next(&d_iter); + } + + if (visualtype == NULL) { + weston_log("no 32 bit visualtype\n"); + return; + } + + wm->visual_id = visualtype->visual_id; + wm->colormap = xcb_generate_id(wm->conn); + xcb_create_colormap(wm->conn, XCB_COLORMAP_ALLOC_NONE, + wm->colormap, wm->screen->root, wm->visual_id); +} + +static void +weston_wm_get_resources(struct weston_wm *wm) +{ + +#define F(field) offsetof(struct weston_wm, field) + + static const struct { const char *name; int offset; } atoms[] = { + { "WM_PROTOCOLS", F(atom.wm_protocols) }, + { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) }, + { "WM_TAKE_FOCUS", F(atom.wm_take_focus) }, + { "WM_DELETE_WINDOW", F(atom.wm_delete_window) }, + { "WM_STATE", F(atom.wm_state) }, + { "WM_S0", F(atom.wm_s0) }, + { "WM_CLIENT_MACHINE", F(atom.wm_client_machine) }, + { "_NET_WM_CM_S0", F(atom.net_wm_cm_s0) }, + { "_NET_WM_NAME", F(atom.net_wm_name) }, + { "_NET_WM_PID", F(atom.net_wm_pid) }, + { "_NET_WM_ICON", F(atom.net_wm_icon) }, + { "_NET_WM_STATE", F(atom.net_wm_state) }, + { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) }, + { "_NET_WM_USER_TIME", F(atom.net_wm_user_time) }, + { "_NET_WM_ICON_NAME", F(atom.net_wm_icon_name) }, + { "_NET_WM_WINDOW_TYPE", F(atom.net_wm_window_type) }, + + { "_NET_WM_WINDOW_TYPE_DESKTOP", F(atom.net_wm_window_type_desktop) }, + { "_NET_WM_WINDOW_TYPE_DOCK", F(atom.net_wm_window_type_dock) }, + { "_NET_WM_WINDOW_TYPE_TOOLBAR", F(atom.net_wm_window_type_toolbar) }, + { "_NET_WM_WINDOW_TYPE_MENU", F(atom.net_wm_window_type_menu) }, + { "_NET_WM_WINDOW_TYPE_UTILITY", F(atom.net_wm_window_type_utility) }, + { "_NET_WM_WINDOW_TYPE_SPLASH", F(atom.net_wm_window_type_splash) }, + { "_NET_WM_WINDOW_TYPE_DIALOG", F(atom.net_wm_window_type_dialog) }, + { "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", F(atom.net_wm_window_type_dropdown) }, + { "_NET_WM_WINDOW_TYPE_POPUP_MENU", F(atom.net_wm_window_type_popup) }, + { "_NET_WM_WINDOW_TYPE_TOOLTIP", F(atom.net_wm_window_type_tooltip) }, + { "_NET_WM_WINDOW_TYPE_NOTIFICATION", F(atom.net_wm_window_type_notification) }, + { "_NET_WM_WINDOW_TYPE_COMBO", F(atom.net_wm_window_type_combo) }, + { "_NET_WM_WINDOW_TYPE_DND", F(atom.net_wm_window_type_dnd) }, + { "_NET_WM_WINDOW_TYPE_NORMAL", F(atom.net_wm_window_type_normal) }, + + { "_NET_WM_MOVERESIZE", F(atom.net_wm_moveresize) }, + { "_NET_SUPPORTING_WM_CHECK", + F(atom.net_supporting_wm_check) }, + { "_NET_SUPPORTED", F(atom.net_supported) }, + { "_MOTIF_WM_HINTS", F(atom.motif_wm_hints) }, + { "CLIPBOARD", F(atom.clipboard) }, + { "CLIPBOARD_MANAGER", F(atom.clipboard_manager) }, + { "TARGETS", F(atom.targets) }, + { "UTF8_STRING", F(atom.utf8_string) }, + { "_WL_SELECTION", F(atom.wl_selection) }, + { "INCR", F(atom.incr) }, + { "TIMESTAMP", F(atom.timestamp) }, + { "MULTIPLE", F(atom.multiple) }, + { "UTF8_STRING" , F(atom.utf8_string) }, + { "COMPOUND_TEXT", F(atom.compound_text) }, + { "TEXT", F(atom.text) }, + { "STRING", F(atom.string) }, + { "text/plain;charset=utf-8", F(atom.text_plain_utf8) }, + { "text/plain", F(atom.text_plain) }, + { "XdndSelection", F(atom.xdnd_selection) }, + { "XdndAware", F(atom.xdnd_aware) }, + { "XdndEnter", F(atom.xdnd_enter) }, + { "XdndLeave", F(atom.xdnd_leave) }, + { "XdndDrop", F(atom.xdnd_drop) }, + { "XdndStatus", F(atom.xdnd_status) }, + { "XdndFinished", F(atom.xdnd_finished) }, + { "XdndTypeList", F(atom.xdnd_type_list) }, + { "XdndActionCopy", F(atom.xdnd_action_copy) } + }; +#undef F + + xcb_xfixes_query_version_cookie_t xfixes_cookie; + xcb_xfixes_query_version_reply_t *xfixes_reply; + xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)]; + xcb_intern_atom_reply_t *reply; + xcb_render_query_pict_formats_reply_t *formats_reply; + xcb_render_query_pict_formats_cookie_t formats_cookie; + xcb_render_pictforminfo_t *formats; + uint32_t i; + + xcb_prefetch_extension_data (wm->conn, &xcb_xfixes_id); + xcb_prefetch_extension_data (wm->conn, &xcb_composite_id); + + formats_cookie = xcb_render_query_pict_formats(wm->conn); + + for (i = 0; i < ARRAY_LENGTH(atoms); i++) + cookies[i] = xcb_intern_atom (wm->conn, 0, + strlen(atoms[i].name), + atoms[i].name); + + for (i = 0; i < ARRAY_LENGTH(atoms); i++) { + reply = xcb_intern_atom_reply (wm->conn, cookies[i], NULL); + *(xcb_atom_t *) ((char *) wm + atoms[i].offset) = reply->atom; + free(reply); + } + + wm->xfixes = xcb_get_extension_data(wm->conn, &xcb_xfixes_id); + if (!wm->xfixes || !wm->xfixes->present) + weston_log("xfixes not available\n"); + + xfixes_cookie = xcb_xfixes_query_version(wm->conn, + XCB_XFIXES_MAJOR_VERSION, + XCB_XFIXES_MINOR_VERSION); + xfixes_reply = xcb_xfixes_query_version_reply(wm->conn, + xfixes_cookie, NULL); + + weston_log("xfixes version: %d.%d\n", + xfixes_reply->major_version, xfixes_reply->minor_version); + + free(xfixes_reply); + + formats_reply = xcb_render_query_pict_formats_reply(wm->conn, + formats_cookie, 0); + if (formats_reply == NULL) + return; + + formats = xcb_render_query_pict_formats_formats(formats_reply); + for (i = 0; i < formats_reply->num_formats; i++) { + if (formats[i].direct.red_mask != 0xff && + formats[i].direct.red_shift != 16) + continue; + if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT && + formats[i].depth == 24) + wm->format_rgb = formats[i]; + if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT && + formats[i].depth == 32 && + formats[i].direct.alpha_mask == 0xff && + formats[i].direct.alpha_shift == 24) + wm->format_rgba = formats[i]; + } + + free(formats_reply); +} + +static void +weston_wm_create_wm_window(struct weston_wm *wm) +{ + static const char name[] = "Weston WM"; + + wm->wm_window = xcb_generate_id(wm->conn); + xcb_create_window(wm->conn, + XCB_COPY_FROM_PARENT, + wm->wm_window, + wm->screen->root, + 0, 0, + 10, 10, + 0, + XCB_WINDOW_CLASS_INPUT_OUTPUT, + wm->screen->root_visual, + 0, NULL); + + xcb_change_property(wm->conn, + XCB_PROP_MODE_REPLACE, + wm->wm_window, + wm->atom.net_supporting_wm_check, + XCB_ATOM_WINDOW, + 32, /* format */ + 1, &wm->wm_window); + + xcb_change_property(wm->conn, + XCB_PROP_MODE_REPLACE, + wm->wm_window, + wm->atom.net_wm_name, + wm->atom.utf8_string, + 8, /* format */ + strlen(name), name); + + xcb_change_property(wm->conn, + XCB_PROP_MODE_REPLACE, + wm->screen->root, + wm->atom.net_supporting_wm_check, + XCB_ATOM_WINDOW, + 32, /* format */ + 1, &wm->wm_window); + + /* Claim the WM_S0 selection even though we don't suport + * the --replace functionality. */ + xcb_set_selection_owner(wm->conn, + wm->wm_window, + wm->atom.wm_s0, + XCB_TIME_CURRENT_TIME); + + xcb_set_selection_owner(wm->conn, + wm->wm_window, + wm->atom.net_wm_cm_s0, + XCB_TIME_CURRENT_TIME); +} + +struct weston_wm * +weston_wm_create(struct weston_xserver *wxs) +{ + struct weston_wm *wm; + struct wl_event_loop *loop; + xcb_screen_iterator_t s; + uint32_t values[1]; + int sv[2]; + xcb_atom_t supported[3]; + + wm = zalloc(sizeof *wm); + if (wm == NULL) + return NULL; + + wm->server = wxs; + wm->window_hash = hash_table_create(); + if (wm->window_hash == NULL) { + free(wm); + return NULL; + } + + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) { + weston_log("socketpair failed\n"); + hash_table_destroy(wm->window_hash); + free(wm); + return NULL; + } + + xserver_send_client(wxs->resource, sv[1]); + wl_client_flush(wl_resource_get_client(wxs->resource)); + close(sv[1]); + + /* xcb_connect_to_fd takes ownership of the fd. */ + wm->conn = xcb_connect_to_fd(sv[0], NULL); + if (xcb_connection_has_error(wm->conn)) { + weston_log("xcb_connect_to_fd failed\n"); + close(sv[0]); + hash_table_destroy(wm->window_hash); + free(wm); + return NULL; + } + + s = xcb_setup_roots_iterator(xcb_get_setup(wm->conn)); + wm->screen = s.data; + + loop = wl_display_get_event_loop(wxs->wl_display); + wm->source = + wl_event_loop_add_fd(loop, sv[0], + WL_EVENT_READABLE, + weston_wm_handle_event, wm); + wl_event_source_check(wm->source); + + weston_wm_get_resources(wm); + weston_wm_get_visual_and_colormap(wm); + + values[0] = + XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | + XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | + XCB_EVENT_MASK_PROPERTY_CHANGE; + xcb_change_window_attributes(wm->conn, wm->screen->root, + XCB_CW_EVENT_MASK, values); + + xcb_composite_redirect_subwindows(wm->conn, wm->screen->root, + XCB_COMPOSITE_REDIRECT_MANUAL); + + wm->theme = theme_create(); + + weston_wm_create_wm_window(wm); + + supported[0] = wm->atom.net_wm_moveresize; + supported[1] = wm->atom.net_wm_state; + supported[2] = wm->atom.net_wm_state_fullscreen; + xcb_change_property(wm->conn, + XCB_PROP_MODE_REPLACE, + wm->screen->root, + wm->atom.net_supported, + XCB_ATOM_ATOM, + 32, /* format */ + ARRAY_LENGTH(supported), supported); + + weston_wm_selection_init(wm); + + weston_wm_dnd_init(wm); + + xcb_flush(wm->conn); + + wm->activate_listener.notify = weston_wm_window_activate; + wl_signal_add(&wxs->compositor->activate_signal, + &wm->activate_listener); + wm->transform_listener.notify = weston_wm_window_transform; + wl_signal_add(&wxs->compositor->transform_signal, + &wm->transform_listener); + wm->kill_listener.notify = weston_wm_kill_client; + wl_signal_add(&wxs->compositor->kill_signal, + &wm->kill_listener); + + weston_wm_create_cursors(wm); + weston_wm_window_set_cursor(wm, wm->screen->root, XWM_CURSOR_LEFT_PTR); + + weston_log("created wm\n"); + + return wm; +} + +void +weston_wm_destroy(struct weston_wm *wm) +{ + /* FIXME: Free windows in hash. */ + hash_table_destroy(wm->window_hash); + weston_wm_destroy_cursors(wm); + xcb_disconnect(wm->conn); + wl_event_source_remove(wm->source); + wl_list_remove(&wm->selection_listener.link); + wl_list_remove(&wm->activate_listener.link); + wl_list_remove(&wm->kill_listener.link); + wl_list_remove(&wm->transform_listener.link); + + free(wm); +} + +static void +surface_destroy(struct wl_listener *listener, void *data) +{ + struct weston_wm_window *window = + container_of(listener, + struct weston_wm_window, surface_destroy_listener); + + wm_log("surface for xid %d destroyed\n", window->id); + + /* This should have been freed by the shell. + Don't try to use it later. */ + window->shsurf = NULL; + window->surface = NULL; + window->view = NULL; +} + +static struct weston_wm_window * +get_wm_window(struct weston_surface *surface) +{ + struct wl_listener *listener; + + listener = wl_signal_get(&surface->destroy_signal, surface_destroy); + if (listener) + return container_of(listener, struct weston_wm_window, + surface_destroy_listener); + + return NULL; +} + +static void +weston_wm_window_configure(void *data) +{ + struct weston_wm_window *window = data; + struct weston_wm *wm = window->wm; + uint32_t values[4]; + int x, y, width, height; + + weston_wm_window_get_child_position(window, &x, &y); + values[0] = x; + values[1] = y; + values[2] = window->width; + values[3] = window->height; + xcb_configure_window(wm->conn, + window->id, + XCB_CONFIG_WINDOW_X | + XCB_CONFIG_WINDOW_Y | + XCB_CONFIG_WINDOW_WIDTH | + XCB_CONFIG_WINDOW_HEIGHT, + values); + + weston_wm_window_get_frame_size(window, &width, &height); + values[0] = width; + values[1] = height; + xcb_configure_window(wm->conn, + window->frame_id, + XCB_CONFIG_WINDOW_WIDTH | + XCB_CONFIG_WINDOW_HEIGHT, + values); + + window->configure_source = NULL; + + weston_wm_window_schedule_repaint(window); +} + +static void +send_configure(struct weston_surface *surface, + uint32_t edges, int32_t width, int32_t height) +{ + struct weston_wm_window *window = get_wm_window(surface); + struct weston_wm *wm = window->wm; + struct theme *t = window->wm->theme; + int vborder, hborder; + + if (window->fullscreen) { + hborder = 0; + vborder = 0; + } else if (window->decorate) { + hborder = 2 * (t->margin + t->width); + vborder = 2 * t->margin + t->titlebar_height + t->width; + } else { + hborder = 2 * t->margin; + vborder = 2 * t->margin; + } + + if (width > hborder) + window->width = width - hborder; + else + window->width = 1; + + if (height > vborder) + window->height = height - vborder; + else + window->height = 1; + + if (window->frame) + frame_resize_inside(window->frame, window->width, window->height); + + if (window->configure_source) + return; + + window->configure_source = + wl_event_loop_add_idle(wm->server->loop, + weston_wm_window_configure, window); +} + +static const struct weston_shell_client shell_client = { + send_configure +}; + +static int +legacy_fullscreen(struct weston_wm *wm, + struct weston_wm_window *window, + struct weston_output **output_ret) +{ + struct weston_compositor *compositor = wm->server->compositor; + struct weston_output *output; + uint32_t minmax = PMinSize | PMaxSize; + int matching_size; + + /* Heuristics for detecting legacy fullscreen windows... */ + + wl_list_for_each(output, &compositor->output_list, link) { + if (output->x == window->x && + output->y == window->y && + output->width == window->width && + output->height == window->height && + window->override_redirect) { + *output_ret = output; + return 1; + } + + matching_size = 0; + if ((window->size_hints.flags & (USSize |PSize)) && + window->size_hints.width == output->width && + window->size_hints.height == output->height) + matching_size = 1; + if ((window->size_hints.flags & minmax) == minmax && + window->size_hints.min_width == output->width && + window->size_hints.min_height == output->height && + window->size_hints.max_width == output->width && + window->size_hints.max_height == output->height) + matching_size = 1; + + if (matching_size && !window->decorate && + (window->size_hints.flags & (USPosition | PPosition)) && + window->size_hints.x == output->x && + window->size_hints.y == output->y) { + *output_ret = output; + return 1; + } + } + + return 0; +} +static void +xserver_map_shell_surface(struct weston_wm *wm, + struct weston_wm_window *window) +{ + struct weston_shell_interface *shell_interface = + &wm->server->compositor->shell_interface; + struct weston_output *output; + + if (!shell_interface->create_shell_surface) + return; + + if (!shell_interface->get_primary_view) + return; + + window->shsurf = + shell_interface->create_shell_surface(shell_interface->shell, + window->surface, + &shell_client); + window->view = shell_interface->get_primary_view(shell_interface->shell, + window->shsurf); + + if (window->name) + shell_interface->set_title(window->shsurf, window->name); + + if (window->fullscreen) { + window->saved_width = window->width; + window->saved_height = window->height; + shell_interface->set_fullscreen(window->shsurf, + WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, + 0, NULL); + return; + } else if (legacy_fullscreen(wm, window, &output)) { + window->fullscreen = 1; + shell_interface->set_fullscreen(window->shsurf, + WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, + 0, output); + } else if (!window->override_redirect && !window->transient_for) { + shell_interface->set_toplevel(window->shsurf); + return; + } else { + shell_interface->set_xwayland(window->shsurf, + window->x, + window->y, + WL_SHELL_SURFACE_TRANSIENT_INACTIVE); + } +} + +static void +xserver_set_window_id(struct wl_client *client, struct wl_resource *resource, + struct wl_resource *surface_resource, uint32_t id) +{ + struct weston_xserver *wxs = wl_resource_get_user_data(resource); + struct weston_wm *wm = wxs->wm; + struct weston_surface *surface = + wl_resource_get_user_data(surface_resource); + struct weston_wm_window *window; + + if (client != wxs->client) + return; + + window = hash_table_lookup(wm->window_hash, id); + if (window == NULL) { + weston_log("set_window_id for unknown window %d\n", id); + return; + } + + wm_log("set_window_id %d for surface %p\n", id, surface); + + weston_wm_window_read_properties(window); + + /* A weston_wm_window may have many different surfaces assigned + * throughout its life, so we must make sure to remove the listener + * from the old surface signal list. */ + if (window->surface) + wl_list_remove(&window->surface_destroy_listener.link); + + window->surface = (struct weston_surface *) surface; + window->surface_destroy_listener.notify = surface_destroy; + wl_signal_add(&surface->destroy_signal, + &window->surface_destroy_listener); + + weston_wm_window_schedule_repaint(window); + xserver_map_shell_surface(wm, window); +} + +const struct xserver_interface xserver_implementation = { + xserver_set_window_id +}; diff --git a/xwayland/xwayland.h b/xwayland/xwayland.h new file mode 100644 index 00000000..c684cc51 --- /dev/null +++ b/xwayland/xwayland.h @@ -0,0 +1,175 @@ +/* + * Copyright © 2012 Intel Corporation + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of the copyright holders not be used in + * advertising or publicity pertaining to distribution of the software + * without specific, written prior permission. The copyright holders make + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <wayland-server.h> +#include <xcb/xcb.h> +#include <xcb/xfixes.h> +#include <xcb/composite.h> +#include <cairo/cairo-xcb.h> + +#include "compositor.h" + +#define SEND_EVENT_MASK (0x80) +#define EVENT_TYPE(event) ((event)->response_type & ~SEND_EVENT_MASK) + +struct weston_xserver { + struct wl_display *wl_display; + struct wl_event_loop *loop; + struct wl_event_source *sigchld_source; + int abstract_fd; + struct wl_event_source *abstract_source; + int unix_fd; + struct wl_event_source *unix_source; + int display; + struct weston_process process; + struct wl_resource *resource; + struct wl_client *client; + struct weston_compositor *compositor; + struct weston_wm *wm; + struct wl_listener destroy_listener; +}; + +struct weston_wm { + xcb_connection_t *conn; + const xcb_query_extension_reply_t *xfixes; + struct wl_event_source *source; + xcb_screen_t *screen; + struct hash_table *window_hash; + struct weston_xserver *server; + xcb_window_t wm_window; + struct weston_wm_window *focus_window; + struct theme *theme; + xcb_cursor_t *cursors; + int last_cursor; + xcb_render_pictforminfo_t format_rgb, format_rgba; + xcb_visualid_t visual_id; + xcb_colormap_t colormap; + struct wl_listener activate_listener; + struct wl_listener transform_listener; + struct wl_listener kill_listener; + + xcb_window_t selection_window; + xcb_window_t selection_owner; + int incr; + int data_source_fd; + struct wl_event_source *property_source; + xcb_get_property_reply_t *property_reply; + int property_start; + struct wl_array source_data; + xcb_selection_request_event_t selection_request; + xcb_atom_t selection_target; + xcb_timestamp_t selection_timestamp; + int selection_property_set; + int flush_property_on_delete; + struct wl_listener selection_listener; + + xcb_window_t dnd_window; + xcb_window_t dnd_owner; + + struct { + xcb_atom_t wm_protocols; + xcb_atom_t wm_normal_hints; + xcb_atom_t wm_take_focus; + xcb_atom_t wm_delete_window; + xcb_atom_t wm_state; + xcb_atom_t wm_s0; + xcb_atom_t wm_client_machine; + xcb_atom_t net_wm_cm_s0; + xcb_atom_t net_wm_name; + xcb_atom_t net_wm_pid; + xcb_atom_t net_wm_icon; + xcb_atom_t net_wm_state; + xcb_atom_t net_wm_state_fullscreen; + xcb_atom_t net_wm_user_time; + xcb_atom_t net_wm_icon_name; + xcb_atom_t net_wm_window_type; + xcb_atom_t net_wm_window_type_desktop; + xcb_atom_t net_wm_window_type_dock; + xcb_atom_t net_wm_window_type_toolbar; + xcb_atom_t net_wm_window_type_menu; + xcb_atom_t net_wm_window_type_utility; + xcb_atom_t net_wm_window_type_splash; + xcb_atom_t net_wm_window_type_dialog; + xcb_atom_t net_wm_window_type_dropdown; + xcb_atom_t net_wm_window_type_popup; + xcb_atom_t net_wm_window_type_tooltip; + xcb_atom_t net_wm_window_type_notification; + xcb_atom_t net_wm_window_type_combo; + xcb_atom_t net_wm_window_type_dnd; + xcb_atom_t net_wm_window_type_normal; + xcb_atom_t net_wm_moveresize; + xcb_atom_t net_supporting_wm_check; + xcb_atom_t net_supported; + xcb_atom_t motif_wm_hints; + xcb_atom_t clipboard; + xcb_atom_t clipboard_manager; + xcb_atom_t targets; + xcb_atom_t utf8_string; + xcb_atom_t wl_selection; + xcb_atom_t incr; + xcb_atom_t timestamp; + xcb_atom_t multiple; + xcb_atom_t compound_text; + xcb_atom_t text; + xcb_atom_t string; + xcb_atom_t text_plain_utf8; + xcb_atom_t text_plain; + xcb_atom_t xdnd_selection; + xcb_atom_t xdnd_aware; + xcb_atom_t xdnd_enter; + xcb_atom_t xdnd_leave; + xcb_atom_t xdnd_drop; + xcb_atom_t xdnd_status; + xcb_atom_t xdnd_finished; + xcb_atom_t xdnd_type_list; + xcb_atom_t xdnd_action_copy; + } atom; +}; + +void +dump_property(struct weston_wm *wm, xcb_atom_t property, + xcb_get_property_reply_t *reply); + +const char * +get_atom_name(xcb_connection_t *c, xcb_atom_t atom); + +void +weston_wm_selection_init(struct weston_wm *wm); +int +weston_wm_handle_selection_event(struct weston_wm *wm, + xcb_generic_event_t *event); + +extern const struct xserver_interface xserver_implementation; + +struct weston_wm * +weston_wm_create(struct weston_xserver *wxs); +void +weston_wm_destroy(struct weston_wm *wm); + +struct weston_seat * +weston_wm_pick_seat(struct weston_wm *wm); + +int +weston_wm_handle_dnd_event(struct weston_wm *wm, + xcb_generic_event_t *event); +void +weston_wm_dnd_init(struct weston_wm *wm); |