Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the list of open windows from xserver

Tags:

x11

xserver

Anyone got an idea how to get from an Xserver the list of all open windows?

like image 652
kender Avatar asked Oct 31 '08 08:10

kender


People also ask

How do I find Windows on Linux?

wmctrl -l may be what you're looking for. The wmctrl program can also perform some simple actions on the windows like moving them around and setting their properties. Show activity on this post.

Is X11 window manager?

The windowing system based on the X11 protocol keeps display server and window manager as separate components.

How do you use Xwininfo?

The xwininfo (X window information) command can be used to display information about a window. The xwininfo command will prompt you to select a window: [root@server1 ~]# xwininfo xwininfo: Please select the window about which you would like information by clicking the mouse in that window.


3 Answers

From the CLI you can use

xwininfo -tree -root

If you need to do this within your own code then you need to use the XQueryTree function from the Xlib library.

like image 200
Alnitak Avatar answered Oct 22 '22 19:10

Alnitak


If your window manager implements EWMH specification, you can also take a look at the _NET_CLIENT_LIST value of the root window. This is set by most modern window managers:

xprop -root|grep ^_NET_CLIENT_LIST

That value can easily be obtained programmatically, see your Xlib documentation!

like image 34
Marten Avatar answered Oct 22 '22 19:10

Marten


Building off of Marten's answer, (assuming your window manager supports Extended Window Manager Hints) you can feed that list of window ids back into xprop to get the _NET_WM_NAME property:

$ xprop -root _NET_CLIENT_LIST |
    pcregrep -o1 '# (.*)' |
    sed 's/, /\n/g' |
    xargs -I{} -n1 xprop -id {} _NET_WM_NAME

But at the command line, it would just be easier to use wmctrl:

$ wmctrl -l

Programmatically, with python-xlib, you can do the same with:

#!/usr/bin/env python
from Xlib.display import Display
from Xlib.X import AnyPropertyType

display = Display()
root = display.screen().root

_NET_CLIENT_LIST = display.get_atom('_NET_CLIENT_LIST')
_NET_WM_NAME = display.get_atom('_NET_WM_NAME')

client_list = root.get_full_property(
    _NET_CLIENT_LIST,
    property_type=AnyPropertyType,
).value

for window_id in client_list:
    window = display.create_resource_object('window', window_id)
    window_name = window.get_full_property(
        _NET_WM_NAME,
        property_type=AnyPropertyType,
    ).value
    print(window_name)

Or, better yet, using the EWMH library:

#!/usr/bin/env python
from ewmh import EWMH

window_manager_manager = EWMH()
client_list = window_manager_manager.getClientList()

for window in client_list:
    print(window_manager_manager.getWmName(window))
like image 11
Christian Reall-Fluharty Avatar answered Oct 22 '22 19:10

Christian Reall-Fluharty