I was curious about how hardware interacted with the OS and came across this post: How do keyboard input and text output work?
It seems like a lot of the magic happens in the /dev/input directory. I decided to take a look on my own OS (Ubuntu 16.10) to see what I could find out. All of these files are listed as 0 bytes, and when I do sudo cat mouse0 | hexdump -C
I get a ton of hexdata that looks like this:
00000000 b3 82 8a 58 00 00 00 00 53 74 09 00 00 00 00 00 |...X....St......|
00000010 01 00 1c 00 00 00 00 00 b3 82 8a 58 00 00 00 00 |...........X....|
00000020 53 74 09 00 00 00 00 00 00 00 00 00 00 00 00 00 |St..............|
00000030 b6 82 8a 58 00 00 00 00 06 56 0e 00 00 00 00 00 |...X.....V......|
00000040 01 00 10 00 01 00 00 00 b6 82 8a 58 00 00 00 00 |...........X....|
00000050 06 56 0e 00 00 00 00 00 00 00 00 00 00 00 00 00 |.V..............|
So I have a few questions:
-
What is the purpose of this file? It seems to me that these device files are only used as middle-men to transfer the scancode from the kernel to the X server. Why not just send it directly from the kernel to the X server?
-
Why there are so many? I have a little over 20 individual event files, but only one keyboard and mouse.
Best Answer
I'll go with the question in reverse order:
Those are devices that stand for most inputs present on a machine (there are others, a microphone for example will not be managed in
/dev/input
). Contrary to the assumption that one keyboard plus one mouse would give 2 devices, even the simplest keyboard and simplest mouse would still give 6 of them.Why 6? Because Xorg will create a test input keyboard and a test input mouse (both virtual) during its startup. Also, it will aggregate the test keyboard with the actual keyboard into a main virtual device. i.e. it will perform muxing of the input. The same will happen to the test and actual mouse.
Plus a typical computer (desktop or laptop) has other buttons apart from the keyboard: power button, sleep button.
The
eventN
devices in there are devices for the things that Xorg creates and for what the computer have. TheN
comes from sequential IDs and is analogous to the IDs inxinput
. For example on my machine I have:And
xinput
gives me analogous IDs:(Look that
eventN
corresponds toid=N
)Without Xorg
Note that all random inputs (including my USB camera!) are seen by Xorg as part of the virtual keyboard. This allows for muxing and demuxing input. For example, I can move my mouse through my USB mouse or through my trackpad and an application does not need to know the difference.
(The fact that the USB camera is part of the virtual keyboard is because it has a button to turn it on and off. And since a it is a button, it becomes part of the keyboard subsystem. The actual video input is handled in
/sys/class/video4linux
.)In other words, for an application there really is only one keyboard and only one mouse. But both Xorg and the kernel needs to know the differences. And this leads to the last part:
Because Xorg needs to know the difference.
And there are situations in which it is useful. You can remap keys in Xorg to each slave input device differently. For example, I have a gaming set with pedals, when used in a racing game it outputs a, b and c for each of its pedals. Yet, when programming I remap these keys to Esc, Ctrl and Alt, without remapping the keys on the keyboard itself.
Also, it isn't necessary that a machine runs Xorg. For example, on a headless server I can get the following output:
Where the input devices correspond to serial ports (notably in this case they do) instead of keyboard or mouse.