summary refs log tree commit diff
path: root/view.c
blob: 7a401235686da6d5ff489c452f3a5902fbd19bf3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Copyright (C) 2019  C. McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * 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 "term.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, const struct Term *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->mode & Cursor));
	leaveok(win, !(term->mode & Cursor));
	wmove(win, term->y, term->x);
}

int main(void) {
	int error;
	setlocale(LC_CTYPE, "");

	// TODO: Read info from file.
	const char *path = "example.sock";
	struct Term *term = termAlloc(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(term, ch);
			pos += n;
		}

		render(win, term);
		wrefresh(win);
	}
}