]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_ttyflags.c
ncurses 6.2 - patch 20200912
[ncurses.git] / ncurses / tinfo / lib_ttyflags.c
1 /****************************************************************************
2  * Copyright 2020 Thomas E. Dickey                                          *
3  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
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>
40
41 #ifndef CUR
42 #define CUR SP_TERMTYPE
43 #endif
44
45 MODULE_ID("$Id: lib_ttyflags.c,v 1.36 2020/09/05 22:54:47 tom Exp $")
46
47 NCURSES_EXPORT(int)
48 NCURSES_SP_NAME(_nc_get_tty_mode) (NCURSES_SP_DCLx TTY * buf)
49 {
50     TERMINAL *termp = TerminalOf(SP_PARM);
51     int result = OK;
52
53     if (buf == 0 || termp == 0) {
54         result = ERR;
55     } else {
56
57 #ifdef USE_TERM_DRIVER
58         if (SP_PARM != 0) {
59             result = CallDriver_2(SP_PARM, td_sgmode, FALSE, buf);
60         } else {
61             result = ERR;
62         }
63 #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
73
74         TR(TRACE_BITS, ("_nc_get_tty_mode(%d): %s",
75                         termp ? termp->Filedes : -1,
76                         _nc_trace_ttymode(buf)));
77     }
78     if (result == ERR && buf != 0)
79         memset(buf, 0, sizeof(*buf));
80
81     return (result);
82 }
83
84 #if NCURSES_SP_FUNCS
85 NCURSES_EXPORT(int)
86 _nc_get_tty_mode(TTY * buf)
87 {
88     return NCURSES_SP_NAME(_nc_get_tty_mode) (CURRENT_SCREEN, buf);
89 }
90 #endif
91
92 NCURSES_EXPORT(int)
93 NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_DCLx TTY * buf)
94 {
95     int result = OK;
96
97     if (buf == 0 || SP_PARM == 0) {
98         result = ERR;
99     } else {
100         TERMINAL *termp = TerminalOf(SP_PARM);
101
102         if (0 == termp) {
103             result = ERR;
104         } else {
105 #ifdef USE_TERM_DRIVER
106             result = CallDriver_2(SP_PARM, td_sgmode, TRUE, buf);
107 #else
108             for (;;) {
109                 if ((SET_TTY(termp->Filedes, buf) != 0)
110 #if USE_KLIBC_KBD
111                     && !NC_ISATTY(termp->Filedes)
112 #endif
113                     ) {
114                     if (errno == EINTR)
115                         continue;
116                     if ((errno == ENOTTY) && (SP_PARM != 0))
117                         SP_PARM->_notty = TRUE;
118                     result = ERR;
119                 }
120                 break;
121             }
122 #endif
123         }
124         TR(TRACE_BITS, ("_nc_set_tty_mode(%d): %s",
125                         termp ? termp->Filedes : -1,
126                         _nc_trace_ttymode(buf)));
127     }
128     return (result);
129 }
130
131 #if NCURSES_SP_FUNCS
132 NCURSES_EXPORT(int)
133 _nc_set_tty_mode(TTY * buf)
134 {
135     return NCURSES_SP_NAME(_nc_set_tty_mode) (CURRENT_SCREEN, buf);
136 }
137 #endif
138
139 NCURSES_EXPORT(int)
140 NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_DCL0)
141 {
142     int rc = ERR;
143     TERMINAL *termp = TerminalOf(SP_PARM);
144
145     T((T_CALLED("def_shell_mode(%p) ->term %p"),
146        (void *) SP_PARM, (void *) termp));
147
148     if (termp != 0) {
149 #ifdef USE_TERM_DRIVER
150         rc = CallDriver_2(SP_PARM, td_mode, FALSE, TRUE);
151 #else
152         /*
153          * If XTABS was on, remove the tab and backtab capabilities.
154          */
155         if (_nc_get_tty_mode(&termp->Ottyb) == OK) {
156 #ifdef TERMIOS
157             if (termp->Ottyb.c_oflag & OFLAGS_TABS)
158                 tab = back_tab = NULL;
159 #elif defined(EXP_WIN32_DRIVER)
160             /* noop */
161 #else
162             if (termp->Ottyb.sg_flags & XTABS)
163                 tab = back_tab = NULL;
164 #endif
165             rc = OK;
166         }
167 #endif
168     }
169     returnCode(rc);
170 }
171
172 #if NCURSES_SP_FUNCS
173 NCURSES_EXPORT(int)
174 def_shell_mode(void)
175 {
176     return NCURSES_SP_NAME(def_shell_mode) (CURRENT_SCREEN);
177 }
178 #endif
179
180 NCURSES_EXPORT(int)
181 NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_DCL0)
182 {
183     int rc = ERR;
184     TERMINAL *termp = TerminalOf(SP_PARM);
185
186     T((T_CALLED("def_prog_mode(%p) ->term %p"), (void *) SP_PARM, (void *) termp));
187
188     if (termp != 0) {
189 #ifdef USE_TERM_DRIVER
190         rc = CallDriver_2(SP_PARM, td_mode, TRUE, TRUE);
191 #else
192         /*
193          * Turn off the XTABS bit in the tty structure if it was on.
194          */
195         if (_nc_get_tty_mode(&termp->Nttyb) == OK) {
196 #ifdef TERMIOS
197             termp->Nttyb.c_oflag &= (unsigned) (~OFLAGS_TABS);
198 #elif defined(EXP_WIN32_DRIVER)
199             /* noop */
200 #else
201             termp->Nttyb.sg_flags &= (unsigned) (~XTABS);
202 #endif
203             rc = OK;
204         }
205 #endif
206     }
207     returnCode(rc);
208 }
209
210 #if NCURSES_SP_FUNCS
211 NCURSES_EXPORT(int)
212 def_prog_mode(void)
213 {
214     return NCURSES_SP_NAME(def_prog_mode) (CURRENT_SCREEN);
215 }
216 #endif
217
218 NCURSES_EXPORT(int)
219 NCURSES_SP_NAME(reset_prog_mode) (NCURSES_SP_DCL0)
220 {
221     int rc = ERR;
222     TERMINAL *termp = TerminalOf(SP_PARM);
223
224     T((T_CALLED("reset_prog_mode(%p) ->term %p"), (void *) SP_PARM, (void *) termp));
225
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
243 NCURSES_EXPORT(int)
244 reset_prog_mode(void)
245 {
246     return NCURSES_SP_NAME(reset_prog_mode) (CURRENT_SCREEN);
247 }
248 #endif
249
250 NCURSES_EXPORT(int)
251 NCURSES_SP_NAME(reset_shell_mode) (NCURSES_SP_DCL0)
252 {
253     int rc = ERR;
254     TERMINAL *termp = TerminalOf(SP_PARM);
255
256     T((T_CALLED("reset_shell_mode(%p) ->term %p"),
257        (void *) SP_PARM, (void *) termp));
258
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();
266         }
267         rc = _nc_set_tty_mode(&termp->Ottyb);
268 #endif
269     }
270     returnCode(rc);
271 }
272
273 #if NCURSES_SP_FUNCS
274 NCURSES_EXPORT(int)
275 reset_shell_mode(void)
276 {
277     return NCURSES_SP_NAME(reset_shell_mode) (CURRENT_SCREEN);
278 }
279 #endif
280
281 static TTY *
282 saved_tty(NCURSES_SP_DCL0)
283 {
284     TTY *result = 0;
285
286     if (SP_PARM != 0) {
287         result = (TTY *) & (SP_PARM->_saved_tty);
288     } 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
302 NCURSES_EXPORT(int)
303 NCURSES_SP_NAME(savetty) (NCURSES_SP_DCL0)
304 {
305     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)));
307 }
308
309 #if NCURSES_SP_FUNCS
310 NCURSES_EXPORT(int)
311 savetty(void)
312 {
313     return NCURSES_SP_NAME(savetty) (CURRENT_SCREEN);
314 }
315 #endif
316
317 NCURSES_EXPORT(int)
318 NCURSES_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
325 NCURSES_EXPORT(int)
326 resetty(void)
327 {
328     return NCURSES_SP_NAME(resetty) (CURRENT_SCREEN);
329 }
330 #endif