2015-06-26 01:36:36 +01:00
|
|
|
#!/usr/bin/python2
|
2014-03-28 15:44:13 +00:00
|
|
|
|
2013-03-09 19:26:22 +00:00
|
|
|
# -*- coding: utf-8; -*-
|
2014-04-02 12:14:24 +01:00
|
|
|
# Copyright (C) 2013 Ozcan Esen <ozcanesen@gmail.com>
|
2013-03-09 19:26:22 +00:00
|
|
|
# Copyright (C) 2008 Luca Bruno <lethalman88@gmail.com>
|
|
|
|
#
|
|
|
|
# This a slightly modified version of the globalkeybinding.py file which is part of FreeSpeak.
|
2017-08-26 16:35:45 +01:00
|
|
|
#
|
2013-03-09 19:26:22 +00:00
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
2017-08-26 16:35:45 +01:00
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
2013-03-09 19:26:22 +00:00
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
2017-08-26 16:35:45 +01:00
|
|
|
#
|
2013-03-09 19:26:22 +00:00
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
2017-08-26 16:35:45 +01:00
|
|
|
#
|
2013-03-09 19:26:22 +00:00
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
2017-08-26 16:35:45 +01:00
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
2013-03-09 19:26:22 +00:00
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
2017-08-26 16:35:45 +01:00
|
|
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
2013-03-09 19:26:22 +00:00
|
|
|
# DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
import threading
|
|
|
|
|
2013-03-09 19:26:22 +00:00
|
|
|
import gi
|
2016-09-06 15:58:34 +01:00
|
|
|
gi.require_version("Gtk", "3.0")
|
2019-02-11 13:54:20 +00:00
|
|
|
from gi.repository import Gtk, Gdk, GLib, GObject
|
2013-03-09 19:26:22 +00:00
|
|
|
|
|
|
|
from Xlib import X, error
|
2019-02-11 13:54:20 +00:00
|
|
|
from Xlib.display import Display
|
2013-03-09 19:26:22 +00:00
|
|
|
|
2013-04-01 02:02:36 +01:00
|
|
|
SPECIAL_MODS = (["Super_L", "<Super>"],
|
|
|
|
["Super_R", "<Super>"],
|
|
|
|
["Alt_L", "<Alt>"],
|
|
|
|
["Alt_R", "<Alt>"],
|
|
|
|
["Control_L", "<Primary>"],
|
|
|
|
["Control_R", "<Primary>"],
|
|
|
|
["Shift_L", "<Shift>"],
|
|
|
|
["Shift_R", "<Shift>"])
|
|
|
|
|
2013-03-10 00:43:27 +00:00
|
|
|
class GlobalKeyBinding(GObject.GObject, threading.Thread):
|
|
|
|
__gsignals__ = {
|
|
|
|
'activate': (GObject.SignalFlags.RUN_LAST, None, ()),
|
|
|
|
}
|
2013-03-09 19:26:22 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
2013-03-10 00:43:27 +00:00
|
|
|
GObject.GObject.__init__ (self)
|
|
|
|
threading.Thread.__init__ (self)
|
|
|
|
self.setDaemon (True)
|
2013-03-09 19:26:22 +00:00
|
|
|
|
2016-09-06 15:58:34 +01:00
|
|
|
self.keymap = Gdk.Keymap().get_default()
|
2013-03-09 19:26:22 +00:00
|
|
|
self.display = Display()
|
|
|
|
self.screen = self.display.screen()
|
2014-01-22 20:16:27 +00:00
|
|
|
self.window = self.screen.root
|
2013-03-09 19:26:22 +00:00
|
|
|
self.ignored_masks = self.get_mask_combinations(X.LockMask | X.Mod2Mask | X.Mod5Mask)
|
|
|
|
self.map_modifiers()
|
2013-03-31 04:46:01 +01:00
|
|
|
self.raw_keyval = None
|
2014-01-22 20:16:27 +00:00
|
|
|
self.keytext = ""
|
2013-03-31 04:46:01 +01:00
|
|
|
|
2013-03-09 19:26:22 +00:00
|
|
|
def map_modifiers(self):
|
|
|
|
gdk_modifiers =(Gdk.ModifierType.CONTROL_MASK, Gdk.ModifierType.SHIFT_MASK, Gdk.ModifierType.MOD1_MASK,
|
2017-08-26 16:35:45 +01:00
|
|
|
Gdk.ModifierType.MOD2_MASK, Gdk.ModifierType.MOD3_MASK, Gdk.ModifierType.MOD4_MASK, Gdk.ModifierType.MOD5_MASK,
|
|
|
|
Gdk.ModifierType.SUPER_MASK, Gdk.ModifierType.HYPER_MASK)
|
2013-03-09 19:26:22 +00:00
|
|
|
self.known_modifiers_mask = 0
|
|
|
|
for modifier in gdk_modifiers:
|
2013-04-01 02:02:36 +01:00
|
|
|
if "Mod" not in Gtk.accelerator_name(0, modifier) or "Mod4" in Gtk.accelerator_name(0, modifier):
|
2013-03-09 19:26:22 +00:00
|
|
|
self.known_modifiers_mask |= modifier
|
|
|
|
|
|
|
|
def grab(self, key):
|
|
|
|
accelerator = key
|
2013-04-01 02:02:36 +01:00
|
|
|
accelerator = accelerator.replace("<Super>", "<Mod4>")
|
2013-03-09 19:26:22 +00:00
|
|
|
keyval, modifiers = Gtk.accelerator_parse(accelerator)
|
2013-03-10 00:43:27 +00:00
|
|
|
if not accelerator or (not keyval and not modifiers):
|
|
|
|
self.keycode = None
|
|
|
|
self.modifiers = None
|
|
|
|
return False
|
2013-03-31 04:46:01 +01:00
|
|
|
|
2014-01-22 20:16:27 +00:00
|
|
|
self.keytext = key
|
2017-04-28 13:24:38 +01:00
|
|
|
try:
|
|
|
|
self.keycode = self.keymap.get_entries_for_keyval(keyval).keys[0].keycode
|
|
|
|
except:
|
|
|
|
# In Betsy, the get_entries_for_keyval() returns an unamed tuple...
|
|
|
|
self.keycode = self.keymap.get_entries_for_keyval(keyval)[1][0].keycode
|
2013-03-09 19:26:22 +00:00
|
|
|
self.modifiers = int(modifiers)
|
|
|
|
|
|
|
|
catch = error.CatchError(error.BadAccess)
|
|
|
|
for ignored_mask in self.ignored_masks:
|
|
|
|
mod = modifiers | ignored_mask
|
2017-05-05 16:09:26 +01:00
|
|
|
result = self.window.grab_key(self.keycode, mod, True, X.GrabModeAsync, X.GrabModeAsync, onerror=catch)
|
2014-01-22 20:16:27 +00:00
|
|
|
self.display.flush()
|
|
|
|
# sync has been blocking. Don't know why.
|
|
|
|
#self.display.sync()
|
2013-03-09 19:26:22 +00:00
|
|
|
if catch.get_error():
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2013-03-31 04:46:01 +01:00
|
|
|
def ungrab(self):
|
2013-03-09 19:26:22 +00:00
|
|
|
if self.keycode:
|
2014-01-22 20:16:27 +00:00
|
|
|
self.window.ungrab_key(self.keycode, X.AnyModifier, self.window)
|
2017-08-26 16:35:45 +01:00
|
|
|
|
2014-01-22 20:16:27 +00:00
|
|
|
def rebind(self, key):
|
|
|
|
self.ungrab()
|
|
|
|
if key != "":
|
|
|
|
self.grab(key)
|
|
|
|
else:
|
|
|
|
self.keytext = ""
|
2017-08-26 16:35:45 +01:00
|
|
|
|
2014-01-22 20:16:27 +00:00
|
|
|
def set_focus_window(self, window = None):
|
|
|
|
self.ungrab()
|
|
|
|
if window is None:
|
|
|
|
self.window = self.screen.root
|
|
|
|
else:
|
2019-02-11 13:54:20 +00:00
|
|
|
print("set_focus_window")
|
|
|
|
print("xid:", window.get_xid())
|
2016-09-06 15:58:34 +01:00
|
|
|
self.window = self.display.create_resource_object("window", window.get_xid())
|
2014-01-22 20:16:27 +00:00
|
|
|
self.grab(self.keytext)
|
2013-03-09 19:26:22 +00:00
|
|
|
|
|
|
|
def get_mask_combinations(self, mask):
|
|
|
|
return [x for x in xrange(mask+1) if not (x & ~mask)]
|
|
|
|
|
|
|
|
def idle(self):
|
|
|
|
self.emit("activate")
|
|
|
|
return False
|
|
|
|
|
2013-03-09 20:37:17 +00:00
|
|
|
def activate(self):
|
2013-03-14 00:24:32 +00:00
|
|
|
GLib.idle_add(self.run)
|
2013-03-09 20:37:17 +00:00
|
|
|
|
2013-03-09 19:26:22 +00:00
|
|
|
def run(self):
|
|
|
|
self.running = True
|
|
|
|
wait_for_release = False
|
|
|
|
while self.running:
|
|
|
|
event = self.display.next_event()
|
2013-05-21 02:37:21 +01:00
|
|
|
try:
|
|
|
|
self.current_event_time = event.time
|
|
|
|
if event.detail == self.keycode and event.type == X.KeyPress and not wait_for_release:
|
|
|
|
modifiers = event.state & self.known_modifiers_mask
|
|
|
|
if modifiers == self.modifiers:
|
|
|
|
wait_for_release = True
|
|
|
|
self.display.allow_events(X.AsyncKeyboard, event.time)
|
|
|
|
else:
|
|
|
|
self.display.allow_events(X.ReplayKeyboard, event.time)
|
|
|
|
elif event.detail == self.keycode and wait_for_release:
|
|
|
|
if event.type == X.KeyRelease:
|
|
|
|
wait_for_release = False
|
|
|
|
GLib.idle_add(self.idle)
|
2013-03-09 19:26:22 +00:00
|
|
|
self.display.allow_events(X.AsyncKeyboard, event.time)
|
|
|
|
else:
|
2017-05-05 16:09:26 +01:00
|
|
|
self.display.send_event(self.window, event, X.KeyPressMask | X.KeyReleaseMask, True)
|
2013-03-09 19:26:22 +00:00
|
|
|
self.display.allow_events(X.ReplayKeyboard, event.time)
|
2017-05-05 16:09:26 +01:00
|
|
|
wait_for_release = False
|
2013-05-21 02:37:21 +01:00
|
|
|
except AttributeError:
|
|
|
|
continue
|
2013-03-09 19:26:22 +00:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.running = False
|
|
|
|
self.ungrab()
|
|
|
|
self.display.close()
|
|
|
|
|
2016-09-06 15:58:34 +01:00
|
|
|
class KeybindingWidget(Gtk.Box):
|
2013-03-31 04:46:01 +01:00
|
|
|
__gsignals__ = {
|
|
|
|
'accel-edited': (GObject.SignalFlags.RUN_LAST, None, ()),
|
|
|
|
}
|
2013-04-02 00:50:50 +01:00
|
|
|
def __init__(self, desc):
|
2013-03-31 04:46:01 +01:00
|
|
|
super(KeybindingWidget, self).__init__()
|
|
|
|
self.desc = desc
|
|
|
|
self.label = Gtk.Label(desc)
|
|
|
|
if self.desc != "":
|
|
|
|
self.pack_start(self.label, False, False, 0)
|
2013-04-01 02:02:36 +01:00
|
|
|
self.button = Gtk.Button()
|
|
|
|
self.button.set_tooltip_text(_("Click to set a new accelerator key for opening and closing the menu. ") +
|
2013-04-02 00:50:50 +01:00
|
|
|
_("Press Escape or click again to cancel the operation. ") +
|
|
|
|
_("Press Backspace to clear the existing keybinding."))
|
2013-04-01 02:11:54 +01:00
|
|
|
self.button.connect("clicked", self.clicked)
|
2013-04-01 02:02:36 +01:00
|
|
|
self.button.set_size_request(200, -1)
|
|
|
|
self.pack_start(self.button, False, False, 4)
|
2013-03-31 04:46:01 +01:00
|
|
|
|
|
|
|
self.show_all()
|
2013-04-01 02:02:36 +01:00
|
|
|
self.event_id = None
|
2013-04-01 02:11:54 +01:00
|
|
|
self.teaching = False
|
|
|
|
|
|
|
|
def clicked(self, widget):
|
|
|
|
if not self.teaching:
|
2013-04-02 00:50:50 +01:00
|
|
|
Gdk.keyboard_grab(self.get_window(), False, Gdk.CURRENT_TIME)
|
|
|
|
|
2013-04-01 02:11:54 +01:00
|
|
|
self.button.set_label(_("Pick an accelerator"))
|
2013-04-02 00:50:50 +01:00
|
|
|
self.event_id = self.connect( "key-release-event", self.on_key_release )
|
2013-04-01 02:11:54 +01:00
|
|
|
self.teaching = True
|
|
|
|
else:
|
|
|
|
if self.event_id:
|
2013-04-02 00:50:50 +01:00
|
|
|
self.disconnect(self.event_id)
|
|
|
|
self.ungrab()
|
|
|
|
self.set_button_text()
|
2013-04-01 02:11:54 +01:00
|
|
|
self.teaching = False
|
2013-03-31 04:46:01 +01:00
|
|
|
|
2013-04-01 02:02:36 +01:00
|
|
|
def on_key_release(self, widget, event):
|
2013-04-02 00:50:50 +01:00
|
|
|
self.disconnect(self.event_id)
|
|
|
|
self.ungrab()
|
2013-04-01 02:11:54 +01:00
|
|
|
self.event_id = None
|
2013-04-01 02:02:36 +01:00
|
|
|
if event.keyval == Gdk.KEY_Escape:
|
2013-04-02 00:50:50 +01:00
|
|
|
self.set_button_text()
|
|
|
|
self.teaching = False
|
|
|
|
return True
|
|
|
|
if event.keyval == Gdk.KEY_BackSpace:
|
|
|
|
self.teaching = False
|
|
|
|
self.value = ""
|
|
|
|
self.set_button_text()
|
|
|
|
self.emit("accel-edited")
|
2013-04-01 02:02:36 +01:00
|
|
|
return True
|
2016-09-06 15:58:34 +01:00
|
|
|
accel_string = Gtk.accelerator_name( event.keyval, event.state )
|
2013-04-01 02:02:36 +01:00
|
|
|
accel_string = self.sanitize(accel_string)
|
2013-03-31 04:46:01 +01:00
|
|
|
self.value = accel_string
|
2013-04-02 00:50:50 +01:00
|
|
|
self.set_button_text()
|
2013-04-01 02:11:54 +01:00
|
|
|
self.teaching = False
|
2013-04-02 00:50:50 +01:00
|
|
|
self.emit("accel-edited")
|
2013-04-01 02:02:36 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
def sanitize(self, string):
|
|
|
|
accel_string = string.replace("<Mod2>", "")
|
|
|
|
accel_string = accel_string.replace("<Mod4>", "")
|
|
|
|
for single, mod in SPECIAL_MODS:
|
|
|
|
if single in accel_string and mod in accel_string:
|
|
|
|
accel_string = accel_string.replace(mod, "")
|
|
|
|
return accel_string
|
2013-03-31 04:46:01 +01:00
|
|
|
|
|
|
|
def get_val(self):
|
|
|
|
return self.value
|
|
|
|
|
|
|
|
def set_val(self, value):
|
|
|
|
self.value = value
|
2013-04-02 00:50:50 +01:00
|
|
|
self.set_button_text()
|
|
|
|
|
|
|
|
def ungrab(self):
|
|
|
|
Gdk.keyboard_ungrab(Gdk.CURRENT_TIME)
|
|
|
|
|
|
|
|
def set_button_text(self):
|
|
|
|
if self.value == "":
|
|
|
|
self.button.set_label(_("<not set>"))
|
|
|
|
else:
|
|
|
|
self.button.set_label(self.value)
|