]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_options.c
0edf9d5a9a0191094e9a5c235c54896887fe61ad
[ncurses.git] / ncurses / tinfo / lib_options.c
1 /****************************************************************************
2  * Copyright (c) 1998-2002,2003 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-2003               *
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
46 MODULE_ID("$Id: lib_options.c,v 1.47 2003/10/25 19:51:38 tom Exp $")
47
48 NCURSES_EXPORT(int)
49 idlok(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
60 NCURSES_EXPORT(void)
61 idcok(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
71 NCURSES_EXPORT(int)
72 halfdelay(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
84 NCURSES_EXPORT(int)
85 nodelay(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
99 NCURSES_EXPORT(int)
100 notimeout(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
111 NCURSES_EXPORT(void)
112 wtimeout(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
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     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
159 NCURSES_EXPORT(int)
160 curs_set(int vis)
161 {
162     int cursor = SP->_cursor;
163
164     T((T_CALLED("curs_set(%d)"), vis));
165
166     if (vis < 0 || vis > 2)
167         returnCode(ERR);
168
169     if (vis == cursor)
170         returnCode(cursor);
171
172     switch (vis) {
173     case 2:
174         if (cursor_visible) {
175             TPUTS_TRACE("cursor_visible");
176             putp(cursor_visible);
177         } else
178             returnCode(ERR);
179         break;
180     case 1:
181         if (cursor_normal) {
182             TPUTS_TRACE("cursor_normal");
183             putp(cursor_normal);
184         } else
185             returnCode(ERR);
186         break;
187     case 0:
188         if (cursor_invisible) {
189             TPUTS_TRACE("cursor_invisible");
190             putp(cursor_invisible);
191         } else
192             returnCode(ERR);
193         break;
194     }
195     SP->_cursor = vis;
196     _nc_flush();
197
198     returnCode(cursor == -1 ? 1 : cursor);
199 }
200
201 NCURSES_EXPORT(int)
202 typeahead(int fd)
203 {
204     T((T_CALLED("typeahead(%d)"), fd));
205     if (SP != 0) {
206         SP->_checkfd = fd;
207         returnCode(OK);
208     } else {
209         returnCode(ERR);
210     }
211 }
212
213 /*
214 **      has_key()
215 **
216 **      Return TRUE if the current terminal has the given key
217 **
218 */
219
220 #if NCURSES_EXT_FUNCS
221 static int
222 has_key_internal(int keycode, struct tries *tp)
223 {
224     if (tp == 0)
225         return (FALSE);
226     else if (tp->value == keycode)
227         return (TRUE);
228     else
229         return (has_key_internal(keycode, tp->child)
230                 || has_key_internal(keycode, tp->sibling));
231 }
232
233 NCURSES_EXPORT(int)
234 has_key(int keycode)
235 {
236     T((T_CALLED("has_key(%d)"), keycode));
237     returnCode(SP != 0 ? has_key_internal(keycode, SP->_keytry) : FALSE);
238 }
239 #endif /* NCURSES_EXT_FUNCS */
240
241 /* Turn the keypad on/off
242  *
243  * Note:  we flush the output because changing this mode causes some terminals
244  * to emit different escape sequences for cursor and keypad keys.  If we don't
245  * flush, then the next wgetch may get the escape sequence that corresponds to
246  * the terminal state _before_ switching modes.
247  */
248 NCURSES_EXPORT(int)
249 _nc_keypad(bool flag)
250 {
251     if (flag && keypad_xmit) {
252         TPUTS_TRACE("keypad_xmit");
253         putp(keypad_xmit);
254         _nc_flush();
255     } else if (!flag && keypad_local) {
256         TPUTS_TRACE("keypad_local");
257         putp(keypad_local);
258         _nc_flush();
259     }
260
261     if (flag && SP != 0 && !SP->_tried) {
262         _nc_init_keytry();
263         SP->_tried = TRUE;
264     }
265     SP->_keypad_on = flag;
266     return (OK);
267 }