aboutsummaryrefslogtreecommitdiff
path: root/demo.c
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2014-09-23 01:38:08 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2014-09-23 03:05:01 +0200
commitb630bf7a5f5ec85317db04f770ffc90664ac28f0 (patch)
tree52a0f40045809e71bc6fcac40d52e570f2fb2b59 /demo.c
parent7909067ac05e885211dafa255da0526543bb87bf (diff)
downloadtermo-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.c')
-rw-r--r--demo.c201
1 files changed, 109 insertions, 92 deletions
diff --git a/demo.c b/demo.c
index 3ff68e0..c53eb0b 100644
--- a/demo.c
+++ b/demo.c
@@ -7,112 +7,129 @@
#include "termkey.h"
-int main(int argc, char *argv[])
+int
+main(int argc, char *argv[])
{
- TERMKEY_CHECK_VERSION;
+ TERMKEY_CHECK_VERSION;
- int mouse = 0;
- int mouse_proto = 0;
- TermKeyFormat format = TERMKEY_FORMAT_VIM;
+ int mouse = 0;
+ int mouse_proto = 0;
+ termkey_format_t format = TERMKEY_FORMAT_VIM;
- char buffer[50];
- TermKey *tk;
+ char buffer[50];
+ termkey_t *tk;
- int opt;
- while((opt = getopt(argc, argv, "m::p:")) != -1) {
- switch(opt) {
- case 'm':
- if(optarg)
- mouse = atoi(optarg);
- else
- mouse = 1000;
+ int opt;
+ while ((opt = getopt (argc, argv, "m::p:")) != -1)
+ {
+ switch (opt)
+ {
+ case 'm':
+ if (optarg)
+ mouse = atoi (optarg);
+ else
+ mouse = 1000;
+ break;
- break;
+ case 'p':
+ mouse_proto = atoi (optarg);
+ break;
- case 'p':
- mouse_proto = atoi(optarg);
- break;
+ default:
+ fprintf (stderr, "Usage: %s [-m]\n", argv[0]);
+ return 1;
+ }
+ }
- default:
- fprintf(stderr, "Usage: %s [-m]\n", argv[0]);
- return 1;
- }
- }
+ tk = termkey_new (0, TERMKEY_FLAG_SPACESYMBOL | TERMKEY_FLAG_CTRLC);
- tk = termkey_new(0, TERMKEY_FLAG_SPACESYMBOL|TERMKEY_FLAG_CTRLC);
+ if (!tk)
+ {
+ fprintf (stderr, "Cannot allocate termkey instance\n");
+ exit (1);
+ }
- if(!tk) {
- fprintf(stderr, "Cannot allocate termkey instance\n");
- exit(1);
- }
+ if (termkey_get_flags (tk) & TERMKEY_FLAG_UTF8)
+ printf ("Termkey in UTF-8 mode\n");
+ else if (termkey_get_flags (tk) & TERMKEY_FLAG_RAW)
+ printf ("Termkey in RAW mode\n");
- if(termkey_get_flags(tk) & TERMKEY_FLAG_UTF8)
- printf("Termkey in UTF-8 mode\n");
- else if(termkey_get_flags(tk) & TERMKEY_FLAG_RAW)
- printf("Termkey in RAW mode\n");
+ termkey_result_t ret;
+ termkey_key_t key;
- TermKeyResult ret;
- TermKeyKey key;
+ if (mouse)
+ {
+ printf ("\033[?%dhMouse mode active\n", mouse);
+ if (mouse_proto)
+ printf ("\033[?%dh", mouse_proto);
+ }
- if(mouse) {
- printf("\033[?%dhMouse mode active\n", mouse);
- if(mouse_proto)
- printf("\033[?%dh", mouse_proto);
- }
+ while ((ret = termkey_waitkey (tk, &key)) != TERMKEY_RES_EOF)
+ {
+ if (ret == TERMKEY_RES_KEY)
+ {
+ termkey_strfkey (tk, buffer, sizeof buffer, &key, format);
+ if (key.type == TERMKEY_TYPE_MOUSE)
+ {
+ int line, col;
+ termkey_interpret_mouse (tk, &key, NULL, NULL, &line, &col);
+ printf ("%s at line=%d, col=%d\n", buffer, line, col);
+ }
+ else if (key.type == TERMKEY_TYPE_POSITION)
+ {
+ int line, col;
+ termkey_interpret_position (tk, &key, &line, &col);
+ printf ("Cursor position report at line=%d, col=%d\n",
+ line, col);
+ }
+ else if (key.type == TERMKEY_TYPE_MODEREPORT)
+ {
+ int initial, mode, value;
+ termkey_interpret_modereport
+ (tk, &key, &initial, &mode, &value);
+ printf ("Mode report %s mode %d = %d\n",
+ initial ? "DEC" : "ANSI", mode, value);
+ }
+ else if (key.type == TERMKEY_TYPE_UNKNOWN_CSI)
+ {
+ long args[16];
+ size_t nargs = 16;
+ unsigned long command;
+ termkey_interpret_csi (tk, &key, args, &nargs, &command);
+ printf ("Unrecognised CSI %c %ld;%ld %c%c\n",
+ (char) (command >> 8), args[0], args[1],
+ (char) (command >> 16), (char) command);
+ }
+ else
+ printf ("Key %s\n", buffer);
- while((ret = termkey_waitkey(tk, &key)) != TERMKEY_RES_EOF) {
- if(ret == TERMKEY_RES_KEY) {
- termkey_strfkey(tk, buffer, sizeof buffer, &key, format);
- if(key.type == TERMKEY_TYPE_MOUSE) {
- int line, col;
- termkey_interpret_mouse(tk, &key, NULL, NULL, &line, &col);
- printf("%s at line=%d, col=%d\n", buffer, line, col);
- }
- else if(key.type == TERMKEY_TYPE_POSITION) {
- int line, col;
- termkey_interpret_position(tk, &key, &line, &col);
- printf("Cursor position report at line=%d, col=%d\n", line, col);
- }
- else if(key.type == TERMKEY_TYPE_MODEREPORT) {
- int initial, mode, value;
- termkey_interpret_modereport(tk, &key, &initial, &mode, &value);
- printf("Mode report %s mode %d = %d\n", initial ? "DEC" : "ANSI", mode, value);
- }
- else if(key.type == TERMKEY_TYPE_UNKNOWN_CSI) {
- long args[16];
- size_t nargs = 16;
- unsigned long command;
- termkey_interpret_csi(tk, &key, args, &nargs, &command);
- printf("Unrecognised CSI %c %ld;%ld %c%c\n", (char)(command >> 8), args[0], args[1], (char)(command >> 16), (char)command);
- }
- else {
- printf("Key %s\n", buffer);
- }
+ if (key.type == TERMKEY_TYPE_UNICODE
+ && key.modifiers & TERMKEY_KEYMOD_CTRL
+ && (key.code.codepoint == 'C' || key.code.codepoint == 'c'))
+ break;
- if(key.type == TERMKEY_TYPE_UNICODE &&
- key.modifiers & TERMKEY_KEYMOD_CTRL &&
- (key.code.codepoint == 'C' || key.code.codepoint == 'c'))
- break;
+ if (key.type == TERMKEY_TYPE_UNICODE
+ && key.modifiers == 0
+ && key.code.codepoint == '?')
+ {
+ // printf("\033[?6n"); // DECDSR 6 == request cursor position
+ printf ("\033[?1$p"); // DECRQM == request mode, DEC origin mode
+ fflush (stdout);
+ }
+ }
+ else if (ret == TERMKEY_RES_ERROR)
+ {
+ if (errno != EINTR)
+ {
+ perror("termkey_waitkey");
+ break;
+ }
+ printf ("Interrupted by signal\n");
+ }
+ }
- if(key.type == TERMKEY_TYPE_UNICODE &&
- key.modifiers == 0 &&
- key.code.codepoint == '?') {
- // printf("\033[?6n"); // DECDSR 6 == request cursor position
- printf("\033[?1$p"); // DECRQM == request mode, DEC origin mode
- fflush(stdout);
- }
- }
- else if(ret == TERMKEY_RES_ERROR) {
- if(errno != EINTR) {
- perror("termkey_waitkey");
- break;
- }
- printf("Interrupted by signal\n");
- }
- }
+ if (mouse)
+ printf ("\033[?%dlMouse mode deactivated\n", mouse);
- if(mouse)
- printf("\033[?%dlMouse mode deactivated\n", mouse);
-
- termkey_destroy(tk);
+ termkey_destroy (tk);
}