You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1859 lines
41 KiB
C

9 years ago
#include "termo.h"
#include "termo-internal.h"
16 years ago
#include <ctype.h>
#include <errno.h>
#include <poll.h>
16 years ago
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <langinfo.h>
16 years ago
#include <stdio.h>
void
9 years ago
termo_check_version (int major, int minor)
{
9 years ago
if (major != TERMO_VERSION_MAJOR)
fprintf (stderr, "libtermo major version mismatch;"
" %d (wants) != %d (library)\n",
9 years ago
major, TERMO_VERSION_MAJOR);
else if (minor > TERMO_VERSION_MINOR)
fprintf (stderr, "libtermo minor version mismatch;"
" %d (wants) > %d (library)\n",
9 years ago
minor, TERMO_VERSION_MINOR);
else
return;
exit (1);
}
9 years ago
static termo_driver_t *drivers[] =
{
9 years ago
&termo_driver_ti,
&termo_driver_csi,
NULL,
};
// Forwards for the "protected" methods
9 years ago
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);
9 years ago
static termo_result_t peekkey_mouse (termo_t *tk,
termo_key_t *key, size_t *nbytes);
9 years ago
static termo_sym_t register_c0 (termo_t *tk, termo_sym_t sym,
unsigned char ctrl, const char *name);
9 years ago
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
{
9 years ago
termo_sym_t sym;
const char *name;
}
keynames[] =
{
9 years ago
{ 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" },
9 years ago
{ 0, NULL },
};
#define CHARAT(i) (tk->buffer[tk->buffstart + (i)])
#ifdef DEBUG
// Some internal deubgging functions
static void
9 years ago
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
9 years ago
print_key (termo_t *tk, termo_key_t *key)
{
switch (key->type)
{
9 years ago
case TERMO_TYPE_KEY:
fprintf (stderr, "Unicode codepoint=U+%04lx multibyte='%s'",
(long) key->code.codepoint, key->multibyte);
break;
9 years ago
case TERMO_TYPE_FUNCTION:
fprintf (stderr, "Function F%d", key->code.number);
break;
9 years ago
case TERMO_TYPE_KEYSYM:
fprintf (stderr, "Keysym sym=%d(%s)",
9 years ago
key->code.sym, termo_get_keyname (tk, key->code.sym));
break;
9 years ago
case TERMO_TYPE_MOUSE:
{
9 years ago
termo_mouse_event_t ev;
int button, line, col;
9 years ago
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;
9 years ago
case TERMO_TYPE_POSITION:
{
int line, col;
9 years ago
termo_interpret_position (tk, key, &line, &col);
fprintf (stderr, "Position report pos=(%d,%d)\n", line, col);
break;
}
9 years ago
case TERMO_TYPE_MODEREPORT:
{
int initial, mode, value;
9 years ago
termo_interpret_modereport (tk, key, &initial, &mode, &value);
fprintf (stderr, "Mode report mode=%s %d val=%d\n",
initial == '?' ? "DEC" : "ANSI", mode, value);
break;
}
9 years ago
case TERMO_TYPE_UNKNOWN_CSI:
fprintf (stderr, "unknown CSI\n");
}
int m = key->modifiers;
fprintf (stderr, " mod=%s%s%s+%02x",
9 years ago
(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 *
9 years ago
res2str (termo_result_t res)
{
static char errorbuffer[256];
switch (res)
{
9 years ago
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,
9 years ago
"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;
}
9 years ago
static termo_t *
termo_alloc (void)
16 years ago
{
9 years ago
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++)
9 years ago
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
9 years ago
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++)
9 years ago
if (termo_register_keyname (tk,
keynames[i].sym, keynames[i].name) == -1)
goto abort_free_keynames;
9 years ago
register_c0 (tk, TERMO_SYM_TAB, 0x09, NULL);
register_c0 (tk, TERMO_SYM_ENTER, 0x0d, NULL);
register_c0 (tk, TERMO_SYM_ESCAPE, 0x1b, NULL);
9 years ago
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
9 years ago
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:
9 years ago
for (termo_driver_node_t *p = tk->drivers; p; )
{
(*p->driver->free_driver) (p->info);
9 years ago
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;
}
9 years ago
termo_t *
termo_new (int fd, const char *encoding, int flags)
{
9 years ago
termo_t *tk = termo_alloc ();
if (!tk)
return NULL;
tk->fd = fd;
9 years ago
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;
}
9 years ago
termo_t *
termo_new_abstract (const char *term, const char *encoding, int flags)
{
9 years ago
termo_t *tk = termo_alloc ();
if (!tk)
return NULL;
tk->fd = -1;
9 years ago
termo_set_flags (tk, flags);
9 years ago
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;
16 years ago
}
void
9 years ago
termo_free (termo_t *tk)
{
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;
9 years ago
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);
}
void
9 years ago
termo_destroy (termo_t *tk)
{
if (tk->is_started)
9 years ago
termo_stop (tk);
9 years ago
termo_free (tk);
}
int
9 years ago
termo_start (termo_t *tk)
{
if (tk->is_started)
return 1;
9 years ago
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;
9 years ago
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);
}
}
9 years ago
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
9 years ago
fprintf (stderr, "Drivers started; termo instance %p is ready\n", tk);
#endif
tk->is_started = 1;
return 1;
}
int
9 years ago
termo_stop (termo_t *tk)
{
if (!tk->is_started)
return 1;
9 years ago
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
9 years ago
termo_is_started (termo_t *tk)
{
return tk->is_started;
}
int
9 years ago
termo_get_fd (termo_t *tk)
{
return tk->fd;
}
int
9 years ago
termo_get_flags (termo_t *tk)
{
return tk->flags;
}
void
9 years ago
termo_set_flags (termo_t *tk, int newflags)
{
tk->flags = newflags;
9 years ago
if (tk->flags & TERMO_FLAG_SPACESYMBOL)
tk->canonflags |= TERMO_CANON_SPACESYMBOL;
else
9 years ago
tk->canonflags &= ~TERMO_CANON_SPACESYMBOL;
}
void
9 years ago
termo_set_waittime (termo_t *tk, int msec)
{
tk->waittime = msec;
}
int
9 years ago
termo_get_waittime (termo_t *tk)
{
return tk->waittime;
16 years ago
}
int
9 years ago
termo_get_canonflags (termo_t *tk)
{
return tk->canonflags;
}
void
9 years ago
termo_set_canonflags (termo_t *tk, int flags)
{
tk->canonflags = flags;
9 years ago
if (tk->canonflags & TERMO_CANON_SPACESYMBOL)
tk->flags |= TERMO_FLAG_SPACESYMBOL;
else
9 years ago
tk->flags &= ~TERMO_FLAG_SPACESYMBOL;
}
size_t
9 years ago
termo_get_buffer_size (termo_t *tk)
{
return tk->buffsize;
}
int
9 years ago
termo_set_buffer_size (termo_t *tk, size_t size)
{
unsigned char *buffer = realloc (tk->buffer, size);
if (!buffer)
return 0;
tk->buffer = buffer;
tk->buffsize = size;
return 1;
}
size_t
9 years ago
termo_get_buffer_remaining (termo_t *tk)
{
// Return the total number of free bytes in the buffer,
// because that's what is available to the user.
return tk->buffsize - tk->buffcount;
}
termo_mouse_proto_t
termo_get_mouse_proto (termo_t *tk)
{
return tk->mouse_proto;
}
termo_mouse_proto_t
termo_guess_mouse_proto (termo_t *tk)
{
return tk->guessed_mouse_proto;
}
int
termo_set_mouse_proto (termo_t *tk, termo_mouse_proto_t proto)
{
termo_mouse_proto_t old_proto = tk->mouse_proto;
tk->mouse_proto = proto;
// Call the TI driver to apply the change if needed
if (proto == old_proto
|| !tk->is_started
|| !tk->ti_method.set_mouse_proto)
return true;
// Unsetting the protocol disables tracking; this is a bit hackish
if (!tk->ti_method.set_mouse_tracking_mode (tk->ti_data,
tk->mouse_tracking, proto != TERMO_MOUSE_PROTO_NONE))
return false;
return tk->ti_method.set_mouse_proto (tk->ti_data, old_proto, false)
&& tk->ti_method.set_mouse_proto (tk->ti_data, proto, true);
}
termo_mouse_tracking_t
termo_get_mouse_tracking_mode (termo_t *tk)
{
return tk->mouse_tracking;
}
int
termo_set_mouse_tracking_mode (termo_t *tk, termo_mouse_tracking_t mode)
{
termo_mouse_tracking_t old_mode = tk->mouse_tracking;
tk->mouse_tracking = mode;
// Call the TI driver to apply the change if needed
if (mode == old_mode
|| !tk->is_started
|| !tk->ti_method.set_mouse_tracking_mode)
return true;
return tk->ti_method.set_mouse_tracking_mode (tk->ti_data, old_mode, false)
&& tk->ti_method.set_mouse_tracking_mode (tk->ti_data, mode, true);
}
static void
9 years ago
eat_bytes (termo_t *tk, size_t count)
{
if (count >= tk->buffcount)
{
tk->buffstart = 0;
tk->buffcount = 0;
return;
}
tk->buffstart += count;
tk->buffcount -= count;
}
#define MULTIBYTE_INVALID '?'
static void
9 years ago
fill_multibyte (termo_t *tk, termo_key_t *key)
{
size_t codepoint_len = sizeof key->code.codepoint;
char *codepoint_ptr = (char *) &key->code.codepoint;
size_t multibyte_len = sizeof key->multibyte;
char *multibyte_ptr = (char *) key->multibyte;
size_t result = iconv (tk->from_utf32_conv,
&codepoint_ptr, &codepoint_len, &multibyte_ptr, &multibyte_len);
size_t output = sizeof key->multibyte - multibyte_len;
// Something broke
if (result == (size_t) -1 || output == 0)
{
key->multibyte[0] = MULTIBYTE_INVALID;
key->multibyte[1] = 0;
return;
}
// Append a null character, as it wasn't part of the input
key->multibyte[output] = 0;
}
9 years ago
static termo_result_t
parse_multibyte (termo_t *tk, const unsigned char *bytes, size_t len,
uint32_t *cp, size_t *nbytep)