forked from thp/psmoveapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpsmoveapi.cpp
More file actions
396 lines (335 loc) · 12.4 KB
/
psmoveapi.cpp
File metadata and controls
396 lines (335 loc) · 12.4 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/**
* PS Move API - An interface for the PS Move Motion Controller
* Copyright (c) 2016 Thomas Perl <m@thp.io>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
**/
#include "psmoveapi.h"
#include "psmove_port.h"
#include "psmove_private.h"
#include "daemon/moved_monitor.h"
#include <vector>
#include <map>
#include <string>
#include <stdlib.h>
#include <string.h>
namespace {
struct ControllerGlue {
ControllerGlue(int index, const std::string &serial);
~ControllerGlue();
void add_handle(PSMove *handle);
void update_connection_flags();
ControllerGlue(const ControllerGlue &other) = delete;
Controller &operator=(const ControllerGlue &other) = delete;
PSMove *read_move() { return move_bluetooth; }
PSMove *write_move() { return move_usb ? move_usb : move_bluetooth; }
PSMove *move_bluetooth;
PSMove *move_usb;
std::string serial;
struct Controller controller;
bool connected;
bool api_connected;
};
struct PSMoveAPI {
PSMoveAPI(EventReceiver *receiver, void *user_data);
~PSMoveAPI();
void update();
static void on_monitor_event(enum MonitorEvent event, enum MonitorEventDeviceType device_type, const char *path, const wchar_t *serial, unsigned short pid, void *user_data);
EventReceiver *receiver;
void *user_data;
std::vector<ControllerGlue *> controllers;
moved_monitor *monitor;
};
PSMoveAPI *
g_psmove_api = nullptr;
}; // end anonymous namespace
ControllerGlue::ControllerGlue(int index, const std::string &serial)
: move_bluetooth(nullptr)
, move_usb(nullptr)
, serial(serial)
, controller()
, connected(false)
, api_connected(false)
{
memset(&controller, 0, sizeof(controller));
controller.index = index;
controller.serial = this->serial.c_str();
}
void
ControllerGlue::add_handle(PSMove *handle)
{
if (psmove_connection_type(handle) == Conn_USB) {
if (move_usb != nullptr) {
PSMOVE_WARNING("USB handle already exists for this controller");
psmove_disconnect(move_usb);
}
move_usb = handle;
} else {
if (move_bluetooth != nullptr) {
PSMOVE_WARNING("Bluetooth handle already exists for this controller -- leaking");
psmove_disconnect(move_bluetooth);
}
move_bluetooth = handle;
}
}
void
ControllerGlue::update_connection_flags()
{
controller.usb = (move_usb != nullptr);
controller.bluetooth = (move_bluetooth != nullptr);
if (controller.usb && !controller.bluetooth) {
controller.battery = Batt_CHARGING;
}
connected = (controller.usb || controller.bluetooth);
}
ControllerGlue::~ControllerGlue()
{
if (move_bluetooth != nullptr) {
psmove_disconnect(move_bluetooth);
}
if (move_usb != nullptr) {
psmove_disconnect(move_usb);
}
}
PSMoveAPI::PSMoveAPI(EventReceiver *receiver, void *user_data)
: receiver(receiver)
, user_data(user_data)
, controllers()
, monitor(nullptr)
{
std::map<std::string, std::vector<PSMove *>> moves;
int n = psmove_count_connected();
for (int i=0; i<n; i++) {
PSMove *move = psmove_connect_by_id(i);
if (!move) {
PSMOVE_WARNING("Failed to connect to controller #%d", i);
continue;
}
char *tmp = psmove_get_serial(move);
if (!tmp) {
PSMOVE_WARNING("Failed to get serial for controller #%d", i);
continue;
}
std::string serial(tmp);
psmove_free_mem(tmp);
moves[serial].emplace_back(move);
}
int i = 0;
for (auto &kv: moves) {
auto c = new ControllerGlue(i++, kv.first);
for (auto &handle: kv.second) {
c->add_handle(handle);
}
controllers.emplace_back(c);
}
#ifndef _WIN32
monitor = moved_monitor_new(PSMoveAPI::on_monitor_event, this);
#endif
}
PSMoveAPI::~PSMoveAPI()
{
#ifndef _WIN32
moved_monitor_free(monitor);
#endif
for (auto &c: controllers) {
if (c->api_connected) {
if (receiver->disconnect != nullptr) {
// Send disconnect event
receiver->disconnect(&c->controller, user_data);
}
c->api_connected = false;
}
delete c;
}
}
void
PSMoveAPI::update()
{
#ifndef _WIN32
if (moved_monitor_get_fd(monitor) == -1) {
moved_monitor_poll(monitor);
} else {
struct pollfd pfd;
pfd.fd = moved_monitor_get_fd(monitor);
pfd.events = POLLIN;
while (poll(&pfd, 1, 0) > 0) {
moved_monitor_poll(monitor);
}
}
#endif
for (auto &c: controllers) {
c->update_connection_flags();
if (c->connected && !c->api_connected) {
if (receiver->connect != nullptr) {
// Send initial connect event
receiver->connect(&c->controller, user_data);
}
c->api_connected = c->connected;
}
if (!c->connected && c->api_connected) {
if (receiver->disconnect != nullptr) {
// Send disconnect event
receiver->disconnect(&c->controller, user_data);
}
c->api_connected = c->connected;
}
if (!c->connected) {
// Done with this controller (TODO: Can we check reconnect?)
continue;
}
auto read_move = c->read_move();
if (read_move == nullptr) {
// We don't have a handle that supports reading (USB-only connection);
// we call update exactly once (no new data will be reported), so that
// the update method can change the LED and rumble values
if (receiver->update != nullptr) {
receiver->update(&c->controller, user_data);
}
} else {
while (psmove_poll(read_move)) {
if (receiver->update != nullptr) {
int previous = c->controller.buttons;
c->controller.buttons = psmove_get_buttons(read_move);
c->controller.pressed = c->controller.buttons & ~previous;
c->controller.released = previous & ~c->controller.buttons;
c->controller.trigger = float(psmove_get_trigger(read_move)) / 255.f;
psmove_get_accelerometer_frame(read_move, Frame_SecondHalf,
&c->controller.accelerometer.x,
&c->controller.accelerometer.y,
&c->controller.accelerometer.z);
psmove_get_gyroscope_frame(read_move, Frame_SecondHalf,
&c->controller.gyroscope.x,
&c->controller.gyroscope.y,
&c->controller.gyroscope.z);
psmove_get_magnetometer_vector(read_move,
&c->controller.magnetometer.x,
&c->controller.magnetometer.y,
&c->controller.magnetometer.z);
c->controller.battery = psmove_get_battery(read_move);
receiver->update(&c->controller, user_data);
}
}
}
auto write_move = c->write_move();
if (write_move != nullptr) {
psmove_set_leds(write_move,
uint32_t(255 * c->controller.color.r),
uint32_t(255 * c->controller.color.g),
uint32_t(255 * c->controller.color.b));
psmove_set_rumble(write_move, uint32_t(255 * c->controller.rumble));
psmove_update_leds(write_move);
}
}
}
void
PSMoveAPI::on_monitor_event(enum MonitorEvent event, enum MonitorEventDeviceType device_type, const char *path, const wchar_t *serial, unsigned short pid, void *user_data)
{
auto self = static_cast<PSMoveAPI *>(user_data);
switch (event) {
case EVENT_DEVICE_ADDED:
{
PSMOVE_DEBUG("on_monitor_event(event=EVENT_DEVICE_ADDED, device_type=0x%08x, path=\"%s\", serial=%p)",
device_type, path, serial);
for (auto &c: self->controllers) {
if ((c->move_bluetooth != nullptr && strcmp(_psmove_get_device_path(c->move_bluetooth), path) == 0) ||
(c->move_usb != nullptr && strcmp(_psmove_get_device_path(c->move_usb), path) == 0)) {
PSMOVE_WARNING("This controller is already active!");
return;
}
}
PSMove *move = psmove_connect_internal(serial, path, -1, pid);
if (move == nullptr) {
PSMOVE_ERROR("Cannot open move for retrieving serial!");
return;
}
char *serial_number = psmove_get_serial(move);
bool found = false;
for (auto &c: self->controllers) {
if (strcmp(c->serial.c_str(), serial_number) == 0) {
c->add_handle(move);
found = true;
break;
}
}
if (!found) {
auto c = new ControllerGlue(self->controllers.size(), std::string(serial_number));
c->add_handle(move);
self->controllers.emplace_back(c);
}
psmove_free_mem(serial_number);
}
break;
case EVENT_DEVICE_REMOVED:
{
PSMOVE_DEBUG("on_monitor_event(event=EVENT_DEVICE_REMOVED, device_type=0x%08x, path=\"%s\", serial=%p)",
device_type, path, serial);
bool found = false;
for (auto &c: self->controllers) {
if (c->move_bluetooth != nullptr) {
const char *devpath = _psmove_get_device_path(c->move_bluetooth);
if (devpath != nullptr && strcmp(devpath, path) == 0) {
psmove_disconnect(c->move_bluetooth), c->move_bluetooth = nullptr;
found = true;
break;
}
}
if (c->move_usb != nullptr) {
const char *devpath = _psmove_get_device_path(c->move_usb);
if (devpath != nullptr && strcmp(devpath, path) == 0) {
psmove_disconnect(c->move_usb), c->move_usb = nullptr;
found = true;
break;
}
}
}
if (!found) {
PSMOVE_ERROR("Did not find device for removal");
}
}
break;
default:
PSMOVE_ERROR("Invalid event");
break;
}
}
void
psmoveapi_init(EventReceiver *receiver, void *user_data)
{
if (g_psmove_api == nullptr) {
g_psmove_api = new PSMoveAPI(receiver, user_data);
}
}
void
psmoveapi_update()
{
if (g_psmove_api != nullptr) {
g_psmove_api->update();
}
}
void
psmoveapi_quit()
{
delete g_psmove_api;
g_psmove_api = nullptr;
}