Hyprland/src/managers/KeybindManager.cpp

683 lines
24 KiB
C++
Raw Normal View History

2022-03-19 17:48:18 +01:00
#include "KeybindManager.hpp"
2022-04-21 15:50:52 +02:00
CKeybindManager::CKeybindManager() {
// initialize all dispatchers
2022-05-18 12:18:58 +02:00
m_mDispatchers["exec"] = spawn;
m_mDispatchers["killactive"] = killActive;
m_mDispatchers["togglefloating"] = toggleActiveFloating;
m_mDispatchers["workspace"] = changeworkspace;
m_mDispatchers["fullscreen"] = fullscreenActive;
m_mDispatchers["movetoworkspace"] = moveActiveToWorkspace;
m_mDispatchers["movetoworkspacesilent"] = moveActiveToWorkspaceSilent;
m_mDispatchers["pseudo"] = toggleActivePseudo;
m_mDispatchers["movefocus"] = moveFocusTo;
m_mDispatchers["movewindow"] = moveActiveTo;
m_mDispatchers["togglegroup"] = toggleGroup;
m_mDispatchers["changegroupactive"] = changeGroupActive;
m_mDispatchers["togglesplit"] = toggleSplit;
m_mDispatchers["splitratio"] = alterSplitRatio;
m_mDispatchers["focusmonitor"] = focusMonitor;
2022-05-22 11:52:39 +02:00
m_mDispatchers["movecursortocorner"] = moveCursorToCorner;
2022-05-26 19:05:32 +02:00
m_mDispatchers["workspaceopt"] = workspaceOpt;
m_mDispatchers["exit"] = exitHyprland;
2022-04-21 15:50:52 +02:00
}
2022-03-19 17:48:18 +01:00
void CKeybindManager::addKeybind(SKeybind kb) {
2022-04-21 17:06:43 +02:00
m_lKeybinds.push_back(kb);
}
void CKeybindManager::removeKeybind(uint32_t mod, const std::string& key) {
for (auto it = m_lKeybinds.begin(); it != m_lKeybinds.end(); ++it) {
if (it->modmask == mod && it->key == key) {
it = m_lKeybinds.erase(it);
}
}
2022-03-19 17:48:18 +01:00
}
uint32_t CKeybindManager::stringToModMask(std::string mods) {
uint32_t modMask = 0;
if (mods.find("SHIFT") != std::string::npos)
modMask |= WLR_MODIFIER_SHIFT;
if (mods.find("CAPS") != std::string::npos)
modMask |= WLR_MODIFIER_CAPS;
if (mods.find("CTRL") != std::string::npos || mods.find("CONTROL") != std::string::npos)
modMask |= WLR_MODIFIER_CTRL;
if (mods.find("ALT") != std::string::npos)
modMask |= WLR_MODIFIER_ALT;
if (mods.find("MOD2") != std::string::npos)
modMask |= WLR_MODIFIER_MOD2;
if (mods.find("MOD3") != std::string::npos)
modMask |= WLR_MODIFIER_MOD3;
if (mods.find("SUPER") != std::string::npos || mods.find("WIN") != std::string::npos || mods.find("LOGO") != std::string::npos || mods.find("MOD4") != std::string::npos)
modMask |= WLR_MODIFIER_LOGO;
if (mods.find("MOD5") != std::string::npos)
modMask |= WLR_MODIFIER_MOD5;
return modMask;
}
2022-03-19 22:03:40 +01:00
bool CKeybindManager::handleKeybinds(const uint32_t& modmask, const xkb_keysym_t& key) {
bool found = false;
if (handleInternalKeybinds(key))
return true;
2022-04-18 17:16:01 +02:00
if (g_pCompositor->m_sSeat.exclusiveClient){
Debug::log(LOG, "Not handling keybinds due to there being an exclusive inhibited client.");
return false;
}
2022-04-21 17:06:43 +02:00
for (auto& k : m_lKeybinds) {
2022-03-19 17:48:18 +01:00
if (modmask != k.modmask)
continue;
// oMg such performance hit!!11!
// this little maneouver is gonna cost us 4µs
const auto KBKEY = xkb_keysym_from_name(k.key.c_str(), XKB_KEYSYM_CASE_INSENSITIVE);
2022-03-23 16:51:48 +01:00
const auto KBKEYUPPER = xkb_keysym_to_upper(KBKEY);
// small TODO: fix 0-9 keys and other modified ones with shift
2022-03-22 22:22:59 +01:00
2022-03-23 16:51:48 +01:00
if (key != KBKEY && key != KBKEYUPPER)
2022-03-19 17:48:18 +01:00
continue;
2022-04-21 15:50:52 +02:00
const auto DISPATCHER = m_mDispatchers.find(k.handler);
// Should never happen, as we check in the ConfigManager, but oh well
if (DISPATCHER == m_mDispatchers.end()) {
2022-04-14 23:02:10 +02:00
Debug::log(ERR, "Inavlid handler in a keybind! (handler %s does not exist)", k.handler.c_str());
2022-04-21 15:50:52 +02:00
} else {
// call the dispatcher
DISPATCHER->second(k.arg);
2022-04-14 23:02:10 +02:00
}
2022-03-19 22:03:40 +01:00
found = true;
2022-03-19 17:48:18 +01:00
}
2022-03-19 22:03:40 +01:00
return found;
2022-03-19 17:48:18 +01:00
}
bool CKeybindManager::handleInternalKeybinds(xkb_keysym_t keysym) {
// Handles the CTRL+ALT+FX TTY keybinds
if (!(keysym >= XKB_KEY_XF86Switch_VT_1 && keysym <= XKB_KEY_XF86Switch_VT_12))
return false;
const auto PSESSION = wlr_backend_get_session(g_pCompositor->m_sWLRBackend);
if (PSESSION) {
const auto TTY = keysym - XKB_KEY_XF86Switch_VT_1 + 1;
wlr_session_change_vt(PSESSION, TTY);
return true;
}
return false;
}
2022-03-19 17:48:18 +01:00
// Dispatchers
void CKeybindManager::spawn(std::string args) {
2022-04-20 15:58:02 +02:00
if (g_pXWaylandManager->m_sWLRXWayland)
args = "WAYLAND_DISPLAY=" + std::string(g_pCompositor->m_szWLDisplaySocket) + " DISPLAY=" + std::string(g_pXWaylandManager->m_sWLRXWayland->display_name) + " " + args;
else
args = "WAYLAND_DISPLAY=" + std::string(g_pCompositor->m_szWLDisplaySocket) + " " + args;
2022-04-20 16:18:58 +02:00
2022-03-19 17:48:18 +01:00
Debug::log(LOG, "Executing %s", args.c_str());
if (fork() == 0) {
execl("/bin/sh", "/bin/sh", "-c", args.c_str(), nullptr);
_exit(0);
}
}
void CKeybindManager::killActive(std::string args) {
2022-04-02 18:57:09 +02:00
if (g_pCompositor->m_pLastWindow && g_pCompositor->windowValidMapped(g_pCompositor->m_pLastWindow)) {
g_pXWaylandManager->sendCloseWindow(g_pCompositor->m_pLastWindow);
g_pCompositor->m_pLastFocus = nullptr;
g_pCompositor->m_pLastWindow = nullptr;
g_pEventManager->postEvent(SHyprIPCEvent("activewindow", ",")); // post an activewindow event to empty, as we are currently unfocused
2022-04-02 18:57:09 +02:00
}
2022-03-20 11:14:24 +01:00
g_pCompositor->focusWindow(g_pCompositor->windowFromCursor());
}
void CKeybindManager::clearKeybinds() {
2022-04-21 17:06:43 +02:00
m_lKeybinds.clear();
2022-03-20 11:14:24 +01:00
}
void CKeybindManager::toggleActiveFloating(std::string args) {
2022-04-02 18:57:09 +02:00
const auto ACTIVEWINDOW = g_pCompositor->m_pLastWindow;
2022-03-20 11:14:24 +01:00
if (g_pCompositor->windowValidMapped(ACTIVEWINDOW)) {
ACTIVEWINDOW->m_bIsFloating = !ACTIVEWINDOW->m_bIsFloating;
2022-03-20 13:37:07 +01:00
2022-04-23 14:16:02 +02:00
ACTIVEWINDOW->m_vRealPosition.setValue(ACTIVEWINDOW->m_vRealPosition.vec() + Vector2D(5, 5));
ACTIVEWINDOW->m_vSize = ACTIVEWINDOW->m_vRealPosition.vec() - Vector2D(10, 10);
2022-03-20 13:37:07 +01:00
2022-03-20 11:14:24 +01:00
g_pLayoutManager->getCurrentLayout()->changeWindowFloatingMode(ACTIVEWINDOW);
}
2022-03-20 15:55:47 +01:00
}
2022-04-02 20:04:32 +02:00
void CKeybindManager::toggleActivePseudo(std::string args) {
const auto ACTIVEWINDOW = g_pCompositor->m_pLastWindow;
if (!g_pCompositor->windowValidMapped(ACTIVEWINDOW))
return;
ACTIVEWINDOW->m_bIsPseudotiled = !ACTIVEWINDOW->m_bIsPseudotiled;
g_pLayoutManager->getCurrentLayout()->recalculateWindow(ACTIVEWINDOW);
}
2022-04-21 17:21:55 +02:00
void CKeybindManager::changeworkspace(std::string args) {
2022-04-21 16:38:48 +02:00
int workspaceToChangeTo = 0;
std::string workspaceName = "";
2022-05-18 12:18:58 +02:00
workspaceToChangeTo = getWorkspaceIDFromString(args, workspaceName);
2022-04-14 23:02:10 +02:00
2022-04-20 16:53:41 +02:00
if (workspaceToChangeTo == INT_MAX) {
Debug::log(ERR, "Error in changeworkspace, invalid value");
return;
2022-03-20 15:55:47 +01:00
}
// remove constraints
g_pCompositor->m_sSeat.mouse->constraintActive = false;
2022-03-20 15:55:47 +01:00
// if it exists, we warp to it
if (g_pCompositor->getWorkspaceByID(workspaceToChangeTo)) {
const auto PMONITOR = g_pCompositor->getMonitorFromID(g_pCompositor->getWorkspaceByID(workspaceToChangeTo)->m_iMonitorID);
2022-03-20 15:55:47 +01:00
const auto PWORKSPACETOCHANGETO = g_pCompositor->getWorkspaceByID(workspaceToChangeTo);
2022-03-20 15:55:47 +01:00
// if it's not visible, make it visible.
if (!g_pCompositor->isWorkspaceVisible(workspaceToChangeTo)) {
const auto OLDWORKSPACEID = PMONITOR->activeWorkspace;
// change it
2022-03-20 15:55:47 +01:00
PMONITOR->activeWorkspace = workspaceToChangeTo;
// we need to move XWayland windows to narnia or otherwise they will still process our cursor and shit
// and that'd be annoying as hell
g_pCompositor->fixXWaylandWindowsOnWorkspace(OLDWORKSPACEID);
// and fix on the new workspace
g_pCompositor->fixXWaylandWindowsOnWorkspace(PMONITOR->activeWorkspace);
2022-05-12 11:27:31 +02:00
// here and only here begin anim. we don't want to anim visible workspaces on other monitors.
2022-05-12 13:03:02 +02:00
// check if anim left or right
const auto ANIMTOLEFT = workspaceToChangeTo > OLDWORKSPACEID;
2022-05-12 11:27:31 +02:00
// start anim on old workspace
2022-05-16 23:13:32 +02:00
g_pCompositor->getWorkspaceByID(OLDWORKSPACEID)->startAnim(false, ANIMTOLEFT);
2022-05-12 11:27:31 +02:00
// start anim on new workspace
PWORKSPACETOCHANGETO->startAnim(true, ANIMTOLEFT);
// Event ONLY if workspace is actually "changed" and we arent just focusing
if (!m_bSuppressWorkspaceChangeEvents)
g_pEventManager->postEvent(SHyprIPCEvent("workspace", PWORKSPACETOCHANGETO->m_szName));
}
2022-03-20 15:55:47 +01:00
// If the monitor is not the one our cursor's at, warp to it.
if (PMONITOR != g_pCompositor->getMonitorFromCursor()) {
Vector2D middle = PMONITOR->vecPosition + PMONITOR->vecSize / 2.f;
wlr_cursor_warp(g_pCompositor->m_sWLRCursor, nullptr, middle.x, middle.y);
}
// focus the first window
g_pCompositor->focusWindow(g_pCompositor->getFirstWindowOnWorkspace(workspaceToChangeTo));
// set active and deactivate all other in wlr
g_pCompositor->deactivateAllWLRWorkspaces(PWORKSPACETOCHANGETO->m_pWlrHandle);
2022-05-25 10:25:36 +02:00
PWORKSPACETOCHANGETO->setActive(true);
Debug::log(LOG, "Changed to workspace %i", workspaceToChangeTo);
2022-04-14 23:02:10 +02:00
// focus
g_pInputManager->refocus();
// mark the monitor dirty
g_pHyprRenderer->damageMonitor(PMONITOR);
2022-03-20 15:55:47 +01:00
return;
}
// Workspace doesn't exist, create and switch
const auto PMONITOR = g_pCompositor->getMonitorFromCursor();
2022-03-20 19:28:57 +01:00
const auto OLDWORKSPACE = PMONITOR->activeWorkspace;
2022-05-12 13:03:02 +02:00
// get anim direction
const auto ANIMTOLEFT = workspaceToChangeTo > OLDWORKSPACE;
2022-05-12 11:27:31 +02:00
// start anim on old workspace
2022-05-16 23:13:32 +02:00
g_pCompositor->getWorkspaceByID(OLDWORKSPACE)->startAnim(false, ANIMTOLEFT);
2022-05-12 11:27:31 +02:00
g_pCompositor->m_lWorkspaces.emplace_back(PMONITOR->ID);
2022-03-20 15:55:47 +01:00
const auto PWORKSPACE = &g_pCompositor->m_lWorkspaces.back();
2022-05-12 11:27:31 +02:00
// start anim on new workspace
2022-05-16 23:13:32 +02:00
PWORKSPACE->startAnim(true, ANIMTOLEFT);
2022-05-12 11:27:31 +02:00
// We are required to set the name here immediately
2022-04-21 16:38:48 +02:00
wlr_ext_workspace_handle_v1_set_name(PWORKSPACE->m_pWlrHandle, workspaceName.c_str());
PWORKSPACE->m_iID = workspaceToChangeTo;
PWORKSPACE->m_iMonitorID = PMONITOR->ID;
2022-04-21 16:38:48 +02:00
PWORKSPACE->m_szName = workspaceName;
2022-03-20 15:55:47 +01:00
PMONITOR->activeWorkspace = workspaceToChangeTo;
2022-03-20 19:28:57 +01:00
// we need to move XWayland windows to narnia or otherwise they will still process our cursor and shit
// and that'd be annoying as hell
2022-03-20 19:29:50 +01:00
g_pCompositor->fixXWaylandWindowsOnWorkspace(OLDWORKSPACE);
// set active and deactivate all other
2022-04-28 17:55:25 +02:00
g_pCompositor->deactivateAllWLRWorkspaces(PWORKSPACE->m_pWlrHandle);
2022-05-25 10:25:36 +02:00
PWORKSPACE->setActive(true);
2022-04-14 20:08:39 +02:00
// mark the monitor dirty
g_pHyprRenderer->damageMonitor(PMONITOR);
2022-04-14 23:02:10 +02:00
// focus (clears the last)
g_pInputManager->refocus();
// Event
if (!m_bSuppressWorkspaceChangeEvents)
g_pEventManager->postEvent(SHyprIPCEvent("workspace", PWORKSPACE->m_szName));
Debug::log(LOG, "Changed to workspace %i", workspaceToChangeTo);
2022-03-21 19:18:33 +01:00
}
void CKeybindManager::fullscreenActive(std::string args) {
2022-04-02 18:57:09 +02:00
const auto PWINDOW = g_pCompositor->m_pLastWindow;
2022-03-21 19:18:33 +01:00
if (!g_pCompositor->windowValidMapped(PWINDOW))
return;
g_pLayoutManager->getCurrentLayout()->fullscreenRequestForWindow(PWINDOW);
2022-03-30 17:39:04 +02:00
g_pXWaylandManager->setWindowFullscreen(PWINDOW, PWINDOW->m_bIsFullscreen);
// make all windows on the same workspace under the fullscreen window
for (auto& w : g_pCompositor->m_lWindows) {
if (w.m_iWorkspaceID == PWINDOW->m_iWorkspaceID)
w.m_bCreatedOverFullscreen = false;
}
2022-03-23 16:51:48 +01:00
}
void CKeybindManager::moveActiveToWorkspace(std::string args) {
2022-04-02 18:57:09 +02:00
const auto PWINDOW = g_pCompositor->m_pLastWindow;
2022-03-23 16:51:48 +01:00
if (!g_pCompositor->windowValidMapped(PWINDOW))
return;
g_pLayoutManager->getCurrentLayout()->onWindowRemoved(PWINDOW);
const auto OLDWORKSPACE = g_pCompositor->getWorkspaceByID(PWINDOW->m_iWorkspaceID);
// hack
2022-04-21 16:38:48 +02:00
g_pKeybindManager->changeworkspace(args);
const auto PWORKSPACE = g_pCompositor->getWorkspaceByString(args);
2022-03-23 16:51:48 +01:00
2022-04-21 16:38:48 +02:00
if (PWORKSPACE == OLDWORKSPACE) {
Debug::log(LOG, "Not moving to workspace because it didn't change.");
return;
}
2022-03-23 16:51:48 +01:00
OLDWORKSPACE->m_bHasFullscreenWindow = false;
2022-03-23 16:51:48 +01:00
2022-04-21 16:38:48 +02:00
PWINDOW->m_iWorkspaceID = PWORKSPACE->m_iID;
PWINDOW->m_iMonitorID = PWORKSPACE->m_iMonitorID;
2022-03-23 16:51:48 +01:00
PWINDOW->m_bIsFullscreen = false;
2022-04-21 16:38:48 +02:00
if (PWORKSPACE->m_bHasFullscreenWindow) {
g_pCompositor->getFullscreenWindowOnWorkspace(PWORKSPACE->m_iID)->m_bIsFullscreen = false;
PWORKSPACE->m_bHasFullscreenWindow = false;
2022-03-23 16:51:48 +01:00
}
// Hack: So that the layout doesnt find our window at the cursor
PWINDOW->m_vPosition = Vector2D(-42069, -42069);
2022-04-10 21:45:24 +02:00
// Save the real position and size because the layout might set its own
2022-04-23 14:16:02 +02:00
const auto PSAVEDSIZE = PWINDOW->m_vRealSize.vec();
const auto PSAVEDPOS = PWINDOW->m_vRealPosition.vec();
2022-03-23 16:51:48 +01:00
g_pLayoutManager->getCurrentLayout()->onWindowCreated(PWINDOW);
2022-04-10 21:45:24 +02:00
// and restore it
2022-04-23 14:16:02 +02:00
PWINDOW->m_vRealPosition.setValue(PSAVEDPOS);
PWINDOW->m_vRealSize.setValue(PSAVEDSIZE);
2022-03-23 16:51:48 +01:00
if (PWINDOW->m_bIsFloating) {
2022-04-23 14:16:02 +02:00
PWINDOW->m_vRealPosition.setValue(PWINDOW->m_vRealPosition.vec() - g_pCompositor->getMonitorFromID(OLDWORKSPACE->m_iMonitorID)->vecPosition);
PWINDOW->m_vRealPosition.setValue(PWINDOW->m_vRealPosition.vec() + g_pCompositor->getMonitorFromID(PWORKSPACE->m_iMonitorID)->vecPosition);
PWINDOW->m_vPosition = PWINDOW->m_vRealPosition.vec();
2022-03-23 16:51:48 +01:00
}
}
2022-04-09 13:26:55 +02:00
2022-05-18 12:18:58 +02:00
void CKeybindManager::moveActiveToWorkspaceSilent(std::string args) {
// hacky, but works lol
int workspaceToMoveTo = 0;
std::string workspaceName = "";
workspaceToMoveTo = getWorkspaceIDFromString(args, workspaceName);
if (workspaceToMoveTo == INT_MAX) {
Debug::log(ERR, "Error in moveActiveToWorkspaceSilent, invalid value");
return;
}
const auto PWINDOW = g_pCompositor->m_pLastWindow;
if (!g_pCompositor->windowValidMapped(PWINDOW))
return;
const auto PMONITOR = g_pCompositor->getMonitorFromID(PWINDOW->m_iMonitorID);
if (workspaceToMoveTo == PMONITOR->activeWorkspace)
return;
// may be null until later!
auto PWORKSPACE = g_pCompositor->getWorkspaceByID(workspaceToMoveTo);
const auto PMONITORNEW = PWORKSPACE ? g_pCompositor->getMonitorFromID(PWORKSPACE->m_iMonitorID) : PMONITOR;
const auto OLDWORKSPACEIDONMONITOR = PMONITORNEW->activeWorkspace;
const auto OLDWORKSPACEIDRETURN = PMONITOR->activeWorkspace;
const auto POLDWORKSPACEONMON = g_pCompositor->getWorkspaceByID(OLDWORKSPACEIDONMONITOR);
const auto POLDWORKSPACEIDRETURN = g_pCompositor->getWorkspaceByID(OLDWORKSPACEIDRETURN);
m_bSuppressWorkspaceChangeEvents = true;
2022-05-18 12:18:58 +02:00
moveActiveToWorkspace(args);
PWORKSPACE = g_pCompositor->getWorkspaceByID(workspaceToMoveTo);
changeworkspace(std::to_string(OLDWORKSPACEIDONMONITOR));
changeworkspace(std::to_string(OLDWORKSPACEIDRETURN));
// revert animations
PWORKSPACE->m_vRenderOffset.setValueAndWarp(Vector2D(0,0));
PWORKSPACE->m_fAlpha.setValueAndWarp(0.f);
POLDWORKSPACEIDRETURN->m_vRenderOffset.setValueAndWarp(Vector2D(0, 0));
POLDWORKSPACEIDRETURN->m_fAlpha.setValueAndWarp(255.f);
POLDWORKSPACEONMON->m_vRenderOffset.setValueAndWarp(Vector2D(0, 0));
POLDWORKSPACEONMON->m_fAlpha.setValueAndWarp(255.f);
m_bSuppressWorkspaceChangeEvents = false;
2022-05-18 12:18:58 +02:00
}
2022-04-09 13:26:55 +02:00
void CKeybindManager::moveFocusTo(std::string args) {
char arg = args[0];
2022-05-05 12:50:25 +02:00
if (!isDirection(args)) {
2022-04-20 16:18:58 +02:00
Debug::log(ERR, "Cannot move focus in direction %c, unsupported direction. Supported: l,r,u/t,d/b", arg);
2022-04-09 13:26:55 +02:00
return;
}
const auto PLASTWINDOW = g_pCompositor->m_pLastWindow;
// remove constraints
g_pCompositor->m_sSeat.mouse->constraintActive = false;
2022-04-13 20:45:06 +02:00
auto switchToWindow = [&](CWindow* PWINDOWTOCHANGETO) {
g_pCompositor->focusWindow(PWINDOWTOCHANGETO);
Vector2D middle = PWINDOWTOCHANGETO->m_vRealPosition.goalv() + PWINDOWTOCHANGETO->m_vRealSize.goalv() / 2.f;
2022-04-13 20:45:06 +02:00
wlr_cursor_warp(g_pCompositor->m_sWLRCursor, nullptr, middle.x, middle.y);
};
if (!g_pCompositor->windowValidMapped(PLASTWINDOW)) {
const auto PWINDOWTOCHANGETO = g_pCompositor->getFirstWindowOnWorkspace(g_pCompositor->m_pLastMonitor->activeWorkspace);
if (!PWINDOWTOCHANGETO)
return;
switchToWindow(PWINDOWTOCHANGETO);
2022-04-09 13:26:55 +02:00
return;
2022-04-13 20:45:06 +02:00
}
2022-04-09 13:26:55 +02:00
const auto PWINDOWTOCHANGETO = g_pCompositor->getWindowInDirection(PLASTWINDOW, arg);
2022-04-09 13:33:44 +02:00
if (PWINDOWTOCHANGETO) {
2022-04-13 20:45:06 +02:00
switchToWindow(PWINDOWTOCHANGETO);
} else {
const auto PWINDOWNEXT = g_pCompositor->getNextWindowOnWorkspace(PLASTWINDOW);
if (PWINDOWNEXT) {
switchToWindow(PWINDOWNEXT);
}
}
}
2022-04-20 16:18:58 +02:00
void CKeybindManager::moveActiveTo(std::string args) {
char arg = args[0];
2022-05-05 13:02:55 +02:00
const auto LASTMONITOR = g_pCompositor->m_pLastMonitor;
if (args.find("mon:") == 0) {
// hack: save the active window
const auto PACTIVE = g_pCompositor->m_pLastWindow;
// monitor
focusMonitor(args.substr(4));
if (LASTMONITOR == g_pCompositor->m_pLastMonitor) {
Debug::log(ERR, "moveActiveTo: moving to an invalid mon");
return;
}
// restore the active
g_pCompositor->focusWindow(PACTIVE);
moveActiveToWorkspace(std::to_string(g_pCompositor->m_pLastMonitor->activeWorkspace));
return;
}
2022-05-05 12:50:25 +02:00
if (!isDirection(args)) {
2022-04-20 16:18:58 +02:00
Debug::log(ERR, "Cannot move window in direction %c, unsupported direction. Supported: l,r,u/t,d/b", arg);
return;
}
const auto PLASTWINDOW = g_pCompositor->m_pLastWindow;
2022-04-20 16:53:41 +02:00
if (!g_pCompositor->windowValidMapped(PLASTWINDOW))
2022-04-20 16:18:58 +02:00
return;
const auto PWINDOWTOCHANGETO = g_pCompositor->getWindowInDirection(PLASTWINDOW, arg);
2022-04-20 16:53:41 +02:00
if (!g_pCompositor->windowValidMapped(PWINDOWTOCHANGETO))
2022-04-20 16:18:58 +02:00
return;
g_pLayoutManager->getCurrentLayout()->switchWindows(PLASTWINDOW, PWINDOWTOCHANGETO);
}
void CKeybindManager::toggleGroup(std::string args) {
2022-05-16 17:37:46 +02:00
SLayoutMessageHeader header;
header.pWindow = g_pCompositor->m_pLastWindow;
g_pLayoutManager->getCurrentLayout()->layoutMessage(header, "togglegroup");
}
void CKeybindManager::changeGroupActive(std::string args) {
2022-05-16 17:37:46 +02:00
SLayoutMessageHeader header;
header.pWindow = g_pCompositor->m_pLastWindow;
2022-05-28 19:16:20 +02:00
if (args == "b")
g_pLayoutManager->getCurrentLayout()->layoutMessage(header, "changegroupactiveb");
else
g_pLayoutManager->getCurrentLayout()->layoutMessage(header, "changegroupactivef");
2022-05-16 17:37:46 +02:00
}
void CKeybindManager::toggleSplit(std::string args) {
SLayoutMessageHeader header;
header.pWindow = g_pCompositor->m_pLastWindow;
g_pLayoutManager->getCurrentLayout()->layoutMessage(header, "togglesplit");
2022-04-20 16:53:41 +02:00
}
void CKeybindManager::alterSplitRatio(std::string args) {
float splitratio = 0;
if (args == "+" || args == "-") {
Debug::log(LOG, "alterSplitRatio: using LEGACY +/-, consider switching to the Hyprland syntax.");
splitratio = (args == "+" ? 0.05f : -0.05f);
}
if (splitratio == 0) {
splitratio = getPlusMinusKeywordResult(args, 0);
}
if (splitratio == INT_MAX) {
Debug::log(ERR, "Splitratio invalid in alterSplitRatio!");
return;
}
const auto PLASTWINDOW = g_pCompositor->m_pLastWindow;
if (!g_pCompositor->windowValidMapped(PLASTWINDOW))
return;
g_pLayoutManager->getCurrentLayout()->alterSplitRatioBy(PLASTWINDOW, splitratio);
2022-05-05 12:50:25 +02:00
}
void CKeybindManager::focusMonitor(std::string arg) {
if (isNumber(arg)) {
// change by ID
int monID = -1;
try {
monID = std::stoi(arg);
} catch (std::exception& e) {
// shouldn't happen but jic
Debug::log(ERR, "Error in focusMonitor: invalid num");
}
if (monID > -1 && monID < (int)g_pCompositor->m_lMonitors.size()) {
changeworkspace(std::to_string(g_pCompositor->getMonitorFromID(monID)->activeWorkspace));
} else {
Debug::log(ERR, "Error in focusMonitor: invalid arg 1");
}
} else {
if (isDirection(arg)) {
const auto PMONITOR = g_pCompositor->getMonitorInDirection(arg[0]);
if (PMONITOR) {
2022-05-28 18:38:49 +02:00
if (PMONITOR->activeWorkspace < 0) {
const auto PWORKSPACE = g_pCompositor->getWorkspaceByID(PMONITOR->activeWorkspace);
changeworkspace("name:" + PWORKSPACE->m_szName);
}
else
changeworkspace(std::to_string(PMONITOR->activeWorkspace));
2022-05-05 12:50:25 +02:00
return;
}
} else {
for (auto& m : g_pCompositor->m_lMonitors) {
if (m.szName == arg) {
changeworkspace(std::to_string(m.activeWorkspace));
return;
}
}
}
Debug::log(ERR, "Error in focusMonitor: no such monitor");
}
2022-05-18 12:18:58 +02:00
}
2022-05-22 11:52:39 +02:00
void CKeybindManager::moveCursorToCorner(std::string arg) {
if (!isNumber(arg)) {
Debug::log(ERR, "moveCursorToCorner, arg has to be a number.");
return;
}
const auto CORNER = std::stoi(arg);
if (CORNER < 0 || CORNER > 3) {
Debug::log(ERR, "moveCursorToCorner, corner not 0 - 3.");
return;
}
const auto PWINDOW = g_pCompositor->m_pLastWindow;
if (!g_pCompositor->windowValidMapped(PWINDOW))
return;
switch (CORNER) {
case 0:
// bottom left
wlr_cursor_warp(g_pCompositor->m_sWLRCursor, g_pCompositor->m_sSeat.mouse->mouse, PWINDOW->m_vRealPosition.vec().x, PWINDOW->m_vRealPosition.vec().y + PWINDOW->m_vRealSize.vec().y);
break;
case 1:
// bottom right
wlr_cursor_warp(g_pCompositor->m_sWLRCursor, g_pCompositor->m_sSeat.mouse->mouse, PWINDOW->m_vRealPosition.vec().x + PWINDOW->m_vRealSize.vec().x, PWINDOW->m_vRealPosition.vec().y + PWINDOW->m_vRealSize.vec().y);
break;
case 2:
// top right
wlr_cursor_warp(g_pCompositor->m_sWLRCursor, g_pCompositor->m_sSeat.mouse->mouse, PWINDOW->m_vRealPosition.vec().x + PWINDOW->m_vRealSize.vec().x, PWINDOW->m_vRealPosition.vec().y);
break;
case 3:
// top left
wlr_cursor_warp(g_pCompositor->m_sWLRCursor, g_pCompositor->m_sSeat.mouse->mouse, PWINDOW->m_vRealPosition.vec().x, PWINDOW->m_vRealPosition.vec().y);
break;
}
2022-05-26 19:05:32 +02:00
}
void CKeybindManager::workspaceOpt(std::string args) {
// current workspace
const auto PWORKSPACE = g_pCompositor->getWorkspaceByID(g_pCompositor->m_pLastMonitor->activeWorkspace);
if (!PWORKSPACE)
return; // ????
if (args == "allpseudo") {
PWORKSPACE->m_bDefaultPseudo = !PWORKSPACE->m_bDefaultPseudo;
// apply
for (auto& w : g_pCompositor->m_lWindows) {
if (!w.m_bIsMapped || w.m_iWorkspaceID != PWORKSPACE->m_iID)
continue;
w.m_bIsPseudotiled = PWORKSPACE->m_bDefaultPseudo;
}
} else if (args == "allfloat") {
PWORKSPACE->m_bDefaultFloating = !PWORKSPACE->m_bDefaultFloating;
// apply
// we make a copy because changeWindowFloatingMode might invalidate the iterator
std::deque<CWindow*> ptrs;
for (auto& w : g_pCompositor->m_lWindows)
ptrs.push_back(&w);
for (auto& w : ptrs) {
if (!w->m_bIsMapped || w->m_iWorkspaceID != PWORKSPACE->m_iID)
continue;
if (!w->m_bRequestsFloat && w->m_bIsFloating != PWORKSPACE->m_bDefaultFloating) {
const auto SAVEDPOS = w->m_vRealPosition.vec();
const auto SAVEDSIZE = w->m_vRealSize.vec();
w->m_bIsFloating = PWORKSPACE->m_bDefaultFloating;
g_pLayoutManager->getCurrentLayout()->changeWindowFloatingMode(w);
if (PWORKSPACE->m_bDefaultFloating) {
w->m_vRealPosition.setValueAndWarp(SAVEDPOS);
w->m_vRealSize.setValueAndWarp(SAVEDSIZE);
g_pXWaylandManager->setWindowSize(w, SAVEDSIZE);
w->m_vRealSize = w->m_vRealSize.vec() + Vector2D(4,4);
w->m_vRealPosition = w->m_vRealPosition.vec() - Vector2D(2,2);
}
}
}
} else {
Debug::log(ERR, "Invalid arg in workspaceOpt, opt \"%s\" doesn't exist.", args.c_str());
return;
}
// recalc mon
g_pLayoutManager->getCurrentLayout()->recalculateMonitor(g_pCompositor->m_pLastMonitor->ID);
}
void CKeybindManager::exitHyprland(std::string argz) {
g_pCompositor->cleanupExit();
exit(0);
2022-05-22 11:52:39 +02:00
}