Add icons to bluetooth widget

And add one more icon type
This commit is contained in:
scorpion-26 2023-01-15 11:09:06 +01:00
parent ddc8af62a5
commit 07b2d5e35d
4 changed files with 39 additions and 28 deletions

View file

@ -104,19 +104,7 @@ namespace Bar
{ {
if (!dev.connected) if (!dev.connected)
continue; continue;
std::string ico = ""; std::string ico = System::BTTypeToIcon(dev);
if (dev.type == "input-keyboard")
{
ico = "";
}
else if (dev.type == "input-mouse")
{
ico = "";
}
else if (dev.type == "audio-headset")
{
ico = "";
}
tooltip += dev.name + " & "; tooltip += dev.name + " & ";
btDev += ico; btDev += ico;
} }

View file

@ -48,7 +48,7 @@ namespace BluetoothDevices
button.RemoveClass("inactive"); button.RemoveClass("inactive");
state |= DeviceState::RequestConnect; state |= DeviceState::RequestConnect;
System::Connect(device.device, [&dev = device, &but = button](bool success, System::BluetoothDevice&) System::ConnectBTDevice(device.device, [&dev = device, &but = button](bool success, System::BluetoothDevice&)
{ {
deviceMutex.lock(); deviceMutex.lock();
if (!success) if (!success)
@ -66,7 +66,7 @@ namespace BluetoothDevices
button.RemoveClass("active"); button.RemoveClass("active");
state |= DeviceState::RequestDisconnect; state |= DeviceState::RequestDisconnect;
System::Disconnect(device.device, [&dev = device, &but = button](bool success, System::BluetoothDevice&) System::DisconnectBTDevice(device.device, [&dev = device, &but = button](bool success, System::BluetoothDevice&)
{ {
deviceMutex.lock(); deviceMutex.lock();
if (!success) if (!success)
@ -84,7 +84,7 @@ namespace BluetoothDevices
{ {
if (device.device.name.size()) if (device.device.name.size())
{ {
button.SetText(device.device.name); button.SetText(System::BTTypeToIcon(device.device) + device.device.name);
} }
else else
{ {
@ -239,13 +239,13 @@ namespace BluetoothDevices
{ {
button.AddClass("active"); button.AddClass("active");
button.RemoveClass("inactive"); button.RemoveClass("inactive");
System::StartScan(); System::StartBTScan();
} }
else else
{ {
button.AddClass("inactive"); button.AddClass("inactive");
button.RemoveClass("active"); button.RemoveClass("active");
System::StopScan(); System::StopBTScan();
} }
} }
} }

View file

@ -264,12 +264,12 @@ namespace System
} }
static Process btctlProcess{-1}; static Process btctlProcess{-1};
void StartScan() void StartBTScan()
{ {
StopScan(); StopBTScan();
btctlProcess = OpenProcess("/bin/sh", "/bin/sh", "-c", "bluetoothctl scan on", NULL); btctlProcess = OpenProcess("/bin/sh", "/bin/sh", "-c", "bluetoothctl scan on", NULL);
} }
void StopScan() void StopBTScan()
{ {
if (btctlProcess.pid != -1) if (btctlProcess.pid != -1)
{ {
@ -279,7 +279,7 @@ namespace System
} }
} }
void Connect(BluetoothDevice& device, std::function<void(bool, BluetoothDevice&)> onFinish) void ConnectBTDevice(BluetoothDevice& device, std::function<void(bool, BluetoothDevice&)> onFinish)
{ {
auto thread = [&, mac = device.mac, onFinish]() auto thread = [&, mac = device.mac, onFinish]()
{ {
@ -308,7 +308,7 @@ namespace System
std::thread worker(thread); std::thread worker(thread);
worker.detach(); worker.detach();
} }
void Disconnect(BluetoothDevice& device, std::function<void(bool, BluetoothDevice&)> onFinish) void DisconnectBTDevice(BluetoothDevice& device, std::function<void(bool, BluetoothDevice&)> onFinish)
{ {
auto thread = [&, mac = device.mac, onFinish]() auto thread = [&, mac = device.mac, onFinish]()
{ {
@ -332,6 +332,27 @@ namespace System
{ {
OpenProcess("/bin/sh", "/bin/sh", "-c", "gBar bluetooth"); OpenProcess("/bin/sh", "/bin/sh", "-c", "gBar bluetooth");
} }
std::string BTTypeToIcon(const BluetoothDevice& dev)
{
if (dev.type == "input-keyboard")
{
return "";
}
else if (dev.type == "input-mouse")
{
return "";
}
else if (dev.type == "audio-headset")
{
return "";
}
else if (dev.type == "input-gaming")
{
return "";
}
return "";
}
#endif #endif
AudioInfo GetAudioInfo() AudioInfo GetAudioInfo()
@ -417,7 +438,7 @@ namespace System
#endif #endif
PulseAudio::Shutdown(); PulseAudio::Shutdown();
#ifdef HAS_BLUEZ #ifdef HAS_BLUEZ
StopScan(); StopBTScan();
#endif #endif
} }
} }

View file

@ -57,14 +57,16 @@ namespace System
std::vector<BluetoothDevice> devices; std::vector<BluetoothDevice> devices;
}; };
BluetoothInfo GetBluetoothInfo(); BluetoothInfo GetBluetoothInfo();
void StartScan(); void StartBTScan();
void StopScan(); void StopBTScan();
// MT functions, callback, is from different thread // MT functions, callback, is from different thread
void Connect(BluetoothDevice& device, std::function<void(bool, BluetoothDevice&)> onFinish); void ConnectBTDevice(BluetoothDevice& device, std::function<void(bool, BluetoothDevice&)> onFinish);
void Disconnect(BluetoothDevice& device, std::function<void(bool, BluetoothDevice&)> onFinish); void DisconnectBTDevice(BluetoothDevice& device, std::function<void(bool, BluetoothDevice&)> onFinish);
void OpenBTWidget(); void OpenBTWidget();
std::string BTTypeToIcon(const BluetoothDevice& dev);
#endif #endif
struct AudioInfo struct AudioInfo