2019-06-15 12:57:52 +00:00
|
|
|
#include "AModule.hpp"
|
2022-04-06 06:37:19 +00:00
|
|
|
|
2019-06-15 12:57:52 +00:00
|
|
|
#include <fmt/format.h>
|
2022-04-06 06:37:19 +00:00
|
|
|
|
2019-06-15 12:57:52 +00:00
|
|
|
#include <util/command.hpp>
|
|
|
|
|
|
|
|
namespace waybar {
|
|
|
|
|
|
|
|
AModule::AModule(const Json::Value& config, const std::string& name, const std::string& id,
|
|
|
|
bool enable_click, bool enable_scroll)
|
2022-04-06 06:37:19 +00:00
|
|
|
: name_(std::move(name)),
|
|
|
|
config_(std::move(config)),
|
2023-11-10 14:57:26 +00:00
|
|
|
isTooltip{config_["tooltip"].isBool() ? config_["tooltip"].asBool() : true},
|
2022-04-06 06:37:19 +00:00
|
|
|
distance_scrolled_y_(0.0),
|
|
|
|
distance_scrolled_x_(0.0) {
|
2023-03-01 20:39:36 +00:00
|
|
|
// Configure module action Map
|
|
|
|
const Json::Value actions{config_["actions"]};
|
2024-04-24 22:15:40 +00:00
|
|
|
|
2023-03-01 20:39:36 +00:00
|
|
|
for (Json::Value::const_iterator it = actions.begin(); it != actions.end(); ++it) {
|
|
|
|
if (it.key().isString() && it->isString())
|
|
|
|
if (eventActionMap_.count(it.key().asString()) == 0) {
|
|
|
|
eventActionMap_.insert({it.key().asString(), it->asString()});
|
|
|
|
enable_click = true;
|
|
|
|
enable_scroll = true;
|
|
|
|
} else
|
|
|
|
spdlog::warn("Dublicate action is ignored: {0}", it.key().asString());
|
|
|
|
else
|
|
|
|
spdlog::warn("Wrong actions section configuration. See config by index: {}", it.index());
|
|
|
|
}
|
|
|
|
|
2024-04-17 20:23:59 +00:00
|
|
|
event_box_.signal_enter_notify_event().connect(sigc::mem_fun(*this, &AModule::handleMouseEnter));
|
|
|
|
event_box_.signal_leave_notify_event().connect(sigc::mem_fun(*this, &AModule::handleMouseLeave));
|
|
|
|
|
2019-06-15 12:57:52 +00:00
|
|
|
// configure events' user commands
|
2024-04-24 22:15:40 +00:00
|
|
|
// hasUserEvents is true if any element from eventMap_ is satisfying the condition in the lambda
|
|
|
|
bool hasUserEvents =
|
2023-08-16 15:11:44 +00:00
|
|
|
std::find_if(eventMap_.cbegin(), eventMap_.cend(), [&config](const auto& eventEntry) {
|
2023-08-16 15:33:36 +00:00
|
|
|
// True if there is any non-release type event
|
|
|
|
return eventEntry.first.second != GdkEventType::GDK_BUTTON_RELEASE &&
|
|
|
|
config[eventEntry.second].isString();
|
2023-08-16 15:11:44 +00:00
|
|
|
}) != eventMap_.cend();
|
|
|
|
|
2024-04-24 22:15:40 +00:00
|
|
|
if (enable_click || hasUserEvents) {
|
|
|
|
hasUserEvents_ = true;
|
2019-06-15 12:57:52 +00:00
|
|
|
event_box_.add_events(Gdk::BUTTON_PRESS_MASK);
|
|
|
|
event_box_.signal_button_press_event().connect(sigc::mem_fun(*this, &AModule::handleToggle));
|
2024-04-24 22:15:40 +00:00
|
|
|
} else {
|
|
|
|
hasUserEvents_ = false;
|
2019-06-15 12:57:52 +00:00
|
|
|
}
|
2023-08-16 15:14:49 +00:00
|
|
|
|
|
|
|
bool hasReleaseEvent =
|
|
|
|
std::find_if(eventMap_.cbegin(), eventMap_.cend(), [&config](const auto& eventEntry) {
|
2023-08-16 15:33:36 +00:00
|
|
|
// True if there is any non-release type event
|
|
|
|
return eventEntry.first.second == GdkEventType::GDK_BUTTON_RELEASE &&
|
|
|
|
config[eventEntry.second].isString();
|
2023-08-16 15:14:49 +00:00
|
|
|
}) != eventMap_.cend();
|
|
|
|
if (hasReleaseEvent) {
|
|
|
|
event_box_.add_events(Gdk::BUTTON_RELEASE_MASK);
|
|
|
|
event_box_.signal_button_release_event().connect(sigc::mem_fun(*this, &AModule::handleRelease));
|
|
|
|
}
|
2024-03-08 20:48:27 +00:00
|
|
|
if (config_["on-scroll-up"].isString() || config_["on-scroll-down"].isString() ||
|
|
|
|
config_["on-scroll-left"].isString() || config_["on-scroll-right"].isString() ||
|
|
|
|
enable_scroll) {
|
2019-06-15 12:57:52 +00:00
|
|
|
event_box_.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
|
|
|
|
event_box_.signal_scroll_event().connect(sigc::mem_fun(*this, &AModule::handleScroll));
|
2022-04-06 06:37:19 +00:00
|
|
|
}
|
2019-06-15 12:57:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AModule::~AModule() {
|
|
|
|
for (const auto& pid : pid_) {
|
|
|
|
if (pid != -1) {
|
2020-05-27 07:10:38 +00:00
|
|
|
killpg(pid, SIGTERM);
|
2019-06-15 12:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto AModule::update() -> void {
|
2020-04-12 16:30:21 +00:00
|
|
|
// Run user-provided update handler if configured
|
|
|
|
if (config_["on-update"].isString()) {
|
|
|
|
pid_.push_back(util::command::forkExec(config_["on-update"].asString()));
|
|
|
|
}
|
2019-06-15 12:57:52 +00:00
|
|
|
}
|
2023-02-28 12:32:28 +00:00
|
|
|
// Get mapping between event name and module action name
|
|
|
|
// Then call overrided doAction in order to call appropriate module action
|
|
|
|
auto AModule::doAction(const std::string& name) -> void {
|
|
|
|
if (!name.empty()) {
|
|
|
|
const std::map<std::string, std::string>::const_iterator& recA{eventActionMap_.find(name)};
|
|
|
|
// Call overrided action if derrived class has implemented it
|
|
|
|
if (recA != eventActionMap_.cend() && name != recA->second) this->doAction(recA->second);
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 12:57:52 +00:00
|
|
|
|
2024-04-24 22:15:40 +00:00
|
|
|
void AModule::setCursor(Gdk::CursorType c) {
|
|
|
|
auto cursor = Gdk::Cursor::create(Gdk::HAND2);
|
|
|
|
auto gdk_window = event_box_.get_window();
|
|
|
|
gdk_window->set_cursor(cursor);
|
|
|
|
}
|
|
|
|
|
2024-04-17 20:23:59 +00:00
|
|
|
bool AModule::handleMouseEnter(GdkEventCrossing* const& e) {
|
|
|
|
if (auto* module = event_box_.get_child(); module != nullptr) {
|
|
|
|
module->set_state_flags(Gtk::StateFlags::STATE_FLAG_PRELIGHT);
|
|
|
|
}
|
2024-04-24 22:16:15 +00:00
|
|
|
|
2024-04-24 22:15:40 +00:00
|
|
|
if (hasUserEvents_) {
|
|
|
|
setCursor(Gdk::HAND2);
|
|
|
|
}
|
2024-04-23 14:18:54 +00:00
|
|
|
return false;
|
2024-04-17 20:23:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AModule::handleMouseLeave(GdkEventCrossing* const& e) {
|
|
|
|
if (auto* module = event_box_.get_child(); module != nullptr) {
|
|
|
|
module->unset_state_flags(Gtk::StateFlags::STATE_FLAG_PRELIGHT);
|
|
|
|
}
|
2024-04-24 22:16:15 +00:00
|
|
|
|
2024-04-24 22:15:40 +00:00
|
|
|
if (hasUserEvents_) {
|
|
|
|
setCursor(Gdk::ARROW);
|
|
|
|
}
|
2024-04-23 14:18:54 +00:00
|
|
|
return false;
|
2024-04-17 20:23:59 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 15:33:36 +00:00
|
|
|
bool AModule::handleToggle(GdkEventButton* const& e) { return handleUserEvent(e); }
|
2023-08-16 15:14:49 +00:00
|
|
|
|
2023-08-16 15:33:36 +00:00
|
|
|
bool AModule::handleRelease(GdkEventButton* const& e) { return handleUserEvent(e); }
|
2023-08-16 15:14:49 +00:00
|
|
|
|
|
|
|
bool AModule::handleUserEvent(GdkEventButton* const& e) {
|
2023-02-28 12:32:28 +00:00
|
|
|
std::string format{};
|
2022-04-06 06:37:19 +00:00
|
|
|
const std::map<std::pair<uint, GdkEventType>, std::string>::const_iterator& rec{
|
|
|
|
eventMap_.find(std::pair(e->button, e->type))};
|
2024-04-24 22:15:40 +00:00
|
|
|
|
2023-02-28 12:32:28 +00:00
|
|
|
if (rec != eventMap_.cend()) {
|
|
|
|
// First call module actions
|
|
|
|
this->AModule::doAction(rec->second);
|
2022-03-29 09:26:05 +00:00
|
|
|
|
2023-02-28 12:32:28 +00:00
|
|
|
format = rec->second;
|
|
|
|
}
|
|
|
|
// Second call user scripts
|
2022-03-29 09:26:05 +00:00
|
|
|
if (!format.empty()) {
|
2022-04-06 06:37:19 +00:00
|
|
|
if (config_[format].isString())
|
|
|
|
format = config_[format].asString();
|
|
|
|
else
|
|
|
|
format.clear();
|
2019-06-15 12:57:52 +00:00
|
|
|
}
|
|
|
|
if (!format.empty()) {
|
2019-10-16 17:37:12 +00:00
|
|
|
pid_.push_back(util::command::forkExec(format));
|
2019-06-15 12:57:52 +00:00
|
|
|
}
|
|
|
|
dp.emit();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
AModule::SCROLL_DIR AModule::getScrollDir(GdkEventScroll* e) {
|
2023-06-12 04:55:28 +00:00
|
|
|
// only affects up/down
|
|
|
|
bool reverse = config_["reverse-scrolling"].asBool();
|
2023-07-01 15:35:37 +00:00
|
|
|
bool reverse_mouse = config_["reverse-mouse-scrolling"].asBool();
|
2023-06-12 04:55:28 +00:00
|
|
|
|
2023-07-01 06:53:20 +00:00
|
|
|
// ignore reverse-scrolling if event comes from a mouse wheel
|
2023-07-03 22:26:16 +00:00
|
|
|
GdkDevice* device = gdk_event_get_source_device((GdkEvent*)e);
|
2023-07-01 06:53:20 +00:00
|
|
|
if (device != NULL && gdk_device_get_source(device) == GDK_SOURCE_MOUSE) {
|
2023-07-01 15:35:37 +00:00
|
|
|
reverse = reverse_mouse;
|
2023-07-01 06:53:20 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 06:37:19 +00:00
|
|
|
switch (e->direction) {
|
|
|
|
case GDK_SCROLL_UP:
|
2023-06-12 04:55:28 +00:00
|
|
|
return reverse ? SCROLL_DIR::DOWN : SCROLL_DIR::UP;
|
2022-04-06 06:37:19 +00:00
|
|
|
case GDK_SCROLL_DOWN:
|
2023-06-12 04:55:28 +00:00
|
|
|
return reverse ? SCROLL_DIR::UP : SCROLL_DIR::DOWN;
|
2022-04-06 06:37:19 +00:00
|
|
|
case GDK_SCROLL_LEFT:
|
|
|
|
return SCROLL_DIR::LEFT;
|
|
|
|
case GDK_SCROLL_RIGHT:
|
|
|
|
return SCROLL_DIR::RIGHT;
|
2019-06-17 18:37:37 +00:00
|
|
|
case GDK_SCROLL_SMOOTH: {
|
|
|
|
SCROLL_DIR dir{SCROLL_DIR::NONE};
|
2019-06-17 18:29:37 +00:00
|
|
|
|
2019-06-17 18:37:37 +00:00
|
|
|
distance_scrolled_y_ += e->delta_y;
|
|
|
|
distance_scrolled_x_ += e->delta_x;
|
2019-06-16 11:17:34 +00:00
|
|
|
|
2019-06-17 18:37:37 +00:00
|
|
|
gdouble threshold = 0;
|
|
|
|
if (config_["smooth-scrolling-threshold"].isNumeric()) {
|
|
|
|
threshold = config_["smooth-scrolling-threshold"].asDouble();
|
|
|
|
}
|
2019-06-15 12:57:52 +00:00
|
|
|
|
2019-06-17 18:37:37 +00:00
|
|
|
if (distance_scrolled_y_ < -threshold) {
|
2023-07-01 08:09:47 +00:00
|
|
|
dir = reverse ? SCROLL_DIR::DOWN : SCROLL_DIR::UP;
|
2019-06-17 18:37:37 +00:00
|
|
|
} else if (distance_scrolled_y_ > threshold) {
|
2023-07-01 08:09:47 +00:00
|
|
|
dir = reverse ? SCROLL_DIR::UP : SCROLL_DIR::DOWN;
|
2019-06-17 18:37:37 +00:00
|
|
|
} else if (distance_scrolled_x_ > threshold) {
|
|
|
|
dir = SCROLL_DIR::RIGHT;
|
|
|
|
} else if (distance_scrolled_x_ < -threshold) {
|
|
|
|
dir = SCROLL_DIR::LEFT;
|
|
|
|
}
|
2019-06-16 11:17:34 +00:00
|
|
|
|
2019-06-17 18:41:18 +00:00
|
|
|
switch (dir) {
|
|
|
|
case SCROLL_DIR::UP:
|
|
|
|
case SCROLL_DIR::DOWN:
|
|
|
|
distance_scrolled_y_ = 0;
|
|
|
|
break;
|
|
|
|
case SCROLL_DIR::LEFT:
|
|
|
|
case SCROLL_DIR::RIGHT:
|
|
|
|
distance_scrolled_x_ = 0;
|
|
|
|
break;
|
|
|
|
case SCROLL_DIR::NONE:
|
|
|
|
break;
|
2019-06-17 18:37:37 +00:00
|
|
|
}
|
2019-06-17 18:41:18 +00:00
|
|
|
|
2019-06-17 18:37:37 +00:00
|
|
|
return dir;
|
2019-06-15 12:57:52 +00:00
|
|
|
}
|
2019-06-17 18:37:37 +00:00
|
|
|
// Silence -Wreturn-type:
|
2022-04-06 06:37:19 +00:00
|
|
|
default:
|
|
|
|
return SCROLL_DIR::NONE;
|
2019-06-15 12:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AModule::handleScroll(GdkEventScroll* e) {
|
|
|
|
auto dir = getScrollDir(e);
|
2023-02-28 12:32:28 +00:00
|
|
|
std::string eventName{};
|
|
|
|
|
|
|
|
if (dir == SCROLL_DIR::UP)
|
|
|
|
eventName = "on-scroll-up";
|
|
|
|
else if (dir == SCROLL_DIR::DOWN)
|
|
|
|
eventName = "on-scroll-down";
|
2024-03-08 20:48:27 +00:00
|
|
|
else if (dir == SCROLL_DIR::LEFT)
|
|
|
|
eventName = "on-scroll-left";
|
|
|
|
else if (dir == SCROLL_DIR::RIGHT)
|
|
|
|
eventName = "on-scroll-right";
|
2023-02-28 12:32:28 +00:00
|
|
|
|
|
|
|
// First call module actions
|
|
|
|
this->AModule::doAction(eventName);
|
|
|
|
// Second call user scripts
|
|
|
|
if (config_[eventName].isString())
|
|
|
|
pid_.push_back(util::command::forkExec(config_[eventName].asString()));
|
|
|
|
|
2019-06-15 12:57:52 +00:00
|
|
|
dp.emit();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-10 14:57:26 +00:00
|
|
|
bool AModule::tooltipEnabled() { return isTooltip; }
|
2019-06-15 12:57:52 +00:00
|
|
|
|
|
|
|
AModule::operator Gtk::Widget&() { return event_box_; }
|
|
|
|
|
|
|
|
} // namespace waybar
|