勇闖新世界︰ 《 Kernel 4.X 》之整裝蓄勢‧設備管理及應用‧二上

就讓我們從 freedesktop.org 的 libevdev 之說明開始︰

libevdev  1.4.3
A wrapper library for evdev devices

libevdev is a library for handling evdev kernel devices. It abstracts the evdev ioctls through type-safe interfaces and provides functions to change the appearance of the device.

Development of libevdev is discussed on input-tools@lists.freedesktop.org Please submit patches, questions or general comments there.

Handling events and SYN_DROPPED

libevdev provides an interface for handling events, including most notably SYN_DROPPED events. SYN_DROPPED events are sent by the kernel when the process does not read events fast enough and the kernel is forced to drop some events. This causes the device to get out of sync with the process’ view of it. libevdev handles this by telling the caller that a SYN_DROPPED has been received and that the state of the device is different to what is to be expected. It then provides the delta between the previous state and the actual state of the device as a set of events. See libevdev_next_event() and SYN_DROPPED handling for more information on how SYN_DROPPED is handled.

Signal safety

libevdev is signal-safe for the majority of its operations, i.e. many of its functions are safe to be called from within a signal handler. Check the API documentation to make sure, unless explicitly stated a call is not signal safe.

Device handling

A libevdev context is valid for a given file descriptor and its duration. Closing the file descriptor will not destroy the libevdev device but libevdev will not be able to read further events.

libevdev does not attempt duplicate detection. Initializing two libevdev devices for the same fd is valid and behaves the same as for two different devices.

libevdev does not handle the file descriptors directly, it merely uses them. The caller is responsible for opening the file descriptors, setting them to O_NONBLOCK and handling permissions.

Where does libevdev sit?

libevdev is essentially a read(2) on steroids for /dev/input/eventX devices. It sits below the process that handles input events, in between the kernel and that process. In the simplest case, e.g. an evtest-like tool the stack would look like this:

 kernel → libevdev → evtest

For X.Org input modules, the stack would look like this:

 kernel → libevdev → xf86-input-evdev → X server → X client

For Weston/Wayland, the stack would look like this:

 kernel → libevdev → Weston → Wayland client

libevdev does not have knowledge of X clients or Wayland clients, it is too low in the stack. ……

 

這個 C 語言的程式庫,提供了核心 evdev 模組與使用者空間程式的界面。然而 python-evdev 不只提供了 evdev 模組的程式界面,

evdev 0.5.0

Bindings to the Linux input handling subsystem

This package provides bindings to the generic input event interface in Linux. 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. Uinput allows userspace programs to create and handle input devices that can inject events directly into the input subsystem.

Documentation:
http://python-evdev.readthedocs.org/en/latest/
Development:
https://github.com/gvalkov/python-evdev
Package:
http://pypi.python.org/pypi/evdev
Changelog:
http://python-evdev.readthedocs.org/en/latest/changelog.html

 

還提供了 linux uinput 輸入子系統界面。

Linux/include/uapi/linux/uinput.h

1 /*
2 * User level driver support for input subsystem
3 *
4 * Heavily based on evdev.c by Vojtech Pavlik
5 *

───

Getting started with uinput: the user level input subsystem

uinput is a linux kernel module that allows to handle the input subsystem from user land. It can be used to create and to handle input devices from an application. It creates a character device in /dev/input directory. The device is a virtual interface, it doesn’t belong to a physical device.

In this document, we will see how to create a such input device and how it can be used. ……

 

這不僅開啟了用派生寫作輸入裝置『驅動程式』之大門,還打開了創造『虛擬裝置』的一扇窗。

我們的旅程也從這個軟體的安裝展開了︰

sudo pip-3.2 install evdev

【python-evdev 參考資料】

完整範例

API 參考