Created
February 21, 2018 13:49
-
-
Save olekenneth/a776faa3b5235ff8299d063ad2064bbb to your computer and use it in GitHub Desktop.
This file contains 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
#include <stdio.h> | |
#include <math.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netdb.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <sys/epoll.h> | |
#include <errno.h> | |
#include <linux/input.h> | |
#define MAXEVENTS 10 | |
static const char *const evval[3] = { | |
"RELEASED", | |
"PRESSED ", | |
"REPEATED" | |
}; | |
static int make_socket_non_blocking(int sfd) | |
{ | |
int flags, s; | |
flags = fcntl(sfd, F_GETFL, 0); | |
if (flags == -1) { | |
perror("fcntl"); | |
return -1; | |
} | |
flags |= O_NONBLOCK; | |
s = fcntl(sfd, F_SETFL, flags); | |
if (s == -1) { | |
perror("fcntl"); | |
return -1; | |
} | |
return 0; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int sfd, s; | |
int efd; | |
struct epoll_event event; | |
struct epoll_event *events; | |
char *dev_path; | |
int done = 0; | |
if (argc != 2) { | |
fprintf(stderr, "Usage: %s [devPath]\n", argv[0]); | |
exit(EXIT_FAILURE); | |
} | |
dev_path = argv[1]; | |
sfd = open(dev_path, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK); | |
if (sfd == -1) { | |
fprintf(stderr, "open_port: Unable to open %s\n", dev_path); | |
return (-1); | |
} | |
make_socket_non_blocking(sfd); | |
char name[256] = "Unknown"; | |
ioctl(sfd, EVIOCGNAME(sizeof(name)), name); | |
printf("Reading from device : %s (%s)\n", dev_path, name); | |
efd = epoll_create1(0); | |
if (efd == -1) { | |
perror("epoll_create"); | |
abort(); | |
} | |
event.data.fd = sfd; | |
event.events = EPOLLIN | EPOLLET; | |
s = epoll_ctl(efd, EPOLL_CTL_ADD, sfd, &event); | |
if (s == -1) { | |
perror("epoll_ctl"); | |
abort(); | |
} | |
/* Buffer where events are returned */ | |
events = calloc(MAXEVENTS, sizeof event); | |
#define MAX_INTS 30 | |
char int_str[MAX_INTS]; | |
memset(int_str, '\0', MAX_INTS); | |
int controlOdd = 0; | |
int controlEven = 0; | |
printf("\n"); | |
while (1) { | |
int n, i; | |
n = epoll_wait(efd, events, MAXEVENTS, -1); | |
for (i = 0; i < n; i++) { | |
if ((events[i].events & EPOLLERR) || | |
(events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) { | |
fprintf(stderr, "epoll error\n"); | |
close(events[i].data.fd); | |
continue; | |
} | |
else if (sfd == events[i].data.fd) { | |
while (1) { | |
int len = 0; | |
struct input_event key_press[64]; | |
if ((len = read(sfd, &key_press, sizeof key_press)) != -1) { | |
if (key_press[1].value == 1) { | |
printf("\n\nLen: %d\n", len); | |
int code = key_press[1].code; | |
int number = 0; | |
if (code < 12) { | |
number = key_press[1].code - 1; | |
if (code == 11) { | |
number = 0; | |
} | |
if (number % 2 == 0) { | |
controlEven = controlEven + number; | |
} | |
if (number % 2 != 0) { | |
controlOdd = controlOdd + number; | |
} | |
sprintf(int_str, "%s%d", int_str, number); | |
printf("%d\n", number); | |
printf("Type %d Code %d Number %d\n", key_press[1].type, key_press[1].code, number); | |
} | |
if (code == 28) { | |
controlEven = (controlEven * 3) + controlOdd; | |
controlOdd = abs((ceil(controlEven/10) * 10) - controlEven); | |
printf("String: %s%d\n", int_str, controlOdd); | |
sprintf(int_str, "%s", ""); | |
} | |
} | |
// printf("Code: %d\n", code); | |
} | |
break; | |
} | |
continue; | |
} else { | |
int done = 0; | |
if (done) { | |
printf("Closed on descriptor %d\n", | |
events[i].data.fd); | |
close(events[i].data.fd); | |
} | |
} | |
} | |
} | |
free(events); | |
close(sfd); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment