Python의 Xlib를 사용하여 X 창 찾기

Python의 Xlib를 사용하여 X 창 찾기

내 X 서버에서 매핑되지 않은 창을 찾아서 다시 매핑하고 일부를 보내려고 합니다.유럽 ​​WMH힌트. 창이 매핑되지 않았기 때문에 EWMH를 사용하여 창 관리자에게 직접 문의할 수 없습니다. 그래서 Xlib를 통해 얻으려고 했지만 문제가 발생했습니다. 전체 API가 나에게 매우 혼란스럽습니다.

저는 파이썬을 사용하고 있어요Xlib 래퍼. 이제 다음 Python 스크립트를 살펴보겠습니다.

import subprocess
from time import sleep
from ewmh import EWMH

subprocess.Popen(['urxvt']) # Run some program, here it is URXVT terminal.
sleep(1) # Wait until the term is ready, 1 second is really enought time.

ewmh = EWMH() # Python has a lib for EWMH, I use it for simplicity here.

# Get all windows?
windows = ewmh.display.screen().root.query_tree().children

# Print WM_CLASS properties of all windows.
for w in windows: print(w.get_wm_class())

스크립트의 출력은 무엇입니까? 열려 있는 URXVT 터미널은 다음과 같습니다.

None
None
None
None
('xscreensaver', 'XScreenSaver')
('firefox', 'Firefox')
('Toplevel', 'Firefox')
None
('Popup', 'Firefox')
None
('Popup', 'Firefox')
('VIM', 'Vim_xterm')

하지만이 명령을 실행하고 열린 터미널을 클릭하면 다음과 같습니다.

$ xprop | grep WM_CLASS
WM_CLASS(STRING) = "urxvt", "URxvt"

와 동일WM_NAME재산.

마지막 질문: 스크립트 출력에 "URxvt" 문자열이 없는 이유는 무엇입니까?

답변1

문자열이 없는 이유"urxvt", "URxvt"XWindows는 계층적입니다. 어떤 이유로 내 데스크탑에서는 urxvt 창이 첫 번째 수준에 없습니다.

따라서 다음과 같이 전체 트리를 탐색해야 합니다.

from Xlib.display import Display

def printWindowHierrarchy(window, indent):
    children = window.query_tree().children
    for w in children:
        print(indent, w.get_wm_class())
        printWindowHierrarchy(w, indent+'-')

display = Display()
root = display.screen().root
printWindowHierrarchy(root, '-')

스크립트 출력의 한 줄(매우 긴)은 다음과 같습니다.

--- ('urxvt', 'URxvt')

관련 정보