summaryrefslogtreecommitdiff
path: root/handle_devices.c
blob: 3a746bbf9f04d194d9b9d47653ac5de37d3a6eea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
#include "global_defs.h"

void open_wii(int* controller_event_fptrs, uint64_t* connected_controllers) {
	//Determine if WII REMOTE is connected via bluetooth
        int wii[4] = { 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 | \
				cut -d ' ' -f1 | \
				paste - -s", \
				"r");
        fscanf(wii_pipe, "%2d %2d %2d %2d", &wii[GYRO], &wii[IR], &wii[BUTTONS], &wii[NUNCHUK]);
        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][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_GYRO_IR_AND_BUTTONS; 
		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: NINTENDO WII GYRO AND IR\n", NINTENDO_GYRO_AND_IR);
		printf("%4d: NINTENDO WII GYRO AND BUTTONS\n", NINTENDO_GYRO_AND_BUTTONS);
		printf("%4d: NINTENDO WII IR AND BUTTONS\n", NINTENDO_IR_AND_BUTTONS);
		printf("%4d: NINTENDO WII GYRO AND IR AND BUTTONS\n", NINTENDO_GYRO_IR_AND_BUTTONS);
		//NUNCHUK CONNECTED?
		if (wii[NUNCHUK] != 0) { 
			*connected_controllers |= NINTENDO_ALL_BITS;
			snprintf(wii_events[NUNCHUK], 25, "/dev/input/event%d", wii[NUNCHUK]);
			controller_event_fptrs[WII_NUNCHUK] = open(wii_events[NUNCHUK], O_RDONLY);
			printf("%4d: NINTENDO WII NUNCHUK\n", NINTENDO_NUNCHUK_BIT);
			printf("%4d: NINTENDO WII GYRO AND NUNCHUK\n", NINTENDO_GYRO_AND_NUNCHUK);
			printf("%4d: NINTENDO WII IR AND NUNCHUK\n", NINTENDO_IR_AND_NUNCHUK);
			printf("%4d: NINTENDO WII BUTTONS AND NUNCHUK\n", NINTENDO_BUTTONS_AND_NUNCHUK);
			printf("%4d: NINTENDO WII GYRO AND IR AND NUNCHUK\n", NINTENDO_GYRO_IR_AND_NUNCHUK);
			printf("%4d: NINTENDO WII GYRO AND BUTTONS AND NUNCHUK\n", NINTENDO_GYRO_BUTTONS_AND_NUNCHUK);
			printf("%4d: NINTENDO WII IR AND BUTTONS AND NUNCHUK\n", NINTENDO_IR_BUTTONS_AND_NUNCHUK);
			printf("%4d: ALL NINTENDO BT\n", NINTENDO_ALL_BITS);
		} 
        } 
}

void open_ps4_bt(int* controller_event_fptrs, uint64_t* connected_controllers) {
        int ps4[NUM_EVENTS_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_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: PS4 BT TOUCH AND GYRO \n", PS4_BT_TOUCH_AND_GYRO);
		printf("%4d: PS4 BT TOUCH AND BUTTONS\n", PS4_BT_TOUCH_AND_BUTTONS);
		printf("%4d: PS4 BT GYRO AND BUTTONS\n", PS4_BT_GYRO_AND_BUTTONS);
		printf("%4d: ALL PS4 BT\n", PS4_BT_ALL_BITS);
        } 
}

void open_ps4_wired(int* controller_event_fptrs, uint64_t* connected_controllers) {
	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("%4d: PS4 WIRED TOUCH\n", PS4_WIRED_TOUCH_BIT);
		printf("%4d: PS4 WIRED GYRO\n", PS4_WIRED_GYRO_BIT);
		printf("%4d: PS4 WIRED BUTTONS\n", PS4_WIRED_BUTTONS_BIT);
		printf("%4d: PS4 WIRED TOUCH AND GYRO \n", PS4_WIRED_TOUCH_AND_GYRO);
		printf("%4d: PS4 WIRED TOUCH AND BUTTONS\n", PS4_WIRED_TOUCH_AND_BUTTONS);
		printf("%4d: PS4 WIRED GYRO AND BUTTONS\n", PS4_WIRED_GYRO_AND_BUTTONS);
		printf("%4d: ALL PS4 WIRED\n", PS4_WIRED_ALL_BITS);
	}
}

void open_steam(int* controller_event_fptrs, uint64_t* connected_controllers) {
	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 |= (uint64_t) 1;
		printf("%4d: VALVE STEAM\n", 1);
	}

}

void open_xbox_360(int* controller_event_fptrs, uint64_t* connected_controllers) {
	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 |= (uint64_t) 2;
		printf("%4d: XBOX 360\n", 2);
	}
}


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);
	//get arguments
	for (int i = 0; i < num_exceptions; ++i) {
		events_to_skip[i] = va_arg(list_of_args, int);		
	}
	//close all but the provided arguments
	for (int i = 0; i < NUM_EVENTS; ++i) {
		for (int j = 0; j < num_exceptions; ++j) {
			if (i == events_to_skip[j]) {
				goto SKIP_CLOSE;
			} 
		}
		close(controller_event_fptrs[i]);
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) {
		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",
				event_number,
				event1.time.tv_sec - start_sec,
				event1.time.tv_usec,
				event1.type,
				event1.code,
				event1.value); 
	}
	fprintf(stderr, "DISCONNECTED\n");
}