no libc, start on socket x11

This commit is contained in:
veclavtlica
2026-10-04 11:13:07 +03:00
parent 1125073568
commit 14062e9d00
21 changed files with 516 additions and 222 deletions
+101 -95
View File
@@ -1,19 +1,20 @@
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "../../Common/lzw.h"
#include "../../Common/mat.h"
#include "../../Common/std.h"
#include "../draw.h"
#include "display.h"
/* https://hereket.com/posts/from-scratch-x11-windowing/ */
bool quited = false;
/* TODO: Have a cfg.h configuration header setting those. */
#define WIDTH 640
#define HEIGHT 480
@@ -27,7 +28,7 @@ extern int plr_display_x11_main(int argc, char *argv[]) {
bool test = com_lzw_compress(&table, test_string, sizeof(test_string),
&compressed_string, &compressed_string_sz);
com_lzw_free_table(&table);
assert(test);
com_std_assert(test);
com_mat_t m0 = com_mat_identity();
com_mat_t m1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
@@ -46,98 +47,103 @@ extern int plr_display_x11_main(int argc, char *argv[]) {
com_fix_run_bench();
com_mat_run_bench();
Display *display = XOpenDisplay(NULL);
if (NULL == display) {
fprintf(stderr, "Failed to initialize display");
return EXIT_FAILURE;
// Display *display = XOpenDisplay(NULL);
// if (NULL == display) {
// com_std_printf("Failed to initialize display");
// return -1;
// }
// Window root = DefaultRootWindow(display);
// if (None == root) {
// com_std_printf("No root window found");
// XCloseDisplay(display);
// return -1;
// }
// int screen = DefaultScreen(display);
// Visual *visual = DefaultVisual(display, screen);
// int depth = DefaultDepth(display, screen);
// Window window =
// XCreateSimpleWindow(display, root, 0, 0, WIDTH, HEIGHT, 0, 0,
// 0xffffffff);
// if (None == window) {
// com_std_printf("Failed to create window");
// XCloseDisplay(display);
// return -1;
// }
// XSizeHints *hints = XAllocSizeHints();
// if (hints == NULL)
// return -1;
// // Pinning min and max to the same values disables resizing
// hints->flags = PMinSize | PMaxSize;
// hints->min_width = hints->max_width = WIDTH;
// hints->min_height = hints->max_height = HEIGHT;
// XSetWMNormalHints(display, window, hints);
// XFree(hints);
// XSelectInput(display, window, ExposureMask | KeyPressMask);
// XMapWindow(display, window);
// GC gc = XCreateGC(display, window, 0, NULL);
// Atom wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False);
// XSetWMProtocols(display, window, &wm_delete_window, 1);
// // TODO: can't be not true
// int bytes_per_pixel = 4;
// // char *pixel_buffer = (char *)malloc(WIDTH * HEIGHT * bytes_per_pixel);
// char pixel_buffer[WIDTH * HEIGHT * bytes_per_pixel];
// XImage *ximage = XCreateImage(display, visual, depth, ZPixmap, 0,
// pixel_buffer, WIDTH, HEIGHT, 32, 0);
// if (!ximage) {
// com_std_printf("Failed to create XImage\n");
// // com_std_alloc(pixel_buffer, 0);
// XCloseDisplay(display);
// return -1;
// }
// int frame = 0;
// XEvent event;
// while (!quited) {
// XNextEvent(display, &event);
// switch (event.type) {
// case ClientMessage:
// if (event.xclient.data.l[0] == wm_delete_window) {
// XDestroyWindow(display, window);
// quited = true;
// }
// break;
// case Expose:
// frame += 2;
// plr_draw_screen((uint8_t *)pixel_buffer);
// // Draw the complete image onto the window when exposed
// XPutImage(display,
// window, // Target drawable
// gc, // Graphics Context
// ximage, // Source XImage
// 0, 0, // Source coordinates (x, y)
// 0, 0, // Destination coordinates (x, y)
// WIDTH, HEIGHT // Dimensions to copy
// );
// break;
// }
// }
// XCloseDisplay(display);
while (1) {
}
Window root = DefaultRootWindow(display);
if (None == root) {
fprintf(stderr, "No root window found");
XCloseDisplay(display);
return EXIT_FAILURE;
}
int screen = DefaultScreen(display);
Visual *visual = DefaultVisual(display, screen);
int depth = DefaultDepth(display, screen);
Window window =
XCreateSimpleWindow(display, root, 0, 0, WIDTH, HEIGHT, 0, 0, 0xffffffff);
if (None == window) {
fprintf(stderr, "Failed to create window");
XCloseDisplay(display);
return EXIT_FAILURE;
}
XSizeHints *hints = XAllocSizeHints();
if (hints == NULL)
return EXIT_FAILURE;
// Pinning min and max to the same values disables resizing
hints->flags = PMinSize | PMaxSize;
hints->min_width = hints->max_width = WIDTH;
hints->min_height = hints->max_height = HEIGHT;
XSetWMNormalHints(display, window, hints);
XFree(hints);
XSelectInput(display, window, ExposureMask | KeyPressMask);
XMapWindow(display, window);
GC gc = XCreateGC(display, window, 0, NULL);
Atom wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &wm_delete_window, 1);
// TODO: can't be not true
int bytes_per_pixel = 4;
char *pixel_buffer = (char *)malloc(WIDTH * HEIGHT * bytes_per_pixel);
XImage *ximage = XCreateImage(display, visual, depth, ZPixmap, 0,
pixel_buffer, WIDTH, HEIGHT, 32, 0);
if (!ximage) {
fprintf(stderr, "Failed to create XImage\n");
free(pixel_buffer);
XCloseDisplay(display);
return EXIT_FAILURE;
}
int frame = 0;
XEvent event;
while (!quited) {
XNextEvent(display, &event);
switch (event.type) {
case ClientMessage:
if (event.xclient.data.l[0] == wm_delete_window) {
XDestroyWindow(display, window);
quited = true;
}
break;
case Expose:
frame += 2;
plr_draw_screen((uint8_t *)pixel_buffer);
// Draw the complete image onto the window when exposed
XPutImage(display,
window, // Target drawable
gc, // Graphics Context
ximage, // Source XImage
0, 0, // Source coordinates (x, y)
0, 0, // Destination coordinates (x, y)
WIDTH, HEIGHT // Dimensions to copy
);
break;
}
}
XCloseDisplay(display);
return 0;
}