]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_options.c
ncurses 5.7 - patch 20090221
[ncurses.git] / ncurses / tinfo / lib_options.c
1 /****************************************************************************
2  * Copyright (c) 1998-2008,2009 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  *     and: Juergen Pfeifer                         2009                    *
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("$Id: lib_options.c,v 1.59 2009/02/15 00:48:40 tom Exp $")
48
49 static int _nc_curs_set(SCREEN *, int);
50 static int _nc_meta(SCREEN *, bool);
51
52 NCURSES_EXPORT(int)
53 idlok(WINDOW *win, bool flag)
54 {
55     T((T_CALLED("idlok(%p,%d)"), win, flag));
56
57     if (win) {
58         _nc_idlok = win->_idlok = (flag && (has_il() || change_scroll_region));
59         returnCode(OK);
60     } else
61         returnCode(ERR);
62 }
63
64 NCURSES_EXPORT(void)
65 idcok(WINDOW *win, bool flag)
66 {
67     T((T_CALLED("idcok(%p,%d)"), win, flag));
68
69     if (win)
70         _nc_idcok = win->_idcok = (flag && has_ic());
71
72     returnVoid;
73 }
74
75 NCURSES_EXPORT(int)
76 NCURSES_SP_NAME(halfdelay) (NCURSES_SP_DCLx int t)
77 {
78     T((T_CALLED("halfdelay(%d)"), t));
79
80     if (t < 1 || t > 255 || SP_PARM == 0)
81         returnCode(ERR);
82
83     cbreak();
84     SP_PARM->_cbreak = t + 1;
85     returnCode(OK);
86 }
87
88 #if NCURSES_SP_FUNCS
89 NCURSES_EXPORT(int)
90 halfdelay(int t)
91 {
92     return NCURSES_SP_NAME(halfdelay) (CURRENT_SCREEN, t);
93 }
94 #endif
95
96 NCURSES_EXPORT(int)
97 nodelay(WINDOW *win, bool flag)
98 {
99     T((T_CALLED("nodelay(%p,%d)"), win, flag));
100
101     if (win) {
102         if (flag == TRUE)
103             win->_delay = 0;
104         else
105             win->_delay = -1;
106         returnCode(OK);
107     } else
108         returnCode(ERR);
109 }
110
111 NCURSES_EXPORT(int)
112 notimeout(WINDOW *win, bool f)
113 {
114     T((T_CALLED("notimeout(%p,%d)"), win, f));
115
116     if (win) {
117         win->_notimeout = f;
118         returnCode(OK);
119     } else
120         returnCode(ERR);
121 }
122
123 NCURSES_EXPORT(void)
124 wtimeout(WINDOW *win, int delay)
125 {
126     T((T_CALLED("wtimeout(%p,%d)"), win, delay));
127
128     if (win) {
129         win->_delay = delay;
130     }
131     returnVoid;
132 }
133
134 NCURSES_EXPORT(int)
135 keypad(WINDOW *win, bool flag)
136 {
137     T((T_CALLED("keypad(%p,%d)"), win, flag));
138
139     if (win) {
140         win->_use_keypad = flag;
141         returnCode(_nc_keypad(SP, flag));
142     } else
143         returnCode(ERR);
144 }
145
146 NCURSES_EXPORT(int)
147 meta(WINDOW *win GCC_UNUSED, bool flag)
148 {
149     int result;
150
151     /* Ok, we stay relaxed and don't signal an error if win is NULL */
152     T((T_CALLED("meta(%p,%d)"), win, flag));
153     result = _nc_meta(SP, flag);
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 result;
163
164     T((T_CALLED("curs_set(%d)"), vis));
165     result = _nc_curs_set(SP, vis);
166     returnCode(result);
167 }
168
169 NCURSES_EXPORT(int)
170 NCURSES_SP_NAME(typeahead) (NCURSES_SP_DCLx int fd)
171 {
172     T((T_CALLED("typeahead(%d)"), fd));
173     if (SP_PARM != 0) {
174         SP_PARM->_checkfd = fd;
175         returnCode(OK);
176     } else {
177         returnCode(ERR);
178     }
179 }
180
181 #if NCURSES_SP_FUNCS
182 NCURSES_EXPORT(int)
183 typeahead(int fd)
184 {
185     return NCURSES_SP_NAME(typeahead) (CURRENT_SCREEN, fd);
186 }
187 #endif
188
189 /*
190 **      has_key()
191 **
192 **      Return TRUE if the current terminal has the given key
193 **
194 */
195
196 #if NCURSES_EXT_FUNCS
197 static int
198 has_key_internal(int keycode, TRIES * tp)
199 {
200     if (tp == 0)
201         return (FALSE);
202     else if (tp->value == keycode)
203         return (TRUE);
204     else
205         return (has_key_internal(keycode, tp->child)
206                 || has_key_internal(keycode, tp->sibling));
207 }
208
209 NCURSES_EXPORT(int)
210 has_key(int keycode)
211 {
212     T((T_CALLED("has_key(%d)"), keycode));
213     returnCode(SP != 0 ? has_key_internal(keycode, SP->_keytry) : FALSE);
214 }
215 #endif /* NCURSES_EXT_FUNCS */
216
217 /*
218  * Internal entrypoints use SCREEN* parameter to obtain capabilities rather
219  * than cur_term.
220  */
221 #undef CUR
222 #define CUR (sp->_term)->type.
223
224 static int
225 _nc_putp(const char *name GCC_UNUSED, const char *value)
226 {
227     int rc = ERR;
228
229     if (value) {
230         TPUTS_TRACE(name);
231         rc = putp(value);
232     }
233     return rc;
234 }
235
236 static int
237 _nc_putp_flush(const char *name, const char *value)
238 {
239     int rc = _nc_putp(name, value);
240     if (rc != ERR) {
241         _nc_flush();
242     }
243     return rc;
244 }
245
246 /* Turn the keypad on/off
247  *
248  * Note:  we flush the output because changing this mode causes some terminals
249  * to emit different escape sequences for cursor and keypad keys.  If we don't
250  * flush, then the next wgetch may get the escape sequence that corresponds to
251  * the terminal state _before_ switching modes.
252  */
253 NCURSES_EXPORT(int)
254 _nc_keypad(SCREEN *sp, bool flag)
255 {
256     int rc = ERR;
257
258     if (sp != 0) {
259 #ifdef USE_PTHREADS
260         /*
261          * We might have this situation in a multithreaded application that
262          * has wgetch() reading in more than one thread.  putp() and below
263          * may use SP explicitly.
264          */
265         if (_nc_use_pthreads && sp != SP) {
266             SCREEN *save_sp;
267
268             /* cannot use use_screen(), since that is not in tinfo library */
269             _nc_lock_global(curses);
270             save_sp = SP;
271             _nc_set_screen(sp);
272             rc = _nc_keypad(sp, flag);
273             _nc_set_screen(save_sp);
274             _nc_unlock_global(curses);
275         } else
276 #endif
277         {
278             if (flag) {
279                 (void) _nc_putp_flush("keypad_xmit", keypad_xmit);
280             } else if (!flag && keypad_local) {
281                 (void) _nc_putp_flush("keypad_local", keypad_local);
282             }
283
284             if (flag && !sp->_tried) {
285                 _nc_init_keytry(sp);
286                 sp->_tried = TRUE;
287             }
288             sp->_keypad_on = flag;
289             rc = OK;
290         }
291     }
292     return (rc);
293 }
294
295 static int
296 _nc_curs_set(SCREEN *sp, int vis)
297 {
298     int result = ERR;
299
300     T((T_CALLED("curs_set(%d)"), vis));
301     if (sp != 0 && vis >= 0 && vis <= 2) {
302         int cursor = sp->_cursor;
303
304         if (vis == cursor) {
305             result = cursor;
306         } else {
307             switch (vis) {
308             case 2:
309                 result = _nc_putp_flush("cursor_visible", cursor_visible);
310                 break;
311             case 1:
312                 result = _nc_putp_flush("cursor_normal", cursor_normal);
313                 break;
314             case 0:
315                 result = _nc_putp_flush("cursor_invisible", cursor_invisible);
316                 break;
317             }
318             if (result != ERR)
319                 result = (cursor == -1 ? 1 : cursor);
320             sp->_cursor = vis;
321         }
322     }
323     returnCode(result);
324 }
325
326 static int
327 _nc_meta(SCREEN *sp, bool flag)
328 {
329     int result = ERR;
330
331     /* Ok, we stay relaxed and don't signal an error if win is NULL */
332
333     if (SP != 0) {
334         SP->_use_meta = flag;
335
336         if (flag) {
337             _nc_putp("meta_on", meta_on);
338         } else {
339             _nc_putp("meta_off", meta_off);
340         }
341         result = OK;
342     }
343     return result;
344 }