Only allow one instance of the bluetooth widget

This works analogous to the audio flyin
Fixes https://github.com/scorpion-26/gBar/issues/21
This commit is contained in:
scorpion-26 2023-07-14 19:36:23 +02:00
parent 18f3e123ed
commit fcf4d75ea0

View file

@ -11,22 +11,24 @@
#include <cstdio> #include <cstdio>
#include <unistd.h> #include <unistd.h>
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) void OpenAudioFlyin(Window& window, int32_t monitor, AudioFlyin::Type type)
{ {
flyin = true; tmpFileOpen = true;
if (access(audioTmpFileOpen, F_OK) != 0) if (access(audioTmpFilePath, F_OK) != 0)
{ {
FILE* audioTempFile = fopen(audioTmpFileOpen, "w"); FILE* audioTempFile = fopen(audioTmpFilePath, "w");
AudioFlyin::Create(window, monitor, type); AudioFlyin::Create(window, monitor, type);
fclose(audioTempFile); fclose(audioTempFile);
} }
else else
{ {
// Already open, close // Already open, close
LOG("Audio flyin already open"); LOG("Audio flyin already open (/tmp/gBar__audio exists)! Exiting...");
exit(0); exit(0);
} }
} }
@ -61,7 +63,19 @@ int main(int argc, char** argv)
{ {
if (RuntimeConfig::Get().hasBlueZ) 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 else
{ {
@ -78,9 +92,11 @@ int main(int argc, char** argv)
window.Run(); window.Run();
System::FreeResources(); System::FreeResources();
if (flyin) if (tmpFileOpen)
{ {
remove(audioTmpFileOpen); remove(audioTmpFilePath);
LOG("Remove bt");
remove(bluetoothTmpFilePath);
} }
return 0; return 0;
} }