blob: 0ee11393512956d78210ab63818c6b4192a6b9ce [file] [log] [blame]
Quentin Glidic248dd102016-08-12 10:41:34 +02001/*
2 * Copyright © 2016 Quentin "Sardem FF7" Glidic
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * 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
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#include "config.h"
25
26#include <string.h>
27
28#include <wayland-server.h>
29#include <assert.h>
30
31#include "compositor.h"
32#include "zalloc.h"
33#include "helpers.h"
34
35#include "libweston-desktop.h"
36#include "internal.h"
37
38
39struct weston_desktop {
40 struct weston_compositor *compositor;
41 struct weston_desktop_api api;
42 void *user_data;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +020043 struct wl_global *xdg_shell_v6;
Quentin Glidic248dd102016-08-12 10:41:34 +020044 struct wl_global *xdg_shell_v5;
45 struct wl_global *wl_shell;
46};
47
48void
49weston_desktop_destroy_request(struct wl_client *client,
50 struct wl_resource *resource)
51{
52 wl_resource_destroy(resource);
53}
54
55WL_EXPORT struct weston_desktop *
56weston_desktop_create(struct weston_compositor *compositor,
57 const struct weston_desktop_api *api, void *user_data)
58{
59 struct weston_desktop *desktop;
60 struct wl_display *display = compositor->wl_display;
61
62 assert(api->surface_added);
63 assert(api->surface_removed);
64
65 desktop = zalloc(sizeof(struct weston_desktop));
66 desktop->compositor = compositor;
67 desktop->user_data = user_data;
68
69 desktop->api.struct_size =
70 MIN(sizeof(struct weston_desktop_api), api->struct_size);
71 memcpy(&desktop->api, api, desktop->api.struct_size);
72
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +020073 desktop->xdg_shell_v6 =
74 weston_desktop_xdg_shell_v6_create(desktop, display);
75 if (desktop->xdg_shell_v6 == NULL) {
76 weston_desktop_destroy(desktop);
77 return NULL;
78 }
79
Quentin Glidic248dd102016-08-12 10:41:34 +020080 desktop->xdg_shell_v5 =
81 weston_desktop_xdg_shell_v5_create(desktop, display);
82 if (desktop->xdg_shell_v5 == NULL) {
83 weston_desktop_destroy(desktop);
84 return NULL;
85 }
86
87 desktop->wl_shell =
88 weston_desktop_wl_shell_create(desktop, display);
89 if (desktop->wl_shell == NULL) {
90 weston_desktop_destroy(desktop);
91 return NULL;
92 }
93
94 weston_desktop_xwayland_init(desktop);
95
96 return desktop;
97}
98
99WL_EXPORT void
100weston_desktop_destroy(struct weston_desktop *desktop)
101{
102 if (desktop == NULL)
103 return;
104
105 if (desktop->wl_shell != NULL)
106 wl_global_destroy(desktop->wl_shell);
107 if (desktop->xdg_shell_v5 != NULL)
108 wl_global_destroy(desktop->xdg_shell_v5);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200109 if (desktop->xdg_shell_v6 != NULL)
110 wl_global_destroy(desktop->xdg_shell_v6);
Quentin Glidic248dd102016-08-12 10:41:34 +0200111
112 free(desktop);
113}
114
115
116struct weston_compositor *
117weston_desktop_get_compositor(struct weston_desktop *desktop)
118{
119 return desktop->compositor;
120}
121
122struct wl_display *
123weston_desktop_get_display(struct weston_desktop *desktop)
124{
125 return desktop->compositor->wl_display;
126}
127
128void
129weston_desktop_api_ping_timeout(struct weston_desktop *desktop,
130 struct weston_desktop_client *client)
131{
132 if (desktop->api.ping_timeout != NULL)
133 desktop->api.ping_timeout(client, desktop->user_data);
134}
135
136void
137weston_desktop_api_pong(struct weston_desktop *desktop,
138 struct weston_desktop_client *client)
139{
140 if (desktop->api.pong != NULL)
141 desktop->api.pong(client, desktop->user_data);
142}
143
144void
145weston_desktop_api_surface_added(struct weston_desktop *desktop,
146 struct weston_desktop_surface *surface)
147{
148 struct weston_desktop_client *client =
149 weston_desktop_surface_get_client(surface);
150 struct wl_list *list = weston_desktop_client_get_surface_list(client);
151 struct wl_list *link = weston_desktop_surface_get_client_link(surface);
152
153 desktop->api.surface_added(surface, desktop->user_data);
154 wl_list_insert(list, link);
155}
156
157void
158weston_desktop_api_surface_removed(struct weston_desktop *desktop,
159 struct weston_desktop_surface *surface)
160{
161 struct wl_list *link = weston_desktop_surface_get_client_link(surface);
162
163 wl_list_remove(link);
164 wl_list_init(link);
165 desktop->api.surface_removed(surface, desktop->user_data);
166}
167
168void
169weston_desktop_api_committed(struct weston_desktop *desktop,
170 struct weston_desktop_surface *surface,
171 int32_t sx, int32_t sy)
172{
173 if (desktop->api.committed != NULL)
174 desktop->api.committed(surface, sx, sy, desktop->user_data);
175}
176
177void
178weston_desktop_api_show_window_menu(struct weston_desktop *desktop,
179 struct weston_desktop_surface *surface,
180 struct weston_seat *seat,
181 int32_t x, int32_t y)
182{
183 if (desktop->api.show_window_menu != NULL)
184 desktop->api.show_window_menu(surface, seat, x, y,
185 desktop->user_data);
186}
187
188void
189weston_desktop_api_set_parent(struct weston_desktop *desktop,
190 struct weston_desktop_surface *surface,
191 struct weston_desktop_surface *parent)
192{
193 if (desktop->api.set_parent != NULL)
194 desktop->api.set_parent(surface, parent, desktop->user_data);
195}
196
197void
198weston_desktop_api_move(struct weston_desktop *desktop,
199 struct weston_desktop_surface *surface,
200 struct weston_seat *seat, uint32_t serial)
201{
202 if (desktop->api.move != NULL)
203 desktop->api.move(surface, seat, serial, desktop->user_data);
204}
205
206void
207weston_desktop_api_resize(struct weston_desktop *desktop,
208 struct weston_desktop_surface *surface,
209 struct weston_seat *seat, uint32_t serial,
210 enum weston_desktop_surface_edge edges)
211{
212 if (desktop->api.resize != NULL)
213 desktop->api.resize(surface, seat, serial, edges,
214 desktop->user_data);
215}
216
217void
218weston_desktop_api_fullscreen_requested(struct weston_desktop *desktop,
219 struct weston_desktop_surface *surface,
220 bool fullscreen,
221 struct weston_output *output)
222{
223 if (desktop->api.fullscreen_requested != NULL)
224 desktop->api.fullscreen_requested(surface, fullscreen, output,
225 desktop->user_data);
226}
227
228void
229weston_desktop_api_maximized_requested(struct weston_desktop *desktop,
230 struct weston_desktop_surface *surface,
231 bool maximized)
232{
233 if (desktop->api.maximized_requested != NULL)
234 desktop->api.maximized_requested(surface, maximized,
235 desktop->user_data);
236}
237
238void
239weston_desktop_api_minimized_requested(struct weston_desktop *desktop,
240 struct weston_desktop_surface *surface)
241{
242 if (desktop->api.minimized_requested != NULL)
243 desktop->api.minimized_requested(surface, desktop->user_data);
244}