2014-09-28 03:51:45 +02:00
|
|
|
// We want optarg
|
2011-08-25 23:49:51 +02:00
|
|
|
#define _XOPEN_SOURCE 600
|
|
|
|
|
2008-02-07 01:58:41 +01:00
|
|
|
#include <stdio.h>
|
2011-08-25 23:49:51 +02:00
|
|
|
#include <unistd.h>
|
2011-08-18 12:20:41 +02:00
|
|
|
#include <errno.h>
|
2014-09-28 03:51:45 +02:00
|
|
|
#include <locale.h>
|
2008-02-07 01:58:41 +01:00
|
|
|
|
2014-10-09 23:47:24 +02:00
|
|
|
#include "termkey2.h"
|
2008-02-07 01:58:41 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
2008-12-08 23:54:14 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
TERMKEY_CHECK_VERSION;
|
2014-09-28 03:51:45 +02:00
|
|
|
setlocale (LC_CTYPE, "");
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
int mouse = 0;
|
|
|
|
int mouse_proto = 0;
|
|
|
|
termkey_format_t format = TERMKEY_FORMAT_VIM;
|
|
|
|
|
|
|
|
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;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
mouse_proto = atoi (optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf (stderr, "Usage: %s [-m]\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
tk = termkey_new (STDIN_FILENO, NULL,
|
|
|
|
TERMKEY_FLAG_SPACESYMBOL | TERMKEY_FLAG_CTRLC);
|
2014-09-23 01:38:08 +02:00
|
|
|
if (!tk)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "Cannot allocate termkey instance\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
if (termkey_get_flags (tk) & TERMKEY_FLAG_RAW)
|
2014-09-23 01:38:08 +02:00
|
|
|
printf ("Termkey in RAW mode\n");
|
2014-09-28 03:51:45 +02:00
|
|
|
else
|
|
|
|
printf ("Termkey in multibyte mode\n");
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
termkey_result_t ret;
|
|
|
|
termkey_key_t key;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
if (key.type == TERMKEY_TYPE_KEY
|
2014-09-23 01:38:08 +02:00
|
|
|
&& key.modifiers & TERMKEY_KEYMOD_CTRL
|
|
|
|
&& (key.code.codepoint == 'C' || key.code.codepoint == 'c'))
|
|
|
|
break;
|
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
if (key.type == TERMKEY_TYPE_KEY
|
2014-09-23 01:38:08 +02:00
|
|
|
&& 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)
|
|
|
|
{
|
2014-09-28 03:51:45 +02:00
|
|
|
perror ("termkey_waitkey");
|
2014-09-23 01:38:08 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf ("Interrupted by signal\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mouse)
|
|
|
|
printf ("\033[?%dlMouse mode deactivated\n", mouse);
|
|
|
|
|
|
|
|
termkey_destroy (tk);
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|