housekeeping
This commit is contained in:
parent
cf4b5d6eb0
commit
c6bdbf7fd8
6
Makefile
6
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
|
||||
|
|
18
client.c
18
client.c
|
@ -1,4 +1,5 @@
|
|||
#include "client.h"
|
||||
|
||||
#include "const.h"
|
||||
#include "json.h"
|
||||
#include "net.h"
|
||||
|
@ -10,7 +11,6 @@
|
|||
#include <linux/input-event-codes.h>
|
||||
#include <linux/input.h>
|
||||
#include <linux/uinput.h>
|
||||
#include <math.h>
|
||||
#include <netinet/in.h>
|
||||
#include <poll.h>
|
||||
#include <stdbool.h>
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
1
hid.h
1
hid.h
|
@ -5,6 +5,7 @@
|
|||
#include "server.h"
|
||||
|
||||
#include <linux/input-event-codes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// Unique identifier for devices (provided by linux), May be the mac address
|
||||
|
|
35
json.c
35
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);
|
||||
|
@ -208,8 +207,7 @@ 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
|
||||
} 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,7 +781,7 @@ 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;
|
||||
uint8_t *p = (uint8_t *)ptr + adapter->props[i].offset;
|
||||
size_t size = adapter->props[i].type->size;
|
||||
|
||||
if (header->type == Array) {
|
||||
|
@ -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';
|
||||
}
|
||||
|
||||
|
|
14
json.h
14
json.h
|
@ -2,9 +2,9 @@
|
|||
#ifndef JSON_H_
|
||||
#define JSON_H_
|
||||
#include <limits.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
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 = {
|
||||
|
|
1
main.c
1
main.c
|
@ -1,5 +1,4 @@
|
|||
#include "client.h"
|
||||
#include "hid.h"
|
||||
#include "server.h"
|
||||
#include "util.h"
|
||||
|
||||
|
|
18
net.c
18
net.c
|
@ -2,19 +2,19 @@
|
|||
|
||||
#include "util.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
@ -186,11 +186,11 @@ int msg_deserialize(const uint8_t *buf, size_t len, Message *restrict dst) {
|
|||
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;
|
||||
|
||||
|
@ -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) {
|
||||
|
|
4
net.h
4
net.h
|
@ -1,6 +1,8 @@
|
|||
// vi:ft=c
|
||||
#ifndef NET_H_
|
||||
#define NET_H_
|
||||
#include "util.h"
|
||||
|
||||
#include <linux/input-event-codes.h>
|
||||
#include <stdint.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_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
|
||||
|
|
4
server.c
4
server.c
|
@ -9,12 +9,12 @@
|
|||
|
||||
#include <linux/input-event-codes.h>
|
||||
#include <linux/input.h>
|
||||
#include <math.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <poll.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -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;
|
||||
|
|
2
server.h
2
server.h
|
@ -14,7 +14,7 @@ typedef struct {
|
|||
int32_t product;
|
||||
bool js;
|
||||
// NULL means no filter
|
||||
char * name;
|
||||
char *name;
|
||||
} ControllerFilter;
|
||||
|
||||
typedef struct {
|
||||
|
|
13
util.c
13
util.c
|
@ -6,6 +6,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
|
6
util.h
6
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
|
||||
|
|
5
vec.c
5
vec.c
|
@ -1,5 +1,6 @@
|
|||
#include "vec.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.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) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue