xref: /openbsd-src/usr.bin/tmux/tty-acs.c (revision 7350f337b9e3eb4461d99580e625c7ef148d107c)
1 /* $OpenBSD: tty-acs.c,v 1.7 2019/05/17 05:48:25 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2010 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <stdlib.h>
22 
23 #include "tmux.h"
24 
25 static int	tty_acs_cmp(const void *, const void *);
26 
27 /* Table mapping ACS entries to UTF-8. */
28 struct tty_acs_entry {
29 	u_char	 	 key;
30 	const char	*string;
31 };
32 static const struct tty_acs_entry tty_acs_table[] = {
33 	{ '+', "\342\206\222" },	/* arrow pointing right */
34 	{ ',', "\342\206\220" },	/* arrow pointing left */
35 	{ '-', "\342\206\221" },	/* arrow pointing up */
36 	{ '.', "\342\206\223" },	/* arrow pointing down */
37 	{ '0', "\342\226\256" },	/* solid square block */
38 	{ '`', "\342\227\206" },	/* diamond */
39 	{ 'a', "\342\226\222" },	/* checker board (stipple) */
40 	{ 'b', "\342\220\211" },
41 	{ 'c', "\342\220\214" },
42 	{ 'd', "\342\220\215" },
43 	{ 'e', "\342\220\212" },
44 	{ 'f', "\302\260" },		/* degree symbol */
45 	{ 'g', "\302\261" },		/* plus/minus */
46 	{ 'h', "\342\220\244" },
47 	{ 'i', "\342\220\213" },
48 	{ 'j', "\342\224\230" },	/* lower right corner */
49 	{ 'k', "\342\224\220" },	/* upper right corner */
50 	{ 'l', "\342\224\214" },	/* upper left corner */
51 	{ 'm', "\342\224\224" },	/* lower left corner */
52 	{ 'n', "\342\224\274" },	/* large plus or crossover */
53 	{ 'o', "\342\216\272" },	/* scan line 1 */
54 	{ 'p', "\342\216\273" },	/* scan line 3 */
55 	{ 'q', "\342\224\200" },	/* horizontal line */
56 	{ 'r', "\342\216\274" },	/* scan line 7 */
57 	{ 's', "\342\216\275" },	/* scan line 9 */
58 	{ 't', "\342\224\234" },	/* tee pointing right */
59 	{ 'u', "\342\224\244" },	/* tee pointing left */
60 	{ 'v', "\342\224\264" },	/* tee pointing up */
61 	{ 'w', "\342\224\254" },	/* tee pointing down */
62 	{ 'x', "\342\224\202" },	/* vertical line */
63 	{ 'y', "\342\211\244" },	/* less-than-or-equal-to */
64 	{ 'z', "\342\211\245" },	/* greater-than-or-equal-to */
65 	{ '{', "\317\200" },   		/* greek pi */
66 	{ '|', "\342\211\240" },	/* not-equal */
67 	{ '}', "\302\243" },		/* UK pound sign */
68 	{ '~', "\302\267" }		/* bullet */
69 };
70 
71 static int
72 tty_acs_cmp(const void *key, const void *value)
73 {
74 	const struct tty_acs_entry	*entry = value;
75 	u_char				 ch;
76 
77 	ch = *(u_char *) key;
78 	return (ch - entry->key);
79 }
80 
81 /* Should this terminal use ACS instead of UTF-8 line drawing? */
82 int
83 tty_acs_needed(struct tty *tty)
84 {
85 	if (tty == NULL)
86 		return (0);
87 
88 	/*
89 	 * If the U8 flag is present, it marks whether a terminal supports
90 	 * UTF-8 and ACS together.
91 	 *
92 	 * If it is present and zero, we force ACS - this gives users a way to
93 	 * turn off UTF-8 line drawing.
94 	 *
95 	 * If it is nonzero, we can fall through to the default and use UTF-8
96 	 * line drawing on UTF-8 terminals.
97 	 */
98 	if (tty_term_has(tty->term, TTYC_U8) &&
99 	    tty_term_number(tty->term, TTYC_U8) == 0)
100 		return (1);
101 
102 	if (tty->flags & TTY_UTF8)
103 		return (0);
104 	return (1);
105 }
106 
107 /* Retrieve ACS to output as a string. */
108 const char *
109 tty_acs_get(struct tty *tty, u_char ch)
110 {
111 	struct tty_acs_entry	*entry;
112 
113 	/* Use the ACS set instead of UTF-8 if needed. */
114 	if (tty_acs_needed(tty)) {
115 		if (tty->term->acs[ch][0] == '\0')
116 			return (NULL);
117 		return (&tty->term->acs[ch][0]);
118 	}
119 
120 	/* Otherwise look up the UTF-8 translation. */
121 	entry = bsearch(&ch, tty_acs_table, nitems(tty_acs_table),
122 	    sizeof tty_acs_table[0], tty_acs_cmp);
123 	if (entry == NULL)
124 		return (NULL);
125 	return (entry->string);
126 }
127