quick commit

This commit is contained in:
2026-01-31 11:46:12 +01:00
parent 0e613141da
commit ad5e678c5d
25 changed files with 287 additions and 622 deletions

View File

@@ -12,10 +12,12 @@ find_package(PkgConfig REQUIRED)
pkg_check_modules(GTKMM REQUIRED gtkmm-4.0) pkg_check_modules(GTKMM REQUIRED gtkmm-4.0)
pkg_check_modules(LAYERSHELL REQUIRED gtk4-layer-shell-0) pkg_check_modules(LAYERSHELL REQUIRED gtk4-layer-shell-0)
pkg_check_modules(WEBKIT REQUIRED webkitgtk-6.0) pkg_check_modules(WEBKIT REQUIRED webkitgtk-6.0)
pkg_check_modules(SQLITE3 REQUIRED sqlite3) pkg_check_modules(CURL REQUIRED libcurl)
include_directories(${GTKMM_INCLUDE_DIRS} ${LAYERSHELL_INCLUDE_DIRS} ${WEBKIT_INCLUDE_DIRS} ${SQLITE3_INCLUDE_DIRS}) find_package(nlohmann_json 3.2.0 REQUIRED)
link_directories(${GTKMM_LIBRARY_DIRS} ${LAYERSHELL_LIBRARY_DIRS} ${WEBKIT_LIBRARY_DIRS} ${SQLITE3_LIBRARY_DIRS})
include_directories(${GTKMM_INCLUDE_DIRS} ${LAYERSHELL_INCLUDE_DIRS} ${WEBKIT_INCLUDE_DIRS} ${CURL_INCLUDE_DIRS} ${nlohmann_json_INCLUDE_DIRS})
link_directories(${GTKMM_LIBRARY_DIRS} ${LAYERSHELL_LIBRARY_DIRS} ${WEBKIT_LIBRARY_DIRS} ${CURL_LIBRARY_DIRS})
add_library(bar_lib) add_library(bar_lib)
target_sources(bar_lib target_sources(bar_lib
@@ -29,20 +31,16 @@ target_sources(bar_lib
src/widgets/volumeWidget.cpp src/widgets/volumeWidget.cpp
src/widgets/webWidget.cpp src/widgets/webWidget.cpp
src/services/todo.cpp
src/services/sqliteTodoAdapter.cpp
src/services/hyprland.cpp src/services/hyprland.cpp
src/services/tray.cpp src/services/tray.cpp
src/services/notifications.cpp src/services/notifications.cpp
src/services/bluetooth.cpp src/services/bluetooth.cpp
src/widgets/tray.cpp src/widgets/tray.cpp
src/widgets/todo.cpp
src/widgets/bluetooth.cpp src/widgets/bluetooth.cpp
src/widgets/controlCenter.cpp src/widgets/controlCenter.cpp
src/components/popover.cpp src/components/popover.cpp
src/components/todoEntry.cpp
src/components/base/button.cpp src/components/base/button.cpp
) )
include_directories(bar_lib PRIVATE include_directories(bar_lib PRIVATE
@@ -51,7 +49,7 @@ include_directories(bar_lib PRIVATE
add_executable(bar main.cpp) add_executable(bar main.cpp)
target_link_libraries(bar bar_lib ${GTKMM_LIBRARIES} ${LAYERSHELL_LIBRARIES} ${WEBKIT_LIBRARIES} ${SQLITE3_LIBRARIES}) target_link_libraries(bar bar_lib ${GTKMM_LIBRARIES} ${LAYERSHELL_LIBRARIES} ${WEBKIT_LIBRARIES} ${CURL_LIBRARIES} nlohmann_json::nlohmann_json)
# Copy `resources/bar.css` into the build directory when it changes # Copy `resources/bar.css` into the build directory when it changes
set(RES_SRC "${CMAKE_CURRENT_SOURCE_DIR}/resources/bar.css") set(RES_SRC "${CMAKE_CURRENT_SOURCE_DIR}/resources/bar.css")

View File

@@ -3,7 +3,6 @@
#include <gtk4-layer-shell/gtk4-layer-shell.h> #include <gtk4-layer-shell/gtk4-layer-shell.h>
#include <gtkmm.h> #include <gtkmm.h>
#include "icons.hpp"
#include "widgets/clock.hpp" #include "widgets/clock.hpp"
#include "widgets/date.hpp" #include "widgets/date.hpp"
#include "widgets/tray.hpp" #include "widgets/tray.hpp"
@@ -26,8 +25,8 @@ class Bar : public Gtk::Window {
Clock clock; Clock clock;
Date date; Date date;
WebWidget homeAssistant{ICON_HOME, "Home Assistant", "https://home.rivercry.com"}; WebWidget homeAssistant{"\ue88a", "Home Assistant", "https://home.rivercry.com"};
ControlCenter controlCenter{"\ue8bb", "Control Center"}; ControlCenter controlCenter{"\ue88a", "Control Center"};
WorkspaceIndicator *workspaceIndicator = nullptr; WorkspaceIndicator *workspaceIndicator = nullptr;
TrayWidget *trayWidget = nullptr; TrayWidget *trayWidget = nullptr;

View File

@@ -1,21 +0,0 @@
#pragma once
#include <sys/stat.h>
#include "gtkmm/box.h"
class TodoEntry : public Gtk::Box {
public:
TodoEntry(int id, std::string text, sigc::signal<void(int)> signal_dismissed, sigc::signal<void(int, std::string)> signal_edited);
int get_id() const { return id; }
std::string get_text() const { return text; }
private:
int id;
std::string text;
sigc::signal<void(int)> signal_dismissed;
sigc::signal<void(int, std::string)> signal_edited;
void on_dismiss_clicked();
};

View File

@@ -0,0 +1,31 @@
#pragma once
#include <array>
#include <memory>
#include <stdexcept>
#include <string>
#include <cstdio>
class CommandHelper {
public:
static std::string exec(const char *cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, int(*)(FILE*)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), static_cast<int>(buffer.size()), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
static void execNoOutput(std::string cmd) {
std::string command = cmd + " > /dev/null 2>&1";
int ret = std::system(command.c_str());
if (ret != 0) {
throw std::runtime_error("Command failed with return code: " + std::to_string(ret));
}
}
};

69
include/helpers/hypr.hpp Normal file
View File

@@ -0,0 +1,69 @@
#pragma once
#include <cassert>
#include <nlohmann/json.hpp>
#include <string>
#include <sys/socket.h>
#include "helpers/command.hpp"
class HyprctlHelper {
public:
static nlohmann::json getMonitorData() {
std::string result = CommandHelper::exec("hyprctl -j monitors");
assert(!result.empty() && "Failed to get monitor data from hyprctl");
auto json = nlohmann::json::parse(result);
assert(json.is_array());
return json;
}
static nlohmann::json getWorkspaceData() {
std::string result = CommandHelper::exec("hyprctl -j workspaces");
assert(!result.empty() && "Failed to get workspace data from hyprctl");
auto json = nlohmann::json::parse(result);
assert(json.is_array());
return json;
}
static nlohmann::json getClientData() {
std::string result = CommandHelper::exec("hyprctl -j clients");
assert(!result.empty() && "Failed to get client data from hyprctl");
auto json = nlohmann::json::parse(result);
assert(json.is_array());
return json;
}
static void dispatchWorkspace(int workspaceNumber) {
std::string out = "hyprctl dispatch workspace " + std::to_string(workspaceNumber);
CommandHelper::execNoOutput(out.c_str());
}
};
class HyprSocketHelper {
public:
static std::string getHyprlandSocketPath() {
const char *hyprlandInstanceSignature = std::getenv("HYPRLAND_INSTANCE_SIGNATURE");
const char *xdgRuntimeDir = std::getenv("XDG_RUNTIME_DIR");
if (!xdgRuntimeDir || !hyprlandInstanceSignature) {
return std::string();
}
std::string basePath = std::string(xdgRuntimeDir) + "/hypr/" + std::string(hyprlandInstanceSignature) + "/";
std::string sock1 = basePath + ".socket2.sock";
if (access(sock1.c_str(), F_OK) == 0) {
return sock1;
}
return std::string();
}
};

View File

@@ -0,0 +1,52 @@
#pragma once
#include <cassert>
#include <iostream>
#include <string>
#include <sys/socket.h>
#include <vector>
#include "helper/string.hpp"
class SocketHelper {
typedef struct SocketMessage {
std::string eventType;
std::string eventData;
} SocketMessage;
public:
static std::vector<SocketMessage> parseSocketMessage(int socketFd, const std::string &delimiter) {
char buffer[4096];
std::string data;
ssize_t bytesRead = recv(socketFd, buffer, sizeof(buffer) - 1, 0);
if (bytesRead > 0) {
buffer[bytesRead] = '\0';
data = std::string(buffer);
} else if (bytesRead == 0) {
std::cerr << "Socket closed by peer" << std::endl;
} else {
std::cerr << "Error reading from socket" << std::endl;
}
auto delimiterPos = data.find(delimiter);
if (delimiterPos == std::string::npos) {
assert(false && "Delimiter not found in socket message");
}
auto splitMessages = StringHelper::split(data, '\n');
auto splitMessagesFinal = std::vector<SocketMessage>();
for (auto splitMessage : splitMessages) {
SocketMessage message;
auto messageCommandVector = StringHelper::split(splitMessage, ">>");
message.eventType = messageCommandVector[0];
message.eventData = messageCommandVector.size() > 1 ? messageCommandVector[1] : "";
splitMessagesFinal.push_back(message);
}
return splitMessagesFinal;
}
};

View File

@@ -0,0 +1,40 @@
#pragma once
#include <string>
#include <vector>
class StringHelper {
public:
static std::vector<std::string> split(const std::string &input, char delimiter) {
std::vector<std::string> tokens;
std::string token;
for (char ch : input) {
if (ch == delimiter) {
if (!token.empty()) {
tokens.push_back(token);
token.clear();
}
} else {
token += ch;
}
}
if (!token.empty()) {
tokens.push_back(token);
}
return tokens;
}
static std::vector<std::string> split(const std::string &input, std::string delimiter) {
std::vector<std::string> tokens;
size_t start = 0;
size_t end = input.find(delimiter);
while (end != std::string::npos) {
tokens.push_back(input.substr(start, end - start));
start = end + delimiter.length();
end = input.find(delimiter, start);
}
tokens.push_back(input.substr(start));
return tokens;
}
};

View File

@@ -0,0 +1,17 @@
#pragma once
#include <fstream>
class SystemHelper {
public:
static std::string read_file_to_string(const std::string &filePath) {
std::ifstream file(filePath);
if (!file.is_open()) {
throw std::runtime_error("Could not open file: " + filePath);
}
std::string content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
file.close();
return content;
}
};

View File

@@ -1,39 +0,0 @@
#pragma once
#include <array>
#include <fstream>
#include <memory>
#include <sstream>
#include <string>
class SystemHelper {
public:
static std::string get_command_output(const char *cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, int (*)(FILE *)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
// Read the output a chunk at a time until the stream ends
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
// Read an entire file into a string. Throws std::runtime_error on failure.
static std::string read_file_to_string(const std::string &path) {
std::ifstream in(path, std::ios::in | std::ios::binary);
if (!in) {
throw std::runtime_error("Failed to open file: " + path);
}
std::ostringstream ss;
ss << in.rdbuf();
return ss.str();
}
};

View File

@@ -1,3 +0,0 @@
#pragma once
#define ICON_HOME "\ue88a"

View File

@@ -13,13 +13,6 @@ class HyprlandService {
public: public:
static constexpr int kWorkspaceSlotCount = 7; static constexpr int kWorkspaceSlotCount = 7;
const char *kMonitorCommand = "hyprctl monitors -j";
const char *kWorkspaceCommand = "hyprctl workspaces -j";
const char *kClientsCommand = "hyprctl clients -j";
struct WindowState {
int hyprId = -1;
};
struct WorkspaceState { struct WorkspaceState {
int hyprId = -1; int hyprId = -1;
@@ -64,6 +57,10 @@ class HyprlandService {
} }
private: private:
const char *kMonitorCommand = "hyprctl monitors -j";
const char *kWorkspaceCommand = "hyprctl workspaces -j";
const char *kClientsCommand = "hyprctl clients -j";
HyprlandService(); HyprlandService();
~HyprlandService(); ~HyprlandService();

View File

@@ -1,28 +0,0 @@
#pragma once
#include <memory>
#include <string>
#include "components/todoEntry.hpp"
#include "services/todoAdapter.hpp"
class TodoService {
public:
TodoService(sigc::signal<void()> refreshSignal);
~TodoService();
std::map<int, TodoEntry *> getTodos();
void init();
void removeTodo(int id);
TodoEntry *addTodo(std::string text, bool emitSignal = true, bool persist = true);
void updateTodo(int id, std::string text);
private:
void load();
int nextId = 1;
std::map<int, TodoEntry *> todos;
sigc::signal<void()> refreshSignal;
std::unique_ptr<ITodoAdapter> adapter;
};

View File

@@ -1,21 +0,0 @@
#pragma once
#include <string>
#include <vector>
struct TodoRecord {
int id;
std::string text;
};
class ITodoAdapter {
public:
virtual ~ITodoAdapter() = default;
virtual bool init() = 0;
virtual std::vector<TodoRecord> listTodos() = 0;
virtual int addTodo(const std::string &text) = 0;
virtual bool removeTodo(int id) = 0;
virtual bool updateTodo(int id, const std::string &text) = 0;
};

View File

@@ -1,21 +0,0 @@
#pragma once
#include <string>
#include "components/popover.hpp"
#include "services/todo.hpp"
class TodoPopover : public Popover {
public:
TodoPopover(std::string icon, std::string title);
void update();
private:
std::string name;
TodoService *todoService = nullptr;
Gtk::Box container;
Gtk::Box inputArea;
Gtk::Box *todoList = nullptr;
};

View File

@@ -136,3 +136,35 @@ button {
opacity: 1; opacity: 1;
} }
} }
.todo-tag-area {
min-height: 40px;
padding: 5px;
}
.tag-button {
background-color: #444444;
color: #ffffff;
padding: 2px 8px;
margin: 2px;
border-radius: 12px;
font-size: 12px;
font-family: "Hack Nerd Font Mono", sans-serif;
min-height: 24px;
min-width: 50px;
}
.tag-button:hover {
background-color: #555555;
}
.tag-button.suggested-action {
background-color: #3498db;
color: #ffffff;
}
.todo-entry-box {
margin-top: 5px;
margin-bottom: 5px;
}

View File

@@ -5,10 +5,9 @@
#include <gtkmm/label.h> #include <gtkmm/label.h>
#include <gtkmm/window.h> #include <gtkmm/window.h>
#include "helpers/systemHelper.hpp" #include "helpers/system.hpp"
#include "widgets/date.hpp" #include "widgets/date.hpp"
#include "widgets/spacer.hpp" #include "widgets/spacer.hpp"
#include "widgets/todo.hpp"
#include "widgets/volumeWidget.hpp" #include "widgets/volumeWidget.hpp"
#include "widgets/workspaceIndicator.hpp" #include "widgets/workspaceIndicator.hpp"
@@ -66,8 +65,6 @@ void Bar::setup_left_box() {
void Bar::setup_center_box() { void Bar::setup_center_box() {
center_box.set_hexpand(false); center_box.set_hexpand(false);
center_box.append(*(new TodoPopover("\uf23a", "To-Do")));
center_box.append(*(new Spacer()));
center_box.append(this->date); center_box.append(this->date);
center_box.append(*(new Spacer())); center_box.append(*(new Spacer()));
center_box.append(this->clock); center_box.append(this->clock);

View File

@@ -2,18 +2,13 @@
#include "sigc++/functors/mem_fun.h" #include "sigc++/functors/mem_fun.h"
Button::Button(const std::string label) : Gtk::Button(label) {
Button::Button(const std::string label) signal_clicked().connect(sigc::mem_fun(*this, &Button::on_clicked));
: Gtk::Button(label) {
signal_clicked().connect(
sigc::mem_fun(*this, &Button::on_clicked));
this->add_css_class("button"); this->add_css_class("button");
} }
Button::Button(Gtk::Image &image) Button::Button(Gtk::Image &image) : Gtk::Button() {
: Gtk::Button() {
set_child(image); set_child(image);
signal_clicked().connect( signal_clicked().connect(sigc::mem_fun(*this, &Button::on_clicked));
sigc::mem_fun(*this, &Button::on_clicked));
this->add_css_class("button"); this->add_css_class("button");
} }

View File

@@ -1,8 +1,5 @@
#include "components/popover.hpp" #include "components/popover.hpp"
#include "gtkmm/label.h"
#include "gtkmm/object.h"
Popover::Popover(const std::string icon, std::string name): Button(icon) { Popover::Popover(const std::string icon, std::string name): Button(icon) {
signal_clicked().connect(sigc::mem_fun(*this, &Popover::on_toggle_window)); signal_clicked().connect(sigc::mem_fun(*this, &Popover::on_toggle_window));

View File

@@ -1,47 +0,0 @@
#include "components/todoEntry.hpp"
#include <gtkmm/label.h>
#include <string>
#include "components/base/button.hpp"
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);
box->set_halign(Gtk::Align::START);
box->set_valign(Gtk::Align::CENTER);
box->set_name("todo-entry-box");
box->add_css_class("todo-entry-box");
append(*box);
auto label = Gtk::make_managed<Gtk::Label>(text);
label->set_halign(Gtk::Align::START);
label->set_valign(Gtk::Align::CENTER);
box->append(*label);
auto buttonBox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL);
buttonBox->set_halign(Gtk::Align::END);
buttonBox->set_valign(Gtk::Align::CENTER);
append(*buttonBox);
auto dismissButton = Gtk::make_managed<Button>("\uf00d");
dismissButton->set_valign(Gtk::Align::CENTER);
dismissButton->set_tooltip_text("Dismiss");
dismissButton->signal_clicked().connect(sigc::mem_fun(*this, &TodoEntry::on_dismiss_clicked));
auto editButton = Gtk::make_managed<Button>("\uf044");
editButton->set_valign(Gtk::Align::CENTER);
buttonBox->append(*editButton);
buttonBox->append(*dismissButton);
}
void TodoEntry::on_dismiss_clicked() {
this->signal_dismissed.emit(this->id);
}

View File

@@ -13,7 +13,8 @@
#include <unordered_set> #include <unordered_set>
#include <unistd.h> #include <unistd.h>
#include "helpers/systemHelper.hpp" #include "helpers/command.hpp"
#include "helpers/hypr.hpp"
HyprlandService::HyprlandService() = default; HyprlandService::HyprlandService() = default;
@@ -23,10 +24,10 @@ HyprlandService::~HyprlandService() {
fd = -1; fd = -1;
} }
// free allocated workspace pointers
for (auto &p : this->workspaces) { for (auto &p : this->workspaces) {
delete p.second; delete p.second;
} }
this->workspaces.clear(); this->workspaces.clear();
this->monitors.clear(); this->monitors.clear();
} }
@@ -67,8 +68,7 @@ void HyprlandService::start() {
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
std::strncpy(addr.sun_path, socket_path.c_str(), sizeof(addr.sun_path) - 1); std::strncpy(addr.sun_path, socket_path.c_str(), sizeof(addr.sun_path) - 1);
if (connect(fd, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)) == if (connect(fd, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)) == -1) {
-1) {
std::cerr << "[Hyprland] Failed to connect to " << socket_path std::cerr << "[Hyprland] Failed to connect to " << socket_path
<< std::endl; << std::endl;
close(fd); close(fd);
@@ -145,15 +145,13 @@ std::string HyprlandService::get_socket_path() {
} }
void HyprlandService::refresh_monitors() { void HyprlandService::refresh_monitors() {
// free any previously allocated WorkspaceState objects before rebuilding
for (auto &p : this->workspaces) { for (auto &p : this->workspaces) {
delete p.second; delete p.second;
} }
this->workspaces.clear(); this->workspaces.clear();
this->monitors.clear(); this->monitors.clear();
std::string output = SystemHelper::get_command_output(kMonitorCommand); auto monitorsJson = HyprctlHelper::getMonitorData();
auto monitorsJson = nlohmann::json::parse(output, nullptr, false);
for (const auto &monitorJson : monitorsJson) { for (const auto &monitorJson : monitorsJson) {
Monitor monitor; Monitor monitor;
@@ -188,35 +186,27 @@ void HyprlandService::refresh_monitors() {
monitorStateChanged.emit(); monitorStateChanged.emit();
} }
/**
* Called every time when workspace changes have been detected.
* Used to Update the internal state for the Workspaces,
*/
void HyprlandService::refresh_workspaces() { void HyprlandService::refresh_workspaces() {
std::string output = SystemHelper::get_command_output(kWorkspaceCommand); auto workspacesJson = HyprctlHelper::getWorkspaceData();
auto workspacesJson = nlohmann::json::parse(output, nullptr, false);
for (auto &[id, ws] : this->workspaces) { for (auto &[id, ws] : this->workspaces) {
ws->focused = false; ws->focused = false;
ws->active = false; ws->active = false;
} }
output = SystemHelper::get_command_output(kMonitorCommand); auto monitorsJson = HyprctlHelper::getMonitorData();
auto monitorsJson = nlohmann::json::parse(output, nullptr, false);
for (const auto &monitorJson : monitorsJson) { for (const auto &monitorJson : monitorsJson) {
const int monitorId = monitorJson.value("id", -1); const int monitorId = monitorJson.value("id", -1);
const int focusedWorkspaceId = monitorJson["activeWorkspace"].value("id", -1); const int focusedWorkspaceId = monitorJson["activeWorkspace"].value("id", -1);
// write into the stored monitor (use reference)
auto it = this->monitors.find(monitorId); auto it = this->monitors.find(monitorId);
if (it != this->monitors.end()) { if (it != this->monitors.end()) {
it->second.focusedWorkspaceId = focusedWorkspaceId; it->second.focusedWorkspaceId = focusedWorkspaceId;
} }
} }
std::string clientsOutput = SystemHelper::get_command_output(kClientsCommand); auto clientsJson = HyprctlHelper::getClientData();
auto clientsJson = nlohmann::json::parse(clientsOutput, nullptr, false);
std::unordered_set<std::string> liveClientAddresses; std::unordered_set<std::string> liveClientAddresses;
for (const auto &clientJson : clientsJson) { for (const auto &clientJson : clientsJson) {
const std::string addr = clientJson.value("address", ""); const std::string addr = clientJson.value("address", "");
@@ -234,21 +224,23 @@ void HyprlandService::refresh_workspaces() {
for (const auto &workspaceJson : workspacesJson) { for (const auto &workspaceJson : workspacesJson) {
const int workspaceId = workspaceJson.value("id", -1); const int workspaceId = workspaceJson.value("id", -1);
auto workspaceStateIt = this->workspaces.find(workspaceId); auto workspaceStateIt = this->workspaces.find(workspaceId);
if (workspaceStateIt == this->workspaces.end()) { if (workspaceStateIt == this->workspaces.end()) {
continue; continue;
} }
WorkspaceState *workspaceState = workspaceStateIt->second; WorkspaceState *workspaceState = workspaceStateIt->second;
auto mit = this->monitors.find(workspaceState->monitorId); auto mit = this->monitors.find(workspaceState->monitorId);
if (mit != this->monitors.end()) { if (mit != this->monitors.end()) {
workspaceState->focused = mit->second.focusedWorkspaceId == workspaceId; workspaceState->focused = mit->second.focusedWorkspaceId == workspaceId;
} else { } else {
workspaceState->focused = false; workspaceState->focused = false;
} }
workspaceState->active = true; workspaceState->active = true;
} }
// drop urgent flags for windows no longer reported by hyprctl clients
for (auto &[id, ws] : this->workspaces) { for (auto &[id, ws] : this->workspaces) {
auto &urgent = ws->urgentWindows; auto &urgent = ws->urgentWindows;
auto newEnd = std::remove_if(urgent.begin(), urgent.end(), [&](const std::string &addr) { auto newEnd = std::remove_if(urgent.begin(), urgent.end(), [&](const std::string &addr) {
@@ -267,7 +259,7 @@ void HyprlandService::switchToWorkspace(int workspaceId) {
"hyprctl dispatch workspace " + std::to_string(workspaceId); "hyprctl dispatch workspace " + std::to_string(workspaceId);
try { try {
(void)SystemHelper::get_command_output(cmd.c_str()); (void)CommandHelper::execNoOutput(cmd.c_str());
} catch (const std::exception &ex) { } catch (const std::exception &ex) {
std::cerr << "[Hyprland] Failed to dispatch workspace command: " std::cerr << "[Hyprland] Failed to dispatch workspace command: "
<< ex.what() << " cmd=" << cmd << std::endl; << ex.what() << " cmd=" << cmd << std::endl;
@@ -275,8 +267,7 @@ void HyprlandService::switchToWorkspace(int workspaceId) {
} }
void HyprlandService::onUrgentEvent(std::string windowAddress) { void HyprlandService::onUrgentEvent(std::string windowAddress) {
std::string output = SystemHelper::get_command_output(kClientsCommand); auto clientsJson = HyprctlHelper::getClientData();
auto clientsJson = nlohmann::json::parse(output, nullptr, false);
for (const auto &clientJson : clientsJson) { for (const auto &clientJson : clientsJson) {
const std::string addr = clientJson.value("address", ""); const std::string addr = clientJson.value("address", "");
@@ -301,8 +292,7 @@ void HyprlandService::onUrgentEvent(std::string windowAddress) {
} }
void HyprlandService::onActiveWindowEvent(std::string windowAddress) { void HyprlandService::onActiveWindowEvent(std::string windowAddress) {
std::string output = SystemHelper::get_command_output(kClientsCommand); auto clientsJson = HyprctlHelper::getClientData();
auto clientsJson = nlohmann::json::parse(output, nullptr, false);
for (const auto &clientJson : clientsJson) { for (const auto &clientJson : clientsJson) {
const std::string addr = clientJson.value("address", ""); const std::string addr = clientJson.value("address", "");

View File

@@ -1,164 +0,0 @@
#include <filesystem>
#include <iostream>
#include <memory>
#include <sqlite3.h>
#include "services/todoAdapter.hpp"
namespace {
std::string getDbPath() {
const char *homeDir = getenv("HOME");
if (!homeDir) {
return "todos.db";
}
std::string path = std::string(homeDir) + "/.config/bar";
if (!std::filesystem::exists(path)) {
std::filesystem::create_directories(path);
}
return path + "/todos.db";
}
class SqliteTodoAdapter final : public ITodoAdapter {
public:
~SqliteTodoAdapter() override {
if (db_) {
sqlite3_close(db_);
db_ = nullptr;
}
}
bool init() override {
std::string dbPath = getDbPath();
int rc = sqlite3_open(dbPath.c_str(), &db_);
if (rc != SQLITE_OK) {
std::cerr << "Can't open database: " << sqlite3_errmsg(db_) << std::endl;
return false;
}
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);
if (rc != SQLITE_OK) {
std::cerr << "SQL error (create table): " << (zErrMsg ? zErrMsg : "") << std::endl;
sqlite3_free(zErrMsg);
return false;
}
return true;
}
std::vector<TodoRecord> listTodos() override {
std::vector<TodoRecord> results;
if (!db_) {
return results;
}
sqlite3_stmt *stmt = nullptr;
const char *sql = "SELECT id, text FROM todos";
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
std::cerr << "Failed to fetch data: " << sqlite3_errmsg(db_) << std::endl;
return results;
}
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
int id = sqlite3_column_int(stmt, 0);
const unsigned char *txt = sqlite3_column_text(stmt, 1);
results.push_back({id, txt ? reinterpret_cast<const char *>(txt) : ""});
}
sqlite3_finalize(stmt);
return results;
}
int addTodo(const std::string &text) override {
if (!db_) {
return -1;
}
sqlite3_stmt *stmt = nullptr;
const char *sql = "INSERT INTO todos (text) VALUES (?1);";
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
std::cerr << "SQL prepare error (insert): " << sqlite3_errmsg(db_) << std::endl;
return -1;
}
sqlite3_bind_text(stmt, 1, text.c_str(), -1, SQLITE_TRANSIENT);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
std::cerr << "SQL step error (insert): " << sqlite3_errmsg(db_) << std::endl;
sqlite3_finalize(stmt);
return -1;
}
sqlite3_finalize(stmt);
return static_cast<int>(sqlite3_last_insert_rowid(db_));
}
bool removeTodo(int id) override {
if (!db_) {
return false;
}
sqlite3_stmt *stmt = nullptr;
const char *sql = "DELETE FROM todos WHERE id = ?1;";
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
std::cerr << "SQL prepare error (delete): " << sqlite3_errmsg(db_) << std::endl;
return false;
}
sqlite3_bind_int(stmt, 1, id);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
std::cerr << "SQL step error (delete): " << sqlite3_errmsg(db_) << std::endl;
sqlite3_finalize(stmt);
return false;
}
sqlite3_finalize(stmt);
return true;
}
bool updateTodo(int id, const std::string &text) override {
if (!db_) {
return false;
}
sqlite3_stmt *stmt = nullptr;
const char *sql = "UPDATE todos SET text = ?1 WHERE id = ?2;";
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
std::cerr << "SQL prepare error (update): " << sqlite3_errmsg(db_) << std::endl;
return false;
}
sqlite3_bind_text(stmt, 1, text.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 2, id);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
std::cerr << "SQL step error (update): " << sqlite3_errmsg(db_) << std::endl;
sqlite3_finalize(stmt);
return false;
}
sqlite3_finalize(stmt);
return true;
}
private:
sqlite3 *db_ = nullptr;
};
} // namespace
// Factory kept local to this TU for now; TodoService uses it.
std::unique_ptr<ITodoAdapter> makeSqliteTodoAdapter() {
return std::make_unique<SqliteTodoAdapter>();
}

View File

@@ -1,128 +0,0 @@
#include "services/todo.hpp"
#include <cassert>
#include <iostream>
#include <gtkmm/object.h>
#include "components/todoEntry.hpp"
std::unique_ptr<ITodoAdapter> makeSqliteTodoAdapter();
TodoService::TodoService(sigc::signal<void()> refreshSignal) {
this->refreshSignal = refreshSignal;
this->adapter = makeSqliteTodoAdapter();
this->init();
}
TodoService::~TodoService() {
this->adapter.reset();
}
std::map<int, TodoEntry *> TodoService::getTodos() {
return this->todos;
}
void TodoService::init() {
if (!this->adapter) {
std::cerr << "Todo adapter not set" << std::endl;
return;
}
if (!this->adapter->init()) {
std::cerr << "Todo adapter init failed" << std::endl;
return;
}
this->load();
}
void TodoService::removeTodo(int id) {
if (todos.find(id) == todos.end()) {
std::cerr << "Todo with id " << id << " not found!" << std::endl;
assert(false);
}
if (this->adapter) {
this->adapter->removeTodo(id);
}
todos.erase(id);
this->refreshSignal.emit();
}
TodoEntry *TodoService::addTodo(std::string text, bool emitSignal, bool persist) {
int id = nextId;
if (persist) {
if (!this->adapter) {
return nullptr;
}
int newId = this->adapter->addTodo(text);
if (newId < 0) {
return nullptr;
}
id = newId;
}
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;
}
if (emitSignal) {
this->refreshSignal.emit();
}
return todo;
}
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;
}
auto rows = this->adapter->listTodos();
int count = 0;
for (const auto &row : rows) {
count++;
int id = row.id;
std::string text = row.text;
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;
}
}
}

View File

@@ -2,23 +2,23 @@
ControlCenter::ControlCenter(std::string icon, std::string name) ControlCenter::ControlCenter(std::string icon, std::string name)
: Popover(icon, name) { : Popover(icon, name) {
this->popover->set_size_request(200, -1); this->popover->set_size_request(200, -1);
set_popover_child(this->container); set_popover_child(this->container);
this->bluetoothWidget = Gtk::make_managed<BluetoothWidget>(); this->bluetoothWidget = Gtk::make_managed<BluetoothWidget>();
this->container.append(*this->bluetoothWidget); this->container.append(*this->bluetoothWidget);
bluetoothService->powerStateChangedSignal.connect( bluetoothService->powerStateChangedSignal.connect(
sigc::mem_fun(*this->bluetoothWidget, &BluetoothWidget::setPowerState)); sigc::mem_fun(*this->bluetoothWidget, &BluetoothWidget::setPowerState));
bluetoothWidget->onPowerStateButtonClickedSignal.connect( bluetoothWidget->onPowerStateButtonClickedSignal.connect(
sigc::mem_fun(*this->bluetoothService, &BluetoothService::togglePowerState)); sigc::mem_fun(*this->bluetoothService, &BluetoothService::togglePowerState));
bluetoothWidget->setPowerState(bluetoothService->getPowerState()); bluetoothWidget->setPowerState(bluetoothService->getPowerState());
bluetoothService->isDiscoveringChangedSignal.connect( bluetoothService->isDiscoveringChangedSignal.connect(
sigc::mem_fun(*this->bluetoothWidget, &BluetoothWidget::setIsDiscovering)); sigc::mem_fun(*this->bluetoothWidget, &BluetoothWidget::setIsDiscovering));
bluetoothWidget->onIsDiscoveringButtonClickedSignal.connect( bluetoothWidget->onIsDiscoveringButtonClickedSignal.connect(
sigc::mem_fun(*this->bluetoothService, &BluetoothService::toggleIsDiscovering)); sigc::mem_fun(*this->bluetoothService, &BluetoothService::toggleIsDiscovering));
bluetoothWidget->setIsDiscovering(bluetoothService->getIsDiscovering()); bluetoothWidget->setIsDiscovering(bluetoothService->getIsDiscovering());
} }

View File

@@ -1,78 +0,0 @@
#include "widgets/todo.hpp"
#include <gtkmm/box.h>
#include <gtkmm/entry.h>
#include <string>
TodoPopover::TodoPopover(std::string icon, std::string title) : Popover(icon, title) {
this->name = title;
this->popover->set_size_request(300, -1);
container.set_orientation(Gtk::Orientation::VERTICAL);
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("");
}
});
container.append(inputArea);
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();
}
void TodoPopover::update() {
auto todos = this->todoService->getTodos();
Gtk::Widget *child = todoList->get_first_child();
while (child) {
Gtk::Widget *next = child->get_next_sibling();
bool found = false;
for (auto &[id, todo] : todos) {
if (child == todo) {
found = true;
break;
}
}
if (!found) {
todoList->remove(*child);
}
child = next;
}
for (auto &[id, todo] : todos) {
if (todo->get_parent() == nullptr) {
todoList->append(*todo);
}
}
}

View File

@@ -5,7 +5,9 @@
#include <regex> #include <regex>
#include <sigc++/functors/mem_fun.h> #include <sigc++/functors/mem_fun.h>
#include "helpers/systemHelper.hpp" #include "helpers/command.hpp"
#include "giomm/applicationcommandline.h"
VolumeWidget::VolumeWidget() : Gtk::Box(Gtk::Orientation::HORIZONTAL) { VolumeWidget::VolumeWidget() : Gtk::Box(Gtk::Orientation::HORIZONTAL) {
set_valign(Gtk::Align::CENTER); set_valign(Gtk::Align::CENTER);
@@ -21,7 +23,7 @@ VolumeWidget::VolumeWidget() : Gtk::Box(Gtk::Orientation::HORIZONTAL) {
click->set_button(GDK_BUTTON_PRIMARY); click->set_button(GDK_BUTTON_PRIMARY);
click->signal_released().connect([this](int, double, double) { click->signal_released().connect([this](int, double, double) {
try { try {
(void)SystemHelper::get_command_output( (void)CommandHelper::exec(
"wpctl set-mute @DEFAULT_SINK@ toggle"); "wpctl set-mute @DEFAULT_SINK@ toggle");
} catch (const std::exception &ex) { } catch (const std::exception &ex) {
std::cerr << "[VolumeWidget] failed to toggle mute: " << ex.what() std::cerr << "[VolumeWidget] failed to toggle mute: " << ex.what()
@@ -44,8 +46,7 @@ VolumeWidget::~VolumeWidget() {
void VolumeWidget::update() { void VolumeWidget::update() {
try { try {
const std::string out = const std::string out = CommandHelper::exec("wpctl get-volume @DEFAULT_SINK@");
SystemHelper::get_command_output("wpctl get-volume @DEFAULT_SINK@");
std::smatch m; std::smatch m;
std::regex r_percent(R"((\d+(?:\.\d+)?)%)"); std::regex r_percent(R"((\d+(?:\.\d+)?)%)");