From b2476e348028ce2873796709a0e7bb90f4626076 Mon Sep 17 00:00:00 2001 From: Niellun Date: Mon, 26 May 2025 00:15:04 +0300 Subject: [PATCH] Run script on phone connect/disconnect --- settings.txt | 21 +++++++++++++++++++-- src/helper/functions.h | 8 ++++++++ src/helper/settings_base.h | 4 ++-- src/helper/ufont.h | 3 +++ src/main.cpp | 4 ++++ src/protocol.cpp | 24 +++++++++++++++++++----- src/protocol.h | 2 ++ src/settings.h | 2 ++ 8 files changed, 59 insertions(+), 9 deletions(-) diff --git a/settings.txt b/settings.txt index d7ba657..34fe25f 100644 --- a/settings.txt +++ b/settings.txt @@ -43,7 +43,7 @@ # Size of video and audio buffers. Increase if you see artifacts #queue-size = 32 -# Font size for messgaes on screen +# Font size for messgaes on screen. Set to 0 is you do not want any #font-size = 30 # USB communication protocol encryption @@ -53,4 +53,21 @@ #encryption = false # Enable automatic connection to wireless devices -#autoconnect = true \ No newline at end of file +#autoconnect = true + +# Run script or app on phone connected and disconnected. +# This script/app should be fast, otherwise it will block system. +# If you need to start application in background use scripts with fork +# +# #!/bin/bash +# MyApp & +# echo $! > app.pid +# +# #!/bin/bash +# if [ -f app.pid ]; then +# kill $(cat app.pid) +# rm app.pid +# fi +# +#on-connect-script = +#on-disconnect-script = \ No newline at end of file diff --git a/src/helper/functions.h b/src/helper/functions.h index 03af1ed..df017cc 100644 --- a/src/helper/functions.h +++ b/src/helper/functions.h @@ -29,4 +29,12 @@ inline void write_uint32_le(uint8_t *dst, uint32_t value) dst[3] = (value >> 24) & 0xFF; } +inline void execute(const char* path) { + if (!path || *path == '\0') { + throw std::invalid_argument("Program path cannot be empty"); + } + + std::system(path); +} + #endif /* SRC_HELPER_FUNCTIONS */ diff --git a/src/helper/settings_base.h b/src/helper/settings_base.h index a9a4431..2c40cce 100644 --- a/src/helper/settings_base.h +++ b/src/helper/settings_base.h @@ -13,7 +13,7 @@ class ISetting public: std::string name; virtual void parse(std::string &str) = 0; - virtual std::string asString() const = 0; + virtual std::string asString() const = 0; }; // Holds the global registry @@ -49,9 +49,9 @@ public: { try { - std::transform(str.begin(), str.end(), str.begin(), ::tolower); if constexpr (std::is_same_v) { + std::transform(str.begin(), str.end(), str.begin(), ::tolower); if (str == "1" || str == "true") value = true; else if (str == "0" || str == "false") diff --git a/src/helper/ufont.h b/src/helper/ufont.h index 1c6c4d6..9a92f9c 100644 --- a/src/helper/ufont.h +++ b/src/helper/ufont.h @@ -9,6 +9,9 @@ class UFont public: UFont(const void *font_data, int data_size, int ptsize) { + if(ptsize < 1) + return; + SDL_RWops *font_rw = SDL_RWFromConstMem(font_data, data_size); if (!font_rw) { diff --git a/src/main.cpp b/src/main.cpp index 36cddef..a68405f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -108,6 +108,10 @@ void processKey(Protocol &protocol, SDL_Keysym key) SDL_SetWindowBordered(window, fullscreen ? SDL_FALSE : SDL_TRUE); break; + case SDLK_q: + active = false; + break; + case SDLK_LEFT: protocol.sendKey(100); break; diff --git a/src/protocol.cpp b/src/protocol.cpp index 2025635..0f56850 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include Protocol::Protocol(uint16_t width, uint16_t height, uint16_t fps, uint16_t padding) : connector(padding), @@ -182,14 +182,28 @@ void Protocol::onDevice(bool connected) if (Settings::autoconnect) sendInt(CMD_CONTROL, 1002); if (Settings::encryption) - sendEncryption(); + sendEncryption(); } else { + onPhone(false); connector.setEncryption(false); } } +void Protocol::onPhone(bool connected) +{ + if (connected == phoneConnected) + return; + phoneConnected = connected; + + if (connected && Settings::onConnect.value.length() > 1) + execute(Settings::onConnect.value.c_str()); + + if (!connected && Settings::onDisconnect.value.length() > 1) + execute(Settings::onDisconnect.value.c_str()); +} + void Protocol::onData(uint32_t cmd, uint32_t length, uint8_t *data) { bool dispose = true; @@ -235,19 +249,19 @@ void Protocol::onData(uint32_t cmd, uint32_t length, uint8_t *data) } case CMD_PLUGGED: { - phoneConnected = true; + onPhone(true); break; } case CMD_UNPLUGGED: { - phoneConnected = false; + onPhone(false); break; } case CMD_ENCRYPTION: { if (length == 0) connector.setEncryption(true); - print_message(cmd, length, data); + print_message(cmd, length, data); break; } diff --git a/src/protocol.h b/src/protocol.h index 6153e45..99239c4 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -50,6 +50,8 @@ private: void print_ints(uint32_t length, uint8_t *data, uint16_t max); void print_bytes(uint32_t length, uint8_t *data, uint16_t max); + void onPhone(bool connected); + uint16_t _width; uint16_t _height; uint16_t _fps; diff --git a/src/settings.h b/src/settings.h index 7f075ec..3a8cd19 100644 --- a/src/settings.h +++ b/src/settings.h @@ -23,6 +23,8 @@ public: static inline Setting fontSize{"font-size", 30}; static inline Setting encryption{"encryption", false}; static inline Setting autoconnect{"autoconnect", true}; + static inline Setting onConnect{"on-connect-script", ""}; + static inline Setting onDisconnect{"on-disconnect-script", ""}; static void load(const std::string &filename); static void print();