]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_raw.c
ncurses 6.0 - patch 20170204
[ncurses.git] / ncurses / tinfo / lib_raw.c
1 /****************************************************************************
2  * Copyright (c) 1998-2012,2016 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.22 2016/05/28 23:22:52 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             SP_PARM->_raw = TRUE;
116             SP_PARM->_cbreak = 1;
117             termp->Nttyb = buf;
118         }
119         AFTER("raw");
120     }
121     returnCode(result);
122 }
123
124 #if NCURSES_SP_FUNCS
125 NCURSES_EXPORT(int)
126 raw(void)
127 {
128     return NCURSES_SP_NAME(raw) (CURRENT_SCREEN);
129 }
130 #endif
131
132 NCURSES_EXPORT(int)
133 NCURSES_SP_NAME(cbreak) (NCURSES_SP_DCL0)
134 {
135     int result = ERR;
136     TERMINAL *termp;
137
138     T((T_CALLED("cbreak(%p)"), (void *) SP_PARM));
139     if ((termp = TerminalOf(SP_PARM)) != 0) {
140         TTY buf;
141
142         BEFORE("cbreak");
143         _nc_setmode(O_BINARY);
144
145         buf = termp->Nttyb;
146 #ifdef TERMIOS
147         buf.c_lflag &= (unsigned) ~ICANON;
148         buf.c_iflag &= (unsigned) ~ICRNL;
149         buf.c_lflag |= ISIG;
150         buf.c_cc[VMIN] = 1;
151         buf.c_cc[VTIME] = 0;
152 #else
153         buf.sg_flags |= CBREAK;
154 #endif
155         result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
156         if (result == OK) {
157             SP_PARM->_cbreak = 1;
158             termp->Nttyb = buf;
159         }
160         AFTER("cbreak");
161     }
162     returnCode(result);
163 }
164
165 #if NCURSES_SP_FUNCS
166 NCURSES_EXPORT(int)
167 cbreak(void)
168 {
169     return NCURSES_SP_NAME(cbreak) (CURRENT_SCREEN);
170 }
171 #endif
172
173 /*
174  * Note:
175  * this implementation may be wrong.  See the comment under intrflush().
176  */
177 NCURSES_EXPORT(void)
178 NCURSES_SP_NAME(qiflush) (NCURSES_SP_DCL0)
179 {
180     TERMINAL *termp;
181
182     T((T_CALLED("qiflush(%p)"), (void *) SP_PARM));
183     if ((termp = TerminalOf(SP_PARM)) != 0) {
184         TTY buf;
185         int result;
186
187         BEFORE("qiflush");
188         buf = termp->Nttyb;
189 #ifdef TERMIOS
190         buf.c_lflag &= (unsigned) ~(NOFLSH);
191         result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
192 #else
193         result = ERR;
194         /* FIXME */
195 #endif
196         if (result == OK)
197             termp->Nttyb = buf;
198         AFTER("qiflush");
199     }
200     returnVoid;
201 }
202
203 #if NCURSES_SP_FUNCS
204 NCURSES_EXPORT(void)
205 qiflush(void)
206 {
207     NCURSES_SP_NAME(qiflush) (CURRENT_SCREEN);
208 }
209 #endif
210
211 NCURSES_EXPORT(int)
212 NCURSES_SP_NAME(noraw) (NCURSES_SP_DCL0)
213 {
214     int result = ERR;
215     TERMINAL *termp;
216
217     T((T_CALLED("noraw(%p)"), (void *) SP_PARM));
218     if ((termp = TerminalOf(SP_PARM)) != 0) {
219         TTY buf;
220
221         BEFORE("noraw");
222         _nc_setmode(O_TEXT);
223
224         buf = termp->Nttyb;
225 #ifdef TERMIOS
226         buf.c_lflag |= ISIG | ICANON |
227             (termp->Ottyb.c_lflag & IEXTEN);
228         buf.c_iflag |= COOKED_INPUT;
229 #else
230         buf.sg_flags &= ~(RAW | CBREAK);
231 #endif
232         result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
233         if (result == OK) {
234 #if USE_KLIBC_KBD
235             KBDINFO kbdinfo;
236
237             kbdinfo.cb = sizeof(kbdinfo);
238             KbdGetStatus(&kbdinfo, 0);
239
240             kbdinfo.cb = sizeof(kbdinfo);
241             kbdinfo.fsMask &= ~KEYBOARD_BINARY_MODE;
242             kbdinfo.fsMask |= KEYBOARD_ASCII_MODE;
243             KbdSetStatus(&kbdinfo, 0);
244 #endif
245             SP_PARM->_raw = FALSE;
246             SP_PARM->_cbreak = 0;
247             termp->Nttyb = buf;
248         }
249         AFTER("noraw");
250     }
251     returnCode(result);
252 }
253
254 #if NCURSES_SP_FUNCS
255 NCURSES_EXPORT(int)
256 noraw(void)
257 {
258     return NCURSES_SP_NAME(noraw) (CURRENT_SCREEN);
259 }
260 #endif
261
262 NCURSES_EXPORT(int)
263 NCURSES_SP_NAME(nocbreak) (NCURSES_SP_DCL0)
264 {
265     int result = ERR;
266     TERMINAL *termp;
267
268     T((T_CALLED("nocbreak(%p)"), (void *) SP_PARM));
269     if ((termp = TerminalOf(SP_PARM)) != 0) {
270         TTY buf;
271
272         BEFORE("nocbreak");
273         _nc_setmode(O_TEXT);
274
275         buf = termp->Nttyb;
276 #ifdef TERMIOS
277         buf.c_lflag |= ICANON;
278         buf.c_iflag |= ICRNL;
279 #else
280         buf.sg_flags &= ~CBREAK;
281 #endif
282         result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
283         if (result == OK) {
284             SP_PARM->_cbreak = 0;
285             termp->Nttyb = buf;
286         }
287         AFTER("nocbreak");
288     }
289     returnCode(result);
290 }
291
292 #if NCURSES_SP_FUNCS
293 NCURSES_EXPORT(int)
294 nocbreak(void)
295 {
296     return NCURSES_SP_NAME(nocbreak) (CURRENT_SCREEN);
297 }
298 #endif
299
300 NCURSES_EXPORT(void)
301 NCURSES_SP_NAME(noqiflush) (NCURSES_SP_DCL0)
302 {
303     TERMINAL *termp;
304
305     T((T_CALLED("noqiflush(%p)"), (void *) SP_PARM));
306     if ((termp = TerminalOf(SP_PARM)) != 0) {
307         TTY buf;
308         int result;
309
310         BEFORE("noqiflush");
311         buf = termp->Nttyb;
312 #ifdef TERMIOS
313         buf.c_lflag |= NOFLSH;
314         result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
315 #else
316         /* FIXME */
317         result = ERR;
318 #endif
319         if (result == OK)
320             termp->Nttyb = buf;
321         AFTER("noqiflush");
322     }
323     returnVoid;
324 }
325
326 #if NCURSES_SP_FUNCS
327 NCURSES_EXPORT(void)
328 noqiflush(void)
329 {
330     NCURSES_SP_NAME(noqiflush) (CURRENT_SCREEN);
331 }
332 #endif
333
334 /*
335  * This call does the same thing as the qiflush()/noqiflush() pair.  We know
336  * for certain that SVr3 intrflush() tweaks the NOFLSH bit; on the other hand,
337  * the match (in the SVr4 man pages) between the language describing NOFLSH in
338  * termio(7) and the language describing qiflush()/noqiflush() in
339  * curs_inopts(3x) is too exact to be coincidence.
340  */
341 NCURSES_EXPORT(int)
342 NCURSES_SP_NAME(intrflush) (NCURSES_SP_DCLx WINDOW *win GCC_UNUSED, bool flag)
343 {
344     int result = ERR;
345     TERMINAL *termp;
346
347     T((T_CALLED("intrflush(%p,%d)"), (void *) SP_PARM, flag));
348     if (SP_PARM == 0)
349         returnCode(ERR);
350
351     if ((termp = TerminalOf(SP_PARM)) != 0) {
352         TTY buf;
353
354         BEFORE("intrflush");
355         buf = termp->Nttyb;
356 #ifdef TERMIOS
357         if (flag)
358             buf.c_lflag &= (unsigned) ~(NOFLSH);
359         else
360             buf.c_lflag |= (NOFLSH);
361         result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
362 #else
363         /* FIXME */
364 #endif
365         if (result == OK) {
366             termp->Nttyb = buf;
367         }
368         AFTER("intrflush");
369     }
370     returnCode(result);
371 }
372
373 #if NCURSES_SP_FUNCS
374 NCURSES_EXPORT(int)
375 intrflush(WINDOW *win GCC_UNUSED, bool flag)
376 {
377     return NCURSES_SP_NAME(intrflush) (CURRENT_SCREEN, win, flag);
378 }
379 #endif