9

I want to draw some primitives on top of all windows on the screen. I've found some code in C and tried to port it to use python's xlib:

from Xlib.display import Display
from Xlib import X
from Xlib import protocol

display = Display(':0')
root = display.screen().root
gc = root.create_gc()
root.fill_rectangle(gc, 100, 100, 500, 500)

But nothing appears on the screen (however, the root window is assigned: grabbing keyboard after it works). How to do this correctly?

2
  • Unless you are writing a window manager, writing the X root is a really poor idea. Stealing the input focus from everything else is an example of how "rude" an application that does this is. I'd look for a "modal dialog" in whatever GUI toolkit you are using.
    – msw
    Jan 7, 2013 at 17:37
  • 1
    @msw: I'm writing not a window manager, but another low-level application, so it's really necessary to draw on top of all the windows.
    – aplavin
    Jan 7, 2013 at 17:44

1 Answer 1

6

You can draw on root window - your code probably works, but root window may be obscured by some kind of desktop window (nautilus, kde desktop etc). Try to start X session without any programs running and you'll see updates (you can use Xephyr or Xnest for this)

Another approach would be to create transparent window, make it topmost and draw on it's surface. You'll need to propagate mouse events to underlying windows manually.

To actually draw on top of all windows you need to create simple composite manager or if there is already CM running use it's overlay window.

From composite protocol spec:

Version 0.3 of the protocol adds the Composite Overlay Window, which provides compositing managers with a surface on which to draw without interference. This window is always above normal windows and is always below the screen saver window. It is an InputOutput window whose width and height are the screen dimensions. Its visual is the root visual and its border width is zero. Attempts to redirect it using the composite extension are ignored. This window does not appear in the reply of the QueryTree request. It is also an override redirect window. These last two features make it invisible to window managers and other X11 clients. The only way to access the XID of this window is via the CompositeGetOverlayWindow request. Initially, the Composite Overlay Window is unmapped.

CompositeGetOverlayWindow returns the XID of the Composite Overlay Window. If the window has not yet been mapped, it is mapped by this request. When all clients who have called this request have terminated their X11 connections the window is unmapped.

Composite managers may render directly to the Composite Overlay Window, or they may reparent other windows to be children of this window and render to these. Multiple clients may render to the Composite Overlay Window, create child windows of it, reshape it, and redefine its input region, but the specific arbitration rules followed by these clients is not defined by this specification; these policies should be defined by the clients themselves.

2
  • 3
    Unfortunately, the xlib documentation is terrible, and although it does appear to have a composite extension implemented, there is no guide how to use it.
    – Benubird
    May 6, 2015 at 14:32
  • 1
    Drawing on top of the overlay window (i.e. using cairo) requires requesting and releasing the overlay every time we want to draw something. But after drawing an image on top of the overlay, it will dissappear in a couple of milliseconds. It seems that we need some sort of the notification ("expose" event?) for the composite overlay window in order to know when we're allowed to draw on it.
    – Daniel
    Feb 20, 2018 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.