| 127 | } |
| 128 | |
| 129 | auto waybar::modules::Bluetooth::update() -> void { |
| 130 | // focussed device is either: |
| 131 | // - the first device in the device_preference_ list that is connected to the |
| 132 | // current controller (if none fallback to last connected device) |
| 133 | // - it is the last device that connected to the current controller |
| 134 | if (!connected_devices_.empty()) { |
| 135 | bool preferred_device_connected = false; |
| 136 | if (!device_preference_.empty()) { |
| 137 | for (const std::string& device_alias : device_preference_) { |
| 138 | auto it = |
| 139 | std::find_if(connected_devices_.begin(), connected_devices_.end(), |
| 140 | [device_alias](auto device) { return device_alias == device.alias; }); |
| 141 | if (it != connected_devices_.end()) { |
| 142 | preferred_device_connected = true; |
| 143 | cur_focussed_device_ = *it; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | if (!preferred_device_connected) { |
| 149 | cur_focussed_device_ = connected_devices_.back(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | std::string state; |
| 154 | std::string tooltip_format; |
| 155 | if (cur_controller_) { |
| 156 | if (!cur_controller_->powered) |
| 157 | state = "off"; |
| 158 | else if (!connected_devices_.empty()) |
| 159 | state = "connected"; |
| 160 | else |
| 161 | state = "on"; |
| 162 | } else { |
| 163 | state = "no-controller"; |
| 164 | } |
| 165 | #ifdef WANT_RFKILL |
| 166 | if (rfkill_.getState()) state = "disabled"; |
| 167 | #endif |
| 168 | bool battery_available = |
| 169 | state == "connected" && cur_focussed_device_.battery_percentage.has_value(); |
| 170 | |
| 171 | #ifdef WANT_RFKILL |
| 172 | // also adds enabled icon if icon for state is not defined |
| 173 | std::vector<std::string> states = {state, rfkill_.getState() ? "disabled" : "enabled"}; |
| 174 | std::string icon = getIcon(0, states); |
| 175 | #else |
| 176 | std::string icon = getIcon(0, state); |
| 177 | #endif |
| 178 | std::string icon_label = icon; |
| 179 | std::string icon_tooltip = icon; |
| 180 | |
| 181 | if (!alt_) { |
| 182 | if (battery_available && config_["format-connected-battery"].isString()) { |
| 183 | format_ = config_["format-connected-battery"].asString(); |
| 184 | icon_label = getIcon(cur_focussed_device_.battery_percentage.value_or(0)); |
| 185 | } else if (config_["format-" + state].isString()) { |
| 186 | format_ = config_["format-" + state].asString(); |