#include #include #include #include #include #include int WIDTH = 1920; int HEIGHT = 1080; void hex_color(XColor *color, Display *display, Colormap cm, const char *hexcode) { XParseColor(display, cm, hexcode, color); XAllocColor(display, cm, color); } void fill_color(Display *display, Window window, GC gc, Colormap cm, const char *hexcode) { XColor color; hex_color(&color, display, cm, hexcode); XSetForeground(display, gc, color.pixel); XFillRectangle(display, window, gc, 0, 0, WIDTH, HEIGHT); } void black(Display *display, Window window, GC gc, Colormap cm) { fill_color(display, window, gc, cm, "#000000"); } void white(Display *display, Window window, GC gc, Colormap cm) { fill_color(display, window, gc, cm, "#FFFFFF"); } int main(void) { Display *display; Window window; int screen; XEvent event; display = XOpenDisplay(NULL); if (display == NULL) { fprintf(stderr, "Cannot open display\n"); exit(1); } screen = DefaultScreen(display); window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, WIDTH, HEIGHT, 1, BlackPixel(display, screen), WhitePixel(display, screen)); XSelectInput(display, window, ExposureMask | KeyPressMask); XMapWindow(display, window); GC gc = XCreateGC(display, window, 0, NULL); Colormap cm = DefaultColormap(display, screen); XColor black; hex_color(&black, display, cm, "#000000"); XColor white; hex_color(&white, display, cm, "#FFFFFF"); while (1) { XNextEvent(display, &event); if (event.type == Expose) { // loop forever (until q) static int is_black = false; while (1) { // set color to black XSetForeground(display, gc, black.pixel); for (int i = 0; i < 50; ++i) { int x = (WIDTH / 2) + (i % 10) * 5; int y = (HEIGHT / 2) + (i / 10) * 5; XDrawRectangle(display, window, gc, x, y, 1, 1); } XFlush(display); sleep(2); while (XPending(display)) { XNextEvent(display, &event); if (event.type == KeyPress) { KeySym key = XLookupKeysym(&event.xkey, 0); if (key == XK_q || key == XK_Q) { printf("Stopping...\n"); exit(0); } } } } } } XFreeGC(display, gc); XDestroyWindow(display, window); XCloseDisplay(display); return 0; }