event事件 linux
2013-03-22 09:31:12 gangyanliang 阅读数 4282

linux kernel 中很多设备可以上报input事件,比方键盘、鼠标、加速传感器 (accelerometer sensor)等等,为在开发中调试分析这些事件,有个很好的网上工具evtest可以帮助分析event:

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




修改后:




/*
 * $Id: evtest.c,v 1.23 2005/02/06 13:51:42 vojtech Exp $
 *
 *  Copyright (c) 1999-2000 Vojtech Pavlik
 *
 *  Event device test program
 */


/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * Should you need to contact me, the author, you can do so either by
 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
 */


#include <stdint.h>


#include <linux/input.h>


#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>


#ifndef EV_SYN
#define EV_SYN 0
#endif


char *events[EV_MAX + 1] = {
[0 ... EV_MAX] = NULL,
[EV_SYN] = "Sync", [EV_KEY] = "Key",
[EV_REL] = "Relative", [EV_ABS] = "Absolute",
[EV_MSC] = "Misc", [EV_LED] = "LED",
[EV_SND] = "Sound", [EV_REP] = "Repeat",
[EV_FF] = "ForceFeedback", [EV_PWR] = "Power",
[EV_FF_STATUS] = "ForceFeedbackStatus",
};


char *keys[KEY_MAX + 1] = {
[0 ... KEY_MAX] = NULL,
[KEY_RESERVED] = "Reserved", [KEY_ESC] = "Esc",
[KEY_1] = "1", [KEY_2] = "2",
[KEY_3] = "3", [KEY_4] = "4",
[KEY_5] = "5", [KEY_6] = "6",
[KEY_7] = "7", [KEY_8] = "8",
[KEY_9] = "9", [KEY_0] = "0",
[KEY_MINUS] = "Minus", [KEY_EQUAL] = "Equal",
[KEY_BACKSPACE] = "Backspace", [KEY_TAB] = "Tab",
[KEY_Q] = "Q", [KEY_W] = "W",
[KEY_E] = "E", [KEY_R] = "R",
[KEY_T] = "T", [KEY_Y] = "Y",
[KEY_U] = "U", [KEY_I] = "I",
[KEY_O] = "O", [KEY_P] = "P",
[KEY_LEFTBRACE] = "LeftBrace", [KEY_RIGHTBRACE] = "RightBrace",
[KEY_ENTER] = "Enter", [KEY_LEFTCTRL] = "LeftControl",
[KEY_A] = "A", [KEY_S] = "S",
[KEY_D] = "D", [KEY_F] = "F",
[KEY_G] = "G", [KEY_H] = "H",
[KEY_J] = "J", [KEY_K] = "K",
[KEY_L] = "L", [KEY_SEMICOLON] = "Semicolon",
[KEY_APOSTROPHE] = "Apostrophe", [KEY_GRAVE] = "Grave",
[KEY_LEFTSHIFT] = "LeftShift", [KEY_BACKSLASH] = "BackSlash",
[KEY_Z] = "Z", [KEY_X] = "X",
[KEY_C] = "C", [KEY_V] = "V",
[KEY_B] = "B", [KEY_N] = "N",
[KEY_M] = "M", [KEY_COMMA] = "Comma",
[KEY_DOT] = "Dot", [KEY_SLASH] = "Slash",
[KEY_RIGHTSHIFT] = "RightShift", [KEY_KPASTERISK] = "KPAsterisk",
[KEY_LEFTALT] = "LeftAlt", [KEY_SPACE] = "Space",
[KEY_CAPSLOCK] = "CapsLock", [KEY_F1] = "F1",
[KEY_F2] = "F2", [KEY_F3] = "F3",
[KEY_F4] = "F4", [KEY_F5] = "F5",
[KEY_F6] = "F6", [KEY_F7] = "F7",
[KEY_F8] = "F8", [KEY_F9] = "F9",
[KEY_F10] = "F10", [KEY_NUMLOCK] = "NumLock",
[KEY_SCROLLLOCK] = "ScrollLock", [KEY_KP7] = "KP7",
[KEY_KP8] = "KP8", [KEY_KP9] = "KP9",
[KEY_KPMINUS] = "KPMinus", [KEY_KP4] = "KP4",
[KEY_KP5] = "KP5", [KEY_KP6] = "KP6",
[KEY_KPPLUS] = "KPPlus", [KEY_KP1] = "KP1",
[KEY_KP2] = "KP2", [KEY_KP3] = "KP3",
[KEY_KP0] = "KP0", [KEY_KPDOT] = "KPDot",
[KEY_ZENKAKUHANKAKU] = "Zenkaku/Hankaku", [KEY_102ND] = "102nd",
[KEY_F11] = "F11", [KEY_F12] = "F12",
[KEY_RO] = "RO", [KEY_KATAKANA] = "Katakana",
[KEY_HIRAGANA] = "HIRAGANA", [KEY_HENKAN] = "Henkan",
[KEY_KATAKANAHIRAGANA] = "Katakana/Hiragana", [KEY_MUHENKAN] = "Muhenkan",
[KEY_KPJPCOMMA] = "KPJpComma", [KEY_KPENTER] = "KPEnter",
[KEY_RIGHTCTRL] = "RightCtrl", [KEY_KPSLASH] = "KPSlash",
[KEY_SYSRQ] = "SysRq", [KEY_RIGHTALT] = "RightAlt",
[KEY_LINEFEED] = "LineFeed", [KEY_HOME] = "Home",
[KEY_UP] = "Up", [KEY_PAGEUP] = "PageUp",
[KEY_LEFT] = "Left", [KEY_RIGHT] = "Right",
[KEY_END] = "End", [KEY_DOWN] = "Down",
[KEY_PAGEDOWN] = "PageDown", [KEY_INSERT] = "Insert",
[KEY_DELETE] = "Delete", [KEY_MACRO] = "Macro",
[KEY_MUTE] = "Mute", [KEY_VOLUMEDOWN] = "VolumeDown",
[KEY_VOLUMEUP] = "VolumeUp", [KEY_POWER] = "Power",
[KEY_KPEQUAL] = "KPEqual", [KEY_KPPLUSMINUS] = "KPPlusMinus",
[KEY_PAUSE] = "Pause", [KEY_KPCOMMA] = "KPComma",
[KEY_HANGUEL] = "Hanguel", [KEY_HANJA] = "Hanja",
[KEY_YEN] = "Yen", [KEY_LEFTMETA] = "LeftMeta",
[KEY_RIGHTMETA] = "RightMeta", [KEY_COMPOSE] = "Compose",
[KEY_STOP] = "Stop", [KEY_AGAIN] = "Again",
[KEY_PROPS] = "Props", [KEY_UNDO] = "Undo",
[KEY_FRONT] = "Front", [KEY_COPY] = "Copy",
[KEY_OPEN] = "Open", [KEY_PASTE] = "Paste",
[KEY_FIND] = "Find", [KEY_CUT] = "Cut",
[KEY_HELP] = "Help", [KEY_MENU] = "Menu",
[KEY_CALC] = "Calc", [KEY_SETUP] = "Setup",
[KEY_SLEEP] = "Sleep", [KEY_WAKEUP] = "WakeUp",
[KEY_FILE] = "File", [KEY_SENDFILE] = "SendFile",
[KEY_DELETEFILE] = "DeleteFile", [KEY_XFER] = "X-fer",
[KEY_PROG1] = "Prog1", [KEY_PROG2] = "Prog2",
[KEY_WWW] = "WWW", [KEY_MSDOS] = "MSDOS",
[KEY_COFFEE] = "Coffee", [KEY_DIRECTION] = "Direction",
[KEY_CYCLEWINDOWS] = "CycleWindows", [KEY_MAIL] = "Mail",
[KEY_BOOKMARKS] = "Bookmarks", [KEY_COMPUTER] = "Computer",
[KEY_BACK] = "Back", [KEY_FORWARD] = "Forward",
[KEY_CLOSECD] = "CloseCD", [KEY_EJECTCD] = "EjectCD",
[KEY_EJECTCLOSECD] = "EjectCloseCD", [KEY_NEXTSONG] = "NextSong",
[KEY_PLAYPAUSE] = "PlayPause", [KEY_PREVIOUSSONG] = "PreviousSong",
[KEY_STOPCD] = "StopCD", [KEY_RECORD] = "Record",
[KEY_REWIND] = "Rewind", [KEY_PHONE] = "Phone",
[KEY_ISO] = "ISOKey", [KEY_CONFIG] = "Config",
[KEY_HOMEPAGE] = "HomePage", [KEY_REFRESH] = "Refresh",
[KEY_EXIT] = "Exit", [KEY_MOVE] = "Move",
[KEY_EDIT] = "Edit", [KEY_SCROLLUP] = "ScrollUp",
[KEY_SCROLLDOWN] = "ScrollDown", [KEY_KPLEFTPAREN] = "KPLeftParenthesis",
[KEY_KPRIGHTPAREN] = "KPRightParenthesis", [KEY_F13] = "F13",
[KEY_F14] = "F14", [KEY_F15] = "F15",
[KEY_F16] = "F16", [KEY_F17] = "F17",
[KEY_F18] = "F18", [KEY_F19] = "F19",
[KEY_F20] = "F20", [KEY_F21] = "F21",
[KEY_F22] = "F22", [KEY_F23] = "F23",
[KEY_F24] = "F24", [KEY_PLAYCD] = "PlayCD",
[KEY_PAUSECD] = "PauseCD", [KEY_PROG3] = "Prog3",
[KEY_PROG4] = "Prog4", [KEY_SUSPEND] = "Suspend",
[KEY_CLOSE] = "Close", [KEY_PLAY] = "Play",
[KEY_FASTFORWARD] = "Fast Forward", [KEY_BASSBOOST] = "Bass Boost",
[KEY_PRINT] = "Print", [KEY_HP] = "HP",
[KEY_CAMERA] = "Camera", [KEY_SOUND] = "Sound",
[KEY_QUESTION] = "Question", [KEY_EMAIL] = "Email",
[KEY_CHAT] = "Chat", [KEY_SEARCH] = "Search",
[KEY_CONNECT] = "Connect", [KEY_FINANCE] = "Finance",
[KEY_SPORT] = "Sport", [KEY_SHOP] = "Shop",
[KEY_ALTERASE] = "Alternate Erase", [KEY_CANCEL] = "Cancel",
[KEY_BRIGHTNESSDOWN] = "Brightness down", [KEY_BRIGHTNESSUP] = "Brightness up",
[KEY_MEDIA] = "Media", [KEY_UNKNOWN] = "Unknown",
[BTN_0] = "Btn0", [BTN_1] = "Btn1",
[BTN_2] = "Btn2", [BTN_3] = "Btn3",
[BTN_4] = "Btn4", [BTN_5] = "Btn5",
[BTN_6] = "Btn6", [BTN_7] = "Btn7",
[BTN_8] = "Btn8", [BTN_9] = "Btn9",
[BTN_LEFT] = "LeftBtn", [BTN_RIGHT] = "RightBtn",
[BTN_MIDDLE] = "MiddleBtn", [BTN_SIDE] = "SideBtn",
[BTN_EXTRA] = "ExtraBtn", [BTN_FORWARD] = "ForwardBtn",
[BTN_BACK] = "BackBtn", [BTN_TASK] = "TaskBtn",
[BTN_TRIGGER] = "Trigger", [BTN_THUMB] = "ThumbBtn",
[BTN_THUMB2] = "ThumbBtn2", [BTN_TOP] = "TopBtn",
[BTN_TOP2] = "TopBtn2", [BTN_PINKIE] = "PinkieBtn",
[BTN_BASE] = "BaseBtn", [BTN_BASE2] = "BaseBtn2",
[BTN_BASE3] = "BaseBtn3", [BTN_BASE4] = "BaseBtn4",
[BTN_BASE5] = "BaseBtn5", [BTN_BASE6] = "BaseBtn6",
[BTN_DEAD] = "BtnDead", [BTN_A] = "BtnA",
[BTN_B] = "BtnB", [BTN_C] = "BtnC",
[BTN_X] = "BtnX", [BTN_Y] = "BtnY",
[BTN_Z] = "BtnZ", [BTN_TL] = "BtnTL",
[BTN_TR] = "BtnTR", [BTN_TL2] = "BtnTL2",
[BTN_TR2] = "BtnTR2", [BTN_SELECT] = "BtnSelect",
[BTN_START] = "BtnStart", [BTN_MODE] = "BtnMode",
[BTN_THUMBL] = "BtnThumbL", [BTN_THUMBR] = "BtnThumbR",
[BTN_TOOL_PEN] = "ToolPen", [BTN_TOOL_RUBBER] = "ToolRubber",
[BTN_TOOL_BRUSH] = "ToolBrush", [BTN_TOOL_PENCIL] = "ToolPencil",
[BTN_TOOL_AIRBRUSH] = "ToolAirbrush", [BTN_TOOL_FINGER] = "ToolFinger",
[BTN_TOOL_MOUSE] = "ToolMouse", [BTN_TOOL_LENS] = "ToolLens",
[BTN_TOUCH] = "Touch", [BTN_STYLUS] = "Stylus",
[BTN_STYLUS2] = "Stylus2", [BTN_TOOL_DOUBLETAP] = "Tool Doubletap",
[BTN_TOOL_TRIPLETAP] = "Tool Tripletap", [BTN_GEAR_DOWN] = "WheelBtn",
[BTN_GEAR_UP] = "Gear up", [KEY_OK] = "Ok",
[KEY_SELECT] = "Select", [KEY_GOTO] = "Goto",
[KEY_CLEAR] = "Clear", [KEY_POWER2] = "Power2",
[KEY_OPTION] = "Option", [KEY_INFO] = "Info",
[KEY_TIME] = "Time", [KEY_VENDOR] = "Vendor",
[KEY_ARCHIVE] = "Archive", [KEY_PROGRAM] = "Program",
[KEY_CHANNEL] = "Channel", [KEY_FAVORITES] = "Favorites",
[KEY_EPG] = "EPG", [KEY_PVR] = "PVR",
[KEY_MHP] = "MHP", [KEY_LANGUAGE] = "Language",
[KEY_TITLE] = "Title", [KEY_SUBTITLE] = "Subtitle",
[KEY_ANGLE] = "Angle", [KEY_ZOOM] = "Zoom",
[KEY_MODE] = "Mode", [KEY_KEYBOARD] = "Keyboard",
[KEY_SCREEN] = "Screen", [KEY_PC] = "PC",
[KEY_TV] = "TV", [KEY_TV2] = "TV2",
[KEY_VCR] = "VCR", [KEY_VCR2] = "VCR2",
[KEY_SAT] = "Sat", [KEY_SAT2] = "Sat2",
[KEY_CD] = "CD", [KEY_TAPE] = "Tape",
[KEY_RADIO] = "Radio", [KEY_TUNER] = "Tuner",
[KEY_PLAYER] = "Player", [KEY_TEXT] = "Text",
[KEY_DVD] = "DVD", [KEY_AUX] = "Aux",
[KEY_MP3] = "MP3", [KEY_AUDIO] = "Audio",
[KEY_VIDEO] = "Video", [KEY_DIRECTORY] = "Directory",
[KEY_LIST] = "List", [KEY_MEMO] = "Memo",
[KEY_CALENDAR] = "Calendar", [KEY_RED] = "Red",
[KEY_GREEN] = "Green", [KEY_YELLOW] = "Yellow",
[KEY_BLUE] = "Blue", [KEY_CHANNELUP] = "ChannelUp",
[KEY_CHANNELDOWN] = "ChannelDown", [KEY_FIRST] = "First",
[KEY_LAST] = "Last", [KEY_AB] = "AB",
[KEY_NEXT] = "Next", [KEY_RESTART] = "Restart",
[KEY_SLOW] = "Slow", [KEY_SHUFFLE] = "Shuffle",
[KEY_BREAK] = "Break", [KEY_PREVIOUS] = "Previous",
[KEY_DIGITS] = "Digits", [KEY_TEEN] = "TEEN",
[KEY_TWEN] = "TWEN", [KEY_DEL_EOL] = "Delete EOL",
[KEY_DEL_EOS] = "Delete EOS", [KEY_INS_LINE] = "Insert line",
[KEY_DEL_LINE] = "Delete line",
};


char *absval[5] = { "Value", "Min  ", "Max  ", "Fuzz ", "Flat " };


char *relatives[REL_MAX + 1] = {
[0 ... REL_MAX] = NULL,
[REL_X] = "X", [REL_Y] = "Y",
[REL_Z] = "Z", [REL_HWHEEL] = "HWheel",
[REL_DIAL] = "Dial", [REL_WHEEL] = "Wheel", 
[REL_MISC] = "Misc",
};


char *absolutes[ABS_MAX + 1] = {
[0 ... ABS_MAX] = NULL,
[ABS_X] = "X", [ABS_Y] = "Y",
[ABS_Z] = "Z", [ABS_RX] = "Rx",
[ABS_RY] = "Ry", [ABS_RZ] = "Rz",
[ABS_THROTTLE] = "Throttle", [ABS_RUDDER] = "Rudder",
[ABS_WHEEL] = "Wheel", [ABS_GAS] = "Gas",
[ABS_BRAKE] = "Brake", [ABS_HAT0X] = "Hat0X",
[ABS_HAT0Y] = "Hat0Y", [ABS_HAT1X] = "Hat1X",
[ABS_HAT1Y] = "Hat1Y", [ABS_HAT2X] = "Hat2X",
[ABS_HAT2Y] = "Hat2Y", [ABS_HAT3X] = "Hat3X",
[ABS_HAT3Y] = "Hat 3Y", [ABS_PRESSURE] = "Pressure",
[ABS_DISTANCE] = "Distance", [ABS_TILT_X] = "XTilt",
[ABS_TILT_Y] = "YTilt", [ABS_TOOL_WIDTH] = "Tool Width",
[ABS_VOLUME] = "Volume", [ABS_MISC] = "Misc",
};


char *misc[MSC_MAX + 1] = {
[ 0 ... MSC_MAX] = NULL,
[MSC_SERIAL] = "Serial", [MSC_PULSELED] = "Pulseled",
[MSC_GESTURE] = "Gesture", [MSC_RAW] = "RawData",
[MSC_SCAN] = "ScanCode",
};


char *leds[LED_MAX + 1] = {
[0 ... LED_MAX] = NULL,
[LED_NUML] = "NumLock", [LED_CAPSL] = "CapsLock", 
[LED_SCROLLL] = "ScrollLock", [LED_COMPOSE] = "Compose",
[LED_KANA] = "Kana", [LED_SLEEP] = "Sleep", 
[LED_SUSPEND] = "Suspend", [LED_MUTE] = "Mute",
[LED_MISC] = "Misc",
};


char *repeats[REP_MAX + 1] = {
[0 ... REP_MAX] = NULL,
[REP_DELAY] = "Delay", [REP_PERIOD] = "Period"
};


char *sounds[SND_MAX + 1] = {
[0 ... SND_MAX] = NULL,
[SND_CLICK] = "Click", [SND_BELL] = "Bell",
[SND_TONE] = "Tone"
};


char **names[EV_MAX + 1] = {
[0 ... EV_MAX] = NULL,
[EV_SYN] = events, [EV_KEY] = keys,
[EV_REL] = relatives, [EV_ABS] = absolutes,
[EV_MSC] = misc, [EV_LED] = leds,
[EV_SND] = sounds, [EV_REP] = repeats,
};


#define BITS_PER_LONG (sizeof(long) * 8)
#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
#define OFF(x)  ((x)%BITS_PER_LONG)
#define BIT(x)  (1UL<<OFF(x))
#define LONG(x) ((x)/BITS_PER_LONG)
#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)


int main (int argc, char **argv)
{
int fd, rd, i, j, k;
struct input_event ev[64];
int version;
unsigned short id[4];
unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
char name[256] = "Unknown";
int abs[5];


if (argc < 2) {
printf("Usage: evtest /dev/input/eventX\n");
printf("Where X = input device number\n");
return 1;
}


if ((fd = open(argv[argc - 1], O_RDONLY)) < 0) {
perror("evtest");
return 1;
}


if (ioctl(fd, EVIOCGVERSION, &version)) {
perror("evtest: can't get version");
return 1;
}


printf("Input driver version is %d.%d.%d\n",
version >> 16, (version >> 8) & 0xff, version & 0xff);


ioctl(fd, EVIOCGID, id);
printf("Input device ID: bus 0x%x vendor 0x%x product 0x%x version 0x%x\n",
id[ID_BUS], id[ID_VENDOR], id[ID_PRODUCT], id[ID_VERSION]);


ioctl(fd, EVIOCGNAME(sizeof(name)), name);
printf("Input device name: \"%s\"\n", name);


memset(bit, 0, sizeof(bit));
ioctl(fd, EVIOCGBIT(0, EV_MAX), bit[0]);
printf("Supported events:\n");


for (i = 0; i < EV_MAX; i++)
if (test_bit(i, bit[0])) {
printf("  Event type %d (%s)\n", i, events[i] ? events[i] : "?");
if (!i) continue;
ioctl(fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
for (j = 0; j < KEY_MAX; j++) 
if (test_bit(j, bit[i])) {
printf("    Event code %d (%s)\n", j, names[i] ? (names[i][j] ? names[i][j] : "?") : "?");
if (i == EV_ABS) {
ioctl(fd, EVIOCGABS(j), abs);
for (k = 0; k < 5; k++)
if ((k < 3) || abs[k])
printf("      %s %6d\n", absval[k], abs[k]);
}
}
}



printf("Testing ... (interrupt to exit)\n");


while (1) {
rd = read(fd, ev, sizeof(struct input_event) * 64);


if (rd < (int) sizeof(struct input_event)) {
printf("yyy\n");
perror("\nevtest: error reading");
return 1;
}


for (i = 0; i < rd / sizeof(struct input_event); i++)


if (ev[i].type == EV_SYN) {
printf("Event: time %ld.%06ld, -------------- %s ------------\n",
ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].code ? "Config Sync" : "Report Sync" );
} else if (ev[i].type == EV_MSC && (ev[i].code == MSC_RAW || ev[i].code == MSC_SCAN)) {
printf("Event: time %ld.%06ld, type %d (%s), code %d (%s), value %02x\n",
ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].type,
events[ev[i].type] ? events[ev[i].type] : "?",
ev[i].code,
names[ev[i].type] ? (names[ev[i].type][ev[i].code] ? names[ev[i].type][ev[i].code] : "?") : "?",
ev[i].value);
} else {
printf("Event: time %ld.%06ld, type %d (%s), code %d (%s), value %d\n",
ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].type,
events[ev[i].type] ? events[ev[i].type] : "?",
ev[i].code,
names[ev[i].type] ? (names[ev[i].type][ev[i].code] ? names[ev[i].type][ev[i].code] : "?") : "?",
ev[i].value);
}


}
}

2010-12-08 13:46:00 wbtgiant 阅读数 6899

linux kernel 中很多设备可以上报input事件,比方键盘、鼠标、加速传感器 (accelerometer sensor)等等,为在开发中调试分析这些事件,有个很好的网上工具evtest可以帮助分析event:


现在android 本身集成了getevent命令,很好用。

2017-07-15 11:10:00 weixin_33885253 阅读数 12
#ifndef _HIK_EVENT_H_ 
#define _HIK_EVENT_H_

#ifdef _MSC_VER
#include <Windows.h>
#define hik_event_handle HANDLE
#else
#include <pthread.h>
typedef struct  
{
    bool state;
    bool manual_reset;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
}event_t;
#define event_handle event_t*
#endif

//返回值:NULL 出错
event_handle event_create(bool manual_reset, bool init_state);

//返回值:0 等到事件,-1出错
int event_wait(event_handle hevent);

//返回值:0 等到事件,1 超时,-1出错
int event_timedwait(event_handle hevent, long milliseconds);

//返回值:0 成功,-1出错
int event_set(event_handle hevent);

//返回值:0 成功,-1出错
int event_reset(event_handle hevent);

//返回值:无
void event_destroy(event_handle hevent);

#endif

////////////
<PRE class=cpp name="code">#include "event.h"
#ifdef __linux
#include <sys/time.h>
#include <errno.h>
#endif
#include <iostream>
event_handle event_create(bool manual_reset, bool init_state)
{   
#ifdef _MSC_VER
    HANDLE hevent = CreateEvent(NULL, manual_reset, init_state, NULL);
#else
    event_handle hevent = new(std::nothrow) event_t;
    if (hevent == NULL)
    {
        return NULL;
    }
    hevent->state = init_state;
    hevent->manual_reset = manual_reset;
    if (pthread_mutex_init(&hevent->mutex, NULL))
    {
        delete hevent;
        return NULL;
    }
    if (pthread_cond_init(&hevent->cond, NULL))
    {
        pthread_mutex_destroy(&hevent->mutex);
        delete hevent;
        return NULL;
    }
#endif
    return hevent;
}
int event_wait(event_handle hevent)
{
#ifdef _MSC_VER
    DWORD ret = WaitForSingleObject(hevent, INFINITE);
    if (ret == WAIT_OBJECT_0)
    {
        return 0;
    }
    return -1;
#else
    if (pthread_mutex_lock(&hevent->mutex))   
    {      
        return -1;   
    }   
    while (!hevent->state)    
    {      
        if (pthread_cond_wait(&hevent->cond, &hevent->mutex))   
        {   
            pthread_mutex_unlock(&hevent->mutex); 
            return -1;   
        }   
    }   
    if (!hevent->manual_reset) 
    {
        hevent->state = false;
    }
    if (pthread_mutex_unlock(&hevent->mutex))   
    {     
        return -1;   
    }  
    return 0;
#endif
}
int event_timedwait(event_handle hevent, long milliseconds)
{
#ifdef _MSC_VER
    DWORD ret = WaitForSingleObject(hevent, milliseconds);
    if (ret == WAIT_OBJECT_0)
    {
        return 0;
    }
    if (ret == WAIT_TIMEOUT)
    {
        return 1;
    }
    return -1;
#else

    int rc = 0;   
    struct timespec abstime;   
    struct timeval tv;   
    gettimeofday(&tv, NULL);   
    abstime.tv_sec  = tv.tv_sec + milliseconds / 1000;   
    abstime.tv_nsec = tv.tv_usec*1000 + (milliseconds % 1000)*1000000;   
    if (abstime.tv_nsec >= 1000000000)   
    {   
        abstime.tv_nsec -= 1000000000;   
        abstime.tv_sec++;   
    }   

    if (pthread_mutex_lock(&hevent->mutex) != 0)   
    {     
        return -1;   
    }   
    while (!hevent->state)    
    {      
        if (rc = pthread_cond_timedwait(&hevent->cond, &hevent->mutex, &abstime))   
        {   
            if (rc == ETIMEDOUT) break;   
            pthread_mutex_unlock(&hevent->mutex);    
            return -1;   
        }   
    }   
    if (rc == 0 && !hevent->manual_reset)   
    {
        hevent->state = false;
    }
    if (pthread_mutex_unlock(&hevent->mutex) != 0)   
    {      
        return -1;   
    }
    if (rc == ETIMEDOUT)
    {
        //timeout return 1
        return 1;
    }
    //wait event success return 0
    return 0;
#endif
}
int event_set(event_handle hevent)
{
#ifdef _MSC_VER
    return !SetEvent(hevent);
#else
    if (pthread_mutex_lock(&hevent->mutex) != 0)
    {
        return -1;
    }

    hevent->state = true;

    if (hevent->manual_reset)
    {
        if(pthread_cond_broadcast(&hevent->cond))
        {
            return -1;
        }
    }
    else
    {
        if(pthread_cond_signal(&hevent->cond))
        {
            return -1;
        }
    }

    if (pthread_mutex_unlock(&hevent->mutex) != 0)
    {
        return -1;
    }

    return 0;
#endif
}
int event_reset(event_handle hevent) 
{
#ifdef _MSC_VER
    //ResetEvent 返回非零表示成功
    if (ResetEvent(hevent))
    {
        return 0;
    } 
    return -1;
#else
    if (pthread_mutex_lock(&hevent->mutex) != 0)
    {
        return -1;
    }

    hevent->state = false;

    if (pthread_mutex_unlock(&hevent->mutex) != 0)
    {      
        return -1;
    }
    return 0;
#endif
}
void event_destroy(event_handle hevent)
{
#ifdef _MSC_VER
    CloseHandle(hevent);
#else
    pthread_cond_destroy(&hevent->cond);
    pthread_mutex_destroy(&hevent->mutex);
    delete hevent;
#endif
}
2014-07-29 15:27:18 tmWin32 阅读数 500
#ifndef _HIK_EVENT_H_ 
#define _HIK_EVENT_H_

#ifdef _MSC_VER
#include <Windows.h>
#define hik_event_handle HANDLE
#else
#include <pthread.h>
typedef struct  
{
    bool state;
    bool manual_reset;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
}event_t;
#define event_handle event_t*
#endif

//返回值:NULL 出错
event_handle event_create(bool manual_reset, bool init_state);

//返回值:0 等到事件,-1出错
int event_wait(event_handle hevent);

//返回值:0 等到事件,1 超时,-1出错
int event_timedwait(event_handle hevent, long milliseconds);

//返回值:0 成功,-1出错
int event_set(event_handle hevent);

//返回值:0 成功,-1出错
int event_reset(event_handle hevent);

//返回值:无
void event_destroy(event_handle hevent);

#endif

////////////
<PRE class=cpp name="code">#include "event.h"
#ifdef __linux
#include <sys/time.h>
#include <errno.h>
#endif
#include <iostream>
event_handle event_create(bool manual_reset, bool init_state)
{   
#ifdef _MSC_VER
    HANDLE hevent = CreateEvent(NULL, manual_reset, init_state, NULL);
#else
    event_handle hevent = new(std::nothrow) event_t;
    if (hevent == NULL)
    {
        return NULL;
    }
    hevent->state = init_state;
    hevent->manual_reset = manual_reset;
    if (pthread_mutex_init(&hevent->mutex, NULL))
    {
        delete hevent;
        return NULL;
    }
    if (pthread_cond_init(&hevent->cond, NULL))
    {
        pthread_mutex_destroy(&hevent->mutex);
        delete hevent;
        return NULL;
    }
#endif
    return hevent;
}
int event_wait(event_handle hevent)
{
#ifdef _MSC_VER
    DWORD ret = WaitForSingleObject(hevent, INFINITE);
    if (ret == WAIT_OBJECT_0)
    {
        return 0;
    }
    return -1;
#else
    if (pthread_mutex_lock(&hevent->mutex))   
    {      
        return -1;   
    }   
    while (!hevent->state)    
    {      
        if (pthread_cond_wait(&hevent->cond, &hevent->mutex))   
        {   
            pthread_mutex_unlock(&hevent->mutex); 
            return -1;   
        }   
    }   
    if (!hevent->manual_reset) 
    {
        hevent->state = false;
    }
    if (pthread_mutex_unlock(&hevent->mutex))   
    {     
        return -1;   
    }  
    return 0;
#endif
}
int event_timedwait(event_handle hevent, long milliseconds)
{
#ifdef _MSC_VER
    DWORD ret = WaitForSingleObject(hevent, milliseconds);
    if (ret == WAIT_OBJECT_0)
    {
        return 0;
    }
    if (ret == WAIT_TIMEOUT)
    {
        return 1;
    }
    return -1;
#else

    int rc = 0;   
    struct timespec abstime;   
    struct timeval tv;   
    gettimeofday(&tv, NULL);   
    abstime.tv_sec  = tv.tv_sec + milliseconds / 1000;   
    abstime.tv_nsec = tv.tv_usec*1000 + (milliseconds % 1000)*1000000;   
    if (abstime.tv_nsec >= 1000000000)   
    {   
        abstime.tv_nsec -= 1000000000;   
        abstime.tv_sec++;   
    }   

    if (pthread_mutex_lock(&hevent->mutex) != 0)   
    {     
        return -1;   
    }   
    while (!hevent->state)    
    {      
        if (rc = pthread_cond_timedwait(&hevent->cond, &hevent->mutex, &abstime))   
        {   
            if (rc == ETIMEDOUT) break;   
            pthread_mutex_unlock(&hevent->mutex);    
            return -1;   
        }   
    }   
    if (rc == 0 && !hevent->manual_reset)   
    {
        hevent->state = false;
    }
    if (pthread_mutex_unlock(&hevent->mutex) != 0)   
    {      
        return -1;   
    }
    if (rc == ETIMEDOUT)
    {
        //timeout return 1
        return 1;
    }
    //wait event success return 0
    return 0;
#endif
}
int event_set(event_handle hevent)
{
#ifdef _MSC_VER
    return !SetEvent(hevent);
#else
    if (pthread_mutex_lock(&hevent->mutex) != 0)
    {
        return -1;
    }

    hevent->state = true;

    if (hevent->manual_reset)
    {
        if(pthread_cond_broadcast(&hevent->cond))
        {
            return -1;
        }
    }
    else
    {
        if(pthread_cond_signal(&hevent->cond))
        {
            return -1;
        }
    }

    if (pthread_mutex_unlock(&hevent->mutex) != 0)
    {
        return -1;
    }

    return 0;
#endif
}
int event_reset(event_handle hevent) 
{
#ifdef _MSC_VER
    //ResetEvent 返回非零表示成功
    if (ResetEvent(hevent))
    {
        return 0;
    } 
    return -1;
#else
    if (pthread_mutex_lock(&hevent->mutex) != 0)
    {
        return -1;
    }

    hevent->state = false;

    if (pthread_mutex_unlock(&hevent->mutex) != 0)
    {      
        return -1;
    }
    return 0;
#endif
}
void event_destroy(event_handle hevent)
{
#ifdef _MSC_VER
    CloseHandle(hevent);
#else
    pthread_cond_destroy(&hevent->cond);
    pthread_mutex_destroy(&hevent->mutex);
    delete hevent;
#endif
}
转:http://www.cnblogs.com/lihaozy/archive/2012/08/06/2625233.html
2012-06-29 12:50:21 zhangpeng_linux 阅读数 5997
#ifndef _HIK_EVENT_H_ 
#define _HIK_EVENT_H_

#ifdef _MSC_VER
#include <Windows.h>
#define hik_event_handle HANDLE
#else
#include <pthread.h>
typedef struct  
{
    bool state;
    bool manual_reset;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
}event_t;
#define event_handle event_t*
#endif

//返回值:NULL 出错
event_handle event_create(bool manual_reset, bool init_state);

//返回值:0 等到事件,-1出错
int event_wait(event_handle hevent);

//返回值:0 等到事件,1 超时,-1出错
int event_timedwait(event_handle hevent, long milliseconds);

//返回值:0 成功,-1出错
int event_set(event_handle hevent);

//返回值:0 成功,-1出错
int event_reset(event_handle hevent);

//返回值:无
void event_destroy(event_handle hevent);

#endif

////////////
#include "event.h"
#ifdef __linux
#include <sys/time.h>
#include <errno.h>
#endif
#include <iostream>
event_handle event_create(bool manual_reset, bool init_state)
{   
#ifdef _MSC_VER
    HANDLE hevent = CreateEvent(NULL, manual_reset, init_state, NULL);
#else
    event_handle hevent = new(std::nothrow) event_t;
    if (hevent == NULL)
    {
        return NULL;
    }
    hevent->state = init_state;
    hevent->manual_reset = manual_reset;
    if (pthread_mutex_init(&hevent->mutex, NULL))
    {
        delete hevent;
        return NULL;
    }
    if (pthread_cond_init(&hevent->cond, NULL))
    {
        pthread_mutex_destroy(&hevent->mutex);
        delete hevent;
        return NULL;
    }
#endif
    return hevent;
}
int event_wait(event_handle hevent)
{
#ifdef _MSC_VER
    DWORD ret = WaitForSingleObject(hevent, INFINITE);
    if (ret == WAIT_OBJECT_0)
    {
        return 0;
    }
    return -1;
#else
    if (pthread_mutex_lock(&hevent->mutex))   
    {      
        return -1;   
    }   
    while (!hevent->state)    
    {      
        if (pthread_cond_wait(&hevent->cond, &hevent->mutex))   
        {   
            pthread_mutex_unlock(&hevent->mutex); 
            return -1;   
        }   
    }   
    if (!hevent->manual_reset) 
    {
        hevent->state = false;
    }
    if (pthread_mutex_unlock(&hevent->mutex))   
    {     
        return -1;   
    }  
    return 0;
#endif
}
int event_timedwait(event_handle hevent, long milliseconds)
{
#ifdef _MSC_VER
    DWORD ret = WaitForSingleObject(hevent, milliseconds);
    if (ret == WAIT_OBJECT_0)
    {
        return 0;
    }
    if (ret == WAIT_TIMEOUT)
    {
        return 1;
    }
    return -1;
#else

    int rc = 0;   
    struct timespec abstime;   
    struct timeval tv;   
    gettimeofday(&tv, NULL);   
    abstime.tv_sec  = tv.tv_sec + milliseconds / 1000;   
    abstime.tv_nsec = tv.tv_usec*1000 + (milliseconds % 1000)*1000000;   
    if (abstime.tv_nsec >= 1000000000)   
    {   
        abstime.tv_nsec -= 1000000000;   
        abstime.tv_sec++;   
    }   

    if (pthread_mutex_lock(&hevent->mutex) != 0)   
    {     
        return -1;   
    }   
    while (!hevent->state)    
    {      
        if (rc = pthread_cond_timedwait(&hevent->cond, &hevent->mutex, &abstime))   
        {   
            if (rc == ETIMEDOUT) break;   
            pthread_mutex_unlock(&hevent->mutex);    
            return -1;   
        }   
    }   
    if (rc == 0 && !hevent->manual_reset)   
    {
        hevent->state = false;
    }
    if (pthread_mutex_unlock(&hevent->mutex) != 0)   
    {      
        return -1;   
    }
    if (rc == ETIMEDOUT)
    {
        //timeout return 1
        return 1;
    }
    //wait event success return 0
    return 0;
#endif
}
int event_set(event_handle hevent)
{
#ifdef _MSC_VER
    return !SetEvent(hevent);
#else
    if (pthread_mutex_lock(&hevent->mutex) != 0)
    {
        return -1;
    }

    hevent->state = true;

    if (hevent->manual_reset)
    {
        if(pthread_cond_broadcast(&hevent->cond))
        {
            return -1;
        }
    }
    else
    {
        if(pthread_cond_signal(&hevent->cond))
        {
            return -1;
        }
    }

    if (pthread_mutex_unlock(&hevent->mutex) != 0)
    {
        return -1;
    }

    return 0;
#endif
}
int event_reset(event_handle hevent) 
{
#ifdef _MSC_VER
    //ResetEvent 返回非零表示成功
    if (ResetEvent(hevent))
    {
        return 0;
    } 
    return -1;
#else
    if (pthread_mutex_lock(&hevent->mutex) != 0)
    {
        return -1;
    }

    hevent->state = false;

    if (pthread_mutex_unlock(&hevent->mutex) != 0)
    {      
        return -1;
    }
    return 0;
#endif
}
void event_destroy(event_handle hevent)
{
#ifdef _MSC_VER
    CloseHandle(hevent);
#else
    pthread_cond_destroy(&hevent->cond);
    pthread_mutex_destroy(&hevent->mutex);
    delete hevent;
#endif
}


                            
没有更多推荐了,返回首页