【shell】linux设备 通过捕捉event实现简单UI自动化

    这边收到了很多ai硬件设备,需要做压力测试、稳定性测试。苦于非安卓系统,不能借助input tap等方式进行ui自动化,也没找到合适的工具。上网查了资料加上自己多次测试排了一些坑,基本可以通过捕捉event实现简单UI自动化。

1、关于linux设备

    /dev/input目录下的事件都是在驱动中调用input_register_device(struct input_dev *dev)产生的。

/dev/input # ls -al
crw-rw----  1 root input 13, 64 Jun  1 14:00 event0
crw-rw----  1 root input 13, 65 Jun  1 14:00 event1
crw-rw----  1 root input 13, 66 Jun  1 14:00 event2
crw-rw----  1 root input 13, 67 Jun  1 14:00 event3
crw-rw----  1 root input 13, 68 Jun  1 14:00 event4

每个event将上报指定的事件:
比如:

event0 ---> 触摸屏事件
event1 ---> 电源键事件
event2 ---> 音量键事件
event3 ---> ...

通常触摸屏事件就会上报坐标轴事件,电源键比如按下是 1,松开是 0。而与event对应的相关设备信息位于/proc/bus/input/devices。可以直接cat 查看devces信息。小米手机为例:

jason:/ $ cat /proc/bus/input/devices
I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="qpnp_pon"
P: Phys=qpnp_pon/input0
S: Sysfs=/devices/virtual/input/input0
U: Uniq=
H: Handlers=event0 cpufreq
B: PROP=0
B: EV=3
B: KEY=14000000000000 0

I: Bus=0000 Vendor=0000 Product=0003 Version=2065
N: Name="synaptics_dsx"
P: Phys=synaptics_dsx/touch_input
S: Sysfs=/devices/soc/c178000.i2c/i2c-4/4-0020/input/input1
U: Uniq=
H: Handlers=mdss_fb kgsl mouse0 event1 cpufreq
B: PROP=2
B: EV=b
B: KEY=420 0 0 40008800 4000000000 0
B: ABS=263800000000003

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="hbtp_vm"
P: Phys=
S: Sysfs=/devices/virtual/input/input2
U: Uniq=
H: Handlers=mouse1 event2 cpufreq
B: PROP=2
B: EV=7
B: KEY=30000 0 0 0 0
B: REL=3

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="uinput-goodix"
P: Phys=
S: Sysfs=/devices/virtual/input/input3
U: Uniq=
H: Handlers=event3 cpufreq
B: PROP=0
B: EV=3
B: KEY=3100000 40000800 1c16c000000000 0

I: Bus=0019 Vendor=0001 Product=0001 Version=0100
N: Name="gpio-keys"
P: Phys=gpio-keys/input0
S: Sysfs=/devices/soc/soc:gpio_keys/input/input4
U: Uniq=
H: Handlers=event4 cpufreq
B: PROP=0
B: EV=23
B: KEY=8000000000000 0
B: SW=1

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="sdm660-snd-card Headset Jack"
P: Phys=ALSA
S: Sysfs=/devices/soc/soc:sound/sound/card0/input5
U: Uniq=
H: Handlers=event5
B: PROP=0
B: EV=21
B: SW=780d4

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="sdm660-snd-card Button Jack"
P: Phys=ALSA
S: Sysfs=/devices/soc/soc:sound/sound/card0/input6
U: Uniq=
H: Handlers=event6 cpufreq
B: PROP=40
B: EV=3
B: KEY=3e 400000000 0 0 0
2、捕捉消息

    首先要会捕捉触摸屏以及所需要控制的按键事件,安卓系统比较简单,用自带的getevent就可以了。这边用小米手机为例:

jason:/dev/input $ getevent -l
add device 1: /dev/input/event6
  name:     "sdm660-snd-card Button Jack"
add device 2: /dev/input/event5
  name:     "sdm660-snd-card Headset Jack"
could not get driver version for /dev/input/mouse1, Not a typewriter
add device 3: /dev/input/event2
  name:     "hbtp_vm"
add device 4: /dev/input/event0
  name:     "qpnp_pon"
could not get driver version for /dev/input/mouse0, Not a typewriter
add device 5: /dev/input/event4
  name:     "gpio-keys"
could not get driver version for /dev/input/mice, Not a typewriter
add device 6: /dev/input/event1
  name:     "synaptics_dsx"
add device 7: /dev/input/event3
  name:     "uinput-goodix"
/dev/input/event1: EV_ABS       ABS_MT_TRACKING_ID   00000018
/dev/input/event1: EV_KEY       BTN_TOUCH            DOWN
/dev/input/event1: EV_KEY       BTN_TOOL_FINGER      DOWN
/dev/input/event1: EV_ABS       ABS_MT_POSITION_X    000000f1
/dev/input/event1: EV_ABS       ABS_MT_POSITION_Y    000002d3
/dev/input/event1: EV_ABS       ABS_MT_TOUCH_MAJOR   00000004
/dev/input/event1: EV_ABS       ABS_MT_TOUCH_MINOR   00000004
/dev/input/event1: EV_SYN       SYN_REPORT           00000000
/dev/input/event1: EV_ABS       ABS_MT_TOUCH_MAJOR   00000005
/dev/input/event1: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event1: EV_SYN       SYN_REPORT           00000000
/dev/input/event1: EV_ABS       ABS_MT_TOUCH_MINOR   00000004
/dev/input/event1: EV_SYN       SYN_REPORT           00000000
/dev/input/event1: EV_ABS       ABS_MT_TOUCH_MINOR   00000005
/dev/input/event1: EV_SYN       SYN_REPORT           00000000
/dev/input/event1: EV_ABS       ABS_MT_POSITION_X    000000dd
/dev/input/event1: EV_ABS       ABS_MT_POSITION_Y    000002c9

我这边使用的触摸屏事件,打印的event1信息,可以理解为event1上报触摸屏事件。同样,使用电源键、音量键、home键也会用不同的event信息打印:

/dev/input/event4: EV_KEY       KEY_VOLUMEUP         DOWN
/dev/input/event4: EV_SYN       SYN_REPORT           00000000
/dev/input/event4: EV_KEY       KEY_VOLUMEUP         UP
/dev/input/event4: EV_SYN       SYN_REPORT           00000000
/dev/input/event0: EV_KEY       KEY_VOLUMEDOWN       DOWN
/dev/input/event0: EV_SYN       SYN_REPORT           00000000
/dev/input/event0: EV_KEY       KEY_VOLUMEDOWN       UP
/dev/input/event0: EV_SYN       SYN_REPORT           00000000
/dev/input/event3: EV_KEY       KEY_HOME             DOWN
/dev/input/event3: EV_SYN       SYN_REPORT           00000000
/dev/input/event3: EV_KEY       KEY_HOME             UP
/dev/input/event3: EV_SYN       SYN_REPORT           00000000

这里对应event0、event3、event4

但是在绝大多数linux是没有的!!!(有时候BSP对带,which一下就知道了)
如果没有的话只能编译一个放进去,这里推荐evtest

/*
 * $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);
            }    

    }
}

编译方法:

(base) ➜  evtest vi Evtest.c
(base) ➜  evtest chmod +x Evtest.c
(base) ➜  evtest gcc Evtest.c -o evtest

注:arm设备需要使用特定的交叉编译工具,否则可能无法执行;

3、使用evtest捕获

方法:evtest /dev/input/event

/ # evtest /dev/input/event0
Input driver version is 1.0.1
Input device ID: bus 0x18 vendor 0x0 product 0x0 version 0x0
Input device name: "st16xx"
Supported events:
  Event type 0 (EV_SYN)
  Event type 1 (EV_KEY)
    Event code 142 (KEY_SLEEP)
    Event code 143 (KEY_WAKEUP)
    Event code 330 (BTN_TOUCH)
  Event type 3 (EV_ABS)
    Event code 47 (ABS_MT_SLOT)
      Value      0
      Min        0
      Max        1
    Event code 53 (ABS_MT_POSITION_X)
      Value      0
      Min        0
      Max      320
    Event code 54 (ABS_MT_POSITION_Y)
      Value      0
      Min        0
      Max      320
    Event code 57 (ABS_MT_TRACKING_ID)
      Value      0
      Min        0
      Max    65535
Properties:
  Property type 1 (INPUT_PROP_DIRECT)
Testing ... (interrupt to exit)
Event: time 1591000623.654802, type 3 (EV_ABS), code 57 (ABS_MT_TRACKING_ID), value 39
Event: time 1591000623.654802, type 1 (EV_KEY), code 330 (BTN_TOUCH), value 1
Event: time 1591000623.654802, type 3 (EV_ABS), code 53 (ABS_MT_POSITION_X), value 72
Event: time 1591000623.654802, type 3 (EV_ABS), code 54 (ABS_MT_POSITION_Y), value 6
Event: time 1591000623.654802, -------------- SYN_REPORT ------------
Event: time 1591000623.671811, type 3 (EV_ABS), code 53 (ABS_MT_POSITION_X), value 73
4、TouchScreen实现点触摸屏

通过evtest.c可以查看设备事件消息,通过TouchScreen.c可以实现点触摸屏某个坐标
[参考文档](https://download.csdn.net/download/asd2479745295/10712830
编译文件push到设备。./touch xxx yyy

这边在使用时懒得充值,没找到合适的免费资源,找到了另一种解决方法。

5、使用dd+cat 指令实现

(https://www.jianshu.com/p/6b5a172e064a)
可以参考之前一篇模拟快速点击的方法;

捕捉:
使用dd指令

sdcard # dd if=/dev/input/event0 of=./event0
^C
sdcard #

模拟:
1、反向使用

dd if=/sdcard/event0 of=/dev/input/event0

2、可以使用追加

cat ./event0 >> dev/input/event0

此方法可以模拟点击、滑屏、快速点击,各种实体按键短按、长按、快速点击等。暂时没有找到触摸屏长按的方法,原因是 无法控制长按的时间(追加的速度过快),但是可以通过脚本循环实现。

/mnt/sdcard # cat test.sh
while((true));do
cat ./event0 >> /dev/input/event0
done
/mnt/sdcard #

一个简单的循环,测试可以实现长按,但是不好控制时间,有一个思路是获取获取linux系统时间,在循环体中,true修改成时间条件但是可能打断长按事件。
还有一种可行性高,就是单独做成一个脚本供调用,另一个主脚本控制时间,到时间后kill进程来控制。方法很多,因为工作中没有用到这一项就没测试了。

5、脚本测试case示例
function test_case_04()
{
    i_04=1
    while(( $i_04<=50 ))
    do
    echo "Loop "$i_04
    cat Events/wiki/event1_01 >> /dev/input/event1
    sleep 10
    cat Events/wiki/event1_02 >> /dev/input/event1
    sleep 10
    ((i_04++))
    echo ""
    done
}
6、遇到的坑

1、ubuntu下编译解决。

(base) ➜  evtest gcc Evtest.c -o evtest
Evtest.c:31:10: fatal error: 'linux/input.h' file not found
#include <linux/input.h>
         ^~~~~~~~~~~~~~~
1 error generated.

2、捕捉的evest为空
数据量太少,dd的版本较低。更新busybox或者dd版本

3、while((1))报错
设备没有bash,只有sh,修改为while((true))可以解释通过。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,921评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,635评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,393评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,836评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,833评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,685评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,043评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,694评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,671评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,670评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,779评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,424评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,027评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,984评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,214评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,108评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,517评论 2 343