最終更新:2019-03-01 (金) 04:14:10 (1880d)
uinput
Top / uinput
User level driver support for input subsystem
ドキュメント (Linux 4.12~)
- https://www.kernel.org/doc/html/latest/input/uinput.html
- https://www.kernel.org/doc/Documentation/input/uinput.rst
- Linuxカーネル/ドキュメントのSphinx化に伴って今まで存在してなかったが追加された模様
概要
- CONFIG_INPUT_UINPUTが有効になっているとLinux/dev/uinputというファイルができて、そこにioctlをすることによってOSにイベントを発生させることができる。
.config
バージョン
- UINPUT_VERSION? - uapi/linux/uinput.hで定義
0.5 5 2015/08/13 Linux 4.5~ add UI_DEV_SETUP, UI_ABS_SETUP?, UI_GET_VERSION? ioctl 0.4 4 2014/09/01 Linux 3.15~ update ff support for the changes in kernel interface
add UI_GET_SYSNAME? ioctl0.3 3 2006/05/24 Linux 2.6.19?~ add UINPUT_VERSION? 0.2 2004/10/16 added force feedback support
added UI_SET_PHYS?0.1 2002/06/20 first public version
デバイスファイル
ソースコード
イベントコード
有効化
- modprobe uinput
uinput 0.5用のサンプル
変更点
- デバイス名などの設定のための情報uinput_user_devをwriteする代わりにuinput_setupをioctl(UI_DEV_SETUP)する
使い方 (古いuinput 0.4のコード)
ひな形
#include <linux/input.h> #include <linux/uinput.h> ... int fd; fd = open("/dev/input/uinput", O_WRONLY | O_NONBLOCK); if(fd < 0) { ... exit(EXIT_FAILURE); }
どういうデバイスを作るか書き込み
- 使用するinput_eventのtypeとcodeをあらかじめ書き込む。
ioctlの引数
- UI_SET_EVBIT?
- UI_SET_RELBIT?
- UI_SET_ABSBIT?
- UI_SET_MSCBIT?
- UI_SET_LEDBIT?
- UI_SET_SNDBIT?
- UI_SET_FFBIT?
- UI_SET_PHYS?
- UI_SET_SWBIT?
- UI_SET_PROPBIT?
ioctl(fd, UI_SET_EVBIT, EV_REL); ioctl(fd, UI_SET_RELBIT, REL_X); ioctl(fd, UI_SET_RELBIT, REL_Y); ioctl(fd, UI_SET_EVBIT, EV_KEY); ioctl(fd, UI_SET_KEYBIT, KEY_D);
uinput_user_devの登録
struct uinput_user_dev device; //memset(&uidev, 0, sizeof(uidev)); snprintf(device.name, UINPUT_MAX_NAME_SIZE, "uinput-sample"); device.id.bustype = 0;//BUS_USBとか device.id.vendor = 0; device.id.product = 0; device.id.version = 0; ret = write(ui_fd, &device, sizeof(device)) < sizeof(device));
デバイスの作成
- UI_DEV_CREATE?
ioctl(ui_fd, UI_DEV_CREATE, NULL);
イベント(input_event)の書き込み
struct input_event ev; memset(&ev, 0, sizeof(ev)); ev.type = EV_KEY; ev.code = KEY_D; ev.value = 1; ret = write(fd, &ev, sizeof(ev))