mirror of
https://github.com/niellun/FastCarPlay.git
synced 2026-06-07 09:38:25 +02:00
Run script on phone connect/disconnect
This commit is contained in:
@@ -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 */
|
||||
|
||||
@@ -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<T, bool>)
|
||||
{
|
||||
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
|
||||
if (str == "1" || str == "true")
|
||||
value = true;
|
||||
else if (str == "0" || str == "false")
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
+19
-5
@@ -4,7 +4,7 @@
|
||||
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
#include <cctype>
|
||||
#include <cctype>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -23,6 +23,8 @@ public:
|
||||
static inline Setting<int> fontSize{"font-size", 30};
|
||||
static inline Setting<bool> encryption{"encryption", false};
|
||||
static inline Setting<bool> autoconnect{"autoconnect", true};
|
||||
static inline Setting<std::string> onConnect{"on-connect-script", ""};
|
||||
static inline Setting<std::string> onDisconnect{"on-disconnect-script", ""};
|
||||
|
||||
static void load(const std::string &filename);
|
||||
static void print();
|
||||
|
||||
Reference in New Issue
Block a user