jsfw/main.c

85 lines
1.7 KiB
C
Raw Normal View History

2022-08-29 17:27:03 -05:00
#include<stdio.h>
#include<sys/ioctl.h>
#include<linux/joystick.h>
#include<stdint.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
#include<stdarg.h>
#include<unistd.h>
#include<pthread.h>
2022-08-27 19:29:43 -05:00
#include "main.h"
#include "hid.h"
2022-08-29 17:27:03 -05:00
#include "server.h"
2022-08-27 19:29:43 -05:00
const char* USAGE[] = {
2022-08-29 17:27:03 -05:00
"jsfw client [address] [port]\n",
2022-08-27 19:29:43 -05:00
"jsfw server [port]\n",
};
const size_t EVENT_SIZE = sizeof(struct js_event);
void panicf(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
exit(1);
}
uint16_t parse_port(const char * str) {
long long n = atoll(str);
2022-08-29 17:27:03 -05:00
if(n <= 0 || n > UINT16_MAX)
panicf("Invalid port: Expected a number in the range 1..%d, got '%s'\n", UINT16_MAX, str);
2022-08-27 19:29:43 -05:00
return n;
}
void server(uint16_t port) {
printf("Server (port: %u).\n", port);
2022-08-29 17:27:03 -05:00
pthread_t _;
pthread_create(&_, NULL, hid_thread, NULL);
2022-08-27 19:29:43 -05:00
2022-08-29 17:27:03 -05:00
server_run(port);
}
2022-08-27 19:29:43 -05:00
2022-08-29 17:27:03 -05:00
void client(char * address, uint16_t port) {
printf("JSFW Client (%s:%d)\n", address, port);
2022-08-27 19:29:43 -05:00
}
int main(int argc, char* argv[]) {
if(argc < 2) {
printf("Usage: %s", USAGE[0]);
printf(" %s", USAGE[1]);
return 1;
}
char* mode = argv[1];
if(strcmp(mode, "server") == 0) {
if(argc < 3)
panicf("Usage: %s", USAGE[1]);
uint16_t port = parse_port(argv[2]);
server(port);
} else if(strcmp(mode, "client") == 0) {
2022-08-29 17:27:03 -05:00
if(argc < 4)
2022-08-27 19:29:43 -05:00
panicf("Usage: %s", USAGE[0]);
2022-08-29 17:27:03 -05:00
char * address = argv[2];
uint16_t port = parse_port(argv[3]);
client(address, port);
2022-08-27 19:29:43 -05:00
} else {
printf("Unknown mode: '%s'\n", mode);
printf("Usage: %s", USAGE[0]);
printf(" %s", USAGE[1]);
return 1;
}
return 0;
}