summary refs log tree commit diff
path: root/term.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-02-01 02:55:07 -0500
committerJune McEnroe <june@causal.agency>2020-02-01 02:55:07 -0500
commite289ff6b18e643eea4c72e04f7c0dc6ff768a335 (patch)
treed8f515192ac7756ffb908b8171787d7f3924637b /term.c
parentAdd IDs and names (diff)
downloadcatgirl-e289ff6b18e643eea4c72e04f7c0dc6ff768a335.tar.gz
catgirl-e289ff6b18e643eea4c72e04f7c0dc6ff768a335.zip
Add term stuff
Copied almost verbatim from existing catgirl... I think I did a better
job on that state machine this time tbh.
Diffstat (limited to 'term.c')
-rw-r--r--term.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/term.c b/term.c
new file mode 100644
index 0000000..ade5392
--- /dev/null
+++ b/term.c
@@ -0,0 +1,79 @@
+/* Copyright (C) 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"
+
+static bool xterm;
+
+void termInit(void) {
+	const char *term = getenv("TERM");
+	xterm = (term && !strncmp(term, "xterm", 5));
+}
+
+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);
+}
+
+void termTitle(const char *title) {
+	if (!xterm) return;
+	printf("\33]0;%s\33\\", title);
+	fflush(stdout);
+}
+
+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
+}