2020-01-30 00:25:37 +01: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 09:10:38 +02:00
|
|
|
*
|
2020-01-30 00:25:37 +01: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 09:10:38 +02:00
|
|
|
*
|
2020-01-30 00:25:37 +01: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 17:48:45 +01:00
|
|
|
#include "util/rfkill.hpp"
|
2020-05-27 09:10:38 +02:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
2020-01-26 05:34:31 +01:00
|
|
|
#include <linux/rfkill.h>
|
2020-09-06 21:48:42 +02:00
|
|
|
#include <poll.h>
|
2020-01-21 17:48:45 +01:00
|
|
|
#include <stdlib.h>
|
2020-05-27 09:10:38 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-01-21 17:48:45 +01:00
|
|
|
#include <cerrno>
|
2020-05-27 09:10:38 +02:00
|
|
|
#include <cstring>
|
2020-01-26 05:34:31 +01:00
|
|
|
#include <stdexcept>
|
2020-01-21 17:48:45 +01:00
|
|
|
|
2020-05-27 09:10:38 +02:00
|
|
|
waybar::util::Rfkill::Rfkill(const enum rfkill_type rfkill_type) : rfkill_type_(rfkill_type) {}
|
2020-01-23 17:17:29 +01:00
|
|
|
|
|
|
|
void waybar::util::Rfkill::waitForEvent() {
|
|
|
|
struct rfkill_event event;
|
2020-05-27 09:10:38 +02:00
|
|
|
struct pollfd p;
|
|
|
|
ssize_t len;
|
|
|
|
int fd, n;
|
2020-01-23 17:17:29 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-27 09:10:38 +02:00
|
|
|
if (n == 0) continue;
|
2020-01-23 17:17:29 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-02 03:50:45 +01:00
|
|
|
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-05-27 09:10:38 +02: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);
|
|
|
|
}
|
|
|
|
|
2020-05-27 09:10:38 +02:00
|
|
|
bool waybar::util::Rfkill::getState() const { return state_; }
|