Linux 内核事件:timeval 或 timespec

Linux kernel event: timeval or timespec

本文关键字:timespec timeval 内核 事件 Linux      更新时间:2023-10-16

我正在从linux内核读取(触摸(事件。我想记录这些事件的时间,但我不知道这些是作为时间规范还是时间值传递的。谁能指出我正确的方向?

示例代码(从缓冲区读取事件后(

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);
    }

以及其中一个流程功能:

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;
}
}

使用时间瓦尔它可以工作,但这也可以是自动幸运类型转换吗?

编辑:当我写这篇文章时,我想到了某种方法来检查它。 读取 Linux 内核事件的代码 'evtest' 是开源的。在第 1060 行,他们使用 timeval 结构来报告事件时间。我猜这是确定的答案:或者它仍然是一个不可预见的类型转换?

谁能指出我正确的方向?

请参阅 Documentation/input/input.rst。
/dev/input/eventX设备读取会返回一个struct input_event的数据,其第一个成员是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.
<</div> div class="answers">

C++不会自动转换结构,尽管它们可以定义转换。 (来自 Linux 的 C 结构永远不会这样做。