summaryrefslogtreecommitdiffstats
path: root/tty-acs.c
blob: 3dab31b6f1f6dd2e154e86e2bcf9203c5e8fe991 (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
/* $OpenBSD$ */

/*
 * Copyright (c) 2010 Nicholas Marriott <nicholas.marriott@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>

#include <stdlib.h>
#include <string.h>

#include "tmux.h"

/* Table mapping ACS entries to UTF-8. */
struct tty_acs_entry {
	u_char		 key;
	const char	*string;
};
static const struct tty_acs_entry tty_acs_table[] = {
	{ '+', "\342\206\222" },	/* arrow pointing right */
	{ ',', "\342\206\220" },	/* arrow pointing left */
	{ '-', "\342\206\221" },	/* arrow pointing up */
	{ '.', "\342\206\223" },	/* arrow pointing down */
	{ '0', "\342\226\256" },	/* solid square block */
	{ '`', "\342\227\206" },	/* diamond */
	{ 'a', "\342\226\222" },	/* checker board (stipple) */
	{ 'b', "\342\220\211" },
	{ 'c', "\342\220\214" },
	{ 'd', "\342\220\215" },
	{ 'e', "\342\220\212" },
	{ 'f', "\302\260" },		/* degree symbol */
	{ 'g', "\302\261" },		/* plus/minus */
	{ 'h', "\342\220\244" },
	{ 'i', "\342\220\213" },
	{ 'j', "\342\224\230" },	/* lower right corner */
	{ 'k', "\342\224\220" },	/* upper right corner */
	{ 'l', "\342\224\214" },	/* upper left corner */
	{ 'm', "\342\224\224" },	/* lower left corner */
	{ 'n', "\342\224\274" },	/* large plus or crossover */
	{ 'o', "\342\216\272" },	/* scan line 1 */
	{ 'p', "\342\216\273" },	/* scan line 3 */
	{ 'q', "\342\224\200" },	/* horizontal line */
	{ 'r', "\342\216\274" },	/* scan line 7 */
	{ 's', "\342\216\275" },	/* scan line 9 */
	{ 't', "\342\224\234" },	/* tee pointing right */
	{ 'u', "\342\224\244" },	/* tee pointing left */
	{ 'v', "\342\224\264" },	/* tee pointing up */
	{ 'w', "\342\224\254" },	/* tee pointing down */
	{ 'x', "\342\224\202" },	/* vertical line */
	{ 'y', "\342\211\244" },	/* less-than-or-equal-to */
	{ 'z', "\342\211\245" },	/* greater-than-or-equal-to */
	{ '{', "\317\200" },		/* greek pi */
	{ '|', "\342\211\240" },	/* not-equal */
	{ '}', "\302\243" },		/* UK pound sign */
	{ '~', "\302\267" }		/* bullet */
};

/* Table mapping UTF-8 to ACS entries. */
struct tty_acs_reverse_entry {
	const char	*string;
	u_char		 key;
};
static const struct tty_acs_reverse_entry tty_acs_reverse2[] = {
	{ "\302\267", '~' }
};
static const struct tty_acs_reverse_entry tty_acs_reverse3[] = {
	{ "\342\224\200", 'q' },
	{ "\342\224\201", 'q' },
	{ "\342\224\202", 'x' },
	{ "\342\224\203", 'x' },
	{ "\342\224\214", 'l' },
	{ "\342\224\217", 'k' },
	{ "\342\224\220", 'k' },
	{ "\342\224\223", 'l' },
	{ "\342\224\224", 'm' },
	{ "\342\224\227", 'm' },
	{ "\342\224\230", 'j' },
	{ "\342\224\233", 'j' },
	{ "\342\224\234", 't' },
	{ "\342\224\243", 't' },
	{ "\342\224\244", 'u' },
	{ "\342\224\253", 'u' },
	{ "\342\224\263", 'w' },
	{ "\342\224\264", 'v' },
	{ "\342\224\273", 'v' },
	{ "\342\224\274", 'n' },
	{ "\342\225\213", 'n' },
	{ "\342\225\220", 'q' },
	{ "\342\225\221", 'x' },
	{ "\342\225\224", 'l' },
	{ "\342\225\227", 'k' },
	{ "\342\225\232", 'm' },
	{ "\342\225\235", 'j' },
	{ "\342\225\240", 't' },
	{ "\342\225\243", 'u' },
	{ "\342\225\246", 'w' },
	{ "\342\225\251", 'v' },
	{ "\342\225\254", 'n' },
};

/* UTF-8 double borders. */
static const struct utf8_data tty_acs_double_borders_list[] = {
	{ "", 0, 0, 0 },
	{ "\342\225\221", 0, 3, 1 }, /* U+2551 */
	{ "\342\225\220", 0, 3, 1 }, /* U+2550 */
	{ "\342\225\224", 0, 3, 1 }, /* U+2554 */
	{ "\342\225\227", 0, 3, 1 }, /* U+2557 */
	{ "\342\225\232", 0, 3, 1 }, /* U+255A */
	{ "\342\225\235", 0, 3, 1 }, /* U+255D */
	{ "\342\225\246", 0, 3, 1 }, /* U+2566 */
	{ "\342\225\251", 0, 3, 1 }, /* U+2569 */
	{ "\342\225\240", 0, 3, 1 }, /* U+2560 */
	{ "\342\225\243", 0, 3, 1 }, /* U+2563 */
	{ "\342\225\254", 0, 3, 1 }, /* U+256C */
	{ "\302\267",	  0, 2, 1 }  /* U+00B7 */
};

/* UTF-8 heavy borders. */
static const struct utf8_data tty_acs_heavy_borders_list[] = {
	{ "", 0, 0, 0 },
	{ "\342\224\203", 0, 3, 1 }, /* U+2503 */
	{ "\342\224\201", 0, 3, 1 }, /* U+2501 */
	{ "\342\224\217", 0, 3, 1 }, /* U+250F */
	{ "\342\224\223", 0, 3, 1 }, /* U+2513 */
	{ "\342\224\227", 0, 3, 1 }, /* U+2517 */
	{ "\342\224\233", 0, 3, 1 }, /* U+251B */
	{ "\342\224\263", 0, 3, 1 }, /* U+2533 */
	{ "\342\224\273", 0, 3, 1 }, /* U+253B */
	{ "\342\224\243", 0, 3, 1 }, /* U+2523 */
	{ "\342\224\253", 0, 3, 1 }, /* U+252B */
	{ "\342\225\213", 0, 3, 1 }, /* U+254B */
	{ "\302\267",	  0, 2, 1 }  /* U+00B7 */
};

/* UTF-8 rounded borders. */
static const struct utf8_data tty_acs_rounded_borders_list[] = {
	{ "", 0, 0, 0 },
	{ "\342\224\202", 0, 3, 1 }, /* U+2502 */
	{ "\342\224\200", 0, 3, 1 }, /* U+2500 */
	{ "\342\225\255", 0, 3, 1 }, /* U+256D */
	{ "\342\225\256", 0, 3, 1 }, /* U+256E */
	{ "\342\225\260", 0, 3, 1 }, /* U+2570 */
	{ "\342\225\257", 0, 3, 1 }, /* U+256F */
	{ "\342\224\263", 0, 3, 1 }, /* U+2533 */
	{ "\342\224\273", 0, 3, 1 }, /* U+253B */
	{ "\342\224\234", 0, 3, 1 }, /* U+2524 */
	{ "\342\224\244", 0, 3, 1 }, /* U+251C */
	{ "\342\225\213", 0, 3, 1 }, /* U+254B */
	{ "\302\267",	  0, 2, 1 }  /* U+00B7 */
};

/* Get cell border character for double style. */
const struct utf8_data *
tty_acs_double_borders(int cell_type)
{
	return (&tty_acs_double_borders_list[cell_type]);
}

/* Get cell border character for heavy style. */
const struct utf8_data *
tty_acs_heavy_borders(int cell_type)
{
	return (&tty_acs_heavy_borders_list[cell_type]);
}

/* Get cell border character for rounded style. */
const struct utf8_data *
tty_acs_rounded_borders(int cell_type)
{
	return (&tty_acs_rounded_borders_list[cell_type]);
}

static int
tty_acs_cmp(const void *key, const void *value)
{
	const struct tty_acs_entry	*entry = value;
	int				 test = *(u_char *)key;

	return (test - entry->key);
}

static int
tty_acs_reverse_cmp(const void *key, const void *value)
{
	const struct tty_acs_reverse_entry	*entry = value;
	const char				*test = key;

	return (strcmp(test, entry->string));
}

/* Should this terminal use ACS instead of UTF-8 line drawing? */
int
tty_acs_needed(struct tty *tty)
{
	if (tty == NULL)
		return (0);

	/*
	 * If the U8 flag is present, it marks whether a terminal supports
	 * UTF-8 and ACS together.
	 *
	 * If it is present and zero, we force ACS - this gives users a way to
	 * turn off UTF-8 line drawing.
	 *
	 * If it is nonzero, we can fall through to the default and use UTF-8
	 * line drawing on UTF-8 terminals.
	 */
	if (tty_term_has(tty->term, TTYC_U8) &&
	    tty_term_number(tty->term, TTYC_U8) == 0)
		return (1);

	if (tty->client->flags & CLIENT_UTF8)
		return (0);
	return (1);
}

/* Retrieve ACS to output as UTF-8. */
const char *
tty_acs_get(struct tty *tty, u_char ch)
{
	const struct tty_acs_entry	*entry;

	/* Use the ACS set instead of UTF-8 if needed. */
	if (tty_acs_needed(tty)) {
		if (tty->term->acs[ch][0] == '\0')
			return (NULL);
		return (&tty->term->acs[ch][0]);
	}

	/* Otherwise look up the UTF-8 translation. */
	entry = bsearch(&ch, tty_acs_table, nitems(tty_acs_table),
	    sizeof tty_acs_table[0], tty_acs_cmp);
	if (entry == NULL)
		return (NULL);
	return (entry->string);
}

/* Reverse UTF-8 into ACS. */
int
tty_acs_reverse_get(__unused struct tty *tty, const char *s, size_t slen)
{
	const struct tty_acs_reverse_entry	*table, *entry;
	u_int					 items;

	if (slen == 2) {
		table = tty_acs_reverse2;
		items = nitems(tty_acs_reverse2);
	} else if (slen == 3) {
		table = tty_acs_reverse3;
		items = nitems(tty_acs_reverse3);
	} else
		return (-1);
	entry = bsearch(s, table, items, sizeof table[0], tty_acs_reverse_cmp);
	if (entry == NULL)
		return (-1);
	return (entry->key);
}