diff options
Diffstat (limited to 'handle_devices.c')
-rw-r--r-- | handle_devices.c | 95 |
1 files changed, 68 insertions, 27 deletions
diff --git a/handle_devices.c b/handle_devices.c index f7ae0c9..7481f61 100644 --- a/handle_devices.c +++ b/handle_devices.c @@ -3,8 +3,7 @@ #include <stdarg.h> #include "global_defs.h" #include <stdbool.h> - - +#include <sys/mman.h> void open_wii(int* controller_event_fptrs, uint64_t* connected_controllers, int choice) { //Determine if WII REMOTE is connected via bluetooth @@ -145,27 +144,37 @@ 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); - time_t start_sec; - char buffer[BUFSIZE]; - int string_length; - if (-1 == read(controller_event_fptrs[index], &event1, event_size)) { + size_t start_sec; + int initial_read = read(controller_event_fptrs[index], &event1, event_size); + if (initial_read == -1) { fprintf(stderr, "PROBLEM READING FILE!\n"); exit(-1); } + char buffer[BUFSIZE+1] = {0}; start_sec = event1.time.tv_sec; while (read(controller_event_fptrs[index], &event1, event_size) != -1) { if (event1.type == 0) continue; - string_length = snprintf(buffer, BUFSIZE - 1, "%d %5lu.%06lu %d %3d %11d\n", +/* + printf("%d %lu.%06lu %d %3d %11d\n", event_number, (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); +*/ +///* + snprintf(buffer, BUFSIZE, "%d %lu.%06lu %d %3d %11d\n", + event_number, + (unsigned long) event1.time.tv_sec - start_sec, + (unsigned long) event1.time.tv_usec, + event1.type, + event1.code, + event1.value); + write(STDOUT_FILENO, buffer, sizeof(buffer)); +//*/ } fprintf(stderr, "DISCONNECTED\n"); - exit(-1); } void print_multiple_events(int num_exceptions, int controller_event_fptrs[], ...) { @@ -182,6 +191,14 @@ void print_multiple_events(int num_exceptions, int controller_event_fptrs[], ... //prepare to start printing inputs struct input_event event[num_exceptions]; int event_size = sizeof(struct input_event); + int read_time = read(controller_event_fptrs[pairs[0].event_fptr_index], &event[0], event_size); //set a start_sec variable to be used across processes for consistent time + if (read_time == -1) { + fprintf(stderr, "PROBLEM READING FILE FOR TIME!\n"); + exit(-1); + } + unsigned long start_sec = event[0].time.tv_sec; + //create shared memory to hold the value of the microseconds to be shared among all of the forked processes + unsigned long* shared_usec = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); //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(); @@ -194,47 +211,71 @@ 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) { - char buffer[BUFSIZE]; - int string_length; - if ( -1 == read(controller_event_fptrs[pairs[0].event_fptr_index], &event[0], event_size)) { + char buffer[BUFSIZE+1]; + 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) { + *shared_usec = event[0].time.tv_usec; if (event[0].type == 0) continue; - string_length = snprintf(buffer, BUFSIZE, "%d %lu.%06lu %d %3d %11d\n", +///* + printf("%d %lu.%-6lu %d %3d %11d\n", + pairs[0].event_user_num, + event[0].time.tv_sec - start_sec, + *shared_usec, + event[0].type, + event[0].code, + event[0].value); +//*/ +/* + snprintf(buffer, BUFSIZE, "%d %lu.%-6lu %d %3d %11d\n", pairs[0].event_user_num, - (unsigned long) event[0].time.tv_sec - start_sec, - (unsigned long) event[0].time.tv_usec, + event[0].time.tv_sec - start_sec, + event[0].time.tv_usec, event[0].type, event[0].code, event[0].value); - write(STDOUT_FILENO, buffer, string_length); + write(STDOUT_FILENO, buffer, sizeof(buffer)); +*/ } 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); + //sleep(1); find a way to make sure the time is consistent...shared memory using mmap(NULL, PAGESIZE, PROT_READ | PROT_WRITE); for time? ??? for (int i = 0; i < num_exceptions; ++i) { if (pids[i] == 0) { - char buffer[BUFSIZE]; - int string_length; - if (-1 == read(controller_event_fptrs[pairs[i+1].event_fptr_index], &event[i+1], event_size)) { + //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) { 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; - string_length = snprintf(buffer, BUFSIZE, "%d %lu.%06lu %d %3d %11d\n", +///* + printf("%d %zu.%-6zu %d %3d %11d\n", + pairs[i+1].event_user_num, + event[i+1].time.tv_sec - start_sec, + *shared_usec, + //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", pairs[i+1].event_user_num, - (unsigned long) event[i+1].time.tv_sec - start_sec, - (unsigned long) event[i+1].time.tv_usec, + 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); - write(STDOUT_FILENO, buffer, string_length); + write(STDOUT_FILENO, buffer, sizeof(buffer)); +*/ } fprintf(stderr, "DISCONNECTED\n"); } |