Hyprland/src/protocols/VirtualKeyboard.cpp

178 lines
6.1 KiB
C++
Raw Normal View History

2024-05-03 00:31:48 +01:00
#include "VirtualKeyboard.hpp"
#include <filesystem>
2024-05-03 00:31:48 +01:00
#include <sys/mman.h>
#include "../config/ConfigValue.hpp"
#include "../config/ConfigManager.hpp"
#include "../devices/IKeyboard.hpp"
#include "../helpers/time/Time.hpp"
#include "../helpers/MiscFunctions.hpp"
core: begin using CFileDescriptor from hyprutils (#9122) * config: make fd use CFileDescriptor make use of the new hyprutils CFileDescriptor instead of manual FD handling. * hyprctl: make fd use CFileDescriptor make use of the new hyprutils CFileDescriptor instead of manual FD handling. * ikeyboard: make fd use CFileDescriptor make use of the new CFileDescriptor instead of manual FD handling, also in sendKeymap remove dead code, it already early returns if keyboard isnt valid, and dont try to close the FD that ikeyboard owns. * core: make SHMFile functions use CFileDescriptor make SHMFile misc functions use CFileDescriptor and its associated usage in dmabuf and keyboard. * core: make explicit sync use CFileDescriptor begin using CFileDescriptor in explicit sync and its timelines and eglsync usage in opengl, there is still a bit left with manual handling that requires future aquamarine change aswell. * eventmgr: make fd and sockets use CFileDescriptor make use of the hyprutils CFileDescriptor instead of manual FD and socket handling and closing. * eventloopmgr: make timerfd use CFileDescriptor make the timerfd use CFileDescriptor instead of manual fd handling * opengl: make gbm fd use CFileDescriptor make the gbm rendernode fd use CFileDescriptor instead of manual fd handling * core: make selection source/offer use CFileDescriptor make data selection source and offers use CFileDescriptor and its associated use in xwm and protocols * protocols: convert protocols fd to CFileDescriptor make most fd handling use CFileDescriptor in protocols * shm: make SHMPool use CfileDescriptor make SHMPool use CFileDescriptor instead of manual fd handling. * opengl: duplicate fd with CFileDescriptor duplicate fenceFD with CFileDescriptor duplicate instead. * xwayland: make sockets and fds use CFileDescriptor instead of manual opening/closing make sockets and fds use CFileDescriptor * keybindmgr: make sockets and fds use CFileDescriptor make sockets and fds use CFileDescriptor instead of manual handling.
2025-01-30 12:30:12 +01:00
using namespace Hyprutils::OS;
2024-05-03 00:31:48 +01:00
static std::string virtualKeyboardNameForWlClient(wl_client* client) {
std::string name = "hl-virtual-keyboard";
static auto PVKNAMEPROC = CConfigValue<Hyprlang::INT>("misc:name_vk_after_proc");
if (!*PVKNAMEPROC)
return name;
name += "-";
const auto CLIENTNAME = binaryNameForWlClient(client);
if (CLIENTNAME.has_value()) {
const auto PATH = std::filesystem::path(CLIENTNAME.value());
if (PATH.has_filename()) {
const auto FILENAME = PATH.filename();
const auto NAME = deviceNameToInternalString(FILENAME);
name += NAME;
return name;
}
}
name += "unknown";
return name;
}
CVirtualKeyboardV1Resource::CVirtualKeyboardV1Resource(SP<CZwpVirtualKeyboardV1> resource_) : m_resource(resource_) {
if UNLIKELY (!good())
2024-05-03 00:31:48 +01:00
return;
m_resource->setDestroy([this](CZwpVirtualKeyboardV1* r) { destroy(); });
m_resource->setOnDestroy([this](CZwpVirtualKeyboardV1* r) { destroy(); });
2024-05-03 00:31:48 +01:00
m_resource->setKey([this](CZwpVirtualKeyboardV1* r, uint32_t timeMs, uint32_t key, uint32_t state) {
if UNLIKELY (!m_hasKeymap) {
2024-05-03 00:31:48 +01:00
r->error(ZWP_VIRTUAL_KEYBOARD_V1_ERROR_NO_KEYMAP, "Key event received before a keymap was set");
return;
}
m_events.key.emit(IKeyboard::SKeyEvent{
.timeMs = timeMs,
.keycode = key,
.state = (wl_keyboard_key_state)state,
});
const bool CONTAINS = std::ranges::contains(m_pressed, key);
if (state && !CONTAINS)
m_pressed.emplace_back(key);
else if (!state && CONTAINS)
std::erase(m_pressed, key);
2024-05-03 00:31:48 +01:00
});
m_resource->setModifiers([this](CZwpVirtualKeyboardV1* r, uint32_t depressed, uint32_t latched, uint32_t locked, uint32_t group) {
if UNLIKELY (!m_hasKeymap) {
2024-05-03 00:31:48 +01:00
r->error(ZWP_VIRTUAL_KEYBOARD_V1_ERROR_NO_KEYMAP, "Mods event received before a keymap was set");
return;
}
m_events.modifiers.emit(IKeyboard::SModifiersEvent{
.depressed = depressed,
.latched = latched,
.locked = locked,
.group = group,
});
2024-05-03 00:31:48 +01:00
});
m_resource->setKeymap([this](CZwpVirtualKeyboardV1* r, uint32_t fmt, int32_t fd, uint32_t len) {
core: begin using CFileDescriptor from hyprutils (#9122) * config: make fd use CFileDescriptor make use of the new hyprutils CFileDescriptor instead of manual FD handling. * hyprctl: make fd use CFileDescriptor make use of the new hyprutils CFileDescriptor instead of manual FD handling. * ikeyboard: make fd use CFileDescriptor make use of the new CFileDescriptor instead of manual FD handling, also in sendKeymap remove dead code, it already early returns if keyboard isnt valid, and dont try to close the FD that ikeyboard owns. * core: make SHMFile functions use CFileDescriptor make SHMFile misc functions use CFileDescriptor and its associated usage in dmabuf and keyboard. * core: make explicit sync use CFileDescriptor begin using CFileDescriptor in explicit sync and its timelines and eglsync usage in opengl, there is still a bit left with manual handling that requires future aquamarine change aswell. * eventmgr: make fd and sockets use CFileDescriptor make use of the hyprutils CFileDescriptor instead of manual FD and socket handling and closing. * eventloopmgr: make timerfd use CFileDescriptor make the timerfd use CFileDescriptor instead of manual fd handling * opengl: make gbm fd use CFileDescriptor make the gbm rendernode fd use CFileDescriptor instead of manual fd handling * core: make selection source/offer use CFileDescriptor make data selection source and offers use CFileDescriptor and its associated use in xwm and protocols * protocols: convert protocols fd to CFileDescriptor make most fd handling use CFileDescriptor in protocols * shm: make SHMPool use CfileDescriptor make SHMPool use CFileDescriptor instead of manual fd handling. * opengl: duplicate fd with CFileDescriptor duplicate fenceFD with CFileDescriptor duplicate instead. * xwayland: make sockets and fds use CFileDescriptor instead of manual opening/closing make sockets and fds use CFileDescriptor * keybindmgr: make sockets and fds use CFileDescriptor make sockets and fds use CFileDescriptor instead of manual handling.
2025-01-30 12:30:12 +01:00
auto xkbContext = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
CFileDescriptor keymapFd{fd};
if UNLIKELY (!xkbContext) {
2024-05-03 00:31:48 +01:00
LOGM(ERR, "xkbContext creation failed");
r->noMemory();
return;
}
core: begin using CFileDescriptor from hyprutils (#9122) * config: make fd use CFileDescriptor make use of the new hyprutils CFileDescriptor instead of manual FD handling. * hyprctl: make fd use CFileDescriptor make use of the new hyprutils CFileDescriptor instead of manual FD handling. * ikeyboard: make fd use CFileDescriptor make use of the new CFileDescriptor instead of manual FD handling, also in sendKeymap remove dead code, it already early returns if keyboard isnt valid, and dont try to close the FD that ikeyboard owns. * core: make SHMFile functions use CFileDescriptor make SHMFile misc functions use CFileDescriptor and its associated usage in dmabuf and keyboard. * core: make explicit sync use CFileDescriptor begin using CFileDescriptor in explicit sync and its timelines and eglsync usage in opengl, there is still a bit left with manual handling that requires future aquamarine change aswell. * eventmgr: make fd and sockets use CFileDescriptor make use of the hyprutils CFileDescriptor instead of manual FD and socket handling and closing. * eventloopmgr: make timerfd use CFileDescriptor make the timerfd use CFileDescriptor instead of manual fd handling * opengl: make gbm fd use CFileDescriptor make the gbm rendernode fd use CFileDescriptor instead of manual fd handling * core: make selection source/offer use CFileDescriptor make data selection source and offers use CFileDescriptor and its associated use in xwm and protocols * protocols: convert protocols fd to CFileDescriptor make most fd handling use CFileDescriptor in protocols * shm: make SHMPool use CfileDescriptor make SHMPool use CFileDescriptor instead of manual fd handling. * opengl: duplicate fd with CFileDescriptor duplicate fenceFD with CFileDescriptor duplicate instead. * xwayland: make sockets and fds use CFileDescriptor instead of manual opening/closing make sockets and fds use CFileDescriptor * keybindmgr: make sockets and fds use CFileDescriptor make sockets and fds use CFileDescriptor instead of manual handling.
2025-01-30 12:30:12 +01:00
auto keymapData = mmap(nullptr, len, PROT_READ, MAP_PRIVATE, keymapFd.get(), 0);
if UNLIKELY (keymapData == MAP_FAILED) {
2024-05-03 00:31:48 +01:00
LOGM(ERR, "keymapData alloc failed");
xkb_context_unref(xkbContext);
r->noMemory();
return;
}
auto xkbKeymap = xkb_keymap_new_from_string(xkbContext, (const char*)keymapData, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
munmap(keymapData, len);
if UNLIKELY (!xkbKeymap) {
2024-05-03 00:31:48 +01:00
LOGM(ERR, "xkbKeymap creation failed");
xkb_context_unref(xkbContext);
r->noMemory();
return;
}
m_events.keymap.emit(IKeyboard::SKeymapEvent{
.keymap = xkbKeymap,
});
m_hasKeymap = true;
2024-05-03 00:31:48 +01:00
xkb_keymap_unref(xkbKeymap);
xkb_context_unref(xkbContext);
});
m_name = virtualKeyboardNameForWlClient(resource_->client());
2024-05-03 00:31:48 +01:00
}
CVirtualKeyboardV1Resource::~CVirtualKeyboardV1Resource() {
m_events.destroy.emit();
2024-05-03 00:31:48 +01:00
}
bool CVirtualKeyboardV1Resource::good() {
return m_resource->resource();
2024-05-03 00:31:48 +01:00
}
wl_client* CVirtualKeyboardV1Resource::client() {
return m_resource->resource() ? m_resource->client() : nullptr;
2024-05-03 00:31:48 +01:00
}
void CVirtualKeyboardV1Resource::releasePressed() {
for (auto const& p : m_pressed) {
m_events.key.emit(IKeyboard::SKeyEvent{
.timeMs = Time::millis(Time::steadyNow()),
.keycode = p,
.state = WL_KEYBOARD_KEY_STATE_RELEASED,
});
}
m_pressed.clear();
}
void CVirtualKeyboardV1Resource::destroy() {
const auto RELEASEPRESSED = g_pConfigManager->getDeviceInt(m_name, "release_pressed_on_close", "input:virtualkeyboard:release_pressed_on_close");
if (RELEASEPRESSED)
releasePressed();
m_events.destroy.emit();
PROTO::virtualKeyboard->destroyResource(this);
}
2024-05-03 00:31:48 +01:00
CVirtualKeyboardProtocol::CVirtualKeyboardProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) {
;
}
void CVirtualKeyboardProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) {
const auto RESOURCE = m_managers.emplace_back(makeUnique<CZwpVirtualKeyboardManagerV1>(client, ver, id)).get();
2024-05-03 00:31:48 +01:00
RESOURCE->setOnDestroy([this](CZwpVirtualKeyboardManagerV1* p) { this->onManagerResourceDestroy(p->resource()); });
RESOURCE->setCreateVirtualKeyboard([this](CZwpVirtualKeyboardManagerV1* pMgr, wl_resource* seat, uint32_t id) { this->onCreateKeeb(pMgr, seat, id); });
}
void CVirtualKeyboardProtocol::onManagerResourceDestroy(wl_resource* res) {
std::erase_if(m_managers, [&](const auto& other) { return other->resource() == res; });
2024-05-03 00:31:48 +01:00
}
void CVirtualKeyboardProtocol::destroyResource(CVirtualKeyboardV1Resource* keeb) {
std::erase_if(m_keyboards, [&](const auto& other) { return other.get() == keeb; });
2024-05-03 00:31:48 +01:00
}
void CVirtualKeyboardProtocol::onCreateKeeb(CZwpVirtualKeyboardManagerV1* pMgr, wl_resource* seat, uint32_t id) {
const auto RESOURCE = m_keyboards.emplace_back(makeShared<CVirtualKeyboardV1Resource>(makeShared<CZwpVirtualKeyboardV1>(pMgr->client(), pMgr->version(), id)));
2024-05-03 00:31:48 +01:00
if UNLIKELY (!RESOURCE->good()) {
2024-05-03 00:31:48 +01:00
pMgr->noMemory();
m_keyboards.pop_back();
2024-05-03 00:31:48 +01:00
return;
}
LOGM(LOG, "New VKeyboard at id {}", id);
m_events.newKeyboard.emit(RESOURCE);
}