2008-08-11 14:30:51 +00:00
|
|
|
|
|
|
|
import re, gtk
|
2008-08-15 03:11:24 +00:00
|
|
|
import terminatorlib.config
|
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 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
|
|
|
|
for action, binding in self.keys.items():
|
|
|
|
try:
|
|
|
|
mask, key = self._parsebinding(binding)
|
|
|
|
keyval = gtk.gdk.keyval_from_name(key)
|
|
|
|
if keyval == 0:
|
|
|
|
raise KeymapError("Key '%s' is unrecognised" % key)
|
|
|
|
except KeymapError, e:
|
|
|
|
e.action = action
|
|
|
|
raise e
|
|
|
|
else:
|
|
|
|
self._lookup.setdefault(mask, {})
|
2008-08-11 18:02:14 +00:00
|
|
|
self._lookup[mask][keyval] = action
|
|
|
|
if key == 'Tab':
|
2008-08-13 01:06:49 +00:00
|
|
|
self._lookup[mask][gtk.keysyms.ISO_Left_Tab] = action
|
2008-08-11 14:30:51 +00:00
|
|
|
self._masks |= mask
|
|
|
|
|
|
|
|
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 03:11:24 +00:00
|
|
|
return (mask, re.sub(Modifier, '', binding))
|
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):
|
|
|
|
mask = event.state & 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
|
|
|
|