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 10 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
7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ pkg_check_modules(
libpipewire-0.3>=1.1.82
libspa-0.2
libdrm
libeis-1.0
gbm
hyprlang>=0.2.0
hyprutils
hyprwayland-scanner>=0.4.2)
hyprwayland-scanner>=0.4.2
uuid)

# check whether we can find sdbus-c++ through pkg-config
pkg_check_modules(SDBUS IMPORTED_TARGET sdbus-c++>=2.0.0)
Expand All @@ -88,7 +90,6 @@ add_executable(xdg-desktop-portal-hyprland ${SRCFILES})
target_link_libraries(
xdg-desktop-portal-hyprland PRIVATE rt PkgConfig::SDBUS Threads::Threads
PkgConfig::deps)

# protocols
pkg_get_variable(WAYLAND_PROTOCOLS_DIR wayland-protocols pkgdatadir)
message(STATUS "Found wayland-protocols at ${WAYLAND_PROTOCOLS_DIR}")
Expand Down Expand Up @@ -130,6 +131,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("unstable/linux-dmabuf" "linux-dmabuf-unstable-v1" false)

# Installation
Expand Down
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 / 'unstable/linux-dmabuf/linux-dmabuf-unstable-v1.xml',
]

Expand Down
77 changes: 49 additions & 28 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,13 +18,19 @@ 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) { g_pPortalManager->m_sPortals.inputCapture->zonesChanged(); });
}

CPortalManager::CPortalManager() {
Expand Down Expand Up @@ -62,7 +67,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 @@ -268,32 +275,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 @@ -366,13 +362,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 @@ -382,12 +378,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 @@ -416,6 +418,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 @@ -437,6 +440,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 @@ -486,6 +493,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
34 changes: 24 additions & 10 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 All @@ -122,4 +136,4 @@ class CPortalManager {
std::mutex m_mEventLock;
};

inline std::unique_ptr<CPortalManager> g_pPortalManager;
inline std::unique_ptr<CPortalManager> g_pPortalManager;
Loading