lib_acs.c revision 262685
1/****************************************************************************
2 * Copyright (c) 1998-2010,2013 Free Software Foundation, Inc.              *
3 *                                                                          *
4 * Permission is hereby granted, free of charge, to any person obtaining a  *
5 * copy of this software and associated documentation files (the            *
6 * "Software"), to deal in the Software without restriction, including      *
7 * without limitation the rights to use, copy, modify, merge, publish,      *
8 * distribute, distribute with modifications, sublicense, and/or sell       *
9 * copies of the Software, and to permit persons to whom the Software is    *
10 * furnished to do so, subject to the following conditions:                 *
11 *                                                                          *
12 * The above copyright notice and this permission notice shall be included  *
13 * in all copies or substantial portions of the Software.                   *
14 *                                                                          *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22 *                                                                          *
23 * Except as contained in this notice, the name(s) of the above copyright   *
24 * holders shall not be used in advertising or otherwise to promote the     *
25 * sale, use or other dealings in this Software without prior written       *
26 * authorization.                                                           *
27 ****************************************************************************/
28
29/****************************************************************************
30 *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31 *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32 *     and: Thomas E. Dickey                        1996-on                 *
33 *     and: Juergen Pfeifer                         2008                    *
34 ****************************************************************************/
35
36#include <curses.priv.h>
37
38#ifndef CUR
39#define CUR SP_TERMTYPE
40#endif
41
42MODULE_ID("$Id: lib_acs.c,v 1.44 2013/01/12 17:24:42 tom Exp $")
43
44#if BROKEN_LINKER || USE_REENTRANT
45#define MyBuffer _nc_prescreen.real_acs_map
46NCURSES_EXPORT(chtype *)
47NCURSES_PUBLIC_VAR(acs_map) (void)
48{
49    if (MyBuffer == 0)
50	MyBuffer = typeCalloc(chtype, ACS_LEN);
51    return MyBuffer;
52}
53#undef MyBuffer
54#else
55NCURSES_EXPORT_VAR (chtype) acs_map[ACS_LEN] =
56{
57    0
58};
59#endif
60
61#ifdef USE_TERM_DRIVER
62NCURSES_EXPORT(chtype)
63NCURSES_SP_NAME(_nc_acs_char) (NCURSES_SP_DCLx int c)
64{
65    chtype *map;
66    if (c < 0 || c >= ACS_LEN)
67	return (chtype) 0;
68    map = (SP_PARM != 0) ? SP_PARM->_acs_map :
69#if BROKEN_LINKER || USE_REENTRANT
70	_nc_prescreen.real_acs_map
71#else
72	acs_map
73#endif
74	;
75    return map[c];
76}
77#endif /* USE_TERM_DRIVER */
78
79NCURSES_EXPORT(void)
80NCURSES_SP_NAME(_nc_init_acs) (NCURSES_SP_DCL0)
81{
82    chtype *fake_map = acs_map;
83    chtype *real_map = SP_PARM != 0 ? SP_PARM->_acs_map : fake_map;
84    int j;
85
86    T(("initializing ACS map"));
87
88    /*
89     * If we're using this from curses (rather than terminfo), we are storing
90     * the mapping information in the SCREEN struct so we can decide how to
91     * render it.
92     */
93    if (real_map != fake_map) {
94	for (j = 1; j < ACS_LEN; ++j) {
95	    real_map[j] = 0;
96	    fake_map[j] = A_ALTCHARSET | (chtype) j;
97	    if (SP_PARM)
98		SP_PARM->_screen_acs_map[j] = FALSE;
99	}
100    } else {
101	for (j = 1; j < ACS_LEN; ++j) {
102	    real_map[j] = 0;
103	}
104    }
105
106    /*
107     * Initializations for a UNIX-like multi-terminal environment.  Use
108     * ASCII chars and count on the terminfo description to do better.
109     */
110    real_map['l'] = '+';	/* should be upper left corner */
111    real_map['m'] = '+';	/* should be lower left corner */
112    real_map['k'] = '+';	/* should be upper right corner */
113    real_map['j'] = '+';	/* should be lower right corner */
114    real_map['u'] = '+';	/* should be tee pointing left */
115    real_map['t'] = '+';	/* should be tee pointing right */
116    real_map['v'] = '+';	/* should be tee pointing up */
117    real_map['w'] = '+';	/* should be tee pointing down */
118    real_map['q'] = '-';	/* should be horizontal line */
119    real_map['x'] = '|';	/* should be vertical line */
120    real_map['n'] = '+';	/* should be large plus or crossover */
121    real_map['o'] = '~';	/* should be scan line 1 */
122    real_map['s'] = '_';	/* should be scan line 9 */
123    real_map['`'] = '+';	/* should be diamond */
124    real_map['a'] = ':';	/* should be checker board (stipple) */
125    real_map['f'] = '\'';	/* should be degree symbol */
126    real_map['g'] = '#';	/* should be plus/minus */
127    real_map['~'] = 'o';	/* should be bullet */
128    real_map[','] = '<';	/* should be arrow pointing left */
129    real_map['+'] = '>';	/* should be arrow pointing right */
130    real_map['.'] = 'v';	/* should be arrow pointing down */
131    real_map['-'] = '^';	/* should be arrow pointing up */
132    real_map['h'] = '#';	/* should be board of squares */
133    real_map['i'] = '#';	/* should be lantern symbol */
134    real_map['0'] = '#';	/* should be solid square block */
135    /* these defaults were invented for ncurses */
136    real_map['p'] = '-';	/* should be scan line 3 */
137    real_map['r'] = '-';	/* should be scan line 7 */
138    real_map['y'] = '<';	/* should be less-than-or-equal-to */
139    real_map['z'] = '>';	/* should be greater-than-or-equal-to */
140    real_map['{'] = '*';	/* should be greek pi */
141    real_map['|'] = '!';	/* should be not-equal */
142    real_map['}'] = 'f';	/* should be pound-sterling symbol */
143    /* thick-line-drawing */
144    real_map['L'] = '+';	/* upper left corner */
145    real_map['M'] = '+';	/* lower left corner */
146    real_map['K'] = '+';	/* upper right corner */
147    real_map['J'] = '+';	/* lower right corner */
148    real_map['T'] = '+';	/* tee pointing left */
149    real_map['U'] = '+';	/* tee pointing right */
150    real_map['V'] = '+';	/* tee pointing up */
151    real_map['W'] = '+';	/* tee pointing down */
152    real_map['Q'] = '-';	/* horizontal line */
153    real_map['X'] = '|';	/* vertical line */
154    real_map['N'] = '+';	/* large plus or crossover */
155    /* double-line-drawing */
156    real_map['C'] = '+';	/* upper left corner */
157    real_map['D'] = '+';	/* lower left corner */
158    real_map['B'] = '+';	/* upper right corner */
159    real_map['A'] = '+';	/* lower right corner */
160    real_map['G'] = '+';	/* tee pointing left */
161    real_map['F'] = '+';	/* tee pointing right */
162    real_map['H'] = '+';	/* tee pointing up */
163    real_map['I'] = '+';	/* tee pointing down */
164    real_map['R'] = '-';	/* horizontal line */
165    real_map['Y'] = '|';	/* vertical line */
166    real_map['E'] = '+';	/* large plus or crossover */
167
168#ifdef USE_TERM_DRIVER
169    CallDriver_2(SP_PARM, initacs, real_map, fake_map);
170#else
171    if (ena_acs != NULL) {
172	NCURSES_PUTP2("ena_acs", ena_acs);
173    }
174#if NCURSES_EXT_FUNCS
175    /*
176     * Linux console "supports" the "PC ROM" character set by the coincidence
177     * that smpch/rmpch and smacs/rmacs have the same values.  ncurses has
178     * no codepage support (see SCO Merge for an example).  Outside of the
179     * values defined in acsc, there are no definitions for the "PC ROM"
180     * character set (assumed by some applications to be codepage 437), but we
181     * allow those applications to use those codepoints.
182     *
183     * test/blue.c uses this feature.
184     */
185#define PCH_KLUDGE(a,b) (a != 0 && b != 0 && !strcmp(a,b))
186    if (PCH_KLUDGE(enter_pc_charset_mode, enter_alt_charset_mode) &&
187	PCH_KLUDGE(exit_pc_charset_mode, exit_alt_charset_mode)) {
188	size_t i;
189	for (i = 1; i < ACS_LEN; ++i) {
190	    if (real_map[i] == 0) {
191		real_map[i] = (chtype) i;
192		if (real_map != fake_map) {
193		    if (SP != 0)
194			SP->_screen_acs_map[i] = TRUE;
195		}
196	    }
197	}
198    }
199#endif
200
201    if (acs_chars != NULL) {
202	size_t i = 0;
203	size_t length = strlen(acs_chars);
204
205	while (i + 1 < length) {
206	    if (acs_chars[i] != 0 && UChar(acs_chars[i]) < ACS_LEN) {
207		real_map[UChar(acs_chars[i])] = UChar(acs_chars[i + 1]) | A_ALTCHARSET;
208		if (SP != 0)
209		    SP->_screen_acs_map[UChar(acs_chars[i])] = TRUE;
210	    }
211	    i += 2;
212	}
213    }
214#ifdef TRACE
215    /* Show the equivalent mapping, noting if it does not match the
216     * given attribute, whether by re-ordering or duplication.
217     */
218    if (USE_TRACEF(TRACE_CALLS)) {
219	size_t n, m;
220	char show[ACS_LEN * 2 + 1];
221	for (n = 1, m = 0; n < ACS_LEN; n++) {
222	    if (real_map[n] != 0) {
223		show[m++] = (char) n;
224		show[m++] = (char) ChCharOf(real_map[n]);
225	    }
226	}
227	show[m] = 0;
228	if (acs_chars == NULL || strcmp(acs_chars, show))
229	    _tracef("%s acs_chars %s",
230		    (acs_chars == NULL) ? "NULL" : "READ",
231		    _nc_visbuf(acs_chars));
232	_tracef("%s acs_chars %s",
233		(acs_chars == NULL)
234		? "NULL"
235		: (strcmp(acs_chars, show)
236		   ? "DIFF"
237		   : "SAME"),
238		_nc_visbuf(show));
239	_nc_unlock_global(tracef);
240    }
241#endif /* TRACE */
242#endif
243}
244
245#if NCURSES_SP_FUNCS
246NCURSES_EXPORT(void)
247_nc_init_acs(void)
248{
249    NCURSES_SP_NAME(_nc_init_acs) (CURRENT_SCREEN);
250}
251#endif
252