mirror of
https://github.com/hyprwm/xdg-desktop-portal-hyprland.git
synced 2024-11-21 22:25:58 +01:00
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:
parent
2f2fe91ff4
commit
2b659c42a7
1 changed files with 41 additions and 2 deletions
43
screenshot.c
43
screenshot.c
|
@ -1,11 +1,41 @@
|
|||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue