basic x bullshittery

This commit is contained in:
2025-07-03 05:55:19 +02:00
parent f7d8292029
commit cbfff3f6f7
2 changed files with 112 additions and 0 deletions

18
x11/Makefile Normal file
View File

@ -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)

94
x11/xHandler.c Normal file
View File

@ -0,0 +1,94 @@
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/keysym.h>
#include <stdbool.h>
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;
}