bluetooth module handles rfkill events instantly

This commit is contained in:
Marc
2020-01-23 17:17:29 +01:00
parent d85f0e1060
commit e3bf6b968c
6 changed files with 97 additions and 17 deletions

View File

@ -3,9 +3,71 @@
#include <stdlib.h>
#include <cstring>
#include <fcntl.h>
#include <sys/poll.h>
#include <cerrno>
#include <stdio.h>
bool waybar::util::rfkill::isDisabled(enum rfkill_type rfkill_type) {
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) {
//perror("Can't open RFKILL control device");
return;
}
memset(&p, 0, sizeof(p));
p.fd = fd;
p.events = POLLIN | POLLHUP;
while (1) {
n = poll(&p, 1, -1);
if (n < 0) {
//perror("Failed to poll RFKILL control device");
break;
}
if (n == 0)
continue;
len = read(fd, &event, sizeof(event));
if (len < 0) {
//perror("Reading of RFKILL events failed");
break;
}
if (len != RFKILL_EVENT_SIZE_V1) {
//fprintf(stderr, "Wrong size of RFKILL event\n");
continue;
}
if(event.type == rfkill_type_) {
state_ = event.soft || event.hard;
if (prev_state_ != state_) {
prev_state_ = state_;
break;
}
//ret = event.soft || event.hard;
}
}
close(fd);
return;
}
int waybar::util::Rfkill::getState() {
return state_;
}
bool waybar::util::Rfkill::isDisabled() const {
struct rfkill_event event;
ssize_t len;
int fd;
@ -38,7 +100,7 @@ bool waybar::util::rfkill::isDisabled(enum rfkill_type rfkill_type) {
return false;
}
if(event.type == rfkill_type) {
if(event.type == rfkill_type_) {
ret = event.soft || event.hard;
break;
}