1 // <poll.h> might need this for sigset_t
2 #define _XOPEN_SOURCE 600
3
4 #include <poll.h>
5 #include <stdio.h>
6
7 #include "termkey.h"
8
on_key(TermKey * tk,TermKeyKey * key)9 static void on_key(TermKey *tk, TermKeyKey *key)
10 {
11 char buffer[50];
12 termkey_strfkey(tk, buffer, sizeof buffer, key, TERMKEY_FORMAT_VIM);
13 printf("%s\n", buffer);
14 }
15
main(int argc,char * argv[])16 int main(int argc, char *argv[])
17 {
18 TERMKEY_CHECK_VERSION;
19
20 TermKey *tk = termkey_new(0, 0);
21
22 if(!tk) {
23 fprintf(stderr, "Cannot allocate termkey instance\n");
24 exit(1);
25 }
26
27 struct pollfd fd;
28
29 fd.fd = 0; /* the file descriptor we passed to termkey_new() */
30 fd.events = POLLIN;
31
32 TermKeyResult ret;
33 TermKeyKey key;
34
35 int running = 1;
36 int nextwait = -1;
37
38 while(running) {
39 if(poll(&fd, 1, nextwait) == 0) {
40 // Timed out
41 if(termkey_getkey_force(tk, &key) == TERMKEY_RES_KEY)
42 on_key(tk, &key);
43 }
44
45 if(fd.revents & (POLLIN|POLLHUP|POLLERR))
46 termkey_advisereadable(tk);
47
48 while((ret = termkey_getkey(tk, &key)) == TERMKEY_RES_KEY) {
49 on_key(tk, &key);
50
51 if(key.type == TERMKEY_TYPE_UNICODE &&
52 key.modifiers & TERMKEY_KEYMOD_CTRL &&
53 (key.code.codepoint == 'C' || key.code.codepoint == 'c'))
54 running = 0;
55 }
56
57 if(ret == TERMKEY_RES_AGAIN)
58 nextwait = termkey_get_waittime(tk);
59 else
60 nextwait = -1;
61 }
62
63 termkey_destroy(tk);
64 }
65