-
Notifications
You must be signed in to change notification settings - Fork 61
/
screenshot.c
300 lines (252 loc) · 6.33 KB
/
screenshot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include "xdpw.h"
#include "screenshot.h"
static const char object_path[] = "/org/freedesktop/portal/desktop";
static const char interface_name[] = "org.freedesktop.impl.portal.Screenshot";
static bool exec_screenshooter(const char *path) {
pid_t pid = fork();
if (pid < 0) {
perror("fork");
return false;
} else if (pid == 0) {
char *const argv[] = {
"grim",
"--",
(char *)path,
NULL,
};
execvp("grim", argv);
perror("execvp");
exit(127);
}
int stat;
if (waitpid(pid, &stat, 0) < 0) {
perror("waitpid");
return false;
}
return stat == 0;
}
static bool exec_screenshooter_interactive(const char *path) {
pid_t pid = fork();
if (pid < 0) {
perror("fork");
return false;
} else if (pid == 0) {
char cmd[strlen(path) + 25];
snprintf(cmd, sizeof(cmd), "grim -g \"$(slurp)\" -- %s", path);
execl("/bin/sh", "/bin/sh", "-c", cmd, NULL);
perror("execl");
exit(127);
}
int stat;
if (waitpid(pid, &stat, 0) < 0) {
perror("waitpid");
return false;
}
return stat == 0;
}
static int method_screenshot(sd_bus_message *msg, void *data,
sd_bus_error *ret_error) {
int ret = 0;
bool interactive = false;
char *handle, *app_id, *parent_window;
ret = sd_bus_message_read(msg, "oss", &handle, &app_id, &parent_window);
if (ret < 0) {
return ret;
}
ret = sd_bus_message_enter_container(msg, 'a', "{sv}");
if (ret < 0) {
return ret;
}
char *key;
int inner_ret = 0;
while ((ret = sd_bus_message_enter_container(msg, 'e', "sv")) > 0) {
inner_ret = sd_bus_message_read(msg, "s", &key);
if (inner_ret < 0) {
return inner_ret;
}
if (strcmp(key, "interactive") == 0) {
int mode;
sd_bus_message_read(msg, "v", "b", &mode);
logprint(DEBUG, "dbus: option interactive: %d", mode);
interactive = mode;
} else if (strcmp(key, "modal") == 0) {
int modal;
sd_bus_message_read(msg, "v", "b", &modal);
logprint(DEBUG, "dbus: option modal: %d", modal);
} else {
logprint(WARN, "dbus: unknown option %s", key);
sd_bus_message_skip(msg, "v");
}
inner_ret = sd_bus_message_exit_container(msg);
if (inner_ret < 0) {
return inner_ret;
}
}
if (ret < 0) {
return ret;
}
ret = sd_bus_message_exit_container(msg);
if (ret < 0) {
return ret;
}
// TODO: cleanup this
struct xdpw_request *req =
xdpw_request_create(sd_bus_message_get_bus(msg), handle);
if (req == NULL) {
return -ENOMEM;
}
// TODO: choose a better path
const char path[] = "/tmp/out.png";
if (interactive && !exec_screenshooter_interactive(path)) {
return -1;
}
if (!interactive && !exec_screenshooter(path)) {
return -1;
}
const char uri_prefix[] = "file://";
char uri[strlen(path) + strlen(uri_prefix) + 1];
snprintf(uri, sizeof(uri), "%s%s", uri_prefix, path);
sd_bus_message *reply = NULL;
ret = sd_bus_message_new_method_return(msg, &reply);
if (ret < 0) {
return ret;
}
ret = sd_bus_message_append(reply, "ua{sv}", PORTAL_RESPONSE_SUCCESS, 1, "uri", "s", uri);
if (ret < 0) {
return ret;
}
ret = sd_bus_send(NULL, reply, NULL);
if (ret < 0) {
return ret;
}
sd_bus_message_unref(reply);
return 0;
}
static bool spawn_chooser(int chooser_out[2]) {
pid_t pid = fork();
if (pid < 0) {
perror("fork");
return false;
} else if (pid == 0) {
close(chooser_out[0]);
dup2(chooser_out[1], STDOUT_FILENO);
close(chooser_out[1]);
execl("/bin/sh", "/bin/sh", "-c", "grim -g \"$(slurp -p)\" -t ppm -", NULL);
perror("execl");
_exit(127);
}
int stat;
if (waitpid(pid, &stat, 0) < 0) {
perror("waitpid");
return false;
}
close(chooser_out[1]);
return stat == 0;
}
static bool exec_color_picker(struct xdpw_ppm_pixel *pixel) {
int chooser_out[2];
if (pipe(chooser_out) == -1) {
perror("pipe chooser_out");
return false;
}
if (!spawn_chooser(chooser_out)) {
logprint(ERROR, "Selection failed");
close(chooser_out[0]);
return false;
}
FILE *f = fdopen(chooser_out[0], "r");
if (f == NULL) {
perror("fopen pipe chooser_out");
close(chooser_out[0]);
return false;
}
char *format = NULL;
size_t len = 1;
if (getline(&format, &len, f) < 0) {
goto error_img;
}
if (strcmp(format, "P6\n") != 0) {
goto error_img;
}
if (getline(&format, &len, f) < 0) {
goto error_img;
}
if (strcmp(format, "1 1\n") != 0) {
goto error_img;
}
if (fscanf(f, "%d\n", &pixel->max_color_value) != 1) {
goto error_img;
}
unsigned char pixels[3];
if (fread(pixels, 3, 1, f) != 1) {
goto error_img;
}
pixel->red = pixels[0];
pixel->green = pixels[1];
pixel->blue = pixels[2];
free(format);
fclose(f);
return true;
error_img:
logprint(WARN, "Invalid image format or size");
free(format);
fclose(f);
return false;
}
static int method_pick_color(sd_bus_message *msg, void *data,
sd_bus_error *ret_error) {
int ret = 0;
char *handle, *app_id, *parent_window;
ret = sd_bus_message_read(msg, "oss", &handle, &app_id, &parent_window);
if (ret < 0) {
return ret;
}
struct xdpw_request *req =
xdpw_request_create(sd_bus_message_get_bus(msg), handle);
if (req == NULL) {
return -ENOMEM;
}
struct xdpw_ppm_pixel pixel = {0};
if (!exec_color_picker(&pixel)) {
return -1;
}
double red = pixel.red / (pixel.max_color_value * 1.0);
double green = pixel.green / (pixel.max_color_value * 1.0);
double blue = pixel.blue / (pixel.max_color_value * 1.0);
sd_bus_message *reply = NULL;
ret = sd_bus_message_new_method_return(msg, &reply);
if (ret < 0) {
return ret;
}
ret = sd_bus_message_append(reply, "ua{sv}", PORTAL_RESPONSE_SUCCESS, 1, "color", "(ddd)", red, green, blue);
if (ret < 0) {
return ret;
}
ret = sd_bus_send(NULL, reply, NULL);
if (ret < 0) {
return ret;
}
sd_bus_message_unref(reply);
return 0;
}
static const sd_bus_vtable screenshot_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_METHOD("Screenshot", "ossa{sv}", "ua{sv}", method_screenshot, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("PickColor", "ossa{sv}", "ua{sv}", method_pick_color, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_PROPERTY("version", "u", NULL,
offsetof(struct xdpw_state, screenshot_version),
SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_VTABLE_END
};
int xdpw_screenshot_init(struct xdpw_state *state) {
// TODO: cleanup
sd_bus_slot *slot = NULL;
return sd_bus_add_object_vtable(state->bus, &slot, object_path, interface_name,
screenshot_vtable, state);
}