From 387127b12ab5df8f8f40e8da7d76f47636562cab Mon Sep 17 00:00:00 2001 From: Ikalco <73481042+ikalco@users.noreply.github.com> Date: Thu, 2 May 2024 20:39:19 -0500 Subject: [PATCH] config: added option to choose the default monitor for the cursor (#5847) * added option to choose the default monitor that the cursor will appear in upon startup * fix: don't set cursor to default monitor after startup * refactor to checkDefaultCursorWarp also fix focus --- src/config/ConfigManager.cpp | 1 + src/events/Monitors.cpp | 55 +++++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index c12badf9..a2db6040 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -318,6 +318,7 @@ CConfigManager::CConfigManager() { m_pConfig->addConfigValue("general:layout", {"dwindle"}); m_pConfig->addConfigValue("general:allow_tearing", Hyprlang::INT{0}); m_pConfig->addConfigValue("general:resize_corner", Hyprlang::INT{0}); + m_pConfig->addConfigValue("general:default_cursor_monitor", {STRVAL_EMPTY}); m_pConfig->addConfigValue("misc:disable_hyprland_logo", Hyprlang::INT{0}); m_pConfig->addConfigValue("misc:disable_splash_rendering", Hyprlang::INT{0}); diff --git a/src/events/Monitors.cpp b/src/events/Monitors.cpp index d91ec4ba..11b7a25c 100644 --- a/src/events/Monitors.cpp +++ b/src/events/Monitors.cpp @@ -60,13 +60,45 @@ void Events::listener_change(wl_listener* listener, void* data) { wlr_output_manager_v1_set_configuration(g_pCompositor->m_sWLROutputMgr, CONFIG); } +static void checkDefaultCursorWarp(std::shared_ptr* PNEWMONITORWRAP, std::string monitorName) { + const auto PNEWMONITOR = PNEWMONITORWRAP->get(); + + static auto PCURSORMONITOR = CConfigValue("general:default_cursor_monitor"); + static auto firstMonitorAdded = std::chrono::system_clock::now(); + static bool cursorDefaultDone = false; + static bool firstLaunch = true; + + const auto POS = PNEWMONITOR->middle(); + + // by default, cursor should be set to first monitor detected + // this is needed as a default if the monitor given in config above doesn't exist + if (firstLaunch) { + firstLaunch = false; + g_pCompositor->warpCursorTo(POS, true); + g_pInputManager->refocus(); + } + + if (cursorDefaultDone || *PCURSORMONITOR == STRVAL_EMPTY) + return; + + // after 10s, don't set cursor to default monitor + auto timePassedSec = std::chrono::duration_cast(std::chrono::system_clock::now() - firstMonitorAdded); + if (timePassedSec.count() > 10) { + cursorDefaultDone = true; + return; + } + + if (*PCURSORMONITOR == monitorName) { + cursorDefaultDone = true; + g_pCompositor->warpCursorTo(POS, true); + g_pInputManager->refocus(); + } +} + void Events::listener_newOutput(wl_listener* listener, void* data) { // new monitor added, let's accommodate for that. const auto OUTPUT = (wlr_output*)data; - // for warping the cursor on launch - static bool firstLaunch = true; - if (!OUTPUT->name) { Debug::log(ERR, "New monitor has no name?? Ignoring"); return; @@ -101,17 +133,12 @@ void Events::listener_newOutput(wl_listener* listener, void* data) { g_pConfigManager->m_bWantsMonitorReload = true; g_pCompositor->scheduleFrameForMonitor(PNEWMONITOR); - if (firstLaunch) { - firstLaunch = false; - const auto POS = PNEWMONITOR->middle(); - if (g_pCompositor->m_sSeat.mouse) - wlr_cursor_warp(g_pCompositor->m_sWLRCursor, g_pCompositor->m_sSeat.mouse->mouse, POS.x, POS.y); - } else { - for (auto& w : g_pCompositor->m_vWindows) { - if (w->m_iMonitorID == PNEWMONITOR->ID) { - w->m_iLastSurfaceMonitorID = -1; - w->updateSurfaceScaleTransformDetails(); - } + checkDefaultCursorWarp(PNEWMONITORWRAP, OUTPUT->name); + + for (auto& w : g_pCompositor->m_vWindows) { + if (w->m_iMonitorID == PNEWMONITOR->ID) { + w->m_iLastSurfaceMonitorID = -1; + w->updateSurfaceScaleTransformDetails(); } } }