xi2ros allows you to send XInput events (keyboard, mouse) to a ROS system
It is not a real ROS package, nor a library. It is just a piece of code
that you are invited to modify. For instance, the demo tool sends 'pose'
messages on the '/xi2ros/pose' topic when you move your mouse. Most likely,
this is not what you need for your own project. So, feel free to change
this.
Tested on Debian Bullseye in June 2020
Package dependencies
xcb: libxcb1-dev libxcb-xkb-dev libxcb-keysyms1-dev libxcb-xinput-dev
ros: libroscpp-dev libroscpp-core-dev librosconsole-dev
Just type 'make'
Note: This compiles outside of catkin! Yes it is!
Retrieve the id of your mouse with 'xi2ros -p'
Start 'roscore' and 'rostopic echo /xi2ros/pose' in others consoles
Then start './xi2ros -d <your_mouse_id>'</your_mouse_id>
Move your mouse, use the wheel: this change the position
Do it while pressing the button left, this change the orientation
To exit, press button right three times
First of all, play with a standard X11 command: 'xinput' (read the man)
This shows you a 'virtual core pointer' and a 'virtual core keyboard'.
These 'master' devices are the ones that send events to
applications. But you have to attach some physical devices to
these virtual devices. By default, all input devices plugged on
your computer (keyboards, mice, trackpad, touchpad, joysticks,
etc.) are attached to these two master/virtual devices.
A physical input device may be 'attached' or 'floating', 'enabled'
or 'disabled' (of course, a device can't be attached if it is
disabled).
So, try 'xinput list --long', 'xinput test <id>', 'xinput
test-xi2 <id>', etc.</id></id>
Also note that the concept of 'modifiers' (shift, caps-lock, etc.)
is managed by these 'virtual core devices', not by physical
devices which just send press/release events.
Now, play with 'xi2ros'
Try 'xi2ros -p' and 'xi2ros -P'.
You get almost the same as 'xinput' and 'xinput list --long'
Try 'xi2ros -c show -d <id>'. This dummy 'show callback' shows you
the events sent by your device, like 'xinput test <id>'. To exit,
press button right or the escape key three times (depending on the
type of device you have selected)</id></id>
To sum up, 'xi2ros' do the following:
1) It detachs the device(s) you have selected from the master(s).
So your device(s) has no more impact on others applications on
the screen. You can select several devices in the same time.
2) It grab each event of this device as reported by the X11 server,
and passes it to a 'callback' function that you have to code. In
this demo, there is only the 'show' callback which shows you the
events on your console, and the 'ros' callback which sends 'pose'
messages to ROS as described above.
3) At the end, either by pressing the magic key three times, or at
the end of a timer that you can configure (type 'xi2ros -h' to
see the options), it re-attaches your device to its master and
exits.
4) Note that if something goes wrong, you may get an uncomfortable
situation with a device detached from your session. You still
may reattach it with 'xinput', or just unplug-replug it; or use a
secondary one.
The main class is the one named 'X11'
On initialization, it retrieves the list of connected devices and
stores the information in 'device' and 'device_class' objects
One has to register on it a 'callback' class
Its offers several methods to select / deselect desired devices
Then it runs the main loop, grab the input events and transmits
them to the callback
The class named 'callback' is abstract
Each concrete callback class must inherit from it and implements
the 'Hid ()' method. This method is called by X11 whenever an HID
event occurs.
The class 'callback_show' implements a 'Hid()' method which
displays the event information on the console. You can get an
idea of the possibilities.
The 'callback_ros' class implements a 'Hid()' method which sends
'pose' messages to ROS, as described above.
You must code your own class 'callback_XXXX', like the two
examples.
Good luck. Be happy.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.