refactor: adapter renamed to controller

Since it is also called controller in bluetoothctl
This commit is contained in:
Daan Goossens 2022-05-06 00:38:04 +02:00
parent 13df878bc3
commit 954bed3f5e
3 changed files with 68 additions and 64 deletions

View File

@ -12,7 +12,7 @@
namespace waybar::modules { namespace waybar::modules {
class Bluetooth : public ALabel { class Bluetooth : public ALabel {
struct AdapterInfo struct ControllerInfo
{ {
std::string path; std::string path;
std::string address; std::string address;
@ -29,7 +29,7 @@ class Bluetooth : public ALabel {
struct DeviceInfo struct DeviceInfo
{ {
std::string path; std::string path;
std::string paired_adapter; std::string paired_controller;
std::string address; std::string address;
std::string address_type; std::string address_type;
// std::optional<std::string> name; // just use alias instead // std::optional<std::string> name; // just use alias instead
@ -55,9 +55,9 @@ class Bluetooth : public ALabel {
auto getDeviceBatteryPercentage(GDBusObject*) -> std::optional<unsigned char>; auto getDeviceBatteryPercentage(GDBusObject*) -> std::optional<unsigned char>;
auto getDeviceProperties(GDBusObject*, DeviceInfo&) -> bool; auto getDeviceProperties(GDBusObject*, DeviceInfo&) -> bool;
auto getAdapterProperties(GDBusObject*, AdapterInfo&) -> bool; auto getControllerProperties(GDBusObject*, ControllerInfo&) -> bool;
auto findCurAdapter(AdapterInfo&) -> bool; auto findCurController(ControllerInfo&) -> bool;
auto findConnectedDevices(const std::string&, std::vector<DeviceInfo>&) -> void; auto findConnectedDevices(const std::string&, std::vector<DeviceInfo>&) -> void;
@ -67,7 +67,7 @@ class Bluetooth : public ALabel {
const std::unique_ptr<GDBusObjectManager, void (*)(GDBusObjectManager*)> manager_; const std::unique_ptr<GDBusObjectManager, void (*)(GDBusObjectManager*)> manager_;
std::string state_; std::string state_;
AdapterInfo cur_adapter_; ControllerInfo cur_controller_;
std::vector<DeviceInfo> connected_devices_; std::vector<DeviceInfo> connected_devices_;
DeviceInfo cur_focussed_device_; DeviceInfo cur_focussed_device_;
std::string device_enumerate_; std::string device_enumerate_;

View File

@ -6,15 +6,18 @@ waybar - bluetooth module
# DESCRIPTION # DESCRIPTION
The *bluetooth* module displays information about the bluetooth adapter and its connections. The *bluetooth* module displays information about the bluetooth controller and its connections.
# CONFIGURATION # CONFIGURATION
Addressed by *bluetooth* Addressed by *bluetooth*
*adapter-alias*: ++ *controller*: ++
typeof: string ++ typeof: string ++
Use the adapter with the defined alias (name). Otherwise select a random adapter to display. Recommended to define when there is more than 1 adapter available to the system (use ```bluetoothctl show``` to show all available adapters). Use the controller with the defined alias (name). Otherwise select a
random controller to display. Recommended to define when there is more
than 1 controller available to the system (use ```bluetoothctl show```
to show all available controller).
*format-device-preference*: ++ *format-device-preference*: ++
typeof: array ++ typeof: array ++
@ -27,19 +30,19 @@ Addressed by *bluetooth*
*format-disabled*: ++ *format-disabled*: ++
typeof: string ++ typeof: string ++
This format is used when the displayed adapter is disabled. This format is used when the displayed controller is disabled.
*format-off*: ++ *format-off*: ++
typeof: string ++ typeof: string ++
This format is used when the displayed adapter is turned off. This format is used when the displayed controller is turned off.
*format-on*: ++ *format-on*: ++
typeof: string ++ typeof: string ++
This format is used when the displayed adapter is turned on with no devices connected. This format is used when the displayed controller is turned on with no devices connected.
*format-connected*: ++ *format-connected*: ++
typeof: string ++ typeof: string ++
This format is used when the displayed adapter is connected to at least 1 device. This format is used when the displayed controller is connected to at least 1 device.
*format-connected-battery*: ++ *format-connected-battery*: ++
typeof: string ++ typeof: string ++
@ -96,19 +99,19 @@ Addressed by *bluetooth*
*tooltip-format-disabled*: ++ *tooltip-format-disabled*: ++
typeof: string ++ typeof: string ++
This format is used when the displayed adapter is disabled. This format is used when the displayed controller is disabled.
*tooltip-format-off*: ++ *tooltip-format-off*: ++
typeof: string ++ typeof: string ++
This format is used when the displayed adapter is turned off. This format is used when the displayed controller is turned off.
*tooltip-format-on*: ++ *tooltip-format-on*: ++
typeof: string ++ typeof: string ++
This format is used when the displayed adapter is turned on with no devices connected. This format is used when the displayed controller is turned on with no devices connected.
*tooltip-format-connected*: ++ *tooltip-format-connected*: ++
typeof: string ++ typeof: string ++
This format is used when the displayed adapter is connected to at least 1 device. This format is used when the displayed controller is connected to at least 1 device.
*tooltip-format-connected-battery*: ++ *tooltip-format-connected-battery*: ++
typeof: string ++ typeof: string ++
@ -127,13 +130,14 @@ Addressed by *bluetooth*
*{status}*: Status of the bluetooth device. *{status}*: Status of the bluetooth device.
*{num_connections}*: Number of connections the selected adapter has. *{num_connections}*: Number of connections the selected controller has.
*{adapter_address}*: Address of the selected adapter. *{controller_address}*: Address of the selected controller.
*{adapter_address_type}*: Address type of the selected adapter. *{controller_address_type}*: Address type of the selected controller.
*{adapter_alias}*: Alias of the selected adapter. By default equal to the *adapter_name* but can be changed by the user when there are conflicts. *{controller_alias}*: Alias of the selected controller. By default equal to the
*controller_name* but can be changed by the user when there are conflicts.
*{device_address}*: Address of the current selected selected connected device. *{device_address}*: Address of the current selected selected connected device.
@ -149,11 +153,11 @@ Addressed by *bluetooth*
``` ```
"bluetooth": { "bluetooth": {
// "adapter-alias": "adapter1", // specify the adapter alias (name) if there are more than 1 on the system // "controller": "controller1", // specify the controller alias (name) if there are more than 1 on the system
"format": " {status}", "format": " {status}",
"format-connected": " {num_connections} connected", "format-connected": " {num_connections} connected",
"tooltip-format": "{adapter_alias}\t{adapter_address}", "tooltip-format": "{controller_alias}\t{controller_address}",
"tooltip-format-connected": "{adapter_alias}\t{adapter_address}\n\n{device_enumerate}", "tooltip-format-connected": "{controller_alias}\t{controller_address}\n\n{device_enumerate}",
"tooltip-format-enumerate-connected": "{device_alias}\t{device_address}", "tooltip-format-enumerate-connected": "{device_alias}\t{device_address}",
"tooltip-format-enumerate-connected-battery": "{device_alias}\t{device_address}\t{device_battery_percentage}%" "tooltip-format-enumerate-connected-battery": "{device_alias}\t{device_address}\t{device_battery_percentage}%"
} }
@ -165,7 +169,7 @@ Addressed by *bluetooth*
"format-connected": " {device_alias}", "format-connected": " {device_alias}",
"format-connected-battery": " {device_alias} {device_battery_percentage}%", "format-connected-battery": " {device_alias} {device_battery_percentage}%",
// "format-device-preference": [ "alias1", "alias2" ], // preference list deciding which device to show in format-connected format-connected-battery // "format-device-preference": [ "alias1", "alias2" ], // preference list deciding which device to show in format-connected format-connected-battery
"tooltip-format": "{adapter_alias}\t{adapter_address}\n\n{num_connections} connected\n\n{device_enumerate}", "tooltip-format": "{controller_alias}\t{controller_address}\n\n{num_connections} connected\n\n{device_enumerate}",
"tooltip-format-enumerate-connected": "{device_alias}\t{device_address}", "tooltip-format-enumerate-connected": "{device_alias}\t{device_address}",
"tooltip-format-enumerate-connected-battery": "{device_alias}\t{device_address}\t{device_battery_percentage}%" "tooltip-format-enumerate-connected-battery": "{device_alias}\t{device_address}\t{device_battery_percentage}%"
} }

View File

@ -98,17 +98,17 @@ waybar::modules::Bluetooth::Bluetooth(const std::string& id, const Json::Value&
[](auto x){ return x.asString(); }); [](auto x){ return x.asString(); });
} }
// NOTE: assumption made that the adapter that is selcected stays unchanged // NOTE: assumption made that the controller that is selcected stays unchanged
// for duration of the module // for duration of the module
if (!findCurAdapter(cur_adapter_)) { if (!findCurController(cur_controller_)) {
if (config_["adapter-alias"].isString()) { if (config_["controller-alias"].isString()) {
spdlog::error("find_cur_adapter() failed: no bluetooth adapter found with alias '{}'", config_["adapter-alias"].asString()); spdlog::error("findCurController() failed: no bluetooth controller found with alias '{}'", config_["controller-alias"].asString());
} else { } else {
spdlog::error("find_cur_adapter() failed: no bluetooth adapter found"); spdlog::error("findCurController() failed: no bluetooth controller found");
} }
return; return;
} }
findConnectedDevices(cur_adapter_.path, connected_devices_); findConnectedDevices(cur_controller_.path, connected_devices_);
g_signal_connect(manager_.get(), "interface-proxy-properties-changed", G_CALLBACK(onInterfaceProxyPropertiesChanged), this); g_signal_connect(manager_.get(), "interface-proxy-properties-changed", G_CALLBACK(onInterfaceProxyPropertiesChanged), this);
g_signal_connect(manager_.get(), "interface-added", G_CALLBACK(onInterfaceAddedOrRemoved), this); g_signal_connect(manager_.get(), "interface-added", G_CALLBACK(onInterfaceAddedOrRemoved), this);
@ -123,8 +123,8 @@ waybar::modules::Bluetooth::Bluetooth(const std::string& id, const Json::Value&
auto waybar::modules::Bluetooth::update() -> void { auto waybar::modules::Bluetooth::update() -> void {
// focussed device is either: // focussed device is either:
// - the first device in the device_preference_ list that is connected to the // - the first device in the device_preference_ list that is connected to the
// current adapter (if none fallback to last connected device) // current controller (if none fallback to last connected device)
// - it is the last device that connected to the current adapter // - it is the last device that connected to the current controller
if (!connected_devices_.empty()) { if (!connected_devices_.empty()) {
bool preferred_device_connected = false; bool preferred_device_connected = false;
if (!device_preference_.empty()) { if (!device_preference_.empty()) {
@ -144,7 +144,7 @@ auto waybar::modules::Bluetooth::update() -> void {
std::string state; std::string state;
std::string tooltip_format; std::string tooltip_format;
if (!cur_adapter_.powered) if (!cur_controller_.powered)
state = "off"; state = "off";
else if (!connected_devices_.empty()) else if (!connected_devices_.empty())
state = "connected"; state = "connected";
@ -179,9 +179,9 @@ auto waybar::modules::Bluetooth::update() -> void {
label_.get_style_context()->remove_class(style_class); label_.get_style_context()->remove_class(style_class);
} }
}; };
update_style_context("discoverable", cur_adapter_.discoverable); update_style_context("discoverable", cur_controller_.discoverable);
update_style_context("discovering", cur_adapter_.discovering); update_style_context("discovering", cur_controller_.discovering);
update_style_context("pairable", cur_adapter_.pairable); update_style_context("pairable", cur_controller_.pairable);
if (!state_.empty()) { if (!state_.empty()) {
update_style_context(state_, false); update_style_context(state_, false);
} }
@ -191,9 +191,9 @@ auto waybar::modules::Bluetooth::update() -> void {
label_.set_markup(fmt::format(format_, label_.set_markup(fmt::format(format_,
fmt::arg("status", state_), fmt::arg("status", state_),
fmt::arg("num_connections", connected_devices_.size()), fmt::arg("num_connections", connected_devices_.size()),
fmt::arg("adapter_address", cur_adapter_.address), fmt::arg("controller_address", cur_controller_.address),
fmt::arg("adapter_address_type", cur_adapter_.address_type), fmt::arg("controller_address_type", cur_controller_.address_type),
fmt::arg("adapter_alias", cur_adapter_.alias), fmt::arg("controller_alias", cur_controller_.alias),
fmt::arg("device_address", cur_focussed_device_.address), fmt::arg("device_address", cur_focussed_device_.address),
fmt::arg("device_address_type", cur_focussed_device_.address_type), fmt::arg("device_address_type", cur_focussed_device_.address_type),
fmt::arg("device_alias", cur_focussed_device_.alias), fmt::arg("device_alias", cur_focussed_device_.alias),
@ -225,9 +225,9 @@ auto waybar::modules::Bluetooth::update() -> void {
label_.set_tooltip_text(fmt::format(tooltip_format, label_.set_tooltip_text(fmt::format(tooltip_format,
fmt::arg("status", state_), fmt::arg("status", state_),
fmt::arg("num_connections", connected_devices_.size()), fmt::arg("num_connections", connected_devices_.size()),
fmt::arg("adapter_address", cur_adapter_.address), fmt::arg("controller_address", cur_controller_.address),
fmt::arg("adapter_address_type", cur_adapter_.address_type), fmt::arg("controller_address_type", cur_controller_.address_type),
fmt::arg("adapter_alias", cur_adapter_.alias), fmt::arg("controller_alias", cur_controller_.alias),
fmt::arg("device_address", cur_focussed_device_.address), fmt::arg("device_address", cur_focussed_device_.address),
fmt::arg("device_address_type", cur_focussed_device_.address_type), fmt::arg("device_address_type", cur_focussed_device_.address_type),
fmt::arg("device_alias", cur_focussed_device_.alias), fmt::arg("device_alias", cur_focussed_device_.alias),
@ -269,8 +269,8 @@ auto waybar::modules::Bluetooth::onInterfaceProxyPropertiesChanged(GDBusObjectMa
Bluetooth* bt = static_cast<Bluetooth*>(user_data); Bluetooth* bt = static_cast<Bluetooth*>(user_data);
if (interface_name == "org.bluez.Adapter1") { if (interface_name == "org.bluez.Adapter1") {
if (object_path == bt->cur_adapter_.path) { if (object_path == bt->cur_controller_.path) {
bt->getAdapterProperties(G_DBUS_OBJECT(object_proxy), bt->cur_adapter_); bt->getControllerProperties(G_DBUS_OBJECT(object_proxy), bt->cur_controller_);
bt->dp.emit(); bt->dp.emit();
} }
} else if (interface_name == "org.bluez.Device1" || } else if (interface_name == "org.bluez.Device1" ||
@ -310,7 +310,7 @@ auto waybar::modules::Bluetooth::getDeviceProperties(GDBusObject* object, Device
if (proxy_device != NULL) { if (proxy_device != NULL) {
device_info.path = g_dbus_object_get_object_path(object); device_info.path = g_dbus_object_get_object_path(object);
device_info.paired_adapter = getStringProperty(proxy_device, "Adapter"); device_info.paired_controller = getStringProperty(proxy_device, "Adapter");
device_info.address = getStringProperty(proxy_device, "Address"); device_info.address = getStringProperty(proxy_device, "Address");
device_info.address_type = getStringProperty(proxy_device, "AddressType"); device_info.address_type = getStringProperty(proxy_device, "AddressType");
device_info.alias = getStringProperty(proxy_device, "Alias"); device_info.alias = getStringProperty(proxy_device, "Alias");
@ -330,49 +330,49 @@ auto waybar::modules::Bluetooth::getDeviceProperties(GDBusObject* object, Device
return false; return false;
} }
auto waybar::modules::Bluetooth::getAdapterProperties(GDBusObject* object, AdapterInfo& adapter_info) -> bool { auto waybar::modules::Bluetooth::getControllerProperties(GDBusObject* object, ControllerInfo& controller_info) -> bool {
GDBusProxy* proxy_adapter = G_DBUS_PROXY(g_dbus_object_get_interface(object, "org.bluez.Adapter1")); GDBusProxy* proxy_controller = G_DBUS_PROXY(g_dbus_object_get_interface(object, "org.bluez.Adapter1"));
if (proxy_adapter != NULL) { if (proxy_controller != NULL) {
adapter_info.path = g_dbus_object_get_object_path(object); controller_info.path = g_dbus_object_get_object_path(object);
adapter_info.address = getStringProperty(proxy_adapter, "Address"); controller_info.address = getStringProperty(proxy_controller, "Address");
adapter_info.address_type = getStringProperty(proxy_adapter, "AddressType"); controller_info.address_type = getStringProperty(proxy_controller, "AddressType");
adapter_info.alias = getStringProperty(proxy_adapter, "Alias"); controller_info.alias = getStringProperty(proxy_controller, "Alias");
adapter_info.powered = getBoolProperty(proxy_adapter, "Powered"); controller_info.powered = getBoolProperty(proxy_controller, "Powered");
adapter_info.discoverable = getBoolProperty(proxy_adapter, "Discoverable"); controller_info.discoverable = getBoolProperty(proxy_controller, "Discoverable");
adapter_info.pairable = getBoolProperty(proxy_adapter, "Pairable"); controller_info.pairable = getBoolProperty(proxy_controller, "Pairable");
adapter_info.discovering = getBoolProperty(proxy_adapter, "Discovering"); controller_info.discovering = getBoolProperty(proxy_controller, "Discovering");
g_object_unref(proxy_adapter); g_object_unref(proxy_controller);
return true; return true;
} }
return false; return false;
} }
auto waybar::modules::Bluetooth::findCurAdapter(AdapterInfo& adapter_info) -> bool { auto waybar::modules::Bluetooth::findCurController(ControllerInfo& controller_info) -> bool {
bool found_adapter = false; bool found_controller = false;
GList* objects = g_dbus_object_manager_get_objects(manager_.get()); GList* objects = g_dbus_object_manager_get_objects(manager_.get());
for (GList* l = objects; l != NULL; l = l->next) { for (GList* l = objects; l != NULL; l = l->next) {
GDBusObject* object = G_DBUS_OBJECT(l->data); GDBusObject* object = G_DBUS_OBJECT(l->data);
if (getAdapterProperties(object, adapter_info) && (!config_["adapter-alias"].isString() || config_["adapter-alias"].asString() == adapter_info.alias)) { if (getControllerProperties(object, controller_info) && (!config_["controller-alias"].isString() || config_["controller-alias"].asString() == controller_info.alias)) {
found_adapter = true; found_controller = true;
break; break;
} }
} }
g_list_free_full(objects, g_object_unref); g_list_free_full(objects, g_object_unref);
return found_adapter; return found_controller;
} }
auto waybar::modules::Bluetooth::findConnectedDevices(const std::string& cur_adapter_path, std::vector<DeviceInfo>& connected_devices) -> void { auto waybar::modules::Bluetooth::findConnectedDevices(const std::string& cur_controller_path, std::vector<DeviceInfo>& connected_devices) -> void {
GList* objects = g_dbus_object_manager_get_objects(manager_.get()); GList* objects = g_dbus_object_manager_get_objects(manager_.get());
for (GList* l = objects; l != NULL; l = l->next) for (GList* l = objects; l != NULL; l = l->next)
{ {
GDBusObject* object = G_DBUS_OBJECT(l->data); GDBusObject* object = G_DBUS_OBJECT(l->data);
DeviceInfo device; DeviceInfo device;
if (getDeviceProperties(object, device) && device.connected && device.paired_adapter == cur_adapter_.path) { if (getDeviceProperties(object, device) && device.connected && device.paired_controller == cur_controller_.path) {
connected_devices.push_back(device); connected_devices.push_back(device);
} }
} }