diff --git a/Makefile b/Makefile index b5006bb..5599e40 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ Q=@ -CC=clang +CC=gcc GCCCFLAGS=-Wno-format-truncation -CFLAGS=-std=c11 -pedantic -g -Wall -pthread -D_GNU_SOURCE -fsanitize=undefined -LDFLAGS=-lm -fsanitize=undefined +CFLAGS=-std=c11 -pedantic -g -Wall -pthread -D_GNU_SOURCE +LDFLAGS=-lm BUILD_DIR=./objects BIN=jsfw diff --git a/client.c b/client.c index 441fc59..0b83afa 100644 --- a/client.c +++ b/client.c @@ -1,4 +1,5 @@ #include "client.h" + #include "const.h" #include "json.h" #include "net.h" @@ -10,7 +11,6 @@ #include #include #include -#include #include #include #include @@ -138,7 +138,7 @@ void device_init(MessageDeviceInfo *dev) { device_destroy(dev->index); - int fd = *(int*)vec_get(&devices_fd, dev->index); + int fd = *(int *)vec_get(&devices_fd, dev->index); // Abs if (dev->abs_count > 0) { @@ -186,19 +186,20 @@ void device_init(MessageDeviceInfo *dev) { ioctl(fd, UI_DEV_SETUP, &setup); 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)); - 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 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; } - int fd = *(int*) vec_get(&devices_fd, index); + int fd = *(int *)vec_get(&devices_fd, index); struct input_event event = {0}; event.type = type; @@ -215,10 +216,9 @@ void device_handle_report(MessageDeviceReport *report) { 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 || - report->key_count != info->key_count) { + if (report->abs_count != info->abs_count || report->rel_count != info->rel_count || report->key_count != info->key_count) { printf("CLIENT: Report doesn't match with device info\n"); return; } diff --git a/client.h b/client.h index ba7e7ce..ec2d7f9 100644 --- a/client.h +++ b/client.h @@ -15,7 +15,7 @@ typedef struct { typedef struct { ClientController *controllers; - size_t controller_count; + size_t controller_count; char *fifo_path; struct timespec retry_delay; diff --git a/hid.h b/hid.h index c3b2395..89d5490 100644 --- a/hid.h +++ b/hid.h @@ -5,6 +5,7 @@ #include "server.h" #include +#include #include // Unique identifier for devices (provided by linux), May be the mac address diff --git a/json.c b/json.c index c619dad..4d54324 100644 --- a/json.c +++ b/json.c @@ -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 // *dst must be 8 aligned -static inline int json_parse_string(const char **buf, const char *buf_end, uint8_t **restrict dst, - const uint8_t *dst_end) { +static inline int json_parse_string(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) { // Ensure enough space for the header if (*dst + sizeof(JSONHeader) >= dst_end) { 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)++ = 0x80 | (un_codepoint >> 6 & 0x3F); - *(*dst)++ = 0x80 | (un_codepoint >> 0 & 0x3F); + *(*dst)++ = 0x80 | (un_codepoint >> 6 & 0x3F); + *(*dst)++ = 0x80 | (un_codepoint >> 0 & 0x3F); header->len += 3; } else if (un_codepoint <= 0x10ffff) { // 4 byte codepoint 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)++ = 0x80 | (un_codepoint >> 12 & 0x3F); - *(*dst)++ = 0x80 | (un_codepoint >> 6 & 0x3F); - *(*dst)++ = 0x80 | (un_codepoint >> 0 & 0x3F); + *(*dst)++ = 0x80 | (un_codepoint >> 6 & 0x3F); + *(*dst)++ = 0x80 | (un_codepoint >> 0 & 0x3F); header->len += 4; } else { // Illegal codepoint return set_jerrno(StringBadUnicode); @@ -208,9 +207,8 @@ static inline int json_parse_string(const char **buf, const char *buf_end, uint8 (*buf)++; return 0; - } else if ((c < ' ' && c != '\t') || - c == 0x7f) { // Illegal characters, technically tab isn't allowed either - // but it felt weird so I added it + } else if ((c < ' ' && c != '\t') || c == 0x7f) { // Illegal characters, technically tab isn't allowed either + // but it felt weird so I added it jerrno = StringBadChar; 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 -static int json_parse_number(const char **buf, const char *buf_end, uint8_t **restrict dst, - const uint8_t *dst_end) { +static int json_parse_number(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) { // Ensure enough space for header and value if (*dst + sizeof(JSONHeader) + sizeof(double) >= dst_end) { 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 -static int json_parse_boolean(const char **buf, const char *buf_end, uint8_t **restrict dst, - const uint8_t *dst_end) { +static int json_parse_boolean(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) { // Ensure enough space for header and value if (*dst + sizeof(JSONHeader) + 8 >= dst_end) { // 8: sizeof(uint64_t) 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 -static int json_parse_null(const char **buf, const char *buf_end, uint8_t **restrict dst, - const uint8_t *dst_end) { +static int json_parse_null(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) { // Ensure enough size for the header (no value) if (*dst + sizeof(JSONHeader) >= dst_end) { 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 -static int json_parse_array(const char **buf, const char *buf_end, uint8_t **restrict dst, - const uint8_t *dst_end) { +static int json_parse_array(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) { // Ensure enough space for the header if (*dst + sizeof(JSONHeader) >= dst_end) { 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 -static int json_parse_object(const char **buf, const char *buf_end, uint8_t **restrict dst, - const uint8_t *dst_end) { +static int json_parse_object(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) { // Esnure enough space for the header if (*dst + sizeof(JSONHeader) >= dst_end) { 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 -static int json_parse_value(const char **buf, const char *buf_end, uint8_t **restrict dst, - const uint8_t *dst_end) { +static int json_parse_value(const char **buf, const char *buf_end, uint8_t **restrict dst, const uint8_t *dst_end) { for (; *buf < buf_end; (*buf)++) { // Ignore initial whitespaces 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) { if (!is_primitive(adapter)) { 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, "[]")) { *(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 // full_path: points to the "current" path // 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, - char *full_path, char *path) { +static void json_adapt_priv(uint8_t **buf, const JSONAdapter *adapter, void *ptr, char *path_buffer, char *full_path, + char *path) { JSONHeader *header = (JSONHeader *)*buf; 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++) { if (strcmp(adapter->props[i].path, full_path) == 0) { - uint8_t *p = (uint8_t*)ptr + adapter->props[i].offset; - size_t size = adapter->props[i].type->size; + uint8_t *p = (uint8_t *)ptr + adapter->props[i].offset; + size_t size = adapter->props[i].type->size; if (header->type == Array) { 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++) { path[0] = '.'; path[1] = '\0'; - json_adapt_priv(&array_buf, adapter->props[i].type, array_ptr + index * size, path_buffer, - path, path); + json_adapt_priv(&array_buf, adapter->props[i].type, array_ptr + index * size, path_buffer, path, path); path[0] = '\0'; } diff --git a/json.h b/json.h index 0a298d7..1790b01 100644 --- a/json.h +++ b/json.h @@ -2,9 +2,9 @@ #ifndef JSON_H_ #define JSON_H_ #include +#include #include #include -#include typedef struct __attribute__((packed, aligned(8))) { uint32_t type; @@ -38,8 +38,6 @@ typedef enum { JERRORNO_MAX = 10 } JSONError; -struct JSONAdapter; - typedef struct { char *path; const struct JSONAdapter *type; @@ -83,14 +81,8 @@ static const char *JSONErrorMessage[JERRORNO_MAX + 1] = { "?", }; -static const char * JSONTypeName[7] = { - "[Unknown]", - "String", - "Number", - "Object", - "Array", - "Boolean", - "Null", +static const char *JSONTypeName[7] = { + "[Unknown]", "String", "Number", "Object", "Array", "Boolean", "Null", }; const JSONAdapter NumberAdapter = { diff --git a/main.c b/main.c index eeb9c6b..966b92d 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,4 @@ #include "client.h" -#include "hid.h" #include "server.h" #include "util.h" diff --git a/net.c b/net.c index 76c93aa..adf3e6d 100644 --- a/net.c +++ b/net.c @@ -2,19 +2,19 @@ #include "util.h" +#include #include #include - // Deserialize the message in buf, buf must be at least 4 aligned. Returns -1 on error, otherwise returns 0 // and writes result to dst int msg_deserialize(const uint8_t *buf, size_t len, Message *restrict dst) { { - if(len <= MAGIC_SIZE) { + if (len <= MAGIC_SIZE) { return -1; } - if(*(MAGIC_TYPE*)buf != MAGIC_BEG) { + if (*(MAGIC_TYPE *)buf != MAGIC_BEG) { printf("NET: No magic in message\n"); return -1; } @@ -26,9 +26,9 @@ int msg_deserialize(const uint8_t *buf, size_t len, Message *restrict dst) { if (len-- < 1) return -1; // This ensures that only a byte is read instead of a full enum value - uint8_t code_byte = buf[0]; - MessageCode code = (MessageCode)code_byte; - uint32_t size = 0; + uint8_t code_byte = buf[0]; + MessageCode code = (MessageCode)code_byte; + uint32_t size = 0; 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.flash_on = buf[9]; dst->controller_state.flash_off = buf[10]; - size = MSS_CONTROLLER_STATE + 1; + size = MSS_CONTROLLER_STATE + 1; buf += size; break; case Request: { @@ -170,7 +170,7 @@ int msg_deserialize(const uint8_t *buf, size_t len, Message *restrict dst) { } dst->request.requests = tags; - size = expected_len + 1; + size = expected_len + 1; break; } case DeviceDestroy: @@ -179,18 +179,18 @@ int msg_deserialize(const uint8_t *buf, size_t len, Message *restrict dst) { dst->code = code; dst->destroy.index = *(uint16_t *)(buf + 2); - size = MSS_DESTROY + 1; + size = MSS_DESTROY + 1; buf += size; break; default: return -1; } - if(size + MAGIC_SIZE > len + 1) { + if (size + MAGIC_SIZE > len + 1) { return -1; } - if(*(MAGIC_TYPE*)buf != MAGIC_END) { + if (*(MAGIC_TYPE *)buf != MAGIC_END) { printf("NET: Magic not found\n"); 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) return -1; - *(MAGIC_TYPE*)buf = MAGIC_BEG; + *(MAGIC_TYPE *)buf = MAGIC_BEG; buf += MAGIC_SIZE; 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[9] = msg->controller_state.flash_on; buf[10] = msg->controller_state.flash_off; - size = MSS_CONTROLLER_STATE + 1; + size = MSS_CONTROLLER_STATE + 1; buf += size; break; 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; *(uint16_t *)(buf + 2) = msg->controller_state.index; - size = MSS_DESTROY + 1; + size = MSS_DESTROY + 1; buf += size; break; default: @@ -355,11 +355,11 @@ int msg_serialize(uint8_t *restrict buf, size_t len, const Message *msg) { return -1; } - if(size + MAGIC_SIZE > len) { + if (size + MAGIC_SIZE > len) { return -1; } - *(MAGIC_TYPE*)buf = MAGIC_END; + *(MAGIC_TYPE *)buf = MAGIC_END; 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; for (int i = 0; i < len; i++) { if (i + MAGIC_SIZE <= len) { diff --git a/net.h b/net.h index 625e416..181fd86 100644 --- a/net.h +++ b/net.h @@ -1,6 +1,8 @@ // vi:ft=c #ifndef NET_H_ #define NET_H_ +#include "util.h" + #include #include #include @@ -104,6 +106,6 @@ typedef union { 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); 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 diff --git a/server.c b/server.c index 94d2164..187069d 100644 --- a/server.c +++ b/server.c @@ -9,12 +9,12 @@ #include #include -#include #include #include #include #include #include +#include #include #include #include @@ -114,7 +114,7 @@ void *device_thread(void *args_) { while (true) { *args->controller = NULL; Controller *ctr = get_device(args->tag, &args->conn->closed); - if(ctr == NULL) { + if (ctr == NULL) { break; } *args->controller = ctr; diff --git a/server.h b/server.h index 99fbbec..5886dce 100644 --- a/server.h +++ b/server.h @@ -14,7 +14,7 @@ typedef struct { int32_t product; bool js; // NULL means no filter - char * name; + char *name; } ControllerFilter; typedef struct { diff --git a/util.c b/util.c index a845ba1..1f52f42 100644 --- a/util.c +++ b/util.c @@ -6,6 +6,7 @@ #include #include #include +#include #ifndef __has_builtin #define __has_builtin(_) 0 @@ -169,12 +170,12 @@ void tsf_hex_to_color(void *arg, void *ptr) { free(s); } -void tsf_num_to_u8_clamp(void * arg, void *ptr) { - double n = *(double*)arg; - *(uint8_t*)ptr = n > 255.0 ? 255.0 : n < 0.0 ? 0.0 : n; +void tsf_num_to_u8_clamp(void *arg, void *ptr) { + double n = *(double *)arg; + *(uint8_t *)ptr = n > 255.0 ? 255.0 : n < 0.0 ? 0.0 : n; } -void tsf_num_to_int(void * arg, void *ptr) { - double n = *(double*)arg; - *(int*)ptr = n; +void tsf_num_to_int(void *arg, void *ptr) { + double n = *(double *)arg; + *(int *)ptr = n; } diff --git a/util.h b/util.h index e724f2d..5be02f6 100644 --- a/util.h +++ b/util.h @@ -33,9 +33,9 @@ void tsf_numsec_to_timespec(void *arg, void *ptr); void tsf_numsec_to_intms(void *arg, void *ptr); void tsf_uniq_to_u64(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_num_to_u8_clamp(void * arg, void *ptr); -void tsf_num_to_int(void * arg, void *ptr); +void tsf_num_to_u8_clamp(void *arg, void *ptr); +void tsf_num_to_int(void *arg, void *ptr); #endif diff --git a/vec.c b/vec.c index fc84e26..690f31c 100644 --- a/vec.c +++ b/vec.c @@ -1,5 +1,6 @@ #include "vec.h" +#include #include #include #include @@ -40,7 +41,7 @@ static inline void vec_grow(Vec *v, size_t cap) { void vec_push(Vec *v, void *data) { 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) { @@ -103,7 +104,7 @@ void vec_extend(Vec *v, void *data, size_t len) { return; } 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; } diff --git a/vec.h b/vec.h index 62f64a8..65302e6 100644 --- a/vec.h +++ b/vec.h @@ -1,16 +1,16 @@ // vi:ft=c #ifndef VEC_H_ #define VEC_H_ -#include #include +#include #define vec_of(type) vec_new(sizeof(type)) typedef struct { uint8_t *data; - size_t cap; - size_t len; - size_t stride; + size_t cap; + size_t len; + size_t stride; } Vec; // Create a new vector