From dcbf69f8b8337f37c8a98cd299fc52ec618a831b Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Fri, 19 Nov 2021 22:29:44 +0100 Subject: [PATCH] added moving windows and fixed sticks --- src/KeybindManager.cpp | 11 ++++++ src/KeybindManager.hpp | 1 + src/defines.hpp | 2 +- src/windowManager.cpp | 78 +++++++++++++++++++++++++++++++++++++++--- src/windowManager.hpp | 2 ++ 5 files changed, 88 insertions(+), 6 deletions(-) diff --git a/src/KeybindManager.cpp b/src/KeybindManager.cpp index 5c7ef3e..0228101 100644 --- a/src/KeybindManager.cpp +++ b/src/KeybindManager.cpp @@ -17,9 +17,16 @@ void KeybindManager::reloadAllKeybinds() { // todo: config KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0x72 /* R */, "dmenu_run", &KeybindManager::call)); KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0x71 /* Q */, "kitty", &KeybindManager::call)); + KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0xff0d /* Enter */, "xterm", &KeybindManager::call)); KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0x62 /* G */, "google-chrome-stable", &KeybindManager::call)); KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0x63 /* C */, "", &KeybindManager::killactive)); + + // move window + KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0xff51 /* < */, "l", &KeybindManager::movewindow)); + KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0xff53 /* > */, "r", &KeybindManager::movewindow)); + KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0xff52 /* ^ */, "t", &KeybindManager::movewindow)); + KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0xff54 /* v */, "b", &KeybindManager::movewindow)); } unsigned int KeybindManager::modToMask(MODS mod) { @@ -94,4 +101,8 @@ void KeybindManager::call(std::string args) { _exit(0); } wait(NULL); +} + +void KeybindManager::movewindow(std::string arg) { + WindowManager::moveActiveWindowTo(arg[0]); } \ No newline at end of file diff --git a/src/KeybindManager.hpp b/src/KeybindManager.hpp index 9d5bd0f..a1c9f96 100644 --- a/src/KeybindManager.hpp +++ b/src/KeybindManager.hpp @@ -17,4 +17,5 @@ namespace KeybindManager { // Dispatchers void call(std::string args); void killactive(std::string args); + void movewindow(std::string args); }; \ No newline at end of file diff --git a/src/defines.hpp b/src/defines.hpp index 4598c84..d06f305 100644 --- a/src/defines.hpp +++ b/src/defines.hpp @@ -21,4 +21,4 @@ #define EVENT(name) \ void event##name(xcb_generic_event_t* event); -#define STICKS(a, b) abs(a - b) < 2 \ No newline at end of file +#define STICKS(a, b) abs((a) - (b)) < 2 \ No newline at end of file diff --git a/src/windowManager.cpp b/src/windowManager.cpp index 0568292..ab7433a 100644 --- a/src/windowManager.cpp +++ b/src/windowManager.cpp @@ -78,6 +78,8 @@ bool WindowManager::handleEvent() { void WindowManager::refreshDirtyWindows() { for(auto& window : windows) { if (window.getDirty()) { + + Values[0] = (int)window.getEffectiveSize().x; Values[1] = (int)window.getEffectiveSize().y; xcb_configure_window(WindowManager::DisplayConnection, window.getDrawable(), XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, Values); @@ -109,7 +111,7 @@ void WindowManager::refreshDirtyWindows() { window.setDirty(false); - Debug::log(LOG, "Refreshed dirty window."); + Debug::log(LOG, "Refreshed dirty window, with an ID of " + std::to_string(window.getDrawable())); } } } @@ -131,8 +133,6 @@ CWindow* WindowManager::getWindowFromDrawable(xcb_drawable_t window) { if (w.getDrawable() == window) { return &w; } - - Debug::log(LOG, "Not " + std::to_string(w.getDrawable())); } return nullptr; } @@ -159,7 +159,6 @@ void WindowManager::removeWindowFromVectorSafe(xcb_drawable_t window) { WindowManager::windows.push_back(p); continue; } - Debug::log(LOG, "Is, removing: " + std::to_string(p.getDrawable())); } } @@ -286,7 +285,7 @@ bool canEatWindow(CWindow* a, CWindow* toEat) { } void eatWindow(CWindow* a, CWindow* toEat) { - + // Size is pos + size max - pos const auto OPPCORNERA = a->getPosition() + a->getSize(); const auto OPPCORNERB = toEat->getPosition() + toEat->getSize(); @@ -323,4 +322,73 @@ void WindowManager::fixWindowOnClose(CWindow* pClosedWindow) { WindowManager::setFocusedWindow(neighbor->getDrawable()); // Set focus. :) setEffectiveSizePosUsingConfig(neighbor); +} + +CWindow* getNeighborInDir(char dir) { + + const auto CURRENTWINDOW = WindowManager::getWindowFromDrawable(WindowManager::LastWindow); + + if (!CURRENTWINDOW) + return nullptr; + + const auto POSA = CURRENTWINDOW->getPosition(); + const auto SIZEA = CURRENTWINDOW->getSize(); + + for (auto& w : WindowManager::windows) { + if (w.getDrawable() == CURRENTWINDOW->getDrawable()) + continue; + + const auto POSB = w.getPosition(); + const auto SIZEB = w.getSize(); + + switch (dir) { + case 'l': + if (STICKS(POSA.x, POSB.x + SIZEB.x)) + return &w; + break; + case 'r': + if (STICKS(POSA.x + SIZEA.x, POSB.x)) + return &w; + break; + case 't': + if (STICKS(POSA.y, POSB.y + SIZEB.y)) + return &w; + break; + case 'b': + if (STICKS(POSA.y + SIZEA.y, POSB.y)) + return &w; + break; + } + } + + return nullptr; +} + +void WindowManager::moveActiveWindowTo(char dir) { + + const auto CURRENTWINDOW = WindowManager::getWindowFromDrawable(WindowManager::LastWindow); + + if (!CURRENTWINDOW) + return; + + const auto neighbor = getNeighborInDir(dir); + + if (!neighbor) + return; + + // swap their stuff and mark dirty + const auto TEMP_SIZEA = CURRENTWINDOW->getSize(); + const auto TEMP_POSA = CURRENTWINDOW->getPosition(); + + CURRENTWINDOW->setSize(neighbor->getSize()); + CURRENTWINDOW->setPosition(neighbor->getPosition()); + + neighbor->setSize(TEMP_SIZEA); + neighbor->setPosition(TEMP_POSA); + + CURRENTWINDOW->setDirty(true); + neighbor->setDirty(true); + + setEffectiveSizePosUsingConfig(neighbor); + setEffectiveSizePosUsingConfig(CURRENTWINDOW); } \ No newline at end of file diff --git a/src/windowManager.hpp b/src/windowManager.hpp index 9afede3..cd29388 100644 --- a/src/windowManager.hpp +++ b/src/windowManager.hpp @@ -34,4 +34,6 @@ namespace WindowManager { void calculateNewWindowParams(CWindow*); void fixWindowOnClose(CWindow*); + + void moveActiveWindowTo(char); }; \ No newline at end of file