Remove term.c in favor of more curses APIs

master
C. McEnroe 2020-02-02 19:34:35 -05:00
parent 8ec17d4f8c
commit 5c328c7a88
4 changed files with 26 additions and 84 deletions

View File

@ -8,7 +8,6 @@ LDLIBS = -lcurses -lcrypto -ltls
OBJS += chat.o
OBJS += handle.o
OBJS += irc.o
OBJS += term.o
OBJS += ui.o
dev: tags all

15
chat.h
View File

@ -118,21 +118,6 @@ void uiFormat(
size_t id, enum Heat heat, const struct tm *time, const char *format, ...
) __attribute__((format(printf, 4, 5)));
enum TermMode {
TermFocus,
TermPaste,
};
enum TermEvent {
TermNone,
TermFocusIn,
TermFocusOut,
TermPasteStart,
TermPasteEnd,
};
void termNoFlow(void);
void termMode(enum TermMode mode, bool set);
enum TermEvent termEvent(char ch);
static inline enum Color hash(const char *str) {
if (*str == '~') str++;
uint32_t hash = 0;

66
term.c
View File

@ -1,66 +0,0 @@
/* Copyright (C) 2018, 2020 C. McEnroe <june@causal.agency>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include "chat.h"
void termNoFlow(void) {
struct termios attr;
int error = tcgetattr(STDIN_FILENO, &attr);
if (error) return;
attr.c_iflag &= ~IXON;
attr.c_cc[VDISCARD] = _POSIX_VDISABLE;
tcsetattr(STDIN_FILENO, TCSANOW, &attr);
}
static void privateMode(const char *mode, bool set) {
printf("\33[?%s%c", mode, (set ? 'h' : 'l'));
fflush(stdout);
}
void termMode(enum TermMode mode, bool set) {
switch (mode) {
break; case TermFocus: privateMode("1004", set);
break; case TermPaste: privateMode("2004", set);
}
}
enum { Esc = '\33' };
enum TermEvent termEvent(char ch) {
static int st;
#define T(st, ch) ((st) << 8 | (ch))
switch (T(st, ch)) {
break; case T(0, Esc): st = 1;
break; case T(1, '['): st = 2;
break; case T(2, 'I'): st = 0; return TermFocusIn;
break; case T(2, 'O'): st = 0; return TermFocusOut;
break; case T(2, '2'): st = 3;
break; case T(3, '0'): st = 4;
break; case T(4, '0'): st = 5;
break; case T(5, '~'): st = 0; return TermPasteStart;
break; case T(4, '1'): st = 6;
break; case T(6, '~'): st = 0; return TermPasteEnd;
break; default: st = 0;
}
return 0;
#undef T
}

28
ui.c
View File

@ -25,7 +25,9 @@
#include <string.h>
#include <sysexits.h>
#include <term.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#include <wchar.h>
#include <wctype.h>
@ -125,6 +127,23 @@ static struct Window *windowFor(size_t id) {
return window;
}
enum {
KeyFocusIn = KEY_MAX + 1,
KeyFocusOut,
KeyPasteOn,
KeyPasteOff,
};
static void disableFlowControl(void) {
struct termios term;
int error = tcgetattr(STDOUT_FILENO, &term);
if (error) err(EX_OSERR, "tcgetattr");
term.c_iflag &= ~IXON;
term.c_cc[VDISCARD] = _POSIX_VDISABLE;
error = tcsetattr(STDOUT_FILENO, TCSADRAIN, &term);
if (error) err(EX_OSERR, "tcsetattr");
}
static void errExit(int eval) {
(void)eval;
reset_shell_mode();
@ -134,15 +153,20 @@ void uiInit(void) {
initscr();
cbreak();
noecho();
termNoFlow();
disableFlowControl();
def_prog_mode();
err_set_exit(errExit);
colorInit();
if (!to_status_line && !strncmp(termname(), "xterm", 5)) {
to_status_line = "\33]2;";
from_status_line = "\7";
}
define_key("\33[I", KeyFocusIn);
define_key("\33[O", KeyFocusOut);
define_key("\33[200~", KeyPasteOn);
define_key("\33[201~", KeyPasteOff);
colorInit();
status = newwin(1, COLS, 0, 0);
input = newpad(1, InputCols);
keypad(input, true);