bar can now toggle bluetooth power and discovery

This commit is contained in:
2025-12-22 01:41:01 +01:00
parent 0101ea1ec0
commit 5a429a3b8b
31 changed files with 507 additions and 114 deletions

View File

@@ -29,8 +29,10 @@ App::App() {
continue;
}
auto bar = new Bar(monitor->gobj(), this->hyprlandService,
this->trayService, hyprlandMonitor->id);
this->trayService, this->bluetoothService, hyprlandMonitor->id);
bar->set_application(app);
bar->show();

View File

@@ -1,26 +1,24 @@
#include "bar/bar.hpp"
#include "gtk/gtk.h"
#include "widgets/date.hpp"
#include "widgets/spacer.hpp"
#include "widgets/todoPopover.hpp"
#include "widgets/volumeWidget.hpp"
#include "widgets/workspaceIndicator.hpp"
#include "helpers/systemHelper.hpp"
#include <filesystem>
#include <gtkmm/enums.h>
#include <gtkmm/label.h>
#include <gtkmm/window.h>
#include "helpers/systemHelper.hpp"
#include "widgets/date.hpp"
#include "widgets/spacer.hpp"
#include "widgets/todo.hpp"
#include "widgets/volumeWidget.hpp"
#include "widgets/workspaceIndicator.hpp"
#include "glibmm/main.h"
#include "gtk/gtk.h"
#include "sigc++/functors/mem_fun.h"
Bar::Bar(GdkMonitor *monitor, HyprlandService &hyprlandService,
TrayService &trayService, int monitorId)
: hyprlandService(hyprlandService), trayService(trayService),
TrayService &trayService, BluetoothService &bluetoothService, int monitorId)
: hyprlandService(hyprlandService), trayService(trayService), bluetoothService(bluetoothService),
monitorId(monitorId) {
set_name("bar-window");
@@ -39,20 +37,30 @@ Bar::Bar(GdkMonitor *monitor, HyprlandService &hyprlandService,
set_child(main_box);
this->volumeWidget = Gtk::make_managed<VolumeWidget>();
this->volumeWidget = Gtk::make_managed<VolumeWidget>();
this->workspaceIndicator = Gtk::make_managed<WorkspaceIndicator>(hyprlandService, monitorId);
this->trayWidget = Gtk::make_managed<TrayWidget>(trayService);
this->trayWidget = Gtk::make_managed<TrayWidget>(trayService);
this->bluetoothWidget = Gtk::make_managed<BluetoothWidget>("\ue8bb", "Bluetooth");
bluetoothService.powerStateChangedSignal.connect(
sigc::mem_fun(*this->bluetoothWidget, &BluetoothWidget::setPowerState));
bluetoothWidget->powerStateChangedSignal.connect(
sigc::mem_fun(bluetoothService, &BluetoothService::setPowerState));
bluetoothWidget->setPowerState(bluetoothService.getPowerState());
bluetoothService.isDiscoveringChangedSignal.connect(
sigc::mem_fun(*this->bluetoothWidget, &BluetoothWidget::setIsDiscovering));
bluetoothWidget->isDiscoveringChangedSignal.connect(
sigc::mem_fun(bluetoothService, &BluetoothService::setIsDiscovering));
bluetoothWidget->setIsDiscovering(bluetoothService.getIsDiscovering());
load_css();
setup_ui();
clock.onUpdate();
Glib::signal_timeout().connect(sigc::mem_fun(clock, &Clock::onUpdate),
1000);
date.onUpdate();
Glib::signal_timeout().connect(sigc::mem_fun(clock, &Clock::onUpdate), 1000);
Glib::signal_timeout().connect(sigc::mem_fun(date, &Date::onUpdate), 1000);
}
@@ -83,8 +91,9 @@ void Bar::setup_center_box() {
}
void Bar::setup_right_box() {
right_box.append(*trayWidget);
right_box.append(homeAssistant);
right_box.append(*this->trayWidget);
right_box.append(this->homeAssistant);
right_box.append(*this->bluetoothWidget);
}
void Bar::load_css() {

View File

@@ -1,15 +1,16 @@
#include "components/todoEntry.hpp"
#include <gtkmm/label.h>
#include <string>
#include "gtkmm/button.h"
TodoEntry::TodoEntry(int id, std::string text, sigc::signal<void(int)> signal_dismissed)
TodoEntry::TodoEntry(int id, std::string text, sigc::signal<void(int)> signal_dismissed, sigc::signal<void(int, std::string)> signal_edited)
: Gtk::Box(Gtk::Orientation::HORIZONTAL) {
this->id = id;
this->text = text;
this->signal_dismissed = signal_dismissed;
this->signal_edited = signal_edited;
auto box = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL);
box->set_hexpand(true);
@@ -35,6 +36,10 @@ TodoEntry::TodoEntry(int id, std::string text, sigc::signal<void(int)> signal_di
dismissButton->signal_clicked().connect(sigc::mem_fun(*this, &TodoEntry::on_dismiss_clicked));
auto editButton = Gtk::make_managed<Gtk::Button>("\uf044");
editButton->set_valign(Gtk::Align::CENTER);
buttonBox->append(*editButton);
buttonBox->append(*dismissButton);
}

185
src/services/bluetooth.cpp Normal file
View File

@@ -0,0 +1,185 @@
#include "services/bluetooth.hpp"
#include <cassert>
#include <iostream>
#include "glib.h"
#include "sigc++/signal.h"
BluetoothService::BluetoothService() {
GError *error = nullptr;
this->adapter_proxy = g_dbus_proxy_new_for_bus_sync(
G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE,
nullptr,
"org.bluez",
"/org/bluez/hci0",
"org.bluez.Adapter1",
nullptr,
&error);
if (error) {
std::cerr << "Error creating Bluetooth adapter proxy: "
<< error->message << std::endl;
g_error_free(error);
assert(false);
} else {
std::cout << "Bluetooth adapter proxy created successfully." << std::endl;
}
this->powerState = this->getPowerState();
this->isDiscovering = this->getIsDiscovering();
g_signal_connect(
this->adapter_proxy,
"g-properties-changed",
G_CALLBACK(BluetoothService::onPropertyChangedStatic),
this);
}
bool BluetoothService::getPowerState() {
GVariant *result = g_dbus_proxy_get_cached_property(
this->adapter_proxy,
"Powered");
if (!result) {
std::cerr << "Error getting Powered property." << std::endl;
return false;
}
gboolean powered;
g_variant_get(result, "b", &powered);
g_variant_unref(result);
return powered;
}
bool BluetoothService::getIsDiscovering() {
GVariant *result = g_dbus_proxy_get_cached_property(
this->adapter_proxy,
"Discovering");
if (!result) {
std::cerr << "Error getting Discovering property." << std::endl;
return false;
}
gboolean discovering;
g_variant_get(result, "b", &discovering);
g_variant_unref(result);
return discovering;
}
void BluetoothService::setPowerState(bool state) {
GError *error = nullptr;
GDBusConnection *connection = g_dbus_proxy_get_connection(this->adapter_proxy);
GVariant *reply = g_dbus_connection_call_sync(
connection,
"org.bluez",
"/org/bluez/hci0",
"org.freedesktop.DBus.Properties",
"Set",
g_variant_new(
"(ssv)",
"org.bluez.Adapter1",
"Powered",
g_variant_new_boolean(state)),
nullptr,
G_DBUS_CALL_FLAGS_NONE,
-1,
nullptr,
&error);
if (error) {
std::cerr << "Error setting Powered property: "
<< error->message << std::endl;
g_error_free(error);
}
if (reply) {
g_variant_unref(reply);
}
if (!error) {
this->powerState = state;
}
}
void BluetoothService::setIsDiscovering(bool state) {
const bool currently_discovering = this->getIsDiscovering();
this->isDiscovering = currently_discovering;
if (currently_discovering == state) {
return;
}
GError *error = nullptr;
const char *method = state ? "StartDiscovery" : "StopDiscovery";
GVariant *reply = g_dbus_proxy_call_sync(
this->adapter_proxy,
method,
nullptr,
G_DBUS_CALL_FLAGS_NONE,
-1,
nullptr,
&error);
if (error) {
std::cerr << "Error calling " << method << ": "
<< error->message << std::endl;
g_error_free(error);
if (reply) {
g_variant_unref(reply);
}
return;
}
this->isDiscovering = state;
if (reply) {
g_variant_unref(reply);
}
}
void BluetoothService::onPropertyChanged(GDBusProxy *proxy,
GVariant *changed_properties,
const gchar *const *invalidated_properties,
gpointer user_data) {
gboolean is_powered = FALSE;
gboolean is_discovering = FALSE;
if (g_variant_lookup(changed_properties, "Powered", "b", &is_powered)) {
this->setIsDiscovering(false);
isDiscoveringChangedSignal.emit(isDiscovering);
this->powerState = is_powered;
powerStateChangedSignal.emit(powerState);
}
if (g_variant_lookup(changed_properties, "Discovering", "b", &is_discovering)) {
this->isDiscovering = is_discovering;
isDiscoveringChangedSignal.emit(isDiscovering);
}
}
void BluetoothService::onPropertyChangedStatic(GDBusProxy *proxy,
GVariant *changed_properties,
const gchar *const *invalidated_properties,
gpointer user_data) {
BluetoothService *service = static_cast<BluetoothService *>(user_data);
if (service) {
service->onPropertyChanged(proxy, changed_properties, invalidated_properties, user_data);
} else {
std::cerr << "Error: BluetoothService instance is null in static callback." << std::endl;
assert(false);
}
}

View File

@@ -2,6 +2,7 @@
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iterator>
#include <nlohmann/json.hpp>
#include <stdexcept>
@@ -10,8 +11,6 @@
#include <sys/un.h>
#include <unistd.h>
#include <iostream>
#include "helpers/systemHelper.hpp"
HyprlandService::HyprlandService() = default;

View File

@@ -1,10 +1,10 @@
#include "services/todoAdapter.hpp"
#include <filesystem>
#include <iostream>
#include <memory>
#include <sqlite3.h>
#include "services/todoAdapter.hpp"
namespace {
std::string getDbPath() {
const char *homeDir = getenv("HOME");
@@ -31,7 +31,6 @@ class SqliteTodoAdapter final : public ITodoAdapter {
bool init() override {
std::string dbPath = getDbPath();
std::cout << "Opening database at: " << dbPath << std::endl;
int rc = sqlite3_open(dbPath.c_str(), &db_);
if (rc != SQLITE_OK) {
@@ -42,8 +41,8 @@ class SqliteTodoAdapter final : public ITodoAdapter {
const char *sql = "CREATE TABLE IF NOT EXISTS todos ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"text TEXT NOT NULL);";
char *zErrMsg = nullptr;
rc = sqlite3_exec(db_, sql, nullptr, nullptr, &zErrMsg);
char *zErrMsg = nullptr;
rc = sqlite3_exec(db_, sql, nullptr, nullptr, &zErrMsg);
if (rc != SQLITE_OK) {
std::cerr << "SQL error (create table): " << (zErrMsg ? zErrMsg : "") << std::endl;
sqlite3_free(zErrMsg);

View File

@@ -1,8 +1,10 @@
#include "services/todo.hpp"
#include "components/todoEntry.hpp"
#include "gtkmm/object.h"
#include <cassert>
#include <iostream>
#include <gtkmm/object.h>
#include "components/todoEntry.hpp"
std::unique_ptr<ITodoAdapter> makeSqliteTodoAdapter();
@@ -62,9 +64,13 @@ TodoEntry *TodoService::addTodo(std::string text, bool emitSignal, bool persist)
id = newId;
}
auto signal = sigc::signal<void(int)>();
signal.connect(sigc::mem_fun(*this, &TodoService::removeTodo));
TodoEntry *todo = Gtk::make_managed<TodoEntry>(id, text, signal);
auto dismissSignal = sigc::signal<void(int)>();
dismissSignal.connect(sigc::mem_fun(*this, &TodoService::removeTodo));
auto editSignal = sigc::signal<void(int, std::string)>();
editSignal.connect(sigc::mem_fun(*this, &TodoService::updateTodo));
TodoEntry *todo = Gtk::make_managed<TodoEntry>(id, text, dismissSignal, editSignal);
todos[id] = todo;
@@ -79,14 +85,24 @@ TodoEntry *TodoService::addTodo(std::string text, bool emitSignal, bool persist)
return todo;
}
void TodoService::updateTodo(int id, std::string text) {}
void TodoService::updateTodo(int id, std::string text) {
if (todos.find(id) == todos.end()) {
std::cerr << "Todo with id " << id << " not found!" << std::endl;
assert(false);
}
if (this->adapter) {
this->adapter->updateTodo(id, text);
}
this->refreshSignal.emit();
}
void TodoService::load() {
if (!this->adapter) {
return;
}
std::cout << "Loading todos from database..." << std::endl;
auto rows = this->adapter->listTodos();
int count = 0;
@@ -96,14 +112,17 @@ void TodoService::load() {
int id = row.id;
std::string text = row.text;
auto signal = sigc::signal<void(int)>();
signal.connect(sigc::mem_fun(*this, &TodoService::removeTodo));
TodoEntry *todo = Gtk::make_managed<TodoEntry>(id, text, signal);
auto dismissSignal = sigc::signal<void(int)>();
dismissSignal.connect(sigc::mem_fun(*this, &TodoService::removeTodo));
auto editSignal = sigc::signal<void(int, std::string)>();
editSignal.connect(sigc::mem_fun(*this, &TodoService::updateTodo));
TodoEntry *todo = Gtk::make_managed<TodoEntry>(id, text, dismissSignal, editSignal);
todos[id] = todo;
if (id >= nextId) {
nextId = id + 1;
}
}
std::cout << "Loaded " << count << " todos." << std::endl;
}

View File

@@ -1,5 +1,7 @@
#include "services/tray.hpp"
#include <algorithm>
#include <cstring>
#include <gdkmm/pixbuf.h>
#include <gdkmm/texture.h>
#include <gio/gdbusmenumodel.h>
@@ -7,9 +9,6 @@
#include <giomm/dbusactiongroup.h>
#include <giomm/dbusownname.h>
#include <giomm/menumodel.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <tuple>
#include <vector>

70
src/widgets/bluetooth.cpp Normal file
View File

@@ -0,0 +1,70 @@
#include "widgets/bluetooth.hpp"
BluetoothWidget::BluetoothWidget(std::string icon, std::string title) : Popover(icon, title) {
this->popover->set_size_request(100, -1);
set_popover_child(this->container);
this->container.set_orientation(Gtk::Orientation::VERTICAL);
this->container.set_spacing(10);
this->container.add_css_class("bluetooth-popover-container");
this->statusArea.add_css_class("bluetooth-status-area");
this->statusArea.set_hexpand(true);
this->statusArea.set_halign(Gtk::Align::FILL);
this->container.append(this->statusArea);
this->toggleButton = Gtk::make_managed<Gtk::Button>("\ue1a8");
this->toggleButton->add_css_class("bluetooth-toggle-button");
this->toggleButton->set_tooltip_text("Turn Bluetooth Off");
this->toggleButton->signal_clicked().connect([this]() {
const bool newState = !isPowered;
setPowerState(newState);
powerStateChangedSignal.emit(newState);
});
this->toggleButton->add_css_class("toggle-button");
this->scanButton = Gtk::make_managed<Gtk::Button>("\ue1aa");
this->scanButton->set_tooltip_text("Scan for Devices");
this->scanButton->add_css_class("bluetooth-scan-button");
this->scanButton->signal_clicked().connect([this]() {
const bool newState = !isDiscovering;
setIsDiscovering(newState);
isDiscoveringChangedSignal.emit(newState);
});
this->statusArea.append(*this->toggleButton);
this->statusArea.append(*this->scanButton);
}
void BluetoothWidget::setPowerState(bool state) {
this->isPowered = state;
if (state) {
this->toggleButton->set_label("\ue1a8");
this->toggleButton->add_css_class("toggle-button-on");
this->toggleButton->remove_css_class("toggle-button-off");
} else {
this->toggleButton->set_label("\ue1a9");
this->toggleButton->add_css_class("toggle-button-off");
this->toggleButton->remove_css_class("toggle-button-on");
}
this->setIsDiscovering(false);
}
void BluetoothWidget::setIsDiscovering(bool state) {
this->isDiscovering = state;
if (state) {
this->scanButton->add_css_class("toggle-button-on");
this->scanButton->remove_css_class("toggle-button-off");
} else {
this->scanButton->add_css_class("toggle-button-off");
this->scanButton->remove_css_class("toggle-button-on");
}
}
void BluetoothWidget::update() {
setPowerState(isPowered);
setIsDiscovering(isDiscovering);
}

View File

@@ -1,4 +1,4 @@
#include "widgets/todoPopover.hpp"
#include "widgets/todo.hpp"
#include <gtkmm/box.h>
#include <gtkmm/entry.h>
@@ -12,24 +12,23 @@ TodoPopover::TodoPopover(std::string icon, std::string title) : Popover(icon, ti
container.set_spacing(10);
container.add_css_class("todo-popover-container");
auto entry = Gtk::make_managed<Gtk::Entry>();
entry->set_placeholder_text("Enter your to-do item...");
entry->add_css_class("todo-input");
entry->set_hexpand(true);
entry->set_halign(Gtk::Align::FILL);
entry->set_valign(Gtk::Align::START);
inputArea.append(*entry);
inputArea.add_css_class("todo-input-area");
inputArea.set_hexpand(true);
inputArea.set_halign(Gtk::Align::FILL);
entry->signal_activate().connect([this, entry]() {
std::string text = entry->get_text();
if (!text.empty()) {
this->todoService->addTodo(text);
entry->set_text("");
}
});
@@ -39,12 +38,12 @@ TodoPopover::TodoPopover(std::string icon, std::string title) : Popover(icon, ti
this->todoList = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL);
container.append(*todoList);
todoList->add_css_class("todo-list");
auto signal = sigc::signal<void()>();
this->todoService = new TodoService(signal);
signal.connect(sigc::mem_fun(*this, &TodoPopover::update));
this->set_popover_child(this->container);
this->update();
}
@@ -52,23 +51,23 @@ TodoPopover::TodoPopover(std::string icon, std::string title) : Popover(icon, ti
void TodoPopover::update() {
auto todos = this->todoService->getTodos();
Gtk::Widget* child = todoList->get_first_child();
Gtk::Widget *child = todoList->get_first_child();
while (child) {
Gtk::Widget* next = child->get_next_sibling();
Gtk::Widget *next = child->get_next_sibling();
bool found = false;
for (auto& [id, todo] : todos) {
for (auto &[id, todo] : todos) {
if (child == todo) {
found = true;
break;
}
}
if (!found) {
todoList->remove(*child);
}
child = next;
}

View File

@@ -1,12 +1,12 @@
#include "widgets/volumeWidget.hpp"
#include "helpers/systemHelper.hpp"
#include <cmath>
#include <iostream>
#include <regex>
#include <sigc++/functors/mem_fun.h>
#include "helpers/systemHelper.hpp"
VolumeWidget::VolumeWidget() : Gtk::Box(Gtk::Orientation::HORIZONTAL) {
set_valign(Gtk::Align::CENTER);
set_halign(Gtk::Align::CENTER);

View File

@@ -1,4 +1,5 @@
#include "widgets/webWidget.hpp"
#include <gtkmm/label.h>
#include <webkit/webkit.h>

View File

@@ -1,5 +1,4 @@
#include "widgets/workspaceIndicator.hpp"
#include "services/hyprland.hpp"
#include <cassert>
#include <gdk/gdk.h>
@@ -7,6 +6,8 @@
#include <gtkmm/widget.h>
#include <sigc++/functors/mem_fun.h>
#include "services/hyprland.hpp"
WorkspaceIndicator::WorkspaceIndicator(HyprlandService &service, int monitorId)
: Gtk::Box(Gtk::Orientation::HORIZONTAL), service(service),
monitorId(monitorId) {