housekeeping

This commit is contained in:
viandoxdev 2022-10-12 22:28:50 +02:00
parent cf4b5d6eb0
commit c6bdbf7fd8
No known key found for this signature in database
GPG Key ID: AF1410C5BC10AA25
15 changed files with 76 additions and 89 deletions

View File

@ -1,9 +1,9 @@
Q=@ Q=@
CC=clang CC=gcc
GCCCFLAGS=-Wno-format-truncation GCCCFLAGS=-Wno-format-truncation
CFLAGS=-std=c11 -pedantic -g -Wall -pthread -D_GNU_SOURCE -fsanitize=undefined CFLAGS=-std=c11 -pedantic -g -Wall -pthread -D_GNU_SOURCE
LDFLAGS=-lm -fsanitize=undefined LDFLAGS=-lm
BUILD_DIR=./objects BUILD_DIR=./objects
BIN=jsfw BIN=jsfw

View File

@ -1,4 +1,5 @@
#include "client.h" #include "client.h"
#include "const.h" #include "const.h"
#include "json.h" #include "json.h"
#include "net.h" #include "net.h"
@ -10,7 +11,6 @@
#include <linux/input-event-codes.h> #include <linux/input-event-codes.h>
#include <linux/input.h> #include <linux/input.h>
#include <linux/uinput.h> #include <linux/uinput.h>
#include <math.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <poll.h> #include <poll.h>
#include <stdbool.h> #include <stdbool.h>
@ -138,7 +138,7 @@ void device_init(MessageDeviceInfo *dev) {
device_destroy(dev->index); device_destroy(dev->index);
int fd = *(int*)vec_get(&devices_fd, dev->index); int fd = *(int *)vec_get(&devices_fd, dev->index);
// Abs // Abs
if (dev->abs_count > 0) { if (dev->abs_count > 0) {
@ -186,19 +186,20 @@ void device_init(MessageDeviceInfo *dev) {
ioctl(fd, UI_DEV_SETUP, &setup); ioctl(fd, UI_DEV_SETUP, &setup);
ioctl(fd, UI_DEV_CREATE); ioctl(fd, UI_DEV_CREATE);
MessageDeviceInfo * dst = vec_get(&devices_info, dev->index); MessageDeviceInfo *dst = vec_get(&devices_info, dev->index);
memcpy(dst, dev, sizeof(MessageDeviceInfo)); memcpy(dst, dev, sizeof(MessageDeviceInfo));
printf("CLIENT: Got device [%d]: '%s' (abs: %d, rel: %d, key: %d)\n", dev->index, ctr->device_name, dev->abs_count, dev->rel_count, dev->key_count); printf("CLIENT: Got device [%d]: '%s' (abs: %d, rel: %d, key: %d)\n", dev->index, ctr->device_name, dev->abs_count,
dev->rel_count, dev->key_count);
} }
// Send an event to uinput, device must exist // Send an event to uinput, device must exist
bool device_emit(int index, uint16_t type, uint16_t id, uint32_t value) { bool device_emit(int index, uint16_t type, uint16_t id, uint32_t value) {
if(index >= devices_fd.len) { if (index >= devices_fd.len) {
return true; return true;
} }
int fd = *(int*) vec_get(&devices_fd, index); int fd = *(int *)vec_get(&devices_fd, index);
struct input_event event = {0}; struct input_event event = {0};
event.type = type; event.type = type;
@ -215,10 +216,9 @@ void device_handle_report(MessageDeviceReport *report) {
return; return;
} }
MessageDeviceInfo * info = vec_get(&devices_info, report->index); MessageDeviceInfo *info = vec_get(&devices_info, report->index);
if (report->abs_count != info->abs_count || report->rel_count != info->rel_count || if (report->abs_count != info->abs_count || report->rel_count != info->rel_count || report->key_count != info->key_count) {
report->key_count != info->key_count) {
printf("CLIENT: Report doesn't match with device info\n"); printf("CLIENT: Report doesn't match with device info\n");
return; return;
} }

View File

@ -15,7 +15,7 @@ typedef struct {
typedef struct { typedef struct {
ClientController *controllers; ClientController *controllers;
size_t controller_count; size_t controller_count;
char *fifo_path; char *fifo_path;
struct timespec retry_delay; struct timespec retry_delay;

1
hid.h
View File

@ -5,6 +5,7 @@
#include "server.h" #include "server.h"
#include <linux/input-event-codes.h> #include <linux/input-event-codes.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
// Unique identifier for devices (provided by linux), May be the mac address // Unique identifier for devices (provided by linux), May be the mac address

47
json.c
View File

@ -55,8 +55,7 @@ static int json_parse_value(const char **buf, const char *buf_end, uint8_t **res
const uint8_t *dst_end); // Declaration for recursion const uint8_t *dst_end); // Declaration for recursion
// *dst must be 8 aligned // *dst must be 8 aligned
static inline int json_parse_string(const char **buf, const char *buf_end, uint8_t **restrict dst, static inline int json_parse_string(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) {
const uint8_t *dst_end) {
// Ensure enough space for the header // Ensure enough space for the header
if (*dst + sizeof(JSONHeader) >= dst_end) { if (*dst + sizeof(JSONHeader) >= dst_end) {
return set_jerrno(DstOverflow); return set_jerrno(DstOverflow);
@ -130,8 +129,8 @@ static inline int json_parse_string(const char **buf, const char *buf_end, uint8
} }
*(*dst)++ = 0xE0 | (un_codepoint >> 12 & 0x0F); *(*dst)++ = 0xE0 | (un_codepoint >> 12 & 0x0F);
*(*dst)++ = 0x80 | (un_codepoint >> 6 & 0x3F); *(*dst)++ = 0x80 | (un_codepoint >> 6 & 0x3F);
*(*dst)++ = 0x80 | (un_codepoint >> 0 & 0x3F); *(*dst)++ = 0x80 | (un_codepoint >> 0 & 0x3F);
header->len += 3; header->len += 3;
} else if (un_codepoint <= 0x10ffff) { // 4 byte codepoint } else if (un_codepoint <= 0x10ffff) { // 4 byte codepoint
if (*dst + 4 >= dst_end) { if (*dst + 4 >= dst_end) {
@ -140,8 +139,8 @@ static inline int json_parse_string(const char **buf, const char *buf_end, uint8
*(*dst)++ = 0xF0 | (un_codepoint >> 18 & 0x07); *(*dst)++ = 0xF0 | (un_codepoint >> 18 & 0x07);
*(*dst)++ = 0x80 | (un_codepoint >> 12 & 0x3F); *(*dst)++ = 0x80 | (un_codepoint >> 12 & 0x3F);
*(*dst)++ = 0x80 | (un_codepoint >> 6 & 0x3F); *(*dst)++ = 0x80 | (un_codepoint >> 6 & 0x3F);
*(*dst)++ = 0x80 | (un_codepoint >> 0 & 0x3F); *(*dst)++ = 0x80 | (un_codepoint >> 0 & 0x3F);
header->len += 4; header->len += 4;
} else { // Illegal codepoint } else { // Illegal codepoint
return set_jerrno(StringBadUnicode); return set_jerrno(StringBadUnicode);
@ -208,9 +207,8 @@ static inline int json_parse_string(const char **buf, const char *buf_end, uint8
(*buf)++; (*buf)++;
return 0; return 0;
} else if ((c < ' ' && c != '\t') || } else if ((c < ' ' && c != '\t') || c == 0x7f) { // Illegal characters, technically tab isn't allowed either
c == 0x7f) { // Illegal characters, technically tab isn't allowed either // but it felt weird so I added it
// but it felt weird so I added it
jerrno = StringBadChar; jerrno = StringBadChar;
return -1; return -1;
} }
@ -229,8 +227,7 @@ static inline int json_parse_string(const char **buf, const char *buf_end, uint8
} }
// *dst must be 8 aligned // *dst must be 8 aligned
static int json_parse_number(const char **buf, const char *buf_end, uint8_t **restrict dst, static int json_parse_number(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) {
const uint8_t *dst_end) {
// Ensure enough space for header and value // Ensure enough space for header and value
if (*dst + sizeof(JSONHeader) + sizeof(double) >= dst_end) { if (*dst + sizeof(JSONHeader) + sizeof(double) >= dst_end) {
return set_jerrno(DstOverflow); return set_jerrno(DstOverflow);
@ -349,8 +346,7 @@ static int json_parse_number(const char **buf, const char *buf_end, uint8_t **re
} }
// *dst must be 8 aligned // *dst must be 8 aligned
static int json_parse_boolean(const char **buf, const char *buf_end, uint8_t **restrict dst, static int json_parse_boolean(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) {
const uint8_t *dst_end) {
// Ensure enough space for header and value // Ensure enough space for header and value
if (*dst + sizeof(JSONHeader) + 8 >= dst_end) { // 8: sizeof(uint64_t) if (*dst + sizeof(JSONHeader) + 8 >= dst_end) { // 8: sizeof(uint64_t)
return set_jerrno(DstOverflow); return set_jerrno(DstOverflow);
@ -389,8 +385,7 @@ static int json_parse_boolean(const char **buf, const char *buf_end, uint8_t **r
} }
// *dst must be 8 aligned // *dst must be 8 aligned
static int json_parse_null(const char **buf, const char *buf_end, uint8_t **restrict dst, static int json_parse_null(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) {
const uint8_t *dst_end) {
// Ensure enough size for the header (no value) // Ensure enough size for the header (no value)
if (*dst + sizeof(JSONHeader) >= dst_end) { if (*dst + sizeof(JSONHeader) >= dst_end) {
return set_jerrno(DstOverflow); return set_jerrno(DstOverflow);
@ -415,8 +410,7 @@ static int json_parse_null(const char **buf, const char *buf_end, uint8_t **rest
} }
// *dst must be 8 aligned // *dst must be 8 aligned
static int json_parse_array(const char **buf, const char *buf_end, uint8_t **restrict dst, static int json_parse_array(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) {
const uint8_t *dst_end) {
// Ensure enough space for the header // Ensure enough space for the header
if (*dst + sizeof(JSONHeader) >= dst_end) { if (*dst + sizeof(JSONHeader) >= dst_end) {
return set_jerrno(DstOverflow); return set_jerrno(DstOverflow);
@ -477,8 +471,7 @@ static int json_parse_array(const char **buf, const char *buf_end, uint8_t **res
} }
// *dst must be 8 aligned // *dst must be 8 aligned
static int json_parse_object(const char **buf, const char *buf_end, uint8_t **restrict dst, static int json_parse_object(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) {
const uint8_t *dst_end) {
// Esnure enough space for the header // Esnure enough space for the header
if (*dst + sizeof(JSONHeader) >= dst_end) { if (*dst + sizeof(JSONHeader) >= dst_end) {
return set_jerrno(DstOverflow); return set_jerrno(DstOverflow);
@ -557,8 +550,7 @@ static int json_parse_object(const char **buf, const char *buf_end, uint8_t **re
} }
// *dst must be 8 aligned // *dst must be 8 aligned
static int json_parse_value(const char **buf, const char *buf_end, uint8_t **restrict dst, static int json_parse_value(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) {
const uint8_t *dst_end) {
for (; *buf < buf_end; (*buf)++) { for (; *buf < buf_end; (*buf)++) {
// Ignore initial whitespaces // Ignore initial whitespaces
if (is_whitespace(**buf)) if (is_whitespace(**buf))
@ -714,7 +706,7 @@ static inline bool ends_with(const char *str, const char *pat) {
static void json_adapt_set_defaults(const JSONAdapter *adapter, void *ptr) { static void json_adapt_set_defaults(const JSONAdapter *adapter, void *ptr) {
if (!is_primitive(adapter)) { if (!is_primitive(adapter)) {
for (int i = 0; i < adapter->prop_count; i++) { for (int i = 0; i < adapter->prop_count; i++) {
uint8_t *p = (uint8_t*)ptr + adapter->props[i].offset; uint8_t *p = (uint8_t *)ptr + adapter->props[i].offset;
if (ends_with(adapter->props[i].path, "[]")) { if (ends_with(adapter->props[i].path, "[]")) {
*(size_t *)(p + sizeof(void *)) = 0; *(size_t *)(p + sizeof(void *)) = 0;
@ -736,8 +728,8 @@ static void json_adapt_set_defaults(const JSONAdapter *adapter, void *ptr) {
// path_buffer: points to the begining of the path buffer // path_buffer: points to the begining of the path buffer
// full_path: points to the "current" path // full_path: points to the "current" path
// path: points to the end of the current path (most of the times) // path: points to the end of the current path (most of the times)
static void json_adapt_priv(uint8_t **buf, const JSONAdapter *adapter, void *ptr, char *path_buffer, static void json_adapt_priv(uint8_t **buf, const JSONAdapter *adapter, void *ptr, char *path_buffer, char *full_path,
char *full_path, char *path) { char *path) {
JSONHeader *header = (JSONHeader *)*buf; JSONHeader *header = (JSONHeader *)*buf;
if (is_primitive(adapter)) { if (is_primitive(adapter)) {
@ -789,8 +781,8 @@ static void json_adapt_priv(uint8_t **buf, const JSONAdapter *adapter, void *ptr
for (int i = 0; i < adapter->prop_count; i++) { for (int i = 0; i < adapter->prop_count; i++) {
if (strcmp(adapter->props[i].path, full_path) == 0) { if (strcmp(adapter->props[i].path, full_path) == 0) {
uint8_t *p = (uint8_t*)ptr + adapter->props[i].offset; uint8_t *p = (uint8_t *)ptr + adapter->props[i].offset;
size_t size = adapter->props[i].type->size; size_t size = adapter->props[i].type->size;
if (header->type == Array) { if (header->type == Array) {
uint8_t *array_buf = *buf + sizeof(JSONHeader); uint8_t *array_buf = *buf + sizeof(JSONHeader);
@ -807,8 +799,7 @@ static void json_adapt_priv(uint8_t **buf, const JSONAdapter *adapter, void *ptr
for (size_t index = 0; index < len; index++) { for (size_t index = 0; index < len; index++) {
path[0] = '.'; path[0] = '.';
path[1] = '\0'; path[1] = '\0';
json_adapt_priv(&array_buf, adapter->props[i].type, array_ptr + index * size, path_buffer, json_adapt_priv(&array_buf, adapter->props[i].type, array_ptr + index * size, path_buffer, path, path);
path, path);
path[0] = '\0'; path[0] = '\0';
} }

14
json.h
View File

@ -2,9 +2,9 @@
#ifndef JSON_H_ #ifndef JSON_H_
#define JSON_H_ #define JSON_H_
#include <limits.h> #include <limits.h>
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
typedef struct __attribute__((packed, aligned(8))) { typedef struct __attribute__((packed, aligned(8))) {
uint32_t type; uint32_t type;
@ -38,8 +38,6 @@ typedef enum {
JERRORNO_MAX = 10 JERRORNO_MAX = 10
} JSONError; } JSONError;
struct JSONAdapter;
typedef struct { typedef struct {
char *path; char *path;
const struct JSONAdapter *type; const struct JSONAdapter *type;
@ -83,14 +81,8 @@ static const char *JSONErrorMessage[JERRORNO_MAX + 1] = {
"?", "?",
}; };
static const char * JSONTypeName[7] = { static const char *JSONTypeName[7] = {
"[Unknown]", "[Unknown]", "String", "Number", "Object", "Array", "Boolean", "Null",
"String",
"Number",
"Object",
"Array",
"Boolean",
"Null",
}; };
const JSONAdapter NumberAdapter = { const JSONAdapter NumberAdapter = {

1
main.c
View File

@ -1,5 +1,4 @@
#include "client.h" #include "client.h"
#include "hid.h"
#include "server.h" #include "server.h"
#include "util.h" #include "util.h"

34
net.c
View File

@ -2,19 +2,19 @@
#include "util.h" #include "util.h"
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
// Deserialize the message in buf, buf must be at least 4 aligned. Returns -1 on error, otherwise returns 0 // Deserialize the message in buf, buf must be at least 4 aligned. Returns -1 on error, otherwise returns 0
// and writes result to dst // and writes result to dst
int msg_deserialize(const uint8_t *buf, size_t len, Message *restrict dst) { int msg_deserialize(const uint8_t *buf, size_t len, Message *restrict dst) {
{ {
if(len <= MAGIC_SIZE) { if (len <= MAGIC_SIZE) {
return -1; return -1;
} }
if(*(MAGIC_TYPE*)buf != MAGIC_BEG) { if (*(MAGIC_TYPE *)buf != MAGIC_BEG) {
printf("NET: No magic in message\n"); printf("NET: No magic in message\n");
return -1; return -1;
} }
@ -26,9 +26,9 @@ int msg_deserialize(const uint8_t *buf, size_t len, Message *restrict dst) {
if (len-- < 1) if (len-- < 1)
return -1; return -1;
// This ensures that only a byte is read instead of a full enum value // This ensures that only a byte is read instead of a full enum value
uint8_t code_byte = buf[0]; uint8_t code_byte = buf[0];
MessageCode code = (MessageCode)code_byte; MessageCode code = (MessageCode)code_byte;
uint32_t size = 0; uint32_t size = 0;
uint16_t abs, rel, key, index, *buf16; uint16_t abs, rel, key, index, *buf16;
@ -129,7 +129,7 @@ int msg_deserialize(const uint8_t *buf, size_t len, Message *restrict dst) {
dst->controller_state.big_rumble = buf[8]; dst->controller_state.big_rumble = buf[8];
dst->controller_state.flash_on = buf[9]; dst->controller_state.flash_on = buf[9];
dst->controller_state.flash_off = buf[10]; dst->controller_state.flash_off = buf[10];
size = MSS_CONTROLLER_STATE + 1; size = MSS_CONTROLLER_STATE + 1;
buf += size; buf += size;
break; break;
case Request: { case Request: {
@ -170,7 +170,7 @@ int msg_deserialize(const uint8_t *buf, size_t len, Message *restrict dst) {
} }
dst->request.requests = tags; dst->request.requests = tags;
size = expected_len + 1; size = expected_len + 1;
break; break;
} }
case DeviceDestroy: case DeviceDestroy:
@ -179,18 +179,18 @@ int msg_deserialize(const uint8_t *buf, size_t len, Message *restrict dst) {
dst->code = code; dst->code = code;
dst->destroy.index = *(uint16_t *)(buf + 2); dst->destroy.index = *(uint16_t *)(buf + 2);
size = MSS_DESTROY + 1; size = MSS_DESTROY + 1;
buf += size; buf += size;
break; break;
default: default:
return -1; return -1;
} }
if(size + MAGIC_SIZE > len + 1) { if (size + MAGIC_SIZE > len + 1) {
return -1; return -1;
} }
if(*(MAGIC_TYPE*)buf != MAGIC_END) { if (*(MAGIC_TYPE *)buf != MAGIC_END) {
printf("NET: Magic not found\n"); printf("NET: Magic not found\n");
return -1; return -1;
} }
@ -204,7 +204,7 @@ int msg_serialize(uint8_t *restrict buf, size_t len, const Message *msg) {
if (len < MAGIC_SIZE * 2 + 1) if (len < MAGIC_SIZE * 2 + 1)
return -1; return -1;
*(MAGIC_TYPE*)buf = MAGIC_BEG; *(MAGIC_TYPE *)buf = MAGIC_BEG;
buf += MAGIC_SIZE; buf += MAGIC_SIZE;
len -= MAGIC_SIZE + 1; len -= MAGIC_SIZE + 1;
@ -305,7 +305,7 @@ int msg_serialize(uint8_t *restrict buf, size_t len, const Message *msg) {
buf[8] = msg->controller_state.big_rumble; buf[8] = msg->controller_state.big_rumble;
buf[9] = msg->controller_state.flash_on; buf[9] = msg->controller_state.flash_on;
buf[10] = msg->controller_state.flash_off; buf[10] = msg->controller_state.flash_off;
size = MSS_CONTROLLER_STATE + 1; size = MSS_CONTROLLER_STATE + 1;
buf += size; buf += size;
break; break;
case Request: { case Request: {
@ -347,7 +347,7 @@ int msg_serialize(uint8_t *restrict buf, size_t len, const Message *msg) {
buf[0] = (uint8_t)msg->code; buf[0] = (uint8_t)msg->code;
*(uint16_t *)(buf + 2) = msg->controller_state.index; *(uint16_t *)(buf + 2) = msg->controller_state.index;
size = MSS_DESTROY + 1; size = MSS_DESTROY + 1;
buf += size; buf += size;
break; break;
default: default:
@ -355,11 +355,11 @@ int msg_serialize(uint8_t *restrict buf, size_t len, const Message *msg) {
return -1; return -1;
} }
if(size + MAGIC_SIZE > len) { if (size + MAGIC_SIZE > len) {
return -1; return -1;
} }
*(MAGIC_TYPE*)buf = MAGIC_END; *(MAGIC_TYPE *)buf = MAGIC_END;
return size + MAGIC_SIZE * 2; return size + MAGIC_SIZE * 2;
} }
@ -373,7 +373,7 @@ void msg_free(Message *msg) {
} }
} }
void print_message_buffer(const uint8_t * buf, int len) { void print_message_buffer(const uint8_t *buf, int len) {
bool last_beg = false; bool last_beg = false;
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
if (i + MAGIC_SIZE <= len) { if (i + MAGIC_SIZE <= len) {

4
net.h
View File

@ -1,6 +1,8 @@
// vi:ft=c // vi:ft=c
#ifndef NET_H_ #ifndef NET_H_
#define NET_H_ #define NET_H_
#include "util.h"
#include <linux/input-event-codes.h> #include <linux/input-event-codes.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
@ -104,6 +106,6 @@ typedef union {
int msg_deserialize(const uint8_t *buf, size_t len, Message *restrict dst); int msg_deserialize(const uint8_t *buf, size_t len, Message *restrict dst);
int msg_serialize(uint8_t *restrict buf, size_t len, const Message *msg); int msg_serialize(uint8_t *restrict buf, size_t len, const Message *msg);
void msg_free(Message *msg); void msg_free(Message *msg);
void print_message_buffer(const uint8_t * buf, int len); void print_message_buffer(const uint8_t *buf, int len);
#endif #endif

View File

@ -9,12 +9,12 @@
#include <linux/input-event-codes.h> #include <linux/input-event-codes.h>
#include <linux/input.h> #include <linux/input.h>
#include <math.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <poll.h> #include <poll.h>
#include <pthread.h> #include <pthread.h>
#include <signal.h> #include <signal.h>
#include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -114,7 +114,7 @@ void *device_thread(void *args_) {
while (true) { while (true) {
*args->controller = NULL; *args->controller = NULL;
Controller *ctr = get_device(args->tag, &args->conn->closed); Controller *ctr = get_device(args->tag, &args->conn->closed);
if(ctr == NULL) { if (ctr == NULL) {
break; break;
} }
*args->controller = ctr; *args->controller = ctr;

View File

@ -14,7 +14,7 @@ typedef struct {
int32_t product; int32_t product;
bool js; bool js;
// NULL means no filter // NULL means no filter
char * name; char *name;
} ControllerFilter; } ControllerFilter;
typedef struct { typedef struct {

13
util.c
View File

@ -6,6 +6,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#ifndef __has_builtin #ifndef __has_builtin
#define __has_builtin(_) 0 #define __has_builtin(_) 0
@ -169,12 +170,12 @@ void tsf_hex_to_color(void *arg, void *ptr) {
free(s); free(s);
} }
void tsf_num_to_u8_clamp(void * arg, void *ptr) { void tsf_num_to_u8_clamp(void *arg, void *ptr) {
double n = *(double*)arg; double n = *(double *)arg;
*(uint8_t*)ptr = n > 255.0 ? 255.0 : n < 0.0 ? 0.0 : n; *(uint8_t *)ptr = n > 255.0 ? 255.0 : n < 0.0 ? 0.0 : n;
} }
void tsf_num_to_int(void * arg, void *ptr) { void tsf_num_to_int(void *arg, void *ptr) {
double n = *(double*)arg; double n = *(double *)arg;
*(int*)ptr = n; *(int *)ptr = n;
} }

6
util.h
View File

@ -33,9 +33,9 @@ void tsf_numsec_to_timespec(void *arg, void *ptr);
void tsf_numsec_to_intms(void *arg, void *ptr); void tsf_numsec_to_intms(void *arg, void *ptr);
void tsf_uniq_to_u64(void *arg, void *ptr); void tsf_uniq_to_u64(void *arg, void *ptr);
void tsf_hex_to_i32(void *arg, void *ptr); void tsf_hex_to_i32(void *arg, void *ptr);
void tsf_double_to_size(void * arg, void * ptr); void tsf_double_to_size(void *arg, void *ptr);
void tsf_hex_to_color(void *arg, void *ptr); void tsf_hex_to_color(void *arg, void *ptr);
void tsf_num_to_u8_clamp(void * arg, void *ptr); void tsf_num_to_u8_clamp(void *arg, void *ptr);
void tsf_num_to_int(void * arg, void *ptr); void tsf_num_to_int(void *arg, void *ptr);
#endif #endif

5
vec.c
View File

@ -1,5 +1,6 @@
#include "vec.h" #include "vec.h"
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -40,7 +41,7 @@ static inline void vec_grow(Vec *v, size_t cap) {
void vec_push(Vec *v, void *data) { void vec_push(Vec *v, void *data) {
vec_grow(v, v->len + 1); vec_grow(v, v->len + 1);
memcpy((u_int8_t *)v->data + v->stride * v->len++, data, v->stride); memcpy((uint8_t *)v->data + v->stride * v->len++, data, v->stride);
} }
void vec_pop(Vec *v, void *data) { void vec_pop(Vec *v, void *data) {
@ -103,7 +104,7 @@ void vec_extend(Vec *v, void *data, size_t len) {
return; return;
} }
vec_grow(v, v->len + len); vec_grow(v, v->len + len);
memcpy((uint8_t*)v->data + v->stride * v->len, data, v->stride * len); memcpy((uint8_t *)v->data + v->stride * v->len, data, v->stride * len);
v->len += len; v->len += len;
} }

8
vec.h
View File

@ -1,16 +1,16 @@
// vi:ft=c // vi:ft=c
#ifndef VEC_H_ #ifndef VEC_H_
#define VEC_H_ #define VEC_H_
#include <unistd.h>
#include <stdint.h> #include <stdint.h>
#include <unistd.h>
#define vec_of(type) vec_new(sizeof(type)) #define vec_of(type) vec_new(sizeof(type))
typedef struct { typedef struct {
uint8_t *data; uint8_t *data;
size_t cap; size_t cap;
size_t len; size_t len;
size_t stride; size_t stride;
} Vec; } Vec;
// Create a new vector // Create a new vector