You are not logged in.

#1 2015-10-04 11:51:16

knezi
Member
Registered: 2014-04-13
Posts: 45

uinput does not work

Hi,
I am trying to remap some keys on my keyboard. Thinking of using pystromo, but doesn't work (see https://answers.launchpad.net/pystromo/+question/269532).
I am trying to remap it on my own. I have made a script in cpp:

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
#include <linux/uinput.h>

#define die(str, args...) do { \
        perror(str); \
        exit(EXIT_FAILURE); \
    } while(0)

using namespace std;
int
main(void)
{
    int                    fd;
    struct uinput_user_dev uidev;
    struct input_event     ev;
    int                    dx, dy;
    int                    i;

    fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
    if(fd < 0)
        die("error: open");

    if(ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0)
        die("error: ioctl");
    if(ioctl(fd, UI_SET_KEYBIT, KEY_D) < 0)
        die("error: ioctl");

    memset(&uidev, 0, sizeof(uidev));
    snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-sample");
    uidev.id.bustype = BUS_USB;
    uidev.id.vendor  = 0x1;
    uidev.id.product = 0x1;
    uidev.id.version = 1;

    if(write(fd, &uidev, sizeof(uidev)) < 0)
        die("error: write");

    if(ioctl(fd, UI_DEV_CREATE) < 0)
        die("error: ioctl");

    sleep(2);

	cout<<"a"<<flush;
    while(1) {
			memset(&ev, 0, sizeof(ev));
			ev.type=EV_KEY;
			ev.code=KEY_D;
			ev.value=1;
			write(fd, &ev, sizeof(ev));
	//cout<<"a"<<flush;
        sleep(1);
    }

    sleep(2);

    if(ioctl(fd, UI_DEV_DESTROY) < 0)
        die("error: ioctl");

    close(fd);

    return 0;
}

If I run it, new device in xinput appears as slave keyboard, but in terminal emulator nothing appears.
So, is it uinput still supported in Linux? Should I do it in this way? If so, is there a stupid mistake which I am not getting?
Thanks.

Offline

Board footer

Powered by FluxBB