~vcs-imports/kupfer/master-new

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
This is a program of its own, that does not integrate with the
Kupfer process.
"""
import __builtin__
import os

import gtk
import keybinder
import dbus

from dbus.mainloop.glib import DBusGMainLoop

SERV = "se.kaizer.kupfer"
OBJ = "/interface"
IFACE = "se.kaizer.kupfer.Listener"

if not hasattr(__builtin__, '_'):
	def _(x):
		return x

def get_all_keys():
	try:
		bus = dbus.Bus()
		obj = bus.get_object(SERV, OBJ)
		iface = dbus.Interface(obj, IFACE)
		return iface.GetBoundKeys(byte_arrays=True)
	except dbus.DBusException as exc:
		print exc
		print "Waiting for Kupfer to start.."
		return []

def rebind_key(keystring, is_bound):
	if is_bound:
		print "binding", keystring
		keybinder.bind(keystring, relay_key, keystring)
	else:
		print "unbinding", keystring
		keybinder.unbind(keystring)

def relay_key(key):
	print "Relaying", key
	time = keybinder.get_current_event_time()
	s_id = "kupfer-%d_TIME%s" % (os.getpid(), time)
	bus = dbus.Bus()
	obj = bus.get_object(SERV, OBJ, introspect=False)
	iface = dbus.Interface(obj, IFACE)
	iface.RelayKeysFromDisplay(key, os.getenv("DISPLAY", ":0"), s_id)

def main():
	DBusGMainLoop(set_as_default=True)

	relayed_keys = list(get_all_keys())

	for key in relayed_keys:
		rebind_key(key, True)
	bus = dbus.Bus()
	bus.add_signal_receiver(rebind_key, 'BoundKeyChanged',
			dbus_interface=IFACE)
	sicon = gtk.status_icon_new_from_icon_name("kupfer")
	display = os.getenv("DISPLAY", ":0")
	sicon.set_tooltip(_("Keyboard relay is active for display %s") % display)
	sicon.set_visible(True)
	try:
		gtk.main()
	except KeyboardInterrupt:
		raise SystemExit(0)

if __name__ == '__main__':
	main()