housekeeping

This commit is contained in:
viandoxdev 2022-08-30 19:08:36 +02:00
parent 2297685cd6
commit 5bdf9f4c58
No known key found for this signature in database
GPG Key ID: AF1410C5BC10AA25
10 changed files with 67 additions and 83 deletions

View File

@ -1,6 +1,4 @@
#include<stdint.h>
#include "client.h"
#include <stdint.h>
void client_run(char * address, uint16_t port) {
}
void client_run(char *address, uint16_t port) {}

View File

@ -3,6 +3,6 @@
#define CLIENT_H
#include <stdint.h>
void client_run(char * address, uint16_t port);
void client_run(char *address, uint16_t port);
#endif

13
hid.c
View File

@ -1,17 +1,16 @@
#include <dirent.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/joystick.h>
#include <linux/uinput.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <time.h>
#include <unistd.h>
#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);

2
hid.h
View File

@ -2,9 +2,7 @@
#ifndef HID_H
#define HID_H
#include "net.h"
#include "vec.h"
#include <linux/input.h>
#include <pthread.h>
#include <stdint.h>
typedef uint64_t uniq_t;

10
main.c
View File

@ -1,13 +1,8 @@
#include <fcntl.h>
#include <linux/joystick.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#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);
}

View File

@ -1,20 +1,19 @@
#include <arpa/inet.h>
#include <fcntl.h>
#include <linux/input.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <poll.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <unistd.h>
#include "hid.h"
#include "net.h"
#include "util.h"
#include "vec.h"
struct Connection {
int socket;

View File

@ -1,7 +1,7 @@
// vi: set ft=c
#ifndef SERVER_H
#define SERVER_H
#include<stdint.h>
#include <stdint.h>
void server_run(uint16_t port);

7
util.c
View File

@ -1,7 +1,6 @@
#include<limits.h>
#include<stdarg.h>
#include<stdio.h>
#include<stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "util.h"

66
vec.c
View File

@ -1,7 +1,7 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "vec.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INIT_CAP 8
@ -10,9 +10,7 @@ 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;
@ -23,78 +21,78 @@ Vec vec_cap(size_t data_size, size_t initial_capacity) {
return v;
}
static inline void vec_grow(Vec * v, size_t cap) {
if(v->cap >= cap) return;
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();
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;
}
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); }

19
vec.h
View File

@ -1,13 +1,12 @@
// vi: set ft=c
#ifndef VEC_H
#define VEC_H
#include<unistd.h>
#include<stdint.h>
#include <unistd.h>
#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);