Gears-C/src/platform_linux.h
2025-03-02 17:09:19 +11:00

151 lines
2.9 KiB
C

#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <xcb/xcb.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <assert.h>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <dlfcn.h>
#define Assert(condition, message) do { assert(condition && message); } while(0)
// generic defines
#define KB(n) n * 1024LL
#define MB(n) KB(n) * 1024LL
#define GB(n) MB(n) * 1024LL
#define TB(n) GB(n) * 1024LL
// syscall defines
#define SYS_ERR -1
// syscall write defines
#define _STDIN 0
#define _STDOUT 1
#define _STDERR 2
// xcb defines
#define XCB_CHECK_CURRENT_ERROR(window, error, message) do { \
error = xcb_request_check(window->connection, cookie); \
if (error != NULL) { \
EPrintf("%s ERROR CODE: %d\n", message, error->error_code); \
free(error); \
return false; \
} \
} while (0)
#define XCB_CHECK_ERROR(window, cookie, error, message) do { \
error = xcb_request_check(window->connection, cookie); \
XCB_CHECK_CURRENT_ERROR(window, error, message); \
} while (0)
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef intptr_t intptr;
typedef uintptr_t uintptr;
typedef ssize_t isize;
typedef size_t usize;
typedef float f32;
typedef double f64;
typedef uint8_t b8;
typedef uint32_t b32;
typedef void * rawptr;
typedef struct {
xcb_connection_t *connection;
xcb_window_t window;
xcb_atom_t close_event;
xcb_atom_t minimize_event;
u16 w, h;
} Window;
typedef struct {
void *lib;
} Library;
typedef struct {
void *fn;
} Function;
// X11
typedef struct
{
} XConnectRequest;
typedef struct {
u8 opcode;
u16 len; // length in units of 4 bytes
u8 data;
// Extra data
} XRequest;
typedef struct {
u32 len;
} XReply;
typedef struct {
} XError;
typedef struct {
} XEvent;
typedef u32 XWindow;
typedef u32 XAtom;
typedef u32 XVisID;
// Platform API
// Init Functions
b32 _LoadLib(const char *name, Library *out_lib);
b32 _LoadFn(const char *name, Library *lib, Function *out_fn);
b32 _InitPlatform();
// Memory Functions
rawptr _MemAlloc(isize size);
rawptr _MemAllocZeroed(isize size);
isize _GetPageSize();
// Print Functions
i32 _EPrint(void const *str);
i32 _Printf(const char *fmt, va_list arg);
i32 _Printfln(const char *fmt, va_list arg);
i32 _EPrintf(const char *fmt, va_list arg);
// Window Functions
b32 _CreateWindow();
Window *_GetWindow();
b32 _GetWindowEvent(WindowEvent *event);
void _WaitForWindowEvent(WindowEvent *event);
WindowSize _GetWindowSize();
b32 HandleWindowEvent(WindowEvent *event, b32 wait_for_event);
// Platform API END
// General Utils
b32 CheckSyscallErr(void *ptr);
// Write Utils
i32 Write(int fd, void const *str, isize count);