4 * Copyright (C) 2004, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: keyboard.c,v 1.13 2007/06/19 23:47:18 tbox Exp */
24 #include <sys/param.h>
25 #include <sys/types.h>
36 #include <isc/keyboard.h>
40 isc_keyboard_open(isc_keyboard_t
*keyboard
) {
43 struct termios current_mode
;
45 REQUIRE(keyboard
!= NULL
);
47 fd
= open("/dev/tty", O_RDONLY
, 0);
49 return (ISC_R_IOERROR
);
53 if (tcgetattr(fd
, &keyboard
->saved_mode
) < 0) {
58 current_mode
= keyboard
->saved_mode
;
60 current_mode
.c_iflag
&=
61 ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
62 current_mode
.c_oflag
&= ~OPOST
;
63 current_mode
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
64 current_mode
.c_cflag
&= ~(CSIZE
|PARENB
);
65 current_mode
.c_cflag
|= CS8
;
67 current_mode
.c_cc
[VMIN
] = 1;
68 current_mode
.c_cc
[VTIME
] = 0;
69 if (tcsetattr(fd
, TCSAFLUSH
, ¤t_mode
) < 0) {
74 keyboard
->result
= ISC_R_SUCCESS
;
76 return (ISC_R_SUCCESS
);
85 isc_keyboard_close(isc_keyboard_t
*keyboard
, unsigned int sleeptime
) {
86 REQUIRE(keyboard
!= NULL
);
88 if (sleeptime
> 0 && keyboard
->result
!= ISC_R_CANCELED
)
89 (void)sleep(sleeptime
);
91 (void)tcsetattr(keyboard
->fd
, TCSAFLUSH
, &keyboard
->saved_mode
);
92 (void)close(keyboard
->fd
);
96 return (ISC_R_SUCCESS
);
100 isc_keyboard_getchar(isc_keyboard_t
*keyboard
, unsigned char *cp
) {
105 REQUIRE(keyboard
!= NULL
);
108 cc
= read(keyboard
->fd
, &c
, 1);
110 keyboard
->result
= ISC_R_IOERROR
;
111 return (keyboard
->result
);
114 controlchars
= keyboard
->saved_mode
.c_cc
;
115 if (c
== controlchars
[VINTR
] || c
== controlchars
[VQUIT
]) {
116 keyboard
->result
= ISC_R_CANCELED
;
117 return (keyboard
->result
);
122 return (ISC_R_SUCCESS
);
126 isc_keyboard_canceled(isc_keyboard_t
*keyboard
) {
127 return (ISC_TF(keyboard
->result
== ISC_R_CANCELED
));