How to tell if a full-screen application is running?

Is it possible in python to tell if a full screen application on linux is running?
I have a feeling it might be possible using Xlib but I haven’t found a way.

EDIT: By full screen I mean the WHOLE screen nothing else but the application, such as a full-screen game.

If all Window Managers you’re interested in running under support EWMH, the Extended Window Manager Hints standard, there are elegant ways to perform this (speaking to Xlib via ctypes, for example). The _NET_ACTIVE_WINDOW property of the root window (see here) tells you which window is active (if any); the _NET_WM_STATE property of the active window is then a list of atoms describing its state which will include _NET_WM_STATE_FULLSCREEN if that window is fullscreen. (If you have multiple monitors of course a window could be fullscreen on one of them without being active; I believe other cases may exist in which a window may be fullscreen without being active — I don’t think there’s any way to cover them all without essentially checking _NET_WM_STATE for every window, though).

Found a solution:

import Xlib.display

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

num_of_fs = 0
for window in root_win.query_tree()._data['children']:
    window_name = window.get_wm_name()
    width = window.get_geometry()._data["width"]
    height = window.get_geometry()._data["height"]

    if width == screen.width_in_pixels and height == screen.height_in_pixels:
        num_of_fs += 1

print num_of_fs

This prints out the number of fullscreen windows which for me is normally one. When playing a full screen game its 2.

Just updating the code posted by Isaiah, which didn’t work for me.

This is a preliminary version, it can be simplified, in my tests it worked well, this code is part of a project called xkeysnail and it works well in getting the window name.

import sys
from time import sleep
import Xlib.display

def window_is_fullscreen(display=Xlib.display.Display()):
    """Check if window is fullscreen or not."""
    focused_window = display.get_input_focus().focus
    screen = display.screen()
    wm_info = win_width = get_focused_window_info(focused_window)
    win_width = get_focused_window_info(focused_window)['width']
    win_height = get_focused_window_info(focused_window)['height']
    reso = get_desktop_resolution(screen)
    # Desktop environments in general should appear in fullscreen,
    # generating false positives, a way to ignore them is necessary.
    if wm_info['class'] != 'xfdesktop':
        if win_width == reso['width'] and win_height == reso['height']:
            return True
        else:
            return False

def get_desktop_resolution(screen):
    """Get desktop resolution."""
    return {
        'width': screen.width_in_pixels, 
        'height': screen.height_in_pixels
    }

def get_focused_window_info(window):
    """Get info from focused window'"""
    wmname = window.get_wm_name()
    try:
        wmclass = window.get_wm_class()[0]
    except TypeError:
        wmclass = window.get_wm_class()
        pass
    wm_data = window.get_geometry()._data
    width = wm_data['width']
    height = wm_data['height']
    # workaround for Java app
    # https://github.com/JetBrains/jdk8u_jdk/blob/master/src/solaris/classes/sun/awt/X11/XFocusProxyWindow.java#L35
    if not wmclass and not wmname or "FocusProxy" in wmclass:
        parent_window = window.query_tree().parent
        if parent_window:
            return get_focused_window_info(parent_window)
    elif wmclass and wmname:
        return {'class': wmclass, 'name': wmname, 'width': width, 'height': height}
    elif wmclass and not wmname:
        return {'class': wmclass, 'width': width, 'height': height}
    elif not wmclass and wmname:
        return {'name': wmname, 'width': width, 'height': height}
    return None
    
while True:
    try:
        print(window_is_fullscreen(), end='r')
        sleep(1)
    except KeyboardInterrupt:
        sys.exit(0)

Leave a Comment