-
-
Notifications
You must be signed in to change notification settings - Fork 689
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1015 from alebastr/rfkill-events
rfkill code refactoring
- Loading branch information
Showing
7 changed files
with
67 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
#pragma once | ||
|
||
#include <glibmm/iochannel.h> | ||
#include <linux/rfkill.h> | ||
#include <sigc++/signal.h> | ||
#include <sigc++/trackable.h> | ||
|
||
namespace waybar::util { | ||
|
||
class Rfkill { | ||
class Rfkill : public sigc::trackable { | ||
public: | ||
Rfkill(enum rfkill_type rfkill_type); | ||
~Rfkill() = default; | ||
void waitForEvent(); | ||
~Rfkill(); | ||
bool getState() const; | ||
|
||
sigc::signal<void(struct rfkill_event&)> on_update; | ||
|
||
private: | ||
enum rfkill_type rfkill_type_; | ||
int state_ = 0; | ||
bool state_ = false; | ||
int fd_ = -1; | ||
|
||
bool on_event(Glib::IOCondition cond); | ||
}; | ||
|
||
} // namespace waybar::util |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,25 @@ | ||
#include "modules/bluetooth.hpp" | ||
#include "util/rfkill.hpp" | ||
#include <linux/rfkill.h> | ||
#include <time.h> | ||
|
||
#include <fmt/format.h> | ||
|
||
waybar::modules::Bluetooth::Bluetooth(const std::string& id, const Json::Value& config) | ||
: ALabel(config, "bluetooth", id, "{icon}", 10), | ||
status_("disabled"), | ||
rfkill_{RFKILL_TYPE_BLUETOOTH} { | ||
thread_ = [this] { | ||
dp.emit(); | ||
rfkill_.waitForEvent(); | ||
}; | ||
intervall_thread_ = [this] { | ||
auto now = std::chrono::system_clock::now(); | ||
auto timeout = std::chrono::floor<std::chrono::seconds>(now + interval_); | ||
auto diff = std::chrono::seconds(timeout.time_since_epoch().count() % interval_.count()); | ||
thread_.sleep_until(timeout - diff); | ||
dp.emit(); | ||
}; | ||
: ALabel(config, "bluetooth", id, "{icon}", 10), rfkill_{RFKILL_TYPE_BLUETOOTH} { | ||
rfkill_.on_update.connect(sigc::hide(sigc::mem_fun(*this, &Bluetooth::update))); | ||
} | ||
|
||
auto waybar::modules::Bluetooth::update() -> void { | ||
if (rfkill_.getState()) { | ||
status_ = "disabled"; | ||
} else { | ||
status_ = "enabled"; | ||
} | ||
std::string status = rfkill_.getState() ? "disabled" : "enabled"; | ||
|
||
label_.set_markup( | ||
fmt::format( | ||
format_, | ||
fmt::arg("status", status_), | ||
fmt::arg("icon", getIcon(0, status_)))); | ||
fmt::format(format_, fmt::arg("status", status), fmt::arg("icon", getIcon(0, status)))); | ||
|
||
if (tooltipEnabled()) { | ||
if (config_["tooltip-format"].isString()) { | ||
auto tooltip_format = config_["tooltip-format"].asString(); | ||
auto tooltip_text = fmt::format(tooltip_format, status_); | ||
auto tooltip_text = fmt::format(tooltip_format, status); | ||
label_.set_tooltip_text(tooltip_text); | ||
} else { | ||
label_.set_tooltip_text(status_); | ||
label_.set_tooltip_text(status); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters