This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
pylock/Message.py

79 lines
2.8 KiB
Python
Raw Normal View History

# Copyright (c) 2012, 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.XSendEvent.argtypes = display_p, xid, x11_bool, ctypes.c_long, XEvent_p
libX11.XSendEvent.restype = status
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")
atomPylockActionLock = GdkX11.x11_get_xatom_by_name("PYLOCK_ACTION_LOCK")
def getSelection():
return libX11.XGetSelectionOwner(dpy_p, atomPylockWindow)
def sendLockMessage():
window = getSelection()
if window == 0:
return
gdkWindow = GdkX11.X11Window.foreign_new_for_display(display, window)
gdkWindow.show()
display.sync()
def acquireSelection(window):
if getSelection() != 0:
return False
window.realize()
xid = GdkX11.X11Window.get_xid(window.get_window())
libX11.XSetSelectionOwner(dpy_p, atomPylockWindow, xid, 0)
return (getSelection() == xid)