Merge branch 'master' into fix-appearance-change-css-watching
This commit is contained in:
+36
-16
@@ -40,12 +40,16 @@ AudioBackend::AudioBackend(std::function<void()> on_updated_cb, private_construc
|
||||
}
|
||||
|
||||
AudioBackend::~AudioBackend() {
|
||||
if (context_ != nullptr) {
|
||||
pa_context_disconnect(context_);
|
||||
}
|
||||
|
||||
if (mainloop_ != nullptr) {
|
||||
mainloop_api_->quit(mainloop_api_, 0);
|
||||
// Lock the mainloop so we can safely disconnect the context.
|
||||
// This must be done before stopping the thread.
|
||||
pa_threaded_mainloop_lock(mainloop_);
|
||||
if (context_ != nullptr) {
|
||||
pa_context_disconnect(context_);
|
||||
pa_context_unref(context_);
|
||||
context_ = nullptr;
|
||||
}
|
||||
pa_threaded_mainloop_unlock(mainloop_);
|
||||
pa_threaded_mainloop_stop(mainloop_);
|
||||
pa_threaded_mainloop_free(mainloop_);
|
||||
}
|
||||
@@ -73,7 +77,14 @@ void AudioBackend::contextStateCb(pa_context* c, void* 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);
|
||||
// Only quit the mainloop if this is still the active context.
|
||||
// During reconnection, the old context fires TERMINATED after the new one
|
||||
// has already been created; quitting in that case would kill the new context.
|
||||
// Note: context_ is only written from PA callbacks (while the mainloop lock is
|
||||
// held), so this comparison is safe within any PA callback.
|
||||
if (backend->context_ == nullptr || backend->context_ == c) {
|
||||
backend->mainloop_api_->quit(backend->mainloop_api_, 0);
|
||||
}
|
||||
break;
|
||||
case PA_CONTEXT_READY:
|
||||
pa_context_get_server_info(c, serverInfoCb, data);
|
||||
@@ -88,13 +99,17 @@ void AudioBackend::contextStateCb(pa_context* c, void* data) {
|
||||
nullptr, nullptr);
|
||||
break;
|
||||
case PA_CONTEXT_FAILED:
|
||||
// When pulseaudio server restarts, the connection is "failed". Try to reconnect.
|
||||
// pa_threaded_mainloop_lock is already acquired in callback threads.
|
||||
// So there is no need to lock it again.
|
||||
if (backend->context_ != nullptr) {
|
||||
pa_context_disconnect(backend->context_);
|
||||
if (pa_context_errno(c) != PA_ERR_CONNECTIONREFUSED) {
|
||||
// When pulseaudio server restarts, the connection is "failed". Try to reconnect.
|
||||
// pa_threaded_mainloop_lock is already acquired in callback threads.
|
||||
// So there is no need to lock it again.
|
||||
if (backend->context_ != nullptr) {
|
||||
pa_context_disconnect(backend->context_);
|
||||
pa_context_unref(backend->context_);
|
||||
backend->context_ = nullptr;
|
||||
}
|
||||
backend->connectContext();
|
||||
}
|
||||
backend->connectContext();
|
||||
break;
|
||||
case PA_CONTEXT_CONNECTING:
|
||||
case PA_CONTEXT_AUTHORIZING:
|
||||
@@ -237,9 +252,10 @@ void AudioBackend::sourceInfoCb(pa_context* /*context*/, const pa_source_info* i
|
||||
*/
|
||||
void AudioBackend::serverInfoCb(pa_context* context, const pa_server_info* i, void* data) {
|
||||
auto* backend = static_cast<AudioBackend*>(data);
|
||||
backend->current_sink_name_ = i->default_sink_name;
|
||||
backend->default_sink_name = i->default_sink_name;
|
||||
backend->default_source_name_ = i->default_source_name;
|
||||
if (i == nullptr) return;
|
||||
backend->current_sink_name_ = i->default_sink_name ? i->default_sink_name : "";
|
||||
backend->default_sink_name = i->default_sink_name ? i->default_sink_name : "";
|
||||
backend->default_source_name_ = i->default_source_name ? i->default_source_name : "";
|
||||
|
||||
pa_context_get_sink_info_list(context, sinkInfoCb, data);
|
||||
pa_context_get_source_info_list(context, sourceInfoCb, data);
|
||||
@@ -355,6 +371,7 @@ void AudioBackend::changeVolume(ChangeType change_type, double step, uint16_t ma
|
||||
}
|
||||
|
||||
void AudioBackend::toggleSinkMute() {
|
||||
if (context_ == nullptr || pa_context_get_state(context_) != PA_CONTEXT_READY) return;
|
||||
muted_ = !muted_;
|
||||
pa_threaded_mainloop_lock(mainloop_);
|
||||
pa_context_set_sink_mute_by_index(context_, sink_idx_, static_cast<int>(muted_), nullptr,
|
||||
@@ -363,6 +380,7 @@ void AudioBackend::toggleSinkMute() {
|
||||
}
|
||||
|
||||
void AudioBackend::toggleSinkMute(bool mute) {
|
||||
if (context_ == nullptr || pa_context_get_state(context_) != PA_CONTEXT_READY) return;
|
||||
muted_ = mute;
|
||||
pa_threaded_mainloop_lock(mainloop_);
|
||||
pa_context_set_sink_mute_by_index(context_, sink_idx_, static_cast<int>(muted_), nullptr,
|
||||
@@ -371,7 +389,8 @@ void AudioBackend::toggleSinkMute(bool mute) {
|
||||
}
|
||||
|
||||
void AudioBackend::toggleSourceMute() {
|
||||
source_muted_ = !muted_;
|
||||
if (context_ == nullptr || pa_context_get_state(context_) != PA_CONTEXT_READY) return;
|
||||
source_muted_ = !source_muted_;
|
||||
pa_threaded_mainloop_lock(mainloop_);
|
||||
pa_context_set_source_mute_by_index(context_, source_idx_, static_cast<int>(source_muted_),
|
||||
nullptr, nullptr);
|
||||
@@ -379,6 +398,7 @@ void AudioBackend::toggleSourceMute() {
|
||||
}
|
||||
|
||||
void AudioBackend::toggleSourceMute(bool mute) {
|
||||
if (context_ == nullptr || pa_context_get_state(context_) != PA_CONTEXT_READY) return;
|
||||
source_muted_ = mute;
|
||||
pa_threaded_mainloop_lock(mainloop_);
|
||||
pa_context_set_source_mute_by_index(context_, source_idx_, static_cast<int>(source_muted_),
|
||||
|
||||
@@ -79,18 +79,38 @@ static void upsert_device(std::vector<BacklightDevice>& devices, udev_device* de
|
||||
});
|
||||
if (found != devices.end()) {
|
||||
if (actual != nullptr) {
|
||||
found->set_actual(std::stoi(actual));
|
||||
try {
|
||||
found->set_actual(std::stoi(actual));
|
||||
} catch (const std::exception&) {
|
||||
}
|
||||
}
|
||||
if (max != nullptr) {
|
||||
found->set_max(std::stoi(max));
|
||||
try {
|
||||
found->set_max(std::stoi(max));
|
||||
} catch (const std::exception&) {
|
||||
}
|
||||
}
|
||||
if (power != nullptr) {
|
||||
found->set_powered(std::stoi(power) == 0);
|
||||
try {
|
||||
found->set_powered(std::stoi(power) == 0);
|
||||
} catch (const std::exception&) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const int actual_int = actual == nullptr ? 0 : std::stoi(actual);
|
||||
const int max_int = max == nullptr ? 0 : std::stoi(max);
|
||||
const bool power_bool = power == nullptr ? true : std::stoi(power) == 0;
|
||||
int actual_int = 0, max_int = 0;
|
||||
bool power_bool = true;
|
||||
try {
|
||||
if (actual != nullptr) actual_int = std::stoi(actual);
|
||||
} catch (const std::exception&) {
|
||||
}
|
||||
try {
|
||||
if (max != nullptr) max_int = std::stoi(max);
|
||||
} catch (const std::exception&) {
|
||||
}
|
||||
try {
|
||||
if (power != nullptr) power_bool = std::stoi(power) == 0;
|
||||
} catch (const std::exception&) {
|
||||
}
|
||||
devices.emplace_back(name, actual_int, max_int, power_bool);
|
||||
}
|
||||
}
|
||||
@@ -261,6 +281,11 @@ void BacklightBackend::set_brightness(const std::string& preferred_device, Chang
|
||||
|
||||
void BacklightBackend::set_brightness_internal(const std::string& device_name, int brightness,
|
||||
int max_brightness) {
|
||||
if (!login_proxy_) {
|
||||
spdlog::error("Login proxy not available, cannot set brightness");
|
||||
return;
|
||||
}
|
||||
|
||||
brightness = std::clamp(brightness, 0, max_brightness);
|
||||
|
||||
auto call_args = Glib::VariantContainerBase(
|
||||
@@ -273,6 +298,7 @@ int BacklightBackend::get_scaled_brightness(const std::string& preferred_device)
|
||||
GET_BEST_DEVICE(best, (*this), preferred_device);
|
||||
|
||||
if (best != nullptr) {
|
||||
if (best->get_max() == 0) return 0;
|
||||
return static_cast<int>(std::round(best->get_actual() * 100.0F / best->get_max()));
|
||||
}
|
||||
|
||||
|
||||
@@ -122,11 +122,15 @@ std::vector<std::string> waybar::CssReloadHelper::parseImports(const std::string
|
||||
auto maxIterations = 100U;
|
||||
do {
|
||||
previousSize = imports.size();
|
||||
std::vector<std::string> to_parse;
|
||||
for (const auto& [file, parsed] : imports) {
|
||||
if (!parsed) {
|
||||
parseImports(file, imports);
|
||||
to_parse.push_back(file);
|
||||
}
|
||||
}
|
||||
for (const auto& file : to_parse) {
|
||||
parseImports(file, imports);
|
||||
}
|
||||
|
||||
} while (imports.size() > previousSize && maxIterations-- > 0);
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ Glib::RefPtr<Gdk::Pixbuf> DefaultGtkIconThemeWrapper::load_icon(
|
||||
const std::lock_guard<std::mutex> lock(default_theme_mutex);
|
||||
|
||||
auto default_theme = Gtk::IconTheme::get_default();
|
||||
default_theme->rescan_if_needed();
|
||||
|
||||
auto icon_info = default_theme->lookup_icon(name, tmp_size, flags);
|
||||
|
||||
|
||||
@@ -5,8 +5,11 @@
|
||||
std::vector<std::string> IconLoader::search_prefix() {
|
||||
std::vector<std::string> prefixes = {""};
|
||||
|
||||
std::string home_dir = std::getenv("HOME");
|
||||
prefixes.push_back(home_dir + "/.local/share/");
|
||||
const char* home_env = std::getenv("HOME");
|
||||
std::string home_dir = home_env ? home_env : "";
|
||||
if (!home_dir.empty()) {
|
||||
prefixes.push_back(home_dir + "/.local/share/");
|
||||
}
|
||||
|
||||
auto xdg_data_dirs = std::getenv("XDG_DATA_DIRS");
|
||||
if (!xdg_data_dirs) {
|
||||
@@ -139,7 +142,7 @@ bool IconLoader::image_load_icon(Gtk::Image& image, const Glib::RefPtr<Gtk::Icon
|
||||
}
|
||||
|
||||
if (pixbuf) {
|
||||
if (pixbuf->get_width() != scaled_icon_size) {
|
||||
if (pixbuf->get_width() != scaled_icon_size && pixbuf->get_height() > 0) {
|
||||
int width = scaled_icon_size * pixbuf->get_width() / pixbuf->get_height();
|
||||
pixbuf = pixbuf->scale_simple(width, scaled_icon_size, Gdk::InterpType::INTERP_BILINEAR);
|
||||
}
|
||||
|
||||
@@ -54,11 +54,17 @@ PipewireBackend::PipewireBackend(PrivateConstructorTag tag)
|
||||
context_ = pw_context_new(pw_thread_loop_get_loop(mainloop_), nullptr, 0);
|
||||
if (context_ == nullptr) {
|
||||
pw_thread_loop_unlock(mainloop_);
|
||||
pw_thread_loop_destroy(mainloop_);
|
||||
mainloop_ = nullptr;
|
||||
throw std::runtime_error("pa_context_new() failed.");
|
||||
}
|
||||
core_ = pw_context_connect(context_, nullptr, 0);
|
||||
if (core_ == nullptr) {
|
||||
pw_thread_loop_unlock(mainloop_);
|
||||
pw_context_destroy(context_);
|
||||
context_ = nullptr;
|
||||
pw_thread_loop_destroy(mainloop_);
|
||||
mainloop_ = nullptr;
|
||||
throw std::runtime_error("pw_context_connect() failed");
|
||||
}
|
||||
registry_ = pw_core_get_registry(core_, PW_VERSION_REGISTRY, 0);
|
||||
@@ -136,7 +142,10 @@ void PipewireBackend::handleRegistryEventGlobal(uint32_t id, uint32_t permission
|
||||
|
||||
pw_proxy_add_object_listener(proxy, &pNodeInfo->object_listener, &NODE_EVENTS, pNodeInfo);
|
||||
|
||||
privacy_nodes.insert_or_assign(id, pNodeInfo);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
privacy_nodes.insert_or_assign(id, pNodeInfo);
|
||||
}
|
||||
}
|
||||
|
||||
void PipewireBackend::handleRegistryEventGlobalRemove(uint32_t id) {
|
||||
|
||||
+19
-13
@@ -58,21 +58,27 @@ void waybar::Portal::refreshAppearance() {
|
||||
// xdg-desktop-portal 1.17 will fix this issue with a new `ReadOne` method,
|
||||
// but this version is not yet released.
|
||||
// TODO(xdg-desktop-portal v1.17): switch to ReadOne
|
||||
auto container = Glib::VariantBase::cast_dynamic<Glib::VariantContainerBase>(response);
|
||||
Glib::VariantBase modev;
|
||||
container.get_child(modev, 0);
|
||||
auto mode =
|
||||
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::Variant<Glib::Variant<uint32_t>>>>(modev)
|
||||
.get()
|
||||
.get()
|
||||
.get();
|
||||
auto newMode = Appearance(mode);
|
||||
if (newMode == currentMode) {
|
||||
try {
|
||||
auto container = Glib::VariantBase::cast_dynamic<Glib::VariantContainerBase>(response);
|
||||
Glib::VariantBase modev;
|
||||
container.get_child(modev, 0);
|
||||
auto mode =
|
||||
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::Variant<Glib::Variant<uint32_t>>>>(
|
||||
modev)
|
||||
.get()
|
||||
.get()
|
||||
.get();
|
||||
auto newMode = Appearance(mode);
|
||||
if (newMode == currentMode) {
|
||||
return;
|
||||
}
|
||||
spdlog::info("Discovered appearance '{}'", newMode);
|
||||
currentMode = newMode;
|
||||
m_signal_appearance_changed.emit(currentMode);
|
||||
} catch (const std::bad_cast& e) {
|
||||
spdlog::error("Unexpected appearance variant format: {}", e.what());
|
||||
return;
|
||||
}
|
||||
spdlog::info("Discovered appearance '{}'", newMode);
|
||||
currentMode = newMode;
|
||||
m_signal_appearance_changed.emit(currentMode);
|
||||
}
|
||||
|
||||
waybar::Appearance waybar::Portal::getAppearance() { return currentMode; };
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
struct TransformResult {
|
||||
std::string css;
|
||||
bool was_transformed;
|
||||
};
|
||||
|
||||
TransformResult transform_8bit_to_hex(const std::string& file_path) {
|
||||
std::ifstream f(file_path, std::ios::in | std::ios::binary);
|
||||
const auto size = fs::file_size(file_path);
|
||||
std::string result(size, '\0');
|
||||
if (!f.is_open() || !f.good()) {
|
||||
throw std::runtime_error("Cannot open file: " + file_path);
|
||||
}
|
||||
|
||||
if (size == 0) {
|
||||
return {.css = result, .was_transformed = false};
|
||||
}
|
||||
|
||||
f.read(result.data(), size);
|
||||
|
||||
static std::regex pattern(
|
||||
R"(\#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2}))");
|
||||
std::string final_output;
|
||||
|
||||
auto it = std::sregex_iterator(result.begin(), result.end(), pattern);
|
||||
auto eof = std::sregex_iterator();
|
||||
|
||||
if (it == eof) {
|
||||
return {.css = result, .was_transformed = false};
|
||||
}
|
||||
|
||||
std::smatch match;
|
||||
while (it != eof) {
|
||||
match = *it;
|
||||
|
||||
final_output += match.prefix().str();
|
||||
|
||||
int r = stoi(match[1].str(), nullptr, 16);
|
||||
int g = stoi(match[2].str(), nullptr, 16);
|
||||
int b = stoi(match[3].str(), nullptr, 16);
|
||||
double a = (stoi(match[4].str(), nullptr, 16) / 255.0);
|
||||
|
||||
std::stringstream ss;
|
||||
ss << "rgba(" << r << "," << g << "," << b << "," << std::fixed << std::setprecision(2) << a
|
||||
<< ")";
|
||||
final_output += ss.str();
|
||||
|
||||
++it;
|
||||
}
|
||||
|
||||
final_output += match.suffix().str();
|
||||
|
||||
return {.css = final_output, .was_transformed = true};
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
#include <glib.h>
|
||||
|
||||
#include <string>
|
||||
#include <util/utf8_string.hpp>
|
||||
|
||||
namespace waybar::util {
|
||||
|
||||
namespace {
|
||||
// Wide characters count as two, zero-width characters count as zero
|
||||
// Modifies str in-place (unless width = std::string::npos)
|
||||
// Returns the total width of the string pre-truncating
|
||||
size_t measure_and_truncate(std::string& str, size_t width = std::string::npos) {
|
||||
if (str.length() == 0) return 0;
|
||||
|
||||
const gchar* trunc_end = nullptr;
|
||||
|
||||
size_t total_width = 0;
|
||||
|
||||
for (gchar *data = str.data(), *end = data + str.size(); data != nullptr;) {
|
||||
gunichar c = g_utf8_get_char_validated(data, end - data);
|
||||
if (c == -1U || c == -2U) {
|
||||
// invalid unicode, treat string as ascii
|
||||
if (width != std::string::npos && str.length() > width) str.resize(width);
|
||||
return str.length();
|
||||
} else if (g_unichar_iswide(c)) {
|
||||
total_width += 2;
|
||||
} else if (!g_unichar_iszerowidth(c) && c != 0xAD) { // neither zero-width nor soft hyphen
|
||||
total_width += 1;
|
||||
}
|
||||
|
||||
data = g_utf8_find_next_char(data, end);
|
||||
if (width != std::string::npos && total_width <= width && !g_unichar_isspace(c))
|
||||
trunc_end = data;
|
||||
}
|
||||
|
||||
if (trunc_end) str.resize(trunc_end - str.data());
|
||||
|
||||
return total_width;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
size_t utf8_width(const std::string& str) {
|
||||
return measure_and_truncate(const_cast<std::string&>(str));
|
||||
}
|
||||
|
||||
void utf8_truncate(std::string& s, const std::string& ellipsis, size_t max_len) {
|
||||
if (max_len == 0) {
|
||||
s.resize(0);
|
||||
return;
|
||||
}
|
||||
size_t len = measure_and_truncate(s, max_len);
|
||||
if (len > max_len) {
|
||||
size_t ellipsis_len = utf8_width(ellipsis);
|
||||
if (max_len >= ellipsis_len) {
|
||||
if (ellipsis_len) measure_and_truncate(s, max_len - ellipsis_len);
|
||||
s += ellipsis;
|
||||
} else {
|
||||
s.resize(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace waybar::util
|
||||
Reference in New Issue
Block a user