From fcf4d75ea0d81f5c52ff275a37ee8c353901c8cc Mon Sep 17 00:00:00 2001 From: scorpion-26 Date: Fri, 14 Jul 2023 19:36:23 +0200 Subject: [PATCH] Only allow one instance of the bluetooth widget This works analogous to the audio flyin Fixes https://github.com/scorpion-26/gBar/issues/21 --- src/gBar.cpp | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/gBar.cpp b/src/gBar.cpp index 5f244cc..cc2439c 100644 --- a/src/gBar.cpp +++ b/src/gBar.cpp @@ -11,22 +11,24 @@ #include #include -const char* audioTmpFileOpen = "/tmp/gBar__audio"; +const char* audioTmpFilePath = "/tmp/gBar__audio"; +const char* bluetoothTmpFilePath = "/tmp/gBar__bluetooth"; + +static bool tmpFileOpen = false; -static bool flyin = false; void OpenAudioFlyin(Window& window, int32_t monitor, AudioFlyin::Type type) { - flyin = true; - if (access(audioTmpFileOpen, F_OK) != 0) + tmpFileOpen = true; + if (access(audioTmpFilePath, F_OK) != 0) { - FILE* audioTempFile = fopen(audioTmpFileOpen, "w"); + FILE* audioTempFile = fopen(audioTmpFilePath, "w"); AudioFlyin::Create(window, monitor, type); fclose(audioTempFile); } else { // Already open, close - LOG("Audio flyin already open"); + LOG("Audio flyin already open (/tmp/gBar__audio exists)! Exiting..."); exit(0); } } @@ -61,7 +63,19 @@ int main(int argc, char** argv) { if (RuntimeConfig::Get().hasBlueZ) { - BluetoothDevices::Create(window, monitor); + if (access(bluetoothTmpFilePath, F_OK) != 0) + { + tmpFileOpen = true; + FILE* bluetoothTmpFile = fopen(bluetoothTmpFilePath, "w"); + BluetoothDevices::Create(window, monitor); + fclose(bluetoothTmpFile); + } + else + { + // Already open, close + LOG("Bluetooth widget already open (/tmp/gBar__bluetooth exists)! Exiting..."); + exit(0); + } } else { @@ -78,9 +92,11 @@ int main(int argc, char** argv) window.Run(); System::FreeResources(); - if (flyin) + if (tmpFileOpen) { - remove(audioTmpFileOpen); + remove(audioTmpFilePath); + LOG("Remove bt"); + remove(bluetoothTmpFilePath); } return 0; }