-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchin.cpp
218 lines (197 loc) · 4.99 KB
/
chin.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
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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <string>
#include <fstream>
#include <netdb.h>
#include <arpa/inet.h>
int main(int argc, char ** argv)
{
int fd, n, RWin_State, F12_State, bytesReceived, port;
int sock=socket(AF_INET,SOCK_STREAM,0);
int connectRes=INT16_MAX;
bool inputIsSwitched=false;
char buf[256], dataBuffer[1];
std::string line,usbip,keyFD,keyboard,mouse,IP;
std::string user=getlogin();
std::string configPath="/home/"+user+"/.config/play/config";
std::ifstream configReader;
//Get configuration information
try
{
configReader.open(configPath);
while(!configReader.eof())
{
getline(configReader,line);
if(line.substr(0,1)!="#")//Hashtag denotes comments
{
if(line.find("usbip")==0)
{
usbip=line.substr(line.find("=")+1,line.length()-line.find("="));
fprintf(stdout,"Setting Windows usbip path to %s\n",usbip.c_str());
}
else if(line.find("keyFD")==0)
{
keyFD=line.substr(line.find("=")+1,line.length()-line.find("="));
fprintf(stdout,"Setting keyboard file descriptor path to %s\n",keyFD.c_str());
}
else if(line.find("keyboard")==0)
{
keyboard=line.substr(line.find("=")+1,line.length()-line.find("="));
fprintf(stdout,"Setting busid for keyboard to %s\n",keyboard.c_str());
}
else if(line.find("mouse")==0)
{
mouse=line.substr(line.find("=")+1,line.length()-line.find("="));
fprintf(stdout,"Setting busid for mouse to %s\n",mouse.c_str());
}
else if(line.find("IP")==0)
{
IP=line.substr(line.find("=")+1,line.length()-line.find("="));
fprintf(stdout,"Setting IP to %s\n",mouse.c_str());
}
else if(line.find("port")==0)
{
port=stoi(line.substr(line.find("=")+1,line.length()-line.find("=")));
fprintf(stdout,"Setting port to %d\n",port);
}
}
}
if(usbip=="")
{
fprintf(stderr,"usbip path not found\n");
return -2;
}
if(keyFD=="")
{
fprintf(stderr,"keyboard file descriptor path not found\n");
return -3;
}
if(keyboard=="")
{
fprintf(stderr,"keyboard busid not found\n");
return -4;
}
if(mouse=="")
{
fprintf(stderr,"mouse busid not found\n");
return -5;
}
if(IP=="")
{
fprintf(stderr,"IP not found\n");
return -6;
}
if(port==0)
{
fprintf(stderr,"port not found\n");
return -7;
}
}
catch(...)
{
fprintf(stderr,"Error while attempting to read config file at \"%s\"\n",configPath.c_str());
return -1;
}
//Create a hint structure for the server we're connecting with
sockaddr_in hint;
hint.sin_family=AF_INET;
hint.sin_port=htons(port);
inet_pton(AF_INET,IP.c_str(),&hint.sin_addr);
if(sock==-1)
{
fprintf(stderr,"Unable to create socket on port %d\n",port);
return -8;
}
else
{
fprintf(stdout,"Successfully initialized socket %d on port %d\n",sock,port);
}
//Connect to the server on the socket
connectRes=connect(sock,(sockaddr*)&hint,sizeof(hint));
if(connectRes==-1)
{
fprintf(stderr,"Could not connect to %s:%d\n",IP.c_str(),port);
return -9;
}
else
{
fprintf(stdout,"Successfully connected to %s:%d\n",IP.c_str(),port);
}
//Open keyboard file descriptor
fd = open(("/dev/input/by-id/"+keyFD).c_str(), O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
if (fd == -1)
{
fprintf(stderr,"open_port: Unable to open /dev/input/by-id/%s - \n",keyFD.c_str());
return -10;
}
// Turn off blocking for reads, use (fd, F_SETFL, FNDELAY) if you want that
fcntl(fd, F_SETFL, 0);
sleep(1);
while(1)
{
n=read(fd,(void*)buf, 255);
if (n==0)
{
fprintf(stdout,"No data on port\n");
}
else if(n>0)
{
if(!inputIsSwitched)
{
buf[n] = '\0';
//printf("%i bytes read : %s", n, buf);
//printf("Key: %d\n",buf[42]);
//printf("State: %d\n",buf[44]);
/*for(int i=0; i<n; i++)
{
printf("buf[%d]=%d\n",i,buf[i]);
printf("%d-",buf[i]);
}*/
printf("\n");
if(buf[42]==125)
{
RWin_State=buf[44];
}
else if(buf[42]==88)
{
F12_State=buf[44];
}
if(RWin_State==1&&F12_State==1)
{
fprintf(stdout,"Switching input stream to Windows\n");
inputIsSwitched=true;
system(("usbip bind --busid="+keyboard).c_str());
system(("winrun \""+usbip+" attach_ude -r 192.168.1.2 --busid="+keyboard+"\"").c_str());
system(("usbip bind --busid="+mouse).c_str());
system(("winrun \""+usbip+" attach_ude -r 192.168.1.2 --busid="+mouse+"\"").c_str());
//Artifically set to 0 to avoid them "sticking" in program
RWin_State=0;
F12_State=0;
}
}
else
{
//Wait to receive change signal from chin_win
bytesReceived=recv(sock,dataBuffer,1,0);
if(bytesReceived>0)
{
fprintf(stdout,"Switching input stream to Linux\n");
inputIsSwitched=false;
system(("usbip unbind --busid="+keyboard).c_str());
system(("usbip unbind --busid="+mouse).c_str());
}
else
{
fprintf(stderr,"Zero-length buffer recieved\n");
return -11;
}
}
}
}
close(fd);
return 0;
}