Search code examples
gtkx11cairoxlib

X11: Draw overlay / Remove drawings from overlay


I need to draw a shape directly on screen (a little arrow) in X11, it serves as an overlay. I am searching for about an hour no with no good results. Could anyone provide me a good entry point for what I need? The technologies I can use are cairo, gtk or XLib.

Everything I have found so far either depends on Composition, which is not always available, or will create a white shape behind my arrow (rectangle, a window).

EDIT: I am now able to draw an X11 Overlay using Composite and Cairo. I do it this way (Note: This is a minimal example. It has few to no error checking!!!). Start from Terminal to be able to quit it!

// COMPILE WITH: g++ -o overlay overlay.cc -lXfixes -lXcomposite -lX11 `pkg-config --cflags --libs cairo`
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/shape.h>
#include <cairo/cairo.h>
#include <cairo/cairo-xlib.h>

#include <stdio.h>
#include <unistd.h>

Display *display_;
int old_x_, old_y_;
cairo_surface_t *surf_;
Window overlay_;
int screen_;
int height_;
int width_;
cairo_t *cr_;


void paint_cursor(int new_x, int new_y, bool first = false)
{

    if (!first)
    {
        cairo_set_operator(cr_, CAIRO_OPERATOR_CLEAR);
        cairo_rectangle(cr_, old_x_, old_y_, 20, 20);
        cairo_fill(cr_);
    }
    old_x_ = new_x;
    old_y_ = new_y;

    cairo_set_operator(cr_, CAIRO_OPERATOR_SOURCE);
    cairo_move_to(cr_, new_x, new_y);
    cairo_line_to(cr_, new_x + 0, new_y + 16);
    cairo_line_to(cr_, new_x + 4, new_y + 13);
    cairo_line_to(cr_, new_x + 7, new_y + 18);
    cairo_line_to(cr_, new_x + 9, new_y + 17);
    cairo_line_to(cr_, new_x + 6, new_y + 12);
    cairo_line_to(cr_, new_x + 11, new_y + 12);
    cairo_line_to(cr_, new_x + 0, new_y + 0);

    cairo_set_source_rgba(cr_, 0.0, 0.0, 0.0, 0.5);
    cairo_stroke_preserve(cr_);
    cairo_set_source_rgba(cr_, 0.0, 1.0, 0.0, 0.5);
    cairo_fill(cr_);
}

int main()
{

    display_ = ::XOpenDisplay(0);
    if (!display_)
    {
        return -1;
    }

    screen_ = ::XDefaultScreen(display_);
    Window root = RootWindow(display_, screen_);

    ::XCompositeRedirectSubwindows(display_, root, CompositeRedirectAutomatic);
    ::XSelectInput(display_, root, SubstructureNotifyMask);

    overlay_ = ::XCompositeGetOverlayWindow(display_, root);

    XserverRegion region = ::XFixesCreateRegion(display_, 0, 0);
    ::XFixesSetWindowShapeRegion(display_, overlay_, ShapeBounding, 0, 0, 0);
    ::XFixesSetWindowShapeRegion(display_, overlay_, ShapeInput, 0, 0, region);
    ::XFixesDestroyRegion(display_, region);

    width_ = DisplayWidth(display_, screen_);
    height_ = DisplayHeight(display_, screen_);

    surf_ = ::cairo_xlib_surface_create(display_, overlay_, DefaultVisual(display_, screen_), width_, height_);

    cr_ = ::cairo_create(surf_);
    ::XSelectInput(display_, overlay_, ExposureMask);

    old_x_ = 0;
    old_y_ = 0;
    paint_cursor(0, 0, true);

    XEvent ev;

    Window root_return, child_return;
    int root_x_return, root_y_return;
    int win_x_return, win_y_return;
    unsigned int mask;


    for (;;)
    {
        XQueryPointer(display_, root, &root_return, &child_return, &root_x_return, &root_y_return, &win_x_return, &win_y_return, &mask);
        paint_cursor(root_x_return, root_y_return);
        printf("Paint\n");
    }

    return 0;
}

The question that is left: How to I remove the old cursor that was drawn? I tried XClearArea, I tried to overpaint with Cairo, I tried the Cairo Clear operator, etc. Nothing worked. Can someone point me here in the right direction?


Solution

  • I have solved this issue myself now using the Shape extension from X11 and a separate message loop in a thread. Its not ideal but it works. Ill post code as soon as I have access to the code again (sick atm).