xref: /openbsd-src/lib/libcurses/tinfo/lib_options.c (revision 43003dfe3ad45d1698bed8a37f2b0f5b14f20d4f)
1 /*	$OpenBSD: lib_options.c,v 1.9 2001/01/22 18:01:53 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35 
36 /*
37 **	lib_options.c
38 **
39 **	The routines to handle option setting.
40 **
41 */
42 
43 #include <curses.priv.h>
44 
45 #include <term.h>
46 
47 MODULE_ID("$From: lib_options.c,v 1.42 2000/12/10 02:55:07 tom Exp $")
48 
49 NCURSES_EXPORT(int)
50 idlok(WINDOW *win, bool flag)
51 {
52     T((T_CALLED("idlok(%p,%d)"), win, flag));
53 
54     if (win) {
55 	_nc_idlok = win->_idlok = (flag && (has_il() || change_scroll_region));
56 	returnCode(OK);
57     } else
58 	returnCode(ERR);
59 }
60 
61 NCURSES_EXPORT(void)
62 idcok(WINDOW *win, bool flag)
63 {
64     T((T_CALLED("idcok(%p,%d)"), win, flag));
65 
66     if (win)
67 	_nc_idcok = win->_idcok = (flag && has_ic());
68 
69     returnVoid;
70 }
71 
72 NCURSES_EXPORT(int)
73 halfdelay(int t)
74 {
75     T((T_CALLED("halfdelay(%d)"), t));
76 
77     if (t < 1 || t > 255)
78 	returnCode(ERR);
79 
80     cbreak();
81     SP->_cbreak = t + 1;
82     returnCode(OK);
83 }
84 
85 NCURSES_EXPORT(int)
86 nodelay(WINDOW *win, bool flag)
87 {
88     T((T_CALLED("nodelay(%p,%d)"), win, flag));
89 
90     if (win) {
91 	if (flag == TRUE)
92 	    win->_delay = 0;
93 	else
94 	    win->_delay = -1;
95 	returnCode(OK);
96     } else
97 	returnCode(ERR);
98 }
99 
100 NCURSES_EXPORT(int)
101 notimeout(WINDOW *win, bool f)
102 {
103     T((T_CALLED("notimout(%p,%d)"), win, f));
104 
105     if (win) {
106 	win->_notimeout = f;
107 	returnCode(OK);
108     } else
109 	returnCode(ERR);
110 }
111 
112 NCURSES_EXPORT(void)
113 wtimeout(WINDOW *win, int delay)
114 {
115     T((T_CALLED("wtimeout(%p,%d)"), win, delay));
116 
117     if (win) {
118 	win->_delay = delay;
119     }
120 }
121 
122 NCURSES_EXPORT(int)
123 keypad(WINDOW *win, bool flag)
124 {
125     T((T_CALLED("keypad(%p,%d)"), win, flag));
126 
127     if (win) {
128 	win->_use_keypad = flag;
129 	returnCode(_nc_keypad(flag));
130     } else
131 	returnCode(ERR);
132 }
133 
134 NCURSES_EXPORT(int)
135 meta(WINDOW *win GCC_UNUSED, bool flag)
136 {
137     /* Ok, we stay relaxed and don't signal an error if win is NULL */
138     T((T_CALLED("meta(%p,%d)"), win, flag));
139 
140     SP->_use_meta = flag;
141 
142     if (flag && meta_on) {
143 	TPUTS_TRACE("meta_on");
144 	putp(meta_on);
145     } else if (!flag && meta_off) {
146 	TPUTS_TRACE("meta_off");
147 	putp(meta_off);
148     }
149     returnCode(OK);
150 }
151 
152 /* curs_set() moved here to narrow the kernel interface */
153 
154 NCURSES_EXPORT(int)
155 curs_set(int vis)
156 {
157     int cursor = SP->_cursor;
158 
159     T((T_CALLED("curs_set(%d)"), vis));
160 
161     if (vis < 0 || vis > 2)
162 	returnCode(ERR);
163 
164     if (vis == cursor)
165 	returnCode(cursor);
166 
167     switch (vis) {
168     case 2:
169 	if (cursor_visible) {
170 	    TPUTS_TRACE("cursor_visible");
171 	    putp(cursor_visible);
172 	} else
173 	    returnCode(ERR);
174 	break;
175     case 1:
176 	if (cursor_normal) {
177 	    TPUTS_TRACE("cursor_normal");
178 	    putp(cursor_normal);
179 	} else
180 	    returnCode(ERR);
181 	break;
182     case 0:
183 	if (cursor_invisible) {
184 	    TPUTS_TRACE("cursor_invisible");
185 	    putp(cursor_invisible);
186 	} else
187 	    returnCode(ERR);
188 	break;
189     }
190     SP->_cursor = vis;
191     _nc_flush();
192 
193     returnCode(cursor == -1 ? 1 : cursor);
194 }
195 
196 NCURSES_EXPORT(int)
197 typeahead(int fd)
198 {
199     T((T_CALLED("typeahead(%d)"), fd));
200     SP->_checkfd = fd;
201     returnCode(OK);
202 }
203 
204 /*
205 **      has_key()
206 **
207 **      Return TRUE if the current terminal has the given key
208 **
209 */
210 
211 #if NCURSES_EXT_FUNCS
212 static int
213 has_key_internal(int keycode, struct tries *tp)
214 {
215     if (tp == 0)
216 	return (FALSE);
217     else if (tp->value == keycode)
218 	return (TRUE);
219     else
220 	return (has_key_internal(keycode, tp->child)
221 		|| has_key_internal(keycode, tp->sibling));
222 }
223 
224 NCURSES_EXPORT(int)
225 has_key(int keycode)
226 {
227     T((T_CALLED("has_key(%d)"), keycode));
228     returnCode(has_key_internal(keycode, SP->_keytry));
229 }
230 #endif /* NCURSES_EXT_FUNCS */
231 
232 /* Turn the keypad on/off
233  *
234  * Note:  we flush the output because changing this mode causes some terminals
235  * to emit different escape sequences for cursor and keypad keys.  If we don't
236  * flush, then the next wgetch may get the escape sequence that corresponds to
237  * the terminal state _before_ switching modes.
238  */
239 NCURSES_EXPORT(int)
240 _nc_keypad(bool flag)
241 {
242     if (flag && keypad_xmit) {
243 	TPUTS_TRACE("keypad_xmit");
244 	putp(keypad_xmit);
245 	_nc_flush();
246     } else if (!flag && keypad_local) {
247 	TPUTS_TRACE("keypad_local");
248 	putp(keypad_local);
249 	_nc_flush();
250     }
251 
252     if (flag && !SP->_tried) {
253 	_nc_init_keytry();
254 	SP->_tried = TRUE;
255     }
256     return (OK);
257 }
258