2 * ========================================================================
3 * Copyright 2006 University of Washington
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * ========================================================================
15 * Keyboard input test and read functions
18 #include <system.h> /* os-dep defs/includes */
19 #include <general.h> /* generally useful definitions */
21 #include "../keydefs.h"
25 #endif /* !_WINDOWS */
30 static time_t _time_of_last_input
;
33 time_of_last_input(void)
35 if(_time_of_last_input
== 0)
36 _time_of_last_input
= time((time_t *) 0);
38 return(_time_of_last_input
);
44 #ifdef HAVE_SYS_SELECT_H
45 # include <sys/select.h>
50 * Check whether or not a character is ready to be read, or time out.
51 * This version uses a select call.
53 * Args: time_out -- number of seconds before it will time out.
55 * Result: NO_OP_IDLE: timed out before any chars ready, time_out > 25
56 * NO_OP_COMMAND: timed out before any chars ready, time_out <= 25
57 * READ_INTR: read was interrupted
58 * READY_TO_READ: input is available
59 * BAIL_OUT: reading input failed, time to bail gracefully
60 * PANIC_NOW: system call error, die now
63 input_ready(int time_out
)
66 fd_set readfds
, errfds
;
72 /* Check to see if there are bytes to read with a timeout */
75 FD_SET(STDIN_FD
, &readfds
);
76 FD_SET(STDIN_FD
, &errfds
);
77 tmo
.tv_sec
= time_out
;
79 res
= select(STDIN_FD
+1, &readfds
, 0, &errfds
, &tmo
);
81 if(errno
== EINTR
|| errno
== EAGAIN
)
87 if(res
== 0){ /* the select timed out */
96 * "15" is the minimum allowed mail check interval.
97 * Anything less, and we're being told to cycle thru
98 * the command loop because some task is pending...
100 return(time_out
< IDLE_TIMEOUT
? NO_OP_COMMAND
: NO_OP_IDLE
);
104 _time_of_last_input
= time((time_t *) 0);
105 return(READY_TO_READ
);
112 #ifdef HAVE_STROPTS_H
113 # include <stropts.h>
116 #ifdef HAVE_SYS_POLL_H
121 * Check whether or not a character is ready to be read, or time out.
122 * This version uses a poll call.
124 * Args: time_out -- number of seconds before it will time out.
126 * Result: NO_OP_IDLE: timed out before any chars ready, time_out > 25
127 * NO_OP_COMMAND: timed out before any chars ready, time_out <= 25
128 * READ_INTR: read was interrupted
129 * READY_TO_READ: input is available
130 * BAIL_OUT: reading input failed, time to bail gracefully
131 * PANIC_NOW: system call error, die now
134 input_ready(int time_out
)
136 struct pollfd pollfd
;
142 /* Check to see if there are bytes to read with a timeout */
143 pollfd
.fd
= STDIN_FD
;
144 pollfd
.events
= POLLIN
;
145 res
= poll (&pollfd
, 1, time_out
* 1000);
146 if(res
>= 0){ /* status bits OK? */
147 if(pollfd
.revents
& (POLLERR
| POLLNVAL
))
148 res
= -1; /* bad news, exit below! */
149 else if(pollfd
.revents
& POLLHUP
)
154 if(errno
== EINTR
|| errno
== EAGAIN
)
160 if(res
== 0){ /* the select timed out */
162 /* Parent is init! */
167 * "15" is the minimum allowed mail check interval.
168 * Anything less, and we're being told to cycle thru
169 * the command loop because some task is pending...
171 return(time_out
< IDLE_TIMEOUT
? NO_OP_COMMAND
: NO_OP_IDLE
);
175 _time_of_last_input
= time((time_t *) 0);
176 return(READY_TO_READ
);
179 #endif /* HAVE_POLL */
183 * Read one character from STDIN.
185 * Result: -- the single character read
186 * READ_INTR -- read was interrupted
187 * BAIL_OUT -- read error of some sort
195 res
= read(STDIN_FD
, &c
, 1);
199 * Error reading from terminal!
200 * The only acceptable failure is being interrupted. If so,
201 * return a value indicating such...
203 if(res
< 0 && errno
== EINTR
)
214 set_time_of_last_input(void)
216 _time_of_last_input
= time(0L);
219 #endif /* _WINDOWS */