compositor: Add missing SIGCHLD handler
[src/agl-compositor.git] / src / screenshooter.c
1 /*
2  * Copyright © 2020 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 "shared/helpers.h"
28
29 #include <libweston/libweston.h>
30 #include "agl-screenshooter-server-protocol.h"
31 #include <libweston/weston-log.h>
32
33 struct screenshooter {
34         struct ivi_compositor *ivi;
35         struct wl_global *global;
36         struct wl_client *client;
37         struct wl_listener destroy_listener;
38 };
39
40 static void
41 screenshooter_done(void *data, enum weston_screenshooter_outcome outcome)
42 {
43         struct wl_resource *resource = data;
44
45         if (outcome == WESTON_SCREENSHOOTER_NO_MEMORY) {
46                 wl_resource_post_no_memory(resource);
47                 return;
48         }
49
50         agl_screenshooter_send_done(resource, outcome);
51 }
52
53 static void
54 screenshooter_shoot(struct wl_client *client,
55                     struct wl_resource *resource,
56                     struct wl_resource *output_resource,
57                     struct wl_resource *buffer_resource)
58 {
59         struct weston_output *output =
60                 weston_head_from_resource(output_resource)->output;
61         struct weston_buffer *buffer =
62                 weston_buffer_from_resource(buffer_resource);
63
64         if (buffer == NULL) {
65                 wl_resource_post_no_memory(resource);
66                 return;
67         }
68
69         weston_screenshooter_shoot(output, buffer, screenshooter_done, resource);
70 }
71
72 static void
73 screenshooter_destructor_destroy(struct wl_client *client,
74                                  struct wl_resource *global_resource)
75 {
76         wl_resource_destroy(global_resource);
77 }
78
79 struct agl_screenshooter_interface screenshooter_implementation = {
80         screenshooter_shoot,
81         screenshooter_destructor_destroy
82 };
83
84 static void
85 bind_shooter(struct wl_client *client,
86              void *data, uint32_t version, uint32_t id)
87 {
88         struct screenshooter *shooter = data;
89         struct wl_resource *resource;
90         bool debug_enabled = true;
91
92         resource = wl_resource_create(client,
93                                       &agl_screenshooter_interface, 1, id);
94
95         if (!debug_enabled && !shooter->client) {
96                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
97                                        "screenshooter failed: permission denied. "\
98                                        "Debug must be enabled");
99                 return;
100         }
101
102         wl_resource_set_implementation(resource, &screenshooter_implementation,
103                                        data, NULL);
104 }
105
106 static void
107 screenshooter_destroy(struct wl_listener *listener, void *data)
108 {
109         struct screenshooter *shooter =
110                 container_of(listener, struct screenshooter, destroy_listener);
111
112         wl_list_remove(&shooter->destroy_listener.link);
113
114         wl_global_destroy(shooter->global);
115         free(shooter);
116 }
117
118 void
119 ivi_screenshooter_create(struct ivi_compositor *ivi)
120 {
121         struct weston_compositor *ec = ivi->compositor;
122         struct screenshooter *shooter;
123
124         shooter = zalloc(sizeof(*shooter));
125         if (shooter == NULL)
126                 return;
127
128         shooter->ivi = ivi;
129         shooter->global = wl_global_create(ec->wl_display,
130                                            &agl_screenshooter_interface, 1,
131                                            shooter, bind_shooter);
132
133         shooter->destroy_listener.notify = screenshooter_destroy;
134         wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
135
136         weston_log("Screenshooter interface created\n");
137 }