Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lib(s)uinput: creating joystick with more than one button

Tags:

c

linux

uinput

I can't find information, how create joystick with several buttons on it using uinput/suinput. Example on python using python-uinput:

import uinput

def main():
    events = (
        uinput.BTN_JOYSTICK,
        uinput.ABS_X + (0, 255, 0, 0),
        uinput.ABS_Y + (0, 255, 0, 0),
        )

    with uinput.Device(events) as device:
        for i in range(20):
            # syn=False to emit an "atomic" (5, 5) event.
            device.emit(uinput.ABS_X, 5, syn=False)
            device.emit(uinput.ABS_Y, 5)
        device.emit_click(uinput.BTN_JOYSTICK)

if __name__ == "__main__":
    main()

As you can see, in this example using BTN_JOYSTICK as button. And how create second button/addictional two ABS_X/ABS_Y?

Note: I'm using python as example, application language is C with libsuinput.

like image 420
val is still with Monica Avatar asked Sep 18 '16 15:09

val is still with Monica


1 Answers

More buttons — more button codes. Originally button codes are defined in the linux/input.h header from userspace perspective, or uapi/linux/input-event-codes.h from the kernel perspective. For python they are duplicated in the ev.py. As you can see, there are a lot of them and you're mostly interested in things between BTN_JOYSTICK and BTN_THUMBR (but BTN_TRIGGER_HAPPY values are also used by some). The most interesting ones are BTN_THUMB, BTN_PINKIE, BTN_[ABC], BTN_SELECT and BTN_START.

As for additional axis, choose any between ABS_X and ABS_MISC. The most interesting additional ones are ABS_R[XYZ] and ABS_HAT0[XYZ].

So to use that additional buttons/axis you just use these identifiers in the same fashion you use BTN_JOYSTICK with ABS_X and ABS_Y.

Now, another interesting question probably is what values from this list are used by real joysticks that you're trying to emulate. This values can be obtained by using evtest or evemu programs (using real joystick, of course). Some widely available joysticks (Xbox and PS3) were also discussed here (as you can see, joysticks are very different in what button codes they produce). And to be really sure (or to emulate some device you can't check with evtest) you can also take a look at real joystick drivers (just BTN_ and ABS_ things they use).

like image 135
Roman Khimov Avatar answered Oct 22 '22 14:10

Roman Khimov