From 6f2e8320a9555c698b431f889eea84d1a56f10c9 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 2 Apr 2026 15:22:42 +0300 Subject: [PATCH] TaskBar: Show current volume level --- userspace/programs/TaskBar/CMakeLists.txt | 1 + userspace/programs/TaskBar/main.cpp | 64 +++++++++++++++++++++-- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/userspace/programs/TaskBar/CMakeLists.txt b/userspace/programs/TaskBar/CMakeLists.txt index 270eb042..548e4388 100644 --- a/userspace/programs/TaskBar/CMakeLists.txt +++ b/userspace/programs/TaskBar/CMakeLists.txt @@ -7,6 +7,7 @@ banan_link_library(TaskBar ban) banan_link_library(TaskBar libc) banan_link_library(TaskBar libfont) banan_link_library(TaskBar libgui) +banan_include_headers(TaskBar libaudio) banan_include_headers(TaskBar libinput) install(TARGETS TaskBar OPTIONAL) diff --git a/userspace/programs/TaskBar/main.cpp b/userspace/programs/TaskBar/main.cpp index e6e5ff02..c6c7e36e 100644 --- a/userspace/programs/TaskBar/main.cpp +++ b/userspace/programs/TaskBar/main.cpp @@ -1,9 +1,12 @@ +#include #include #include #include #include #include +#include +#include static BAN::ErrorOr read_integer_from_file(const char* file) { @@ -60,12 +63,57 @@ static BAN::String get_battery_percentage() return result; } +static int open_audio_server_fd() +{ + int fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd == -1) + return -1; + + sockaddr_un addr; + addr.sun_family = AF_UNIX; + strcpy(addr.sun_path, LibAudio::s_audio_server_socket.data()); + if (connect(fd, reinterpret_cast(&addr), sizeof(addr)) == -1) + return -1; + + return fd; +} + +static BAN::String get_audio_volume() +{ + static int fd = -1; + if (fd == -1 && (fd = open_audio_server_fd()) == -1) + return {}; + + const LibAudio::Packet request { + .type = LibAudio::Packet::GetVolume, + .parameter = {}, + }; + if (send(fd, &request, sizeof(request), 0) != sizeof(request)) + { + close(fd); + fd = -1; + return {}; + } + + uint32_t response; + if (recv(fd, &response, sizeof(response), 0) != sizeof(response)) + { + close(fd); + fd = -1; + return {}; + } + + return MUST(BAN::String::formatted("vol {}% | ", response / 10)); +} + static BAN::ErrorOr get_task_bar_string() { BAN::String result; TRY(result.append(get_battery_percentage())); + TRY(result.append(get_audio_volume())); + const time_t current_time = time(nullptr); TRY(result.append(ctime(¤t_time))); result.pop_back(); @@ -79,6 +127,8 @@ int main() constexpr uint32_t bg_color = 0xFF202020; constexpr uint32_t fg_color = 0xFFFFFFFF; + signal(SIGUSR1, [](int) {}); + auto font = MUST(LibFont::Font::load("/usr/share/fonts/lat0-16.psfu"_sv)); auto attributes = LibGUI::Window::default_attributes; @@ -99,7 +149,8 @@ int main() bool is_running = true; - const auto update_time_string = + uint32_t old_text_w = 0; + const auto update_string = [&]() { auto text_or_error = get_task_bar_string(); @@ -112,15 +163,20 @@ int main() const uint32_t text_x = window->width() - text_w - padding; const uint32_t text_y = padding; + const uint32_t inval_w = BAN::Math::max(text_w, old_text_w); + const uint32_t inval_x = window->width() - inval_w - padding; + auto& texture = window->texture(); - texture.fill_rect(text_x, text_y, text_w, text_h, bg_color); + texture.fill_rect(inval_x, text_y, inval_w, text_h, bg_color); texture.draw_text(text, font, text_x, text_y, fg_color); - window->invalidate(text_x, text_y, text_w, text_h); + window->invalidate(inval_x, text_y, inval_w, text_h); + + old_text_w = text_w; }; while (is_running) { - update_time_string(); + update_string(); constexpr uint64_t ns_per_s = 1'000'000'000;