19

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 ;)

8
  • 3
    I disagree, for example the notification is very specific to Ubuntu, so I want to know how the notification got send. My guess is that somewhere a script must be called, so I want to know where those scripts are. Jan 16, 2013 at 10:51
  • 2
    This is a valid Ubuntu-specific question as the way Ubuntu handles this has changed of the years IIRC. It's very interesting to know what the sequence of events is in Ubuntu on the event pressing a hotkey triggering appropriate actions. Possible purposes: custom brightness controls, debugging hot key issues, etc.
    – gertvdijk
    Jan 16, 2013 at 11:05
  • 2
    @Seth Seems outdated. HAL is deprecated a long time ago, since 10.04. It also lacks information how the desktop notifications are triggered.
    – gertvdijk
    Jan 30, 2013 at 21:33
  • 1
    Today I came across wiki.ubuntu.com/Hotkeys/Troubleshooting, which gives some nice pointers (there is a link to wiki.ubuntu.com/Hotkeys/Architecture which is very informative). I think Stephen Ostermiller's answer is in the right direction, but I want to find those scripts and be able to change them;) Feb 5, 2013 at 20:55
  • 1
    ^^ this information seems to be outdated, no keybindings under gnome-settings-daemon here... A lot of hits for brightness under the compiz section though (I'm guessing for unity?) Who knows how notify-osd works? I think that's where the brightness notifications are send... Feb 5, 2013 at 21:31

3 Answers 3

6
+200

Ok, found this at https://help.ubuntu.com/community/MultimediaKeys

When you hit a key on your keyboard, the linux kernel generates a raw scancode for it (if it is assigned). Each scancode can be mapped to a keycode. This is at kernel level. X has a (quasi) total independent way of mapping keys: X reads the kernel keycode table at startup, then maps the keycode to its independent keycode table (it is the same as the kernel keycodes but different :)). Then each keycode can be mapped to a keysym, i.e. a string which represent a key or suggest an action. Thus to have our keys fully functional, they need a kernel scancode/keycode plus a X keycode/keysym. It may seem weird, but X developers have their reason to keep a separate keyboard mapping from the kernel. It is not difficult at all, just a quite tedious procedure.

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:

#define XF86XK_AudioLowerVolume 0x1008FF11   /* Volume control down        */
#define XF86XK_AudioMute    0x1008FF12   /* Mute sound from the system */
#define XF86XK_AudioRaiseVolume 0x1008FF13   /* Volume control up          */

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: enter image description here

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)

9
  • Looks like this is getting somewhere! Marked as CW from the start so I guess you're still improving? :)
    – gertvdijk
    Feb 5, 2013 at 22:55
  • yep, I need to look into some more, the diagram has a lot of question marks in it.
    – Mateo
    Feb 5, 2013 at 23:34
  • Did you by the way accidentally make it into a community wiki answer? Maybe ask a mod to undo it... Oh and those graphs are most likely a bit out of date, because HAL is deprecated since a long time... Well at least we're getting somewhere :D Feb 6, 2013 at 7:47
  • @GerhardBurger He'll still get the bounty. CW or not CW.
    – gertvdijk
    Feb 6, 2013 at 11:39
  • @gertvdijk I know, but it would be a shame for all the upvotes he's going to miss ;) Feb 6, 2013 at 11:44
1

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.

4
  • then how are the desktop notifications triggered? Jan 31, 2013 at 9:22
  • I believe that the gnome setting daemon is responsible for triggering those in the case of volume and brightness for example. Jan 31, 2013 at 12:00
  • 2
    I ran your script, but all it seems to do is getting the shortcuts that you can access with System Settings > Keyboard > Shortcuts, I did not find any reference to brightness for example. Feb 6, 2013 at 7:51
  • cool script for getting shoutcut keys.
    – Mateo
    Feb 6, 2013 at 19:37
0

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 driver drives, manages, controls, directs and monitors the entity under its command. What a bus driver does with a bus, a device driver does with a computer device (any piece of hardware connected to a computer) like a mouse, keyboard, monitor, hard disk, Web-camera, clock, and more.

Further, a “pilot” could be a person or even an automatic system monitored by a person (an auto-pilot system in airliners, for example). Similarly, a specific piece of hardware could be controlled by a piece of software (a device driver), or could be controlled by another hardware device, which in turn could be managed by a software device driver. In the latter case, such a controlling device is commonly called a device controller. This, being a device itself, often also needs a driver, which is commonly referred to as a bus driver.

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:

2
  • 3
    Uhm... I don't think drivers actually have any notion of the meaning of the key I press on my keyboard. Some special hotkey devices in notebooks may need a driver for this - but this doesn't cover regular multimedia hotkeys like "volume up". This must be covered in a more common area of the kernel/X/DE. I also think the references are very broad about Linux kerner device driver programming.
    – gertvdijk
    Feb 5, 2013 at 21:27
  • I think for most modern keyboards Ubuntu uses the evdev driver, but I don't see how that would be helpful here... Feb 5, 2013 at 21:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .