3624a8cb4c21799049d16b11ccb20dc60d869057
[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 "ivi-compositor.h"
27 #include "policy.h"
28
29 #include <libweston/libweston.h>
30 #include <libweston-desktop/libweston-desktop.h>
31
32 #if 0
33 static struct weston_output *
34 get_default_output(struct weston_compositor *compositor)
35 {
36         if (wl_list_empty(&compositor->output_list))
37                 return NULL;
38
39         return wl_container_of(compositor->output_list.next,
40                                struct weston_output, link);
41 }
42 #endif
43
44 static void
45 desktop_ping_timeout(struct weston_desktop_client *dclient, void *userdata)
46 {
47         /* not supported */
48 }
49
50 static void
51 desktop_pong(struct weston_desktop_client *dclient, void *userdata)
52 {
53         /* not supported */
54 }
55
56 static void
57 desktop_surface_added(struct weston_desktop_surface *dsurface, void *userdata)
58 {
59         struct ivi_compositor *ivi = userdata;
60         struct weston_desktop_client *dclient;
61         struct wl_client *client;
62         struct ivi_surface *surface;
63
64         dclient = weston_desktop_surface_get_client(dsurface);
65         client = weston_desktop_client_get_client(dclient);
66
67         surface = zalloc(sizeof *surface);
68         if (!surface) {
69                 wl_client_post_no_memory(client);
70                 return;
71         }
72
73         surface->view = weston_desktop_surface_create_view(dsurface);
74         if (!surface->view) {
75                 free(surface);
76                 wl_client_post_no_memory(client);
77                 return;
78         }
79
80         surface->ivi = ivi;
81         surface->dsurface = dsurface;
82         surface->role = IVI_SURFACE_ROLE_NONE;
83         surface->activated_by_default = false;
84
85         if (ivi->policy && ivi->policy->api.surface_create &&
86             !ivi->policy->api.surface_create(surface, ivi)) {
87                 free(surface);
88                 wl_client_post_no_memory(client);
89                 return;
90         }
91
92         weston_desktop_surface_set_user_data(dsurface, surface);
93
94         if (ivi->shell_client.ready) {
95                 ivi_check_pending_desktop_surface(surface);
96         } else {
97                 /*
98                  * We delay creating "normal" desktop surfaces until later, to
99                  * give the shell-client an oppurtunity to set the surface as a
100                  * background/panel.
101                  */
102                 wl_list_insert(&ivi->pending_surfaces, &surface->link);
103         }
104 }
105
106 static void
107 desktop_surface_removed(struct weston_desktop_surface *dsurface, void *userdata)
108 {
109         struct ivi_surface *surface =
110                 weston_desktop_surface_get_user_data(dsurface);
111         struct weston_surface *wsurface =
112                 weston_desktop_surface_get_surface(dsurface);
113
114         struct ivi_output *output;
115
116         if (surface->role == IVI_SURFACE_ROLE_DESKTOP)
117                 output = surface->desktop.last_output;
118         else if (surface->role == IVI_SURFACE_ROLE_POPUP)
119                 output = surface->popup.output;
120         else
121                 return;
122
123         /* reset the active surface as well */
124         if (output && output->active && output->active == surface) {
125                 output->active->view->is_mapped = false;
126                 output->active->view->surface->is_mapped = false;
127
128                 weston_layer_entry_remove(&output->active->view->layer_link);
129                 output->active = NULL;
130         }
131         if (weston_surface_is_mapped(wsurface)) {
132                 weston_desktop_surface_unlink_view(surface->view);
133                 weston_view_destroy(surface->view);
134         }
135
136         wl_list_remove(&surface->link);
137         free(surface);
138 }
139
140 static void
141 desktop_committed(struct weston_desktop_surface *dsurface, 
142                   int32_t sx, int32_t sy, void *userdata)
143 {
144         struct ivi_surface *surface =
145                 weston_desktop_surface_get_user_data(dsurface);
146         struct ivi_policy *policy = surface->ivi->policy;
147
148         if (policy && policy->api.surface_commited &&
149             !policy->api.surface_commited(surface, surface->ivi))
150                 return;
151
152         weston_compositor_schedule_repaint(surface->ivi->compositor);
153
154         switch (surface->role) {
155         case IVI_SURFACE_ROLE_DESKTOP:
156                 ivi_layout_desktop_committed(surface);
157                 break;
158         case IVI_SURFACE_ROLE_PANEL:
159                 ivi_layout_panel_committed(surface);
160                 break;
161         case IVI_SURFACE_ROLE_POPUP:
162                 ivi_layout_popup_committed(surface);
163                 break;
164         case IVI_SURFACE_ROLE_NONE:
165         case IVI_SURFACE_ROLE_BACKGROUND:
166         default: /* fall through */
167                 break;
168         }
169 }
170
171 static void
172 desktop_show_window_menu(struct weston_desktop_surface *dsurface,
173                          struct weston_seat *seat, int32_t x, int32_t y,
174                          void *userdata)
175 {
176         /* not supported */
177 }
178
179 static void
180 desktop_set_parent(struct weston_desktop_surface *dsurface,
181                    struct weston_desktop_surface *parent, void *userdata)
182 {
183         /* not supported */
184 }
185
186 static void
187 desktop_move(struct weston_desktop_surface *dsurface,
188              struct weston_seat *seat, uint32_t serial, void *userdata)
189 {
190         /* not supported */
191 }
192
193 static void
194 desktop_resize(struct weston_desktop_surface *dsurface,
195                struct weston_seat *seat, uint32_t serial,
196                enum weston_desktop_surface_edge edges, void *user_data)
197 {
198         /* not supported */
199 }
200
201 static void
202 desktop_fullscreen_requested(struct weston_desktop_surface *dsurface,
203                              bool fullscreen, struct weston_output *output,
204                              void *userdata)
205 {
206         /* not supported */
207 }
208
209 static void
210 desktop_maximized_requested(struct weston_desktop_surface *dsurface,
211                             bool maximized, void *userdata)
212 {
213         /* not supported */
214 }
215
216 static void
217 desktop_minimized_requested(struct weston_desktop_surface *dsurface,
218                             void *userdata)
219 {
220         /* not supported */
221 }
222
223 static void
224 desktop_set_xwayland_position(struct weston_desktop_surface *dsurface,
225                               int32_t x, int32_t y, void *userdata)
226 {
227         /* not supported */
228 }
229
230 static const struct weston_desktop_api desktop_api = {
231         .struct_size = sizeof desktop_api,
232         .ping_timeout = desktop_ping_timeout,
233         .pong = desktop_pong,
234         .surface_added = desktop_surface_added,
235         .surface_removed = desktop_surface_removed,
236         .committed = desktop_committed,
237         .show_window_menu = desktop_show_window_menu,
238         .set_parent = desktop_set_parent,
239         .move = desktop_move,
240         .resize = desktop_resize,
241         .fullscreen_requested = desktop_fullscreen_requested,
242         .maximized_requested = desktop_maximized_requested,
243         .minimized_requested = desktop_minimized_requested,
244         .set_xwayland_position = desktop_set_xwayland_position,
245 };
246
247 int
248 ivi_desktop_init(struct ivi_compositor *ivi)
249 {
250         ivi->desktop = weston_desktop_create(ivi->compositor, &desktop_api, ivi);
251         if (!ivi->desktop) {
252                 weston_log("Failed to create desktop globals");
253                 return -1;
254         }
255
256         return 0;
257 }