Sunday, May 2, 2010

What happens when you plug a keyboard into your computer

While reporting a bug, I learned a lot about the different programs which are involved when a keyboard is plugged in. As simple as such an action can sound, it is amazingly complex as it traverse many layers. So, what happens when you are using gnome in Linux and you insert a new keyboard in your USB port?

  1. The Linux kernel usb system detects a new device
  2. From the device properties it is determined as an HID device, and therefore a new input device is created by the evdev module (which is loaded if it was not before)
  3. Event info about a new device is sent to udev
  4. Udev parses its rules and creates a new file in /dev for this new device
  5. Udev transmits the info of a new device to listeners such as DeviceKit and Hal
  6. Hal parses its rules, one of which is the distro script which set the default layout to the device
  7. Hal transmits the info of a new device to its listeners, such as Xorg
  8. Xorg add the new device to the children of the Xkb core keyboard
  9. Hal transmits the options for this new device to Xorg via Xkb
  10. Xorg, via the Xkb extension, transmits the info that the Xkb config has be modified to listeners such as xklavier
  11. xklavier transmits the info of a change in config to its listeners, such as libgnomekdb
  12. libgnomekdb reloads the configuration of the keyboard and update the status icon in the system tray
  13. Xorg, via the XINPUT extension, transmits the info that a new device is present to the listeners, like xklavier
  14. xklavier transmits the info of a new device to its listeners, such as gnome-settings-daemon (g-s-d)
  15. g-s-d changes this new device's layout to the user preferences via libgnomekdb.
Voilà!

Note that it depends a bit on your system, and in particular in very modern Xorg's (xserver >= 1.8) the Hal part is removed and udev and Xorg communicate together directly. It might also contain some mistakes, don't hesitate to mention it in the comments.

No comments: