2020-01-21 17:48:45 +01:00
|
|
|
#include "util/rfkill.hpp"
|
2020-01-26 05:34:31 +01:00
|
|
|
#include <linux/rfkill.h>
|
2020-01-21 17:48:45 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <cstring>
|
|
|
|
#include <fcntl.h>
|
2020-01-23 17:17:29 +01:00
|
|
|
#include <sys/poll.h>
|
2020-01-21 17:48:45 +01:00
|
|
|
#include <cerrno>
|
2020-01-26 05:34:31 +01:00
|
|
|
#include <stdexcept>
|
2020-01-21 17:48:45 +01:00
|
|
|
|
2020-01-23 17:17:29 +01:00
|
|
|
waybar::util::Rfkill::Rfkill(const enum rfkill_type rfkill_type)
|
|
|
|
: rfkill_type_(rfkill_type) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void waybar::util::Rfkill::waitForEvent() {
|
|
|
|
struct rfkill_event event;
|
|
|
|
struct pollfd p;
|
|
|
|
ssize_t len;
|
|
|
|
int fd, n;
|
|
|
|
|
|
|
|
fd = open("/dev/rfkill", O_RDONLY);
|
|
|
|
if (fd < 0) {
|
2020-01-26 05:34:31 +01:00
|
|
|
throw std::runtime_error("Can't open RFKILL control device");
|
2020-01-23 17:17:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&p, 0, sizeof(p));
|
|
|
|
p.fd = fd;
|
|
|
|
p.events = POLLIN | POLLHUP;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
n = poll(&p, 1, -1);
|
|
|
|
if (n < 0) {
|
2020-01-26 05:34:31 +01:00
|
|
|
throw std::runtime_error("Failed to poll RFKILL control device");
|
2020-01-23 17:17:29 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
len = read(fd, &event, sizeof(event));
|
|
|
|
if (len < 0) {
|
2020-01-26 05:34:31 +01:00
|
|
|
throw std::runtime_error("Reading of RFKILL events failed");
|
2020-01-23 17:17:29 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len != RFKILL_EVENT_SIZE_V1) {
|
2020-01-26 05:34:31 +01:00
|
|
|
throw std::runtime_error("Wrong size of RFKILL event");
|
2020-01-23 17:17:29 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-26 05:34:31 +01:00
|
|
|
if(event.type == rfkill_type_ && event.op == RFKILL_OP_CHANGE) {
|
2020-01-23 17:17:29 +01:00
|
|
|
state_ = event.soft || event.hard;
|
2020-01-26 05:34:31 +01:00
|
|
|
break;
|
2020-01-23 17:17:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-26 05:34:31 +01:00
|
|
|
int waybar::util::Rfkill::getState() const {
|
2020-01-23 17:17:29 +01:00
|
|
|
return state_;
|
|
|
|
}
|