reformat and better naming

xkbcommon
Alessandro Mauri 5 years ago
parent 58c01fb9d1
commit 636bfe7a5e
  1. 50
      macrod.c

@ -33,7 +33,7 @@
#define OS unix #define OS unix
#endif #endif
struct pressed_buffer { struct key_buffer {
unsigned short *buf; unsigned short *buf;
unsigned int size; unsigned int size;
}; };
@ -41,11 +41,12 @@ struct pressed_buffer {
int term = 0; // exit flag int term = 0; // exit flag
const char ev_root[] = "/dev/input/"; const char ev_root[] = "/dev/input/";
int pressBufferAdd (struct pressed_buffer*, unsigned short); //int reloadFileDescriptors
int pressBufferRemove (struct pressed_buffer*, unsigned short); int key_buffer_add (struct key_buffer*, unsigned short);
void termHandler (int signum); int key_buffer_remove (struct key_buffer*, unsigned short);
void int_handler (int signum);
void die (const char *, int); void die (const char *, int);
void execCommand(const char *); void exec_command(const char *);
// TODO: use getopts() to parse command line options // TODO: use getopts() to parse command line options
int main (void) int main (void)
@ -54,7 +55,7 @@ int main (void)
term = 0; term = 0;
struct sigaction action; struct sigaction action;
memset(&action, 0, sizeof(action)); memset(&action, 0, sizeof(action));
action.sa_handler = termHandler; action.sa_handler = int_handler;
sigaction(SIGINT, &action, NULL); sigaction(SIGINT, &action, NULL);
/* Open the event directory */ /* Open the event directory */
@ -125,11 +126,10 @@ int main (void)
// portability across other *NIX derivatives, could also use libev // portability across other *NIX derivatives, could also use libev
struct input_event event; struct input_event event;
struct pressed_buffer pb = {NULL, 0}; // Pressed keys buffer struct key_buffer pb = {NULL, 0}; // Pressed keys buffer
ssize_t rb; // Read bits ssize_t rb; // Read bits
/* Prepare for using epoll */ #if OS == linux
#if OS == linux
struct epoll_event epoll_read_ev; struct epoll_event epoll_read_ev;
epoll_read_ev.events = EPOLLIN; epoll_read_ev.events = EPOLLIN;
int ev_fd = epoll_create(1); int ev_fd = epoll_create(1);
@ -138,13 +138,13 @@ int main (void)
for (int i = 0; i < fd_num; i++) for (int i = 0; i < fd_num; i++)
if (epoll_ctl(ev_fd, EPOLL_CTL_ADD, fds[i].fd, &epoll_read_ev) < 0) if (epoll_ctl(ev_fd, EPOLL_CTL_ADD, fds[i].fd, &epoll_read_ev) < 0)
die("epoll_ctl", errno); die("epoll_ctl", errno);
#endif #endif /* Prepare for using epoll */
for (;;) { for (;;) {
// TODO: better error reporting // TODO: better error reporting
/* On linux use epoll(2) as it gives better performance */ /* On linux use epoll(2) as it gives better performance */
#if OS == linux #if OS == linux
static struct epoll_event ev_type; static struct epoll_event ev_type;
if (epoll_wait(ev_fd, &ev_type, fd_num, -1) == -1 || term) if (epoll_wait(ev_fd, &ev_type, fd_num, -1) == -1 || term)
break; break;
@ -152,21 +152,21 @@ int main (void)
// TODO: use and test kqueue(2) for BSD systems // TODO: use and test kqueue(2) for BSD systems
/* On other systems use poll(2) to wait por a file dscriptor /* On other systems use poll(2) to wait por a file dscriptor
* to become ready for reading. */ * to become ready for reading. */
#else // TODO: add unix and bsd cases #else // TODO: add unix and bsd cases
if (poll(fds, fd_num, -1) != -1 || term) if (poll(fds, fd_num, -1) != -1 || term)
break; break;
#endif #endif
static int i; static int i;
static unsigned int prev_size; static unsigned 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++) {
#if OS == linux #if OS == linux
if (ev_type.events == EPOLLIN) { if (ev_type.events == EPOLLIN) {
#else // TODO: add unix and bsd cases #else // TODO: add unix and bsd cases
if (fds[i].revents == fds[i].events) { if (fds[i].revents == fds[i].events) {
#endif #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;
@ -183,11 +183,11 @@ int main (void)
switch (event.value) { switch (event.value) {
/* Key released */ /* Key released */
case (0): case (0):
pressBufferRemove(&pb, event.code); key_buffer_remove(&pb, event.code);
break; break;
/* Key pressed */ /* Key pressed */
case (1): case (1):
pressBufferAdd(&pb, event.code); key_buffer_add(&pb, event.code);
break; break;
} }
} }
@ -202,7 +202,7 @@ int main (void)
if (pb.size == 2) if (pb.size == 2)
if (pb.buf[0] == 56 || pb.buf[0] == 31) if (pb.buf[0] == 56 || pb.buf[0] == 31)
if (pb.buf[1] == 31 || pb.buf[1] == 56) if (pb.buf[1] == 31 || pb.buf[1] == 56)
execCommand("/home/ale/hello"); exec_command("/home/ale/hello");
} }
} }
@ -221,7 +221,7 @@ int main (void)
} }
// TODO: optimize functions to preallocate some memory // TODO: optimize functions to preallocate some memory
int pressBufferAdd (struct pressed_buffer *pb, unsigned short key) int key_buffer_add (struct key_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
* Returns non zero if the key was not added. */ * Returns non zero if the key was not added. */
@ -236,14 +236,14 @@ int pressBufferAdd (struct pressed_buffer *pb, unsigned short key)
unsigned short *b; unsigned short *b;
b = realloc(pb->buf, sizeof(unsigned short) * (pb->size + 1)); b = realloc(pb->buf, sizeof(unsigned short) * (pb->size + 1));
if (!b) if (!b)
die("realloc failed in pressBufferAdd", errno); die("realloc failed in key_buffer_add", errno);
pb->buf = b; pb->buf = b;
pb->buf[pb->size++] = key; pb->buf[pb->size++] = key;
return 0; return 0;
} }
int pressBufferRemove (struct pressed_buffer *pb, unsigned short key) int key_buffer_remove (struct key_buffer *pb, unsigned short key)
{ {
/* Removes a keycode from a pressed buffer if it is present returns /* Removes a keycode from a pressed buffer if it is present returns
* non zero in case of failure (key not present or buffer empty). */ * non zero in case of failure (key not present or buffer empty). */
@ -257,7 +257,7 @@ int pressBufferRemove (struct pressed_buffer *pb, unsigned short key)
b = realloc(pb->buf, sizeof(unsigned short) * pb->size); b = realloc(pb->buf, sizeof(unsigned short) * pb->size);
/* if realloc failed but the buffer is populated throw an error */ /* if realloc failed but the buffer is populated throw an error */
if (!b && pb->size) if (!b && pb->size)
die("realloc failed in pressBufferRemove: %s", errno); die("realloc failed in key_buffer_remove: %s", errno);
pb->buf = b; pb->buf = b;
return 0; return 0;
} }
@ -265,7 +265,7 @@ int pressBufferRemove (struct pressed_buffer *pb, unsigned short key)
return 1; return 1;
} }
void termHandler (int signum) void int_handler (int signum)
{ {
fputs("Received interrupt signal, exiting gracefully...\n", stderr); fputs("Received interrupt signal, exiting gracefully...\n", stderr);
term = 1; term = 1;
@ -277,7 +277,7 @@ void die (const char *msg, int err)
exit(err); exit(err);
} }
void execCommand (const char *path) void exec_command (const char *path)
{ {
switch (fork()) { switch (fork()) {
case -1: case -1:

Loading…
Cancel
Save