Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format of /dev/input/event*

What is the "format" of the character devices located in /dev/input/event*?

In other words, how can I decode the character stream? A Python example would be greatly appreciated.

like image 654
jldupont Avatar asked Feb 20 '11 23:02

jldupont


People also ask

What is Dev input event?

evdev (short for 'event device') is a generic input event interface in the Linux kernel and FreeBSD.

How do you read a dev input event?

You can use blocking and nonblocking reads, also select() on the /dev/input/eventX devices, and you'll always get a whole number of input events on a read. Their layout is: struct input_event { struct timeval time; unsigned short type; unsigned short code; unsigned int value; };

What is evdev in Python?

The evdev interface serves the purpose of passing events generated in the kernel directly to userspace through character devices that are typically located in /dev/input/. This package also comes with bindings to uinput, the userspace input subsystem.

When does the input event fire?

The input event fires when the value of an <input>, <select>, or <textarea> element has been changed. The event also applies to elements with contenteditable enabled, and to any element when designMode is turned on. In the case of contenteditable and designMode, the event target is the editing host.

What is the input event in HTML?

The input event fires when the value of an <input>, <select>, or <textarea> element has been changed. The event also applies to elements with contenteditable enabled, and to any element when designMode is turned on.

What is the difference between change and input events?

Note: The input event is fired every time the value of the element changes. This is unlike the change event, which only fires when the value is committed, such as by pressing the enter key, selecting a value from a list of options, and the like.

What is the input protocol and how does it work?

The input protocol uses a map of types and codes to express input device values to userspace. This document describes the types and codes and how and when they may be used. A single hardware event generates multiple input events. Each input event contains the new value of a single data item.


3 Answers

A simple and raw reader can be just done using:

#!/usr/bin/python
import struct
import time
import sys

infile_path = "/dev/input/event" + (sys.argv[1] if len(sys.argv) > 1 else "0")

"""
FORMAT represents the format used by linux kernel input event struct
See https://github.com/torvalds/linux/blob/v5.5-rc5/include/uapi/linux/input.h#L28
Stands for: long int, long int, unsigned short, unsigned short, unsigned int
"""
FORMAT = 'llHHI'
EVENT_SIZE = struct.calcsize(FORMAT)

#open file in binary mode
in_file = open(infile_path, "rb")

event = in_file.read(EVENT_SIZE)

while event:
    (tv_sec, tv_usec, type, code, value) = struct.unpack(FORMAT, event)

    if type != 0 or code != 0 or value != 0:
        print("Event type %u, code %u, value %u at %d.%d" % \
            (type, code, value, tv_sec, tv_usec))
    else:
        # Events with code, type and value == 0 are "separator" events
        print("===========================================")

    event = in_file.read(EVENT_SIZE)

in_file.close()
like image 181
Treviño Avatar answered Oct 12 '22 16:10

Treviño


The format is described in the Documentation/input/input.txt file in the Linux source. Basically, you read structs of the following form from the file:

struct input_event {
    struct timeval time;
    unsigned short type;
    unsigned short code;
    unsigned int value;
};

type and code are values defined in linux/input.h. For example, type might be EV_REL for relative moment of a mouse, or EV_KEY for a keypress, and code is the keycode, or REL_X or ABS_X for a mouse.

like image 29
nelhage Avatar answered Oct 12 '22 15:10

nelhage


Right here in the Input.py module. You'll also need the event.py module.

like image 11
Keith Avatar answered Oct 12 '22 16:10

Keith