layout, desktop: Treat the remote surface role like the desktop one
[src/agl-compositor.git] / src / desktop.c
1 /*
2  * Copyright © 2019 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include <assert.h>
27 #include "ivi-compositor.h"
28 #include "policy.h"
29
30 #include <libweston/libweston.h>
31 #include <libweston-desktop/libweston-desktop.h>
32
33 #if 0
34 static struct weston_output *
35 get_default_output(struct weston_compositor *compositor)
36 {
37         if (wl_list_empty(&compositor->output_list))
38                 return NULL;
39
40         return wl_container_of(compositor->output_list.next,
41                                struct weston_output, link);
42 }
43 #endif
44
45 static void
46 desktop_ping_timeout(struct weston_desktop_client *dclient, void *userdata)
47 {
48         /* not supported */
49 }
50
51 static void
52 desktop_pong(struct weston_desktop_client *dclient, void *userdata)
53 {
54         /* not supported */
55 }
56
57 static void
58 desktop_surface_added(struct weston_desktop_surface *dsurface, void *userdata)
59 {
60         struct ivi_compositor *ivi = userdata;
61         struct weston_desktop_client *dclient;
62         struct wl_client *client;
63         struct ivi_surface *surface;
64
65         dclient = weston_desktop_surface_get_client(dsurface);
66         client = weston_desktop_client_get_client(dclient);
67
68         surface = zalloc(sizeof *surface);
69         if (!surface) {
70                 wl_client_post_no_memory(client);
71                 return;
72         }
73
74         surface->view = weston_desktop_surface_create_view(dsurface);
75         if (!surface->view) {
76                 free(surface);
77                 wl_client_post_no_memory(client);
78                 return;
79         }
80
81         surface->ivi = ivi;
82         surface->dsurface = dsurface;
83         surface->role = IVI_SURFACE_ROLE_NONE;
84         surface->activated_by_default = false;
85
86         if (ivi->policy && ivi->policy->api.surface_create &&
87             !ivi->policy->api.surface_create(surface, ivi)) {
88                 free(surface);
89                 wl_client_post_no_memory(client);
90                 return;
91         }
92
93         weston_desktop_surface_set_user_data(dsurface, surface);
94
95         if (ivi->shell_client.ready) {
96                 ivi_check_pending_desktop_surface(surface);
97         } else {
98                 /*
99                  * We delay creating "normal" desktop surfaces until later, to
100                  * give the shell-client an oppurtunity to set the surface as a
101                  * background/panel.
102                  */
103                 wl_list_insert(&ivi->pending_surfaces, &surface->link);
104         }
105 }
106
107 static void
108 desktop_surface_removed(struct weston_desktop_surface *dsurface, void *userdata)
109 {
110         struct ivi_surface *surface =
111                 weston_desktop_surface_get_user_data(dsurface);
112         struct weston_surface *wsurface =
113                 weston_desktop_surface_get_surface(dsurface);
114
115         struct ivi_output *output = ivi_layout_get_output_from_surface(surface);
116         assert(output != NULL);
117
118         /* resize the active surface to the original size */
119         if (surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
120             surface->role == IVI_SURFACE_ROLE_SPLIT_V) {
121                 if (output && output->active) {
122                         ivi_layout_desktop_resize(output->active, output->area_saved);
123                 }
124                 /* restore the area back so we can re-use it again if needed */
125                 output->area = output->area_saved;
126         }
127
128         /* reset the active surface as well */
129         if (output && output->active && output->active == surface) {
130                 output->active->view->is_mapped = false;
131                 output->active->view->surface->is_mapped = false;
132
133                 weston_layer_entry_remove(&output->active->view->layer_link);
134                 output->active = NULL;
135         }
136         if (weston_surface_is_mapped(wsurface)) {
137                 weston_desktop_surface_unlink_view(surface->view);
138                 weston_view_destroy(surface->view);
139         }
140
141         /* invalidate agl-shell surfaces so we can re-use them when
142          * binding again */
143         if (surface->role == IVI_SURFACE_ROLE_PANEL) {
144                 switch (surface->panel.edge) {
145                 case AGL_SHELL_EDGE_TOP:
146                         output->top = NULL;
147                         break;
148                 case AGL_SHELL_EDGE_BOTTOM:
149                         output->bottom = NULL;
150                         break;
151                 case AGL_SHELL_EDGE_LEFT:
152                         output->left = NULL;
153                         break;
154                 case AGL_SHELL_EDGE_RIGHT:
155                         output->right = NULL;
156                         break;
157                 default:
158                         assert(!"Invalid edge detected\n");
159                 }
160         } else if (surface->role == IVI_SURFACE_ROLE_BACKGROUND) {
161                 output->background = NULL;
162         }
163
164         wl_list_remove(&surface->link);
165         free(surface);
166 }
167
168 static void
169 desktop_committed(struct weston_desktop_surface *dsurface, 
170                   int32_t sx, int32_t sy, void *userdata)
171 {
172         struct ivi_surface *surface =
173                 weston_desktop_surface_get_user_data(dsurface);
174         struct ivi_policy *policy = surface->ivi->policy;
175
176         if (policy && policy->api.surface_commited &&
177             !policy->api.surface_commited(surface, surface->ivi))
178                 return;
179
180         weston_compositor_schedule_repaint(surface->ivi->compositor);
181
182         switch (surface->role) {
183         case IVI_SURFACE_ROLE_DESKTOP:
184         case IVI_SURFACE_ROLE_REMOTE:
185                 ivi_layout_desktop_committed(surface);
186                 break;
187         case IVI_SURFACE_ROLE_PANEL:
188                 ivi_layout_panel_committed(surface);
189                 break;
190         case IVI_SURFACE_ROLE_POPUP:
191                 ivi_layout_popup_committed(surface);
192                 break;
193         case IVI_SURFACE_ROLE_FULLSCREEN:
194                 ivi_layout_fullscreen_committed(surface);
195                 break;
196         case IVI_SURFACE_ROLE_SPLIT_H:
197         case IVI_SURFACE_ROLE_SPLIT_V:
198                 ivi_layout_split_committed(surface);
199                 break;
200         case IVI_SURFACE_ROLE_NONE:
201         case IVI_SURFACE_ROLE_BACKGROUND:
202         default: /* fall through */
203                 break;
204         }
205 }
206
207 static void
208 desktop_show_window_menu(struct weston_desktop_surface *dsurface,
209                          struct weston_seat *seat, int32_t x, int32_t y,
210                          void *userdata)
211 {
212         /* not supported */
213 }
214
215 static void
216 desktop_set_parent(struct weston_desktop_surface *dsurface,
217                    struct weston_desktop_surface *parent, void *userdata)
218 {
219         /* not supported */
220 }
221
222 static void
223 desktop_move(struct weston_desktop_surface *dsurface,
224              struct weston_seat *seat, uint32_t serial, void *userdata)
225 {
226         /* not supported */
227 }
228
229 static void
230 desktop_resize(struct weston_desktop_surface *dsurface,
231                struct weston_seat *seat, uint32_t serial,
232                enum weston_desktop_surface_edge edges, void *user_data)
233 {
234         /* not supported */
235 }
236
237 static void
238 desktop_fullscreen_requested(struct weston_desktop_surface *dsurface,
239                              bool fullscreen, struct weston_output *output,
240                              void *userdata)
241 {
242         /* not supported */
243 }
244
245 static void
246 desktop_maximized_requested(struct weston_desktop_surface *dsurface,
247                             bool maximized, void *userdata)
248 {
249         /* not supported */
250 }
251
252 static void
253 desktop_minimized_requested(struct weston_desktop_surface *dsurface,
254                             void *userdata)
255 {
256         /* not supported */
257 }
258
259 static void
260 desktop_set_xwayland_position(struct weston_desktop_surface *dsurface,
261                               int32_t x, int32_t y, void *userdata)
262 {
263         /* not supported */
264 }
265
266 static const struct weston_desktop_api desktop_api = {
267         .struct_size = sizeof desktop_api,
268         .ping_timeout = desktop_ping_timeout,
269         .pong = desktop_pong,
270         .surface_added = desktop_surface_added,
271         .surface_removed = desktop_surface_removed,
272         .committed = desktop_committed,
273         .show_window_menu = desktop_show_window_menu,
274         .set_parent = desktop_set_parent,
275         .move = desktop_move,
276         .resize = desktop_resize,
277         .fullscreen_requested = desktop_fullscreen_requested,
278         .maximized_requested = desktop_maximized_requested,
279         .minimized_requested = desktop_minimized_requested,
280         .set_xwayland_position = desktop_set_xwayland_position,
281 };
282
283 int
284 ivi_desktop_init(struct ivi_compositor *ivi)
285 {
286         ivi->desktop = weston_desktop_create(ivi->compositor, &desktop_api, ivi);
287         if (!ivi->desktop) {
288                 weston_log("Failed to create desktop globals");
289                 return -1;
290         }
291
292         return 0;
293 }