diff --git a/terminatorlib/config.py b/terminatorlib/config.py index 7bb23c7f..7129512c 100644 --- a/terminatorlib/config.py +++ b/terminatorlib/config.py @@ -77,7 +77,6 @@ from configobj import ConfigObj, flatten_errors from validate import Validator from .borg import Borg from .util import dbg, err, DEBUG, get_system_config_dir, get_config_dir, dict_diff, update_config_to_cell_height -from terminatorlib.plugin_util import KeyBindUtil from gi.repository import Gio @@ -721,6 +720,7 @@ class ConfigBase(Borg): dbg('Processing section: %s' % section_name) section = getattr(self, section_name) if section_name == 'keybindings': + from terminatorlib.plugin import KeyBindUtil # for plugin KeyBindUtil assist in plugin_util keybindutil = KeyBindUtil(); keyb_keys = keybindutil.get_act_to_keys() diff --git a/terminatorlib/plugin.py b/terminatorlib/plugin.py index 7a0487a4..db18835b 100644 --- a/terminatorlib/plugin.py +++ b/terminatorlib/plugin.py @@ -187,3 +187,124 @@ class MenuItem(Plugin): def callback(self, menuitems, menu, terminal): """Callback to transform the enclosed URL""" raise NotImplementedError + + +""" +-Basic plugin util for key-press handling, has all mapping to be used +in layout keybindings + +Vishweshwar Saran Singh Deo vssdeo@gmail.com +""" + +from gi.repository import Gtk, Gdk +from terminatorlib.keybindings import Keybindings, KeymapError + +PLUGIN_UTIL_DESC = 0 +PLUGIN_UTIL_ACT = 1 +PLUGIN_UTIL_KEYS = 2 + +class KeyBindUtil: + + keybindings = Keybindings() + + map_key_to_act = {} + map_act_to_keys = {} + map_act_to_desc = {} + + def __init__(self, config=None): + self.config = config + + #Example + # bind + # first param is desc, second is action str + # self.keyb.bindkey([PluginUrlFindNext , PluginUrlActFindNext, "j"]) + # + # get action name + # act = self.keyb.keyaction(event) + + # if act == "url_find_next": + + + #check map key_val_mask -> action + def _check_keybind_change(self, key): + act = key[PLUGIN_UTIL_ACT] + for key_val_mask in self.map_key_to_act: + old_act = self.map_key_to_act[key_val_mask] + if act == old_act: + return key_val_mask + return None + + #check in config before binding + def bindkey_check_config(self, key): + if not self.config: + raise Warning("bindkey_check_config called without config init") + + actstr = key[PLUGIN_UTIL_ACT] + kbsect = self.config.base.get_item('keybindings') + keystr = kbsect[actstr] if actstr in kbsect else "" + dbg("bindkey_check_config:action (%s) key str:(%s)" % (actstr, keystr)) + if len(keystr): + key[PLUGIN_UTIL_KEYS] = keystr + dbg("found new Action->KeyVal in config: (%s, %s)" + % (actstr, keystr)); + self.bindkey(key) + + def bindkey(self, key): + (keyval, mask) = self.keybindings._parsebinding(key[PLUGIN_UTIL_KEYS]) + keyval = Gdk.keyval_to_lower(keyval) + mask = Gdk.ModifierType(mask) + + ret = (keyval, mask) + dbg("bindkey: (%s) (%s)" % (key[PLUGIN_UTIL_KEYS], str(ret))) + + #remove if any old key_val_mask + old_key_val_mask = self._check_keybind_change(key) + if old_key_val_mask: + dbg("found old key binding, removing: (%s)" % str(old_key_val_mask)) + del self.map_key_to_act[old_key_val_mask] + + #map key-val-mask to action, used to ease key-press management + self.map_key_to_act[ret] = key[PLUGIN_UTIL_ACT] + + + #map action to key-combo-str, used in preferences->keybinding + self.map_act_to_keys[key[PLUGIN_UTIL_ACT]] = key[PLUGIN_UTIL_KEYS] + #map action to key-combo description, in used preferences->keybinding + self.map_act_to_desc[key[PLUGIN_UTIL_ACT]] = key[PLUGIN_UTIL_DESC] + + def unbindkey(self, key): + + # Suppose user changed the key-combo and its diff from + # what the plugin had set by default, we need to get + # current key-combo + act = key[PLUGIN_UTIL_ACT] + act_keys = self.map_act_to_keys[act] + + (keyval, mask) = self.keybindings._parsebinding(act_keys) + keyval = Gdk.keyval_to_lower(keyval) + mask = Gdk.ModifierType(mask) + + ret = (keyval, mask) + dbg("unbindkey: (%s) (%s)" % (key[PLUGIN_UTIL_KEYS], str(ret))) + + # FIXME keys should always be there, can also use .pop(key, None) + # lets do it after testing + del self.map_key_to_act[ret] + del self.map_act_to_keys[key[PLUGIN_UTIL_ACT]] + del self.map_act_to_desc[key[PLUGIN_UTIL_ACT]] + + + def keyaction(self, event): + #FIXME MOD2 mask comes in the event, remove + event.state &= ~Gdk.ModifierType.MOD2_MASK + + keyval = Gdk.keyval_to_lower(event.keyval) + ret = (keyval, event.state) + dbg("keyaction: (%s)" % str(ret)) + return self.map_key_to_act.get(ret, None) + + def get_act_to_keys(self): + return self.map_act_to_keys + + def get_act_to_desc(self): + return self.map_act_to_desc diff --git a/terminatorlib/plugin_util.py b/terminatorlib/plugin_util.py deleted file mode 100644 index e6547686..00000000 --- a/terminatorlib/plugin_util.py +++ /dev/null @@ -1,95 +0,0 @@ -""" --Basic plugin util for key-press handling, has all mapping to be used -in layout keybindings - -Vishweshwar Saran Singh Deo vssdeo@gmail.com -""" - -from gi.repository import Gtk, Gdk - -from terminatorlib.util import get_config_dir, err, dbg, gerr -from terminatorlib.keybindings import Keybindings, KeymapError - -PLUGIN_UTIL_DESC = 0 -PLUGIN_UTIL_ACT = 1 -PLUGIN_UTIL_KEYS = 2 - -#FIXME to sync this with keybinding preferences -class KeyBindUtil: - - keybindings = Keybindings() - - map_key_to_act = {} - map_act_to_keys = {} - map_act_to_desc = {} - - - #Example - # bind - # first param is desc, second is action str - # self.keyb.bindkey([PluginUrlFindNext , PluginUrlActFindNext, "j"]) - # - # get action name - # act = self.keyb.keyaction(event) - - # if act == "url_find_next": - - - #check map key_val_mask -> action - def _check_keybind_change(self, key): - act = key[PLUGIN_UTIL_ACT] - for key_val_mask in self.map_key_to_act: - old_act = self.map_key_to_act[key_val_mask] - if act == old_act: - return key_val_mask - return None - - #check in config before binding - def bindkey_check_config(self, key, config): - actstr = key[PLUGIN_UTIL_ACT] - kbsect = config.base.get_item('keybindings') - keystr = kbsect[actstr] if actstr in kbsect else "" - dbg("bindkey_check_config:action (%s) key str:(%s)" % (actstr, keystr)) - if len(keystr): - key[PLUGIN_UTIL_KEYS] = keystr - dbg("found new Action->KeyVal in config: (%s, %s)" - % (actstr, keystr)); - self.bindkey(key) - - def bindkey(self, key): - (keyval, mask) = self.keybindings._parsebinding(key[PLUGIN_UTIL_KEYS]) - keyval = Gdk.keyval_to_lower(keyval) - mask = Gdk.ModifierType(mask) - - ret = (keyval, mask) - dbg("bindkey: (%s) (%s)" % (key[PLUGIN_UTIL_KEYS], str(ret))) - - #remove if any old key_val_mask - old_key_val_mask = self._check_keybind_change(key) - if old_key_val_mask: - dbg("found old key binding, removing: (%s)" % str(old_key_val_mask)) - del self.map_key_to_act[old_key_val_mask] - - #map key-val-mask to action, used to ease key-press management - self.map_key_to_act[ret] = key[PLUGIN_UTIL_ACT] - - - #map action to key-combo-str, used in preferences->keybinding - self.map_act_to_keys[key[PLUGIN_UTIL_ACT]] = key[PLUGIN_UTIL_KEYS] - #map action to key-combo description, in used preferences->keybinding - self.map_act_to_desc[key[PLUGIN_UTIL_ACT]] = key[PLUGIN_UTIL_DESC] - - def keyaction(self, event): - #FIXME MOD2 mask comes in the event, remove - event.state &= ~Gdk.ModifierType.MOD2_MASK - - keyval = Gdk.keyval_to_lower(event.keyval) - ret = (keyval, event.state) - dbg("keyaction: (%s)" % str(ret)) - return self.map_key_to_act.get(ret, None) - - def get_act_to_keys(self): - return self.map_act_to_keys - - def get_act_to_desc(self): - return self.map_act_to_desc diff --git a/terminatorlib/plugins/mousefree_url_handler.py b/terminatorlib/plugins/mousefree_url_handler.py index 37c80f90..bb782ea3 100644 --- a/terminatorlib/plugins/mousefree_url_handler.py +++ b/terminatorlib/plugins/mousefree_url_handler.py @@ -14,10 +14,9 @@ from terminatorlib.terminator import Terminator from terminatorlib.config import Config import terminatorlib.plugin as plugin -from terminatorlib.plugin_util import KeyBindUtil +from terminatorlib.plugin import KeyBindUtil from terminatorlib.util import get_config_dir, err, dbg, gerr -from terminatorlib.keybindings import Keybindings, KeymapError from terminatorlib import regex import re @@ -45,7 +44,8 @@ class MouseFreeURLHandler(plugin.Plugin): match = None flag_http_on = False - keyb = KeyBindUtil() + config = Config() + keyb = KeyBindUtil(config) matches = [] matches_ptr = -1 #basic pattern @@ -54,21 +54,34 @@ class MouseFreeURLHandler(plugin.Plugin): def __init__(self): self.connect_signals() - config = Config() - self.keyb.bindkey_check_config([PluginUrlFindNext , PluginUrlActFindNext, "j"], config) - self.keyb.bindkey_check_config([PluginUrlFindPrev , PluginUrlActFindPrev, "k"], config) - self.keyb.bindkey_check_config([PluginUrlEsc , PluginUrlActEsc, "Escape"], config) - self.keyb.bindkey_check_config([PluginUrlLaunch, PluginUrlActLaunch, "Return"], config) + self.keyb.bindkey_check_config( + [PluginUrlFindNext , PluginUrlActFindNext, "j"]) + self.keyb.bindkey_check_config( + [PluginUrlFindPrev , PluginUrlActFindPrev, "k"]) + self.keyb.bindkey_check_config( + [PluginUrlEsc , PluginUrlActEsc, "Escape"]) + self.keyb.bindkey_check_config( + [PluginUrlLaunch, PluginUrlActLaunch, "Return"]) def connect_signals(self): self.windows = Terminator().get_windows() for window in self.windows: window.connect('key-press-event', self.on_keypress) - return - def callback(self, url): - """Actually we don't need to do anything for this to work""" - return(url) + + def unload(self): + # + for window in self.windows: + window.disconnect_by_func(self.on_keypress) + + self.keyb.unbindkey( + [PluginUrlFindNext , PluginUrlActFindNext, "j"]) + self.keyb.unbindkey( + [PluginUrlFindPrev , PluginUrlActFindPrev, "k"]) + self.keyb.unbindkey( + [PluginUrlEsc , PluginUrlActEsc, "Escape"]) + self.keyb.unbindkey( + [PluginUrlLaunch, PluginUrlActLaunch, "Return"]) def extract(self): #can we do extract more efficiently diff --git a/terminatorlib/prefseditor.py b/terminatorlib/prefseditor.py index 5cdfb336..f34a0fac 100755 --- a/terminatorlib/prefseditor.py +++ b/terminatorlib/prefseditor.py @@ -18,7 +18,7 @@ from .terminator import Terminator from .plugin import PluginRegistry from .version import APP_NAME -from .plugin_util import KeyBindUtil +from .plugin import KeyBindUtil def get_color_string(widcol): return('#%02x%02x%02x' % (widcol.red>>8, widcol.green>>8, widcol.blue>>8))