From a5214f942e7e0df1d2362ba9591a6ec40688a4cb Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sun, 3 May 2026 00:15:30 -0400 Subject: [PATCH] Refactor to a control api. --- meson.build | 1 + src/bezos_ctl.cpp | 3 ++- src/control.cpp | 29 +++++++++++++++++++++++++++++ src/control.hpp | 23 +++++++++++++++++++++++ src/main.cpp | 34 +++++----------------------------- 5 files changed, 60 insertions(+), 30 deletions(-) create mode 100644 src/control.cpp create mode 100644 src/control.hpp diff --git a/meson.build b/meson.build index 633fb2d..a54ab49 100644 --- a/meson.build +++ b/meson.build @@ -95,6 +95,7 @@ sources = [ 'src/slides_ui.cpp', 'src/parser.cpp', 'src/backend.cpp', + 'src/control.cpp', ] tests = [ diff --git a/src/bezos_ctl.cpp b/src/bezos_ctl.cpp index 3855f8c..54c6be5 100644 --- a/src/bezos_ctl.cpp +++ b/src/bezos_ctl.cpp @@ -3,6 +3,7 @@ #include #include #include +#include "control.hpp" using namespace std::chrono_literals; @@ -18,7 +19,7 @@ struct Options { void request_focus(Options& options) { fmt::println("Moving control window front: port={}", options.port); sf::UdpSocket sock; - uint32_t cmd = 2; + uint32_t cmd = uint32_t(CtrlCommand::FOCUS); auto localhost = sf::IpAddress::getLocalAddress(); auto result = sock.send(&cmd, sizeof cmd, *localhost, options.port); } diff --git a/src/control.cpp b/src/control.cpp new file mode 100644 index 0000000..7d35f4d --- /dev/null +++ b/src/control.cpp @@ -0,0 +1,29 @@ +#include "control.hpp" +#include "constants.hpp" +#include +#include +#include + +void CtrlSocket::listen() { + auto status = $socket.bind(port); + + if(status == sf::Socket::Status::Error) { + fmt::println("Error binding UDP port {}", port); + } + + $socket.setBlocking(false); + + fmt::println("Focus listener on port {}", port); +} + +CtrlCommand CtrlSocket::receive() { + uint32_t cmd = uint32_t(CtrlCommand::NONE); + size_t received = 0; + std::optional sender; + + if($socket.receive(&cmd, sizeof cmd, received, sender, port) == sf::Socket::Status::Done) { + fmt::println("sender {} sent {}", sender->toString(), cmd); + } + + return static_cast(cmd); +} diff --git a/src/control.hpp b/src/control.hpp new file mode 100644 index 0000000..329ed6e --- /dev/null +++ b/src/control.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include +#include + +enum class CtrlCommand { + FOCUS, + FULL_SCREEN, + MOVE_LEFT, + MOVE_RIGHT, + NEXT_SLIDE, + PREV_SLIDE, + QUIT, + NONE +}; + +struct CtrlSocket { + unsigned short port=9898; + sf::UdpSocket $socket{}; + + void listen(); + CtrlCommand receive(); +}; diff --git a/src/main.cpp b/src/main.cpp index df370ff..97b3300 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,9 +9,9 @@ #include #include #include -#include #include #include +#include "control.hpp" using namespace std::chrono_literals; @@ -73,22 +73,6 @@ void print_usage() { fmt::println("USAGE: bezos [-p PORT] [-hf] -d deck.md"); } -std::optional listen_control(Options& opts) { - sf::UdpSocket socket; - auto status = socket.bind(opts.port); - - if(status == sf::Socket::Status::Error) { - fmt::println("Failed to bind port"); - return std::nullopt; - } - - socket.setBlocking(false); - - fmt::println("Focus listener on port {}", opts.port); - - return socket; -} - Options parse_options(int argc, char* argv[]) { int opt = 0; Options result; @@ -130,12 +114,6 @@ int main(int argc, char *argv[]) { return 1; } - auto control = listen_control(options); - - if(control == std::nullopt) { - fmt::println("Error binding UDP port {}", options.port); - } - auto& modes = sf::VideoMode::getFullscreenModes(); auto screen_mode = std::find_if(modes.begin(), modes.end(), [=](const auto& a) -> bool { return a.size.x == WINDOW_WIDTH && a.size.y == WINDOW_HEIGHT; @@ -163,18 +141,16 @@ int main(int argc, char *argv[]) { control_ui.init(); control_ui.full_screen(false); - dbc::check(control_ui.$status != nullptr, "bad ptr"); + CtrlSocket ctrl_sock{options.port}; + ctrl_sock.listen(); ChangeDetector slides_reloader{options.deck_file}; ChangeDetector layout_reloader{slides->$deck->config["layouts"]}; while(controller.isOpen()) { - uint32_t cmd = 0; - std::size_t received = 0; - std::optional sender; + auto command = ctrl_sock.receive(); - if(control->receive(&cmd, sizeof cmd, received, sender, options.port) == sf::Socket::Status::Done) { - fmt::println("sender {} sent {}", sender->toString(), cmd); + if(command == CtrlCommand::FOCUS) { sf::Mouse::setPosition({WINDOW_WIDTH - 100, WINDOW_HEIGHT - 100}, presenter); }