xref: /netbsd-src/share/examples/rump/ukbd_read/kbd.c (revision 4737161def5c458e7c6ed6e7d2f661a507019cf0)
1 /*      $NetBSD: kbd.c,v 1.1 2010/01/11 02:16:51 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * Experimental proof-of-concept program:
30  *
31  * Read keyboard events from a USB keyboard using rump drivers.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/time.h>
36 
37 #include <dev/wscons/wsconsio.h>
38 
39 #include <rump/rump.h>
40 #include <rump/rump_syscalls.h>
41 
42 #include <err.h>
43 #include <paths.h>
44 #include <string.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 
48 #define SHIFT (-0x20)
49 
50 int
main(int argc,char * argv[])51 main(int argc, char *argv[])
52 {
53 	struct wscons_event *wev;
54 	int shift = 0;
55 	char buf[128];
56 	int fd;
57 
58 	rump_boot_sethowto(RUMP_AB_VERBOSE);
59 	rump_init();
60 
61 	fd = rump_sys_open("/dev/wskbd", 0);
62 	if (fd == -1)
63 		err(1, "open");
64 
65 	while (rump_sys_read(fd, buf, sizeof(buf)) > 0) {
66 		const char *typestr;
67 
68 		/* XXX: timespec in 5.0 vs. -current */
69 		wev = (void *)buf;
70 
71 		switch (wev->type) {
72 		case WSCONS_EVENT_KEY_UP:
73 			typestr = "up";
74 			if (wev->value == 0xe1 || wev->value == 0xe5)
75 				shift = 0;
76 			break;
77 		case WSCONS_EVENT_KEY_DOWN:
78 			typestr = "down";
79 			if (wev->value == 0xe1 || wev->value == 0xe5)
80 				shift = SHIFT;
81 			break;
82 		default:
83 			typestr = "unknown";
84 			break;
85 		}
86 		printf("event type: %d (%s)\n", wev->type, typestr);
87 		printf("value 0x%x", wev->value);
88 		/*
89 		 * There's probably a value-to-readable tool somewhere
90 		 * in the tree, but i'm not sure where or how to use it,
91 		 * so I'll just punt with the supersimple version for now.
92 		 */
93 		if (wev->value >= 0x04 && wev->value <= 0x1d)
94 			printf(" (%c)", wev->value - 0x04 + 'a' + shift);
95 		printf("\n");
96 	}
97 }
98