As an example of using the X11/Xlib interface, let's create an X window and write some text on it.
void close_x(Display *display, Window win, GC gc) {
XFreeGC(display, gc);
XDestroyWindow(display, win);
XCloseDisplay(display);
exit(1);
}
int main(int argc, char *argv[]) {
Display *display;
int screen;
Window win;
GC gc;
unsigned long black, white;
XEvent event;
KeySym key;
char text[255];
display = XOpenDisplay((char *)0);
if(display == NULL) {
std::cout << "Error connecting to X server" << '\n';
exit(-1);
}
screen = DefaultScreen(display);
black = BlackPixel(display, screen);
white = WhitePixel(display, screen);
win = XCreateSimpleWindow(
display, XDefaultRootWindow(display), 0, 0, 1440, 720, 4, black, white
);
XMapWindow(display, win);
XSelectInput(display, win, KeyPressMask | ButtonPressMask | ExposureMask);
gc = XCreateGC(display, win, 0, 0);
XSetLineAttributes(display, gc, 1, LineSolid, CapRound, JoinRound);
while(1) {
XNextEvent(display, &event);
if(event.type == Expose && !event.xexpose.count) {
draw_glyphs(display, &win, &gc, text_to_display, 360, 360, 4);
}
if(event.type == KeyPress && XLookupString(&event.xkey, text, 255, &key, 0)) {
if(text[0] == 'q') {
close_x(display, win, gc);
}
}
}
return 0;
}
A Hershey vector font.
Definition font.hpp:19
A character string comprising Hershey vector font glyphs.
Definition string.hpp:20
Note that the majority of the code here is Xlib boilerplate for creating the window to draw on, as well as the colours, line style and width to draw with.
Compile, e.g.
Run, e.g.