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