]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_options.c
ncurses 5.0
[ncurses.git] / ncurses / tinfo / lib_options.c
1 /****************************************************************************
2  * Copyright (c) 1998 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 /*
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>       /* keypad_xmit, keypad_local, meta_on, meta_off */
45                         /* cursor_visible,cursor_normal,cursor_invisible */
46
47 MODULE_ID("$Id: lib_options.c,v 1.36 1999/10/22 21:38:57 tom Exp $")
48
49 int 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         }
57         else
58           returnCode(ERR);
59 }
60
61
62 void 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 int halfdelay(int t)
73 {
74         T((T_CALLED("halfdelay(%d)"), t));
75
76         if (t < 1 || t > 255)
77                 returnCode(ERR);
78
79         cbreak();
80         SP->_cbreak = t+1;
81         returnCode(OK);
82 }
83
84 int 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 win->_delay = -1;
92           returnCode(OK);
93         }
94         else
95           returnCode(ERR);
96 }
97
98 int notimeout(WINDOW *win, bool f)
99 {
100         T((T_CALLED("notimout(%p,%d)"), win, f));
101
102         if (win) {
103           win->_notimeout = f;
104           returnCode(OK);
105         }
106         else
107           returnCode(ERR);
108 }
109
110 void wtimeout(WINDOW *win, int delay)
111 {
112         T((T_CALLED("wtimeout(%p,%d)"), win, delay));
113
114         if (win) {
115           win->_delay = delay;
116         }
117 }
118
119 int keypad(WINDOW *win, bool flag)
120 {
121         T((T_CALLED("keypad(%p,%d)"), win, flag));
122
123         if (win) {
124           win->_use_keypad = flag;
125           returnCode(_nc_keypad(flag));
126         }
127         else
128           returnCode(ERR);
129 }
130
131
132 int meta(WINDOW *win GCC_UNUSED, bool flag)
133 {
134         /* Ok, we stay relaxed and don't signal an error if win is NULL */
135         T((T_CALLED("meta(%p,%d)"), win, flag));
136
137         SP->_use_meta = flag;
138
139         if (flag  &&  meta_on)
140         {
141             TPUTS_TRACE("meta_on");
142             putp(meta_on);
143         }
144         else if (! flag  &&  meta_off)
145         {
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 int curs_set(int vis)
155 {
156 int cursor = SP->_cursor;
157
158         T((T_CALLED("curs_set(%d)"), vis));
159
160         if (vis < 0 || vis > 2)
161                 returnCode(ERR);
162
163         if (vis == cursor)
164                 returnCode(cursor);
165
166         switch(vis) {
167         case 2:
168                 if (cursor_visible)
169                 {
170                         TPUTS_TRACE("cursor_visible");
171                         putp(cursor_visible);
172                 }
173                 else
174                         returnCode(ERR);
175                 break;
176         case 1:
177                 if (cursor_normal)
178                 {
179                         TPUTS_TRACE("cursor_normal");
180                         putp(cursor_normal);
181                 }
182                 else
183                         returnCode(ERR);
184                 break;
185         case 0:
186                 if (cursor_invisible)
187                 {
188                         TPUTS_TRACE("cursor_invisible");
189                         putp(cursor_invisible);
190                 }
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 int typeahead(int fd)
202 {
203         T((T_CALLED("typeahead(%d)"), fd));
204         SP->_checkfd = fd;
205         returnCode(OK);
206 }
207
208 /*
209 **      has_key()
210 **
211 **      Return TRUE if the current terminal has the given key
212 **
213 */
214
215 #ifdef NCURSES_EXT_FUNCS
216 static int has_key_internal(int keycode, struct tries *tp)
217 {
218     if (tp == 0)
219         return(FALSE);
220     else if (tp->value == keycode)
221         return(TRUE);
222     else
223         return(has_key_internal(keycode, tp->child)
224                || has_key_internal(keycode, tp->sibling));
225 }
226
227 int has_key(int keycode)
228 {
229     T((T_CALLED("has_key(%d)"), keycode));
230     returnCode(has_key_internal(keycode, SP->_keytry));
231 }
232 #endif /* NCURSES_EXT_FUNCS */
233
234 /* Turn the keypad on/off
235  *
236  * Note:  we flush the output because changing this mode causes some terminals
237  * to emit different escape sequences for cursor and keypad keys.  If we don't
238  * flush, then the next wgetch may get the escape sequence that corresponds to
239  * the terminal state _before_ switching modes.
240  */
241 int _nc_keypad(bool flag)
242 {
243         if (flag  &&  keypad_xmit)
244         {
245             TPUTS_TRACE("keypad_xmit");
246             putp(keypad_xmit);
247             _nc_flush();
248         }
249         else if (! flag  &&  keypad_local)
250         {
251             TPUTS_TRACE("keypad_local");
252             putp(keypad_local);
253             _nc_flush();
254         }
255
256         if (flag && !SP->_tried) {
257             _nc_init_keytry();
258             SP->_tried = TRUE;
259         }
260         return(OK);
261 }