about summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-08-05 19:32:11 -0400
committerJune McEnroe <june@causal.agency>2018-08-05 19:32:11 -0400
commit1fb2c98b392b44f8a9ad3512608c92e1dad9c7af (patch)
tree116e245075517c79707632d3a59cd7b113a2999d
parentAdd chat to index.html (diff)
downloadtorus-1fb2c98b392b44f8a9ad3512608c92e1dad9c7af.tar.gz
torus-1fb2c98b392b44f8a9ad3512608c92e1dad9c7af.zip
Fix color pairs once and for all v1
8/16 color setup adapted from chat/ui.c. termcap patch hack no longer
required. Black on black and bright black now work. Nothing will appear
bold anymore on 256-color terminals.

The keys for black have definitely always been in the help. You just
didn't notice.
Diffstat (limited to '')
-rw-r--r--.gitignore2
-rw-r--r--Makefile12
-rw-r--r--client.c65
-rw-r--r--help.c5
-rw-r--r--merge.c32
-rw-r--r--termcap.diff20
6 files changed, 70 insertions, 66 deletions
diff --git a/.gitignore b/.gitignore
index 6a75e89..2f400d8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,7 +6,5 @@ meta
 root
 server
 tags
-termcap
-termcap.db
 torus.dat
 torus.sock
diff --git a/Makefile b/Makefile
index daef391..8df5425 100644
--- a/Makefile
+++ b/Makefile
@@ -11,13 +11,7 @@ $(BINS): torus.h
 .c:
 	$(CC) $(CFLAGS) $(LDFLAGS) $< $(LDLIBS) -o $@
 
-termcap: termcap.diff
-	patch -p0 -o termcap < termcap.diff
-
-termcap.db: termcap
-	cap_mkdb termcap
-
-chroot.tar: server client help termcap.db
+chroot.tar: server client help
 	mkdir -p root
 	install -d -o root -g wheel \
 	    root/bin \
@@ -36,7 +30,7 @@ chroot.tar: server client help termcap.db
 	    /lib/libncurses.so.8 \
 	    /lib/libncursesw.so.8 \
 	    root/lib
-	install -o root -g wheel -m 444 termcap.db root/usr/share/misc
+	install -o root -g wheel -m 444 /usr/share/misc/termcap.db root/usr/share/misc
 	install -o root -g wheel -m 555 /bin/sh root/bin
 	install -o root -g wheel -m 555 server client help root/bin
 	tar -c -f chroot.tar -C root bin home lib libexec usr
@@ -45,4 +39,4 @@ tags: *.h *.c
 	ctags -w *.h *.c
 
 clean:
-	rm -f tags $(BINS) termcap termcap.db chroot.tar
+	rm -f tags $(BINS) chroot.tar
diff --git a/client.c b/client.c
index d46649b..7af6950 100644
--- a/client.c
+++ b/client.c
@@ -71,18 +71,31 @@ static void clientMap(void) {
 	clientMessage(msg);
 }
 
-static chtype colorAttrs(uint8_t color) {
-	uint8_t bright = color & COLOR_BRIGHT;
-	uint8_t fg = color & 0x07;
-	uint8_t bg = color & 0x70;
-	return COLOR_PAIR(bg >> 1 | fg) | (bright ? A_BOLD : 0);
+static void colorPairs(void) {
+	assume_default_colors(0, 0);
+	if (COLORS >= 16) {
+		for (short pair = 1; pair < 0x80; ++pair) {
+			init_pair(pair, pair & 0x0F, (pair & 0xF0) >> 4);
+		}
+	} else {
+		for (short pair = 1; pair < 0100; ++pair) {
+			init_pair(pair, pair & 007, (pair & 070) >> 3);
+		}
+	}
+}
+
+static chtype colorAttr(uint8_t color) {
+	if (COLORS >= 16) return COLOR_PAIR(color);
+	chtype bold = (color & COLOR_BRIGHT) ? A_BOLD : A_NORMAL;
+	short pair = (color & 0x70) >> 1 | (color & 0x07);
+	return bold | COLOR_PAIR(pair);
 }
 
-static uint8_t attrsColor(chtype attrs) {
-	chtype bright = attrs & A_BOLD;
-	short fg = PAIR_NUMBER(attrs) & 007;
-	short bg = PAIR_NUMBER(attrs) & 070;
-	return bg << 1 | fg | (bright ? COLOR_BRIGHT : 0);
+static uint8_t attrColor(chtype attr) {
+	if (COLORS >= 16) return PAIR_NUMBER(attr);
+	uint8_t bright = (attr & A_BOLD) ? COLOR_BRIGHT : 0;
+	short pair = PAIR_NUMBER(attr);
+	return (pair & 070) << 1 | bright | (pair & 007);
 }
 
 static struct {
@@ -129,13 +142,13 @@ static void insertMode(int8_t dx, int8_t dy) {
 }
 
 static void swapCell(int8_t dx, int8_t dy) {
-	uint8_t aColor = attrsColor(inch());
+	uint8_t aColor = attrColor(inch());
 	char aCell = inch() & A_CHARTEXT;
 
 	int sy, sx;
 	getyx(stdscr, sy, sx);
 	move(sy + dy, sx + dx);
-	uint8_t bColor = attrsColor(inch());
+	uint8_t bColor = attrColor(inch());
 	char bCell = inch() & A_CHARTEXT;
 	move(sy, sx);
 
@@ -164,7 +177,7 @@ static void inputNormal(int c) {
 		break; case 'r': input.mode = MODE_REPLACE;
 		break; case 'p': input.mode = MODE_PUT;
 		break; case 'R': input.mode = MODE_DRAW; input.draw = 0;
-		break; case 'x': clientPut(attrsColor(inch()), ' ');
+		break; case 'x': clientPut(attrColor(inch()), ' ');
 
 		break; case '~': {
 			clientPut(input.color, inch() & A_CHARTEXT);
@@ -192,7 +205,7 @@ static void inputNormal(int c) {
 		break; case 'B': swapCell(-1,  1);
 		break; case 'N': swapCell( 1,  1);
 
-		break; case '`': input.color = attrsColor(inch());
+		break; case '`': input.color = attrColor(inch());
 
 		break; case '0': colorFg(COLOR_BLACK);
 		break; case '1': colorFg(COLOR_RED);
@@ -260,7 +273,7 @@ static void inputInsert(int c) {
 }
 
 static void inputReplace(int c) {
-	if (isprint(c)) clientPut(attrsColor(inch()), c);
+	if (isprint(c)) clientPut(attrColor(inch()), c);
 	input.mode = MODE_NORMAL;
 }
 
@@ -294,7 +307,7 @@ static void readInput(void) {
 }
 
 static void serverPut(uint8_t x, uint8_t y, uint8_t color, char cell) {
-	mvaddch(y, x, colorAttrs(color) | cell);
+	mvaddch(y, x, colorAttr(color) | cell);
 }
 
 static void serverTile(void) {
@@ -370,15 +383,15 @@ static void serverMap(void) {
 			time *= ARRAY_LEN(MAP_COLORS) - 1;
 
 			char cell = MAP_CELLS[(int)round(count)];
-			chtype attrs = colorAttrs(MAP_COLORS[(int)round(time)]);
+			chtype attr = colorAttr(MAP_COLORS[(int)round(time)]);
 			if (y == MAP_ROWS / 2 && x == MAP_COLS / 2) {
-				attrs |= A_REVERSE;
+				attr |= A_REVERSE;
 			}
 
 			wmove(mapWindow, y, 3 * x);
-			waddch(mapWindow, attrs | cell);
-			waddch(mapWindow, attrs | cell);
-			waddch(mapWindow, attrs | cell);
+			waddch(mapWindow, attr | cell);
+			waddch(mapWindow, attr | cell);
+			waddch(mapWindow, attr | cell);
 		}
 	}
 
@@ -456,11 +469,7 @@ static void curse(void) {
 		);
 		exit(EX_CONFIG);
 	}
-	for (int bg = COLOR_BLACK; bg < COLOR_BRIGHT; ++bg) {
-		for (int fg = COLOR_BLACK; fg < COLOR_BRIGHT; ++fg) {
-			init_pair(PAIR_NUMBER(colorAttrs(bg << 4 | fg)), fg, bg);
-		}
-	}
+	colorPairs();
 
 	if (LINES < CELL_ROWS || COLS < CELL_COLS) {
 		endwin();
@@ -468,6 +477,8 @@ static void curse(void) {
 		fprintf(stderr, "It needs to be at least 80x25 characters.\n");
 		exit(EX_CONFIG);
 	}
+
+	attrset(colorAttr(COLOR_WHITE));
 	if (LINES > CELL_ROWS) {
 		mvhline(CELL_ROWS, 0, 0, CELL_COLS);
 	}
@@ -477,6 +488,7 @@ static void curse(void) {
 	if (LINES > CELL_ROWS && COLS > CELL_COLS) {
 		mvaddch(CELL_ROWS, CELL_COLS, ACS_LRCORNER);
 	}
+	attrset(A_NORMAL);
 
 	mapFrame = newwin(
 		MAP_ROWS + 2,
@@ -490,6 +502,7 @@ static void curse(void) {
 		CELL_INIT_Y - MAP_ROWS / 2,
 		CELL_INIT_X - 3 * MAP_COLS / 2
 	);
+	wattrset(mapFrame, colorAttr(COLOR_WHITE));
 	box(mapFrame, 0, 0);
 }
 
diff --git a/help.c b/help.c
index f000638..dd13c4f 100644
--- a/help.c
+++ b/help.c
@@ -49,6 +49,7 @@ static void clientPut(uint8_t color, char cell) {
 static const useconds_t DELAY = 50000;
 
 enum {
+	K = COLOR_BLACK,
 	R = COLOR_RED,
 	G = COLOR_GREEN,
 	Y = COLOR_YELLOW,
@@ -164,9 +165,11 @@ int main() {
 		clientPut(Y, '3'); k();
 		clientPut(G, '2'); k();
 		clientPut(R, '1'); k();
+		clientPut(K, '0');
 
-		l(); n();
+		l(); l();
 
+		clientPut(K << 4, ')'); j();
 		clientPut(R << 4, '!'); j();
 		clientPut(G << 4, '@'); j();
 		clientPut(Y << 4, '#'); j();
diff --git a/merge.c b/merge.c
index 7ae7cac..70c5d4e 100644
--- a/merge.c
+++ b/merge.c
@@ -22,15 +22,33 @@
 
 #include "torus.h"
 
+static void colorPairs(void) {
+	assume_default_colors(0, 0);
+	if (COLORS >= 16) {
+		for (short pair = 1; pair < 0x80; ++pair) {
+			init_pair(pair, pair & 0x0F, (pair & 0xF0) >> 4);
+		}
+	} else {
+		for (short pair = 1; pair < 0100; ++pair) {
+			init_pair(pair, pair & 007, (pair & 070) >> 3);
+		}
+	}
+}
+
+static chtype colorAttr(uint8_t color) {
+	if (COLORS >= 16) return COLOR_PAIR(color);
+	chtype bold = (color & COLOR_BRIGHT) ? A_BOLD : A_NORMAL;
+	short pair = (color & 0x70) >> 1 | (color & 0x07);
+	return bold | COLOR_PAIR(pair);
+}
+
 static void drawTile(int offsetY, const struct Tile *tile) {
 	for (uint8_t y = 0; y < CELL_ROWS; ++y) {
 		for (uint8_t x = 0; x < CELL_COLS; ++x) {
 			uint8_t color = tile->colors[y][x];
 			char cell = tile->cells[y][x];
 
-			int attrs = COLOR_PAIR(color & ~COLOR_BRIGHT);
-			if (color & COLOR_BRIGHT) attrs |= A_BOLD;
-			mvaddch(offsetY + y, x, attrs | cell);
+			mvaddch(offsetY + y, x, colorAttr(color) | cell);
 		}
 	}
 }
@@ -54,17 +72,15 @@ int main(int argc, char *argv[]) {
 	set_escdelay(100);
 
 	start_color();
-	for (int bg = COLOR_BLACK; bg < COLOR_BRIGHT; ++bg) {
-		for (int fg = COLOR_BLACK; fg < COLOR_BRIGHT; ++fg) {
-			init_pair(bg << 4 | fg, fg, bg);
-		}
-	}
+	colorPairs();
 
+	attrset(colorAttr(COLOR_WHITE));
 	mvhline(CELL_ROWS, 0, 0, CELL_COLS);
 	mvhline(CELL_ROWS * 2 + 1, 0, 0, CELL_COLS);
 	mvvline(0, CELL_COLS, 0, CELL_ROWS * 2 + 1);
 	mvaddch(CELL_ROWS, CELL_COLS, ACS_RTEE);
 	mvaddch(CELL_ROWS * 2 + 1, CELL_COLS, ACS_LRCORNER);
+	attrset(A_NORMAL);
 
 	struct Tile tileA, tileB;
 	for (;;) {
diff --git a/termcap.diff b/termcap.diff
deleted file mode 100644
index 109fb95..0000000
--- a/termcap.diff
+++ /dev/null
@@ -1,20 +0,0 @@
---- /usr/share/misc/termcap	2017-07-29 16:46:57.370274000 +0000
-+++ termcap	2017-08-09 03:09:03.753635000 +0000
-@@ -2883,7 +2883,7 @@
- 
- xterm-256color|xterm alias 3:\
- 	:Co#256:pa#32767:\
--	:AB=\E[48;5;%dm:AF=\E[38;5;%dm:tc=xterm-new:
-+	:tc=xterm-new:
- 
- xterm-nrc|xterm alias 4:\
- 	:tc=xterm:
-@@ -4636,7 +4636,7 @@
- 
- rxvt-256color|rxvt terminal emulator with 256 colors:\
- 	:Co#256:pa#32767:\
--	:AB=\E[48;5;%dm:AF=\E[38;5;%dm:op=\E[39;49m:tc=rxvt-unicode:
-+	:AF=\E[3%dm:AB=\E[4%dm:op=\E[39;49m:tc=rxvt-unicode:
- 
- rxvt-unicode-256color|rxvt-unicode terminal with 256 colors (X Window System):\
- 	:tc=rxvt-256color: