]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_options.c
ncurses 5.9 - patch 20130309
[ncurses.git] / ncurses / tinfo / lib_options.c
1 /****************************************************************************
2  * Copyright (c) 1998-2011,2013 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 #ifndef CUR
46 #define CUR SP_TERMTYPE
47 #endif
48
49 MODULE_ID("$Id: lib_options.c,v 1.74 2013/01/12 16:44:17 tom Exp $")
50
51 NCURSES_EXPORT(int)
52 idlok(WINDOW *win, bool flag)
53 {
54     int res = ERR;
55     T((T_CALLED("idlok(%p,%d)"), (void *) win, flag));
56
57     if (win) {
58         SCREEN *sp = _nc_screen_of(win);
59         if (sp && IsTermInfo(sp)) {
60             sp->_nc_sp_idlok =
61                 win->_idlok = (flag && (NCURSES_SP_NAME(has_il) (NCURSES_SP_ARG)
62                                         || change_scroll_region));
63             res = OK;
64         }
65     }
66     returnCode(res);
67 }
68
69 NCURSES_EXPORT(void)
70 idcok(WINDOW *win, bool flag)
71 {
72     T((T_CALLED("idcok(%p,%d)"), (void *) win, flag));
73
74     if (win) {
75         SCREEN *sp = _nc_screen_of(win);
76         sp->_nc_sp_idcok = win->_idcok = (flag && NCURSES_SP_NAME(has_ic) (NCURSES_SP_ARG));
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(%p,%d)"), (void *) SP_PARM, t));
85
86     if (t < 1 || t > 255 || !IsValidTIScreen(SP_PARM))
87         returnCode(ERR);
88
89     NCURSES_SP_NAME(cbreak) (NCURSES_SP_ARG);
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)"), (void *) 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)"), (void *) 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)"), (void *) 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)"), (void *) win, flag));
144
145     if (win) {
146         win->_use_keypad = flag;
147         returnCode(_nc_keypad(_nc_screen_of(win), flag));
148     } else
149         returnCode(ERR);
150 }
151
152 NCURSES_EXPORT(int)
153 meta(WINDOW *win GCC_UNUSED, bool flag)
154 {
155     int result = ERR;
156     SCREEN *sp = (win == 0) ? CURRENT_SCREEN : _nc_screen_of(win);
157
158     /* Ok, we stay relaxed and don't signal an error if win is NULL */
159     T((T_CALLED("meta(%p,%d)"), (void *) win, flag));
160
161     /* Ok, we stay relaxed and don't signal an error if win is NULL */
162
163     if (sp != 0) {
164         sp->_use_meta = flag;
165 #ifdef USE_TERM_DRIVER
166         if (IsTermInfo(sp)) {
167             if (flag) {
168                 NCURSES_PUTP2("meta_on", meta_on);
169             } else {
170                 NCURSES_PUTP2("meta_off", meta_off);
171             }
172         }
173 #else
174         if (flag) {
175             NCURSES_PUTP2("meta_on", meta_on);
176         } else {
177             NCURSES_PUTP2("meta_off", meta_off);
178         }
179 #endif
180         result = OK;
181     }
182     returnCode(result);
183 }
184
185 /* curs_set() moved here to narrow the kernel interface */
186
187 NCURSES_EXPORT(int)
188 NCURSES_SP_NAME(curs_set) (NCURSES_SP_DCLx int vis)
189 {
190     int code = ERR;
191     T((T_CALLED("curs_set(%p,%d)"), (void *) SP_PARM, vis));
192
193     if (SP_PARM != 0 && vis >= 0 && vis <= 2) {
194         int cursor = SP_PARM->_cursor;
195         bool bBuiltIn = !IsTermInfo(SP_PARM);
196         if (vis == cursor) {
197             code = cursor;
198         } else {
199             if (!bBuiltIn) {
200                 switch (vis) {
201                 case 2:
202                     code = NCURSES_PUTP2_FLUSH("cursor_visible",
203                                                cursor_visible);
204                     break;
205                 case 1:
206                     code = NCURSES_PUTP2_FLUSH("cursor_normal",
207                                                cursor_normal);
208                     break;
209                 case 0:
210                     code = NCURSES_PUTP2_FLUSH("cursor_invisible",
211                                                cursor_invisible);
212                     break;
213                 }
214             } else
215                 code = ERR;
216             if (code != ERR)
217                 code = (cursor == -1 ? 1 : cursor);
218             SP_PARM->_cursor = vis;
219         }
220     }
221     returnCode(code);
222 }
223
224 #if NCURSES_SP_FUNCS
225 NCURSES_EXPORT(int)
226 curs_set(int vis)
227 {
228     return (NCURSES_SP_NAME(curs_set) (CURRENT_SCREEN, vis));
229 }
230 #endif
231
232 NCURSES_EXPORT(int)
233 NCURSES_SP_NAME(typeahead) (NCURSES_SP_DCLx int fd)
234 {
235     T((T_CALLED("typeahead(%p, %d)"), (void *) SP_PARM, fd));
236     if (IsValidTIScreen(SP_PARM)) {
237         SP_PARM->_checkfd = fd;
238         returnCode(OK);
239     } else {
240         returnCode(ERR);
241     }
242 }
243
244 #if NCURSES_SP_FUNCS
245 NCURSES_EXPORT(int)
246 typeahead(int fd)
247 {
248     return NCURSES_SP_NAME(typeahead) (CURRENT_SCREEN, fd);
249 }
250 #endif
251
252 /*
253 **      has_key()
254 **
255 **      Return TRUE if the current terminal has the given key
256 **
257 */
258
259 #if NCURSES_EXT_FUNCS
260 static int
261 has_key_internal(int keycode, TRIES * tp)
262 {
263     if (tp == 0)
264         return (FALSE);
265     else if (tp->value == keycode)
266         return (TRUE);
267     else
268         return (has_key_internal(keycode, tp->child)
269                 || has_key_internal(keycode, tp->sibling));
270 }
271
272 #ifdef USE_TERM_DRIVER
273 NCURSES_EXPORT(int)
274 TINFO_HAS_KEY(SCREEN *sp, int keycode)
275 {
276     return IsValidTIScreen(sp) ?
277         has_key_internal(keycode, sp->_keytry) : 0;
278 }
279 #else
280 NCURSES_EXPORT(int)
281 NCURSES_SP_NAME(has_key) (NCURSES_SP_DCLx int keycode)
282 {
283     T((T_CALLED("has_key(%p,%d)"), (void *) SP_PARM, keycode));
284     returnCode(SP != 0 ? has_key_internal(keycode, SP_PARM->_keytry) : FALSE);
285 }
286
287 #if NCURSES_SP_FUNCS
288 NCURSES_EXPORT(int)
289 has_key(int keycode)
290 {
291     return NCURSES_SP_NAME(has_key) (CURRENT_SCREEN, keycode);
292 }
293 #endif
294 #endif
295 #endif /* NCURSES_EXT_FUNCS */
296
297 NCURSES_EXPORT(int)
298 NCURSES_SP_NAME(_nc_putp_flush) (NCURSES_SP_DCLx
299                                  const char *name, const char *value)
300 {
301     int rc = NCURSES_PUTP2(name, value);
302     if (rc != ERR) {
303         _nc_flush();
304     }
305     return rc;
306 }
307
308 #if 0 && NCURSES_SP_FUNCS
309 NCURSES_EXPORT(int)
310 _nc_putp_flush(const char *name, const char *value)
311 {
312     return NCURSES_SP_NAME(_nc_putp_flush) (CURRENT_SCREEN, name, value);
313 }
314 #endif
315
316 /* Turn the keypad on/off
317  *
318  * Note:  we flush the output because changing this mode causes some terminals
319  * to emit different escape sequences for cursor and keypad keys.  If we don't
320  * flush, then the next wgetch may get the escape sequence that corresponds to
321  * the terminal state _before_ switching modes.
322  */
323 NCURSES_EXPORT(int)
324 _nc_keypad(SCREEN *sp, int flag)
325 {
326     int rc = ERR;
327
328     if (sp != 0) {
329 #ifdef USE_PTHREADS
330         /*
331          * We might have this situation in a multithreaded application that
332          * has wgetch() reading in more than one thread.  putp() and below
333          * may use SP explicitly.
334          */
335         if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
336             SCREEN *save_sp;
337
338             /* cannot use use_screen(), since that is not in tinfo library */
339             _nc_lock_global(curses);
340             save_sp = CURRENT_SCREEN;
341             _nc_set_screen(sp);
342             rc = _nc_keypad(sp, flag);
343             _nc_set_screen(save_sp);
344             _nc_unlock_global(curses);
345         } else
346 #endif
347         {
348 #ifdef USE_TERM_DRIVER
349             rc = CallDriver_1(sp, kpad, flag);
350             if (rc == OK)
351                 sp->_keypad_on = flag;
352 #else
353             if (flag) {
354                 (void) NCURSES_PUTP2_FLUSH("keypad_xmit", keypad_xmit);
355             } else if (!flag && keypad_local) {
356                 (void) NCURSES_PUTP2_FLUSH("keypad_local", keypad_local);
357             }
358
359             if (flag && !sp->_tried) {
360                 _nc_init_keytry(sp);
361                 sp->_tried = TRUE;
362             }
363             sp->_keypad_on = flag;
364             rc = OK;
365 #endif
366         }
367     }
368     return (rc);
369 }