termo/termo.c

1859 lines
41 KiB
C
Raw Permalink Normal View History

2014-10-14 00:08:15 +02:00
#include "termo.h"
#include "termo-internal.h"
2008-02-07 01:58:41 +01:00
#include <ctype.h>
2008-02-09 20:01:49 +01:00
#include <errno.h>
#include <poll.h>
2008-02-07 01:58:41 +01:00
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <langinfo.h>
2008-02-07 01:58:41 +01:00
#include <stdio.h>
void
2014-10-14 00:08:15 +02:00
termo_check_version (int major, int minor)
{
2014-10-14 00:08:15 +02:00
if (major != TERMO_VERSION_MAJOR)
fprintf (stderr, "libtermo major version mismatch;"
" %d (wants) != %d (library)\n",
2014-10-14 00:08:15 +02:00
major, TERMO_VERSION_MAJOR);
else if (minor > TERMO_VERSION_MINOR)
fprintf (stderr, "libtermo minor version mismatch;"
" %d (wants) > %d (library)\n",
2014-10-14 00:08:15 +02:00
minor, TERMO_VERSION_MINOR);
else
return;
exit (1);
}
2014-10-14 00:08:15 +02:00
static termo_driver_t *drivers[] =
{
2014-10-14 00:08:15 +02:00
&termo_driver_ti,
&termo_driver_csi,
NULL,
};
// Forwards for the "protected" methods
2014-10-14 00:08:15 +02:00
static void emit_codepoint (termo_t *tk, uint32_t codepoint, termo_key_t *key);
static termo_result_t peekkey_simple (termo_t *tk,
termo_key_t *key, int flags, size_t *nbytes);
2014-10-14 00:08:15 +02:00
static termo_result_t peekkey_mouse (termo_t *tk,
termo_key_t *key, size_t *nbytes);
2014-10-14 00:08:15 +02:00
static termo_sym_t register_c0 (termo_t *tk, termo_sym_t sym,
unsigned char ctrl, const char *name);
2014-10-14 00:08:15 +02:00
static termo_sym_t register_c0_full (termo_t *tk, termo_sym_t sym,
int modifier_set, int modifier_mask, unsigned char ctrl, const char *name);
static struct
{
2014-10-14 00:08:15 +02:00
termo_sym_t sym;
const char *name;
}
keynames[] =
{
2014-10-14 00:08:15 +02:00
{ TERMO_SYM_NONE, "NONE" },
{ TERMO_SYM_BACKSPACE, "Backspace" },
{ TERMO_SYM_TAB, "Tab" },
{ TERMO_SYM_ENTER, "Enter" },
{ TERMO_SYM_ESCAPE, "Escape" },
{ TERMO_SYM_SPACE, "Space" },
{ TERMO_SYM_DEL, "DEL" },
{ TERMO_SYM_UP, "Up" },
{ TERMO_SYM_DOWN, "Down" },
{ TERMO_SYM_LEFT, "Left" },
{ TERMO_SYM_RIGHT, "Right" },
{ TERMO_SYM_BEGIN, "Begin" },
{ TERMO_SYM_FIND, "Find" },
{ TERMO_SYM_INSERT, "Insert" },
{ TERMO_SYM_DELETE, "Delete" },
{ TERMO_SYM_SELECT, "Select" },
{ TERMO_SYM_PAGEUP, "PageUp" },
{ TERMO_SYM_PAGEDOWN, "PageDown" },
{ TERMO_SYM_HOME, "Home" },
{ TERMO_SYM_END, "End" },
{ TERMO_SYM_CANCEL, "Cancel" },
{ TERMO_SYM_CLEAR, "Clear" },
{ TERMO_SYM_CLOSE, "Close" },
{ TERMO_SYM_COMMAND, "Command" },
{ TERMO_SYM_COPY, "Copy" },
{ TERMO_SYM_EXIT, "Exit" },
{ TERMO_SYM_HELP, "Help" },
{ TERMO_SYM_MARK, "Mark" },
{ TERMO_SYM_MESSAGE, "Message" },
{ TERMO_SYM_MOVE, "Move" },
{ TERMO_SYM_OPEN, "Open" },
{ TERMO_SYM_OPTIONS, "Options" },
{ TERMO_SYM_PRINT, "Print" },
{ TERMO_SYM_REDO, "Redo" },
{ TERMO_SYM_REFERENCE, "Reference" },
{ TERMO_SYM_REFRESH, "Refresh" },
{ TERMO_SYM_REPLACE, "Replace" },
{ TERMO_SYM_RESTART, "Restart" },
{ TERMO_SYM_RESUME, "Resume" },
{ TERMO_SYM_SAVE, "Save" },
{ TERMO_SYM_SUSPEND, "Suspend" },
{ TERMO_SYM_UNDO, "Undo" },
{ TERMO_SYM_KP0, "KP0" },
{ TERMO_SYM_KP1, "KP1" },
{ TERMO_SYM_KP2, "KP2" },
{ TERMO_SYM_KP3, "KP3" },
{ TERMO_SYM_KP4, "KP4" },
{ TERMO_SYM_KP5, "KP5" },
{ TERMO_SYM_KP6, "KP6" },
{ TERMO_SYM_KP7, "KP7" },
{ TERMO_SYM_KP8, "KP8" },
{ TERMO_SYM_KP9, "KP9" },
{ TERMO_SYM_KPENTER, "KPEnter" },
{ TERMO_SYM_KPPLUS, "KPPlus" },
{ TERMO_SYM_KPMINUS, "KPMinus" },
{ TERMO_SYM_KPMULT, "KPMult" },
{ TERMO_SYM_KPDIV, "KPDiv" },
{ TERMO_SYM_KPCOMMA, "KPComma" },
{ TERMO_SYM_KPPERIOD, "KPPeriod" },
{ TERMO_SYM_KPEQUALS, "KPEquals" },
2014-10-18 19:11:08 +02:00
{ 0, NULL },
};
#define CHARAT(i) (tk->buffer[tk->buffstart + (i)])
#ifdef DEBUG
// Some internal deubgging functions
static void
2014-10-14 00:08:15 +02:00
print_buffer (termo_t *tk)
{
size_t i;
for (i = 0; i < tk->buffcount && i < 20; i++)
fprintf (stderr, "%02x ", CHARAT (i));
if (tk->buffcount > 20)
fprintf (stderr, "...");
}
static void
2014-10-14 00:08:15 +02:00
print_key (termo_t *tk, termo_key_t *key)
{
switch (key->type)
{
2014-10-14 00:08:15 +02:00
case TERMO_TYPE_KEY:
fprintf (stderr, "Unicode codepoint=U+%04lx multibyte='%s'",
(long) key->code.codepoint, key->multibyte);
break;
2014-10-14 00:08:15 +02:00
case TERMO_TYPE_FUNCTION:
fprintf (stderr, "Function F%d", key->code.number);
break;
2014-10-14 00:08:15 +02:00
case TERMO_TYPE_KEYSYM:
fprintf (stderr, "Keysym sym=%d(%s)",
2014-10-14 00:08:15 +02:00
key->code.sym, termo_get_keyname (tk, key->code.sym));
break;
2014-10-14 00:08:15 +02:00
case TERMO_TYPE_MOUSE:
{
2014-10-14 00:08:15 +02:00
termo_mouse_event_t ev;
int button, line, col;
2014-10-14 00:08:15 +02:00
termo_interpret_mouse (tk, key, &ev, &button, &line, &col);
fprintf (stderr, "Mouse ev=%d button=%d pos=(%d,%d)\n",
ev, button, line, col);
break;
}
case TERMO_TYPE_FOCUS:
fprintf (stderr, "%s\n", key->code.focused ? "Focused" : "Defocused");
break;
2014-10-14 00:08:15 +02:00
case TERMO_TYPE_POSITION:
{
int line, col;
2014-10-14 00:08:15 +02:00
termo_interpret_position (tk, key, &line, &col);
fprintf (stderr, "Position report pos=(%d,%d)\n", line, col);
break;
}
2014-10-14 00:08:15 +02:00
case TERMO_TYPE_MODEREPORT:
{
int initial, mode, value;
2014-10-14 00:08:15 +02:00
termo_interpret_modereport (tk, key, &initial, &mode, &value);
fprintf (stderr, "Mode report mode=%s %d val=%d\n",
initial == '?' ? "DEC" : "ANSI", mode, value);
break;
}
2014-10-14 00:08:15 +02:00
case TERMO_TYPE_UNKNOWN_CSI:
fprintf (stderr, "unknown CSI\n");
}
int m = key->modifiers;
fprintf (stderr, " mod=%s%s%s+%02x",
2014-10-14 00:08:15 +02:00
(m & TERMO_KEYMOD_CTRL ? "C" : ""),
(m & TERMO_KEYMOD_ALT ? "A" : ""),
(m & TERMO_KEYMOD_SHIFT ? "S" : ""),
m & ~(TERMO_KEYMOD_CTRL | TERMO_KEYMOD_ALT | TERMO_KEYMOD_SHIFT));
}
static const char *
2014-10-14 00:08:15 +02:00
res2str (termo_result_t res)
{
static char errorbuffer[256];
switch (res)
{
2014-10-14 00:08:15 +02:00
case TERMO_RES_KEY:
return "TERMO_RES_KEY";
case TERMO_RES_EOF:
return "TERMO_RES_EOF";
case TERMO_RES_AGAIN:
return "TERMO_RES_AGAIN";
case TERMO_RES_NONE:
return "TERMO_RES_NONE";
case TERMO_RES_ERROR:
snprintf (errorbuffer, sizeof errorbuffer,
2014-10-14 00:08:15 +02:00
"TERMO_RES_ERROR(errno=%d)\n", errno);
return (const char*) errorbuffer;
}
return "unknown";
}
#endif
// Similar to snprintf(str, size, "%s", src) except it turns CamelCase into
// space separated values
static int
snprint_cameltospaces (char *str, size_t size, const char *src)
{
int prev_lower = 0;
size_t l = 0;
while (*src && l < size - 1)
{
if (isupper (*src) && prev_lower)
{
if (str)
str[l++] = ' ';
if (l >= size - 1)
break;
}
prev_lower = islower (*src);
str[l++] = tolower (*src++);
}
str[l] = 0;
// For consistency with snprintf, return the number of bytes that would
// have been written, excluding '\0'
for (; *src; src++)
{
if (isupper (*src) && prev_lower)
l++;
prev_lower = islower (*src);
l++;
}
return l;
}
// Similar to strcmp(str, strcamel, n) except that:
// it compares CamelCase in strcamel with space separated values in str;
// it takes char**s and updates them
// n counts bytes of strcamel, not str
static int
strpncmp_camel (const char **strp, const char **strcamelp, size_t n)
{
const char *str = *strp, *strcamel = *strcamelp;
int prev_lower = 0;
for (; (*str || *strcamel) && n; n--)
{
char b = tolower (*strcamel);
if (isupper (*strcamel) && prev_lower)
{
if (*str != ' ')
break;
str++;
if (*str != b)
break;
}
else if (*str != b)
break;
prev_lower = islower (*strcamel);
str++;
strcamel++;
}
*strp = str;
*strcamelp = strcamel;
return *str - *strcamel;
}
2014-10-14 00:08:15 +02:00
static termo_t *
termo_alloc (void)
2008-02-07 01:58:41 +01:00
{
2014-10-14 00:08:15 +02:00
termo_t *tk = malloc (sizeof *tk);
if (!tk)
return NULL;
// Default all the object fields but don't allocate anything
tk->fd = -1;
tk->flags = 0;
tk->canonflags = 0;
tk->buffer = NULL;
tk->buffstart = 0;
tk->buffcount = 0;
tk->buffsize = 256; // bytes
tk->hightide = 0;
tk->restore_termios_valid = false;
tk->waittime = 50; // msec
tk->is_closed = false;
tk->is_started = false;
tk->nkeynames = 64;
tk->keynames = NULL;
for (int i = 0; i < 32; i++)
2014-10-14 00:08:15 +02:00
tk->c0[i].sym = TERMO_SYM_NONE;
tk->drivers = NULL;
tk->method.emit_codepoint = &emit_codepoint;
tk->method.peekkey_simple = &peekkey_simple;
tk->method.peekkey_mouse = &peekkey_mouse;
tk->mouse_proto = TERMO_MOUSE_PROTO_NONE;
tk->mouse_tracking = TERMO_MOUSE_TRACKING_CLICK;
tk->guessed_mouse_proto = TERMO_MOUSE_PROTO_NONE;
tk->ti_data = NULL;
tk->ti_method.set_mouse_proto = NULL;
tk->ti_method.set_mouse_tracking_mode = NULL;
return tk;
}
static int
2014-10-14 00:08:15 +02:00
termo_init (termo_t *tk, const char *term, const char *encoding)
{
if (!encoding)
encoding = nl_langinfo (CODESET);
// If we don't specify the endianity, iconv() outputs the BOM first
static const uint16_t endianity = 0x0102;
const char *utf32 = (*(uint8_t *) &endianity == 0x01)
? "UTF-32BE" : "UTF-32LE";
if ((tk->to_utf32_conv = iconv_open (utf32, encoding)) == (iconv_t) -1)
return 0;
if ((tk->from_utf32_conv = iconv_open (encoding, utf32)) == (iconv_t) -1)
goto abort_free_to_utf32;
tk->buffer = malloc (tk->buffsize);
if (!tk->buffer)
goto abort_free_from_utf32;
tk->keynames = malloc (sizeof tk->keynames[0] * tk->nkeynames);
if (!tk->keynames)
goto abort_free_buffer;
int i;
for (i = 0; i < tk->nkeynames; i++)
tk->keynames[i] = NULL;
for (i = 0; keynames[i].name; i++)
2014-10-14 00:08:15 +02:00
if (termo_register_keyname (tk,
keynames[i].sym, keynames[i].name) == -1)
goto abort_free_keynames;
2014-10-14 00:08:15 +02:00
register_c0 (tk, TERMO_SYM_TAB, 0x09, NULL);
register_c0 (tk, TERMO_SYM_ENTER, 0x0d, NULL);
register_c0 (tk, TERMO_SYM_ESCAPE, 0x1b, NULL);
2014-10-14 00:08:15 +02:00
termo_driver_node_t **tail = &tk->drivers;
for (i = 0; drivers[i]; i++)
{
void *info = (*drivers[i]->new_driver) (tk, term);
if (!info)
continue;
#ifdef DEBUG
fprintf (stderr, "Loading the %s driver...\n", drivers[i]->name);
#endif
2014-10-14 00:08:15 +02:00
termo_driver_node_t *thisdrv = malloc (sizeof *thisdrv);
if (!thisdrv)
goto abort_free_drivers;
thisdrv->driver = drivers[i];
thisdrv->info = info;
thisdrv->next = NULL;
*tail = thisdrv;
tail = &thisdrv->next;
#ifdef DEBUG
fprintf (stderr, "Loaded %s driver\n", drivers[i]->name);
#endif
}
if (!tk->drivers)
{
errno = ENOENT;
goto abort_free_keynames;
}
return 1;
abort_free_drivers:
2014-10-14 00:08:15 +02:00
for (termo_driver_node_t *p = tk->drivers; p; )
{
(*p->driver->free_driver) (p->info);
2014-10-14 00:08:15 +02:00
termo_driver_node_t *next = p->next;
free (p);
p = next;
}
abort_free_keynames:
free (tk->keynames);
abort_free_buffer:
free (tk->buffer);
abort_free_from_utf32:
iconv_close (tk->from_utf32_conv);
abort_free_to_utf32:
iconv_close (tk->to_utf32_conv);
return 0;
}
2014-10-14 00:08:15 +02:00
termo_t *
termo_new (int fd, const char *encoding, int flags)
{
2014-10-14 00:08:15 +02:00
termo_t *tk = termo_alloc ();
if (!tk)
return NULL;
tk->fd = fd;
2014-10-14 00:08:15 +02:00
termo_set_flags (tk, flags);
const char *term = getenv ("TERM");
if (!termo_init (tk, term, encoding))
free (tk);
else if (!(flags & TERMO_FLAG_NOSTART) && !termo_start (tk))
termo_free (tk);
else
return tk;
return NULL;
2012-01-26 13:55:16 +01:00
}
2014-10-14 00:08:15 +02:00
termo_t *
termo_new_abstract (const char *term, const char *encoding, int flags)
2012-01-26 13:55:16 +01:00
{
2014-10-14 00:08:15 +02:00
termo_t *tk = termo_alloc ();
if (!tk)
return NULL;
tk->fd = -1;
2014-10-14 00:08:15 +02:00
termo_set_flags (tk, flags);
2014-10-14 00:08:15 +02:00
if (!termo_init (tk, term, encoding))
free (tk);
else if (!(flags & TERMO_FLAG_NOSTART) && !termo_start (tk))
termo_free (tk);
else
return tk;
return NULL;
2008-02-07 01:58:41 +01:00
}
void
2014-10-14 00:08:15 +02:00
termo_free (termo_t *tk)
2008-02-23 21:07:48 +01:00
{
free (tk->buffer); tk->buffer = NULL;
free (tk->keynames); tk->keynames = NULL;
iconv_close (tk->to_utf32_conv);
tk->to_utf32_conv = (iconv_t) -1;
iconv_close (tk->from_utf32_conv);
tk->from_utf32_conv = (iconv_t) -1;
2014-10-14 00:08:15 +02:00
termo_driver_node_t *p, *next;
for (p = tk->drivers; p; p = next)
{
(*p->driver->free_driver) (p->info);
next = p->next;
free (p);
}
free (tk);
2008-02-23 21:07:48 +01:00
}
void
2014-10-14 00:08:15 +02:00
termo_destroy (termo_t *tk)
{
if (tk->is_started)
2014-10-14 00:08:15 +02:00
termo_stop (tk);
2014-10-14 00:08:15 +02:00
termo_free (tk);
}
int
2014-10-14 00:08:15 +02:00
termo_start (termo_t *tk)
{
if (tk->is_started)
return 1;
2014-10-14 00:08:15 +02:00
if (tk->fd != -1 && !(tk->flags & TERMO_FLAG_NOTERMIOS))
{
struct termios termios;
if (tcgetattr (tk->fd, &termios) == 0)
{
tk->restore_termios = termios;
tk->restore_termios_valid = true;
termios.c_iflag &= ~(IXON|INLCR|ICRNL);
termios.c_lflag &= ~(ICANON|ECHO);
termios.c_cc[VMIN] = 1;
termios.c_cc[VTIME] = 0;
2014-10-14 00:08:15 +02:00
if (tk->flags & TERMO_FLAG_CTRLC)
// want no signal keys at all, so just disable ISIG
termios.c_lflag &= ~ISIG;
else
{
// Disable ^\ == VQUIT and ^D == VSUSP but leave ^C as SIGINT
termios.c_cc[VQUIT] = _POSIX_VDISABLE;
termios.c_cc[VSUSP] = _POSIX_VDISABLE;
// Some OSes have ^Y == VDSUSP
#ifdef VDSUSP
termios.c_cc[VDSUSP] = _POSIX_VDISABLE;
#endif
}
#ifdef DEBUG
fprintf (stderr, "Setting termios(3) flags\n");
#endif
tcsetattr (tk->fd, TCSANOW, &termios);
}
}
2014-10-14 00:08:15 +02:00
termo_driver_node_t *p;
for (p = tk->drivers; p; p = p->next)
if (p->driver->start_driver)
if (!(*p->driver->start_driver) (tk, p->info))
return 0;
#ifdef DEBUG
2014-10-14 00:08:15 +02:00
fprintf (stderr, "Drivers started; termo instance %p is ready\n", tk);
#endif
tk->is_started = 1;
return 1;
}
int
2014-10-14 00:08:15 +02:00
termo_stop (termo_t *tk)
{
if (!tk->is_started)
return 1;
2014-10-14 00:08:15 +02:00
struct termo_driver_node *p;
for (p = tk->drivers; p; p = p->next)
if (p->driver->stop_driver)
(*p->driver->stop_driver) (tk, p->info);
if (tk->restore_termios_valid)
tcsetattr (tk->fd, TCSANOW, &tk->restore_termios);
tk->is_started = false;
return 1;
}
int
2014-10-14 00:08:15 +02:00
termo_is_started (termo_t *tk)
{
return tk->is_started;
}
int
2014-10-14 00:08:15 +02:00
termo_get_fd (termo_t *tk)
2008-12-01 23:36:14 +01:00
{
return tk->fd;
2008-12-01 23:36:14 +01:00
}
int
2014-10-14 00:08:15 +02:00
termo_get_flags (termo_t *tk)
{
return tk->flags;
}
void
2014-10-14 00:08:15 +02:00
termo_set_flags (termo_t *tk, int newflags)