[cairo] cairo / xlib not updating window content

Pavel Petrovic pavel.petrovic.level2 at gmail.com
Sat Jul 28 07:50:54 UTC 2018

I am trying to learn how to use Cairo 2D drawing library with xlib surfaces.

I wrote a little test program that allows creating multiple windows.
Each function may have a custom paint() function that is called
regularly to add some graphics content to the window, or redraw it
completely if desired. There is also an option to define mouse and key
listener. The main routine checks for X events (to delegate them to
mouse and key listener) and for timeout for periodic call of those
paint() functions.

I tried with the 1.14.6 version of Cairo (that is currently available
as package in Ubuntu 16.04), and the latest 1.15.12, but the results
are the same.

The expected behavior of this demo is to open 3 windows. One will have
random rectangles being added, another one random texts, and the third
random circles.

In addition, clicking into windows should produce lines (connecting to
mouse click, or randomly), and using arrow keys should draw a red line
in the window with circles.

The circles and text seem to show up regularly as expected. All three
windows should have white background, but two of them are black. And
the worst, the window with rectangles does not get updated much (and
it does not matter if it is the first window created or not, it is
always the rectangles that do not show up properly).

They are only shown when the focus changes to or from that window -
then the remaining rectangles that should have been drawn meanwhile
suddenly appear.

I am calling cairo_surface_flush() on the surface of each window after
adding any content, but that does not help. I also tried posting
XEvents to that window of various kind (such as focus), they arrive,
but rectangles do not show up.

Furthermore, even though drawing lines with mouse works fine, drawing
line with key arrows suffers from the same problem - it is drawn, but
not shown properly.

I am obviously wrong in some of my assumptions about what this library
can do, but I am not sure where.

It seems that there are some two competing versions of drawing being
shown, since it happens sometimes that one or two rectangles, or
pieces of the red line are flashing. Some kind of strange buffering,
caching? It may just be some bug in my program, I do not know.

Another observation - the black background is because drawing white
background happens before the window is shown, and thus those
cairo_paint calls are somehow discarded. I do not know how to make the
window appear earlier, it seems it appears only after some later
changes on the screen.

I am stuck on this after a couple of desperate days, could you help me
out at least in part, please?

The program is here: https://kempelen.dai.fmph.uniba.sk/test_cairo/test_cairo.c

An example screenshot (with a broken red line drawn by keys, and
rectangles not showing up properly):

To compile (on Ubuntu 16.04 or similar system):

gcc -o test_cairo test_cairo.c -I/usr/include/cairo -lX11 -lcairo

More information about the cairo mailing list