protocol: Advertise the applications to regular clients
[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                 ivi_set_desktop_surface(surface);
311         }
312 }
313
314 static void
315 shell_set_background(struct wl_client *client,
316                      struct wl_resource *shell_res,
317                      struct wl_resource *surface_res,
318                      struct wl_resource *output_res)
319 {
320         struct weston_head *head = weston_head_from_resource(output_res);
321         struct weston_output *woutput = weston_head_get_output(head);
322         struct ivi_output *output = to_ivi_output(woutput);
323         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
324         struct weston_desktop_surface *dsurface;
325         struct ivi_surface *surface;
326
327         dsurface = weston_surface_get_desktop_surface(wsurface);
328         if (!dsurface) {
329                 wl_resource_post_error(shell_res,
330                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
331                                        "surface must be a desktop surface");
332                 return;
333         }
334
335         surface = weston_desktop_surface_get_user_data(dsurface);
336         if (surface->role != IVI_SURFACE_ROLE_NONE) {
337                 wl_resource_post_error(shell_res,
338                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
339                                        "surface already has another ivi role");
340                 return;
341         }
342
343         if (output->background) {
344                 wl_resource_post_error(shell_res,
345                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
346                                        "output already has background");
347                 return;
348         }
349
350         surface->role = IVI_SURFACE_ROLE_BACKGROUND;
351         surface->bg.output = output;
352         wl_list_remove(&surface->link);
353         wl_list_init(&surface->link);
354
355         output->background = surface;
356
357         weston_desktop_surface_set_maximized(dsurface, true);
358         weston_desktop_surface_set_size(dsurface,
359                                         output->output->width,
360                                         output->output->height);
361 }
362
363 static void
364 shell_set_panel(struct wl_client *client,
365                 struct wl_resource *shell_res,
366                 struct wl_resource *surface_res,
367                 struct wl_resource *output_res,
368                 uint32_t edge)
369 {
370         struct weston_head *head = weston_head_from_resource(output_res);
371         struct weston_output *woutput = weston_head_get_output(head);
372         struct ivi_output *output = to_ivi_output(woutput);
373         struct weston_surface *wsurface = wl_resource_get_user_data(surface_res);
374         struct weston_desktop_surface *dsurface;
375         struct ivi_surface *surface;
376         struct ivi_surface **member;
377         int32_t width = 0, height = 0;
378
379         dsurface = weston_surface_get_desktop_surface(wsurface);
380         if (!dsurface) {
381                 wl_resource_post_error(shell_res,
382                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
383                                        "surface must be a desktop surface");
384                 return;
385         }
386
387         surface = weston_desktop_surface_get_user_data(dsurface);
388         if (surface->role != IVI_SURFACE_ROLE_NONE) {
389                 wl_resource_post_error(shell_res,
390                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
391                                        "surface already has another ivi role");
392                 return;
393         }
394
395         switch (edge) {
396         case AGL_SHELL_EDGE_TOP:
397                 member = &output->top;
398                 break;
399         case AGL_SHELL_EDGE_BOTTOM:
400                 member = &output->bottom;
401                 break;
402         case AGL_SHELL_EDGE_LEFT:
403                 member = &output->left;
404                 break;
405         case AGL_SHELL_EDGE_RIGHT:
406                 member = &output->right;
407                 break;
408         default:
409                 wl_resource_post_error(shell_res,
410                                        AGL_SHELL_ERROR_INVALID_ARGUMENT,
411                                        "invalid edge for panel");
412                 return;
413         }
414
415         if (*member) {
416                 wl_resource_post_error(shell_res,
417                                        AGL_SHELL_ERROR_BACKGROUND_EXISTS,
418                                        "output already has panel on this edge");
419                 return;
420         }
421
422         surface->role = IVI_SURFACE_ROLE_PANEL;
423         surface->panel.output = output;
424         surface->panel.edge = edge;
425         wl_list_remove(&surface->link);
426         wl_list_init(&surface->link);
427
428         *member = surface;
429
430         switch (surface->panel.edge) {
431         case AGL_SHELL_EDGE_TOP:
432         case AGL_SHELL_EDGE_BOTTOM:
433                 width = woutput->width;
434                 break;
435         case AGL_SHELL_EDGE_LEFT:
436         case AGL_SHELL_EDGE_RIGHT:
437                 height = woutput->height;
438                 break;
439         }
440
441         weston_desktop_surface_set_size(dsurface, width, height);
442 }
443
444 static void
445 shell_activate_app(struct wl_client *client,
446                    struct wl_resource *shell_res,
447                    const char *app_id,
448                    struct wl_resource *output_res)
449 {
450         struct weston_head *head = weston_head_from_resource(output_res);
451         struct weston_output *woutput = weston_head_get_output(head);
452         struct ivi_output *output = to_ivi_output(woutput);
453
454         ivi_layout_activate(output, app_id);
455 }
456
457 static const struct agl_shell_interface agl_shell_implementation = {
458         .ready = shell_ready,
459         .set_background = shell_set_background,
460         .set_panel = shell_set_panel,
461         .activate_app = shell_activate_app,
462 };
463
464 static const struct agl_shell_desktop_interface agl_shell_desktop_implementation = {
465         .activate_app = shell_activate_app,
466 };
467
468 static void
469 unbind_agl_shell(struct wl_resource *resource)
470 {
471         struct ivi_compositor *ivi;
472         struct ivi_output *output;
473         struct ivi_surface *surf, *surf_tmp;
474
475         ivi = wl_resource_get_user_data(resource);
476         wl_list_for_each(output, &ivi->outputs, link) {
477                 free(output->background);
478                 output->background = NULL;
479
480                 free(output->top);
481                 output->top = NULL;
482
483                 free(output->bottom);
484                 output->bottom = NULL;
485
486                 free(output->left);
487                 output->left = NULL;
488
489                 free(output->right);
490                 output->right = NULL;
491
492                 /* reset the active surf if there's one present */
493                 if (output->active) {
494                         output->active->view->is_mapped = false;
495                         output->active->view->surface->is_mapped = false;
496
497                         weston_layer_entry_remove(&output->active->view->layer_link);
498                         output->active = NULL;
499                 }
500
501                 insert_black_surface(output);
502         }
503
504         wl_list_for_each_safe(surf, surf_tmp, &ivi->surfaces, link) {
505                 wl_list_remove(&surf->link);
506                 wl_list_init(&surf->link);
507         }
508
509         wl_list_for_each_safe(surf, surf_tmp, &ivi->pending_surfaces, link) {
510                 wl_list_remove(&surf->link);
511                 wl_list_init(&surf->link);
512         }
513
514         wl_list_init(&ivi->surfaces);
515         wl_list_init(&ivi->pending_surfaces);
516
517         ivi->shell_client.ready = false;
518         ivi->shell_client.resource = NULL;
519         ivi->shell_client.client = NULL;
520 }
521
522 static void
523 bind_agl_shell(struct wl_client *client,
524                void *data, uint32_t version, uint32_t id)
525 {
526         struct ivi_compositor *ivi = data;
527         struct wl_resource *resource;
528
529         resource = wl_resource_create(client, &agl_shell_interface,
530                                       1, id);
531         if (!resource) {
532                 wl_client_post_no_memory(client);
533                 return;
534         }
535
536 #if 0
537         if (ivi->shell_client.client != client) {
538                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
539                                        "client not authorized to use agl_shell");
540                 return;
541         }
542 #endif
543
544         if (ivi->shell_client.resource) {
545                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
546                                        "agl_shell has already been bound");
547                 return;
548         }
549
550         wl_resource_set_implementation(resource, &agl_shell_implementation,
551                                        ivi, unbind_agl_shell);
552         ivi->shell_client.resource = resource;
553 }
554
555 static void
556 unbind_agl_shell_desktop(struct wl_resource *resource)
557 {
558         struct desktop_client *dclient = wl_resource_get_user_data(resource);
559
560         wl_list_remove(&dclient->link);
561         free(dclient);
562 }
563
564 static void
565 bind_agl_shell_desktop(struct wl_client *client,
566                        void *data, uint32_t version, uint32_t id)
567 {
568         struct ivi_compositor *ivi = data;
569         struct wl_resource *resource;
570         struct desktop_client *dclient = zalloc(sizeof(*dclient));
571
572         if (!dclient) {
573                 wl_client_post_no_memory(client);
574                 return;
575         }
576
577         resource = wl_resource_create(client, &agl_shell_desktop_interface,
578                                       version, id);
579         if (!resource) {
580                 wl_client_post_no_memory(client);
581                 return;
582         }
583
584         wl_resource_set_implementation(resource, &agl_shell_desktop_implementation,
585                                        dclient, unbind_agl_shell_desktop);
586
587         dclient->resource = resource;
588         wl_list_insert(&ivi->desktop_clients, &dclient->link);
589
590         /* advertise xdg surfaces */
591         ivi_shell_advertise_xdg_surfaces(ivi, resource);
592 }
593
594 int
595 ivi_shell_create_global(struct ivi_compositor *ivi)
596 {
597         ivi->agl_shell = wl_global_create(ivi->compositor->wl_display,
598                                           &agl_shell_interface, 1,
599                                           ivi, bind_agl_shell);
600         if (!ivi->agl_shell) {
601                 weston_log("Failed to create wayland global.\n");
602                 return -1;
603         }
604
605         ivi->agl_shell_desktop = wl_global_create(ivi->compositor->wl_display,
606                                                   &agl_shell_desktop_interface, 1,
607                                                   ivi, bind_agl_shell_desktop);
608         if (!ivi->agl_shell_desktop) {
609                 weston_log("Failed to create wayland global (agl_shell_desktop).\n");
610                 return -1;
611         }
612
613         return 0;
614 }