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 <[email protected]> 1992,1995 *
31 * and: Eric S. Raymond <[email protected]> *
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.76 2013/12/14 22:23:58 tom Exp $")
50
NCURSES_EXPORT(int)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 != 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);
71 }
72
73 NCURSES_EXPORT(void)
idcok(WINDOW * win,bool flag)74 idcok(WINDOW *win, bool flag)
75 {
76 T((T_CALLED("idcok(%p,%d)"), (void *) win, flag));
77
78 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 }
82 returnVoid;
83 }
84
85 NCURSES_EXPORT(int)
NCURSES_SP_NAME(halfdelay)86 NCURSES_SP_NAME(halfdelay) (NCURSES_SP_DCLx int t)
87 {
88 T((T_CALLED("halfdelay(%p,%d)"), (void *) SP_PARM, t));
89
90 if (t < 1 || t > 255 || !IsValidTIScreen(SP_PARM))
91 returnCode(ERR);
92
93 NCURSES_SP_NAME(cbreak) (NCURSES_SP_ARG);
94 SP_PARM->_cbreak = t + 1;
95 returnCode(OK);
96 }
97
98 #if NCURSES_SP_FUNCS
99 NCURSES_EXPORT(int)
halfdelay(int t)100 halfdelay(int t)
101 {
102 return NCURSES_SP_NAME(halfdelay) (CURRENT_SCREEN, t);
103 }
104 #endif
105
106 NCURSES_EXPORT(int)
nodelay(WINDOW * win,bool flag)107 nodelay(WINDOW *win, bool flag)
108 {
109 T((T_CALLED("nodelay(%p,%d)"), (void *) win, flag));
110
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
121 NCURSES_EXPORT(int)
notimeout(WINDOW * win,bool f)122 notimeout(WINDOW *win, bool f)
123 {
124 T((T_CALLED("notimeout(%p,%d)"), (void *) win, f));
125
126 if (win) {
127 win->_notimeout = f;
128 returnCode(OK);
129 } else
130 returnCode(ERR);
131 }
132
133 NCURSES_EXPORT(void)
wtimeout(WINDOW * win,int delay)134 wtimeout(WINDOW *win, int delay)
135 {
136 T((T_CALLED("wtimeout(%p,%d)"), (void *) win, delay));
137
138 if (win) {
139 win->_delay = delay;
140 }
141 returnVoid;
142 }
143
144 NCURSES_EXPORT(int)
keypad(WINDOW * win,bool flag)145 keypad(WINDOW *win, bool flag)
146 {
147 T((T_CALLED("keypad(%p,%d)"), (void *) win, flag));
148
149 if (win) {
150 win->_use_keypad = flag;
151 returnCode(_nc_keypad(_nc_screen_of(win), flag));
152 } else
153 returnCode(ERR);
154 }
155
156 NCURSES_EXPORT(int)
meta(WINDOW * win GCC_UNUSED,bool flag)157 meta(WINDOW *win GCC_UNUSED, bool flag)
158 {
159 int result = ERR;
160 SCREEN *sp = (win == 0) ? CURRENT_SCREEN : _nc_screen_of(win);
161
162 /* Ok, we stay relaxed and don't signal an error if win is NULL */
163 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 }
186 returnCode(result);
187 }
188
189 /* curs_set() moved here to narrow the kernel interface */
190
191 NCURSES_EXPORT(int)
NCURSES_SP_NAME(curs_set)192 NCURSES_SP_NAME(curs_set) (NCURSES_SP_DCLx int vis)
193 {
194 int code = ERR;
195 T((T_CALLED("curs_set(%p,%d)"), (void *) SP_PARM, vis));
196
197 if (SP_PARM != 0 && vis >= 0 && vis <= 2) {
198 int cursor = SP_PARM->_cursor;
199 bool bBuiltIn = !IsTermInfo(SP_PARM);
200 if (vis == cursor) {
201 code = cursor;
202 } else {
203 if (!bBuiltIn) {
204 switch (vis) {
205 case 2:
206 code = NCURSES_PUTP2_FLUSH("cursor_visible",
207 cursor_visible);
208 break;
209 case 1:
210 code = NCURSES_PUTP2_FLUSH("cursor_normal",
211 cursor_normal);
212 break;
213 case 0:
214 code = NCURSES_PUTP2_FLUSH("cursor_invisible",
215 cursor_invisible);
216 break;
217 }
218 } else
219 code = ERR;
220 if (code != ERR)
221 code = (cursor == -1 ? 1 : cursor);
222 SP_PARM->_cursor = vis;
223 }
224 }
225 returnCode(code);
226 }
227
228 #if NCURSES_SP_FUNCS
229 NCURSES_EXPORT(int)
curs_set(int vis)230 curs_set(int vis)
231 {
232 return (NCURSES_SP_NAME(curs_set) (CURRENT_SCREEN, vis));
233 }
234 #endif
235
236 NCURSES_EXPORT(int)
NCURSES_SP_NAME(typeahead)237 NCURSES_SP_NAME(typeahead) (NCURSES_SP_DCLx int fd)
238 {
239 T((T_CALLED("typeahead(%p, %d)"), (void *) SP_PARM, fd));
240 if (IsValidTIScreen(SP_PARM)) {
241 SP_PARM->_checkfd = fd;
242 returnCode(OK);
243 } else {
244 returnCode(ERR);
245 }
246 }
247
248 #if NCURSES_SP_FUNCS
249 NCURSES_EXPORT(int)
typeahead(int fd)250 typeahead(int fd)
251 {
252 return NCURSES_SP_NAME(typeahead) (CURRENT_SCREEN, fd);
253 }
254 #endif
255
256 /*
257 ** has_key()
258 **
259 ** Return TRUE if the current terminal has the given key
260 **
261 */
262
263 #if NCURSES_EXT_FUNCS
264 static int
has_key_internal(int keycode,TRIES * tp)265 has_key_internal(int keycode, TRIES * tp)
266 {
267 if (tp == 0)
268 return (FALSE);
269 else if (tp->value == keycode)
270 return (TRUE);
271 else
272 return (has_key_internal(keycode, tp->child)
273 || has_key_internal(keycode, tp->sibling));
274 }
275
276 #ifdef USE_TERM_DRIVER
277 NCURSES_EXPORT(int)
TINFO_HAS_KEY(SCREEN * sp,int keycode)278 TINFO_HAS_KEY(SCREEN *sp, int keycode)
279 {
280 return IsValidTIScreen(sp) ?
281 has_key_internal(keycode, sp->_keytry) : 0;
282 }
283 #else
284 NCURSES_EXPORT(int)
NCURSES_SP_NAME(has_key)285 NCURSES_SP_NAME(has_key) (NCURSES_SP_DCLx int keycode)
286 {
287 T((T_CALLED("has_key(%p,%d)"), (void *) SP_PARM, keycode));
288 returnCode(SP != 0 ? has_key_internal(keycode, SP_PARM->_keytry) : FALSE);
289 }
290
291 #if NCURSES_SP_FUNCS
292 NCURSES_EXPORT(int)
has_key(int keycode)293 has_key(int keycode)
294 {
295 return NCURSES_SP_NAME(has_key) (CURRENT_SCREEN, keycode);
296 }
297 #endif
298 #endif
299 #endif /* NCURSES_EXT_FUNCS */
300
301 NCURSES_EXPORT(int)
NCURSES_SP_NAME(_nc_putp_flush)302 NCURSES_SP_NAME(_nc_putp_flush) (NCURSES_SP_DCLx
303 const char *name, const char *value)
304 {
305 int rc = NCURSES_PUTP2(name, value);
306 if (rc != ERR) {
307 _nc_flush();
308 }
309 return rc;
310 }
311
312 #if 0 && NCURSES_SP_FUNCS
313 NCURSES_EXPORT(int)
314 _nc_putp_flush(const char *name, const char *value)
315 {
316 return NCURSES_SP_NAME(_nc_putp_flush) (CURRENT_SCREEN, name, value);
317 }
318 #endif
319
320 /* Turn the keypad on/off
321 *
322 * Note: we flush the output because changing this mode causes some terminals
323 * to emit different escape sequences for cursor and keypad keys. If we don't
324 * flush, then the next wgetch may get the escape sequence that corresponds to
325 * the terminal state _before_ switching modes.
326 */
327 NCURSES_EXPORT(int)
_nc_keypad(SCREEN * sp,int flag)328 _nc_keypad(SCREEN *sp, int flag)
329 {
330 int rc = ERR;
331
332 if (sp != 0) {
333 #ifdef USE_PTHREADS
334 /*
335 * We might have this situation in a multithreaded application that
336 * has wgetch() reading in more than one thread. putp() and below
337 * may use SP explicitly.
338 */
339 if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
340 SCREEN *save_sp;
341
342 /* cannot use use_screen(), since that is not in tinfo library */
343 _nc_lock_global(curses);
344 save_sp = CURRENT_SCREEN;
345 _nc_set_screen(sp);
346 rc = _nc_keypad(sp, flag);
347 _nc_set_screen(save_sp);
348 _nc_unlock_global(curses);
349 } else
350 #endif
351 {
352 #ifdef USE_TERM_DRIVER
353 rc = CallDriver_1(sp, kpad, flag);
354 if (rc == OK)
355 sp->_keypad_on = flag;
356 #else
357 if (flag) {
358 (void) NCURSES_PUTP2_FLUSH("keypad_xmit", keypad_xmit);
359 } else if (!flag && keypad_local) {
360 (void) NCURSES_PUTP2_FLUSH("keypad_local", keypad_local);
361 }
362
363 if (flag && !sp->_tried) {
364 _nc_init_keytry(sp);
365 sp->_tried = TRUE;
366 }
367 sp->_keypad_on = flag;
368 rc = OK;
369 #endif
370 }
371 }
372 return (rc);
373 }
374