-
Notifications
You must be signed in to change notification settings - Fork 0
/
pad.c
291 lines (256 loc) · 6.41 KB
/
pad.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <termios.h>
#include <fcntl.h>
#define PAD_PREFIX "GET /pad.ps3?"
#define PAD_HOME "_psbtn_home"
#define PAD_HOME_HOLD "_psbtn_home_hold"
#define PAD_UP "up"
#define PAD_DOWN "down"
#define PAD_RIGHT "right"
#define PAD_LEFT "left"
#define PAD_CROSS "cross"
#define PAD_CIRCLE "circle"
#define PAD_TRIANGLE "triangle"
#define PAD_SQUARE "square"
#define PAD_L1 "l1"
#define PAD_R1 "r1"
#define PAD_L2 "l2"
#define PAD_R2 "r2"
#define PAD_START "start"
#define PAD_SELECT "select"
int pad_connect(void);
int term_setup(void);
int fcntl_setup(int);
int pad_setup(char *, char *, int *);
int fdflags;
struct sockaddr_in sockopt;
int main(int argc, char **argv)
{
struct hostent *resaddr;
if(argc < 2 || argc > 2)
{
fprintf(stderr, "Usage: ./a.out <PlayStation 3 ip>\n");
return -1;
}
else if((resaddr = gethostbyname(argv[1])) == NULL)
{
fprintf(stderr, "Invalid address.\n");
return -1;
}
sockopt.sin_family = AF_INET;
sockopt.sin_port = htons(80);
sockopt.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr *) resaddr->h_addr));
if(!term_setup())
{
fprintf(stderr, "Failed to set up the terminal.\n");
return -1;
}
else if((fdflags = fcntl(0, F_GETFL)) < 0)
{
fprintf(stderr, "Failed to get stdin descriptor flags.\r\n");
return -1;
}
while(1)
if(!pad_connect()) return -1;
}
int pad_connect(void)
{
int sockfd;
char *pad_input;
switch(getchar())
{
case 'z':
{
char pad_arr[sizeof PAD_PREFIX + sizeof PAD_CROSS - 1];
if(!pad_setup(PAD_CROSS, pad_arr, &sockfd)) return 0;
pad_input = pad_arr;
break;
}
case 'a':
{
char pad_arr[sizeof PAD_PREFIX + sizeof PAD_SQUARE - 1];
if(!pad_setup(PAD_SQUARE, pad_arr, &sockfd)) return 0;
pad_input = pad_arr;
break;
}
case 's':
{
char pad_arr[sizeof PAD_PREFIX + sizeof PAD_CIRCLE - 1];
if(!pad_setup(PAD_CIRCLE, pad_arr, &sockfd)) return 0;
pad_input = pad_arr;
break;
}
case 'w':
{
char pad_arr[sizeof PAD_PREFIX + sizeof PAD_TRIANGLE - 1];
if(!pad_setup(PAD_TRIANGLE, pad_arr, &sockfd)) return 0;
pad_input = pad_arr;
break;
}
case 'q':
{
char pad_arr[sizeof PAD_PREFIX + sizeof PAD_L1 - 1];
if(!pad_setup(PAD_L1, pad_arr, &sockfd)) return 0;
pad_input = pad_arr;
break;
}
case 'e':
{
char pad_arr[sizeof PAD_PREFIX + sizeof PAD_R1 - 1];
if(!pad_setup(PAD_R1, pad_arr, &sockfd)) return 0;
pad_input = pad_arr;
break;
}
case 'x':
{
char pad_arr[sizeof PAD_PREFIX + sizeof PAD_L2 - 1];
if(!pad_setup(PAD_L2, pad_arr, &sockfd)) return 0;
pad_input = pad_arr;
break;
}
case 'c':
{
char pad_arr[sizeof PAD_PREFIX + sizeof PAD_R2 - 1];
if(!pad_setup(PAD_R2, pad_arr, &sockfd)) return 0;
pad_input = pad_arr;
break;
}
case '1':
{
char pad_arr[sizeof PAD_PREFIX + sizeof PAD_START - 1];
if(!pad_setup(PAD_START, pad_arr, &sockfd)) return 0;
pad_input = pad_arr;
break;
}
case '2':
{
char pad_arr[sizeof PAD_PREFIX + sizeof PAD_SELECT - 1];
if(!pad_setup(PAD_SELECT, pad_arr, &sockfd)) return 0;
pad_input = pad_arr;
break;
}
case 'h':
{
char pad_arr[sizeof PAD_PREFIX + sizeof PAD_HOME - 1];
if(!pad_setup(PAD_HOME, pad_arr, &sockfd)) return 0;
pad_input = pad_arr;
break;
}
case 'H':
{
char pad_arr[sizeof PAD_PREFIX + sizeof PAD_HOME_HOLD - 1];
if(!pad_setup(PAD_HOME_HOLD, pad_arr, &sockfd)) return 0;
pad_input = pad_arr;
break;
}
case 27: /* For arrow keys. */
if(!fcntl_setup(0)) return 0;
usleep(1000);
if(getchar() == '[')
{
usleep(1000);
switch(getchar())
{
case 'A':
{
char pad_arr[sizeof PAD_PREFIX + sizeof PAD_UP - 1];
if(!fcntl_setup(1)) return 0;
else if(!pad_setup(PAD_UP, pad_arr, &sockfd)) return 0;
pad_input = pad_arr;
break;
}
case 'B':
{
char pad_arr[sizeof PAD_PREFIX + sizeof PAD_DOWN - 1];
if(!fcntl_setup(1)) return 0;
else if(!pad_setup(PAD_DOWN, pad_arr, &sockfd)) return 0;
pad_input = pad_arr;
break;
}
case 'C':
{
char pad_arr[sizeof PAD_PREFIX + sizeof PAD_RIGHT - 1];
if(!fcntl_setup(1)) return 0;
else if(!pad_setup(PAD_RIGHT, pad_arr, &sockfd)) return 0;
pad_input = pad_arr;
break;
}
case 'D':
{
char pad_arr[sizeof PAD_PREFIX + sizeof PAD_LEFT - 1];
if(!fcntl_setup(1)) return 0;
else if(!pad_setup(PAD_LEFT, pad_arr, &sockfd)) return 0;
pad_input = pad_arr;
break;
}
default:
if(!fcntl_setup(1)) return 0;
return 1;
}
}
else
{
if(!fcntl_setup(1)) return 0;
return 1;
}
break;
case 3: /* For Ctrl+C. */
printf("Exiting...\r\n");
exit(0);
default:
return 1;
}
printf("%s\r\n", pad_input);
write(sockfd, pad_input, strlen(pad_input));
close(sockfd);
return 1;
}
int term_setup(void)
{
struct termios term;
if(tcgetattr(0, &term) < 0) return 0;
cfmakeraw(&term);
if(tcsetattr(0, TCSANOW, &term) < 0) return 0;
return 1;
}
int fcntl_setup(int reset)
{
if(reset)
{
if(fcntl(0, F_SETFL, fdflags) < 0)
{
fprintf(stderr, "Failed to reset stdin descriptor flags.\r\n");
return 0;
}
}
else
if(fcntl(0, F_SETFL, fdflags | O_NONBLOCK) < 0)
{
fprintf(stderr, "Failed to set stdin descriptor flags.\r\n");
return 0;
}
return 1;
}
int pad_setup(char *padkey, char *pad_input, int *sockfd)
{
if((*sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
fprintf(stderr, "Failed to open socket.\r\n");
return 0;
}
else if(connect(*sockfd, (struct sockaddr *) &sockopt, sizeof sockopt) < 0)
{
fprintf(stderr, "Failed to connect.\r\n");
return 0;
}
memcpy(pad_input, PAD_PREFIX, sizeof PAD_PREFIX - 1);
strcpy(pad_input + sizeof PAD_PREFIX - 1, padkey);
return 1;
}