]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/services/gfx/mw/v2_0/src/drivers/kbd_tty.c
Initial revision
[karo-tx-redboot.git] / packages / services / gfx / mw / v2_0 / src / drivers / kbd_tty.c
1 /*
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.
6  *
7  * /dev/tty TTY Keyboard Driver
8  */
9 #include <stdlib.h>
10 #include <sys/types.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <errno.h>
14 #include <termios.h>
15 #include "device.h"
16
17 #if ELKS
18 #define KEYBOARD        "/dev/tty1"     /* keyboard associated with screen*/
19 #else
20 #define KEYBOARD        "/dev/tty"      /* keyboard associated with screen*/
21 #endif
22
23 extern int escape_quits;
24
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);
30
31 KBDDEVICE kbddev = {
32         TTY_Open,
33         TTY_Close,
34         TTY_GetModifierInfo,
35         TTY_Read,
36 #if _MINIX
37         TTY_Poll
38 #else
39         NULL
40 #endif
41 };
42
43 static  int             fd;             /* file descriptor for keyboard */
44 static  struct termios  old;            /* original terminal modes */
45
46 /*
47  * Open the keyboard.
48  * This is real simple, we just use a special file handle
49  * that allows non-blocking I/O, and put the terminal into
50  * character mode.
51  */
52 static int
53 TTY_Open(KBDDEVICE *pkd)
54 {
55   char *env;
56
57         int             i;
58         int             ledstate = 0;
59         struct termios  new;
60
61         /* Open "CONSOLE" or /dev/tty device*/
62         if(!(env = getenv("CONSOLE")))
63                 fd = open(KEYBOARD, O_NONBLOCK);
64         else
65                 fd = open(env, O_NONBLOCK);
66         if (fd < 0)
67                 return -1;
68
69         if (tcgetattr(fd, &old) < 0)
70                 goto err;
71
72         new = old;
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);
77         new.c_cflag |= CS8;
78         new.c_cc[VMIN] = 0;
79         new.c_cc[VTIME] = 0;
80
81         if(tcsetattr(fd, TCSAFLUSH, &new) < 0)
82                 goto err;
83         return fd;
84
85 err:
86         close(fd);
87         fd = 0;
88         return -1;
89 }
90
91 /*
92  * Close the keyboard.
93  * This resets the terminal modes.
94  */
95 static void
96 TTY_Close(void)
97 {
98         tcsetattr(fd, TCSANOW, &old);
99         close(fd);
100         fd = 0;
101 }
102
103 /*
104  * Return the possible modifiers for the keyboard.
105  */
106 static  void
107 TTY_GetModifierInfo(MWKEYMOD *modifiers, MWKEYMOD *curmodifiers)
108 {
109         if (modifiers)
110                 *modifiers = 0;         /* no modifiers available */
111         if (curmodifiers)
112                 *curmodifiers = 0;
113 }
114
115 /*
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.
120  */
121 static int
122 TTY_Read(MWKEY *kbuf, MWKEYMOD *modifiers, MWSCANCODE *scancode)
123 {
124         int     cc;                     /* characters read */
125         MWKEY   mwkey;
126         unsigned char buf[1];
127
128         cc = read(fd, buf, 1);
129         if (cc > 0) {
130                 mwkey = buf[0];
131                 if (mwkey == 27 && escape_quits)        /* ESC -> quit*/
132                         mwkey = MWKEY_QUIT;
133                 else if (mwkey == ('P'&0x1f))           /* ^P -> print*/
134                         mwkey = MWKEY_PRINT;
135
136                 *kbuf = mwkey;          /* no translation*/
137                 *modifiers = 0;         /* no modifiers*/
138                 *scancode = 0;          /* no scancode*/
139                 return 1;               /* keypress*/
140         }
141         if ((cc < 0) && (errno != EINTR) && (errno != EAGAIN)) {
142                 return -1;
143         }
144         return 0;
145 }
146
147 static int
148 TTY_Poll(void)
149 {
150         return 1;       /* used by _MINIX only*/
151 }