]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_raw.c
ncurses 6.1 - patch 20180414
[ncurses.git] / ncurses / tinfo / lib_raw.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  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  *     and: Thomas E. Dickey                        1998-on                 *
33  *     and: Juergen Pfeifer                         2009                    *
34  ****************************************************************************/
35
36 /*
37  *      raw.c
38  *
39  *      Routines:
40  *              raw()
41  *              cbreak()
42  *              noraw()
43  *              nocbreak()
44  *              qiflush()
45  *              noqiflush()
46  *              intrflush()
47  *
48  */
49
50 #include <curses.priv.h>
51
52 MODULE_ID("$Id: lib_raw.c,v 1.23 2017/04/15 22:24:45 tom Exp $")
53
54 #if HAVE_SYS_TERMIO_H
55 #include <sys/termio.h>         /* needed for ISC */
56 #endif
57
58 #ifdef __EMX__
59 #include <io.h>
60 #define _nc_setmode(mode) setmode(SP_PARM->_ifd, mode)
61 #else
62 #define _nc_setmode(mode)       /* nothing */
63 #endif
64
65 #if USE_KLIBC_KBD
66 #define INCL_KBD
67 #include <os2.h>
68 #endif
69
70 #define COOKED_INPUT    (IXON|BRKINT|PARMRK)
71
72 #ifdef TRACE
73 #define BEFORE(N)       if (USE_TRACEF(TRACE_BITS)) _nc_locked_tracef("%s before bits: %s", N, _nc_tracebits())
74 #define AFTER(N)        if (USE_TRACEF(TRACE_BITS)) _nc_locked_tracef("%s after bits: %s", N, _nc_tracebits())
75 #else
76 #define BEFORE(s)
77 #define AFTER(s)
78 #endif /* TRACE */
79
80 NCURSES_EXPORT(int)
81 NCURSES_SP_NAME(raw) (NCURSES_SP_DCL0)
82 {
83     int result = ERR;
84     TERMINAL *termp;
85
86     T((T_CALLED("raw(%p)"), (void *) SP_PARM));
87     if ((termp = TerminalOf(SP_PARM)) != 0) {
88         TTY buf;
89
90         BEFORE("raw");
91         _nc_setmode(O_BINARY);
92
93         buf = termp->Nttyb;
94 #ifdef TERMIOS
95         buf.c_lflag &= (unsigned) ~(ICANON | ISIG | IEXTEN);
96         buf.c_iflag &= (unsigned) ~(COOKED_INPUT);
97         buf.c_cc[VMIN] = 1;
98         buf.c_cc[VTIME] = 0;
99 #else
100         buf.sg_flags |= RAW;
101 #endif
102         result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
103         if (result == OK) {
104 #if USE_KLIBC_KBD
105             KBDINFO kbdinfo;
106
107             kbdinfo.cb = sizeof(kbdinfo);
108             KbdGetStatus(&kbdinfo, 0);
109
110             kbdinfo.cb = sizeof(kbdinfo);
111             kbdinfo.fsMask &= ~KEYBOARD_ASCII_MODE;
112             kbdinfo.fsMask |= KEYBOARD_BINARY_MODE;
113             KbdSetStatus(&kbdinfo, 0);
114 #endif
115             if (SP_PARM) {
116                 SP_PARM->_raw = TRUE;
117                 SP_PARM->_cbreak = 1;
118             }
119             termp->Nttyb = buf;
120         }
121         AFTER("raw");
122     }
123     returnCode(result);
124 }
125
126 #if NCURSES_SP_FUNCS
127 NCURSES_EXPORT(int)
128 raw(void)
129 {
130     return NCURSES_SP_NAME(raw) (CURRENT_SCREEN);
131 }
132 #endif
133
134 NCURSES_EXPORT(int)
135 NCURSES_SP_NAME(cbreak) (NCURSES_SP_DCL0)
136 {
137     int result = ERR;
138     TERMINAL *termp;
139
140     T((T_CALLED("cbreak(%p)"), (void *) SP_PARM));
141     if ((termp = TerminalOf(SP_PARM)) != 0) {
142         TTY buf;
143
144         BEFORE("cbreak");
145         _nc_setmode(O_BINARY);
146
147         buf = termp->Nttyb;
148 #ifdef TERMIOS
149         buf.c_lflag &= (unsigned) ~ICANON;
150         buf.c_iflag &= (unsigned) ~ICRNL;
151         buf.c_lflag |= ISIG;
152         buf.c_cc[VMIN] = 1;
153         buf.c_cc[VTIME] = 0;
154 #else
155         buf.sg_flags |= CBREAK;
156 #endif
157         result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
158         if (result == OK) {
159             if (SP_PARM) {
160                 SP_PARM->_cbreak = 1;
161             }
162             termp->Nttyb = buf;
163         }
164         AFTER("cbreak");
165     }
166     returnCode(result);
167 }
168
169 #if NCURSES_SP_FUNCS
170 NCURSES_EXPORT(int)
171 cbreak(void)
172 {
173     return NCURSES_SP_NAME(cbreak) (CURRENT_SCREEN);
174 }
175 #endif
176
177 /*
178  * Note:
179  * this implementation may be wrong.  See the comment under intrflush().
180  */
181 NCURSES_EXPORT(void)
182 NCURSES_SP_NAME(qiflush) (NCURSES_SP_DCL0)
183 {
184     TERMINAL *termp;
185
186     T((T_CALLED("qiflush(%p)"), (void *) SP_PARM));
187     if ((termp = TerminalOf(SP_PARM)) != 0) {
188         TTY buf;
189         int result;
190
191         BEFORE("qiflush");
192         buf = termp->Nttyb;
193 #ifdef TERMIOS
194         buf.c_lflag &= (unsigned) ~(NOFLSH);
195         result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
196 #else
197         result = ERR;
198         /* FIXME */
199 #endif
200         if (result == OK)
201             termp->Nttyb = buf;
202         AFTER("qiflush");
203     }
204     returnVoid;
205 }
206
207 #if NCURSES_SP_FUNCS
208 NCURSES_EXPORT(void)
209 qiflush(void)
210 {
211     NCURSES_SP_NAME(qiflush) (CURRENT_SCREEN);
212 }
213 #endif
214
215 NCURSES_EXPORT(int)
216 NCURSES_SP_NAME(noraw) (NCURSES_SP_DCL0)
217 {
218     int result = ERR;
219     TERMINAL *termp;
220
221     T((T_CALLED("noraw(%p)"), (void *) SP_PARM));
222     if ((termp = TerminalOf(SP_PARM)) != 0) {
223         TTY buf;
224
225         BEFORE("noraw");
226         _nc_setmode(O_TEXT);
227
228         buf = termp->Nttyb;
229 #ifdef TERMIOS
230         buf.c_lflag |= ISIG | ICANON |
231             (termp->Ottyb.c_lflag & IEXTEN);
232         buf.c_iflag |= COOKED_INPUT;
233 #else
234         buf.sg_flags &= ~(RAW | CBREAK);
235 #endif
236         result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
237         if (result == OK) {
238 #if USE_KLIBC_KBD
239             KBDINFO kbdinfo;
240
241             kbdinfo.cb = sizeof(kbdinfo);
242             KbdGetStatus(&kbdinfo, 0);
243
244             kbdinfo.cb = sizeof(kbdinfo);
245             kbdinfo.fsMask &= ~KEYBOARD_BINARY_MODE;
246             kbdinfo.fsMask |= KEYBOARD_ASCII_MODE;
247             KbdSetStatus(&kbdinfo, 0);
248 #endif
249             if (SP_PARM) {
250                 SP_PARM->_raw = FALSE;
251                 SP_PARM->_cbreak = 0;
252             }
253             termp->Nttyb = buf;
254         }
255         AFTER("noraw");
256     }
257     returnCode(result);
258 }
259
260 #if NCURSES_SP_FUNCS
261 NCURSES_EXPORT(int)
262 noraw(void)
263 {
264     return NCURSES_SP_NAME(noraw) (CURRENT_SCREEN);
265 }
266 #endif
267
268 NCURSES_EXPORT(int)
269 NCURSES_SP_NAME(nocbreak) (NCURSES_SP_DCL0)
270 {
271     int result = ERR;
272     TERMINAL *termp;
273
274     T((T_CALLED("nocbreak(%p)"), (void *) SP_PARM));
275     if ((termp = TerminalOf(SP_PARM)) != 0) {
276         TTY buf;
277
278         BEFORE("nocbreak");
279         _nc_setmode(O_TEXT);
280
281         buf = termp->Nttyb;
282 #ifdef TERMIOS
283         buf.c_lflag |= ICANON;
284         buf.c_iflag |= ICRNL;
285 #else
286         buf.sg_flags &= ~CBREAK;
287 #endif
288         result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
289         if (result == OK) {
290             if (SP_PARM) {
291                 SP_PARM->_cbreak = 0;
292             }
293             termp->Nttyb = buf;
294         }
295         AFTER("nocbreak");
296     }
297     returnCode(result);
298 }
299
300 #if NCURSES_SP_FUNCS
301 NCURSES_EXPORT(int)
302 nocbreak(void)
303 {
304     return NCURSES_SP_NAME(nocbreak) (CURRENT_SCREEN);
305 }
306 #endif
307
308 NCURSES_EXPORT(void)
309 NCURSES_SP_NAME(noqiflush) (NCURSES_SP_DCL0)
310 {
311     TERMINAL *termp;
312
313     T((T_CALLED("noqiflush(%p)"), (void *) SP_PARM));
314     if ((termp = TerminalOf(SP_PARM)) != 0) {
315         TTY buf;
316         int result;
317
318         BEFORE("noqiflush");
319         buf = termp->Nttyb;
320 #ifdef TERMIOS
321         buf.c_lflag |= NOFLSH;
322         result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
323 #else
324         /* FIXME */
325         result = ERR;
326 #endif
327         if (result == OK)
328             termp->Nttyb = buf;
329         AFTER("noqiflush");
330     }
331     returnVoid;
332 }
333
334 #if NCURSES_SP_FUNCS
335 NCURSES_EXPORT(void)
336 noqiflush(void)
337 {
338     NCURSES_SP_NAME(noqiflush) (CURRENT_SCREEN);
339 }
340 #endif
341
342 /*
343  * This call does the same thing as the qiflush()/noqiflush() pair.  We know
344  * for certain that SVr3 intrflush() tweaks the NOFLSH bit; on the other hand,
345  * the match (in the SVr4 man pages) between the language describing NOFLSH in
346  * termio(7) and the language describing qiflush()/noqiflush() in
347  * curs_inopts(3x) is too exact to be coincidence.
348  */
349 NCURSES_EXPORT(int)
350 NCURSES_SP_NAME(intrflush) (NCURSES_SP_DCLx WINDOW *win GCC_UNUSED, bool flag)
351 {
352     int result = ERR;
353     TERMINAL *termp;
354
355     T((T_CALLED("intrflush(%p,%d)"), (void *) SP_PARM, flag));
356     if (SP_PARM == 0)
357         returnCode(ERR);
358
359     if ((termp = TerminalOf(SP_PARM)) != 0) {
360         TTY buf;
361
362         BEFORE("intrflush");
363         buf = termp->Nttyb;
364 #ifdef TERMIOS
365         if (flag)
366             buf.c_lflag &= (unsigned) ~(NOFLSH);
367         else
368             buf.c_lflag |= (NOFLSH);
369         result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
370 #else
371         /* FIXME */
372 #endif
373         if (result == OK) {
374             termp->Nttyb = buf;
375         }
376         AFTER("intrflush");
377     }
378     returnCode(result);
379 }
380
381 #if NCURSES_SP_FUNCS
382 NCURSES_EXPORT(int)
383 intrflush(WINDOW *win GCC_UNUSED, bool flag)
384 {
385     return NCURSES_SP_NAME(intrflush) (CURRENT_SCREEN, win, flag);
386 }
387 #endif