forked from mikepurvis/sixad
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sixad-sixaxis.cpp
380 lines (323 loc) · 12.6 KB
/
sixad-sixaxis.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* sixad-sixaxis.cpp
*
* This file is part of the QtSixA, the Sixaxis Joystick Manager
* Copyright 2008-10 Filipe Coelho <falktx@gmail.com>
*
* QtSixA can be redistributed and/or modified under the terms of the GNU General
* Public License (Version 2), as published by the Free Software Foundation.
* A copy of the license is included in the QtSixA source code, or can be found
* online at www.gnu.org/licenses.
*
* QtSixA is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
*/
#include "shared.h"
#include "sixaxis.h"
#include "uinput.h"
#include <cstdlib>
#include <iostream>
#include <poll.h>
#include <signal.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <sys/socket.h>
int csk = 0;
int isk = 1;
int debug;
int led_n;
bool old_rumble_mode;
volatile bool active = false;
volatile int weak = 0;
volatile int strong = 0;
volatile int timeout = 0;
struct uinput_fd *ufd;
static void uinput_listen()
{
struct input_event event;
struct uinput_ff_upload upload;
struct uinput_ff_erase erase;
struct rumble_effect effects[MAX_RUMBLE_EFFECTS];
int i;
//reset effects
for (i=0; i<MAX_RUMBLE_EFFECTS; i++) {
effects[i].id = -2;
}
while (!io_canceled()) {
if (read(ufd->js, &event, sizeof event) != sizeof event) {
syslog(LOG_INFO, "uinput_listen::Error on uinput read");
continue;
}
if (debug) syslog(LOG_INFO, "GOT event :: type is %i; code is %i; value is %i\n", event.type, event.code, event.value);
switch (event.type) {
case EV_UINPUT:
switch (event.code) {
case UI_FF_UPLOAD:
memset(&upload, 0, sizeof(upload));
upload.request_id = event.value;
if (ioctl(ufd->js, UI_BEGIN_FF_UPLOAD, &upload) < 0) {
syslog(LOG_ERR, "uinput_listen::Error on ff upload begin");
}
weak = upload.effect.u.rumble.weak_magnitude/0x101;
strong = upload.effect.u.rumble.strong_magnitude/0x101;
timeout = upload.effect.replay.length/0x101;
if (!old_rumble_mode) {
for (i=0; i<MAX_RUMBLE_EFFECTS; i++) {
if (effects[i].id == -2) {
effects[i].id = upload.effect.id;
effects[i].weak = weak;
effects[i].strong = strong;
effects[i].timeout = timeout;
if (debug) syslog(LOG_INFO, "going for effect #%i", i);
break;
}
}
} else {
active = true;
}
upload.retval = 0;
if (ioctl(ufd->js, UI_END_FF_UPLOAD, &upload) < 0) {
syslog(LOG_ERR, "uinput_listen::Error on ff upload end");
}
break;
case UI_FF_ERASE:
memset(&erase, 0, sizeof(erase));
erase.request_id = event.value;
if (ioctl(ufd->js, UI_BEGIN_FF_ERASE, &erase) < 0) {
syslog(LOG_ERR, "uinput_listen::Error on ff erase begin");
}
if (!old_rumble_mode) {
for (i=0; i<MAX_RUMBLE_EFFECTS; i++) {
if (effects[i].id == event.value) {
effects[i].id = -2;
break;
}
}
} else {
weak = 0;
strong = 0;
timeout = 0;
active = true;
}
erase.retval = 0;
if (ioctl(ufd->js, UI_END_FF_ERASE, &erase) < 0) {
syslog(LOG_ERR, "uinput_listen::Error on ff erase end");
}
break;
default:
break;
}
break;
case EV_FF:
if (event.value) {
for (i=0; i<MAX_RUMBLE_EFFECTS; i++) {
if (effects[i].id == event.code) {
do_rumble(csk, led_n, effects[i].weak, effects[i].strong, effects[i].timeout);
if (debug) syslog(LOG_INFO, "RUMBLE now :: %i|%i|%i", effects[i].weak, effects[i].strong, effects[i].timeout);
break;
}
}
} else {
if (!old_rumble_mode) {
do_rumble(csk, led_n, 0, 0, 0);
if (debug) syslog(LOG_INFO, "RUMBLE clean");
}
}
break;
default:
break;
}
}
pthread_exit((void*)1);
}
static void rumble_listen()
{
while (!io_canceled()) {
if (active) {
do_rumble(csk, led_n, weak, strong, timeout);
active = false;
} else
usleep(5000);
}
pthread_exit((void*)1);
}
static void process_sixaxis(struct device_settings settings, const char *mac)
{
int br;
bool msg = true;
unsigned char buf[128];
sigset_t sigs;
sigfillset(&sigs);
sigdelset(&sigs, SIGCHLD);
sigdelset(&sigs, SIGPIPE);
sigdelset(&sigs, SIGTERM);
sigdelset(&sigs, SIGINT);
sigdelset(&sigs, SIGHUP);
while (!io_canceled()) {
br = read(isk, buf, sizeof(buf));
if (msg) {
syslog(LOG_INFO, "Connected 'PLAYSTATION(R)3 Controller (%s)' [Battery %02X]", mac, buf[31]);
msg = false;
}
if (br < 0) {
break;
} else if (br==50 && buf[0]==0xa1 && buf[1]==0x01 && buf[2]==0x00) { //only continue if we've got a Sixaxis
if (settings.joystick.enabled) do_joystick(ufd->js, buf, settings.joystick);
if (settings.input.enabled) do_input(ufd->mk, buf, settings.input);
} else if (br==50 && buf[0]==0xa1 && buf[1]==0x01 && buf[2]==0xff) {
if (debug) syslog(LOG_ERR, "Got 0xff Sixaxis buffer, ignored");
} else if (buf[0]==0xa1 && buf[1]==0x01 && buf[2]==0x00) {
syslog(LOG_ERR, "Bad Sixaxis buffer (out of battery?), disconnecting now...");
sig_term(0);
break;
} else {
if (debug) syslog(LOG_ERR, "Non-Sixaxis packet received and ignored (0x%02x|0x%02x|0x%02x)", buf[0], buf[1], buf[2]);
}
// Use poll to wait on the bluetooth socket's next packet,
// in order to enforce a strict timeout.
if (settings.safety_timeout.enabled)
{
pollfd p;
p.events = POLLIN;
p.fd = isk;
p.revents = 0;
timespec timeout;
timeout.tv_sec = 0;
timeout.tv_nsec = 1000000 * settings.safety_timeout.timeout_ms;
if (ppoll(&p, 1, &timeout, &sigs) <= 0)
{
syslog(LOG_ERR, "Controller connection timed out. Please re-pair.");
// Clear all buttons to zero state and send a final update.
buf[3] = buf[4] = buf[5] = 0;
if (settings.joystick.enabled) do_joystick(ufd->js, buf, settings.joystick);
if (settings.input.enabled) do_input(ufd->mk, buf, settings.input);
// Disconnect.
sig_term(0);
break;
}
}
}
if (debug) syslog(LOG_ERR, "Read loop was broken on the Sixaxis process");
}
int main(int argc, char *argv[])
{
struct pollfd p[4];
struct timespec timeout;
struct device_settings settings;
struct sigaction sa;
pthread_t uinput_listen_thread, rumble_listen_thread;
sigset_t sigs;
short events;
if (argc < 3) {
std::cout << "Running " << argv[0] << " requires 'sixad'. Please run sixad instead" << std::endl;
return 1;
}
const char *mac = argv[1];
debug = atoi(argv[2]);
open_log("sixad-sixaxis");
syslog(LOG_INFO, "started");
settings = init_values(mac);
settings.remote.enabled = false;
ufd = uinput_open(DEV_TYPE_SIXAXIS, mac, settings);
if (ufd->js < 0 || ufd->mk < 0) {
return 1;
} else if (ufd->js == 0 && ufd->mk == 0) {
syslog(LOG_ERR, "sixaxis config has no joystick or input mode selected - please choose one!");
return 1;
}
enable_sixaxis(csk);
led_n = set_sixaxis_led(csk, settings.led, settings.rumble.enabled);
if (settings.rumble.enabled) {
old_rumble_mode = settings.rumble.old_mode;
if (pthread_create(&uinput_listen_thread, NULL, (void *(*)(void *))uinput_listen, NULL)) {
syslog(LOG_ERR, "error starting uinput listen thread");
return 1;
}
if (settings.rumble.old_mode) {
if (pthread_create(&rumble_listen_thread, NULL, (void *(*)(void *))rumble_listen, NULL)) {
syslog(LOG_ERR, "error starting rumble listen thread");
return 1;
}
}
}
sigfillset(&sigs);
sigdelset(&sigs, SIGCHLD);
sigdelset(&sigs, SIGPIPE);
sigdelset(&sigs, SIGTERM);
sigdelset(&sigs, SIGINT);
sigdelset(&sigs, SIGHUP);
memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_NOCLDSTOP;
sa.sa_handler = sig_term;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sa.sa_handler = SIG_IGN;
sigaction(SIGCHLD, &sa, NULL);
sigaction(SIGPIPE, &sa, NULL);
p[0].events = POLLIN | POLLERR | POLLHUP;
p[1].events = POLLIN | POLLERR | POLLHUP;
p[2].events = POLLIN | POLLERR | POLLHUP;
p[3].events = POLLIN | POLLERR | POLLHUP;
p[0].fd = 0;
p[1].fd = 1;
p[2].fd = ufd->js;
p[3].fd = ufd->mk;
if (!ufd->js)
p[2].fd = ufd->mk;
int idx = (ufd->js && ufd->mk) ? 4 : 3;
while (!io_canceled()) {
int i;
for (i = 0; i < 4; i++)
p[i].revents = 0;
timeout.tv_sec = 1;
timeout.tv_nsec = 0;
if (ppoll(p, idx, &timeout, &sigs) < 1)
continue;
if (p[1].revents & POLLIN) {
process_sixaxis(settings, mac);
}
events = p[0].revents | p[1].revents | p[2].revents | p[3].revents;
if (events & (POLLERR | POLLHUP)) {
sig_term(0);
break;
}
}
if (settings.rumble.enabled) {
if (pthread_cancel(uinput_listen_thread)) {
if (pthread_join(uinput_listen_thread, NULL)) {
syslog(LOG_ERR, "Error canceling uinput listen thread");
}
}
if (settings.rumble.old_mode) {
if (pthread_cancel(rumble_listen_thread)) {
if (pthread_join(rumble_listen_thread, NULL)) {
syslog(LOG_ERR, "Error canceling rumble listen thread");
}
}
}
}
if (debug) syslog(LOG_INFO, "Closing uinput...");
if (settings.joystick.enabled) {
uinput_close(ufd->js, debug);
}
if (settings.input.enabled) {
uinput_close(ufd->mk, debug);
}
do_rumble(csk, 10, 0xff, 0xff, 0x01);
usleep(10*1000);
shutdown(isk, SHUT_RDWR);
shutdown(csk, SHUT_RDWR);
delete ufd;
// hack for force disconnect
char cmd[32] = { 0 };
strcpy(cmd, "hcitool dc ");
strcat(cmd, mac);
usleep(10*1000);
syslog(LOG_INFO, "Force disconnect of \"%s\"", mac);
system(cmd);
if (debug) syslog(LOG_INFO, "Done");
return 0;
}