Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Input Capture Desktop Portal #268

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[submodule "hyprland-protocols"]
path = subprojects/hyprland-protocols
url = https://github.com/hyprwm/hyprland-protocols
url = https://github.com/3l0w/hyprland-protocols
branch = feat/input-capture-impl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

marking this one as a blocker until this is removed so we dont forget (don't resolve)

[submodule "subprojects/sdbus-cpp"]
path = subprojects/sdbus-cpp
url = https://github.com/Kistler-Group/sdbus-cpp
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pkg_check_modules(
libpipewire-0.3>=1.1.82
libspa-0.2
libdrm
libeis-1.0
uuid
gbm
hyprlang>=0.2.0
hyprutils>=0.2.6
Expand Down Expand Up @@ -130,6 +132,8 @@ protocolnew("${HYPRLAND_PROTOCOLS}/protocols" "hyprland-global-shortcuts-v1"
true)
protocolnew("${HYPRLAND_PROTOCOLS}/protocols" "hyprland-toplevel-export-v1"
true)
protocolnew("${HYPRLAND_PROTOCOLS}/protocols" "hyprland-input-capture-v1"
true)
protocolnew("stable/linux-dmabuf" "linux-dmabuf-v1" false)

# Installation
Expand Down
2 changes: 1 addition & 1 deletion hyprland-share-picker/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ executable('hyprland-share-picker',
sources,
ui_files,
moc,
dependencies: qtdep,
dependencies: [qtdep, dependency('hyprutils')],
install: true
)
2 changes: 1 addition & 1 deletion hyprland.portal
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[portal]
DBusName=org.freedesktop.impl.portal.desktop.hyprland
Interfaces=org.freedesktop.impl.portal.Screenshot;org.freedesktop.impl.portal.ScreenCast;org.freedesktop.impl.portal.GlobalShortcuts;
Interfaces=org.freedesktop.impl.portal.Screenshot;org.freedesktop.impl.portal.ScreenCast;org.freedesktop.impl.portal.GlobalShortcuts;org.freedesktop.impl.portal.InputCapture;
UseIn=wlroots;Hyprland;sway;Wayfire;river;
3 changes: 3 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ install_data(
'hyprland.portal',
install_dir: join_paths(get_option('datadir'), 'xdg-desktop-portal', 'portals'),
)
version = run_command('cat', files('VERSION'), check: true).stdout().strip()

add_project_arguments(f'-DXDPH_VERSION="@version@"', language : 'cpp')

inc = include_directories('.', 'protocols')

Expand Down
1 change: 1 addition & 0 deletions protocols/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ client_protocols = [
'wlr-foreign-toplevel-management-unstable-v1.xml',
hl_protocol_dir / 'protocols/hyprland-toplevel-export-v1.xml',
hl_protocol_dir / 'protocols/hyprland-global-shortcuts-v1.xml',
hl_protocol_dir / 'protocols/hyprland-input-capture-v1.xml',
wl_protocol_dir / 'stable/linux-dmabuf/linux-dmabuf-v1.xml',
]

Expand Down
78 changes: 51 additions & 27 deletions src/core/PortalManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "../helpers/MiscFunctions.hpp"

#include <pipewire/pipewire.h>
#include <poll.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
Expand All @@ -19,12 +18,21 @@ SOutput::SOutput(SP<CCWlOutput> output_) : output(output_) {

Debug::log(LOG, "Found output name {}", name);
});
output->setMode([this](CCWlOutput* r, uint32_t flags, int32_t width, int32_t height, int32_t refresh) { //
output->setMode([this](CCWlOutput* r, uint32_t flags, int32_t width_, int32_t height_, int32_t refresh) {
refreshRate = refresh;
width = width_;
height = height_;
});
output->setGeometry([this](CCWlOutput* r, int32_t x, int32_t y, int32_t physical_width, int32_t physical_height, int32_t subpixel, const char* make, const char* model,
int32_t transform_) { //
transform = (wl_output_transform)transform_;
output->setGeometry(
[this](CCWlOutput* r, int32_t x_, int32_t y_, int32_t physical_width, int32_t physical_height, int32_t subpixel, const char* make, const char* model, int32_t transform_) {
transform = (wl_output_transform)transform_;
x = x_;
y = y_;
});
output->setScale([this](CCWlOutput* r, uint32_t factor_) { scale = factor_; });
output->setDone([](CCWlOutput* r) {
if (g_pPortalManager->m_sPortals.inputCapture != nullptr)
g_pPortalManager->m_sPortals.inputCapture->zonesChanged();
});
}

Expand Down Expand Up @@ -63,7 +71,9 @@ void CPortalManager::onGlobal(uint32_t name, const char* interface, uint32_t ver
m_sPortals.globalShortcuts = std::make_unique<CGlobalShortcutsPortal>(makeShared<CCHyprlandGlobalShortcutsManagerV1>(
(wl_proxy*)wl_registry_bind((wl_registry*)m_sWaylandConnection.registry->resource(), name, &hyprland_global_shortcuts_manager_v1_interface, version)));
}

if (INTERFACE == hyprland_input_capture_manager_v1_interface.name)
m_sPortals.inputCapture = std::make_unique<CInputCapturePortal>(makeShared<CCHyprlandInputCaptureManagerV1>(
(wl_proxy*)wl_registry_bind((wl_registry*)m_sWaylandConnection.registry->resource(), name, &hyprland_input_capture_manager_v1_interface, version)));
else if (INTERFACE == hyprland_toplevel_export_manager_v1_interface.name) {
m_sWaylandConnection.hyprlandToplevelMgr = makeShared<CCHyprlandToplevelExportManagerV1>(
(wl_proxy*)wl_registry_bind((wl_registry*)m_sWaylandConnection.registry->resource(), name, &hyprland_toplevel_export_manager_v1_interface, version));
Expand Down Expand Up @@ -269,32 +279,21 @@ void CPortalManager::init() {
}

void CPortalManager::startEventLoop() {
addFdToEventLoop(m_pConnection->getEventLoopPollData().fd, POLLIN, nullptr);
addFdToEventLoop(wl_display_get_fd(m_sWaylandConnection.display), POLLIN, nullptr);
addFdToEventLoop(pw_loop_get_fd(m_sPipewire.loop), POLLIN, nullptr);

pollfd pollfds[] = {
{
.fd = m_pConnection->getEventLoopPollData().fd,
.events = POLLIN,
},
{
.fd = wl_display_get_fd(m_sWaylandConnection.display),
.events = POLLIN,
},
{
.fd = pw_loop_get_fd(m_sPipewire.loop),
.events = POLLIN,
},
};

std::thread pollThr([this, &pollfds]() {
std::thread pollThr([this]() {
while (1) {
int ret = poll(pollfds, 3, 5000 /* 5 seconds, reasonable. It's because we might need to terminate */);

int ret = poll(m_sEventLoopInternals.pollFds.data(), m_sEventLoopInternals.pollFds.size(), 5000 /* 5 seconds, reasonable. It's because we might need to terminate */);
if (ret < 0) {
Debug::log(CRIT, "[core] Polling fds failed with {}", strerror(errno));
g_pPortalManager->terminate();
}

for (size_t i = 0; i < 3; ++i) {
if (pollfds[i].revents & POLLHUP) {
if (m_sEventLoopInternals.pollFds.data()->revents & POLLHUP) {
Debug::log(CRIT, "[core] Disconnected from pollfd id {}", i);
g_pPortalManager->terminate();
}
Expand Down Expand Up @@ -367,13 +366,13 @@ void CPortalManager::startEventLoop() {

m_mEventLock.lock();

if (pollfds[0].revents & POLLIN /* dbus */) {
if (m_sEventLoopInternals.pollFds[0].revents & POLLIN /* dbus */) {
while (m_pConnection->processPendingEvent()) {
;
}
}

if (pollfds[1].revents & POLLIN /* wl */) {
if (m_sEventLoopInternals.pollFds[1].revents & POLLIN /* wl */) {
wl_display_flush(m_sWaylandConnection.display);
if (wl_display_prepare_read(m_sWaylandConnection.display) == 0) {
wl_display_read_events(m_sWaylandConnection.display);
Expand All @@ -383,12 +382,18 @@ void CPortalManager::startEventLoop() {
}
}

if (pollfds[2].revents & POLLIN /* pw */) {
if (m_sEventLoopInternals.pollFds[2].revents & POLLIN /* pw */) {
while (pw_loop_iterate(m_sPipewire.loop, 0) != 0) {
;
}
}

for (pollfd p : m_sEventLoopInternals.pollFds) {
if (p.revents & POLLIN && m_sEventLoopInternals.pollCallbacks.contains(p.fd)) {
m_sEventLoopInternals.pollCallbacks[p.fd]();
}
}

std::vector<CTimer*> toRemove;
for (auto& t : m_sTimersThread.timers) {
if (t->passed()) {
Expand Down Expand Up @@ -417,6 +422,7 @@ void CPortalManager::startEventLoop() {
m_sPortals.screencopy.reset();
m_sPortals.screenshot.reset();
m_sHelpers.toplevel.reset();
m_sPortals.inputCapture.reset();

m_pConnection.reset();
pw_loop_destroy(m_sPipewire.loop);
Expand All @@ -438,6 +444,10 @@ SOutput* CPortalManager::getOutputFromName(const std::string& name) {
return nullptr;
}

std::vector<std::unique_ptr<SOutput>> const& CPortalManager::getAllOutputs() {
return m_vOutputs;
}

static char* gbm_find_render_node(drmDevice* device) {
drmDevice* devices[64];
char* render_node = NULL;
Expand Down Expand Up @@ -487,6 +497,20 @@ void CPortalManager::addTimer(const CTimer& timer) {
m_sTimersThread.loopSignal.notify_all();
}

void CPortalManager::addFdToEventLoop(int fd, short events, std::function<void()> callback) {
m_sEventLoopInternals.pollFds.emplace_back(pollfd{.fd = fd, .events = POLLIN});

if (callback == nullptr)
return;

m_sEventLoopInternals.pollCallbacks[fd] = callback;
}

void CPortalManager::removeFdFromEventLoop(int fd) {
std::erase_if(m_sEventLoopInternals.pollFds, [fd](const pollfd& p) { return p.fd == fd; });
m_sEventLoopInternals.pollCallbacks.erase(fd);
}

void CPortalManager::terminate() {
m_bTerminate = true;

Expand Down
32 changes: 23 additions & 9 deletions src/core/PortalManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#include "../portals/Screencopy.hpp"
#include "../portals/Screenshot.hpp"
#include "../portals/GlobalShortcuts.hpp"
#include "../portals/InputCapture.hpp"
#include "../helpers/Timer.hpp"
#include "../shared/ToplevelManager.hpp"
#include <gbm.h>
#include <poll.h>
#include <xf86drm.h>

#include "hyprland-toplevel-export-v1.hpp"
Expand All @@ -33,6 +35,11 @@ struct SOutput {
uint32_t id = 0;
float refreshRate = 60.0;
wl_output_transform transform = WL_OUTPUT_TRANSFORM_NORMAL;
uint32_t width = 0;
uint32_t height = 0;
int32_t x = 0;
int32_t y = 0;
int32_t scale = 1;
};

struct SDMABUFModifier {
Expand All @@ -44,13 +51,14 @@ class CPortalManager {
public:
CPortalManager();

void init();
void init();

void onGlobal(uint32_t name, const char* interface, uint32_t version);
void onGlobalRemoved(uint32_t name);
void onGlobal(uint32_t name, const char* interface, uint32_t version);
void onGlobalRemoved(uint32_t name);

sdbus::IConnection* getConnection();
SOutput* getOutputFromName(const std::string& name);
sdbus::IConnection* getConnection();
SOutput* getOutputFromName(const std::string& name);
std::vector<std::unique_ptr<SOutput>> const& getAllOutputs();

struct {
pw_loop* loop = nullptr;
Expand All @@ -60,6 +68,7 @@ class CPortalManager {
std::unique_ptr<CScreencopyPortal> screencopy;
std::unique_ptr<CScreenshotPortal> screenshot;
std::unique_ptr<CGlobalShortcutsPortal> globalShortcuts;
std::unique_ptr<CInputCapturePortal> inputCapture;
} m_sPortals;

struct {
Expand Down Expand Up @@ -92,6 +101,9 @@ class CPortalManager {

gbm_device* createGBMDevice(drmDevice* dev);

void addFdToEventLoop(int fd, short events, std::function<void()> callback);
void removeFdFromEventLoop(int fd);

// terminate after the event loop has been created. Before we can exit()
void terminate();

Expand All @@ -102,10 +114,12 @@ class CPortalManager {
pid_t m_iPID = 0;

struct {
std::condition_variable loopSignal;
std::mutex loopMutex;
std::atomic<bool> shouldProcess = false;
std::mutex loopRequestMutex;
std::condition_variable loopSignal;
std::mutex loopMutex;
std::atomic<bool> shouldProcess = false;
std::mutex loopRequestMutex;
std::vector<pollfd> pollFds;
std::map<int, std::function<void()>> pollCallbacks;
} m_sEventLoopInternals;

struct {
Expand Down
85 changes: 85 additions & 0 deletions src/helpers/MiscFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include <vector>
#include <string>
#include <algorithm>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <uuid/uuid.h>

#include <hyprutils/os/Process.hpp>
using namespace Hyprutils::OS;
Expand Down Expand Up @@ -55,3 +59,84 @@ bool inShellPath(const std::string& exec) {

return std::ranges::any_of(paths, [&exec](std::string& path) { return access((path + "/" + exec).c_str(), X_OK) == 0; });
}

std::string getRandomUUID() {
std::string uuid;
uuid_t uuid_;
uuid_generate_random(uuid_);
return std::format("{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}", (uint16_t)uuid_[0], (uint16_t)uuid_[1],
(uint16_t)uuid_[2], (uint16_t)uuid_[3], (uint16_t)uuid_[4], (uint16_t)uuid_[5], (uint16_t)uuid_[6], (uint16_t)uuid_[7], (uint16_t)uuid_[8],
(uint16_t)uuid_[9], (uint16_t)uuid_[10], (uint16_t)uuid_[11], (uint16_t)uuid_[12], (uint16_t)uuid_[13], (uint16_t)uuid_[14], (uint16_t)uuid_[15]);
}

std::pair<int, std::string> openExclusiveShm() {
// Only absolute paths can be shared across different shm_open() calls
std::string name = "/" + getRandomUUID();

for (size_t i = 0; i < 69; ++i) {
int fd = shm_open(name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd >= 0)
return {fd, name};
}

return {-1, ""};
}

int allocateSHMFile(size_t len) {
auto [fd, name] = openExclusiveShm();
if (fd < 0)
return -1;

shm_unlink(name.c_str());

int ret;
do {
ret = ftruncate(fd, len);
} while (ret < 0 && errno == EINTR);

if (ret < 0) {
close(fd);
return -1;
}

return fd;
}

bool allocateSHMFilePair(size_t size, int* rw_fd_ptr, int* ro_fd_ptr) {
auto [fd, name] = openExclusiveShm();
if (fd < 0) {
return false;
}

// CLOEXEC is guaranteed to be set by shm_open
int ro_fd = shm_open(name.c_str(), O_RDONLY, 0);
if (ro_fd < 0) {
shm_unlink(name.c_str());
close(fd);
return false;
}

shm_unlink(name.c_str());

// Make sure the file cannot be re-opened in read-write mode (e.g. via
// "/proc/self/fd/" on Linux)
if (fchmod(fd, 0) != 0) {
close(fd);
close(ro_fd);
return false;
}

int ret;
do {
ret = ftruncate(fd, size);
} while (ret < 0 && errno == EINTR);
if (ret < 0) {
close(fd);
close(ro_fd);
return false;
}

*rw_fd_ptr = fd;
*ro_fd_ptr = ro_fd;
return true;
}
Loading