This repository has been archived by the owner on Jan 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathds360.cpp
130 lines (117 loc) · 3.52 KB
/
ds360.cpp
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
#include <string>
#include <iostream>
#include <fstream>
#include <cstring>
#include <exception>
#include <cassert>
#include <unistd.h>
template <std::size_t N>
int execvp(const char *file, const char *const (&argv)[N])
{
assert((N > 0) && (argv[N - 1] == nullptr));
return execvp(file, const_cast<char *const *>(argv));
}
struct VIDException : public std::exception
{
const char *what() const throw()
{
return "VID not found in /proc/bus/input/devices, please make sure your Dualsense is connected";
}
};
std::string get_device(std::string device_vid, std::string device_pid)
{
std::ifstream file_input;
std::size_t pos;
std::string device_path, current_line, search_str, event_str;
std::string device_list_file = "/proc/bus/input/devices";
bool vid_pid_found = false;
bool debug = false;
// 1. open device list file
file_input.open(device_list_file.c_str());
if (!file_input.is_open())
{
std::cerr << "file_input.open >> " << std::strerror(errno) << std::endl;
throw -2;
}
// 2. search for first VID:PID and get event number
search_str = "Vendor=" + device_vid + " Product=" + device_pid;
while (getline(file_input, current_line))
{
if (!vid_pid_found)
{
pos = current_line.find(search_str, 0);
if (pos != std::string::npos)
{
vid_pid_found = true;
search_str = "event";
}
}
else
{
pos = current_line.find(search_str, 0);
if (pos != std::string::npos)
{
event_str = current_line.substr(pos);
// find space and substring event##
pos = event_str.find(' ', 0);
event_str = event_str.substr(0, pos);
break;
}
}
}
if (!vid_pid_found)
throw VIDException();
// 3. build device path
device_path = "evdev=/dev/input/" + event_str;
if (debug)
std::cout << device_path << std::endl;
return device_path;
}
void create_pid_file() {
std::string pid_file = "/tmp/ds360.pid";
pid_t pid = getpid();
std::ofstream file_output;
auto pid_str = std::to_string(pid);
std::cout << "pid: " << pid_str << std::endl;
file_output.open(pid_file.c_str());
if (!file_output.is_open())
{
std::cerr << "file_output.open >> " << std::strerror(errno) << std::endl;
throw -2;
}
file_output << pid_str.c_str() << std::endl;
file_output.close();
}
int main(int argc, char* argv[])
{
try
{
create_pid_file();
std::string event_path = get_device("054c", "0ce6");
if (argc < 2) {
execvp("xboxdrv",
{"xboxdrv",
"-o", event_path.c_str(),
"--mimic-xpad",
"--silent",
"--quiet",
"--axismap",
"-y1=y1,-y2=y2",
"--evdev-absmap",
"ABS_HAT0X=dpad_x,ABS_HAT0Y=dpad_y,ABS_X=X1,ABS_Y=Y1,ABS_RX=X2,ABS_RY=Y2,ABS_Z=LT,ABS_RZ=RT",
"--evdev-keymap",
"BTN_SOUTH=A,BTN_EAST=B,BTN_NORTH=Y,BTN_WEST=X,BTN_START=start,BTN_MODE=guide,BTN_SELECT=back,BTN_TL=LB,BTN_TR=RB,BTN_TL2=LT,BTN_TR2=RT,BTN_THUMBL=TL,BTN_THUMBR=TR",
nullptr}
);
}
else {
execvp("xboxdrv", {"xboxdrv", "-c", argv[1], "-o", event_path.c_str(), nullptr});
}
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n';
return 1;
}
return 0;
}