huge progress on ncomplib

This commit is contained in:
2026-03-29 19:50:16 +02:00
parent f722108806
commit 37cc73a21c
6 changed files with 1228 additions and 43 deletions

View File

@@ -3,6 +3,144 @@
#include "neonucleus.h"
#define NCL_EEPROM "ncl-eeprom"
#define NCL_FS "ncl-filesystem"
#define NCL_DRIVE "ncl-drive"
#define NCL_GPU "ncl-gpu"
#define NCL_SCREEN "ncl-screen"
#define NCL_MAX_VRAMBUF 128
// very low-level actions
// some environment have VFSes so
// we support wrapping those
typedef struct ncl_Stat {
// whether the entry is a directory
bool isDirectory;
// the logical size of the entry
// as in, for files it is how many bytes are in there.
// For directories, it should be 0.
// Every entry has a base cost, and thus fear not,
// it will not lead to infinite disk usage.
// Instead, make their size representative of the
// size on disk / number of entries.
size_t size;
// the real size.
// This is for realSpaceUsed, and is a safety mechanism
// against disk-hogging.
size_t diskSize;
// The UNIX timestamp of the last modified date
// of the entry.
size_t lastModified;
} ncl_Stat;
typedef enum ncl_VFSAction {
NCL_VFS_OPEN,
NCL_VFS_CLOSE,
NCL_VFS_READ,
NCL_VFS_SEEK,
NCL_VFS_WRITE,
NCL_VFS_OPENDIR,
NCL_VFS_CLOSEDIR,
NCL_VFS_READDIR,
// non-recursively remove entry
NCL_VFS_REMOVE,
NCL_VFS_STAT,
} ncl_VFSAction;
typedef struct ncl_VFSRequest {
void *state;
ncl_VFSAction action;
union {
struct {
const char *path;
// same r, w and a modes as regular filesystem component
const char *mode;
void *file;
} open;
struct {
// set to NULL for EoF
char *buf;
size_t len;
void *file;
} read;
struct {
const char *buf;
size_t len;
void *file;
} write;
struct {
nn_FSWhence whence;
int off;
void *file;
} seek;
void *close;
struct {
const char *path;
void *dir;
} opendir;
struct {
// set to NULL for EoF
// buffer size is NN_MAX_PATH.
// Remember to account for terminator
char *name;
void *dir;
} readdir;
void *closedir;
const char *remove;
struct {
// set to NULL if missing
const char *path;
ncl_Stat *stat;
} stat;
struct {
// path of directory that / represents
const char *path;
// get the estimated amount of space
// used up by an empty entry.
// Used for enforcing capacity.
size_t size;
} entrysize;
};
} ncl_VFSRequest;
typedef struct ncl_VFS {
// the internal state
void *state;
// the handler.
// True on success, false on failure.
bool (*handler)(ncl_VFSRequest *request);
// the path separator
char pathsep;
// the assumed cost of a file in spaceUsedIn.
size_t fileCost;
} ncl_VFS;
extern ncl_VFS ncl_defaultFS;
void *ncl_openfile(ncl_VFS vfs, const char *path, const char *mode);
void ncl_closefile(ncl_VFS vfs, void *file);
// returns false on EoF
bool ncl_readfile(ncl_VFS vfs, void *file, char *buf, size_t *len);
bool ncl_writefile(ncl_VFS vfs, void *file, const char *data, size_t len);
bool ncl_seekfile(ncl_VFS vfs, void *file, nn_FSWhence whence, int *off);
bool ncl_stat(ncl_VFS vfs, const char *path, ncl_Stat *stat);
void *ncl_opendir(ncl_VFS vfs, const char *path);
void ncl_closedir(ncl_VFS vfs, void *dir);
// returns false on EoF
bool ncl_readdir(ncl_VFS vfs, void *dir, char name[NN_MAX_PATH]);
size_t ncl_spaceUsedIn(ncl_VFS vfs, const char *path);
// gets the real space used
size_t ncl_spaceUsedBy(ncl_VFS vfs, const char *path);
bool ncl_remove(ncl_VFS vfs, const char *path);
bool ncl_removeRecursive(ncl_VFS vfs, const char *path);
typedef struct ncl_EncodedState {
char *buf;
size_t len;
@@ -12,11 +150,17 @@ nn_Exit ncl_encodeComponentState(nn_Universe *universe, nn_Component *comp, ncl_
void ncl_freeEncodedState(nn_Universe *universe, ncl_EncodedState *state);
nn_Exit ncl_loadComponentState(nn_Component *comp, const ncl_EncodedState *state);
nn_Component *ncl_createFilesystem(nn_Universe *universe, const char *address, const char *path, const nn_Filesystem *fs);
nn_Component *ncl_createDrive(nn_Universe *universe, const char *address, const char *path, const nn_Drive *drive);
nn_Component *ncl_createEEPROM(nn_Universe *universe, const char *address, const char *codepath, const char *datapath);
nn_Component *ncl_createScreen(nn_Universe *universe, const char *address, const nn_ScreenConfig *config);
nn_Component *ncl_createGPU(nn_Universe *universe, const char *address, const nn_GPU *gpu);
size_t ncl_getLabel(nn_Component *c, char buf[NN_MAX_LABEL]);
size_t ncl_setLabel(nn_Component *c, const char *label, size_t len);
nn_Component *ncl_createFilesystem(nn_Universe *universe, const char *address, const char *path, const nn_Filesystem *fs, bool isReadonly);
nn_Component *ncl_createDrive(nn_Universe *universe, const char *address, const char *path, const nn_Drive *drive, bool isReadonly);
nn_Component *ncl_createEEPROM(nn_Universe *universe, const char *address, const char *codepath, const char *datapath, bool isReadonly);
// Sets the VFS bound to a filesystem, drive or eeprom.
// This determines the filesystem the operations are run in.
// Returns the old VFS.
ncl_VFS ncl_setVFS(nn_Component *component, ncl_VFS vfs);
// TODO, stuff we could implement:
// redstone, hologram, oled, ipu, vt, led, tape_drive, cd_drive, serial, colorful_lamp
@@ -32,8 +176,71 @@ typedef struct ncl_Pixel {
typedef struct ncl_ScreenState ncl_ScreenState;
typedef enum ncl_ScreenFlags {
NCL_SCREEN_ON = 1<<0,
NCL_SCREEN_PRECISE = 1<<1,
NCL_SCREEN_TOUCHINVERTED = 1<<2,
} ncl_ScreenFlags;
nn_Component *ncl_createScreen(nn_Universe *universe, const char *address, const nn_ScreenConfig *config);
nn_Component *ncl_createGPU(nn_Universe *universe, const char *address, const nn_GPU *gpu);
typedef struct ncl_ComponentStat {
// common ones
char label[NN_MAX_LABEL];
size_t labellen;
// used for indicating usage. If higher than last time, something happened.
// This can be used to show a light or play a sound.
size_t usageCounter;
bool isReadonly;
// specific properties
union {
struct {
const nn_EEPROM *conf;
size_t codeUsed;
size_t dataUsed;
const char *codepath;
const char *datapath;
} eeprom;
struct {
const nn_Filesystem *conf;
size_t spaceUsed;
size_t realDiskUsage;
size_t filesOpen;
const char *path;
} fs;
struct {
const nn_Drive *conf;
size_t lastSector;
const char *path;
} drive;
struct {
const nn_GPU *conf;
size_t vramFree;
size_t bufferCount;
// can be NULL if there is none
const char *boundScreen;
} gpu;
struct {
const nn_ScreenConfig *conf;
ncl_ScreenState *state;
ncl_ScreenFlags flags;
int viewportWidth;
int viewportHeight;
} screen;
};
} ncl_ComponentStat;
void ncl_statComponent(nn_Component *component, ncl_ComponentStat *stat);
// For EEPROMs, filesystems, drives
// Returns whether it was successful or not.
bool ncl_makeReadonly(nn_Component *component);
void ncl_resetScreen(ncl_ScreenState *state);
void ncl_getScreenResolution(const ncl_ScreenState *state, size_t *width, size_t *height);
void ncl_getScreenViewport(const ncl_ScreenState *state, size_t *width, size_t *height);
ncl_Pixel ncl_getScreenPixel(const ncl_ScreenState *state, int x, int y);
ncl_ScreenFlags ncl_getScreenFlags(const ncl_ScreenState *state);
void ncl_setScreenFlags(ncl_ScreenState *state, ncl_ScreenFlags flags);
#endif