some progress

This commit is contained in:
veclavtlica
2026-10-07 11:21:21 +03:00
parent 14062e9d00
commit 8d502173a2
26 changed files with 705 additions and 212 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef PLR_DISPLAY_H
#define PLR_DISPLAY_H
extern int plr_display_x11_main(int argc, char *argv[]);
__attribute__((used)) extern int plr_display_x11_main(int argc, char *argv[]);
extern int plr_display_dos_main(int argc, char *argv[]);
#endif
+119 -10
View File
@@ -1,22 +1,79 @@
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "../../Common/bin.h"
#include "../../Common/buf.h"
#include "../../Common/cfg.h"
#include "../../Common/lzw.h"
#include "../../Common/mat.h"
#include "../../Common/std.h"
#include "../../Common/str.h"
#include "../draw.h"
#include "display.h"
/* https://hereket.com/posts/from-scratch-x11-windowing/ */
/* https://gaultier.github.io/blog/write_a_video_game_from_scratch_like_1987.html
*/
bool quited = false;
/* TODO: Have a cfg.h configuration header setting those. */
#define WIDTH 640
#define HEIGHT 480
bool fileread(void *ctx, char *into, uint32_t bytes) {
return com_std_syscall(0, (intptr_t)ctx, (intptr_t)into, bytes);
}
bool fileskip(void *ctx, uint32_t bytes) {
return com_std_syscall(8, (intptr_t)ctx, bytes, 1) >= 0;
}
static struct plr_x11_auth_entry {
uint16_t family;
struct com_buf *auth_name;
struct com_buf *auth_data;
bool success;
} plr_x11_read_auth_entry(struct com_bin *b, struct com_buf *buf) {
struct plr_x11_auth_entry result = {0};
if (!com_bin_read_u16(b, &result.family)) {
com_std_printf("Failure to initialize X11\n");
goto BREAK;
}
uint16_t addres_len;
if (!com_bin_read_u16(b, &addres_len))
goto BREAK;
if (!com_bin_skip(b, addres_len))
goto BREAK;
uint16_t display_len;
if (!com_bin_read_u16(b, &display_len))
goto BREAK;
if (!com_bin_skip(b, display_len))
goto BREAK;
uint16_t name_len;
if (!com_bin_read_u16(b, &name_len))
goto BREAK;
uint32_t name_idx;
if (!com_bin_read_bufbi(b, buf, &name_idx, name_len))
goto BREAK;
uint16_t data_len;
if (!com_bin_read_u16(b, &data_len))
goto BREAK;
uint32_t data_idx;
if (!com_bin_read_bufbi(b, buf, &data_idx, name_len))
goto BREAK;
result.auth_name = com_buf_index(struct com_buf, buf, name_idx);
result.auth_data = com_buf_index(struct com_buf, buf, data_idx);
result.success = true;
return result;
BREAK:
com_std_printf("Failure to initialize X11\n");
return result;
}
// TODO: correct error handling.
extern int plr_display_x11_main(int argc, char *argv[]) {
@@ -43,9 +100,61 @@ extern int plr_display_x11_main(int argc, char *argv[]) {
// com_vec_t v0 = com_mat_vec_project(com_mat_reoder(m0), com_vec_identity());
// com_fix_print(v0.a[0]);
com_fix_run_tests();
com_fix_run_bench();
com_mat_run_bench();
// com_fix_run_tests();
// com_fix_run_bench();
// com_mat_run_bench();
/* TODO: Move init to separate func. */
/* Open X11 socket for communication. */
int const xfd = com_std_syscall(41, AF_UNIX, SOCK_STREAM, 0);
if (xfd < 0) {
com_std_printf("Failure to initialize X11\n");
return -1;
}
struct sockaddr_un const addr = {.sun_family = AF_UNIX,
.sun_path = "/tmp/.X11-unix/X0"};
if (com_std_syscall(42, xfd, (intptr_t)&addr, sizeof addr)) {
com_std_printf("Failure to initialize X11\n");
return -1;
}
/* First we need to authenticate X11 connection, which requires some funk. */
/* We assume that $XAUTHORITY is set and I hope it is true. */
char *xauthpath = NULL;
for (char **envp = &argv[argc + 1]; *envp != NULL; ++envp) {
static const struct com_str xauthlit = com_str_lit("XAUTHORITY=");
if (com_str_ext_prefixed(*envp, xauthlit)) {
xauthpath = &((*envp)[xauthlit.len]);
break;
}
}
/* Open it and parse. */
int const xauthfd = com_std_syscall(2, (intptr_t)xauthpath, O_RDONLY, 0);
if (xauthfd < 0) {
com_std_printf("Failure to initialize X11\n");
return -1;
}
com_buf_make_on_stack(char, xauthbuf, 1024, 0);
struct com_bin xauthbin = {
.ctx = (void *)(int64_t)xauthfd,
.flags = COM_BIN_BIG_ENDIAN,
.interface = {.readn = fileread, .skipn = fileskip}};
struct plr_x11_auth_entry auth_entry =
plr_x11_read_auth_entry(&xauthbin, xauthbuf);
com_std_print_str(auth_entry.auth_name->data, auth_entry.auth_name->len,
true);
com_std_print_str(auth_entry.auth_data->data, auth_entry.auth_data->len,
true);
com_buf_free(xauthbuf);
char sendbuf[16 * 1024] = {};
char readbuf[16 * 1024] = {};
// Display *display = XOpenDisplay(NULL);
// if (NULL == display) {
@@ -90,13 +199,13 @@ extern int plr_display_x11_main(int argc, char *argv[]) {
// GC gc = XCreateGC(display, window, 0, NULL);
// Atom wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False);
// XSetWMProtocols(display, window, &wm_delete_window, 1);
// 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];
// // 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);