]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_ttyflags.c
ncurses 6.1 - patch 20191207
[ncurses.git] / ncurses / tinfo / lib_ttyflags.c
1 /****************************************************************************
2  * Copyright (c) 1998-2016,2017 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  *              def_prog_mode()
31  *              def_shell_mode()
32  *              reset_prog_mode()
33  *              reset_shell_mode()
34  *              savetty()
35  *              resetty()
36  */
37
38 #include <curses.priv.h>
39
40 #ifndef CUR
41 #define CUR SP_TERMTYPE
42 #endif
43
44 MODULE_ID("$Id: lib_ttyflags.c,v 1.33 2017/04/02 14:30:26 tom Exp $")
45
46 NCURSES_EXPORT(int)
47 NCURSES_SP_NAME(_nc_get_tty_mode) (NCURSES_SP_DCLx TTY * buf)
48 {
49     TERMINAL *termp = TerminalOf(SP_PARM);
50     int result = OK;
51
52     if (buf == 0 || termp == 0) {
53         result = ERR;
54     } else {
55
56 #ifdef USE_TERM_DRIVER
57         if (SP_PARM != 0) {
58             result = CallDriver_2(SP_PARM, td_sgmode, FALSE, buf);
59         } else {
60             result = ERR;
61         }
62 #else
63         for (;;) {
64             if (GET_TTY(termp->Filedes, buf) != 0) {
65                 if (errno == EINTR)
66                     continue;
67                 result = ERR;
68             }
69             break;
70         }
71 #endif
72
73         TR(TRACE_BITS, ("_nc_get_tty_mode(%d): %s",
74                         termp ? termp->Filedes : -1,
75                         _nc_trace_ttymode(buf)));
76     }
77     if (result == ERR && buf != 0)
78         memset(buf, 0, sizeof(*buf));
79
80     return (result);
81 }
82
83 #if NCURSES_SP_FUNCS
84 NCURSES_EXPORT(int)
85 _nc_get_tty_mode(TTY * buf)
86 {
87     return NCURSES_SP_NAME(_nc_get_tty_mode) (CURRENT_SCREEN, buf);
88 }
89 #endif
90
91 NCURSES_EXPORT(int)
92 NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_DCLx TTY * buf)
93 {
94     int result = OK;
95
96     if (buf == 0 || SP_PARM == 0) {
97         result = ERR;
98     } else {
99         TERMINAL *termp = TerminalOf(SP_PARM);
100
101         if (0 == termp) {
102             result = ERR;
103         } else {
104 #ifdef USE_TERM_DRIVER
105             result = CallDriver_2(SP_PARM, td_sgmode, TRUE, buf);
106 #else
107             for (;;) {
108                 if ((SET_TTY(termp->Filedes, buf) != 0)
109 #if USE_KLIBC_KBD
110                     && !NC_ISATTY(termp->Filedes)
111 #endif
112                     ) {
113                     if (errno == EINTR)
114                         continue;
115                     if ((errno == ENOTTY) && (SP_PARM != 0))
116                         SP_PARM->_notty = TRUE;
117                     result = ERR;
118                 }
119                 break;
120             }
121 #endif
122         }
123         TR(TRACE_BITS, ("_nc_set_tty_mode(%d): %s",
124                         termp ? termp->Filedes : -1,
125                         _nc_trace_ttymode(buf)));
126     }
127     return (result);
128 }
129
130 #if NCURSES_SP_FUNCS
131 NCURSES_EXPORT(int)
132 _nc_set_tty_mode(TTY * buf)
133 {
134     return NCURSES_SP_NAME(_nc_set_tty_mode) (CURRENT_SCREEN, buf);
135 }
136 #endif
137
138 NCURSES_EXPORT(int)
139 NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_DCL0)
140 {
141     int rc = ERR;
142     TERMINAL *termp = TerminalOf(SP_PARM);
143
144     T((T_CALLED("def_shell_mode(%p) ->term %p"),
145        (void *) SP_PARM, (void *) termp));
146
147     if (termp != 0) {
148 #ifdef USE_TERM_DRIVER
149         rc = CallDriver_2(SP_PARM, td_mode, FALSE, TRUE);
150 #else
151         /*
152          * If XTABS was on, remove the tab and backtab capabilities.
153          */
154         if (_nc_get_tty_mode(&termp->Ottyb) == OK) {
155 #ifdef TERMIOS
156             if (termp->Ottyb.c_oflag & OFLAGS_TABS)
157                 tab = back_tab = NULL;
158 #else
159             if (termp->Ottyb.sg_flags & XTABS)
160                 tab = back_tab = NULL;
161 #endif
162             rc = OK;
163         }
164 #endif
165     }
166     returnCode(rc);
167 }
168
169 #if NCURSES_SP_FUNCS
170 NCURSES_EXPORT(int)
171 def_shell_mode(void)
172 {
173     return NCURSES_SP_NAME(def_shell_mode) (CURRENT_SCREEN);
174 }
175 #endif
176
177 NCURSES_EXPORT(int)
178 NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_DCL0)
179 {
180     int rc = ERR;
181     TERMINAL *termp = TerminalOf(SP_PARM);
182
183     T((T_CALLED("def_prog_mode(%p) ->term %p"), (void *) SP_PARM, (void *) termp));
184
185     if (termp != 0) {
186 #ifdef USE_TERM_DRIVER
187         rc = CallDriver_2(SP_PARM, td_mode, TRUE, TRUE);
188 #else
189         /*
190          * Turn off the XTABS bit in the tty structure if it was on.
191          */
192         if (_nc_get_tty_mode(&termp->Nttyb) == OK) {
193 #ifdef TERMIOS
194             termp->Nttyb.c_oflag &= (unsigned) (~OFLAGS_TABS);
195 #else
196             termp->Nttyb.sg_flags &= (unsigned) (~XTABS);
197 #endif
198             rc = OK;
199         }
200 #endif
201     }
202     returnCode(rc);
203 }
204
205 #if NCURSES_SP_FUNCS
206 NCURSES_EXPORT(int)
207 def_prog_mode(void)
208 {
209     return NCURSES_SP_NAME(def_prog_mode) (CURRENT_SCREEN);
210 }
211 #endif
212
213 NCURSES_EXPORT(int)
214 NCURSES_SP_NAME(reset_prog_mode) (NCURSES_SP_DCL0)
215 {
216     int rc = ERR;
217     TERMINAL *termp = TerminalOf(SP_PARM);
218
219     T((T_CALLED("reset_prog_mode(%p) ->term %p"), (void *) SP_PARM, (void *) termp));
220
221     if (termp != 0) {
222 #ifdef USE_TERM_DRIVER
223         rc = CallDriver_2(SP_PARM, td_mode, TRUE, FALSE);
224 #else
225         if (_nc_set_tty_mode(&termp->Nttyb) == OK) {
226             if (SP_PARM) {
227                 if (SP_PARM->_keypad_on)
228                     _nc_keypad(SP_PARM, TRUE);
229             }
230             rc = OK;
231         }
232 #endif
233     }
234     returnCode(rc);
235 }
236
237 #if NCURSES_SP_FUNCS
238 NCURSES_EXPORT(int)
239 reset_prog_mode(void)
240 {
241     return NCURSES_SP_NAME(reset_prog_mode) (CURRENT_SCREEN);
242 }
243 #endif
244
245 NCURSES_EXPORT(int)
246 NCURSES_SP_NAME(reset_shell_mode) (NCURSES_SP_DCL0)
247 {
248     int rc = ERR;
249     TERMINAL *termp = TerminalOf(SP_PARM);
250
251     T((T_CALLED("reset_shell_mode(%p) ->term %p"),
252        (void *) SP_PARM, (void *) termp));
253
254     if (termp != 0) {
255 #ifdef USE_TERM_DRIVER
256         rc = CallDriver_2(SP_PARM, td_mode, FALSE, FALSE);
257 #else
258         if (SP_PARM) {
259             _nc_keypad(SP_PARM, FALSE);
260             _nc_flush();
261         }
262         rc = _nc_set_tty_mode(&termp->Ottyb);
263 #endif
264     }
265     returnCode(rc);
266 }
267
268 #if NCURSES_SP_FUNCS
269 NCURSES_EXPORT(int)
270 reset_shell_mode(void)
271 {
272     return NCURSES_SP_NAME(reset_shell_mode) (CURRENT_SCREEN);
273 }
274 #endif
275
276 static TTY *
277 saved_tty(NCURSES_SP_DCL0)
278 {
279     TTY *result = 0;
280
281     if (SP_PARM != 0) {
282         result = (TTY *) & (SP_PARM->_saved_tty);
283     } else {
284         if (_nc_prescreen.saved_tty == 0) {
285             _nc_prescreen.saved_tty = typeCalloc(TTY, 1);
286         }
287         result = _nc_prescreen.saved_tty;
288     }
289     return result;
290 }
291
292 /*
293 **      savetty()  and  resetty()
294 **
295 */
296
297 NCURSES_EXPORT(int)
298 NCURSES_SP_NAME(savetty) (NCURSES_SP_DCL0)
299 {
300     T((T_CALLED("savetty(%p)"), (void *) SP_PARM));
301     returnCode(NCURSES_SP_NAME(_nc_get_tty_mode) (NCURSES_SP_ARGx saved_tty(NCURSES_SP_ARG)));
302 }
303
304 #if NCURSES_SP_FUNCS
305 NCURSES_EXPORT(int)
306 savetty(void)
307 {
308     return NCURSES_SP_NAME(savetty) (CURRENT_SCREEN);
309 }
310 #endif
311
312 NCURSES_EXPORT(int)
313 NCURSES_SP_NAME(resetty) (NCURSES_SP_DCL0)
314 {
315     T((T_CALLED("resetty(%p)"), (void *) SP_PARM));
316     returnCode(NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx saved_tty(NCURSES_SP_ARG)));
317 }
318
319 #if NCURSES_SP_FUNCS
320 NCURSES_EXPORT(int)
321 resetty(void)
322 {
323     return NCURSES_SP_NAME(resetty) (CURRENT_SCREEN);
324 }
325 #endif