2008-08-11 14:30:51 +00:00
|
|
|
|
|
|
|
import re, gtk
|
2008-08-15 03:11:24 +00:00
|
|
|
import terminatorlib.config
|
2008-10-28 22:24:42 +00:00
|
|
|
from terminatorlib.config import err
|
2008-08-11 14:30:51 +00:00
|
|
|
|
|
|
|
class KeymapError(Exception):
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
self.action = 'unknown'
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "Keybinding '%s' invalid: %s" % (self.action, self.value)
|
|
|
|
|
2008-08-15 03:11:24 +00:00
|
|
|
Modifier = re.compile('<([^<]+)>')
|
2008-08-11 14:30:51 +00:00
|
|
|
class TerminatorKeybindings:
|
|
|
|
|
|
|
|
modifiers = {
|
|
|
|
'ctrl': gtk.gdk.CONTROL_MASK,
|
|
|
|
'shift': gtk.gdk.SHIFT_MASK,
|
|
|
|
'alt': gtk.gdk.MOD1_MASK
|
|
|
|
}
|
|
|
|
|
|
|
|
empty = {}
|
|
|
|
|
|
|
|
def __init__(self):
|
2008-08-15 14:46:38 +00:00
|
|
|
self.keymap = gtk.gdk.keymap_get_default()
|
2008-08-15 03:11:24 +00:00
|
|
|
self.configure({})
|
|
|
|
|
|
|
|
def configure(self, bindings):
|
|
|
|
self.keys = bindings
|
2008-08-11 18:02:14 +00:00
|
|
|
self.reload()
|
2008-08-11 14:30:51 +00:00
|
|
|
|
2008-08-11 18:02:14 +00:00
|
|
|
def reload(self):
|
2008-08-11 14:30:51 +00:00
|
|
|
self._lookup = {}
|
|
|
|
self._masks = 0
|
2008-08-23 22:57:51 +00:00
|
|
|
for action, bindings in self.keys.items():
|
|
|
|
if not isinstance(bindings, tuple):
|
|
|
|
bindings = (bindings,)
|
|
|
|
|
|
|
|
for binding in bindings:
|
|
|
|
try:
|
|
|
|
keyval, mask = self._parsebinding(binding)
|
|
|
|
# Does much the same, but with poorer error handling.
|
|
|
|
#keyval, mask = gtk.accelerator_parse(binding)
|
|
|
|
except KeymapError, e:
|
|
|
|
e.action = action
|
|
|
|
raise e
|
2008-08-15 15:20:08 +00:00
|
|
|
else:
|
2008-08-23 22:57:51 +00:00
|
|
|
if mask & gtk.gdk.SHIFT_MASK:
|
|
|
|
if keyval == gtk.keysyms.Tab:
|
|
|
|
keyval = gtk.keysyms.ISO_Left_Tab
|
|
|
|
mask &= ~gtk.gdk.SHIFT_MASK
|
|
|
|
else:
|
|
|
|
keyvals = gtk.gdk.keyval_convert_case(keyval)
|
|
|
|
if keyvals[0] != keyvals[1]:
|
|
|
|
keyval = keyvals[1]
|
|
|
|
mask &= ~gtk.gdk.SHIFT_MASK
|
|
|
|
else:
|
|
|
|
keyval = gtk.gdk.keyval_to_lower(keyval)
|
|
|
|
self._lookup.setdefault(mask, {})
|
|
|
|
self._lookup[mask][keyval] = action
|
|
|
|
self._masks |= mask
|
2008-08-11 14:30:51 +00:00
|
|
|
|
|
|
|
def _parsebinding(self, binding):
|
|
|
|
mask = 0
|
2008-08-15 03:11:24 +00:00
|
|
|
modifiers = re.findall(Modifier, binding)
|
2008-08-11 14:30:51 +00:00
|
|
|
if modifiers:
|
|
|
|
for modifier in modifiers:
|
|
|
|
mask |= self._lookup_modifier(modifier)
|
2008-08-15 03:11:24 +00:00
|
|
|
key = re.sub(Modifier, '', binding)
|
2008-08-11 14:30:51 +00:00
|
|
|
if key == '':
|
|
|
|
raise KeymapError('No key found')
|
2008-08-15 14:46:38 +00:00
|
|
|
keyval = gtk.gdk.keyval_from_name(key)
|
|
|
|
if keyval == 0:
|
|
|
|
raise KeymapError("Key '%s' is unrecognised" % key)
|
|
|
|
return (keyval, mask)
|
2008-08-11 14:30:51 +00:00
|
|
|
|
|
|
|
def _lookup_modifier(self, modifier):
|
|
|
|
try:
|
|
|
|
return self.modifiers[modifier.lower()]
|
|
|
|
except KeyError:
|
|
|
|
raise KeymapError("Unhandled modifier '<%s>'" % modifier)
|
|
|
|
|
|
|
|
def lookup(self, event):
|
2008-10-13 08:37:26 +00:00
|
|
|
try:
|
|
|
|
keyval, egroup, level, consumed = self.keymap.translate_keyboard_state(event.hardware_keycode, event.state, event.group)
|
|
|
|
except TypeError:
|
|
|
|
err ("keybindings.lookup failed to translate keyboard event: %s"%dir(event))
|
|
|
|
return None
|
2008-08-15 14:46:38 +00:00
|
|
|
mask = (event.state & ~consumed) & self._masks
|
2008-08-11 18:02:14 +00:00
|
|
|
return self._lookup.get(mask, self.empty).get(event.keyval, None)
|
2008-08-11 14:30:51 +00:00
|
|
|
|