1/****************************************************************************
2 * Copyright (c) 1998-2003,2005 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 ****************************************************************************/
34
35/*
36**	lib_options.c
37**
38**	The routines to handle option setting.
39**
40*/
41
42#include <curses.priv.h>
43
44#include <term.h>
45
46MODULE_ID("$Id: lib_options.c,v 1.48 2005/01/22 21:13:34 tom Exp $")
47
48NCURSES_EXPORT(int)
49idlok(WINDOW *win, bool flag)
50{
51    T((T_CALLED("idlok(%p,%d)"), win, flag));
52
53    if (win) {
54	_nc_idlok = win->_idlok = (flag && (has_il() || change_scroll_region));
55	returnCode(OK);
56    } else
57	returnCode(ERR);
58}
59
60NCURSES_EXPORT(void)
61idcok(WINDOW *win, bool flag)
62{
63    T((T_CALLED("idcok(%p,%d)"), win, flag));
64
65    if (win)
66	_nc_idcok = win->_idcok = (flag && has_ic());
67
68    returnVoid;
69}
70
71NCURSES_EXPORT(int)
72halfdelay(int t)
73{
74    T((T_CALLED("halfdelay(%d)"), t));
75
76    if (t < 1 || t > 255 || SP == 0)
77	returnCode(ERR);
78
79    cbreak();
80    SP->_cbreak = t + 1;
81    returnCode(OK);
82}
83
84NCURSES_EXPORT(int)
85nodelay(WINDOW *win, bool flag)
86{
87    T((T_CALLED("nodelay(%p,%d)"), win, flag));
88
89    if (win) {
90	if (flag == TRUE)
91	    win->_delay = 0;
92	else
93	    win->_delay = -1;
94	returnCode(OK);
95    } else
96	returnCode(ERR);
97}
98
99NCURSES_EXPORT(int)
100notimeout(WINDOW *win, bool f)
101{
102    T((T_CALLED("notimout(%p,%d)"), win, f));
103
104    if (win) {
105	win->_notimeout = f;
106	returnCode(OK);
107    } else
108	returnCode(ERR);
109}
110
111NCURSES_EXPORT(void)
112wtimeout(WINDOW *win, int delay)
113{
114    T((T_CALLED("wtimeout(%p,%d)"), win, delay));
115
116    if (win) {
117	win->_delay = delay;
118    }
119    returnVoid;
120}
121
122NCURSES_EXPORT(int)
123keypad(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
134NCURSES_EXPORT(int)
135meta(WINDOW *win GCC_UNUSED, bool flag)
136{
137    int result = ERR;
138
139    /* Ok, we stay relaxed and don't signal an error if win is NULL */
140    T((T_CALLED("meta(%p,%d)"), win, flag));
141
142    if (SP != 0) {
143	SP->_use_meta = flag;
144
145	if (flag && meta_on) {
146	    TPUTS_TRACE("meta_on");
147	    putp(meta_on);
148	} else if (!flag && meta_off) {
149	    TPUTS_TRACE("meta_off");
150	    putp(meta_off);
151	}
152	result = OK;
153    }
154    returnCode(result);
155}
156
157/* curs_set() moved here to narrow the kernel interface */
158
159NCURSES_EXPORT(int)
160curs_set(int vis)
161{
162    int result = ERR;
163
164    T((T_CALLED("curs_set(%d)"), vis));
165    if (SP != 0 && vis >= 0 && vis <= 2) {
166	int cursor = SP->_cursor;
167
168	if (vis == cursor) {
169	    result = cursor;
170	} else {
171	    result = (cursor == -1 ? 1 : cursor);
172	    switch (vis) {
173	    case 2:
174		if (cursor_visible) {
175		    TPUTS_TRACE("cursor_visible");
176		    putp(cursor_visible);
177		} else
178		    result = ERR;
179		break;
180	    case 1:
181		if (cursor_normal) {
182		    TPUTS_TRACE("cursor_normal");
183		    putp(cursor_normal);
184		} else
185		    result = ERR;
186		break;
187	    case 0:
188		if (cursor_invisible) {
189		    TPUTS_TRACE("cursor_invisible");
190		    putp(cursor_invisible);
191		} else
192		    result = ERR;
193		break;
194	    }
195	    SP->_cursor = vis;
196	    _nc_flush();
197	}
198    }
199    returnCode(result);
200}
201
202NCURSES_EXPORT(int)
203typeahead(int fd)
204{
205    T((T_CALLED("typeahead(%d)"), fd));
206    if (SP != 0) {
207	SP->_checkfd = fd;
208	returnCode(OK);
209    } else {
210	returnCode(ERR);
211    }
212}
213
214/*
215**      has_key()
216**
217**      Return TRUE if the current terminal has the given key
218**
219*/
220
221#if NCURSES_EXT_FUNCS
222static int
223has_key_internal(int keycode, struct tries *tp)
224{
225    if (tp == 0)
226	return (FALSE);
227    else if (tp->value == keycode)
228	return (TRUE);
229    else
230	return (has_key_internal(keycode, tp->child)
231		|| has_key_internal(keycode, tp->sibling));
232}
233
234NCURSES_EXPORT(int)
235has_key(int keycode)
236{
237    T((T_CALLED("has_key(%d)"), keycode));
238    returnCode(SP != 0 ? has_key_internal(keycode, SP->_keytry) : FALSE);
239}
240#endif /* NCURSES_EXT_FUNCS */
241
242/* Turn the keypad on/off
243 *
244 * Note:  we flush the output because changing this mode causes some terminals
245 * to emit different escape sequences for cursor and keypad keys.  If we don't
246 * flush, then the next wgetch may get the escape sequence that corresponds to
247 * the terminal state _before_ switching modes.
248 */
249NCURSES_EXPORT(int)
250_nc_keypad(bool flag)
251{
252    if (flag && keypad_xmit) {
253	TPUTS_TRACE("keypad_xmit");
254	putp(keypad_xmit);
255	_nc_flush();
256    } else if (!flag && keypad_local) {
257	TPUTS_TRACE("keypad_local");
258	putp(keypad_local);
259	_nc_flush();
260    }
261
262    if (SP != 0) {
263	if (flag && !SP->_tried) {
264	    _nc_init_keytry();
265	    SP->_tried = TRUE;
266	}
267	SP->_keypad_on = flag;
268    }
269    return (OK);
270}
271