Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.