blob: 4583ede22c66299f8402aa4cbb9a50108b5cee9b [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;
43 struct wl_global *xdg_shell_v5;
44 struct wl_global *wl_shell;
45};
46
47void
48weston_desktop_destroy_request(struct wl_client *client,
49 struct wl_resource *resource)
50{
51 wl_resource_destroy(resource);
52}
53
54WL_EXPORT struct weston_desktop *
55weston_desktop_create(struct weston_compositor *compositor,
56 const struct weston_desktop_api *api, void *user_data)
57{
58 struct weston_desktop *desktop;
59 struct wl_display *display = compositor->wl_display;
60
61 assert(api->surface_added);
62 assert(api->surface_removed);
63
64 desktop = zalloc(sizeof(struct weston_desktop));
65 desktop->compositor = compositor;
66 desktop->user_data = user_data;
67
68 desktop->api.struct_size =
69 MIN(sizeof(struct weston_desktop_api), api->struct_size);
70 memcpy(&desktop->api, api, desktop->api.struct_size);
71
72 desktop->xdg_shell_v5 =
73 weston_desktop_xdg_shell_v5_create(desktop, display);
74 if (desktop->xdg_shell_v5 == NULL) {
75 weston_desktop_destroy(desktop);
76 return NULL;
77 }
78
79 desktop->wl_shell =
80 weston_desktop_wl_shell_create(desktop, display);
81 if (desktop->wl_shell == NULL) {
82 weston_desktop_destroy(desktop);
83 return NULL;
84 }
85
86 weston_desktop_xwayland_init(desktop);
87
88 return desktop;
89}
90
91WL_EXPORT void
92weston_desktop_destroy(struct weston_desktop *desktop)
93{
94 if (desktop == NULL)
95 return;
96
97 if (desktop->wl_shell != NULL)
98 wl_global_destroy(desktop->wl_shell);
99 if (desktop->xdg_shell_v5 != NULL)
100 wl_global_destroy(desktop->xdg_shell_v5);
101
102 free(desktop);
103}
104
105
106struct weston_compositor *
107weston_desktop_get_compositor(struct weston_desktop *desktop)
108{
109 return desktop->compositor;
110}
111
112struct wl_display *
113weston_desktop_get_display(struct weston_desktop *desktop)
114{
115 return desktop->compositor->wl_display;
116}
117
118void
119weston_desktop_api_ping_timeout(struct weston_desktop *desktop,
120 struct weston_desktop_client *client)
121{
122 if (desktop->api.ping_timeout != NULL)
123 desktop->api.ping_timeout(client, desktop->user_data);
124}
125
126void
127weston_desktop_api_pong(struct weston_desktop *desktop,
128 struct weston_desktop_client *client)
129{
130 if (desktop->api.pong != NULL)
131 desktop->api.pong(client, desktop->user_data);
132}
133
134void
135weston_desktop_api_surface_added(struct weston_desktop *desktop,
136 struct weston_desktop_surface *surface)
137{
138 struct weston_desktop_client *client =
139 weston_desktop_surface_get_client(surface);
140 struct wl_list *list = weston_desktop_client_get_surface_list(client);
141 struct wl_list *link = weston_desktop_surface_get_client_link(surface);
142
143 desktop->api.surface_added(surface, desktop->user_data);
144 wl_list_insert(list, link);
145}
146
147void
148weston_desktop_api_surface_removed(struct weston_desktop *desktop,
149 struct weston_desktop_surface *surface)
150{
151 struct wl_list *link = weston_desktop_surface_get_client_link(surface);
152
153 wl_list_remove(link);
154 wl_list_init(link);
155 desktop->api.surface_removed(surface, desktop->user_data);
156}
157
158void
159weston_desktop_api_committed(struct weston_desktop *desktop,
160 struct weston_desktop_surface *surface,
161 int32_t sx, int32_t sy)
162{
163 if (desktop->api.committed != NULL)
164 desktop->api.committed(surface, sx, sy, desktop->user_data);
165}
166
167void
168weston_desktop_api_show_window_menu(struct weston_desktop *desktop,
169 struct weston_desktop_surface *surface,
170 struct weston_seat *seat,
171 int32_t x, int32_t y)
172{
173 if (desktop->api.show_window_menu != NULL)
174 desktop->api.show_window_menu(surface, seat, x, y,
175 desktop->user_data);
176}
177
178void
179weston_desktop_api_set_parent(struct weston_desktop *desktop,
180 struct weston_desktop_surface *surface,
181 struct weston_desktop_surface *parent)
182{
183 if (desktop->api.set_parent != NULL)
184 desktop->api.set_parent(surface, parent, desktop->user_data);
185}
186
187void
188weston_desktop_api_move(struct weston_desktop *desktop,
189 struct weston_desktop_surface *surface,
190 struct weston_seat *seat, uint32_t serial)
191{
192 if (desktop->api.move != NULL)
193 desktop->api.move(surface, seat, serial, desktop->user_data);
194}
195
196void
197weston_desktop_api_resize(struct weston_desktop *desktop,
198 struct weston_desktop_surface *surface,
199 struct weston_seat *seat, uint32_t serial,
200 enum weston_desktop_surface_edge edges)
201{
202 if (desktop->api.resize != NULL)
203 desktop->api.resize(surface, seat, serial, edges,
204 desktop->user_data);
205}
206
207void
208weston_desktop_api_fullscreen_requested(struct weston_desktop *desktop,
209 struct weston_desktop_surface *surface,
210 bool fullscreen,
211 struct weston_output *output)
212{
213 if (desktop->api.fullscreen_requested != NULL)
214 desktop->api.fullscreen_requested(surface, fullscreen, output,
215 desktop->user_data);
216}
217
218void
219weston_desktop_api_maximized_requested(struct weston_desktop *desktop,
220 struct weston_desktop_surface *surface,
221 bool maximized)
222{
223 if (desktop->api.maximized_requested != NULL)
224 desktop->api.maximized_requested(surface, maximized,
225 desktop->user_data);
226}
227
228void
229weston_desktop_api_minimized_requested(struct weston_desktop *desktop,
230 struct weston_desktop_surface *surface)
231{
232 if (desktop->api.minimized_requested != NULL)
233 desktop->api.minimized_requested(surface, desktop->user_data);
234}