Skip to content

Commit

Permalink
config: added option to choose the default monitor for the cursor (#5847
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
ikalco authored May 3, 2024
1 parent 41cf94f commit 387127b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/config/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down
55 changes: 41 additions & 14 deletions src/events/Monitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CMonitor>* PNEWMONITORWRAP, std::string monitorName) {
const auto PNEWMONITOR = PNEWMONITORWRAP->get();

static auto PCURSORMONITOR = CConfigValue<std::string>("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::seconds>(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;
Expand Down Expand Up @@ -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();
}
}
}
Expand Down

0 comments on commit 387127b

Please sign in to comment.