val val - 2 months ago 6
C Question

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

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
.

Answer

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).

Comments