blob: 6363a805be9c44a038aa20b87f1d414b1e487c30 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2020 Thomas E. Dickey *
3 * Copyright 1998-2016,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 * def_prog_mode()
32 * def_shell_mode()
33 * reset_prog_mode()
34 * reset_shell_mode()
35 * savetty()
36 * resetty()
37 */
38
39#include <curses.priv.h>
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053040
Steve Kondikae271bc2015-11-15 02:50:53 +010041#ifndef CUR
42#define CUR SP_TERMTYPE
43#endif
44
micky3879b9f5e72025-07-08 18:04:53 -040045MODULE_ID("$Id: lib_ttyflags.c,v 1.36 2020/09/05 22:54:47 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053046
47NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +010048NCURSES_SP_NAME(_nc_get_tty_mode) (NCURSES_SP_DCLx TTY * buf)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053049{
micky3879b9f5e72025-07-08 18:04:53 -040050 TERMINAL *termp = TerminalOf(SP_PARM);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053051 int result = OK;
52
micky3879b9f5e72025-07-08 18:04:53 -040053 if (buf == 0 || termp == 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053054 result = ERR;
55 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +010056
Steve Kondikae271bc2015-11-15 02:50:53 +010057#ifdef USE_TERM_DRIVER
micky3879b9f5e72025-07-08 18:04:53 -040058 if (SP_PARM != 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +010059 result = CallDriver_2(SP_PARM, td_sgmode, FALSE, buf);
micky3879b9f5e72025-07-08 18:04:53 -040060 } else {
61 result = ERR;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053062 }
micky3879b9f5e72025-07-08 18:04:53 -040063#else
64 for (;;) {
65 if (GET_TTY(termp->Filedes, buf) != 0) {
66 if (errno == EINTR)
67 continue;
68 result = ERR;
69 }
70 break;
71 }
72#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053073
74 TR(TRACE_BITS, ("_nc_get_tty_mode(%d): %s",
Steve Kondikae271bc2015-11-15 02:50:53 +010075 termp ? termp->Filedes : -1,
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053076 _nc_trace_ttymode(buf)));
77 }
micky3879b9f5e72025-07-08 18:04:53 -040078 if (result == ERR && buf != 0)
79 memset(buf, 0, sizeof(*buf));
80
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053081 return (result);
82}
83
Steve Kondikae271bc2015-11-15 02:50:53 +010084#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053085NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +010086_nc_get_tty_mode(TTY * buf)
87{
88 return NCURSES_SP_NAME(_nc_get_tty_mode) (CURRENT_SCREEN, buf);
89}
90#endif
91
92NCURSES_EXPORT(int)
93NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_DCLx TTY * buf)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053094{
95 int result = OK;
96
Steve Kondikae271bc2015-11-15 02:50:53 +010097 if (buf == 0 || SP_PARM == 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053098 result = ERR;
99 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +0100100 TERMINAL *termp = TerminalOf(SP_PARM);
101
102 if (0 == termp) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530103 result = ERR;
104 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +0100105#ifdef USE_TERM_DRIVER
106 result = CallDriver_2(SP_PARM, td_sgmode, TRUE, buf);
107#else
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530108 for (;;) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100109 if ((SET_TTY(termp->Filedes, buf) != 0)
110#if USE_KLIBC_KBD
111 && !NC_ISATTY(termp->Filedes)
112#endif
113 ) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530114 if (errno == EINTR)
115 continue;
Steve Kondikae271bc2015-11-15 02:50:53 +0100116 if ((errno == ENOTTY) && (SP_PARM != 0))
117 SP_PARM->_notty = TRUE;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530118 result = ERR;
119 }
120 break;
121 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100122#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530123 }
124 TR(TRACE_BITS, ("_nc_set_tty_mode(%d): %s",
Steve Kondikae271bc2015-11-15 02:50:53 +0100125 termp ? termp->Filedes : -1,
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530126 _nc_trace_ttymode(buf)));
127 }
128 return (result);
129}
130
Steve Kondikae271bc2015-11-15 02:50:53 +0100131#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530132NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +0100133_nc_set_tty_mode(TTY * buf)
134{
135 return NCURSES_SP_NAME(_nc_set_tty_mode) (CURRENT_SCREEN, buf);
136}
137#endif
138
139NCURSES_EXPORT(int)
140NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_DCL0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530141{
142 int rc = ERR;
Steve Kondikae271bc2015-11-15 02:50:53 +0100143 TERMINAL *termp = TerminalOf(SP_PARM);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530144
micky3879b9f5e72025-07-08 18:04:53 -0400145 T((T_CALLED("def_shell_mode(%p) ->term %p"),
146 (void *) SP_PARM, (void *) termp));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530147
Steve Kondikae271bc2015-11-15 02:50:53 +0100148 if (termp != 0) {
149#ifdef USE_TERM_DRIVER
150 rc = CallDriver_2(SP_PARM, td_mode, FALSE, TRUE);
151#else
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530152 /*
153 * If XTABS was on, remove the tab and backtab capabilities.
154 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100155 if (_nc_get_tty_mode(&termp->Ottyb) == OK) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530156#ifdef TERMIOS
Steve Kondikae271bc2015-11-15 02:50:53 +0100157 if (termp->Ottyb.c_oflag & OFLAGS_TABS)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530158 tab = back_tab = NULL;
micky3879b9f5e72025-07-08 18:04:53 -0400159#elif defined(EXP_WIN32_DRIVER)
160 /* noop */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530161#else
Steve Kondikae271bc2015-11-15 02:50:53 +0100162 if (termp->Ottyb.sg_flags & XTABS)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530163 tab = back_tab = NULL;
164#endif
165 rc = OK;
166 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100167#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530168 }
169 returnCode(rc);
170}
171
Steve Kondikae271bc2015-11-15 02:50:53 +0100172#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530173NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +0100174def_shell_mode(void)
175{
176 return NCURSES_SP_NAME(def_shell_mode) (CURRENT_SCREEN);
177}
178#endif
179
180NCURSES_EXPORT(int)
181NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_DCL0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530182{
183 int rc = ERR;
Steve Kondikae271bc2015-11-15 02:50:53 +0100184 TERMINAL *termp = TerminalOf(SP_PARM);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530185
micky3879b9f5e72025-07-08 18:04:53 -0400186 T((T_CALLED("def_prog_mode(%p) ->term %p"), (void *) SP_PARM, (void *) termp));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530187
Steve Kondikae271bc2015-11-15 02:50:53 +0100188 if (termp != 0) {
189#ifdef USE_TERM_DRIVER
190 rc = CallDriver_2(SP_PARM, td_mode, TRUE, TRUE);
191#else
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530192 /*
193 * Turn off the XTABS bit in the tty structure if it was on.
194 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100195 if (_nc_get_tty_mode(&termp->Nttyb) == OK) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530196#ifdef TERMIOS
Steve Kondikae271bc2015-11-15 02:50:53 +0100197 termp->Nttyb.c_oflag &= (unsigned) (~OFLAGS_TABS);
micky3879b9f5e72025-07-08 18:04:53 -0400198#elif defined(EXP_WIN32_DRIVER)
199 /* noop */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530200#else
Steve Kondikae271bc2015-11-15 02:50:53 +0100201 termp->Nttyb.sg_flags &= (unsigned) (~XTABS);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530202#endif
203 rc = OK;
204 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100205#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530206 }
207 returnCode(rc);
208}
209
Steve Kondikae271bc2015-11-15 02:50:53 +0100210#if NCURSES_SP_FUNCS
211NCURSES_EXPORT(int)
212def_prog_mode(void)
213{
214 return NCURSES_SP_NAME(def_prog_mode) (CURRENT_SCREEN);
215}
216#endif
217
218NCURSES_EXPORT(int)
219NCURSES_SP_NAME(reset_prog_mode) (NCURSES_SP_DCL0)
220{
221 int rc = ERR;
222 TERMINAL *termp = TerminalOf(SP_PARM);
223
micky3879b9f5e72025-07-08 18:04:53 -0400224 T((T_CALLED("reset_prog_mode(%p) ->term %p"), (void *) SP_PARM, (void *) termp));
Steve Kondikae271bc2015-11-15 02:50:53 +0100225
226 if (termp != 0) {
227#ifdef USE_TERM_DRIVER
228 rc = CallDriver_2(SP_PARM, td_mode, TRUE, FALSE);
229#else
230 if (_nc_set_tty_mode(&termp->Nttyb) == OK) {
231 if (SP_PARM) {
232 if (SP_PARM->_keypad_on)
233 _nc_keypad(SP_PARM, TRUE);
234 }
235 rc = OK;
236 }
237#endif
238 }
239 returnCode(rc);
240}
241
242#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530243NCURSES_EXPORT(int)
244reset_prog_mode(void)
245{
Steve Kondikae271bc2015-11-15 02:50:53 +0100246 return NCURSES_SP_NAME(reset_prog_mode) (CURRENT_SCREEN);
247}
248#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530249
Steve Kondikae271bc2015-11-15 02:50:53 +0100250NCURSES_EXPORT(int)
251NCURSES_SP_NAME(reset_shell_mode) (NCURSES_SP_DCL0)
252{
253 int rc = ERR;
254 TERMINAL *termp = TerminalOf(SP_PARM);
255
micky3879b9f5e72025-07-08 18:04:53 -0400256 T((T_CALLED("reset_shell_mode(%p) ->term %p"),
257 (void *) SP_PARM, (void *) termp));
Steve Kondikae271bc2015-11-15 02:50:53 +0100258
259 if (termp != 0) {
260#ifdef USE_TERM_DRIVER
261 rc = CallDriver_2(SP_PARM, td_mode, FALSE, FALSE);
262#else
263 if (SP_PARM) {
264 _nc_keypad(SP_PARM, FALSE);
265 _nc_flush();
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530266 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100267 rc = _nc_set_tty_mode(&termp->Ottyb);
268#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530269 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100270 returnCode(rc);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530271}
272
Steve Kondikae271bc2015-11-15 02:50:53 +0100273#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530274NCURSES_EXPORT(int)
275reset_shell_mode(void)
276{
Steve Kondikae271bc2015-11-15 02:50:53 +0100277 return NCURSES_SP_NAME(reset_shell_mode) (CURRENT_SCREEN);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530278}
Steve Kondikae271bc2015-11-15 02:50:53 +0100279#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530280
281static TTY *
Steve Kondikae271bc2015-11-15 02:50:53 +0100282saved_tty(NCURSES_SP_DCL0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530283{
284 TTY *result = 0;
285
Steve Kondikae271bc2015-11-15 02:50:53 +0100286 if (SP_PARM != 0) {
287 result = (TTY *) & (SP_PARM->_saved_tty);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530288 } else {
289 if (_nc_prescreen.saved_tty == 0) {
290 _nc_prescreen.saved_tty = typeCalloc(TTY, 1);
291 }
292 result = _nc_prescreen.saved_tty;
293 }
294 return result;
295}
296
297/*
298** savetty() and resetty()
299**
300*/
301
302NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +0100303NCURSES_SP_NAME(savetty) (NCURSES_SP_DCL0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530304{
Steve Kondikae271bc2015-11-15 02:50:53 +0100305 T((T_CALLED("savetty(%p)"), (void *) SP_PARM));
306 returnCode(NCURSES_SP_NAME(_nc_get_tty_mode) (NCURSES_SP_ARGx saved_tty(NCURSES_SP_ARG)));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530307}
308
Steve Kondikae271bc2015-11-15 02:50:53 +0100309#if NCURSES_SP_FUNCS
310NCURSES_EXPORT(int)
311savetty(void)
312{
313 return NCURSES_SP_NAME(savetty) (CURRENT_SCREEN);
314}
315#endif
316
317NCURSES_EXPORT(int)
318NCURSES_SP_NAME(resetty) (NCURSES_SP_DCL0)
319{
320 T((T_CALLED("resetty(%p)"), (void *) SP_PARM));
321 returnCode(NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx saved_tty(NCURSES_SP_ARG)));
322}
323
324#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530325NCURSES_EXPORT(int)
326resetty(void)
327{
Steve Kondikae271bc2015-11-15 02:50:53 +0100328 return NCURSES_SP_NAME(resetty) (CURRENT_SCREEN);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530329}
Steve Kondikae271bc2015-11-15 02:50:53 +0100330#endif