evtest.c 9.6 KB

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