use epoll instead of poll on linux
This commit is contained in:
parent
1b017733cb
commit
56024c6a4e
74
macrod.c
74
macrod.c
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
#include <linux/input.h>
|
#include <linux/input.h>
|
||||||
|
#include <sys/epoll.h>
|
||||||
#endif
|
#endif
|
||||||
#ifdef __FreeBSD__
|
#ifdef __FreeBSD__
|
||||||
#include <dev/evdev/input.h>
|
#include <dev/evdev/input.h>
|
||||||
@ -33,6 +34,7 @@ const char ev_root[] = "/dev/input/";
|
|||||||
int pressBufferAdd (struct pressed_buffer*, unsigned short);
|
int pressBufferAdd (struct pressed_buffer*, unsigned short);
|
||||||
int pressBufferRemove (struct pressed_buffer*, unsigned short);
|
int pressBufferRemove (struct pressed_buffer*, unsigned short);
|
||||||
void termHandler (int signum);
|
void termHandler (int signum);
|
||||||
|
void die (void);
|
||||||
|
|
||||||
// TODO: use getopts() to parse commanfìd line options
|
// TODO: use getopts() to parse commanfìd line options
|
||||||
int main (void)
|
int main (void)
|
||||||
@ -45,11 +47,8 @@ int main (void)
|
|||||||
sigaction(SIGINT, &action, NULL);
|
sigaction(SIGINT, &action, NULL);
|
||||||
|
|
||||||
DIR *ev_dir = opendir(ev_root);
|
DIR *ev_dir = opendir(ev_root);
|
||||||
if (!ev_dir) {
|
if (!ev_dir) die();
|
||||||
fputs(strerror(errno), stderr);
|
|
||||||
exit(errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
char ev_path[sizeof(ev_root) + NAME_MAX];
|
char ev_path[sizeof(ev_root) + NAME_MAX];
|
||||||
struct dirent *file_ent;
|
struct dirent *file_ent;
|
||||||
void *tmp;
|
void *tmp;
|
||||||
@ -59,10 +58,7 @@ int main (void)
|
|||||||
if (file_ent->d_type == DT_CHR) {
|
if (file_ent->d_type == DT_CHR) {
|
||||||
|
|
||||||
tmp = realloc(fds, sizeof(struct pollfd) * (fd_num + 1));
|
tmp = realloc(fds, sizeof(struct pollfd) * (fd_num + 1));
|
||||||
if (!tmp) {
|
if (!tmp) die();
|
||||||
fputs(strerror(errno), stderr);
|
|
||||||
exit(errno);
|
|
||||||
}
|
|
||||||
fds = tmp;
|
fds = tmp;
|
||||||
|
|
||||||
strncpy(ev_path, ev_root, sizeof(ev_root) + NAME_MAX);
|
strncpy(ev_path, ev_root, sizeof(ev_root) + NAME_MAX);
|
||||||
@ -71,10 +67,8 @@ int main (void)
|
|||||||
fds[fd_num].events = POLLIN;
|
fds[fd_num].events = POLLIN;
|
||||||
// TODO: test performance ipact of O_NONBLOCK
|
// TODO: test performance ipact of O_NONBLOCK
|
||||||
fds[fd_num].fd = open(ev_path, O_RDONLY | O_NONBLOCK);
|
fds[fd_num].fd = open(ev_path, O_RDONLY | O_NONBLOCK);
|
||||||
if (!fds[fd_num].fd) {
|
if (!fds[fd_num].fd) die();
|
||||||
fputs(strerror(errno), stderr);
|
|
||||||
exit(errno);
|
|
||||||
}
|
|
||||||
fd_num++;
|
fd_num++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -89,18 +83,45 @@ int main (void)
|
|||||||
struct pressed_buffer pb = {NULL, 0}; // Pressed keys buffer
|
struct pressed_buffer pb = {NULL, 0}; // Pressed keys buffer
|
||||||
ssize_t rb; // Read bits
|
ssize_t rb; // Read bits
|
||||||
|
|
||||||
|
/* Prepare for using epoll */
|
||||||
|
#ifdef __linux__
|
||||||
|
struct epoll_event epoll_read_ev;
|
||||||
|
epoll_read_ev.events = EPOLLIN;
|
||||||
|
int ev_fd = epoll_create(1);
|
||||||
|
if (ev_fd == -1) die();
|
||||||
|
for (int i = 0; i < fd_num; i++)
|
||||||
|
if (epoll_ctl(ev_fd, EPOLL_CTL_ADD, fds[i].fd, &epoll_read_ev) == -1)
|
||||||
|
die();
|
||||||
|
#endif
|
||||||
|
|
||||||
// TODO: optimize the loop with an O(1) call as it runs for every
|
// TODO: optimize the loop with an O(1) call as it runs for every
|
||||||
// event, some of those are in the previous comment
|
// event, some of those are in the previous comment
|
||||||
while (poll(fds, fd_num, -1) != -1 && !term) {
|
for (;;) {
|
||||||
/* Use poll(2) to wait por a file dscriptor to become ready for reading.
|
|
||||||
* NOTE: this could use select(2) but I don't know how */
|
// TODO: better error reporting
|
||||||
|
/* On linux use epoll(2) as it gives better performance */
|
||||||
|
#ifdef __linux__
|
||||||
|
static struct epoll_event ev_type;
|
||||||
|
if (epoll_wait(ev_fd, &ev_type, fd_num, -1) == -1 || term)
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* On other systems use poll(2) to wait por a file dscriptor
|
||||||
|
* to become ready for reading. */
|
||||||
|
#else
|
||||||
|
if (poll(fds, fd_num, -1) != -1 || term)
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
static int i;
|
static int i;
|
||||||
static int prev_size;
|
static int prev_size;
|
||||||
|
|
||||||
prev_size = pb.size;
|
prev_size = pb.size;
|
||||||
for (i = 0; i < fd_num; i++) {
|
for (i = 0; i < fd_num; i++) {
|
||||||
|
#ifdef __linux__
|
||||||
|
if (ev_type.events == EPOLLIN) {
|
||||||
|
#else
|
||||||
if (fds[i].revents == fds[i].events) {
|
if (fds[i].revents == fds[i].events) {
|
||||||
|
#endif
|
||||||
|
|
||||||
rb = read(fds[i].fd, &event, sizeof(struct input_event));
|
rb = read(fds[i].fd, &event, sizeof(struct input_event));
|
||||||
if (rb != sizeof(struct input_event)) continue;
|
if (rb != sizeof(struct input_event)) continue;
|
||||||
@ -108,11 +129,11 @@ int main (void)
|
|||||||
/* Ignore touchpad events */
|
/* Ignore touchpad events */
|
||||||
// TODO: make a event blacklist system
|
// TODO: make a event blacklist system
|
||||||
if (
|
if (
|
||||||
event.type == EV_KEY &&
|
event.type == EV_KEY &&
|
||||||
event.code != BTN_TOUCH &&
|
event.code != BTN_TOUCH &&
|
||||||
event.code != BTN_TOOL_FINGER &&
|
event.code != BTN_TOOL_FINGER &&
|
||||||
event.code != BTN_TOOL_DOUBLETAP &&
|
event.code != BTN_TOOL_DOUBLETAP &&
|
||||||
event.code != BTN_TOOL_TRIPLETAP
|
event.code != BTN_TOOL_TRIPLETAP
|
||||||
) {
|
) {
|
||||||
switch (event.value) {
|
switch (event.value) {
|
||||||
/* Key released */
|
/* Key released */
|
||||||
@ -142,14 +163,12 @@ int main (void)
|
|||||||
if (!term)
|
if (!term)
|
||||||
fputs("An error occured\n", stderr);
|
fputs("An error occured\n", stderr);
|
||||||
for (int i = 0; i < fd_num; i++) {
|
for (int i = 0; i < fd_num; i++) {
|
||||||
if (close(fds[i].fd) == -1) {
|
if (close(fds[i].fd) == -1) die();
|
||||||
fputs(strerror(errno), stderr);
|
|
||||||
exit(errno);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: optimize functions to preallocate some memory
|
||||||
int pressBufferAdd (struct pressed_buffer *pb, unsigned short key)
|
int pressBufferAdd (struct pressed_buffer *pb, unsigned short key)
|
||||||
{
|
{
|
||||||
/* Adds a keycode to the pressed buffer if it is not already present
|
/* Adds a keycode to the pressed buffer if it is not already present
|
||||||
@ -203,3 +222,8 @@ void termHandler (int signum) {
|
|||||||
fputs("Received interrupt signal, exiting gracefully...\n", stderr);
|
fputs("Received interrupt signal, exiting gracefully...\n", stderr);
|
||||||
term = 1;
|
term = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void die (void) {
|
||||||
|
fputs(strerror(errno), stderr);
|
||||||
|
exit(errno);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user