]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_options.c
ncurses 5.7 - patch 20090228
[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.60 2009/02/28 21:07:56 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 NCURSES_EXPORT(int)
225 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_DCLx
226                            const char *name GCC_UNUSED, const char *value)
227 {
228     int rc = ERR;
229
230     if (value) {
231         TPUTS_TRACE(name);
232         rc = putp(value);
233     }
234     return rc;
235 }
236
237 #if NCURSES_SP_FUNCS
238 NCURSES_EXPORT(int)
239 _nc_putp(const char *name, const char *value)
240 {
241     return NCURSES_SP_NAME(_nc_putp) (CURRENT_SCREEN, name, value);
242 }
243 #endif
244
245 NCURSES_EXPORT(int)
246 NCURSES_SP_NAME(_nc_putp_flush) (NCURSES_SP_DCLx
247                                  const char *name, const char *value)
248 {
249     int rc = _nc_putp(name, value);
250     if (rc != ERR) {
251         _nc_flush();
252     }
253     return rc;
254 }
255
256 #if NCURSES_SP_FUNCS
257 NCURSES_EXPORT(int)
258 _nc_putp_flush(const char *name, const char *value)
259 {
260     return NCURSES_SP_NAME(_nc_putp_flush) (CURRENT_SCREEN, name, value);
261 }
262 #endif
263
264 /* Turn the keypad on/off
265  *
266  * Note:  we flush the output because changing this mode causes some terminals
267  * to emit different escape sequences for cursor and keypad keys.  If we don't
268  * flush, then the next wgetch may get the escape sequence that corresponds to
269  * the terminal state _before_ switching modes.
270  */
271 NCURSES_EXPORT(int)
272 _nc_keypad(SCREEN *sp, bool flag)
273 {
274     int rc = ERR;
275
276     if (sp != 0) {
277 #ifdef USE_PTHREADS
278         /*
279          * We might have this situation in a multithreaded application that
280          * has wgetch() reading in more than one thread.  putp() and below
281          * may use SP explicitly.
282          */
283         if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
284             SCREEN *save_sp;
285
286             /* cannot use use_screen(), since that is not in tinfo library */
287             _nc_lock_global(curses);
288             save_sp = CURRENT_SCREEN;
289             _nc_set_screen(sp);
290             rc = _nc_keypad(sp, flag);
291             _nc_set_screen(save_sp);
292             _nc_unlock_global(curses);
293         } else
294 #endif
295         {
296             if (flag) {
297                 (void) _nc_putp_flush("keypad_xmit", keypad_xmit);
298             } else if (!flag && keypad_local) {
299                 (void) _nc_putp_flush("keypad_local", keypad_local);
300             }
301
302             if (flag && !sp->_tried) {
303                 _nc_init_keytry(sp);
304                 sp->_tried = TRUE;
305             }
306             sp->_keypad_on = flag;
307             rc = OK;
308         }
309     }
310     return (rc);
311 }
312
313 static int
314 _nc_curs_set(SCREEN *sp, int vis)
315 {
316     int result = ERR;
317
318     T((T_CALLED("curs_set(%d)"), vis));
319     if (sp != 0 && vis >= 0 && vis <= 2) {
320         int cursor = sp->_cursor;
321
322         if (vis == cursor) {
323             result = cursor;
324         } else {
325             switch (vis) {
326             case 2:
327                 result = _nc_putp_flush("cursor_visible", cursor_visible);
328                 break;
329             case 1:
330                 result = _nc_putp_flush("cursor_normal", cursor_normal);
331                 break;
332             case 0:
333                 result = _nc_putp_flush("cursor_invisible", cursor_invisible);
334                 break;
335             }
336             if (result != ERR)
337                 result = (cursor == -1 ? 1 : cursor);
338             sp->_cursor = vis;
339         }
340     }
341     returnCode(result);
342 }
343
344 static int
345 _nc_meta(SCREEN *sp, bool flag)
346 {
347     int result = ERR;
348
349     /* Ok, we stay relaxed and don't signal an error if win is NULL */
350
351     if (sp != 0) {
352         sp->_use_meta = flag;
353
354         if (flag) {
355             _nc_putp("meta_on", meta_on);
356         } else {
357             _nc_putp("meta_off", meta_off);
358         }
359         result = OK;
360     }
361     return result;
362 }