forked from mikepurvis/sixad
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sixad-remote.cpp
177 lines (141 loc) · 4.76 KB
/
sixad-remote.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
/*
* sixad-sixaxis.cpp
*
* This file is part of the QtSixA, the Sixaxis Joystick Manager
* Copyright 2011 Filipe Coelho <[email protected]>
*
* 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 "remote.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 timeout = 30;
volatile bool active = false;
struct uinput_fd *ufd;
static void process_remote(struct device_settings settings, const char *mac, int modes)
{
int br;
bool msg = true;
unsigned char buf[128];
while (!io_canceled()) {
br = read(isk, buf, sizeof(buf));
if (msg) {
syslog(LOG_INFO, "Connected 'PLAYSTATION(R)3 Remote (%s)'", mac);
msg = false;
}
if (br < 0) {
break;
} else if (br==13 && buf[0]==0xa1 && buf[1]==0x01) { //only continue if we've got a Remote
if (settings.joystick.enabled) do_joystick(ufd->js, buf, settings.joystick);
if (settings.remote.enabled) do_remote(ufd->mk, buf, modes);
if (settings.input.enabled) do_input(ufd->mk, buf, settings.input);
} else {
if (debug) syslog(LOG_ERR, "Non-Remote packet received and ignored (0x%02x|0x%02x|0x%02x)", buf[0], buf[1], buf[2]);
}
}
if (debug) syslog(LOG_ERR, "Read loop was broken on the Remote process");
}
int main(int argc, char *argv[])
{
struct pollfd p[3];
struct timespec timeout;
struct device_settings settings;
struct sigaction sa;
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-remote");
settings = init_values(mac);
settings.joystick.axis = false;
settings.joystick.sbuttons = false;
settings.joystick.accel = false;
settings.joystick.speed = false;
settings.joystick.pos = false;;
settings.led.enabled = false;
settings.rumble.enabled = false;
ufd = uinput_open(DEV_TYPE_REMOTE, mac, settings);
if (ufd->js < 0 || ufd->mk < 0) {
return 1;
} else if (ufd->js == 0 && ufd->mk == 0) {
syslog(LOG_ERR, "remote config has no joystick or input mode selected - please choose one!");
return 1;
}
int modes = 0;
if (settings.remote.numeric) modes |= REMOTE_KEYMODE_NUMBERIC;
if (settings.remote.dvd) modes |= REMOTE_KEYMODE_DVD;
if (settings.remote.directional) modes |= REMOTE_KEYMODE_DIRECTIONAL;
if (settings.remote.multimedia) modes |= REMOTE_KEYMODE_MULTIMEDIA;
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);
if (debug) syslog(LOG_INFO, "Press any to activate");
p[0].fd = 0;
p[0].events = POLLIN | POLLERR | POLLHUP;
p[1].fd = 1;
p[1].events = POLLIN | POLLERR | POLLHUP;
p[2].fd = ufd->mk ? ufd->mk : ufd->js;
p[2].events = POLLIN | POLLERR | POLLHUP;
while (!io_canceled()) {
int i, idx = 3;
for (i = 0; i < idx; 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_remote(settings, mac, modes);
}
events = p[0].revents | p[1].revents | p[2].revents;
if (events & (POLLERR | POLLHUP)) {
break;
}
}
if (debug) syslog(LOG_INFO, "Closing uinput...");
if (settings.joystick.enabled) {
uinput_close(ufd->js, debug);
}
if (settings.remote.enabled || settings.input.enabled) {
uinput_close(ufd->mk, debug);
}
delete ufd;
shutdown(isk, SHUT_RDWR);
shutdown(csk, SHUT_RDWR);
if (debug) syslog(LOG_INFO, "Done");
return 0;
}