diff --git a/Makefile b/Makefile deleted file mode 100644 index abb316a..0000000 --- a/Makefile +++ /dev/null @@ -1,19 +0,0 @@ -#CFLAGS= -g -O0 -D _DEFAULT_SOURCE -D_POSIX_C_SOURCE=200112L -std=c11 -fPIE -pedantic -Wall # -Werror -CFLAGS= -std=c17 -O3 -march=znver4 -D _DEFAULT_SOURCE -D_POSIX_C_SOURCE=200112L -std=c11 -fPIE -pedantic -Wall -flto=full # -Werror -LDFLAGS=-lpthread -levdev -ludev -lconfig -lrt -lm -flto=full -CC=clang -OBJECTS=main.o dev_in.o dev_out.o dev_iio.o dev_evdev.o dev_hidraw.o settings.o virt_ds4.o virt_ds5.o devices_status.o xbox360.o rog_ally.o legion_go.o rogue_enemy.o -TARGET=rogue-enemy - -all: $(TARGET) - -$(TARGET): $(OBJECTS) - $(CC) $(LDFLAGS) $(OBJECTS) -o $@ - -include depends - -depends: - $(CC) -MM $(OBJECTS:.o=.c) > depends - -clean: - rm -f ./$(TARGET) *.o depends \ No newline at end of file diff --git a/dev_in.c b/dev_in.c index e1f5a69..600e68b 100644 --- a/dev_in.c +++ b/dev_in.c @@ -334,10 +334,22 @@ static void handle_rumble(dev_in_t *const in_devs, size_t in_devs_count, const o } } -static int open_socket(void) { +static int open_socket(struct sockaddr_un *serveraddr) { int res = -ENODEV; + int sd = socket(AF_UNIX, SOCK_SEQPACKET, 0); + if (sd < 0) + { + res = sd; + goto open_socket_err; + } + res = connect(sd, (struct sockaddr *)serveraddr, SUN_LEN(serveraddr)); + if (res < 0) { + goto open_socket_err; + } + + res = sd; open_socket_err: return res; @@ -378,11 +390,11 @@ void* dev_in_thread_func(void *ptr) { if (dev_in_data->communication.type == ipc_unix_pipe) { FD_SET(dev_in_data->communication.endpoint.pipe.out_message_pipe_fd, &read_fds); } else if (dev_in_data->communication.type == ipc_client_socket) { - dev_in_data->communication.endpoint.socket = open_socket(); + dev_in_data->communication.endpoint.socket.fd = open_socket(&dev_in_data->communication.endpoint.socket.serveraddr); // do not do a thing! that will consume messages and they won't be available anymore! - if (dev_in_data->communication.endpoint.socket < 0) { - fprintf(stderr, "Unable to connect to server: %d -- will retry connection\n", dev_in_data->communication.endpoint.socket); + if (dev_in_data->communication.endpoint.socket.fd < 0) { + fprintf(stderr, "Unable to connect to server: %d -- will retry connection\n", dev_in_data->communication.endpoint.socket.fd); usleep(500000); continue; } @@ -466,7 +478,7 @@ void* dev_in_thread_func(void *ptr) { // in case of an error reschedule to socket for reconnection if (dev_in_data->communication.type == ipc_client_socket) { close(out_message_fd); - dev_in_data->communication.endpoint.socket = -1; + dev_in_data->communication.endpoint.socket.fd = -1; } } } @@ -529,7 +541,7 @@ void* dev_in_thread_func(void *ptr) { if (controller_msg_count > 0) { int in_message_fd = -1; if (dev_in_data->communication.type == ipc_client_socket) { - in_message_fd = dev_in_data->communication.endpoint.socket; + in_message_fd = dev_in_data->communication.endpoint.socket.fd; } else if (dev_in_data->communication.type == ipc_unix_pipe) { in_message_fd = dev_in_data->communication.endpoint.pipe.in_message_pipe_fd; } @@ -541,7 +553,7 @@ void* dev_in_thread_func(void *ptr) { // in case of an error reschedule to socket for reconnection if (dev_in_data->communication.type == ipc_client_socket) { close(in_message_fd); - dev_in_data->communication.endpoint.socket = -1; + dev_in_data->communication.endpoint.socket.fd = -1; } } } diff --git a/ipc.h b/ipc.h index c298a80..9e9f994 100644 --- a/ipc.h +++ b/ipc.h @@ -5,9 +5,14 @@ #define MAX_CONNECTED_CLIENTS 8 typedef struct ipc_strategy_socket { + struct sockaddr_un serveraddr; + int fd; +} ipc_strategy_socket_t; + +typedef struct ipc_strategy_ssocket { pthread_mutex_t mutex; int clients[MAX_CONNECTED_CLIENTS]; -} ipc_strategy_socket_t; +} ipc_strategy_ssocket_t; typedef struct ipc_strategy_pipe { @@ -29,10 +34,10 @@ typedef struct ipc { ipc_strategy_t type; union { ipc_strategy_pipe_t pipe; - ipc_strategy_socket_t ssocket; - int socket; + ipc_strategy_ssocket_t ssocket; + ipc_strategy_socket_t socket; } endpoint; } ipc_t; -#define SERVER_PATH "/tmp/server.sock" \ No newline at end of file +#define SERVER_PATH "/tmp/rogue-enemy.sock" \ No newline at end of file diff --git a/main.c b/main.c index d4eee0f..5015ca7 100644 --- a/main.c +++ b/main.c @@ -4,6 +4,7 @@ #include "input_dev.h" #include "dev_in.h" #include "dev_out.h" +#include "ipc.h" #include "settings.h" #include "rog_ally.h" @@ -62,10 +63,18 @@ int main(int argc, char ** argv) { .communication = { .type = ipc_client_socket, .endpoint = { - .socket = -1, + .socket = { + .fd = -1, + .serveraddr = { + .sun_path = SERVER_PATH, + .sun_family = AF_UNIX, + } + }, } } }; + + //memset(&dev_in_thread_data.communication.endpoint.socket.serveraddr, 0, sizeof(dev_in_thread_data.communication.endpoint.socket.serveraddr)); pthread_t dev_in_thread; const int dev_in_thread_creation = pthread_create(&dev_in_thread, NULL, dev_in_thread_func, (void*)(&dev_in_thread_data)); diff --git a/rogue_iio.c b/rogue_iio.c new file mode 100644 index 0000000..e69de29 diff --git a/stray_ally.c b/stray_ally.c index dbb0c0c..eda4648 100644 --- a/stray_ally.c +++ b/stray_ally.c @@ -60,10 +60,9 @@ int main(int argc, char ** argv) { } int sd=-1; - int rc, length; struct sockaddr_un serveraddr; do { - sd = socket(AF_UNIX, SOCK_STREAM, 0); + sd = socket(AF_UNIX, SOCK_SEQPACKET, 0); if (sd < 0) { fprintf(stderr, "socket() failed"); diff --git a/stray_output.c b/stray_output.c new file mode 100644 index 0000000..e69de29