Linux kernel event: timeval or timespec

馋奶兔 提交于 2020-02-05 14:01:56

问题


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?


回答1:


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.



回答2:


C++ doesn’t convert structs automatically, although they can define conversions. (C structs from Linux will never do so.)



来源:https://stackoverflow.com/questions/57173044/linux-kernel-event-timeval-or-timespec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!