summaryrefslogtreecommitdiff
path: root/handle_devices.c
diff options
context:
space:
mode:
authorClay Smith <claysmith158@gmail.com>2023-02-01 18:48:57 -0600
committerClay Smith <claysmith158@gmail.com>2023-02-01 18:48:57 -0600
commit038cca642bc8627162bdf50b132396595f9f73ea (patch)
treeb9387f606c55e673419124dd5125b507f63c4aa4 /handle_devices.c
parentf2bea22f979015b439f0f9347f2fad8c7babc481 (diff)
Done minux adding new controllers?
Diffstat (limited to 'handle_devices.c')
-rw-r--r--handle_devices.c75
1 files changed, 74 insertions, 1 deletions
diff --git a/handle_devices.c b/handle_devices.c
index 3a746bb..2164cec 100644
--- a/handle_devices.c
+++ b/handle_devices.c
@@ -2,6 +2,7 @@
#include <stdio.h>
#include <stdarg.h>
#include "global_defs.h"
+#include <stdbool.h>
void open_wii(int* controller_event_fptrs, uint64_t* connected_controllers) {
//Determine if WII REMOTE is connected via bluetooth
@@ -107,7 +108,6 @@ void open_xbox_360(int* controller_event_fptrs, uint64_t* connected_controllers)
void close_unneeded_files(int num_exceptions, int controller_event_fptrs[], ...) {
- //int events_to_skip[num_exceptions] = { -1 };
int* events_to_skip = (int *) malloc(sizeof(int) * num_exceptions);
va_list list_of_args;
va_start(list_of_args, controller_event_fptrs);
@@ -150,3 +150,76 @@ void print_one_event(int controller_event_fptrs[], int index, int event_number)
}
fprintf(stderr, "DISCONNECTED\n");
}
+
+void print_multiple_events(int num_exceptions, int controller_event_fptrs[], ...) {
+ pid_t pids[num_exceptions];
+ //set all pids to -1 at start
+ for (int i = 0; i < num_exceptions; ++i) { pids[i] = -1; }
+ //get all the variadic pairs sent in and store them in an array of the appropriate size
+ struct event_pairs pairs[num_exceptions];
+ va_list list_of_args;
+ va_start(list_of_args, controller_event_fptrs);
+ for (int i = 0; i < num_exceptions; ++i) {
+ pairs[i] = va_arg(list_of_args, struct event_pairs);
+ }
+ //prepare to start printing inputs
+ struct input_event event[num_exceptions];
+ int event_size = sizeof(struct input_event);
+ //create the amount of processes appropriate to the number of events to print
+ for (int i = 0; i < num_exceptions - 1; ++i) {
+ pids[i] = fork();
+ if (pids[i] == 0) break;
+ }
+ //mark the parent
+ bool isParent = true;
+ for (int i = 0; i < num_exceptions; ++i) {
+ if (pids[i] == 0) isParent &= false;
+ }
+ //find and run code just for the parent process (first event (a.k.a. index 0))
+ if (isParent) {
+ //printf("\nIm the parent, pid: %d\n", getpid());
+ //put the code you want the parent to run here?
+ int initial_read = read(controller_event_fptrs[pairs[0].event_fptr_index], &event[0], event_size);
+ if (initial_read == -1) {
+ fprintf(stderr, "PROBLEM READING FILE!\n");
+ exit(-1);
+ }
+ size_t start_sec = event[0].time.tv_sec;
+ while (read(controller_event_fptrs[pairs[0].event_fptr_index], &event[0], event_size) != -1) {
+ if (event[0].type == 0) continue;
+ printf("%d %zu.%-6zu %d %3d %9d\n",
+ pairs[0].event_user_num,
+ event[0].time.tv_sec - start_sec,
+ event[0].time.tv_usec,
+ event[0].type,
+ event[0].code,
+ event[0].value);
+ }
+ fprintf(stderr, "DISCONNECTED\n");
+ }
+ //find the child process(es) and run the correct code for each (event #2 onwards) (a.k.a. index 1+))
+ sleep(1);
+ for (int i = 0; i < num_exceptions; ++i) {
+ if (pids[i] == 0) {
+ //printf("Im child #%d.\n", i + 1);
+ //put the rest of the code in here?
+ int initial_read = read(controller_event_fptrs[pairs[i+1].event_fptr_index], &event[i+1], event_size);
+ if (initial_read == -1) {
+ fprintf(stderr, "PROBLEM READING FILE!\n");
+ exit(-1);
+ }
+ size_t start_sec = event[i+1].time.tv_sec;
+ while (read(controller_event_fptrs[pairs[i+1].event_fptr_index], &event[i+1], event_size) != -1) {
+ if (event[i+1].type == 0) continue;
+ printf("%d %zu.%-6zu %d %3d %9d\n",
+ pairs[i+1].event_user_num,
+ event[i+1].time.tv_sec - start_sec,
+ event[i+1].time.tv_usec,
+ event[i+1].type,
+ event[i+1].code,
+ event[i+1].value);
+ }
+ fprintf(stderr, "DISCONNECTED\n");
+ }
+ }
+}