2 * Copyright (c) 1999 Greg Haerr <greg@censoft.com>
3 * Copyright (c) 1991 David I. Bell
4 * Permission is granted to use, distribute, or modify this source,
5 * provided that this copyright notice remains intact.
7 * /dev/tty TTY Keyboard Driver
10 #include <sys/types.h>
18 #define KEYBOARD "/dev/tty1" /* keyboard associated with screen*/
20 #define KEYBOARD "/dev/tty" /* keyboard associated with screen*/
23 extern int escape_quits;
25 static int TTY_Open(KBDDEVICE *pkd);
26 static void TTY_Close(void);
27 static void TTY_GetModifierInfo(MWKEYMOD *modifiers, MWKEYMOD *curmodifiers);
28 static int TTY_Read(MWKEY *kbuf, MWKEYMOD *modifiers, MWSCANCODE *scancode);
29 static int TTY_Poll(void);
43 static int fd; /* file descriptor for keyboard */
44 static struct termios old; /* original terminal modes */
48 * This is real simple, we just use a special file handle
49 * that allows non-blocking I/O, and put the terminal into
53 TTY_Open(KBDDEVICE *pkd)
61 /* Open "CONSOLE" or /dev/tty device*/
62 if(!(env = getenv("CONSOLE")))
63 fd = open(KEYBOARD, O_NONBLOCK);
65 fd = open(env, O_NONBLOCK);
69 if (tcgetattr(fd, &old) < 0)
73 /* If you uncomment ISIG and BRKINT below, then ^C will be ignored.*/
74 new.c_lflag &= ~(ECHO | ICANON | IEXTEN /*| ISIG*/);
75 new.c_iflag &= ~(ICRNL | INPCK | ISTRIP | IXON /*| BRKINT*/);
76 new.c_cflag &= ~(CSIZE | PARENB);
81 if(tcsetattr(fd, TCSAFLUSH, &new) < 0)
93 * This resets the terminal modes.
98 tcsetattr(fd, TCSANOW, &old);
104 * Return the possible modifiers for the keyboard.
107 TTY_GetModifierInfo(MWKEYMOD *modifiers, MWKEYMOD *curmodifiers)
110 *modifiers = 0; /* no modifiers available */
116 * This reads one keystroke from the keyboard, and the current state of
117 * the modifier keys (ALT, SHIFT, etc). Returns -1 on error, 0 if no data
118 * is ready, 1 on a keypress, and 2 on keyrelease.
119 * This is a non-blocking call.
122 TTY_Read(MWKEY *kbuf, MWKEYMOD *modifiers, MWSCANCODE *scancode)
124 int cc; /* characters read */
126 unsigned char buf[1];
128 cc = read(fd, buf, 1);
131 if (mwkey == 27 && escape_quits) /* ESC -> quit*/
133 else if (mwkey == ('P'&0x1f)) /* ^P -> print*/
136 *kbuf = mwkey; /* no translation*/
137 *modifiers = 0; /* no modifiers*/
138 *scancode = 0; /* no scancode*/
139 return 1; /* keypress*/
141 if ((cc < 0) && (errno != EINTR) && (errno != EAGAIN)) {
150 return 1; /* used by _MINIX only*/