blob: b736d5fcab40c065840705d1705bec03863e9da3 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
Steve Kondikae271bc2015-11-15 02:50:53 +01002 * Copyright (c) 1998-2013,2014 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05303 * *
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 *
Steve Kondikae271bc2015-11-15 02:50:53 +010033 * and: Juergen Pfeifer 2009 *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053034 ****************************************************************************/
35
36/*
37** lib_options.c
38**
39** The routines to handle option setting.
40**
41*/
42
43#include <curses.priv.h>
44
Steve Kondikae271bc2015-11-15 02:50:53 +010045#ifndef CUR
46#define CUR SP_TERMTYPE
47#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053048
Steve Kondikae271bc2015-11-15 02:50:53 +010049MODULE_ID("$Id: lib_options.c,v 1.78 2014/09/27 21:55:24 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053050
51NCURSES_EXPORT(int)
52idlok(WINDOW *win, bool flag)
53{
Steve Kondikae271bc2015-11-15 02:50:53 +010054 int res = ERR;
55 T((T_CALLED("idlok(%p,%d)"), (void *) win, flag));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053056
57 if (win) {
Steve Kondikae271bc2015-11-15 02:50:53 +010058 SCREEN *sp = _nc_screen_of(win);
59 if (sp != 0
60#ifdef USE_TERM_DRIVER
61 && IsTermInfo(sp)
62#endif
63 ) {
64 sp->_nc_sp_idlok =
65 win->_idlok = (flag && (NCURSES_SP_NAME(has_il) (NCURSES_SP_ARG)
66 || change_scroll_region));
67 res = OK;
68 }
69 }
70 returnCode(res);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053071}
72
73NCURSES_EXPORT(void)
74idcok(WINDOW *win, bool flag)
75{
Steve Kondikae271bc2015-11-15 02:50:53 +010076 T((T_CALLED("idcok(%p,%d)"), (void *) win, flag));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053077
Steve Kondikae271bc2015-11-15 02:50:53 +010078 if (win) {
79 SCREEN *sp = _nc_screen_of(win);
80 sp->_nc_sp_idcok = win->_idcok = (flag && NCURSES_SP_NAME(has_ic) (NCURSES_SP_ARG));
81 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053082 returnVoid;
83}
84
85NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +010086NCURSES_SP_NAME(halfdelay) (NCURSES_SP_DCLx int t)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053087{
Steve Kondikae271bc2015-11-15 02:50:53 +010088 T((T_CALLED("halfdelay(%p,%d)"), (void *) SP_PARM, t));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053089
Steve Kondikae271bc2015-11-15 02:50:53 +010090 if (t < 1 || t > 255 || !IsValidTIScreen(SP_PARM))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053091 returnCode(ERR);
92
Steve Kondikae271bc2015-11-15 02:50:53 +010093 NCURSES_SP_NAME(cbreak) (NCURSES_SP_ARG);
94 SP_PARM->_cbreak = t + 1;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053095 returnCode(OK);
96}
97
Steve Kondikae271bc2015-11-15 02:50:53 +010098#if NCURSES_SP_FUNCS
99NCURSES_EXPORT(int)
100halfdelay(int t)
101{
102 return NCURSES_SP_NAME(halfdelay) (CURRENT_SCREEN, t);
103}
104#endif
105
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530106NCURSES_EXPORT(int)
107nodelay(WINDOW *win, bool flag)
108{
Steve Kondikae271bc2015-11-15 02:50:53 +0100109 T((T_CALLED("nodelay(%p,%d)"), (void *) win, flag));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530110
111 if (win) {
112 if (flag == TRUE)
113 win->_delay = 0;
114 else
115 win->_delay = -1;
116 returnCode(OK);
117 } else
118 returnCode(ERR);
119}
120
121NCURSES_EXPORT(int)
122notimeout(WINDOW *win, bool f)
123{
Steve Kondikae271bc2015-11-15 02:50:53 +0100124 T((T_CALLED("notimeout(%p,%d)"), (void *) win, f));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530125
126 if (win) {
127 win->_notimeout = f;
128 returnCode(OK);
129 } else
130 returnCode(ERR);
131}
132
133NCURSES_EXPORT(void)
134wtimeout(WINDOW *win, int delay)
135{
Steve Kondikae271bc2015-11-15 02:50:53 +0100136 T((T_CALLED("wtimeout(%p,%d)"), (void *) win, delay));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530137
138 if (win) {
139 win->_delay = delay;
140 }
141 returnVoid;
142}
143
144NCURSES_EXPORT(int)
145keypad(WINDOW *win, bool flag)
146{
Steve Kondikae271bc2015-11-15 02:50:53 +0100147 T((T_CALLED("keypad(%p,%d)"), (void *) win, flag));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530148
149 if (win) {
150 win->_use_keypad = flag;
Steve Kondikae271bc2015-11-15 02:50:53 +0100151 returnCode(_nc_keypad(_nc_screen_of(win), flag));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530152 } else
153 returnCode(ERR);
154}
155
156NCURSES_EXPORT(int)
157meta(WINDOW *win GCC_UNUSED, bool flag)
158{
Steve Kondikae271bc2015-11-15 02:50:53 +0100159 int result = ERR;
160 SCREEN *sp = (win == 0) ? CURRENT_SCREEN : _nc_screen_of(win);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530161
162 /* Ok, we stay relaxed and don't signal an error if win is NULL */
Steve Kondikae271bc2015-11-15 02:50:53 +0100163 T((T_CALLED("meta(%p,%d)"), (void *) win, flag));
164
165 /* Ok, we stay relaxed and don't signal an error if win is NULL */
166
167 if (sp != 0) {
168 sp->_use_meta = flag;
169#ifdef USE_TERM_DRIVER
170 if (IsTermInfo(sp)) {
171 if (flag) {
172 NCURSES_PUTP2("meta_on", meta_on);
173 } else {
174 NCURSES_PUTP2("meta_off", meta_off);
175 }
176 }
177#else
178 if (flag) {
179 NCURSES_PUTP2("meta_on", meta_on);
180 } else {
181 NCURSES_PUTP2("meta_off", meta_off);
182 }
183#endif
184 result = OK;
185 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530186 returnCode(result);
187}
188
189/* curs_set() moved here to narrow the kernel interface */
190
191NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +0100192NCURSES_SP_NAME(curs_set) (NCURSES_SP_DCLx int vis)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530193{
Steve Kondikae271bc2015-11-15 02:50:53 +0100194 int code = ERR;
195 T((T_CALLED("curs_set(%p,%d)"), (void *) SP_PARM, vis));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530196
Steve Kondikae271bc2015-11-15 02:50:53 +0100197 if (SP_PARM != 0 && vis >= 0 && vis <= 2) {
198 int cursor = SP_PARM->_cursor;
199 if (vis == cursor) {
200 code = cursor;
201 } else {
202#ifdef USE_TERM_DRIVER
203 code = CallDriver_1(SP_PARM, td_cursorSet, vis);
204#else
205 if (IsTermInfo(SP_PARM)) {
206 switch (vis) {
207 case 2:
208 code = NCURSES_PUTP2_FLUSH("cursor_visible",
209 cursor_visible);
210 break;
211 case 1:
212 code = NCURSES_PUTP2_FLUSH("cursor_normal",
213 cursor_normal);
214 break;
215 case 0:
216 code = NCURSES_PUTP2_FLUSH("cursor_invisible",
217 cursor_invisible);
218 break;
219 }
220 } else {
221 code = ERR;
222 }
223#endif
224 if (code != ERR)
225 code = (cursor == -1 ? 1 : cursor);
226 SP_PARM->_cursor = vis;
227 }
228 }
229 returnCode(code);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530230}
231
Steve Kondikae271bc2015-11-15 02:50:53 +0100232#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530233NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +0100234curs_set(int vis)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530235{
Steve Kondikae271bc2015-11-15 02:50:53 +0100236 return (NCURSES_SP_NAME(curs_set) (CURRENT_SCREEN, vis));
237}
238#endif
239
240NCURSES_EXPORT(int)
241NCURSES_SP_NAME(typeahead) (NCURSES_SP_DCLx int fd)
242{
243 T((T_CALLED("typeahead(%p, %d)"), (void *) SP_PARM, fd));
244 if (IsValidTIScreen(SP_PARM)) {
245 SP_PARM->_checkfd = fd;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530246 returnCode(OK);
247 } else {
248 returnCode(ERR);
249 }
250}
251
Steve Kondikae271bc2015-11-15 02:50:53 +0100252#if NCURSES_SP_FUNCS
253NCURSES_EXPORT(int)
254typeahead(int fd)
255{
256 return NCURSES_SP_NAME(typeahead) (CURRENT_SCREEN, fd);
257}
258#endif
259
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530260/*
261** has_key()
262**
263** Return TRUE if the current terminal has the given key
264**
265*/
266
267#if NCURSES_EXT_FUNCS
268static int
269has_key_internal(int keycode, TRIES * tp)
270{
271 if (tp == 0)
272 return (FALSE);
273 else if (tp->value == keycode)
274 return (TRUE);
275 else
276 return (has_key_internal(keycode, tp->child)
277 || has_key_internal(keycode, tp->sibling));
278}
279
Steve Kondikae271bc2015-11-15 02:50:53 +0100280#ifdef USE_TERM_DRIVER
281NCURSES_EXPORT(int)
282TINFO_HAS_KEY(SCREEN *sp, int keycode)
283{
284 return IsValidTIScreen(sp) ?
285 has_key_internal(keycode, sp->_keytry) : 0;
286}
287#else
288NCURSES_EXPORT(int)
289NCURSES_SP_NAME(has_key) (NCURSES_SP_DCLx int keycode)
290{
291 T((T_CALLED("has_key(%p,%d)"), (void *) SP_PARM, keycode));
292 returnCode(SP != 0 ? has_key_internal(keycode, SP_PARM->_keytry) : FALSE);
293}
294
295#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530296NCURSES_EXPORT(int)
297has_key(int keycode)
298{
Steve Kondikae271bc2015-11-15 02:50:53 +0100299 return NCURSES_SP_NAME(has_key) (CURRENT_SCREEN, keycode);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530300}
Steve Kondikae271bc2015-11-15 02:50:53 +0100301#endif
302#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530303#endif /* NCURSES_EXT_FUNCS */
304
Steve Kondikae271bc2015-11-15 02:50:53 +0100305NCURSES_EXPORT(int)
306NCURSES_SP_NAME(_nc_putp_flush) (NCURSES_SP_DCLx
307 const char *name, const char *value)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530308{
Steve Kondikae271bc2015-11-15 02:50:53 +0100309 int rc = NCURSES_PUTP2(name, value);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530310 if (rc != ERR) {
311 _nc_flush();
312 }
313 return rc;
314}
315
Steve Kondikae271bc2015-11-15 02:50:53 +0100316#if 0 && NCURSES_SP_FUNCS
317NCURSES_EXPORT(int)
318_nc_putp_flush(const char *name, const char *value)
319{
320 return NCURSES_SP_NAME(_nc_putp_flush) (CURRENT_SCREEN, name, value);
321}
322#endif
323
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530324/* Turn the keypad on/off
325 *
326 * Note: we flush the output because changing this mode causes some terminals
327 * to emit different escape sequences for cursor and keypad keys. If we don't
328 * flush, then the next wgetch may get the escape sequence that corresponds to
329 * the terminal state _before_ switching modes.
330 */
331NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +0100332_nc_keypad(SCREEN *sp, int flag)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530333{
334 int rc = ERR;
335
336 if (sp != 0) {
337#ifdef USE_PTHREADS
338 /*
339 * We might have this situation in a multithreaded application that
340 * has wgetch() reading in more than one thread. putp() and below
341 * may use SP explicitly.
342 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100343 if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530344 SCREEN *save_sp;
345
346 /* cannot use use_screen(), since that is not in tinfo library */
347 _nc_lock_global(curses);
Steve Kondikae271bc2015-11-15 02:50:53 +0100348 save_sp = CURRENT_SCREEN;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530349 _nc_set_screen(sp);
350 rc = _nc_keypad(sp, flag);
351 _nc_set_screen(save_sp);
352 _nc_unlock_global(curses);
353 } else
354#endif
355 {
Steve Kondikae271bc2015-11-15 02:50:53 +0100356#ifdef USE_TERM_DRIVER
357 rc = CallDriver_1(sp, td_kpad, flag);
358 if (rc == OK)
359 sp->_keypad_on = flag;
360#else
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530361 if (flag) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100362 (void) NCURSES_PUTP2_FLUSH("keypad_xmit", keypad_xmit);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530363 } else if (!flag && keypad_local) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100364 (void) NCURSES_PUTP2_FLUSH("keypad_local", keypad_local);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530365 }
366
367 if (flag && !sp->_tried) {
368 _nc_init_keytry(sp);
369 sp->_tried = TRUE;
370 }
371 sp->_keypad_on = flag;
372 rc = OK;
Steve Kondikae271bc2015-11-15 02:50:53 +0100373#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530374 }
375 }
376 return (rc);
377}