blob: 43bed3536cfc1d63c32d29ecedec1031394e6419 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
Steve Kondikae271bc2015-11-15 02:50:53 +01002 * Copyright (c) 1998-2012,2014 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05303 * *
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>
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053039
Steve Kondikae271bc2015-11-15 02:50:53 +010040#ifndef CUR
41#define CUR SP_TERMTYPE
42#endif
43
44MODULE_ID("$Id: lib_ttyflags.c,v 1.30 2014/04/26 18:47:20 juergen Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053045
46NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +010047NCURSES_SP_NAME(_nc_get_tty_mode) (NCURSES_SP_DCLx TTY * buf)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053048{
49 int result = OK;
50
Steve Kondikae271bc2015-11-15 02:50:53 +010051 if (buf == 0 || SP_PARM == 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053052 result = ERR;
53 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +010054 TERMINAL *termp = TerminalOf(SP_PARM);
55
56 if (0 == termp) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053057 result = ERR;
58 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +010059#ifdef USE_TERM_DRIVER
60 result = CallDriver_2(SP_PARM, td_sgmode, FALSE, buf);
61#else
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053062 for (;;) {
Steve Kondikae271bc2015-11-15 02:50:53 +010063 if (GET_TTY(termp->Filedes, buf) != 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053064 if (errno == EINTR)
65 continue;
66 result = ERR;
67 }
68 break;
69 }
Steve Kondikae271bc2015-11-15 02:50:53 +010070#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053071 }
72
73 if (result == ERR)
74 memset(buf, 0, sizeof(*buf));
75
76 TR(TRACE_BITS, ("_nc_get_tty_mode(%d): %s",
Steve Kondikae271bc2015-11-15 02:50:53 +010077 termp ? termp->Filedes : -1,
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053078 _nc_trace_ttymode(buf)));
79 }
80 return (result);
81}
82
Steve Kondikae271bc2015-11-15 02:50:53 +010083#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053084NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +010085_nc_get_tty_mode(TTY * buf)
86{
87 return NCURSES_SP_NAME(_nc_get_tty_mode) (CURRENT_SCREEN, buf);
88}
89#endif
90
91NCURSES_EXPORT(int)
92NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_DCLx TTY * buf)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053093{
94 int result = OK;
95
Steve Kondikae271bc2015-11-15 02:50:53 +010096 if (buf == 0 || SP_PARM == 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053097 result = ERR;
98 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +010099 TERMINAL *termp = TerminalOf(SP_PARM);
100
101 if (0 == termp) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530102 result = ERR;
103 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +0100104#ifdef USE_TERM_DRIVER
105 result = CallDriver_2(SP_PARM, td_sgmode, TRUE, buf);
106#else
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530107 for (;;) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100108 if ((SET_TTY(termp->Filedes, buf) != 0)
109#if USE_KLIBC_KBD
110 && !NC_ISATTY(termp->Filedes)
111#endif
112 ) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530113 if (errno == EINTR)
114 continue;
Steve Kondikae271bc2015-11-15 02:50:53 +0100115 if ((errno == ENOTTY) && (SP_PARM != 0))
116 SP_PARM->_notty = TRUE;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530117 result = ERR;
118 }
119 break;
120 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100121#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530122 }
123 TR(TRACE_BITS, ("_nc_set_tty_mode(%d): %s",
Steve Kondikae271bc2015-11-15 02:50:53 +0100124 termp ? termp->Filedes : -1,
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530125 _nc_trace_ttymode(buf)));
126 }
127 return (result);
128}
129
Steve Kondikae271bc2015-11-15 02:50:53 +0100130#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530131NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +0100132_nc_set_tty_mode(TTY * buf)
133{
134 return NCURSES_SP_NAME(_nc_set_tty_mode) (CURRENT_SCREEN, buf);
135}
136#endif
137
138NCURSES_EXPORT(int)
139NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_DCL0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530140{
141 int rc = ERR;
Steve Kondikae271bc2015-11-15 02:50:53 +0100142 TERMINAL *termp = TerminalOf(SP_PARM);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530143
Steve Kondikae271bc2015-11-15 02:50:53 +0100144 T((T_CALLED("def_shell_mode(%p)"), (void *) SP_PARM));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530145
Steve Kondikae271bc2015-11-15 02:50:53 +0100146 if (termp != 0) {
147#ifdef USE_TERM_DRIVER
148 rc = CallDriver_2(SP_PARM, td_mode, FALSE, TRUE);
149#else
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530150 /*
151 * If XTABS was on, remove the tab and backtab capabilities.
152 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100153 if (_nc_get_tty_mode(&termp->Ottyb) == OK) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530154#ifdef TERMIOS
Steve Kondikae271bc2015-11-15 02:50:53 +0100155 if (termp->Ottyb.c_oflag & OFLAGS_TABS)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530156 tab = back_tab = NULL;
157#else
Steve Kondikae271bc2015-11-15 02:50:53 +0100158 if (termp->Ottyb.sg_flags & XTABS)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530159 tab = back_tab = NULL;
160#endif
161 rc = OK;
162 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100163#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530164 }
165 returnCode(rc);
166}
167
Steve Kondikae271bc2015-11-15 02:50:53 +0100168#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530169NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +0100170def_shell_mode(void)
171{
172 return NCURSES_SP_NAME(def_shell_mode) (CURRENT_SCREEN);
173}
174#endif
175
176NCURSES_EXPORT(int)
177NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_DCL0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530178{
179 int rc = ERR;
Steve Kondikae271bc2015-11-15 02:50:53 +0100180 TERMINAL *termp = TerminalOf(SP_PARM);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530181
Steve Kondikae271bc2015-11-15 02:50:53 +0100182 T((T_CALLED("def_prog_mode(%p)"), (void *) SP_PARM));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530183
Steve Kondikae271bc2015-11-15 02:50:53 +0100184 if (termp != 0) {
185#ifdef USE_TERM_DRIVER
186 rc = CallDriver_2(SP_PARM, td_mode, TRUE, TRUE);
187#else
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530188 /*
189 * Turn off the XTABS bit in the tty structure if it was on.
190 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100191 if (_nc_get_tty_mode(&termp->Nttyb) == OK) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530192#ifdef TERMIOS
Steve Kondikae271bc2015-11-15 02:50:53 +0100193 termp->Nttyb.c_oflag &= (unsigned) (~OFLAGS_TABS);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530194#else
Steve Kondikae271bc2015-11-15 02:50:53 +0100195 termp->Nttyb.sg_flags &= (unsigned) (~XTABS);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530196#endif
197 rc = OK;
198 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100199#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530200 }
201 returnCode(rc);
202}
203
Steve Kondikae271bc2015-11-15 02:50:53 +0100204#if NCURSES_SP_FUNCS
205NCURSES_EXPORT(int)
206def_prog_mode(void)
207{
208 return NCURSES_SP_NAME(def_prog_mode) (CURRENT_SCREEN);
209}
210#endif
211
212NCURSES_EXPORT(int)
213NCURSES_SP_NAME(reset_prog_mode) (NCURSES_SP_DCL0)
214{
215 int rc = ERR;
216 TERMINAL *termp = TerminalOf(SP_PARM);
217
218 T((T_CALLED("reset_prog_mode(%p)"), (void *) SP_PARM));
219
220 if (termp != 0) {
221#ifdef USE_TERM_DRIVER
222 rc = CallDriver_2(SP_PARM, td_mode, TRUE, FALSE);
223#else
224 if (_nc_set_tty_mode(&termp->Nttyb) == OK) {
225 if (SP_PARM) {
226 if (SP_PARM->_keypad_on)
227 _nc_keypad(SP_PARM, TRUE);
228 }
229 rc = OK;
230 }
231#endif
232 }
233 returnCode(rc);
234}
235
236#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530237NCURSES_EXPORT(int)
238reset_prog_mode(void)
239{
Steve Kondikae271bc2015-11-15 02:50:53 +0100240 return NCURSES_SP_NAME(reset_prog_mode) (CURRENT_SCREEN);
241}
242#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530243
Steve Kondikae271bc2015-11-15 02:50:53 +0100244NCURSES_EXPORT(int)
245NCURSES_SP_NAME(reset_shell_mode) (NCURSES_SP_DCL0)
246{
247 int rc = ERR;
248 TERMINAL *termp = TerminalOf(SP_PARM);
249
250 T((T_CALLED("reset_shell_mode(%p)"), (void *) SP_PARM));
251
252 if (termp != 0) {
253#ifdef USE_TERM_DRIVER
254 rc = CallDriver_2(SP_PARM, td_mode, FALSE, FALSE);
255#else
256 if (SP_PARM) {
257 _nc_keypad(SP_PARM, FALSE);
258 _nc_flush();
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530259 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100260 rc = _nc_set_tty_mode(&termp->Ottyb);
261#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530262 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100263 returnCode(rc);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530264}
265
Steve Kondikae271bc2015-11-15 02:50:53 +0100266#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530267NCURSES_EXPORT(int)
268reset_shell_mode(void)
269{
Steve Kondikae271bc2015-11-15 02:50:53 +0100270 return NCURSES_SP_NAME(reset_shell_mode) (CURRENT_SCREEN);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530271}
Steve Kondikae271bc2015-11-15 02:50:53 +0100272#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530273
274static TTY *
Steve Kondikae271bc2015-11-15 02:50:53 +0100275saved_tty(NCURSES_SP_DCL0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530276{
277 TTY *result = 0;
278
Steve Kondikae271bc2015-11-15 02:50:53 +0100279 if (SP_PARM != 0) {
280 result = (TTY *) & (SP_PARM->_saved_tty);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530281 } else {
282 if (_nc_prescreen.saved_tty == 0) {
283 _nc_prescreen.saved_tty = typeCalloc(TTY, 1);
284 }
285 result = _nc_prescreen.saved_tty;
286 }
287 return result;
288}
289
290/*
291** savetty() and resetty()
292**
293*/
294
295NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +0100296NCURSES_SP_NAME(savetty) (NCURSES_SP_DCL0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530297{
Steve Kondikae271bc2015-11-15 02:50:53 +0100298 T((T_CALLED("savetty(%p)"), (void *) SP_PARM));
299 returnCode(NCURSES_SP_NAME(_nc_get_tty_mode) (NCURSES_SP_ARGx saved_tty(NCURSES_SP_ARG)));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530300}
301
Steve Kondikae271bc2015-11-15 02:50:53 +0100302#if NCURSES_SP_FUNCS
303NCURSES_EXPORT(int)
304savetty(void)
305{
306 return NCURSES_SP_NAME(savetty) (CURRENT_SCREEN);
307}
308#endif
309
310NCURSES_EXPORT(int)
311NCURSES_SP_NAME(resetty) (NCURSES_SP_DCL0)
312{
313 T((T_CALLED("resetty(%p)"), (void *) SP_PARM));
314 returnCode(NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx saved_tty(NCURSES_SP_ARG)));
315}
316
317#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530318NCURSES_EXPORT(int)
319resetty(void)
320{
Steve Kondikae271bc2015-11-15 02:50:53 +0100321 return NCURSES_SP_NAME(resetty) (CURRENT_SCREEN);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530322}
Steve Kondikae271bc2015-11-15 02:50:53 +0100323#endif