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

begin using CFileDescriptor from hyprutils #9122

Merged
merged 14 commits into from
Jan 30, 2025
Merged
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
25 changes: 11 additions & 14 deletions src/config/ConfigWatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,24 @@
#include <fcntl.h>
#include <unistd.h>

using namespace Hyprutils::OS;

CConfigWatcher::CConfigWatcher() : m_inotifyFd(inotify_init()) {
if (m_inotifyFd < 0) {
if (!m_inotifyFd.isValid()) {
Debug::log(ERR, "CConfigWatcher couldn't open an inotify node. Config will not be automatically reloaded");
return;
}

const int FLAGS = fcntl(m_inotifyFd, F_GETFL, 0);
if (fcntl(m_inotifyFd, F_SETFL, FLAGS | O_NONBLOCK) < 0) {
// TODO: make CFileDescriptor take F_GETFL, F_SETFL
const int FLAGS = fcntl(m_inotifyFd.get(), F_GETFL, 0);
if (fcntl(m_inotifyFd.get(), F_SETFL, FLAGS | O_NONBLOCK) < 0) {
Debug::log(ERR, "CConfigWatcher couldn't non-block inotify node. Config will not be automatically reloaded");
close(m_inotifyFd);
m_inotifyFd = -1;
m_inotifyFd.reset();
return;
}
}

CConfigWatcher::~CConfigWatcher() {
if (m_inotifyFd >= 0)
close(m_inotifyFd);
}

int CConfigWatcher::getInotifyFD() {
CFileDescriptor& CConfigWatcher::getInotifyFD() {
return m_inotifyFd;
}

Expand All @@ -38,15 +35,15 @@ void CConfigWatcher::setWatchList(const std::vector<std::string>& paths) {

// cleanup old paths
for (auto& watch : m_watches) {
inotify_rm_watch(m_inotifyFd, watch.wd);
inotify_rm_watch(m_inotifyFd.get(), watch.wd);
}

m_watches.clear();

// add new paths
for (const auto& path : paths) {
m_watches.emplace_back(SInotifyWatch{
.wd = inotify_add_watch(m_inotifyFd, path.c_str(), IN_MODIFY),
.wd = inotify_add_watch(m_inotifyFd.get(), path.c_str(), IN_MODIFY),
.file = path,
});
}
Expand All @@ -58,7 +55,7 @@ void CConfigWatcher::setOnChange(const std::function<void(const SConfigWatchEven

void CConfigWatcher::onInotifyEvent() {
inotify_event ev;
while (read(m_inotifyFd, &ev, sizeof(ev)) > 0) {
while (read(m_inotifyFd.get(), &ev, sizeof(ev)) > 0) {
const auto WD = std::ranges::find_if(m_watches.begin(), m_watches.end(), [wd = ev.wd](const auto& e) { return e.wd == wd; });

if (WD == m_watches.end()) {
Expand Down
15 changes: 8 additions & 7 deletions src/config/ConfigWatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
#include <vector>
#include <string>
#include <functional>
#include <hyprutils/os/FileDescriptor.hpp>

class CConfigWatcher {
public:
CConfigWatcher();
~CConfigWatcher();
~CConfigWatcher() = default;

struct SConfigWatchEvent {
std::string file;
};

int getInotifyFD();
void setWatchList(const std::vector<std::string>& paths);
void setOnChange(const std::function<void(const SConfigWatchEvent&)>& fn);
void onInotifyEvent();
Hyprutils::OS::CFileDescriptor& getInotifyFD();
void setWatchList(const std::vector<std::string>& paths);
void setOnChange(const std::function<void(const SConfigWatchEvent&)>& fn);
void onInotifyEvent();

private:
struct SInotifyWatch {
Expand All @@ -26,7 +27,7 @@ class CConfigWatcher {

std::function<void(const SConfigWatchEvent&)> m_watchCallback;
std::vector<SInotifyWatch> m_watches;
int m_inotifyFd = -1;
Hyprutils::OS::CFileDescriptor m_inotifyFd;
};

inline UP<CConfigWatcher> g_pConfigWatcher = makeUnique<CConfigWatcher>();
inline UP<CConfigWatcher> g_pConfigWatcher = makeUnique<CConfigWatcher>();
18 changes: 10 additions & 8 deletions src/debug/HyprCtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <hyprutils/string/String.hpp>
using namespace Hyprutils::String;
using namespace Hyprutils::OS;
#include <aquamarine/input/Input.hpp>

#include "../config/ConfigDataValues.hpp"
Expand Down Expand Up @@ -1680,8 +1681,6 @@ CHyprCtl::CHyprCtl() {
CHyprCtl::~CHyprCtl() {
if (m_eventSource)
wl_event_source_remove(m_eventSource);
if (m_iSocketFD >= 0)
close(m_iSocketFD);
if (!m_socketPath.empty())
unlink(m_socketPath.c_str());
}
Expand Down Expand Up @@ -1840,10 +1839,13 @@ static int hyprCtlFDTick(int fd, uint32_t mask, void* data) {
if (mask & WL_EVENT_ERROR || mask & WL_EVENT_HANGUP)
return 0;

if (!g_pHyprCtl->m_iSocketFD.isValid())
return 0;

sockaddr_in clientAddress;
socklen_t clientSize = sizeof(clientAddress);

const auto ACCEPTEDCONNECTION = accept4(g_pHyprCtl->m_iSocketFD, (sockaddr*)&clientAddress, &clientSize, SOCK_CLOEXEC);
const auto ACCEPTEDCONNECTION = accept4(g_pHyprCtl->m_iSocketFD.get(), (sockaddr*)&clientAddress, &clientSize, SOCK_CLOEXEC);

std::array<char, 1024> readBuffer;

Expand Down Expand Up @@ -1900,9 +1902,9 @@ static int hyprCtlFDTick(int fd, uint32_t mask, void* data) {
}

void CHyprCtl::startHyprCtlSocket() {
m_iSocketFD = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
m_iSocketFD = CFileDescriptor{socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)};

if (m_iSocketFD < 0) {
if (!m_iSocketFD.isValid()) {
Debug::log(ERR, "Couldn't start the Hyprland Socket. (1) IPC will not work.");
return;
}
Expand All @@ -1913,15 +1915,15 @@ void CHyprCtl::startHyprCtlSocket() {

strcpy(SERVERADDRESS.sun_path, m_socketPath.c_str());

if (bind(m_iSocketFD, (sockaddr*)&SERVERADDRESS, SUN_LEN(&SERVERADDRESS)) < 0) {
if (bind(m_iSocketFD.get(), (sockaddr*)&SERVERADDRESS, SUN_LEN(&SERVERADDRESS)) < 0) {
Debug::log(ERR, "Couldn't start the Hyprland Socket. (2) IPC will not work.");
return;
}

// 10 max queued.
listen(m_iSocketFD, 10);
listen(m_iSocketFD.get(), 10);

Debug::log(LOG, "Hypr socket started at {}", m_socketPath);

m_eventSource = wl_event_loop_add_fd(g_pCompositor->m_sWLEventLoop, m_iSocketFD, WL_EVENT_READABLE, hyprCtlFDTick, nullptr);
m_eventSource = wl_event_loop_add_fd(g_pCompositor->m_sWLEventLoop, m_iSocketFD.get(), WL_EVENT_READABLE, hyprCtlFDTick, nullptr);
}
11 changes: 6 additions & 5 deletions src/debug/HyprCtl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "../helpers/MiscFunctions.hpp"
#include "../desktop/Window.hpp"
#include <functional>
#include <hyprutils/os/FileDescriptor.hpp>

// exposed for main.cpp
std::string systemInfoRequest(eHyprCtlOutputFormat format, std::string request);
Expand All @@ -14,12 +15,12 @@ class CHyprCtl {
CHyprCtl();
~CHyprCtl();

std::string makeDynamicCall(const std::string& input);
SP<SHyprCtlCommand> registerCommand(SHyprCtlCommand cmd);
void unregisterCommand(const SP<SHyprCtlCommand>& cmd);
std::string getReply(std::string);
std::string makeDynamicCall(const std::string& input);
SP<SHyprCtlCommand> registerCommand(SHyprCtlCommand cmd);
void unregisterCommand(const SP<SHyprCtlCommand>& cmd);
std::string getReply(std::string);

int m_iSocketFD = -1;
Hyprutils::OS::CFileDescriptor m_iSocketFD;

struct {
bool all = false;
Expand Down
26 changes: 12 additions & 14 deletions src/devices/IKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <aquamarine/input/Input.hpp>
#include <cstring>

using namespace Hyprutils::OS;

#define LED_COUNT 3

constexpr static std::array<const char*, 8> MODNAMES = {
Expand Down Expand Up @@ -41,17 +43,14 @@ void IKeyboard::clearManuallyAllocd() {
if (xkbKeymap)
xkb_keymap_unref(xkbKeymap);

if (xkbKeymapFD >= 0)
close(xkbKeymapFD);

if (xkbSymState)
xkb_state_unref(xkbSymState);

xkbSymState = nullptr;
xkbKeymap = nullptr;
xkbState = nullptr;
xkbStaticState = nullptr;
xkbKeymapFD = -1;
xkbKeymapFD.reset();
}

void IKeyboard::setKeymap(const SStringRuleNames& rules) {
Expand Down Expand Up @@ -147,31 +146,30 @@ void IKeyboard::setKeymap(const SStringRuleNames& rules) {
void IKeyboard::updateKeymapFD() {
Debug::log(LOG, "Updating keymap fd for keyboard {}", deviceName);

if (xkbKeymapFD >= 0)
close(xkbKeymapFD);
xkbKeymapFD = -1;
if (xkbKeymapFD.isValid())
xkbKeymapFD.reset();

auto cKeymapStr = xkb_keymap_get_as_string(xkbKeymap, XKB_KEYMAP_FORMAT_TEXT_V1);
xkbKeymapString = cKeymapStr;
free(cKeymapStr);

int rw, ro;
if (!allocateSHMFilePair(xkbKeymapString.length() + 1, &rw, &ro))
CFileDescriptor rw, ro;
if (!allocateSHMFilePair(xkbKeymapString.length() + 1, rw, ro))
Debug::log(ERR, "IKeyboard: failed to allocate shm pair for the keymap");
else {
auto keymapFDDest = mmap(nullptr, xkbKeymapString.length() + 1, PROT_READ | PROT_WRITE, MAP_SHARED, rw, 0);
close(rw);
auto keymapFDDest = mmap(nullptr, xkbKeymapString.length() + 1, PROT_READ | PROT_WRITE, MAP_SHARED, rw.get(), 0);
rw.reset();
if (keymapFDDest == MAP_FAILED) {
Debug::log(ERR, "IKeyboard: failed to mmap a shm pair for the keymap");
close(ro);
ro.reset();
} else {
memcpy(keymapFDDest, xkbKeymapString.c_str(), xkbKeymapString.length());
munmap(keymapFDDest, xkbKeymapString.length() + 1);
xkbKeymapFD = ro;
xkbKeymapFD = std::move(ro);
}
}

Debug::log(LOG, "Updated keymap fd to {}", xkbKeymapFD);
Debug::log(LOG, "Updated keymap fd to {}", xkbKeymapFD.get());
}

void IKeyboard::updateXKBTranslationState(xkb_keymap* const keymap) {
Expand Down
3 changes: 2 additions & 1 deletion src/devices/IKeyboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <optional>
#include <xkbcommon/xkbcommon.h>
#include <hyprutils/os/FileDescriptor.hpp>

AQUAMARINE_FORWARD(IKeyboard);

Expand Down Expand Up @@ -96,7 +97,7 @@ class IKeyboard : public IHID {

std::string xkbFilePath = "";
std::string xkbKeymapString = "";
int xkbKeymapFD = -1;
Hyprutils::OS::CFileDescriptor xkbKeymapFD;

SStringRuleNames currentRules;
int repeatRate = 0;
Expand Down
44 changes: 19 additions & 25 deletions src/helpers/MiscFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,75 +836,69 @@ bool envEnabled(const std::string& env) {
return std::string(ENV) == "1";
}

std::pair<int, std::string> openExclusiveShm() {
std::pair<CFileDescriptor, std::string> openExclusiveShm() {
// Only absolute paths can be shared across different shm_open() calls
std::string name = "/" + g_pTokenManager->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};
CFileDescriptor fd{shm_open(name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600)};
if (fd.isValid())
return {std::move(fd), name};
}

return {-1, ""};
return {{}, ""};
}

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

shm_unlink(name.c_str());

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

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

return fd;
return std::move(fd);
}

bool allocateSHMFilePair(size_t size, int* rw_fd_ptr, int* ro_fd_ptr) {
bool allocateSHMFilePair(size_t size, CFileDescriptor& rw_fd_ptr, CFileDescriptor& ro_fd_ptr) {
auto [fd, name] = openExclusiveShm();
if (fd < 0) {
if (!fd.isValid()) {
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) {
CFileDescriptor ro_fd{shm_open(name.c_str(), O_RDONLY, 0)};
if (!ro_fd.isValid()) {
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);
if (fchmod(fd.get(), 0) != 0) {
return false;
}

int ret;
do {
ret = ftruncate(fd, size);
ret = ftruncate(fd.get(), 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;
rw_fd_ptr = std::move(fd);
ro_fd_ptr = std::move(ro_fd);
return true;
}

Expand Down
5 changes: 3 additions & 2 deletions src/helpers/MiscFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vector>
#include <format>
#include <expected>
#include <hyprutils/os/FileDescriptor.hpp>
#include "../SharedDefs.hpp"
#include "../macros.hpp"

Expand Down Expand Up @@ -35,8 +36,8 @@ double normalizeAngleRad(double ang);
std::vector<SCallstackFrameInfo> getBacktrace();
void throwError(const std::string& err);
bool envEnabled(const std::string& env);
int allocateSHMFile(size_t len);
bool allocateSHMFilePair(size_t size, int* rw_fd_ptr, int* ro_fd_ptr);
Hyprutils::OS::CFileDescriptor allocateSHMFile(size_t len);
bool allocateSHMFilePair(size_t size, Hyprutils::OS::CFileDescriptor& rw_fd_ptr, Hyprutils::OS::CFileDescriptor& ro_fd_ptr);
float stringToPercentage(const std::string& VALUE, const float REL);

template <typename... Args>
Expand Down
Loading
Loading