summary refs log tree commit diff
path: root/view.c
diff options
context:
space:
mode:
Diffstat (limited to 'view.c')
-rw-r--r--view.c122
1 files changed, 121 insertions, 1 deletions
diff --git a/view.c b/view.c
index 77a236d..a5861c4 100644
--- a/view.c
+++ b/view.c
@@ -14,11 +14,131 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#define _XOPEN_SOURCE_EXTENDED
+
+#include <curses.h>
 #include <err.h>
+#include <locale.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/un.h>
 #include <sysexits.h>
+#include <unistd.h>
+
+#include "stream.h"
+
+#ifndef A_ITALIC
+#define A_ITALIC A_UNDERLINE
+#endif
+
+static short colorPair(int bg, int fg) {
+	if (bg >= COLORS || fg >= COLORS) return 0;
+	if (bg < 8 && fg < 8) {
+		return 9 * (bg + 1) + (fg + 1);
+	}
+
+	enum {
+		Base = 9 * 9,
+		Ring = 256 - Base,
+	};
+	static struct {
+		int bg, fg;
+	} pairs[Ring];
+	static size_t len = 0;
+
+	short i;
+	for (i = 0; i < Ring; ++i) {
+		if (pairs[i].bg == bg && pairs[i].fg == fg) return Base + i;
+		if (!pairs[i].bg && !pairs[i].fg) break;
+	}
+
+	i = len++ % Ring;
+	pairs[i].bg = bg;
+	pairs[i].fg = fg;
+	init_pair(Base + i, fg, bg);
+	touchwin(stdscr);
+	return Base + i;
+}
+
+static void curse(void) {
+	initscr();
+	cbreak();
+	noecho();
+	start_color();
+	use_default_colors();
+	for (int bg = -1; bg < 8; ++bg) {
+		for (int fg = -1; fg < 8; ++fg) {
+			init_pair(colorPair(bg, fg), fg, bg);
+		}
+	}
+}
+
+static attr_t styleAttr(struct Style style) {
+	attr_t attr = A_NORMAL;
+	if (style.attr & Bold) attr |= A_BOLD;
+	if (style.attr & Dim) attr |= A_DIM;
+	if (style.attr & Italic) attr |= A_ITALIC;
+	if (style.attr & Underline) attr |= A_UNDERLINE;
+	if (style.attr & Blink) attr |= A_BLINK;
+	if (style.attr & Reverse) attr |= A_REVERSE;
+	return attr;
+}
+
+static void render(WINDOW *win, struct Display term) {
+	for (uint y = 0; y < term.rows; ++y) {
+		for (uint x = 0; x < term.cols; ++x) {
+			struct Cell cell = term.cells[term.cols * y + x];
+			if (!cell.ch) continue;
+			wattr_set(
+				win,
+				styleAttr(cell.style),
+				colorPair(cell.style.bg, cell.style.fg),
+				NULL
+			);
+			mvwaddnwstr(win, y, x, &cell.ch, 1);
+		}
+	}
+	curs_set(term.cursor);
+	leaveok(win, !term.cursor);
+	wmove(win, term.y, term.x);
+}
 
 int main(void) {
-	// TODO: Connect to socket, emulate terminal.
+	int error;
+	setlocale(LC_CTYPE, "");
+
+	// TODO: Read info from file.
+	const char *path = "example.sock";
+	termInit(24, 80);
+
+	int client = socket(PF_LOCAL, SOCK_STREAM, 0);
+	if (client < 0) err(EX_OSERR, "socket");
+
+	struct sockaddr_un addr = { .sun_family = AF_LOCAL };
+	strncpy(addr.sun_path, path, sizeof(addr.sun_path));
+	error = connect(client, (struct sockaddr *)&addr, SUN_LEN(&addr));
+	if (error) err(EX_NOINPUT, "%s", path);
+
+	curse();
+	WINDOW *win = newwin(24, 80, 0, 0);
+
+	for (;;) {
+		char buf[4096];
+		ssize_t len = read(client, buf, sizeof(buf));
+		if (len < 0) err(EX_IOERR, "read");
+
+		ssize_t pos = 0;
+		while (pos < len) {
+			wchar_t ch;
+			int n = mbtowc(&ch, &buf[pos], len - pos);
+			if (n <= 0) break;
+			termUpdate(ch);
+			pos += n;
+		}
+
+		render(win, termDisplay());
+		wrefresh(win);
+	}
 }