-
Notifications
You must be signed in to change notification settings - Fork 1
/
clipboard-relay
executable file
·53 lines (44 loc) · 1.36 KB
/
clipboard-relay
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
#! /usr/bin/env ruby
# relays Clipboard and Primary changes over DBus
require 'rubygems'
require 'dbus'
require 'gtk2'
class ClipboardRelay < DBus::Object
def initialize(path, gtk_id)
super(path)
@last_text = nil
@gtk_clipboard = Gtk::Clipboard.get(gtk_id)
@gtk_clipboard.signal_connect("owner-change") do |clipboard, event|
owner_change(clipboard)
end
@gtk_clipboard.request_text { |clipboard, text| got_text(text) }
end
def owner_change(a_clipboard)
a_clipboard.request_text { |clipboard, text| got_text(text) }
end
def got_text(text)
return if text.nil? # clipboard is not textual
return if text == @last_text
self.Changed(text)
@last_text = text
end
dbus_interface "net.vidner.ClipboardRelay" do
dbus_method :Set, "in text:s" do |text|
@gtk_clipboard.text = text
end
dbus_method :Get, "out text:s" do
[@last_text]
end
dbus_signal :Changed, "text:s"
end
end
bus = DBus::SessionBus.instance
bus.glibize
clipboard = ClipboardRelay.new("/net/vidner/ClipboardRelay/Clipboard",
Gdk::Atom.intern("CLIPBOARD"))
primary = ClipboardRelay.new("/net/vidner/ClipboardRelay/Primary",
Gdk::Atom.intern("PRIMARY"))
service = bus.request_service "net.vidner.ClipboardRelay"
service.export clipboard
service.export primary
Gtk.main