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

  • I think this question is more general relating with the design of the computer hardware. Not very specific to ubuntu. Considering this useful question off topic for the site – Anwar Jan 16 '13 at 10:46
  • 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. – Gerhard Burger Jan 16 '13 at 10:51
  • I might be wrong about the question may be. Can youadd this to the question? – Anwar Jan 16 '13 at 10:53
  • 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 '13 at 11:05
  • Does this help at all? – Seth Jan 30 '13 at 21:23
  • 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 '13 at 21:33
  • 1
    Today I came across https://wiki.ubuntu.com/Hotkeys/Troubleshooting, which gives some nice pointers (there is a link to https://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;) – Gerhard Burger Feb 05 '13 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... – Gerhard Burger Feb 05 '13 at 21:31

3 Answers3

6

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)

Mateo
  • 8,104
  • Looks like this is getting somewhere! Marked as CW from the start so I guess you're still improving? :) – gertvdijk Feb 05 '13 at 22:55
  • yep, I need to look into some more, the diagram has a lot of question marks in it. – Mateo Feb 05 '13 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 – Gerhard Burger Feb 06 '13 at 07:47
  • @gertvdijk I know, but it would be a shame for all the upvotes he's going to miss ;) – Gerhard Burger Feb 06 '13 at 11:44
  • @GerhardBurger It may still look to listen to HAL (possibly backwards compatibility), in your other question did it still display notifications using halevt maybe that is the reason the ? is present. – Mateo Feb 06 '13 at 18:18
  • Then again, I found the date of the picture to be 2009-02-18... – Mateo Feb 06 '13 at 18:24
  • this may be of interest, http://pubs.vmware.com/workstation-9/index.jsp?topic=%2Fcom.vmware.ws.using.doc%2FGUID-44F3733A-99C3-4F7E-BF2F-ECE932D4BA54.html I think we still need to figure out what happens after the keysym step. – Mateo Feb 06 '13 at 18:27
  • so it looks like gnome-settings-daemon sends a DBus signal? http://superuser.com/questions/247982/dbus-signal-for-volume-up-down – Mateo Feb 06 '13 at 19:50
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.

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:

green
  • 14,306
  • 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 05 '13 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... – Gerhard Burger Feb 05 '13 at 21:28