X11 - Draw on Overlay Window

前端 未结 2 1831
花落未央
花落未央 2020-12-16 00:43

I want to draw simple primitives at specific pixels on the screen (similar to this question). In order to do that I draw on top of all windows using the Overlay Window of th

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-16 01:38

    sleep(50)! that's too much, it's 50 seconds. I used 5ms delay which works well.

    Your problem seems with the runtime environment. You should have a composite display manager running already. (Not all display managers work as expected, better to try on different ones)

    I confirm that screen below updated without any problem and I could interact with it.

    This was run on:

    Ubuntu 15.10
    Kernel 4.2.0-18-generic
    X.Org X Server 1.17.2
    Compiz 0.9.12.2
    

    Here the full code with just delay modification:

    #include 
    #include 
    #include 
    #include 
    
    #include 
    #include 
    #include 
    
    #include 
    #include 
    
    Display *d;
    Window overlay;
    Window root;
    int width, height;
    
    void
    allow_input_passthrough (Window w)
    {
        XserverRegion region = XFixesCreateRegion (d, NULL, 0);
    
        XFixesSetWindowShapeRegion (d, w, ShapeBounding, 0, 0, 0);
        XFixesSetWindowShapeRegion (d, w, ShapeInput, 0, 0, region);
    
        XFixesDestroyRegion (d, region);
    }
    
    void
    prep_overlay (void)
    {
        overlay = XCompositeGetOverlayWindow (d, root);
        allow_input_passthrough (overlay);
    }
    
    void draw(cairo_t *cr) {
        int quarter_w = width / 4;
        int quarter_h = height / 4;
        cairo_set_source_rgb(cr, 1.0, 0.0, 0.0);
        cairo_rectangle(cr, quarter_w, quarter_h, quarter_w * 2, quarter_h * 2);
        cairo_fill(cr);
    }
    
    int main() {
        struct timespec ts = {0, 5000000};
    
        d = XOpenDisplay(NULL);
    
        int s = DefaultScreen(d);
        root = RootWindow(d, s);
    
        XCompositeRedirectSubwindows (d, root, CompositeRedirectAutomatic);
        XSelectInput (d, root, SubstructureNotifyMask);
    
        width = DisplayWidth(d, s);
        height = DisplayHeight(d, s);
    
        prep_overlay();
    
        cairo_surface_t *surf = cairo_xlib_surface_create(d, overlay,
                                      DefaultVisual(d, s),
                                      width, height);
        cairo_t *cr = cairo_create(surf);
    
        XSelectInput(d, overlay, ExposureMask);
    
        draw(cr);
    
        XEvent ev;
        while(1) {
          overlay = XCompositeGetOverlayWindow (d, root);
          draw(cr);
          XCompositeReleaseOverlayWindow (d, root);
          nanosleep(&ts, NULL);
        }
    
        cairo_destroy(cr);
        cairo_surface_destroy(surf);
        XCloseDisplay(d);
        return 0;
    }
    

提交回复
热议问题