2011-08-25 23:52:06 +02:00
|
|
|
// <poll.h> might need this for sigset_t
|
|
|
|
#define _XOPEN_SOURCE 600
|
|
|
|
|
2008-11-06 20:33:07 +01:00
|
|
|
#include <poll.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "termkey.h"
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
static void on_key(TermKey *tk, TermKeyKey *key)
|
2008-11-06 20:33:07 +01:00
|
|
|
{
|
|
|
|
char buffer[50];
|
2011-03-31 12:08:43 +02:00
|
|
|
termkey_strfkey(tk, buffer, sizeof buffer, key, TERMKEY_FORMAT_VIM);
|
2008-11-06 20:33:07 +01:00
|
|
|
printf("%s\n", buffer);
|
|
|
|
}
|
|
|
|
|
2008-12-08 23:54:14 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2008-11-16 13:40:14 +01:00
|
|
|
TERMKEY_CHECK_VERSION;
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
TermKey *tk = termkey_new(0, 0);
|
2008-11-06 20:33:07 +01:00
|
|
|
|
|
|
|
if(!tk) {
|
|
|
|
fprintf(stderr, "Cannot allocate termkey instance\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct pollfd fd;
|
|
|
|
|
|
|
|
fd.fd = 0; /* the file descriptor we passed to termkey_new() */
|
|
|
|
fd.events = POLLIN;
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
TermKeyResult ret;
|
|
|
|
TermKeyKey key;
|
2008-11-06 20:33:07 +01:00
|
|
|
|
|
|
|
int running = 1;
|
|
|
|
int nextwait = -1;
|
|
|
|
|
|
|
|
while(running) {
|
|
|
|
if(poll(&fd, 1, nextwait) == 0) {
|
|
|
|
// Timed out
|
|
|
|
if(termkey_getkey_force(tk, &key) == TERMKEY_RES_KEY)
|
|
|
|
on_key(tk, &key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fd.revents & (POLLIN|POLLHUP|POLLERR))
|
|
|
|
termkey_advisereadable(tk);
|
|
|
|
|
|
|
|
while((ret = termkey_getkey(tk, &key)) == TERMKEY_RES_KEY) {
|
|
|
|
on_key(tk, &key);
|
|
|
|
|
|
|
|
if(key.type == TERMKEY_TYPE_UNICODE &&
|
|
|
|
key.modifiers & TERMKEY_KEYMOD_CTRL &&
|
|
|
|
(key.code.codepoint == 'C' || key.code.codepoint == 'c'))
|
|
|
|
running = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ret == TERMKEY_RES_AGAIN)
|
2008-11-06 21:29:50 +01:00
|
|
|
nextwait = termkey_get_waittime(tk);
|
2008-11-06 20:33:07 +01:00
|
|
|
else
|
|
|
|
nextwait = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
termkey_destroy(tk);
|
|
|
|
}
|