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