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
27#include "xdg-decoration-unstable-v1-server-protocol.h"
28#include <wayland-server.h>
29
30struct xdg_toplevel_decoration {
31 struct wl_resource *resource;
32 struct wl_listener toplevel_destroy_listener;
33};
34
35static void
36set_mode(struct wl_client *client, struct wl_resource *resource, uint32_t mode)
37{
38}
39
40static void
41unset_mode(struct wl_client *client, struct wl_resource *resource)
42{
43}
44
45static const struct zxdg_toplevel_decoration_v1_interface decoration_impl = {
46 .destroy = destroy_resource,
47 .set_mode = set_mode,
48 .unset_mode = unset_mode,
49};
50
51static void
52handle_toplevel_destroy(struct wl_listener *listener, void *data)
53{
54 struct xdg_toplevel_decoration *decoration =
55 wl_container_of(listener, decoration, toplevel_destroy_listener);
56
57 wl_resource_destroy(decoration->resource);
58}
59
60static void
61decoration_destroy(struct wl_resource *resource)
62{
63 struct xdg_toplevel_decoration *decoration =
64 wl_resource_get_user_data(resource);
65
66 wl_list_remove(&decoration->toplevel_destroy_listener.link);
67 free(decoration);
68}
69
70static void
71get_toplevel_decoration(struct wl_client *client, struct wl_resource *resource,
72 uint32_t id, struct wl_resource *toplevel_resource)
73{
74 struct xdg_toplevel_decoration *decoration;
75
76 decoration = malloc(sizeof(*decoration));
77 if (!decoration) {
78 goto error0;
79 }
80 decoration->resource =
81 wl_resource_create(client, &zxdg_toplevel_decoration_v1_interface,
82 wl_resource_get_version(resource), id);
83 if (!decoration->resource) {
84 goto error1;
85 }
86 decoration->toplevel_destroy_listener.notify = &handle_toplevel_destroy;
87 wl_resource_add_destroy_listener(toplevel_resource,
88 &decoration->toplevel_destroy_listener);
89 wl_resource_set_implementation(decoration->resource, &decoration_impl,
90 decoration, decoration_destroy);
91 zxdg_toplevel_decoration_v1_send_configure(
92 decoration->resource, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
93 return;
94
95error1:
96 free(decoration);
97error0:
98 wl_resource_post_no_memory(resource);
99}
100
101static const struct zxdg_decoration_manager_v1_interface
102 decoration_manager_impl = {
103 .destroy = destroy_resource,
104 .get_toplevel_decoration = get_toplevel_decoration,
105};
106
107static void
108bind_decoration_manager(struct wl_client *client, void *data, uint32_t version,
109 uint32_t id)
110{
111 struct wl_resource *resource;
112
113 resource = wl_resource_create(client, &zxdg_decoration_manager_v1_interface,
114 version, id);
115 if (!resource) {
116 wl_client_post_no_memory(client);
117 return;
118 }
119 wl_resource_set_implementation(resource, &decoration_manager_impl, NULL,
120 NULL);
121}
122
123struct wl_global *
124xdg_decoration_manager_create(struct wl_display *display)
125{
126 return wl_global_create(display, &zxdg_decoration_manager_v1_interface, 1,
127 NULL, &bind_decoration_manager);
128}