-
Notifications
You must be signed in to change notification settings - Fork 15
Share devices over the net
This is a tutorial on how to share a group of input event devies over the net.
Here we will share a keyboard an a mouse, and use a key on the keyboard as hotkey to toggle grabbing of both devices, so that we have a hotkey to switch which machine should recieve input events.
The advantage of using input devices to do so, is that it doesn’t depend on X11, and you can even use the device in the console if you wanted to.
The disadvantage of this method is that whoever wants to share these devices needs read-access to the input device files. However, this shouldn’t be a problem, since we are talking about devices which are physically connected to the machine, so network-security shouldn’t be such a big problem. The general usecase would be two machines standing next to each other, and you only want to have one keyboard and one mouse to use both of them, but do not want to rely on X11. (Tools like x2x only allow you to share the devices once you’re logged into an X session. Our tool allows you to use the device everywhere, including GDM/KDM or the console).
First of all we need to decide what key we want to use to toggle between the two machines. In my case, I’ll use the ‘eject’ key of my Apple Wireless Keyboard.
The Showevents mode can be used to figure out the hotkey event for it. Usually it should be something like: EV_KEY:some value
:1
blub@blubmb ~ % sleep 0.1 && netevent -showevents 4 /dev/input/event9
0) Event time: Tue Feb 16 12:07:03 2010
Type = 4 EV_MSC Code = 4 Value = 786616
1) Event time: Tue Feb 16 12:07:03 2010
Type = 1 EV_KEY Code = 161 Value = 1
(snip)
The sleep command makes sure that the release of the RETURN key when issuing the command is still recognized – otherwise I’d get a bunch of key-repeats.
So the line
Type = 1 EV_KEY Code = 161 Value = 1
tells me that my hotkey will be: EV_KEY:161:1
The netevent
tool only acts as a pipe, it does not do any networking, so we need to help ourselves some way. The easiest and most obvious way is the great tool netcat
. We use it to wait for a device on a specific port. We need to use one TCP port for each device.
So the only thing we need to do on the machine which does not have the devices physicall connected is:
netcat -t -l -p 1955 | netevent -write &
netcat -t -l -p 1956 | netevent -write
We could also use the wrapper script from the netevent repository:
sh host.sh 1955 &
sh host.sh 1956
Now we can connect to ports 1955 and 1956 and send over device events.
Here it gets interesting. We need to specify the hotkey on the keyboard to toggle grabbing AND forward this to the mouse device.
But first we need to figure out which event devices we are after. There are several ways to do so, like looking into the output of hal-device
or walking through the maze in the /sys/devices
directory. Or we use the provided devname
utility.
Here’s some output:
blub@blubmb ~ % devname /dev/input/event1
Lid Switch
blub@blubmb ~ % devname /dev/input/event2
Power Button
blub@blubmb ~ % devname /dev/input/event4
AT Translated Set 2 keyboard
blub@blubmb ~ % devname /dev/input/event9
Apple Wireless Keyboard
So the mouse is event4, and the keyboard is event9.
Now let’s assume the other machine has an IP of 192.168.0.50 and let’s start with the mouse device since it’s the easier one:
netevent -toggler "~/.netdevgrab" -read /dev/input/event4 | netcat -t 192.168.0.50 1955 &
At this point, the mouse will already be usable on the other machine, and not available on the current machine anymore.
So we have to continue with the next command already.
We know our hotkey is EV_KEY:161:1
, and we know we can toggle the mouse by @echo@ing 0 or 1 into ~/.netdevgrab
.
So to get our mouse back, we could do: echo 0 > ~/.netdevgrab
.
All hotkey commands, and “ontoggle” commands know the current grabbing state. Let’s start simple: We want our hotkey to toggle device gabbing, so we use: -hotkey EV_KEY:161:1 @toggle
The special command @toggle toggles our device grabbing. Now, we also want to pass on the new grabbing-state to the mouse device. For this we can use: -ontoggle "echo \$GRAB > ~/.netdevgrab"
Putting it together, we get:
netevent -hotkey EV_KEY:161:1 "@toggle" -ontoggle "echo \$GRAB > ~/.netdevgrab" -read /dev/input/event9 | netcat -t 192.168.0.50 1956
Now we have a hotkey to toggle to which device input events should be passed.