Created
October 24, 2020 21:43
-
-
Save saivert/e98ae0cc9a91e39073de1dfd83a9b51b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-2.0 | |
#include <stdio.h> | |
#include <fcntl.h> | |
#include <poll.h> | |
#include <signal.h> | |
#include <unistd.h> | |
#include <libudev.h> | |
#include <libinput.h> | |
#include <errno.h> | |
#include <termios.h> | |
static volatile sig_atomic_t doquit; | |
static void quit(int sig) | |
{ | |
(void)sig; | |
doquit = 1; | |
} | |
static int open_restricted(const char *path, int flags, void *user_data) | |
{ | |
int fd = open(path, flags); | |
return fd < 0 ? -errno : fd; | |
} | |
static void close_restricted(int fd, void *user_data) | |
{ | |
close(fd); | |
} | |
const static struct libinput_interface interface = { | |
.open_restricted = open_restricted, | |
.close_restricted = close_restricted, | |
}; | |
void handle_event(struct libinput *li, struct libinput_event *event) | |
{ | |
switch (libinput_event_get_type(event)) { | |
case LIBINPUT_EVENT_KEYBOARD_KEY: { | |
struct libinput_event_keyboard *kbdevent; | |
int keycode; | |
kbdevent = libinput_event_get_keyboard_event(event); | |
keycode = libinput_event_keyboard_get_key(kbdevent); | |
if (libinput_event_keyboard_get_key_state(kbdevent) == LIBINPUT_KEY_STATE_PRESSED) | |
printf("\33[2K\rGot keyboard event, keycode = %d!\n", keycode); | |
} | |
break; | |
case LIBINPUT_EVENT_POINTER_MOTION: { | |
struct libinput_event_pointer *ptrevent; | |
ptrevent = libinput_event_get_pointer_event(event); | |
double x = libinput_event_pointer_get_dx(ptrevent); | |
double y = libinput_event_pointer_get_dy(ptrevent); | |
printf("\33[2K\rX = %f, Y = %f relative", x, y); | |
fflush(stdout); | |
} | |
break; | |
case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE: { | |
struct libinput_event_pointer *ptrevent; | |
ptrevent = libinput_event_get_pointer_event(event); | |
double x = libinput_event_pointer_get_absolute_x_transformed(ptrevent, INT16_MAX); | |
double y = libinput_event_pointer_get_absolute_y_transformed(ptrevent, INT16_MAX); | |
printf("\33[2K\rX = %f, Y = %f absolute", x, y); | |
fflush(stdout); | |
} | |
break; | |
} | |
} | |
int main(void) | |
{ | |
struct libinput *li; | |
struct libinput_event *event; | |
struct udev *udev; | |
int ret; | |
// Disable local echo | |
struct termios old, new; | |
tcgetattr(STDIN_FILENO, &old); // get current settings | |
new = old; // create a backup | |
new.c_lflag &= ~(ICANON | ECHO); // disable line buffering and feedback | |
tcsetattr(STDIN_FILENO, TCSANOW, &new); // set our new config | |
udev = udev_new(); | |
li = libinput_udev_create_context(&interface, NULL, udev); | |
if (!li) { | |
printf("Error: cannot create libinput context!\n"); | |
return 1; | |
} | |
if (libinput_udev_assign_seat(li, "seat0") == -1) { | |
printf("Error: cannot assign seat!\n"); | |
return 1; | |
} | |
if (libinput_dispatch(li) < 0) { | |
printf("Error: cannot dispatch!\n"); | |
return 1; | |
} | |
struct pollfd fds[1]; | |
fds[0].fd = libinput_get_fd(li); | |
fds[0].events = POLLIN; | |
while (!doquit) { | |
ret = poll(fds, 1, 10000); | |
if (ret > 0 && fds[0].revents & POLLIN) { | |
libinput_dispatch(li); | |
while ((event = libinput_get_event(li)) != NULL) { | |
handle_event(li, event); | |
libinput_event_destroy(event); | |
//libinput_dispatch(li); | |
} | |
} else { | |
printf("\33[2K\rtimeout"); | |
fflush(stdout); | |
} | |
} | |
libinput_unref(li); | |
udev_unref(udev); | |
tcsetattr(STDIN_FILENO, TCSANOW, &old); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment