#linux #uinput
#linux #uinput
Вопрос:
Я пытаюсь освоить использование / dev / uinput в Linux и скопировал простой код из kernel.org/doc/html/v4.12/input/uinput.html который ниже:
#include <linux/uinput.h>
void emit(int fd, int type, int code, int val)
{
struct input_event ie;
ie.type = type;
ie.code = code;
ie.value = val;
/* timestamp values below are ignored */
ie.time.tv_sec = 0;
ie.time.tv_usec = 0;
write(fd, amp;ie, sizeof(ie));
}
int main(void)
{
struct uinput_setup usetup;
int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
/*
* The ioctls below will enable the device that is about to be
* created, to pass key events, in this case the space key.
*/
ioctl(fd, UI_SET_EVBIT, EV_KEY);
ioctl(fd, UI_SET_KEYBIT, KEY_SPACE);
memset(amp;usetup, 0, sizeof(usetup));
usetup.id.bustype = BUS_USB;
usetup.id.vendor = 0x1234; /* sample vendor */
usetup.id.product = 0x5678; /* sample product */
strcpy(usetup.name, "Example device");
ioctl(fd, UI_DEV_SETUP, amp;usetup);
ioctl(fd, UI_DEV_CREATE);
/*
* On UI_DEV_CREATE the kernel will create the device node for this
* device. We are inserting a pause here so that userspace has time
* to detect, initialize the new device, and can start listening to
* the event, otherwise it will not notice the event we are about
* to send. This pause is only needed in our example code!
*/
sleep(1);
/* Key press, report the event, send key release, and report again */
emit(fd, EV_KEY, KEY_SPACE, 1);
emit(fd, EV_SYN, SYN_REPORT, 0);
emit(fd, EV_KEY, KEY_SPACE, 0);
emit(fd, EV_SYN, SYN_REPORT, 0);
/*
* Give userspace some time to read the events before we destroy the
* device with UI_DEV_DESTOY.
*/
sleep(1);
//ioctl(fd, UI_DEV_DESTROY);
close(fd);
return 0;
}
Он компилируется и успешно запускается без ошибок. Но я не могу найти ни одного устройства, созданного с помощью этой команды xinput
в терминале. Я также проверил внутри /dev /input / , но никаких изменений не обнаружено после выполнения моей этой программы.
Я пробовал это на Ubuntu, Kali. Чего мне не хватает?
Комментарии:
1. Сначала заставьте его работать с Ubuntu. Удачи.
2. Я использую Ubuntu. Но, скажите мне, почему не создается новое устройство?
Ответ №1:
Может быть, это поможет другим.
int i=0;
while(i<100){
i ;
sleep(1);
emit(fd, EV_KEY, KEY_SPACE, 1);
emit(fd, EV_SYN, SYN_REPORT, 0);
emit(fd, EV_KEY, KEY_SPACE, 0);
emit(fd, EV_SYN, SYN_REPORT, 0);
}
Я обнаружил, что во время выполнения программы в цикле создается устройство. Во время этого вы можете проверить с помощью xinput
команды. После завершения программы устройство уничтожается, даже если я не вызываю ioctl(fd,UI_DEV_DESTROY)
.