util: clang-tidy

This commit is contained in:
Austin Horstman 2024-07-02 10:30:04 -05:00
parent 14c3235c12
commit d66685a3aa
No known key found for this signature in database
9 changed files with 54 additions and 48 deletions

View File

@ -56,10 +56,10 @@ class BacklightBackend {
void set_previous_best_device(const BacklightDevice *device);
void set_brightness(std::string preferred_device, ChangeType change_type, double step);
void set_brightness(const std::string &preferred_device, ChangeType change_type, double step);
void set_scaled_brightness(std::string preferred_device, int brightness);
int get_scaled_brightness(std::string preferred_device);
void set_scaled_brightness(const std::string &preferred_device, int brightness);
int get_scaled_brightness(const std::string &preferred_device);
bool is_login_proxy_initialized() const { return static_cast<bool>(login_proxy_); }
@ -70,7 +70,7 @@ class BacklightBackend {
std::mutex udev_thread_mutex_;
private:
void set_brightness_internal(std::string device_name, int brightness, int max_brightness);
void set_brightness_internal(const std::string &device_name, int brightness, int max_brightness);
std::function<void()> on_updated_cb_;
std::chrono::milliseconds polling_interval_;

View File

@ -5,6 +5,7 @@
#include <functional>
#include <regex>
#include <string>
#include <utility>
namespace waybar::util {
@ -17,7 +18,7 @@ struct Rule {
// See https://en.cppreference.com/w/cpp/compiler_support/20 "Parenthesized initialization of
// aggregates"
Rule(std::regex rule, std::string repr, int priority)
: rule(rule), repr(repr), priority(priority) {}
: rule(std::move(rule)), repr(std::move(repr)), priority(priority) {}
};
int default_priority_function(std::string& key);
@ -40,8 +41,9 @@ class RegexCollection {
public:
RegexCollection() = default;
RegexCollection(const Json::Value& map, std::string default_repr = "",
std::function<int(std::string&)> priority_function = default_priority_function);
RegexCollection(
const Json::Value& map, std::string default_repr = "",
const std::function<int(std::string&)>& priority_function = default_priority_function);
~RegexCollection() = default;
std::string& get(std::string& value, bool& matched_any);

View File

@ -8,6 +8,7 @@
#include <algorithm>
#include <cmath>
#include <stdexcept>
#include <utility>
namespace waybar::util {
@ -15,13 +16,11 @@ AudioBackend::AudioBackend(std::function<void()> on_updated_cb, private_construc
: mainloop_(nullptr),
mainloop_api_(nullptr),
context_(nullptr),
sink_idx_(0),
volume_(0),
muted_(false),
source_idx_(0),
source_volume_(0),
source_muted_(false),
on_updated_cb_(on_updated_cb) {
on_updated_cb_(std::move(on_updated_cb)) {
mainloop_ = pa_threaded_mainloop_new();
if (mainloop_ == nullptr) {
throw std::runtime_error("pa_mainloop_new() failed.");
@ -66,7 +65,7 @@ void AudioBackend::connectContext() {
}
void AudioBackend::contextStateCb(pa_context *c, void *data) {
auto backend = static_cast<AudioBackend *>(data);
auto *backend = static_cast<AudioBackend *>(data);
switch (pa_context_get_state(c)) {
case PA_CONTEXT_TERMINATED:
backend->mainloop_api_->quit(backend->mainloop_api_, 0);
@ -127,7 +126,7 @@ void AudioBackend::subscribeCb(pa_context *context, pa_subscription_event_type_t
* Called in response to a volume change request
*/
void AudioBackend::volumeModifyCb(pa_context *c, int success, void *data) {
auto backend = static_cast<AudioBackend *>(data);
auto *backend = static_cast<AudioBackend *>(data);
if (success != 0) {
pa_context_get_sink_info_by_index(backend->context_, backend->sink_idx_, sinkInfoCb, data);
}
@ -140,7 +139,7 @@ void AudioBackend::sinkInfoCb(pa_context * /*context*/, const pa_sink_info *i, i
void *data) {
if (i == nullptr) return;
auto backend = static_cast<AudioBackend *>(data);
auto *backend = static_cast<AudioBackend *>(data);
if (!backend->ignored_sinks_.empty()) {
for (const auto &ignored_sink : backend->ignored_sinks_) {
@ -151,11 +150,7 @@ void AudioBackend::sinkInfoCb(pa_context * /*context*/, const pa_sink_info *i, i
}
if (backend->current_sink_name_ == i->name) {
if (i->state != PA_SINK_RUNNING) {
backend->current_sink_running_ = false;
} else {
backend->current_sink_running_ = true;
}
backend->current_sink_running_ = i->state == PA_SINK_RUNNING;
}
if (!backend->current_sink_running_ && i->state == PA_SINK_RUNNING) {
@ -173,7 +168,7 @@ void AudioBackend::sinkInfoCb(pa_context * /*context*/, const pa_sink_info *i, i
backend->desc_ = i->description;
backend->monitor_ = i->monitor_source_name;
backend->port_name_ = i->active_port != nullptr ? i->active_port->name : "Unknown";
if (auto ff = pa_proplist_gets(i->proplist, PA_PROP_DEVICE_FORM_FACTOR)) {
if (const auto *ff = pa_proplist_gets(i->proplist, PA_PROP_DEVICE_FORM_FACTOR)) {
backend->form_factor_ = ff;
} else {
backend->form_factor_ = "";
@ -187,7 +182,7 @@ void AudioBackend::sinkInfoCb(pa_context * /*context*/, const pa_sink_info *i, i
*/
void AudioBackend::sourceInfoCb(pa_context * /*context*/, const pa_source_info *i, int /*eol*/,
void *data) {
auto backend = static_cast<AudioBackend *>(data);
auto *backend = static_cast<AudioBackend *>(data);
if (i != nullptr && backend->default_source_name_ == i->name) {
auto source_volume = static_cast<float>(pa_cvolume_avg(&(i->volume))) / float{PA_VOLUME_NORM};
backend->source_volume_ = std::round(source_volume * 100.0F);
@ -204,7 +199,7 @@ void AudioBackend::sourceInfoCb(pa_context * /*context*/, const pa_source_info *
* used to find the default PulseAudio sink.
*/
void AudioBackend::serverInfoCb(pa_context *context, const pa_server_info *i, void *data) {
auto backend = static_cast<AudioBackend *>(data);
auto *backend = static_cast<AudioBackend *>(data);
backend->current_sink_name_ = i->default_sink_name;
backend->default_source_name_ = i->default_source_name;
@ -253,22 +248,26 @@ void AudioBackend::changeVolume(ChangeType change_type, double step, uint16_t ma
void AudioBackend::toggleSinkMute() {
muted_ = !muted_;
pa_context_set_sink_mute_by_index(context_, sink_idx_, muted_, nullptr, nullptr);
pa_context_set_sink_mute_by_index(context_, sink_idx_, static_cast<int>(muted_), nullptr,
nullptr);
}
void AudioBackend::toggleSinkMute(bool mute) {
muted_ = mute;
pa_context_set_sink_mute_by_index(context_, sink_idx_, muted_, nullptr, nullptr);
pa_context_set_sink_mute_by_index(context_, sink_idx_, static_cast<int>(muted_), nullptr,
nullptr);
}
void AudioBackend::toggleSourceMute() {
source_muted_ = !muted_;
pa_context_set_source_mute_by_index(context_, source_idx_, source_muted_, nullptr, nullptr);
pa_context_set_source_mute_by_index(context_, source_idx_, static_cast<int>(source_muted_),
nullptr, nullptr);
}
void AudioBackend::toggleSourceMute(bool mute) {
source_muted_ = mute;
pa_context_set_source_mute_by_index(context_, source_idx_, source_muted_, nullptr, nullptr);
pa_context_set_source_mute_by_index(context_, source_idx_, static_cast<int>(source_muted_),
nullptr, nullptr);
}
bool AudioBackend::isBluetooth() {
@ -287,4 +286,4 @@ void AudioBackend::setIgnoredSinks(const Json::Value &config) {
}
}
} // namespace waybar::util
} // namespace waybar::util

View File

@ -4,7 +4,9 @@
#include <spdlog/spdlog.h>
#include <sys/epoll.h>
#include <cmath>
#include <optional>
#include <utility>
namespace {
class FileDescriptor {
@ -122,7 +124,7 @@ static void enumerate_devices(std::vector<BacklightDevice> &devices, udev *udev)
}
BacklightDevice::BacklightDevice(std::string name, int actual, int max, bool powered)
: name_(name), actual_(actual), max_(max), powered_(powered) {}
: name_(std::move(name)), actual_(actual), max_(max), powered_(powered) {}
std::string BacklightDevice::name() const { return name_; }
@ -140,7 +142,7 @@ void BacklightDevice::set_powered(bool powered) { powered_ = powered; }
BacklightBackend::BacklightBackend(std::chrono::milliseconds interval,
std::function<void()> on_updated_cb)
: on_updated_cb_(on_updated_cb), polling_interval_(interval), previous_best_({}) {
: on_updated_cb_(std::move(on_updated_cb)), polling_interval_(interval), previous_best_({}) {
std::unique_ptr<udev, UdevDeleter> udev_check{udev_new()};
check_nn(udev_check.get(), "Udev check new failed");
enumerate_devices(devices_, udev_check.get());
@ -236,24 +238,24 @@ void BacklightBackend::set_previous_best_device(const BacklightDevice *device) {
}
}
void BacklightBackend::set_scaled_brightness(std::string preferred_device, int brightness) {
void BacklightBackend::set_scaled_brightness(const std::string &preferred_device, int brightness) {
GET_BEST_DEVICE(best, (*this), preferred_device);
if (best != nullptr) {
const auto max = best->get_max();
const auto abs_val = static_cast<int>(round(brightness * max / 100.0f));
const auto abs_val = static_cast<int>(std::round(brightness * max / 100.0F));
set_brightness_internal(best->name(), abs_val, best->get_max());
}
}
void BacklightBackend::set_brightness(std::string preferred_device, ChangeType change_type,
void BacklightBackend::set_brightness(const std::string &preferred_device, ChangeType change_type,
double step) {
GET_BEST_DEVICE(best, (*this), preferred_device);
if (best != nullptr) {
const auto max = best->get_max();
const auto abs_step = static_cast<int>(round(step * max / 100.0f));
const auto abs_step = static_cast<int>(round(step * max / 100.0F));
const int new_brightness = change_type == ChangeType::Increase ? best->get_actual() + abs_step
: best->get_actual() - abs_step;
@ -261,7 +263,7 @@ void BacklightBackend::set_brightness(std::string preferred_device, ChangeType c
}
}
void BacklightBackend::set_brightness_internal(std::string device_name, int brightness,
void BacklightBackend::set_brightness_internal(const std::string &device_name, int brightness,
int max_brightness) {
brightness = std::clamp(brightness, 0, max_brightness);
@ -271,7 +273,7 @@ void BacklightBackend::set_brightness_internal(std::string device_name, int brig
login_proxy_->call_sync("SetBrightness", call_args);
}
int BacklightBackend::get_scaled_brightness(std::string preferred_device) {
int BacklightBackend::get_scaled_brightness(const std::string &preferred_device) {
GET_BEST_DEVICE(best, (*this), preferred_device);
if (best != nullptr) {

View File

@ -85,7 +85,9 @@ void waybar::Portal::on_signal(const Glib::ustring& sender_name, const Glib::ust
if (signal_name != "SettingChanged" || parameters.get_n_children() != 3) {
return;
}
Glib::VariantBase nspcv, keyv, valuev;
Glib::VariantBase nspcv;
Glib::VariantBase keyv;
Glib::VariantBase valuev;
parameters.get_child(nspcv, 0);
parameters.get_child(keyv, 1);
parameters.get_child(valuev, 2);

View File

@ -7,14 +7,14 @@ namespace {
class PrepareForSleep {
private:
PrepareForSleep() {
login1_connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
if (!login1_connection) {
login1_connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, nullptr);
if (login1_connection == nullptr) {
spdlog::warn("Unable to connect to the SYSTEM Bus!...");
} else {
login1_id = g_dbus_connection_signal_subscribe(
login1_connection, "org.freedesktop.login1", "org.freedesktop.login1.Manager",
"PrepareForSleep", "/org/freedesktop/login1", NULL, G_DBUS_SIGNAL_FLAGS_NONE,
prepareForSleep_cb, this, NULL);
"PrepareForSleep", "/org/freedesktop/login1", nullptr, G_DBUS_SIGNAL_FLAGS_NONE,
prepareForSleep_cb, this, nullptr);
}
}
@ -22,11 +22,11 @@ class PrepareForSleep {
const gchar *object_path, const gchar *interface_name,
const gchar *signal_name, GVariant *parameters,
gpointer user_data) {
if (g_variant_is_of_type(parameters, G_VARIANT_TYPE("(b)"))) {
if (g_variant_is_of_type(parameters, G_VARIANT_TYPE("(b)")) != 0) {
gboolean sleeping;
g_variant_get(parameters, "(b)", &sleeping);
PrepareForSleep *self = static_cast<PrepareForSleep *>(user_data);
auto *self = static_cast<PrepareForSleep *>(user_data);
self->signal.emit(sleeping);
}
}

View File

@ -3,13 +3,15 @@
#include <json/value.h>
#include <spdlog/spdlog.h>
#include <utility>
namespace waybar::util {
int default_priority_function(std::string& key) { return 0; }
RegexCollection::RegexCollection(const Json::Value& map, std::string default_repr,
std::function<int(std::string&)> priority_function)
: default_repr(default_repr) {
const std::function<int(std::string&)>& priority_function)
: default_repr(std::move(default_repr)) {
if (!map.isObject()) {
spdlog::warn("Mapping is not an object");
return;

View File

@ -10,9 +10,8 @@ std::string sanitize_string(std::string str) {
const std::pair<char, std::string> replacement_table[] = {
{'&', "&amp;"}, {'<', "&lt;"}, {'>', "&gt;"}, {'"', "&quot;"}, {'\'', "&apos;"}};
size_t startpoint;
for (size_t i = 0; i < (sizeof(replacement_table) / sizeof(replacement_table[0])); ++i) {
for (const auto& pair : replacement_table) {
startpoint = 0;
std::pair pair = replacement_table[i];
while ((startpoint = str.find(pair.first, startpoint)) != std::string::npos) {
str.replace(startpoint, 1, pair.second);
startpoint += pair.second.length();

View File

@ -2,8 +2,8 @@
int ustring_clen(const Glib::ustring &str) {
int total = 0;
for (auto i = str.begin(); i != str.end(); ++i) {
total += g_unichar_iswide(*i) + 1;
for (unsigned int i : str) {
total += g_unichar_iswide(i) + 1;
}
return total;
}
}