1 /****************************************************************************
2 * Copyright 2020-2023,2024 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1998-on *
34 * and: Juergen Pfeifer 2009 *
35 ****************************************************************************/
51 #include <curses.priv.h>
53 MODULE_ID("$Id: lib_raw.c,v 1.30 2024/03/30 15:54:17 tom Exp $")
56 #include <sys/termio.h> /* needed for ISC */
61 #define _nc_setmode(mode) setmode(SP_PARM->_ifd, mode)
63 #define _nc_setmode(mode) /* nothing */
71 #define COOKED_INPUT (IXON|BRKINT|PARMRK)
74 #define BEFORE(N) if (USE_TRACEF(TRACE_BITS)) _nc_locked_tracef("%s before bits: %s", N, _nc_tracebits())
75 #define AFTER(N) if (USE_TRACEF(TRACE_BITS)) _nc_locked_tracef("%s after bits: %s", N, _nc_tracebits())
82 NCURSES_SP_NAME(raw) (NCURSES_SP_DCL0)
87 T((T_CALLED("raw(%p)"), (void *) SP_PARM));
88 if ((termp = TerminalOf(SP_PARM)) != 0) {
92 _nc_setmode(O_BINARY);
96 buf.c_lflag &= (unsigned) ~(ICANON | ISIG | IEXTEN);
97 buf.c_iflag &= (unsigned) ~(COOKED_INPUT);
100 #elif defined(EXP_WIN32_DRIVER)
101 buf.dwFlagIn &= (unsigned long) ~CONMODE_NORAW;
105 result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
110 kbdinfo.cb = sizeof(kbdinfo);
111 KbdGetStatus(&kbdinfo, 0);
113 kbdinfo.cb = sizeof(kbdinfo);
114 kbdinfo.fsMask &= ~KEYBOARD_ASCII_MODE;
115 kbdinfo.fsMask |= KEYBOARD_BINARY_MODE;
116 KbdSetStatus(&kbdinfo, 0);
119 IsRaw(SP_PARM) = TRUE;
120 IsCbreak(SP_PARM) = 1;
133 return NCURSES_SP_NAME(raw) (CURRENT_SCREEN);
138 NCURSES_SP_NAME(cbreak) (NCURSES_SP_DCL0)
143 T((T_CALLED("cbreak(%p)"), (void *) SP_PARM));
144 if ((termp = TerminalOf(SP_PARM)) != 0) {
148 _nc_setmode(O_BINARY);
152 buf.c_lflag &= (unsigned) ~ICANON;
153 buf.c_iflag &= (unsigned) ~ICRNL;
156 #elif defined(EXP_WIN32_DRIVER)
157 buf.dwFlagIn |= CONMODE_NORAW;
158 buf.dwFlagIn &= (unsigned long) ~CONMODE_NOCBREAK;
160 buf.sg_flags |= CBREAK;
162 result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
165 IsCbreak(SP_PARM) = 1;
178 return NCURSES_SP_NAME(cbreak) (CURRENT_SCREEN);
184 * this implementation may be wrong. See the comment under intrflush().
187 NCURSES_SP_NAME(qiflush) (NCURSES_SP_DCL0)
191 T((T_CALLED("qiflush(%p)"), (void *) SP_PARM));
192 if ((termp = TerminalOf(SP_PARM)) != 0) {
199 buf.c_lflag &= (unsigned) ~(NOFLSH);
200 result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
216 NCURSES_SP_NAME(qiflush) (CURRENT_SCREEN);
221 NCURSES_SP_NAME(noraw) (NCURSES_SP_DCL0)
226 T((T_CALLED("noraw(%p)"), (void *) SP_PARM));
227 if ((termp = TerminalOf(SP_PARM)) != 0) {
235 buf.c_lflag |= ISIG | ICANON |
236 (termp->Ottyb.c_lflag & IEXTEN);
237 buf.c_iflag |= COOKED_INPUT;
238 #elif defined(EXP_WIN32_DRIVER)
239 buf.dwFlagIn |= CONMODE_NORAW;
241 buf.sg_flags &= ~(RAW | CBREAK);
243 result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
248 kbdinfo.cb = sizeof(kbdinfo);
249 KbdGetStatus(&kbdinfo, 0);
251 kbdinfo.cb = sizeof(kbdinfo);
252 kbdinfo.fsMask &= ~KEYBOARD_BINARY_MODE;
253 kbdinfo.fsMask |= KEYBOARD_ASCII_MODE;
254 KbdSetStatus(&kbdinfo, 0);
257 IsRaw(SP_PARM) = FALSE;
258 IsCbreak(SP_PARM) = 0;
271 return NCURSES_SP_NAME(noraw) (CURRENT_SCREEN);
276 NCURSES_SP_NAME(nocbreak) (NCURSES_SP_DCL0)
281 T((T_CALLED("nocbreak(%p)"), (void *) SP_PARM));
282 if ((termp = TerminalOf(SP_PARM)) != 0) {
290 buf.c_lflag |= ICANON;
291 buf.c_iflag |= ICRNL;
292 #elif defined(EXP_WIN32_DRIVER)
293 buf.dwFlagIn |= (CONMODE_NOCBREAK | CONMODE_NORAW);
295 buf.sg_flags &= ~CBREAK;
297 result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
300 IsCbreak(SP_PARM) = 0;
313 return NCURSES_SP_NAME(nocbreak) (CURRENT_SCREEN);
318 NCURSES_SP_NAME(noqiflush) (NCURSES_SP_DCL0)
322 T((T_CALLED("noqiflush(%p)"), (void *) SP_PARM));
323 if ((termp = TerminalOf(SP_PARM)) != 0) {
330 buf.c_lflag |= NOFLSH;
331 result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
347 NCURSES_SP_NAME(noqiflush) (CURRENT_SCREEN);
352 * This call does the same thing as the qiflush()/noqiflush() pair. We know
353 * for certain that SVr3 intrflush() tweaks the NOFLSH bit; on the other hand,
354 * the match (in the SVr4 man pages) between the language describing NOFLSH in
355 * termio(7) and the language describing qiflush()/noqiflush() in
356 * curs_inopts(3x) is too exact to be coincidence.
359 NCURSES_SP_NAME(intrflush) (NCURSES_SP_DCLx WINDOW *win GCC_UNUSED, bool flag)
364 T((T_CALLED("intrflush(%p,%d)"), (void *) SP_PARM, flag));
368 if ((termp = TerminalOf(SP_PARM)) != 0) {
375 buf.c_lflag &= (unsigned) ~(NOFLSH);
377 buf.c_lflag |= (NOFLSH);
378 result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
392 intrflush(WINDOW *win GCC_UNUSED, bool flag)
394 return NCURSES_SP_NAME(intrflush) (CURRENT_SCREEN, win, flag);
398 #if NCURSES_EXT_FUNCS
402 * SCREEN is always opaque, but nl/raw/cbreak/echo set properties in it.
403 * As an extension, provide a way to query the properties.
405 * There are other properties which could be queried, e.g., filter, keypad,
406 * use_env, use_meta, but these particular properties are saved/restored within
407 * the wgetnstr() and wgetn_wstr() functions, which requires that the higher
408 * level curses library knows about the internal state of the lower level
412 #define is_TEST(show,what) \
413 NCURSES_EXPORT(int) \
414 NCURSES_SP_NAME(show) (NCURSES_SP_DCL0) \
416 return ((SP_PARM != NULL) ? (what(SP_PARM) ? 1 : 0) : -1); \
420 is_TEST(is_raw, IsRaw)
421 is_TEST(is_cbreak, IsCbreak)
422 is_TEST(is_echo, IsEcho)
426 #define is_TEST(show) \
427 NCURSES_EXPORT(int) \
430 return NCURSES_SP_NAME(show) (CURRENT_SCREEN); \
439 #endif /* extensions */