Invoke grim when taking screenshot

Backends should provide their own UI. As I don't want to copy-paste grim and
slurp here, let's just exec them. This should be made configurable in the
future.
This commit is contained in:
emersion 2018-12-21 19:00:32 +01:00
parent 2f2fe91ff4
commit 2b659c42a7
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48

View file

@ -1,11 +1,41 @@
#include <errno.h> #include <errno.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include "xdpw.h" #include "xdpw.h"
static const char object_path[] = "/org/freedesktop/portal/desktop"; static const char object_path[] = "/org/freedesktop/portal/desktop";
static const char interface_name[] = "org.freedesktop.impl.portal.Screenshot"; 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 int method_screenshot(sd_bus_message *msg, void *data, static int method_screenshot(sd_bus_message *msg, void *data,
sd_bus_error *ret_error) { sd_bus_error *ret_error) {
int ret = 0; int ret = 0;
@ -55,8 +85,17 @@ static int method_screenshot(sd_bus_message *msg, void *data,
return ret; return ret;
} }
// TODO: take an actual screenshot // TODO: choose a better path
ret = sd_bus_message_append_basic(reply, 's', "file:///tmp/out.png"); const char path[] = "/tmp/out.png";
if (!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);
ret = sd_bus_message_append_basic(reply, 's', uri);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }