This example illustrates how you can use the X11 XSync extension (not to be confused with XSync()) to achieve smooth interactive resizing on the majority of X environments, assuming your program can redraw fast enough.
DISCLAIMER: This example shows only the basic usage of XSync using a single counter, which is the bare minimum required to support smooth resizing.
-
Indicate that your client is willing to receive sync requests:
- Add
_NET_WM_SYNC_REQUESTto your window'sWM_PROTOCOLS. - Create an XSync Counter and set the property
_NET_WM_SYNC_REQUEST_COUNTERto its ID.
- Add
-
Handle the
_NET_WM_SYNC_REQUESTclient message event:- You will get this event when a resize is issued, but before the resize is presented.
- When you receive this client message, hold on to the counter value contained within the message, and initiate a redraw.
-
Update the counter:
- After drawing in response to the resize, use the stored counter value from earlier to update the server's counter with
XSyncSetCounter(). This indicates that the resize is ready to be presented.
- After drawing in response to the resize, use the stored counter value from earlier to update the server's counter with
NOTE: If you're using OpenGL for graphics, resizing may be sluggish on some systems if you don't disable vsync: (egl/glx)SwapInterval(0)
- https://www.x.org/releases/X11R7.7/doc/xextproto/sync.html
- https://fishsoup.net/misc/wm-spec-synchronization.html
Below is the actual implementation. If this program does not work as expected, please bring it to my attention!
Compile with: cc -o smooth_resize smooth_resize.c -lX11 -lXext