Esempio n. 1
0
def parse_xmodmap(xmodmap_data):
    if type(xmodmap_data) == str and xmodmap_data.find("\n"):
        xmodmap_data = xmodmap_data.splitlines()
    import pygtk
    pygtk.require("2.0")
    import gtk
    root = gtk.gdk.get_default_root_window()
    instructions = []
    for line in xmodmap_data:
        log("parsing: %s", line)
        if not line:
            continue
        try:
            parts = line.split()
            if parts[0] == "keycode" and len(parts) > 2 and parts[2] == "=":
                keycode = parse_keycode(root, parts[1])
                if len(parts) == 3:
                    #ie: 'keycode 123 ='
                    continue
                if keycode == 0 and len(parts) == 4:
                    #keycode=0 means "any", ie: 'keycode any = Shift_L'
                    instructions.append(("keycode", 0, parts[2:3]))
                    continue
                elif keycode > 0:
                    #ie: keycode   9 = Escape NoSymbol Escape
                    keysyms = parts[3:]
                    instructions.append(("keycode", keycode, keysyms))
                    continue
            elif parts[0] == "clear" and len(parts) >= 2:
                #ie: 'clear Lock'
                modifier = parse_modifier(parts[1])
                if modifier < 0:
                    log.error("unknown modifier: %s, ignoring '%s'", parts[1],
                              line)
                else:
                    instructions.append(("clear", modifier))
                continue
            elif parts[0] == "add" and len(parts) >= 3:
                #ie: 'add Control = Control_L Control_R'
                modifier = parse_modifier(parts[1])
                keysyms = parts[3:]
                if modifier < 0:
                    log.error("unknown modifier: %s, ignoring '%s'", parts[1],
                              line)
                elif len(keysyms) == 0:
                    log.error("no keysyms! ignoring '%s'", line)
                else:
                    instructions.append(("add", modifier, keysyms))
                continue
            log.error(
                "parse_xmodmap instruction not recognized: %s (%s parts: %s)",
                line, len(parts), parts)
        except Exception, e:
            log.error("cannot parse %s: %s", line, e)
Esempio n. 2
0
def parse_xmodmap(xmodmap_data):
    if type(xmodmap_data)==str and xmodmap_data.find("\n"):
        xmodmap_data = xmodmap_data.splitlines()
    import pygtk
    pygtk.require("2.0")
    import gtk
    root = gtk.gdk.get_default_root_window()
    instructions = []
    for line in xmodmap_data:
        log("parsing: %s", line)
        if not line:
            continue
        try:
            parts = line.split()
            if parts[0]=="keycode" and len(parts)>2 and parts[2]=="=":
                keycode = parse_keycode(root, parts[1])
                if len(parts)==3:
                    #ie: 'keycode 123 ='
                    continue
                if keycode==0 and len(parts)==4:
                    #keycode=0 means "any", ie: 'keycode any = Shift_L'
                    instructions.append(("keycode", 0, parts[2:3]))
                    continue
                elif keycode>0:
                    #ie: keycode   9 = Escape NoSymbol Escape
                    keysyms = parts[3:]
                    instructions.append(("keycode", keycode, keysyms))
                    continue
            elif parts[0]=="clear" and len(parts)>=2:
                #ie: 'clear Lock' 
                modifier = parse_modifier(parts[1])
                if modifier<0:
                    log.error("unknown modifier: %s, ignoring '%s'", parts[1], line)
                else:
                    instructions.append(("clear", modifier))
                continue
            elif parts[0]=="add" and len(parts)>=3:
                #ie: 'add Control = Control_L Control_R'
                modifier = parse_modifier(parts[1])
                keysyms = parts[3:]
                if modifier<0:
                    log.error("unknown modifier: %s, ignoring '%s'", parts[1], line)
                elif len(keysyms)==0:
                    log.error("no keysyms! ignoring '%s'", line)
                else:
                    instructions.append(("add", modifier, keysyms))
                continue
            log.error("parse_xmodmap instruction not recognized: %s (%s parts: %s)", line, len(parts), parts)
        except Exception, e:
            log.error("cannot parse %s: %s", line, e)
Esempio n. 3
0
def set_modifiers(modifiers):
    """
        modifiers is a dict: {modifier : [keynames]}
        Note: the same keysym cannot appear in more than one modifier
    """
    instructions = []
    for modifier, keynames in modifiers.items():
        mod = parse_modifier(modifier)
        if mod>=0:
            instructions.append(("add", mod, keynames))
        else:
            log.error("set_modifiers_from_dict: unknown modifier %s", modifier)
    debug("set_modifiers: %s", instructions)
    def apply_or_trim(instructions):
        err = apply_xmodmap(instructions)
        debug("set_modifiers: err=%s", err)
        if len(err):
            log("set_modifiers %s failed, retrying one more at a time", instructions)
            l = len(instructions)
            for i in range(1, l):
                subset = instructions[:i]
                debug("set_modifiers testing with [:%s]=%s", i, subset)
                err = apply_xmodmap(subset)
                debug("err=%s", err)
                if len(err)>0:
                    log.warn("removing problematic modifier mapping: %s", instructions[i-1])
                    instructions = instructions[:i-1]+instructions[i:]
                    return apply_or_trim(instructions)
    apply_or_trim(instructions)
    return  modifiers
Esempio n. 4
0
def set_modifiers(modifiers):
    """
        modifiers is a dict: {modifier : [keynames]}
        Note: the same keysym cannot appear in more than one modifier
    """
    instructions = []
    for modifier, keynames in modifiers.items():
        mod = parse_modifier(modifier)
        if mod >= 0:
            instructions.append(("add", mod, keynames))
        else:
            log.error("set_modifiers_from_dict: unknown modifier %s", modifier)
    debug("set_modifiers: %s", instructions)

    def apply_or_trim(instructions):
        err = apply_xmodmap(instructions)
        debug("set_modifiers: err=%s", err)
        if len(err):
            log("set_modifiers %s failed, retrying one more at a time",
                instructions)
            l = len(instructions)
            for i in range(1, l):
                subset = instructions[:i]
                debug("set_modifiers testing with [:%s]=%s", i, subset)
                err = apply_xmodmap(subset)
                debug("err=%s", err)
                if len(err) > 0:
                    log.warn("removing problematic modifier mapping: %s",
                             instructions[i - 1])
                    instructions = instructions[:i - 1] + instructions[i:]
                    return apply_or_trim(instructions)

    apply_or_trim(instructions)
    return modifiers
Esempio n. 5
0
def set_modifiers_from_dict(modifiers):
    """
        modifiers is a dict: {modifier : [keynames]}
    """
    instructions = []
    for modifier, keynames in modifiers.items():
        mod = parse_modifier(modifier)
        if mod>=0:
            instructions.append(("add", mod, keynames))
        else:
            log.error("set_modifiers_from_dict: unknown modifier %s", modifier)
    log.debug("set_modifiers_from_dict: %s", instructions)
    unset = apply_xmodmap(instructions)
    log.debug("unset=%s", unset)
    return  modifiers
Esempio n. 6
0
def set_modifiers_from_dict(modifiers):
    """
        modifiers is a dict: {modifier : [keynames]}
    """
    instructions = []
    for modifier, keynames in modifiers.items():
        mod = parse_modifier(modifier)
        if mod >= 0:
            instructions.append(("add", mod, keynames))
        else:
            log.error("set_modifiers_from_dict: unknown modifier %s", modifier)
    log.debug("set_modifiers_from_dict: %s", instructions)
    unset = apply_xmodmap(instructions)
    log.debug("unset=%s", unset)
    return modifiers