From 2b659c42a7f63bb713a599359bbddf10fef32ae3 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 21 Dec 2018 19:00:32 +0100 Subject: [PATCH] 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. --- screenshot.c | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/screenshot.c b/screenshot.c index 787eee9..e6d620a 100644 --- a/screenshot.c +++ b/screenshot.c @@ -1,11 +1,41 @@ #include +#include #include #include +#include +#include #include "xdpw.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 int method_screenshot(sd_bus_message *msg, void *data, sd_bus_error *ret_error) { int ret = 0; @@ -55,8 +85,17 @@ static int method_screenshot(sd_bus_message *msg, void *data, return ret; } - // TODO: take an actual screenshot - ret = sd_bus_message_append_basic(reply, 's', "file:///tmp/out.png"); + // TODO: choose a better path + 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) { return ret; }