shell: Add a black surface in the fullscreen layer
[src/agl-compositor.git] / src / shell.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
28 #include <assert.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <signal.h>
32 #include <string.h>
33 #include <sys/socket.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <libweston-6/compositor.h>
37 #include <libweston-6/config-parser.h>
38
39 #include "shared/os-compatibility.h"
40
41 #include "agl-shell-server-protocol.h"
42
43 static void
44 create_black_surface_view(struct ivi_output *output);
45
46 static void
47 insert_black_surface(struct ivi_output *output);
48
49 void
50 ivi_set_desktop_surface(struct ivi_surface *surface)
51 {
52         assert(surface->role == IVI_SURFACE_ROLE_NONE);
53
54         surface->role = IVI_SURFACE_ROLE_DESKTOP;
55         wl_list_insert(&surface->ivi->surfaces, &surface->link);
56 }
57
58 void
59 ivi_shell_init_black_fs(struct ivi_compositor *ivi)
60 {
61         struct ivi_output *out;
62
63         wl_list_for_each(out, &ivi->outputs, link) {
64                 create_black_surface_view(out);
65                 insert_black_surface(out);
66         }
67 }
68
69 int
70 ivi_shell_init(struct ivi_compositor *ivi)
71 {
72         weston_layer_init(&ivi->hidden, ivi->compositor);
73         weston_layer_init(&ivi->background, ivi->compositor);
74         weston_layer_init(&ivi->normal, ivi->compositor);
75         weston_layer_init(&ivi->panel, ivi->compositor);
76         weston_layer_init(&ivi->fullscreen, ivi->compositor);
77
78         weston_layer_set_position(&ivi->hidden,
79                                   WESTON_LAYER_POSITION_HIDDEN);
80         weston_layer_set_position(&ivi->background,
81                                   WESTON_LAYER_POSITION_BACKGROUND);
82         weston_layer_set_position(&ivi->normal,
83                                   WESTON_LAYER_POSITION_NORMAL);
84         weston_layer_set_position(&ivi->panel,
85                                   WESTON_LAYER_POSITION_UI);
86         weston_layer_set_position(&ivi->fullscreen,
87                                   WESTON_LAYER_POSITION_FULLSCREEN);
88
89         return 0;
90 }
91
92 static void
93 client_exec(const char *command, int fd)
94 {
95         sigset_t sig;
96         char s[32];
97
98         /* Don't give the child our signal mask */
99         sigfillset(&sig);
100         sigprocmask(SIG_UNBLOCK, &sig, NULL);
101
102         /* Launch clients as the user; don't give them the wrong euid */
103         if (seteuid(getuid()) == -1) {
104                 weston_log("seteuid failed: %s\n", strerror(errno));
105                 return;
106         }
107
108         /* Duplicate fd to unset the CLOEXEC flag. We don't need to worry about
109          * clobbering fd, as we'll exit/exec either way.
110          */
111         fd = dup(fd);
112         if (fd == -1) {
113                 weston_log("dup failed: %s\n", strerror(errno));
114                 return;
115         }
116
117         snprintf(s, sizeof s, "%d", fd);
118         setenv("WAYLAND_SOCKET", s, 1);
119
120         execl("/bin/sh", "/bin/sh", "-c", command, NULL);
121         weston_log("executing '%s' failed: %s", command, strerror(errno));
122 }
123
124 static struct wl_client *
125 launch_shell_client(struct ivi_compositor *ivi, const char *command)
126 {
127         struct wl_client *client;
128         int sock[2];
129         pid_t pid;
130
131         weston_log("launching' %s'\n", command);
132
133         if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sock) < 0) {
134                 weston_log("socketpair failed while launching '%s': %s\n",
135                            command, strerror(errno));
136                 return NULL;
137         }
138
139         pid = fork();
140         if (pid == -1) {
141                 close(sock[0]);
142                 close(sock[1]);
143                 weston_log("fork failed while launching '%s': %s\n",
144                            command, strerror(errno));
145                 return NULL;
146         }
147
148         if (pid == 0) {
149                 client_exec(command, sock[1]);
150                 _Exit(EXIT_FAILURE);
151         }
152         close(sock[1]);
153
154         client = wl_client_create(ivi->compositor->wl_display, sock[0]);
155         if (!client) {
156                 close(sock[0]);
157                 weston_log("Failed to create wayland client for '%s'",
158                            command);
159                 return NULL;
160         }
161
162         return client;
163 }
164
165 int
166 ivi_launch_shell_client(struct ivi_compositor *ivi)
167 {
168         struct weston_config_section *section;
169         char *command = NULL;
170
171         section = weston_config_get_section(ivi->config, "shell-client",
172                                             NULL, NULL);
173         if (section)
174                 weston_config_section_get_string(section, "command",
175                                                  &command, NULL);
176
177         if (!command)
178                 return -1;
179
180         ivi->shell_client.client = launch_shell_client(ivi, command);
181         if (!ivi->shell_client.client)
182                 return -1;
183
184         return 0;
185 }
186
187 static void
188 destroy_black_view(struct wl_listener *listener, void *data)
189 {
190         struct fullscreen_view *fs =
191                 wl_container_of(listener, fs, fs_destroy);
192
193
194         if (fs && fs->fs) {
195                 if (fs->fs->view && fs->fs->view->surface) {
196                         weston_surface_destroy(fs->fs->view->surface);
197                         fs->fs->view = NULL;
198                 }
199
200                 free(fs->fs);
201                 wl_list_remove(&fs->fs_destroy.link);
202         }
203 }
204
205
206 static void
207 create_black_surface_view(struct ivi_output *output)
208 {
209         struct weston_surface *surface = NULL;
210         struct weston_view *view;
211         struct ivi_compositor *ivi = output->ivi;
212         struct weston_compositor *wc= ivi->compositor;
213         struct weston_output *woutput = output->output;
214
215         surface = weston_surface_create(wc);
216         view = weston_view_create(surface);
217
218         assert(view || surface);
219
220         weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
221         weston_surface_set_size(surface, woutput->width, woutput->height);
222         weston_view_set_position(view, woutput->x, woutput->y);
223
224         output->fullscreen_view.fs = zalloc(sizeof(struct ivi_surface));
225         output->fullscreen_view.fs->view = view;
226
227         output->fullscreen_view.fs_destroy.notify = destroy_black_view;
228         wl_signal_add(&woutput->destroy_signal,
229                       &output->fullscreen_view.fs_destroy);
230 }
231
232 static void
233 remove_black_surface(struct ivi_output *output)
234 {
235         struct weston_view *view = output->fullscreen_view.fs->view;
236
237         assert(view->is_mapped == true ||
238                view->surface->is_mapped == true);
239
240         view->is_mapped = false;
241         view->surface->is_mapped = false;
242
243         weston_layer_entry_remove(&view->layer_link);
244         weston_view_update_transform(view);
245
246         weston_output_damage(output->output);
247 }
248
249 static void
250 insert_black_surface(struct ivi_output *output)
251 {
252         struct weston_view *view = output->fullscreen_view.fs->view;
253
254         if (view->is_mapped || view->surface->is_mapped)
255                 return;
256
257         weston_layer_entry_remove(&view->layer_link);
258         weston_layer_entry_insert(&output->ivi->fullscreen.view_list,
259                                   &view->layer_link);
260
261         view->is_mapped = true;
262         view->surface->is_mapped = true;
263
264         weston_view_update_transform(view);
265         weston_output_damage(output->output);
266 }
267
268 static void
269 shell_ready(struct wl_client *client, struct wl_resource *shell_res)
270 {
271         struct ivi_compositor *ivi = wl_resource_get_user_data(shell_res);
272         struct ivi_output *output;
273         struct ivi_surface *surface, *tmp;
274
275         /* Init already finished. Do nothing */
276         if (ivi->shell_client.ready)
277                 return;
278
279         ivi->shell_client.ready = true;
280
281         wl_list_for_each(output, &ivi->outputs, link) {
282                 remove_black_surface(output);
283                 ivi_layout_init(ivi, output);
284         }
285
286         wl_list_for_each_safe(surface, tmp, &ivi->pending_surfaces, link) {
287                 wl_list_remove(&surface->link);
288                 ivi_set_desktop_surface(surface);
289         }
290 }
291
292 static void
293 shell_set_background(struct wl_client *client,
294                      struct wl_resource *shell_res,
295                      struct wl_resource *surface_res,
296                      struct wl_resource *output_res)
297 {
298         struct weston_head *head = weston_head_from_resource(output_res);
299         struct weston_output *woutput = weston_head_get_output(head);
300         struct ivi_output *output = to_ivi_output(woutput);
301         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
302         struct weston_desktop_surface *dsurface;
303         struct ivi_surface *surface;
304
305         dsurface = weston_surface_get_desktop_surface(wsurface);
306         if (!dsurface) {
307                 wl_resource_post_error(shell_res,
308                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
309                                        "surface must be a desktop surface");
310                 return;
311         }
312
313         surface = weston_desktop_surface_get_user_data(dsurface);
314         if (surface->role != IVI_SURFACE_ROLE_NONE) {
315                 wl_resource_post_error(shell_res,
316                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
317                                        "surface already has another ivi role");
318                 return;
319         }
320
321         if (output->background) {
322                 wl_resource_post_error(shell_res,
323                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
324                                        "output already has background");
325                 return;
326         }
327
328         surface->role = IVI_SURFACE_ROLE_BACKGROUND;
329         surface->bg.output = output;
330         wl_list_remove(&surface->link);
331         wl_list_init(&surface->link);
332
333         output->background = surface;
334
335         weston_desktop_surface_set_maximized(dsurface, true);
336         weston_desktop_surface_set_size(dsurface,
337                                         output->output->width,
338                                         output->output->height);
339 }
340
341 static void
342 shell_set_panel(struct wl_client *client,
343                 struct wl_resource *shell_res,
344                 struct wl_resource *surface_res,
345                 struct wl_resource *output_res,
346                 uint32_t edge)
347 {
348         struct weston_head *head = weston_head_from_resource(output_res);
349         struct weston_output *woutput = weston_head_get_output(head);
350         struct ivi_output *output = to_ivi_output(woutput);
351         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
352         struct weston_desktop_surface *dsurface;
353         struct ivi_surface *surface;
354         struct ivi_surface **member;
355         int32_t width = 0, height = 0;
356
357         dsurface = weston_surface_get_desktop_surface(wsurface);
358         if (!dsurface) {
359                 wl_resource_post_error(shell_res,
360                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
361                                        "surface must be a desktop surface");
362                 return;
363         }
364
365         surface = weston_desktop_surface_get_user_data(dsurface);
366         if (surface->role != IVI_SURFACE_ROLE_NONE) {
367                 wl_resource_post_error(shell_res,
368                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
369                                        "surface already has another ivi role");
370                 return;
371         }
372
373         switch (edge) {
374         case AGL_SHELL_EDGE_TOP:
375                 member = &output->top;
376                 break;
377         case AGL_SHELL_EDGE_BOTTOM:
378                 member = &output->bottom;
379                 break;
380         case AGL_SHELL_EDGE_LEFT:
381                 member = &output->left;
382                 break;
383         case AGL_SHELL_EDGE_RIGHT:
384                 member = &output->right;
385                 break;
386         default:
387                 wl_resource_post_error(shell_res,
388                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
389                                        "invalid edge for panel");
390                 return;
391         }
392
393         if (*member) {
394                 wl_resource_post_error(shell_res,
395                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
396                                        "output already has panel on this edge");
397                 return;
398         }
399
400         surface->role = IVI_SURFACE_ROLE_PANEL;
401         surface->panel.output = output;
402         surface->panel.edge = edge;
403         wl_list_remove(&surface->link);
404         wl_list_init(&surface->link);
405
406         *member = surface;
407
408         switch (surface->panel.edge) {
409         case AGL_SHELL_EDGE_TOP:
410         case AGL_SHELL_EDGE_BOTTOM:
411                 width = woutput->width;
412                 break;
413         case AGL_SHELL_EDGE_LEFT:
414         case AGL_SHELL_EDGE_RIGHT:
415                 height = woutput->height;
416                 break;
417         }
418
419         weston_desktop_surface_set_size(dsurface, width, height);
420 }
421
422 static void
423 shell_activate_app(struct wl_client *client,
424                    struct wl_resource *shell_res,
425                    const char *app_id,
426                    struct wl_resource *output_res)
427 {
428         struct weston_head *head = weston_head_from_resource(output_res);
429         struct weston_output *woutput = weston_head_get_output(head);
430         struct ivi_output *output = to_ivi_output(woutput);
431
432         ivi_layout_activate(output, app_id);
433 }
434
435 static const struct agl_shell_interface agl_shell_implementation = {
436         .ready = shell_ready,
437         .set_background = shell_set_background,
438         .set_panel = shell_set_panel,
439         .activate_app = shell_activate_app,
440 };
441
442 static void
443 unbind_agl_shell(struct wl_resource *resource)
444 {
445         struct ivi_compositor *ivi;
446         struct ivi_output *output;
447         struct ivi_surface *surf, *surf_tmp;
448
449         ivi = wl_resource_get_user_data(resource);
450         wl_list_for_each(output, &ivi->outputs, link) {
451                 free(output->background);
452                 output->background = NULL;
453
454                 free(output->top);
455                 output->top = NULL;
456
457                 free(output->bottom);
458                 output->bottom = NULL;
459
460                 free(output->left);
461                 output->left = NULL;
462
463                 free(output->right);
464                 output->right = NULL;
465
466                 /* reset the active surf if there's one present */
467                 if (output->active) {
468                         output->active->view->is_mapped = false;
469                         output->active->view->surface->is_mapped = false;
470
471                         weston_layer_entry_remove(&output->active->view->layer_link);
472                         output->active = NULL;
473                 }
474
475                 insert_black_surface(output);
476         }
477
478         wl_list_for_each_safe(surf, surf_tmp, &ivi->surfaces, link) {
479                 wl_list_remove(&surf->link);
480                 wl_list_init(&surf->link);
481         }
482
483         wl_list_for_each_safe(surf, surf_tmp, &ivi->pending_surfaces, link) {
484                 wl_list_remove(&surf->link);
485                 wl_list_init(&surf->link);
486         }
487
488         wl_list_init(&ivi->surfaces);
489         wl_list_init(&ivi->pending_surfaces);
490
491         ivi->shell_client.ready = false;
492         ivi->shell_client.resource = NULL;
493         ivi->shell_client.client = NULL;
494 }
495
496 static void
497 bind_agl_shell(struct wl_client *client,
498                void *data, uint32_t version, uint32_t id)
499 {
500         struct ivi_compositor *ivi = data;
501         struct wl_resource *resource;
502
503         resource = wl_resource_create(client, &agl_shell_interface,
504                                       1, id);
505         if (!resource) {
506                 wl_client_post_no_memory(client);
507                 return;
508         }
509
510 #if 0
511         if (ivi->shell_client.client != client) {
512                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
513                                        "client not authorized to use agl_shell");
514                 return;
515         }
516 #endif
517
518         if (ivi->shell_client.resource) {
519                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
520                                        "agl_shell has already been bound");
521                 return;
522         }
523
524         wl_resource_set_implementation(resource, &agl_shell_implementation,
525                                        ivi, unbind_agl_shell);
526         ivi->shell_client.resource = resource;
527 }
528
529 int
530 ivi_shell_create_global(struct ivi_compositor *ivi)
531 {
532         ivi->agl_shell = wl_global_create(ivi->compositor->wl_display,
533                                           &agl_shell_interface, 1,
534                                           ivi, bind_agl_shell);
535         if (!ivi->agl_shell) {
536                 weston_log("Failed to create wayland global.\n");
537                 return -1;
538         }
539
540         return 0;
541 }