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