Blob Blame Raw
#!/usr/bin/env python
# encoding: utf-8
# vim: tabstop=4 expandtab autoindent shiftwidth=4 fileencoding=utf-8
"""
fedora-setup-keyboard.py

Written by Alexander D. Kanevskiy <kad@blackcatlinux.com>
"""

import sys, os, dbus
import rhpl.keyboard_models
from rhpl.simpleconfig import SimpleConfigFile

KBDCONFIG = "/etc/sysconfig/keyboard"

def main():
    # Sanity checks
    if 'UDI' not in os.environ:
        sys.exit(1)
    if not os.access(KBDCONFIG, os.R_OK):
        sys.exit(0)
    # Read config
    kbd_config = SimpleConfigFile()
    kbd_config.read(KBDCONFIG)
    kbd_models = rhpl.keyboard_models.KeyboardModels().get_models()
    (layout, model, variant, options) = tuple(kbd_models.get(kbd_config.info.get('KEYTABLE',''), ['', '', '', '', ''])[1:])
    result_dict = { 'layout': layout, 'model': model, 'variant': variant, 'options': options }
    
    # Now let's dbus fun begin
    if 'HALD_DIRECT_ADDR' in os.environ:
        bus = dbus.connection.Connection(os.environ['HALD_DIRECT_ADDR'])
    else:
        bus = dbus.SystemBus()
    kbd = dbus.Interface(bus.get_object("org.freedesktop.Hal", os.environ['UDI']), 'org.freedesktop.Hal.Device')
    for key in result_dict:
        # Respect user settings
        value = kbd_config.info.get(key.upper(), result_dict[key])
        if not value:
            try:
                kbd.RemoveProperty("input.xkb.%s" % key)
            except dbus.exceptions.DBusException:
                pass # key already not exist
        else:
            kbd.SetPropertyString("input.xkb.%s" % key, value)


if __name__ == '__main__':
    main()