forked from NeroReflex/ROGueENEMY
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
220 lines (183 loc) · 6.53 KB
/
main.c
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <signal.h>
#include <stdlib.h>
#include <stdlib.h>
#include "input_dev.h"
#include "output_dev.h"
#include "logic.h"
logic_t global_logic;
static output_dev_t out_gamepadd_dev = {
.gamepad_fd = -1,
.imu_fd = -1,
.logic = &global_logic,
};
static iio_filters_t in_iio_filters = {
// .name = "accel_3d",
.name = "gyro_3d",
};
static input_dev_t in_iio_dev = {
.dev_type = input_dev_type_iio,
.iio_filters = &in_iio_filters,
.logic = &global_logic,
//.input_filter_fn = input_filter_imu_identity,
};
static input_dev_t in_hidraw_dev = {
.dev_type = input_dev_type_hidraw,
.logic = &global_logic
};
static uinput_filters_t in_asus_kb_1_filters = {
.name = "Asus Keyboard",
};
static input_dev_t in_asus_kb_1_dev = {
.dev_type = input_dev_type_uinput,
.ev_filters = &in_asus_kb_1_filters,
.logic = &global_logic,
.ev_input_filter_fn = input_filter_asus_kb,
};
static uinput_filters_t in_asus_kb_2_filters = {
.name = "Asus Keyboard",
};
static input_dev_t in_asus_kb_2_dev = {
.dev_type = input_dev_type_uinput,
.ev_filters = &in_asus_kb_2_filters,
.logic = &global_logic,
.ev_input_filter_fn = input_filter_asus_kb,
};
static uinput_filters_t in_asus_kb_3_filters = {
.name = "Asus Keyboard",
};
static input_dev_t in_asus_kb_3_dev = {
.dev_type = input_dev_type_uinput,
.ev_filters = &in_asus_kb_3_filters,
.logic = &global_logic,
.ev_input_filter_fn = input_filter_asus_kb,
};
static uinput_filters_t in_xbox_filters = {
.name = "Generic X-Box pad",
};
static input_dev_t in_xbox_dev = {
.dev_type = input_dev_type_uinput,
.ev_filters = &in_xbox_filters,
.logic = &global_logic,
.ev_input_filter_fn = input_filter_identity,
};
// void sig_handler(int signo)
// {
// if (signo == SIGINT) {
// logic_request_termination(&global_logic);
// printf("received SIGINT\n");
// }
// }
int main(int argc, char ** argv) {
const int logic_creation_res = logic_create(&global_logic);
if (logic_creation_res < 0) {
fprintf(stderr, "Unable to create logic: %d", logic_creation_res);
return EXIT_FAILURE;
}
int imu_fd = create_output_dev("/dev/uinput", output_dev_imu);
if (imu_fd < 0) {
fprintf(stderr, "Unable to create IMU virtual device\n");
return EXIT_FAILURE;
}
int gamepad_fd = create_output_dev("/dev/uinput", output_dev_gamepad);
if (gamepad_fd < 0) {
close(imu_fd);
fprintf(stderr, "Unable to create gamepad virtual device\n");
return EXIT_FAILURE;
}
int mouse_fd = create_output_dev("/dev/uinput", output_dev_mouse);
if (mouse_fd < 0) {
close(gamepad_fd);
close(imu_fd);
fprintf(stderr, "Unable to create mouse virtual device\n");
return EXIT_FAILURE;
}
out_gamepadd_dev.gamepad_fd = gamepad_fd;
out_gamepadd_dev.imu_fd = imu_fd;
out_gamepadd_dev.mouse_fd = mouse_fd;
/*
__sighandler_t sigint_hndl = signal(SIGINT, sig_handler);
if (sigint_hndl == SIG_ERR) {
fprintf(stderr, "Error registering SIGINT handler\n");
return EXIT_FAILURE;
}
*/
signal(SIGTERM, sigterm_handler);
int ret = 0;
pthread_t gamepad_thread;
pthread_t xbox_thread, asus_kb_1_thread, asus_kb_2_thread, asus_kb_3_thread, iio_thread, hidraw_thread;
//Added 1ms intial "input delay" to match hardware controller delay. Updated to 1.5ms, seems good so far.
//Reduced cpu usage by ~12%
const int gamepad_thread_creation = pthread_create(&gamepad_thread, NULL, output_dev_thread_func, (void*)(&out_gamepadd_dev));
if (gamepad_thread_creation != 0) {
fprintf(stderr, "Error creating gamepad output thread: %d\n", gamepad_thread_creation);
ret = -1;
logic_request_termination(&global_logic);
goto gamepad_thread_err;
}
//Using 30% cpu usage for some reason
//Updated sleep to 5K -> reduced cpu usage by ~20%
const int xbox_thread_creation = pthread_create(&xbox_thread, NULL, input_dev_thread_func, (void*)(&in_xbox_dev));
if (xbox_thread_creation != 0) {
fprintf(stderr, "Error creating xbox input thread: %d\n", xbox_thread_creation);
ret = -1;
logic_request_termination(&global_logic);
goto xbox_drv_thread_err;
}
/* Disabling had no effect on CPU usage avg
// const int asus_kb_1_thread_creation = pthread_create(&asus_kb_1_thread, NULL, input_dev_thread_func, (void*)(&in_asus_kb_1_dev));
// if (asus_kb_1_thread_creation != 0) {
// fprintf(stderr, "Error creating asus keyboard (1) input thread: %d\n", asus_kb_1_thread_creation);
// ret = -1;
// logic_request_termination(&global_logic);
// goto asus_kb_1_thread_err;
// }
// const int asus_kb_2_thread_creation = pthread_create(&asus_kb_2_thread, NULL, input_dev_thread_func, (void*)(&in_asus_kb_2_dev));
// if (asus_kb_2_thread_creation != 0) {
// fprintf(stderr, "Error creating asus keyboard (2) input thread: %d\n", asus_kb_2_thread_creation);
// ret = -1;
// logic_request_termination(&global_logic);
// goto asus_kb_2_thread_err;
// }
// const int asus_kb_3_thread_creation = pthread_create(&asus_kb_3_thread, NULL, input_dev_thread_func, (void*)(&in_asus_kb_3_dev));
// if (asus_kb_3_thread_creation != 0) {
// fprintf(stderr, "Error creating asus keyboard (3) input thread: %d\n", asus_kb_3_thread_creation);
// ret = -1;
// logic_request_termination(&global_logic);
// goto asus_kb_3_thread_err;
// }
*/
const int iio_thread_creation = pthread_create(&iio_thread, NULL, input_dev_thread_func, (void*)(&in_iio_dev));
if (iio_thread_creation != 0) {
fprintf(stderr, "Error creating iio input thread: %d\n", iio_thread_creation);
ret = -1;
logic_request_termination(&global_logic);
goto iio_thread_err;
}
// Disabling had no signification effect avg CPU usage
const int hidraw_thread_creation = pthread_create(&hidraw_thread, NULL, input_dev_thread_func, (void*)(&in_hidraw_dev));
if (hidraw_thread_creation != 0) {
fprintf(stderr, "Error creating iio input thread: %d\n", hidraw_thread_creation);
ret = -1;
logic_request_termination(&global_logic);
goto hidraw_thread_err;
}
pthread_join(iio_thread, NULL);
hidraw_thread_err:
pthread_join(hidraw_thread, NULL);
iio_thread_err:
pthread_join(asus_kb_3_thread, NULL);
asus_kb_3_thread_err:
pthread_join(asus_kb_2_thread, NULL);
asus_kb_2_thread_err:
pthread_join(asus_kb_1_thread, NULL);
asus_kb_1_thread_err:
pthread_join(xbox_thread, NULL);
xbox_drv_thread_err:
pthread_join(gamepad_thread, NULL);
gamepad_thread_err:
ioctl(gamepad_fd, UI_DEV_DESTROY);
close(gamepad_fd);
// TODO: free(imu_dev.events_list);
// TODO: free(gamepadd_dev.events_list);
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}