2022-08-30 11:45:53 -05:00
|
|
|
#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"
|
2022-08-27 19:29:43 -05:00
|
|
|
#include "hid.h"
|
2022-08-30 11:45:53 -05:00
|
|
|
#include "main.h"
|
2022-08-29 17:27:03 -05:00
|
|
|
#include "server.h"
|
2022-08-30 08:37:34 -05:00
|
|
|
#include "util.h"
|
2022-08-27 19:29:43 -05:00
|
|
|
|
2022-08-30 11:45:53 -05:00
|
|
|
const char *USAGE[] = {
|
|
|
|
"jsfw client [address] [port]\n",
|
|
|
|
"jsfw server [port]\n",
|
2022-08-27 19:29:43 -05:00
|
|
|
};
|
|
|
|
const size_t EVENT_SIZE = sizeof(struct js_event);
|
|
|
|
|
2022-08-30 11:45:53 -05:00
|
|
|
uint16_t parse_port(const char *str) {
|
2022-08-27 19:29:43 -05:00
|
|
|
long long n = atoll(str);
|
2022-08-30 11:45:53 -05:00
|
|
|
if (n <= 0 || n > UINT16_MAX)
|
2022-08-29 17:27:03 -05:00
|
|
|
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) {
|
2022-08-30 11:45:53 -05:00
|
|
|
printf("Server (port: %u).\n\n", port);
|
2022-08-27 19:29:43 -05:00
|
|
|
|
2022-08-30 08:37:34 -05:00
|
|
|
pthread_t thread;
|
|
|
|
pthread_create(&thread, NULL, hid_thread, NULL);
|
2022-08-29 17:27:03 -05:00
|
|
|
server_run(port);
|
|
|
|
}
|
2022-08-27 19:29:43 -05:00
|
|
|
|
2022-08-30 11:45:53 -05:00
|
|
|
void client(char *address, uint16_t port) {
|
|
|
|
printf("Client (%s:%d)\n\n", address, port);
|
|
|
|
client_run(address, port);
|
2022-08-27 19:29:43 -05:00
|
|
|
}
|
|
|
|
|
2022-08-30 11:45:53 -05:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
if (argc < 2) {
|
2022-08-27 19:29:43 -05:00
|
|
|
printf("Usage: %s", USAGE[0]);
|
|
|
|
printf(" %s", USAGE[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-08-30 11:45:53 -05:00
|
|
|
char *mode = argv[1];
|
2022-08-27 19:29:43 -05:00
|
|
|
|
2022-08-30 11:45:53 -05:00
|
|
|
if (strcmp(mode, "server") == 0) {
|
2022-08-27 19:29:43 -05:00
|
|
|
|
2022-08-30 11:45:53 -05:00
|
|
|
if (argc < 3)
|
2022-08-27 19:29:43 -05:00
|
|
|
panicf("Usage: %s", USAGE[1]);
|
|
|
|
|
|
|
|
uint16_t port = parse_port(argv[2]);
|
|
|
|
server(port);
|
|
|
|
|
2022-08-30 11:45:53 -05:00
|
|
|
} else if (strcmp(mode, "client") == 0) {
|
2022-08-27 19:29:43 -05:00
|
|
|
|
2022-08-30 11:45:53 -05:00
|
|
|
if (argc < 4)
|
2022-08-27 19:29:43 -05:00
|
|
|
panicf("Usage: %s", USAGE[0]);
|
|
|
|
|
2022-08-30 11:45:53 -05:00
|
|
|
char *address = argv[2];
|
|
|
|
uint16_t port = parse_port(argv[3]);
|
2022-08-29 17:27:03 -05:00
|
|
|
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;
|
|
|
|
}
|