diff --git a/x11/Makefile b/x11/Makefile new file mode 100644 index 0000000..e3827bd --- /dev/null +++ b/x11/Makefile @@ -0,0 +1,18 @@ +CC = gcc +CFLAGS = -Wall -g +LDFLAGS = -lX11 +TARGET = a.out +SRC = *.c + +all: $(TARGET) + +$(TARGET): $(SRC) + $(CC) $(CFLAGS) -o $(TARGET) $(SRC) $(LDFLAGS) + +clean: + rm -f $(TARGET) + +run: $(TARGET) + $(MAKE) clean + $(MAKE) all + ./$(TARGET) \ No newline at end of file diff --git a/x11/xHandler.c b/x11/xHandler.c new file mode 100644 index 0000000..330e4e7 --- /dev/null +++ b/x11/xHandler.c @@ -0,0 +1,94 @@ +#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 fill_black(Display *display, Window window, GC gc, Colormap cm) { + fill_color(display, window, gc, cm, "#000000"); +} + +void fill_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; +} \ No newline at end of file