multi event handling prototype

xkbcommon
Alessandro Mauri 5 years ago
parent 4f3a2daddf
commit cca522c197
  1. BIN
      .macrod.c.swp
  2. 96
      macrod.c

Binary file not shown.

@ -1,3 +1,4 @@
/* Standard stuff */
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -9,6 +10,10 @@
#include <fcntl.h> #include <fcntl.h>
#include <dirent.h> #include <dirent.h>
#include <unistd.h> #include <unistd.h>
/* Polling */
#include <poll.h>
/* Signaling */
#include <signal.h>
#ifdef __linux__ #ifdef __linux__
#include <linux/input.h> #include <linux/input.h>
@ -22,56 +27,86 @@ struct pressed_buffer {
unsigned int size; unsigned int size;
}; };
int term = 0; // Received SIGTERM flag
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);
int main (void) // remember getopt() to automaically parse options int main (void) // remember getopt() to automaically parse options
{ {
int fd; term = 0;
fd = open("/dev/input/event0", O_RDONLY); // TEST: O_NONBLOCK struct sigaction action;
if (!fd) { memset(&action, 0, sizeof(action));
action.sa_handler = termHandler;
sigaction(SIGINT, &action, NULL);
struct pollfd fds[2];
fds[0].events = POLLIN;
fds[0].fd = open("/dev/input/event0", O_RDONLY); // TEST: O_NONBLOCK
if (!fds[0].fd) {
fputs(strerror(errno), stderr);
exit(errno);
}
fds[1].events = POLLIN;
fds[1].fd = open("/dev/input/event3", O_RDONLY);
if (!fds[1].fd) {
fputs(strerror(errno), stderr); fputs(strerror(errno), stderr);
exit(errno); exit(errno);
} }
//int f2 = open("/dev/input/event3", O_RDONLY);
//struct input_event ev;
struct input_event event; struct input_event event;
struct pressed_buffer pb = {NULL, 0}; // Pressed keys buffer struct pressed_buffer pb = {NULL, 0}; // Pressed keys buffer
ssize_t rb; // Read bits
while (1) { while (poll(fds, 2, -1) != -1 || !term) {
/* Use polling poll(2) to wait por a file dscriptor to become ready for reading. /* 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 */ * NOTE: this could use select(2) but I don't know how */
//fread(&event, sizeof(struct input_event), 1, fp); static int i;
static int prev_size;
ssize_t rb;
rb = read(fd, &event, sizeof(struct input_event)); prev_size = pb.size;
if (rb != sizeof(struct input_event)) continue; for (i = 0; i < 2; i++) {
if (fds[i].revents == fds[i].events) {
if (event.type == EV_KEY) {
rb = read(fds[i].fd, &event, sizeof(struct input_event));
switch (event.value) { if (rb != sizeof(struct input_event)) continue;
case (0): // Key release
pressBufferRemove(&pb, event.code);
break;
case (1): // Key press
pressBufferAdd(&pb, event.code);
break;
}
if (event.type == EV_KEY) {
switch (event.value) {
/* Key released */
case (0):
pressBufferRemove(&pb, event.code);
break;
/* Key pressed */
case (1):
pressBufferAdd(&pb, event.code);
break;
}
}
}
}
if (pb.size != prev_size) {
printf("Pressed keys: "); printf("Pressed keys: ");
for (int i = 0; i < pb.size; i++) for (int i = 0; i < pb.size; i++)
printf("%d ", pb.buf[i]); printf("%d ", pb.buf[i]);
putchar('\n'); putchar('\n');
} }
} }
if (close(fd) == -1) { if (!term)
fputs(strerror(errno), stderr); fputs("An error occured\n", stderr);
exit(errno); for (int i = 0; i < 2; i++) {
} if (close(fds[i].fd) == -1) {
fputs(strerror(errno), stderr);
exit(errno);
}
}
return 0; return 0;
} }
@ -123,3 +158,8 @@ int pressBufferRemove (struct pressed_buffer *pb, unsigned short key)
return 1; return 1;
} }
void termHandler (int signum) {
fputs("Received interrupt signal, exiting gracefully...\n", stderr);
term = 1;
}

Loading…
Cancel
Save