|
|
1.1 root 1: /*
2: * Copyright (c) 1981 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
6: * provided that the above copyright notice and this paragraph are
7: * duplicated in all such forms and that any documentation,
8: * advertising materials, and other materials related to such
9: * distribution and use acknowledge that the software was developed
10: * by the University of California, Berkeley. The name of the
11: * University may not be used to endorse or promote products derived
12: * from this software without specific prior written permission.
13: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15: * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16: */
17:
18: #ifndef COHERENT
19: #ifndef lint
20: static uchar sccsid[] = "@(#)getch.c 5.5 (Berkeley) 6/30/88";
21: #endif /* not lint */
22: #endif /* not COHERENT */
23:
24: # include <fcntl.h>
25: # include "curses.ext"
26:
27: /*
28: * This routine reads in a character from the window.
29: *
30: */
31: wgetch(win)
32: reg WINDOW *win; {
33:
34: reg bool weset = FALSE;
35: reg int inp;
36: reg int fflgs; /* fcntl flags to be restored */
37: reg int fmods; /* fcntl flags were modified */
38:
39: if (!win->_scroll && (win->_flags&_FULLWIN)
40: && win->_curx == win->_maxx - 1 && win->_cury == win->_maxy - 1)
41: return ERR;
42: # ifdef DEBUG
43: fprintf(outf, "WGETCH: _echoit = %c, _rawmode = %c\n", _echoit ? 'T' : 'F', _rawmode ? 'T' : 'F');
44: # endif
45: #ifndef COHERENT
46: if (_echoit && !_rawmode) {
47: cbreak();
48: weset++;
49: }
50:
51: /*
52: * Obtain the fcntl flags.
53: */
54: fflgs = fcntl( fileno(stdin), F_GETFL, 0 );
55: fmods = 0;
56:
57: /*
58: * Non-blocking input is required, fcntl flags may have to be changed.
59: */
60: if ( win->_nodelay ) {
61: if ( (fflgs & O_NDELAY) == 0 ) {
62: fcntl( fileno(stdin), F_SETFL, fflgs | O_NDELAY );
63: fmods++;
64: }
65: }
66:
67: /*
68: * Blocking input is required, fcntl flags may have to be changed.
69: */
70: else {
71: if ( (fflgs & O_NDELAY) != 0 ) {
72: fcntl( fileno(stdin), F_SETFL, fflgs & ~O_NDELAY );
73: fmods++;
74: }
75: }
76: #endif
77: /*
78: * Get next character, or keyboard token.
79: * NOTE: Valid characters are in range 1 to 127.
80: * Valid keyboard tokens start at 256 [0400].
81: */
82: if ( win->_use_keypad )
83: inp = getkey();
84:
85: /*
86: * Get next character, without token translation.
87: */
88: else
89: inp = getchar();
90:
91: if ( (inp < 0200) && (inp > 0) ) {
92: # ifdef DEBUG
93: fprintf(outf,"WGETCH got '%s'\n",unctrl(inp));
94: # endif
95: if (_echoit) {
96: mvwaddch(curscr, win->_cury + win->_begy,
97: win->_curx + win->_begx, inp);
98: waddch(win, inp);
99: }
100: }
101: # ifdef DEBUG
102: else {
103: fprintf( outf, "WGETCH got 0%o\n", inp );
104: }
105: #endif
106:
107: #ifndef COHERENT
108: /*
109: * Fcntl flags were modified - restore previous value.
110: */
111: if ( fmods )
112: fcntl( fileno(stdin), F_SETFL, fflgs );
113:
114: if (weset)
115: nocbreak();
116: #endif
117: return inp;
118: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.