linuxinputubuntu-12.04

writing to /dev/uinput (on ubuntu 12.04)


I'm developing a little program that creates virtual joysticks on linux, with a python front end. It is a fork of Linux-Virtual-Joystick.

I need to write to dev/uinput in order to create the user-defined joystick. The file is opened with O_RDWR (I temporarily added read/write access others for the file whilst debugging). When I do

write(uifd, &uidev, sizeof(uinput_user_dev));

it returns -1 and sets errno to 22(EINVAL). The arguments are correct, and the file was successfully opened.

Did anyone else encounter this problem? I shelved the project for about a month, but I remember that it worked in the last version of Ubuntu.

Update: uinput works on ubuntu 12.10


Solution

  • I think the problem you have is with access rights to uinput. The error message you receive is typical of that and I have seen the identical behaviour before with other devices.

    In order to test that assumption, change the /dev/uinput permissions to allow access to all:

    chmod +0666 /dev/uinput
    

    Then try again your code. If now it works fine, you will need to make that change permanent, since otherwise it will revert back to the original permissions after reboot.

    To do that in a safe fashion, add a rule file such as 01-uinput.rules to /etc/udev/rules.d with the following line:

    KERNEL=="uinput", GROUP="udev_group"
    

    To see how a rule file should look like, check the udev rules file located at:

    /lib/udev/rules.d/50-udev-default.rules
    

    When ready, add a a group named udev_group and add your user name to it (or any user that is supposed to have write access to uinput).

    You may need to reboot to get the new rule working.

    The outcome would be that any user who's member of that group will have full access to uinput, which is exactly what you wanted.


    to add the group you can install "Users and Groups":

    sudo apt-get install gnome-system-tools
    

    and launch it at:

    Application -> System Tools -> Administration -> Users and Groups**
    

    or in terminal:

    gnome-system-tools