Skip to content

Commit

Permalink
Improved keyboard detection on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Azarattum committed Dec 29, 2020
1 parent bf9a6f3 commit 171005e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/linux/client/ipc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ bool send_config(int fd, const Config& config) {
send(fd, static_cast<uint16_t>(config.commands.size()));
for (const auto& command : config.commands) {
send(fd, command.input);
send(fd, command.default_mapping);
///!SEND HERE!
send(fd, command.default_mapping.sequence);
}

// send mapping overrides
Expand All @@ -72,7 +73,8 @@ bool send_config(int fd, const Config& config) {
send(fd, static_cast<uint16_t>(context_mappings.size()));
for (const auto& mapping : context_mappings) {
send(fd, static_cast<uint16_t>(mapping.first));
send(fd, mapping.second->output);
///!SEND HERE!
send(fd, mapping.second->output.sequence);
}
}
return !g_pipe_broken;
Expand Down
8 changes: 5 additions & 3 deletions src/linux/server/ipc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ std::unique_ptr<Stage> read_config(int fd) {

auto mappings = std::vector<Mapping>();
auto input = KeySequence();
auto output = KeySequence();
Action output = {ActionType::Sequence, "", KeySequence()};
for (auto i = 0; i < commmand_count; ++i) {
///!READ HERE!
if (!read(fd, &input) ||
!read(fd, &output))
!read(fd, &output.sequence))
return nullptr;
mappings.push_back({ std::move(input), std::move(output) });
}
Expand All @@ -105,8 +106,9 @@ std::unique_ptr<Stage> read_config(int fd) {

for (auto j = 0; j < overrides_count; ++j) {
auto mapping_index = uint16_t{ };
///!READ HERE!
if (!read(fd, &mapping_index) ||
!read(fd, &output))
!read(fd, &output.sequence))
return nullptr;
overrides.push_back({ mapping_index, std::move(output) });
}
Expand Down
23 changes: 21 additions & 2 deletions src/linux/server/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,31 @@ bool grab_keyboard(int fd, bool grab) {
return (::ioctl(fd, EVIOCGRAB, (grab ? 1 : 0)) == 0);
}

bool is_real(int fd) {
char led = 0;
ioctl(fd, EVIOCGLED(1), &led);
led = led & 1;

input_event ev = {EV_LED, LED_NUML, (u_int16_t) led};
write(fd, &ev, sizeof(input_event));

fd_set set;
FD_ZERO(&set);
FD_SET(fd, &set);
timeval timeout = {1};
if (select(fd + 1, &set, NULL, NULL, &timeout) <= 0)
return false;
read(fd, &ev, sizeof(input_event));
return ev.type == EV_MSC;
}

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);
const auto fd = ::open(buffer.data(), O_RDWR);
if (fd >= 0)
return fd;
} while (errno == EINTR);
Expand All @@ -97,7 +115,8 @@ int grab_first_keyboard() {
if (fd >= 0) {
if (is_keyboard(fd) &&
wait_until_keys_released(fd) &&
grab_keyboard(fd, true))
grab_keyboard(fd, true) &&
is_real(fd))
return fd;
::close(fd);
}
Expand Down
7 changes: 6 additions & 1 deletion src/linux/server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ int main() {
static_cast<KeyCode>(code),
(value == 0 ? KeyState::Up : KeyState::Down),
};
auto output = stage->apply_input(event);
auto action = stage->apply_input(event);
if (action.type == ActionType::Command) {
system(action.command.c_str());
}

auto output = action.sequence;
send_key_sequence(uinput_fd, output);
stage->reuse_buffer(std::move(output));
}
Expand Down

0 comments on commit 171005e

Please sign in to comment.