Skip to content

Instantly share code, notes, and snippets.

@tkil
Created October 22, 2012 03:34
Show Gist options
  • Save tkil/3929496 to your computer and use it in GitHub Desktop.
Save tkil/3929496 to your computer and use it in GitHub Desktop.
Xlib seems to require a delay after GC creation.
/* compile line: gcc -o test xlib-delay-required.c -lX11 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/Xlib.h>
int
main(int argc, char* argv[])
{
char * dpy_name;
int delay_ms;
Display * dpy;
int scr;
Window root;
unsigned long black, white;
int x, y, w, h;
Window win;
GC gc;
struct timespec ts;
dpy_name = getenv( "DISPLAY" );
if ( ! dpy_name )
{
fprintf( stderr, "%s: no DISPLAY set\n", argv[0] );
return 1;
}
delay_ms = ( argc > 1 ) ? atoi( argv[1] ) : 10;
XInitThreads();
dpy = XOpenDisplay( dpy_name );
if ( ! dpy )
{
fprintf( stderr, "%s: cannot connect to X server '%s'\n",
argv[0], dpy_name );
return 2;
}
scr = DefaultScreen( dpy );
root = RootWindow( dpy, scr );
black = BlackPixel( dpy, scr );
white = WhitePixel( dpy, scr );
x = 0;
y = 0;
h = 300;
w = 300;
win = XCreateSimpleWindow( dpy, root,
x, y, w, h,
0, /* border */
black, /* foreground */
white /* background */ );
XMapWindow(dpy, win);
gc = XCreateGC( dpy, win, 0, 0 );
if ( ! gc ) {
fprintf( stderr, "%s: XCreateGC\n", argv[0] );
return 3;
}
XSetForeground( dpy, gc, black );
XSetBackground( dpy, gc, white );
XSetLineAttributes( dpy, gc,
2, /* line width */
LineSolid,
CapButt,
JoinBevel );
XSetFillStyle( dpy, gc, FillSolid );
XSync( dpy, False );
ts.tv_sec = 0;
ts.tv_nsec = delay_ms * 1000 * 1000;
nanosleep(&ts, 0);
XDrawPoint( dpy, win, gc, 5, 5);
XDrawPoint( dpy, win, gc, 5, h-5);
XDrawPoint( dpy, win, gc, w-5, 5);
XDrawPoint( dpy, win, gc, w-5, h-5);
XDrawLine( dpy, win, gc, 50, 0, 50, 200 );
XDrawLine( dpy, win, gc, 0, 100, 200, 100 );
XDrawArc( dpy, win, gc, 50-(30/2), 100-(30/2), 30, 30, 0, 360*64 );
{
XPoint points[] = {
{0, 0},
{15, 15},
{0, 15},
{0, 0}
};
int npoints = sizeof(points)/sizeof(XPoint);
XDrawLines( dpy, win, gc, points, npoints, CoordModeOrigin );
}
XDrawRectangle( dpy, win, gc, 120, 150, 50, 60 );
XFillRectangle( dpy, win, gc, 60, 150, 50, 60 );
XFlush( dpy );
XSync( dpy, False );
sleep( 4 );
XCloseDisplay( dpy );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment