After figuring out how to change the mapping of scan codes to key codes using udev
, see this question, I was wondering how those key codes (or events if you will) are mapped to the appropriate action.
So, for example, if you press volume up on your keyboard, a scan code is sent which will then be converted to the volumeup
key code. But how is this key code intercepted, the volume turned up, and the appropriate notification displayed?
My guess is that somewhere a script must be called, so I'd like to know where those scripts are.
EDIT: The key codes I mention are not to be confused with the key codes xev
returns, but I am also interested in those ;)
Ok, found this at https://help.ubuntu.com/community/MultimediaKeys
So keycodes's are mapped to keysym's, So where are the keysym's? I found and answer from this question: Where do I find a list of all X keysyms these days? Since we are talking about the volume keys it would be found in
XF86keysym.h
in the source-code mentioned in the answer.In that file on my computer I found the following for volume:
Strange... different values from anything else, maybe there are multiple systems for handling keys? http://crunchbang.org/forums/viewtopic.php?id=16656
I am using Xubuntu, and to control the keys, I need to map the actions manually(like this How do I change my keyboard shortcuts in xubuntu?). However the notifications seem independent as if they are picking up on the key-press and acting accordingly, It may mean that other programs in Ubuntu are set up this way, so there is no need to map scripts to the keys.
So I'm quite sure that programs are now picking up on the key (so no scrips to be found).
In Xubuntu I had this problem with Pulse Audio and using custom scripts for changing the volume, It seemed that Pulse was intercepting the Mute key, Mute key mutes Alsa and PulseAudio, but unmutes only Alsa made for interesting workarounds.
Check this out about NotifyOSD https://wiki.ubuntu.com/NotifyOSD#Volume_changes
If you look a These diagrams: https://wiki.ubuntu.com/NotifyOSD#Architecture
Especially this one:
It shows that there is a "hardware keys listener" which receives form DBus or HAL? It then "retrieves visual element from system" which the sound and brightness icons are in the source of Notify-OSD, and then makes the bubble from there.
This is all crazy confusing, but as far as I understand it (so far):
raw scancode (ex. e016) > keycode (ex. 160) > keysym (ex. XF86AudioMute) > gnome-settings-daemon (ex. volume-up) > DBus signal > hardware-keys-listener for notify-osd (or other program listening)
In most cases, there are no scripts that are run. They cause events to be sent to the window manager or settings daemon. The only time that I am aware of scripting in the process is when you configure custom key bindings. For custom keybindings, you can add command lines (executables or scripts) and have them bound to keys.
Please see this answer that I wrote to a question about backing up keybindings in Ubuntu: Where are GNOME keyboard shortcuts stored? I have a script that backs up or restores all the keybindings, including the custom keybindings. If you run the script, you can see where in dconf the keybindings are stored, and which application gets notified about the key event.
The answer relates to Drivers.
Every hardware must have a driver in order to interact with the operating system.
Quoting http://www.linuxforu.com/2010/11/understanding-linux-device-drivers/:
A device has a device register which stores the Control/Status bits and Data bits. Whenever some data needs to be transferred it is usually sent by setting the Data bits.
So, whenever you press some key on your keyboard, some data is written on the register. Those bits are read by the device driver and appropriate action is performed. This is the short explanation.
Links:
If you are interested, I'd recommend that you read this series on Device Drivers consisting of 17 well-written articles by linuxforu: http://www.linuxforu.com/tag/linux-device-drivers-series/.
You can read the specific document containing instructions on how to write a device driver for USB keyboard: http://www.emntech.com/docs/USB_KeyBoard_Driver_eMNTech.pdf
If you're very much excited about linux device drivers, read the book "Linux Device Drivers" which is available as pdf for free: http://lwn.net/Kernel/LDD3/