530b1f
#!/usr/bin/env python
530b1f
# encoding: utf-8
530b1f
# vim: tabstop=4 expandtab autoindent shiftwidth=4 fileencoding=utf-8
530b1f
"""
530b1f
fedora-setup-keyboard.py
b6fae1
530b1f
Written by Alexander D. Kanevskiy <kad@blackcatlinux.com>
530b1f
"""
b6fae1
530b1f
import sys, os, dbus
530b1f
import rhpl.keyboard_models
530b1f
from rhpl.simpleconfig import SimpleConfigFile
b6fae1
530b1f
KBDCONFIG = "/etc/sysconfig/keyboard"
b6fae1
530b1f
def main():
530b1f
    # Sanity checks
530b1f
    if 'UDI' not in os.environ:
530b1f
        sys.exit(1)
530b1f
    if not os.access(KBDCONFIG, os.R_OK):
530b1f
        sys.exit(0)
530b1f
    # Read config
530b1f
    kbd_config = SimpleConfigFile()
530b1f
    kbd_config.read(KBDCONFIG)
530b1f
    kbd_models = rhpl.keyboard_models.KeyboardModels().get_models()
530b1f
    (layout, model, variant, options) = tuple(kbd_models.get(kbd_config.info.get('KEYTABLE',''), ['', '', '', '', ''])[1:])
530b1f
    result_dict = { 'layout': layout, 'model': model, 'variant': variant, 'options': options }
530b1f
    
530b1f
    # Now let's dbus fun begin
530b1f
    if 'HALD_DIRECT_ADDR' in os.environ:
530b1f
        bus = dbus.connection.Connection(os.environ['HALD_DIRECT_ADDR'])
530b1f
    else:
530b1f
        bus = dbus.SystemBus()
530b1f
    kbd = dbus.Interface(bus.get_object("org.freedesktop.Hal", os.environ['UDI']), 'org.freedesktop.Hal.Device')
530b1f
    for key in result_dict:
530b1f
        # Respect user settings
530b1f
        value = kbd_config.info.get(key.upper(), result_dict[key])
530b1f
        if not value:
530b1f
            try:
530b1f
                kbd.RemoveProperty("input.xkb.%s" % key)
530b1f
            except dbus.exceptions.DBusException:
530b1f
                pass # key already not exist
530b1f
        else:
530b1f
            kbd.SetPropertyString("input.xkb.%s" % key, value)
b6fae1
b6fae1
530b1f
if __name__ == '__main__':
530b1f
    main()