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>
|
2014-11-19 02:41:36 +01:00
|
|
|
#include <strings.h>
|
2008-02-07 01:58:41 +01:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
#include "termo.h"
|
2008-02-07 01:58:41 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
int
|
2021-10-30 02:53:18 +02:00
|
|
|
main (int argc, char *argv[])
|
2008-12-08 23:54:14 +01:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
TERMO_CHECK_VERSION;
|
2014-09-28 03:51:45 +02:00
|
|
|
setlocale (LC_CTYPE, "");
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-11-19 02:41:36 +01:00
|
|
|
termo_mouse_tracking_t mouse = TERMO_MOUSE_TRACKING_OFF;
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_format_t format = TERMO_FORMAT_VIM;
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
char buffer[50];
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_t *tk;
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
int opt;
|
2014-11-19 02:41:36 +01:00
|
|
|
while ((opt = getopt (argc, argv, "m::")) != -1)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
switch (opt)
|
|
|
|
{
|
|
|
|
case 'm':
|
2014-11-19 02:41:36 +01:00
|
|
|
if (!optarg)
|
|
|
|
mouse = TERMO_MOUSE_TRACKING_DRAG;
|
|
|
|
else if (!strcasecmp (optarg, "off"))
|
|
|
|
mouse = TERMO_MOUSE_TRACKING_OFF;
|
|
|
|
else if (!strcasecmp (optarg, "click"))
|
|
|
|
mouse = TERMO_MOUSE_TRACKING_CLICK;
|
|
|
|
else if (!strcasecmp (optarg, "drag"))
|
|
|
|
mouse = TERMO_MOUSE_TRACKING_DRAG;
|
|
|
|
else if (!strcasecmp (optarg, "move"))
|
|
|
|
mouse = TERMO_MOUSE_TRACKING_MOVE;
|
2014-09-23 01:38:08 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-11-19 02:41:36 +01:00
|
|
|
fprintf (stderr,
|
|
|
|
"Usage: %s [ -m [ off | click | drag | move ]]\n", argv[0]);
|
2014-09-23 01:38:08 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
tk = termo_new (STDIN_FILENO, NULL,
|
|
|
|
TERMO_FLAG_SPACESYMBOL | TERMO_FLAG_CTRLC);
|
2014-09-23 01:38:08 +02:00
|
|
|
if (!tk)
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
fprintf (stderr, "Cannot allocate termo instance\n");
|
2014-09-23 01:38:08 +02:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (termo_get_flags (tk) & TERMO_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
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_result_t ret;
|
|
|
|
termo_key_t key;
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-11-19 02:41:36 +01:00
|
|
|
termo_set_mouse_tracking_mode (tk, mouse);
|
|
|
|
if (mouse != TERMO_MOUSE_TRACKING_OFF)
|
|
|
|
printf ("Mouse mode active\n");
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
while ((ret = termo_waitkey (tk, &key)) != TERMO_RES_EOF)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
if (ret == TERMO_RES_KEY)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_strfkey (tk, buffer, sizeof buffer, &key, format);
|
|
|
|
if (key.type == TERMO_TYPE_MOUSE)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
int line, col;
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_interpret_mouse (tk, &key, NULL, NULL, &line, &col);
|
2014-09-23 01:38:08 +02:00
|
|
|
printf ("%s at line=%d, col=%d\n", buffer, line, col);
|
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
else if (key.type == TERMO_TYPE_POSITION)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
int line, col;
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_interpret_position (tk, &key, &line, &col);
|
2014-09-23 01:38:08 +02:00
|
|
|
printf ("Cursor position report at line=%d, col=%d\n",
|
|
|
|
line, col);
|
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
else if (key.type == TERMO_TYPE_MODEREPORT)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
int initial, mode, value;
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_interpret_modereport
|
2014-09-23 01:38:08 +02:00
|
|
|
(tk, &key, &initial, &mode, &value);
|
|
|
|
printf ("Mode report %s mode %d = %d\n",
|
|
|
|
initial ? "DEC" : "ANSI", mode, value);
|
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
else if (key.type == TERMO_TYPE_UNKNOWN_CSI)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
long args[16];
|
|
|
|
size_t nargs = 16;
|
|
|
|
unsigned long command;
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_interpret_csi (tk, &key, args, &nargs, &command);
|
2014-09-23 01:38:08 +02:00
|
|
|
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-10-14 00:08:15 +02:00
|
|
|
if (key.type == TERMO_TYPE_KEY
|
|
|
|
&& key.modifiers & TERMO_KEYMOD_CTRL
|
2014-09-23 01:38:08 +02:00
|
|
|
&& (key.code.codepoint == 'C' || key.code.codepoint == 'c'))
|
|
|
|
break;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (key.type == TERMO_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);
|
|
|
|
}
|
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
else if (ret == TERMO_RES_ERROR)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
if (errno != EINTR)
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
perror ("termo_waitkey");
|
2014-09-23 01:38:08 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf ("Interrupted by signal\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_destroy (tk);
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|