internal: unify VT getting

This commit is contained in:
vaxerski
2025-08-14 17:13:15 +02:00
parent beee22a95e
commit 60d769a899
4 changed files with 28 additions and 21 deletions

View File

@@ -3213,3 +3213,24 @@ void CCompositor::ensurePersistentWorkspacesPresent(const std::vector<SWorkspace
}
}
}
std::optional<unsigned int> CCompositor::getVTNr() {
if (!m_aqBackend->hasSession())
return std::nullopt;
unsigned int ttynum = 0;
Hyprutils::OS::CFileDescriptor fd{open("/dev/tty", O_RDONLY | O_NOCTTY)};
if (fd.isValid()) {
#if defined(VT_GETSTATE)
struct vt_stat st;
if (!ioctl(fd.get(), VT_GETSTATE, &st))
ttynum = st.v_active;
#elif defined(VT_GETACTIVE)
int vt;
if (!ioctl(fd.get(), VT_GETACTIVE, &vt))
ttynum = vt;
#endif
}
return ttynum;
}

View File

@@ -156,6 +156,7 @@ class CCompositor {
void updateSuspendedStates();
void onNewMonitor(SP<Aquamarine::IOutput> output);
void ensurePersistentWorkspacesPresent(const std::vector<SWorkspaceRule>& rules, PHLWORKSPACE pWorkspace = nullptr);
std::optional<unsigned int> getVTNr();
NColorManagement::SImageDescription getPreferredImageDescription();
bool shouldChangePreferredImageDescription();

View File

@@ -875,25 +875,12 @@ bool CKeybindManager::handleVT(xkb_keysym_t keysym) {
if (g_pCompositor->m_aqBackend->hasSession()) {
const unsigned int TTY = keysym - XKB_KEY_XF86Switch_VT_1 + 1;
// vtnr is bugged for some reason.
unsigned int ttynum = 0;
Hyprutils::OS::CFileDescriptor fd{open("/dev/tty", O_RDONLY | O_NOCTTY)};
if (fd.isValid()) {
#if defined(VT_GETSTATE)
struct vt_stat st;
if (!ioctl(fd.get(), VT_GETSTATE, &st))
ttynum = st.v_active;
#elif defined(VT_GETACTIVE)
int vt;
if (!ioctl(fd.get(), VT_GETACTIVE, &vt))
ttynum = vt;
#endif
}
const auto CURRENT_TTY = g_pCompositor->getVTNr();
if (ttynum == TTY)
if (!CURRENT_TTY.has_value() || *CURRENT_TTY == TTY)
return true;
Debug::log(LOG, "Switching from VT {} to VT {}", ttynum, TTY);
Debug::log(LOG, "Switching from VT {} to VT {}", *CURRENT_TTY, TTY);
g_pCompositor->m_aqBackend->session->switchVT(TTY);
}

View File

@@ -2856,11 +2856,9 @@ void CHyprOpenGLImpl::ensureLockTexturesRendered(bool load) {
m_lockDeadTexture = loadAsset("lockdead.png");
m_lockDead2Texture = loadAsset("lockdead2.png");
m_lockTtyTextTexture = renderText(std::format("Running on tty {}",
g_pCompositor->m_aqBackend->hasSession() && g_pCompositor->m_aqBackend->session->vt > 0 ?
std::to_string(g_pCompositor->m_aqBackend->session->vt) :
"unknown"),
CHyprColor{0.9F, 0.9F, 0.9F, 0.7F}, 20, true);
const auto VT = g_pCompositor->getVTNr();
m_lockTtyTextTexture = renderText(std::format("Running on tty {}", VT.has_value() ? std::to_string(*VT) : "unknown"), CHyprColor{0.9F, 0.9F, 0.9F, 0.7F}, 20, true);
} else {
m_lockDeadTexture.reset();
m_lockDead2Texture.reset();