From 5a6ab9faf253d3a25c559ec13db081d956fe706e Mon Sep 17 00:00:00 2001 From: Clay Smith Date: Tue, 7 Feb 2023 01:13:52 -0600 Subject: Done except fix scanf issue and add more controllers. --- Makefile | 2 +- controller_prog | Bin 0 -> 45464 bytes handle_devices.c | 73 ++++++++++++++++++++----------------------------------- main.c | 5 ++-- student.c | 22 ++++++----------- test | Bin 0 -> 27728 bytes 6 files changed, 38 insertions(+), 64 deletions(-) create mode 100644 controller_prog create mode 100644 test diff --git a/Makefile b/Makefile index 12106c3..197df31 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC = gcc ALL_FLAGS = -g main: main.c handle_devices.c - ${CC} ${ALL_FLAGS} main.c handle_devices.c + ${CC} ${ALL_FLAGS} main.c handle_devices.c -o controller_prog student: student.c ${CC} ${ALL_FLAGS} student.c -o test diff --git a/controller_prog b/controller_prog new file mode 100644 index 0000000..879db89 Binary files /dev/null and b/controller_prog differ diff --git a/handle_devices.c b/handle_devices.c index f32b85e..f7ae0c9 100644 --- a/handle_devices.c +++ b/handle_devices.c @@ -4,6 +4,8 @@ #include "global_defs.h" #include + + void open_wii(int* controller_event_fptrs, uint64_t* connected_controllers, int choice) { //Determine if WII REMOTE is connected via bluetooth int wii[4] = { 0 }; @@ -143,24 +145,27 @@ SKIP_CLOSE: void print_one_event(int controller_event_fptrs[], int index, int event_number) { struct input_event event1; int event_size = sizeof(struct input_event); - size_t start_sec; - int initial_read = read(controller_event_fptrs[index], &event1, event_size); - if (initial_read == -1) { + time_t start_sec; + char buffer[BUFSIZE]; + int string_length; + if (-1 == read(controller_event_fptrs[index], &event1, event_size)) { fprintf(stderr, "PROBLEM READING FILE!\n"); exit(-1); } start_sec = event1.time.tv_sec; while (read(controller_event_fptrs[index], &event1, event_size) != -1) { if (event1.type == 0) continue; - printf("%d %zu.%-6zu %d %3d %9d\n", + string_length = snprintf(buffer, BUFSIZE - 1, "%d %5lu.%06lu %d %3d %11d\n", event_number, - event1.time.tv_sec - start_sec, - event1.time.tv_usec, + (unsigned long) event1.time.tv_sec - start_sec, + (unsigned long) event1.time.tv_usec, event1.type, event1.code, event1.value); + write(STDOUT_FILENO, buffer, string_length); } fprintf(stderr, "DISCONNECTED\n"); + exit(-1); } void print_multiple_events(int num_exceptions, int controller_event_fptrs[], ...) { @@ -189,73 +194,47 @@ void print_multiple_events(int num_exceptions, int controller_event_fptrs[], ... } //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? - char buffer[BUFSIZE+1]; - int initial_read = read(controller_event_fptrs[pairs[0].event_fptr_index], &event[0], event_size); - if (initial_read == -1) { + char buffer[BUFSIZE]; + int string_length; + if ( -1 == read(controller_event_fptrs[pairs[0].event_fptr_index], &event[0], event_size)) { 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 %11d\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); -*/ - - snprintf(buffer, BUFSIZE, "%d %zu.%-6zu %d %3d %11d\n", + string_length = snprintf(buffer, BUFSIZE, "%d %lu.%06lu %d %3d %11d\n", pairs[0].event_user_num, - event[0].time.tv_sec - start_sec, - event[0].time.tv_usec, + (unsigned long) event[0].time.tv_sec - start_sec, + (unsigned long) event[0].time.tv_usec, event[0].type, event[0].code, event[0].value); - write(STDOUT_FILENO, buffer, sizeof(buffer)); - + write(STDOUT_FILENO, buffer, string_length); } 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? - char buffer[BUFSIZE+1]; - int initial_read = read(controller_event_fptrs[pairs[i+1].event_fptr_index], &event[i+1], event_size); - if (initial_read == -1) { + char buffer[BUFSIZE]; + int string_length; + if (-1 == read(controller_event_fptrs[pairs[i+1].event_fptr_index], &event[i+1], event_size)) { 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 %11d\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); - -/* - snprintf(buffer, BUFSIZE, "%d %zu.%-6zu %d %3d %11d\n", + string_length = snprintf(buffer, BUFSIZE, "%d %lu.%06lu %d %3d %11d\n", pairs[i+1].event_user_num, - event[i+1].time.tv_sec - start_sec, - event[i+1].time.tv_usec, + (unsigned long) event[i+1].time.tv_sec - start_sec, + (unsigned long) event[i+1].time.tv_usec, event[i+1].type, event[i+1].code, event[i+1].value); - write(STDOUT_FILENO, buffer, sizeof(buffer)); -*/ + write(STDOUT_FILENO, buffer, string_length); } fprintf(stderr, "DISCONNECTED\n"); } diff --git a/main.c b/main.c index 06e5f95..ce63cd3 100644 --- a/main.c +++ b/main.c @@ -24,11 +24,12 @@ int main(int argc, char** argv) if (!choice) { if (connected_controllers == 0) { - fprintf(stderr, "No device matching the programs requirments (DualShock 4, Xbox 360, Valve Steam, or Wii Controller) was found, please double check the connection if you do not think that this is the case.\n"); + fprintf(stderr, "No device matching the programs requirements (DualShock 4, Xbox 360, Valve Steam, or Wii Controller) was found, please double check the connection if you do not think that this is the case.\n"); return(-1); } - fprintf(stderr, "\n\nPress the number corresponding to the device above that you would like to use and then press ENTER.\n"); + fprintf(stderr, "\n\nUse the same command with a space followed by the number corresponding to the input listed above that you want to use.\n\n"); + //fprintf(stderr, "\n\nIf no devices are listed above, then the program failed to find an acceptable device connected to the computer.\n\n"); } else { struct input_event event1, event2, event3, event4; diff --git a/student.c b/student.c index 908daa9..b0437fc 100644 --- a/student.c +++ b/student.c @@ -1,24 +1,18 @@ #include -#include #include "user_defs.h" int main(void) { - char buffer[100]; - int num = -1, type = -1, code = -1, value = -1; - double time = -1; - int scan = -2; - char c; + char buffer[100]; fgets(buffer, 99, stdin); //mandatory to fix controller programs input stream, not needed if they use fgets and sscanf instead of scanf + + int num = -2, type = -2, code = -2, value = -2; + double time = -2; + + while (1) { - fgets(buffer, 99, stdin); - //printf("%s", buffer); - //if (buffer[0] == '1') { - // printf("first value is 1\n"); - // } - // scan = sscanf(buffer, "%d %lf %d %d %d\n", &num, &time, &type, &code, &value); - // printf("%d\n", scan); - scanf("%d %lf %d %d %d\n", &num, &time, &type, &code, &value); + scanf("%d %lf %d %d %d", &num, &time, &type, &code, &value); printf("%d, %lf, %d, %d, %d\n", num, time, type, code, value); } + return 0; } diff --git a/test b/test new file mode 100644 index 0000000..0b54f7a Binary files /dev/null and b/test differ -- cgit v1.2.1