]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_raw.c
ncurses 5.7 - patch 20090418
[ncurses.git] / ncurses / tinfo / lib_raw.c
1 /****************************************************************************
2  * Copyright (c) 1998-2007,2009 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 #include <term.h>               /* cur_term */
52
53 MODULE_ID("$Id: lib_raw.c,v 1.15 2009/02/15 00:49:02 tom Exp $")
54
55 #if SVR4_TERMIO && !defined(_POSIX_SOURCE)
56 #define _POSIX_SOURCE
57 #endif
58
59 #if HAVE_SYS_TERMIO_H
60 #include <sys/termio.h>         /* needed for ISC */
61 #endif
62
63 #ifdef __EMX__
64 #include <io.h>
65 #define _nc_setmode(mode) setmode(SP_PARM->_ifd, mode)
66 #else
67 #define _nc_setmode(mode)       /* nothing */
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
85     T((T_CALLED("raw()")));
86
87     if (SP_PARM != 0 && cur_term != 0) {
88         TTY buf;
89
90         BEFORE("raw");
91         _nc_setmode(O_BINARY);
92
93         buf = cur_term->Nttyb;
94 #ifdef TERMIOS
95         buf.c_lflag &= ~(ICANON | ISIG | IEXTEN);
96         buf.c_iflag &= ~(COOKED_INPUT);
97         buf.c_cc[VMIN] = 1;
98         buf.c_cc[VTIME] = 0;
99 #else
100         buf.sg_flags |= RAW;
101 #endif
102         if ((result = _nc_set_tty_mode(&buf)) == OK) {
103             SP_PARM->_raw = TRUE;
104             SP_PARM->_cbreak = 1;
105             cur_term->Nttyb = buf;
106         }
107         AFTER("raw");
108     }
109     returnCode(result);
110 }
111
112 #if NCURSES_SP_FUNCS
113 NCURSES_EXPORT(int)
114 raw(void)
115 {
116     return NCURSES_SP_NAME(raw) (CURRENT_SCREEN);
117 }
118 #endif
119
120 NCURSES_EXPORT(int)
121 NCURSES_SP_NAME(cbreak) (NCURSES_SP_DCL0)
122 {
123     int result = ERR;
124
125     T((T_CALLED("cbreak()")));
126
127     if (SP_PARM != 0 && cur_term != 0) {
128         TTY buf;
129
130         BEFORE("cbreak");
131         _nc_setmode(O_BINARY);
132
133         buf = cur_term->Nttyb;
134 #ifdef TERMIOS
135         buf.c_lflag &= ~ICANON;
136         buf.c_iflag &= ~ICRNL;
137         buf.c_lflag |= ISIG;
138         buf.c_cc[VMIN] = 1;
139         buf.c_cc[VTIME] = 0;
140 #else
141         buf.sg_flags |= CBREAK;
142 #endif
143         if ((result = _nc_set_tty_mode(&buf)) == OK) {
144             SP_PARM->_cbreak = 1;
145             cur_term->Nttyb = buf;
146         }
147         AFTER("cbreak");
148     }
149     returnCode(result);
150 }
151
152 #if NCURSES_SP_FUNCS
153 NCURSES_EXPORT(int)
154 cbreak(void)
155 {
156     return NCURSES_SP_NAME(cbreak) (CURRENT_SCREEN);
157 }
158 #endif
159
160 /*
161  * Note:
162  * this implementation may be wrong.  See the comment under intrflush().
163  */
164 NCURSES_EXPORT(void)
165 NCURSES_SP_NAME(qiflush) (NCURSES_SP_DCL0)
166 {
167     int result = ERR;
168
169     T((T_CALLED("qiflush()")));
170
171     if (cur_term != 0) {
172         TTY buf;
173
174         BEFORE("qiflush");
175         buf = cur_term->Nttyb;
176 #ifdef TERMIOS
177         buf.c_lflag &= ~(NOFLSH);
178         result = _nc_set_tty_mode(&buf);
179 #else
180         /* FIXME */
181 #endif
182         if (result == OK)
183             cur_term->Nttyb = buf;
184         AFTER("qiflush");
185     }
186     returnVoid;
187 }
188
189 #if NCURSES_SP_FUNCS
190 NCURSES_EXPORT(void)
191 qiflush(void)
192 {
193     NCURSES_SP_NAME(qiflush) (CURRENT_SCREEN);
194 }
195 #endif
196
197 NCURSES_EXPORT(int)
198 NCURSES_SP_NAME(noraw) (NCURSES_SP_DCL0)
199 {
200     int result = ERR;
201
202     T((T_CALLED("noraw()")));
203
204     if (SP_PARM != 0 && cur_term != 0) {
205         TTY buf;
206
207         BEFORE("noraw");
208         _nc_setmode(O_TEXT);
209
210         buf = cur_term->Nttyb;
211 #ifdef TERMIOS
212         buf.c_lflag |= ISIG | ICANON |
213             (cur_term->Ottyb.c_lflag & IEXTEN);
214         buf.c_iflag |= COOKED_INPUT;
215 #else
216         buf.sg_flags &= ~(RAW | CBREAK);
217 #endif
218         if ((result = _nc_set_tty_mode(&buf)) == OK) {
219             SP_PARM->_raw = FALSE;
220             SP_PARM->_cbreak = 0;
221             cur_term->Nttyb = buf;
222         }
223         AFTER("noraw");
224     }
225     returnCode(result);
226 }
227
228 #if NCURSES_SP_FUNCS
229 NCURSES_EXPORT(int)
230 noraw(void)
231 {
232     return NCURSES_SP_NAME(noraw) (CURRENT_SCREEN);
233 }
234 #endif
235
236 NCURSES_EXPORT(int)
237 NCURSES_SP_NAME(nocbreak) (NCURSES_SP_DCL0)
238 {
239     int result = ERR;
240
241     T((T_CALLED("nocbreak()")));
242
243     if (SP_PARM != 0 && cur_term != 0) {
244         TTY buf;
245
246         BEFORE("nocbreak");
247         _nc_setmode(O_TEXT);
248
249         buf = cur_term->Nttyb;
250 #ifdef TERMIOS
251         buf.c_lflag |= ICANON;
252         buf.c_iflag |= ICRNL;
253 #else
254         buf.sg_flags &= ~CBREAK;
255 #endif
256         if ((result = _nc_set_tty_mode(&buf)) == OK) {
257             SP_PARM->_cbreak = 0;
258             cur_term->Nttyb = buf;
259         }
260         AFTER("nocbreak");
261     }
262     returnCode(result);
263 }
264
265 #if NCURSES_SP_FUNCS
266 NCURSES_EXPORT(int)
267 nocbreak(void)
268 {
269     return NCURSES_SP_NAME(nocbreak) (CURRENT_SCREEN);
270 }
271 #endif
272
273 /*
274  * Note:
275  * this implementation may be wrong.  See the comment under intrflush().
276  */
277 NCURSES_EXPORT(void)
278 NCURSES_SP_NAME(noqiflush) (NCURSES_SP_DCL0)
279 {
280     int result = ERR;
281
282     T((T_CALLED("noqiflush()")));
283
284     if (cur_term != 0) {
285         TTY buf;
286
287         BEFORE("noqiflush");
288         buf = cur_term->Nttyb;
289 #ifdef TERMIOS
290         buf.c_lflag |= NOFLSH;
291         result = _nc_set_tty_mode(&buf);
292 #else
293         /* FIXME */
294 #endif
295         if (result == OK) {
296             cur_term->Nttyb = buf;
297         }
298         AFTER("noqiflush");
299     }
300     returnVoid;
301 }
302
303 #if NCURSES_SP_FUNCS
304 NCURSES_EXPORT(void)
305 noqiflush(void)
306 {
307     NCURSES_SP_NAME(noqiflush) (CURRENT_SCREEN);
308 }
309 #endif
310
311 /*
312  * This call does the same thing as the qiflush()/noqiflush() pair.  We know
313  * for certain that SVr3 intrflush() tweaks the NOFLSH bit; on the other hand,
314  * the match (in the SVr4 man pages) between the language describing NOFLSH in
315  * termio(7) and the language describing qiflush()/noqiflush() in
316  * curs_inopts(3x) is too exact to be coincidence.
317  */
318 NCURSES_EXPORT(int)
319 NCURSES_SP_NAME(intrflush) (NCURSES_SP_DCLx WINDOW *win GCC_UNUSED, bool flag)
320 {
321     int result = ERR;
322
323     T((T_CALLED("intrflush(%d)"), flag));
324
325     if (cur_term != 0) {
326         TTY buf;
327
328         BEFORE("intrflush");
329         buf = cur_term->Nttyb;
330 #ifdef TERMIOS
331         if (flag)
332             buf.c_lflag &= ~(NOFLSH);
333         else
334             buf.c_lflag |= (NOFLSH);
335         result = _nc_set_tty_mode(&buf);
336 #else
337         /* FIXME */
338 #endif
339         if (result == OK) {
340             cur_term->Nttyb = buf;
341         }
342         AFTER("intrflush");
343     }
344     returnCode(result);
345 }
346
347 #if NCURSES_SP_FUNCS
348 NCURSES_EXPORT(int)
349 intrflush(WINDOW *win GCC_UNUSED, bool flag)
350 {
351     return NCURSES_SP_NAME(intrflush) (CURRENT_SCREEN, win, flag);
352 }
353 #endif