diff options
author | Přemysl Janouch <p.janouch@gmail.com> | 2014-09-23 01:38:08 +0200 |
---|---|---|
committer | Přemysl Janouch <p.janouch@gmail.com> | 2014-09-23 03:05:01 +0200 |
commit | b630bf7a5f5ec85317db04f770ffc90664ac28f0 (patch) | |
tree | 52a0f40045809e71bc6fcac40d52e570f2fb2b59 /demo-async.c | |
parent | 7909067ac05e885211dafa255da0526543bb87bf (diff) | |
download | termo-b630bf7a5f5ec85317db04f770ffc90664ac28f0.tar.gz termo-b630bf7a5f5ec85317db04f770ffc90664ac28f0.tar.xz termo-b630bf7a5f5ec85317db04f770ffc90664ac28f0.zip |
WIP: Is mine now (^3^)
Seriously though, I've got some issues with how this thing is designed,
as well as with its formatting, and when you add the fact that the
original author wants to merge this thing into his bigger library that
also handles terminal output, which I'll kindly leave to ncurses,
it kind of makes sense for me to do this.
Manpages have been removed as they are going to become obsolete and
they're rather difficult to maintain. If anything, there will be
Doxygen-generated documentation.
The plan is to throw away any direct UTF-8 support and support all uni-
and multibyte character encodings. However some unrelated refactoring
is about to come first.
Diffstat (limited to 'demo-async.c')
-rw-r--r-- | demo-async.c | 83 |
1 files changed, 43 insertions, 40 deletions
diff --git a/demo-async.c b/demo-async.c index 9d398a7..51bc1b3 100644 --- a/demo-async.c +++ b/demo-async.c @@ -6,59 +6,62 @@ #include "termkey.h" -static void on_key(TermKey *tk, TermKeyKey *key) +static void +on_key (termkey_t *tk, termkey_key_t *key) { - char buffer[50]; - termkey_strfkey(tk, buffer, sizeof buffer, key, TERMKEY_FORMAT_VIM); - printf("%s\n", buffer); + char buffer[50]; + termkey_strfkey (tk, buffer, sizeof buffer, key, TERMKEY_FORMAT_VIM); + printf ("%s\n", buffer); } -int main(int argc, char *argv[]) +int +main (int argc, char *argv[]) { - TERMKEY_CHECK_VERSION; + TERMKEY_CHECK_VERSION; - TermKey *tk = termkey_new(0, 0); + termkey_t *tk = termkey_new (0, 0); - if(!tk) { - fprintf(stderr, "Cannot allocate termkey instance\n"); - exit(1); - } + if (!tk) + { + fprintf (stderr, "Cannot allocate termkey instance\n"); + exit (1); + } - struct pollfd fd; + struct pollfd fd; + fd.fd = 0; /* the file descriptor we passed to termkey_new() */ + fd.events = POLLIN; - fd.fd = 0; /* the file descriptor we passed to termkey_new() */ - fd.events = POLLIN; + termkey_result_t ret; + termkey_key_t key; - TermKeyResult ret; - TermKeyKey key; + int running = 1; + int nextwait = -1; - 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); - 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); - if(fd.revents & (POLLIN|POLLHUP|POLLERR)) - termkey_advisereadable(tk); + while ((ret = termkey_getkey (tk, &key)) == TERMKEY_RES_KEY) + { + on_key (tk, &key); - 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(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) + nextwait = termkey_get_waittime (tk); + else + nextwait = -1; + } - if(ret == TERMKEY_RES_AGAIN) - nextwait = termkey_get_waittime(tk); - else - nextwait = -1; - } - - termkey_destroy(tk); + termkey_destroy (tk); } |