Ask Your Question
2

What is the method to identify the primary display using Python Xlib?

asked 2022-11-25 11:00:00 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-11-07 00:00:00 +0000

woof gravatar image

The primary display in X Window System is usually referred to as Display 0 or :0.

Here is the method to identify the primary display using Python Xlib:

from Xlib import X, display

# get the default display
default_display = display.Display()

# get the screen of the default display
screen = default_display.screen()

# get the root window of the screen
root_window = screen.root

# get the atom of the primary screen
atom = default_display.intern_atom('_NET_ACTIVE_WINDOW', False)

# get the primary window using the atom
primary_window = root_window.get_full_property(atom, X.AnyPropertyType).value[0]

# get the display string of the primary window
primary_display_string = primary_window.get_full_property(
    default_display.intern_atom('_NET_WM_NAME', False),
    X.AnyPropertyType
).value

# convert the display string to a string
primary_display_string = primary_display_string.decode('utf-8')

# print the primary display string (usually :0)
print(primary_display_string)

This method gets the default display, screen, and root window of the X Window System. It then gets the atom of the primary screen and uses it to get the primary window. Finally, it gets the display string of the primary window and converts it to a string. The result is usually ":0", which is the display string of the primary display.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-11-25 11:00:00 +0000

Seen: 7 times

Last updated: Nov 07 '21