summarylogtreecommitdiffstats
path: root/term.c
blob: 8a23eac55ab2ce1f5c42373eecef35a64baa0f6e (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* $Id$ */

/*
 * hed - Hexadecimal editor
 * Copyright (C) 2004  Petr Baudis <pasky@ucw.cz>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 * 
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * `There's only stars, as I thought,' he said. Then he gave a low gasp, for
 * the stars went out. As if a dark veil had been withdrawn, the Mirror grew
 * grey, and then clear. There was sun shining, and the branches of trees were
 * waving and tossing in the wind.
 */

/* Feature macros needed for:
 *  - stpcpy
 *  - vasprintf
 */
#define _GNU_SOURCE

#include <config.h>

#include <curses.h>
#include <stdlib.h>

#include <hed.h>

#include <term/term.h>


static WINDOW *scr;
int color2attr[64];

#define DEFCOLOR(PN, FG, BG, ATTRS)				\
	[ ( COLOR_ ## PN ) - 1 ] = {				\
		.fg = ( COLOR_ ## FG ),				\
		.bg = ( COLOR_ ## BG ),				\
		.attrs = COLOR_PAIR( COLOR_ ## PN ) | (ATTRS ) }

static struct {
	short fg, bg;
	int attrs;
} colordefs[] = {
	//	pair number	fg	bg	attrs
	DEFCOLOR(NEUTRAL,	WHITE,	BLACK,	0),
	DEFCOLOR(OFFSET,	YELLOW,	BLACK,	A_BOLD),
	DEFCOLOR(STATUS,	WHITE,	BLACK,	0),
	DEFCOLOR(MODE,		WHITE,	BLACK,	A_BOLD),
	DEFCOLOR(MARK,		YELLOW,	BLACK,	A_BOLD),
	DEFCOLOR(ERROR,		RED,	BLACK,	A_BOLD),
	DEFCOLOR(DIRTY,		RED,	BLACK,	A_BOLD),

	DEFCOLOR(HEX,		WHITE,	BLACK,	0),
	DEFCOLOR(UNHEX,		WHITE,	BLUE,	0),
	DEFCOLOR(ASC,		GREEN,	BLACK,	A_BOLD),
	DEFCOLOR(UNASC,		GREEN,	BLUE,	A_BOLD),
	DEFCOLOR(CURHEX,	WHITE,	BLACK,	A_BOLD),
	DEFCOLOR(CURUNHEX,	WHITE,	BLUE,	A_BOLD),
	DEFCOLOR(CURASC,	YELLOW,	BLACK,	A_BOLD),
	DEFCOLOR(CURUNASC,	YELLOW,	BLUE,	A_BOLD),
};

#undef DEFCOLOR

#ifdef CONFIG_CUSTOM_COLORS

#include <string.h>
#include <ctype.h>
#include <config/config.h>

struct valdesc {
	const char *const name;
	const size_t len;
	const int val;
};

static int
parseval(char **pstr, const struct valdesc *vals, int nvals, bool mult)
{
	char *p, *q;
	size_t len;
	int i, ret = 0;

	while (**pstr) {
		for (p = *pstr; isspace(*p); ++p);
		for (q = p; *q && !isspace(*q); ++q);
		len = q - p;
		*pstr = q;

		for (i = 0; i < nvals; ++i) {
			if (vals[i].len != len)
				continue;
			if (strncasecmp(p, vals[i].name, len))
				continue;
			ret |= vals[i].val;
		}
		if (!mult)
			break;
	}
	return ret;
}

static inline int
parsecolor(char **pstr)
{
#define COLDESC(COL) {				\
		.name = #COL,			\
		.len = (sizeof(#COL)) - 1,	\
		.val = COLOR_ ## COL }
	static const struct valdesc colors[] = {
		COLDESC(BLACK),
		COLDESC(RED),
		COLDESC(GREEN),
		COLDESC(YELLOW),
		COLDESC(BLUE),
		COLDESC(MAGENTA),
		COLDESC(CYAN),
		COLDESC(WHITE),
	};
#undef COLDESC

	return parseval(pstr,
			colors, sizeof(colors) / sizeof(colors[0]),
			false);
}

static inline int
parseattrs(char **pstr)
{
#define ATTRDESC(ATTR) {			\
		.name = #ATTR,			\
		.len = (sizeof(#ATTR)) - 1,	\
		.val = A_ ## ATTR }
	static const struct valdesc attrs[] = {
		ATTRDESC(NORMAL),
		ATTRDESC(STANDOUT),
		ATTRDESC(UNDERLINE),
		ATTRDESC(REVERSE),
		ATTRDESC(BLINK),
		ATTRDESC(DIM),
		ATTRDESC(BOLD),
		ATTRDESC(PROTECT),
		ATTRDESC(INVIS),
		ATTRDESC(ALTCHARSET),
	};
#undef ATTRDESC

	return parseval(pstr,
			attrs, sizeof(attrs) / sizeof(attrs[0]),
			true);
}

static void
config_colors(void)
{
	static const char * const names[] = {
		"neutral", "offset", "status", "mode",
		"mark", "error", "dirty",
		"hex", "unhex", "asc", "unasc",
		"curhex", "curunhex", "curasc", "curunasc" };
	int i;

	for (i = 0; i < sizeof(names) / sizeof(names[0]); ++i) {
		char *spec, name[16];

		strcpy(stpcpy(name, "color_"), names[i]);
		if (! (spec = get_opt_str(name, NULL)) )
			continue;

		colordefs[i].fg = parsecolor(&spec);
		colordefs[i].bg = parsecolor(&spec);
		colordefs[i].attrs = parseattrs(&spec) | COLOR_PAIR(i + 1);
	}
}

#else

#define config_colors()	do { } while(0)

#endif	/* CONFIG_CUSTOM_COLORS */

/* Initialize the terminal. */
void
term_init(void)
{
	int i;

	scr = initscr();
	cbreak(); noecho(); nonl();
	intrflush(scr, FALSE);
	keypad(scr, TRUE);

	config_colors();
	start_color();
	for (i = 0; i < sizeof(colordefs) / sizeof(colordefs[0]); ++i) {
		init_pair(i + 1, colordefs[i].fg, colordefs[i].bg);
		color2attr[i+1] = colordefs[i].attrs;
	}

	refresh();
}

/* Deinitialize the terminal. */
void
term_done(void)
{
	endwin();
}

/* Redraw the terminal. */
void term_redraw(void)
{
	refresh();
}


/* Move the cursor around to the given position. */
void
term_goto_cursor(int x, int y)
{
	move(y, x);
}

/* Get window dimensions. */
int
term_get_max_x(void)
{
	int x, y;
	getmaxyx(stdscr, y, x);
	return x;
}

int
term_get_max_y(void)
{
	int x, y;
	getmaxyx(stdscr, y, x);
	return y;
}

/* Clear the given line. Can move cursor around. */
void
term_clear_line(int y)
{
	move(y, 0);
	clrtoeol();
}

/* Print formatted output to the terminal. */
int
term_printf(int x, int y, term_color color, const char *fmt, ...)
{
	va_list va;
	char *str;
	int len;

	va_start(va, fmt);
	len = vasprintf(&str, fmt, va);
	va_end(va);
	term_print_string(x, y, color, str);
	free(str);
	return len;
}

/* Get a character from the terminal. */
int
term_get_char(void)
{
	return getch();
}