mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
feat: init repo
This commit is contained in:
49
src/modules/battery.cpp
Normal file
49
src/modules/battery.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
#include "modules/battery.hpp"
|
||||
|
||||
waybar::modules::Battery::Battery()
|
||||
{
|
||||
try {
|
||||
for (auto &node : fs::directory_iterator(_data_dir)) {
|
||||
if (fs::is_directory(node) && fs::exists(node / "charge_now") &&
|
||||
fs::exists(node / "charge_full")) {
|
||||
_batteries.push_back(node);
|
||||
}
|
||||
}
|
||||
} catch (fs::filesystem_error &e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
|
||||
_label.get_style_context()->add_class("battery-status");
|
||||
|
||||
_thread = [this] {
|
||||
update();
|
||||
_thread.sleep_for(chrono::minutes(1));
|
||||
};
|
||||
}
|
||||
|
||||
auto waybar::modules::Battery::update() -> void
|
||||
{
|
||||
try {
|
||||
for (auto &bat : _batteries) {
|
||||
int full, now;
|
||||
std::string status;
|
||||
std::ifstream(bat / "charge_now") >> now;
|
||||
std::ifstream(bat / "charge_full") >> full;
|
||||
std::ifstream(bat / "status") >> status;
|
||||
if (status == "Charging") {
|
||||
_label.get_style_context()->add_class("battery-charging");
|
||||
} else {
|
||||
_label.get_style_context()->remove_class("battery-charging");
|
||||
}
|
||||
int pct = float(now) / float(full) * 100.f;
|
||||
_label.set_text_with_mnemonic(fmt::format("{}% {}", pct, ""));
|
||||
}
|
||||
} catch (std::exception &e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
waybar::modules::Battery::operator Gtk::Widget &()
|
||||
{
|
||||
return _label;
|
||||
}
|
20
src/modules/clock.cpp
Normal file
20
src/modules/clock.cpp
Normal file
@ -0,0 +1,20 @@
|
||||
#include "modules/clock.hpp"
|
||||
|
||||
waybar::modules::Clock::Clock()
|
||||
{
|
||||
_label.get_style_context()->add_class("clock-widget");
|
||||
_thread = [this] {
|
||||
auto now = waybar::chrono::clock::now();
|
||||
auto t = std::time(nullptr);
|
||||
auto localtime = std::localtime(&t);
|
||||
_label.set_text(
|
||||
fmt::format("{:02}:{:02}", localtime->tm_hour, localtime->tm_min));
|
||||
auto timeout =
|
||||
std::chrono::floor<std::chrono::minutes>(now + std::chrono::minutes(1));
|
||||
_thread.sleep_until(timeout);
|
||||
};
|
||||
};
|
||||
|
||||
waybar::modules::Clock::operator Gtk::Widget &() {
|
||||
return _label;
|
||||
}
|
83
src/modules/workspaces.cpp
Normal file
83
src/modules/workspaces.cpp
Normal file
@ -0,0 +1,83 @@
|
||||
#include "modules/workspaces.hpp"
|
||||
#include "ipc/client.hpp"
|
||||
|
||||
waybar::modules::WorkspaceSelector::WorkspaceSelector(Bar &bar)
|
||||
: _bar(bar), _box(Gtk::manage(new Gtk::Box))
|
||||
{
|
||||
_box->get_style_context()->add_class("workspace-selector");
|
||||
std::string socketPath = get_socketpath();
|
||||
_ipcSocketfd = ipc_open_socket(socketPath);
|
||||
_ipcEventSocketfd = ipc_open_socket(socketPath);
|
||||
const char *subscribe = "[ \"workspace\", \"mode\" ]";
|
||||
uint32_t len = strlen(subscribe);
|
||||
ipc_single_command(_ipcEventSocketfd, IPC_SUBSCRIBE, subscribe, &len);
|
||||
_thread = [this] {
|
||||
update();
|
||||
};
|
||||
}
|
||||
|
||||
auto waybar::modules::WorkspaceSelector::update() -> void
|
||||
{
|
||||
Json::Value workspaces = _getWorkspaces();
|
||||
for (auto it = _buttons.begin(); it != _buttons.end(); ++it) {
|
||||
auto ws = std::find_if(workspaces.begin(), workspaces.end(),
|
||||
[it](auto node) -> bool { return node["num"].asInt() == it->first; });
|
||||
if (ws == workspaces.end()) {
|
||||
it->second.hide();
|
||||
}
|
||||
}
|
||||
for (auto node : workspaces) {
|
||||
auto it = _buttons.find(node["num"].asInt());
|
||||
if (it == _buttons.end()) {
|
||||
_addWorkspace(node);
|
||||
} else {
|
||||
auto styleContext = it->second.get_style_context();
|
||||
bool isCurrent = node["focused"].asBool();
|
||||
if (styleContext->has_class("current") && !isCurrent) {
|
||||
styleContext->remove_class("current");
|
||||
} else if (!styleContext->has_class("current") && isCurrent) {
|
||||
styleContext->add_class("current");
|
||||
}
|
||||
it->second.show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void waybar::modules::WorkspaceSelector::_addWorkspace(Json::Value node)
|
||||
{
|
||||
auto pair = _buttons.emplace(node["num"].asInt(), node["name"].asString());
|
||||
auto &button = pair.first->second;
|
||||
button.set_relief(Gtk::RELIEF_NONE);
|
||||
button.signal_clicked().connect([this, pair] {
|
||||
auto value = fmt::format("workspace \"{}\"", pair.first->first);
|
||||
uint32_t size = value.size();
|
||||
ipc_single_command(_ipcSocketfd, IPC_COMMAND, value.c_str(), &size);
|
||||
});
|
||||
_box->pack_start(button, false, false, 0);
|
||||
if (node["focused"].asBool()) {
|
||||
button.get_style_context()->add_class("current");
|
||||
}
|
||||
button.show();
|
||||
}
|
||||
|
||||
Json::Value waybar::modules::WorkspaceSelector::_getWorkspaces()
|
||||
{
|
||||
uint32_t len = 0;
|
||||
Json::Value root;
|
||||
Json::CharReaderBuilder builder;
|
||||
Json::CharReader* reader = builder.newCharReader();
|
||||
std::string err;
|
||||
std::string str = ipc_single_command(_ipcSocketfd, IPC_GET_WORKSPACES,
|
||||
nullptr, &len);
|
||||
bool res = reader->parse(str.c_str(), str.c_str() + str.size(), &root, &err);
|
||||
delete reader;
|
||||
if (!res) {
|
||||
std::cerr << err << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
waybar::modules::WorkspaceSelector::operator Gtk::Widget &() {
|
||||
return *_box;
|
||||
}
|
Reference in New Issue
Block a user