281 lines
7.9 KiB
C
281 lines
7.9 KiB
C
#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;
|
|
|
|
bool fileread(void *ctx, char *into, uint32_t bytes) {
|
|
return com_std_read(ctx, into, bytes) == bytes;
|
|
}
|
|
|
|
bool fileskip(void *ctx, uint32_t bytes) { return com_std_sfwd(ctx, bytes); }
|
|
|
|
#define AUTH_ENTRY_MAGIC_COOKIE "MIT-MAGIC-COOKIE-1"
|
|
|
|
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))
|
|
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, data_len))
|
|
goto BREAK;
|
|
|
|
result.auth_name = com_buf_index_as(struct com_buf, buf, name_idx);
|
|
result.auth_data = com_buf_index_as(struct com_buf, buf, data_idx);
|
|
result.success = true;
|
|
return result;
|
|
BREAK:
|
|
com_std_printf("Failure to initialize X11\n");
|
|
return result;
|
|
}
|
|
|
|
static bool plr_x11_handshake(struct plr_x11_auth_entry auth_entry) {
|
|
struct {
|
|
uint8_t endianness;
|
|
uint8_t pad0;
|
|
uint16_t major_version;
|
|
uint16_t minor_version;
|
|
uint16_t auth_len;
|
|
uint16_t auth_data_len;
|
|
uint16_t pad1;
|
|
} __attribute__((packed)) request = {
|
|
.endianness = 'l',
|
|
.major_version = 11,
|
|
.auth_len = sizeof(AUTH_ENTRY_MAGIC_COOKIE) - 1, // IS -1 NEEDED?
|
|
.auth_data_len = auth_entry.auth_data->len,
|
|
};
|
|
return true;
|
|
}
|
|
|
|
// TODO: correct error handling.
|
|
extern int plr_display_x11_main(int argc, char *argv[]) {
|
|
char test_string[] = "what is this?";
|
|
char *compressed_string = NULL;
|
|
uint32_t compressed_string_sz = 0;
|
|
struct com_lzw_table table =
|
|
com_lzw_infer_table(test_string, sizeof(test_string));
|
|
bool test = com_lzw_compress(&table, test_string, sizeof(test_string),
|
|
&compressed_string, &compressed_string_sz);
|
|
com_lzw_free_table(&table);
|
|
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};
|
|
for (int i = 0; i < 16; ++i)
|
|
m1.a[i] *= COM_FIX_FRACUNIT;
|
|
|
|
com_mat_t t0 = com_mat_mul(m1, m0);
|
|
com_mat_t t1 = com_mat_mul_reodered(com_mat_reoder(m1), m0);
|
|
com_fix_print(t0.a[4]);
|
|
com_fix_print(t1.a[4]);
|
|
|
|
// 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();
|
|
|
|
/* TODO: Move init to separate func. */
|
|
/* Open X11 socket for communication. */
|
|
com_std_fd const xfd = com_std_socket(false);
|
|
if (!xfd) {
|
|
com_std_printf("Failure to initialize X11\n");
|
|
return -1;
|
|
}
|
|
|
|
if (!com_std_connect(xfd, false, "/tmp/.X11-unix/X0")) {
|
|
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. */
|
|
com_std_fd const xauthfd = com_std_open(xauthpath, COM_STD_OPEN_READ);
|
|
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}};
|
|
|
|
/* Loop over auth entries until we find magic cookie one. */
|
|
struct plr_x11_auth_entry auth_entry;
|
|
do {
|
|
com_buf_clear(xauthbuf);
|
|
auth_entry = plr_x11_read_auth_entry(&xauthbin, xauthbuf);
|
|
} while (auth_entry.success && auth_entry.family != 256);
|
|
if (!auth_entry.success) {
|
|
com_std_printf("Failure to initialize X11\n");
|
|
return -1;
|
|
}
|
|
|
|
com_std_close(xauthfd);
|
|
|
|
/* TODO: Check for auth_entry.name == MAGIC ? */
|
|
plr_x11_handshake(auth_entry);
|
|
|
|
com_buf_free(xauthbuf);
|
|
|
|
char sendbuf[16 * 1024] = {};
|
|
char readbuf[16 * 1024] = {};
|
|
|
|
// 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) {
|
|
}
|
|
|
|
return 0;
|
|
}
|