summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorClay Smith <claysmith158@gmail.com>2023-01-26 03:48:38 -0600
committerClay Smith <claysmith158@gmail.com>2023-01-26 03:48:38 -0600
commitd83a049ca7556ebc0a03f7f66dd7f89b0b2a72e5 (patch)
tree6a44481570ad95316096f60d01f35af5381412bf /main.c
First Commit
Diffstat (limited to 'main.c')
-rw-r--r--main.c316
1 files changed, 316 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..9f9b7dd
--- /dev/null
+++ b/main.c
@@ -0,0 +1,316 @@
+#include "global_defs.h"
+
+int main(void)
+{
+ system("clear");
+ setvbuf(stdout, NULL, _IONBF, BUFSIZ);
+
+ int controller_event_fptrs[NUM_EVENTS];
+ uint64_t connected_controllers = 0;
+
+ //Determine if WII REMOTE is connected via bluetooth
+ int wii[NUM_EVENTS_WII_PS4] = { 0 };
+ FILE* wii_pipe = popen("cat /proc/bus/input/devices | grep -A 5 \"^I: Bus=0005 Vendor=057e Product=0306\" | grep \"event\" | cut -d 't' -f2 | paste - -s", "r");
+ fscanf(wii_pipe, "%2d %2d %2d", &wii[GYRO], &wii[IR], &wii[BUTTONS]);
+ if ( wii[GYRO] != 0 && wii[IR] != 0 && wii[BUTTONS] != 0) {
+ //wii is connected, create strings of event PATHS, open event files
+ char wii_events[NUM_EVENTS_WII_PS4][26];
+ snprintf(wii_events[GYRO], 25, "/dev/input/event%d", wii[GYRO]);
+ snprintf(wii_events[IR], 25, "/dev/input/event%d", wii[IR]);
+ snprintf(wii_events[BUTTONS], 25, "/dev/input/event%d", wii[BUTTONS]);
+ controller_event_fptrs[WII_GYRO] = open(wii_events[GYRO], O_RDONLY);
+ controller_event_fptrs[WII_IR] = open(wii_events[IR], O_RDONLY);
+ controller_event_fptrs[WII_BUTTONS] = open(wii_events[BUTTONS], O_RDONLY);
+ //tell user and program that this device is available for use
+ connected_controllers |= NINTENDO_ALL_BITS;
+ printf("%4d: NINTENDO WII GYRO\n", NINTENDO_GYRO_BIT);
+ printf("%4d: NINTENDO WII IR \n", NINTENDO_IR_BIT);
+ printf("%4d: NINTENDO WII BUTTONS\n", NINTENDO_BUTTONS_BIT);
+ printf("%4d: ALL NINTENDO BT\n", NINTENDO_ALL_BITS);
+ }
+
+ //PS4 Bluetooth
+ int ps4[NUM_EVENTS_WII_PS4] = { 0 };
+ FILE* ps4_bt_pipe = popen("cat /proc/bus/input/devices | grep -A 5 \"^I: Bus=0005 Vendor=054c Product=09cc\" | grep \"event\" | cut -d 't' -f2 | paste - -s", "r");
+ fscanf(ps4_bt_pipe, "%2d %2d %2d", &ps4[TOUCHPAD], &ps4[GYRO], &ps4[BUTTONS]);
+ if ( ps4[TOUCHPAD] != 0 && ps4[GYRO] != 0 && ps4[BUTTONS] != 0) {
+ char ps4_events[NUM_EVENTS_WII_PS4][26];
+ snprintf(ps4_events[TOUCHPAD], 25, "/dev/input/event%d", ps4[TOUCHPAD]);
+ snprintf(ps4_events[GYRO], 25, "/dev/input/event%d", ps4[GYRO]);
+ snprintf(ps4_events[BUTTONS], 25, "/dev/input/event%d", ps4[BUTTONS]);
+ controller_event_fptrs[PS4_BT_TOUCH] = open(ps4_events[TOUCHPAD], O_RDONLY);
+ controller_event_fptrs[PS4_BT_GYRO] = open(ps4_events[GYRO], O_RDONLY);
+ controller_event_fptrs[PS4_BT_BUTTONS] = open(ps4_events[BUTTONS], O_RDONLY);
+ connected_controllers |= PS4_BT_ALL_BITS;
+ printf("%4d: PS4 BT TOUCH \n", PS4_BT_TOUCH_BIT);
+ printf("%4d: PS4 BT GYRO\n", PS4_BT_GYRO_BIT);
+ printf("%4d: PS4 BT BUTTONS\n", PS4_BT_BUTTONS_BIT);
+ printf("%4d: ALL PS4 BT\n", PS4_BT_ALL_BITS);
+ }
+
+ //PS4 Wired
+ controller_event_fptrs[PS4_WIRED_GYRO] = open("/dev/input/by-id/usb-Sony_Interactive_Entertainment_Wireless_Controller-event-if03", O_RDONLY);
+ controller_event_fptrs[PS4_WIRED_BUTTONS] = open("/dev/input/by-id/usb-Sony_Interactive_Entertainment_Wireless_Controller-if03-event-joystick", O_RDONLY);
+ controller_event_fptrs[PS4_WIRED_TOUCH] = open("/dev/input/by-id/usb-Sony_Interactive_Entertainment_Wireless_Controller-if03-event-mouse", O_RDONLY);
+ if (controller_event_fptrs[PS4_WIRED_GYRO] != -1 && controller_event_fptrs[PS4_WIRED_BUTTONS] != -1 && controller_event_fptrs[PS4_WIRED_TOUCH] != -1) {
+ connected_controllers |= PS4_WIRED_ALL_BITS;
+ printf("4 : PS4 WIRED TOUCH\n");
+ printf("8 : PS4 WIRED GYRO\n");
+ printf("16 : PS4 WIRED BUTTONS\n");
+ printf("28 : ALL PS4 WIRED\n");
+ }
+
+ //VALVE_STEAM WIRED
+ controller_event_fptrs[VALVE_STEAM] = open("/dev/input/by-id/usb-Valve_Software_Wired_Controller-if02-event-joystick", O_RDONLY);
+ if (controller_event_fptrs[VALVE_STEAM] != -1) {
+ connected_controllers |= 1;
+ printf("1 : VALVE STEAM\n");
+ }
+
+ //Xbox Bluetooth Dongle
+ controller_event_fptrs[XBOX_360] = open("/dev/input/by-id/usb-©Microsoft_Xbox_360_Wireless_Receiver_for_Windows_E15D4C50-event-joystick", O_RDONLY);
+ if (controller_event_fptrs[XBOX_360] != -1) {
+ connected_controllers |= 2;
+ printf("2 : XBOX 360\n");
+ }
+
+ 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");
+ exit(-1);
+ }
+
+DUMB_USER:
+ printf("\n\nPress the number corresponding to the device above that you would like to use and then press ENTER.\n");
+
+ short choice;
+ int chosen_fd[3] = {-1};
+ scanf("%d", &choice);
+
+ struct input_event event1;
+ int ievt_size = sizeof(struct input_event);
+ size_t start_sec;
+
+ switch (choice) {
+ case 1: //valve steam controller //easy
+ {
+ chosen_fd[0] = controller_event_fptrs[VALVE_STEAM];
+ for (int i = 0; i < NUM_EVENTS; ++i) {
+ if (i == VALVE_STEAM) continue;
+ close(controller_event_fptrs[i]);
+ }
+ read(chosen_fd[0], &event1, ievt_size);
+ start_sec = event1.time.tv_sec;
+ while (1) {
+ read(chosen_fd[0], &event1, ievt_size);
+ if (event1.type == 0) continue;
+ printf("%zu.%-6zu %d %3d %6d\n", event1.time.tv_sec - start_sec, event1.time.tv_usec, event1.type, event1.code, event1.value);
+ }
+ break;
+ }
+ case 2: //xbox 360 controller //easy
+ {
+ chosen_fd[0] = controller_event_fptrs[XBOX_360];
+ for (int i = 0; i < NUM_EVENTS; ++i) {
+ if (i == XBOX_360) continue;
+ close(controller_event_fptrs[i]);
+ }
+ read(chosen_fd[0], &event1, ievt_size);
+ start_sec = event1.time.tv_sec;
+ while (1) {
+ read(chosen_fd[0], &event1, ievt_size);
+ if (event1.type == 0) continue;
+ printf("%zu.%-6zu %d %3d %6d\n", event1.time.tv_sec - start_sec, event1.time.tv_usec, event1.type, event1.code, event1.value);
+ }
+ break;
+ }
+ case PS4_WIRED_TOUCH_BIT: //easy
+ {
+ chosen_fd[0] = controller_event_fptrs[PS4_WIRED_TOUCH];
+ for (int i = 0; i < NUM_EVENTS; ++i) {
+ if (i == PS4_WIRED_TOUCH) continue;
+ close(controller_event_fptrs[i]);
+ }
+ read(chosen_fd[0], &event1, ievt_size);
+ start_sec = event1.time.tv_sec;
+ while (1) {
+ read(chosen_fd[0], &event1, ievt_size);
+ if (event1.type == 0) continue;
+ printf("%zu.%-6zu %d %3d %6d\n", event1.time.tv_sec - start_sec, event1.time.tv_usec, event1.type, event1.code, event1.value);
+ }
+ break;
+ }
+ case PS4_WIRED_GYRO_BIT: //easy
+ {
+ chosen_fd[0] = controller_event_fptrs[PS4_WIRED_GYRO];
+ for (int i = 0; i < NUM_EVENTS; ++i) {
+ if (i == PS4_WIRED_GYRO) continue;
+ close(controller_event_fptrs[i]);
+ }
+ read(chosen_fd[0], &event1, ievt_size);
+ start_sec = event1.time.tv_sec;
+ while (1) {
+ read(chosen_fd[0], &event1, ievt_size);
+ if (event1.type == 0) continue;
+ printf("%zu.%-6zu %d %3d %6d\n", event1.time.tv_sec - start_sec, event1.time.tv_usec, event1.type, event1.code, event1.value);
+ }
+ break;
+ }
+ case PS4_WIRED_BUTTONS_BIT: //easy
+ {
+ chosen_fd[0] = controller_event_fptrs[PS4_WIRED_BUTTONS];
+ for (int i = 0; i < NUM_EVENTS; ++i) {
+ if (i == PS4_WIRED_BUTTONS) continue;
+ close(controller_event_fptrs[i]);
+ }
+ read(chosen_fd[0], &event1, ievt_size);
+ start_sec = event1.time.tv_sec;
+ while (1) {
+ read(chosen_fd[0], &event1, ievt_size);
+ if (event1.type == 0) continue;
+ printf("%zu.%-6zu %d %3d %6d\n", event1.time.tv_sec - start_sec, event1.time.tv_usec, event1.type, event1.code, event1.value);
+ }
+ break;
+ }
+ case PS4_WIRED_ALL_BITS: //HARD
+ {
+ struct input_event event2, event3;
+ chosen_fd[0] = controller_event_fptrs[PS4_WIRED_TOUCH];
+ chosen_fd[1] = controller_event_fptrs[PS4_WIRED_GYRO];
+ chosen_fd[2] = controller_event_fptrs[PS4_WIRED_BUTTONS];
+ for (int i = 0; i < NUM_EVENTS; ++i) {
+ if (i == PS4_WIRED_TOUCH || i == PS4_WIRED_GYRO || i == PS4_WIRED_BUTTONS) continue;
+ close(controller_event_fptrs[i]);
+ }
+ break;
+ }
+ case PS4_BT_TOUCH_BIT: //easy
+ {
+ chosen_fd[0] = controller_event_fptrs[PS4_BT_TOUCH];
+ for (int i = 0; i < NUM_EVENTS; ++i) {
+ if (i == PS4_BT_TOUCH) continue;
+ close(controller_event_fptrs[i]);
+ }
+ read(chosen_fd[0], &event1, ievt_size);
+ start_sec = event1.time.tv_sec;
+ while (1) {
+ read(chosen_fd[0], &event1, ievt_size);
+ if (event1.type == 0) continue;
+ printf("%zu.%-6zu %d %3d %6d\n", event1.time.tv_sec - start_sec, event1.time.tv_usec, event1.type, event1.code, event1.value);
+ }
+ break;
+ }
+ case PS4_BT_GYRO_BIT: //easy
+ {
+ chosen_fd[0] = controller_event_fptrs[PS4_BT_GYRO];
+ for (int i = 0; i < NUM_EVENTS; ++i) {
+ if (i == PS4_BT_GYRO) continue;
+ close(controller_event_fptrs[i]);
+ }
+ read(chosen_fd[0], &event1, ievt_size);
+ start_sec = event1.time.tv_sec;
+ while (1) {
+ read(chosen_fd[0], &event1, ievt_size);
+ if (event1.type == 0) continue;
+ printf("%zu.%-6zu %d %3d %6d\n", event1.time.tv_sec - start_sec, event1.time.tv_usec, event1.type, event1.code, event1.value);
+ }
+ break;
+ }
+ case PS4_BT_BUTTONS_BIT: //easy
+ {
+ chosen_fd[0] = controller_event_fptrs[PS4_BT_BUTTONS];
+ for (int i = 0; i < NUM_EVENTS; ++i) {
+ if (i == PS4_BT_BUTTONS) continue;
+ close(controller_event_fptrs[i]);
+ }
+ read(chosen_fd[0], &event1, ievt_size);
+ start_sec = event1.time.tv_sec;
+ while (1) {
+ read(chosen_fd[0], &event1, ievt_size);
+ if (event1.type == 0) continue;
+ printf("%zu.%-6zu %d %3d %6d\n", event1.time.tv_sec - start_sec, event1.time.tv_usec, event1.type, event1.code, event1.value);
+ }
+ break;
+ }
+ case PS4_BT_ALL_BITS: //HARD
+ {
+ struct input_event event2, event3;
+ chosen_fd[0] = controller_event_fptrs[PS4_BT_TOUCH];
+ chosen_fd[1] = controller_event_fptrs[PS4_BT_GYRO];
+ chosen_fd[2] = controller_event_fptrs[PS4_BT_BUTTONS];
+ for (int i = 0; i < NUM_EVENTS; ++i) {
+ if (i == PS4_BT_TOUCH || i == PS4_BT_GYRO || i == PS4_BT_BUTTONS) continue;
+ close(controller_event_fptrs[i]);
+ }
+ break;
+ }
+ case NINTENDO_GYRO_BIT: //easy
+ {
+ chosen_fd[0] = controller_event_fptrs[WII_GYRO];
+ for (int i = 0; i < NUM_EVENTS; ++i) {
+ if (i == WII_GYRO) continue;
+ close(controller_event_fptrs[i]);
+ }
+ read(chosen_fd[0], &event1, ievt_size);
+ start_sec = event1.time.tv_sec;
+ while (1) {
+ read(chosen_fd[0], &event1, ievt_size);
+ if (event1.type == 0) continue;
+ printf("%zu.%-6zu %d %3d %6d\n", event1.time.tv_sec - start_sec, event1.time.tv_usec, event1.type, event1.code, event1.value);
+ }
+ break;
+ }
+ case NINTENDO_IR_BIT: //easy
+ {
+ chosen_fd[0] = controller_event_fptrs[WII_IR];
+ for (int i = 0; i < NUM_EVENTS; ++i) {
+ if (i == WII_IR) continue;
+ close(controller_event_fptrs[i]);
+ }
+ read(chosen_fd[0], &event1, ievt_size);
+ start_sec = event1.time.tv_sec;
+ while (1) {
+ read(chosen_fd[0], &event1, ievt_size);
+ if (event1.type == 0) continue;
+ printf("%zu.%-6zu %d %3d %6d\n", event1.time.tv_sec - start_sec, event1.time.tv_usec, event1.type, event1.code, event1.value);
+ }
+ break;
+ }
+ case NINTENDO_BUTTONS_BIT: //easy
+ {
+ chosen_fd[0] = controller_event_fptrs[WII_BUTTONS];
+ for (int i = 0; i < NUM_EVENTS; ++i) {
+ if (i == WII_BUTTONS) continue;
+ close(controller_event_fptrs[i]);
+ }
+ read(chosen_fd[0], &event1, ievt_size);
+ start_sec = event1.time.tv_sec;
+ while (1) {
+ read(chosen_fd[0], &event1, ievt_size);
+ if (event1.type == 0) continue;
+ printf("%zu.%-6zu %d %3d %6d\n", event1.time.tv_sec - start_sec, event1.time.tv_usec, event1.type, event1.code, event1.value);
+ }
+ break;
+ }
+ case NINTENDO_ALL_BITS: //HARD
+ {
+ struct input_event event2, event3;
+ chosen_fd[0] = controller_event_fptrs[WII_GYRO];
+ chosen_fd[1] = controller_event_fptrs[WII_IR];
+ chosen_fd[2] = controller_event_fptrs[WII_BUTTONS];
+ for (int i = 0; i < NUM_EVENTS; ++i) {
+ if (i == WII_GYRO || i == WII_IR || i == WII_BUTTONS) continue;
+ close(controller_event_fptrs[i]);
+ }
+ break;
+ }
+ default:
+ {
+ fprintf(stderr, "ERROR: Do not know how you made it here.\n");
+ exit(-1);
+ break;
+ }
+ }
+
+
+ return 0;
+}