Added support for DE200C VFD
[lcdproc-de200c.git] / server / drivers / de200c-vfd / showkeys / showkey.c
blob8e06dfb4c7c3b4c92ee3c1abda140266011771e1
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <getopt.h>
4 #include <signal.h>
5 #include <fcntl.h>
6 #include <termios.h>
7 #include <time.h>
8 #include <linux/kd.h>
9 #include <linux/keyboard.h>
10 #include <sys/ioctl.h>
11 #include <errno.h>
12 #include "getfd.h"
13 #include "nls.h"
14 #include "version.h"
15 #include "vfd.h"
17 int tmp; /* for debugging */
19 int fd;
20 int oldkbmode;
21 struct termios old;
24 * version 0.81 of showkey would restore kbmode unconditially to XLATE,
25 * thus making the console unusable when it was called under X.
27 static void
28 get_mode(void) {
29 char *m;
31 if (ioctl(fd, KDGKBMODE, &oldkbmode)) {
32 perror("KDGKBMODE");
33 exit(1);
35 switch(oldkbmode) {
36 case K_RAW:
37 m = "RAW"; break;
38 case K_XLATE:
39 m = "XLATE"; break;
40 case K_MEDIUMRAW:
41 m = "MEDIUMRAW"; break;
42 case K_UNICODE:
43 m = "UNICODE"; break;
44 default:
45 m = _("?UNKNOWN?"); break;
47 printf(_("kb mode was %s\n"), m);
48 if (oldkbmode != K_XLATE) {
49 printf(_("[ if you are trying this under X, it might not work\n"
50 "since the X server is also reading /dev/console ]\n"));
52 printf("\n");
55 static void
56 clean_up(void) {
57 if (ioctl(fd, KDSKBMODE, oldkbmode)) {
58 perror("KDSKBMODE");
59 exit(1);
61 if (tcsetattr(fd, 0, &old) == -1)
62 perror("tcsetattr");
63 close(fd);
66 static void
67 die(int x) {
68 printf(_("caught signal %d, cleaning up...\n"), x);
69 clean_up();
70 exit(1);
73 static void
74 watch_dog(int x) {
75 clean_up();
76 exit(0);
79 static void
80 usage(void) {
81 fprintf(stderr, _(
82 "showkey version %s\n\n"
83 "usage: showkey [options...]\n"
84 "\n"
85 "valid options are:\n"
86 "\n"
87 " -h --help display this help text\n"
88 " -a --ascii display the decimal/octal/hex values of the keys\n"
89 " -s --scancodes display only the raw scan-codes\n"
90 " -k --keycodes display only the interpreted keycodes (default)\n"
91 ), VERSION);
92 exit(1);
95 int
96 main (int argc, char *argv[]) {
97 static char buffer[32];
98 const char *short_opts = "haskV";
99 const struct option long_opts[] = {
100 { "help", no_argument, NULL, 'h' },
101 { "ascii", no_argument, NULL, 'a' },
102 { "scancodes", no_argument, NULL, 's' },
103 { "keycodes", no_argument, NULL, 'k' },
104 { "version", no_argument, NULL, 'V' },
105 { NULL, 0, NULL, 0 }
107 int c;
108 int count = 0;
109 int show_keycodes = 1;
110 int print_ascii = 0;
111 int x, y, d;
112 float bright;
113 FILE *fortune = NULL;
114 int fscroll = 0;
115 char fbytes[160];
116 char *fptr;
118 struct termios new;
119 unsigned char buf[16];
120 int i, n;
122 set_progname(argv[0]);
124 setlocale(LC_ALL, "");
125 bindtextdomain(PACKAGE, LOCALEDIR);
126 textdomain(PACKAGE);
128 while ((c = getopt_long(argc, argv,
129 short_opts, long_opts, NULL)) != -1) {
130 switch (c) {
131 case 's':
132 show_keycodes = 0;
133 break;
134 case 'k':
135 show_keycodes = 1;
136 break;
137 case 'a':
138 print_ascii = 1;
139 break;
140 case 'V':
141 print_version_and_exit();
142 case 'h':
143 case '?':
144 usage();
148 if (optind < argc)
149 usage();
151 if (print_ascii) {
152 /* no mode and signal and timer stuff - just read stdin */
153 fd = 0;
155 if (tcgetattr(fd, &old) == -1)
156 perror("tcgetattr");
157 if (tcgetattr(fd, &new) == -1)
158 perror("tcgetattr");
160 new.c_lflag &= ~ (ICANON | ISIG);
161 new.c_lflag |= (ECHO | ECHOCTL);
162 new.c_iflag = 0;
163 new.c_cc[VMIN] = 1;
164 new.c_cc[VTIME] = 0;
166 if (tcsetattr(fd, TCSAFLUSH, &new) == -1)
167 perror("tcgetattr");
168 printf(_("\nPress any keys - "
169 "Ctrl-D will terminate this program\n\n"));
171 while (1) {
172 n = read(fd, buf, 1);
173 if (n == 1)
174 printf(" \t%3d 0%03o 0x%02x\n",
175 buf[0], buf[0], buf[0]);
176 if (n != 1 || buf[0] == 04)
177 break;
180 if (tcsetattr(fd, 0, &old) == -1)
181 perror("tcsetattr");
182 exit(0);
185 fd = getfd();
187 /* the program terminates when there is no input for 10 secs */
188 signal(SIGALRM, watch_dog);
191 if we receive a signal, we want to exit nicely, in
192 order not to leave the keyboard in an unusable mode
194 signal(SIGHUP, die);
195 signal(SIGINT, die);
196 signal(SIGQUIT, die);
197 signal(SIGILL, die);
198 signal(SIGTRAP, die);
199 signal(SIGABRT, die);
200 signal(SIGIOT, die);
201 signal(SIGFPE, die);
202 signal(SIGKILL, die);
203 signal(SIGUSR1, die);
204 signal(SIGSEGV, die);
205 signal(SIGUSR2, die);
206 signal(SIGPIPE, die);
207 signal(SIGTERM, die);
208 #ifdef SIGSTKFLT
209 signal(SIGSTKFLT, die);
210 #endif
211 // signal(SIGCHLD, die);
212 // signal(SIGCONT, die);
213 // signal(SIGSTOP, die);
214 // signal(SIGTSTP, die);
215 //signal(SIGTTIN, die);
216 //signal(SIGTTOU, die);
218 get_mode();
219 if (tcgetattr(fd, &old) == -1)
220 perror("tcgetattr");
221 if (tcgetattr(fd, &new) == -1)
222 perror("tcgetattr");
224 new.c_lflag &= ~ (ICANON | ECHO | ISIG);
225 new.c_iflag = 0;
226 new.c_cc[VMIN] = sizeof(buf);
227 new.c_cc[VTIME] = 1; /* 0.1 sec intercharacter timeout */
229 if (tcsetattr(fd, TCSAFLUSH, &new) == -1)
230 perror("tcsetattr");
231 if (ioctl(fd, KDSKBMODE, show_keycodes ? K_MEDIUMRAW : K_RAW)) {
232 perror("KDSKBMODE");
233 exit(1);
236 printf(_("Running...\n"));
237 VFD_Init();
238 VFD_Brightness(.3);
239 x = d = 0;
240 while (1) {
241 char *ptr = buf;
242 time_t t = time(NULL);
243 float load1, load2, load3;
244 int l, l1, l2, l3, s;
245 FILE *f = fopen("/proc/loadavg", "r");
247 // Display oscillating date/time
248 d++;
249 if (d & 16)
250 x--;
251 else
252 x++;
253 VFD_SetXY(x, 0);
254 VFD_Data(0);
255 VFD_Text(ctime(&t));
257 // Display load averages
258 fscanf(f, "%f %f %f", &load1, &load2, &load3);
259 fclose(f);
260 l1 = load1 * 120;
261 l2 = load2 * 120;
262 l3 = load3 * 120;
264 VFD_SetXY(0, 2);
265 VFD_Text("Load: ");
266 for (l = 0; l < 120; l++)
268 int b = 0;
269 if (l1 >= l) b |= 0x07;
270 if (l2 >= l) b |= 0x1C;
271 if (l3 >= l) b |= 0x70;
272 VFD_Data(b);
275 // Display current fortune
276 for (s = 0; s < 2; s++)
278 if (!fortune)
280 system ("/usr/games/fortune > /tmp/glop");
281 fortune = fopen("/tmp/glop", "r");
283 if (fscroll == 0)
285 c = fgetc(fortune);
286 if (c == EOF)
288 fclose (fortune);
289 fortune = NULL;
290 c = 0;
291 fscroll = 80;
293 else
295 fscroll = 6;
297 fptr = VFD_Font[c];
299 memmove(fbytes, fbytes+1, 159);
300 fbytes[159] = *fptr++;
301 fscroll--;
302 VFD_SetXY(0, 1);
304 for (l = 0; l < 160; l++)
305 VFD_Data(fbytes[l]);
307 // Read hex keys from console
308 n = read(fd, buf, sizeof(buf));
309 if (n < 0)
311 if (errno != EAGAIN)
312 perror("showkey:read");
313 n = 0;
315 count += n;
317 #define MAX 9
318 for (i = 0; i < n; i++)
320 memmove(buffer, buffer+1, MAX);
321 buffer[MAX] = *ptr++;
324 VFD_SetXY(0,3);
325 sprintf(buf, "%d:", count);
326 VFD_Text(buf);
327 for (i = 0; i < MAX; i++)
329 sprintf(buf, "%02X", (unsigned char) buffer[i]);
330 VFD_Text(buf);
331 VFD_Data(0);
332 VFD_Data(0);
334 VFD_Data(-1);
336 usleep(30 MS);
339 clean_up();
340 exit(0);