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