2020-01-29 23:25:37 +00:00
|
|
|
/* https://git.kernel.org/pub/scm/linux/kernel/git/jberg/rfkill.git/
|
|
|
|
*
|
|
|
|
* Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
|
|
|
|
* Copyright 2009 Marcel Holtmann <marcel@holtmann.org>
|
|
|
|
* Copyright 2009 Tim Gardner <tim.gardner@canonical.com>
|
2020-05-27 07:10:38 +00:00
|
|
|
*
|
2020-01-29 23:25:37 +00:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2020-05-27 07:10:38 +00:00
|
|
|
*
|
2020-01-29 23:25:37 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
2020-01-21 16:48:45 +00:00
|
|
|
#include "util/rfkill.hpp"
|
2020-05-27 07:10:38 +00:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
2021-02-03 03:17:06 +00:00
|
|
|
#include <glibmm/main.h>
|
2020-01-26 04:34:31 +00:00
|
|
|
#include <linux/rfkill.h>
|
2021-02-03 03:17:06 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
2020-05-27 07:10:38 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-01-21 16:48:45 +00:00
|
|
|
#include <cerrno>
|
|
|
|
|
2021-02-03 03:17:06 +00:00
|
|
|
waybar::util::Rfkill::Rfkill(const enum rfkill_type rfkill_type) : rfkill_type_(rfkill_type) {
|
|
|
|
fd_ = open("/dev/rfkill", O_RDONLY);
|
|
|
|
if (fd_ < 0) {
|
|
|
|
spdlog::error("Can't open RFKILL control device");
|
2020-01-23 16:17:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-02-03 03:17:06 +00:00
|
|
|
int rc = fcntl(fd_, F_SETFL, O_NONBLOCK);
|
|
|
|
if (rc < 0) {
|
|
|
|
spdlog::error("Can't set RFKILL control device to non-blocking: {}", errno);
|
|
|
|
close(fd_);
|
|
|
|
fd_ = -1;
|
|
|
|
return;
|
|
|
|
}
|
2022-04-06 06:37:19 +00:00
|
|
|
Glib::signal_io().connect(sigc::mem_fun(*this, &Rfkill::on_event), fd_,
|
|
|
|
Glib::IO_IN | Glib::IO_ERR | Glib::IO_HUP);
|
2021-02-03 03:17:06 +00:00
|
|
|
}
|
2020-01-23 16:17:29 +00:00
|
|
|
|
2021-02-03 03:17:06 +00:00
|
|
|
waybar::util::Rfkill::~Rfkill() {
|
|
|
|
if (fd_ >= 0) {
|
|
|
|
close(fd_);
|
|
|
|
}
|
|
|
|
}
|
2020-01-23 16:17:29 +00:00
|
|
|
|
2021-02-03 03:17:06 +00:00
|
|
|
bool waybar::util::Rfkill::on_event(Glib::IOCondition cond) {
|
|
|
|
if (cond & Glib::IO_IN) {
|
|
|
|
struct rfkill_event event;
|
2022-04-06 06:37:19 +00:00
|
|
|
ssize_t len;
|
2020-01-23 16:17:29 +00:00
|
|
|
|
2021-02-03 03:17:06 +00:00
|
|
|
len = read(fd_, &event, sizeof(event));
|
2020-01-23 16:17:29 +00:00
|
|
|
if (len < 0) {
|
2021-02-10 16:22:22 +00:00
|
|
|
if (errno == EAGAIN) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-03 03:17:06 +00:00
|
|
|
spdlog::error("Reading of RFKILL events failed: {}", errno);
|
|
|
|
return false;
|
2020-01-23 16:17:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-01 08:05:41 +00:00
|
|
|
if (static_cast<size_t>(len) < RFKILL_EVENT_SIZE_V1) {
|
2021-02-10 16:22:22 +00:00
|
|
|
spdlog::error("Wrong size of RFKILL event: {} < {}", len, RFKILL_EVENT_SIZE_V1);
|
2021-02-03 03:17:06 +00:00
|
|
|
return true;
|
2020-01-23 16:17:29 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 03:17:06 +00:00
|
|
|
if (event.type == rfkill_type_ && (event.op == RFKILL_OP_ADD || event.op == RFKILL_OP_CHANGE)) {
|
2020-01-23 16:17:29 +00:00
|
|
|
state_ = event.soft || event.hard;
|
2021-02-03 03:17:06 +00:00
|
|
|
on_update.emit(event);
|
2020-01-23 16:17:29 +00:00
|
|
|
}
|
2021-02-03 03:17:06 +00:00
|
|
|
return true;
|
2020-01-23 16:17:29 +00:00
|
|
|
}
|
2023-07-01 08:05:41 +00:00
|
|
|
spdlog::error("Failed to poll RFKILL control device");
|
|
|
|
return false;
|
2020-01-23 16:17:29 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 07:10:38 +00:00
|
|
|
bool waybar::util::Rfkill::getState() const { return state_; }
|