mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-08-28 01:21:35 +00:00
Convert ticks to 64-bit, added nanosecond precision to the API
Fixes https://github.com/libsdl-org/SDL/issues/5512 Fixes https://github.com/libsdl-org/SDL/issues/6731
This commit is contained in:
@@ -137,7 +137,7 @@ typedef struct
|
||||
SDL_bool report_touchpad;
|
||||
SDL_bool hardware_calibration;
|
||||
IMUCalibrationData calibration[6];
|
||||
Uint32 last_packet;
|
||||
Uint64 last_packet;
|
||||
int player_index;
|
||||
Uint8 rumble_left;
|
||||
Uint8 rumble_right;
|
||||
@@ -1037,7 +1037,7 @@ static SDL_bool HIDAPI_DriverPS4_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
Uint8 data[USB_PACKET_LENGTH * 2];
|
||||
int size;
|
||||
int packet_count = 0;
|
||||
Uint32 now = SDL_GetTicks();
|
||||
Uint64 now = SDL_GetTicks();
|
||||
|
||||
if (device->num_joysticks > 0) {
|
||||
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
|
||||
@@ -1089,7 +1089,7 @@ static SDL_bool HIDAPI_DriverPS4_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
if (device->is_bluetooth) {
|
||||
if (packet_count == 0) {
|
||||
/* Check to see if it looks like the device disconnected */
|
||||
if (SDL_TICKS_PASSED(now, ctx->last_packet + BLUETOOTH_DISCONNECT_TIMEOUT_MS)) {
|
||||
if (now >= (ctx->last_packet + BLUETOOTH_DISCONNECT_TIMEOUT_MS)) {
|
||||
/* Send an empty output report to tickle the Bluetooth stack */
|
||||
HIDAPI_DriverPS4_TickleBluetooth(device);
|
||||
}
|
||||
@@ -1106,7 +1106,7 @@ static SDL_bool HIDAPI_DriverPS4_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
if (packet_count == 0) {
|
||||
if (device->num_joysticks > 0) {
|
||||
/* Check to see if it looks like the device disconnected */
|
||||
if (SDL_TICKS_PASSED(now, ctx->last_packet + BLUETOOTH_DISCONNECT_TIMEOUT_MS)) {
|
||||
if (now >= (ctx->last_packet + BLUETOOTH_DISCONNECT_TIMEOUT_MS)) {
|
||||
HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,7 +215,7 @@ typedef struct
|
||||
SDL_bool hardware_calibration;
|
||||
IMUCalibrationData calibration[6];
|
||||
Uint16 firmware_version;
|
||||
Uint32 last_packet;
|
||||
Uint64 last_packet;
|
||||
int player_index;
|
||||
SDL_bool player_lights;
|
||||
Uint8 rumble_left;
|
||||
@@ -703,7 +703,7 @@ static void HIDAPI_DriverPS5_CheckPendingLEDReset(SDL_HIDAPI_Device *device)
|
||||
packet->rgucSensorTimestamp[1],
|
||||
packet->rgucSensorTimestamp[2],
|
||||
packet->rgucSensorTimestamp[3]);
|
||||
if (SDL_TICKS_PASSED(timestamp, connection_complete)) {
|
||||
if ((Sint32)(connection_complete - timestamp) <= 0) {
|
||||
led_reset_complete = SDL_TRUE;
|
||||
}
|
||||
} else {
|
||||
@@ -1305,7 +1305,7 @@ static SDL_bool HIDAPI_DriverPS5_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
Uint8 data[USB_PACKET_LENGTH * 2];
|
||||
int size;
|
||||
int packet_count = 0;
|
||||
Uint32 now = SDL_GetTicks();
|
||||
Uint64 now = SDL_GetTicks();
|
||||
|
||||
if (device->num_joysticks > 0) {
|
||||
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
|
||||
@@ -1365,7 +1365,7 @@ static SDL_bool HIDAPI_DriverPS5_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
if (device->is_bluetooth) {
|
||||
if (packet_count == 0) {
|
||||
/* Check to see if it looks like the device disconnected */
|
||||
if (SDL_TICKS_PASSED(now, ctx->last_packet + BLUETOOTH_DISCONNECT_TIMEOUT_MS)) {
|
||||
if (now >= (ctx->last_packet + BLUETOOTH_DISCONNECT_TIMEOUT_MS)) {
|
||||
/* Send an empty output report to tickle the Bluetooth stack */
|
||||
HIDAPI_DriverPS5_TickleBluetooth(device);
|
||||
}
|
||||
|
||||
@@ -78,13 +78,13 @@ typedef struct
|
||||
|
||||
SDL_JoystickPowerLevel battery_level;
|
||||
SDL_bool charging;
|
||||
Uint32 last_battery_query_time;
|
||||
Uint64 last_battery_query_time;
|
||||
|
||||
SDL_bool rumble_report_pending;
|
||||
SDL_bool rumble_update_pending;
|
||||
Uint8 left_motor_amplitude;
|
||||
Uint8 right_motor_amplitude;
|
||||
Uint32 last_rumble_time;
|
||||
Uint64 last_rumble_time;
|
||||
|
||||
Uint8 last_state[USB_PACKET_LENGTH];
|
||||
} SDL_DriverShield_Context;
|
||||
@@ -541,14 +541,14 @@ static SDL_bool HIDAPI_DriverShield_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
}
|
||||
|
||||
/* Ask for battery state again if we're due for an update */
|
||||
if (joystick && SDL_TICKS_PASSED(SDL_GetTicks(), ctx->last_battery_query_time + BATTERY_POLL_INTERVAL_MS)) {
|
||||
if (joystick && SDL_GetTicks() >= (ctx->last_battery_query_time + BATTERY_POLL_INTERVAL_MS)) {
|
||||
ctx->last_battery_query_time = SDL_GetTicks();
|
||||
HIDAPI_DriverShield_SendCommand(device, CMD_BATTERY_STATE, NULL, 0);
|
||||
}
|
||||
|
||||
/* Retransmit rumble packets if they've lasted longer than the hardware supports */
|
||||
if ((ctx->left_motor_amplitude != 0 || ctx->right_motor_amplitude != 0) &&
|
||||
SDL_TICKS_PASSED(SDL_GetTicks(), ctx->last_rumble_time + RUMBLE_REFRESH_INTERVAL_MS)) {
|
||||
SDL_GetTicks() >= (ctx->last_rumble_time + RUMBLE_REFRESH_INTERVAL_MS)) {
|
||||
ctx->rumble_update_pending = SDL_TRUE;
|
||||
HIDAPI_DriverShield_SendNextRumble(device);
|
||||
}
|
||||
|
||||
@@ -254,18 +254,18 @@ typedef struct
|
||||
SwitchCommonOutputPacket_t m_RumblePacket;
|
||||
Uint8 m_rgucReadBuffer[k_unSwitchMaxOutputPacketLength];
|
||||
SDL_bool m_bRumbleActive;
|
||||
Uint32 m_unRumbleSent;
|
||||
Uint64 m_ulRumbleSent;
|
||||
SDL_bool m_bRumblePending;
|
||||
SDL_bool m_bRumbleZeroPending;
|
||||
Uint32 m_unRumblePending;
|
||||
SDL_bool m_bReportSensors;
|
||||
SDL_bool m_bHasSensorData;
|
||||
Uint32 m_unLastInput;
|
||||
Uint32 m_unLastIMUReset;
|
||||
Uint32 m_unIMUSampleTimestamp;
|
||||
Uint64 m_ulLastInput;
|
||||
Uint64 m_ulLastIMUReset;
|
||||
Uint64 m_ulIMUSampleTimestampNS;
|
||||
Uint32 m_unIMUSamples;
|
||||
Uint32 m_unIMUUpdateIntervalUS;
|
||||
Uint64 m_ulTimestampUS;
|
||||
Uint64 m_ulIMUUpdateIntervalNS;
|
||||
Uint64 m_ulTimestampNS;
|
||||
SDL_bool m_bVerticalMode;
|
||||
|
||||
SwitchInputOnlyControllerStatePacket_t m_lastInputOnlyState;
|
||||
@@ -329,8 +329,7 @@ static int WriteOutput(SDL_DriverSwitch_Context *ctx, const Uint8 *data, int siz
|
||||
static SwitchSubcommandInputPacket_t *ReadSubcommandReply(SDL_DriverSwitch_Context *ctx, ESwitchSubcommandIDs expectedID)
|
||||
{
|
||||
/* Average response time for messages is ~30ms */
|
||||
Uint32 TimeoutMs = 100;
|
||||
Uint32 startTicks = SDL_GetTicks();
|
||||
Uint64 endTicks = SDL_GetTicks() + 100;
|
||||
|
||||
int nRead = 0;
|
||||
while ((nRead = ReadInput(ctx)) != -1) {
|
||||
@@ -345,7 +344,7 @@ static SwitchSubcommandInputPacket_t *ReadSubcommandReply(SDL_DriverSwitch_Conte
|
||||
SDL_Delay(1);
|
||||
}
|
||||
|
||||
if (SDL_TICKS_PASSED(SDL_GetTicks(), startTicks + TimeoutMs)) {
|
||||
if (SDL_GetTicks() >= endTicks) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -355,8 +354,7 @@ static SwitchSubcommandInputPacket_t *ReadSubcommandReply(SDL_DriverSwitch_Conte
|
||||
static SDL_bool ReadProprietaryReply(SDL_DriverSwitch_Context *ctx, ESwitchProprietaryCommandIDs expectedID)
|
||||
{
|
||||
/* Average response time for messages is ~30ms */
|
||||
Uint32 TimeoutMs = 100;
|
||||
Uint32 startTicks = SDL_GetTicks();
|
||||
Uint64 endTicks = SDL_GetTicks() + 100;
|
||||
|
||||
int nRead = 0;
|
||||
while ((nRead = ReadInput(ctx)) != -1) {
|
||||
@@ -368,7 +366,7 @@ static SDL_bool ReadProprietaryReply(SDL_DriverSwitch_Context *ctx, ESwitchPropr
|
||||
SDL_Delay(1);
|
||||
}
|
||||
|
||||
if (SDL_TICKS_PASSED(SDL_GetTicks(), startTicks + TimeoutMs)) {
|
||||
if (SDL_GetTicks() >= endTicks) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -536,7 +534,7 @@ static SDL_bool WriteRumble(SDL_DriverSwitch_Context *ctx)
|
||||
ctx->m_nCommandNumber = (ctx->m_nCommandNumber + 1) & 0xF;
|
||||
|
||||
/* Refresh the rumble state periodically */
|
||||
ctx->m_unRumbleSent = SDL_GetTicks();
|
||||
ctx->m_ulRumbleSent = SDL_GetTicks();
|
||||
|
||||
return WritePacket(ctx, (Uint8 *)&ctx->m_RumblePacket, sizeof(ctx->m_RumblePacket));
|
||||
}
|
||||
@@ -1370,8 +1368,8 @@ static SDL_bool HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_Device *device, SDL_
|
||||
|
||||
/* Set up for input */
|
||||
ctx->m_bSyncWrite = SDL_FALSE;
|
||||
ctx->m_unLastIMUReset = ctx->m_unLastInput = SDL_GetTicks();
|
||||
ctx->m_unIMUUpdateIntervalUS = 5 * 1000; /* Start off at 5 ms update rate */
|
||||
ctx->m_ulLastIMUReset = ctx->m_ulLastInput = SDL_GetTicks();
|
||||
ctx->m_ulIMUUpdateIntervalNS = SDL_MS_TO_NS(5); /* Start off at 5 ms update rate */
|
||||
|
||||
/* Set up for vertical mode */
|
||||
ctx->m_bVerticalMode = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_VERTICAL_JOY_CONS, SDL_FALSE);
|
||||
@@ -1410,7 +1408,7 @@ static int HIDAPI_DriverSwitch_ActuallyRumbleJoystick(SDL_DriverSwitch_Context *
|
||||
|
||||
static int HIDAPI_DriverSwitch_SendPendingRumble(SDL_DriverSwitch_Context *ctx)
|
||||
{
|
||||
if (!SDL_TICKS_PASSED(SDL_GetTicks(), ctx->m_unRumbleSent + RUMBLE_WRITE_FREQUENCY_MS)) {
|
||||
if (SDL_GetTicks() < (ctx->m_ulRumbleSent + RUMBLE_WRITE_FREQUENCY_MS)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1419,7 +1417,7 @@ static int HIDAPI_DriverSwitch_SendPendingRumble(SDL_DriverSwitch_Context *ctx)
|
||||
Uint16 high_frequency_rumble = (Uint16)ctx->m_unRumblePending;
|
||||
|
||||
#ifdef DEBUG_RUMBLE
|
||||
SDL_Log("Sent pending rumble %d/%d, %d ms after previous rumble\n", low_frequency_rumble, high_frequency_rumble, SDL_GetTicks() - ctx->m_unRumbleSent);
|
||||
SDL_Log("Sent pending rumble %d/%d, %d ms after previous rumble\n", low_frequency_rumble, high_frequency_rumble, SDL_GetTicks() - ctx->m_ulRumbleSent);
|
||||
#endif
|
||||
ctx->m_bRumblePending = SDL_FALSE;
|
||||
ctx->m_unRumblePending = 0;
|
||||
@@ -1431,7 +1429,7 @@ static int HIDAPI_DriverSwitch_SendPendingRumble(SDL_DriverSwitch_Context *ctx)
|
||||
ctx->m_bRumbleZeroPending = SDL_FALSE;
|
||||
|
||||
#ifdef DEBUG_RUMBLE
|
||||
SDL_Log("Sent pending zero rumble, %d ms after previous rumble\n", SDL_GetTicks() - ctx->m_unRumbleSent);
|
||||
SDL_Log("Sent pending zero rumble, %d ms after previous rumble\n", SDL_GetTicks() - ctx->m_ulRumbleSent);
|
||||
#endif
|
||||
return HIDAPI_DriverSwitch_ActuallyRumbleJoystick(ctx, 0, 0);
|
||||
}
|
||||
@@ -1463,7 +1461,7 @@ static int HIDAPI_DriverSwitch_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joy
|
||||
}
|
||||
}
|
||||
|
||||
if (!SDL_TICKS_PASSED(SDL_GetTicks(), ctx->m_unRumbleSent + RUMBLE_WRITE_FREQUENCY_MS)) {
|
||||
if (SDL_GetTicks() < (ctx->m_ulRumbleSent + RUMBLE_WRITE_FREQUENCY_MS)) {
|
||||
if (low_frequency_rumble || high_frequency_rumble) {
|
||||
Uint32 unRumblePending = ((Uint32)low_frequency_rumble << 16) | high_frequency_rumble;
|
||||
|
||||
@@ -1522,7 +1520,7 @@ static int HIDAPI_DriverSwitch_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *devi
|
||||
SetIMUEnabled(ctx, enabled);
|
||||
ctx->m_bReportSensors = enabled;
|
||||
ctx->m_unIMUSamples = 0;
|
||||
ctx->m_unIMUSampleTimestamp = SDL_GetTicks();
|
||||
ctx->m_ulIMUSampleTimestampNS = SDL_GetTicksNS();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1988,22 +1986,22 @@ static void HandleFullControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_C
|
||||
/* We got three IMU samples, calculate the IMU update rate and timestamps */
|
||||
ctx->m_unIMUSamples += 3;
|
||||
if (ctx->m_unIMUSamples >= IMU_UPDATE_RATE_SAMPLE_FREQUENCY) {
|
||||
Uint32 now = SDL_GetTicks();
|
||||
Uint32 elapsed = (now - ctx->m_unIMUSampleTimestamp);
|
||||
Uint64 now = SDL_GetTicksNS();
|
||||
Uint64 elapsed = (now - ctx->m_ulIMUSampleTimestampNS);
|
||||
|
||||
if (elapsed > 0) {
|
||||
ctx->m_unIMUUpdateIntervalUS = (elapsed * 1000) / ctx->m_unIMUSamples;
|
||||
ctx->m_ulIMUUpdateIntervalNS = elapsed / ctx->m_unIMUSamples;
|
||||
}
|
||||
ctx->m_unIMUSamples = 0;
|
||||
ctx->m_unIMUSampleTimestamp = now;
|
||||
ctx->m_ulIMUSampleTimestampNS = now;
|
||||
}
|
||||
|
||||
ctx->m_ulTimestampUS += ctx->m_unIMUUpdateIntervalUS;
|
||||
timestamp[0] = ctx->m_ulTimestampUS;
|
||||
ctx->m_ulTimestampUS += ctx->m_unIMUUpdateIntervalUS;
|
||||
timestamp[1] = ctx->m_ulTimestampUS;
|
||||
ctx->m_ulTimestampUS += ctx->m_unIMUUpdateIntervalUS;
|
||||
timestamp[2] = ctx->m_ulTimestampUS;
|
||||
ctx->m_ulTimestampNS += ctx->m_ulIMUUpdateIntervalNS;
|
||||
timestamp[0] = SDL_NS_TO_US(ctx->m_ulTimestampNS);
|
||||
ctx->m_ulTimestampNS += ctx->m_ulIMUUpdateIntervalNS;
|
||||
timestamp[1] = SDL_NS_TO_US(ctx->m_ulTimestampNS);
|
||||
ctx->m_ulTimestampNS += ctx->m_ulIMUUpdateIntervalNS;
|
||||
timestamp[2] = SDL_NS_TO_US(ctx->m_ulTimestampNS);
|
||||
|
||||
if (!ctx->device->parent ||
|
||||
ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConRight) {
|
||||
@@ -2042,10 +2040,10 @@ static void HandleFullControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_C
|
||||
|
||||
} else if (ctx->m_bHasSensorData) {
|
||||
/* Uh oh, someone turned off the IMU? */
|
||||
const Uint32 IMU_RESET_DELAY_MS = 3000;
|
||||
Uint32 now = SDL_GetTicks();
|
||||
const int IMU_RESET_DELAY_MS = 3000;
|
||||
Uint64 now = SDL_GetTicks();
|
||||
|
||||
if (SDL_TICKS_PASSED(now, ctx->m_unLastIMUReset + IMU_RESET_DELAY_MS)) {
|
||||
if (now >= (ctx->m_ulLastIMUReset + IMU_RESET_DELAY_MS)) {
|
||||
SDL_HIDAPI_Device *device = ctx->device;
|
||||
|
||||
if (device->updating) {
|
||||
@@ -2057,7 +2055,7 @@ static void HandleFullControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_C
|
||||
if (device->updating) {
|
||||
SDL_LockMutex(device->dev_lock);
|
||||
}
|
||||
ctx->m_unLastIMUReset = now;
|
||||
ctx->m_ulLastIMUReset = now;
|
||||
}
|
||||
|
||||
} else {
|
||||
@@ -2074,7 +2072,7 @@ static SDL_bool HIDAPI_DriverSwitch_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
SDL_Joystick *joystick = NULL;
|
||||
int size;
|
||||
int packet_count = 0;
|
||||
Uint32 now = SDL_GetTicks();
|
||||
Uint64 now = SDL_GetTicks();
|
||||
|
||||
if (device->num_joysticks > 0) {
|
||||
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
|
||||
@@ -2085,7 +2083,7 @@ static SDL_bool HIDAPI_DriverSwitch_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
HIDAPI_DumpPacket("Nintendo Switch packet: size = %d", ctx->m_rgucReadBuffer, size);
|
||||
#endif
|
||||
++packet_count;
|
||||
ctx->m_unLastInput = now;
|
||||
ctx->m_ulLastInput = now;
|
||||
|
||||
if (joystick == NULL) {
|
||||
continue;
|
||||
@@ -2111,14 +2109,14 @@ static SDL_bool HIDAPI_DriverSwitch_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
if (packet_count == 0) {
|
||||
if (!ctx->m_bInputOnly && !device->is_bluetooth &&
|
||||
ctx->device->product_id != USB_PRODUCT_NINTENDO_SWITCH_JOYCON_GRIP) {
|
||||
const Uint32 INPUT_WAIT_TIMEOUT_MS = 100;
|
||||
if (SDL_TICKS_PASSED(now, ctx->m_unLastInput + INPUT_WAIT_TIMEOUT_MS)) {
|
||||
const int INPUT_WAIT_TIMEOUT_MS = 100;
|
||||
if (now >= (ctx->m_ulLastInput + INPUT_WAIT_TIMEOUT_MS)) {
|
||||
/* Steam may have put the controller back into non-reporting mode */
|
||||
WriteProprietary(ctx, k_eSwitchProprietaryCommandIDs_ForceUSB, NULL, 0, SDL_FALSE);
|
||||
}
|
||||
} else if (device->is_bluetooth) {
|
||||
const Uint32 INPUT_WAIT_TIMEOUT_MS = 3000;
|
||||
if (SDL_TICKS_PASSED(now, ctx->m_unLastInput + INPUT_WAIT_TIMEOUT_MS)) {
|
||||
const int INPUT_WAIT_TIMEOUT_MS = 3000;
|
||||
if (now >= (ctx->m_ulLastInput + INPUT_WAIT_TIMEOUT_MS)) {
|
||||
/* Bluetooth may have disconnected, try reopening the controller */
|
||||
size = -1;
|
||||
}
|
||||
@@ -2128,9 +2126,9 @@ static SDL_bool HIDAPI_DriverSwitch_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
if (ctx->m_bRumblePending || ctx->m_bRumbleZeroPending) {
|
||||
HIDAPI_DriverSwitch_SendPendingRumble(ctx);
|
||||
} else if (ctx->m_bRumbleActive &&
|
||||
SDL_TICKS_PASSED(now, ctx->m_unRumbleSent + RUMBLE_REFRESH_FREQUENCY_MS)) {
|
||||
now >= (ctx->m_ulRumbleSent + RUMBLE_REFRESH_FREQUENCY_MS)) {
|
||||
#ifdef DEBUG_RUMBLE
|
||||
SDL_Log("Sent continuing rumble, %d ms after previous rumble\n", now - ctx->m_unRumbleSent);
|
||||
SDL_Log("Sent continuing rumble, %d ms after previous rumble\n", now - ctx->m_ulRumbleSent);
|
||||
#endif
|
||||
WriteRumble(ctx);
|
||||
}
|
||||
|
||||
@@ -142,9 +142,9 @@ typedef struct
|
||||
Uint8 m_ucMotionPlusMode;
|
||||
SDL_bool m_bReportSensors;
|
||||
Uint8 m_rgucReadBuffer[k_unWiiPacketDataLength];
|
||||
Uint32 m_unLastInput;
|
||||
Uint32 m_unLastStatus;
|
||||
Uint32 m_unNextMotionPlusCheck;
|
||||
Uint64 m_ulLastInput;
|
||||
Uint64 m_ulLastStatus;
|
||||
Uint64 m_ulNextMotionPlusCheck;
|
||||
SDL_bool m_bDisconnected;
|
||||
|
||||
struct StickCalibrationData
|
||||
@@ -225,8 +225,7 @@ static SDL_bool WriteOutput(SDL_DriverWii_Context *ctx, const Uint8 *data, int s
|
||||
|
||||
static SDL_bool ReadInputSync(SDL_DriverWii_Context *ctx, EWiiInputReportIDs expectedID, SDL_bool (*isMine)(const Uint8 *))
|
||||
{
|
||||
Uint32 TimeoutMs = 250; /* Seeing successful reads after about 200 ms */
|
||||
Uint32 startTicks = SDL_GetTicks();
|
||||
Uint64 endTicks = SDL_GetTicks() + 250; /* Seeing successful reads after about 200 ms */
|
||||
|
||||
int nRead = 0;
|
||||
while ((nRead = ReadInput(ctx)) != -1) {
|
||||
@@ -235,7 +234,7 @@ static SDL_bool ReadInputSync(SDL_DriverWii_Context *ctx, EWiiInputReportIDs exp
|
||||
return SDL_TRUE;
|
||||
}
|
||||
} else {
|
||||
if (SDL_TICKS_PASSED(SDL_GetTicks(), startTicks + TimeoutMs)) {
|
||||
if (SDL_GetTicks() >= endTicks) {
|
||||
break;
|
||||
}
|
||||
SDL_Delay(1);
|
||||
@@ -443,10 +442,7 @@ static SDL_bool NeedsPeriodicMotionPlusCheck(SDL_DriverWii_Context *ctx, SDL_boo
|
||||
|
||||
static void SchedulePeriodicMotionPlusCheck(SDL_DriverWii_Context *ctx)
|
||||
{
|
||||
ctx->m_unNextMotionPlusCheck = SDL_GetTicks() + MOTION_PLUS_UPDATE_TIME_MS;
|
||||
if (!ctx->m_unNextMotionPlusCheck) {
|
||||
ctx->m_unNextMotionPlusCheck = 1;
|
||||
}
|
||||
ctx->m_ulNextMotionPlusCheck = SDL_GetTicks() + MOTION_PLUS_UPDATE_TIME_MS;
|
||||
}
|
||||
|
||||
static void CheckMotionPlusConnection(SDL_DriverWii_Context *ctx)
|
||||
@@ -807,7 +803,7 @@ static SDL_bool HIDAPI_DriverWii_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joy
|
||||
}
|
||||
joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
|
||||
|
||||
ctx->m_unLastInput = SDL_GetTicks();
|
||||
ctx->m_ulLastInput = SDL_GetTicks();
|
||||
|
||||
return SDL_TRUE;
|
||||
}
|
||||
@@ -1433,7 +1429,7 @@ static void HandleStatus(SDL_DriverWii_Context *ctx, SDL_Joystick *joystick)
|
||||
* Motion Plus packets.
|
||||
*/
|
||||
if (NeedsPeriodicMotionPlusCheck(ctx, SDL_TRUE)) {
|
||||
ctx->m_unNextMotionPlusCheck = SDL_GetTicks();
|
||||
ctx->m_ulNextMotionPlusCheck = SDL_GetTicks();
|
||||
}
|
||||
|
||||
} else if (hadExtension != hasExtension) {
|
||||
@@ -1568,7 +1564,7 @@ static SDL_bool HIDAPI_DriverWii_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
SDL_DriverWii_Context *ctx = (SDL_DriverWii_Context *)device->context;
|
||||
SDL_Joystick *joystick = NULL;
|
||||
int size;
|
||||
Uint32 now;
|
||||
Uint64 now;
|
||||
|
||||
if (device->num_joysticks > 0) {
|
||||
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
|
||||
@@ -1582,7 +1578,7 @@ static SDL_bool HIDAPI_DriverWii_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
if (joystick) {
|
||||
HandleInput(ctx, joystick);
|
||||
}
|
||||
ctx->m_unLastInput = now;
|
||||
ctx->m_ulLastInput = now;
|
||||
}
|
||||
|
||||
/* Check to see if we've lost connection to the controller.
|
||||
@@ -1591,7 +1587,7 @@ static SDL_bool HIDAPI_DriverWii_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
{
|
||||
SDL_COMPILE_TIME_ASSERT(ENABLE_CONTINUOUS_REPORTING, ENABLE_CONTINUOUS_REPORTING);
|
||||
}
|
||||
if (SDL_TICKS_PASSED(now, ctx->m_unLastInput + INPUT_WAIT_TIMEOUT_MS)) {
|
||||
if (now >= (ctx->m_ulLastInput + INPUT_WAIT_TIMEOUT_MS)) {
|
||||
/* Bluetooth may have disconnected, try reopening the controller */
|
||||
size = -1;
|
||||
}
|
||||
@@ -1601,26 +1597,24 @@ static SDL_bool HIDAPI_DriverWii_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
if (ctx->m_eExtensionControllerType != k_eWiiExtensionControllerType_WiiUPro) {
|
||||
|
||||
/* Check to see if the Motion Plus extension status has changed */
|
||||
if (ctx->m_unNextMotionPlusCheck &&
|
||||
SDL_TICKS_PASSED(now, ctx->m_unNextMotionPlusCheck)) {
|
||||
if (ctx->m_ulNextMotionPlusCheck && now >= ctx->m_ulNextMotionPlusCheck) {
|
||||
CheckMotionPlusConnection(ctx);
|
||||
if (NeedsPeriodicMotionPlusCheck(ctx, SDL_FALSE)) {
|
||||
SchedulePeriodicMotionPlusCheck(ctx);
|
||||
} else {
|
||||
ctx->m_unNextMotionPlusCheck = 0;
|
||||
ctx->m_ulNextMotionPlusCheck = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Request a status update periodically to make sure our battery value is up to date */
|
||||
if (!ctx->m_unLastStatus ||
|
||||
SDL_TICKS_PASSED(now, ctx->m_unLastStatus + STATUS_UPDATE_TIME_MS)) {
|
||||
if (!ctx->m_ulLastStatus || now >= (ctx->m_ulLastStatus + STATUS_UPDATE_TIME_MS)) {
|
||||
Uint8 data[2];
|
||||
|
||||
data[0] = k_eWiiOutputReportIDs_StatusRequest;
|
||||
data[1] = ctx->m_bRumbleActive;
|
||||
WriteOutput(ctx, data, sizeof(data), SDL_FALSE);
|
||||
|
||||
ctx->m_unLastStatus = now;
|
||||
ctx->m_ulLastStatus = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,9 +117,9 @@ typedef struct
|
||||
SDL_bool bluetooth;
|
||||
SDL_XboxOneInitState init_state;
|
||||
int init_packet;
|
||||
Uint32 start_time;
|
||||
Uint64 start_time;
|
||||
Uint8 sequence;
|
||||
Uint32 send_time;
|
||||
Uint64 send_time;
|
||||
SDL_bool has_guide_packet;
|
||||
SDL_bool has_color_led;
|
||||
SDL_bool has_paddles;
|
||||
@@ -132,7 +132,7 @@ typedef struct
|
||||
Uint8 left_trigger_rumble;
|
||||
Uint8 right_trigger_rumble;
|
||||
SDL_XboxOneRumbleState rumble_state;
|
||||
Uint32 rumble_time;
|
||||
Uint64 rumble_time;
|
||||
SDL_bool rumble_pending;
|
||||
Uint8 last_state[USB_PACKET_LENGTH];
|
||||
} SDL_DriverXboxOne_Context;
|
||||
@@ -456,8 +456,8 @@ static int HIDAPI_DriverXboxOne_UpdateRumble(SDL_HIDAPI_Device *device)
|
||||
}
|
||||
|
||||
if (ctx->rumble_state == XBOX_ONE_RUMBLE_STATE_BUSY) {
|
||||
const Uint32 RUMBLE_BUSY_TIME_MS = ctx->bluetooth ? 50 : 10;
|
||||
if (SDL_TICKS_PASSED(SDL_GetTicks(), ctx->rumble_time + RUMBLE_BUSY_TIME_MS)) {
|
||||
const int RUMBLE_BUSY_TIME_MS = ctx->bluetooth ? 50 : 10;
|
||||
if (SDL_GetTicks() >= (ctx->rumble_time + RUMBLE_BUSY_TIME_MS)) {
|
||||
ctx->rumble_time = 0;
|
||||
ctx->rumble_state = XBOX_ONE_RUMBLE_STATE_IDLE;
|
||||
}
|
||||
@@ -1087,7 +1087,7 @@ static SDL_bool HIDAPI_DriverXboxOne_UpdateInitState(SDL_HIDAPI_Device *device,
|
||||
#endif
|
||||
break;
|
||||
case XBOX_ONE_INIT_STATE_NEGOTIATING:
|
||||
if (SDL_TICKS_PASSED(SDL_GetTicks(), ctx->send_time + CONTROLLER_NEGOTIATION_TIMEOUT_MS)) {
|
||||
if (SDL_GetTicks() >= (ctx->send_time + CONTROLLER_NEGOTIATION_TIMEOUT_MS)) {
|
||||
/* We haven't heard anything, let's move on */
|
||||
#ifdef DEBUG_JOYSTICK
|
||||
SDL_Log("Init sequence %d timed out after %u ms\n", ctx->init_packet, (SDL_GetTicks() - ctx->send_time));
|
||||
@@ -1099,7 +1099,7 @@ static SDL_bool HIDAPI_DriverXboxOne_UpdateInitState(SDL_HIDAPI_Device *device,
|
||||
}
|
||||
break;
|
||||
case XBOX_ONE_INIT_STATE_PREPARE_INPUT:
|
||||
if (SDL_TICKS_PASSED(SDL_GetTicks(), ctx->send_time + CONTROLLER_PREPARE_INPUT_TIMEOUT_MS)) {
|
||||
if (SDL_GetTicks() >= (ctx->send_time + CONTROLLER_PREPARE_INPUT_TIMEOUT_MS)) {
|
||||
#ifdef DEBUG_JOYSTICK
|
||||
SDL_Log("Prepare input complete after %u ms\n", (SDL_GetTicks() - ctx->send_time));
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user