Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
houmain committed Jan 21, 2021
1 parent 3eccdfd commit 7cbd0fa
Showing 1 changed file with 65 additions and 65 deletions.
130 changes: 65 additions & 65 deletions src/linux/server/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,84 +12,84 @@
const auto EVDEV_MINORS = 32;

namespace {
bool read_all(int fd, char* buffer, size_t length) {
while (length != 0) {
auto ret = ::read(fd, buffer, length);
if (ret == -1 && errno == EINTR)
continue;
if (ret <= 0)
return false;
length -= static_cast<size_t>(ret);
buffer += ret;
}
return true;
}
} // namespace
bool is_keyboard(int fd) {
auto version = int{ };
if (::ioctl(fd, EVIOCGVERSION, &version) == -1 ||
version != EV_VERSION)
return false;

bool is_keyboard(int fd) {
auto version = int{ };
if (::ioctl(fd, EVIOCGVERSION, &version) == -1 ||
version != EV_VERSION)
return false;
auto devinfo = input_id{ };
if (::ioctl(fd, EVIOCGID, &devinfo) != 0)
return false;

auto devinfo = input_id{ };
if (::ioctl(fd, EVIOCGID, &devinfo) != 0)
return false;
switch (devinfo.bustype) {
case BUS_USB:
case BUS_I8042:
case BUS_ADB:
break;
default:
return false;
}

switch (devinfo.bustype) {
case BUS_USB:
case BUS_I8042:
case BUS_ADB:
break;
default:
const auto required_bits = (1 << EV_SYN) | (1 << EV_KEY) | (1 << EV_REP);
auto bits = 0;
if (::ioctl(fd, EVIOCGBIT(0, sizeof(bits)), &bits) == -1 ||
(bits & required_bits) != required_bits)
return false;
}

const auto required_bits = (1 << EV_SYN) | (1 << EV_KEY) | (1 << EV_REP);
auto bits = 0;
if (::ioctl(fd, EVIOCGBIT(0, sizeof(bits)), &bits) == -1 ||
(bits & required_bits) != required_bits)
return false;
return true;
}

return true;
}
bool wait_until_keys_released(int fd) {
const auto retries = 1000;
const auto sleep_ms = 5;
for (auto i = 0; i < retries; ++i) {
auto bits = std::array<char, (KEY_MAX + 7) / 8>();
if (::ioctl(fd, EVIOCGKEY(bits.size()), bits.data()) == -1)
return false;

bool wait_until_keys_released(int fd) {
const auto retries = 1000;
const auto sleep_ms = 5;
for (auto i = 0; i < retries; ++i) {
auto bits = std::array<char, (KEY_MAX + 7) / 8>();
if (::ioctl(fd, EVIOCGKEY(bits.size()), bits.data()) == -1)
return false;
const auto all_keys_released =
std::none_of(std::cbegin(bits), std::cend(bits),
[](char bits) { return (bits != 0); });
if (all_keys_released)
return true;

const auto all_keys_released =
std::none_of(std::cbegin(bits), std::cend(bits),
[](char bits) { return (bits != 0); });
if (all_keys_released)
return true;
::usleep(sleep_ms * 1000);
}
return false;
}

::usleep(sleep_ms * 1000);
bool grab_keyboard(int fd, bool grab) {
return (::ioctl(fd, EVIOCGRAB, (grab ? 1 : 0)) == 0);
}
return false;
}

bool grab_keyboard(int fd, bool grab) {
return (::ioctl(fd, EVIOCGRAB, (grab ? 1 : 0)) == 0);
}
int open_event_device(int index) {
const auto paths = { "/dev/input/event%d", "/dev/event%d" };
for (const auto path : paths) {
auto buffer = std::array<char, 128>();
std::snprintf(buffer.data(), buffer.size(), path, index);
do {
const auto fd = ::open(buffer.data(), O_RDONLY);
if (fd >= 0)
return fd;
} while (errno == EINTR);
}
return -1;
}

int open_event_device(int index) {
const auto paths = { "/dev/input/event%d", "/dev/event%d" };
for (const auto path : paths) {
auto buffer = std::array<char, 128>();
std::snprintf(buffer.data(), buffer.size(), path, index);
do {
const auto fd = ::open(buffer.data(), O_RDONLY);
if (fd >= 0)
return fd;
} while (errno == EINTR);
bool read_all(int fd, char* buffer, size_t length) {
while (length != 0) {
auto ret = ::read(fd, buffer, length);
if (ret == -1 && errno == EINTR)
continue;
if (ret <= 0)
return false;
length -= static_cast<size_t>(ret);
buffer += ret;
}
return true;
}
return -1;
}
} // namespace

std::vector<int> grab_keyboards() {
auto fds = std::vector<int>();
Expand Down

0 comments on commit 7cbd0fa

Please sign in to comment.