Removed ncurses dependeny.

This commit is contained in:
Johannes Findeisen 2026-11-27 02:27:34 +01:00
commit 09e72d1b1f
3 changed files with 300 additions and 153 deletions

View file

@ -1,7 +1,6 @@
CC ?= cc CC ?= cc
CFLAGS ?= -O2 CFLAGS ?= -O2 -Wall -Wextra -Wpedantic -pthread
CFLAGS ?= -O2 -Wall -Wextra -Wpedantic -pthread $(shell pkg-config --cflags ncurses 2>/dev/null) LDLIBS ?= -pthread
LDLIBS ?= -pthread $(shell pkg-config --libs ncurses 2>/dev/null || echo -lncurses)
PREFIX ?= /usr/local PREFIX ?= /usr/local
BINDIR = $(PREFIX)/bin BINDIR = $(PREFIX)/bin
MANDIR ?= $(PREFIX)/share/man MANDIR ?= $(PREFIX)/share/man

View file

@ -22,8 +22,8 @@ Connection attempts (DNS, connect and TLS handshake) run in the background, so
the interface opens and stays usable even when the network is down or DNS is the interface opens and stays usable even when the network is down or DNS is
slow; failed servers retry automatically. slow; failed servers retry automatically.
Requires ncurses and a C compiler. TLS is compiled in automatically when Requires a C compiler and an ANSI-compatible terminal with an alternate screen.
OpenSSL development files are available (`pkg-config --exists openssl`); no TLS is compiled in automatically when OpenSSL development files are available (`pkg-config --exists openssl`); no
special flags are needed. Without OpenSSL the client still builds, but server special flags are needed. Without OpenSSL the client still builds, but server
lines marked `ssl`/`tls` cannot connect. lines marked `ssl`/`tls` cannot connect.

444
irc9.c
View file

@ -5,8 +5,11 @@
* and ctrl-n / ctrl-p to walk through them. * and ctrl-n / ctrl-p to walk through them.
*/ */
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#endif
#include <ctype.h> #include <ctype.h>
#include <curses.h>
#ifdef __APPLE__ #ifdef __APPLE__
#include <mach-o/dyld.h> #include <mach-o/dyld.h>
#endif #endif
@ -23,9 +26,14 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <strings.h>
#include <sys/ioctl.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <termios.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <wchar.h>
#include <wctype.h>
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
#include <openssl/err.h> #include <openssl/err.h>
#include <openssl/ssl.h> #include <openssl/ssl.h>
@ -136,11 +144,16 @@ static int connect_pipe[2] = { -1, -1 };
static Window *curwin; static Window *curwin;
static volatile sig_atomic_t quit_flag; static volatile sig_atomic_t quit_flag;
static int dirty = 1; static int dirty = 1;
static WINDOW *titlewin, *treewin, *mainwin, *inputwin; static int cols = 80, lines = 24;
static int treew = TREEW, mainh = 1; static int treew = TREEW, mainh = 1;
static int sel_attr = A_REVERSE; /* opaque highlight only when default colours are unsupported */ static struct termios saved_terminal;
static int ui_attr = A_REVERSE; /* title and status bar emphasis */ static int terminal_active;
static int transparent_bg = 0; /* terminal default background, i.e. transparent where supported */ static int terminal_color;
enum {
KEY_UP = 256, KEY_DOWN, KEY_LEFT, KEY_RIGHT,
KEY_HOME, KEY_END, KEY_PPAGE, KEY_NPAGE
};
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
static SSL_CTX *tls_ctx; static SSL_CTX *tls_ctx;
@ -167,9 +180,18 @@ static int comp_count, comp_start, comp_end;
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* small helpers */ /* small helpers */
static void terminal_finish(void)
{
if (!terminal_active) return;
fputs("\033[0m\033[?25h\033[?1l\033>\033[?1049l", stdout);
fflush(stdout);
tcsetattr(STDIN_FILENO, TCSANOW, &saved_terminal);
terminal_active = 0;
}
static void fatal(const char *msg) static void fatal(const char *msg)
{ {
endwin(); terminal_finish();
fprintf(stderr, "irc9: %s\n", msg); fprintf(stderr, "irc9: %s\n", msg);
exit(1); exit(1);
} }
@ -1814,7 +1836,6 @@ static void submit_input(void)
static void handle_key(int c) static void handle_key(int c)
{ {
if (c == ERR) return;
if (c != '\t') comp_active = 0; if (c != '\t') comp_active = 0;
if (c == '\t') { complete_input(); return; } if (c == '\t') { complete_input(); return; }
if (c == 0x0e) { switch_window(1); return; } /* ctrl-n */ if (c == 0x0e) { switch_window(1); return; } /* ctrl-n */
@ -1827,7 +1848,7 @@ static void handle_key(int c)
if (c == '\n' || c == '\r') { submit_input(); return; } if (c == '\n' || c == '\r') { submit_input(); return; }
if (c == 127 || c == 8 || c == KEY_BACKSPACE) { if (c == 127 || c == 8) {
if (input_pos > 0) { if (input_pos > 0) {
memmove(input + input_pos - 1, input + input_pos, memmove(input + input_pos - 1, input + input_pos,
(size_t)(input_len - input_pos) + 1); (size_t)(input_len - input_pos) + 1);
@ -1877,25 +1898,57 @@ static void handle_key(int c)
static void layout(void) static void layout(void)
{ {
if (titlewin) delwin(titlewin); treew = cols >= 60 ? TREEW : (cols >= 30 ? 16 : 8);
if (treewin) delwin(treewin); if (treew > cols / 2) treew = cols / 2;
if (mainwin) delwin(mainwin); mainh = lines > 4 ? lines - 4 : 0;
if (inputwin) delwin(inputwin); }
treew = COLS >= 60 ? TREEW : (COLS >= 30 ? 16 : 8); static void terminal_move(int row, int col)
if (treew > COLS / 2) treew = COLS / 2; {
mainh = LINES - 4; printf("\033[%d;%dH", row + 1, col + 1);
if (mainh < 1) mainh = 1; }
titlewin = newwin(1, COLS, 0, 0); static void terminal_repeat(int c, int count)
treewin = newwin(mainh, treew, 1, 0); {
mainwin = newwin(mainh, COLS - treew, 1, treew); for (int i = 0; i < count; i++) putchar(c);
inputwin = newwin(3, COLS, LINES - 3, 0); }
/* Consume complete printable characters without forwarding terminal controls. */
static size_t terminal_character(const char *s, size_t available, int *width)
{
mbstate_t state = {0};
wchar_t wc;
size_t len = mbrtowc(&wc, s, available, &state);
*width = 1;
if (len == 0 || len > available) return 0;
if (iswcntrl(wc)) return 0;
*width = wcwidth(wc);
if (*width < 0) {
*width = 1;
return 0;
}
return len;
}
static void terminal_text(const char *s, int columns)
{
size_t remaining = strlen(s);
while (remaining && columns >= 0) {
int width;
size_t len = terminal_character(s, remaining, &width);
if (width > columns) break;
if (len) fwrite(s, 1, len, stdout);
else { putchar('?'); len = 1; }
columns -= width;
remaining -= len;
s += len;
}
} }
static int ircfmt_vislen(const char *s) static int ircfmt_vislen(const char *s)
{ {
int n = 0; int n = 0;
size_t length = strlen(s);
for (size_t i = 0; s[i]; i++) { for (size_t i = 0; s[i]; i++) {
unsigned char c = (unsigned char)s[i]; unsigned char c = (unsigned char)s[i];
if (c == 0x02 || c == 0x1d || c == 0x1f || c == 0x16 || c == 0x0f) continue; if (c == 0x02 || c == 0x1d || c == 0x1f || c == 0x16 || c == 0x0f) continue;
@ -1912,16 +1965,20 @@ static int ircfmt_vislen(const char *s)
i--; i--;
continue; continue;
} }
n++; int width;
size_t len = terminal_character(s + i, length - i, &width);
if (len) i += len - 1;
n += width;
} }
return n; return n;
} }
static size_t ircfmt_bytes_for_vis(const char *s, int vis) static size_t ircfmt_bytes_for_vis(const char *s, int vis, int *used)
{ {
int n = 0; int n = 0;
size_t length = strlen(s);
size_t i = 0; size_t i = 0;
for (; s[i] && n < vis; i++) { for (; s[i]; i++) {
unsigned char c = (unsigned char)s[i]; unsigned char c = (unsigned char)s[i];
if (c == 0x02 || c == 0x1d || c == 0x1f || c == 0x16 || c == 0x0f) continue; if (c == 0x02 || c == 0x1d || c == 0x1f || c == 0x16 || c == 0x0f) continue;
if (c == 0x03) { if (c == 0x03) {
@ -1937,8 +1994,13 @@ static size_t ircfmt_bytes_for_vis(const char *s, int vis)
i = j - 1; i = j - 1;
continue; continue;
} }
n++; int width;
size_t len = terminal_character(s + i, length - i, &width);
if (n && width > vis - n) break;
if (len) i += len - 1;
n += width;
} }
*used = n;
return i; return i;
} }
@ -1962,9 +2024,10 @@ static void push_wrapped(char **rows, int *nr, int maxrows, const char *s, int w
size_t tlbytes = strlen(tok); size_t tlbytes = strlen(tok);
if (curvis == 0) { if (curvis == 0) {
while (tlvis > width) { while (tlvis > width) {
size_t b = ircfmt_bytes_for_vis(tok, width); int used;
size_t b = ircfmt_bytes_for_vis(tok, width, &used);
if (nseg < 128) segs[nseg++] = dupnstr(tok, b); if (nseg < 128) segs[nseg++] = dupnstr(tok, b);
tok += b; tlvis -= width; tlbytes -= b; tok += b; tlvis -= used; tlbytes -= b;
} }
memcpy(cur, tok, tlbytes); cur[tlbytes] = 0; clen = tlbytes; curvis = tlvis; memcpy(cur, tok, tlbytes); cur[tlbytes] = 0; clen = tlbytes; curvis = tlvis;
} else if (curvis + 1 + tlvis <= width) { } else if (curvis + 1 + tlvis <= width) {
@ -1976,9 +2039,10 @@ static void push_wrapped(char **rows, int *nr, int maxrows, const char *s, int w
} else { } else {
if (nseg < 128) segs[nseg++] = dupstr(cur); if (nseg < 128) segs[nseg++] = dupstr(cur);
while (tlvis > width) { while (tlvis > width) {
size_t b = ircfmt_bytes_for_vis(tok, width); int used;
size_t b = ircfmt_bytes_for_vis(tok, width, &used);
if (nseg < 128) segs[nseg++] = dupnstr(tok, b); if (nseg < 128) segs[nseg++] = dupnstr(tok, b);
tok += b; tlvis -= width; tlbytes -= b; tok += b; tlvis -= used; tlbytes -= b;
} }
memcpy(cur, tok, tlbytes); cur[tlbytes] = 0; clen = tlbytes; curvis = tlvis; memcpy(cur, tok, tlbytes); cur[tlbytes] = 0; clen = tlbytes; curvis = tlvis;
} }
@ -1996,78 +2060,72 @@ static void push_wrapped(char **rows, int *nr, int maxrows, const char *s, int w
static void draw_tree(void) static void draw_tree(void)
{ {
werase(treewin); if (treew < 2 || mainh < 2) return;
wborder(treewin, 0, ACS_VLINE, 0, 0, 0, 0, 0, 0); fputs("\033(0", stdout);
terminal_move(1, 0);
putchar('l');
terminal_repeat('q', treew - 2);
putchar('k');
for (int row = 2; row < mainh; row++) {
terminal_move(row, 0);
putchar('x');
terminal_move(row, treew - 1);
putchar('x');
}
terminal_move(mainh, 0);
putchar('m');
terminal_repeat('q', treew - 2);
putchar('j');
fputs("\033(B", stdout);
int y = 1; int y = 1;
for (Server *s = servers; s; s = s->next) { for (Server *s = servers; s; s = s->next) {
if (y >= mainh - 1) break; if (y >= mainh - 1) break;
int selected = (serverwin(s) == curwin); int selected = (serverwin(s) == curwin);
if (selected) wattron(treewin, sel_attr); if (treew > 4) {
else wattron(treewin, A_BOLD); terminal_move(y, 1);
mvwprintw(treewin, y++, 1, "%c %-.*s", s->connected ? '*' : ' ', fputs(selected ? (terminal_color ? "\033[1;4m" : "\033[7m") : "\033[1m", stdout);
treew - 4, s->host); printf("%c ", s->connected ? '*' : ' ');
if (selected) wattroff(treewin, sel_attr); terminal_text(s->host, treew - 4);
else wattroff(treewin, A_BOLD); fputs("\033[0m", stdout);
}
y++;
for (Window *w = s->windows->next; w; w = w->next) { for (Window *w = s->windows->next; w; w = w->next) {
if (y >= mainh - 1) break; if (y >= mainh - 1) break;
char label[CHANLEN + 16]; char label[CHANLEN + 16];
if (w->unread) snprintf(label, sizeof label, "%s(%d)", w->name, w->unread); if (w->unread) snprintf(label, sizeof label, "%s(%d)", w->name, w->unread);
else snprintf(label, sizeof label, "%s", w->name); else snprintf(label, sizeof label, "%s", w->name);
if (w == curwin) wattron(treewin, sel_attr); if (treew > 5) {
mvwprintw(treewin, y++, 3, "%-.*s", treew - 5, label); terminal_move(y, 3);
if (w == curwin) wattroff(treewin, sel_attr); if (w == curwin) fputs(terminal_color ? "\033[1;4m" : "\033[7m", stdout);
terminal_text(label, treew - 5);
fputs("\033[0m", stdout);
}
y++;
} }
} }
wnoutrefresh(treewin);
} }
static int irc_to_curses_color(int c) static int irc_to_ansi_color(int color)
{ {
static const int map[16] = { static const int map[] = {7, 0, 4, 2, 1, 1, 5, 3, 3, 2, 6, 6, 4, 5, 0, 7};
COLOR_WHITE, COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, return color >= 0 && color < 16 ? map[color] : 9;
COLOR_RED, COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW,
COLOR_YELLOW, COLOR_GREEN, COLOR_CYAN, COLOR_CYAN,
COLOR_BLUE, COLOR_MAGENTA, COLOR_BLACK, COLOR_WHITE
};
if (c < 0 || c > 15) return -1;
return map[c];
} }
static int ircfmt_color_pair(int fg, int bg) static void draw_irc_text(int y, int x, const char *s, int maxvis)
{
static short pair_fg[256];
static short pair_bg[256];
static int init;
static int next = 16;
if (!has_colors()) return 0;
if (!init) {
for (int i = 0; i < 256; i++) { pair_fg[i] = -2; pair_bg[i] = -2; }
init = 1;
}
for (int i = 16; i < next; i++)
if (pair_fg[i] == fg && pair_bg[i] == bg) return i;
if (next >= COLOR_PAIRS || next >= 256) return 0;
short pfg = fg < 0 ? (transparent_bg ? -1 : COLOR_WHITE) : (short)irc_to_curses_color(fg);
short pbg = bg < 0 ? (transparent_bg ? -1 : COLOR_BLACK) : (short)irc_to_curses_color(bg);
init_pair((short)next, pfg, pbg);
pair_fg[next] = (short)fg;
pair_bg[next] = (short)bg;
return next++;
}
static void waddnstr_ircfmt(WINDOW *w, int y, int x, const char *s, int maxvis)
{ {
int vis = 0; int vis = 0;
int bold = 0, ul = 0, rev = 0; int bold = 0, ul = 0, rev = 0;
int fg = -1, bg = -1; int fg = -1, bg = -1;
wmove(w, y, x); terminal_move(y, x);
for (size_t i = 0; s[i] && vis < maxvis; i++) { size_t length = strlen(s);
int style_dirty = 1;
for (size_t i = 0; s[i]; i++) {
unsigned char c = (unsigned char)s[i]; unsigned char c = (unsigned char)s[i];
if (c == 0x02) { bold = !bold; continue; } if (c == 0x02) { bold = !bold; style_dirty = 1; continue; }
if (c == 0x1f) { ul = !ul; continue; } if (c == 0x1f) { ul = !ul; style_dirty = 1; continue; }
if (c == 0x16) { rev = !rev; continue; } if (c == 0x16) { rev = !rev; style_dirty = 1; continue; }
if (c == 0x1d) { continue; } /* italic - not widely supported in ncurses */ if (c == 0x1d) { continue; }
if (c == 0x0f) { bold = ul = rev = 0; fg = bg = -1; continue; } if (c == 0x0f) { bold = ul = rev = 0; fg = bg = -1; style_dirty = 1; continue; }
if (c == 0x03) { if (c == 0x03) {
int nfg = -1, nbg = -1, saw = 0; int nfg = -1, nbg = -1, saw = 0;
i++; i++;
@ -2082,26 +2140,39 @@ static void waddnstr_ircfmt(WINDOW *w, int y, int x, const char *s, int maxvis)
i--; i--;
if (saw) { fg = nfg; bg = nbg; } if (saw) { fg = nfg; bg = nbg; }
else { fg = bg = -1; } else { fg = bg = -1; }
style_dirty = 1;
continue; continue;
} }
attr_t attr = A_NORMAL; if (style_dirty) {
if (bold) attr |= A_BOLD; printf("\033[0%s%s%s", bold ? ";1" : "", ul ? ";4" : "", rev ? ";7" : "");
if (ul) attr |= A_UNDERLINE; if (terminal_color) printf(";%d;%d", 30 + irc_to_ansi_color(fg),
if (rev) attr |= A_REVERSE; 40 + irc_to_ansi_color(bg));
int cp = (fg >= 0 || bg >= 0) ? ircfmt_color_pair(fg, bg) : 0; putchar('m');
wattr_set(w, attr, (short)cp, NULL); style_dirty = 0;
waddch(w, c);
vis++;
} }
wattr_set(w, A_NORMAL, 0, NULL); int width;
size_t len = terminal_character(s + i, length - i, &width);
if (width > maxvis - vis) {
if (vis == 0 && maxvis == 1) putchar('?');
break;
}
if (len) {
fwrite(s + i, 1, len, stdout);
i += len - 1;
} else {
putchar('?');
}
vis += width;
}
fputs("\033[0m", stdout);
} }
static void draw_title(void) static void draw_title(void)
{ {
werase(titlewin); fputs(terminal_color ? "\033[1m" : "\033[7m", stdout);
wattron(titlewin, ui_attr); terminal_move(0, 0);
wmove(titlewin, 0, 0); terminal_repeat(' ', cols);
whline(titlewin, ' ', COLS); terminal_move(0, 0);
if (curwin) { if (curwin) {
char head[MAXLINE]; char head[MAXLINE];
if (curwin->type == W_SERVER) if (curwin->type == W_SERVER)
@ -2113,18 +2184,16 @@ static void draw_title(void)
else else
snprintf(head, sizeof head, "[irc9] %s / %s%s", curwin->srv->host, curwin->name, snprintf(head, sizeof head, "[irc9] %s / %s%s", curwin->srv->host, curwin->name,
curwin->scroll > 0 ? " [scroll]" : ""); curwin->scroll > 0 ? " [scroll]" : "");
mvwaddnstr(titlewin, 0, 0, head, COLS); terminal_text(head, cols);
} }
wattroff(titlewin, ui_attr); fputs("\033[0m", stdout);
wnoutrefresh(titlewin);
} }
static void draw_main(void) static void draw_main(void)
{ {
werase(mainwin); int width = cols - treew;
int width = COLS - treew;
if (curwin && mainh > 0) { if (curwin && mainh > 0 && width >= 3) {
int ww = width - 2; int ww = width - 2;
if (ww < 1) ww = 1; if (ww < 1) ww = 1;
int h = mainh; int h = mainh;
@ -2146,22 +2215,23 @@ static void draw_main(void)
for (int r = 0; r < h; r++) { for (int r = 0; r < h; r++) {
int idx = curwin->scroll + (h - 1 - r); int idx = curwin->scroll + (h - 1 - r);
if (idx >= 0 && idx < nr && rows[idx]) if (idx >= 0 && idx < nr && rows[idx])
waddnstr_ircfmt(mainwin, r, 1, rows[idx], ww); draw_irc_text(r + 1, treew + 1, rows[idx], ww);
} }
for (int i = 0; i < nr; i++) free(rows[i]); for (int i = 0; i < nr; i++) free(rows[i]);
free(rows); free(rows);
} }
wnoutrefresh(mainwin);
} }
static void draw_input(void) static void draw_input(void)
{ {
werase(inputwin); if (lines < 4) return;
mvwhline(inputwin, 0, 0, ACS_HLINE, COLS); terminal_move(lines - 3, 0);
fputs("\033(0", stdout);
wattron(inputwin, ui_attr); terminal_repeat('q', cols);
wmove(inputwin, 1, 0); fputs("\033(B", stdout);
whline(inputwin, ' ', COLS); fputs(terminal_color ? "\033[1m" : "\033[7m", stdout);
terminal_move(lines - 2, 0);
terminal_repeat(' ', cols);
if (curwin) { if (curwin) {
char now[16], status[MAXLINE], umode[40]; char now[16], status[MAXLINE], umode[40];
fmttime(now, sizeof now, "%H:%M"); fmttime(now, sizeof now, "%H:%M");
@ -2173,38 +2243,142 @@ static void draw_input(void)
now, curwin->srv->nick, curwin->srv->host, curwin->srv->port, now, curwin->srv->nick, curwin->srv->host, curwin->srv->port,
curwin->srv->tls ? " [TLS]" : "", curwin->srv->tls ? " [TLS]" : "",
umode[0] ? " " : "", umode); umode[0] ? " " : "", umode);
mvwaddnstr(inputwin, 1, 1, status, COLS - 2); if (cols > 2) {
terminal_move(lines - 2, 1);
terminal_text(status, cols - 2);
} }
wattroff(inputwin, ui_attr); }
fputs("\033[0m", stdout);
mvwaddch(inputwin, 2, 0, '>'); terminal_move(lines - 1, 0);
mvwaddch(inputwin, 2, 1, ' '); if (cols > 1) fputs("> ", stdout);
mvwaddnstr(inputwin, 2, 2, input, COLS - 3); terminal_text(input, cols - 3);
int x = 2 + input_pos; int x = 2;
if (x >= COLS) x = COLS - 1; for (int i = 0; i < input_pos; ) {
wmove(inputwin, 2, x); int width;
wnoutrefresh(inputwin); size_t len = terminal_character(input + i, (size_t)(input_pos - i), &width);
x += width;
i += len ? (int)len : 1;
}
if (x >= cols) x = cols - 1;
terminal_move(lines - 1, x);
} }
static void redraw(void) static void redraw(void)
{ {
fputs("\033[?25l\033[0m\033[2J", stdout);
draw_title(); draw_title();
draw_tree(); draw_tree();
draw_main(); draw_main();
draw_input(); draw_input();
doupdate(); fputs("\033[?25h", stdout);
fflush(stdout);
dirty = 0; dirty = 0;
} }
static void check_resize(void) static void check_resize(void)
{ {
if (is_term_resized(LINES, COLS)) { struct winsize size;
resize_term(0, 0); if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == 0 &&
size.ws_row && size.ws_col &&
(lines != size.ws_row || cols != size.ws_col)) {
lines = size.ws_row;
cols = size.ws_col;
layout(); layout();
dirty = 1; dirty = 1;
} }
} }
static void terminal_start(void)
{
const char *term = getenv("TERM");
if (!isatty(STDOUT_FILENO) || tcgetattr(STDIN_FILENO, &saved_terminal) < 0)
fatal("a terminal is required");
struct termios mode = saved_terminal;
mode.c_lflag &= ~(ICANON | ECHO | IEXTEN);
mode.c_iflag &= ~(ICRNL | INLCR | IGNCR | ISTRIP | IXON | IXOFF);
mode.c_cc[VMIN] = 0;
mode.c_cc[VTIME] = 0;
if (tcsetattr(STDIN_FILENO, TCSANOW, &mode) < 0)
fatal("cannot configure terminal");
terminal_active = 1;
terminal_color = term && strcmp(term, "dumb") != 0 && strncmp(term, "vt", 2) != 0;
fputs("\033[?1049h\033[?1h\033=", stdout);
check_resize();
layout();
}
/* Decode both normal and application cursor keys, including split reads. */
static void terminal_read_keys(void)
{
static int state, number, parameters;
static struct timespec last_escape;
struct timespec now = {0};
clock_gettime(CLOCK_MONOTONIC, &now);
if (state && (now.tv_sec - last_escape.tv_sec) * 1000LL +
(now.tv_nsec - last_escape.tv_nsec) / 1000000 >= 1000)
state = 0;
unsigned char bytes[256];
ssize_t count = read(STDIN_FILENO, bytes, sizeof bytes);
for (ssize_t i = 0; i < count && !quit_flag; i++) {
int c = bytes[i];
if (c == 27) {
state = 1;
last_escape = now;
continue;
}
if (state == 1) {
if (c == '[' || c == 'O') {
state = c;
number = parameters = 0;
continue;
}
state = 0;
} else if (state) {
if (c >= '0' && c <= '9') {
if (!parameters && number < 100) number = number * 10 + c - '0';
continue;
}
if (c >= 0x20 && c <= 0x3f) {
parameters = 1;
continue;
}
int sequence = state;
state = 0;
if (sequence == 'O' && c == 'M') {
handle_key('\n');
continue;
}
if (sequence == '[' && c == '[') {
state = -1; /* Linux console function-key prefix */
continue;
}
if (sequence == -1) continue;
switch (c) {
case 'A': c = KEY_UP; break;
case 'B': c = KEY_DOWN; break;
case 'C': c = KEY_RIGHT; break;
case 'D': c = KEY_LEFT; break;
case 'H': c = KEY_HOME; break;
case 'F': c = KEY_END; break;
case '~':
switch (number) {
case 1: case 7: c = KEY_HOME; break;
case 4: case 8: c = KEY_END; break;
case 5: c = KEY_PPAGE; break;
case 6: c = KEY_NPAGE; break;
default: continue;
}
break;
default: continue;
}
}
handle_key(c);
}
}
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* main */ /* main */
@ -2285,37 +2459,12 @@ int main(int argc, char **argv)
int fl = fcntl(connect_pipe[0], F_GETFL, 0); int fl = fcntl(connect_pipe[0], F_GETFL, 0);
fcntl(connect_pipe[0], F_SETFL, fl | O_NONBLOCK); fcntl(connect_pipe[0], F_SETFL, fl | O_NONBLOCK);
initscr(); terminal_start();
cbreak();
noecho();
keypad(stdscr, TRUE);
nodelay(stdscr, TRUE);
curs_set(1);
if (has_colors()) {
start_color();
if (use_default_colors() == OK) {
/* -1 = terminal default: foot's transparent background stays visible. */
transparent_bg = 1;
init_pair(1, -1, -1);
sel_attr = A_BOLD | A_UNDERLINE | COLOR_PAIR(1);
ui_attr = A_BOLD | COLOR_PAIR(1);
} else {
/* No default-colour support: keep the old opaque highlight. */
init_pair(1, COLOR_BLACK, COLOR_WHITE);
sel_attr = COLOR_PAIR(1);
}
}
layout();
curwin = servers->windows; curwin = servers->windows;
redraw(); redraw();
while (!quit_flag) { while (!quit_flag) {
int c; terminal_read_keys();
while ((c = getch()) != ERR) {
if (c == KEY_RESIZE) { resize_term(0, 0); layout(); dirty = 1; continue; }
handle_key(c);
if (quit_flag) break;
}
if (quit_flag) break; if (quit_flag) break;
handle_connect_results(); handle_connect_results();
@ -2337,8 +2486,7 @@ int main(int argc, char **argv)
owner[nf] = s; owner[nf] = s;
nf++; nf++;
} }
if (nf) poll(fds, (nfds_t)nf, 50); poll(fds, (nfds_t)nf, 50);
else napms(50);
for (int i = 0; i < nf; i++) for (int i = 0; i < nf; i++)
if (fds[i].revents & (POLLIN | POLLHUP | POLLERR)) if (fds[i].revents & (POLLIN | POLLHUP | POLLERR))
@ -2347,7 +2495,7 @@ int main(int argc, char **argv)
if (dirty) redraw(); if (dirty) redraw();
} }
endwin(); terminal_finish();
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
/* A worker may still be using the SSL_CTX; leave it to process exit. */ /* A worker may still be using the SSL_CTX; leave it to process exit. */