Sunday, February 20, 2022

[SOLVED] Linux kernel event: timeval or timespec

Issue

I am reading (touch)event from the linux kernel. I'd like to log the time of these events, but I am unaware whether these are passed as timespec or timeval. Could anyone point me in the right direction?

Example code (after the events are read from the buffer)

switch(evnt.code) {
    case ABS_X:
    case ABS_Y:
      break;
        case ABS_MT_SLOT: 
       // this one sets the digit (virtual representation of the finger)
           current.setSlot(evnt.value);
          break;
    case ABS_MT_POSITION_X:
      current.setX(evnt.value, evnt.time);
      break;
    case ABS_MT_POSITION_Y:
      current.setY(evnt.value, evnt.time);
      break;
    case ABS_MT_TRACKING_ID:
      current.setActive(evnt.value >= 0, evnt.time);
      break;
    default:
      W_MOD("EV_ABS, unhandled event code " << evnt.code);
    }

and one of the process functions:

inline void setY(int value, struct timeval KernelTime)
{
    if (slot < ndigits) {
    // store both time and value
        digit[slot].y = value;
        digit[slot].TimeOfEvent = KernelTime.tv_sec*1000000 +  KernelTime.tv_usec;;
        digit[slot].changed = true;
}
}

With timeval it works, but could this also be an automatic lucky typecasting?

EDIT: as soon as I wrote this I figured some way to check it. the code 'evtest' which reads linux kernel events is open source. On line 1060 they use a timeval struct to report the event time. I am guessing this is the definite answer: or could it still be a unforeseen typecasting?


Solution

Could anyone point me in the right direction?

See Documentation/input/input.rst.
Reading from a /dev/input/eventX device returns data for a struct input_event, whose first member is struct timeval time;

Event interface
===============
...
    struct input_event {
        struct timeval time;
        unsigned short type;
        unsigned short code;
        unsigned int value;
    };

``time`` is the timestamp, it returns the time at which the event happened.


Answered By - sawdust
Answer Checked By - David Marino (WPSolving Volunteer)