blob: 6a8bb39489ced25d38e6f97bef9a69b796df13aa [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2020-2021,2023 Thomas E. Dickey *
3 * Copyright 1998-2014,2017 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30/****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
Steve Kondikae271bc2015-11-15 02:50:53 +010034 * and: Juergen Pfeifer 2009 *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053035 ****************************************************************************/
36
37/*
38** lib_options.c
39**
40** The routines to handle option setting.
41**
42*/
43
44#include <curses.priv.h>
45
Steve Kondikae271bc2015-11-15 02:50:53 +010046#ifndef CUR
47#define CUR SP_TERMTYPE
48#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053049
micky3879b9f5e72025-07-08 18:04:53 -040050MODULE_ID("$Id: lib_options.c,v 1.83 2023/04/29 18:56:12 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053051
52NCURSES_EXPORT(int)
53idlok(WINDOW *win, bool flag)
54{
Steve Kondikae271bc2015-11-15 02:50:53 +010055 int res = ERR;
56 T((T_CALLED("idlok(%p,%d)"), (void *) win, flag));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053057
58 if (win) {
Steve Kondikae271bc2015-11-15 02:50:53 +010059 SCREEN *sp = _nc_screen_of(win);
60 if (sp != 0
61#ifdef USE_TERM_DRIVER
62 && IsTermInfo(sp)
63#endif
64 ) {
65 sp->_nc_sp_idlok =
66 win->_idlok = (flag && (NCURSES_SP_NAME(has_il) (NCURSES_SP_ARG)
67 || change_scroll_region));
68 res = OK;
69 }
70 }
71 returnCode(res);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053072}
73
74NCURSES_EXPORT(void)
75idcok(WINDOW *win, bool flag)
76{
Steve Kondikae271bc2015-11-15 02:50:53 +010077 T((T_CALLED("idcok(%p,%d)"), (void *) win, flag));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053078
Steve Kondikae271bc2015-11-15 02:50:53 +010079 if (win) {
80 SCREEN *sp = _nc_screen_of(win);
81 sp->_nc_sp_idcok = win->_idcok = (flag && NCURSES_SP_NAME(has_ic) (NCURSES_SP_ARG));
82 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053083 returnVoid;
84}
85
86NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +010087NCURSES_SP_NAME(halfdelay) (NCURSES_SP_DCLx int t)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053088{
Steve Kondikae271bc2015-11-15 02:50:53 +010089 T((T_CALLED("halfdelay(%p,%d)"), (void *) SP_PARM, t));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053090
micky3879b9f5e72025-07-08 18:04:53 -040091 if (t < 1 || t > 255 || !SP_PARM || !IsValidTIScreen(SP_PARM))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053092 returnCode(ERR);
93
Steve Kondikae271bc2015-11-15 02:50:53 +010094 NCURSES_SP_NAME(cbreak) (NCURSES_SP_ARG);
micky3879b9f5e72025-07-08 18:04:53 -040095 IsCbreak(SP_PARM) = t + 1;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053096 returnCode(OK);
97}
98
Steve Kondikae271bc2015-11-15 02:50:53 +010099#if NCURSES_SP_FUNCS
100NCURSES_EXPORT(int)
101halfdelay(int t)
102{
103 return NCURSES_SP_NAME(halfdelay) (CURRENT_SCREEN, t);
104}
105#endif
106
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530107NCURSES_EXPORT(int)
108nodelay(WINDOW *win, bool flag)
109{
Steve Kondikae271bc2015-11-15 02:50:53 +0100110 T((T_CALLED("nodelay(%p,%d)"), (void *) win, flag));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530111
112 if (win) {
113 if (flag == TRUE)
114 win->_delay = 0;
115 else
116 win->_delay = -1;
117 returnCode(OK);
118 } else
119 returnCode(ERR);
120}
121
122NCURSES_EXPORT(int)
123notimeout(WINDOW *win, bool f)
124{
Steve Kondikae271bc2015-11-15 02:50:53 +0100125 T((T_CALLED("notimeout(%p,%d)"), (void *) win, f));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530126
127 if (win) {
128 win->_notimeout = f;
129 returnCode(OK);
130 } else
131 returnCode(ERR);
132}
133
134NCURSES_EXPORT(void)
135wtimeout(WINDOW *win, int delay)
136{
Steve Kondikae271bc2015-11-15 02:50:53 +0100137 T((T_CALLED("wtimeout(%p,%d)"), (void *) win, delay));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530138
139 if (win) {
140 win->_delay = delay;
141 }
142 returnVoid;
143}
144
145NCURSES_EXPORT(int)
146keypad(WINDOW *win, bool flag)
147{
Steve Kondikae271bc2015-11-15 02:50:53 +0100148 T((T_CALLED("keypad(%p,%d)"), (void *) win, flag));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530149
150 if (win) {
151 win->_use_keypad = flag;
Steve Kondikae271bc2015-11-15 02:50:53 +0100152 returnCode(_nc_keypad(_nc_screen_of(win), flag));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530153 } else
154 returnCode(ERR);
155}
156
157NCURSES_EXPORT(int)
158meta(WINDOW *win GCC_UNUSED, bool flag)
159{
Steve Kondikae271bc2015-11-15 02:50:53 +0100160 int result = ERR;
161 SCREEN *sp = (win == 0) ? CURRENT_SCREEN : _nc_screen_of(win);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530162
163 /* Ok, we stay relaxed and don't signal an error if win is NULL */
Steve Kondikae271bc2015-11-15 02:50:53 +0100164 T((T_CALLED("meta(%p,%d)"), (void *) win, flag));
165
166 /* Ok, we stay relaxed and don't signal an error if win is NULL */
167
168 if (sp != 0) {
169 sp->_use_meta = flag;
170#ifdef USE_TERM_DRIVER
171 if (IsTermInfo(sp)) {
172 if (flag) {
173 NCURSES_PUTP2("meta_on", meta_on);
174 } else {
175 NCURSES_PUTP2("meta_off", meta_off);
176 }
177 }
178#else
179 if (flag) {
180 NCURSES_PUTP2("meta_on", meta_on);
181 } else {
182 NCURSES_PUTP2("meta_off", meta_off);
183 }
184#endif
185 result = OK;
186 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530187 returnCode(result);
188}
189
190/* curs_set() moved here to narrow the kernel interface */
191
192NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +0100193NCURSES_SP_NAME(curs_set) (NCURSES_SP_DCLx int vis)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530194{
Steve Kondikae271bc2015-11-15 02:50:53 +0100195 int code = ERR;
196 T((T_CALLED("curs_set(%p,%d)"), (void *) SP_PARM, vis));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530197
Steve Kondikae271bc2015-11-15 02:50:53 +0100198 if (SP_PARM != 0 && vis >= 0 && vis <= 2) {
199 int cursor = SP_PARM->_cursor;
200 if (vis == cursor) {
201 code = cursor;
202 } else {
203#ifdef USE_TERM_DRIVER
204 code = CallDriver_1(SP_PARM, td_cursorSet, vis);
205#else
micky3879b9f5e72025-07-08 18:04:53 -0400206 if (IsValidTIScreen(SP_PARM)) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100207 switch (vis) {
208 case 2:
209 code = NCURSES_PUTP2_FLUSH("cursor_visible",
210 cursor_visible);
211 break;
212 case 1:
213 code = NCURSES_PUTP2_FLUSH("cursor_normal",
214 cursor_normal);
215 break;
216 case 0:
217 code = NCURSES_PUTP2_FLUSH("cursor_invisible",
218 cursor_invisible);
219 break;
220 }
221 } else {
222 code = ERR;
223 }
224#endif
225 if (code != ERR)
226 code = (cursor == -1 ? 1 : cursor);
227 SP_PARM->_cursor = vis;
228 }
229 }
230 returnCode(code);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530231}
232
Steve Kondikae271bc2015-11-15 02:50:53 +0100233#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530234NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +0100235curs_set(int vis)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530236{
Steve Kondikae271bc2015-11-15 02:50:53 +0100237 return (NCURSES_SP_NAME(curs_set) (CURRENT_SCREEN, vis));
238}
239#endif
240
241NCURSES_EXPORT(int)
242NCURSES_SP_NAME(typeahead) (NCURSES_SP_DCLx int fd)
243{
244 T((T_CALLED("typeahead(%p, %d)"), (void *) SP_PARM, fd));
micky3879b9f5e72025-07-08 18:04:53 -0400245 if (SP_PARM && IsValidTIScreen(SP_PARM)) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100246 SP_PARM->_checkfd = fd;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530247 returnCode(OK);
248 } else {
249 returnCode(ERR);
250 }
251}
252
Steve Kondikae271bc2015-11-15 02:50:53 +0100253#if NCURSES_SP_FUNCS
254NCURSES_EXPORT(int)
255typeahead(int fd)
256{
257 return NCURSES_SP_NAME(typeahead) (CURRENT_SCREEN, fd);
258}
259#endif
260
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530261/*
262** has_key()
263**
264** Return TRUE if the current terminal has the given key
265**
266*/
267
268#if NCURSES_EXT_FUNCS
269static int
270has_key_internal(int keycode, TRIES * tp)
271{
272 if (tp == 0)
273 return (FALSE);
274 else if (tp->value == keycode)
275 return (TRUE);
276 else
277 return (has_key_internal(keycode, tp->child)
278 || has_key_internal(keycode, tp->sibling));
279}
280
Steve Kondikae271bc2015-11-15 02:50:53 +0100281#ifdef USE_TERM_DRIVER
282NCURSES_EXPORT(int)
283TINFO_HAS_KEY(SCREEN *sp, int keycode)
284{
285 return IsValidTIScreen(sp) ?
286 has_key_internal(keycode, sp->_keytry) : 0;
287}
288#else
289NCURSES_EXPORT(int)
290NCURSES_SP_NAME(has_key) (NCURSES_SP_DCLx int keycode)
291{
292 T((T_CALLED("has_key(%p,%d)"), (void *) SP_PARM, keycode));
293 returnCode(SP != 0 ? has_key_internal(keycode, SP_PARM->_keytry) : FALSE);
294}
295
296#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530297NCURSES_EXPORT(int)
298has_key(int keycode)
299{
Steve Kondikae271bc2015-11-15 02:50:53 +0100300 return NCURSES_SP_NAME(has_key) (CURRENT_SCREEN, keycode);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530301}
Steve Kondikae271bc2015-11-15 02:50:53 +0100302#endif
303#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530304#endif /* NCURSES_EXT_FUNCS */
305
Steve Kondikae271bc2015-11-15 02:50:53 +0100306NCURSES_EXPORT(int)
307NCURSES_SP_NAME(_nc_putp_flush) (NCURSES_SP_DCLx
308 const char *name, const char *value)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530309{
Steve Kondikae271bc2015-11-15 02:50:53 +0100310 int rc = NCURSES_PUTP2(name, value);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530311 if (rc != ERR) {
312 _nc_flush();
313 }
314 return rc;
315}
316
Steve Kondikae271bc2015-11-15 02:50:53 +0100317#if 0 && NCURSES_SP_FUNCS
318NCURSES_EXPORT(int)
319_nc_putp_flush(const char *name, const char *value)
320{
321 return NCURSES_SP_NAME(_nc_putp_flush) (CURRENT_SCREEN, name, value);
322}
323#endif
324
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530325/* Turn the keypad on/off
326 *
327 * Note: we flush the output because changing this mode causes some terminals
328 * to emit different escape sequences for cursor and keypad keys. If we don't
329 * flush, then the next wgetch may get the escape sequence that corresponds to
330 * the terminal state _before_ switching modes.
331 */
332NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +0100333_nc_keypad(SCREEN *sp, int flag)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530334{
335 int rc = ERR;
336
337 if (sp != 0) {
338#ifdef USE_PTHREADS
339 /*
340 * We might have this situation in a multithreaded application that
341 * has wgetch() reading in more than one thread. putp() and below
342 * may use SP explicitly.
343 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100344 if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530345 SCREEN *save_sp;
346
347 /* cannot use use_screen(), since that is not in tinfo library */
348 _nc_lock_global(curses);
Steve Kondikae271bc2015-11-15 02:50:53 +0100349 save_sp = CURRENT_SCREEN;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530350 _nc_set_screen(sp);
351 rc = _nc_keypad(sp, flag);
352 _nc_set_screen(save_sp);
353 _nc_unlock_global(curses);
354 } else
355#endif
356 {
Steve Kondikae271bc2015-11-15 02:50:53 +0100357#ifdef USE_TERM_DRIVER
358 rc = CallDriver_1(sp, td_kpad, flag);
359 if (rc == OK)
360 sp->_keypad_on = flag;
361#else
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530362 if (flag) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100363 (void) NCURSES_PUTP2_FLUSH("keypad_xmit", keypad_xmit);
micky3879b9f5e72025-07-08 18:04:53 -0400364 } else if (keypad_local) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100365 (void) NCURSES_PUTP2_FLUSH("keypad_local", keypad_local);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530366 }
367
368 if (flag && !sp->_tried) {
369 _nc_init_keytry(sp);
370 sp->_tried = TRUE;
371 }
372 sp->_keypad_on = flag;
373 rc = OK;
Steve Kondikae271bc2015-11-15 02:50:53 +0100374#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530375 }
376 }
377 return (rc);
378}