From 853b0bac96431522d8060c6eb6166842cc6f9770 Mon Sep 17 00:00:00 2001 From: IonutParau Date: Sun, 8 Jun 2025 21:21:04 +0200 Subject: [PATCH] screen stuff --- src/components/filesystem.c | 28 +++++++++--------- src/components/screen.c | 57 +++++++++++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 17 deletions(-) diff --git a/src/components/filesystem.c b/src/components/filesystem.c index b7c5b08..139ac59 100644 --- a/src/components/filesystem.c +++ b/src/components/filesystem.c @@ -408,21 +408,21 @@ void nn_loadFilesystemTable(nn_universe *universe) { nn_defineMethod(fsTable, "getLabel", false, (void *)nn_fs_getLabel, NULL, "getLabel(): string - Returns the label of the filesystem."); nn_defineMethod(fsTable, "setLabel", false, (void *)nn_fs_setLabel, NULL, "setLabel(label: string): string - Sets a new label for the filesystem and returns the new label of the filesystem, which may have been truncated."); nn_defineMethod(fsTable, "spaceUsed", false, (void *)nn_fs_spaceUsed, NULL, "spaceUsed(): integer - Returns the amounts of bytes used."); - nn_defineMethod(fsTable, "spaceTotal", false, (void *)nn_fs_spaceTotal, NULL, "spaceTotal(): integer - Returns the capacity of the filesystem."); + nn_defineMethod(fsTable, "spaceTotal", true, (void *)nn_fs_spaceTotal, NULL, "spaceTotal(): integer - Returns the capacity of the filesystem."); nn_defineMethod(fsTable, "isReadOnly", true, (void *)nn_fs_isReadOnly, NULL, "isReadOnly(): boolean - Returns whether the filesystem is in read-only mode."); - nn_defineMethod(fsTable, "size", true, (void *)nn_fs_size, NULL, "size(path: string): integer - Gets the size, in bytes, of a file."); - nn_defineMethod(fsTable, "remove", true, (void *)nn_fs_remove, NULL, "remove(path: string): boolean - Removes a file. Returns whether the operation succeeded."); - nn_defineMethod(fsTable, "lastModified", true, (void *)nn_fs_lastModified, NULL, "remove(path: string): boolean - Removes a file. Returns whether the operation succeeded."); - nn_defineMethod(fsTable, "rename", true, (void *)nn_fs_rename, NULL, "rename(from: string, to: string): boolean - Moves files from one path to another."); - nn_defineMethod(fsTable, "exists", true, (void *)nn_fs_exists, NULL, "exists(path: string): boolean - Checks whether a file exists."); - nn_defineMethod(fsTable, "isDirectory", true, (void *)nn_fs_isDirectory, NULL, "isDirectory(path: string): boolean - Returns whether a file is actually a directory."); - nn_defineMethod(fsTable, "makeDirectory", true, (void *)nn_fs_makeDirectory, NULL, "makeDirectory(path: string): boolean - Creates a new directory at the given path. Returns whether it succeeded."); - nn_defineMethod(fsTable, "list", true, (void *)nn_fs_list, NULL, "list(path: string): string[] - Returns a list of file paths. Directories will have a / after them"); - nn_defineMethod(fsTable, "open", true, (void *)nn_fs_open, NULL, "open(path: string[, mode: string = \"r\"]): integer - Opens a file, may create it."); - nn_defineMethod(fsTable, "close", true, (void *)nn_fs_close, NULL, "close(fd: integer): boolean - Closes a file."); - nn_defineMethod(fsTable, "write", true, (void *)nn_fs_write, NULL, "write(fd: integer, data: string): boolean - Writes data to a file."); - nn_defineMethod(fsTable, "read", true, (void *)nn_fs_read, NULL, "read(fd: integer, len: number): string - Reads bytes from a file. Infinity is a valid length, in which case it reads as much as possible."); - nn_defineMethod(fsTable, "seek", true, (void *)nn_fs_seek, NULL, "seek(fd: integer, whence: string, offset: integer): integer - Seeks a file. Returns the new position. Valid whences are set, cur and end."); + nn_defineMethod(fsTable, "size", false, (void *)nn_fs_size, NULL, "size(path: string): integer - Gets the size, in bytes, of a file."); + nn_defineMethod(fsTable, "remove", false, (void *)nn_fs_remove, NULL, "remove(path: string): boolean - Removes a file. Returns whether the operation succeeded."); + nn_defineMethod(fsTable, "lastModified", false, (void *)nn_fs_lastModified, NULL, "remove(path: string): boolean - Removes a file. Returns whether the operation succeeded."); + nn_defineMethod(fsTable, "rename", false, (void *)nn_fs_rename, NULL, "rename(from: string, to: string): boolean - Moves files from one path to another."); + nn_defineMethod(fsTable, "exists", false, (void *)nn_fs_exists, NULL, "exists(path: string): boolean - Checks whether a file exists."); + nn_defineMethod(fsTable, "isDirectory", false, (void *)nn_fs_isDirectory, NULL, "isDirectory(path: string): boolean - Returns whether a file is actually a directory."); + nn_defineMethod(fsTable, "makeDirectory", false, (void *)nn_fs_makeDirectory, NULL, "makeDirectory(path: string): boolean - Creates a new directory at the given path. Returns whether it succeeded."); + nn_defineMethod(fsTable, "list", false, (void *)nn_fs_list, NULL, "list(path: string): string[] - Returns a list of file paths. Directories will have a / after them"); + nn_defineMethod(fsTable, "open", false, (void *)nn_fs_open, NULL, "open(path: string[, mode: string = \"r\"]): integer - Opens a file, may create it."); + nn_defineMethod(fsTable, "close", false, (void *)nn_fs_close, NULL, "close(fd: integer): boolean - Closes a file."); + nn_defineMethod(fsTable, "write", false, (void *)nn_fs_write, NULL, "write(fd: integer, data: string): boolean - Writes data to a file."); + nn_defineMethod(fsTable, "read", false, (void *)nn_fs_read, NULL, "read(fd: integer, len: number): string - Reads bytes from a file. Infinity is a valid length, in which case it reads as much as possible."); + nn_defineMethod(fsTable, "seek", false, (void *)nn_fs_seek, NULL, "seek(fd: integer, whence: string, offset: integer): integer - Seeks a file. Returns the new position. Valid whences are set, cur and end."); } nn_component *nn_addFileSystem(nn_computer *computer, nn_address address, int slot, nn_filesystem *filesystem) { diff --git a/src/components/screen.c b/src/components/screen.c index 1b10258..d42c46b 100644 --- a/src/components/screen.c +++ b/src/components/screen.c @@ -1,6 +1,32 @@ #include "screen.h" +#include -nn_screen *nn_newScreen(int maxWidth, int maxHeight, int maxDepth, int editableColors, int paletteColors); +nn_screen *nn_newScreen(int maxWidth, int maxHeight, int maxDepth, int editableColors, int paletteColors) { + nn_screen *screen = nn_malloc(sizeof(nn_screen)); + screen->buffer = nn_malloc(sizeof(nn_screenChar) * maxWidth * maxHeight); + screen->lock = nn_newGuard(); + screen->refc = 1; + screen->width = maxWidth; + screen->height = maxHeight; + screen->viewportWidth = maxWidth; + screen->viewportHeight = maxHeight; + screen->maxWidth = maxWidth; + screen->maxHeight = maxHeight; + screen->maxDepth = maxDepth; + screen->depth = maxDepth; + screen->editableColors = editableColors; + screen->paletteColors = paletteColors; + screen->palette = nn_malloc(sizeof(int) * screen->paletteColors); + memset(screen->palette, 0, sizeof(int) * screen->paletteColors); + screen->aspectRatioWidth = 1; + screen->aspectRatioHeight = 1; + screen->isOn = true; + screen->isTouchModeInverted = true; + screen->isPrecise = true; + screen->isDirty = true; + screen->keyboardCount = 0; + return screen; +} void nn_retainScreen(nn_screen *screen) { nn_incRef(&screen->refc); @@ -62,7 +88,18 @@ void nn_addKeyboard(nn_screen *screen, nn_address address) { screen->keyboards[screen->keyboardCount++] = nn_strdup(address); } -void nn_removeKeyboard(nn_screen *screen, nn_address address); +void nn_removeKeyboard(nn_screen *screen, nn_address address) { + size_t j = 0; + for(size_t i = 0; i < screen->keyboardCount; i++) { + if(strcmp(screen->keyboards[i], address) == 0) { + nn_free(screen->keyboards[i]); + } else { + screen->keyboards[j] = screen->keyboards[i]; + j++; + } + } + screen->keyboardCount = j; +} nn_address nn_getKeyboard(nn_screen *screen, size_t idx) { if(idx >= screen->keyboardCount) return NULL; @@ -148,5 +185,19 @@ void nn_setOn(nn_screen *buffer, bool on) { buffer->isOn = on; } -void nn_loadScreenTable(nn_universe *universe); +void nn_screenComp_destroy(void *_, nn_component *component, nn_screen *screen) { + nn_destroyScreen(screen); +} + +void nn_screenComp_getKeyboards(nn_screen *screen, void *_, nn_component *component, nn_computer *computer) { + nn_value arr = nn_values_array(nn_getKeyboardCount(screen)); +} + +void nn_loadScreenTable(nn_universe *universe) { + nn_componentTable *screenTable = nn_newComponentTable("screen", NULL, NULL, (void *)nn_screenComp_destroy); + nn_storeUserdata(universe, "NN:SCREEN", screenTable); + + nn_defineMethod(screenTable, "getKeyboards", false, (void *)nn_screenComp_getKeyboards, NULL, "getKeyboards(): string[] - Returns the keyboards registered to this screen."); +} + nn_component *nn_addScreen(nn_computer *computer, nn_address address, int slot, nn_screen *screen);