From 5bdf9f4c58e04d77018e7e5ee91078d79ba67add Mon Sep 17 00:00:00 2001 From: viandoxdev Date: Tue, 30 Aug 2022 19:08:36 +0200 Subject: [PATCH] housekeeping --- client.c | 6 ++--- client.h | 2 +- hid.c | 13 +++++----- hid.h | 2 -- main.c | 10 ++------ server.c | 13 +++++----- server.h | 2 +- util.c | 7 +++--- vec.c | 76 +++++++++++++++++++++++++++----------------------------- vec.h | 19 +++++++------- 10 files changed, 67 insertions(+), 83 deletions(-) diff --git a/client.c b/client.c index b049b8b..34f5e46 100644 --- a/client.c +++ b/client.c @@ -1,6 +1,4 @@ -#include #include "client.h" +#include -void client_run(char * address, uint16_t port) { - -} +void client_run(char *address, uint16_t port) {} diff --git a/client.h b/client.h index 6a7a14a..4eb3f0c 100644 --- a/client.h +++ b/client.h @@ -3,6 +3,6 @@ #define CLIENT_H #include -void client_run(char * address, uint16_t port); +void client_run(char *address, uint16_t port); #endif diff --git a/hid.c b/hid.c index 5b46c01..bb2ecdb 100644 --- a/hid.c +++ b/hid.c @@ -1,17 +1,16 @@ #include #include #include -#include -#include +#include #include #include #include #include #include #include +#include #include "hid.h" -#include "main.h" #include "vec.h" // List of uniq of the currently known devices @@ -57,11 +56,11 @@ void setup_device(PhysicalDevice *dev) { dev->device_info.rel_count = 0; dev->device_info.key_count = 0; - for(int i = 0; i < ABS_CNT; i++) + for (int i = 0; i < ABS_CNT; i++) dev->mapping.abs_indices[i] = -1; - for(int i = 0; i < REL_CNT; i++) + for (int i = 0; i < REL_CNT; i++) dev->mapping.key_indices[i] = -1; - for(int i = 0; i < KEY_CNT; i++) + for (int i = 0; i < KEY_CNT; i++) dev->mapping.key_indices[i] = -1; uint8_t bits[EV_MAX] = {}; @@ -304,7 +303,7 @@ void apply_controller_state(PhysicalDevice *dev, MessageControllerState *state) buf[10] = state->flash_off; write(dev->hidraw, buf, 32); - if(state->flash_on == 0 && state->flash_off == 0) { + if (state->flash_on == 0 && state->flash_off == 0) { fsync(dev->hidraw); // Send a second time because it doesn't work otherwise write(dev->hidraw, buf, 32); diff --git a/hid.h b/hid.h index 847cd27..69dae9c 100644 --- a/hid.h +++ b/hid.h @@ -2,9 +2,7 @@ #ifndef HID_H #define HID_H #include "net.h" -#include "vec.h" #include -#include #include typedef uint64_t uniq_t; diff --git a/main.c b/main.c index 0b8a64f..5fa18d0 100644 --- a/main.c +++ b/main.c @@ -1,13 +1,8 @@ -#include -#include #include -#include #include #include #include #include -#include -#include #include "client.h" #include "hid.h" @@ -19,7 +14,6 @@ const char *USAGE[] = { "jsfw client [address] [port]\n", "jsfw server [port]\n", }; -const size_t EVENT_SIZE = sizeof(struct js_event); uint16_t parse_port(const char *str) { long long n = atoll(str); @@ -29,7 +23,7 @@ uint16_t parse_port(const char *str) { } void server(uint16_t port) { - printf("Server (port: %u).\n\n", port); + printf("[Server (port: %u)]\n\n", port); pthread_t thread; pthread_create(&thread, NULL, hid_thread, NULL); @@ -37,7 +31,7 @@ void server(uint16_t port) { } void client(char *address, uint16_t port) { - printf("Client (%s:%d)\n\n", address, port); + printf("[Client (%s:%d)]\n\n", address, port); client_run(address, port); } diff --git a/server.c b/server.c index d106aae..4ccd0ba 100644 --- a/server.c +++ b/server.c @@ -1,20 +1,19 @@ -#include -#include #include +#include #include +#include #include #include #include #include +#include #include -#include -#include #include +#include #include "hid.h" #include "net.h" #include "util.h" -#include "vec.h" struct Connection { int socket; @@ -57,8 +56,8 @@ void *server_handle_conn(void *args_) { event_poll->events = POLLIN; MessageDeviceReport report = {}; - - report.code = DeviceReport; + + report.code = DeviceReport; report.abs_count = dev.device_info.abs_count; report.rel_count = dev.device_info.rel_count; report.key_count = dev.device_info.key_count; diff --git a/server.h b/server.h index 6839fa6..bdcbf07 100644 --- a/server.h +++ b/server.h @@ -1,7 +1,7 @@ // vi: set ft=c #ifndef SERVER_H #define SERVER_H -#include +#include void server_run(uint16_t port); diff --git a/util.c b/util.c index ec76976..0624439 100644 --- a/util.c +++ b/util.c @@ -1,7 +1,6 @@ -#include -#include -#include -#include +#include +#include +#include #include "util.h" diff --git a/vec.c b/vec.c index 82049bb..43daa6c 100644 --- a/vec.c +++ b/vec.c @@ -1,7 +1,7 @@ -#include -#include -#include #include "vec.h" +#include +#include +#include #define INIT_CAP 8 @@ -10,91 +10,89 @@ static void handle_alloc_error() { exit(2); } -Vec vec_new(size_t data_size) { - return vec_cap(data_size, INIT_CAP); -} +Vec vec_new(size_t data_size) { return vec_cap(data_size, INIT_CAP); } Vec vec_cap(size_t data_size, size_t initial_capacity) { Vec v; - v.cap = initial_capacity; - v.len = 0; + v.cap = initial_capacity; + v.len = 0; v.stride = data_size; - v.data = malloc(data_size * initial_capacity); + v.data = malloc(data_size * initial_capacity); return v; } -static inline void vec_grow(Vec * v, size_t cap) { - if(v->cap >= cap) return; - size_t new_cap = cap > v->cap * 2 ? cap : v->cap * 2; - void * new_data = realloc(v->data, new_cap * v->stride); - if(new_data == NULL) handle_alloc_error(); +static inline void vec_grow(Vec *v, size_t cap) { + if (v->cap >= cap) + return; + size_t new_cap = cap > v->cap * 2 ? cap : v->cap * 2; + void *new_data = realloc(v->data, new_cap * v->stride); + if (new_data == NULL) + handle_alloc_error(); v->data = new_data; - v->cap = new_cap; + v->cap = new_cap; } -void vec_push(Vec * v, void * data) { +void vec_push(Vec *v, void *data) { vec_grow(v, v->len + 1); memcpy(v->data + v->stride * v->len++, data, v->stride); } -void vec_pop(Vec * v, void * data) { - if(v->len == 0) { +void vec_pop(Vec *v, void *data) { + if (v->len == 0) { printf("ERR(vec_pop): Trying to pop an element from an empty vector\n"); return; } - if(data != NULL) + if (data != NULL) memcpy(data, v->data + v->stride * (v->len - 1), v->stride); v->len--; } -void * vec_get(Vec * v, size_t index) { - if(index >= v->len) return NULL; +void *vec_get(Vec *v, size_t index) { + if (index >= v->len) + return NULL; return v->data + index * v->stride; } -void vec_insert(Vec * v, void * data, size_t index) { - if(index > v->len) { +void vec_insert(Vec *v, void *data, size_t index) { + if (index > v->len) { printf("ERR(vec_insert): Trying to insert past the end of the vector.\n"); return; } vec_grow(v, v->len + 1); - void * slot = v->data + index * v->stride; - if(index < v->len) { + void *slot = v->data + index * v->stride; + if (index < v->len) { memmove(slot + v->stride, slot, (v->len - index) * v->stride); } memcpy(slot, data, v->stride); v->len++; } -void vec_remove(Vec * v, size_t index, void * data) { - if(v->len == 0) { +void vec_remove(Vec *v, size_t index, void *data) { + if (v->len == 0) { printf("ERR(vec_remove): Trying to remove an element from an empty vector\n"); return; } - if(index >= v->len) { + if (index >= v->len) { printf("ERR(vec_remove): Trying to remove past the end of the vector\n"); return; } - void * slot = v->data + index * v->stride; - if(data != NULL) + void *slot = v->data + index * v->stride; + if (data != NULL) memcpy(data, slot, v->stride); - if(index < --v->len) + if (index < --v->len) memmove(slot, slot + v->stride, (v->len - index) * v->stride); } -void vec_clear(Vec * v) { - v->len = 0; -} +void vec_clear(Vec *v) { v->len = 0; } -void vec_extend(Vec * v, void * data, size_t len) { - if(len == 0) return; +void vec_extend(Vec *v, void *data, size_t len) { + if (len == 0) + return; vec_grow(v, v->len + len); memcpy(v->data + v->stride * v->len, data, v->stride * len); v->len += len; } -void vec_free(Vec v) { - free(v.data); -} +void vec_free(Vec v) { free(v.data); } diff --git a/vec.h b/vec.h index fbc7c7e..af08a0b 100644 --- a/vec.h +++ b/vec.h @@ -1,13 +1,12 @@ // vi: set ft=c #ifndef VEC_H #define VEC_H -#include -#include +#include #define vec_of(type) vec_new(sizeof(type)) typedef struct { - void * data; + void *data; size_t cap; size_t len; size_t stride; @@ -18,19 +17,19 @@ Vec vec_new(size_t data_size); // Create a new vector with an initial capacity Vec vec_cap(size_t data_size, size_t initial_capacity); // Push an element into the vector -void vec_push(Vec * v, void * data); +void vec_push(Vec *v, void *data); // Pop an element into the vector, and put it in data if it is not null -void vec_pop(Vec * v, void * data); +void vec_pop(Vec *v, void *data); // Get a pointer to the element at an index, returns NULL if there is no such element -void * vec_get(Vec * v, size_t index); +void *vec_get(Vec *v, size_t index); // Insert an element at any index in the vector (except past the end) -void vec_insert(Vec * v, void * data, size_t index); +void vec_insert(Vec *v, void *data, size_t index); // Remove an element from the vector, and put it in data if it is not NULL -void vec_remove(Vec * v, size_t index, void * data); +void vec_remove(Vec *v, size_t index, void *data); // Clear the vector -void vec_clear(Vec * v); +void vec_clear(Vec *v); // Extend the vector with the content of data -void vec_extend(Vec * v, void * data, size_t len); +void vec_extend(Vec *v, void *data, size_t len); // Free the vector void vec_free(Vec v);