From 983e67633f613f49e903ff4b79a42c3853c2cacd Mon Sep 17 00:00:00 2001 From: Tom Englund Date: Thu, 21 Nov 2024 04:08:54 +0100 Subject: [PATCH] ikeyboard: move to CFileDescriptor use CFileDescriptor instead of manual fd handling. --- src/devices/IKeyboard.cpp | 14 +++++--------- src/devices/IKeyboard.hpp | 5 ++++- src/protocols/core/Seat.cpp | 2 +- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/devices/IKeyboard.cpp b/src/devices/IKeyboard.cpp index d1119772..e3497de8 100644 --- a/src/devices/IKeyboard.cpp +++ b/src/devices/IKeyboard.cpp @@ -41,13 +41,10 @@ void IKeyboard::clearManuallyAllocd() { if (xkbKeymap) xkb_keymap_unref(xkbKeymap); - if (xkbKeymapFD >= 0) - close(xkbKeymapFD); - + xkbKeymapFD.reset(); xkbKeymap = nullptr; xkbState = nullptr; xkbStaticState = nullptr; - xkbKeymapFD = -1; } void IKeyboard::setKeymap(const SStringRuleNames& rules) { @@ -143,9 +140,8 @@ 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; @@ -163,11 +159,11 @@ void IKeyboard::updateKeymapFD() { } else { memcpy(keymapFDDest, xkbKeymapString.c_str(), xkbKeymapString.length()); munmap(keymapFDDest, xkbKeymapString.length() + 1); - xkbKeymapFD = ro; + xkbKeymapFD = CFileDescriptor(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) { diff --git a/src/devices/IKeyboard.hpp b/src/devices/IKeyboard.hpp index dce2127a..0c822262 100644 --- a/src/devices/IKeyboard.hpp +++ b/src/devices/IKeyboard.hpp @@ -6,6 +6,9 @@ #include #include +#include + +using namespace Hyprutils::OS; AQUAMARINE_FORWARD(IKeyboard); @@ -96,7 +99,7 @@ class IKeyboard : public IHID { std::string xkbFilePath = ""; std::string xkbKeymapString = ""; - int xkbKeymapFD = -1; + CFileDescriptor xkbKeymapFD; SStringRuleNames currentRules; int repeatRate = 0; diff --git a/src/protocols/core/Seat.cpp b/src/protocols/core/Seat.cpp index d95e0e12..585b7dcc 100644 --- a/src/protocols/core/Seat.cpp +++ b/src/protocols/core/Seat.cpp @@ -322,7 +322,7 @@ void CWLKeyboardResource::sendKeymap(SP keyboard) { uint32_t size; if (keyboard) { keymap = keyboard->xkbKeymapString; - fd = keyboard->xkbKeymapFD; + fd = keyboard->xkbKeymapFD.get(); size = keyboard->xkbKeymapString.length() + 1; } else { fd = open("/dev/null", O_RDONLY | O_CLOEXEC);