Skip to content
42 changes: 35 additions & 7 deletions src/psmove.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#endif

#ifdef _WIN32
# include <winsock2.h>
# include <windows.h>
# include <bthsdpdef.h>
# include <bluetoothapis.h>
Expand Down Expand Up @@ -258,6 +259,7 @@ struct _PSMove {

/* The handle to the HIDAPI device */
hid_device *handle;
hid_device *handle_calib;

/* The handle to the moved client */
moved_client *client;
Expand All @@ -275,6 +277,7 @@ struct _PSMove {

/* Device path of the controller */
char *device_path;
char *device_path_calib;

/* Nonzero if the value of the LEDs or rumble has changed */
unsigned char leds_dirty;
Expand Down Expand Up @@ -564,9 +567,7 @@ psmove_connect_internal(wchar_t *serial, char *path, int id)
/* XXX Ugly: Convert "col02" path to "col01" path (tested w/ BT and USB) */
char *p;
psmove_return_val_if_fail((p = strstr(path, "&col02#")) != NULL, NULL);
p[5] = '1';
psmove_return_val_if_fail((p = strstr(path, "&0001#")) != NULL, NULL);
p[4] = '0';
#endif

if (serial == NULL && path != NULL) {
Expand All @@ -583,6 +584,27 @@ psmove_connect_internal(wchar_t *serial, char *path, int id)
/* Use Non-Blocking I/O */
hid_set_nonblocking(move->handle, 1);

#ifdef _WIN32
char *path_calib = (char*) calloc(strlen(path)+1, sizeof(char));
strncpy(path_calib, path, strlen(path)+1);
psmove_return_val_if_fail((p = strstr(path_calib, "&col02#")) != NULL, NULL);
p[5] = '1';
psmove_return_val_if_fail((p = strstr(path_calib, "&0001#")) != NULL, NULL);
p[4] = '0';

move->handle_calib = hid_open_path(path_calib);
if (!move->handle_calib) {
free(move);
return NULL;
}

hid_set_nonblocking(move->handle_calib, 1);

if (path_calib != NULL) {
move->device_path_calib = strdup(path_calib);
}
#endif

/* Message type for LED set requests */
move->leds.type = PSMove_Req_SetLEDs;

Expand Down Expand Up @@ -915,7 +937,11 @@ _psmove_get_calibration_blob(PSMove *move, char **dest, size_t *size)
for (x=0; x<3; x++) {
memset(cal, 0, sizeof(cal));
cal[0] = PSMove_Req_GetCalibration;
res = hid_get_feature_report(move->handle, cal, sizeof(cal));

if(move->handle_calib)
res = hid_get_feature_report(move->handle_calib, cal, sizeof(cal));
else
res = hid_get_feature_report(move->handle, cal, sizeof(cal));
#if defined(__linux)
if(res == -1) {
psmove_WARNING("hid_get_feature_report failed, kernel issue? see %s\n",
Expand Down Expand Up @@ -1864,8 +1890,6 @@ psmove_get_magnetometer_calibration_filename(PSMove *move)
char filename[PATH_MAX];

char *serial = psmove_get_serial(move);
psmove_return_val_if_fail(serial != NULL, NULL);

int i;
for (i=0; i<strlen(serial); i++) {
if (serial[i] == ':') {
Expand Down Expand Up @@ -1980,6 +2004,8 @@ psmove_disconnect(PSMove *move)
switch (move->type) {
case PSMove_HIDAPI:
hid_close(move->handle);
if(move->handle_calib)
hid_close(move->handle_calib);
break;
case PSMove_MOVED:
// XXX: Close connection?
Expand All @@ -1996,6 +2022,8 @@ psmove_disconnect(PSMove *move)

free(move->serial_number);
free(move->device_path);
if(move->device_path_calib)
free(move->device_path_calib);
free(move);

/* Bookkeeping of open handles (for psmove_reinit) */
Expand Down Expand Up @@ -2176,7 +2204,7 @@ _psmove_normalize_btaddr(const char *addr, int lowercase, char separator)
return result;
}

#if defined(__APPLE__) || defined(_WIN32)
#if defined(__APPLE__)

#define CLOCK_MONOTONIC 0

Expand All @@ -2191,7 +2219,7 @@ clock_gettime(int unused, struct timespec *ts)

return 0;
}
#endif /* __APPLE__ || _WIN32 */
#endif /* __APPLE__ */

PSMove_timestamp
_psmove_timestamp()
Expand Down