1 /* $OpenBSD: tty-acs.c,v 1.6 2017/05/15 16:44:04 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 { 'f', "\302\260" }, /* degree symbol */ 41 { 'g', "\302\261" }, /* plus/minus */ 42 { 'h', "\342\226\222" }, /* board of squares */ 43 { 'i', "\342\230\203" }, /* lantern symbol */ 44 { 'j', "\342\224\230" }, /* lower right corner */ 45 { 'k', "\342\224\220" }, /* upper right corner */ 46 { 'l', "\342\224\214" }, /* upper left corner */ 47 { 'm', "\342\224\224" }, /* lower left corner */ 48 { 'n', "\342\224\274" }, /* large plus or crossover */ 49 { 'o', "\342\216\272" }, /* scan line 1 */ 50 { 'p', "\342\216\273" }, /* scan line 3 */ 51 { 'q', "\342\224\200" }, /* horizontal line */ 52 { 'r', "\342\216\274" }, /* scan line 7 */ 53 { 's', "\342\216\275" }, /* scan line 9 */ 54 { 't', "\342\224\234" }, /* tee pointing right */ 55 { 'u', "\342\224\244" }, /* tee pointing left */ 56 { 'v', "\342\224\264" }, /* tee pointing up */ 57 { 'w', "\342\224\254" }, /* tee pointing down */ 58 { 'x', "\342\224\202" }, /* vertical line */ 59 { 'y', "\342\211\244" }, /* less-than-or-equal-to */ 60 { 'z', "\342\211\245" }, /* greater-than-or-equal-to */ 61 { '{', "\317\200" }, /* greek pi */ 62 { '|', "\342\211\240" }, /* not-equal */ 63 { '}', "\302\243" }, /* UK pound sign */ 64 { '~', "\302\267" } /* bullet */ 65 }; 66 67 static int 68 tty_acs_cmp(const void *key, const void *value) 69 { 70 const struct tty_acs_entry *entry = value; 71 u_char ch; 72 73 ch = *(u_char *) key; 74 return (ch - entry->key); 75 } 76 77 /* Should this terminal use ACS instead of UTF-8 line drawing? */ 78 int 79 tty_acs_needed(struct tty *tty) 80 { 81 if (tty == NULL) 82 return (0); 83 84 /* 85 * If the U8 flag is present, it marks whether a terminal supports 86 * UTF-8 and ACS together. 87 * 88 * If it is present and zero, we force ACS - this gives users a way to 89 * turn off UTF-8 line drawing. 90 * 91 * If it is nonzero, we can fall through to the default and use UTF-8 92 * line drawing on UTF-8 terminals. 93 */ 94 if (tty_term_has(tty->term, TTYC_U8) && 95 tty_term_number(tty->term, TTYC_U8) == 0) 96 return (1); 97 98 if (tty->flags & TTY_UTF8) 99 return (0); 100 return (1); 101 } 102 103 /* Retrieve ACS to output as a string. */ 104 const char * 105 tty_acs_get(struct tty *tty, u_char ch) 106 { 107 struct tty_acs_entry *entry; 108 109 /* Use the ACS set instead of UTF-8 if needed. */ 110 if (tty_acs_needed(tty)) { 111 if (tty->term->acs[ch][0] == '\0') 112 return (NULL); 113 return (&tty->term->acs[ch][0]); 114 } 115 116 /* Otherwise look up the UTF-8 translation. */ 117 entry = bsearch(&ch, tty_acs_table, nitems(tty_acs_table), 118 sizeof tty_acs_table[0], tty_acs_cmp); 119 if (entry == NULL) 120 return (NULL); 121 return (entry->string); 122 } 123