• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <libweston/libweston.h>
32 #include <libweston/zalloc.h>
33 #include "shared/helpers.h"
34 
35 #include <libweston-desktop/libweston-desktop.h>
36 #include "internal.h"
37 
38 
39 struct weston_desktop {
40 	struct weston_compositor *compositor;
41 	struct weston_desktop_api api;
42 	void *user_data;
43 	struct wl_global *xdg_wm_base;	 /* Stable protocol xdg_shell replaces xdg_shell_unstable_v6 */
44 	struct wl_global *xdg_shell_v6;  /* Unstable xdg_shell_unstable_v6 protocol. */
45 	struct wl_global *wl_shell;
46 };
47 
48 void
weston_desktop_destroy_request(struct wl_client * client,struct wl_resource * resource)49 weston_desktop_destroy_request(struct wl_client *client,
50 			       struct wl_resource *resource)
51 {
52 	wl_resource_destroy(resource);
53 }
54 
55 WL_EXPORT struct weston_desktop *
weston_desktop_create(struct weston_compositor * compositor,const struct weston_desktop_api * api,void * user_data)56 weston_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 
73 	desktop->xdg_wm_base =
74 		weston_desktop_xdg_wm_base_create(desktop, display);
75 	if (desktop->xdg_wm_base == NULL) {
76 		weston_desktop_destroy(desktop);
77 		return NULL;
78 	}
79 
80 	desktop->xdg_shell_v6 =
81 		weston_desktop_xdg_shell_v6_create(desktop, display);
82 	if (desktop->xdg_shell_v6 == 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 // OHOS
95 //	weston_desktop_xwayland_init(desktop);
96 
97 	return desktop;
98 }
99 
100 WL_EXPORT void
weston_desktop_destroy(struct weston_desktop * desktop)101 weston_desktop_destroy(struct weston_desktop *desktop)
102 {
103 	if (desktop == NULL)
104 		return;
105 
106 	if (desktop->wl_shell != NULL)
107 		wl_global_destroy(desktop->wl_shell);
108 	if (desktop->xdg_shell_v6 != NULL)
109 		wl_global_destroy(desktop->xdg_shell_v6);
110 	if (desktop->xdg_wm_base != NULL)
111 		wl_global_destroy(desktop->xdg_wm_base);
112 
113 	free(desktop);
114 }
115 
116 
117 struct weston_compositor *
weston_desktop_get_compositor(struct weston_desktop * desktop)118 weston_desktop_get_compositor(struct weston_desktop *desktop)
119 {
120 	return desktop->compositor;
121 }
122 
123 struct wl_display *
weston_desktop_get_display(struct weston_desktop * desktop)124 weston_desktop_get_display(struct weston_desktop *desktop)
125 {
126 	return desktop->compositor->wl_display;
127 }
128 
129 void
weston_desktop_api_ping_timeout(struct weston_desktop * desktop,struct weston_desktop_client * client)130 weston_desktop_api_ping_timeout(struct weston_desktop *desktop,
131 				struct weston_desktop_client *client)
132 {
133 	if (desktop->api.ping_timeout != NULL)
134 		desktop->api.ping_timeout(client, desktop->user_data);
135 }
136 
137 void
weston_desktop_api_pong(struct weston_desktop * desktop,struct weston_desktop_client * client)138 weston_desktop_api_pong(struct weston_desktop *desktop,
139 			struct weston_desktop_client *client)
140 {
141 	if (desktop->api.pong != NULL)
142 		desktop->api.pong(client, desktop->user_data);
143 }
144 
145 void
weston_desktop_api_surface_added(struct weston_desktop * desktop,struct weston_desktop_surface * surface)146 weston_desktop_api_surface_added(struct weston_desktop *desktop,
147 				 struct weston_desktop_surface *surface)
148 {
149 	struct weston_desktop_client *client =
150 		weston_desktop_surface_get_client(surface);
151 	struct wl_list *list = weston_desktop_client_get_surface_list(client);
152 	struct wl_list *link = weston_desktop_surface_get_client_link(surface);
153 
154 	desktop->api.surface_added(surface, desktop->user_data);
155 	wl_list_insert(list, link);
156 }
157 
158 void
weston_desktop_api_surface_removed(struct weston_desktop * desktop,struct weston_desktop_surface * surface)159 weston_desktop_api_surface_removed(struct weston_desktop *desktop,
160 				   struct weston_desktop_surface *surface)
161 {
162 	struct wl_list *link = weston_desktop_surface_get_client_link(surface);
163 
164 	wl_list_remove(link);
165 	wl_list_init(link);
166 	desktop->api.surface_removed(surface, desktop->user_data);
167 }
168 
169 void
weston_desktop_api_committed(struct weston_desktop * desktop,struct weston_desktop_surface * surface,int32_t sx,int32_t sy)170 weston_desktop_api_committed(struct weston_desktop *desktop,
171 			     struct weston_desktop_surface *surface,
172 			     int32_t sx, int32_t sy)
173 {
174 	if (desktop->api.committed != NULL)
175 		desktop->api.committed(surface, sx, sy, desktop->user_data);
176 }
177 
178 void
weston_desktop_api_show_window_menu(struct weston_desktop * desktop,struct weston_desktop_surface * surface,struct weston_seat * seat,int32_t x,int32_t y)179 weston_desktop_api_show_window_menu(struct weston_desktop *desktop,
180 				    struct weston_desktop_surface *surface,
181 				    struct weston_seat *seat,
182 				    int32_t x, int32_t y)
183 {
184 	if (desktop->api.show_window_menu != NULL)
185 		desktop->api.show_window_menu(surface, seat, x, y,
186 					      desktop->user_data);
187 }
188 
189 void
weston_desktop_api_set_parent(struct weston_desktop * desktop,struct weston_desktop_surface * surface,struct weston_desktop_surface * parent)190 weston_desktop_api_set_parent(struct weston_desktop *desktop,
191 			      struct weston_desktop_surface *surface,
192 			      struct weston_desktop_surface *parent)
193 {
194 	if (desktop->api.set_parent != NULL)
195 		desktop->api.set_parent(surface, parent, desktop->user_data);
196 }
197 
198 void
weston_desktop_api_move(struct weston_desktop * desktop,struct weston_desktop_surface * surface,struct weston_seat * seat,uint32_t serial)199 weston_desktop_api_move(struct weston_desktop *desktop,
200 			struct weston_desktop_surface *surface,
201 			struct weston_seat *seat, uint32_t serial)
202 {
203 	if (desktop->api.move != NULL)
204 		desktop->api.move(surface, seat, serial, desktop->user_data);
205 }
206 
207 void
weston_desktop_api_resize(struct weston_desktop * desktop,struct weston_desktop_surface * surface,struct weston_seat * seat,uint32_t serial,enum weston_desktop_surface_edge edges)208 weston_desktop_api_resize(struct weston_desktop *desktop,
209 			  struct weston_desktop_surface *surface,
210 			  struct weston_seat *seat, uint32_t serial,
211 			  enum weston_desktop_surface_edge edges)
212 {
213 	if (desktop->api.resize != NULL)
214 		desktop->api.resize(surface, seat, serial, edges,
215 				    desktop->user_data);
216 }
217 
218 void
weston_desktop_api_fullscreen_requested(struct weston_desktop * desktop,struct weston_desktop_surface * surface,bool fullscreen,struct weston_output * output)219 weston_desktop_api_fullscreen_requested(struct weston_desktop *desktop,
220 					struct weston_desktop_surface *surface,
221 					bool fullscreen,
222 					struct weston_output *output)
223 {
224 	if (desktop->api.fullscreen_requested != NULL)
225 		desktop->api.fullscreen_requested(surface, fullscreen, output,
226 						  desktop->user_data);
227 }
228 
229 void
weston_desktop_api_maximized_requested(struct weston_desktop * desktop,struct weston_desktop_surface * surface,bool maximized)230 weston_desktop_api_maximized_requested(struct weston_desktop *desktop,
231 				       struct weston_desktop_surface *surface,
232 				       bool maximized)
233 {
234 	if (desktop->api.maximized_requested != NULL)
235 		desktop->api.maximized_requested(surface, maximized,
236 						 desktop->user_data);
237 }
238 
239 void
weston_desktop_api_minimized_requested(struct weston_desktop * desktop,struct weston_desktop_surface * surface)240 weston_desktop_api_minimized_requested(struct weston_desktop *desktop,
241 				       struct weston_desktop_surface *surface)
242 {
243 	if (desktop->api.minimized_requested != NULL)
244 		desktop->api.minimized_requested(surface, desktop->user_data);
245 }
246 
247 void
weston_desktop_api_set_xwayland_position(struct weston_desktop * desktop,struct weston_desktop_surface * surface,int32_t x,int32_t y)248 weston_desktop_api_set_xwayland_position(struct weston_desktop *desktop,
249 					 struct weston_desktop_surface *surface,
250 					 int32_t x, int32_t y)
251 {
252 	if (desktop->api.set_xwayland_position != NULL)
253 		desktop->api.set_xwayland_position(surface, x, y,
254 						   desktop->user_data);
255 }
256