Sample Header Ad - 728x90

How to emulate touch event by writing to /dev/input/eventX in Linux?

0 votes
1 answer
1945 views
I am working on an embedded Linux (kernel 5.10.24) with a touch screen. Now I want to trigger touch event by writing to /dev/input/eventX corresponding to the touch screen. To figure out the event I want to emulate, I firstly collected the events by touching the screen. Then I hard coded the events in codes and write them one by one to the /dev/input/eventX. Below are the events I got from a touching.
~ # ./touchevent
timeS=1651152027,timeUS=312095,type=3,code=57,value=50
timeS=1651152027,timeUS=312095,type=3,code=53,value=218
timeS=1651152027,timeUS=312095,type=3,code=54,value=1223
timeS=1651152027,timeUS=312095,type=3,code=48,value=54
timeS=1651152027,timeUS=312095,type=3,code=58,value=54
timeS=1651152027,timeUS=312095,type=1,code=330,value=1
timeS=1651152027,timeUS=312095,type=0,code=0,value=0
timeS=1651152027,timeUS=448388,type=3,code=57,value=-1
timeS=1651152027,timeUS=448388,type=1,code=330,value=0
timeS=1651152027,timeUS=448388,type=0,code=0,value=0
I hardcoded the first 7 events (including an EV_SYN) to write to /dev/input/eventX.
#include 
#include 
#include 
#include 
#include 
int main( void )
{
        int                     fd;
        int                     ret;
        struct input_event      event;

        fd = open( "/dev/input/event0", O_RDWR);
        if ( fd < 0 )
        {
                perror( "/dev/input/event0" );
                return(-1);
        }

        event.type = 3;
        event.code = 53;
        event.value = 218;
        ret     = write( fd, &event, sizeof(struct input_event) );
        printf("ret: %d\n", ret);

        event.type = 3;
        event.code = 54;
        event.value = 1223;
        ret     = write( fd, &event, sizeof(struct input_event) );
        printf("ret: %d\n", ret);

        event.type = 3;
        event.code = 48;
        event.value = 54;
        ret     = write( fd, &event, sizeof(struct input_event) );
        printf("ret: %d\n", ret);

        event.type = 3;
        event.code = 58;
        event.value = 54;
        ret     = write( fd, &event, sizeof(struct input_event) );
        printf("ret: %d\n", ret);

        event.type = 1;
        event.code = 330;
        event.value = 1;
        ret     = write( fd, &event, sizeof(struct input_event) );
        printf("ret: %d\n", ret);

        event.type = 0;
        event.code = 0;
        event.value = 0;
        ret     = write( fd, &event, sizeof(struct input_event) );
        printf("ret: %d\n", ret);

        close( fd );
.....
}
When I ran the code on the target, there is NO expected response from the touch screen. I am not sure if it is possible to emulate touch events by writing to /dev/input/eventX, if so, what did I miss on doing it ? I don't have sendevent/getevent tools from Andriod, and I want to do this by myself.
Asked by wangt13 (631 rep)
May 5, 2023, 01:31 AM
Last activity: May 30, 2025, 10:07 AM