|
| 1 | +module; |
| 2 | + |
| 3 | +#include <CppUtils/System/Windows.hpp> |
| 4 | +#include <cstdio> |
| 5 | + |
| 6 | +export module CppUtils.Terminal.Mouse; |
| 7 | + |
| 8 | +import std; |
| 9 | +import CppUtils.Container.Size; |
| 10 | +import CppUtils.Terminal.RawTerminal; |
| 11 | + |
| 12 | +export namespace CppUtils::Terminal::Mouse |
| 13 | +{ |
| 14 | + enum class Button |
| 15 | + { |
| 16 | + None, |
| 17 | + Left, |
| 18 | + Middle, |
| 19 | + Right, |
| 20 | + ScrollUp, |
| 21 | + ScrollDown |
| 22 | + }; |
| 23 | + |
| 24 | + struct Event |
| 25 | + { |
| 26 | + Button button = Button::None; |
| 27 | + bool pressed = false; |
| 28 | + bool dragging = false; |
| 29 | + bool shift = false; |
| 30 | + bool alt = false; |
| 31 | + bool ctrl = false; |
| 32 | + Container::Size2 position; |
| 33 | + }; |
| 34 | + |
| 35 | + namespace Ansi |
| 36 | + { |
| 37 | + inline constexpr auto EnableMouseTracking = "\x1b[?1003h"; // All motion |
| 38 | + inline constexpr auto DisableMouseTracking = "\x1b[?1003l"; |
| 39 | + inline constexpr auto EnableExtendedCoordinates = "\x1b[?1006h"; // SGR extended mode |
| 40 | + inline constexpr auto DisableExtendedCoordinates = "\x1b[?1006l"; |
| 41 | + } |
| 42 | + |
| 43 | + class Tracker final |
| 44 | + { |
| 45 | + public: |
| 46 | + inline Tracker(): |
| 47 | + m_active{true} |
| 48 | + { |
| 49 | +#if defined(OS_LINUX) or defined(OS_MACOS) |
| 50 | + std::print("{}{}", Ansi::EnableMouseTracking, Ansi::EnableExtendedCoordinates); |
| 51 | + std::fflush(stdout); |
| 52 | +#endif |
| 53 | + } |
| 54 | + |
| 55 | + inline ~Tracker() |
| 56 | + { |
| 57 | + if (m_active) |
| 58 | + { |
| 59 | +#if defined(OS_LINUX) or defined(OS_MACOS) |
| 60 | + std::print("{}{}", Ansi::DisableMouseTracking, Ansi::DisableExtendedCoordinates); |
| 61 | + std::fflush(stdout); |
| 62 | +#endif |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + Tracker(const Tracker&) = delete; |
| 67 | + Tracker& operator=(const Tracker&) = delete; |
| 68 | + |
| 69 | + inline Tracker(Tracker&& other) noexcept: |
| 70 | + m_active{std::exchange(other.m_active, false)} |
| 71 | + {} |
| 72 | + |
| 73 | + inline Tracker& operator=(Tracker&& other) noexcept |
| 74 | + { |
| 75 | + if (this != std::addressof(other)) |
| 76 | + m_active = std::exchange(other.m_active, false); |
| 77 | + return *this; |
| 78 | + } |
| 79 | + |
| 80 | + private: |
| 81 | + bool m_active = false; |
| 82 | + }; |
| 83 | + |
| 84 | + [[nodiscard]] inline auto getEvent() -> std::optional<Event> |
| 85 | + { |
| 86 | +#if defined(OS_LINUX) or defined(OS_MACOS) |
| 87 | + if (not RawTerminal::isInputAvailable()) |
| 88 | + return std::nullopt; |
| 89 | + |
| 90 | + auto raw = RawTerminal{}; |
| 91 | + |
| 92 | + if (raw.readChar() != '\x1b') |
| 93 | + return std::nullopt; |
| 94 | + if (raw.readChar() != '[') |
| 95 | + return std::nullopt; |
| 96 | + if (raw.readChar() != '<') |
| 97 | + return std::nullopt; |
| 98 | + |
| 99 | + auto buffer = std::string{}; |
| 100 | + auto c = char{}; |
| 101 | + while ((c = raw.readChar()) != 'M' and c != 'm') |
| 102 | + buffer += c; |
| 103 | + |
| 104 | + auto buttonData = 0, x = 0, y = 0; |
| 105 | + if (std::sscanf(buffer.c_str(), "%d;%d;%d", std::addressof(buttonData), std::addressof(x), std::addressof(y)) != 3) |
| 106 | + return std::nullopt; |
| 107 | + |
| 108 | + auto event = Event{}; |
| 109 | + event.position = Container::Size2{static_cast<std::size_t>(x - 1), static_cast<std::size_t>(y - 1)}; |
| 110 | + event.shift = (buttonData & 4) != 0; |
| 111 | + event.alt = (buttonData & 8) != 0; |
| 112 | + event.ctrl = (buttonData & 16) != 0; |
| 113 | + event.dragging = (buttonData & 32) != 0; |
| 114 | + |
| 115 | + const auto isRelease = (c == 'm'); |
| 116 | + event.pressed = not isRelease; |
| 117 | + |
| 118 | + if (auto scroll = buttonData & 64; scroll != 0) |
| 119 | + { |
| 120 | + event.button = (buttonData & 1) ? Button::ScrollDown : Button::ScrollUp; |
| 121 | + event.pressed = true; |
| 122 | + } |
| 123 | + else |
| 124 | + switch (buttonData & 3) |
| 125 | + { |
| 126 | + case 0: event.button = Button::Left; break; |
| 127 | + case 1: event.button = Button::Middle; break; |
| 128 | + case 2: event.button = Button::Right; break; |
| 129 | + case 3: event.button = Button::None; break; |
| 130 | + } |
| 131 | + |
| 132 | + return event; |
| 133 | +#elif defined(OS_WINDOWS) |
| 134 | + return std::nullopt; |
| 135 | +#endif |
| 136 | + } |
| 137 | + |
| 138 | + [[nodiscard]] inline auto getPosition() -> std::optional<Container::Size2> |
| 139 | + { |
| 140 | + if (const auto event = getEvent()) |
| 141 | + return event->position; |
| 142 | + return std::nullopt; |
| 143 | + } |
| 144 | +} |
0 commit comments