1 /*
2 * Copyright © 2008-2011 Kristian Høgsberg
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 "config.h"
27
28 #include <stdint.h>
29 #include <linux/input.h>
30
31 #include <libweston/libweston.h>
32 #include "weston.h"
33 #include "weston-screenshooter-server-protocol.h"
34 #include "shared/helpers.h"
35 #include <libweston/weston-log.h>
36
37 struct screenshooter {
38 struct weston_compositor *ec;
39 struct wl_global *global;
40 struct wl_client *client;
41 struct weston_process process;
42 struct wl_listener destroy_listener;
43 struct weston_recorder *recorder;
44 };
45
46 static void
screenshooter_done(void * data,enum weston_screenshooter_outcome outcome)47 screenshooter_done(void *data, enum weston_screenshooter_outcome outcome)
48 {
49 struct wl_resource *resource = data;
50
51 switch (outcome) {
52 case WESTON_SCREENSHOOTER_SUCCESS:
53 weston_screenshooter_send_done(resource);
54 break;
55 case WESTON_SCREENSHOOTER_NO_MEMORY:
56 wl_resource_post_no_memory(resource);
57 break;
58 default:
59 break;
60 }
61 }
62
63 static void
screenshooter_shoot(struct wl_client * client,struct wl_resource * resource,struct wl_resource * output_resource,struct wl_resource * buffer_resource)64 screenshooter_shoot(struct wl_client *client,
65 struct wl_resource *resource,
66 struct wl_resource *output_resource,
67 struct wl_resource *buffer_resource)
68 {
69 struct weston_output *output =
70 weston_head_from_resource(output_resource)->output;
71 struct weston_buffer *buffer =
72 weston_buffer_from_resource(buffer_resource);
73
74 if (buffer == NULL) {
75 wl_resource_post_no_memory(resource);
76 return;
77 }
78
79 weston_screenshooter_shoot(output, buffer, screenshooter_done, resource);
80 }
81
82 struct weston_screenshooter_interface screenshooter_implementation = {
83 screenshooter_shoot
84 };
85
86 static void
bind_shooter(struct wl_client * client,void * data,uint32_t version,uint32_t id)87 bind_shooter(struct wl_client *client,
88 void *data, uint32_t version, uint32_t id)
89 {
90 struct screenshooter *shooter = data;
91 struct wl_resource *resource;
92 // OHOS remove debugger
93 // bool debug_enabled =
94 // weston_compositor_is_debug_protocol_enabled(shooter->ec);
95 bool debug_enabled = false;
96
97 resource = wl_resource_create(client,
98 &weston_screenshooter_interface, 1, id);
99
100 if (!debug_enabled && !shooter->client) {
101 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
102 "screenshooter failed: permission denied. "\
103 "Debug protocol must be enabled");
104 return;
105 } else if (!debug_enabled && client != shooter->client) {
106 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
107 "screenshooter failed: permission denied.");
108 return;
109 }
110
111 wl_resource_set_implementation(resource, &screenshooter_implementation,
112 data, NULL);
113 }
114
115 static void
screenshooter_sigchld(struct weston_process * process,int status)116 screenshooter_sigchld(struct weston_process *process, int status)
117 {
118 struct screenshooter *shooter =
119 container_of(process, struct screenshooter, process);
120
121 shooter->client = NULL;
122 }
123
124 static void
screenshooter_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)125 screenshooter_binding(struct weston_keyboard *keyboard,
126 const struct timespec *time, uint32_t key, void *data)
127 {
128 struct screenshooter *shooter = data;
129 char *screenshooter_exe;
130
131
132 screenshooter_exe = wet_get_bindir_path("weston-screenshooter");
133 if (!screenshooter_exe) {
134 weston_log("Could not construct screenshooter path.\n");
135 return;
136 }
137
138 if (!shooter->client)
139 shooter->client = weston_client_launch(shooter->ec,
140 &shooter->process,
141 screenshooter_exe, screenshooter_sigchld);
142 free(screenshooter_exe);
143 }
144
145 static void
recorder_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)146 recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
147 uint32_t key, void *data)
148 {
149 struct weston_compositor *ec = keyboard->seat->compositor;
150 struct weston_output *output;
151 struct screenshooter *shooter = data;
152 struct weston_recorder *recorder = shooter->recorder;;
153 static const char filename[] = "capture.wcap";
154
155 if (recorder) {
156 weston_recorder_stop(recorder);
157 shooter->recorder = NULL;
158 } else {
159 if (keyboard->focus && keyboard->focus->output)
160 output = keyboard->focus->output;
161 else
162 output = container_of(ec->output_list.next,
163 struct weston_output, link);
164
165 shooter->recorder = weston_recorder_start(output, filename);
166 }
167 }
168
169 static void
screenshooter_destroy(struct wl_listener * listener,void * data)170 screenshooter_destroy(struct wl_listener *listener, void *data)
171 {
172 struct screenshooter *shooter =
173 container_of(listener, struct screenshooter, destroy_listener);
174
175 wl_list_remove(&shooter->destroy_listener.link);
176
177 wl_global_destroy(shooter->global);
178 free(shooter);
179 }
180
181 WL_EXPORT void
screenshooter_create(struct weston_compositor * ec)182 screenshooter_create(struct weston_compositor *ec)
183 {
184 struct screenshooter *shooter;
185
186 shooter = zalloc(sizeof *shooter);
187 if (shooter == NULL)
188 return;
189
190 shooter->ec = ec;
191
192 shooter->global = wl_global_create(ec->wl_display,
193 &weston_screenshooter_interface, 1,
194 shooter, bind_shooter);
195 weston_compositor_add_key_binding(ec, KEY_S, MODIFIER_SUPER,
196 screenshooter_binding, shooter);
197 weston_compositor_add_key_binding(ec, KEY_R, MODIFIER_SUPER,
198 recorder_binding, shooter);
199
200 shooter->destroy_listener.notify = screenshooter_destroy;
201 wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
202 }
203