2个分析input event事件的开源工具evtest,getevent

linux 的/etc/input/eventxx 设备可以用来方便地调试 鼠标、键盘、触摸板等输入设备
有两个开源可用来读取input event事件的值,evtest与getevent(这个是从android中提取的)

evtest
Evtest.c

点击(此处)折叠或打开

  1. /*
  2.  * $Id: evtest.c,v 1.23 2005/02/06 13:51:42 vojtech 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 <stdint.h>
  30.  
  31. #include <linux/input.h>
  32.  
  33. #include <string.h>
  34. #include <fcntl.h>
  35. #include <unistd.h>
  36. #include <stdio.h>
  37.  
  38. #ifndef EV_SYN
  39. #define EV_SYN 0
  40. #endif
  41.  
  42. char *events[EV_MAX + 1] = {
  43.     [0 ... EV_MAX] = NULL,
  44.     [EV_SYN] = "Sync",            [EV_KEY] = "Key",
  45.     [EV_REL] = "Relative",            [EV_ABS] = "Absolute",
  46.     [EV_MSC] = "Misc",            [EV_LED] = "LED",
  47.     [EV_SND] = "Sound",            [EV_REP] = "Repeat",
  48.     [EV_FF] = "ForceFeedback",        [EV_PWR] = "Power",
  49.     [EV_FF_STATUS] = "ForceFeedbackStatus",
  50. };
  51.  
  52. char *keys[KEY_MAX + 1] = {
  53.     [0 ... KEY_MAX] = NULL,
  54.     [KEY_RESERVED] = "Reserved",        [KEY_ESC] = "Esc",
  55.     [KEY_1] = "1",                [KEY_2] = "2",
  56.     [KEY_3] = "3",                [KEY_4] = "4",
  57.     [KEY_5] = "5",                [KEY_6] = "6",
  58.     [KEY_7] = "7",                [KEY_8] = "8",
  59.     [KEY_9] = "9",                [KEY_0] = "0",
  60.     [KEY_MINUS] = "Minus",            [KEY_EQUAL] = "Equal",
  61.     [KEY_BACKSPACE] = "Backspace",        [KEY_TAB] = "Tab",
  62.     [KEY_Q] = "Q",                [KEY_W] = "W",
  63.     [KEY_E] = "E",                [KEY_R] = "R",
  64.     [KEY_T] = "T",                [KEY_Y] = "Y",
  65.     [KEY_U] = "U",                [KEY_I] = "I",
  66.     [KEY_O] = "O",                [KEY_P] = "P",
  67.     [KEY_LEFTBRACE] = "LeftBrace",        [KEY_RIGHTBRACE] = "RightBrace",
  68.     [KEY_ENTER] = "Enter",            [KEY_LEFTCTRL] = "LeftControl",
  69.     [KEY_A] = "A",                [KEY_S] = "S",
  70.     [KEY_D] = "D",                [KEY_F] = "F",
  71.     [KEY_G] = "G",                [KEY_H] = "H",
  72.     [KEY_J] = "J",                [KEY_K] = "K",
  73.     [KEY_L] = "L",                [KEY_SEMICOLON] = "Semicolon",
  74.     [KEY_APOSTROPHE] = "Apostrophe",    [KEY_GRAVE] = "Grave",
  75.     [KEY_LEFTSHIFT] = "LeftShift",        [KEY_BACKSLASH] = "BackSlash",
  76.     [KEY_Z] = "Z",                [KEY_X] = "X",
  77.     [KEY_C] = "C",                [KEY_V] = "V",
  78.     [KEY_B] = "B",                [KEY_N] = "N",
  79.     [KEY_M] = "M",                [KEY_COMMA] = "Comma",
  80.     [KEY_DOT] = "Dot",            [KEY_SLASH] = "Slash",
  81.     [KEY_RIGHTSHIFT] = "RightShift",    [KEY_KPASTERISK] = "KPAsterisk",
  82.     [KEY_LEFTALT] = "LeftAlt",        [KEY_SPACE] = "Space",
  83.     [KEY_CAPSLOCK] = "CapsLock",        [KEY_F1] = "F1",
  84.     [KEY_F2] = "F2",            [KEY_F3] = "F3",
  85.     [KEY_F4] = "F4",            [KEY_F5] = "F5",
  86.     [KEY_F6] = "F6",            [KEY_F7] = "F7",
  87.     [KEY_F8] = "F8",            [KEY_F9] = "F9",
  88.     [KEY_F10] = "F10",            [KEY_NUMLOCK] = "NumLock",
  89.     [KEY_SCROLLLOCK] = "ScrollLock",    [KEY_KP7] = "KP7",
  90.     [KEY_KP8] = "KP8",            [KEY_KP9] = "KP9",
  91.     [KEY_KPMINUS] = "KPMinus",        [KEY_KP4] = "KP4",
  92.     [KEY_KP5] = "KP5",            [KEY_KP6] = "KP6",
  93.     [KEY_KPPLUS] = "KPPlus",        [KEY_KP1] = "KP1",
  94.     [KEY_KP2] = "KP2",            [KEY_KP3] = "KP3",
  95.     [KEY_KP0] = "KP0",            [KEY_KPDOT] = "KPDot",
  96.     [KEY_ZENKAKUHANKAKU] = "Zenkaku/Hankaku", [KEY_102ND] = "102nd",
  97.     [KEY_F11] = "F11",            [KEY_F12] = "F12",
  98.     [KEY_RO] = "RO",            [KEY_KATAKANA] = "Katakana",
  99.     [KEY_HIRAGANA] = "HIRAGANA",        [KEY_HENKAN] = "Henkan",
  100.     [KEY_KATAKANAHIRAGANA] = "Katakana/Hiragana", [KEY_MUHENKAN] = "Muhenkan",
  101.     [KEY_KPJPCOMMA] = "KPJpComma",        [KEY_KPENTER] = "KPEnter",
  102.     [KEY_RIGHTCTRL] = "RightCtrl",        [KEY_KPSLASH] = "KPSlash",
  103.     [KEY_SYSRQ] = "SysRq",            [KEY_RIGHTALT] = "RightAlt",
  104.     [KEY_LINEFEED] = "LineFeed",        [KEY_HOME] = "Home",
  105.     [KEY_UP] = "Up",            [KEY_PAGEUP] = "PageUp",
  106.     [KEY_LEFT] = "Left",            [KEY_RIGHT] = "Right",
  107.     [KEY_END] = "End",            [KEY_DOWN] = "Down",
  108.     [KEY_PAGEDOWN] = "PageDown",        [KEY_INSERT] = "Insert",
  109.     [KEY_DELETE] = "Delete",        [KEY_MACRO] = "Macro",
  110.     [KEY_MUTE] = "Mute",            [KEY_VOLUMEDOWN] = "VolumeDown",
  111.     [KEY_VOLUMEUP] = "VolumeUp",        [KEY_POWER] = "Power",
  112.     [KEY_KPEQUAL] = "KPEqual",        [KEY_KPPLUSMINUS] = "KPPlusMinus",
  113.     [KEY_PAUSE] = "Pause",            [KEY_KPCOMMA] = "KPComma",
  114.     [KEY_HANGUEL] = "Hanguel",        [KEY_HANJA] = "Hanja",
  115.     [KEY_YEN] = "Yen",            [KEY_LEFTMETA] = "LeftMeta",
  116.     [KEY_RIGHTMETA] = "RightMeta",        [KEY_COMPOSE] = "Compose",
  117.     [KEY_STOP] = "Stop",            [KEY_AGAIN] = "Again",
  118.     [KEY_PROPS] = "Props",            [KEY_UNDO] = "Undo",
  119.     [KEY_FRONT] = "Front",            [KEY_COPY] = "Copy",
  120.     [KEY_OPEN] = "Open",            [KEY_PASTE] = "Paste",
  121.     [KEY_FIND] = "Find",            [KEY_CUT] = "Cut",
  122.     [KEY_HELP] = "Help",            [KEY_MENU] = "Menu",
  123.     [KEY_CALC] = "Calc",            [KEY_SETUP] = "Setup",
  124.     [KEY_SLEEP] = "Sleep",            [KEY_WAKEUP] = "WakeUp",
  125.     [KEY_FILE] = "File",            [KEY_SENDFILE] = "SendFile",
  126.     [KEY_DELETEFILE] = "DeleteFile",    [KEY_XFER] = "X-fer",
  127.     [KEY_PROG1] = "Prog1",            [KEY_PROG2] = "Prog2",
  128.     [KEY_WWW] = "WWW",            [KEY_MSDOS] = "MSDOS",
  129.     [KEY_COFFEE] = "Coffee",        [KEY_DIRECTION] = "Direction",
  130.     [KEY_CYCLEWINDOWS] = "CycleWindows",    [KEY_MAIL] = "Mail",
  131.     [KEY_BOOKMARKS] = "Bookmarks",        [KEY_COMPUTER] = "Computer",
  132.     [KEY_BACK] = "Back",            [KEY_FORWARD] = "Forward",
  133.     [KEY_CLOSECD] = "CloseCD",        [KEY_EJECTCD] = "EjectCD",
  134.     [KEY_EJECTCLOSECD] = "EjectCloseCD",    [KEY_NEXTSONG] = "NextSong",
  135.     [KEY_PLAYPAUSE] = "PlayPause",        [KEY_PREVIOUSSONG] = "PreviousSong",
  136.     [KEY_STOPCD] = "StopCD",        [KEY_RECORD] = "Record",
  137.     [KEY_REWIND] = "Rewind",        [KEY_PHONE] = "Phone",
  138.     [KEY_ISO] = "ISOKey",            [KEY_CONFIG] = "Config",
  139.     [KEY_HOMEPAGE] = "HomePage",        [KEY_REFRESH] = "Refresh",
  140.     [KEY_EXIT] = "Exit",            [KEY_MOVE] = "Move",
  141.     [KEY_EDIT] = "Edit",            [KEY_SCROLLUP] = "ScrollUp",
  142.     [KEY_SCROLLDOWN] = "ScrollDown",    [KEY_KPLEFTPAREN] = "KPLeftParenthesis",
  143.     [KEY_KPRIGHTPAREN] = "KPRightParenthesis", [KEY_F13] = "F13",
  144.     [KEY_F14] = "F14",            [KEY_F15] = "F15",
  145.     [KEY_F16] = "F16",            [KEY_F17] = "F17",
  146.     [KEY_F18] = "F18",            [KEY_F19] = "F19",
  147.     [KEY_F20] = "F20",            [KEY_F21] = "F21",
  148.     [KEY_F22] = "F22",            [KEY_F23] = "F23",
  149.     [KEY_F24] = "F24",            [KEY_PLAYCD] = "PlayCD",
  150.     [KEY_PAUSECD] = "PauseCD",        [KEY_PROG3] = "Prog3",
  151.     [KEY_PROG4] = "Prog4",            [KEY_SUSPEND] = "Suspend",
  152.     [KEY_CLOSE] = "Close",            [KEY_PLAY] = "Play",
  153.     [KEY_FASTFORWARD] = "Fast Forward",    [KEY_BASSBOOST] = "Bass Boost",
  154.     [KEY_PRINT] = "Print",            [KEY_HP] = "HP",
  155.     [KEY_CAMERA] = "Camera",        [KEY_SOUND] = "Sound",
  156.     [KEY_QUESTION] = "Question",        [KEY_EMAIL] = "Email",
  157.     [KEY_CHAT] = "Chat",            [KEY_SEARCH] = "Search",
  158.     [KEY_CONNECT] = "Connect",        [KEY_FINANCE] = "Finance",
  159.     [KEY_SPORT] = "Sport",            [KEY_SHOP] = "Shop",
  160.     [KEY_ALTERASE] = "Alternate Erase",    [KEY_CANCEL] = "Cancel",
  161.     [KEY_BRIGHTNESSDOWN] = "Brightness down", [KEY_BRIGHTNESSUP] = "Brightness up",
  162.     [KEY_MEDIA] = "Media",            [KEY_UNKNOWN] = "Unknown",
  163.     [BTN_0] = "Btn0",            [BTN_1] = "Btn1",
  164.     [BTN_2] = "Btn2",            [BTN_3] = "Btn3",
  165.     [BTN_4] = "Btn4",            [BTN_5] = "Btn5",
  166.     [BTN_6] = "Btn6",            [BTN_7] = "Btn7",
  167.     [BTN_8] = "Btn8",            [BTN_9] = "Btn9",
  168.     [BTN_LEFT] = "LeftBtn",            [BTN_RIGHT] = "RightBtn",
  169.     [BTN_MIDDLE] = "MiddleBtn",        [BTN_SIDE] = "SideBtn",
  170.     [BTN_EXTRA] = "ExtraBtn",        [BTN_FORWARD] = "ForwardBtn",
  171.     [BTN_BACK] = "BackBtn",            [BTN_TASK] = "TaskBtn",
  172.     [BTN_TRIGGER] = "Trigger",        [BTN_THUMB] = "ThumbBtn",
  173.     [BTN_THUMB2] = "ThumbBtn2",        [BTN_TOP] = "TopBtn",
  174.     [BTN_TOP2] = "TopBtn2",            [BTN_PINKIE] = "PinkieBtn",
  175.     [BTN_BASE] = "BaseBtn",            [BTN_BASE2] = "BaseBtn2",
  176.     [BTN_BASE3] = "BaseBtn3",        [BTN_BASE4] = "BaseBtn4",
  177.     [BTN_BASE5] = "BaseBtn5",        [BTN_BASE6] = "BaseBtn6",
  178.     [BTN_DEAD] = "BtnDead",            [BTN_A] = "BtnA",
  179.     [BTN_B] = "BtnB",            [BTN_C] = "BtnC",
  180.     [BTN_X] = "BtnX",            [BTN_Y] = "BtnY",
  181.     [BTN_Z] = "BtnZ",            [BTN_TL] = "BtnTL",
  182.     [BTN_TR] = "BtnTR",            [BTN_TL2] = "BtnTL2",
  183.     [BTN_TR2] = "BtnTR2",            [BTN_SELECT] = "BtnSelect",
  184.     [BTN_START] = "BtnStart",        [BTN_MODE] = "BtnMode",
  185.     [BTN_THUMBL] = "BtnThumbL",        [BTN_THUMBR] = "BtnThumbR",
  186.     [BTN_TOOL_PEN] = "ToolPen",        [BTN_TOOL_RUBBER] = "ToolRubber",
  187.     [BTN_TOOL_BRUSH] = "ToolBrush",        [BTN_TOOL_PENCIL] = "ToolPencil",
  188.     [BTN_TOOL_AIRBRUSH] = "ToolAirbrush",    [BTN_TOOL_FINGER] = "ToolFinger",
  189.     [BTN_TOOL_MOUSE] = "ToolMouse",        [BTN_TOOL_LENS] = "ToolLens",
  190.     [BTN_TOUCH] = "Touch",            [BTN_STYLUS] = "Stylus",
  191.     [BTN_STYLUS2] = "Stylus2",        [BTN_TOOL_DOUBLETAP] = "Tool Doubletap",
  192.     [BTN_TOOL_TRIPLETAP] = "Tool Tripletap", [BTN_GEAR_DOWN] = "WheelBtn",
  193.     [BTN_GEAR_UP] = "Gear up",        [KEY_OK] = "Ok",
  194.     [KEY_SELECT] = "Select",        [KEY_GOTO] = "Goto",
  195.     [KEY_CLEAR] = "Clear",            [KEY_POWER2] = "Power2",
  196.     [KEY_OPTION] = "Option",        [KEY_INFO] = "Info",
  197.     [KEY_TIME] = "Time",            [KEY_VENDOR] = "Vendor",
  198.     [KEY_ARCHIVE] = "Archive",        [KEY_PROGRAM] = "Program",
  199.     [KEY_CHANNEL] = "Channel",        [KEY_FAVORITES] = "Favorites",
  200.     [KEY_EPG] = "EPG",            [KEY_PVR] = "PVR",
  201.     [KEY_MHP] = "MHP",            [KEY_LANGUAGE] = "Language",
  202.     [KEY_TITLE] = "Title",            [KEY_SUBTITLE] = "Subtitle",
  203.     [KEY_ANGLE] = "Angle",            [KEY_ZOOM] = "Zoom",
  204.     [KEY_MODE] = "Mode",            [KEY_KEYBOARD] = "Keyboard",
  205.     [KEY_SCREEN] = "Screen",        [KEY_PC] = "PC",
  206.     [KEY_TV] = "TV",            [KEY_TV2] = "TV2",
  207.     [KEY_VCR] = "VCR",            [KEY_VCR2] = "VCR2",
  208.     [KEY_SAT] = "Sat",            [KEY_SAT2] = "Sat2",
  209.     [KEY_CD] = "CD",            [KEY_TAPE] = "Tape",
  210.     [KEY_RADIO] = "Radio",            [KEY_TUNER] = "Tuner",
  211.     [KEY_PLAYER] = "Player",        [KEY_TEXT] = "Text",
  212.     [KEY_DVD] = "DVD",            [KEY_AUX] = "Aux",
  213.     [KEY_MP3] = "MP3",            [KEY_AUDIO] = "Audio",
  214.     [KEY_VIDEO] = "Video",            [KEY_DIRECTORY] = "Directory",
  215.     [KEY_LIST] = "List",            [KEY_MEMO] = "Memo",
  216.     [KEY_CALENDAR] = "Calendar",        [KEY_RED] = "Red",
  217.     [KEY_GREEN] = "Green",            [KEY_YELLOW] = "Yellow",
  218.     [KEY_BLUE] = "Blue",            [KEY_CHANNELUP] = "ChannelUp",
  219.     [KEY_CHANNELDOWN] = "ChannelDown",    [KEY_FIRST] = "First",
  220.     [KEY_LAST] = "Last",            [KEY_AB] = "AB",
  221.     [KEY_NEXT] = "Next",            [KEY_RESTART] = "Restart",
  222.     [KEY_SLOW] = "Slow",            [KEY_SHUFFLE] = "Shuffle",
  223.     [KEY_BREAK] = "Break",            [KEY_PREVIOUS] = "Previous",
  224.     [KEY_DIGITS] = "Digits",        [KEY_TEEN] = "TEEN",
  225.     [KEY_TWEN] = "TWEN",            [KEY_DEL_EOL] = "Delete EOL",
  226.     [KEY_DEL_EOS] = "Delete EOS",        [KEY_INS_LINE] = "Insert line",
  227.     [KEY_DEL_LINE] = "Delete line",
  228. };
  229.  
  230. char *absval[5] = { "Value", "Min ", "Max ", "Fuzz ", "Flat " };
  231.  
  232. char *relatives[REL_MAX + 1] = {
  233.     [0 ... REL_MAX] = NULL,
  234.     [REL_X] = "X",            [REL_Y] = "Y",
  235.     [REL_Z] = "Z",            [REL_HWHEEL] = "HWheel",
  236.     [REL_DIAL] = "Dial",        [REL_WHEEL] = "Wheel",
  237.     [REL_MISC] = "Misc",    
  238. };
  239.  
  240. char *absolutes[ABS_MAX + 1] = {
  241.     [0 ... ABS_MAX] = NULL,
  242.     [ABS_X] = "X",            [ABS_Y] = "Y",
  243.     [ABS_Z] = "Z",            [ABS_RX] = "Rx",
  244.     [ABS_RY] = "Ry",        [ABS_RZ] = "Rz",
  245.     [ABS_THROTTLE] = "Throttle",    [ABS_RUDDER] = "Rudder",
  246.     [ABS_WHEEL] = "Wheel",        [ABS_GAS] = "Gas",
  247.     [ABS_BRAKE] = "Brake",        [ABS_HAT0X] = "Hat0X",
  248.     [ABS_HAT0Y] = "Hat0Y",        [ABS_HAT1X] = "Hat1X",
  249.     [ABS_HAT1Y] = "Hat1Y",        [ABS_HAT2X] = "Hat2X",
  250.     [ABS_HAT2Y] = "Hat2Y",        [ABS_HAT3X] = "Hat3X",
  251.     [ABS_HAT3Y] = "Hat 3Y",        [ABS_PRESSURE] = "Pressure",
  252.     [ABS_DISTANCE] = "Distance",    [ABS_TILT_X] = "XTilt",
  253.     [ABS_TILT_Y] = "YTilt",        [ABS_TOOL_WIDTH] = "Tool Width",
  254.     [ABS_VOLUME] = "Volume",    [ABS_MISC] = "Misc",
  255. };
  256.  
  257. char *misc[MSC_MAX + 1] = {
  258.     [ 0 ... MSC_MAX] = NULL,
  259.     [MSC_SERIAL] = "Serial",    [MSC_PULSELED] = "Pulseled",
  260.     [MSC_GESTURE] = "Gesture",    [MSC_RAW] = "RawData",
  261.     [MSC_SCAN] = "ScanCode",
  262. };
  263.  
  264. char *leds[LED_MAX + 1] = {
  265.     [0 ... LED_MAX] = NULL,
  266.     [LED_NUML] = "NumLock",        [LED_CAPSL] = "CapsLock",
  267.     [LED_SCROLLL] = "ScrollLock",    [LED_COMPOSE] = "Compose",
  268.     [LED_KANA] = "Kana",        [LED_SLEEP] = "Sleep",
  269.     [LED_SUSPEND] = "Suspend",    [LED_MUTE] = "Mute",
  270.     [LED_MISC] = "Misc",
  271. };
  272.  
  273. char *repeats[REP_MAX + 1] = {
  274.     [0 ... REP_MAX] = NULL,
  275.     [REP_DELAY] = "Delay",        [REP_PERIOD] = "Period"
  276. };
  277.  
  278. char *sounds[SND_MAX + 1] = {
  279.     [0 ... SND_MAX] = NULL,
  280.     [SND_CLICK] = "Click",        [SND_BELL] = "Bell",
  281.     [SND_TONE] = "Tone"
  282. };
  283.  
  284. char **names[EV_MAX + 1] = {
  285.     [0 ... EV_MAX] = NULL,
  286.     [EV_SYN] = events,            [EV_KEY] = keys,
  287.     [EV_REL] = relatives,            [EV_ABS] = absolutes,
  288.     [EV_MSC] = misc,            [EV_LED] = leds,
  289.     [EV_SND] = sounds,            [EV_REP] = repeats,
  290. };
  291.  
  292. #define BITS_PER_LONG (sizeof(long) * 8)
  293. #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
  294. #define OFF(x) ((x)%BITS_PER_LONG)
  295. #define BIT(x) (1UL<<OFF(x))
  296. #define LONG(x) ((x)/BITS_PER_LONG)
  297. #define test_bit(bit, array)    ((array[LONG(bit)] >> OFF(bit)) & 1)
  298.  
  299. int main (int argc, char **argv)
  300. {
  301.     int fd, rd, i, j, k;
  302.     struct input_event ev[64];
  303.     int version;
  304.     unsigned short id[4];
  305.     unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
  306.     char name[256] = "Unknown";
  307.     int abs[5];
  308.  
  309.     if (argc < 2) {
  310.         printf("Usage: evtest /dev/input/eventX\n");
  311.         printf("Where X = input device number\n");
  312.         return 1;
  313.     }
  314.  
  315.     if ((fd = open(argv[argc - 1], O_RDONLY)) < 0) {
  316.         perror("evtest");
  317.         return 1;
  318.     }
  319.  
  320.     if (ioctl(fd, EVIOCGVERSION, &version)) {
  321.         perror("evtest: can't get version");
  322.         return 1;
  323.     }
  324.  
  325.     printf("Input driver version is %d.%d.%d\n",
  326.         version >> 16, (version >> 8) & 0xff, version & 0xff);
  327.  
  328.     ioctl(fd, EVIOCGID, id);
  329.     printf("Input device ID: bus 0x%x vendor 0x%x product 0x%x version 0x%x\n",
  330.         id[ID_BUS], id[ID_VENDOR], id[ID_PRODUCT], id[ID_VERSION]);
  331.  
  332.     ioctl(fd, EVIOCGNAME(sizeof(name)), name);
  333.     printf("Input device name: \"%s\"\n", name);
  334.  
  335.     memset(bit, 0, sizeof(bit));
  336.     ioctl(fd, EVIOCGBIT(0, EV_MAX), bit[0]);
  337.     printf("Supported events:\n");
  338.  
  339.     for (i = 0; i < EV_MAX; i++)
  340.         if (test_bit(i, bit[0])) {
  341.             printf(" Event type %d (%s)\n", i, events[i] ? events[i] : "?");
  342.             if (!i) continue;
  343.             ioctl(fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
  344.             for (j = 0; j < KEY_MAX; j++)
  345.                 if (test_bit(j, bit[i])) {
  346.                     printf(" Event code %d (%s)\n", j, names[i] ? (names[i][j] ? names[i][j] : "?") : "?");
  347.                     if (i == EV_ABS) {
  348.                         ioctl(fd, EVIOCGABS(j), abs);
  349.                         for (k = 0; k < 5; k++)
  350.                             if ((k < 3) || abs[k])
  351.                                 printf(" %s %6d\n", absval[k], abs[k]);
  352.                     }
  353.                 }
  354.         }
  355.         
  356.  
  357.     printf("Testing ... (interrupt to exit)\n");
  358.  
  359.     while (1) {
  360.         rd = read(fd, ev, sizeof(struct input_event) * 64);
  361.  
  362.         if (rd < (int) sizeof(struct input_event)) {
  363.             printf("yyy\n");
  364.             perror("\nevtest: error reading");
  365.             return 1;
  366.         }
  367.  
  368.         for (i = 0; i < rd / sizeof(struct input_event); i++)
  369.  
  370.             if (ev[i].type == EV_SYN) {
  371.                 printf("Event: time %ld.%06ld, -------------- %s ------------\n",
  372.                     ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].code ? "Config Sync" : "Report Sync" );
  373.             } else if (ev[i].type == EV_MSC && (ev[i].code == MSC_RAW || ev[i].code == MSC_SCAN)) {
  374.                 printf("Event: time %ld.%06ld, type %d (%s), code %d (%s), value %02x\n",
  375.                     ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].type,
  376.                     events[ev[i].type] ? events[ev[i].type] : "?",
  377.                     ev[i].code,
  378.                     names[ev[i].type] ? (names[ev[i].type][ev[i].code] ? names[ev[i].type][ev[i].code] : "?") : "?",
  379.                     ev[i].value);
  380.             } else {
  381.                 printf("Event: time %ld.%06ld, type %d (%s), code %d (%s), value %d\n",
  382.                     ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].type,
  383.                     events[ev[i].type] ? events[ev[i].type] : "?",
  384.                     ev[i].code,
  385.                     names[ev[i].type] ? (names[ev[i].type][ev[i].code] ? names[ev[i].type][ev[i].code] : "?") : "?",
  386.                     ev[i].value);
  387.             }    
  388.  
  389.     }
  390. }

gcc  Evtest.c -o evtest

getevent单独提取出来在linux下编译做了些修改

getevent.h
 

点击(此处)折叠或打开

  1. #include <linux/input.h>
  2. #include <stdio.h>
  3.  
  4. struct label {
  5.     const char *name;
  6.     int value;
  7. };
  8.  
  9. #define LABEL(constant) { #constant, constant }
  10. #define LABEL_END { NULL, -1 }
  11.  
  12. static struct label input_prop_labels[] = {
  13.         LABEL(INPUT_PROP_POINTER),
  14.         LABEL(INPUT_PROP_DIRECT),
  15.         LABEL(INPUT_PROP_BUTTONPAD),
  16.         LABEL(INPUT_PROP_SEMI_MT),
  17.         LABEL_END,
  18. };
  19.  
  20. static struct label ev_labels[] = {
  21.         LABEL(EV_SYN),
  22.         LABEL(EV_KEY),
  23.         LABEL(EV_REL),
  24.         LABEL(EV_ABS),
  25.         LABEL(EV_MSC),
  26.         LABEL(EV_SW),
  27.         LABEL(EV_LED),
  28.         LABEL(EV_SND),
  29.         LABEL(EV_REP),
  30.         LABEL(EV_FF),
  31.         LABEL(EV_PWR),
  32.         LABEL(EV_FF_STATUS),
  33.         LABEL_END,
  34. };
  35.  
  36. static struct label syn_labels[] = {
  37.         LABEL(SYN_REPORT),
  38.         LABEL(SYN_CONFIG),
  39.         LABEL(SYN_MT_REPORT),
  40.         LABEL(SYN_DROPPED),
  41.         LABEL_END,
  42. };
  43.  
  44. static struct label key_labels[] = {
  45.         LABEL(KEY_RESERVED),
  46.         LABEL(KEY_ESC),
  47.         LABEL(KEY_1),
  48.         LABEL(KEY_2),
  49.         LABEL(KEY_3),
  50.         LABEL(KEY_4),
  51.         LABEL(KEY_5),
  52.         LABEL(KEY_6),
  53.         LABEL(KEY_7),
  54.         LABEL(KEY_8),
  55.         LABEL(KEY_9),
  56.         LABEL(KEY_0),
  57.         LABEL(KEY_MINUS),
  58.         LABEL(KEY_EQUAL),
  59.         LABEL(KEY_BACKSPACE),
  60.         LABEL(KEY_TAB),
  61.         LABEL(KEY_Q),
  62.         LABEL(KEY_W),
  63.         LABEL(KEY_E),
  64.         LABEL(KEY_R),
  65.         LABEL(KEY_T),
  66.         LABEL(KEY_Y),
  67.         LABEL(KEY_U),
  68.         LABEL(KEY_I),
  69.         LABEL(KEY_O),
  70.         LABEL(KEY_P),
  71.         LABEL(KEY_LEFTBRACE),
  72.         LABEL(KEY_RIGHTBRACE),
  73.         LABEL(KEY_ENTER),
  74.         LABEL(KEY_LEFTCTRL),
  75.         LABEL(KEY_A),
  76.         LABEL(KEY_S),
  77.         LABEL(KEY_D),
  78.         LABEL(KEY_F),
  79.         LABEL(KEY_G),
  80.         LABEL(KEY_H),
  81.         LABEL(KEY_J),
  82.         LABEL(KEY_K),
  83.         LABEL(KEY_L),
  84.         LABEL(KEY_SEMICOLON),
  85.         LABEL(KEY_APOSTROPHE),
  86.         LABEL(KEY_GRAVE),
  87.         LABEL(KEY_LEFTSHIFT),
  88.         LABEL(KEY_BACKSLASH),
  89.         LABEL(KEY_Z),
  90.         LABEL(KEY_X),
  91.         LABEL(KEY_C),
  92.         LABEL(KEY_V),
  93.         LABEL(KEY_B),
  94.         LABEL(KEY_N),
  95.         LABEL(KEY_M),
  96.         LABEL(KEY_COMMA),
  97.         LABEL(KEY_DOT),
  98.         LABEL(KEY_SLASH),
  99.         LABEL(KEY_RIGHTSHIFT),
  100.         LABEL(KEY_KPASTERISK),
  101.         LABEL(KEY_LEFTALT),
  102.         LABEL(KEY_SPACE),
  103.         LABEL(KEY_CAPSLOCK),
  104.         LABEL(KEY_F1),
  105.         LABEL(KEY_F2),
  106.         LABEL(KEY_F3),
  107.         LABEL(KEY_F4),
  108.         LABEL(KEY_F5),
  109.         LABEL(KEY_F6),
  110.         LABEL(KEY_F7),
  111.         LABEL(KEY_F8),
  112.         LABEL(KEY_F9),
  113.         LABEL(KEY_F10),
  114.         LABEL(KEY_NUMLOCK),
  115.         LABEL(KEY_SCROLLLOCK),
  116.         LABEL(KEY_KP7),
  117.         LABEL(KEY_KP8),
  118.         LABEL(KEY_KP9),
  119.         LABEL(KEY_KPMINUS),
  120.         LABEL(KEY_KP4),
  121.         LABEL(KEY_KP5),
  122.         LABEL(KEY_KP6),
  123.         LABEL(KEY_KPPLUS),
  124.         LABEL(KEY_KP1),
  125.         LABEL(KEY_KP2),
  126.         LABEL(KEY_KP3),
  127.         LABEL(KEY_KP0),
  128.         LABEL(KEY_KPDOT),
  129.         LABEL(KEY_ZENKAKUHANKAKU),
  130.         LABEL(KEY_102ND),
  131.         LABEL(KEY_F11),
  132.         LABEL(KEY_F12),
  133.         LABEL(KEY_RO),
  134.         LABEL(KEY_KATAKANA),
  135.         LABEL(KEY_HIRAGANA),
  136.         LABEL(KEY_HENKAN),
  137.         LABEL(KEY_KATAKANAHIRAGANA),
  138.         LABEL(KEY_MUHENKAN),
  139.         LABEL(KEY_KPJPCOMMA),
  140.         LABEL(KEY_KPENTER),
  141.         LABEL(KEY_RIGHTCTRL),
  142.         LABEL(KEY_KPSLASH),
  143.         LABEL(KEY_SYSRQ),
  144.         LABEL(KEY_RIGHTALT),
  145.         LABEL(KEY_LINEFEED),
  146.         LABEL(KEY_HOME),
  147.         LABEL(KEY_UP),
  148.         LABEL(KEY_PAGEUP),
  149.         LABEL(KEY_LEFT),
  150.         LABEL(KEY_RIGHT),
  151.         LABEL(KEY_END),
  152.         LABEL(KEY_DOWN),
  153.         LABEL(KEY_PAGEDOWN),
  154.         LABEL(KEY_INSERT),
  155.         LABEL(KEY_DELETE),
  156.         LABEL(KEY_MACRO),
  157.         LABEL(KEY_MUTE),
  158.         LABEL(KEY_VOLUMEDOWN),
  159.         LABEL(KEY_VOLUMEUP),
  160.         LABEL(KEY_POWER),
  161.         LABEL(KEY_KPEQUAL),
  162.         LABEL(KEY_KPPLUSMINUS),
  163.         LABEL(KEY_PAUSE),
  164.         LABEL(KEY_SCALE),
  165.         LABEL(KEY_KPCOMMA),
  166.         LABEL(KEY_HANGEUL),
  167.         LABEL(KEY_HANGUEL),
  168.         LABEL(KEY_HANJA),
  169.         LABEL(KEY_YEN),
  170.         LABEL(KEY_LEFTMETA),
  171.         LABEL(KEY_RIGHTMETA),
  172.         LABEL(KEY_COMPOSE),
  173.         LABEL(KEY_STOP),
  174.         LABEL(KEY_AGAIN),
  175.         LABEL(KEY_PROPS),
  176.         LABEL(KEY_UNDO),
  177.         LABEL(KEY_FRONT),
  178.         LABEL(KEY_COPY),
  179.         LABEL(KEY_OPEN),
  180.         LABEL(KEY_PASTE),
  181.         LABEL(KEY_FIND),
  182.         LABEL(KEY_CUT),
  183.         LABEL(KEY_HELP),
  184.         LABEL(KEY_MENU),
  185.         LABEL(KEY_CALC),
  186.         LABEL(KEY_SETUP),
  187.         LABEL(KEY_SLEEP),
  188.         LABEL(KEY_WAKEUP),
  189.         LABEL(KEY_FILE),
  190.         LABEL(KEY_SENDFILE),
  191.         LABEL(KEY_DELETEFILE),
  192.         LABEL(KEY_XFER),
  193.         LABEL(KEY_PROG1),
  194.         LABEL(KEY_PROG2),
  195.         LABEL(KEY_WWW),
  196.         LABEL(KEY_MSDOS),
  197.         LABEL(KEY_COFFEE),
  198.         LABEL(KEY_SCREENLOCK),
  199.         LABEL(KEY_DIRECTION),
  200.         LABEL(KEY_CYCLEWINDOWS),
  201.         LABEL(KEY_MAIL),
  202.         LABEL(KEY_BOOKMARKS),
  203.         LABEL(KEY_COMPUTER),
  204.         LABEL(KEY_BACK),
  205.         LABEL(KEY_FORWARD),
  206.         LABEL(KEY_CLOSECD),
  207.         LABEL(KEY_EJECTCD),
  208.         LABEL(KEY_EJECTCLOSECD),
  209.         LABEL(KEY_NEXTSONG),
  210.         LABEL(KEY_PLAYPAUSE),
  211.         LABEL(KEY_PREVIOUSSONG),
  212.         LABEL(KEY_STOPCD),
  213.         LABEL(KEY_RECORD),
  214.         LABEL(KEY_REWIND),
  215.         LABEL(KEY_PHONE),
  216.         LABEL(KEY_ISO),
  217.         LABEL(KEY_CONFIG),
  218.         LABEL(KEY_HOMEPAGE),
  219.         LABEL(KEY_REFRESH),
  220.         LABEL(KEY_EXIT),
  221.         LABEL(KEY_MOVE),
  222.         LABEL(KEY_EDIT),
  223.         LABEL(KEY_SCROLLUP),
  224.         LABEL(KEY_SCROLLDOWN),
  225.         LABEL(KEY_KPLEFTPAREN),
  226.         LABEL(KEY_KPRIGHTPAREN),
  227.         LABEL(KEY_NEW),
  228.         LABEL(KEY_REDO),
  229.         LABEL(KEY_F13),
  230.         LABEL(KEY_F14),
  231.         LABEL(KEY_F15),
  232.         LABEL(KEY_F16),
  233.         LABEL(KEY_F17),
  234.         LABEL(KEY_F18),
  235.         LABEL(KEY_F19),
  236.         LABEL(KEY_F20),
  237.         LABEL(KEY_F21),
  238.         LABEL(KEY_F22),
  239.         LABEL(KEY_F23),
  240.         LABEL(KEY_F24),
  241.         LABEL(KEY_PLAYCD),
  242.         LABEL(KEY_PAUSECD),
  243.         LABEL(KEY_PROG3),
  244.         LABEL(KEY_PROG4),
  245.         LABEL(KEY_DASHBOARD),
  246.         LABEL(KEY_SUSPEND),
  247.         LABEL(KEY_CLOSE),
  248.         LABEL(KEY_PLAY),
  249.         LABEL(KEY_FASTFORWARD),
  250.         LABEL(KEY_BASSBOOST),
  251.         LABEL(KEY_PRINT),
  252.         LABEL(KEY_HP),
  253.         LABEL(KEY_CAMERA),
  254.         LABEL(KEY_SOUND),
  255.         LABEL(KEY_QUESTION),
  256.         LABEL(KEY_EMAIL),
  257.         LABEL(KEY_CHAT),
  258.         LABEL(KEY_SEARCH),
  259.         LABEL(KEY_CONNECT),
  260.         LABEL(KEY_FINANCE),
  261.         LABEL(KEY_SPORT),
  262.         LABEL(KEY_SHOP),
  263.         LABEL(KEY_ALTERASE),
  264.         LABEL(KEY_CANCEL),
  265.         LABEL(KEY_BRIGHTNESSDOWN),
  266.         LABEL(KEY_BRIGHTNESSUP),
  267.         LABEL(KEY_MEDIA),
  268.         LABEL(KEY_SWITCHVIDEOMODE),
  269.         LABEL(KEY_KBDILLUMTOGGLE),
  270.         LABEL(KEY_KBDILLUMDOWN),
  271.         LABEL(KEY_KBDILLUMUP),
  272.         LABEL(KEY_SEND),
  273.         LABEL(KEY_REPLY),
  274.         LABEL(KEY_FORWARDMAIL),
  275.         LABEL(KEY_SAVE),
  276.         LABEL(KEY_DOCUMENTS),
  277.         LABEL(KEY_BATTERY),
  278.         LABEL(KEY_BLUETOOTH),
  279.         LABEL(KEY_WLAN),
  280.         LABEL(KEY_UWB),
  281.         LABEL(KEY_UNKNOWN),
  282.         LABEL(KEY_VIDEO_NEXT),
  283.         LABEL(KEY_VIDEO_PREV),
  284.         LABEL(KEY_BRIGHTNESS_CYCLE),
  285.         LABEL(KEY_BRIGHTNESS_ZERO),
  286.         LABEL(KEY_DISPLAY_OFF),
  287.         LABEL(KEY_WIMAX),
  288.         LABEL(KEY_RFKILL),
  289.         LABEL(BTN_0),
  290.         LABEL(BTN_1),
  291.         LABEL(BTN_2),
  292.         LABEL(BTN_3),
  293.         LABEL(BTN_4),
  294.         LABEL(BTN_5),
  295.         LABEL(BTN_6),
  296.         LABEL(BTN_7),
  297.         LABEL(BTN_8),
  298.         LABEL(BTN_9),
  299.         LABEL(BTN_LEFT),
  300.         LABEL(BTN_RIGHT),
  301.         LABEL(BTN_MIDDLE),
  302.         LABEL(BTN_SIDE),
  303.         LABEL(BTN_EXTRA),
  304.         LABEL(BTN_FORWARD),
  305.         LABEL(BTN_BACK),
  306.         LABEL(BTN_TASK),
  307.         LABEL(BTN_JOYSTICK),
  308.         LABEL(BTN_TRIGGER),
  309.         LABEL(BTN_THUMB),
  310.         LABEL(BTN_THUMB2),
  311.         LABEL(BTN_TOP),
  312.         LABEL(BTN_TOP2),
  313.         LABEL(BTN_PINKIE),
  314.         LABEL(BTN_BASE),
  315.         LABEL(BTN_BASE2),
  316.         LABEL(BTN_BASE3),
  317.         LABEL(BTN_BASE4),
  318.         LABEL(BTN_BASE5),
  319.         LABEL(BTN_BASE6),
  320.         LABEL(BTN_DEAD),
  321.         LABEL(BTN_A),
  322.         LABEL(BTN_B),
  323.         LABEL(BTN_C),
  324.         LABEL(BTN_X),
  325.         LABEL(BTN_Y),
  326.         LABEL(BTN_Z),
  327.         LABEL(BTN_TL),
  328.         LABEL(BTN_TR),
  329.         LABEL(BTN_TL2),
  330.         LABEL(BTN_TR2),
  331.         LABEL(BTN_SELECT),
  332.         LABEL(BTN_START),
  333.         LABEL(BTN_MODE),
  334.         LABEL(BTN_THUMBL),
  335.         LABEL(BTN_THUMBR),
  336.         LABEL(BTN_TOOL_PEN),
  337.         LABEL(BTN_TOOL_RUBBER),
  338.         LABEL(BTN_TOOL_BRUSH),
  339.         LABEL(BTN_TOOL_PENCIL),
  340.         LABEL(BTN_TOOL_AIRBRUSH),
  341.         LABEL(BTN_TOOL_FINGER),
  342.         LABEL(BTN_TOOL_MOUSE),
  343.         LABEL(BTN_TOOL_LENS),
  344.         LABEL(BTN_TOUCH),
  345.         LABEL(BTN_STYLUS),
  346.         LABEL(BTN_STYLUS2),
  347.         LABEL(BTN_TOOL_DOUBLETAP),
  348.         LABEL(BTN_TOOL_TRIPLETAP),
  349.         LABEL(BTN_TOOL_QUADTAP),
  350.         LABEL(BTN_GEAR_DOWN),
  351.         LABEL(BTN_GEAR_UP),
  352.         LABEL(KEY_OK),
  353.         LABEL(KEY_SELECT),
  354.         LABEL(KEY_GOTO),
  355.         LABEL(KEY_CLEAR),
  356.         LABEL(KEY_POWER2),
  357.         LABEL(KEY_OPTION),
  358.         LABEL(KEY_INFO),
  359.         LABEL(KEY_TIME),
  360.         LABEL(KEY_VENDOR),
  361.         LABEL(KEY_ARCHIVE),
  362.         LABEL(KEY_PROGRAM),
  363.         LABEL(KEY_CHANNEL),
  364.         LABEL(KEY_FAVORITES),
  365.         LABEL(KEY_EPG),
  366.         LABEL(KEY_PVR),
  367.         LABEL(KEY_MHP),
  368.         LABEL(KEY_LANGUAGE),
  369.         LABEL(KEY_TITLE),
  370.         LABEL(KEY_SUBTITLE),
  371.         LABEL(KEY_ANGLE),
  372.         LABEL(KEY_ZOOM),
  373.         LABEL(KEY_MODE),
  374.         LABEL(KEY_KEYBOARD),
  375.         LABEL(KEY_SCREEN),
  376.         LABEL(KEY_PC),
  377.         LABEL(KEY_TV),
  378.         LABEL(KEY_TV2),
  379.         LABEL(KEY_VCR),
  380.         LABEL(KEY_VCR2),
  381.         LABEL(KEY_SAT),
  382.         LABEL(KEY_SAT2),
  383.         LABEL(KEY_CD),
  384.         LABEL(KEY_TAPE),
  385.         LABEL(KEY_RADIO),
  386.         LABEL(KEY_TUNER),
  387.         LABEL(KEY_PLAYER),
  388.         LABEL(KEY_TEXT),
  389.         LABEL(KEY_DVD),
  390.         LABEL(KEY_AUX),
  391.         LABEL(KEY_MP3),
  392.         LABEL(KEY_AUDIO),
  393.         LABEL(KEY_VIDEO),
  394.         LABEL(KEY_DIRECTORY),
  395.         LABEL(KEY_LIST),
  396.         LABEL(KEY_MEMO),
  397.         LABEL(KEY_CALENDAR),
  398.         LABEL(KEY_RED),
  399.         LABEL(KEY_GREEN),
  400.         LABEL(KEY_YELLOW),
  401.         LABEL(KEY_BLUE),
  402.         LABEL(KEY_CHANNELUP),
  403.         LABEL(KEY_CHANNELDOWN),
  404.         LABEL(KEY_FIRST),
  405.         LABEL(KEY_LAST),
  406.         LABEL(KEY_AB),
  407.         LABEL(KEY_NEXT),
  408.         LABEL(KEY_RESTART),
  409.         LABEL(KEY_SLOW),
  410.         LABEL(KEY_SHUFFLE),
  411.         LABEL(KEY_BREAK),
  412.         LABEL(KEY_PREVIOUS),
  413.         LABEL(KEY_DIGITS),
  414.         LABEL(KEY_TEEN),
  415.         LABEL(KEY_TWEN),
  416.         LABEL(KEY_VIDEOPHONE),
  417.         LABEL(KEY_GAMES),
  418.         LABEL(KEY_ZOOMIN),
  419.         LABEL(KEY_ZOOMOUT),
  420.         LABEL(KEY_ZOOMRESET),
  421.         LABEL(KEY_WORDPROCESSOR),
  422.         LABEL(KEY_EDITOR),
  423.         LABEL(KEY_SPREADSHEET),
  424.         LABEL(KEY_GRAPHICSEDITOR),
  425.         LABEL(KEY_PRESENTATION),
  426.         LABEL(KEY_DATABASE),
  427.         LABEL(KEY_NEWS),
  428.         LABEL(KEY_VOICEMAIL),
  429.         LABEL(KEY_ADDRESSBOOK),
  430.         LABEL(KEY_MESSENGER),
  431.         LABEL(KEY_DISPLAYTOGGLE),
  432.         LABEL(KEY_SPELLCHECK),
  433.         LABEL(KEY_LOGOFF),
  434.         LABEL(KEY_DOLLAR),
  435.         LABEL(KEY_EURO),
  436.         LABEL(KEY_FRAMEBACK),
  437.         LABEL(KEY_FRAMEFORWARD),
  438.         LABEL(KEY_CONTEXT_MENU),
  439.         LABEL(KEY_MEDIA_REPEAT),
  440.         LABEL(KEY_10CHANNELSUP),
  441.         LABEL(KEY_10CHANNELSDOWN),
  442.         LABEL(KEY_IMAGES),
  443.         LABEL(KEY_DEL_EOL),
  444.         LABEL(KEY_DEL_EOS),
  445.         LABEL(KEY_INS_LINE),
  446.         LABEL(KEY_DEL_LINE),
  447.         LABEL(KEY_FN),
  448.         LABEL(KEY_FN_ESC),
  449.         LABEL(KEY_FN_F1),
  450.         LABEL(KEY_FN_F2),
  451.         LABEL(KEY_FN_F3),
  452.         LABEL(KEY_FN_F4),
  453.         LABEL(KEY_FN_F5),
  454.         LABEL(KEY_FN_F6),
  455.         LABEL(KEY_FN_F7),
  456.         LABEL(KEY_FN_F8),
  457.         LABEL(KEY_FN_F9),
  458.         LABEL(KEY_FN_F10),
  459.         LABEL(KEY_FN_F11),
  460.         LABEL(KEY_FN_F12),
  461.         LABEL(KEY_FN_1),
  462.         LABEL(KEY_FN_2),
  463.         LABEL(KEY_FN_D),
  464.         LABEL(KEY_FN_E),
  465.         LABEL(KEY_FN_F),
  466.         LABEL(KEY_FN_S),
  467.         LABEL(KEY_FN_B),
  468.         LABEL(KEY_BRL_DOT1),
  469.         LABEL(KEY_BRL_DOT2),
  470.         LABEL(KEY_BRL_DOT3),
  471.         LABEL(KEY_BRL_DOT4),
  472.         LABEL(KEY_BRL_DOT5),
  473.         LABEL(KEY_BRL_DOT6),
  474.         LABEL(KEY_BRL_DOT7),
  475.         LABEL(KEY_BRL_DOT8),
  476.         LABEL(KEY_BRL_DOT9),
  477.         LABEL(KEY_BRL_DOT10),
  478.         LABEL(KEY_NUMERIC_0),
  479.         LABEL(KEY_NUMERIC_1),
  480.         LABEL(KEY_NUMERIC_2),
  481.         LABEL(KEY_NUMERIC_3),
  482.         LABEL(KEY_NUMERIC_4),
  483.         LABEL(KEY_NUMERIC_5),
  484.         LABEL(KEY_NUMERIC_6),
  485.         LABEL(KEY_NUMERIC_7),
  486.         LABEL(KEY_NUMERIC_8),
  487.         LABEL(KEY_NUMERIC_9),
  488.         LABEL(KEY_NUMERIC_STAR),
  489.         LABEL(KEY_NUMERIC_POUND),
  490.         LABEL(KEY_CAMERA_FOCUS),
  491.         LABEL(KEY_WPS_BUTTON),
  492.         LABEL(KEY_TOUCHPAD_TOGGLE),
  493.         LABEL(KEY_TOUCHPAD_ON),
  494.         LABEL(KEY_TOUCHPAD_OFF),
  495.         LABEL(KEY_CAMERA_ZOOMIN),
  496.         LABEL(KEY_CAMERA_ZOOMOUT),
  497.         LABEL(KEY_CAMERA_UP),
  498.         LABEL(KEY_CAMERA_DOWN),
  499.         LABEL(KEY_CAMERA_LEFT),
  500.         LABEL(KEY_CAMERA_RIGHT),
  501.         LABEL(BTN_TRIGGER_HAPPY1),
  502.         LABEL(BTN_TRIGGER_HAPPY2),
  503.         LABEL(BTN_TRIGGER_HAPPY3),
  504.         LABEL(BTN_TRIGGER_HAPPY4),
  505.         LABEL(BTN_TRIGGER_HAPPY5),
  506.         LABEL(BTN_TRIGGER_HAPPY6),
  507.         LABEL(BTN_TRIGGER_HAPPY7),
  508.         LABEL(BTN_TRIGGER_HAPPY8),
  509.         LABEL(BTN_TRIGGER_HAPPY9),
  510.         LABEL(BTN_TRIGGER_HAPPY10),
  511.         LABEL(BTN_TRIGGER_HAPPY11),
  512.         LABEL(BTN_TRIGGER_HAPPY12),
  513.         LABEL(BTN_TRIGGER_HAPPY13),
  514.         LABEL(BTN_TRIGGER_HAPPY14),
  515.         LABEL(BTN_TRIGGER_HAPPY15),
  516.         LABEL(BTN_TRIGGER_HAPPY16),
  517.         LABEL(BTN_TRIGGER_HAPPY17),
  518.         LABEL(BTN_TRIGGER_HAPPY18),
  519.         LABEL(BTN_TRIGGER_HAPPY19),
  520.         LABEL(BTN_TRIGGER_HAPPY20),
  521.         LABEL(BTN_TRIGGER_HAPPY21),
  522.         LABEL(BTN_TRIGGER_HAPPY22),
  523.         LABEL(BTN_TRIGGER_HAPPY23),
  524.         LABEL(BTN_TRIGGER_HAPPY24),
  525.         LABEL(BTN_TRIGGER_HAPPY25),
  526.         LABEL(BTN_TRIGGER_HAPPY26),
  527.         LABEL(BTN_TRIGGER_HAPPY27),
  528.         LABEL(BTN_TRIGGER_HAPPY28),
  529.         LABEL(BTN_TRIGGER_HAPPY29),
  530.         LABEL(BTN_TRIGGER_HAPPY30),
  531.         LABEL(BTN_TRIGGER_HAPPY31),
  532.         LABEL(BTN_TRIGGER_HAPPY32),
  533.         LABEL(BTN_TRIGGER_HAPPY33),
  534.         LABEL(BTN_TRIGGER_HAPPY34),
  535.         LABEL(BTN_TRIGGER_HAPPY35),
  536.         LABEL(BTN_TRIGGER_HAPPY36),
  537.         LABEL(BTN_TRIGGER_HAPPY37),
  538.         LABEL(BTN_TRIGGER_HAPPY38),
  539.         LABEL(BTN_TRIGGER_HAPPY39),
  540.         LABEL(BTN_TRIGGER_HAPPY40),
  541.         LABEL_END,
  542. };
  543.  
  544. static struct label rel_labels[] = {
  545.         LABEL(REL_X),
  546.         LABEL(REL_Y),
  547.         LABEL(REL_Z),
  548.         LABEL(REL_RX),
  549.         LABEL(REL_RY),
  550.         LABEL(REL_RZ),
  551.         LABEL(REL_HWHEEL),
  552.         LABEL(REL_DIAL),
  553.         LABEL(REL_WHEEL),
  554.         LABEL(REL_MISC),
  555.         LABEL_END,
  556. };
  557.  
  558. static struct label abs_labels[] = {
  559.         LABEL(ABS_X),
  560.         LABEL(ABS_Y),
  561.         LABEL(ABS_Z),
  562.         LABEL(ABS_RX),
  563.         LABEL(ABS_RY),
  564.         LABEL(ABS_RZ),
  565.         LABEL(ABS_THROTTLE),
  566.         LABEL(ABS_RUDDER),
  567.         LABEL(ABS_WHEEL),
  568.         LABEL(ABS_GAS),
  569.         LABEL(ABS_BRAKE),
  570.         LABEL(ABS_HAT0X),
  571.         LABEL(ABS_HAT0Y),
  572.         LABEL(ABS_HAT1X),
  573.         LABEL(ABS_HAT1Y),
  574.         LABEL(ABS_HAT2X),
  575.         LABEL(ABS_HAT2Y),
  576.         LABEL(ABS_HAT3X),
  577.         LABEL(ABS_HAT3Y),
  578.         LABEL(ABS_PRESSURE),
  579.         LABEL(ABS_DISTANCE),
  580.         LABEL(ABS_TILT_X),
  581.         LABEL(ABS_TILT_Y),
  582.         LABEL(ABS_TOOL_WIDTH),
  583.         LABEL(ABS_VOLUME),
  584.         LABEL(ABS_MISC),
  585.         LABEL(ABS_MT_SLOT),
  586.         LABEL(ABS_MT_TOUCH_MAJOR),
  587.         LABEL(ABS_MT_TOUCH_MINOR),
  588.         LABEL(ABS_MT_WIDTH_MAJOR),
  589.         LABEL(ABS_MT_WIDTH_MINOR),
  590.         LABEL(ABS_MT_ORIENTATION),
  591.         LABEL(ABS_MT_POSITION_X),
  592.         LABEL(ABS_MT_POSITION_Y),
  593.         LABEL(ABS_MT_TOOL_TYPE),
  594.         LABEL(ABS_MT_BLOB_ID),
  595.         LABEL(ABS_MT_TRACKING_ID),
  596.         LABEL(ABS_MT_PRESSURE),
  597.         LABEL(ABS_MT_DISTANCE),
  598.         LABEL_END,
  599. };
  600.  
  601. static struct label sw_labels[] = {
  602.         LABEL(SW_LID),
  603.         LABEL(SW_TABLET_MODE),
  604.         LABEL(SW_HEADPHONE_INSERT),
  605.         LABEL(SW_RFKILL_ALL),
  606.         LABEL(SW_RADIO),
  607.         LABEL(SW_MICROPHONE_INSERT),
  608.         LABEL(SW_DOCK),
  609.         LABEL(SW_LINEOUT_INSERT),
  610.         LABEL(SW_JACK_PHYSICAL_INSERT),
  611.         LABEL(SW_VIDEOOUT_INSERT),
  612.         LABEL(SW_CAMERA_LENS_COVER),
  613.         LABEL(SW_KEYPAD_SLIDE),
  614.         LABEL(SW_FRONT_PROXIMITY),
  615.         LABEL(SW_ROTATE_LOCK),
  616.         LABEL_END,
  617. };
  618.  
  619. static struct label msc_labels[] = {
  620.         LABEL(MSC_SERIAL),
  621.         LABEL(MSC_PULSELED),
  622.         LABEL(MSC_GESTURE),
  623.         LABEL(MSC_RAW),
  624.         LABEL(MSC_SCAN),
  625.         LABEL_END,
  626. };
  627.  
  628. static struct label led_labels[] = {
  629.         LABEL(LED_NUML),
  630.         LABEL(LED_CAPSL),
  631.         LABEL(LED_SCROLLL),
  632.         LABEL(LED_COMPOSE),
  633.         LABEL(LED_KANA),
  634.         LABEL(LED_SLEEP),
  635.         LABEL(LED_SUSPEND),
  636.         LABEL(LED_MUTE),
  637.         LABEL(LED_MISC),
  638.         LABEL(LED_MAIL),
  639.         LABEL(LED_CHARGING),
  640.         LABEL_END,
  641. };
  642.  
  643. static struct label rep_labels[] = {
  644.         LABEL(REP_DELAY),
  645.         LABEL(REP_PERIOD),
  646.         LABEL_END,
  647. };
  648.  
  649. static struct label snd_labels[] = {
  650.         LABEL(SND_CLICK),
  651.         LABEL(SND_BELL),
  652.         LABEL(SND_TONE),
  653.         LABEL_END,
  654. };
  655.  
  656. #if 0
  657. static struct label id_labels[] = {
  658.         LABEL(ID_BUS),
  659.         LABEL(ID_VENDOR),
  660.         LABEL(ID_PRODUCT),
  661.         LABEL(ID_VERSION),
  662.         LABEL_END,
  663. };
  664. static struct label bus_labels[] = {
  665.         LABEL(BUS_PCI),
  666.         LABEL(BUS_ISAPNP),
  667.         LABEL(BUS_USB),
  668.         LABEL(BUS_HIL),
  669.         LABEL(BUS_BLUETOOTH),
  670.         LABEL(BUS_VIRTUAL),
  671.         LABEL(BUS_ISA),
  672.         LABEL(BUS_I8042),
  673.         LABEL(BUS_XTKBD),
  674.         LABEL(BUS_RS232),
  675.         LABEL(BUS_GAMEPORT),
  676.         LABEL(BUS_PARPORT),
  677.         LABEL(BUS_AMIGA),
  678.         LABEL(BUS_ADB),
  679.         LABEL(BUS_I2C),
  680.         LABEL(BUS_HOST),
  681.         LABEL(BUS_GSC),
  682.         LABEL(BUS_ATARI),
  683.         LABEL(BUS_SPI),
  684.         LABEL_END,
  685. };
  686. #endif
  687.  
  688. static struct label mt_tool_labels[] = {
  689.         LABEL(MT_TOOL_FINGER),
  690.         LABEL(MT_TOOL_PEN),
  691.         LABEL(MT_TOOL_MAX),
  692.         LABEL_END,
  693. };
  694.  
  695. static struct label ff_status_labels[] = {
  696.         LABEL(FF_STATUS_STOPPED),
  697.         LABEL(FF_STATUS_PLAYING),
  698.         LABEL(FF_STATUS_MAX),
  699.         LABEL_END,
  700. };
  701.  
  702. static struct label ff_labels[] = {
  703.         LABEL(FF_RUMBLE),
  704.         LABEL(FF_PERIODIC),
  705.         LABEL(FF_CONSTANT),
  706.         LABEL(FF_SPRING),
  707.         LABEL(FF_FRICTION),
  708.         LABEL(FF_DAMPER),
  709.         LABEL(FF_INERTIA),
  710.         LABEL(FF_RAMP),
  711.         LABEL(FF_SQUARE),
  712.         LABEL(FF_TRIANGLE),
  713.         LABEL(FF_SINE),
  714.         LABEL(FF_SAW_UP),
  715.         LABEL(FF_SAW_DOWN),
  716.         LABEL(FF_CUSTOM),
  717.         LABEL(FF_GAIN),
  718.         LABEL(FF_AUTOCENTER),
  719.         LABEL_END,
  720. };
  721.  
  722. static struct label key_value_labels[] = {
  723.         { "UP", 0 },
  724.         { "DOWN", 1 },
  725.         { "REPEAT", 2 },
  726.         LABEL_END,
  727. }


getevent.c

 

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <dirent.h>
  6. #include <fcntl.h>
  7. #include <sys/ioctl.h>
  8. #include <sys/inotify.h>
  9. #include <linux/limits.h>
  10. #include <sys/poll.h>
  11. #include <linux/input.h>
  12. #include <errno.h>
  13. #include <unistd.h>
  14. #include <time.h>
  15.  
  16. #include "getevent.h"
  17.  
  18. static struct pollfd *ufds;
  19. static char **device_names;
  20. static int nfds;
  21.  
  22. enum {
  23.     PRINT_DEVICE_ERRORS = 1U << 0,
  24.     PRINT_DEVICE = 1U << 1,
  25.     PRINT_DEVICE_NAME = 1U << 2,
  26.     PRINT_DEVICE_INFO = 1U << 3,
  27.     PRINT_VERSION = 1U << 4,
  28.     PRINT_POSSIBLE_EVENTS = 1U << 5,
  29.     PRINT_INPUT_PROPS = 1U << 6,
  30.     PRINT_HID_DESCRIPTOR = 1U << 7,
  31.  
  32.     PRINT_ALL_INFO = (1U << 8) - 1,
  33.  
  34.     PRINT_LABELS = 1U << 16,
  35. };
  36.  
  37. static const char *get_label(const struct label *labels, int value)
  38. {
  39.     while(labels->name && value != labels->value) {
  40.         labels++;
  41.     }
  42.     return labels->name;
  43. }
  44.  
  45. static int print_input_props(int fd)
  46. {
  47.     uint8_t bits[INPUT_PROP_CNT / 8];
  48.     int i, j;
  49.     int res;
  50.     int count;
  51.     const char *bit_label;
  52.  
  53.     printf(" input props:\n");
  54.     res = ioctl(fd, EVIOCGPROP(sizeof(bits)), bits);
  55.     if(res < 0) {
  56.         printf(" );
  57.         return 1;
  58.     }
  59.     count = 0;
  60.     for(i = 0; i < res; i++) {
  61.         for(j = 0; j < 8; j++) {
  62.             if (bits[i] & 1 << j) {
  63.                 bit_label = get_label(input_prop_labels, i * 8 + j);
  64.                 if(bit_label)
  65.                     printf(" %s\n", bit_label);
  66.                 else
  67.                     printf(" %04x\n", i * 8 + j);
  68.                 count++;
  69.             }
  70.         }
  71.     }
  72.     if (!count)
  73.         printf(" \n");
  74.     return 0;
  75. }
  76.  
  77. static int print_possible_events(int fd, int print_flags)
  78. {
  79.     uint8_t *bits = NULL;
  80.     ssize_t bits_size = 0;
  81.     const char* label;
  82.     int i, j, k;
  83.     int res, res2;
  84.     struct label* bit_labels;
  85.     const char *bit_label;
  86.  
  87.     printf(" events:\n");
  88.     for(i = EV_KEY; i <= EV_MAX; i++) { // skip EV_SYN since we cannot query its available codes
  89.         int count = 0;
  90.         while(1) {
  91.             res = ioctl(fd, EVIOCGBIT(i, bits_size), bits);
  92.             if(res < bits_size)
  93.                 break;
  94.             bits_size = res + 16;
  95.             bits = realloc(bits, bits_size * 2);
  96.             if(bits == NULL) {
  97.                 fprintf(stderr, "failed to allocate buffer of size %d\n", (int)bits_size);
  98.                 return 1;
  99.             }
  100.         }
  101.         res2 = 0;
  102.         switch(i) {
  103.             case EV_KEY:
  104.                 res2 = ioctl(fd, EVIOCGKEY(res), bits + bits_size);
  105.                 label = "KEY";
  106.                 bit_labels = key_labels;
  107.                 break;
  108.             case EV_REL:
  109.                 label = "REL";
  110.                 bit_labels = rel_labels;
  111.                 break;
  112.             case EV_ABS:
  113.                 label = "ABS";
  114.                 bit_labels = abs_labels;
  115.                 break;
  116.             case EV_MSC:
  117.                 label = "MSC";
  118.                 bit_labels = msc_labels;
  119.                 break;
  120.             case EV_LED:
  121.                 res2 = ioctl(fd, EVIOCGLED(res), bits + bits_size);
  122.                 label = "LED";
  123.                 bit_labels = led_labels;
  124.                 break;
  125.             case EV_SND:
  126.                 res2 = ioctl(fd, EVIOCGSND(res), bits + bits_size);
  127.                 label = "SND";
  128.                 bit_labels = snd_labels;
  129.                 break;
  130.             case EV_SW:
  131.                 res2 = ioctl(fd, EVIOCGSW(bits_size), bits + bits_size);
  132.                 label = "SW ";
  133.                 bit_labels = sw_labels;
  134.                 break;
  135.             case EV_REP:
  136.                 label = "REP";
  137.                 bit_labels = rep_labels;
  138.                 break;
  139.             case EV_FF:
  140.                 label = "FF ";
  141.                 bit_labels = ff_labels;
  142.                 break;
  143.             case EV_PWR:
  144.                 label = "PWR";
  145.                 bit_labels = NULL;
  146.                 break;
  147.             case EV_FF_STATUS:
  148.                 label = "FFS";
  149.                 bit_labels = ff_status_labels;
  150.                 break;
  151.             default:
  152.                 res2 = 0;
  153.                 label = "???";
  154.                 bit_labels = NULL;
  155.         }
  156.         for(j = 0; j < res; j++) {
  157.             for(k = 0; k < 8; k++)
  158.                 if(bits[j] & 1 << k) {
  159.                     char down;
  160.                     if(j < res2 && (bits[j + bits_size] & 1 << k))
  161.                         down = '*';
  162.                     else
  163.                         down = ' ';
  164.                     if(count == 0)
  165.                         printf(" %s (%04x):", label, i);
  166.                     else if((count & (print_flags & PRINT_LABELS ? 0x3 : 0x7)) == 0 || i == EV_ABS)
  167.                         printf("\n ");
  168.                     if(bit_labels && (print_flags & PRINT_LABELS)) {
  169.                         bit_label = get_label(bit_labels, j * 8 + k);
  170.                         if(bit_label)
  171.                             printf(" %.20s%c%*s", bit_label, down, (int) (20 - strlen(bit_label)), "");
  172.                         else
  173.                             printf(" %04x%c ", j * 8 + k, down);
  174.                     } else {
  175.                         printf(" %04x%c", j * 8 + k, down);
  176.                     }
  177.                     if(i == EV_ABS) {
  178.                         struct input_absinfo abs;
  179.                         if(ioctl(fd, EVIOCGABS(j * 8 + k), &abs) == 0) {
  180.                             printf(" : value %d, min %d, max %d, fuzz %d, flat %d, resolution %d",
  181.                                 abs.value, abs.minimum, abs.maximum, abs.fuzz, abs.flat,
  182.                                 abs.resolution);
  183.                         }
  184.                     }
  185.                     count++;
  186.                 }
  187.         }
  188.         if(count)
  189.             printf("\n");
  190.     }
  191.     free(bits);
  192.     return 0;
  193. }
  194.  
  195. static void print_event(int type, int code, int value, int print_flags)
  196. {
  197.     const char *type_label, *code_label, *value_label;
  198.  
  199.     if (print_flags & PRINT_LABELS) {
  200.         type_label = get_label(ev_labels, type);
  201.         code_label = NULL;
  202.         value_label = NULL;
  203.  
  204.         switch(type) {
  205.             case EV_SYN:
  206.                 code_label = get_label(syn_labels, code);
  207.                 break;
  208.             case EV_KEY:
  209.                 code_label = get_label(key_labels, code);
  210.                 value_label = get_label(key_value_labels, value);
  211.                 break;
  212.             case EV_REL:
  213.                 code_label = get_label(rel_labels, code);
  214.                 break;
  215.             case EV_ABS:
  216.                 code_label = get_label(abs_labels, code);
  217.                 switch(code) {
  218.                     case ABS_MT_TOOL_TYPE:
  219.                         value_label = get_label(mt_tool_labels, value);
  220.                 }
  221.                 break;
  222.             case EV_MSC:
  223.                 code_label = get_label(msc_labels, code);
  224.                 break;
  225.             case EV_LED:
  226.                 code_label = get_label(led_labels, code);
  227.                 break;
  228.             case EV_SND:
  229.                 code_label = get_label(snd_labels, code);
  230.                 break;
  231.             case EV_SW:
  232.                 code_label = get_label(sw_labels, code);
  233.                 break;
  234.             case EV_REP:
  235.                 code_label = get_label(rep_labels, code);
  236.                 break;
  237.             case EV_FF:
  238.                 code_label = get_label(ff_labels, code);
  239.                 break;
  240.             case EV_FF_STATUS:
  241.                 code_label = get_label(ff_status_labels, code);
  242.                 break;
  243.         }
  244.  
  245.         if (type_label)
  246.             printf("%-12.12s", type_label);
  247.         else
  248.             printf("%04x ", type);
  249.         if (code_label)
  250.             printf(" %-20.20s", code_label);
  251.         else
  252.             printf(" %04x ", code);
  253.         if (value_label)
  254.             printf(" %-20.20s", value_label);
  255.         else
  256.             printf(" %08x ", value);
  257.     } else {
  258.         printf("%04x %04x %08x", type, code, value);
  259.     }
  260. }
  261.  
  262. static void print_hid_descriptor(int bus, int vendor, int product)
  263. {
  264.     const char *dirname = "/sys/kernel/debug/hid";
  265.     char prefix[16];
  266.     DIR *dir;
  267.     struct dirent *de;
  268.     char filename[PATH_MAX];
  269.     FILE *file;
  270.     char line[2048];
  271.  
  272.     snprintf(prefix, sizeof(prefix), "%04X:%04X:%04X.", bus, vendor, product);
  273.  
  274.     dir = opendir(dirname);
  275.     if(dir == NULL)
  276.         return;
  277.     while((de = readdir(dir))) {
  278.         if (strstr(de->d_name, prefix) == de->d_name) {
  279.             snprintf(filename, sizeof(filename), "%s/%s/rdesc", dirname, de->d_name);
  280.  
  281.             file = fopen(filename, "r");
  282.             if (file) {
  283.                 printf(" HID descriptor: %s\n\n", de->d_name);
  284.                 while (fgets(line, sizeof(line), file)) {
  285.                     fputs(" ", stdout);
  286.                     fputs(line, stdout);
  287.                 }
  288.                 fclose(file);
  289.                 puts("");
  290.             }
  291.         }
  292.     }
  293.     closedir(dir);
  294. }
  295.  
  296. static int open_device(const char *device, int print_flags)
  297. {
  298.     int version;
  299.     int fd;
  300.     int clkid = CLOCK_MONOTONIC;
  301.     struct pollfd *new_ufds;
  302.     char **new_device_names;
  303.     char name[80];
  304.     char location[80];
  305.     char idstr[80];
  306.     struct input_id id;
  307.  
  308.     fd = open(device, O_RDWR);
  309.     if(fd < 0) {
  310.         if(print_flags & PRINT_DEVICE_ERRORS)
  311.             fprintf(stderr, "could not open %s, %s\n", device, strerror(errno));
  312.         return -1;
  313.     }
  314.     
  315.     if(ioctl(fd, EVIOCGVERSION, &version)) {
  316.         if(print_flags & PRINT_DEVICE_ERRORS)
  317.             fprintf(stderr, "could not get driver version for %s, %s\n", device, strerror(errno));
  318.         return -1;
  319.     }
  320.     if(ioctl(fd, EVIOCGID, &id)) {
  321.         if(print_flags & PRINT_DEVICE_ERRORS)
  322.             fprintf(stderr, "could not get driver id for %s, %s\n", device, strerror(errno));
  323.         return -1;
  324.     }
  325.     name[sizeof(name) - 1] = '\0';
  326.     location[sizeof(location) - 1] = '\0';
  327.     idstr[sizeof(idstr) - 1] = '\0';
  328.     if(ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
  329.         //fprintf(stderr, "could not get device name for %s, %s\n", device, strerror(errno));
  330.         name[0] = '\0';
  331.     }
  332.     if(ioctl(fd, EVIOCGPHYS(sizeof(location) - 1), &location) < 1) {
  333.         //fprintf(stderr, "could not get location for %s, %s\n", device, strerror(errno));
  334.         location[0] = '\0';
  335.     }
  336.     if(ioctl(fd, EVIOCGUNIQ(sizeof(idstr) - 1), &idstr) < 1) {
  337.         //fprintf(stderr, "could not get idstring for %s, %s\n", device, strerror(errno));
  338.         idstr[0] = '\0';
  339.     }
  340.  
  341.     if (ioctl(fd, EVIOCSCLOCKID, &clkid) != 0) {
  342.         fprintf(stderr, "Can't enable monotonic clock reporting: %s\n", strerror(errno));
  343.         // a non-fatal error
  344.     }
  345.  
  346.     new_ufds = realloc(ufds, sizeof(ufds[0]) * (nfds + 1));
  347.     if(new_ufds == NULL) {
  348.         fprintf(stderr, "out of memory\n");
  349.         return -1;
  350.     }
  351.     ufds = new_ufds;
  352.     new_device_names = realloc(device_names, sizeof(device_names[0]) * (nfds + 1));
  353.     if(new_device_names == NULL) {
  354.         fprintf(stderr, "out of memory\n");
  355.         return -1;
  356.     }
  357.     device_names = new_device_names;
  358.  
  359.     if(print_flags & PRINT_DEVICE)
  360.         printf("add device %d: %s\n", nfds, device);
  361.     if(print_flags & PRINT_DEVICE_INFO)
  362.         printf(" bus: %04x\n"
  363.                " vendor %04x\n"
  364.                " product %04x\n"
  365.                " version %04x\n",
  366.                id.bustype, id.vendor, id.product, id.version);
  367.     if(print_flags & PRINT_DEVICE_NAME)
  368.         printf(" name: \"%s\"\n", name);
  369.     if(print_flags & PRINT_DEVICE_INFO)
  370.         printf(" location: \"%s\"\n"
  371.                " id: \"%s\"\n", location, idstr);
  372.     if(print_flags & PRINT_VERSION)
  373.         printf(" version: %d.%d.%d\n",
  374.                version >> 16, (version >> 8) & 0xff, version & 0xff);
  375.  
  376.     if(print_flags & PRINT_POSSIBLE_EVENTS) {
  377.         print_possible_events(fd, print_flags);
  378.     }
  379.  
  380.     if(print_flags & PRINT_INPUT_PROPS) {
  381.         print_input_props(fd);
  382.     }
  383.     if(print_flags & PRINT_HID_DESCRIPTOR) {
  384.         print_hid_descriptor(id.bustype, id.vendor, id.product);
  385.     }
  386.  
  387.     ufds[nfds].fd = fd;
  388.     ufds[nfds].events = POLLIN;
  389.     device_names[nfds] = strdup(device);
  390.     nfds++;
  391.  
  392.     return 0;
  393. }
  394.  
  395. int close_device(const char *device, int print_flags)
  396. {
  397.     int i;
  398.     for(i = 1; i < nfds; i++) {
  399.         if(strcmp(device_names[i], device) == 0) {
  400.             int count = nfds - i - 1;
  401.             if(print_flags & PRINT_DEVICE)
  402.                 printf("remove device %d: %s\n", i, device);
  403.             free(device_names[i]);
  404.             memmove(device_names + i, device_names + i + 1, sizeof(device_names[0]) * count);
  405.             memmove(ufds + i, ufds + i + 1, sizeof(ufds[0]) * count);
  406.             nfds--;
  407.             return 0;
  408.         }
  409.     }
  410.     if(print_flags & PRINT_DEVICE_ERRORS)
  411.         fprintf(stderr, "remote device: %s not found\n", device);
  412.     return -1;
  413. }
  414.  
  415. static int read_notify(const char *dirname, int nfd, int print_flags)
  416. {
  417.     int res;
  418.     char devname[PATH_MAX];
  419.     char *filename;
  420.     char event_buf[512];
  421.     int event_size;
  422.     int event_pos = 0;
  423.     struct inotify_event *event;
  424.  
  425.     res = read(nfd, event_buf, sizeof(event_buf));
  426.     if(res < (int)sizeof(*event)) {
  427.         if(errno == EINTR)
  428.             return 0;
  429.         fprintf(stderr, "could not get event, %s\n", strerror(errno));
  430.         return 1;
  431.     }
  432.     //printf("got %d bytes of event information\n", res);
  433.  
  434.     strcpy(devname, dirname);
  435.     filename = devname + strlen(devname);
  436.     *filename++ = '/';
  437.  
  438.     while(res >= (int)sizeof(*event)) {
  439.         event = (struct inotify_event *)(event_buf + event_pos);
  440.         //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
  441.         if(event->len) {
  442.             strcpy(filename, event->name);
  443.             if(event->mask & IN_CREATE) {
  444.                 open_device(devname, print_flags);
  445.             }
  446.             else {
  447.                 close_device(devname, print_flags);
  448.             }
  449.         }
  450.         event_size = sizeof(*event) + event->len;
  451.         res -= event_size;
  452.         event_pos += event_size;
  453.     }
  454.     return 0;
  455. }
  456.  
  457. static int scan_dir(const char *dirname, int print_flags)
  458. {
  459.     char devname[PATH_MAX];
  460.     char *filename;
  461.     DIR *dir;
  462.     struct dirent *de;
  463.     dir = opendir(dirname);
  464.     if(dir == NULL)
  465.         return -1;
  466.     strcpy(devname, dirname);
  467.     filename = devname + strlen(devname);
  468.     *filename++ = '/';
  469.     while((de = readdir(dir))) {
  470.         if(de->d_name[0] == '.' &&
  471.            (de->d_name[1] == '\0' ||
  472.             (de->d_name[1] == '.' && de->d_name[2] == '\0')))
  473.             continue;
  474.         strcpy(filename, de->d_name);
  475.         open_device(devname, print_flags);
  476.     }
  477.     closedir(dir);
  478.     return 0;
  479. }
  480.  
  481. static void usage(char *name)
  482. {
  483.     fprintf(stderr, "Usage: %s [-t] [-n] [-s switchmask] [-S] [-v [mask]] [-d] [-p] [-i] [-l] [-q] [-c count] [-r] [device]\n", name);
  484.     fprintf(stderr, " -t: show time stamps\n");
  485.     fprintf(stderr, " -n: don't print newlines\n");
  486.     fprintf(stderr, " -s: print switch states for given bits\n");
  487.     fprintf(stderr, " -S: print all switch states\n");
  488.     fprintf(stderr, " -v: verbosity mask (errs=1, dev=2, name=4, info=8, vers=16, pos. events=32, props=64)\n");
  489.     fprintf(stderr, " -d: show HID descriptor, if available\n");
  490.     fprintf(stderr, " -p: show possible events (errs, dev, name, pos. events)\n");
  491.     fprintf(stderr, " -i: show all device info and possible events\n");
  492.     fprintf(stderr, " -l: label event types and names in plain text\n");
  493.     fprintf(stderr, " -q: quiet (clear verbosity mask)\n");
  494.     fprintf(stderr, " -c: print given number of events then exit\n");
  495.     fprintf(stderr, " -r: print rate events are received\n");
  496. }
  497.  
  498. int main(int argc, char *argv[])
  499. {
  500.     int c;
  501.     int i;
  502.     int res;
  503.     int get_time = 0;
  504.     int print_device = 0;
  505.     char *newline = "\n";
  506.     uint16_t get_switch = 0;
  507.     struct input_event event;
  508.     int print_flags = 0;
  509.     int print_flags_set = 0;
  510.     int dont_block = -1;
  511.     int event_count = 0;
  512.     int sync_rate = 0;
  513.     int64_t last_sync_time = 0;
  514.     const char *device = NULL;
  515.     const char *device_path = "/dev/input";
  516.  
  517.     opterr = 0;
  518.     do {
  519.         c = getopt(argc, argv, "tns:Sv::dpilqc:rh");
  520.         if (c == EOF)
  521.             break;
  522.         switch (c) {
  523.         case 't':
  524.             get_time = 1;
  525.             break;
  526.         case 'n':
  527.             newline = "";
  528.             break;
  529.         case 's':
  530.             get_switch = strtoul(optarg, NULL, 0);
  531.             if(dont_block == -1)
  532.                 dont_block = 1;
  533.             break;
  534.         case 'S':
  535.             get_switch = ~0;
  536.             if(dont_block == -1)
  537.                 dont_block = 1;
  538.             break;
  539.         case 'v':
  540.             if(optarg)
  541.                 print_flags |= strtoul(optarg, NULL, 0);
  542.             else
  543.                 print_flags |= PRINT_DEVICE | PRINT_DEVICE_NAME | PRINT_DEVICE_INFO | PRINT_VERSION;
  544.             print_flags_set = 1;
  545.             break;
  546.         case 'd':
  547.             print_flags |= PRINT_HID_DESCRIPTOR;
  548.             break;
  549.         case 'p':
  550.             print_flags |= PRINT_DEVICE_ERRORS | PRINT_DEVICE
  551.                     | PRINT_DEVICE_NAME | PRINT_POSSIBLE_EVENTS | PRINT_INPUT_PROPS;
  552.             print_flags_set = 1;
  553.             if(dont_block == -1)
  554.                 dont_block = 1;
  555.             break;
  556.         case 'i':
  557.             print_flags |= PRINT_ALL_INFO;
  558.             print_flags_set = 1;
  559.             if(dont_block == -1)
  560.                 dont_block = 1;
  561.             break;
  562.         case 'l':
  563.             print_flags |= PRINT_LABELS;
  564.             break;
  565.         case 'q':
  566.             print_flags_set = 1;
  567.             break;
  568.         case 'c':
  569.             event_count = atoi(optarg);
  570.             dont_block = 0;
  571.             break;
  572.         case 'r':
  573.             sync_rate = 1;
  574.             break;
  575.         case '?':
  576.             fprintf(stderr, "%s: invalid option -%c\n",
  577.                 argv[0], optopt);
  578.         case 'h':
  579.             usage(argv[0]);
  580.             exit(1);
  581.         }
  582.     } while (1);
  583.     if(dont_block == -1)
  584.         dont_block = 0;
  585.  
  586.     if (optind + 1 == argc) {
  587.         device = argv[optind];
  588.         optind++;
  589.     }
  590.     if (optind != argc) {
  591.         usage(argv[0]);
  592.         exit(1);
  593.     }
  594.     nfds = 1;
  595.     ufds = calloc(1, sizeof(ufds[0]));
  596.     ufds[0].fd = inotify_init();
  597.     ufds[0].events = POLLIN;
  598.     if(device) {
  599.         if(!print_flags_set)
  600.             print_flags |= PRINT_DEVICE_ERRORS;
  601.         res = open_device(device, print_flags);
  602.         if(res < 0) {
  603.             return 1;
  604.         }
  605.     } else {
  606.         if(!print_flags_set)
  607.             print_flags |= PRINT_DEVICE_ERRORS | PRINT_DEVICE | PRINT_DEVICE_NAME;
  608.         print_device = 1;
  609.         res = inotify_add_watch(ufds[0].fd, device_path, IN_DELETE | IN_CREATE);
  610.         if(res < 0) {
  611.             fprintf(stderr, "could not add watch for %s, %s\n", device_path, strerror(errno));
  612.             return 1;
  613.         }
  614.         res = scan_dir(device_path, print_flags);
  615.         if(res < 0) {
  616.             fprintf(stderr, "scan dir failed for %s\n", device_path);
  617.             return 1;
  618.         }
  619.     }
  620.  
  621.     if(get_switch) {
  622.         for(i = 1; i < nfds; i++) {
  623.             uint16_t sw;
  624.             res = ioctl(ufds[i].fd, EVIOCGSW(1), &sw);
  625.             if(res < 0) {
  626.                 fprintf(stderr, "could not get switch state, %s\n", strerror(errno));
  627.                 return 1;
  628.             }
  629.             sw &= get_switch;
  630.             printf("%04x%s", sw, newline);
  631.         }
  632.     }
  633.  
  634.     if(dont_block)
  635.         return 0;
  636.  
  637.     while(1) {
  638.         //int pollres =
  639.         poll(ufds, nfds, -1);
  640.         //printf("poll %d, returned %d\n", nfds, pollres);
  641.         if(ufds[0].revents & POLLIN) {
  642.             read_notify(device_path, ufds[0].fd, print_flags);
  643.         }
  644.         for(i = 1; i < nfds; i++) {
  645.             if(ufds[i].revents) {
  646.                 if(ufds[i].revents & POLLIN) {
  647.                     res = read(ufds[i].fd, &event, sizeof(event));
  648.                     if(res < (int)sizeof(event)) {
  649.                         fprintf(stderr, "could not get event\n");
  650.                         return 1;
  651.                     }
  652.                     if(get_time) {
  653.                         printf("[%8ld.%06ld] ", event.time.tv_sec, event.time.tv_usec);
  654.                     }
  655.                     if(print_device)
  656.                         printf("%s: ", device_names[i]);
  657.                     print_event(event.type, event.code, event.value, print_flags);
  658.                     if(sync_rate && event.type == 0 && event.code == 0) {
  659.                         int64_t now = event.time.tv_sec * 1000000LL + event.time.tv_usec;
  660.                         if(last_sync_time)
  661.                             printf(" rate %lld", 1000000LL / (now - last_sync_time));
  662.                         last_sync_time = now;
  663.                     }
  664.                     printf("%s", newline);
  665.                     if(event_count && --event_count == 0)
  666.                         return 0;
  667.                 }
  668.             }
  669.         }
  670.     }
  671.  
  672.     return 0;
  673. }

gcc getevent.c getevent.h -I ./ -o getevent
作者:帅得不敢出门  程序员群:31843264

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值