src/joystick/hidapi/SDL_hidapi_ps4.c
author Sam Lantinga
Mon, 16 Dec 2019 10:20:03 -0800
changeset 13348 448528dc13da
parent 13277 b72b5a030eee
child 13354 8c22865bd138
permissions -rw-r--r--
Fixed bug 4898 - No rumble because of integer overflow in SDL_JoystickRumble

meyraud705

On a Dualshock 4 controller using hidapi driver, calling SDL_JoystickRumble with a duration too long (SDL_HAPTIC_INFINITY for example) causes the rumble to stop immediately.

This happens because of integer overflow on line 301 of SDL_hidapi_ps4.c
(https://hg.libsdl.org/SDL/file/99ecd178999f/src/joystick/hidapi/SDL_hidapi_ps4.c#l301), which sets expiration time in the past.
slouken@12088
     1
/*
slouken@12088
     2
  Simple DirectMedia Layer
slouken@12503
     3
  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
slouken@12088
     4
slouken@12088
     5
  This software is provided 'as-is', without any express or implied
slouken@12088
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@12088
     7
  arising from the use of this software.
slouken@12088
     8
slouken@12088
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@12088
    10
  including commercial applications, and to alter it and redistribute it
slouken@12088
    11
  freely, subject to the following restrictions:
slouken@12088
    12
slouken@12088
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@12088
    14
     claim that you wrote the original software. If you use this software
slouken@12088
    15
     in a product, an acknowledgment in the product documentation would be
slouken@12088
    16
     appreciated but is not required.
slouken@12088
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@12088
    18
     misrepresented as being the original software.
slouken@12088
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@12088
    20
*/
slouken@12097
    21
/* This driver supports both simplified reports and the extended input reports enabled by Steam.
slouken@12097
    22
   Code and logic contributed by Valve Corporation under the SDL zlib license.
slouken@12097
    23
*/
slouken@12088
    24
#include "../../SDL_internal.h"
slouken@12088
    25
slouken@12088
    26
#ifdef SDL_JOYSTICK_HIDAPI
slouken@12088
    27
slouken@12088
    28
#include "SDL_hints.h"
slouken@12088
    29
#include "SDL_log.h"
slouken@12088
    30
#include "SDL_events.h"
slouken@12088
    31
#include "SDL_timer.h"
slouken@12088
    32
#include "SDL_joystick.h"
slouken@12088
    33
#include "SDL_gamecontroller.h"
slouken@12088
    34
#include "../SDL_sysjoystick.h"
slouken@12088
    35
#include "SDL_hidapijoystick_c.h"
slouken@12088
    36
slouken@12088
    37
slouken@12088
    38
#ifdef SDL_JOYSTICK_HIDAPI_PS4
slouken@12088
    39
slouken@12088
    40
#define SONY_USB_VID        0x054C
slouken@12088
    41
#define SONY_DS4_PID        0x05C4
slouken@12088
    42
#define SONY_DS4_DONGLE_PID 0x0BA0
slouken@12088
    43
#define SONY_DS4_SLIM_PID   0x09CC
slouken@12088
    44
slouken@12172
    45
#define RAZER_USB_VID       0x1532
slouken@12172
    46
#define RAZER_PANTHERA_PID  0X0401
slouken@12176
    47
#define RAZER_PANTHERA_EVO_PID  0x1008
slouken@12172
    48
slouken@12088
    49
#define USB_PACKET_LENGTH   64
slouken@12088
    50
slouken@12088
    51
typedef enum
slouken@12088
    52
{
slouken@12088
    53
    k_EPS4ReportIdUsbState = 1,
slouken@12088
    54
    k_EPS4ReportIdUsbEffects = 5,
slouken@12088
    55
    k_EPS4ReportIdBluetoothState = 17,
slouken@12088
    56
    k_EPS4ReportIdBluetoothEffects = 17,
slouken@12088
    57
    k_EPS4ReportIdDisconnectMessage = 226,
slouken@12088
    58
} EPS4ReportId;
slouken@12088
    59
slouken@12088
    60
typedef enum 
slouken@12088
    61
{
slouken@12088
    62
    k_ePS4FeatureReportIdGyroCalibration_USB = 0x02,
slouken@12088
    63
    k_ePS4FeatureReportIdGyroCalibration_BT = 0x05,
slouken@12088
    64
    k_ePS4FeatureReportIdSerialNumber = 0x12,
slouken@12088
    65
} EPS4FeatureReportID;
slouken@12088
    66
slouken@12088
    67
typedef struct
slouken@12088
    68
{
slouken@12088
    69
    Uint8 ucLeftJoystickX;
slouken@12088
    70
    Uint8 ucLeftJoystickY;
slouken@12088
    71
    Uint8 ucRightJoystickX;
slouken@12088
    72
    Uint8 ucRightJoystickY;
slouken@12088
    73
    Uint8 rgucButtonsHatAndCounter[ 3 ];
slouken@12088
    74
    Uint8 ucTriggerLeft;
slouken@12088
    75
    Uint8 ucTriggerRight;
slouken@12088
    76
    Uint8 _rgucPad0[ 3 ];
slouken@12088
    77
    Sint16 sGyroX;
slouken@12088
    78
    Sint16 sGyroY;
slouken@12088
    79
    Sint16 sGyroZ;
slouken@12088
    80
    Sint16 sAccelX;
slouken@12088
    81
    Sint16 sAccelY;
slouken@12088
    82
    Sint16 sAccelZ;
slouken@12088
    83
    Uint8 _rgucPad1[ 5 ];
slouken@12088
    84
    Uint8 ucBatteryLevel;
slouken@12088
    85
    Uint8 _rgucPad2[ 4 ];
slouken@12088
    86
    Uint8 ucTrackpadCounter1;
slouken@12088
    87
    Uint8 rgucTrackpadData1[ 3 ];
slouken@12088
    88
    Uint8 ucTrackpadCounter2;
slouken@12088
    89
    Uint8 rgucTrackpadData2[ 3 ];
slouken@12088
    90
} PS4StatePacket_t;
slouken@12088
    91
slouken@12088
    92
typedef struct
slouken@12088
    93
{
slouken@12088
    94
    Uint8 ucRumbleRight;
slouken@12088
    95
    Uint8 ucRumbleLeft;
slouken@12088
    96
    Uint8 ucLedRed;
slouken@12088
    97
    Uint8 ucLedGreen;
slouken@12088
    98
    Uint8 ucLedBlue;
slouken@12088
    99
    Uint8 ucLedDelayOn;
slouken@12088
   100
    Uint8 ucLedDelayOff;
slouken@12088
   101
    Uint8 _rgucPad0[ 8 ];
slouken@12088
   102
    Uint8 ucVolumeLeft;
slouken@12088
   103
    Uint8 ucVolumeRight;
slouken@12088
   104
    Uint8 ucVolumeMic;
slouken@12088
   105
    Uint8 ucVolumeSpeaker;
slouken@12088
   106
} DS4EffectsState_t;
slouken@12088
   107
slouken@12088
   108
typedef struct {
slouken@12088
   109
    SDL_bool is_dongle;
slouken@12088
   110
    SDL_bool is_bluetooth;
slouken@12088
   111
    SDL_bool audio_supported;
slouken@12163
   112
    SDL_bool rumble_supported;
slouken@12088
   113
    Uint8 volume;
slouken@12088
   114
    Uint32 last_volume_check;
slouken@12088
   115
    Uint32 rumble_expiration;
slouken@12088
   116
    PS4StatePacket_t last_state;
slouken@12088
   117
} SDL_DriverPS4_Context;
slouken@12088
   118
slouken@12088
   119
slouken@12088
   120
/* Public domain CRC implementation adapted from:
slouken@12088
   121
   http://home.thep.lu.se/~bjorn/crc/crc32_simple.c
slouken@12088
   122
*/
slouken@12088
   123
static Uint32 crc32_for_byte(Uint32 r)
slouken@12088
   124
{
slouken@12088
   125
    int i;
slouken@12088
   126
    for(i = 0; i < 8; ++i) {
slouken@12088
   127
        r = (r & 1? 0: (Uint32)0xEDB88320L) ^ r >> 1;
slouken@12088
   128
    }
slouken@12088
   129
    return r ^ (Uint32)0xFF000000L;
slouken@12088
   130
}
slouken@12088
   131
slouken@12088
   132
static Uint32 crc32(Uint32 crc, const void *data, int count)
slouken@12088
   133
{
slouken@12088
   134
    int i;
slouken@12088
   135
    for(i = 0; i < count; ++i) {
slouken@12088
   136
        crc = crc32_for_byte((Uint8)crc ^ ((const Uint8*)data)[i]) ^ crc >> 8;
slouken@12088
   137
    }
slouken@12088
   138
    return crc;
slouken@12088
   139
}
slouken@12088
   140
slouken@12088
   141
static SDL_bool
slouken@13160
   142
HIDAPI_DriverPS4_IsSupportedDevice(Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, const char *name)
slouken@12088
   143
{
slouken@13277
   144
    return (SDL_GetJoystickGameControllerType(vendor_id, product_id, name) == SDL_CONTROLLER_TYPE_PS4);
slouken@12088
   145
}
slouken@12088
   146
slouken@12088
   147
static const char *
slouken@12088
   148
HIDAPI_DriverPS4_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
slouken@12088
   149
{
slouken@12088
   150
    if (vendor_id == SONY_USB_VID) {
slouken@12088
   151
        return "PS4 Controller";
slouken@12088
   152
    }
slouken@12088
   153
    return NULL;
slouken@12088
   154
}
slouken@12088
   155
slouken@12088
   156
static SDL_bool ReadFeatureReport(hid_device *dev, Uint8 report_id, Uint8 *data, size_t size)
slouken@12088
   157
{
slouken@12088
   158
    Uint8 report[USB_PACKET_LENGTH + 1];
slouken@12088
   159
slouken@12088
   160
    SDL_memset(report, 0, sizeof(report));
slouken@12088
   161
    report[0] = report_id;
slouken@12088
   162
    if (hid_get_feature_report(dev, report, sizeof(report)) < 0) {
slouken@12088
   163
        return SDL_FALSE;
slouken@12088
   164
    }
slouken@12088
   165
    SDL_memcpy(data, report, SDL_min(size, sizeof(report)));
slouken@12088
   166
    return SDL_TRUE;
slouken@12088
   167
}
slouken@12088
   168
slouken@12088
   169
static SDL_bool CheckUSBConnected(hid_device *dev)
slouken@12088
   170
{
slouken@12088
   171
    int i;
slouken@12088
   172
    Uint8 data[16];
slouken@12088
   173
slouken@12088
   174
    /* This will fail if we're on Bluetooth */
slouken@12088
   175
    if (ReadFeatureReport(dev, k_ePS4FeatureReportIdSerialNumber, data, sizeof(data))) {
slouken@12088
   176
        for (i = 0; i < sizeof(data); ++i) {
slouken@12088
   177
            if (data[i] != 0x00) {
slouken@12088
   178
                return SDL_TRUE;
slouken@12088
   179
            }
slouken@12088
   180
        }
slouken@12088
   181
        /* Maybe the dongle without a connected controller? */
slouken@12088
   182
    }
slouken@12088
   183
    return SDL_FALSE;
slouken@12088
   184
}
slouken@12088
   185
slouken@12172
   186
static SDL_bool HIDAPI_DriverPS4_CanRumble(Uint16 vendor_id, Uint16 product_id)
slouken@12172
   187
{
slouken@12172
   188
    /* The Razer Panthera fight stick hangs when trying to rumble */
slouken@12176
   189
    if (vendor_id == RAZER_USB_VID &&
slouken@12176
   190
        (product_id == RAZER_PANTHERA_PID || product_id == RAZER_PANTHERA_EVO_PID)) {
slouken@12172
   191
        return SDL_FALSE;
slouken@12172
   192
    }
slouken@12172
   193
    return SDL_TRUE;
slouken@12172
   194
}
slouken@12172
   195
slouken@12896
   196
static int HIDAPI_DriverPS4_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms);
slouken@12896
   197
slouken@12088
   198
static SDL_bool
slouken@12896
   199
HIDAPI_DriverPS4_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id, Uint16 product_id, void **context)
slouken@12088
   200
{
slouken@12088
   201
    SDL_DriverPS4_Context *ctx;
slouken@12088
   202
slouken@12088
   203
    ctx = (SDL_DriverPS4_Context *)SDL_calloc(1, sizeof(*ctx));
slouken@12088
   204
    if (!ctx) {
slouken@12088
   205
        SDL_OutOfMemory();
slouken@12088
   206
        return SDL_FALSE;
slouken@12088
   207
    }
slouken@12896
   208
    *context = ctx;
slouken@12088
   209
slouken@12088
   210
    /* Check for type of connection */
slouken@12088
   211
    ctx->is_dongle = (vendor_id == SONY_USB_VID && product_id == SONY_DS4_DONGLE_PID);
slouken@12088
   212
    if (ctx->is_dongle) {
slouken@12088
   213
        ctx->is_bluetooth = SDL_FALSE;
slouken@12088
   214
    } else if (vendor_id == SONY_USB_VID) {
slouken@12896
   215
        ctx->is_bluetooth = !CheckUSBConnected(dev);
slouken@12088
   216
    } else {
slouken@12088
   217
        /* Third party controllers appear to all be wired */
slouken@12088
   218
        ctx->is_bluetooth = SDL_FALSE;
slouken@12088
   219
    }
slouken@12088
   220
#ifdef DEBUG_PS4
slouken@12088
   221
    SDL_Log("PS4 dongle = %s, bluetooth = %s\n", ctx->is_dongle ? "TRUE" : "FALSE", ctx->is_bluetooth ? "TRUE" : "FALSE");
slouken@12088
   222
#endif
slouken@12088
   223
slouken@12088
   224
    /* Check to see if audio is supported */
slouken@12088
   225
    if (vendor_id == SONY_USB_VID &&
slouken@12088
   226
        (product_id == SONY_DS4_SLIM_PID || product_id == SONY_DS4_DONGLE_PID )) {
slouken@12088
   227
        ctx->audio_supported = SDL_TRUE;
slouken@12088
   228
    }
slouken@12088
   229
slouken@12172
   230
    if (HIDAPI_DriverPS4_CanRumble(vendor_id, product_id)) {
slouken@12172
   231
        if (ctx->is_bluetooth) {
slouken@12172
   232
            ctx->rumble_supported = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, SDL_FALSE);
slouken@12172
   233
        } else {
slouken@12172
   234
            ctx->rumble_supported = SDL_TRUE;
slouken@12172
   235
        }
slouken@12163
   236
    }
slouken@12163
   237
slouken@12088
   238
    /* Initialize LED and effect state */
slouken@12896
   239
    HIDAPI_DriverPS4_Rumble(joystick, dev, ctx, 0, 0, 0);
slouken@12088
   240
slouken@12088
   241
    /* Initialize the joystick capabilities */
slouken@12790
   242
    joystick->nbuttons = 16;
slouken@12088
   243
    joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
slouken@12088
   244
    joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
slouken@12088
   245
slouken@12088
   246
    return SDL_TRUE;
slouken@12088
   247
}
slouken@12088
   248
slouken@12088
   249
static int
slouken@12896
   250
HIDAPI_DriverPS4_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
slouken@12088
   251
{
slouken@12896
   252
    SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)context;
slouken@12088
   253
    DS4EffectsState_t *effects;
slouken@12088
   254
    Uint8 data[78];
slouken@12088
   255
    int report_size, offset;
slouken@12088
   256
slouken@12163
   257
    if (!ctx->rumble_supported) {
slouken@12163
   258
        return SDL_Unsupported();
slouken@12163
   259
    }
slouken@12163
   260
slouken@12088
   261
    /* In order to send rumble, we have to send a complete effect packet */
slouken@12088
   262
    SDL_memset(data, 0, sizeof(data));
slouken@12088
   263
slouken@12088
   264
    if (ctx->is_bluetooth) {
slouken@12088
   265
        data[0] = k_EPS4ReportIdBluetoothEffects;
slouken@12088
   266
        data[1] = 0xC0 | 0x04;  /* Magic value HID + CRC, also sets interval to 4ms for samples */
slouken@12088
   267
        data[3] = 0x03;  /* 0x1 is rumble, 0x2 is lightbar, 0x4 is the blink interval */
slouken@12088
   268
slouken@12088
   269
        report_size = 78;
slouken@12088
   270
        offset = 6;
slouken@12088
   271
    } else {
slouken@12088
   272
        data[0] = k_EPS4ReportIdUsbEffects;
slouken@12088
   273
        data[1] = 0x07;  /* Magic value */
slouken@12088
   274
slouken@12088
   275
        report_size = 32;
slouken@12088
   276
        offset = 4;
slouken@12088
   277
    }
slouken@12088
   278
    effects = (DS4EffectsState_t *)&data[offset];
slouken@12088
   279
slouken@12088
   280
    effects->ucRumbleLeft = (low_frequency_rumble >> 8);
slouken@12088
   281
    effects->ucRumbleRight = (high_frequency_rumble >> 8);
slouken@12088
   282
slouken@12088
   283
    effects->ucLedRed = 0;
slouken@12088
   284
    effects->ucLedGreen = 0;
slouken@12088
   285
    effects->ucLedBlue = 80;
slouken@12088
   286
slouken@12088
   287
    if (ctx->is_bluetooth) {
slouken@12088
   288
        /* Bluetooth reports need a CRC at the end of the packet (at least on Linux) */
slouken@12088
   289
        Uint8 ubHdr = 0xA2; /* hidp header is part of the CRC calculation */
slouken@12088
   290
        Uint32 unCRC;
slouken@12088
   291
        unCRC = crc32(0, &ubHdr, 1);
slouken@12088
   292
        unCRC = crc32(unCRC, data, (Uint32)(report_size - sizeof(unCRC)));
slouken@12088
   293
        SDL_memcpy(&data[report_size - sizeof(unCRC)], &unCRC, sizeof(unCRC));
slouken@12088
   294
    }
slouken@12088
   295
slouken@12896
   296
    if (hid_write(dev, data, report_size) != report_size) {
slouken@12088
   297
        return SDL_SetError("Couldn't send rumble packet");
slouken@12088
   298
    }
slouken@12088
   299
slouken@12088
   300
    if ((low_frequency_rumble || high_frequency_rumble) && duration_ms) {
slouken@13348
   301
        ctx->rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS);
slouken@12088
   302
    } else {
slouken@12088
   303
        ctx->rumble_expiration = 0;
slouken@12088
   304
    }
slouken@12088
   305
    return 0;
slouken@12088
   306
}
slouken@12088
   307
slouken@12088
   308
static void
slouken@12088
   309
HIDAPI_DriverPS4_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverPS4_Context *ctx, PS4StatePacket_t *packet)
slouken@12088
   310
{
slouken@12088
   311
    Sint16 axis;
slouken@12088
   312
slouken@12088
   313
    if (ctx->last_state.rgucButtonsHatAndCounter[0] != packet->rgucButtonsHatAndCounter[0]) {
slouken@12088
   314
        {
slouken@12088
   315
            Uint8 data = (packet->rgucButtonsHatAndCounter[0] >> 4);
slouken@12088
   316
slouken@12088
   317
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   318
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   319
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data & 0x04) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   320
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data & 0x08) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   321
        }
slouken@12088
   322
        {
slouken@12088
   323
            Uint8 data = (packet->rgucButtonsHatAndCounter[0] & 0x0F);
slouken@12088
   324
            SDL_bool dpad_up = SDL_FALSE;
slouken@12088
   325
            SDL_bool dpad_down = SDL_FALSE;
slouken@12088
   326
            SDL_bool dpad_left = SDL_FALSE;
slouken@12088
   327
            SDL_bool dpad_right = SDL_FALSE;
slouken@12088
   328
slouken@12088
   329
            switch (data) {
slouken@12088
   330
            case 0:
slouken@12088
   331
                dpad_up = SDL_TRUE;
slouken@12088
   332
                break;
slouken@12088
   333
            case 1:
slouken@12088
   334
                dpad_up = SDL_TRUE;
slouken@12088
   335
                dpad_right = SDL_TRUE;
slouken@12088
   336
                break;
slouken@12088
   337
            case 2:
slouken@12088
   338
                dpad_right = SDL_TRUE;
slouken@12088
   339
                break;
slouken@12088
   340
            case 3:
slouken@12088
   341
                dpad_right = SDL_TRUE;
slouken@12088
   342
                dpad_down = SDL_TRUE;
slouken@12088
   343
                break;
slouken@12088
   344
            case 4:
slouken@12088
   345
                dpad_down = SDL_TRUE;
slouken@12088
   346
                break;
slouken@12088
   347
            case 5:
slouken@12088
   348
                dpad_left = SDL_TRUE;
slouken@12088
   349
                dpad_down = SDL_TRUE;
slouken@12088
   350
                break;
slouken@12088
   351
            case 6:
slouken@12088
   352
                dpad_left = SDL_TRUE;
slouken@12088
   353
                break;
slouken@12088
   354
            case 7:
slouken@12088
   355
                dpad_up = SDL_TRUE;
slouken@12088
   356
                dpad_left = SDL_TRUE;
slouken@12088
   357
                break;
slouken@12088
   358
            default:
slouken@12088
   359
                break;
slouken@12088
   360
            }
slouken@12088
   361
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
slouken@12088
   362
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
slouken@12088
   363
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
slouken@12088
   364
            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
slouken@12088
   365
        }
slouken@12088
   366
    }
slouken@12088
   367
slouken@12088
   368
    if (ctx->last_state.rgucButtonsHatAndCounter[1] != packet->rgucButtonsHatAndCounter[1]) {
slouken@12088
   369
        Uint8 data = packet->rgucButtonsHatAndCounter[1];
slouken@12088
   370
slouken@12088
   371
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   372
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   373
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data & 0x10) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   374
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data & 0x20) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   375
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data & 0x40) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   376
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data & 0x80) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   377
    }
slouken@12088
   378
slouken@12088
   379
    if (ctx->last_state.rgucButtonsHatAndCounter[2] != packet->rgucButtonsHatAndCounter[2]) {
slouken@12088
   380
        Uint8 data = (packet->rgucButtonsHatAndCounter[2] & 0x03);
slouken@12088
   381
slouken@12088
   382
        SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
slouken@12790
   383
        SDL_PrivateJoystickButton(joystick, 15, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
slouken@12088
   384
    }
slouken@12088
   385
slouken@12088
   386
    axis = ((int)packet->ucTriggerLeft * 257) - 32768;
slouken@12088
   387
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
slouken@12088
   388
    axis = ((int)packet->ucTriggerRight * 257) - 32768;
slouken@12088
   389
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
slouken@12088
   390
    axis = ((int)packet->ucLeftJoystickX * 257) - 32768;
slouken@12088
   391
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
slouken@12088
   392
    axis = ((int)packet->ucLeftJoystickY * 257) - 32768;
slouken@12088
   393
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
slouken@12088
   394
    axis = ((int)packet->ucRightJoystickX * 257) - 32768;
slouken@12088
   395
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
slouken@12088
   396
    axis = ((int)packet->ucRightJoystickY * 257) - 32768;
slouken@12088
   397
    SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
slouken@12088
   398
slouken@12088
   399
    if (packet->ucBatteryLevel & 0x10) {
slouken@12088
   400
        joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
slouken@12088
   401
    } else {
slouken@12088
   402
        /* Battery level ranges from 0 to 10 */
slouken@12088
   403
        int level = (packet->ucBatteryLevel & 0xF);
slouken@12088
   404
        if (level == 0) {
slouken@12088
   405
            joystick->epowerlevel = SDL_JOYSTICK_POWER_EMPTY;
slouken@12088
   406
        } else if (level <= 2) {
slouken@12088
   407
            joystick->epowerlevel = SDL_JOYSTICK_POWER_LOW;
slouken@12088
   408
        } else if (level <= 7) {
slouken@12088
   409
            joystick->epowerlevel = SDL_JOYSTICK_POWER_MEDIUM;
slouken@12088
   410
        } else {
slouken@12088
   411
            joystick->epowerlevel = SDL_JOYSTICK_POWER_FULL;
slouken@12088
   412
        }
slouken@12088
   413
    }
slouken@12088
   414
slouken@12088
   415
    SDL_memcpy(&ctx->last_state, packet, sizeof(ctx->last_state));
slouken@12088
   416
}
slouken@12088
   417
slouken@12111
   418
static SDL_bool
slouken@12896
   419
HIDAPI_DriverPS4_Update(SDL_Joystick *joystick, hid_device *dev, void *context)
slouken@12088
   420
{
slouken@12896
   421
    SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)context;
slouken@12088
   422
    Uint8 data[USB_PACKET_LENGTH];
slouken@12088
   423
    int size;
slouken@12088
   424
slouken@12896
   425
    while ((size = hid_read_timeout(dev, data, sizeof(data), 0)) > 0) {
slouken@12088
   426
        switch (data[0]) {
slouken@12088
   427
        case k_EPS4ReportIdUsbState:
slouken@12896
   428
            HIDAPI_DriverPS4_HandleStatePacket(joystick, dev, ctx, (PS4StatePacket_t *)&data[1]);
slouken@12088
   429
            break;
slouken@12088
   430
        case k_EPS4ReportIdBluetoothState:
slouken@12088
   431
            /* Bluetooth state packets have two additional bytes at the beginning */
slouken@12896
   432
            HIDAPI_DriverPS4_HandleStatePacket(joystick, dev, ctx, (PS4StatePacket_t *)&data[3]);
slouken@12088
   433
            break;
slouken@12088
   434
        default:
slouken@12088
   435
#ifdef DEBUG_JOYSTICK
slouken@12088
   436
            SDL_Log("Unknown PS4 packet: 0x%.2x\n", data[0]);
slouken@12088
   437
#endif
slouken@12088
   438
            break;
slouken@12088
   439
        }
slouken@12088
   440
    }
slouken@12088
   441
slouken@12088
   442
    if (ctx->rumble_expiration) {
slouken@12088
   443
        Uint32 now = SDL_GetTicks();
slouken@12088
   444
        if (SDL_TICKS_PASSED(now, ctx->rumble_expiration)) {
slouken@12896
   445
            HIDAPI_DriverPS4_Rumble(joystick, dev, context, 0, 0, 0);
slouken@12088
   446
        }
slouken@12088
   447
    }
slouken@12111
   448
slouken@12163
   449
    return (size >= 0);
slouken@12088
   450
}
slouken@12088
   451
slouken@12896
   452
static void
slouken@12896
   453
HIDAPI_DriverPS4_Quit(SDL_Joystick *joystick, hid_device *dev, void *context)
slouken@12896
   454
{
slouken@12896
   455
    SDL_free(context);
slouken@12896
   456
}
slouken@12896
   457
slouken@12088
   458
SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS4 =
slouken@12088
   459
{
slouken@12088
   460
    SDL_HINT_JOYSTICK_HIDAPI_PS4,
slouken@12088
   461
    SDL_TRUE,
slouken@12088
   462
    HIDAPI_DriverPS4_IsSupportedDevice,
slouken@12088
   463
    HIDAPI_DriverPS4_GetDeviceName,
slouken@12896
   464
    HIDAPI_DriverPS4_Init,
slouken@12896
   465
    HIDAPI_DriverPS4_Rumble,
slouken@12896
   466
    HIDAPI_DriverPS4_Update,
slouken@12896
   467
    HIDAPI_DriverPS4_Quit
slouken@12088
   468
};
slouken@12088
   469
slouken@12088
   470
#endif /* SDL_JOYSTICK_HIDAPI_PS4 */
slouken@12088
   471
slouken@12088
   472
#endif /* SDL_JOYSTICK_HIDAPI */
slouken@12088
   473
slouken@12088
   474
/* vi: set ts=4 sw=4 expandtab: */