xdg_decoration.c (4832B)
1 /* swc: libswc/xdg_decoration.c 2 * 3 * Copyright (c) 2020 Michael Forney 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a copy 6 * of this software and associated documentation files (the "Software"), to deal 7 * in the Software without restriction, including without limitation the rights 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 * copies of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 * SOFTWARE. 22 */ 23 24 #include "xdg_decoration.h" 25 #include "util.h" 26 #include "xdg_shell.h" 27 28 #include "xdg-decoration-unstable-v1-server-protocol.h" 29 #include <wayland-server.h> 30 31 struct xdg_toplevel_decoration { 32 struct wl_resource *resource; 33 struct wl_resource *toplevel; 34 struct wl_listener toplevel_destroy_listener; 35 }; 36 37 /* The mode is always the server's. A mode asked for is answered with it and 38 * then a configure of the toplevel, as the protocol has it: a client may 39 * wait for that configure to know its decoration settled before it maps. */ 40 static void 41 answer_mode(struct wl_resource *resource) 42 { 43 struct xdg_toplevel_decoration *decoration = 44 wl_resource_get_user_data(resource); 45 46 zxdg_toplevel_decoration_v1_send_configure( 47 resource, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); 48 xdg_toplevel_send_state(decoration->toplevel); 49 } 50 51 static void 52 set_mode(struct wl_client *client, struct wl_resource *resource, uint32_t mode) 53 { 54 answer_mode(resource); 55 } 56 57 static void 58 unset_mode(struct wl_client *client, struct wl_resource *resource) 59 { 60 answer_mode(resource); 61 } 62 63 static const struct zxdg_toplevel_decoration_v1_interface decoration_impl = { 64 .destroy = destroy_resource, 65 .set_mode = set_mode, 66 .unset_mode = unset_mode, 67 }; 68 69 static void 70 handle_toplevel_destroy(struct wl_listener *listener, void *data) 71 { 72 struct xdg_toplevel_decoration *decoration = 73 wl_container_of(listener, decoration, toplevel_destroy_listener); 74 75 wl_resource_destroy(decoration->resource); 76 } 77 78 static void 79 decoration_destroy(struct wl_resource *resource) 80 { 81 struct xdg_toplevel_decoration *decoration = 82 wl_resource_get_user_data(resource); 83 84 wl_list_remove(&decoration->toplevel_destroy_listener.link); 85 free(decoration); 86 } 87 88 static void 89 get_toplevel_decoration(struct wl_client *client, struct wl_resource *resource, 90 uint32_t id, struct wl_resource *toplevel_resource) 91 { 92 struct xdg_toplevel_decoration *decoration; 93 94 decoration = malloc(sizeof(*decoration)); 95 if (!decoration) { 96 goto error0; 97 } 98 decoration->resource = 99 wl_resource_create(client, &zxdg_toplevel_decoration_v1_interface, 100 wl_resource_get_version(resource), id); 101 if (!decoration->resource) { 102 goto error1; 103 } 104 decoration->toplevel = toplevel_resource; 105 decoration->toplevel_destroy_listener.notify = &handle_toplevel_destroy; 106 wl_resource_add_destroy_listener(toplevel_resource, 107 &decoration->toplevel_destroy_listener); 108 wl_resource_set_implementation(decoration->resource, &decoration_impl, 109 decoration, decoration_destroy); 110 zxdg_toplevel_decoration_v1_send_configure( 111 decoration->resource, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); 112 return; 113 114 error1: 115 free(decoration); 116 error0: 117 wl_resource_post_no_memory(resource); 118 } 119 120 static const struct zxdg_decoration_manager_v1_interface 121 decoration_manager_impl = { 122 .destroy = destroy_resource, 123 .get_toplevel_decoration = get_toplevel_decoration, 124 }; 125 126 static void 127 bind_decoration_manager(struct wl_client *client, void *data, uint32_t version, 128 uint32_t id) 129 { 130 struct wl_resource *resource; 131 132 resource = wl_resource_create(client, &zxdg_decoration_manager_v1_interface, 133 version, id); 134 if (!resource) { 135 wl_client_post_no_memory(client); 136 return; 137 } 138 wl_resource_set_implementation(resource, &decoration_manager_impl, NULL, 139 NULL); 140 } 141 142 struct wl_global * 143 xdg_decoration_manager_create(struct wl_display *display) 144 { 145 return wl_global_create(display, &zxdg_decoration_manager_v1_interface, 1, 146 NULL, &bind_decoration_manager); 147 }