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 "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 #define CLIENT_H
#include <stdint.h> #include <stdint.h>
void client_run(char * address, uint16_t port); void client_run(char *address, uint16_t port);
#endif #endif

13
hid.c
View File

@ -1,17 +1,16 @@
#include <dirent.h> #include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
#include <linux/input.h> #include <linux/input.h>
#include <linux/joystick.h> #include <pthread.h>
#include <linux/uinput.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include "hid.h" #include "hid.h"
#include "main.h"
#include "vec.h" #include "vec.h"
// List of uniq of the currently known devices // 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.rel_count = 0;
dev->device_info.key_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; 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; 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; dev->mapping.key_indices[i] = -1;
uint8_t bits[EV_MAX] = {}; uint8_t bits[EV_MAX] = {};
@ -304,7 +303,7 @@ void apply_controller_state(PhysicalDevice *dev, MessageControllerState *state)
buf[10] = state->flash_off; buf[10] = state->flash_off;
write(dev->hidraw, buf, 32); 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); fsync(dev->hidraw);
// Send a second time because it doesn't work otherwise // Send a second time because it doesn't work otherwise
write(dev->hidraw, buf, 32); write(dev->hidraw, buf, 32);

2
hid.h
View File

@ -2,9 +2,7 @@
#ifndef HID_H #ifndef HID_H
#define HID_H #define HID_H
#include "net.h" #include "net.h"
#include "vec.h"
#include <linux/input.h> #include <linux/input.h>
#include <pthread.h>
#include <stdint.h> #include <stdint.h>
typedef uint64_t uniq_t; 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 <pthread.h>
#include <stdarg.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "client.h" #include "client.h"
#include "hid.h" #include "hid.h"
@ -19,7 +14,6 @@ const char *USAGE[] = {
"jsfw client [address] [port]\n", "jsfw client [address] [port]\n",
"jsfw server [port]\n", "jsfw server [port]\n",
}; };
const size_t EVENT_SIZE = sizeof(struct js_event);
uint16_t parse_port(const char *str) { uint16_t parse_port(const char *str) {
long long n = atoll(str); long long n = atoll(str);
@ -29,7 +23,7 @@ uint16_t parse_port(const char *str) {
} }
void server(uint16_t port) { void server(uint16_t port) {
printf("Server (port: %u).\n\n", port); printf("[Server (port: %u)]\n\n", port);
pthread_t thread; pthread_t thread;
pthread_create(&thread, NULL, hid_thread, NULL); pthread_create(&thread, NULL, hid_thread, NULL);
@ -37,7 +31,7 @@ void server(uint16_t port) {
} }
void client(char *address, 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); client_run(address, port);
} }

View File

@ -1,20 +1,19 @@
#include <arpa/inet.h>
#include <fcntl.h>
#include <linux/input.h> #include <linux/input.h>
#include <netinet/in.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <poll.h>
#include <pthread.h> #include <pthread.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h>
#include "hid.h" #include "hid.h"
#include "net.h" #include "net.h"
#include "util.h" #include "util.h"
#include "vec.h"
struct Connection { struct Connection {
int socket; int socket;
@ -58,7 +57,7 @@ void *server_handle_conn(void *args_) {
MessageDeviceReport report = {}; MessageDeviceReport report = {};
report.code = DeviceReport; report.code = DeviceReport;
report.abs_count = dev.device_info.abs_count; report.abs_count = dev.device_info.abs_count;
report.rel_count = dev.device_info.rel_count; report.rel_count = dev.device_info.rel_count;
report.key_count = dev.device_info.key_count; report.key_count = dev.device_info.key_count;

View File

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

7
util.c
View File

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

76
vec.c
View File

@ -1,7 +1,7 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "vec.h" #include "vec.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INIT_CAP 8 #define INIT_CAP 8
@ -10,91 +10,89 @@ static void handle_alloc_error() {
exit(2); exit(2);
} }
Vec vec_new(size_t data_size) { Vec vec_new(size_t data_size) { return vec_cap(data_size, INIT_CAP); }
return vec_cap(data_size, INIT_CAP);
}
Vec vec_cap(size_t data_size, size_t initial_capacity) { Vec vec_cap(size_t data_size, size_t initial_capacity) {
Vec v; Vec v;
v.cap = initial_capacity; v.cap = initial_capacity;
v.len = 0; v.len = 0;
v.stride = data_size; v.stride = data_size;
v.data = malloc(data_size * initial_capacity); v.data = malloc(data_size * initial_capacity);
return v; return v;
} }
static inline void vec_grow(Vec * v, size_t cap) { static inline void vec_grow(Vec *v, size_t cap) {
if(v->cap >= cap) return; if (v->cap >= cap)
size_t new_cap = cap > v->cap * 2 ? cap : v->cap * 2; return;
void * new_data = realloc(v->data, new_cap * v->stride); size_t new_cap = cap > v->cap * 2 ? cap : v->cap * 2;
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->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); vec_grow(v, v->len + 1);
memcpy(v->data + v->stride * v->len++, data, v->stride); memcpy(v->data + v->stride * v->len++, data, v->stride);
} }
void vec_pop(Vec * v, void * data) { void vec_pop(Vec *v, void *data) {
if(v->len == 0) { if (v->len == 0) {
printf("ERR(vec_pop): Trying to pop an element from an empty vector\n"); printf("ERR(vec_pop): Trying to pop an element from an empty vector\n");
return; return;
} }
if(data != NULL) if (data != NULL)
memcpy(data, v->data + v->stride * (v->len - 1), v->stride); memcpy(data, v->data + v->stride * (v->len - 1), v->stride);
v->len--; v->len--;
} }
void * vec_get(Vec * v, size_t index) { void *vec_get(Vec *v, size_t index) {
if(index >= v->len) return NULL; if (index >= v->len)
return NULL;
return v->data + index * v->stride; return v->data + index * v->stride;
} }
void vec_insert(Vec * v, void * data, size_t index) { void vec_insert(Vec *v, void *data, size_t index) {
if(index > v->len) { if (index > v->len) {
printf("ERR(vec_insert): Trying to insert past the end of the vector.\n"); printf("ERR(vec_insert): Trying to insert past the end of the vector.\n");
return; return;
} }
vec_grow(v, v->len + 1); vec_grow(v, v->len + 1);
void * slot = v->data + index * v->stride; void *slot = v->data + index * v->stride;
if(index < v->len) { if (index < v->len) {
memmove(slot + v->stride, slot, (v->len - index) * v->stride); memmove(slot + v->stride, slot, (v->len - index) * v->stride);
} }
memcpy(slot, data, v->stride); memcpy(slot, data, v->stride);
v->len++; v->len++;
} }
void vec_remove(Vec * v, size_t index, void * data) { void vec_remove(Vec *v, size_t index, void *data) {
if(v->len == 0) { if (v->len == 0) {
printf("ERR(vec_remove): Trying to remove an element from an empty vector\n"); printf("ERR(vec_remove): Trying to remove an element from an empty vector\n");
return; return;
} }
if(index >= v->len) { if (index >= v->len) {
printf("ERR(vec_remove): Trying to remove past the end of the vector\n"); printf("ERR(vec_remove): Trying to remove past the end of the vector\n");
return; return;
} }
void * slot = v->data + index * v->stride; void *slot = v->data + index * v->stride;
if(data != NULL) if (data != NULL)
memcpy(data, slot, v->stride); memcpy(data, slot, v->stride);
if(index < --v->len) if (index < --v->len)
memmove(slot, slot + v->stride, (v->len - index) * v->stride); memmove(slot, slot + v->stride, (v->len - index) * v->stride);
} }
void vec_clear(Vec * v) { void vec_clear(Vec *v) { v->len = 0; }
v->len = 0;
}
void vec_extend(Vec * v, void * data, size_t len) { void vec_extend(Vec *v, void *data, size_t len) {
if(len == 0) return; if (len == 0)
return;
vec_grow(v, v->len + len); vec_grow(v, v->len + len);
memcpy(v->data + v->stride * v->len, data, v->stride * len); memcpy(v->data + v->stride * v->len, data, v->stride * len);
v->len += len; v->len += len;
} }
void vec_free(Vec v) { void vec_free(Vec v) { free(v.data); }
free(v.data);
}

19
vec.h
View File

@ -1,13 +1,12 @@
// vi: set ft=c // vi: set ft=c
#ifndef VEC_H #ifndef VEC_H
#define VEC_H #define VEC_H
#include<unistd.h> #include <unistd.h>
#include<stdint.h>
#define vec_of(type) vec_new(sizeof(type)) #define vec_of(type) vec_new(sizeof(type))
typedef struct { typedef struct {
void * data; void *data;
size_t cap; size_t cap;
size_t len; size_t len;
size_t stride; size_t stride;
@ -18,19 +17,19 @@ Vec vec_new(size_t data_size);
// Create a new vector with an initial capacity // Create a new vector with an initial capacity
Vec vec_cap(size_t data_size, size_t initial_capacity); Vec vec_cap(size_t data_size, size_t initial_capacity);
// Push an element into the vector // 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 // 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 // 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) // 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 // 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 // Clear the vector
void vec_clear(Vec * v); void vec_clear(Vec *v);
// Extend the vector with the content of data // 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 // Free the vector
void vec_free(Vec v); void vec_free(Vec v);