diff --git a/Cargo.lock b/Cargo.lock index 78e2169..030efe3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,15 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "anstream" version = "0.6.18" @@ -173,6 +182,29 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "env_filter" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "186e05a59d4c50738528153b83b0b0194d3a29507dfec16eccd4b342903397d0" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c863f0904021b108aa8b2f55046443e6b1ebde8fd4a15c399893aae4fa069f" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "jiff", + "log", +] + [[package]] name = "equivalent" version = "1.0.2" @@ -257,6 +289,30 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" +[[package]] +name = "jiff" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f02000660d30638906021176af16b17498bd0d12813dbfe7b276d8bc7f3c0806" +dependencies = [ + "jiff-static", + "log", + "portable-atomic", + "portable-atomic-util", + "serde", +] + +[[package]] +name = "jiff-static" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3c30758ddd7188629c6713fc45d1188af4f44c90582311d0c8d8c9907f60c48" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "jobserver" version = "0.1.33" @@ -312,6 +368,21 @@ version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" +[[package]] +name = "portable-atomic" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e" + +[[package]] +name = "portable-atomic-util" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8a2f0d8d040d7848a709caf78912debcc3f33ee4b3cac47d73d1e1069e83507" +dependencies = [ + "portable-atomic", +] + [[package]] name = "proc-macro2" version = "1.0.95" @@ -342,6 +413,35 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + [[package]] name = "rust-lzma" version = "0.6.0" @@ -415,8 +515,6 @@ dependencies = [ [[package]] name = "servicepoint" version = "0.14.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6bd5cfa49c73aeecb344680ffbf697abf73e0563a441b93b9723ae43867500f" dependencies = [ "bitvec", "bzip2", @@ -433,6 +531,7 @@ name = "servicepoint_binding_c" version = "0.14.1" dependencies = [ "cbindgen", + "env_logger", "servicepoint", ] diff --git a/Cargo.toml b/Cargo.toml index 0eba069..4cbe337 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,13 +18,17 @@ crate-type = ["staticlib", "cdylib", "rlib"] cbindgen = "0.28.0" [dependencies.servicepoint] -package = "servicepoint" version = "0.14.1" default-features = false +[dependencies.env_logger] +version = "0.11.8" +optional = true + [features] all_compressions = ["servicepoint/all_compressions"] -default = ["all_compressions", "servicepoint/default"] +default = ["all_compressions", "servicepoint/default", "env_logger"] +env_logger = ["dep:env_logger"] [lints.rust] missing-docs = "warn" diff --git a/cbindgen.toml b/cbindgen.toml index 7841755..6edd33f 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -16,16 +16,18 @@ line_endings = "LF" ############################# Codegen Options ################################## -style = "type" +style = "both" usize_is_size_t = true # this is needed because otherwise the order in the C bindings is different on different machines sort_by = "Name" +include_guard = "SERVICEPOINT_BINDINGS_C" + [parse] parse_deps = true include = ["servicepoint", "std"] -extra_bindings = ["servicepoint"] +extra_bindings = ["servicepoint", "servicepoint_binding_c"] [parse.expand] features = ["full"] diff --git a/example/Makefile b/example/Makefile index 32e6091..e041b08 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,7 +1,7 @@ CARGO ?= cargo STRIP ?= strip -FEATURES := "" +FEATURES := "env_logger" THIS_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) REPO_ROOT := $(realpath $(THIS_DIR)/..) @@ -10,7 +10,7 @@ export SERVICEPOINT_HEADER_OUT := $(REPO_ROOT)/include override CFG_MUSL := $(if $(CFG_MUSL),$(CFG_MUSL),$(if $(MUSL),$(MUSL),0)) override CFG_PROFILE := $(if $(CFG_PROFILE),$(CFG_PROFILE),$(if $(PROFILE),$(PROFILE),release)) -CCFLAGS += -Wall -fwhole-program -fPIE -pie +CCFLAGS += -Wall -Wextra -pedantic -fwhole-program -fPIE -pie STRIPFLAGS := -s --strip-unneeded -R .comment -R .gnu.version -R .note -R .note.gnu.build-id -R .note.ABI-tag @@ -99,7 +99,7 @@ clean-rust: .PHONY: all clean sizes $(_run_programs) clean-c clean-rust -$(_unstripped_bins): out/%_unstripped: src/%.c $(SERVICEPOINT_HEADER_OUT)/servicepoint.h $(_sp_artifacts) +$(_unstripped_bins): out/%_unstripped: src/%.c $(SERVICEPOINT_HEADER_OUT)/servicepoint.h src/helpers.h $(_sp_artifacts) mkdir -p out || true $(CC) $< \ -I $(SERVICEPOINT_HEADER_OUT) \ diff --git a/example/src/announce.c b/example/src/announce.c index d256cff..bfdc48f 100644 --- a/example/src/announce.c +++ b/example/src/announce.c @@ -1,16 +1,9 @@ -#include -#include "servicepoint.h" - +#include "helpers.h" int main(void) { - printf("test\n"); + sock_init(); - UdpSocket *connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); - //UdpSocket *connection = sp_udp_open_ipv4(127, 0, 0, 1, 2342); - if (connection == NULL) - return 1; - - sp_udp_send_header(connection, (Header) {.command_code = COMMAND_CODE_CLEAR}); + sp_udp_send_header(sock, (Header) {.command_code = COMMAND_CODE_CLEAR}); CharGrid *grid = sp_char_grid_new(5, 2); if (grid == NULL) @@ -30,8 +23,7 @@ int main(void) { Packet *packet = sp_char_grid_into_packet(grid, 0, 0); if (packet == NULL) return 1; - sp_udp_send_packet(connection, packet); + sp_udp_send_packet(sock, packet); - sp_udp_free(connection); return 0; } diff --git a/example/src/brightness_tester.c b/example/src/brightness_tester.c index 21d814d..51790cf 100644 --- a/example/src/brightness_tester.c +++ b/example/src/brightness_tester.c @@ -1,6 +1,5 @@ #include "servicepoint.h" - -static UdpSocket *connection = NULL; +#include "helpers.h" void enable_all_pixels(void) { Bitmap *all_on = sp_bitmap_new_max_sized(); @@ -9,26 +8,18 @@ void enable_all_pixels(void) { BitmapCommand *bitmapCommand = sp_cmd_bitmap_from_bitmap(all_on); Packet *packet = sp_cmd_bitmap_try_into_packet(bitmapCommand); if (packet != NULL) - sp_udp_send_packet(connection, packet); + sp_udp_send_packet(sock, packet); } void make_brightness_pattern(BrightnessGrid *grid) { ByteSlice slice = sp_brightness_grid_unsafe_data_ref(grid); for (size_t index = 0; index < slice.length; index++) { - slice.start[index] = (uint8_t) (index % ((size_t) Brightness_MAX)); + slice.start[index] = (uint8_t)(index % ((size_t) Brightness_MAX)); } } -void run_at_exit() { - sp_udp_free(connection); -} - int main(void) { - //UdpSocket *connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); - connection = sp_udp_open_ipv4(127, 0, 0, 1, 2342); - if (connection == NULL) - return -1; - atexit(run_at_exit); + sock_init(); enable_all_pixels(); @@ -37,8 +28,8 @@ int main(void) { Packet *packet = sp_cmd_brightness_grid_into_packet(sp_cmd_brightness_grid_from_grid(grid)); if (packet == NULL) - return -2; + return -2; - sp_udp_send_packet(connection, packet); + sp_udp_send_packet(sock, packet); return 0; } diff --git a/example/src/header_logger.c b/example/src/header_logger.c new file mode 100644 index 0000000..97cd3dd --- /dev/null +++ b/example/src/header_logger.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +#include +#include +#include "servicepoint.h" + +#define DEFAULT_LISTEN_IP "127.0.0.1" + +void handle_error(const char *msg) { + perror(msg); + exit(EXIT_FAILURE); +} + +int main(int argc, char **argv) { + init_env_logger(); + + int udp_socket = socket(AF_INET, SOCK_DGRAM, 0); + if (udp_socket == -1) + handle_error("socket could not be created\n"); + + char *listen_addr_arg; + if (argc > 1) { + listen_addr_arg = argv[1]; + } else { + listen_addr_arg = DEFAULT_LISTEN_IP; + } + + struct in_addr addr; + if (inet_aton(listen_addr_arg, &addr) == 0) + handle_error("listen ip could not be parsed\n"); + + struct sockaddr_in sockaddrIn = { + .sin_addr = addr, + .sin_family = AF_INET, + .sin_port = htons(2342), + }; + memset(sockaddrIn.sin_zero, 0, sizeof(sockaddrIn.sin_zero)); + + if (bind(udp_socket, (struct sockaddr *) &sockaddrIn, sizeof(sockaddrIn)) == -1) + handle_error("could not bind socket\n"); + + printf("socket ready to receive on %s\n", listen_addr_arg); + + uint8_t buffer[10000]; + bool done = false; + while (!done) { + memset(buffer, 0, sizeof(buffer)); + + ssize_t num_bytes = recv(udp_socket, (void *) buffer, sizeof(buffer), 0); + if (num_bytes == -1) + handle_error("could not read from client"); + + Packet *packet = sp_packet_try_load((ByteSlice) { + .start = buffer, + .length = num_bytes, + }); + if (packet == NULL) { + printf("received invalid packet\n"); + continue; + } + + struct Header *header = sp_packet_get_header(packet); + ByteSlice payload = sp_packet_get_payload(packet); + printf("Received packet: cc=%d, a=%d, b=%d, c=%d, d=%d, payload=%p (len %zu)\n", + header->command_code, header->a, header->b, header->c, header->d, + payload.start, payload.length); + + done = header->command_code == COMMAND_CODE_HARD_RESET; + sp_packet_free(packet); + } + + exit(EXIT_SUCCESS); +} diff --git a/example/src/helpers.h b/example/src/helpers.h new file mode 100644 index 0000000..904e86b --- /dev/null +++ b/example/src/helpers.h @@ -0,0 +1,45 @@ +#pragma once +#ifndef SERVICEPOINT_BINDING_C_MSLEEP_H +#define SERVICEPOINT_BINDING_C_MSLEEP_H + +#include +#include +#include +#include "servicepoint.h" + +static UdpSocket *sock = NULL; + +void sock_free() { + sp_udp_free(sock); +} + +void sock_init() { + //sock = sp_udp_open_ipv4(127, 0, 0, 1, 2342); + sock = sp_udp_open_ipv4(172, 23, 42, 29, 2342); + if (sock == NULL) + exit(-1); + atexit(sock_free); +} + +/// TODO: all of this for sleeping n ms? There should be a better way! +int msleep(long msec) { + int res; + + if (msec < 0) { + errno = EINVAL; + return -1; + } + + struct timespec ts = { + .tv_sec = msec / 1000, + .tv_nsec = (msec % 1000) * 1000000, + }; + + do { + res = nanosleep(&ts, &ts); + } while (res && errno == EINTR); + + return res; +} + +#endif //SERVICEPOINT_BINDING_C_MSLEEP_H diff --git a/example/src/moving_line.c b/example/src/moving_line.c new file mode 100644 index 0000000..35ba20d --- /dev/null +++ b/example/src/moving_line.c @@ -0,0 +1,34 @@ +#include "servicepoint.h" +#include "helpers.h" + +int main(void) { + sock_init(); + + int result = 0; + Bitmap *bitmap = sp_bitmap_new_max_sized(); + for (size_t x = 0; x < sp_bitmap_width(bitmap); x++) { + sp_bitmap_fill(bitmap, false); + + for (size_t y = 0; y < sp_bitmap_height(bitmap); y++) { + sp_bitmap_set(bitmap, (y + x) % PIXEL_WIDTH, y, true); + } + + BitmapCommand *command = sp_cmd_bitmap_from_bitmap(sp_bitmap_clone(bitmap)); + Packet *packet = sp_cmd_bitmap_try_into_packet(command); + if (packet == NULL) { + result = -2; + goto exit; + } + + if (!sp_udp_send_packet(sock, packet)) { + result = -3; + goto exit; + } + + msleep(SP_FRAME_PACING_MS); + } + + exit: + sp_bitmap_free(bitmap); + return result; +} diff --git a/example/src/random_stuff.c b/example/src/random_stuff.c index 2ebd485..2d818e3 100644 --- a/example/src/random_stuff.c +++ b/example/src/random_stuff.c @@ -1,10 +1,9 @@ #include #include "servicepoint.h" +#include "helpers.h" int main(void) { - UdpSocket *connection = sp_udp_open_ipv4(127, 0, 0, 1, 2342); - if (connection == NULL) - return 1; + sock_init(); Bitmap *pixels = sp_bitmap_new(PIXEL_WIDTH, PIXEL_HEIGHT); if (pixels == NULL) @@ -19,8 +18,6 @@ int main(void) { Header *header = sp_packet_get_header(packet); printf("[%d, %d, %d, %d, %d]\n", header->command_code, header->a, header->b, header->c, header->d); - sp_udp_send_packet(connection, packet); - - sp_udp_free(connection); + sp_udp_send_packet(sock, packet); return 0; } diff --git a/example/src/wiping_clear.c b/example/src/wiping_clear.c new file mode 100644 index 0000000..a3f7017 --- /dev/null +++ b/example/src/wiping_clear.c @@ -0,0 +1,34 @@ +#include "servicepoint.h" +#include "helpers.h" + +int main() { + sock_init(); + + Bitmap *enabled_pixels = sp_bitmap_new_max_sized(); + sp_bitmap_fill(enabled_pixels, true); + + int result = 0; + for (int x = 0; x < PIXEL_WIDTH; x++) { + for (int y = 0; y < PIXEL_HEIGHT; y++) { + sp_bitmap_set(enabled_pixels, x, y, false); + } + + BitVec *bitvec = sp_bitmap_into_bitvec(sp_bitmap_clone(enabled_pixels)); + BitVecCommand *command = sp_cmd_bitvec_new(bitvec, 0, BINARY_OPERATION_AND, COMPRESSION_CODE_LZMA); + Packet *packet = sp_cmd_bitvec_try_into_packet(command); + if (packet == NULL) { + result = -2; + goto exit; + } + if (!sp_udp_send_packet(sock, packet)) { + result = -3; + goto exit; + } + + msleep(SP_FRAME_PACING_MS); + } + + exit: + sp_bitmap_free(enabled_pixels); + return result; +} diff --git a/include/servicepoint.h b/include/servicepoint.h index d5b0199..dec3af2 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -1,3 +1,6 @@ +#ifndef SERVICEPOINT_BINDINGS_C +#define SERVICEPOINT_BINDINGS_C + /* Generated with cbindgen:0.28.0 */ /* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */ @@ -446,12 +449,19 @@ typedef struct ValueGrid_u8 ValueGrid_u8; * - accesses to the memory pointed to with `start` is never accessed outside `length` * - the lifetime of the `CByteSlice` does not outlive the memory it points to, as described in * the function returning this type. + * - if `start` is NULL or `length` is 0, do not dereference `start`. + * + * # Examples + * + * ```c + * ByteSlice empty = {.start: NULL, .length = 0}; + * ``` */ -typedef struct { +typedef struct ByteSlice { /** * The start address of the memory. */ - uint8_t */*notnull*/ start; + uint8_t *start; /** * The amount of memory in bytes. */ @@ -476,7 +486,7 @@ typedef struct { * }).unwrap() * ``` */ -typedef ValueGrid_Brightness BrightnessGrid; +typedef struct ValueGrid_Brightness BrightnessGrid; /** * A display brightness value, checked for correct value range @@ -522,7 +532,7 @@ typedef uint8_t Brightness; * connection.send_command(command).unwrap() * ``` */ -typedef ValueGrid_char CharGrid; +typedef struct ValueGrid_char CharGrid; /** * Type alias for documenting the meaning of the u16 in enum values @@ -534,23 +544,23 @@ typedef size_t Offset; * * The encoding is currently not enforced. */ -typedef ValueGrid_u8 Cp437Grid; +typedef struct ValueGrid_u8 Cp437Grid; /** * Pointer to one of the available command structs. */ -typedef union { +typedef union CommandUnion { uint8_t *null; - BitmapCommand */*notnull*/ bitmap; - BitVecCommand */*notnull*/ bitvec; - BrightnessGridCommand */*notnull*/ brightness_grid; - CharGridCommand */*notnull*/ char_grid; - Cp437GridCommand */*notnull*/ cp437_grid; - GlobalBrightnessCommand */*notnull*/ global_brightness; - ClearCommand */*notnull*/ clear; - BitmapLegacyCommand */*notnull*/ bitmap_legacy; - HardResetCommand */*notnull*/ hard_reset; - FadeOutCommand */*notnull*/ fade_out; + struct BitmapCommand */*notnull*/ bitmap; + struct BitVecCommand */*notnull*/ bitvec; + struct BrightnessGridCommand */*notnull*/ brightness_grid; + struct CharGridCommand */*notnull*/ char_grid; + struct Cp437GridCommand */*notnull*/ cp437_grid; + struct GlobalBrightnessCommand */*notnull*/ global_brightness; + struct ClearCommand */*notnull*/ clear; + struct BitmapLegacyCommand */*notnull*/ bitmap_legacy; + struct HardResetCommand */*notnull*/ hard_reset; + struct FadeOutCommand */*notnull*/ fade_out; } CommandUnion; /** @@ -560,7 +570,7 @@ typedef union { * * Rust equivalent: [TypedCommand]. */ -typedef struct { +typedef struct Command { /** * Specifies which kind of command struct is contained in `data` */ @@ -568,7 +578,7 @@ typedef struct { /** * The pointer to the command struct */ - CommandUnion data; + union CommandUnion data; } Command; /** @@ -581,7 +591,7 @@ typedef struct { * * The contained values are in platform endian-ness and may need to be converted before sending. */ -typedef struct { +typedef struct Header { /** * The first two bytes specify which command this packet represents. */ @@ -606,14 +616,23 @@ typedef struct { +/** + * Actual hardware limit is around 28-29ms/frame. Rounded up for less dropped packets. + */ +#define SP_FRAME_PACING_MS 30 + + + #ifdef __cplusplus extern "C" { #endif // __cplusplus +void init_env_logger(void); + /** * Clones a [Bitmap]. */ -Bitmap */*notnull*/ sp_bitmap_clone(Bitmap */*notnull*/ bitmap); +struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ bitmap); /** * Sets the state of all pixels in the [Bitmap]. @@ -623,12 +642,12 @@ Bitmap */*notnull*/ sp_bitmap_clone(Bitmap */*notnull*/ bitmap); * - `bitmap`: instance to write to * - `value`: the value to set all pixels to */ -void sp_bitmap_fill(Bitmap */*notnull*/ bitmap, bool value); +void sp_bitmap_fill(struct Bitmap */*notnull*/ bitmap, bool value); /** * Deallocates a [Bitmap]. */ -void sp_bitmap_free(Bitmap */*notnull*/ bitmap); +void sp_bitmap_free(struct Bitmap */*notnull*/ bitmap); /** * Tries to convert the BitVec to a Bitmap. @@ -637,7 +656,7 @@ void sp_bitmap_free(Bitmap */*notnull*/ bitmap); * * Returns NULL in case of error. */ -Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); +struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); /** * Gets the current value at the specified position in the [Bitmap]. @@ -651,7 +670,7 @@ Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); * * - when accessing `x` or `y` out of bounds */ -bool sp_bitmap_get(Bitmap */*notnull*/ bitmap, size_t x, size_t y); +bool sp_bitmap_get(struct Bitmap */*notnull*/ bitmap, size_t x, size_t y); /** * Gets the height in pixels of the [Bitmap] instance. @@ -660,12 +679,12 @@ bool sp_bitmap_get(Bitmap */*notnull*/ bitmap, size_t x, size_t y); * * - `bitmap`: instance to read from */ -size_t sp_bitmap_height(Bitmap */*notnull*/ bitmap); +size_t sp_bitmap_height(struct Bitmap */*notnull*/ bitmap); /** * Consumes the Bitmap and returns the contained BitVec */ -BitVec */*notnull*/ sp_bitmap_into_bitvec(Bitmap */*notnull*/ bitmap); +BitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); /** * Creates a [BitmapCommand] and immediately turns that into a [Packet]. @@ -674,10 +693,10 @@ BitVec */*notnull*/ sp_bitmap_into_bitvec(Bitmap */*notnull*/ bitmap); * * Returns NULL in case of an error. */ -Packet *sp_bitmap_into_packet(Bitmap */*notnull*/ bitmap, - size_t x, - size_t y, - CompressionCode compression); +struct Packet *sp_bitmap_into_packet(struct Bitmap */*notnull*/ bitmap, + size_t x, + size_t y, + CompressionCode compression); /** * Loads a [Bitmap] with the specified dimensions from the provided data. @@ -689,9 +708,9 @@ Packet *sp_bitmap_into_packet(Bitmap */*notnull*/ bitmap, * * returns: [Bitmap] that contains a copy of the provided data, or NULL in case of an error. */ -Bitmap *sp_bitmap_load(size_t width, - size_t height, - ByteSlice data); +struct Bitmap *sp_bitmap_load(size_t width, + size_t height, + struct ByteSlice data); /** * Creates a new [Bitmap] with the specified dimensions. @@ -718,14 +737,14 @@ Bitmap *sp_bitmap_load(size_t width, * sp_bitmap_free(grid); * ``` */ -Bitmap *sp_bitmap_new(size_t width, size_t height); +struct Bitmap *sp_bitmap_new(size_t width, size_t height); /** * Creates a new [Bitmap] with a size matching the screen. * * returns: [Bitmap] initialized to all pixels off. */ -Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); +struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); /** * Sets the value of the specified position in the [Bitmap]. @@ -740,14 +759,17 @@ Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); * * - when accessing `x` or `y` out of bounds */ -void sp_bitmap_set(Bitmap */*notnull*/ bitmap, size_t x, size_t y, bool value); +void sp_bitmap_set(struct Bitmap */*notnull*/ bitmap, + size_t x, + size_t y, + bool value); /** * Gets an unsafe reference to the data of the [Bitmap] instance. * * The returned memory is valid for the lifetime of the bitmap. */ -ByteSlice sp_bitmap_unsafe_data_ref(Bitmap */*notnull*/ bitmap); +struct ByteSlice sp_bitmap_unsafe_data_ref(struct Bitmap */*notnull*/ bitmap); /** * Gets the width in pixels of the [Bitmap] instance. @@ -766,7 +788,7 @@ ByteSlice sp_bitmap_unsafe_data_ref(Bitmap */*notnull*/ bitmap); * * - `bitmap` points to a valid [Bitmap] */ -size_t sp_bitmap_width(Bitmap */*notnull*/ bitmap); +size_t sp_bitmap_width(struct Bitmap */*notnull*/ bitmap); /** * Clones a [DisplayBitVec]. @@ -811,10 +833,10 @@ bool sp_bitvec_get(BitVec */*notnull*/ bit_vec, size_t index); * * Returns NULL in case of an error. */ -Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, - size_t offset, - BinaryOperation operation, - CompressionCode compression); +struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, + size_t offset, + BinaryOperation operation, + CompressionCode compression); /** * Returns true if length is 0. @@ -839,7 +861,7 @@ size_t sp_bitvec_len(BitVec */*notnull*/ bit_vec); * * returns: [DisplayBitVec] instance containing data. */ -BitVec */*notnull*/ sp_bitvec_load(ByteSlice data); +BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); /** * Creates a new [DisplayBitVec] instance. @@ -880,7 +902,7 @@ void sp_bitvec_set(BitVec */*notnull*/ bit_vec, size_t index, bool value); * * - `bit_vec`: instance to write to */ -ByteSlice sp_bitvec_unsafe_data_ref(BitVec */*notnull*/ bit_vec); +struct ByteSlice sp_bitvec_unsafe_data_ref(BitVec */*notnull*/ bit_vec); /** * Clones a [BrightnessGrid]. @@ -938,9 +960,9 @@ size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ brightness_grid); * * Returns NULL in case of an error. */ -Packet *sp_brightness_grid_into_packet(BrightnessGrid */*notnull*/ grid, - size_t x, - size_t y); +struct Packet *sp_brightness_grid_into_packet(BrightnessGrid */*notnull*/ grid, + size_t x, + size_t y); /** * Loads a [BrightnessGrid] with the specified dimensions from the provided data. @@ -951,7 +973,7 @@ Packet *sp_brightness_grid_into_packet(BrightnessGrid */*notnull*/ grid, */ BrightnessGrid *sp_brightness_grid_load(size_t width, size_t height, - ByteSlice data); + struct ByteSlice data); /** * Creates a new [BrightnessGrid] with the specified dimensions. @@ -1005,7 +1027,7 @@ void sp_brightness_grid_set(BrightnessGrid */*notnull*/ brightness_grid, * * returns: slice of bytes underlying the `brightness_grid`. */ -ByteSlice sp_brightness_grid_unsafe_data_ref(BrightnessGrid */*notnull*/ brightness_grid); +struct ByteSlice sp_brightness_grid_unsafe_data_ref(BrightnessGrid */*notnull*/ brightness_grid); /** * Gets the width of the [BrightnessGrid] instance. @@ -1068,16 +1090,16 @@ size_t sp_char_grid_height(CharGrid */*notnull*/ char_grid); * * Returns NULL in case of an error. */ -Packet *sp_char_grid_into_packet(CharGrid */*notnull*/ grid, - size_t x, - size_t y); +struct Packet *sp_char_grid_into_packet(CharGrid */*notnull*/ grid, + size_t x, + size_t y); /** * Loads a [CharGrid] with the specified dimensions from the provided data. * * returns: new CharGrid or NULL in case of an error */ -CharGrid *sp_char_grid_load(size_t width, size_t height, ByteSlice data); +CharGrid *sp_char_grid_load(size_t width, size_t height, struct ByteSlice data); /** * Creates a new [CharGrid] with the specified dimensions. @@ -1129,12 +1151,12 @@ size_t sp_char_grid_width(CharGrid */*notnull*/ char_grid); * * returns: a new [BitmapCommand] instance. */ -BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(BitmapCommand */*notnull*/ command); +struct BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(struct BitmapCommand */*notnull*/ command); /** * Deallocates a [BitmapCommand] instance. */ -void sp_cmd_bitmap_free(BitmapCommand */*notnull*/ command); +void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ command); /** * Move the provided [Bitmap] into a new [BitmapCommand], @@ -1142,7 +1164,7 @@ void sp_cmd_bitmap_free(BitmapCommand */*notnull*/ command); * * Rust equivalent: `BitmapCommand::from(bitmap)` */ -BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(Bitmap */*notnull*/ bitmap); +struct BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(struct Bitmap */*notnull*/ bitmap); /** * Returns a pointer to the provided `BitmapCommand`. @@ -1152,17 +1174,17 @@ BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(Bitmap */*notnull*/ bitmap) * - The returned bitmap inherits the lifetime of the command in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. */ -Bitmap */*notnull*/ sp_cmd_bitmap_get(BitmapCommand */*notnull*/ command); +struct Bitmap */*notnull*/ sp_cmd_bitmap_get(struct BitmapCommand */*notnull*/ command); /** * Reads the compression kind of the [BitmapCommand]. */ -CompressionCode sp_cmd_bitmap_get_compression(BitmapCommand */*notnull*/ command); +CompressionCode sp_cmd_bitmap_get_compression(struct BitmapCommand */*notnull*/ command); /** * Reads the origin field of the [BitmapCommand]. */ -void sp_cmd_bitmap_get_origin(BitmapCommand */*notnull*/ command, +void sp_cmd_bitmap_get_origin(struct BitmapCommand */*notnull*/ command, size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); @@ -1173,27 +1195,27 @@ void sp_cmd_bitmap_get_origin(BitmapCommand */*notnull*/ command, * * Returns: a new [BitmapCommand] instance. */ -BitmapCommand */*notnull*/ sp_cmd_bitmap_new(Bitmap */*notnull*/ bitmap, - size_t origin_x, - size_t origin_y, - CompressionCode compression); +struct BitmapCommand */*notnull*/ sp_cmd_bitmap_new(struct Bitmap */*notnull*/ bitmap, + size_t origin_x, + size_t origin_y, + CompressionCode compression); /** * Moves the provided [Bitmap] to be contained in the [BitmapCommand]. */ -void sp_cmd_bitmap_set(BitmapCommand */*notnull*/ command, - Bitmap */*notnull*/ bitmap); +void sp_cmd_bitmap_set(struct BitmapCommand */*notnull*/ command, + struct Bitmap */*notnull*/ bitmap); /** * Overwrites the compression kind of the [BitmapCommand]. */ -void sp_cmd_bitmap_set_compression(BitmapCommand */*notnull*/ command, +void sp_cmd_bitmap_set_compression(struct BitmapCommand */*notnull*/ command, CompressionCode compression); /** * Overwrites the origin field of the [BitmapCommand]. */ -void sp_cmd_bitmap_set_origin(BitmapCommand */*notnull*/ command, +void sp_cmd_bitmap_set_origin(struct BitmapCommand */*notnull*/ command, size_t origin_x, size_t origin_y); @@ -1202,39 +1224,39 @@ void sp_cmd_bitmap_set_origin(BitmapCommand */*notnull*/ command, * * Returns: NULL or a [Packet] containing the command. */ -Packet *sp_cmd_bitmap_try_into_packet(BitmapCommand */*notnull*/ command); +struct Packet *sp_cmd_bitmap_try_into_packet(struct BitmapCommand */*notnull*/ command); /** * Clones an [BitVecCommand] instance. * * returns: a new [BitVecCommand] instance. */ -BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(BitVecCommand */*notnull*/ command); +struct BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(struct BitVecCommand */*notnull*/ command); /** * Deallocates a [BitVecCommand]. */ -void sp_cmd_bitvec_free(BitVecCommand */*notnull*/ command); +void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ command); /** * Returns a pointer to the [BitVec] contained in the [BitVecCommand]. */ -BitVec *sp_cmd_bitvec_get(BitVecCommand */*notnull*/ command); +BitVec *sp_cmd_bitvec_get(struct BitVecCommand */*notnull*/ command); /** * Reads the compression kind of the [BitVecCommand]. */ -CompressionCode sp_cmd_bitvec_get_compression(BitVecCommand */*notnull*/ command); +CompressionCode sp_cmd_bitvec_get_compression(struct BitVecCommand */*notnull*/ command); /** * Reads the offset field of the [BitVecCommand]. */ -Offset sp_cmd_bitvec_get_offset(BitVecCommand */*notnull*/ command); +Offset sp_cmd_bitvec_get_offset(struct BitVecCommand */*notnull*/ command); /** * Returns the [BinaryOperation] of the command. */ -BinaryOperation sp_cmd_bitvec_get_operation(BitVecCommand */*notnull*/ command); +BinaryOperation sp_cmd_bitvec_get_operation(struct BitVecCommand */*notnull*/ command); /** * Set pixel data starting at the pixel offset on screen. @@ -1250,33 +1272,33 @@ BinaryOperation sp_cmd_bitvec_get_operation(BitVecCommand */*notnull*/ command); * * The contained [`DisplayBitVec`] is always uncompressed. */ -BitVecCommand */*notnull*/ sp_cmd_bitvec_new(BitVec */*notnull*/ bitvec, - size_t offset, - BinaryOperation operation, - CompressionCode compression); +struct BitVecCommand */*notnull*/ sp_cmd_bitvec_new(BitVec */*notnull*/ bitvec, + size_t offset, + BinaryOperation operation, + CompressionCode compression); /** * Moves the provided [BitVec] to be contained in the [BitVecCommand]. */ -void sp_cmd_bitvec_set(BitVecCommand */*notnull*/ command, +void sp_cmd_bitvec_set(struct BitVecCommand */*notnull*/ command, BitVec */*notnull*/ bitvec); /** * Overwrites the compression kind of the [BitVecCommand]. */ -void sp_cmd_bitvec_set_compression(BitVecCommand */*notnull*/ command, +void sp_cmd_bitvec_set_compression(struct BitVecCommand */*notnull*/ command, CompressionCode compression); /** * Overwrites the offset field of the [BitVecCommand]. */ -void sp_cmd_bitvec_set_offset(BitVecCommand */*notnull*/ command, +void sp_cmd_bitvec_set_offset(struct BitVecCommand */*notnull*/ command, Offset offset); /** * Overwrites the [BinaryOperation] of the command. */ -void sp_cmd_bitvec_set_operation(BitVecCommand */*notnull*/ command, +void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ command, BinaryOperation operation); /** @@ -1284,32 +1306,32 @@ void sp_cmd_bitvec_set_operation(BitVecCommand */*notnull*/ command, * * Returns: NULL or a [Packet] containing the command. */ -Packet *sp_cmd_bitvec_try_into_packet(BitVecCommand */*notnull*/ command); +struct Packet *sp_cmd_bitvec_try_into_packet(struct BitVecCommand */*notnull*/ command); /** * Clones an [GlobalBrightnessCommand] instance. * * returns: a new [GlobalBrightnessCommand] instance. */ -GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(GlobalBrightnessCommand */*notnull*/ command); +struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(struct GlobalBrightnessCommand */*notnull*/ command); -void sp_cmd_brightness_global_free(BitmapCommand */*notnull*/ command); +void sp_cmd_brightness_global_free(struct BitmapCommand */*notnull*/ command); -Brightness *sp_cmd_brightness_global_get(GlobalBrightnessCommand */*notnull*/ command); +Brightness *sp_cmd_brightness_global_get(struct GlobalBrightnessCommand */*notnull*/ command); -Packet */*notnull*/ sp_cmd_brightness_global_into_packet(GlobalBrightnessCommand */*notnull*/ command); +struct Packet */*notnull*/ sp_cmd_brightness_global_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); /** * Set the brightness of all tiles to the same value. * * Returns: a new [GlobalBrightnessCommand] instance. */ -GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightness brightness); +struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightness brightness); /** * Moves the provided bitmap to be contained in the command. */ -void sp_cmd_brightness_global_set(GlobalBrightnessCommand */*notnull*/ command, +void sp_cmd_brightness_global_set(struct GlobalBrightnessCommand */*notnull*/ command, Brightness brightness); /** @@ -1317,28 +1339,28 @@ void sp_cmd_brightness_global_set(GlobalBrightnessCommand */*notnull*/ command, * * returns: a new [BrightnessGridCommand] instance. */ -BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_clone(BrightnessGridCommand */*notnull*/ command); +struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_clone(struct BrightnessGridCommand */*notnull*/ command); /** * Deallocates a [BitmapCommand]. */ -void sp_cmd_brightness_grid_free(BitmapCommand */*notnull*/ command); +void sp_cmd_brightness_grid_free(struct BitmapCommand */*notnull*/ command); /** * Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], * leaving other fields as their default values. */ -BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_from_grid(BrightnessGrid */*notnull*/ grid); +struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_from_grid(BrightnessGrid */*notnull*/ grid); /** * Returns a pointer to the [BrightnessGrid] contained in the [BrightnessGridCommand]. */ -BrightnessGrid *sp_cmd_brightness_grid_get(BrightnessGridCommand */*notnull*/ command); +BrightnessGrid *sp_cmd_brightness_grid_get(struct BrightnessGridCommand */*notnull*/ command); /** * Overwrites the origin field of the [BrightnessGridCommand]. */ -void sp_cmd_brightness_grid_get_origin(BrightnessGridCommand */*notnull*/ command, +void sp_cmd_brightness_grid_get_origin(struct BrightnessGridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); @@ -1347,7 +1369,7 @@ void sp_cmd_brightness_grid_get_origin(BrightnessGridCommand */*notnull*/ comman * * Returns: NULL or a [Packet] containing the command. */ -Packet *sp_cmd_brightness_grid_into_packet(BrightnessGridCommand */*notnull*/ command); +struct Packet *sp_cmd_brightness_grid_into_packet(struct BrightnessGridCommand */*notnull*/ command); /** * Set the brightness of individual tiles in a rectangular area of the display. @@ -1356,20 +1378,20 @@ Packet *sp_cmd_brightness_grid_into_packet(BrightnessGridCommand */*notnull*/ co * * Returns: a new [BrightnessGridCommand] instance. */ -BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_new(BrightnessGrid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); +struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_new(BrightnessGrid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); /** * Moves the provided [BrightnessGrid] to be contained in the [BrightnessGridCommand]. */ -void sp_cmd_brightness_grid_set(BrightnessGridCommand */*notnull*/ command, +void sp_cmd_brightness_grid_set(struct BrightnessGridCommand */*notnull*/ command, BrightnessGrid */*notnull*/ grid); /** * Reads the origin field of the [BrightnessGridCommand]. */ -void sp_cmd_brightness_grid_set_origin(BrightnessGridCommand */*notnull*/ command, +void sp_cmd_brightness_grid_set_origin(struct BrightnessGridCommand */*notnull*/ command, size_t origin_x, size_t origin_y); @@ -1378,28 +1400,28 @@ void sp_cmd_brightness_grid_set_origin(BrightnessGridCommand */*notnull*/ comman * * returns: a new [CharGridCommand] instance. */ -CharGridCommand */*notnull*/ sp_cmd_char_grid_clone(CharGridCommand */*notnull*/ command); +struct CharGridCommand */*notnull*/ sp_cmd_char_grid_clone(struct CharGridCommand */*notnull*/ command); /** * Deallocates a [BitmapCommand]. */ -void sp_cmd_char_grid_free(BitmapCommand */*notnull*/ command); +void sp_cmd_char_grid_free(struct BitmapCommand */*notnull*/ command); /** * Moves the provided [CharGrid] into a new [CharGridCommand], * leaving other fields as their default values. */ -CharGridCommand */*notnull*/ sp_cmd_char_grid_from_grid(CharGrid */*notnull*/ grid); +struct CharGridCommand */*notnull*/ sp_cmd_char_grid_from_grid(CharGrid */*notnull*/ grid); /** * Returns a pointer to the [CharGrid] contained in the [CharGridCommand]. */ -CharGrid *sp_cmd_char_grid_get(CharGridCommand */*notnull*/ command); +CharGrid *sp_cmd_char_grid_get(struct CharGridCommand */*notnull*/ command); /** * Reads the origin field of the [CharGridCommand]. */ -void sp_cmd_char_grid_get_origin(CharGridCommand */*notnull*/ command, +void sp_cmd_char_grid_get_origin(struct CharGridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); @@ -1410,20 +1432,20 @@ void sp_cmd_char_grid_get_origin(CharGridCommand */*notnull*/ command, * * Returns: a new [CharGridCommand] instance. */ -CharGridCommand */*notnull*/ sp_cmd_char_grid_new(CharGrid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); +struct CharGridCommand */*notnull*/ sp_cmd_char_grid_new(CharGrid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); /** * Moves the provided [CharGrid] to be contained in the [CharGridCommand]. */ -void sp_cmd_char_grid_set(CharGridCommand */*notnull*/ command, +void sp_cmd_char_grid_set(struct CharGridCommand */*notnull*/ command, CharGrid */*notnull*/ grid); /** * Overwrites the origin field of the [CharGridCommand]. */ -void sp_cmd_char_grid_set_origin(CharGridCommand */*notnull*/ command, +void sp_cmd_char_grid_set_origin(struct CharGridCommand */*notnull*/ command, size_t origin_x, size_t origin_y); @@ -1432,12 +1454,12 @@ void sp_cmd_char_grid_set_origin(CharGridCommand */*notnull*/ command, * * Returns: NULL or a [Packet] containing the command. */ -Packet *sp_cmd_char_grid_try_into_packet(CharGridCommand */*notnull*/ command); +struct Packet *sp_cmd_char_grid_try_into_packet(struct CharGridCommand */*notnull*/ command); /** * Deallocates a [ClearCommand]. */ -void sp_cmd_clear_free(ClearCommand */*notnull*/ command); +void sp_cmd_clear_free(struct ClearCommand */*notnull*/ command); /** * Set all pixels to the off state. @@ -1446,25 +1468,25 @@ void sp_cmd_clear_free(ClearCommand */*notnull*/ command); * * Returns: a new [ClearCommand] instance. */ -ClearCommand */*notnull*/ sp_cmd_clear_new(void); +struct ClearCommand */*notnull*/ sp_cmd_clear_new(void); /** * Clones an [Cp437GridCommand] instance. * * returns: a new [Cp437GridCommand] instance. */ -Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_clone(Cp437GridCommand */*notnull*/ command); +struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_clone(struct Cp437GridCommand */*notnull*/ command); /** * Deallocates a [Cp437GridCommand]. */ -void sp_cmd_cp437_grid_free(BitmapCommand */*notnull*/ command); +void sp_cmd_cp437_grid_free(struct BitmapCommand */*notnull*/ command); /** * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], * leaving other fields as their default values. */ -Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*notnull*/ grid); +struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*notnull*/ grid); /** * Show text on the screen. @@ -1474,14 +1496,14 @@ Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*notnull*/ * * [CP-437]: https://en.wikipedia.org/wiki/Code_page_437 */ -Cp437Grid *sp_cmd_cp437_grid_get(Cp437GridCommand */*notnull*/ command); +Cp437Grid *sp_cmd_cp437_grid_get(struct Cp437GridCommand */*notnull*/ command); /** * Gets the origin field of the [Cp437GridCommand]. * * Rust equivalent: `cp437_command.origin` */ -void sp_cmd_cp437_grid_get_origin(Cp437GridCommand */*notnull*/ command, +void sp_cmd_cp437_grid_get_origin(struct Cp437GridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); @@ -1492,16 +1514,16 @@ void sp_cmd_cp437_grid_get_origin(Cp437GridCommand */*notnull*/ command, * * The origin is relative to the top-left of the display. */ -Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_new(Cp437Grid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); +struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_new(Cp437Grid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); /** * Moves the provided bitmap into the provided command. * * This drops the previously contained [Cp437Grid]. */ -void sp_cmd_cp437_grid_set(Cp437GridCommand */*notnull*/ command, +void sp_cmd_cp437_grid_set(struct Cp437GridCommand */*notnull*/ command, Cp437Grid */*notnull*/ grid); /** @@ -1509,7 +1531,7 @@ void sp_cmd_cp437_grid_set(Cp437GridCommand */*notnull*/ command, * * Rust equivalent: `cp437_command.origin = Origin::new(origin_x, origin_y)` */ -void sp_cmd_cp437_grid_set_origin(Cp437GridCommand */*notnull*/ command, +void sp_cmd_cp437_grid_set_origin(struct Cp437GridCommand */*notnull*/ command, size_t origin_x, size_t origin_y); @@ -1518,26 +1540,26 @@ void sp_cmd_cp437_grid_set_origin(Cp437GridCommand */*notnull*/ command, * * Returns: NULL or a [Packet] containing the command. */ -Packet *sp_cmd_cp437_grid_try_into_packet(Cp437GridCommand */*notnull*/ command); +struct Packet *sp_cmd_cp437_grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command); /** * Deallocates a [FadeOutCommand]. */ -void sp_cmd_fade_out_free(ClearCommand */*notnull*/ command); +void sp_cmd_fade_out_free(struct ClearCommand */*notnull*/ command); /** * A yet-to-be-tested command. * * Returns: a new [FadeOutCommand] instance. */ -FadeOutCommand */*notnull*/ sp_cmd_fade_out_new(void); +struct FadeOutCommand */*notnull*/ sp_cmd_fade_out_new(void); /** * Clones an [SPCommand] instance. * * returns: a new [SPCommand] instance. */ -Command sp_cmd_generic_clone(Command command); +struct Command sp_cmd_generic_clone(struct Command command); /** * Deallocates an [SPCommand]. @@ -1549,7 +1571,7 @@ Command sp_cmd_generic_clone(Command command); * sp_command_free(c); * ``` */ -void sp_cmd_generic_free(Command command); +void sp_cmd_generic_free(struct Command command); /** * Tries to turn a [SPCommand] into a [Packet]. @@ -1557,7 +1579,7 @@ void sp_cmd_generic_free(Command command); * * Returns tag [CommandTag::Invalid] in case of an error. */ -Packet *sp_cmd_generic_into_packet(Command command); +struct Packet *sp_cmd_generic_into_packet(struct Command command); /** * Tries to turn a [Packet] into a [SPCommand]. @@ -1566,12 +1588,12 @@ Packet *sp_cmd_generic_into_packet(Command command); * * Returns: pointer to new [SPCommand] instance or NULL if parsing failed. */ -Command *sp_cmd_generic_try_from_packet(Packet */*notnull*/ packet); +struct Command *sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet); /** * Deallocates a [HardResetCommand]. */ -void sp_cmd_hard_reset_free(ClearCommand */*notnull*/ command); +void sp_cmd_hard_reset_free(struct ClearCommand */*notnull*/ command); /** * Kills the udp daemon on the display, which usually results in a restart. @@ -1580,7 +1602,7 @@ void sp_cmd_hard_reset_free(ClearCommand */*notnull*/ command); * * Returns: a new [HardResetCommand] instance. */ -HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void); +struct HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void); /** * Clones a [Cp437Grid]. @@ -1634,14 +1656,16 @@ size_t sp_cp437_grid_height(Cp437Grid */*notnull*/ cp437_grid); * * Returns NULL in case of an error. */ -Packet *sp_cp437_grid_into_packet(Cp437Grid */*notnull*/ grid, - size_t x, - size_t y); +struct Packet *sp_cp437_grid_into_packet(Cp437Grid */*notnull*/ grid, + size_t x, + size_t y); /** * Loads a [Cp437Grid] with the specified dimensions from the provided data. */ -Cp437Grid *sp_cp437_grid_load(size_t width, size_t height, ByteSlice data); +Cp437Grid *sp_cp437_grid_load(size_t width, + size_t height, + struct ByteSlice data); /** * Creates a new [Cp437Grid] with the specified dimensions. @@ -1675,7 +1699,7 @@ void sp_cp437_grid_set(Cp437Grid */*notnull*/ cp437_grid, * * The returned memory is valid for the lifetime of the grid. */ -ByteSlice sp_cp437_grid_unsafe_data_ref(Cp437Grid */*notnull*/ cp437_grid); +struct ByteSlice sp_cp437_grid_unsafe_data_ref(Cp437Grid */*notnull*/ cp437_grid); /** * Gets the width of the [Cp437Grid] instance. @@ -1691,34 +1715,36 @@ size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ cp437_grid); * * returns: a new [Packet] instance. */ -Packet */*notnull*/ sp_packet_clone(Packet */*notnull*/ packet); +struct Packet */*notnull*/ sp_packet_clone(struct Packet */*notnull*/ packet); /** * Deallocates a [Packet]. */ -void sp_packet_free(Packet */*notnull*/ packet); +void sp_packet_free(struct Packet */*notnull*/ packet); /** * Creates a raw [Packet] from parts. * * returns: new instance. Will never return null. */ -Packet */*notnull*/ sp_packet_from_parts(Header header, - const ByteSlice *payload); +struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, + struct ByteSlice payload); /** * Returns a pointer to the header field of the provided packet. * * The returned header can be changed and will be valid for the lifetime of the packet. */ -Header */*notnull*/ sp_packet_get_header(Packet */*notnull*/ packet); +struct Header */*notnull*/ sp_packet_get_header(struct Packet */*notnull*/ packet); /** * Returns a pointer to the current payload of the provided packet. * + * Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. + * * The returned memory can be changed and will be valid until a new payload is set. */ -ByteSlice sp_packet_get_payload(Packet */*notnull*/ packet); +struct ByteSlice sp_packet_get_payload(struct Packet */*notnull*/ packet); /** * Serialize the packet into the provided buffer. @@ -1727,21 +1753,23 @@ ByteSlice sp_packet_get_payload(Packet */*notnull*/ packet); * * - if the buffer is not big enough to hold header+payload. */ -void sp_packet_serialize_to(Packet */*notnull*/ packet, ByteSlice buffer); +void sp_packet_serialize_to(struct Packet */*notnull*/ packet, + struct ByteSlice buffer); /** * Sets the payload of the provided packet to the provided data. * * This makes previous payload pointers invalid. */ -void sp_packet_set_payload(Packet */*notnull*/ packet, ByteSlice data); +void sp_packet_set_payload(struct Packet */*notnull*/ packet, + struct ByteSlice data); /** * Tries to load a [Packet] from the passed array with the specified length. * * returns: NULL in case of an error, pointer to the allocated packet otherwise */ -Packet *sp_packet_try_load(ByteSlice data); +struct Packet *sp_packet_try_load(struct ByteSlice data); /** * Converts u16 into [CommandCode]. @@ -1754,7 +1782,7 @@ bool sp_u16_to_command_code(uint16_t code, /** * Closes and deallocates a [UdpSocket]. */ -void sp_udp_free(UdpSocket */*notnull*/ connection); +void sp_udp_free(struct UdpSocket */*notnull*/ connection); /** * Creates a new instance of [UdpSocket]. @@ -1769,7 +1797,7 @@ void sp_udp_free(UdpSocket */*notnull*/ connection); * sp_udp_send_command(connection, sp_command_clear()); * ``` */ -UdpSocket *sp_udp_open(char */*notnull*/ host); +struct UdpSocket *sp_udp_open(char */*notnull*/ host); /** * Creates a new instance of [UdpSocket]. @@ -1784,11 +1812,11 @@ UdpSocket *sp_udp_open(char */*notnull*/ host); * sp_udp_send_command(connection, sp_command_clear()); * ``` */ -UdpSocket *sp_udp_open_ipv4(uint8_t ip1, - uint8_t ip2, - uint8_t ip3, - uint8_t ip4, - uint16_t port); +struct UdpSocket *sp_udp_open_ipv4(uint8_t ip1, + uint8_t ip2, + uint8_t ip3, + uint8_t ip4, + uint16_t port); /** * Sends a [SPCommand] to the display using the [UdpSocket]. @@ -1803,7 +1831,8 @@ UdpSocket *sp_udp_open_ipv4(uint8_t ip1, * sp_udp_send_command(connection, sp_command_brightness(5)); * ``` */ -bool sp_udp_send_command(UdpSocket */*notnull*/ connection, Command command); +bool sp_udp_send_command(struct UdpSocket */*notnull*/ connection, + struct Command command); /** * Sends a [Header] to the display using the [UdpSocket]. @@ -1816,7 +1845,8 @@ bool sp_udp_send_command(UdpSocket */*notnull*/ connection, Command command); * sp_udp_send_header(connection, sp_command_brightness(5)); * ``` */ -bool sp_udp_send_header(UdpSocket */*notnull*/ udp_connection, Header header); +bool sp_udp_send_header(struct UdpSocket */*notnull*/ udp_connection, + struct Header header); /** * Sends a [Packet] to the display using the [UdpSocket]. @@ -1825,9 +1855,11 @@ bool sp_udp_send_header(UdpSocket */*notnull*/ udp_connection, Header header); * * returns: true in case of success */ -bool sp_udp_send_packet(UdpSocket */*notnull*/ connection, - Packet */*notnull*/ packet); +bool sp_udp_send_packet(struct UdpSocket */*notnull*/ connection, + struct Packet */*notnull*/ packet); #ifdef __cplusplus } // extern "C" #endif // __cplusplus + +#endif /* SERVICEPOINT_BINDINGS_C */ diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 617e3af..b49cdc5 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -65,7 +65,7 @@ pub unsafe extern "C" fn sp_cmd_bitvec_free(command: NonNull) { pub unsafe extern "C" fn sp_cmd_bitvec_get( mut command: NonNull, ) -> *mut DisplayBitVec { - &mut unsafe { command.as_mut() }.bitvec + unsafe { &mut command.as_mut().bitvec } } /// Moves the provided [BitVec] to be contained in the [BitVecCommand]. @@ -103,7 +103,7 @@ pub unsafe extern "C" fn sp_cmd_bitvec_set_offset( pub unsafe extern "C" fn sp_cmd_bitvec_get_operation( command: NonNull, ) -> BinaryOperation { - unsafe { command.as_ref().operation.clone() } // TODO remove clone + unsafe { command.as_ref().operation } } /// Overwrites the [BinaryOperation] of the command. diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 80e1b89..7410f2d 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -76,7 +76,7 @@ pub unsafe extern "C" fn sp_cmd_brightness_grid_set( pub unsafe extern "C" fn sp_cmd_brightness_grid_get( mut command: NonNull, ) -> *mut BrightnessGrid { - &mut unsafe { command.as_mut() }.grid + unsafe { &mut command.as_mut().grid } } /// Overwrites the origin field of the [BrightnessGridCommand]. diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index 8a220a5..1fbd66c 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -74,7 +74,7 @@ pub unsafe extern "C" fn sp_cmd_char_grid_set( pub unsafe extern "C" fn sp_cmd_char_grid_get( mut command: NonNull, ) -> *mut CharGrid { - &mut unsafe { command.as_mut() }.grid + unsafe { &mut command.as_mut().grid } } /// Reads the origin field of the [CharGridCommand]. diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index f653ef5..f034c6d 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -83,7 +83,7 @@ pub unsafe extern "C" fn sp_cmd_cp437_grid_set( pub unsafe extern "C" fn sp_cmd_cp437_grid_get( mut command: NonNull, ) -> *mut Cp437Grid { - &mut unsafe { command.as_mut() }.grid + unsafe { &mut command.as_mut().grid } } /// Gets the origin field of the [Cp437GridCommand]. diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 063dea7..6cb777f 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -53,5 +53,5 @@ pub unsafe extern "C" fn sp_cmd_brightness_global_set( pub unsafe extern "C" fn sp_cmd_brightness_global_get( mut command: NonNull, ) -> *mut Brightness { - &mut unsafe { command.as_mut() }.brightness + unsafe { &mut command.as_mut().brightness } } diff --git a/src/containers/byte_slice.rs b/src/containers/byte_slice.rs index de7d0df..0668318 100644 --- a/src/containers/byte_slice.rs +++ b/src/containers/byte_slice.rs @@ -1,7 +1,5 @@ //! FFI slice helper -use std::ptr::NonNull; - /// Represents a span of memory (`&mut [u8]` ) as a struct. /// /// # Safety @@ -11,28 +9,39 @@ use std::ptr::NonNull; /// - accesses to the memory pointed to with `start` is never accessed outside `length` /// - the lifetime of the `CByteSlice` does not outlive the memory it points to, as described in /// the function returning this type. +/// - if `start` is NULL or `length` is 0, do not dereference `start`. +/// +/// # Examples +/// +/// ```c +/// ByteSlice empty = {.start: NULL, .length = 0}; +/// ``` #[repr(C)] +#[derive(Copy, Clone, Debug, Eq, PartialEq)] pub struct ByteSlice { /// The start address of the memory. - pub start: NonNull, + pub start: *mut u8, /// The amount of memory in bytes. pub length: usize, } impl ByteSlice { + pub(crate) const INVALID: ByteSlice = ByteSlice { + start: std::ptr::null_mut(), + length: 0, + }; + pub(crate) unsafe fn as_slice(&self) -> &[u8] { - unsafe { std::slice::from_raw_parts(self.start.as_ptr(), self.length) } + unsafe { std::slice::from_raw_parts(self.start, self.length) } } pub(crate) unsafe fn as_slice_mut(&mut self) -> &mut [u8] { - unsafe { - std::slice::from_raw_parts_mut(self.start.as_ptr(), self.length) - } + unsafe { std::slice::from_raw_parts_mut(self.start, self.length) } } pub(crate) unsafe fn from_slice(slice: &mut [u8]) -> Self { Self { - start: NonNull::new(slice.as_mut_ptr()).unwrap(), + start: slice.as_mut_ptr(), length: slice.len(), } } diff --git a/src/lib.rs b/src/lib.rs index 377b2a0..17c4b3a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,13 +35,19 @@ pub mod packet; /// Functions related to [UdpSocket]. pub mod udp; -use std::time::Duration; - /// Actual hardware limit is around 28-29ms/frame. Rounded up for less dropped packets. -pub const SP_FRAME_PACING_MS: u128 = Duration::from_millis(30).as_millis(); +pub const SP_FRAME_PACING_MS: u128 = 30; /// This is a type only used by cbindgen to have a type for pointers. pub struct UdpSocket; /// This is a type only used by cbindgen to have a type for pointers. pub struct DisplayBitVec; + +#[cfg(feature = "env_logger")] +pub mod feature_env_logger { + #[no_mangle] + pub unsafe extern "C" fn init_env_logger() { + env_logger::init(); + } +} diff --git a/src/packet.rs b/src/packet.rs index e9593c9..b1269df 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -20,13 +20,12 @@ pub unsafe extern "C" fn sp_packet_try_load(data: ByteSlice) -> *mut Packet { #[no_mangle] pub unsafe extern "C" fn sp_packet_from_parts( header: Header, - payload: *const ByteSlice, + payload: ByteSlice, ) -> NonNull { - let payload = if payload.is_null() { - vec![] + let payload = if payload == ByteSlice::INVALID { + None } else { - let payload = unsafe { (*payload).as_slice() }; - Vec::from(payload) + Some(Vec::from(unsafe { payload.as_slice() })) }; heap_move_nonnull(Packet { header, payload }) @@ -39,17 +38,24 @@ pub unsafe extern "C" fn sp_packet_from_parts( pub unsafe extern "C" fn sp_packet_get_header( packet: NonNull, ) -> NonNull
{ - NonNull::from(&mut unsafe { (*packet.as_ptr()).header }) + NonNull::from(unsafe { &mut (*packet.as_ptr()).header }) } /// Returns a pointer to the current payload of the provided packet. /// +/// Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. +/// /// The returned memory can be changed and will be valid until a new payload is set. #[no_mangle] pub unsafe extern "C" fn sp_packet_get_payload( packet: NonNull, ) -> ByteSlice { - unsafe { ByteSlice::from_slice(&mut (*packet.as_ptr()).payload) } + unsafe { + match &mut (*packet.as_ptr()).payload { + None => ByteSlice::INVALID, + Some(payload) => ByteSlice::from_slice(payload), + } + } } /// Sets the payload of the provided packet to the provided data. @@ -60,7 +66,13 @@ pub unsafe extern "C" fn sp_packet_set_payload( packet: NonNull, data: ByteSlice, ) { - unsafe { (*packet.as_ptr()).payload = data.as_slice().to_vec() } + unsafe { + (*packet.as_ptr()).payload = if data == ByteSlice::INVALID { + None + } else { + Some(data.as_slice().to_vec()) + } + } } /// Serialize the packet into the provided buffer. diff --git a/src/udp.rs b/src/udp.rs index 7c2ab2f..fe9f876 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -136,7 +136,7 @@ pub unsafe extern "C" fn sp_udp_send_header( ) -> bool { let packet = Packet { header, - payload: vec![], + payload: None, }; unsafe { udp_connection.as_ref() } .send(&Vec::from(packet))