2 /* This work is copyrighted. See COPYRIGHT.OLD & COPYRIGHT.NEW for *
3 * details. If they are missing then this copy is in violation of *
4 * the copyright conditions. */
9 ** The routine getch().
13 #include <sys/types.h>
17 #if defined(BRAINDEAD)
20 #include "curses.priv.h"
22 #define head SP->_fifohead
23 #define tail SP->_fifotail
24 #define peek SP->_fifopeek
26 #define h_inc() { head == FIFO_SIZE-1 ? head = 0 : head++; if (head == tail) head = -1, tail = 0;}
27 #define h_dec() { head == 0 ? head = FIFO_SIZE-1 : head--; if (head == tail) tail = -1;}
28 #define t_inc() { tail == FIFO_SIZE-1 ? tail = 0 : tail++; if (tail == head) tail = -1;}
29 #define p_inc() { peek == FIFO_SIZE-1 ? peek = 0 : peek++;}
31 static int fifo_peek()
33 T(("peeking at %d", peek+1));
34 return SP->_fifo[++peek];
37 static inline void fifo_dump()
40 T(("head = %d, tail = %d, peek = %d", head, tail, peek));
41 for (i = 0; i < 10; i++)
42 T(("char %d = %d (%c)", i, SP->_fifo[i], (unsigned char)SP->_fifo[i]));
45 static inline int fifo_pull()
49 T(("pulling %d from %d", ch, head));
72 static inline int fifo_push()
77 if (tail == -1) return ERR;
79 n = read(fileno(SP->_ifp), &ch, 1);
80 if (n == -1 && errno == EINTR)
82 T(("read %d characters", n));
84 if (head == -1) head = tail;
86 T(("pushed %d at %d", ch, tail));
91 static inline void fifo_clear()
94 for (i = 0; i < FIFO_SIZE; i++)
96 head = -1; tail = peek = 0;
99 static int kgetch(WINDOW *);
104 bool setHere = FALSE; /* cbreak mode was set here */
107 T(("wgetch(%x) called", win));
109 /* this should be eliminated */
110 if (! win->_scroll && (SP->_echo) && (win->_flags & _FULLWIN)
111 && win->_curx == win->_maxx && win->_cury == win->_maxy)
114 if ((is_wintouched(win) || (win->_flags & _HASMOVED)) &&
115 !(win->_flags & _ISPAD))
118 if (SP->_echo && ! (SP->_raw || SP->_cbreak)) {
123 if (win->_delay >= 0 || SP->_cbreak > 1) {
126 T(("timed delay in wgetch()"));
128 delay = (SP->_cbreak-1) * 100;
132 T(("delay is %d microseconds", delay));
134 if (head == -1) /* fifo is empty */
135 if (timed_wait(fileno(SP->_ifp), delay, NULL) == 0)
137 /* else go on to read data available */
140 if (win->_use_keypad)
148 /* This should be eliminated */
149 /* handle 8-bit input */
154 /* there must be a simpler way of doing this */
155 if (!(win->_flags & _ISPAD) &&
156 SP->_echo && ch < 0400) { /* ch < 0400 => not a keypad key */
157 mvwaddch(curscr, win->_begy + win->_cury,
158 win->_begx + win->_curx, ch | win->_attrs);
159 waddch(win, ch | win->_attrs);
164 T(("wgetch returning : '%c', '0x%x'", ch, ch));
174 ** Get an input character, but take care of keypad sequences, returning
175 ** an appropriate code when one matches the input. After each character
176 ** is received, set a one-second alarm call. If no more of the sequence
177 ** is received by the time the alarm goes off, pass through the sequence
189 T(("kgetch(%x) called", win));
196 while (ptr != NULL) {
198 while ((ptr != NULL) && (ptr->ch != (unsigned char)ch))
202 if (ptr->value != 0) { /* sequence terminated */
203 T(("end of sequence"));
206 } else { /* go back for another character */
208 T(("going back for more"));
212 T(("waiting for rest of sequence"));
213 if (timed_wait(fileno(SP->_ifp), timeleft, &timeleft) < 1) {
214 T(("ran out of time"));