summaryrefslogtreecommitdiffstats
path: root/Selection.py
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2013-03-06 14:16:47 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2013-03-06 14:16:47 +0100
commit4659883ba00bd499f41f4cc1a5f96e4bdd5ac9de (patch)
tree74211870b90bb20c0041c8a76d677579a88206a9 /Selection.py
parente342d5c7241cd04352948fb6937e05a98000b70c (diff)
downloadpylock-4659883ba00bd499f41f4cc1a5f96e4bdd5ac9de.tar
pylock-4659883ba00bd499f41f4cc1a5f96e4bdd5ac9de.zip
Rework pylock to behave like xlock
Diffstat (limited to 'Selection.py')
-rw-r--r--Selection.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/Selection.py b/Selection.py
new file mode 100644
index 0000000..11a7285
--- /dev/null
+++ b/Selection.py
@@ -0,0 +1,66 @@
+# Copyright (c) 2012-2013, Matthias Schiffer <mschiffer@universe-factory.net>
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
+import ctypes
+import ctypes.util
+
+from gi.repository import Gdk, GdkX11
+
+
+XEvent_p = ctypes.c_void_p
+display_p = ctypes.c_void_p
+xid = ctypes.c_ulong
+x11_bool = ctypes.c_int
+x11_time = ctypes.c_ulong
+status = ctypes.c_int
+
+
+libX11 = ctypes.util.find_library('X11')
+if libX11 == None:
+ raise OSError('libX11 could not be found.')
+libX11 = ctypes.cdll.LoadLibrary(libX11)
+
+libX11.XSetSelectionOwner.argtypes = display_p, xid, xid, x11_time
+libX11.XSetSelectionOwner.restype = ctypes.c_int
+libX11.XGetSelectionOwner.argtypes = display_p, xid
+libX11.XGetSelectionOwner.restype = xid
+
+
+display = Gdk.Display.get_default()
+dpy_p = display_p(hash(GdkX11.X11Display.get_xdisplay(display)))
+atomPylockWindow = GdkX11.x11_get_xatom_by_name("PYLOCK_WINDOW")
+
+
+def get():
+ return libX11.XGetSelectionOwner(dpy_p, atomPylockWindow)
+
+def acquire(window):
+ if get() != 0:
+ return False
+
+ window.realize()
+ xid = GdkX11.X11Window.get_xid(window.get_window())
+ libX11.XSetSelectionOwner(dpy_p, atomPylockWindow, xid, 0)
+
+ return (get() == xid)