1 /*
2  * $Id: evtest.c,v 1.3 2005/08/15 20:43:52 js Exp $
3  *
4  *  Copyright (c) 1999-2000 Vojtech Pavlik
5  *
6  *  Event device test program
7  */
8 
9 /*
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  * Should you need to contact me, the author, you can do so either by
25  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
26  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
27  */
28 
29 #include <linux/input.h>
30 
31 #include <string.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 
37 char *events[EV_MAX + 1] = { "Sync", "Key", "Relative", "Absolute", "Misc", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
38 NULL, NULL, NULL, "LED", "Sound", NULL, "Repeat", "ForceFeedback", "Power", "ForceFeedbackStatus"};
39 char *syncs[2] = { "Sync", "Config"};
40 char *keys[KEY_MAX + 1] = {
41   // 0x000
42   "Reserved", "Esc", "1", "2", "3", "4", "5", "6",
43   "7", "8", "9", "0", "Minus", "Equal", "Backspace", "Tab",
44   // 0x010
45   "Q", "W", "E", "R", "T", "Y", "U", "I",
46   "O", "P", "LeftBrace", "RightBrace", "Enter", "LeftControl", "A", "S",
47   // 0x020
48   "D", "F", "G", "H", "J", "K", "L", "Semicolon",
49   "Apostrophe", "Grave", "LeftShift", "BackSlash", "Z", "X", "C", "V",
50   // 0x030
51   "B", "N", "M", "Comma", "Dot", "Slash", "RightShift", "KPAsterisk",
52   "LeftAlt", "Space", "CapsLock", "F1", "F2", "F3", "F4", "F5",
53   // 0x040
54   "F6", "F7", "F8", "F9", "F10", "NumLock", "ScrollLock", "KP7",
55   "KP8", "KP9", "KPMinus", "KP4", "KP5", "KP6", "KPPlus", "KP1",
56   // 0x050
57   "KP2", "KP3", "KP0", "KPDot", NULL, "ZENKAKUHANKAKU", "102nd", "F11",
58   "F12", "RO", "KATAKANA", "HIRAGANA", "HENKAN", "KATAKANAHIRAGANA", "MUHENKAN", "KPJPCOMMA",
59   // 0x060
60   "KPEnter", "RightCtrl", "KPSlash", "SysRq", "RightAlt", "LineFeed", "Home", "Up",
61   "PageUp", "Left", "Right", "End", "Down", "PageDown", "Insert", "Delete",
62   // 0x070
63   "Macro", "Mute", "VolumeDown", "VolumeUp", "Power", "KPEqual", "KPPlusMinus", "Pause",
64   NULL, "KPComma", "HANGUEL", "HANJA", "YEN", "LeftMeta", "RightMeta", "Compose",
65   // 0x080
66   "Stop", "Again", "Props", "Undo", "Front", "Copy", "Open", "Paste",
67   "Find", "Cut", "Help", "Menu", "Calc", "Setup", "Sleep", "WakeUp",
68   // 0x090
69   "File", "SendFile", "DeleteFile", "X-fer", "Prog1", "Prog2", "WWW", "MSDOS",
70   "Coffee", "Direction", "CycleWindows", "Mail", "Bookmarks", "Computer", "Back", "Forward",
71   // 0x0A0
72   "CloseCD", "EjectCD", "EjectCloseCD", "NextSong", "PlayPause", "PreviousSong", "StopCD", "Record",
73   "Rewind", "Phone", "ISOKey", "Config", "HomePage", "Refresh", "Exit", "Move",
74   // 0x0B0
75   "Edit", "ScrollUp", "ScrollDown", "KPLeftParenthesis", "KPRightParenthesis", NULL, NULL, "F13",
76   "F14", "F15", "F16", "F17", "F18", "F19", "F20", "F21",
77   // 0x0C0
78   "F22", "F23", "F24", NULL, NULL, NULL, NULL, NULL,
79   "PlayCD", "PauseCD", "Prog3", "Prog4", NULL, "Suspend", "Close", "Play",
80   // 0x0D0
81   "FastForward", "BassBoost", "Print", "HP", "Camera", "Sound", "Question", "EMail",
82   "Chat", "Search", "Connect", "Finance", "Sport", "Shop", "AltErase", "Cancel",
83   // 0x0E0
84   "BrightnessDown", "BrightnessUp", "Media", NULL, NULL, NULL, NULL, NULL,
85   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
86   // 0x0F0
87   "Unknown", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
88   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
89   // 0x100
90   "Btn0", "Btn1", "Btn2", "Btn3", "Btn4", "Btn5", "Btn6", "Btn7",
91   "Btn8", "Btn9", NULL, NULL,  NULL, NULL, NULL, NULL,
92   // 0x110
93   "LeftBtn", "RightBtn", "MiddleBtn", "SideBtn", "ExtraBtn", "ForwardBtn", "BackBtn", "TaskBtn",
94   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
95   // 0x120
96   "Trigger", "ThumbBtn", "ThumbBtn2", "TopBtn", "TopBtn2", "PinkieBtn", "BaseBtn", "BaseBtn2",
97   "BaseBtn3", "BaseBtn4", "BaseBtn5", "BaseBtn6", NULL, NULL, NULL, "BtnDead",
98   // 0x130
99   "BtnA", "BtnB", "BtnC", "BtnX", "BtnY", "BtnZ", "BtnTL", "BtnTR",
100   "BtnTL2", "BtnTR2", "BtnSelect", "BtnStart", "BtnMode", "BtnThumbL", "BtnThumbR", NULL,
101   // 0x140
102   "ToolPen", "ToolRubber", "ToolBrush", "ToolPencil", "ToolAirbrush", "ToolFinger", "ToolMouse", "ToolLens",
103   NULL, NULL, "Touch", "Stylus", "Stylus2", "ToolDoubleTap", "ToolTripleTap", NULL,
104   // 0x150
105   "GearDown", "GearUp", NULL, NULL, NULL, NULL, NULL, NULL,
106   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
107   // 0x160
108   "Ok", "Select", "Goto", "Clear", "Power2", "Option", "Info", "Time",
109   "Vendor", "Archive", "Program", "Channel", "Favorites", "EPG", "PVR", "MHP",
110   // 0x170
111   "Language", "Title", "Subtitle", "Angle", "Zoom", "Mode", "Keyboard", "Screen",
112   "PC", "TV", "TV2", "VCR", "VCR2", "Sat", "Sat2", "CD",
113   // 0x180
114   "Tape", "Radio", "Tuner", "Player", "Text", "DVD", "Aux", "MP3",
115   "Audio", "Video", "Directory", "List", "Memo", "Calendar", "Red", "Green",
116   // 0x190
117   "Yellow", "Blue", "ChannelUp", "ChannelDown", "First", "Last", "AB", "Next",
118   "Restart", "Slow", "Shuffle", "Break", "Previous", "Digits", "Teen", "Twen",
119   // 0x1A0
120   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
121   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
122   // 0x1B0
123   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
124   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
125   // 0x1C0
126   "DelEOL", "DelEOS", "InsLine", "DelLine", NULL, NULL, NULL, NULL,
127   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
128   // 0x1D0
129   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
130   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
131   // 0x1E0
132   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
133   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
134   // 0x1F0
135   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
136   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
137 };
138 
139 char *absval[5] = { "Value", "Min  ", "Max  ", "Fuzz ", "Flat " };
140 char *relatives[REL_MAX + 1] = {
141   "X", "Y", "Z", NULL, NULL, NULL, "HWheel", "Dial",
142   "Wheel", "Misc", NULL, NULL, NULL, NULL, NULL, NULL,
143 };
144 char *absolutes[ABS_MAX + 1] = {
145   // 0x00
146   "X", "Y", "Z", "Rx", "Ry", "Rz", "Throttle", "Rudder",
147   "Wheel", "Gas", "Brake", NULL, NULL, NULL, NULL, NULL,
148   // 0x10
149   "Hat0X", "Hat0Y", "Hat1X", "Hat1Y", "Hat2X", "Hat2Y", "Hat3X", "Hat 3Y",
150   "Pressure", "Distance", "XTilt", "YTilt", "ToolWidth", NULL, NULL, NULL,
151   // 0x20
152   "Volume", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
153   "Misc", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
154   // 0x30
155   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
156   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
157 };
158 char *leds[LED_MAX + 1] = { "NumLock", "CapsLock", "ScrollLock", "Compose", "Kana", "Sleep", "Suspend", "Mute" };
159 char *repeats[REP_MAX + 1] = { "Delay", "Period" };
160 char *sounds[SND_MAX + 1] = { "Bell", "Click" };
161 
162 char **names[EV_MAX + 1] = { syncs, keys, relatives, absolutes, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
163 NULL, NULL, leds, sounds, NULL, repeats, NULL, NULL, NULL };
164 
165 #ifndef BITS_PER_LONG
166 #define BITS_PER_LONG (sizeof(long) * 8)
167 #endif
168 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
169 #define OFF(x)  ((x)%BITS_PER_LONG)
170 #define BIT(x)  (1UL<<OFF(x))
171 #define LONG(x) ((x)/BITS_PER_LONG)
172 #define test_bit(bit, array)	((array[LONG(bit)] >> OFF(bit)) & 1)
173 
main(int argc,char ** argv)174 int main (int argc, char **argv)
175 {
176 	int fd, rd, i, j, k;
177 	struct input_event ev[64];
178 	int version;
179 	unsigned short id[4];
180 	unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
181 	char name[256] = "Unknown";
182 	int _abs[5];
183 
184 	if (argc < 2) {
185 		printf("Usage: evtest /dev/input/eventX\n");
186 		printf("Where X = input device number\n");
187 		exit(1);
188 	}
189 
190 	if ((fd = open(argv[argc - 1], O_RDONLY)) < 0) {
191 		perror("evtest");
192 		exit(1);
193 	}
194 
195 	if (ioctl(fd, EVIOCGVERSION, &version)) {
196 		perror("evtest: can't get version");
197 		exit(1);
198 	}
199 
200 	printf("Input driver version is %d.%d.%d\n",
201 		version >> 16, (version >> 8) & 0xff, version & 0xff);
202 
203 	ioctl(fd, EVIOCGID, id);
204 	printf("Input device ID: bus 0x%x vendor 0x%x product 0x%x version 0x%x\n",
205 		id[ID_BUS], id[ID_VENDOR], id[ID_PRODUCT], id[ID_VERSION]);
206 
207 	ioctl(fd, EVIOCGNAME(sizeof(name)), name);
208 	printf("Input device name: \"%s\"\n", name);
209 
210 	memset(bit, 0, sizeof(bit));
211 	ioctl(fd, EVIOCGBIT(0, EV_MAX), bit[0]);
212 	printf("Supported events:\n");
213 
214 	for (i = 0; i < EV_MAX; i++)
215 		if (test_bit(i, bit[0])) {
216 			printf("  Event type %d (%s)\n", i, events[i] ? events[i] : "?");
217 			ioctl(fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
218 			for (j = 0; j < KEY_MAX; j++)
219 				if (test_bit(j, bit[i])) {
220 					printf("    Event code %d (%s)\n", j, names[i] ? (names[i][j] ? names[i][j] : "?") : "?");
221 					if (i == EV_ABS) {
222 						ioctl(fd, EVIOCGABS(j), _abs);
223 						for (k = 0; k < 5; k++)
224 							if ((k < 3) || _abs[k])
225 								printf("      %s %6d\n", absval[k], _abs[k]);
226 					}
227 				}
228 		}
229 
230 
231 	printf("Testing ... (interrupt to exit)\n");
232 
233 	while (1) {
234 		rd = read(fd, ev, sizeof(struct input_event) * 64);
235 
236 		if (rd < (int) sizeof(struct input_event)) {
237 			printf("yyy\n");
238 			perror("\nevtest: error reading");
239 			exit (1);
240 		}
241 
242 		for (i = 0; i < rd / (int) sizeof(struct input_event); i++)
243 			printf("Event: time %ld.%06ld, type %d (%s), code %d (%s), value %d\n",
244 				ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].type,
245 				events[ev[i].type] ? events[ev[i].type] : "?",
246 				ev[i].code,
247 				names[ev[i].type] ? (names[ev[i].type][ev[i].code] ? names[ev[i].type][ev[i].code] : "?") : "?",
248 				ev[i].value);
249 
250 	}
251 }
252