使用Python xlib更改活动X窗口时获取通知

问题描述 投票:3回答:1

我想监视在运行X的Linux系统上哪个窗口处于活动状态,以及何时调整该活动窗口的大小或移动该窗口。我可以监视活动窗口(该窗口保留在根窗口的_NET_ACTIVE_WINDOW属性中,并且可以在根窗口上注册PropertyNotify事件以发现该属性何时更改)。但是,我不知道如何监视活动窗口以了解它是否已调整大小或移动。

import Xlib
import Xlib.display

disp = Xlib.display.Display()
Xroot = disp.screen().root
NET_ACTIVE_WINDOW = disp.intern_atom('_NET_ACTIVE_WINDOW')
Xroot.change_attributes(event_mask=Xlib.X.PropertyChangeMask)

while True:
    # loop until an event happens that we care about
    # we care about a change to which window is active
    # (NET_ACTIVE_WINDOW property changes on the root)
    # or about the currently active window changing
    # in size or position (don't know how to do this)
    event = disp.next_event()
    if (event.type == Xlib.X.PropertyNotify and
            event.atom == NET_ACTIVE_WINDOW):
        active = disp.get_input_focus().focus
        try:
            name = active.get_wm_class()[1]
        except TypeError:
            name = "unknown"
        print("The active window has changed! It is now", name)

有没有办法做到这一点?它可能涉及在当前活动的窗口上侦听ConfigureNotify事件(并在该窗口变为活动状态时在该窗口上调用change_attributes以设置适当的掩码),但我无法使其正常工作。

((注意:我没有使用Gtk,所以请不要使用Gtk解决方案。)

更新:有一种相当可疑的方法来检测窗口调整大小,方法是通过监视活动窗口中_NET_WM_OPAQUE_REGION属性的值更改(因为我正确地接收了PropertyChange事件,尽管我没有收到ConfigureNotify事件, )。但是,尚不清楚所有窗口管理器是否都设置了此属性,并且仅在调整窗口大小时会更改此属性。它不会随窗口移动而改变(其他任何属性也不会改变)。

python x11 xlib
1个回答
0
投票

这样做的方法是在根窗口上选择SubstructureNotifyMask,然后读取所有ConfigureNotify事件,并忽略那些不在我们关心的窗口中的事件,因此:

import Xlib
import Xlib.display

disp = Xlib.display.Display()
Xroot = disp.screen().root
NET_ACTIVE_WINDOW = disp.intern_atom('_NET_ACTIVE_WINDOW')
Xroot.change_attributes(event_mask=Xlib.X.PropertyChangeMask |
                        Xlib.X.SubstructureNotifyMask)

windows = []

while True:
    # loop until an event happens that we care about
    # we care about a change to which window is active
    # (NET_ACTIVE_WINDOW property changes on the root)
    # or about the currently active window changing
    # in size or position (ConfigureNotify event for
    # our window or one of its ancestors)
    event = disp.next_event()
    if (event.type == Xlib.X.PropertyNotify and
            event.atom == NET_ACTIVE_WINDOW):
        active = disp.get_input_focus().focus
        try:
            name = active.get_wm_class()[1]
        except TypeError:
            name = "unknown"
        print("The active window has changed! It is now", name)

        # Because an X window is not necessarily just what one thinks of
        # as a window (the window manager may add an invisible frame, and
        # so on), we record not just the active window but its ancestors
        # up to the root, and treat a ConfigureNotify on any of those
        # ancestors as meaning that the active window has been moved or resized
        pointer = active
        windows = []
        while pointer.id != Xroot.id:
            windows.append(pointer)
            pointer = pointer.query_tree().parent
    elif event.type == Xlib.X.ConfigureNotify and event.window in windows:
        print("Active window size/position is now", event.x, event.y,
              event.width, event.height)
© www.soinside.com 2019 - 2024. All rights reserved.